2 * Copyright (C) <2006> Wim Taymans <wim.taymans@gmail.com>
3 * Copyright (C) <2014> Jurgen Slowack <jurgenslowack@gmail.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
28 #include <gst/base/gstbitreader.h>
29 #include <gst/rtp/gstrtpbuffer.h>
30 #include <gst/video/video.h>
31 #include "gstrtph265depay.h"
32 #include "gstrtputils.h"
34 GST_DEBUG_CATEGORY_STATIC (rtph265depay_debug);
35 #define GST_CAT_DEFAULT (rtph265depay_debug)
37 /* This is what we'll default to when downstream hasn't
38 * expressed a restriction or preference via caps */
39 #define DEFAULT_STREAM_FORMAT GST_H265_STREAM_FORMAT_BYTESTREAM
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_h265_depay_src_template =
46 GST_STATIC_PAD_TEMPLATE ("src",
50 ("video/x-h265, stream-format=(string)hvc1, alignment=(string)au; "
51 /* FIXME: hev1 format is not supported yet */
53 "stream-format = (string) hev1, alignment = (string) au; " */
55 "stream-format = (string) byte-stream, alignment = (string) { nal, au }")
58 static GstStaticPadTemplate gst_rtp_h265_depay_sink_template =
59 GST_STATIC_PAD_TEMPLATE ("sink",
62 GST_STATIC_CAPS ("application/x-rtp, "
63 "media = (string) \"video\", "
64 "clock-rate = (int) 90000, " "encoding-name = (string) \"H265\"")
65 /* optional parameters */
66 /* "profile-space = (int) [ 0, 3 ], " */
67 /* "profile-id = (int) [ 0, 31 ], " */
68 /* "tier-flag = (int) [ 0, 1 ], " */
69 /* "level-id = (int) [ 0, 255 ], " */
70 /* "interop-constraints = (string) ANY, " */
71 /* "profile-compatibility-indicator = (string) ANY, " */
72 /* "sprop-sub-layer-id = (int) [ 0, 6 ], " */
73 /* "recv-sub-layer-id = (int) [ 0, 6 ], " */
74 /* "max-recv-level-id = (int) [ 0, 255 ], " */
75 /* "tx-mode = (string) {MST , SST}, " */
76 /* "sprop-vps = (string) ANY, " */
77 /* "sprop-sps = (string) ANY, " */
78 /* "sprop-pps = (string) ANY, " */
79 /* "sprop-sei = (string) ANY, " */
80 /* "max-lsr = (int) ANY, " *//* MUST be in the range of MaxLumaSR to 16 * MaxLumaSR, inclusive */
81 /* "max-lps = (int) ANY, " *//* MUST be in the range of MaxLumaPS to 16 * MaxLumaPS, inclusive */
82 /* "max-cpb = (int) ANY, " *//* MUST be in the range of MaxCPB to 16 * MaxCPB, inclusive */
83 /* "max-dpb = (int) [1, 16], " */
84 /* "max-br = (int) ANY, " *//* MUST be in the range of MaxBR to 16 * MaxBR, inclusive, for the highest level */
85 /* "max-tr = (int) ANY, " *//* MUST be in the range of MaxTileRows to 16 * MaxTileRows, inclusive, for the highest level */
86 /* "max-tc = (int) ANY, " *//* MUST be in the range of MaxTileCols to 16 * MaxTileCols, inclusive, for the highest level */
87 /* "max-fps = (int) ANY, " */
88 /* "sprop-max-don-diff = (int) [0, 32767], " */
89 /* "sprop-depack-buf-nalus = (int) [0, 32767], " */
90 /* "sprop-depack-buf-nalus = (int) [0, 4294967295], " */
91 /* "depack-buf-cap = (int) [1, 4294967295], " */
92 /* "sprop-segmentation-id = (int) [0, 3], " */
93 /* "sprop-spatial-segmentation-idc = (string) ANY, " */
94 /* "dec-parallel-cap = (string) ANY, " */
97 #define gst_rtp_h265_depay_parent_class parent_class
98 G_DEFINE_TYPE (GstRtpH265Depay, gst_rtp_h265_depay,
99 GST_TYPE_RTP_BASE_DEPAYLOAD);
101 static void gst_rtp_h265_depay_finalize (GObject * object);
103 static GstStateChangeReturn gst_rtp_h265_depay_change_state (GstElement *
104 element, GstStateChange transition);
106 static GstBuffer *gst_rtp_h265_depay_process (GstRTPBaseDepayload * depayload,
108 static gboolean gst_rtp_h265_depay_setcaps (GstRTPBaseDepayload * filter,
110 static gboolean gst_rtp_h265_depay_handle_event (GstRTPBaseDepayload * depay,
112 static GstBuffer *gst_rtp_h265_complete_au (GstRtpH265Depay * rtph265depay,
113 GstClockTime * out_timestamp, gboolean * out_keyframe);
114 static void gst_rtp_h265_depay_push (GstRtpH265Depay * rtph265depay,
115 GstBuffer * outbuf, gboolean keyframe, GstClockTime timestamp,
120 gst_rtp_h265_depay_class_init (GstRtpH265DepayClass * klass)
122 GObjectClass *gobject_class;
123 GstElementClass *gstelement_class;
124 GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
126 gobject_class = (GObjectClass *) klass;
127 gstelement_class = (GstElementClass *) klass;
128 gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
130 gobject_class->finalize = gst_rtp_h265_depay_finalize;
132 gst_element_class_add_static_pad_template (gstelement_class,
133 &gst_rtp_h265_depay_src_template);
134 gst_element_class_add_static_pad_template (gstelement_class,
135 &gst_rtp_h265_depay_sink_template);
137 gst_element_class_set_static_metadata (gstelement_class,
138 "RTP H265 depayloader", "Codec/Depayloader/Network/RTP",
139 "Extracts H265 video from RTP packets (RFC 7798)",
140 "Jurgen Slowack <jurgenslowack@gmail.com>");
141 gstelement_class->change_state = gst_rtp_h265_depay_change_state;
143 gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_h265_depay_process;
144 gstrtpbasedepayload_class->set_caps = gst_rtp_h265_depay_setcaps;
145 gstrtpbasedepayload_class->handle_event = gst_rtp_h265_depay_handle_event;
149 gst_rtp_h265_depay_init (GstRtpH265Depay * rtph265depay)
151 rtph265depay->adapter = gst_adapter_new ();
152 rtph265depay->picture_adapter = gst_adapter_new ();
153 rtph265depay->output_format = DEFAULT_STREAM_FORMAT;
154 rtph265depay->byte_stream =
155 (DEFAULT_STREAM_FORMAT == GST_H265_STREAM_FORMAT_BYTESTREAM);
156 rtph265depay->stream_format = NULL;
157 rtph265depay->merge = DEFAULT_ACCESS_UNIT;
158 rtph265depay->vps = g_ptr_array_new_with_free_func (
159 (GDestroyNotify) gst_buffer_unref);
160 rtph265depay->sps = g_ptr_array_new_with_free_func (
161 (GDestroyNotify) gst_buffer_unref);
162 rtph265depay->pps = g_ptr_array_new_with_free_func (
163 (GDestroyNotify) gst_buffer_unref);
167 gst_rtp_h265_depay_reset (GstRtpH265Depay * rtph265depay, gboolean hard)
169 gst_adapter_clear (rtph265depay->adapter);
170 rtph265depay->wait_start = TRUE;
171 gst_adapter_clear (rtph265depay->picture_adapter);
172 rtph265depay->picture_start = FALSE;
173 rtph265depay->last_keyframe = FALSE;
174 rtph265depay->last_ts = 0;
175 rtph265depay->current_fu_type = 0;
176 rtph265depay->new_codec_data = FALSE;
177 g_ptr_array_set_size (rtph265depay->vps, 0);
178 g_ptr_array_set_size (rtph265depay->sps, 0);
179 g_ptr_array_set_size (rtph265depay->pps, 0);
182 if (rtph265depay->allocator != NULL) {
183 gst_object_unref (rtph265depay->allocator);
184 rtph265depay->allocator = NULL;
186 gst_allocation_params_init (&rtph265depay->params);
191 gst_rtp_h265_depay_drain (GstRtpH265Depay * rtph265depay)
193 GstClockTime timestamp;
197 if (!rtph265depay->picture_start)
200 outbuf = gst_rtp_h265_complete_au (rtph265depay, ×tamp, &keyframe);
202 gst_rtp_h265_depay_push (rtph265depay, outbuf, keyframe, timestamp, FALSE);
206 gst_rtp_h265_depay_finalize (GObject * object)
208 GstRtpH265Depay *rtph265depay;
210 rtph265depay = GST_RTP_H265_DEPAY (object);
212 if (rtph265depay->codec_data)
213 gst_buffer_unref (rtph265depay->codec_data);
215 g_object_unref (rtph265depay->adapter);
216 g_object_unref (rtph265depay->picture_adapter);
218 g_ptr_array_free (rtph265depay->vps, TRUE);
219 g_ptr_array_free (rtph265depay->sps, TRUE);
220 g_ptr_array_free (rtph265depay->pps, TRUE);
222 G_OBJECT_CLASS (parent_class)->finalize (object);
225 static inline const gchar *
226 stream_format_get_nick (GstH265StreamFormat fmt)
229 case GST_H265_STREAM_FORMAT_BYTESTREAM:
230 return "byte-stream";
231 case GST_H265_STREAM_FORMAT_HVC1:
233 case GST_H265_STREAM_FORMAT_HEV1:
242 gst_rtp_h265_depay_negotiate (GstRtpH265Depay * rtph265depay)
244 GstH265StreamFormat stream_format = GST_H265_STREAM_FORMAT_UNKNOWN;
249 gst_pad_get_allowed_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (rtph265depay));
251 GST_DEBUG_OBJECT (rtph265depay, "allowed caps: %" GST_PTR_FORMAT, caps);
254 if (gst_caps_get_size (caps) > 0) {
255 GstStructure *s = gst_caps_get_structure (caps, 0);
256 const gchar *str = NULL;
258 if ((str = gst_structure_get_string (s, "stream-format"))) {
259 rtph265depay->stream_format = g_intern_string (str);
261 if (strcmp (str, "hev1") == 0) {
262 stream_format = GST_H265_STREAM_FORMAT_HEV1;
263 } else if (strcmp (str, "hvc1") == 0) {
264 stream_format = GST_H265_STREAM_FORMAT_HVC1;
265 } else if (strcmp (str, "byte-stream") == 0) {
266 stream_format = GST_H265_STREAM_FORMAT_BYTESTREAM;
268 GST_DEBUG_OBJECT (rtph265depay, "unknown stream-format: %s", str);
272 if ((str = gst_structure_get_string (s, "alignment"))) {
273 if (strcmp (str, "au") == 0) {
275 } else if (strcmp (str, "nal") == 0) {
278 GST_DEBUG_OBJECT (rtph265depay, "unknown alignment: %s", str);
282 gst_caps_unref (caps);
285 if (stream_format != GST_H265_STREAM_FORMAT_UNKNOWN) {
286 GST_DEBUG_OBJECT (rtph265depay, "downstream wants stream-format %s",
287 stream_format_get_nick (stream_format));
288 rtph265depay->output_format = stream_format;
290 GST_DEBUG_OBJECT (rtph265depay, "defaulting to output stream-format %s",
291 stream_format_get_nick (DEFAULT_STREAM_FORMAT));
292 rtph265depay->stream_format =
293 stream_format_get_nick (DEFAULT_STREAM_FORMAT);
294 rtph265depay->output_format = DEFAULT_STREAM_FORMAT;
296 rtph265depay->byte_stream =
297 (rtph265depay->output_format == GST_H265_STREAM_FORMAT_BYTESTREAM);
300 GST_DEBUG_OBJECT (rtph265depay, "downstream requires merge %d", merge);
301 rtph265depay->merge = merge;
303 GST_DEBUG_OBJECT (rtph265depay, "defaulting to merge %d",
304 DEFAULT_ACCESS_UNIT);
305 rtph265depay->merge = DEFAULT_ACCESS_UNIT;
310 parse_sps (GstMapInfo * map, guint32 * sps_id)
311 { /* To parse seq_parameter_set_id */
312 GstBitReader br = GST_BIT_READER_INIT (map->data + 15,
315 GST_MEMDUMP ("SPS", map->data, map->size);
320 if (!gst_rtp_read_golomb (&br, sps_id))
327 parse_pps (GstMapInfo * map, guint32 * sps_id, guint32 * pps_id)
328 { /* To parse picture_parameter_set_id */
329 GstBitReader br = GST_BIT_READER_INIT (map->data + 2,
332 GST_MEMDUMP ("PPS", map->data, map->size);
337 if (!gst_rtp_read_golomb (&br, pps_id))
339 if (!gst_rtp_read_golomb (&br, sps_id))
346 gst_rtp_h265_depay_set_output_caps (GstRtpH265Depay * rtph265depay,
349 GstAllocationParams params;
350 GstAllocator *allocator = NULL;
354 gst_allocation_params_init (¶ms);
356 srcpad = GST_RTP_BASE_DEPAYLOAD_SRCPAD (rtph265depay);
358 res = gst_pad_set_caps (srcpad, caps);
363 query = gst_query_new_allocation (caps, TRUE);
364 if (!gst_pad_peer_query (srcpad, query)) {
365 GST_DEBUG_OBJECT (rtph265depay, "downstream ALLOCATION query failed");
368 if (gst_query_get_n_allocation_params (query) > 0) {
369 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
372 gst_query_unref (query);
375 if (rtph265depay->allocator)
376 gst_object_unref (rtph265depay->allocator);
378 rtph265depay->allocator = allocator;
379 rtph265depay->params = params;
385 gst_rtp_h265_set_src_caps (GstRtpH265Depay * rtph265depay)
387 gboolean res, update_caps;
392 if (!rtph265depay->byte_stream &&
393 (!rtph265depay->new_codec_data ||
394 rtph265depay->vps->len == 0 || rtph265depay->sps->len == 0
395 || rtph265depay->pps->len == 0))
398 srccaps = gst_caps_new_simple ("video/x-h265",
399 "stream-format", G_TYPE_STRING, rtph265depay->stream_format,
400 "alignment", G_TYPE_STRING, rtph265depay->merge ? "au" : "nal", NULL);
402 if (!rtph265depay->byte_stream) {
403 GstBuffer *codec_data;
406 guint num_vps = rtph265depay->vps->len;
407 guint num_sps = rtph265depay->sps->len;
408 guint num_pps = rtph265depay->pps->len;
409 GstMapInfo map, nalmap;
411 guint8 num_arrays = 0;
416 guint32 max_sub_layers_minus1, temporal_id_nesting_flag, chroma_format_idc,
417 bit_depth_luma_minus8, bit_depth_chroma_minus8,
418 min_spatial_segmentation_idc;
420 /* Fixme: Current implementation is not embedding SEI in codec_data */
425 /* start with 23 bytes header */
428 num_arrays = (num_vps > 0) + (num_sps > 0) + (num_pps > 0);
429 len += 3 * num_arrays;
431 /* add size of vps, sps & pps */
432 for (i = 0; i < num_vps; i++)
433 len += 2 + gst_buffer_get_size (g_ptr_array_index (rtph265depay->vps, i));
434 for (i = 0; i < num_sps; i++)
435 len += 2 + gst_buffer_get_size (g_ptr_array_index (rtph265depay->sps, i));
436 for (i = 0; i < num_pps; i++)
437 len += 2 + gst_buffer_get_size (g_ptr_array_index (rtph265depay->pps, i));
439 GST_DEBUG_OBJECT (rtph265depay,
440 "constructing codec_data: num_vps =%d num_sps=%d, num_pps=%d", num_vps,
443 codec_data = gst_buffer_new_and_alloc (len);
444 gst_buffer_map (codec_data, &map, GST_MAP_READWRITE);
447 memset (data, 0, map.size);
449 /* Parsing sps to get the info required further on */
451 gst_buffer_map (g_ptr_array_index (rtph265depay->sps, 0), &nalmap,
454 max_sub_layers_minus1 = ((nalmap.data[2]) >> 1) & 0x07;
455 temporal_id_nesting_flag = nalmap.data[2] & 0x01;
457 gst_bit_reader_init (&br, nalmap.data + 15, nalmap.size - 15);
459 gst_rtp_read_golomb (&br, &tmp); /* sps_seq_parameter_set_id */
460 gst_rtp_read_golomb (&br, &chroma_format_idc); /* chroma_format_idc */
462 if (chroma_format_idc == 3)
464 gst_bit_reader_get_bits_uint8 (&br, &tmp8, 1); /* separate_colour_plane_flag */
466 gst_rtp_read_golomb (&br, &tmp); /* pic_width_in_luma_samples */
467 gst_rtp_read_golomb (&br, &tmp); /* pic_height_in_luma_samples */
469 gst_bit_reader_get_bits_uint8 (&br, &tmp8, 1); /* conformance_window_flag */
471 gst_rtp_read_golomb (&br, &tmp); /* conf_win_left_offset */
472 gst_rtp_read_golomb (&br, &tmp); /* conf_win_right_offset */
473 gst_rtp_read_golomb (&br, &tmp); /* conf_win_top_offset */
474 gst_rtp_read_golomb (&br, &tmp); /* conf_win_bottom_offset */
477 gst_rtp_read_golomb (&br, &bit_depth_luma_minus8); /* bit_depth_luma_minus8 */
478 gst_rtp_read_golomb (&br, &bit_depth_chroma_minus8); /* bit_depth_chroma_minus8 */
480 GST_DEBUG_OBJECT (rtph265depay,
481 "Ignoring min_spatial_segmentation for now (assuming zero)");
483 min_spatial_segmentation_idc = 0; /* NOTE - we ignore this for now, but in a perfect world, we should continue parsing to obtain the real value */
485 gst_buffer_unmap (g_ptr_array_index (rtph265depay->sps, 0), &nalmap);
487 /* HEVCDecoderConfigurationVersion = 1 */
490 /* Copy from profile_tier_level (Rec. ITU-T H.265 (04/2013) section 7.3.3
492 * profile_space | tier_flat | profile_idc |
493 * profile_compatibility_flags | constraint_indicator_flags |
494 * level_idc | progressive_source_flag | interlaced_source_flag
495 * non_packed_constraint_flag | frame_only_constraint_flag
496 * reserved_zero_44bits | level_idc */
497 gst_buffer_map (g_ptr_array_index (rtph265depay->sps, 0), &nalmap,
499 for (i = 0; i < 12; i++)
500 data[i + 1] = nalmap.data[i];
501 gst_buffer_unmap (g_ptr_array_index (rtph265depay->sps, 0), &nalmap);
503 /* min_spatial_segmentation_idc */
504 GST_WRITE_UINT16_BE (data + 13, min_spatial_segmentation_idc);
506 data[15] = 0xfc; /* keeping parrallelismType as zero (unknown) */
507 data[16] = 0xfc | chroma_format_idc;
508 data[17] = 0xf8 | bit_depth_luma_minus8;
509 data[18] = 0xf8 | bit_depth_chroma_minus8;
510 data[19] = 0x00; /* keep avgFrameRate as unspecified */
511 data[20] = 0x00; /* keep avgFrameRate as unspecified */
512 /* constFrameRate(2 bits): 0, stream may or may not be of constant framerate
513 * numTemporalLayers (3 bits): number of temporal layers, value from SPS
514 * TemporalIdNested (1 bit): sps_temporal_id_nesting_flag from SPS
515 * lengthSizeMinusOne (2 bits): plus 1 indicates the length of the NALUnitLength */
516 /* we always output NALs with 4-byte nal unit length markers (or sync code) */
517 data[21] = rtph265depay->byte_stream ? 0x00 : 0x03;
518 data[21] |= ((max_sub_layers_minus1 + 1) << 3);
519 data[21] |= (temporal_id_nesting_flag << 2);
520 GST_WRITE_UINT8 (data + 22, num_arrays); /* numOfArrays */
526 /* array_completeness | reserved_zero bit | nal_unit_type */
527 data[0] = 0x00 | 0x20;
530 GST_WRITE_UINT16_BE (data, num_vps);
533 for (i = 0; i < num_vps; i++) {
535 gst_buffer_get_size (g_ptr_array_index (rtph265depay->vps, i));
536 GST_WRITE_UINT16_BE (data, nal_size);
537 gst_buffer_extract (g_ptr_array_index (rtph265depay->vps, i), 0,
539 data += 2 + nal_size;
540 GST_DEBUG_OBJECT (rtph265depay, "Copied VPS %d of length %u", i,
547 /* array_completeness | reserved_zero bit | nal_unit_type */
548 data[0] = 0x00 | 0x21;
551 GST_WRITE_UINT16_BE (data, num_sps);
554 for (i = 0; i < num_sps; i++) {
556 gst_buffer_get_size (g_ptr_array_index (rtph265depay->sps, i));
557 GST_WRITE_UINT16_BE (data, nal_size);
558 gst_buffer_extract (g_ptr_array_index (rtph265depay->sps, i), 0,
560 data += 2 + nal_size;
561 GST_DEBUG_OBJECT (rtph265depay, "Copied SPS %d of length %u", i,
568 /* array_completeness | reserved_zero bit | nal_unit_type */
569 data[0] = 0x00 | 0x22;
572 GST_WRITE_UINT16_BE (data, num_pps);
575 for (i = 0; i < num_pps; i++) {
577 gst_buffer_get_size (g_ptr_array_index (rtph265depay->pps, i));
578 GST_WRITE_UINT16_BE (data, nal_size);
579 gst_buffer_extract (g_ptr_array_index (rtph265depay->pps, i), 0,
581 data += 2 + nal_size;
582 GST_DEBUG_OBJECT (rtph265depay, "Copied PPS %d of length %u", i,
587 new_size = data - map.data;
588 gst_buffer_unmap (codec_data, &map);
589 gst_buffer_set_size (codec_data, new_size);
591 gst_caps_set_simple (srccaps,
592 "codec_data", GST_TYPE_BUFFER, codec_data, NULL);
593 gst_buffer_unref (codec_data);
596 srcpad = GST_RTP_BASE_DEPAYLOAD_SRCPAD (rtph265depay);
598 old_caps = gst_pad_get_current_caps (srcpad);
599 if (old_caps != NULL) {
601 /* Only update the caps if they are not equal. For
602 * AVC we don't update caps if only the codec_data
603 * changes. This is the same behaviour as in h264parse
604 * and gstrtph264depay
606 if (rtph265depay->byte_stream) {
607 update_caps = !gst_caps_is_equal (srccaps, old_caps);
609 GstCaps *tmp_caps = gst_caps_copy (srccaps);
610 GstStructure *old_s, *tmp_s;
612 old_s = gst_caps_get_structure (old_caps, 0);
613 tmp_s = gst_caps_get_structure (tmp_caps, 0);
614 if (gst_structure_has_field (old_s, "codec_data"))
615 gst_structure_set_value (tmp_s, "codec_data",
616 gst_structure_get_value (old_s, "codec_data"));
618 update_caps = !gst_caps_is_equal (old_caps, tmp_caps);
619 gst_caps_unref (tmp_caps);
621 gst_caps_unref (old_caps);
627 res = gst_rtp_h265_depay_set_output_caps (rtph265depay, srccaps);
632 gst_caps_unref (srccaps);
634 /* Insert SPS and PPS into the stream on next opportunity */
635 if (rtph265depay->output_format != GST_H265_STREAM_FORMAT_HVC1
636 && (rtph265depay->sps->len > 0 || rtph265depay->pps->len > 0)) {
638 GstBuffer *codec_data;
643 for (i = 0; i < rtph265depay->sps->len; i++) {
644 len += 4 + gst_buffer_get_size (g_ptr_array_index (rtph265depay->sps, i));
647 for (i = 0; i < rtph265depay->pps->len; i++) {
648 len += 4 + gst_buffer_get_size (g_ptr_array_index (rtph265depay->pps, i));
651 codec_data = gst_buffer_new_and_alloc (len);
652 gst_buffer_map (codec_data, &map, GST_MAP_WRITE);
655 for (i = 0; i < rtph265depay->sps->len; i++) {
656 GstBuffer *sps_buf = g_ptr_array_index (rtph265depay->sps, i);
657 guint sps_size = gst_buffer_get_size (sps_buf);
659 if (rtph265depay->byte_stream)
660 memcpy (data, sync_bytes, sizeof (sync_bytes));
662 GST_WRITE_UINT32_BE (data, sps_size);
663 gst_buffer_extract (sps_buf, 0, data + 4, -1);
664 data += 4 + sps_size;
667 for (i = 0; i < rtph265depay->pps->len; i++) {
668 GstBuffer *pps_buf = g_ptr_array_index (rtph265depay->pps, i);
669 guint pps_size = gst_buffer_get_size (pps_buf);
671 if (rtph265depay->byte_stream)
672 memcpy (data, sync_bytes, sizeof (sync_bytes));
674 GST_WRITE_UINT32_BE (data, pps_size);
675 gst_buffer_extract (pps_buf, 0, data + 4, -1);
676 data += 4 + pps_size;
679 gst_buffer_unmap (codec_data, &map);
680 if (rtph265depay->codec_data)
681 gst_buffer_unref (rtph265depay->codec_data);
682 rtph265depay->codec_data = codec_data;
686 rtph265depay->new_codec_data = FALSE;
692 gst_rtp_h265_add_vps_sps_pps (GstElement * rtph265, GPtrArray * vps_array,
693 GPtrArray * sps_array, GPtrArray * pps_array, GstBuffer * nal)
699 gst_buffer_map (nal, &map, GST_MAP_READ);
701 type = (map.data[0] >> 1) & 0x3f;
703 if (type == GST_H265_VPS_NUT) {
704 guint32 vps_id = (map.data[2] >> 4) & 0x0f;
706 for (i = 0; i < vps_array->len; i++) {
707 GstBuffer *vps = g_ptr_array_index (vps_array, i);
711 gst_buffer_map (vps, &vpsmap, GST_MAP_READ);
712 tmp_vps_id = (vpsmap.data[2] >> 4) & 0x0f;
714 if (vps_id == tmp_vps_id) {
715 if (map.size == vpsmap.size &&
716 memcmp (map.data, vpsmap.data, vpsmap.size) == 0) {
717 GST_LOG_OBJECT (rtph265, "Unchanged VPS %u, not updating", vps_id);
718 gst_buffer_unmap (vps, &vpsmap);
721 gst_buffer_unmap (vps, &vpsmap);
722 g_ptr_array_remove_index_fast (vps_array, i);
723 g_ptr_array_add (vps_array, nal);
724 GST_LOG_OBJECT (rtph265, "Modified VPS %u, replacing", vps_id);
728 gst_buffer_unmap (vps, &vpsmap);
730 GST_LOG_OBJECT (rtph265, "Adding new VPS %u", vps_id);
731 g_ptr_array_add (vps_array, nal);
732 } else if (type == GST_H265_SPS_NUT) {
735 if (!parse_sps (&map, &sps_id)) {
736 GST_WARNING_OBJECT (rtph265, "Invalid SPS,"
737 " can't parse seq_parameter_set_id");
741 for (i = 0; i < sps_array->len; i++) {
742 GstBuffer *sps = g_ptr_array_index (sps_array, i);
746 gst_buffer_map (sps, &spsmap, GST_MAP_READ);
747 parse_sps (&spsmap, &tmp_sps_id);
749 if (sps_id == tmp_sps_id) {
750 if (map.size == spsmap.size &&
751 memcmp (map.data, spsmap.data, spsmap.size) == 0) {
752 GST_LOG_OBJECT (rtph265, "Unchanged SPS %u, not updating", sps_id);
753 gst_buffer_unmap (sps, &spsmap);
756 gst_buffer_unmap (sps, &spsmap);
757 g_ptr_array_remove_index_fast (sps_array, i);
758 g_ptr_array_add (sps_array, nal);
759 GST_LOG_OBJECT (rtph265, "Modified SPS %u, replacing", sps_id);
763 gst_buffer_unmap (sps, &spsmap);
765 GST_LOG_OBJECT (rtph265, "Adding new SPS %u", sps_id);
766 g_ptr_array_add (sps_array, nal);
767 } else if (type == GST_H265_PPS_NUT) {
771 if (!parse_pps (&map, &sps_id, &pps_id)) {
772 GST_WARNING_OBJECT (rtph265, "Invalid PPS,"
773 " can't parse seq_parameter_set_id or pic_parameter_set_id");
777 for (i = 0; i < pps_array->len; i++) {
778 GstBuffer *pps = g_ptr_array_index (pps_array, i);
784 gst_buffer_map (pps, &ppsmap, GST_MAP_READ);
785 parse_pps (&ppsmap, &tmp_sps_id, &tmp_pps_id);
787 if (pps_id == tmp_pps_id) {
788 if (map.size == ppsmap.size &&
789 memcmp (map.data, ppsmap.data, ppsmap.size) == 0) {
790 GST_LOG_OBJECT (rtph265, "Unchanged PPS %u:%u, not updating", sps_id,
792 gst_buffer_unmap (pps, &ppsmap);
795 gst_buffer_unmap (pps, &ppsmap);
796 g_ptr_array_remove_index_fast (pps_array, i);
797 g_ptr_array_add (pps_array, nal);
798 GST_LOG_OBJECT (rtph265, "Modified PPS %u:%u, replacing",
803 gst_buffer_unmap (pps, &ppsmap);
805 GST_LOG_OBJECT (rtph265, "Adding new PPS %u:%i", sps_id, pps_id);
806 g_ptr_array_add (pps_array, nal);
812 gst_buffer_unmap (nal, &map);
817 gst_buffer_unmap (nal, &map);
818 gst_buffer_unref (nal);
825 gst_rtp_h265_depay_add_vps_sps_pps (GstRtpH265Depay * rtph265depay,
828 if (gst_rtp_h265_add_vps_sps_pps (GST_ELEMENT (rtph265depay),
829 rtph265depay->vps, rtph265depay->sps, rtph265depay->pps, nal))
830 rtph265depay->new_codec_data = TRUE;
834 gst_rtp_h265_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
837 GstStructure *structure = gst_caps_get_structure (caps, 0);
838 GstRtpH265Depay *rtph265depay;
846 rtph265depay = GST_RTP_H265_DEPAY (depayload);
848 if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
850 depayload->clock_rate = clock_rate;
852 /* Base64 encoded, comma separated config NALs */
853 vps = gst_structure_get_string (structure, "sprop-vps");
854 sps = gst_structure_get_string (structure, "sprop-sps");
855 pps = gst_structure_get_string (structure, "sprop-pps");
856 if (vps == NULL || sps == NULL || pps == NULL) {
859 ps = g_strdup_printf ("%s,%s,%s", vps, sps, pps);
862 /* negotiate with downstream w.r.t. output format and alignment */
863 gst_rtp_h265_depay_negotiate (rtph265depay);
865 if (rtph265depay->byte_stream && ps != NULL) {
866 /* for bytestream we only need the parameter sets but we don't error out
867 * when they are not there, we assume they are in the stream. */
869 GstBuffer *codec_data;
873 params = g_strsplit (ps, ",", 0);
875 /* count total number of bytes in base64. Also include the sync bytes in
876 * front of the params. */
878 for (i = 0; params[i]; i++) {
879 len += strlen (params[i]);
880 len += sizeof (sync_bytes);
882 /* we seriously overshoot the length, but it's fine. */
883 codec_data = gst_buffer_new_and_alloc (len);
885 gst_buffer_map (codec_data, &map, GST_MAP_WRITE);
888 for (i = 0; params[i]; i++) {
892 GST_DEBUG_OBJECT (depayload, "decoding param %d (%s)", i, params[i]);
893 memcpy (ptr, sync_bytes, sizeof (sync_bytes));
894 ptr += sizeof (sync_bytes);
896 g_base64_decode_step (params[i], strlen (params[i]), ptr, &state,
898 GST_DEBUG_OBJECT (depayload, "decoded %d bytes", len);
899 total += len + sizeof (sync_bytes);
902 gst_buffer_unmap (codec_data, &map);
903 gst_buffer_resize (codec_data, 0, total);
906 /* keep the codec_data, we need to send it as the first buffer. We cannot
907 * push it in the adapter because the adapter might be flushed on discont.
909 if (rtph265depay->codec_data)
910 gst_buffer_unref (rtph265depay->codec_data);
911 rtph265depay->codec_data = codec_data;
912 } else if (!rtph265depay->byte_stream) {
917 goto incomplete_caps;
919 params = g_strsplit (ps, ",", 0);
921 GST_DEBUG_OBJECT (depayload, "we have %d params", g_strv_length (params));
923 /* start with 23 bytes header */
924 for (i = 0; params[i]; i++) {
931 nal_len = strlen (params[i]);
933 GST_WARNING_OBJECT (depayload, "empty param '%s' (#%d)", params[i], i);
936 nal = gst_buffer_new_and_alloc (nal_len);
937 gst_buffer_map (nal, &nalmap, GST_MAP_READWRITE);
940 g_base64_decode_step (params[i], nal_len, nalmap.data, &state, &save);
942 GST_DEBUG_OBJECT (depayload, "adding param %d as %s", i,
943 (((nalmap.data[0] >> 1) & 0x3f) ==
944 32) ? "VPS" : (((nalmap.data[0] >> 1) & 0x3f) ==
945 33) ? "SPS" : "PPS");
947 gst_buffer_unmap (nal, &nalmap);
948 gst_buffer_set_size (nal, nal_len);
950 gst_rtp_h265_depay_add_vps_sps_pps (rtph265depay, nal);
954 if (rtph265depay->vps->len == 0 || rtph265depay->sps->len == 0 ||
955 rtph265depay->pps->len == 0) {
956 goto incomplete_caps;
962 return gst_rtp_h265_set_src_caps (rtph265depay);
967 GST_DEBUG_OBJECT (depayload, "we have incomplete caps,"
968 " doing setcaps later");
975 gst_rtp_h265_depay_allocate_output_buffer (GstRtpH265Depay * depay, gsize size)
977 GstBuffer *buffer = NULL;
979 g_return_val_if_fail (size > 0, NULL);
981 GST_LOG_OBJECT (depay, "want output buffer of %u bytes", (guint) size);
983 buffer = gst_buffer_new_allocate (depay->allocator, size, &depay->params);
984 if (buffer == NULL) {
985 GST_INFO_OBJECT (depay, "couldn't allocate output buffer");
986 buffer = gst_buffer_new_allocate (NULL, size, NULL);
993 gst_rtp_h265_complete_au (GstRtpH265Depay * rtph265depay,
994 GstClockTime * out_timestamp, gboolean * out_keyframe)
999 guint outsize, offset = 0;
1000 gint b, n_bufs, m, n_mem;
1002 /* we had a picture in the adapter and we completed it */
1003 GST_DEBUG_OBJECT (rtph265depay, "taking completed AU");
1004 outsize = gst_adapter_available (rtph265depay->picture_adapter);
1006 outbuf = gst_rtp_h265_depay_allocate_output_buffer (rtph265depay, outsize);
1011 if (!gst_buffer_map (outbuf, &outmap, GST_MAP_WRITE))
1014 list = gst_adapter_take_buffer_list (rtph265depay->picture_adapter, outsize);
1016 n_bufs = gst_buffer_list_length (list);
1017 for (b = 0; b < n_bufs; ++b) {
1018 GstBuffer *buf = gst_buffer_list_get (list, b);
1020 n_mem = gst_buffer_n_memory (buf);
1021 for (m = 0; m < n_mem; ++m) {
1022 GstMemory *mem = gst_buffer_peek_memory (buf, m);
1023 gsize mem_size = gst_memory_get_sizes (mem, NULL, NULL);
1026 if (gst_memory_map (mem, &mem_map, GST_MAP_READ)) {
1027 memcpy (outmap.data + offset, mem_map.data, mem_size);
1028 gst_memory_unmap (mem, &mem_map);
1030 memset (outmap.data + offset, 0, mem_size);
1035 gst_rtp_copy_video_meta (rtph265depay, outbuf, buf);
1037 gst_buffer_list_unref (list);
1038 gst_buffer_unmap (outbuf, &outmap);
1040 *out_timestamp = rtph265depay->last_ts;
1041 *out_keyframe = rtph265depay->last_keyframe;
1043 rtph265depay->last_keyframe = FALSE;
1044 rtph265depay->picture_start = FALSE;
1049 /* VPS/SPS/PPS/RADL/TSA/RASL/IDR/CRA is considered key, all others DELTA;
1050 * so downstream waiting for keyframe can pick up at VPS/SPS/PPS/IDR */
1052 #define NAL_TYPE_IS_PARAMETER_SET(nt) ( ((nt) == GST_H265_VPS_NUT)\
1053 || ((nt) == GST_H265_SPS_NUT)\
1054 || ((nt) == GST_H265_PPS_NUT) )
1056 #define NAL_TYPE_IS_CODED_SLICE_SEGMENT(nt) ( ((nt) == GST_H265_NAL_SLICE_TRAIL_N)\
1057 || ((nt) == GST_H265_NAL_SLICE_TRAIL_R)\
1058 || ((nt) == GST_H265_NAL_SLICE_TSA_N)\
1059 || ((nt) == GST_H265_NAL_SLICE_TSA_R)\
1060 || ((nt) == GST_H265_NAL_SLICE_STSA_N)\
1061 || ((nt) == GST_H265_NAL_SLICE_STSA_R)\
1062 || ((nt) == GST_H265_NAL_SLICE_RASL_N)\
1063 || ((nt) == GST_H265_NAL_SLICE_RASL_R)\
1064 || ((nt) == GST_H265_NAL_SLICE_BLA_W_LP)\
1065 || ((nt) == GST_H265_NAL_SLICE_BLA_W_RADL)\
1066 || ((nt) == GST_H265_NAL_SLICE_BLA_N_LP)\
1067 || ((nt) == GST_H265_NAL_SLICE_IDR_W_RADL)\
1068 || ((nt) == GST_H265_NAL_SLICE_IDR_N_LP)\
1069 || ((nt) == GST_H265_NAL_SLICE_CRA_NUT) )
1071 /* Intra random access point */
1072 #define NAL_TYPE_IS_IRAP(nt) (((nt) == GST_H265_NAL_SLICE_BLA_W_LP) \
1073 || ((nt) == GST_H265_NAL_SLICE_BLA_W_RADL) \
1074 || ((nt) == GST_H265_NAL_SLICE_BLA_N_LP) \
1075 || ((nt) == GST_H265_NAL_SLICE_IDR_W_RADL) \
1076 || ((nt) == GST_H265_NAL_SLICE_IDR_N_LP) \
1077 || ((nt) == GST_H265_NAL_SLICE_CRA_NUT))
1079 #define NAL_TYPE_IS_KEY(nt) (NAL_TYPE_IS_PARAMETER_SET(nt) || NAL_TYPE_IS_IRAP(nt))
1082 gst_rtp_h265_depay_push (GstRtpH265Depay * rtph265depay, GstBuffer * outbuf,
1083 gboolean keyframe, GstClockTime timestamp, gboolean marker)
1085 /* prepend codec_data */
1086 if (rtph265depay->codec_data) {
1087 GST_DEBUG_OBJECT (rtph265depay, "prepending codec_data");
1088 gst_rtp_copy_video_meta (rtph265depay, rtph265depay->codec_data, outbuf);
1089 outbuf = gst_buffer_append (rtph265depay->codec_data, outbuf);
1090 rtph265depay->codec_data = NULL;
1093 outbuf = gst_buffer_make_writable (outbuf);
1095 gst_rtp_drop_non_video_meta (rtph265depay, outbuf);
1097 GST_BUFFER_PTS (outbuf) = timestamp;
1100 GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
1102 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
1105 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_MARKER);
1107 gst_rtp_base_depayload_push (GST_RTP_BASE_DEPAYLOAD (rtph265depay), outbuf);
1111 gst_rtp_h265_depay_handle_nal (GstRtpH265Depay * rtph265depay, GstBuffer * nal,
1112 GstClockTime in_timestamp, gboolean marker)
1114 GstRTPBaseDepayload *depayload = GST_RTP_BASE_DEPAYLOAD (rtph265depay);
1117 GstBuffer *outbuf = NULL;
1118 GstClockTime out_timestamp;
1119 gboolean keyframe, out_keyframe;
1121 gst_buffer_map (nal, &map, GST_MAP_READ);
1122 if (G_UNLIKELY (map.size < 5))
1125 nal_type = (map.data[4] >> 1) & 0x3f;
1126 GST_DEBUG_OBJECT (rtph265depay, "handle NAL type %d (RTP marker bit %d)",
1129 keyframe = NAL_TYPE_IS_KEY (nal_type);
1131 out_keyframe = keyframe;
1132 out_timestamp = in_timestamp;
1134 if (!rtph265depay->byte_stream) {
1135 if (NAL_TYPE_IS_PARAMETER_SET (nal_type)) {
1136 gst_rtp_h265_depay_add_vps_sps_pps (rtph265depay,
1137 gst_buffer_copy_region (nal, GST_BUFFER_COPY_ALL,
1138 4, gst_buffer_get_size (nal) - 4));
1139 gst_buffer_unmap (nal, &map);
1140 gst_buffer_unref (nal);
1142 } else if (rtph265depay->sps->len == 0 || rtph265depay->pps->len == 0) {
1143 /* Down push down any buffer in non-bytestream mode if the SPS/PPS haven't
1146 gst_pad_push_event (GST_RTP_BASE_DEPAYLOAD_SINKPAD (depayload),
1147 gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM,
1148 gst_structure_new ("GstForceKeyUnit",
1149 "all-headers", G_TYPE_BOOLEAN, TRUE, NULL)));
1150 gst_buffer_unmap (nal, &map);
1151 gst_buffer_unref (nal);
1155 if (rtph265depay->new_codec_data &&
1156 rtph265depay->sps->len > 0 && rtph265depay->pps->len > 0)
1157 gst_rtp_h265_set_src_caps (rtph265depay);
1160 if (rtph265depay->merge) {
1161 gboolean start = FALSE, complete = FALSE;
1163 /* marker bit isn't mandatory so in the following code we try to detect
1164 * an AU boundary (see H.265 spec section 7.4.2.4.4) */
1166 if (NAL_TYPE_IS_CODED_SLICE_SEGMENT (nal_type)) {
1167 /* A NAL unit (X) ends an access unit if the next-occurring VCL NAL unit (Y) has the high-order bit of the first byte after its NAL unit header equal to 1 */
1169 if (((map.data[6] >> 7) & 0x01) == 1) {
1172 } else if ((nal_type >= 32 && nal_type <= 35)
1173 || nal_type == 39 || (nal_type >= 41 && nal_type <= 44)
1174 || (nal_type >= 48 && nal_type <= 55)) {
1175 /* VPS, SPS, PPS, SEI, ... terminate an access unit */
1178 GST_DEBUG_OBJECT (depayload, "start %d, complete %d", start, complete);
1180 if (complete && rtph265depay->picture_start)
1181 outbuf = gst_rtp_h265_complete_au (rtph265depay, &out_timestamp,
1184 /* add to adapter */
1185 gst_buffer_unmap (nal, &map);
1187 GST_DEBUG_OBJECT (depayload, "adding NAL to picture adapter");
1188 gst_adapter_push (rtph265depay->picture_adapter, nal);
1189 rtph265depay->last_ts = in_timestamp;
1190 rtph265depay->last_keyframe |= keyframe;
1191 rtph265depay->picture_start |= start;
1194 outbuf = gst_rtp_h265_complete_au (rtph265depay, &out_timestamp,
1197 /* no merge, output is input nal */
1198 GST_DEBUG_OBJECT (depayload, "using NAL as output");
1200 gst_buffer_unmap (nal, &map);
1204 gst_rtp_h265_depay_push (rtph265depay, outbuf, out_keyframe, out_timestamp,
1213 GST_WARNING_OBJECT (depayload, "dropping short NAL");
1214 gst_buffer_unmap (nal, &map);
1215 gst_buffer_unref (nal);
1221 gst_rtp_h265_finish_fragmentation_unit (GstRtpH265Depay * rtph265depay)
1227 outsize = gst_adapter_available (rtph265depay->adapter);
1228 g_assert (outsize >= 4);
1230 outbuf = gst_adapter_take_buffer (rtph265depay->adapter, outsize);
1232 gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
1233 GST_DEBUG_OBJECT (rtph265depay, "output %d bytes", outsize);
1235 if (rtph265depay->byte_stream) {
1236 memcpy (map.data, sync_bytes, sizeof (sync_bytes));
1238 GST_WRITE_UINT32_BE (map.data, outsize - 4);
1240 gst_buffer_unmap (outbuf, &map);
1242 rtph265depay->current_fu_type = 0;
1244 gst_rtp_h265_depay_handle_nal (rtph265depay, outbuf,
1245 rtph265depay->fu_timestamp, rtph265depay->fu_marker);
1249 gst_rtp_h265_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
1251 GstRtpH265Depay *rtph265depay;
1252 GstBuffer *outbuf = NULL;
1253 guint8 nal_unit_type;
1255 rtph265depay = GST_RTP_H265_DEPAY (depayload);
1257 /* flush remaining data on discont */
1258 if (GST_BUFFER_IS_DISCONT (rtp->buffer)) {
1259 gst_adapter_clear (rtph265depay->adapter);
1260 rtph265depay->wait_start = TRUE;
1261 rtph265depay->current_fu_type = 0;
1262 rtph265depay->last_fu_seqnum = 0;
1270 guint outsize, nalu_size;
1271 GstClockTime timestamp;
1273 guint8 nuh_layer_id, nuh_temporal_id_plus1;
1277 gboolean donl_present = FALSE;
1280 timestamp = GST_BUFFER_PTS (rtp->buffer);
1282 payload_len = gst_rtp_buffer_get_payload_len (rtp);
1283 payload = gst_rtp_buffer_get_payload (rtp);
1284 marker = gst_rtp_buffer_get_marker (rtp);
1286 GST_DEBUG_OBJECT (rtph265depay, "receiving %d bytes", payload_len);
1288 if (payload_len == 0)
1291 /* +---------------+---------------+
1292 * |0|1|2|3|4|5|6|7|0|1|2|3|4|5|6|7|
1293 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1294 * |F| Type | LayerId | TID |
1295 * +-------------+-----------------+
1300 nal_unit_type = (payload[0] >> 1) & 0x3f;
1301 nuh_layer_id = ((payload[0] & 0x01) << 5) | (payload[1] >> 3); /* should be zero for now but this could change in future HEVC extensions */
1302 nuh_temporal_id_plus1 = payload[1] & 0x03;
1304 /* At least two byte header with type */
1307 GST_DEBUG_OBJECT (rtph265depay,
1308 "NAL header nal_unit_type %d, nuh_temporal_id_plus1 %d", nal_unit_type,
1309 nuh_temporal_id_plus1);
1311 GST_FIXME_OBJECT (rtph265depay, "Assuming DONL field is not present");
1313 /* FIXME - assuming DONL field is not present for now */
1314 /*donl_present = (tx-mode == "MST") || (sprop-max-don-diff > 0); */
1316 /* If FU unit was being processed, but the current nal is of a different
1317 * type. Assume that the remote payloader is buggy (didn't set the end bit
1318 * when the FU ended) and send out what we gathered thusfar */
1319 if (G_UNLIKELY (rtph265depay->current_fu_type != 0 &&
1320 nal_unit_type != rtph265depay->current_fu_type))
1321 gst_rtp_h265_finish_fragmentation_unit (rtph265depay);
1323 switch (nal_unit_type) {
1326 GST_DEBUG_OBJECT (rtph265depay, "Processing aggregation packet");
1328 /* Aggregation packet (section 4.7) */
1330 /* An example of an AP packet containing two aggregation units
1331 without the DONL and DOND fields
1334 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
1335 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1337 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1338 | PayloadHdr (Type=48) | NALU 1 Size |
1339 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1341 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ NALU 1 Data |
1344 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1345 | . . . | NALU 2 Size | NALU 2 HDR |
1346 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1348 +-+-+-+-+-+-+-+-+ NALU 2 Data |
1350 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1351 | :...OPTIONAL RTP padding |
1352 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1356 payload += header_len;
1357 payload_len -= header_len;
1359 rtph265depay->wait_start = FALSE;
1363 goto not_implemented_donl_present;
1366 while (payload_len > 2) {
1367 gboolean last = FALSE;
1369 nalu_size = (payload[0] << 8) | payload[1];
1371 /* don't include nalu_size */
1372 if (nalu_size > (payload_len - 2))
1373 nalu_size = payload_len - 2;
1375 outsize = nalu_size + sizeof (sync_bytes);
1376 outbuf = gst_buffer_new_and_alloc (outsize);
1378 gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
1379 if (rtph265depay->byte_stream) {
1380 memcpy (map.data, sync_bytes, sizeof (sync_bytes));
1382 GST_WRITE_UINT32_BE (map.data, nalu_size);
1385 /* strip NALU size */
1389 memcpy (map.data + sizeof (sync_bytes), payload, nalu_size);
1390 gst_buffer_unmap (outbuf, &map);
1392 gst_rtp_copy_video_meta (rtph265depay, outbuf, rtp->buffer);
1394 if (payload_len - nalu_size <= 2)
1397 gst_rtp_h265_depay_handle_nal (rtph265depay, outbuf, timestamp,
1400 payload += nalu_size;
1401 payload_len -= nalu_size;
1407 GST_DEBUG_OBJECT (rtph265depay, "Processing Fragmentation Unit");
1409 /* Fragmentation units (FUs) Section 4.8 */
1411 /* The structure of a Fragmentation Unit (FU)
1413 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
1414 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1415 | PayloadHdr (Type=49) | FU header | DONL (cond) |
1416 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-|
1421 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1422 | :...OPTIONAL RTP padding |
1423 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1429 payload += header_len;
1430 payload_len -= header_len;
1432 /* processing FU header */
1433 S = (payload[0] & 0x80) == 0x80;
1434 E = (payload[0] & 0x40) == 0x40;
1436 GST_DEBUG_OBJECT (rtph265depay,
1437 "FU header with S %d, E %d, nal_unit_type %d", S, E,
1440 if (rtph265depay->wait_start && !S)
1445 goto not_implemented_donl_present;
1450 GST_DEBUG_OBJECT (rtph265depay, "Start of Fragmentation Unit");
1452 /* If a new FU unit started, while still processing an older one.
1453 * Assume that the remote payloader is buggy (doesn't set the end
1454 * bit) and send out what we've gathered thusfar */
1455 if (G_UNLIKELY (rtph265depay->current_fu_type != 0))
1456 gst_rtp_h265_finish_fragmentation_unit (rtph265depay);
1458 rtph265depay->current_fu_type = nal_unit_type;
1459 rtph265depay->fu_timestamp = timestamp;
1460 rtph265depay->last_fu_seqnum = gst_rtp_buffer_get_seq (rtp);
1462 rtph265depay->wait_start = FALSE;
1464 /* reconstruct NAL header */
1466 ((payload[0] & 0x3f) << 9) | (nuh_layer_id << 3) |
1467 nuh_temporal_id_plus1;
1469 /* go back one byte so we can copy the payload + two bytes more in the front which
1470 * will be overwritten by the nal_header
1475 nalu_size = payload_len;
1476 outsize = nalu_size + sizeof (sync_bytes);
1477 outbuf = gst_buffer_new_and_alloc (outsize);
1479 gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
1480 if (rtph265depay->byte_stream) {
1481 GST_WRITE_UINT32_BE (map.data, 0x00000001);
1483 /* will be fixed up in finish_fragmentation_unit() */
1484 GST_WRITE_UINT32_BE (map.data, 0xffffffff);
1486 memcpy (map.data + sizeof (sync_bytes), payload, nalu_size);
1487 map.data[4] = nal_header >> 8;
1488 map.data[5] = nal_header & 0xff;
1489 gst_buffer_unmap (outbuf, &map);
1491 gst_rtp_copy_video_meta (rtph265depay, outbuf, rtp->buffer);
1493 GST_DEBUG_OBJECT (rtph265depay, "queueing %d bytes", outsize);
1495 /* and assemble in the adapter */
1496 gst_adapter_push (rtph265depay->adapter, outbuf);
1498 if (rtph265depay->current_fu_type == 0) {
1499 /* previous FU packet missing start bit? */
1500 GST_WARNING_OBJECT (rtph265depay, "missing FU start bit on an "
1501 "earlier packet. Dropping.");
1502 gst_adapter_clear (rtph265depay->adapter);
1505 if (gst_rtp_buffer_compare_seqnum (rtph265depay->last_fu_seqnum,
1506 gst_rtp_buffer_get_seq (rtp)) != 1) {
1507 /* jump in sequence numbers within an FU is cause for discarding */
1508 GST_WARNING_OBJECT (rtph265depay, "Jump in sequence numbers from "
1509 "%u to %u within Fragmentation Unit. Data was lost, dropping "
1510 "stored.", rtph265depay->last_fu_seqnum,
1511 gst_rtp_buffer_get_seq (rtp));
1512 gst_adapter_clear (rtph265depay->adapter);
1515 rtph265depay->last_fu_seqnum = gst_rtp_buffer_get_seq (rtp);
1517 GST_DEBUG_OBJECT (rtph265depay,
1518 "Following part of Fragmentation Unit");
1520 /* strip off FU header byte */
1524 outsize = payload_len;
1525 outbuf = gst_buffer_new_and_alloc (outsize);
1526 gst_buffer_fill (outbuf, 0, payload, outsize);
1528 gst_rtp_copy_video_meta (rtph265depay, outbuf, rtp->buffer);
1530 GST_DEBUG_OBJECT (rtph265depay, "queueing %d bytes", outsize);
1532 /* and assemble in the adapter */
1533 gst_adapter_push (rtph265depay->adapter, outbuf);
1537 rtph265depay->fu_marker = marker;
1539 /* if NAL unit ends, flush the adapter */
1541 gst_rtp_h265_finish_fragmentation_unit (rtph265depay);
1542 GST_DEBUG_OBJECT (rtph265depay, "End of Fragmentation Unit");
1547 goto not_implemented; /* PACI packets Section 4.9 */
1550 rtph265depay->wait_start = FALSE;
1552 /* All other cases: Single NAL unit packet Section 4.6 */
1553 /* the entire payload is the output buffer */
1557 goto not_implemented_donl_present;
1560 nalu_size = payload_len;
1561 outsize = nalu_size + sizeof (sync_bytes);
1562 outbuf = gst_buffer_new_and_alloc (outsize);
1564 gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
1565 if (rtph265depay->byte_stream) {
1566 memcpy (map.data, sync_bytes, sizeof (sync_bytes));
1568 GST_WRITE_UINT32_BE (map.data, nalu_size);
1570 memcpy (map.data + 4, payload, nalu_size);
1571 gst_buffer_unmap (outbuf, &map);
1573 gst_rtp_copy_video_meta (rtph265depay, outbuf, rtp->buffer);
1575 gst_rtp_h265_depay_handle_nal (rtph265depay, outbuf, timestamp, marker);
1586 GST_DEBUG_OBJECT (rtph265depay, "empty packet");
1591 GST_DEBUG_OBJECT (rtph265depay, "waiting for start");
1595 not_implemented_donl_present:
1597 GST_ELEMENT_ERROR (rtph265depay, STREAM, FORMAT,
1598 (NULL), ("DONL field present not supported yet"));
1604 GST_ELEMENT_ERROR (rtph265depay, STREAM, FORMAT,
1605 (NULL), ("NAL unit type %d not supported yet", nal_unit_type));
1611 gst_rtp_h265_depay_handle_event (GstRTPBaseDepayload * depay, GstEvent * event)
1613 GstRtpH265Depay *rtph265depay;
1615 rtph265depay = GST_RTP_H265_DEPAY (depay);
1617 switch (GST_EVENT_TYPE (event)) {
1618 case GST_EVENT_FLUSH_STOP:
1619 gst_rtp_h265_depay_reset (rtph265depay, FALSE);
1622 gst_rtp_h265_depay_drain (rtph265depay);
1629 GST_RTP_BASE_DEPAYLOAD_CLASS (parent_class)->handle_event (depay, event);
1632 static GstStateChangeReturn
1633 gst_rtp_h265_depay_change_state (GstElement * element,
1634 GstStateChange transition)
1636 GstRtpH265Depay *rtph265depay;
1637 GstStateChangeReturn ret;
1639 rtph265depay = GST_RTP_H265_DEPAY (element);
1641 switch (transition) {
1642 case GST_STATE_CHANGE_NULL_TO_READY:
1644 case GST_STATE_CHANGE_READY_TO_PAUSED:
1645 gst_rtp_h265_depay_reset (rtph265depay, TRUE);
1651 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1653 switch (transition) {
1654 case GST_STATE_CHANGE_PAUSED_TO_READY:
1655 gst_rtp_h265_depay_reset (rtph265depay, TRUE);
1657 case GST_STATE_CHANGE_READY_TO_NULL:
1666 gst_rtp_h265_depay_plugin_init (GstPlugin * plugin)
1668 GST_DEBUG_CATEGORY_INIT (rtph265depay_debug, "rtph265depay", 0,
1669 "H265 Video RTP Depayloader");
1671 return gst_element_register (plugin, "rtph265depay",
1672 GST_RANK_SECONDARY, GST_TYPE_RTP_H265_DEPAY);