2 * Copyright (C) 2015 Intel Corporation
3 * Author: Sreerenj Balachandran <sreerenj.balachandran@intel.com>
4 * Copyright (C) 2019 Seungha Yang <seungha.yang@navercorp.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
22 * SECTION:gsth265decoder
23 * @title: GstH265Decoder
24 * @short_description: Base class to implement stateless H.265 decoders
33 #include <gst/base/base.h>
34 #include "gsth265decoder.h"
36 GST_DEBUG_CATEGORY (gst_h265_decoder_debug);
37 #define GST_CAT_DEFAULT gst_h265_decoder_debug
41 GST_H265_DECODER_FORMAT_NONE,
42 GST_H265_DECODER_FORMAT_HVC1,
43 GST_H265_DECODER_FORMAT_HEV1,
44 GST_H265_DECODER_FORMAT_BYTE
45 } GstH265DecoderFormat;
49 GST_H265_DECODER_ALIGN_NONE,
50 GST_H265_DECODER_ALIGN_NAL,
51 GST_H265_DECODER_ALIGN_AU
52 } GstH265DecoderAlign;
54 struct _GstH265DecoderPrivate
58 guint8 conformance_window_flag;
60 gint crop_rect_height;
64 guint nal_length_size;
67 GstH265DecoderFormat in_format;
68 GstH265DecoderAlign align;
69 GstH265Parser *parser;
72 /* 0: frame or field-pair interlaced stream
73 * 1: alternating, single field interlaced stream.
74 * When equal to 1, picture timing SEI shall be present in every AU */
75 guint8 field_seq_flag;
76 guint8 progressive_source_flag;
77 guint8 interlaced_source_flag;
79 /* Updated/cleared per handle_frame() by using picture timeing SEI */
80 GstH265SEIPicStructType cur_pic_struct;
81 guint8 cur_source_scan_type;
82 guint8 cur_duplicate_flag;
84 gboolean no_output_of_prior_pics_flag;
86 /* vps/sps/pps of the current slice */
87 const GstH265VPS *active_vps;
88 const GstH265SPS *active_sps;
89 const GstH265PPS *active_pps;
91 guint32 SpsMaxLatencyPictures;
93 /* Picture currently being processed/decoded */
94 GstH265Picture *current_picture;
95 GstVideoCodecFrame *current_frame;
97 /* Slice (slice header + nalu) currently being processed/decoded */
98 GstH265Slice current_slice;
99 GstH265Slice prev_slice;
100 GstH265Slice prev_independent_slice;
102 gint32 poc; // PicOrderCntVal
103 gint32 poc_msb; // PicOrderCntMsb
104 gint32 poc_lsb; // pic_order_cnt_lsb (from slice_header())
105 gint32 prev_poc_msb; // prevPicOrderCntMsb
106 gint32 prev_poc_lsb; // prevPicOrderCntLsb
107 gint32 prev_tid0pic_poc_lsb;
108 gint32 prev_tid0pic_poc_msb;
109 gint32 PocStCurrBefore[16];
110 gint32 PocStCurrAfter[16];
111 gint32 PocStFoll[16];
112 gint32 PocLtCurr[16];
113 gint32 PocLtFoll[16];
115 /* PicOrderCount of the previously outputted frame */
116 gint last_output_poc;
118 gboolean associated_irap_NoRaslOutputFlag;
119 gboolean new_bitstream;
120 gboolean prev_nal_is_eos;
122 /* Reference picture lists, constructed for each slice */
123 gboolean process_ref_pic_lists;
124 GArray *ref_pic_list_tmp;
125 GArray *ref_pic_list0;
126 GArray *ref_pic_list1;
130 /* Split packetized data into actual nal chunks (for malformed stream) */
133 /* For delayed output */
134 guint preferred_output_delay;
136 GstQueueArray *output_queue;
138 gboolean input_state_changed;
149 } GstH265DecoderNalUnit;
154 GstVideoCodecFrame *frame;
155 GstH265Picture *picture;
157 GstH265Decoder *self;
158 } GstH265DecoderOutputFrame;
160 #define UPDATE_FLOW_RETURN(ret,new_ret) G_STMT_START { \
161 if (*(ret) == GST_FLOW_OK) \
165 #define parent_class gst_h265_decoder_parent_class
166 G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GstH265Decoder, gst_h265_decoder,
167 GST_TYPE_VIDEO_DECODER,
168 G_ADD_PRIVATE (GstH265Decoder);
169 GST_DEBUG_CATEGORY_INIT (gst_h265_decoder_debug, "h265decoder", 0,
170 "H.265 Video Decoder"));
172 static void gst_h265_decoder_finalize (GObject * object);
174 static gboolean gst_h265_decoder_start (GstVideoDecoder * decoder);
175 static gboolean gst_h265_decoder_stop (GstVideoDecoder * decoder);
176 static gboolean gst_h265_decoder_set_format (GstVideoDecoder * decoder,
177 GstVideoCodecState * state);
178 static gboolean gst_h265_decoder_negotiate (GstVideoDecoder * decoder);
179 static GstFlowReturn gst_h265_decoder_finish (GstVideoDecoder * decoder);
180 static gboolean gst_h265_decoder_flush (GstVideoDecoder * decoder);
181 static GstFlowReturn gst_h265_decoder_drain (GstVideoDecoder * decoder);
182 static GstFlowReturn gst_h265_decoder_handle_frame (GstVideoDecoder * decoder,
183 GstVideoCodecFrame * frame);
185 static void gst_h265_decoder_finish_current_picture (GstH265Decoder * self,
186 GstFlowReturn * ret);
187 static void gst_h265_decoder_clear_ref_pic_sets (GstH265Decoder * self);
188 static void gst_h265_decoder_clear_dpb (GstH265Decoder * self, gboolean flush);
189 static GstFlowReturn gst_h265_decoder_drain_internal (GstH265Decoder * self);
191 gst_h265_decoder_start_current_picture (GstH265Decoder * self);
192 static void gst_h265_decoder_clear_nalu (GstH265DecoderNalUnit * nalu);
194 gst_h265_decoder_clear_output_frame (GstH265DecoderOutputFrame * output_frame);
197 gst_h265_decoder_class_init (GstH265DecoderClass * klass)
199 GstVideoDecoderClass *decoder_class = GST_VIDEO_DECODER_CLASS (klass);
200 GObjectClass *object_class = G_OBJECT_CLASS (klass);
202 object_class->finalize = GST_DEBUG_FUNCPTR (gst_h265_decoder_finalize);
204 decoder_class->start = GST_DEBUG_FUNCPTR (gst_h265_decoder_start);
205 decoder_class->stop = GST_DEBUG_FUNCPTR (gst_h265_decoder_stop);
206 decoder_class->set_format = GST_DEBUG_FUNCPTR (gst_h265_decoder_set_format);
207 decoder_class->negotiate = GST_DEBUG_FUNCPTR (gst_h265_decoder_negotiate);
208 decoder_class->finish = GST_DEBUG_FUNCPTR (gst_h265_decoder_finish);
209 decoder_class->flush = GST_DEBUG_FUNCPTR (gst_h265_decoder_flush);
210 decoder_class->drain = GST_DEBUG_FUNCPTR (gst_h265_decoder_drain);
211 decoder_class->handle_frame =
212 GST_DEBUG_FUNCPTR (gst_h265_decoder_handle_frame);
216 gst_h265_decoder_init (GstH265Decoder * self)
218 GstH265DecoderPrivate *priv;
220 gst_video_decoder_set_packetized (GST_VIDEO_DECODER (self), TRUE);
221 gst_video_decoder_set_needs_format (GST_VIDEO_DECODER (self), TRUE);
223 self->priv = priv = gst_h265_decoder_get_instance_private (self);
225 priv->last_output_poc = G_MININT32;
227 priv->ref_pic_list_tmp = g_array_sized_new (FALSE, TRUE,
228 sizeof (GstH265Picture *), 32);
229 priv->ref_pic_list0 = g_array_sized_new (FALSE, TRUE,
230 sizeof (GstH265Picture *), 32);
231 priv->ref_pic_list1 = g_array_sized_new (FALSE, TRUE,
232 sizeof (GstH265Picture *), 32);
233 priv->nalu = g_array_sized_new (FALSE, TRUE, sizeof (GstH265DecoderNalUnit),
235 priv->split_nalu = g_array_new (FALSE, FALSE, sizeof (GstH265NalUnit));
236 g_array_set_clear_func (priv->nalu,
237 (GDestroyNotify) gst_h265_decoder_clear_nalu);
239 gst_queue_array_new_for_struct (sizeof (GstH265DecoderOutputFrame), 1);
240 gst_queue_array_set_clear_func (priv->output_queue,
241 (GDestroyNotify) gst_h265_decoder_clear_output_frame);
245 gst_h265_decoder_finalize (GObject * object)
247 GstH265Decoder *self = GST_H265_DECODER (object);
248 GstH265DecoderPrivate *priv = self->priv;
250 g_array_unref (priv->ref_pic_list_tmp);
251 g_array_unref (priv->ref_pic_list0);
252 g_array_unref (priv->ref_pic_list1);
253 g_array_unref (priv->nalu);
254 g_array_unref (priv->split_nalu);
255 gst_queue_array_free (priv->output_queue);
257 G_OBJECT_CLASS (parent_class)->finalize (object);
261 gst_h265_decoder_start (GstVideoDecoder * decoder)
263 GstH265Decoder *self = GST_H265_DECODER (decoder);
264 GstH265DecoderPrivate *priv = self->priv;
266 priv->parser = gst_h265_parser_new ();
267 priv->dpb = gst_h265_dpb_new ();
268 priv->new_bitstream = TRUE;
269 priv->prev_nal_is_eos = FALSE;
275 gst_h265_decoder_stop (GstVideoDecoder * decoder)
277 GstH265Decoder *self = GST_H265_DECODER (decoder);
278 GstH265DecoderPrivate *priv = self->priv;
280 if (self->input_state) {
281 gst_video_codec_state_unref (self->input_state);
282 self->input_state = NULL;
286 gst_h265_parser_free (priv->parser);
291 gst_h265_dpb_free (priv->dpb);
295 gst_h265_decoder_clear_ref_pic_sets (self);
301 gst_h265_decoder_clear_output_frame (GstH265DecoderOutputFrame * output_frame)
306 if (output_frame->frame) {
307 gst_video_decoder_release_frame (GST_VIDEO_DECODER (output_frame->self),
308 output_frame->frame);
309 output_frame->frame = NULL;
312 gst_clear_h265_picture (&output_frame->picture);
316 gst_h265_decoder_is_crop_rect_changed (GstH265Decoder * self, GstH265SPS * sps)
318 GstH265DecoderPrivate *priv = self->priv;
320 if (priv->conformance_window_flag != sps->conformance_window_flag)
322 if (priv->crop_rect_width != sps->crop_rect_width)
324 if (priv->crop_rect_height != sps->crop_rect_height)
326 if (priv->crop_rect_x != sps->crop_rect_x)
328 if (priv->crop_rect_y != sps->crop_rect_y)
335 gst_h265_decoder_drain_output_queue (GstH265Decoder * self, guint num,
338 GstH265DecoderPrivate *priv = self->priv;
339 GstH265DecoderClass *klass = GST_H265_DECODER_GET_CLASS (self);
341 g_assert (klass->output_picture);
342 g_assert (ret != NULL);
344 while (gst_queue_array_get_length (priv->output_queue) > num) {
345 GstH265DecoderOutputFrame *output_frame = (GstH265DecoderOutputFrame *)
346 gst_queue_array_pop_head_struct (priv->output_queue);
347 GstFlowReturn flow_ret = klass->output_picture (self, output_frame->frame,
348 output_frame->picture);
350 UPDATE_FLOW_RETURN (ret, flow_ret);
355 gst_h265_decoder_set_latency (GstH265Decoder * self, const GstH265SPS * sps,
358 GstH265DecoderPrivate *priv = self->priv;
360 GstClockTime min, max;
361 GstStructure *structure;
362 gint fps_d = 1, fps_n = 0;
365 caps = gst_pad_get_current_caps (GST_VIDEO_DECODER_SRC_PAD (self));
366 if (!caps && self->input_state)
367 caps = gst_caps_ref (self->input_state->caps);
370 structure = gst_caps_get_structure (caps, 0);
371 if (gst_structure_get_fraction (structure, "framerate", &fps_n, &fps_d)) {
373 /* variable framerate: see if we have a max-framerate */
374 gst_structure_get_fraction (structure, "max-framerate", &fps_n, &fps_d);
377 gst_caps_unref (caps);
380 /* if no fps or variable, then 25/1 */
386 /* Minimum possible latency could be calculated based on C.5.2.3
387 * 1) # of pictures (marked as "needed for output") in DPB > sps_max_num_reorder_pics
388 * - We will assume all pictures in DPB are marked as "needed for output"
389 * 2) sps_max_latency_increase_plus1 != 0 and
390 * PicLatencyCount >= SpsMaxLatencyPictures
391 * - SpsMaxLatencyPictures is equal to
392 * "sps_max_num_reorder_pics + sps_max_latency_increase_plus1 - 1"
393 * and PicLatencyCount of each picture in DPB is increased by 1 per
394 * decoding loop. Note that PicLatencyCount of the currently decoded
395 * picture is zero. So, in case that all pictures in DPB are marked as
396 * "needed for output", Only condition 1) will have an effect
397 * regardless of sps_max_latency_increase_plus1.
399 * For example, assume sps_max_num_reorder_pics is 2 and
400 * sps_max_latency_increase_plus1 is 1, then SpsMaxLatencyPictures is 2.
401 * For a picture in DPB to have PicLatencyCount >= SpsMaxLatencyPictures,
402 * there must be at least 3 pictures including current picture in DPB
403 * (current picture's PicLatencyCount is zero).
404 * This is already covered by the condition 1). So, this condition 2)
405 * will have effect only when there are pictures marked as
406 * "not needed for output" in DPB.
408 * Thus, we can take sps_max_num_reorder_pics as a min latency value
410 frames_delay = sps->max_num_reorder_pics[sps->max_sub_layers_minus1];
412 /* Consider output delay wanted by subclass */
413 frames_delay += priv->preferred_output_delay;
415 min = gst_util_uint64_scale_int (frames_delay * GST_SECOND, fps_d, fps_n);
416 max = gst_util_uint64_scale_int ((max_dpb_size + priv->preferred_output_delay)
417 * GST_SECOND, fps_d, fps_n);
419 GST_DEBUG_OBJECT (self,
420 "latency min %" GST_TIME_FORMAT " max %" GST_TIME_FORMAT
421 " min-frames-delay %d", GST_TIME_ARGS (min), GST_TIME_ARGS (max),
424 gst_video_decoder_set_latency (GST_VIDEO_DECODER (self), min, max);
428 gst_h265_decoder_process_sps (GstH265Decoder * self, GstH265SPS * sps)
430 GstH265DecoderPrivate *priv = self->priv;
432 gint prev_max_dpb_size;
434 const gint MaxDpbPicBuf = 6;
435 gint PicSizeInSamplesY;
436 guint8 field_seq_flag = 0;
437 guint8 progressive_source_flag = 0;
438 guint8 interlaced_source_flag = 0;
439 GstFlowReturn ret = GST_FLOW_OK;
442 MaxLumaPS = 35651584;
443 PicSizeInSamplesY = sps->width * sps->height;
444 if (PicSizeInSamplesY <= (MaxLumaPS >> 2))
445 max_dpb_size = MaxDpbPicBuf * 4;
446 else if (PicSizeInSamplesY <= (MaxLumaPS >> 1))
447 max_dpb_size = MaxDpbPicBuf * 2;
448 else if (PicSizeInSamplesY <= ((3 * MaxLumaPS) >> 2))
449 max_dpb_size = (MaxDpbPicBuf * 4) / 3;
451 max_dpb_size = MaxDpbPicBuf;
453 max_dpb_size = MIN (max_dpb_size, 16);
455 if (sps->vui_parameters_present_flag)
456 field_seq_flag = sps->vui_params.field_seq_flag;
458 progressive_source_flag = sps->profile_tier_level.progressive_source_flag;
459 interlaced_source_flag = sps->profile_tier_level.interlaced_source_flag;
461 prev_max_dpb_size = gst_h265_dpb_get_max_num_pics (priv->dpb);
462 if (priv->width != sps->width || priv->height != sps->height ||
463 prev_max_dpb_size != max_dpb_size ||
464 priv->field_seq_flag != field_seq_flag ||
465 priv->progressive_source_flag != progressive_source_flag ||
466 priv->interlaced_source_flag != interlaced_source_flag ||
467 gst_h265_decoder_is_crop_rect_changed (self, sps)) {
468 GstH265DecoderClass *klass = GST_H265_DECODER_GET_CLASS (self);
470 GST_DEBUG_OBJECT (self,
471 "SPS updated, resolution: %dx%d -> %dx%d, dpb size: %d -> %d, "
472 "field_seq_flag: %d -> %d, progressive_source_flag: %d -> %d, "
473 "interlaced_source_flag: %d -> %d",
474 priv->width, priv->height, sps->width, sps->height,
475 prev_max_dpb_size, max_dpb_size, priv->field_seq_flag, field_seq_flag,
476 priv->progressive_source_flag, progressive_source_flag,
477 priv->interlaced_source_flag, interlaced_source_flag);
479 if (priv->no_output_of_prior_pics_flag) {
480 gst_h265_decoder_drain_output_queue (self, 0, &ret);
481 gst_h265_decoder_clear_dpb (self, FALSE);
483 ret = gst_h265_decoder_drain_internal (self);
486 if (ret != GST_FLOW_OK)
489 if (klass->get_preferred_output_delay) {
490 priv->preferred_output_delay =
491 klass->get_preferred_output_delay (self, priv->is_live);
493 priv->preferred_output_delay = 0;
496 g_assert (klass->new_sequence);
497 ret = klass->new_sequence (self,
498 sps, max_dpb_size + priv->preferred_output_delay);
499 if (ret != GST_FLOW_OK) {
500 GST_WARNING_OBJECT (self, "subclass does not want accept new sequence");
504 priv->width = sps->width;
505 priv->height = sps->height;
506 priv->conformance_window_flag = sps->conformance_window_flag;
507 priv->crop_rect_width = sps->crop_rect_width;
508 priv->crop_rect_height = sps->crop_rect_height;
509 priv->crop_rect_x = sps->crop_rect_x;
510 priv->crop_rect_y = sps->crop_rect_y;
511 priv->field_seq_flag = field_seq_flag;
512 priv->progressive_source_flag = progressive_source_flag;
513 priv->interlaced_source_flag = interlaced_source_flag;
515 gst_h265_dpb_set_max_num_pics (priv->dpb, max_dpb_size);
516 gst_h265_decoder_set_latency (self, sps, max_dpb_size);
518 GST_DEBUG_OBJECT (self, "Set DPB max size %d", max_dpb_size);
521 if (sps->max_latency_increase_plus1[sps->max_sub_layers_minus1]) {
522 priv->SpsMaxLatencyPictures =
523 sps->max_num_reorder_pics[sps->max_sub_layers_minus1] +
524 sps->max_latency_increase_plus1[sps->max_sub_layers_minus1] - 1;
526 priv->SpsMaxLatencyPictures = 0;
532 static GstH265ParserResult
533 gst_h265_decoder_parse_sei (GstH265Decoder * self, GstH265NalUnit * nalu)
535 GstH265DecoderPrivate *priv = self->priv;
536 GstH265ParserResult pres;
537 GArray *messages = NULL;
540 pres = gst_h265_parser_parse_sei (priv->parser, nalu, &messages);
541 if (pres != GST_H265_PARSER_OK) {
542 GST_WARNING_OBJECT (self, "Failed to parse SEI, result %d", pres);
544 /* XXX: Ignore error from SEI parsing, it might be malformed bitstream,
545 * or our fault. But shouldn't be critical */
546 g_clear_pointer (&messages, g_array_unref);
547 return GST_H265_PARSER_OK;
550 for (i = 0; i < messages->len; i++) {
551 GstH265SEIMessage *sei = &g_array_index (messages, GstH265SEIMessage, i);
553 switch (sei->payloadType) {
554 case GST_H265_SEI_PIC_TIMING:
555 priv->cur_pic_struct = sei->payload.pic_timing.pic_struct;
556 priv->cur_source_scan_type = sei->payload.pic_timing.source_scan_type;
557 priv->cur_duplicate_flag = sei->payload.pic_timing.duplicate_flag;
559 GST_TRACE_OBJECT (self,
560 "Picture Timing SEI, pic_struct: %d, source_scan_type: %d, "
561 "duplicate_flag: %d", priv->cur_pic_struct,
562 priv->cur_source_scan_type, priv->cur_duplicate_flag);
569 g_array_free (messages, TRUE);
570 GST_LOG_OBJECT (self, "SEI parsed");
572 return GST_H265_PARSER_OK;
576 gst_h265_decoder_process_ref_pic_lists (GstH265Decoder * self,
577 GstH265Picture * curr_pic, GstH265Slice * slice,
578 GArray ** ref_pic_list0, GArray ** ref_pic_list1)
580 GstH265DecoderPrivate *priv = self->priv;
581 GstH265RefPicListModification *ref_mod =
582 &slice->header.ref_pic_list_modification;
583 GstH265PPSSccExtensionParams *scc_ext =
584 &slice->header.pps->pps_scc_extension_params;
586 gint num_tmp_refs, i;
588 *ref_pic_list0 = priv->ref_pic_list0;
589 *ref_pic_list1 = priv->ref_pic_list1;
591 /* There is nothing to be done for I slices */
592 if (GST_H265_IS_I_SLICE (&slice->header))
595 /* Infinite loop prevention */
596 if (self->NumPocStCurrBefore == 0 && self->NumPocStCurrAfter == 0 &&
597 self->NumPocLtCurr == 0 && !scc_ext->pps_curr_pic_ref_enabled_flag) {
598 GST_WARNING_OBJECT (self,
599 "Expected references, got none, preventing infinite loop.");
603 /* 8.3.4 Deriving l0 */
604 tmp_refs = priv->ref_pic_list_tmp;
607 * Deriving l0 consists of appending in loop RefPicSetStCurrBefore,
608 * RefPicSetStCurrAfter and RefPicSetLtCurr until NumRpsCurrTempList0 item
612 /* NumRpsCurrTempList0 */
613 num_tmp_refs = MAX (slice->header.num_ref_idx_l0_active_minus1 + 1,
614 self->NumPicTotalCurr);
616 while (tmp_refs->len < num_tmp_refs) {
617 for (i = 0; i < self->NumPocStCurrBefore && tmp_refs->len < num_tmp_refs;
619 g_array_append_val (tmp_refs, self->RefPicSetStCurrBefore[i]);
620 for (i = 0; i < self->NumPocStCurrAfter && tmp_refs->len < num_tmp_refs;
622 g_array_append_val (tmp_refs, self->RefPicSetStCurrAfter[i]);
623 for (i = 0; i < self->NumPocLtCurr && tmp_refs->len < num_tmp_refs; i++)
624 g_array_append_val (tmp_refs, self->RefPicSetLtCurr[i]);
625 if (scc_ext->pps_curr_pic_ref_enabled_flag)
626 g_array_append_val (tmp_refs, curr_pic);
630 * If needed, apply the modification based on the lookup table found in the
631 * slice header (list_entry_l0).
633 for (i = 0; i <= slice->header.num_ref_idx_l0_active_minus1; i++) {
634 GstH265Picture **tmp = (GstH265Picture **) tmp_refs->data;
636 if (ref_mod->ref_pic_list_modification_flag_l0)
637 g_array_append_val (*ref_pic_list0, tmp[ref_mod->list_entry_l0[i]]);
639 g_array_append_val (*ref_pic_list0, tmp[i]);
642 if (scc_ext->pps_curr_pic_ref_enabled_flag &&
643 !ref_mod->ref_pic_list_modification_flag_l0 &&
644 num_tmp_refs > (slice->header.num_ref_idx_l0_active_minus1 + 1)) {
645 g_array_index (*ref_pic_list0, GstH265Picture *,
646 slice->header.num_ref_idx_l0_active_minus1) = curr_pic;
649 g_array_set_size (tmp_refs, 0);
651 /* For P slices we only need l0 */
652 if (GST_H265_IS_P_SLICE (&slice->header))
655 /* 8.3.4 Deriving l1 */
657 * Deriving l1 consists of appending in loop RefPicSetStCurrAfter,
658 * RefPicSetStCurrBefore and RefPicSetLtCurr until NumRpsCurrTempList1 items
662 /* NumRpsCurrTempList1 */
663 num_tmp_refs = MAX (slice->header.num_ref_idx_l1_active_minus1 + 1,
664 self->NumPicTotalCurr);
666 while (tmp_refs->len < num_tmp_refs) {
667 for (i = 0; i < self->NumPocStCurrAfter && tmp_refs->len < num_tmp_refs;
669 g_array_append_val (tmp_refs, self->RefPicSetStCurrAfter[i]);
670 for (i = 0; i < self->NumPocStCurrBefore && tmp_refs->len < num_tmp_refs;
672 g_array_append_val (tmp_refs, self->RefPicSetStCurrBefore[i]);
673 for (i = 0; i < self->NumPocLtCurr && tmp_refs->len < num_tmp_refs; i++)
674 g_array_append_val (tmp_refs, self->RefPicSetLtCurr[i]);
675 if (scc_ext->pps_curr_pic_ref_enabled_flag)
676 g_array_append_val (tmp_refs, curr_pic);
680 * If needed, apply the modification based on the lookup table found in the
681 * slice header (list_entry_l1).
683 for (i = 0; i <= slice->header.num_ref_idx_l1_active_minus1; i++) {
684 GstH265Picture **tmp = (GstH265Picture **) tmp_refs->data;
686 if (ref_mod->ref_pic_list_modification_flag_l1)
687 g_array_append_val (*ref_pic_list1, tmp[ref_mod->list_entry_l1[i]]);
689 g_array_append_val (*ref_pic_list1, tmp[i]);
692 g_array_set_size (tmp_refs, 0);
696 gst_h265_decoder_decode_slice (GstH265Decoder * self)
698 GstH265DecoderClass *klass = GST_H265_DECODER_GET_CLASS (self);
699 GstH265DecoderPrivate *priv = self->priv;
700 GstH265Slice *slice = &priv->current_slice;
701 GstH265Picture *picture = priv->current_picture;
704 GstFlowReturn ret = GST_FLOW_OK;
707 GST_ERROR_OBJECT (self, "No current picture");
708 return GST_FLOW_ERROR;
711 g_assert (klass->decode_slice);
713 if (priv->process_ref_pic_lists) {
714 l0 = priv->ref_pic_list0;
715 l1 = priv->ref_pic_list1;
716 gst_h265_decoder_process_ref_pic_lists (self, picture, slice, &l0, &l1);
719 ret = klass->decode_slice (self, picture, slice, l0, l1);
721 if (priv->process_ref_pic_lists) {
722 g_array_set_size (l0, 0);
723 g_array_set_size (l1, 0);
730 gst_h265_decoder_preprocess_slice (GstH265Decoder * self, GstH265Slice * slice)
732 GstH265DecoderPrivate *priv = self->priv;
733 const GstH265SliceHdr *slice_hdr = &slice->header;
735 if (priv->current_picture && slice_hdr->first_slice_segment_in_pic_flag) {
736 GST_WARNING_OBJECT (self,
737 "Current picture is not finished but slice header has "
738 "first_slice_segment_in_pic_flag");
739 return GST_FLOW_ERROR;
746 gst_h265_decoder_process_slice (GstH265Decoder * self, GstH265Slice * slice)
748 GstH265DecoderPrivate *priv = self->priv;
749 GstFlowReturn ret = GST_FLOW_OK;
751 priv->current_slice = *slice;
753 if (priv->current_slice.header.dependent_slice_segment_flag) {
754 GstH265SliceHdr *slice_hdr = &priv->current_slice.header;
755 GstH265SliceHdr *indep_slice_hdr = &priv->prev_independent_slice.header;
757 memcpy (&slice_hdr->type, &indep_slice_hdr->type,
758 G_STRUCT_OFFSET (GstH265SliceHdr, num_entry_point_offsets) -
759 G_STRUCT_OFFSET (GstH265SliceHdr, type));
761 priv->prev_independent_slice = priv->current_slice;
762 memset (&priv->prev_independent_slice.nalu, 0, sizeof (GstH265NalUnit));
765 ret = gst_h265_decoder_preprocess_slice (self, &priv->current_slice);
766 if (ret != GST_FLOW_OK)
769 /* The used SPS may not be the latest parsed one, make
770 * sure we have updated it before decode the frame */
771 ret = gst_h265_decoder_process_sps (self,
772 priv->current_slice.header.pps->sps);
773 if (ret != GST_FLOW_OK) {
774 GST_WARNING_OBJECT (self, "Failed to process sps");
778 priv->active_pps = priv->current_slice.header.pps;
779 priv->active_sps = priv->active_pps->sps;
781 if (!priv->current_picture) {
782 GstH265DecoderClass *klass = GST_H265_DECODER_GET_CLASS (self);
783 GstH265Picture *picture;
784 GstFlowReturn ret = GST_FLOW_OK;
786 g_assert (priv->current_frame);
788 picture = gst_h265_picture_new ();
789 /* This allows accessing the frame from the picture. */
790 picture->system_frame_number = priv->current_frame->system_frame_number;
792 priv->current_picture = picture;
794 if (klass->new_picture)
795 ret = klass->new_picture (self, priv->current_frame, picture);
797 if (ret != GST_FLOW_OK) {
798 GST_WARNING_OBJECT (self, "subclass does not want accept new picture");
799 priv->current_picture = NULL;
800 gst_h265_picture_unref (picture);
804 ret = gst_h265_decoder_start_current_picture (self);
805 if (ret != GST_FLOW_OK) {
806 GST_WARNING_OBJECT (self, "start picture failed");
810 /* this picture was dropped */
811 if (!priv->current_picture)
815 return gst_h265_decoder_decode_slice (self);
818 static GstH265ParserResult
819 gst_h265_decoder_parse_slice (GstH265Decoder * self, GstH265NalUnit * nalu)
821 GstH265DecoderPrivate *priv = self->priv;
822 GstH265ParserResult pres;
824 GstH265DecoderNalUnit decoder_nalu;
826 memset (&slice, 0, sizeof (GstH265Slice));
828 pres = gst_h265_parser_parse_slice_hdr (priv->parser, nalu, &slice.header);
829 if (pres != GST_H265_PARSER_OK)
832 /* NOTE: gst_h265_parser_parse_slice_hdr() allocates array
833 * GstH265SliceHdr::entry_point_offset_minus1 but we don't use it
834 * in this h265decoder baseclass at the moment
836 gst_h265_slice_hdr_free (&slice.header);
839 if (nalu->type >= GST_H265_NAL_SLICE_BLA_W_LP &&
840 nalu->type <= GST_H265_NAL_SLICE_CRA_NUT) {
841 slice.rap_pic_flag = TRUE;
844 /* NoRaslOutputFlag == 1 if the current picture is
847 * 3) a CRA picture that is the first access unit in the bitstream
848 * 4) first picture that follows an end of sequence NAL unit in decoding order
849 * 5) has HandleCraAsBlaFlag == 1 (set by external means, so not considering )
851 if (GST_H265_IS_NAL_TYPE_IDR (nalu->type) ||
852 GST_H265_IS_NAL_TYPE_BLA (nalu->type) ||
853 (GST_H265_IS_NAL_TYPE_CRA (nalu->type) && priv->new_bitstream) ||
854 priv->prev_nal_is_eos) {
855 slice.no_rasl_output_flag = TRUE;
858 if (GST_H265_IS_NAL_TYPE_IRAP (nalu->type)) {
859 slice.intra_pic_flag = TRUE;
861 if (slice.no_rasl_output_flag && !priv->new_bitstream) {
863 slice.clear_dpb = TRUE;
864 if (nalu->type == GST_H265_NAL_SLICE_CRA_NUT) {
865 slice.no_output_of_prior_pics_flag = TRUE;
867 slice.no_output_of_prior_pics_flag =
868 slice.header.no_output_of_prior_pics_flag;
873 if (slice.no_output_of_prior_pics_flag)
874 priv->no_output_of_prior_pics_flag = TRUE;
876 decoder_nalu.unit.slice = slice;
877 decoder_nalu.is_slice = TRUE;
878 g_array_append_val (priv->nalu, decoder_nalu);
880 return GST_H265_PARSER_OK;
883 static GstH265ParserResult
884 gst_h265_decoder_parse_nalu (GstH265Decoder * self, GstH265NalUnit * nalu)
886 GstH265DecoderPrivate *priv = self->priv;
890 GstH265ParserResult ret = GST_H265_PARSER_OK;
891 GstH265DecoderNalUnit decoder_nalu;
893 GST_LOG_OBJECT (self, "Parsed nal type: %d, offset %d, size %d",
894 nalu->type, nalu->offset, nalu->size);
896 switch (nalu->type) {
897 case GST_H265_NAL_VPS:
898 ret = gst_h265_parser_parse_vps (priv->parser, nalu, &vps);
900 case GST_H265_NAL_SPS:
901 ret = gst_h265_parser_parse_sps (priv->parser, nalu, &sps, TRUE);
902 if (ret != GST_H265_PARSER_OK)
905 memset (&decoder_nalu, 0, sizeof (GstH265DecoderNalUnit));
906 decoder_nalu.unit.sps = sps;
907 g_array_append_val (priv->nalu, decoder_nalu);
909 case GST_H265_NAL_PPS:
910 ret = gst_h265_parser_parse_pps (priv->parser, nalu, &pps);
912 case GST_H265_NAL_PREFIX_SEI:
913 case GST_H265_NAL_SUFFIX_SEI:
914 ret = gst_h265_decoder_parse_sei (self, nalu);
916 case GST_H265_NAL_SLICE_TRAIL_N:
917 case GST_H265_NAL_SLICE_TRAIL_R:
918 case GST_H265_NAL_SLICE_TSA_N:
919 case GST_H265_NAL_SLICE_TSA_R:
920 case GST_H265_NAL_SLICE_STSA_N:
921 case GST_H265_NAL_SLICE_STSA_R:
922 case GST_H265_NAL_SLICE_RADL_N:
923 case GST_H265_NAL_SLICE_RADL_R:
924 case GST_H265_NAL_SLICE_RASL_N:
925 case GST_H265_NAL_SLICE_RASL_R:
926 case GST_H265_NAL_SLICE_BLA_W_LP:
927 case GST_H265_NAL_SLICE_BLA_W_RADL:
928 case GST_H265_NAL_SLICE_BLA_N_LP:
929 case GST_H265_NAL_SLICE_IDR_W_RADL:
930 case GST_H265_NAL_SLICE_IDR_N_LP:
931 case GST_H265_NAL_SLICE_CRA_NUT:
932 ret = gst_h265_decoder_parse_slice (self, nalu);
933 priv->new_bitstream = FALSE;
934 priv->prev_nal_is_eos = FALSE;
936 case GST_H265_NAL_EOB:
937 priv->new_bitstream = TRUE;
939 case GST_H265_NAL_EOS:
940 priv->prev_nal_is_eos = TRUE;
950 gst_h265_decoder_decode_nalu (GstH265Decoder * self,
951 GstH265DecoderNalUnit * nalu)
954 return gst_h265_decoder_process_slice (self, &nalu->unit.slice);
960 gst_h265_decoder_format_from_caps (GstH265Decoder * self, GstCaps * caps,
961 GstH265DecoderFormat * format, GstH265DecoderAlign * align)
964 *format = GST_H265_DECODER_FORMAT_NONE;
967 *align = GST_H265_DECODER_ALIGN_NONE;
969 if (!gst_caps_is_fixed (caps)) {
970 GST_WARNING_OBJECT (self, "Caps wasn't fixed");
974 GST_DEBUG_OBJECT (self, "parsing caps: %" GST_PTR_FORMAT, caps);
976 if (caps && gst_caps_get_size (caps) > 0) {
977 GstStructure *s = gst_caps_get_structure (caps, 0);
978 const gchar *str = NULL;
981 if ((str = gst_structure_get_string (s, "stream-format"))) {
982 if (strcmp (str, "hvc1") == 0)
983 *format = GST_H265_DECODER_FORMAT_HVC1;
984 else if (strcmp (str, "hev1") == 0)
985 *format = GST_H265_DECODER_FORMAT_HEV1;
986 else if (strcmp (str, "byte-stream") == 0)
987 *format = GST_H265_DECODER_FORMAT_BYTE;
992 if ((str = gst_structure_get_string (s, "alignment"))) {
993 if (strcmp (str, "au") == 0)
994 *align = GST_H265_DECODER_ALIGN_AU;
995 else if (strcmp (str, "nal") == 0)
996 *align = GST_H265_DECODER_ALIGN_NAL;
1002 static GstFlowReturn
1003 gst_h265_decoder_parse_codec_data (GstH265Decoder * self, const guint8 * data,
1006 GstH265DecoderPrivate *priv = self->priv;
1007 guint num_nal_arrays;
1009 guint num_nals, i, j;
1010 GstH265ParserResult pres;
1011 GstH265NalUnit nalu;
1016 /* parse the hvcC data */
1018 GST_WARNING_OBJECT (self, "hvcC too small");
1019 return GST_FLOW_ERROR;
1022 /* wrong hvcC version */
1023 if (data[0] != 0 && data[0] != 1) {
1024 return GST_FLOW_ERROR;
1027 priv->nal_length_size = (data[21] & 0x03) + 1;
1028 GST_DEBUG_OBJECT (self, "nal length size %u", priv->nal_length_size);
1030 num_nal_arrays = data[22];
1033 for (i = 0; i < num_nal_arrays; i++) {
1034 if (off + 3 >= size) {
1035 GST_WARNING_OBJECT (self, "hvcC too small");
1036 return GST_FLOW_ERROR;
1039 num_nals = GST_READ_UINT16_BE (data + off + 1);
1041 for (j = 0; j < num_nals; j++) {
1042 pres = gst_h265_parser_identify_nalu_hevc (priv->parser,
1043 data, off, size, 2, &nalu);
1045 if (pres != GST_H265_PARSER_OK) {
1046 GST_WARNING_OBJECT (self, "hvcC too small");
1047 return GST_FLOW_ERROR;
1050 switch (nalu.type) {
1051 case GST_H265_NAL_VPS:
1052 pres = gst_h265_parser_parse_vps (priv->parser, &nalu, &vps);
1053 if (pres != GST_H265_PARSER_OK) {
1054 GST_WARNING_OBJECT (self, "Failed to parse VPS");
1055 return GST_FLOW_ERROR;
1058 case GST_H265_NAL_SPS:
1059 pres = gst_h265_parser_parse_sps (priv->parser, &nalu, &sps, TRUE);
1060 if (pres != GST_H265_PARSER_OK) {
1061 GST_WARNING_OBJECT (self, "Failed to parse SPS");
1062 return GST_FLOW_ERROR;
1065 case GST_H265_NAL_PPS:
1066 pres = gst_h265_parser_parse_pps (priv->parser, &nalu, &pps);
1067 if (pres != GST_H265_PARSER_OK) {
1068 GST_WARNING_OBJECT (self, "Failed to parse PPS");
1069 return GST_FLOW_ERROR;
1076 off = nalu.offset + nalu.size;
1084 gst_h265_decoder_set_format (GstVideoDecoder * decoder,
1085 GstVideoCodecState * state)
1087 GstH265Decoder *self = GST_H265_DECODER (decoder);
1088 GstH265DecoderPrivate *priv = self->priv;
1091 GST_DEBUG_OBJECT (decoder, "Set format");
1093 priv->input_state_changed = TRUE;
1095 if (self->input_state)
1096 gst_video_codec_state_unref (self->input_state);
1098 self->input_state = gst_video_codec_state_ref (state);
1100 priv->is_live = FALSE;
1101 query = gst_query_new_latency ();
1102 if (gst_pad_peer_query (GST_VIDEO_DECODER_SINK_PAD (self), query))
1103 gst_query_parse_latency (query, &priv->is_live, NULL, NULL);
1104 gst_query_unref (query);
1107 GstH265DecoderFormat format;
1108 GstH265DecoderAlign align;
1110 gst_h265_decoder_format_from_caps (self, state->caps, &format, &align);
1112 if (format == GST_H265_DECODER_FORMAT_NONE) {
1113 /* codec_data implies packetized */
1114 if (state->codec_data) {
1115 GST_WARNING_OBJECT (self,
1116 "video/x-h265 caps with codec_data but no stream-format=hev1 or hvc1");
1117 format = GST_H265_DECODER_FORMAT_HEV1;
1119 /* otherwise assume bytestream input */
1120 GST_WARNING_OBJECT (self,
1121 "video/x-h265 caps without codec_data or stream-format");
1122 format = GST_H265_DECODER_FORMAT_BYTE;
1126 if (format == GST_H265_DECODER_FORMAT_HEV1 ||
1127 format == GST_H265_DECODER_FORMAT_HVC1) {
1128 if (!state->codec_data) {
1129 /* Try it with size 4 anyway */
1130 priv->nal_length_size = 4;
1131 GST_WARNING_OBJECT (self,
1132 "packetized format without codec data, assuming nal length size is 4");
1135 /* AVC implies alignment=au */
1136 if (align == GST_H265_DECODER_ALIGN_NONE)
1137 align = GST_H265_DECODER_ALIGN_AU;
1140 if (format == GST_H265_DECODER_FORMAT_BYTE && state->codec_data)
1141 GST_WARNING_OBJECT (self, "bytestream with codec data");
1143 priv->in_format = format;
1144 priv->align = align;
1147 if (state->codec_data) {
1150 gst_buffer_map (state->codec_data, &map, GST_MAP_READ);
1151 if (gst_h265_decoder_parse_codec_data (self, map.data, map.size) !=
1153 /* keep going without error.
1154 * Probably inband SPS/PPS might be valid data */
1155 GST_WARNING_OBJECT (self, "Failed to handle codec data");
1157 gst_buffer_unmap (state->codec_data, &map);
1164 gst_h265_decoder_negotiate (GstVideoDecoder * decoder)
1166 GstH265Decoder *self = GST_H265_DECODER (decoder);
1168 /* output state must be updated by subclass using new input state already */
1169 self->priv->input_state_changed = FALSE;
1171 return GST_VIDEO_DECODER_CLASS (parent_class)->negotiate (decoder);
1175 gst_h265_decoder_flush (GstVideoDecoder * decoder)
1177 GstH265Decoder *self = GST_H265_DECODER (decoder);
1179 gst_h265_decoder_clear_dpb (self, TRUE);
1184 static GstFlowReturn
1185 gst_h265_decoder_drain (GstVideoDecoder * decoder)
1187 GstH265Decoder *self = GST_H265_DECODER (decoder);
1189 /* dpb will be cleared by this method */
1190 return gst_h265_decoder_drain_internal (self);
1193 static GstFlowReturn
1194 gst_h265_decoder_finish (GstVideoDecoder * decoder)
1196 return gst_h265_decoder_drain (decoder);
1200 gst_h265_decoder_fill_picture_from_slice (GstH265Decoder * self,
1201 const GstH265Slice * slice, GstH265Picture * picture)
1203 GstH265DecoderPrivate *priv = self->priv;
1204 const GstH265SliceHdr *slice_hdr = &slice->header;
1205 const GstH265NalUnit *nalu = &slice->nalu;
1207 picture->RapPicFlag = slice->rap_pic_flag;
1208 picture->NoRaslOutputFlag = slice->no_rasl_output_flag;
1209 picture->IntraPicFlag = slice->intra_pic_flag;
1210 picture->NoOutputOfPriorPicsFlag = slice->no_output_of_prior_pics_flag;
1211 if (picture->IntraPicFlag) {
1212 priv->associated_irap_NoRaslOutputFlag = picture->NoRaslOutputFlag;
1215 if (GST_H265_IS_NAL_TYPE_RASL (nalu->type) &&
1216 priv->associated_irap_NoRaslOutputFlag) {
1217 picture->output_flag = FALSE;
1219 picture->output_flag = slice_hdr->pic_output_flag;
1225 #define RSV_VCL_N10 10
1226 #define RSV_VCL_N12 12
1227 #define RSV_VCL_N14 14
1230 nal_is_ref (guint8 nal_type)
1232 gboolean ret = FALSE;
1234 case GST_H265_NAL_SLICE_TRAIL_N:
1235 case GST_H265_NAL_SLICE_TSA_N:
1236 case GST_H265_NAL_SLICE_STSA_N:
1237 case GST_H265_NAL_SLICE_RADL_N:
1238 case GST_H265_NAL_SLICE_RASL_N:
1252 gst_h265_decoder_calculate_poc (GstH265Decoder * self,
1253 const GstH265Slice * slice, GstH265Picture * picture)
1255 GstH265DecoderPrivate *priv = self->priv;
1256 const GstH265SliceHdr *slice_hdr = &slice->header;
1257 const GstH265NalUnit *nalu = &slice->nalu;
1258 const GstH265SPS *sps = priv->active_sps;
1259 gint32 MaxPicOrderCntLsb = 1 << (sps->log2_max_pic_order_cnt_lsb_minus4 + 4);
1262 priv->prev_poc_lsb = priv->poc_lsb;
1263 priv->prev_poc_msb = priv->poc_msb;
1265 is_irap = GST_H265_IS_NAL_TYPE_IRAP (nalu->type);
1267 if (!(is_irap && picture->NoRaslOutputFlag)) {
1268 priv->prev_poc_lsb = priv->prev_tid0pic_poc_lsb;
1269 priv->prev_poc_msb = priv->prev_tid0pic_poc_msb;
1272 /* Finding PicOrderCntMsb */
1273 if (is_irap && picture->NoRaslOutputFlag) {
1277 if ((slice_hdr->pic_order_cnt_lsb < priv->prev_poc_lsb) &&
1278 ((priv->prev_poc_lsb - slice_hdr->pic_order_cnt_lsb) >=
1279 (MaxPicOrderCntLsb / 2)))
1280 priv->poc_msb = priv->prev_poc_msb + MaxPicOrderCntLsb;
1282 else if ((slice_hdr->pic_order_cnt_lsb > priv->prev_poc_lsb) &&
1283 ((slice_hdr->pic_order_cnt_lsb - priv->prev_poc_lsb) >
1284 (MaxPicOrderCntLsb / 2)))
1285 priv->poc_msb = priv->prev_poc_msb - MaxPicOrderCntLsb;
1288 priv->poc_msb = priv->prev_poc_msb;
1292 priv->poc = picture->pic_order_cnt =
1293 priv->poc_msb + slice_hdr->pic_order_cnt_lsb;
1294 priv->poc_lsb = picture->pic_order_cnt_lsb = slice_hdr->pic_order_cnt_lsb;
1296 if (GST_H265_IS_NAL_TYPE_IDR (nalu->type)) {
1297 picture->pic_order_cnt = 0;
1298 picture->pic_order_cnt_lsb = 0;
1301 priv->prev_poc_lsb = 0;
1302 priv->prev_poc_msb = 0;
1303 priv->prev_tid0pic_poc_lsb = 0;
1304 priv->prev_tid0pic_poc_msb = 0;
1307 GST_LOG_OBJECT (self,
1308 "PicOrderCntVal %d, (lsb %d)", picture->pic_order_cnt,
1309 picture->pic_order_cnt_lsb);
1311 if (nalu->temporal_id_plus1 == 1 && !GST_H265_IS_NAL_TYPE_RASL (nalu->type) &&
1312 !GST_H265_IS_NAL_TYPE_RADL (nalu->type) && nal_is_ref (nalu->type)) {
1313 priv->prev_tid0pic_poc_lsb = slice_hdr->pic_order_cnt_lsb;
1314 priv->prev_tid0pic_poc_msb = priv->poc_msb;
1321 gst_h265_decoder_set_buffer_flags (GstH265Decoder * self,
1322 GstH265Picture * picture)
1324 GstH265DecoderPrivate *priv = self->priv;
1326 switch (picture->pic_struct) {
1327 case GST_H265_SEI_PIC_STRUCT_FRAME:
1329 case GST_H265_SEI_PIC_STRUCT_TOP_FIELD:
1330 case GST_H265_SEI_PIC_STRUCT_TOP_PAIRED_PREVIOUS_BOTTOM:
1331 case GST_H265_SEI_PIC_STRUCT_TOP_PAIRED_NEXT_BOTTOM:
1332 if (!priv->field_seq_flag) {
1333 GST_FIXME_OBJECT (self,
1334 "top-field with field_seq_flag == 0, what does it mean?");
1336 picture->buffer_flags = GST_VIDEO_BUFFER_FLAG_TOP_FIELD;
1339 case GST_H265_SEI_PIC_STRUCT_BOTTOM_FIELD:
1340 case GST_H265_SEI_PIC_STRUCT_BOTTOM_PAIRED_PREVIOUS_TOP:
1341 case GST_H265_SEI_PIC_STRUCT_BOTTOM_PAIRED_NEXT_TOP:
1342 if (!priv->field_seq_flag) {
1343 GST_FIXME_OBJECT (self,
1344 "bottom-field with field_seq_flag == 0, what does it mean?");
1346 picture->buffer_flags = GST_VIDEO_BUFFER_FLAG_BOTTOM_FIELD;
1349 case GST_H265_SEI_PIC_STRUCT_TOP_BOTTOM:
1350 if (priv->field_seq_flag) {
1351 GST_FIXME_OBJECT (self,
1352 "TFF with field_seq_flag == 1, what does it mean?");
1354 picture->buffer_flags =
1355 GST_VIDEO_BUFFER_FLAG_INTERLACED | GST_VIDEO_BUFFER_FLAG_TFF;
1358 case GST_H265_SEI_PIC_STRUCT_BOTTOM_TOP:
1359 if (priv->field_seq_flag) {
1360 GST_FIXME_OBJECT (self,
1361 "BFF with field_seq_flag == 1, what does it mean?");
1363 picture->buffer_flags = GST_VIDEO_BUFFER_FLAG_INTERLACED;
1367 GST_FIXME_OBJECT (self, "Unhandled picture time SEI pic_struct %d",
1368 picture->pic_struct);
1376 gst_h265_decoder_init_current_picture (GstH265Decoder * self)
1378 GstH265DecoderPrivate *priv = self->priv;
1380 if (!gst_h265_decoder_fill_picture_from_slice (self, &priv->current_slice,
1381 priv->current_picture)) {
1385 if (!gst_h265_decoder_calculate_poc (self,
1386 &priv->current_slice, priv->current_picture))
1389 /* Use picture struct parsed from picture timing SEI */
1390 priv->current_picture->pic_struct = priv->cur_pic_struct;
1391 priv->current_picture->source_scan_type = priv->cur_source_scan_type;
1392 priv->current_picture->duplicate_flag = priv->cur_duplicate_flag;
1393 gst_h265_decoder_set_buffer_flags (self, priv->current_picture);
1399 has_entry_in_rps (GstH265Picture * dpb_pic,
1400 GstH265Picture ** rps_list, guint rps_list_length)
1404 if (!dpb_pic || !rps_list || !rps_list_length)
1407 for (i = 0; i < rps_list_length; i++) {
1408 if (rps_list[i] && rps_list[i]->pic_order_cnt == dpb_pic->pic_order_cnt)
1415 gst_h265_decoder_clear_ref_pic_sets (GstH265Decoder * self)
1419 for (i = 0; i < 16; i++) {
1420 gst_h265_picture_replace (&self->RefPicSetLtCurr[i], NULL);
1421 gst_h265_picture_replace (&self->RefPicSetLtFoll[i], NULL);
1422 gst_h265_picture_replace (&self->RefPicSetStCurrBefore[i], NULL);
1423 gst_h265_picture_replace (&self->RefPicSetStCurrAfter[i], NULL);
1424 gst_h265_picture_replace (&self->RefPicSetStFoll[i], NULL);
1429 gst_h265_decoder_derive_and_mark_rps (GstH265Decoder * self,
1430 GstH265Picture * picture, gint32 * CurrDeltaPocMsbPresentFlag,
1431 gint32 * FollDeltaPocMsbPresentFlag)
1433 GstH265DecoderPrivate *priv = self->priv;
1437 gst_h265_decoder_clear_ref_pic_sets (self);
1440 for (i = 0; i < self->NumPocLtCurr; i++) {
1441 if (!CurrDeltaPocMsbPresentFlag[i]) {
1442 self->RefPicSetLtCurr[i] =
1443 gst_h265_dpb_get_ref_by_poc_lsb (priv->dpb, priv->PocLtCurr[i]);
1445 self->RefPicSetLtCurr[i] =
1446 gst_h265_dpb_get_ref_by_poc (priv->dpb, priv->PocLtCurr[i]);
1450 for (i = 0; i < self->NumPocLtFoll; i++) {
1451 if (!FollDeltaPocMsbPresentFlag[i]) {
1452 self->RefPicSetLtFoll[i] =
1453 gst_h265_dpb_get_ref_by_poc_lsb (priv->dpb, priv->PocLtFoll[i]);
1455 self->RefPicSetLtFoll[i] =
1456 gst_h265_dpb_get_ref_by_poc (priv->dpb, priv->PocLtFoll[i]);
1460 /* Mark all ref pics in RefPicSetLtCurr and RefPicSetLtFol as long_term_refs */
1461 for (i = 0; i < self->NumPocLtCurr; i++) {
1462 if (self->RefPicSetLtCurr[i]) {
1463 self->RefPicSetLtCurr[i]->ref = TRUE;
1464 self->RefPicSetLtCurr[i]->long_term = TRUE;
1468 for (i = 0; i < self->NumPocLtFoll; i++) {
1469 if (self->RefPicSetLtFoll[i]) {
1470 self->RefPicSetLtFoll[i]->ref = TRUE;
1471 self->RefPicSetLtFoll[i]->long_term = TRUE;
1476 for (i = 0; i < self->NumPocStCurrBefore; i++) {
1477 self->RefPicSetStCurrBefore[i] =
1478 gst_h265_dpb_get_short_ref_by_poc (priv->dpb, priv->PocStCurrBefore[i]);
1481 for (i = 0; i < self->NumPocStCurrAfter; i++) {
1482 self->RefPicSetStCurrAfter[i] =
1483 gst_h265_dpb_get_short_ref_by_poc (priv->dpb, priv->PocStCurrAfter[i]);
1486 for (i = 0; i < self->NumPocStFoll; i++) {
1487 self->RefPicSetStFoll[i] =
1488 gst_h265_dpb_get_short_ref_by_poc (priv->dpb, priv->PocStFoll[i]);
1491 /* Mark all dpb pics not beloging to RefPicSet*[] as unused for ref */
1492 dpb_array = gst_h265_dpb_get_pictures_all (priv->dpb);
1493 for (i = 0; i < dpb_array->len; i++) {
1494 GstH265Picture *dpb_pic = g_array_index (dpb_array, GstH265Picture *, i);
1497 !has_entry_in_rps (dpb_pic, self->RefPicSetLtCurr, self->NumPocLtCurr)
1498 && !has_entry_in_rps (dpb_pic, self->RefPicSetLtFoll,
1500 && !has_entry_in_rps (dpb_pic, self->RefPicSetStCurrAfter,
1501 self->NumPocStCurrAfter)
1502 && !has_entry_in_rps (dpb_pic, self->RefPicSetStCurrBefore,
1503 self->NumPocStCurrBefore)
1504 && !has_entry_in_rps (dpb_pic, self->RefPicSetStFoll,
1505 self->NumPocStFoll)) {
1506 GST_LOG_OBJECT (self, "Mark Picture %p (poc %d) as non-ref", dpb_pic,
1507 dpb_pic->pic_order_cnt);
1508 dpb_pic->ref = FALSE;
1509 dpb_pic->long_term = FALSE;
1513 g_array_unref (dpb_array);
1517 gst_h265_decoder_prepare_rps (GstH265Decoder * self, const GstH265Slice * slice,
1518 GstH265Picture * picture)
1520 GstH265DecoderPrivate *priv = self->priv;
1521 gint32 CurrDeltaPocMsbPresentFlag[16] = { 0, };
1522 gint32 FollDeltaPocMsbPresentFlag[16] = { 0, };
1523 const GstH265SliceHdr *slice_hdr = &slice->header;
1524 const GstH265NalUnit *nalu = &slice->nalu;
1525 const GstH265SPS *sps = priv->active_sps;
1526 guint32 MaxPicOrderCntLsb = 1 << (sps->log2_max_pic_order_cnt_lsb_minus4 + 4);
1529 /* if it is an irap pic, set all ref pics in dpb as unused for ref */
1530 if (GST_H265_IS_NAL_TYPE_IRAP (nalu->type) && picture->NoRaslOutputFlag) {
1531 GST_DEBUG_OBJECT (self, "Mark all pictures in DPB as non-ref");
1532 gst_h265_dpb_mark_all_non_ref (priv->dpb);
1535 /* Reset everything for IDR */
1536 if (GST_H265_IS_NAL_TYPE_IDR (nalu->type)) {
1537 memset (priv->PocStCurrBefore, 0, sizeof (priv->PocStCurrBefore));
1538 memset (priv->PocStCurrAfter, 0, sizeof (priv->PocStCurrAfter));
1539 memset (priv->PocStFoll, 0, sizeof (priv->PocStFoll));
1540 memset (priv->PocLtCurr, 0, sizeof (priv->PocLtCurr));
1541 memset (priv->PocLtFoll, 0, sizeof (priv->PocLtFoll));
1542 self->NumPocStCurrBefore = self->NumPocStCurrAfter = self->NumPocStFoll = 0;
1543 self->NumPocLtCurr = self->NumPocLtFoll = 0;
1545 const GstH265ShortTermRefPicSet *stRefPic = NULL;
1546 gint32 num_lt_pics, pocLt;
1547 gint32 PocLsbLt[16] = { 0, };
1548 gint32 UsedByCurrPicLt[16] = { 0, };
1549 gint32 DeltaPocMsbCycleLt[16] = { 0, };
1550 gint numtotalcurr = 0;
1552 /* this is based on CurrRpsIdx described in spec */
1553 if (!slice_hdr->short_term_ref_pic_set_sps_flag)
1554 stRefPic = &slice_hdr->short_term_ref_pic_sets;
1555 else if (sps->num_short_term_ref_pic_sets)
1557 &sps->short_term_ref_pic_set[slice_hdr->short_term_ref_pic_set_idx];
1559 if (stRefPic == NULL)
1562 GST_LOG_OBJECT (self,
1563 "NumDeltaPocs: %d, NumNegativePics: %d, NumPositivePics %d",
1564 stRefPic->NumDeltaPocs, stRefPic->NumNegativePics,
1565 stRefPic->NumPositivePics);
1567 for (i = 0, j = 0, k = 0; i < stRefPic->NumNegativePics; i++) {
1568 if (stRefPic->UsedByCurrPicS0[i]) {
1569 priv->PocStCurrBefore[j++] =
1570 picture->pic_order_cnt + stRefPic->DeltaPocS0[i];
1573 priv->PocStFoll[k++] = picture->pic_order_cnt + stRefPic->DeltaPocS0[i];
1575 self->NumPocStCurrBefore = j;
1576 for (i = 0, j = 0; i < stRefPic->NumPositivePics; i++) {
1577 if (stRefPic->UsedByCurrPicS1[i]) {
1578 priv->PocStCurrAfter[j++] =
1579 picture->pic_order_cnt + stRefPic->DeltaPocS1[i];
1582 priv->PocStFoll[k++] = picture->pic_order_cnt + stRefPic->DeltaPocS1[i];
1584 self->NumPocStCurrAfter = j;
1585 self->NumPocStFoll = k;
1586 num_lt_pics = slice_hdr->num_long_term_sps + slice_hdr->num_long_term_pics;
1587 /* The variables PocLsbLt[i] and UsedByCurrPicLt[i] are derived as follows: */
1588 for (i = 0; i < num_lt_pics; i++) {
1589 if (i < slice_hdr->num_long_term_sps) {
1590 PocLsbLt[i] = sps->lt_ref_pic_poc_lsb_sps[slice_hdr->lt_idx_sps[i]];
1591 UsedByCurrPicLt[i] =
1592 sps->used_by_curr_pic_lt_sps_flag[slice_hdr->lt_idx_sps[i]];
1594 PocLsbLt[i] = slice_hdr->poc_lsb_lt[i];
1595 UsedByCurrPicLt[i] = slice_hdr->used_by_curr_pic_lt_flag[i];
1597 if (UsedByCurrPicLt[i])
1601 self->NumPicTotalCurr = numtotalcurr;
1603 /* The variable DeltaPocMsbCycleLt[i] is derived as follows: (7-38) */
1604 for (i = 0; i < num_lt_pics; i++) {
1605 if (i == 0 || i == slice_hdr->num_long_term_sps)
1606 DeltaPocMsbCycleLt[i] = slice_hdr->delta_poc_msb_cycle_lt[i];
1608 DeltaPocMsbCycleLt[i] =
1609 slice_hdr->delta_poc_msb_cycle_lt[i] + DeltaPocMsbCycleLt[i - 1];
1613 for (i = 0, j = 0, k = 0; i < num_lt_pics; i++) {
1614 pocLt = PocLsbLt[i];
1615 if (slice_hdr->delta_poc_msb_present_flag[i])
1617 picture->pic_order_cnt - DeltaPocMsbCycleLt[i] * MaxPicOrderCntLsb -
1618 slice_hdr->pic_order_cnt_lsb;
1619 if (UsedByCurrPicLt[i]) {
1620 priv->PocLtCurr[j] = pocLt;
1621 CurrDeltaPocMsbPresentFlag[j++] =
1622 slice_hdr->delta_poc_msb_present_flag[i];
1624 priv->PocLtFoll[k] = pocLt;
1625 FollDeltaPocMsbPresentFlag[k++] =
1626 slice_hdr->delta_poc_msb_present_flag[i];
1629 self->NumPocLtCurr = j;
1630 self->NumPocLtFoll = k;
1633 GST_LOG_OBJECT (self, "NumPocStCurrBefore: %d", self->NumPocStCurrBefore);
1634 GST_LOG_OBJECT (self, "NumPocStCurrAfter: %d", self->NumPocStCurrAfter);
1635 GST_LOG_OBJECT (self, "NumPocStFoll: %d", self->NumPocStFoll);
1636 GST_LOG_OBJECT (self, "NumPocLtCurr: %d", self->NumPocLtCurr);
1637 GST_LOG_OBJECT (self, "NumPocLtFoll: %d", self->NumPocLtFoll);
1638 GST_LOG_OBJECT (self, "NumPicTotalCurr: %d", self->NumPicTotalCurr);
1640 /* the derivation process for the RPS and the picture marking */
1641 gst_h265_decoder_derive_and_mark_rps (self, picture,
1642 CurrDeltaPocMsbPresentFlag, FollDeltaPocMsbPresentFlag);
1648 gst_h265_decoder_do_output_picture (GstH265Decoder * self,
1649 GstH265Picture * picture, GstFlowReturn * ret)
1651 GstH265DecoderPrivate *priv = self->priv;
1652 GstVideoCodecFrame *frame = NULL;
1653 GstH265DecoderOutputFrame output_frame;
1654 GstFlowReturn flow_ret = GST_FLOW_OK;
1656 g_assert (ret != NULL);
1658 GST_LOG_OBJECT (self, "Output picture %p (poc %d)", picture,
1659 picture->pic_order_cnt);
1661 if (picture->pic_order_cnt < priv->last_output_poc) {
1662 GST_WARNING_OBJECT (self,
1663 "Outputting out of order %d -> %d, likely a broken stream",
1664 priv->last_output_poc, picture->pic_order_cnt);
1667 priv->last_output_poc = picture->pic_order_cnt;
1669 frame = gst_video_decoder_get_frame (GST_VIDEO_DECODER (self),
1670 picture->system_frame_number);
1673 GST_ERROR_OBJECT (self,
1674 "No available codec frame with frame number %d",
1675 picture->system_frame_number);
1676 UPDATE_FLOW_RETURN (ret, GST_FLOW_ERROR);
1678 gst_h265_picture_unref (picture);
1682 output_frame.frame = frame;
1683 output_frame.picture = picture;
1684 output_frame.self = self;
1685 gst_queue_array_push_tail_struct (priv->output_queue, &output_frame);
1687 gst_h265_decoder_drain_output_queue (self, priv->preferred_output_delay,
1689 UPDATE_FLOW_RETURN (ret, flow_ret);
1693 gst_h265_decoder_clear_dpb (GstH265Decoder * self, gboolean flush)
1695 GstVideoDecoder *decoder = GST_VIDEO_DECODER (self);
1696 GstH265DecoderPrivate *priv = self->priv;
1697 GstH265Picture *picture;
1699 /* If we are not flushing now, videodecoder baseclass will hold
1700 * GstVideoCodecFrame. Release frames manually */
1702 while ((picture = gst_h265_dpb_bump (priv->dpb, TRUE)) != NULL) {
1703 GstVideoCodecFrame *frame = gst_video_decoder_get_frame (decoder,
1704 picture->system_frame_number);
1707 gst_video_decoder_release_frame (decoder, frame);
1708 gst_h265_picture_unref (picture);
1712 gst_queue_array_clear (priv->output_queue);
1713 gst_h265_dpb_clear (priv->dpb);
1714 priv->last_output_poc = G_MININT32;
1717 static GstFlowReturn
1718 gst_h265_decoder_drain_internal (GstH265Decoder * self)
1720 GstH265DecoderPrivate *priv = self->priv;
1721 GstH265Picture *picture;
1722 GstFlowReturn ret = GST_FLOW_OK;
1724 while ((picture = gst_h265_dpb_bump (priv->dpb, TRUE)) != NULL)
1725 gst_h265_decoder_do_output_picture (self, picture, &ret);
1727 gst_h265_decoder_drain_output_queue (self, 0, &ret);
1729 gst_h265_dpb_clear (priv->dpb);
1730 priv->last_output_poc = G_MININT32;
1736 static GstFlowReturn
1737 gst_h265_decoder_dpb_init (GstH265Decoder * self, const GstH265Slice * slice,
1738 GstH265Picture * picture)
1740 GstH265DecoderPrivate *priv = self->priv;
1741 const GstH265SPS *sps = priv->active_sps;
1742 GstH265Picture *to_output;
1743 GstFlowReturn ret = GST_FLOW_OK;
1746 if (slice->clear_dpb) {
1747 if (picture->NoOutputOfPriorPicsFlag) {
1748 GST_DEBUG_OBJECT (self, "Clear dpb");
1749 gst_h265_decoder_drain_output_queue (self, 0, &ret);
1750 gst_h265_decoder_clear_dpb (self, FALSE);
1752 gst_h265_dpb_delete_unused (priv->dpb);
1753 while ((to_output = gst_h265_dpb_bump (priv->dpb, FALSE)) != NULL)
1754 gst_h265_decoder_do_output_picture (self, to_output, &ret);
1756 if (gst_h265_dpb_get_size (priv->dpb) > 0) {
1757 GST_WARNING_OBJECT (self, "IDR or BLA frame failed to clear the dpb, "
1758 "there are still %d pictures in the dpb, last output poc is %d",
1759 gst_h265_dpb_get_size (priv->dpb), priv->last_output_poc);
1761 priv->last_output_poc = G_MININT32;
1765 gst_h265_dpb_delete_unused (priv->dpb);
1766 while (gst_h265_dpb_needs_bump (priv->dpb,
1767 sps->max_num_reorder_pics[sps->max_sub_layers_minus1],
1768 priv->SpsMaxLatencyPictures,
1769 sps->max_dec_pic_buffering_minus1[sps->max_sub_layers_minus1] +
1771 to_output = gst_h265_dpb_bump (priv->dpb, FALSE);
1773 /* Something wrong... */
1775 GST_WARNING_OBJECT (self, "Bumping is needed but no picture to output");
1779 gst_h265_decoder_do_output_picture (self, to_output, &ret);
1786 static GstFlowReturn
1787 gst_h265_decoder_start_current_picture (GstH265Decoder * self)
1789 GstH265DecoderClass *klass;
1790 GstH265DecoderPrivate *priv = self->priv;
1791 GstFlowReturn ret = GST_FLOW_OK;
1793 g_assert (priv->current_picture != NULL);
1794 g_assert (priv->active_sps != NULL);
1795 g_assert (priv->active_pps != NULL);
1797 if (!gst_h265_decoder_init_current_picture (self))
1798 return GST_FLOW_ERROR;
1800 /* Drop all RASL pictures having NoRaslOutputFlag is TRUE for the
1801 * associated IRAP picture */
1802 if (GST_H265_IS_NAL_TYPE_RASL (priv->current_slice.nalu.type) &&
1803 priv->associated_irap_NoRaslOutputFlag) {
1804 GST_DEBUG_OBJECT (self, "Drop current picture");
1805 gst_h265_picture_replace (&priv->current_picture, NULL);
1809 /* If subclass didn't update output state at this point,
1810 * marking this picture as a discont and stores current input state */
1811 if (priv->input_state_changed) {
1812 priv->current_picture->discont_state =
1813 gst_video_codec_state_ref (self->input_state);
1814 priv->input_state_changed = FALSE;
1817 if (!gst_h265_decoder_prepare_rps (self, &priv->current_slice,
1818 priv->current_picture)) {
1819 GST_WARNING_OBJECT (self, "Failed to prepare ref pic set");
1820 gst_clear_h265_picture (&priv->current_picture);
1821 return GST_FLOW_ERROR;
1824 ret = gst_h265_decoder_dpb_init (self,
1825 &priv->current_slice, priv->current_picture);
1826 if (ret != GST_FLOW_OK) {
1827 GST_WARNING_OBJECT (self, "Failed to init dpb");
1831 klass = GST_H265_DECODER_GET_CLASS (self);
1832 if (klass->start_picture) {
1833 ret = klass->start_picture (self, priv->current_picture,
1834 &priv->current_slice, priv->dpb);
1836 if (ret != GST_FLOW_OK) {
1837 GST_WARNING_OBJECT (self, "subclass does not want to start picture");
1846 gst_h265_decoder_finish_picture (GstH265Decoder * self,
1847 GstH265Picture * picture, GstFlowReturn * ret)
1849 GstVideoDecoder *decoder = GST_VIDEO_DECODER (self);
1850 GstH265DecoderPrivate *priv = self->priv;
1851 const GstH265SPS *sps = priv->active_sps;
1853 g_assert (ret != NULL);
1855 GST_LOG_OBJECT (self,
1856 "Finishing picture %p (poc %d), entries in DPB %d",
1857 picture, picture->pic_order_cnt, gst_h265_dpb_get_size (priv->dpb));
1859 gst_h265_dpb_delete_unused (priv->dpb);
1861 /* This picture is decode only, drop corresponding frame */
1862 if (!picture->output_flag) {
1863 GstVideoCodecFrame *frame = gst_video_decoder_get_frame (decoder,
1864 picture->system_frame_number);
1866 gst_video_decoder_release_frame (decoder, frame);
1869 /* gst_h265_dpb_add() will take care of pic_latency_cnt increment and
1870 * reference picture marking for this picture */
1871 gst_h265_dpb_add (priv->dpb, picture);
1873 /* NOTE: As per C.5.2.2, bumping by sps_max_dec_pic_buffering_minus1 is
1874 * applied only for the output and removal of pictures from the DPB before
1875 * the decoding of the current picture. So pass zero here */
1876 while (gst_h265_dpb_needs_bump (priv->dpb,
1877 sps->max_num_reorder_pics[sps->max_sub_layers_minus1],
1878 priv->SpsMaxLatencyPictures, 0)) {
1879 GstH265Picture *to_output = gst_h265_dpb_bump (priv->dpb, FALSE);
1881 /* Something wrong... */
1883 GST_WARNING_OBJECT (self, "Bumping is needed but no picture to output");
1887 gst_h265_decoder_do_output_picture (self, to_output, ret);
1892 gst_h265_decoder_finish_current_picture (GstH265Decoder * self,
1893 GstFlowReturn * ret)
1895 GstH265DecoderPrivate *priv = self->priv;
1896 GstH265DecoderClass *klass;
1897 GstFlowReturn flow_ret = GST_FLOW_OK;
1899 g_assert (ret != NULL);
1901 if (!priv->current_picture)
1904 klass = GST_H265_DECODER_GET_CLASS (self);
1906 if (klass->end_picture) {
1907 flow_ret = klass->end_picture (self, priv->current_picture);
1908 if (flow_ret != GST_FLOW_OK) {
1909 GST_WARNING_OBJECT (self, "End picture failed");
1911 /* continue to empty dpb */
1912 UPDATE_FLOW_RETURN (ret, flow_ret);
1916 /* finish picture takes ownership of the picture */
1917 gst_h265_decoder_finish_picture (self, priv->current_picture, &flow_ret);
1918 priv->current_picture = NULL;
1920 UPDATE_FLOW_RETURN (ret, flow_ret);
1924 gst_h265_decoder_reset_frame_state (GstH265Decoder * self)
1926 GstH265DecoderPrivate *priv = self->priv;
1928 /* Clear picture struct information */
1929 priv->cur_pic_struct = GST_H265_SEI_PIC_STRUCT_FRAME;
1930 priv->cur_source_scan_type = 2;
1931 priv->cur_duplicate_flag = 0;
1932 priv->no_output_of_prior_pics_flag = FALSE;
1933 priv->current_frame = NULL;
1934 g_array_set_size (priv->nalu, 0);
1937 static GstFlowReturn
1938 gst_h265_decoder_handle_frame (GstVideoDecoder * decoder,
1939 GstVideoCodecFrame * frame)
1941 GstH265Decoder *self = GST_H265_DECODER (decoder);
1942 GstH265DecoderPrivate *priv = self->priv;
1943 GstBuffer *in_buf = frame->input_buffer;
1944 GstH265NalUnit nalu;
1945 GstH265ParserResult pres;
1947 GstFlowReturn decode_ret = GST_FLOW_OK;
1950 GST_LOG_OBJECT (self,
1951 "handle frame, PTS: %" GST_TIME_FORMAT ", DTS: %"
1952 GST_TIME_FORMAT, GST_TIME_ARGS (GST_BUFFER_PTS (in_buf)),
1953 GST_TIME_ARGS (GST_BUFFER_DTS (in_buf)));
1955 gst_h265_decoder_reset_frame_state (self);
1957 priv->current_frame = frame;
1959 if (!gst_buffer_map (in_buf, &map, GST_MAP_READ)) {
1960 GST_ELEMENT_ERROR (self, RESOURCE, READ,
1961 ("Failed to map memory for reading"), (NULL));
1962 return GST_FLOW_ERROR;
1965 if (priv->in_format == GST_H265_DECODER_FORMAT_HVC1 ||
1966 priv->in_format == GST_H265_DECODER_FORMAT_HEV1) {
1971 pres = gst_h265_parser_identify_and_split_nalu_hevc (priv->parser,
1972 map.data, offset, map.size, priv->nal_length_size, priv->split_nalu,
1974 if (pres != GST_H265_PARSER_OK)
1977 for (i = 0; i < priv->split_nalu->len; i++) {
1978 GstH265NalUnit *nl =
1979 &g_array_index (priv->split_nalu, GstH265NalUnit, i);
1980 pres = gst_h265_decoder_parse_nalu (self, nl);
1981 if (pres != GST_H265_PARSER_OK)
1985 if (pres != GST_H265_PARSER_OK)
1989 } while (pres == GST_H265_PARSER_OK);
1991 pres = gst_h265_parser_identify_nalu (priv->parser,
1992 map.data, 0, map.size, &nalu);
1994 if (pres == GST_H265_PARSER_NO_NAL_END)
1995 pres = GST_H265_PARSER_OK;
1997 while (pres == GST_H265_PARSER_OK) {
1998 pres = gst_h265_decoder_parse_nalu (self, &nalu);
1999 if (pres != GST_H265_PARSER_OK)
2002 pres = gst_h265_parser_identify_nalu (priv->parser,
2003 map.data, nalu.offset + nalu.size, map.size, &nalu);
2004 if (pres == GST_H265_PARSER_NO_NAL_END)
2005 pres = GST_H265_PARSER_OK;
2009 for (i = 0; i < priv->nalu->len && decode_ret == GST_FLOW_OK; i++) {
2010 GstH265DecoderNalUnit *decoder_nalu =
2011 &g_array_index (priv->nalu, GstH265DecoderNalUnit, i);
2012 decode_ret = gst_h265_decoder_decode_nalu (self, decoder_nalu);
2015 gst_buffer_unmap (in_buf, &map);
2016 gst_h265_decoder_reset_frame_state (self);
2018 if (decode_ret != GST_FLOW_OK) {
2019 if (decode_ret == GST_FLOW_ERROR) {
2020 GST_VIDEO_DECODER_ERROR (self, 1, STREAM, DECODE,
2021 ("Failed to decode data"), (NULL), decode_ret);
2024 gst_video_decoder_drop_frame (decoder, frame);
2025 gst_clear_h265_picture (&priv->current_picture);
2030 if (priv->current_picture) {
2031 gst_h265_decoder_finish_current_picture (self, &decode_ret);
2032 gst_video_codec_frame_unref (frame);
2034 /* This picture was dropped */
2035 gst_video_decoder_release_frame (decoder, frame);
2038 if (decode_ret == GST_FLOW_ERROR) {
2039 GST_VIDEO_DECODER_ERROR (self, 1, STREAM, DECODE,
2040 ("Failed to decode data"), (NULL), decode_ret);
2047 gst_h265_decoder_clear_nalu (GstH265DecoderNalUnit * nalu)
2052 memset (nalu, 0, sizeof (GstH265DecoderNalUnit));
2056 * gst_h265_decoder_set_process_ref_pic_lists:
2057 * @decoder: a #GstH265Decoder
2058 * @process: whether subclass is requiring reference picture modification process
2060 * Called to en/disable reference picture modification process.
2065 gst_h265_decoder_set_process_ref_pic_lists (GstH265Decoder * decoder,
2068 decoder->priv->process_ref_pic_lists = process;
2072 * gst_h265_decoder_get_picture:
2073 * @decoder: a #GstH265Decoder
2074 * @system_frame_number: a target system frame number of #GstH265Picture
2076 * Retrive DPB and return a #GstH265Picture corresponding to
2077 * the @system_frame_number
2079 * Returns: (transfer full) (nullable): a #GstH265Picture if successful, or %NULL otherwise
2084 gst_h265_decoder_get_picture (GstH265Decoder * decoder,
2085 guint32 system_frame_number)
2087 return gst_h265_dpb_get_picture (decoder->priv->dpb, system_frame_number);