2 * Copyright (C) <2005> Wim Taymans <wim.taymans@gmail.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 MPEG4 ES depayloader",
36 "Codec/Depayloader/Network",
37 "Extracts MPEG4 elementary streams from RTP packets (RFC 3640)",
38 "Wim Taymans <wim.taymans@gmail.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 ();
191 rtpmp4gdepay->packets = g_queue_new ();
195 gst_rtp_mp4g_depay_finalize (GObject * object)
197 GstRtpMP4GDepay *rtpmp4gdepay;
199 rtpmp4gdepay = GST_RTP_MP4G_DEPAY (object);
201 g_object_unref (rtpmp4gdepay->adapter);
202 rtpmp4gdepay->adapter = NULL;
203 g_queue_free (rtpmp4gdepay->packets);
204 rtpmp4gdepay->packets = NULL;
206 G_OBJECT_CLASS (parent_class)->finalize (object);
210 gst_rtp_mp4g_depay_parse_int (GstStructure * structure, const gchar * field,
216 if ((str = gst_structure_get_string (structure, field)))
219 if (gst_structure_get_int (structure, field, &res))
226 gst_rtp_mp4g_depay_setcaps (GstBaseRTPDepayload * depayload, GstCaps * caps)
228 GstStructure *structure;
229 GstRtpMP4GDepay *rtpmp4gdepay;
230 GstCaps *srccaps = NULL;
236 rtpmp4gdepay = GST_RTP_MP4G_DEPAY (depayload);
238 structure = gst_caps_get_structure (caps, 0);
240 if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
241 clock_rate = 90000; /* default */
242 depayload->clock_rate = clock_rate;
244 if ((str = gst_structure_get_string (structure, "media"))) {
245 if (strcmp (str, "audio") == 0) {
246 srccaps = gst_caps_new_simple ("audio/mpeg",
247 "mpegversion", G_TYPE_INT, 4, NULL);
248 /* AAC always has a default constant duration of 1024 but it can be
249 * overriden below. */
250 rtpmp4gdepay->constantDuration = 1024;
251 } else if (strcmp (str, "video") == 0) {
252 srccaps = gst_caps_new_simple ("video/mpeg",
253 "mpegversion", G_TYPE_INT, 4,
254 "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
255 rtpmp4gdepay->constantDuration = 0;
261 /* these values are optional and have a default value of 0 (no header) */
262 rtpmp4gdepay->sizelength =
263 gst_rtp_mp4g_depay_parse_int (structure, "sizelength", 0);
264 rtpmp4gdepay->indexlength =
265 gst_rtp_mp4g_depay_parse_int (structure, "indexlength", 0);
266 rtpmp4gdepay->indexdeltalength =
267 gst_rtp_mp4g_depay_parse_int (structure, "indexdeltalength", 0);
268 rtpmp4gdepay->ctsdeltalength =
269 gst_rtp_mp4g_depay_parse_int (structure, "ctsdeltalength", 0);
270 rtpmp4gdepay->dtsdeltalength =
271 gst_rtp_mp4g_depay_parse_int (structure, "dtsdeltalength", 0);
273 gst_rtp_mp4g_depay_parse_int (structure, "randomaccessindication", 0);
274 rtpmp4gdepay->randomaccessindication = someint > 0 ? 1 : 0;
275 rtpmp4gdepay->streamstateindication =
276 gst_rtp_mp4g_depay_parse_int (structure, "streamstateindication", 0);
277 rtpmp4gdepay->auxiliarydatasizelength =
278 gst_rtp_mp4g_depay_parse_int (structure, "auxiliarydatasizelength", 0);
279 rtpmp4gdepay->constantSize =
280 gst_rtp_mp4g_depay_parse_int (structure, "constantsize", 0);
281 rtpmp4gdepay->constantDuration =
282 gst_rtp_mp4g_depay_parse_int (structure, "constantduration",
283 rtpmp4gdepay->constantDuration);
286 /* get config string */
287 if ((str = gst_structure_get_string (structure, "config"))) {
290 g_value_init (&v, GST_TYPE_BUFFER);
291 if (gst_value_deserialize (&v, str)) {
294 buffer = gst_value_get_buffer (&v);
295 gst_caps_set_simple (srccaps,
296 "codec_data", GST_TYPE_BUFFER, buffer, NULL);
299 g_warning ("cannot convert config to buffer");
303 res = gst_pad_set_caps (depayload->srcpad, srccaps);
304 gst_caps_unref (srccaps);
311 GST_DEBUG_OBJECT (rtpmp4gdepay, "Unknown media type");
317 gst_rtp_mp4g_depay_clear_queue (GstRtpMP4GDepay * rtpmp4gdepay)
321 while ((outbuf = g_queue_pop_head (rtpmp4gdepay->packets)))
322 gst_buffer_unref (outbuf);
326 gst_rtp_mp4g_depay_flush_queue (GstRtpMP4GDepay * rtpmp4gdepay)
329 gboolean discont = FALSE;
332 while ((outbuf = g_queue_pop_head (rtpmp4gdepay->packets))) {
333 AU_index = GST_BUFFER_OFFSET (outbuf);
335 GST_DEBUG_OBJECT (rtpmp4gdepay, "next available AU_index %u", AU_index);
337 if (rtpmp4gdepay->next_AU_index != AU_index) {
338 GST_DEBUG_OBJECT (rtpmp4gdepay, "discont, expected AU_index %u",
339 rtpmp4gdepay->next_AU_index);
344 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
348 GST_DEBUG_OBJECT (rtpmp4gdepay, "pushing AU_index %u", AU_index);
349 gst_base_rtp_depayload_push (GST_BASE_RTP_DEPAYLOAD (rtpmp4gdepay), outbuf);
350 rtpmp4gdepay->next_AU_index = AU_index + 1;
355 gst_rtp_mp4g_depay_queue (GstRtpMP4GDepay * rtpmp4gdepay, GstBuffer * outbuf)
357 guint AU_index = GST_BUFFER_OFFSET (outbuf);
359 if (rtpmp4gdepay->next_AU_index == -1) {
360 GST_DEBUG_OBJECT (rtpmp4gdepay, "Init AU counter %u", AU_index);
361 rtpmp4gdepay->next_AU_index = AU_index;
364 if (rtpmp4gdepay->next_AU_index == AU_index) {
365 GST_DEBUG_OBJECT (rtpmp4gdepay, "pushing expected AU_index %u", AU_index);
367 /* we received the expected packet, push it and flush as much as we can from
369 gst_base_rtp_depayload_push (GST_BASE_RTP_DEPAYLOAD (rtpmp4gdepay), outbuf);
370 rtpmp4gdepay->next_AU_index++;
372 while ((outbuf = g_queue_peek_head (rtpmp4gdepay->packets))) {
373 AU_index = GST_BUFFER_OFFSET (outbuf);
375 GST_DEBUG_OBJECT (rtpmp4gdepay, "next available AU_index %u", AU_index);
377 if (rtpmp4gdepay->next_AU_index == AU_index) {
378 GST_DEBUG_OBJECT (rtpmp4gdepay, "pushing expected AU_index %u",
380 outbuf = g_queue_pop_head (rtpmp4gdepay->packets);
381 gst_base_rtp_depayload_push (GST_BASE_RTP_DEPAYLOAD (rtpmp4gdepay),
383 rtpmp4gdepay->next_AU_index++;
385 GST_DEBUG_OBJECT (rtpmp4gdepay, "waiting for next AU_index %u",
386 rtpmp4gdepay->next_AU_index);
393 GST_DEBUG_OBJECT (rtpmp4gdepay, "queueing AU_index %u", AU_index);
395 /* loop the list to skip strictly smaller AU_index buffers */
396 for (list = rtpmp4gdepay->packets->head; list; list = g_list_next (list)) {
400 idx = GST_BUFFER_OFFSET (GST_BUFFER_CAST (list->data));
402 /* compare the new seqnum to the one in the buffer */
403 gap = (gint) (idx - AU_index);
405 GST_DEBUG_OBJECT (rtpmp4gdepay, "compare with AU_index %u, gap %d", idx,
408 /* AU_index <= idx, we can stop looking */
409 if (G_LIKELY (gap > 0))
413 g_queue_insert_before (rtpmp4gdepay->packets, list, outbuf);
415 g_queue_push_tail (rtpmp4gdepay->packets, outbuf);
420 gst_rtp_mp4g_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
422 GstRtpMP4GDepay *rtpmp4gdepay;
424 GstClockTime timestamp;
426 rtpmp4gdepay = GST_RTP_MP4G_DEPAY (depayload);
428 /* flush remaining data on discont */
429 if (GST_BUFFER_IS_DISCONT (buf)) {
430 GST_DEBUG_OBJECT (rtpmp4gdepay, "received DISCONT");
431 gst_adapter_clear (rtpmp4gdepay->adapter);
434 timestamp = GST_BUFFER_TIMESTAMP (buf);
437 gint payload_len, payload_AU;
440 guint AU_headers_len;
441 guint AU_size, AU_index, AU_index_delta, payload_AU_size;
444 payload_len = gst_rtp_buffer_get_payload_len (buf);
445 payload = gst_rtp_buffer_get_payload (buf);
447 rtptime = gst_rtp_buffer_get_timestamp (buf);
448 M = gst_rtp_buffer_get_marker (buf);
450 if (rtpmp4gdepay->sizelength > 0) {
451 gint num_AU_headers, AU_headers_bytes, i;
457 /* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- .. -+-+-+-+-+-+-+-+-+-+
458 * |AU-headers-length|AU-header|AU-header| |AU-header|padding|
459 * | | (1) | (2) | | (n) * | bits |
460 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- .. -+-+-+-+-+-+-+-+-+-+
462 * The length is 2 bytes and contains the length of the following
463 * AU-headers in bits.
465 AU_headers_len = (payload[0] << 8) | payload[1];
466 AU_headers_bytes = (AU_headers_len + 7) / 8;
467 num_AU_headers = AU_headers_len / 16;
469 GST_DEBUG_OBJECT (rtpmp4gdepay, "AU headers len %d, bytes %d, num %d",
470 AU_headers_len, AU_headers_bytes, num_AU_headers);
476 if (payload_len < AU_headers_bytes)
479 /* skip special headers, point to first payload AU */
480 payload_AU = 2 + AU_headers_bytes;
481 payload_AU_size = payload_len - AU_headers_bytes;
483 /* point the bitstream parser to the first AU header bit */
484 gst_bs_parse_init (&bs, payload, payload_len);
485 AU_index = AU_index_delta = 0;
487 for (i = 0; i < num_AU_headers && payload_AU_size > 0; i++) {
489 * +---------------------------------------+
491 * +---------------------------------------+
492 * | AU-Index / AU-Index-delta |
493 * +---------------------------------------+
495 * +---------------------------------------+
497 * +---------------------------------------+
499 * +---------------------------------------+
501 * +---------------------------------------+
503 * +---------------------------------------+
505 * +---------------------------------------+
507 AU_size = gst_bs_parse_read (&bs, rtpmp4gdepay->sizelength);
509 /* calculate the AU_index, which is only on the first AU of the packet
510 * and the AU_index_delta on the other AUs. This will be used to
511 * reconstruct the AU ordering when interleaving. */
513 AU_index = gst_bs_parse_read (&bs, rtpmp4gdepay->indexlength);
514 if (AU_index == 0 && rtpmp4gdepay->prev_AU_index == 0) {
517 /* if we see two consecutive packets with AU_index of 0, we can
518 * assume we have constantDuration packets. Since we don't have
519 * the index we must use the AU duration to calculate the
520 * index. Get the diff between the timestamps first, this can be
521 * positive or negative. */
522 if (rtpmp4gdepay->prev_rtptime <= rtptime)
523 diff = rtptime - rtpmp4gdepay->prev_rtptime;
525 diff = -(rtpmp4gdepay->prev_rtptime - rtptime);
527 /* get the number of packets by dividing with the duration */
528 diff /= rtpmp4gdepay->constantDuration;
530 rtpmp4gdepay->last_AU_index += diff;
531 rtpmp4gdepay->prev_AU_index = AU_index;
533 AU_index = rtpmp4gdepay->last_AU_index;
536 rtpmp4gdepay->prev_AU_index = AU_index;
537 rtpmp4gdepay->last_AU_index = AU_index;
540 /* keep track of the higest AU_index */
541 if (rtpmp4gdepay->max_AU_index != -1
542 && rtpmp4gdepay->max_AU_index <= AU_index) {
543 GST_DEBUG_OBJECT (rtpmp4gdepay, "new interleave group, flushing");
544 /* a new interleave group started, flush */
545 gst_rtp_mp4g_depay_flush_queue (rtpmp4gdepay);
547 rtpmp4gdepay->prev_rtptime = rtptime;
550 gst_bs_parse_read (&bs, rtpmp4gdepay->indexdeltalength);
551 AU_index += AU_index_delta + 1;
553 /* keep track of highest AU_index */
554 if (rtpmp4gdepay->max_AU_index == -1
555 || AU_index > rtpmp4gdepay->max_AU_index)
556 rtpmp4gdepay->max_AU_index = AU_index;
558 /* the presentation time offset, a 2s-complement value, we need this to
559 * calculate the timestamp on the output packet. */
560 if (rtpmp4gdepay->ctsdeltalength > 0) {
561 if (gst_bs_parse_read (&bs, 1))
562 gst_bs_parse_read (&bs, rtpmp4gdepay->ctsdeltalength);
564 /* the decoding time offset, a 2s-complement value */
565 if (rtpmp4gdepay->dtsdeltalength > 0) {
566 if (gst_bs_parse_read (&bs, 1))
567 gst_bs_parse_read (&bs, rtpmp4gdepay->dtsdeltalength);
569 /* RAP-flag to indicate that the AU contains a keyframe */
570 if (rtpmp4gdepay->randomaccessindication)
571 gst_bs_parse_read (&bs, 1);
573 if (rtpmp4gdepay->streamstateindication > 0)
574 gst_bs_parse_read (&bs, rtpmp4gdepay->streamstateindication);
576 GST_DEBUG_OBJECT (rtpmp4gdepay, "size %d, index %d, delta %d", AU_size,
577 AU_index, AU_index_delta);
579 /* fragmented pakets have the AU_size set to the size of the
580 * unfragmented AU. */
581 if (AU_size > payload_AU_size)
582 AU_size = payload_AU_size;
584 /* collect stuff in the adapter, strip header from payload and push in
587 gst_rtp_buffer_get_payload_subbuffer (buf, payload_AU, AU_size);
588 gst_adapter_push (rtpmp4gdepay->adapter, outbuf);
593 /* packet is complete, flush */
594 avail = gst_adapter_available (rtpmp4gdepay->adapter);
596 outbuf = gst_adapter_take_buffer (rtpmp4gdepay->adapter, avail);
597 gst_buffer_set_caps (outbuf, GST_PAD_CAPS (depayload->srcpad));
599 /* copy some of the fields we calculated above on the buffer. We also
600 * copy the AU_index so that we can sort the packets in our queue. */
601 GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
602 GST_BUFFER_OFFSET (outbuf) = AU_index;
604 /* make sure we don't use the timestamp again for other AUs in this
608 GST_DEBUG_OBJECT (depayload, "pushing buffer of size %d",
609 GST_BUFFER_SIZE (outbuf));
611 gst_rtp_mp4g_depay_queue (rtpmp4gdepay, outbuf);
614 payload_AU += AU_size;
615 payload_AU_size -= AU_size;
618 /* push complete buffer in adapter */
619 outbuf = gst_rtp_buffer_get_payload_subbuffer (buf, 0, payload_len);
620 gst_adapter_push (rtpmp4gdepay->adapter, outbuf);
622 /* if this was the last packet of the VOP, create and push a buffer */
626 avail = gst_adapter_available (rtpmp4gdepay->adapter);
628 outbuf = gst_adapter_take_buffer (rtpmp4gdepay->adapter, avail);
630 GST_DEBUG ("gst_rtp_mp4g_depay_chain: pushing buffer of size %d",
631 GST_BUFFER_SIZE (outbuf));
642 GST_ELEMENT_WARNING (rtpmp4gdepay, STREAM, DECODE,
643 ("Packet payload was too short."), (NULL));
648 static GstStateChangeReturn
649 gst_rtp_mp4g_depay_change_state (GstElement * element,
650 GstStateChange transition)
652 GstRtpMP4GDepay *rtpmp4gdepay;
653 GstStateChangeReturn ret;
655 rtpmp4gdepay = GST_RTP_MP4G_DEPAY (element);
657 switch (transition) {
658 case GST_STATE_CHANGE_READY_TO_PAUSED:
659 gst_adapter_clear (rtpmp4gdepay->adapter);
660 rtpmp4gdepay->max_AU_index = -1;
661 rtpmp4gdepay->next_AU_index = -1;
662 rtpmp4gdepay->prev_AU_index = -1;
663 rtpmp4gdepay->prev_rtptime = -1;
664 rtpmp4gdepay->last_AU_index = -1;
670 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
672 switch (transition) {
673 case GST_STATE_CHANGE_PAUSED_TO_READY:
674 gst_adapter_clear (rtpmp4gdepay->adapter);
675 gst_rtp_mp4g_depay_clear_queue (rtpmp4gdepay);
684 gst_rtp_mp4g_depay_plugin_init (GstPlugin * plugin)
686 return gst_element_register (plugin, "rtpmp4gdepay",
687 GST_RANK_MARGINAL, GST_TYPE_RTP_MP4G_DEPAY);