3 * Copyright 2007 Nokia Corporation
4 * Copyright 2007 Collabora Ltd,
5 * @author: Philippe Kalaf <philippe.kalaf@collabora.co.uk>
7 * Copyright (C) <2005> Wim Taymans <wim.taymans@gmail.com>
8 * <2007> Edward Hervey <bilboed@bilboed.com>
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.
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.
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.
32 #include <gst/rtp/gstrtpbuffer.h>
33 #include <gst/video/video.h>
34 #include "gstrtph263depay.h"
35 #include "gstrtputils.h"
37 GST_DEBUG_CATEGORY_STATIC (rtph263depay_debug);
38 #define GST_CAT_DEFAULT (rtph263depay_debug)
40 #define GST_RFC2190A_HEADER_LEN 4
41 #define GST_RFC2190B_HEADER_LEN 8
42 #define GST_RFC2190C_HEADER_LEN 12
44 static GstStaticPadTemplate gst_rtp_h263_depay_src_template =
45 GST_STATIC_PAD_TEMPLATE ("src",
48 GST_STATIC_CAPS ("video/x-h263, "
49 "variant = (string) \"itu\", " "h263version = (string) \"h263\"")
52 static GstStaticPadTemplate gst_rtp_h263_depay_sink_template =
53 GST_STATIC_PAD_TEMPLATE ("sink",
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\", "
64 "media = (string) \"video\", "
65 "clock-rate = (int) 90000, " "encoding-name = (string) \"H263\""
66 /* optional SDP attribute:
67 * "a-framesize = (string) \"1234-1234\", "
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);
76 static void gst_rtp_h263_depay_finalize (GObject * object);
78 static GstStateChangeReturn gst_rtp_h263_depay_change_state (GstElement *
79 element, GstStateChange transition);
81 static GstBuffer *gst_rtp_h263_depay_process (GstRTPBaseDepayload * depayload,
83 gboolean gst_rtp_h263_depay_setcaps (GstRTPBaseDepayload * filter,
87 gst_rtp_h263_depay_class_init (GstRtpH263DepayClass * klass)
89 GObjectClass *gobject_class;
90 GstElementClass *gstelement_class;
91 GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
93 GST_DEBUG_CATEGORY_INIT (rtph263depay_debug, "rtph263depay", 0,
94 "H263 Video RTP Depayloader");
96 gobject_class = (GObjectClass *) klass;
97 gstelement_class = (GstElementClass *) klass;
98 gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
100 gobject_class->finalize = gst_rtp_h263_depay_finalize;
102 gstelement_class->change_state = gst_rtp_h263_depay_change_state;
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);
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>");
115 gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_h263_depay_process;
116 gstrtpbasedepayload_class->set_caps = gst_rtp_h263_depay_setcaps;
120 gst_rtp_h263_depay_init (GstRtpH263Depay * rtph263depay)
122 rtph263depay->adapter = gst_adapter_new ();
124 rtph263depay->offset = 0;
125 rtph263depay->leftover = 0;
129 gst_rtp_h263_depay_finalize (GObject * object)
131 GstRtpH263Depay *rtph263depay;
133 rtph263depay = GST_RTP_H263_DEPAY (object);
135 g_object_unref (rtph263depay->adapter);
136 rtph263depay->adapter = NULL;
138 G_OBJECT_CLASS (parent_class)->finalize (object);
142 gst_rtp_h263_parse_framesize (GstRTPBaseDepayload * filter,
143 const gchar * media_attr, GstCaps * srccaps)
145 gchar *dimension, *endptr;
149 width = g_ascii_strtoull (media_attr, &endptr, 10);
151 GST_ERROR_OBJECT (filter,
152 "Framesize media attribute width out of valid range");
154 } else if (*endptr != '-') {
155 GST_ERROR_OBJECT (filter,
156 "Framesize media attribute has invalid dimension separator");
160 dimension = endptr + 1;
161 height = g_ascii_strtoull (dimension, &endptr, 10);
163 GST_ERROR_OBJECT (filter,
164 "Framesize media attribute height out of valid range");
166 } else if (*endptr != '\0') {
167 GST_ERROR_OBJECT (filter,
168 "Framesize media attribute unexpectedly has trailing characters");
172 d = gst_caps_get_structure (srccaps, 0);
173 gst_structure_set (d, "width", G_TYPE_INT, width, "height", G_TYPE_INT,
180 gst_rtp_h263_depay_setcaps (GstRTPBaseDepayload * filter, GstCaps * caps)
183 GstStructure *structure = gst_caps_get_structure (caps, 0);
185 const gchar *framesize;
187 srccaps = gst_caps_new_simple ("video/x-h263",
188 "variant", G_TYPE_STRING, "itu",
189 "h263version", G_TYPE_STRING, "h263", NULL);
191 if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
192 clock_rate = 90000; /* default */
193 filter->clock_rate = clock_rate;
195 framesize = gst_structure_get_string (structure, "a-framesize");
196 if (framesize != NULL) {
197 if (!gst_rtp_h263_parse_framesize (filter, framesize, srccaps)) {
202 gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (filter), srccaps);
203 gst_caps_unref (srccaps);
209 gst_rtp_h263_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
211 GstRtpH263Depay *rtph263depay;
220 rtph263depay = GST_RTP_H263_DEPAY (depayload);
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;
231 payload_len = gst_rtp_buffer_get_payload_len (rtp);
232 payload = gst_rtp_buffer_get_payload (rtp);
234 M = gst_rtp_buffer_get_marker (rtp);
239 /* Let's see what mode we are using */
240 F = (payload[0] & 0x80) == 0x80;
241 P = (payload[0] & 0x40) == 0x40;
244 SBIT = (payload[0] & 0x38) >> 3;
245 EBIT = (payload[0] & 0x07);
247 /* Figure out header length and I-flag */
249 /* F == 0 and P == 0 or 1
251 header_len = GST_RFC2190A_HEADER_LEN;
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 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
260 if (payload_len <= header_len)
262 I = (payload[1] & 0x10) == 0x10;
267 header_len = GST_RFC2190B_HEADER_LEN;
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 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
278 if (payload_len <= header_len)
280 I = (payload[4] & 0x80) == 0x80;
284 header_len = GST_RFC2190C_HEADER_LEN;
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 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
297 if (payload_len <= header_len)
299 I = (payload[4] & 0x80) == 0x80;
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);
309 payload += header_len;
310 payload_len -= header_len;
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");
322 rtph263depay->psc_I = I;
324 GST_DEBUG ("no frame start yet, skipping payload");
329 /* only trust I info from Mode A starting packet
330 * from buggy payloaders or hw */
331 I = rtph263depay->psc_I;
334 /* take the leftover and merge it at the beginning, FIXME make the buffer
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;
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);
354 /* Take the entire buffer except for the last byte */
355 tmp = gst_rtp_buffer_get_payload_subbuffer (rtp, header_len,
357 gst_adapter_push (rtph263depay->adapter, tmp);
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);
369 if (rtph263depay->start) {
370 /* frame is completed */
373 if (rtph263depay->offset) {
374 /* push in the leftover */
375 GstBuffer *buf = gst_buffer_new_and_alloc (1);
377 GST_DEBUG ("Pushing leftover in adapter");
378 gst_buffer_fill (buf, 0, &rtph263depay->leftover, 1);
379 gst_adapter_push (rtph263depay->adapter, buf);
382 avail = gst_adapter_available (rtph263depay->adapter);
383 outbuf = gst_adapter_take_buffer (rtph263depay->adapter, avail);
386 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
388 GST_DEBUG ("Pushing out a buffer of %d bytes", avail);
390 gst_rtp_drop_non_video_meta (rtph263depay, outbuf);
392 gst_rtp_base_depayload_push (depayload, outbuf);
393 rtph263depay->offset = 0;
394 rtph263depay->leftover = 0;
395 rtph263depay->start = FALSE;
397 rtph263depay->start = TRUE;
405 GST_ELEMENT_WARNING (rtph263depay, STREAM, DECODE,
406 ("Packet payload was too small"), (NULL));
411 static GstStateChangeReturn
412 gst_rtp_h263_depay_change_state (GstElement * element,
413 GstStateChange transition)
415 GstRtpH263Depay *rtph263depay;
416 GstStateChangeReturn ret;
418 rtph263depay = GST_RTP_H263_DEPAY (element);
420 switch (transition) {
421 case GST_STATE_CHANGE_NULL_TO_READY:
423 case GST_STATE_CHANGE_READY_TO_PAUSED:
424 gst_adapter_clear (rtph263depay->adapter);
425 rtph263depay->start = TRUE;
431 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
433 switch (transition) {
434 case GST_STATE_CHANGE_READY_TO_NULL:
443 gst_rtp_h263_depay_plugin_init (GstPlugin * plugin)
445 return gst_element_register (plugin, "rtph263depay",
446 GST_RANK_SECONDARY, GST_TYPE_RTP_H263_DEPAY);