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., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
26 #include <gst/rtp/gstrtpbuffer.h>
28 #include "gstrtpmp4gdepay.h"
29 #include "gstrtputils.h"
31 GST_DEBUG_CATEGORY_STATIC (rtpmp4gdepay_debug);
32 #define GST_CAT_DEFAULT (rtpmp4gdepay_debug)
34 static GstStaticPadTemplate gst_rtp_mp4g_depay_src_template =
35 GST_STATIC_PAD_TEMPLATE ("src",
38 GST_STATIC_CAPS ("video/mpeg,"
39 "mpegversion=(int) 4,"
40 "systemstream=(boolean)false;"
41 "audio/mpeg," "mpegversion=(int) 4, " "stream-format=(string)raw")
44 static GstStaticPadTemplate gst_rtp_mp4g_depay_sink_template =
45 GST_STATIC_PAD_TEMPLATE ("sink",
48 GST_STATIC_CAPS ("application/x-rtp, "
49 "media = (string) { \"video\", \"audio\", \"application\" }, "
50 "clock-rate = (int) [1, MAX ], "
51 "encoding-name = (string) \"MPEG4-GENERIC\", "
52 /* required string params */
53 /* "streamtype = (string) { \"4\", \"5\" }, " Not set by Wowza 4 = video, 5 = audio */
54 /* "profile-level-id = (string) [1,MAX], " */
55 /* "config = (string) [1,MAX]" */
56 "mode = (string) { \"generic\", \"CELP-cbr\", \"CELP-vbr\", \"AAC-lbr\", \"AAC-hbr\", \"aac-hbr\" } "
57 /* Optional general parameters */
58 /* "objecttype = (string) [1,MAX], " */
59 /* "constantsize = (string) [1,MAX], " *//* constant size of each AU */
60 /* "constantduration = (string) [1,MAX], " *//* constant duration of each AU */
61 /* "maxdisplacement = (string) [1,MAX], " */
62 /* "de-interleavebuffersize = (string) [1,MAX], " */
63 /* Optional configuration parameters */
64 /* "sizelength = (string) [1, 32], " */
65 /* "indexlength = (string) [1, 32], " */
66 /* "indexdeltalength = (string) [1, 32], " */
67 /* "ctsdeltalength = (string) [1, 32], " */
68 /* "dtsdeltalength = (string) [1, 32], " */
69 /* "randomaccessindication = (string) {0, 1}, " */
70 /* "streamstateindication = (string) [0, 32], " */
71 /* "auxiliarydatasizelength = (string) [0, 32]" */ )
74 /* simple bitstream parser */
79 gint head; /* bitpos in the cache of next bit */
80 guint64 cache; /* cached bytes */
84 gst_bs_parse_init (GstBsParse * bs, const guint8 * data, guint size)
87 bs->end = data + size;
89 bs->cache = 0xffffffff;
93 gst_bs_parse_read (GstBsParse * bs, guint n)
101 /* fill up the cache if we need to */
102 while (bs->head < n) {
103 if (bs->data >= bs->end) {
104 /* we're at the end, can't produce more than head number of bits */
108 /* shift bytes in cache, moving the head bits of the cache left */
109 bs->cache = (bs->cache << 8) | *bs->data++;
113 /* bring the required bits down and truncate */
114 if ((shift = bs->head - n) > 0)
115 res = bs->cache >> shift;
119 /* mask out required bits */
129 #define gst_rtp_mp4g_depay_parent_class parent_class
130 G_DEFINE_TYPE (GstRtpMP4GDepay, gst_rtp_mp4g_depay,
131 GST_TYPE_RTP_BASE_DEPAYLOAD);
133 static void gst_rtp_mp4g_depay_finalize (GObject * object);
135 static gboolean gst_rtp_mp4g_depay_setcaps (GstRTPBaseDepayload * depayload,
137 static GstBuffer *gst_rtp_mp4g_depay_process (GstRTPBaseDepayload * depayload,
139 static gboolean gst_rtp_mp4g_depay_handle_event (GstRTPBaseDepayload * filter,
142 static GstStateChangeReturn gst_rtp_mp4g_depay_change_state (GstElement *
143 element, GstStateChange transition);
147 gst_rtp_mp4g_depay_class_init (GstRtpMP4GDepayClass * klass)
149 GObjectClass *gobject_class;
150 GstElementClass *gstelement_class;
151 GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
153 gobject_class = (GObjectClass *) klass;
154 gstelement_class = (GstElementClass *) klass;
155 gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
157 gobject_class->finalize = gst_rtp_mp4g_depay_finalize;
159 gstelement_class->change_state = gst_rtp_mp4g_depay_change_state;
161 gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_mp4g_depay_process;
162 gstrtpbasedepayload_class->set_caps = gst_rtp_mp4g_depay_setcaps;
163 gstrtpbasedepayload_class->handle_event = gst_rtp_mp4g_depay_handle_event;
165 gst_element_class_add_static_pad_template (gstelement_class,
166 &gst_rtp_mp4g_depay_src_template);
167 gst_element_class_add_static_pad_template (gstelement_class,
168 &gst_rtp_mp4g_depay_sink_template);
170 gst_element_class_set_static_metadata (gstelement_class,
171 "RTP MPEG4 ES depayloader", "Codec/Depayloader/Network/RTP",
172 "Extracts MPEG4 elementary streams from RTP packets (RFC 3640)",
173 "Wim Taymans <wim.taymans@gmail.com>");
175 GST_DEBUG_CATEGORY_INIT (rtpmp4gdepay_debug, "rtpmp4gdepay", 0,
176 "MP4-generic RTP Depayloader");
180 gst_rtp_mp4g_depay_init (GstRtpMP4GDepay * rtpmp4gdepay)
182 rtpmp4gdepay->adapter = gst_adapter_new ();
183 rtpmp4gdepay->packets = g_queue_new ();
187 gst_rtp_mp4g_depay_finalize (GObject * object)
189 GstRtpMP4GDepay *rtpmp4gdepay;
191 rtpmp4gdepay = GST_RTP_MP4G_DEPAY (object);
193 g_object_unref (rtpmp4gdepay->adapter);
194 rtpmp4gdepay->adapter = NULL;
195 g_queue_free (rtpmp4gdepay->packets);
196 rtpmp4gdepay->packets = NULL;
198 G_OBJECT_CLASS (parent_class)->finalize (object);
202 gst_rtp_mp4g_depay_parse_int (GstStructure * structure, const gchar * field,
208 if ((str = gst_structure_get_string (structure, field)))
211 if (gst_structure_get_int (structure, field, &res))
218 gst_rtp_mp4g_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
220 GstStructure *structure;
221 GstRtpMP4GDepay *rtpmp4gdepay;
222 GstCaps *srccaps = NULL;
228 rtpmp4gdepay = GST_RTP_MP4G_DEPAY (depayload);
230 structure = gst_caps_get_structure (caps, 0);
232 if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
233 clock_rate = 90000; /* default */
234 depayload->clock_rate = clock_rate;
236 rtpmp4gdepay->check_adts = FALSE;
238 if ((str = gst_structure_get_string (structure, "media"))) {
239 if (strcmp (str, "audio") == 0) {
240 srccaps = gst_caps_new_simple ("audio/mpeg",
241 "mpegversion", G_TYPE_INT, 4, "stream-format", G_TYPE_STRING, "raw",
243 rtpmp4gdepay->check_adts = TRUE;
244 rtpmp4gdepay->warn_adts = TRUE;
245 } else if (strcmp (str, "video") == 0) {
246 srccaps = gst_caps_new_simple ("video/mpeg",
247 "mpegversion", G_TYPE_INT, 4,
248 "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
254 /* these values are optional and have a default value of 0 (no header) */
255 rtpmp4gdepay->sizelength =
256 gst_rtp_mp4g_depay_parse_int (structure, "sizelength", 0);
257 rtpmp4gdepay->indexlength =
258 gst_rtp_mp4g_depay_parse_int (structure, "indexlength", 0);
259 rtpmp4gdepay->indexdeltalength =
260 gst_rtp_mp4g_depay_parse_int (structure, "indexdeltalength", 0);
261 rtpmp4gdepay->ctsdeltalength =
262 gst_rtp_mp4g_depay_parse_int (structure, "ctsdeltalength", 0);
263 rtpmp4gdepay->dtsdeltalength =
264 gst_rtp_mp4g_depay_parse_int (structure, "dtsdeltalength", 0);
266 gst_rtp_mp4g_depay_parse_int (structure, "randomaccessindication", 0);
267 rtpmp4gdepay->randomaccessindication = someint > 0 ? 1 : 0;
268 rtpmp4gdepay->streamstateindication =
269 gst_rtp_mp4g_depay_parse_int (structure, "streamstateindication", 0);
270 rtpmp4gdepay->auxiliarydatasizelength =
271 gst_rtp_mp4g_depay_parse_int (structure, "auxiliarydatasizelength", 0);
272 rtpmp4gdepay->constantSize =
273 gst_rtp_mp4g_depay_parse_int (structure, "constantsize", 0);
274 rtpmp4gdepay->constantDuration =
275 gst_rtp_mp4g_depay_parse_int (structure, "constantduration", 0);
276 rtpmp4gdepay->maxDisplacement =
277 gst_rtp_mp4g_depay_parse_int (structure, "maxdisplacement", 0);
280 /* get config string */
281 if ((str = gst_structure_get_string (structure, "config"))) {
284 g_value_init (&v, GST_TYPE_BUFFER);
285 if (gst_value_deserialize (&v, str)) {
288 buffer = gst_value_get_buffer (&v);
289 gst_caps_set_simple (srccaps,
290 "codec_data", GST_TYPE_BUFFER, buffer, NULL);
293 g_warning ("cannot convert config to buffer");
297 res = gst_pad_set_caps (depayload->srcpad, srccaps);
298 gst_caps_unref (srccaps);
305 GST_DEBUG_OBJECT (rtpmp4gdepay, "Unknown media type");
311 gst_rtp_mp4g_depay_clear_queue (GstRtpMP4GDepay * rtpmp4gdepay)
315 while ((outbuf = g_queue_pop_head (rtpmp4gdepay->packets)))
316 gst_buffer_unref (outbuf);
320 gst_rtp_mp4g_depay_reset (GstRtpMP4GDepay * rtpmp4gdepay)
322 gst_adapter_clear (rtpmp4gdepay->adapter);
323 rtpmp4gdepay->max_AU_index = -1;
324 rtpmp4gdepay->next_AU_index = -1;
325 rtpmp4gdepay->prev_AU_index = -1;
326 rtpmp4gdepay->prev_rtptime = -1;
327 rtpmp4gdepay->last_AU_index = -1;
328 gst_rtp_mp4g_depay_clear_queue (rtpmp4gdepay);
332 gst_rtp_mp4g_depay_push_outbuf (GstRtpMP4GDepay * rtpmp4gdepay,
333 GstBuffer * outbuf, guint AU_index)
335 gboolean discont = FALSE;
337 if (AU_index != rtpmp4gdepay->next_AU_index) {
338 GST_DEBUG_OBJECT (rtpmp4gdepay, "discont, expected AU_index %u",
339 rtpmp4gdepay->next_AU_index);
340 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
344 GST_DEBUG_OBJECT (rtpmp4gdepay, "pushing %sAU_index %u",
345 discont ? "" : "expected ", AU_index);
347 gst_rtp_drop_meta (GST_ELEMENT_CAST (rtpmp4gdepay), outbuf, 0);
348 gst_rtp_base_depayload_push (GST_RTP_BASE_DEPAYLOAD (rtpmp4gdepay), outbuf);
349 rtpmp4gdepay->next_AU_index = AU_index + 1;
353 gst_rtp_mp4g_depay_flush_queue (GstRtpMP4GDepay * rtpmp4gdepay)
358 while ((outbuf = g_queue_pop_head (rtpmp4gdepay->packets))) {
359 AU_index = GST_BUFFER_OFFSET (outbuf);
361 GST_DEBUG_OBJECT (rtpmp4gdepay, "next available AU_index %u", AU_index);
363 gst_rtp_mp4g_depay_push_outbuf (rtpmp4gdepay, outbuf, AU_index);
368 gst_rtp_mp4g_depay_queue (GstRtpMP4GDepay * rtpmp4gdepay, GstBuffer * outbuf)
370 guint AU_index = GST_BUFFER_OFFSET (outbuf);
372 if (rtpmp4gdepay->next_AU_index == -1) {
373 GST_DEBUG_OBJECT (rtpmp4gdepay, "Init AU counter %u", AU_index);
374 rtpmp4gdepay->next_AU_index = AU_index;
377 if (rtpmp4gdepay->next_AU_index == AU_index) {
378 GST_DEBUG_OBJECT (rtpmp4gdepay, "pushing expected AU_index %u", AU_index);
380 /* we received the expected packet, push it and flush as much as we can from
382 gst_rtp_mp4g_depay_push_outbuf (rtpmp4gdepay, outbuf, AU_index);
384 while ((outbuf = g_queue_peek_head (rtpmp4gdepay->packets))) {
385 AU_index = GST_BUFFER_OFFSET (outbuf);
387 GST_DEBUG_OBJECT (rtpmp4gdepay, "next available AU_index %u", AU_index);
389 if (rtpmp4gdepay->next_AU_index == AU_index) {
390 outbuf = g_queue_pop_head (rtpmp4gdepay->packets);
391 gst_rtp_mp4g_depay_push_outbuf (rtpmp4gdepay, outbuf, AU_index);
393 GST_DEBUG_OBJECT (rtpmp4gdepay, "waiting for next AU_index %u",
394 rtpmp4gdepay->next_AU_index);
401 GST_DEBUG_OBJECT (rtpmp4gdepay, "queueing AU_index %u", AU_index);
403 /* loop the list to skip strictly smaller AU_index buffers */
404 for (list = rtpmp4gdepay->packets->head; list; list = g_list_next (list)) {
408 idx = GST_BUFFER_OFFSET (GST_BUFFER_CAST (list->data));
410 /* compare the new seqnum to the one in the buffer */
411 gap = (gint) (idx - AU_index);
413 GST_DEBUG_OBJECT (rtpmp4gdepay, "compare with AU_index %u, gap %d", idx,
416 /* AU_index <= idx, we can stop looking */
417 if (G_LIKELY (gap > 0))
421 g_queue_insert_before (rtpmp4gdepay->packets, list, outbuf);
423 g_queue_push_tail (rtpmp4gdepay->packets, outbuf);
428 gst_rtp_mp4g_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
430 GstRtpMP4GDepay *rtpmp4gdepay;
431 GstBuffer *outbuf = NULL;
432 GstClockTime timestamp;
434 rtpmp4gdepay = GST_RTP_MP4G_DEPAY (depayload);
436 /* flush remaining data on discont */
437 if (GST_BUFFER_IS_DISCONT (rtp->buffer)) {
438 GST_DEBUG_OBJECT (rtpmp4gdepay, "received DISCONT");
439 gst_adapter_clear (rtpmp4gdepay->adapter);
442 timestamp = GST_BUFFER_PTS (rtp->buffer);
445 gint payload_len, payload_AU;
448 guint AU_headers_len;
449 guint AU_size, AU_index, AU_index_delta, payload_AU_size;
452 payload_len = gst_rtp_buffer_get_payload_len (rtp);
453 payload = gst_rtp_buffer_get_payload (rtp);
455 GST_DEBUG_OBJECT (rtpmp4gdepay, "received payload of %d", payload_len);
457 rtptime = gst_rtp_buffer_get_timestamp (rtp);
458 M = gst_rtp_buffer_get_marker (rtp);
460 if (rtpmp4gdepay->sizelength > 0) {
461 gint num_AU_headers, AU_headers_bytes, i;
467 /* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- .. -+-+-+-+-+-+-+-+-+-+
468 * |AU-headers-length|AU-header|AU-header| |AU-header|padding|
469 * | | (1) | (2) | | (n) * | bits |
470 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- .. -+-+-+-+-+-+-+-+-+-+
472 * The length is 2 bytes and contains the length of the following
473 * AU-headers in bits.
475 AU_headers_len = (payload[0] << 8) | payload[1];
476 AU_headers_bytes = (AU_headers_len + 7) / 8;
477 num_AU_headers = AU_headers_len / 16;
479 GST_DEBUG_OBJECT (rtpmp4gdepay, "AU headers len %d, bytes %d, num %d",
480 AU_headers_len, AU_headers_bytes, num_AU_headers);
486 if (payload_len < AU_headers_bytes)
489 /* skip special headers, point to first payload AU */
490 payload_AU = 2 + AU_headers_bytes;
491 payload_AU_size = payload_len - AU_headers_bytes;
493 if (G_UNLIKELY (rtpmp4gdepay->auxiliarydatasizelength)) {
496 /* point the bitstream parser to the first auxiliary data bit */
497 gst_bs_parse_init (&bs, payload + AU_headers_bytes,
498 payload_len - AU_headers_bytes);
500 gst_bs_parse_read (&bs, rtpmp4gdepay->auxiliarydatasizelength);
501 /* convert to bytes */
502 aux_size = (aux_size + 7) / 8;
503 /* AU data then follows auxiliary data */
504 if (payload_AU_size < aux_size)
506 payload_AU += aux_size;
507 payload_AU_size -= aux_size;
510 /* point the bitstream parser to the first AU header bit */
511 gst_bs_parse_init (&bs, payload, payload_len);
512 AU_index = AU_index_delta = 0;
514 for (i = 0; i < num_AU_headers && payload_AU_size > 0; i++) {
516 * +---------------------------------------+
518 * +---------------------------------------+
519 * | AU-Index / AU-Index-delta |
520 * +---------------------------------------+
522 * +---------------------------------------+
524 * +---------------------------------------+
526 * +---------------------------------------+
528 * +---------------------------------------+
530 * +---------------------------------------+
532 * +---------------------------------------+
534 AU_size = gst_bs_parse_read (&bs, rtpmp4gdepay->sizelength);
536 /* calculate the AU_index, which is only on the first AU of the packet
537 * and the AU_index_delta on the other AUs. This will be used to
538 * reconstruct the AU ordering when interleaving. */
540 AU_index = gst_bs_parse_read (&bs, rtpmp4gdepay->indexlength);
542 GST_DEBUG_OBJECT (rtpmp4gdepay, "AU index %u", AU_index);
544 if (AU_index == 0 && rtpmp4gdepay->prev_AU_index == 0) {
548 /* if we see two consecutive packets with AU_index of 0, we can
549 * assume we have constantDuration packets. Since we don't have
550 * the index we must use the AU duration to calculate the
551 * index. Get the diff between the timestamps first, this can be
552 * positive or negative. */
553 if (rtpmp4gdepay->prev_rtptime <= rtptime)
554 diff = rtptime - rtpmp4gdepay->prev_rtptime;
556 diff = -(rtpmp4gdepay->prev_rtptime - rtptime);
558 /* if no constantDuration was given, make one */
559 if (rtpmp4gdepay->constantDuration != 0) {
560 cd = rtpmp4gdepay->constantDuration;
561 GST_DEBUG_OBJECT (depayload, "using constantDuration %d", cd);
562 } else if (rtpmp4gdepay->prev_AU_num > 0) {
563 /* use number of packets and of previous frame */
564 cd = diff / rtpmp4gdepay->prev_AU_num;
565 GST_DEBUG_OBJECT (depayload, "guessing constantDuration %d", cd);
566 if (!GST_BUFFER_IS_DISCONT (rtp->buffer)) {
568 * if we see two consecutive packets with AU_index of 0 and
569 * there has been no discontinuity, we must conclude that this
570 * value of constantDuration is correct from now on. */
571 GST_DEBUG_OBJECT (depayload,
572 "constantDuration of %d detected", cd);
573 rtpmp4gdepay->constantDuration = cd;
576 /* assume this frame has the same number of packets as the
578 cd = diff / num_AU_headers;
579 GST_DEBUG_OBJECT (depayload, "guessing constantDuration %d", cd);
583 /* get the number of packets by dividing with the duration */
589 rtpmp4gdepay->last_AU_index += diff;
590 rtpmp4gdepay->prev_AU_index = AU_index;
592 AU_index = rtpmp4gdepay->last_AU_index;
594 GST_DEBUG_OBJECT (rtpmp4gdepay, "diff %d, AU index %u", diff,
597 rtpmp4gdepay->prev_AU_index = AU_index;
598 rtpmp4gdepay->last_AU_index = AU_index;
601 /* keep track of the highest AU_index */
602 if (rtpmp4gdepay->max_AU_index != -1
603 && rtpmp4gdepay->max_AU_index <= AU_index) {
604 GST_DEBUG_OBJECT (rtpmp4gdepay, "new interleave group, flushing");
605 /* a new interleave group started, flush */
606 gst_rtp_mp4g_depay_flush_queue (rtpmp4gdepay);
608 if (G_UNLIKELY (!rtpmp4gdepay->maxDisplacement &&
609 rtpmp4gdepay->max_AU_index != -1
610 && rtpmp4gdepay->max_AU_index >= AU_index)) {
613 /* some broken non-interleaved streams have AU-index jumping around
614 * all over the place, apparently assuming receiver disregards */
615 GST_DEBUG_OBJECT (rtpmp4gdepay, "non-interleaved broken AU indices;"
616 " forcing continuous flush");
617 /* reset AU to avoid repeated DISCONT in such case */
618 outbuf = g_queue_peek_head (rtpmp4gdepay->packets);
619 if (G_LIKELY (outbuf)) {
620 rtpmp4gdepay->next_AU_index = GST_BUFFER_OFFSET (outbuf);
621 gst_rtp_mp4g_depay_flush_queue (rtpmp4gdepay);
623 /* rebase next_AU_index to current rtp's first AU_index */
624 rtpmp4gdepay->next_AU_index = AU_index;
626 rtpmp4gdepay->prev_rtptime = rtptime;
627 rtpmp4gdepay->prev_AU_num = num_AU_headers;
630 gst_bs_parse_read (&bs, rtpmp4gdepay->indexdeltalength);
631 AU_index += AU_index_delta + 1;
633 /* keep track of highest AU_index */
634 if (rtpmp4gdepay->max_AU_index == -1
635 || AU_index > rtpmp4gdepay->max_AU_index)
636 rtpmp4gdepay->max_AU_index = AU_index;
638 /* the presentation time offset, a 2s-complement value, we need this to
639 * calculate the timestamp on the output packet. */
640 if (rtpmp4gdepay->ctsdeltalength > 0) {
641 if (gst_bs_parse_read (&bs, 1))
642 gst_bs_parse_read (&bs, rtpmp4gdepay->ctsdeltalength);
644 /* the decoding time offset, a 2s-complement value */
645 if (rtpmp4gdepay->dtsdeltalength > 0) {
646 if (gst_bs_parse_read (&bs, 1))
647 gst_bs_parse_read (&bs, rtpmp4gdepay->dtsdeltalength);
649 /* RAP-flag to indicate that the AU contains a keyframe */
650 if (rtpmp4gdepay->randomaccessindication)
651 gst_bs_parse_read (&bs, 1);
653 if (rtpmp4gdepay->streamstateindication > 0)
654 gst_bs_parse_read (&bs, rtpmp4gdepay->streamstateindication);
656 GST_DEBUG_OBJECT (rtpmp4gdepay, "size %d, index %d, delta %d", AU_size,
657 AU_index, AU_index_delta);
659 /* fragmented pakets have the AU_size set to the size of the
660 * unfragmented AU. */
661 if (AU_size > payload_AU_size)
662 AU_size = payload_AU_size;
664 /* collect stuff in the adapter, strip header from payload and push in
667 gst_rtp_buffer_get_payload_subbuffer (rtp, payload_AU, AU_size);
668 gst_adapter_push (rtpmp4gdepay->adapter, outbuf);
674 /* packet is complete, flush */
675 avail = gst_adapter_available (rtpmp4gdepay->adapter);
677 /* Some broken senders send ADTS headers (e.g. some Sony cameras).
678 * Try to detect those and skip them (still needs config set), but
679 * don't check every frame, only the first (unless we detect ADTS) */
680 if (rtpmp4gdepay->check_adts && avail >= 7) {
681 if (gst_adapter_masked_scan_uint32_peek (rtpmp4gdepay->adapter,
682 0xfffe0000, 0xfff00000, 0, 4, &v) == 0) {
683 guint adts_hdr_len = (((v >> 16) & 0x1) == 0) ? 9 : 7;
684 if (avail > adts_hdr_len) {
685 if (rtpmp4gdepay->warn_adts) {
686 GST_WARNING_OBJECT (rtpmp4gdepay, "Detected ADTS header of "
687 "%u bytes, skipping", adts_hdr_len);
688 rtpmp4gdepay->warn_adts = FALSE;
690 gst_adapter_flush (rtpmp4gdepay->adapter, adts_hdr_len);
691 avail -= adts_hdr_len;
694 rtpmp4gdepay->check_adts = FALSE;
695 rtpmp4gdepay->warn_adts = TRUE;
699 outbuf = gst_adapter_take_buffer (rtpmp4gdepay->adapter, avail);
701 /* copy some of the fields we calculated above on the buffer. We also
702 * copy the AU_index so that we can sort the packets in our queue. */
703 GST_BUFFER_PTS (outbuf) = timestamp;
704 GST_BUFFER_OFFSET (outbuf) = AU_index;
706 if (rtpmp4gdepay->constantDuration != 0) {
707 /* if we have constantDuration, calculate timestamp for next AU
708 * in this RTP packet. */
709 timestamp += (rtpmp4gdepay->constantDuration * GST_SECOND) /
710 depayload->clock_rate;
712 /* otherwise, make sure we don't use the timestamp again for other
714 timestamp = GST_CLOCK_TIME_NONE;
717 GST_DEBUG_OBJECT (depayload,
718 "pushing buffer of size %" G_GSIZE_FORMAT,
719 gst_buffer_get_size (outbuf));
721 gst_rtp_mp4g_depay_queue (rtpmp4gdepay, outbuf);
724 payload_AU += AU_size;
725 payload_AU_size -= AU_size;
728 /* push complete buffer in adapter */
729 outbuf = gst_rtp_buffer_get_payload_subbuffer (rtp, 0, payload_len);
730 gst_adapter_push (rtpmp4gdepay->adapter, outbuf);
732 /* if this was the last packet of the VOP, create and push a buffer */
736 avail = gst_adapter_available (rtpmp4gdepay->adapter);
738 outbuf = gst_adapter_take_buffer (rtpmp4gdepay->adapter, avail);
740 GST_DEBUG ("gst_rtp_mp4g_depay_chain: pushing buffer of size %"
741 G_GSIZE_FORMAT, gst_buffer_get_size (outbuf));
753 GST_ELEMENT_WARNING (rtpmp4gdepay, STREAM, DECODE,
754 ("Packet payload was too short."), (NULL));
760 gst_rtp_mp4g_depay_handle_event (GstRTPBaseDepayload * filter, GstEvent * event)
763 GstRtpMP4GDepay *rtpmp4gdepay;
765 rtpmp4gdepay = GST_RTP_MP4G_DEPAY (filter);
767 switch (GST_EVENT_TYPE (event)) {
768 case GST_EVENT_FLUSH_STOP:
769 gst_rtp_mp4g_depay_reset (rtpmp4gdepay);
776 GST_RTP_BASE_DEPAYLOAD_CLASS (parent_class)->handle_event (filter, event);
781 static GstStateChangeReturn
782 gst_rtp_mp4g_depay_change_state (GstElement * element,
783 GstStateChange transition)
785 GstRtpMP4GDepay *rtpmp4gdepay;
786 GstStateChangeReturn ret;
788 rtpmp4gdepay = GST_RTP_MP4G_DEPAY (element);
790 switch (transition) {
791 case GST_STATE_CHANGE_READY_TO_PAUSED:
792 gst_rtp_mp4g_depay_reset (rtpmp4gdepay);
798 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
800 switch (transition) {
801 case GST_STATE_CHANGE_PAUSED_TO_READY:
802 gst_rtp_mp4g_depay_reset (rtpmp4gdepay);
811 gst_rtp_mp4g_depay_plugin_init (GstPlugin * plugin)
813 return gst_element_register (plugin, "rtpmp4gdepay",
814 GST_RANK_SECONDARY, GST_TYPE_RTP_MP4G_DEPAY);