rtph263pdepay: init debug category
[platform/upstream/gstreamer.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., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, 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 GST_DEBUG_CATEGORY_STATIC (rtph263pdepay_debug);
30 #define GST_CAT_DEFAULT (rtph263pdepay_debug)
31
32 static GstStaticPadTemplate gst_rtp_h263p_depay_src_template =
33 GST_STATIC_PAD_TEMPLATE ("src",
34     GST_PAD_SRC,
35     GST_PAD_ALWAYS,
36     GST_STATIC_CAPS ("video/x-h263, " "variant = (string) \"itu\" ")
37     );
38
39 static GstStaticPadTemplate gst_rtp_h263p_depay_sink_template =
40     GST_STATIC_PAD_TEMPLATE ("sink",
41     GST_PAD_SINK,
42     GST_PAD_ALWAYS,
43     GST_STATIC_CAPS ("application/x-rtp, "
44         "media = (string) \"video\", "
45         "clock-rate = (int) [1, MAX], "
46         "encoding-name = (string) \"H263-1998\"; "
47         /* optional params */
48         /* NOTE all optional SDP params must be strings in the caps */
49         /*
50            "sqcif = (string) [1, 32], "
51            "qcif = (string) [1, 32], "
52            "cif = (string) [1, 32], "
53            "cif4 = (string) [1, 32], "
54            "cif16 = (string) [1, 32], "
55            "custom = (string) ANY, "
56            "f = (string) {0, 1},"
57            "i = (string) {0, 1},"
58            "j = (string) {0, 1},"
59            "t = (string) {0, 1},"
60            "k = (string) {1, 2, 3, 4},"
61            "n = (string) {1, 2, 3, 4},"
62            "p = (string) ANY,"
63            "par = (string) ANY, "
64            "cpcf = (string) ANY, "
65            "bpp = (string) [0, 65536], "
66            "hrd = (string) {0, 1}; "
67          */
68         "application/x-rtp, "
69         "media = (string) \"video\", "
70         "clock-rate = (int) [1, MAX], "
71         "encoding-name = (string) \"H263-2000\" "
72         /* optional params */
73         /* NOTE all optional SDP params must be strings in the caps */
74         /*
75            "profile = (string) [0, 10], "
76            "level = (string) {10, 20, 30, 40, 45, 50, 60, 70}, "
77            "interlace = (string) {0, 1};"
78          */
79     )
80     );
81
82 #define gst_rtp_h263p_depay_parent_class parent_class
83 G_DEFINE_TYPE (GstRtpH263PDepay, gst_rtp_h263p_depay,
84     GST_TYPE_RTP_BASE_DEPAYLOAD);
85
86 static void gst_rtp_h263p_depay_finalize (GObject * object);
87
88 static GstStateChangeReturn gst_rtp_h263p_depay_change_state (GstElement *
89     element, GstStateChange transition);
90
91 static GstBuffer *gst_rtp_h263p_depay_process (GstRTPBaseDepayload * depayload,
92     GstBuffer * buf);
93 gboolean gst_rtp_h263p_depay_setcaps (GstRTPBaseDepayload * filter,
94     GstCaps * caps);
95
96 static void
97 gst_rtp_h263p_depay_class_init (GstRtpH263PDepayClass * klass)
98 {
99   GObjectClass *gobject_class;
100   GstElementClass *gstelement_class;
101   GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
102
103   gobject_class = (GObjectClass *) klass;
104   gstelement_class = (GstElementClass *) klass;
105   gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
106
107   gobject_class->finalize = gst_rtp_h263p_depay_finalize;
108
109   gstelement_class->change_state = gst_rtp_h263p_depay_change_state;
110
111   gst_element_class_add_pad_template (gstelement_class,
112       gst_static_pad_template_get (&gst_rtp_h263p_depay_src_template));
113   gst_element_class_add_pad_template (gstelement_class,
114       gst_static_pad_template_get (&gst_rtp_h263p_depay_sink_template));
115
116   gst_element_class_set_static_metadata (gstelement_class,
117       "RTP H263 depayloader", "Codec/Depayloader/Network/RTP",
118       "Extracts H263/+/++ video from RTP packets (RFC 4629)",
119       "Wim Taymans <wim.taymans@gmail.com>");
120
121   gstrtpbasedepayload_class->process = gst_rtp_h263p_depay_process;
122   gstrtpbasedepayload_class->set_caps = gst_rtp_h263p_depay_setcaps;
123
124   GST_DEBUG_CATEGORY_INIT (rtph263pdepay_debug, "rtph263pdepay", 0,
125       "H263+ Video RTP Depayloader");
126 }
127
128 static void
129 gst_rtp_h263p_depay_init (GstRtpH263PDepay * rtph263pdepay)
130 {
131   rtph263pdepay->adapter = gst_adapter_new ();
132 }
133
134 static void
135 gst_rtp_h263p_depay_finalize (GObject * object)
136 {
137   GstRtpH263PDepay *rtph263pdepay;
138
139   rtph263pdepay = GST_RTP_H263P_DEPAY (object);
140
141   g_object_unref (rtph263pdepay->adapter);
142   rtph263pdepay->adapter = NULL;
143
144   G_OBJECT_CLASS (parent_class)->finalize (object);
145 }
146
147 gboolean
148 gst_rtp_h263p_depay_setcaps (GstRTPBaseDepayload * filter, GstCaps * caps)
149 {
150   GstCaps *srccaps = NULL;
151   GstStructure *structure = gst_caps_get_structure (caps, 0);
152   gint clock_rate;
153   const gchar *encoding_name = NULL;
154   gboolean res;
155
156   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
157     clock_rate = 90000;         /* default */
158   filter->clock_rate = clock_rate;
159
160   encoding_name = gst_structure_get_string (structure, "encoding-name");
161   if (encoding_name == NULL)
162     goto no_encoding_name;
163
164   if (g_ascii_strcasecmp (encoding_name, "H263-2000") == 0) {
165     /* always h263++ */
166     srccaps = gst_caps_new_simple ("video/x-h263",
167         "variant", G_TYPE_STRING, "itu",
168         "h263version", G_TYPE_STRING, "h263pp", NULL);
169   } else if (g_ascii_strcasecmp (encoding_name, "H263-1998") == 0) {
170     /* this can be H263 or H263+ depending on defined appendixes in the optional
171      * SDP params */
172     const gchar *F, *I, *J, *T, *K, *N, *P;
173     gboolean is_h263p = FALSE;
174
175     F = gst_structure_get_string (structure, "f");
176     if (F)
177       if (g_ascii_strcasecmp (F, "1") == 0)
178         is_h263p = TRUE;
179     I = gst_structure_get_string (structure, "i");
180     if (I)
181       if (g_ascii_strcasecmp (I, "1") == 0)
182         is_h263p = TRUE;
183     J = gst_structure_get_string (structure, "j");
184     if (J)
185       if (g_ascii_strcasecmp (J, "1") == 0)
186         is_h263p = TRUE;
187     T = gst_structure_get_string (structure, "t");
188     if (T)
189       if (g_ascii_strcasecmp (T, "1") == 0)
190         is_h263p = TRUE;
191     K = gst_structure_get_string (structure, "k");
192     if (K)
193       is_h263p = TRUE;
194     N = gst_structure_get_string (structure, "n");
195     if (N)
196       is_h263p = TRUE;
197     P = gst_structure_get_string (structure, "p");
198     if (P)
199       is_h263p = TRUE;
200
201     if (is_h263p) {
202       srccaps = gst_caps_new_simple ("video/x-h263",
203           "variant", G_TYPE_STRING, "itu",
204           "h263version", G_TYPE_STRING, "h263p", NULL);
205     } else {
206       srccaps = gst_caps_new_simple ("video/x-h263",
207           "variant", G_TYPE_STRING, "itu",
208           "h263version", G_TYPE_STRING, "h263", NULL);
209     }
210   }
211   if (!srccaps)
212     goto no_caps;
213
214   res = gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (filter), srccaps);
215   gst_caps_unref (srccaps);
216
217   return res;
218
219   /* ERRORS */
220 no_encoding_name:
221   {
222     GST_ERROR_OBJECT (filter, "no encoding-name");
223     return FALSE;
224   }
225 no_caps:
226   {
227     GST_ERROR_OBJECT (filter, "invalid encoding-name");
228     return FALSE;
229   }
230 }
231
232 static GstBuffer *
233 gst_rtp_h263p_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
234 {
235   GstRtpH263PDepay *rtph263pdepay;
236   GstBuffer *outbuf;
237   gint payload_len;
238   guint8 *payload;
239   gboolean P, V, M;
240   guint header_len;
241   guint8 PLEN, PEBIT;
242   GstRTPBuffer rtp = { NULL };
243
244   rtph263pdepay = GST_RTP_H263P_DEPAY (depayload);
245
246   /* flush remaining data on discont */
247   if (GST_BUFFER_IS_DISCONT (buf)) {
248     GST_LOG_OBJECT (depayload, "DISCONT, flushing adapter");
249     gst_adapter_clear (rtph263pdepay->adapter);
250     rtph263pdepay->wait_start = TRUE;
251   }
252
253   gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);
254
255   payload_len = gst_rtp_buffer_get_payload_len (&rtp);
256   header_len = 2;
257
258   if (payload_len < header_len)
259     goto too_small;
260
261   payload = gst_rtp_buffer_get_payload (&rtp);
262
263   M = gst_rtp_buffer_get_marker (&rtp);
264
265   /*  0                   1
266    *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
267    * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
268    * |   RR    |P|V|   PLEN    |PEBIT|
269    * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
270    */
271   P = (payload[0] & 0x04) == 0x04;
272   V = (payload[0] & 0x02) == 0x02;
273   PLEN = ((payload[0] & 0x1) << 5) | (payload[1] >> 3);
274   PEBIT = payload[1] & 0x7;
275
276   GST_LOG_OBJECT (depayload, "P %d, V %d, PLEN %d, PEBIT %d", P, V, PLEN,
277       PEBIT);
278
279   if (V) {
280     header_len++;
281   }
282   if (PLEN) {
283     header_len += PLEN;
284   }
285
286   if ((!P && payload_len < header_len) || (P && payload_len < header_len - 2))
287     goto too_small;
288
289   if (P) {
290     /* FIXME, have to make the packet writable hear. Better to reset these
291      * bytes when we copy the packet below */
292     rtph263pdepay->wait_start = FALSE;
293     header_len -= 2;
294     payload[header_len] = 0;
295     payload[header_len + 1] = 0;
296   }
297
298   if (rtph263pdepay->wait_start)
299     goto waiting_start;
300
301   if (payload_len < header_len)
302     goto too_small;
303
304   /* FIXME do not ignore the VRC header (See RFC 2429 section 4.2) */
305   /* FIXME actually use the RTP picture header when it is lost in the network */
306   /* for now strip off header */
307   payload += header_len;
308   payload_len -= header_len;
309
310   if (M) {
311     /* frame is completed: append to previous, push it out */
312     guint len, padlen;
313     guint avail;
314     GstBuffer *padbuf;
315
316     GST_LOG_OBJECT (depayload, "Frame complete");
317
318     outbuf =
319         gst_rtp_buffer_get_payload_subbuffer (&rtp, header_len, payload_len);
320     gst_adapter_push (rtph263pdepay->adapter, outbuf);
321     outbuf = NULL;
322
323     avail = gst_adapter_available (rtph263pdepay->adapter);
324     len = avail + payload_len;
325     padlen = (len % 4) + 4;
326
327     outbuf = gst_adapter_take_buffer (rtph263pdepay->adapter, avail);
328     if (padlen) {
329       padbuf = gst_buffer_new_and_alloc (padlen);
330       gst_buffer_memset (padbuf, 0, 0, padlen);
331       outbuf = gst_buffer_append (outbuf, padbuf);
332     }
333     gst_rtp_buffer_unmap (&rtp);
334
335     return outbuf;
336
337   } else {
338     /* frame not completed: store in adapter */
339     GST_LOG_OBJECT (depayload, "Frame incomplete, storing %d", payload_len);
340
341     outbuf =
342         gst_rtp_buffer_get_payload_subbuffer (&rtp, header_len, payload_len);
343     gst_adapter_push (rtph263pdepay->adapter, outbuf);
344     gst_rtp_buffer_unmap (&rtp);
345   }
346   return NULL;
347
348 too_small:
349   {
350     GST_ELEMENT_WARNING (rtph263pdepay, STREAM, DECODE,
351         ("Packet payload was too small"), (NULL));
352     gst_rtp_buffer_unmap (&rtp);
353     return NULL;
354   }
355 waiting_start:
356   {
357     GST_DEBUG_OBJECT (rtph263pdepay, "waiting for picture start");
358     gst_rtp_buffer_unmap (&rtp);
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 }