rtp: port remaining to 0.11
[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 static GstStaticPadTemplate gst_rtp_mp4g_depay_src_template =
34     GST_STATIC_PAD_TEMPLATE ("src",
35     GST_PAD_SRC,
36     GST_PAD_ALWAYS,
37     GST_STATIC_CAPS ("video/mpeg,"
38         "mpegversion=(int) 4,"
39         "systemstream=(boolean)false;"
40         "audio/mpeg," "mpegversion=(int) 4, " "stream-format=(string)raw")
41     );
42
43 static GstStaticPadTemplate gst_rtp_mp4g_depay_sink_template =
44 GST_STATIC_PAD_TEMPLATE ("sink",
45     GST_PAD_SINK,
46     GST_PAD_ALWAYS,
47     GST_STATIC_CAPS ("application/x-rtp, "
48         "media = (string) { \"video\", \"audio\", \"application\" }, "
49         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
50         "clock-rate = (int) [1, MAX ], "
51         "encoding-name = (string) \"MPEG4-GENERIC\", "
52         /* required string params */
53         "streamtype = (string) { \"4\", \"5\" }, "      /* 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_BASE_RTP_DEPAYLOAD);
132
133 static void gst_rtp_mp4g_depay_finalize (GObject * object);
134
135 static gboolean gst_rtp_mp4g_depay_setcaps (GstBaseRTPDepayload * depayload,
136     GstCaps * caps);
137 static GstBuffer *gst_rtp_mp4g_depay_process (GstBaseRTPDepayload * depayload,
138     GstBuffer * buf);
139 static gboolean gst_rtp_mp4g_depay_handle_event (GstBaseRTPDepayload * 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   GstBaseRTPDepayloadClass *gstbasertpdepayload_class;
152
153   gobject_class = (GObjectClass *) klass;
154   gstelement_class = (GstElementClass *) klass;
155   gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) 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   gstbasertpdepayload_class->process = gst_rtp_mp4g_depay_process;
162   gstbasertpdepayload_class->set_caps = gst_rtp_mp4g_depay_setcaps;
163   gstbasertpdepayload_class->handle_event = gst_rtp_mp4g_depay_handle_event;
164
165   gst_element_class_add_pad_template (gstelement_class,
166       gst_static_pad_template_get (&gst_rtp_mp4g_depay_src_template));
167   gst_element_class_add_pad_template (gstelement_class,
168       gst_static_pad_template_get (&gst_rtp_mp4g_depay_sink_template));
169
170   gst_element_class_set_details_simple (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 (GstBaseRTPDepayload * 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   if ((str = gst_structure_get_string (structure, "media"))) {
237     if (strcmp (str, "audio") == 0) {
238       srccaps = gst_caps_new_simple ("audio/mpeg",
239           "mpegversion", G_TYPE_INT, 4, "stream-format", G_TYPE_STRING, "raw",
240           NULL);
241     } else if (strcmp (str, "video") == 0) {
242       srccaps = gst_caps_new_simple ("video/mpeg",
243           "mpegversion", G_TYPE_INT, 4,
244           "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
245     }
246   }
247   if (srccaps == NULL)
248     goto unknown_media;
249
250   /* these values are optional and have a default value of 0 (no header) */
251   rtpmp4gdepay->sizelength =
252       gst_rtp_mp4g_depay_parse_int (structure, "sizelength", 0);
253   rtpmp4gdepay->indexlength =
254       gst_rtp_mp4g_depay_parse_int (structure, "indexlength", 0);
255   rtpmp4gdepay->indexdeltalength =
256       gst_rtp_mp4g_depay_parse_int (structure, "indexdeltalength", 0);
257   rtpmp4gdepay->ctsdeltalength =
258       gst_rtp_mp4g_depay_parse_int (structure, "ctsdeltalength", 0);
259   rtpmp4gdepay->dtsdeltalength =
260       gst_rtp_mp4g_depay_parse_int (structure, "dtsdeltalength", 0);
261   someint =
262       gst_rtp_mp4g_depay_parse_int (structure, "randomaccessindication", 0);
263   rtpmp4gdepay->randomaccessindication = someint > 0 ? 1 : 0;
264   rtpmp4gdepay->streamstateindication =
265       gst_rtp_mp4g_depay_parse_int (structure, "streamstateindication", 0);
266   rtpmp4gdepay->auxiliarydatasizelength =
267       gst_rtp_mp4g_depay_parse_int (structure, "auxiliarydatasizelength", 0);
268   rtpmp4gdepay->constantSize =
269       gst_rtp_mp4g_depay_parse_int (structure, "constantsize", 0);
270   rtpmp4gdepay->constantDuration =
271       gst_rtp_mp4g_depay_parse_int (structure, "constantduration", 0);
272   rtpmp4gdepay->maxDisplacement =
273       gst_rtp_mp4g_depay_parse_int (structure, "maxdisplacement", 0);
274
275
276   /* get config string */
277   if ((str = gst_structure_get_string (structure, "config"))) {
278     GValue v = { 0 };
279
280     g_value_init (&v, GST_TYPE_BUFFER);
281     if (gst_value_deserialize (&v, str)) {
282       GstBuffer *buffer;
283
284       buffer = gst_value_get_buffer (&v);
285       gst_caps_set_simple (srccaps,
286           "codec_data", GST_TYPE_BUFFER, buffer, NULL);
287       g_value_unset (&v);
288     } else {
289       g_warning ("cannot convert config to buffer");
290     }
291   }
292
293   res = gst_pad_set_caps (depayload->srcpad, srccaps);
294   gst_caps_unref (srccaps);
295
296   return res;
297
298   /* ERRORS */
299 unknown_media:
300   {
301     GST_DEBUG_OBJECT (rtpmp4gdepay, "Unknown media type");
302     return FALSE;
303   }
304 }
305
306 static void
307 gst_rtp_mp4g_depay_clear_queue (GstRtpMP4GDepay * rtpmp4gdepay)
308 {
309   GstBuffer *outbuf;
310
311   while ((outbuf = g_queue_pop_head (rtpmp4gdepay->packets)))
312     gst_buffer_unref (outbuf);
313 }
314
315 static void
316 gst_rtp_mp4g_depay_reset (GstRtpMP4GDepay * rtpmp4gdepay)
317 {
318   gst_adapter_clear (rtpmp4gdepay->adapter);
319   rtpmp4gdepay->max_AU_index = -1;
320   rtpmp4gdepay->next_AU_index = -1;
321   rtpmp4gdepay->prev_AU_index = -1;
322   rtpmp4gdepay->prev_rtptime = -1;
323   rtpmp4gdepay->last_AU_index = -1;
324   gst_rtp_mp4g_depay_clear_queue (rtpmp4gdepay);
325 }
326
327 static void
328 gst_rtp_mp4g_depay_flush_queue (GstRtpMP4GDepay * rtpmp4gdepay)
329 {
330   GstBuffer *outbuf;
331   gboolean discont = FALSE;
332   guint AU_index;
333
334   while ((outbuf = g_queue_pop_head (rtpmp4gdepay->packets))) {
335     AU_index = GST_BUFFER_OFFSET (outbuf);
336
337     GST_DEBUG_OBJECT (rtpmp4gdepay, "next available AU_index %u", AU_index);
338
339     if (rtpmp4gdepay->next_AU_index != AU_index) {
340       GST_DEBUG_OBJECT (rtpmp4gdepay, "discont, expected AU_index %u",
341           rtpmp4gdepay->next_AU_index);
342       discont = TRUE;
343     }
344
345     if (discont) {
346       GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
347       discont = FALSE;
348     }
349
350     GST_DEBUG_OBJECT (rtpmp4gdepay, "pushing AU_index %u", AU_index);
351     gst_base_rtp_depayload_push (GST_BASE_RTP_DEPAYLOAD (rtpmp4gdepay), outbuf);
352     rtpmp4gdepay->next_AU_index = AU_index + 1;
353   }
354 }
355
356 static void
357 gst_rtp_mp4g_depay_queue (GstRtpMP4GDepay * rtpmp4gdepay, GstBuffer * outbuf)
358 {
359   guint AU_index = GST_BUFFER_OFFSET (outbuf);
360
361   if (rtpmp4gdepay->next_AU_index == -1) {
362     GST_DEBUG_OBJECT (rtpmp4gdepay, "Init AU counter %u", AU_index);
363     rtpmp4gdepay->next_AU_index = AU_index;
364   }
365
366   if (rtpmp4gdepay->next_AU_index == AU_index) {
367     GST_DEBUG_OBJECT (rtpmp4gdepay, "pushing expected AU_index %u", AU_index);
368
369     /* we received the expected packet, push it and flush as much as we can from
370      * the queue */
371     gst_base_rtp_depayload_push (GST_BASE_RTP_DEPAYLOAD (rtpmp4gdepay), outbuf);
372     rtpmp4gdepay->next_AU_index++;
373
374     while ((outbuf = g_queue_peek_head (rtpmp4gdepay->packets))) {
375       AU_index = GST_BUFFER_OFFSET (outbuf);
376
377       GST_DEBUG_OBJECT (rtpmp4gdepay, "next available AU_index %u", AU_index);
378
379       if (rtpmp4gdepay->next_AU_index == AU_index) {
380         GST_DEBUG_OBJECT (rtpmp4gdepay, "pushing expected AU_index %u",
381             AU_index);
382         outbuf = g_queue_pop_head (rtpmp4gdepay->packets);
383         gst_base_rtp_depayload_push (GST_BASE_RTP_DEPAYLOAD (rtpmp4gdepay),
384             outbuf);
385         rtpmp4gdepay->next_AU_index++;
386       } else {
387         GST_DEBUG_OBJECT (rtpmp4gdepay, "waiting for next AU_index %u",
388             rtpmp4gdepay->next_AU_index);
389         break;
390       }
391     }
392   } else {
393     GList *list;
394
395     GST_DEBUG_OBJECT (rtpmp4gdepay, "queueing AU_index %u", AU_index);
396
397     /* loop the list to skip strictly smaller AU_index buffers */
398     for (list = rtpmp4gdepay->packets->head; list; list = g_list_next (list)) {
399       guint idx;
400       gint gap;
401
402       idx = GST_BUFFER_OFFSET (GST_BUFFER_CAST (list->data));
403
404       /* compare the new seqnum to the one in the buffer */
405       gap = (gint) (idx - AU_index);
406
407       GST_DEBUG_OBJECT (rtpmp4gdepay, "compare with AU_index %u, gap %d", idx,
408           gap);
409
410       /* AU_index <= idx, we can stop looking */
411       if (G_LIKELY (gap > 0))
412         break;
413     }
414     if (G_LIKELY (list))
415       g_queue_insert_before (rtpmp4gdepay->packets, list, outbuf);
416     else
417       g_queue_push_tail (rtpmp4gdepay->packets, outbuf);
418   }
419 }
420
421 static GstBuffer *
422 gst_rtp_mp4g_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
423 {
424   GstRtpMP4GDepay *rtpmp4gdepay;
425   GstBuffer *outbuf = NULL;
426   GstClockTime timestamp;
427   GstRTPBuffer rtp;
428
429   rtpmp4gdepay = GST_RTP_MP4G_DEPAY (depayload);
430
431   /* flush remaining data on discont */
432   if (GST_BUFFER_IS_DISCONT (buf)) {
433     GST_DEBUG_OBJECT (rtpmp4gdepay, "received DISCONT");
434     gst_adapter_clear (rtpmp4gdepay->adapter);
435   }
436
437   timestamp = GST_BUFFER_TIMESTAMP (buf);
438
439   {
440     gint payload_len, payload_AU;
441     guint8 *payload;
442     guint32 rtptime;
443     guint AU_headers_len;
444     guint AU_size, AU_index, AU_index_delta, payload_AU_size;
445     gboolean M;
446
447     gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);
448     payload_len = gst_rtp_buffer_get_payload_len (&rtp);
449     payload = gst_rtp_buffer_get_payload (&rtp);
450
451     GST_DEBUG_OBJECT (rtpmp4gdepay, "received payload of %d", payload_len);
452
453     rtptime = gst_rtp_buffer_get_timestamp (&rtp);
454     M = gst_rtp_buffer_get_marker (&rtp);
455
456     gst_rtp_buffer_unmap (&rtp);
457
458     if (rtpmp4gdepay->sizelength > 0) {
459       gint num_AU_headers, AU_headers_bytes, i;
460       GstBsParse bs;
461
462       if (payload_len < 2)
463         goto short_payload;
464
465       /* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- .. -+-+-+-+-+-+-+-+-+-+
466        * |AU-headers-length|AU-header|AU-header|      |AU-header|padding|
467        * |                 |   (1)   |   (2)   |      |   (n) * | bits  |
468        * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- .. -+-+-+-+-+-+-+-+-+-+
469        *
470        * The length is 2 bytes and contains the length of the following
471        * AU-headers in bits.
472        */
473       AU_headers_len = (payload[0] << 8) | payload[1];
474       AU_headers_bytes = (AU_headers_len + 7) / 8;
475       num_AU_headers = AU_headers_len / 16;
476
477       GST_DEBUG_OBJECT (rtpmp4gdepay, "AU headers len %d, bytes %d, num %d",
478           AU_headers_len, AU_headers_bytes, num_AU_headers);
479
480       /* skip header */
481       payload += 2;
482       payload_len -= 2;
483
484       if (payload_len < AU_headers_bytes)
485         goto short_payload;
486
487       /* skip special headers, point to first payload AU */
488       payload_AU = 2 + AU_headers_bytes;
489       payload_AU_size = payload_len - AU_headers_bytes;
490
491       if (G_UNLIKELY (rtpmp4gdepay->auxiliarydatasizelength)) {
492         gint aux_size;
493
494         /* point the bitstream parser to the first auxiliary data bit */
495         gst_bs_parse_init (&bs, payload + AU_headers_bytes,
496             payload_len - AU_headers_bytes);
497         aux_size =
498             gst_bs_parse_read (&bs, rtpmp4gdepay->auxiliarydatasizelength);
499         /* convert to bytes */
500         aux_size = (aux_size + 7) / 8;
501         /* AU data then follows auxiliary data */
502         if (payload_AU_size < aux_size)
503           goto short_payload;
504         payload_AU += aux_size;
505         payload_AU_size -= aux_size;
506       }
507
508       /* point the bitstream parser to the first AU header bit */
509       gst_bs_parse_init (&bs, payload, payload_len);
510       AU_index = AU_index_delta = 0;
511
512       for (i = 0; i < num_AU_headers && payload_AU_size > 0; i++) {
513         /* parse AU header
514          *  +---------------------------------------+
515          *  |     AU-size                           |
516          *  +---------------------------------------+
517          *  |     AU-Index / AU-Index-delta         |
518          *  +---------------------------------------+
519          *  |     CTS-flag                          |
520          *  +---------------------------------------+
521          *  |     CTS-delta                         |
522          *  +---------------------------------------+
523          *  |     DTS-flag                          |
524          *  +---------------------------------------+
525          *  |     DTS-delta                         |
526          *  +---------------------------------------+
527          *  |     RAP-flag                          |
528          *  +---------------------------------------+
529          *  |     Stream-state                      |
530          *  +---------------------------------------+
531          */
532         AU_size = gst_bs_parse_read (&bs, rtpmp4gdepay->sizelength);
533
534         /* calculate the AU_index, which is only on the first AU of the packet
535          * and the AU_index_delta on the other AUs. This will be used to
536          * reconstruct the AU ordering when interleaving. */
537         if (i == 0) {
538           AU_index = gst_bs_parse_read (&bs, rtpmp4gdepay->indexlength);
539
540           GST_DEBUG_OBJECT (rtpmp4gdepay, "AU index %u", AU_index);
541
542           if (AU_index == 0 && rtpmp4gdepay->prev_AU_index == 0) {
543             gint diff;
544             gint cd;
545
546             /* if we see two consecutive packets with AU_index of 0, we can
547              * assume we have constantDuration packets. Since we don't have
548              * the index we must use the AU duration to calculate the
549              * index. Get the diff between the timestamps first, this can be
550              * positive or negative. */
551             if (rtpmp4gdepay->prev_rtptime <= rtptime)
552               diff = rtptime - rtpmp4gdepay->prev_rtptime;
553             else
554               diff = -(rtpmp4gdepay->prev_rtptime - rtptime);
555
556             /* if no constantDuration was given, make one */
557             if (rtpmp4gdepay->constantDuration != 0) {
558               cd = rtpmp4gdepay->constantDuration;
559               GST_DEBUG_OBJECT (depayload, "using constantDuration %d", cd);
560             } else if (rtpmp4gdepay->prev_AU_num > 0) {
561               /* use number of packets and of previous frame */
562               cd = diff / rtpmp4gdepay->prev_AU_num;
563               GST_DEBUG_OBJECT (depayload, "guessing constantDuration %d", cd);
564             } else {
565               /* assume this frame has the same number of packets as the
566                * previous one */
567               cd = diff / num_AU_headers;
568               GST_DEBUG_OBJECT (depayload, "guessing constantDuration %d", cd);
569             }
570
571             if (cd > 0) {
572               /* get the number of packets by dividing with the duration */
573               diff /= cd;
574             } else {
575               diff = 0;
576             }
577
578             rtpmp4gdepay->last_AU_index += diff;
579             rtpmp4gdepay->prev_AU_index = AU_index;
580
581             AU_index = rtpmp4gdepay->last_AU_index;
582
583             GST_DEBUG_OBJECT (rtpmp4gdepay, "diff %d, AU index %u", diff,
584                 AU_index);
585           } else {
586             rtpmp4gdepay->prev_AU_index = AU_index;
587             rtpmp4gdepay->last_AU_index = AU_index;
588           }
589
590           /* keep track of the higest AU_index */
591           if (rtpmp4gdepay->max_AU_index != -1
592               && rtpmp4gdepay->max_AU_index <= AU_index) {
593             GST_DEBUG_OBJECT (rtpmp4gdepay, "new interleave group, flushing");
594             /* a new interleave group started, flush */
595             gst_rtp_mp4g_depay_flush_queue (rtpmp4gdepay);
596           }
597           if (G_UNLIKELY (!rtpmp4gdepay->maxDisplacement &&
598                   rtpmp4gdepay->max_AU_index != -1
599                   && rtpmp4gdepay->max_AU_index >= AU_index)) {
600             GstBuffer *outbuf;
601
602             /* some broken non-interleaved streams have AU-index jumping around
603              * all over the place, apparently assuming receiver disregards */
604             GST_DEBUG_OBJECT (rtpmp4gdepay, "non-interleaved broken AU indices;"
605                 " forcing continuous flush");
606             /* reset AU to avoid repeated DISCONT in such case */
607             outbuf = g_queue_peek_head (rtpmp4gdepay->packets);
608             if (G_LIKELY (outbuf)) {
609               rtpmp4gdepay->next_AU_index = GST_BUFFER_OFFSET (outbuf);
610               gst_rtp_mp4g_depay_flush_queue (rtpmp4gdepay);
611             }
612           }
613           rtpmp4gdepay->prev_rtptime = rtptime;
614           rtpmp4gdepay->prev_AU_num = num_AU_headers;
615         } else {
616           AU_index_delta =
617               gst_bs_parse_read (&bs, rtpmp4gdepay->indexdeltalength);
618           AU_index += AU_index_delta + 1;
619         }
620         /* keep track of highest AU_index */
621         if (rtpmp4gdepay->max_AU_index == -1
622             || AU_index > rtpmp4gdepay->max_AU_index)
623           rtpmp4gdepay->max_AU_index = AU_index;
624
625         /* the presentation time offset, a 2s-complement value, we need this to
626          * calculate the timestamp on the output packet. */
627         if (rtpmp4gdepay->ctsdeltalength > 0) {
628           if (gst_bs_parse_read (&bs, 1))
629             gst_bs_parse_read (&bs, rtpmp4gdepay->ctsdeltalength);
630         }
631         /* the decoding time offset, a 2s-complement value */
632         if (rtpmp4gdepay->dtsdeltalength > 0) {
633           if (gst_bs_parse_read (&bs, 1))
634             gst_bs_parse_read (&bs, rtpmp4gdepay->dtsdeltalength);
635         }
636         /* RAP-flag to indicate that the AU contains a keyframe */
637         if (rtpmp4gdepay->randomaccessindication)
638           gst_bs_parse_read (&bs, 1);
639         /* stream-state */
640         if (rtpmp4gdepay->streamstateindication > 0)
641           gst_bs_parse_read (&bs, rtpmp4gdepay->streamstateindication);
642
643         GST_DEBUG_OBJECT (rtpmp4gdepay, "size %d, index %d, delta %d", AU_size,
644             AU_index, AU_index_delta);
645
646         /* fragmented pakets have the AU_size set to the size of the
647          * unfragmented AU. */
648         if (AU_size > payload_AU_size)
649           AU_size = payload_AU_size;
650
651         /* collect stuff in the adapter, strip header from payload and push in
652          * the adapter */
653         outbuf =
654             gst_rtp_buffer_get_payload_subbuffer (&rtp, payload_AU, AU_size);
655         gst_adapter_push (rtpmp4gdepay->adapter, outbuf);
656
657         if (M) {
658           guint avail;
659
660           /* packet is complete, flush */
661           avail = gst_adapter_available (rtpmp4gdepay->adapter);
662
663           outbuf = gst_adapter_take_buffer (rtpmp4gdepay->adapter, avail);
664
665           /* copy some of the fields we calculated above on the buffer. We also
666            * copy the AU_index so that we can sort the packets in our queue. */
667           GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
668           GST_BUFFER_OFFSET (outbuf) = AU_index;
669
670           /* make sure we don't use the timestamp again for other AUs in this
671            * RTP packet. */
672           timestamp = -1;
673
674           GST_DEBUG_OBJECT (depayload, "pushing buffer of size %d",
675               gst_buffer_get_size (outbuf));
676
677           gst_rtp_mp4g_depay_queue (rtpmp4gdepay, outbuf);
678
679         }
680         payload_AU += AU_size;
681         payload_AU_size -= AU_size;
682       }
683     } else {
684       /* push complete buffer in adapter */
685       outbuf = gst_rtp_buffer_get_payload_subbuffer (&rtp, 0, payload_len);
686       gst_adapter_push (rtpmp4gdepay->adapter, outbuf);
687
688       /* if this was the last packet of the VOP, create and push a buffer */
689       if (M) {
690         guint avail;
691
692         avail = gst_adapter_available (rtpmp4gdepay->adapter);
693
694         outbuf = gst_adapter_take_buffer (rtpmp4gdepay->adapter, avail);
695
696         GST_DEBUG ("gst_rtp_mp4g_depay_chain: pushing buffer of size %d",
697             gst_buffer_get_size (outbuf));
698
699         gst_rtp_buffer_unmap (&rtp);
700         return outbuf;
701       }
702     }
703   }
704
705   gst_rtp_buffer_unmap (&rtp);
706   return NULL;
707
708   /* ERRORS */
709 short_payload:
710   {
711     GST_ELEMENT_WARNING (rtpmp4gdepay, STREAM, DECODE,
712         ("Packet payload was too short."), (NULL));
713     gst_rtp_buffer_unmap (&rtp);
714     return NULL;
715   }
716 }
717
718 static gboolean
719 gst_rtp_mp4g_depay_handle_event (GstBaseRTPDepayload * filter, GstEvent * event)
720 {
721   gboolean ret;
722   GstRtpMP4GDepay *rtpmp4gdepay;
723
724   rtpmp4gdepay = GST_RTP_MP4G_DEPAY (filter);
725
726   switch (GST_EVENT_TYPE (event)) {
727     case GST_EVENT_FLUSH_STOP:
728       gst_rtp_mp4g_depay_reset (rtpmp4gdepay);
729       break;
730     default:
731       break;
732   }
733
734   ret =
735       GST_BASE_RTP_DEPAYLOAD_CLASS (parent_class)->handle_event (filter, event);
736
737   return ret;
738 }
739
740 static GstStateChangeReturn
741 gst_rtp_mp4g_depay_change_state (GstElement * element,
742     GstStateChange transition)
743 {
744   GstRtpMP4GDepay *rtpmp4gdepay;
745   GstStateChangeReturn ret;
746
747   rtpmp4gdepay = GST_RTP_MP4G_DEPAY (element);
748
749   switch (transition) {
750     case GST_STATE_CHANGE_READY_TO_PAUSED:
751       gst_rtp_mp4g_depay_reset (rtpmp4gdepay);
752       break;
753     default:
754       break;
755   }
756
757   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
758
759   switch (transition) {
760     case GST_STATE_CHANGE_PAUSED_TO_READY:
761       gst_rtp_mp4g_depay_reset (rtpmp4gdepay);
762       break;
763     default:
764       break;
765   }
766   return ret;
767 }
768
769 gboolean
770 gst_rtp_mp4g_depay_plugin_init (GstPlugin * plugin)
771 {
772   return gst_element_register (plugin, "rtpmp4gdepay",
773       GST_RANK_SECONDARY, GST_TYPE_RTP_MP4G_DEPAY);
774 }