Merge branch 'plugin-move-rtp-opus'
[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_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_static_metadata (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   gstrtpbasedepayload_class->process_rtp_packet =
111       GST_DEBUG_FUNCPTR (gst_rtp_dv_depay_process);
112   gstrtpbasedepayload_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 (GstRTPBaseDepayload * 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
171   rtpdvdepay = GST_RTP_DV_DEPAY (depayload);
172
173   structure = gst_caps_get_structure (caps, 0);
174
175   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
176     clock_rate = 90000;         /* default */
177   depayload->clock_rate = clock_rate;
178
179   /* we really need the encode property to figure out the frame size, it's also
180    * required by the spec */
181   if (!(encode = gst_structure_get_string (structure, "encode")))
182     goto no_encode;
183
184   /* figure out the size of one frame */
185   if (!parse_encode (rtpdvdepay, encode))
186     goto unknown_encode;
187
188   /* check the media, this tells us that the stream has video or not */
189   if (!(media = gst_structure_get_string (structure, "media")))
190     goto no_media;
191
192   systemstream = FALSE;
193
194   if (!strcmp (media, "audio")) {
195     /* we need a demuxer for audio only */
196     systemstream = TRUE;
197   } else if (!strcmp (media, "video")) {
198     const gchar *audio;
199
200     /* check the optional audio field, if it's present and set to bundled, we
201      * are dealing with a system stream. */
202     if ((audio = gst_structure_get_string (structure, "audio"))) {
203       if (!strcmp (audio, "bundled"))
204         systemstream = TRUE;
205     }
206   }
207
208   /* allocate accumulator */
209   rtpdvdepay->acc = gst_buffer_new_and_alloc (rtpdvdepay->frame_size);
210
211   /* Initialize the new accumulator frame.
212    * If the previous frame exists, copy that into the accumulator frame.
213    * This way, missing packets in the stream won't show up badly. */
214   gst_buffer_memset (rtpdvdepay->acc, 0, 0, rtpdvdepay->frame_size);
215
216   srccaps = gst_caps_new_simple ("video/x-dv",
217       "systemstream", G_TYPE_BOOLEAN, systemstream,
218       "width", G_TYPE_INT, rtpdvdepay->width,
219       "height", G_TYPE_INT, rtpdvdepay->height,
220       "framerate", GST_TYPE_FRACTION, rtpdvdepay->rate_num,
221       rtpdvdepay->rate_denom, NULL);
222   ret = gst_pad_set_caps (depayload->srcpad, srccaps);
223   gst_caps_unref (srccaps);
224
225   return ret;
226
227   /* ERRORS */
228 no_encode:
229   {
230     GST_ERROR_OBJECT (rtpdvdepay, "required encode property not found in caps");
231     return FALSE;
232   }
233 unknown_encode:
234   {
235     GST_ERROR_OBJECT (rtpdvdepay, "unknown encode property %s found", encode);
236     return FALSE;
237   }
238 no_media:
239   {
240     GST_ERROR_OBJECT (rtpdvdepay, "required media property not found in caps");
241     return FALSE;
242   }
243 }
244
245 /* A DV frame consists of a bunch of 80-byte DIF blocks.
246  * Each DIF block contains a 3-byte header telling where in the DV frame the
247  * DIF block should go. We use this information to calculate its position.
248  */
249 static guint
250 calculate_difblock_location (guint8 * block)
251 {
252   gint block_type, dif_sequence, dif_block;
253   guint location;
254
255   block_type = block[0] >> 5;
256   dif_sequence = block[1] >> 4;
257   dif_block = block[2];
258
259   location = dif_sequence * 150;
260
261   switch (block_type) {
262     case 0:                    /* Header block, no offset */
263       break;
264     case 1:                    /* Subcode block */
265       location += (1 + dif_block);
266       break;
267     case 2:                    /* VAUX block */
268       location += (3 + dif_block);
269       break;
270     case 3:                    /* Audio block */
271       location += (6 + dif_block * 16);
272       break;
273     case 4:                    /* Video block */
274       location += (7 + (dif_block / 15) + dif_block);
275       break;
276     default:                   /* Something bogus */
277       GST_DEBUG ("UNKNOWN BLOCK");
278       location = -1;
279       break;
280   }
281   return location;
282 }
283
284 static gboolean
285 foreach_metadata_drop (GstBuffer * inbuf, GstMeta ** meta, gpointer user_data)
286 {
287   *meta = NULL;
288   return TRUE;
289 }
290
291 /* Process one RTP packet. Accumulate RTP payload in the proper place in a DV
292  * frame, and return that frame if we detect a new frame, or NULL otherwise.
293  * We assume a DV frame is 144000 bytes. That should accomodate PAL as well as
294  * NTSC.
295  */
296 static GstBuffer *
297 gst_rtp_dv_depay_process (GstRTPBaseDepayload * base, GstRTPBuffer * rtp)
298 {
299   GstBuffer *out = NULL;
300   guint8 *payload;
301   guint32 rtp_ts;
302   guint payload_len, location;
303   GstRTPDVDepay *dvdepay = GST_RTP_DV_DEPAY (base);
304   gboolean marker;
305   GstMapInfo map;
306
307   marker = gst_rtp_buffer_get_marker (rtp);
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 (rtp);
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     gst_buffer_foreach_meta (dvdepay->acc, foreach_metadata_drop, NULL);
323   }
324
325   /* Extract the payload */
326   payload_len = gst_rtp_buffer_get_payload_len (rtp);
327   payload = gst_rtp_buffer_get_payload (rtp);
328
329   /* copy all DIF chunks in their place. */
330   gst_buffer_map (dvdepay->acc, &map, GST_MAP_READWRITE);
331   while (payload_len >= 80) {
332     guint offset;
333
334     /* Calculate where in the frame the payload should go */
335     location = calculate_difblock_location (payload);
336
337     if (location < 6) {
338       /* part of a header, set the flag to mark that we have the header. */
339       dvdepay->header_mask |= (1 << location);
340       GST_LOG_OBJECT (dvdepay, "got header at location %d, now %02x", location,
341           dvdepay->header_mask);
342     } else {
343       GST_LOG_OBJECT (dvdepay, "got block at location %d", location);
344     }
345
346     if (location != -1) {
347       /* get the byte offset of the dif block */
348       offset = location * 80;
349
350       /* And copy it in, provided the location is sane. */
351       if (offset <= dvdepay->frame_size - 80) {
352         memcpy (map.data + offset, payload, 80);
353         gst_rtp_copy_meta (GST_ELEMENT_CAST (dvdepay), dvdepay->acc,
354             rtp->buffer, 0);
355       }
356     }
357
358     payload += 80;
359     payload_len -= 80;
360   }
361   gst_buffer_unmap (dvdepay->acc, &map);
362
363   if (marker) {
364     GST_DEBUG_OBJECT (dvdepay, "marker bit complete frame %u", rtp_ts);
365     /* only copy the frame when we have a complete header */
366     if (dvdepay->header_mask == 0x3f) {
367       /* The marker marks the end of a frame that we need to push. The next frame
368        * will change the timestamp but we won't copy the accumulator again because
369        * we set the prev_ts to -1. */
370       out = gst_buffer_copy (dvdepay->acc);
371       gst_buffer_foreach_meta (dvdepay->acc, foreach_metadata_drop, NULL);
372     } else {
373       GST_WARNING_OBJECT (dvdepay, "waiting for frame headers %02x",
374           dvdepay->header_mask);
375     }
376     dvdepay->prev_ts = -1;
377   } else {
378     /* save last timestamp */
379     dvdepay->prev_ts = rtp_ts;
380   }
381   return out;
382 }
383
384 static void
385 gst_rtp_dv_depay_reset (GstRTPDVDepay * depay)
386 {
387   if (depay->acc)
388     gst_buffer_unref (depay->acc);
389   depay->acc = NULL;
390
391   depay->prev_ts = -1;
392   depay->header_mask = 0;
393 }
394
395 static GstStateChangeReturn
396 gst_rtp_dv_depay_change_state (GstElement * element, GstStateChange transition)
397 {
398   GstStateChangeReturn ret;
399   GstRTPDVDepay *depay = GST_RTP_DV_DEPAY (element);
400
401   switch (transition) {
402     case GST_STATE_CHANGE_READY_TO_PAUSED:
403       gst_rtp_dv_depay_reset (depay);
404       break;
405     default:
406       break;
407   }
408
409   ret = GST_CALL_PARENT_WITH_DEFAULT (GST_ELEMENT_CLASS, change_state,
410       (element, transition), GST_STATE_CHANGE_FAILURE);
411
412   switch (transition) {
413     case GST_STATE_CHANGE_PAUSED_TO_READY:
414       gst_rtp_dv_depay_reset (depay);
415       break;
416     default:
417       break;
418   }
419   return ret;
420 }
421
422 gboolean
423 gst_rtp_dv_depay_plugin_init (GstPlugin * plugin)
424 {
425   return gst_element_register (plugin, "rtpdvdepay",
426       GST_RANK_SECONDARY, GST_TYPE_RTP_DV_DEPAY);
427 }