2 * Copyright (C) <2006> Wim Taymans <wim.taymans@gmail.com>
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.
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.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
27 #include <gst/base/gstbitreader.h>
28 #include <gst/rtp/gstrtpbuffer.h>
29 #include "gstrtph264depay.h"
31 GST_DEBUG_CATEGORY_STATIC (rtph264depay_debug);
32 #define GST_CAT_DEFAULT (rtph264depay_debug)
34 #define DEFAULT_BYTE_STREAM TRUE
35 #define DEFAULT_ACCESS_UNIT FALSE
46 /* 3 zero bytes syncword */
47 static const guint8 sync_bytes[] = { 0, 0, 0, 1 };
49 static GstStaticPadTemplate gst_rtp_h264_depay_src_template =
50 GST_STATIC_PAD_TEMPLATE ("src",
53 GST_STATIC_CAPS ("video/x-h264, "
54 "stream-format = (string) avc, alignment = (string) au; "
56 "stream-format = (string) byte-stream, alignment = (string) { nal, au }")
59 static GstStaticPadTemplate gst_rtp_h264_depay_sink_template =
60 GST_STATIC_PAD_TEMPLATE ("sink",
63 GST_STATIC_CAPS ("application/x-rtp, "
64 "media = (string) \"video\", "
65 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
66 "clock-rate = (int) 90000, " "encoding-name = (string) \"H264\"")
67 /** optional parameters **/
68 /* "profile-level-id = (string) ANY, " */
69 /* "max-mbps = (string) ANY, " */
70 /* "max-fs = (string) ANY, " */
71 /* "max-cpb = (string) ANY, " */
72 /* "max-dpb = (string) ANY, " */
73 /* "max-br = (string) ANY, " */
74 /* "redundant-pic-cap = (string) { \"0\", \"1\" }, " */
75 /* "sprop-parameter-sets = (string) ANY, " */
76 /* "parameter-add = (string) { \"0\", \"1\" }, " */
77 /* "packetization-mode = (string) { \"0\", \"1\", \"2\" }, " */
78 /* "sprop-interleaving-depth = (string) ANY, " */
79 /* "sprop-deint-buf-req = (string) ANY, " */
80 /* "deint-buf-cap = (string) ANY, " */
81 /* "sprop-init-buf-time = (string) ANY, " */
82 /* "sprop-max-don-diff = (string) ANY, " */
83 /* "max-rcmd-nalu-size = (string) ANY " */
86 #define gst_rtp_h264_depay_parent_class parent_class
87 G_DEFINE_TYPE (GstRtpH264Depay, gst_rtp_h264_depay,
88 GST_TYPE_RTP_BASE_DEPAYLOAD);
90 static void gst_rtp_h264_depay_finalize (GObject * object);
91 static void gst_rtp_h264_depay_set_property (GObject * object, guint prop_id,
92 const GValue * value, GParamSpec * pspec);
93 static void gst_rtp_h264_depay_get_property (GObject * object, guint prop_id,
94 GValue * value, GParamSpec * pspec);
96 static GstStateChangeReturn gst_rtp_h264_depay_change_state (GstElement *
97 element, GstStateChange transition);
99 static GstBuffer *gst_rtp_h264_depay_process (GstRTPBaseDepayload * depayload,
101 static gboolean gst_rtp_h264_depay_setcaps (GstRTPBaseDepayload * filter,
103 static gboolean gst_rtp_h264_depay_handle_event (GstRTPBaseDepayload * depay,
107 gst_rtp_h264_depay_class_init (GstRtpH264DepayClass * klass)
109 GObjectClass *gobject_class;
110 GstElementClass *gstelement_class;
111 GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
113 gobject_class = (GObjectClass *) klass;
114 gstelement_class = (GstElementClass *) klass;
115 gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
117 gobject_class->finalize = gst_rtp_h264_depay_finalize;
119 gobject_class->set_property = gst_rtp_h264_depay_set_property;
120 gobject_class->get_property = gst_rtp_h264_depay_get_property;
122 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BYTE_STREAM,
123 g_param_spec_boolean ("byte-stream", "Byte Stream",
124 "Generate byte stream format of NALU (deprecated; use caps)",
125 DEFAULT_BYTE_STREAM, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
126 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_ACCESS_UNIT,
127 g_param_spec_boolean ("access-unit", "Access Unit",
128 "Merge NALU into AU (picture) (deprecated; use caps)",
129 DEFAULT_ACCESS_UNIT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
131 gst_element_class_add_pad_template (gstelement_class,
132 gst_static_pad_template_get (&gst_rtp_h264_depay_src_template));
133 gst_element_class_add_pad_template (gstelement_class,
134 gst_static_pad_template_get (&gst_rtp_h264_depay_sink_template));
136 gst_element_class_set_details_simple (gstelement_class,
137 "RTP H264 depayloader", "Codec/Depayloader/Network/RTP",
138 "Extracts H264 video from RTP packets (RFC 3984)",
139 "Wim Taymans <wim.taymans@gmail.com>");
140 gstelement_class->change_state = gst_rtp_h264_depay_change_state;
142 gstrtpbasedepayload_class->process = gst_rtp_h264_depay_process;
143 gstrtpbasedepayload_class->set_caps = gst_rtp_h264_depay_setcaps;
144 gstrtpbasedepayload_class->handle_event = gst_rtp_h264_depay_handle_event;
146 GST_DEBUG_CATEGORY_INIT (rtph264depay_debug, "rtph264depay", 0,
147 "H264 Video RTP Depayloader");
151 gst_rtp_h264_depay_init (GstRtpH264Depay * rtph264depay)
153 rtph264depay->adapter = gst_adapter_new ();
154 rtph264depay->picture_adapter = gst_adapter_new ();
155 rtph264depay->byte_stream = DEFAULT_BYTE_STREAM;
156 rtph264depay->merge = DEFAULT_ACCESS_UNIT;
157 rtph264depay->sps = g_ptr_array_new_with_free_func (
158 (GDestroyNotify) gst_buffer_unref);
159 rtph264depay->pps = g_ptr_array_new_with_free_func (
160 (GDestroyNotify) gst_buffer_unref);
164 gst_rtp_h264_depay_reset (GstRtpH264Depay * rtph264depay)
166 gst_adapter_clear (rtph264depay->adapter);
167 rtph264depay->wait_start = TRUE;
168 gst_adapter_clear (rtph264depay->picture_adapter);
169 rtph264depay->picture_start = FALSE;
170 rtph264depay->last_keyframe = FALSE;
171 rtph264depay->last_ts = 0;
172 rtph264depay->current_fu_type = 0;
173 rtph264depay->new_codec_data = FALSE;
174 g_ptr_array_set_size (rtph264depay->sps, 0);
175 g_ptr_array_set_size (rtph264depay->pps, 0);
179 gst_rtp_h264_depay_finalize (GObject * object)
181 GstRtpH264Depay *rtph264depay;
183 rtph264depay = GST_RTP_H264_DEPAY (object);
185 if (rtph264depay->codec_data)
186 gst_buffer_unref (rtph264depay->codec_data);
188 g_object_unref (rtph264depay->adapter);
189 g_object_unref (rtph264depay->picture_adapter);
191 g_ptr_array_free (rtph264depay->sps, TRUE);
192 g_ptr_array_free (rtph264depay->pps, TRUE);
194 G_OBJECT_CLASS (parent_class)->finalize (object);
198 gst_rtp_h264_depay_set_property (GObject * object, guint prop_id,
199 const GValue * value, GParamSpec * pspec)
201 GstRtpH264Depay *rtph264depay;
203 rtph264depay = GST_RTP_H264_DEPAY (object);
206 case PROP_BYTE_STREAM:
207 rtph264depay->byte_stream = g_value_get_boolean (value);
209 case PROP_ACCESS_UNIT:
210 rtph264depay->merge = g_value_get_boolean (value);
213 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
219 gst_rtp_h264_depay_get_property (GObject * object, guint prop_id,
220 GValue * value, GParamSpec * pspec)
222 GstRtpH264Depay *rtph264depay;
224 rtph264depay = GST_RTP_H264_DEPAY (object);
227 case PROP_BYTE_STREAM:
228 g_value_set_boolean (value, rtph264depay->byte_stream);
230 case PROP_ACCESS_UNIT:
231 g_value_set_boolean (value, rtph264depay->merge);
234 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
240 gst_rtp_h264_depay_negotiate (GstRtpH264Depay * rtph264depay)
243 gint byte_stream = -1;
247 gst_pad_get_allowed_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (rtph264depay));
249 GST_DEBUG_OBJECT (rtph264depay, "allowed caps: %" GST_PTR_FORMAT, caps);
252 if (gst_caps_get_size (caps) > 0) {
253 GstStructure *s = gst_caps_get_structure (caps, 0);
254 const gchar *str = NULL;
256 if ((str = gst_structure_get_string (s, "stream-format"))) {
257 if (strcmp (str, "avc") == 0) {
259 } else if (strcmp (str, "byte-stream") == 0) {
262 GST_DEBUG_OBJECT (rtph264depay, "unknown stream-format: %s", str);
266 if ((str = gst_structure_get_string (s, "alignment"))) {
267 if (strcmp (str, "au") == 0) {
269 } else if (strcmp (str, "nal") == 0) {
272 GST_DEBUG_OBJECT (rtph264depay, "unknown alignment: %s", str);
276 gst_caps_unref (caps);
279 if (byte_stream >= 0) {
280 GST_DEBUG_OBJECT (rtph264depay, "downstream requires byte-stream %d",
282 if (rtph264depay->byte_stream != byte_stream) {
283 GST_WARNING_OBJECT (rtph264depay,
284 "overriding property setting based on caps");
285 rtph264depay->byte_stream = byte_stream;
289 GST_DEBUG_OBJECT (rtph264depay, "downstream requires merge %d", merge);
290 if (rtph264depay->merge != merge) {
291 GST_WARNING_OBJECT (rtph264depay,
292 "overriding property setting based on caps");
293 rtph264depay->merge = merge;
298 /* Stolen from bad/gst/mpegtsdemux/payloader_parsers.c */
299 /* variable length Exp-Golomb parsing according to H.264 spec 9.1*/
301 read_golomb (GstBitReader * br, guint32 * value)
303 guint8 b, leading_zeros = -1;
306 for (b = 0; !b; leading_zeros++) {
307 if (!gst_bit_reader_get_bits_uint8 (br, &b, 1))
312 *value = (*value >> 1) - 1;
313 if (leading_zeros > 0) {
315 if (!gst_bit_reader_get_bits_uint32 (br, &tmp, leading_zeros))
324 parse_sps (GstMapInfo * map, guint32 * sps_id)
326 GstBitReader br = GST_BIT_READER_INIT (map->data + 4,
332 if (!read_golomb (&br, sps_id))
339 parse_pps (GstMapInfo * map, guint32 * sps_id, guint32 * pps_id)
341 GstBitReader br = GST_BIT_READER_INIT (map->data + 1,
347 if (!read_golomb (&br, pps_id))
349 if (!read_golomb (&br, sps_id))
357 gst_rtp_h264_set_src_caps (GstRtpH264Depay * rtph264depay)
362 guchar profile_compat = G_MAXUINT8;
364 if (!rtph264depay->byte_stream &&
365 (!rtph264depay->new_codec_data ||
366 rtph264depay->sps->len == 0 || rtph264depay->pps->len == 0))
369 srccaps = gst_caps_new_simple ("video/x-h264",
370 "stream-format", G_TYPE_STRING,
371 rtph264depay->byte_stream ? "byte-stream" : "avc",
372 "alignment", G_TYPE_STRING, rtph264depay->merge ? "au" : "nal", NULL);
374 if (!rtph264depay->byte_stream) {
375 GstBuffer *codec_data;
383 /* start with 7 bytes header */
385 /* count sps & pps */
386 for (i = 0; i < rtph264depay->sps->len; i++)
387 len += 2 + gst_buffer_get_size (g_ptr_array_index (rtph264depay->sps, i));
388 for (i = 0; i < rtph264depay->pps->len; i++)
389 len += 2 + gst_buffer_get_size (g_ptr_array_index (rtph264depay->pps, i));
391 codec_data = gst_buffer_new_and_alloc (len);
392 g_debug ("alloc_len: %u", len);
393 gst_buffer_map (codec_data, &map, GST_MAP_READWRITE);
396 /* 8 bits version == 1 */
399 /* According to: ISO/IEC 14496-15:2004(E) section 5.2.4.1
400 * The level is the max level of all SPSes
401 * A profile compat bit can only be set if all SPSes include that bit
403 for (i = 0; i < rtph264depay->sps->len; i++) {
404 gst_buffer_map (g_ptr_array_index (rtph264depay->sps, i), &nalmap,
406 profile_compat &= nalmap.data[2];
407 level = MAX (level, nalmap.data[3]);
408 gst_buffer_unmap (g_ptr_array_index (rtph264depay->sps, i), &nalmap);
411 /* Assume all SPSes use the same profile, so extract from the first SPS */
412 gst_buffer_map (g_ptr_array_index (rtph264depay->sps, 0), &nalmap,
414 *data++ = nalmap.data[1];
415 gst_buffer_unmap (g_ptr_array_index (rtph264depay->sps, 0), &nalmap);
416 *data++ = profile_compat;
419 /* 6 bits reserved | 2 bits lengthSizeMinusOn */
421 /* 3 bits reserved | 5 bits numOfSequenceParameterSets */
422 *data++ = 0xe0 | (rtph264depay->sps->len & 0x1f);
425 for (i = 0; i < rtph264depay->sps->len; i++) {
426 gst_buffer_map (g_ptr_array_index (rtph264depay->sps, i), &nalmap,
429 GST_DEBUG_OBJECT (rtph264depay, "copy SPS %d of length %d", i,
431 GST_WRITE_UINT16_BE (data, nalmap.size);
433 memcpy (data, nalmap.data, nalmap.size);
435 gst_buffer_unmap (g_ptr_array_index (rtph264depay->sps, i), &nalmap);
438 /* 8 bits numOfPictureParameterSets */
439 *data++ = rtph264depay->pps->len;
441 for (i = 0; i < rtph264depay->pps->len; i++) {
442 gst_buffer_map (g_ptr_array_index (rtph264depay->pps, i), &nalmap,
445 GST_DEBUG_OBJECT (rtph264depay, "copy PPS %d of length %d", i,
447 GST_WRITE_UINT16_BE (data, nalmap.size);
449 memcpy (data, nalmap.data, nalmap.size);
451 gst_buffer_unmap (g_ptr_array_index (rtph264depay->pps, i), &nalmap);
454 new_size = data - map.data;
455 gst_buffer_unmap (codec_data, &map);
456 gst_buffer_set_size (codec_data, new_size);
459 gst_caps_set_simple (srccaps,
460 "codec_data", GST_TYPE_BUFFER, codec_data, NULL);
461 gst_buffer_unref (codec_data);
464 res = gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (rtph264depay),
466 gst_caps_unref (srccaps);
469 rtph264depay->new_codec_data = FALSE;
475 gst_rtp_h264_add_sps_pps (GstRtpH264Depay * rtph264depay, GstBuffer * nal)
481 gst_buffer_map (nal, &map, GST_MAP_READ);
483 type = map.data[0] & 0x1f;
488 if (!parse_sps (&map, &sps_id)) {
489 GST_WARNING_OBJECT (rtph264depay, "Invalid SPS,"
490 " can't parse seq_parameter_set_id");
494 for (i = 0; i < rtph264depay->sps->len; i++) {
495 GstBuffer *sps = g_ptr_array_index (rtph264depay->sps, i);
499 gst_buffer_map (sps, &spsmap, GST_MAP_READ);
500 parse_sps (&spsmap, &tmp_sps_id);
502 if (sps_id == tmp_sps_id) {
503 if (map.size == spsmap.size &&
504 memcmp (map.data, spsmap.data, spsmap.size) == 0) {
505 GST_LOG_OBJECT (rtph264depay, "Unchanged SPS %u, not updating",
507 gst_buffer_unmap (sps, &spsmap);
510 gst_buffer_unmap (sps, &spsmap);
511 g_ptr_array_remove_index_fast (rtph264depay->sps, i);
512 g_ptr_array_add (rtph264depay->sps, nal);
513 GST_LOG_OBJECT (rtph264depay, "Modified SPS %u, replacing", sps_id);
517 gst_buffer_unmap (sps, &spsmap);
519 GST_LOG_OBJECT (rtph264depay, "Adding new SPS %u", sps_id);
520 g_ptr_array_add (rtph264depay->sps, nal);
521 } else if (type == 8) {
525 if (!parse_pps (&map, &sps_id, &pps_id)) {
526 GST_WARNING_OBJECT (rtph264depay, "Invalid PPS,"
527 " can't parse seq_parameter_set_id or pic_parameter_set_id");
531 for (i = 0; i < rtph264depay->pps->len; i++) {
532 GstBuffer *pps = g_ptr_array_index (rtph264depay->pps, i);
538 gst_buffer_map (pps, &ppsmap, GST_MAP_READ);
539 parse_pps (&ppsmap, &tmp_sps_id, &tmp_pps_id);
541 if (sps_id == tmp_sps_id && pps_id == tmp_pps_id) {
542 if (map.size == ppsmap.size &&
543 memcmp (map.data, ppsmap.data, ppsmap.size) == 0) {
544 GST_LOG_OBJECT (rtph264depay, "Unchanged PPS %u:%u, not updating",
546 gst_buffer_unmap (pps, &ppsmap);
549 gst_buffer_unmap (pps, &ppsmap);
550 g_ptr_array_remove_index_fast (rtph264depay->pps, i);
551 g_ptr_array_add (rtph264depay->pps, nal);
552 GST_LOG_OBJECT (rtph264depay, "Modified PPS %u:%u, replacing",
557 gst_buffer_unmap (pps, &ppsmap);
559 GST_LOG_OBJECT (rtph264depay, "Adding new PPS %u:%i", sps_id, pps_id);
560 g_ptr_array_add (rtph264depay->pps, nal);
566 gst_buffer_unmap (nal, &map);
567 rtph264depay->new_codec_data = TRUE;
572 gst_buffer_unmap (nal, &map);
573 gst_buffer_unref (nal);
579 gst_rtp_h264_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
582 GstStructure *structure = gst_caps_get_structure (caps, 0);
583 GstRtpH264Depay *rtph264depay;
585 GstBuffer *codec_data;
589 rtph264depay = GST_RTP_H264_DEPAY (depayload);
591 if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
593 depayload->clock_rate = clock_rate;
595 /* Base64 encoded, comma separated config NALs */
596 ps = gst_structure_get_string (structure, "sprop-parameter-sets");
598 /* negotiate with downstream w.r.t. output format and alignment */
599 gst_rtp_h264_depay_negotiate (rtph264depay);
601 if (rtph264depay->byte_stream && ps != NULL) {
602 /* for bytestream we only need the parameter sets but we don't error out
603 * when they are not there, we assume they are in the stream. */
608 params = g_strsplit (ps, ",", 0);
610 /* count total number of bytes in base64. Also include the sync bytes in
611 * front of the params. */
613 for (i = 0; params[i]; i++) {
614 len += strlen (params[i]);
615 len += sizeof (sync_bytes);
617 /* we seriously overshoot the length, but it's fine. */
618 codec_data = gst_buffer_new_and_alloc (len);
620 gst_buffer_map (codec_data, &map, GST_MAP_WRITE);
623 for (i = 0; params[i]; i++) {
627 GST_DEBUG_OBJECT (depayload, "decoding param %d (%s)", i, params[i]);
628 memcpy (ptr, sync_bytes, sizeof (sync_bytes));
629 ptr += sizeof (sync_bytes);
631 g_base64_decode_step (params[i], strlen (params[i]), ptr, &state,
633 GST_DEBUG_OBJECT (depayload, "decoded %d bytes", len);
634 total += len + sizeof (sync_bytes);
637 gst_buffer_unmap (codec_data, &map);
638 gst_buffer_resize (codec_data, 0, total);
641 /* keep the codec_data, we need to send it as the first buffer. We cannot
642 * push it in the adapter because the adapter might be flushed on discont.
644 if (rtph264depay->codec_data)
645 gst_buffer_unref (rtph264depay->codec_data);
646 rtph264depay->codec_data = codec_data;
647 } else if (!rtph264depay->byte_stream) {
652 goto incomplete_caps;
654 params = g_strsplit (ps, ",", 0);
656 GST_DEBUG_OBJECT (depayload, "we have %d params", g_strv_length (params));
658 /* start with 7 bytes header */
659 for (i = 0; params[i]; i++) {
666 nal_len = strlen (params[i]);
667 nal = gst_buffer_new_and_alloc (nal_len);
668 gst_buffer_map (nal, &nalmap, GST_MAP_READWRITE);
671 g_base64_decode_step (params[i], nal_len, nalmap.data, &state, &save);
673 GST_DEBUG_OBJECT (depayload, "adding param %d as %s", i,
674 ((nalmap.data[0] & 0x1f) == 7) ? "SPS" : "PPS");
676 gst_buffer_unmap (nal, &nalmap);
677 gst_buffer_set_size (nal, nal_len);
679 gst_rtp_h264_add_sps_pps (rtph264depay, nal);
683 if (rtph264depay->sps->len == 0 || rtph264depay->pps->len == 0)
684 goto incomplete_caps;
687 return gst_rtp_h264_set_src_caps (rtph264depay);
692 GST_DEBUG_OBJECT (depayload, "we have incomplete caps,"
693 " doing setcaps later");
699 gst_rtp_h264_complete_au (GstRtpH264Depay * rtph264depay,
700 GstClockTime * out_timestamp, gboolean * out_keyframe)
705 /* we had a picture in the adapter and we completed it */
706 GST_DEBUG_OBJECT (rtph264depay, "taking completed AU");
707 outsize = gst_adapter_available (rtph264depay->picture_adapter);
708 outbuf = gst_adapter_take_buffer (rtph264depay->picture_adapter, outsize);
710 *out_timestamp = rtph264depay->last_ts;
711 *out_keyframe = rtph264depay->last_keyframe;
713 rtph264depay->last_keyframe = FALSE;
714 rtph264depay->picture_start = FALSE;
719 /* SPS/PPS/IDR considered key, all others DELTA;
720 * so downstream waiting for keyframe can pick up at SPS/PPS/IDR */
721 #define NAL_TYPE_IS_KEY(nt) (((nt) == 5) || ((nt) == 7) || ((nt) == 8))
724 gst_rtp_h264_depay_handle_nal (GstRtpH264Depay * rtph264depay, GstBuffer * nal,
725 GstClockTime in_timestamp, gboolean marker)
727 GstRTPBaseDepayload *depayload = GST_RTP_BASE_DEPAYLOAD (rtph264depay);
730 GstBuffer *outbuf = NULL;
731 GstClockTime out_timestamp;
732 gboolean keyframe, out_keyframe;
734 gst_buffer_map (nal, &map, GST_MAP_READ);
735 if (G_UNLIKELY (map.size < 5))
738 nal_type = map.data[4] & 0x1f;
739 GST_DEBUG_OBJECT (rtph264depay, "handle NAL type %d", nal_type);
741 keyframe = NAL_TYPE_IS_KEY (nal_type);
743 out_keyframe = keyframe;
744 out_timestamp = in_timestamp;
746 if (!rtph264depay->byte_stream) {
747 if (nal_type == 7 || nal_type == 8) {
748 gst_rtp_h264_add_sps_pps (rtph264depay,
749 gst_buffer_copy_region (nal, GST_BUFFER_COPY_ALL,
750 4, gst_buffer_get_size (nal) - 4));
751 gst_buffer_unmap (nal, &map);
752 gst_buffer_unref (nal);
754 } else if (rtph264depay->sps->len == 0 || rtph264depay->pps->len == 0) {
755 /* Down push down any buffer in non-bytestream mode if the SPS/PPS haven't
758 gst_pad_push_event (GST_RTP_BASE_DEPAYLOAD_SINKPAD (depayload),
759 gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM,
760 gst_structure_new ("GstForceKeyUnit",
761 "all-headers", G_TYPE_BOOLEAN, TRUE, NULL)));
762 gst_buffer_unmap (nal, &map);
763 gst_buffer_unref (nal);
767 if (rtph264depay->new_codec_data &&
768 rtph264depay->sps->len > 0 && rtph264depay->pps->len > 0)
769 gst_rtp_h264_set_src_caps (rtph264depay);
773 if (rtph264depay->merge) {
774 gboolean start = FALSE, complete = FALSE;
776 /* consider a coded slices (IDR or not) to start a picture,
777 * (so ending the previous one) if first_mb_in_slice == 0
778 * (non-0 is part of previous one) */
779 /* NOTE this is not entirely according to Access Unit specs in 7.4.1.2.4,
780 * but in practice it works in sane cases, needs not much parsing,
781 * and also works with broken frame_num in NAL (where spec-wise would fail) */
782 if (nal_type == 1 || nal_type == 2 || nal_type == 5) {
783 /* we have a picture start */
785 if (map.data[5] & 0x80) {
786 /* first_mb_in_slice == 0 completes a picture */
789 } else if (nal_type >= 6 && nal_type <= 9) {
790 /* SEI, SPS, PPS, AU terminate picture */
793 GST_DEBUG_OBJECT (depayload, "start %d, complete %d", start, complete);
795 if (complete && rtph264depay->picture_start)
796 outbuf = gst_rtp_h264_complete_au (rtph264depay, &out_timestamp,
800 gst_buffer_unmap (nal, &map);
802 GST_DEBUG_OBJECT (depayload, "adding NAL to picture adapter");
803 gst_adapter_push (rtph264depay->picture_adapter, nal);
804 rtph264depay->last_ts = in_timestamp;
805 rtph264depay->last_keyframe |= keyframe;
806 rtph264depay->picture_start |= start;
809 outbuf = gst_rtp_h264_complete_au (rtph264depay, &out_timestamp,
812 /* no merge, output is input nal */
813 GST_DEBUG_OBJECT (depayload, "using NAL as output");
815 gst_buffer_unmap (nal, &map);
819 /* prepend codec_data */
820 if (rtph264depay->codec_data) {
821 GST_DEBUG_OBJECT (depayload, "prepending codec_data");
822 outbuf = gst_buffer_append (rtph264depay->codec_data, outbuf);
823 rtph264depay->codec_data = NULL;
826 outbuf = gst_buffer_make_writable (outbuf);
828 GST_BUFFER_TIMESTAMP (outbuf) = out_timestamp;
831 GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
833 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
841 GST_WARNING_OBJECT (depayload, "dropping short NAL");
842 gst_buffer_unmap (nal, &map);
843 gst_buffer_unref (nal);
849 gst_rtp_h264_push_fragmentation_unit (GstRtpH264Depay * rtph264depay,
856 outsize = gst_adapter_available (rtph264depay->adapter);
857 outbuf = gst_adapter_take_buffer (rtph264depay->adapter, outsize);
859 gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
860 GST_DEBUG_OBJECT (rtph264depay, "output %d bytes", outsize);
862 if (rtph264depay->byte_stream) {
863 memcpy (map.data, sync_bytes, sizeof (sync_bytes));
866 map.data[0] = (outsize >> 24);
867 map.data[1] = (outsize >> 16);
868 map.data[2] = (outsize >> 8);
869 map.data[3] = (outsize);
871 gst_buffer_unmap (outbuf, &map);
873 rtph264depay->current_fu_type = 0;
876 outbuf = gst_rtp_h264_depay_handle_nal (rtph264depay, outbuf,
877 rtph264depay->fu_timestamp, rtph264depay->fu_marker);
879 gst_rtp_base_depayload_push (GST_RTP_BASE_DEPAYLOAD (rtph264depay),
883 return gst_rtp_h264_depay_handle_nal (rtph264depay, outbuf,
884 rtph264depay->fu_timestamp, rtph264depay->fu_marker);
889 gst_rtp_h264_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
891 GstRtpH264Depay *rtph264depay;
892 GstBuffer *outbuf = NULL;
893 guint8 nal_unit_type;
894 GstRTPBuffer rtp = { NULL };
896 rtph264depay = GST_RTP_H264_DEPAY (depayload);
898 /* flush remaining data on discont */
899 if (GST_BUFFER_IS_DISCONT (buf)) {
900 gst_adapter_clear (rtph264depay->adapter);
901 rtph264depay->wait_start = TRUE;
902 rtph264depay->current_fu_type = 0;
911 guint outsize, nalu_size;
912 GstClockTime timestamp;
915 timestamp = GST_BUFFER_TIMESTAMP (buf);
917 gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);
919 payload_len = gst_rtp_buffer_get_payload_len (&rtp);
920 payload = gst_rtp_buffer_get_payload (&rtp);
921 marker = gst_rtp_buffer_get_marker (&rtp);
923 GST_DEBUG_OBJECT (rtph264depay, "receiving %d bytes", payload_len);
925 if (payload_len == 0)
936 nal_ref_idc = (payload[0] & 0x60) >> 5;
937 nal_unit_type = payload[0] & 0x1f;
939 /* at least one byte header with type */
942 GST_DEBUG_OBJECT (rtph264depay, "NRI %d, Type %d", nal_ref_idc,
945 /* If FU unit was being processed, but the current nal is of a different
946 * type. Assume that the remote payloader is buggy (didn't set the end bit
947 * when the FU ended) and send out what we gathered thusfar */
948 if (G_UNLIKELY (rtph264depay->current_fu_type != 0 &&
949 nal_unit_type != rtph264depay->current_fu_type))
950 gst_rtp_h264_push_fragmentation_unit (rtph264depay, TRUE);
952 switch (nal_unit_type) {
959 /* STAP-B Single-time aggregation packet 5.7.1 */
960 /* 2 byte extra header for DON */
966 payload += header_len;
967 payload_len -= header_len;
969 rtph264depay->wait_start = FALSE;
972 /* STAP-A Single-time aggregation packet 5.7.1 */
973 while (payload_len > 2) {
975 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
976 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
978 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
980 nalu_size = (payload[0] << 8) | payload[1];
982 /* dont include nalu_size */
983 if (nalu_size > (payload_len - 2))
984 nalu_size = payload_len - 2;
986 outsize = nalu_size + sizeof (sync_bytes);
987 outbuf = gst_buffer_new_and_alloc (outsize);
989 gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
990 if (rtph264depay->byte_stream) {
991 memcpy (map.data, sync_bytes, sizeof (sync_bytes));
993 map.data[0] = map.data[1] = 0;
994 map.data[2] = payload[0];
995 map.data[3] = payload[1];
998 /* strip NALU size */
1002 memcpy (map.data + sizeof (sync_bytes), payload, nalu_size);
1003 gst_buffer_unmap (outbuf, &map);
1005 gst_adapter_push (rtph264depay->adapter, outbuf);
1007 payload += nalu_size;
1008 payload_len -= nalu_size;
1011 outsize = gst_adapter_available (rtph264depay->adapter);
1012 outbuf = gst_adapter_take_buffer (rtph264depay->adapter, outsize);
1014 outbuf = gst_rtp_h264_depay_handle_nal (rtph264depay, outbuf, timestamp,
1019 /* MTAP16 Multi-time aggregation packet 5.7.2 */
1021 /* fallthrough, not implemented */
1023 /* MTAP24 Multi-time aggregation packet 5.7.2 */
1025 goto not_implemented;
1030 /* FU-A Fragmentation unit 5.8 */
1031 /* FU-B Fragmentation unit 5.8 */
1034 /* +---------------+
1040 * R is reserved and always 0
1042 S = (payload[1] & 0x80) == 0x80;
1043 E = (payload[1] & 0x40) == 0x40;
1045 GST_DEBUG_OBJECT (rtph264depay, "S %d, E %d", S, E);
1047 if (rtph264depay->wait_start && !S)
1051 /* NAL unit starts here */
1054 /* If a new FU unit started, while still processing an older one.
1055 * Assume that the remote payloader is buggy (doesn't set the end
1056 * bit) and send out what we've gathered thusfar */
1057 if (G_UNLIKELY (rtph264depay->current_fu_type != 0))
1058 gst_rtp_h264_push_fragmentation_unit (rtph264depay, TRUE);
1060 rtph264depay->current_fu_type = nal_unit_type;
1061 rtph264depay->fu_timestamp = timestamp;
1063 rtph264depay->wait_start = FALSE;
1065 /* reconstruct NAL header */
1066 nal_header = (payload[0] & 0xe0) | (payload[1] & 0x1f);
1068 /* strip type header, keep FU header, we'll reuse it to reconstruct
1069 * the NAL header. */
1073 nalu_size = payload_len;
1074 outsize = nalu_size + sizeof (sync_bytes);
1075 outbuf = gst_buffer_new_and_alloc (outsize);
1077 gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
1078 memcpy (map.data + sizeof (sync_bytes), payload, nalu_size);
1079 map.data[sizeof (sync_bytes)] = nal_header;
1080 gst_buffer_unmap (outbuf, &map);
1082 GST_DEBUG_OBJECT (rtph264depay, "queueing %d bytes", outsize);
1084 /* and assemble in the adapter */
1085 gst_adapter_push (rtph264depay->adapter, outbuf);
1087 /* strip off FU indicator and FU header bytes */
1091 outsize = payload_len;
1092 outbuf = gst_buffer_new_and_alloc (outsize);
1093 gst_buffer_fill (outbuf, 0, payload, outsize);
1095 GST_DEBUG_OBJECT (rtph264depay, "queueing %d bytes", outsize);
1097 /* and assemble in the adapter */
1098 gst_adapter_push (rtph264depay->adapter, outbuf);
1102 rtph264depay->fu_marker = marker;
1104 /* if NAL unit ends, flush the adapter */
1106 outbuf = gst_rtp_h264_push_fragmentation_unit (rtph264depay, FALSE);
1111 rtph264depay->wait_start = FALSE;
1113 /* 1-23 NAL unit Single NAL unit packet per H.264 5.6 */
1114 /* the entire payload is the output buffer */
1115 nalu_size = payload_len;
1116 outsize = nalu_size + sizeof (sync_bytes);
1117 outbuf = gst_buffer_new_and_alloc (outsize);
1119 gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
1120 if (rtph264depay->byte_stream) {
1121 memcpy (map.data, sync_bytes, sizeof (sync_bytes));
1123 map.data[0] = map.data[1] = 0;
1124 map.data[2] = nalu_size >> 8;
1125 map.data[3] = nalu_size & 0xff;
1127 memcpy (map.data + sizeof (sync_bytes), payload, nalu_size);
1128 gst_buffer_unmap (outbuf, &map);
1130 outbuf = gst_rtp_h264_depay_handle_nal (rtph264depay, outbuf, timestamp,
1135 gst_rtp_buffer_unmap (&rtp);
1143 GST_DEBUG_OBJECT (rtph264depay, "empty packet");
1144 gst_rtp_buffer_unmap (&rtp);
1149 GST_ELEMENT_WARNING (rtph264depay, STREAM, DECODE,
1150 (NULL), ("Undefined packet type"));
1151 gst_rtp_buffer_unmap (&rtp);
1156 GST_DEBUG_OBJECT (rtph264depay, "waiting for start");
1157 gst_rtp_buffer_unmap (&rtp);
1162 GST_ELEMENT_ERROR (rtph264depay, STREAM, FORMAT,
1163 (NULL), ("NAL unit type %d not supported yet", nal_unit_type));
1164 gst_rtp_buffer_unmap (&rtp);
1170 gst_rtp_h264_depay_handle_event (GstRTPBaseDepayload * depay, GstEvent * event)
1172 GstRtpH264Depay *rtph264depay;
1174 rtph264depay = GST_RTP_H264_DEPAY (depay);
1176 switch (GST_EVENT_TYPE (event)) {
1177 case GST_EVENT_FLUSH_STOP:
1178 gst_rtp_h264_depay_reset (rtph264depay);
1185 GST_RTP_BASE_DEPAYLOAD_CLASS (parent_class)->handle_event (depay, event);
1188 static GstStateChangeReturn
1189 gst_rtp_h264_depay_change_state (GstElement * element,
1190 GstStateChange transition)
1192 GstRtpH264Depay *rtph264depay;
1193 GstStateChangeReturn ret;
1195 rtph264depay = GST_RTP_H264_DEPAY (element);
1197 switch (transition) {
1198 case GST_STATE_CHANGE_NULL_TO_READY:
1200 case GST_STATE_CHANGE_READY_TO_PAUSED:
1201 gst_rtp_h264_depay_reset (rtph264depay);
1207 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1209 switch (transition) {
1210 case GST_STATE_CHANGE_READY_TO_NULL:
1219 gst_rtp_h264_depay_plugin_init (GstPlugin * plugin)
1221 return gst_element_register (plugin, "rtph264depay",
1222 GST_RANK_SECONDARY, GST_TYPE_RTP_H264_DEPAY);