h264decoder,h265decoder: Do not hold codec_data buffer
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-bad / gst-libs / gst / codecs / gsth264decoder.c
1 /* GStreamer
2  * Copyright (C) 2019 Seungha Yang <seungha.yang@navercorp.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  *
19  * NOTE: some of implementations are copied/modified from Chromium code
20  *
21  * Copyright 2015 The Chromium Authors. All rights reserved.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions are
25  * met:
26  *
27  *    * Redistributions of source code must retain the above copyright
28  * notice, this list of conditions and the following disclaimer.
29  *    * Redistributions in binary form must reproduce the above
30  * copyright notice, this list of conditions and the following disclaimer
31  * in the documentation and/or other materials provided with the
32  * distribution.
33  *    * Neither the name of Google Inc. nor the names of its
34  * contributors may be used to endorse or promote products derived from
35  * this software without specific prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
38  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
39  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
40  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
41  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
44  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
45  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
46  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
47  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48  */
49 /**
50  * SECTION:gsth264decoder
51  * @title: GstH264Decoder
52  * @short_description: Base class to implement stateless H.264 decoders
53  * @sources:
54  * - gsth264picture.h
55  */
56
57 #ifdef HAVE_CONFIG_H
58 #include <config.h>
59 #endif
60
61 #include <gst/base/base.h>
62 #include "gsth264decoder.h"
63
64 GST_DEBUG_CATEGORY (gst_h264_decoder_debug);
65 #define GST_CAT_DEFAULT gst_h264_decoder_debug
66
67 typedef enum
68 {
69   GST_H264_DECODER_FORMAT_NONE,
70   GST_H264_DECODER_FORMAT_AVC,
71   GST_H264_DECODER_FORMAT_BYTE
72 } GstH264DecoderFormat;
73
74 typedef enum
75 {
76   GST_H264_DECODER_ALIGN_NONE,
77   GST_H264_DECODER_ALIGN_NAL,
78   GST_H264_DECODER_ALIGN_AU
79 } GstH264DecoderAlign;
80
81 struct _GstH264DecoderPrivate
82 {
83   GstH264DecoderCompliance compliance;
84
85   guint8 profile_idc;
86   gint width, height;
87
88   guint nal_length_size;
89
90   /* state */
91   GstH264DecoderFormat in_format;
92   GstH264DecoderAlign align;
93   GstH264NalParser *parser;
94   GstH264Dpb *dpb;
95   /* Cache last field which can not enter the DPB, should be a non ref */
96   GstH264Picture *last_field;
97
98   /* used for low-latency vs. high throughput mode decision */
99   gboolean is_live;
100
101   /* sps/pps of the current slice */
102   const GstH264SPS *active_sps;
103   const GstH264PPS *active_pps;
104
105   /* Picture currently being processed/decoded */
106   GstH264Picture *current_picture;
107   GstVideoCodecFrame *current_frame;
108
109   /* Slice (slice header + nalu) currently being processed/decodec */
110   GstH264Slice current_slice;
111
112   gint max_frame_num;
113   gint max_pic_num;
114   gint max_long_term_frame_idx;
115
116   gint prev_frame_num;
117   gint prev_ref_frame_num;
118   gint prev_frame_num_offset;
119   gboolean prev_has_memmgmnt5;
120
121   /* Values related to previously decoded reference picture */
122   gboolean prev_ref_has_memmgmnt5;
123   gint prev_ref_top_field_order_cnt;
124   gint prev_ref_pic_order_cnt_msb;
125   gint prev_ref_pic_order_cnt_lsb;
126
127   GstH264PictureField prev_ref_field;
128
129   /* PicOrderCount of the previously outputted frame */
130   gint last_output_poc;
131
132   gboolean process_ref_pic_lists;
133   guint preferred_output_delay;
134
135   /* Reference picture lists, constructed for each frame */
136   GArray *ref_pic_list_p0;
137   GArray *ref_pic_list_b0;
138   GArray *ref_pic_list_b1;
139
140   /* Temporary picture list, for reference picture lists in fields,
141    * corresponding to 8.2.4.2.2 refFrameList0ShortTerm, refFrameList0LongTerm
142    * and 8.2.4.2.5 refFrameList1ShortTerm and refFrameListLongTerm */
143   GArray *ref_frame_list_0_short_term;
144   GArray *ref_frame_list_1_short_term;
145   GArray *ref_frame_list_long_term;
146
147   /* Reference picture lists, constructed for each slice */
148   GArray *ref_pic_list0;
149   GArray *ref_pic_list1;
150
151   /* For delayed output */
152   GstQueueArray *output_queue;
153 };
154
155 typedef struct
156 {
157   /* Holds ref */
158   GstVideoCodecFrame *frame;
159   GstH264Picture *picture;
160   /* Without ref */
161   GstH264Decoder *self;
162 } GstH264DecoderOutputFrame;
163
164 #define UPDATE_FLOW_RETURN(ret,new_ret) G_STMT_START { \
165   if (*(ret) == GST_FLOW_OK) \
166     *(ret) = new_ret; \
167 } G_STMT_END
168
169 #define parent_class gst_h264_decoder_parent_class
170 G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GstH264Decoder, gst_h264_decoder,
171     GST_TYPE_VIDEO_DECODER,
172     G_ADD_PRIVATE (GstH264Decoder);
173     GST_DEBUG_CATEGORY_INIT (gst_h264_decoder_debug, "h264decoder", 0,
174         "H.264 Video Decoder"));
175
176 static void gst_h264_decoder_finalize (GObject * object);
177
178 static gboolean gst_h264_decoder_start (GstVideoDecoder * decoder);
179 static gboolean gst_h264_decoder_stop (GstVideoDecoder * decoder);
180 static gboolean gst_h264_decoder_set_format (GstVideoDecoder * decoder,
181     GstVideoCodecState * state);
182 static GstFlowReturn gst_h264_decoder_finish (GstVideoDecoder * decoder);
183 static gboolean gst_h264_decoder_flush (GstVideoDecoder * decoder);
184 static GstFlowReturn gst_h264_decoder_drain (GstVideoDecoder * decoder);
185 static GstFlowReturn gst_h264_decoder_handle_frame (GstVideoDecoder * decoder,
186     GstVideoCodecFrame * frame);
187
188 /* codec specific functions */
189 static GstFlowReturn gst_h264_decoder_process_sps (GstH264Decoder * self,
190     GstH264SPS * sps);
191 static GstFlowReturn gst_h264_decoder_decode_slice (GstH264Decoder * self);
192 static GstFlowReturn gst_h264_decoder_decode_nal (GstH264Decoder * self,
193     GstH264NalUnit * nalu);
194 static gboolean gst_h264_decoder_fill_picture_from_slice (GstH264Decoder * self,
195     const GstH264Slice * slice, GstH264Picture * picture);
196 static gboolean gst_h264_decoder_calculate_poc (GstH264Decoder * self,
197     GstH264Picture * picture);
198 static gboolean gst_h264_decoder_init_gap_picture (GstH264Decoder * self,
199     GstH264Picture * picture, gint frame_num);
200 static GstFlowReturn gst_h264_decoder_drain_internal (GstH264Decoder * self);
201 static void gst_h264_decoder_finish_current_picture (GstH264Decoder * self,
202     GstFlowReturn * ret);
203 static void gst_h264_decoder_finish_picture (GstH264Decoder * self,
204     GstH264Picture * picture, GstFlowReturn * ret);
205 static void gst_h264_decoder_prepare_ref_pic_lists (GstH264Decoder * self,
206     GstH264Picture * current_picture);
207 static void gst_h264_decoder_clear_ref_pic_lists (GstH264Decoder * self);
208 static gboolean gst_h264_decoder_modify_ref_pic_lists (GstH264Decoder * self);
209 static gboolean
210 gst_h264_decoder_sliding_window_picture_marking (GstH264Decoder * self,
211     GstH264Picture * picture);
212 static void gst_h264_decoder_do_output_picture (GstH264Decoder * self,
213     GstH264Picture * picture, GstFlowReturn * ret);
214 static GstH264Picture *gst_h264_decoder_new_field_picture (GstH264Decoder *
215     self, GstH264Picture * picture);
216 static void
217 gst_h264_decoder_clear_output_frame (GstH264DecoderOutputFrame * output_frame);
218
219 enum
220 {
221   PROP_0,
222   PROP_COMPLIANCE,
223 };
224
225 /**
226  * gst_h264_decoder_compliance_get_type:
227  *
228  * Get the compliance type of the h264 decoder.
229  *
230  * Since: 1.20
231  */
232 GType
233 gst_h264_decoder_compliance_get_type (void)
234 {
235   static gsize h264_decoder_compliance_type = 0;
236   static const GEnumValue compliances[] = {
237     {GST_H264_DECODER_COMPLIANCE_AUTO, "GST_H264_DECODER_COMPLIANCE_AUTO",
238         "auto"},
239     {GST_H264_DECODER_COMPLIANCE_STRICT, "GST_H264_DECODER_COMPLIANCE_STRICT",
240         "strict"},
241     {GST_H264_DECODER_COMPLIANCE_NORMAL, "GST_H264_DECODER_COMPLIANCE_NORMAL",
242         "normal"},
243     {GST_H264_DECODER_COMPLIANCE_FLEXIBLE,
244         "GST_H264_DECODER_COMPLIANCE_FLEXIBLE", "flexible"},
245     {0, NULL, NULL},
246   };
247
248
249   if (g_once_init_enter (&h264_decoder_compliance_type)) {
250     GType _type;
251
252     _type = g_enum_register_static ("GstH264DecoderCompliance", compliances);
253     g_once_init_leave (&h264_decoder_compliance_type, _type);
254   }
255
256   return (GType) h264_decoder_compliance_type;
257 }
258
259 static void
260 gst_h264_decoder_get_property (GObject * object, guint property_id,
261     GValue * value, GParamSpec * pspec)
262 {
263   GstH264Decoder *self = GST_H264_DECODER (object);
264   GstH264DecoderPrivate *priv = self->priv;
265
266   switch (property_id) {
267     case PROP_COMPLIANCE:
268       GST_OBJECT_LOCK (self);
269       g_value_set_enum (value, priv->compliance);
270       GST_OBJECT_UNLOCK (self);
271       break;
272     default:
273       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
274       break;
275   }
276 }
277
278 static void
279 gst_h264_decoder_set_property (GObject * object, guint property_id,
280     const GValue * value, GParamSpec * pspec)
281 {
282   GstH264Decoder *self = GST_H264_DECODER (object);
283   GstH264DecoderPrivate *priv = self->priv;
284
285   switch (property_id) {
286     case PROP_COMPLIANCE:
287       GST_OBJECT_LOCK (self);
288       priv->compliance = g_value_get_enum (value);
289       GST_OBJECT_UNLOCK (self);
290       break;
291     default:
292       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
293       break;
294   }
295 }
296
297 static void
298 gst_h264_decoder_class_init (GstH264DecoderClass * klass)
299 {
300   GstVideoDecoderClass *decoder_class = GST_VIDEO_DECODER_CLASS (klass);
301   GObjectClass *object_class = G_OBJECT_CLASS (klass);
302
303   object_class->finalize = GST_DEBUG_FUNCPTR (gst_h264_decoder_finalize);
304   object_class->get_property = gst_h264_decoder_get_property;
305   object_class->set_property = gst_h264_decoder_set_property;
306
307   decoder_class->start = GST_DEBUG_FUNCPTR (gst_h264_decoder_start);
308   decoder_class->stop = GST_DEBUG_FUNCPTR (gst_h264_decoder_stop);
309   decoder_class->set_format = GST_DEBUG_FUNCPTR (gst_h264_decoder_set_format);
310   decoder_class->finish = GST_DEBUG_FUNCPTR (gst_h264_decoder_finish);
311   decoder_class->flush = GST_DEBUG_FUNCPTR (gst_h264_decoder_flush);
312   decoder_class->drain = GST_DEBUG_FUNCPTR (gst_h264_decoder_drain);
313   decoder_class->handle_frame =
314       GST_DEBUG_FUNCPTR (gst_h264_decoder_handle_frame);
315
316   /**
317    * GstH264Decoder:compliance:
318    *
319    * The compliance controls the behavior of the decoder to handle some
320    * subtle cases and contexts, such as the low-latency DPB bumping or
321    * mapping the baseline profile as the constrained-baseline profile,
322    * etc.
323    *
324    * Since: 1.20
325    */
326   g_object_class_install_property (object_class, PROP_COMPLIANCE,
327       g_param_spec_enum ("compliance", "Decoder Compliance",
328           "The decoder's behavior in compliance with the h264 spec.",
329           GST_TYPE_H264_DECODER_COMPLIANCE, GST_H264_DECODER_COMPLIANCE_AUTO,
330           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT));
331 }
332
333 static void
334 gst_h264_decoder_init (GstH264Decoder * self)
335 {
336   GstH264DecoderPrivate *priv;
337
338   gst_video_decoder_set_packetized (GST_VIDEO_DECODER (self), TRUE);
339
340   self->priv = priv = gst_h264_decoder_get_instance_private (self);
341
342   priv->last_output_poc = G_MININT32;
343
344   priv->ref_pic_list_p0 = g_array_sized_new (FALSE, TRUE,
345       sizeof (GstH264Picture *), 32);
346   g_array_set_clear_func (priv->ref_pic_list_p0,
347       (GDestroyNotify) gst_clear_h264_picture);
348
349   priv->ref_pic_list_b0 = g_array_sized_new (FALSE, TRUE,
350       sizeof (GstH264Picture *), 32);
351   g_array_set_clear_func (priv->ref_pic_list_b0,
352       (GDestroyNotify) gst_clear_h264_picture);
353
354   priv->ref_pic_list_b1 = g_array_sized_new (FALSE, TRUE,
355       sizeof (GstH264Picture *), 32);
356   g_array_set_clear_func (priv->ref_pic_list_b1,
357       (GDestroyNotify) gst_clear_h264_picture);
358
359   priv->ref_frame_list_0_short_term = g_array_sized_new (FALSE, TRUE,
360       sizeof (GstH264Picture *), 32);
361   g_array_set_clear_func (priv->ref_frame_list_0_short_term,
362       (GDestroyNotify) gst_clear_h264_picture);
363
364   priv->ref_frame_list_1_short_term = g_array_sized_new (FALSE, TRUE,
365       sizeof (GstH264Picture *), 32);
366   g_array_set_clear_func (priv->ref_frame_list_1_short_term,
367       (GDestroyNotify) gst_clear_h264_picture);
368
369   priv->ref_frame_list_long_term = g_array_sized_new (FALSE, TRUE,
370       sizeof (GstH264Picture *), 32);
371   g_array_set_clear_func (priv->ref_frame_list_long_term,
372       (GDestroyNotify) gst_clear_h264_picture);
373
374   priv->ref_pic_list0 = g_array_sized_new (FALSE, TRUE,
375       sizeof (GstH264Picture *), 32);
376   priv->ref_pic_list1 = g_array_sized_new (FALSE, TRUE,
377       sizeof (GstH264Picture *), 32);
378
379   priv->output_queue =
380       gst_queue_array_new_for_struct (sizeof (GstH264DecoderOutputFrame), 1);
381   gst_queue_array_set_clear_func (priv->output_queue,
382       (GDestroyNotify) gst_h264_decoder_clear_output_frame);
383 }
384
385 static void
386 gst_h264_decoder_finalize (GObject * object)
387 {
388   GstH264Decoder *self = GST_H264_DECODER (object);
389   GstH264DecoderPrivate *priv = self->priv;
390
391   g_array_unref (priv->ref_pic_list_p0);
392   g_array_unref (priv->ref_pic_list_b0);
393   g_array_unref (priv->ref_pic_list_b1);
394   g_array_unref (priv->ref_frame_list_0_short_term);
395   g_array_unref (priv->ref_frame_list_1_short_term);
396   g_array_unref (priv->ref_frame_list_long_term);
397   g_array_unref (priv->ref_pic_list0);
398   g_array_unref (priv->ref_pic_list1);
399   gst_queue_array_free (priv->output_queue);
400
401   G_OBJECT_CLASS (parent_class)->finalize (object);
402 }
403
404 static void
405 gst_h264_decoder_reset (GstH264Decoder * self)
406 {
407   GstH264DecoderPrivate *priv = self->priv;
408
409   g_clear_pointer (&self->input_state, gst_video_codec_state_unref);
410   g_clear_pointer (&priv->parser, gst_h264_nal_parser_free);
411   g_clear_pointer (&priv->dpb, gst_h264_dpb_free);
412   gst_clear_h264_picture (&priv->last_field);
413
414   priv->profile_idc = 0;
415   priv->width = 0;
416   priv->height = 0;
417   priv->nal_length_size = 4;
418 }
419
420 static gboolean
421 gst_h264_decoder_start (GstVideoDecoder * decoder)
422 {
423   GstH264Decoder *self = GST_H264_DECODER (decoder);
424   GstH264DecoderPrivate *priv = self->priv;
425
426   gst_h264_decoder_reset (self);
427
428   priv->parser = gst_h264_nal_parser_new ();
429   priv->dpb = gst_h264_dpb_new ();
430
431   return TRUE;
432 }
433
434 static gboolean
435 gst_h264_decoder_stop (GstVideoDecoder * decoder)
436 {
437   GstH264Decoder *self = GST_H264_DECODER (decoder);
438
439   gst_h264_decoder_reset (self);
440
441   return TRUE;
442 }
443
444 static void
445 gst_h264_decoder_clear_output_frame (GstH264DecoderOutputFrame * output_frame)
446 {
447   if (!output_frame)
448     return;
449
450   if (output_frame->frame) {
451     gst_video_decoder_release_frame (GST_VIDEO_DECODER (output_frame->self),
452         output_frame->frame);
453     output_frame->frame = NULL;
454   }
455
456   gst_clear_h264_picture (&output_frame->picture);
457 }
458
459 static void
460 gst_h264_decoder_clear_dpb (GstH264Decoder * self, gboolean flush)
461 {
462   GstVideoDecoder *decoder = GST_VIDEO_DECODER (self);
463   GstH264DecoderPrivate *priv = self->priv;
464   GstH264Picture *picture;
465
466   /* If we are not flushing now, videodecoder baseclass will hold
467    * GstVideoCodecFrame. Release frames manually */
468   if (!flush) {
469     while ((picture = gst_h264_dpb_bump (priv->dpb, TRUE)) != NULL) {
470       GstVideoCodecFrame *frame = gst_video_decoder_get_frame (decoder,
471           picture->system_frame_number);
472
473       if (frame)
474         gst_video_decoder_release_frame (decoder, frame);
475       gst_h264_picture_unref (picture);
476     }
477   }
478
479   gst_queue_array_clear (priv->output_queue);
480   gst_h264_decoder_clear_ref_pic_lists (self);
481   gst_clear_h264_picture (&priv->last_field);
482   gst_h264_dpb_clear (priv->dpb);
483   priv->last_output_poc = G_MININT32;
484 }
485
486 static gboolean
487 gst_h264_decoder_flush (GstVideoDecoder * decoder)
488 {
489   GstH264Decoder *self = GST_H264_DECODER (decoder);
490
491   gst_h264_decoder_clear_dpb (self, TRUE);
492
493   return TRUE;
494 }
495
496 static GstFlowReturn
497 gst_h264_decoder_drain (GstVideoDecoder * decoder)
498 {
499   GstH264Decoder *self = GST_H264_DECODER (decoder);
500
501   /* dpb will be cleared by this method */
502   return gst_h264_decoder_drain_internal (self);
503 }
504
505 static GstFlowReturn
506 gst_h264_decoder_finish (GstVideoDecoder * decoder)
507 {
508   return gst_h264_decoder_drain (decoder);
509 }
510
511 static GstFlowReturn
512 gst_h264_decoder_handle_frame (GstVideoDecoder * decoder,
513     GstVideoCodecFrame * frame)
514 {
515   GstH264Decoder *self = GST_H264_DECODER (decoder);
516   GstH264DecoderPrivate *priv = self->priv;
517   GstBuffer *in_buf = frame->input_buffer;
518   GstH264NalUnit nalu;
519   GstH264ParserResult pres;
520   GstMapInfo map;
521   GstFlowReturn decode_ret = GST_FLOW_OK;
522
523   GST_LOG_OBJECT (self,
524       "handle frame, PTS: %" GST_TIME_FORMAT ", DTS: %"
525       GST_TIME_FORMAT, GST_TIME_ARGS (GST_BUFFER_PTS (in_buf)),
526       GST_TIME_ARGS (GST_BUFFER_DTS (in_buf)));
527
528   priv->current_frame = frame;
529
530   gst_buffer_map (in_buf, &map, GST_MAP_READ);
531   if (priv->in_format == GST_H264_DECODER_FORMAT_AVC) {
532     pres = gst_h264_parser_identify_nalu_avc (priv->parser,
533         map.data, 0, map.size, priv->nal_length_size, &nalu);
534
535     while (pres == GST_H264_PARSER_OK && decode_ret == GST_FLOW_OK) {
536       decode_ret = gst_h264_decoder_decode_nal (self, &nalu);
537
538       pres = gst_h264_parser_identify_nalu_avc (priv->parser,
539           map.data, nalu.offset + nalu.size, map.size, priv->nal_length_size,
540           &nalu);
541     }
542   } else {
543     pres = gst_h264_parser_identify_nalu (priv->parser,
544         map.data, 0, map.size, &nalu);
545
546     if (pres == GST_H264_PARSER_NO_NAL_END)
547       pres = GST_H264_PARSER_OK;
548
549     while (pres == GST_H264_PARSER_OK && decode_ret == GST_FLOW_OK) {
550       decode_ret = gst_h264_decoder_decode_nal (self, &nalu);
551
552       pres = gst_h264_parser_identify_nalu (priv->parser,
553           map.data, nalu.offset + nalu.size, map.size, &nalu);
554
555       if (pres == GST_H264_PARSER_NO_NAL_END)
556         pres = GST_H264_PARSER_OK;
557     }
558   }
559
560   gst_buffer_unmap (in_buf, &map);
561
562   if (decode_ret != GST_FLOW_OK) {
563     if (decode_ret == GST_FLOW_ERROR) {
564       GST_VIDEO_DECODER_ERROR (self, 1, STREAM, DECODE,
565           ("Failed to decode data"), (NULL), decode_ret);
566     }
567
568     gst_video_decoder_drop_frame (decoder, frame);
569     gst_clear_h264_picture (&priv->current_picture);
570     priv->current_frame = NULL;
571
572     return decode_ret;
573   }
574
575   gst_h264_decoder_finish_current_picture (self, &decode_ret);
576   gst_video_codec_frame_unref (frame);
577   priv->current_frame = NULL;
578
579   if (decode_ret == GST_FLOW_ERROR) {
580     GST_VIDEO_DECODER_ERROR (self, 1, STREAM, DECODE,
581         ("Failed to decode data"), (NULL), decode_ret);
582   }
583
584   return decode_ret;
585 }
586
587 static GstFlowReturn
588 gst_h264_decoder_parse_sps (GstH264Decoder * self, GstH264NalUnit * nalu)
589 {
590   GstH264DecoderPrivate *priv = self->priv;
591   GstH264SPS sps;
592   GstH264ParserResult pres;
593   GstFlowReturn ret;
594
595   pres = gst_h264_parse_sps (nalu, &sps);
596   if (pres != GST_H264_PARSER_OK) {
597     GST_WARNING_OBJECT (self, "Failed to parse SPS, result %d", pres);
598     return GST_FLOW_ERROR;
599   }
600
601   GST_LOG_OBJECT (self, "SPS parsed");
602
603   ret = gst_h264_decoder_process_sps (self, &sps);
604   if (ret != GST_FLOW_OK) {
605     GST_WARNING_OBJECT (self, "Failed to process SPS");
606   } else if (gst_h264_parser_update_sps (priv->parser,
607           &sps) != GST_H264_PARSER_OK) {
608     GST_WARNING_OBJECT (self, "Failed to update SPS");
609     ret = GST_FLOW_ERROR;
610   }
611
612   gst_h264_sps_clear (&sps);
613
614   return ret;
615 }
616
617 static GstFlowReturn
618 gst_h264_decoder_parse_pps (GstH264Decoder * self, GstH264NalUnit * nalu)
619 {
620   GstH264DecoderPrivate *priv = self->priv;
621   GstH264PPS pps;
622   GstH264ParserResult pres;
623   GstFlowReturn ret = GST_FLOW_OK;
624
625   pres = gst_h264_parse_pps (priv->parser, nalu, &pps);
626   if (pres != GST_H264_PARSER_OK) {
627     GST_WARNING_OBJECT (self, "Failed to parse PPS, result %d", pres);
628     return GST_FLOW_ERROR;
629   }
630
631   GST_LOG_OBJECT (self, "PPS parsed");
632
633   if (pps.num_slice_groups_minus1 > 0) {
634     GST_FIXME_OBJECT (self, "FMO is not supported");
635     ret = GST_FLOW_ERROR;
636   } else if (gst_h264_parser_update_pps (priv->parser, &pps)
637       != GST_H264_PARSER_OK) {
638     GST_WARNING_OBJECT (self, "Failed to update PPS");
639     ret = GST_FLOW_ERROR;
640   }
641
642   gst_h264_pps_clear (&pps);
643
644   return ret;
645 }
646
647 static GstFlowReturn
648 gst_h264_decoder_parse_codec_data (GstH264Decoder * self, const guint8 * data,
649     gsize size)
650 {
651   GstH264DecoderPrivate *priv = self->priv;
652   guint num_sps, num_pps;
653   guint off;
654   gint i;
655   GstH264ParserResult pres;
656   GstH264NalUnit nalu;
657   GstFlowReturn ret = GST_FLOW_OK;
658 #ifndef GST_DISABLE_GST_DEBUG
659   guint profile;
660 #endif
661
662   /* parse the avcC data */
663   if (size < 7) {               /* when numSPS==0 and numPPS==0, length is 7 bytes */
664     return GST_FLOW_ERROR;
665   }
666
667   /* parse the version, this must be 1 */
668   if (data[0] != 1) {
669     return GST_FLOW_ERROR;
670   }
671 #ifndef GST_DISABLE_GST_DEBUG
672   /* AVCProfileIndication */
673   /* profile_compat */
674   /* AVCLevelIndication */
675   profile = (data[1] << 16) | (data[2] << 8) | data[3];
676   GST_DEBUG_OBJECT (self, "profile %06x", profile);
677 #endif
678
679   /* 6 bits reserved | 2 bits lengthSizeMinusOne */
680   /* this is the number of bytes in front of the NAL units to mark their
681    * length */
682   priv->nal_length_size = (data[4] & 0x03) + 1;
683   GST_DEBUG_OBJECT (self, "nal length size %u", priv->nal_length_size);
684
685   num_sps = data[5] & 0x1f;
686   off = 6;
687   for (i = 0; i < num_sps; i++) {
688     pres = gst_h264_parser_identify_nalu_avc (priv->parser,
689         data, off, size, 2, &nalu);
690     if (pres != GST_H264_PARSER_OK) {
691       GST_WARNING_OBJECT (self, "Failed to identify SPS nalu");
692       return GST_FLOW_ERROR;
693     }
694
695     ret = gst_h264_decoder_parse_sps (self, &nalu);
696     if (ret != GST_FLOW_OK) {
697       GST_WARNING_OBJECT (self, "Failed to parse SPS");
698       return ret;
699     }
700     off = nalu.offset + nalu.size;
701   }
702
703   if (off >= size) {
704     GST_WARNING_OBJECT (self, "Too small avcC");
705     return GST_FLOW_ERROR;
706   }
707
708   num_pps = data[off];
709   off++;
710
711   for (i = 0; i < num_pps; i++) {
712     pres = gst_h264_parser_identify_nalu_avc (priv->parser,
713         data, off, size, 2, &nalu);
714     if (pres != GST_H264_PARSER_OK) {
715       GST_WARNING_OBJECT (self, "Failed to identify PPS nalu");
716       return GST_FLOW_ERROR;
717     }
718
719     ret = gst_h264_decoder_parse_pps (self, &nalu);
720     if (ret != GST_FLOW_OK) {
721       GST_WARNING_OBJECT (self, "Failed to parse PPS");
722       return ret;
723     }
724     off = nalu.offset + nalu.size;
725   }
726
727   return GST_FLOW_OK;
728 }
729
730 static gboolean
731 gst_h264_decoder_preprocess_slice (GstH264Decoder * self, GstH264Slice * slice)
732 {
733   GstH264DecoderPrivate *priv = self->priv;
734
735   if (!priv->current_picture) {
736     if (slice->header.first_mb_in_slice != 0) {
737       GST_ERROR_OBJECT (self, "Invalid stream, first_mb_in_slice %d",
738           slice->header.first_mb_in_slice);
739       return FALSE;
740     }
741   }
742
743   return TRUE;
744 }
745
746 static void
747 gst_h264_decoder_update_pic_nums (GstH264Decoder * self,
748     GstH264Picture * current_picture, gint frame_num)
749 {
750   GstH264DecoderPrivate *priv = self->priv;
751   GArray *dpb = gst_h264_dpb_get_pictures_all (priv->dpb);
752   gint i;
753
754   for (i = 0; i < dpb->len; i++) {
755     GstH264Picture *picture = g_array_index (dpb, GstH264Picture *, i);
756
757     if (!GST_H264_PICTURE_IS_REF (picture))
758       continue;
759
760     if (GST_H264_PICTURE_IS_LONG_TERM_REF (picture)) {
761       if (GST_H264_PICTURE_IS_FRAME (current_picture))
762         picture->long_term_pic_num = picture->long_term_frame_idx;
763       else if (current_picture->field == picture->field)
764         picture->long_term_pic_num = 2 * picture->long_term_frame_idx + 1;
765       else
766         picture->long_term_pic_num = 2 * picture->long_term_frame_idx;
767     } else {
768       if (picture->frame_num > frame_num)
769         picture->frame_num_wrap = picture->frame_num - priv->max_frame_num;
770       else
771         picture->frame_num_wrap = picture->frame_num;
772
773       if (GST_H264_PICTURE_IS_FRAME (current_picture))
774         picture->pic_num = picture->frame_num_wrap;
775       else if (picture->field == current_picture->field)
776         picture->pic_num = 2 * picture->frame_num_wrap + 1;
777       else
778         picture->pic_num = 2 * picture->frame_num_wrap;
779     }
780   }
781
782   g_array_unref (dpb);
783 }
784
785 static GstH264Picture *
786 gst_h264_decoder_split_frame (GstH264Decoder * self, GstH264Picture * picture)
787 {
788   GstH264Picture *other_field;
789
790   g_assert (GST_H264_PICTURE_IS_FRAME (picture));
791
792   other_field = gst_h264_decoder_new_field_picture (self, picture);
793   if (!other_field) {
794     GST_WARNING_OBJECT (self,
795         "Couldn't split frame into complementary field pair");
796     return NULL;
797   }
798
799   GST_LOG_OBJECT (self, "Split picture %p, poc %d, frame num %d",
800       picture, picture->pic_order_cnt, picture->frame_num);
801
802   /* FIXME: enhance TFF decision by using picture timing SEI */
803   if (picture->top_field_order_cnt < picture->bottom_field_order_cnt) {
804     picture->field = GST_H264_PICTURE_FIELD_TOP_FIELD;
805     picture->pic_order_cnt = picture->top_field_order_cnt;
806
807     other_field->field = GST_H264_PICTURE_FIELD_BOTTOM_FIELD;
808     other_field->pic_order_cnt = picture->bottom_field_order_cnt;
809   } else {
810     picture->field = GST_H264_PICTURE_FIELD_BOTTOM_FIELD;
811     picture->pic_order_cnt = picture->bottom_field_order_cnt;
812
813     other_field->field = GST_H264_PICTURE_FIELD_TOP_FIELD;
814     other_field->pic_order_cnt = picture->top_field_order_cnt;
815   }
816
817   other_field->top_field_order_cnt = picture->top_field_order_cnt;
818   other_field->bottom_field_order_cnt = picture->bottom_field_order_cnt;
819   other_field->frame_num = picture->frame_num;
820   other_field->ref = picture->ref;
821   other_field->nonexisting = picture->nonexisting;
822   other_field->system_frame_number = picture->system_frame_number;
823   other_field->field_pic_flag = picture->field_pic_flag;
824
825   return other_field;
826 }
827
828 static void
829 output_picture_directly (GstH264Decoder * self, GstH264Picture * picture,
830     GstFlowReturn * ret)
831 {
832   GstH264DecoderPrivate *priv = self->priv;
833   GstH264Picture *out_pic = NULL;
834   GstFlowReturn flow_ret = GST_FLOW_OK;
835
836   g_assert (ret != NULL);
837
838   if (GST_H264_PICTURE_IS_FRAME (picture)) {
839     g_assert (priv->last_field == NULL);
840     out_pic = g_steal_pointer (&picture);
841     goto output;
842   }
843
844   if (priv->last_field == NULL) {
845     if (picture->second_field) {
846       GST_WARNING ("Set the last output %p poc:%d, without first field",
847           picture, picture->pic_order_cnt);
848
849       flow_ret = GST_FLOW_ERROR;
850       goto output;
851     }
852
853     /* Just cache the first field. */
854     priv->last_field = g_steal_pointer (&picture);
855   } else {
856     if (!picture->second_field || !picture->other_field
857         || picture->other_field != priv->last_field) {
858       GST_WARNING ("The last field %p poc:%d is not the pair of the "
859           "current field %p poc:%d",
860           priv->last_field, priv->last_field->pic_order_cnt,
861           picture, picture->pic_order_cnt);
862
863       gst_clear_h264_picture (&priv->last_field);
864       flow_ret = GST_FLOW_ERROR;
865       goto output;
866     }
867
868     GST_TRACE ("Pair the last field %p poc:%d and the current"
869         " field %p poc:%d",
870         priv->last_field, priv->last_field->pic_order_cnt,
871         picture, picture->pic_order_cnt);
872
873     out_pic = priv->last_field;
874     priv->last_field = NULL;
875     /* Link each field. */
876     out_pic->other_field = picture;
877   }
878
879 output:
880   if (out_pic) {
881     gst_h264_dpb_set_last_output (priv->dpb, out_pic);
882     gst_h264_decoder_do_output_picture (self, out_pic, &flow_ret);
883   }
884
885   gst_clear_h264_picture (&picture);
886
887   UPDATE_FLOW_RETURN (ret, flow_ret);
888 }
889
890 static void
891 add_picture_to_dpb (GstH264Decoder * self, GstH264Picture * picture)
892 {
893   GstH264DecoderPrivate *priv = self->priv;
894
895   if (!gst_h264_dpb_get_interlaced (priv->dpb)) {
896     g_assert (priv->last_field == NULL);
897     gst_h264_dpb_add (priv->dpb, picture);
898     return;
899   }
900
901   /* The first field of the last picture may not be able to enter the
902      DPB if it is a non ref, but if the second field enters the DPB, we
903      need to add both of them. */
904   if (priv->last_field && picture->other_field == priv->last_field) {
905     gst_h264_dpb_add (priv->dpb, priv->last_field);
906     priv->last_field = NULL;
907   }
908
909   gst_h264_dpb_add (priv->dpb, picture);
910 }
911
912 static void
913 _bump_dpb (GstH264Decoder * self, GstH264DpbBumpMode bump_level,
914     GstH264Picture * current_picture, GstFlowReturn * ret)
915 {
916   GstH264DecoderPrivate *priv = self->priv;
917
918   g_assert (ret != NULL);
919
920   while (gst_h264_dpb_needs_bump (priv->dpb, current_picture, bump_level)) {
921     GstH264Picture *to_output;
922
923     to_output = gst_h264_dpb_bump (priv->dpb, FALSE);
924
925     if (!to_output) {
926       GST_WARNING_OBJECT (self, "Bumping is needed but no picture to output");
927       break;
928     }
929
930     gst_h264_decoder_do_output_picture (self, to_output, ret);
931   }
932 }
933
934 static GstFlowReturn
935 gst_h264_decoder_handle_frame_num_gap (GstH264Decoder * self, gint frame_num)
936 {
937   GstH264DecoderPrivate *priv = self->priv;
938   const GstH264SPS *sps = priv->active_sps;
939   gint unused_short_term_frame_num;
940
941   if (!sps) {
942     GST_ERROR_OBJECT (self, "No active sps");
943     return GST_FLOW_ERROR;
944   }
945
946   if (priv->prev_ref_frame_num == frame_num) {
947     GST_TRACE_OBJECT (self,
948         "frame_num == PrevRefFrameNum (%d), not a gap", frame_num);
949     return GST_FLOW_OK;
950   }
951
952   if (((priv->prev_ref_frame_num + 1) % priv->max_frame_num) == frame_num) {
953     GST_TRACE_OBJECT (self,
954         "frame_num ==  (PrevRefFrameNum + 1) %% MaxFrameNum (%d), not a gap",
955         frame_num);
956     return GST_FLOW_OK;
957   }
958
959   if (gst_h264_dpb_get_size (priv->dpb) == 0) {
960     GST_TRACE_OBJECT (self, "DPB is empty, not a gap");
961     return GST_FLOW_OK;
962   }
963
964   if (!sps->gaps_in_frame_num_value_allowed_flag) {
965     /* This is likely the case where some frames were dropped.
966      * then we need to keep decoding without error out */
967     GST_WARNING_OBJECT (self, "Invalid frame num %d, maybe frame drop",
968         frame_num);
969
970     return GST_FLOW_OK;
971   }
972
973   GST_DEBUG_OBJECT (self, "Handling frame num gap %d -> %d (MaxFrameNum: %d)",
974       priv->prev_ref_frame_num, frame_num, priv->max_frame_num);
975
976   /* 7.4.3/7-23 */
977   unused_short_term_frame_num =
978       (priv->prev_ref_frame_num + 1) % priv->max_frame_num;
979   while (unused_short_term_frame_num != frame_num) {
980     GstH264Picture *picture = gst_h264_picture_new ();
981     GstFlowReturn ret = GST_FLOW_OK;
982
983     if (!gst_h264_decoder_init_gap_picture (self, picture,
984             unused_short_term_frame_num))
985       return GST_FLOW_ERROR;
986
987     gst_h264_decoder_update_pic_nums (self, picture,
988         unused_short_term_frame_num);
989
990     /* C.2.1 */
991     if (!gst_h264_decoder_sliding_window_picture_marking (self, picture)) {
992       GST_ERROR_OBJECT (self,
993           "Couldn't perform sliding window picture marking");
994       return GST_FLOW_ERROR;
995     }
996
997     gst_h264_dpb_delete_unused (priv->dpb);
998
999     _bump_dpb (self, GST_H264_DPB_BUMP_NORMAL_LATENCY, picture, &ret);
1000     if (ret != GST_FLOW_OK)
1001       return ret;
1002
1003     /* the picture is short term ref, add to DPB. */
1004     if (gst_h264_dpb_get_interlaced (priv->dpb)) {
1005       GstH264Picture *other_field =
1006           gst_h264_decoder_split_frame (self, picture);
1007
1008       add_picture_to_dpb (self, picture);
1009       add_picture_to_dpb (self, other_field);
1010     } else {
1011       add_picture_to_dpb (self, picture);
1012     }
1013
1014     unused_short_term_frame_num++;
1015     unused_short_term_frame_num %= priv->max_frame_num;
1016   }
1017
1018   return GST_FLOW_OK;
1019 }
1020
1021 static gboolean
1022 gst_h264_decoder_init_current_picture (GstH264Decoder * self)
1023 {
1024   GstH264DecoderPrivate *priv = self->priv;
1025
1026   if (!gst_h264_decoder_fill_picture_from_slice (self, &priv->current_slice,
1027           priv->current_picture)) {
1028     return FALSE;
1029   }
1030
1031   if (!gst_h264_decoder_calculate_poc (self, priv->current_picture))
1032     return FALSE;
1033
1034   /* If the slice header indicates we will have to perform reference marking
1035    * process after this picture is decoded, store required data for that
1036    * purpose */
1037   if (priv->current_slice.header.
1038       dec_ref_pic_marking.adaptive_ref_pic_marking_mode_flag) {
1039     priv->current_picture->dec_ref_pic_marking =
1040         priv->current_slice.header.dec_ref_pic_marking;
1041   }
1042
1043   return TRUE;
1044 }
1045
1046 static GstFlowReturn
1047 gst_h264_decoder_start_current_picture (GstH264Decoder * self)
1048 {
1049   GstH264DecoderClass *klass;
1050   GstH264DecoderPrivate *priv = self->priv;
1051   const GstH264SPS *sps;
1052   gint frame_num;
1053   GstFlowReturn ret = GST_FLOW_OK;
1054   GstH264Picture *current_picture;
1055
1056   g_assert (priv->current_picture != NULL);
1057   g_assert (priv->active_sps != NULL);
1058   g_assert (priv->active_pps != NULL);
1059
1060   sps = priv->active_sps;
1061
1062   priv->max_frame_num = sps->max_frame_num;
1063   frame_num = priv->current_slice.header.frame_num;
1064   if (priv->current_slice.nalu.idr_pic_flag)
1065     priv->prev_ref_frame_num = 0;
1066
1067   ret = gst_h264_decoder_handle_frame_num_gap (self, frame_num);
1068   if (ret != GST_FLOW_OK)
1069     return ret;
1070
1071   if (!gst_h264_decoder_init_current_picture (self))
1072     return GST_FLOW_ERROR;
1073
1074   current_picture = priv->current_picture;
1075
1076   /* If the new picture is an IDR, flush DPB */
1077   if (current_picture->idr) {
1078     if (!current_picture->dec_ref_pic_marking.no_output_of_prior_pics_flag) {
1079       ret = gst_h264_decoder_drain_internal (self);
1080       if (ret != GST_FLOW_OK)
1081         return ret;
1082     } else {
1083       /* C.4.4 Removal of pictures from the DPB before possible insertion
1084        * of the current picture
1085        *
1086        * If decoded picture is IDR and no_output_of_prior_pics_flag is equal to 1
1087        * or is inferred to be equal to 1, all frame buffers in the DPB
1088        * are emptied without output of the pictures they contain,
1089        * and DPB fullness is set to 0.
1090        */
1091       gst_h264_decoder_clear_dpb (self, FALSE);
1092     }
1093   }
1094
1095   gst_h264_decoder_update_pic_nums (self, current_picture, frame_num);
1096
1097   if (priv->process_ref_pic_lists)
1098     gst_h264_decoder_prepare_ref_pic_lists (self, current_picture);
1099
1100   klass = GST_H264_DECODER_GET_CLASS (self);
1101   if (klass->start_picture) {
1102     ret = klass->start_picture (self, priv->current_picture,
1103         &priv->current_slice, priv->dpb);
1104
1105     if (ret != GST_FLOW_OK) {
1106       GST_WARNING_OBJECT (self, "subclass does not want to start picture");
1107       return ret;
1108     }
1109   }
1110
1111   return GST_FLOW_OK;
1112 }
1113
1114 static GstH264Picture *
1115 gst_h264_decoder_new_field_picture (GstH264Decoder * self,
1116     GstH264Picture * picture)
1117 {
1118   GstH264DecoderClass *klass = GST_H264_DECODER_GET_CLASS (self);
1119   GstH264Picture *new_picture;
1120
1121   if (!klass->new_field_picture) {
1122     GST_WARNING_OBJECT (self, "Subclass does not support interlaced stream");
1123     return NULL;
1124   }
1125
1126   new_picture = gst_h264_picture_new ();
1127   /* don't confuse subclass by non-existing picture */
1128   if (!picture->nonexisting) {
1129     GstFlowReturn ret;
1130
1131     ret = klass->new_field_picture (self, picture, new_picture);
1132     if (ret != GST_FLOW_OK) {
1133       GST_WARNING_OBJECT (self, "Subclass couldn't handle new field picture");
1134       gst_h264_picture_unref (new_picture);
1135
1136       return NULL;
1137     }
1138   }
1139
1140   new_picture->other_field = picture;
1141   new_picture->second_field = TRUE;
1142
1143   return new_picture;
1144 }
1145
1146 static gboolean
1147 gst_h264_decoder_find_first_field_picture (GstH264Decoder * self,
1148     GstH264Slice * slice, GstH264Picture ** first_field)
1149 {
1150   GstH264DecoderPrivate *priv = self->priv;
1151   const GstH264SliceHdr *slice_hdr = &slice->header;
1152   GstH264Picture *prev_field;
1153   gboolean in_dpb;
1154
1155   *first_field = NULL;
1156   prev_field = NULL;
1157   in_dpb = FALSE;
1158   if (gst_h264_dpb_get_interlaced (priv->dpb)) {
1159     if (priv->last_field) {
1160       prev_field = priv->last_field;
1161       in_dpb = FALSE;
1162     } else if (gst_h264_dpb_get_size (priv->dpb) > 0) {
1163       GstH264Picture *prev_picture;
1164       GArray *pictures;
1165
1166       pictures = gst_h264_dpb_get_pictures_all (priv->dpb);
1167       prev_picture =
1168           g_array_index (pictures, GstH264Picture *, pictures->len - 1);
1169       g_array_unref (pictures); /* prev_picture should be held */
1170
1171       /* Previous picture was a field picture. */
1172       if (!GST_H264_PICTURE_IS_FRAME (prev_picture)
1173           && !prev_picture->other_field) {
1174         prev_field = prev_picture;
1175         in_dpb = TRUE;
1176       }
1177     }
1178   } else {
1179     g_assert (priv->last_field == NULL);
1180   }
1181
1182   /* This is not a field picture */
1183   if (!slice_hdr->field_pic_flag) {
1184     if (!prev_field)
1185       return TRUE;
1186
1187     GST_WARNING_OBJECT (self, "Previous picture %p (poc %d) is not complete",
1188         prev_field, prev_field->pic_order_cnt);
1189     goto error;
1190   }
1191
1192   /* OK, this is the first field. */
1193   if (!prev_field)
1194     return TRUE;
1195
1196   if (prev_field->frame_num != slice_hdr->frame_num) {
1197     GST_WARNING_OBJECT (self, "Previous picture %p (poc %d) is not complete",
1198         prev_field, prev_field->pic_order_cnt);
1199     goto error;
1200   } else {
1201     GstH264PictureField current_field = slice_hdr->bottom_field_flag ?
1202         GST_H264_PICTURE_FIELD_BOTTOM_FIELD : GST_H264_PICTURE_FIELD_TOP_FIELD;
1203
1204     if (current_field == prev_field->field) {
1205       GST_WARNING_OBJECT (self,
1206           "Currnet picture and previous picture have identical field %d",
1207           current_field);
1208       goto error;
1209     }
1210   }
1211
1212   *first_field = gst_h264_picture_ref (prev_field);
1213   return TRUE;
1214
1215 error:
1216   if (!in_dpb) {
1217     gst_clear_h264_picture (&priv->last_field);
1218   } else {
1219     /* FIXME: implement fill gap field picture if it is already in DPB */
1220   }
1221
1222   return FALSE;
1223 }
1224
1225 static GstFlowReturn
1226 gst_h264_decoder_parse_slice (GstH264Decoder * self, GstH264NalUnit * nalu)
1227 {
1228   GstH264DecoderPrivate *priv = self->priv;
1229   GstH264ParserResult pres = GST_H264_PARSER_OK;
1230   GstFlowReturn ret = GST_FLOW_OK;
1231
1232   memset (&priv->current_slice, 0, sizeof (GstH264Slice));
1233
1234   pres = gst_h264_parser_parse_slice_hdr (priv->parser, nalu,
1235       &priv->current_slice.header, TRUE, TRUE);
1236
1237   if (pres != GST_H264_PARSER_OK) {
1238     GST_ERROR_OBJECT (self, "Failed to parse slice header, ret %d", pres);
1239     memset (&priv->current_slice, 0, sizeof (GstH264Slice));
1240
1241     return GST_FLOW_ERROR;
1242   }
1243
1244   priv->current_slice.nalu = *nalu;
1245
1246   if (!gst_h264_decoder_preprocess_slice (self, &priv->current_slice))
1247     return GST_FLOW_ERROR;
1248
1249   priv->active_pps = priv->current_slice.header.pps;
1250   priv->active_sps = priv->active_pps->sequence;
1251
1252   /* Check whether field picture boundary within given codec frame.
1253    * This might happen in case that upstream sent buffer per frame unit,
1254    * not picture unit (i.e., AU unit).
1255    * If AU boundary is detected, then finish first field picture we decoded
1256    * in this chain, we should finish the current picture and
1257    * start new field picture decoding */
1258   if (gst_h264_dpb_get_interlaced (priv->dpb) && priv->current_picture &&
1259       !GST_H264_PICTURE_IS_FRAME (priv->current_picture) &&
1260       !priv->current_picture->second_field) {
1261     GstH264PictureField prev_field = priv->current_picture->field;
1262     GstH264PictureField cur_field = GST_H264_PICTURE_FIELD_FRAME;
1263     if (priv->current_slice.header.field_pic_flag)
1264       cur_field = priv->current_slice.header.bottom_field_flag ?
1265           GST_H264_PICTURE_FIELD_BOTTOM_FIELD :
1266           GST_H264_PICTURE_FIELD_TOP_FIELD;
1267
1268     if (cur_field != prev_field) {
1269       GST_LOG_OBJECT (self,
1270           "Found new field picture, finishing the first field picture");
1271       gst_h264_decoder_finish_current_picture (self, &ret);
1272     }
1273   }
1274
1275   if (!priv->current_picture) {
1276     GstH264DecoderClass *klass = GST_H264_DECODER_GET_CLASS (self);
1277     GstH264Picture *picture = NULL;
1278     GstH264Picture *first_field = NULL;
1279     GstFlowReturn ret = GST_FLOW_OK;
1280
1281     g_assert (priv->current_frame);
1282
1283     if (!gst_h264_decoder_find_first_field_picture (self,
1284             &priv->current_slice, &first_field)) {
1285       GST_ERROR_OBJECT (self, "Couldn't find or determine first picture");
1286       return GST_FLOW_ERROR;
1287     }
1288
1289     if (first_field) {
1290       picture = gst_h264_decoder_new_field_picture (self, first_field);
1291       gst_h264_picture_unref (first_field);
1292
1293       if (!picture) {
1294         GST_ERROR_OBJECT (self, "Couldn't duplicate the first field picture");
1295         return GST_FLOW_ERROR;
1296       }
1297     } else {
1298       picture = gst_h264_picture_new ();
1299
1300       if (klass->new_picture)
1301         ret = klass->new_picture (self, priv->current_frame, picture);
1302
1303       if (ret != GST_FLOW_OK) {
1304         GST_WARNING_OBJECT (self, "subclass does not want accept new picture");
1305         priv->current_picture = NULL;
1306         gst_h264_picture_unref (picture);
1307         return ret;
1308       }
1309     }
1310
1311     /* This allows accessing the frame from the picture. */
1312     picture->system_frame_number = priv->current_frame->system_frame_number;
1313     priv->current_picture = picture;
1314
1315     ret = gst_h264_decoder_start_current_picture (self);
1316     if (ret != GST_FLOW_OK) {
1317       GST_WARNING_OBJECT (self, "start picture failed");
1318       return ret;
1319     }
1320   }
1321
1322   return gst_h264_decoder_decode_slice (self);
1323 }
1324
1325 static GstFlowReturn
1326 gst_h264_decoder_decode_nal (GstH264Decoder * self, GstH264NalUnit * nalu)
1327 {
1328   GstFlowReturn ret = GST_FLOW_OK;
1329
1330   GST_LOG_OBJECT (self, "Parsed nal type: %d, offset %d, size %d",
1331       nalu->type, nalu->offset, nalu->size);
1332
1333   switch (nalu->type) {
1334     case GST_H264_NAL_SPS:
1335       ret = gst_h264_decoder_parse_sps (self, nalu);
1336       break;
1337     case GST_H264_NAL_PPS:
1338       ret = gst_h264_decoder_parse_pps (self, nalu);
1339       break;
1340     case GST_H264_NAL_SLICE:
1341     case GST_H264_NAL_SLICE_DPA:
1342     case GST_H264_NAL_SLICE_DPB:
1343     case GST_H264_NAL_SLICE_DPC:
1344     case GST_H264_NAL_SLICE_IDR:
1345     case GST_H264_NAL_SLICE_EXT:
1346       ret = gst_h264_decoder_parse_slice (self, nalu);
1347       break;
1348     default:
1349       break;
1350   }
1351
1352   return ret;
1353 }
1354
1355 static void
1356 gst_h264_decoder_format_from_caps (GstH264Decoder * self, GstCaps * caps,
1357     GstH264DecoderFormat * format, GstH264DecoderAlign * align)
1358 {
1359   if (format)
1360     *format = GST_H264_DECODER_FORMAT_NONE;
1361
1362   if (align)
1363     *align = GST_H264_DECODER_ALIGN_NONE;
1364
1365   if (!gst_caps_is_fixed (caps)) {
1366     GST_WARNING_OBJECT (self, "Caps wasn't fixed");
1367     return;
1368   }
1369
1370   GST_DEBUG_OBJECT (self, "parsing caps: %" GST_PTR_FORMAT, caps);
1371
1372   if (caps && gst_caps_get_size (caps) > 0) {
1373     GstStructure *s = gst_caps_get_structure (caps, 0);
1374     const gchar *str = NULL;
1375
1376     if (format) {
1377       if ((str = gst_structure_get_string (s, "stream-format"))) {
1378         if (strcmp (str, "avc") == 0 || strcmp (str, "avc3") == 0)
1379           *format = GST_H264_DECODER_FORMAT_AVC;
1380         else if (strcmp (str, "byte-stream") == 0)
1381           *format = GST_H264_DECODER_FORMAT_BYTE;
1382       }
1383     }
1384
1385     if (align) {
1386       if ((str = gst_structure_get_string (s, "alignment"))) {
1387         if (strcmp (str, "au") == 0)
1388           *align = GST_H264_DECODER_ALIGN_AU;
1389         else if (strcmp (str, "nal") == 0)
1390           *align = GST_H264_DECODER_ALIGN_NAL;
1391       }
1392     }
1393   }
1394 }
1395
1396 static gboolean
1397 gst_h264_decoder_set_format (GstVideoDecoder * decoder,
1398     GstVideoCodecState * state)
1399 {
1400   GstH264Decoder *self = GST_H264_DECODER (decoder);
1401   GstH264DecoderPrivate *priv = self->priv;
1402   GstQuery *query;
1403
1404   GST_DEBUG_OBJECT (decoder, "Set format");
1405
1406   if (self->input_state)
1407     gst_video_codec_state_unref (self->input_state);
1408
1409   self->input_state = gst_video_codec_state_ref (state);
1410
1411   if (state->caps) {
1412     GstH264DecoderFormat format;
1413     GstH264DecoderAlign align;
1414
1415     gst_h264_decoder_format_from_caps (self, state->caps, &format, &align);
1416
1417     if (format == GST_H264_DECODER_FORMAT_NONE) {
1418       /* codec_data implies avc */
1419       if (state->codec_data) {
1420         GST_WARNING_OBJECT (self,
1421             "video/x-h264 caps with codec_data but no stream-format=avc");
1422         format = GST_H264_DECODER_FORMAT_AVC;
1423       } else {
1424         /* otherwise assume bytestream input */
1425         GST_WARNING_OBJECT (self,
1426             "video/x-h264 caps without codec_data or stream-format");
1427         format = GST_H264_DECODER_FORMAT_BYTE;
1428       }
1429     }
1430
1431     if (format == GST_H264_DECODER_FORMAT_AVC) {
1432       /* AVC requires codec_data, AVC3 might have one and/or SPS/PPS inline */
1433       if (!state->codec_data) {
1434         /* Try it with size 4 anyway */
1435         priv->nal_length_size = 4;
1436         GST_WARNING_OBJECT (self,
1437             "avc format without codec data, assuming nal length size is 4");
1438       }
1439
1440       /* AVC implies alignment=au */
1441       if (align == GST_H264_DECODER_ALIGN_NONE)
1442         align = GST_H264_DECODER_ALIGN_AU;
1443     }
1444
1445     if (format == GST_H264_DECODER_FORMAT_BYTE && state->codec_data)
1446       GST_WARNING_OBJECT (self, "bytestream with codec data");
1447
1448     priv->in_format = format;
1449     priv->align = align;
1450   }
1451
1452   if (state->codec_data) {
1453     GstMapInfo map;
1454
1455     gst_buffer_map (state->codec_data, &map, GST_MAP_READ);
1456     if (gst_h264_decoder_parse_codec_data (self, map.data, map.size) !=
1457         GST_FLOW_OK) {
1458       /* keep going without error.
1459        * Probably inband SPS/PPS might be valid data */
1460       GST_WARNING_OBJECT (self, "Failed to handle codec data");
1461     }
1462     gst_buffer_unmap (state->codec_data, &map);
1463   }
1464
1465   /* in case live streaming, we will run on low-latency mode */
1466   priv->is_live = FALSE;
1467   query = gst_query_new_latency ();
1468   if (gst_pad_peer_query (GST_VIDEO_DECODER_SINK_PAD (self), query))
1469     gst_query_parse_latency (query, &priv->is_live, NULL, NULL);
1470   gst_query_unref (query);
1471
1472   if (priv->is_live)
1473     GST_DEBUG_OBJECT (self, "Live source, will run on low-latency mode");
1474
1475   return TRUE;
1476 }
1477
1478 static gboolean
1479 gst_h264_decoder_fill_picture_from_slice (GstH264Decoder * self,
1480     const GstH264Slice * slice, GstH264Picture * picture)
1481 {
1482   GstH264DecoderClass *klass = GST_H264_DECODER_GET_CLASS (self);
1483   const GstH264SliceHdr *slice_hdr = &slice->header;
1484   const GstH264PPS *pps;
1485   const GstH264SPS *sps;
1486
1487   pps = slice_hdr->pps;
1488   if (!pps) {
1489     GST_ERROR_OBJECT (self, "No pps in slice header");
1490     return FALSE;
1491   }
1492
1493   sps = pps->sequence;
1494   if (!sps) {
1495     GST_ERROR_OBJECT (self, "No sps in pps");
1496     return FALSE;
1497   }
1498
1499   picture->idr = slice->nalu.idr_pic_flag;
1500   picture->dec_ref_pic_marking = slice_hdr->dec_ref_pic_marking;
1501   picture->field_pic_flag = slice_hdr->field_pic_flag;
1502
1503   if (picture->idr)
1504     picture->idr_pic_id = slice_hdr->idr_pic_id;
1505
1506   if (slice_hdr->field_pic_flag)
1507     picture->field =
1508         slice_hdr->bottom_field_flag ?
1509         GST_H264_PICTURE_FIELD_BOTTOM_FIELD : GST_H264_PICTURE_FIELD_TOP_FIELD;
1510   else
1511     picture->field = GST_H264_PICTURE_FIELD_FRAME;
1512
1513   if (!GST_H264_PICTURE_IS_FRAME (picture) && !klass->new_field_picture) {
1514     GST_FIXME_OBJECT (self, "Subclass doesn't support interlace stream");
1515     return FALSE;
1516   }
1517
1518   picture->nal_ref_idc = slice->nalu.ref_idc;
1519   if (slice->nalu.ref_idc != 0)
1520     gst_h264_picture_set_reference (picture,
1521         GST_H264_PICTURE_REF_SHORT_TERM, FALSE);
1522
1523   picture->frame_num = slice_hdr->frame_num;
1524
1525   /* 7.4.3 */
1526   if (!slice_hdr->field_pic_flag)
1527     picture->pic_num = slice_hdr->frame_num;
1528   else
1529     picture->pic_num = 2 * slice_hdr->frame_num + 1;
1530
1531   picture->pic_order_cnt_type = sps->pic_order_cnt_type;
1532   switch (picture->pic_order_cnt_type) {
1533     case 0:
1534       picture->pic_order_cnt_lsb = slice_hdr->pic_order_cnt_lsb;
1535       picture->delta_pic_order_cnt_bottom =
1536           slice_hdr->delta_pic_order_cnt_bottom;
1537       break;
1538     case 1:
1539       picture->delta_pic_order_cnt0 = slice_hdr->delta_pic_order_cnt[0];
1540       picture->delta_pic_order_cnt1 = slice_hdr->delta_pic_order_cnt[1];
1541       break;
1542     case 2:
1543       break;
1544     default:
1545       g_assert_not_reached ();
1546       return FALSE;
1547   }
1548
1549   return TRUE;
1550 }
1551
1552 static gboolean
1553 gst_h264_decoder_calculate_poc (GstH264Decoder * self, GstH264Picture * picture)
1554 {
1555   GstH264DecoderPrivate *priv = self->priv;
1556   const GstH264SPS *sps = priv->active_sps;
1557
1558   if (!sps) {
1559     GST_ERROR_OBJECT (self, "No active SPS");
1560     return FALSE;
1561   }
1562
1563   switch (picture->pic_order_cnt_type) {
1564     case 0:{
1565       /* See spec 8.2.1.1 */
1566       gint prev_pic_order_cnt_msb, prev_pic_order_cnt_lsb;
1567       gint max_pic_order_cnt_lsb;
1568
1569       if (picture->idr) {
1570         prev_pic_order_cnt_msb = prev_pic_order_cnt_lsb = 0;
1571       } else {
1572         if (priv->prev_ref_has_memmgmnt5) {
1573           if (priv->prev_ref_field != GST_H264_PICTURE_FIELD_BOTTOM_FIELD) {
1574             prev_pic_order_cnt_msb = 0;
1575             prev_pic_order_cnt_lsb = priv->prev_ref_top_field_order_cnt;
1576           } else {
1577             prev_pic_order_cnt_msb = 0;
1578             prev_pic_order_cnt_lsb = 0;
1579           }
1580         } else {
1581           prev_pic_order_cnt_msb = priv->prev_ref_pic_order_cnt_msb;
1582           prev_pic_order_cnt_lsb = priv->prev_ref_pic_order_cnt_lsb;
1583         }
1584       }
1585
1586       max_pic_order_cnt_lsb = 1 << (sps->log2_max_pic_order_cnt_lsb_minus4 + 4);
1587
1588       if ((picture->pic_order_cnt_lsb < prev_pic_order_cnt_lsb) &&
1589           (prev_pic_order_cnt_lsb - picture->pic_order_cnt_lsb >=
1590               max_pic_order_cnt_lsb / 2)) {
1591         picture->pic_order_cnt_msb =
1592             prev_pic_order_cnt_msb + max_pic_order_cnt_lsb;
1593       } else if ((picture->pic_order_cnt_lsb > prev_pic_order_cnt_lsb)
1594           && (picture->pic_order_cnt_lsb - prev_pic_order_cnt_lsb >
1595               max_pic_order_cnt_lsb / 2)) {
1596         picture->pic_order_cnt_msb =
1597             prev_pic_order_cnt_msb - max_pic_order_cnt_lsb;
1598       } else {
1599         picture->pic_order_cnt_msb = prev_pic_order_cnt_msb;
1600       }
1601
1602       if (picture->field != GST_H264_PICTURE_FIELD_BOTTOM_FIELD) {
1603         picture->top_field_order_cnt =
1604             picture->pic_order_cnt_msb + picture->pic_order_cnt_lsb;
1605       }
1606
1607       switch (picture->field) {
1608         case GST_H264_PICTURE_FIELD_FRAME:
1609           picture->top_field_order_cnt = picture->pic_order_cnt_msb +
1610               picture->pic_order_cnt_lsb;
1611           picture->bottom_field_order_cnt = picture->top_field_order_cnt +
1612               picture->delta_pic_order_cnt_bottom;
1613           break;
1614         case GST_H264_PICTURE_FIELD_TOP_FIELD:
1615           picture->top_field_order_cnt = picture->pic_order_cnt_msb +
1616               picture->pic_order_cnt_lsb;
1617           break;
1618         case GST_H264_PICTURE_FIELD_BOTTOM_FIELD:
1619           picture->bottom_field_order_cnt = picture->pic_order_cnt_msb +
1620               picture->pic_order_cnt_lsb;
1621           break;
1622       }
1623       break;
1624     }
1625
1626     case 1:{
1627       gint abs_frame_num = 0;
1628       gint expected_pic_order_cnt = 0;
1629       gint i;
1630
1631       /* See spec 8.2.1.2 */
1632       if (priv->prev_has_memmgmnt5)
1633         priv->prev_frame_num_offset = 0;
1634
1635       if (picture->idr)
1636         picture->frame_num_offset = 0;
1637       else if (priv->prev_frame_num > picture->frame_num)
1638         picture->frame_num_offset =
1639             priv->prev_frame_num_offset + priv->max_frame_num;
1640       else
1641         picture->frame_num_offset = priv->prev_frame_num_offset;
1642
1643       if (sps->num_ref_frames_in_pic_order_cnt_cycle != 0)
1644         abs_frame_num = picture->frame_num_offset + picture->frame_num;
1645       else
1646         abs_frame_num = 0;
1647
1648       if (picture->nal_ref_idc == 0 && abs_frame_num > 0)
1649         --abs_frame_num;
1650
1651       if (abs_frame_num > 0) {
1652         gint pic_order_cnt_cycle_cnt, frame_num_in_pic_order_cnt_cycle;
1653         gint expected_delta_per_pic_order_cnt_cycle = 0;
1654
1655         if (sps->num_ref_frames_in_pic_order_cnt_cycle == 0) {
1656           GST_WARNING_OBJECT (self,
1657               "Invalid num_ref_frames_in_pic_order_cnt_cycle in stream");
1658           return FALSE;
1659         }
1660
1661         pic_order_cnt_cycle_cnt =
1662             (abs_frame_num - 1) / sps->num_ref_frames_in_pic_order_cnt_cycle;
1663         frame_num_in_pic_order_cnt_cycle =
1664             (abs_frame_num - 1) % sps->num_ref_frames_in_pic_order_cnt_cycle;
1665
1666         for (i = 0; i < sps->num_ref_frames_in_pic_order_cnt_cycle; i++) {
1667           expected_delta_per_pic_order_cnt_cycle +=
1668               sps->offset_for_ref_frame[i];
1669         }
1670
1671         expected_pic_order_cnt = pic_order_cnt_cycle_cnt *
1672             expected_delta_per_pic_order_cnt_cycle;
1673         /* frame_num_in_pic_order_cnt_cycle is verified < 255 in parser */
1674         for (i = 0; i <= frame_num_in_pic_order_cnt_cycle; ++i)
1675           expected_pic_order_cnt += sps->offset_for_ref_frame[i];
1676       }
1677
1678       if (!picture->nal_ref_idc)
1679         expected_pic_order_cnt += sps->offset_for_non_ref_pic;
1680
1681       if (GST_H264_PICTURE_IS_FRAME (picture)) {
1682         picture->top_field_order_cnt =
1683             expected_pic_order_cnt + picture->delta_pic_order_cnt0;
1684         picture->bottom_field_order_cnt = picture->top_field_order_cnt +
1685             sps->offset_for_top_to_bottom_field + picture->delta_pic_order_cnt1;
1686       } else if (picture->field != GST_H264_PICTURE_FIELD_BOTTOM_FIELD) {
1687         picture->top_field_order_cnt =
1688             expected_pic_order_cnt + picture->delta_pic_order_cnt0;
1689       } else {
1690         picture->bottom_field_order_cnt = expected_pic_order_cnt +
1691             sps->offset_for_top_to_bottom_field + picture->delta_pic_order_cnt0;
1692       }
1693       break;
1694     }
1695
1696     case 2:{
1697       gint temp_pic_order_cnt;
1698
1699       /* See spec 8.2.1.3 */
1700       if (priv->prev_has_memmgmnt5)
1701         priv->prev_frame_num_offset = 0;
1702
1703       if (picture->idr)
1704         picture->frame_num_offset = 0;
1705       else if (priv->prev_frame_num > picture->frame_num)
1706         picture->frame_num_offset =
1707             priv->prev_frame_num_offset + priv->max_frame_num;
1708       else
1709         picture->frame_num_offset = priv->prev_frame_num_offset;
1710
1711       if (picture->idr) {
1712         temp_pic_order_cnt = 0;
1713       } else if (!picture->nal_ref_idc) {
1714         temp_pic_order_cnt =
1715             2 * (picture->frame_num_offset + picture->frame_num) - 1;
1716       } else {
1717         temp_pic_order_cnt =
1718             2 * (picture->frame_num_offset + picture->frame_num);
1719       }
1720
1721       if (GST_H264_PICTURE_IS_FRAME (picture)) {
1722         picture->top_field_order_cnt = temp_pic_order_cnt;
1723         picture->bottom_field_order_cnt = temp_pic_order_cnt;
1724       } else if (picture->field == GST_H264_PICTURE_FIELD_BOTTOM_FIELD) {
1725         picture->bottom_field_order_cnt = temp_pic_order_cnt;
1726       } else {
1727         picture->top_field_order_cnt = temp_pic_order_cnt;
1728       }
1729       break;
1730     }
1731
1732     default:
1733       GST_WARNING_OBJECT (self,
1734           "Invalid pic_order_cnt_type: %d", sps->pic_order_cnt_type);
1735       return FALSE;
1736   }
1737
1738   switch (picture->field) {
1739     case GST_H264_PICTURE_FIELD_FRAME:
1740       picture->pic_order_cnt =
1741           MIN (picture->top_field_order_cnt, picture->bottom_field_order_cnt);
1742       break;
1743     case GST_H264_PICTURE_FIELD_TOP_FIELD:
1744       picture->pic_order_cnt = picture->top_field_order_cnt;
1745       break;
1746     case GST_H264_PICTURE_FIELD_BOTTOM_FIELD:
1747       picture->pic_order_cnt = picture->bottom_field_order_cnt;
1748       break;
1749     default:
1750       g_assert_not_reached ();
1751       return FALSE;
1752   }
1753
1754   return TRUE;
1755 }
1756
1757 static void
1758 gst_h264_decoder_drain_output_queue (GstH264Decoder * self, guint num,
1759     GstFlowReturn * ret)
1760 {
1761   GstH264DecoderPrivate *priv = self->priv;
1762   GstH264DecoderClass *klass = GST_H264_DECODER_GET_CLASS (self);
1763
1764   g_assert (klass->output_picture);
1765   g_assert (ret != NULL);
1766
1767   while (gst_queue_array_get_length (priv->output_queue) > num) {
1768     GstH264DecoderOutputFrame *output_frame = (GstH264DecoderOutputFrame *)
1769         gst_queue_array_pop_head_struct (priv->output_queue);
1770     GstFlowReturn flow_ret = klass->output_picture (self, output_frame->frame,
1771         output_frame->picture);
1772
1773     UPDATE_FLOW_RETURN (ret, flow_ret);
1774   }
1775 }
1776
1777 static void
1778 gst_h264_decoder_do_output_picture (GstH264Decoder * self,
1779     GstH264Picture * picture, GstFlowReturn * ret)
1780 {
1781   GstH264DecoderPrivate *priv = self->priv;
1782   GstVideoCodecFrame *frame = NULL;
1783   GstH264DecoderOutputFrame output_frame;
1784   GstFlowReturn flow_ret = GST_FLOW_OK;
1785
1786   g_assert (ret != NULL);
1787
1788   GST_LOG_OBJECT (self, "Outputting picture %p (frame_num %d, poc %d)",
1789       picture, picture->frame_num, picture->pic_order_cnt);
1790
1791   if (picture->pic_order_cnt < priv->last_output_poc) {
1792     GST_WARNING_OBJECT (self,
1793         "Outputting out of order %d -> %d, likely a broken stream",
1794         priv->last_output_poc, picture->pic_order_cnt);
1795   }
1796
1797   priv->last_output_poc = picture->pic_order_cnt;
1798
1799   frame = gst_video_decoder_get_frame (GST_VIDEO_DECODER (self),
1800       picture->system_frame_number);
1801
1802   if (!frame) {
1803     GST_ERROR_OBJECT (self,
1804         "No available codec frame with frame number %d",
1805         picture->system_frame_number);
1806     UPDATE_FLOW_RETURN (ret, GST_FLOW_ERROR);
1807
1808     gst_h264_picture_unref (picture);
1809
1810     return;
1811   }
1812
1813   output_frame.frame = frame;
1814   output_frame.picture = picture;
1815   output_frame.self = self;
1816   gst_queue_array_push_tail_struct (priv->output_queue, &output_frame);
1817
1818   gst_h264_decoder_drain_output_queue (self, priv->preferred_output_delay,
1819       &flow_ret);
1820   UPDATE_FLOW_RETURN (ret, flow_ret);
1821 }
1822
1823 static void
1824 gst_h264_decoder_finish_current_picture (GstH264Decoder * self,
1825     GstFlowReturn * ret)
1826 {
1827   GstH264DecoderPrivate *priv = self->priv;
1828   GstH264DecoderClass *klass;
1829   GstFlowReturn flow_ret = GST_FLOW_OK;
1830
1831   if (!priv->current_picture)
1832     return;
1833
1834   klass = GST_H264_DECODER_GET_CLASS (self);
1835
1836   if (klass->end_picture) {
1837     flow_ret = klass->end_picture (self, priv->current_picture);
1838     if (flow_ret != GST_FLOW_OK) {
1839       GST_WARNING_OBJECT (self,
1840           "end picture failed, marking picture %p non-existing "
1841           "(frame_num %d, poc %d)", priv->current_picture,
1842           priv->current_picture->frame_num,
1843           priv->current_picture->pic_order_cnt);
1844       priv->current_picture->nonexisting = TRUE;
1845
1846       /* this fake nonexisting picture will not trigger ouput_picture() */
1847       gst_video_decoder_drop_frame (GST_VIDEO_DECODER (self),
1848           gst_video_codec_frame_ref (priv->current_frame));
1849     }
1850   }
1851
1852   /* We no longer need the per frame reference lists */
1853   gst_h264_decoder_clear_ref_pic_lists (self);
1854
1855   /* finish picture takes ownership of the picture */
1856   gst_h264_decoder_finish_picture (self, priv->current_picture, &flow_ret);
1857   priv->current_picture = NULL;
1858
1859   UPDATE_FLOW_RETURN (ret, flow_ret);
1860 }
1861
1862 static gint
1863 poc_asc_compare (const GstH264Picture ** a, const GstH264Picture ** b)
1864 {
1865   return (*a)->pic_order_cnt - (*b)->pic_order_cnt;
1866 }
1867
1868 static gint
1869 poc_desc_compare (const GstH264Picture ** a, const GstH264Picture ** b)
1870 {
1871   return (*b)->pic_order_cnt - (*a)->pic_order_cnt;
1872 }
1873
1874 static GstFlowReturn
1875 gst_h264_decoder_drain_internal (GstH264Decoder * self)
1876 {
1877   GstH264DecoderPrivate *priv = self->priv;
1878   GstH264Picture *picture;
1879   GstFlowReturn ret = GST_FLOW_OK;
1880
1881   while ((picture = gst_h264_dpb_bump (priv->dpb, TRUE)) != NULL) {
1882     gst_h264_decoder_do_output_picture (self, picture, &ret);
1883   }
1884
1885   gst_h264_decoder_drain_output_queue (self, 0, &ret);
1886
1887   gst_clear_h264_picture (&priv->last_field);
1888   gst_h264_dpb_clear (priv->dpb);
1889   priv->last_output_poc = G_MININT32;
1890
1891   return ret;
1892 }
1893
1894 static gboolean
1895 gst_h264_decoder_handle_memory_management_opt (GstH264Decoder * self,
1896     GstH264Picture * picture)
1897 {
1898   GstH264DecoderPrivate *priv = self->priv;
1899   gint i;
1900
1901   for (i = 0; i < G_N_ELEMENTS (picture->dec_ref_pic_marking.ref_pic_marking);
1902       i++) {
1903     GstH264RefPicMarking *ref_pic_marking =
1904         &picture->dec_ref_pic_marking.ref_pic_marking[i];
1905     guint8 type = ref_pic_marking->memory_management_control_operation;
1906
1907     GST_TRACE_OBJECT (self, "memory management operation %d, type %d", i, type);
1908
1909     /* Normal end of operations' specification */
1910     if (type == 0)
1911       return TRUE;
1912
1913     switch (type) {
1914       case 4:
1915         priv->max_long_term_frame_idx =
1916             ref_pic_marking->max_long_term_frame_idx_plus1 - 1;
1917         break;
1918       case 5:
1919         priv->max_long_term_frame_idx = -1;
1920         break;
1921       default:
1922         break;
1923     }
1924
1925     if (!gst_h264_dpb_perform_memory_management_control_operation (priv->dpb,
1926             ref_pic_marking, picture)) {
1927       GST_WARNING_OBJECT (self, "memory management operation type %d failed",
1928           type);
1929       /* Most likely our implementation fault, but let's just perform
1930        * next MMCO if any */
1931     }
1932   }
1933
1934   return TRUE;
1935 }
1936
1937 static gboolean
1938 gst_h264_decoder_sliding_window_picture_marking (GstH264Decoder * self,
1939     GstH264Picture * picture)
1940 {
1941   GstH264DecoderPrivate *priv = self->priv;
1942   const GstH264SPS *sps = priv->active_sps;
1943   gint num_ref_pics;
1944   gint max_num_ref_frames;
1945
1946   /* Skip this for the second field */
1947   if (picture->second_field)
1948     return TRUE;
1949
1950   if (!sps) {
1951     GST_ERROR_OBJECT (self, "No active sps");
1952     return FALSE;
1953   }
1954
1955   /* 8.2.5.3. Ensure the DPB doesn't overflow by discarding the oldest picture */
1956   num_ref_pics = gst_h264_dpb_num_ref_frames (priv->dpb);
1957   max_num_ref_frames = MAX (1, sps->num_ref_frames);
1958
1959   if (num_ref_pics < max_num_ref_frames)
1960     return TRUE;
1961
1962   /* In theory, num_ref_pics shouldn't be larger than max_num_ref_frames
1963    * but it could happen if our implementation is wrong somehow or so.
1964    * Just try to remove reference pictures as many as possible in order to
1965    * avoid DPB overflow.
1966    */
1967   while (num_ref_pics >= max_num_ref_frames) {
1968     /* Max number of reference pics reached, need to remove one of the short
1969      * term ones. Find smallest frame_num_wrap short reference picture and mark
1970      * it as unused */
1971     GstH264Picture *to_unmark =
1972         gst_h264_dpb_get_lowest_frame_num_short_ref (priv->dpb);
1973
1974     if (num_ref_pics > max_num_ref_frames) {
1975       GST_WARNING_OBJECT (self,
1976           "num_ref_pics %d is larger than allowed maximum %d",
1977           num_ref_pics, max_num_ref_frames);
1978     }
1979
1980     if (!to_unmark) {
1981       GST_WARNING_OBJECT (self, "Could not find a short ref picture to unmark");
1982       return FALSE;
1983     }
1984
1985     GST_TRACE_OBJECT (self,
1986         "Unmark reference flag of picture %p (frame_num %d, poc %d)",
1987         to_unmark, to_unmark->frame_num, to_unmark->pic_order_cnt);
1988
1989     gst_h264_picture_set_reference (to_unmark, GST_H264_PICTURE_REF_NONE, TRUE);
1990     gst_h264_picture_unref (to_unmark);
1991
1992     num_ref_pics--;
1993   }
1994
1995   return TRUE;
1996 }
1997
1998 /* This method ensures that DPB does not overflow, either by removing
1999  * reference pictures as specified in the stream, or using a sliding window
2000  * procedure to remove the oldest one.
2001  * It also performs marking and unmarking pictures as reference.
2002  * See spac 8.2.5.1 */
2003 static gboolean
2004 gst_h264_decoder_reference_picture_marking (GstH264Decoder * self,
2005     GstH264Picture * picture)
2006 {
2007   GstH264DecoderPrivate *priv = self->priv;
2008
2009   /* If the current picture is an IDR, all reference pictures are unmarked */
2010   if (picture->idr) {
2011     gst_h264_dpb_mark_all_non_ref (priv->dpb);
2012
2013     if (picture->dec_ref_pic_marking.long_term_reference_flag) {
2014       gst_h264_picture_set_reference (picture,
2015           GST_H264_PICTURE_REF_LONG_TERM, FALSE);
2016       picture->long_term_frame_idx = 0;
2017       priv->max_long_term_frame_idx = 0;
2018     } else {
2019       gst_h264_picture_set_reference (picture,
2020           GST_H264_PICTURE_REF_SHORT_TERM, FALSE);
2021       priv->max_long_term_frame_idx = -1;
2022     }
2023
2024     return TRUE;
2025   }
2026
2027   /* Not an IDR. If the stream contains instructions on how to discard pictures
2028    * from DPB and how to mark/unmark existing reference pictures, do so.
2029    * Otherwise, fall back to default sliding window process */
2030   if (picture->dec_ref_pic_marking.adaptive_ref_pic_marking_mode_flag) {
2031     if (picture->nonexisting) {
2032       GST_WARNING_OBJECT (self,
2033           "Invalid memory management operation for non-existing picture "
2034           "%p (frame_num %d, poc %d", picture, picture->frame_num,
2035           picture->pic_order_cnt);
2036     }
2037
2038     return gst_h264_decoder_handle_memory_management_opt (self, picture);
2039   }
2040
2041   return gst_h264_decoder_sliding_window_picture_marking (self, picture);
2042 }
2043
2044 static GstH264DpbBumpMode
2045 get_bump_level (GstH264Decoder * self)
2046 {
2047   GstH264DecoderPrivate *priv = self->priv;
2048
2049   /* User set the mode explicitly. */
2050   switch (priv->compliance) {
2051     case GST_H264_DECODER_COMPLIANCE_STRICT:
2052       return GST_H264_DPB_BUMP_NORMAL_LATENCY;
2053     case GST_H264_DECODER_COMPLIANCE_NORMAL:
2054       return GST_H264_DPB_BUMP_LOW_LATENCY;
2055     case GST_H264_DECODER_COMPLIANCE_FLEXIBLE:
2056       return GST_H264_DPB_BUMP_VERY_LOW_LATENCY;
2057     default:
2058       break;
2059   }
2060
2061   /* GST_H264_DECODER_COMPLIANCE_AUTO case. */
2062
2063   if (priv->is_live) {
2064     /* The baseline and constrained-baseline profiles do not have B frames
2065        and do not use the picture reorder, safe to use the higher bump level. */
2066     if (priv->profile_idc == GST_H264_PROFILE_BASELINE)
2067       return GST_H264_DPB_BUMP_VERY_LOW_LATENCY;
2068
2069     return GST_H264_DPB_BUMP_LOW_LATENCY;
2070   }
2071
2072   return GST_H264_DPB_BUMP_NORMAL_LATENCY;
2073 }
2074
2075 static void
2076 gst_h264_decoder_finish_picture (GstH264Decoder * self,
2077     GstH264Picture * picture, GstFlowReturn * ret)
2078 {
2079   GstVideoDecoder *decoder = GST_VIDEO_DECODER (self);
2080   GstH264DecoderPrivate *priv = self->priv;
2081   GstH264DpbBumpMode bump_level = get_bump_level (self);
2082
2083   /* Finish processing the picture.
2084    * Start by storing previous picture data for later use */
2085   if (picture->ref) {
2086     gst_h264_decoder_reference_picture_marking (self, picture);
2087     priv->prev_ref_has_memmgmnt5 = picture->mem_mgmt_5;
2088     priv->prev_ref_top_field_order_cnt = picture->top_field_order_cnt;
2089     priv->prev_ref_pic_order_cnt_msb = picture->pic_order_cnt_msb;
2090     priv->prev_ref_pic_order_cnt_lsb = picture->pic_order_cnt_lsb;
2091     priv->prev_ref_field = picture->field;
2092     priv->prev_ref_frame_num = picture->frame_num;
2093   }
2094
2095   priv->prev_frame_num = picture->frame_num;
2096   priv->prev_has_memmgmnt5 = picture->mem_mgmt_5;
2097   priv->prev_frame_num_offset = picture->frame_num_offset;
2098
2099   /* Remove unused (for reference or later output) pictures from DPB, marking
2100    * them as such */
2101   gst_h264_dpb_delete_unused (priv->dpb);
2102
2103   /* If field pictures belong to different codec frame,
2104    * drop codec frame of the second field because we are consuming
2105    * only the first codec frame via GstH264Decoder::output_picture() method */
2106   if (picture->second_field && picture->other_field &&
2107       picture->system_frame_number !=
2108       picture->other_field->system_frame_number) {
2109     GstVideoCodecFrame *frame = gst_video_decoder_get_frame (decoder,
2110         picture->system_frame_number);
2111
2112     gst_video_decoder_release_frame (decoder, frame);
2113   }
2114
2115   /* C.4.4 */
2116   if (picture->mem_mgmt_5) {
2117     GstFlowReturn drain_ret;
2118
2119     GST_TRACE_OBJECT (self, "Memory management type 5, drain the DPB");
2120
2121     drain_ret = gst_h264_decoder_drain_internal (self);
2122     UPDATE_FLOW_RETURN (ret, drain_ret);
2123   }
2124
2125   _bump_dpb (self, bump_level, picture, ret);
2126
2127   /* Add a ref to avoid the case of directly outputed and destroyed. */
2128   gst_h264_picture_ref (picture);
2129
2130   /* C.4.5.1, C.4.5.2
2131      - If the current decoded picture is the second field of a complementary
2132      reference field pair, add to DPB.
2133      C.4.5.1
2134      For A reference decoded picture, the "bumping" process is invoked
2135      repeatedly until there is an empty frame buffer, then add to DPB:
2136      C.4.5.2
2137      For a non-reference decoded picture, if there is empty frame buffer
2138      after bumping the smaller POC, add to DPB.
2139      Otherwise, output directly. */
2140   if ((picture->second_field && picture->other_field
2141           && picture->other_field->ref)
2142       || picture->ref || gst_h264_dpb_has_empty_frame_buffer (priv->dpb)) {
2143     /* Split frame into top/bottom field pictures for reference picture marking
2144      * process. Even if current picture has field_pic_flag equal to zero,
2145      * if next picture is a field picture, complementary field pair of reference
2146      * frame should have individual pic_num and long_term_pic_num.
2147      */
2148     if (gst_h264_dpb_get_interlaced (priv->dpb) &&
2149         GST_H264_PICTURE_IS_FRAME (picture)) {
2150       GstH264Picture *other_field =
2151           gst_h264_decoder_split_frame (self, picture);
2152
2153       add_picture_to_dpb (self, picture);
2154       if (!other_field) {
2155         GST_WARNING_OBJECT (self,
2156             "Couldn't split frame into complementary field pair");
2157         /* Keep decoding anyway... */
2158       } else {
2159         add_picture_to_dpb (self, other_field);
2160       }
2161     } else {
2162       add_picture_to_dpb (self, picture);
2163     }
2164   } else {
2165     output_picture_directly (self, picture, ret);
2166   }
2167
2168   GST_LOG_OBJECT (self,
2169       "Finishing picture %p (frame_num %d, poc %d), entries in DPB %d",
2170       picture, picture->frame_num, picture->pic_order_cnt,
2171       gst_h264_dpb_get_size (priv->dpb));
2172
2173   gst_h264_picture_unref (picture);
2174
2175   /* For the live mode, we try to bump here to avoid waiting
2176      for another decoding circle. */
2177   if (priv->is_live && priv->compliance != GST_H264_DECODER_COMPLIANCE_STRICT)
2178     _bump_dpb (self, bump_level, NULL, ret);
2179 }
2180
2181 static gboolean
2182 gst_h264_decoder_update_max_num_reorder_frames (GstH264Decoder * self,
2183     GstH264SPS * sps)
2184 {
2185   GstH264DecoderPrivate *priv = self->priv;
2186   gsize max_num_reorder_frames = 0;
2187
2188   if (sps->vui_parameters_present_flag
2189       && sps->vui_parameters.bitstream_restriction_flag) {
2190     max_num_reorder_frames = sps->vui_parameters.num_reorder_frames;
2191     if (max_num_reorder_frames > gst_h264_dpb_get_max_num_frames (priv->dpb)) {
2192       GST_WARNING
2193           ("max_num_reorder_frames present, but larger than MaxDpbFrames (%d > %d)",
2194           (gint) max_num_reorder_frames,
2195           gst_h264_dpb_get_max_num_frames (priv->dpb));
2196
2197       max_num_reorder_frames = 0;
2198       return FALSE;
2199     }
2200
2201     gst_h264_dpb_set_max_num_reorder_frames (priv->dpb, max_num_reorder_frames);
2202
2203     return TRUE;
2204   }
2205
2206   if (priv->compliance == GST_H264_DECODER_COMPLIANCE_STRICT) {
2207     gst_h264_dpb_set_max_num_reorder_frames (priv->dpb,
2208         gst_h264_dpb_get_max_num_frames (priv->dpb));
2209     return TRUE;
2210   }
2211
2212   /* max_num_reorder_frames not present, infer it from profile/constraints. */
2213   if (sps->profile_idc == 66 || sps->profile_idc == 83) {
2214     /* baseline, constrained baseline and scalable-baseline profiles
2215        only contain I/P frames. */
2216     max_num_reorder_frames = 0;
2217   } else if (sps->constraint_set3_flag) {
2218     /* constraint_set3_flag may mean the -intra only profile. */
2219     switch (sps->profile_idc) {
2220       case 44:
2221       case 86:
2222       case 100:
2223       case 110:
2224       case 122:
2225       case 244:
2226         max_num_reorder_frames = 0;
2227         break;
2228       default:
2229         max_num_reorder_frames = gst_h264_dpb_get_max_num_frames (priv->dpb);
2230         break;
2231     }
2232   } else {
2233     max_num_reorder_frames = gst_h264_dpb_get_max_num_frames (priv->dpb);
2234   }
2235
2236   gst_h264_dpb_set_max_num_reorder_frames (priv->dpb, max_num_reorder_frames);
2237
2238   return TRUE;
2239 }
2240
2241 typedef enum
2242 {
2243   GST_H264_LEVEL_L1 = 10,
2244   GST_H264_LEVEL_L1B = 9,
2245   GST_H264_LEVEL_L1_1 = 11,
2246   GST_H264_LEVEL_L1_2 = 12,
2247   GST_H264_LEVEL_L1_3 = 13,
2248   GST_H264_LEVEL_L2_0 = 20,
2249   GST_H264_LEVEL_L2_1 = 21,
2250   GST_H264_LEVEL_L2_2 = 22,
2251   GST_H264_LEVEL_L3 = 30,
2252   GST_H264_LEVEL_L3_1 = 31,
2253   GST_H264_LEVEL_L3_2 = 32,
2254   GST_H264_LEVEL_L4 = 40,
2255   GST_H264_LEVEL_L4_1 = 41,
2256   GST_H264_LEVEL_L4_2 = 42,
2257   GST_H264_LEVEL_L5 = 50,
2258   GST_H264_LEVEL_L5_1 = 51,
2259   GST_H264_LEVEL_L5_2 = 52,
2260   GST_H264_LEVEL_L6 = 60,
2261   GST_H264_LEVEL_L6_1 = 61,
2262   GST_H264_LEVEL_L6_2 = 62,
2263 } GstH264DecoderLevel;
2264
2265 typedef struct
2266 {
2267   GstH264DecoderLevel level;
2268
2269   guint32 max_mbps;
2270   guint32 max_fs;
2271   guint32 max_dpb_mbs;
2272   guint32 max_main_br;
2273 } LevelLimits;
2274
2275 static const LevelLimits level_limits_map[] = {
2276   {GST_H264_LEVEL_L1, 1485, 99, 396, 64},
2277   {GST_H264_LEVEL_L1B, 1485, 99, 396, 128},
2278   {GST_H264_LEVEL_L1_1, 3000, 396, 900, 192},
2279   {GST_H264_LEVEL_L1_2, 6000, 396, 2376, 384},
2280   {GST_H264_LEVEL_L1_3, 11800, 396, 2376, 768},
2281   {GST_H264_LEVEL_L2_0, 11880, 396, 2376, 2000},
2282   {GST_H264_LEVEL_L2_1, 19800, 792, 4752, 4000},
2283   {GST_H264_LEVEL_L2_2, 20250, 1620, 8100, 4000},
2284   {GST_H264_LEVEL_L3, 40500, 1620, 8100, 10000},
2285   {GST_H264_LEVEL_L3_1, 108000, 3600, 18000, 14000},
2286   {GST_H264_LEVEL_L3_2, 216000, 5120, 20480, 20000},
2287   {GST_H264_LEVEL_L4, 245760, 8192, 32768, 20000},
2288   {GST_H264_LEVEL_L4_1, 245760, 8192, 32768, 50000},
2289   {GST_H264_LEVEL_L4_2, 522240, 8704, 34816, 50000},
2290   {GST_H264_LEVEL_L5, 589824, 22080, 110400, 135000},
2291   {GST_H264_LEVEL_L5_1, 983040, 36864, 184320, 240000},
2292   {GST_H264_LEVEL_L5_2, 2073600, 36864, 184320, 240000},
2293   {GST_H264_LEVEL_L6, 4177920, 139264, 696320, 240000},
2294   {GST_H264_LEVEL_L6_1, 8355840, 139264, 696320, 480000},
2295   {GST_H264_LEVEL_L6_2, 16711680, 139264, 696320, 800000}
2296 };
2297
2298 static gint
2299 h264_level_to_max_dpb_mbs (GstH264DecoderLevel level)
2300 {
2301   gint i;
2302   for (i = 0; i < G_N_ELEMENTS (level_limits_map); i++) {
2303     if (level == level_limits_map[i].level)
2304       return level_limits_map[i].max_dpb_mbs;
2305   }
2306
2307   return 0;
2308 }
2309
2310 static void
2311 gst_h264_decoder_set_latency (GstH264Decoder * self, const GstH264SPS * sps,
2312     gint max_dpb_size)
2313 {
2314   GstH264DecoderPrivate *priv = self->priv;
2315   GstCaps *caps;
2316   GstClockTime min, max;
2317   GstStructure *structure;
2318   gint fps_d = 1, fps_n = 0;
2319   GstH264DpbBumpMode bump_level;
2320   guint32 frames_delay;
2321
2322   caps = gst_pad_get_current_caps (GST_VIDEO_DECODER_SRC_PAD (self));
2323   if (!caps)
2324     return;
2325
2326   structure = gst_caps_get_structure (caps, 0);
2327   if (gst_structure_get_fraction (structure, "framerate", &fps_n, &fps_d)) {
2328     if (fps_n == 0) {
2329       /* variable framerate: see if we have a max-framerate */
2330       gst_structure_get_fraction (structure, "max-framerate", &fps_n, &fps_d);
2331     }
2332   }
2333   gst_caps_unref (caps);
2334
2335   /* if no fps or variable, then 25/1 */
2336   if (fps_n == 0) {
2337     fps_n = 25;
2338     fps_d = 1;
2339   }
2340
2341   bump_level = get_bump_level (self);
2342   frames_delay = 0;
2343   switch (bump_level) {
2344     case GST_H264_DPB_BUMP_NORMAL_LATENCY:
2345       /* We always wait the DPB full before bumping. */
2346       frames_delay = max_dpb_size;
2347       break;
2348     case GST_H264_DPB_BUMP_LOW_LATENCY:
2349       /* We bump the IDR if the second frame is not a minus POC. */
2350       frames_delay = 1;
2351       break;
2352     case GST_H264_DPB_BUMP_VERY_LOW_LATENCY:
2353       /* We bump the IDR immediately. */
2354       frames_delay = 0;
2355       break;
2356     default:
2357       g_assert_not_reached ();
2358       break;
2359   }
2360
2361   /* Consider output delay wanted by subclass */
2362   frames_delay += priv->preferred_output_delay;
2363
2364   min = gst_util_uint64_scale_int (frames_delay * GST_SECOND, fps_d, fps_n);
2365   max = gst_util_uint64_scale_int ((max_dpb_size + priv->preferred_output_delay)
2366       * GST_SECOND, fps_d, fps_n);
2367
2368   GST_LOG_OBJECT (self,
2369       "latency min %" G_GUINT64_FORMAT " max %" G_GUINT64_FORMAT, min, max);
2370
2371   gst_video_decoder_set_latency (GST_VIDEO_DECODER (self), min, max);
2372 }
2373
2374 static GstFlowReturn
2375 gst_h264_decoder_process_sps (GstH264Decoder * self, GstH264SPS * sps)
2376 {
2377   GstH264DecoderClass *klass = GST_H264_DECODER_GET_CLASS (self);
2378   GstH264DecoderPrivate *priv = self->priv;
2379   guint8 level;
2380   gint max_dpb_mbs;
2381   gint width_mb, height_mb;
2382   gint max_dpb_frames;
2383   gint max_dpb_size;
2384   gint prev_max_dpb_size;
2385   gboolean prev_interlaced;
2386   gboolean interlaced;
2387   GstFlowReturn ret = GST_FLOW_OK;
2388
2389   if (sps->frame_mbs_only_flag == 0) {
2390     if (!klass->new_field_picture) {
2391       GST_FIXME_OBJECT (self,
2392           "frame_mbs_only_flag != 1 not supported by subclass");
2393       return GST_FLOW_NOT_NEGOTIATED;
2394     }
2395
2396     if (sps->mb_adaptive_frame_field_flag) {
2397       GST_LOG_OBJECT (self,
2398           "mb_adaptive_frame_field_flag == 1, MBAFF sequence");
2399     } else {
2400       GST_LOG_OBJECT (self, "mb_adaptive_frame_field_flag == 0, PAFF sequence");
2401     }
2402   }
2403
2404   interlaced = !sps->frame_mbs_only_flag;
2405
2406   /* Spec A.3.1 and A.3.2
2407    * For Baseline, Constrained Baseline and Main profile, the indicated level is
2408    * Level 1b if level_idc is equal to 11 and constraint_set3_flag is equal to 1
2409    */
2410   level = sps->level_idc;
2411   if (level == 11 && (sps->profile_idc == 66 || sps->profile_idc == 77) &&
2412       sps->constraint_set3_flag) {
2413     /* Level 1b */
2414     level = 9;
2415   }
2416
2417   max_dpb_mbs = h264_level_to_max_dpb_mbs ((GstH264DecoderLevel) level);
2418   if (!max_dpb_mbs)
2419     return GST_FLOW_ERROR;
2420
2421   width_mb = sps->width / 16;
2422   height_mb = sps->height / 16;
2423
2424   max_dpb_frames = MIN (max_dpb_mbs / (width_mb * height_mb),
2425       GST_H264_DPB_MAX_SIZE);
2426
2427   if (sps->vui_parameters_present_flag
2428       && sps->vui_parameters.bitstream_restriction_flag)
2429     max_dpb_frames = MAX (1, sps->vui_parameters.max_dec_frame_buffering);
2430
2431   /* Case 1) There might be some non-conforming streams that require more DPB
2432    * size than that of specified one by SPS
2433    * Case 2) If bitstream_restriction_flag is not present,
2434    * max_dec_frame_buffering should be inferred
2435    * to be equal to MaxDpbFrames, then MaxDpbFrames can exceed num_ref_frames
2436    * See https://chromium-review.googlesource.com/c/chromium/src/+/760276/
2437    */
2438   max_dpb_size = MAX (max_dpb_frames, sps->num_ref_frames);
2439   if (max_dpb_size > GST_H264_DPB_MAX_SIZE) {
2440     GST_WARNING_OBJECT (self, "Too large calculated DPB size %d", max_dpb_size);
2441     max_dpb_size = GST_H264_DPB_MAX_SIZE;
2442   }
2443
2444   /* Safety, so that subclass don't need bound checking */
2445   g_return_val_if_fail (max_dpb_size <= GST_H264_DPB_MAX_SIZE, GST_FLOW_ERROR);
2446
2447   prev_max_dpb_size = gst_h264_dpb_get_max_num_frames (priv->dpb);
2448   prev_interlaced = gst_h264_dpb_get_interlaced (priv->dpb);
2449   if (priv->width != sps->width || priv->height != sps->height ||
2450       prev_max_dpb_size != max_dpb_size || prev_interlaced != interlaced) {
2451     GstH264DecoderClass *klass = GST_H264_DECODER_GET_CLASS (self);
2452
2453     GST_DEBUG_OBJECT (self,
2454         "SPS updated, resolution: %dx%d -> %dx%d, dpb size: %d -> %d, "
2455         "interlaced %d -> %d",
2456         priv->width, priv->height, sps->width, sps->height,
2457         prev_max_dpb_size, max_dpb_size, prev_interlaced, interlaced);
2458
2459     ret = gst_h264_decoder_drain (GST_VIDEO_DECODER (self));
2460     if (ret != GST_FLOW_OK)
2461       return ret;
2462
2463     g_assert (klass->new_sequence);
2464
2465     if (klass->get_preferred_output_delay) {
2466       priv->preferred_output_delay =
2467           klass->get_preferred_output_delay (self, priv->is_live);
2468     } else {
2469       priv->preferred_output_delay = 0;
2470     }
2471
2472     ret = klass->new_sequence (self,
2473         sps, max_dpb_size + priv->preferred_output_delay);
2474     if (ret != GST_FLOW_OK) {
2475       GST_WARNING_OBJECT (self, "subclass does not want accept new sequence");
2476       return ret;
2477     }
2478
2479     priv->profile_idc = sps->profile_idc;
2480     priv->width = sps->width;
2481     priv->height = sps->height;
2482
2483     gst_h264_decoder_set_latency (self, sps, max_dpb_size);
2484     gst_h264_dpb_set_max_num_frames (priv->dpb, max_dpb_size);
2485     gst_h264_dpb_set_interlaced (priv->dpb, interlaced);
2486   }
2487
2488   if (!gst_h264_decoder_update_max_num_reorder_frames (self, sps))
2489     return GST_FLOW_ERROR;
2490
2491   return GST_FLOW_OK;
2492 }
2493
2494 static gboolean
2495 gst_h264_decoder_init_gap_picture (GstH264Decoder * self,
2496     GstH264Picture * picture, gint frame_num)
2497 {
2498   picture->nonexisting = TRUE;
2499   picture->nal_ref_idc = 1;
2500   picture->frame_num = picture->pic_num = frame_num;
2501   picture->dec_ref_pic_marking.adaptive_ref_pic_marking_mode_flag = FALSE;
2502   picture->ref = GST_H264_PICTURE_REF_SHORT_TERM;
2503   picture->ref_pic = TRUE;
2504   picture->dec_ref_pic_marking.long_term_reference_flag = FALSE;
2505   picture->field = GST_H264_PICTURE_FIELD_FRAME;
2506
2507   return gst_h264_decoder_calculate_poc (self, picture);
2508 }
2509
2510 static GstFlowReturn
2511 gst_h264_decoder_decode_slice (GstH264Decoder * self)
2512 {
2513   GstH264DecoderClass *klass = GST_H264_DECODER_GET_CLASS (self);
2514   GstH264DecoderPrivate *priv = self->priv;
2515   GstH264Slice *slice = &priv->current_slice;
2516   GstH264Picture *picture = priv->current_picture;
2517   GArray *ref_pic_list0 = NULL;
2518   GArray *ref_pic_list1 = NULL;
2519   GstFlowReturn ret = GST_FLOW_OK;
2520
2521   if (!picture) {
2522     GST_ERROR_OBJECT (self, "No current picture");
2523     return GST_FLOW_ERROR;
2524   }
2525
2526   GST_LOG_OBJECT (self, "Decode picture %p (frame_num %d, poc %d)",
2527       picture, picture->frame_num, picture->pic_order_cnt);
2528
2529   priv->max_pic_num = slice->header.max_pic_num;
2530
2531   if (priv->process_ref_pic_lists) {
2532     if (!gst_h264_decoder_modify_ref_pic_lists (self)) {
2533       ret = GST_FLOW_ERROR;
2534       goto beach;
2535     }
2536
2537     ref_pic_list0 = priv->ref_pic_list0;
2538     ref_pic_list1 = priv->ref_pic_list1;
2539   }
2540
2541   g_assert (klass->decode_slice);
2542
2543   ret = klass->decode_slice (self, picture, slice, ref_pic_list0,
2544       ref_pic_list1);
2545   if (ret != GST_FLOW_OK) {
2546     GST_WARNING_OBJECT (self,
2547         "Subclass didn't want to decode picture %p (frame_num %d, poc %d)",
2548         picture, picture->frame_num, picture->pic_order_cnt);
2549   }
2550
2551 beach:
2552   g_array_set_size (priv->ref_pic_list0, 0);
2553   g_array_set_size (priv->ref_pic_list1, 0);
2554
2555   return ret;
2556 }
2557
2558 static gint
2559 pic_num_desc_compare (const GstH264Picture ** a, const GstH264Picture ** b)
2560 {
2561   return (*b)->pic_num - (*a)->pic_num;
2562 }
2563
2564 static gint
2565 long_term_pic_num_asc_compare (const GstH264Picture ** a,
2566     const GstH264Picture ** b)
2567 {
2568   return (*a)->long_term_pic_num - (*b)->long_term_pic_num;
2569 }
2570
2571 static void
2572 construct_ref_pic_lists_p (GstH264Decoder * self,
2573     GstH264Picture * current_picture)
2574 {
2575   GstH264DecoderPrivate *priv = self->priv;
2576   gint pos;
2577
2578   /* RefPicList0 (8.2.4.2.1) [[1] [2]], where:
2579    * [1] shortterm ref pics sorted by descending pic_num,
2580    * [2] longterm ref pics by ascending long_term_pic_num.
2581    */
2582   g_array_set_size (priv->ref_pic_list_p0, 0);
2583
2584   gst_h264_dpb_get_pictures_short_term_ref (priv->dpb,
2585       TRUE, FALSE, priv->ref_pic_list_p0);
2586   g_array_sort (priv->ref_pic_list_p0, (GCompareFunc) pic_num_desc_compare);
2587
2588   pos = priv->ref_pic_list_p0->len;
2589   gst_h264_dpb_get_pictures_long_term_ref (priv->dpb,
2590       FALSE, priv->ref_pic_list_p0);
2591   g_qsort_with_data (&g_array_index (priv->ref_pic_list_p0, gpointer, pos),
2592       priv->ref_pic_list_p0->len - pos, sizeof (gpointer),
2593       (GCompareDataFunc) long_term_pic_num_asc_compare, NULL);
2594
2595 #ifndef GST_DISABLE_GST_DEBUG
2596   if (gst_debug_category_get_threshold (GST_CAT_DEFAULT) >= GST_LEVEL_DEBUG) {
2597     GString *str = g_string_new (NULL);
2598     for (pos = 0; pos < priv->ref_pic_list_p0->len; pos++) {
2599       GstH264Picture *ref =
2600           g_array_index (priv->ref_pic_list_p0, GstH264Picture *, pos);
2601       if (!GST_H264_PICTURE_IS_LONG_TERM_REF (ref))
2602         g_string_append_printf (str, "|%i", ref->pic_num);
2603       else
2604         g_string_append_printf (str, "|%is", ref->pic_num);
2605     }
2606     GST_DEBUG_OBJECT (self, "ref_pic_list_p0: %s|", str->str);
2607     g_string_free (str, TRUE);
2608   }
2609 #endif
2610 }
2611
2612 static gint
2613 frame_num_wrap_desc_compare (const GstH264Picture ** a,
2614     const GstH264Picture ** b)
2615 {
2616   return (*b)->frame_num_wrap - (*a)->frame_num_wrap;
2617 }
2618
2619 static gint
2620 long_term_frame_idx_asc_compare (const GstH264Picture ** a,
2621     const GstH264Picture ** b)
2622 {
2623   return (*a)->long_term_frame_idx - (*b)->long_term_frame_idx;
2624 }
2625
2626 /* init_picture_refs_fields_1 in gstvaapidecoder_h264.c */
2627 static void
2628 init_picture_refs_fields_1 (GstH264Decoder * self, GstH264PictureField field,
2629     GArray * ref_frame_list, GArray * ref_pic_list_x)
2630 {
2631   guint i = 0, j = 0;
2632
2633   do {
2634     for (; i < ref_frame_list->len; i++) {
2635       GstH264Picture *pic = g_array_index (ref_frame_list, GstH264Picture *, i);
2636       if (pic->field == field) {
2637         pic = gst_h264_picture_ref (pic);
2638         g_array_append_val (ref_pic_list_x, pic);
2639         i++;
2640         break;
2641       }
2642     }
2643
2644     for (; j < ref_frame_list->len; j++) {
2645       GstH264Picture *pic = g_array_index (ref_frame_list, GstH264Picture *, j);
2646       if (pic->field != field) {
2647         pic = gst_h264_picture_ref (pic);
2648         g_array_append_val (ref_pic_list_x, pic);
2649         j++;
2650         break;
2651       }
2652     }
2653   } while (i < ref_frame_list->len || j < ref_frame_list->len);
2654 }
2655
2656 static void
2657 construct_ref_field_pic_lists_p (GstH264Decoder * self,
2658     GstH264Picture * current_picture)
2659 {
2660   GstH264DecoderPrivate *priv = self->priv;
2661   gint pos;
2662
2663   g_array_set_size (priv->ref_pic_list_p0, 0);
2664   g_array_set_size (priv->ref_frame_list_0_short_term, 0);
2665   g_array_set_size (priv->ref_frame_list_long_term, 0);
2666
2667   /* 8.2.4.2.2, 8.2.4.2.5 refFrameList0ShortTerm:
2668    * short-term ref pictures sorted by descending frame_num_wrap.
2669    */
2670   gst_h264_dpb_get_pictures_short_term_ref (priv->dpb,
2671       TRUE, TRUE, priv->ref_frame_list_0_short_term);
2672   g_array_sort (priv->ref_frame_list_0_short_term,
2673       (GCompareFunc) frame_num_wrap_desc_compare);
2674
2675 #ifndef GST_DISABLE_GST_DEBUG
2676   if (gst_debug_category_get_threshold (GST_CAT_DEFAULT) >= GST_LEVEL_TRACE
2677       && priv->ref_frame_list_0_short_term->len) {
2678     GString *str = g_string_new (NULL);
2679     for (pos = 0; pos < priv->ref_frame_list_0_short_term->len; pos++) {
2680       GstH264Picture *ref = g_array_index (priv->ref_frame_list_0_short_term,
2681           GstH264Picture *, pos);
2682       g_string_append_printf (str, "|%i(%d)", ref->frame_num_wrap, ref->field);
2683     }
2684     GST_TRACE_OBJECT (self, "ref_frame_list_0_short_term (%d): %s|",
2685         current_picture->field, str->str);
2686     g_string_free (str, TRUE);
2687   }
2688 #endif
2689
2690   /* 8.2.4.2.2 refFrameList0LongTerm,:
2691    * long-term ref pictures sorted by ascending long_term_frame_idx.
2692    */
2693   gst_h264_dpb_get_pictures_long_term_ref (priv->dpb,
2694       TRUE, priv->ref_frame_list_long_term);
2695   g_array_sort (priv->ref_frame_list_long_term,
2696       (GCompareFunc) long_term_frame_idx_asc_compare);
2697
2698 #ifndef GST_DISABLE_GST_DEBUG
2699   if (gst_debug_category_get_threshold (GST_CAT_DEFAULT) >= GST_LEVEL_TRACE
2700       && priv->ref_frame_list_long_term->len) {
2701     GString *str = g_string_new (NULL);
2702     for (pos = 0; pos < priv->ref_frame_list_long_term->len; pos++) {
2703       GstH264Picture *ref = g_array_index (priv->ref_frame_list_0_short_term,
2704           GstH264Picture *, pos);
2705       g_string_append_printf (str, "|%i(%d)", ref->long_term_frame_idx,
2706           ref->field);
2707     }
2708     GST_TRACE_OBJECT (self, "ref_frame_list_0_long_term (%d): %s|",
2709         current_picture->field, str->str);
2710     g_string_free (str, TRUE);
2711   }
2712 #endif
2713
2714   /* 8.2.4.2.5 */
2715   init_picture_refs_fields_1 (self, current_picture->field,
2716       priv->ref_frame_list_0_short_term, priv->ref_pic_list_p0);
2717   init_picture_refs_fields_1 (self, current_picture->field,
2718       priv->ref_frame_list_long_term, priv->ref_pic_list_p0);
2719
2720 #ifndef GST_DISABLE_GST_DEBUG
2721   if (gst_debug_category_get_threshold (GST_CAT_DEFAULT) >= GST_LEVEL_DEBUG
2722       && priv->ref_pic_list_p0->len) {
2723     GString *str = g_string_new (NULL);
2724     for (pos = 0; pos < priv->ref_pic_list_p0->len; pos++) {
2725       GstH264Picture *ref =
2726           g_array_index (priv->ref_pic_list_p0, GstH264Picture *, pos);
2727       if (!GST_H264_PICTURE_IS_LONG_TERM_REF (ref))
2728         g_string_append_printf (str, "|%i(%d)s", ref->frame_num_wrap,
2729             ref->field);
2730       else
2731         g_string_append_printf (str, "|%i(%d)l", ref->long_term_frame_idx,
2732             ref->field);
2733     }
2734     GST_DEBUG_OBJECT (self, "ref_pic_list_p0 (%d): %s|", current_picture->field,
2735         str->str);
2736     g_string_free (str, TRUE);
2737   }
2738 #endif
2739
2740   /* Clear temporary lists, now pictures are owned by ref_pic_list_p0 */
2741   g_array_set_size (priv->ref_frame_list_0_short_term, 0);
2742   g_array_set_size (priv->ref_frame_list_long_term, 0);
2743 }
2744
2745 static gboolean
2746 lists_are_equal (GArray * l1, GArray * l2)
2747 {
2748   gint i;
2749
2750   if (l1->len != l2->len)
2751     return FALSE;
2752
2753   for (i = 0; i < l1->len; i++)
2754     if (g_array_index (l1, gpointer, i) != g_array_index (l2, gpointer, i))
2755       return FALSE;
2756
2757   return TRUE;
2758 }
2759
2760 static gint
2761 split_ref_pic_list_b (GstH264Decoder * self, GArray * ref_pic_list_b,
2762     GCompareFunc compare_func)
2763 {
2764   gint pos;
2765
2766   for (pos = 0; pos < ref_pic_list_b->len; pos++) {
2767     GstH264Picture *pic = g_array_index (ref_pic_list_b, GstH264Picture *, pos);
2768     if (compare_func (&pic, &self->priv->current_picture) > 0)
2769       break;
2770   }
2771
2772   return pos;
2773 }
2774
2775 static void
2776 print_ref_pic_list_b (GstH264Decoder * self, GArray * ref_list_b,
2777     const gchar * name)
2778 {
2779 #ifndef GST_DISABLE_GST_DEBUG
2780   GString *str;
2781   gint i;
2782
2783   if (gst_debug_category_get_threshold (GST_CAT_DEFAULT) < GST_LEVEL_DEBUG)
2784     return;
2785
2786   str = g_string_new (NULL);
2787
2788   for (i = 0; i < ref_list_b->len; i++) {
2789     GstH264Picture *ref = g_array_index (ref_list_b, GstH264Picture *, i);
2790
2791     if (!GST_H264_PICTURE_IS_LONG_TERM_REF (ref))
2792       g_string_append_printf (str, "|%i", ref->pic_order_cnt);
2793     else
2794       g_string_append_printf (str, "|%il", ref->long_term_pic_num);
2795   }
2796
2797   GST_DEBUG_OBJECT (self, "%s: %s| curr %i", name, str->str,
2798       self->priv->current_picture->pic_order_cnt);
2799   g_string_free (str, TRUE);
2800 #endif
2801 }
2802
2803 static void
2804 construct_ref_pic_lists_b (GstH264Decoder * self,
2805     GstH264Picture * current_picture)
2806 {
2807   GstH264DecoderPrivate *priv = self->priv;
2808   gint pos;
2809
2810   /* RefPicList0 (8.2.4.2.3) [[1] [2] [3]], where:
2811    * [1] shortterm ref pics with POC < current_picture's POC sorted by descending POC,
2812    * [2] shortterm ref pics with POC > current_picture's POC by ascending POC,
2813    * [3] longterm ref pics by ascending long_term_pic_num.
2814    */
2815   g_array_set_size (priv->ref_pic_list_b0, 0);
2816   g_array_set_size (priv->ref_pic_list_b1, 0);
2817
2818   /* 8.2.4.2.3
2819    * When pic_order_cnt_type is equal to 0, reference pictures that are marked
2820    * as "non-existing" as specified in clause 8.2.5.2 are not included in either
2821    * RefPicList0 or RefPicList1
2822    */
2823   gst_h264_dpb_get_pictures_short_term_ref (priv->dpb,
2824       current_picture->pic_order_cnt_type != 0, FALSE, priv->ref_pic_list_b0);
2825
2826   /* First sort ascending, this will put [1] in right place and finish
2827    * [2]. */
2828   print_ref_pic_list_b (self, priv->ref_pic_list_b0, "ref_pic_list_b0");
2829   g_array_sort (priv->ref_pic_list_b0, (GCompareFunc) poc_asc_compare);
2830   print_ref_pic_list_b (self, priv->ref_pic_list_b0, "ref_pic_list_b0");
2831
2832   /* Find first with POC > current_picture's POC to get first element
2833    * in [2]... */
2834   pos = split_ref_pic_list_b (self, priv->ref_pic_list_b0,
2835       (GCompareFunc) poc_asc_compare);
2836
2837   GST_DEBUG_OBJECT (self, "split point %i", pos);
2838
2839   /* and sort [1] descending, thus finishing sequence [1] [2]. */
2840   g_qsort_with_data (priv->ref_pic_list_b0->data, pos, sizeof (gpointer),
2841       (GCompareDataFunc) poc_desc_compare, NULL);
2842
2843   /* Now add [3] and sort by ascending long_term_pic_num. */
2844   pos = priv->ref_pic_list_b0->len;
2845   gst_h264_dpb_get_pictures_long_term_ref (priv->dpb,
2846       FALSE, priv->ref_pic_list_b0);
2847   g_qsort_with_data (&g_array_index (priv->ref_pic_list_b0, gpointer, pos),
2848       priv->ref_pic_list_b0->len - pos, sizeof (gpointer),
2849       (GCompareDataFunc) long_term_pic_num_asc_compare, NULL);
2850
2851   /* RefPicList1 (8.2.4.2.4) [[1] [2] [3]], where:
2852    * [1] shortterm ref pics with POC > curr_pic's POC sorted by ascending POC,
2853    * [2] shortterm ref pics with POC < curr_pic's POC by descending POC,
2854    * [3] longterm ref pics by ascending long_term_pic_num.
2855    */
2856   gst_h264_dpb_get_pictures_short_term_ref (priv->dpb,
2857       current_picture->pic_order_cnt_type != 0, FALSE, priv->ref_pic_list_b1);
2858
2859   /* First sort by descending POC. */
2860   g_array_sort (priv->ref_pic_list_b1, (GCompareFunc) poc_desc_compare);
2861
2862   /* Split at first with POC < current_picture's POC to get first element
2863    * in [2]... */
2864   pos = split_ref_pic_list_b (self, priv->ref_pic_list_b1,
2865       (GCompareFunc) poc_desc_compare);
2866
2867   /* and sort [1] ascending. */
2868   g_qsort_with_data (priv->ref_pic_list_b1->data, pos, sizeof (gpointer),
2869       (GCompareDataFunc) poc_asc_compare, NULL);
2870
2871   /* Now add [3] and sort by ascending long_term_pic_num */
2872   pos = priv->ref_pic_list_b1->len;
2873   gst_h264_dpb_get_pictures_long_term_ref (priv->dpb,
2874       FALSE, priv->ref_pic_list_b1);
2875   g_qsort_with_data (&g_array_index (priv->ref_pic_list_b1, gpointer, pos),
2876       priv->ref_pic_list_b1->len - pos, sizeof (gpointer),
2877       (GCompareDataFunc) long_term_pic_num_asc_compare, NULL);
2878
2879   /* If lists identical, swap first two entries in RefPicList1 (spec
2880    * 8.2.4.2.3) */
2881   if (priv->ref_pic_list_b1->len > 1
2882       && lists_are_equal (priv->ref_pic_list_b0, priv->ref_pic_list_b1)) {
2883     /* swap */
2884     GstH264Picture **list = (GstH264Picture **) priv->ref_pic_list_b1->data;
2885     GstH264Picture *pic = list[0];
2886     list[0] = list[1];
2887     list[1] = pic;
2888   }
2889
2890   print_ref_pic_list_b (self, priv->ref_pic_list_b0, "ref_pic_list_b0");
2891   print_ref_pic_list_b (self, priv->ref_pic_list_b1, "ref_pic_list_b1");
2892 }
2893
2894 static void
2895 construct_ref_field_pic_lists_b (GstH264Decoder * self,
2896     GstH264Picture * current_picture)
2897 {
2898   GstH264DecoderPrivate *priv = self->priv;
2899   gint pos;
2900
2901   /* refFrameList0ShortTerm (8.2.4.2.4) [[1] [2]], where:
2902    * [1] shortterm ref pics with POC < current_picture's POC sorted by descending POC,
2903    * [2] shortterm ref pics with POC > current_picture's POC by ascending POC,
2904    */
2905   g_array_set_size (priv->ref_pic_list_b0, 0);
2906   g_array_set_size (priv->ref_pic_list_b1, 0);
2907   g_array_set_size (priv->ref_frame_list_0_short_term, 0);
2908   g_array_set_size (priv->ref_frame_list_1_short_term, 0);
2909   g_array_set_size (priv->ref_frame_list_long_term, 0);
2910
2911   /* 8.2.4.2.4
2912    * When pic_order_cnt_type is equal to 0, reference pictures that are marked
2913    * as "non-existing" as specified in clause 8.2.5.2 are not included in either
2914    * RefPicList0 or RefPicList1
2915    */
2916   gst_h264_dpb_get_pictures_short_term_ref (priv->dpb,
2917       current_picture->pic_order_cnt_type != 0, TRUE,
2918       priv->ref_frame_list_0_short_term);
2919
2920   /* First sort ascending, this will put [1] in right place and finish
2921    * [2]. */
2922   print_ref_pic_list_b (self, priv->ref_frame_list_0_short_term,
2923       "ref_frame_list_0_short_term");
2924   g_array_sort (priv->ref_frame_list_0_short_term,
2925       (GCompareFunc) poc_asc_compare);
2926   print_ref_pic_list_b (self, priv->ref_frame_list_0_short_term,
2927       "ref_frame_list_0_short_term");
2928
2929   /* Find first with POC > current_picture's POC to get first element
2930    * in [2]... */
2931   pos = split_ref_pic_list_b (self, priv->ref_frame_list_0_short_term,
2932       (GCompareFunc) poc_asc_compare);
2933
2934   GST_DEBUG_OBJECT (self, "split point %i", pos);
2935
2936   /* and sort [1] descending, thus finishing sequence [1] [2]. */
2937   g_qsort_with_data (priv->ref_frame_list_0_short_term->data, pos,
2938       sizeof (gpointer), (GCompareDataFunc) poc_desc_compare, NULL);
2939
2940   /* refFrameList1ShortTerm (8.2.4.2.4) [[1] [2]], where:
2941    * [1] shortterm ref pics with POC > curr_pic's POC sorted by ascending POC,
2942    * [2] shortterm ref pics with POC < curr_pic's POC by descending POC,
2943    */
2944   gst_h264_dpb_get_pictures_short_term_ref (priv->dpb,
2945       current_picture->pic_order_cnt_type != 0, TRUE,
2946       priv->ref_frame_list_1_short_term);
2947
2948   /* First sort by descending POC. */
2949   g_array_sort (priv->ref_frame_list_1_short_term,
2950       (GCompareFunc) poc_desc_compare);
2951
2952   /* Split at first with POC < current_picture's POC to get first element
2953    * in [2]... */
2954   pos = split_ref_pic_list_b (self, priv->ref_frame_list_1_short_term,
2955       (GCompareFunc) poc_desc_compare);
2956
2957   /* and sort [1] ascending. */
2958   g_qsort_with_data (priv->ref_frame_list_1_short_term->data, pos,
2959       sizeof (gpointer), (GCompareDataFunc) poc_asc_compare, NULL);
2960
2961   /* 8.2.4.2.2 refFrameList0LongTerm,:
2962    * long-term ref pictures sorted by ascending long_term_frame_idx.
2963    */
2964   gst_h264_dpb_get_pictures_long_term_ref (priv->dpb,
2965       TRUE, priv->ref_frame_list_long_term);
2966   g_array_sort (priv->ref_frame_list_long_term,
2967       (GCompareFunc) long_term_frame_idx_asc_compare);
2968
2969   /* 8.2.4.2.5 RefPicList0 */
2970   init_picture_refs_fields_1 (self, current_picture->field,
2971       priv->ref_frame_list_0_short_term, priv->ref_pic_list_b0);
2972   init_picture_refs_fields_1 (self, current_picture->field,
2973       priv->ref_frame_list_long_term, priv->ref_pic_list_b0);
2974
2975   /* 8.2.4.2.5 RefPicList1 */
2976   init_picture_refs_fields_1 (self, current_picture->field,
2977       priv->ref_frame_list_1_short_term, priv->ref_pic_list_b1);
2978   init_picture_refs_fields_1 (self, current_picture->field,
2979       priv->ref_frame_list_long_term, priv->ref_pic_list_b1);
2980
2981   /* If lists identical, swap first two entries in RefPicList1 (spec
2982    * 8.2.4.2.5) */
2983   if (priv->ref_pic_list_b1->len > 1
2984       && lists_are_equal (priv->ref_pic_list_b0, priv->ref_pic_list_b1)) {
2985     /* swap */
2986     GstH264Picture **list = (GstH264Picture **) priv->ref_pic_list_b1->data;
2987     GstH264Picture *pic = list[0];
2988     list[0] = list[1];
2989     list[1] = pic;
2990   }
2991
2992   print_ref_pic_list_b (self, priv->ref_pic_list_b0, "ref_pic_list_b0");
2993   print_ref_pic_list_b (self, priv->ref_pic_list_b1, "ref_pic_list_b1");
2994
2995   /* Clear temporary lists, now pictures are owned by ref_pic_list_b0
2996    * and ref_pic_list_b1 */
2997   g_array_set_size (priv->ref_frame_list_0_short_term, 0);
2998   g_array_set_size (priv->ref_frame_list_1_short_term, 0);
2999   g_array_set_size (priv->ref_frame_list_long_term, 0);
3000 }
3001
3002 static void
3003 gst_h264_decoder_prepare_ref_pic_lists (GstH264Decoder * self,
3004     GstH264Picture * current_picture)
3005 {
3006   GstH264DecoderPrivate *priv = self->priv;
3007   gboolean construct_list = FALSE;
3008   gint i;
3009   GArray *dpb_array = gst_h264_dpb_get_pictures_all (priv->dpb);
3010
3011   /* 8.2.4.2.1 ~ 8.2.4.2.4
3012    * When this process is invoked, there shall be at least one reference entry
3013    * that is currently marked as "used for reference"
3014    * (i.e., as "used for short-term reference" or "used for long-term reference")
3015    * and is not marked as "non-existing"
3016    */
3017   for (i = 0; i < dpb_array->len; i++) {
3018     GstH264Picture *picture = g_array_index (dpb_array, GstH264Picture *, i);
3019     if (GST_H264_PICTURE_IS_REF (picture) && !picture->nonexisting) {
3020       construct_list = TRUE;
3021       break;
3022     }
3023   }
3024   g_array_unref (dpb_array);
3025
3026   if (!construct_list) {
3027     gst_h264_decoder_clear_ref_pic_lists (self);
3028     return;
3029   }
3030
3031   if (GST_H264_PICTURE_IS_FRAME (current_picture)) {
3032     construct_ref_pic_lists_p (self, current_picture);
3033     construct_ref_pic_lists_b (self, current_picture);
3034   } else {
3035     construct_ref_field_pic_lists_p (self, current_picture);
3036     construct_ref_field_pic_lists_b (self, current_picture);
3037   }
3038 }
3039
3040 static void
3041 gst_h264_decoder_clear_ref_pic_lists (GstH264Decoder * self)
3042 {
3043   GstH264DecoderPrivate *priv = self->priv;
3044
3045   g_array_set_size (priv->ref_pic_list_p0, 0);
3046   g_array_set_size (priv->ref_pic_list_b0, 0);
3047   g_array_set_size (priv->ref_pic_list_b1, 0);
3048 }
3049
3050 static gint
3051 long_term_pic_num_f (GstH264Decoder * self, const GstH264Picture * picture)
3052 {
3053   if (GST_H264_PICTURE_IS_LONG_TERM_REF (picture))
3054     return picture->long_term_pic_num;
3055   return 2 * (self->priv->max_long_term_frame_idx + 1);
3056 }
3057
3058 static gint
3059 pic_num_f (GstH264Decoder * self, const GstH264Picture * picture)
3060 {
3061   if (!GST_H264_PICTURE_IS_LONG_TERM_REF (picture))
3062     return picture->pic_num;
3063   return self->priv->max_pic_num;
3064 }
3065
3066 /* shift elements on the |array| starting from |from| to |to|,
3067  * inclusive, one position to the right and insert pic at |from| */
3068 static void
3069 shift_right_and_insert (GArray * array, gint from, gint to,
3070     GstH264Picture * picture)
3071 {
3072   g_return_if_fail (from <= to);
3073   g_return_if_fail (array && picture);
3074
3075   g_array_set_size (array, to + 2);
3076   g_array_insert_val (array, from, picture);
3077 }
3078
3079 /* This can process either ref_pic_list0 or ref_pic_list1, depending
3080  * on the list argument. Set up pointers to proper list to be
3081  * processed here. */
3082 static gboolean
3083 modify_ref_pic_list (GstH264Decoder * self, int list)
3084 {
3085   GstH264DecoderPrivate *priv = self->priv;
3086   GstH264Picture *picture = priv->current_picture;
3087   GArray *ref_pic_listx;
3088   const GstH264SliceHdr *slice_hdr = &priv->current_slice.header;
3089   const GstH264RefPicListModification *list_mod;
3090   gboolean ref_pic_list_modification_flag_lX;
3091   gint num_ref_idx_lX_active_minus1;
3092   guint num_ref_pic_list_modifications;
3093   gint i;
3094   gint pic_num_lx_pred = picture->pic_num;
3095   gint ref_idx_lx = 0, src, dst;
3096   gint pic_num_lx_no_wrap;
3097   gint pic_num_lx;
3098   gboolean done = FALSE;
3099   GstH264Picture *pic;
3100
3101   if (list == 0) {
3102     ref_pic_listx = priv->ref_pic_list0;
3103     ref_pic_list_modification_flag_lX =
3104         slice_hdr->ref_pic_list_modification_flag_l0;
3105     num_ref_pic_list_modifications = slice_hdr->n_ref_pic_list_modification_l0;
3106     num_ref_idx_lX_active_minus1 = slice_hdr->num_ref_idx_l0_active_minus1;
3107     list_mod = slice_hdr->ref_pic_list_modification_l0;
3108   } else {
3109     ref_pic_listx = priv->ref_pic_list1;
3110     ref_pic_list_modification_flag_lX =
3111         slice_hdr->ref_pic_list_modification_flag_l1;
3112     num_ref_pic_list_modifications = slice_hdr->n_ref_pic_list_modification_l1;
3113     num_ref_idx_lX_active_minus1 = slice_hdr->num_ref_idx_l1_active_minus1;
3114     list_mod = slice_hdr->ref_pic_list_modification_l1;
3115   }
3116
3117   /* Resize the list to the size requested in the slice header.
3118    *
3119    * Note that per 8.2.4.2 it's possible for
3120    * num_ref_idx_lX_active_minus1 to indicate there should be more ref
3121    * pics on list than we constructed.  Those superfluous ones should
3122    * be treated as non-reference and will be initialized to null,
3123    * which must be handled by clients */
3124   g_assert (num_ref_idx_lX_active_minus1 >= 0);
3125   if (ref_pic_listx->len > num_ref_idx_lX_active_minus1 + 1)
3126     g_array_set_size (ref_pic_listx, num_ref_idx_lX_active_minus1 + 1);
3127
3128   if (!ref_pic_list_modification_flag_lX)
3129     return TRUE;
3130
3131   /* Spec 8.2.4.3:
3132    * Reorder pictures on the list in a way specified in the stream. */
3133   for (i = 0; i < num_ref_pic_list_modifications && !done; i++) {
3134     switch (list_mod->modification_of_pic_nums_idc) {
3135         /* 8.2.4.3.1 - Modify short reference picture position. */
3136       case 0:
3137       case 1:
3138         /* 8-34 */
3139         if (list_mod->modification_of_pic_nums_idc == 0) {
3140           /* Substract given value from predicted PicNum. */
3141           pic_num_lx_no_wrap = pic_num_lx_pred -
3142               (list_mod->value.abs_diff_pic_num_minus1 + 1);
3143           /* Wrap around max_pic_num if it becomes < 0 as result of
3144            * subtraction */
3145           if (pic_num_lx_no_wrap < 0)
3146             pic_num_lx_no_wrap += priv->max_pic_num;
3147         } else {                /* 8-35 */
3148           /* Add given value to predicted PicNum. */
3149           pic_num_lx_no_wrap = pic_num_lx_pred +
3150               (list_mod->value.abs_diff_pic_num_minus1 + 1);
3151           /* Wrap around max_pic_num if it becomes >= max_pic_num as
3152            * result of the addition */
3153           if (pic_num_lx_no_wrap >= priv->max_pic_num)
3154             pic_num_lx_no_wrap -= priv->max_pic_num;
3155         }
3156
3157         /* For use in next iteration */
3158         pic_num_lx_pred = pic_num_lx_no_wrap;
3159
3160         /* 8-36 */
3161         if (pic_num_lx_no_wrap > picture->pic_num)
3162           pic_num_lx = pic_num_lx_no_wrap - priv->max_pic_num;
3163         else
3164           pic_num_lx = pic_num_lx_no_wrap;
3165
3166         /* 8-37 */
3167         g_assert (num_ref_idx_lX_active_minus1 + 1 < 32);
3168         pic = gst_h264_dpb_get_short_ref_by_pic_num (priv->dpb, pic_num_lx);
3169         if (!pic) {
3170           GST_WARNING_OBJECT (self, "Malformed stream, no pic num %d",
3171               pic_num_lx);
3172           break;
3173         }
3174         shift_right_and_insert (ref_pic_listx, ref_idx_lx,
3175             num_ref_idx_lX_active_minus1, pic);
3176         ref_idx_lx++;
3177
3178         for (src = ref_idx_lx, dst = ref_idx_lx;
3179             src <= num_ref_idx_lX_active_minus1 + 1; src++) {
3180           GstH264Picture *src_pic =
3181               g_array_index (ref_pic_listx, GstH264Picture *, src);
3182           gint src_pic_num_lx = src_pic ? pic_num_f (self, src_pic) : -1;
3183           if (src_pic_num_lx != pic_num_lx)
3184             g_array_index (ref_pic_listx, GstH264Picture *, dst++) = src_pic;
3185         }
3186
3187         break;
3188
3189         /* 8.2.4.3.2 - Long-term reference pictures */
3190       case 2:
3191         /* (8-28) */
3192         g_assert (num_ref_idx_lX_active_minus1 + 1 < 32);
3193         pic = gst_h264_dpb_get_long_ref_by_long_term_pic_num (priv->dpb,
3194             list_mod->value.long_term_pic_num);
3195         if (!pic) {
3196           GST_WARNING_OBJECT (self, "Malformed stream, no pic num %d",
3197               list_mod->value.long_term_pic_num);
3198           break;
3199         }
3200         shift_right_and_insert (ref_pic_listx, ref_idx_lx,
3201             num_ref_idx_lX_active_minus1, pic);
3202         ref_idx_lx++;
3203
3204         for (src = ref_idx_lx, dst = ref_idx_lx;
3205             src <= num_ref_idx_lX_active_minus1 + 1; src++) {
3206           GstH264Picture *src_pic =
3207               g_array_index (ref_pic_listx, GstH264Picture *, src);
3208           if (long_term_pic_num_f (self, src_pic) !=
3209               list_mod->value.long_term_pic_num)
3210             g_array_index (ref_pic_listx, GstH264Picture *, dst++) = src_pic;
3211         }
3212
3213         break;
3214
3215         /* End of modification list */
3216       case 3:
3217         done = TRUE;
3218         break;
3219
3220       default:
3221         /* may be recoverable */
3222         GST_WARNING ("Invalid modification_of_pic_nums_idc = %d",
3223             list_mod->modification_of_pic_nums_idc);
3224         break;
3225     }
3226
3227     list_mod++;
3228   }
3229
3230   /* Per NOTE 2 in 8.2.4.3.2, the ref_pic_listx in the above loop is
3231    * temporarily made one element longer than the required final list.
3232    * Resize the list back to its required size. */
3233   if (ref_pic_listx->len > num_ref_idx_lX_active_minus1 + 1)
3234     g_array_set_size (ref_pic_listx, num_ref_idx_lX_active_minus1 + 1);
3235
3236   return TRUE;
3237 }
3238
3239 static void
3240 copy_pic_list_into (GArray * dest, GArray * src)
3241 {
3242   gint i;
3243   g_array_set_size (dest, 0);
3244
3245   for (i = 0; i < src->len; i++)
3246     g_array_append_val (dest, g_array_index (src, gpointer, i));
3247 }
3248
3249 static gboolean
3250 gst_h264_decoder_modify_ref_pic_lists (GstH264Decoder * self)
3251 {
3252   GstH264DecoderPrivate *priv = self->priv;
3253   GstH264SliceHdr *slice_hdr = &priv->current_slice.header;
3254
3255   g_array_set_size (priv->ref_pic_list0, 0);
3256   g_array_set_size (priv->ref_pic_list1, 0);
3257
3258   if (GST_H264_IS_P_SLICE (slice_hdr) || GST_H264_IS_SP_SLICE (slice_hdr)) {
3259     /* 8.2.4 fill reference picture list RefPicList0 for P or SP slice */
3260     copy_pic_list_into (priv->ref_pic_list0, priv->ref_pic_list_p0);
3261     return modify_ref_pic_list (self, 0);
3262   } else if (GST_H264_IS_B_SLICE (slice_hdr)) {
3263     /* 8.2.4 fill reference picture list RefPicList0 and RefPicList1 for B slice */
3264     copy_pic_list_into (priv->ref_pic_list0, priv->ref_pic_list_b0);
3265     copy_pic_list_into (priv->ref_pic_list1, priv->ref_pic_list_b1);
3266     return modify_ref_pic_list (self, 0)
3267         && modify_ref_pic_list (self, 1);
3268   }
3269
3270   return TRUE;
3271 }
3272
3273 /**
3274  * gst_h264_decoder_set_process_ref_pic_lists:
3275  * @decoder: a #GstH264Decoder
3276  * @process: whether subclass is requiring reference picture modification process
3277  *
3278  * Called to en/disable reference picture modification process.
3279  *
3280  * Since: 1.18
3281  */
3282 void
3283 gst_h264_decoder_set_process_ref_pic_lists (GstH264Decoder * decoder,
3284     gboolean process)
3285 {
3286   decoder->priv->process_ref_pic_lists = process;
3287 }
3288
3289 /**
3290  * gst_h264_decoder_get_picture:
3291  * @decoder: a #GstH264Decoder
3292  * @system_frame_number: a target system frame number of #GstH264Picture
3293  *
3294  * Retrive DPB and return a #GstH264Picture corresponding to
3295  * the @system_frame_number
3296  *
3297  * Returns: (transfer full): a #GstH264Picture if successful, or %NULL otherwise
3298  *
3299  * Since: 1.18
3300  */
3301 GstH264Picture *
3302 gst_h264_decoder_get_picture (GstH264Decoder * decoder,
3303     guint32 system_frame_number)
3304 {
3305   return gst_h264_dpb_get_picture (decoder->priv->dpb, system_frame_number);
3306 }