Move CODEC base classes into it's own library
[platform/upstream/gstreamer.git] / 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 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include "gsth265decoder.h"
27
28 GST_DEBUG_CATEGORY (gst_h265_decoder_debug);
29 #define GST_CAT_DEFAULT gst_h265_decoder_debug
30
31 typedef enum
32 {
33   GST_H265_DECODER_FORMAT_NONE,
34   GST_H265_DECODER_FORMAT_HVC1,
35   GST_H265_DECODER_FORMAT_HEV1,
36   GST_H265_DECODER_FORMAT_BYTE
37 } GstH265DecoderFormat;
38
39 typedef enum
40 {
41   GST_H265_DECODER_ALIGN_NONE,
42   GST_H265_DECODER_ALIGN_NAL,
43   GST_H265_DECODER_ALIGN_AU
44 } GstH265DecoderAlign;
45
46 struct _GstH265DecoderPrivate
47 {
48   gint width, height;
49
50   /* input codec_data, if any */
51   GstBuffer *codec_data;
52   guint nal_length_size;
53
54   /* state */
55   GstH265DecoderFormat in_format;
56   GstH265DecoderAlign align;
57   GstH265Parser *parser;
58   GstH265Dpb *dpb;
59   GstFlowReturn last_ret;
60
61   /* vps/sps/pps of the current slice */
62   const GstH265VPS *active_vps;
63   const GstH265SPS *active_sps;
64   const GstH265PPS *active_pps;
65
66   guint32 SpsMaxLatencyPictures;
67   gint32 WpOffsetHalfRangeC;
68
69   /* Picture currently being processed/decoded */
70   GstH265Picture *current_picture;
71   GstVideoCodecFrame *current_frame;
72
73   /* Slice (slice header + nalu) currently being processed/decodec */
74   GstH265Slice current_slice;
75   GstH265Slice prev_slice;
76   GstH265Slice prev_independent_slice;
77
78   gint32 poc;                   // PicOrderCntVal
79   gint32 poc_msb;               // PicOrderCntMsb
80   gint32 poc_lsb;               // pic_order_cnt_lsb (from slice_header())
81   gint32 prev_poc_msb;          // prevPicOrderCntMsb
82   gint32 prev_poc_lsb;          // prevPicOrderCntLsb
83   gint32 prev_tid0pic_poc_lsb;
84   gint32 prev_tid0pic_poc_msb;
85   gint32 PocStCurrBefore[16];
86   gint32 PocStCurrAfter[16];
87   gint32 PocStFoll[16];
88   gint32 PocLtCurr[16];
89   gint32 PocLtFoll[16];
90
91   /* PicOrderCount of the previously outputted frame */
92   gint last_output_poc;
93
94   gboolean associated_irap_NoRaslOutputFlag;
95   gboolean new_bitstream;
96   gboolean prev_nal_is_eos;
97 };
98
99 #define parent_class gst_h265_decoder_parent_class
100 G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GstH265Decoder, gst_h265_decoder,
101     GST_TYPE_VIDEO_DECODER,
102     G_ADD_PRIVATE (GstH265Decoder);
103     GST_DEBUG_CATEGORY_INIT (gst_h265_decoder_debug, "h265decoder", 0,
104         "H.265 Video Decoder"));
105
106 static gboolean gst_h265_decoder_start (GstVideoDecoder * decoder);
107 static gboolean gst_h265_decoder_stop (GstVideoDecoder * decoder);
108 static gboolean gst_h265_decoder_set_format (GstVideoDecoder * decoder,
109     GstVideoCodecState * state);
110 static GstFlowReturn gst_h265_decoder_finish (GstVideoDecoder * decoder);
111 static gboolean gst_h265_decoder_flush (GstVideoDecoder * decoder);
112 static GstFlowReturn gst_h265_decoder_drain (GstVideoDecoder * decoder);
113 static GstFlowReturn gst_h265_decoder_handle_frame (GstVideoDecoder * decoder,
114     GstVideoCodecFrame * frame);
115
116 static gboolean gst_h265_decoder_finish_current_picture (GstH265Decoder * self);
117 static void gst_h265_decoder_clear_dpb (GstH265Decoder * self);
118 static gboolean
119 gst_h265_decoder_output_all_remaining_pics (GstH265Decoder * self);
120 static gboolean gst_h265_decoder_start_current_picture (GstH265Decoder * self);
121
122 static void
123 gst_h265_decoder_class_init (GstH265DecoderClass * klass)
124 {
125   GstVideoDecoderClass *decoder_class = GST_VIDEO_DECODER_CLASS (klass);
126
127   decoder_class->start = GST_DEBUG_FUNCPTR (gst_h265_decoder_start);
128   decoder_class->stop = GST_DEBUG_FUNCPTR (gst_h265_decoder_stop);
129   decoder_class->set_format = GST_DEBUG_FUNCPTR (gst_h265_decoder_set_format);
130   decoder_class->finish = GST_DEBUG_FUNCPTR (gst_h265_decoder_finish);
131   decoder_class->flush = GST_DEBUG_FUNCPTR (gst_h265_decoder_flush);
132   decoder_class->drain = GST_DEBUG_FUNCPTR (gst_h265_decoder_drain);
133   decoder_class->handle_frame =
134       GST_DEBUG_FUNCPTR (gst_h265_decoder_handle_frame);
135 }
136
137 static void
138 gst_h265_decoder_init (GstH265Decoder * self)
139 {
140   gst_video_decoder_set_packetized (GST_VIDEO_DECODER (self), TRUE);
141
142   self->priv = gst_h265_decoder_get_instance_private (self);
143 }
144
145 static gboolean
146 gst_h265_decoder_start (GstVideoDecoder * decoder)
147 {
148   GstH265Decoder *self = GST_H265_DECODER (decoder);
149   GstH265DecoderPrivate *priv = self->priv;
150
151   priv->parser = gst_h265_parser_new ();
152   priv->dpb = gst_h265_dpb_new ();
153   priv->new_bitstream = TRUE;
154   priv->prev_nal_is_eos = FALSE;
155
156   return TRUE;
157 }
158
159 static gboolean
160 gst_h265_decoder_stop (GstVideoDecoder * decoder)
161 {
162   GstH265Decoder *self = GST_H265_DECODER (decoder);
163   GstH265DecoderPrivate *priv = self->priv;
164
165   if (self->input_state) {
166     gst_video_codec_state_unref (self->input_state);
167     self->input_state = NULL;
168   }
169
170   gst_clear_buffer (&priv->codec_data);
171
172   if (priv->parser) {
173     gst_h265_parser_free (priv->parser);
174     priv->parser = NULL;
175   }
176
177   if (priv->dpb) {
178     gst_h265_dpb_free (priv->dpb);
179     priv->dpb = NULL;
180   }
181
182   return TRUE;
183 }
184
185 static gboolean
186 gst_h265_decoder_parse_vps (GstH265Decoder * self, GstH265NalUnit * nalu)
187 {
188   GstH265DecoderPrivate *priv = self->priv;
189   GstH265VPS vps;
190   GstH265ParserResult pres;
191   gboolean ret = TRUE;
192
193   pres = gst_h265_parser_parse_vps (priv->parser, nalu, &vps);
194   if (pres != GST_H265_PARSER_OK) {
195     GST_WARNING_OBJECT (self, "Failed to parse VPS, result %d", pres);
196     return FALSE;
197   }
198
199   GST_LOG_OBJECT (self, "VPS parsed");
200
201   return ret;
202 }
203
204 static gboolean
205 gst_h265_decoder_process_sps (GstH265Decoder * self, GstH265SPS * sps)
206 {
207   GstH265DecoderPrivate *priv = self->priv;
208   gint max_dpb_size;
209   gint prev_max_dpb_size;
210   gint MaxLumaPS;
211   const gint MaxDpbPicBuf = 6;
212   gint PicSizeInSamplesY;
213   guint high_precision_offsets_enabled_flag = 0;
214   guint bitdepthC = 0;
215
216   /* A.4.1 */
217   MaxLumaPS = 35651584;
218   PicSizeInSamplesY = sps->width * sps->height;
219   if (PicSizeInSamplesY <= (MaxLumaPS >> 2))
220     max_dpb_size = MaxDpbPicBuf * 4;
221   else if (PicSizeInSamplesY <= (MaxLumaPS >> 1))
222     max_dpb_size = MaxDpbPicBuf * 2;
223   else if (PicSizeInSamplesY <= ((3 * MaxLumaPS) >> 2))
224     max_dpb_size = (MaxDpbPicBuf * 4) / 3;
225   else
226     max_dpb_size = MaxDpbPicBuf;
227
228   max_dpb_size = MIN (max_dpb_size, 16);
229
230   prev_max_dpb_size = gst_h265_dpb_get_max_num_pics (priv->dpb);
231   if (priv->width != sps->width || priv->height != sps->height ||
232       prev_max_dpb_size != max_dpb_size) {
233     GstH265DecoderClass *klass = GST_H265_DECODER_GET_CLASS (self);
234
235     GST_DEBUG_OBJECT (self,
236         "SPS updated, resolution: %dx%d -> %dx%d, dpb size: %d -> %d",
237         priv->width, priv->height, sps->width, sps->height,
238         prev_max_dpb_size, max_dpb_size);
239
240     g_assert (klass->new_sequence);
241
242     if (!klass->new_sequence (self, sps)) {
243       GST_ERROR_OBJECT (self, "subclass does not want accept new sequence");
244       return FALSE;
245     }
246
247     priv->width = sps->width;
248     priv->height = sps->height;
249
250     gst_h265_dpb_set_max_num_pics (priv->dpb, max_dpb_size);
251   }
252
253   if (sps->max_latency_increase_plus1[sps->max_sub_layers_minus1]) {
254     priv->SpsMaxLatencyPictures =
255         sps->max_num_reorder_pics[sps->max_sub_layers_minus1] +
256         sps->max_latency_increase_plus1[sps->max_sub_layers_minus1] - 1;
257   }
258
259   /* Calculate WpOffsetHalfRangeC: (7-34)
260    * FIXME: We don't have parser API for sps_range_extension, so
261    * assuming high_precision_offsets_enabled_flag as zero */
262   bitdepthC = sps->bit_depth_chroma_minus8 + 8;
263   priv->WpOffsetHalfRangeC =
264       1 << (high_precision_offsets_enabled_flag ? (bitdepthC - 1) : 7);
265
266   GST_DEBUG_OBJECT (self, "Set DPB max size %d", max_dpb_size);
267
268   return TRUE;
269 }
270
271 static gboolean
272 gst_h265_decoder_parse_sps (GstH265Decoder * self, GstH265NalUnit * nalu)
273 {
274   GstH265DecoderPrivate *priv = self->priv;
275   GstH265SPS sps;
276   GstH265ParserResult pres;
277   gboolean ret = TRUE;
278
279   pres = gst_h265_parser_parse_sps (priv->parser, nalu, &sps, TRUE);
280   if (pres != GST_H265_PARSER_OK) {
281     GST_WARNING_OBJECT (self, "Failed to parse SPS, result %d", pres);
282     return FALSE;
283   }
284
285   GST_LOG_OBJECT (self, "SPS parsed");
286
287   if (!gst_h265_decoder_process_sps (self, &sps))
288     ret = FALSE;
289
290   return ret;
291 }
292
293 static gboolean
294 gst_h265_decoder_parse_pps (GstH265Decoder * self, GstH265NalUnit * nalu)
295 {
296   GstH265DecoderPrivate *priv = self->priv;
297   GstH265PPS pps;
298   GstH265ParserResult pres;
299
300   pres = gst_h265_parser_parse_pps (priv->parser, nalu, &pps);
301   if (pres != GST_H265_PARSER_OK) {
302     GST_WARNING_OBJECT (self, "Failed to parse PPS, result %d", pres);
303     return FALSE;
304   }
305
306   GST_LOG_OBJECT (self, "PPS parsed");
307
308   return TRUE;
309 }
310
311 static gboolean
312 gst_h265_decoder_decode_slice (GstH265Decoder * self)
313 {
314   GstH265DecoderClass *klass = GST_H265_DECODER_GET_CLASS (self);
315   GstH265DecoderPrivate *priv = self->priv;
316   GstH265Slice *slice = &priv->current_slice;
317   GstH265Picture *picture = priv->current_picture;
318
319   if (!picture) {
320     GST_ERROR_OBJECT (self, "No current picture");
321     return FALSE;
322   }
323
324   g_assert (klass->decode_slice);
325
326   return klass->decode_slice (self, picture, slice);
327 }
328
329 static gboolean
330 gst_h265_decoder_preprocess_slice (GstH265Decoder * self, GstH265Slice * slice)
331 {
332   GstH265DecoderPrivate *priv = self->priv;
333   const GstH265SliceHdr *slice_hdr = &slice->header;
334   const GstH265NalUnit *nalu = &slice->nalu;
335
336   if (priv->current_picture && slice_hdr->first_slice_segment_in_pic_flag) {
337     GST_WARNING_OBJECT (self,
338         "Current picture is not finished but slice header has "
339         "first_slice_segment_in_pic_flag");
340     return FALSE;
341   }
342
343   if (IS_IDR (nalu->type)) {
344     GST_DEBUG_OBJECT (self, "IDR nalu, clear dpb");
345     gst_h265_decoder_drain (GST_VIDEO_DECODER (self));
346   }
347
348   return TRUE;
349 }
350
351 static gboolean
352 gst_h265_decoder_parse_slice (GstH265Decoder * self, GstH265NalUnit * nalu,
353     GstClockTime pts)
354 {
355   GstH265DecoderPrivate *priv = self->priv;
356   GstH265ParserResult pres = GST_H265_PARSER_OK;
357
358   memset (&priv->current_slice, 0, sizeof (GstH265Slice));
359
360   pres = gst_h265_parser_parse_slice_hdr (priv->parser, nalu,
361       &priv->current_slice.header);
362
363   if (pres != GST_H265_PARSER_OK) {
364     GST_ERROR_OBJECT (self, "Failed to parse slice header, ret %d", pres);
365     memset (&priv->current_slice, 0, sizeof (GstH265Slice));
366
367     return FALSE;
368   }
369
370   priv->current_slice.nalu = *nalu;
371
372   if (!gst_h265_decoder_preprocess_slice (self, &priv->current_slice))
373     return FALSE;
374
375   priv->active_pps = priv->current_slice.header.pps;
376   priv->active_sps = priv->active_pps->sps;
377
378   if (!priv->current_picture) {
379     GstH265DecoderClass *klass = GST_H265_DECODER_GET_CLASS (self);
380     GstH265Picture *picture;
381     gboolean ret = TRUE;
382
383     picture = gst_h265_picture_new ();
384     picture->pts = pts;
385
386     if (klass->new_picture)
387       ret = klass->new_picture (self, picture);
388
389     if (!ret) {
390       GST_ERROR_OBJECT (self, "subclass does not want accept new picture");
391       gst_h265_picture_unref (picture);
392       return FALSE;
393     }
394
395     priv->current_picture = picture;
396     gst_video_codec_frame_set_user_data (priv->current_frame,
397         gst_h265_picture_ref (priv->current_picture),
398         (GDestroyNotify) gst_h265_picture_unref);
399
400     if (!gst_h265_decoder_start_current_picture (self)) {
401       GST_ERROR_OBJECT (self, "start picture failed");
402       return FALSE;
403     }
404
405     /* this picture was dropped */
406     if (!priv->current_picture)
407       return TRUE;
408   }
409
410   return gst_h265_decoder_decode_slice (self);
411 }
412
413 static GstFlowReturn
414 gst_h265_decoder_decode_nal (GstH265Decoder * self, GstH265NalUnit * nalu,
415     GstClockTime pts)
416 {
417   GstH265DecoderPrivate *priv = self->priv;
418   gboolean ret = TRUE;
419
420   GST_LOG_OBJECT (self, "Parsed nal type: %d, offset %d, size %d",
421       nalu->type, nalu->offset, nalu->size);
422
423   switch (nalu->type) {
424     case GST_H265_NAL_VPS:
425       ret = gst_h265_decoder_parse_vps (self, nalu);
426       break;
427     case GST_H265_NAL_SPS:
428       ret = gst_h265_decoder_parse_sps (self, nalu);
429       break;
430     case GST_H265_NAL_PPS:
431       ret = gst_h265_decoder_parse_pps (self, nalu);
432       break;
433     case GST_H265_NAL_SLICE_TRAIL_N:
434     case GST_H265_NAL_SLICE_TRAIL_R:
435     case GST_H265_NAL_SLICE_TSA_N:
436     case GST_H265_NAL_SLICE_TSA_R:
437     case GST_H265_NAL_SLICE_STSA_N:
438     case GST_H265_NAL_SLICE_STSA_R:
439     case GST_H265_NAL_SLICE_RADL_N:
440     case GST_H265_NAL_SLICE_RADL_R:
441     case GST_H265_NAL_SLICE_RASL_N:
442     case GST_H265_NAL_SLICE_RASL_R:
443     case GST_H265_NAL_SLICE_BLA_W_LP:
444     case GST_H265_NAL_SLICE_BLA_W_RADL:
445     case GST_H265_NAL_SLICE_BLA_N_LP:
446     case GST_H265_NAL_SLICE_IDR_W_RADL:
447     case GST_H265_NAL_SLICE_IDR_N_LP:
448     case GST_H265_NAL_SLICE_CRA_NUT:
449       ret = gst_h265_decoder_parse_slice (self, nalu, pts);
450       priv->new_bitstream = FALSE;
451       priv->prev_nal_is_eos = FALSE;
452       break;
453     case GST_H265_NAL_EOB:
454       gst_h265_decoder_drain (GST_VIDEO_DECODER (self));
455       priv->new_bitstream = TRUE;
456       break;
457     case GST_H265_NAL_EOS:
458       gst_h265_decoder_drain (GST_VIDEO_DECODER (self));
459       priv->prev_nal_is_eos = TRUE;
460       break;
461     default:
462       break;
463   }
464
465   return ret;
466 }
467
468 static void
469 gst_h265_decoder_format_from_caps (GstH265Decoder * self, GstCaps * caps,
470     GstH265DecoderFormat * format, GstH265DecoderAlign * align)
471 {
472   if (format)
473     *format = GST_H265_DECODER_FORMAT_NONE;
474
475   if (align)
476     *align = GST_H265_DECODER_ALIGN_NONE;
477
478   if (!gst_caps_is_fixed (caps)) {
479     GST_WARNING_OBJECT (self, "Caps wasn't fixed");
480     return;
481   }
482
483   GST_DEBUG_OBJECT (self, "parsing caps: %" GST_PTR_FORMAT, caps);
484
485   if (caps && gst_caps_get_size (caps) > 0) {
486     GstStructure *s = gst_caps_get_structure (caps, 0);
487     const gchar *str = NULL;
488
489     if (format) {
490       if ((str = gst_structure_get_string (s, "stream-format"))) {
491         if (strcmp (str, "hvc1") == 0)
492           *format = GST_H265_DECODER_FORMAT_HVC1;
493         else if (strcmp (str, "hev1") == 0)
494           *format = GST_H265_DECODER_FORMAT_HEV1;
495         else if (strcmp (str, "byte-stream") == 0)
496           *format = GST_H265_DECODER_FORMAT_BYTE;
497       }
498     }
499
500     if (align) {
501       if ((str = gst_structure_get_string (s, "alignment"))) {
502         if (strcmp (str, "au") == 0)
503           *align = GST_H265_DECODER_ALIGN_AU;
504         else if (strcmp (str, "nal") == 0)
505           *align = GST_H265_DECODER_ALIGN_NAL;
506       }
507     }
508   }
509 }
510
511 static gboolean
512 gst_h265_decoder_parse_codec_data (GstH265Decoder * self, const guint8 * data,
513     gsize size)
514 {
515   GstH265DecoderPrivate *priv = self->priv;
516   guint num_nal_arrays;
517   guint off;
518   guint num_nals, i, j;
519   GstH265ParserResult pres;
520   GstH265NalUnit nalu;
521
522   /* parse the hvcC data */
523   if (size < 23) {
524     GST_WARNING_OBJECT (self, "hvcC too small");
525     return FALSE;
526   }
527
528   /* wrong hvcC version */
529   if (data[0] != 0 && data[0] != 1) {
530     return FALSE;
531   }
532
533   priv->nal_length_size = (data[21] & 0x03) + 1;
534   GST_DEBUG_OBJECT (self, "nal length size %u", priv->nal_length_size);
535
536   num_nal_arrays = data[22];
537   off = 23;
538
539   for (i = 0; i < num_nal_arrays; i++) {
540     if (off + 3 >= size) {
541       GST_WARNING_OBJECT (self, "hvcC too small");
542       return FALSE;
543     }
544
545     num_nals = GST_READ_UINT16_BE (data + off + 1);
546     off += 3;
547     for (j = 0; j < num_nals; j++) {
548       pres = gst_h265_parser_identify_nalu_hevc (priv->parser,
549           data, off, size, 2, &nalu);
550
551       if (pres != GST_H265_PARSER_OK) {
552         GST_WARNING_OBJECT (self, "hvcC too small");
553         return FALSE;
554       }
555
556       switch (nalu.type) {
557         case GST_H265_NAL_VPS:
558           gst_h265_decoder_parse_vps (self, &nalu);
559           break;
560         case GST_H265_NAL_SPS:
561           gst_h265_decoder_parse_sps (self, &nalu);
562           break;
563         case GST_H265_NAL_PPS:
564           gst_h265_decoder_parse_pps (self, &nalu);
565           break;
566         default:
567           break;
568       }
569
570       off = nalu.offset + nalu.size;
571     }
572   }
573
574   return TRUE;
575 }
576
577 static gboolean
578 gst_h265_decoder_set_format (GstVideoDecoder * decoder,
579     GstVideoCodecState * state)
580 {
581   GstH265Decoder *self = GST_H265_DECODER (decoder);
582   GstH265DecoderPrivate *priv = self->priv;
583
584   GST_DEBUG_OBJECT (decoder, "Set format");
585
586   if (self->input_state)
587     gst_video_codec_state_unref (self->input_state);
588
589   self->input_state = gst_video_codec_state_ref (state);
590
591   if (state->caps) {
592     GstStructure *str;
593     const GValue *codec_data_value;
594     GstH265DecoderFormat format;
595     GstH265DecoderAlign align;
596
597     gst_h265_decoder_format_from_caps (self, state->caps, &format, &align);
598
599     str = gst_caps_get_structure (state->caps, 0);
600     codec_data_value = gst_structure_get_value (str, "codec_data");
601
602     if (GST_VALUE_HOLDS_BUFFER (codec_data_value)) {
603       gst_buffer_replace (&priv->codec_data,
604           gst_value_get_buffer (codec_data_value));
605     } else {
606       gst_buffer_replace (&priv->codec_data, NULL);
607     }
608
609     if (format == GST_H265_DECODER_FORMAT_NONE) {
610       /* codec_data implies packetized */
611       if (codec_data_value != NULL) {
612         GST_WARNING_OBJECT (self,
613             "video/x-h265 caps with codec_data but no stream-format=hev1 or hvc1");
614         format = GST_H265_DECODER_FORMAT_HEV1;
615       } else {
616         /* otherwise assume bytestream input */
617         GST_WARNING_OBJECT (self,
618             "video/x-h265 caps without codec_data or stream-format");
619         format = GST_H265_DECODER_FORMAT_BYTE;
620       }
621     }
622
623     if (format == GST_H265_DECODER_FORMAT_HEV1 ||
624         format == GST_H265_DECODER_FORMAT_HVC1) {
625       if (codec_data_value == NULL) {
626         /* Try it with size 4 anyway */
627         priv->nal_length_size = 4;
628         GST_WARNING_OBJECT (self,
629             "packetized format without codec data, assuming nal length size is 4");
630       }
631
632       /* AVC implies alignment=au */
633       if (align == GST_H265_DECODER_ALIGN_NONE)
634         align = GST_H265_DECODER_ALIGN_AU;
635     }
636
637     if (format == GST_H265_DECODER_FORMAT_BYTE) {
638       if (codec_data_value != NULL) {
639         GST_WARNING_OBJECT (self, "bytestream with codec data");
640       }
641     }
642
643     priv->in_format = format;
644     priv->align = align;
645   }
646
647   if (priv->codec_data) {
648     GstMapInfo map;
649
650     gst_buffer_map (priv->codec_data, &map, GST_MAP_READ);
651     gst_h265_decoder_parse_codec_data (self, map.data, map.size);
652     gst_buffer_unmap (priv->codec_data, &map);
653   }
654
655   return TRUE;
656 }
657
658 static gboolean
659 gst_h265_decoder_flush (GstVideoDecoder * decoder)
660 {
661   GstH265Decoder *self = GST_H265_DECODER (decoder);
662
663   gst_h265_decoder_clear_dpb (self);
664
665   return TRUE;
666 }
667
668 static GstFlowReturn
669 gst_h265_decoder_drain (GstVideoDecoder * decoder)
670 {
671   GstH265Decoder *self = GST_H265_DECODER (decoder);
672   GstH265DecoderPrivate *priv = self->priv;
673
674   priv->last_ret = GST_FLOW_OK;
675   gst_h265_decoder_output_all_remaining_pics (self);
676   gst_h265_decoder_clear_dpb (self);
677
678   return priv->last_ret;
679 }
680
681 static GstFlowReturn
682 gst_h265_decoder_finish (GstVideoDecoder * decoder)
683 {
684   return gst_h265_decoder_drain (decoder);
685 }
686
687 static gboolean
688 gst_h265_decoder_fill_picture_from_slice (GstH265Decoder * self,
689     const GstH265Slice * slice, GstH265Picture * picture)
690 {
691   GstH265DecoderPrivate *priv = self->priv;
692   const GstH265SliceHdr *slice_hdr = &slice->header;
693   const GstH265NalUnit *nalu = &slice->nalu;
694
695   if (nalu->type >= GST_H265_NAL_SLICE_BLA_W_LP &&
696       nalu->type <= GST_H265_NAL_SLICE_CRA_NUT)
697     picture->RapPicFlag = TRUE;
698
699   /* FIXME: Use SEI header values */
700   picture->field = GST_H265_PICTURE_FIELD_FRAME;
701
702   /* NoRaslOutputFlag == 1 if the current picture is
703    * 1) an IDR picture
704    * 2) a BLA picture
705    * 3) a CRA picture that is the first access unit in the bitstream
706    * 4) first picture that follows an end of sequence NAL unit in decoding order
707    * 5) has HandleCraAsBlaFlag == 1 (set by external means, so not considering )
708    */
709   if (IS_IDR (nalu->type) || IS_BLA (nalu->type) ||
710       (IS_CRA (nalu->type) && priv->new_bitstream) || priv->prev_nal_is_eos) {
711     picture->NoRaslOutputFlag = TRUE;
712   }
713
714   if (IS_IRAP (nalu->type)) {
715     picture->IntraPicFlag = TRUE;
716     priv->associated_irap_NoRaslOutputFlag = picture->NoRaslOutputFlag;
717   }
718
719   if (IS_RASL (nalu->type) && priv->associated_irap_NoRaslOutputFlag) {
720     picture->output_flag = FALSE;
721   } else {
722     picture->output_flag = slice_hdr->pic_output_flag;
723   }
724
725   return TRUE;
726 }
727
728 #define RSV_VCL_N10 10
729 #define RSV_VCL_N12 12
730 #define RSV_VCL_N14 14
731
732 static gboolean
733 nal_is_ref (guint8 nal_type)
734 {
735   gboolean ret = FALSE;
736   switch (nal_type) {
737     case GST_H265_NAL_SLICE_TRAIL_N:
738     case GST_H265_NAL_SLICE_TSA_N:
739     case GST_H265_NAL_SLICE_STSA_N:
740     case GST_H265_NAL_SLICE_RADL_N:
741     case GST_H265_NAL_SLICE_RASL_N:
742     case RSV_VCL_N10:
743     case RSV_VCL_N12:
744     case RSV_VCL_N14:
745       ret = FALSE;
746       break;
747     default:
748       ret = TRUE;
749       break;
750   }
751   return ret;
752 }
753
754 static gboolean
755 gst_h265_decoder_calculate_poc (GstH265Decoder * self,
756     const GstH265Slice * slice, GstH265Picture * picture)
757 {
758   GstH265DecoderPrivate *priv = self->priv;
759   const GstH265SliceHdr *slice_hdr = &slice->header;
760   const GstH265NalUnit *nalu = &slice->nalu;
761   const GstH265SPS *sps = priv->active_sps;
762   gint32 MaxPicOrderCntLsb = 1 << (sps->log2_max_pic_order_cnt_lsb_minus4 + 4);
763
764   GST_DEBUG_OBJECT (self, "decode PicOrderCntVal");
765
766   priv->prev_poc_lsb = priv->poc_lsb;
767   priv->prev_poc_msb = priv->poc_msb;
768
769   if (!(IS_IRAP (nalu->type) && picture->NoRaslOutputFlag)) {
770     priv->prev_poc_lsb = priv->prev_tid0pic_poc_lsb;
771     priv->prev_poc_msb = priv->prev_tid0pic_poc_msb;
772   }
773
774   /* Finding PicOrderCntMsb */
775   if (IS_IRAP (nalu->type) && picture->NoRaslOutputFlag)
776     priv->poc_msb = 0;
777   else {
778     /* (8-1) */
779     if ((slice_hdr->pic_order_cnt_lsb < priv->prev_poc_lsb) &&
780         ((priv->prev_poc_lsb - slice_hdr->pic_order_cnt_lsb) >=
781             (MaxPicOrderCntLsb / 2)))
782       priv->poc_msb = priv->prev_poc_msb + MaxPicOrderCntLsb;
783
784     else if ((slice_hdr->pic_order_cnt_lsb > priv->prev_poc_lsb) &&
785         ((slice_hdr->pic_order_cnt_lsb - priv->prev_poc_lsb) >
786             (MaxPicOrderCntLsb / 2)))
787       priv->poc_msb = priv->prev_poc_msb - MaxPicOrderCntLsb;
788
789     else
790       priv->poc_msb = priv->prev_poc_msb;
791   }
792
793   /* (8-2) */
794   priv->poc = picture->pic_order_cnt =
795       priv->poc_msb + slice_hdr->pic_order_cnt_lsb;
796   priv->poc_lsb = picture->pic_order_cnt_lsb = slice_hdr->pic_order_cnt_lsb;
797
798   if (IS_IDR (nalu->type)) {
799     picture->pic_order_cnt = 0;
800     picture->pic_order_cnt_lsb = 0;
801     priv->poc_lsb = 0;
802     priv->poc_msb = 0;
803     priv->prev_poc_lsb = 0;
804     priv->prev_poc_msb = 0;
805     priv->prev_tid0pic_poc_lsb = 0;
806     priv->prev_tid0pic_poc_msb = 0;
807   }
808
809   GST_DEBUG_OBJECT (self,
810       "PicOrderCntVal %d, (lsb %d)", picture->pic_order_cnt,
811       picture->pic_order_cnt_lsb);
812
813   if (nalu->temporal_id_plus1 == 1 && !IS_RASL (nalu->type) &&
814       !IS_RADL (nalu->type) && nal_is_ref (nalu->type)) {
815     priv->prev_tid0pic_poc_lsb = slice_hdr->pic_order_cnt_lsb;
816     priv->prev_tid0pic_poc_msb = priv->poc_msb;
817   }
818
819   return TRUE;
820 }
821
822 static gboolean
823 gst_h265_decoder_init_current_picture (GstH265Decoder * self)
824 {
825   GstH265DecoderPrivate *priv = self->priv;
826
827   if (!gst_h265_decoder_fill_picture_from_slice (self, &priv->current_slice,
828           priv->current_picture)) {
829     return FALSE;
830   }
831
832   if (!gst_h265_decoder_calculate_poc (self,
833           &priv->current_slice, priv->current_picture))
834     return FALSE;
835
836   return TRUE;
837 }
838
839 static gboolean
840 has_entry_in_rps (GstH265Picture * dpb_pic,
841     GstH265Picture ** rps_list, guint rps_list_length)
842 {
843   guint i;
844
845   if (!dpb_pic || !rps_list || !rps_list_length)
846     return FALSE;
847
848   for (i = 0; i < rps_list_length; i++) {
849     if (rps_list[i] && rps_list[i]->pic_order_cnt == dpb_pic->pic_order_cnt)
850       return TRUE;
851   }
852   return FALSE;
853 }
854
855 static void
856 gst_h265_decoder_derive_and_mark_rps (GstH265Decoder * self,
857     GstH265Picture * picture, gint32 * CurrDeltaPocMsbPresentFlag,
858     gint32 * FollDeltaPocMsbPresentFlag)
859 {
860   GstH265DecoderPrivate *priv = self->priv;
861   guint i;
862   GArray *dpb_array;
863
864   for (i = 0; i < 16; i++) {
865     gst_h265_picture_replace (&self->RefPicSetLtCurr[i], NULL);
866     gst_h265_picture_replace (&self->RefPicSetLtFoll[i], NULL);
867     gst_h265_picture_replace (&self->RefPicSetStCurrBefore[i], NULL);
868     gst_h265_picture_replace (&self->RefPicSetStCurrAfter[i], NULL);
869     gst_h265_picture_replace (&self->RefPicSetStFoll[i], NULL);
870   }
871
872   /* (8-6) */
873   for (i = 0; i < self->NumPocLtCurr; i++) {
874     if (!CurrDeltaPocMsbPresentFlag[i]) {
875       self->RefPicSetLtCurr[i] =
876           gst_h265_dpb_get_ref_by_poc_lsb (priv->dpb, priv->PocLtCurr[i]);
877     } else {
878       self->RefPicSetLtCurr[i] =
879           gst_h265_dpb_get_ref_by_poc (priv->dpb, priv->PocLtCurr[i]);
880     }
881   }
882
883   for (i = 0; i < self->NumPocLtFoll; i++) {
884     if (!FollDeltaPocMsbPresentFlag[i]) {
885       self->RefPicSetLtFoll[i] =
886           gst_h265_dpb_get_ref_by_poc_lsb (priv->dpb, priv->PocLtFoll[i]);
887     } else {
888       self->RefPicSetLtFoll[i] =
889           gst_h265_dpb_get_ref_by_poc (priv->dpb, priv->PocLtFoll[i]);
890     }
891   }
892
893   /* Mark all ref pics in RefPicSetLtCurr and RefPicSetLtFol as long_term_refs */
894   for (i = 0; i < self->NumPocLtCurr; i++) {
895     if (self->RefPicSetLtCurr[i]) {
896       self->RefPicSetLtCurr[i]->ref = TRUE;
897       self->RefPicSetLtCurr[i]->long_term = TRUE;
898     }
899   }
900
901   for (i = 0; i < self->NumPocLtFoll; i++) {
902     if (self->RefPicSetLtFoll[i]) {
903       self->RefPicSetLtFoll[i]->ref = TRUE;
904       self->RefPicSetLtFoll[i]->long_term = TRUE;
905     }
906   }
907
908   /* (8-7) */
909   for (i = 0; i < self->NumPocStCurrBefore; i++) {
910     self->RefPicSetStCurrBefore[i] =
911         gst_h265_dpb_get_short_ref_by_poc (priv->dpb, priv->PocStCurrBefore[i]);
912   }
913
914   for (i = 0; i < self->NumPocStCurrAfter; i++) {
915     self->RefPicSetStCurrAfter[i] =
916         gst_h265_dpb_get_short_ref_by_poc (priv->dpb, priv->PocStCurrAfter[i]);
917   }
918
919   for (i = 0; i < self->NumPocStFoll; i++) {
920     self->RefPicSetStFoll[i] =
921         gst_h265_dpb_get_short_ref_by_poc (priv->dpb, priv->PocStFoll[i]);
922   }
923
924   /* Mark all dpb pics not beloging to RefPicSet*[] as unused for ref */
925   dpb_array = gst_h265_dpb_get_pictures_all (priv->dpb);
926   for (i = 0; i < dpb_array->len; i++) {
927     GstH265Picture *dpb_pic = g_array_index (dpb_array, GstH265Picture *, i);
928
929     if (dpb_pic &&
930         !has_entry_in_rps (dpb_pic, self->RefPicSetLtCurr, self->NumPocLtCurr)
931         && !has_entry_in_rps (dpb_pic, self->RefPicSetLtFoll,
932             self->NumPocLtFoll)
933         && !has_entry_in_rps (dpb_pic, self->RefPicSetStCurrAfter,
934             self->NumPocStCurrAfter)
935         && !has_entry_in_rps (dpb_pic, self->RefPicSetStCurrBefore,
936             self->NumPocStCurrBefore)
937         && !has_entry_in_rps (dpb_pic, self->RefPicSetStFoll,
938             self->NumPocStFoll)) {
939       GST_LOG_OBJECT (self, "Mark Picture %p (poc %d) as non-ref", dpb_pic,
940           dpb_pic->pic_order_cnt);
941       dpb_pic->ref = FALSE;
942       dpb_pic->long_term = FALSE;
943     }
944   }
945
946   g_array_unref (dpb_array);
947 }
948
949 static gboolean
950 gst_h265_decoder_prepare_rps (GstH265Decoder * self, const GstH265Slice * slice,
951     GstH265Picture * picture)
952 {
953   GstH265DecoderPrivate *priv = self->priv;
954   gint32 CurrDeltaPocMsbPresentFlag[16] = { 0, };
955   gint32 FollDeltaPocMsbPresentFlag[16] = { 0, };
956   const GstH265SliceHdr *slice_hdr = &slice->header;
957   const GstH265NalUnit *nalu = &slice->nalu;
958   const GstH265SPS *sps = priv->active_sps;
959   guint32 MaxPicOrderCntLsb = 1 << (sps->log2_max_pic_order_cnt_lsb_minus4 + 4);
960   gint i, j, k;
961
962   /* if it is an irap pic, set all ref pics in dpb as unused for ref */
963   if (IS_IRAP (nalu->type) && picture->NoRaslOutputFlag) {
964     GST_DEBUG_OBJECT (self, "Mark all pictures in DPB as non-ref");
965     gst_h265_dpb_mark_all_non_ref (priv->dpb);
966   }
967
968   /* Reset everything for IDR */
969   if (IS_IDR (nalu->type)) {
970     memset (priv->PocStCurrBefore, 0, sizeof (priv->PocStCurrBefore));
971     memset (priv->PocStCurrAfter, 0, sizeof (priv->PocStCurrAfter));
972     memset (priv->PocStFoll, 0, sizeof (priv->PocStFoll));
973     memset (priv->PocLtCurr, 0, sizeof (priv->PocLtCurr));
974     memset (priv->PocLtFoll, 0, sizeof (priv->PocLtFoll));
975     self->NumPocStCurrBefore = self->NumPocStCurrAfter = self->NumPocStFoll = 0;
976     self->NumPocLtCurr = self->NumPocLtFoll = 0;
977   } else {
978     const GstH265ShortTermRefPicSet *stRefPic = NULL;
979     gint32 num_lt_pics, pocLt;
980     gint32 PocLsbLt[16] = { 0, };
981     gint32 UsedByCurrPicLt[16] = { 0, };
982     gint32 DeltaPocMsbCycleLt[16] = { 0, };
983     gint numtotalcurr = 0;
984
985     /* this is based on CurrRpsIdx described in spec */
986     if (!slice_hdr->short_term_ref_pic_set_sps_flag)
987       stRefPic = &slice_hdr->short_term_ref_pic_sets;
988     else if (sps->num_short_term_ref_pic_sets)
989       stRefPic =
990           &sps->short_term_ref_pic_set[slice_hdr->short_term_ref_pic_set_idx];
991
992     g_assert (stRefPic != NULL);
993
994     GST_LOG_OBJECT (self,
995         "NumDeltaPocs: %d, NumNegativePics: %d, NumPositivePics %d",
996         stRefPic->NumDeltaPocs, stRefPic->NumNegativePics,
997         stRefPic->NumPositivePics);
998
999     for (i = 0, j = 0, k = 0; i < stRefPic->NumNegativePics; i++) {
1000       if (stRefPic->UsedByCurrPicS0[i]) {
1001         priv->PocStCurrBefore[j++] =
1002             picture->pic_order_cnt + stRefPic->DeltaPocS0[i];
1003         numtotalcurr++;
1004       } else
1005         priv->PocStFoll[k++] = picture->pic_order_cnt + stRefPic->DeltaPocS0[i];
1006     }
1007     self->NumPocStCurrBefore = j;
1008     for (i = 0, j = 0; i < stRefPic->NumPositivePics; i++) {
1009       if (stRefPic->UsedByCurrPicS1[i]) {
1010         priv->PocStCurrAfter[j++] =
1011             picture->pic_order_cnt + stRefPic->DeltaPocS1[i];
1012         numtotalcurr++;
1013       } else
1014         priv->PocStFoll[k++] = picture->pic_order_cnt + stRefPic->DeltaPocS1[i];
1015     }
1016     self->NumPocStCurrAfter = j;
1017     self->NumPocStFoll = k;
1018     num_lt_pics = slice_hdr->num_long_term_sps + slice_hdr->num_long_term_pics;
1019     /* The variables PocLsbLt[i] and UsedByCurrPicLt[i] are derived as follows: */
1020     for (i = 0; i < num_lt_pics; i++) {
1021       if (i < slice_hdr->num_long_term_sps) {
1022         PocLsbLt[i] = sps->lt_ref_pic_poc_lsb_sps[slice_hdr->lt_idx_sps[i]];
1023         UsedByCurrPicLt[i] =
1024             sps->used_by_curr_pic_lt_sps_flag[slice_hdr->lt_idx_sps[i]];
1025       } else {
1026         PocLsbLt[i] = slice_hdr->poc_lsb_lt[i];
1027         UsedByCurrPicLt[i] = slice_hdr->used_by_curr_pic_lt_flag[i];
1028       }
1029       if (UsedByCurrPicLt[i])
1030         numtotalcurr++;
1031     }
1032
1033     self->NumPocTotalCurr = numtotalcurr;
1034
1035     /* The variable DeltaPocMsbCycleLt[i] is derived as follows: (7-38) */
1036     for (i = 0; i < num_lt_pics; i++) {
1037       if (i == 0 || i == slice_hdr->num_long_term_sps)
1038         DeltaPocMsbCycleLt[i] = slice_hdr->delta_poc_msb_cycle_lt[i];
1039       else
1040         DeltaPocMsbCycleLt[i] =
1041             slice_hdr->delta_poc_msb_cycle_lt[i] + DeltaPocMsbCycleLt[i - 1];
1042     }
1043
1044     /* (8-5) */
1045     for (i = 0, j = 0, k = 0; i < num_lt_pics; i++) {
1046       pocLt = PocLsbLt[i];
1047       if (slice_hdr->delta_poc_msb_present_flag[i])
1048         pocLt +=
1049             picture->pic_order_cnt - DeltaPocMsbCycleLt[i] * MaxPicOrderCntLsb -
1050             slice_hdr->pic_order_cnt_lsb;
1051       if (UsedByCurrPicLt[i]) {
1052         priv->PocLtCurr[j] = pocLt;
1053         CurrDeltaPocMsbPresentFlag[j++] =
1054             slice_hdr->delta_poc_msb_present_flag[i];
1055       } else {
1056         priv->PocLtFoll[k] = pocLt;
1057         FollDeltaPocMsbPresentFlag[k++] =
1058             slice_hdr->delta_poc_msb_present_flag[i];
1059       }
1060     }
1061     self->NumPocLtCurr = j;
1062     self->NumPocLtFoll = k;
1063   }
1064
1065   GST_LOG_OBJECT (self, "NumPocStCurrBefore: %d", self->NumPocStCurrBefore);
1066   GST_LOG_OBJECT (self, "NumPocStCurrAfter:  %d", self->NumPocStCurrAfter);
1067   GST_LOG_OBJECT (self, "NumPocStFoll:       %d", self->NumPocStFoll);
1068   GST_LOG_OBJECT (self, "NumPocLtCurr:       %d", self->NumPocLtCurr);
1069   GST_LOG_OBJECT (self, "NumPocLtFoll:       %d", self->NumPocLtFoll);
1070   GST_LOG_OBJECT (self, "NumPocTotalCurr:    %d", self->NumPocTotalCurr);
1071
1072   /* the derivation process for the RPS and the picture marking */
1073   gst_h265_decoder_derive_and_mark_rps (self, picture,
1074       CurrDeltaPocMsbPresentFlag, FollDeltaPocMsbPresentFlag);
1075
1076   return TRUE;
1077 }
1078
1079 static void
1080 gst_h265_decoder_clear_dpb (GstH265Decoder * self)
1081 {
1082   GstH265DecoderPrivate *priv = self->priv;
1083
1084   gst_h265_dpb_clear (priv->dpb);
1085   priv->last_output_poc = -1;
1086 }
1087
1088 static void
1089 gst_h265_decoder_do_output_picture (GstH265Decoder * self,
1090     GstH265Picture * picture)
1091 {
1092   GstH265DecoderPrivate *priv = self->priv;
1093   GstH265DecoderClass *klass;
1094
1095   picture->outputted = TRUE;
1096
1097   if (picture->pic_order_cnt < priv->last_output_poc) {
1098     GST_WARNING_OBJECT (self,
1099         "Outputting out of order %d -> %d, likely a broken stream",
1100         priv->last_output_poc, picture->pic_order_cnt);
1101   }
1102
1103   priv->last_output_poc = picture->pic_order_cnt;
1104
1105   klass = GST_H265_DECODER_GET_CLASS (self);
1106
1107   if (klass->output_picture)
1108     priv->last_ret = klass->output_picture (self, picture);
1109 }
1110
1111 static gint
1112 poc_asc_compare (const GstH265Picture * a, const GstH265Picture * b)
1113 {
1114   return a->pic_order_cnt > b->pic_order_cnt;
1115 }
1116
1117 static gboolean
1118 gst_h265_decoder_output_all_remaining_pics (GstH265Decoder * self)
1119 {
1120   GstH265DecoderPrivate *priv = self->priv;
1121   GList *to_output = NULL;
1122   GList *iter;
1123
1124   gst_h265_dpb_get_pictures_not_outputted (priv->dpb, &to_output);
1125
1126   to_output = g_list_sort (to_output, (GCompareFunc) poc_asc_compare);
1127
1128   for (iter = to_output; iter; iter = g_list_next (iter)) {
1129     GstH265Picture *picture = (GstH265Picture *) iter->data;
1130
1131     GST_LOG_OBJECT (self, "Output picture %p (poc %d)", picture,
1132         picture->pic_order_cnt);
1133     gst_h265_decoder_do_output_picture (self, picture);
1134   }
1135
1136   if (to_output)
1137     g_list_free_full (to_output, (GDestroyNotify) gst_h265_picture_unref);
1138
1139   return TRUE;
1140 }
1141
1142 /* C.5.2.2 */
1143 static gboolean
1144 gst_h265_decoder_dpb_init (GstH265Decoder * self, const GstH265Slice * slice,
1145     GstH265Picture * picture)
1146 {
1147   GstH265DecoderPrivate *priv = self->priv;
1148   const GstH265SliceHdr *slice_hdr = &slice->header;
1149   const GstH265NalUnit *nalu = &slice->nalu;
1150
1151   if (IS_IRAP (nalu->type) && picture->NoRaslOutputFlag && !priv->new_bitstream) {
1152     if (nalu->type == GST_H265_NAL_SLICE_CRA_NUT)
1153       picture->NoOutputOfPriorPicsFlag = TRUE;
1154     else
1155       picture->NoOutputOfPriorPicsFlag =
1156           slice_hdr->no_output_of_prior_pics_flag;
1157
1158     if (picture->NoOutputOfPriorPicsFlag) {
1159       GST_DEBUG_OBJECT (self, "Clear dpb");
1160       gst_h265_decoder_drain (GST_VIDEO_DECODER (self));
1161     }
1162   }
1163
1164   return TRUE;
1165 }
1166
1167 static gboolean
1168 gst_h265_decoder_start_current_picture (GstH265Decoder * self)
1169 {
1170   GstH265DecoderClass *klass;
1171   GstH265DecoderPrivate *priv = self->priv;
1172   gboolean ret = TRUE;
1173
1174   g_assert (priv->current_picture != NULL);
1175   g_assert (priv->active_sps != NULL);
1176   g_assert (priv->active_pps != NULL);
1177
1178   if (!gst_h265_decoder_init_current_picture (self))
1179     return FALSE;
1180
1181   /* Drop all RASL pictures having NoRaslOutputFlag is TRUE for the
1182    * associated IRAP picture */
1183   if (IS_RASL (priv->current_slice.nalu.type) &&
1184       priv->associated_irap_NoRaslOutputFlag) {
1185     GST_DEBUG_OBJECT (self, "Drop current picture");
1186     gst_h265_picture_replace (&priv->current_picture, NULL);
1187     return TRUE;
1188   }
1189
1190   gst_h265_decoder_prepare_rps (self, &priv->current_slice,
1191       priv->current_picture);
1192
1193   gst_h265_decoder_dpb_init (self, &priv->current_slice, priv->current_picture);
1194
1195   klass = GST_H265_DECODER_GET_CLASS (self);
1196   if (klass->start_picture)
1197     ret = klass->start_picture (self, priv->current_picture,
1198         &priv->current_slice, priv->dpb);
1199
1200   if (!ret) {
1201     GST_ERROR_OBJECT (self, "subclass does not want to start picture");
1202     return FALSE;
1203   }
1204
1205   return TRUE;
1206 }
1207
1208 static gboolean
1209 gst_h265_decoder_finish_picture (GstH265Decoder * self,
1210     GstH265Picture * picture)
1211 {
1212   GstH265DecoderPrivate *priv = self->priv;
1213   const GstH265SPS *sps = priv->active_sps;
1214   GList *not_outputted = NULL;
1215   guint num_remaining;
1216   GList *iter;
1217 #ifndef GST_DISABLE_GST_DEBUG
1218   gint i;
1219 #endif
1220
1221   /* Remove unused (for reference or later output) pictures from DPB, marking
1222    * them as such */
1223   gst_h265_dpb_delete_unused (priv->dpb);
1224
1225   GST_LOG_OBJECT (self,
1226       "Finishing picture %p (poc %d), entries in DPB %d",
1227       picture, picture->pic_order_cnt, gst_h265_dpb_get_size (priv->dpb));
1228
1229   /* The ownership of pic will either be transferred to DPB - if the picture is
1230    * still needed (for output and/or reference) - or we will release it
1231    * immediately if we manage to output it here and won't have to store it for
1232    * future reference */
1233
1234   /* Get all pictures that haven't been outputted yet */
1235   gst_h265_dpb_get_pictures_not_outputted (priv->dpb, &not_outputted);
1236
1237   /* C.5.2.3 */
1238   for (iter = not_outputted; iter; iter = g_list_next (iter)) {
1239     GstH265Picture *other = GST_H265_PICTURE (iter->data);
1240
1241     if (!other->outputted)
1242       other->pic_latency_cnt++;
1243   }
1244
1245   if (picture->output_flag) {
1246     picture->outputted = FALSE;
1247     picture->pic_latency_cnt = 0;
1248   } else {
1249     picture->outputted = TRUE;
1250   }
1251
1252   /* set pic as short_term_ref */
1253   picture->ref = TRUE;
1254   picture->long_term = FALSE;
1255
1256   /* Include the one we've just decoded */
1257   not_outputted = g_list_append (not_outputted, picture);
1258
1259   /* for debugging */
1260 #ifndef GST_DISABLE_GST_DEBUG
1261   GST_TRACE_OBJECT (self, "Before sorting not outputted list");
1262   i = 0;
1263   for (iter = not_outputted; iter; iter = g_list_next (iter)) {
1264     GstH265Picture *tmp = (GstH265Picture *) iter->data;
1265
1266     GST_TRACE_OBJECT (self,
1267         "\t%dth picture %p (poc %d)", i, tmp, tmp->pic_order_cnt);
1268     i++;
1269   }
1270 #endif
1271
1272   /* Sort in output order */
1273   not_outputted = g_list_sort (not_outputted, (GCompareFunc) poc_asc_compare);
1274
1275 #ifndef GST_DISABLE_GST_DEBUG
1276   GST_TRACE_OBJECT (self,
1277       "After sorting not outputted list in poc ascending order");
1278   i = 0;
1279   for (iter = not_outputted; iter; iter = g_list_next (iter)) {
1280     GstH265Picture *tmp = (GstH265Picture *) iter->data;
1281
1282     GST_TRACE_OBJECT (self,
1283         "\t%dth picture %p (poc %d)", i, tmp, tmp->pic_order_cnt);
1284     i++;
1285   }
1286 #endif
1287
1288   /* Try to output as many pictures as we can. A picture can be output,
1289    * if the number of decoded and not yet outputted pictures that would remain
1290    * in DPB afterwards would at least be equal to max_num_reorder_frames.
1291    * If the outputted picture is not a reference picture, it doesn't have
1292    * to remain in the DPB and can be removed */
1293   iter = not_outputted;
1294   num_remaining = g_list_length (not_outputted);
1295
1296   while (num_remaining > sps->max_num_reorder_pics[sps->max_sub_layers_minus1]
1297       || (num_remaining &&
1298           sps->max_latency_increase_plus1[sps->max_sub_layers_minus1] &&
1299           !GST_H265_PICTURE (iter->data)->outputted &&
1300           GST_H265_PICTURE (iter->data)->pic_latency_cnt >=
1301           priv->SpsMaxLatencyPictures)) {
1302     GstH265Picture *to_output = GST_H265_PICTURE (iter->data);
1303
1304     GST_LOG_OBJECT (self,
1305         "Output picture %p (poc %d)", to_output, to_output->pic_order_cnt);
1306     gst_h265_decoder_do_output_picture (self, to_output);
1307     if (!to_output->ref) {
1308       /* Current picture hasn't been inserted into DPB yet, so don't remove it
1309        * if we managed to output it immediately */
1310       gint outputted_poc = to_output->pic_order_cnt;
1311       if (outputted_poc != picture->pic_order_cnt) {
1312         GST_LOG_OBJECT (self, "Delete picture %p (poc %d) from DPB",
1313             to_output, to_output->pic_order_cnt);
1314         gst_h265_dpb_delete_by_poc (priv->dpb, outputted_poc);
1315       }
1316     }
1317
1318     iter = g_list_next (iter);
1319     num_remaining--;
1320   }
1321
1322   /* If we haven't managed to output the picture that we just decoded, or if
1323    * it's a reference picture, we have to store it in DPB */
1324   if (!picture->outputted || picture->ref) {
1325     if (gst_h265_dpb_is_full (priv->dpb)) {
1326       /* If we haven't managed to output anything to free up space in DPB
1327        * to store this picture, it's an error in the stream */
1328       GST_WARNING_OBJECT (self, "Could not free up space in DPB");
1329       return FALSE;
1330     }
1331
1332     GST_TRACE_OBJECT (self,
1333         "Put picture %p (outputted %d, ref %d, poc %d) to dpb",
1334         picture, picture->outputted, picture->ref, picture->pic_order_cnt);
1335     gst_h265_dpb_add (priv->dpb, gst_h265_picture_ref (picture));
1336   }
1337
1338   if (not_outputted)
1339     g_list_free_full (not_outputted, (GDestroyNotify) gst_h265_picture_unref);
1340
1341   return TRUE;
1342 }
1343
1344 static gboolean
1345 gst_h265_decoder_finish_current_picture (GstH265Decoder * self)
1346 {
1347   GstH265DecoderPrivate *priv = self->priv;
1348   GstH265DecoderClass *klass;
1349   gboolean ret = TRUE;
1350
1351   if (!priv->current_picture)
1352     return TRUE;
1353
1354   klass = GST_H265_DECODER_GET_CLASS (self);
1355
1356   if (klass->end_picture)
1357     ret = klass->end_picture (self, priv->current_picture);
1358
1359   /* finish picture takes ownership of the picture */
1360   ret = gst_h265_decoder_finish_picture (self, priv->current_picture);
1361   priv->current_picture = NULL;
1362
1363   if (!ret) {
1364     GST_ERROR_OBJECT (self, "Failed to finish picture");
1365     return FALSE;
1366   }
1367
1368   return TRUE;
1369 }
1370
1371 static GstFlowReturn
1372 gst_h265_decoder_handle_frame (GstVideoDecoder * decoder,
1373     GstVideoCodecFrame * frame)
1374 {
1375   GstH265Decoder *self = GST_H265_DECODER (decoder);
1376   GstH265DecoderPrivate *priv = self->priv;
1377   GstBuffer *in_buf = frame->input_buffer;
1378   GstH265NalUnit nalu;
1379   GstH265ParserResult pres;
1380   GstMapInfo map;
1381   gboolean decode_ret = TRUE;
1382
1383   GST_LOG_OBJECT (self,
1384       "handle frame, PTS: %" GST_TIME_FORMAT ", DTS: %"
1385       GST_TIME_FORMAT, GST_TIME_ARGS (GST_BUFFER_PTS (in_buf)),
1386       GST_TIME_ARGS (GST_BUFFER_DTS (in_buf)));
1387
1388   priv->current_frame = frame;
1389   priv->last_ret = GST_FLOW_OK;
1390
1391   gst_buffer_map (in_buf, &map, GST_MAP_READ);
1392   if (priv->in_format == GST_H265_DECODER_FORMAT_HVC1 ||
1393       priv->in_format == GST_H265_DECODER_FORMAT_HEV1) {
1394     pres = gst_h265_parser_identify_nalu_hevc (priv->parser,
1395         map.data, 0, map.size, priv->nal_length_size, &nalu);
1396
1397     while (pres == GST_H265_PARSER_OK && decode_ret) {
1398       decode_ret = gst_h265_decoder_decode_nal (self,
1399           &nalu, GST_BUFFER_PTS (in_buf));
1400
1401       pres = gst_h265_parser_identify_nalu_hevc (priv->parser,
1402           map.data, nalu.offset + nalu.size, map.size, priv->nal_length_size,
1403           &nalu);
1404     }
1405   } else {
1406     pres = gst_h265_parser_identify_nalu (priv->parser,
1407         map.data, 0, map.size, &nalu);
1408
1409     if (pres == GST_H265_PARSER_NO_NAL_END)
1410       pres = GST_H265_PARSER_OK;
1411
1412     while (pres == GST_H265_PARSER_OK && decode_ret) {
1413       decode_ret = gst_h265_decoder_decode_nal (self,
1414           &nalu, GST_BUFFER_PTS (in_buf));
1415
1416       pres = gst_h265_parser_identify_nalu (priv->parser,
1417           map.data, nalu.offset + nalu.size, map.size, &nalu);
1418
1419       if (pres == GST_H265_PARSER_NO_NAL_END)
1420         pres = GST_H265_PARSER_OK;
1421     }
1422   }
1423
1424   gst_buffer_unmap (in_buf, &map);
1425   priv->current_frame = NULL;
1426
1427   if (!decode_ret) {
1428     GST_VIDEO_DECODER_ERROR (self, 1, STREAM, DECODE,
1429         ("Failed to decode data"), (NULL), priv->last_ret);
1430     gst_video_decoder_drop_frame (decoder, frame);
1431
1432     gst_h265_picture_clear (&priv->current_picture);
1433
1434     return priv->last_ret;
1435   }
1436
1437   gst_h265_decoder_finish_current_picture (self);
1438   gst_video_codec_frame_unref (frame);
1439
1440   return priv->last_ret;
1441 }