h264decoder,h265decoder: Do not hold codec_data buffer
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-bad / gst-libs / gst / codecs / gsth265decoder.c
1 /* GStreamer
2  * Copyright (C) 2015 Intel Corporation
3  *    Author: Sreerenj Balachandran <sreerenj.balachandran@intel.com>
4  * Copyright (C) 2019 Seungha Yang <seungha.yang@navercorp.com>
5  *
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.
10  *
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.
15  *
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.
20  */
21 /**
22  * SECTION:gsth265decoder
23  * @title: GstH265Decoder
24  * @short_description: Base class to implement stateless H.265 decoders
25  * @sources:
26  * - gsth265picture.h
27  */
28
29 #ifdef HAVE_CONFIG_H
30 #include <config.h>
31 #endif
32
33 #include <gst/base/base.h>
34 #include "gsth265decoder.h"
35
36 GST_DEBUG_CATEGORY (gst_h265_decoder_debug);
37 #define GST_CAT_DEFAULT gst_h265_decoder_debug
38
39 typedef enum
40 {
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;
46
47 typedef enum
48 {
49   GST_H265_DECODER_ALIGN_NONE,
50   GST_H265_DECODER_ALIGN_NAL,
51   GST_H265_DECODER_ALIGN_AU
52 } GstH265DecoderAlign;
53
54 struct _GstH265DecoderPrivate
55 {
56   gint width, height;
57
58   guint8 conformance_window_flag;
59   gint crop_rect_width;
60   gint crop_rect_height;
61   gint crop_rect_x;
62   gint crop_rect_y;
63
64   guint nal_length_size;
65
66   /* state */
67   GstH265DecoderFormat in_format;
68   GstH265DecoderAlign align;
69   GstH265Parser *parser;
70   GstH265Dpb *dpb;
71
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;
78
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;
83
84   gboolean no_output_of_prior_pics_flag;
85
86   /* vps/sps/pps of the current slice */
87   const GstH265VPS *active_vps;
88   const GstH265SPS *active_sps;
89   const GstH265PPS *active_pps;
90
91   guint32 SpsMaxLatencyPictures;
92
93   /* Picture currently being processed/decoded */
94   GstH265Picture *current_picture;
95   GstVideoCodecFrame *current_frame;
96
97   /* Slice (slice header + nalu) currently being processed/decoded */
98   GstH265Slice current_slice;
99   GstH265Slice prev_slice;
100   GstH265Slice prev_independent_slice;
101
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];
114
115   /* PicOrderCount of the previously outputted frame */
116   gint last_output_poc;
117
118   gboolean associated_irap_NoRaslOutputFlag;
119   gboolean new_bitstream;
120   gboolean prev_nal_is_eos;
121
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;
127
128   GArray *nalu;
129
130   /* For delayed output */
131   guint preferred_output_delay;
132   gboolean is_live;
133   GstQueueArray *output_queue;
134 };
135
136 typedef struct
137 {
138   union
139   {
140     GstH265SPS sps;
141     GstH265Slice slice;
142   } unit;
143   gboolean is_slice;
144 } GstH265DecoderNalUnit;
145
146 typedef struct
147 {
148   /* Holds ref */
149   GstVideoCodecFrame *frame;
150   GstH265Picture *picture;
151   /* Without ref */
152   GstH265Decoder *self;
153 } GstH265DecoderOutputFrame;
154
155 #define UPDATE_FLOW_RETURN(ret,new_ret) G_STMT_START { \
156   if (*(ret) == GST_FLOW_OK) \
157     *(ret) = new_ret; \
158 } G_STMT_END
159
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"));
166
167 static void gst_h265_decoder_finalize (GObject * object);
168
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);
178
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);
184 static GstFlowReturn
185 gst_h265_decoder_start_current_picture (GstH265Decoder * self);
186 static void gst_h265_decoder_clear_nalu (GstH265DecoderNalUnit * nalu);
187 static void
188 gst_h265_decoder_clear_output_frame (GstH265DecoderOutputFrame * output_frame);
189
190 static void
191 gst_h265_decoder_class_init (GstH265DecoderClass * klass)
192 {
193   GstVideoDecoderClass *decoder_class = GST_VIDEO_DECODER_CLASS (klass);
194   GObjectClass *object_class = G_OBJECT_CLASS (klass);
195
196   object_class->finalize = GST_DEBUG_FUNCPTR (gst_h265_decoder_finalize);
197
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);
206 }
207
208 static void
209 gst_h265_decoder_init (GstH265Decoder * self)
210 {
211   GstH265DecoderPrivate *priv;
212
213   gst_video_decoder_set_packetized (GST_VIDEO_DECODER (self), TRUE);
214
215   self->priv = priv = gst_h265_decoder_get_instance_private (self);
216
217   priv->last_output_poc = G_MININT32;
218
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),
226       8);
227   g_array_set_clear_func (priv->nalu,
228       (GDestroyNotify) gst_h265_decoder_clear_nalu);
229   priv->output_queue =
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);
233 }
234
235 static void
236 gst_h265_decoder_finalize (GObject * object)
237 {
238   GstH265Decoder *self = GST_H265_DECODER (object);
239   GstH265DecoderPrivate *priv = self->priv;
240
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);
246
247   G_OBJECT_CLASS (parent_class)->finalize (object);
248 }
249
250 static gboolean
251 gst_h265_decoder_start (GstVideoDecoder * decoder)
252 {
253   GstH265Decoder *self = GST_H265_DECODER (decoder);
254   GstH265DecoderPrivate *priv = self->priv;
255
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;
260
261   return TRUE;
262 }
263
264 static gboolean
265 gst_h265_decoder_stop (GstVideoDecoder * decoder)
266 {
267   GstH265Decoder *self = GST_H265_DECODER (decoder);
268   GstH265DecoderPrivate *priv = self->priv;
269
270   if (self->input_state) {
271     gst_video_codec_state_unref (self->input_state);
272     self->input_state = NULL;
273   }
274
275   if (priv->parser) {
276     gst_h265_parser_free (priv->parser);
277     priv->parser = NULL;
278   }
279
280   if (priv->dpb) {
281     gst_h265_dpb_free (priv->dpb);
282     priv->dpb = NULL;
283   }
284
285   gst_h265_decoder_clear_ref_pic_sets (self);
286
287   return TRUE;
288 }
289
290 static void
291 gst_h265_decoder_clear_output_frame (GstH265DecoderOutputFrame * output_frame)
292 {
293   if (!output_frame)
294     return;
295
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;
300   }
301
302   gst_clear_h265_picture (&output_frame->picture);
303 }
304
305 static gboolean
306 gst_h265_decoder_is_crop_rect_changed (GstH265Decoder * self, GstH265SPS * sps)
307 {
308   GstH265DecoderPrivate *priv = self->priv;
309
310   if (priv->conformance_window_flag != sps->conformance_window_flag)
311     return TRUE;
312   if (priv->crop_rect_width != sps->crop_rect_width)
313     return TRUE;
314   if (priv->crop_rect_height != sps->crop_rect_height)
315     return TRUE;
316   if (priv->crop_rect_x != sps->crop_rect_x)
317     return TRUE;
318   if (priv->crop_rect_y != sps->crop_rect_y)
319     return TRUE;
320
321   return FALSE;
322 }
323
324 static void
325 gst_h265_decoder_drain_output_queue (GstH265Decoder * self, guint num,
326     GstFlowReturn * ret)
327 {
328   GstH265DecoderPrivate *priv = self->priv;
329   GstH265DecoderClass *klass = GST_H265_DECODER_GET_CLASS (self);
330
331   g_assert (klass->output_picture);
332   g_assert (ret != NULL);
333
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);
339
340     UPDATE_FLOW_RETURN (ret, flow_ret);
341   }
342 }
343
344 static GstFlowReturn
345 gst_h265_decoder_process_sps (GstH265Decoder * self, GstH265SPS * sps)
346 {
347   GstH265DecoderPrivate *priv = self->priv;
348   gint max_dpb_size;
349   gint prev_max_dpb_size;
350   gint MaxLumaPS;
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;
357
358   /* A.4.1 */
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;
367   else
368     max_dpb_size = MaxDpbPicBuf;
369
370   max_dpb_size = MIN (max_dpb_size, 16);
371
372   if (sps->vui_parameters_present_flag)
373     field_seq_flag = sps->vui_params.field_seq_flag;
374
375   progressive_source_flag = sps->profile_tier_level.progressive_source_flag;
376   interlaced_source_flag = sps->profile_tier_level.interlaced_source_flag;
377
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);
386
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);
395
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);
399     } else {
400       ret = gst_h265_decoder_drain_internal (self);
401     }
402
403     if (ret != GST_FLOW_OK)
404       return ret;
405
406     if (klass->get_preferred_output_delay) {
407       priv->preferred_output_delay =
408           klass->get_preferred_output_delay (self, priv->is_live);
409     } else {
410       priv->preferred_output_delay = 0;
411     }
412
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");
418       return ret;
419     }
420
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;
431
432     gst_h265_dpb_set_max_num_pics (priv->dpb, max_dpb_size);
433   }
434
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;
439   }
440
441   GST_DEBUG_OBJECT (self, "Set DPB max size %d", max_dpb_size);
442
443   return GST_FLOW_OK;
444 }
445
446 static GstH265ParserResult
447 gst_h265_decoder_parse_sei (GstH265Decoder * self, GstH265NalUnit * nalu)
448 {
449   GstH265DecoderPrivate *priv = self->priv;
450   GstH265ParserResult pres;
451   GArray *messages = NULL;
452   guint i;
453
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);
457
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;
462   }
463
464   for (i = 0; i < messages->len; i++) {
465     GstH265SEIMessage *sei = &g_array_index (messages, GstH265SEIMessage, i);
466
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;
472
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);
477         break;
478       default:
479         break;
480     }
481   }
482
483   g_array_free (messages, TRUE);
484   GST_LOG_OBJECT (self, "SEI parsed");
485
486   return GST_H265_PARSER_OK;
487 }
488
489 static void
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)
493 {
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;
499   GArray *tmp_refs;
500   gint num_tmp_refs, i;
501
502   *ref_pic_list0 = priv->ref_pic_list0;
503   *ref_pic_list1 = priv->ref_pic_list1;
504
505   /* There is nothing to be done for I slices */
506   if (GST_H265_IS_I_SLICE (&slice->header))
507     return;
508
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.");
514     return;
515   }
516
517   /* 8.3.4 Deriving l0 */
518   tmp_refs = priv->ref_pic_list_tmp;
519
520   /* (8-8)
521    * Deriving l0 consist of appending in loop RefPicSetStCurrBefore,
522    * RefPicSetStCurrAfter and RefPicSetLtCurr until NumRpsCurrTempList0 item
523    * has been reached.
524    */
525
526   /* NumRpsCurrTempList0 */
527   num_tmp_refs = MAX (slice->header.num_ref_idx_l0_active_minus1 + 1,
528       self->NumPicTotalCurr);
529
530   while (tmp_refs->len < num_tmp_refs) {
531     for (i = 0; i < self->NumPocStCurrBefore && tmp_refs->len < num_tmp_refs;
532         i++)
533       g_array_append_val (tmp_refs, self->RefPicSetStCurrBefore[i]);
534     for (i = 0; i < self->NumPocStCurrAfter && tmp_refs->len < num_tmp_refs;
535         i++)
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);
541   }
542
543   /* (8-9)
544    * If needed, apply the modificaiton base on the lookup table found in the
545    * slice header (list_entry_l0).
546    */
547   for (i = 0; i <= slice->header.num_ref_idx_l0_active_minus1; i++) {
548     GstH265Picture **tmp = (GstH265Picture **) tmp_refs->data;
549
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]]);
552     else
553       g_array_append_val (*ref_pic_list0, tmp[i]);
554   }
555
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;
561   }
562
563   g_array_set_size (tmp_refs, 0);
564
565   /* For P slices we only need l0 */
566   if (GST_H265_IS_P_SLICE (&slice->header))
567     return;
568
569   /* 8.3.4 Deriving l1 */
570   /* (8-10)
571    * Deriving l1 consist of appending in loop RefPicSetStCurrAfter,
572    * RefPicSetStCurrBefore and RefPicSetLtCurr until NumRpsCurrTempList0 item
573    * has been reached.
574    */
575
576   /* NumRpsCurrTempList1 */
577   num_tmp_refs = MAX (slice->header.num_ref_idx_l1_active_minus1 + 1,
578       self->NumPicTotalCurr);
579
580   while (tmp_refs->len < num_tmp_refs) {
581     for (i = 0; i < self->NumPocStCurrAfter && tmp_refs->len < num_tmp_refs;
582         i++)
583       g_array_append_val (tmp_refs, self->RefPicSetStCurrAfter[i]);
584     for (i = 0; i < self->NumPocStCurrBefore && tmp_refs->len < num_tmp_refs;
585         i++)
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);
591   }
592
593   /* (8-11)
594    * If needed, apply the modificaiton base on the lookup table found in the
595    * slice header (list_entry_l1).
596    */
597   for (i = 0; i <= slice->header.num_ref_idx_l1_active_minus1; i++) {
598     GstH265Picture **tmp = (GstH265Picture **) tmp_refs->data;
599
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]]);
602     else
603       g_array_append_val (*ref_pic_list1, tmp[i]);
604   }
605
606   g_array_set_size (tmp_refs, 0);
607 }
608
609 static GstFlowReturn
610 gst_h265_decoder_decode_slice (GstH265Decoder * self)
611 {
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;
616   GArray *l0 = NULL;
617   GArray *l1 = NULL;
618   GstFlowReturn ret = GST_FLOW_OK;
619
620   if (!picture) {
621     GST_ERROR_OBJECT (self, "No current picture");
622     return GST_FLOW_ERROR;
623   }
624
625   g_assert (klass->decode_slice);
626
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);
631   }
632
633   ret = klass->decode_slice (self, picture, slice, l0, l1);
634
635   if (priv->process_ref_pic_lists) {
636     g_array_set_size (l0, 0);
637     g_array_set_size (l1, 0);
638   }
639
640   return ret;
641 }
642
643 static GstFlowReturn
644 gst_h265_decoder_preprocess_slice (GstH265Decoder * self, GstH265Slice * slice)
645 {
646   GstH265DecoderPrivate *priv = self->priv;
647   const GstH265SliceHdr *slice_hdr = &slice->header;
648
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;
654   }
655
656   return GST_FLOW_OK;
657 }
658
659 static GstFlowReturn
660 gst_h265_decoder_process_slice (GstH265Decoder * self, GstH265Slice * slice)
661 {
662   GstH265DecoderPrivate *priv = self->priv;
663   GstFlowReturn ret = GST_FLOW_OK;
664
665   priv->current_slice = *slice;
666
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;
670
671     memcpy (&slice_hdr->type, &indep_slice_hdr->type,
672         G_STRUCT_OFFSET (GstH265SliceHdr, num_entry_point_offsets) -
673         G_STRUCT_OFFSET (GstH265SliceHdr, type));
674   } else {
675     priv->prev_independent_slice = priv->current_slice;
676     memset (&priv->prev_independent_slice.nalu, 0, sizeof (GstH265NalUnit));
677   }
678
679   ret = gst_h265_decoder_preprocess_slice (self, &priv->current_slice);
680   if (ret != GST_FLOW_OK)
681     return ret;
682
683   priv->active_pps = priv->current_slice.header.pps;
684   priv->active_sps = priv->active_pps->sps;
685
686   if (!priv->current_picture) {
687     GstH265DecoderClass *klass = GST_H265_DECODER_GET_CLASS (self);
688     GstH265Picture *picture;
689     GstFlowReturn ret = GST_FLOW_OK;
690
691     g_assert (priv->current_frame);
692
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;
696
697     priv->current_picture = picture;
698
699     if (klass->new_picture)
700       ret = klass->new_picture (self, priv->current_frame, picture);
701
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);
706       return ret;
707     }
708
709     ret = gst_h265_decoder_start_current_picture (self);
710     if (ret != GST_FLOW_OK) {
711       GST_WARNING_OBJECT (self, "start picture failed");
712       return ret;
713     }
714
715     /* this picture was dropped */
716     if (!priv->current_picture)
717       return GST_FLOW_OK;
718   }
719
720   return gst_h265_decoder_decode_slice (self);
721 }
722
723 static GstH265ParserResult
724 gst_h265_decoder_parse_slice (GstH265Decoder * self, GstH265NalUnit * nalu)
725 {
726   GstH265DecoderPrivate *priv = self->priv;
727   GstH265ParserResult pres;
728   GstH265Slice slice;
729   GstH265DecoderNalUnit decoder_nalu;
730
731   memset (&slice, 0, sizeof (GstH265Slice));
732
733   pres = gst_h265_parser_parse_slice_hdr (priv->parser, nalu, &slice.header);
734   if (pres != GST_H265_PARSER_OK)
735     return pres;
736
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
740    */
741   gst_h265_slice_hdr_free (&priv->current_slice.header);
742   slice.nalu = *nalu;
743
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;
747   }
748
749   /* NoRaslOutputFlag == 1 if the current picture is
750    * 1) an IDR picture
751    * 2) a BLA picture
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 )
755    */
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;
761   }
762
763   if (GST_H265_IS_NAL_TYPE_IRAP (nalu->type)) {
764     slice.intra_pic_flag = TRUE;
765
766     if (slice.no_rasl_output_flag && !priv->new_bitstream) {
767       /* C 3.2 */
768       slice.clear_dpb = TRUE;
769       if (nalu->type == GST_H265_NAL_SLICE_CRA_NUT) {
770         slice.no_output_of_prior_pics_flag = TRUE;
771       } else {
772         slice.no_output_of_prior_pics_flag =
773             slice.header.no_output_of_prior_pics_flag;
774       }
775     }
776   }
777
778   if (slice.no_output_of_prior_pics_flag)
779     priv->no_output_of_prior_pics_flag = TRUE;
780
781   decoder_nalu.unit.slice = slice;
782   decoder_nalu.is_slice = TRUE;
783   g_array_append_val (priv->nalu, decoder_nalu);
784
785   return GST_H265_PARSER_OK;
786 }
787
788 static GstH265ParserResult
789 gst_h265_decoder_parse_nalu (GstH265Decoder * self, GstH265NalUnit * nalu)
790 {
791   GstH265DecoderPrivate *priv = self->priv;
792   GstH265VPS vps;
793   GstH265SPS sps;
794   GstH265PPS pps;
795   GstH265ParserResult ret = GST_H265_PARSER_OK;
796   GstH265DecoderNalUnit decoder_nalu;
797
798   GST_LOG_OBJECT (self, "Parsed nal type: %d, offset %d, size %d",
799       nalu->type, nalu->offset, nalu->size);
800
801   switch (nalu->type) {
802     case GST_H265_NAL_VPS:
803       ret = gst_h265_parser_parse_vps (priv->parser, nalu, &vps);
804       break;
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)
808         break;
809
810       memset (&decoder_nalu, 0, sizeof (GstH265DecoderNalUnit));
811       decoder_nalu.unit.sps = sps;
812       g_array_append_val (priv->nalu, decoder_nalu);
813       break;
814     case GST_H265_NAL_PPS:
815       ret = gst_h265_parser_parse_pps (priv->parser, nalu, &pps);
816       break;
817     case GST_H265_NAL_PREFIX_SEI:
818     case GST_H265_NAL_SUFFIX_SEI:
819       ret = gst_h265_decoder_parse_sei (self, nalu);
820       break;
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;
840       break;
841     case GST_H265_NAL_EOB:
842       priv->new_bitstream = TRUE;
843       break;
844     case GST_H265_NAL_EOS:
845       priv->prev_nal_is_eos = TRUE;
846       break;
847     default:
848       break;
849   }
850
851   return ret;
852 }
853
854 static GstFlowReturn
855 gst_h265_decoder_decode_nalu (GstH265Decoder * self,
856     GstH265DecoderNalUnit * nalu)
857 {
858   if (!nalu->is_slice)
859     return gst_h265_decoder_process_sps (self, &nalu->unit.sps);
860
861   return gst_h265_decoder_process_slice (self, &nalu->unit.slice);
862 }
863
864 static void
865 gst_h265_decoder_format_from_caps (GstH265Decoder * self, GstCaps * caps,
866     GstH265DecoderFormat * format, GstH265DecoderAlign * align)
867 {
868   if (format)
869     *format = GST_H265_DECODER_FORMAT_NONE;
870
871   if (align)
872     *align = GST_H265_DECODER_ALIGN_NONE;
873
874   if (!gst_caps_is_fixed (caps)) {
875     GST_WARNING_OBJECT (self, "Caps wasn't fixed");
876     return;
877   }
878
879   GST_DEBUG_OBJECT (self, "parsing caps: %" GST_PTR_FORMAT, caps);
880
881   if (caps && gst_caps_get_size (caps) > 0) {
882     GstStructure *s = gst_caps_get_structure (caps, 0);
883     const gchar *str = NULL;
884
885     if (format) {
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;
893       }
894     }
895
896     if (align) {
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;
902       }
903     }
904   }
905 }
906
907 static GstFlowReturn
908 gst_h265_decoder_parse_codec_data (GstH265Decoder * self, const guint8 * data,
909     gsize size)
910 {
911   GstH265DecoderPrivate *priv = self->priv;
912   guint num_nal_arrays;
913   guint off;
914   guint num_nals, i, j;
915   GstH265ParserResult pres;
916   GstH265NalUnit nalu;
917   GstFlowReturn ret = GST_FLOW_OK;
918   GstH265VPS vps;
919   GstH265SPS sps;
920   GstH265PPS pps;
921
922   /* parse the hvcC data */
923   if (size < 23) {
924     GST_WARNING_OBJECT (self, "hvcC too small");
925     return GST_FLOW_ERROR;
926   }
927
928   /* wrong hvcC version */
929   if (data[0] != 0 && data[0] != 1) {
930     return GST_FLOW_ERROR;
931   }
932
933   priv->nal_length_size = (data[21] & 0x03) + 1;
934   GST_DEBUG_OBJECT (self, "nal length size %u", priv->nal_length_size);
935
936   num_nal_arrays = data[22];
937   off = 23;
938
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;
943     }
944
945     num_nals = GST_READ_UINT16_BE (data + off + 1);
946     off += 3;
947     for (j = 0; j < num_nals; j++) {
948       pres = gst_h265_parser_identify_nalu_hevc (priv->parser,
949           data, off, size, 2, &nalu);
950
951       if (pres != GST_H265_PARSER_OK) {
952         GST_WARNING_OBJECT (self, "hvcC too small");
953         return GST_FLOW_ERROR;
954       }
955
956       switch (nalu.type) {
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;
962           }
963           break;
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;
969           }
970
971           ret = gst_h265_decoder_process_sps (self, &sps);
972           if (ret != GST_FLOW_OK) {
973             GST_WARNING_OBJECT (self, "Failed to process SPS");
974             return ret;
975           }
976           break;
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;
982           }
983           break;
984         default:
985           break;
986       }
987
988       off = nalu.offset + nalu.size;
989     }
990   }
991
992   return GST_FLOW_OK;
993 }
994
995 static gboolean
996 gst_h265_decoder_set_format (GstVideoDecoder * decoder,
997     GstVideoCodecState * state)
998 {
999   GstH265Decoder *self = GST_H265_DECODER (decoder);
1000   GstH265DecoderPrivate *priv = self->priv;
1001   GstQuery *query;
1002
1003   GST_DEBUG_OBJECT (decoder, "Set format");
1004
1005   if (self->input_state)
1006     gst_video_codec_state_unref (self->input_state);
1007
1008   self->input_state = gst_video_codec_state_ref (state);
1009
1010   if (state->caps) {
1011     GstH265DecoderFormat format;
1012     GstH265DecoderAlign align;
1013
1014     gst_h265_decoder_format_from_caps (self, state->caps, &format, &align);
1015
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;
1022       } else {
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;
1027       }
1028     }
1029
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");
1037       }
1038
1039       /* AVC implies alignment=au */
1040       if (align == GST_H265_DECODER_ALIGN_NONE)
1041         align = GST_H265_DECODER_ALIGN_AU;
1042     }
1043
1044     if (format == GST_H265_DECODER_FORMAT_BYTE && state->codec_data)
1045       GST_WARNING_OBJECT (self, "bytestream with codec data");
1046
1047     priv->in_format = format;
1048     priv->align = align;
1049   }
1050
1051   if (state->codec_data) {
1052     GstMapInfo map;
1053
1054     gst_buffer_map (state->codec_data, &map, GST_MAP_READ);
1055     if (gst_h265_decoder_parse_codec_data (self, map.data, map.size) !=
1056         GST_FLOW_OK) {
1057       /* keep going without error.
1058        * Probably inband SPS/PPS might be valid data */
1059       GST_WARNING_OBJECT (self, "Failed to handle codec data");
1060     }
1061     gst_buffer_unmap (state->codec_data, &map);
1062   }
1063
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);
1069
1070   return TRUE;
1071 }
1072
1073 static gboolean
1074 gst_h265_decoder_flush (GstVideoDecoder * decoder)
1075 {
1076   GstH265Decoder *self = GST_H265_DECODER (decoder);
1077
1078   gst_h265_decoder_clear_dpb (self, TRUE);
1079
1080   return TRUE;
1081 }
1082
1083 static GstFlowReturn
1084 gst_h265_decoder_drain (GstVideoDecoder * decoder)
1085 {
1086   GstH265Decoder *self = GST_H265_DECODER (decoder);
1087
1088   /* dpb will be cleared by this method */
1089   return gst_h265_decoder_drain_internal (self);
1090 }
1091
1092 static GstFlowReturn
1093 gst_h265_decoder_finish (GstVideoDecoder * decoder)
1094 {
1095   return gst_h265_decoder_drain (decoder);
1096 }
1097
1098 static gboolean
1099 gst_h265_decoder_fill_picture_from_slice (GstH265Decoder * self,
1100     const GstH265Slice * slice, GstH265Picture * picture)
1101 {
1102   GstH265DecoderPrivate *priv = self->priv;
1103   const GstH265SliceHdr *slice_hdr = &slice->header;
1104   const GstH265NalUnit *nalu = &slice->nalu;
1105
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;
1112   }
1113
1114   if (GST_H265_IS_NAL_TYPE_RASL (nalu->type) &&
1115       priv->associated_irap_NoRaslOutputFlag) {
1116     picture->output_flag = FALSE;
1117   } else {
1118     picture->output_flag = slice_hdr->pic_output_flag;
1119   }
1120
1121   return TRUE;
1122 }
1123
1124 #define RSV_VCL_N10 10
1125 #define RSV_VCL_N12 12
1126 #define RSV_VCL_N14 14
1127
1128 static gboolean
1129 nal_is_ref (guint8 nal_type)
1130 {
1131   gboolean ret = FALSE;
1132   switch (nal_type) {
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:
1138     case RSV_VCL_N10:
1139     case RSV_VCL_N12:
1140     case RSV_VCL_N14:
1141       ret = FALSE;
1142       break;
1143     default:
1144       ret = TRUE;
1145       break;
1146   }
1147   return ret;
1148 }
1149
1150 static gboolean
1151 gst_h265_decoder_calculate_poc (GstH265Decoder * self,
1152     const GstH265Slice * slice, GstH265Picture * picture)
1153 {
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);
1159   gboolean is_irap;
1160
1161   GST_DEBUG_OBJECT (self, "decode PicOrderCntVal");
1162
1163   priv->prev_poc_lsb = priv->poc_lsb;
1164   priv->prev_poc_msb = priv->poc_msb;
1165
1166   is_irap = GST_H265_IS_NAL_TYPE_IRAP (nalu->type);
1167
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;
1171   }
1172
1173   /* Finding PicOrderCntMsb */
1174   if (is_irap && picture->NoRaslOutputFlag) {
1175     priv->poc_msb = 0;
1176   } else {
1177     /* (8-1) */
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;
1182
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;
1187
1188     else
1189       priv->poc_msb = priv->prev_poc_msb;
1190   }
1191
1192   /* (8-2) */
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;
1196
1197   if (GST_H265_IS_NAL_TYPE_IDR (nalu->type)) {
1198     picture->pic_order_cnt = 0;
1199     picture->pic_order_cnt_lsb = 0;
1200     priv->poc_lsb = 0;
1201     priv->poc_msb = 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;
1206   }
1207
1208   GST_DEBUG_OBJECT (self,
1209       "PicOrderCntVal %d, (lsb %d)", picture->pic_order_cnt,
1210       picture->pic_order_cnt_lsb);
1211
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;
1216   }
1217
1218   return TRUE;
1219 }
1220
1221 static gboolean
1222 gst_h265_decoder_set_buffer_flags (GstH265Decoder * self,
1223     GstH265Picture * picture)
1224 {
1225   GstH265DecoderPrivate *priv = self->priv;
1226
1227   switch (picture->pic_struct) {
1228     case GST_H265_SEI_PIC_STRUCT_FRAME:
1229       break;
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?");
1236       } else {
1237         picture->buffer_flags = GST_VIDEO_BUFFER_FLAG_TOP_FIELD;
1238       }
1239       break;
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?");
1246       } else {
1247         picture->buffer_flags = GST_VIDEO_BUFFER_FLAG_BOTTOM_FIELD;
1248       }
1249       break;
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?");
1254       } else {
1255         picture->buffer_flags =
1256             GST_VIDEO_BUFFER_FLAG_INTERLACED | GST_VIDEO_BUFFER_FLAG_TFF;
1257       }
1258       break;
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?");
1263       } else {
1264         picture->buffer_flags = GST_VIDEO_BUFFER_FLAG_INTERLACED;
1265       }
1266       break;
1267     default:
1268       GST_FIXME_OBJECT (self, "Unhandled picture time SEI pic_struct %d",
1269           picture->pic_struct);
1270       break;
1271   }
1272
1273   return TRUE;
1274 }
1275
1276 static gboolean
1277 gst_h265_decoder_init_current_picture (GstH265Decoder * self)
1278 {
1279   GstH265DecoderPrivate *priv = self->priv;
1280
1281   if (!gst_h265_decoder_fill_picture_from_slice (self, &priv->current_slice,
1282           priv->current_picture)) {
1283     return FALSE;
1284   }
1285
1286   if (!gst_h265_decoder_calculate_poc (self,
1287           &priv->current_slice, priv->current_picture))
1288     return FALSE;
1289
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);
1295
1296   return TRUE;
1297 }
1298
1299 static gboolean
1300 has_entry_in_rps (GstH265Picture * dpb_pic,
1301     GstH265Picture ** rps_list, guint rps_list_length)
1302 {
1303   guint i;
1304
1305   if (!dpb_pic || !rps_list || !rps_list_length)
1306     return FALSE;
1307
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)
1310       return TRUE;
1311   }
1312   return FALSE;
1313 }
1314
1315 static void
1316 gst_h265_decoder_clear_ref_pic_sets (GstH265Decoder * self)
1317 {
1318   guint i;
1319
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);
1326   }
1327 }
1328
1329 static void
1330 gst_h265_decoder_derive_and_mark_rps (GstH265Decoder * self,
1331     GstH265Picture * picture, gint32 * CurrDeltaPocMsbPresentFlag,
1332     gint32 * FollDeltaPocMsbPresentFlag)
1333 {
1334   GstH265DecoderPrivate *priv = self->priv;
1335   guint i;
1336   GArray *dpb_array;
1337
1338   gst_h265_decoder_clear_ref_pic_sets (self);
1339
1340   /* (8-6) */
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]);
1345     } else {
1346       self->RefPicSetLtCurr[i] =
1347           gst_h265_dpb_get_ref_by_poc (priv->dpb, priv->PocLtCurr[i]);
1348     }
1349   }
1350
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]);
1355     } else {
1356       self->RefPicSetLtFoll[i] =
1357           gst_h265_dpb_get_ref_by_poc (priv->dpb, priv->PocLtFoll[i]);
1358     }
1359   }
1360
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;
1366     }
1367   }
1368
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;
1373     }
1374   }
1375
1376   /* (8-7) */
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]);
1380   }
1381
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]);
1385   }
1386
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]);
1390   }
1391
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);
1396
1397     if (dpb_pic &&
1398         !has_entry_in_rps (dpb_pic, self->RefPicSetLtCurr, self->NumPocLtCurr)
1399         && !has_entry_in_rps (dpb_pic, self->RefPicSetLtFoll,
1400             self->NumPocLtFoll)
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;
1411     }
1412   }
1413
1414   g_array_unref (dpb_array);
1415 }
1416
1417 static gboolean
1418 gst_h265_decoder_prepare_rps (GstH265Decoder * self, const GstH265Slice * slice,
1419     GstH265Picture * picture)
1420 {
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);
1428   gint i, j, k;
1429
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);
1434   }
1435
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;
1445   } else {
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;
1452
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)
1457       stRefPic =
1458           &sps->short_term_ref_pic_set[slice_hdr->short_term_ref_pic_set_idx];
1459
1460     g_assert (stRefPic != NULL);
1461
1462     GST_LOG_OBJECT (self,
1463         "NumDeltaPocs: %d, NumNegativePics: %d, NumPositivePics %d",
1464         stRefPic->NumDeltaPocs, stRefPic->NumNegativePics,
1465         stRefPic->NumPositivePics);
1466
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];
1471         numtotalcurr++;
1472       } else
1473         priv->PocStFoll[k++] = picture->pic_order_cnt + stRefPic->DeltaPocS0[i];
1474     }
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];
1480         numtotalcurr++;
1481       } else
1482         priv->PocStFoll[k++] = picture->pic_order_cnt + stRefPic->DeltaPocS1[i];
1483     }
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]];
1493       } else {
1494         PocLsbLt[i] = slice_hdr->poc_lsb_lt[i];
1495         UsedByCurrPicLt[i] = slice_hdr->used_by_curr_pic_lt_flag[i];
1496       }
1497       if (UsedByCurrPicLt[i])
1498         numtotalcurr++;
1499     }
1500
1501     self->NumPicTotalCurr = numtotalcurr;
1502
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];
1507       else
1508         DeltaPocMsbCycleLt[i] =
1509             slice_hdr->delta_poc_msb_cycle_lt[i] + DeltaPocMsbCycleLt[i - 1];
1510     }
1511
1512     /* (8-5) */
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])
1516         pocLt +=
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];
1523       } else {
1524         priv->PocLtFoll[k] = pocLt;
1525         FollDeltaPocMsbPresentFlag[k++] =
1526             slice_hdr->delta_poc_msb_present_flag[i];
1527       }
1528     }
1529     self->NumPocLtCurr = j;
1530     self->NumPocLtFoll = k;
1531   }
1532
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);
1539
1540   /* the derivation process for the RPS and the picture marking */
1541   gst_h265_decoder_derive_and_mark_rps (self, picture,
1542       CurrDeltaPocMsbPresentFlag, FollDeltaPocMsbPresentFlag);
1543
1544   return TRUE;
1545 }
1546
1547 static void
1548 gst_h265_decoder_do_output_picture (GstH265Decoder * self,
1549     GstH265Picture * picture, GstFlowReturn * ret)
1550 {
1551   GstH265DecoderPrivate *priv = self->priv;
1552   GstVideoCodecFrame *frame = NULL;
1553   GstH265DecoderOutputFrame output_frame;
1554   GstFlowReturn flow_ret = GST_FLOW_OK;
1555
1556   g_assert (ret != NULL);
1557
1558   GST_LOG_OBJECT (self, "Output picture %p (poc %d)", picture,
1559       picture->pic_order_cnt);
1560
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);
1565   }
1566
1567   priv->last_output_poc = picture->pic_order_cnt;
1568
1569   frame = gst_video_decoder_get_frame (GST_VIDEO_DECODER (self),
1570       picture->system_frame_number);
1571
1572   if (!frame) {
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);
1577
1578     gst_h265_picture_unref (picture);
1579     return;
1580   }
1581
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);
1586
1587   gst_h265_decoder_drain_output_queue (self, priv->preferred_output_delay,
1588       &flow_ret);
1589   UPDATE_FLOW_RETURN (ret, flow_ret);
1590 }
1591
1592 static void
1593 gst_h265_decoder_clear_dpb (GstH265Decoder * self, gboolean flush)
1594 {
1595   GstVideoDecoder *decoder = GST_VIDEO_DECODER (self);
1596   GstH265DecoderPrivate *priv = self->priv;
1597   GstH265Picture *picture;
1598
1599   /* If we are not flushing now, videodecoder baseclass will hold
1600    * GstVideoCodecFrame. Release frames manually */
1601   if (!flush) {
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);
1605
1606       if (frame)
1607         gst_video_decoder_release_frame (decoder, frame);
1608       gst_h265_picture_unref (picture);
1609     }
1610   }
1611
1612   gst_queue_array_clear (priv->output_queue);
1613   gst_h265_dpb_clear (priv->dpb);
1614   priv->last_output_poc = G_MININT32;
1615 }
1616
1617 static GstFlowReturn
1618 gst_h265_decoder_drain_internal (GstH265Decoder * self)
1619 {
1620   GstH265DecoderPrivate *priv = self->priv;
1621   GstH265Picture *picture;
1622   GstFlowReturn ret = GST_FLOW_OK;
1623
1624   while ((picture = gst_h265_dpb_bump (priv->dpb, TRUE)) != NULL)
1625     gst_h265_decoder_do_output_picture (self, picture, &ret);
1626
1627   gst_h265_decoder_drain_output_queue (self, 0, &ret);
1628
1629   gst_h265_dpb_clear (priv->dpb);
1630   priv->last_output_poc = G_MININT32;
1631
1632   return ret;
1633 }
1634
1635 /* C.5.2.2 */
1636 static GstFlowReturn
1637 gst_h265_decoder_dpb_init (GstH265Decoder * self, const GstH265Slice * slice,
1638     GstH265Picture * picture)
1639 {
1640   GstH265DecoderPrivate *priv = self->priv;
1641   const GstH265SPS *sps = priv->active_sps;
1642   GstH265Picture *to_output;
1643   GstFlowReturn ret = GST_FLOW_OK;
1644
1645   /* C 3.2 */
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);
1651     } else {
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);
1655
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);
1660       } else {
1661         priv->last_output_poc = G_MININT32;
1662       }
1663     }
1664   } else {
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] +
1670             1)) {
1671       to_output = gst_h265_dpb_bump (priv->dpb, FALSE);
1672
1673       /* Something wrong... */
1674       if (!to_output) {
1675         GST_WARNING_OBJECT (self, "Bumping is needed but no picture to output");
1676         break;
1677       }
1678
1679       gst_h265_decoder_do_output_picture (self, to_output, &ret);
1680     }
1681   }
1682
1683   return ret;
1684 }
1685
1686 static GstFlowReturn
1687 gst_h265_decoder_start_current_picture (GstH265Decoder * self)
1688 {
1689   GstH265DecoderClass *klass;
1690   GstH265DecoderPrivate *priv = self->priv;
1691   GstFlowReturn ret = GST_FLOW_OK;
1692
1693   g_assert (priv->current_picture != NULL);
1694   g_assert (priv->active_sps != NULL);
1695   g_assert (priv->active_pps != NULL);
1696
1697   if (!gst_h265_decoder_init_current_picture (self))
1698     return GST_FLOW_ERROR;
1699
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);
1706     return GST_FLOW_OK;
1707   }
1708
1709   gst_h265_decoder_prepare_rps (self, &priv->current_slice,
1710       priv->current_picture);
1711
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");
1716     return ret;
1717   }
1718
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);
1723
1724     if (ret != GST_FLOW_OK) {
1725       GST_WARNING_OBJECT (self, "subclass does not want to start picture");
1726       return ret;
1727     }
1728   }
1729
1730   return GST_FLOW_OK;
1731 }
1732
1733 static void
1734 gst_h265_decoder_finish_picture (GstH265Decoder * self,
1735     GstH265Picture * picture, GstFlowReturn * ret)
1736 {
1737   GstVideoDecoder *decoder = GST_VIDEO_DECODER (self);
1738   GstH265DecoderPrivate *priv = self->priv;
1739   const GstH265SPS *sps = priv->active_sps;
1740
1741   g_assert (ret != NULL);
1742
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));
1746
1747   gst_h265_dpb_delete_unused (priv->dpb);
1748
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);
1753
1754     gst_video_decoder_release_frame (decoder, frame);
1755   }
1756
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);
1760
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);
1768
1769     /* Something wrong... */
1770     if (!to_output) {
1771       GST_WARNING_OBJECT (self, "Bumping is needed but no picture to output");
1772       break;
1773     }
1774
1775     gst_h265_decoder_do_output_picture (self, to_output, ret);
1776   }
1777 }
1778
1779 static void
1780 gst_h265_decoder_finish_current_picture (GstH265Decoder * self,
1781     GstFlowReturn * ret)
1782 {
1783   GstH265DecoderPrivate *priv = self->priv;
1784   GstH265DecoderClass *klass;
1785   GstFlowReturn flow_ret = GST_FLOW_OK;
1786
1787   g_assert (ret != NULL);
1788
1789   if (!priv->current_picture)
1790     return;
1791
1792   klass = GST_H265_DECODER_GET_CLASS (self);
1793
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");
1798
1799       /* continue to empty dpb */
1800       UPDATE_FLOW_RETURN (ret, flow_ret);
1801     }
1802   }
1803
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;
1807
1808   UPDATE_FLOW_RETURN (ret, flow_ret);
1809 }
1810
1811 static void
1812 gst_h265_decoder_reset_frame_state (GstH265Decoder * self)
1813 {
1814   GstH265DecoderPrivate *priv = self->priv;
1815
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);
1823 }
1824
1825 static GstFlowReturn
1826 gst_h265_decoder_handle_frame (GstVideoDecoder * decoder,
1827     GstVideoCodecFrame * frame)
1828 {
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;
1834   GstMapInfo map;
1835   GstFlowReturn decode_ret = GST_FLOW_OK;
1836   guint i;
1837
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)));
1842
1843   gst_h265_decoder_reset_frame_state (self);
1844
1845   priv->current_frame = frame;
1846
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;
1851   }
1852
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);
1857
1858     while (pres == GST_H265_PARSER_OK) {
1859       pres = gst_h265_decoder_parse_nalu (self, &nalu);
1860       if (pres != GST_H265_PARSER_OK)
1861         break;
1862
1863       pres = gst_h265_parser_identify_nalu_hevc (priv->parser,
1864           map.data, nalu.offset + nalu.size, map.size, priv->nal_length_size,
1865           &nalu);
1866     }
1867   } else {
1868     pres = gst_h265_parser_identify_nalu (priv->parser,
1869         map.data, 0, map.size, &nalu);
1870
1871     if (pres == GST_H265_PARSER_NO_NAL_END)
1872       pres = GST_H265_PARSER_OK;
1873
1874     while (pres == GST_H265_PARSER_OK) {
1875       pres = gst_h265_decoder_parse_nalu (self, &nalu);
1876       if (pres != GST_H265_PARSER_OK)
1877         break;
1878
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;
1883     }
1884   }
1885
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);
1890   }
1891
1892   gst_buffer_unmap (in_buf, &map);
1893   gst_h265_decoder_reset_frame_state (self);
1894
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);
1899     }
1900
1901     gst_video_decoder_drop_frame (decoder, frame);
1902     gst_clear_h265_picture (&priv->current_picture);
1903
1904     return decode_ret;
1905   }
1906
1907   if (priv->current_picture) {
1908     gst_h265_decoder_finish_current_picture (self, &decode_ret);
1909     gst_video_codec_frame_unref (frame);
1910   } else {
1911     /* This picture was dropped */
1912     gst_video_decoder_release_frame (decoder, frame);
1913   }
1914
1915   if (decode_ret == GST_FLOW_ERROR) {
1916     GST_VIDEO_DECODER_ERROR (self, 1, STREAM, DECODE,
1917         ("Failed to decode data"), (NULL), decode_ret);
1918   }
1919
1920   return decode_ret;
1921 }
1922
1923 static void
1924 gst_h265_decoder_clear_nalu (GstH265DecoderNalUnit * nalu)
1925 {
1926   if (!nalu)
1927     return;
1928
1929   memset (nalu, 0, sizeof (GstH265DecoderNalUnit));
1930 }
1931
1932 /**
1933  * gst_h265_decoder_set_process_ref_pic_lists:
1934  * @decoder: a #GstH265Decoder
1935  * @process: whether subclass is requiring reference picture modification process
1936  *
1937  * Called to en/disable reference picture modification process.
1938  *
1939  * Since: 1.20
1940  */
1941 void
1942 gst_h265_decoder_set_process_ref_pic_lists (GstH265Decoder * decoder,
1943     gboolean process)
1944 {
1945   decoder->priv->process_ref_pic_lists = process;
1946 }
1947
1948 /**
1949  * gst_h265_decoder_get_picture:
1950  * @decoder: a #GstH265Decoder
1951  * @system_frame_number: a target system frame number of #GstH265Picture
1952  *
1953  * Retrive DPB and return a #GstH265Picture corresponding to
1954  * the @system_frame_number
1955  *
1956  * Returns: (transfer full): a #GstH265Picture if successful, or %NULL otherwise
1957  *
1958  * Since: 1.20
1959  */
1960 GstH265Picture *
1961 gst_h265_decoder_get_picture (GstH265Decoder * decoder,
1962     guint32 system_frame_number)
1963 {
1964   return gst_h265_dpb_get_picture (decoder->priv->dpb, system_frame_number);
1965 }