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