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;
823 gst_rtp_h264_depay_push (GstRtpH264Depay * rtph264depay, GstBuffer * outbuf,
824 gboolean keyframe, GstClockTime timestamp, gboolean marker)
826 /* prepend codec_data */
827 if (rtph264depay->codec_data) {
828 GST_DEBUG_OBJECT (rtph264depay, "prepending codec_data");
829 gst_rtp_copy_video_meta (rtph264depay, rtph264depay->codec_data, outbuf);
830 outbuf = gst_buffer_append (rtph264depay->codec_data, outbuf);
831 rtph264depay->codec_data = NULL;
834 outbuf = gst_buffer_make_writable (outbuf);
836 gst_rtp_drop_non_video_meta (rtph264depay, outbuf);
838 GST_BUFFER_PTS (outbuf) = timestamp;
841 GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
843 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
846 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_MARKER);
848 gst_rtp_base_depayload_push (GST_RTP_BASE_DEPAYLOAD (rtph264depay), outbuf);
851 /* SPS/PPS/IDR considered key, all others DELTA;
852 * so downstream waiting for keyframe can pick up at SPS/PPS/IDR */
853 #define NAL_TYPE_IS_KEY(nt) (((nt) == 5) || ((nt) == 7) || ((nt) == 8))
856 gst_rtp_h264_depay_handle_nal (GstRtpH264Depay * rtph264depay, GstBuffer * nal,
857 GstClockTime in_timestamp, gboolean marker)
859 GstRTPBaseDepayload *depayload = GST_RTP_BASE_DEPAYLOAD (rtph264depay);
862 GstBuffer *outbuf = NULL;
863 GstClockTime out_timestamp;
864 gboolean keyframe, out_keyframe;
866 gst_buffer_map (nal, &map, GST_MAP_READ);
867 if (G_UNLIKELY (map.size < 5))
870 nal_type = map.data[4] & 0x1f;
871 GST_DEBUG_OBJECT (rtph264depay, "handle NAL type %d", nal_type);
873 keyframe = NAL_TYPE_IS_KEY (nal_type);
875 out_keyframe = keyframe;
876 out_timestamp = in_timestamp;
878 if (!rtph264depay->byte_stream) {
879 if (nal_type == 7 || nal_type == 8) {
880 gst_rtp_h264_depay_add_sps_pps (rtph264depay,
881 gst_buffer_copy_region (nal, GST_BUFFER_COPY_ALL,
882 4, gst_buffer_get_size (nal) - 4));
883 gst_buffer_unmap (nal, &map);
884 gst_buffer_unref (nal);
886 } else if (rtph264depay->sps->len == 0 || rtph264depay->pps->len == 0) {
887 /* Down push down any buffer in non-bytestream mode if the SPS/PPS haven't
890 gst_pad_push_event (GST_RTP_BASE_DEPAYLOAD_SINKPAD (depayload),
891 gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM,
892 gst_structure_new ("GstForceKeyUnit",
893 "all-headers", G_TYPE_BOOLEAN, TRUE, NULL)));
894 gst_buffer_unmap (nal, &map);
895 gst_buffer_unref (nal);
899 if (rtph264depay->new_codec_data &&
900 rtph264depay->sps->len > 0 && rtph264depay->pps->len > 0)
901 gst_rtp_h264_set_src_caps (rtph264depay);
905 if (rtph264depay->merge) {
906 gboolean start = FALSE, complete = FALSE;
908 /* marker bit isn't mandatory so in the following code we try to guess
909 * an AU boundary by detecting a new picture start */
911 /* consider a coded slices (IDR or not) to start a picture,
912 * (so ending the previous one) if first_mb_in_slice == 0
913 * (non-0 is part of previous one) */
914 /* NOTE this is not entirely according to Access Unit specs in 7.4.1.2.4,
915 * but in practice it works in sane cases, needs not much parsing,
916 * and also works with broken frame_num in NAL (where spec-wise would fail) */
917 /* FIXME: this code isn't correct for interlaced content as AUs should be
918 * constructed with pairs of fields and the guess here will just push out
919 * AUs with a single field in it */
920 if (nal_type == 1 || nal_type == 2 || nal_type == 5) {
921 /* we have a picture start */
923 if (map.data[5] & 0x80) {
924 /* first_mb_in_slice == 0 completes a picture */
927 } else if (nal_type >= 6 && nal_type <= 9) {
928 /* SEI, SPS, PPS, AU terminate picture */
931 GST_DEBUG_OBJECT (depayload, "start %d, complete %d", start, complete);
933 if (complete && rtph264depay->picture_start)
934 outbuf = gst_rtp_h264_complete_au (rtph264depay, &out_timestamp,
938 gst_buffer_unmap (nal, &map);
940 GST_DEBUG_OBJECT (depayload, "adding NAL to picture adapter");
941 gst_adapter_push (rtph264depay->picture_adapter, nal);
942 rtph264depay->last_ts = in_timestamp;
943 rtph264depay->last_keyframe |= keyframe;
944 rtph264depay->picture_start |= start;
947 outbuf = gst_rtp_h264_complete_au (rtph264depay, &out_timestamp,
950 /* no merge, output is input nal */
951 GST_DEBUG_OBJECT (depayload, "using NAL as output");
953 gst_buffer_unmap (nal, &map);
957 gst_rtp_h264_depay_push (rtph264depay, outbuf, out_keyframe, out_timestamp,
966 GST_WARNING_OBJECT (depayload, "dropping short NAL");
967 gst_buffer_unmap (nal, &map);
968 gst_buffer_unref (nal);
974 gst_rtp_h264_finish_fragmentation_unit (GstRtpH264Depay * rtph264depay)
980 outsize = gst_adapter_available (rtph264depay->adapter);
981 outbuf = gst_adapter_take_buffer (rtph264depay->adapter, outsize);
983 gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
984 GST_DEBUG_OBJECT (rtph264depay, "output %d bytes", outsize);
986 if (rtph264depay->byte_stream) {
987 memcpy (map.data, sync_bytes, sizeof (sync_bytes));
990 map.data[0] = (outsize >> 24);
991 map.data[1] = (outsize >> 16);
992 map.data[2] = (outsize >> 8);
993 map.data[3] = (outsize);
995 gst_buffer_unmap (outbuf, &map);
997 rtph264depay->current_fu_type = 0;
999 gst_rtp_h264_depay_handle_nal (rtph264depay, outbuf,
1000 rtph264depay->fu_timestamp, rtph264depay->fu_marker);
1004 gst_rtp_h264_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
1006 GstRtpH264Depay *rtph264depay;
1007 GstBuffer *outbuf = NULL;
1008 guint8 nal_unit_type;
1010 rtph264depay = GST_RTP_H264_DEPAY (depayload);
1012 /* flush remaining data on discont */
1013 if (GST_BUFFER_IS_DISCONT (rtp->buffer)) {
1014 gst_adapter_clear (rtph264depay->adapter);
1015 rtph264depay->wait_start = TRUE;
1016 rtph264depay->current_fu_type = 0;
1025 guint outsize, nalu_size;
1026 GstClockTime timestamp;
1029 timestamp = GST_BUFFER_PTS (rtp->buffer);
1031 payload_len = gst_rtp_buffer_get_payload_len (rtp);
1032 payload = gst_rtp_buffer_get_payload (rtp);
1033 marker = gst_rtp_buffer_get_marker (rtp);
1035 GST_DEBUG_OBJECT (rtph264depay, "receiving %d bytes", payload_len);
1037 if (payload_len == 0)
1040 /* +---------------+
1048 nal_ref_idc = (payload[0] & 0x60) >> 5;
1049 nal_unit_type = payload[0] & 0x1f;
1051 /* at least one byte header with type */
1054 GST_DEBUG_OBJECT (rtph264depay, "NRI %d, Type %d %s", nal_ref_idc,
1055 nal_unit_type, marker ? "marker" : "");
1057 /* If FU unit was being processed, but the current nal is of a different
1058 * type. Assume that the remote payloader is buggy (didn't set the end bit
1059 * when the FU ended) and send out what we gathered thusfar */
1060 if (G_UNLIKELY (rtph264depay->current_fu_type != 0 &&
1061 nal_unit_type != rtph264depay->current_fu_type))
1062 gst_rtp_h264_finish_fragmentation_unit (rtph264depay);
1064 switch (nal_unit_type) {
1069 goto undefined_type;
1071 /* STAP-B Single-time aggregation packet 5.7.1 */
1072 /* 2 byte extra header for DON */
1078 payload += header_len;
1079 payload_len -= header_len;
1081 rtph264depay->wait_start = FALSE;
1084 /* STAP-A Single-time aggregation packet 5.7.1 */
1085 while (payload_len > 2) {
1087 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
1088 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1090 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1092 nalu_size = (payload[0] << 8) | payload[1];
1094 /* dont include nalu_size */
1095 if (nalu_size > (payload_len - 2))
1096 nalu_size = payload_len - 2;
1098 outsize = nalu_size + sizeof (sync_bytes);
1099 outbuf = gst_buffer_new_and_alloc (outsize);
1101 gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
1102 if (rtph264depay->byte_stream) {
1103 memcpy (map.data, sync_bytes, sizeof (sync_bytes));
1105 map.data[0] = map.data[1] = 0;
1106 map.data[2] = payload[0];
1107 map.data[3] = payload[1];
1110 /* strip NALU size */
1114 memcpy (map.data + sizeof (sync_bytes), payload, nalu_size);
1115 gst_buffer_unmap (outbuf, &map);
1117 gst_rtp_copy_video_meta (rtph264depay, outbuf, rtp->buffer);
1119 gst_rtp_h264_depay_handle_nal (rtph264depay, outbuf, timestamp,
1122 payload += nalu_size;
1123 payload_len -= nalu_size;
1128 /* MTAP16 Multi-time aggregation packet 5.7.2 */
1130 /* fallthrough, not implemented */
1132 /* MTAP24 Multi-time aggregation packet 5.7.2 */
1134 goto not_implemented;
1139 /* FU-A Fragmentation unit 5.8 */
1140 /* FU-B Fragmentation unit 5.8 */
1143 /* +---------------+
1149 * R is reserved and always 0
1151 S = (payload[1] & 0x80) == 0x80;
1152 E = (payload[1] & 0x40) == 0x40;
1154 GST_DEBUG_OBJECT (rtph264depay, "S %d, E %d", S, E);
1156 if (rtph264depay->wait_start && !S)
1160 /* NAL unit starts here */
1163 /* If a new FU unit started, while still processing an older one.
1164 * Assume that the remote payloader is buggy (doesn't set the end
1165 * bit) and send out what we've gathered thusfar */
1166 if (G_UNLIKELY (rtph264depay->current_fu_type != 0))
1167 gst_rtp_h264_finish_fragmentation_unit (rtph264depay);
1169 rtph264depay->current_fu_type = nal_unit_type;
1170 rtph264depay->fu_timestamp = timestamp;
1172 rtph264depay->wait_start = FALSE;
1174 /* reconstruct NAL header */
1175 nal_header = (payload[0] & 0xe0) | (payload[1] & 0x1f);
1177 /* strip type header, keep FU header, we'll reuse it to reconstruct
1178 * the NAL header. */
1182 nalu_size = payload_len;
1183 outsize = nalu_size + sizeof (sync_bytes);
1184 outbuf = gst_buffer_new_and_alloc (outsize);
1186 gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
1187 memcpy (map.data + sizeof (sync_bytes), payload, nalu_size);
1188 map.data[sizeof (sync_bytes)] = nal_header;
1189 gst_buffer_unmap (outbuf, &map);
1191 gst_rtp_copy_video_meta (rtph264depay, outbuf, rtp->buffer);
1193 GST_DEBUG_OBJECT (rtph264depay, "queueing %d bytes", outsize);
1195 /* and assemble in the adapter */
1196 gst_adapter_push (rtph264depay->adapter, outbuf);
1198 /* strip off FU indicator and FU header bytes */
1202 outsize = payload_len;
1203 outbuf = gst_buffer_new_and_alloc (outsize);
1204 gst_buffer_fill (outbuf, 0, payload, outsize);
1206 gst_rtp_copy_video_meta (rtph264depay, outbuf, rtp->buffer);
1208 GST_DEBUG_OBJECT (rtph264depay, "queueing %d bytes", outsize);
1210 /* and assemble in the adapter */
1211 gst_adapter_push (rtph264depay->adapter, outbuf);
1215 rtph264depay->fu_marker = marker;
1217 /* if NAL unit ends, flush the adapter */
1219 gst_rtp_h264_finish_fragmentation_unit (rtph264depay);
1224 rtph264depay->wait_start = FALSE;
1226 /* 1-23 NAL unit Single NAL unit packet per H.264 5.6 */
1227 /* the entire payload is the output buffer */
1228 nalu_size = payload_len;
1229 outsize = nalu_size + sizeof (sync_bytes);
1230 outbuf = gst_buffer_new_and_alloc (outsize);
1232 gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
1233 if (rtph264depay->byte_stream) {
1234 memcpy (map.data, sync_bytes, sizeof (sync_bytes));
1236 map.data[0] = map.data[1] = 0;
1237 map.data[2] = nalu_size >> 8;
1238 map.data[3] = nalu_size & 0xff;
1240 memcpy (map.data + sizeof (sync_bytes), payload, nalu_size);
1241 gst_buffer_unmap (outbuf, &map);
1243 gst_rtp_copy_video_meta (rtph264depay, outbuf, rtp->buffer);
1245 gst_rtp_h264_depay_handle_nal (rtph264depay, outbuf, timestamp, marker);
1256 GST_DEBUG_OBJECT (rtph264depay, "empty packet");
1261 GST_ELEMENT_WARNING (rtph264depay, STREAM, DECODE,
1262 (NULL), ("Undefined packet type"));
1267 GST_DEBUG_OBJECT (rtph264depay, "waiting for start");
1272 GST_ELEMENT_ERROR (rtph264depay, STREAM, FORMAT,
1273 (NULL), ("NAL unit type %d not supported yet", nal_unit_type));
1279 gst_rtp_h264_depay_handle_event (GstRTPBaseDepayload * depay, GstEvent * event)
1281 GstRtpH264Depay *rtph264depay;
1283 rtph264depay = GST_RTP_H264_DEPAY (depay);
1285 switch (GST_EVENT_TYPE (event)) {
1286 case GST_EVENT_FLUSH_STOP:
1287 gst_rtp_h264_depay_reset (rtph264depay, FALSE);
1294 GST_RTP_BASE_DEPAYLOAD_CLASS (parent_class)->handle_event (depay, event);
1297 static GstStateChangeReturn
1298 gst_rtp_h264_depay_change_state (GstElement * element,
1299 GstStateChange transition)
1301 GstRtpH264Depay *rtph264depay;
1302 GstStateChangeReturn ret;
1304 rtph264depay = GST_RTP_H264_DEPAY (element);
1306 switch (transition) {
1307 case GST_STATE_CHANGE_NULL_TO_READY:
1309 case GST_STATE_CHANGE_READY_TO_PAUSED:
1310 gst_rtp_h264_depay_reset (rtph264depay, TRUE);
1316 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1318 switch (transition) {
1319 case GST_STATE_CHANGE_PAUSED_TO_READY:
1320 gst_rtp_h264_depay_reset (rtph264depay, TRUE);
1322 case GST_STATE_CHANGE_READY_TO_NULL:
1331 gst_rtp_h264_depay_plugin_init (GstPlugin * plugin)
1333 GST_DEBUG_CATEGORY_INIT (rtph264depay_debug, "rtph264depay", 0,
1334 "H264 Video RTP Depayloader");
1336 return gst_element_register (plugin, "rtph264depay",
1337 GST_RANK_SECONDARY, GST_TYPE_RTP_H264_DEPAY);