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