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