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_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_h265_depay_src_template =
46 GST_STATIC_PAD_TEMPLATE ("src",
50 /* FIXME - hvc1 and hev1 formats are not supported yet */
52 "stream-format = (string) hvc1, alignment = (string) au; "
54 "stream-format = (string) hev1, alignment = (string) au; " */
56 "stream-format = (string) byte-stream, alignment = (string) { nal, au }")
59 static GstStaticPadTemplate gst_rtp_h265_depay_sink_template =
60 GST_STATIC_PAD_TEMPLATE ("sink",
63 GST_STATIC_CAPS ("application/x-rtp, "
64 "media = (string) \"video\", "
65 "clock-rate = (int) 90000, " "encoding-name = (string) \"H265\"")
66 /** optional parameters **/
67 /* "profile-space = (int) [ 0, 3 ], " */
68 /* "profile-id = (int) [ 0, 31 ], " */
69 /* "tier-flag = (int) [ 0, 1 ], " */
70 /* "level-id = (int) [ 0, 255 ], " */
71 /* "interop-constraints = (string) ANY, " */
72 /* "profile-compatibility-indicator = (string) ANY, " */
73 /* "sprop-sub-layer-id = (int) [ 0, 6 ], " */
74 /* "recv-sub-layer-id = (int) [ 0, 6 ], " */
75 /* "max-recv-level-id = (int) [ 0, 255 ], " */
76 /* "tx-mode = (string) {MST , SST}, " */
77 /* "sprop-vps = (string) ANY, " */
78 /* "sprop-sps = (string) ANY, " */
79 /* "sprop-pps = (string) ANY, " */
80 /* "sprop-sei = (string) ANY, " */
81 /* "max-lsr = (int) ANY, " *//* MUST be in the range of MaxLumaSR to 16 * MaxLumaSR, inclusive */
82 /* "max-lps = (int) ANY, " *//* MUST be in the range of MaxLumaPS to 16 * MaxLumaPS, inclusive */
83 /* "max-cpb = (int) ANY, " *//* MUST be in the range of MaxCPB to 16 * MaxCPB, inclusive */
84 /* "max-dpb = (int) [1, 16], " */
85 /* "max-br = (int) ANY, " *//* MUST be in the range of MaxBR to 16 * MaxBR, inclusive, for the highest level */
86 /* "max-tr = (int) ANY, " *//* MUST be in the range of MaxTileRows to 16 * MaxTileRows, inclusive, for the highest level */
87 /* "max-tc = (int) ANY, " *//* MUST be in the range of MaxTileCols to 16 * MaxTileCols, inclusive, for the highest level */
88 /* "max-fps = (int) ANY, " */
89 /* "sprop-max-don-diff = (int) [0, 32767], " */
90 /* "sprop-depack-buf-nalus = (int) [0, 32767], " */
91 /* "sprop-depack-buf-nalus = (int) [0, 4294967295], " */
92 /* "depack-buf-cap = (int) [1, 4294967295], " */
93 /* "sprop-segmentation-id = (int) [0, 3], " */
94 /* "sprop-spatial-segmentation-idc = (string) ANY, " */
95 /* "dec-parallel-cap = (string) ANY, " */
98 #define gst_rtp_h265_depay_parent_class parent_class
99 G_DEFINE_TYPE (GstRtpH265Depay, gst_rtp_h265_depay,
100 GST_TYPE_RTP_BASE_DEPAYLOAD);
102 static void gst_rtp_h265_depay_finalize (GObject * object);
104 static GstStateChangeReturn gst_rtp_h265_depay_change_state (GstElement *
105 element, GstStateChange transition);
107 static GstBuffer *gst_rtp_h265_depay_process (GstRTPBaseDepayload * depayload,
109 static gboolean gst_rtp_h265_depay_setcaps (GstRTPBaseDepayload * filter,
111 static gboolean gst_rtp_h265_depay_handle_event (GstRTPBaseDepayload * depay,
115 gst_rtp_h265_depay_class_init (GstRtpH265DepayClass * klass)
117 GObjectClass *gobject_class;
118 GstElementClass *gstelement_class;
119 GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
121 gobject_class = (GObjectClass *) klass;
122 gstelement_class = (GstElementClass *) klass;
123 gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
125 gobject_class->finalize = gst_rtp_h265_depay_finalize;
127 gst_element_class_add_static_pad_template (gstelement_class,
128 &gst_rtp_h265_depay_src_template);
129 gst_element_class_add_static_pad_template (gstelement_class,
130 &gst_rtp_h265_depay_sink_template);
132 gst_element_class_set_static_metadata (gstelement_class,
133 "RTP H265 depayloader", "Codec/Depayloader/Network/RTP",
134 "Extracts H265 video from RTP packets (draft-ietf-payload-rtp-h265-03.txt)",
135 "Jurgen Slowack <jurgenslowack@gmail.com>");
136 gstelement_class->change_state = gst_rtp_h265_depay_change_state;
138 gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_h265_depay_process;
139 gstrtpbasedepayload_class->set_caps = gst_rtp_h265_depay_setcaps;
140 gstrtpbasedepayload_class->handle_event = gst_rtp_h265_depay_handle_event;
144 gst_rtp_h265_depay_init (GstRtpH265Depay * rtph265depay)
146 rtph265depay->adapter = gst_adapter_new ();
147 rtph265depay->picture_adapter = gst_adapter_new ();
148 rtph265depay->byte_stream = DEFAULT_BYTE_STREAM;
149 rtph265depay->stream_format = NULL;
150 rtph265depay->merge = DEFAULT_ACCESS_UNIT;
151 rtph265depay->vps = g_ptr_array_new_with_free_func (
152 (GDestroyNotify) gst_buffer_unref);
153 rtph265depay->sps = g_ptr_array_new_with_free_func (
154 (GDestroyNotify) gst_buffer_unref);
155 rtph265depay->pps = g_ptr_array_new_with_free_func (
156 (GDestroyNotify) gst_buffer_unref);
160 gst_rtp_h265_depay_reset (GstRtpH265Depay * rtph265depay)
162 gst_adapter_clear (rtph265depay->adapter);
163 rtph265depay->wait_start = TRUE;
164 gst_adapter_clear (rtph265depay->picture_adapter);
165 rtph265depay->picture_start = FALSE;
166 rtph265depay->last_keyframe = FALSE;
167 rtph265depay->last_ts = 0;
168 rtph265depay->current_fu_type = 0;
169 rtph265depay->new_codec_data = FALSE;
170 g_ptr_array_set_size (rtph265depay->vps, 0);
171 g_ptr_array_set_size (rtph265depay->sps, 0);
172 g_ptr_array_set_size (rtph265depay->pps, 0);
176 gst_rtp_h265_depay_finalize (GObject * object)
178 GstRtpH265Depay *rtph265depay;
180 rtph265depay = GST_RTP_H265_DEPAY (object);
182 if (rtph265depay->codec_data)
183 gst_buffer_unref (rtph265depay->codec_data);
185 g_free (rtph265depay->stream_format);
187 g_object_unref (rtph265depay->adapter);
188 g_object_unref (rtph265depay->picture_adapter);
190 g_ptr_array_free (rtph265depay->vps, TRUE);
191 g_ptr_array_free (rtph265depay->sps, TRUE);
192 g_ptr_array_free (rtph265depay->pps, TRUE);
194 G_OBJECT_CLASS (parent_class)->finalize (object);
198 gst_rtp_h265_depay_negotiate (GstRtpH265Depay * rtph265depay)
201 gint byte_stream = -1;
205 gst_pad_get_allowed_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (rtph265depay));
207 GST_DEBUG_OBJECT (rtph265depay, "allowed caps: %" GST_PTR_FORMAT, caps);
210 if (gst_caps_get_size (caps) > 0) {
211 GstStructure *s = gst_caps_get_structure (caps, 0);
212 const gchar *str = NULL;
214 if ((str = gst_structure_get_string (s, "stream-format"))) {
215 g_free (rtph265depay->stream_format);
216 rtph265depay->stream_format = g_strdup (str);
218 if (strcmp (str, "hev1") == 0) {
220 } else if (strcmp (str, "hvc1") == 0) {
222 } else if (strcmp (str, "byte-stream") == 0) {
225 GST_DEBUG_OBJECT (rtph265depay, "unknown stream-format: %s", str);
229 if ((str = gst_structure_get_string (s, "alignment"))) {
230 if (strcmp (str, "au") == 0) {
232 } else if (strcmp (str, "nal") == 0) {
235 GST_DEBUG_OBJECT (rtph265depay, "unknown alignment: %s", str);
239 gst_caps_unref (caps);
242 if (byte_stream != -1) {
243 GST_DEBUG_OBJECT (rtph265depay, "downstream requires byte-stream %d",
245 rtph265depay->byte_stream = byte_stream;
247 GST_DEBUG_OBJECT (rtph265depay, "defaulting to byte-stream %d",
248 DEFAULT_BYTE_STREAM);
249 g_free (rtph265depay->stream_format);
250 rtph265depay->stream_format = g_strdup ("byte-stream");
251 rtph265depay->byte_stream = DEFAULT_BYTE_STREAM;
254 GST_DEBUG_OBJECT (rtph265depay, "downstream requires merge %d", merge);
255 rtph265depay->merge = merge;
257 GST_DEBUG_OBJECT (rtph265depay, "defaulting to merge %d",
258 DEFAULT_ACCESS_UNIT);
259 rtph265depay->merge = DEFAULT_ACCESS_UNIT;
264 parse_sps (GstMapInfo * map, guint32 * sps_id)
265 { /* To parse seq_parameter_set_id */
266 GstBitReader br = GST_BIT_READER_INIT (map->data + 15,
272 if (!gst_rtp_read_golomb (&br, sps_id))
279 parse_pps (GstMapInfo * map, guint32 * sps_id, guint32 * pps_id)
280 { /* To parse picture_parameter_set_id */
281 GstBitReader br = GST_BIT_READER_INIT (map->data + 2,
287 if (!gst_rtp_read_golomb (&br, pps_id))
289 if (!gst_rtp_read_golomb (&br, sps_id))
297 gst_rtp_h265_set_src_caps (GstRtpH265Depay * rtph265depay)
303 if (!rtph265depay->byte_stream &&
304 (!rtph265depay->new_codec_data ||
305 rtph265depay->vps->len == 0 || rtph265depay->sps->len == 0
306 || rtph265depay->pps->len == 0))
309 srccaps = gst_caps_new_simple ("video/x-h265",
310 "stream-format", G_TYPE_STRING, rtph265depay->stream_format,
311 "alignment", G_TYPE_STRING, rtph265depay->merge ? "au" : "nal", NULL);
313 if (!rtph265depay->byte_stream) {
314 GstBuffer *codec_data;
317 guint num_vps = rtph265depay->vps->len;
318 guint num_sps = rtph265depay->sps->len;
319 guint num_pps = rtph265depay->pps->len;
320 GstMapInfo map, nalmap;
323 guint8 num_arrays = 0;
328 guint32 max_sub_layers_minus1, temporal_id_nesting_flag, chroma_format_idc,
329 bit_depth_luma_minus8, bit_depth_chroma_minus8,
330 min_spatial_segmentation_idc;
332 /* Fixme: Current implementation is not embedding SEI in codec_data */
337 /* start with 23 bytes header */
340 num_arrays = (num_vps > 0) + (num_sps > 0) + (num_pps > 0);
343 /* add size of vps, sps & pps */
344 for (i = 0; i < num_vps; i++)
345 len += 2 + gst_buffer_get_size (g_ptr_array_index (rtph265depay->vps, i));
346 for (i = 0; i < num_sps; i++)
347 len += 2 + gst_buffer_get_size (g_ptr_array_index (rtph265depay->sps, i));
348 for (i = 0; i < num_pps; i++)
349 len += 2 + gst_buffer_get_size (g_ptr_array_index (rtph265depay->pps, i));
351 GST_DEBUG_OBJECT (rtph265depay,
352 "constructing codec_data: num_vps =%d num_sps=%d, num_pps=%d", num_vps,
355 codec_data = gst_buffer_new_and_alloc (len);
356 g_debug ("alloc_len: %u", len);
357 gst_buffer_map (codec_data, &map, GST_MAP_READWRITE);
360 memset (data, 0, map.size);
362 /* Parsing sps to get the info required further on */
364 gst_buffer_map (g_ptr_array_index (rtph265depay->sps, 0), &nalmap,
367 max_sub_layers_minus1 = ((nalmap.data[2]) >> 1) & 0x07;
368 temporal_id_nesting_flag = nalmap.data[2] & 0x01;
370 gst_bit_reader_init (&br, nalmap.data + 15, nalmap.size - 15);
372 gst_rtp_read_golomb (&br, &tmp); /* sps_seq_parameter_set_id */
373 gst_rtp_read_golomb (&br, &chroma_format_idc); /* chroma_format_idc */
375 if (chroma_format_idc == 3)
377 gst_bit_reader_get_bits_uint8 (&br, &tmp8, 1); /* separate_colour_plane_flag */
379 gst_rtp_read_golomb (&br, &tmp); /* pic_width_in_luma_samples */
380 gst_rtp_read_golomb (&br, &tmp); /* pic_height_in_luma_samples */
382 gst_bit_reader_get_bits_uint8 (&br, &tmp8, 1); /* conformance_window_flag */
384 gst_rtp_read_golomb (&br, &tmp); /* conf_win_left_offset */
385 gst_rtp_read_golomb (&br, &tmp); /* conf_win_right_offset */
386 gst_rtp_read_golomb (&br, &tmp); /* conf_win_top_offset */
387 gst_rtp_read_golomb (&br, &tmp); /* conf_win_bottom_offset */
390 gst_rtp_read_golomb (&br, &bit_depth_luma_minus8); /* bit_depth_luma_minus8 */
391 gst_rtp_read_golomb (&br, &bit_depth_chroma_minus8); /* bit_depth_chroma_minus8 */
393 GST_DEBUG_OBJECT (rtph265depay,
394 "Ignoring min_spatial_segmentation for now (assuming zero)");
396 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 */
400 gst_buffer_unmap (g_ptr_array_index (rtph265depay->sps, 0), &nalmap);
402 /* HEVCDecoderConfigurationVersion = 1 */
405 /* Copy from profile_tier_level (Rec. ITU-T H.265 (04/2013) section 7.3.3
407 * profile_space | tier_flat | profile_idc |
408 * profile_compatibility_flags | constraint_indicator_flags |
409 * level_idc | progressive_source_flag | interlaced_source_flag
410 * non_packed_constraint_flag | frame_only_constraint_flag
411 * reserved_zero_44bits | level_idc */
412 gst_buffer_map (g_ptr_array_index (rtph265depay->sps, 0), &nalmap,
414 for (i = 0; i < 12; i++)
415 data[i + 1] = nalmap.data[i];
416 gst_buffer_unmap (g_ptr_array_index (rtph265depay->sps, 0), &nalmap);
418 /* min_spatial_segmentation_idc */
419 GST_WRITE_UINT16_BE (data + 13, min_spatial_segmentation_idc);
421 data[15] = 0xfc; /* keeping parrallelismType as zero (unknown) */
422 data[16] = 0xfc | chroma_format_idc;
423 data[17] = 0xf8 | bit_depth_luma_minus8;
424 data[18] = 0xf8 | bit_depth_chroma_minus8;
425 data[19] = 0x00; /* keep avgFrameRate as unspecified */
426 data[20] = 0x00; /* keep avgFrameRate as unspecified */
427 /* constFrameRate(2 bits): 0, stream may or may not be of constant framerate
428 * numTemporalLayers (3 bits): number of temporal layers, value from SPS
429 * TemporalIdNested (1 bit): sps_temporal_id_nesting_flag from SPS
430 * lengthSizeMinusOne (2 bits): plus 1 indicates the length of the NALUnitLength */
432 0x00 | ((max_sub_layers_minus1 +
433 1) << 3) | (temporal_id_nesting_flag << 2) | (nl - 1);
434 GST_WRITE_UINT8 (data + 22, num_arrays); /* numOfArrays */
440 /* array_completeness | reserved_zero bit | nal_unit_type */
441 data[0] = 0x00 | 0x20;
444 GST_WRITE_UINT16_BE (data, num_vps);
447 for (i = 0; i < num_vps; i++) {
449 gst_buffer_get_size (g_ptr_array_index (rtph265depay->vps, i));
450 GST_WRITE_UINT16_BE (data, nal_size);
451 gst_buffer_extract (g_ptr_array_index (rtph265depay->vps, i), 0,
453 data += 2 + nal_size;
454 GST_DEBUG_OBJECT (rtph265depay, "Copied VPS %d of length %u", i,
461 /* array_completeness | reserved_zero bit | nal_unit_type */
462 data[0] = 0x00 | 0x21;
465 GST_WRITE_UINT16_BE (data, num_sps);
468 for (i = 0; i < num_sps; i++) {
470 gst_buffer_get_size (g_ptr_array_index (rtph265depay->sps, i));
471 GST_WRITE_UINT16_BE (data, nal_size);
472 gst_buffer_extract (g_ptr_array_index (rtph265depay->sps, i), 0,
474 data += 2 + nal_size;
475 GST_DEBUG_OBJECT (rtph265depay, "Copied SPS %d of length %u", i,
482 /* array_completeness | reserved_zero bit | nal_unit_type */
483 data[0] = 0x00 | 0x22;
486 GST_WRITE_UINT16_BE (data, num_pps);
489 for (i = 0; i < num_pps; i++) {
491 gst_buffer_get_size (g_ptr_array_index (rtph265depay->pps, i));
492 GST_WRITE_UINT16_BE (data, nal_size);
493 gst_buffer_extract (g_ptr_array_index (rtph265depay->pps, i), 0,
495 data += 2 + nal_size;
496 GST_DEBUG_OBJECT (rtph265depay, "Copied PPS %d of length %u", i,
501 new_size = data - map.data;
502 gst_buffer_unmap (codec_data, &map);
503 gst_buffer_set_size (codec_data, new_size);
505 gst_caps_set_simple (srccaps,
506 "codec_data", GST_TYPE_BUFFER, codec_data, NULL);
507 gst_buffer_unref (codec_data);
511 gst_pad_get_current_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (rtph265depay));
512 if (old_caps != NULL) {
514 /* Only update the caps if they are not equal. For
515 * AVC we don't update caps if only the codec_data
516 * changes. This is the same behaviour as in h264parse
517 * and gstrtph264depay
519 if (rtph265depay->byte_stream) {
520 if (!gst_caps_is_equal (srccaps, old_caps))
522 gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (rtph265depay),
527 GstCaps *tmp_caps = gst_caps_copy (srccaps);
528 GstStructure *old_s, *tmp_s;
530 old_s = gst_caps_get_structure (old_caps, 0);
531 tmp_s = gst_caps_get_structure (tmp_caps, 0);
532 if (gst_structure_has_field (old_s, "codec_data"))
533 gst_structure_set_value (tmp_s, "codec_data",
534 gst_structure_get_value (old_s, "codec_data"));
536 if (!gst_caps_is_equal (old_caps, tmp_caps))
538 gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (rtph265depay),
543 gst_caps_unref (tmp_caps);
547 gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (rtph265depay),
551 gst_caps_unref (srccaps);
553 /* Insert SPS and PPS into the stream on next opportunity */
554 if (rtph265depay->sps->len > 0 || rtph265depay->pps->len > 0) {
556 GstBuffer *codec_data;
561 for (i = 0; i < rtph265depay->sps->len; i++) {
562 len += 4 + gst_buffer_get_size (g_ptr_array_index (rtph265depay->sps, i));
565 for (i = 0; i < rtph265depay->pps->len; i++) {
566 len += 4 + gst_buffer_get_size (g_ptr_array_index (rtph265depay->pps, i));
569 codec_data = gst_buffer_new_and_alloc (len);
570 gst_buffer_map (codec_data, &map, GST_MAP_WRITE);
573 for (i = 0; i < rtph265depay->sps->len; i++) {
574 GstBuffer *sps_buf = g_ptr_array_index (rtph265depay->sps, i);
575 guint sps_size = gst_buffer_get_size (sps_buf);
577 if (rtph265depay->byte_stream)
578 memcpy (data, sync_bytes, sizeof (sync_bytes));
580 GST_WRITE_UINT32_BE (data, sps_size);
581 gst_buffer_extract (sps_buf, 0, data + 4, -1);
582 data += 4 + sps_size;
585 for (i = 0; i < rtph265depay->pps->len; i++) {
586 GstBuffer *pps_buf = g_ptr_array_index (rtph265depay->pps, i);
587 guint pps_size = gst_buffer_get_size (pps_buf);
589 if (rtph265depay->byte_stream)
590 memcpy (data, sync_bytes, sizeof (sync_bytes));
592 GST_WRITE_UINT32_BE (data, pps_size);
593 gst_buffer_extract (pps_buf, 0, data + 4, -1);
594 data += 4 + pps_size;
597 gst_buffer_unmap (codec_data, &map);
598 if (rtph265depay->codec_data)
599 gst_buffer_unref (rtph265depay->codec_data);
600 rtph265depay->codec_data = codec_data;
604 rtph265depay->new_codec_data = FALSE;
610 gst_rtp_h265_add_vps_sps_pps (GstElement * rtph265, GPtrArray * vps_array,
611 GPtrArray * sps_array, GPtrArray * pps_array, GstBuffer * nal)
617 gst_buffer_map (nal, &map, GST_MAP_READ);
619 type = (map.data[0] >> 1) & 0x3f;
621 if (type == GST_H265_VPS_NUT) {
622 guint32 vps_id = (map.data[2] >> 4) & 0x0f;
624 for (i = 0; i < vps_array->len; i++) {
625 GstBuffer *vps = g_ptr_array_index (vps_array, i);
629 gst_buffer_map (vps, &vpsmap, GST_MAP_READ);
630 tmp_vps_id = (vpsmap.data[2] >> 4) & 0x0f;
632 if (vps_id == tmp_vps_id) {
633 if (map.size == vpsmap.size &&
634 memcmp (map.data, vpsmap.data, vpsmap.size) == 0) {
635 GST_LOG_OBJECT (rtph265, "Unchanged VPS %u, not updating", vps_id);
636 gst_buffer_unmap (vps, &vpsmap);
639 gst_buffer_unmap (vps, &vpsmap);
640 g_ptr_array_remove_index_fast (vps_array, i);
641 g_ptr_array_add (vps_array, nal);
642 GST_LOG_OBJECT (rtph265, "Modified VPS %u, replacing", vps_id);
646 gst_buffer_unmap (vps, &vpsmap);
648 GST_LOG_OBJECT (rtph265, "Adding new VPS %u", vps_id);
649 g_ptr_array_add (vps_array, nal);
650 } else if (type == GST_H265_SPS_NUT) {
653 if (!parse_sps (&map, &sps_id)) {
654 GST_WARNING_OBJECT (rtph265, "Invalid SPS,"
655 " can't parse seq_parameter_set_id");
659 for (i = 0; i < sps_array->len; i++) {
660 GstBuffer *sps = g_ptr_array_index (sps_array, i);
664 gst_buffer_map (sps, &spsmap, GST_MAP_READ);
665 parse_sps (&spsmap, &tmp_sps_id);
667 if (sps_id == tmp_sps_id) {
668 if (map.size == spsmap.size &&
669 memcmp (map.data, spsmap.data, spsmap.size) == 0) {
670 GST_LOG_OBJECT (rtph265, "Unchanged SPS %u, not updating", sps_id);
671 gst_buffer_unmap (sps, &spsmap);
674 gst_buffer_unmap (sps, &spsmap);
675 g_ptr_array_remove_index_fast (sps_array, i);
676 g_ptr_array_add (sps_array, nal);
677 GST_LOG_OBJECT (rtph265, "Modified SPS %u, replacing", sps_id);
681 gst_buffer_unmap (sps, &spsmap);
683 GST_LOG_OBJECT (rtph265, "Adding new SPS %u", sps_id);
684 g_ptr_array_add (sps_array, nal);
685 } else if (type == GST_H265_PPS_NUT) {
689 if (!parse_pps (&map, &sps_id, &pps_id)) {
690 GST_WARNING_OBJECT (rtph265, "Invalid PPS,"
691 " can't parse seq_parameter_set_id or pic_parameter_set_id");
695 for (i = 0; i < pps_array->len; i++) {
696 GstBuffer *pps = g_ptr_array_index (pps_array, i);
702 gst_buffer_map (pps, &ppsmap, GST_MAP_READ);
703 parse_pps (&ppsmap, &tmp_sps_id, &tmp_pps_id);
705 if (pps_id == tmp_pps_id) {
706 if (map.size == ppsmap.size &&
707 memcmp (map.data, ppsmap.data, ppsmap.size) == 0) {
708 GST_LOG_OBJECT (rtph265, "Unchanged PPS %u:%u, not updating", sps_id,
710 gst_buffer_unmap (pps, &ppsmap);
713 gst_buffer_unmap (pps, &ppsmap);
714 g_ptr_array_remove_index_fast (pps_array, i);
715 g_ptr_array_add (pps_array, nal);
716 GST_LOG_OBJECT (rtph265, "Modified PPS %u:%u, replacing",
721 gst_buffer_unmap (pps, &ppsmap);
723 GST_LOG_OBJECT (rtph265, "Adding new PPS %u:%i", sps_id, pps_id);
724 g_ptr_array_add (pps_array, nal);
730 gst_buffer_unmap (nal, &map);
735 gst_buffer_unmap (nal, &map);
736 gst_buffer_unref (nal);
743 gst_rtp_h265_depay_add_vps_sps_pps (GstRtpH265Depay * rtph265depay,
746 if (gst_rtp_h265_add_vps_sps_pps (GST_ELEMENT (rtph265depay),
747 rtph265depay->vps, rtph265depay->sps, rtph265depay->pps, nal))
748 rtph265depay->new_codec_data = TRUE;
752 gst_rtp_h265_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
755 GstStructure *structure = gst_caps_get_structure (caps, 0);
756 GstRtpH265Depay *rtph265depay;
758 GstBuffer *codec_data;
762 rtph265depay = GST_RTP_H265_DEPAY (depayload);
764 if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
766 depayload->clock_rate = clock_rate;
768 /* Base64 encoded, comma separated config NALs */
769 ps = gst_structure_get_string (structure, "sprop-parameter-sets");
771 /* negotiate with downstream w.r.t. output format and alignment */
772 gst_rtp_h265_depay_negotiate (rtph265depay);
774 if (rtph265depay->byte_stream && ps != NULL) {
775 /* for bytestream we only need the parameter sets but we don't error out
776 * when they are not there, we assume they are in the stream. */
781 params = g_strsplit (ps, ",", 0);
783 /* count total number of bytes in base64. Also include the sync bytes in
784 * front of the params. */
786 for (i = 0; params[i]; i++) {
787 len += strlen (params[i]);
788 len += sizeof (sync_bytes);
790 /* we seriously overshoot the length, but it's fine. */
791 codec_data = gst_buffer_new_and_alloc (len);
793 gst_buffer_map (codec_data, &map, GST_MAP_WRITE);
796 for (i = 0; params[i]; i++) {
800 GST_DEBUG_OBJECT (depayload, "decoding param %d (%s)", i, params[i]);
801 memcpy (ptr, sync_bytes, sizeof (sync_bytes));
802 ptr += sizeof (sync_bytes);
804 g_base64_decode_step (params[i], strlen (params[i]), ptr, &state,
806 GST_DEBUG_OBJECT (depayload, "decoded %d bytes", len);
807 total += len + sizeof (sync_bytes);
810 gst_buffer_unmap (codec_data, &map);
811 gst_buffer_resize (codec_data, 0, total);
814 /* keep the codec_data, we need to send it as the first buffer. We cannot
815 * push it in the adapter because the adapter might be flushed on discont.
817 if (rtph265depay->codec_data)
818 gst_buffer_unref (rtph265depay->codec_data);
819 rtph265depay->codec_data = codec_data;
820 } else if (!rtph265depay->byte_stream) {
825 goto incomplete_caps;
827 params = g_strsplit (ps, ",", 0);
829 GST_DEBUG_OBJECT (depayload, "we have %d params", g_strv_length (params));
831 /* start with 23 bytes header */
832 for (i = 0; params[i]; i++) {
839 nal_len = strlen (params[i]);
840 nal = gst_buffer_new_and_alloc (nal_len);
841 gst_buffer_map (nal, &nalmap, GST_MAP_READWRITE);
844 g_base64_decode_step (params[i], nal_len, nalmap.data, &state, &save);
846 GST_DEBUG_OBJECT (depayload, "adding param %d as %s", i,
847 (((nalmap.data[0] >> 1) & 0x3f) ==
848 32) ? "VPS" : (((nalmap.data[0] >> 1) & 0x3f) ==
849 33) ? "SPS" : "PPS");
851 gst_buffer_unmap (nal, &nalmap);
852 gst_buffer_set_size (nal, nal_len);
854 gst_rtp_h265_depay_add_vps_sps_pps (rtph265depay, nal);
858 if (rtph265depay->sps->len == 0 || rtph265depay->pps->len == 0)
859 goto incomplete_caps;
862 return gst_rtp_h265_set_src_caps (rtph265depay);
867 GST_DEBUG_OBJECT (depayload, "we have incomplete caps,"
868 " doing setcaps later");
874 gst_rtp_h265_complete_au (GstRtpH265Depay * rtph265depay,
875 GstClockTime * out_timestamp, gboolean * out_keyframe)
880 /* we had a picture in the adapter and we completed it */
881 GST_DEBUG_OBJECT (rtph265depay, "taking completed AU");
882 outsize = gst_adapter_available (rtph265depay->picture_adapter);
883 outbuf = gst_adapter_take_buffer (rtph265depay->picture_adapter, outsize);
885 *out_timestamp = rtph265depay->last_ts;
886 *out_keyframe = rtph265depay->last_keyframe;
888 rtph265depay->last_keyframe = FALSE;
889 rtph265depay->picture_start = FALSE;
894 /* VPS/SPS/PPS/RADL/TSA/RASL/IDR/CRA is considered key, all others DELTA;
895 * so downstream waiting for keyframe can pick up at VPS/SPS/PPS/IDR */
897 #define NAL_TYPE_IS_PARAMETER_SET(nt) ( ((nt) == GST_H265_VPS_NUT)\
898 || ((nt) == GST_H265_SPS_NUT)\
899 || ((nt) == GST_H265_PPS_NUT) )
901 #define NAL_TYPE_IS_CODED_SLICE_SEGMENT(nt) ( ((nt) == GST_H265_NAL_SLICE_TRAIL_N)\
902 || ((nt) == GST_H265_NAL_SLICE_TRAIL_R)\
903 || ((nt) == GST_H265_NAL_SLICE_TSA_N)\
904 || ((nt) == GST_H265_NAL_SLICE_TSA_R)\
905 || ((nt) == GST_H265_NAL_SLICE_STSA_N)\
906 || ((nt) == GST_H265_NAL_SLICE_STSA_R)\
907 || ((nt) == GST_H265_NAL_SLICE_RASL_N)\
908 || ((nt) == GST_H265_NAL_SLICE_RASL_R)\
909 || ((nt) == GST_H265_NAL_SLICE_BLA_W_LP)\
910 || ((nt) == GST_H265_NAL_SLICE_BLA_W_RADL)\
911 || ((nt) == GST_H265_NAL_SLICE_BLA_N_LP)\
912 || ((nt) == GST_H265_NAL_SLICE_IDR_W_RADL)\
913 || ((nt) == GST_H265_NAL_SLICE_IDR_N_LP)\
914 || ((nt) == GST_H265_NAL_SLICE_CRA_NUT) )
916 #define NAL_TYPE_IS_KEY(nt) (NAL_TYPE_IS_PARAMETER_SET(nt) || NAL_TYPE_IS_CODED_SLICE_SEGMENT(nt))
919 gst_rtp_h265_depay_handle_nal (GstRtpH265Depay * rtph265depay, GstBuffer * nal,
920 GstClockTime in_timestamp, gboolean marker)
922 GstRTPBaseDepayload *depayload = GST_RTP_BASE_DEPAYLOAD (rtph265depay);
925 GstBuffer *outbuf = NULL;
926 GstClockTime out_timestamp;
927 gboolean keyframe, out_keyframe;
929 gst_buffer_map (nal, &map, GST_MAP_READ);
930 if (G_UNLIKELY (map.size < 5))
933 nal_type = (map.data[4] >> 1) & 0x3f;
934 GST_DEBUG_OBJECT (rtph265depay, "handle NAL type %d (RTP marker bit %d)",
937 keyframe = NAL_TYPE_IS_KEY (nal_type);
939 out_keyframe = keyframe;
940 out_timestamp = in_timestamp;
942 if (!rtph265depay->byte_stream) {
943 if (NAL_TYPE_IS_PARAMETER_SET (nal_type)) {
944 gst_rtp_h265_depay_add_vps_sps_pps (rtph265depay,
945 gst_buffer_copy_region (nal, GST_BUFFER_COPY_ALL,
946 4, gst_buffer_get_size (nal) - 4));
947 gst_buffer_unmap (nal, &map);
948 gst_buffer_unref (nal);
950 } else if (rtph265depay->sps->len == 0 || rtph265depay->pps->len == 0) {
951 /* Down push down any buffer in non-bytestream mode if the SPS/PPS haven't
954 gst_pad_push_event (GST_RTP_BASE_DEPAYLOAD_SINKPAD (depayload),
955 gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM,
956 gst_structure_new ("GstForceKeyUnit",
957 "all-headers", G_TYPE_BOOLEAN, TRUE, NULL)));
958 gst_buffer_unmap (nal, &map);
959 gst_buffer_unref (nal);
963 if (rtph265depay->new_codec_data &&
964 rtph265depay->sps->len > 0 && rtph265depay->pps->len > 0)
965 gst_rtp_h265_set_src_caps (rtph265depay);
968 if (rtph265depay->merge) {
969 gboolean start = FALSE, complete = FALSE;
971 /* marker bit isn't mandatory so in the following code we try to detect
972 * an AU boundary (see H.265 spec section 7.4.2.4.4) */
974 if (NAL_TYPE_IS_CODED_SLICE_SEGMENT (nal_type)) {
975 /* 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 */
977 if (((map.data[6] >> 7) & 0x01) == 1) {
980 } else if ((nal_type >= 32 && nal_type <= 35)
981 || nal_type == 39 || (nal_type >= 41 && nal_type <= 44)
982 || (nal_type >= 48 && nal_type <= 55)) {
983 /* VPS, SPS, PPS, SEI, ... terminate an access unit */
986 GST_DEBUG_OBJECT (depayload, "start %d, complete %d", start, complete);
988 if (complete && rtph265depay->picture_start)
989 outbuf = gst_rtp_h265_complete_au (rtph265depay, &out_timestamp,
993 gst_buffer_unmap (nal, &map);
995 GST_DEBUG_OBJECT (depayload, "adding NAL to picture adapter");
996 gst_adapter_push (rtph265depay->picture_adapter, nal);
997 rtph265depay->last_ts = in_timestamp;
998 rtph265depay->last_keyframe |= keyframe;
999 rtph265depay->picture_start |= start;
1002 outbuf = gst_rtp_h265_complete_au (rtph265depay, &out_timestamp,
1005 /* no merge, output is input nal */
1006 GST_DEBUG_OBJECT (depayload, "using NAL as output");
1008 gst_buffer_unmap (nal, &map);
1012 /* prepend codec_data */
1013 if (rtph265depay->codec_data) {
1014 GST_DEBUG_OBJECT (depayload, "prepending codec_data");
1015 gst_rtp_copy_meta (GST_ELEMENT_CAST (rtph265depay),
1016 rtph265depay->codec_data, outbuf,
1017 g_quark_from_static_string (GST_META_TAG_VIDEO_STR));
1018 outbuf = gst_buffer_append (rtph265depay->codec_data, outbuf);
1019 rtph265depay->codec_data = NULL;
1020 out_keyframe = TRUE;
1022 outbuf = gst_buffer_make_writable (outbuf);
1024 gst_rtp_drop_meta (GST_ELEMENT_CAST (rtph265depay), outbuf,
1025 g_quark_from_static_string (GST_META_TAG_VIDEO_STR));
1027 GST_BUFFER_PTS (outbuf) = out_timestamp;
1030 GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
1032 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
1040 GST_WARNING_OBJECT (depayload, "dropping short NAL");
1041 gst_buffer_unmap (nal, &map);
1042 gst_buffer_unref (nal);
1048 gst_rtp_h265_push_fragmentation_unit (GstRtpH265Depay * rtph265depay,
1055 outsize = gst_adapter_available (rtph265depay->adapter);
1056 outbuf = gst_adapter_take_buffer (rtph265depay->adapter, outsize);
1058 gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
1059 GST_DEBUG_OBJECT (rtph265depay, "output %d bytes", outsize);
1061 if (rtph265depay->byte_stream) {
1062 memcpy (map.data, sync_bytes, sizeof (sync_bytes));
1064 goto not_implemented;
1066 gst_buffer_unmap (outbuf, &map);
1068 rtph265depay->current_fu_type = 0;
1070 outbuf = gst_rtp_h265_depay_handle_nal (rtph265depay, outbuf,
1071 rtph265depay->fu_timestamp, rtph265depay->fu_marker);
1073 if (send && outbuf) {
1074 gst_rtp_base_depayload_push (GST_RTP_BASE_DEPAYLOAD (rtph265depay), outbuf);
1081 GST_ERROR_OBJECT (rtph265depay,
1082 ("Only bytestream format is currently supported."));
1083 gst_buffer_unmap (outbuf, &map);
1089 gst_rtp_h265_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
1091 GstRtpH265Depay *rtph265depay;
1092 GstBuffer *outbuf = NULL;
1093 guint8 nal_unit_type;
1095 rtph265depay = GST_RTP_H265_DEPAY (depayload);
1097 /* flush remaining data on discont */
1098 if (GST_BUFFER_IS_DISCONT (rtp->buffer)) {
1099 gst_adapter_clear (rtph265depay->adapter);
1100 rtph265depay->wait_start = TRUE;
1101 rtph265depay->current_fu_type = 0;
1109 guint outsize, nalu_size;
1110 GstClockTime timestamp;
1112 guint8 nuh_layer_id, nuh_temporal_id_plus1;
1116 gboolean donl_present = FALSE;
1119 timestamp = GST_BUFFER_PTS (rtp->buffer);
1121 payload_len = gst_rtp_buffer_get_payload_len (rtp);
1122 payload = gst_rtp_buffer_get_payload (rtp);
1123 marker = gst_rtp_buffer_get_marker (rtp);
1125 GST_DEBUG_OBJECT (rtph265depay, "receiving %d bytes", payload_len);
1127 if (payload_len == 0)
1130 /* +---------------+---------------+
1131 * |0|1|2|3|4|5|6|7|0|1|2|3|4|5|6|7|
1132 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1133 * |F| Type | LayerId | TID |
1134 * +-------------+-----------------+
1139 nal_unit_type = (payload[0] >> 1) & 0x3f;
1140 nuh_layer_id = ((payload[0] & 0x01) << 5) | (payload[1] >> 3); /* should be zero for now but this could change in future HEVC extensions */
1141 nuh_temporal_id_plus1 = payload[1] & 0x03;
1143 /* At least two byte header with type */
1146 GST_DEBUG_OBJECT (rtph265depay,
1147 "NAL header nal_unit_type %d, nuh_temporal_id_plus1 %d", nal_unit_type,
1148 nuh_temporal_id_plus1);
1150 GST_FIXME_OBJECT (rtph265depay, "Assuming DONL field is not present");
1152 /* FIXME - assuming DONL field is not present for now */
1153 /*donl_present = (tx-mode == "MST") || (sprop-max-don-diff > 0); */
1155 /* If FU unit was being processed, but the current nal is of a different
1156 * type. Assume that the remote payloader is buggy (didn't set the end bit
1157 * when the FU ended) and send out what we gathered thusfar */
1158 if (G_UNLIKELY (rtph265depay->current_fu_type != 0 &&
1159 nal_unit_type != rtph265depay->current_fu_type))
1160 gst_rtp_h265_push_fragmentation_unit (rtph265depay, TRUE);
1162 switch (nal_unit_type) {
1165 GST_DEBUG_OBJECT (rtph265depay, "Processing aggregation packet");
1167 /* Aggregation packet (section 4.7) */
1169 /* An example of an AP packet containing two aggregation units
1170 without the DONL and DOND fields
1173 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
1174 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1176 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1177 | PayloadHdr (Type=48) | NALU 1 Size |
1178 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1180 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ NALU 1 Data |
1183 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1184 | . . . | NALU 2 Size | NALU 2 HDR |
1185 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1187 +-+-+-+-+-+-+-+-+ NALU 2 Data |
1189 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1190 | :...OPTIONAL RTP padding |
1191 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1195 payload += header_len;
1196 payload_len -= header_len;
1198 rtph265depay->wait_start = FALSE;
1202 goto not_implemented_donl_present;
1205 while (payload_len > 2) {
1207 nalu_size = (payload[0] << 8) | payload[1];
1209 /* dont include nalu_size */
1210 if (nalu_size > (payload_len - 2))
1211 nalu_size = payload_len - 2;
1213 outsize = nalu_size + sizeof (sync_bytes);
1214 outbuf = gst_buffer_new_and_alloc (outsize);
1216 gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
1217 if (rtph265depay->byte_stream) {
1218 memcpy (map.data, sync_bytes, sizeof (sync_bytes));
1220 goto not_implemented;
1223 /* strip NALU size */
1227 memcpy (map.data + sizeof (sync_bytes), payload, nalu_size);
1228 gst_buffer_unmap (outbuf, &map);
1230 gst_rtp_copy_meta (GST_ELEMENT_CAST (rtph265depay), outbuf,
1231 rtp->buffer, g_quark_from_static_string (GST_META_TAG_VIDEO_STR));
1234 gst_rtp_h265_depay_handle_nal (rtph265depay, outbuf, timestamp,
1237 gst_adapter_push (rtph265depay->adapter, outbuf);
1239 payload += nalu_size;
1240 payload_len -= nalu_size;
1243 outsize = gst_adapter_available (rtph265depay->adapter);
1245 outbuf = gst_adapter_take_buffer (rtph265depay->adapter, outsize);
1247 gst_rtp_h265_depay_handle_nal (rtph265depay, outbuf, timestamp,
1254 GST_DEBUG_OBJECT (rtph265depay, "Processing Fragmentation Unit");
1256 /* Fragmentation units (FUs) Section 4.8 */
1258 /* The structure of a Fragmentation Unit (FU)
1260 * 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
1261 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1262 | PayloadHdr (Type=49) | FU header | DONL (cond) |
1263 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-|
1268 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1269 | :...OPTIONAL RTP padding |
1270 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1276 payload += header_len;
1277 payload_len -= header_len;
1279 /* processing FU header */
1280 S = (payload[0] & 0x80) == 0x80;
1281 E = (payload[0] & 0x40) == 0x40;
1283 GST_DEBUG_OBJECT (rtph265depay,
1284 "FU header with S %d, E %d, nal_unit_type %d", S, E,
1287 if (rtph265depay->wait_start && !S)
1292 goto not_implemented_donl_present;
1297 GST_DEBUG_OBJECT (rtph265depay, "Start of Fragmentation Unit");
1299 /* If a new FU unit started, while still processing an older one.
1300 * Assume that the remote payloader is buggy (doesn't set the end
1301 * bit) and send out what we've gathered thusfar */
1302 if (G_UNLIKELY (rtph265depay->current_fu_type != 0))
1303 gst_rtp_h265_push_fragmentation_unit (rtph265depay, TRUE);
1305 rtph265depay->current_fu_type = nal_unit_type;
1306 rtph265depay->fu_timestamp = timestamp;
1308 rtph265depay->wait_start = FALSE;
1310 /* reconstruct NAL header */
1312 ((payload[0] & 0x3f) << 9) | (nuh_layer_id << 3) |
1313 nuh_temporal_id_plus1;
1315 /* go back one byte so we can copy the payload + two bytes more in the front which
1316 * will be overwritten by the nal_header
1321 nalu_size = payload_len;
1322 outsize = nalu_size + sizeof (sync_bytes);
1323 outbuf = gst_buffer_new_and_alloc (outsize);
1325 gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
1326 memcpy (map.data + sizeof (sync_bytes), payload, nalu_size);
1327 map.data[sizeof (sync_bytes)] = nal_header >> 8;
1328 map.data[sizeof (sync_bytes) + 1] = nal_header & 0xff;
1329 gst_buffer_unmap (outbuf, &map);
1331 gst_rtp_copy_meta (GST_ELEMENT_CAST (rtph265depay), outbuf,
1332 rtp->buffer, g_quark_from_static_string (GST_META_TAG_VIDEO_STR));
1334 GST_DEBUG_OBJECT (rtph265depay, "queueing %d bytes", outsize);
1336 /* and assemble in the adapter */
1337 gst_adapter_push (rtph265depay->adapter, outbuf);
1340 GST_DEBUG_OBJECT (rtph265depay,
1341 "Following part of Fragmentation Unit");
1343 /* strip off FU header byte */
1347 outsize = payload_len;
1348 outbuf = gst_buffer_new_and_alloc (outsize);
1349 gst_buffer_fill (outbuf, 0, payload, outsize);
1351 gst_rtp_copy_meta (GST_ELEMENT_CAST (rtph265depay), outbuf,
1352 rtp->buffer, g_quark_from_static_string (GST_META_TAG_VIDEO_STR));
1354 GST_DEBUG_OBJECT (rtph265depay, "queueing %d bytes", outsize);
1356 /* and assemble in the adapter */
1357 gst_adapter_push (rtph265depay->adapter, outbuf);
1361 rtph265depay->fu_marker = marker;
1363 /* if NAL unit ends, flush the adapter */
1365 outbuf = gst_rtp_h265_push_fragmentation_unit (rtph265depay, FALSE);
1366 GST_DEBUG_OBJECT (rtph265depay, "End of Fragmentation Unit");
1371 goto not_implemented; /* PACI packets Section 4.9 */
1374 rtph265depay->wait_start = FALSE;
1376 /* All other cases: Single NAL unit packet Section 4.6 */
1377 /* the entire payload is the output buffer */
1381 goto not_implemented_donl_present;
1384 nalu_size = payload_len;
1385 outsize = nalu_size + sizeof (sync_bytes);
1386 outbuf = gst_buffer_new_and_alloc (outsize);
1388 gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
1389 if (rtph265depay->byte_stream) {
1390 memcpy (map.data, sync_bytes, sizeof (sync_bytes));
1392 goto not_implemented;
1394 memcpy (map.data + sizeof (sync_bytes), payload, nalu_size);
1395 gst_buffer_unmap (outbuf, &map);
1397 gst_rtp_copy_meta (GST_ELEMENT_CAST (rtph265depay), outbuf,
1398 rtp->buffer, g_quark_from_static_string (GST_META_TAG_VIDEO_STR));
1400 outbuf = gst_rtp_h265_depay_handle_nal (rtph265depay, outbuf, timestamp,
1412 GST_DEBUG_OBJECT (rtph265depay, "empty packet");
1417 GST_DEBUG_OBJECT (rtph265depay, "waiting for start");
1421 not_implemented_donl_present:
1423 GST_ELEMENT_ERROR (rtph265depay, STREAM, FORMAT,
1424 (NULL), ("DONL field present not supported yet"));
1430 GST_ELEMENT_ERROR (rtph265depay, STREAM, FORMAT,
1431 (NULL), ("NAL unit type %d not supported yet", nal_unit_type));
1437 gst_rtp_h265_depay_handle_event (GstRTPBaseDepayload * depay, GstEvent * event)
1439 GstRtpH265Depay *rtph265depay;
1441 rtph265depay = GST_RTP_H265_DEPAY (depay);
1443 switch (GST_EVENT_TYPE (event)) {
1444 case GST_EVENT_FLUSH_STOP:
1445 gst_rtp_h265_depay_reset (rtph265depay);
1452 GST_RTP_BASE_DEPAYLOAD_CLASS (parent_class)->handle_event (depay, event);
1455 static GstStateChangeReturn
1456 gst_rtp_h265_depay_change_state (GstElement * element,
1457 GstStateChange transition)
1459 GstRtpH265Depay *rtph265depay;
1460 GstStateChangeReturn ret;
1462 rtph265depay = GST_RTP_H265_DEPAY (element);
1464 switch (transition) {
1465 case GST_STATE_CHANGE_NULL_TO_READY:
1467 case GST_STATE_CHANGE_READY_TO_PAUSED:
1468 gst_rtp_h265_depay_reset (rtph265depay);
1474 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1476 switch (transition) {
1477 case GST_STATE_CHANGE_READY_TO_NULL:
1486 gst_rtp_h265_depay_plugin_init (GstPlugin * plugin)
1488 GST_DEBUG_CATEGORY_INIT (rtph265depay_debug, "rtph265depay", 0,
1489 "H265 Video RTP Depayloader");
1491 return gst_element_register (plugin, "rtph265depay",
1492 GST_RANK_SECONDARY, GST_TYPE_RTP_H265_DEPAY);