2 * Copyright (C) <2005> Wim Taymans <wim@fluendo.com>
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.
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.
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.
26 #include <gst/rtp/gstrtpbuffer.h>
28 #include "gstrtpmp4gdepay.h"
30 GST_DEBUG_CATEGORY_STATIC (rtpmp4gdepay_debug);
31 #define GST_CAT_DEFAULT (rtpmp4gdepay_debug)
33 /* elementfactory information */
34 static const GstElementDetails gst_rtp_mp4gdepay_details =
35 GST_ELEMENT_DETAILS ("RTP packet depayloader",
36 "Codec/Depayloader/Network",
37 "Extracts MPEG4 elementary streams from RTP packets (RFC 3640)",
38 "Wim Taymans <wim@fluendo.com>");
40 static GstStaticPadTemplate gst_rtp_mp4g_depay_src_template =
41 GST_STATIC_PAD_TEMPLATE ("src",
44 GST_STATIC_CAPS ("video/mpeg,"
45 "mpegversion=(int) 4,"
46 "systemstream=(boolean)false;" "audio/mpeg," "mpegversion=(int) 4")
49 static GstStaticPadTemplate gst_rtp_mp4g_depay_sink_template =
50 GST_STATIC_PAD_TEMPLATE ("sink",
53 GST_STATIC_CAPS ("application/x-rtp, "
54 "media = (string) { \"video\", \"audio\", \"application\" }, "
55 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
56 "clock-rate = (int) [1, MAX ], "
57 "encoding-name = (string) \"MPEG4-GENERIC\", "
58 /* required string params */
59 "streamtype = (string) { \"4\", \"5\" }, " /* 4 = video, 5 = audio */
60 /* "profile-level-id = (string) [1,MAX], " */
61 /* "config = (string) [1,MAX]" */
62 "mode = (string) { \"generic\", \"CELP-cbr\", \"CELP-vbr\", \"AAC-lbr\", \"AAC-hbr\" } "
63 /* Optional general parameters */
64 /* "objecttype = (string) [1,MAX], " */
65 /* "constantsize = (string) [1,MAX], " *//* constant size of each AU */
66 /* "constantduration = (string) [1,MAX], " *//* constant duration of each AU */
67 /* "maxdisplacement = (string) [1,MAX], " */
68 /* "de-interleavebuffersize = (string) [1,MAX], " */
69 /* Optional configuration parameters */
70 /* "sizelength = (string) [1, 32], " */
71 /* "indexlength = (string) [1, 32], " */
72 /* "indexdeltalength = (string) [1, 32], " */
73 /* "ctsdeltalength = (string) [1, 32], " */
74 /* "dtsdeltalength = (string) [1, 32], " */
75 /* "randomaccessindication = (string) {0, 1}, " */
76 /* "streamstateindication = (string) [0, 32], " */
77 /* "auxiliarydatasizelength = (string) [0, 32]" */ )
80 /* simple bitstream parser */
85 gint head; /* bitpos in the cache of next bit */
86 guint64 cache; /* cached bytes */
90 gst_bs_parse_init (GstBsParse * bs, const guint8 * data, guint size)
93 bs->end = data + size;
95 bs->cache = 0xffffffff;
99 gst_bs_parse_read (GstBsParse * bs, guint n)
107 /* fill up the cache if we need to */
108 while (bs->head < n) {
109 if (bs->data >= bs->end) {
110 /* we're at the end, can't produce more than head number of bits */
114 /* shift bytes in cache, moving the head bits of the cache left */
115 bs->cache = (bs->cache << 8) | *bs->data++;
119 /* bring the required bits down and truncate */
120 if ((shift = bs->head - n) > 0)
121 res = bs->cache >> shift;
125 /* mask out required bits */
135 GST_BOILERPLATE (GstRtpMP4GDepay, gst_rtp_mp4g_depay, GstBaseRTPDepayload,
136 GST_TYPE_BASE_RTP_DEPAYLOAD);
138 static void gst_rtp_mp4g_depay_finalize (GObject * object);
140 static gboolean gst_rtp_mp4g_depay_setcaps (GstBaseRTPDepayload * depayload,
142 static GstBuffer *gst_rtp_mp4g_depay_process (GstBaseRTPDepayload * depayload,
145 static GstStateChangeReturn gst_rtp_mp4g_depay_change_state (GstElement *
146 element, GstStateChange transition);
150 gst_rtp_mp4g_depay_base_init (gpointer klass)
152 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
154 gst_element_class_add_pad_template (element_class,
155 gst_static_pad_template_get (&gst_rtp_mp4g_depay_src_template));
156 gst_element_class_add_pad_template (element_class,
157 gst_static_pad_template_get (&gst_rtp_mp4g_depay_sink_template));
159 gst_element_class_set_details (element_class, &gst_rtp_mp4gdepay_details);
163 gst_rtp_mp4g_depay_class_init (GstRtpMP4GDepayClass * klass)
165 GObjectClass *gobject_class;
166 GstElementClass *gstelement_class;
167 GstBaseRTPDepayloadClass *gstbasertpdepayload_class;
169 gobject_class = (GObjectClass *) klass;
170 gstelement_class = (GstElementClass *) klass;
171 gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;
173 parent_class = g_type_class_peek_parent (klass);
175 gobject_class->finalize = gst_rtp_mp4g_depay_finalize;
177 gstelement_class->change_state = gst_rtp_mp4g_depay_change_state;
179 gstbasertpdepayload_class->process = gst_rtp_mp4g_depay_process;
180 gstbasertpdepayload_class->set_caps = gst_rtp_mp4g_depay_setcaps;
182 GST_DEBUG_CATEGORY_INIT (rtpmp4gdepay_debug, "rtpmp4gdepay", 0,
183 "MP4-generic RTP Depayloader");
187 gst_rtp_mp4g_depay_init (GstRtpMP4GDepay * rtpmp4gdepay,
188 GstRtpMP4GDepayClass * klass)
190 rtpmp4gdepay->adapter = gst_adapter_new ();
194 gst_rtp_mp4g_depay_finalize (GObject * object)
196 GstRtpMP4GDepay *rtpmp4gdepay;
198 rtpmp4gdepay = GST_RTP_MP4G_DEPAY (object);
200 g_object_unref (rtpmp4gdepay->adapter);
201 rtpmp4gdepay->adapter = NULL;
203 G_OBJECT_CLASS (parent_class)->finalize (object);
207 gst_rtp_mp4g_depay_parse_int (GstStructure * structure, const gchar * field,
213 if ((str = gst_structure_get_string (structure, field)))
216 if (gst_structure_get_int (structure, field, &res))
223 gst_rtp_mp4g_depay_setcaps (GstBaseRTPDepayload * depayload, GstCaps * caps)
226 GstStructure *structure;
227 GstRtpMP4GDepay *rtpmp4gdepay;
228 GstCaps *srccaps = NULL;
230 gint clock_rate = 90000; /* default */
233 rtpmp4gdepay = GST_RTP_MP4G_DEPAY (depayload);
235 structure = gst_caps_get_structure (caps, 0);
237 gst_structure_get_int (structure, "clock-rate", &clock_rate);
238 depayload->clock_rate = clock_rate;
240 if ((str = gst_structure_get_string (structure, "media"))) {
241 if (strcmp (str, "audio") == 0) {
242 srccaps = gst_caps_new_simple ("audio/mpeg",
243 "mpegversion", G_TYPE_INT, 4, NULL);
244 } else if (strcmp (str, "video") == 0) {
245 srccaps = gst_caps_new_simple ("video/mpeg",
246 "mpegversion", G_TYPE_INT, 4,
247 "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
253 /* these values are optional and have a default value of 0 (no header) */
254 rtpmp4gdepay->sizelength =
255 gst_rtp_mp4g_depay_parse_int (structure, "sizelength", 0);
256 rtpmp4gdepay->indexlength =
257 gst_rtp_mp4g_depay_parse_int (structure, "indexlength", 0);
258 rtpmp4gdepay->indexdeltalength =
259 gst_rtp_mp4g_depay_parse_int (structure, "indexdeltalength", 0);
260 rtpmp4gdepay->ctsdeltalength =
261 gst_rtp_mp4g_depay_parse_int (structure, "ctsdeltalength", 0);
262 rtpmp4gdepay->dtsdeltalength =
263 gst_rtp_mp4g_depay_parse_int (structure, "dtsdeltalength", 0);
265 gst_rtp_mp4g_depay_parse_int (structure, "randomaccessindication", 0);
266 rtpmp4gdepay->randomaccessindication = someint > 0 ? 1 : 0;
267 rtpmp4gdepay->streamstateindication =
268 gst_rtp_mp4g_depay_parse_int (structure, "streamstateindication", 0);
269 rtpmp4gdepay->auxiliarydatasizelength =
270 gst_rtp_mp4g_depay_parse_int (structure, "auxiliarydatasizelength", 0);
272 /* get config string */
273 if ((str = gst_structure_get_string (structure, "config"))) {
276 g_value_init (&v, GST_TYPE_BUFFER);
277 if (gst_value_deserialize (&v, str)) {
280 buffer = gst_value_get_buffer (&v);
281 gst_caps_set_simple (srccaps,
282 "codec_data", GST_TYPE_BUFFER, buffer, NULL);
285 g_warning ("cannot convert config to buffer");
289 gst_pad_set_caps (depayload->srcpad, srccaps);
290 gst_caps_unref (srccaps);
297 GST_DEBUG_OBJECT (rtpmp4gdepay, "Unknown media type");
303 gst_rtp_mp4g_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
305 GstRtpMP4GDepay *rtpmp4gdepay;
308 rtpmp4gdepay = GST_RTP_MP4G_DEPAY (depayload);
310 if (!gst_rtp_buffer_validate (buf))
313 /* flush remaining data on discont */
314 if (GST_BUFFER_IS_DISCONT (buf)) {
315 gst_adapter_clear (rtpmp4gdepay->adapter);
319 gint payload_len, payload_AU;
322 guint AU_headers_len;
323 guint AU_size, AU_index, payload_AU_size;
326 payload_len = gst_rtp_buffer_get_payload_len (buf);
327 payload = gst_rtp_buffer_get_payload (buf);
329 timestamp = gst_rtp_buffer_get_timestamp (buf);
330 M = gst_rtp_buffer_get_marker (buf);
332 if (rtpmp4gdepay->sizelength > 0) {
333 gint num_AU_headers, AU_headers_bytes, i;
339 /* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- .. -+-+-+-+-+-+-+-+-+-+
340 * |AU-headers-length|AU-header|AU-header| |AU-header|padding|
341 * | | (1) | (2) | | (n) * | bits |
342 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- .. -+-+-+-+-+-+-+-+-+-+
344 * The length is 2 bytes and contains the length of the following
345 * AU-headers in bits.
347 AU_headers_len = (payload[0] << 8) | payload[1];
348 AU_headers_bytes = (AU_headers_len + 7) / 8;
349 num_AU_headers = AU_headers_len / 16;
351 GST_DEBUG_OBJECT (rtpmp4gdepay, "AU headers len %d, bytes %d, num %d",
352 AU_headers_len, AU_headers_bytes, num_AU_headers);
358 if (payload_len < AU_headers_bytes)
361 /* skip special headers, point to first payload AU */
362 payload_AU = 2 + AU_headers_bytes;
363 payload_AU_size = payload_len - AU_headers_bytes;
365 /* point the bitstream parser to the first AU header bit */
366 gst_bs_parse_init (&bs, payload, payload_len);
368 for (i = 0; i < num_AU_headers && payload_AU_size > 0; i++) {
370 * +---------------------------------------+
372 * +---------------------------------------+
373 * | AU-Index / AU-Index-delta |
374 * +---------------------------------------+
376 * +---------------------------------------+
378 * +---------------------------------------+
380 * +---------------------------------------+
382 * +---------------------------------------+
384 * +---------------------------------------+
386 * +---------------------------------------+
388 AU_size = gst_bs_parse_read (&bs, rtpmp4gdepay->sizelength);
390 AU_index = gst_bs_parse_read (&bs, rtpmp4gdepay->indexlength);
392 AU_index = gst_bs_parse_read (&bs, rtpmp4gdepay->indexdeltalength);
393 if (rtpmp4gdepay->ctsdeltalength > 0) {
394 if (gst_bs_parse_read (&bs, 1))
395 gst_bs_parse_read (&bs, rtpmp4gdepay->ctsdeltalength);
397 if (rtpmp4gdepay->dtsdeltalength > 0) {
398 if (gst_bs_parse_read (&bs, 1))
399 gst_bs_parse_read (&bs, rtpmp4gdepay->dtsdeltalength);
401 if (rtpmp4gdepay->randomaccessindication)
402 gst_bs_parse_read (&bs, 1);
403 if (rtpmp4gdepay->streamstateindication > 0)
404 gst_bs_parse_read (&bs, rtpmp4gdepay->streamstateindication);
406 GST_DEBUG_OBJECT (rtpmp4gdepay, "size %d, index %d", AU_size, AU_index);
408 /* fragmented pakets have the AU_size set to the size of the
409 * unfragmented AU. */
410 if (AU_size > payload_AU_size)
411 AU_size = payload_AU_size;
413 /* collect stuff in the adapter, strip header from payload and push in
416 gst_rtp_buffer_get_payload_subbuffer (buf, payload_AU, AU_size);
417 gst_adapter_push (rtpmp4gdepay->adapter, outbuf);
422 /* packet is complete, flush */
423 avail = gst_adapter_available (rtpmp4gdepay->adapter);
425 outbuf = gst_adapter_take_buffer (rtpmp4gdepay->adapter, avail);
426 gst_buffer_set_caps (outbuf, GST_PAD_CAPS (depayload->srcpad));
428 GST_DEBUG ("gst_rtp_mp4g_depay_chain: pushing buffer of size %d",
429 GST_BUFFER_SIZE (outbuf));
431 /* only apply the timestamp for the first buffer */
433 gst_base_rtp_depayload_push_ts (depayload, timestamp, outbuf);
435 gst_base_rtp_depayload_push (depayload, outbuf);
437 payload_AU += AU_size;
438 payload_AU_size -= AU_size;
441 /* push complete buffer in adapter */
442 outbuf = gst_rtp_buffer_get_payload_subbuffer (buf, 0, payload_len);
443 gst_adapter_push (rtpmp4gdepay->adapter, outbuf);
445 /* if this was the last packet of the VOP, create and push a buffer */
449 avail = gst_adapter_available (rtpmp4gdepay->adapter);
451 outbuf = gst_adapter_take_buffer (rtpmp4gdepay->adapter, avail);
452 gst_buffer_set_caps (outbuf, GST_PAD_CAPS (depayload->srcpad));
454 GST_DEBUG ("gst_rtp_mp4g_depay_chain: pushing buffer of size %d",
455 GST_BUFFER_SIZE (outbuf));
466 GST_ELEMENT_WARNING (rtpmp4gdepay, STREAM, DECODE,
467 ("Packet did not validate."), (NULL));
472 GST_ELEMENT_WARNING (rtpmp4gdepay, STREAM, DECODE,
473 ("Packet payload was too short."), (NULL));
478 static GstStateChangeReturn
479 gst_rtp_mp4g_depay_change_state (GstElement * element,
480 GstStateChange transition)
482 GstRtpMP4GDepay *rtpmp4gdepay;
483 GstStateChangeReturn ret;
485 rtpmp4gdepay = GST_RTP_MP4G_DEPAY (element);
487 switch (transition) {
488 case GST_STATE_CHANGE_READY_TO_PAUSED:
489 gst_adapter_clear (rtpmp4gdepay->adapter);
495 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
497 switch (transition) {
505 gst_rtp_mp4g_depay_plugin_init (GstPlugin * plugin)
507 return gst_element_register (plugin, "rtpmp4gdepay",
508 GST_RANK_MARGINAL, GST_TYPE_RTP_MP4G_DEPAY);