gstdepay: check for correct fragment offset
[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., 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 #define gst_rtp_h263p_depay_parent_class parent_class
82 G_DEFINE_TYPE (GstRtpH263PDepay, gst_rtp_h263p_depay,
83     GST_TYPE_RTP_BASE_DEPAYLOAD);
84
85 static void gst_rtp_h263p_depay_finalize (GObject * object);
86
87 static GstStateChangeReturn gst_rtp_h263p_depay_change_state (GstElement *
88     element, GstStateChange transition);
89
90 static GstBuffer *gst_rtp_h263p_depay_process (GstRTPBaseDepayload * depayload,
91     GstBuffer * buf);
92 gboolean gst_rtp_h263p_depay_setcaps (GstRTPBaseDepayload * filter,
93     GstCaps * caps);
94
95 static void
96 gst_rtp_h263p_depay_class_init (GstRtpH263PDepayClass * klass)
97 {
98   GObjectClass *gobject_class;
99   GstElementClass *gstelement_class;
100   GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
101
102   gobject_class = (GObjectClass *) klass;
103   gstelement_class = (GstElementClass *) klass;
104   gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
105
106   gobject_class->finalize = gst_rtp_h263p_depay_finalize;
107
108   gstelement_class->change_state = gst_rtp_h263p_depay_change_state;
109
110   gst_element_class_add_pad_template (gstelement_class,
111       gst_static_pad_template_get (&gst_rtp_h263p_depay_src_template));
112   gst_element_class_add_pad_template (gstelement_class,
113       gst_static_pad_template_get (&gst_rtp_h263p_depay_sink_template));
114
115   gst_element_class_set_static_metadata (gstelement_class,
116       "RTP H263 depayloader", "Codec/Depayloader/Network/RTP",
117       "Extracts H263/+/++ video from RTP packets (RFC 4629)",
118       "Wim Taymans <wim.taymans@gmail.com>");
119
120   gstrtpbasedepayload_class->process = gst_rtp_h263p_depay_process;
121   gstrtpbasedepayload_class->set_caps = gst_rtp_h263p_depay_setcaps;
122 }
123
124 static void
125 gst_rtp_h263p_depay_init (GstRtpH263PDepay * rtph263pdepay)
126 {
127   rtph263pdepay->adapter = gst_adapter_new ();
128 }
129
130 static void
131 gst_rtp_h263p_depay_finalize (GObject * object)
132 {
133   GstRtpH263PDepay *rtph263pdepay;
134
135   rtph263pdepay = GST_RTP_H263P_DEPAY (object);
136
137   g_object_unref (rtph263pdepay->adapter);
138   rtph263pdepay->adapter = NULL;
139
140   G_OBJECT_CLASS (parent_class)->finalize (object);
141 }
142
143 gboolean
144 gst_rtp_h263p_depay_setcaps (GstRTPBaseDepayload * filter, GstCaps * caps)
145 {
146   GstCaps *srccaps = NULL;
147   GstStructure *structure = gst_caps_get_structure (caps, 0);
148   gint clock_rate;
149   const gchar *encoding_name = NULL;
150   gboolean res;
151
152   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
153     clock_rate = 90000;         /* default */
154   filter->clock_rate = clock_rate;
155
156   encoding_name = gst_structure_get_string (structure, "encoding-name");
157   if (encoding_name == NULL)
158     goto no_encoding_name;
159
160   if (g_ascii_strcasecmp (encoding_name, "H263-2000") == 0) {
161     /* always h263++ */
162     srccaps = gst_caps_new_simple ("video/x-h263",
163         "variant", G_TYPE_STRING, "itu",
164         "h263version", G_TYPE_STRING, "h263pp", NULL);
165   } else if (g_ascii_strcasecmp (encoding_name, "H263-1998") == 0) {
166     /* this can be H263 or H263+ depending on defined appendixes in the optional
167      * SDP params */
168     const gchar *F, *I, *J, *T, *K, *N, *P;
169     gboolean is_h263p = FALSE;
170
171     F = gst_structure_get_string (structure, "f");
172     if (F)
173       if (g_ascii_strcasecmp (F, "1") == 0)
174         is_h263p = TRUE;
175     I = gst_structure_get_string (structure, "i");
176     if (I)
177       if (g_ascii_strcasecmp (I, "1") == 0)
178         is_h263p = TRUE;
179     J = gst_structure_get_string (structure, "j");
180     if (J)
181       if (g_ascii_strcasecmp (J, "1") == 0)
182         is_h263p = TRUE;
183     T = gst_structure_get_string (structure, "t");
184     if (T)
185       if (g_ascii_strcasecmp (T, "1") == 0)
186         is_h263p = TRUE;
187     K = gst_structure_get_string (structure, "k");
188     if (K)
189       is_h263p = TRUE;
190     N = gst_structure_get_string (structure, "n");
191     if (N)
192       is_h263p = TRUE;
193     P = gst_structure_get_string (structure, "p");
194     if (P)
195       is_h263p = TRUE;
196
197     if (is_h263p) {
198       srccaps = gst_caps_new_simple ("video/x-h263",
199           "variant", G_TYPE_STRING, "itu",
200           "h263version", G_TYPE_STRING, "h263p", NULL);
201     } else {
202       srccaps = gst_caps_new_simple ("video/x-h263",
203           "variant", G_TYPE_STRING, "itu",
204           "h263version", G_TYPE_STRING, "h263", NULL);
205     }
206   }
207   if (!srccaps)
208     goto no_caps;
209
210   res = gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (filter), srccaps);
211   gst_caps_unref (srccaps);
212
213   return res;
214
215   /* ERRORS */
216 no_encoding_name:
217   {
218     GST_ERROR_OBJECT (filter, "no encoding-name");
219     return FALSE;
220   }
221 no_caps:
222   {
223     GST_ERROR_OBJECT (filter, "invalid encoding-name");
224     return FALSE;
225   }
226 }
227
228 static GstBuffer *
229 gst_rtp_h263p_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
230 {
231   GstRtpH263PDepay *rtph263pdepay;
232   GstBuffer *outbuf;
233   gint payload_len;
234   guint8 *payload;
235   gboolean P, V, M;
236   guint header_len;
237   guint8 PLEN, PEBIT;
238   GstRTPBuffer rtp = { NULL };
239
240   rtph263pdepay = GST_RTP_H263P_DEPAY (depayload);
241
242   /* flush remaining data on discont */
243   if (GST_BUFFER_IS_DISCONT (buf)) {
244     GST_LOG_OBJECT (depayload, "DISCONT, flushing adapter");
245     gst_adapter_clear (rtph263pdepay->adapter);
246     rtph263pdepay->wait_start = TRUE;
247   }
248
249   gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);
250
251   payload_len = gst_rtp_buffer_get_payload_len (&rtp);
252   header_len = 2;
253
254   if (payload_len < header_len)
255     goto too_small;
256
257   payload = gst_rtp_buffer_get_payload (&rtp);
258
259   M = gst_rtp_buffer_get_marker (&rtp);
260
261   /*  0                   1
262    *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
263    * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
264    * |   RR    |P|V|   PLEN    |PEBIT|
265    * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
266    */
267   P = (payload[0] & 0x04) == 0x04;
268   V = (payload[0] & 0x02) == 0x02;
269   PLEN = ((payload[0] & 0x1) << 5) | (payload[1] >> 3);
270   PEBIT = payload[1] & 0x7;
271
272   GST_LOG_OBJECT (depayload, "P %d, V %d, PLEN %d, PEBIT %d", P, V, PLEN,
273       PEBIT);
274
275   if (V) {
276     header_len++;
277   }
278   if (PLEN) {
279     header_len += PLEN;
280   }
281
282   if ((!P && payload_len < header_len) || (P && payload_len < header_len - 2))
283     goto too_small;
284
285   if (P) {
286     /* FIXME, have to make the packet writable hear. Better to reset these
287      * bytes when we copy the packet below */
288     rtph263pdepay->wait_start = FALSE;
289     header_len -= 2;
290     payload[header_len] = 0;
291     payload[header_len + 1] = 0;
292   }
293
294   if (rtph263pdepay->wait_start)
295     goto waiting_start;
296
297   if (payload_len < header_len)
298     goto too_small;
299
300   /* FIXME do not ignore the VRC header (See RFC 2429 section 4.2) */
301   /* FIXME actually use the RTP picture header when it is lost in the network */
302   /* for now strip off header */
303   payload += header_len;
304   payload_len -= header_len;
305
306   if (M) {
307     /* frame is completed: append to previous, push it out */
308     guint len, padlen;
309     guint avail;
310     GstMapInfo map;
311
312     GST_LOG_OBJECT (depayload, "Frame complete");
313
314     avail = gst_adapter_available (rtph263pdepay->adapter);
315     len = avail + payload_len;
316     padlen = (len % 4) + 4;
317
318     outbuf = gst_buffer_new_and_alloc (len + padlen);
319
320     gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
321     memset (map.data + len, 0, padlen);
322
323     /* prepend previous data */
324     if (avail > 0) {
325       gst_adapter_copy (rtph263pdepay->adapter, map.data, 0, avail);
326       gst_adapter_flush (rtph263pdepay->adapter, avail);
327     }
328     memcpy (map.data + avail, payload, payload_len);
329     gst_buffer_unmap (outbuf, &map);
330     gst_rtp_buffer_unmap (&rtp);
331
332     return outbuf;
333
334   } else {
335     /* frame not completed: store in adapter */
336     outbuf = gst_buffer_new_and_alloc (payload_len);
337
338     GST_LOG_OBJECT (depayload, "Frame incomplete, storing %d", payload_len);
339     gst_buffer_fill (outbuf, 0, payload, payload_len);
340
341     gst_adapter_push (rtph263pdepay->adapter, outbuf);
342     gst_rtp_buffer_unmap (&rtp);
343   }
344   return NULL;
345
346 too_small:
347   {
348     GST_ELEMENT_WARNING (rtph263pdepay, STREAM, DECODE,
349         ("Packet payload was too small"), (NULL));
350     gst_rtp_buffer_unmap (&rtp);
351     return NULL;
352   }
353 waiting_start:
354   {
355     GST_DEBUG_OBJECT (rtph263pdepay, "waiting for picture start");
356     gst_rtp_buffer_unmap (&rtp);
357     return NULL;
358   }
359 }
360
361 static GstStateChangeReturn
362 gst_rtp_h263p_depay_change_state (GstElement * element,
363     GstStateChange transition)
364 {
365   GstRtpH263PDepay *rtph263pdepay;
366   GstStateChangeReturn ret;
367
368   rtph263pdepay = GST_RTP_H263P_DEPAY (element);
369
370   switch (transition) {
371     case GST_STATE_CHANGE_NULL_TO_READY:
372       break;
373     case GST_STATE_CHANGE_READY_TO_PAUSED:
374       gst_adapter_clear (rtph263pdepay->adapter);
375       rtph263pdepay->wait_start = TRUE;
376       break;
377     default:
378       break;
379   }
380
381   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
382
383   switch (transition) {
384     case GST_STATE_CHANGE_READY_TO_NULL:
385       break;
386     default:
387       break;
388   }
389   return ret;
390 }
391
392 gboolean
393 gst_rtp_h263p_depay_plugin_init (GstPlugin * plugin)
394 {
395   return gst_element_register (plugin, "rtph263pdepay",
396       GST_RANK_SECONDARY, GST_TYPE_RTP_H263P_DEPAY);
397 }