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