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