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