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., 51 Franklin St, Fifth Floor,
23  * Boston, MA 02110-1301, 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 <gst/video/video.h>
34 #include "gstrtph263depay.h"
35 #include "gstrtputils.h"
36 
37 GST_DEBUG_CATEGORY_STATIC (rtph263depay_debug);
38 #define GST_CAT_DEFAULT (rtph263depay_debug)
39 
40 #define GST_RFC2190A_HEADER_LEN 4
41 #define GST_RFC2190B_HEADER_LEN 8
42 #define GST_RFC2190C_HEADER_LEN 12
43 
44 static GstStaticPadTemplate gst_rtp_h263_depay_src_template =
45 GST_STATIC_PAD_TEMPLATE ("src",
46     GST_PAD_SRC,
47     GST_PAD_ALWAYS,
48     GST_STATIC_CAPS ("video/x-h263, "
49         "variant = (string) \"itu\", " "h263version = (string) \"h263\"")
50     );
51 
52 static GstStaticPadTemplate gst_rtp_h263_depay_sink_template =
53     GST_STATIC_PAD_TEMPLATE ("sink",
54     GST_PAD_SINK,
55     GST_PAD_ALWAYS,
56     GST_STATIC_CAPS ("application/x-rtp, "
57         "media = (string) \"video\", "
58         "payload = (int) " GST_RTP_PAYLOAD_H263_STRING ", "
59         "clock-rate = (int) 90000; "
60         /* optional SDP attribute:
61          * "a-framesize = (string) \"1234-1234\", "
62          */
63         "application/x-rtp, "
64         "media = (string) \"video\", "
65         "clock-rate = (int) 90000, " "encoding-name = (string) \"H263\""
66         /* optional SDP attribute:
67          * "a-framesize = (string) \"1234-1234\", "
68          */
69     )
70     );
71 
72 #define gst_rtp_h263_depay_parent_class parent_class
73 G_DEFINE_TYPE (GstRtpH263Depay, gst_rtp_h263_depay,
74     GST_TYPE_RTP_BASE_DEPAYLOAD);
75 
76 static void gst_rtp_h263_depay_finalize (GObject * object);
77 
78 static GstStateChangeReturn gst_rtp_h263_depay_change_state (GstElement *
79     element, GstStateChange transition);
80 
81 static GstBuffer *gst_rtp_h263_depay_process (GstRTPBaseDepayload * depayload,
82     GstRTPBuffer * rtp);
83 gboolean gst_rtp_h263_depay_setcaps (GstRTPBaseDepayload * filter,
84     GstCaps * caps);
85 
86 static void
gst_rtp_h263_depay_class_init(GstRtpH263DepayClass * klass)87 gst_rtp_h263_depay_class_init (GstRtpH263DepayClass * klass)
88 {
89   GObjectClass *gobject_class;
90   GstElementClass *gstelement_class;
91   GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
92 
93   GST_DEBUG_CATEGORY_INIT (rtph263depay_debug, "rtph263depay", 0,
94       "H263 Video RTP Depayloader");
95 
96   gobject_class = (GObjectClass *) klass;
97   gstelement_class = (GstElementClass *) klass;
98   gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
99 
100   gobject_class->finalize = gst_rtp_h263_depay_finalize;
101 
102   gstelement_class->change_state = gst_rtp_h263_depay_change_state;
103 
104   gst_element_class_add_static_pad_template (gstelement_class,
105       &gst_rtp_h263_depay_src_template);
106   gst_element_class_add_static_pad_template (gstelement_class,
107       &gst_rtp_h263_depay_sink_template);
108 
109   gst_element_class_set_static_metadata (gstelement_class,
110       "RTP H263 depayloader", "Codec/Depayloader/Network/RTP",
111       "Extracts H263 video from RTP packets (RFC 2190)",
112       "Philippe Kalaf <philippe.kalaf@collabora.co.uk>, "
113       "Edward Hervey <bilboed@bilboed.com>");
114 
115   gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_h263_depay_process;
116   gstrtpbasedepayload_class->set_caps = gst_rtp_h263_depay_setcaps;
117 }
118 
119 static void
gst_rtp_h263_depay_init(GstRtpH263Depay * rtph263depay)120 gst_rtp_h263_depay_init (GstRtpH263Depay * rtph263depay)
121 {
122   rtph263depay->adapter = gst_adapter_new ();
123 
124   rtph263depay->offset = 0;
125   rtph263depay->leftover = 0;
126 }
127 
128 static void
gst_rtp_h263_depay_finalize(GObject * object)129 gst_rtp_h263_depay_finalize (GObject * object)
130 {
131   GstRtpH263Depay *rtph263depay;
132 
133   rtph263depay = GST_RTP_H263_DEPAY (object);
134 
135   g_object_unref (rtph263depay->adapter);
136   rtph263depay->adapter = NULL;
137 
138   G_OBJECT_CLASS (parent_class)->finalize (object);
139 }
140 
141 static gboolean
gst_rtp_h263_parse_framesize(GstRTPBaseDepayload * filter,const gchar * media_attr,GstCaps * srccaps)142 gst_rtp_h263_parse_framesize (GstRTPBaseDepayload * filter,
143     const gchar * media_attr, GstCaps * srccaps)
144 {
145   gchar *dimension, *endptr;
146   gint width, height;
147   GstStructure *d;
148 
149   width = g_ascii_strtoull (media_attr, &endptr, 10);
150   if (width <= 0) {
151     GST_ERROR_OBJECT (filter,
152         "Framesize media attribute width out of valid range");
153     return FALSE;
154   } else if (*endptr != '-') {
155     GST_ERROR_OBJECT (filter,
156         "Framesize media attribute has invalid dimension separator");
157     return FALSE;
158   }
159 
160   dimension = endptr + 1;
161   height = g_ascii_strtoull (dimension, &endptr, 10);
162   if (height <= 0) {
163     GST_ERROR_OBJECT (filter,
164         "Framesize media attribute height out of valid range");
165     return FALSE;
166   } else if (*endptr != '\0') {
167     GST_ERROR_OBJECT (filter,
168         "Framesize media attribute unexpectedly has trailing characters");
169     return FALSE;
170   }
171 
172   d = gst_caps_get_structure (srccaps, 0);
173   gst_structure_set (d, "width", G_TYPE_INT, width, "height", G_TYPE_INT,
174       height, NULL);
175 
176   return TRUE;
177 }
178 
179 gboolean
gst_rtp_h263_depay_setcaps(GstRTPBaseDepayload * filter,GstCaps * caps)180 gst_rtp_h263_depay_setcaps (GstRTPBaseDepayload * filter, GstCaps * caps)
181 {
182   GstCaps *srccaps;
183   GstStructure *structure = gst_caps_get_structure (caps, 0);
184   gint clock_rate;
185   const gchar *framesize;
186 
187   srccaps = gst_caps_new_simple ("video/x-h263",
188       "variant", G_TYPE_STRING, "itu",
189       "h263version", G_TYPE_STRING, "h263", NULL);
190 
191   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
192     clock_rate = 90000;         /* default */
193   filter->clock_rate = clock_rate;
194 
195   framesize = gst_structure_get_string (structure, "a-framesize");
196   if (framesize != NULL) {
197     if (!gst_rtp_h263_parse_framesize (filter, framesize, srccaps)) {
198       return FALSE;
199     }
200   }
201 
202   gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (filter), srccaps);
203   gst_caps_unref (srccaps);
204 
205   return TRUE;
206 }
207 
208 static GstBuffer *
gst_rtp_h263_depay_process(GstRTPBaseDepayload * depayload,GstRTPBuffer * rtp)209 gst_rtp_h263_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
210 {
211   GstRtpH263Depay *rtph263depay;
212   GstBuffer *outbuf;
213   gint payload_len;
214   guint8 *payload;
215   guint header_len;
216   guint SBIT, EBIT;
217   gboolean F, P, M;
218   gboolean I;
219 
220   rtph263depay = GST_RTP_H263_DEPAY (depayload);
221 
222   /* flush remaining data on discont */
223   if (GST_BUFFER_IS_DISCONT (rtp->buffer)) {
224     GST_LOG_OBJECT (depayload, "Discont buffer, flushing adapter");
225     gst_adapter_clear (rtph263depay->adapter);
226     rtph263depay->offset = 0;
227     rtph263depay->leftover = 0;
228     rtph263depay->start = FALSE;
229   }
230 
231   payload_len = gst_rtp_buffer_get_payload_len (rtp);
232   payload = gst_rtp_buffer_get_payload (rtp);
233 
234   M = gst_rtp_buffer_get_marker (rtp);
235 
236   if (payload_len < 1)
237     goto too_small;
238 
239   /* Let's see what mode we are using */
240   F = (payload[0] & 0x80) == 0x80;
241   P = (payload[0] & 0x40) == 0x40;
242 
243   /* Bit shifting */
244   SBIT = (payload[0] & 0x38) >> 3;
245   EBIT = (payload[0] & 0x07);
246 
247   /* Figure out header length and I-flag */
248   if (F == 0) {
249     /* F == 0 and P == 0 or 1
250      * mode A */
251     header_len = GST_RFC2190A_HEADER_LEN;
252     GST_LOG ("Mode A");
253 
254     /* 0                   1                   2                   3
255      * 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
256      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
257      * |F|P|SBIT |EBIT | SRC |I|U|S|A|R      |DBQ| TRB |    TR         |
258      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
259      */
260     if (payload_len <= header_len)
261       goto too_small;
262     I = (payload[1] & 0x10) == 0x10;
263   } else {
264     if (P == 0) {
265       /* F == 1 and P == 0
266        * mode B */
267       header_len = GST_RFC2190B_HEADER_LEN;
268       GST_LOG ("Mode B");
269 
270       /* 0                   1                   2                   3
271        * 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
272        * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
273        * |F|P|SBIT |EBIT | SRC | QUANT   |  GOBN   |   MBA           |R  |
274        * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
275        * |I|U|S|A| HMV1        | VMV1        | HMV2        | VMV2        |
276        * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
277        */
278       if (payload_len <= header_len)
279         goto too_small;
280       I = (payload[4] & 0x80) == 0x80;
281     } else {
282       /* F == 1 and P == 1
283        * mode C */
284       header_len = GST_RFC2190C_HEADER_LEN;
285       GST_LOG ("Mode C");
286 
287       /* 0                   1                   2                   3
288        * 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
289        * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
290        * |F|P|SBIT |EBIT | SRC | QUANT   |  GOBN   |   MBA           |R  |
291        * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
292        * |I|U|S|A| HMV1        | VMV1        | HMV2        | VMV2        |
293        * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
294        * | RR                                  |DBQ| TRB |    TR         |
295        * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
296        */
297       if (payload_len <= header_len)
298         goto too_small;
299       I = (payload[4] & 0x80) == 0x80;
300     }
301   }
302 
303   GST_LOG ("F/P/M/I : %d/%d/%d/%d", F, P, M, I);
304   GST_LOG ("SBIT : %d , EBIT : %d", SBIT, EBIT);
305   GST_LOG ("payload_len : %d, header_len : %d , leftover : 0x%x",
306       payload_len, header_len, rtph263depay->leftover);
307 
308   /* skip header */
309   payload += header_len;
310   payload_len -= header_len;
311 
312   if (!rtph263depay->start) {
313     /* only mode A should be used when there is a picture start code, but
314      * buggy payloaders may send mode B/C in start of frame */
315     if (payload_len > 4 && (GST_READ_UINT32_BE (payload) >> 10 == 0x20)) {
316       GST_DEBUG ("Mode %c with PSC => frame start", "ABC"[F + P]);
317       rtph263depay->start = TRUE;
318       if ((! !(payload[4] & 0x02)) != I) {
319         GST_DEBUG ("Wrong Picture Coding Type Flag in rtp header");
320         I = !I;
321       }
322       rtph263depay->psc_I = I;
323     } else {
324       GST_DEBUG ("no frame start yet, skipping payload");
325       goto skip;
326     }
327   }
328 
329   /* only trust I info from Mode A starting packet
330    * from buggy payloaders or hw */
331   I = rtph263depay->psc_I;
332 
333   if (SBIT) {
334     /* take the leftover and merge it at the beginning, FIXME make the buffer
335      * data writable. */
336     GST_LOG ("payload[0] : 0x%x", payload[0]);
337     payload[0] &= 0xFF >> SBIT;
338     GST_LOG ("payload[0] : 0x%x", payload[0]);
339     payload[0] |= rtph263depay->leftover;
340     GST_LOG ("payload[0] : 0x%x", payload[0]);
341     rtph263depay->leftover = 0;
342     rtph263depay->offset = 0;
343   }
344 
345   if (!EBIT) {
346     GstBuffer *tmp;
347 
348     /* Take the entire buffer */
349     tmp = gst_rtp_buffer_get_payload_subbuffer (rtp, header_len, payload_len);
350     gst_adapter_push (rtph263depay->adapter, tmp);
351   } else {
352     GstBuffer *tmp;
353 
354     /* Take the entire buffer except for the last byte */
355     tmp = gst_rtp_buffer_get_payload_subbuffer (rtp, header_len,
356         payload_len - 1);
357     gst_adapter_push (rtph263depay->adapter, tmp);
358 
359     /* Put the last byte into the leftover */
360     GST_DEBUG ("payload[payload_len - 1] : 0x%x", payload[payload_len - 1]);
361     GST_DEBUG ("mask : 0x%x", 0xFF << EBIT);
362     rtph263depay->leftover = (payload[payload_len - 1] >> EBIT) << EBIT;
363     rtph263depay->offset = 1;
364     GST_DEBUG ("leftover : 0x%x", rtph263depay->leftover);
365   }
366 
367 skip:
368   if (M) {
369     if (rtph263depay->start) {
370       /* frame is completed */
371       guint avail;
372 
373       if (rtph263depay->offset) {
374         /* push in the leftover */
375         GstBuffer *buf = gst_buffer_new_and_alloc (1);
376 
377         GST_DEBUG ("Pushing leftover in adapter");
378         gst_buffer_fill (buf, 0, &rtph263depay->leftover, 1);
379         gst_adapter_push (rtph263depay->adapter, buf);
380       }
381 
382       avail = gst_adapter_available (rtph263depay->adapter);
383       outbuf = gst_adapter_take_buffer (rtph263depay->adapter, avail);
384 
385       if (I)
386         GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
387 
388       GST_DEBUG ("Pushing out a buffer of %d bytes", avail);
389 
390       gst_rtp_drop_non_video_meta (rtph263depay, outbuf);
391 
392       gst_rtp_base_depayload_push (depayload, outbuf);
393       rtph263depay->offset = 0;
394       rtph263depay->leftover = 0;
395       rtph263depay->start = FALSE;
396     } else {
397       rtph263depay->start = TRUE;
398     }
399   }
400 
401   return NULL;
402 
403 too_small:
404   {
405     GST_ELEMENT_WARNING (rtph263depay, STREAM, DECODE,
406         ("Packet payload was too small"), (NULL));
407     return NULL;
408   }
409 }
410 
411 static GstStateChangeReturn
gst_rtp_h263_depay_change_state(GstElement * element,GstStateChange transition)412 gst_rtp_h263_depay_change_state (GstElement * element,
413     GstStateChange transition)
414 {
415   GstRtpH263Depay *rtph263depay;
416   GstStateChangeReturn ret;
417 
418   rtph263depay = GST_RTP_H263_DEPAY (element);
419 
420   switch (transition) {
421     case GST_STATE_CHANGE_NULL_TO_READY:
422       break;
423     case GST_STATE_CHANGE_READY_TO_PAUSED:
424       gst_adapter_clear (rtph263depay->adapter);
425       rtph263depay->start = TRUE;
426       break;
427     default:
428       break;
429   }
430 
431   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
432 
433   switch (transition) {
434     case GST_STATE_CHANGE_READY_TO_NULL:
435       break;
436     default:
437       break;
438   }
439   return ret;
440 }
441 
442 gboolean
gst_rtp_h263_depay_plugin_init(GstPlugin * plugin)443 gst_rtp_h263_depay_plugin_init (GstPlugin * plugin)
444 {
445   return gst_element_register (plugin, "rtph263depay",
446       GST_RANK_SECONDARY, GST_TYPE_RTP_H263_DEPAY);
447 }
448