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