rtph264depay : fix mem leak
[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   GstCaps *old_caps;
296
297   if (!rtph264depay->byte_stream &&
298       (!rtph264depay->new_codec_data ||
299           rtph264depay->sps->len == 0 || rtph264depay->pps->len == 0))
300     return TRUE;
301
302   srccaps = gst_caps_new_simple ("video/x-h264",
303       "stream-format", G_TYPE_STRING,
304       rtph264depay->byte_stream ? "byte-stream" : "avc",
305       "alignment", G_TYPE_STRING, rtph264depay->merge ? "au" : "nal", NULL);
306
307   if (!rtph264depay->byte_stream) {
308     GstBuffer *codec_data;
309     GstMapInfo map;
310     GstMapInfo nalmap;
311     guint8 *data;
312     guint len;
313     guint new_size;
314     guint i;
315     guchar level = 0;
316     guchar profile_compat = G_MAXUINT8;
317
318     /* start with 7 bytes header */
319     len = 7;
320     /* count sps & pps */
321     for (i = 0; i < rtph264depay->sps->len; i++)
322       len += 2 + gst_buffer_get_size (g_ptr_array_index (rtph264depay->sps, i));
323     for (i = 0; i < rtph264depay->pps->len; i++)
324       len += 2 + gst_buffer_get_size (g_ptr_array_index (rtph264depay->pps, i));
325
326     codec_data = gst_buffer_new_and_alloc (len);
327     g_debug ("alloc_len: %u", len);
328     gst_buffer_map (codec_data, &map, GST_MAP_READWRITE);
329     data = map.data;
330
331     /* 8 bits version == 1 */
332     *data++ = 1;
333
334     /* According to: ISO/IEC 14496-15:2004(E) section 5.2.4.1
335      * The level is the max level of all SPSes
336      * A profile compat bit can only be set if all SPSes include that bit
337      */
338     for (i = 0; i < rtph264depay->sps->len; i++) {
339       gst_buffer_map (g_ptr_array_index (rtph264depay->sps, i), &nalmap,
340           GST_MAP_READ);
341       profile_compat &= nalmap.data[2];
342       level = MAX (level, nalmap.data[3]);
343       gst_buffer_unmap (g_ptr_array_index (rtph264depay->sps, i), &nalmap);
344     }
345
346     /* Assume all SPSes use the same profile, so extract from the first SPS */
347     gst_buffer_map (g_ptr_array_index (rtph264depay->sps, 0), &nalmap,
348         GST_MAP_READ);
349     *data++ = nalmap.data[1];
350     gst_buffer_unmap (g_ptr_array_index (rtph264depay->sps, 0), &nalmap);
351     *data++ = profile_compat;
352     *data++ = level;
353
354     /* 6 bits reserved | 2 bits lengthSizeMinusOn */
355     *data++ = 0xff;
356     /* 3 bits reserved | 5 bits numOfSequenceParameterSets */
357     *data++ = 0xe0 | (rtph264depay->sps->len & 0x1f);
358
359     /* copy all SPS */
360     for (i = 0; i < rtph264depay->sps->len; i++) {
361       gst_buffer_map (g_ptr_array_index (rtph264depay->sps, i), &nalmap,
362           GST_MAP_READ);
363
364       GST_DEBUG_OBJECT (rtph264depay, "copy SPS %d of length %u", i,
365           (guint) nalmap.size);
366       GST_WRITE_UINT16_BE (data, nalmap.size);
367       data += 2;
368       memcpy (data, nalmap.data, nalmap.size);
369       data += nalmap.size;
370       gst_buffer_unmap (g_ptr_array_index (rtph264depay->sps, i), &nalmap);
371     }
372
373     /* 8 bits numOfPictureParameterSets */
374     *data++ = rtph264depay->pps->len;
375     /* copy all PPS */
376     for (i = 0; i < rtph264depay->pps->len; i++) {
377       gst_buffer_map (g_ptr_array_index (rtph264depay->pps, i), &nalmap,
378           GST_MAP_READ);
379
380       GST_DEBUG_OBJECT (rtph264depay, "copy PPS %d of length %u", i,
381           (guint) nalmap.size);
382       GST_WRITE_UINT16_BE (data, nalmap.size);
383       data += 2;
384       memcpy (data, nalmap.data, nalmap.size);
385       data += nalmap.size;
386       gst_buffer_unmap (g_ptr_array_index (rtph264depay->pps, i), &nalmap);
387     }
388
389     new_size = data - map.data;
390     gst_buffer_unmap (codec_data, &map);
391     gst_buffer_set_size (codec_data, new_size);
392
393     gst_caps_set_simple (srccaps,
394         "codec_data", GST_TYPE_BUFFER, codec_data, NULL);
395     gst_buffer_unref (codec_data);
396   }
397
398   /* Set profile a level from SPS */
399   {
400     gint i;
401     GstBuffer *max_level_sps = NULL;
402     gint level = 0;
403     GstMapInfo nalmap;
404
405     /* Get the SPS with the highest level. We assume
406      * all SPS have the same profile */
407     for (i = 0; i < rtph264depay->sps->len; i++) {
408       gst_buffer_map (g_ptr_array_index (rtph264depay->sps, i), &nalmap,
409           GST_MAP_READ);
410       if (level == 0 || level < nalmap.data[3]) {
411         max_level_sps = g_ptr_array_index (rtph264depay->sps, i);
412         level = nalmap.data[3];
413       }
414       gst_buffer_unmap (g_ptr_array_index (rtph264depay->sps, i), &nalmap);
415     }
416
417     if (max_level_sps) {
418       gst_buffer_map (max_level_sps, &nalmap, GST_MAP_READ);
419       gst_codec_utils_h264_caps_set_level_and_profile (srccaps, nalmap.data + 1,
420           nalmap.size - 1);
421       gst_buffer_unmap (max_level_sps, &nalmap);
422     }
423   }
424
425
426   old_caps =
427       gst_pad_get_current_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (rtph264depay));
428
429   if (old_caps != NULL) {
430     /* Only update the caps if they are not equal. For
431      * AVC we don't update caps if only the codec_data
432      * changes. This is the same behaviour as in h264parse
433      */
434     if (rtph264depay->byte_stream) {
435       if (!gst_caps_is_equal (srccaps, old_caps))
436         res =
437             gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (rtph264depay),
438             srccaps);
439       else
440         res = TRUE;
441     } else {
442       GstCaps *tmp_caps = gst_caps_copy (srccaps);
443       GstStructure *old_s, *tmp_s;
444
445       old_s = gst_caps_get_structure (old_caps, 0);
446       tmp_s = gst_caps_get_structure (tmp_caps, 0);
447       if (gst_structure_has_field (old_s, "codec_data"))
448         gst_structure_set_value (tmp_s, "codec_data",
449             gst_structure_get_value (old_s, "codec_data"));
450
451       if (!gst_caps_is_equal (old_caps, tmp_caps))
452         res =
453             gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (rtph264depay),
454             srccaps);
455       else
456         res = TRUE;
457
458       gst_caps_unref (tmp_caps);
459     }
460     gst_caps_unref (old_caps);
461   } else {
462     res =
463         gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (rtph264depay),
464         srccaps);
465   }
466
467   gst_caps_unref (srccaps);
468
469   /* Insert SPS and PPS into the stream on next opportunity */
470   if (rtph264depay->sps->len > 0 || rtph264depay->pps->len > 0) {
471     gint i;
472     GstBuffer *codec_data;
473     GstMapInfo map;
474     guint8 *data;
475     guint len = 0;
476
477     for (i = 0; i < rtph264depay->sps->len; i++) {
478       len += 4 + gst_buffer_get_size (g_ptr_array_index (rtph264depay->sps, i));
479     }
480
481     for (i = 0; i < rtph264depay->pps->len; i++) {
482       len += 4 + gst_buffer_get_size (g_ptr_array_index (rtph264depay->pps, i));
483     }
484
485     codec_data = gst_buffer_new_and_alloc (len);
486     gst_buffer_map (codec_data, &map, GST_MAP_WRITE);
487     data = map.data;
488
489     for (i = 0; i < rtph264depay->sps->len; i++) {
490       GstBuffer *sps_buf = g_ptr_array_index (rtph264depay->sps, i);
491       guint sps_size = gst_buffer_get_size (sps_buf);
492
493       if (rtph264depay->byte_stream)
494         memcpy (data, sync_bytes, sizeof (sync_bytes));
495       else
496         GST_WRITE_UINT32_BE (data, sps_size);
497       gst_buffer_extract (sps_buf, 0, data + 4, -1);
498       data += 4 + sps_size;
499     }
500
501     for (i = 0; i < rtph264depay->pps->len; i++) {
502       GstBuffer *pps_buf = g_ptr_array_index (rtph264depay->pps, i);
503       guint pps_size = gst_buffer_get_size (pps_buf);
504
505       if (rtph264depay->byte_stream)
506         memcpy (data, sync_bytes, sizeof (sync_bytes));
507       else
508         GST_WRITE_UINT32_BE (data, pps_size);
509       gst_buffer_extract (pps_buf, 0, data + 4, -1);
510       data += 4 + pps_size;
511     }
512
513     gst_buffer_unmap (codec_data, &map);
514     if (rtph264depay->codec_data)
515       gst_buffer_unref (rtph264depay->codec_data);
516     rtph264depay->codec_data = codec_data;
517   }
518
519   if (res)
520     rtph264depay->new_codec_data = FALSE;
521
522   return res;
523 }
524
525 gboolean
526 gst_rtp_h264_add_sps_pps (GstElement * rtph264, GPtrArray * sps_array,
527     GPtrArray * pps_array, GstBuffer * nal)
528 {
529   GstMapInfo map;
530   guchar type;
531   guint i;
532
533   gst_buffer_map (nal, &map, GST_MAP_READ);
534
535   type = map.data[0] & 0x1f;
536
537   if (type == 7) {
538     guint32 sps_id;
539
540     if (!parse_sps (&map, &sps_id)) {
541       GST_WARNING_OBJECT (rtph264, "Invalid SPS,"
542           " can't parse seq_parameter_set_id");
543       goto drop;
544     }
545
546     for (i = 0; i < sps_array->len; i++) {
547       GstBuffer *sps = g_ptr_array_index (sps_array, i);
548       GstMapInfo spsmap;
549       guint32 tmp_sps_id;
550
551       gst_buffer_map (sps, &spsmap, GST_MAP_READ);
552       parse_sps (&spsmap, &tmp_sps_id);
553
554       if (sps_id == tmp_sps_id) {
555         if (map.size == spsmap.size &&
556             memcmp (map.data, spsmap.data, spsmap.size) == 0) {
557           GST_LOG_OBJECT (rtph264, "Unchanged SPS %u, not updating", sps_id);
558           gst_buffer_unmap (sps, &spsmap);
559           goto drop;
560         } else {
561           gst_buffer_unmap (sps, &spsmap);
562           g_ptr_array_remove_index_fast (sps_array, i);
563           g_ptr_array_add (sps_array, nal);
564           GST_LOG_OBJECT (rtph264, "Modified SPS %u, replacing", sps_id);
565           goto done;
566         }
567       }
568       gst_buffer_unmap (sps, &spsmap);
569     }
570     GST_LOG_OBJECT (rtph264, "Adding new SPS %u", sps_id);
571     g_ptr_array_add (sps_array, nal);
572   } else if (type == 8) {
573     guint32 sps_id;
574     guint32 pps_id;
575
576     if (!parse_pps (&map, &sps_id, &pps_id)) {
577       GST_WARNING_OBJECT (rtph264, "Invalid PPS,"
578           " can't parse seq_parameter_set_id or pic_parameter_set_id");
579       goto drop;
580     }
581
582     for (i = 0; i < pps_array->len; i++) {
583       GstBuffer *pps = g_ptr_array_index (pps_array, i);
584       GstMapInfo ppsmap;
585       guint32 tmp_sps_id;
586       guint32 tmp_pps_id;
587
588
589       gst_buffer_map (pps, &ppsmap, GST_MAP_READ);
590       parse_pps (&ppsmap, &tmp_sps_id, &tmp_pps_id);
591
592       if (pps_id == tmp_pps_id) {
593         if (map.size == ppsmap.size &&
594             memcmp (map.data, ppsmap.data, ppsmap.size) == 0) {
595           GST_LOG_OBJECT (rtph264, "Unchanged PPS %u:%u, not updating", sps_id,
596               pps_id);
597           gst_buffer_unmap (pps, &ppsmap);
598           goto drop;
599         } else {
600           gst_buffer_unmap (pps, &ppsmap);
601           g_ptr_array_remove_index_fast (pps_array, i);
602           g_ptr_array_add (pps_array, nal);
603           GST_LOG_OBJECT (rtph264, "Modified PPS %u:%u, replacing",
604               sps_id, pps_id);
605           goto done;
606         }
607       }
608       gst_buffer_unmap (pps, &ppsmap);
609     }
610     GST_LOG_OBJECT (rtph264, "Adding new PPS %u:%i", sps_id, pps_id);
611     g_ptr_array_add (pps_array, nal);
612   } else {
613     goto drop;
614   }
615
616 done:
617   gst_buffer_unmap (nal, &map);
618
619   return TRUE;
620
621 drop:
622   gst_buffer_unmap (nal, &map);
623   gst_buffer_unref (nal);
624
625   return FALSE;
626 }
627
628
629 static void
630 gst_rtp_h264_depay_add_sps_pps (GstRtpH264Depay * rtph264depay, GstBuffer * nal)
631 {
632   if (gst_rtp_h264_add_sps_pps (GST_ELEMENT (rtph264depay),
633           rtph264depay->sps, rtph264depay->pps, nal))
634     rtph264depay->new_codec_data = TRUE;
635 }
636
637 static gboolean
638 gst_rtp_h264_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
639 {
640   gint clock_rate;
641   GstStructure *structure = gst_caps_get_structure (caps, 0);
642   GstRtpH264Depay *rtph264depay;
643   const gchar *ps;
644   GstBuffer *codec_data;
645   GstMapInfo map;
646   guint8 *ptr;
647
648   rtph264depay = GST_RTP_H264_DEPAY (depayload);
649
650   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
651     clock_rate = 90000;
652   depayload->clock_rate = clock_rate;
653
654   /* Base64 encoded, comma separated config NALs */
655   ps = gst_structure_get_string (structure, "sprop-parameter-sets");
656
657   /* negotiate with downstream w.r.t. output format and alignment */
658   gst_rtp_h264_depay_negotiate (rtph264depay);
659
660   if (rtph264depay->byte_stream && ps != NULL) {
661     /* for bytestream we only need the parameter sets but we don't error out
662      * when they are not there, we assume they are in the stream. */
663     gchar **params;
664     guint len, total;
665     gint i;
666
667     params = g_strsplit (ps, ",", 0);
668
669     /* count total number of bytes in base64. Also include the sync bytes in
670      * front of the params. */
671     len = 0;
672     for (i = 0; params[i]; i++) {
673       len += strlen (params[i]);
674       len += sizeof (sync_bytes);
675     }
676     /* we seriously overshoot the length, but it's fine. */
677     codec_data = gst_buffer_new_and_alloc (len);
678
679     gst_buffer_map (codec_data, &map, GST_MAP_WRITE);
680     ptr = map.data;
681     total = 0;
682     for (i = 0; params[i]; i++) {
683       guint save = 0;
684       gint state = 0;
685
686       GST_DEBUG_OBJECT (depayload, "decoding param %d (%s)", i, params[i]);
687       memcpy (ptr, sync_bytes, sizeof (sync_bytes));
688       ptr += sizeof (sync_bytes);
689       len =
690           g_base64_decode_step (params[i], strlen (params[i]), ptr, &state,
691           &save);
692       GST_DEBUG_OBJECT (depayload, "decoded %d bytes", len);
693       total += len + sizeof (sync_bytes);
694       ptr += len;
695     }
696     gst_buffer_unmap (codec_data, &map);
697     gst_buffer_resize (codec_data, 0, total);
698     g_strfreev (params);
699
700     /* keep the codec_data, we need to send it as the first buffer. We cannot
701      * push it in the adapter because the adapter might be flushed on discont.
702      */
703     if (rtph264depay->codec_data)
704       gst_buffer_unref (rtph264depay->codec_data);
705     rtph264depay->codec_data = codec_data;
706   } else if (!rtph264depay->byte_stream) {
707     gchar **params;
708     gint i;
709
710     if (ps == NULL)
711       goto incomplete_caps;
712
713     params = g_strsplit (ps, ",", 0);
714
715     GST_DEBUG_OBJECT (depayload, "we have %d params", g_strv_length (params));
716
717     /* start with 7 bytes header */
718     for (i = 0; params[i]; i++) {
719       GstBuffer *nal;
720       GstMapInfo nalmap;
721       gsize nal_len;
722       guint save = 0;
723       gint state = 0;
724
725       nal_len = strlen (params[i]);
726       nal = gst_buffer_new_and_alloc (nal_len);
727       gst_buffer_map (nal, &nalmap, GST_MAP_READWRITE);
728
729       nal_len =
730           g_base64_decode_step (params[i], nal_len, nalmap.data, &state, &save);
731
732       GST_DEBUG_OBJECT (depayload, "adding param %d as %s", i,
733           ((nalmap.data[0] & 0x1f) == 7) ? "SPS" : "PPS");
734
735       gst_buffer_unmap (nal, &nalmap);
736       gst_buffer_set_size (nal, nal_len);
737
738       gst_rtp_h264_depay_add_sps_pps (rtph264depay, nal);
739     }
740     g_strfreev (params);
741
742     if (rtph264depay->sps->len == 0 || rtph264depay->pps->len == 0)
743       goto incomplete_caps;
744   }
745
746   return gst_rtp_h264_set_src_caps (rtph264depay);
747
748   /* ERRORS */
749 incomplete_caps:
750   {
751     GST_DEBUG_OBJECT (depayload, "we have incomplete caps,"
752         " doing setcaps later");
753     return TRUE;
754   }
755 }
756
757 static GstBuffer *
758 gst_rtp_h264_complete_au (GstRtpH264Depay * rtph264depay,
759     GstClockTime * out_timestamp, gboolean * out_keyframe)
760 {
761   guint outsize;
762   GstBuffer *outbuf;
763
764   /* we had a picture in the adapter and we completed it */
765   GST_DEBUG_OBJECT (rtph264depay, "taking completed AU");
766   outsize = gst_adapter_available (rtph264depay->picture_adapter);
767   outbuf = gst_adapter_take_buffer (rtph264depay->picture_adapter, outsize);
768
769   *out_timestamp = rtph264depay->last_ts;
770   *out_keyframe = rtph264depay->last_keyframe;
771
772   rtph264depay->last_keyframe = FALSE;
773   rtph264depay->picture_start = FALSE;
774
775   return outbuf;
776 }
777
778 /* SPS/PPS/IDR considered key, all others DELTA;
779  * so downstream waiting for keyframe can pick up at SPS/PPS/IDR */
780 #define NAL_TYPE_IS_KEY(nt) (((nt) == 5) || ((nt) == 7) || ((nt) == 8))
781
782 static GstBuffer *
783 gst_rtp_h264_depay_handle_nal (GstRtpH264Depay * rtph264depay, GstBuffer * nal,
784     GstClockTime in_timestamp, gboolean marker)
785 {
786   GstRTPBaseDepayload *depayload = GST_RTP_BASE_DEPAYLOAD (rtph264depay);
787   gint nal_type;
788   GstMapInfo map;
789   GstBuffer *outbuf = NULL;
790   GstClockTime out_timestamp;
791   gboolean keyframe, out_keyframe;
792
793   gst_buffer_map (nal, &map, GST_MAP_READ);
794   if (G_UNLIKELY (map.size < 5))
795     goto short_nal;
796
797   nal_type = map.data[4] & 0x1f;
798   GST_DEBUG_OBJECT (rtph264depay, "handle NAL type %d", nal_type);
799
800   keyframe = NAL_TYPE_IS_KEY (nal_type);
801
802   out_keyframe = keyframe;
803   out_timestamp = in_timestamp;
804
805   if (!rtph264depay->byte_stream) {
806     if (nal_type == 7 || nal_type == 8) {
807       gst_rtp_h264_depay_add_sps_pps (rtph264depay,
808           gst_buffer_copy_region (nal, GST_BUFFER_COPY_ALL,
809               4, gst_buffer_get_size (nal) - 4));
810       gst_buffer_unmap (nal, &map);
811       gst_buffer_unref (nal);
812       return NULL;
813     } else if (rtph264depay->sps->len == 0 || rtph264depay->pps->len == 0) {
814       /* Down push down any buffer in non-bytestream mode if the SPS/PPS haven't
815        * go through yet
816        */
817       gst_pad_push_event (GST_RTP_BASE_DEPAYLOAD_SINKPAD (depayload),
818           gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM,
819               gst_structure_new ("GstForceKeyUnit",
820                   "all-headers", G_TYPE_BOOLEAN, TRUE, NULL)));
821       gst_buffer_unmap (nal, &map);
822       gst_buffer_unref (nal);
823       return NULL;
824     }
825
826     if (rtph264depay->new_codec_data &&
827         rtph264depay->sps->len > 0 && rtph264depay->pps->len > 0)
828       gst_rtp_h264_set_src_caps (rtph264depay);
829   }
830
831
832   if (rtph264depay->merge) {
833     gboolean start = FALSE, complete = FALSE;
834
835     /* marker bit isn't mandatory so in the following code we try to guess
836      * an AU boundary by detecting a new picture start */
837     if (!marker) {
838       /* consider a coded slices (IDR or not) to start a picture,
839        * (so ending the previous one) if first_mb_in_slice == 0
840        * (non-0 is part of previous one) */
841       /* NOTE this is not entirely according to Access Unit specs in 7.4.1.2.4,
842        * but in practice it works in sane cases, needs not much parsing,
843        * and also works with broken frame_num in NAL (where spec-wise would fail) */
844       /* FIXME: this code isn't correct for interlaced content as AUs should be
845        * constructed with pairs of fields and the guess here will just push out
846        * AUs with a single field in it */
847       if (nal_type == 1 || nal_type == 2 || nal_type == 5) {
848         /* we have a picture start */
849         start = TRUE;
850         if (map.data[5] & 0x80) {
851           /* first_mb_in_slice == 0 completes a picture */
852           complete = TRUE;
853         }
854       } else if (nal_type >= 6 && nal_type <= 9) {
855         /* SEI, SPS, PPS, AU terminate picture */
856         complete = TRUE;
857       }
858       GST_DEBUG_OBJECT (depayload, "start %d, complete %d", start, complete);
859
860       if (complete && rtph264depay->picture_start)
861         outbuf = gst_rtp_h264_complete_au (rtph264depay, &out_timestamp,
862             &out_keyframe);
863     }
864     /* add to adapter */
865     gst_buffer_unmap (nal, &map);
866
867     GST_DEBUG_OBJECT (depayload, "adding NAL to picture adapter");
868     gst_adapter_push (rtph264depay->picture_adapter, nal);
869     rtph264depay->last_ts = in_timestamp;
870     rtph264depay->last_keyframe |= keyframe;
871     rtph264depay->picture_start |= start;
872
873     if (marker)
874       outbuf = gst_rtp_h264_complete_au (rtph264depay, &out_timestamp,
875           &out_keyframe);
876   } else {
877     /* no merge, output is input nal */
878     GST_DEBUG_OBJECT (depayload, "using NAL as output");
879     outbuf = nal;
880     gst_buffer_unmap (nal, &map);
881   }
882
883   if (outbuf) {
884     /* prepend codec_data */
885     if (rtph264depay->codec_data) {
886       GST_DEBUG_OBJECT (depayload, "prepending codec_data");
887       gst_rtp_copy_meta (GST_ELEMENT_CAST (rtph264depay),
888           rtph264depay->codec_data, outbuf,
889           g_quark_from_static_string (GST_META_TAG_VIDEO_STR));
890       outbuf = gst_buffer_append (rtph264depay->codec_data, outbuf);
891       rtph264depay->codec_data = NULL;
892       out_keyframe = TRUE;
893     }
894     outbuf = gst_buffer_make_writable (outbuf);
895
896     gst_rtp_drop_meta (GST_ELEMENT_CAST (rtph264depay), outbuf,
897         g_quark_from_static_string (GST_META_TAG_VIDEO_STR));
898
899     GST_BUFFER_PTS (outbuf) = out_timestamp;
900
901     if (out_keyframe)
902       GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
903     else
904       GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
905   }
906
907   return outbuf;
908
909   /* ERRORS */
910 short_nal:
911   {
912     GST_WARNING_OBJECT (depayload, "dropping short NAL");
913     gst_buffer_unmap (nal, &map);
914     gst_buffer_unref (nal);
915     return NULL;
916   }
917 }
918
919 static GstBuffer *
920 gst_rtp_h264_push_fragmentation_unit (GstRtpH264Depay * rtph264depay,
921     gboolean send)
922 {
923   guint outsize;
924   GstMapInfo map;
925   GstBuffer *outbuf;
926
927   outsize = gst_adapter_available (rtph264depay->adapter);
928   outbuf = gst_adapter_take_buffer (rtph264depay->adapter, outsize);
929
930   gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
931   GST_DEBUG_OBJECT (rtph264depay, "output %d bytes", outsize);
932
933   if (rtph264depay->byte_stream) {
934     memcpy (map.data, sync_bytes, sizeof (sync_bytes));
935   } else {
936     outsize -= 4;
937     map.data[0] = (outsize >> 24);
938     map.data[1] = (outsize >> 16);
939     map.data[2] = (outsize >> 8);
940     map.data[3] = (outsize);
941   }
942   gst_buffer_unmap (outbuf, &map);
943
944   rtph264depay->current_fu_type = 0;
945
946   outbuf = gst_rtp_h264_depay_handle_nal (rtph264depay, outbuf,
947       rtph264depay->fu_timestamp, rtph264depay->fu_marker);
948
949   if (send && outbuf) {
950     gst_rtp_base_depayload_push (GST_RTP_BASE_DEPAYLOAD (rtph264depay), outbuf);
951     outbuf = NULL;
952   }
953   return outbuf;
954 }
955
956 static GstBuffer *
957 gst_rtp_h264_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
958 {
959   GstRtpH264Depay *rtph264depay;
960   GstBuffer *buf;
961   GstBuffer *outbuf = NULL;
962   guint8 nal_unit_type;
963
964   rtph264depay = GST_RTP_H264_DEPAY (depayload);
965
966   /* flush remaining data on discont */
967   if (GST_BUFFER_IS_DISCONT (rtp->buffer)) {
968     gst_adapter_clear (rtph264depay->adapter);
969     rtph264depay->wait_start = TRUE;
970     rtph264depay->current_fu_type = 0;
971   }
972
973   {
974     gint payload_len;
975     guint8 *payload;
976     guint header_len;
977     guint8 nal_ref_idc;
978     GstMapInfo map;
979     guint outsize, nalu_size;
980     GstClockTime timestamp;
981     gboolean marker;
982
983     timestamp = GST_BUFFER_PTS (rtp->buffer);
984
985     payload_len = gst_rtp_buffer_get_payload_len (rtp);
986     payload = gst_rtp_buffer_get_payload (rtp);
987     buf = gst_rtp_buffer_get_payload_buffer (rtp);
988     marker = gst_rtp_buffer_get_marker (rtp);
989
990     GST_DEBUG_OBJECT (rtph264depay, "receiving %d bytes", payload_len);
991
992     if (payload_len == 0)
993       goto empty_packet;
994
995     /* +---------------+
996      * |0|1|2|3|4|5|6|7|
997      * +-+-+-+-+-+-+-+-+
998      * |F|NRI|  Type   |
999      * +---------------+
1000      *
1001      * F must be 0.
1002      */
1003     nal_ref_idc = (payload[0] & 0x60) >> 5;
1004     nal_unit_type = payload[0] & 0x1f;
1005
1006     /* at least one byte header with type */
1007     header_len = 1;
1008
1009     GST_DEBUG_OBJECT (rtph264depay, "NRI %d, Type %d", nal_ref_idc,
1010         nal_unit_type);
1011
1012     /* If FU unit was being processed, but the current nal is of a different
1013      * type.  Assume that the remote payloader is buggy (didn't set the end bit
1014      * when the FU ended) and send out what we gathered thusfar */
1015     if (G_UNLIKELY (rtph264depay->current_fu_type != 0 &&
1016             nal_unit_type != rtph264depay->current_fu_type))
1017       gst_rtp_h264_push_fragmentation_unit (rtph264depay, TRUE);
1018
1019     switch (nal_unit_type) {
1020       case 0:
1021       case 30:
1022       case 31:
1023         /* undefined */
1024         goto undefined_type;
1025       case 25:
1026         /* STAP-B    Single-time aggregation packet     5.7.1 */
1027         /* 2 byte extra header for DON */
1028         header_len += 2;
1029         /* fallthrough */
1030       case 24:
1031       {
1032         /* strip headers */
1033         payload += header_len;
1034         payload_len -= header_len;
1035
1036         rtph264depay->wait_start = FALSE;
1037
1038
1039         /* STAP-A    Single-time aggregation packet     5.7.1 */
1040         while (payload_len > 2) {
1041           /*                      1          
1042            *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 
1043            * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1044            * |         NALU Size             |
1045            * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1046            */
1047           nalu_size = (payload[0] << 8) | payload[1];
1048
1049           /* dont include nalu_size */
1050           if (nalu_size > (payload_len - 2))
1051             nalu_size = payload_len - 2;
1052
1053           outsize = nalu_size + sizeof (sync_bytes);
1054           outbuf = gst_buffer_new_and_alloc (outsize);
1055
1056           gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
1057           if (rtph264depay->byte_stream) {
1058             memcpy (map.data, sync_bytes, sizeof (sync_bytes));
1059           } else {
1060             map.data[0] = map.data[1] = 0;
1061             map.data[2] = payload[0];
1062             map.data[3] = payload[1];
1063           }
1064
1065           /* strip NALU size */
1066           payload += 2;
1067           payload_len -= 2;
1068
1069           memcpy (map.data + sizeof (sync_bytes), payload, nalu_size);
1070           gst_buffer_unmap (outbuf, &map);
1071
1072           gst_rtp_copy_meta (GST_ELEMENT_CAST (rtph264depay), outbuf, buf,
1073               g_quark_from_static_string (GST_META_TAG_VIDEO_STR));
1074
1075           outbuf =
1076               gst_rtp_h264_depay_handle_nal (rtph264depay, outbuf, timestamp,
1077               marker);
1078           if (outbuf)
1079             gst_adapter_push (rtph264depay->adapter, outbuf);
1080
1081           payload += nalu_size;
1082           payload_len -= nalu_size;
1083         }
1084
1085         outsize = gst_adapter_available (rtph264depay->adapter);
1086         if (outsize > 0) {
1087           outbuf = gst_adapter_take_buffer (rtph264depay->adapter, outsize);
1088           outbuf =
1089               gst_rtp_h264_depay_handle_nal (rtph264depay, outbuf, timestamp,
1090               marker);
1091         }
1092         break;
1093       }
1094       case 26:
1095         /* MTAP16    Multi-time aggregation packet      5.7.2 */
1096         // header_len = 5;
1097         /* fallthrough, not implemented */
1098       case 27:
1099         /* MTAP24    Multi-time aggregation packet      5.7.2 */
1100         // header_len = 6;
1101         goto not_implemented;
1102         break;
1103       case 28:
1104       case 29:
1105       {
1106         /* FU-A      Fragmentation unit                 5.8 */
1107         /* FU-B      Fragmentation unit                 5.8 */
1108         gboolean S, E;
1109
1110         /* +---------------+
1111          * |0|1|2|3|4|5|6|7|
1112          * +-+-+-+-+-+-+-+-+
1113          * |S|E|R|  Type   |
1114          * +---------------+
1115          *
1116          * R is reserved and always 0
1117          */
1118         S = (payload[1] & 0x80) == 0x80;
1119         E = (payload[1] & 0x40) == 0x40;
1120
1121         GST_DEBUG_OBJECT (rtph264depay, "S %d, E %d", S, E);
1122
1123         if (rtph264depay->wait_start && !S)
1124           goto waiting_start;
1125
1126         if (S) {
1127           /* NAL unit starts here */
1128           guint8 nal_header;
1129
1130           /* If a new FU unit started, while still processing an older one.
1131            * Assume that the remote payloader is buggy (doesn't set the end
1132            * bit) and send out what we've gathered thusfar */
1133           if (G_UNLIKELY (rtph264depay->current_fu_type != 0))
1134             gst_rtp_h264_push_fragmentation_unit (rtph264depay, TRUE);
1135
1136           rtph264depay->current_fu_type = nal_unit_type;
1137           rtph264depay->fu_timestamp = timestamp;
1138
1139           rtph264depay->wait_start = FALSE;
1140
1141           /* reconstruct NAL header */
1142           nal_header = (payload[0] & 0xe0) | (payload[1] & 0x1f);
1143
1144           /* strip type header, keep FU header, we'll reuse it to reconstruct
1145            * the NAL header. */
1146           payload += 1;
1147           payload_len -= 1;
1148
1149           nalu_size = payload_len;
1150           outsize = nalu_size + sizeof (sync_bytes);
1151           outbuf = gst_buffer_new_and_alloc (outsize);
1152
1153           gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
1154           memcpy (map.data + sizeof (sync_bytes), payload, nalu_size);
1155           map.data[sizeof (sync_bytes)] = nal_header;
1156           gst_buffer_unmap (outbuf, &map);
1157
1158           gst_rtp_copy_meta (GST_ELEMENT_CAST (rtph264depay), outbuf, buf,
1159               g_quark_from_static_string (GST_META_TAG_VIDEO_STR));
1160
1161           GST_DEBUG_OBJECT (rtph264depay, "queueing %d bytes", outsize);
1162
1163           /* and assemble in the adapter */
1164           gst_adapter_push (rtph264depay->adapter, outbuf);
1165         } else {
1166           /* strip off FU indicator and FU header bytes */
1167           payload += 2;
1168           payload_len -= 2;
1169
1170           outsize = payload_len;
1171           outbuf = gst_buffer_new_and_alloc (outsize);
1172           gst_buffer_fill (outbuf, 0, payload, outsize);
1173
1174           gst_rtp_copy_meta (GST_ELEMENT_CAST (rtph264depay), outbuf, buf,
1175               g_quark_from_static_string (GST_META_TAG_VIDEO_STR));
1176
1177           GST_DEBUG_OBJECT (rtph264depay, "queueing %d bytes", outsize);
1178
1179           /* and assemble in the adapter */
1180           gst_adapter_push (rtph264depay->adapter, outbuf);
1181         }
1182
1183         outbuf = NULL;
1184         rtph264depay->fu_marker = marker;
1185
1186         /* if NAL unit ends, flush the adapter */
1187         if (E)
1188           outbuf = gst_rtp_h264_push_fragmentation_unit (rtph264depay, FALSE);
1189         break;
1190       }
1191       default:
1192       {
1193         rtph264depay->wait_start = FALSE;
1194
1195         /* 1-23   NAL unit  Single NAL unit packet per H.264   5.6 */
1196         /* the entire payload is the output buffer */
1197         nalu_size = payload_len;
1198         outsize = nalu_size + sizeof (sync_bytes);
1199         outbuf = gst_buffer_new_and_alloc (outsize);
1200
1201         gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
1202         if (rtph264depay->byte_stream) {
1203           memcpy (map.data, sync_bytes, sizeof (sync_bytes));
1204         } else {
1205           map.data[0] = map.data[1] = 0;
1206           map.data[2] = nalu_size >> 8;
1207           map.data[3] = nalu_size & 0xff;
1208         }
1209         memcpy (map.data + sizeof (sync_bytes), payload, nalu_size);
1210         gst_buffer_unmap (outbuf, &map);
1211
1212         gst_rtp_copy_meta (GST_ELEMENT_CAST (rtph264depay), outbuf, buf,
1213             g_quark_from_static_string (GST_META_TAG_VIDEO_STR));
1214
1215         outbuf = gst_rtp_h264_depay_handle_nal (rtph264depay, outbuf, timestamp,
1216             marker);
1217         break;
1218       }
1219     }
1220   }
1221
1222   gst_buffer_unref (buf);
1223
1224   return outbuf;
1225
1226   /* ERRORS */
1227 empty_packet:
1228   {
1229     GST_DEBUG_OBJECT (rtph264depay, "empty packet");
1230     gst_buffer_unref (buf);
1231     return NULL;
1232   }
1233 undefined_type:
1234   {
1235     GST_ELEMENT_WARNING (rtph264depay, STREAM, DECODE,
1236         (NULL), ("Undefined packet type"));
1237     gst_buffer_unref (buf);
1238     return NULL;
1239   }
1240 waiting_start:
1241   {
1242     GST_DEBUG_OBJECT (rtph264depay, "waiting for start");
1243     gst_buffer_unref (buf);
1244     return NULL;
1245   }
1246 not_implemented:
1247   {
1248     GST_ELEMENT_ERROR (rtph264depay, STREAM, FORMAT,
1249         (NULL), ("NAL unit type %d not supported yet", nal_unit_type));
1250     gst_buffer_unref (buf);
1251     return NULL;
1252   }
1253 }
1254
1255 static gboolean
1256 gst_rtp_h264_depay_handle_event (GstRTPBaseDepayload * depay, GstEvent * event)
1257 {
1258   GstRtpH264Depay *rtph264depay;
1259
1260   rtph264depay = GST_RTP_H264_DEPAY (depay);
1261
1262   switch (GST_EVENT_TYPE (event)) {
1263     case GST_EVENT_FLUSH_STOP:
1264       gst_rtp_h264_depay_reset (rtph264depay);
1265       break;
1266     default:
1267       break;
1268   }
1269
1270   return
1271       GST_RTP_BASE_DEPAYLOAD_CLASS (parent_class)->handle_event (depay, event);
1272 }
1273
1274 static GstStateChangeReturn
1275 gst_rtp_h264_depay_change_state (GstElement * element,
1276     GstStateChange transition)
1277 {
1278   GstRtpH264Depay *rtph264depay;
1279   GstStateChangeReturn ret;
1280
1281   rtph264depay = GST_RTP_H264_DEPAY (element);
1282
1283   switch (transition) {
1284     case GST_STATE_CHANGE_NULL_TO_READY:
1285       break;
1286     case GST_STATE_CHANGE_READY_TO_PAUSED:
1287       gst_rtp_h264_depay_reset (rtph264depay);
1288       break;
1289     default:
1290       break;
1291   }
1292
1293   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1294
1295   switch (transition) {
1296     case GST_STATE_CHANGE_READY_TO_NULL:
1297       break;
1298     default:
1299       break;
1300   }
1301   return ret;
1302 }
1303
1304 gboolean
1305 gst_rtp_h264_depay_plugin_init (GstPlugin * plugin)
1306 {
1307   GST_DEBUG_CATEGORY_INIT (rtph264depay_debug, "rtph264depay", 0,
1308       "H264 Video RTP Depayloader");
1309
1310   return gst_element_register (plugin, "rtph264depay",
1311       GST_RANK_SECONDARY, GST_TYPE_RTP_H264_DEPAY);
1312 }