cd05e62051867079b169ea80ba73b3a87f884f48
[platform/upstream/gst-plugins-good.git] / gst / rtp / gstrtph264depay.c
1 /* GStreamer
2  * Copyright (C) <2006> Wim Taymans <wim.taymans@gmail.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <stdio.h>
25 #include <string.h>
26
27 #include <gst/base/gstbitreader.h>
28 #include <gst/rtp/gstrtpbuffer.h>
29 #include <gst/pbutils/pbutils.h>
30 #include <gst/video/video.h>
31 #include "gstrtph264depay.h"
32 #include "gstrtputils.h"
33
34 GST_DEBUG_CATEGORY_STATIC (rtph264depay_debug);
35 #define GST_CAT_DEFAULT (rtph264depay_debug)
36
37 /* This is what we'll default to when downstream hasn't
38  * expressed a restriction or preference via caps */
39 #define DEFAULT_BYTE_STREAM   TRUE
40 #define DEFAULT_ACCESS_UNIT   FALSE
41
42 /* 3 zero bytes syncword */
43 static const guint8 sync_bytes[] = { 0, 0, 0, 1 };
44
45 static GstStaticPadTemplate gst_rtp_h264_depay_src_template =
46     GST_STATIC_PAD_TEMPLATE ("src",
47     GST_PAD_SRC,
48     GST_PAD_ALWAYS,
49     GST_STATIC_CAPS ("video/x-h264, "
50         "stream-format = (string) avc, alignment = (string) au; "
51         "video/x-h264, "
52         "stream-format = (string) byte-stream, alignment = (string) { nal, au }")
53     );
54
55 static GstStaticPadTemplate gst_rtp_h264_depay_sink_template =
56 GST_STATIC_PAD_TEMPLATE ("sink",
57     GST_PAD_SINK,
58     GST_PAD_ALWAYS,
59     GST_STATIC_CAPS ("application/x-rtp, "
60         "media = (string) \"video\", "
61         "clock-rate = (int) 90000, " "encoding-name = (string) \"H264\"")
62         /** optional parameters **/
63     /* "profile-level-id = (string) ANY, " */
64     /* "max-mbps = (string) ANY, " */
65     /* "max-fs = (string) ANY, " */
66     /* "max-cpb = (string) ANY, " */
67     /* "max-dpb = (string) ANY, " */
68     /* "max-br = (string) ANY, " */
69     /* "redundant-pic-cap = (string) { \"0\", \"1\" }, " */
70     /* "sprop-parameter-sets = (string) ANY, " */
71     /* "parameter-add = (string) { \"0\", \"1\" }, " */
72     /* "packetization-mode = (string) { \"0\", \"1\", \"2\" }, " */
73     /* "sprop-interleaving-depth = (string) ANY, " */
74     /* "sprop-deint-buf-req = (string) ANY, " */
75     /* "deint-buf-cap = (string) ANY, " */
76     /* "sprop-init-buf-time = (string) ANY, " */
77     /* "sprop-max-don-diff = (string) ANY, " */
78     /* "max-rcmd-nalu-size = (string) ANY " */
79     );
80
81 #define gst_rtp_h264_depay_parent_class parent_class
82 G_DEFINE_TYPE (GstRtpH264Depay, gst_rtp_h264_depay,
83     GST_TYPE_RTP_BASE_DEPAYLOAD);
84
85 static void gst_rtp_h264_depay_finalize (GObject * object);
86
87 static GstStateChangeReturn gst_rtp_h264_depay_change_state (GstElement *
88     element, GstStateChange transition);
89
90 static GstBuffer *gst_rtp_h264_depay_process (GstRTPBaseDepayload * depayload,
91     GstRTPBuffer * rtp);
92 static gboolean gst_rtp_h264_depay_setcaps (GstRTPBaseDepayload * filter,
93     GstCaps * caps);
94 static gboolean gst_rtp_h264_depay_handle_event (GstRTPBaseDepayload * depay,
95     GstEvent * event);
96
97 static void
98 gst_rtp_h264_depay_class_init (GstRtpH264DepayClass * klass)
99 {
100   GObjectClass *gobject_class;
101   GstElementClass *gstelement_class;
102   GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
103
104   gobject_class = (GObjectClass *) klass;
105   gstelement_class = (GstElementClass *) klass;
106   gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
107
108   gobject_class->finalize = gst_rtp_h264_depay_finalize;
109
110   gst_element_class_add_pad_template (gstelement_class,
111       gst_static_pad_template_get (&gst_rtp_h264_depay_src_template));
112   gst_element_class_add_pad_template (gstelement_class,
113       gst_static_pad_template_get (&gst_rtp_h264_depay_sink_template));
114
115   gst_element_class_set_static_metadata (gstelement_class,
116       "RTP H264 depayloader", "Codec/Depayloader/Network/RTP",
117       "Extracts H264 video from RTP packets (RFC 3984)",
118       "Wim Taymans <wim.taymans@gmail.com>");
119   gstelement_class->change_state = gst_rtp_h264_depay_change_state;
120
121   gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_h264_depay_process;
122   gstrtpbasedepayload_class->set_caps = gst_rtp_h264_depay_setcaps;
123   gstrtpbasedepayload_class->handle_event = gst_rtp_h264_depay_handle_event;
124 }
125
126 static void
127 gst_rtp_h264_depay_init (GstRtpH264Depay * rtph264depay)
128 {
129   rtph264depay->adapter = gst_adapter_new ();
130   rtph264depay->picture_adapter = gst_adapter_new ();
131   rtph264depay->byte_stream = DEFAULT_BYTE_STREAM;
132   rtph264depay->merge = DEFAULT_ACCESS_UNIT;
133   rtph264depay->sps = g_ptr_array_new_with_free_func (
134       (GDestroyNotify) gst_buffer_unref);
135   rtph264depay->pps = g_ptr_array_new_with_free_func (
136       (GDestroyNotify) gst_buffer_unref);
137 }
138
139 static void
140 gst_rtp_h264_depay_reset (GstRtpH264Depay * rtph264depay)
141 {
142   gst_adapter_clear (rtph264depay->adapter);
143   rtph264depay->wait_start = TRUE;
144   gst_adapter_clear (rtph264depay->picture_adapter);
145   rtph264depay->picture_start = FALSE;
146   rtph264depay->last_keyframe = FALSE;
147   rtph264depay->last_ts = 0;
148   rtph264depay->current_fu_type = 0;
149   rtph264depay->new_codec_data = FALSE;
150   g_ptr_array_set_size (rtph264depay->sps, 0);
151   g_ptr_array_set_size (rtph264depay->pps, 0);
152 }
153
154 static void
155 gst_rtp_h264_depay_finalize (GObject * object)
156 {
157   GstRtpH264Depay *rtph264depay;
158
159   rtph264depay = GST_RTP_H264_DEPAY (object);
160
161   if (rtph264depay->codec_data)
162     gst_buffer_unref (rtph264depay->codec_data);
163
164   g_object_unref (rtph264depay->adapter);
165   g_object_unref (rtph264depay->picture_adapter);
166
167   g_ptr_array_free (rtph264depay->sps, TRUE);
168   g_ptr_array_free (rtph264depay->pps, TRUE);
169
170   G_OBJECT_CLASS (parent_class)->finalize (object);
171 }
172
173 static void
174 gst_rtp_h264_depay_negotiate (GstRtpH264Depay * rtph264depay)
175 {
176   GstCaps *caps;
177   gint byte_stream = -1;
178   gint merge = -1;
179
180   caps =
181       gst_pad_get_allowed_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (rtph264depay));
182
183   GST_DEBUG_OBJECT (rtph264depay, "allowed caps: %" GST_PTR_FORMAT, caps);
184
185   if (caps) {
186     if (gst_caps_get_size (caps) > 0) {
187       GstStructure *s = gst_caps_get_structure (caps, 0);
188       const gchar *str = NULL;
189
190       if ((str = gst_structure_get_string (s, "stream-format"))) {
191         if (strcmp (str, "avc") == 0) {
192           byte_stream = FALSE;
193         } else if (strcmp (str, "byte-stream") == 0) {
194           byte_stream = TRUE;
195         } else {
196           GST_DEBUG_OBJECT (rtph264depay, "unknown stream-format: %s", str);
197         }
198       }
199
200       if ((str = gst_structure_get_string (s, "alignment"))) {
201         if (strcmp (str, "au") == 0) {
202           merge = TRUE;
203         } else if (strcmp (str, "nal") == 0) {
204           merge = FALSE;
205         } else {
206           GST_DEBUG_OBJECT (rtph264depay, "unknown alignment: %s", str);
207         }
208       }
209     }
210     gst_caps_unref (caps);
211   }
212
213   if (byte_stream != -1) {
214     GST_DEBUG_OBJECT (rtph264depay, "downstream requires byte-stream %d",
215         byte_stream);
216     rtph264depay->byte_stream = byte_stream;
217   } else {
218     GST_DEBUG_OBJECT (rtph264depay, "defaulting to byte-stream %d",
219         DEFAULT_BYTE_STREAM);
220     rtph264depay->byte_stream = DEFAULT_BYTE_STREAM;
221   }
222   if (merge != -1) {
223     GST_DEBUG_OBJECT (rtph264depay, "downstream requires merge %d", merge);
224     rtph264depay->merge = merge;
225   } else {
226     GST_DEBUG_OBJECT (rtph264depay, "defaulting to merge %d",
227         DEFAULT_ACCESS_UNIT);
228     rtph264depay->merge = DEFAULT_ACCESS_UNIT;
229   }
230 }
231
232 /* Stolen from bad/gst/mpegtsdemux/payloader_parsers.c */
233 /* variable length Exp-Golomb parsing according to H.264 spec 9.1*/
234 static gboolean
235 read_golomb (GstBitReader * br, guint32 * value)
236 {
237   guint8 b, leading_zeros = -1;
238   *value = 1;
239
240   for (b = 0; !b; leading_zeros++) {
241     if (!gst_bit_reader_get_bits_uint8 (br, &b, 1))
242       return FALSE;
243     *value *= 2;
244   }
245
246   *value = (*value >> 1) - 1;
247   if (leading_zeros > 0) {
248     guint32 tmp = 0;
249     if (!gst_bit_reader_get_bits_uint32 (br, &tmp, leading_zeros))
250       return FALSE;
251     *value += tmp;
252   }
253
254   return TRUE;
255 }
256
257 static gboolean
258 parse_sps (GstMapInfo * map, guint32 * sps_id)
259 {
260   GstBitReader br = GST_BIT_READER_INIT (map->data + 4,
261       map->size - 4);
262
263   if (map->size < 5)
264     return FALSE;
265
266   if (!read_golomb (&br, sps_id))
267     return FALSE;
268
269   return TRUE;
270 }
271
272 static gboolean
273 parse_pps (GstMapInfo * map, guint32 * sps_id, guint32 * pps_id)
274 {
275   GstBitReader br = GST_BIT_READER_INIT (map->data + 1,
276       map->size - 1);
277
278   if (map->size < 2)
279     return FALSE;
280
281   if (!read_golomb (&br, pps_id))
282     return FALSE;
283   if (!read_golomb (&br, sps_id))
284     return FALSE;
285
286   return TRUE;
287 }
288
289
290 static gboolean
291 gst_rtp_h264_set_src_caps (GstRtpH264Depay * rtph264depay)
292 {
293   gboolean res;
294   GstCaps *srccaps;
295
296   if (!rtph264depay->byte_stream &&
297       (!rtph264depay->new_codec_data ||
298           rtph264depay->sps->len == 0 || rtph264depay->pps->len == 0))
299     return TRUE;
300
301   srccaps = gst_caps_new_simple ("video/x-h264",
302       "stream-format", G_TYPE_STRING,
303       rtph264depay->byte_stream ? "byte-stream" : "avc",
304       "alignment", G_TYPE_STRING, rtph264depay->merge ? "au" : "nal", NULL);
305
306   if (!rtph264depay->byte_stream) {
307     GstBuffer *codec_data;
308     GstMapInfo map;
309     GstMapInfo nalmap;
310     guint8 *data;
311     guint len;
312     guint new_size;
313     guint i;
314     guchar level = 0;
315     guchar profile_compat = G_MAXUINT8;
316
317     /* start with 7 bytes header */
318     len = 7;
319     /* count sps & pps */
320     for (i = 0; i < rtph264depay->sps->len; i++)
321       len += 2 + gst_buffer_get_size (g_ptr_array_index (rtph264depay->sps, i));
322     for (i = 0; i < rtph264depay->pps->len; i++)
323       len += 2 + gst_buffer_get_size (g_ptr_array_index (rtph264depay->pps, i));
324
325     codec_data = gst_buffer_new_and_alloc (len);
326     g_debug ("alloc_len: %u", len);
327     gst_buffer_map (codec_data, &map, GST_MAP_READWRITE);
328     data = map.data;
329
330     /* 8 bits version == 1 */
331     *data++ = 1;
332
333     /* According to: ISO/IEC 14496-15:2004(E) section 5.2.4.1
334      * The level is the max level of all SPSes
335      * A profile compat bit can only be set if all SPSes include that bit
336      */
337     for (i = 0; i < rtph264depay->sps->len; i++) {
338       gst_buffer_map (g_ptr_array_index (rtph264depay->sps, i), &nalmap,
339           GST_MAP_READ);
340       profile_compat &= nalmap.data[2];
341       level = MAX (level, nalmap.data[3]);
342       gst_buffer_unmap (g_ptr_array_index (rtph264depay->sps, i), &nalmap);
343     }
344
345     /* Assume all SPSes use the same profile, so extract from the first SPS */
346     gst_buffer_map (g_ptr_array_index (rtph264depay->sps, 0), &nalmap,
347         GST_MAP_READ);
348     *data++ = nalmap.data[1];
349     gst_buffer_unmap (g_ptr_array_index (rtph264depay->sps, 0), &nalmap);
350     *data++ = profile_compat;
351     *data++ = level;
352
353     /* 6 bits reserved | 2 bits lengthSizeMinusOn */
354     *data++ = 0xff;
355     /* 3 bits reserved | 5 bits numOfSequenceParameterSets */
356     *data++ = 0xe0 | (rtph264depay->sps->len & 0x1f);
357
358     /* copy all SPS */
359     for (i = 0; i < rtph264depay->sps->len; i++) {
360       gst_buffer_map (g_ptr_array_index (rtph264depay->sps, i), &nalmap,
361           GST_MAP_READ);
362
363       GST_DEBUG_OBJECT (rtph264depay, "copy SPS %d of length %u", i,
364           (guint) nalmap.size);
365       GST_WRITE_UINT16_BE (data, nalmap.size);
366       data += 2;
367       memcpy (data, nalmap.data, nalmap.size);
368       data += nalmap.size;
369       gst_buffer_unmap (g_ptr_array_index (rtph264depay->sps, i), &nalmap);
370     }
371
372     /* 8 bits numOfPictureParameterSets */
373     *data++ = rtph264depay->pps->len;
374     /* copy all PPS */
375     for (i = 0; i < rtph264depay->pps->len; i++) {
376       gst_buffer_map (g_ptr_array_index (rtph264depay->pps, i), &nalmap,
377           GST_MAP_READ);
378
379       GST_DEBUG_OBJECT (rtph264depay, "copy PPS %d of length %u", i,
380           (guint) nalmap.size);
381       GST_WRITE_UINT16_BE (data, nalmap.size);
382       data += 2;
383       memcpy (data, nalmap.data, nalmap.size);
384       data += nalmap.size;
385       gst_buffer_unmap (g_ptr_array_index (rtph264depay->pps, i), &nalmap);
386     }
387
388     new_size = data - map.data;
389     gst_buffer_unmap (codec_data, &map);
390     gst_buffer_set_size (codec_data, new_size);
391
392     gst_caps_set_simple (srccaps,
393         "codec_data", GST_TYPE_BUFFER, codec_data, NULL);
394     gst_buffer_unref (codec_data);
395   }
396
397   /* Set profile a level from SPS */
398   {
399     gint i;
400     GstBuffer *max_level_sps = NULL;
401     gint level = 0;
402     GstMapInfo nalmap;
403
404     /* Get the SPS with the highest level. We assume
405      * all SPS have the same profile */
406     for (i = 0; i < rtph264depay->sps->len; i++) {
407       gst_buffer_map (g_ptr_array_index (rtph264depay->sps, i), &nalmap,
408           GST_MAP_READ);
409       if (level == 0 || level < nalmap.data[3]) {
410         max_level_sps = g_ptr_array_index (rtph264depay->sps, i);
411         level = nalmap.data[3];
412       }
413       gst_buffer_unmap (g_ptr_array_index (rtph264depay->sps, i), &nalmap);
414     }
415
416     if (max_level_sps) {
417       gst_buffer_map (max_level_sps, &nalmap, GST_MAP_READ);
418       gst_codec_utils_h264_caps_set_level_and_profile (srccaps, nalmap.data + 1,
419           nalmap.size - 1);
420       gst_buffer_unmap (max_level_sps, &nalmap);
421     }
422   }
423
424
425   if (gst_pad_has_current_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (rtph264depay))) {
426     GstCaps *old_caps =
427         gst_pad_get_current_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (rtph264depay));
428
429     /* Only update the caps if they are not equal. For
430      * AVC we don't update caps if only the codec_data
431      * changes. This is the same behaviour as in h264parse
432      */
433     if (rtph264depay->byte_stream) {
434       if (!gst_caps_is_equal (srccaps, old_caps))
435         res =
436             gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (rtph264depay),
437             srccaps);
438       else
439         res = TRUE;
440     } else {
441       GstCaps *tmp_caps = gst_caps_copy (srccaps);
442       GstStructure *old_s, *tmp_s;
443
444       old_s = gst_caps_get_structure (old_caps, 0);
445       tmp_s = gst_caps_get_structure (tmp_caps, 0);
446       if (gst_structure_has_field (old_s, "codec_data"))
447         gst_structure_set_value (tmp_s, "codec_data",
448             gst_structure_get_value (old_s, "codec_data"));
449
450       if (!gst_caps_is_equal (old_caps, tmp_caps))
451         res =
452             gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (rtph264depay),
453             srccaps);
454       else
455         res = TRUE;
456
457       gst_caps_unref (tmp_caps);
458     }
459   } else {
460     res =
461         gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (rtph264depay),
462         srccaps);
463   }
464
465   gst_caps_unref (srccaps);
466
467   /* Insert SPS and PPS into the stream on next opportunity */
468   if (rtph264depay->sps->len > 0 || rtph264depay->pps->len > 0) {
469     gint i;
470     GstBuffer *codec_data;
471     GstMapInfo map;
472     guint8 *data;
473     guint len = 0;
474
475     for (i = 0; i < rtph264depay->sps->len; i++) {
476       len += 4 + gst_buffer_get_size (g_ptr_array_index (rtph264depay->sps, i));
477     }
478
479     for (i = 0; i < rtph264depay->pps->len; i++) {
480       len += 4 + gst_buffer_get_size (g_ptr_array_index (rtph264depay->pps, i));
481     }
482
483     codec_data = gst_buffer_new_and_alloc (len);
484     gst_buffer_map (codec_data, &map, GST_MAP_WRITE);
485     data = map.data;
486
487     for (i = 0; i < rtph264depay->sps->len; i++) {
488       GstBuffer *sps_buf = g_ptr_array_index (rtph264depay->sps, i);
489       guint sps_size = gst_buffer_get_size (sps_buf);
490
491       if (rtph264depay->byte_stream)
492         memcpy (data, sync_bytes, sizeof (sync_bytes));
493       else
494         GST_WRITE_UINT32_BE (data, sps_size);
495       gst_buffer_extract (sps_buf, 0, data + 4, -1);
496       data += 4 + sps_size;
497     }
498
499     for (i = 0; i < rtph264depay->pps->len; i++) {
500       GstBuffer *pps_buf = g_ptr_array_index (rtph264depay->pps, i);
501       guint pps_size = gst_buffer_get_size (pps_buf);
502
503       if (rtph264depay->byte_stream)
504         memcpy (data, sync_bytes, sizeof (sync_bytes));
505       else
506         GST_WRITE_UINT32_BE (data, pps_size);
507       gst_buffer_extract (pps_buf, 0, data + 4, -1);
508       data += 4 + pps_size;
509     }
510
511     gst_buffer_unmap (codec_data, &map);
512     if (rtph264depay->codec_data)
513       gst_buffer_unref (rtph264depay->codec_data);
514     rtph264depay->codec_data = codec_data;
515   }
516
517   if (res)
518     rtph264depay->new_codec_data = FALSE;
519
520   return res;
521 }
522
523 gboolean
524 gst_rtp_h264_add_sps_pps (GstElement * rtph264, GPtrArray * sps_array,
525     GPtrArray * pps_array, GstBuffer * nal)
526 {
527   GstMapInfo map;
528   guchar type;
529   guint i;
530
531   gst_buffer_map (nal, &map, GST_MAP_READ);
532
533   type = map.data[0] & 0x1f;
534
535   if (type == 7) {
536     guint32 sps_id;
537
538     if (!parse_sps (&map, &sps_id)) {
539       GST_WARNING_OBJECT (rtph264, "Invalid SPS,"
540           " can't parse seq_parameter_set_id");
541       goto drop;
542     }
543
544     for (i = 0; i < sps_array->len; i++) {
545       GstBuffer *sps = g_ptr_array_index (sps_array, i);
546       GstMapInfo spsmap;
547       guint32 tmp_sps_id;
548
549       gst_buffer_map (sps, &spsmap, GST_MAP_READ);
550       parse_sps (&spsmap, &tmp_sps_id);
551
552       if (sps_id == tmp_sps_id) {
553         if (map.size == spsmap.size &&
554             memcmp (map.data, spsmap.data, spsmap.size) == 0) {
555           GST_LOG_OBJECT (rtph264, "Unchanged SPS %u, not updating", sps_id);
556           gst_buffer_unmap (sps, &spsmap);
557           goto drop;
558         } else {
559           gst_buffer_unmap (sps, &spsmap);
560           g_ptr_array_remove_index_fast (sps_array, i);
561           g_ptr_array_add (sps_array, nal);
562           GST_LOG_OBJECT (rtph264, "Modified SPS %u, replacing", sps_id);
563           goto done;
564         }
565       }
566       gst_buffer_unmap (sps, &spsmap);
567     }
568     GST_LOG_OBJECT (rtph264, "Adding new SPS %u", sps_id);
569     g_ptr_array_add (sps_array, nal);
570   } else if (type == 8) {
571     guint32 sps_id;
572     guint32 pps_id;
573
574     if (!parse_pps (&map, &sps_id, &pps_id)) {
575       GST_WARNING_OBJECT (rtph264, "Invalid PPS,"
576           " can't parse seq_parameter_set_id or pic_parameter_set_id");
577       goto drop;
578     }
579
580     for (i = 0; i < pps_array->len; i++) {
581       GstBuffer *pps = g_ptr_array_index (pps_array, i);
582       GstMapInfo ppsmap;
583       guint32 tmp_sps_id;
584       guint32 tmp_pps_id;
585
586
587       gst_buffer_map (pps, &ppsmap, GST_MAP_READ);
588       parse_pps (&ppsmap, &tmp_sps_id, &tmp_pps_id);
589
590       if (pps_id == tmp_pps_id) {
591         if (map.size == ppsmap.size &&
592             memcmp (map.data, ppsmap.data, ppsmap.size) == 0) {
593           GST_LOG_OBJECT (rtph264, "Unchanged PPS %u:%u, not updating", sps_id,
594               pps_id);
595           gst_buffer_unmap (pps, &ppsmap);
596           goto drop;
597         } else {
598           gst_buffer_unmap (pps, &ppsmap);
599           g_ptr_array_remove_index_fast (pps_array, i);
600           g_ptr_array_add (pps_array, nal);
601           GST_LOG_OBJECT (rtph264, "Modified PPS %u:%u, replacing",
602               sps_id, pps_id);
603           goto done;
604         }
605       }
606       gst_buffer_unmap (pps, &ppsmap);
607     }
608     GST_LOG_OBJECT (rtph264, "Adding new PPS %u:%i", sps_id, pps_id);
609     g_ptr_array_add (pps_array, nal);
610   } else {
611     goto drop;
612   }
613
614 done:
615   gst_buffer_unmap (nal, &map);
616
617   return TRUE;
618
619 drop:
620   gst_buffer_unmap (nal, &map);
621   gst_buffer_unref (nal);
622
623   return FALSE;
624 }
625
626
627 static void
628 gst_rtp_h264_depay_add_sps_pps (GstRtpH264Depay * rtph264depay, GstBuffer * nal)
629 {
630   if (gst_rtp_h264_add_sps_pps (GST_ELEMENT (rtph264depay),
631           rtph264depay->sps, rtph264depay->pps, nal))
632     rtph264depay->new_codec_data = TRUE;
633 }
634
635 static gboolean
636 gst_rtp_h264_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
637 {
638   gint clock_rate;
639   GstStructure *structure = gst_caps_get_structure (caps, 0);
640   GstRtpH264Depay *rtph264depay;
641   const gchar *ps;
642   GstBuffer *codec_data;
643   GstMapInfo map;
644   guint8 *ptr;
645
646   rtph264depay = GST_RTP_H264_DEPAY (depayload);
647
648   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
649     clock_rate = 90000;
650   depayload->clock_rate = clock_rate;
651
652   /* Base64 encoded, comma separated config NALs */
653   ps = gst_structure_get_string (structure, "sprop-parameter-sets");
654
655   /* negotiate with downstream w.r.t. output format and alignment */
656   gst_rtp_h264_depay_negotiate (rtph264depay);
657
658   if (rtph264depay->byte_stream && ps != NULL) {
659     /* for bytestream we only need the parameter sets but we don't error out
660      * when they are not there, we assume they are in the stream. */
661     gchar **params;
662     guint len, total;
663     gint i;
664
665     params = g_strsplit (ps, ",", 0);
666
667     /* count total number of bytes in base64. Also include the sync bytes in
668      * front of the params. */
669     len = 0;
670     for (i = 0; params[i]; i++) {
671       len += strlen (params[i]);
672       len += sizeof (sync_bytes);
673     }
674     /* we seriously overshoot the length, but it's fine. */
675     codec_data = gst_buffer_new_and_alloc (len);
676
677     gst_buffer_map (codec_data, &map, GST_MAP_WRITE);
678     ptr = map.data;
679     total = 0;
680     for (i = 0; params[i]; i++) {
681       guint save = 0;
682       gint state = 0;
683
684       GST_DEBUG_OBJECT (depayload, "decoding param %d (%s)", i, params[i]);
685       memcpy (ptr, sync_bytes, sizeof (sync_bytes));
686       ptr += sizeof (sync_bytes);
687       len =
688           g_base64_decode_step (params[i], strlen (params[i]), ptr, &state,
689           &save);
690       GST_DEBUG_OBJECT (depayload, "decoded %d bytes", len);
691       total += len + sizeof (sync_bytes);
692       ptr += len;
693     }
694     gst_buffer_unmap (codec_data, &map);
695     gst_buffer_resize (codec_data, 0, total);
696     g_strfreev (params);
697
698     /* keep the codec_data, we need to send it as the first buffer. We cannot
699      * push it in the adapter because the adapter might be flushed on discont.
700      */
701     if (rtph264depay->codec_data)
702       gst_buffer_unref (rtph264depay->codec_data);
703     rtph264depay->codec_data = codec_data;
704   } else if (!rtph264depay->byte_stream) {
705     gchar **params;
706     gint i;
707
708     if (ps == NULL)
709       goto incomplete_caps;
710
711     params = g_strsplit (ps, ",", 0);
712
713     GST_DEBUG_OBJECT (depayload, "we have %d params", g_strv_length (params));
714
715     /* start with 7 bytes header */
716     for (i = 0; params[i]; i++) {
717       GstBuffer *nal;
718       GstMapInfo nalmap;
719       gsize nal_len;
720       guint save = 0;
721       gint state = 0;
722
723       nal_len = strlen (params[i]);
724       nal = gst_buffer_new_and_alloc (nal_len);
725       gst_buffer_map (nal, &nalmap, GST_MAP_READWRITE);
726
727       nal_len =
728           g_base64_decode_step (params[i], nal_len, nalmap.data, &state, &save);
729
730       GST_DEBUG_OBJECT (depayload, "adding param %d as %s", i,
731           ((nalmap.data[0] & 0x1f) == 7) ? "SPS" : "PPS");
732
733       gst_buffer_unmap (nal, &nalmap);
734       gst_buffer_set_size (nal, nal_len);
735
736       gst_rtp_h264_depay_add_sps_pps (rtph264depay, nal);
737     }
738     g_strfreev (params);
739
740     if (rtph264depay->sps->len == 0 || rtph264depay->pps->len == 0)
741       goto incomplete_caps;
742   }
743
744   return gst_rtp_h264_set_src_caps (rtph264depay);
745
746   /* ERRORS */
747 incomplete_caps:
748   {
749     GST_DEBUG_OBJECT (depayload, "we have incomplete caps,"
750         " doing setcaps later");
751     return TRUE;
752   }
753 }
754
755 static GstBuffer *
756 gst_rtp_h264_complete_au (GstRtpH264Depay * rtph264depay,
757     GstClockTime * out_timestamp, gboolean * out_keyframe)
758 {
759   guint outsize;
760   GstBuffer *outbuf;
761
762   /* we had a picture in the adapter and we completed it */
763   GST_DEBUG_OBJECT (rtph264depay, "taking completed AU");
764   outsize = gst_adapter_available (rtph264depay->picture_adapter);
765   outbuf = gst_adapter_take_buffer (rtph264depay->picture_adapter, outsize);
766
767   *out_timestamp = rtph264depay->last_ts;
768   *out_keyframe = rtph264depay->last_keyframe;
769
770   rtph264depay->last_keyframe = FALSE;
771   rtph264depay->picture_start = FALSE;
772
773   return outbuf;
774 }
775
776 /* SPS/PPS/IDR considered key, all others DELTA;
777  * so downstream waiting for keyframe can pick up at SPS/PPS/IDR */
778 #define NAL_TYPE_IS_KEY(nt) (((nt) == 5) || ((nt) == 7) || ((nt) == 8))
779
780 static GstBuffer *
781 gst_rtp_h264_depay_handle_nal (GstRtpH264Depay * rtph264depay, GstBuffer * nal,
782     GstClockTime in_timestamp, gboolean marker)
783 {
784   GstRTPBaseDepayload *depayload = GST_RTP_BASE_DEPAYLOAD (rtph264depay);
785   gint nal_type;
786   GstMapInfo map;
787   GstBuffer *outbuf = NULL;
788   GstClockTime out_timestamp;
789   gboolean keyframe, out_keyframe;
790
791   gst_buffer_map (nal, &map, GST_MAP_READ);
792   if (G_UNLIKELY (map.size < 5))
793     goto short_nal;
794
795   nal_type = map.data[4] & 0x1f;
796   GST_DEBUG_OBJECT (rtph264depay, "handle NAL type %d", nal_type);
797
798   keyframe = NAL_TYPE_IS_KEY (nal_type);
799
800   out_keyframe = keyframe;
801   out_timestamp = in_timestamp;
802
803   if (!rtph264depay->byte_stream) {
804     if (nal_type == 7 || nal_type == 8) {
805       gst_rtp_h264_depay_add_sps_pps (rtph264depay,
806           gst_buffer_copy_region (nal, GST_BUFFER_COPY_ALL,
807               4, gst_buffer_get_size (nal) - 4));
808       gst_buffer_unmap (nal, &map);
809       gst_buffer_unref (nal);
810       return NULL;
811     } else if (rtph264depay->sps->len == 0 || rtph264depay->pps->len == 0) {
812       /* Down push down any buffer in non-bytestream mode if the SPS/PPS haven't
813        * go through yet
814        */
815       gst_pad_push_event (GST_RTP_BASE_DEPAYLOAD_SINKPAD (depayload),
816           gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM,
817               gst_structure_new ("GstForceKeyUnit",
818                   "all-headers", G_TYPE_BOOLEAN, TRUE, NULL)));
819       gst_buffer_unmap (nal, &map);
820       gst_buffer_unref (nal);
821       return NULL;
822     }
823
824     if (rtph264depay->new_codec_data &&
825         rtph264depay->sps->len > 0 && rtph264depay->pps->len > 0)
826       gst_rtp_h264_set_src_caps (rtph264depay);
827   }
828
829
830   if (rtph264depay->merge) {
831     gboolean start = FALSE, complete = FALSE;
832
833     /* marker bit isn't mandatory so in the following code we try to guess
834      * an AU boundary by detecting a new picture start */
835     if (!marker) {
836       /* consider a coded slices (IDR or not) to start a picture,
837        * (so ending the previous one) if first_mb_in_slice == 0
838        * (non-0 is part of previous one) */
839       /* NOTE this is not entirely according to Access Unit specs in 7.4.1.2.4,
840        * but in practice it works in sane cases, needs not much parsing,
841        * and also works with broken frame_num in NAL (where spec-wise would fail) */
842       /* FIXME: this code isn't correct for interlaced content as AUs should be
843        * constructed with pairs of fields and the guess here will just push out
844        * AUs with a single field in it */
845       if (nal_type == 1 || nal_type == 2 || nal_type == 5) {
846         /* we have a picture start */
847         start = TRUE;
848         if (map.data[5] & 0x80) {
849           /* first_mb_in_slice == 0 completes a picture */
850           complete = TRUE;
851         }
852       } else if (nal_type >= 6 && nal_type <= 9) {
853         /* SEI, SPS, PPS, AU terminate picture */
854         complete = TRUE;
855       }
856       GST_DEBUG_OBJECT (depayload, "start %d, complete %d", start, complete);
857
858       if (complete && rtph264depay->picture_start)
859         outbuf = gst_rtp_h264_complete_au (rtph264depay, &out_timestamp,
860             &out_keyframe);
861     }
862     /* add to adapter */
863     gst_buffer_unmap (nal, &map);
864
865     GST_DEBUG_OBJECT (depayload, "adding NAL to picture adapter");
866     gst_adapter_push (rtph264depay->picture_adapter, nal);
867     rtph264depay->last_ts = in_timestamp;
868     rtph264depay->last_keyframe |= keyframe;
869     rtph264depay->picture_start |= start;
870
871     if (marker)
872       outbuf = gst_rtp_h264_complete_au (rtph264depay, &out_timestamp,
873           &out_keyframe);
874   } else {
875     /* no merge, output is input nal */
876     GST_DEBUG_OBJECT (depayload, "using NAL as output");
877     outbuf = nal;
878     gst_buffer_unmap (nal, &map);
879   }
880
881   if (outbuf) {
882     /* prepend codec_data */
883     if (rtph264depay->codec_data) {
884       GST_DEBUG_OBJECT (depayload, "prepending codec_data");
885       gst_rtp_copy_meta (GST_ELEMENT_CAST (rtph264depay),
886           rtph264depay->codec_data, outbuf,
887           g_quark_from_static_string (GST_META_TAG_VIDEO_STR));
888       outbuf = gst_buffer_append (rtph264depay->codec_data, outbuf);
889       rtph264depay->codec_data = NULL;
890       out_keyframe = TRUE;
891     }
892     outbuf = gst_buffer_make_writable (outbuf);
893
894     gst_rtp_drop_meta (GST_ELEMENT_CAST (rtph264depay), outbuf,
895         g_quark_from_static_string (GST_META_TAG_VIDEO_STR));
896
897     GST_BUFFER_PTS (outbuf) = out_timestamp;
898
899     if (out_keyframe)
900       GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
901     else
902       GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
903   }
904
905   return outbuf;
906
907   /* ERRORS */
908 short_nal:
909   {
910     GST_WARNING_OBJECT (depayload, "dropping short NAL");
911     gst_buffer_unmap (nal, &map);
912     gst_buffer_unref (nal);
913     return NULL;
914   }
915 }
916
917 static GstBuffer *
918 gst_rtp_h264_push_fragmentation_unit (GstRtpH264Depay * rtph264depay,
919     gboolean send)
920 {
921   guint outsize;
922   GstMapInfo map;
923   GstBuffer *outbuf;
924
925   outsize = gst_adapter_available (rtph264depay->adapter);
926   outbuf = gst_adapter_take_buffer (rtph264depay->adapter, outsize);
927
928   gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
929   GST_DEBUG_OBJECT (rtph264depay, "output %d bytes", outsize);
930
931   if (rtph264depay->byte_stream) {
932     memcpy (map.data, sync_bytes, sizeof (sync_bytes));
933   } else {
934     outsize -= 4;
935     map.data[0] = (outsize >> 24);
936     map.data[1] = (outsize >> 16);
937     map.data[2] = (outsize >> 8);
938     map.data[3] = (outsize);
939   }
940   gst_buffer_unmap (outbuf, &map);
941
942   rtph264depay->current_fu_type = 0;
943
944   outbuf = gst_rtp_h264_depay_handle_nal (rtph264depay, outbuf,
945       rtph264depay->fu_timestamp, rtph264depay->fu_marker);
946
947   if (send && outbuf) {
948     gst_rtp_base_depayload_push (GST_RTP_BASE_DEPAYLOAD (rtph264depay), outbuf);
949     outbuf = NULL;
950   }
951   return outbuf;
952 }
953
954 static GstBuffer *
955 gst_rtp_h264_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
956 {
957   GstRtpH264Depay *rtph264depay;
958   GstBuffer *buf;
959   GstBuffer *outbuf = NULL;
960   guint8 nal_unit_type;
961
962   rtph264depay = GST_RTP_H264_DEPAY (depayload);
963
964   /* flush remaining data on discont */
965   if (GST_BUFFER_IS_DISCONT (rtp->buffer)) {
966     gst_adapter_clear (rtph264depay->adapter);
967     rtph264depay->wait_start = TRUE;
968     rtph264depay->current_fu_type = 0;
969   }
970
971   {
972     gint payload_len;
973     guint8 *payload;
974     guint header_len;
975     guint8 nal_ref_idc;
976     GstMapInfo map;
977     guint outsize, nalu_size;
978     GstClockTime timestamp;
979     gboolean marker;
980
981     timestamp = GST_BUFFER_PTS (rtp->buffer);
982
983     payload_len = gst_rtp_buffer_get_payload_len (rtp);
984     payload = gst_rtp_buffer_get_payload (rtp);
985     buf = gst_rtp_buffer_get_payload_buffer (rtp);
986     marker = gst_rtp_buffer_get_marker (rtp);
987
988     GST_DEBUG_OBJECT (rtph264depay, "receiving %d bytes", payload_len);
989
990     if (payload_len == 0)
991       goto empty_packet;
992
993     /* +---------------+
994      * |0|1|2|3|4|5|6|7|
995      * +-+-+-+-+-+-+-+-+
996      * |F|NRI|  Type   |
997      * +---------------+
998      *
999      * F must be 0.
1000      */
1001     nal_ref_idc = (payload[0] & 0x60) >> 5;
1002     nal_unit_type = payload[0] & 0x1f;
1003
1004     /* at least one byte header with type */
1005     header_len = 1;
1006
1007     GST_DEBUG_OBJECT (rtph264depay, "NRI %d, Type %d", nal_ref_idc,
1008         nal_unit_type);
1009
1010     /* If FU unit was being processed, but the current nal is of a different
1011      * type.  Assume that the remote payloader is buggy (didn't set the end bit
1012      * when the FU ended) and send out what we gathered thusfar */
1013     if (G_UNLIKELY (rtph264depay->current_fu_type != 0 &&
1014             nal_unit_type != rtph264depay->current_fu_type))
1015       gst_rtp_h264_push_fragmentation_unit (rtph264depay, TRUE);
1016
1017     switch (nal_unit_type) {
1018       case 0:
1019       case 30:
1020       case 31:
1021         /* undefined */
1022         goto undefined_type;
1023       case 25:
1024         /* STAP-B    Single-time aggregation packet     5.7.1 */
1025         /* 2 byte extra header for DON */
1026         header_len += 2;
1027         /* fallthrough */
1028       case 24:
1029       {
1030         /* strip headers */
1031         payload += header_len;
1032         payload_len -= header_len;
1033
1034         rtph264depay->wait_start = FALSE;
1035
1036
1037         /* STAP-A    Single-time aggregation packet     5.7.1 */
1038         while (payload_len > 2) {
1039           /*                      1          
1040            *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 
1041            * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1042            * |         NALU Size             |
1043            * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1044            */
1045           nalu_size = (payload[0] << 8) | payload[1];
1046
1047           /* dont include nalu_size */
1048           if (nalu_size > (payload_len - 2))
1049             nalu_size = payload_len - 2;
1050
1051           outsize = nalu_size + sizeof (sync_bytes);
1052           outbuf = gst_buffer_new_and_alloc (outsize);
1053
1054           gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
1055           if (rtph264depay->byte_stream) {
1056             memcpy (map.data, sync_bytes, sizeof (sync_bytes));
1057           } else {
1058             map.data[0] = map.data[1] = 0;
1059             map.data[2] = payload[0];
1060             map.data[3] = payload[1];
1061           }
1062
1063           /* strip NALU size */
1064           payload += 2;
1065           payload_len -= 2;
1066
1067           memcpy (map.data + sizeof (sync_bytes), payload, nalu_size);
1068           gst_buffer_unmap (outbuf, &map);
1069
1070           gst_rtp_copy_meta (GST_ELEMENT_CAST (rtph264depay), outbuf, buf,
1071               g_quark_from_static_string (GST_META_TAG_VIDEO_STR));
1072
1073           outbuf =
1074               gst_rtp_h264_depay_handle_nal (rtph264depay, outbuf, timestamp,
1075               marker);
1076           if (outbuf)
1077             gst_adapter_push (rtph264depay->adapter, outbuf);
1078
1079           payload += nalu_size;
1080           payload_len -= nalu_size;
1081         }
1082
1083         outsize = gst_adapter_available (rtph264depay->adapter);
1084         if (outsize > 0) {
1085           outbuf = gst_adapter_take_buffer (rtph264depay->adapter, outsize);
1086           outbuf =
1087               gst_rtp_h264_depay_handle_nal (rtph264depay, outbuf, timestamp,
1088               marker);
1089         }
1090         break;
1091       }
1092       case 26:
1093         /* MTAP16    Multi-time aggregation packet      5.7.2 */
1094         // header_len = 5;
1095         /* fallthrough, not implemented */
1096       case 27:
1097         /* MTAP24    Multi-time aggregation packet      5.7.2 */
1098         // header_len = 6;
1099         goto not_implemented;
1100         break;
1101       case 28:
1102       case 29:
1103       {
1104         /* FU-A      Fragmentation unit                 5.8 */
1105         /* FU-B      Fragmentation unit                 5.8 */
1106         gboolean S, E;
1107
1108         /* +---------------+
1109          * |0|1|2|3|4|5|6|7|
1110          * +-+-+-+-+-+-+-+-+
1111          * |S|E|R|  Type   |
1112          * +---------------+
1113          *
1114          * R is reserved and always 0
1115          */
1116         S = (payload[1] & 0x80) == 0x80;
1117         E = (payload[1] & 0x40) == 0x40;
1118
1119         GST_DEBUG_OBJECT (rtph264depay, "S %d, E %d", S, E);
1120
1121         if (rtph264depay->wait_start && !S)
1122           goto waiting_start;
1123
1124         if (S) {
1125           /* NAL unit starts here */
1126           guint8 nal_header;
1127
1128           /* If a new FU unit started, while still processing an older one.
1129            * Assume that the remote payloader is buggy (doesn't set the end
1130            * bit) and send out what we've gathered thusfar */
1131           if (G_UNLIKELY (rtph264depay->current_fu_type != 0))
1132             gst_rtp_h264_push_fragmentation_unit (rtph264depay, TRUE);
1133
1134           rtph264depay->current_fu_type = nal_unit_type;
1135           rtph264depay->fu_timestamp = timestamp;
1136
1137           rtph264depay->wait_start = FALSE;
1138
1139           /* reconstruct NAL header */
1140           nal_header = (payload[0] & 0xe0) | (payload[1] & 0x1f);
1141
1142           /* strip type header, keep FU header, we'll reuse it to reconstruct
1143            * the NAL header. */
1144           payload += 1;
1145           payload_len -= 1;
1146
1147           nalu_size = payload_len;
1148           outsize = nalu_size + sizeof (sync_bytes);
1149           outbuf = gst_buffer_new_and_alloc (outsize);
1150
1151           gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
1152           memcpy (map.data + sizeof (sync_bytes), payload, nalu_size);
1153           map.data[sizeof (sync_bytes)] = nal_header;
1154           gst_buffer_unmap (outbuf, &map);
1155
1156           gst_rtp_copy_meta (GST_ELEMENT_CAST (rtph264depay), outbuf, buf,
1157               g_quark_from_static_string (GST_META_TAG_VIDEO_STR));
1158
1159           GST_DEBUG_OBJECT (rtph264depay, "queueing %d bytes", outsize);
1160
1161           /* and assemble in the adapter */
1162           gst_adapter_push (rtph264depay->adapter, outbuf);
1163         } else {
1164           /* strip off FU indicator and FU header bytes */
1165           payload += 2;
1166           payload_len -= 2;
1167
1168           outsize = payload_len;
1169           outbuf = gst_buffer_new_and_alloc (outsize);
1170           gst_buffer_fill (outbuf, 0, payload, outsize);
1171
1172           gst_rtp_copy_meta (GST_ELEMENT_CAST (rtph264depay), outbuf, buf,
1173               g_quark_from_static_string (GST_META_TAG_VIDEO_STR));
1174
1175           GST_DEBUG_OBJECT (rtph264depay, "queueing %d bytes", outsize);
1176
1177           /* and assemble in the adapter */
1178           gst_adapter_push (rtph264depay->adapter, outbuf);
1179         }
1180
1181         outbuf = NULL;
1182         rtph264depay->fu_marker = marker;
1183
1184         /* if NAL unit ends, flush the adapter */
1185         if (E)
1186           outbuf = gst_rtp_h264_push_fragmentation_unit (rtph264depay, FALSE);
1187         break;
1188       }
1189       default:
1190       {
1191         rtph264depay->wait_start = FALSE;
1192
1193         /* 1-23   NAL unit  Single NAL unit packet per H.264   5.6 */
1194         /* the entire payload is the output buffer */
1195         nalu_size = payload_len;
1196         outsize = nalu_size + sizeof (sync_bytes);
1197         outbuf = gst_buffer_new_and_alloc (outsize);
1198
1199         gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
1200         if (rtph264depay->byte_stream) {
1201           memcpy (map.data, sync_bytes, sizeof (sync_bytes));
1202         } else {
1203           map.data[0] = map.data[1] = 0;
1204           map.data[2] = nalu_size >> 8;
1205           map.data[3] = nalu_size & 0xff;
1206         }
1207         memcpy (map.data + sizeof (sync_bytes), payload, nalu_size);
1208         gst_buffer_unmap (outbuf, &map);
1209
1210         gst_rtp_copy_meta (GST_ELEMENT_CAST (rtph264depay), outbuf, buf,
1211             g_quark_from_static_string (GST_META_TAG_VIDEO_STR));
1212
1213         outbuf = gst_rtp_h264_depay_handle_nal (rtph264depay, outbuf, timestamp,
1214             marker);
1215         break;
1216       }
1217     }
1218   }
1219
1220   gst_buffer_unref (buf);
1221
1222   return outbuf;
1223
1224   /* ERRORS */
1225 empty_packet:
1226   {
1227     GST_DEBUG_OBJECT (rtph264depay, "empty packet");
1228     gst_buffer_unref (buf);
1229     return NULL;
1230   }
1231 undefined_type:
1232   {
1233     GST_ELEMENT_WARNING (rtph264depay, STREAM, DECODE,
1234         (NULL), ("Undefined packet type"));
1235     gst_buffer_unref (buf);
1236     return NULL;
1237   }
1238 waiting_start:
1239   {
1240     GST_DEBUG_OBJECT (rtph264depay, "waiting for start");
1241     gst_buffer_unref (buf);
1242     return NULL;
1243   }
1244 not_implemented:
1245   {
1246     GST_ELEMENT_ERROR (rtph264depay, STREAM, FORMAT,
1247         (NULL), ("NAL unit type %d not supported yet", nal_unit_type));
1248     gst_buffer_unref (buf);
1249     return NULL;
1250   }
1251 }
1252
1253 static gboolean
1254 gst_rtp_h264_depay_handle_event (GstRTPBaseDepayload * depay, GstEvent * event)
1255 {
1256   GstRtpH264Depay *rtph264depay;
1257
1258   rtph264depay = GST_RTP_H264_DEPAY (depay);
1259
1260   switch (GST_EVENT_TYPE (event)) {
1261     case GST_EVENT_FLUSH_STOP:
1262       gst_rtp_h264_depay_reset (rtph264depay);
1263       break;
1264     default:
1265       break;
1266   }
1267
1268   return
1269       GST_RTP_BASE_DEPAYLOAD_CLASS (parent_class)->handle_event (depay, event);
1270 }
1271
1272 static GstStateChangeReturn
1273 gst_rtp_h264_depay_change_state (GstElement * element,
1274     GstStateChange transition)
1275 {
1276   GstRtpH264Depay *rtph264depay;
1277   GstStateChangeReturn ret;
1278
1279   rtph264depay = GST_RTP_H264_DEPAY (element);
1280
1281   switch (transition) {
1282     case GST_STATE_CHANGE_NULL_TO_READY:
1283       break;
1284     case GST_STATE_CHANGE_READY_TO_PAUSED:
1285       gst_rtp_h264_depay_reset (rtph264depay);
1286       break;
1287     default:
1288       break;
1289   }
1290
1291   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1292
1293   switch (transition) {
1294     case GST_STATE_CHANGE_READY_TO_NULL:
1295       break;
1296     default:
1297       break;
1298   }
1299   return ret;
1300 }
1301
1302 gboolean
1303 gst_rtp_h264_depay_plugin_init (GstPlugin * plugin)
1304 {
1305   GST_DEBUG_CATEGORY_INIT (rtph264depay_debug, "rtph264depay", 0,
1306       "H264 Video RTP Depayloader");
1307
1308   return gst_element_register (plugin, "rtph264depay",
1309       GST_RANK_SECONDARY, GST_TYPE_RTP_H264_DEPAY);
1310 }