2 * Copyright (C) 2019 Seungha Yang <seungha.yang@navercorp.com>
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.
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.
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.
19 * NOTE: some of implementations are copied/modified from Chromium code
21 * Copyright 2015 The Chromium Authors. All rights reserved.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions are
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
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.
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.
50 * SECTION:gsth264decoder
51 * @title: GstH264Decoder
52 * @short_description: Base class to implement stateless H.264 decoders
61 #include <gst/base/base.h>
62 #include "gsth264decoder.h"
64 GST_DEBUG_CATEGORY (gst_h264_decoder_debug);
65 #define GST_CAT_DEFAULT gst_h264_decoder_debug
69 GST_H264_DECODER_FORMAT_NONE,
70 GST_H264_DECODER_FORMAT_AVC,
71 GST_H264_DECODER_FORMAT_BYTE
72 } GstH264DecoderFormat;
76 GST_H264_DECODER_ALIGN_NONE,
77 GST_H264_DECODER_ALIGN_NAL,
78 GST_H264_DECODER_ALIGN_AU
79 } GstH264DecoderAlign;
81 struct _GstH264DecoderPrivate
83 GstH264DecoderCompliance compliance;
88 guint nal_length_size;
91 GstH264DecoderFormat in_format;
92 GstH264DecoderAlign align;
93 GstH264NalParser *parser;
95 /* Cache last field which can not enter the DPB, should be a non ref */
96 GstH264Picture *last_field;
98 /* used for low-latency vs. high throughput mode decision */
101 /* sps/pps of the current slice */
102 const GstH264SPS *active_sps;
103 const GstH264PPS *active_pps;
105 /* Picture currently being processed/decoded */
106 GstH264Picture *current_picture;
107 GstVideoCodecFrame *current_frame;
109 /* Slice (slice header + nalu) currently being processed/decodec */
110 GstH264Slice current_slice;
114 gint max_long_term_frame_idx;
117 gint prev_ref_frame_num;
118 gint prev_frame_num_offset;
119 gboolean prev_has_memmgmnt5;
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;
127 GstH264PictureField prev_ref_field;
129 /* PicOrderCount of the previously outputted frame */
130 gint last_output_poc;
132 gboolean process_ref_pic_lists;
133 guint preferred_output_delay;
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;
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;
147 /* Reference picture lists, constructed for each slice */
148 GArray *ref_pic_list0;
149 GArray *ref_pic_list1;
151 /* For delayed output */
152 GstQueueArray *output_queue;
158 GstVideoCodecFrame *frame;
159 GstH264Picture *picture;
161 GstH264Decoder *self;
162 } GstH264DecoderOutputFrame;
164 #define UPDATE_FLOW_RETURN(ret,new_ret) G_STMT_START { \
165 if (*(ret) == GST_FLOW_OK) \
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"));
176 static void gst_h264_decoder_finalize (GObject * object);
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);
188 /* codec specific functions */
189 static GstFlowReturn gst_h264_decoder_process_sps (GstH264Decoder * self,
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);
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);
217 gst_h264_decoder_clear_output_frame (GstH264DecoderOutputFrame * output_frame);
226 * gst_h264_decoder_compliance_get_type:
228 * Get the compliance type of the h264 decoder.
233 gst_h264_decoder_compliance_get_type (void)
235 static gsize h264_decoder_compliance_type = 0;
236 static const GEnumValue compliances[] = {
237 {GST_H264_DECODER_COMPLIANCE_AUTO, "GST_H264_DECODER_COMPLIANCE_AUTO",
239 {GST_H264_DECODER_COMPLIANCE_STRICT, "GST_H264_DECODER_COMPLIANCE_STRICT",
241 {GST_H264_DECODER_COMPLIANCE_NORMAL, "GST_H264_DECODER_COMPLIANCE_NORMAL",
243 {GST_H264_DECODER_COMPLIANCE_FLEXIBLE,
244 "GST_H264_DECODER_COMPLIANCE_FLEXIBLE", "flexible"},
249 if (g_once_init_enter (&h264_decoder_compliance_type)) {
252 _type = g_enum_register_static ("GstH264DecoderCompliance", compliances);
253 g_once_init_leave (&h264_decoder_compliance_type, _type);
256 return (GType) h264_decoder_compliance_type;
260 gst_h264_decoder_get_property (GObject * object, guint property_id,
261 GValue * value, GParamSpec * pspec)
263 GstH264Decoder *self = GST_H264_DECODER (object);
264 GstH264DecoderPrivate *priv = self->priv;
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);
273 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
279 gst_h264_decoder_set_property (GObject * object, guint property_id,
280 const GValue * value, GParamSpec * pspec)
282 GstH264Decoder *self = GST_H264_DECODER (object);
283 GstH264DecoderPrivate *priv = self->priv;
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);
292 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
298 gst_h264_decoder_class_init (GstH264DecoderClass * klass)
300 GstVideoDecoderClass *decoder_class = GST_VIDEO_DECODER_CLASS (klass);
301 GObjectClass *object_class = G_OBJECT_CLASS (klass);
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;
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);
317 * GstH264Decoder:compliance:
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,
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));
334 gst_h264_decoder_init (GstH264Decoder * self)
336 GstH264DecoderPrivate *priv;
338 gst_video_decoder_set_packetized (GST_VIDEO_DECODER (self), TRUE);
340 self->priv = priv = gst_h264_decoder_get_instance_private (self);
342 priv->last_output_poc = G_MININT32;
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);
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);
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);
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);
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);
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);
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);
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);
386 gst_h264_decoder_finalize (GObject * object)
388 GstH264Decoder *self = GST_H264_DECODER (object);
389 GstH264DecoderPrivate *priv = self->priv;
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);
401 G_OBJECT_CLASS (parent_class)->finalize (object);
405 gst_h264_decoder_reset (GstH264Decoder * self)
407 GstH264DecoderPrivate *priv = self->priv;
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);
414 priv->profile_idc = 0;
417 priv->nal_length_size = 4;
421 gst_h264_decoder_start (GstVideoDecoder * decoder)
423 GstH264Decoder *self = GST_H264_DECODER (decoder);
424 GstH264DecoderPrivate *priv = self->priv;
426 gst_h264_decoder_reset (self);
428 priv->parser = gst_h264_nal_parser_new ();
429 priv->dpb = gst_h264_dpb_new ();
435 gst_h264_decoder_stop (GstVideoDecoder * decoder)
437 GstH264Decoder *self = GST_H264_DECODER (decoder);
439 gst_h264_decoder_reset (self);
445 gst_h264_decoder_clear_output_frame (GstH264DecoderOutputFrame * output_frame)
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;
456 gst_clear_h264_picture (&output_frame->picture);
460 gst_h264_decoder_clear_dpb (GstH264Decoder * self, gboolean flush)
462 GstVideoDecoder *decoder = GST_VIDEO_DECODER (self);
463 GstH264DecoderPrivate *priv = self->priv;
464 GstH264Picture *picture;
466 /* If we are not flushing now, videodecoder baseclass will hold
467 * GstVideoCodecFrame. Release frames manually */
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);
474 gst_video_decoder_release_frame (decoder, frame);
475 gst_h264_picture_unref (picture);
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;
487 gst_h264_decoder_flush (GstVideoDecoder * decoder)
489 GstH264Decoder *self = GST_H264_DECODER (decoder);
491 gst_h264_decoder_clear_dpb (self, TRUE);
497 gst_h264_decoder_drain (GstVideoDecoder * decoder)
499 GstH264Decoder *self = GST_H264_DECODER (decoder);
501 /* dpb will be cleared by this method */
502 return gst_h264_decoder_drain_internal (self);
506 gst_h264_decoder_finish (GstVideoDecoder * decoder)
508 return gst_h264_decoder_drain (decoder);
512 gst_h264_decoder_handle_frame (GstVideoDecoder * decoder,
513 GstVideoCodecFrame * frame)
515 GstH264Decoder *self = GST_H264_DECODER (decoder);
516 GstH264DecoderPrivate *priv = self->priv;
517 GstBuffer *in_buf = frame->input_buffer;
519 GstH264ParserResult pres;
521 GstFlowReturn decode_ret = GST_FLOW_OK;
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)));
528 priv->current_frame = frame;
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);
535 while (pres == GST_H264_PARSER_OK && decode_ret == GST_FLOW_OK) {
536 decode_ret = gst_h264_decoder_decode_nal (self, &nalu);
538 pres = gst_h264_parser_identify_nalu_avc (priv->parser,
539 map.data, nalu.offset + nalu.size, map.size, priv->nal_length_size,
543 pres = gst_h264_parser_identify_nalu (priv->parser,
544 map.data, 0, map.size, &nalu);
546 if (pres == GST_H264_PARSER_NO_NAL_END)
547 pres = GST_H264_PARSER_OK;
549 while (pres == GST_H264_PARSER_OK && decode_ret == GST_FLOW_OK) {
550 decode_ret = gst_h264_decoder_decode_nal (self, &nalu);
552 pres = gst_h264_parser_identify_nalu (priv->parser,
553 map.data, nalu.offset + nalu.size, map.size, &nalu);
555 if (pres == GST_H264_PARSER_NO_NAL_END)
556 pres = GST_H264_PARSER_OK;
560 gst_buffer_unmap (in_buf, &map);
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);
568 gst_video_decoder_drop_frame (decoder, frame);
569 gst_clear_h264_picture (&priv->current_picture);
570 priv->current_frame = NULL;
575 gst_h264_decoder_finish_current_picture (self, &decode_ret);
576 gst_video_codec_frame_unref (frame);
577 priv->current_frame = NULL;
579 if (decode_ret == GST_FLOW_ERROR) {
580 GST_VIDEO_DECODER_ERROR (self, 1, STREAM, DECODE,
581 ("Failed to decode data"), (NULL), decode_ret);
588 gst_h264_decoder_parse_sps (GstH264Decoder * self, GstH264NalUnit * nalu)
590 GstH264DecoderPrivate *priv = self->priv;
592 GstH264ParserResult pres;
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;
601 GST_LOG_OBJECT (self, "SPS parsed");
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;
612 gst_h264_sps_clear (&sps);
618 gst_h264_decoder_parse_pps (GstH264Decoder * self, GstH264NalUnit * nalu)
620 GstH264DecoderPrivate *priv = self->priv;
622 GstH264ParserResult pres;
623 GstFlowReturn ret = GST_FLOW_OK;
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;
631 GST_LOG_OBJECT (self, "PPS parsed");
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;
642 gst_h264_pps_clear (&pps);
648 gst_h264_decoder_parse_codec_data (GstH264Decoder * self, const guint8 * data,
651 GstH264DecoderPrivate *priv = self->priv;
652 guint num_sps, num_pps;
655 GstH264ParserResult pres;
657 GstFlowReturn ret = GST_FLOW_OK;
658 #ifndef GST_DISABLE_GST_DEBUG
662 /* parse the avcC data */
663 if (size < 7) { /* when numSPS==0 and numPPS==0, length is 7 bytes */
664 return GST_FLOW_ERROR;
667 /* parse the version, this must be 1 */
669 return GST_FLOW_ERROR;
671 #ifndef GST_DISABLE_GST_DEBUG
672 /* AVCProfileIndication */
674 /* AVCLevelIndication */
675 profile = (data[1] << 16) | (data[2] << 8) | data[3];
676 GST_DEBUG_OBJECT (self, "profile %06x", profile);
679 /* 6 bits reserved | 2 bits lengthSizeMinusOne */
680 /* this is the number of bytes in front of the NAL units to mark their
682 priv->nal_length_size = (data[4] & 0x03) + 1;
683 GST_DEBUG_OBJECT (self, "nal length size %u", priv->nal_length_size);
685 num_sps = data[5] & 0x1f;
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;
695 ret = gst_h264_decoder_parse_sps (self, &nalu);
696 if (ret != GST_FLOW_OK) {
697 GST_WARNING_OBJECT (self, "Failed to parse SPS");
700 off = nalu.offset + nalu.size;
704 GST_WARNING_OBJECT (self, "Too small avcC");
705 return GST_FLOW_ERROR;
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;
719 ret = gst_h264_decoder_parse_pps (self, &nalu);
720 if (ret != GST_FLOW_OK) {
721 GST_WARNING_OBJECT (self, "Failed to parse PPS");
724 off = nalu.offset + nalu.size;
731 gst_h264_decoder_preprocess_slice (GstH264Decoder * self, GstH264Slice * slice)
733 GstH264DecoderPrivate *priv = self->priv;
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);
747 gst_h264_decoder_update_pic_nums (GstH264Decoder * self,
748 GstH264Picture * current_picture, gint frame_num)
750 GstH264DecoderPrivate *priv = self->priv;
751 GArray *dpb = gst_h264_dpb_get_pictures_all (priv->dpb);
754 for (i = 0; i < dpb->len; i++) {
755 GstH264Picture *picture = g_array_index (dpb, GstH264Picture *, i);
757 if (!GST_H264_PICTURE_IS_REF (picture))
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;
766 picture->long_term_pic_num = 2 * picture->long_term_frame_idx;
768 if (picture->frame_num > frame_num)
769 picture->frame_num_wrap = picture->frame_num - priv->max_frame_num;
771 picture->frame_num_wrap = picture->frame_num;
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;
778 picture->pic_num = 2 * picture->frame_num_wrap;
785 static GstH264Picture *
786 gst_h264_decoder_split_frame (GstH264Decoder * self, GstH264Picture * picture)
788 GstH264Picture *other_field;
790 g_assert (GST_H264_PICTURE_IS_FRAME (picture));
792 other_field = gst_h264_decoder_new_field_picture (self, picture);
794 GST_WARNING_OBJECT (self,
795 "Couldn't split frame into complementary field pair");
799 GST_LOG_OBJECT (self, "Split picture %p, poc %d, frame num %d",
800 picture, picture->pic_order_cnt, picture->frame_num);
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;
807 other_field->field = GST_H264_PICTURE_FIELD_BOTTOM_FIELD;
808 other_field->pic_order_cnt = picture->bottom_field_order_cnt;
810 picture->field = GST_H264_PICTURE_FIELD_BOTTOM_FIELD;
811 picture->pic_order_cnt = picture->bottom_field_order_cnt;
813 other_field->field = GST_H264_PICTURE_FIELD_TOP_FIELD;
814 other_field->pic_order_cnt = picture->top_field_order_cnt;
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;
829 output_picture_directly (GstH264Decoder * self, GstH264Picture * picture,
832 GstH264DecoderPrivate *priv = self->priv;
833 GstH264Picture *out_pic = NULL;
834 GstFlowReturn flow_ret = GST_FLOW_OK;
836 g_assert (ret != NULL);
838 if (GST_H264_PICTURE_IS_FRAME (picture)) {
839 g_assert (priv->last_field == NULL);
840 out_pic = g_steal_pointer (&picture);
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);
849 flow_ret = GST_FLOW_ERROR;
853 /* Just cache the first field. */
854 priv->last_field = g_steal_pointer (&picture);
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);
863 gst_clear_h264_picture (&priv->last_field);
864 flow_ret = GST_FLOW_ERROR;
868 GST_TRACE ("Pair the last field %p poc:%d and the current"
870 priv->last_field, priv->last_field->pic_order_cnt,
871 picture, picture->pic_order_cnt);
873 out_pic = priv->last_field;
874 priv->last_field = NULL;
875 /* Link each field. */
876 out_pic->other_field = picture;
881 gst_h264_dpb_set_last_output (priv->dpb, out_pic);
882 gst_h264_decoder_do_output_picture (self, out_pic, &flow_ret);
885 gst_clear_h264_picture (&picture);
887 UPDATE_FLOW_RETURN (ret, flow_ret);
891 add_picture_to_dpb (GstH264Decoder * self, GstH264Picture * picture)
893 GstH264DecoderPrivate *priv = self->priv;
895 if (!gst_h264_dpb_get_interlaced (priv->dpb)) {
896 g_assert (priv->last_field == NULL);
897 gst_h264_dpb_add (priv->dpb, picture);
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;
909 gst_h264_dpb_add (priv->dpb, picture);
913 _bump_dpb (GstH264Decoder * self, GstH264DpbBumpMode bump_level,
914 GstH264Picture * current_picture, GstFlowReturn * ret)
916 GstH264DecoderPrivate *priv = self->priv;
918 g_assert (ret != NULL);
920 while (gst_h264_dpb_needs_bump (priv->dpb, current_picture, bump_level)) {
921 GstH264Picture *to_output;
923 to_output = gst_h264_dpb_bump (priv->dpb, FALSE);
926 GST_WARNING_OBJECT (self, "Bumping is needed but no picture to output");
930 gst_h264_decoder_do_output_picture (self, to_output, ret);
935 gst_h264_decoder_handle_frame_num_gap (GstH264Decoder * self, gint frame_num)
937 GstH264DecoderPrivate *priv = self->priv;
938 const GstH264SPS *sps = priv->active_sps;
939 gint unused_short_term_frame_num;
942 GST_ERROR_OBJECT (self, "No active sps");
943 return GST_FLOW_ERROR;
946 if (priv->prev_ref_frame_num == frame_num) {
947 GST_TRACE_OBJECT (self,
948 "frame_num == PrevRefFrameNum (%d), not a gap", frame_num);
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",
959 if (gst_h264_dpb_get_size (priv->dpb) == 0) {
960 GST_TRACE_OBJECT (self, "DPB is empty, not a gap");
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",
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);
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;
983 if (!gst_h264_decoder_init_gap_picture (self, picture,
984 unused_short_term_frame_num))
985 return GST_FLOW_ERROR;
987 gst_h264_decoder_update_pic_nums (self, picture,
988 unused_short_term_frame_num);
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;
997 gst_h264_dpb_delete_unused (priv->dpb);
999 _bump_dpb (self, GST_H264_DPB_BUMP_NORMAL_LATENCY, picture, &ret);
1000 if (ret != GST_FLOW_OK)
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);
1008 add_picture_to_dpb (self, picture);
1009 add_picture_to_dpb (self, other_field);
1011 add_picture_to_dpb (self, picture);
1014 unused_short_term_frame_num++;
1015 unused_short_term_frame_num %= priv->max_frame_num;
1022 gst_h264_decoder_init_current_picture (GstH264Decoder * self)
1024 GstH264DecoderPrivate *priv = self->priv;
1026 if (!gst_h264_decoder_fill_picture_from_slice (self, &priv->current_slice,
1027 priv->current_picture)) {
1031 if (!gst_h264_decoder_calculate_poc (self, priv->current_picture))
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
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;
1046 static GstFlowReturn
1047 gst_h264_decoder_start_current_picture (GstH264Decoder * self)
1049 GstH264DecoderClass *klass;
1050 GstH264DecoderPrivate *priv = self->priv;
1051 const GstH264SPS *sps;
1053 GstFlowReturn ret = GST_FLOW_OK;
1054 GstH264Picture *current_picture;
1056 g_assert (priv->current_picture != NULL);
1057 g_assert (priv->active_sps != NULL);
1058 g_assert (priv->active_pps != NULL);
1060 sps = priv->active_sps;
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;
1067 ret = gst_h264_decoder_handle_frame_num_gap (self, frame_num);
1068 if (ret != GST_FLOW_OK)
1071 if (!gst_h264_decoder_init_current_picture (self))
1072 return GST_FLOW_ERROR;
1074 current_picture = priv->current_picture;
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)
1083 /* C.4.4 Removal of pictures from the DPB before possible insertion
1084 * of the current picture
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.
1091 gst_h264_decoder_clear_dpb (self, FALSE);
1095 gst_h264_decoder_update_pic_nums (self, current_picture, frame_num);
1097 if (priv->process_ref_pic_lists)
1098 gst_h264_decoder_prepare_ref_pic_lists (self, current_picture);
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);
1105 if (ret != GST_FLOW_OK) {
1106 GST_WARNING_OBJECT (self, "subclass does not want to start picture");
1114 static GstH264Picture *
1115 gst_h264_decoder_new_field_picture (GstH264Decoder * self,
1116 GstH264Picture * picture)
1118 GstH264DecoderClass *klass = GST_H264_DECODER_GET_CLASS (self);
1119 GstH264Picture *new_picture;
1121 if (!klass->new_field_picture) {
1122 GST_WARNING_OBJECT (self, "Subclass does not support interlaced stream");
1126 new_picture = gst_h264_picture_new ();
1127 /* don't confuse subclass by non-existing picture */
1128 if (!picture->nonexisting) {
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);
1140 new_picture->other_field = picture;
1141 new_picture->second_field = TRUE;
1147 gst_h264_decoder_find_first_field_picture (GstH264Decoder * self,
1148 GstH264Slice * slice, GstH264Picture ** first_field)
1150 GstH264DecoderPrivate *priv = self->priv;
1151 const GstH264SliceHdr *slice_hdr = &slice->header;
1152 GstH264Picture *prev_field;
1155 *first_field = NULL;
1158 if (gst_h264_dpb_get_interlaced (priv->dpb)) {
1159 if (priv->last_field) {
1160 prev_field = priv->last_field;
1162 } else if (gst_h264_dpb_get_size (priv->dpb) > 0) {
1163 GstH264Picture *prev_picture;
1166 pictures = gst_h264_dpb_get_pictures_all (priv->dpb);
1168 g_array_index (pictures, GstH264Picture *, pictures->len - 1);
1169 g_array_unref (pictures); /* prev_picture should be held */
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;
1179 g_assert (priv->last_field == NULL);
1182 /* This is not a field picture */
1183 if (!slice_hdr->field_pic_flag) {
1187 GST_WARNING_OBJECT (self, "Previous picture %p (poc %d) is not complete",
1188 prev_field, prev_field->pic_order_cnt);
1192 /* OK, this is the first field. */
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);
1201 GstH264PictureField current_field = slice_hdr->bottom_field_flag ?
1202 GST_H264_PICTURE_FIELD_BOTTOM_FIELD : GST_H264_PICTURE_FIELD_TOP_FIELD;
1204 if (current_field == prev_field->field) {
1205 GST_WARNING_OBJECT (self,
1206 "Currnet picture and previous picture have identical field %d",
1212 *first_field = gst_h264_picture_ref (prev_field);
1217 gst_clear_h264_picture (&priv->last_field);
1219 /* FIXME: implement fill gap field picture if it is already in DPB */
1225 static GstFlowReturn
1226 gst_h264_decoder_parse_slice (GstH264Decoder * self, GstH264NalUnit * nalu)
1228 GstH264DecoderPrivate *priv = self->priv;
1229 GstH264ParserResult pres = GST_H264_PARSER_OK;
1230 GstFlowReturn ret = GST_FLOW_OK;
1232 memset (&priv->current_slice, 0, sizeof (GstH264Slice));
1234 pres = gst_h264_parser_parse_slice_hdr (priv->parser, nalu,
1235 &priv->current_slice.header, TRUE, TRUE);
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));
1241 return GST_FLOW_ERROR;
1244 priv->current_slice.nalu = *nalu;
1246 if (!gst_h264_decoder_preprocess_slice (self, &priv->current_slice))
1247 return GST_FLOW_ERROR;
1249 priv->active_pps = priv->current_slice.header.pps;
1250 priv->active_sps = priv->active_pps->sequence;
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;
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);
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;
1281 g_assert (priv->current_frame);
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;
1290 picture = gst_h264_decoder_new_field_picture (self, first_field);
1291 gst_h264_picture_unref (first_field);
1294 GST_ERROR_OBJECT (self, "Couldn't duplicate the first field picture");
1295 return GST_FLOW_ERROR;
1298 picture = gst_h264_picture_new ();
1300 if (klass->new_picture)
1301 ret = klass->new_picture (self, priv->current_frame, picture);
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);
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;
1315 ret = gst_h264_decoder_start_current_picture (self);
1316 if (ret != GST_FLOW_OK) {
1317 GST_WARNING_OBJECT (self, "start picture failed");
1322 return gst_h264_decoder_decode_slice (self);
1325 static GstFlowReturn
1326 gst_h264_decoder_decode_nal (GstH264Decoder * self, GstH264NalUnit * nalu)
1328 GstFlowReturn ret = GST_FLOW_OK;
1330 GST_LOG_OBJECT (self, "Parsed nal type: %d, offset %d, size %d",
1331 nalu->type, nalu->offset, nalu->size);
1333 switch (nalu->type) {
1334 case GST_H264_NAL_SPS:
1335 ret = gst_h264_decoder_parse_sps (self, nalu);
1337 case GST_H264_NAL_PPS:
1338 ret = gst_h264_decoder_parse_pps (self, nalu);
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);
1356 gst_h264_decoder_format_from_caps (GstH264Decoder * self, GstCaps * caps,
1357 GstH264DecoderFormat * format, GstH264DecoderAlign * align)
1360 *format = GST_H264_DECODER_FORMAT_NONE;
1363 *align = GST_H264_DECODER_ALIGN_NONE;
1365 if (!gst_caps_is_fixed (caps)) {
1366 GST_WARNING_OBJECT (self, "Caps wasn't fixed");
1370 GST_DEBUG_OBJECT (self, "parsing caps: %" GST_PTR_FORMAT, caps);
1372 if (caps && gst_caps_get_size (caps) > 0) {
1373 GstStructure *s = gst_caps_get_structure (caps, 0);
1374 const gchar *str = NULL;
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;
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;
1397 gst_h264_decoder_set_format (GstVideoDecoder * decoder,
1398 GstVideoCodecState * state)
1400 GstH264Decoder *self = GST_H264_DECODER (decoder);
1401 GstH264DecoderPrivate *priv = self->priv;
1404 GST_DEBUG_OBJECT (decoder, "Set format");
1406 if (self->input_state)
1407 gst_video_codec_state_unref (self->input_state);
1409 self->input_state = gst_video_codec_state_ref (state);
1412 GstH264DecoderFormat format;
1413 GstH264DecoderAlign align;
1415 gst_h264_decoder_format_from_caps (self, state->caps, &format, &align);
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;
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;
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");
1440 /* AVC implies alignment=au */
1441 if (align == GST_H264_DECODER_ALIGN_NONE)
1442 align = GST_H264_DECODER_ALIGN_AU;
1445 if (format == GST_H264_DECODER_FORMAT_BYTE && state->codec_data)
1446 GST_WARNING_OBJECT (self, "bytestream with codec data");
1448 priv->in_format = format;
1449 priv->align = align;
1452 if (state->codec_data) {
1455 gst_buffer_map (state->codec_data, &map, GST_MAP_READ);
1456 if (gst_h264_decoder_parse_codec_data (self, map.data, map.size) !=
1458 /* keep going without error.
1459 * Probably inband SPS/PPS might be valid data */
1460 GST_WARNING_OBJECT (self, "Failed to handle codec data");
1462 gst_buffer_unmap (state->codec_data, &map);
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);
1473 GST_DEBUG_OBJECT (self, "Live source, will run on low-latency mode");
1479 gst_h264_decoder_fill_picture_from_slice (GstH264Decoder * self,
1480 const GstH264Slice * slice, GstH264Picture * picture)
1482 GstH264DecoderClass *klass = GST_H264_DECODER_GET_CLASS (self);
1483 const GstH264SliceHdr *slice_hdr = &slice->header;
1484 const GstH264PPS *pps;
1485 const GstH264SPS *sps;
1487 pps = slice_hdr->pps;
1489 GST_ERROR_OBJECT (self, "No pps in slice header");
1493 sps = pps->sequence;
1495 GST_ERROR_OBJECT (self, "No sps in pps");
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;
1504 picture->idr_pic_id = slice_hdr->idr_pic_id;
1506 if (slice_hdr->field_pic_flag)
1508 slice_hdr->bottom_field_flag ?
1509 GST_H264_PICTURE_FIELD_BOTTOM_FIELD : GST_H264_PICTURE_FIELD_TOP_FIELD;
1511 picture->field = GST_H264_PICTURE_FIELD_FRAME;
1513 if (!GST_H264_PICTURE_IS_FRAME (picture) && !klass->new_field_picture) {
1514 GST_FIXME_OBJECT (self, "Subclass doesn't support interlace stream");
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);
1523 picture->frame_num = slice_hdr->frame_num;
1526 if (!slice_hdr->field_pic_flag)
1527 picture->pic_num = slice_hdr->frame_num;
1529 picture->pic_num = 2 * slice_hdr->frame_num + 1;
1531 picture->pic_order_cnt_type = sps->pic_order_cnt_type;
1532 switch (picture->pic_order_cnt_type) {
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;
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];
1545 g_assert_not_reached ();
1553 gst_h264_decoder_calculate_poc (GstH264Decoder * self, GstH264Picture * picture)
1555 GstH264DecoderPrivate *priv = self->priv;
1556 const GstH264SPS *sps = priv->active_sps;
1559 GST_ERROR_OBJECT (self, "No active SPS");
1563 switch (picture->pic_order_cnt_type) {
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;
1570 prev_pic_order_cnt_msb = prev_pic_order_cnt_lsb = 0;
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;
1577 prev_pic_order_cnt_msb = 0;
1578 prev_pic_order_cnt_lsb = 0;
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;
1586 max_pic_order_cnt_lsb = 1 << (sps->log2_max_pic_order_cnt_lsb_minus4 + 4);
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;
1599 picture->pic_order_cnt_msb = prev_pic_order_cnt_msb;
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;
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;
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;
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;
1627 gint abs_frame_num = 0;
1628 gint expected_pic_order_cnt = 0;
1631 /* See spec 8.2.1.2 */
1632 if (priv->prev_has_memmgmnt5)
1633 priv->prev_frame_num_offset = 0;
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;
1641 picture->frame_num_offset = priv->prev_frame_num_offset;
1643 if (sps->num_ref_frames_in_pic_order_cnt_cycle != 0)
1644 abs_frame_num = picture->frame_num_offset + picture->frame_num;
1648 if (picture->nal_ref_idc == 0 && abs_frame_num > 0)
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;
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");
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;
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];
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];
1678 if (!picture->nal_ref_idc)
1679 expected_pic_order_cnt += sps->offset_for_non_ref_pic;
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;
1690 picture->bottom_field_order_cnt = expected_pic_order_cnt +
1691 sps->offset_for_top_to_bottom_field + picture->delta_pic_order_cnt0;
1697 gint temp_pic_order_cnt;
1699 /* See spec 8.2.1.3 */
1700 if (priv->prev_has_memmgmnt5)
1701 priv->prev_frame_num_offset = 0;
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;
1709 picture->frame_num_offset = priv->prev_frame_num_offset;
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;
1717 temp_pic_order_cnt =
1718 2 * (picture->frame_num_offset + picture->frame_num);
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;
1727 picture->top_field_order_cnt = temp_pic_order_cnt;
1733 GST_WARNING_OBJECT (self,
1734 "Invalid pic_order_cnt_type: %d", sps->pic_order_cnt_type);
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);
1743 case GST_H264_PICTURE_FIELD_TOP_FIELD:
1744 picture->pic_order_cnt = picture->top_field_order_cnt;
1746 case GST_H264_PICTURE_FIELD_BOTTOM_FIELD:
1747 picture->pic_order_cnt = picture->bottom_field_order_cnt;
1750 g_assert_not_reached ();
1758 gst_h264_decoder_drain_output_queue (GstH264Decoder * self, guint num,
1759 GstFlowReturn * ret)
1761 GstH264DecoderPrivate *priv = self->priv;
1762 GstH264DecoderClass *klass = GST_H264_DECODER_GET_CLASS (self);
1764 g_assert (klass->output_picture);
1765 g_assert (ret != NULL);
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);
1773 UPDATE_FLOW_RETURN (ret, flow_ret);
1778 gst_h264_decoder_do_output_picture (GstH264Decoder * self,
1779 GstH264Picture * picture, GstFlowReturn * ret)
1781 GstH264DecoderPrivate *priv = self->priv;
1782 GstVideoCodecFrame *frame = NULL;
1783 GstH264DecoderOutputFrame output_frame;
1784 GstFlowReturn flow_ret = GST_FLOW_OK;
1786 g_assert (ret != NULL);
1788 GST_LOG_OBJECT (self, "Outputting picture %p (frame_num %d, poc %d)",
1789 picture, picture->frame_num, picture->pic_order_cnt);
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);
1797 priv->last_output_poc = picture->pic_order_cnt;
1799 frame = gst_video_decoder_get_frame (GST_VIDEO_DECODER (self),
1800 picture->system_frame_number);
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);
1808 gst_h264_picture_unref (picture);
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);
1818 gst_h264_decoder_drain_output_queue (self, priv->preferred_output_delay,
1820 UPDATE_FLOW_RETURN (ret, flow_ret);
1824 gst_h264_decoder_finish_current_picture (GstH264Decoder * self,
1825 GstFlowReturn * ret)
1827 GstH264DecoderPrivate *priv = self->priv;
1828 GstH264DecoderClass *klass;
1829 GstFlowReturn flow_ret = GST_FLOW_OK;
1831 if (!priv->current_picture)
1834 klass = GST_H264_DECODER_GET_CLASS (self);
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;
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));
1852 /* We no longer need the per frame reference lists */
1853 gst_h264_decoder_clear_ref_pic_lists (self);
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;
1859 UPDATE_FLOW_RETURN (ret, flow_ret);
1863 poc_asc_compare (const GstH264Picture ** a, const GstH264Picture ** b)
1865 return (*a)->pic_order_cnt - (*b)->pic_order_cnt;
1869 poc_desc_compare (const GstH264Picture ** a, const GstH264Picture ** b)
1871 return (*b)->pic_order_cnt - (*a)->pic_order_cnt;
1874 static GstFlowReturn
1875 gst_h264_decoder_drain_internal (GstH264Decoder * self)
1877 GstH264DecoderPrivate *priv = self->priv;
1878 GstH264Picture *picture;
1879 GstFlowReturn ret = GST_FLOW_OK;
1881 while ((picture = gst_h264_dpb_bump (priv->dpb, TRUE)) != NULL) {
1882 gst_h264_decoder_do_output_picture (self, picture, &ret);
1885 gst_h264_decoder_drain_output_queue (self, 0, &ret);
1887 gst_clear_h264_picture (&priv->last_field);
1888 gst_h264_dpb_clear (priv->dpb);
1889 priv->last_output_poc = G_MININT32;
1895 gst_h264_decoder_handle_memory_management_opt (GstH264Decoder * self,
1896 GstH264Picture * picture)
1898 GstH264DecoderPrivate *priv = self->priv;
1901 for (i = 0; i < G_N_ELEMENTS (picture->dec_ref_pic_marking.ref_pic_marking);
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;
1907 GST_TRACE_OBJECT (self, "memory management operation %d, type %d", i, type);
1909 /* Normal end of operations' specification */
1915 priv->max_long_term_frame_idx =
1916 ref_pic_marking->max_long_term_frame_idx_plus1 - 1;
1919 priv->max_long_term_frame_idx = -1;
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",
1929 /* Most likely our implementation fault, but let's just perform
1930 * next MMCO if any */
1938 gst_h264_decoder_sliding_window_picture_marking (GstH264Decoder * self,
1939 GstH264Picture * picture)
1941 GstH264DecoderPrivate *priv = self->priv;
1942 const GstH264SPS *sps = priv->active_sps;
1944 gint max_num_ref_frames;
1946 /* Skip this for the second field */
1947 if (picture->second_field)
1951 GST_ERROR_OBJECT (self, "No active sps");
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);
1959 if (num_ref_pics < max_num_ref_frames)
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.
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
1971 GstH264Picture *to_unmark =
1972 gst_h264_dpb_get_lowest_frame_num_short_ref (priv->dpb);
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);
1981 GST_WARNING_OBJECT (self, "Could not find a short ref picture to unmark");
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);
1989 gst_h264_picture_set_reference (to_unmark, GST_H264_PICTURE_REF_NONE, TRUE);
1990 gst_h264_picture_unref (to_unmark);
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 */
2004 gst_h264_decoder_reference_picture_marking (GstH264Decoder * self,
2005 GstH264Picture * picture)
2007 GstH264DecoderPrivate *priv = self->priv;
2009 /* If the current picture is an IDR, all reference pictures are unmarked */
2011 gst_h264_dpb_mark_all_non_ref (priv->dpb);
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;
2019 gst_h264_picture_set_reference (picture,
2020 GST_H264_PICTURE_REF_SHORT_TERM, FALSE);
2021 priv->max_long_term_frame_idx = -1;
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);
2038 return gst_h264_decoder_handle_memory_management_opt (self, picture);
2041 return gst_h264_decoder_sliding_window_picture_marking (self, picture);
2044 static GstH264DpbBumpMode
2045 get_bump_level (GstH264Decoder * self)
2047 GstH264DecoderPrivate *priv = self->priv;
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;
2061 /* GST_H264_DECODER_COMPLIANCE_AUTO case. */
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;
2069 return GST_H264_DPB_BUMP_LOW_LATENCY;
2072 return GST_H264_DPB_BUMP_NORMAL_LATENCY;
2076 gst_h264_decoder_finish_picture (GstH264Decoder * self,
2077 GstH264Picture * picture, GstFlowReturn * ret)
2079 GstVideoDecoder *decoder = GST_VIDEO_DECODER (self);
2080 GstH264DecoderPrivate *priv = self->priv;
2081 GstH264DpbBumpMode bump_level = get_bump_level (self);
2083 /* Finish processing the picture.
2084 * Start by storing previous picture data for later use */
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;
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;
2099 /* Remove unused (for reference or later output) pictures from DPB, marking
2101 gst_h264_dpb_delete_unused (priv->dpb);
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);
2112 gst_video_decoder_release_frame (decoder, frame);
2116 if (picture->mem_mgmt_5) {
2117 GstFlowReturn drain_ret;
2119 GST_TRACE_OBJECT (self, "Memory management type 5, drain the DPB");
2121 drain_ret = gst_h264_decoder_drain_internal (self);
2122 UPDATE_FLOW_RETURN (ret, drain_ret);
2125 _bump_dpb (self, bump_level, picture, ret);
2127 /* Add a ref to avoid the case of directly outputed and destroyed. */
2128 gst_h264_picture_ref (picture);
2131 - If the current decoded picture is the second field of a complementary
2132 reference field pair, add to DPB.
2134 For A reference decoded picture, the "bumping" process is invoked
2135 repeatedly until there is an empty frame buffer, then add to DPB:
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.
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);
2153 add_picture_to_dpb (self, picture);
2155 GST_WARNING_OBJECT (self,
2156 "Couldn't split frame into complementary field pair");
2157 /* Keep decoding anyway... */
2159 add_picture_to_dpb (self, other_field);
2162 add_picture_to_dpb (self, picture);
2165 output_picture_directly (self, picture, ret);
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));
2173 gst_h264_picture_unref (picture);
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);
2182 gst_h264_decoder_update_max_num_reorder_frames (GstH264Decoder * self,
2185 GstH264DecoderPrivate *priv = self->priv;
2186 gsize max_num_reorder_frames = 0;
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)) {
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));
2197 max_num_reorder_frames = 0;
2201 gst_h264_dpb_set_max_num_reorder_frames (priv->dpb, max_num_reorder_frames);
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));
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) {
2226 max_num_reorder_frames = 0;
2229 max_num_reorder_frames = gst_h264_dpb_get_max_num_frames (priv->dpb);
2233 max_num_reorder_frames = gst_h264_dpb_get_max_num_frames (priv->dpb);
2236 gst_h264_dpb_set_max_num_reorder_frames (priv->dpb, max_num_reorder_frames);
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;
2267 GstH264DecoderLevel level;
2271 guint32 max_dpb_mbs;
2272 guint32 max_main_br;
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}
2299 h264_level_to_max_dpb_mbs (GstH264DecoderLevel level)
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;
2311 gst_h264_decoder_set_latency (GstH264Decoder * self, const GstH264SPS * sps,
2314 GstH264DecoderPrivate *priv = self->priv;
2316 GstClockTime min, max;
2317 GstStructure *structure;
2318 gint fps_d = 1, fps_n = 0;
2319 GstH264DpbBumpMode bump_level;
2320 guint32 frames_delay;
2322 caps = gst_pad_get_current_caps (GST_VIDEO_DECODER_SRC_PAD (self));
2326 structure = gst_caps_get_structure (caps, 0);
2327 if (gst_structure_get_fraction (structure, "framerate", &fps_n, &fps_d)) {
2329 /* variable framerate: see if we have a max-framerate */
2330 gst_structure_get_fraction (structure, "max-framerate", &fps_n, &fps_d);
2333 gst_caps_unref (caps);
2335 /* if no fps or variable, then 25/1 */
2341 bump_level = get_bump_level (self);
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;
2348 case GST_H264_DPB_BUMP_LOW_LATENCY:
2349 /* We bump the IDR if the second frame is not a minus POC. */
2352 case GST_H264_DPB_BUMP_VERY_LOW_LATENCY:
2353 /* We bump the IDR immediately. */
2357 g_assert_not_reached ();
2361 /* Consider output delay wanted by subclass */
2362 frames_delay += priv->preferred_output_delay;
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);
2368 GST_LOG_OBJECT (self,
2369 "latency min %" G_GUINT64_FORMAT " max %" G_GUINT64_FORMAT, min, max);
2371 gst_video_decoder_set_latency (GST_VIDEO_DECODER (self), min, max);
2374 static GstFlowReturn
2375 gst_h264_decoder_process_sps (GstH264Decoder * self, GstH264SPS * sps)
2377 GstH264DecoderClass *klass = GST_H264_DECODER_GET_CLASS (self);
2378 GstH264DecoderPrivate *priv = self->priv;
2381 gint width_mb, height_mb;
2382 gint max_dpb_frames;
2384 gint prev_max_dpb_size;
2385 gboolean prev_interlaced;
2386 gboolean interlaced;
2387 GstFlowReturn ret = GST_FLOW_OK;
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;
2396 if (sps->mb_adaptive_frame_field_flag) {
2397 GST_LOG_OBJECT (self,
2398 "mb_adaptive_frame_field_flag == 1, MBAFF sequence");
2400 GST_LOG_OBJECT (self, "mb_adaptive_frame_field_flag == 0, PAFF sequence");
2404 interlaced = !sps->frame_mbs_only_flag;
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
2410 level = sps->level_idc;
2411 if (level == 11 && (sps->profile_idc == 66 || sps->profile_idc == 77) &&
2412 sps->constraint_set3_flag) {
2417 max_dpb_mbs = h264_level_to_max_dpb_mbs ((GstH264DecoderLevel) level);
2419 return GST_FLOW_ERROR;
2421 width_mb = sps->width / 16;
2422 height_mb = sps->height / 16;
2424 max_dpb_frames = MIN (max_dpb_mbs / (width_mb * height_mb),
2425 GST_H264_DPB_MAX_SIZE);
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);
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/
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;
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);
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);
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);
2459 ret = gst_h264_decoder_drain (GST_VIDEO_DECODER (self));
2460 if (ret != GST_FLOW_OK)
2463 g_assert (klass->new_sequence);
2465 if (klass->get_preferred_output_delay) {
2466 priv->preferred_output_delay =
2467 klass->get_preferred_output_delay (self, priv->is_live);
2469 priv->preferred_output_delay = 0;
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");
2479 priv->profile_idc = sps->profile_idc;
2480 priv->width = sps->width;
2481 priv->height = sps->height;
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);
2488 if (!gst_h264_decoder_update_max_num_reorder_frames (self, sps))
2489 return GST_FLOW_ERROR;
2495 gst_h264_decoder_init_gap_picture (GstH264Decoder * self,
2496 GstH264Picture * picture, gint frame_num)
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;
2507 return gst_h264_decoder_calculate_poc (self, picture);
2510 static GstFlowReturn
2511 gst_h264_decoder_decode_slice (GstH264Decoder * self)
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;
2522 GST_ERROR_OBJECT (self, "No current picture");
2523 return GST_FLOW_ERROR;
2526 GST_LOG_OBJECT (self, "Decode picture %p (frame_num %d, poc %d)",
2527 picture, picture->frame_num, picture->pic_order_cnt);
2529 priv->max_pic_num = slice->header.max_pic_num;
2531 if (priv->process_ref_pic_lists) {
2532 if (!gst_h264_decoder_modify_ref_pic_lists (self)) {
2533 ret = GST_FLOW_ERROR;
2537 ref_pic_list0 = priv->ref_pic_list0;
2538 ref_pic_list1 = priv->ref_pic_list1;
2541 g_assert (klass->decode_slice);
2543 ret = klass->decode_slice (self, picture, slice, ref_pic_list0,
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);
2552 g_array_set_size (priv->ref_pic_list0, 0);
2553 g_array_set_size (priv->ref_pic_list1, 0);
2559 pic_num_desc_compare (const GstH264Picture ** a, const GstH264Picture ** b)
2561 return (*b)->pic_num - (*a)->pic_num;
2565 long_term_pic_num_asc_compare (const GstH264Picture ** a,
2566 const GstH264Picture ** b)
2568 return (*a)->long_term_pic_num - (*b)->long_term_pic_num;
2572 construct_ref_pic_lists_p (GstH264Decoder * self,
2573 GstH264Picture * current_picture)
2575 GstH264DecoderPrivate *priv = self->priv;
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.
2582 g_array_set_size (priv->ref_pic_list_p0, 0);
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);
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);
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);
2604 g_string_append_printf (str, "|%is", ref->pic_num);
2606 GST_DEBUG_OBJECT (self, "ref_pic_list_p0: %s|", str->str);
2607 g_string_free (str, TRUE);
2613 frame_num_wrap_desc_compare (const GstH264Picture ** a,
2614 const GstH264Picture ** b)
2616 return (*b)->frame_num_wrap - (*a)->frame_num_wrap;
2620 long_term_frame_idx_asc_compare (const GstH264Picture ** a,
2621 const GstH264Picture ** b)
2623 return (*a)->long_term_frame_idx - (*b)->long_term_frame_idx;
2626 /* init_picture_refs_fields_1 in gstvaapidecoder_h264.c */
2628 init_picture_refs_fields_1 (GstH264Decoder * self, GstH264PictureField field,
2629 GArray * ref_frame_list, GArray * ref_pic_list_x)
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);
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);
2653 } while (i < ref_frame_list->len || j < ref_frame_list->len);
2657 construct_ref_field_pic_lists_p (GstH264Decoder * self,
2658 GstH264Picture * current_picture)
2660 GstH264DecoderPrivate *priv = self->priv;
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);
2667 /* 8.2.4.2.2, 8.2.4.2.5 refFrameList0ShortTerm:
2668 * short-term ref pictures sorted by descending frame_num_wrap.
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);
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);
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);
2690 /* 8.2.4.2.2 refFrameList0LongTerm,:
2691 * long-term ref pictures sorted by ascending long_term_frame_idx.
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);
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,
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);
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);
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,
2731 g_string_append_printf (str, "|%i(%d)l", ref->long_term_frame_idx,
2734 GST_DEBUG_OBJECT (self, "ref_pic_list_p0 (%d): %s|", current_picture->field,
2736 g_string_free (str, TRUE);
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);
2746 lists_are_equal (GArray * l1, GArray * l2)
2750 if (l1->len != l2->len)
2753 for (i = 0; i < l1->len; i++)
2754 if (g_array_index (l1, gpointer, i) != g_array_index (l2, gpointer, i))
2761 split_ref_pic_list_b (GstH264Decoder * self, GArray * ref_pic_list_b,
2762 GCompareFunc compare_func)
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)
2776 print_ref_pic_list_b (GstH264Decoder * self, GArray * ref_list_b,
2779 #ifndef GST_DISABLE_GST_DEBUG
2783 if (gst_debug_category_get_threshold (GST_CAT_DEFAULT) < GST_LEVEL_DEBUG)
2786 str = g_string_new (NULL);
2788 for (i = 0; i < ref_list_b->len; i++) {
2789 GstH264Picture *ref = g_array_index (ref_list_b, GstH264Picture *, i);
2791 if (!GST_H264_PICTURE_IS_LONG_TERM_REF (ref))
2792 g_string_append_printf (str, "|%i", ref->pic_order_cnt);
2794 g_string_append_printf (str, "|%il", ref->long_term_pic_num);
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);
2804 construct_ref_pic_lists_b (GstH264Decoder * self,
2805 GstH264Picture * current_picture)
2807 GstH264DecoderPrivate *priv = self->priv;
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.
2815 g_array_set_size (priv->ref_pic_list_b0, 0);
2816 g_array_set_size (priv->ref_pic_list_b1, 0);
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
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);
2826 /* First sort ascending, this will put [1] in right place and finish
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");
2832 /* Find first with POC > current_picture's POC to get first element
2834 pos = split_ref_pic_list_b (self, priv->ref_pic_list_b0,
2835 (GCompareFunc) poc_asc_compare);
2837 GST_DEBUG_OBJECT (self, "split point %i", pos);
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);
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);
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.
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);
2859 /* First sort by descending POC. */
2860 g_array_sort (priv->ref_pic_list_b1, (GCompareFunc) poc_desc_compare);
2862 /* Split at first with POC < current_picture's POC to get first element
2864 pos = split_ref_pic_list_b (self, priv->ref_pic_list_b1,
2865 (GCompareFunc) poc_desc_compare);
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);
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);
2879 /* If lists identical, swap first two entries in RefPicList1 (spec
2881 if (priv->ref_pic_list_b1->len > 1
2882 && lists_are_equal (priv->ref_pic_list_b0, priv->ref_pic_list_b1)) {
2884 GstH264Picture **list = (GstH264Picture **) priv->ref_pic_list_b1->data;
2885 GstH264Picture *pic = list[0];
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");
2895 construct_ref_field_pic_lists_b (GstH264Decoder * self,
2896 GstH264Picture * current_picture)
2898 GstH264DecoderPrivate *priv = self->priv;
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,
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);
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
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);
2920 /* First sort ascending, this will put [1] in right place and finish
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");
2929 /* Find first with POC > current_picture's POC to get first element
2931 pos = split_ref_pic_list_b (self, priv->ref_frame_list_0_short_term,
2932 (GCompareFunc) poc_asc_compare);
2934 GST_DEBUG_OBJECT (self, "split point %i", pos);
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);
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,
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);
2948 /* First sort by descending POC. */
2949 g_array_sort (priv->ref_frame_list_1_short_term,
2950 (GCompareFunc) poc_desc_compare);
2952 /* Split at first with POC < current_picture's POC to get first element
2954 pos = split_ref_pic_list_b (self, priv->ref_frame_list_1_short_term,
2955 (GCompareFunc) poc_desc_compare);
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);
2961 /* 8.2.4.2.2 refFrameList0LongTerm,:
2962 * long-term ref pictures sorted by ascending long_term_frame_idx.
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);
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);
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);
2981 /* If lists identical, swap first two entries in RefPicList1 (spec
2983 if (priv->ref_pic_list_b1->len > 1
2984 && lists_are_equal (priv->ref_pic_list_b0, priv->ref_pic_list_b1)) {
2986 GstH264Picture **list = (GstH264Picture **) priv->ref_pic_list_b1->data;
2987 GstH264Picture *pic = list[0];
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");
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);
3003 gst_h264_decoder_prepare_ref_pic_lists (GstH264Decoder * self,
3004 GstH264Picture * current_picture)
3006 GstH264DecoderPrivate *priv = self->priv;
3007 gboolean construct_list = FALSE;
3009 GArray *dpb_array = gst_h264_dpb_get_pictures_all (priv->dpb);
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"
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;
3024 g_array_unref (dpb_array);
3026 if (!construct_list) {
3027 gst_h264_decoder_clear_ref_pic_lists (self);
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);
3035 construct_ref_field_pic_lists_p (self, current_picture);
3036 construct_ref_field_pic_lists_b (self, current_picture);
3041 gst_h264_decoder_clear_ref_pic_lists (GstH264Decoder * self)
3043 GstH264DecoderPrivate *priv = self->priv;
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);
3051 long_term_pic_num_f (GstH264Decoder * self, const GstH264Picture * picture)
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);
3059 pic_num_f (GstH264Decoder * self, const GstH264Picture * picture)
3061 if (!GST_H264_PICTURE_IS_LONG_TERM_REF (picture))
3062 return picture->pic_num;
3063 return self->priv->max_pic_num;
3066 /* shift elements on the |array| starting from |from| to |to|,
3067 * inclusive, one position to the right and insert pic at |from| */
3069 shift_right_and_insert (GArray * array, gint from, gint to,
3070 GstH264Picture * picture)
3072 g_return_if_fail (from <= to);
3073 g_return_if_fail (array && picture);
3075 g_array_set_size (array, to + 2);
3076 g_array_insert_val (array, from, picture);
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. */
3083 modify_ref_pic_list (GstH264Decoder * self, int list)
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;
3094 gint pic_num_lx_pred = picture->pic_num;
3095 gint ref_idx_lx = 0, src, dst;
3096 gint pic_num_lx_no_wrap;
3098 gboolean done = FALSE;
3099 GstH264Picture *pic;
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;
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;
3117 /* Resize the list to the size requested in the slice header.
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);
3128 if (!ref_pic_list_modification_flag_lX)
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. */
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
3145 if (pic_num_lx_no_wrap < 0)
3146 pic_num_lx_no_wrap += priv->max_pic_num;
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;
3157 /* For use in next iteration */
3158 pic_num_lx_pred = pic_num_lx_no_wrap;
3161 if (pic_num_lx_no_wrap > picture->pic_num)
3162 pic_num_lx = pic_num_lx_no_wrap - priv->max_pic_num;
3164 pic_num_lx = pic_num_lx_no_wrap;
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);
3170 GST_WARNING_OBJECT (self, "Malformed stream, no pic num %d",
3174 shift_right_and_insert (ref_pic_listx, ref_idx_lx,
3175 num_ref_idx_lX_active_minus1, pic);
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;
3189 /* 8.2.4.3.2 - Long-term reference pictures */
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);
3196 GST_WARNING_OBJECT (self, "Malformed stream, no pic num %d",
3197 list_mod->value.long_term_pic_num);
3200 shift_right_and_insert (ref_pic_listx, ref_idx_lx,
3201 num_ref_idx_lX_active_minus1, pic);
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;
3215 /* End of modification list */
3221 /* may be recoverable */
3222 GST_WARNING ("Invalid modification_of_pic_nums_idc = %d",
3223 list_mod->modification_of_pic_nums_idc);
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);
3240 copy_pic_list_into (GArray * dest, GArray * src)
3243 g_array_set_size (dest, 0);
3245 for (i = 0; i < src->len; i++)
3246 g_array_append_val (dest, g_array_index (src, gpointer, i));
3250 gst_h264_decoder_modify_ref_pic_lists (GstH264Decoder * self)
3252 GstH264DecoderPrivate *priv = self->priv;
3253 GstH264SliceHdr *slice_hdr = &priv->current_slice.header;
3255 g_array_set_size (priv->ref_pic_list0, 0);
3256 g_array_set_size (priv->ref_pic_list1, 0);
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);
3274 * gst_h264_decoder_set_process_ref_pic_lists:
3275 * @decoder: a #GstH264Decoder
3276 * @process: whether subclass is requiring reference picture modification process
3278 * Called to en/disable reference picture modification process.
3283 gst_h264_decoder_set_process_ref_pic_lists (GstH264Decoder * decoder,
3286 decoder->priv->process_ref_pic_lists = process;
3290 * gst_h264_decoder_get_picture:
3291 * @decoder: a #GstH264Decoder
3292 * @system_frame_number: a target system frame number of #GstH264Picture
3294 * Retrive DPB and return a #GstH264Picture corresponding to
3295 * the @system_frame_number
3297 * Returns: (transfer full): a #GstH264Picture if successful, or %NULL otherwise
3302 gst_h264_decoder_get_picture (GstH264Decoder * decoder,
3303 guint32 system_frame_number)
3305 return gst_h264_dpb_get_picture (decoder->priv->dpb, system_frame_number);