1 /* GStreamer
2 * Copyright (C) <2006> Wim Taymans <wim.taymans@gmail.com>
3 * Copyright (C) <2014> Jurgen Slowack <jurgenslowack@gmail.com>
4 *
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.
9 *
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.
14 *
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.
19 */
20
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24
25 #include <stdio.h>
26 #include <string.h>
27
28 #include <gst/base/gstbitreader.h>
29 #include <gst/rtp/gstrtpbuffer.h>
30 #include <gst/video/video.h>
31 #include "gstrtpelements.h"
32 #include "gstrtph265depay.h"
33 #include "gstrtputils.h"
34
35 GST_DEBUG_CATEGORY_STATIC (rtph265depay_debug);
36 #define GST_CAT_DEFAULT (rtph265depay_debug)
37
38 /* This is what we'll default to when downstream hasn't
39 * expressed a restriction or preference via caps */
40 #define DEFAULT_STREAM_FORMAT GST_H265_STREAM_FORMAT_BYTESTREAM
41 #define DEFAULT_ACCESS_UNIT FALSE
42
43 /* 3 zero bytes syncword */
44 static const guint8 sync_bytes[] = { 0, 0, 0, 1 };
45
46 static GstStaticPadTemplate gst_rtp_h265_depay_src_template =
47 GST_STATIC_PAD_TEMPLATE ("src",
48 GST_PAD_SRC,
49 GST_PAD_ALWAYS,
50 GST_STATIC_CAPS
51 ("video/x-h265, stream-format=(string)hvc1, alignment=(string)au; "
52 /* FIXME: hev1 format is not supported yet */
53 /* "video/x-h265, "
54 "stream-format = (string) hev1, alignment = (string) au; " */
55 "video/x-h265, "
56 "stream-format = (string) byte-stream, alignment = (string) { nal, au }")
57 );
58
59 static GstStaticPadTemplate gst_rtp_h265_depay_sink_template =
60 GST_STATIC_PAD_TEMPLATE ("sink",
61 GST_PAD_SINK,
62 GST_PAD_ALWAYS,
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, " */
96 );
97
98 #define gst_rtp_h265_depay_parent_class parent_class
99 G_DEFINE_TYPE_WITH_CODE (GstRtpH265Depay, gst_rtp_h265_depay,
100 GST_TYPE_RTP_BASE_DEPAYLOAD, GST_DEBUG_CATEGORY_INIT (rtph265depay_debug,
101 "rtph265depay", 0, "H265 Video RTP Depayloader"));
102 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (rtph265depay, "rtph265depay",
103 GST_RANK_SECONDARY, GST_TYPE_RTP_H265_DEPAY, rtp_element_init (plugin));
104
105 static void gst_rtp_h265_depay_finalize (GObject * object);
106
107 static GstStateChangeReturn gst_rtp_h265_depay_change_state (GstElement *
108 element, GstStateChange transition);
109
110 static GstBuffer *gst_rtp_h265_depay_process (GstRTPBaseDepayload * depayload,
111 GstRTPBuffer * rtp);
112 static gboolean gst_rtp_h265_depay_setcaps (GstRTPBaseDepayload * filter,
113 GstCaps * caps);
114 static gboolean gst_rtp_h265_depay_handle_event (GstRTPBaseDepayload * depay,
115 GstEvent * event);
116 static GstBuffer *gst_rtp_h265_complete_au (GstRtpH265Depay * rtph265depay,
117 GstClockTime * out_timestamp, gboolean * out_keyframe);
118 static void gst_rtp_h265_depay_push (GstRtpH265Depay * rtph265depay,
119 GstBuffer * outbuf, gboolean keyframe, GstClockTime timestamp,
120 gboolean marker);
121
122
123 static void
gst_rtp_h265_depay_class_init(GstRtpH265DepayClass * klass)124 gst_rtp_h265_depay_class_init (GstRtpH265DepayClass * klass)
125 {
126 GObjectClass *gobject_class;
127 GstElementClass *gstelement_class;
128 GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
129
130 gobject_class = (GObjectClass *) klass;
131 gstelement_class = (GstElementClass *) klass;
132 gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
133
134 gobject_class->finalize = gst_rtp_h265_depay_finalize;
135
136 gst_element_class_add_static_pad_template (gstelement_class,
137 &gst_rtp_h265_depay_src_template);
138 gst_element_class_add_static_pad_template (gstelement_class,
139 &gst_rtp_h265_depay_sink_template);
140
141 gst_element_class_set_static_metadata (gstelement_class,
142 "RTP H265 depayloader", "Codec/Depayloader/Network/RTP",
143 "Extracts H265 video from RTP packets (RFC 7798)",
144 "Jurgen Slowack <jurgenslowack@gmail.com>");
145 gstelement_class->change_state = gst_rtp_h265_depay_change_state;
146
147 gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_h265_depay_process;
148 gstrtpbasedepayload_class->set_caps = gst_rtp_h265_depay_setcaps;
149 gstrtpbasedepayload_class->handle_event = gst_rtp_h265_depay_handle_event;
150 }
151
152 static void
gst_rtp_h265_depay_init(GstRtpH265Depay * rtph265depay)153 gst_rtp_h265_depay_init (GstRtpH265Depay * rtph265depay)
154 {
155 rtph265depay->adapter = gst_adapter_new ();
156 rtph265depay->picture_adapter = gst_adapter_new ();
157 rtph265depay->output_format = DEFAULT_STREAM_FORMAT;
158 rtph265depay->byte_stream =
159 (DEFAULT_STREAM_FORMAT == GST_H265_STREAM_FORMAT_BYTESTREAM);
160 rtph265depay->stream_format = NULL;
161 rtph265depay->merge = DEFAULT_ACCESS_UNIT;
162 rtph265depay->vps = g_ptr_array_new_with_free_func (
163 (GDestroyNotify) gst_buffer_unref);
164 rtph265depay->sps = g_ptr_array_new_with_free_func (
165 (GDestroyNotify) gst_buffer_unref);
166 rtph265depay->pps = g_ptr_array_new_with_free_func (
167 (GDestroyNotify) gst_buffer_unref);
168 }
169
170 static void
gst_rtp_h265_depay_reset(GstRtpH265Depay * rtph265depay,gboolean hard)171 gst_rtp_h265_depay_reset (GstRtpH265Depay * rtph265depay, gboolean hard)
172 {
173 gst_adapter_clear (rtph265depay->adapter);
174 rtph265depay->wait_start = TRUE;
175 gst_adapter_clear (rtph265depay->picture_adapter);
176 rtph265depay->picture_start = FALSE;
177 rtph265depay->last_keyframe = FALSE;
178 rtph265depay->last_ts = 0;
179 rtph265depay->current_fu_type = 0;
180 rtph265depay->new_codec_data = FALSE;
181 g_ptr_array_set_size (rtph265depay->vps, 0);
182 g_ptr_array_set_size (rtph265depay->sps, 0);
183 g_ptr_array_set_size (rtph265depay->pps, 0);
184
185 if (hard) {
186 if (rtph265depay->allocator != NULL) {
187 gst_object_unref (rtph265depay->allocator);
188 rtph265depay->allocator = NULL;
189 }
190 gst_allocation_params_init (&rtph265depay->params);
191 }
192 }
193
194 static void
gst_rtp_h265_depay_drain(GstRtpH265Depay * rtph265depay)195 gst_rtp_h265_depay_drain (GstRtpH265Depay * rtph265depay)
196 {
197 GstClockTime timestamp;
198 gboolean keyframe;
199 GstBuffer *outbuf;
200
201 if (!rtph265depay->picture_start)
202 return;
203
204 outbuf = gst_rtp_h265_complete_au (rtph265depay, ×tamp, &keyframe);
205 if (outbuf)
206 gst_rtp_h265_depay_push (rtph265depay, outbuf, keyframe, timestamp, FALSE);
207 }
208
209 static void
gst_rtp_h265_depay_finalize(GObject * object)210 gst_rtp_h265_depay_finalize (GObject * object)
211 {
212 GstRtpH265Depay *rtph265depay;
213
214 rtph265depay = GST_RTP_H265_DEPAY (object);
215
216 if (rtph265depay->codec_data)
217 gst_buffer_unref (rtph265depay->codec_data);
218
219 g_object_unref (rtph265depay->adapter);
220 g_object_unref (rtph265depay->picture_adapter);
221
222 g_ptr_array_free (rtph265depay->vps, TRUE);
223 g_ptr_array_free (rtph265depay->sps, TRUE);
224 g_ptr_array_free (rtph265depay->pps, TRUE);
225
226 G_OBJECT_CLASS (parent_class)->finalize (object);
227 }
228
229 static inline const gchar *
stream_format_get_nick(GstH265StreamFormat fmt)230 stream_format_get_nick (GstH265StreamFormat fmt)
231 {
232 switch (fmt) {
233 case GST_H265_STREAM_FORMAT_BYTESTREAM:
234 return "byte-stream";
235 case GST_H265_STREAM_FORMAT_HVC1:
236 return "hvc1";
237 case GST_H265_STREAM_FORMAT_HEV1:
238 return "hev1";
239 default:
240 break;
241 }
242 return "unknown";
243 }
244
245 static void
gst_rtp_h265_depay_negotiate(GstRtpH265Depay * rtph265depay)246 gst_rtp_h265_depay_negotiate (GstRtpH265Depay * rtph265depay)
247 {
248 GstH265StreamFormat stream_format = GST_H265_STREAM_FORMAT_UNKNOWN;
249 GstCaps *caps;
250 gint merge = -1;
251
252 caps =
253 gst_pad_get_allowed_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (rtph265depay));
254
255 GST_DEBUG_OBJECT (rtph265depay, "allowed caps: %" GST_PTR_FORMAT, caps);
256
257 if (caps) {
258 if (gst_caps_get_size (caps) > 0) {
259 GstStructure *s = gst_caps_get_structure (caps, 0);
260 const gchar *str = NULL;
261
262 if ((str = gst_structure_get_string (s, "stream-format"))) {
263 rtph265depay->stream_format = g_intern_string (str);
264
265 if (strcmp (str, "hev1") == 0) {
266 stream_format = GST_H265_STREAM_FORMAT_HEV1;
267 } else if (strcmp (str, "hvc1") == 0) {
268 stream_format = GST_H265_STREAM_FORMAT_HVC1;
269 } else if (strcmp (str, "byte-stream") == 0) {
270 stream_format = GST_H265_STREAM_FORMAT_BYTESTREAM;
271 } else {
272 GST_DEBUG_OBJECT (rtph265depay, "unknown stream-format: %s", str);
273 }
274 }
275
276 if ((str = gst_structure_get_string (s, "alignment"))) {
277 if (strcmp (str, "au") == 0) {
278 merge = TRUE;
279 } else if (strcmp (str, "nal") == 0) {
280 merge = FALSE;
281 } else {
282 GST_DEBUG_OBJECT (rtph265depay, "unknown alignment: %s", str);
283 }
284 }
285 }
286 gst_caps_unref (caps);
287 }
288
289 if (stream_format != GST_H265_STREAM_FORMAT_UNKNOWN) {
290 GST_DEBUG_OBJECT (rtph265depay, "downstream wants stream-format %s",
291 stream_format_get_nick (stream_format));
292 rtph265depay->output_format = stream_format;
293 } else {
294 GST_DEBUG_OBJECT (rtph265depay, "defaulting to output stream-format %s",
295 stream_format_get_nick (DEFAULT_STREAM_FORMAT));
296 rtph265depay->stream_format =
297 stream_format_get_nick (DEFAULT_STREAM_FORMAT);
298 rtph265depay->output_format = DEFAULT_STREAM_FORMAT;
299 }
300 rtph265depay->byte_stream =
301 (rtph265depay->output_format == GST_H265_STREAM_FORMAT_BYTESTREAM);
302
303 if (merge != -1) {
304 GST_DEBUG_OBJECT (rtph265depay, "downstream requires merge %d", merge);
305 rtph265depay->merge = merge;
306 } else {
307 GST_DEBUG_OBJECT (rtph265depay, "defaulting to merge %d",
308 DEFAULT_ACCESS_UNIT);
309 rtph265depay->merge = DEFAULT_ACCESS_UNIT;
310 }
311 }
312
313 static gboolean
parse_sps(GstMapInfo * map,guint32 * sps_id)314 parse_sps (GstMapInfo * map, guint32 * sps_id)
315 { /* To parse seq_parameter_set_id */
316 GstBitReader br = GST_BIT_READER_INIT (map->data + 15,
317 map->size - 15);
318
319 GST_MEMDUMP ("SPS", map->data, map->size);
320
321 if (map->size < 16)
322 return FALSE;
323
324 if (!gst_rtp_read_golomb (&br, sps_id))
325 return FALSE;
326
327 return TRUE;
328 }
329
330 static gboolean
parse_pps(GstMapInfo * map,guint32 * sps_id,guint32 * pps_id)331 parse_pps (GstMapInfo * map, guint32 * sps_id, guint32 * pps_id)
332 { /* To parse picture_parameter_set_id */
333 GstBitReader br = GST_BIT_READER_INIT (map->data + 2,
334 map->size - 2);
335
336 GST_MEMDUMP ("PPS", map->data, map->size);
337
338 if (map->size < 3)
339 return FALSE;
340
341 if (!gst_rtp_read_golomb (&br, pps_id))
342 return FALSE;
343 if (!gst_rtp_read_golomb (&br, sps_id))
344 return FALSE;
345
346 return TRUE;
347 }
348
349 static gboolean
gst_rtp_h265_depay_set_output_caps(GstRtpH265Depay * rtph265depay,GstCaps * caps)350 gst_rtp_h265_depay_set_output_caps (GstRtpH265Depay * rtph265depay,
351 GstCaps * caps)
352 {
353 GstAllocationParams params;
354 GstAllocator *allocator = NULL;
355 GstPad *srcpad;
356 gboolean res;
357
358 gst_allocation_params_init (¶ms);
359
360 srcpad = GST_RTP_BASE_DEPAYLOAD_SRCPAD (rtph265depay);
361
362 res = gst_pad_set_caps (srcpad, caps);
363
364 if (res) {
365 GstQuery *query;
366
367 query = gst_query_new_allocation (caps, TRUE);
368 if (!gst_pad_peer_query (srcpad, query)) {
369 GST_DEBUG_OBJECT (rtph265depay, "downstream ALLOCATION query failed");
370 }
371
372 if (gst_query_get_n_allocation_params (query) > 0) {
373 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
374 }
375
376 gst_query_unref (query);
377 }
378
379 if (rtph265depay->allocator)
380 gst_object_unref (rtph265depay->allocator);
381
382 rtph265depay->allocator = allocator;
383 rtph265depay->params = params;
384
385 return res;
386 }
387
388 static gboolean
gst_rtp_h265_set_src_caps(GstRtpH265Depay * rtph265depay)389 gst_rtp_h265_set_src_caps (GstRtpH265Depay * rtph265depay)
390 {
391 gboolean res;
392 GstCaps *old_caps;
393 GstCaps *srccaps;
394 GstPad *srcpad;
395
396 if (!rtph265depay->byte_stream &&
397 (!rtph265depay->new_codec_data ||
398 rtph265depay->vps->len == 0 || rtph265depay->sps->len == 0
399 || rtph265depay->pps->len == 0))
400 return TRUE;
401
402 srccaps = gst_caps_new_simple ("video/x-h265",
403 "stream-format", G_TYPE_STRING, rtph265depay->stream_format,
404 "alignment", G_TYPE_STRING, rtph265depay->merge ? "au" : "nal", NULL);
405
406 if (!rtph265depay->byte_stream) {
407 GstBuffer *codec_data;
408 gint i = 0;
409 gint len;
410 guint num_vps = rtph265depay->vps->len;
411 guint num_sps = rtph265depay->sps->len;
412 guint num_pps = rtph265depay->pps->len;
413 GstMapInfo map, nalmap;
414 guint8 *data;
415 guint8 num_arrays = 0;
416 guint new_size;
417 GstBitReader br;
418 guint32 tmp;
419 guint8 tmp8 = 0;
420 guint32 max_sub_layers_minus1, temporal_id_nesting_flag, chroma_format_idc,
421 bit_depth_luma_minus8, bit_depth_chroma_minus8,
422 min_spatial_segmentation_idc;
423
424 /* Fixme: Current implementation is not embedding SEI in codec_data */
425
426 if (num_sps == 0)
427 return FALSE;
428
429 /* start with 23 bytes header */
430 len = 23;
431
432 num_arrays = (num_vps > 0) + (num_sps > 0) + (num_pps > 0);
433 len += 3 * num_arrays;
434
435 /* add size of vps, sps & pps */
436 for (i = 0; i < num_vps; i++)
437 len += 2 + gst_buffer_get_size (g_ptr_array_index (rtph265depay->vps, i));
438 for (i = 0; i < num_sps; i++)
439 len += 2 + gst_buffer_get_size (g_ptr_array_index (rtph265depay->sps, i));
440 for (i = 0; i < num_pps; i++)
441 len += 2 + gst_buffer_get_size (g_ptr_array_index (rtph265depay->pps, i));
442
443 GST_DEBUG_OBJECT (rtph265depay,
444 "constructing codec_data: num_vps =%d num_sps=%d, num_pps=%d", num_vps,
445 num_sps, num_pps);
446
447 codec_data = gst_buffer_new_and_alloc (len);
448 gst_buffer_map (codec_data, &map, GST_MAP_READWRITE);
449 data = map.data;
450
451 memset (data, 0, map.size);
452
453 /* Parsing sps to get the info required further on */
454
455 gst_buffer_map (g_ptr_array_index (rtph265depay->sps, 0), &nalmap,
456 GST_MAP_READ);
457
458 max_sub_layers_minus1 = ((nalmap.data[2]) >> 1) & 0x07;
459 temporal_id_nesting_flag = nalmap.data[2] & 0x01;
460
461 gst_bit_reader_init (&br, nalmap.data + 15, nalmap.size - 15);
462
463 gst_rtp_read_golomb (&br, &tmp); /* sps_seq_parameter_set_id */
464 gst_rtp_read_golomb (&br, &chroma_format_idc); /* chroma_format_idc */
465
466 if (chroma_format_idc == 3)
467 gst_bit_reader_get_bits_uint8 (&br, &tmp8, 1); /* separate_colour_plane_flag */
468
469 gst_rtp_read_golomb (&br, &tmp); /* pic_width_in_luma_samples */
470 gst_rtp_read_golomb (&br, &tmp); /* pic_height_in_luma_samples */
471
472 gst_bit_reader_get_bits_uint8 (&br, &tmp8, 1); /* conformance_window_flag */
473 if (tmp8) {
474 gst_rtp_read_golomb (&br, &tmp); /* conf_win_left_offset */
475 gst_rtp_read_golomb (&br, &tmp); /* conf_win_right_offset */
476 gst_rtp_read_golomb (&br, &tmp); /* conf_win_top_offset */
477 gst_rtp_read_golomb (&br, &tmp); /* conf_win_bottom_offset */
478 }
479
480 gst_rtp_read_golomb (&br, &bit_depth_luma_minus8); /* bit_depth_luma_minus8 */
481 gst_rtp_read_golomb (&br, &bit_depth_chroma_minus8); /* bit_depth_chroma_minus8 */
482
483 GST_DEBUG_OBJECT (rtph265depay,
484 "Ignoring min_spatial_segmentation for now (assuming zero)");
485
486 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 */
487
488 gst_buffer_unmap (g_ptr_array_index (rtph265depay->sps, 0), &nalmap);
489
490 /* HEVCDecoderConfigurationVersion = 1 */
491 data[0] = 1;
492
493 /* Copy from profile_tier_level (Rec. ITU-T H.265 (04/2013) section 7.3.3
494 *
495 * profile_space | tier_flat | profile_idc |
496 * profile_compatibility_flags | constraint_indicator_flags |
497 * level_idc | progressive_source_flag | interlaced_source_flag
498 * non_packed_constraint_flag | frame_only_constraint_flag
499 * reserved_zero_44bits | level_idc */
500 gst_buffer_map (g_ptr_array_index (rtph265depay->sps, 0), &nalmap,
501 GST_MAP_READ);
502 for (i = 0; i < 12; i++)
503 data[i + 1] = nalmap.data[i];
504 gst_buffer_unmap (g_ptr_array_index (rtph265depay->sps, 0), &nalmap);
505
506 /* min_spatial_segmentation_idc */
507 GST_WRITE_UINT16_BE (data + 13, min_spatial_segmentation_idc);
508 data[13] |= 0xf0;
509 data[15] = 0xfc; /* keeping parrallelismType as zero (unknown) */
510 data[16] = 0xfc | chroma_format_idc;
511 data[17] = 0xf8 | bit_depth_luma_minus8;
512 data[18] = 0xf8 | bit_depth_chroma_minus8;
513 data[19] = 0x00; /* keep avgFrameRate as unspecified */
514 data[20] = 0x00; /* keep avgFrameRate as unspecified */
515 /* constFrameRate(2 bits): 0, stream may or may not be of constant framerate
516 * numTemporalLayers (3 bits): number of temporal layers, value from SPS
517 * TemporalIdNested (1 bit): sps_temporal_id_nesting_flag from SPS
518 * lengthSizeMinusOne (2 bits): plus 1 indicates the length of the NALUnitLength */
519 /* we always output NALs with 4-byte nal unit length markers (or sync code) */
520 data[21] = rtph265depay->byte_stream ? 0x00 : 0x03;
521 data[21] |= ((max_sub_layers_minus1 + 1) << 3);
522 data[21] |= (temporal_id_nesting_flag << 2);
523 GST_WRITE_UINT8 (data + 22, num_arrays); /* numOfArrays */
524
525 data += 23;
526
527 /* copy all VPS */
528 if (num_vps > 0) {
529 /* array_completeness | reserved_zero bit | nal_unit_type */
530 data[0] = 0x00 | 0x20;
531 data++;
532
533 GST_WRITE_UINT16_BE (data, num_vps);
534 data += 2;
535
536 for (i = 0; i < num_vps; i++) {
537 gsize nal_size =
538 gst_buffer_get_size (g_ptr_array_index (rtph265depay->vps, i));
539 GST_WRITE_UINT16_BE (data, nal_size);
540 gst_buffer_extract (g_ptr_array_index (rtph265depay->vps, i), 0,
541 data + 2, nal_size);
542 data += 2 + nal_size;
543 GST_DEBUG_OBJECT (rtph265depay, "Copied VPS %d of length %u", i,
544 (guint) nal_size);
545 }
546 }
547
548 /* copy all SPS */
549 if (num_sps > 0) {
550 /* array_completeness | reserved_zero bit | nal_unit_type */
551 data[0] = 0x00 | 0x21;
552 data++;
553
554 GST_WRITE_UINT16_BE (data, num_sps);
555 data += 2;
556
557 for (i = 0; i < num_sps; i++) {
558 gsize nal_size =
559 gst_buffer_get_size (g_ptr_array_index (rtph265depay->sps, i));
560 GST_WRITE_UINT16_BE (data, nal_size);
561 gst_buffer_extract (g_ptr_array_index (rtph265depay->sps, i), 0,
562 data + 2, nal_size);
563 data += 2 + nal_size;
564 GST_DEBUG_OBJECT (rtph265depay, "Copied SPS %d of length %u", i,
565 (guint) nal_size);
566 }
567 }
568
569 /* copy all PPS */
570 if (num_pps > 0) {
571 /* array_completeness | reserved_zero bit | nal_unit_type */
572 data[0] = 0x00 | 0x22;
573 data++;
574
575 GST_WRITE_UINT16_BE (data, num_pps);
576 data += 2;
577
578 for (i = 0; i < num_pps; i++) {
579 gsize nal_size =
580 gst_buffer_get_size (g_ptr_array_index (rtph265depay->pps, i));
581 GST_WRITE_UINT16_BE (data, nal_size);
582 gst_buffer_extract (g_ptr_array_index (rtph265depay->pps, i), 0,
583 data + 2, nal_size);
584 data += 2 + nal_size;
585 GST_DEBUG_OBJECT (rtph265depay, "Copied PPS %d of length %u", i,
586 (guint) nal_size);
587 }
588 }
589
590 new_size = data - map.data;
591 gst_buffer_unmap (codec_data, &map);
592 gst_buffer_set_size (codec_data, new_size);
593
594 gst_caps_set_simple (srccaps,
595 "codec_data", GST_TYPE_BUFFER, codec_data, NULL);
596 gst_buffer_unref (codec_data);
597 }
598
599 srcpad = GST_RTP_BASE_DEPAYLOAD_SRCPAD (rtph265depay);
600 old_caps = gst_pad_get_current_caps (srcpad);
601
602 if (old_caps == NULL || !gst_caps_is_equal (srccaps, old_caps)) {
603 res = gst_rtp_h265_depay_set_output_caps (rtph265depay, srccaps);
604 } else {
605 res = TRUE;
606 }
607
608 gst_caps_unref (srccaps);
609
610 /* Insert SPS and PPS into the stream on next opportunity */
611 if (rtph265depay->output_format != GST_H265_STREAM_FORMAT_HVC1
612 && (rtph265depay->sps->len > 0 || rtph265depay->pps->len > 0)) {
613 gint i;
614 GstBuffer *codec_data;
615 GstMapInfo map;
616 guint8 *data;
617 guint len = 0;
618
619 for (i = 0; i < rtph265depay->sps->len; i++) {
620 len += 4 + gst_buffer_get_size (g_ptr_array_index (rtph265depay->sps, i));
621 }
622
623 for (i = 0; i < rtph265depay->pps->len; i++) {
624 len += 4 + gst_buffer_get_size (g_ptr_array_index (rtph265depay->pps, i));
625 }
626
627 codec_data = gst_buffer_new_and_alloc (len);
628 gst_buffer_map (codec_data, &map, GST_MAP_WRITE);
629 data = map.data;
630
631 for (i = 0; i < rtph265depay->sps->len; i++) {
632 GstBuffer *sps_buf = g_ptr_array_index (rtph265depay->sps, i);
633 guint sps_size = gst_buffer_get_size (sps_buf);
634
635 if (rtph265depay->byte_stream)
636 memcpy (data, sync_bytes, sizeof (sync_bytes));
637 else
638 GST_WRITE_UINT32_BE (data, sps_size);
639 gst_buffer_extract (sps_buf, 0, data + 4, -1);
640 data += 4 + sps_size;
641 }
642
643 for (i = 0; i < rtph265depay->pps->len; i++) {
644 GstBuffer *pps_buf = g_ptr_array_index (rtph265depay->pps, i);
645 guint pps_size = gst_buffer_get_size (pps_buf);
646
647 if (rtph265depay->byte_stream)
648 memcpy (data, sync_bytes, sizeof (sync_bytes));
649 else
650 GST_WRITE_UINT32_BE (data, pps_size);
651 gst_buffer_extract (pps_buf, 0, data + 4, -1);
652 data += 4 + pps_size;
653 }
654
655 gst_buffer_unmap (codec_data, &map);
656 if (rtph265depay->codec_data)
657 gst_buffer_unref (rtph265depay->codec_data);
658 rtph265depay->codec_data = codec_data;
659 }
660
661 if (res)
662 rtph265depay->new_codec_data = FALSE;
663
664 return res;
665 }
666
667 gboolean
gst_rtp_h265_add_vps_sps_pps(GstElement * rtph265,GPtrArray * vps_array,GPtrArray * sps_array,GPtrArray * pps_array,GstBuffer * nal)668 gst_rtp_h265_add_vps_sps_pps (GstElement * rtph265, GPtrArray * vps_array,
669 GPtrArray * sps_array, GPtrArray * pps_array, GstBuffer * nal)
670 {
671 GstMapInfo map;
672 guchar type;
673 guint i;
674
675 gst_buffer_map (nal, &map, GST_MAP_READ);
676
677 type = (map.data[0] >> 1) & 0x3f;
678
679 if (type == GST_H265_VPS_NUT) {
680 guint32 vps_id = (map.data[2] >> 4) & 0x0f;
681
682 for (i = 0; i < vps_array->len; i++) {
683 GstBuffer *vps = g_ptr_array_index (vps_array, i);
684 GstMapInfo vpsmap;
685 guint32 tmp_vps_id;
686
687 gst_buffer_map (vps, &vpsmap, GST_MAP_READ);
688 tmp_vps_id = (vpsmap.data[2] >> 4) & 0x0f;
689
690 if (vps_id == tmp_vps_id) {
691 if (map.size == vpsmap.size &&
692 memcmp (map.data, vpsmap.data, vpsmap.size) == 0) {
693 GST_LOG_OBJECT (rtph265, "Unchanged VPS %u, not updating", vps_id);
694 gst_buffer_unmap (vps, &vpsmap);
695 goto drop;
696 } else {
697 gst_buffer_unmap (vps, &vpsmap);
698 g_ptr_array_remove_index_fast (vps_array, i);
699 g_ptr_array_add (vps_array, nal);
700 GST_LOG_OBJECT (rtph265, "Modified VPS %u, replacing", vps_id);
701 goto done;
702 }
703 }
704 gst_buffer_unmap (vps, &vpsmap);
705 }
706 GST_LOG_OBJECT (rtph265, "Adding new VPS %u", vps_id);
707 g_ptr_array_add (vps_array, nal);
708 } else if (type == GST_H265_SPS_NUT) {
709 guint32 sps_id;
710
711 if (!parse_sps (&map, &sps_id)) {
712 GST_WARNING_OBJECT (rtph265, "Invalid SPS,"
713 " can't parse seq_parameter_set_id");
714 goto drop;
715 }
716
717 for (i = 0; i < sps_array->len; i++) {
718 GstBuffer *sps = g_ptr_array_index (sps_array, i);
719 GstMapInfo spsmap;
720 guint32 tmp_sps_id;
721
722 gst_buffer_map (sps, &spsmap, GST_MAP_READ);
723 parse_sps (&spsmap, &tmp_sps_id);
724
725 if (sps_id == tmp_sps_id) {
726 if (map.size == spsmap.size &&
727 memcmp (map.data, spsmap.data, spsmap.size) == 0) {
728 GST_LOG_OBJECT (rtph265, "Unchanged SPS %u, not updating", sps_id);
729 gst_buffer_unmap (sps, &spsmap);
730 goto drop;
731 } else {
732 gst_buffer_unmap (sps, &spsmap);
733 g_ptr_array_remove_index_fast (sps_array, i);
734 g_ptr_array_add (sps_array, nal);
735 GST_LOG_OBJECT (rtph265, "Modified SPS %u, replacing", sps_id);
736 goto done;
737 }
738 }
739 gst_buffer_unmap (sps, &spsmap);
740 }
741 GST_LOG_OBJECT (rtph265, "Adding new SPS %u", sps_id);
742 g_ptr_array_add (sps_array, nal);
743 } else if (type == GST_H265_PPS_NUT) {
744 guint32 sps_id;
745 guint32 pps_id;
746
747 if (!parse_pps (&map, &sps_id, &pps_id)) {
748 GST_WARNING_OBJECT (rtph265, "Invalid PPS,"
749 " can't parse seq_parameter_set_id or pic_parameter_set_id");
750 goto drop;
751 }
752
753 for (i = 0; i < pps_array->len; i++) {
754 GstBuffer *pps = g_ptr_array_index (pps_array, i);
755 GstMapInfo ppsmap;
756 guint32 tmp_sps_id;
757 guint32 tmp_pps_id;
758
759
760 gst_buffer_map (pps, &ppsmap, GST_MAP_READ);
761 parse_pps (&ppsmap, &tmp_sps_id, &tmp_pps_id);
762
763 if (pps_id == tmp_pps_id) {
764 if (map.size == ppsmap.size &&
765 memcmp (map.data, ppsmap.data, ppsmap.size) == 0) {
766 GST_LOG_OBJECT (rtph265, "Unchanged PPS %u:%u, not updating", sps_id,
767 pps_id);
768 gst_buffer_unmap (pps, &ppsmap);
769 goto drop;
770 } else {
771 gst_buffer_unmap (pps, &ppsmap);
772 g_ptr_array_remove_index_fast (pps_array, i);
773 g_ptr_array_add (pps_array, nal);
774 GST_LOG_OBJECT (rtph265, "Modified PPS %u:%u, replacing",
775 sps_id, pps_id);
776 goto done;
777 }
778 }
779 gst_buffer_unmap (pps, &ppsmap);
780 }
781 GST_LOG_OBJECT (rtph265, "Adding new PPS %u:%i", sps_id, pps_id);
782 g_ptr_array_add (pps_array, nal);
783 } else {
784 goto drop;
785 }
786
787 done:
788 gst_buffer_unmap (nal, &map);
789
790 return TRUE;
791
792 drop:
793 gst_buffer_unmap (nal, &map);
794 gst_buffer_unref (nal);
795
796 return FALSE;
797 }
798
799
800 static void
gst_rtp_h265_depay_add_vps_sps_pps(GstRtpH265Depay * rtph265depay,GstBuffer * nal)801 gst_rtp_h265_depay_add_vps_sps_pps (GstRtpH265Depay * rtph265depay,
802 GstBuffer * nal)
803 {
804 if (gst_rtp_h265_add_vps_sps_pps (GST_ELEMENT (rtph265depay),
805 rtph265depay->vps, rtph265depay->sps, rtph265depay->pps, nal))
806 rtph265depay->new_codec_data = TRUE;
807 }
808
809 static gboolean
gst_rtp_h265_depay_setcaps(GstRTPBaseDepayload * depayload,GstCaps * caps)810 gst_rtp_h265_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
811 {
812 gint clock_rate;
813 GstStructure *structure = gst_caps_get_structure (caps, 0);
814 GstRtpH265Depay *rtph265depay;
815 const gchar *vps;
816 const gchar *sps;
817 const gchar *pps;
818 gchar *ps;
819 GstMapInfo map;
820 guint8 *ptr;
821
822 rtph265depay = GST_RTP_H265_DEPAY (depayload);
823
824 if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
825 clock_rate = 90000;
826 depayload->clock_rate = clock_rate;
827
828 /* Base64 encoded, comma separated config NALs */
829 vps = gst_structure_get_string (structure, "sprop-vps");
830 sps = gst_structure_get_string (structure, "sprop-sps");
831 pps = gst_structure_get_string (structure, "sprop-pps");
832 if (vps == NULL || sps == NULL || pps == NULL) {
833 ps = NULL;
834 } else {
835 ps = g_strdup_printf ("%s,%s,%s", vps, sps, pps);
836 }
837
838 /* negotiate with downstream w.r.t. output format and alignment */
839 gst_rtp_h265_depay_negotiate (rtph265depay);
840
841 if (rtph265depay->byte_stream && ps != NULL) {
842 /* for bytestream we only need the parameter sets but we don't error out
843 * when they are not there, we assume they are in the stream. */
844 gchar **params;
845 GstBuffer *codec_data;
846 guint len, total;
847 gint i;
848
849 params = g_strsplit (ps, ",", 0);
850
851 /* count total number of bytes in base64. Also include the sync bytes in
852 * front of the params. */
853 len = 0;
854 for (i = 0; params[i]; i++) {
855 len += strlen (params[i]);
856 len += sizeof (sync_bytes);
857 }
858 /* we seriously overshoot the length, but it's fine. */
859 codec_data = gst_buffer_new_and_alloc (len);
860
861 gst_buffer_map (codec_data, &map, GST_MAP_WRITE);
862 ptr = map.data;
863 total = 0;
864 for (i = 0; params[i]; i++) {
865 guint save = 0;
866 gint state = 0;
867
868 GST_DEBUG_OBJECT (depayload, "decoding param %d (%s)", i, params[i]);
869 memcpy (ptr, sync_bytes, sizeof (sync_bytes));
870 ptr += sizeof (sync_bytes);
871 len =
872 g_base64_decode_step (params[i], strlen (params[i]), ptr, &state,
873 &save);
874 GST_DEBUG_OBJECT (depayload, "decoded %d bytes", len);
875 total += len + sizeof (sync_bytes);
876 ptr += len;
877 }
878 gst_buffer_unmap (codec_data, &map);
879 gst_buffer_resize (codec_data, 0, total);
880 g_strfreev (params);
881
882 /* keep the codec_data, we need to send it as the first buffer. We cannot
883 * push it in the adapter because the adapter might be flushed on discont.
884 */
885 if (rtph265depay->codec_data)
886 gst_buffer_unref (rtph265depay->codec_data);
887 rtph265depay->codec_data = codec_data;
888 } else if (!rtph265depay->byte_stream) {
889 gchar **params;
890 gint i;
891
892 if (ps == NULL)
893 goto incomplete_caps;
894
895 params = g_strsplit (ps, ",", 0);
896
897 GST_DEBUG_OBJECT (depayload, "we have %d params", g_strv_length (params));
898
899 /* start with 23 bytes header */
900 for (i = 0; params[i]; i++) {
901 GstBuffer *nal;
902 GstMapInfo nalmap;
903 gsize nal_len;
904 guint save = 0;
905 gint state = 0;
906
907 nal_len = strlen (params[i]);
908 if (nal_len == 0) {
909 GST_WARNING_OBJECT (depayload, "empty param '%s' (#%d)", params[i], i);
910 continue;
911 }
912 nal = gst_buffer_new_and_alloc (nal_len);
913 gst_buffer_map (nal, &nalmap, GST_MAP_READWRITE);
914
915 nal_len =
916 g_base64_decode_step (params[i], nal_len, nalmap.data, &state, &save);
917
918 GST_DEBUG_OBJECT (depayload, "adding param %d as %s", i,
919 (((nalmap.data[0] >> 1) & 0x3f) ==
920 32) ? "VPS" : (((nalmap.data[0] >> 1) & 0x3f) ==
921 33) ? "SPS" : "PPS");
922
923 gst_buffer_unmap (nal, &nalmap);
924 gst_buffer_set_size (nal, nal_len);
925
926 gst_rtp_h265_depay_add_vps_sps_pps (rtph265depay, nal);
927 }
928 g_strfreev (params);
929
930 if (rtph265depay->vps->len == 0 || rtph265depay->sps->len == 0 ||
931 rtph265depay->pps->len == 0) {
932 goto incomplete_caps;
933 }
934 }
935
936 g_free (ps);
937
938 return gst_rtp_h265_set_src_caps (rtph265depay);
939
940 /* ERRORS */
941 incomplete_caps:
942 {
943 GST_DEBUG_OBJECT (depayload, "we have incomplete caps,"
944 " doing setcaps later");
945 g_free (ps);
946 return TRUE;
947 }
948 }
949
950 static GstBuffer *
gst_rtp_h265_depay_allocate_output_buffer(GstRtpH265Depay * depay,gsize size)951 gst_rtp_h265_depay_allocate_output_buffer (GstRtpH265Depay * depay, gsize size)
952 {
953 GstBuffer *buffer = NULL;
954
955 g_return_val_if_fail (size > 0, NULL);
956
957 GST_LOG_OBJECT (depay, "want output buffer of %u bytes", (guint) size);
958
959 buffer = gst_buffer_new_allocate (depay->allocator, size, &depay->params);
960 if (buffer == NULL) {
961 GST_INFO_OBJECT (depay, "couldn't allocate output buffer");
962 buffer = gst_buffer_new_allocate (NULL, size, NULL);
963 }
964
965 return buffer;
966 }
967
968 static GstBuffer *
gst_rtp_h265_complete_au(GstRtpH265Depay * rtph265depay,GstClockTime * out_timestamp,gboolean * out_keyframe)969 gst_rtp_h265_complete_au (GstRtpH265Depay * rtph265depay,
970 GstClockTime * out_timestamp, gboolean * out_keyframe)
971 {
972 GstBufferList *list;
973 GstMapInfo outmap;
974 GstBuffer *outbuf;
975 guint outsize, offset = 0;
976 gint b, n_bufs, m, n_mem;
977
978 /* we had a picture in the adapter and we completed it */
979 GST_DEBUG_OBJECT (rtph265depay, "taking completed AU");
980 outsize = gst_adapter_available (rtph265depay->picture_adapter);
981
982 outbuf = gst_rtp_h265_depay_allocate_output_buffer (rtph265depay, outsize);
983
984 if (outbuf == NULL)
985 return NULL;
986
987 if (!gst_buffer_map (outbuf, &outmap, GST_MAP_WRITE))
988 return NULL;
989
990 list = gst_adapter_take_buffer_list (rtph265depay->picture_adapter, outsize);
991
992 n_bufs = gst_buffer_list_length (list);
993 for (b = 0; b < n_bufs; ++b) {
994 GstBuffer *buf = gst_buffer_list_get (list, b);
995
996 n_mem = gst_buffer_n_memory (buf);
997 for (m = 0; m < n_mem; ++m) {
998 GstMemory *mem = gst_buffer_peek_memory (buf, m);
999 gsize mem_size = gst_memory_get_sizes (mem, NULL, NULL);
1000 GstMapInfo mem_map;
1001
1002 if (gst_memory_map (mem, &mem_map, GST_MAP_READ)) {
1003 memcpy (outmap.data + offset, mem_map.data, mem_size);
1004 gst_memory_unmap (mem, &mem_map);
1005 } else {
1006 memset (outmap.data + offset, 0, mem_size);
1007 }
1008 offset += mem_size;
1009 }
1010
1011 gst_rtp_copy_video_meta (rtph265depay, outbuf, buf);
1012 }
1013 gst_buffer_list_unref (list);
1014 gst_buffer_unmap (outbuf, &outmap);
1015
1016 *out_timestamp = rtph265depay->last_ts;
1017 *out_keyframe = rtph265depay->last_keyframe;
1018
1019 rtph265depay->last_keyframe = FALSE;
1020 rtph265depay->picture_start = FALSE;
1021
1022 return outbuf;
1023 }
1024
1025 /* VPS/SPS/PPS/RADL/TSA/RASL/IDR/CRA is considered key, all others DELTA;
1026 * so downstream waiting for keyframe can pick up at VPS/SPS/PPS/IDR */
1027
1028 #define NAL_TYPE_IS_PARAMETER_SET(nt) ( ((nt) == GST_H265_VPS_NUT)\
1029 || ((nt) == GST_H265_SPS_NUT)\
1030 || ((nt) == GST_H265_PPS_NUT) )
1031
1032 #define NAL_TYPE_IS_CODED_SLICE_SEGMENT(nt) ( ((nt) == GST_H265_NAL_SLICE_TRAIL_N)\
1033 || ((nt) == GST_H265_NAL_SLICE_TRAIL_R)\
1034 || ((nt) == GST_H265_NAL_SLICE_TSA_N)\
1035 || ((nt) == GST_H265_NAL_SLICE_TSA_R)\
1036 || ((nt) == GST_H265_NAL_SLICE_STSA_N)\
1037 || ((nt) == GST_H265_NAL_SLICE_STSA_R)\
1038 || ((nt) == GST_H265_NAL_SLICE_RASL_N)\
1039 || ((nt) == GST_H265_NAL_SLICE_RASL_R)\
1040 || ((nt) == GST_H265_NAL_SLICE_BLA_W_LP)\
1041 || ((nt) == GST_H265_NAL_SLICE_BLA_W_RADL)\
1042 || ((nt) == GST_H265_NAL_SLICE_BLA_N_LP)\
1043 || ((nt) == GST_H265_NAL_SLICE_IDR_W_RADL)\
1044 || ((nt) == GST_H265_NAL_SLICE_IDR_N_LP)\
1045 || ((nt) == GST_H265_NAL_SLICE_CRA_NUT) )
1046
1047 /* Intra random access point */
1048 #define NAL_TYPE_IS_IRAP(nt) (((nt) == GST_H265_NAL_SLICE_BLA_W_LP) \
1049 || ((nt) == GST_H265_NAL_SLICE_BLA_W_RADL) \
1050 || ((nt) == GST_H265_NAL_SLICE_BLA_N_LP) \
1051 || ((nt) == GST_H265_NAL_SLICE_IDR_W_RADL) \
1052 || ((nt) == GST_H265_NAL_SLICE_IDR_N_LP) \
1053 || ((nt) == GST_H265_NAL_SLICE_CRA_NUT))
1054
1055 #define NAL_TYPE_IS_KEY(nt) (NAL_TYPE_IS_PARAMETER_SET(nt) || NAL_TYPE_IS_IRAP(nt))
1056
1057 static void
gst_rtp_h265_depay_push(GstRtpH265Depay * rtph265depay,GstBuffer * outbuf,gboolean keyframe,GstClockTime timestamp,gboolean marker)1058 gst_rtp_h265_depay_push (GstRtpH265Depay * rtph265depay, GstBuffer * outbuf,
1059 gboolean keyframe, GstClockTime timestamp, gboolean marker)
1060 {
1061 /* prepend codec_data */
1062 if (rtph265depay->codec_data) {
1063 GST_DEBUG_OBJECT (rtph265depay, "prepending codec_data");
1064 gst_rtp_copy_video_meta (rtph265depay, rtph265depay->codec_data, outbuf);
1065 outbuf = gst_buffer_append (rtph265depay->codec_data, outbuf);
1066 rtph265depay->codec_data = NULL;
1067 keyframe = TRUE;
1068 }
1069 outbuf = gst_buffer_make_writable (outbuf);
1070
1071 gst_rtp_drop_non_video_meta (rtph265depay, outbuf);
1072
1073 GST_BUFFER_PTS (outbuf) = timestamp;
1074
1075 if (keyframe)
1076 GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
1077 else
1078 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
1079
1080 if (marker)
1081 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_MARKER);
1082
1083 gst_rtp_base_depayload_push (GST_RTP_BASE_DEPAYLOAD (rtph265depay), outbuf);
1084 }
1085
1086 static void
gst_rtp_h265_depay_handle_nal(GstRtpH265Depay * rtph265depay,GstBuffer * nal,GstClockTime in_timestamp,gboolean marker)1087 gst_rtp_h265_depay_handle_nal (GstRtpH265Depay * rtph265depay, GstBuffer * nal,
1088 GstClockTime in_timestamp, gboolean marker)
1089 {
1090 GstRTPBaseDepayload *depayload = GST_RTP_BASE_DEPAYLOAD (rtph265depay);
1091 gint nal_type;
1092 GstMapInfo map;
1093 GstBuffer *outbuf = NULL;
1094 GstClockTime out_timestamp;
1095 gboolean keyframe, out_keyframe;
1096
1097 gst_buffer_map (nal, &map, GST_MAP_READ);
1098 if (G_UNLIKELY (map.size < 5))
1099 goto short_nal;
1100
1101 nal_type = (map.data[4] >> 1) & 0x3f;
1102 GST_DEBUG_OBJECT (rtph265depay, "handle NAL type %d (RTP marker bit %d)",
1103 nal_type, marker);
1104
1105 keyframe = NAL_TYPE_IS_KEY (nal_type);
1106
1107 out_keyframe = keyframe;
1108 out_timestamp = in_timestamp;
1109
1110 if (!rtph265depay->byte_stream) {
1111 if (NAL_TYPE_IS_PARAMETER_SET (nal_type)) {
1112 gst_rtp_h265_depay_add_vps_sps_pps (rtph265depay,
1113 gst_buffer_copy_region (nal, GST_BUFFER_COPY_ALL,
1114 4, gst_buffer_get_size (nal) - 4));
1115 gst_buffer_unmap (nal, &map);
1116 gst_buffer_unref (nal);
1117 return;
1118 } else if (rtph265depay->sps->len == 0 || rtph265depay->pps->len == 0) {
1119 /* Down push down any buffer in non-bytestream mode if the SPS/PPS haven't
1120 * go through yet
1121 */
1122 gst_pad_push_event (GST_RTP_BASE_DEPAYLOAD_SINKPAD (depayload),
1123 gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM,
1124 gst_structure_new ("GstForceKeyUnit",
1125 "all-headers", G_TYPE_BOOLEAN, TRUE, NULL)));
1126 gst_buffer_unmap (nal, &map);
1127 gst_buffer_unref (nal);
1128 return;
1129 }
1130
1131 if (rtph265depay->new_codec_data &&
1132 rtph265depay->sps->len > 0 && rtph265depay->pps->len > 0)
1133 gst_rtp_h265_set_src_caps (rtph265depay);
1134 }
1135
1136 if (rtph265depay->merge) {
1137 gboolean start = FALSE, complete = FALSE;
1138
1139 /* marker bit isn't mandatory so in the following code we try to detect
1140 * an AU boundary (see H.265 spec section 7.4.2.4.4) */
1141 if (!marker) {
1142 if (NAL_TYPE_IS_CODED_SLICE_SEGMENT (nal_type)) {
1143 /* 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 */
1144 start = TRUE;
1145 if (((map.data[6] >> 7) & 0x01) == 1) {
1146 complete = TRUE;
1147 }
1148 } else if ((nal_type >= 32 && nal_type <= 35)
1149 || nal_type == 39 || (nal_type >= 41 && nal_type <= 44)
1150 || (nal_type >= 48 && nal_type <= 55)) {
1151 /* VPS, SPS, PPS, SEI, ... terminate an access unit */
1152 complete = TRUE;
1153 }
1154 GST_DEBUG_OBJECT (depayload, "start %d, complete %d", start, complete);
1155
1156 if (complete && rtph265depay->picture_start)
1157 outbuf = gst_rtp_h265_complete_au (rtph265depay, &out_timestamp,
1158 &out_keyframe);
1159 }
1160 /* add to adapter */
1161 gst_buffer_unmap (nal, &map);
1162
1163 GST_DEBUG_OBJECT (depayload, "adding NAL to picture adapter");
1164 gst_adapter_push (rtph265depay->picture_adapter, nal);
1165 rtph265depay->last_ts = in_timestamp;
1166 rtph265depay->last_keyframe |= keyframe;
1167 rtph265depay->picture_start |= start;
1168
1169 if (marker)
1170 outbuf = gst_rtp_h265_complete_au (rtph265depay, &out_timestamp,
1171 &out_keyframe);
1172 } else {
1173 /* no merge, output is input nal */
1174 GST_DEBUG_OBJECT (depayload, "using NAL as output");
1175 outbuf = nal;
1176 gst_buffer_unmap (nal, &map);
1177 }
1178
1179 if (outbuf) {
1180 gst_rtp_h265_depay_push (rtph265depay, outbuf, out_keyframe, out_timestamp,
1181 marker);
1182 }
1183
1184 return;
1185
1186 /* ERRORS */
1187 short_nal:
1188 {
1189 GST_WARNING_OBJECT (depayload, "dropping short NAL");
1190 gst_buffer_unmap (nal, &map);
1191 gst_buffer_unref (nal);
1192 return;
1193 }
1194 }
1195
1196 static void
gst_rtp_h265_finish_fragmentation_unit(GstRtpH265Depay * rtph265depay)1197 gst_rtp_h265_finish_fragmentation_unit (GstRtpH265Depay * rtph265depay)
1198 {
1199 guint outsize;
1200 GstMapInfo map;
1201 GstBuffer *outbuf;
1202
1203 outsize = gst_adapter_available (rtph265depay->adapter);
1204 g_assert (outsize >= 4);
1205
1206 outbuf = gst_adapter_take_buffer (rtph265depay->adapter, outsize);
1207
1208 gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
1209 GST_DEBUG_OBJECT (rtph265depay, "output %d bytes", outsize);
1210
1211 if (rtph265depay->byte_stream) {
1212 memcpy (map.data, sync_bytes, sizeof (sync_bytes));
1213 } else {
1214 GST_WRITE_UINT32_BE (map.data, outsize - 4);
1215 }
1216 gst_buffer_unmap (outbuf, &map);
1217
1218 rtph265depay->current_fu_type = 0;
1219
1220 gst_rtp_h265_depay_handle_nal (rtph265depay, outbuf,
1221 rtph265depay->fu_timestamp, rtph265depay->fu_marker);
1222 }
1223
1224 static GstBuffer *
gst_rtp_h265_depay_process(GstRTPBaseDepayload * depayload,GstRTPBuffer * rtp)1225 gst_rtp_h265_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
1226 {
1227 GstRtpH265Depay *rtph265depay;
1228 GstBuffer *outbuf = NULL;
1229 guint8 nal_unit_type;
1230
1231 rtph265depay = GST_RTP_H265_DEPAY (depayload);
1232
1233 /* flush remaining data on discont */
1234 if (GST_BUFFER_IS_DISCONT (rtp->buffer)) {
1235 gst_adapter_clear (rtph265depay->adapter);
1236 rtph265depay->wait_start = TRUE;
1237 rtph265depay->current_fu_type = 0;
1238 rtph265depay->last_fu_seqnum = 0;
1239 }
1240
1241 {
1242 gint payload_len;
1243 guint8 *payload;
1244 guint header_len;
1245 GstMapInfo map;
1246 guint outsize, nalu_size;
1247 GstClockTime timestamp;
1248 gboolean marker;
1249 guint8 nuh_layer_id, nuh_temporal_id_plus1;
1250 guint8 S, E;
1251 guint16 nal_header;
1252 #if 0
1253 gboolean donl_present = FALSE;
1254 #endif
1255
1256 timestamp = GST_BUFFER_PTS (rtp->buffer);
1257
1258 payload_len = gst_rtp_buffer_get_payload_len (rtp);
1259 payload = gst_rtp_buffer_get_payload (rtp);
1260 marker = gst_rtp_buffer_get_marker (rtp);
1261
1262 GST_DEBUG_OBJECT (rtph265depay, "receiving %d bytes", payload_len);
1263
1264 if (payload_len == 0)
1265 goto empty_packet;
1266
1267 /* +---------------+---------------+
1268 * |0|1|2|3|4|5|6|7|0|1|2|3|4|5|6|7|
1269 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1270 * |F| Type | LayerId | TID |
1271 * +-------------+-----------------+
1272 *
1273 * F must be 0.
1274 *
1275 */
1276 nal_unit_type = (payload[0] >> 1) & 0x3f;
1277 nuh_layer_id = ((payload[0] & 0x01) << 5) | (payload[1] >> 3); /* should be zero for now but this could change in future HEVC extensions */
1278 nuh_temporal_id_plus1 = payload[1] & 0x03;
1279
1280 /* At least two byte header with type */
1281 header_len = 2;
1282
1283 GST_DEBUG_OBJECT (rtph265depay,
1284 "NAL header nal_unit_type %d, nuh_temporal_id_plus1 %d", nal_unit_type,
1285 nuh_temporal_id_plus1);
1286
1287 GST_FIXME_OBJECT (rtph265depay, "Assuming DONL field is not present");
1288
1289 /* FIXME - assuming DONL field is not present for now */
1290 /*donl_present = (tx-mode == "MST") || (sprop-max-don-diff > 0); */
1291
1292 /* If FU unit was being processed, but the current nal is of a different
1293 * type. Assume that the remote payloader is buggy (didn't set the end bit
1294 * when the FU ended) and send out what we gathered thusfar */
1295 if (G_UNLIKELY (rtph265depay->current_fu_type != 0 &&
1296 nal_unit_type != rtph265depay->current_fu_type))
1297 gst_rtp_h265_finish_fragmentation_unit (rtph265depay);
1298
1299 switch (nal_unit_type) {
1300 case 48:
1301 {
1302 GST_DEBUG_OBJECT (rtph265depay, "Processing aggregation packet");
1303
1304 /* Aggregation packet (section 4.7) */
1305
1306 /* An example of an AP packet containing two aggregation units
1307 without the DONL and DOND fields
1308
1309 0 1 2 3
1310 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
1311 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1312 | RTP Header |
1313 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1314 | PayloadHdr (Type=48) | NALU 1 Size |
1315 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1316 | NALU 1 HDR | |
1317 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ NALU 1 Data |
1318 | . . . |
1319 | |
1320 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1321 | . . . | NALU 2 Size | NALU 2 HDR |
1322 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1323 | NALU 2 HDR | |
1324 +-+-+-+-+-+-+-+-+ NALU 2 Data |
1325 | . . . |
1326 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1327 | :...OPTIONAL RTP padding |
1328 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1329 */
1330
1331 /* strip headers */
1332 payload += header_len;
1333 payload_len -= header_len;
1334
1335 rtph265depay->wait_start = FALSE;
1336
1337 #if 0
1338 if (donl_present)
1339 goto not_implemented_donl_present;
1340 #endif
1341
1342 while (payload_len > 2) {
1343 gboolean last = FALSE;
1344
1345 nalu_size = (payload[0] << 8) | payload[1];
1346
1347 /* don't include nalu_size */
1348 if (nalu_size > (payload_len - 2))
1349 nalu_size = payload_len - 2;
1350
1351 outsize = nalu_size + sizeof (sync_bytes);
1352 outbuf = gst_buffer_new_and_alloc (outsize);
1353
1354 gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
1355 if (rtph265depay->byte_stream) {
1356 memcpy (map.data, sync_bytes, sizeof (sync_bytes));
1357 } else {
1358 GST_WRITE_UINT32_BE (map.data, nalu_size);
1359 }
1360
1361 /* strip NALU size */
1362 payload += 2;
1363 payload_len -= 2;
1364
1365 memcpy (map.data + sizeof (sync_bytes), payload, nalu_size);
1366 gst_buffer_unmap (outbuf, &map);
1367
1368 gst_rtp_copy_video_meta (rtph265depay, outbuf, rtp->buffer);
1369
1370 if (payload_len - nalu_size <= 2)
1371 last = TRUE;
1372
1373 gst_rtp_h265_depay_handle_nal (rtph265depay, outbuf, timestamp,
1374 marker && last);
1375
1376 payload += nalu_size;
1377 payload_len -= nalu_size;
1378 }
1379 break;
1380 }
1381 case 49:
1382 {
1383 GST_DEBUG_OBJECT (rtph265depay, "Processing Fragmentation Unit");
1384
1385 /* Fragmentation units (FUs) Section 4.8 */
1386
1387 /* The structure of a Fragmentation Unit (FU)
1388 *
1389 * 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
1390 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1391 | PayloadHdr (Type=49) | FU header | DONL (cond) |
1392 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-|
1393 | DONL (cond) | |
1394 |-+-+-+-+-+-+-+-+ |
1395 | FU payload |
1396 | |
1397 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1398 | :...OPTIONAL RTP padding |
1399 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1400 *
1401 *
1402 */
1403
1404 /* strip headers */
1405 payload += header_len;
1406 payload_len -= header_len;
1407
1408 /* processing FU header */
1409 S = (payload[0] & 0x80) == 0x80;
1410 E = (payload[0] & 0x40) == 0x40;
1411
1412 GST_DEBUG_OBJECT (rtph265depay,
1413 "FU header with S %d, E %d, nal_unit_type %d", S, E,
1414 payload[0] & 0x3f);
1415
1416 if (rtph265depay->wait_start && !S)
1417 goto waiting_start;
1418
1419 #if 0
1420 if (donl_present)
1421 goto not_implemented_donl_present;
1422 #endif
1423
1424 if (S) {
1425
1426 GST_DEBUG_OBJECT (rtph265depay, "Start of Fragmentation Unit");
1427
1428 /* If a new FU unit started, while still processing an older one.
1429 * Assume that the remote payloader is buggy (doesn't set the end
1430 * bit) and send out what we've gathered thusfar */
1431 if (G_UNLIKELY (rtph265depay->current_fu_type != 0))
1432 gst_rtp_h265_finish_fragmentation_unit (rtph265depay);
1433
1434 rtph265depay->current_fu_type = nal_unit_type;
1435 rtph265depay->fu_timestamp = timestamp;
1436 rtph265depay->last_fu_seqnum = gst_rtp_buffer_get_seq (rtp);
1437
1438 rtph265depay->wait_start = FALSE;
1439
1440 /* reconstruct NAL header */
1441 nal_header =
1442 ((payload[0] & 0x3f) << 9) | (nuh_layer_id << 3) |
1443 nuh_temporal_id_plus1;
1444
1445 /* go back one byte so we can copy the payload + two bytes more in the front which
1446 * will be overwritten by the nal_header
1447 */
1448 payload -= 1;
1449 payload_len += 1;
1450
1451 nalu_size = payload_len;
1452 outsize = nalu_size + sizeof (sync_bytes);
1453 outbuf = gst_buffer_new_and_alloc (outsize);
1454
1455 gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
1456 if (rtph265depay->byte_stream) {
1457 GST_WRITE_UINT32_BE (map.data, 0x00000001);
1458 } else {
1459 /* will be fixed up in finish_fragmentation_unit() */
1460 GST_WRITE_UINT32_BE (map.data, 0xffffffff);
1461 }
1462 memcpy (map.data + sizeof (sync_bytes), payload, nalu_size);
1463 map.data[4] = nal_header >> 8;
1464 map.data[5] = nal_header & 0xff;
1465 gst_buffer_unmap (outbuf, &map);
1466
1467 gst_rtp_copy_video_meta (rtph265depay, outbuf, rtp->buffer);
1468
1469 GST_DEBUG_OBJECT (rtph265depay, "queueing %d bytes", outsize);
1470
1471 /* and assemble in the adapter */
1472 gst_adapter_push (rtph265depay->adapter, outbuf);
1473 } else {
1474 if (rtph265depay->current_fu_type == 0) {
1475 /* previous FU packet missing start bit? */
1476 GST_WARNING_OBJECT (rtph265depay, "missing FU start bit on an "
1477 "earlier packet. Dropping.");
1478 gst_adapter_clear (rtph265depay->adapter);
1479 return NULL;
1480 }
1481 if (gst_rtp_buffer_compare_seqnum (rtph265depay->last_fu_seqnum,
1482 gst_rtp_buffer_get_seq (rtp)) != 1) {
1483 /* jump in sequence numbers within an FU is cause for discarding */
1484 GST_WARNING_OBJECT (rtph265depay, "Jump in sequence numbers from "
1485 "%u to %u within Fragmentation Unit. Data was lost, dropping "
1486 "stored.", rtph265depay->last_fu_seqnum,
1487 gst_rtp_buffer_get_seq (rtp));
1488 gst_adapter_clear (rtph265depay->adapter);
1489 return NULL;
1490 }
1491 rtph265depay->last_fu_seqnum = gst_rtp_buffer_get_seq (rtp);
1492
1493 GST_DEBUG_OBJECT (rtph265depay,
1494 "Following part of Fragmentation Unit");
1495
1496 /* strip off FU header byte */
1497 payload += 1;
1498 payload_len -= 1;
1499
1500 outsize = payload_len;
1501 outbuf = gst_buffer_new_and_alloc (outsize);
1502 gst_buffer_fill (outbuf, 0, payload, outsize);
1503
1504 gst_rtp_copy_video_meta (rtph265depay, outbuf, rtp->buffer);
1505
1506 GST_DEBUG_OBJECT (rtph265depay, "queueing %d bytes", outsize);
1507
1508 /* and assemble in the adapter */
1509 gst_adapter_push (rtph265depay->adapter, outbuf);
1510 }
1511
1512 outbuf = NULL;
1513 rtph265depay->fu_marker = marker;
1514
1515 /* if NAL unit ends, flush the adapter */
1516 if (E) {
1517 gst_rtp_h265_finish_fragmentation_unit (rtph265depay);
1518 GST_DEBUG_OBJECT (rtph265depay, "End of Fragmentation Unit");
1519 }
1520 break;
1521 }
1522 case 50:
1523 goto not_implemented; /* PACI packets Section 4.9 */
1524 default:
1525 {
1526 rtph265depay->wait_start = FALSE;
1527
1528 /* All other cases: Single NAL unit packet Section 4.6 */
1529 /* the entire payload is the output buffer */
1530
1531 #if 0
1532 if (donl_present)
1533 goto not_implemented_donl_present;
1534 #endif
1535
1536 nalu_size = payload_len;
1537 outsize = nalu_size + sizeof (sync_bytes);
1538 outbuf = gst_buffer_new_and_alloc (outsize);
1539
1540 gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
1541 if (rtph265depay->byte_stream) {
1542 memcpy (map.data, sync_bytes, sizeof (sync_bytes));
1543 } else {
1544 GST_WRITE_UINT32_BE (map.data, nalu_size);
1545 }
1546 memcpy (map.data + 4, payload, nalu_size);
1547 gst_buffer_unmap (outbuf, &map);
1548
1549 gst_rtp_copy_video_meta (rtph265depay, outbuf, rtp->buffer);
1550
1551 gst_rtp_h265_depay_handle_nal (rtph265depay, outbuf, timestamp, marker);
1552 break;
1553 }
1554 }
1555 }
1556
1557 return NULL;
1558
1559 /* ERRORS */
1560 empty_packet:
1561 {
1562 GST_DEBUG_OBJECT (rtph265depay, "empty packet");
1563 return NULL;
1564 }
1565 waiting_start:
1566 {
1567 GST_DEBUG_OBJECT (rtph265depay, "waiting for start");
1568 return NULL;
1569 }
1570 #if 0
1571 not_implemented_donl_present:
1572 {
1573 GST_ELEMENT_ERROR (rtph265depay, STREAM, FORMAT,
1574 (NULL), ("DONL field present not supported yet"));
1575 return NULL;
1576 }
1577 #endif
1578 not_implemented:
1579 {
1580 GST_ELEMENT_ERROR (rtph265depay, STREAM, FORMAT,
1581 (NULL), ("NAL unit type %d not supported yet", nal_unit_type));
1582 return NULL;
1583 }
1584 }
1585
1586 static gboolean
gst_rtp_h265_depay_handle_event(GstRTPBaseDepayload * depay,GstEvent * event)1587 gst_rtp_h265_depay_handle_event (GstRTPBaseDepayload * depay, GstEvent * event)
1588 {
1589 GstRtpH265Depay *rtph265depay;
1590
1591 rtph265depay = GST_RTP_H265_DEPAY (depay);
1592
1593 switch (GST_EVENT_TYPE (event)) {
1594 case GST_EVENT_FLUSH_STOP:
1595 gst_rtp_h265_depay_reset (rtph265depay, FALSE);
1596 break;
1597 case GST_EVENT_EOS:
1598 gst_rtp_h265_depay_drain (rtph265depay);
1599 break;
1600 default:
1601 break;
1602 }
1603
1604 return
1605 GST_RTP_BASE_DEPAYLOAD_CLASS (parent_class)->handle_event (depay, event);
1606 }
1607
1608 static GstStateChangeReturn
gst_rtp_h265_depay_change_state(GstElement * element,GstStateChange transition)1609 gst_rtp_h265_depay_change_state (GstElement * element,
1610 GstStateChange transition)
1611 {
1612 GstRtpH265Depay *rtph265depay;
1613 GstStateChangeReturn ret;
1614
1615 rtph265depay = GST_RTP_H265_DEPAY (element);
1616
1617 switch (transition) {
1618 case GST_STATE_CHANGE_NULL_TO_READY:
1619 break;
1620 case GST_STATE_CHANGE_READY_TO_PAUSED:
1621 gst_rtp_h265_depay_reset (rtph265depay, TRUE);
1622 break;
1623 default:
1624 break;
1625 }
1626
1627 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1628
1629 switch (transition) {
1630 case GST_STATE_CHANGE_PAUSED_TO_READY:
1631 gst_rtp_h265_depay_reset (rtph265depay, TRUE);
1632 break;
1633 case GST_STATE_CHANGE_READY_TO_NULL:
1634 break;
1635 default:
1636 break;
1637 }
1638 return ret;
1639 }
1640