rtpvrawpay: Add missing break
[platform/upstream/gst-plugins-good.git] / gst / rtp / gstrtph263depay.c
1 /* GStreamer
2  *
3  * Copyright 2007 Nokia Corporation
4  * Copyright 2007 Collabora Ltd,
5  *  @author: Philippe Kalaf <philippe.kalaf@collabora.co.uk>
6  *
7  * Copyright (C) <2005> Wim Taymans <wim.taymans@gmail.com>
8  *               <2007> Edward Hervey <bilboed@bilboed.com>
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Library General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Library General Public License for more details.
19  *
20  * You should have received a copy of the GNU Library General Public
21  * License along with this library; if not, write to the
22  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23  * Boston, MA 02110-1301, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 #  include "config.h"
28 #endif
29
30 #include <string.h>
31
32 #include <gst/rtp/gstrtpbuffer.h>
33 #include "gstrtph263depay.h"
34
35 GST_DEBUG_CATEGORY_STATIC (rtph263depay_debug);
36 #define GST_CAT_DEFAULT (rtph263depay_debug)
37
38 #define GST_RFC2190A_HEADER_LEN 4
39 #define GST_RFC2190B_HEADER_LEN 8
40 #define GST_RFC2190C_HEADER_LEN 12
41
42 static GstStaticPadTemplate gst_rtp_h263_depay_src_template =
43 GST_STATIC_PAD_TEMPLATE ("src",
44     GST_PAD_SRC,
45     GST_PAD_ALWAYS,
46     GST_STATIC_CAPS ("video/x-h263, "
47         "variant = (string) \"itu\", " "h263version = (string) \"h263\"")
48     );
49
50 static GstStaticPadTemplate gst_rtp_h263_depay_sink_template =
51     GST_STATIC_PAD_TEMPLATE ("sink",
52     GST_PAD_SINK,
53     GST_PAD_ALWAYS,
54     GST_STATIC_CAPS ("application/x-rtp, "
55         "media = (string) \"video\", "
56         "payload = (int) " GST_RTP_PAYLOAD_H263_STRING ", "
57         "clock-rate = (int) 90000; "
58         "application/x-rtp, "
59         "media = (string) \"video\", "
60         "clock-rate = (int) 90000, " "encoding-name = (string) \"H263\"")
61     );
62
63 #define gst_rtp_h263_depay_parent_class parent_class
64 G_DEFINE_TYPE (GstRtpH263Depay, gst_rtp_h263_depay,
65     GST_TYPE_RTP_BASE_DEPAYLOAD);
66
67 static void gst_rtp_h263_depay_finalize (GObject * object);
68
69 static GstStateChangeReturn gst_rtp_h263_depay_change_state (GstElement *
70     element, GstStateChange transition);
71
72 static GstBuffer *gst_rtp_h263_depay_process (GstRTPBaseDepayload * depayload,
73     GstBuffer * buf);
74 gboolean gst_rtp_h263_depay_setcaps (GstRTPBaseDepayload * filter,
75     GstCaps * caps);
76
77 static void
78 gst_rtp_h263_depay_class_init (GstRtpH263DepayClass * klass)
79 {
80   GObjectClass *gobject_class;
81   GstElementClass *gstelement_class;
82   GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
83
84   GST_DEBUG_CATEGORY_INIT (rtph263depay_debug, "rtph263depay", 0,
85       "H263 Video RTP Depayloader");
86
87   gobject_class = (GObjectClass *) klass;
88   gstelement_class = (GstElementClass *) klass;
89   gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
90
91   gobject_class->finalize = gst_rtp_h263_depay_finalize;
92
93   gstelement_class->change_state = gst_rtp_h263_depay_change_state;
94
95   gst_element_class_add_pad_template (gstelement_class,
96       gst_static_pad_template_get (&gst_rtp_h263_depay_src_template));
97   gst_element_class_add_pad_template (gstelement_class,
98       gst_static_pad_template_get (&gst_rtp_h263_depay_sink_template));
99
100   gst_element_class_set_static_metadata (gstelement_class,
101       "RTP H263 depayloader", "Codec/Depayloader/Network/RTP",
102       "Extracts H263 video from RTP packets (RFC 2190)",
103       "Philippe Kalaf <philippe.kalaf@collabora.co.uk>, "
104       "Edward Hervey <bilboed@bilboed.com>");
105
106   gstrtpbasedepayload_class->process = gst_rtp_h263_depay_process;
107   gstrtpbasedepayload_class->set_caps = gst_rtp_h263_depay_setcaps;
108 }
109
110 static void
111 gst_rtp_h263_depay_init (GstRtpH263Depay * rtph263depay)
112 {
113   rtph263depay->adapter = gst_adapter_new ();
114
115   rtph263depay->offset = 0;
116   rtph263depay->leftover = 0;
117 }
118
119 static void
120 gst_rtp_h263_depay_finalize (GObject * object)
121 {
122   GstRtpH263Depay *rtph263depay;
123
124   rtph263depay = GST_RTP_H263_DEPAY (object);
125
126   g_object_unref (rtph263depay->adapter);
127   rtph263depay->adapter = NULL;
128
129   G_OBJECT_CLASS (parent_class)->finalize (object);
130 }
131
132 gboolean
133 gst_rtp_h263_depay_setcaps (GstRTPBaseDepayload * filter, GstCaps * caps)
134 {
135   GstCaps *srccaps;
136   GstStructure *structure = gst_caps_get_structure (caps, 0);
137   gint clock_rate;
138
139   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
140     clock_rate = 90000;         /* default */
141   filter->clock_rate = clock_rate;
142
143   srccaps = gst_caps_new_simple ("video/x-h263",
144       "variant", G_TYPE_STRING, "itu",
145       "h263version", G_TYPE_STRING, "h263", NULL);
146   gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (filter), srccaps);
147   gst_caps_unref (srccaps);
148
149   return TRUE;
150 }
151
152 static GstBuffer *
153 gst_rtp_h263_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
154 {
155   GstRtpH263Depay *rtph263depay;
156   GstBuffer *outbuf;
157   gint payload_len;
158   guint8 *payload;
159   guint header_len;
160   guint SBIT, EBIT;
161   gboolean F, P, M;
162   gboolean I;
163   GstRTPBuffer rtp = { NULL };
164
165   rtph263depay = GST_RTP_H263_DEPAY (depayload);
166
167   /* flush remaining data on discont */
168   if (GST_BUFFER_IS_DISCONT (buf)) {
169     GST_LOG_OBJECT (depayload, "Discont buffer, flushing adapter");
170     gst_adapter_clear (rtph263depay->adapter);
171     rtph263depay->offset = 0;
172     rtph263depay->leftover = 0;
173     rtph263depay->start = FALSE;
174   }
175
176   gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);
177
178   payload_len = gst_rtp_buffer_get_payload_len (&rtp);
179   payload = gst_rtp_buffer_get_payload (&rtp);
180
181   M = gst_rtp_buffer_get_marker (&rtp);
182
183   /* Let's see what mode we are using */
184   F = (payload[0] & 0x80) == 0x80;
185   P = (payload[0] & 0x40) == 0x40;
186
187   /* Bit shifting */
188   SBIT = (payload[0] & 0x38) >> 3;
189   EBIT = (payload[0] & 0x07);
190
191   /* Figure out header length and I-flag */
192   if (F == 0) {
193     /* F == 0 and P == 0 or 1
194      * mode A */
195     header_len = GST_RFC2190A_HEADER_LEN;
196     GST_LOG ("Mode A");
197
198     /* 0                   1                   2                   3
199      * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
200      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
201      * |F|P|SBIT |EBIT | SRC |I|U|S|A|R      |DBQ| TRB |    TR         |
202      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
203      */
204     I = (payload[1] & 0x10) == 0x10;
205   } else {
206     if (P == 0) {
207       /* F == 1 and P == 0
208        * mode B */
209       header_len = GST_RFC2190B_HEADER_LEN;
210       GST_LOG ("Mode B");
211
212       /* 0                   1                   2                   3
213        * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
214        * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
215        * |F|P|SBIT |EBIT | SRC | QUANT   |  GOBN   |   MBA           |R  |
216        * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
217        * |I|U|S|A| HMV1        | VMV1        | HMV2        | VMV2        |
218        * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
219        */
220       I = (payload[4] & 0x80) == 0x80;
221     } else {
222       /* F == 1 and P == 1
223        * mode C */
224       header_len = GST_RFC2190C_HEADER_LEN;
225       GST_LOG ("Mode C");
226
227       /* 0                   1                   2                   3
228        * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
229        * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
230        * |F|P|SBIT |EBIT | SRC | QUANT   |  GOBN   |   MBA           |R  |
231        * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
232        * |I|U|S|A| HMV1        | VMV1        | HMV2        | VMV2        |
233        * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
234        * | RR                                  |DBQ| TRB |    TR         |
235        * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
236        */
237       I = (payload[4] & 0x80) == 0x80;
238     }
239   }
240
241   GST_LOG ("F/P/M/I : %d/%d/%d/%d", F, P, M, I);
242   GST_LOG ("SBIT : %d , EBIT : %d", SBIT, EBIT);
243   GST_LOG ("payload_len : %d, header_len : %d , leftover : 0x%x",
244       payload_len, header_len, rtph263depay->leftover);
245
246   /* skip header */
247   payload += header_len;
248   payload_len -= header_len;
249
250   if (!rtph263depay->start) {
251     /* do not skip this fragment if it is a Mode A with picture start code */
252     if (!F && payload_len > 4 && (GST_READ_UINT32_BE (payload) >> 10 == 0x20)) {
253       GST_DEBUG ("Mode A with PSC => frame start");
254       rtph263depay->start = TRUE;
255       if (! !(payload[4] & 0x02) != I) {
256         GST_DEBUG ("Wrong Picture Coding Type Flag in rtp header");
257         I = !I;
258       }
259       rtph263depay->psc_I = I;
260     } else {
261       GST_DEBUG ("no frame start yet, skipping payload");
262       goto skip;
263     }
264   }
265
266   /* only trust I info from Mode A starting packet
267    * from buggy payloaders or hw */
268   I = rtph263depay->psc_I;
269
270   if (SBIT) {
271     /* take the leftover and merge it at the beginning, FIXME make the buffer
272      * data writable. */
273     GST_LOG ("payload[0] : 0x%x", payload[0]);
274     payload[0] &= 0xFF >> SBIT;
275     GST_LOG ("payload[0] : 0x%x", payload[0]);
276     payload[0] |= rtph263depay->leftover;
277     GST_LOG ("payload[0] : 0x%x", payload[0]);
278     rtph263depay->leftover = 0;
279     rtph263depay->offset = 0;
280   }
281
282   if (!EBIT) {
283     GstBuffer *tmp;
284
285     /* Take the entire buffer */
286     tmp = gst_rtp_buffer_get_payload_subbuffer (&rtp, header_len, payload_len);
287     gst_adapter_push (rtph263depay->adapter, tmp);
288   } else {
289     GstBuffer *tmp;
290
291     /* Take the entire buffer except for the last byte */
292     tmp = gst_rtp_buffer_get_payload_subbuffer (&rtp, header_len,
293         payload_len - 1);
294     gst_adapter_push (rtph263depay->adapter, tmp);
295
296     /* Put the last byte into the leftover */
297     GST_DEBUG ("payload[payload_len - 1] : 0x%x", payload[payload_len - 1]);
298     GST_DEBUG ("mask : 0x%x", 0xFF << EBIT);
299     rtph263depay->leftover = (payload[payload_len - 1] >> EBIT) << EBIT;
300     rtph263depay->offset = 1;
301     GST_DEBUG ("leftover : 0x%x", rtph263depay->leftover);
302   }
303
304 skip:
305   if (M) {
306     if (rtph263depay->start) {
307       /* frame is completed */
308       guint avail;
309
310       if (rtph263depay->offset) {
311         /* push in the leftover */
312         GstBuffer *buf = gst_buffer_new_and_alloc (1);
313
314         GST_DEBUG ("Pushing leftover in adapter");
315         gst_buffer_fill (buf, 0, &rtph263depay->leftover, 1);
316         gst_adapter_push (rtph263depay->adapter, buf);
317       }
318
319       avail = gst_adapter_available (rtph263depay->adapter);
320       outbuf = gst_adapter_take_buffer (rtph263depay->adapter, avail);
321
322       if (I)
323         GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
324
325       GST_DEBUG ("Pushing out a buffer of %d bytes", avail);
326
327       gst_rtp_base_depayload_push (depayload, outbuf);
328       rtph263depay->offset = 0;
329       rtph263depay->leftover = 0;
330       rtph263depay->start = FALSE;
331     } else {
332       rtph263depay->start = TRUE;
333     }
334   }
335   gst_rtp_buffer_unmap (&rtp);
336
337   return NULL;
338 }
339
340 static GstStateChangeReturn
341 gst_rtp_h263_depay_change_state (GstElement * element,
342     GstStateChange transition)
343 {
344   GstRtpH263Depay *rtph263depay;
345   GstStateChangeReturn ret;
346
347   rtph263depay = GST_RTP_H263_DEPAY (element);
348
349   switch (transition) {
350     case GST_STATE_CHANGE_NULL_TO_READY:
351       break;
352     case GST_STATE_CHANGE_READY_TO_PAUSED:
353       gst_adapter_clear (rtph263depay->adapter);
354       rtph263depay->start = TRUE;
355       break;
356     default:
357       break;
358   }
359
360   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
361
362   switch (transition) {
363     case GST_STATE_CHANGE_READY_TO_NULL:
364       break;
365     default:
366       break;
367   }
368   return ret;
369 }
370
371 gboolean
372 gst_rtp_h263_depay_plugin_init (GstPlugin * plugin)
373 {
374   return gst_element_register (plugin, "rtph263depay",
375       GST_RANK_SECONDARY, GST_TYPE_RTP_H263_DEPAY);
376 }