Merge remote-tracking branch 'origin/master' into 0.11
[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., 59 Temple Place - Suite 330,
23  * Boston, MA 02111-1307, 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, " "encoding-name = (string) \"H263\"; "
58         "application/x-rtp, "
59         "media = (string) \"video\", "
60         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
61         "clock-rate = (int) 90000, " "encoding-name = (string) \"H263\"")
62     );
63
64 #define gst_rtp_h263_depay_parent_class parent_class
65 G_DEFINE_TYPE (GstRtpH263Depay, gst_rtp_h263_depay,
66     GST_TYPE_RTP_BASE_DEPAYLOAD);
67
68 static void gst_rtp_h263_depay_finalize (GObject * object);
69
70 static GstStateChangeReturn gst_rtp_h263_depay_change_state (GstElement *
71     element, GstStateChange transition);
72
73 static GstBuffer *gst_rtp_h263_depay_process (GstRTPBaseDepayload * depayload,
74     GstBuffer * buf);
75 gboolean gst_rtp_h263_depay_setcaps (GstRTPBaseDepayload * filter,
76     GstCaps * caps);
77
78 static void
79 gst_rtp_h263_depay_class_init (GstRtpH263DepayClass * klass)
80 {
81   GObjectClass *gobject_class;
82   GstElementClass *gstelement_class;
83   GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
84
85   GST_DEBUG_CATEGORY_INIT (rtph263depay_debug, "rtph263depay", 0,
86       "H263 Video RTP Depayloader");
87
88   gobject_class = (GObjectClass *) klass;
89   gstelement_class = (GstElementClass *) klass;
90   gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
91
92   gobject_class->finalize = gst_rtp_h263_depay_finalize;
93
94   gstelement_class->change_state = gst_rtp_h263_depay_change_state;
95
96   gst_element_class_add_pad_template (gstelement_class,
97       gst_static_pad_template_get (&gst_rtp_h263_depay_src_template));
98   gst_element_class_add_pad_template (gstelement_class,
99       gst_static_pad_template_get (&gst_rtp_h263_depay_sink_template));
100
101   gst_element_class_set_details_simple (gstelement_class,
102       "RTP H263 depayloader", "Codec/Depayloader/Network/RTP",
103       "Extracts H263 video from RTP packets (RFC 2190)",
104       "Philippe Kalaf <philippe.kalaf@collabora.co.uk>, "
105       "Edward Hervey <bilboed@bilboed.com>");
106
107   gstrtpbasedepayload_class->process = gst_rtp_h263_depay_process;
108   gstrtpbasedepayload_class->set_caps = gst_rtp_h263_depay_setcaps;
109 }
110
111 static void
112 gst_rtp_h263_depay_init (GstRtpH263Depay * rtph263depay)
113 {
114   rtph263depay->adapter = gst_adapter_new ();
115
116   rtph263depay->offset = 0;
117   rtph263depay->leftover = 0;
118 }
119
120 static void
121 gst_rtp_h263_depay_finalize (GObject * object)
122 {
123   GstRtpH263Depay *rtph263depay;
124
125   rtph263depay = GST_RTP_H263_DEPAY (object);
126
127   g_object_unref (rtph263depay->adapter);
128   rtph263depay->adapter = NULL;
129
130   G_OBJECT_CLASS (parent_class)->finalize (object);
131 }
132
133 gboolean
134 gst_rtp_h263_depay_setcaps (GstRTPBaseDepayload * filter, GstCaps * caps)
135 {
136   GstCaps *srccaps;
137   GstStructure *structure = gst_caps_get_structure (caps, 0);
138   gint clock_rate;
139
140   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
141     clock_rate = 90000;         /* default */
142   filter->clock_rate = clock_rate;
143
144   srccaps = gst_caps_new_simple ("video/x-h263",
145       "variant", G_TYPE_STRING, "itu",
146       "h263version", G_TYPE_STRING, "h263", NULL);
147   gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (filter), srccaps);
148   gst_caps_unref (srccaps);
149
150   return TRUE;
151 }
152
153 static GstBuffer *
154 gst_rtp_h263_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
155 {
156   GstRtpH263Depay *rtph263depay;
157   GstBuffer *outbuf;
158   gint payload_len;
159   guint8 *payload;
160   guint header_len;
161   guint SBIT, EBIT;
162   gboolean F, P, M;
163   gboolean I;
164   GstRTPBuffer rtp = { NULL };
165
166   rtph263depay = GST_RTP_H263_DEPAY (depayload);
167
168   /* flush remaining data on discont */
169   if (GST_BUFFER_IS_DISCONT (buf)) {
170     GST_LOG_OBJECT (depayload, "Discont buffer, flushing adapter");
171     gst_adapter_clear (rtph263depay->adapter);
172     rtph263depay->offset = 0;
173     rtph263depay->leftover = 0;
174     rtph263depay->start = FALSE;
175   }
176
177   gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);
178
179   payload_len = gst_rtp_buffer_get_payload_len (&rtp);
180   payload = gst_rtp_buffer_get_payload (&rtp);
181
182   M = gst_rtp_buffer_get_marker (&rtp);
183
184   /* Let's see what mode we are using */
185   F = (payload[0] & 0x80) == 0x80;
186   P = (payload[0] & 0x40) == 0x40;
187
188   /* Bit shifting */
189   SBIT = (payload[0] & 0x38) >> 3;
190   EBIT = (payload[0] & 0x07);
191
192   /* Figure out header length and I-flag */
193   if (F == 0) {
194     /* F == 0 and P == 0 or 1
195      * mode A */
196     header_len = GST_RFC2190A_HEADER_LEN;
197     GST_LOG ("Mode A");
198
199     /* 0                   1                   2                   3
200      * 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
201      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
202      * |F|P|SBIT |EBIT | SRC |I|U|S|A|R      |DBQ| TRB |    TR         |
203      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
204      */
205     I = (payload[1] & 0x10) == 0x10;
206   } else {
207     if (P == 0) {
208       /* F == 1 and P == 0
209        * mode B */
210       header_len = GST_RFC2190B_HEADER_LEN;
211       GST_LOG ("Mode B");
212
213       /* 0                   1                   2                   3
214        * 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
215        * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
216        * |F|P|SBIT |EBIT | SRC | QUANT   |  GOBN   |   MBA           |R  |
217        * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
218        * |I|U|S|A| HMV1        | VMV1        | HMV2        | VMV2        |
219        * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
220        */
221       I = (payload[4] & 0x80) == 0x80;
222     } else {
223       /* F == 1 and P == 1
224        * mode C */
225       header_len = GST_RFC2190C_HEADER_LEN;
226       GST_LOG ("Mode C");
227
228       /* 0                   1                   2                   3
229        * 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
230        * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
231        * |F|P|SBIT |EBIT | SRC | QUANT   |  GOBN   |   MBA           |R  |
232        * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
233        * |I|U|S|A| HMV1        | VMV1        | HMV2        | VMV2        |
234        * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
235        * | RR                                  |DBQ| TRB |    TR         |
236        * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
237        */
238       I = (payload[4] & 0x80) == 0x80;
239     }
240   }
241
242   GST_LOG ("F/P/M/I : %d/%d/%d/%d", F, P, M, I);
243   GST_LOG ("SBIT : %d , EBIT : %d", SBIT, EBIT);
244   GST_LOG ("payload_len : %d, header_len : %d , leftover : 0x%x",
245       payload_len, header_len, rtph263depay->leftover);
246
247   /* skip header */
248   payload += header_len;
249   payload_len -= header_len;
250
251   if (!rtph263depay->start) {
252     /* do not skip this fragment if it is a Mode A with picture start code */
253     if (!F && payload_len > 4 && (GST_READ_UINT32_BE (payload) >> 10 == 0x20)) {
254       GST_DEBUG ("Mode A with PSC => frame start");
255       rtph263depay->start = TRUE;
256       if (! !(payload[4] & 0x02) != I) {
257         GST_DEBUG ("Wrong Picture Coding Type Flag in rtp header");
258         I = !I;
259       }
260       rtph263depay->psc_I = I;
261     } else {
262       GST_DEBUG ("no frame start yet, skipping payload");
263       goto skip;
264     }
265   }
266
267   /* only trust I info from Mode A starting packet
268    * from buggy payloaders or hw */
269   I = rtph263depay->psc_I;
270
271   if (SBIT) {
272     /* take the leftover and merge it at the beginning, FIXME make the buffer
273      * data writable. */
274     GST_LOG ("payload[0] : 0x%x", payload[0]);
275     payload[0] &= 0xFF >> SBIT;
276     GST_LOG ("payload[0] : 0x%x", payload[0]);
277     payload[0] |= rtph263depay->leftover;
278     GST_LOG ("payload[0] : 0x%x", payload[0]);
279     rtph263depay->leftover = 0;
280     rtph263depay->offset = 0;
281   }
282
283   if (!EBIT) {
284     GstBuffer *tmp;
285
286     /* Take the entire buffer */
287     tmp = gst_rtp_buffer_get_payload_subbuffer (&rtp, header_len, payload_len);
288     gst_adapter_push (rtph263depay->adapter, tmp);
289   } else {
290     GstBuffer *tmp;
291
292     /* Take the entire buffer except for the last byte */
293     tmp = gst_rtp_buffer_get_payload_subbuffer (&rtp, header_len,
294         payload_len - 1);
295     gst_adapter_push (rtph263depay->adapter, tmp);
296
297     /* Put the last byte into the leftover */
298     GST_DEBUG ("payload[payload_len - 1] : 0x%x", payload[payload_len - 1]);
299     GST_DEBUG ("mask : 0x%x", 0xFF << EBIT);
300     rtph263depay->leftover = (payload[payload_len - 1] >> EBIT) << EBIT;
301     rtph263depay->offset = 1;
302     GST_DEBUG ("leftover : 0x%x", rtph263depay->leftover);
303   }
304
305 skip:
306   if (M) {
307     if (rtph263depay->start) {
308       /* frame is completed */
309       guint avail;
310
311       if (rtph263depay->offset) {
312         /* push in the leftover */
313         GstBuffer *buf = gst_buffer_new_and_alloc (1);
314
315         GST_DEBUG ("Pushing leftover in adapter");
316         gst_buffer_fill (buf, 0, &rtph263depay->leftover, 1);
317         gst_adapter_push (rtph263depay->adapter, buf);
318       }
319
320       avail = gst_adapter_available (rtph263depay->adapter);
321       outbuf = gst_adapter_take_buffer (rtph263depay->adapter, avail);
322
323       if (I)
324         GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
325
326       GST_DEBUG ("Pushing out a buffer of %d bytes", avail);
327
328       gst_rtp_base_depayload_push (depayload, outbuf);
329       rtph263depay->offset = 0;
330       rtph263depay->leftover = 0;
331       rtph263depay->start = FALSE;
332     } else {
333       rtph263depay->start = TRUE;
334     }
335   }
336   gst_rtp_buffer_unmap (&rtp);
337
338   return NULL;
339 }
340
341 static GstStateChangeReturn
342 gst_rtp_h263_depay_change_state (GstElement * element,
343     GstStateChange transition)
344 {
345   GstRtpH263Depay *rtph263depay;
346   GstStateChangeReturn ret;
347
348   rtph263depay = GST_RTP_H263_DEPAY (element);
349
350   switch (transition) {
351     case GST_STATE_CHANGE_NULL_TO_READY:
352       break;
353     case GST_STATE_CHANGE_READY_TO_PAUSED:
354       gst_adapter_clear (rtph263depay->adapter);
355       rtph263depay->start = TRUE;
356       break;
357     default:
358       break;
359   }
360
361   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
362
363   switch (transition) {
364     case GST_STATE_CHANGE_READY_TO_NULL:
365       break;
366     default:
367       break;
368   }
369   return ret;
370 }
371
372 gboolean
373 gst_rtp_h263_depay_plugin_init (GstPlugin * plugin)
374 {
375   return gst_element_register (plugin, "rtph263depay",
376       GST_RANK_SECONDARY, GST_TYPE_RTP_H263_DEPAY);
377 }