Remove unused variables in _class_init
[platform/upstream/gstreamer.git] / gst / rtp / gstrtpdvdepay.c
1 /* Farsight
2  * Copyright (C) 2006 Marcel Moreaux <marcelm@spacelabs.nl>
3  *           (C) 2008 Wim Taymans <wim.taymans@gmail.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 /*
22  * RTP DV depayloader.
23  *
24  * Important note for NTSC-users:
25  *
26  * Because the author uses PAL video, and he does not have proper DV
27  * documentation (the DV format specification is not freely available),
28  * this code may very well contain PAL-specific assumptions.
29  */
30
31 #include <stdlib.h>
32 #include <string.h>
33 #include <gst/gst.h>
34
35 #include "gstrtpdvdepay.h"
36
37 GST_DEBUG_CATEGORY (rtpdvdepay_debug);
38 #define GST_CAT_DEFAULT (rtpdvdepay_debug)
39 /* Filter signals and args */
40 enum
41 {
42   /* FILL ME */
43   LAST_SIGNAL
44 };
45
46 enum
47 {
48   ARG_0,
49 };
50
51 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
52     GST_PAD_SRC,
53     GST_PAD_ALWAYS,
54     GST_STATIC_CAPS ("video/x-dv")
55     );
56
57 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
58     GST_PAD_SINK,
59     GST_PAD_ALWAYS,
60     GST_STATIC_CAPS ("application/x-rtp, "
61         "media = (string) { \"video\", \"audio\" },"
62         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
63         "encoding-name = (string) \"DV\", "
64         "clock-rate = (int) 90000,"
65         "encode = (string) { \"SD-VCR/525-60\", \"SD-VCR/625-50\", \"HD-VCR/1125-60\","
66         "\"HD-VCR/1250-50\", \"SDL-VCR/525-60\", \"SDL-VCR/625-50\","
67         "\"306M/525-60\", \"306M/625-50\", \"314M-25/525-60\","
68         "\"314M-25/625-50\", \"314M-50/525-60\", \"314M-50/625-50\" }"
69         /* optional parameters can't go in the template
70          * "audio = (string) { \"bundled\", \"none\" }"
71          */
72     )
73     );
74
75 static GstStateChangeReturn
76 gst_rtp_dv_depay_change_state (GstElement * element, GstStateChange transition);
77
78 static GstBuffer *gst_rtp_dv_depay_process (GstBaseRTPDepayload * base,
79     GstBuffer * in);
80 static gboolean gst_rtp_dv_depay_setcaps (GstBaseRTPDepayload * depayload,
81     GstCaps * caps);
82
83 GST_BOILERPLATE (GstRTPDVDepay, gst_rtp_dv_depay, GstBaseRTPDepayload,
84     GST_TYPE_BASE_RTP_DEPAYLOAD)
85
86      static void gst_rtp_dv_depay_base_init (gpointer g_class)
87 {
88   static GstElementDetails plugin_details = {
89     "RTP DV Depayloader",
90     "Codec/Depayloader/Network",
91     "Depayloads DV from RTP packets (RFC 3189)",
92     "Marcel Moreaux <marcelm@spacelabs.nl>, Wim Taymans <wim.taymans@gmail.com>"
93   };
94   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
95
96   gst_element_class_add_pad_template (element_class,
97       gst_static_pad_template_get (&src_factory));
98   gst_element_class_add_pad_template (element_class,
99       gst_static_pad_template_get (&sink_factory));
100
101   gst_element_class_set_details (element_class, &plugin_details);
102 }
103
104 /* initialize the plugin's class */
105 static void
106 gst_rtp_dv_depay_class_init (GstRTPDVDepayClass * klass)
107 {
108   GstElementClass *gstelement_class = (GstElementClass *) klass;
109   GstBaseRTPDepayloadClass *gstbasertpdepayload_class =
110       (GstBaseRTPDepayloadClass *) klass;
111
112   gstelement_class->change_state =
113       GST_DEBUG_FUNCPTR (gst_rtp_dv_depay_change_state);
114
115   gstbasertpdepayload_class->process =
116       GST_DEBUG_FUNCPTR (gst_rtp_dv_depay_process);
117   gstbasertpdepayload_class->set_caps =
118       GST_DEBUG_FUNCPTR (gst_rtp_dv_depay_setcaps);
119
120   GST_DEBUG_CATEGORY_INIT (rtpdvdepay_debug, "rtpdvdepay", 0,
121       "DV RTP Depayloader");
122 }
123
124 /* initialize the new element
125  * instantiate pads and add them to element
126  * set functions
127  * initialize structure
128  */
129 static void
130 gst_rtp_dv_depay_init (GstRTPDVDepay * filter, GstRTPDVDepayClass * klass)
131 {
132 }
133
134 static gboolean
135 parse_encode (GstRTPDVDepay * rtpdvdepay, const gchar * encode)
136 {
137   rtpdvdepay->width = 720;
138   if (!strcmp (encode, "314M-25/525-60")) {
139     rtpdvdepay->frame_size = 240000;
140     rtpdvdepay->height = 480;
141     rtpdvdepay->rate_num = 30000;
142     rtpdvdepay->rate_denom = 1001;
143   } else if (!strcmp (encode, "SD-VCR/525-60")) {
144     rtpdvdepay->frame_size = 120000;
145     rtpdvdepay->height = 480;
146     rtpdvdepay->rate_num = 30000;
147     rtpdvdepay->rate_denom = 1001;
148   } else if (!strcmp (encode, "314M-50/625-50")) {
149     rtpdvdepay->frame_size = 288000;
150     rtpdvdepay->height = 576;
151     rtpdvdepay->rate_num = 25;
152     rtpdvdepay->rate_denom = 1;
153   } else if (!strcmp (encode, "SD-VCR/625-50")) {
154     rtpdvdepay->frame_size = 144000;
155     rtpdvdepay->height = 576;
156     rtpdvdepay->rate_num = 25;
157     rtpdvdepay->rate_denom = 1;
158   } else if (!strcmp (encode, "314M-25/625-50")) {
159     rtpdvdepay->frame_size = 144000;
160     rtpdvdepay->height = 576;
161     rtpdvdepay->rate_num = 25;
162     rtpdvdepay->rate_denom = 1;
163   } else
164     rtpdvdepay->frame_size = -1;
165
166   return rtpdvdepay->frame_size != -1;
167 }
168
169 static gboolean
170 gst_rtp_dv_depay_setcaps (GstBaseRTPDepayload * depayload, GstCaps * caps)
171 {
172   GstStructure *structure;
173   GstRTPDVDepay *rtpdvdepay;
174   GstCaps *srccaps;
175   gint clock_rate;
176   gboolean systemstream, ret;
177   const gchar *encode, *media;
178
179   rtpdvdepay = GST_RTP_DV_DEPAY (depayload);
180
181   structure = gst_caps_get_structure (caps, 0);
182
183   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
184     clock_rate = 90000;         /* default */
185   depayload->clock_rate = clock_rate;
186
187   /* we really need the encode property to figure out the frame size, it's also
188    * required by the spec */
189   if (!(encode = gst_structure_get_string (structure, "encode")))
190     goto no_encode;
191
192   /* figure out the size of one frame */
193   if (!parse_encode (rtpdvdepay, encode))
194     goto unknown_encode;
195
196   /* check the media, this tells us that the stream has video or not */
197   if (!(media = gst_structure_get_string (structure, "media")))
198     goto no_media;
199
200   systemstream = FALSE;
201
202   if (!strcmp (media, "audio")) {
203     /* we need a demuxer for audio only */
204     systemstream = TRUE;
205   } else if (!strcmp (media, "video")) {
206     const gchar *audio;
207
208     /* check the optional audio field, if it's present and set to bundled, we
209      * are dealing with a system stream. */
210     if ((audio = gst_structure_get_string (structure, "audio"))) {
211       if (!strcmp (audio, "bundled"))
212         systemstream = TRUE;
213     }
214   }
215
216   /* allocate accumulator */
217   rtpdvdepay->acc = gst_buffer_new_and_alloc (rtpdvdepay->frame_size);
218
219   /* Initialize the new accumulator frame.
220    * If the previous frame exists, copy that into the accumulator frame.
221    * This way, missing packets in the stream won't show up badly. */
222   memset (GST_BUFFER_DATA (rtpdvdepay->acc), 0, rtpdvdepay->frame_size);
223
224   srccaps = gst_caps_new_simple ("video/x-dv",
225       "systemstream", G_TYPE_BOOLEAN, systemstream,
226       "width", G_TYPE_INT, rtpdvdepay->width,
227       "height", G_TYPE_INT, rtpdvdepay->height,
228       "framerate", GST_TYPE_FRACTION, rtpdvdepay->rate_num,
229       rtpdvdepay->rate_denom, NULL);
230   ret = gst_pad_set_caps (depayload->srcpad, srccaps);
231   gst_caps_unref (srccaps);
232
233   return ret;
234
235   /* ERRORS */
236 no_encode:
237   {
238     GST_ERROR_OBJECT (rtpdvdepay, "required encode property not found in caps");
239     return FALSE;
240   }
241 unknown_encode:
242   {
243     GST_ERROR_OBJECT (rtpdvdepay, "unknown encode property %s found", encode);
244     return FALSE;
245   }
246 no_media:
247   {
248     GST_ERROR_OBJECT (rtpdvdepay, "required media property not found in caps");
249     return FALSE;
250   }
251 }
252
253 /* A DV frame consists of a bunch of 80-byte DIF blocks.
254  * Each DIF block contains a 3-byte header telling where in the DV frame the
255  * DIF block should go. We use this information to calculate its position.
256  */
257 static guint
258 calculate_difblock_location (guint8 * block)
259 {
260   gint block_type, dif_sequence, dif_block;
261   guint location;
262
263   block_type = block[0] >> 5;
264   dif_sequence = block[1] >> 4;
265   dif_block = block[2];
266
267   switch (block_type) {
268     case 0:                    /* Header block */
269       location = dif_sequence * 150 * 80;
270       break;
271     case 1:                    /* Subcode block */
272       location = dif_sequence * 150 * 80 + (1 + dif_block) * 80;
273       break;
274     case 2:                    /* VAUX block */
275       location = dif_sequence * 150 * 80 + (3 + dif_block) * 80;
276       break;
277     case 3:                    /* Audio block */
278       location = dif_sequence * 150 * 80 + (6 + dif_block * 16) * 80;
279       break;
280     case 4:                    /* Video block */
281       location = dif_sequence * 150 * 80 +
282           (7 + (dif_block / 15) + dif_block) * 80;
283       break;
284     default:                   /* Something bogus */
285       GST_DEBUG ("UNKNOWN BLOCK");
286       location = -1;
287       break;
288   }
289   return location;
290 }
291
292 /* Process one RTP packet. Accumulate RTP payload in the proper place in a DV
293  * frame, and return that frame if we detect a new frame, or NULL otherwise.
294  * We assume a DV frame is 144000 bytes. That should accomodate PAL as well as
295  * NTSC.
296  */
297 static GstBuffer *
298 gst_rtp_dv_depay_process (GstBaseRTPDepayload * base, GstBuffer * in)
299 {
300   GstBuffer *out = NULL;
301   guint8 *payload;
302   guint32 rtp_ts;
303   guint payload_len, location;
304   GstRTPDVDepay *dvdepay = GST_RTP_DV_DEPAY (base);
305   gboolean marker;
306
307   marker = gst_rtp_buffer_get_marker (in);
308
309   /* Check if the received packet contains (the start of) a new frame, we do
310    * this by checking the RTP timestamp. */
311   rtp_ts = gst_rtp_buffer_get_timestamp (in);
312
313   /* we cannot copy the packet yet if the marker is set, we will do that below
314    * after taking out the data */
315   if (dvdepay->prev_ts != -1 && rtp_ts != dvdepay->prev_ts && !marker) {
316     /* the timestamp changed */
317     GST_DEBUG_OBJECT (dvdepay, "new frame with ts %u, old ts %u", rtp_ts,
318         dvdepay->prev_ts);
319
320     /* return copy of accumulator. */
321     out = gst_buffer_copy (dvdepay->acc);
322   }
323
324   /* Extract the payload */
325   payload_len = gst_rtp_buffer_get_payload_len (in);
326   payload = gst_rtp_buffer_get_payload (in);
327
328   /* copy all DIF chunks in their place. */
329   while (payload_len >= 80) {
330     /* Calculate where in the frame the payload should go */
331     location = calculate_difblock_location (payload);
332
333     /* Check if we received a header. We will not pass on frames until
334      * we've received a header, otherwise the DV decoder goes wacko. */
335     if (location == 0)
336       dvdepay->have_header = TRUE;
337
338     /* And copy it in, provided the location is sane. */
339     if (location >= 0 && location <= dvdepay->frame_size - 80)
340       memcpy (GST_BUFFER_DATA (dvdepay->acc) + location, payload, 80);
341
342     payload += 80;
343     payload_len -= 80;
344   }
345
346   if (marker) {
347     /* The marker marks the end of a frame that we need to push. The next frame
348      * will change the timestamp but we won't copy the accumulator again because
349      * we set the prev_ts to -1. */
350     out = gst_buffer_copy (dvdepay->acc);
351     GST_DEBUG_OBJECT (dvdepay, "marker bit complete frame %u", rtp_ts);
352     dvdepay->prev_ts = -1;
353   } else {
354     /* save last timestamp */
355     dvdepay->prev_ts = rtp_ts;
356   }
357   return out;
358 }
359
360 static void
361 gst_rtp_dv_depay_reset (GstRTPDVDepay * depay)
362 {
363   if (depay->acc)
364     gst_buffer_unref (depay->acc);
365   depay->acc = NULL;
366
367   depay->prev_ts = -1;
368   depay->have_header = FALSE;
369   depay->frame_nr = 0;
370 }
371
372 static GstStateChangeReturn
373 gst_rtp_dv_depay_change_state (GstElement * element, GstStateChange transition)
374 {
375   GstStateChangeReturn ret;
376   GstRTPDVDepay *depay = GST_RTP_DV_DEPAY (element);
377
378   switch (transition) {
379     case GST_STATE_CHANGE_READY_TO_PAUSED:
380       gst_rtp_dv_depay_reset (depay);
381       break;
382     default:
383       break;
384   }
385
386   ret = GST_CALL_PARENT_WITH_DEFAULT (GST_ELEMENT_CLASS, change_state,
387       (element, transition), GST_STATE_CHANGE_FAILURE);
388
389   switch (transition) {
390     case GST_STATE_CHANGE_PAUSED_TO_READY:
391       gst_rtp_dv_depay_reset (depay);
392       break;
393     default:
394       break;
395   }
396   return ret;
397 }
398
399 gboolean
400 gst_rtp_dv_depay_plugin_init (GstPlugin * plugin)
401 {
402   return gst_element_register (plugin, "rtpdvdepay",
403       GST_RANK_NONE, GST_TYPE_RTP_DV_DEPAY);
404 }