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