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 "gstrtph265depay.h"
32 GST_DEBUG_CATEGORY_STATIC (rtph265depay_debug);
33 #define GST_CAT_DEFAULT (rtph265depay_debug)
35 /* This is what we'll default to when downstream hasn't
36 * expressed a restriction or preference via caps */
37 #define DEFAULT_BYTE_STREAM TRUE
38 #define DEFAULT_ACCESS_UNIT FALSE
40 /* 3 zero bytes syncword */
41 static const guint8 sync_bytes[] = { 0, 0, 0, 1 };
43 static GstStaticPadTemplate gst_rtp_h265_depay_src_template =
44 GST_STATIC_PAD_TEMPLATE ("src",
48 /* FIXME - hvc1 and hev1 formats are not supported yet */
50 "stream-format = (string) hvc1, alignment = (string) au; "
52 "stream-format = (string) hev1, alignment = (string) au; " */
54 "stream-format = (string) byte-stream, alignment = (string) { nal, au }")
57 static GstStaticPadTemplate gst_rtp_h265_depay_sink_template =
58 GST_STATIC_PAD_TEMPLATE ("sink",
61 GST_STATIC_CAPS ("application/x-rtp, "
62 "media = (string) \"video\", "
63 "clock-rate = (int) 90000, " "encoding-name = (string) \"H265\"")
64 /** optional parameters **/
65 /* "profile-space = (int) [ 0, 3 ], " */
66 /* "profile-id = (int) [ 0, 31 ], " */
67 /* "tier-flag = (int) [ 0, 1 ], " */
68 /* "level-id = (int) [ 0, 255 ], " */
69 /* "interop-constraints = (string) ANY, " */
70 /* "profile-compatibility-indicator = (string) ANY, " */
71 /* "sprop-sub-layer-id = (int) [ 0, 6 ], " */
72 /* "recv-sub-layer-id = (int) [ 0, 6 ], " */
73 /* "max-recv-level-id = (int) [ 0, 255 ], " */
74 /* "tx-mode = (string) {MST , SST}, " */
75 /* "sprop-vps = (string) ANY, " */
76 /* "sprop-sps = (string) ANY, " */
77 /* "sprop-pps = (string) ANY, " */
78 /* "sprop-sei = (string) ANY, " */
79 /* "max-lsr = (int) ANY, " *//* MUST be in the range of MaxLumaSR to 16 * MaxLumaSR, inclusive */
80 /* "max-lps = (int) ANY, " *//* MUST be in the range of MaxLumaPS to 16 * MaxLumaPS, inclusive */
81 /* "max-cpb = (int) ANY, " *//* MUST be in the range of MaxCPB to 16 * MaxCPB, inclusive */
82 /* "max-dpb = (int) [1, 16], " */
83 /* "max-br = (int) ANY, " *//* MUST be in the range of MaxBR to 16 * MaxBR, inclusive, for the highest level */
84 /* "max-tr = (int) ANY, " *//* MUST be in the range of MaxTileRows to 16 * MaxTileRows, inclusive, for the highest level */
85 /* "max-tc = (int) ANY, " *//* MUST be in the range of MaxTileCols to 16 * MaxTileCols, inclusive, for the highest level */
86 /* "max-fps = (int) ANY, " */
87 /* "sprop-max-don-diff = (int) [0, 32767], " */
88 /* "sprop-depack-buf-nalus = (int) [0, 32767], " */
89 /* "sprop-depack-buf-nalus = (int) [0, 4294967295], " */
90 /* "depack-buf-cap = (int) [1, 4294967295], " */
91 /* "sprop-segmentation-id = (int) [0, 3], " */
92 /* "sprop-spatial-segmentation-idc = (string) ANY, " */
93 /* "dec-parallel-cap = (string) ANY, " */
96 #define gst_rtp_h265_depay_parent_class parent_class
97 G_DEFINE_TYPE (GstRtpH265Depay, gst_rtp_h265_depay,
98 GST_TYPE_RTP_BASE_DEPAYLOAD);
100 static void gst_rtp_h265_depay_finalize (GObject * object);
102 static GstStateChangeReturn gst_rtp_h265_depay_change_state (GstElement *
103 element, GstStateChange transition);
105 static GstBuffer *gst_rtp_h265_depay_process (GstRTPBaseDepayload * depayload,
107 static gboolean gst_rtp_h265_depay_setcaps (GstRTPBaseDepayload * filter,
109 static gboolean gst_rtp_h265_depay_handle_event (GstRTPBaseDepayload * depay,
113 gst_rtp_h265_depay_class_init (GstRtpH265DepayClass * klass)
115 GObjectClass *gobject_class;
116 GstElementClass *gstelement_class;
117 GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
119 gobject_class = (GObjectClass *) klass;
120 gstelement_class = (GstElementClass *) klass;
121 gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
123 gobject_class->finalize = gst_rtp_h265_depay_finalize;
125 gst_element_class_add_pad_template (gstelement_class,
126 gst_static_pad_template_get (&gst_rtp_h265_depay_src_template));
127 gst_element_class_add_pad_template (gstelement_class,
128 gst_static_pad_template_get (&gst_rtp_h265_depay_sink_template));
130 gst_element_class_set_static_metadata (gstelement_class,
131 "RTP H265 depayloader", "Codec/Depayloader/Network/RTP",
132 "Extracts H265 video from RTP packets (draft-ietf-payload-rtp-h265-03.txt)",
133 "Jurgen Slowack <jurgenslowack@gmail.com>");
134 gstelement_class->change_state = gst_rtp_h265_depay_change_state;
136 gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_h265_depay_process;
137 gstrtpbasedepayload_class->set_caps = gst_rtp_h265_depay_setcaps;
138 gstrtpbasedepayload_class->handle_event = gst_rtp_h265_depay_handle_event;
142 gst_rtp_h265_depay_init (GstRtpH265Depay * rtph265depay)
144 rtph265depay->adapter = gst_adapter_new ();
145 rtph265depay->picture_adapter = gst_adapter_new ();
146 rtph265depay->byte_stream = DEFAULT_BYTE_STREAM;
147 rtph265depay->stream_format = (gchar *) g_malloc (10);
148 rtph265depay->merge = DEFAULT_ACCESS_UNIT;
149 rtph265depay->vps = g_ptr_array_new_with_free_func (
150 (GDestroyNotify) gst_buffer_unref);
151 rtph265depay->sps = g_ptr_array_new_with_free_func (
152 (GDestroyNotify) gst_buffer_unref);
153 rtph265depay->pps = g_ptr_array_new_with_free_func (
154 (GDestroyNotify) gst_buffer_unref);
158 gst_rtp_h265_depay_reset (GstRtpH265Depay * rtph265depay)
160 gst_adapter_clear (rtph265depay->adapter);
161 rtph265depay->wait_start = TRUE;
162 gst_adapter_clear (rtph265depay->picture_adapter);
163 rtph265depay->picture_start = FALSE;
164 rtph265depay->last_keyframe = FALSE;
165 rtph265depay->last_ts = 0;
166 rtph265depay->current_fu_type = 0;
167 rtph265depay->new_codec_data = FALSE;
168 g_ptr_array_set_size (rtph265depay->vps, 0);
169 g_ptr_array_set_size (rtph265depay->sps, 0);
170 g_ptr_array_set_size (rtph265depay->pps, 0);
174 gst_rtp_h265_depay_finalize (GObject * object)
176 GstRtpH265Depay *rtph265depay;
178 rtph265depay = GST_RTP_H265_DEPAY (object);
180 if (rtph265depay->codec_data)
181 gst_buffer_unref (rtph265depay->codec_data);
183 g_free (rtph265depay->stream_format);
185 g_object_unref (rtph265depay->adapter);
186 g_object_unref (rtph265depay->picture_adapter);
188 g_ptr_array_free (rtph265depay->vps, TRUE);
189 g_ptr_array_free (rtph265depay->sps, TRUE);
190 g_ptr_array_free (rtph265depay->pps, TRUE);
192 G_OBJECT_CLASS (parent_class)->finalize (object);
196 gst_rtp_h265_depay_negotiate (GstRtpH265Depay * rtph265depay)
199 gint byte_stream = -1;
203 gst_pad_get_allowed_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (rtph265depay));
205 GST_DEBUG_OBJECT (rtph265depay, "allowed caps: %" GST_PTR_FORMAT, caps);
208 if (gst_caps_get_size (caps) > 0) {
209 GstStructure *s = gst_caps_get_structure (caps, 0);
210 const gchar *str = NULL;
212 if ((str = gst_structure_get_string (s, "stream-format"))) {
214 strcpy (rtph265depay->stream_format, str);
216 if (strcmp (str, "hev1") == 0) {
218 } else if (strcmp (str, "hvc1") == 0) {
220 } else if (strcmp (str, "byte-stream") == 0) {
223 GST_DEBUG_OBJECT (rtph265depay, "unknown stream-format: %s", str);
227 if ((str = gst_structure_get_string (s, "alignment"))) {
228 if (strcmp (str, "au") == 0) {
230 } else if (strcmp (str, "nal") == 0) {
233 GST_DEBUG_OBJECT (rtph265depay, "unknown alignment: %s", str);
237 gst_caps_unref (caps);
240 if (byte_stream != -1) {
241 GST_DEBUG_OBJECT (rtph265depay, "downstream requires byte-stream %d",
243 rtph265depay->byte_stream = byte_stream;
245 GST_DEBUG_OBJECT (rtph265depay, "defaulting to byte-stream %d",
246 DEFAULT_BYTE_STREAM);
247 strcpy (rtph265depay->stream_format, "byte-stream");
248 rtph265depay->byte_stream = DEFAULT_BYTE_STREAM;
251 GST_DEBUG_OBJECT (rtph265depay, "downstream requires merge %d", merge);
252 rtph265depay->merge = merge;
254 GST_DEBUG_OBJECT (rtph265depay, "defaulting to merge %d",
255 DEFAULT_ACCESS_UNIT);
256 rtph265depay->merge = DEFAULT_ACCESS_UNIT;
260 /* Stolen from bad/gst/mpegtsdemux/payloader_parsers.c */
261 /* variable length Exp-Golomb parsing according to H.265 spec section 9.2*/
263 read_golomb (GstBitReader * br, guint32 * value)
265 guint8 b, leading_zeros = -1;
268 for (b = 0; !b; leading_zeros++) {
269 if (!gst_bit_reader_get_bits_uint8 (br, &b, 1))
274 *value = (*value >> 1) - 1;
275 if (leading_zeros > 0) {
277 if (!gst_bit_reader_get_bits_uint32 (br, &tmp, leading_zeros))
286 parse_sps (GstMapInfo * map, guint32 * sps_id)
287 { /* To parse seq_parameter_set_id */
288 GstBitReader br = GST_BIT_READER_INIT (map->data + 15,
294 if (!read_golomb (&br, sps_id))
301 parse_pps (GstMapInfo * map, guint32 * sps_id, guint32 * pps_id)
302 { /* To parse picture_parameter_set_id */
303 GstBitReader br = GST_BIT_READER_INIT (map->data + 2,
309 if (!read_golomb (&br, pps_id))
311 if (!read_golomb (&br, sps_id))
319 gst_rtp_h265_set_src_caps (GstRtpH265Depay * rtph265depay)
324 if (!rtph265depay->byte_stream &&
325 (!rtph265depay->new_codec_data ||
326 rtph265depay->vps->len == 0 || rtph265depay->sps->len == 0
327 || rtph265depay->pps->len == 0))
330 srccaps = gst_caps_new_simple ("video/x-h265",
331 "stream-format", G_TYPE_STRING,
332 rtph265depay->stream_format,
333 "alignment", G_TYPE_STRING, rtph265depay->merge ? "au" : "nal", NULL);
335 if (!rtph265depay->byte_stream) {
337 GstBuffer *codec_data;
340 guint num_vps = rtph265depay->vps->len;
341 guint num_sps = rtph265depay->sps->len;
342 guint num_pps = rtph265depay->pps->len;
343 GstMapInfo map, nalmap;
346 guint8 num_arrays = 0;
351 guint32 max_sub_layers_minus1, temporal_id_nesting_flag, chroma_format_idc,
352 bit_depth_luma_minus8, bit_depth_chroma_minus8,
353 min_spatial_segmentation_idc;
355 /* Fixme: Current implementation is not embedding SEI in codec_data */
360 /* start with 23 bytes header */
363 num_arrays = (num_vps > 0) + (num_sps > 0) + (num_pps > 0);
366 /* add size of vps, sps & pps */
367 for (i = 0; i < num_vps; i++)
368 len += 2 + gst_buffer_get_size (g_ptr_array_index (rtph265depay->vps, i));
369 for (i = 0; i < num_sps; i++)
370 len += 2 + gst_buffer_get_size (g_ptr_array_index (rtph265depay->sps, i));
371 for (i = 0; i < num_pps; i++)
372 len += 2 + gst_buffer_get_size (g_ptr_array_index (rtph265depay->pps, i));
374 GST_DEBUG_OBJECT (rtph265depay,
375 "constructing codec_data: num_vps =%d num_sps=%d, num_pps=%d", num_vps,
378 codec_data = gst_buffer_new_and_alloc (len);
379 g_debug ("alloc_len: %u", len);
380 gst_buffer_map (codec_data, &map, GST_MAP_READWRITE);
383 memset (data, 0, map.size);
385 /* Parsing sps to get the info required further on */
387 gst_buffer_map (g_ptr_array_index (rtph265depay->sps, 0), &nalmap,
390 max_sub_layers_minus1 = ((nalmap.data[2]) >> 1) & 0x07;
391 temporal_id_nesting_flag = nalmap.data[2] & 0x01;
393 gst_bit_reader_init (&br, nalmap.data + 15, nalmap.size - 15);
395 read_golomb (&br, &tmp); /* sps_seq_parameter_set_id */
396 read_golomb (&br, &chroma_format_idc); /* chroma_format_idc */
398 if (chroma_format_idc == 3)
400 gst_bit_reader_get_bits_uint8 (&br, &tmp8, 1); /* separate_colour_plane_flag */
402 read_golomb (&br, &tmp); /* pic_width_in_luma_samples */
403 read_golomb (&br, &tmp); /* pic_height_in_luma_samples */
405 gst_bit_reader_get_bits_uint8 (&br, &tmp8, 1); /* conformance_window_flag */
407 read_golomb (&br, &tmp); /* conf_win_left_offset */
408 read_golomb (&br, &tmp); /* conf_win_right_offset */
409 read_golomb (&br, &tmp); /* conf_win_top_offset */
410 read_golomb (&br, &tmp); /* conf_win_bottom_offset */
413 read_golomb (&br, &bit_depth_luma_minus8); /* bit_depth_luma_minus8 */
414 read_golomb (&br, &bit_depth_chroma_minus8); /* bit_depth_chroma_minus8 */
416 GST_DEBUG_OBJECT (rtph265depay,
417 "Ignoring min_spatial_segmentation for now (assuming zero)");
419 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 */
423 gst_buffer_unmap (g_ptr_array_index (rtph265depay->sps, 0), &nalmap);
425 /* HEVCDecoderConfigurationVersion = 1 */
428 /* Copy from profile_tier_level (Rec. ITU-T H.265 (04/2013) section 7.3.3
430 * profile_space | tier_flat | profile_idc |
431 * profile_compatibility_flags | constraint_indicator_flags |
432 * level_idc | progressive_source_flag | interlaced_source_flag
433 * non_packed_constraint_flag | frame_only_constraint_flag
434 * reserved_zero_44bits | level_idc */
435 gst_buffer_map (g_ptr_array_index (rtph265depay->sps, 0), &nalmap,
437 for (i = 0; i < 12; i++)
438 data[i + 1] = nalmap.data[i];
439 gst_buffer_unmap (g_ptr_array_index (rtph265depay->sps, 0), &nalmap);
441 /* min_spatial_segmentation_idc */
442 GST_WRITE_UINT16_BE (data + 13, min_spatial_segmentation_idc);
444 data[15] = 0xfc; /* keeping parrallelismType as zero (unknown) */
445 data[16] = 0xfc | chroma_format_idc;
446 data[17] = 0xf8 | bit_depth_luma_minus8;
447 data[18] = 0xf8 | bit_depth_chroma_minus8;
448 data[19] = 0x00; /* keep avgFrameRate as unspecified */
449 data[20] = 0x00; /* keep avgFrameRate as unspecified */
450 /* constFrameRate(2 bits): 0, stream may or may not be of constant framerate
451 * numTemporalLayers (3 bits): number of temporal layers, value from SPS
452 * TemporalIdNested (1 bit): sps_temporal_id_nesting_flag from SPS
453 * lengthSizeMinusOne (2 bits): plus 1 indicates the length of the NALUnitLength */
455 0x00 | ((max_sub_layers_minus1 +
456 1) << 3) | (temporal_id_nesting_flag << 2) | (nl - 1);
457 GST_WRITE_UINT8 (data + 22, num_arrays); /* numOfArrays */
463 /* array_completeness | reserved_zero bit | nal_unit_type */
464 data[0] = 0x00 | 0x20;
467 GST_WRITE_UINT16_BE (data, num_vps);
470 for (i = 0; i < num_vps; i++) {
472 gst_buffer_get_size (g_ptr_array_index (rtph265depay->vps, i));
473 GST_WRITE_UINT16_BE (data, nal_size);
474 gst_buffer_extract (g_ptr_array_index (rtph265depay->vps, i), 0,
476 data += 2 + nal_size;
477 GST_DEBUG_OBJECT (rtph265depay, "Copied VPS %d of length %u", i,
484 /* array_completeness | reserved_zero bit | nal_unit_type */
485 data[0] = 0x00 | 0x21;
488 GST_WRITE_UINT16_BE (data, num_sps);
491 for (i = 0; i < num_sps; i++) {
493 gst_buffer_get_size (g_ptr_array_index (rtph265depay->sps, i));
494 GST_WRITE_UINT16_BE (data, nal_size);
495 gst_buffer_extract (g_ptr_array_index (rtph265depay->sps, i), 0,
497 data += 2 + nal_size;
498 GST_DEBUG_OBJECT (rtph265depay, "Copied SPS %d of length %u", i,
505 /* array_completeness | reserved_zero bit | nal_unit_type */
506 data[0] = 0x00 | 0x22;
509 GST_WRITE_UINT16_BE (data, num_pps);
512 for (i = 0; i < num_pps; i++) {
514 gst_buffer_get_size (g_ptr_array_index (rtph265depay->pps, i));
515 GST_WRITE_UINT16_BE (data, nal_size);
516 gst_buffer_extract (g_ptr_array_index (rtph265depay->pps, i), 0,
518 data += 2 + nal_size;
519 GST_DEBUG_OBJECT (rtph265depay, "Copied PPS %d of length %u", i,
524 new_size = data - map.data;
525 gst_buffer_unmap (codec_data, &map);
526 gst_buffer_set_size (codec_data, new_size);
528 gst_caps_set_simple (srccaps,
529 "codec_data", GST_TYPE_BUFFER, codec_data, NULL);
530 gst_buffer_unref (codec_data);
533 if (gst_pad_has_current_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (rtph265depay))) {
535 gst_pad_get_current_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (rtph265depay));
537 /* Only update the caps if they are not equal. For
538 * AVC we don't update caps if only the codec_data
539 * changes. This is the same behaviour as in h264parse
540 * and gstrtph264depay
542 if (rtph265depay->byte_stream) {
543 if (!gst_caps_is_equal (srccaps, old_caps))
545 gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (rtph265depay),
550 GstCaps *tmp_caps = gst_caps_copy (srccaps);
551 GstStructure *old_s, *tmp_s;
553 old_s = gst_caps_get_structure (old_caps, 0);
554 tmp_s = gst_caps_get_structure (tmp_caps, 0);
555 if (gst_structure_has_field (old_s, "codec_data"))
556 gst_structure_set_value (tmp_s, "codec_data",
557 gst_structure_get_value (old_s, "codec_data"));
559 if (!gst_caps_is_equal (old_caps, tmp_caps))
561 gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (rtph265depay),
566 gst_caps_unref (tmp_caps);
570 gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (rtph265depay),
574 gst_caps_unref (srccaps);
576 /* Insert SPS and PPS into the stream on next opportunity */
577 if (rtph265depay->sps->len > 0 || rtph265depay->pps->len > 0) {
579 GstBuffer *codec_data;
584 for (i = 0; i < rtph265depay->sps->len; i++) {
585 len += 4 + gst_buffer_get_size (g_ptr_array_index (rtph265depay->sps, i));
588 for (i = 0; i < rtph265depay->pps->len; i++) {
589 len += 4 + gst_buffer_get_size (g_ptr_array_index (rtph265depay->pps, i));
592 codec_data = gst_buffer_new_and_alloc (len);
593 gst_buffer_map (codec_data, &map, GST_MAP_WRITE);
596 for (i = 0; i < rtph265depay->sps->len; i++) {
597 GstBuffer *sps_buf = g_ptr_array_index (rtph265depay->sps, i);
598 guint sps_size = gst_buffer_get_size (sps_buf);
600 if (rtph265depay->byte_stream)
601 memcpy (data, sync_bytes, sizeof (sync_bytes));
603 GST_WRITE_UINT32_BE (data, sps_size);
604 gst_buffer_extract (sps_buf, 0, data + 4, -1);
605 data += 4 + sps_size;
608 for (i = 0; i < rtph265depay->pps->len; i++) {
609 GstBuffer *pps_buf = g_ptr_array_index (rtph265depay->pps, i);
610 guint pps_size = gst_buffer_get_size (pps_buf);
612 if (rtph265depay->byte_stream)
613 memcpy (data, sync_bytes, sizeof (sync_bytes));
615 GST_WRITE_UINT32_BE (data, pps_size);
616 gst_buffer_extract (pps_buf, 0, data + 4, -1);
617 data += 4 + pps_size;
620 gst_buffer_unmap (codec_data, &map);
621 if (rtph265depay->codec_data)
622 gst_buffer_unref (rtph265depay->codec_data);
623 rtph265depay->codec_data = codec_data;
627 rtph265depay->new_codec_data = FALSE;
633 gst_rtp_h265_add_vps_sps_pps (GstElement * rtph265, GPtrArray * vps_array,
634 GPtrArray * sps_array, GPtrArray * pps_array, GstBuffer * nal)
640 gst_buffer_map (nal, &map, GST_MAP_READ);
642 type = (map.data[0] >> 1) & 0x3f;
644 if (type == GST_H265_VPS_NUT) {
645 guint32 vps_id = (map.data[2] >> 4) & 0x0f;
647 for (i = 0; i < vps_array->len; i++) {
648 GstBuffer *vps = g_ptr_array_index (vps_array, i);
652 gst_buffer_map (vps, &vpsmap, GST_MAP_READ);
653 tmp_vps_id = (vpsmap.data[2] >> 4) & 0x0f;
655 if (vps_id == tmp_vps_id) {
656 if (map.size == vpsmap.size &&
657 memcmp (map.data, vpsmap.data, vpsmap.size) == 0) {
658 GST_LOG_OBJECT (rtph265, "Unchanged VPS %u, not updating", vps_id);
659 gst_buffer_unmap (vps, &vpsmap);
662 gst_buffer_unmap (vps, &vpsmap);
663 g_ptr_array_remove_index_fast (vps_array, i);
664 g_ptr_array_add (vps_array, nal);
665 GST_LOG_OBJECT (rtph265, "Modified VPS %u, replacing", vps_id);
669 gst_buffer_unmap (vps, &vpsmap);
671 GST_LOG_OBJECT (rtph265, "Adding new VPS %u", vps_id);
672 g_ptr_array_add (vps_array, nal);
673 } else if (type == GST_H265_SPS_NUT) {
676 if (!parse_sps (&map, &sps_id)) {
677 GST_WARNING_OBJECT (rtph265, "Invalid SPS,"
678 " can't parse seq_parameter_set_id");
682 for (i = 0; i < sps_array->len; i++) {
683 GstBuffer *sps = g_ptr_array_index (sps_array, i);
687 gst_buffer_map (sps, &spsmap, GST_MAP_READ);
688 parse_sps (&spsmap, &tmp_sps_id);
690 if (sps_id == tmp_sps_id) {
691 if (map.size == spsmap.size &&
692 memcmp (map.data, spsmap.data, spsmap.size) == 0) {
693 GST_LOG_OBJECT (rtph265, "Unchanged SPS %u, not updating", sps_id);
694 gst_buffer_unmap (sps, &spsmap);
697 gst_buffer_unmap (sps, &spsmap);
698 g_ptr_array_remove_index_fast (sps_array, i);
699 g_ptr_array_add (sps_array, nal);
700 GST_LOG_OBJECT (rtph265, "Modified SPS %u, replacing", sps_id);
704 gst_buffer_unmap (sps, &spsmap);
706 GST_LOG_OBJECT (rtph265, "Adding new SPS %u", sps_id);
707 g_ptr_array_add (sps_array, nal);
708 } else if (type == GST_H265_PPS_NUT) {
712 if (!parse_pps (&map, &sps_id, &pps_id)) {
713 GST_WARNING_OBJECT (rtph265, "Invalid PPS,"
714 " can't parse seq_parameter_set_id or pic_parameter_set_id");
718 for (i = 0; i < pps_array->len; i++) {
719 GstBuffer *pps = g_ptr_array_index (pps_array, i);
725 gst_buffer_map (pps, &ppsmap, GST_MAP_READ);
726 parse_pps (&ppsmap, &tmp_sps_id, &tmp_pps_id);
728 if (pps_id == tmp_pps_id) {
729 if (map.size == ppsmap.size &&
730 memcmp (map.data, ppsmap.data, ppsmap.size) == 0) {
731 GST_LOG_OBJECT (rtph265, "Unchanged PPS %u:%u, not updating", sps_id,
733 gst_buffer_unmap (pps, &ppsmap);
736 gst_buffer_unmap (pps, &ppsmap);
737 g_ptr_array_remove_index_fast (pps_array, i);
738 g_ptr_array_add (pps_array, nal);
739 GST_LOG_OBJECT (rtph265, "Modified PPS %u:%u, replacing",
744 gst_buffer_unmap (pps, &ppsmap);
746 GST_LOG_OBJECT (rtph265, "Adding new PPS %u:%i", sps_id, pps_id);
747 g_ptr_array_add (pps_array, nal);
753 gst_buffer_unmap (nal, &map);
758 gst_buffer_unmap (nal, &map);
759 gst_buffer_unref (nal);
766 gst_rtp_h265_depay_add_vps_sps_pps (GstRtpH265Depay * rtph265depay,
769 if (gst_rtp_h265_add_vps_sps_pps (GST_ELEMENT (rtph265depay),
770 rtph265depay->vps, rtph265depay->sps, rtph265depay->pps, nal))
771 rtph265depay->new_codec_data = TRUE;
775 gst_rtp_h265_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
778 GstStructure *structure = gst_caps_get_structure (caps, 0);
779 GstRtpH265Depay *rtph265depay;
781 GstBuffer *codec_data;
785 rtph265depay = GST_RTP_H265_DEPAY (depayload);
787 if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
789 depayload->clock_rate = clock_rate;
791 /* Base64 encoded, comma separated config NALs */
792 ps = gst_structure_get_string (structure, "sprop-parameter-sets");
794 /* negotiate with downstream w.r.t. output format and alignment */
795 gst_rtp_h265_depay_negotiate (rtph265depay);
797 if (rtph265depay->byte_stream && ps != NULL) {
798 /* for bytestream we only need the parameter sets but we don't error out
799 * when they are not there, we assume they are in the stream. */
804 params = g_strsplit (ps, ",", 0);
806 /* count total number of bytes in base64. Also include the sync bytes in
807 * front of the params. */
809 for (i = 0; params[i]; i++) {
810 len += strlen (params[i]);
811 len += sizeof (sync_bytes);
813 /* we seriously overshoot the length, but it's fine. */
814 codec_data = gst_buffer_new_and_alloc (len);
816 gst_buffer_map (codec_data, &map, GST_MAP_WRITE);
819 for (i = 0; params[i]; i++) {
823 GST_DEBUG_OBJECT (depayload, "decoding param %d (%s)", i, params[i]);
824 memcpy (ptr, sync_bytes, sizeof (sync_bytes));
825 ptr += sizeof (sync_bytes);
827 g_base64_decode_step (params[i], strlen (params[i]), ptr, &state,
829 GST_DEBUG_OBJECT (depayload, "decoded %d bytes", len);
830 total += len + sizeof (sync_bytes);
833 gst_buffer_unmap (codec_data, &map);
834 gst_buffer_resize (codec_data, 0, total);
837 /* keep the codec_data, we need to send it as the first buffer. We cannot
838 * push it in the adapter because the adapter might be flushed on discont.
840 if (rtph265depay->codec_data)
841 gst_buffer_unref (rtph265depay->codec_data);
842 rtph265depay->codec_data = codec_data;
843 } else if (!rtph265depay->byte_stream) {
848 goto incomplete_caps;
850 params = g_strsplit (ps, ",", 0);
852 GST_DEBUG_OBJECT (depayload, "we have %d params", g_strv_length (params));
854 /* start with 23 bytes header */
855 for (i = 0; params[i]; i++) {
862 nal_len = strlen (params[i]);
863 nal = gst_buffer_new_and_alloc (nal_len);
864 gst_buffer_map (nal, &nalmap, GST_MAP_READWRITE);
867 g_base64_decode_step (params[i], nal_len, nalmap.data, &state, &save);
869 GST_DEBUG_OBJECT (depayload, "adding param %d as %s", i,
870 (((nalmap.data[0] >> 1) & 0x3f) ==
871 32) ? "VPS" : (((nalmap.data[0] >> 1) & 0x3f) ==
872 33) ? "SPS" : "PPS");
874 gst_buffer_unmap (nal, &nalmap);
875 gst_buffer_set_size (nal, nal_len);
877 gst_rtp_h265_depay_add_vps_sps_pps (rtph265depay, nal);
881 if (rtph265depay->sps->len == 0 || rtph265depay->pps->len == 0)
882 goto incomplete_caps;
885 return gst_rtp_h265_set_src_caps (rtph265depay);
890 GST_DEBUG_OBJECT (depayload, "we have incomplete caps,"
891 " doing setcaps later");
897 gst_rtp_h265_complete_au (GstRtpH265Depay * rtph265depay,
898 GstClockTime * out_timestamp, gboolean * out_keyframe)
903 /* we had a picture in the adapter and we completed it */
904 GST_DEBUG_OBJECT (rtph265depay, "taking completed AU");
905 outsize = gst_adapter_available (rtph265depay->picture_adapter);
906 outbuf = gst_adapter_take_buffer (rtph265depay->picture_adapter, outsize);
908 *out_timestamp = rtph265depay->last_ts;
909 *out_keyframe = rtph265depay->last_keyframe;
911 rtph265depay->last_keyframe = FALSE;
912 rtph265depay->picture_start = FALSE;
917 /* VPS/SPS/PPS/RADL/TSA/RASL/IDR/CRA is considered key, all others DELTA;
918 * so downstream waiting for keyframe can pick up at VPS/SPS/PPS/IDR */
920 #define NAL_TYPE_IS_PARAMETER_SET(nt) ( ((nt) == GST_H265_VPS_NUT)\
921 || ((nt) == GST_H265_SPS_NUT)\
922 || ((nt) == GST_H265_PPS_NUT) )
924 #define NAL_TYPE_IS_CODED_SLICE_SEGMENT(nt) ( ((nt) == GST_H265_NAL_SLICE_TRAIL_N)\
925 || ((nt) == GST_H265_NAL_SLICE_TRAIL_R)\
926 || ((nt) == GST_H265_NAL_SLICE_TSA_N)\
927 || ((nt) == GST_H265_NAL_SLICE_TSA_R)\
928 || ((nt) == GST_H265_NAL_SLICE_STSA_N)\
929 || ((nt) == GST_H265_NAL_SLICE_STSA_R)\
930 || ((nt) == GST_H265_NAL_SLICE_RASL_N)\
931 || ((nt) == GST_H265_NAL_SLICE_RASL_R)\
932 || ((nt) == GST_H265_NAL_SLICE_BLA_W_LP)\
933 || ((nt) == GST_H265_NAL_SLICE_BLA_W_RADL)\
934 || ((nt) == GST_H265_NAL_SLICE_BLA_N_LP)\
935 || ((nt) == GST_H265_NAL_SLICE_IDR_W_RADL)\
936 || ((nt) == GST_H265_NAL_SLICE_IDR_N_LP)\
937 || ((nt) == GST_H265_NAL_SLICE_CRA_NUT) )
939 #define NAL_TYPE_IS_KEY(nt) (NAL_TYPE_IS_PARAMETER_SET(nt) || NAL_TYPE_IS_CODED_SLICE_SEGMENT(nt))
942 gst_rtp_h265_depay_handle_nal (GstRtpH265Depay * rtph265depay, GstBuffer * nal,
943 GstClockTime in_timestamp, gboolean marker)
945 GstRTPBaseDepayload *depayload = GST_RTP_BASE_DEPAYLOAD (rtph265depay);
948 GstBuffer *outbuf = NULL;
949 GstClockTime out_timestamp;
950 gboolean keyframe, out_keyframe;
952 gst_buffer_map (nal, &map, GST_MAP_READ);
953 if (G_UNLIKELY (map.size < 5))
956 nal_type = (map.data[4] >> 1) & 0x3f;
957 GST_DEBUG_OBJECT (rtph265depay, "handle NAL type %d (RTP marker bit %d)",
960 keyframe = NAL_TYPE_IS_KEY (nal_type);
962 out_keyframe = keyframe;
963 out_timestamp = in_timestamp;
965 if (!rtph265depay->byte_stream) {
966 if (NAL_TYPE_IS_PARAMETER_SET (nal_type)) {
967 gst_rtp_h265_depay_add_vps_sps_pps (rtph265depay,
968 gst_buffer_copy_region (nal, GST_BUFFER_COPY_ALL,
969 4, gst_buffer_get_size (nal) - 4));
970 gst_buffer_unmap (nal, &map);
971 gst_buffer_unref (nal);
973 } else if (rtph265depay->sps->len == 0 || rtph265depay->pps->len == 0) {
974 /* Down push down any buffer in non-bytestream mode if the SPS/PPS haven't
977 gst_pad_push_event (GST_RTP_BASE_DEPAYLOAD_SINKPAD (depayload),
978 gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM,
979 gst_structure_new ("GstForceKeyUnit",
980 "all-headers", G_TYPE_BOOLEAN, TRUE, NULL)));
981 gst_buffer_unmap (nal, &map);
982 gst_buffer_unref (nal);
986 if (rtph265depay->new_codec_data &&
987 rtph265depay->sps->len > 0 && rtph265depay->pps->len > 0)
988 gst_rtp_h265_set_src_caps (rtph265depay);
991 if (rtph265depay->merge) {
992 gboolean start = FALSE, complete = FALSE;
994 /* marker bit isn't mandatory so in the following code we try to detect
995 * an AU boundary (see H.265 spec section 7.4.2.4.4) */
998 if (NAL_TYPE_IS_CODED_SLICE_SEGMENT (nal_type)) {
999 /* 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 */
1001 if (((map.data[6] >> 7) & 0x01) == 1) {
1005 } else if ((nal_type >= 32 && nal_type <= 35)
1006 || nal_type == 39 || (nal_type >= 41 && nal_type <= 44)
1007 || (nal_type >= 48 && nal_type <= 55)) {
1008 /* VPS, SPS, PPS, SEI, ... terminate an access unit */
1011 GST_DEBUG_OBJECT (depayload, "start %d, complete %d", start, complete);
1013 if (complete && rtph265depay->picture_start)
1014 outbuf = gst_rtp_h265_complete_au (rtph265depay, &out_timestamp,
1017 /* add to adapter */
1018 gst_buffer_unmap (nal, &map);
1020 GST_DEBUG_OBJECT (depayload, "adding NAL to picture adapter");
1021 gst_adapter_push (rtph265depay->picture_adapter, nal);
1022 rtph265depay->last_ts = in_timestamp;
1023 rtph265depay->last_keyframe |= keyframe;
1024 rtph265depay->picture_start |= start;
1027 outbuf = gst_rtp_h265_complete_au (rtph265depay, &out_timestamp,
1030 /* no merge, output is input nal */
1031 GST_DEBUG_OBJECT (depayload, "using NAL as output");
1033 gst_buffer_unmap (nal, &map);
1037 /* prepend codec_data */
1038 if (rtph265depay->codec_data) {
1039 GST_DEBUG_OBJECT (depayload, "prepending codec_data");
1040 outbuf = gst_buffer_append (rtph265depay->codec_data, outbuf);
1041 rtph265depay->codec_data = NULL;
1042 out_keyframe = TRUE;
1044 outbuf = gst_buffer_make_writable (outbuf);
1046 GST_BUFFER_PTS (outbuf) = out_timestamp;
1049 GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
1051 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
1059 GST_WARNING_OBJECT (depayload, "dropping short NAL");
1060 gst_buffer_unmap (nal, &map);
1061 gst_buffer_unref (nal);
1067 gst_rtp_h265_push_fragmentation_unit (GstRtpH265Depay * rtph265depay,
1074 outsize = gst_adapter_available (rtph265depay->adapter);
1075 outbuf = gst_adapter_take_buffer (rtph265depay->adapter, outsize);
1077 gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
1078 GST_DEBUG_OBJECT (rtph265depay, "output %d bytes", outsize);
1080 if (rtph265depay->byte_stream) {
1081 memcpy (map.data, sync_bytes, sizeof (sync_bytes));
1083 goto not_implemented;
1085 gst_buffer_unmap (outbuf, &map);
1087 rtph265depay->current_fu_type = 0;
1089 outbuf = gst_rtp_h265_depay_handle_nal (rtph265depay, outbuf,
1090 rtph265depay->fu_timestamp, rtph265depay->fu_marker);
1092 if (send && outbuf) {
1093 gst_rtp_base_depayload_push (GST_RTP_BASE_DEPAYLOAD (rtph265depay), outbuf);
1100 GST_ERROR_OBJECT (rtph265depay,
1101 ("Only bytestream format is currently supported."));
1102 gst_buffer_unmap (outbuf, &map);
1108 gst_rtp_h265_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
1110 GstRtpH265Depay *rtph265depay;
1111 GstBuffer *outbuf = NULL;
1112 guint8 nal_unit_type;
1114 rtph265depay = GST_RTP_H265_DEPAY (depayload);
1116 /* flush remaining data on discont */
1117 if (GST_BUFFER_IS_DISCONT (rtp->buffer)) {
1118 gst_adapter_clear (rtph265depay->adapter);
1119 rtph265depay->wait_start = TRUE;
1120 rtph265depay->current_fu_type = 0;
1128 guint outsize, nalu_size;
1129 GstClockTime timestamp;
1131 guint8 nuh_layer_id, nuh_temporal_id_plus1;
1135 gboolean donl_present = FALSE;
1138 timestamp = GST_BUFFER_PTS (rtp->buffer);
1140 payload_len = gst_rtp_buffer_get_payload_len (rtp);
1141 payload = gst_rtp_buffer_get_payload (rtp);
1142 marker = gst_rtp_buffer_get_marker (rtp);
1144 GST_DEBUG_OBJECT (rtph265depay, "receiving %d bytes", payload_len);
1146 if (payload_len == 0)
1149 /* +---------------+---------------+
1150 * |0|1|2|3|4|5|6|7|0|1|2|3|4|5|6|7|
1151 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1152 * |F| Type | LayerId | TID |
1153 * +-------------+-----------------+
1158 nal_unit_type = (payload[0] >> 1) & 0x3f;
1159 nuh_layer_id = ((payload[0] & 0x01) << 5) | (payload[1] >> 3); /* should be zero for now but this could change in future HEVC extensions */
1160 nuh_temporal_id_plus1 = payload[1] & 0x03;
1162 /* At least two byte header with type */
1165 GST_DEBUG_OBJECT (rtph265depay,
1166 "NAL header nal_unit_type %d, nuh_temporal_id_plus1 %d", nal_unit_type,
1167 nuh_temporal_id_plus1);
1169 GST_FIXME_OBJECT (rtph265depay, "Assuming DONL field is not present");
1171 /* FIXME - assuming DONL field is not present for now */
1172 /*donl_present = (tx-mode == "MST") || (sprop-max-don-diff > 0); */
1174 /* If FU unit was being processed, but the current nal is of a different
1175 * type. Assume that the remote payloader is buggy (didn't set the end bit
1176 * when the FU ended) and send out what we gathered thusfar */
1177 if (G_UNLIKELY (rtph265depay->current_fu_type != 0 &&
1178 nal_unit_type != rtph265depay->current_fu_type))
1179 gst_rtp_h265_push_fragmentation_unit (rtph265depay, TRUE);
1181 switch (nal_unit_type) {
1184 GST_DEBUG_OBJECT (rtph265depay, "Processing aggregation packet");
1186 /* Aggregation packet (section 4.7) */
1188 /* An example of an AP packet containing two aggregation units
1189 without the DONL and DOND fields
1192 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
1193 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1195 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1196 | PayloadHdr (Type=48) | NALU 1 Size |
1197 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1199 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ NALU 1 Data |
1202 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1203 | . . . | NALU 2 Size | NALU 2 HDR |
1204 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1206 +-+-+-+-+-+-+-+-+ NALU 2 Data |
1208 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1209 | :...OPTIONAL RTP padding |
1210 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1214 payload += header_len;
1215 payload_len -= header_len;
1217 rtph265depay->wait_start = FALSE;
1221 goto not_implemented_donl_present;
1224 while (payload_len > 2) {
1226 nalu_size = (payload[0] << 8) | payload[1];
1228 /* dont include nalu_size */
1229 if (nalu_size > (payload_len - 2))
1230 nalu_size = payload_len - 2;
1232 outsize = nalu_size + sizeof (sync_bytes);
1233 outbuf = gst_buffer_new_and_alloc (outsize);
1235 gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
1236 if (rtph265depay->byte_stream) {
1237 memcpy (map.data, sync_bytes, sizeof (sync_bytes));
1239 goto not_implemented;
1242 /* strip NALU size */
1246 memcpy (map.data + sizeof (sync_bytes), payload, nalu_size);
1247 gst_buffer_unmap (outbuf, &map);
1249 gst_adapter_push (rtph265depay->adapter, outbuf);
1251 payload += nalu_size;
1252 payload_len -= nalu_size;
1255 outsize = gst_adapter_available (rtph265depay->adapter);
1257 outbuf = gst_adapter_take_buffer (rtph265depay->adapter, outsize);
1259 gst_rtp_h265_depay_handle_nal (rtph265depay, outbuf, timestamp,
1266 GST_DEBUG_OBJECT (rtph265depay, "Processing Fragmentation Unit");
1268 /* Fragmentation units (FUs) Section 4.8 */
1270 /* The structure of a Fragmentation Unit (FU)
1272 * 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
1273 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1274 | PayloadHdr (Type=49) | FU header | DONL (cond) |
1275 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-|
1280 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1281 | :...OPTIONAL RTP padding |
1282 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1288 payload += header_len;
1289 payload_len -= header_len;
1291 /* processing FU header */
1292 S = (payload[0] & 0x80) == 0x80;
1293 E = (payload[0] & 0x40) == 0x40;
1295 GST_DEBUG_OBJECT (rtph265depay,
1296 "FU header with S %d, E %d, nal_unit_type %d", S, E,
1299 if (rtph265depay->wait_start && !S)
1304 goto not_implemented_donl_present;
1309 GST_DEBUG_OBJECT (rtph265depay, "Start of Fragmentation Unit");
1311 /* If a new FU unit started, while still processing an older one.
1312 * Assume that the remote payloader is buggy (doesn't set the end
1313 * bit) and send out what we've gathered thusfar */
1314 if (G_UNLIKELY (rtph265depay->current_fu_type != 0))
1315 gst_rtp_h265_push_fragmentation_unit (rtph265depay, TRUE);
1317 rtph265depay->current_fu_type = nal_unit_type;
1318 rtph265depay->fu_timestamp = timestamp;
1320 rtph265depay->wait_start = FALSE;
1322 /* reconstruct NAL header */
1324 ((payload[0] & 0x3f) << 9) | (nuh_layer_id << 3) |
1325 nuh_temporal_id_plus1;
1327 /* go back one byte so we can copy the payload + two bytes more in the front which
1328 * will be overwritten by the nal_header
1333 nalu_size = payload_len;
1334 outsize = nalu_size + sizeof (sync_bytes);
1335 outbuf = gst_buffer_new_and_alloc (outsize);
1337 gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
1338 memcpy (map.data + sizeof (sync_bytes), payload, nalu_size);
1339 map.data[sizeof (sync_bytes)] = nal_header >> 8;
1340 map.data[sizeof (sync_bytes) + 1] = nal_header & 0xff;
1341 gst_buffer_unmap (outbuf, &map);
1343 GST_DEBUG_OBJECT (rtph265depay, "queueing %d bytes", outsize);
1345 /* and assemble in the adapter */
1346 gst_adapter_push (rtph265depay->adapter, outbuf);
1349 GST_DEBUG_OBJECT (rtph265depay,
1350 "Following part of Fragmentation Unit");
1352 /* strip off FU header byte */
1356 outsize = payload_len;
1357 outbuf = gst_buffer_new_and_alloc (outsize);
1358 gst_buffer_fill (outbuf, 0, payload, outsize);
1360 GST_DEBUG_OBJECT (rtph265depay, "queueing %d bytes", outsize);
1362 /* and assemble in the adapter */
1363 gst_adapter_push (rtph265depay->adapter, outbuf);
1367 rtph265depay->fu_marker = marker;
1369 /* if NAL unit ends, flush the adapter */
1371 outbuf = gst_rtp_h265_push_fragmentation_unit (rtph265depay, FALSE);
1372 GST_DEBUG_OBJECT (rtph265depay, "End of Fragmentation Unit");
1377 goto not_implemented; /* PACI packets Section 4.9 */
1380 rtph265depay->wait_start = FALSE;
1382 /* All other cases: Single NAL unit packet Section 4.6 */
1383 /* the entire payload is the output buffer */
1387 goto not_implemented_donl_present;
1390 nalu_size = payload_len;
1391 outsize = nalu_size + sizeof (sync_bytes);
1392 outbuf = gst_buffer_new_and_alloc (outsize);
1394 gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
1395 if (rtph265depay->byte_stream) {
1396 memcpy (map.data, sync_bytes, sizeof (sync_bytes));
1398 goto not_implemented;
1400 memcpy (map.data + sizeof (sync_bytes), payload, nalu_size);
1401 gst_buffer_unmap (outbuf, &map);
1403 outbuf = gst_rtp_h265_depay_handle_nal (rtph265depay, outbuf, timestamp,
1415 GST_DEBUG_OBJECT (rtph265depay, "empty packet");
1420 GST_DEBUG_OBJECT (rtph265depay, "waiting for start");
1424 not_implemented_donl_present:
1426 GST_ELEMENT_ERROR (rtph265depay, STREAM, FORMAT,
1427 (NULL), ("DONL field present not supported yet"));
1433 GST_ELEMENT_ERROR (rtph265depay, STREAM, FORMAT,
1434 (NULL), ("NAL unit type %d not supported yet", nal_unit_type));
1440 gst_rtp_h265_depay_handle_event (GstRTPBaseDepayload * depay, GstEvent * event)
1442 GstRtpH265Depay *rtph265depay;
1444 rtph265depay = GST_RTP_H265_DEPAY (depay);
1446 switch (GST_EVENT_TYPE (event)) {
1447 case GST_EVENT_FLUSH_STOP:
1448 gst_rtp_h265_depay_reset (rtph265depay);
1455 GST_RTP_BASE_DEPAYLOAD_CLASS (parent_class)->handle_event (depay, event);
1458 static GstStateChangeReturn
1459 gst_rtp_h265_depay_change_state (GstElement * element,
1460 GstStateChange transition)
1462 GstRtpH265Depay *rtph265depay;
1463 GstStateChangeReturn ret;
1465 rtph265depay = GST_RTP_H265_DEPAY (element);
1467 switch (transition) {
1468 case GST_STATE_CHANGE_NULL_TO_READY:
1470 case GST_STATE_CHANGE_READY_TO_PAUSED:
1471 gst_rtp_h265_depay_reset (rtph265depay);
1477 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1479 switch (transition) {
1480 case GST_STATE_CHANGE_READY_TO_NULL:
1489 gst_rtp_h265_depay_plugin_init (GstPlugin * plugin)
1491 GST_DEBUG_CATEGORY_INIT (rtph265depay_debug, "rtph265depay", 0,
1492 "H265 Video RTP Depayloader");
1494 return gst_element_register (plugin, "rtph265depay",
1495 GST_RANK_SECONDARY, GST_TYPE_RTP_H265_DEPAY);