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