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., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
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"
34 GST_DEBUG_CATEGORY_STATIC (rtph264depay_debug);
35 #define GST_CAT_DEFAULT (rtph264depay_debug)
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
42 /* 3 zero bytes syncword */
43 static const guint8 sync_bytes[] = { 0, 0, 0, 1 };
45 static GstStaticPadTemplate gst_rtp_h264_depay_src_template =
46 GST_STATIC_PAD_TEMPLATE ("src",
49 GST_STATIC_CAPS ("video/x-h264, "
50 "stream-format = (string) avc, alignment = (string) au; "
52 "stream-format = (string) byte-stream, alignment = (string) { nal, au }")
55 static GstStaticPadTemplate gst_rtp_h264_depay_sink_template =
56 GST_STATIC_PAD_TEMPLATE ("sink",
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 " */
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);
85 static void gst_rtp_h264_depay_finalize (GObject * object);
87 static GstStateChangeReturn gst_rtp_h264_depay_change_state (GstElement *
88 element, GstStateChange transition);
90 static GstBuffer *gst_rtp_h264_depay_process (GstRTPBaseDepayload * depayload,
92 static gboolean gst_rtp_h264_depay_setcaps (GstRTPBaseDepayload * filter,
94 static gboolean gst_rtp_h264_depay_handle_event (GstRTPBaseDepayload * depay,
98 gst_rtp_h264_depay_class_init (GstRtpH264DepayClass * klass)
100 GObjectClass *gobject_class;
101 GstElementClass *gstelement_class;
102 GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
104 gobject_class = (GObjectClass *) klass;
105 gstelement_class = (GstElementClass *) klass;
106 gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
108 gobject_class->finalize = gst_rtp_h264_depay_finalize;
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);
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;
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;
127 gst_rtp_h264_depay_init (GstRtpH264Depay * rtph264depay)
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);
140 gst_rtp_h264_depay_reset (GstRtpH264Depay * rtph264depay, gboolean hard)
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);
154 if (rtph264depay->allocator != NULL) {
155 gst_object_unref (rtph264depay->allocator);
156 rtph264depay->allocator = NULL;
158 gst_allocation_params_init (&rtph264depay->params);
163 gst_rtp_h264_depay_finalize (GObject * object)
165 GstRtpH264Depay *rtph264depay;
167 rtph264depay = GST_RTP_H264_DEPAY (object);
169 if (rtph264depay->codec_data)
170 gst_buffer_unref (rtph264depay->codec_data);
172 g_object_unref (rtph264depay->adapter);
173 g_object_unref (rtph264depay->picture_adapter);
175 g_ptr_array_free (rtph264depay->sps, TRUE);
176 g_ptr_array_free (rtph264depay->pps, TRUE);
178 G_OBJECT_CLASS (parent_class)->finalize (object);
182 gst_rtp_h264_depay_negotiate (GstRtpH264Depay * rtph264depay)
185 gint byte_stream = -1;
189 gst_pad_get_allowed_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (rtph264depay));
191 GST_DEBUG_OBJECT (rtph264depay, "allowed caps: %" GST_PTR_FORMAT, caps);
194 if (gst_caps_get_size (caps) > 0) {
195 GstStructure *s = gst_caps_get_structure (caps, 0);
196 const gchar *str = NULL;
198 if ((str = gst_structure_get_string (s, "stream-format"))) {
199 if (strcmp (str, "avc") == 0) {
201 } else if (strcmp (str, "byte-stream") == 0) {
204 GST_DEBUG_OBJECT (rtph264depay, "unknown stream-format: %s", str);
208 if ((str = gst_structure_get_string (s, "alignment"))) {
209 if (strcmp (str, "au") == 0) {
211 } else if (strcmp (str, "nal") == 0) {
214 GST_DEBUG_OBJECT (rtph264depay, "unknown alignment: %s", str);
218 gst_caps_unref (caps);
221 if (byte_stream != -1) {
222 GST_DEBUG_OBJECT (rtph264depay, "downstream requires byte-stream %d",
224 rtph264depay->byte_stream = byte_stream;
226 GST_DEBUG_OBJECT (rtph264depay, "defaulting to byte-stream %d",
227 DEFAULT_BYTE_STREAM);
228 rtph264depay->byte_stream = DEFAULT_BYTE_STREAM;
231 GST_DEBUG_OBJECT (rtph264depay, "downstream requires merge %d", merge);
232 rtph264depay->merge = merge;
234 GST_DEBUG_OBJECT (rtph264depay, "defaulting to merge %d",
235 DEFAULT_ACCESS_UNIT);
236 rtph264depay->merge = DEFAULT_ACCESS_UNIT;
241 parse_sps (GstMapInfo * map, guint32 * sps_id)
243 GstBitReader br = GST_BIT_READER_INIT (map->data + 4,
249 if (!gst_rtp_read_golomb (&br, sps_id))
256 parse_pps (GstMapInfo * map, guint32 * sps_id, guint32 * pps_id)
258 GstBitReader br = GST_BIT_READER_INIT (map->data + 1,
264 if (!gst_rtp_read_golomb (&br, pps_id))
266 if (!gst_rtp_read_golomb (&br, sps_id))
273 gst_rtp_h264_depay_set_output_caps (GstRtpH264Depay * rtph264depay,
276 GstAllocationParams params;
277 GstAllocator *allocator = NULL;
281 gst_allocation_params_init (¶ms);
283 srcpad = GST_RTP_BASE_DEPAYLOAD_SRCPAD (rtph264depay);
284 res = gst_pad_set_caps (srcpad, caps);
288 query = gst_query_new_allocation (caps, TRUE);
289 if (!gst_pad_peer_query (srcpad, query)) {
290 GST_DEBUG_OBJECT (rtph264depay, "downstream ALLOCATION query failed");
293 if (gst_query_get_n_allocation_params (query) > 0) {
294 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
297 gst_query_unref (query);
300 if (rtph264depay->allocator)
301 gst_object_unref (rtph264depay->allocator);
303 rtph264depay->allocator = allocator;
304 rtph264depay->params = params;
310 gst_rtp_h264_set_src_caps (GstRtpH264Depay * rtph264depay)
317 if (!rtph264depay->byte_stream &&
318 (!rtph264depay->new_codec_data ||
319 rtph264depay->sps->len == 0 || rtph264depay->pps->len == 0))
322 srccaps = gst_caps_new_simple ("video/x-h264",
323 "stream-format", G_TYPE_STRING,
324 rtph264depay->byte_stream ? "byte-stream" : "avc",
325 "alignment", G_TYPE_STRING, rtph264depay->merge ? "au" : "nal", NULL);
327 if (!rtph264depay->byte_stream) {
328 GstBuffer *codec_data;
336 guchar profile_compat = G_MAXUINT8;
338 /* start with 7 bytes header */
340 /* count sps & pps */
341 for (i = 0; i < rtph264depay->sps->len; i++)
342 len += 2 + gst_buffer_get_size (g_ptr_array_index (rtph264depay->sps, i));
343 for (i = 0; i < rtph264depay->pps->len; i++)
344 len += 2 + gst_buffer_get_size (g_ptr_array_index (rtph264depay->pps, i));
346 codec_data = gst_buffer_new_and_alloc (len);
347 gst_buffer_map (codec_data, &map, GST_MAP_READWRITE);
350 /* 8 bits version == 1 */
353 /* According to: ISO/IEC 14496-15:2004(E) section 5.2.4.1
354 * The level is the max level of all SPSes
355 * A profile compat bit can only be set if all SPSes include that bit
357 for (i = 0; i < rtph264depay->sps->len; i++) {
358 gst_buffer_map (g_ptr_array_index (rtph264depay->sps, i), &nalmap,
360 profile_compat &= nalmap.data[2];
361 level = MAX (level, nalmap.data[3]);
362 gst_buffer_unmap (g_ptr_array_index (rtph264depay->sps, i), &nalmap);
365 /* Assume all SPSes use the same profile, so extract from the first SPS */
366 gst_buffer_map (g_ptr_array_index (rtph264depay->sps, 0), &nalmap,
368 *data++ = nalmap.data[1];
369 gst_buffer_unmap (g_ptr_array_index (rtph264depay->sps, 0), &nalmap);
370 *data++ = profile_compat;
373 /* 6 bits reserved | 2 bits lengthSizeMinusOn */
375 /* 3 bits reserved | 5 bits numOfSequenceParameterSets */
376 *data++ = 0xe0 | (rtph264depay->sps->len & 0x1f);
379 for (i = 0; i < rtph264depay->sps->len; i++) {
380 gst_buffer_map (g_ptr_array_index (rtph264depay->sps, i), &nalmap,
383 GST_DEBUG_OBJECT (rtph264depay, "copy SPS %d of length %u", i,
384 (guint) nalmap.size);
385 GST_WRITE_UINT16_BE (data, nalmap.size);
387 memcpy (data, nalmap.data, nalmap.size);
389 gst_buffer_unmap (g_ptr_array_index (rtph264depay->sps, i), &nalmap);
392 /* 8 bits numOfPictureParameterSets */
393 *data++ = rtph264depay->pps->len;
395 for (i = 0; i < rtph264depay->pps->len; i++) {
396 gst_buffer_map (g_ptr_array_index (rtph264depay->pps, i), &nalmap,
399 GST_DEBUG_OBJECT (rtph264depay, "copy PPS %d of length %u", i,
400 (guint) nalmap.size);
401 GST_WRITE_UINT16_BE (data, nalmap.size);
403 memcpy (data, nalmap.data, nalmap.size);
405 gst_buffer_unmap (g_ptr_array_index (rtph264depay->pps, i), &nalmap);
408 new_size = data - map.data;
409 gst_buffer_unmap (codec_data, &map);
410 gst_buffer_set_size (codec_data, new_size);
412 gst_caps_set_simple (srccaps,
413 "codec_data", GST_TYPE_BUFFER, codec_data, NULL);
414 gst_buffer_unref (codec_data);
417 /* Set profile a level from SPS */
420 GstBuffer *max_level_sps = NULL;
424 /* Get the SPS with the highest level. We assume
425 * all SPS have the same profile */
426 for (i = 0; i < rtph264depay->sps->len; i++) {
427 gst_buffer_map (g_ptr_array_index (rtph264depay->sps, i), &nalmap,
429 if (level == 0 || level < nalmap.data[3]) {
430 max_level_sps = g_ptr_array_index (rtph264depay->sps, i);
431 level = nalmap.data[3];
433 gst_buffer_unmap (g_ptr_array_index (rtph264depay->sps, i), &nalmap);
437 gst_buffer_map (max_level_sps, &nalmap, GST_MAP_READ);
438 gst_codec_utils_h264_caps_set_level_and_profile (srccaps, nalmap.data + 1,
440 gst_buffer_unmap (max_level_sps, &nalmap);
444 srcpad = GST_RTP_BASE_DEPAYLOAD_SRCPAD (rtph264depay);
446 old_caps = gst_pad_get_current_caps (srcpad);
448 if (old_caps == NULL || !gst_caps_is_equal (srccaps, old_caps)) {
449 res = gst_rtp_h264_depay_set_output_caps (rtph264depay, srccaps);
452 gst_caps_unref (srccaps);
454 /* Insert SPS and PPS into the stream on next opportunity (if bytestream) */
455 if (rtph264depay->byte_stream
456 && (rtph264depay->sps->len > 0 || rtph264depay->pps->len > 0)) {
458 GstBuffer *codec_data;
463 for (i = 0; i < rtph264depay->sps->len; i++) {
464 len += 4 + gst_buffer_get_size (g_ptr_array_index (rtph264depay->sps, i));
467 for (i = 0; i < rtph264depay->pps->len; i++) {
468 len += 4 + gst_buffer_get_size (g_ptr_array_index (rtph264depay->pps, i));
471 codec_data = gst_buffer_new_and_alloc (len);
472 gst_buffer_map (codec_data, &map, GST_MAP_WRITE);
475 for (i = 0; i < rtph264depay->sps->len; i++) {
476 GstBuffer *sps_buf = g_ptr_array_index (rtph264depay->sps, i);
477 guint sps_size = gst_buffer_get_size (sps_buf);
479 if (rtph264depay->byte_stream)
480 memcpy (data, sync_bytes, sizeof (sync_bytes));
482 GST_WRITE_UINT32_BE (data, sps_size);
483 gst_buffer_extract (sps_buf, 0, data + 4, -1);
484 data += 4 + sps_size;
487 for (i = 0; i < rtph264depay->pps->len; i++) {
488 GstBuffer *pps_buf = g_ptr_array_index (rtph264depay->pps, i);
489 guint pps_size = gst_buffer_get_size (pps_buf);
491 if (rtph264depay->byte_stream)
492 memcpy (data, sync_bytes, sizeof (sync_bytes));
494 GST_WRITE_UINT32_BE (data, pps_size);
495 gst_buffer_extract (pps_buf, 0, data + 4, -1);
496 data += 4 + pps_size;
499 gst_buffer_unmap (codec_data, &map);
500 if (rtph264depay->codec_data)
501 gst_buffer_unref (rtph264depay->codec_data);
502 rtph264depay->codec_data = codec_data;
506 rtph264depay->new_codec_data = FALSE;
512 gst_rtp_h264_add_sps_pps (GstElement * rtph264, GPtrArray * sps_array,
513 GPtrArray * pps_array, GstBuffer * nal)
519 gst_buffer_map (nal, &map, GST_MAP_READ);
521 type = map.data[0] & 0x1f;
526 if (!parse_sps (&map, &sps_id)) {
527 GST_WARNING_OBJECT (rtph264, "Invalid SPS,"
528 " can't parse seq_parameter_set_id");
532 for (i = 0; i < sps_array->len; i++) {
533 GstBuffer *sps = g_ptr_array_index (sps_array, i);
537 gst_buffer_map (sps, &spsmap, GST_MAP_READ);
538 parse_sps (&spsmap, &tmp_sps_id);
540 if (sps_id == tmp_sps_id) {
541 if (map.size == spsmap.size &&
542 memcmp (map.data, spsmap.data, spsmap.size) == 0) {
543 GST_LOG_OBJECT (rtph264, "Unchanged SPS %u, not updating", sps_id);
544 gst_buffer_unmap (sps, &spsmap);
547 gst_buffer_unmap (sps, &spsmap);
548 g_ptr_array_remove_index_fast (sps_array, i);
549 g_ptr_array_add (sps_array, nal);
550 GST_LOG_OBJECT (rtph264, "Modified SPS %u, replacing", sps_id);
554 gst_buffer_unmap (sps, &spsmap);
556 GST_LOG_OBJECT (rtph264, "Adding new SPS %u", sps_id);
557 g_ptr_array_add (sps_array, nal);
558 } else if (type == 8) {
562 if (!parse_pps (&map, &sps_id, &pps_id)) {
563 GST_WARNING_OBJECT (rtph264, "Invalid PPS,"
564 " can't parse seq_parameter_set_id or pic_parameter_set_id");
568 for (i = 0; i < pps_array->len; i++) {
569 GstBuffer *pps = g_ptr_array_index (pps_array, i);
575 gst_buffer_map (pps, &ppsmap, GST_MAP_READ);
576 parse_pps (&ppsmap, &tmp_sps_id, &tmp_pps_id);
578 if (pps_id == tmp_pps_id) {
579 if (map.size == ppsmap.size &&
580 memcmp (map.data, ppsmap.data, ppsmap.size) == 0) {
581 GST_LOG_OBJECT (rtph264, "Unchanged PPS %u:%u, not updating", sps_id,
583 gst_buffer_unmap (pps, &ppsmap);
586 gst_buffer_unmap (pps, &ppsmap);
587 g_ptr_array_remove_index_fast (pps_array, i);
588 g_ptr_array_add (pps_array, nal);
589 GST_LOG_OBJECT (rtph264, "Modified PPS %u:%u, replacing",
594 gst_buffer_unmap (pps, &ppsmap);
596 GST_LOG_OBJECT (rtph264, "Adding new PPS %u:%i", sps_id, pps_id);
597 g_ptr_array_add (pps_array, nal);
603 gst_buffer_unmap (nal, &map);
608 gst_buffer_unmap (nal, &map);
609 gst_buffer_unref (nal);
616 gst_rtp_h264_depay_add_sps_pps (GstRtpH264Depay * rtph264depay, GstBuffer * nal)
618 if (gst_rtp_h264_add_sps_pps (GST_ELEMENT (rtph264depay),
619 rtph264depay->sps, rtph264depay->pps, nal))
620 rtph264depay->new_codec_data = TRUE;
624 gst_rtp_h264_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
627 GstStructure *structure = gst_caps_get_structure (caps, 0);
628 GstRtpH264Depay *rtph264depay;
630 GstBuffer *codec_data;
634 rtph264depay = GST_RTP_H264_DEPAY (depayload);
636 if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
638 depayload->clock_rate = clock_rate;
640 /* Base64 encoded, comma separated config NALs */
641 ps = gst_structure_get_string (structure, "sprop-parameter-sets");
643 /* negotiate with downstream w.r.t. output format and alignment */
644 gst_rtp_h264_depay_negotiate (rtph264depay);
646 if (rtph264depay->byte_stream && ps != NULL) {
647 /* for bytestream we only need the parameter sets but we don't error out
648 * when they are not there, we assume they are in the stream. */
653 params = g_strsplit (ps, ",", 0);
655 /* count total number of bytes in base64. Also include the sync bytes in
656 * front of the params. */
658 for (i = 0; params[i]; i++) {
659 len += strlen (params[i]);
660 len += sizeof (sync_bytes);
662 /* we seriously overshoot the length, but it's fine. */
663 codec_data = gst_buffer_new_and_alloc (len);
665 gst_buffer_map (codec_data, &map, GST_MAP_WRITE);
668 for (i = 0; params[i]; i++) {
672 GST_DEBUG_OBJECT (depayload, "decoding param %d (%s)", i, params[i]);
673 memcpy (ptr, sync_bytes, sizeof (sync_bytes));
674 ptr += sizeof (sync_bytes);
676 g_base64_decode_step (params[i], strlen (params[i]), ptr, &state,
678 GST_DEBUG_OBJECT (depayload, "decoded %d bytes", len);
679 total += len + sizeof (sync_bytes);
682 gst_buffer_unmap (codec_data, &map);
683 gst_buffer_resize (codec_data, 0, total);
686 /* keep the codec_data, we need to send it as the first buffer. We cannot
687 * push it in the adapter because the adapter might be flushed on discont.
689 if (rtph264depay->codec_data)
690 gst_buffer_unref (rtph264depay->codec_data);
691 rtph264depay->codec_data = codec_data;
692 } else if (!rtph264depay->byte_stream) {
697 goto incomplete_caps;
699 params = g_strsplit (ps, ",", 0);
701 GST_DEBUG_OBJECT (depayload, "we have %d params", g_strv_length (params));
703 /* start with 7 bytes header */
704 for (i = 0; params[i]; i++) {
711 nal_len = strlen (params[i]);
713 GST_WARNING_OBJECT (depayload, "empty param '%s' (#%d)", params[i], i);
716 nal = gst_buffer_new_and_alloc (nal_len);
717 gst_buffer_map (nal, &nalmap, GST_MAP_READWRITE);
720 g_base64_decode_step (params[i], nal_len, nalmap.data, &state, &save);
722 GST_DEBUG_OBJECT (depayload, "adding param %d as %s", i,
723 ((nalmap.data[0] & 0x1f) == 7) ? "SPS" : "PPS");
725 gst_buffer_unmap (nal, &nalmap);
726 gst_buffer_set_size (nal, nal_len);
728 gst_rtp_h264_depay_add_sps_pps (rtph264depay, nal);
732 if (rtph264depay->sps->len == 0 || rtph264depay->pps->len == 0)
733 goto incomplete_caps;
736 return gst_rtp_h264_set_src_caps (rtph264depay);
741 GST_DEBUG_OBJECT (depayload, "we have incomplete caps,"
742 " doing setcaps later");
748 gst_rtp_h264_depay_allocate_output_buffer (GstRtpH264Depay * depay, gsize size)
750 GstBuffer *buffer = NULL;
752 g_return_val_if_fail (size > 0, NULL);
754 GST_LOG_OBJECT (depay, "want output buffer of %u bytes", (guint) size);
756 buffer = gst_buffer_new_allocate (depay->allocator, size, &depay->params);
757 if (buffer == NULL) {
758 GST_INFO_OBJECT (depay, "couldn't allocate output buffer");
759 buffer = gst_buffer_new_allocate (NULL, size, NULL);
766 gst_rtp_h264_complete_au (GstRtpH264Depay * rtph264depay,
767 GstClockTime * out_timestamp, gboolean * out_keyframe)
772 guint outsize, offset = 0;
773 gint b, n_bufs, m, n_mem;
775 /* we had a picture in the adapter and we completed it */
776 GST_DEBUG_OBJECT (rtph264depay, "taking completed AU");
777 outsize = gst_adapter_available (rtph264depay->picture_adapter);
779 outbuf = gst_rtp_h264_depay_allocate_output_buffer (rtph264depay, outsize);
784 if (!gst_buffer_map (outbuf, &outmap, GST_MAP_WRITE))
787 list = gst_adapter_take_buffer_list (rtph264depay->picture_adapter, outsize);
789 n_bufs = gst_buffer_list_length (list);
790 for (b = 0; b < n_bufs; ++b) {
791 GstBuffer *buf = gst_buffer_list_get (list, b);
793 n_mem = gst_buffer_n_memory (buf);
794 for (m = 0; m < n_mem; ++m) {
795 GstMemory *mem = gst_buffer_peek_memory (buf, m);
796 gsize mem_size = gst_memory_get_sizes (mem, NULL, NULL);
799 if (gst_memory_map (mem, &mem_map, GST_MAP_READ)) {
800 memcpy (outmap.data + offset, mem_map.data, mem_size);
801 gst_memory_unmap (mem, &mem_map);
803 memset (outmap.data + offset, 0, mem_size);
808 gst_rtp_copy_video_meta (rtph264depay, outbuf, buf);
810 gst_buffer_list_unref (list);
811 gst_buffer_unmap (outbuf, &outmap);
813 *out_timestamp = rtph264depay->last_ts;
814 *out_keyframe = rtph264depay->last_keyframe;
816 rtph264depay->last_keyframe = FALSE;
817 rtph264depay->picture_start = FALSE;
822 /* SPS/PPS/IDR considered key, all others DELTA;
823 * so downstream waiting for keyframe can pick up at SPS/PPS/IDR */
824 #define NAL_TYPE_IS_KEY(nt) (((nt) == 5) || ((nt) == 7) || ((nt) == 8))
827 gst_rtp_h264_depay_handle_nal (GstRtpH264Depay * rtph264depay, GstBuffer * nal,
828 GstClockTime in_timestamp, gboolean marker)
830 GstRTPBaseDepayload *depayload = GST_RTP_BASE_DEPAYLOAD (rtph264depay);
833 GstBuffer *outbuf = NULL;
834 GstClockTime out_timestamp;
835 gboolean keyframe, out_keyframe;
837 gst_buffer_map (nal, &map, GST_MAP_READ);
838 if (G_UNLIKELY (map.size < 5))
841 nal_type = map.data[4] & 0x1f;
842 GST_DEBUG_OBJECT (rtph264depay, "handle NAL type %d", nal_type);
844 keyframe = NAL_TYPE_IS_KEY (nal_type);
846 out_keyframe = keyframe;
847 out_timestamp = in_timestamp;
849 if (!rtph264depay->byte_stream) {
850 if (nal_type == 7 || nal_type == 8) {
851 gst_rtp_h264_depay_add_sps_pps (rtph264depay,
852 gst_buffer_copy_region (nal, GST_BUFFER_COPY_ALL,
853 4, gst_buffer_get_size (nal) - 4));
854 gst_buffer_unmap (nal, &map);
855 gst_buffer_unref (nal);
857 } else if (rtph264depay->sps->len == 0 || rtph264depay->pps->len == 0) {
858 /* Down push down any buffer in non-bytestream mode if the SPS/PPS haven't
861 gst_pad_push_event (GST_RTP_BASE_DEPAYLOAD_SINKPAD (depayload),
862 gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM,
863 gst_structure_new ("GstForceKeyUnit",
864 "all-headers", G_TYPE_BOOLEAN, TRUE, NULL)));
865 gst_buffer_unmap (nal, &map);
866 gst_buffer_unref (nal);
870 if (rtph264depay->new_codec_data &&
871 rtph264depay->sps->len > 0 && rtph264depay->pps->len > 0)
872 gst_rtp_h264_set_src_caps (rtph264depay);
876 if (rtph264depay->merge) {
877 gboolean start = FALSE, complete = FALSE;
879 /* marker bit isn't mandatory so in the following code we try to guess
880 * an AU boundary by detecting a new picture start */
882 /* consider a coded slices (IDR or not) to start a picture,
883 * (so ending the previous one) if first_mb_in_slice == 0
884 * (non-0 is part of previous one) */
885 /* NOTE this is not entirely according to Access Unit specs in 7.4.1.2.4,
886 * but in practice it works in sane cases, needs not much parsing,
887 * and also works with broken frame_num in NAL (where spec-wise would fail) */
888 /* FIXME: this code isn't correct for interlaced content as AUs should be
889 * constructed with pairs of fields and the guess here will just push out
890 * AUs with a single field in it */
891 if (nal_type == 1 || nal_type == 2 || nal_type == 5) {
892 /* we have a picture start */
894 if (map.data[5] & 0x80) {
895 /* first_mb_in_slice == 0 completes a picture */
898 } else if (nal_type >= 6 && nal_type <= 9) {
899 /* SEI, SPS, PPS, AU terminate picture */
902 GST_DEBUG_OBJECT (depayload, "start %d, complete %d", start, complete);
904 if (complete && rtph264depay->picture_start)
905 outbuf = gst_rtp_h264_complete_au (rtph264depay, &out_timestamp,
909 gst_buffer_unmap (nal, &map);
911 GST_DEBUG_OBJECT (depayload, "adding NAL to picture adapter");
912 gst_adapter_push (rtph264depay->picture_adapter, nal);
913 rtph264depay->last_ts = in_timestamp;
914 rtph264depay->last_keyframe |= keyframe;
915 rtph264depay->picture_start |= start;
918 outbuf = gst_rtp_h264_complete_au (rtph264depay, &out_timestamp,
921 /* no merge, output is input nal */
922 GST_DEBUG_OBJECT (depayload, "using NAL as output");
924 gst_buffer_unmap (nal, &map);
928 /* prepend codec_data */
929 if (rtph264depay->codec_data) {
930 GST_DEBUG_OBJECT (depayload, "prepending codec_data");
931 gst_rtp_copy_video_meta (rtph264depay, rtph264depay->codec_data, outbuf);
932 outbuf = gst_buffer_append (rtph264depay->codec_data, outbuf);
933 rtph264depay->codec_data = NULL;
936 outbuf = gst_buffer_make_writable (outbuf);
938 gst_rtp_drop_non_video_meta (rtph264depay, outbuf);
940 GST_BUFFER_PTS (outbuf) = out_timestamp;
943 GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
945 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
948 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_MARKER);
950 gst_rtp_base_depayload_push (depayload, outbuf);
958 GST_WARNING_OBJECT (depayload, "dropping short NAL");
959 gst_buffer_unmap (nal, &map);
960 gst_buffer_unref (nal);
966 gst_rtp_h264_finish_fragmentation_unit (GstRtpH264Depay * rtph264depay)
972 outsize = gst_adapter_available (rtph264depay->adapter);
973 outbuf = gst_adapter_take_buffer (rtph264depay->adapter, outsize);
975 gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
976 GST_DEBUG_OBJECT (rtph264depay, "output %d bytes", outsize);
978 if (rtph264depay->byte_stream) {
979 memcpy (map.data, sync_bytes, sizeof (sync_bytes));
982 map.data[0] = (outsize >> 24);
983 map.data[1] = (outsize >> 16);
984 map.data[2] = (outsize >> 8);
985 map.data[3] = (outsize);
987 gst_buffer_unmap (outbuf, &map);
989 rtph264depay->current_fu_type = 0;
991 gst_rtp_h264_depay_handle_nal (rtph264depay, outbuf,
992 rtph264depay->fu_timestamp, rtph264depay->fu_marker);
996 gst_rtp_h264_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
998 GstRtpH264Depay *rtph264depay;
999 GstBuffer *outbuf = NULL;
1000 guint8 nal_unit_type;
1002 rtph264depay = GST_RTP_H264_DEPAY (depayload);
1004 /* flush remaining data on discont */
1005 if (GST_BUFFER_IS_DISCONT (rtp->buffer)) {
1006 gst_adapter_clear (rtph264depay->adapter);
1007 rtph264depay->wait_start = TRUE;
1008 rtph264depay->current_fu_type = 0;
1017 guint outsize, nalu_size;
1018 GstClockTime timestamp;
1021 timestamp = GST_BUFFER_PTS (rtp->buffer);
1023 payload_len = gst_rtp_buffer_get_payload_len (rtp);
1024 payload = gst_rtp_buffer_get_payload (rtp);
1025 marker = gst_rtp_buffer_get_marker (rtp);
1027 GST_DEBUG_OBJECT (rtph264depay, "receiving %d bytes", payload_len);
1029 if (payload_len == 0)
1032 /* +---------------+
1040 nal_ref_idc = (payload[0] & 0x60) >> 5;
1041 nal_unit_type = payload[0] & 0x1f;
1043 /* at least one byte header with type */
1046 GST_DEBUG_OBJECT (rtph264depay, "NRI %d, Type %d %s", nal_ref_idc,
1047 nal_unit_type, marker ? "marker" : "");
1049 /* If FU unit was being processed, but the current nal is of a different
1050 * type. Assume that the remote payloader is buggy (didn't set the end bit
1051 * when the FU ended) and send out what we gathered thusfar */
1052 if (G_UNLIKELY (rtph264depay->current_fu_type != 0 &&
1053 nal_unit_type != rtph264depay->current_fu_type))
1054 gst_rtp_h264_finish_fragmentation_unit (rtph264depay);
1056 switch (nal_unit_type) {
1061 goto undefined_type;
1063 /* STAP-B Single-time aggregation packet 5.7.1 */
1064 /* 2 byte extra header for DON */
1070 payload += header_len;
1071 payload_len -= header_len;
1073 rtph264depay->wait_start = FALSE;
1076 /* STAP-A Single-time aggregation packet 5.7.1 */
1077 while (payload_len > 2) {
1079 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
1080 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1082 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1084 nalu_size = (payload[0] << 8) | payload[1];
1086 /* dont include nalu_size */
1087 if (nalu_size > (payload_len - 2))
1088 nalu_size = payload_len - 2;
1090 outsize = nalu_size + sizeof (sync_bytes);
1091 outbuf = gst_buffer_new_and_alloc (outsize);
1093 gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
1094 if (rtph264depay->byte_stream) {
1095 memcpy (map.data, sync_bytes, sizeof (sync_bytes));
1097 map.data[0] = map.data[1] = 0;
1098 map.data[2] = payload[0];
1099 map.data[3] = payload[1];
1102 /* strip NALU size */
1106 memcpy (map.data + sizeof (sync_bytes), payload, nalu_size);
1107 gst_buffer_unmap (outbuf, &map);
1109 gst_rtp_copy_video_meta (rtph264depay, outbuf, rtp->buffer);
1111 gst_rtp_h264_depay_handle_nal (rtph264depay, outbuf, timestamp,
1114 payload += nalu_size;
1115 payload_len -= nalu_size;
1120 /* MTAP16 Multi-time aggregation packet 5.7.2 */
1122 /* fallthrough, not implemented */
1124 /* MTAP24 Multi-time aggregation packet 5.7.2 */
1126 goto not_implemented;
1131 /* FU-A Fragmentation unit 5.8 */
1132 /* FU-B Fragmentation unit 5.8 */
1135 /* +---------------+
1141 * R is reserved and always 0
1143 S = (payload[1] & 0x80) == 0x80;
1144 E = (payload[1] & 0x40) == 0x40;
1146 GST_DEBUG_OBJECT (rtph264depay, "S %d, E %d", S, E);
1148 if (rtph264depay->wait_start && !S)
1152 /* NAL unit starts here */
1155 /* If a new FU unit started, while still processing an older one.
1156 * Assume that the remote payloader is buggy (doesn't set the end
1157 * bit) and send out what we've gathered thusfar */
1158 if (G_UNLIKELY (rtph264depay->current_fu_type != 0))
1159 gst_rtp_h264_finish_fragmentation_unit (rtph264depay);
1161 rtph264depay->current_fu_type = nal_unit_type;
1162 rtph264depay->fu_timestamp = timestamp;
1164 rtph264depay->wait_start = FALSE;
1166 /* reconstruct NAL header */
1167 nal_header = (payload[0] & 0xe0) | (payload[1] & 0x1f);
1169 /* strip type header, keep FU header, we'll reuse it to reconstruct
1170 * the NAL header. */
1174 nalu_size = payload_len;
1175 outsize = nalu_size + sizeof (sync_bytes);
1176 outbuf = gst_buffer_new_and_alloc (outsize);
1178 gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
1179 memcpy (map.data + sizeof (sync_bytes), payload, nalu_size);
1180 map.data[sizeof (sync_bytes)] = nal_header;
1181 gst_buffer_unmap (outbuf, &map);
1183 gst_rtp_copy_video_meta (rtph264depay, outbuf, rtp->buffer);
1185 GST_DEBUG_OBJECT (rtph264depay, "queueing %d bytes", outsize);
1187 /* and assemble in the adapter */
1188 gst_adapter_push (rtph264depay->adapter, outbuf);
1190 /* strip off FU indicator and FU header bytes */
1194 outsize = payload_len;
1195 outbuf = gst_buffer_new_and_alloc (outsize);
1196 gst_buffer_fill (outbuf, 0, payload, outsize);
1198 gst_rtp_copy_video_meta (rtph264depay, outbuf, rtp->buffer);
1200 GST_DEBUG_OBJECT (rtph264depay, "queueing %d bytes", outsize);
1202 /* and assemble in the adapter */
1203 gst_adapter_push (rtph264depay->adapter, outbuf);
1207 rtph264depay->fu_marker = marker;
1209 /* if NAL unit ends, flush the adapter */
1211 gst_rtp_h264_finish_fragmentation_unit (rtph264depay);
1216 rtph264depay->wait_start = FALSE;
1218 /* 1-23 NAL unit Single NAL unit packet per H.264 5.6 */
1219 /* the entire payload is the output buffer */
1220 nalu_size = payload_len;
1221 outsize = nalu_size + sizeof (sync_bytes);
1222 outbuf = gst_buffer_new_and_alloc (outsize);
1224 gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
1225 if (rtph264depay->byte_stream) {
1226 memcpy (map.data, sync_bytes, sizeof (sync_bytes));
1228 map.data[0] = map.data[1] = 0;
1229 map.data[2] = nalu_size >> 8;
1230 map.data[3] = nalu_size & 0xff;
1232 memcpy (map.data + sizeof (sync_bytes), payload, nalu_size);
1233 gst_buffer_unmap (outbuf, &map);
1235 gst_rtp_copy_video_meta (rtph264depay, outbuf, rtp->buffer);
1237 gst_rtp_h264_depay_handle_nal (rtph264depay, outbuf, timestamp, marker);
1248 GST_DEBUG_OBJECT (rtph264depay, "empty packet");
1253 GST_ELEMENT_WARNING (rtph264depay, STREAM, DECODE,
1254 (NULL), ("Undefined packet type"));
1259 GST_DEBUG_OBJECT (rtph264depay, "waiting for start");
1264 GST_ELEMENT_ERROR (rtph264depay, STREAM, FORMAT,
1265 (NULL), ("NAL unit type %d not supported yet", nal_unit_type));
1271 gst_rtp_h264_depay_handle_event (GstRTPBaseDepayload * depay, GstEvent * event)
1273 GstRtpH264Depay *rtph264depay;
1275 rtph264depay = GST_RTP_H264_DEPAY (depay);
1277 switch (GST_EVENT_TYPE (event)) {
1278 case GST_EVENT_FLUSH_STOP:
1279 gst_rtp_h264_depay_reset (rtph264depay, FALSE);
1286 GST_RTP_BASE_DEPAYLOAD_CLASS (parent_class)->handle_event (depay, event);
1289 static GstStateChangeReturn
1290 gst_rtp_h264_depay_change_state (GstElement * element,
1291 GstStateChange transition)
1293 GstRtpH264Depay *rtph264depay;
1294 GstStateChangeReturn ret;
1296 rtph264depay = GST_RTP_H264_DEPAY (element);
1298 switch (transition) {
1299 case GST_STATE_CHANGE_NULL_TO_READY:
1301 case GST_STATE_CHANGE_READY_TO_PAUSED:
1302 gst_rtp_h264_depay_reset (rtph264depay, TRUE);
1308 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1310 switch (transition) {
1311 case GST_STATE_CHANGE_PAUSED_TO_READY:
1312 gst_rtp_h264_depay_reset (rtph264depay, TRUE);
1314 case GST_STATE_CHANGE_READY_TO_NULL:
1323 gst_rtp_h264_depay_plugin_init (GstPlugin * plugin)
1325 GST_DEBUG_CATEGORY_INIT (rtph264depay_debug, "rtph264depay", 0,
1326 "H264 Video RTP Depayloader");
1328 return gst_element_register (plugin, "rtph264depay",
1329 GST_RANK_SECONDARY, GST_TYPE_RTP_H264_DEPAY);