mp4gdepay: improve constantDuration guessing
[platform/upstream/gst-plugins-good.git] / gst / rtp / gstrtpmp4gdepay.c
1 /* GStreamer
2  * Copyright (C) <2005> Wim Taymans <wim.taymans@gmail.com>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <string.h>
25 #include <stdlib.h>
26 #include <gst/rtp/gstrtpbuffer.h>
27
28 #include "gstrtpmp4gdepay.h"
29
30 GST_DEBUG_CATEGORY_STATIC (rtpmp4gdepay_debug);
31 #define GST_CAT_DEFAULT (rtpmp4gdepay_debug)
32
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>");
39
40 static GstStaticPadTemplate gst_rtp_mp4g_depay_src_template =
41     GST_STATIC_PAD_TEMPLATE ("src",
42     GST_PAD_SRC,
43     GST_PAD_ALWAYS,
44     GST_STATIC_CAPS ("video/mpeg,"
45         "mpegversion=(int) 4,"
46         "systemstream=(boolean)false;"
47         "audio/mpeg," "mpegversion=(int) 4, " "stream-format=(string)raw")
48     );
49
50 static GstStaticPadTemplate gst_rtp_mp4g_depay_sink_template =
51 GST_STATIC_PAD_TEMPLATE ("sink",
52     GST_PAD_SINK,
53     GST_PAD_ALWAYS,
54     GST_STATIC_CAPS ("application/x-rtp, "
55         "media = (string) { \"video\", \"audio\", \"application\" }, "
56         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
57         "clock-rate = (int) [1, MAX ], "
58         "encoding-name = (string) \"MPEG4-GENERIC\", "
59         /* required string params */
60         "streamtype = (string) { \"4\", \"5\" }, "      /* 4 = video, 5 = audio */
61         /* "profile-level-id = (string) [1,MAX], " */
62         /* "config = (string) [1,MAX]" */
63         "mode = (string) { \"generic\", \"CELP-cbr\", \"CELP-vbr\", \"AAC-lbr\", \"AAC-hbr\" } "
64         /* Optional general parameters */
65         /* "objecttype = (string) [1,MAX], " */
66         /* "constantsize = (string) [1,MAX], " *//* constant size of each AU */
67         /* "constantduration = (string) [1,MAX], " *//* constant duration of each AU */
68         /* "maxdisplacement = (string) [1,MAX], " */
69         /* "de-interleavebuffersize = (string) [1,MAX], " */
70         /* Optional configuration parameters */
71         /* "sizelength = (string) [1, 32], " */
72         /* "indexlength = (string) [1, 32], " */
73         /* "indexdeltalength = (string) [1, 32], " */
74         /* "ctsdeltalength = (string) [1, 32], " */
75         /* "dtsdeltalength = (string) [1, 32], " */
76         /* "randomaccessindication = (string) {0, 1}, " */
77         /* "streamstateindication = (string) [0, 32], " */
78         /* "auxiliarydatasizelength = (string) [0, 32]" */ )
79     );
80
81 /* simple bitstream parser */
82 typedef struct
83 {
84   const guint8 *data;
85   const guint8 *end;
86   gint head;                    /* bitpos in the cache of next bit */
87   guint64 cache;                /* cached bytes */
88 } GstBsParse;
89
90 static void
91 gst_bs_parse_init (GstBsParse * bs, const guint8 * data, guint size)
92 {
93   bs->data = data;
94   bs->end = data + size;
95   bs->head = 0;
96   bs->cache = 0xffffffff;
97 }
98
99 static guint32
100 gst_bs_parse_read (GstBsParse * bs, guint n)
101 {
102   guint32 res = 0;
103   gint shift;
104
105   if (n == 0)
106     return res;
107
108   /* fill up the cache if we need to */
109   while (bs->head < n) {
110     if (bs->data >= bs->end) {
111       /* we're at the end, can't produce more than head number of bits */
112       n = bs->head;
113       break;
114     }
115     /* shift bytes in cache, moving the head bits of the cache left */
116     bs->cache = (bs->cache << 8) | *bs->data++;
117     bs->head += 8;
118   }
119
120   /* bring the required bits down and truncate */
121   if ((shift = bs->head - n) > 0)
122     res = bs->cache >> shift;
123   else
124     res = bs->cache;
125
126   /* mask out required bits */
127   if (n < 32)
128     res &= (1 << n) - 1;
129
130   bs->head = shift;
131
132   return res;
133 }
134
135
136 GST_BOILERPLATE (GstRtpMP4GDepay, gst_rtp_mp4g_depay, GstBaseRTPDepayload,
137     GST_TYPE_BASE_RTP_DEPAYLOAD);
138
139 static void gst_rtp_mp4g_depay_finalize (GObject * object);
140
141 static gboolean gst_rtp_mp4g_depay_setcaps (GstBaseRTPDepayload * depayload,
142     GstCaps * caps);
143 static GstBuffer *gst_rtp_mp4g_depay_process (GstBaseRTPDepayload * depayload,
144     GstBuffer * buf);
145
146 static GstStateChangeReturn gst_rtp_mp4g_depay_change_state (GstElement *
147     element, GstStateChange transition);
148
149
150 static void
151 gst_rtp_mp4g_depay_base_init (gpointer klass)
152 {
153   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
154
155   gst_element_class_add_pad_template (element_class,
156       gst_static_pad_template_get (&gst_rtp_mp4g_depay_src_template));
157   gst_element_class_add_pad_template (element_class,
158       gst_static_pad_template_get (&gst_rtp_mp4g_depay_sink_template));
159
160   gst_element_class_set_details (element_class, &gst_rtp_mp4gdepay_details);
161 }
162
163 static void
164 gst_rtp_mp4g_depay_class_init (GstRtpMP4GDepayClass * klass)
165 {
166   GObjectClass *gobject_class;
167   GstElementClass *gstelement_class;
168   GstBaseRTPDepayloadClass *gstbasertpdepayload_class;
169
170   gobject_class = (GObjectClass *) klass;
171   gstelement_class = (GstElementClass *) klass;
172   gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;
173
174   gobject_class->finalize = gst_rtp_mp4g_depay_finalize;
175
176   gstelement_class->change_state = gst_rtp_mp4g_depay_change_state;
177
178   gstbasertpdepayload_class->process = gst_rtp_mp4g_depay_process;
179   gstbasertpdepayload_class->set_caps = gst_rtp_mp4g_depay_setcaps;
180
181   GST_DEBUG_CATEGORY_INIT (rtpmp4gdepay_debug, "rtpmp4gdepay", 0,
182       "MP4-generic RTP Depayloader");
183 }
184
185 static void
186 gst_rtp_mp4g_depay_init (GstRtpMP4GDepay * rtpmp4gdepay,
187     GstRtpMP4GDepayClass * klass)
188 {
189   rtpmp4gdepay->adapter = gst_adapter_new ();
190   rtpmp4gdepay->packets = g_queue_new ();
191 }
192
193 static void
194 gst_rtp_mp4g_depay_finalize (GObject * object)
195 {
196   GstRtpMP4GDepay *rtpmp4gdepay;
197
198   rtpmp4gdepay = GST_RTP_MP4G_DEPAY (object);
199
200   g_object_unref (rtpmp4gdepay->adapter);
201   rtpmp4gdepay->adapter = NULL;
202   g_queue_free (rtpmp4gdepay->packets);
203   rtpmp4gdepay->packets = NULL;
204
205   G_OBJECT_CLASS (parent_class)->finalize (object);
206 }
207
208 static gint
209 gst_rtp_mp4g_depay_parse_int (GstStructure * structure, const gchar * field,
210     gint def)
211 {
212   const gchar *str;
213   gint res;
214
215   if ((str = gst_structure_get_string (structure, field)))
216     return atoi (str);
217
218   if (gst_structure_get_int (structure, field, &res))
219     return res;
220
221   return def;
222 }
223
224 static gboolean
225 gst_rtp_mp4g_depay_setcaps (GstBaseRTPDepayload * depayload, GstCaps * caps)
226 {
227   GstStructure *structure;
228   GstRtpMP4GDepay *rtpmp4gdepay;
229   GstCaps *srccaps = NULL;
230   const gchar *str;
231   gint clock_rate;
232   gint someint;
233   gboolean res;
234
235   rtpmp4gdepay = GST_RTP_MP4G_DEPAY (depayload);
236
237   structure = gst_caps_get_structure (caps, 0);
238
239   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
240     clock_rate = 90000;         /* default */
241   depayload->clock_rate = clock_rate;
242
243   if ((str = gst_structure_get_string (structure, "media"))) {
244     if (strcmp (str, "audio") == 0) {
245       srccaps = gst_caps_new_simple ("audio/mpeg",
246           "mpegversion", G_TYPE_INT, 4, "stream-format", G_TYPE_STRING, "raw",
247           NULL);
248     } else if (strcmp (str, "video") == 0) {
249       srccaps = gst_caps_new_simple ("video/mpeg",
250           "mpegversion", G_TYPE_INT, 4,
251           "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
252     }
253   }
254   if (srccaps == NULL)
255     goto unknown_media;
256
257   /* these values are optional and have a default value of 0 (no header) */
258   rtpmp4gdepay->sizelength =
259       gst_rtp_mp4g_depay_parse_int (structure, "sizelength", 0);
260   rtpmp4gdepay->indexlength =
261       gst_rtp_mp4g_depay_parse_int (structure, "indexlength", 0);
262   rtpmp4gdepay->indexdeltalength =
263       gst_rtp_mp4g_depay_parse_int (structure, "indexdeltalength", 0);
264   rtpmp4gdepay->ctsdeltalength =
265       gst_rtp_mp4g_depay_parse_int (structure, "ctsdeltalength", 0);
266   rtpmp4gdepay->dtsdeltalength =
267       gst_rtp_mp4g_depay_parse_int (structure, "dtsdeltalength", 0);
268   someint =
269       gst_rtp_mp4g_depay_parse_int (structure, "randomaccessindication", 0);
270   rtpmp4gdepay->randomaccessindication = someint > 0 ? 1 : 0;
271   rtpmp4gdepay->streamstateindication =
272       gst_rtp_mp4g_depay_parse_int (structure, "streamstateindication", 0);
273   rtpmp4gdepay->auxiliarydatasizelength =
274       gst_rtp_mp4g_depay_parse_int (structure, "auxiliarydatasizelength", 0);
275   rtpmp4gdepay->constantSize =
276       gst_rtp_mp4g_depay_parse_int (structure, "constantsize", 0);
277   rtpmp4gdepay->constantDuration =
278       gst_rtp_mp4g_depay_parse_int (structure, "constantduration", 0);
279   rtpmp4gdepay->maxDisplacement =
280       gst_rtp_mp4g_depay_parse_int (structure, "maxdisplacement", 0);
281
282
283   /* get config string */
284   if ((str = gst_structure_get_string (structure, "config"))) {
285     GValue v = { 0 };
286
287     g_value_init (&v, GST_TYPE_BUFFER);
288     if (gst_value_deserialize (&v, str)) {
289       GstBuffer *buffer;
290
291       buffer = gst_value_get_buffer (&v);
292       gst_caps_set_simple (srccaps,
293           "codec_data", GST_TYPE_BUFFER, buffer, NULL);
294       g_value_unset (&v);
295     } else {
296       g_warning ("cannot convert config to buffer");
297     }
298   }
299
300   res = gst_pad_set_caps (depayload->srcpad, srccaps);
301   gst_caps_unref (srccaps);
302
303   return res;
304
305   /* ERRORS */
306 unknown_media:
307   {
308     GST_DEBUG_OBJECT (rtpmp4gdepay, "Unknown media type");
309     return FALSE;
310   }
311 }
312
313 static void
314 gst_rtp_mp4g_depay_clear_queue (GstRtpMP4GDepay * rtpmp4gdepay)
315 {
316   GstBuffer *outbuf;
317
318   while ((outbuf = g_queue_pop_head (rtpmp4gdepay->packets)))
319     gst_buffer_unref (outbuf);
320 }
321
322 static void
323 gst_rtp_mp4g_depay_flush_queue (GstRtpMP4GDepay * rtpmp4gdepay)
324 {
325   GstBuffer *outbuf;
326   gboolean discont = FALSE;
327   guint AU_index;
328
329   while ((outbuf = g_queue_pop_head (rtpmp4gdepay->packets))) {
330     AU_index = GST_BUFFER_OFFSET (outbuf);
331
332     GST_DEBUG_OBJECT (rtpmp4gdepay, "next available AU_index %u", AU_index);
333
334     if (rtpmp4gdepay->next_AU_index != AU_index) {
335       GST_DEBUG_OBJECT (rtpmp4gdepay, "discont, expected AU_index %u",
336           rtpmp4gdepay->next_AU_index);
337       discont = TRUE;
338     }
339
340     if (discont) {
341       GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
342       discont = FALSE;
343     }
344
345     GST_DEBUG_OBJECT (rtpmp4gdepay, "pushing AU_index %u", AU_index);
346     gst_base_rtp_depayload_push (GST_BASE_RTP_DEPAYLOAD (rtpmp4gdepay), outbuf);
347     rtpmp4gdepay->next_AU_index = AU_index + 1;
348   }
349 }
350
351 static void
352 gst_rtp_mp4g_depay_queue (GstRtpMP4GDepay * rtpmp4gdepay, GstBuffer * outbuf)
353 {
354   guint AU_index = GST_BUFFER_OFFSET (outbuf);
355
356   if (rtpmp4gdepay->next_AU_index == -1) {
357     GST_DEBUG_OBJECT (rtpmp4gdepay, "Init AU counter %u", AU_index);
358     rtpmp4gdepay->next_AU_index = AU_index;
359   }
360
361   if (rtpmp4gdepay->next_AU_index == AU_index) {
362     GST_DEBUG_OBJECT (rtpmp4gdepay, "pushing expected AU_index %u", AU_index);
363
364     /* we received the expected packet, push it and flush as much as we can from
365      * the queue */
366     gst_base_rtp_depayload_push (GST_BASE_RTP_DEPAYLOAD (rtpmp4gdepay), outbuf);
367     rtpmp4gdepay->next_AU_index++;
368
369     while ((outbuf = g_queue_peek_head (rtpmp4gdepay->packets))) {
370       AU_index = GST_BUFFER_OFFSET (outbuf);
371
372       GST_DEBUG_OBJECT (rtpmp4gdepay, "next available AU_index %u", AU_index);
373
374       if (rtpmp4gdepay->next_AU_index == AU_index) {
375         GST_DEBUG_OBJECT (rtpmp4gdepay, "pushing expected AU_index %u",
376             AU_index);
377         outbuf = g_queue_pop_head (rtpmp4gdepay->packets);
378         gst_base_rtp_depayload_push (GST_BASE_RTP_DEPAYLOAD (rtpmp4gdepay),
379             outbuf);
380         rtpmp4gdepay->next_AU_index++;
381       } else {
382         GST_DEBUG_OBJECT (rtpmp4gdepay, "waiting for next AU_index %u",
383             rtpmp4gdepay->next_AU_index);
384         break;
385       }
386     }
387   } else {
388     GList *list;
389
390     GST_DEBUG_OBJECT (rtpmp4gdepay, "queueing AU_index %u", AU_index);
391
392     /* loop the list to skip strictly smaller AU_index buffers */
393     for (list = rtpmp4gdepay->packets->head; list; list = g_list_next (list)) {
394       guint idx;
395       gint gap;
396
397       idx = GST_BUFFER_OFFSET (GST_BUFFER_CAST (list->data));
398
399       /* compare the new seqnum to the one in the buffer */
400       gap = (gint) (idx - AU_index);
401
402       GST_DEBUG_OBJECT (rtpmp4gdepay, "compare with AU_index %u, gap %d", idx,
403           gap);
404
405       /* AU_index <= idx, we can stop looking */
406       if (G_LIKELY (gap > 0))
407         break;
408     }
409     if (G_LIKELY (list))
410       g_queue_insert_before (rtpmp4gdepay->packets, list, outbuf);
411     else
412       g_queue_push_tail (rtpmp4gdepay->packets, outbuf);
413   }
414 }
415
416 static GstBuffer *
417 gst_rtp_mp4g_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
418 {
419   GstRtpMP4GDepay *rtpmp4gdepay;
420   GstBuffer *outbuf;
421   GstClockTime timestamp;
422
423   rtpmp4gdepay = GST_RTP_MP4G_DEPAY (depayload);
424
425   /* flush remaining data on discont */
426   if (GST_BUFFER_IS_DISCONT (buf)) {
427     GST_DEBUG_OBJECT (rtpmp4gdepay, "received DISCONT");
428     gst_adapter_clear (rtpmp4gdepay->adapter);
429   }
430
431   timestamp = GST_BUFFER_TIMESTAMP (buf);
432
433   {
434     gint payload_len, payload_AU;
435     guint8 *payload;
436     guint32 rtptime;
437     guint AU_headers_len;
438     guint AU_size, AU_index, AU_index_delta, payload_AU_size;
439     gboolean M;
440
441     payload_len = gst_rtp_buffer_get_payload_len (buf);
442     payload = gst_rtp_buffer_get_payload (buf);
443
444     GST_DEBUG_OBJECT (rtpmp4gdepay, "received payload of %d", payload_len);
445
446     rtptime = gst_rtp_buffer_get_timestamp (buf);
447     M = gst_rtp_buffer_get_marker (buf);
448
449     if (rtpmp4gdepay->sizelength > 0) {
450       gint num_AU_headers, AU_headers_bytes, i;
451       GstBsParse bs;
452
453       if (payload_len < 2)
454         goto short_payload;
455
456       /* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- .. -+-+-+-+-+-+-+-+-+-+
457        * |AU-headers-length|AU-header|AU-header|      |AU-header|padding|
458        * |                 |   (1)   |   (2)   |      |   (n) * | bits  |
459        * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- .. -+-+-+-+-+-+-+-+-+-+
460        *
461        * The length is 2 bytes and contains the length of the following
462        * AU-headers in bits.
463        */
464       AU_headers_len = (payload[0] << 8) | payload[1];
465       AU_headers_bytes = (AU_headers_len + 7) / 8;
466       num_AU_headers = AU_headers_len / 16;
467
468       GST_DEBUG_OBJECT (rtpmp4gdepay, "AU headers len %d, bytes %d, num %d",
469           AU_headers_len, AU_headers_bytes, num_AU_headers);
470
471       /* skip header */
472       payload += 2;
473       payload_len -= 2;
474
475       if (payload_len < AU_headers_bytes)
476         goto short_payload;
477
478       /* skip special headers, point to first payload AU */
479       payload_AU = 2 + AU_headers_bytes;
480       payload_AU_size = payload_len - AU_headers_bytes;
481
482       if (G_UNLIKELY (rtpmp4gdepay->auxiliarydatasizelength)) {
483         gint aux_size;
484
485         /* point the bitstream parser to the first auxiliary data bit */
486         gst_bs_parse_init (&bs, payload + AU_headers_bytes,
487             payload_len - AU_headers_bytes);
488         aux_size =
489             gst_bs_parse_read (&bs, rtpmp4gdepay->auxiliarydatasizelength);
490         /* convert to bytes */
491         aux_size = (aux_size + 7) / 8;
492         /* AU data then follows auxiliary data */
493         if (payload_AU_size < aux_size)
494           goto short_payload;
495         payload_AU += aux_size;
496         payload_AU_size -= aux_size;
497       }
498
499       /* point the bitstream parser to the first AU header bit */
500       gst_bs_parse_init (&bs, payload, payload_len);
501       AU_index = AU_index_delta = 0;
502
503       for (i = 0; i < num_AU_headers && payload_AU_size > 0; i++) {
504         /* parse AU header
505          *  +---------------------------------------+
506          *  |     AU-size                           |
507          *  +---------------------------------------+
508          *  |     AU-Index / AU-Index-delta         |
509          *  +---------------------------------------+
510          *  |     CTS-flag                          |
511          *  +---------------------------------------+
512          *  |     CTS-delta                         |
513          *  +---------------------------------------+
514          *  |     DTS-flag                          |
515          *  +---------------------------------------+
516          *  |     DTS-delta                         |
517          *  +---------------------------------------+
518          *  |     RAP-flag                          |
519          *  +---------------------------------------+
520          *  |     Stream-state                      |
521          *  +---------------------------------------+
522          */
523         AU_size = gst_bs_parse_read (&bs, rtpmp4gdepay->sizelength);
524
525         /* calculate the AU_index, which is only on the first AU of the packet
526          * and the AU_index_delta on the other AUs. This will be used to
527          * reconstruct the AU ordering when interleaving. */
528         if (i == 0) {
529           AU_index = gst_bs_parse_read (&bs, rtpmp4gdepay->indexlength);
530
531           GST_DEBUG_OBJECT (rtpmp4gdepay, "AU index %u", AU_index);
532
533           if (AU_index == 0 && rtpmp4gdepay->prev_AU_index == 0) {
534             gint diff;
535             gint cd;
536
537             /* if we see two consecutive packets with AU_index of 0, we can
538              * assume we have constantDuration packets. Since we don't have
539              * the index we must use the AU duration to calculate the
540              * index. Get the diff between the timestamps first, this can be
541              * positive or negative. */
542             if (rtpmp4gdepay->prev_rtptime <= rtptime)
543               diff = rtptime - rtpmp4gdepay->prev_rtptime;
544             else
545               diff = -(rtpmp4gdepay->prev_rtptime - rtptime);
546
547             /* if no constantDuration was given, make one */
548             if (rtpmp4gdepay->constantDuration != 0) {
549               cd = rtpmp4gdepay->constantDuration;
550               GST_DEBUG_OBJECT (depayload, "using constantDuration %d", cd);
551             } else {
552               cd = diff / num_AU_headers;
553               GST_DEBUG_OBJECT (depayload, "guessing constantDuration %d", cd);
554             }
555
556             if (cd > 0) {
557               /* get the number of packets by dividing with the duration */
558               diff /= cd;
559             } else {
560               diff = 0;
561             }
562
563             rtpmp4gdepay->last_AU_index += diff;
564             rtpmp4gdepay->prev_AU_index = AU_index;
565
566             AU_index = rtpmp4gdepay->last_AU_index;
567
568             GST_DEBUG_OBJECT (rtpmp4gdepay, "diff %d, AU index %u", diff,
569                 AU_index);
570           } else {
571             rtpmp4gdepay->prev_AU_index = AU_index;
572             rtpmp4gdepay->last_AU_index = AU_index;
573           }
574
575           /* keep track of the higest AU_index */
576           if (rtpmp4gdepay->max_AU_index != -1
577               && rtpmp4gdepay->max_AU_index <= AU_index) {
578             GST_DEBUG_OBJECT (rtpmp4gdepay, "new interleave group, flushing");
579             /* a new interleave group started, flush */
580             gst_rtp_mp4g_depay_flush_queue (rtpmp4gdepay);
581           }
582           if (G_UNLIKELY (!rtpmp4gdepay->maxDisplacement &&
583                   rtpmp4gdepay->max_AU_index != -1
584                   && rtpmp4gdepay->max_AU_index >= AU_index)) {
585             GstBuffer *outbuf;
586
587             /* some broken non-interleaved streams have AU-index jumping around
588              * all over the place, apparently assuming receiver disregards */
589             GST_DEBUG_OBJECT (rtpmp4gdepay, "non-interleaved broken AU indices;"
590                 " forcing continuous flush");
591             /* reset AU to avoid repeated DISCONT in such case */
592             outbuf = g_queue_peek_head (rtpmp4gdepay->packets);
593             if (G_LIKELY (outbuf)) {
594               rtpmp4gdepay->next_AU_index = GST_BUFFER_OFFSET (outbuf);
595               gst_rtp_mp4g_depay_flush_queue (rtpmp4gdepay);
596             }
597           }
598           rtpmp4gdepay->prev_rtptime = rtptime;
599         } else {
600           AU_index_delta =
601               gst_bs_parse_read (&bs, rtpmp4gdepay->indexdeltalength);
602           AU_index += AU_index_delta + 1;
603         }
604         /* keep track of highest AU_index */
605         if (rtpmp4gdepay->max_AU_index == -1
606             || AU_index > rtpmp4gdepay->max_AU_index)
607           rtpmp4gdepay->max_AU_index = AU_index;
608
609         /* the presentation time offset, a 2s-complement value, we need this to
610          * calculate the timestamp on the output packet. */
611         if (rtpmp4gdepay->ctsdeltalength > 0) {
612           if (gst_bs_parse_read (&bs, 1))
613             gst_bs_parse_read (&bs, rtpmp4gdepay->ctsdeltalength);
614         }
615         /* the decoding time offset, a 2s-complement value */
616         if (rtpmp4gdepay->dtsdeltalength > 0) {
617           if (gst_bs_parse_read (&bs, 1))
618             gst_bs_parse_read (&bs, rtpmp4gdepay->dtsdeltalength);
619         }
620         /* RAP-flag to indicate that the AU contains a keyframe */
621         if (rtpmp4gdepay->randomaccessindication)
622           gst_bs_parse_read (&bs, 1);
623         /* stream-state */
624         if (rtpmp4gdepay->streamstateindication > 0)
625           gst_bs_parse_read (&bs, rtpmp4gdepay->streamstateindication);
626
627         GST_DEBUG_OBJECT (rtpmp4gdepay, "size %d, index %d, delta %d", AU_size,
628             AU_index, AU_index_delta);
629
630         /* fragmented pakets have the AU_size set to the size of the
631          * unfragmented AU. */
632         if (AU_size > payload_AU_size)
633           AU_size = payload_AU_size;
634
635         /* collect stuff in the adapter, strip header from payload and push in
636          * the adapter */
637         outbuf =
638             gst_rtp_buffer_get_payload_subbuffer (buf, payload_AU, AU_size);
639         gst_adapter_push (rtpmp4gdepay->adapter, outbuf);
640
641         if (M) {
642           guint avail;
643
644           /* packet is complete, flush */
645           avail = gst_adapter_available (rtpmp4gdepay->adapter);
646
647           outbuf = gst_adapter_take_buffer (rtpmp4gdepay->adapter, avail);
648           gst_buffer_set_caps (outbuf, GST_PAD_CAPS (depayload->srcpad));
649
650           /* copy some of the fields we calculated above on the buffer. We also
651            * copy the AU_index so that we can sort the packets in our queue. */
652           GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
653           GST_BUFFER_OFFSET (outbuf) = AU_index;
654
655           /* make sure we don't use the timestamp again for other AUs in this
656            * RTP packet. */
657           timestamp = -1;
658
659           GST_DEBUG_OBJECT (depayload, "pushing buffer of size %d",
660               GST_BUFFER_SIZE (outbuf));
661
662           gst_rtp_mp4g_depay_queue (rtpmp4gdepay, outbuf);
663
664         }
665         payload_AU += AU_size;
666         payload_AU_size -= AU_size;
667       }
668     } else {
669       /* push complete buffer in adapter */
670       outbuf = gst_rtp_buffer_get_payload_subbuffer (buf, 0, payload_len);
671       gst_adapter_push (rtpmp4gdepay->adapter, outbuf);
672
673       /* if this was the last packet of the VOP, create and push a buffer */
674       if (M) {
675         guint avail;
676
677         avail = gst_adapter_available (rtpmp4gdepay->adapter);
678
679         outbuf = gst_adapter_take_buffer (rtpmp4gdepay->adapter, avail);
680
681         GST_DEBUG ("gst_rtp_mp4g_depay_chain: pushing buffer of size %d",
682             GST_BUFFER_SIZE (outbuf));
683
684         return outbuf;
685       }
686     }
687   }
688   return NULL;
689
690   /* ERRORS */
691 short_payload:
692   {
693     GST_ELEMENT_WARNING (rtpmp4gdepay, STREAM, DECODE,
694         ("Packet payload was too short."), (NULL));
695     return NULL;
696   }
697 }
698
699 static GstStateChangeReturn
700 gst_rtp_mp4g_depay_change_state (GstElement * element,
701     GstStateChange transition)
702 {
703   GstRtpMP4GDepay *rtpmp4gdepay;
704   GstStateChangeReturn ret;
705
706   rtpmp4gdepay = GST_RTP_MP4G_DEPAY (element);
707
708   switch (transition) {
709     case GST_STATE_CHANGE_READY_TO_PAUSED:
710       gst_adapter_clear (rtpmp4gdepay->adapter);
711       rtpmp4gdepay->max_AU_index = -1;
712       rtpmp4gdepay->next_AU_index = -1;
713       rtpmp4gdepay->prev_AU_index = -1;
714       rtpmp4gdepay->prev_rtptime = -1;
715       rtpmp4gdepay->last_AU_index = -1;
716       break;
717     default:
718       break;
719   }
720
721   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
722
723   switch (transition) {
724     case GST_STATE_CHANGE_PAUSED_TO_READY:
725       gst_adapter_clear (rtpmp4gdepay->adapter);
726       gst_rtp_mp4g_depay_clear_queue (rtpmp4gdepay);
727       break;
728     default:
729       break;
730   }
731   return ret;
732 }
733
734 gboolean
735 gst_rtp_mp4g_depay_plugin_init (GstPlugin * plugin)
736 {
737   return gst_element_register (plugin, "rtpmp4gdepay",
738       GST_RANK_MARGINAL, GST_TYPE_RTP_MP4G_DEPAY);
739 }