2019-12-26 05:24:46 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) 2019 Seungha Yang <seungha.yang@navercorp.com>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* NOTE: some of implementations are copied/modified from Chromium code
|
|
|
|
*
|
|
|
|
* Copyright 2015 The Chromium Authors. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are
|
|
|
|
* met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above
|
|
|
|
* copyright notice, this list of conditions and the following disclaimer
|
|
|
|
* in the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* * Neither the name of Google Inc. nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived from
|
|
|
|
* this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
2020-04-08 12:47:37 +00:00
|
|
|
/**
|
|
|
|
* SECTION:gsth264decoder
|
|
|
|
* @title: GstH264Decoder
|
|
|
|
* @short_description: Base class to implement stateless H.264 decoders
|
|
|
|
* @sources:
|
|
|
|
* - gsth264picture.h
|
|
|
|
*/
|
2019-12-26 05:24:46 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2020-12-29 10:54:35 +00:00
|
|
|
#include <gst/base/base.h>
|
2019-12-26 05:24:46 +00:00
|
|
|
#include "gsth264decoder.h"
|
|
|
|
|
2020-01-31 22:54:57 +00:00
|
|
|
GST_DEBUG_CATEGORY (gst_h264_decoder_debug);
|
|
|
|
#define GST_CAT_DEFAULT gst_h264_decoder_debug
|
2019-12-26 05:24:46 +00:00
|
|
|
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
GST_H264_DECODER_FORMAT_NONE,
|
|
|
|
GST_H264_DECODER_FORMAT_AVC,
|
|
|
|
GST_H264_DECODER_FORMAT_BYTE
|
|
|
|
} GstH264DecoderFormat;
|
|
|
|
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
GST_H264_DECODER_ALIGN_NONE,
|
|
|
|
GST_H264_DECODER_ALIGN_NAL,
|
|
|
|
GST_H264_DECODER_ALIGN_AU
|
|
|
|
} GstH264DecoderAlign;
|
|
|
|
|
|
|
|
struct _GstH264DecoderPrivate
|
|
|
|
{
|
2021-07-26 08:09:19 +00:00
|
|
|
GstH264DecoderCompliance compliance;
|
|
|
|
|
2021-07-28 15:19:15 +00:00
|
|
|
guint8 profile_idc;
|
2019-12-26 05:24:46 +00:00
|
|
|
gint width, height;
|
2021-01-10 13:01:27 +00:00
|
|
|
|
2019-12-26 05:24:46 +00:00
|
|
|
/* input codec_data, if any */
|
|
|
|
GstBuffer *codec_data;
|
|
|
|
guint nal_length_size;
|
|
|
|
|
|
|
|
/* state */
|
|
|
|
GstH264DecoderFormat in_format;
|
|
|
|
GstH264DecoderAlign align;
|
|
|
|
GstH264NalParser *parser;
|
|
|
|
GstH264Dpb *dpb;
|
2021-07-27 02:51:03 +00:00
|
|
|
/* Cache last field which can not enter the DPB, should be a non ref */
|
|
|
|
GstH264Picture *last_field;
|
|
|
|
|
2020-04-24 11:56:17 +00:00
|
|
|
/* used for low-latency vs. high throughput mode decision */
|
|
|
|
gboolean is_live;
|
2019-12-26 05:24:46 +00:00
|
|
|
|
|
|
|
/* sps/pps of the current slice */
|
|
|
|
const GstH264SPS *active_sps;
|
|
|
|
const GstH264PPS *active_pps;
|
|
|
|
|
|
|
|
/* Picture currently being processed/decoded */
|
|
|
|
GstH264Picture *current_picture;
|
2020-02-05 12:27:23 +00:00
|
|
|
GstVideoCodecFrame *current_frame;
|
2019-12-26 05:24:46 +00:00
|
|
|
|
|
|
|
/* Slice (slice header + nalu) currently being processed/decodec */
|
|
|
|
GstH264Slice current_slice;
|
|
|
|
|
|
|
|
gint max_frame_num;
|
|
|
|
gint max_pic_num;
|
|
|
|
gint max_long_term_frame_idx;
|
|
|
|
|
|
|
|
gint prev_frame_num;
|
|
|
|
gint prev_ref_frame_num;
|
|
|
|
gint prev_frame_num_offset;
|
|
|
|
gboolean prev_has_memmgmnt5;
|
|
|
|
|
|
|
|
/* Values related to previously decoded reference picture */
|
|
|
|
gboolean prev_ref_has_memmgmnt5;
|
|
|
|
gint prev_ref_top_field_order_cnt;
|
|
|
|
gint prev_ref_pic_order_cnt_msb;
|
|
|
|
gint prev_ref_pic_order_cnt_lsb;
|
|
|
|
|
|
|
|
GstH264PictureField prev_ref_field;
|
|
|
|
|
|
|
|
/* PicOrderCount of the previously outputted frame */
|
|
|
|
gint last_output_poc;
|
2020-05-15 18:56:27 +00:00
|
|
|
|
2020-04-27 14:53:45 +00:00
|
|
|
gboolean process_ref_pic_lists;
|
2020-12-29 10:54:35 +00:00
|
|
|
guint preferred_output_delay;
|
2020-04-27 14:53:45 +00:00
|
|
|
|
|
|
|
/* Reference picture lists, constructed for each frame */
|
|
|
|
GArray *ref_pic_list_p0;
|
|
|
|
GArray *ref_pic_list_b0;
|
|
|
|
GArray *ref_pic_list_b1;
|
|
|
|
|
2020-11-14 15:20:54 +00:00
|
|
|
/* Temporary picture list, for reference picture lists in fields,
|
|
|
|
* corresponding to 8.2.4.2.2 refFrameList0ShortTerm, refFrameList0LongTerm
|
|
|
|
* and 8.2.4.2.5 refFrameList1ShortTerm and refFrameListLongTerm */
|
|
|
|
GArray *ref_frame_list_0_short_term;
|
|
|
|
GArray *ref_frame_list_1_short_term;
|
|
|
|
GArray *ref_frame_list_long_term;
|
|
|
|
|
2020-05-03 15:30:34 +00:00
|
|
|
/* Reference picture lists, constructed for each slice */
|
|
|
|
GArray *ref_pic_list0;
|
|
|
|
GArray *ref_pic_list1;
|
2020-12-29 10:54:35 +00:00
|
|
|
|
|
|
|
/* For delayed output */
|
|
|
|
GstQueueArray *output_queue;
|
2019-12-26 05:24:46 +00:00
|
|
|
};
|
|
|
|
|
2020-12-29 10:54:35 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
/* Holds ref */
|
|
|
|
GstVideoCodecFrame *frame;
|
|
|
|
GstH264Picture *picture;
|
|
|
|
/* Without ref */
|
|
|
|
GstH264Decoder *self;
|
|
|
|
} GstH264DecoderOutputFrame;
|
|
|
|
|
2021-09-21 13:21:51 +00:00
|
|
|
#define UPDATE_FLOW_RETURN(ret,new_ret) G_STMT_START { \
|
|
|
|
if (*(ret) == GST_FLOW_OK) \
|
|
|
|
*(ret) = new_ret; \
|
|
|
|
} G_STMT_END
|
|
|
|
|
2019-12-26 05:24:46 +00:00
|
|
|
#define parent_class gst_h264_decoder_parent_class
|
2020-01-31 22:54:57 +00:00
|
|
|
G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GstH264Decoder, gst_h264_decoder,
|
|
|
|
GST_TYPE_VIDEO_DECODER,
|
|
|
|
G_ADD_PRIVATE (GstH264Decoder);
|
|
|
|
GST_DEBUG_CATEGORY_INIT (gst_h264_decoder_debug, "h264decoder", 0,
|
|
|
|
"H.264 Video Decoder"));
|
2019-12-26 05:24:46 +00:00
|
|
|
|
2020-05-15 18:56:27 +00:00
|
|
|
static void gst_h264_decoder_finalize (GObject * object);
|
|
|
|
|
2019-12-26 05:24:46 +00:00
|
|
|
static gboolean gst_h264_decoder_start (GstVideoDecoder * decoder);
|
|
|
|
static gboolean gst_h264_decoder_stop (GstVideoDecoder * decoder);
|
|
|
|
static gboolean gst_h264_decoder_set_format (GstVideoDecoder * decoder,
|
|
|
|
GstVideoCodecState * state);
|
|
|
|
static GstFlowReturn gst_h264_decoder_finish (GstVideoDecoder * decoder);
|
|
|
|
static gboolean gst_h264_decoder_flush (GstVideoDecoder * decoder);
|
|
|
|
static GstFlowReturn gst_h264_decoder_drain (GstVideoDecoder * decoder);
|
2020-01-30 11:04:58 +00:00
|
|
|
static GstFlowReturn gst_h264_decoder_handle_frame (GstVideoDecoder * decoder,
|
|
|
|
GstVideoCodecFrame * frame);
|
2019-12-26 05:24:46 +00:00
|
|
|
|
2021-09-21 13:21:51 +00:00
|
|
|
/* codec specific functions */
|
|
|
|
static GstFlowReturn gst_h264_decoder_process_sps (GstH264Decoder * self,
|
2019-12-26 05:24:46 +00:00
|
|
|
GstH264SPS * sps);
|
2021-10-06 16:54:29 +00:00
|
|
|
static GstFlowReturn gst_h264_decoder_decode_slice (GstH264Decoder * self);
|
2021-09-21 13:21:51 +00:00
|
|
|
static GstFlowReturn gst_h264_decoder_decode_nal (GstH264Decoder * self,
|
2020-11-04 09:47:30 +00:00
|
|
|
GstH264NalUnit * nalu);
|
2019-12-26 05:24:46 +00:00
|
|
|
static gboolean gst_h264_decoder_fill_picture_from_slice (GstH264Decoder * self,
|
|
|
|
const GstH264Slice * slice, GstH264Picture * picture);
|
|
|
|
static gboolean gst_h264_decoder_calculate_poc (GstH264Decoder * self,
|
|
|
|
GstH264Picture * picture);
|
|
|
|
static gboolean gst_h264_decoder_init_gap_picture (GstH264Decoder * self,
|
|
|
|
GstH264Picture * picture, gint frame_num);
|
2021-09-21 13:21:51 +00:00
|
|
|
static GstFlowReturn gst_h264_decoder_drain_internal (GstH264Decoder * self);
|
|
|
|
static void gst_h264_decoder_finish_current_picture (GstH264Decoder * self,
|
|
|
|
GstFlowReturn * ret);
|
|
|
|
static void gst_h264_decoder_finish_picture (GstH264Decoder * self,
|
|
|
|
GstH264Picture * picture, GstFlowReturn * ret);
|
2020-11-17 09:39:56 +00:00
|
|
|
static void gst_h264_decoder_prepare_ref_pic_lists (GstH264Decoder * self,
|
|
|
|
GstH264Picture * current_picture);
|
2020-04-27 14:53:45 +00:00
|
|
|
static void gst_h264_decoder_clear_ref_pic_lists (GstH264Decoder * self);
|
2020-05-03 15:30:34 +00:00
|
|
|
static gboolean gst_h264_decoder_modify_ref_pic_lists (GstH264Decoder * self);
|
2020-11-01 15:08:04 +00:00
|
|
|
static gboolean
|
2020-11-09 16:28:03 +00:00
|
|
|
gst_h264_decoder_sliding_window_picture_marking (GstH264Decoder * self,
|
|
|
|
GstH264Picture * picture);
|
2020-11-01 15:08:04 +00:00
|
|
|
static void gst_h264_decoder_do_output_picture (GstH264Decoder * self,
|
2021-09-21 13:21:51 +00:00
|
|
|
GstH264Picture * picture, GstFlowReturn * ret);
|
2020-11-17 09:59:35 +00:00
|
|
|
static GstH264Picture *gst_h264_decoder_new_field_picture (GstH264Decoder *
|
|
|
|
self, GstH264Picture * picture);
|
2020-12-29 10:54:35 +00:00
|
|
|
static void
|
|
|
|
gst_h264_decoder_clear_output_frame (GstH264DecoderOutputFrame * output_frame);
|
2019-12-26 05:24:46 +00:00
|
|
|
|
2021-07-26 08:09:19 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_0,
|
|
|
|
PROP_COMPLIANCE,
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_h264_decoder_compliance_get_type:
|
|
|
|
*
|
|
|
|
* Get the compliance type of the h264 decoder.
|
|
|
|
*
|
|
|
|
* Since: 1.20
|
|
|
|
*/
|
|
|
|
GType
|
|
|
|
gst_h264_decoder_compliance_get_type (void)
|
|
|
|
{
|
|
|
|
static gsize h264_decoder_compliance_type = 0;
|
|
|
|
static const GEnumValue compliances[] = {
|
|
|
|
{GST_H264_DECODER_COMPLIANCE_AUTO, "GST_H264_DECODER_COMPLIANCE_AUTO",
|
|
|
|
"auto"},
|
|
|
|
{GST_H264_DECODER_COMPLIANCE_STRICT, "GST_H264_DECODER_COMPLIANCE_STRICT",
|
|
|
|
"strict"},
|
|
|
|
{GST_H264_DECODER_COMPLIANCE_NORMAL, "GST_H264_DECODER_COMPLIANCE_NORMAL",
|
|
|
|
"normal"},
|
|
|
|
{GST_H264_DECODER_COMPLIANCE_FLEXIBLE,
|
|
|
|
"GST_H264_DECODER_COMPLIANCE_FLEXIBLE", "flexible"},
|
|
|
|
{0, NULL, NULL},
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
if (g_once_init_enter (&h264_decoder_compliance_type)) {
|
|
|
|
GType _type;
|
|
|
|
|
|
|
|
_type = g_enum_register_static ("GstH264DecoderCompliance", compliances);
|
|
|
|
g_once_init_leave (&h264_decoder_compliance_type, _type);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (GType) h264_decoder_compliance_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_h264_decoder_get_property (GObject * object, guint property_id,
|
|
|
|
GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstH264Decoder *self = GST_H264_DECODER (object);
|
|
|
|
GstH264DecoderPrivate *priv = self->priv;
|
|
|
|
|
|
|
|
switch (property_id) {
|
|
|
|
case PROP_COMPLIANCE:
|
|
|
|
GST_OBJECT_LOCK (self);
|
|
|
|
g_value_set_enum (value, priv->compliance);
|
|
|
|
GST_OBJECT_UNLOCK (self);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_h264_decoder_set_property (GObject * object, guint property_id,
|
|
|
|
const GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstH264Decoder *self = GST_H264_DECODER (object);
|
|
|
|
GstH264DecoderPrivate *priv = self->priv;
|
|
|
|
|
|
|
|
switch (property_id) {
|
|
|
|
case PROP_COMPLIANCE:
|
|
|
|
GST_OBJECT_LOCK (self);
|
|
|
|
priv->compliance = g_value_get_enum (value);
|
|
|
|
GST_OBJECT_UNLOCK (self);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-26 05:24:46 +00:00
|
|
|
static void
|
|
|
|
gst_h264_decoder_class_init (GstH264DecoderClass * klass)
|
|
|
|
{
|
|
|
|
GstVideoDecoderClass *decoder_class = GST_VIDEO_DECODER_CLASS (klass);
|
2020-05-15 18:56:27 +00:00
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
|
|
|
|
|
|
|
object_class->finalize = GST_DEBUG_FUNCPTR (gst_h264_decoder_finalize);
|
2021-07-26 08:09:19 +00:00
|
|
|
object_class->get_property = gst_h264_decoder_get_property;
|
|
|
|
object_class->set_property = gst_h264_decoder_set_property;
|
2019-12-26 05:24:46 +00:00
|
|
|
|
|
|
|
decoder_class->start = GST_DEBUG_FUNCPTR (gst_h264_decoder_start);
|
|
|
|
decoder_class->stop = GST_DEBUG_FUNCPTR (gst_h264_decoder_stop);
|
|
|
|
decoder_class->set_format = GST_DEBUG_FUNCPTR (gst_h264_decoder_set_format);
|
|
|
|
decoder_class->finish = GST_DEBUG_FUNCPTR (gst_h264_decoder_finish);
|
|
|
|
decoder_class->flush = GST_DEBUG_FUNCPTR (gst_h264_decoder_flush);
|
|
|
|
decoder_class->drain = GST_DEBUG_FUNCPTR (gst_h264_decoder_drain);
|
2020-01-30 11:04:58 +00:00
|
|
|
decoder_class->handle_frame =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_h264_decoder_handle_frame);
|
2021-07-26 08:09:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* GstH264Decoder:compliance:
|
|
|
|
*
|
|
|
|
* The compliance controls the behavior of the decoder to handle some
|
|
|
|
* subtle cases and contexts, such as the low-latency DPB bumping or
|
|
|
|
* mapping the baseline profile as the constrained-baseline profile,
|
|
|
|
* etc.
|
|
|
|
*
|
|
|
|
* Since: 1.20
|
|
|
|
*/
|
|
|
|
g_object_class_install_property (object_class, PROP_COMPLIANCE,
|
|
|
|
g_param_spec_enum ("compliance", "Decoder Compliance",
|
|
|
|
"The decoder's behavior in compliance with the h264 spec.",
|
|
|
|
GST_TYPE_H264_DECODER_COMPLIANCE, GST_H264_DECODER_COMPLIANCE_AUTO,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT));
|
2019-12-26 05:24:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_h264_decoder_init (GstH264Decoder * self)
|
|
|
|
{
|
2020-05-15 18:56:27 +00:00
|
|
|
GstH264DecoderPrivate *priv;
|
|
|
|
|
2020-02-05 12:27:23 +00:00
|
|
|
gst_video_decoder_set_packetized (GST_VIDEO_DECODER (self), TRUE);
|
2019-12-26 05:24:46 +00:00
|
|
|
|
2020-05-15 18:56:27 +00:00
|
|
|
self->priv = priv = gst_h264_decoder_get_instance_private (self);
|
|
|
|
|
2021-07-06 05:38:16 +00:00
|
|
|
priv->last_output_poc = G_MININT32;
|
|
|
|
|
2020-04-27 14:53:45 +00:00
|
|
|
priv->ref_pic_list_p0 = g_array_sized_new (FALSE, TRUE,
|
|
|
|
sizeof (GstH264Picture *), 32);
|
|
|
|
g_array_set_clear_func (priv->ref_pic_list_p0,
|
|
|
|
(GDestroyNotify) gst_h264_picture_clear);
|
|
|
|
|
|
|
|
priv->ref_pic_list_b0 = g_array_sized_new (FALSE, TRUE,
|
|
|
|
sizeof (GstH264Picture *), 32);
|
|
|
|
g_array_set_clear_func (priv->ref_pic_list_b0,
|
|
|
|
(GDestroyNotify) gst_h264_picture_clear);
|
|
|
|
|
|
|
|
priv->ref_pic_list_b1 = g_array_sized_new (FALSE, TRUE,
|
|
|
|
sizeof (GstH264Picture *), 32);
|
|
|
|
g_array_set_clear_func (priv->ref_pic_list_b1,
|
|
|
|
(GDestroyNotify) gst_h264_picture_clear);
|
|
|
|
|
2020-11-14 15:20:54 +00:00
|
|
|
priv->ref_frame_list_0_short_term = g_array_sized_new (FALSE, TRUE,
|
|
|
|
sizeof (GstH264Picture *), 32);
|
|
|
|
g_array_set_clear_func (priv->ref_frame_list_0_short_term,
|
|
|
|
(GDestroyNotify) gst_h264_picture_clear);
|
|
|
|
|
|
|
|
priv->ref_frame_list_1_short_term = g_array_sized_new (FALSE, TRUE,
|
|
|
|
sizeof (GstH264Picture *), 32);
|
|
|
|
g_array_set_clear_func (priv->ref_frame_list_1_short_term,
|
|
|
|
(GDestroyNotify) gst_h264_picture_clear);
|
|
|
|
|
|
|
|
priv->ref_frame_list_long_term = g_array_sized_new (FALSE, TRUE,
|
|
|
|
sizeof (GstH264Picture *), 32);
|
|
|
|
g_array_set_clear_func (priv->ref_frame_list_long_term,
|
|
|
|
(GDestroyNotify) gst_h264_picture_clear);
|
|
|
|
|
2020-05-03 15:30:34 +00:00
|
|
|
priv->ref_pic_list0 = g_array_sized_new (FALSE, TRUE,
|
|
|
|
sizeof (GstH264Picture *), 32);
|
|
|
|
priv->ref_pic_list1 = g_array_sized_new (FALSE, TRUE,
|
|
|
|
sizeof (GstH264Picture *), 32);
|
2020-12-29 10:54:35 +00:00
|
|
|
|
|
|
|
priv->output_queue =
|
|
|
|
gst_queue_array_new_for_struct (sizeof (GstH264DecoderOutputFrame), 1);
|
|
|
|
gst_queue_array_set_clear_func (priv->output_queue,
|
|
|
|
(GDestroyNotify) gst_h264_decoder_clear_output_frame);
|
2020-05-15 18:56:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_h264_decoder_finalize (GObject * object)
|
|
|
|
{
|
|
|
|
GstH264Decoder *self = GST_H264_DECODER (object);
|
|
|
|
GstH264DecoderPrivate *priv = self->priv;
|
2020-04-27 14:53:45 +00:00
|
|
|
|
|
|
|
g_array_unref (priv->ref_pic_list_p0);
|
|
|
|
g_array_unref (priv->ref_pic_list_b0);
|
|
|
|
g_array_unref (priv->ref_pic_list_b1);
|
2020-11-14 15:20:54 +00:00
|
|
|
g_array_unref (priv->ref_frame_list_0_short_term);
|
|
|
|
g_array_unref (priv->ref_frame_list_1_short_term);
|
|
|
|
g_array_unref (priv->ref_frame_list_long_term);
|
2020-05-03 15:30:34 +00:00
|
|
|
g_array_unref (priv->ref_pic_list0);
|
|
|
|
g_array_unref (priv->ref_pic_list1);
|
2020-12-29 10:54:35 +00:00
|
|
|
gst_queue_array_free (priv->output_queue);
|
2020-05-16 09:14:58 +00:00
|
|
|
|
|
|
|
G_OBJECT_CLASS (parent_class)->finalize (object);
|
2019-12-26 05:24:46 +00:00
|
|
|
}
|
|
|
|
|
2021-01-10 13:01:27 +00:00
|
|
|
static void
|
|
|
|
gst_h264_decoder_reset (GstH264Decoder * self)
|
|
|
|
{
|
|
|
|
GstH264DecoderPrivate *priv = self->priv;
|
|
|
|
|
|
|
|
gst_clear_buffer (&priv->codec_data);
|
|
|
|
g_clear_pointer (&self->input_state, gst_video_codec_state_unref);
|
|
|
|
g_clear_pointer (&priv->parser, gst_h264_nal_parser_free);
|
|
|
|
g_clear_pointer (&priv->dpb, gst_h264_dpb_free);
|
2021-07-27 02:51:03 +00:00
|
|
|
gst_h264_picture_clear (&priv->last_field);
|
2021-01-10 13:01:27 +00:00
|
|
|
|
2021-07-28 15:19:15 +00:00
|
|
|
priv->profile_idc = 0;
|
2021-01-10 13:01:27 +00:00
|
|
|
priv->width = 0;
|
|
|
|
priv->height = 0;
|
|
|
|
priv->nal_length_size = 4;
|
|
|
|
}
|
|
|
|
|
2019-12-26 05:24:46 +00:00
|
|
|
static gboolean
|
|
|
|
gst_h264_decoder_start (GstVideoDecoder * decoder)
|
|
|
|
{
|
|
|
|
GstH264Decoder *self = GST_H264_DECODER (decoder);
|
|
|
|
GstH264DecoderPrivate *priv = self->priv;
|
|
|
|
|
2021-01-10 13:01:27 +00:00
|
|
|
gst_h264_decoder_reset (self);
|
|
|
|
|
2019-12-26 05:24:46 +00:00
|
|
|
priv->parser = gst_h264_nal_parser_new ();
|
|
|
|
priv->dpb = gst_h264_dpb_new ();
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_h264_decoder_stop (GstVideoDecoder * decoder)
|
|
|
|
{
|
|
|
|
GstH264Decoder *self = GST_H264_DECODER (decoder);
|
|
|
|
|
2021-01-10 13:01:27 +00:00
|
|
|
gst_h264_decoder_reset (self);
|
2019-12-26 05:24:46 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2020-12-29 10:54:35 +00:00
|
|
|
static void
|
|
|
|
gst_h264_decoder_clear_output_frame (GstH264DecoderOutputFrame * output_frame)
|
|
|
|
{
|
|
|
|
if (!output_frame)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (output_frame->frame) {
|
|
|
|
gst_video_decoder_release_frame (GST_VIDEO_DECODER (output_frame->self),
|
|
|
|
output_frame->frame);
|
|
|
|
output_frame->frame = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_h264_picture_clear (&output_frame->picture);
|
|
|
|
}
|
|
|
|
|
2019-12-26 05:24:46 +00:00
|
|
|
static void
|
2020-11-01 15:08:04 +00:00
|
|
|
gst_h264_decoder_clear_dpb (GstH264Decoder * self, gboolean flush)
|
2019-12-26 05:24:46 +00:00
|
|
|
{
|
2020-11-01 15:08:04 +00:00
|
|
|
GstVideoDecoder *decoder = GST_VIDEO_DECODER (self);
|
2019-12-26 05:24:46 +00:00
|
|
|
GstH264DecoderPrivate *priv = self->priv;
|
2020-11-01 15:08:04 +00:00
|
|
|
GstH264Picture *picture;
|
|
|
|
|
|
|
|
/* If we are not flushing now, videodecoder baseclass will hold
|
|
|
|
* GstVideoCodecFrame. Release frames manually */
|
|
|
|
if (!flush) {
|
|
|
|
while ((picture = gst_h264_dpb_bump (priv->dpb, TRUE)) != NULL) {
|
|
|
|
GstVideoCodecFrame *frame = gst_video_decoder_get_frame (decoder,
|
|
|
|
picture->system_frame_number);
|
|
|
|
|
|
|
|
if (frame)
|
|
|
|
gst_video_decoder_release_frame (decoder, frame);
|
|
|
|
gst_h264_picture_unref (picture);
|
|
|
|
}
|
|
|
|
}
|
2019-12-26 05:24:46 +00:00
|
|
|
|
2020-12-29 10:54:35 +00:00
|
|
|
gst_queue_array_clear (priv->output_queue);
|
2020-04-27 14:53:45 +00:00
|
|
|
gst_h264_decoder_clear_ref_pic_lists (self);
|
2021-07-27 02:51:03 +00:00
|
|
|
gst_h264_picture_clear (&priv->last_field);
|
2019-12-26 05:24:46 +00:00
|
|
|
gst_h264_dpb_clear (priv->dpb);
|
2021-07-06 05:38:16 +00:00
|
|
|
priv->last_output_poc = G_MININT32;
|
2019-12-26 05:24:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_h264_decoder_flush (GstVideoDecoder * decoder)
|
|
|
|
{
|
|
|
|
GstH264Decoder *self = GST_H264_DECODER (decoder);
|
|
|
|
|
2020-11-01 15:08:04 +00:00
|
|
|
gst_h264_decoder_clear_dpb (self, TRUE);
|
2019-12-26 05:24:46 +00:00
|
|
|
|
2020-02-05 12:27:23 +00:00
|
|
|
return TRUE;
|
2019-12-26 05:24:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static GstFlowReturn
|
|
|
|
gst_h264_decoder_drain (GstVideoDecoder * decoder)
|
|
|
|
{
|
|
|
|
GstH264Decoder *self = GST_H264_DECODER (decoder);
|
|
|
|
|
2020-07-20 10:13:29 +00:00
|
|
|
/* dpb will be cleared by this method */
|
2021-09-21 13:21:51 +00:00
|
|
|
return gst_h264_decoder_drain_internal (self);
|
2019-12-26 05:24:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static GstFlowReturn
|
|
|
|
gst_h264_decoder_finish (GstVideoDecoder * decoder)
|
|
|
|
{
|
|
|
|
return gst_h264_decoder_drain (decoder);
|
|
|
|
}
|
|
|
|
|
2020-01-30 11:04:58 +00:00
|
|
|
static GstFlowReturn
|
|
|
|
gst_h264_decoder_handle_frame (GstVideoDecoder * decoder,
|
|
|
|
GstVideoCodecFrame * frame)
|
|
|
|
{
|
|
|
|
GstH264Decoder *self = GST_H264_DECODER (decoder);
|
|
|
|
GstH264DecoderPrivate *priv = self->priv;
|
|
|
|
GstBuffer *in_buf = frame->input_buffer;
|
2020-02-05 12:27:23 +00:00
|
|
|
GstH264NalUnit nalu;
|
|
|
|
GstH264ParserResult pres;
|
|
|
|
GstMapInfo map;
|
2021-09-21 13:21:51 +00:00
|
|
|
GstFlowReturn decode_ret = GST_FLOW_OK;
|
2020-01-30 11:04:58 +00:00
|
|
|
|
|
|
|
GST_LOG_OBJECT (self,
|
|
|
|
"handle frame, PTS: %" GST_TIME_FORMAT ", DTS: %"
|
|
|
|
GST_TIME_FORMAT, GST_TIME_ARGS (GST_BUFFER_PTS (in_buf)),
|
|
|
|
GST_TIME_ARGS (GST_BUFFER_DTS (in_buf)));
|
|
|
|
|
2020-02-05 12:27:23 +00:00
|
|
|
priv->current_frame = frame;
|
2020-01-30 11:04:58 +00:00
|
|
|
|
2020-02-05 12:27:23 +00:00
|
|
|
gst_buffer_map (in_buf, &map, GST_MAP_READ);
|
|
|
|
if (priv->in_format == GST_H264_DECODER_FORMAT_AVC) {
|
|
|
|
pres = gst_h264_parser_identify_nalu_avc (priv->parser,
|
|
|
|
map.data, 0, map.size, priv->nal_length_size, &nalu);
|
|
|
|
|
2021-09-21 13:21:51 +00:00
|
|
|
while (pres == GST_H264_PARSER_OK && decode_ret == GST_FLOW_OK) {
|
2020-11-04 09:47:30 +00:00
|
|
|
decode_ret = gst_h264_decoder_decode_nal (self, &nalu);
|
2020-02-05 12:27:23 +00:00
|
|
|
|
|
|
|
pres = gst_h264_parser_identify_nalu_avc (priv->parser,
|
|
|
|
map.data, nalu.offset + nalu.size, map.size, priv->nal_length_size,
|
|
|
|
&nalu);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
pres = gst_h264_parser_identify_nalu (priv->parser,
|
|
|
|
map.data, 0, map.size, &nalu);
|
|
|
|
|
|
|
|
if (pres == GST_H264_PARSER_NO_NAL_END)
|
|
|
|
pres = GST_H264_PARSER_OK;
|
|
|
|
|
2021-09-21 13:21:51 +00:00
|
|
|
while (pres == GST_H264_PARSER_OK && decode_ret == GST_FLOW_OK) {
|
2020-11-04 09:47:30 +00:00
|
|
|
decode_ret = gst_h264_decoder_decode_nal (self, &nalu);
|
2020-02-05 12:27:23 +00:00
|
|
|
|
|
|
|
pres = gst_h264_parser_identify_nalu (priv->parser,
|
|
|
|
map.data, nalu.offset + nalu.size, map.size, &nalu);
|
|
|
|
|
|
|
|
if (pres == GST_H264_PARSER_NO_NAL_END)
|
|
|
|
pres = GST_H264_PARSER_OK;
|
|
|
|
}
|
2020-01-30 11:04:58 +00:00
|
|
|
}
|
|
|
|
|
2020-02-05 12:27:23 +00:00
|
|
|
gst_buffer_unmap (in_buf, &map);
|
|
|
|
|
2021-09-21 13:21:51 +00:00
|
|
|
if (decode_ret != GST_FLOW_OK) {
|
2021-10-06 13:06:44 +00:00
|
|
|
if (decode_ret == GST_FLOW_ERROR) {
|
|
|
|
GST_VIDEO_DECODER_ERROR (self, 1, STREAM, DECODE,
|
|
|
|
("Failed to decode data"), (NULL), decode_ret);
|
|
|
|
}
|
2020-02-05 12:27:23 +00:00
|
|
|
|
2021-10-06 13:06:44 +00:00
|
|
|
gst_video_decoder_drop_frame (decoder, frame);
|
2020-02-05 12:27:23 +00:00
|
|
|
gst_h264_picture_clear (&priv->current_picture);
|
2020-07-20 11:54:26 +00:00
|
|
|
priv->current_frame = NULL;
|
2020-02-05 12:27:23 +00:00
|
|
|
|
2021-09-21 13:21:51 +00:00
|
|
|
return decode_ret;
|
2020-02-05 12:27:23 +00:00
|
|
|
}
|
2020-01-30 11:04:58 +00:00
|
|
|
|
2021-09-21 13:21:51 +00:00
|
|
|
gst_h264_decoder_finish_current_picture (self, &decode_ret);
|
2020-01-30 11:04:58 +00:00
|
|
|
gst_video_codec_frame_unref (frame);
|
2020-07-20 11:54:26 +00:00
|
|
|
priv->current_frame = NULL;
|
2020-01-30 11:04:58 +00:00
|
|
|
|
2021-10-06 13:06:44 +00:00
|
|
|
if (decode_ret == GST_FLOW_ERROR) {
|
2021-09-21 13:21:51 +00:00
|
|
|
GST_VIDEO_DECODER_ERROR (self, 1, STREAM, DECODE,
|
|
|
|
("Failed to decode data"), (NULL), decode_ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
return decode_ret;
|
2020-01-30 11:04:58 +00:00
|
|
|
}
|
|
|
|
|
2021-09-21 13:21:51 +00:00
|
|
|
static GstFlowReturn
|
2019-12-26 05:24:46 +00:00
|
|
|
gst_h264_decoder_parse_sps (GstH264Decoder * self, GstH264NalUnit * nalu)
|
|
|
|
{
|
|
|
|
GstH264DecoderPrivate *priv = self->priv;
|
|
|
|
GstH264SPS sps;
|
|
|
|
GstH264ParserResult pres;
|
2020-03-23 05:40:52 +00:00
|
|
|
gboolean ret;
|
2019-12-26 05:24:46 +00:00
|
|
|
|
2020-03-23 05:40:52 +00:00
|
|
|
pres = gst_h264_parse_sps (nalu, &sps);
|
2019-12-26 05:24:46 +00:00
|
|
|
if (pres != GST_H264_PARSER_OK) {
|
|
|
|
GST_WARNING_OBJECT (self, "Failed to parse SPS, result %d", pres);
|
2021-09-21 13:21:51 +00:00
|
|
|
return GST_FLOW_ERROR;
|
2019-12-26 05:24:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GST_LOG_OBJECT (self, "SPS parsed");
|
|
|
|
|
2020-03-23 05:40:52 +00:00
|
|
|
ret = gst_h264_decoder_process_sps (self, &sps);
|
2021-09-21 13:21:51 +00:00
|
|
|
if (ret != GST_FLOW_OK) {
|
2020-03-23 05:40:52 +00:00
|
|
|
GST_WARNING_OBJECT (self, "Failed to process SPS");
|
|
|
|
} else if (gst_h264_parser_update_sps (priv->parser,
|
|
|
|
&sps) != GST_H264_PARSER_OK) {
|
|
|
|
GST_WARNING_OBJECT (self, "Failed to update SPS");
|
2021-09-21 13:21:51 +00:00
|
|
|
ret = GST_FLOW_ERROR;
|
2020-03-23 05:40:52 +00:00
|
|
|
}
|
2019-12-26 05:24:46 +00:00
|
|
|
|
|
|
|
gst_h264_sps_clear (&sps);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-09-21 13:21:51 +00:00
|
|
|
static GstFlowReturn
|
2019-12-26 05:24:46 +00:00
|
|
|
gst_h264_decoder_parse_pps (GstH264Decoder * self, GstH264NalUnit * nalu)
|
|
|
|
{
|
|
|
|
GstH264DecoderPrivate *priv = self->priv;
|
|
|
|
GstH264PPS pps;
|
|
|
|
GstH264ParserResult pres;
|
2021-09-21 13:21:51 +00:00
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
2019-12-26 05:24:46 +00:00
|
|
|
|
2020-05-22 17:33:24 +00:00
|
|
|
pres = gst_h264_parse_pps (priv->parser, nalu, &pps);
|
2019-12-26 05:24:46 +00:00
|
|
|
if (pres != GST_H264_PARSER_OK) {
|
|
|
|
GST_WARNING_OBJECT (self, "Failed to parse PPS, result %d", pres);
|
2021-09-21 13:21:51 +00:00
|
|
|
return GST_FLOW_ERROR;
|
2019-12-26 05:24:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GST_LOG_OBJECT (self, "PPS parsed");
|
2020-05-22 17:33:24 +00:00
|
|
|
|
|
|
|
if (pps.num_slice_groups_minus1 > 0) {
|
|
|
|
GST_FIXME_OBJECT (self, "FMO is not supported");
|
2021-09-21 13:21:51 +00:00
|
|
|
ret = GST_FLOW_ERROR;
|
2020-05-22 17:33:24 +00:00
|
|
|
} else if (gst_h264_parser_update_pps (priv->parser, &pps)
|
|
|
|
!= GST_H264_PARSER_OK) {
|
|
|
|
GST_WARNING_OBJECT (self, "Failed to update PPS");
|
2021-09-21 13:21:51 +00:00
|
|
|
ret = GST_FLOW_ERROR;
|
2020-05-22 17:33:24 +00:00
|
|
|
}
|
|
|
|
|
2019-12-26 05:24:46 +00:00
|
|
|
gst_h264_pps_clear (&pps);
|
|
|
|
|
2020-05-22 17:33:24 +00:00
|
|
|
return ret;
|
2019-12-26 05:24:46 +00:00
|
|
|
}
|
|
|
|
|
2021-09-21 13:21:51 +00:00
|
|
|
static GstFlowReturn
|
2019-12-26 05:24:46 +00:00
|
|
|
gst_h264_decoder_parse_codec_data (GstH264Decoder * self, const guint8 * data,
|
|
|
|
gsize size)
|
|
|
|
{
|
|
|
|
GstH264DecoderPrivate *priv = self->priv;
|
|
|
|
guint num_sps, num_pps;
|
|
|
|
guint off;
|
|
|
|
gint i;
|
|
|
|
GstH264ParserResult pres;
|
|
|
|
GstH264NalUnit nalu;
|
2021-09-21 13:21:51 +00:00
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
2019-12-26 05:24:46 +00:00
|
|
|
#ifndef GST_DISABLE_GST_DEBUG
|
|
|
|
guint profile;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* parse the avcC data */
|
|
|
|
if (size < 7) { /* when numSPS==0 and numPPS==0, length is 7 bytes */
|
2021-09-21 13:21:51 +00:00
|
|
|
return GST_FLOW_ERROR;
|
2019-12-26 05:24:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* parse the version, this must be 1 */
|
|
|
|
if (data[0] != 1) {
|
2021-09-21 13:21:51 +00:00
|
|
|
return GST_FLOW_ERROR;
|
2019-12-26 05:24:46 +00:00
|
|
|
}
|
|
|
|
#ifndef GST_DISABLE_GST_DEBUG
|
|
|
|
/* AVCProfileIndication */
|
|
|
|
/* profile_compat */
|
|
|
|
/* AVCLevelIndication */
|
|
|
|
profile = (data[1] << 16) | (data[2] << 8) | data[3];
|
|
|
|
GST_DEBUG_OBJECT (self, "profile %06x", profile);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* 6 bits reserved | 2 bits lengthSizeMinusOne */
|
|
|
|
/* this is the number of bytes in front of the NAL units to mark their
|
|
|
|
* length */
|
|
|
|
priv->nal_length_size = (data[4] & 0x03) + 1;
|
|
|
|
GST_DEBUG_OBJECT (self, "nal length size %u", priv->nal_length_size);
|
|
|
|
|
|
|
|
num_sps = data[5] & 0x1f;
|
|
|
|
off = 6;
|
|
|
|
for (i = 0; i < num_sps; i++) {
|
|
|
|
pres = gst_h264_parser_identify_nalu_avc (priv->parser,
|
|
|
|
data, off, size, 2, &nalu);
|
|
|
|
if (pres != GST_H264_PARSER_OK) {
|
|
|
|
GST_WARNING_OBJECT (self, "Failed to identify SPS nalu");
|
2021-09-21 13:21:51 +00:00
|
|
|
return GST_FLOW_ERROR;
|
2019-12-26 05:24:46 +00:00
|
|
|
}
|
|
|
|
|
2021-09-21 13:21:51 +00:00
|
|
|
ret = gst_h264_decoder_parse_sps (self, &nalu);
|
|
|
|
if (ret != GST_FLOW_OK) {
|
2020-03-23 05:40:52 +00:00
|
|
|
GST_WARNING_OBJECT (self, "Failed to parse SPS");
|
2021-09-21 13:21:51 +00:00
|
|
|
return ret;
|
2020-03-23 05:40:52 +00:00
|
|
|
}
|
2019-12-26 05:24:46 +00:00
|
|
|
off = nalu.offset + nalu.size;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (off >= size) {
|
|
|
|
GST_WARNING_OBJECT (self, "Too small avcC");
|
2021-09-21 13:21:51 +00:00
|
|
|
return GST_FLOW_ERROR;
|
2019-12-26 05:24:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
num_pps = data[off];
|
|
|
|
off++;
|
|
|
|
|
|
|
|
for (i = 0; i < num_pps; i++) {
|
|
|
|
pres = gst_h264_parser_identify_nalu_avc (priv->parser,
|
|
|
|
data, off, size, 2, &nalu);
|
|
|
|
if (pres != GST_H264_PARSER_OK) {
|
|
|
|
GST_WARNING_OBJECT (self, "Failed to identify PPS nalu");
|
2021-09-21 13:21:51 +00:00
|
|
|
return GST_FLOW_ERROR;
|
2019-12-26 05:24:46 +00:00
|
|
|
}
|
|
|
|
|
2021-09-21 13:21:51 +00:00
|
|
|
ret = gst_h264_decoder_parse_pps (self, &nalu);
|
|
|
|
if (ret != GST_FLOW_OK) {
|
2020-03-23 05:40:52 +00:00
|
|
|
GST_WARNING_OBJECT (self, "Failed to parse PPS");
|
2021-09-21 13:21:51 +00:00
|
|
|
return ret;
|
2020-03-23 05:40:52 +00:00
|
|
|
}
|
2019-12-26 05:24:46 +00:00
|
|
|
off = nalu.offset + nalu.size;
|
|
|
|
}
|
|
|
|
|
2021-09-21 13:21:51 +00:00
|
|
|
return GST_FLOW_OK;
|
2019-12-26 05:24:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_h264_decoder_preprocess_slice (GstH264Decoder * self, GstH264Slice * slice)
|
|
|
|
{
|
|
|
|
GstH264DecoderPrivate *priv = self->priv;
|
|
|
|
|
2020-02-05 12:27:23 +00:00
|
|
|
if (!priv->current_picture) {
|
2019-12-26 05:24:46 +00:00
|
|
|
if (slice->header.first_mb_in_slice != 0) {
|
|
|
|
GST_ERROR_OBJECT (self, "Invalid stream, first_mb_in_slice %d",
|
|
|
|
slice->header.first_mb_in_slice);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-11-04 18:47:35 +00:00
|
|
|
gst_h264_decoder_update_pic_nums (GstH264Decoder * self,
|
|
|
|
GstH264Picture * current_picture, gint frame_num)
|
2019-12-26 05:24:46 +00:00
|
|
|
{
|
|
|
|
GstH264DecoderPrivate *priv = self->priv;
|
|
|
|
GArray *dpb = gst_h264_dpb_get_pictures_all (priv->dpb);
|
|
|
|
gint i;
|
|
|
|
|
|
|
|
for (i = 0; i < dpb->len; i++) {
|
|
|
|
GstH264Picture *picture = g_array_index (dpb, GstH264Picture *, i);
|
|
|
|
|
2020-11-05 16:45:36 +00:00
|
|
|
if (!GST_H264_PICTURE_IS_REF (picture))
|
2019-12-26 05:24:46 +00:00
|
|
|
continue;
|
|
|
|
|
2020-11-05 16:45:36 +00:00
|
|
|
if (GST_H264_PICTURE_IS_LONG_TERM_REF (picture)) {
|
2020-11-15 15:27:28 +00:00
|
|
|
if (GST_H264_PICTURE_IS_FRAME (current_picture))
|
2020-11-09 16:28:03 +00:00
|
|
|
picture->long_term_pic_num = picture->long_term_frame_idx;
|
|
|
|
else if (current_picture->field == picture->field)
|
|
|
|
picture->long_term_pic_num = 2 * picture->long_term_frame_idx + 1;
|
|
|
|
else
|
|
|
|
picture->long_term_pic_num = 2 * picture->long_term_frame_idx;
|
2019-12-26 05:24:46 +00:00
|
|
|
} else {
|
|
|
|
if (picture->frame_num > frame_num)
|
|
|
|
picture->frame_num_wrap = picture->frame_num - priv->max_frame_num;
|
|
|
|
else
|
|
|
|
picture->frame_num_wrap = picture->frame_num;
|
|
|
|
|
2020-11-15 15:27:28 +00:00
|
|
|
if (GST_H264_PICTURE_IS_FRAME (current_picture))
|
2020-11-09 16:28:03 +00:00
|
|
|
picture->pic_num = picture->frame_num_wrap;
|
|
|
|
else if (picture->field == current_picture->field)
|
|
|
|
picture->pic_num = 2 * picture->frame_num_wrap + 1;
|
|
|
|
else
|
|
|
|
picture->pic_num = 2 * picture->frame_num_wrap;
|
2019-12-26 05:24:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
g_array_unref (dpb);
|
|
|
|
}
|
|
|
|
|
2020-11-17 09:59:35 +00:00
|
|
|
static GstH264Picture *
|
|
|
|
gst_h264_decoder_split_frame (GstH264Decoder * self, GstH264Picture * picture)
|
|
|
|
{
|
|
|
|
GstH264Picture *other_field;
|
|
|
|
|
|
|
|
g_assert (GST_H264_PICTURE_IS_FRAME (picture));
|
|
|
|
|
|
|
|
other_field = gst_h264_decoder_new_field_picture (self, picture);
|
|
|
|
if (!other_field) {
|
|
|
|
GST_WARNING_OBJECT (self,
|
|
|
|
"Couldn't split frame into complementary field pair");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_LOG_OBJECT (self, "Split picture %p, poc %d, frame num %d",
|
|
|
|
picture, picture->pic_order_cnt, picture->frame_num);
|
|
|
|
|
|
|
|
/* FIXME: enhance TFF decision by using picture timing SEI */
|
|
|
|
if (picture->top_field_order_cnt < picture->bottom_field_order_cnt) {
|
|
|
|
picture->field = GST_H264_PICTURE_FIELD_TOP_FIELD;
|
|
|
|
picture->pic_order_cnt = picture->top_field_order_cnt;
|
|
|
|
|
|
|
|
other_field->field = GST_H264_PICTURE_FIELD_BOTTOM_FIELD;
|
|
|
|
other_field->pic_order_cnt = picture->bottom_field_order_cnt;
|
|
|
|
} else {
|
|
|
|
picture->field = GST_H264_PICTURE_FIELD_BOTTOM_FIELD;
|
|
|
|
picture->pic_order_cnt = picture->bottom_field_order_cnt;
|
|
|
|
|
|
|
|
other_field->field = GST_H264_PICTURE_FIELD_TOP_FIELD;
|
|
|
|
other_field->pic_order_cnt = picture->top_field_order_cnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
other_field->top_field_order_cnt = picture->top_field_order_cnt;
|
|
|
|
other_field->bottom_field_order_cnt = picture->bottom_field_order_cnt;
|
|
|
|
other_field->frame_num = picture->frame_num;
|
|
|
|
other_field->ref = picture->ref;
|
|
|
|
other_field->nonexisting = picture->nonexisting;
|
2021-01-10 14:11:01 +00:00
|
|
|
other_field->system_frame_number = picture->system_frame_number;
|
2020-11-17 09:59:35 +00:00
|
|
|
|
|
|
|
return other_field;
|
|
|
|
}
|
|
|
|
|
2021-09-21 13:21:51 +00:00
|
|
|
static void
|
|
|
|
output_picture_directly (GstH264Decoder * self, GstH264Picture * picture,
|
|
|
|
GstFlowReturn * ret)
|
2021-07-27 02:51:03 +00:00
|
|
|
{
|
|
|
|
GstH264DecoderPrivate *priv = self->priv;
|
|
|
|
GstH264Picture *out_pic = NULL;
|
2021-09-21 13:21:51 +00:00
|
|
|
GstFlowReturn flow_ret = GST_FLOW_OK;
|
|
|
|
|
|
|
|
g_assert (ret != NULL);
|
2021-07-27 02:51:03 +00:00
|
|
|
|
2021-07-29 13:30:32 +00:00
|
|
|
if (GST_H264_PICTURE_IS_FRAME (picture)) {
|
2021-07-27 02:51:03 +00:00
|
|
|
g_assert (priv->last_field == NULL);
|
|
|
|
out_pic = g_steal_pointer (&picture);
|
|
|
|
goto output;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (priv->last_field == NULL) {
|
|
|
|
if (picture->second_field) {
|
|
|
|
GST_WARNING ("Set the last output %p poc:%d, without first field",
|
|
|
|
picture, picture->pic_order_cnt);
|
|
|
|
|
2021-09-21 13:21:51 +00:00
|
|
|
flow_ret = GST_FLOW_ERROR;
|
2021-07-27 02:51:03 +00:00
|
|
|
goto output;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Just cache the first field. */
|
|
|
|
priv->last_field = g_steal_pointer (&picture);
|
|
|
|
} else {
|
|
|
|
if (!picture->second_field || !picture->other_field
|
|
|
|
|| picture->other_field != priv->last_field) {
|
|
|
|
GST_WARNING ("The last field %p poc:%d is not the pair of the "
|
|
|
|
"current field %p poc:%d",
|
|
|
|
priv->last_field, priv->last_field->pic_order_cnt,
|
|
|
|
picture, picture->pic_order_cnt);
|
|
|
|
|
|
|
|
gst_h264_picture_clear (&priv->last_field);
|
2021-09-21 13:21:51 +00:00
|
|
|
flow_ret = GST_FLOW_ERROR;
|
2021-07-27 02:51:03 +00:00
|
|
|
goto output;
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_TRACE ("Pair the last field %p poc:%d and the current"
|
|
|
|
" field %p poc:%d",
|
|
|
|
priv->last_field, priv->last_field->pic_order_cnt,
|
|
|
|
picture, picture->pic_order_cnt);
|
|
|
|
|
|
|
|
out_pic = priv->last_field;
|
|
|
|
priv->last_field = NULL;
|
|
|
|
/* Link each field. */
|
|
|
|
out_pic->other_field = picture;
|
|
|
|
}
|
|
|
|
|
|
|
|
output:
|
|
|
|
if (out_pic) {
|
|
|
|
gst_h264_dpb_set_last_output (priv->dpb, out_pic);
|
2021-09-21 13:21:51 +00:00
|
|
|
gst_h264_decoder_do_output_picture (self, out_pic, &flow_ret);
|
2021-07-27 02:51:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gst_h264_picture_clear (&picture);
|
|
|
|
|
2021-09-21 13:21:51 +00:00
|
|
|
UPDATE_FLOW_RETURN (ret, flow_ret);
|
2021-07-27 02:51:03 +00:00
|
|
|
}
|
|
|
|
|
2021-07-27 04:16:13 +00:00
|
|
|
static void
|
|
|
|
add_picture_to_dpb (GstH264Decoder * self, GstH264Picture * picture)
|
|
|
|
{
|
|
|
|
GstH264DecoderPrivate *priv = self->priv;
|
|
|
|
|
|
|
|
if (!gst_h264_dpb_get_interlaced (priv->dpb)) {
|
|
|
|
g_assert (priv->last_field == NULL);
|
|
|
|
gst_h264_dpb_add (priv->dpb, picture);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The first field of the last picture may not be able to enter the
|
|
|
|
DPB if it is a non ref, but if the second field enters the DPB, we
|
|
|
|
need to add both of them. */
|
|
|
|
if (priv->last_field && picture->other_field == priv->last_field) {
|
|
|
|
gst_h264_dpb_add (priv->dpb, priv->last_field);
|
|
|
|
priv->last_field = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_h264_dpb_add (priv->dpb, picture);
|
|
|
|
}
|
|
|
|
|
2021-08-31 09:16:05 +00:00
|
|
|
static void
|
|
|
|
_bump_dpb (GstH264Decoder * self, GstH264DpbBumpMode bump_level,
|
2021-09-21 13:21:51 +00:00
|
|
|
GstH264Picture * current_picture, GstFlowReturn * ret)
|
2021-08-31 09:16:05 +00:00
|
|
|
{
|
|
|
|
GstH264DecoderPrivate *priv = self->priv;
|
|
|
|
|
2021-09-21 13:21:51 +00:00
|
|
|
g_assert (ret != NULL);
|
|
|
|
|
2021-08-31 09:16:05 +00:00
|
|
|
while (gst_h264_dpb_needs_bump (priv->dpb, current_picture, bump_level)) {
|
|
|
|
GstH264Picture *to_output;
|
|
|
|
|
|
|
|
to_output = gst_h264_dpb_bump (priv->dpb, FALSE);
|
|
|
|
|
|
|
|
if (!to_output) {
|
|
|
|
GST_WARNING_OBJECT (self, "Bumping is needed but no picture to output");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-09-21 13:21:51 +00:00
|
|
|
gst_h264_decoder_do_output_picture (self, to_output, ret);
|
2021-08-31 09:16:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-21 13:21:51 +00:00
|
|
|
static GstFlowReturn
|
2019-12-26 05:24:46 +00:00
|
|
|
gst_h264_decoder_handle_frame_num_gap (GstH264Decoder * self, gint frame_num)
|
|
|
|
{
|
|
|
|
GstH264DecoderPrivate *priv = self->priv;
|
|
|
|
const GstH264SPS *sps = priv->active_sps;
|
|
|
|
gint unused_short_term_frame_num;
|
|
|
|
|
|
|
|
if (!sps) {
|
|
|
|
GST_ERROR_OBJECT (self, "No active sps");
|
2021-09-21 13:21:51 +00:00
|
|
|
return GST_FLOW_ERROR;
|
2019-12-26 05:24:46 +00:00
|
|
|
}
|
|
|
|
|
2020-11-05 09:27:11 +00:00
|
|
|
if (priv->prev_ref_frame_num == frame_num) {
|
|
|
|
GST_TRACE_OBJECT (self,
|
|
|
|
"frame_num == PrevRefFrameNum (%d), not a gap", frame_num);
|
2021-09-21 13:21:51 +00:00
|
|
|
return GST_FLOW_OK;
|
2020-11-05 09:27:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (((priv->prev_ref_frame_num + 1) % priv->max_frame_num) == frame_num) {
|
|
|
|
GST_TRACE_OBJECT (self,
|
|
|
|
"frame_num == (PrevRefFrameNum + 1) %% MaxFrameNum (%d), not a gap",
|
|
|
|
frame_num);
|
2021-09-21 13:21:51 +00:00
|
|
|
return GST_FLOW_OK;
|
2020-11-05 09:27:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (gst_h264_dpb_get_size (priv->dpb) == 0) {
|
|
|
|
GST_TRACE_OBJECT (self, "DPB is empty, not a gap");
|
2021-09-21 13:21:51 +00:00
|
|
|
return GST_FLOW_OK;
|
2020-11-05 09:27:11 +00:00
|
|
|
}
|
|
|
|
|
2019-12-26 05:24:46 +00:00
|
|
|
if (!sps->gaps_in_frame_num_value_allowed_flag) {
|
2020-04-30 16:51:10 +00:00
|
|
|
/* This is likely the case where some frames were dropped.
|
|
|
|
* then we need to keep decoding without error out */
|
2020-11-12 10:43:22 +00:00
|
|
|
GST_WARNING_OBJECT (self, "Invalid frame num %d, maybe frame drop",
|
|
|
|
frame_num);
|
|
|
|
|
2021-09-21 13:21:51 +00:00
|
|
|
return GST_FLOW_OK;
|
2019-12-26 05:24:46 +00:00
|
|
|
}
|
|
|
|
|
2020-11-05 09:27:11 +00:00
|
|
|
GST_DEBUG_OBJECT (self, "Handling frame num gap %d -> %d (MaxFrameNum: %d)",
|
|
|
|
priv->prev_ref_frame_num, frame_num, priv->max_frame_num);
|
2019-12-26 05:24:46 +00:00
|
|
|
|
|
|
|
/* 7.4.3/7-23 */
|
|
|
|
unused_short_term_frame_num =
|
|
|
|
(priv->prev_ref_frame_num + 1) % priv->max_frame_num;
|
|
|
|
while (unused_short_term_frame_num != frame_num) {
|
|
|
|
GstH264Picture *picture = gst_h264_picture_new ();
|
2021-09-21 13:21:51 +00:00
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
2019-12-26 05:24:46 +00:00
|
|
|
|
|
|
|
if (!gst_h264_decoder_init_gap_picture (self, picture,
|
|
|
|
unused_short_term_frame_num))
|
2021-09-21 13:21:51 +00:00
|
|
|
return GST_FLOW_ERROR;
|
2019-12-26 05:24:46 +00:00
|
|
|
|
2020-11-04 18:47:35 +00:00
|
|
|
gst_h264_decoder_update_pic_nums (self, picture,
|
|
|
|
unused_short_term_frame_num);
|
2019-12-26 05:24:46 +00:00
|
|
|
|
2020-11-01 15:08:04 +00:00
|
|
|
/* C.2.1 */
|
2020-11-09 16:28:03 +00:00
|
|
|
if (!gst_h264_decoder_sliding_window_picture_marking (self, picture)) {
|
2020-11-01 15:08:04 +00:00
|
|
|
GST_ERROR_OBJECT (self,
|
|
|
|
"Couldn't perform sliding window picture marking");
|
2021-09-21 13:21:51 +00:00
|
|
|
return GST_FLOW_ERROR;
|
2019-12-26 05:24:46 +00:00
|
|
|
}
|
|
|
|
|
2020-11-01 15:08:04 +00:00
|
|
|
gst_h264_dpb_delete_unused (priv->dpb);
|
2020-11-17 09:59:35 +00:00
|
|
|
|
2021-09-21 13:21:51 +00:00
|
|
|
_bump_dpb (self, GST_H264_DPB_BUMP_NORMAL_LATENCY, picture, &ret);
|
|
|
|
if (ret != GST_FLOW_OK)
|
|
|
|
return ret;
|
2020-11-01 15:08:04 +00:00
|
|
|
|
2021-07-11 16:31:54 +00:00
|
|
|
/* the picture is short term ref, add to DPB. */
|
|
|
|
if (gst_h264_dpb_get_interlaced (priv->dpb)) {
|
|
|
|
GstH264Picture *other_field =
|
|
|
|
gst_h264_decoder_split_frame (self, picture);
|
|
|
|
|
2021-07-27 04:16:13 +00:00
|
|
|
add_picture_to_dpb (self, picture);
|
|
|
|
add_picture_to_dpb (self, other_field);
|
2021-07-11 16:31:54 +00:00
|
|
|
} else {
|
2021-07-27 04:16:13 +00:00
|
|
|
add_picture_to_dpb (self, picture);
|
2021-07-11 16:31:54 +00:00
|
|
|
}
|
|
|
|
|
2019-12-26 05:24:46 +00:00
|
|
|
unused_short_term_frame_num++;
|
|
|
|
unused_short_term_frame_num %= priv->max_frame_num;
|
|
|
|
}
|
|
|
|
|
2021-09-21 13:21:51 +00:00
|
|
|
return GST_FLOW_OK;
|
2019-12-26 05:24:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_h264_decoder_init_current_picture (GstH264Decoder * self)
|
|
|
|
{
|
|
|
|
GstH264DecoderPrivate *priv = self->priv;
|
|
|
|
|
|
|
|
if (!gst_h264_decoder_fill_picture_from_slice (self, &priv->current_slice,
|
|
|
|
priv->current_picture)) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!gst_h264_decoder_calculate_poc (self, priv->current_picture))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
/* If the slice header indicates we will have to perform reference marking
|
|
|
|
* process after this picture is decoded, store required data for that
|
|
|
|
* purpose */
|
2021-10-04 17:07:57 +00:00
|
|
|
if (priv->current_slice.header.
|
|
|
|
dec_ref_pic_marking.adaptive_ref_pic_marking_mode_flag) {
|
2019-12-26 05:24:46 +00:00
|
|
|
priv->current_picture->dec_ref_pic_marking =
|
|
|
|
priv->current_slice.header.dec_ref_pic_marking;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2021-09-21 13:21:51 +00:00
|
|
|
static GstFlowReturn
|
2019-12-26 05:24:46 +00:00
|
|
|
gst_h264_decoder_start_current_picture (GstH264Decoder * self)
|
|
|
|
{
|
|
|
|
GstH264DecoderClass *klass;
|
|
|
|
GstH264DecoderPrivate *priv = self->priv;
|
|
|
|
const GstH264SPS *sps;
|
|
|
|
gint frame_num;
|
2021-09-21 13:21:51 +00:00
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
2020-11-01 15:08:04 +00:00
|
|
|
GstH264Picture *current_picture;
|
2019-12-26 05:24:46 +00:00
|
|
|
|
|
|
|
g_assert (priv->current_picture != NULL);
|
|
|
|
g_assert (priv->active_sps != NULL);
|
|
|
|
g_assert (priv->active_pps != NULL);
|
|
|
|
|
|
|
|
sps = priv->active_sps;
|
|
|
|
|
2020-05-12 16:23:15 +00:00
|
|
|
priv->max_frame_num = sps->max_frame_num;
|
2019-12-26 05:24:46 +00:00
|
|
|
frame_num = priv->current_slice.header.frame_num;
|
|
|
|
if (priv->current_slice.nalu.idr_pic_flag)
|
|
|
|
priv->prev_ref_frame_num = 0;
|
|
|
|
|
2021-09-21 13:21:51 +00:00
|
|
|
ret = gst_h264_decoder_handle_frame_num_gap (self, frame_num);
|
|
|
|
if (ret != GST_FLOW_OK)
|
|
|
|
return ret;
|
2019-12-26 05:24:46 +00:00
|
|
|
|
|
|
|
if (!gst_h264_decoder_init_current_picture (self))
|
2021-09-21 13:21:51 +00:00
|
|
|
return GST_FLOW_ERROR;
|
2019-12-26 05:24:46 +00:00
|
|
|
|
2020-11-01 15:08:04 +00:00
|
|
|
current_picture = priv->current_picture;
|
|
|
|
|
|
|
|
/* If the new picture is an IDR, flush DPB */
|
|
|
|
if (current_picture->idr) {
|
|
|
|
if (!current_picture->dec_ref_pic_marking.no_output_of_prior_pics_flag) {
|
2021-09-21 13:21:51 +00:00
|
|
|
ret = gst_h264_decoder_drain_internal (self);
|
|
|
|
if (ret != GST_FLOW_OK)
|
|
|
|
return ret;
|
2020-11-01 15:08:04 +00:00
|
|
|
} else {
|
|
|
|
/* C.4.4 Removal of pictures from the DPB before possible insertion
|
|
|
|
* of the current picture
|
|
|
|
*
|
|
|
|
* If decoded picture is IDR and no_output_of_prior_pics_flag is equal to 1
|
|
|
|
* or is inferred to be equal to 1, all frame buffers in the DPB
|
|
|
|
* are emptied without output of the pictures they contain,
|
|
|
|
* and DPB fullness is set to 0.
|
|
|
|
*/
|
|
|
|
gst_h264_decoder_clear_dpb (self, FALSE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-04 18:47:35 +00:00
|
|
|
gst_h264_decoder_update_pic_nums (self, current_picture, frame_num);
|
2019-12-26 05:24:46 +00:00
|
|
|
|
2020-04-27 14:53:45 +00:00
|
|
|
if (priv->process_ref_pic_lists)
|
2020-11-17 09:39:56 +00:00
|
|
|
gst_h264_decoder_prepare_ref_pic_lists (self, current_picture);
|
2020-04-27 14:53:45 +00:00
|
|
|
|
2019-12-26 05:24:46 +00:00
|
|
|
klass = GST_H264_DECODER_GET_CLASS (self);
|
2021-09-21 13:21:51 +00:00
|
|
|
if (klass->start_picture) {
|
2019-12-26 05:24:46 +00:00
|
|
|
ret = klass->start_picture (self, priv->current_picture,
|
|
|
|
&priv->current_slice, priv->dpb);
|
|
|
|
|
2021-09-21 13:21:51 +00:00
|
|
|
if (ret != GST_FLOW_OK) {
|
|
|
|
GST_WARNING_OBJECT (self, "subclass does not want to start picture");
|
|
|
|
return ret;
|
|
|
|
}
|
2019-12-26 05:24:46 +00:00
|
|
|
}
|
|
|
|
|
2021-09-21 13:21:51 +00:00
|
|
|
return GST_FLOW_OK;
|
2019-12-26 05:24:46 +00:00
|
|
|
}
|
|
|
|
|
2020-11-09 16:28:03 +00:00
|
|
|
static GstH264Picture *
|
|
|
|
gst_h264_decoder_new_field_picture (GstH264Decoder * self,
|
|
|
|
GstH264Picture * picture)
|
|
|
|
{
|
|
|
|
GstH264DecoderClass *klass = GST_H264_DECODER_GET_CLASS (self);
|
|
|
|
GstH264Picture *new_picture;
|
|
|
|
|
|
|
|
if (!klass->new_field_picture) {
|
|
|
|
GST_WARNING_OBJECT (self, "Subclass does not support interlaced stream");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
new_picture = gst_h264_picture_new ();
|
2020-11-17 09:59:35 +00:00
|
|
|
/* don't confuse subclass by non-existing picture */
|
2021-09-21 13:21:51 +00:00
|
|
|
if (!picture->nonexisting) {
|
|
|
|
GstFlowReturn ret;
|
2020-11-09 16:28:03 +00:00
|
|
|
|
2021-09-21 13:21:51 +00:00
|
|
|
ret = klass->new_field_picture (self, picture, new_picture);
|
|
|
|
if (ret != GST_FLOW_OK) {
|
|
|
|
GST_WARNING_OBJECT (self, "Subclass couldn't handle new field picture");
|
|
|
|
gst_h264_picture_unref (new_picture);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2020-11-09 16:28:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
new_picture->other_field = picture;
|
|
|
|
new_picture->second_field = TRUE;
|
|
|
|
|
|
|
|
return new_picture;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_h264_decoder_find_first_field_picture (GstH264Decoder * self,
|
|
|
|
GstH264Slice * slice, GstH264Picture ** first_field)
|
|
|
|
{
|
|
|
|
GstH264DecoderPrivate *priv = self->priv;
|
|
|
|
const GstH264SliceHdr *slice_hdr = &slice->header;
|
2021-07-27 04:51:08 +00:00
|
|
|
GstH264Picture *prev_field;
|
|
|
|
gboolean in_dpb;
|
2020-11-09 16:28:03 +00:00
|
|
|
|
|
|
|
*first_field = NULL;
|
2021-07-27 04:51:08 +00:00
|
|
|
prev_field = NULL;
|
|
|
|
in_dpb = FALSE;
|
|
|
|
if (gst_h264_dpb_get_interlaced (priv->dpb)) {
|
|
|
|
if (priv->last_field) {
|
|
|
|
prev_field = priv->last_field;
|
|
|
|
in_dpb = FALSE;
|
|
|
|
} else if (gst_h264_dpb_get_size (priv->dpb) > 0) {
|
|
|
|
GstH264Picture *prev_picture;
|
|
|
|
GArray *pictures;
|
|
|
|
|
|
|
|
pictures = gst_h264_dpb_get_pictures_all (priv->dpb);
|
|
|
|
prev_picture =
|
|
|
|
g_array_index (pictures, GstH264Picture *, pictures->len - 1);
|
|
|
|
g_array_unref (pictures); /* prev_picture should be held */
|
|
|
|
|
|
|
|
/* Previous picture was a field picture. */
|
|
|
|
if (!GST_H264_PICTURE_IS_FRAME (prev_picture)
|
|
|
|
&& !prev_picture->other_field) {
|
|
|
|
prev_field = prev_picture;
|
|
|
|
in_dpb = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
g_assert (priv->last_field == NULL);
|
|
|
|
}
|
2020-11-09 16:28:03 +00:00
|
|
|
|
|
|
|
/* This is not a field picture */
|
|
|
|
if (!slice_hdr->field_pic_flag) {
|
2021-07-27 04:51:08 +00:00
|
|
|
if (!prev_field)
|
|
|
|
return TRUE;
|
2020-11-09 16:28:03 +00:00
|
|
|
|
2021-07-27 04:51:08 +00:00
|
|
|
GST_WARNING_OBJECT (self, "Previous picture %p (poc %d) is not complete",
|
|
|
|
prev_field, prev_field->pic_order_cnt);
|
|
|
|
goto error;
|
2020-11-09 16:28:03 +00:00
|
|
|
}
|
|
|
|
|
2021-07-27 04:51:08 +00:00
|
|
|
/* OK, this is the first field. */
|
|
|
|
if (!prev_field)
|
2020-11-09 16:28:03 +00:00
|
|
|
return TRUE;
|
|
|
|
|
2021-07-27 04:51:08 +00:00
|
|
|
if (prev_field->frame_num != slice_hdr->frame_num) {
|
|
|
|
GST_WARNING_OBJECT (self, "Previous picture %p (poc %d) is not complete",
|
|
|
|
prev_field, prev_field->pic_order_cnt);
|
|
|
|
goto error;
|
|
|
|
} else {
|
2020-11-09 16:28:03 +00:00
|
|
|
GstH264PictureField current_field = slice_hdr->bottom_field_flag ?
|
|
|
|
GST_H264_PICTURE_FIELD_BOTTOM_FIELD : GST_H264_PICTURE_FIELD_TOP_FIELD;
|
|
|
|
|
2021-07-27 04:51:08 +00:00
|
|
|
if (current_field == prev_field->field) {
|
2020-11-09 16:28:03 +00:00
|
|
|
GST_WARNING_OBJECT (self,
|
|
|
|
"Currnet picture and previous picture have identical field %d",
|
|
|
|
current_field);
|
2021-07-27 04:51:08 +00:00
|
|
|
goto error;
|
2020-11-09 16:28:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-27 04:51:08 +00:00
|
|
|
*first_field = gst_h264_picture_ref (prev_field);
|
2020-11-09 16:28:03 +00:00
|
|
|
return TRUE;
|
2021-07-27 04:51:08 +00:00
|
|
|
|
|
|
|
error:
|
|
|
|
if (!in_dpb) {
|
|
|
|
gst_h264_picture_clear (&priv->last_field);
|
|
|
|
} else {
|
|
|
|
/* FIXME: implement fill gap field picture if it is already in DPB */
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
2020-11-09 16:28:03 +00:00
|
|
|
}
|
|
|
|
|
2021-09-21 13:21:51 +00:00
|
|
|
static GstFlowReturn
|
2020-11-04 09:47:30 +00:00
|
|
|
gst_h264_decoder_parse_slice (GstH264Decoder * self, GstH264NalUnit * nalu)
|
2019-12-26 05:24:46 +00:00
|
|
|
{
|
|
|
|
GstH264DecoderPrivate *priv = self->priv;
|
|
|
|
GstH264ParserResult pres = GST_H264_PARSER_OK;
|
2021-09-21 13:21:51 +00:00
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
2019-12-26 05:24:46 +00:00
|
|
|
|
|
|
|
memset (&priv->current_slice, 0, sizeof (GstH264Slice));
|
|
|
|
|
|
|
|
pres = gst_h264_parser_parse_slice_hdr (priv->parser, nalu,
|
|
|
|
&priv->current_slice.header, TRUE, TRUE);
|
|
|
|
|
|
|
|
if (pres != GST_H264_PARSER_OK) {
|
|
|
|
GST_ERROR_OBJECT (self, "Failed to parse slice header, ret %d", pres);
|
|
|
|
memset (&priv->current_slice, 0, sizeof (GstH264Slice));
|
|
|
|
|
2021-09-21 13:21:51 +00:00
|
|
|
return GST_FLOW_ERROR;
|
2019-12-26 05:24:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
priv->current_slice.nalu = *nalu;
|
|
|
|
|
|
|
|
if (!gst_h264_decoder_preprocess_slice (self, &priv->current_slice))
|
2021-09-21 13:21:51 +00:00
|
|
|
return GST_FLOW_ERROR;
|
2019-12-26 05:24:46 +00:00
|
|
|
|
|
|
|
priv->active_pps = priv->current_slice.header.pps;
|
|
|
|
priv->active_sps = priv->active_pps->sequence;
|
|
|
|
|
2021-01-10 14:11:01 +00:00
|
|
|
/* Check whether field picture boundary within given codec frame.
|
|
|
|
* This might happen in case that upstream sent buffer per frame unit,
|
|
|
|
* not picture unit (i.e., AU unit).
|
|
|
|
* If AU boundary is detected, then finish first field picture we decoded
|
|
|
|
* in this chain, we should finish the current picture and
|
|
|
|
* start new field picture decoding */
|
|
|
|
if (gst_h264_dpb_get_interlaced (priv->dpb) && priv->current_picture &&
|
|
|
|
!GST_H264_PICTURE_IS_FRAME (priv->current_picture) &&
|
|
|
|
!priv->current_picture->second_field) {
|
|
|
|
GstH264PictureField prev_field = priv->current_picture->field;
|
|
|
|
GstH264PictureField cur_field = GST_H264_PICTURE_FIELD_FRAME;
|
|
|
|
if (priv->current_slice.header.field_pic_flag)
|
|
|
|
cur_field = priv->current_slice.header.bottom_field_flag ?
|
|
|
|
GST_H264_PICTURE_FIELD_BOTTOM_FIELD :
|
|
|
|
GST_H264_PICTURE_FIELD_TOP_FIELD;
|
|
|
|
|
|
|
|
if (cur_field != prev_field) {
|
|
|
|
GST_LOG_OBJECT (self,
|
|
|
|
"Found new field picture, finishing the first field picture");
|
2021-09-21 13:21:51 +00:00
|
|
|
gst_h264_decoder_finish_current_picture (self, &ret);
|
2021-01-10 14:11:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-26 05:24:46 +00:00
|
|
|
if (!priv->current_picture) {
|
|
|
|
GstH264DecoderClass *klass = GST_H264_DECODER_GET_CLASS (self);
|
2020-11-09 16:28:03 +00:00
|
|
|
GstH264Picture *picture = NULL;
|
|
|
|
GstH264Picture *first_field = NULL;
|
2021-10-14 18:35:45 +00:00
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
2019-12-26 05:24:46 +00:00
|
|
|
|
2020-07-20 08:45:12 +00:00
|
|
|
g_assert (priv->current_frame);
|
|
|
|
|
2020-11-09 16:28:03 +00:00
|
|
|
if (!gst_h264_decoder_find_first_field_picture (self,
|
|
|
|
&priv->current_slice, &first_field)) {
|
|
|
|
GST_ERROR_OBJECT (self, "Couldn't find or determine first picture");
|
2021-09-21 13:21:51 +00:00
|
|
|
return GST_FLOW_ERROR;
|
2019-12-26 05:24:46 +00:00
|
|
|
}
|
|
|
|
|
2020-11-09 16:28:03 +00:00
|
|
|
if (first_field) {
|
|
|
|
picture = gst_h264_decoder_new_field_picture (self, first_field);
|
2020-11-20 21:26:14 +00:00
|
|
|
gst_h264_picture_unref (first_field);
|
2020-11-09 16:28:03 +00:00
|
|
|
|
|
|
|
if (!picture) {
|
|
|
|
GST_ERROR_OBJECT (self, "Couldn't duplicate the first field picture");
|
2021-09-21 13:21:51 +00:00
|
|
|
return GST_FLOW_ERROR;
|
2020-11-09 16:28:03 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
picture = gst_h264_picture_new ();
|
|
|
|
|
|
|
|
if (klass->new_picture)
|
|
|
|
ret = klass->new_picture (self, priv->current_frame, picture);
|
|
|
|
|
2021-09-21 13:21:51 +00:00
|
|
|
if (ret != GST_FLOW_OK) {
|
|
|
|
GST_WARNING_OBJECT (self, "subclass does not want accept new picture");
|
|
|
|
priv->current_picture = NULL;
|
2020-11-09 16:28:03 +00:00
|
|
|
gst_h264_picture_unref (picture);
|
2021-09-21 13:21:51 +00:00
|
|
|
return ret;
|
2020-11-09 16:28:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This allows accessing the frame from the picture. */
|
|
|
|
picture->system_frame_number = priv->current_frame->system_frame_number;
|
|
|
|
priv->current_picture = picture;
|
|
|
|
|
2021-09-21 13:21:51 +00:00
|
|
|
ret = gst_h264_decoder_start_current_picture (self);
|
|
|
|
if (ret != GST_FLOW_OK) {
|
|
|
|
GST_WARNING_OBJECT (self, "start picture failed");
|
|
|
|
return ret;
|
2019-12-26 05:24:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return gst_h264_decoder_decode_slice (self);
|
|
|
|
}
|
|
|
|
|
2021-09-21 13:21:51 +00:00
|
|
|
static GstFlowReturn
|
2020-11-04 09:47:30 +00:00
|
|
|
gst_h264_decoder_decode_nal (GstH264Decoder * self, GstH264NalUnit * nalu)
|
2019-12-26 05:24:46 +00:00
|
|
|
{
|
2021-09-21 13:21:51 +00:00
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
2019-12-26 05:24:46 +00:00
|
|
|
|
|
|
|
GST_LOG_OBJECT (self, "Parsed nal type: %d, offset %d, size %d",
|
2020-02-05 12:27:23 +00:00
|
|
|
nalu->type, nalu->offset, nalu->size);
|
2019-12-26 05:24:46 +00:00
|
|
|
|
2020-02-05 12:27:23 +00:00
|
|
|
switch (nalu->type) {
|
2019-12-26 05:24:46 +00:00
|
|
|
case GST_H264_NAL_SPS:
|
2020-02-05 12:27:23 +00:00
|
|
|
ret = gst_h264_decoder_parse_sps (self, nalu);
|
2019-12-26 05:24:46 +00:00
|
|
|
break;
|
|
|
|
case GST_H264_NAL_PPS:
|
2020-02-05 12:27:23 +00:00
|
|
|
ret = gst_h264_decoder_parse_pps (self, nalu);
|
2019-12-26 05:24:46 +00:00
|
|
|
break;
|
|
|
|
case GST_H264_NAL_SLICE:
|
|
|
|
case GST_H264_NAL_SLICE_DPA:
|
|
|
|
case GST_H264_NAL_SLICE_DPB:
|
|
|
|
case GST_H264_NAL_SLICE_DPC:
|
|
|
|
case GST_H264_NAL_SLICE_IDR:
|
|
|
|
case GST_H264_NAL_SLICE_EXT:
|
2020-11-04 09:47:30 +00:00
|
|
|
ret = gst_h264_decoder_parse_slice (self, nalu);
|
2019-12-26 05:24:46 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_h264_decoder_format_from_caps (GstH264Decoder * self, GstCaps * caps,
|
|
|
|
GstH264DecoderFormat * format, GstH264DecoderAlign * align)
|
|
|
|
{
|
|
|
|
if (format)
|
|
|
|
*format = GST_H264_DECODER_FORMAT_NONE;
|
|
|
|
|
|
|
|
if (align)
|
|
|
|
*align = GST_H264_DECODER_ALIGN_NONE;
|
|
|
|
|
|
|
|
if (!gst_caps_is_fixed (caps)) {
|
|
|
|
GST_WARNING_OBJECT (self, "Caps wasn't fixed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (self, "parsing caps: %" GST_PTR_FORMAT, caps);
|
|
|
|
|
|
|
|
if (caps && gst_caps_get_size (caps) > 0) {
|
|
|
|
GstStructure *s = gst_caps_get_structure (caps, 0);
|
|
|
|
const gchar *str = NULL;
|
|
|
|
|
|
|
|
if (format) {
|
|
|
|
if ((str = gst_structure_get_string (s, "stream-format"))) {
|
|
|
|
if (strcmp (str, "avc") == 0 || strcmp (str, "avc3") == 0)
|
|
|
|
*format = GST_H264_DECODER_FORMAT_AVC;
|
|
|
|
else if (strcmp (str, "byte-stream") == 0)
|
|
|
|
*format = GST_H264_DECODER_FORMAT_BYTE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (align) {
|
|
|
|
if ((str = gst_structure_get_string (s, "alignment"))) {
|
|
|
|
if (strcmp (str, "au") == 0)
|
|
|
|
*align = GST_H264_DECODER_ALIGN_AU;
|
|
|
|
else if (strcmp (str, "nal") == 0)
|
|
|
|
*align = GST_H264_DECODER_ALIGN_NAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_h264_decoder_set_format (GstVideoDecoder * decoder,
|
|
|
|
GstVideoCodecState * state)
|
|
|
|
{
|
|
|
|
GstH264Decoder *self = GST_H264_DECODER (decoder);
|
|
|
|
GstH264DecoderPrivate *priv = self->priv;
|
2020-04-24 11:56:17 +00:00
|
|
|
GstQuery *query;
|
2019-12-26 05:24:46 +00:00
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (decoder, "Set format");
|
|
|
|
|
|
|
|
if (self->input_state)
|
|
|
|
gst_video_codec_state_unref (self->input_state);
|
|
|
|
|
|
|
|
self->input_state = gst_video_codec_state_ref (state);
|
|
|
|
|
|
|
|
if (state->caps) {
|
|
|
|
GstStructure *str;
|
|
|
|
const GValue *codec_data_value;
|
|
|
|
GstH264DecoderFormat format;
|
|
|
|
GstH264DecoderAlign align;
|
|
|
|
|
|
|
|
gst_h264_decoder_format_from_caps (self, state->caps, &format, &align);
|
|
|
|
|
|
|
|
str = gst_caps_get_structure (state->caps, 0);
|
|
|
|
codec_data_value = gst_structure_get_value (str, "codec_data");
|
|
|
|
|
|
|
|
if (GST_VALUE_HOLDS_BUFFER (codec_data_value)) {
|
|
|
|
gst_buffer_replace (&priv->codec_data,
|
|
|
|
gst_value_get_buffer (codec_data_value));
|
|
|
|
} else {
|
|
|
|
gst_buffer_replace (&priv->codec_data, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (format == GST_H264_DECODER_FORMAT_NONE) {
|
|
|
|
/* codec_data implies avc */
|
|
|
|
if (codec_data_value != NULL) {
|
|
|
|
GST_WARNING_OBJECT (self,
|
|
|
|
"video/x-h264 caps with codec_data but no stream-format=avc");
|
|
|
|
format = GST_H264_DECODER_FORMAT_AVC;
|
|
|
|
} else {
|
|
|
|
/* otherwise assume bytestream input */
|
|
|
|
GST_WARNING_OBJECT (self,
|
|
|
|
"video/x-h264 caps without codec_data or stream-format");
|
|
|
|
format = GST_H264_DECODER_FORMAT_BYTE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (format == GST_H264_DECODER_FORMAT_AVC) {
|
|
|
|
/* AVC requires codec_data, AVC3 might have one and/or SPS/PPS inline */
|
|
|
|
if (codec_data_value == NULL) {
|
|
|
|
/* Try it with size 4 anyway */
|
|
|
|
priv->nal_length_size = 4;
|
|
|
|
GST_WARNING_OBJECT (self,
|
|
|
|
"avc format without codec data, assuming nal length size is 4");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* AVC implies alignment=au */
|
|
|
|
if (align == GST_H264_DECODER_ALIGN_NONE)
|
|
|
|
align = GST_H264_DECODER_ALIGN_AU;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (format == GST_H264_DECODER_FORMAT_BYTE) {
|
|
|
|
if (codec_data_value != NULL) {
|
|
|
|
GST_WARNING_OBJECT (self, "bytestream with codec data");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->in_format = format;
|
|
|
|
priv->align = align;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (priv->codec_data) {
|
|
|
|
GstMapInfo map;
|
|
|
|
|
|
|
|
gst_buffer_map (priv->codec_data, &map, GST_MAP_READ);
|
2021-09-21 13:21:51 +00:00
|
|
|
if (gst_h264_decoder_parse_codec_data (self, map.data, map.size) !=
|
|
|
|
GST_FLOW_OK) {
|
2020-03-23 05:40:52 +00:00
|
|
|
/* keep going without error.
|
|
|
|
* Probably inband SPS/PPS might be valid data */
|
|
|
|
GST_WARNING_OBJECT (self, "Failed to handle codec data");
|
|
|
|
}
|
2019-12-26 05:24:46 +00:00
|
|
|
gst_buffer_unmap (priv->codec_data, &map);
|
|
|
|
}
|
|
|
|
|
2020-04-24 11:56:17 +00:00
|
|
|
/* in case live streaming, we will run on low-latency mode */
|
|
|
|
priv->is_live = FALSE;
|
|
|
|
query = gst_query_new_latency ();
|
|
|
|
if (gst_pad_peer_query (GST_VIDEO_DECODER_SINK_PAD (self), query))
|
|
|
|
gst_query_parse_latency (query, &priv->is_live, NULL, NULL);
|
|
|
|
gst_query_unref (query);
|
|
|
|
|
|
|
|
if (priv->is_live)
|
|
|
|
GST_DEBUG_OBJECT (self, "Live source, will run on low-latency mode");
|
|
|
|
|
2019-12-26 05:24:46 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_h264_decoder_fill_picture_from_slice (GstH264Decoder * self,
|
|
|
|
const GstH264Slice * slice, GstH264Picture * picture)
|
|
|
|
{
|
2020-11-04 18:47:35 +00:00
|
|
|
GstH264DecoderClass *klass = GST_H264_DECODER_GET_CLASS (self);
|
2019-12-26 05:24:46 +00:00
|
|
|
const GstH264SliceHdr *slice_hdr = &slice->header;
|
|
|
|
const GstH264PPS *pps;
|
|
|
|
const GstH264SPS *sps;
|
|
|
|
|
|
|
|
pps = slice_hdr->pps;
|
|
|
|
if (!pps) {
|
|
|
|
GST_ERROR_OBJECT (self, "No pps in slice header");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
sps = pps->sequence;
|
|
|
|
if (!sps) {
|
|
|
|
GST_ERROR_OBJECT (self, "No sps in pps");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
picture->idr = slice->nalu.idr_pic_flag;
|
2020-04-23 10:47:09 +00:00
|
|
|
picture->dec_ref_pic_marking = slice_hdr->dec_ref_pic_marking;
|
2019-12-26 05:24:46 +00:00
|
|
|
if (picture->idr)
|
|
|
|
picture->idr_pic_id = slice_hdr->idr_pic_id;
|
|
|
|
|
|
|
|
if (slice_hdr->field_pic_flag)
|
|
|
|
picture->field =
|
|
|
|
slice_hdr->bottom_field_flag ?
|
2020-04-19 16:37:58 +00:00
|
|
|
GST_H264_PICTURE_FIELD_BOTTOM_FIELD : GST_H264_PICTURE_FIELD_TOP_FIELD;
|
2019-12-26 05:24:46 +00:00
|
|
|
else
|
|
|
|
picture->field = GST_H264_PICTURE_FIELD_FRAME;
|
|
|
|
|
2020-11-15 15:27:28 +00:00
|
|
|
if (!GST_H264_PICTURE_IS_FRAME (picture) && !klass->new_field_picture) {
|
2020-11-04 18:47:35 +00:00
|
|
|
GST_FIXME_OBJECT (self, "Subclass doesn't support interlace stream");
|
2019-12-26 05:24:46 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
picture->nal_ref_idc = slice->nalu.ref_idc;
|
2020-11-05 16:45:36 +00:00
|
|
|
if (slice->nalu.ref_idc != 0)
|
2020-11-09 16:28:03 +00:00
|
|
|
gst_h264_picture_set_reference (picture,
|
|
|
|
GST_H264_PICTURE_REF_SHORT_TERM, FALSE);
|
2019-12-26 05:24:46 +00:00
|
|
|
|
2020-11-09 16:28:03 +00:00
|
|
|
picture->frame_num = slice_hdr->frame_num;
|
|
|
|
|
|
|
|
/* 7.4.3 */
|
|
|
|
if (!slice_hdr->field_pic_flag)
|
|
|
|
picture->pic_num = slice_hdr->frame_num;
|
|
|
|
else
|
|
|
|
picture->pic_num = 2 * slice_hdr->frame_num + 1;
|
2019-12-26 05:24:46 +00:00
|
|
|
|
|
|
|
picture->pic_order_cnt_type = sps->pic_order_cnt_type;
|
|
|
|
switch (picture->pic_order_cnt_type) {
|
|
|
|
case 0:
|
|
|
|
picture->pic_order_cnt_lsb = slice_hdr->pic_order_cnt_lsb;
|
|
|
|
picture->delta_pic_order_cnt_bottom =
|
|
|
|
slice_hdr->delta_pic_order_cnt_bottom;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
picture->delta_pic_order_cnt0 = slice_hdr->delta_pic_order_cnt[0];
|
|
|
|
picture->delta_pic_order_cnt1 = slice_hdr->delta_pic_order_cnt[1];
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_h264_decoder_calculate_poc (GstH264Decoder * self, GstH264Picture * picture)
|
|
|
|
{
|
|
|
|
GstH264DecoderPrivate *priv = self->priv;
|
|
|
|
const GstH264SPS *sps = priv->active_sps;
|
|
|
|
|
|
|
|
if (!sps) {
|
|
|
|
GST_ERROR_OBJECT (self, "No active SPS");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (picture->pic_order_cnt_type) {
|
|
|
|
case 0:{
|
|
|
|
/* See spec 8.2.1.1 */
|
|
|
|
gint prev_pic_order_cnt_msb, prev_pic_order_cnt_lsb;
|
|
|
|
gint max_pic_order_cnt_lsb;
|
|
|
|
|
|
|
|
if (picture->idr) {
|
|
|
|
prev_pic_order_cnt_msb = prev_pic_order_cnt_lsb = 0;
|
|
|
|
} else {
|
|
|
|
if (priv->prev_ref_has_memmgmnt5) {
|
|
|
|
if (priv->prev_ref_field != GST_H264_PICTURE_FIELD_BOTTOM_FIELD) {
|
|
|
|
prev_pic_order_cnt_msb = 0;
|
|
|
|
prev_pic_order_cnt_lsb = priv->prev_ref_top_field_order_cnt;
|
|
|
|
} else {
|
|
|
|
prev_pic_order_cnt_msb = 0;
|
|
|
|
prev_pic_order_cnt_lsb = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
prev_pic_order_cnt_msb = priv->prev_ref_pic_order_cnt_msb;
|
|
|
|
prev_pic_order_cnt_lsb = priv->prev_ref_pic_order_cnt_lsb;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
max_pic_order_cnt_lsb = 1 << (sps->log2_max_pic_order_cnt_lsb_minus4 + 4);
|
|
|
|
|
|
|
|
if ((picture->pic_order_cnt_lsb < prev_pic_order_cnt_lsb) &&
|
|
|
|
(prev_pic_order_cnt_lsb - picture->pic_order_cnt_lsb >=
|
|
|
|
max_pic_order_cnt_lsb / 2)) {
|
|
|
|
picture->pic_order_cnt_msb =
|
|
|
|
prev_pic_order_cnt_msb + max_pic_order_cnt_lsb;
|
|
|
|
} else if ((picture->pic_order_cnt_lsb > prev_pic_order_cnt_lsb)
|
|
|
|
&& (picture->pic_order_cnt_lsb - prev_pic_order_cnt_lsb >
|
|
|
|
max_pic_order_cnt_lsb / 2)) {
|
|
|
|
picture->pic_order_cnt_msb =
|
|
|
|
prev_pic_order_cnt_msb - max_pic_order_cnt_lsb;
|
|
|
|
} else {
|
|
|
|
picture->pic_order_cnt_msb = prev_pic_order_cnt_msb;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (picture->field != GST_H264_PICTURE_FIELD_BOTTOM_FIELD) {
|
|
|
|
picture->top_field_order_cnt =
|
|
|
|
picture->pic_order_cnt_msb + picture->pic_order_cnt_lsb;
|
|
|
|
}
|
|
|
|
|
2020-11-05 23:09:06 +00:00
|
|
|
switch (picture->field) {
|
|
|
|
case GST_H264_PICTURE_FIELD_FRAME:
|
|
|
|
picture->top_field_order_cnt = picture->pic_order_cnt_msb +
|
|
|
|
picture->pic_order_cnt_lsb;
|
|
|
|
picture->bottom_field_order_cnt = picture->top_field_order_cnt +
|
2019-12-26 05:24:46 +00:00
|
|
|
picture->delta_pic_order_cnt_bottom;
|
2020-11-05 23:09:06 +00:00
|
|
|
break;
|
|
|
|
case GST_H264_PICTURE_FIELD_TOP_FIELD:
|
|
|
|
picture->top_field_order_cnt = picture->pic_order_cnt_msb +
|
|
|
|
picture->pic_order_cnt_lsb;
|
|
|
|
break;
|
|
|
|
case GST_H264_PICTURE_FIELD_BOTTOM_FIELD:
|
|
|
|
picture->bottom_field_order_cnt = picture->pic_order_cnt_msb +
|
|
|
|
picture->pic_order_cnt_lsb;
|
|
|
|
break;
|
2019-12-26 05:24:46 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 1:{
|
|
|
|
gint abs_frame_num = 0;
|
|
|
|
gint expected_pic_order_cnt = 0;
|
|
|
|
gint i;
|
|
|
|
|
|
|
|
/* See spec 8.2.1.2 */
|
|
|
|
if (priv->prev_has_memmgmnt5)
|
|
|
|
priv->prev_frame_num_offset = 0;
|
|
|
|
|
|
|
|
if (picture->idr)
|
|
|
|
picture->frame_num_offset = 0;
|
|
|
|
else if (priv->prev_frame_num > picture->frame_num)
|
|
|
|
picture->frame_num_offset =
|
|
|
|
priv->prev_frame_num_offset + priv->max_frame_num;
|
|
|
|
else
|
|
|
|
picture->frame_num_offset = priv->prev_frame_num_offset;
|
|
|
|
|
|
|
|
if (sps->num_ref_frames_in_pic_order_cnt_cycle != 0)
|
|
|
|
abs_frame_num = picture->frame_num_offset + picture->frame_num;
|
|
|
|
else
|
|
|
|
abs_frame_num = 0;
|
|
|
|
|
|
|
|
if (picture->nal_ref_idc == 0 && abs_frame_num > 0)
|
|
|
|
--abs_frame_num;
|
|
|
|
|
|
|
|
if (abs_frame_num > 0) {
|
|
|
|
gint pic_order_cnt_cycle_cnt, frame_num_in_pic_order_cnt_cycle;
|
|
|
|
gint expected_delta_per_pic_order_cnt_cycle = 0;
|
|
|
|
|
|
|
|
if (sps->num_ref_frames_in_pic_order_cnt_cycle == 0) {
|
|
|
|
GST_WARNING_OBJECT (self,
|
|
|
|
"Invalid num_ref_frames_in_pic_order_cnt_cycle in stream");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
pic_order_cnt_cycle_cnt =
|
|
|
|
(abs_frame_num - 1) / sps->num_ref_frames_in_pic_order_cnt_cycle;
|
|
|
|
frame_num_in_pic_order_cnt_cycle =
|
|
|
|
(abs_frame_num - 1) % sps->num_ref_frames_in_pic_order_cnt_cycle;
|
|
|
|
|
|
|
|
for (i = 0; i < sps->num_ref_frames_in_pic_order_cnt_cycle; i++) {
|
|
|
|
expected_delta_per_pic_order_cnt_cycle +=
|
|
|
|
sps->offset_for_ref_frame[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
expected_pic_order_cnt = pic_order_cnt_cycle_cnt *
|
|
|
|
expected_delta_per_pic_order_cnt_cycle;
|
|
|
|
/* frame_num_in_pic_order_cnt_cycle is verified < 255 in parser */
|
|
|
|
for (i = 0; i <= frame_num_in_pic_order_cnt_cycle; ++i)
|
|
|
|
expected_pic_order_cnt += sps->offset_for_ref_frame[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!picture->nal_ref_idc)
|
|
|
|
expected_pic_order_cnt += sps->offset_for_non_ref_pic;
|
|
|
|
|
2020-11-15 15:27:28 +00:00
|
|
|
if (GST_H264_PICTURE_IS_FRAME (picture)) {
|
2019-12-26 05:24:46 +00:00
|
|
|
picture->top_field_order_cnt =
|
|
|
|
expected_pic_order_cnt + picture->delta_pic_order_cnt0;
|
|
|
|
picture->bottom_field_order_cnt = picture->top_field_order_cnt +
|
|
|
|
sps->offset_for_top_to_bottom_field + picture->delta_pic_order_cnt1;
|
|
|
|
} else if (picture->field != GST_H264_PICTURE_FIELD_BOTTOM_FIELD) {
|
|
|
|
picture->top_field_order_cnt =
|
|
|
|
expected_pic_order_cnt + picture->delta_pic_order_cnt0;
|
|
|
|
} else {
|
|
|
|
picture->bottom_field_order_cnt = expected_pic_order_cnt +
|
|
|
|
sps->offset_for_top_to_bottom_field + picture->delta_pic_order_cnt0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 2:{
|
|
|
|
gint temp_pic_order_cnt;
|
|
|
|
|
|
|
|
/* See spec 8.2.1.3 */
|
|
|
|
if (priv->prev_has_memmgmnt5)
|
|
|
|
priv->prev_frame_num_offset = 0;
|
|
|
|
|
|
|
|
if (picture->idr)
|
|
|
|
picture->frame_num_offset = 0;
|
|
|
|
else if (priv->prev_frame_num > picture->frame_num)
|
|
|
|
picture->frame_num_offset =
|
|
|
|
priv->prev_frame_num_offset + priv->max_frame_num;
|
|
|
|
else
|
|
|
|
picture->frame_num_offset = priv->prev_frame_num_offset;
|
|
|
|
|
|
|
|
if (picture->idr) {
|
|
|
|
temp_pic_order_cnt = 0;
|
|
|
|
} else if (!picture->nal_ref_idc) {
|
|
|
|
temp_pic_order_cnt =
|
|
|
|
2 * (picture->frame_num_offset + picture->frame_num) - 1;
|
|
|
|
} else {
|
|
|
|
temp_pic_order_cnt =
|
|
|
|
2 * (picture->frame_num_offset + picture->frame_num);
|
|
|
|
}
|
|
|
|
|
2020-11-15 15:27:28 +00:00
|
|
|
if (GST_H264_PICTURE_IS_FRAME (picture)) {
|
2019-12-26 05:24:46 +00:00
|
|
|
picture->top_field_order_cnt = temp_pic_order_cnt;
|
|
|
|
picture->bottom_field_order_cnt = temp_pic_order_cnt;
|
|
|
|
} else if (picture->field == GST_H264_PICTURE_FIELD_BOTTOM_FIELD) {
|
|
|
|
picture->bottom_field_order_cnt = temp_pic_order_cnt;
|
|
|
|
} else {
|
|
|
|
picture->top_field_order_cnt = temp_pic_order_cnt;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
GST_WARNING_OBJECT (self,
|
|
|
|
"Invalid pic_order_cnt_type: %d", sps->pic_order_cnt_type);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (picture->field) {
|
|
|
|
case GST_H264_PICTURE_FIELD_FRAME:
|
|
|
|
picture->pic_order_cnt =
|
|
|
|
MIN (picture->top_field_order_cnt, picture->bottom_field_order_cnt);
|
|
|
|
break;
|
2020-04-19 16:37:58 +00:00
|
|
|
case GST_H264_PICTURE_FIELD_TOP_FIELD:
|
2019-12-26 05:24:46 +00:00
|
|
|
picture->pic_order_cnt = picture->top_field_order_cnt;
|
|
|
|
break;
|
|
|
|
case GST_H264_PICTURE_FIELD_BOTTOM_FIELD:
|
|
|
|
picture->pic_order_cnt = picture->bottom_field_order_cnt;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2020-12-29 10:54:35 +00:00
|
|
|
static void
|
2021-09-21 13:21:51 +00:00
|
|
|
gst_h264_decoder_drain_output_queue (GstH264Decoder * self, guint num,
|
|
|
|
GstFlowReturn * ret)
|
2020-12-29 10:54:35 +00:00
|
|
|
{
|
|
|
|
GstH264DecoderPrivate *priv = self->priv;
|
|
|
|
GstH264DecoderClass *klass = GST_H264_DECODER_GET_CLASS (self);
|
|
|
|
|
2021-05-07 14:02:04 +00:00
|
|
|
g_assert (klass->output_picture);
|
2021-09-21 13:21:51 +00:00
|
|
|
g_assert (ret != NULL);
|
2021-05-07 14:02:04 +00:00
|
|
|
|
2020-12-29 10:54:35 +00:00
|
|
|
while (gst_queue_array_get_length (priv->output_queue) > num) {
|
|
|
|
GstH264DecoderOutputFrame *output_frame = (GstH264DecoderOutputFrame *)
|
|
|
|
gst_queue_array_pop_head_struct (priv->output_queue);
|
2021-09-21 13:21:51 +00:00
|
|
|
GstFlowReturn flow_ret = klass->output_picture (self, output_frame->frame,
|
2020-12-29 10:54:35 +00:00
|
|
|
output_frame->picture);
|
2021-09-21 13:21:51 +00:00
|
|
|
|
|
|
|
UPDATE_FLOW_RETURN (ret, flow_ret);
|
2020-12-29 10:54:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-26 05:24:46 +00:00
|
|
|
static void
|
|
|
|
gst_h264_decoder_do_output_picture (GstH264Decoder * self,
|
2021-09-21 13:21:51 +00:00
|
|
|
GstH264Picture * picture, GstFlowReturn * ret)
|
2019-12-26 05:24:46 +00:00
|
|
|
{
|
|
|
|
GstH264DecoderPrivate *priv = self->priv;
|
2020-07-20 09:24:09 +00:00
|
|
|
GstVideoCodecFrame *frame = NULL;
|
2020-12-29 10:54:35 +00:00
|
|
|
GstH264DecoderOutputFrame output_frame;
|
2021-09-21 13:21:51 +00:00
|
|
|
GstFlowReturn flow_ret = GST_FLOW_OK;
|
|
|
|
|
|
|
|
g_assert (ret != NULL);
|
2019-12-26 05:24:46 +00:00
|
|
|
|
2020-04-20 07:00:30 +00:00
|
|
|
GST_LOG_OBJECT (self, "Outputting picture %p (frame_num %d, poc %d)",
|
|
|
|
picture, picture->frame_num, picture->pic_order_cnt);
|
|
|
|
|
2019-12-26 05:24:46 +00:00
|
|
|
if (picture->pic_order_cnt < priv->last_output_poc) {
|
|
|
|
GST_WARNING_OBJECT (self,
|
|
|
|
"Outputting out of order %d -> %d, likely a broken stream",
|
|
|
|
priv->last_output_poc, picture->pic_order_cnt);
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->last_output_poc = picture->pic_order_cnt;
|
|
|
|
|
2020-07-20 09:24:09 +00:00
|
|
|
frame = gst_video_decoder_get_frame (GST_VIDEO_DECODER (self),
|
|
|
|
picture->system_frame_number);
|
|
|
|
|
|
|
|
if (!frame) {
|
|
|
|
GST_ERROR_OBJECT (self,
|
|
|
|
"No available codec frame with frame number %d",
|
|
|
|
picture->system_frame_number);
|
2021-09-21 13:21:51 +00:00
|
|
|
UPDATE_FLOW_RETURN (ret, GST_FLOW_ERROR);
|
|
|
|
|
2020-07-20 10:13:29 +00:00
|
|
|
gst_h264_picture_unref (picture);
|
2020-07-20 09:24:09 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-12-29 10:54:35 +00:00
|
|
|
output_frame.frame = frame;
|
|
|
|
output_frame.picture = picture;
|
|
|
|
output_frame.self = self;
|
|
|
|
gst_queue_array_push_tail_struct (priv->output_queue, &output_frame);
|
2019-12-26 05:24:46 +00:00
|
|
|
|
2021-09-21 13:21:51 +00:00
|
|
|
gst_h264_decoder_drain_output_queue (self, priv->preferred_output_delay,
|
|
|
|
&flow_ret);
|
|
|
|
UPDATE_FLOW_RETURN (ret, flow_ret);
|
2019-12-26 05:24:46 +00:00
|
|
|
}
|
|
|
|
|
2021-09-21 13:21:51 +00:00
|
|
|
static void
|
|
|
|
gst_h264_decoder_finish_current_picture (GstH264Decoder * self,
|
|
|
|
GstFlowReturn * ret)
|
2019-12-26 05:24:46 +00:00
|
|
|
{
|
|
|
|
GstH264DecoderPrivate *priv = self->priv;
|
|
|
|
GstH264DecoderClass *klass;
|
2021-09-21 13:21:51 +00:00
|
|
|
GstFlowReturn flow_ret = GST_FLOW_OK;
|
2019-12-26 05:24:46 +00:00
|
|
|
|
|
|
|
if (!priv->current_picture)
|
2021-09-21 13:21:51 +00:00
|
|
|
return;
|
2019-12-26 05:24:46 +00:00
|
|
|
|
|
|
|
klass = GST_H264_DECODER_GET_CLASS (self);
|
|
|
|
|
2020-03-18 20:51:11 +00:00
|
|
|
if (klass->end_picture) {
|
2021-09-21 13:21:51 +00:00
|
|
|
flow_ret = klass->end_picture (self, priv->current_picture);
|
|
|
|
if (flow_ret != GST_FLOW_OK) {
|
2020-04-20 07:00:30 +00:00
|
|
|
GST_WARNING_OBJECT (self,
|
|
|
|
"end picture failed, marking picture %p non-existing "
|
|
|
|
"(frame_num %d, poc %d)", priv->current_picture,
|
|
|
|
priv->current_picture->frame_num,
|
|
|
|
priv->current_picture->pic_order_cnt);
|
2020-03-18 20:51:11 +00:00
|
|
|
priv->current_picture->nonexisting = TRUE;
|
2020-07-20 11:54:26 +00:00
|
|
|
|
|
|
|
/* this fake nonexisting picture will not trigger ouput_picture() */
|
|
|
|
gst_video_decoder_drop_frame (GST_VIDEO_DECODER (self),
|
|
|
|
gst_video_codec_frame_ref (priv->current_frame));
|
2020-04-20 07:00:30 +00:00
|
|
|
}
|
2020-03-18 20:51:11 +00:00
|
|
|
}
|
2019-12-26 05:24:46 +00:00
|
|
|
|
2020-04-27 14:53:45 +00:00
|
|
|
/* We no longer need the per frame reference lists */
|
|
|
|
gst_h264_decoder_clear_ref_pic_lists (self);
|
|
|
|
|
2019-12-26 05:24:46 +00:00
|
|
|
/* finish picture takes ownership of the picture */
|
2021-09-21 13:21:51 +00:00
|
|
|
gst_h264_decoder_finish_picture (self, priv->current_picture, &flow_ret);
|
2020-01-30 11:04:58 +00:00
|
|
|
priv->current_picture = NULL;
|
|
|
|
|
2021-09-21 13:21:51 +00:00
|
|
|
UPDATE_FLOW_RETURN (ret, flow_ret);
|
2019-12-26 05:24:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gint
|
2020-05-15 18:56:27 +00:00
|
|
|
poc_asc_compare (const GstH264Picture ** a, const GstH264Picture ** b)
|
2019-12-26 05:24:46 +00:00
|
|
|
{
|
2020-05-15 18:56:27 +00:00
|
|
|
return (*a)->pic_order_cnt - (*b)->pic_order_cnt;
|
2019-12-26 05:24:46 +00:00
|
|
|
}
|
|
|
|
|
2020-04-27 14:53:45 +00:00
|
|
|
static gint
|
|
|
|
poc_desc_compare (const GstH264Picture ** a, const GstH264Picture ** b)
|
|
|
|
{
|
|
|
|
return (*b)->pic_order_cnt - (*a)->pic_order_cnt;
|
|
|
|
}
|
|
|
|
|
2021-09-21 13:21:51 +00:00
|
|
|
static GstFlowReturn
|
2020-07-20 10:13:29 +00:00
|
|
|
gst_h264_decoder_drain_internal (GstH264Decoder * self)
|
2019-12-26 05:24:46 +00:00
|
|
|
{
|
|
|
|
GstH264DecoderPrivate *priv = self->priv;
|
2020-11-01 15:08:04 +00:00
|
|
|
GstH264Picture *picture;
|
2021-09-21 13:21:51 +00:00
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
2020-11-01 15:08:04 +00:00
|
|
|
|
|
|
|
while ((picture = gst_h264_dpb_bump (priv->dpb, TRUE)) != NULL) {
|
2021-09-21 13:21:51 +00:00
|
|
|
gst_h264_decoder_do_output_picture (self, picture, &ret);
|
2019-12-26 05:24:46 +00:00
|
|
|
}
|
|
|
|
|
2021-09-21 13:21:51 +00:00
|
|
|
gst_h264_decoder_drain_output_queue (self, 0, &ret);
|
2020-12-29 10:54:35 +00:00
|
|
|
|
2021-07-27 02:51:03 +00:00
|
|
|
gst_h264_picture_clear (&priv->last_field);
|
2020-07-20 20:48:32 +00:00
|
|
|
gst_h264_dpb_clear (priv->dpb);
|
2021-07-06 05:38:16 +00:00
|
|
|
priv->last_output_poc = G_MININT32;
|
2020-11-01 15:08:04 +00:00
|
|
|
|
2021-09-21 13:21:51 +00:00
|
|
|
return ret;
|
2019-12-26 05:24:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_h264_decoder_handle_memory_management_opt (GstH264Decoder * self,
|
|
|
|
GstH264Picture * picture)
|
|
|
|
{
|
|
|
|
GstH264DecoderPrivate *priv = self->priv;
|
2020-11-01 15:08:04 +00:00
|
|
|
gint i;
|
2019-12-26 05:24:46 +00:00
|
|
|
|
|
|
|
for (i = 0; i < G_N_ELEMENTS (picture->dec_ref_pic_marking.ref_pic_marking);
|
|
|
|
i++) {
|
|
|
|
GstH264RefPicMarking *ref_pic_marking =
|
|
|
|
&picture->dec_ref_pic_marking.ref_pic_marking[i];
|
2020-11-01 15:08:04 +00:00
|
|
|
guint8 type = ref_pic_marking->memory_management_control_operation;
|
2019-12-26 05:24:46 +00:00
|
|
|
|
2020-11-01 15:08:04 +00:00
|
|
|
GST_TRACE_OBJECT (self, "memory management operation %d, type %d", i, type);
|
2019-12-26 05:24:46 +00:00
|
|
|
|
2020-11-01 15:08:04 +00:00
|
|
|
/* Normal end of operations' specification */
|
|
|
|
if (type == 0)
|
|
|
|
return TRUE;
|
2019-12-26 05:24:46 +00:00
|
|
|
|
2020-11-01 15:08:04 +00:00
|
|
|
switch (type) {
|
|
|
|
case 4:
|
2019-12-26 05:24:46 +00:00
|
|
|
priv->max_long_term_frame_idx =
|
|
|
|
ref_pic_marking->max_long_term_frame_idx_plus1 - 1;
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
priv->max_long_term_frame_idx = -1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2020-11-01 15:08:04 +00:00
|
|
|
|
|
|
|
if (!gst_h264_dpb_perform_memory_management_control_operation (priv->dpb,
|
|
|
|
ref_pic_marking, picture)) {
|
|
|
|
GST_WARNING_OBJECT (self, "memory management operation type %d failed",
|
|
|
|
type);
|
2020-11-09 14:04:32 +00:00
|
|
|
/* Most likely our implementation fault, but let's just perform
|
|
|
|
* next MMCO if any */
|
2020-11-01 15:08:04 +00:00
|
|
|
}
|
2019-12-26 05:24:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2020-11-09 16:28:03 +00:00
|
|
|
gst_h264_decoder_sliding_window_picture_marking (GstH264Decoder * self,
|
|
|
|
GstH264Picture * picture)
|
2019-12-26 05:24:46 +00:00
|
|
|
{
|
|
|
|
GstH264DecoderPrivate *priv = self->priv;
|
|
|
|
const GstH264SPS *sps = priv->active_sps;
|
|
|
|
gint num_ref_pics;
|
|
|
|
gint max_num_ref_frames;
|
|
|
|
|
2020-11-09 16:28:03 +00:00
|
|
|
/* Skip this for the second field */
|
|
|
|
if (picture->second_field)
|
|
|
|
return TRUE;
|
|
|
|
|
2019-12-26 05:24:46 +00:00
|
|
|
if (!sps) {
|
|
|
|
GST_ERROR_OBJECT (self, "No active sps");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 8.2.5.3. Ensure the DPB doesn't overflow by discarding the oldest picture */
|
2020-11-04 19:16:54 +00:00
|
|
|
num_ref_pics = gst_h264_dpb_num_ref_frames (priv->dpb);
|
2019-12-26 05:24:46 +00:00
|
|
|
max_num_ref_frames = MAX (1, sps->num_ref_frames);
|
|
|
|
|
2020-11-09 14:04:32 +00:00
|
|
|
if (num_ref_pics < max_num_ref_frames)
|
|
|
|
return TRUE;
|
2019-12-26 05:24:46 +00:00
|
|
|
|
2020-11-09 14:04:32 +00:00
|
|
|
/* In theory, num_ref_pics shouldn't be larger than max_num_ref_frames
|
|
|
|
* but it could happen if our implementation is wrong somehow or so.
|
|
|
|
* Just try to remove reference pictures as many as possible in order to
|
|
|
|
* avoid DPB overflow.
|
|
|
|
*/
|
|
|
|
while (num_ref_pics >= max_num_ref_frames) {
|
2019-12-26 05:24:46 +00:00
|
|
|
/* Max number of reference pics reached, need to remove one of the short
|
|
|
|
* term ones. Find smallest frame_num_wrap short reference picture and mark
|
|
|
|
* it as unused */
|
|
|
|
GstH264Picture *to_unmark =
|
|
|
|
gst_h264_dpb_get_lowest_frame_num_short_ref (priv->dpb);
|
|
|
|
|
2020-11-09 14:04:32 +00:00
|
|
|
if (num_ref_pics > max_num_ref_frames) {
|
|
|
|
GST_WARNING_OBJECT (self,
|
|
|
|
"num_ref_pics %d is larger than allowed maximum %d",
|
|
|
|
num_ref_pics, max_num_ref_frames);
|
|
|
|
}
|
|
|
|
|
2019-12-26 05:24:46 +00:00
|
|
|
if (!to_unmark) {
|
|
|
|
GST_WARNING_OBJECT (self, "Could not find a short ref picture to unmark");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2020-04-20 07:00:30 +00:00
|
|
|
GST_TRACE_OBJECT (self,
|
|
|
|
"Unmark reference flag of picture %p (frame_num %d, poc %d)",
|
|
|
|
to_unmark, to_unmark->frame_num, to_unmark->pic_order_cnt);
|
|
|
|
|
2020-11-09 16:28:03 +00:00
|
|
|
gst_h264_picture_set_reference (to_unmark, GST_H264_PICTURE_REF_NONE, TRUE);
|
2019-12-26 05:24:46 +00:00
|
|
|
gst_h264_picture_unref (to_unmark);
|
2020-11-09 14:04:32 +00:00
|
|
|
|
|
|
|
num_ref_pics--;
|
2019-12-26 05:24:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This method ensures that DPB does not overflow, either by removing
|
|
|
|
* reference pictures as specified in the stream, or using a sliding window
|
|
|
|
* procedure to remove the oldest one.
|
|
|
|
* It also performs marking and unmarking pictures as reference.
|
|
|
|
* See spac 8.2.5.1 */
|
|
|
|
static gboolean
|
|
|
|
gst_h264_decoder_reference_picture_marking (GstH264Decoder * self,
|
|
|
|
GstH264Picture * picture)
|
|
|
|
{
|
|
|
|
GstH264DecoderPrivate *priv = self->priv;
|
|
|
|
|
|
|
|
/* If the current picture is an IDR, all reference pictures are unmarked */
|
|
|
|
if (picture->idr) {
|
|
|
|
gst_h264_dpb_mark_all_non_ref (priv->dpb);
|
|
|
|
|
|
|
|
if (picture->dec_ref_pic_marking.long_term_reference_flag) {
|
2020-11-09 16:28:03 +00:00
|
|
|
gst_h264_picture_set_reference (picture,
|
|
|
|
GST_H264_PICTURE_REF_LONG_TERM, FALSE);
|
2019-12-26 05:24:46 +00:00
|
|
|
picture->long_term_frame_idx = 0;
|
|
|
|
priv->max_long_term_frame_idx = 0;
|
|
|
|
} else {
|
2020-11-09 16:28:03 +00:00
|
|
|
gst_h264_picture_set_reference (picture,
|
|
|
|
GST_H264_PICTURE_REF_SHORT_TERM, FALSE);
|
2019-12-26 05:24:46 +00:00
|
|
|
priv->max_long_term_frame_idx = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Not an IDR. If the stream contains instructions on how to discard pictures
|
|
|
|
* from DPB and how to mark/unmark existing reference pictures, do so.
|
|
|
|
* Otherwise, fall back to default sliding window process */
|
|
|
|
if (picture->dec_ref_pic_marking.adaptive_ref_pic_marking_mode_flag) {
|
2020-04-20 07:00:30 +00:00
|
|
|
if (picture->nonexisting) {
|
|
|
|
GST_WARNING_OBJECT (self,
|
|
|
|
"Invalid memory management operation for non-existing picture "
|
|
|
|
"%p (frame_num %d, poc %d", picture, picture->frame_num,
|
|
|
|
picture->pic_order_cnt);
|
|
|
|
}
|
|
|
|
|
2019-12-26 05:24:46 +00:00
|
|
|
return gst_h264_decoder_handle_memory_management_opt (self, picture);
|
|
|
|
}
|
|
|
|
|
2020-11-09 16:28:03 +00:00
|
|
|
return gst_h264_decoder_sliding_window_picture_marking (self, picture);
|
2019-12-26 05:24:46 +00:00
|
|
|
}
|
|
|
|
|
2021-07-28 15:19:15 +00:00
|
|
|
static GstH264DpbBumpMode
|
|
|
|
get_bump_level (GstH264Decoder * self)
|
|
|
|
{
|
|
|
|
GstH264DecoderPrivate *priv = self->priv;
|
|
|
|
|
|
|
|
/* User set the mode explicitly. */
|
|
|
|
switch (priv->compliance) {
|
|
|
|
case GST_H264_DECODER_COMPLIANCE_STRICT:
|
|
|
|
return GST_H264_DPB_BUMP_NORMAL_LATENCY;
|
|
|
|
case GST_H264_DECODER_COMPLIANCE_NORMAL:
|
|
|
|
return GST_H264_DPB_BUMP_LOW_LATENCY;
|
|
|
|
case GST_H264_DECODER_COMPLIANCE_FLEXIBLE:
|
|
|
|
return GST_H264_DPB_BUMP_VERY_LOW_LATENCY;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* GST_H264_DECODER_COMPLIANCE_AUTO case. */
|
|
|
|
|
|
|
|
if (priv->is_live) {
|
|
|
|
/* The baseline and constrained-baseline profiles do not have B frames
|
|
|
|
and do not use the picture reorder, safe to use the higher bump level. */
|
|
|
|
if (priv->profile_idc == GST_H264_PROFILE_BASELINE)
|
|
|
|
return GST_H264_DPB_BUMP_VERY_LOW_LATENCY;
|
|
|
|
|
|
|
|
return GST_H264_DPB_BUMP_LOW_LATENCY;
|
|
|
|
}
|
|
|
|
|
|
|
|
return GST_H264_DPB_BUMP_NORMAL_LATENCY;
|
|
|
|
}
|
|
|
|
|
2021-09-21 13:21:51 +00:00
|
|
|
static void
|
2019-12-26 05:24:46 +00:00
|
|
|
gst_h264_decoder_finish_picture (GstH264Decoder * self,
|
2021-09-21 13:21:51 +00:00
|
|
|
GstH264Picture * picture, GstFlowReturn * ret)
|
2019-12-26 05:24:46 +00:00
|
|
|
{
|
2020-11-09 16:28:03 +00:00
|
|
|
GstVideoDecoder *decoder = GST_VIDEO_DECODER (self);
|
2019-12-26 05:24:46 +00:00
|
|
|
GstH264DecoderPrivate *priv = self->priv;
|
2021-08-31 09:16:05 +00:00
|
|
|
GstH264DpbBumpMode bump_level = get_bump_level (self);
|
2019-12-26 05:24:46 +00:00
|
|
|
|
|
|
|
/* Finish processing the picture.
|
|
|
|
* Start by storing previous picture data for later use */
|
|
|
|
if (picture->ref) {
|
|
|
|
gst_h264_decoder_reference_picture_marking (self, picture);
|
|
|
|
priv->prev_ref_has_memmgmnt5 = picture->mem_mgmt_5;
|
|
|
|
priv->prev_ref_top_field_order_cnt = picture->top_field_order_cnt;
|
|
|
|
priv->prev_ref_pic_order_cnt_msb = picture->pic_order_cnt_msb;
|
|
|
|
priv->prev_ref_pic_order_cnt_lsb = picture->pic_order_cnt_lsb;
|
|
|
|
priv->prev_ref_field = picture->field;
|
|
|
|
priv->prev_ref_frame_num = picture->frame_num;
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->prev_frame_num = picture->frame_num;
|
|
|
|
priv->prev_has_memmgmnt5 = picture->mem_mgmt_5;
|
|
|
|
priv->prev_frame_num_offset = picture->frame_num_offset;
|
|
|
|
|
|
|
|
/* Remove unused (for reference or later output) pictures from DPB, marking
|
|
|
|
* them as such */
|
|
|
|
gst_h264_dpb_delete_unused (priv->dpb);
|
2020-11-09 16:28:03 +00:00
|
|
|
|
2021-01-10 14:11:01 +00:00
|
|
|
/* If field pictures belong to different codec frame,
|
|
|
|
* drop codec frame of the second field because we are consuming
|
|
|
|
* only the first codec frame via GstH264Decoder::output_picture() method */
|
|
|
|
if (picture->second_field && picture->other_field &&
|
|
|
|
picture->system_frame_number !=
|
|
|
|
picture->other_field->system_frame_number) {
|
2020-11-09 16:28:03 +00:00
|
|
|
GstVideoCodecFrame *frame = gst_video_decoder_get_frame (decoder,
|
|
|
|
picture->system_frame_number);
|
|
|
|
|
|
|
|
gst_video_decoder_release_frame (decoder, frame);
|
|
|
|
}
|
|
|
|
|
2021-07-11 16:31:54 +00:00
|
|
|
/* C.4.4 */
|
|
|
|
if (picture->mem_mgmt_5) {
|
2021-09-21 13:21:51 +00:00
|
|
|
GstFlowReturn drain_ret;
|
|
|
|
|
2021-07-11 16:31:54 +00:00
|
|
|
GST_TRACE_OBJECT (self, "Memory management type 5, drain the DPB");
|
2021-09-21 13:21:51 +00:00
|
|
|
|
|
|
|
drain_ret = gst_h264_decoder_drain_internal (self);
|
|
|
|
UPDATE_FLOW_RETURN (ret, drain_ret);
|
2020-11-14 15:55:09 +00:00
|
|
|
}
|
2019-12-26 05:24:46 +00:00
|
|
|
|
2021-09-21 13:21:51 +00:00
|
|
|
_bump_dpb (self, bump_level, picture, ret);
|
2020-04-23 10:47:09 +00:00
|
|
|
|
2021-07-11 16:31:54 +00:00
|
|
|
/* Add a ref to avoid the case of directly outputed and destroyed. */
|
|
|
|
gst_h264_picture_ref (picture);
|
|
|
|
|
|
|
|
/* C.4.5.1, C.4.5.2
|
|
|
|
- If the current decoded picture is the second field of a complementary
|
|
|
|
reference field pair, add to DPB.
|
|
|
|
C.4.5.1
|
|
|
|
For A reference decoded picture, the "bumping" process is invoked
|
|
|
|
repeatedly until there is an empty frame buffer, then add to DPB:
|
|
|
|
C.4.5.2
|
|
|
|
For a non-reference decoded picture, if there is empty frame buffer
|
|
|
|
after bumping the smaller POC, add to DPB.
|
|
|
|
Otherwise, output directly. */
|
2021-07-27 02:51:03 +00:00
|
|
|
if ((picture->second_field && picture->other_field
|
|
|
|
&& picture->other_field->ref)
|
|
|
|
|| picture->ref || gst_h264_dpb_has_empty_frame_buffer (priv->dpb)) {
|
2021-07-11 16:31:54 +00:00
|
|
|
/* Split frame into top/bottom field pictures for reference picture marking
|
|
|
|
* process. Even if current picture has field_pic_flag equal to zero,
|
|
|
|
* if next picture is a field picture, complementary field pair of reference
|
|
|
|
* frame should have individual pic_num and long_term_pic_num.
|
|
|
|
*/
|
|
|
|
if (gst_h264_dpb_get_interlaced (priv->dpb) &&
|
|
|
|
GST_H264_PICTURE_IS_FRAME (picture)) {
|
|
|
|
GstH264Picture *other_field =
|
|
|
|
gst_h264_decoder_split_frame (self, picture);
|
|
|
|
|
2021-07-27 04:16:13 +00:00
|
|
|
add_picture_to_dpb (self, picture);
|
2021-07-11 16:31:54 +00:00
|
|
|
if (!other_field) {
|
|
|
|
GST_WARNING_OBJECT (self,
|
|
|
|
"Couldn't split frame into complementary field pair");
|
|
|
|
/* Keep decoding anyway... */
|
|
|
|
} else {
|
2021-07-27 04:16:13 +00:00
|
|
|
add_picture_to_dpb (self, other_field);
|
2021-07-11 16:31:54 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-07-27 04:16:13 +00:00
|
|
|
add_picture_to_dpb (self, picture);
|
2021-07-11 16:31:54 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-09-21 13:21:51 +00:00
|
|
|
output_picture_directly (self, picture, ret);
|
2021-07-11 16:31:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GST_LOG_OBJECT (self,
|
|
|
|
"Finishing picture %p (frame_num %d, poc %d), entries in DPB %d",
|
|
|
|
picture, picture->frame_num, picture->pic_order_cnt,
|
|
|
|
gst_h264_dpb_get_size (priv->dpb));
|
|
|
|
|
|
|
|
gst_h264_picture_unref (picture);
|
|
|
|
|
2021-08-31 09:16:05 +00:00
|
|
|
/* For the live mode, we try to bump here to avoid waiting
|
|
|
|
for another decoding circle. */
|
|
|
|
if (priv->is_live && priv->compliance != GST_H264_DECODER_COMPLIANCE_STRICT)
|
2021-09-21 13:21:51 +00:00
|
|
|
_bump_dpb (self, bump_level, NULL, ret);
|
2019-12-26 05:24:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_h264_decoder_update_max_num_reorder_frames (GstH264Decoder * self,
|
|
|
|
GstH264SPS * sps)
|
|
|
|
{
|
|
|
|
GstH264DecoderPrivate *priv = self->priv;
|
2021-07-20 15:49:12 +00:00
|
|
|
gsize max_num_reorder_frames = 0;
|
2019-12-26 05:24:46 +00:00
|
|
|
|
|
|
|
if (sps->vui_parameters_present_flag
|
|
|
|
&& sps->vui_parameters.bitstream_restriction_flag) {
|
2021-07-20 15:49:12 +00:00
|
|
|
max_num_reorder_frames = sps->vui_parameters.num_reorder_frames;
|
|
|
|
if (max_num_reorder_frames > gst_h264_dpb_get_max_num_frames (priv->dpb)) {
|
2019-12-26 05:24:46 +00:00
|
|
|
GST_WARNING
|
|
|
|
("max_num_reorder_frames present, but larger than MaxDpbFrames (%d > %d)",
|
2021-07-20 15:49:12 +00:00
|
|
|
(gint) max_num_reorder_frames,
|
2020-11-04 19:16:54 +00:00
|
|
|
gst_h264_dpb_get_max_num_frames (priv->dpb));
|
2019-12-26 05:24:46 +00:00
|
|
|
|
2021-07-20 15:49:12 +00:00
|
|
|
max_num_reorder_frames = 0;
|
2019-12-26 05:24:46 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2021-07-20 15:49:12 +00:00
|
|
|
gst_h264_dpb_set_max_num_reorder_frames (priv->dpb, max_num_reorder_frames);
|
|
|
|
|
2019-12-26 05:24:46 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2021-08-31 08:39:06 +00:00
|
|
|
if (priv->compliance == GST_H264_DECODER_COMPLIANCE_STRICT) {
|
|
|
|
gst_h264_dpb_set_max_num_reorder_frames (priv->dpb,
|
|
|
|
gst_h264_dpb_get_max_num_frames (priv->dpb));
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* max_num_reorder_frames not present, infer it from profile/constraints. */
|
|
|
|
if (sps->profile_idc == 66 || sps->profile_idc == 83) {
|
|
|
|
/* baseline, constrained baseline and scalable-baseline profiles
|
|
|
|
only contain I/P frames. */
|
|
|
|
max_num_reorder_frames = 0;
|
|
|
|
} else if (sps->constraint_set3_flag) {
|
|
|
|
/* constraint_set3_flag may mean the -intra only profile. */
|
2019-12-26 05:24:46 +00:00
|
|
|
switch (sps->profile_idc) {
|
|
|
|
case 44:
|
|
|
|
case 86:
|
|
|
|
case 100:
|
|
|
|
case 110:
|
|
|
|
case 122:
|
|
|
|
case 244:
|
2021-07-20 15:49:12 +00:00
|
|
|
max_num_reorder_frames = 0;
|
2019-12-26 05:24:46 +00:00
|
|
|
break;
|
|
|
|
default:
|
2021-07-20 15:49:12 +00:00
|
|
|
max_num_reorder_frames = gst_h264_dpb_get_max_num_frames (priv->dpb);
|
2019-12-26 05:24:46 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
2021-07-20 15:49:12 +00:00
|
|
|
max_num_reorder_frames = gst_h264_dpb_get_max_num_frames (priv->dpb);
|
2019-12-26 05:24:46 +00:00
|
|
|
}
|
|
|
|
|
2021-07-20 15:49:12 +00:00
|
|
|
gst_h264_dpb_set_max_num_reorder_frames (priv->dpb, max_num_reorder_frames);
|
|
|
|
|
2019-12-26 05:24:46 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
GST_H264_LEVEL_L1 = 10,
|
|
|
|
GST_H264_LEVEL_L1B = 9,
|
|
|
|
GST_H264_LEVEL_L1_1 = 11,
|
|
|
|
GST_H264_LEVEL_L1_2 = 12,
|
|
|
|
GST_H264_LEVEL_L1_3 = 13,
|
|
|
|
GST_H264_LEVEL_L2_0 = 20,
|
|
|
|
GST_H264_LEVEL_L2_1 = 21,
|
|
|
|
GST_H264_LEVEL_L2_2 = 22,
|
|
|
|
GST_H264_LEVEL_L3 = 30,
|
|
|
|
GST_H264_LEVEL_L3_1 = 31,
|
|
|
|
GST_H264_LEVEL_L3_2 = 32,
|
|
|
|
GST_H264_LEVEL_L4 = 40,
|
|
|
|
GST_H264_LEVEL_L4_1 = 41,
|
|
|
|
GST_H264_LEVEL_L4_2 = 42,
|
|
|
|
GST_H264_LEVEL_L5 = 50,
|
|
|
|
GST_H264_LEVEL_L5_1 = 51,
|
|
|
|
GST_H264_LEVEL_L5_2 = 52,
|
|
|
|
GST_H264_LEVEL_L6 = 60,
|
|
|
|
GST_H264_LEVEL_L6_1 = 61,
|
|
|
|
GST_H264_LEVEL_L6_2 = 62,
|
2020-03-05 05:40:28 +00:00
|
|
|
} GstH264DecoderLevel;
|
2019-12-26 05:24:46 +00:00
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
2020-03-05 05:40:28 +00:00
|
|
|
GstH264DecoderLevel level;
|
2019-12-26 05:24:46 +00:00
|
|
|
|
|
|
|
guint32 max_mbps;
|
|
|
|
guint32 max_fs;
|
|
|
|
guint32 max_dpb_mbs;
|
|
|
|
guint32 max_main_br;
|
|
|
|
} LevelLimits;
|
|
|
|
|
|
|
|
static const LevelLimits level_limits_map[] = {
|
|
|
|
{GST_H264_LEVEL_L1, 1485, 99, 396, 64},
|
|
|
|
{GST_H264_LEVEL_L1B, 1485, 99, 396, 128},
|
|
|
|
{GST_H264_LEVEL_L1_1, 3000, 396, 900, 192},
|
|
|
|
{GST_H264_LEVEL_L1_2, 6000, 396, 2376, 384},
|
|
|
|
{GST_H264_LEVEL_L1_3, 11800, 396, 2376, 768},
|
|
|
|
{GST_H264_LEVEL_L2_0, 11880, 396, 2376, 2000},
|
|
|
|
{GST_H264_LEVEL_L2_1, 19800, 792, 4752, 4000},
|
|
|
|
{GST_H264_LEVEL_L2_2, 20250, 1620, 8100, 4000},
|
|
|
|
{GST_H264_LEVEL_L3, 40500, 1620, 8100, 10000},
|
|
|
|
{GST_H264_LEVEL_L3_1, 108000, 3600, 18000, 14000},
|
|
|
|
{GST_H264_LEVEL_L3_2, 216000, 5120, 20480, 20000},
|
|
|
|
{GST_H264_LEVEL_L4, 245760, 8192, 32768, 20000},
|
|
|
|
{GST_H264_LEVEL_L4_1, 245760, 8192, 32768, 50000},
|
|
|
|
{GST_H264_LEVEL_L4_2, 522240, 8704, 34816, 50000},
|
|
|
|
{GST_H264_LEVEL_L5, 589824, 22080, 110400, 135000},
|
|
|
|
{GST_H264_LEVEL_L5_1, 983040, 36864, 184320, 240000},
|
|
|
|
{GST_H264_LEVEL_L5_2, 2073600, 36864, 184320, 240000},
|
|
|
|
{GST_H264_LEVEL_L6, 4177920, 139264, 696320, 240000},
|
|
|
|
{GST_H264_LEVEL_L6_1, 8355840, 139264, 696320, 480000},
|
|
|
|
{GST_H264_LEVEL_L6_2, 16711680, 139264, 696320, 800000}
|
|
|
|
};
|
|
|
|
|
|
|
|
static gint
|
2020-03-05 05:40:28 +00:00
|
|
|
h264_level_to_max_dpb_mbs (GstH264DecoderLevel level)
|
2019-12-26 05:24:46 +00:00
|
|
|
{
|
|
|
|
gint i;
|
|
|
|
for (i = 0; i < G_N_ELEMENTS (level_limits_map); i++) {
|
|
|
|
if (level == level_limits_map[i].level)
|
|
|
|
return level_limits_map[i].max_dpb_mbs;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-22 10:44:16 +00:00
|
|
|
static void
|
|
|
|
gst_h264_decoder_set_latency (GstH264Decoder * self, const GstH264SPS * sps,
|
|
|
|
gint max_dpb_size)
|
|
|
|
{
|
|
|
|
GstH264DecoderPrivate *priv = self->priv;
|
|
|
|
GstCaps *caps;
|
|
|
|
GstClockTime min, max;
|
|
|
|
GstStructure *structure;
|
|
|
|
gint fps_d = 1, fps_n = 0;
|
2021-10-04 17:07:57 +00:00
|
|
|
GstH264DpbBumpMode bump_level;
|
|
|
|
guint32 frames_delay;
|
2020-08-22 10:44:16 +00:00
|
|
|
|
|
|
|
caps = gst_pad_get_current_caps (GST_VIDEO_DECODER_SRC_PAD (self));
|
|
|
|
if (!caps)
|
|
|
|
return;
|
|
|
|
|
|
|
|
structure = gst_caps_get_structure (caps, 0);
|
|
|
|
if (gst_structure_get_fraction (structure, "framerate", &fps_n, &fps_d)) {
|
|
|
|
if (fps_n == 0) {
|
|
|
|
/* variable framerate: see if we have a max-framerate */
|
|
|
|
gst_structure_get_fraction (structure, "max-framerate", &fps_n, &fps_d);
|
|
|
|
}
|
|
|
|
}
|
2020-09-23 08:58:43 +00:00
|
|
|
gst_caps_unref (caps);
|
2020-08-22 10:44:16 +00:00
|
|
|
|
|
|
|
/* if no fps or variable, then 25/1 */
|
|
|
|
if (fps_n == 0) {
|
|
|
|
fps_n = 25;
|
|
|
|
fps_d = 1;
|
|
|
|
}
|
|
|
|
|
2021-10-04 17:07:57 +00:00
|
|
|
bump_level = get_bump_level (self);
|
|
|
|
frames_delay = 0;
|
|
|
|
switch (bump_level) {
|
|
|
|
case GST_H264_DPB_BUMP_NORMAL_LATENCY:
|
|
|
|
/* We always wait the DPB full before bumping. */
|
|
|
|
frames_delay = max_dpb_size;
|
|
|
|
break;
|
|
|
|
case GST_H264_DPB_BUMP_LOW_LATENCY:
|
|
|
|
/* We bump the IDR if the second frame is not a minus POC. */
|
|
|
|
frames_delay = 1;
|
|
|
|
break;
|
|
|
|
case GST_H264_DPB_BUMP_VERY_LOW_LATENCY:
|
|
|
|
/* We bump the IDR immediately. */
|
|
|
|
frames_delay = 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
break;
|
|
|
|
}
|
2020-08-22 10:44:16 +00:00
|
|
|
|
2020-12-29 10:54:35 +00:00
|
|
|
/* Consider output delay wanted by subclass */
|
2021-10-04 17:07:57 +00:00
|
|
|
frames_delay += priv->preferred_output_delay;
|
2020-12-29 10:54:35 +00:00
|
|
|
|
2021-10-04 17:07:57 +00:00
|
|
|
min = gst_util_uint64_scale_int (frames_delay * GST_SECOND, fps_d, fps_n);
|
2020-12-29 10:54:35 +00:00
|
|
|
max = gst_util_uint64_scale_int ((max_dpb_size + priv->preferred_output_delay)
|
|
|
|
* GST_SECOND, fps_d, fps_n);
|
2020-08-22 10:44:16 +00:00
|
|
|
|
|
|
|
GST_LOG_OBJECT (self,
|
|
|
|
"latency min %" G_GUINT64_FORMAT " max %" G_GUINT64_FORMAT, min, max);
|
|
|
|
|
|
|
|
gst_video_decoder_set_latency (GST_VIDEO_DECODER (self), min, max);
|
|
|
|
}
|
|
|
|
|
2021-09-21 13:21:51 +00:00
|
|
|
static GstFlowReturn
|
2019-12-26 05:24:46 +00:00
|
|
|
gst_h264_decoder_process_sps (GstH264Decoder * self, GstH264SPS * sps)
|
|
|
|
{
|
2020-11-04 18:47:35 +00:00
|
|
|
GstH264DecoderClass *klass = GST_H264_DECODER_GET_CLASS (self);
|
2019-12-26 05:24:46 +00:00
|
|
|
GstH264DecoderPrivate *priv = self->priv;
|
|
|
|
guint8 level;
|
|
|
|
gint max_dpb_mbs;
|
|
|
|
gint width_mb, height_mb;
|
|
|
|
gint max_dpb_frames;
|
|
|
|
gint max_dpb_size;
|
|
|
|
gint prev_max_dpb_size;
|
2020-11-09 16:28:03 +00:00
|
|
|
gboolean prev_interlaced;
|
|
|
|
gboolean interlaced;
|
2021-09-21 13:21:51 +00:00
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
2019-12-26 05:24:46 +00:00
|
|
|
|
2020-11-09 16:28:03 +00:00
|
|
|
if (sps->frame_mbs_only_flag == 0) {
|
|
|
|
if (!klass->new_field_picture) {
|
|
|
|
GST_FIXME_OBJECT (self,
|
|
|
|
"frame_mbs_only_flag != 1 not supported by subclass");
|
2021-09-21 13:21:51 +00:00
|
|
|
return GST_FLOW_NOT_NEGOTIATED;
|
2020-11-09 16:28:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (sps->mb_adaptive_frame_field_flag) {
|
|
|
|
GST_LOG_OBJECT (self,
|
|
|
|
"mb_adaptive_frame_field_flag == 1, MBAFF sequence");
|
|
|
|
} else {
|
|
|
|
GST_LOG_OBJECT (self, "mb_adaptive_frame_field_flag == 0, PAFF sequence");
|
|
|
|
}
|
2019-12-26 05:24:46 +00:00
|
|
|
}
|
|
|
|
|
2020-11-09 16:28:03 +00:00
|
|
|
interlaced = !sps->frame_mbs_only_flag;
|
|
|
|
|
2019-12-26 05:24:46 +00:00
|
|
|
/* Spec A.3.1 and A.3.2
|
|
|
|
* For Baseline, Constrained Baseline and Main profile, the indicated level is
|
|
|
|
* Level 1b if level_idc is equal to 11 and constraint_set3_flag is equal to 1
|
|
|
|
*/
|
|
|
|
level = sps->level_idc;
|
|
|
|
if (level == 11 && (sps->profile_idc == 66 || sps->profile_idc == 77) &&
|
|
|
|
sps->constraint_set3_flag) {
|
codecs: h264decoder: update max_dpb_frames only if VUI is present
There are some streams, with HRD, where the the calculated
max_dpb_frames is zero (max_dpb_mbs is less than size mb). In order to
get the dbp size it is required to rely on the VUI parameters if they
are present.
According to the spec Annex E.2.1
**max_dec_frame_buffering** specifies the required size of the HRD
decoded picture buffer (DPB) in units of frame buffers. It is a
requirement of bitstream conformance that the coded video sequence
shall not require a decoded picture buffer with size of more than
Max(1, max_dec_frame_buffering) frame buffers to enable the output of
decoded pictures at the output times specified by dpb_output_delay of
the picture timing SEI messages. The value of max_dec_frame_buffering
shall be greater than or equal to max_num_ref_frames. An upper bound
for the value of max_dec_frame_buffering is specified by the level
limits in clauses A.3.1, A.3.2, G.10.2.1, and H.10.2.
When the max_dec_frame_buffering syntax element is not present, the
value of max_dec_frame_buffering shall be inferred as follows:
– If profile_idc is equal to 44, 86, 100, 110, 122, or 244 and
constraint_set3_flag is equal to 1, the value of
max_dec_frame_buffering shall be inferred to be equal to 0.
– Otherwise (profile_idc is not equal to 44, 86, 100, 110, 122, or 244
or constraint_set3_flag is equal to 0), the value of
max_dec_frame_buffering shall be inferred to be equal to MaxDpbFrames.
Part-of: <https://gitlab.freedesktop.org/gstreamer/gst-plugins-bad/-/merge_requests/1381>
2020-06-27 11:18:34 +00:00
|
|
|
/* Level 1b */
|
2019-12-26 05:24:46 +00:00
|
|
|
level = 9;
|
|
|
|
}
|
|
|
|
|
2020-03-05 05:40:28 +00:00
|
|
|
max_dpb_mbs = h264_level_to_max_dpb_mbs ((GstH264DecoderLevel) level);
|
2019-12-26 05:24:46 +00:00
|
|
|
if (!max_dpb_mbs)
|
2021-09-21 13:21:51 +00:00
|
|
|
return GST_FLOW_ERROR;
|
2019-12-26 05:24:46 +00:00
|
|
|
|
|
|
|
width_mb = sps->width / 16;
|
|
|
|
height_mb = sps->height / 16;
|
|
|
|
|
|
|
|
max_dpb_frames = MIN (max_dpb_mbs / (width_mb * height_mb),
|
|
|
|
GST_H264_DPB_MAX_SIZE);
|
|
|
|
|
codecs: h264decoder: update max_dpb_frames only if VUI is present
There are some streams, with HRD, where the the calculated
max_dpb_frames is zero (max_dpb_mbs is less than size mb). In order to
get the dbp size it is required to rely on the VUI parameters if they
are present.
According to the spec Annex E.2.1
**max_dec_frame_buffering** specifies the required size of the HRD
decoded picture buffer (DPB) in units of frame buffers. It is a
requirement of bitstream conformance that the coded video sequence
shall not require a decoded picture buffer with size of more than
Max(1, max_dec_frame_buffering) frame buffers to enable the output of
decoded pictures at the output times specified by dpb_output_delay of
the picture timing SEI messages. The value of max_dec_frame_buffering
shall be greater than or equal to max_num_ref_frames. An upper bound
for the value of max_dec_frame_buffering is specified by the level
limits in clauses A.3.1, A.3.2, G.10.2.1, and H.10.2.
When the max_dec_frame_buffering syntax element is not present, the
value of max_dec_frame_buffering shall be inferred as follows:
– If profile_idc is equal to 44, 86, 100, 110, 122, or 244 and
constraint_set3_flag is equal to 1, the value of
max_dec_frame_buffering shall be inferred to be equal to 0.
– Otherwise (profile_idc is not equal to 44, 86, 100, 110, 122, or 244
or constraint_set3_flag is equal to 0), the value of
max_dec_frame_buffering shall be inferred to be equal to MaxDpbFrames.
Part-of: <https://gitlab.freedesktop.org/gstreamer/gst-plugins-bad/-/merge_requests/1381>
2020-06-27 11:18:34 +00:00
|
|
|
if (sps->vui_parameters_present_flag
|
|
|
|
&& sps->vui_parameters.bitstream_restriction_flag)
|
|
|
|
max_dpb_frames = MAX (1, sps->vui_parameters.max_dec_frame_buffering);
|
|
|
|
|
2020-06-29 21:23:07 +00:00
|
|
|
/* Case 1) There might be some non-conforming streams that require more DPB
|
|
|
|
* size than that of specified one by SPS
|
|
|
|
* Case 2) If bitstream_restriction_flag is not present,
|
|
|
|
* max_dec_frame_buffering should be inferred
|
|
|
|
* to be equal to MaxDpbFrames, then MaxDpbFrames can exceed num_ref_frames
|
|
|
|
* See https://chromium-review.googlesource.com/c/chromium/src/+/760276/
|
|
|
|
*/
|
|
|
|
max_dpb_size = MAX (max_dpb_frames, sps->num_ref_frames);
|
|
|
|
if (max_dpb_size > GST_H264_DPB_MAX_SIZE) {
|
|
|
|
GST_WARNING_OBJECT (self, "Too large calculated DPB size %d", max_dpb_size);
|
|
|
|
max_dpb_size = GST_H264_DPB_MAX_SIZE;
|
|
|
|
}
|
2019-12-26 05:24:46 +00:00
|
|
|
|
2020-04-06 19:06:01 +00:00
|
|
|
/* Safety, so that subclass don't need bound checking */
|
2021-09-21 13:21:51 +00:00
|
|
|
g_return_val_if_fail (max_dpb_size <= GST_H264_DPB_MAX_SIZE, GST_FLOW_ERROR);
|
2020-04-06 19:06:01 +00:00
|
|
|
|
2020-11-04 19:16:54 +00:00
|
|
|
prev_max_dpb_size = gst_h264_dpb_get_max_num_frames (priv->dpb);
|
2020-11-09 16:28:03 +00:00
|
|
|
prev_interlaced = gst_h264_dpb_get_interlaced (priv->dpb);
|
2019-12-26 05:24:46 +00:00
|
|
|
if (priv->width != sps->width || priv->height != sps->height ||
|
2020-11-09 16:28:03 +00:00
|
|
|
prev_max_dpb_size != max_dpb_size || prev_interlaced != interlaced) {
|
2019-12-26 05:24:46 +00:00
|
|
|
GstH264DecoderClass *klass = GST_H264_DECODER_GET_CLASS (self);
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (self,
|
2020-11-09 16:28:03 +00:00
|
|
|
"SPS updated, resolution: %dx%d -> %dx%d, dpb size: %d -> %d, "
|
|
|
|
"interlaced %d -> %d",
|
2019-12-26 05:24:46 +00:00
|
|
|
priv->width, priv->height, sps->width, sps->height,
|
2020-11-09 16:28:03 +00:00
|
|
|
prev_max_dpb_size, max_dpb_size, prev_interlaced, interlaced);
|
2019-12-26 05:24:46 +00:00
|
|
|
|
2021-09-21 13:21:51 +00:00
|
|
|
ret = gst_h264_decoder_drain (GST_VIDEO_DECODER (self));
|
|
|
|
if (ret != GST_FLOW_OK)
|
|
|
|
return ret;
|
2019-12-26 05:24:46 +00:00
|
|
|
|
|
|
|
g_assert (klass->new_sequence);
|
|
|
|
|
2020-12-29 10:54:35 +00:00
|
|
|
if (klass->get_preferred_output_delay) {
|
|
|
|
priv->preferred_output_delay =
|
|
|
|
klass->get_preferred_output_delay (self, priv->is_live);
|
|
|
|
} else {
|
|
|
|
priv->preferred_output_delay = 0;
|
|
|
|
}
|
|
|
|
|
2021-09-21 13:21:51 +00:00
|
|
|
ret = klass->new_sequence (self,
|
|
|
|
sps, max_dpb_size + priv->preferred_output_delay);
|
|
|
|
if (ret != GST_FLOW_OK) {
|
|
|
|
GST_WARNING_OBJECT (self, "subclass does not want accept new sequence");
|
|
|
|
return ret;
|
2019-12-26 05:24:46 +00:00
|
|
|
}
|
|
|
|
|
2021-07-28 15:19:15 +00:00
|
|
|
priv->profile_idc = sps->profile_idc;
|
2019-12-26 05:24:46 +00:00
|
|
|
priv->width = sps->width;
|
|
|
|
priv->height = sps->height;
|
|
|
|
|
2020-08-22 10:44:16 +00:00
|
|
|
gst_h264_decoder_set_latency (self, sps, max_dpb_size);
|
2020-11-04 19:16:54 +00:00
|
|
|
gst_h264_dpb_set_max_num_frames (priv->dpb, max_dpb_size);
|
2020-11-09 16:28:03 +00:00
|
|
|
gst_h264_dpb_set_interlaced (priv->dpb, interlaced);
|
2019-12-26 05:24:46 +00:00
|
|
|
}
|
|
|
|
|
2021-09-21 13:21:51 +00:00
|
|
|
if (!gst_h264_decoder_update_max_num_reorder_frames (self, sps))
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
|
|
|
|
return GST_FLOW_OK;
|
2019-12-26 05:24:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_h264_decoder_init_gap_picture (GstH264Decoder * self,
|
|
|
|
GstH264Picture * picture, gint frame_num)
|
|
|
|
{
|
|
|
|
picture->nonexisting = TRUE;
|
|
|
|
picture->nal_ref_idc = 1;
|
|
|
|
picture->frame_num = picture->pic_num = frame_num;
|
|
|
|
picture->dec_ref_pic_marking.adaptive_ref_pic_marking_mode_flag = FALSE;
|
2020-11-05 16:45:36 +00:00
|
|
|
picture->ref = GST_H264_PICTURE_REF_SHORT_TERM;
|
2021-07-20 15:36:38 +00:00
|
|
|
picture->ref_pic = TRUE;
|
2019-12-26 05:24:46 +00:00
|
|
|
picture->dec_ref_pic_marking.long_term_reference_flag = FALSE;
|
|
|
|
picture->field = GST_H264_PICTURE_FIELD_FRAME;
|
|
|
|
|
|
|
|
return gst_h264_decoder_calculate_poc (self, picture);
|
|
|
|
}
|
|
|
|
|
2021-10-06 16:54:29 +00:00
|
|
|
static GstFlowReturn
|
2019-12-26 05:24:46 +00:00
|
|
|
gst_h264_decoder_decode_slice (GstH264Decoder * self)
|
|
|
|
{
|
|
|
|
GstH264DecoderClass *klass = GST_H264_DECODER_GET_CLASS (self);
|
|
|
|
GstH264DecoderPrivate *priv = self->priv;
|
|
|
|
GstH264Slice *slice = &priv->current_slice;
|
|
|
|
GstH264Picture *picture = priv->current_picture;
|
2020-04-27 14:53:45 +00:00
|
|
|
GArray *ref_pic_list0 = NULL;
|
|
|
|
GArray *ref_pic_list1 = NULL;
|
2021-10-06 16:54:29 +00:00
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
2019-12-26 05:24:46 +00:00
|
|
|
|
|
|
|
if (!picture) {
|
|
|
|
GST_ERROR_OBJECT (self, "No current picture");
|
2021-10-06 16:54:29 +00:00
|
|
|
return GST_FLOW_ERROR;
|
2019-12-26 05:24:46 +00:00
|
|
|
}
|
|
|
|
|
2020-04-20 07:00:30 +00:00
|
|
|
GST_LOG_OBJECT (self, "Decode picture %p (frame_num %d, poc %d)",
|
|
|
|
picture, picture->frame_num, picture->pic_order_cnt);
|
|
|
|
|
2020-05-12 16:23:15 +00:00
|
|
|
priv->max_pic_num = slice->header.max_pic_num;
|
2019-12-26 05:24:46 +00:00
|
|
|
|
2020-04-27 14:53:45 +00:00
|
|
|
if (priv->process_ref_pic_lists) {
|
2021-10-06 16:54:29 +00:00
|
|
|
if (!gst_h264_decoder_modify_ref_pic_lists (self)) {
|
|
|
|
ret = GST_FLOW_ERROR;
|
2020-04-27 14:53:45 +00:00
|
|
|
goto beach;
|
2021-10-06 16:54:29 +00:00
|
|
|
}
|
2020-04-27 14:53:45 +00:00
|
|
|
|
|
|
|
ref_pic_list0 = priv->ref_pic_list0;
|
|
|
|
ref_pic_list1 = priv->ref_pic_list1;
|
|
|
|
}
|
|
|
|
|
2019-12-26 05:24:46 +00:00
|
|
|
g_assert (klass->decode_slice);
|
|
|
|
|
2020-05-03 15:30:34 +00:00
|
|
|
ret = klass->decode_slice (self, picture, slice, ref_pic_list0,
|
|
|
|
ref_pic_list1);
|
2021-10-06 16:54:29 +00:00
|
|
|
if (ret != GST_FLOW_OK) {
|
2020-04-20 07:00:30 +00:00
|
|
|
GST_WARNING_OBJECT (self,
|
|
|
|
"Subclass didn't want to decode picture %p (frame_num %d, poc %d)",
|
|
|
|
picture, picture->frame_num, picture->pic_order_cnt);
|
|
|
|
}
|
|
|
|
|
2020-04-27 14:53:45 +00:00
|
|
|
beach:
|
|
|
|
g_array_set_size (priv->ref_pic_list0, 0);
|
|
|
|
g_array_set_size (priv->ref_pic_list1, 0);
|
|
|
|
|
2020-04-20 07:00:30 +00:00
|
|
|
return ret;
|
2019-12-26 05:24:46 +00:00
|
|
|
}
|
2020-04-27 14:53:45 +00:00
|
|
|
|
|
|
|
static gint
|
|
|
|
pic_num_desc_compare (const GstH264Picture ** a, const GstH264Picture ** b)
|
|
|
|
{
|
|
|
|
return (*b)->pic_num - (*a)->pic_num;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gint
|
|
|
|
long_term_pic_num_asc_compare (const GstH264Picture ** a,
|
|
|
|
const GstH264Picture ** b)
|
|
|
|
{
|
|
|
|
return (*a)->long_term_pic_num - (*b)->long_term_pic_num;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-11-17 09:39:56 +00:00
|
|
|
construct_ref_pic_lists_p (GstH264Decoder * self,
|
|
|
|
GstH264Picture * current_picture)
|
2020-04-27 14:53:45 +00:00
|
|
|
{
|
|
|
|
GstH264DecoderPrivate *priv = self->priv;
|
|
|
|
gint pos;
|
|
|
|
|
|
|
|
/* RefPicList0 (8.2.4.2.1) [[1] [2]], where:
|
|
|
|
* [1] shortterm ref pics sorted by descending pic_num,
|
|
|
|
* [2] longterm ref pics by ascending long_term_pic_num.
|
|
|
|
*/
|
|
|
|
g_array_set_size (priv->ref_pic_list_p0, 0);
|
|
|
|
|
2020-11-17 09:39:56 +00:00
|
|
|
gst_h264_dpb_get_pictures_short_term_ref (priv->dpb,
|
|
|
|
TRUE, FALSE, priv->ref_pic_list_p0);
|
2020-04-27 14:53:45 +00:00
|
|
|
g_array_sort (priv->ref_pic_list_p0, (GCompareFunc) pic_num_desc_compare);
|
|
|
|
|
|
|
|
pos = priv->ref_pic_list_p0->len;
|
2020-11-17 09:39:56 +00:00
|
|
|
gst_h264_dpb_get_pictures_long_term_ref (priv->dpb,
|
|
|
|
FALSE, priv->ref_pic_list_p0);
|
2020-04-27 14:53:45 +00:00
|
|
|
g_qsort_with_data (&g_array_index (priv->ref_pic_list_p0, gpointer, pos),
|
|
|
|
priv->ref_pic_list_p0->len - pos, sizeof (gpointer),
|
|
|
|
(GCompareDataFunc) long_term_pic_num_asc_compare, NULL);
|
|
|
|
|
|
|
|
#ifndef GST_DISABLE_GST_DEBUG
|
|
|
|
if (gst_debug_category_get_threshold (GST_CAT_DEFAULT) >= GST_LEVEL_DEBUG) {
|
|
|
|
GString *str = g_string_new (NULL);
|
|
|
|
for (pos = 0; pos < priv->ref_pic_list_p0->len; pos++) {
|
|
|
|
GstH264Picture *ref =
|
|
|
|
g_array_index (priv->ref_pic_list_p0, GstH264Picture *, pos);
|
2020-11-05 16:45:36 +00:00
|
|
|
if (!GST_H264_PICTURE_IS_LONG_TERM_REF (ref))
|
2020-04-27 14:53:45 +00:00
|
|
|
g_string_append_printf (str, "|%i", ref->pic_num);
|
|
|
|
else
|
|
|
|
g_string_append_printf (str, "|%is", ref->pic_num);
|
|
|
|
}
|
|
|
|
GST_DEBUG_OBJECT (self, "ref_pic_list_p0: %s|", str->str);
|
|
|
|
g_string_free (str, TRUE);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-11-14 15:20:54 +00:00
|
|
|
static gint
|
|
|
|
frame_num_wrap_desc_compare (const GstH264Picture ** a,
|
|
|
|
const GstH264Picture ** b)
|
|
|
|
{
|
|
|
|
return (*b)->frame_num_wrap - (*a)->frame_num_wrap;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gint
|
|
|
|
long_term_frame_idx_asc_compare (const GstH264Picture ** a,
|
|
|
|
const GstH264Picture ** b)
|
|
|
|
{
|
|
|
|
return (*a)->long_term_frame_idx - (*b)->long_term_frame_idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* init_picture_refs_fields_1 in gstvaapidecoder_h264.c */
|
|
|
|
static void
|
|
|
|
init_picture_refs_fields_1 (GstH264Decoder * self, GstH264PictureField field,
|
|
|
|
GArray * ref_frame_list, GArray * ref_pic_list_x)
|
|
|
|
{
|
|
|
|
guint i = 0, j = 0;
|
|
|
|
|
|
|
|
do {
|
|
|
|
for (; i < ref_frame_list->len; i++) {
|
|
|
|
GstH264Picture *pic = g_array_index (ref_frame_list, GstH264Picture *, i);
|
|
|
|
if (pic->field == field) {
|
|
|
|
pic = gst_h264_picture_ref (pic);
|
|
|
|
g_array_append_val (ref_pic_list_x, pic);
|
|
|
|
i++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (; j < ref_frame_list->len; j++) {
|
|
|
|
GstH264Picture *pic = g_array_index (ref_frame_list, GstH264Picture *, j);
|
|
|
|
if (pic->field != field) {
|
|
|
|
pic = gst_h264_picture_ref (pic);
|
|
|
|
g_array_append_val (ref_pic_list_x, pic);
|
|
|
|
j++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (i < ref_frame_list->len || j < ref_frame_list->len);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
construct_ref_field_pic_lists_p (GstH264Decoder * self,
|
|
|
|
GstH264Picture * current_picture)
|
|
|
|
{
|
|
|
|
GstH264DecoderPrivate *priv = self->priv;
|
|
|
|
gint pos;
|
|
|
|
|
|
|
|
g_array_set_size (priv->ref_pic_list_p0, 0);
|
|
|
|
g_array_set_size (priv->ref_frame_list_0_short_term, 0);
|
|
|
|
g_array_set_size (priv->ref_frame_list_long_term, 0);
|
|
|
|
|
|
|
|
/* 8.2.4.2.2, 8.2.4.2.5 refFrameList0ShortTerm:
|
|
|
|
* short-term ref pictures sorted by descending frame_num_wrap.
|
|
|
|
*/
|
|
|
|
gst_h264_dpb_get_pictures_short_term_ref (priv->dpb,
|
|
|
|
TRUE, TRUE, priv->ref_frame_list_0_short_term);
|
|
|
|
g_array_sort (priv->ref_frame_list_0_short_term,
|
|
|
|
(GCompareFunc) frame_num_wrap_desc_compare);
|
|
|
|
|
|
|
|
#ifndef GST_DISABLE_GST_DEBUG
|
|
|
|
if (gst_debug_category_get_threshold (GST_CAT_DEFAULT) >= GST_LEVEL_TRACE
|
|
|
|
&& priv->ref_frame_list_0_short_term->len) {
|
|
|
|
GString *str = g_string_new (NULL);
|
|
|
|
for (pos = 0; pos < priv->ref_frame_list_0_short_term->len; pos++) {
|
|
|
|
GstH264Picture *ref = g_array_index (priv->ref_frame_list_0_short_term,
|
|
|
|
GstH264Picture *, pos);
|
|
|
|
g_string_append_printf (str, "|%i(%d)", ref->frame_num_wrap, ref->field);
|
|
|
|
}
|
|
|
|
GST_TRACE_OBJECT (self, "ref_frame_list_0_short_term (%d): %s|",
|
|
|
|
current_picture->field, str->str);
|
|
|
|
g_string_free (str, TRUE);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* 8.2.4.2.2 refFrameList0LongTerm,:
|
|
|
|
* long-term ref pictures sorted by ascending long_term_frame_idx.
|
|
|
|
*/
|
|
|
|
gst_h264_dpb_get_pictures_long_term_ref (priv->dpb,
|
|
|
|
TRUE, priv->ref_frame_list_long_term);
|
|
|
|
g_array_sort (priv->ref_frame_list_long_term,
|
|
|
|
(GCompareFunc) long_term_frame_idx_asc_compare);
|
|
|
|
|
|
|
|
#ifndef GST_DISABLE_GST_DEBUG
|
|
|
|
if (gst_debug_category_get_threshold (GST_CAT_DEFAULT) >= GST_LEVEL_TRACE
|
|
|
|
&& priv->ref_frame_list_long_term->len) {
|
|
|
|
GString *str = g_string_new (NULL);
|
|
|
|
for (pos = 0; pos < priv->ref_frame_list_long_term->len; pos++) {
|
|
|
|
GstH264Picture *ref = g_array_index (priv->ref_frame_list_0_short_term,
|
|
|
|
GstH264Picture *, pos);
|
|
|
|
g_string_append_printf (str, "|%i(%d)", ref->long_term_frame_idx,
|
|
|
|
ref->field);
|
|
|
|
}
|
|
|
|
GST_TRACE_OBJECT (self, "ref_frame_list_0_long_term (%d): %s|",
|
|
|
|
current_picture->field, str->str);
|
|
|
|
g_string_free (str, TRUE);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* 8.2.4.2.5 */
|
|
|
|
init_picture_refs_fields_1 (self, current_picture->field,
|
|
|
|
priv->ref_frame_list_0_short_term, priv->ref_pic_list_p0);
|
|
|
|
init_picture_refs_fields_1 (self, current_picture->field,
|
|
|
|
priv->ref_frame_list_long_term, priv->ref_pic_list_p0);
|
|
|
|
|
|
|
|
#ifndef GST_DISABLE_GST_DEBUG
|
|
|
|
if (gst_debug_category_get_threshold (GST_CAT_DEFAULT) >= GST_LEVEL_DEBUG
|
|
|
|
&& priv->ref_pic_list_p0->len) {
|
|
|
|
GString *str = g_string_new (NULL);
|
|
|
|
for (pos = 0; pos < priv->ref_pic_list_p0->len; pos++) {
|
|
|
|
GstH264Picture *ref =
|
|
|
|
g_array_index (priv->ref_pic_list_p0, GstH264Picture *, pos);
|
|
|
|
if (!GST_H264_PICTURE_IS_LONG_TERM_REF (ref))
|
|
|
|
g_string_append_printf (str, "|%i(%d)s", ref->frame_num_wrap,
|
|
|
|
ref->field);
|
|
|
|
else
|
|
|
|
g_string_append_printf (str, "|%i(%d)l", ref->long_term_frame_idx,
|
|
|
|
ref->field);
|
|
|
|
}
|
|
|
|
GST_DEBUG_OBJECT (self, "ref_pic_list_p0 (%d): %s|", current_picture->field,
|
|
|
|
str->str);
|
|
|
|
g_string_free (str, TRUE);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Clear temporary lists, now pictures are owned by ref_pic_list_p0 */
|
|
|
|
g_array_set_size (priv->ref_frame_list_0_short_term, 0);
|
|
|
|
g_array_set_size (priv->ref_frame_list_long_term, 0);
|
|
|
|
}
|
|
|
|
|
2020-04-27 14:53:45 +00:00
|
|
|
static gboolean
|
|
|
|
lists_are_equal (GArray * l1, GArray * l2)
|
|
|
|
{
|
|
|
|
gint i;
|
|
|
|
|
|
|
|
if (l1->len != l2->len)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
for (i = 0; i < l1->len; i++)
|
|
|
|
if (g_array_index (l1, gpointer, i) != g_array_index (l2, gpointer, i))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gint
|
|
|
|
split_ref_pic_list_b (GstH264Decoder * self, GArray * ref_pic_list_b,
|
|
|
|
GCompareFunc compare_func)
|
|
|
|
{
|
|
|
|
gint pos;
|
|
|
|
|
|
|
|
for (pos = 0; pos < ref_pic_list_b->len; pos++) {
|
|
|
|
GstH264Picture *pic = g_array_index (ref_pic_list_b, GstH264Picture *, pos);
|
|
|
|
if (compare_func (&pic, &self->priv->current_picture) > 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-07-23 14:02:05 +00:00
|
|
|
print_ref_pic_list_b (GstH264Decoder * self, GArray * ref_list_b,
|
|
|
|
const gchar * name)
|
2020-04-27 14:53:45 +00:00
|
|
|
{
|
|
|
|
#ifndef GST_DISABLE_GST_DEBUG
|
|
|
|
GString *str;
|
|
|
|
gint i;
|
|
|
|
|
|
|
|
if (gst_debug_category_get_threshold (GST_CAT_DEFAULT) < GST_LEVEL_DEBUG)
|
|
|
|
return;
|
|
|
|
|
|
|
|
str = g_string_new (NULL);
|
|
|
|
|
|
|
|
for (i = 0; i < ref_list_b->len; i++) {
|
|
|
|
GstH264Picture *ref = g_array_index (ref_list_b, GstH264Picture *, i);
|
|
|
|
|
2020-11-05 16:45:36 +00:00
|
|
|
if (!GST_H264_PICTURE_IS_LONG_TERM_REF (ref))
|
2020-04-27 14:53:45 +00:00
|
|
|
g_string_append_printf (str, "|%i", ref->pic_order_cnt);
|
|
|
|
else
|
|
|
|
g_string_append_printf (str, "|%il", ref->long_term_pic_num);
|
|
|
|
}
|
|
|
|
|
2021-07-23 14:02:05 +00:00
|
|
|
GST_DEBUG_OBJECT (self, "%s: %s| curr %i", name, str->str,
|
2020-04-27 14:53:45 +00:00
|
|
|
self->priv->current_picture->pic_order_cnt);
|
|
|
|
g_string_free (str, TRUE);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-11-17 09:39:56 +00:00
|
|
|
construct_ref_pic_lists_b (GstH264Decoder * self,
|
|
|
|
GstH264Picture * current_picture)
|
2020-04-27 14:53:45 +00:00
|
|
|
{
|
|
|
|
GstH264DecoderPrivate *priv = self->priv;
|
|
|
|
gint pos;
|
|
|
|
|
|
|
|
/* RefPicList0 (8.2.4.2.3) [[1] [2] [3]], where:
|
|
|
|
* [1] shortterm ref pics with POC < current_picture's POC sorted by descending POC,
|
|
|
|
* [2] shortterm ref pics with POC > current_picture's POC by ascending POC,
|
|
|
|
* [3] longterm ref pics by ascending long_term_pic_num.
|
|
|
|
*/
|
|
|
|
g_array_set_size (priv->ref_pic_list_b0, 0);
|
|
|
|
g_array_set_size (priv->ref_pic_list_b1, 0);
|
2020-11-17 09:39:56 +00:00
|
|
|
|
|
|
|
/* 8.2.4.2.3
|
|
|
|
* When pic_order_cnt_type is equal to 0, reference pictures that are marked
|
|
|
|
* as "non-existing" as specified in clause 8.2.5.2 are not included in either
|
|
|
|
* RefPicList0 or RefPicList1
|
|
|
|
*/
|
|
|
|
gst_h264_dpb_get_pictures_short_term_ref (priv->dpb,
|
|
|
|
current_picture->pic_order_cnt_type != 0, FALSE, priv->ref_pic_list_b0);
|
2020-04-27 14:53:45 +00:00
|
|
|
|
|
|
|
/* First sort ascending, this will put [1] in right place and finish
|
|
|
|
* [2]. */
|
2021-07-23 14:02:05 +00:00
|
|
|
print_ref_pic_list_b (self, priv->ref_pic_list_b0, "ref_pic_list_b0");
|
2020-04-27 14:53:45 +00:00
|
|
|
g_array_sort (priv->ref_pic_list_b0, (GCompareFunc) poc_asc_compare);
|
2021-07-23 14:02:05 +00:00
|
|
|
print_ref_pic_list_b (self, priv->ref_pic_list_b0, "ref_pic_list_b0");
|
2020-04-27 14:53:45 +00:00
|
|
|
|
|
|
|
/* Find first with POC > current_picture's POC to get first element
|
|
|
|
* in [2]... */
|
|
|
|
pos = split_ref_pic_list_b (self, priv->ref_pic_list_b0,
|
|
|
|
(GCompareFunc) poc_asc_compare);
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (self, "split point %i", pos);
|
|
|
|
|
|
|
|
/* and sort [1] descending, thus finishing sequence [1] [2]. */
|
|
|
|
g_qsort_with_data (priv->ref_pic_list_b0->data, pos, sizeof (gpointer),
|
|
|
|
(GCompareDataFunc) poc_desc_compare, NULL);
|
|
|
|
|
|
|
|
/* Now add [3] and sort by ascending long_term_pic_num. */
|
|
|
|
pos = priv->ref_pic_list_b0->len;
|
2020-11-17 09:39:56 +00:00
|
|
|
gst_h264_dpb_get_pictures_long_term_ref (priv->dpb,
|
|
|
|
FALSE, priv->ref_pic_list_b0);
|
2020-04-27 14:53:45 +00:00
|
|
|
g_qsort_with_data (&g_array_index (priv->ref_pic_list_b0, gpointer, pos),
|
|
|
|
priv->ref_pic_list_b0->len - pos, sizeof (gpointer),
|
|
|
|
(GCompareDataFunc) long_term_pic_num_asc_compare, NULL);
|
|
|
|
|
|
|
|
/* RefPicList1 (8.2.4.2.4) [[1] [2] [3]], where:
|
|
|
|
* [1] shortterm ref pics with POC > curr_pic's POC sorted by ascending POC,
|
|
|
|
* [2] shortterm ref pics with POC < curr_pic's POC by descending POC,
|
|
|
|
* [3] longterm ref pics by ascending long_term_pic_num.
|
|
|
|
*/
|
2020-11-17 09:39:56 +00:00
|
|
|
gst_h264_dpb_get_pictures_short_term_ref (priv->dpb,
|
|
|
|
current_picture->pic_order_cnt_type != 0, FALSE, priv->ref_pic_list_b1);
|
2020-04-27 14:53:45 +00:00
|
|
|
|
|
|
|
/* First sort by descending POC. */
|
|
|
|
g_array_sort (priv->ref_pic_list_b1, (GCompareFunc) poc_desc_compare);
|
|
|
|
|
|
|
|
/* Split at first with POC < current_picture's POC to get first element
|
|
|
|
* in [2]... */
|
|
|
|
pos = split_ref_pic_list_b (self, priv->ref_pic_list_b1,
|
|
|
|
(GCompareFunc) poc_desc_compare);
|
|
|
|
|
|
|
|
/* and sort [1] ascending. */
|
|
|
|
g_qsort_with_data (priv->ref_pic_list_b1->data, pos, sizeof (gpointer),
|
|
|
|
(GCompareDataFunc) poc_asc_compare, NULL);
|
|
|
|
|
|
|
|
/* Now add [3] and sort by ascending long_term_pic_num */
|
|
|
|
pos = priv->ref_pic_list_b1->len;
|
2020-11-17 09:39:56 +00:00
|
|
|
gst_h264_dpb_get_pictures_long_term_ref (priv->dpb,
|
|
|
|
FALSE, priv->ref_pic_list_b1);
|
2020-04-27 14:53:45 +00:00
|
|
|
g_qsort_with_data (&g_array_index (priv->ref_pic_list_b1, gpointer, pos),
|
|
|
|
priv->ref_pic_list_b1->len - pos, sizeof (gpointer),
|
|
|
|
(GCompareDataFunc) long_term_pic_num_asc_compare, NULL);
|
|
|
|
|
|
|
|
/* If lists identical, swap first two entries in RefPicList1 (spec
|
|
|
|
* 8.2.4.2.3) */
|
|
|
|
if (priv->ref_pic_list_b1->len > 1
|
|
|
|
&& lists_are_equal (priv->ref_pic_list_b0, priv->ref_pic_list_b1)) {
|
|
|
|
/* swap */
|
|
|
|
GstH264Picture **list = (GstH264Picture **) priv->ref_pic_list_b1->data;
|
|
|
|
GstH264Picture *pic = list[0];
|
|
|
|
list[0] = list[1];
|
|
|
|
list[1] = pic;
|
|
|
|
}
|
|
|
|
|
2021-07-23 14:02:05 +00:00
|
|
|
print_ref_pic_list_b (self, priv->ref_pic_list_b0, "ref_pic_list_b0");
|
|
|
|
print_ref_pic_list_b (self, priv->ref_pic_list_b1, "ref_pic_list_b1");
|
2020-04-27 14:53:45 +00:00
|
|
|
}
|
|
|
|
|
2020-11-14 15:20:54 +00:00
|
|
|
static void
|
|
|
|
construct_ref_field_pic_lists_b (GstH264Decoder * self,
|
|
|
|
GstH264Picture * current_picture)
|
|
|
|
{
|
|
|
|
GstH264DecoderPrivate *priv = self->priv;
|
|
|
|
gint pos;
|
|
|
|
|
|
|
|
/* refFrameList0ShortTerm (8.2.4.2.4) [[1] [2]], where:
|
|
|
|
* [1] shortterm ref pics with POC < current_picture's POC sorted by descending POC,
|
|
|
|
* [2] shortterm ref pics with POC > current_picture's POC by ascending POC,
|
|
|
|
*/
|
|
|
|
g_array_set_size (priv->ref_pic_list_b0, 0);
|
|
|
|
g_array_set_size (priv->ref_pic_list_b1, 0);
|
|
|
|
g_array_set_size (priv->ref_frame_list_0_short_term, 0);
|
|
|
|
g_array_set_size (priv->ref_frame_list_1_short_term, 0);
|
|
|
|
g_array_set_size (priv->ref_frame_list_long_term, 0);
|
|
|
|
|
|
|
|
/* 8.2.4.2.4
|
|
|
|
* When pic_order_cnt_type is equal to 0, reference pictures that are marked
|
|
|
|
* as "non-existing" as specified in clause 8.2.5.2 are not included in either
|
|
|
|
* RefPicList0 or RefPicList1
|
|
|
|
*/
|
|
|
|
gst_h264_dpb_get_pictures_short_term_ref (priv->dpb,
|
|
|
|
current_picture->pic_order_cnt_type != 0, TRUE,
|
|
|
|
priv->ref_frame_list_0_short_term);
|
|
|
|
|
|
|
|
/* First sort ascending, this will put [1] in right place and finish
|
|
|
|
* [2]. */
|
2021-07-23 14:02:05 +00:00
|
|
|
print_ref_pic_list_b (self, priv->ref_frame_list_0_short_term,
|
|
|
|
"ref_frame_list_0_short_term");
|
2021-07-23 04:31:17 +00:00
|
|
|
g_array_sort (priv->ref_frame_list_0_short_term,
|
|
|
|
(GCompareFunc) poc_asc_compare);
|
2021-07-23 14:02:05 +00:00
|
|
|
print_ref_pic_list_b (self, priv->ref_frame_list_0_short_term,
|
|
|
|
"ref_frame_list_0_short_term");
|
2020-11-14 15:20:54 +00:00
|
|
|
|
|
|
|
/* Find first with POC > current_picture's POC to get first element
|
|
|
|
* in [2]... */
|
|
|
|
pos = split_ref_pic_list_b (self, priv->ref_frame_list_0_short_term,
|
|
|
|
(GCompareFunc) poc_asc_compare);
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (self, "split point %i", pos);
|
|
|
|
|
|
|
|
/* and sort [1] descending, thus finishing sequence [1] [2]. */
|
|
|
|
g_qsort_with_data (priv->ref_frame_list_0_short_term->data, pos,
|
|
|
|
sizeof (gpointer), (GCompareDataFunc) poc_desc_compare, NULL);
|
|
|
|
|
|
|
|
/* refFrameList1ShortTerm (8.2.4.2.4) [[1] [2]], where:
|
|
|
|
* [1] shortterm ref pics with POC > curr_pic's POC sorted by ascending POC,
|
|
|
|
* [2] shortterm ref pics with POC < curr_pic's POC by descending POC,
|
|
|
|
*/
|
|
|
|
gst_h264_dpb_get_pictures_short_term_ref (priv->dpb,
|
|
|
|
current_picture->pic_order_cnt_type != 0, TRUE,
|
|
|
|
priv->ref_frame_list_1_short_term);
|
|
|
|
|
|
|
|
/* First sort by descending POC. */
|
|
|
|
g_array_sort (priv->ref_frame_list_1_short_term,
|
|
|
|
(GCompareFunc) poc_desc_compare);
|
|
|
|
|
|
|
|
/* Split at first with POC < current_picture's POC to get first element
|
|
|
|
* in [2]... */
|
|
|
|
pos = split_ref_pic_list_b (self, priv->ref_frame_list_1_short_term,
|
|
|
|
(GCompareFunc) poc_desc_compare);
|
|
|
|
|
|
|
|
/* and sort [1] ascending. */
|
|
|
|
g_qsort_with_data (priv->ref_frame_list_1_short_term->data, pos,
|
|
|
|
sizeof (gpointer), (GCompareDataFunc) poc_asc_compare, NULL);
|
|
|
|
|
|
|
|
/* 8.2.4.2.2 refFrameList0LongTerm,:
|
|
|
|
* long-term ref pictures sorted by ascending long_term_frame_idx.
|
|
|
|
*/
|
|
|
|
gst_h264_dpb_get_pictures_long_term_ref (priv->dpb,
|
|
|
|
TRUE, priv->ref_frame_list_long_term);
|
|
|
|
g_array_sort (priv->ref_frame_list_long_term,
|
|
|
|
(GCompareFunc) long_term_frame_idx_asc_compare);
|
|
|
|
|
|
|
|
/* 8.2.4.2.5 RefPicList0 */
|
|
|
|
init_picture_refs_fields_1 (self, current_picture->field,
|
|
|
|
priv->ref_frame_list_0_short_term, priv->ref_pic_list_b0);
|
|
|
|
init_picture_refs_fields_1 (self, current_picture->field,
|
|
|
|
priv->ref_frame_list_long_term, priv->ref_pic_list_b0);
|
|
|
|
|
|
|
|
/* 8.2.4.2.5 RefPicList1 */
|
|
|
|
init_picture_refs_fields_1 (self, current_picture->field,
|
|
|
|
priv->ref_frame_list_1_short_term, priv->ref_pic_list_b1);
|
|
|
|
init_picture_refs_fields_1 (self, current_picture->field,
|
|
|
|
priv->ref_frame_list_long_term, priv->ref_pic_list_b1);
|
|
|
|
|
|
|
|
/* If lists identical, swap first two entries in RefPicList1 (spec
|
|
|
|
* 8.2.4.2.5) */
|
|
|
|
if (priv->ref_pic_list_b1->len > 1
|
|
|
|
&& lists_are_equal (priv->ref_pic_list_b0, priv->ref_pic_list_b1)) {
|
|
|
|
/* swap */
|
|
|
|
GstH264Picture **list = (GstH264Picture **) priv->ref_pic_list_b1->data;
|
|
|
|
GstH264Picture *pic = list[0];
|
|
|
|
list[0] = list[1];
|
|
|
|
list[1] = pic;
|
|
|
|
}
|
|
|
|
|
2021-07-23 14:02:05 +00:00
|
|
|
print_ref_pic_list_b (self, priv->ref_pic_list_b0, "ref_pic_list_b0");
|
|
|
|
print_ref_pic_list_b (self, priv->ref_pic_list_b1, "ref_pic_list_b1");
|
2020-11-14 15:20:54 +00:00
|
|
|
|
|
|
|
/* Clear temporary lists, now pictures are owned by ref_pic_list_b0
|
|
|
|
* and ref_pic_list_b1 */
|
|
|
|
g_array_set_size (priv->ref_frame_list_0_short_term, 0);
|
|
|
|
g_array_set_size (priv->ref_frame_list_1_short_term, 0);
|
|
|
|
g_array_set_size (priv->ref_frame_list_long_term, 0);
|
|
|
|
}
|
|
|
|
|
2020-04-27 14:53:45 +00:00
|
|
|
static void
|
2020-11-17 09:39:56 +00:00
|
|
|
gst_h264_decoder_prepare_ref_pic_lists (GstH264Decoder * self,
|
|
|
|
GstH264Picture * current_picture)
|
2020-04-27 14:53:45 +00:00
|
|
|
{
|
2020-11-16 18:11:46 +00:00
|
|
|
GstH264DecoderPrivate *priv = self->priv;
|
|
|
|
gboolean construct_list = FALSE;
|
|
|
|
gint i;
|
|
|
|
GArray *dpb_array = gst_h264_dpb_get_pictures_all (priv->dpb);
|
|
|
|
|
|
|
|
/* 8.2.4.2.1 ~ 8.2.4.2.4
|
|
|
|
* When this process is invoked, there shall be at least one reference entry
|
|
|
|
* that is currently marked as "used for reference"
|
|
|
|
* (i.e., as "used for short-term reference" or "used for long-term reference")
|
|
|
|
* and is not marked as "non-existing"
|
|
|
|
*/
|
|
|
|
for (i = 0; i < dpb_array->len; i++) {
|
|
|
|
GstH264Picture *picture = g_array_index (dpb_array, GstH264Picture *, i);
|
|
|
|
if (GST_H264_PICTURE_IS_REF (picture) && !picture->nonexisting) {
|
|
|
|
construct_list = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g_array_unref (dpb_array);
|
|
|
|
|
|
|
|
if (!construct_list) {
|
|
|
|
gst_h264_decoder_clear_ref_pic_lists (self);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-14 15:20:54 +00:00
|
|
|
if (GST_H264_PICTURE_IS_FRAME (current_picture)) {
|
|
|
|
construct_ref_pic_lists_p (self, current_picture);
|
|
|
|
construct_ref_pic_lists_b (self, current_picture);
|
|
|
|
} else {
|
|
|
|
construct_ref_field_pic_lists_p (self, current_picture);
|
|
|
|
construct_ref_field_pic_lists_b (self, current_picture);
|
|
|
|
}
|
2020-04-27 14:53:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_h264_decoder_clear_ref_pic_lists (GstH264Decoder * self)
|
|
|
|
{
|
|
|
|
GstH264DecoderPrivate *priv = self->priv;
|
|
|
|
|
|
|
|
g_array_set_size (priv->ref_pic_list_p0, 0);
|
|
|
|
g_array_set_size (priv->ref_pic_list_b0, 0);
|
|
|
|
g_array_set_size (priv->ref_pic_list_b1, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gint
|
|
|
|
long_term_pic_num_f (GstH264Decoder * self, const GstH264Picture * picture)
|
|
|
|
{
|
2020-11-05 16:45:36 +00:00
|
|
|
if (GST_H264_PICTURE_IS_LONG_TERM_REF (picture))
|
2020-04-27 14:53:45 +00:00
|
|
|
return picture->long_term_pic_num;
|
|
|
|
return 2 * (self->priv->max_long_term_frame_idx + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gint
|
|
|
|
pic_num_f (GstH264Decoder * self, const GstH264Picture * picture)
|
|
|
|
{
|
2020-11-05 16:45:36 +00:00
|
|
|
if (!GST_H264_PICTURE_IS_LONG_TERM_REF (picture))
|
2020-04-27 14:53:45 +00:00
|
|
|
return picture->pic_num;
|
|
|
|
return self->priv->max_pic_num;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* shift elements on the |array| starting from |from| to |to|,
|
|
|
|
* inclusive, one position to the right and insert pic at |from| */
|
|
|
|
static void
|
2020-05-03 15:30:34 +00:00
|
|
|
shift_right_and_insert (GArray * array, gint from, gint to,
|
2020-04-27 14:53:45 +00:00
|
|
|
GstH264Picture * picture)
|
|
|
|
{
|
|
|
|
g_return_if_fail (from <= to);
|
|
|
|
g_return_if_fail (array && picture);
|
|
|
|
|
2020-05-03 15:30:34 +00:00
|
|
|
g_array_set_size (array, to + 2);
|
|
|
|
g_array_insert_val (array, from, picture);
|
2020-04-27 14:53:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* This can process either ref_pic_list0 or ref_pic_list1, depending
|
|
|
|
* on the list argument. Set up pointers to proper list to be
|
|
|
|
* processed here. */
|
|
|
|
static gboolean
|
2020-05-03 15:30:34 +00:00
|
|
|
modify_ref_pic_list (GstH264Decoder * self, int list)
|
2020-04-27 14:53:45 +00:00
|
|
|
{
|
|
|
|
GstH264DecoderPrivate *priv = self->priv;
|
|
|
|
GstH264Picture *picture = priv->current_picture;
|
2020-05-03 15:30:34 +00:00
|
|
|
GArray *ref_pic_listx;
|
2020-04-27 14:53:45 +00:00
|
|
|
const GstH264SliceHdr *slice_hdr = &priv->current_slice.header;
|
|
|
|
const GstH264RefPicListModification *list_mod;
|
|
|
|
gboolean ref_pic_list_modification_flag_lX;
|
|
|
|
gint num_ref_idx_lX_active_minus1;
|
|
|
|
guint num_ref_pic_list_modifications;
|
|
|
|
gint i;
|
|
|
|
gint pic_num_lx_pred = picture->pic_num;
|
|
|
|
gint ref_idx_lx = 0, src, dst;
|
|
|
|
gint pic_num_lx_no_wrap;
|
|
|
|
gint pic_num_lx;
|
|
|
|
gboolean done = FALSE;
|
|
|
|
GstH264Picture *pic;
|
|
|
|
|
|
|
|
if (list == 0) {
|
2020-05-03 15:30:34 +00:00
|
|
|
ref_pic_listx = priv->ref_pic_list0;
|
2020-04-27 14:53:45 +00:00
|
|
|
ref_pic_list_modification_flag_lX =
|
|
|
|
slice_hdr->ref_pic_list_modification_flag_l0;
|
|
|
|
num_ref_pic_list_modifications = slice_hdr->n_ref_pic_list_modification_l0;
|
|
|
|
num_ref_idx_lX_active_minus1 = slice_hdr->num_ref_idx_l0_active_minus1;
|
|
|
|
list_mod = slice_hdr->ref_pic_list_modification_l0;
|
|
|
|
} else {
|
2020-05-03 15:30:34 +00:00
|
|
|
ref_pic_listx = priv->ref_pic_list1;
|
2020-04-27 14:53:45 +00:00
|
|
|
ref_pic_list_modification_flag_lX =
|
|
|
|
slice_hdr->ref_pic_list_modification_flag_l1;
|
|
|
|
num_ref_pic_list_modifications = slice_hdr->n_ref_pic_list_modification_l1;
|
|
|
|
num_ref_idx_lX_active_minus1 = slice_hdr->num_ref_idx_l1_active_minus1;
|
|
|
|
list_mod = slice_hdr->ref_pic_list_modification_l1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Resize the list to the size requested in the slice header.
|
|
|
|
*
|
|
|
|
* Note that per 8.2.4.2 it's possible for
|
|
|
|
* num_ref_idx_lX_active_minus1 to indicate there should be more ref
|
|
|
|
* pics on list than we constructed. Those superfluous ones should
|
|
|
|
* be treated as non-reference and will be initialized to null,
|
|
|
|
* which must be handled by clients */
|
|
|
|
g_assert (num_ref_idx_lX_active_minus1 >= 0);
|
|
|
|
if (ref_pic_listx->len > num_ref_idx_lX_active_minus1 + 1)
|
2020-05-03 15:30:34 +00:00
|
|
|
g_array_set_size (ref_pic_listx, num_ref_idx_lX_active_minus1 + 1);
|
2020-04-27 14:53:45 +00:00
|
|
|
|
|
|
|
if (!ref_pic_list_modification_flag_lX)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
/* Spec 8.2.4.3:
|
|
|
|
* Reorder pictures on the list in a way specified in the stream. */
|
|
|
|
for (i = 0; i < num_ref_pic_list_modifications && !done; i++) {
|
|
|
|
switch (list_mod->modification_of_pic_nums_idc) {
|
|
|
|
/* 8.2.4.3.1 - Modify short reference picture position. */
|
|
|
|
case 0:
|
|
|
|
case 1:
|
|
|
|
/* 8-34 */
|
|
|
|
if (list_mod->modification_of_pic_nums_idc == 0) {
|
|
|
|
/* Substract given value from predicted PicNum. */
|
|
|
|
pic_num_lx_no_wrap = pic_num_lx_pred -
|
|
|
|
(list_mod->value.abs_diff_pic_num_minus1 + 1);
|
|
|
|
/* Wrap around max_pic_num if it becomes < 0 as result of
|
|
|
|
* subtraction */
|
|
|
|
if (pic_num_lx_no_wrap < 0)
|
|
|
|
pic_num_lx_no_wrap += priv->max_pic_num;
|
|
|
|
} else { /* 8-35 */
|
|
|
|
/* Add given value to predicted PicNum. */
|
|
|
|
pic_num_lx_no_wrap = pic_num_lx_pred +
|
|
|
|
(list_mod->value.abs_diff_pic_num_minus1 + 1);
|
|
|
|
/* Wrap around max_pic_num if it becomes >= max_pic_num as
|
|
|
|
* result of the addition */
|
|
|
|
if (pic_num_lx_no_wrap >= priv->max_pic_num)
|
|
|
|
pic_num_lx_no_wrap -= priv->max_pic_num;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* For use in next iteration */
|
|
|
|
pic_num_lx_pred = pic_num_lx_no_wrap;
|
|
|
|
|
|
|
|
/* 8-36 */
|
|
|
|
if (pic_num_lx_no_wrap > picture->pic_num)
|
|
|
|
pic_num_lx = pic_num_lx_no_wrap - priv->max_pic_num;
|
|
|
|
else
|
|
|
|
pic_num_lx = pic_num_lx_no_wrap;
|
|
|
|
|
|
|
|
/* 8-37 */
|
|
|
|
g_assert (num_ref_idx_lX_active_minus1 + 1 < 32);
|
|
|
|
pic = gst_h264_dpb_get_short_ref_by_pic_num (priv->dpb, pic_num_lx);
|
|
|
|
if (!pic) {
|
|
|
|
GST_WARNING_OBJECT (self, "Malformed stream, no pic num %d",
|
|
|
|
pic_num_lx);
|
2020-11-13 18:16:07 +00:00
|
|
|
break;
|
2020-04-27 14:53:45 +00:00
|
|
|
}
|
|
|
|
shift_right_and_insert (ref_pic_listx, ref_idx_lx,
|
|
|
|
num_ref_idx_lX_active_minus1, pic);
|
|
|
|
ref_idx_lx++;
|
|
|
|
|
|
|
|
for (src = ref_idx_lx, dst = ref_idx_lx;
|
|
|
|
src <= num_ref_idx_lX_active_minus1 + 1; src++) {
|
2020-05-03 15:30:34 +00:00
|
|
|
GstH264Picture *src_pic =
|
|
|
|
g_array_index (ref_pic_listx, GstH264Picture *, src);
|
2020-04-27 14:53:45 +00:00
|
|
|
gint src_pic_num_lx = src_pic ? pic_num_f (self, src_pic) : -1;
|
|
|
|
if (src_pic_num_lx != pic_num_lx)
|
2020-05-03 15:30:34 +00:00
|
|
|
g_array_index (ref_pic_listx, GstH264Picture *, dst++) = src_pic;
|
2020-04-27 14:53:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* 8.2.4.3.2 - Long-term reference pictures */
|
|
|
|
case 2:
|
|
|
|
/* (8-28) */
|
|
|
|
g_assert (num_ref_idx_lX_active_minus1 + 1 < 32);
|
2020-11-05 09:42:37 +00:00
|
|
|
pic = gst_h264_dpb_get_long_ref_by_long_term_pic_num (priv->dpb,
|
2020-04-27 14:53:45 +00:00
|
|
|
list_mod->value.long_term_pic_num);
|
|
|
|
if (!pic) {
|
|
|
|
GST_WARNING_OBJECT (self, "Malformed stream, no pic num %d",
|
|
|
|
list_mod->value.long_term_pic_num);
|
2020-11-13 18:16:07 +00:00
|
|
|
break;
|
2020-04-27 14:53:45 +00:00
|
|
|
}
|
|
|
|
shift_right_and_insert (ref_pic_listx, ref_idx_lx,
|
|
|
|
num_ref_idx_lX_active_minus1, pic);
|
|
|
|
ref_idx_lx++;
|
|
|
|
|
|
|
|
for (src = ref_idx_lx, dst = ref_idx_lx;
|
|
|
|
src <= num_ref_idx_lX_active_minus1 + 1; src++) {
|
2020-05-03 15:30:34 +00:00
|
|
|
GstH264Picture *src_pic =
|
|
|
|
g_array_index (ref_pic_listx, GstH264Picture *, src);
|
2020-04-27 14:53:45 +00:00
|
|
|
if (long_term_pic_num_f (self, src_pic) !=
|
|
|
|
list_mod->value.long_term_pic_num)
|
2020-05-03 15:30:34 +00:00
|
|
|
g_array_index (ref_pic_listx, GstH264Picture *, dst++) = src_pic;
|
2020-04-27 14:53:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* End of modification list */
|
|
|
|
case 3:
|
|
|
|
done = TRUE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
/* may be recoverable */
|
|
|
|
GST_WARNING ("Invalid modification_of_pic_nums_idc = %d",
|
|
|
|
list_mod->modification_of_pic_nums_idc);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
list_mod++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Per NOTE 2 in 8.2.4.3.2, the ref_pic_listx in the above loop is
|
|
|
|
* temporarily made one element longer than the required final list.
|
|
|
|
* Resize the list back to its required size. */
|
|
|
|
if (ref_pic_listx->len > num_ref_idx_lX_active_minus1 + 1)
|
2020-05-03 15:30:34 +00:00
|
|
|
g_array_set_size (ref_pic_listx, num_ref_idx_lX_active_minus1 + 1);
|
2020-04-27 14:53:45 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-05-03 15:30:34 +00:00
|
|
|
copy_pic_list_into (GArray * dest, GArray * src)
|
2020-04-27 14:53:45 +00:00
|
|
|
{
|
2020-05-03 15:30:34 +00:00
|
|
|
gint i;
|
|
|
|
g_array_set_size (dest, 0);
|
2020-04-27 14:53:45 +00:00
|
|
|
|
2020-05-03 15:30:34 +00:00
|
|
|
for (i = 0; i < src->len; i++)
|
|
|
|
g_array_append_val (dest, g_array_index (src, gpointer, i));
|
2020-04-27 14:53:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2020-05-03 15:30:34 +00:00
|
|
|
gst_h264_decoder_modify_ref_pic_lists (GstH264Decoder * self)
|
2020-04-27 14:53:45 +00:00
|
|
|
{
|
|
|
|
GstH264DecoderPrivate *priv = self->priv;
|
|
|
|
GstH264SliceHdr *slice_hdr = &priv->current_slice.header;
|
|
|
|
|
2020-11-16 18:11:46 +00:00
|
|
|
g_array_set_size (priv->ref_pic_list0, 0);
|
|
|
|
g_array_set_size (priv->ref_pic_list1, 0);
|
|
|
|
|
2020-04-27 14:53:45 +00:00
|
|
|
if (GST_H264_IS_P_SLICE (slice_hdr) || GST_H264_IS_SP_SLICE (slice_hdr)) {
|
2020-11-16 18:11:46 +00:00
|
|
|
/* 8.2.4 fill reference picture list RefPicList0 for P or SP slice */
|
2020-05-03 15:30:34 +00:00
|
|
|
copy_pic_list_into (priv->ref_pic_list0, priv->ref_pic_list_p0);
|
|
|
|
return modify_ref_pic_list (self, 0);
|
2020-11-16 18:11:46 +00:00
|
|
|
} else if (GST_H264_IS_B_SLICE (slice_hdr)) {
|
|
|
|
/* 8.2.4 fill reference picture list RefPicList0 and RefPicList1 for B slice */
|
2020-05-03 15:30:34 +00:00
|
|
|
copy_pic_list_into (priv->ref_pic_list0, priv->ref_pic_list_b0);
|
|
|
|
copy_pic_list_into (priv->ref_pic_list1, priv->ref_pic_list_b1);
|
|
|
|
return modify_ref_pic_list (self, 0)
|
|
|
|
&& modify_ref_pic_list (self, 1);
|
2020-04-27 14:53:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2020-07-21 09:17:09 +00:00
|
|
|
/**
|
|
|
|
* gst_h264_decoder_set_process_ref_pic_lists:
|
|
|
|
* @decoder: a #GstH264Decoder
|
|
|
|
* @process: whether subclass is requiring reference picture modification process
|
|
|
|
*
|
|
|
|
* Called to en/disable reference picture modification process.
|
|
|
|
*
|
|
|
|
* Since: 1.18
|
|
|
|
*/
|
2020-04-27 14:53:45 +00:00
|
|
|
void
|
2020-07-21 08:53:29 +00:00
|
|
|
gst_h264_decoder_set_process_ref_pic_lists (GstH264Decoder * decoder,
|
2020-04-27 14:53:45 +00:00
|
|
|
gboolean process)
|
|
|
|
{
|
2020-07-21 08:53:29 +00:00
|
|
|
decoder->priv->process_ref_pic_lists = process;
|
2020-04-27 14:53:45 +00:00
|
|
|
}
|
2020-07-20 20:48:32 +00:00
|
|
|
|
2020-07-21 09:17:09 +00:00
|
|
|
/**
|
|
|
|
* gst_h264_decoder_get_picture:
|
|
|
|
* @decoder: a #GstH264Decoder
|
|
|
|
* @system_frame_number: a target system frame number of #GstH264Picture
|
|
|
|
*
|
|
|
|
* Retrive DPB and return a #GstH264Picture corresponding to
|
|
|
|
* the @system_frame_number
|
|
|
|
*
|
|
|
|
* Returns: (transfer full): a #GstH264Picture if successful, or %NULL otherwise
|
|
|
|
*
|
|
|
|
* Since: 1.18
|
|
|
|
*/
|
2020-07-20 20:48:32 +00:00
|
|
|
GstH264Picture *
|
2020-07-21 08:53:29 +00:00
|
|
|
gst_h264_decoder_get_picture (GstH264Decoder * decoder,
|
2020-07-20 20:48:32 +00:00
|
|
|
guint32 system_frame_number)
|
|
|
|
{
|
2020-07-21 08:53:29 +00:00
|
|
|
return gst_h264_dpb_get_picture (decoder->priv->dpb, system_frame_number);
|
2020-07-20 20:48:32 +00:00
|
|
|
}
|