Browse Source

avcodec/nvdec: add av1 hwaccel

Signed-off-by: Timo Rothenpieler <timo@rothenpieler.org>
Co-authored-by: James Almer <jamrial@gmail.com>
tags/n4.4
Timo Rothenpieler 4 years ago
parent
commit
ac5b45abab
9 changed files with 370 additions and 2 deletions
  1. +1
    -0
      Changelog
  2. +2
    -0
      configure
  3. +1
    -0
      libavcodec/Makefile
  4. +10
    -1
      libavcodec/av1dec.c
  5. +1
    -0
      libavcodec/hwaccels.h
  6. +4
    -0
      libavcodec/nvdec.c
  7. +1
    -0
      libavcodec/nvdec.h
  8. +349
    -0
      libavcodec/nvdec_av1.c
  9. +1
    -1
      libavcodec/version.h

+ 1
- 0
Changelog View File

@@ -43,6 +43,7 @@ version <next>:
- adenorm filter
- ADPCM IMA AMV encoder
- AMV muxer
- NVDEC AV1 hwaccel


version 4.3:


+ 2
- 0
configure View File

@@ -2918,6 +2918,8 @@ videotoolbox_hwaccel_deps="videotoolbox pthreads"
videotoolbox_hwaccel_extralibs="-framework QuartzCore"
xvmc_deps="X11_extensions_XvMClib_h"

av1_nvdec_hwaccel_deps="nvdec CUVIDAV1PICPARAMS"
av1_nvdec_hwaccel_select="av1_decoder"
av1_vaapi_hwaccel_deps="vaapi VADecPictureParameterBufferAV1_bit_depth_idx"
av1_vaapi_hwaccel_select="av1_decoder"
h263_vaapi_hwaccel_deps="vaapi"


+ 1
- 0
libavcodec/Makefile View File

@@ -912,6 +912,7 @@ OBJS-$(CONFIG_VAAPI) += vaapi_decode.o
OBJS-$(CONFIG_VIDEOTOOLBOX) += videotoolbox.o
OBJS-$(CONFIG_VDPAU) += vdpau.o

OBJS-$(CONFIG_AV1_NVDEC_HWACCEL) += nvdec_av1.o
OBJS-$(CONFIG_AV1_VAAPI_HWACCEL) += vaapi_av1.o
OBJS-$(CONFIG_H263_VAAPI_HWACCEL) += vaapi_mpeg4.o
OBJS-$(CONFIG_H263_VIDEOTOOLBOX_HWACCEL) += videotoolbox.o


+ 10
- 1
libavcodec/av1dec.c View File

@@ -317,7 +317,7 @@ static int get_pixel_format(AVCodecContext *avctx)
uint8_t bit_depth;
int ret;
enum AVPixelFormat pix_fmt = AV_PIX_FMT_NONE;
#define HWACCEL_MAX (CONFIG_AV1_VAAPI_HWACCEL)
#define HWACCEL_MAX (CONFIG_AV1_NVDEC_HWACCEL + CONFIG_AV1_VAAPI_HWACCEL)
enum AVPixelFormat pix_fmts[HWACCEL_MAX + 2], *fmtp = pix_fmts;

if (seq->seq_profile == 2 && seq->color_config.high_bitdepth)
@@ -380,11 +380,17 @@ static int get_pixel_format(AVCodecContext *avctx)

switch (s->pix_fmt) {
case AV_PIX_FMT_YUV420P:
#if CONFIG_AV1_NVDEC_HWACCEL
*fmtp++ = AV_PIX_FMT_CUDA;
#endif
#if CONFIG_AV1_VAAPI_HWACCEL
*fmtp++ = AV_PIX_FMT_VAAPI;
#endif
break;
case AV_PIX_FMT_YUV420P10:
#if CONFIG_AV1_NVDEC_HWACCEL
*fmtp++ = AV_PIX_FMT_CUDA;
#endif
#if CONFIG_AV1_VAAPI_HWACCEL
*fmtp++ = AV_PIX_FMT_VAAPI;
#endif
@@ -968,6 +974,9 @@ AVCodec ff_av1_decoder = {
.flush = av1_decode_flush,
.profiles = NULL_IF_CONFIG_SMALL(ff_av1_profiles),
.hw_configs = (const AVCodecHWConfigInternal * []) {
#if CONFIG_AV1_NVDEC_HWACCEL
HWACCEL_NVDEC(av1),
#endif
#if CONFIG_AV1_VAAPI_HWACCEL
HWACCEL_VAAPI(av1),
#endif


+ 1
- 0
libavcodec/hwaccels.h View File

@@ -21,6 +21,7 @@

#include "avcodec.h"

extern const AVHWAccel ff_av1_nvdec_hwaccel;
extern const AVHWAccel ff_av1_vaapi_hwaccel;
extern const AVHWAccel ff_h263_vaapi_hwaccel;
extern const AVHWAccel ff_h263_videotoolbox_hwaccel;


+ 4
- 0
libavcodec/nvdec.c View File

@@ -62,6 +62,9 @@ typedef struct NVDECFramePool {
static int map_avcodec_id(enum AVCodecID id)
{
switch (id) {
#if CONFIG_AV1_NVDEC_HWACCEL
case AV_CODEC_ID_AV1: return cudaVideoCodec_AV1;
#endif
case AV_CODEC_ID_H264: return cudaVideoCodec_H264;
case AV_CODEC_ID_HEVC: return cudaVideoCodec_HEVC;
case AV_CODEC_ID_MJPEG: return cudaVideoCodec_JPEG;
@@ -258,6 +261,7 @@ int ff_nvdec_decode_uninit(AVCodecContext *avctx)
NVDECContext *ctx = avctx->internal->hwaccel_priv_data;

av_freep(&ctx->bitstream);
av_freep(&ctx->bitstream_internal);
ctx->bitstream_len = 0;
ctx->bitstream_allocated = 0;



+ 1
- 0
libavcodec/nvdec.h View File

@@ -59,6 +59,7 @@ typedef struct NVDECContext {
uint8_t *bitstream;
int bitstream_len;
unsigned int bitstream_allocated;
uint8_t *bitstream_internal;

unsigned *slice_offsets;
int nb_slices;


+ 349
- 0
libavcodec/nvdec_av1.c View File

@@ -0,0 +1,349 @@
/*
* AV1 HW decode acceleration through NVDEC
*
* Copyright (c) 2020 Timo Rothenpieler
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include "avcodec.h"
#include "nvdec.h"
#include "decode.h"
#include "internal.h"
#include "av1dec.h"


static int get_bit_depth_from_seq(const AV1RawSequenceHeader *seq)
{
if (seq->seq_profile == 2 && seq->color_config.high_bitdepth)
return seq->color_config.twelve_bit ? 12 : 10;
else if (seq->seq_profile <= 2 && seq->color_config.high_bitdepth)
return 10;
else
return 8;
}

static int nvdec_av1_start_frame(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
{
const AV1DecContext *s = avctx->priv_data;
const AV1RawSequenceHeader *seq = s->raw_seq;
const AV1RawFrameHeader *frame_header = s->raw_frame_header;

NVDECContext *ctx = avctx->internal->hwaccel_priv_data;
CUVIDPICPARAMS *pp = &ctx->pic_params;
CUVIDAV1PICPARAMS *ppc = &pp->CodecSpecific.av1;
FrameDecodeData *fdd;
NVDECFrame *cf;
AVFrame *cur_frame = s->cur_frame.tf.f;

unsigned char remap_lr_type[4] = { AV1_RESTORE_NONE, AV1_RESTORE_SWITCHABLE, AV1_RESTORE_WIENER, AV1_RESTORE_SGRPROJ };

int ret, i, j;

ret = ff_nvdec_start_frame_sep_ref(avctx, cur_frame, frame_header->apply_grain);
if (ret < 0)
return ret;

fdd = (FrameDecodeData*)cur_frame->private_ref->data;
cf = (NVDECFrame*)fdd->hwaccel_priv;

*pp = (CUVIDPICPARAMS) {
.PicWidthInMbs = (cur_frame->width + 15) / 16,
.FrameHeightInMbs = (cur_frame->height + 15) / 16,
.CurrPicIdx = cf->idx,
.ref_pic_flag = !!frame_header->refresh_frame_flags,
.intra_pic_flag = frame_header->frame_type == AV1_FRAME_INTRA_ONLY ||
frame_header->frame_type == AV1_FRAME_KEY,

.CodecSpecific.av1 = {
.width = cur_frame->width,
.height = cur_frame->height,

.frame_offset = frame_header->order_hint,
.decodePicIdx = cf->ref_idx,

/* Sequence Header */
.profile = seq->seq_profile,
.use_128x128_superblock = seq->use_128x128_superblock,
.subsampling_x = seq->color_config.subsampling_x,
.subsampling_y = seq->color_config.subsampling_y,
.mono_chrome = seq->color_config.mono_chrome,
.bit_depth_minus8 = get_bit_depth_from_seq(seq) - 8,
.enable_filter_intra = seq->enable_filter_intra,
.enable_intra_edge_filter = seq->enable_intra_edge_filter,
.enable_interintra_compound = seq->enable_interintra_compound,
.enable_masked_compound = seq->enable_masked_compound,
.enable_dual_filter = seq->enable_dual_filter,
.enable_order_hint = seq->enable_order_hint,
.order_hint_bits_minus1 = seq->order_hint_bits_minus_1,
.enable_jnt_comp = seq->enable_jnt_comp,
.enable_superres = seq->enable_superres,
.enable_cdef = seq->enable_cdef,
.enable_restoration = seq->enable_restoration,
.enable_fgs = seq->film_grain_params_present,

/* Frame Header */
.frame_type = frame_header->frame_type,
.show_frame = frame_header->show_frame,
.disable_cdf_update = frame_header->disable_cdf_update,
.allow_screen_content_tools = frame_header->allow_screen_content_tools,
.force_integer_mv = frame_header->force_integer_mv ||
frame_header->frame_type == AV1_FRAME_INTRA_ONLY ||
frame_header->frame_type == AV1_FRAME_KEY,
.coded_denom = frame_header->coded_denom,
.allow_intrabc = frame_header->allow_intrabc,
.allow_high_precision_mv = frame_header->allow_high_precision_mv,
.interp_filter = frame_header->interpolation_filter,
.switchable_motion_mode = frame_header->is_motion_mode_switchable,
.use_ref_frame_mvs = frame_header->use_ref_frame_mvs,
.disable_frame_end_update_cdf = frame_header->disable_frame_end_update_cdf,
.delta_q_present = frame_header->delta_q_present,
.delta_q_res = frame_header->delta_q_res,
.using_qmatrix = frame_header->using_qmatrix,
.coded_lossless = s->cur_frame.coded_lossless,
.use_superres = frame_header->use_superres,
.tx_mode = frame_header->tx_mode,
.reference_mode = frame_header->reference_select,
.allow_warped_motion = frame_header->allow_warped_motion,
.reduced_tx_set = frame_header->reduced_tx_set,
.skip_mode = frame_header->skip_mode_present,

/* Tiling Info */
.num_tile_cols = frame_header->tile_cols,
.num_tile_rows = frame_header->tile_rows,
.context_update_tile_id = frame_header->context_update_tile_id,

/* CDEF */
.cdef_damping_minus_3 = frame_header->cdef_damping_minus_3,
.cdef_bits = frame_header->cdef_bits,

/* SkipModeFrames */
.SkipModeFrame0 = frame_header->skip_mode_present ?
s->cur_frame.skip_mode_frame_idx[0] : 0,
.SkipModeFrame1 = frame_header->skip_mode_present ?
s->cur_frame.skip_mode_frame_idx[1] : 0,

/* QP Information */
.base_qindex = frame_header->base_q_idx,
.qp_y_dc_delta_q = frame_header->delta_q_y_dc,
.qp_u_dc_delta_q = frame_header->delta_q_u_dc,
.qp_v_dc_delta_q = frame_header->delta_q_v_dc,
.qp_u_ac_delta_q = frame_header->delta_q_u_ac,
.qp_v_ac_delta_q = frame_header->delta_q_v_ac,
.qm_y = frame_header->qm_y,
.qm_u = frame_header->qm_u,
.qm_v = frame_header->qm_v,

/* Segmentation */
.segmentation_enabled = frame_header->segmentation_enabled,
.segmentation_update_map = frame_header->segmentation_update_map,
.segmentation_update_data = frame_header->segmentation_update_data,
.segmentation_temporal_update = frame_header->segmentation_temporal_update,

/* Loopfilter */
.loop_filter_level[0] = frame_header->loop_filter_level[0],
.loop_filter_level[1] = frame_header->loop_filter_level[1],
.loop_filter_level_u = frame_header->loop_filter_level[2],
.loop_filter_level_v = frame_header->loop_filter_level[3],
.loop_filter_sharpness = frame_header->loop_filter_sharpness,
.loop_filter_delta_enabled = frame_header->loop_filter_delta_enabled,
.loop_filter_delta_update = frame_header->loop_filter_delta_update,
.loop_filter_mode_deltas[0] = frame_header->loop_filter_mode_deltas[0],
.loop_filter_mode_deltas[1] = frame_header->loop_filter_mode_deltas[1],
.delta_lf_present = frame_header->delta_lf_present,
.delta_lf_res = frame_header->delta_lf_res,
.delta_lf_multi = frame_header->delta_lf_multi,

/* Restoration */
.lr_type[0] = remap_lr_type[frame_header->lr_type[0]],
.lr_type[1] = remap_lr_type[frame_header->lr_type[1]],
.lr_type[2] = remap_lr_type[frame_header->lr_type[2]],
.lr_unit_size[0] = 1 + frame_header->lr_unit_shift,
.lr_unit_size[1] = 1 + frame_header->lr_unit_shift - frame_header->lr_uv_shift,
.lr_unit_size[2] = 1 + frame_header->lr_unit_shift - frame_header->lr_uv_shift,

/* Reference Frames */
.temporal_layer_id = s->cur_frame.temporal_id,
.spatial_layer_id = s->cur_frame.spatial_id,

/* Film Grain Params */
.apply_grain = frame_header->apply_grain,
.overlap_flag = frame_header->overlap_flag,
.scaling_shift_minus8 = frame_header->grain_scaling_minus_8,
.chroma_scaling_from_luma = frame_header->chroma_scaling_from_luma,
.ar_coeff_lag = frame_header->ar_coeff_lag,
.ar_coeff_shift_minus6 = frame_header->ar_coeff_shift_minus_6,
.grain_scale_shift = frame_header->grain_scale_shift,
.clip_to_restricted_range = frame_header->clip_to_restricted_range,
.num_y_points = frame_header->num_y_points,
.num_cb_points = frame_header->num_cb_points,
.num_cr_points = frame_header->num_cr_points,
.random_seed = frame_header->grain_seed,
.cb_mult = frame_header->cb_mult,
.cb_luma_mult = frame_header->cb_luma_mult,
.cb_offset = frame_header->cb_offset,
.cr_mult = frame_header->cr_mult,
.cr_luma_mult = frame_header->cr_luma_mult,
.cr_offset = frame_header->cr_offset
}
};

/* Tiling Info */
for (i = 0; i < frame_header->tile_cols; ++i) {
ppc->tile_widths[i] = frame_header->width_in_sbs_minus_1[i] + 1;
}
for (i = 0; i < frame_header->tile_rows; ++i) {
ppc->tile_heights[i] = frame_header->height_in_sbs_minus_1[i] + 1;
}

/* CDEF */
for (i = 0; i < (1 << frame_header->cdef_bits); ++i) {
ppc->cdef_y_strength[i] = (frame_header->cdef_y_pri_strength[i] & 0x0F) | (frame_header->cdef_y_sec_strength[i] << 4);
ppc->cdef_uv_strength[i] = (frame_header->cdef_uv_pri_strength[i] & 0x0F) | (frame_header->cdef_uv_sec_strength[i] << 4);
}

/* Segmentation */
for (i = 0; i < AV1_MAX_SEGMENTS; ++i) {
ppc->segmentation_feature_mask[i] = 0;
for (j = 0; j < AV1_SEG_LVL_MAX; ++j) {
ppc->segmentation_feature_mask[i] |= frame_header->feature_enabled[i][j] << j;
ppc->segmentation_feature_data[i][j] = frame_header->feature_value[i][j];
}
}

for (i = 0; i < AV1_NUM_REF_FRAMES; ++i) {
/* Loopfilter */
ppc->loop_filter_ref_deltas[i] = frame_header->loop_filter_ref_deltas[i];

/* Reference Frames */
ppc->ref_frame_map[i] = ff_nvdec_get_ref_idx(s->ref[i].tf.f);
}

if (frame_header->primary_ref_frame == AV1_PRIMARY_REF_NONE) {
ppc->primary_ref_frame = -1;
} else {
int8_t pri_ref_idx = frame_header->ref_frame_idx[frame_header->primary_ref_frame];
ppc->primary_ref_frame = ppc->ref_frame_map[pri_ref_idx];
}

for (i = 0; i < AV1_REFS_PER_FRAME; ++i) {
/* Ref Frame List */
int8_t ref_idx = frame_header->ref_frame_idx[i];
AVFrame *ref_frame = s->ref[ref_idx].tf.f;

ppc->ref_frame[i].index = ppc->ref_frame_map[ref_idx];
ppc->ref_frame[i].width = ref_frame->width;
ppc->ref_frame[i].height = ref_frame->height;

/* Global Motion */
ppc->global_motion[i].invalid = !frame_header->is_global[AV1_REF_FRAME_LAST + i];
ppc->global_motion[i].wmtype = s->cur_frame.gm_type[AV1_REF_FRAME_LAST + i];
for (j = 0; j < 6; ++j) {
ppc->global_motion[i].wmmat[j] = s->cur_frame.gm_params[AV1_REF_FRAME_LAST + i][j];
}
}

/* Film Grain Params */
if (frame_header->apply_grain) {
for (i = 0; i < 14; ++i) {
ppc->scaling_points_y[i][0] = frame_header->point_y_value[i];
ppc->scaling_points_y[i][1] = frame_header->point_y_scaling[i];
}
for (i = 0; i < 10; ++i) {
ppc->scaling_points_cb[i][0] = frame_header->point_cb_value[i];
ppc->scaling_points_cb[i][1] = frame_header->point_cb_scaling[i];
ppc->scaling_points_cr[i][0] = frame_header->point_cr_value[i];
ppc->scaling_points_cr[i][1] = frame_header->point_cr_scaling[i];
}
for (i = 0; i < 24; ++i) {
ppc->ar_coeffs_y[i] = (short)frame_header->ar_coeffs_y_plus_128[i] - 128;
}
for (i = 0; i < 25; ++i) {
ppc->ar_coeffs_cb[i] = (short)frame_header->ar_coeffs_cb_plus_128[i] - 128;
ppc->ar_coeffs_cr[i] = (short)frame_header->ar_coeffs_cr_plus_128[i] - 128;
}
}

return 0;
}

static int nvdec_av1_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
{
const AV1DecContext *s = avctx->priv_data;
const AV1RawFrameHeader *frame_header = s->raw_frame_header;
NVDECContext *ctx = avctx->internal->hwaccel_priv_data;
void *tmp;

ctx->nb_slices = frame_header->tile_cols * frame_header->tile_rows;

tmp = av_fast_realloc(ctx->slice_offsets, &ctx->slice_offsets_allocated,
ctx->nb_slices * 2 * sizeof(*ctx->slice_offsets));
if (!tmp) {
return AVERROR(ENOMEM);
}
ctx->slice_offsets = tmp;

/* Shortcut if all tiles are in the same buffer */
if (ctx->nb_slices == s->tg_end - s->tg_start + 1) {
ctx->bitstream = (uint8_t*)buffer;
ctx->bitstream_len = size;

for (int i = 0; i < ctx->nb_slices; ++i) {
ctx->slice_offsets[i*2 ] = s->tile_group_info[i].tile_offset;
ctx->slice_offsets[i*2 + 1] = ctx->slice_offsets[i*2] + s->tile_group_info[i].tile_size;
}

return 0;
}

tmp = av_fast_realloc(ctx->bitstream_internal, &ctx->bitstream_allocated,
ctx->bitstream_len + size);
if (!tmp) {
return AVERROR(ENOMEM);
}
ctx->bitstream = ctx->bitstream_internal = tmp;

memcpy(ctx->bitstream + ctx->bitstream_len, buffer, size);

for (uint32_t tile_num = s->tg_start; tile_num <= s->tg_end; ++tile_num) {
ctx->slice_offsets[tile_num*2 ] = ctx->bitstream_len + s->tile_group_info[tile_num].tile_offset;
ctx->slice_offsets[tile_num*2 + 1] = ctx->slice_offsets[tile_num*2] + s->tile_group_info[tile_num].tile_size;
}
ctx->bitstream_len += size;

return 0;
}

static int nvdec_av1_frame_params(AVCodecContext *avctx, AVBufferRef *hw_frames_ctx)
{
/* Maximum of 8 reference frames, but potentially stored twice due to film grain */
return ff_nvdec_frame_params(avctx, hw_frames_ctx, 8 * 2, 0);
}

const AVHWAccel ff_av1_nvdec_hwaccel = {
.name = "av1_nvdec",
.type = AVMEDIA_TYPE_VIDEO,
.id = AV_CODEC_ID_AV1,
.pix_fmt = AV_PIX_FMT_CUDA,
.start_frame = nvdec_av1_start_frame,
.end_frame = ff_nvdec_simple_end_frame,
.decode_slice = nvdec_av1_decode_slice,
.frame_params = nvdec_av1_frame_params,
.init = ff_nvdec_decode_init,
.uninit = ff_nvdec_decode_uninit,
.priv_data_size = sizeof(NVDECContext),
};

+ 1
- 1
libavcodec/version.h View File

@@ -29,7 +29,7 @@

#define LIBAVCODEC_VERSION_MAJOR 58
#define LIBAVCODEC_VERSION_MINOR 112
#define LIBAVCODEC_VERSION_MICRO 101
#define LIBAVCODEC_VERSION_MICRO 102

#define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
LIBAVCODEC_VERSION_MINOR, \


Loading…
Cancel
Save