gst/rtp/: Fix the descriptions and fix some email addresses.
[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   parent_class = g_type_class_peek_parent (klass);
174
175   gobject_class->finalize = gst_rtp_mp4g_depay_finalize;
176
177   gstelement_class->change_state = gst_rtp_mp4g_depay_change_state;
178
179   gstbasertpdepayload_class->process = gst_rtp_mp4g_depay_process;
180   gstbasertpdepayload_class->set_caps = gst_rtp_mp4g_depay_setcaps;
181
182   GST_DEBUG_CATEGORY_INIT (rtpmp4gdepay_debug, "rtpmp4gdepay", 0,
183       "MP4-generic RTP Depayloader");
184 }
185
186 static void
187 gst_rtp_mp4g_depay_init (GstRtpMP4GDepay * rtpmp4gdepay,
188     GstRtpMP4GDepayClass * klass)
189 {
190   rtpmp4gdepay->adapter = gst_adapter_new ();
191   rtpmp4gdepay->packets = g_queue_new ();
192 }
193
194 static void
195 gst_rtp_mp4g_depay_finalize (GObject * object)
196 {
197   GstRtpMP4GDepay *rtpmp4gdepay;
198
199   rtpmp4gdepay = GST_RTP_MP4G_DEPAY (object);
200
201   g_object_unref (rtpmp4gdepay->adapter);
202   rtpmp4gdepay->adapter = NULL;
203   g_queue_free (rtpmp4gdepay->packets);
204   rtpmp4gdepay->packets = NULL;
205
206   G_OBJECT_CLASS (parent_class)->finalize (object);
207 }
208
209 static gint
210 gst_rtp_mp4g_depay_parse_int (GstStructure * structure, const gchar * field,
211     gint def)
212 {
213   const gchar *str;
214   gint res;
215
216   if ((str = gst_structure_get_string (structure, field)))
217     return atoi (str);
218
219   if (gst_structure_get_int (structure, field, &res))
220     return res;
221
222   return def;
223 }
224
225 static gboolean
226 gst_rtp_mp4g_depay_setcaps (GstBaseRTPDepayload * depayload, GstCaps * caps)
227 {
228   GstStructure *structure;
229   GstRtpMP4GDepay *rtpmp4gdepay;
230   GstCaps *srccaps = NULL;
231   const gchar *str;
232   gint clock_rate;
233   gint someint;
234   gboolean res;
235
236   rtpmp4gdepay = GST_RTP_MP4G_DEPAY (depayload);
237
238   structure = gst_caps_get_structure (caps, 0);
239
240   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
241     clock_rate = 90000;         /* default */
242   depayload->clock_rate = clock_rate;
243
244   if ((str = gst_structure_get_string (structure, "media"))) {
245     if (strcmp (str, "audio") == 0) {
246       srccaps = gst_caps_new_simple ("audio/mpeg",
247           "mpegversion", G_TYPE_INT, 4, NULL);
248       /* AAC always has a default constant duration of 1024 but it can be
249        * overriden below. */
250       rtpmp4gdepay->constantDuration = 1024;
251     } else if (strcmp (str, "video") == 0) {
252       srccaps = gst_caps_new_simple ("video/mpeg",
253           "mpegversion", G_TYPE_INT, 4,
254           "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
255       rtpmp4gdepay->constantDuration = 0;
256     }
257   }
258   if (srccaps == NULL)
259     goto unknown_media;
260
261   /* these values are optional and have a default value of 0 (no header) */
262   rtpmp4gdepay->sizelength =
263       gst_rtp_mp4g_depay_parse_int (structure, "sizelength", 0);
264   rtpmp4gdepay->indexlength =
265       gst_rtp_mp4g_depay_parse_int (structure, "indexlength", 0);
266   rtpmp4gdepay->indexdeltalength =
267       gst_rtp_mp4g_depay_parse_int (structure, "indexdeltalength", 0);
268   rtpmp4gdepay->ctsdeltalength =
269       gst_rtp_mp4g_depay_parse_int (structure, "ctsdeltalength", 0);
270   rtpmp4gdepay->dtsdeltalength =
271       gst_rtp_mp4g_depay_parse_int (structure, "dtsdeltalength", 0);
272   someint =
273       gst_rtp_mp4g_depay_parse_int (structure, "randomaccessindication", 0);
274   rtpmp4gdepay->randomaccessindication = someint > 0 ? 1 : 0;
275   rtpmp4gdepay->streamstateindication =
276       gst_rtp_mp4g_depay_parse_int (structure, "streamstateindication", 0);
277   rtpmp4gdepay->auxiliarydatasizelength =
278       gst_rtp_mp4g_depay_parse_int (structure, "auxiliarydatasizelength", 0);
279   rtpmp4gdepay->constantSize =
280       gst_rtp_mp4g_depay_parse_int (structure, "constantsize", 0);
281   rtpmp4gdepay->constantDuration =
282       gst_rtp_mp4g_depay_parse_int (structure, "constantduration",
283       rtpmp4gdepay->constantDuration);
284
285
286   /* get config string */
287   if ((str = gst_structure_get_string (structure, "config"))) {
288     GValue v = { 0 };
289
290     g_value_init (&v, GST_TYPE_BUFFER);
291     if (gst_value_deserialize (&v, str)) {
292       GstBuffer *buffer;
293
294       buffer = gst_value_get_buffer (&v);
295       gst_caps_set_simple (srccaps,
296           "codec_data", GST_TYPE_BUFFER, buffer, NULL);
297       g_value_unset (&v);
298     } else {
299       g_warning ("cannot convert config to buffer");
300     }
301   }
302
303   res = gst_pad_set_caps (depayload->srcpad, srccaps);
304   gst_caps_unref (srccaps);
305
306   return res;
307
308   /* ERRORS */
309 unknown_media:
310   {
311     GST_DEBUG_OBJECT (rtpmp4gdepay, "Unknown media type");
312     return FALSE;
313   }
314 }
315
316 static void
317 gst_rtp_mp4g_depay_clear_queue (GstRtpMP4GDepay * rtpmp4gdepay)
318 {
319   GstBuffer *outbuf;
320
321   while ((outbuf = g_queue_pop_head (rtpmp4gdepay->packets)))
322     gst_buffer_unref (outbuf);
323 }
324
325 static void
326 gst_rtp_mp4g_depay_flush_queue (GstRtpMP4GDepay * rtpmp4gdepay)
327 {
328   GstBuffer *outbuf;
329   gboolean discont = FALSE;
330   guint AU_index;
331
332   while ((outbuf = g_queue_pop_head (rtpmp4gdepay->packets))) {
333     AU_index = GST_BUFFER_OFFSET (outbuf);
334
335     GST_DEBUG_OBJECT (rtpmp4gdepay, "next available AU_index %u", AU_index);
336
337     if (rtpmp4gdepay->next_AU_index != AU_index) {
338       GST_DEBUG_OBJECT (rtpmp4gdepay, "discont, expected AU_index %u",
339           rtpmp4gdepay->next_AU_index);
340       discont = TRUE;
341     }
342
343     if (discont) {
344       GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
345       discont = FALSE;
346     }
347
348     GST_DEBUG_OBJECT (rtpmp4gdepay, "pushing AU_index %u", AU_index);
349     gst_base_rtp_depayload_push (GST_BASE_RTP_DEPAYLOAD (rtpmp4gdepay), outbuf);
350     rtpmp4gdepay->next_AU_index = AU_index + 1;
351   }
352 }
353
354 static void
355 gst_rtp_mp4g_depay_queue (GstRtpMP4GDepay * rtpmp4gdepay, GstBuffer * outbuf)
356 {
357   guint AU_index = GST_BUFFER_OFFSET (outbuf);
358
359   if (rtpmp4gdepay->next_AU_index == -1) {
360     GST_DEBUG_OBJECT (rtpmp4gdepay, "Init AU counter %u", AU_index);
361     rtpmp4gdepay->next_AU_index = AU_index;
362   }
363
364   if (rtpmp4gdepay->next_AU_index == AU_index) {
365     GST_DEBUG_OBJECT (rtpmp4gdepay, "pushing expected AU_index %u", AU_index);
366
367     /* we received the expected packet, push it and flush as much as we can from
368      * the queue */
369     gst_base_rtp_depayload_push (GST_BASE_RTP_DEPAYLOAD (rtpmp4gdepay), outbuf);
370     rtpmp4gdepay->next_AU_index++;
371
372     while ((outbuf = g_queue_peek_head (rtpmp4gdepay->packets))) {
373       AU_index = GST_BUFFER_OFFSET (outbuf);
374
375       GST_DEBUG_OBJECT (rtpmp4gdepay, "next available AU_index %u", AU_index);
376
377       if (rtpmp4gdepay->next_AU_index == AU_index) {
378         GST_DEBUG_OBJECT (rtpmp4gdepay, "pushing expected AU_index %u",
379             AU_index);
380         outbuf = g_queue_pop_head (rtpmp4gdepay->packets);
381         gst_base_rtp_depayload_push (GST_BASE_RTP_DEPAYLOAD (rtpmp4gdepay),
382             outbuf);
383         rtpmp4gdepay->next_AU_index++;
384       } else {
385         GST_DEBUG_OBJECT (rtpmp4gdepay, "waiting for next AU_index %u",
386             rtpmp4gdepay->next_AU_index);
387         break;
388       }
389     }
390   } else {
391     GList *list;
392
393     GST_DEBUG_OBJECT (rtpmp4gdepay, "queueing AU_index %u", AU_index);
394
395     /* loop the list to skip strictly smaller AU_index buffers */
396     for (list = rtpmp4gdepay->packets->head; list; list = g_list_next (list)) {
397       guint idx;
398       gint gap;
399
400       idx = GST_BUFFER_OFFSET (GST_BUFFER_CAST (list->data));
401
402       /* compare the new seqnum to the one in the buffer */
403       gap = (gint) (idx - AU_index);
404
405       GST_DEBUG_OBJECT (rtpmp4gdepay, "compare with AU_index %u, gap %d", idx,
406           gap);
407
408       /* AU_index <= idx, we can stop looking */
409       if (G_LIKELY (gap > 0))
410         break;
411     }
412     if (G_LIKELY (list))
413       g_queue_insert_before (rtpmp4gdepay->packets, list, outbuf);
414     else
415       g_queue_push_tail (rtpmp4gdepay->packets, outbuf);
416   }
417 }
418
419 static GstBuffer *
420 gst_rtp_mp4g_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
421 {
422   GstRtpMP4GDepay *rtpmp4gdepay;
423   GstBuffer *outbuf;
424   GstClockTime timestamp;
425
426   rtpmp4gdepay = GST_RTP_MP4G_DEPAY (depayload);
427
428   /* flush remaining data on discont */
429   if (GST_BUFFER_IS_DISCONT (buf)) {
430     GST_DEBUG_OBJECT (rtpmp4gdepay, "received DISCONT");
431     gst_adapter_clear (rtpmp4gdepay->adapter);
432   }
433
434   timestamp = GST_BUFFER_TIMESTAMP (buf);
435
436   {
437     gint payload_len, payload_AU;
438     guint8 *payload;
439     guint32 rtptime;
440     guint AU_headers_len;
441     guint AU_size, AU_index, AU_index_delta, payload_AU_size;
442     gboolean M;
443
444     payload_len = gst_rtp_buffer_get_payload_len (buf);
445     payload = gst_rtp_buffer_get_payload (buf);
446
447     rtptime = gst_rtp_buffer_get_timestamp (buf);
448     M = gst_rtp_buffer_get_marker (buf);
449
450     if (rtpmp4gdepay->sizelength > 0) {
451       gint num_AU_headers, AU_headers_bytes, i;
452       GstBsParse bs;
453
454       if (payload_len < 2)
455         goto short_payload;
456
457       /* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- .. -+-+-+-+-+-+-+-+-+-+
458        * |AU-headers-length|AU-header|AU-header|      |AU-header|padding|
459        * |                 |   (1)   |   (2)   |      |   (n) * | bits  |
460        * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- .. -+-+-+-+-+-+-+-+-+-+
461        *
462        * The length is 2 bytes and contains the length of the following
463        * AU-headers in bits.
464        */
465       AU_headers_len = (payload[0] << 8) | payload[1];
466       AU_headers_bytes = (AU_headers_len + 7) / 8;
467       num_AU_headers = AU_headers_len / 16;
468
469       GST_DEBUG_OBJECT (rtpmp4gdepay, "AU headers len %d, bytes %d, num %d",
470           AU_headers_len, AU_headers_bytes, num_AU_headers);
471
472       /* skip header */
473       payload += 2;
474       payload_len -= 2;
475
476       if (payload_len < AU_headers_bytes)
477         goto short_payload;
478
479       /* skip special headers, point to first payload AU */
480       payload_AU = 2 + AU_headers_bytes;
481       payload_AU_size = payload_len - AU_headers_bytes;
482
483       /* point the bitstream parser to the first AU header bit */
484       gst_bs_parse_init (&bs, payload, payload_len);
485       AU_index = AU_index_delta = 0;
486
487       for (i = 0; i < num_AU_headers && payload_AU_size > 0; i++) {
488         /* parse AU header
489          *  +---------------------------------------+
490          *  |     AU-size                           |
491          *  +---------------------------------------+
492          *  |     AU-Index / AU-Index-delta         |
493          *  +---------------------------------------+
494          *  |     CTS-flag                          |
495          *  +---------------------------------------+
496          *  |     CTS-delta                         |
497          *  +---------------------------------------+
498          *  |     DTS-flag                          |
499          *  +---------------------------------------+
500          *  |     DTS-delta                         |
501          *  +---------------------------------------+
502          *  |     RAP-flag                          |
503          *  +---------------------------------------+
504          *  |     Stream-state                      |
505          *  +---------------------------------------+
506          */
507         AU_size = gst_bs_parse_read (&bs, rtpmp4gdepay->sizelength);
508
509         /* calculate the AU_index, which is only on the first AU of the packet
510          * and the AU_index_delta on the other AUs. This will be used to
511          * reconstruct the AU ordering when interleaving. */
512         if (i == 0) {
513           AU_index = gst_bs_parse_read (&bs, rtpmp4gdepay->indexlength);
514           if (AU_index == 0 && rtpmp4gdepay->prev_AU_index == 0) {
515             gint diff;
516
517             /* if we see two consecutive packets with AU_index of 0, we can
518              * assume we have constantDuration packets. Since we don't have
519              * the index we must use the AU duration to calculate the
520              * index. Get the diff between the timestamps first, this can be
521              * positive or negative. */
522             if (rtpmp4gdepay->prev_rtptime <= rtptime)
523               diff = rtptime - rtpmp4gdepay->prev_rtptime;
524             else
525               diff = -(rtpmp4gdepay->prev_rtptime - rtptime);
526
527             /* get the number of packets by dividing with the duration */
528             diff /= rtpmp4gdepay->constantDuration;
529
530             rtpmp4gdepay->last_AU_index += diff;
531             rtpmp4gdepay->prev_AU_index = AU_index;
532
533             AU_index = rtpmp4gdepay->last_AU_index;
534
535           } else {
536             rtpmp4gdepay->prev_AU_index = AU_index;
537             rtpmp4gdepay->last_AU_index = AU_index;
538           }
539
540           /* keep track of the higest AU_index */
541           if (rtpmp4gdepay->max_AU_index != -1
542               && rtpmp4gdepay->max_AU_index <= AU_index) {
543             GST_DEBUG_OBJECT (rtpmp4gdepay, "new interleave group, flushing");
544             /* a new interleave group started, flush */
545             gst_rtp_mp4g_depay_flush_queue (rtpmp4gdepay);
546           }
547           rtpmp4gdepay->prev_rtptime = rtptime;
548         } else {
549           AU_index_delta =
550               gst_bs_parse_read (&bs, rtpmp4gdepay->indexdeltalength);
551           AU_index += AU_index_delta + 1;
552         }
553         /* keep track of highest AU_index */
554         if (rtpmp4gdepay->max_AU_index == -1
555             || AU_index > rtpmp4gdepay->max_AU_index)
556           rtpmp4gdepay->max_AU_index = AU_index;
557
558         /* the presentation time offset, a 2s-complement value, we need this to
559          * calculate the timestamp on the output packet. */
560         if (rtpmp4gdepay->ctsdeltalength > 0) {
561           if (gst_bs_parse_read (&bs, 1))
562             gst_bs_parse_read (&bs, rtpmp4gdepay->ctsdeltalength);
563         }
564         /* the decoding time offset, a 2s-complement value */
565         if (rtpmp4gdepay->dtsdeltalength > 0) {
566           if (gst_bs_parse_read (&bs, 1))
567             gst_bs_parse_read (&bs, rtpmp4gdepay->dtsdeltalength);
568         }
569         /* RAP-flag to indicate that the AU contains a keyframe */
570         if (rtpmp4gdepay->randomaccessindication)
571           gst_bs_parse_read (&bs, 1);
572         /* stream-state */
573         if (rtpmp4gdepay->streamstateindication > 0)
574           gst_bs_parse_read (&bs, rtpmp4gdepay->streamstateindication);
575
576         GST_DEBUG_OBJECT (rtpmp4gdepay, "size %d, index %d, delta %d", AU_size,
577             AU_index, AU_index_delta);
578
579         /* fragmented pakets have the AU_size set to the size of the
580          * unfragmented AU. */
581         if (AU_size > payload_AU_size)
582           AU_size = payload_AU_size;
583
584         /* collect stuff in the adapter, strip header from payload and push in
585          * the adapter */
586         outbuf =
587             gst_rtp_buffer_get_payload_subbuffer (buf, payload_AU, AU_size);
588         gst_adapter_push (rtpmp4gdepay->adapter, outbuf);
589
590         if (M) {
591           guint avail;
592
593           /* packet is complete, flush */
594           avail = gst_adapter_available (rtpmp4gdepay->adapter);
595
596           outbuf = gst_adapter_take_buffer (rtpmp4gdepay->adapter, avail);
597           gst_buffer_set_caps (outbuf, GST_PAD_CAPS (depayload->srcpad));
598
599           /* copy some of the fields we calculated above on the buffer. We also
600            * copy the AU_index so that we can sort the packets in our queue. */
601           GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
602           GST_BUFFER_OFFSET (outbuf) = AU_index;
603
604           /* make sure we don't use the timestamp again for other AUs in this
605            * RTP packet. */
606           timestamp = -1;
607
608           GST_DEBUG_OBJECT (depayload, "pushing buffer of size %d",
609               GST_BUFFER_SIZE (outbuf));
610
611           gst_rtp_mp4g_depay_queue (rtpmp4gdepay, outbuf);
612
613         }
614         payload_AU += AU_size;
615         payload_AU_size -= AU_size;
616       }
617     } else {
618       /* push complete buffer in adapter */
619       outbuf = gst_rtp_buffer_get_payload_subbuffer (buf, 0, payload_len);
620       gst_adapter_push (rtpmp4gdepay->adapter, outbuf);
621
622       /* if this was the last packet of the VOP, create and push a buffer */
623       if (M) {
624         guint avail;
625
626         avail = gst_adapter_available (rtpmp4gdepay->adapter);
627
628         outbuf = gst_adapter_take_buffer (rtpmp4gdepay->adapter, avail);
629
630         GST_DEBUG ("gst_rtp_mp4g_depay_chain: pushing buffer of size %d",
631             GST_BUFFER_SIZE (outbuf));
632
633         return outbuf;
634       }
635     }
636   }
637   return NULL;
638
639   /* ERRORS */
640 short_payload:
641   {
642     GST_ELEMENT_WARNING (rtpmp4gdepay, STREAM, DECODE,
643         ("Packet payload was too short."), (NULL));
644     return NULL;
645   }
646 }
647
648 static GstStateChangeReturn
649 gst_rtp_mp4g_depay_change_state (GstElement * element,
650     GstStateChange transition)
651 {
652   GstRtpMP4GDepay *rtpmp4gdepay;
653   GstStateChangeReturn ret;
654
655   rtpmp4gdepay = GST_RTP_MP4G_DEPAY (element);
656
657   switch (transition) {
658     case GST_STATE_CHANGE_READY_TO_PAUSED:
659       gst_adapter_clear (rtpmp4gdepay->adapter);
660       rtpmp4gdepay->max_AU_index = -1;
661       rtpmp4gdepay->next_AU_index = -1;
662       rtpmp4gdepay->prev_AU_index = -1;
663       rtpmp4gdepay->prev_rtptime = -1;
664       rtpmp4gdepay->last_AU_index = -1;
665       break;
666     default:
667       break;
668   }
669
670   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
671
672   switch (transition) {
673     case GST_STATE_CHANGE_PAUSED_TO_READY:
674       gst_adapter_clear (rtpmp4gdepay->adapter);
675       gst_rtp_mp4g_depay_clear_queue (rtpmp4gdepay);
676       break;
677     default:
678       break;
679   }
680   return ret;
681 }
682
683 gboolean
684 gst_rtp_mp4g_depay_plugin_init (GstPlugin * plugin)
685 {
686   return gst_element_register (plugin, "rtpmp4gdepay",
687       GST_RANK_MARGINAL, GST_TYPE_RTP_MP4G_DEPAY);
688 }