Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.git] / gst / rtp / gstrtph263pdepay.c
1 /* GStreamer
2  * Copyright (C) <2005> Wim Taymans <wim.taymans@gmail.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <string.h>
25
26 #include <gst/rtp/gstrtpbuffer.h>
27 #include "gstrtph263pdepay.h"
28
29 static GstStaticPadTemplate gst_rtp_h263p_depay_src_template =
30 GST_STATIC_PAD_TEMPLATE ("src",
31     GST_PAD_SRC,
32     GST_PAD_ALWAYS,
33     GST_STATIC_CAPS ("video/x-h263, " "variant = (string) \"itu\" ")
34     );
35
36 static GstStaticPadTemplate gst_rtp_h263p_depay_sink_template =
37     GST_STATIC_PAD_TEMPLATE ("sink",
38     GST_PAD_SINK,
39     GST_PAD_ALWAYS,
40     GST_STATIC_CAPS ("application/x-rtp, "
41         "media = (string) \"video\", "
42         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
43         "clock-rate = (int) [1, MAX], "
44         "encoding-name = (string) \"H263-1998\"; "
45         /* optional params */
46         /* NOTE all optional SDP params must be strings in the caps */
47         /*
48            "sqcif = (string) [1, 32], "
49            "qcif = (string) [1, 32], "
50            "cif = (string) [1, 32], "
51            "cif4 = (string) [1, 32], "
52            "cif16 = (string) [1, 32], "
53            "custom = (string) ANY, "
54            "f = (string) {0, 1},"
55            "i = (string) {0, 1},"
56            "j = (string) {0, 1},"
57            "t = (string) {0, 1},"
58            "k = (string) {1, 2, 3, 4},"
59            "n = (string) {1, 2, 3, 4},"
60            "p = (string) ANY,"
61            "par = (string) ANY, "
62            "cpcf = (string) ANY, "
63            "bpp = (string) [0, 65536], "
64            "hrd = (string) {0, 1}; "
65          */
66         "application/x-rtp, "
67         "media = (string) \"video\", "
68         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
69         "clock-rate = (int) [1, MAX], "
70         "encoding-name = (string) \"H263-2000\" "
71         /* optional params */
72         /* NOTE all optional SDP params must be strings in the caps */
73         /*
74            "profile = (string) [0, 10], "
75            "level = (string) {10, 20, 30, 40, 45, 50, 60, 70}, "
76            "interlace = (string) {0, 1};"
77          */
78     )
79     );
80
81 GST_BOILERPLATE (GstRtpH263PDepay, gst_rtp_h263p_depay, GstBaseRTPDepayload,
82     GST_TYPE_BASE_RTP_DEPAYLOAD);
83
84 static void gst_rtp_h263p_depay_finalize (GObject * object);
85
86 static GstStateChangeReturn gst_rtp_h263p_depay_change_state (GstElement *
87     element, GstStateChange transition);
88
89 static GstBuffer *gst_rtp_h263p_depay_process (GstBaseRTPDepayload * depayload,
90     GstBuffer * buf);
91 gboolean gst_rtp_h263p_depay_setcaps (GstBaseRTPDepayload * filter,
92     GstCaps * caps);
93
94 static void
95 gst_rtp_h263p_depay_base_init (gpointer klass)
96 {
97   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
98
99   gst_element_class_add_static_pad_template (element_class,
100       &gst_rtp_h263p_depay_src_template);
101   gst_element_class_add_static_pad_template (element_class,
102       &gst_rtp_h263p_depay_sink_template);
103
104
105   gst_element_class_set_details_simple (element_class, "RTP H263 depayloader",
106       "Codec/Depayloader/Network/RTP",
107       "Extracts H263/+/++ video from RTP packets (RFC 4629)",
108       "Wim Taymans <wim.taymans@gmail.com>");
109 }
110
111 static void
112 gst_rtp_h263p_depay_class_init (GstRtpH263PDepayClass * klass)
113 {
114   GObjectClass *gobject_class;
115   GstElementClass *gstelement_class;
116   GstBaseRTPDepayloadClass *gstbasertpdepayload_class;
117
118   gobject_class = (GObjectClass *) klass;
119   gstelement_class = (GstElementClass *) klass;
120   gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;
121
122   gstbasertpdepayload_class->process = gst_rtp_h263p_depay_process;
123   gstbasertpdepayload_class->set_caps = gst_rtp_h263p_depay_setcaps;
124
125   gobject_class->finalize = gst_rtp_h263p_depay_finalize;
126
127   gstelement_class->change_state = gst_rtp_h263p_depay_change_state;
128 }
129
130 static void
131 gst_rtp_h263p_depay_init (GstRtpH263PDepay * rtph263pdepay,
132     GstRtpH263PDepayClass * klass)
133 {
134   rtph263pdepay->adapter = gst_adapter_new ();
135 }
136
137 static void
138 gst_rtp_h263p_depay_finalize (GObject * object)
139 {
140   GstRtpH263PDepay *rtph263pdepay;
141
142   rtph263pdepay = GST_RTP_H263P_DEPAY (object);
143
144   g_object_unref (rtph263pdepay->adapter);
145   rtph263pdepay->adapter = NULL;
146
147   G_OBJECT_CLASS (parent_class)->finalize (object);
148 }
149
150 gboolean
151 gst_rtp_h263p_depay_setcaps (GstBaseRTPDepayload * filter, GstCaps * caps)
152 {
153   GstCaps *srccaps = NULL;
154   GstStructure *structure = gst_caps_get_structure (caps, 0);
155   gint clock_rate;
156   const gchar *encoding_name = NULL;
157   gboolean res;
158
159   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
160     clock_rate = 90000;         /* default */
161   filter->clock_rate = clock_rate;
162
163   encoding_name = gst_structure_get_string (structure, "encoding-name");
164   if (encoding_name == NULL)
165     goto no_encoding_name;
166
167   if (g_ascii_strcasecmp (encoding_name, "H263-2000") == 0) {
168     /* always h263++ */
169     srccaps = gst_caps_new_simple ("video/x-h263",
170         "variant", G_TYPE_STRING, "itu",
171         "h263version", G_TYPE_STRING, "h263pp", NULL);
172   } else if (g_ascii_strcasecmp (encoding_name, "H263-1998") == 0) {
173     /* this can be H263 or H263+ depending on defined appendixes in the optional
174      * SDP params */
175     const gchar *F, *I, *J, *T, *K, *N, *P;
176     gboolean is_h263p = FALSE;
177
178     F = gst_structure_get_string (structure, "f");
179     if (F)
180       if (g_ascii_strcasecmp (F, "1") == 0)
181         is_h263p = TRUE;
182     I = gst_structure_get_string (structure, "i");
183     if (I)
184       if (g_ascii_strcasecmp (I, "1") == 0)
185         is_h263p = TRUE;
186     J = gst_structure_get_string (structure, "j");
187     if (J)
188       if (g_ascii_strcasecmp (J, "1") == 0)
189         is_h263p = TRUE;
190     T = gst_structure_get_string (structure, "t");
191     if (T)
192       if (g_ascii_strcasecmp (T, "1") == 0)
193         is_h263p = TRUE;
194     K = gst_structure_get_string (structure, "k");
195     if (K)
196       is_h263p = TRUE;
197     N = gst_structure_get_string (structure, "n");
198     if (N)
199       is_h263p = TRUE;
200     P = gst_structure_get_string (structure, "p");
201     if (P)
202       is_h263p = TRUE;
203
204     if (is_h263p) {
205       srccaps = gst_caps_new_simple ("video/x-h263",
206           "variant", G_TYPE_STRING, "itu",
207           "h263version", G_TYPE_STRING, "h263p", NULL);
208     } else {
209       srccaps = gst_caps_new_simple ("video/x-h263",
210           "variant", G_TYPE_STRING, "itu",
211           "h263version", G_TYPE_STRING, "h263", NULL);
212     }
213   }
214   if (!srccaps)
215     goto no_caps;
216
217   res = gst_pad_set_caps (GST_BASE_RTP_DEPAYLOAD_SRCPAD (filter), srccaps);
218   gst_caps_unref (srccaps);
219
220   return res;
221
222   /* ERRORS */
223 no_encoding_name:
224   {
225     GST_ERROR_OBJECT (filter, "no encoding-name");
226     return FALSE;
227   }
228 no_caps:
229   {
230     GST_ERROR_OBJECT (filter, "invalid encoding-name");
231     return FALSE;
232   }
233 }
234
235 static GstBuffer *
236 gst_rtp_h263p_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
237 {
238   GstRtpH263PDepay *rtph263pdepay;
239   GstBuffer *outbuf;
240
241   rtph263pdepay = GST_RTP_H263P_DEPAY (depayload);
242
243   /* flush remaining data on discont */
244   if (GST_BUFFER_IS_DISCONT (buf)) {
245     GST_LOG_OBJECT (depayload, "DISCONT, flushing adapter");
246     gst_adapter_clear (rtph263pdepay->adapter);
247     rtph263pdepay->wait_start = TRUE;
248   }
249
250   {
251     gint payload_len;
252     guint8 *payload;
253     gboolean P, V, M;
254     guint header_len;
255     guint8 PLEN, PEBIT;
256
257     payload_len = gst_rtp_buffer_get_payload_len (buf);
258     payload = gst_rtp_buffer_get_payload (buf);
259
260     header_len = 2;
261
262     if (payload_len < header_len)
263       goto too_small;
264
265     M = gst_rtp_buffer_get_marker (buf);
266
267     /*  0                   1
268      *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
269      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
270      * |   RR    |P|V|   PLEN    |PEBIT|
271      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
272      */
273     P = (payload[0] & 0x04) == 0x04;
274     V = (payload[0] & 0x02) == 0x02;
275     PLEN = ((payload[0] & 0x1) << 5) | (payload[1] >> 3);
276     PEBIT = payload[1] & 0x7;
277
278     GST_LOG_OBJECT (depayload, "P %d, V %d, PLEN %d, PEBIT %d", P, V, PLEN,
279         PEBIT);
280
281     if (V) {
282       header_len++;
283     }
284     if (PLEN) {
285       header_len += PLEN;
286     }
287
288     if ((!P && payload_len < header_len) || (P && payload_len < header_len - 2))
289       goto too_small;
290
291     if (P) {
292       /* FIXME, have to make the packet writable hear. Better to reset these
293        * bytes when we copy the packet below */
294       rtph263pdepay->wait_start = FALSE;
295       header_len -= 2;
296       payload[header_len] = 0;
297       payload[header_len + 1] = 0;
298     }
299
300     if (rtph263pdepay->wait_start)
301       goto waiting_start;
302
303     if (payload_len < header_len)
304       goto too_small;
305
306     /* FIXME do not ignore the VRC header (See RFC 2429 section 4.2) */
307     /* FIXME actually use the RTP picture header when it is lost in the network */
308     /* for now strip off header */
309     payload += header_len;
310     payload_len -= header_len;
311
312     if (M) {
313       /* frame is completed: append to previous, push it out */
314       guint len, padlen;
315       guint avail;
316
317       GST_LOG_OBJECT (depayload, "Frame complete");
318
319       avail = gst_adapter_available (rtph263pdepay->adapter);
320
321       len = avail + payload_len;
322       padlen = (len % 4) + 4;
323       outbuf = gst_buffer_new_and_alloc (len + padlen);
324       memset (GST_BUFFER_DATA (outbuf) + len, 0, padlen);
325       GST_BUFFER_SIZE (outbuf) = len;
326
327       /* prepend previous data */
328       if (avail > 0) {
329         gst_adapter_copy (rtph263pdepay->adapter, GST_BUFFER_DATA (outbuf), 0,
330             avail);
331         gst_adapter_flush (rtph263pdepay->adapter, avail);
332       }
333       memcpy (GST_BUFFER_DATA (outbuf) + avail, payload, payload_len);
334
335       return outbuf;
336
337     } else {
338       /* frame not completed: store in adapter */
339       outbuf = gst_buffer_new_and_alloc (payload_len);
340
341       GST_LOG_OBJECT (depayload, "Frame incomplete, storing %d", payload_len);
342
343       memcpy (GST_BUFFER_DATA (outbuf), payload, payload_len);
344
345       gst_adapter_push (rtph263pdepay->adapter, outbuf);
346     }
347   }
348   return NULL;
349
350 too_small:
351   {
352     GST_ELEMENT_WARNING (rtph263pdepay, STREAM, DECODE,
353         ("Packet payload was too small"), (NULL));
354     return NULL;
355   }
356 waiting_start:
357   {
358     GST_DEBUG_OBJECT (rtph263pdepay, "waiting for picture start");
359     return NULL;
360   }
361 }
362
363 static GstStateChangeReturn
364 gst_rtp_h263p_depay_change_state (GstElement * element,
365     GstStateChange transition)
366 {
367   GstRtpH263PDepay *rtph263pdepay;
368   GstStateChangeReturn ret;
369
370   rtph263pdepay = GST_RTP_H263P_DEPAY (element);
371
372   switch (transition) {
373     case GST_STATE_CHANGE_NULL_TO_READY:
374       break;
375     case GST_STATE_CHANGE_READY_TO_PAUSED:
376       gst_adapter_clear (rtph263pdepay->adapter);
377       rtph263pdepay->wait_start = TRUE;
378       break;
379     default:
380       break;
381   }
382
383   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
384
385   switch (transition) {
386     case GST_STATE_CHANGE_READY_TO_NULL:
387       break;
388     default:
389       break;
390   }
391   return ret;
392 }
393
394 gboolean
395 gst_rtp_h263p_depay_plugin_init (GstPlugin * plugin)
396 {
397   return gst_element_register (plugin, "rtph263pdepay",
398       GST_RANK_SECONDARY, GST_TYPE_RTP_H263P_DEPAY);
399 }