2003-04-04 17:42:28 +03:00
|
|
|
/*
|
2011-01-22 05:49:55 +02:00
|
|
|
* H.26L/H.264/AVC/JVT/14496-10/... decoder
|
2003-04-04 17:42:28 +03:00
|
|
|
* Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
|
|
|
|
*
|
2006-10-07 18:30:46 +03:00
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2003-04-04 17:42:28 +03:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2006-10-07 18:30:46 +03:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2003-04-04 17:42:28 +03:00
|
|
|
*
|
2006-10-07 18:30:46 +03:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2003-04-04 17:42:28 +03:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2006-10-07 18:30:46 +03:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2006-01-13 00:43:26 +02:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2003-04-04 17:42:28 +03:00
|
|
|
*/
|
2005-12-17 20:14:38 +02:00
|
|
|
|
2003-04-04 17:42:28 +03:00
|
|
|
/**
|
2010-04-20 17:45:34 +03:00
|
|
|
* @file
|
2016-04-27 19:45:23 +02:00
|
|
|
* H.264 / AVC / MPEG-4 part10 codec.
|
2003-04-04 17:42:28 +03:00
|
|
|
* @author Michael Niedermayer <michaelni@gmx.at>
|
|
|
|
*/
|
|
|
|
|
2011-12-18 05:17:44 +03:00
|
|
|
#define UNCHECKED_BITSTREAM_READER 1
|
|
|
|
|
2022-02-23 14:56:49 +02:00
|
|
|
#include "config_components.h"
|
|
|
|
|
2013-07-26 22:03:55 +03:00
|
|
|
#include "libavutil/avassert.h"
|
2011-02-07 15:37:08 +02:00
|
|
|
#include "libavutil/imgutils.h"
|
2011-11-02 20:39:42 +03:00
|
|
|
#include "libavutil/opt.h"
|
2022-01-24 20:22:47 +02:00
|
|
|
#include "libavutil/thread.h"
|
2020-03-10 19:26:00 +02:00
|
|
|
#include "libavutil/video_enc_params.h"
|
|
|
|
|
2022-03-16 19:18:28 +02:00
|
|
|
#include "codec_internal.h"
|
2009-02-23 15:44:51 +02:00
|
|
|
#include "internal.h"
|
2013-03-06 20:31:14 +03:00
|
|
|
#include "error_resilience.h"
|
2003-04-04 17:42:28 +03:00
|
|
|
#include "avcodec.h"
|
2016-05-18 09:16:35 +02:00
|
|
|
#include "h264.h"
|
2016-05-18 09:02:39 +02:00
|
|
|
#include "h264dec.h"
|
2016-03-21 15:35:48 +02:00
|
|
|
#include "h2645_parse.h"
|
2003-04-04 17:42:28 +03:00
|
|
|
#include "h264data.h"
|
2016-05-18 09:40:33 +02:00
|
|
|
#include "h264_ps.h"
|
2003-04-04 17:42:28 +03:00
|
|
|
#include "golomb.h"
|
2020-04-13 17:33:14 +02:00
|
|
|
#include "hwconfig.h"
|
2014-02-28 15:40:54 +03:00
|
|
|
#include "mpegutils.h"
|
2014-12-31 08:57:59 +02:00
|
|
|
#include "profiles.h"
|
2007-12-15 08:06:16 +02:00
|
|
|
#include "rectangle.h"
|
2022-02-06 15:49:23 +02:00
|
|
|
#include "thread.h"
|
2022-02-06 08:08:01 +02:00
|
|
|
#include "threadframe.h"
|
2003-04-04 17:42:28 +03:00
|
|
|
|
2012-04-04 15:53:25 +03:00
|
|
|
const uint16_t ff_h264_mb_sizes[4] = { 256, 384, 512, 768 };
|
|
|
|
|
2012-07-03 00:16:59 +03:00
|
|
|
int avpriv_h264_has_num_reorder_frames(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
H264Context *h = avctx->priv_data;
|
2016-06-12 13:24:27 +02:00
|
|
|
return h && h->ps.sps ? h->ps.sps->num_reorder_frames : 0;
|
2012-07-03 00:16:59 +03:00
|
|
|
}
|
|
|
|
|
2013-02-02 22:42:07 +03:00
|
|
|
static void h264_er_decode_mb(void *opaque, int ref, int mv_dir, int mv_type,
|
|
|
|
int (*mv)[2][4][2],
|
|
|
|
int mb_x, int mb_y, int mb_intra, int mb_skipped)
|
|
|
|
{
|
2013-07-26 22:03:55 +03:00
|
|
|
H264Context *h = opaque;
|
2015-01-17 23:28:46 +02:00
|
|
|
H264SliceContext *sl = &h->slice_ctx[0];
|
2013-02-02 22:42:07 +03:00
|
|
|
|
2015-01-17 23:28:46 +02:00
|
|
|
sl->mb_x = mb_x;
|
|
|
|
sl->mb_y = mb_y;
|
2015-01-17 23:28:46 +02:00
|
|
|
sl->mb_xy = mb_x + mb_y * h->mb_stride;
|
2015-01-17 23:28:46 +02:00
|
|
|
memset(sl->non_zero_count_cache, 0, sizeof(sl->non_zero_count_cache));
|
2013-02-16 20:46:28 +03:00
|
|
|
av_assert1(ref >= 0);
|
2013-02-02 22:42:07 +03:00
|
|
|
/* FIXME: It is possible albeit uncommon that slice references
|
|
|
|
* differ between slices. We take the easy approach and ignore
|
|
|
|
* it for now. If this turns out to have any relevance in
|
|
|
|
* practice then correct remapping should be added. */
|
2015-01-17 23:28:46 +02:00
|
|
|
if (ref >= sl->ref_count[0])
|
2013-02-02 22:42:07 +03:00
|
|
|
ref = 0;
|
2015-03-21 23:08:19 +02:00
|
|
|
if (!sl->ref_list[0][ref].data[0]) {
|
2013-02-16 23:14:00 +03:00
|
|
|
av_log(h->avctx, AV_LOG_DEBUG, "Reference not available for error concealing\n");
|
2013-02-16 20:46:28 +03:00
|
|
|
ref = 0;
|
|
|
|
}
|
2015-03-21 15:59:58 +02:00
|
|
|
if ((sl->ref_list[0][ref].reference&3) != 3) {
|
2013-02-16 23:14:00 +03:00
|
|
|
av_log(h->avctx, AV_LOG_DEBUG, "Reference invalid\n");
|
2013-02-16 20:46:28 +03:00
|
|
|
return;
|
|
|
|
}
|
2015-01-17 23:28:46 +02:00
|
|
|
fill_rectangle(&h->cur_pic.ref_index[0][4 * sl->mb_xy],
|
2013-02-02 22:42:07 +03:00
|
|
|
2, 2, 2, ref, 1);
|
2015-01-17 23:28:46 +02:00
|
|
|
fill_rectangle(&sl->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1);
|
|
|
|
fill_rectangle(sl->mv_cache[0][scan8[0]], 4, 4, 8,
|
2013-02-16 20:46:28 +03:00
|
|
|
pack16to32((*mv)[0][0][0], (*mv)[0][0][1]), 4);
|
2015-03-21 19:07:01 +02:00
|
|
|
sl->mb_mbaff =
|
2015-03-21 18:30:59 +02:00
|
|
|
sl->mb_field_decoding_flag = 0;
|
2015-01-17 19:45:29 +02:00
|
|
|
ff_h264_hl_decode_mb(h, &h->slice_ctx[0]);
|
2013-02-02 22:42:07 +03:00
|
|
|
}
|
|
|
|
|
2015-01-27 16:53:33 +02:00
|
|
|
void ff_h264_draw_horiz_band(const H264Context *h, H264SliceContext *sl,
|
2015-01-17 23:28:46 +02:00
|
|
|
int y, int height)
|
2013-03-02 22:22:02 +03:00
|
|
|
{
|
2013-03-02 22:22:02 +03:00
|
|
|
AVCodecContext *avctx = h->avctx;
|
2015-04-17 12:09:50 +02:00
|
|
|
const AVFrame *src = h->cur_pic.f;
|
2013-03-02 22:22:02 +03:00
|
|
|
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
|
|
|
|
int vshift = desc->log2_chroma_h;
|
2013-03-02 22:22:02 +03:00
|
|
|
const int field_pic = h->picture_structure != PICT_FRAME;
|
|
|
|
if (field_pic) {
|
|
|
|
height <<= 1;
|
2013-07-26 22:03:55 +03:00
|
|
|
y <<= 1;
|
2013-03-02 22:22:02 +03:00
|
|
|
}
|
|
|
|
|
2013-03-02 22:22:02 +03:00
|
|
|
height = FFMIN(height, avctx->height - y);
|
2013-03-02 22:22:02 +03:00
|
|
|
|
2013-03-02 22:22:02 +03:00
|
|
|
if (field_pic && h->first_field && !(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD))
|
|
|
|
return;
|
2013-03-02 22:22:02 +03:00
|
|
|
|
|
|
|
if (avctx->draw_horiz_band) {
|
|
|
|
int offset[AV_NUM_DATA_POINTERS];
|
|
|
|
int i;
|
|
|
|
|
2013-03-02 22:22:02 +03:00
|
|
|
offset[0] = y * src->linesize[0];
|
|
|
|
offset[1] =
|
|
|
|
offset[2] = (y >> vshift) * src->linesize[1];
|
2013-03-02 22:22:02 +03:00
|
|
|
for (i = 3; i < AV_NUM_DATA_POINTERS; i++)
|
|
|
|
offset[i] = 0;
|
|
|
|
|
|
|
|
emms_c();
|
|
|
|
|
|
|
|
avctx->draw_horiz_band(avctx, src, offset,
|
2013-03-02 22:22:02 +03:00
|
|
|
y, h->picture_structure, height);
|
2013-03-02 22:22:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-06 21:00:42 +02:00
|
|
|
void ff_h264_free_tables(H264Context *h)
|
2012-03-16 20:40:05 +03:00
|
|
|
{
|
2007-05-29 17:35:29 +03:00
|
|
|
int i;
|
2011-06-02 20:15:58 +03:00
|
|
|
|
2003-04-04 17:42:28 +03:00
|
|
|
av_freep(&h->intra4x4_pred_mode);
|
2004-03-26 21:35:53 +02:00
|
|
|
av_freep(&h->chroma_pred_mode_table);
|
|
|
|
av_freep(&h->cbp_table);
|
2004-04-11 19:28:31 +03:00
|
|
|
av_freep(&h->mvd_table[0]);
|
|
|
|
av_freep(&h->mvd_table[1]);
|
2004-12-29 06:17:52 +02:00
|
|
|
av_freep(&h->direct_table);
|
2003-04-04 17:42:28 +03:00
|
|
|
av_freep(&h->non_zero_count);
|
|
|
|
av_freep(&h->slice_table_base);
|
2012-03-16 20:40:05 +03:00
|
|
|
h->slice_table = NULL;
|
2010-01-17 22:35:55 +02:00
|
|
|
av_freep(&h->list_counts);
|
2004-03-26 21:35:53 +02:00
|
|
|
|
2003-04-04 17:42:28 +03:00
|
|
|
av_freep(&h->mb2b_xy);
|
2010-02-25 04:42:25 +02:00
|
|
|
av_freep(&h->mb2br_xy);
|
2005-01-07 07:56:07 +02:00
|
|
|
|
2012-11-21 23:34:46 +03:00
|
|
|
av_buffer_pool_uninit(&h->qscale_table_pool);
|
|
|
|
av_buffer_pool_uninit(&h->mb_type_pool);
|
|
|
|
av_buffer_pool_uninit(&h->motion_val_pool);
|
|
|
|
av_buffer_pool_uninit(&h->ref_index_pool);
|
|
|
|
|
2022-03-24 21:36:10 +02:00
|
|
|
#if CONFIG_ERROR_RESILIENCE
|
|
|
|
av_freep(&h->er.mb_index2xy);
|
|
|
|
av_freep(&h->er.error_status_table);
|
|
|
|
av_freep(&h->er.er_temp_buffer);
|
|
|
|
av_freep(&h->dc_val_base);
|
|
|
|
#endif
|
|
|
|
|
2015-01-17 23:28:46 +02:00
|
|
|
for (i = 0; i < h->nb_slice_ctx; i++) {
|
|
|
|
H264SliceContext *sl = &h->slice_ctx[i];
|
|
|
|
|
|
|
|
av_freep(&sl->bipred_scratchpad);
|
|
|
|
av_freep(&sl->edge_emu_buffer);
|
2015-01-17 23:28:46 +02:00
|
|
|
av_freep(&sl->top_borders[0]);
|
|
|
|
av_freep(&sl->top_borders[1]);
|
2015-01-17 23:28:46 +02:00
|
|
|
|
|
|
|
sl->bipred_scratchpad_allocated = 0;
|
|
|
|
sl->edge_emu_buffer_allocated = 0;
|
2015-01-17 23:28:46 +02:00
|
|
|
sl->top_borders_allocated[0] = 0;
|
|
|
|
sl->top_borders_allocated[1] = 0;
|
2015-01-17 23:28:46 +02:00
|
|
|
}
|
2003-04-04 17:42:28 +03:00
|
|
|
}
|
|
|
|
|
2012-03-16 20:40:05 +03:00
|
|
|
int ff_h264_alloc_tables(H264Context *h)
|
|
|
|
{
|
2022-03-24 21:36:10 +02:00
|
|
|
ERContext *const er = &h->er;
|
2013-07-26 22:03:55 +03:00
|
|
|
const int big_mb_num = h->mb_stride * (h->mb_height + 1);
|
2016-06-21 10:28:14 +02:00
|
|
|
const int row_mb_num = 2*h->mb_stride*FFMAX(h->nb_slice_ctx, 1);
|
2020-05-28 17:13:45 +02:00
|
|
|
const int st_size = big_mb_num + h->mb_stride;
|
2015-04-05 20:51:00 +02:00
|
|
|
int x, y;
|
2012-03-16 20:40:05 +03:00
|
|
|
|
2020-05-28 17:33:04 +02:00
|
|
|
if (!FF_ALLOCZ_TYPED_ARRAY(h->intra4x4_pred_mode, row_mb_num * 8) ||
|
|
|
|
!FF_ALLOCZ_TYPED_ARRAY(h->non_zero_count, big_mb_num) ||
|
|
|
|
!FF_ALLOCZ_TYPED_ARRAY(h->slice_table_base, st_size) ||
|
|
|
|
!FF_ALLOCZ_TYPED_ARRAY(h->cbp_table, big_mb_num) ||
|
|
|
|
!FF_ALLOCZ_TYPED_ARRAY(h->chroma_pred_mode_table, big_mb_num) ||
|
|
|
|
!FF_ALLOCZ_TYPED_ARRAY(h->mvd_table[0], row_mb_num * 8) ||
|
|
|
|
!FF_ALLOCZ_TYPED_ARRAY(h->mvd_table[1], row_mb_num * 8) ||
|
|
|
|
!FF_ALLOCZ_TYPED_ARRAY(h->direct_table, big_mb_num * 4) ||
|
|
|
|
!FF_ALLOCZ_TYPED_ARRAY(h->list_counts, big_mb_num) ||
|
|
|
|
!FF_ALLOCZ_TYPED_ARRAY(h->mb2b_xy, big_mb_num) ||
|
|
|
|
!FF_ALLOCZ_TYPED_ARRAY(h->mb2br_xy, big_mb_num))
|
|
|
|
return AVERROR(ENOMEM);
|
2015-01-17 23:28:46 +02:00
|
|
|
h->slice_ctx[0].intra4x4_pred_mode = h->intra4x4_pred_mode;
|
2015-01-17 23:28:46 +02:00
|
|
|
h->slice_ctx[0].mvd_table[0] = h->mvd_table[0];
|
|
|
|
h->slice_ctx[0].mvd_table[1] = h->mvd_table[1];
|
2012-03-16 20:40:05 +03:00
|
|
|
memset(h->slice_table_base, -1,
|
2020-05-28 17:13:45 +02:00
|
|
|
st_size * sizeof(*h->slice_table_base));
|
2013-02-03 13:10:05 +03:00
|
|
|
h->slice_table = h->slice_table_base + h->mb_stride * 2 + 1;
|
|
|
|
for (y = 0; y < h->mb_height; y++)
|
|
|
|
for (x = 0; x < h->mb_width; x++) {
|
|
|
|
const int mb_xy = x + y * h->mb_stride;
|
2012-03-16 20:40:05 +03:00
|
|
|
const int b_xy = 4 * x + 4 * y * h->b_stride;
|
|
|
|
|
|
|
|
h->mb2b_xy[mb_xy] = b_xy;
|
2013-02-03 13:10:05 +03:00
|
|
|
h->mb2br_xy[mb_xy] = 8 * (FMO ? mb_xy : (mb_xy % (2 * h->mb_stride)));
|
2003-04-04 17:42:28 +03:00
|
|
|
}
|
2005-01-07 07:56:07 +02:00
|
|
|
|
2022-03-24 21:36:10 +02:00
|
|
|
if (CONFIG_ERROR_RESILIENCE) {
|
2020-05-28 17:13:45 +02:00
|
|
|
const int er_size = h->mb_height * h->mb_stride * (4*sizeof(int) + 1);
|
2022-03-24 21:36:10 +02:00
|
|
|
int mb_array_size = h->mb_height * h->mb_stride;
|
|
|
|
int y_size = (2 * h->mb_width + 1) * (2 * h->mb_height + 1);
|
|
|
|
int yc_size = y_size + 2 * big_mb_num;
|
2015-04-04 12:36:43 +02:00
|
|
|
|
2013-03-03 03:57:26 +03:00
|
|
|
/* init ER */
|
|
|
|
er->avctx = h->avctx;
|
|
|
|
er->decode_mb = h264_er_decode_mb;
|
|
|
|
er->opaque = h;
|
|
|
|
er->quarter_sample = 1;
|
|
|
|
|
|
|
|
er->mb_num = h->mb_num;
|
|
|
|
er->mb_width = h->mb_width;
|
|
|
|
er->mb_height = h->mb_height;
|
|
|
|
er->mb_stride = h->mb_stride;
|
|
|
|
er->b8_stride = h->mb_width * 2 + 1;
|
2013-02-03 13:10:05 +03:00
|
|
|
|
2014-07-17 01:31:06 +03:00
|
|
|
// error resilience code looks cleaner with this
|
2020-05-28 17:33:04 +02:00
|
|
|
if (!FF_ALLOCZ_TYPED_ARRAY(er->mb_index2xy, h->mb_num + 1) ||
|
|
|
|
!FF_ALLOCZ_TYPED_ARRAY(er->error_status_table, mb_array_size) ||
|
|
|
|
!FF_ALLOCZ_TYPED_ARRAY(er->er_temp_buffer, er_size) ||
|
2022-03-24 21:36:10 +02:00
|
|
|
!FF_ALLOCZ_TYPED_ARRAY(h->dc_val_base, yc_size))
|
2020-05-28 17:33:04 +02:00
|
|
|
return AVERROR(ENOMEM); // ff_h264_free_tables will clean up for us
|
2014-07-17 01:31:06 +03:00
|
|
|
|
2013-03-03 03:57:26 +03:00
|
|
|
for (y = 0; y < h->mb_height; y++)
|
|
|
|
for (x = 0; x < h->mb_width; x++)
|
|
|
|
er->mb_index2xy[x + y * h->mb_width] = x + y * h->mb_stride;
|
2013-02-03 13:10:05 +03:00
|
|
|
|
2013-03-03 03:57:26 +03:00
|
|
|
er->mb_index2xy[h->mb_height * h->mb_width] = (h->mb_height - 1) *
|
2013-07-26 22:03:55 +03:00
|
|
|
h->mb_stride + h->mb_width;
|
2022-03-24 21:36:10 +02:00
|
|
|
er->dc_val[0] = h->dc_val_base + h->mb_width * 2 + 2;
|
|
|
|
er->dc_val[1] = h->dc_val_base + y_size + h->mb_stride + 1;
|
|
|
|
er->dc_val[2] = er->dc_val[1] + big_mb_num;
|
|
|
|
for (int i = 0; i < yc_size; i++)
|
|
|
|
h->dc_val_base[i] = 1024;
|
2013-03-03 03:57:26 +03:00
|
|
|
}
|
2013-02-02 22:42:07 +03:00
|
|
|
|
2007-09-05 19:18:15 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-03-24 21:36:10 +02:00
|
|
|
/**
|
|
|
|
* Init slice context
|
|
|
|
*/
|
|
|
|
void ff_h264_slice_context_init(H264Context *h, H264SliceContext *sl)
|
|
|
|
{
|
|
|
|
sl->ref_cache[0][scan8[5] + 1] =
|
|
|
|
sl->ref_cache[0][scan8[7] + 1] =
|
|
|
|
sl->ref_cache[0][scan8[13] + 1] =
|
|
|
|
sl->ref_cache[1][scan8[5] + 1] =
|
|
|
|
sl->ref_cache[1][scan8[7] + 1] =
|
|
|
|
sl->ref_cache[1][scan8[13] + 1] = PART_NOT_AVAILABLE;
|
|
|
|
|
|
|
|
sl->er = &h->er;
|
|
|
|
}
|
|
|
|
|
2021-08-17 21:54:56 +02:00
|
|
|
static int h264_init_pic(H264Picture *pic)
|
|
|
|
{
|
|
|
|
pic->f = av_frame_alloc();
|
|
|
|
if (!pic->f)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
pic->f_grain = av_frame_alloc();
|
|
|
|
if (!pic->f_grain)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-03-23 06:46:49 +02:00
|
|
|
static int h264_init_context(AVCodecContext *avctx, H264Context *h)
|
2012-03-16 20:40:05 +03:00
|
|
|
{
|
2021-08-17 21:54:56 +02:00
|
|
|
int i, ret;
|
2011-11-05 21:26:54 +03:00
|
|
|
|
2015-03-23 06:46:49 +02:00
|
|
|
h->avctx = avctx;
|
2015-03-21 17:08:18 +02:00
|
|
|
h->cur_chroma_format_idc = -1;
|
2011-11-05 21:26:54 +03:00
|
|
|
|
2017-05-08 20:46:23 +02:00
|
|
|
h->width_from_caller = avctx->width;
|
|
|
|
h->height_from_caller = avctx->height;
|
|
|
|
|
2015-03-23 06:46:49 +02:00
|
|
|
h->workaround_bugs = avctx->workaround_bugs;
|
|
|
|
h->flags = avctx->flags;
|
2016-03-21 17:14:31 +02:00
|
|
|
h->poc.prev_poc_msb = 1 << 16;
|
2015-03-23 06:46:49 +02:00
|
|
|
h->recovery_frame = -1;
|
|
|
|
h->frame_recovered = 0;
|
2016-06-12 16:06:58 +02:00
|
|
|
h->poc.prev_frame_num = -1;
|
2017-12-01 06:09:43 +02:00
|
|
|
h->sei.frame_packing.arrangement_cancel_flag = -1;
|
2016-06-18 14:04:17 +02:00
|
|
|
h->sei.unregistered.x264_build = -1;
|
2010-05-26 22:00:59 +03:00
|
|
|
|
2015-04-05 08:49:47 +02:00
|
|
|
h->next_outputed_poc = INT_MIN;
|
2022-01-24 19:00:05 +02:00
|
|
|
for (i = 0; i < FF_ARRAY_ELEMS(h->last_pocs); i++)
|
2015-03-23 06:46:49 +02:00
|
|
|
h->last_pocs[i] = INT_MIN;
|
2010-05-26 22:00:59 +03:00
|
|
|
|
2016-03-22 14:31:21 +02:00
|
|
|
ff_h264_sei_uninit(&h->sei);
|
2010-05-26 22:00:59 +03:00
|
|
|
|
2016-04-13 17:28:26 +02:00
|
|
|
h->nb_slice_ctx = (avctx->active_thread_type & FF_THREAD_SLICE) ? avctx->thread_count : 1;
|
2021-09-14 21:31:53 +02:00
|
|
|
h->slice_ctx = av_calloc(h->nb_slice_ctx, sizeof(*h->slice_ctx));
|
2015-01-17 19:45:29 +02:00
|
|
|
if (!h->slice_ctx) {
|
|
|
|
h->nb_slice_ctx = 0;
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
|
2015-04-17 12:09:50 +02:00
|
|
|
for (i = 0; i < H264_MAX_PICTURE_COUNT; i++) {
|
2021-08-17 21:54:56 +02:00
|
|
|
if ((ret = h264_init_pic(&h->DPB[i])) < 0)
|
|
|
|
return ret;
|
2015-04-17 12:09:50 +02:00
|
|
|
}
|
|
|
|
|
2021-08-17 21:54:56 +02:00
|
|
|
if ((ret = h264_init_pic(&h->cur_pic)) < 0)
|
|
|
|
return ret;
|
2015-04-05 20:51:00 +02:00
|
|
|
|
2021-08-17 21:54:56 +02:00
|
|
|
if ((ret = h264_init_pic(&h->last_pic_for_ec)) < 0)
|
|
|
|
return ret;
|
2015-04-05 20:51:00 +02:00
|
|
|
|
2015-01-17 19:45:29 +02:00
|
|
|
for (i = 0; i < h->nb_slice_ctx; i++)
|
2015-01-28 14:26:24 +02:00
|
|
|
h->slice_ctx[i].h264 = h;
|
2015-01-17 19:45:29 +02:00
|
|
|
|
2015-03-23 06:46:49 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-08-17 21:54:56 +02:00
|
|
|
static void h264_free_pic(H264Context *h, H264Picture *pic)
|
|
|
|
{
|
|
|
|
ff_h264_unref_picture(h, pic);
|
|
|
|
av_frame_free(&pic->f);
|
|
|
|
av_frame_free(&pic->f_grain);
|
|
|
|
}
|
|
|
|
|
2016-04-11 15:44:57 +02:00
|
|
|
static av_cold int h264_decode_end(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
H264Context *h = avctx->priv_data;
|
|
|
|
int i;
|
|
|
|
|
2016-06-20 12:00:29 +02:00
|
|
|
ff_h264_remove_all_refs(h);
|
2016-04-11 15:44:57 +02:00
|
|
|
ff_h264_free_tables(h);
|
|
|
|
|
|
|
|
for (i = 0; i < H264_MAX_PICTURE_COUNT; i++) {
|
2021-08-17 21:54:56 +02:00
|
|
|
h264_free_pic(h, &h->DPB[i]);
|
2016-04-11 15:44:57 +02:00
|
|
|
}
|
2016-06-20 12:00:29 +02:00
|
|
|
memset(h->delayed_pic, 0, sizeof(h->delayed_pic));
|
2016-04-11 15:44:57 +02:00
|
|
|
|
|
|
|
h->cur_pic_ptr = NULL;
|
|
|
|
|
|
|
|
av_freep(&h->slice_ctx);
|
|
|
|
h->nb_slice_ctx = 0;
|
|
|
|
|
2016-06-20 12:00:29 +02:00
|
|
|
ff_h264_sei_uninit(&h->sei);
|
2016-06-20 16:02:10 +02:00
|
|
|
ff_h264_ps_uninit(&h->ps);
|
2016-06-20 12:00:29 +02:00
|
|
|
|
2016-04-11 15:44:57 +02:00
|
|
|
ff_h2645_packet_uninit(&h->pkt);
|
|
|
|
|
2021-08-17 21:54:56 +02:00
|
|
|
h264_free_pic(h, &h->cur_pic);
|
|
|
|
h264_free_pic(h, &h->last_pic_for_ec);
|
2016-04-11 15:44:57 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-07 18:33:01 +02:00
|
|
|
static AVOnce h264_vlc_init = AV_ONCE_INIT;
|
|
|
|
|
2016-12-27 19:54:47 +02:00
|
|
|
static av_cold int h264_decode_init(AVCodecContext *avctx)
|
2015-03-23 06:46:49 +02:00
|
|
|
{
|
|
|
|
H264Context *h = avctx->priv_data;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = h264_init_context(avctx, h);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2015-10-07 18:33:01 +02:00
|
|
|
ret = ff_thread_once(&h264_vlc_init, ff_h264_decode_init_vlc);
|
|
|
|
if (ret != 0) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "pthread_once has failed.");
|
|
|
|
return AVERROR_UNKNOWN;
|
|
|
|
}
|
2015-03-23 06:46:49 +02:00
|
|
|
|
2016-08-01 23:15:07 +02:00
|
|
|
if (avctx->ticks_per_frame == 1) {
|
|
|
|
if(h->avctx->time_base.den < INT_MAX/2) {
|
|
|
|
h->avctx->time_base.den *= 2;
|
|
|
|
} else
|
|
|
|
h->avctx->time_base.num /= 2;
|
2010-05-26 22:00:59 +03:00
|
|
|
}
|
2016-05-18 14:33:33 +02:00
|
|
|
avctx->ticks_per_frame = 2;
|
2010-05-26 22:00:59 +03:00
|
|
|
|
2017-01-09 19:04:42 +02:00
|
|
|
if (!avctx->internal->is_copy) {
|
|
|
|
if (avctx->extradata_size > 0 && avctx->extradata) {
|
|
|
|
ret = ff_h264_decode_extradata(avctx->extradata, avctx->extradata_size,
|
|
|
|
&h->ps, &h->is_avc, &h->nal_length_size,
|
|
|
|
avctx->err_recognition, avctx);
|
2017-08-05 18:36:12 +02:00
|
|
|
if (ret < 0) {
|
|
|
|
int explode = avctx->err_recognition & AV_EF_EXPLODE;
|
|
|
|
av_log(avctx, explode ? AV_LOG_ERROR: AV_LOG_WARNING,
|
|
|
|
"Error decoding the extradata\n");
|
|
|
|
if (explode) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
ret = 0;
|
|
|
|
}
|
2013-08-11 11:08:48 +03:00
|
|
|
}
|
2012-04-10 21:31:03 +03:00
|
|
|
}
|
2010-05-26 22:00:59 +03:00
|
|
|
|
2016-03-22 14:31:21 +02:00
|
|
|
if (h->ps.sps && h->ps.sps->bitstream_restriction_flag &&
|
|
|
|
h->avctx->has_b_frames < h->ps.sps->num_reorder_frames) {
|
|
|
|
h->avctx->has_b_frames = h->ps.sps->num_reorder_frames;
|
2010-02-23 03:08:54 +02:00
|
|
|
}
|
2010-02-23 03:07:39 +02:00
|
|
|
|
2014-03-24 03:24:55 +03:00
|
|
|
ff_h264_flush_change(h);
|
2013-10-22 22:46:09 +03:00
|
|
|
|
2015-04-04 01:02:29 +02:00
|
|
|
if (h->enable_er < 0 && (avctx->active_thread_type & FF_THREAD_SLICE))
|
|
|
|
h->enable_er = 0;
|
|
|
|
|
2015-04-04 01:58:37 +02:00
|
|
|
if (h->enable_er && (avctx->active_thread_type & FF_THREAD_SLICE)) {
|
2015-03-31 10:35:15 +02:00
|
|
|
av_log(avctx, AV_LOG_WARNING,
|
2015-04-04 01:02:29 +02:00
|
|
|
"Error resilience with slice threads is enabled. It is unsafe and unsupported and may crash. "
|
2015-03-31 10:35:15 +02:00
|
|
|
"Use it at your own risk\n");
|
|
|
|
}
|
|
|
|
|
2013-10-22 22:46:09 +03:00
|
|
|
return 0;
|
2011-03-22 23:36:57 +02:00
|
|
|
}
|
|
|
|
|
2003-04-04 17:42:28 +03:00
|
|
|
/**
|
2004-12-18 05:49:07 +02:00
|
|
|
* instantaneous decoder refresh.
|
2003-04-04 17:42:28 +03:00
|
|
|
*/
|
2012-03-16 20:40:05 +03:00
|
|
|
static void idr(H264Context *h)
|
|
|
|
{
|
2011-11-17 01:57:17 +03:00
|
|
|
int i;
|
2010-01-12 22:59:00 +02:00
|
|
|
ff_h264_remove_all_refs(h);
|
2016-03-21 17:14:31 +02:00
|
|
|
h->poc.prev_frame_num =
|
2016-06-12 16:06:58 +02:00
|
|
|
h->poc.prev_frame_num_offset = 0;
|
|
|
|
h->poc.prev_poc_msb = 1<<16;
|
2019-07-23 14:08:59 +02:00
|
|
|
h->poc.prev_poc_lsb = -1;
|
2022-01-24 19:00:05 +02:00
|
|
|
for (i = 0; i < FF_ARRAY_ELEMS(h->last_pocs); i++)
|
2011-11-17 01:57:17 +03:00
|
|
|
h->last_pocs[i] = INT_MIN;
|
2003-04-04 17:42:28 +03:00
|
|
|
}
|
|
|
|
|
2005-05-03 09:53:59 +03:00
|
|
|
/* forget old pics after a seek */
|
2014-03-19 18:52:47 +03:00
|
|
|
void ff_h264_flush_change(H264Context *h)
|
2012-03-16 20:40:05 +03:00
|
|
|
{
|
2013-01-14 04:54:16 +03:00
|
|
|
int i, j;
|
|
|
|
|
2015-04-05 08:49:47 +02:00
|
|
|
h->next_outputed_poc = INT_MIN;
|
2009-06-10 00:16:40 +03:00
|
|
|
h->prev_interlaced_frame = 1;
|
2005-05-03 09:53:59 +03:00
|
|
|
idr(h);
|
2013-03-12 05:20:18 +03:00
|
|
|
|
2016-06-12 16:06:58 +02:00
|
|
|
h->poc.prev_frame_num = -1;
|
2013-02-16 23:14:00 +03:00
|
|
|
if (h->cur_pic_ptr) {
|
2012-11-21 23:34:46 +03:00
|
|
|
h->cur_pic_ptr->reference = 0;
|
2013-01-14 04:54:16 +03:00
|
|
|
for (j=i=0; h->delayed_pic[i]; i++)
|
2013-02-16 23:14:00 +03:00
|
|
|
if (h->delayed_pic[i] != h->cur_pic_ptr)
|
2013-01-14 04:54:16 +03:00
|
|
|
h->delayed_pic[j++] = h->delayed_pic[i];
|
|
|
|
h->delayed_pic[j] = NULL;
|
|
|
|
}
|
2015-02-25 04:22:13 +02:00
|
|
|
ff_h264_unref_picture(h, &h->last_pic_for_ec);
|
|
|
|
|
2013-02-03 13:10:05 +03:00
|
|
|
h->first_field = 0;
|
2013-10-11 19:51:50 +03:00
|
|
|
h->recovery_frame = -1;
|
|
|
|
h->frame_recovered = 0;
|
2012-12-19 21:11:06 +03:00
|
|
|
h->current_slice = 0;
|
2013-10-26 02:22:38 +03:00
|
|
|
h->mmco_reset = 1;
|
2012-12-19 19:33:45 +03:00
|
|
|
}
|
|
|
|
|
2017-07-29 21:21:50 +02:00
|
|
|
static void h264_decode_flush(AVCodecContext *avctx)
|
2012-12-19 19:33:45 +03:00
|
|
|
{
|
|
|
|
H264Context *h = avctx->priv_data;
|
|
|
|
int i;
|
|
|
|
|
2014-11-20 08:40:34 +02:00
|
|
|
memset(h->delayed_pic, 0, sizeof(h->delayed_pic));
|
2012-12-19 19:33:45 +03:00
|
|
|
|
2014-03-19 18:52:47 +03:00
|
|
|
ff_h264_flush_change(h);
|
2017-02-23 23:22:56 +02:00
|
|
|
ff_h264_sei_uninit(&h->sei);
|
2013-02-03 13:10:05 +03:00
|
|
|
|
2015-04-12 21:08:34 +02:00
|
|
|
for (i = 0; i < H264_MAX_PICTURE_COUNT; i++)
|
|
|
|
ff_h264_unref_picture(h, &h->DPB[i]);
|
2013-02-03 13:10:05 +03:00
|
|
|
h->cur_pic_ptr = NULL;
|
2014-03-19 18:52:47 +03:00
|
|
|
ff_h264_unref_picture(h, &h->cur_pic);
|
2013-02-03 13:10:05 +03:00
|
|
|
|
2015-01-17 23:28:46 +02:00
|
|
|
h->mb_y = 0;
|
2013-02-03 13:10:05 +03:00
|
|
|
|
2015-04-06 21:00:42 +02:00
|
|
|
ff_h264_free_tables(h);
|
2013-11-04 19:59:25 +03:00
|
|
|
h->context_initialized = 0;
|
2005-05-03 09:53:59 +03:00
|
|
|
}
|
|
|
|
|
2016-03-21 15:35:48 +02:00
|
|
|
static int get_last_needed_nal(H264Context *h)
|
2014-04-05 03:41:12 +03:00
|
|
|
{
|
|
|
|
int nals_needed = 0;
|
2021-08-17 21:25:31 +02:00
|
|
|
int slice_type = 0;
|
|
|
|
int picture_intra_only = 1;
|
2014-04-07 04:36:48 +03:00
|
|
|
int first_slice = 0;
|
2016-07-20 08:31:38 +02:00
|
|
|
int i, ret;
|
2014-04-05 03:41:12 +03:00
|
|
|
|
2016-03-21 15:35:48 +02:00
|
|
|
for (i = 0; i < h->pkt.nb_nals; i++) {
|
|
|
|
H2645NAL *nal = &h->pkt.nals[i];
|
2015-01-27 15:25:01 +02:00
|
|
|
GetBitContext gb;
|
2014-04-05 03:41:12 +03:00
|
|
|
|
|
|
|
/* packets can sometimes contain multiple PPS/SPS,
|
|
|
|
* e.g. two PAFF field pictures in one packet, or a demuxer
|
|
|
|
* which splits NALs strangely if so, when frame threading we
|
|
|
|
* can't start the next thread until we've read all of them */
|
2016-03-21 15:35:48 +02:00
|
|
|
switch (nal->type) {
|
2016-05-18 10:36:33 +02:00
|
|
|
case H264_NAL_SPS:
|
|
|
|
case H264_NAL_PPS:
|
2016-03-21 15:35:48 +02:00
|
|
|
nals_needed = i;
|
2014-04-05 03:41:12 +03:00
|
|
|
break;
|
2016-05-18 10:36:33 +02:00
|
|
|
case H264_NAL_DPA:
|
|
|
|
case H264_NAL_IDR_SLICE:
|
|
|
|
case H264_NAL_SLICE:
|
2016-07-20 08:31:38 +02:00
|
|
|
ret = init_get_bits8(&gb, nal->data + 1, nal->size - 1);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_log(h->avctx, AV_LOG_ERROR, "Invalid zero-sized VCL NAL unit\n");
|
|
|
|
if (h->avctx->err_recognition & AV_EF_EXPLODE)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2015-12-29 09:04:08 +02:00
|
|
|
if (!get_ue_golomb_long(&gb) || // first_mb_in_slice
|
2014-04-07 04:36:48 +03:00
|
|
|
!first_slice ||
|
2016-05-07 10:31:13 +02:00
|
|
|
first_slice != nal->type)
|
2016-03-21 15:35:48 +02:00
|
|
|
nals_needed = i;
|
2021-08-17 21:25:31 +02:00
|
|
|
slice_type = get_ue_golomb_31(&gb);
|
2021-08-25 05:06:01 +02:00
|
|
|
if (slice_type > 9)
|
|
|
|
slice_type = 0;
|
2021-08-17 21:25:31 +02:00
|
|
|
if (slice_type > 4)
|
|
|
|
slice_type -= 5;
|
|
|
|
|
|
|
|
slice_type = ff_h264_golomb_to_pict_type[slice_type];
|
|
|
|
picture_intra_only &= (slice_type & 3) == AV_PICTURE_TYPE_I;
|
2014-04-07 04:36:48 +03:00
|
|
|
if (!first_slice)
|
2016-05-07 10:31:13 +02:00
|
|
|
first_slice = nal->type;
|
2014-04-05 03:41:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-17 21:25:31 +02:00
|
|
|
h->picture_intra_only = picture_intra_only;
|
|
|
|
|
2014-04-05 03:41:12 +03:00
|
|
|
return nals_needed;
|
|
|
|
}
|
|
|
|
|
2016-06-13 22:56:16 +02:00
|
|
|
static void debug_green_metadata(const H264SEIGreenMetaData *gm, void *logctx)
|
2016-06-13 21:41:53 +02:00
|
|
|
{
|
|
|
|
av_log(logctx, AV_LOG_DEBUG, "Green Metadata Info SEI message\n");
|
|
|
|
av_log(logctx, AV_LOG_DEBUG, " green_metadata_type: %d\n", gm->green_metadata_type);
|
|
|
|
|
|
|
|
if (gm->green_metadata_type == 0) {
|
|
|
|
av_log(logctx, AV_LOG_DEBUG, " green_metadata_period_type: %d\n", gm->period_type);
|
|
|
|
|
|
|
|
if (gm->period_type == 2)
|
|
|
|
av_log(logctx, AV_LOG_DEBUG, " green_metadata_num_seconds: %d\n", gm->num_seconds);
|
|
|
|
else if (gm->period_type == 3)
|
|
|
|
av_log(logctx, AV_LOG_DEBUG, " green_metadata_num_pictures: %d\n", gm->num_pictures);
|
|
|
|
|
|
|
|
av_log(logctx, AV_LOG_DEBUG, " SEI GREEN Complexity Metrics: %f %f %f %f\n",
|
|
|
|
(float)gm->percent_non_zero_macroblocks/255,
|
|
|
|
(float)gm->percent_intra_coded_macroblocks/255,
|
|
|
|
(float)gm->percent_six_tap_filtering/255,
|
|
|
|
(float)gm->percent_alpha_point_deblocking_instance/255);
|
|
|
|
|
|
|
|
} else if (gm->green_metadata_type == 1) {
|
|
|
|
av_log(logctx, AV_LOG_DEBUG, " xsd_metric_type: %d\n", gm->xsd_metric_type);
|
|
|
|
|
|
|
|
if (gm->xsd_metric_type == 0)
|
|
|
|
av_log(logctx, AV_LOG_DEBUG, " xsd_metric_value: %f\n",
|
|
|
|
(float)gm->xsd_metric_value/100);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-29 04:09:14 +02:00
|
|
|
static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size)
|
2012-03-16 20:40:05 +03:00
|
|
|
{
|
2013-02-03 13:10:05 +03:00
|
|
|
AVCodecContext *const avctx = h->avctx;
|
2012-03-16 20:40:05 +03:00
|
|
|
int nals_needed = 0; ///< number of NALs that need decoding before the next frame thread starts
|
2012-12-09 07:04:05 +03:00
|
|
|
int idr_cleared=0;
|
2016-03-21 15:35:48 +02:00
|
|
|
int i, ret = 0;
|
2007-09-05 19:18:15 +03:00
|
|
|
|
2017-01-23 23:33:27 +02:00
|
|
|
h->has_slice = 0;
|
2011-12-16 20:52:56 +03:00
|
|
|
h->nal_unit_type= 0;
|
|
|
|
|
2015-06-29 21:59:37 +02:00
|
|
|
if (!(avctx->flags2 & AV_CODEC_FLAG2_CHUNKS)) {
|
2007-09-05 19:18:15 +03:00
|
|
|
h->current_slice = 0;
|
2018-08-17 16:12:14 +02:00
|
|
|
if (!h->first_field) {
|
2013-02-03 13:10:05 +03:00
|
|
|
h->cur_pic_ptr = NULL;
|
2018-08-17 16:12:14 +02:00
|
|
|
ff_h264_sei_uninit(&h->sei);
|
|
|
|
}
|
2007-03-03 01:35:26 +02:00
|
|
|
}
|
|
|
|
|
2012-11-27 20:06:00 +03:00
|
|
|
if (h->nal_length_size == 4) {
|
|
|
|
if (buf_size > 8 && AV_RB32(buf) == 1 && AV_RB32(buf+5) > (unsigned)buf_size) {
|
|
|
|
h->is_avc = 0;
|
|
|
|
}else if(buf_size > 3 && AV_RB32(buf) > 1 && AV_RB32(buf) <= (unsigned)buf_size)
|
|
|
|
h->is_avc = 1;
|
|
|
|
}
|
|
|
|
|
2018-11-28 02:24:09 +02:00
|
|
|
ret = ff_h2645_packet_split(&h->pkt, buf, buf_size, avctx, h->is_avc, h->nal_length_size,
|
2020-05-28 14:33:58 +02:00
|
|
|
avctx->codec_id, 0, 0);
|
2016-03-21 15:35:48 +02:00
|
|
|
if (ret < 0) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR,
|
|
|
|
"Error splitting the input into NAL units.\n");
|
|
|
|
return ret;
|
|
|
|
}
|
2012-03-16 20:40:05 +03:00
|
|
|
|
2014-04-05 03:41:12 +03:00
|
|
|
if (avctx->active_thread_type & FF_THREAD_FRAME)
|
2016-03-21 15:35:48 +02:00
|
|
|
nals_needed = get_last_needed_nal(h);
|
2016-05-19 00:19:52 +02:00
|
|
|
if (nals_needed < 0)
|
|
|
|
return nals_needed;
|
2012-03-16 20:40:05 +03:00
|
|
|
|
2016-03-21 15:35:48 +02:00
|
|
|
for (i = 0; i < h->pkt.nb_nals; i++) {
|
|
|
|
H2645NAL *nal = &h->pkt.nals[i];
|
2016-06-12 14:22:50 +02:00
|
|
|
int max_slice_ctx, err;
|
2012-03-16 20:40:05 +03:00
|
|
|
|
2016-03-21 15:35:48 +02:00
|
|
|
if (avctx->skip_frame >= AVDISCARD_NONREF &&
|
2016-05-18 10:36:33 +02:00
|
|
|
nal->ref_idc == 0 && nal->type != H264_NAL_SEI)
|
2016-03-21 15:35:48 +02:00
|
|
|
continue;
|
2011-09-18 23:10:52 +03:00
|
|
|
|
2016-03-21 15:35:48 +02:00
|
|
|
// FIXME these should stop being context-global variables
|
|
|
|
h->nal_ref_idc = nal->ref_idc;
|
|
|
|
h->nal_unit_type = nal->type;
|
2012-12-18 16:50:12 +03:00
|
|
|
|
2016-03-21 15:35:48 +02:00
|
|
|
err = 0;
|
|
|
|
switch (nal->type) {
|
2016-05-18 10:36:33 +02:00
|
|
|
case H264_NAL_IDR_SLICE:
|
2016-05-07 10:31:13 +02:00
|
|
|
if ((nal->data[1] & 0xFC) == 0x98) {
|
|
|
|
av_log(h->avctx, AV_LOG_ERROR, "Invalid inter IDR frame\n");
|
|
|
|
h->next_outputed_poc = INT_MIN;
|
|
|
|
ret = -1;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
if(!idr_cleared) {
|
|
|
|
idr(h); // FIXME ensure we don't lose some frames if there is reordering
|
|
|
|
}
|
|
|
|
idr_cleared = 1;
|
|
|
|
h->has_recovery_point = 1;
|
2016-05-18 10:36:33 +02:00
|
|
|
case H264_NAL_SLICE:
|
2017-01-23 23:33:27 +02:00
|
|
|
h->has_slice = 1;
|
2015-04-09 13:50:07 +02:00
|
|
|
|
2017-01-27 01:14:02 +02:00
|
|
|
if ((err = ff_h264_queue_decode_slice(h, nal))) {
|
|
|
|
H264SliceContext *sl = h->slice_ctx + h->nb_slice_ctx_queued;
|
|
|
|
sl->ref_count[0] = sl->ref_count[1] = 0;
|
2016-05-20 12:30:42 +02:00
|
|
|
break;
|
2017-01-27 01:14:02 +02:00
|
|
|
}
|
2016-05-20 12:30:42 +02:00
|
|
|
|
2017-01-20 18:24:52 +02:00
|
|
|
if (h->current_slice == 1) {
|
2016-12-03 16:21:40 +02:00
|
|
|
if (avctx->active_thread_type & FF_THREAD_FRAME &&
|
2017-01-20 18:29:09 +02:00
|
|
|
i >= nals_needed && !h->setup_finished && h->cur_pic_ptr) {
|
|
|
|
ff_thread_finish_setup(avctx);
|
|
|
|
h->setup_finished = 1;
|
|
|
|
}
|
2009-02-27 17:45:55 +02:00
|
|
|
|
2016-03-21 15:35:48 +02:00
|
|
|
if (h->avctx->hwaccel &&
|
2016-05-07 10:31:13 +02:00
|
|
|
(ret = h->avctx->hwaccel->start_frame(h->avctx, buf, buf_size)) < 0)
|
|
|
|
goto end;
|
2011-03-29 18:48:59 +03:00
|
|
|
}
|
2009-07-03 08:52:00 +03:00
|
|
|
|
2016-06-12 14:22:50 +02:00
|
|
|
max_slice_ctx = avctx->hwaccel ? 1 : h->nb_slice_ctx;
|
|
|
|
if (h->nb_slice_ctx_queued == max_slice_ctx) {
|
2017-01-17 16:26:38 +02:00
|
|
|
if (h->avctx->hwaccel) {
|
2016-03-21 15:35:48 +02:00
|
|
|
ret = avctx->hwaccel->decode_slice(avctx, nal->raw_data, nal->raw_size);
|
2017-01-17 16:26:38 +02:00
|
|
|
h->nb_slice_ctx_queued = 0;
|
|
|
|
} else
|
2016-06-12 14:22:50 +02:00
|
|
|
ret = ff_h264_execute_decode_slices(h);
|
|
|
|
if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
|
|
|
|
goto end;
|
2012-03-16 20:40:05 +03:00
|
|
|
}
|
2016-03-21 15:35:48 +02:00
|
|
|
break;
|
2016-05-18 10:36:33 +02:00
|
|
|
case H264_NAL_DPA:
|
|
|
|
case H264_NAL_DPB:
|
|
|
|
case H264_NAL_DPC:
|
2016-03-21 15:35:48 +02:00
|
|
|
avpriv_request_sample(avctx, "data partitioning");
|
|
|
|
break;
|
2016-05-18 10:36:33 +02:00
|
|
|
case H264_NAL_SEI:
|
2022-04-27 22:16:51 +02:00
|
|
|
if (h->setup_finished) {
|
|
|
|
avpriv_request_sample(avctx, "Late SEI");
|
|
|
|
break;
|
|
|
|
}
|
2016-03-22 14:31:21 +02:00
|
|
|
ret = ff_h264_sei_decode(&h->sei, &nal->gb, &h->ps, avctx);
|
2016-06-18 14:04:17 +02:00
|
|
|
h->has_recovery_point = h->has_recovery_point || h->sei.recovery_point.recovery_frame_cnt != -1;
|
2016-06-13 21:41:53 +02:00
|
|
|
if (avctx->debug & FF_DEBUG_GREEN_MD)
|
2016-06-18 14:04:17 +02:00
|
|
|
debug_green_metadata(&h->sei.green_metadata, h->avctx);
|
2016-03-21 15:35:48 +02:00
|
|
|
if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
|
|
|
|
goto end;
|
|
|
|
break;
|
2016-08-01 20:11:05 +02:00
|
|
|
case H264_NAL_SPS: {
|
2016-06-12 13:24:27 +02:00
|
|
|
GetBitContext tmp_gb = nal->gb;
|
2017-11-10 22:18:35 +02:00
|
|
|
if (avctx->hwaccel && avctx->hwaccel->decode_params) {
|
|
|
|
ret = avctx->hwaccel->decode_params(avctx,
|
|
|
|
nal->type,
|
|
|
|
nal->raw_data,
|
|
|
|
nal->raw_size);
|
|
|
|
if (ret < 0)
|
|
|
|
goto end;
|
|
|
|
}
|
2016-06-12 13:24:27 +02:00
|
|
|
if (ff_h264_decode_seq_parameter_set(&tmp_gb, avctx, &h->ps, 0) >= 0)
|
2012-03-16 20:40:05 +03:00
|
|
|
break;
|
2016-05-07 10:31:13 +02:00
|
|
|
av_log(h->avctx, AV_LOG_DEBUG,
|
|
|
|
"SPS decoding failure, trying again with the complete NAL\n");
|
2016-06-12 13:24:27 +02:00
|
|
|
init_get_bits8(&tmp_gb, nal->raw_data + 1, nal->raw_size - 1);
|
|
|
|
if (ff_h264_decode_seq_parameter_set(&tmp_gb, avctx, &h->ps, 0) >= 0)
|
2012-03-16 20:40:05 +03:00
|
|
|
break;
|
2016-06-12 13:24:27 +02:00
|
|
|
ff_h264_decode_seq_parameter_set(&nal->gb, avctx, &h->ps, 1);
|
2016-03-21 15:35:48 +02:00
|
|
|
break;
|
2016-06-12 13:24:27 +02:00
|
|
|
}
|
2016-05-18 10:36:33 +02:00
|
|
|
case H264_NAL_PPS:
|
2017-11-10 22:18:35 +02:00
|
|
|
if (avctx->hwaccel && avctx->hwaccel->decode_params) {
|
|
|
|
ret = avctx->hwaccel->decode_params(avctx,
|
|
|
|
nal->type,
|
|
|
|
nal->raw_data,
|
|
|
|
nal->raw_size);
|
|
|
|
if (ret < 0)
|
|
|
|
goto end;
|
|
|
|
}
|
2016-03-22 14:31:21 +02:00
|
|
|
ret = ff_h264_decode_picture_parameter_set(&nal->gb, avctx, &h->ps,
|
|
|
|
nal->size_bits);
|
2016-03-21 15:35:48 +02:00
|
|
|
if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
|
|
|
|
goto end;
|
|
|
|
break;
|
2016-05-18 10:36:33 +02:00
|
|
|
case H264_NAL_AUD:
|
|
|
|
case H264_NAL_END_SEQUENCE:
|
|
|
|
case H264_NAL_END_STREAM:
|
|
|
|
case H264_NAL_FILLER_DATA:
|
|
|
|
case H264_NAL_SPS_EXT:
|
|
|
|
case H264_NAL_AUXILIARY_SLICE:
|
2016-03-21 15:35:48 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
av_log(avctx, AV_LOG_DEBUG, "Unknown NAL code: %d (%d bits)\n",
|
|
|
|
nal->type, nal->size_bits);
|
|
|
|
}
|
2007-09-05 19:18:15 +03:00
|
|
|
|
2016-03-21 15:35:48 +02:00
|
|
|
if (err < 0) {
|
|
|
|
av_log(h->avctx, AV_LOG_ERROR, "decode_slice_header error\n");
|
2007-09-05 19:18:15 +03:00
|
|
|
}
|
|
|
|
}
|
2016-06-12 14:22:50 +02:00
|
|
|
|
|
|
|
ret = ff_h264_execute_decode_slices(h);
|
|
|
|
if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
|
|
|
|
goto end;
|
2012-03-17 01:24:08 +03:00
|
|
|
|
2019-06-21 16:15:17 +02:00
|
|
|
// set decode_error_flags to allow users to detect concealed decoding errors
|
2022-03-24 21:36:10 +02:00
|
|
|
if ((ret < 0 || h->er.error_occurred) && h->cur_pic_ptr) {
|
2019-06-21 16:15:17 +02:00
|
|
|
h->cur_pic_ptr->f->decode_error_flags |= FF_DECODE_ERROR_DECODE_SLICES;
|
|
|
|
}
|
|
|
|
|
2014-06-24 02:54:39 +03:00
|
|
|
ret = 0;
|
2012-03-17 01:24:08 +03:00
|
|
|
end:
|
2016-02-19 02:31:16 +02:00
|
|
|
|
|
|
|
#if CONFIG_ERROR_RESILIENCE
|
|
|
|
/*
|
|
|
|
* FIXME: Error handling code does not seem to support interlaced
|
|
|
|
* when slices span multiple rows
|
|
|
|
* The ff_er_add_slice calls don't work right for bottom
|
|
|
|
* fields; they cause massive erroneous error concealing
|
|
|
|
* Error marking covers both fields (top and bottom).
|
|
|
|
* This causes a mismatched s->error_count
|
|
|
|
* and a bad error table. Further, the error count goes to
|
|
|
|
* INT_MAX when called for bottom field, because mb_y is
|
|
|
|
* past end by one (callers fault) and resync_mb_y != 0
|
|
|
|
* causes problems for the first MB line, too.
|
|
|
|
*/
|
2017-05-01 21:42:54 +02:00
|
|
|
if (!FIELD_PICTURE(h) && h->current_slice && h->enable_er) {
|
2016-06-12 13:24:27 +02:00
|
|
|
|
2016-05-07 10:31:13 +02:00
|
|
|
H264SliceContext *sl = h->slice_ctx;
|
2016-02-19 02:31:16 +02:00
|
|
|
int use_last_pic = h->last_pic_for_ec.f->buf[0] && !sl->ref_count[0];
|
|
|
|
|
2022-03-24 21:36:10 +02:00
|
|
|
ff_h264_set_erpic(&h->er.cur_pic, h->cur_pic_ptr);
|
2016-02-19 02:31:16 +02:00
|
|
|
|
|
|
|
if (use_last_pic) {
|
2022-03-24 21:36:10 +02:00
|
|
|
ff_h264_set_erpic(&h->er.last_pic, &h->last_pic_for_ec);
|
2016-02-19 02:31:16 +02:00
|
|
|
sl->ref_list[0][0].parent = &h->last_pic_for_ec;
|
|
|
|
memcpy(sl->ref_list[0][0].data, h->last_pic_for_ec.f->data, sizeof(sl->ref_list[0][0].data));
|
|
|
|
memcpy(sl->ref_list[0][0].linesize, h->last_pic_for_ec.f->linesize, sizeof(sl->ref_list[0][0].linesize));
|
|
|
|
sl->ref_list[0][0].reference = h->last_pic_for_ec.reference;
|
|
|
|
} else if (sl->ref_count[0]) {
|
2022-03-24 21:36:10 +02:00
|
|
|
ff_h264_set_erpic(&h->er.last_pic, sl->ref_list[0][0].parent);
|
2016-02-19 02:31:16 +02:00
|
|
|
} else
|
2022-03-24 21:36:10 +02:00
|
|
|
ff_h264_set_erpic(&h->er.last_pic, NULL);
|
2016-02-19 02:31:16 +02:00
|
|
|
|
|
|
|
if (sl->ref_count[1])
|
2022-03-24 21:36:10 +02:00
|
|
|
ff_h264_set_erpic(&h->er.next_pic, sl->ref_list[1][0].parent);
|
2016-02-19 02:31:16 +02:00
|
|
|
|
2022-03-24 21:36:10 +02:00
|
|
|
ff_er_frame_end(&h->er);
|
2016-02-19 02:31:16 +02:00
|
|
|
if (use_last_pic)
|
|
|
|
memset(&sl->ref_list[0][0], 0, sizeof(sl->ref_list[0][0]));
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_ERROR_RESILIENCE */
|
2012-03-17 01:24:08 +03:00
|
|
|
/* clean up */
|
2017-01-23 23:33:27 +02:00
|
|
|
if (h->cur_pic_ptr && !h->droppable && h->has_slice) {
|
2012-11-21 23:34:46 +03:00
|
|
|
ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
|
2013-02-03 13:10:05 +03:00
|
|
|
h->picture_structure == PICT_BOTTOM_FIELD);
|
2012-03-17 01:24:08 +03:00
|
|
|
}
|
|
|
|
|
2016-03-21 15:35:48 +02:00
|
|
|
return (ret < 0) ? ret : buf_size;
|
2003-04-04 17:42:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-12-07 15:03:53 +03:00
|
|
|
* Return the number of bytes consumed for building the current frame.
|
2003-04-04 17:42:28 +03:00
|
|
|
*/
|
2013-02-03 13:10:05 +03:00
|
|
|
static int get_consumed_bytes(int pos, int buf_size)
|
2012-03-16 20:40:05 +03:00
|
|
|
{
|
|
|
|
if (pos == 0)
|
2014-07-17 01:31:06 +03:00
|
|
|
pos = 1; // avoid infinite loops (I doubt that is needed but...)
|
2012-03-16 20:40:05 +03:00
|
|
|
if (pos + 10 > buf_size)
|
2014-07-17 01:31:06 +03:00
|
|
|
pos = buf_size; // oops ;)
|
2003-04-04 17:42:28 +03:00
|
|
|
|
2012-03-16 20:40:05 +03:00
|
|
|
return pos;
|
2003-04-04 17:42:28 +03:00
|
|
|
}
|
|
|
|
|
2020-03-10 19:26:00 +02:00
|
|
|
static int h264_export_enc_params(AVFrame *f, H264Picture *p)
|
|
|
|
{
|
|
|
|
AVVideoEncParams *par;
|
|
|
|
unsigned int nb_mb = p->mb_height * p->mb_width;
|
|
|
|
unsigned int x, y;
|
|
|
|
|
|
|
|
par = av_video_enc_params_create_side_data(f, AV_VIDEO_ENC_PARAMS_H264, nb_mb);
|
|
|
|
if (!par)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
par->qp = p->pps->init_qp;
|
|
|
|
|
|
|
|
par->delta_qp[1][0] = p->pps->chroma_qp_index_offset[0];
|
|
|
|
par->delta_qp[1][1] = p->pps->chroma_qp_index_offset[0];
|
|
|
|
par->delta_qp[2][0] = p->pps->chroma_qp_index_offset[1];
|
|
|
|
par->delta_qp[2][1] = p->pps->chroma_qp_index_offset[1];
|
|
|
|
|
|
|
|
for (y = 0; y < p->mb_height; y++)
|
|
|
|
for (x = 0; x < p->mb_width; x++) {
|
|
|
|
const unsigned int block_idx = y * p->mb_width + x;
|
|
|
|
const unsigned int mb_xy = y * p->mb_stride + x;
|
|
|
|
AVVideoBlockParams *b = av_video_enc_params_block(par, block_idx);
|
|
|
|
|
|
|
|
b->src_x = x * 16;
|
|
|
|
b->src_y = y * 16;
|
|
|
|
b->w = 16;
|
|
|
|
b->h = 16;
|
|
|
|
|
|
|
|
b->delta_qp = p->qscale_table[mb_xy] - par->qp;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-03-17 07:49:10 +03:00
|
|
|
static int output_frame(H264Context *h, AVFrame *dst, H264Picture *srcp)
|
2013-02-18 18:32:18 +03:00
|
|
|
{
|
avcodec/h264, videotoolbox: fix crash after VT decoder fails
The way videotoolbox hooks in as a hwaccel is pretty hacky. The VT decode
API is not invoked until end_frame(), so alloc_frame() returns a dummy
frame with a 1-byte buffer. When end_frame() is eventually called, the
dummy buffer is replaced with the actual decoded data from
VTDecompressionSessionDecodeFrame().
When the VT decoder fails, the frame returned to the h264 decoder from
alloc_frame() remains invalid and should not be used. Before
9747219958060d8c4f697df62e7f172c2a77e6c7, it was accidentally being
returned all the way up to the API user. After that commit, the dummy
frame was unref'd so the user received an error.
However, since that commit, VT hwaccel failures started causing random
segfaults in the h264 decoder. This happened more often on iOS where the
VT implementation is more likely to throw errors on bitstream anomolies.
A recent report of this issue can be see in
http://ffmpeg.org/pipermail/libav-user/2016-November/009831.html
The issue here is that the dummy frame is still referenced internally by the
h264 decoder, as part of the reflist and cur_pic_ptr. Deallocating the
frame causes assertions like this one to trip later on during decoding:
Assertion h->cur_pic_ptr->f->buf[0] failed at src/libavcodec/h264_slice.c:1340
With this commit, we leave the dummy 1-byte frame intact, but avoid returning it
to the user.
This reverts commit 9747219958060d8c4f697df62e7f172c2a77e6c7.
Signed-off-by: wm4 <nfxjfg@googlemail.com>
2017-02-21 20:48:37 +02:00
|
|
|
int ret;
|
|
|
|
|
2021-08-17 21:54:56 +02:00
|
|
|
ret = av_frame_ref(dst, srcp->needs_fg ? srcp->f_grain : srcp->f);
|
2013-02-18 18:32:18 +03:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2021-08-17 21:54:56 +02:00
|
|
|
if (srcp->needs_fg && (ret = av_frame_copy_props(dst, srcp->f)) < 0)
|
|
|
|
return ret;
|
|
|
|
|
2016-06-18 14:04:17 +02:00
|
|
|
av_dict_set(&dst->metadata, "stereo_mode", ff_h264_sei_stereo_mode(&h->sei.frame_packing), 0);
|
2013-06-28 00:09:06 +03:00
|
|
|
|
2013-11-03 19:24:05 +03:00
|
|
|
if (srcp->sei_recovery_frame_cnt == 0)
|
|
|
|
dst->key_frame = 1;
|
2013-02-18 18:32:18 +03:00
|
|
|
|
2020-03-10 19:26:00 +02:00
|
|
|
if (h->avctx->export_side_data & AV_CODEC_EXPORT_DATA_VIDEO_ENC_PARAMS) {
|
|
|
|
ret = h264_export_enc_params(dst, srcp);
|
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2021-08-17 21:54:56 +02:00
|
|
|
if (!(h->avctx->export_side_data & AV_CODEC_EXPORT_DATA_FILM_GRAIN))
|
|
|
|
av_frame_remove_side_data(dst, AV_FRAME_DATA_FILM_GRAIN_PARAMS);
|
|
|
|
|
2013-02-18 18:32:18 +03:00
|
|
|
return 0;
|
2020-03-10 19:26:00 +02:00
|
|
|
fail:
|
|
|
|
av_frame_unref(dst);
|
|
|
|
return ret;
|
2013-02-18 18:32:18 +03:00
|
|
|
}
|
|
|
|
|
2020-05-08 04:09:29 +02:00
|
|
|
static int is_avcc_extradata(const uint8_t *buf, int buf_size)
|
2014-07-23 18:38:41 +03:00
|
|
|
{
|
|
|
|
int cnt= buf[5]&0x1f;
|
|
|
|
const uint8_t *p= buf+6;
|
2018-09-06 11:15:11 +02:00
|
|
|
if (!cnt)
|
|
|
|
return 0;
|
2014-07-23 18:38:41 +03:00
|
|
|
while(cnt--){
|
|
|
|
int nalsize= AV_RB16(p) + 2;
|
2016-04-12 22:32:04 +02:00
|
|
|
if(nalsize > buf_size - (p-buf) || (p[2] & 0x9F) != 7)
|
2014-07-23 18:38:41 +03:00
|
|
|
return 0;
|
|
|
|
p += nalsize;
|
|
|
|
}
|
|
|
|
cnt = *(p++);
|
|
|
|
if(!cnt)
|
|
|
|
return 0;
|
|
|
|
while(cnt--){
|
|
|
|
int nalsize= AV_RB16(p) + 2;
|
2016-04-12 22:32:04 +02:00
|
|
|
if(nalsize > buf_size - (p-buf) || (p[2] & 0x9F) != 8)
|
2014-07-23 18:38:41 +03:00
|
|
|
return 0;
|
|
|
|
p += nalsize;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-01-13 12:18:10 +02:00
|
|
|
static int finalize_frame(H264Context *h, AVFrame *dst, H264Picture *out, int *got_frame)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (((h->avctx->flags & AV_CODEC_FLAG_OUTPUT_CORRUPT) ||
|
|
|
|
(h->avctx->flags2 & AV_CODEC_FLAG2_SHOW_ALL) ||
|
|
|
|
out->recovered)) {
|
|
|
|
|
|
|
|
if (!h->avctx->hwaccel &&
|
|
|
|
(out->field_poc[0] == INT_MAX ||
|
|
|
|
out->field_poc[1] == INT_MAX)
|
|
|
|
) {
|
|
|
|
int p;
|
|
|
|
AVFrame *f = out->f;
|
|
|
|
int field = out->field_poc[0] == INT_MAX;
|
|
|
|
uint8_t *dst_data[4];
|
|
|
|
int linesizes[4];
|
|
|
|
const uint8_t *src_data[4];
|
|
|
|
|
|
|
|
av_log(h->avctx, AV_LOG_DEBUG, "Duplicating field %d to fill missing\n", field);
|
|
|
|
|
|
|
|
for (p = 0; p<4; p++) {
|
|
|
|
dst_data[p] = f->data[p] + (field^1)*f->linesize[p];
|
|
|
|
src_data[p] = f->data[p] + field *f->linesize[p];
|
|
|
|
linesizes[p] = 2*f->linesize[p];
|
|
|
|
}
|
|
|
|
|
|
|
|
av_image_copy(dst_data, linesizes, src_data, linesizes,
|
|
|
|
f->format, f->width, f->height>>1);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = output_frame(h, dst, out);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
*got_frame = 1;
|
|
|
|
|
2021-12-24 03:13:41 +02:00
|
|
|
if (CONFIG_MPEGVIDEODEC) {
|
2017-01-13 12:18:10 +02:00
|
|
|
ff_print_debug_info2(h->avctx, dst, NULL,
|
|
|
|
out->mb_type,
|
|
|
|
out->qscale_table,
|
|
|
|
out->motion_val,
|
2021-08-08 21:23:31 +02:00
|
|
|
out->mb_width, out->mb_height, out->mb_stride, 1);
|
2017-01-13 12:18:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-01-17 11:50:01 +02:00
|
|
|
static int send_next_delayed_frame(H264Context *h, AVFrame *dst_frame,
|
|
|
|
int *got_frame, int buf_index)
|
|
|
|
{
|
|
|
|
int ret, i, out_idx;
|
|
|
|
H264Picture *out = h->delayed_pic[0];
|
|
|
|
|
|
|
|
h->cur_pic_ptr = NULL;
|
|
|
|
h->first_field = 0;
|
|
|
|
|
|
|
|
out_idx = 0;
|
|
|
|
for (i = 1;
|
|
|
|
h->delayed_pic[i] &&
|
|
|
|
!h->delayed_pic[i]->f->key_frame &&
|
|
|
|
!h->delayed_pic[i]->mmco_reset;
|
|
|
|
i++)
|
|
|
|
if (h->delayed_pic[i]->poc < out->poc) {
|
|
|
|
out = h->delayed_pic[i];
|
|
|
|
out_idx = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = out_idx; h->delayed_pic[i]; i++)
|
|
|
|
h->delayed_pic[i] = h->delayed_pic[i + 1];
|
|
|
|
|
|
|
|
if (out) {
|
|
|
|
out->reference &= ~DELAYED_PIC_REF;
|
|
|
|
ret = finalize_frame(h, dst_frame, out, got_frame);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf_index;
|
|
|
|
}
|
|
|
|
|
2022-03-30 21:33:24 +02:00
|
|
|
static int h264_decode_frame(AVCodecContext *avctx, AVFrame *pict,
|
2013-12-27 09:48:13 +03:00
|
|
|
int *got_frame, AVPacket *avpkt)
|
2003-04-04 17:42:28 +03:00
|
|
|
{
|
2009-04-07 18:59:50 +03:00
|
|
|
const uint8_t *buf = avpkt->data;
|
2012-03-16 20:40:05 +03:00
|
|
|
int buf_size = avpkt->size;
|
|
|
|
H264Context *h = avctx->priv_data;
|
2017-01-17 11:50:01 +02:00
|
|
|
int buf_index;
|
2012-11-21 23:34:46 +03:00
|
|
|
int ret;
|
2005-12-17 20:14:38 +02:00
|
|
|
|
2013-07-26 22:03:55 +03:00
|
|
|
h->flags = avctx->flags;
|
2015-05-09 21:54:47 +02:00
|
|
|
h->setup_finished = 0;
|
2016-06-12 14:22:50 +02:00
|
|
|
h->nb_slice_ctx_queued = 0;
|
2003-04-04 17:42:28 +03:00
|
|
|
|
2015-01-11 17:40:27 +02:00
|
|
|
ff_h264_unref_picture(h, &h->last_pic_for_ec);
|
|
|
|
|
2012-03-16 20:40:05 +03:00
|
|
|
/* end of stream, output what is still in the buffers */
|
2017-01-17 11:50:01 +02:00
|
|
|
if (buf_size == 0)
|
|
|
|
return send_next_delayed_frame(h, pict, got_frame, 0);
|
2007-02-21 23:16:07 +02:00
|
|
|
|
2020-05-08 04:09:29 +02:00
|
|
|
if (av_packet_get_side_data(avpkt, AV_PKT_DATA_NEW_EXTRADATA, NULL)) {
|
2021-04-14 14:59:32 +02:00
|
|
|
size_t side_size;
|
2014-07-23 18:39:30 +03:00
|
|
|
uint8_t *side = av_packet_get_side_data(avpkt, AV_PKT_DATA_NEW_EXTRADATA, &side_size);
|
2020-05-08 04:09:29 +02:00
|
|
|
ff_h264_decode_extradata(side, side_size,
|
|
|
|
&h->ps, &h->is_avc, &h->nal_length_size,
|
|
|
|
avctx->err_recognition, avctx);
|
2014-07-23 18:39:30 +03:00
|
|
|
}
|
2018-09-06 11:01:49 +02:00
|
|
|
if (h->is_avc && buf_size >= 9 && buf[0]==1 && buf[2]==0 && (buf[4]&0xFC)==0xFC) {
|
2020-05-08 04:09:29 +02:00
|
|
|
if (is_avcc_extradata(buf, buf_size))
|
2016-06-19 12:17:41 +02:00
|
|
|
return ff_h264_decode_extradata(buf, buf_size,
|
|
|
|
&h->ps, &h->is_avc, &h->nal_length_size,
|
|
|
|
avctx->err_recognition, avctx);
|
2011-12-15 19:23:39 +03:00
|
|
|
}
|
2005-12-17 20:14:38 +02:00
|
|
|
|
2016-03-29 04:09:14 +02:00
|
|
|
buf_index = decode_nal_units(h, buf, buf_size);
|
2012-03-16 20:40:05 +03:00
|
|
|
if (buf_index < 0)
|
2013-08-02 19:36:27 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2003-04-04 17:42:28 +03:00
|
|
|
|
2016-05-18 10:36:33 +02:00
|
|
|
if (!h->cur_pic_ptr && h->nal_unit_type == H264_NAL_END_SEQUENCE) {
|
2011-12-16 20:55:04 +03:00
|
|
|
av_assert0(buf_index <= buf_size);
|
2017-01-17 11:50:01 +02:00
|
|
|
return send_next_delayed_frame(h, pict, got_frame, buf_index);
|
2010-11-23 03:16:37 +02:00
|
|
|
}
|
|
|
|
|
2017-01-23 23:33:27 +02:00
|
|
|
if (!(avctx->flags2 & AV_CODEC_FLAG2_CHUNKS) && (!h->cur_pic_ptr || !h->has_slice)) {
|
2011-12-03 05:31:24 +03:00
|
|
|
if (avctx->skip_frame >= AVDISCARD_NONREF ||
|
|
|
|
buf_size >= 4 && !memcmp("Q264", buf, 4))
|
2011-12-16 20:55:52 +03:00
|
|
|
return buf_size;
|
2007-03-03 23:33:33 +02:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "no frame!\n");
|
2013-08-02 19:36:27 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2007-03-03 23:33:33 +02:00
|
|
|
}
|
|
|
|
|
2015-06-29 21:59:37 +02:00
|
|
|
if (!(avctx->flags2 & AV_CODEC_FLAG2_CHUNKS) ||
|
2013-02-03 13:10:05 +03:00
|
|
|
(h->mb_y >= h->mb_height && h->mb_height)) {
|
2015-08-05 19:54:41 +02:00
|
|
|
if ((ret = ff_h264_field_end(h, &h->slice_ctx[0], 0)) < 0)
|
|
|
|
return ret;
|
2011-03-22 23:36:57 +02:00
|
|
|
|
2012-03-17 06:43:12 +03:00
|
|
|
/* Wait for second field. */
|
2017-01-13 12:18:10 +02:00
|
|
|
if (h->next_output_pic) {
|
|
|
|
ret = finalize_frame(h, pict, h->next_output_pic, got_frame);
|
2013-02-18 18:32:18 +03:00
|
|
|
if (ret < 0)
|
2012-11-21 23:34:46 +03:00
|
|
|
return ret;
|
2007-10-08 20:44:38 +03:00
|
|
|
}
|
2005-01-04 03:12:48 +02:00
|
|
|
}
|
|
|
|
|
2015-01-31 03:37:19 +02:00
|
|
|
av_assert0(pict->buf[0] || !*got_frame);
|
2003-04-04 17:42:28 +03:00
|
|
|
|
2015-01-11 17:40:27 +02:00
|
|
|
ff_h264_unref_picture(h, &h->last_pic_for_ec);
|
|
|
|
|
2013-02-03 13:10:05 +03:00
|
|
|
return get_consumed_bytes(buf_index, buf_size);
|
2003-04-04 17:42:28 +03:00
|
|
|
}
|
|
|
|
|
2015-03-31 10:35:15 +02:00
|
|
|
#define OFFSET(x) offsetof(H264Context, x)
|
|
|
|
#define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
|
2021-04-10 21:06:34 +02:00
|
|
|
#define VDX VD | AV_OPT_FLAG_EXPORT
|
2015-03-31 10:35:15 +02:00
|
|
|
static const AVOption h264_options[] = {
|
2021-04-10 21:06:34 +02:00
|
|
|
{ "is_avc", "is avc", OFFSET(is_avc), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, VDX },
|
|
|
|
{ "nal_length_size", "nal_length_size", OFFSET(nal_length_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 4, VDX },
|
2015-11-21 23:04:39 +02:00
|
|
|
{ "enable_er", "Enable error resilience on damaged frames (unsafe)", OFFSET(enable_er), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, VD },
|
2017-12-23 04:13:00 +02:00
|
|
|
{ "x264_build", "Assume this x264 version if no x264 version found in any SEI", OFFSET(x264_build), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VD },
|
2015-03-31 10:35:15 +02:00
|
|
|
{ NULL },
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVClass h264_class = {
|
2015-04-04 00:49:57 +02:00
|
|
|
.class_name = "H264 Decoder",
|
2015-03-31 10:35:15 +02:00
|
|
|
.item_name = av_default_item_name,
|
|
|
|
.option = h264_options,
|
|
|
|
.version = LIBAVUTIL_VERSION_INT,
|
|
|
|
};
|
|
|
|
|
2022-03-16 22:09:54 +02:00
|
|
|
const FFCodec ff_h264_decoder = {
|
|
|
|
.p.name = "h264",
|
|
|
|
.p.long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
|
|
|
|
.p.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.p.id = AV_CODEC_ID_H264,
|
2012-03-16 20:40:05 +03:00
|
|
|
.priv_data_size = sizeof(H264Context),
|
2016-12-27 19:54:47 +02:00
|
|
|
.init = h264_decode_init,
|
2012-03-29 14:52:21 +03:00
|
|
|
.close = h264_decode_end,
|
2022-03-30 23:28:24 +02:00
|
|
|
FF_CODEC_DECODE_CB(h264_decode_frame),
|
2022-03-16 22:09:54 +02:00
|
|
|
.p.capabilities = /*AV_CODEC_CAP_DRAW_HORIZ_BAND |*/ AV_CODEC_CAP_DR1 |
|
2015-07-07 02:41:27 +02:00
|
|
|
AV_CODEC_CAP_DELAY | AV_CODEC_CAP_SLICE_THREADS |
|
|
|
|
AV_CODEC_CAP_FRAME_THREADS,
|
2020-12-27 18:36:59 +02:00
|
|
|
.hw_configs = (const AVCodecHWConfigInternal *const []) {
|
2017-10-26 01:18:40 +02:00
|
|
|
#if CONFIG_H264_DXVA2_HWACCEL
|
|
|
|
HWACCEL_DXVA2(h264),
|
|
|
|
#endif
|
|
|
|
#if CONFIG_H264_D3D11VA_HWACCEL
|
|
|
|
HWACCEL_D3D11VA(h264),
|
|
|
|
#endif
|
|
|
|
#if CONFIG_H264_D3D11VA2_HWACCEL
|
|
|
|
HWACCEL_D3D11VA2(h264),
|
|
|
|
#endif
|
|
|
|
#if CONFIG_H264_NVDEC_HWACCEL
|
|
|
|
HWACCEL_NVDEC(h264),
|
|
|
|
#endif
|
|
|
|
#if CONFIG_H264_VAAPI_HWACCEL
|
|
|
|
HWACCEL_VAAPI(h264),
|
|
|
|
#endif
|
|
|
|
#if CONFIG_H264_VDPAU_HWACCEL
|
|
|
|
HWACCEL_VDPAU(h264),
|
|
|
|
#endif
|
|
|
|
#if CONFIG_H264_VIDEOTOOLBOX_HWACCEL
|
|
|
|
HWACCEL_VIDEOTOOLBOX(h264),
|
|
|
|
#endif
|
|
|
|
NULL
|
|
|
|
},
|
2022-07-10 00:05:45 +02:00
|
|
|
.caps_internal = FF_CODEC_CAP_EXPORTS_CROPPING |
|
2020-05-27 06:35:28 +02:00
|
|
|
FF_CODEC_CAP_ALLOCATE_PROGRESS | FF_CODEC_CAP_INIT_CLEANUP,
|
2017-07-29 21:21:50 +02:00
|
|
|
.flush = h264_decode_flush,
|
2014-03-19 18:52:47 +03:00
|
|
|
.update_thread_context = ONLY_IF_THREADS_ENABLED(ff_h264_update_thread_context),
|
2021-04-10 21:10:11 +02:00
|
|
|
.update_thread_context_for_user = ONLY_IF_THREADS_ENABLED(ff_h264_update_thread_context_for_user),
|
2022-03-16 22:09:54 +02:00
|
|
|
.p.profiles = NULL_IF_CONFIG_SMALL(ff_h264_profiles),
|
|
|
|
.p.priv_class = &h264_class,
|
2003-04-04 17:42:28 +03:00
|
|
|
};
|