2 * Copyright (C) <2010> Mark Nauwelaerts <mark.nauwelaerts@collabora.co.uk>
3 * Copyright (C) <2010> Nokia Corporation
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
25 #include <gst/rtp/gstrtpbuffer.h>
29 #include "gstrtpmparobustdepay.h"
31 GST_DEBUG_CATEGORY_STATIC (rtpmparobustdepay_debug);
32 #define GST_CAT_DEFAULT (rtpmparobustdepay_debug)
34 static GstStaticPadTemplate gst_rtp_mpa_robust_depay_src_template =
35 GST_STATIC_PAD_TEMPLATE ("src",
38 GST_STATIC_CAPS ("audio/mpeg, " "mpegversion = (int) 1")
41 static GstStaticPadTemplate gst_rtp_mpa_robust_depay_sink_template =
42 GST_STATIC_PAD_TEMPLATE ("sink",
45 GST_STATIC_CAPS ("application/x-rtp, "
46 "media = (string) \"audio\", "
47 "clock-rate = (int) 90000, "
48 "encoding-name = (string) \"MPA-ROBUST\" " "; "
49 /* draft versions appear still in use out there */
51 "media = (string) \"audio\", "
52 "clock-rate = (int) [1, MAX], "
53 "encoding-name = (string) { \"X-MP3-DRAFT-00\", \"X-MP3-DRAFT-01\", "
54 " \"X-MP3-DRAFT-02\", \"X-MP3-DRAFT-03\", \"X-MP3-DRAFT-04\", "
55 " \"X-MP3-DRAFT-05\", \"X-MP3-DRAFT-06\" }")
58 typedef struct _GstADUFrame
70 #define gst_rtp_mpa_robust_depay_parent_class parent_class
71 G_DEFINE_TYPE (GstRtpMPARobustDepay, gst_rtp_mpa_robust_depay,
72 GST_TYPE_RTP_BASE_DEPAYLOAD);
74 static GstStateChangeReturn gst_rtp_mpa_robust_change_state (GstElement *
75 element, GstStateChange transition);
77 static gboolean gst_rtp_mpa_robust_depay_setcaps (GstRTPBaseDepayload *
78 depayload, GstCaps * caps);
79 static GstBuffer *gst_rtp_mpa_robust_depay_process (GstRTPBaseDepayload *
80 depayload, GstBuffer * buf);
83 gst_rtp_mpa_robust_depay_finalize (GObject * object)
85 GstRtpMPARobustDepay *rtpmpadepay;
87 rtpmpadepay = (GstRtpMPARobustDepay *) object;
89 g_object_unref (rtpmpadepay->adapter);
90 g_queue_free (rtpmpadepay->adu_frames);
92 G_OBJECT_CLASS (parent_class)->finalize (object);
96 gst_rtp_mpa_robust_depay_class_init (GstRtpMPARobustDepayClass * klass)
98 GObjectClass *gobject_class;
99 GstElementClass *gstelement_class;
100 GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
102 GST_DEBUG_CATEGORY_INIT (rtpmparobustdepay_debug, "rtpmparobustdepay", 0,
103 "Robust MPEG Audio RTP Depayloader");
105 gobject_class = (GObjectClass *) klass;
106 gstelement_class = (GstElementClass *) klass;
107 gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
109 gobject_class->finalize = gst_rtp_mpa_robust_depay_finalize;
111 gstelement_class->change_state =
112 GST_DEBUG_FUNCPTR (gst_rtp_mpa_robust_change_state);
114 gst_element_class_add_pad_template (gstelement_class,
115 gst_static_pad_template_get (&gst_rtp_mpa_robust_depay_src_template));
116 gst_element_class_add_pad_template (gstelement_class,
117 gst_static_pad_template_get (&gst_rtp_mpa_robust_depay_sink_template));
119 gst_element_class_set_static_metadata (gstelement_class,
120 "RTP MPEG audio depayloader", "Codec/Depayloader/Network/RTP",
121 "Extracts MPEG audio from RTP packets (RFC 5219)",
122 "Mark Nauwelaerts <mark.nauwelaerts@collabora.co.uk>");
124 gstrtpbasedepayload_class->set_caps = gst_rtp_mpa_robust_depay_setcaps;
125 gstrtpbasedepayload_class->process = gst_rtp_mpa_robust_depay_process;
129 gst_rtp_mpa_robust_depay_init (GstRtpMPARobustDepay * rtpmpadepay)
131 rtpmpadepay->adapter = gst_adapter_new ();
132 rtpmpadepay->adu_frames = g_queue_new ();
136 gst_rtp_mpa_robust_depay_setcaps (GstRTPBaseDepayload * depayload,
139 GstRtpMPARobustDepay *rtpmpadepay;
140 GstStructure *structure;
142 gint clock_rate, draft;
144 const gchar *encoding;
146 rtpmpadepay = GST_RTP_MPA_ROBUST_DEPAY (depayload);
148 structure = gst_caps_get_structure (caps, 0);
150 if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
152 depayload->clock_rate = clock_rate;
154 rtpmpadepay->has_descriptor = TRUE;
155 if ((encoding = gst_structure_get_string (structure, "encoding-name"))) {
156 if (sscanf (encoding, "X-MP3-DRAFT-%d", &draft) && (draft == 0))
157 rtpmpadepay->has_descriptor = FALSE;
161 gst_caps_new_simple ("audio/mpeg", "mpegversion", G_TYPE_INT, 1, NULL);
162 res = gst_pad_set_caps (depayload->srcpad, outcaps);
163 gst_caps_unref (outcaps);
168 /* thanks again go to mp3parse ... */
170 static const guint mp3types_bitrates[2][3][16] = {
172 {0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448,},
173 {0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384,},
174 {0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320,}
177 {0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256,},
178 {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160,},
179 {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160,}
183 static const guint mp3types_freqs[3][3] = { {44100, 48000, 32000},
184 {22050, 24000, 16000},
189 mp3_type_frame_length_from_header (GstElement * mp3parse, guint32 header,
190 guint * put_version, guint * put_layer, guint * put_channels,
191 guint * put_bitrate, guint * put_samplerate, guint * put_mode,
195 gulong mode, samplerate, bitrate, layer, channels, padding, crc;
199 if (header & (1 << 20)) {
200 lsf = (header & (1 << 19)) ? 0 : 1;
207 version = 1 + lsf + mpg25;
209 layer = 4 - ((header >> 17) & 0x3);
211 crc = (header >> 16) & 0x1;
213 bitrate = (header >> 12) & 0xF;
214 bitrate = mp3types_bitrates[lsf][layer - 1][bitrate] * 1000;
215 /* The caller has ensured we have a valid header, so bitrate can't be
218 GST_DEBUG_OBJECT (mp3parse, "invalid bitrate");
222 samplerate = (header >> 10) & 0x3;
223 samplerate = mp3types_freqs[lsf + mpg25][samplerate];
225 padding = (header >> 9) & 0x1;
227 mode = (header >> 6) & 0x3;
228 channels = (mode == 3) ? 1 : 2;
232 length = 4 * ((bitrate * 12) / samplerate + padding);
235 length = (bitrate * 144) / samplerate + padding;
239 length = (bitrate * 144) / (samplerate << lsf) + padding;
243 GST_LOG_OBJECT (mp3parse, "Calculated mp3 frame length of %u bytes", length);
244 GST_LOG_OBJECT (mp3parse, "samplerate = %lu, bitrate = %lu, version = %lu, "
245 "layer = %lu, channels = %lu, mode = %lu", samplerate, bitrate, version,
246 layer, channels, mode);
249 *put_version = version;
253 *put_channels = channels;
255 *put_bitrate = bitrate;
257 *put_samplerate = samplerate;
263 GST_LOG_OBJECT (mp3parse, "size = %u", length);
267 /* generate empty/silent/dummy frame that mimics @frame,
268 * except for rate, where maximum possible is selected */
270 gst_rtp_mpa_robust_depay_generate_dummy_frame (GstRtpMPARobustDepay *
271 rtpmpadepay, GstADUFrame * frame)
276 dummy = g_slice_dup (GstADUFrame, frame);
278 /* go for maximum bitrate */
279 dummy->header = (frame->header & ~(0xf << 12)) | (0xe << 12);
281 mp3_type_frame_length_from_header (GST_ELEMENT_CAST (rtpmpadepay),
282 dummy->header, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
283 dummy->data_size = dummy->size - 4 - dummy->side_info;
284 dummy->backpointer = 0;
286 dummy->buffer = gst_buffer_new_and_alloc (dummy->side_info + 4);
288 gst_buffer_map (dummy->buffer, &map, GST_MAP_WRITE);
289 memset (map.data, 0, map.size);
290 GST_WRITE_UINT32_BE (map.data, dummy->header);
291 gst_buffer_unmap (dummy->buffer, &map);
293 GST_BUFFER_TIMESTAMP (dummy->buffer) = GST_BUFFER_TIMESTAMP (frame->buffer);
298 /* validates and parses @buf, and queues for further transformation if valid,
299 * otherwise discards @buf
300 * Takes ownership of @buf. */
302 gst_rtp_mpa_robust_depay_queue_frame (GstRtpMPARobustDepay * rtpmpadepay,
305 GstADUFrame *frame = NULL;
306 guint version, layer, channels, size;
310 g_return_val_if_fail (buf != NULL, FALSE);
312 gst_buffer_map (buf, &map, GST_MAP_READ);
317 frame = g_slice_new0 (GstADUFrame);
318 frame->header = GST_READ_UINT32_BE (map.data);
320 size = mp3_type_frame_length_from_header (GST_ELEMENT_CAST (rtpmpadepay),
321 frame->header, &version, &layer, &channels, NULL, NULL, NULL, &crc);
326 frame->layer = layer;
327 if (version == 1 && channels == 2)
328 frame->side_info = 32;
329 else if ((version == 1 && channels == 1) || (version >= 2 && channels == 2))
330 frame->side_info = 17;
331 else if (version >= 2 && channels == 1)
332 frame->side_info = 9;
334 g_assert_not_reached ();
340 frame->backpointer = GST_READ_UINT16_BE (map.data + 4);
341 frame->backpointer >>= 7;
342 GST_LOG_OBJECT (rtpmpadepay, "backpointer: %d", frame->backpointer);
346 frame->side_info += 2;
348 GST_LOG_OBJECT (rtpmpadepay, "side info: %d", frame->side_info);
349 frame->data_size = frame->size - 4 - frame->side_info;
351 /* some size validation checks */
352 if (4 + frame->side_info > map.size)
355 /* ADU data would then extend past MP3 frame,
356 * even using past byte reservoir */
357 if (-frame->backpointer + (gint) (map.size) > frame->size)
360 gst_buffer_unmap (buf, &map);
362 /* ok, take buffer and queue */
364 g_queue_push_tail (rtpmpadepay->adu_frames, frame);
371 GST_DEBUG_OBJECT (rtpmpadepay, "frame is corrupt");
372 gst_buffer_unmap (buf, &map);
373 gst_buffer_unref (buf);
375 g_slice_free (GstADUFrame, frame);
381 gst_rtp_mpa_robust_depay_free_frame (GstADUFrame * frame)
384 gst_buffer_unref (frame->buffer);
385 g_slice_free (GstADUFrame, frame);
389 gst_rtp_mpa_robust_depay_dequeue_frame (GstRtpMPARobustDepay * rtpmpadepay)
393 GST_LOG_OBJECT (rtpmpadepay, "dequeueing ADU frame");
395 if (rtpmpadepay->adu_frames->head == rtpmpadepay->cur_adu_frame)
396 rtpmpadepay->cur_adu_frame = NULL;
398 head = g_queue_pop_head (rtpmpadepay->adu_frames);
399 g_assert (head->buffer);
400 gst_rtp_mpa_robust_depay_free_frame (head);
405 /* returns TRUE if at least one new ADU frame was enqueued for MP3 conversion.
406 * Takes ownership of @buf. */
408 gst_rtp_mpa_robust_depay_deinterleave (GstRtpMPARobustDepay * rtpmpadepay,
411 gboolean ret = FALSE;
413 guint val, iindex, icc;
415 gst_buffer_map (buf, &map, GST_MAP_READ);
416 val = GST_READ_UINT16_BE (map.data) >> 5;
417 gst_buffer_unmap (buf, &map);
422 GST_LOG_OBJECT (rtpmpadepay, "sync: 0x%x, index: %u, cycle count: %u",
425 /* basic case; no interleaving ever seen */
426 if (val == 0x7ff && rtpmpadepay->last_icc < 0) {
427 ret = gst_rtp_mpa_robust_depay_queue_frame (rtpmpadepay, buf);
429 if (G_UNLIKELY (rtpmpadepay->last_icc < 0)) {
430 rtpmpadepay->last_icc = icc;
431 rtpmpadepay->last_ii = iindex;
433 if (icc != rtpmpadepay->last_icc || iindex == rtpmpadepay->last_ii) {
436 for (i = 0; i < 256; ++i) {
437 if (rtpmpadepay->deinter[i] != NULL) {
438 ret |= gst_rtp_mpa_robust_depay_queue_frame (rtpmpadepay,
439 rtpmpadepay->deinter[i]);
440 rtpmpadepay->deinter[i] = NULL;
444 /* rewrite buffer sync header */
445 gst_buffer_map (buf, &map, GST_MAP_READWRITE);
446 val = GST_READ_UINT16_BE (map.data);
447 val = (0x7ff << 5) | val;
448 GST_WRITE_UINT16_BE (map.data, val);
449 gst_buffer_unmap (buf, &map);
450 /* store and keep track of last indices */
451 rtpmpadepay->last_icc = icc;
452 rtpmpadepay->last_ii = iindex;
453 rtpmpadepay->deinter[iindex] = buf;
459 /* Head ADU frame corresponds to mp3_frame (i.e. in header in side-info) that
460 * is currently being written
461 * cur_adu_frame refers to ADU frame whose data should be bytewritten next
462 * (possibly starting from offset rather than start 0) (and is typicall tail
463 * at time of last push round).
464 * If at start, position where it should start writing depends on (data) sizes
465 * of previous mp3 frames (corresponding to foregoing ADU frames) kept in size,
466 * and its backpointer */
468 gst_rtp_mpa_robust_depay_push_mp3_frames (GstRtpMPARobustDepay * rtpmpadepay)
471 GstADUFrame *frame, *head;
473 GstFlowReturn ret = GST_FLOW_OK;
478 if (G_UNLIKELY (!rtpmpadepay->cur_adu_frame)) {
479 rtpmpadepay->cur_adu_frame = rtpmpadepay->adu_frames->head;
480 rtpmpadepay->offset = 0;
481 rtpmpadepay->size = 0;
484 if (G_UNLIKELY (!rtpmpadepay->cur_adu_frame))
487 frame = (GstADUFrame *) rtpmpadepay->cur_adu_frame->data;
488 head = (GstADUFrame *) rtpmpadepay->adu_frames->head->data;
490 /* special case: non-layer III are sent straight through */
491 if (G_UNLIKELY (frame->layer != 3)) {
492 GST_DEBUG_OBJECT (rtpmpadepay, "layer %d frame, sending as-is",
494 gst_rtp_base_depayload_push (GST_RTP_BASE_DEPAYLOAD (rtpmpadepay),
496 frame->buffer = NULL;
497 /* and remove it from any further consideration */
498 g_slice_free (GstADUFrame, frame);
499 g_queue_delete_link (rtpmpadepay->adu_frames, rtpmpadepay->cur_adu_frame);
500 rtpmpadepay->cur_adu_frame = NULL;
504 if (rtpmpadepay->offset == gst_buffer_get_size (frame->buffer)) {
505 if (g_list_next (rtpmpadepay->cur_adu_frame)) {
506 rtpmpadepay->size += frame->data_size;
507 rtpmpadepay->cur_adu_frame = g_list_next (rtpmpadepay->cur_adu_frame);
508 frame = (GstADUFrame *) rtpmpadepay->cur_adu_frame->data;
509 rtpmpadepay->offset = 0;
510 GST_LOG_OBJECT (rtpmpadepay,
511 "moving to next ADU frame, size %d, side_info %d, backpointer %d",
512 frame->size, frame->side_info, frame->backpointer);
513 /* layer I and II packets have no bitreservoir and must be sent as-is;
514 * so flush any pending frame */
515 if (G_UNLIKELY (frame->layer != 3 && rtpmpadepay->mp3_frame))
522 if (G_UNLIKELY (!rtpmpadepay->mp3_frame)) {
523 GST_LOG_OBJECT (rtpmpadepay,
524 "setting up new MP3 frame of size %d, side_info %d",
525 head->size, head->side_info);
526 rtpmpadepay->mp3_frame = gst_byte_writer_new_with_size (head->size, TRUE);
527 /* 0-fill possible gaps */
528 gst_byte_writer_fill_unchecked (rtpmpadepay->mp3_frame, 0, head->size);
529 gst_byte_writer_set_pos (rtpmpadepay->mp3_frame, 0);
530 /* bytewriter corresponds to head frame,
531 * i.e. the header and the side info must match */
532 g_assert (4 + head->side_info <= head->size);
533 gst_buffer_map (head->buffer, &map, GST_MAP_READ);
534 gst_byte_writer_put_data_unchecked (rtpmpadepay->mp3_frame,
535 map.data, 4 + head->side_info);
536 gst_buffer_unmap (head->buffer, &map);
540 av = gst_byte_writer_get_remaining (rtpmpadepay->mp3_frame);
541 GST_LOG_OBJECT (rtpmpadepay, "current mp3 frame remaining: %d", av);
542 GST_LOG_OBJECT (rtpmpadepay, "accumulated ADU frame data_size: %d",
545 if (rtpmpadepay->offset) {
546 gst_buffer_map (buf, &map, GST_MAP_READ);
547 /* no need to position, simply append */
548 g_assert (map.size > rtpmpadepay->offset);
549 av = MIN (av, map.size - rtpmpadepay->offset);
550 GST_LOG_OBJECT (rtpmpadepay,
551 "appending %d bytes from ADU frame at offset %d", av,
552 rtpmpadepay->offset);
553 gst_byte_writer_put_data_unchecked (rtpmpadepay->mp3_frame,
554 map.data + rtpmpadepay->offset, av);
555 rtpmpadepay->offset += av;
556 gst_buffer_unmap (buf, &map);
560 /* position writing according to ADU frame backpointer */
561 pos = gst_byte_writer_get_pos (rtpmpadepay->mp3_frame);
562 tpos = rtpmpadepay->size - frame->backpointer + 4 + head->side_info;
563 GST_LOG_OBJECT (rtpmpadepay, "current MP3 frame at position %d, "
564 "starting new ADU frame data at offset %d", pos, tpos);
568 /* try to insert as few frames as possible,
569 * so go for a reasonably large dummy frame size */
570 GST_LOG_OBJECT (rtpmpadepay,
571 "overlapping previous data; inserting dummy frame");
573 gst_rtp_mpa_robust_depay_generate_dummy_frame (rtpmpadepay, frame);
574 g_queue_insert_before (rtpmpadepay->adu_frames,
575 rtpmpadepay->cur_adu_frame, dummy);
576 /* offset is known to be zero, so we can shift current one */
577 rtpmpadepay->cur_adu_frame = rtpmpadepay->cur_adu_frame->prev;
578 if (!rtpmpadepay->size) {
579 g_assert (rtpmpadepay->cur_adu_frame ==
580 rtpmpadepay->adu_frames->head);
581 GST_LOG_OBJECT (rtpmpadepay, "... which is new head frame");
582 gst_byte_writer_free (rtpmpadepay->mp3_frame);
583 rtpmpadepay->mp3_frame = NULL;
585 /* ... and continue adding that empty one immediately,
586 * and then see if that provided enough extra space */
588 } else if (tpos >= pos + av) {
589 /* ADU frame no longer needs current MP3 frame; move to its end */
590 GST_LOG_OBJECT (rtpmpadepay, "passed current MP3 frame");
591 gst_byte_writer_set_pos (rtpmpadepay->mp3_frame, pos + av);
593 /* position and append */
594 gst_buffer_map (buf, &map, GST_MAP_READ);
595 GST_LOG_OBJECT (rtpmpadepay, "adding to current MP3 frame");
596 gst_byte_writer_set_pos (rtpmpadepay->mp3_frame, tpos);
598 g_assert (map.size >= 4 + frame->side_info);
599 av = MIN (av, map.size - 4 - frame->side_info);
600 gst_byte_writer_put_data_unchecked (rtpmpadepay->mp3_frame,
601 map.data + 4 + frame->side_info, av);
602 rtpmpadepay->offset += av + 4 + frame->side_info;
603 gst_buffer_unmap (buf, &map);
607 /* if mp3 frame filled, send on its way */
608 if (gst_byte_writer_get_remaining (rtpmpadepay->mp3_frame) == 0) {
610 buf = gst_byte_writer_free_and_get_buffer (rtpmpadepay->mp3_frame);
611 rtpmpadepay->mp3_frame = NULL;
612 GST_BUFFER_TIMESTAMP (buf) = GST_BUFFER_TIMESTAMP (head->buffer);
613 /* no longer need head ADU frame header and side info */
614 /* NOTE maybe head == current, then size and offset go off a bit,
615 * but current gets reset to NULL, and then also offset and size */
616 rtpmpadepay->size -= head->data_size;
617 gst_rtp_mpa_robust_depay_dequeue_frame (rtpmpadepay);
619 ret = gst_rtp_base_depayload_push (GST_RTP_BASE_DEPAYLOAD (rtpmpadepay),
627 /* process ADU frame @buf through:
629 * - converting to MP3 frames
630 * Takes ownership of @buf.
633 gst_rtp_mpa_robust_depay_submit_adu (GstRtpMPARobustDepay * rtpmpadepay,
636 if (gst_rtp_mpa_robust_depay_deinterleave (rtpmpadepay, buf))
637 return gst_rtp_mpa_robust_depay_push_mp3_frames (rtpmpadepay);
643 gst_rtp_mpa_robust_depay_process (GstRTPBaseDepayload * depayload,
646 GstRtpMPARobustDepay *rtpmpadepay;
647 gint payload_len, offset;
649 gboolean cont, dtype;
651 GstClockTime timestamp;
652 GstRTPBuffer rtp = { NULL };
654 rtpmpadepay = GST_RTP_MPA_ROBUST_DEPAY (depayload);
656 timestamp = GST_BUFFER_TIMESTAMP (buf);
658 gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);
660 payload_len = gst_rtp_buffer_get_payload_len (&rtp);
661 if (payload_len <= 1)
664 payload = gst_rtp_buffer_get_payload (&rtp);
666 GST_LOG_OBJECT (rtpmpadepay, "payload_len: %d", payload_len);
668 /* strip off descriptor
671 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6
672 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
674 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
676 * C: if 1, data is continuation
677 * T: if 1, size is 14 bits, otherwise 6 bits
678 * ADU size: size of following packet (not including descriptor)
680 while (payload_len) {
681 if (G_LIKELY (rtpmpadepay->has_descriptor)) {
682 cont = ! !(payload[offset] & 0x80);
683 dtype = ! !(payload[offset] & 0x40);
685 size = (payload[offset] & 0x3f) << 8 | payload[offset + 1];
688 } else if (payload_len >= 2) {
689 size = (payload[offset] & 0x3f);
701 GST_LOG_OBJECT (rtpmpadepay, "offset %d has cont: %d, dtype: %d, size: %d",
702 offset, cont, dtype, size);
704 buf = gst_rtp_buffer_get_payload_subbuffer (&rtp, offset,
705 MIN (size, payload_len));
708 av = gst_adapter_available (rtpmpadepay->adapter);
709 if (G_UNLIKELY (!av)) {
710 GST_DEBUG_OBJECT (rtpmpadepay,
711 "discarding continuation fragment without prior fragment");
712 gst_buffer_unref (buf);
714 av += gst_buffer_get_size (buf);
715 gst_adapter_push (rtpmpadepay->adapter, buf);
717 timestamp = gst_adapter_prev_pts (rtpmpadepay->adapter, NULL);
718 buf = gst_adapter_take_buffer (rtpmpadepay->adapter, size);
719 GST_BUFFER_TIMESTAMP (buf) = timestamp;
720 gst_rtp_mpa_robust_depay_submit_adu (rtpmpadepay, buf);
721 } else if (av > size) {
722 GST_DEBUG_OBJECT (rtpmpadepay,
723 "assembled ADU size %d larger than expected %d; discarding",
725 gst_adapter_clear (rtpmpadepay->adapter);
730 /* not continuation, first fragment or whole ADU */
731 if (payload_len == size) {
733 GST_BUFFER_TIMESTAMP (buf) = timestamp;
734 gst_rtp_mpa_robust_depay_submit_adu (rtpmpadepay, buf);
735 } else if (payload_len < size) {
737 gst_adapter_push (rtpmpadepay->adapter, buf);
745 /* timestamp applies to first payload, no idea for subsequent ones */
746 timestamp = GST_CLOCK_TIME_NONE;
748 gst_rtp_buffer_unmap (&rtp);
755 GST_ELEMENT_WARNING (rtpmpadepay, STREAM, DECODE,
756 (NULL), ("Packet contains invalid data"));
757 gst_rtp_buffer_unmap (&rtp);
762 static GstStateChangeReturn
763 gst_rtp_mpa_robust_change_state (GstElement * element,
764 GstStateChange transition)
766 GstStateChangeReturn ret;
767 GstRtpMPARobustDepay *rtpmpadepay;
769 rtpmpadepay = GST_RTP_MPA_ROBUST_DEPAY (element);
771 switch (transition) {
772 case GST_STATE_CHANGE_READY_TO_PAUSED:
773 rtpmpadepay->last_ii = -1;
774 rtpmpadepay->last_icc = -1;
775 rtpmpadepay->size = 0;
776 rtpmpadepay->offset = 0;
781 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
782 if (ret != GST_STATE_CHANGE_SUCCESS)
785 switch (transition) {
786 case GST_STATE_CHANGE_PAUSED_TO_READY:
790 gst_adapter_clear (rtpmpadepay->adapter);
791 for (i = 0; i < G_N_ELEMENTS (rtpmpadepay->deinter); i++) {
792 gst_buffer_replace (&rtpmpadepay->deinter[i], NULL);
794 rtpmpadepay->cur_adu_frame = NULL;
795 g_queue_foreach (rtpmpadepay->adu_frames,
796 (GFunc) gst_rtp_mpa_robust_depay_free_frame, NULL);
797 g_queue_clear (rtpmpadepay->adu_frames);
798 if (rtpmpadepay->mp3_frame)
799 gst_byte_writer_free (rtpmpadepay->mp3_frame);
810 gst_rtp_mpa_robust_depay_plugin_init (GstPlugin * plugin)
812 return gst_element_register (plugin, "rtpmparobustdepay",
813 GST_RANK_SECONDARY, GST_TYPE_RTP_MPA_ROBUST_DEPAY);