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 /* For delayed output */
131 guint preferred_output_delay;
133 GstQueueArray *output_queue;
144 } GstH265DecoderNalUnit;
149 GstVideoCodecFrame *frame;
150 GstH265Picture *picture;
152 GstH265Decoder *self;
153 } GstH265DecoderOutputFrame;
155 #define UPDATE_FLOW_RETURN(ret,new_ret) G_STMT_START { \
156 if (*(ret) == GST_FLOW_OK) \
160 #define parent_class gst_h265_decoder_parent_class
161 G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GstH265Decoder, gst_h265_decoder,
162 GST_TYPE_VIDEO_DECODER,
163 G_ADD_PRIVATE (GstH265Decoder);
164 GST_DEBUG_CATEGORY_INIT (gst_h265_decoder_debug, "h265decoder", 0,
165 "H.265 Video Decoder"));
167 static void gst_h265_decoder_finalize (GObject * object);
169 static gboolean gst_h265_decoder_start (GstVideoDecoder * decoder);
170 static gboolean gst_h265_decoder_stop (GstVideoDecoder * decoder);
171 static gboolean gst_h265_decoder_set_format (GstVideoDecoder * decoder,
172 GstVideoCodecState * state);
173 static GstFlowReturn gst_h265_decoder_finish (GstVideoDecoder * decoder);
174 static gboolean gst_h265_decoder_flush (GstVideoDecoder * decoder);
175 static GstFlowReturn gst_h265_decoder_drain (GstVideoDecoder * decoder);
176 static GstFlowReturn gst_h265_decoder_handle_frame (GstVideoDecoder * decoder,
177 GstVideoCodecFrame * frame);
179 static void gst_h265_decoder_finish_current_picture (GstH265Decoder * self,
180 GstFlowReturn * ret);
181 static void gst_h265_decoder_clear_ref_pic_sets (GstH265Decoder * self);
182 static void gst_h265_decoder_clear_dpb (GstH265Decoder * self, gboolean flush);
183 static GstFlowReturn gst_h265_decoder_drain_internal (GstH265Decoder * self);
185 gst_h265_decoder_start_current_picture (GstH265Decoder * self);
186 static void gst_h265_decoder_clear_nalu (GstH265DecoderNalUnit * nalu);
188 gst_h265_decoder_clear_output_frame (GstH265DecoderOutputFrame * output_frame);
191 gst_h265_decoder_class_init (GstH265DecoderClass * klass)
193 GstVideoDecoderClass *decoder_class = GST_VIDEO_DECODER_CLASS (klass);
194 GObjectClass *object_class = G_OBJECT_CLASS (klass);
196 object_class->finalize = GST_DEBUG_FUNCPTR (gst_h265_decoder_finalize);
198 decoder_class->start = GST_DEBUG_FUNCPTR (gst_h265_decoder_start);
199 decoder_class->stop = GST_DEBUG_FUNCPTR (gst_h265_decoder_stop);
200 decoder_class->set_format = GST_DEBUG_FUNCPTR (gst_h265_decoder_set_format);
201 decoder_class->finish = GST_DEBUG_FUNCPTR (gst_h265_decoder_finish);
202 decoder_class->flush = GST_DEBUG_FUNCPTR (gst_h265_decoder_flush);
203 decoder_class->drain = GST_DEBUG_FUNCPTR (gst_h265_decoder_drain);
204 decoder_class->handle_frame =
205 GST_DEBUG_FUNCPTR (gst_h265_decoder_handle_frame);
209 gst_h265_decoder_init (GstH265Decoder * self)
211 GstH265DecoderPrivate *priv;
213 gst_video_decoder_set_packetized (GST_VIDEO_DECODER (self), TRUE);
215 self->priv = priv = gst_h265_decoder_get_instance_private (self);
217 priv->last_output_poc = G_MININT32;
219 priv->ref_pic_list_tmp = g_array_sized_new (FALSE, TRUE,
220 sizeof (GstH265Picture *), 32);
221 priv->ref_pic_list0 = g_array_sized_new (FALSE, TRUE,
222 sizeof (GstH265Picture *), 32);
223 priv->ref_pic_list1 = g_array_sized_new (FALSE, TRUE,
224 sizeof (GstH265Picture *), 32);
225 priv->nalu = g_array_sized_new (FALSE, TRUE, sizeof (GstH265DecoderNalUnit),
227 g_array_set_clear_func (priv->nalu,
228 (GDestroyNotify) gst_h265_decoder_clear_nalu);
230 gst_queue_array_new_for_struct (sizeof (GstH265DecoderOutputFrame), 1);
231 gst_queue_array_set_clear_func (priv->output_queue,
232 (GDestroyNotify) gst_h265_decoder_clear_output_frame);
236 gst_h265_decoder_finalize (GObject * object)
238 GstH265Decoder *self = GST_H265_DECODER (object);
239 GstH265DecoderPrivate *priv = self->priv;
241 g_array_unref (priv->ref_pic_list_tmp);
242 g_array_unref (priv->ref_pic_list0);
243 g_array_unref (priv->ref_pic_list1);
244 g_array_unref (priv->nalu);
245 gst_queue_array_free (priv->output_queue);
247 G_OBJECT_CLASS (parent_class)->finalize (object);
251 gst_h265_decoder_start (GstVideoDecoder * decoder)
253 GstH265Decoder *self = GST_H265_DECODER (decoder);
254 GstH265DecoderPrivate *priv = self->priv;
256 priv->parser = gst_h265_parser_new ();
257 priv->dpb = gst_h265_dpb_new ();
258 priv->new_bitstream = TRUE;
259 priv->prev_nal_is_eos = FALSE;
265 gst_h265_decoder_stop (GstVideoDecoder * decoder)
267 GstH265Decoder *self = GST_H265_DECODER (decoder);
268 GstH265DecoderPrivate *priv = self->priv;
270 if (self->input_state) {
271 gst_video_codec_state_unref (self->input_state);
272 self->input_state = NULL;
276 gst_h265_parser_free (priv->parser);
281 gst_h265_dpb_free (priv->dpb);
285 gst_h265_decoder_clear_ref_pic_sets (self);
291 gst_h265_decoder_clear_output_frame (GstH265DecoderOutputFrame * output_frame)
296 if (output_frame->frame) {
297 gst_video_decoder_release_frame (GST_VIDEO_DECODER (output_frame->self),
298 output_frame->frame);
299 output_frame->frame = NULL;
302 gst_clear_h265_picture (&output_frame->picture);
306 gst_h265_decoder_is_crop_rect_changed (GstH265Decoder * self, GstH265SPS * sps)
308 GstH265DecoderPrivate *priv = self->priv;
310 if (priv->conformance_window_flag != sps->conformance_window_flag)
312 if (priv->crop_rect_width != sps->crop_rect_width)
314 if (priv->crop_rect_height != sps->crop_rect_height)
316 if (priv->crop_rect_x != sps->crop_rect_x)
318 if (priv->crop_rect_y != sps->crop_rect_y)
325 gst_h265_decoder_drain_output_queue (GstH265Decoder * self, guint num,
328 GstH265DecoderPrivate *priv = self->priv;
329 GstH265DecoderClass *klass = GST_H265_DECODER_GET_CLASS (self);
331 g_assert (klass->output_picture);
332 g_assert (ret != NULL);
334 while (gst_queue_array_get_length (priv->output_queue) > num) {
335 GstH265DecoderOutputFrame *output_frame = (GstH265DecoderOutputFrame *)
336 gst_queue_array_pop_head_struct (priv->output_queue);
337 GstFlowReturn flow_ret = klass->output_picture (self, output_frame->frame,
338 output_frame->picture);
340 UPDATE_FLOW_RETURN (ret, flow_ret);
345 gst_h265_decoder_process_sps (GstH265Decoder * self, GstH265SPS * sps)
347 GstH265DecoderPrivate *priv = self->priv;
349 gint prev_max_dpb_size;
351 const gint MaxDpbPicBuf = 6;
352 gint PicSizeInSamplesY;
353 guint8 field_seq_flag = 0;
354 guint8 progressive_source_flag = 0;
355 guint8 interlaced_source_flag = 0;
356 GstFlowReturn ret = GST_FLOW_OK;
359 MaxLumaPS = 35651584;
360 PicSizeInSamplesY = sps->width * sps->height;
361 if (PicSizeInSamplesY <= (MaxLumaPS >> 2))
362 max_dpb_size = MaxDpbPicBuf * 4;
363 else if (PicSizeInSamplesY <= (MaxLumaPS >> 1))
364 max_dpb_size = MaxDpbPicBuf * 2;
365 else if (PicSizeInSamplesY <= ((3 * MaxLumaPS) >> 2))
366 max_dpb_size = (MaxDpbPicBuf * 4) / 3;
368 max_dpb_size = MaxDpbPicBuf;
370 max_dpb_size = MIN (max_dpb_size, 16);
372 if (sps->vui_parameters_present_flag)
373 field_seq_flag = sps->vui_params.field_seq_flag;
375 progressive_source_flag = sps->profile_tier_level.progressive_source_flag;
376 interlaced_source_flag = sps->profile_tier_level.interlaced_source_flag;
378 prev_max_dpb_size = gst_h265_dpb_get_max_num_pics (priv->dpb);
379 if (priv->width != sps->width || priv->height != sps->height ||
380 prev_max_dpb_size != max_dpb_size ||
381 priv->field_seq_flag != field_seq_flag ||
382 priv->progressive_source_flag != progressive_source_flag ||
383 priv->interlaced_source_flag != interlaced_source_flag ||
384 gst_h265_decoder_is_crop_rect_changed (self, sps)) {
385 GstH265DecoderClass *klass = GST_H265_DECODER_GET_CLASS (self);
387 GST_DEBUG_OBJECT (self,
388 "SPS updated, resolution: %dx%d -> %dx%d, dpb size: %d -> %d, "
389 "field_seq_flag: %d -> %d, progressive_source_flag: %d -> %d, "
390 "interlaced_source_flag: %d -> %d",
391 priv->width, priv->height, sps->width, sps->height,
392 prev_max_dpb_size, max_dpb_size, priv->field_seq_flag, field_seq_flag,
393 priv->progressive_source_flag, progressive_source_flag,
394 priv->interlaced_source_flag, interlaced_source_flag);
396 if (priv->no_output_of_prior_pics_flag) {
397 gst_h265_decoder_drain_output_queue (self, 0, &ret);
398 gst_h265_decoder_clear_dpb (self, FALSE);
400 ret = gst_h265_decoder_drain_internal (self);
403 if (ret != GST_FLOW_OK)
406 if (klass->get_preferred_output_delay) {
407 priv->preferred_output_delay =
408 klass->get_preferred_output_delay (self, priv->is_live);
410 priv->preferred_output_delay = 0;
413 g_assert (klass->new_sequence);
414 ret = klass->new_sequence (self,
415 sps, max_dpb_size + priv->preferred_output_delay);
416 if (ret != GST_FLOW_OK) {
417 GST_WARNING_OBJECT (self, "subclass does not want accept new sequence");
421 priv->width = sps->width;
422 priv->height = sps->height;
423 priv->conformance_window_flag = sps->conformance_window_flag;
424 priv->crop_rect_width = sps->crop_rect_width;
425 priv->crop_rect_height = sps->crop_rect_height;
426 priv->crop_rect_x = sps->crop_rect_x;
427 priv->crop_rect_y = sps->crop_rect_y;
428 priv->field_seq_flag = field_seq_flag;
429 priv->progressive_source_flag = progressive_source_flag;
430 priv->interlaced_source_flag = interlaced_source_flag;
432 gst_h265_dpb_set_max_num_pics (priv->dpb, max_dpb_size);
435 if (sps->max_latency_increase_plus1[sps->max_sub_layers_minus1]) {
436 priv->SpsMaxLatencyPictures =
437 sps->max_num_reorder_pics[sps->max_sub_layers_minus1] +
438 sps->max_latency_increase_plus1[sps->max_sub_layers_minus1] - 1;
441 GST_DEBUG_OBJECT (self, "Set DPB max size %d", max_dpb_size);
446 static GstH265ParserResult
447 gst_h265_decoder_parse_sei (GstH265Decoder * self, GstH265NalUnit * nalu)
449 GstH265DecoderPrivate *priv = self->priv;
450 GstH265ParserResult pres;
451 GArray *messages = NULL;
454 pres = gst_h265_parser_parse_sei (priv->parser, nalu, &messages);
455 if (pres != GST_H265_PARSER_OK) {
456 GST_WARNING_OBJECT (self, "Failed to parse SEI, result %d", pres);
458 /* XXX: Ignore error from SEI parsing, it might be malformed bitstream,
459 * or our fault. But shouldn't be critical */
460 g_clear_pointer (&messages, g_array_unref);
461 return GST_H265_PARSER_OK;
464 for (i = 0; i < messages->len; i++) {
465 GstH265SEIMessage *sei = &g_array_index (messages, GstH265SEIMessage, i);
467 switch (sei->payloadType) {
468 case GST_H265_SEI_PIC_TIMING:
469 priv->cur_pic_struct = sei->payload.pic_timing.pic_struct;
470 priv->cur_source_scan_type = sei->payload.pic_timing.source_scan_type;
471 priv->cur_duplicate_flag = sei->payload.pic_timing.duplicate_flag;
473 GST_TRACE_OBJECT (self,
474 "Picture Timing SEI, pic_struct: %d, source_scan_type: %d, "
475 "duplicate_flag: %d", priv->cur_pic_struct,
476 priv->cur_source_scan_type, priv->cur_duplicate_flag);
483 g_array_free (messages, TRUE);
484 GST_LOG_OBJECT (self, "SEI parsed");
486 return GST_H265_PARSER_OK;
490 gst_h265_decoder_process_ref_pic_lists (GstH265Decoder * self,
491 GstH265Picture * curr_pic, GstH265Slice * slice,
492 GArray ** ref_pic_list0, GArray ** ref_pic_list1)
494 GstH265DecoderPrivate *priv = self->priv;
495 GstH265RefPicListModification *ref_mod =
496 &slice->header.ref_pic_list_modification;
497 GstH265PPSSccExtensionParams *scc_ext =
498 &slice->header.pps->pps_scc_extension_params;
500 gint num_tmp_refs, i;
502 *ref_pic_list0 = priv->ref_pic_list0;
503 *ref_pic_list1 = priv->ref_pic_list1;
505 /* There is nothing to be done for I slices */
506 if (GST_H265_IS_I_SLICE (&slice->header))
509 /* Inifinit loop prevention */
510 if (self->NumPocStCurrBefore == 0 && self->NumPocStCurrAfter == 0 &&
511 self->NumPocLtCurr == 0 && !scc_ext->pps_curr_pic_ref_enabled_flag) {
512 GST_WARNING_OBJECT (self,
513 "Expected references, got none, preventing infinit loop.");
517 /* 8.3.4 Deriving l0 */
518 tmp_refs = priv->ref_pic_list_tmp;
521 * Deriving l0 consist of appending in loop RefPicSetStCurrBefore,
522 * RefPicSetStCurrAfter and RefPicSetLtCurr until NumRpsCurrTempList0 item
526 /* NumRpsCurrTempList0 */
527 num_tmp_refs = MAX (slice->header.num_ref_idx_l0_active_minus1 + 1,
528 self->NumPicTotalCurr);
530 while (tmp_refs->len < num_tmp_refs) {
531 for (i = 0; i < self->NumPocStCurrBefore && tmp_refs->len < num_tmp_refs;
533 g_array_append_val (tmp_refs, self->RefPicSetStCurrBefore[i]);
534 for (i = 0; i < self->NumPocStCurrAfter && tmp_refs->len < num_tmp_refs;
536 g_array_append_val (tmp_refs, self->RefPicSetStCurrAfter[i]);
537 for (i = 0; i < self->NumPocLtCurr && tmp_refs->len < num_tmp_refs; i++)
538 g_array_append_val (tmp_refs, self->RefPicSetLtCurr[i]);
539 if (scc_ext->pps_curr_pic_ref_enabled_flag)
540 g_array_append_val (tmp_refs, curr_pic);
544 * If needed, apply the modificaiton base on the lookup table found in the
545 * slice header (list_entry_l0).
547 for (i = 0; i <= slice->header.num_ref_idx_l0_active_minus1; i++) {
548 GstH265Picture **tmp = (GstH265Picture **) tmp_refs->data;
550 if (ref_mod->ref_pic_list_modification_flag_l0)
551 g_array_append_val (*ref_pic_list0, tmp[ref_mod->list_entry_l0[i]]);
553 g_array_append_val (*ref_pic_list0, tmp[i]);
556 if (scc_ext->pps_curr_pic_ref_enabled_flag &&
557 !ref_mod->ref_pic_list_modification_flag_l0 &&
558 num_tmp_refs > (slice->header.num_ref_idx_l0_active_minus1 + 1)) {
559 g_array_index (*ref_pic_list0, GstH265Picture *,
560 slice->header.num_ref_idx_l0_active_minus1) = curr_pic;
563 g_array_set_size (tmp_refs, 0);
565 /* For P slices we only need l0 */
566 if (GST_H265_IS_P_SLICE (&slice->header))
569 /* 8.3.4 Deriving l1 */
571 * Deriving l1 consist of appending in loop RefPicSetStCurrAfter,
572 * RefPicSetStCurrBefore and RefPicSetLtCurr until NumRpsCurrTempList0 item
576 /* NumRpsCurrTempList1 */
577 num_tmp_refs = MAX (slice->header.num_ref_idx_l1_active_minus1 + 1,
578 self->NumPicTotalCurr);
580 while (tmp_refs->len < num_tmp_refs) {
581 for (i = 0; i < self->NumPocStCurrAfter && tmp_refs->len < num_tmp_refs;
583 g_array_append_val (tmp_refs, self->RefPicSetStCurrAfter[i]);
584 for (i = 0; i < self->NumPocStCurrBefore && tmp_refs->len < num_tmp_refs;
586 g_array_append_val (tmp_refs, self->RefPicSetStCurrBefore[i]);
587 for (i = 0; i < self->NumPocLtCurr && tmp_refs->len < num_tmp_refs; i++)
588 g_array_append_val (tmp_refs, self->RefPicSetLtCurr[i]);
589 if (scc_ext->pps_curr_pic_ref_enabled_flag)
590 g_array_append_val (tmp_refs, curr_pic);
594 * If needed, apply the modificaiton base on the lookup table found in the
595 * slice header (list_entry_l1).
597 for (i = 0; i <= slice->header.num_ref_idx_l1_active_minus1; i++) {
598 GstH265Picture **tmp = (GstH265Picture **) tmp_refs->data;
600 if (ref_mod->ref_pic_list_modification_flag_l1)
601 g_array_append_val (*ref_pic_list1, tmp[ref_mod->list_entry_l1[i]]);
603 g_array_append_val (*ref_pic_list1, tmp[i]);
606 g_array_set_size (tmp_refs, 0);
610 gst_h265_decoder_decode_slice (GstH265Decoder * self)
612 GstH265DecoderClass *klass = GST_H265_DECODER_GET_CLASS (self);
613 GstH265DecoderPrivate *priv = self->priv;
614 GstH265Slice *slice = &priv->current_slice;
615 GstH265Picture *picture = priv->current_picture;
618 GstFlowReturn ret = GST_FLOW_OK;
621 GST_ERROR_OBJECT (self, "No current picture");
622 return GST_FLOW_ERROR;
625 g_assert (klass->decode_slice);
627 if (priv->process_ref_pic_lists) {
628 l0 = priv->ref_pic_list0;
629 l1 = priv->ref_pic_list1;
630 gst_h265_decoder_process_ref_pic_lists (self, picture, slice, &l0, &l1);
633 ret = klass->decode_slice (self, picture, slice, l0, l1);
635 if (priv->process_ref_pic_lists) {
636 g_array_set_size (l0, 0);
637 g_array_set_size (l1, 0);
644 gst_h265_decoder_preprocess_slice (GstH265Decoder * self, GstH265Slice * slice)
646 GstH265DecoderPrivate *priv = self->priv;
647 const GstH265SliceHdr *slice_hdr = &slice->header;
649 if (priv->current_picture && slice_hdr->first_slice_segment_in_pic_flag) {
650 GST_WARNING_OBJECT (self,
651 "Current picture is not finished but slice header has "
652 "first_slice_segment_in_pic_flag");
653 return GST_FLOW_ERROR;
660 gst_h265_decoder_process_slice (GstH265Decoder * self, GstH265Slice * slice)
662 GstH265DecoderPrivate *priv = self->priv;
663 GstFlowReturn ret = GST_FLOW_OK;
665 priv->current_slice = *slice;
667 if (priv->current_slice.header.dependent_slice_segment_flag) {
668 GstH265SliceHdr *slice_hdr = &priv->current_slice.header;
669 GstH265SliceHdr *indep_slice_hdr = &priv->prev_independent_slice.header;
671 memcpy (&slice_hdr->type, &indep_slice_hdr->type,
672 G_STRUCT_OFFSET (GstH265SliceHdr, num_entry_point_offsets) -
673 G_STRUCT_OFFSET (GstH265SliceHdr, type));
675 priv->prev_independent_slice = priv->current_slice;
676 memset (&priv->prev_independent_slice.nalu, 0, sizeof (GstH265NalUnit));
679 ret = gst_h265_decoder_preprocess_slice (self, &priv->current_slice);
680 if (ret != GST_FLOW_OK)
683 priv->active_pps = priv->current_slice.header.pps;
684 priv->active_sps = priv->active_pps->sps;
686 if (!priv->current_picture) {
687 GstH265DecoderClass *klass = GST_H265_DECODER_GET_CLASS (self);
688 GstH265Picture *picture;
689 GstFlowReturn ret = GST_FLOW_OK;
691 g_assert (priv->current_frame);
693 picture = gst_h265_picture_new ();
694 /* This allows accessing the frame from the picture. */
695 picture->system_frame_number = priv->current_frame->system_frame_number;
697 priv->current_picture = picture;
699 if (klass->new_picture)
700 ret = klass->new_picture (self, priv->current_frame, picture);
702 if (ret != GST_FLOW_OK) {
703 GST_WARNING_OBJECT (self, "subclass does not want accept new picture");
704 priv->current_picture = NULL;
705 gst_h265_picture_unref (picture);
709 ret = gst_h265_decoder_start_current_picture (self);
710 if (ret != GST_FLOW_OK) {
711 GST_WARNING_OBJECT (self, "start picture failed");
715 /* this picture was dropped */
716 if (!priv->current_picture)
720 return gst_h265_decoder_decode_slice (self);
723 static GstH265ParserResult
724 gst_h265_decoder_parse_slice (GstH265Decoder * self, GstH265NalUnit * nalu)
726 GstH265DecoderPrivate *priv = self->priv;
727 GstH265ParserResult pres;
729 GstH265DecoderNalUnit decoder_nalu;
731 memset (&slice, 0, sizeof (GstH265Slice));
733 pres = gst_h265_parser_parse_slice_hdr (priv->parser, nalu, &slice.header);
734 if (pres != GST_H265_PARSER_OK)
737 /* NOTE: gst_h265_parser_parse_slice_hdr() allocates array
738 * GstH265SliceHdr::entry_point_offset_minus1 but we don't use it
739 * in this h265decoder baseclass at the moment
741 gst_h265_slice_hdr_free (&priv->current_slice.header);
744 if (nalu->type >= GST_H265_NAL_SLICE_BLA_W_LP &&
745 nalu->type <= GST_H265_NAL_SLICE_CRA_NUT) {
746 slice.rap_pic_flag = TRUE;
749 /* NoRaslOutputFlag == 1 if the current picture is
752 * 3) a CRA picture that is the first access unit in the bitstream
753 * 4) first picture that follows an end of sequence NAL unit in decoding order
754 * 5) has HandleCraAsBlaFlag == 1 (set by external means, so not considering )
756 if (GST_H265_IS_NAL_TYPE_IDR (nalu->type) ||
757 GST_H265_IS_NAL_TYPE_BLA (nalu->type) ||
758 (GST_H265_IS_NAL_TYPE_CRA (nalu->type) && priv->new_bitstream) ||
759 priv->prev_nal_is_eos) {
760 slice.no_rasl_output_flag = TRUE;
763 if (GST_H265_IS_NAL_TYPE_IRAP (nalu->type)) {
764 slice.intra_pic_flag = TRUE;
766 if (slice.no_rasl_output_flag && !priv->new_bitstream) {
768 slice.clear_dpb = TRUE;
769 if (nalu->type == GST_H265_NAL_SLICE_CRA_NUT) {
770 slice.no_output_of_prior_pics_flag = TRUE;
772 slice.no_output_of_prior_pics_flag =
773 slice.header.no_output_of_prior_pics_flag;
778 if (slice.no_output_of_prior_pics_flag)
779 priv->no_output_of_prior_pics_flag = TRUE;
781 decoder_nalu.unit.slice = slice;
782 decoder_nalu.is_slice = TRUE;
783 g_array_append_val (priv->nalu, decoder_nalu);
785 return GST_H265_PARSER_OK;
788 static GstH265ParserResult
789 gst_h265_decoder_parse_nalu (GstH265Decoder * self, GstH265NalUnit * nalu)
791 GstH265DecoderPrivate *priv = self->priv;
795 GstH265ParserResult ret = GST_H265_PARSER_OK;
796 GstH265DecoderNalUnit decoder_nalu;
798 GST_LOG_OBJECT (self, "Parsed nal type: %d, offset %d, size %d",
799 nalu->type, nalu->offset, nalu->size);
801 switch (nalu->type) {
802 case GST_H265_NAL_VPS:
803 ret = gst_h265_parser_parse_vps (priv->parser, nalu, &vps);
805 case GST_H265_NAL_SPS:
806 ret = gst_h265_parser_parse_sps (priv->parser, nalu, &sps, TRUE);
807 if (ret != GST_H265_PARSER_OK)
810 memset (&decoder_nalu, 0, sizeof (GstH265DecoderNalUnit));
811 decoder_nalu.unit.sps = sps;
812 g_array_append_val (priv->nalu, decoder_nalu);
814 case GST_H265_NAL_PPS:
815 ret = gst_h265_parser_parse_pps (priv->parser, nalu, &pps);
817 case GST_H265_NAL_PREFIX_SEI:
818 case GST_H265_NAL_SUFFIX_SEI:
819 ret = gst_h265_decoder_parse_sei (self, nalu);
821 case GST_H265_NAL_SLICE_TRAIL_N:
822 case GST_H265_NAL_SLICE_TRAIL_R:
823 case GST_H265_NAL_SLICE_TSA_N:
824 case GST_H265_NAL_SLICE_TSA_R:
825 case GST_H265_NAL_SLICE_STSA_N:
826 case GST_H265_NAL_SLICE_STSA_R:
827 case GST_H265_NAL_SLICE_RADL_N:
828 case GST_H265_NAL_SLICE_RADL_R:
829 case GST_H265_NAL_SLICE_RASL_N:
830 case GST_H265_NAL_SLICE_RASL_R:
831 case GST_H265_NAL_SLICE_BLA_W_LP:
832 case GST_H265_NAL_SLICE_BLA_W_RADL:
833 case GST_H265_NAL_SLICE_BLA_N_LP:
834 case GST_H265_NAL_SLICE_IDR_W_RADL:
835 case GST_H265_NAL_SLICE_IDR_N_LP:
836 case GST_H265_NAL_SLICE_CRA_NUT:
837 ret = gst_h265_decoder_parse_slice (self, nalu);
838 priv->new_bitstream = FALSE;
839 priv->prev_nal_is_eos = FALSE;
841 case GST_H265_NAL_EOB:
842 priv->new_bitstream = TRUE;
844 case GST_H265_NAL_EOS:
845 priv->prev_nal_is_eos = TRUE;
855 gst_h265_decoder_decode_nalu (GstH265Decoder * self,
856 GstH265DecoderNalUnit * nalu)
859 return gst_h265_decoder_process_sps (self, &nalu->unit.sps);
861 return gst_h265_decoder_process_slice (self, &nalu->unit.slice);
865 gst_h265_decoder_format_from_caps (GstH265Decoder * self, GstCaps * caps,
866 GstH265DecoderFormat * format, GstH265DecoderAlign * align)
869 *format = GST_H265_DECODER_FORMAT_NONE;
872 *align = GST_H265_DECODER_ALIGN_NONE;
874 if (!gst_caps_is_fixed (caps)) {
875 GST_WARNING_OBJECT (self, "Caps wasn't fixed");
879 GST_DEBUG_OBJECT (self, "parsing caps: %" GST_PTR_FORMAT, caps);
881 if (caps && gst_caps_get_size (caps) > 0) {
882 GstStructure *s = gst_caps_get_structure (caps, 0);
883 const gchar *str = NULL;
886 if ((str = gst_structure_get_string (s, "stream-format"))) {
887 if (strcmp (str, "hvc1") == 0)
888 *format = GST_H265_DECODER_FORMAT_HVC1;
889 else if (strcmp (str, "hev1") == 0)
890 *format = GST_H265_DECODER_FORMAT_HEV1;
891 else if (strcmp (str, "byte-stream") == 0)
892 *format = GST_H265_DECODER_FORMAT_BYTE;
897 if ((str = gst_structure_get_string (s, "alignment"))) {
898 if (strcmp (str, "au") == 0)
899 *align = GST_H265_DECODER_ALIGN_AU;
900 else if (strcmp (str, "nal") == 0)
901 *align = GST_H265_DECODER_ALIGN_NAL;
908 gst_h265_decoder_parse_codec_data (GstH265Decoder * self, const guint8 * data,
911 GstH265DecoderPrivate *priv = self->priv;
912 guint num_nal_arrays;
914 guint num_nals, i, j;
915 GstH265ParserResult pres;
917 GstFlowReturn ret = GST_FLOW_OK;
922 /* parse the hvcC data */
924 GST_WARNING_OBJECT (self, "hvcC too small");
925 return GST_FLOW_ERROR;
928 /* wrong hvcC version */
929 if (data[0] != 0 && data[0] != 1) {
930 return GST_FLOW_ERROR;
933 priv->nal_length_size = (data[21] & 0x03) + 1;
934 GST_DEBUG_OBJECT (self, "nal length size %u", priv->nal_length_size);
936 num_nal_arrays = data[22];
939 for (i = 0; i < num_nal_arrays; i++) {
940 if (off + 3 >= size) {
941 GST_WARNING_OBJECT (self, "hvcC too small");
942 return GST_FLOW_ERROR;
945 num_nals = GST_READ_UINT16_BE (data + off + 1);
947 for (j = 0; j < num_nals; j++) {
948 pres = gst_h265_parser_identify_nalu_hevc (priv->parser,
949 data, off, size, 2, &nalu);
951 if (pres != GST_H265_PARSER_OK) {
952 GST_WARNING_OBJECT (self, "hvcC too small");
953 return GST_FLOW_ERROR;
957 case GST_H265_NAL_VPS:
958 pres = gst_h265_parser_parse_vps (priv->parser, &nalu, &vps);
959 if (pres != GST_H265_PARSER_OK) {
960 GST_WARNING_OBJECT (self, "Failed to parse VPS");
961 return GST_FLOW_ERROR;
964 case GST_H265_NAL_SPS:
965 pres = gst_h265_parser_parse_sps (priv->parser, &nalu, &sps, TRUE);
966 if (pres != GST_H265_PARSER_OK) {
967 GST_WARNING_OBJECT (self, "Failed to parse SPS");
968 return GST_FLOW_ERROR;
971 ret = gst_h265_decoder_process_sps (self, &sps);
972 if (ret != GST_FLOW_OK) {
973 GST_WARNING_OBJECT (self, "Failed to process SPS");
977 case GST_H265_NAL_PPS:
978 pres = gst_h265_parser_parse_pps (priv->parser, &nalu, &pps);
979 if (pres != GST_H265_PARSER_OK) {
980 GST_WARNING_OBJECT (self, "Failed to parse PPS");
981 return GST_FLOW_ERROR;
988 off = nalu.offset + nalu.size;
996 gst_h265_decoder_set_format (GstVideoDecoder * decoder,
997 GstVideoCodecState * state)
999 GstH265Decoder *self = GST_H265_DECODER (decoder);
1000 GstH265DecoderPrivate *priv = self->priv;
1003 GST_DEBUG_OBJECT (decoder, "Set format");
1005 if (self->input_state)
1006 gst_video_codec_state_unref (self->input_state);
1008 self->input_state = gst_video_codec_state_ref (state);
1011 GstH265DecoderFormat format;
1012 GstH265DecoderAlign align;
1014 gst_h265_decoder_format_from_caps (self, state->caps, &format, &align);
1016 if (format == GST_H265_DECODER_FORMAT_NONE) {
1017 /* codec_data implies packetized */
1018 if (state->codec_data) {
1019 GST_WARNING_OBJECT (self,
1020 "video/x-h265 caps with codec_data but no stream-format=hev1 or hvc1");
1021 format = GST_H265_DECODER_FORMAT_HEV1;
1023 /* otherwise assume bytestream input */
1024 GST_WARNING_OBJECT (self,
1025 "video/x-h265 caps without codec_data or stream-format");
1026 format = GST_H265_DECODER_FORMAT_BYTE;
1030 if (format == GST_H265_DECODER_FORMAT_HEV1 ||
1031 format == GST_H265_DECODER_FORMAT_HVC1) {
1032 if (!state->codec_data) {
1033 /* Try it with size 4 anyway */
1034 priv->nal_length_size = 4;
1035 GST_WARNING_OBJECT (self,
1036 "packetized format without codec data, assuming nal length size is 4");
1039 /* AVC implies alignment=au */
1040 if (align == GST_H265_DECODER_ALIGN_NONE)
1041 align = GST_H265_DECODER_ALIGN_AU;
1044 if (format == GST_H265_DECODER_FORMAT_BYTE && state->codec_data)
1045 GST_WARNING_OBJECT (self, "bytestream with codec data");
1047 priv->in_format = format;
1048 priv->align = align;
1051 if (state->codec_data) {
1054 gst_buffer_map (state->codec_data, &map, GST_MAP_READ);
1055 if (gst_h265_decoder_parse_codec_data (self, map.data, map.size) !=
1057 /* keep going without error.
1058 * Probably inband SPS/PPS might be valid data */
1059 GST_WARNING_OBJECT (self, "Failed to handle codec data");
1061 gst_buffer_unmap (state->codec_data, &map);
1064 priv->is_live = FALSE;
1065 query = gst_query_new_latency ();
1066 if (gst_pad_peer_query (GST_VIDEO_DECODER_SINK_PAD (self), query))
1067 gst_query_parse_latency (query, &priv->is_live, NULL, NULL);
1068 gst_query_unref (query);
1074 gst_h265_decoder_flush (GstVideoDecoder * decoder)
1076 GstH265Decoder *self = GST_H265_DECODER (decoder);
1078 gst_h265_decoder_clear_dpb (self, TRUE);
1083 static GstFlowReturn
1084 gst_h265_decoder_drain (GstVideoDecoder * decoder)
1086 GstH265Decoder *self = GST_H265_DECODER (decoder);
1088 /* dpb will be cleared by this method */
1089 return gst_h265_decoder_drain_internal (self);
1092 static GstFlowReturn
1093 gst_h265_decoder_finish (GstVideoDecoder * decoder)
1095 return gst_h265_decoder_drain (decoder);
1099 gst_h265_decoder_fill_picture_from_slice (GstH265Decoder * self,
1100 const GstH265Slice * slice, GstH265Picture * picture)
1102 GstH265DecoderPrivate *priv = self->priv;
1103 const GstH265SliceHdr *slice_hdr = &slice->header;
1104 const GstH265NalUnit *nalu = &slice->nalu;
1106 picture->RapPicFlag = slice->rap_pic_flag;
1107 picture->NoRaslOutputFlag = slice->no_rasl_output_flag;
1108 picture->IntraPicFlag = slice->intra_pic_flag;
1109 picture->NoOutputOfPriorPicsFlag = slice->no_output_of_prior_pics_flag;
1110 if (picture->IntraPicFlag) {
1111 priv->associated_irap_NoRaslOutputFlag = picture->NoRaslOutputFlag;
1114 if (GST_H265_IS_NAL_TYPE_RASL (nalu->type) &&
1115 priv->associated_irap_NoRaslOutputFlag) {
1116 picture->output_flag = FALSE;
1118 picture->output_flag = slice_hdr->pic_output_flag;
1124 #define RSV_VCL_N10 10
1125 #define RSV_VCL_N12 12
1126 #define RSV_VCL_N14 14
1129 nal_is_ref (guint8 nal_type)
1131 gboolean ret = FALSE;
1133 case GST_H265_NAL_SLICE_TRAIL_N:
1134 case GST_H265_NAL_SLICE_TSA_N:
1135 case GST_H265_NAL_SLICE_STSA_N:
1136 case GST_H265_NAL_SLICE_RADL_N:
1137 case GST_H265_NAL_SLICE_RASL_N:
1151 gst_h265_decoder_calculate_poc (GstH265Decoder * self,
1152 const GstH265Slice * slice, GstH265Picture * picture)
1154 GstH265DecoderPrivate *priv = self->priv;
1155 const GstH265SliceHdr *slice_hdr = &slice->header;
1156 const GstH265NalUnit *nalu = &slice->nalu;
1157 const GstH265SPS *sps = priv->active_sps;
1158 gint32 MaxPicOrderCntLsb = 1 << (sps->log2_max_pic_order_cnt_lsb_minus4 + 4);
1161 GST_DEBUG_OBJECT (self, "decode PicOrderCntVal");
1163 priv->prev_poc_lsb = priv->poc_lsb;
1164 priv->prev_poc_msb = priv->poc_msb;
1166 is_irap = GST_H265_IS_NAL_TYPE_IRAP (nalu->type);
1168 if (!(is_irap && picture->NoRaslOutputFlag)) {
1169 priv->prev_poc_lsb = priv->prev_tid0pic_poc_lsb;
1170 priv->prev_poc_msb = priv->prev_tid0pic_poc_msb;
1173 /* Finding PicOrderCntMsb */
1174 if (is_irap && picture->NoRaslOutputFlag) {
1178 if ((slice_hdr->pic_order_cnt_lsb < priv->prev_poc_lsb) &&
1179 ((priv->prev_poc_lsb - slice_hdr->pic_order_cnt_lsb) >=
1180 (MaxPicOrderCntLsb / 2)))
1181 priv->poc_msb = priv->prev_poc_msb + MaxPicOrderCntLsb;
1183 else if ((slice_hdr->pic_order_cnt_lsb > priv->prev_poc_lsb) &&
1184 ((slice_hdr->pic_order_cnt_lsb - priv->prev_poc_lsb) >
1185 (MaxPicOrderCntLsb / 2)))
1186 priv->poc_msb = priv->prev_poc_msb - MaxPicOrderCntLsb;
1189 priv->poc_msb = priv->prev_poc_msb;
1193 priv->poc = picture->pic_order_cnt =
1194 priv->poc_msb + slice_hdr->pic_order_cnt_lsb;
1195 priv->poc_lsb = picture->pic_order_cnt_lsb = slice_hdr->pic_order_cnt_lsb;
1197 if (GST_H265_IS_NAL_TYPE_IDR (nalu->type)) {
1198 picture->pic_order_cnt = 0;
1199 picture->pic_order_cnt_lsb = 0;
1202 priv->prev_poc_lsb = 0;
1203 priv->prev_poc_msb = 0;
1204 priv->prev_tid0pic_poc_lsb = 0;
1205 priv->prev_tid0pic_poc_msb = 0;
1208 GST_DEBUG_OBJECT (self,
1209 "PicOrderCntVal %d, (lsb %d)", picture->pic_order_cnt,
1210 picture->pic_order_cnt_lsb);
1212 if (nalu->temporal_id_plus1 == 1 && !GST_H265_IS_NAL_TYPE_RASL (nalu->type) &&
1213 !GST_H265_IS_NAL_TYPE_RADL (nalu->type) && nal_is_ref (nalu->type)) {
1214 priv->prev_tid0pic_poc_lsb = slice_hdr->pic_order_cnt_lsb;
1215 priv->prev_tid0pic_poc_msb = priv->poc_msb;
1222 gst_h265_decoder_set_buffer_flags (GstH265Decoder * self,
1223 GstH265Picture * picture)
1225 GstH265DecoderPrivate *priv = self->priv;
1227 switch (picture->pic_struct) {
1228 case GST_H265_SEI_PIC_STRUCT_FRAME:
1230 case GST_H265_SEI_PIC_STRUCT_TOP_FIELD:
1231 case GST_H265_SEI_PIC_STRUCT_TOP_PAIRED_PREVIOUS_BOTTOM:
1232 case GST_H265_SEI_PIC_STRUCT_TOP_PAIRED_NEXT_BOTTOM:
1233 if (!priv->field_seq_flag) {
1234 GST_FIXME_OBJECT (self,
1235 "top-field with field_seq_flag == 0, what does it mean?");
1237 picture->buffer_flags = GST_VIDEO_BUFFER_FLAG_TOP_FIELD;
1240 case GST_H265_SEI_PIC_STRUCT_BOTTOM_FIELD:
1241 case GST_H265_SEI_PIC_STRUCT_BOTTOM_PAIRED_PREVIOUS_TOP:
1242 case GST_H265_SEI_PIC_STRUCT_BOTTOM_PAIRED_NEXT_TOP:
1243 if (!priv->field_seq_flag) {
1244 GST_FIXME_OBJECT (self,
1245 "bottom-field with field_seq_flag == 0, what does it mean?");
1247 picture->buffer_flags = GST_VIDEO_BUFFER_FLAG_BOTTOM_FIELD;
1250 case GST_H265_SEI_PIC_STRUCT_TOP_BOTTOM:
1251 if (priv->field_seq_flag) {
1252 GST_FIXME_OBJECT (self,
1253 "TFF with field_seq_flag == 1, what does it mean?");
1255 picture->buffer_flags =
1256 GST_VIDEO_BUFFER_FLAG_INTERLACED | GST_VIDEO_BUFFER_FLAG_TFF;
1259 case GST_H265_SEI_PIC_STRUCT_BOTTOM_TOP:
1260 if (priv->field_seq_flag) {
1261 GST_FIXME_OBJECT (self,
1262 "BFF with field_seq_flag == 1, what does it mean?");
1264 picture->buffer_flags = GST_VIDEO_BUFFER_FLAG_INTERLACED;
1268 GST_FIXME_OBJECT (self, "Unhandled picture time SEI pic_struct %d",
1269 picture->pic_struct);
1277 gst_h265_decoder_init_current_picture (GstH265Decoder * self)
1279 GstH265DecoderPrivate *priv = self->priv;
1281 if (!gst_h265_decoder_fill_picture_from_slice (self, &priv->current_slice,
1282 priv->current_picture)) {
1286 if (!gst_h265_decoder_calculate_poc (self,
1287 &priv->current_slice, priv->current_picture))
1290 /* Use picture struct parsed from picture timing SEI */
1291 priv->current_picture->pic_struct = priv->cur_pic_struct;
1292 priv->current_picture->source_scan_type = priv->cur_source_scan_type;
1293 priv->current_picture->duplicate_flag = priv->cur_duplicate_flag;
1294 gst_h265_decoder_set_buffer_flags (self, priv->current_picture);
1300 has_entry_in_rps (GstH265Picture * dpb_pic,
1301 GstH265Picture ** rps_list, guint rps_list_length)
1305 if (!dpb_pic || !rps_list || !rps_list_length)
1308 for (i = 0; i < rps_list_length; i++) {
1309 if (rps_list[i] && rps_list[i]->pic_order_cnt == dpb_pic->pic_order_cnt)
1316 gst_h265_decoder_clear_ref_pic_sets (GstH265Decoder * self)
1320 for (i = 0; i < 16; i++) {
1321 gst_h265_picture_replace (&self->RefPicSetLtCurr[i], NULL);
1322 gst_h265_picture_replace (&self->RefPicSetLtFoll[i], NULL);
1323 gst_h265_picture_replace (&self->RefPicSetStCurrBefore[i], NULL);
1324 gst_h265_picture_replace (&self->RefPicSetStCurrAfter[i], NULL);
1325 gst_h265_picture_replace (&self->RefPicSetStFoll[i], NULL);
1330 gst_h265_decoder_derive_and_mark_rps (GstH265Decoder * self,
1331 GstH265Picture * picture, gint32 * CurrDeltaPocMsbPresentFlag,
1332 gint32 * FollDeltaPocMsbPresentFlag)
1334 GstH265DecoderPrivate *priv = self->priv;
1338 gst_h265_decoder_clear_ref_pic_sets (self);
1341 for (i = 0; i < self->NumPocLtCurr; i++) {
1342 if (!CurrDeltaPocMsbPresentFlag[i]) {
1343 self->RefPicSetLtCurr[i] =
1344 gst_h265_dpb_get_ref_by_poc_lsb (priv->dpb, priv->PocLtCurr[i]);
1346 self->RefPicSetLtCurr[i] =
1347 gst_h265_dpb_get_ref_by_poc (priv->dpb, priv->PocLtCurr[i]);
1351 for (i = 0; i < self->NumPocLtFoll; i++) {
1352 if (!FollDeltaPocMsbPresentFlag[i]) {
1353 self->RefPicSetLtFoll[i] =
1354 gst_h265_dpb_get_ref_by_poc_lsb (priv->dpb, priv->PocLtFoll[i]);
1356 self->RefPicSetLtFoll[i] =
1357 gst_h265_dpb_get_ref_by_poc (priv->dpb, priv->PocLtFoll[i]);
1361 /* Mark all ref pics in RefPicSetLtCurr and RefPicSetLtFol as long_term_refs */
1362 for (i = 0; i < self->NumPocLtCurr; i++) {
1363 if (self->RefPicSetLtCurr[i]) {
1364 self->RefPicSetLtCurr[i]->ref = TRUE;
1365 self->RefPicSetLtCurr[i]->long_term = TRUE;
1369 for (i = 0; i < self->NumPocLtFoll; i++) {
1370 if (self->RefPicSetLtFoll[i]) {
1371 self->RefPicSetLtFoll[i]->ref = TRUE;
1372 self->RefPicSetLtFoll[i]->long_term = TRUE;
1377 for (i = 0; i < self->NumPocStCurrBefore; i++) {
1378 self->RefPicSetStCurrBefore[i] =
1379 gst_h265_dpb_get_short_ref_by_poc (priv->dpb, priv->PocStCurrBefore[i]);
1382 for (i = 0; i < self->NumPocStCurrAfter; i++) {
1383 self->RefPicSetStCurrAfter[i] =
1384 gst_h265_dpb_get_short_ref_by_poc (priv->dpb, priv->PocStCurrAfter[i]);
1387 for (i = 0; i < self->NumPocStFoll; i++) {
1388 self->RefPicSetStFoll[i] =
1389 gst_h265_dpb_get_short_ref_by_poc (priv->dpb, priv->PocStFoll[i]);
1392 /* Mark all dpb pics not beloging to RefPicSet*[] as unused for ref */
1393 dpb_array = gst_h265_dpb_get_pictures_all (priv->dpb);
1394 for (i = 0; i < dpb_array->len; i++) {
1395 GstH265Picture *dpb_pic = g_array_index (dpb_array, GstH265Picture *, i);
1398 !has_entry_in_rps (dpb_pic, self->RefPicSetLtCurr, self->NumPocLtCurr)
1399 && !has_entry_in_rps (dpb_pic, self->RefPicSetLtFoll,
1401 && !has_entry_in_rps (dpb_pic, self->RefPicSetStCurrAfter,
1402 self->NumPocStCurrAfter)
1403 && !has_entry_in_rps (dpb_pic, self->RefPicSetStCurrBefore,
1404 self->NumPocStCurrBefore)
1405 && !has_entry_in_rps (dpb_pic, self->RefPicSetStFoll,
1406 self->NumPocStFoll)) {
1407 GST_LOG_OBJECT (self, "Mark Picture %p (poc %d) as non-ref", dpb_pic,
1408 dpb_pic->pic_order_cnt);
1409 dpb_pic->ref = FALSE;
1410 dpb_pic->long_term = FALSE;
1414 g_array_unref (dpb_array);
1418 gst_h265_decoder_prepare_rps (GstH265Decoder * self, const GstH265Slice * slice,
1419 GstH265Picture * picture)
1421 GstH265DecoderPrivate *priv = self->priv;
1422 gint32 CurrDeltaPocMsbPresentFlag[16] = { 0, };
1423 gint32 FollDeltaPocMsbPresentFlag[16] = { 0, };
1424 const GstH265SliceHdr *slice_hdr = &slice->header;
1425 const GstH265NalUnit *nalu = &slice->nalu;
1426 const GstH265SPS *sps = priv->active_sps;
1427 guint32 MaxPicOrderCntLsb = 1 << (sps->log2_max_pic_order_cnt_lsb_minus4 + 4);
1430 /* if it is an irap pic, set all ref pics in dpb as unused for ref */
1431 if (GST_H265_IS_NAL_TYPE_IRAP (nalu->type) && picture->NoRaslOutputFlag) {
1432 GST_DEBUG_OBJECT (self, "Mark all pictures in DPB as non-ref");
1433 gst_h265_dpb_mark_all_non_ref (priv->dpb);
1436 /* Reset everything for IDR */
1437 if (GST_H265_IS_NAL_TYPE_IDR (nalu->type)) {
1438 memset (priv->PocStCurrBefore, 0, sizeof (priv->PocStCurrBefore));
1439 memset (priv->PocStCurrAfter, 0, sizeof (priv->PocStCurrAfter));
1440 memset (priv->PocStFoll, 0, sizeof (priv->PocStFoll));
1441 memset (priv->PocLtCurr, 0, sizeof (priv->PocLtCurr));
1442 memset (priv->PocLtFoll, 0, sizeof (priv->PocLtFoll));
1443 self->NumPocStCurrBefore = self->NumPocStCurrAfter = self->NumPocStFoll = 0;
1444 self->NumPocLtCurr = self->NumPocLtFoll = 0;
1446 const GstH265ShortTermRefPicSet *stRefPic = NULL;
1447 gint32 num_lt_pics, pocLt;
1448 gint32 PocLsbLt[16] = { 0, };
1449 gint32 UsedByCurrPicLt[16] = { 0, };
1450 gint32 DeltaPocMsbCycleLt[16] = { 0, };
1451 gint numtotalcurr = 0;
1453 /* this is based on CurrRpsIdx described in spec */
1454 if (!slice_hdr->short_term_ref_pic_set_sps_flag)
1455 stRefPic = &slice_hdr->short_term_ref_pic_sets;
1456 else if (sps->num_short_term_ref_pic_sets)
1458 &sps->short_term_ref_pic_set[slice_hdr->short_term_ref_pic_set_idx];
1460 g_assert (stRefPic != NULL);
1462 GST_LOG_OBJECT (self,
1463 "NumDeltaPocs: %d, NumNegativePics: %d, NumPositivePics %d",
1464 stRefPic->NumDeltaPocs, stRefPic->NumNegativePics,
1465 stRefPic->NumPositivePics);
1467 for (i = 0, j = 0, k = 0; i < stRefPic->NumNegativePics; i++) {
1468 if (stRefPic->UsedByCurrPicS0[i]) {
1469 priv->PocStCurrBefore[j++] =
1470 picture->pic_order_cnt + stRefPic->DeltaPocS0[i];
1473 priv->PocStFoll[k++] = picture->pic_order_cnt + stRefPic->DeltaPocS0[i];
1475 self->NumPocStCurrBefore = j;
1476 for (i = 0, j = 0; i < stRefPic->NumPositivePics; i++) {
1477 if (stRefPic->UsedByCurrPicS1[i]) {
1478 priv->PocStCurrAfter[j++] =
1479 picture->pic_order_cnt + stRefPic->DeltaPocS1[i];
1482 priv->PocStFoll[k++] = picture->pic_order_cnt + stRefPic->DeltaPocS1[i];
1484 self->NumPocStCurrAfter = j;
1485 self->NumPocStFoll = k;
1486 num_lt_pics = slice_hdr->num_long_term_sps + slice_hdr->num_long_term_pics;
1487 /* The variables PocLsbLt[i] and UsedByCurrPicLt[i] are derived as follows: */
1488 for (i = 0; i < num_lt_pics; i++) {
1489 if (i < slice_hdr->num_long_term_sps) {
1490 PocLsbLt[i] = sps->lt_ref_pic_poc_lsb_sps[slice_hdr->lt_idx_sps[i]];
1491 UsedByCurrPicLt[i] =
1492 sps->used_by_curr_pic_lt_sps_flag[slice_hdr->lt_idx_sps[i]];
1494 PocLsbLt[i] = slice_hdr->poc_lsb_lt[i];
1495 UsedByCurrPicLt[i] = slice_hdr->used_by_curr_pic_lt_flag[i];
1497 if (UsedByCurrPicLt[i])
1501 self->NumPicTotalCurr = numtotalcurr;
1503 /* The variable DeltaPocMsbCycleLt[i] is derived as follows: (7-38) */
1504 for (i = 0; i < num_lt_pics; i++) {
1505 if (i == 0 || i == slice_hdr->num_long_term_sps)
1506 DeltaPocMsbCycleLt[i] = slice_hdr->delta_poc_msb_cycle_lt[i];
1508 DeltaPocMsbCycleLt[i] =
1509 slice_hdr->delta_poc_msb_cycle_lt[i] + DeltaPocMsbCycleLt[i - 1];
1513 for (i = 0, j = 0, k = 0; i < num_lt_pics; i++) {
1514 pocLt = PocLsbLt[i];
1515 if (slice_hdr->delta_poc_msb_present_flag[i])
1517 picture->pic_order_cnt - DeltaPocMsbCycleLt[i] * MaxPicOrderCntLsb -
1518 slice_hdr->pic_order_cnt_lsb;
1519 if (UsedByCurrPicLt[i]) {
1520 priv->PocLtCurr[j] = pocLt;
1521 CurrDeltaPocMsbPresentFlag[j++] =
1522 slice_hdr->delta_poc_msb_present_flag[i];
1524 priv->PocLtFoll[k] = pocLt;
1525 FollDeltaPocMsbPresentFlag[k++] =
1526 slice_hdr->delta_poc_msb_present_flag[i];
1529 self->NumPocLtCurr = j;
1530 self->NumPocLtFoll = k;
1533 GST_LOG_OBJECT (self, "NumPocStCurrBefore: %d", self->NumPocStCurrBefore);
1534 GST_LOG_OBJECT (self, "NumPocStCurrAfter: %d", self->NumPocStCurrAfter);
1535 GST_LOG_OBJECT (self, "NumPocStFoll: %d", self->NumPocStFoll);
1536 GST_LOG_OBJECT (self, "NumPocLtCurr: %d", self->NumPocLtCurr);
1537 GST_LOG_OBJECT (self, "NumPocLtFoll: %d", self->NumPocLtFoll);
1538 GST_LOG_OBJECT (self, "NumPicTotalCurr: %d", self->NumPicTotalCurr);
1540 /* the derivation process for the RPS and the picture marking */
1541 gst_h265_decoder_derive_and_mark_rps (self, picture,
1542 CurrDeltaPocMsbPresentFlag, FollDeltaPocMsbPresentFlag);
1548 gst_h265_decoder_do_output_picture (GstH265Decoder * self,
1549 GstH265Picture * picture, GstFlowReturn * ret)
1551 GstH265DecoderPrivate *priv = self->priv;
1552 GstVideoCodecFrame *frame = NULL;
1553 GstH265DecoderOutputFrame output_frame;
1554 GstFlowReturn flow_ret = GST_FLOW_OK;
1556 g_assert (ret != NULL);
1558 GST_LOG_OBJECT (self, "Output picture %p (poc %d)", picture,
1559 picture->pic_order_cnt);
1561 if (picture->pic_order_cnt < priv->last_output_poc) {
1562 GST_WARNING_OBJECT (self,
1563 "Outputting out of order %d -> %d, likely a broken stream",
1564 priv->last_output_poc, picture->pic_order_cnt);
1567 priv->last_output_poc = picture->pic_order_cnt;
1569 frame = gst_video_decoder_get_frame (GST_VIDEO_DECODER (self),
1570 picture->system_frame_number);
1573 GST_ERROR_OBJECT (self,
1574 "No available codec frame with frame number %d",
1575 picture->system_frame_number);
1576 UPDATE_FLOW_RETURN (ret, GST_FLOW_ERROR);
1578 gst_h265_picture_unref (picture);
1582 output_frame.frame = frame;
1583 output_frame.picture = picture;
1584 output_frame.self = self;
1585 gst_queue_array_push_tail_struct (priv->output_queue, &output_frame);
1587 gst_h265_decoder_drain_output_queue (self, priv->preferred_output_delay,
1589 UPDATE_FLOW_RETURN (ret, flow_ret);
1593 gst_h265_decoder_clear_dpb (GstH265Decoder * self, gboolean flush)
1595 GstVideoDecoder *decoder = GST_VIDEO_DECODER (self);
1596 GstH265DecoderPrivate *priv = self->priv;
1597 GstH265Picture *picture;
1599 /* If we are not flushing now, videodecoder baseclass will hold
1600 * GstVideoCodecFrame. Release frames manually */
1602 while ((picture = gst_h265_dpb_bump (priv->dpb, TRUE)) != NULL) {
1603 GstVideoCodecFrame *frame = gst_video_decoder_get_frame (decoder,
1604 picture->system_frame_number);
1607 gst_video_decoder_release_frame (decoder, frame);
1608 gst_h265_picture_unref (picture);
1612 gst_queue_array_clear (priv->output_queue);
1613 gst_h265_dpb_clear (priv->dpb);
1614 priv->last_output_poc = G_MININT32;
1617 static GstFlowReturn
1618 gst_h265_decoder_drain_internal (GstH265Decoder * self)
1620 GstH265DecoderPrivate *priv = self->priv;
1621 GstH265Picture *picture;
1622 GstFlowReturn ret = GST_FLOW_OK;
1624 while ((picture = gst_h265_dpb_bump (priv->dpb, TRUE)) != NULL)
1625 gst_h265_decoder_do_output_picture (self, picture, &ret);
1627 gst_h265_decoder_drain_output_queue (self, 0, &ret);
1629 gst_h265_dpb_clear (priv->dpb);
1630 priv->last_output_poc = G_MININT32;
1636 static GstFlowReturn
1637 gst_h265_decoder_dpb_init (GstH265Decoder * self, const GstH265Slice * slice,
1638 GstH265Picture * picture)
1640 GstH265DecoderPrivate *priv = self->priv;
1641 const GstH265SPS *sps = priv->active_sps;
1642 GstH265Picture *to_output;
1643 GstFlowReturn ret = GST_FLOW_OK;
1646 if (slice->clear_dpb) {
1647 if (picture->NoOutputOfPriorPicsFlag) {
1648 GST_DEBUG_OBJECT (self, "Clear dpb");
1649 gst_h265_decoder_drain_output_queue (self, 0, &ret);
1650 gst_h265_decoder_clear_dpb (self, FALSE);
1652 gst_h265_dpb_delete_unused (priv->dpb);
1653 while ((to_output = gst_h265_dpb_bump (priv->dpb, FALSE)) != NULL)
1654 gst_h265_decoder_do_output_picture (self, to_output, &ret);
1656 if (gst_h265_dpb_get_size (priv->dpb) > 0) {
1657 GST_WARNING_OBJECT (self, "IDR or BLA frame failed to clear the dpb, "
1658 "there are still %d pictures in the dpb, last output poc is %d",
1659 gst_h265_dpb_get_size (priv->dpb), priv->last_output_poc);
1661 priv->last_output_poc = G_MININT32;
1665 gst_h265_dpb_delete_unused (priv->dpb);
1666 while (gst_h265_dpb_needs_bump (priv->dpb,
1667 sps->max_num_reorder_pics[sps->max_sub_layers_minus1],
1668 priv->SpsMaxLatencyPictures,
1669 sps->max_dec_pic_buffering_minus1[sps->max_sub_layers_minus1] +
1671 to_output = gst_h265_dpb_bump (priv->dpb, FALSE);
1673 /* Something wrong... */
1675 GST_WARNING_OBJECT (self, "Bumping is needed but no picture to output");
1679 gst_h265_decoder_do_output_picture (self, to_output, &ret);
1686 static GstFlowReturn
1687 gst_h265_decoder_start_current_picture (GstH265Decoder * self)
1689 GstH265DecoderClass *klass;
1690 GstH265DecoderPrivate *priv = self->priv;
1691 GstFlowReturn ret = GST_FLOW_OK;
1693 g_assert (priv->current_picture != NULL);
1694 g_assert (priv->active_sps != NULL);
1695 g_assert (priv->active_pps != NULL);
1697 if (!gst_h265_decoder_init_current_picture (self))
1698 return GST_FLOW_ERROR;
1700 /* Drop all RASL pictures having NoRaslOutputFlag is TRUE for the
1701 * associated IRAP picture */
1702 if (GST_H265_IS_NAL_TYPE_RASL (priv->current_slice.nalu.type) &&
1703 priv->associated_irap_NoRaslOutputFlag) {
1704 GST_DEBUG_OBJECT (self, "Drop current picture");
1705 gst_h265_picture_replace (&priv->current_picture, NULL);
1709 gst_h265_decoder_prepare_rps (self, &priv->current_slice,
1710 priv->current_picture);
1712 ret = gst_h265_decoder_dpb_init (self,
1713 &priv->current_slice, priv->current_picture);
1714 if (ret != GST_FLOW_OK) {
1715 GST_WARNING_OBJECT (self, "Failed to init dpb");
1719 klass = GST_H265_DECODER_GET_CLASS (self);
1720 if (klass->start_picture) {
1721 ret = klass->start_picture (self, priv->current_picture,
1722 &priv->current_slice, priv->dpb);
1724 if (ret != GST_FLOW_OK) {
1725 GST_WARNING_OBJECT (self, "subclass does not want to start picture");
1734 gst_h265_decoder_finish_picture (GstH265Decoder * self,
1735 GstH265Picture * picture, GstFlowReturn * ret)
1737 GstVideoDecoder *decoder = GST_VIDEO_DECODER (self);
1738 GstH265DecoderPrivate *priv = self->priv;
1739 const GstH265SPS *sps = priv->active_sps;
1741 g_assert (ret != NULL);
1743 GST_LOG_OBJECT (self,
1744 "Finishing picture %p (poc %d), entries in DPB %d",
1745 picture, picture->pic_order_cnt, gst_h265_dpb_get_size (priv->dpb));
1747 gst_h265_dpb_delete_unused (priv->dpb);
1749 /* This picture is decode only, drop corresponding frame */
1750 if (!picture->output_flag) {
1751 GstVideoCodecFrame *frame = gst_video_decoder_get_frame (decoder,
1752 picture->system_frame_number);
1754 gst_video_decoder_release_frame (decoder, frame);
1757 /* gst_h265_dpb_add() will take care of pic_latency_cnt increment and
1758 * reference picture marking for this picture */
1759 gst_h265_dpb_add (priv->dpb, picture);
1761 /* NOTE: As per C.5.2.2, bumping by sps_max_dec_pic_buffering_minus1 is
1762 * applied only for the output and removal of pictures from the DPB before
1763 * the decoding of the current picture. So pass zero here */
1764 while (gst_h265_dpb_needs_bump (priv->dpb,
1765 sps->max_num_reorder_pics[sps->max_sub_layers_minus1],
1766 priv->SpsMaxLatencyPictures, 0)) {
1767 GstH265Picture *to_output = gst_h265_dpb_bump (priv->dpb, FALSE);
1769 /* Something wrong... */
1771 GST_WARNING_OBJECT (self, "Bumping is needed but no picture to output");
1775 gst_h265_decoder_do_output_picture (self, to_output, ret);
1780 gst_h265_decoder_finish_current_picture (GstH265Decoder * self,
1781 GstFlowReturn * ret)
1783 GstH265DecoderPrivate *priv = self->priv;
1784 GstH265DecoderClass *klass;
1785 GstFlowReturn flow_ret = GST_FLOW_OK;
1787 g_assert (ret != NULL);
1789 if (!priv->current_picture)
1792 klass = GST_H265_DECODER_GET_CLASS (self);
1794 if (klass->end_picture) {
1795 flow_ret = klass->end_picture (self, priv->current_picture);
1796 if (flow_ret != GST_FLOW_OK) {
1797 GST_WARNING_OBJECT (self, "End picture failed");
1799 /* continue to empty dpb */
1800 UPDATE_FLOW_RETURN (ret, flow_ret);
1804 /* finish picture takes ownership of the picture */
1805 gst_h265_decoder_finish_picture (self, priv->current_picture, &flow_ret);
1806 priv->current_picture = NULL;
1808 UPDATE_FLOW_RETURN (ret, flow_ret);
1812 gst_h265_decoder_reset_frame_state (GstH265Decoder * self)
1814 GstH265DecoderPrivate *priv = self->priv;
1816 /* Clear picture struct information */
1817 priv->cur_pic_struct = GST_H265_SEI_PIC_STRUCT_FRAME;
1818 priv->cur_source_scan_type = 2;
1819 priv->cur_duplicate_flag = 0;
1820 priv->no_output_of_prior_pics_flag = FALSE;
1821 priv->current_frame = NULL;
1822 g_array_set_size (priv->nalu, 0);
1825 static GstFlowReturn
1826 gst_h265_decoder_handle_frame (GstVideoDecoder * decoder,
1827 GstVideoCodecFrame * frame)
1829 GstH265Decoder *self = GST_H265_DECODER (decoder);
1830 GstH265DecoderPrivate *priv = self->priv;
1831 GstBuffer *in_buf = frame->input_buffer;
1832 GstH265NalUnit nalu;
1833 GstH265ParserResult pres;
1835 GstFlowReturn decode_ret = GST_FLOW_OK;
1838 GST_LOG_OBJECT (self,
1839 "handle frame, PTS: %" GST_TIME_FORMAT ", DTS: %"
1840 GST_TIME_FORMAT, GST_TIME_ARGS (GST_BUFFER_PTS (in_buf)),
1841 GST_TIME_ARGS (GST_BUFFER_DTS (in_buf)));
1843 gst_h265_decoder_reset_frame_state (self);
1845 priv->current_frame = frame;
1847 if (!gst_buffer_map (in_buf, &map, GST_MAP_READ)) {
1848 GST_ELEMENT_ERROR (self, RESOURCE, READ,
1849 ("Failed to map memory for reading"), (NULL));
1850 return GST_FLOW_ERROR;
1853 if (priv->in_format == GST_H265_DECODER_FORMAT_HVC1 ||
1854 priv->in_format == GST_H265_DECODER_FORMAT_HEV1) {
1855 pres = gst_h265_parser_identify_nalu_hevc (priv->parser,
1856 map.data, 0, map.size, priv->nal_length_size, &nalu);
1858 while (pres == GST_H265_PARSER_OK) {
1859 pres = gst_h265_decoder_parse_nalu (self, &nalu);
1860 if (pres != GST_H265_PARSER_OK)
1863 pres = gst_h265_parser_identify_nalu_hevc (priv->parser,
1864 map.data, nalu.offset + nalu.size, map.size, priv->nal_length_size,
1868 pres = gst_h265_parser_identify_nalu (priv->parser,
1869 map.data, 0, map.size, &nalu);
1871 if (pres == GST_H265_PARSER_NO_NAL_END)
1872 pres = GST_H265_PARSER_OK;
1874 while (pres == GST_H265_PARSER_OK) {
1875 pres = gst_h265_decoder_parse_nalu (self, &nalu);
1876 if (pres != GST_H265_PARSER_OK)
1879 pres = gst_h265_parser_identify_nalu (priv->parser,
1880 map.data, nalu.offset + nalu.size, map.size, &nalu);
1881 if (pres == GST_H265_PARSER_NO_NAL_END)
1882 pres = GST_H265_PARSER_OK;
1886 for (i = 0; i < priv->nalu->len && decode_ret == GST_FLOW_OK; i++) {
1887 GstH265DecoderNalUnit *decoder_nalu =
1888 &g_array_index (priv->nalu, GstH265DecoderNalUnit, i);
1889 decode_ret = gst_h265_decoder_decode_nalu (self, decoder_nalu);
1892 gst_buffer_unmap (in_buf, &map);
1893 gst_h265_decoder_reset_frame_state (self);
1895 if (decode_ret != GST_FLOW_OK) {
1896 if (decode_ret == GST_FLOW_ERROR) {
1897 GST_VIDEO_DECODER_ERROR (self, 1, STREAM, DECODE,
1898 ("Failed to decode data"), (NULL), decode_ret);
1901 gst_video_decoder_drop_frame (decoder, frame);
1902 gst_clear_h265_picture (&priv->current_picture);
1907 if (priv->current_picture) {
1908 gst_h265_decoder_finish_current_picture (self, &decode_ret);
1909 gst_video_codec_frame_unref (frame);
1911 /* This picture was dropped */
1912 gst_video_decoder_release_frame (decoder, frame);
1915 if (decode_ret == GST_FLOW_ERROR) {
1916 GST_VIDEO_DECODER_ERROR (self, 1, STREAM, DECODE,
1917 ("Failed to decode data"), (NULL), decode_ret);
1924 gst_h265_decoder_clear_nalu (GstH265DecoderNalUnit * nalu)
1929 memset (nalu, 0, sizeof (GstH265DecoderNalUnit));
1933 * gst_h265_decoder_set_process_ref_pic_lists:
1934 * @decoder: a #GstH265Decoder
1935 * @process: whether subclass is requiring reference picture modification process
1937 * Called to en/disable reference picture modification process.
1942 gst_h265_decoder_set_process_ref_pic_lists (GstH265Decoder * decoder,
1945 decoder->priv->process_ref_pic_lists = process;
1949 * gst_h265_decoder_get_picture:
1950 * @decoder: a #GstH265Decoder
1951 * @system_frame_number: a target system frame number of #GstH265Picture
1953 * Retrive DPB and return a #GstH265Picture corresponding to
1954 * the @system_frame_number
1956 * Returns: (transfer full): a #GstH265Picture if successful, or %NULL otherwise
1961 gst_h265_decoder_get_picture (GstH265Decoder * decoder,
1962 guint32 system_frame_number)
1964 return gst_h265_dpb_get_picture (decoder->priv->dpb, system_frame_number);