5d9d2cdf2d218067fed508f5a61005d9aa70c84a
[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., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, 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 #include "gstrtputils.h"
30
31 GST_DEBUG_CATEGORY_STATIC (rtpmp4gdepay_debug);
32 #define GST_CAT_DEFAULT (rtpmp4gdepay_debug)
33
34 static GstStaticPadTemplate gst_rtp_mp4g_depay_src_template =
35     GST_STATIC_PAD_TEMPLATE ("src",
36     GST_PAD_SRC,
37     GST_PAD_ALWAYS,
38     GST_STATIC_CAPS ("video/mpeg,"
39         "mpegversion=(int) 4,"
40         "systemstream=(boolean)false;"
41         "audio/mpeg," "mpegversion=(int) 4, " "stream-format=(string)raw")
42     );
43
44 static GstStaticPadTemplate gst_rtp_mp4g_depay_sink_template =
45 GST_STATIC_PAD_TEMPLATE ("sink",
46     GST_PAD_SINK,
47     GST_PAD_ALWAYS,
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\" } "
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]" */ )
72     );
73
74 /* simple bitstream parser */
75 typedef struct
76 {
77   const guint8 *data;
78   const guint8 *end;
79   gint head;                    /* bitpos in the cache of next bit */
80   guint64 cache;                /* cached bytes */
81 } GstBsParse;
82
83 static void
84 gst_bs_parse_init (GstBsParse * bs, const guint8 * data, guint size)
85 {
86   bs->data = data;
87   bs->end = data + size;
88   bs->head = 0;
89   bs->cache = 0xffffffff;
90 }
91
92 static guint32
93 gst_bs_parse_read (GstBsParse * bs, guint n)
94 {
95   guint32 res = 0;
96   gint shift;
97
98   if (n == 0)
99     return res;
100
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 */
105       n = bs->head;
106       break;
107     }
108     /* shift bytes in cache, moving the head bits of the cache left */
109     bs->cache = (bs->cache << 8) | *bs->data++;
110     bs->head += 8;
111   }
112
113   /* bring the required bits down and truncate */
114   if ((shift = bs->head - n) > 0)
115     res = bs->cache >> shift;
116   else
117     res = bs->cache;
118
119   /* mask out required bits */
120   if (n < 32)
121     res &= (1 << n) - 1;
122
123   bs->head = shift;
124
125   return res;
126 }
127
128
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);
132
133 static void gst_rtp_mp4g_depay_finalize (GObject * object);
134
135 static gboolean gst_rtp_mp4g_depay_setcaps (GstRTPBaseDepayload * depayload,
136     GstCaps * caps);
137 static GstBuffer *gst_rtp_mp4g_depay_process (GstRTPBaseDepayload * depayload,
138     GstRTPBuffer * rtp);
139 static gboolean gst_rtp_mp4g_depay_handle_event (GstRTPBaseDepayload * filter,
140     GstEvent * event);
141
142 static GstStateChangeReturn gst_rtp_mp4g_depay_change_state (GstElement *
143     element, GstStateChange transition);
144
145
146 static void
147 gst_rtp_mp4g_depay_class_init (GstRtpMP4GDepayClass * klass)
148 {
149   GObjectClass *gobject_class;
150   GstElementClass *gstelement_class;
151   GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
152
153   gobject_class = (GObjectClass *) klass;
154   gstelement_class = (GstElementClass *) klass;
155   gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
156
157   gobject_class->finalize = gst_rtp_mp4g_depay_finalize;
158
159   gstelement_class->change_state = gst_rtp_mp4g_depay_change_state;
160
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;
164
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);
169
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>");
174
175   GST_DEBUG_CATEGORY_INIT (rtpmp4gdepay_debug, "rtpmp4gdepay", 0,
176       "MP4-generic RTP Depayloader");
177 }
178
179 static void
180 gst_rtp_mp4g_depay_init (GstRtpMP4GDepay * rtpmp4gdepay)
181 {
182   rtpmp4gdepay->adapter = gst_adapter_new ();
183   rtpmp4gdepay->packets = g_queue_new ();
184 }
185
186 static void
187 gst_rtp_mp4g_depay_finalize (GObject * object)
188 {
189   GstRtpMP4GDepay *rtpmp4gdepay;
190
191   rtpmp4gdepay = GST_RTP_MP4G_DEPAY (object);
192
193   g_object_unref (rtpmp4gdepay->adapter);
194   rtpmp4gdepay->adapter = NULL;
195   g_queue_free (rtpmp4gdepay->packets);
196   rtpmp4gdepay->packets = NULL;
197
198   G_OBJECT_CLASS (parent_class)->finalize (object);
199 }
200
201 static gint
202 gst_rtp_mp4g_depay_parse_int (GstStructure * structure, const gchar * field,
203     gint def)
204 {
205   const gchar *str;
206   gint res;
207
208   if ((str = gst_structure_get_string (structure, field)))
209     return atoi (str);
210
211   if (gst_structure_get_int (structure, field, &res))
212     return res;
213
214   return def;
215 }
216
217 static gboolean
218 gst_rtp_mp4g_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
219 {
220   GstStructure *structure;
221   GstRtpMP4GDepay *rtpmp4gdepay;
222   GstCaps *srccaps = NULL;
223   const gchar *str;
224   gint clock_rate;
225   gint someint;
226   gboolean res;
227
228   rtpmp4gdepay = GST_RTP_MP4G_DEPAY (depayload);
229
230   structure = gst_caps_get_structure (caps, 0);
231
232   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
233     clock_rate = 90000;         /* default */
234   depayload->clock_rate = clock_rate;
235
236   rtpmp4gdepay->check_adts = FALSE;
237
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",
242           NULL);
243       rtpmp4gdepay->check_adts = TRUE;
244     } else if (strcmp (str, "video") == 0) {
245       srccaps = gst_caps_new_simple ("video/mpeg",
246           "mpegversion", G_TYPE_INT, 4,
247           "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
248     }
249   }
250   if (srccaps == NULL)
251     goto unknown_media;
252
253   /* these values are optional and have a default value of 0 (no header) */
254   rtpmp4gdepay->sizelength =
255       gst_rtp_mp4g_depay_parse_int (structure, "sizelength", 0);
256   rtpmp4gdepay->indexlength =
257       gst_rtp_mp4g_depay_parse_int (structure, "indexlength", 0);
258   rtpmp4gdepay->indexdeltalength =
259       gst_rtp_mp4g_depay_parse_int (structure, "indexdeltalength", 0);
260   rtpmp4gdepay->ctsdeltalength =
261       gst_rtp_mp4g_depay_parse_int (structure, "ctsdeltalength", 0);
262   rtpmp4gdepay->dtsdeltalength =
263       gst_rtp_mp4g_depay_parse_int (structure, "dtsdeltalength", 0);
264   someint =
265       gst_rtp_mp4g_depay_parse_int (structure, "randomaccessindication", 0);
266   rtpmp4gdepay->randomaccessindication = someint > 0 ? 1 : 0;
267   rtpmp4gdepay->streamstateindication =
268       gst_rtp_mp4g_depay_parse_int (structure, "streamstateindication", 0);
269   rtpmp4gdepay->auxiliarydatasizelength =
270       gst_rtp_mp4g_depay_parse_int (structure, "auxiliarydatasizelength", 0);
271   rtpmp4gdepay->constantSize =
272       gst_rtp_mp4g_depay_parse_int (structure, "constantsize", 0);
273   rtpmp4gdepay->constantDuration =
274       gst_rtp_mp4g_depay_parse_int (structure, "constantduration", 0);
275   rtpmp4gdepay->maxDisplacement =
276       gst_rtp_mp4g_depay_parse_int (structure, "maxdisplacement", 0);
277
278
279   /* get config string */
280   if ((str = gst_structure_get_string (structure, "config"))) {
281     GValue v = { 0 };
282
283     g_value_init (&v, GST_TYPE_BUFFER);
284     if (gst_value_deserialize (&v, str)) {
285       GstBuffer *buffer;
286
287       buffer = gst_value_get_buffer (&v);
288       gst_caps_set_simple (srccaps,
289           "codec_data", GST_TYPE_BUFFER, buffer, NULL);
290       g_value_unset (&v);
291     } else {
292       g_warning ("cannot convert config to buffer");
293     }
294   }
295
296   res = gst_pad_set_caps (depayload->srcpad, srccaps);
297   gst_caps_unref (srccaps);
298
299   return res;
300
301   /* ERRORS */
302 unknown_media:
303   {
304     GST_DEBUG_OBJECT (rtpmp4gdepay, "Unknown media type");
305     return FALSE;
306   }
307 }
308
309 static void
310 gst_rtp_mp4g_depay_clear_queue (GstRtpMP4GDepay * rtpmp4gdepay)
311 {
312   GstBuffer *outbuf;
313
314   while ((outbuf = g_queue_pop_head (rtpmp4gdepay->packets)))
315     gst_buffer_unref (outbuf);
316 }
317
318 static void
319 gst_rtp_mp4g_depay_reset (GstRtpMP4GDepay * rtpmp4gdepay)
320 {
321   gst_adapter_clear (rtpmp4gdepay->adapter);
322   rtpmp4gdepay->max_AU_index = -1;
323   rtpmp4gdepay->next_AU_index = -1;
324   rtpmp4gdepay->prev_AU_index = -1;
325   rtpmp4gdepay->prev_rtptime = -1;
326   rtpmp4gdepay->last_AU_index = -1;
327   gst_rtp_mp4g_depay_clear_queue (rtpmp4gdepay);
328 }
329
330 static void
331 gst_rtp_mp4g_depay_push_outbuf (GstRtpMP4GDepay * rtpmp4gdepay,
332     GstBuffer * outbuf, guint AU_index)
333 {
334   gboolean discont = FALSE;
335
336   if (AU_index != rtpmp4gdepay->next_AU_index) {
337     GST_DEBUG_OBJECT (rtpmp4gdepay, "discont, expected AU_index %u",
338         rtpmp4gdepay->next_AU_index);
339     GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
340     discont = TRUE;
341   }
342
343   GST_DEBUG_OBJECT (rtpmp4gdepay, "pushing %sAU_index %u",
344       discont ? "" : "expected ", AU_index);
345
346   gst_rtp_drop_meta (GST_ELEMENT_CAST (rtpmp4gdepay), outbuf, 0);
347   gst_rtp_base_depayload_push (GST_RTP_BASE_DEPAYLOAD (rtpmp4gdepay), outbuf);
348   rtpmp4gdepay->next_AU_index = AU_index + 1;
349 }
350
351 static void
352 gst_rtp_mp4g_depay_flush_queue (GstRtpMP4GDepay * rtpmp4gdepay)
353 {
354   GstBuffer *outbuf;
355   guint AU_index;
356
357   while ((outbuf = g_queue_pop_head (rtpmp4gdepay->packets))) {
358     AU_index = GST_BUFFER_OFFSET (outbuf);
359
360     GST_DEBUG_OBJECT (rtpmp4gdepay, "next available AU_index %u", AU_index);
361
362     gst_rtp_mp4g_depay_push_outbuf (rtpmp4gdepay, outbuf, AU_index);
363   }
364 }
365
366 static void
367 gst_rtp_mp4g_depay_queue (GstRtpMP4GDepay * rtpmp4gdepay, GstBuffer * outbuf)
368 {
369   guint AU_index = GST_BUFFER_OFFSET (outbuf);
370
371   if (rtpmp4gdepay->next_AU_index == -1) {
372     GST_DEBUG_OBJECT (rtpmp4gdepay, "Init AU counter %u", AU_index);
373     rtpmp4gdepay->next_AU_index = AU_index;
374   }
375
376   if (rtpmp4gdepay->next_AU_index == AU_index) {
377     GST_DEBUG_OBJECT (rtpmp4gdepay, "pushing expected AU_index %u", AU_index);
378
379     /* we received the expected packet, push it and flush as much as we can from
380      * the queue */
381     gst_rtp_mp4g_depay_push_outbuf (rtpmp4gdepay, outbuf, AU_index);
382
383     while ((outbuf = g_queue_peek_head (rtpmp4gdepay->packets))) {
384       AU_index = GST_BUFFER_OFFSET (outbuf);
385
386       GST_DEBUG_OBJECT (rtpmp4gdepay, "next available AU_index %u", AU_index);
387
388       if (rtpmp4gdepay->next_AU_index == AU_index) {
389         outbuf = g_queue_pop_head (rtpmp4gdepay->packets);
390         gst_rtp_mp4g_depay_push_outbuf (rtpmp4gdepay, outbuf, AU_index);
391       } else {
392         GST_DEBUG_OBJECT (rtpmp4gdepay, "waiting for next AU_index %u",
393             rtpmp4gdepay->next_AU_index);
394         break;
395       }
396     }
397   } else {
398     GList *list;
399
400     GST_DEBUG_OBJECT (rtpmp4gdepay, "queueing AU_index %u", AU_index);
401
402     /* loop the list to skip strictly smaller AU_index buffers */
403     for (list = rtpmp4gdepay->packets->head; list; list = g_list_next (list)) {
404       guint idx;
405       gint gap;
406
407       idx = GST_BUFFER_OFFSET (GST_BUFFER_CAST (list->data));
408
409       /* compare the new seqnum to the one in the buffer */
410       gap = (gint) (idx - AU_index);
411
412       GST_DEBUG_OBJECT (rtpmp4gdepay, "compare with AU_index %u, gap %d", idx,
413           gap);
414
415       /* AU_index <= idx, we can stop looking */
416       if (G_LIKELY (gap > 0))
417         break;
418     }
419     if (G_LIKELY (list))
420       g_queue_insert_before (rtpmp4gdepay->packets, list, outbuf);
421     else
422       g_queue_push_tail (rtpmp4gdepay->packets, outbuf);
423   }
424 }
425
426 static GstBuffer *
427 gst_rtp_mp4g_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
428 {
429   GstRtpMP4GDepay *rtpmp4gdepay;
430   GstBuffer *outbuf = NULL;
431   GstClockTime timestamp;
432
433   rtpmp4gdepay = GST_RTP_MP4G_DEPAY (depayload);
434
435   /* flush remaining data on discont */
436   if (GST_BUFFER_IS_DISCONT (rtp->buffer)) {
437     GST_DEBUG_OBJECT (rtpmp4gdepay, "received DISCONT");
438     gst_adapter_clear (rtpmp4gdepay->adapter);
439   }
440
441   timestamp = GST_BUFFER_PTS (rtp->buffer);
442
443   {
444     gint payload_len, payload_AU;
445     guint8 *payload;
446     guint32 rtptime;
447     guint AU_headers_len;
448     guint AU_size, AU_index, AU_index_delta, payload_AU_size;
449     gboolean M;
450
451     payload_len = gst_rtp_buffer_get_payload_len (rtp);
452     payload = gst_rtp_buffer_get_payload (rtp);
453
454     GST_DEBUG_OBJECT (rtpmp4gdepay, "received payload of %d", payload_len);
455
456     rtptime = gst_rtp_buffer_get_timestamp (rtp);
457     M = gst_rtp_buffer_get_marker (rtp);
458
459     if (rtpmp4gdepay->sizelength > 0) {
460       gint num_AU_headers, AU_headers_bytes, i;
461       GstBsParse bs;
462
463       if (payload_len < 2)
464         goto short_payload;
465
466       /* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- .. -+-+-+-+-+-+-+-+-+-+
467        * |AU-headers-length|AU-header|AU-header|      |AU-header|padding|
468        * |                 |   (1)   |   (2)   |      |   (n) * | bits  |
469        * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- .. -+-+-+-+-+-+-+-+-+-+
470        *
471        * The length is 2 bytes and contains the length of the following
472        * AU-headers in bits.
473        */
474       AU_headers_len = (payload[0] << 8) | payload[1];
475       AU_headers_bytes = (AU_headers_len + 7) / 8;
476       num_AU_headers = AU_headers_len / 16;
477
478       GST_DEBUG_OBJECT (rtpmp4gdepay, "AU headers len %d, bytes %d, num %d",
479           AU_headers_len, AU_headers_bytes, num_AU_headers);
480
481       /* skip header */
482       payload += 2;
483       payload_len -= 2;
484
485       if (payload_len < AU_headers_bytes)
486         goto short_payload;
487
488       /* skip special headers, point to first payload AU */
489       payload_AU = 2 + AU_headers_bytes;
490       payload_AU_size = payload_len - AU_headers_bytes;
491
492       if (G_UNLIKELY (rtpmp4gdepay->auxiliarydatasizelength)) {
493         gint aux_size;
494
495         /* point the bitstream parser to the first auxiliary data bit */
496         gst_bs_parse_init (&bs, payload + AU_headers_bytes,
497             payload_len - AU_headers_bytes);
498         aux_size =
499             gst_bs_parse_read (&bs, rtpmp4gdepay->auxiliarydatasizelength);
500         /* convert to bytes */
501         aux_size = (aux_size + 7) / 8;
502         /* AU data then follows auxiliary data */
503         if (payload_AU_size < aux_size)
504           goto short_payload;
505         payload_AU += aux_size;
506         payload_AU_size -= aux_size;
507       }
508
509       /* point the bitstream parser to the first AU header bit */
510       gst_bs_parse_init (&bs, payload, payload_len);
511       AU_index = AU_index_delta = 0;
512
513       for (i = 0; i < num_AU_headers && payload_AU_size > 0; i++) {
514         /* parse AU header
515          *  +---------------------------------------+
516          *  |     AU-size                           |
517          *  +---------------------------------------+
518          *  |     AU-Index / AU-Index-delta         |
519          *  +---------------------------------------+
520          *  |     CTS-flag                          |
521          *  +---------------------------------------+
522          *  |     CTS-delta                         |
523          *  +---------------------------------------+
524          *  |     DTS-flag                          |
525          *  +---------------------------------------+
526          *  |     DTS-delta                         |
527          *  +---------------------------------------+
528          *  |     RAP-flag                          |
529          *  +---------------------------------------+
530          *  |     Stream-state                      |
531          *  +---------------------------------------+
532          */
533         AU_size = gst_bs_parse_read (&bs, rtpmp4gdepay->sizelength);
534
535         /* calculate the AU_index, which is only on the first AU of the packet
536          * and the AU_index_delta on the other AUs. This will be used to
537          * reconstruct the AU ordering when interleaving. */
538         if (i == 0) {
539           AU_index = gst_bs_parse_read (&bs, rtpmp4gdepay->indexlength);
540
541           GST_DEBUG_OBJECT (rtpmp4gdepay, "AU index %u", AU_index);
542
543           if (AU_index == 0 && rtpmp4gdepay->prev_AU_index == 0) {
544             gint diff;
545             gint cd;
546
547             /* if we see two consecutive packets with AU_index of 0, we can
548              * assume we have constantDuration packets. Since we don't have
549              * the index we must use the AU duration to calculate the
550              * index. Get the diff between the timestamps first, this can be
551              * positive or negative. */
552             if (rtpmp4gdepay->prev_rtptime <= rtptime)
553               diff = rtptime - rtpmp4gdepay->prev_rtptime;
554             else
555               diff = -(rtpmp4gdepay->prev_rtptime - rtptime);
556
557             /* if no constantDuration was given, make one */
558             if (rtpmp4gdepay->constantDuration != 0) {
559               cd = rtpmp4gdepay->constantDuration;
560               GST_DEBUG_OBJECT (depayload, "using constantDuration %d", cd);
561             } else if (rtpmp4gdepay->prev_AU_num > 0) {
562               /* use number of packets and of previous frame */
563               cd = diff / rtpmp4gdepay->prev_AU_num;
564               GST_DEBUG_OBJECT (depayload, "guessing constantDuration %d", cd);
565               if (!GST_BUFFER_IS_DISCONT (rtp->buffer)) {
566                 /* rfc3640 - 3.2.3.2
567                  * if we see two consecutive packets with AU_index of 0 and
568                  * there has been no discontinuity, we must conclude that this
569                  * value of constantDuration is correct from now on. */
570                 GST_DEBUG_OBJECT (depayload,
571                     "constantDuration of %d detected", cd);
572                 rtpmp4gdepay->constantDuration = cd;
573               }
574             } else {
575               /* assume this frame has the same number of packets as the
576                * previous one */
577               cd = diff / num_AU_headers;
578               GST_DEBUG_OBJECT (depayload, "guessing constantDuration %d", cd);
579             }
580
581             if (cd > 0) {
582               /* get the number of packets by dividing with the duration */
583               diff /= cd;
584             } else {
585               diff = 0;
586             }
587
588             rtpmp4gdepay->last_AU_index += diff;
589             rtpmp4gdepay->prev_AU_index = AU_index;
590
591             AU_index = rtpmp4gdepay->last_AU_index;
592
593             GST_DEBUG_OBJECT (rtpmp4gdepay, "diff %d, AU index %u", diff,
594                 AU_index);
595           } else {
596             rtpmp4gdepay->prev_AU_index = AU_index;
597             rtpmp4gdepay->last_AU_index = AU_index;
598           }
599
600           /* keep track of the higest AU_index */
601           if (rtpmp4gdepay->max_AU_index != -1
602               && rtpmp4gdepay->max_AU_index <= AU_index) {
603             GST_DEBUG_OBJECT (rtpmp4gdepay, "new interleave group, flushing");
604             /* a new interleave group started, flush */
605             gst_rtp_mp4g_depay_flush_queue (rtpmp4gdepay);
606           }
607           if (G_UNLIKELY (!rtpmp4gdepay->maxDisplacement &&
608                   rtpmp4gdepay->max_AU_index != -1
609                   && rtpmp4gdepay->max_AU_index >= AU_index)) {
610             GstBuffer *outbuf;
611
612             /* some broken non-interleaved streams have AU-index jumping around
613              * all over the place, apparently assuming receiver disregards */
614             GST_DEBUG_OBJECT (rtpmp4gdepay, "non-interleaved broken AU indices;"
615                 " forcing continuous flush");
616             /* reset AU to avoid repeated DISCONT in such case */
617             outbuf = g_queue_peek_head (rtpmp4gdepay->packets);
618             if (G_LIKELY (outbuf)) {
619               rtpmp4gdepay->next_AU_index = GST_BUFFER_OFFSET (outbuf);
620               gst_rtp_mp4g_depay_flush_queue (rtpmp4gdepay);
621             }
622             /* rebase next_AU_index to current rtp's first AU_index */
623             rtpmp4gdepay->next_AU_index = AU_index;
624           }
625           rtpmp4gdepay->prev_rtptime = rtptime;
626           rtpmp4gdepay->prev_AU_num = num_AU_headers;
627         } else {
628           AU_index_delta =
629               gst_bs_parse_read (&bs, rtpmp4gdepay->indexdeltalength);
630           AU_index += AU_index_delta + 1;
631         }
632         /* keep track of highest AU_index */
633         if (rtpmp4gdepay->max_AU_index == -1
634             || AU_index > rtpmp4gdepay->max_AU_index)
635           rtpmp4gdepay->max_AU_index = AU_index;
636
637         /* the presentation time offset, a 2s-complement value, we need this to
638          * calculate the timestamp on the output packet. */
639         if (rtpmp4gdepay->ctsdeltalength > 0) {
640           if (gst_bs_parse_read (&bs, 1))
641             gst_bs_parse_read (&bs, rtpmp4gdepay->ctsdeltalength);
642         }
643         /* the decoding time offset, a 2s-complement value */
644         if (rtpmp4gdepay->dtsdeltalength > 0) {
645           if (gst_bs_parse_read (&bs, 1))
646             gst_bs_parse_read (&bs, rtpmp4gdepay->dtsdeltalength);
647         }
648         /* RAP-flag to indicate that the AU contains a keyframe */
649         if (rtpmp4gdepay->randomaccessindication)
650           gst_bs_parse_read (&bs, 1);
651         /* stream-state */
652         if (rtpmp4gdepay->streamstateindication > 0)
653           gst_bs_parse_read (&bs, rtpmp4gdepay->streamstateindication);
654
655         GST_DEBUG_OBJECT (rtpmp4gdepay, "size %d, index %d, delta %d", AU_size,
656             AU_index, AU_index_delta);
657
658         /* fragmented pakets have the AU_size set to the size of the
659          * unfragmented AU. */
660         if (AU_size > payload_AU_size)
661           AU_size = payload_AU_size;
662
663         /* collect stuff in the adapter, strip header from payload and push in
664          * the adapter */
665         outbuf =
666             gst_rtp_buffer_get_payload_subbuffer (rtp, payload_AU, AU_size);
667         gst_adapter_push (rtpmp4gdepay->adapter, outbuf);
668
669         if (M) {
670           guint32 v = 0;
671           guint avail;
672
673           /* packet is complete, flush */
674           avail = gst_adapter_available (rtpmp4gdepay->adapter);
675
676           /* Some broken senders send ADTS headers (e.g. some Sony cameras).
677            * Try to detect those and skip them (still needs config set), but
678            * don't check every frame, only the first (unless we detect ADTS) */
679           if (rtpmp4gdepay->check_adts && avail >= 7) {
680             if (gst_adapter_masked_scan_uint32_peek (rtpmp4gdepay->adapter,
681                     0xfffe0000, 0xfff00000, 0, 4, &v) == 0) {
682               guint adts_hdr_len = (((v >> 16) & 0x1) == 0) ? 9 : 7;
683               if (avail > adts_hdr_len) {
684                 GST_WARNING_OBJECT (rtpmp4gdepay, "Detected ADTS header of "
685                     "%u bytes, skipping", adts_hdr_len);
686                 gst_adapter_flush (rtpmp4gdepay->adapter, adts_hdr_len);
687                 avail -= adts_hdr_len;
688               }
689             } else {
690               rtpmp4gdepay->check_adts = FALSE;
691             }
692           }
693
694           outbuf = gst_adapter_take_buffer (rtpmp4gdepay->adapter, avail);
695
696           /* copy some of the fields we calculated above on the buffer. We also
697            * copy the AU_index so that we can sort the packets in our queue. */
698           GST_BUFFER_PTS (outbuf) = timestamp;
699           GST_BUFFER_OFFSET (outbuf) = AU_index;
700
701           if (rtpmp4gdepay->constantDuration != 0) {
702             /* if we have constantDuration, calculate timestamp for next AU
703              * in this RTP packet. */
704             timestamp += (rtpmp4gdepay->constantDuration * GST_SECOND) /
705                 depayload->clock_rate;
706           } else {
707             /* otherwise, make sure we don't use the timestamp again for other
708              * AUs. */
709             timestamp = GST_CLOCK_TIME_NONE;
710           }
711
712           GST_DEBUG_OBJECT (depayload,
713               "pushing buffer of size %" G_GSIZE_FORMAT,
714               gst_buffer_get_size (outbuf));
715
716           gst_rtp_mp4g_depay_queue (rtpmp4gdepay, outbuf);
717
718         }
719         payload_AU += AU_size;
720         payload_AU_size -= AU_size;
721       }
722     } else {
723       /* push complete buffer in adapter */
724       outbuf = gst_rtp_buffer_get_payload_subbuffer (rtp, 0, payload_len);
725       gst_adapter_push (rtpmp4gdepay->adapter, outbuf);
726
727       /* if this was the last packet of the VOP, create and push a buffer */
728       if (M) {
729         guint avail;
730
731         avail = gst_adapter_available (rtpmp4gdepay->adapter);
732
733         outbuf = gst_adapter_take_buffer (rtpmp4gdepay->adapter, avail);
734
735         GST_DEBUG ("gst_rtp_mp4g_depay_chain: pushing buffer of size %"
736             G_GSIZE_FORMAT, gst_buffer_get_size (outbuf));
737
738         return outbuf;
739       }
740     }
741   }
742
743   return NULL;
744
745   /* ERRORS */
746 short_payload:
747   {
748     GST_ELEMENT_WARNING (rtpmp4gdepay, STREAM, DECODE,
749         ("Packet payload was too short."), (NULL));
750     return NULL;
751   }
752 }
753
754 static gboolean
755 gst_rtp_mp4g_depay_handle_event (GstRTPBaseDepayload * filter, GstEvent * event)
756 {
757   gboolean ret;
758   GstRtpMP4GDepay *rtpmp4gdepay;
759
760   rtpmp4gdepay = GST_RTP_MP4G_DEPAY (filter);
761
762   switch (GST_EVENT_TYPE (event)) {
763     case GST_EVENT_FLUSH_STOP:
764       gst_rtp_mp4g_depay_reset (rtpmp4gdepay);
765       break;
766     default:
767       break;
768   }
769
770   ret =
771       GST_RTP_BASE_DEPAYLOAD_CLASS (parent_class)->handle_event (filter, event);
772
773   return ret;
774 }
775
776 static GstStateChangeReturn
777 gst_rtp_mp4g_depay_change_state (GstElement * element,
778     GstStateChange transition)
779 {
780   GstRtpMP4GDepay *rtpmp4gdepay;
781   GstStateChangeReturn ret;
782
783   rtpmp4gdepay = GST_RTP_MP4G_DEPAY (element);
784
785   switch (transition) {
786     case GST_STATE_CHANGE_READY_TO_PAUSED:
787       gst_rtp_mp4g_depay_reset (rtpmp4gdepay);
788       break;
789     default:
790       break;
791   }
792
793   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
794
795   switch (transition) {
796     case GST_STATE_CHANGE_PAUSED_TO_READY:
797       gst_rtp_mp4g_depay_reset (rtpmp4gdepay);
798       break;
799     default:
800       break;
801   }
802   return ret;
803 }
804
805 gboolean
806 gst_rtp_mp4g_depay_plugin_init (GstPlugin * plugin)
807 {
808   return gst_element_register (plugin, "rtpmp4gdepay",
809       GST_RANK_SECONDARY, GST_TYPE_RTP_MP4G_DEPAY);
810 }