Merge branch 'master' into 0.11
[platform/upstream/gst-plugins-good.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 #define gst_rtp_dv_depay_parent_class parent_class
84 G_DEFINE_TYPE (GstRTPDVDepay, gst_rtp_dv_depay, GST_TYPE_BASE_RTP_DEPAYLOAD);
85
86
87 static void
88 gst_rtp_dv_depay_class_init (GstRTPDVDepayClass * klass)
89 {
90   GstElementClass *gstelement_class = (GstElementClass *) klass;
91   GstBaseRTPDepayloadClass *gstbasertpdepayload_class =
92       (GstBaseRTPDepayloadClass *) klass;
93
94   GST_DEBUG_CATEGORY_INIT (rtpdvdepay_debug, "rtpdvdepay", 0,
95       "DV RTP Depayloader");
96
97   gstelement_class->change_state =
98       GST_DEBUG_FUNCPTR (gst_rtp_dv_depay_change_state);
99
100   gst_element_class_add_pad_template (gstelement_class,
101       gst_static_pad_template_get (&src_factory));
102   gst_element_class_add_pad_template (gstelement_class,
103       gst_static_pad_template_get (&sink_factory));
104
105   gst_element_class_set_details_simple (gstelement_class, "RTP DV Depayloader",
106       "Codec/Depayloader/Network/RTP",
107       "Depayloads DV from RTP packets (RFC 3189)",
108       "Marcel Moreaux <marcelm@spacelabs.nl>, Wim Taymans <wim.taymans@gmail.com>");
109
110   gstbasertpdepayload_class->process =
111       GST_DEBUG_FUNCPTR (gst_rtp_dv_depay_process);
112   gstbasertpdepayload_class->set_caps =
113       GST_DEBUG_FUNCPTR (gst_rtp_dv_depay_setcaps);
114 }
115
116 /* initialize the new element
117  * instantiate pads and add them to element
118  * set functions
119  * initialize structure
120  */
121 static void
122 gst_rtp_dv_depay_init (GstRTPDVDepay * filter)
123 {
124 }
125
126 static gboolean
127 parse_encode (GstRTPDVDepay * rtpdvdepay, const gchar * encode)
128 {
129   rtpdvdepay->width = 720;
130   if (!strcmp (encode, "314M-25/525-60")) {
131     rtpdvdepay->frame_size = 240000;
132     rtpdvdepay->height = 480;
133     rtpdvdepay->rate_num = 30000;
134     rtpdvdepay->rate_denom = 1001;
135   } else if (!strcmp (encode, "SD-VCR/525-60")) {
136     rtpdvdepay->frame_size = 120000;
137     rtpdvdepay->height = 480;
138     rtpdvdepay->rate_num = 30000;
139     rtpdvdepay->rate_denom = 1001;
140   } else if (!strcmp (encode, "314M-50/625-50")) {
141     rtpdvdepay->frame_size = 288000;
142     rtpdvdepay->height = 576;
143     rtpdvdepay->rate_num = 25;
144     rtpdvdepay->rate_denom = 1;
145   } else if (!strcmp (encode, "SD-VCR/625-50")) {
146     rtpdvdepay->frame_size = 144000;
147     rtpdvdepay->height = 576;
148     rtpdvdepay->rate_num = 25;
149     rtpdvdepay->rate_denom = 1;
150   } else if (!strcmp (encode, "314M-25/625-50")) {
151     rtpdvdepay->frame_size = 144000;
152     rtpdvdepay->height = 576;
153     rtpdvdepay->rate_num = 25;
154     rtpdvdepay->rate_denom = 1;
155   } else
156     rtpdvdepay->frame_size = -1;
157
158   return rtpdvdepay->frame_size != -1;
159 }
160
161 static gboolean
162 gst_rtp_dv_depay_setcaps (GstBaseRTPDepayload * depayload, GstCaps * caps)
163 {
164   GstStructure *structure;
165   GstRTPDVDepay *rtpdvdepay;
166   GstCaps *srccaps;
167   gint clock_rate;
168   gboolean systemstream, ret;
169   const gchar *encode, *media;
170   guint8 *data;
171   gsize size;
172
173   rtpdvdepay = GST_RTP_DV_DEPAY (depayload);
174
175   structure = gst_caps_get_structure (caps, 0);
176
177   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
178     clock_rate = 90000;         /* default */
179   depayload->clock_rate = clock_rate;
180
181   /* we really need the encode property to figure out the frame size, it's also
182    * required by the spec */
183   if (!(encode = gst_structure_get_string (structure, "encode")))
184     goto no_encode;
185
186   /* figure out the size of one frame */
187   if (!parse_encode (rtpdvdepay, encode))
188     goto unknown_encode;
189
190   /* check the media, this tells us that the stream has video or not */
191   if (!(media = gst_structure_get_string (structure, "media")))
192     goto no_media;
193
194   systemstream = FALSE;
195
196   if (!strcmp (media, "audio")) {
197     /* we need a demuxer for audio only */
198     systemstream = TRUE;
199   } else if (!strcmp (media, "video")) {
200     const gchar *audio;
201
202     /* check the optional audio field, if it's present and set to bundled, we
203      * are dealing with a system stream. */
204     if ((audio = gst_structure_get_string (structure, "audio"))) {
205       if (!strcmp (audio, "bundled"))
206         systemstream = TRUE;
207     }
208   }
209
210   /* allocate accumulator */
211   rtpdvdepay->acc = gst_buffer_new_and_alloc (rtpdvdepay->frame_size);
212
213   /* Initialize the new accumulator frame.
214    * If the previous frame exists, copy that into the accumulator frame.
215    * This way, missing packets in the stream won't show up badly. */
216   data = gst_buffer_map (rtpdvdepay->acc, &size, NULL, GST_MAP_WRITE);
217   memset (data, 0, rtpdvdepay->frame_size);
218   gst_buffer_unmap (rtpdvdepay->acc, data, size);
219
220   srccaps = gst_caps_new_simple ("video/x-dv",
221       "systemstream", G_TYPE_BOOLEAN, systemstream,
222       "width", G_TYPE_INT, rtpdvdepay->width,
223       "height", G_TYPE_INT, rtpdvdepay->height,
224       "framerate", GST_TYPE_FRACTION, rtpdvdepay->rate_num,
225       rtpdvdepay->rate_denom, NULL);
226   ret = gst_pad_set_caps (depayload->srcpad, srccaps);
227   gst_caps_unref (srccaps);
228
229   return ret;
230
231   /* ERRORS */
232 no_encode:
233   {
234     GST_ERROR_OBJECT (rtpdvdepay, "required encode property not found in caps");
235     return FALSE;
236   }
237 unknown_encode:
238   {
239     GST_ERROR_OBJECT (rtpdvdepay, "unknown encode property %s found", encode);
240     return FALSE;
241   }
242 no_media:
243   {
244     GST_ERROR_OBJECT (rtpdvdepay, "required media property not found in caps");
245     return FALSE;
246   }
247 }
248
249 /* A DV frame consists of a bunch of 80-byte DIF blocks.
250  * Each DIF block contains a 3-byte header telling where in the DV frame the
251  * DIF block should go. We use this information to calculate its position.
252  */
253 static guint
254 calculate_difblock_location (guint8 * block)
255 {
256   gint block_type, dif_sequence, dif_block;
257   guint location;
258
259   block_type = block[0] >> 5;
260   dif_sequence = block[1] >> 4;
261   dif_block = block[2];
262
263   location = dif_sequence * 150;
264
265   switch (block_type) {
266     case 0:                    /* Header block, no offset */
267       break;
268     case 1:                    /* Subcode block */
269       location += (1 + dif_block);
270       break;
271     case 2:                    /* VAUX block */
272       location += (3 + dif_block);
273       break;
274     case 3:                    /* Audio block */
275       location += (6 + dif_block * 16);
276       break;
277     case 4:                    /* Video block */
278       location += (7 + (dif_block / 15) + dif_block);
279       break;
280     default:                   /* Something bogus */
281       GST_DEBUG ("UNKNOWN BLOCK");
282       location = -1;
283       break;
284   }
285   return location;
286 }
287
288 /* Process one RTP packet. Accumulate RTP payload in the proper place in a DV
289  * frame, and return that frame if we detect a new frame, or NULL otherwise.
290  * We assume a DV frame is 144000 bytes. That should accomodate PAL as well as
291  * NTSC.
292  */
293 static GstBuffer *
294 gst_rtp_dv_depay_process (GstBaseRTPDepayload * base, GstBuffer * in)
295 {
296   GstBuffer *out = NULL;
297   guint8 *payload;
298   guint32 rtp_ts;
299   guint payload_len, location;
300   GstRTPDVDepay *dvdepay = GST_RTP_DV_DEPAY (base);
301   gboolean marker;
302   GstRTPBuffer rtp = { NULL, };
303
304   gst_rtp_buffer_map (in, GST_MAP_READ, &rtp);
305
306   marker = gst_rtp_buffer_get_marker (&rtp);
307
308   /* Check if the received packet contains (the start of) a new frame, we do
309    * this by checking the RTP timestamp. */
310   rtp_ts = gst_rtp_buffer_get_timestamp (&rtp);
311
312   /* we cannot copy the packet yet if the marker is set, we will do that below
313    * after taking out the data */
314   if (dvdepay->prev_ts != -1 && rtp_ts != dvdepay->prev_ts && !marker) {
315     /* the timestamp changed */
316     GST_DEBUG_OBJECT (dvdepay, "new frame with ts %u, old ts %u", rtp_ts,
317         dvdepay->prev_ts);
318
319     /* return copy of accumulator. */
320     out = gst_buffer_copy (dvdepay->acc);
321   }
322
323   /* Extract the payload */
324   payload_len = gst_rtp_buffer_get_payload_len (&rtp);
325   payload = gst_rtp_buffer_get_payload (&rtp);
326
327   /* copy all DIF chunks in their place. */
328   while (payload_len >= 80) {
329     guint offset;
330
331     /* Calculate where in the frame the payload should go */
332     location = calculate_difblock_location (payload);
333
334     if (location < 6) {
335       /* part of a header, set the flag to mark that we have the header. */
336       dvdepay->header_mask |= (1 << location);
337       GST_LOG_OBJECT (dvdepay, "got header at location %d, now %02x", location,
338           dvdepay->header_mask);
339     } else {
340       GST_LOG_OBJECT (dvdepay, "got block at location %d", location);
341     }
342
343     /* get the byte offset of the dif block */
344     offset = location * 80;
345
346     /* And copy it in, provided the location is sane. */
347     if (offset >= 0 && offset <= dvdepay->frame_size - 80)
348       gst_buffer_fill (dvdepay->acc, offset, payload, 80);
349
350     payload += 80;
351     payload_len -= 80;
352   }
353   gst_rtp_buffer_unmap (&rtp);
354
355   if (marker) {
356     GST_DEBUG_OBJECT (dvdepay, "marker bit complete frame %u", rtp_ts);
357     /* only copy the frame when we have a complete header */
358     if (dvdepay->header_mask == 0x3f) {
359       /* The marker marks the end of a frame that we need to push. The next frame
360        * will change the timestamp but we won't copy the accumulator again because
361        * we set the prev_ts to -1. */
362       out = gst_buffer_copy (dvdepay->acc);
363     } else {
364       GST_WARNING_OBJECT (dvdepay, "waiting for frame headers %02x",
365           dvdepay->header_mask);
366     }
367     dvdepay->prev_ts = -1;
368   } else {
369     /* save last timestamp */
370     dvdepay->prev_ts = rtp_ts;
371   }
372   return out;
373 }
374
375 static void
376 gst_rtp_dv_depay_reset (GstRTPDVDepay * depay)
377 {
378   if (depay->acc)
379     gst_buffer_unref (depay->acc);
380   depay->acc = NULL;
381
382   depay->prev_ts = -1;
383   depay->header_mask = 0;
384 }
385
386 static GstStateChangeReturn
387 gst_rtp_dv_depay_change_state (GstElement * element, GstStateChange transition)
388 {
389   GstStateChangeReturn ret;
390   GstRTPDVDepay *depay = GST_RTP_DV_DEPAY (element);
391
392   switch (transition) {
393     case GST_STATE_CHANGE_READY_TO_PAUSED:
394       gst_rtp_dv_depay_reset (depay);
395       break;
396     default:
397       break;
398   }
399
400   ret = GST_CALL_PARENT_WITH_DEFAULT (GST_ELEMENT_CLASS, change_state,
401       (element, transition), GST_STATE_CHANGE_FAILURE);
402
403   switch (transition) {
404     case GST_STATE_CHANGE_PAUSED_TO_READY:
405       gst_rtp_dv_depay_reset (depay);
406       break;
407     default:
408       break;
409   }
410   return ret;
411 }
412
413 gboolean
414 gst_rtp_dv_depay_plugin_init (GstPlugin * plugin)
415 {
416   return gst_element_register (plugin, "rtpdvdepay",
417       GST_RANK_SECONDARY, GST_TYPE_RTP_DV_DEPAY);
418 }