rtpvrawpay: Add missing break
[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
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         "encoding-name = (string) \"DV\", "
63         "clock-rate = (int) 90000,"
64         "encode = (string) { \"SD-VCR/525-60\", \"SD-VCR/625-50\", \"HD-VCR/1125-60\","
65         "\"HD-VCR/1250-50\", \"SDL-VCR/525-60\", \"SDL-VCR/625-50\","
66         "\"306M/525-60\", \"306M/625-50\", \"314M-25/525-60\","
67         "\"314M-25/625-50\", \"314M-50/525-60\", \"314M-50/625-50\" }"
68         /* optional parameters can't go in the template
69          * "audio = (string) { \"bundled\", \"none\" }"
70          */
71     )
72     );
73
74 static GstStateChangeReturn
75 gst_rtp_dv_depay_change_state (GstElement * element, GstStateChange transition);
76
77 static GstBuffer *gst_rtp_dv_depay_process (GstRTPBaseDepayload * base,
78     GstBuffer * in);
79 static gboolean gst_rtp_dv_depay_setcaps (GstRTPBaseDepayload * depayload,
80     GstCaps * caps);
81
82 #define gst_rtp_dv_depay_parent_class parent_class
83 G_DEFINE_TYPE (GstRTPDVDepay, gst_rtp_dv_depay, GST_TYPE_RTP_BASE_DEPAYLOAD);
84
85
86 static void
87 gst_rtp_dv_depay_class_init (GstRTPDVDepayClass * klass)
88 {
89   GstElementClass *gstelement_class = (GstElementClass *) klass;
90   GstRTPBaseDepayloadClass *gstrtpbasedepayload_class =
91       (GstRTPBaseDepayloadClass *) klass;
92
93   GST_DEBUG_CATEGORY_INIT (rtpdvdepay_debug, "rtpdvdepay", 0,
94       "DV RTP Depayloader");
95
96   gstelement_class->change_state =
97       GST_DEBUG_FUNCPTR (gst_rtp_dv_depay_change_state);
98
99   gst_element_class_add_pad_template (gstelement_class,
100       gst_static_pad_template_get (&src_factory));
101   gst_element_class_add_pad_template (gstelement_class,
102       gst_static_pad_template_get (&sink_factory));
103
104   gst_element_class_set_static_metadata (gstelement_class, "RTP DV Depayloader",
105       "Codec/Depayloader/Network/RTP",
106       "Depayloads DV from RTP packets (RFC 3189)",
107       "Marcel Moreaux <marcelm@spacelabs.nl>, Wim Taymans <wim.taymans@gmail.com>");
108
109   gstrtpbasedepayload_class->process =
110       GST_DEBUG_FUNCPTR (gst_rtp_dv_depay_process);
111   gstrtpbasedepayload_class->set_caps =
112       GST_DEBUG_FUNCPTR (gst_rtp_dv_depay_setcaps);
113 }
114
115 /* initialize the new element
116  * instantiate pads and add them to element
117  * set functions
118  * initialize structure
119  */
120 static void
121 gst_rtp_dv_depay_init (GstRTPDVDepay * filter)
122 {
123 }
124
125 static gboolean
126 parse_encode (GstRTPDVDepay * rtpdvdepay, const gchar * encode)
127 {
128   rtpdvdepay->width = 720;
129   if (!strcmp (encode, "314M-25/525-60")) {
130     rtpdvdepay->frame_size = 240000;
131     rtpdvdepay->height = 480;
132     rtpdvdepay->rate_num = 30000;
133     rtpdvdepay->rate_denom = 1001;
134   } else if (!strcmp (encode, "SD-VCR/525-60")) {
135     rtpdvdepay->frame_size = 120000;
136     rtpdvdepay->height = 480;
137     rtpdvdepay->rate_num = 30000;
138     rtpdvdepay->rate_denom = 1001;
139   } else if (!strcmp (encode, "314M-50/625-50")) {
140     rtpdvdepay->frame_size = 288000;
141     rtpdvdepay->height = 576;
142     rtpdvdepay->rate_num = 25;
143     rtpdvdepay->rate_denom = 1;
144   } else if (!strcmp (encode, "SD-VCR/625-50")) {
145     rtpdvdepay->frame_size = 144000;
146     rtpdvdepay->height = 576;
147     rtpdvdepay->rate_num = 25;
148     rtpdvdepay->rate_denom = 1;
149   } else if (!strcmp (encode, "314M-25/625-50")) {
150     rtpdvdepay->frame_size = 144000;
151     rtpdvdepay->height = 576;
152     rtpdvdepay->rate_num = 25;
153     rtpdvdepay->rate_denom = 1;
154   } else
155     rtpdvdepay->frame_size = -1;
156
157   return rtpdvdepay->frame_size != -1;
158 }
159
160 static gboolean
161 gst_rtp_dv_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
162 {
163   GstStructure *structure;
164   GstRTPDVDepay *rtpdvdepay;
165   GstCaps *srccaps;
166   gint clock_rate;
167   gboolean systemstream, ret;
168   const gchar *encode, *media;
169
170   rtpdvdepay = GST_RTP_DV_DEPAY (depayload);
171
172   structure = gst_caps_get_structure (caps, 0);
173
174   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
175     clock_rate = 90000;         /* default */
176   depayload->clock_rate = clock_rate;
177
178   /* we really need the encode property to figure out the frame size, it's also
179    * required by the spec */
180   if (!(encode = gst_structure_get_string (structure, "encode")))
181     goto no_encode;
182
183   /* figure out the size of one frame */
184   if (!parse_encode (rtpdvdepay, encode))
185     goto unknown_encode;
186
187   /* check the media, this tells us that the stream has video or not */
188   if (!(media = gst_structure_get_string (structure, "media")))
189     goto no_media;
190
191   systemstream = FALSE;
192
193   if (!strcmp (media, "audio")) {
194     /* we need a demuxer for audio only */
195     systemstream = TRUE;
196   } else if (!strcmp (media, "video")) {
197     const gchar *audio;
198
199     /* check the optional audio field, if it's present and set to bundled, we
200      * are dealing with a system stream. */
201     if ((audio = gst_structure_get_string (structure, "audio"))) {
202       if (!strcmp (audio, "bundled"))
203         systemstream = TRUE;
204     }
205   }
206
207   /* allocate accumulator */
208   rtpdvdepay->acc = gst_buffer_new_and_alloc (rtpdvdepay->frame_size);
209
210   /* Initialize the new accumulator frame.
211    * If the previous frame exists, copy that into the accumulator frame.
212    * This way, missing packets in the stream won't show up badly. */
213   gst_buffer_memset (rtpdvdepay->acc, 0, 0, rtpdvdepay->frame_size);
214
215   srccaps = gst_caps_new_simple ("video/x-dv",
216       "systemstream", G_TYPE_BOOLEAN, systemstream,
217       "width", G_TYPE_INT, rtpdvdepay->width,
218       "height", G_TYPE_INT, rtpdvdepay->height,
219       "framerate", GST_TYPE_FRACTION, rtpdvdepay->rate_num,
220       rtpdvdepay->rate_denom, NULL);
221   ret = gst_pad_set_caps (depayload->srcpad, srccaps);
222   gst_caps_unref (srccaps);
223
224   return ret;
225
226   /* ERRORS */
227 no_encode:
228   {
229     GST_ERROR_OBJECT (rtpdvdepay, "required encode property not found in caps");
230     return FALSE;
231   }
232 unknown_encode:
233   {
234     GST_ERROR_OBJECT (rtpdvdepay, "unknown encode property %s found", encode);
235     return FALSE;
236   }
237 no_media:
238   {
239     GST_ERROR_OBJECT (rtpdvdepay, "required media property not found in caps");
240     return FALSE;
241   }
242 }
243
244 /* A DV frame consists of a bunch of 80-byte DIF blocks.
245  * Each DIF block contains a 3-byte header telling where in the DV frame the
246  * DIF block should go. We use this information to calculate its position.
247  */
248 static guint
249 calculate_difblock_location (guint8 * block)
250 {
251   gint block_type, dif_sequence, dif_block;
252   guint location;
253
254   block_type = block[0] >> 5;
255   dif_sequence = block[1] >> 4;
256   dif_block = block[2];
257
258   location = dif_sequence * 150;
259
260   switch (block_type) {
261     case 0:                    /* Header block, no offset */
262       break;
263     case 1:                    /* Subcode block */
264       location += (1 + dif_block);
265       break;
266     case 2:                    /* VAUX block */
267       location += (3 + dif_block);
268       break;
269     case 3:                    /* Audio block */
270       location += (6 + dif_block * 16);
271       break;
272     case 4:                    /* Video block */
273       location += (7 + (dif_block / 15) + dif_block);
274       break;
275     default:                   /* Something bogus */
276       GST_DEBUG ("UNKNOWN BLOCK");
277       location = -1;
278       break;
279   }
280   return location;
281 }
282
283 /* Process one RTP packet. Accumulate RTP payload in the proper place in a DV
284  * frame, and return that frame if we detect a new frame, or NULL otherwise.
285  * We assume a DV frame is 144000 bytes. That should accomodate PAL as well as
286  * NTSC.
287  */
288 static GstBuffer *
289 gst_rtp_dv_depay_process (GstRTPBaseDepayload * base, GstBuffer * in)
290 {
291   GstBuffer *out = NULL;
292   guint8 *payload;
293   guint32 rtp_ts;
294   guint payload_len, location;
295   GstRTPDVDepay *dvdepay = GST_RTP_DV_DEPAY (base);
296   gboolean marker;
297   GstRTPBuffer rtp = { NULL, };
298
299   gst_rtp_buffer_map (in, GST_MAP_READ, &rtp);
300
301   marker = gst_rtp_buffer_get_marker (&rtp);
302
303   /* Check if the received packet contains (the start of) a new frame, we do
304    * this by checking the RTP timestamp. */
305   rtp_ts = gst_rtp_buffer_get_timestamp (&rtp);
306
307   /* we cannot copy the packet yet if the marker is set, we will do that below
308    * after taking out the data */
309   if (dvdepay->prev_ts != -1 && rtp_ts != dvdepay->prev_ts && !marker) {
310     /* the timestamp changed */
311     GST_DEBUG_OBJECT (dvdepay, "new frame with ts %u, old ts %u", rtp_ts,
312         dvdepay->prev_ts);
313
314     /* return copy of accumulator. */
315     out = gst_buffer_copy (dvdepay->acc);
316   }
317
318   /* Extract the payload */
319   payload_len = gst_rtp_buffer_get_payload_len (&rtp);
320   payload = gst_rtp_buffer_get_payload (&rtp);
321
322   /* copy all DIF chunks in their place. */
323   while (payload_len >= 80) {
324     guint offset;
325
326     /* Calculate where in the frame the payload should go */
327     location = calculate_difblock_location (payload);
328
329     if (location < 6) {
330       /* part of a header, set the flag to mark that we have the header. */
331       dvdepay->header_mask |= (1 << location);
332       GST_LOG_OBJECT (dvdepay, "got header at location %d, now %02x", location,
333           dvdepay->header_mask);
334     } else {
335       GST_LOG_OBJECT (dvdepay, "got block at location %d", location);
336     }
337
338     if (location != -1) {
339       /* get the byte offset of the dif block */
340       offset = location * 80;
341
342       /* And copy it in, provided the location is sane. */
343       if (offset <= dvdepay->frame_size - 80)
344         gst_buffer_fill (dvdepay->acc, offset, payload, 80);
345     }
346
347     payload += 80;
348     payload_len -= 80;
349   }
350   gst_rtp_buffer_unmap (&rtp);
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 }