Merge remote-tracking branch 'origin/master' into 0.11
[platform/upstream/gst-plugins-good.git] / gst / rtp / gstrtpmparobustdepay.c
1 /* GStreamer
2  * Copyright (C) <2010> Mark Nauwelaerts <mark.nauwelaerts@collabora.co.uk>
3  * Copyright (C) <2010> Nokia Corporation
4  *
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.
9  *
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.
14  *
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., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #  include "config.h"
23 #endif
24
25 #include <gst/rtp/gstrtpbuffer.h>
26
27 #include <stdio.h>
28 #include <string.h>
29 #include "gstrtpmparobustdepay.h"
30
31 GST_DEBUG_CATEGORY_STATIC (rtpmparobustdepay_debug);
32 #define GST_CAT_DEFAULT (rtpmparobustdepay_debug)
33
34 static GstStaticPadTemplate gst_rtp_mpa_robust_depay_src_template =
35 GST_STATIC_PAD_TEMPLATE ("src",
36     GST_PAD_SRC,
37     GST_PAD_ALWAYS,
38     GST_STATIC_CAPS ("audio/mpeg, " "mpegversion = (int) 1")
39     );
40
41 static GstStaticPadTemplate gst_rtp_mpa_robust_depay_sink_template =
42     GST_STATIC_PAD_TEMPLATE ("sink",
43     GST_PAD_SINK,
44     GST_PAD_ALWAYS,
45     GST_STATIC_CAPS ("application/x-rtp, "
46         "media = (string) \"audio\", "
47         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
48         "clock-rate = (int) 90000, "
49         "encoding-name = (string) \"MPA-ROBUST\" " "; "
50         /* draft versions appear still in use out there */
51         "application/x-rtp, "
52         "media = (string) \"audio\", "
53         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
54         "clock-rate = (int) [1, MAX], "
55         "encoding-name = (string) { \"X-MP3-DRAFT-00\", \"X-MP3-DRAFT-01\", "
56         " \"X-MP3-DRAFT-02\", \"X-MP3-DRAFT-03\", \"X-MP3-DRAFT-04\", "
57         " \"X-MP3-DRAFT-05\", \"X-MP3-DRAFT-06\" }")
58     );
59
60 typedef struct _GstADUFrame
61 {
62   guint32 header;
63   gint size;
64   gint side_info;
65   gint data_size;
66   gint layer;
67   gint backpointer;
68
69   GstBuffer *buffer;
70 } GstADUFrame;
71
72 #define gst_rtp_mpa_robust_depay_parent_class parent_class
73 G_DEFINE_TYPE (GstRtpMPARobustDepay, gst_rtp_mpa_robust_depay,
74     GST_TYPE_RTP_BASE_DEPAYLOAD);
75
76 static GstStateChangeReturn gst_rtp_mpa_robust_change_state (GstElement *
77     element, GstStateChange transition);
78
79 static gboolean gst_rtp_mpa_robust_depay_setcaps (GstRTPBaseDepayload *
80     depayload, GstCaps * caps);
81 static GstBuffer *gst_rtp_mpa_robust_depay_process (GstRTPBaseDepayload *
82     depayload, GstBuffer * buf);
83
84 static void
85 gst_rtp_mpa_robust_depay_finalize (GObject * object)
86 {
87   GstRtpMPARobustDepay *rtpmpadepay;
88
89   rtpmpadepay = (GstRtpMPARobustDepay *) object;
90
91   g_object_unref (rtpmpadepay->adapter);
92   g_queue_free (rtpmpadepay->adu_frames);
93
94   G_OBJECT_CLASS (parent_class)->finalize (object);
95 }
96
97 static void
98 gst_rtp_mpa_robust_depay_class_init (GstRtpMPARobustDepayClass * klass)
99 {
100   GObjectClass *gobject_class;
101   GstElementClass *gstelement_class;
102   GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
103
104   GST_DEBUG_CATEGORY_INIT (rtpmparobustdepay_debug, "rtpmparobustdepay", 0,
105       "Robust MPEG Audio RTP Depayloader");
106
107   gobject_class = (GObjectClass *) klass;
108   gstelement_class = (GstElementClass *) klass;
109   gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
110
111   gobject_class->finalize = gst_rtp_mpa_robust_depay_finalize;
112
113   gstelement_class->change_state =
114       GST_DEBUG_FUNCPTR (gst_rtp_mpa_robust_change_state);
115
116   gst_element_class_add_pad_template (gstelement_class,
117       gst_static_pad_template_get (&gst_rtp_mpa_robust_depay_src_template));
118   gst_element_class_add_pad_template (gstelement_class,
119       gst_static_pad_template_get (&gst_rtp_mpa_robust_depay_sink_template));
120
121   gst_element_class_set_details_simple (gstelement_class,
122       "RTP MPEG audio depayloader", "Codec/Depayloader/Network/RTP",
123       "Extracts MPEG audio from RTP packets (RFC 5219)",
124       "Mark Nauwelaerts <mark.nauwelaerts@collabora.co.uk>");
125
126   gstrtpbasedepayload_class->set_caps = gst_rtp_mpa_robust_depay_setcaps;
127   gstrtpbasedepayload_class->process = gst_rtp_mpa_robust_depay_process;
128 }
129
130 static void
131 gst_rtp_mpa_robust_depay_init (GstRtpMPARobustDepay * rtpmpadepay)
132 {
133   rtpmpadepay->adapter = gst_adapter_new ();
134   rtpmpadepay->adu_frames = g_queue_new ();
135 }
136
137 static gboolean
138 gst_rtp_mpa_robust_depay_setcaps (GstRTPBaseDepayload * depayload,
139     GstCaps * caps)
140 {
141   GstRtpMPARobustDepay *rtpmpadepay;
142   GstStructure *structure;
143   GstCaps *outcaps;
144   gint clock_rate, draft;
145   gboolean res;
146   const gchar *encoding;
147
148   rtpmpadepay = GST_RTP_MPA_ROBUST_DEPAY (depayload);
149
150   structure = gst_caps_get_structure (caps, 0);
151
152   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
153     clock_rate = 90000;
154   depayload->clock_rate = clock_rate;
155
156   rtpmpadepay->has_descriptor = TRUE;
157   if ((encoding = gst_structure_get_string (structure, "encoding-name"))) {
158     if (sscanf (encoding, "X-MP3-DRAFT-%d", &draft) && (draft == 0))
159       rtpmpadepay->has_descriptor = FALSE;
160   }
161
162   outcaps =
163       gst_caps_new_simple ("audio/mpeg", "mpegversion", G_TYPE_INT, 1, NULL);
164   res = gst_pad_set_caps (depayload->srcpad, outcaps);
165   gst_caps_unref (outcaps);
166
167   return res;
168 }
169
170 /* thanks again go to mp3parse ... */
171
172 static const guint mp3types_bitrates[2][3][16] = {
173   {
174         {0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448,},
175         {0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384,},
176         {0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320,}
177       },
178   {
179         {0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256,},
180         {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160,},
181         {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160,}
182       },
183 };
184
185 static const guint mp3types_freqs[3][3] = { {44100, 48000, 32000},
186 {22050, 24000, 16000},
187 {11025, 12000, 8000}
188 };
189
190 static inline guint
191 mp3_type_frame_length_from_header (GstElement * mp3parse, guint32 header,
192     guint * put_version, guint * put_layer, guint * put_channels,
193     guint * put_bitrate, guint * put_samplerate, guint * put_mode,
194     guint * put_crc)
195 {
196   guint length;
197   gulong mode, samplerate, bitrate, layer, channels, padding, crc;
198   gulong version;
199   gint lsf, mpg25;
200
201   if (header & (1 << 20)) {
202     lsf = (header & (1 << 19)) ? 0 : 1;
203     mpg25 = 0;
204   } else {
205     lsf = 1;
206     mpg25 = 1;
207   }
208
209   version = 1 + lsf + mpg25;
210
211   layer = 4 - ((header >> 17) & 0x3);
212
213   crc = (header >> 16) & 0x1;
214
215   bitrate = (header >> 12) & 0xF;
216   bitrate = mp3types_bitrates[lsf][layer - 1][bitrate] * 1000;
217   /* The caller has ensured we have a valid header, so bitrate can't be
218      zero here. */
219   if (bitrate == 0) {
220     GST_DEBUG_OBJECT (mp3parse, "invalid bitrate");
221     return 0;
222   }
223
224   samplerate = (header >> 10) & 0x3;
225   samplerate = mp3types_freqs[lsf + mpg25][samplerate];
226
227   padding = (header >> 9) & 0x1;
228
229   mode = (header >> 6) & 0x3;
230   channels = (mode == 3) ? 1 : 2;
231
232   switch (layer) {
233     case 1:
234       length = 4 * ((bitrate * 12) / samplerate + padding);
235       break;
236     case 2:
237       length = (bitrate * 144) / samplerate + padding;
238       break;
239     default:
240     case 3:
241       length = (bitrate * 144) / (samplerate << lsf) + padding;
242       break;
243   }
244
245   GST_LOG_OBJECT (mp3parse, "Calculated mp3 frame length of %u bytes", length);
246   GST_LOG_OBJECT (mp3parse, "samplerate = %lu, bitrate = %lu, version = %lu, "
247       "layer = %lu, channels = %lu, mode = %lu", samplerate, bitrate, version,
248       layer, channels, mode);
249
250   if (put_version)
251     *put_version = version;
252   if (put_layer)
253     *put_layer = layer;
254   if (put_channels)
255     *put_channels = channels;
256   if (put_bitrate)
257     *put_bitrate = bitrate;
258   if (put_samplerate)
259     *put_samplerate = samplerate;
260   if (put_mode)
261     *put_mode = mode;
262   if (put_crc)
263     *put_crc = crc;
264
265   GST_LOG_OBJECT (mp3parse, "size = %u", length);
266   return length;
267 }
268
269 /* generate empty/silent/dummy frame that mimics @frame,
270  * except for rate, where maximum possible is selected */
271 static GstADUFrame *
272 gst_rtp_mpa_robust_depay_generate_dummy_frame (GstRtpMPARobustDepay *
273     rtpmpadepay, GstADUFrame * frame)
274 {
275   GstADUFrame *dummy;
276   GstMapInfo map;
277
278   dummy = g_slice_dup (GstADUFrame, frame);
279
280   /* go for maximum bitrate */
281   dummy->header = (frame->header & ~(0xf << 12)) | (0xe << 12);
282   dummy->size =
283       mp3_type_frame_length_from_header (GST_ELEMENT_CAST (rtpmpadepay),
284       dummy->header, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
285   dummy->data_size = dummy->size - dummy->side_info;
286   dummy->backpointer = 0;
287
288   dummy->buffer = gst_buffer_new_and_alloc (dummy->side_info + 4);
289
290   gst_buffer_map (dummy->buffer, &map, GST_MAP_WRITE);
291   memset (map.data, 0, map.size);
292   GST_WRITE_UINT32_BE (map.data, dummy->header);
293   gst_buffer_unmap (dummy->buffer, &map);
294
295   GST_BUFFER_TIMESTAMP (dummy->buffer) = GST_BUFFER_TIMESTAMP (frame->buffer);
296
297   return dummy;
298 }
299
300 /* validates and parses @buf, and queues for further transformation if valid,
301  * otherwise discards @buf
302  * Takes ownership of @buf. */
303 static gboolean
304 gst_rtp_mpa_robust_depay_queue_frame (GstRtpMPARobustDepay * rtpmpadepay,
305     GstBuffer * buf)
306 {
307   GstADUFrame *frame = NULL;
308   guint version, layer, channels, size;
309   guint crc;
310   GstMapInfo map;
311
312   g_return_val_if_fail (buf != NULL, FALSE);
313
314   gst_buffer_map (buf, &map, GST_MAP_READ);
315
316   if (map.size < 6)
317     goto corrupt_frame;
318
319   frame = g_slice_new0 (GstADUFrame);
320   frame->header = GST_READ_UINT32_BE (map.data);
321
322   size = mp3_type_frame_length_from_header (GST_ELEMENT_CAST (rtpmpadepay),
323       frame->header, &version, &layer, &channels, NULL, NULL, NULL, &crc);
324   if (!size)
325     goto corrupt_frame;
326
327   frame->size = size;
328   frame->layer = layer;
329   if (version == 1 && channels == 2)
330     frame->side_info = 32;
331   else if ((version == 1 && channels == 1) || (version >= 2 && channels == 2))
332     frame->side_info = 17;
333   else if (version >= 2 && channels == 1)
334     frame->side_info = 9;
335   else {
336     g_assert_not_reached ();
337     goto corrupt_frame;
338   }
339
340   /* backpointer */
341   if (layer == 3) {
342     frame->backpointer = GST_READ_UINT16_BE (map.data + 4);
343     frame->backpointer >>= 7;
344     GST_LOG_OBJECT (rtpmpadepay, "backpointer: %d", frame->backpointer);
345   }
346
347   if (!crc)
348     frame->side_info += 2;
349
350   GST_LOG_OBJECT (rtpmpadepay, "side info: %d", frame->side_info);
351   frame->data_size = frame->size - 4 - frame->side_info;
352
353   /* some size validation checks */
354   if (4 + frame->side_info > map.size)
355     goto corrupt_frame;
356
357   /* ADU data would then extend past MP3 frame,
358    * even using past byte reservoir */
359   if (-frame->backpointer + (gint) (map.size) > frame->size)
360     goto corrupt_frame;
361
362   gst_buffer_unmap (buf, &map);
363
364   /* ok, take buffer and queue */
365   frame->buffer = buf;
366   g_queue_push_tail (rtpmpadepay->adu_frames, frame);
367
368   return TRUE;
369
370   /* ERRORS */
371 corrupt_frame:
372   {
373     GST_DEBUG_OBJECT (rtpmpadepay, "frame is corrupt");
374     gst_buffer_unmap (buf, &map);
375     gst_buffer_unref (buf);
376     if (frame)
377       g_slice_free (GstADUFrame, frame);
378     return FALSE;
379   }
380 }
381
382 static inline void
383 gst_rtp_mpa_robust_depay_free_frame (GstADUFrame * frame)
384 {
385   if (frame->buffer)
386     gst_buffer_unref (frame->buffer);
387   g_slice_free (GstADUFrame, frame);
388 }
389
390 static inline void
391 gst_rtp_mpa_robust_depay_dequeue_frame (GstRtpMPARobustDepay * rtpmpadepay)
392 {
393   GstADUFrame *head;
394
395   GST_LOG_OBJECT (rtpmpadepay, "dequeueing ADU frame");
396
397   if (rtpmpadepay->adu_frames->head == rtpmpadepay->cur_adu_frame)
398     rtpmpadepay->cur_adu_frame = NULL;
399
400   head = g_queue_pop_head (rtpmpadepay->adu_frames);
401   g_assert (head->buffer);
402   gst_rtp_mpa_robust_depay_free_frame (head);
403
404   return;
405 }
406
407 /* returns TRUE if at least one new ADU frame was enqueued for MP3 conversion.
408  * Takes ownership of @buf. */
409 static gboolean
410 gst_rtp_mpa_robust_depay_deinterleave (GstRtpMPARobustDepay * rtpmpadepay,
411     GstBuffer * buf)
412 {
413   gboolean ret = FALSE;
414   GstMapInfo map;
415   guint val, iindex, icc;
416
417   gst_buffer_map (buf, &map, GST_MAP_READ);
418   val = GST_READ_UINT16_BE (map.data) >> 5;
419   gst_buffer_unmap (buf, &map);
420
421   iindex = val >> 3;
422   icc = val & 0x7;
423
424   GST_LOG_OBJECT (rtpmpadepay, "sync: 0x%x, index: %u, cycle count: %u",
425       val, iindex, icc);
426
427   /* basic case; no interleaving ever seen */
428   if (val == 0x7ff && rtpmpadepay->last_icc < 0) {
429     ret = gst_rtp_mpa_robust_depay_queue_frame (rtpmpadepay, buf);
430   } else {
431     if (G_UNLIKELY (rtpmpadepay->last_icc < 0)) {
432       rtpmpadepay->last_icc = icc;
433       rtpmpadepay->last_ii = iindex;
434     }
435     if (icc != rtpmpadepay->last_icc || iindex == rtpmpadepay->last_ii) {
436       gint i;
437
438       for (i = 0; i < 256; ++i) {
439         if (rtpmpadepay->deinter[i] != NULL) {
440           ret |= gst_rtp_mpa_robust_depay_queue_frame (rtpmpadepay,
441               rtpmpadepay->deinter[i]);
442           rtpmpadepay->deinter[i] = NULL;
443         }
444       }
445     }
446     /* rewrite buffer sync header */
447     val = GST_READ_UINT16_BE (buf);
448     val = (0x7ff << 5) | val;
449     GST_WRITE_UINT16_BE (buf, val);
450     /* store and keep track of last indices */
451     rtpmpadepay->last_icc = icc;
452     rtpmpadepay->last_ii = iindex;
453     rtpmpadepay->deinter[iindex] = buf;
454   }
455
456   return ret;
457 }
458
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 */
467 static GstFlowReturn
468 gst_rtp_mpa_robust_depay_push_mp3_frames (GstRtpMPARobustDepay * rtpmpadepay)
469 {
470   GstBuffer *buf;
471   GstADUFrame *frame, *head;
472   gint av;
473   GstFlowReturn ret = GST_FLOW_OK;
474
475   while (1) {
476     GstMapInfo map;
477
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;
482     }
483
484     if (G_UNLIKELY (!rtpmpadepay->cur_adu_frame))
485       break;
486
487     frame = (GstADUFrame *) rtpmpadepay->cur_adu_frame->data;
488     head = (GstADUFrame *) rtpmpadepay->adu_frames->head->data;
489
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",
493           frame->layer);
494       gst_rtp_base_depayload_push (GST_RTP_BASE_DEPAYLOAD (rtpmpadepay),
495           frame->buffer);
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;
501       continue;
502     }
503
504     if (rtpmpadepay->offset == gst_buffer_get_size (frame->buffer)) {
505       if (g_list_next (rtpmpadepay->cur_adu_frame)) {
506         GST_LOG_OBJECT (rtpmpadepay,
507             "moving to next ADU frame, size %d, side_info %d",
508             frame->size, frame->side_info);
509         rtpmpadepay->size += frame->data_size;
510         rtpmpadepay->cur_adu_frame = g_list_next (rtpmpadepay->cur_adu_frame);
511         frame = (GstADUFrame *) rtpmpadepay->cur_adu_frame->data;
512         rtpmpadepay->offset = 0;
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))
516           goto flush;
517       } else {
518         break;
519       }
520     }
521
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 (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       gst_buffer_map (head->buffer, &map, GST_MAP_READ);
533       gst_byte_writer_put_data (rtpmpadepay->mp3_frame,
534           map.data, 4 + head->side_info);
535       gst_buffer_unmap (head->buffer, &map);
536     }
537
538     buf = frame->buffer;
539     av = gst_byte_writer_get_remaining (rtpmpadepay->mp3_frame);
540     GST_LOG_OBJECT (rtpmpadepay, "current mp3 frame remaining: %d", av);
541     GST_LOG_OBJECT (rtpmpadepay, "accumulated ADU frame data_size: %d",
542         rtpmpadepay->size);
543
544     if (rtpmpadepay->offset) {
545       gst_buffer_map (buf, &map, GST_MAP_READ);
546       /* no need to position, simply append */
547       g_assert (map.size > rtpmpadepay->offset);
548       av = MIN (av, map.size - rtpmpadepay->offset);
549       GST_LOG_OBJECT (rtpmpadepay,
550           "appending %d bytes from ADU frame at offset %d", av,
551           rtpmpadepay->offset);
552       gst_byte_writer_put_data (rtpmpadepay->mp3_frame,
553           map.data + rtpmpadepay->offset, av);
554       rtpmpadepay->offset += av;
555       gst_buffer_unmap (buf, &map);
556     } else {
557       gint pos, tpos;
558
559       /* position writing according to ADU frame backpointer */
560       pos = gst_byte_writer_get_pos (rtpmpadepay->mp3_frame);
561       tpos = rtpmpadepay->size - frame->backpointer + 4 + head->side_info;
562       GST_LOG_OBJECT (rtpmpadepay, "current MP3 frame at position %d, "
563           "starting new ADU frame data at offset %d", pos, tpos);
564       if (tpos < pos) {
565         GstADUFrame *dummy;
566
567         /* try to insert as few frames as possible,
568          * so go for a reasonably large dummy frame size */
569         GST_LOG_OBJECT (rtpmpadepay,
570             "overlapping previous data; inserting dummy frame");
571         dummy =
572             gst_rtp_mpa_robust_depay_generate_dummy_frame (rtpmpadepay, frame);
573         g_queue_insert_before (rtpmpadepay->adu_frames,
574             rtpmpadepay->cur_adu_frame, dummy);
575         /* offset is known to be zero, so we can shift current one */
576         rtpmpadepay->cur_adu_frame = rtpmpadepay->cur_adu_frame->prev;
577         if (!rtpmpadepay->size) {
578           g_assert (rtpmpadepay->cur_adu_frame ==
579               rtpmpadepay->adu_frames->head);
580           GST_LOG_OBJECT (rtpmpadepay, "... which is new head frame");
581           gst_byte_writer_free (rtpmpadepay->mp3_frame);
582           rtpmpadepay->mp3_frame = NULL;
583         }
584         /* ... and continue adding that empty one immediately,
585          * and then see if that provided enough extra space */
586         continue;
587       } else if (tpos >= pos + av) {
588         /* ADU frame no longer needs current MP3 frame; move to its end */
589         GST_LOG_OBJECT (rtpmpadepay, "passed current MP3 frame");
590         gst_byte_writer_set_pos (rtpmpadepay->mp3_frame, pos + av);
591       } else {
592         /* position and append */
593         gst_buffer_map (buf, &map, GST_MAP_READ);
594         GST_LOG_OBJECT (rtpmpadepay, "adding to current MP3 frame");
595         gst_byte_writer_set_pos (rtpmpadepay->mp3_frame, tpos);
596         av = MIN (av, map.size - 4 - frame->side_info);
597         gst_byte_writer_put_data (rtpmpadepay->mp3_frame,
598             map.data + 4 + frame->side_info, av);
599         rtpmpadepay->offset += av + 4 + frame->side_info;
600         gst_buffer_unmap (buf, &map);
601       }
602     }
603
604     /* if mp3 frame filled, send on its way */
605     if (gst_byte_writer_get_remaining (rtpmpadepay->mp3_frame) == 0) {
606     flush:
607       buf = gst_byte_writer_free_and_get_buffer (rtpmpadepay->mp3_frame);
608       rtpmpadepay->mp3_frame = NULL;
609       GST_BUFFER_TIMESTAMP (buf) = GST_BUFFER_TIMESTAMP (head->buffer);
610       /* no longer need head ADU frame header and side info */
611       /* NOTE maybe head == current, then size and offset go off a bit,
612        * but current gets reset to NULL, and then also offset and size */
613       rtpmpadepay->size -= head->data_size;
614       gst_rtp_mpa_robust_depay_dequeue_frame (rtpmpadepay);
615       /* send */
616       ret = gst_rtp_base_depayload_push (GST_RTP_BASE_DEPAYLOAD (rtpmpadepay),
617           buf);
618     }
619   }
620
621   return ret;
622 }
623
624 /* process ADU frame @buf through:
625  * - deinterleaving
626  * - converting to MP3 frames
627  * Takes ownership of @buf.
628  */
629 static GstFlowReturn
630 gst_rtp_mpa_robust_depay_submit_adu (GstRtpMPARobustDepay * rtpmpadepay,
631     GstBuffer * buf)
632 {
633   if (gst_rtp_mpa_robust_depay_deinterleave (rtpmpadepay, buf))
634     return gst_rtp_mpa_robust_depay_push_mp3_frames (rtpmpadepay);
635
636   return GST_FLOW_OK;
637 }
638
639 static GstBuffer *
640 gst_rtp_mpa_robust_depay_process (GstRTPBaseDepayload * depayload,
641     GstBuffer * buf)
642 {
643   GstRtpMPARobustDepay *rtpmpadepay;
644   gint payload_len, offset;
645   guint8 *payload;
646   gboolean cont, dtype;
647   guint av, size;
648   GstClockTime timestamp;
649   GstRTPBuffer rtp = { NULL };
650
651   rtpmpadepay = GST_RTP_MPA_ROBUST_DEPAY (depayload);
652
653   timestamp = GST_BUFFER_TIMESTAMP (buf);
654
655   gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);
656
657   payload_len = gst_rtp_buffer_get_payload_len (&rtp);
658   if (payload_len <= 1)
659     goto short_read;
660
661   payload = gst_rtp_buffer_get_payload (&rtp);
662   offset = 0;
663   GST_LOG_OBJECT (rtpmpadepay, "payload_len: %d", payload_len);
664
665   /* strip off descriptor
666    *
667    *  0                   1
668    *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6
669    * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
670    * |C|T|            ADU size         |
671    * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
672    *
673    * C: if 1, data is continuation
674    * T: if 1, size is 14 bits, otherwise 6 bits
675    * ADU size: size of following packet (not including descriptor)
676    */
677   while (payload_len) {
678     if (G_LIKELY (rtpmpadepay->has_descriptor)) {
679       cont = ! !(payload[offset] & 0x80);
680       dtype = ! !(payload[offset] & 0x40);
681       if (dtype) {
682         size = (payload[offset] & 0x3f) << 8 | payload[offset + 1];
683         payload_len--;
684         offset++;
685       } else if (payload_len >= 2) {
686         size = (payload[offset] & 0x3f);
687         payload_len -= 2;
688         offset += 2;
689       } else {
690         goto short_read;
691       }
692     } else {
693       cont = FALSE;
694       dtype = -1;
695       size = payload_len;
696     }
697
698     GST_LOG_OBJECT (rtpmpadepay, "offset %d has cont: %d, dtype: %d, size: %d",
699         offset, cont, dtype, size);
700
701     buf = gst_rtp_buffer_get_payload_subbuffer (&rtp, offset,
702         MIN (size, payload_len));
703
704     if (cont) {
705       av = gst_adapter_available (rtpmpadepay->adapter);
706       if (G_UNLIKELY (!av)) {
707         GST_DEBUG_OBJECT (rtpmpadepay,
708             "discarding continuation fragment without prior fragment");
709         gst_buffer_unref (buf);
710       } else {
711         av += gst_buffer_get_size (buf);
712         gst_adapter_push (rtpmpadepay->adapter, buf);
713         if (av == size) {
714           timestamp = gst_adapter_prev_timestamp (rtpmpadepay->adapter, NULL);
715           buf = gst_adapter_take_buffer (rtpmpadepay->adapter, size);
716           GST_BUFFER_TIMESTAMP (buf) = timestamp;
717           gst_rtp_mpa_robust_depay_submit_adu (rtpmpadepay, buf);
718         } else if (av > size) {
719           GST_DEBUG_OBJECT (rtpmpadepay,
720               "assembled ADU size %d larger than expected %d; discarding",
721               av, size);
722           gst_adapter_clear (rtpmpadepay->adapter);
723         }
724       }
725       size = payload_len;
726     } else {
727       /* not continuation, first fragment or whole ADU */
728       if (payload_len == size) {
729         /* whole ADU */
730         GST_BUFFER_TIMESTAMP (buf) = timestamp;
731         gst_rtp_mpa_robust_depay_submit_adu (rtpmpadepay, buf);
732       } else if (payload_len < size) {
733         /* first fragment */
734         gst_adapter_push (rtpmpadepay->adapter, buf);
735         size = payload_len;
736       }
737     }
738
739     offset += size;
740     payload_len -= size;
741
742     /* timestamp applies to first payload, no idea for subsequent ones */
743     timestamp = GST_CLOCK_TIME_NONE;
744   }
745   gst_rtp_buffer_unmap (&rtp);
746
747   return NULL;
748
749   /* ERRORS */
750 short_read:
751   {
752     GST_ELEMENT_WARNING (rtpmpadepay, STREAM, DECODE,
753         (NULL), ("Packet contains invalid data"));
754     gst_rtp_buffer_unmap (&rtp);
755     return NULL;
756   }
757 }
758
759 static GstStateChangeReturn
760 gst_rtp_mpa_robust_change_state (GstElement * element,
761     GstStateChange transition)
762 {
763   GstStateChangeReturn ret;
764   GstRtpMPARobustDepay *rtpmpadepay;
765
766   rtpmpadepay = GST_RTP_MPA_ROBUST_DEPAY (element);
767
768   switch (transition) {
769     case GST_STATE_CHANGE_READY_TO_PAUSED:
770       rtpmpadepay->last_ii = -1;
771       rtpmpadepay->last_icc = -1;
772       rtpmpadepay->size = 0;
773       rtpmpadepay->offset = 0;
774     default:
775       break;
776   }
777
778   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
779   if (ret != GST_STATE_CHANGE_SUCCESS)
780     return ret;
781
782   switch (transition) {
783     case GST_STATE_CHANGE_PAUSED_TO_READY:
784     {
785       gint i;
786
787       gst_adapter_clear (rtpmpadepay->adapter);
788       for (i = 0; i < G_N_ELEMENTS (rtpmpadepay->deinter); i++) {
789         gst_buffer_replace (&rtpmpadepay->deinter[i], NULL);
790       }
791       rtpmpadepay->cur_adu_frame = NULL;
792       g_queue_foreach (rtpmpadepay->adu_frames,
793           (GFunc) gst_rtp_mpa_robust_depay_free_frame, NULL);
794       g_queue_clear (rtpmpadepay->adu_frames);
795       break;
796     }
797     default:
798       break;
799   }
800
801   return ret;
802 }
803
804 gboolean
805 gst_rtp_mpa_robust_depay_plugin_init (GstPlugin * plugin)
806 {
807   return gst_element_register (plugin, "rtpmparobustdepay",
808       GST_RANK_SECONDARY, GST_TYPE_RTP_MPA_ROBUST_DEPAY);
809 }