From patchwork Mon Nov 9 16:30:54 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Timo Rothenpieler X-Patchwork-Id: 23446 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 46FF144B7B1 for ; Mon, 9 Nov 2020 18:31:18 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 3207068BD0E; Mon, 9 Nov 2020 18:31:18 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from btbn.de (btbn.de [5.9.118.179]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id C96E568BD02 for ; Mon, 9 Nov 2020 18:31:09 +0200 (EET) Received: from Kryux.localdomain (muedsl-82-207-204-068.citykom.de [82.207.204.68]) by btbn.de (Postfix) with ESMTPSA id 4A19A22F335; Mon, 9 Nov 2020 17:31:09 +0100 (CET) From: Timo Rothenpieler To: ffmpeg-devel@ffmpeg.org Date: Mon, 9 Nov 2020 17:30:54 +0100 Message-Id: <20201109163057.18005-2-timo@rothenpieler.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201109163057.18005-1-timo@rothenpieler.org> References: <20201109163057.18005-1-timo@rothenpieler.org> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 1/4] avcodec/av1dec: expose skip mode frame index X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Timo Rothenpieler , James Almer Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Timo Rothenpieler Co-authored-by: James Almer --- libavcodec/av1dec.c | 89 +++++++++++++++++++++++++++++++++++++++++++++ libavcodec/av1dec.h | 3 ++ 2 files changed, 92 insertions(+) diff --git a/libavcodec/av1dec.c b/libavcodec/av1dec.c index 56712279aa..83295699e1 100644 --- a/libavcodec/av1dec.c +++ b/libavcodec/av1dec.c @@ -145,6 +145,87 @@ static void global_motion_params(AV1DecContext *s) } } +static int get_relative_dist(const AV1RawSequenceHeader *seq, + unsigned int a, unsigned int b) +{ + unsigned int diff, m; + if (!seq->enable_order_hint) + return 0; + diff = a - b; + m = 1 << seq->order_hint_bits_minus_1; + diff = (diff & (m - 1)) - (diff & m); + return diff; +} + +static void skip_mode_params(AV1DecContext *s) +{ + const AV1RawFrameHeader *header = s->raw_frame_header; + const AV1RawSequenceHeader *seq = s->raw_seq; + + int forward_idx, backward_idx; + int forward_hint, backward_hint; + int second_forward_idx, second_forward_hint; + int ref_hint, dist, i; + + s->cur_frame.skip_mode_frame_idx[0] = 0; + s->cur_frame.skip_mode_frame_idx[1] = 0; + + if (header->frame_type == AV1_FRAME_KEY || + header->frame_type == AV1_FRAME_INTRA_ONLY || + !header->reference_select || !seq->enable_order_hint) + return; + + forward_idx = -1; + backward_idx = -1; + for (i = 0; i < AV1_REFS_PER_FRAME; i++) { + ref_hint = s->ref[header->ref_frame_idx[i]].order_hint; + dist = get_relative_dist(seq, ref_hint, header->order_hint); + if (dist < 0) { + if (forward_idx < 0 || + get_relative_dist(seq, ref_hint, forward_hint) > 0) { + forward_idx = i; + forward_hint = ref_hint; + } + } else if (dist > 0) { + if (backward_idx < 0 || + get_relative_dist(seq, ref_hint, backward_hint) < 0) { + backward_idx = i; + backward_hint = ref_hint; + } + } + } + + if (forward_idx < 0) { + return; + } else if (backward_idx >= 0) { + s->cur_frame.skip_mode_frame_idx[0] = + AV1_REF_FRAME_LAST + FFMIN(forward_idx, backward_idx); + s->cur_frame.skip_mode_frame_idx[1] = + AV1_REF_FRAME_LAST + FFMAX(forward_idx, backward_idx); + return; + } + + second_forward_idx = -1; + for (i = 0; i < AV1_REFS_PER_FRAME; i++) { + ref_hint = s->ref[header->ref_frame_idx[i]].order_hint; + if (get_relative_dist(seq, ref_hint, forward_hint) < 0) { + if (second_forward_idx < 0 || + get_relative_dist(seq, ref_hint, second_forward_hint) > 0) { + second_forward_idx = i; + second_forward_hint = ref_hint; + } + } + } + + if (second_forward_idx < 0) + return; + + s->cur_frame.skip_mode_frame_idx[0] = + AV1_REF_FRAME_LAST + FFMIN(forward_idx, second_forward_idx); + s->cur_frame.skip_mode_frame_idx[1] = + AV1_REF_FRAME_LAST + FFMAX(forward_idx, second_forward_idx); +} + static int init_tile_data(AV1DecContext *s) { @@ -318,6 +399,7 @@ static void av1_frame_unref(AVCodecContext *avctx, AV1Frame *f) av_buffer_unref(&f->hwaccel_priv_buf); f->hwaccel_picture_private = NULL; f->spatial_id = f->temporal_id = 0; + f->order_hint = 0; } static int av1_frame_ref(AVCodecContext *avctx, AV1Frame *dst, const AV1Frame *src) @@ -337,12 +419,16 @@ static int av1_frame_ref(AVCodecContext *avctx, AV1Frame *dst, const AV1Frame *s dst->spatial_id = src->spatial_id; dst->temporal_id = src->temporal_id; + memcpy(dst->skip_mode_frame_idx, + src->skip_mode_frame_idx, + 2 * sizeof(uint8_t)); memcpy(dst->gm_type, src->gm_type, AV1_NUM_REF_FRAMES * sizeof(uint8_t)); memcpy(dst->gm_params, src->gm_params, AV1_NUM_REF_FRAMES * 6 * sizeof(int32_t)); + dst->order_hint = src->order_hint; return 0; @@ -613,6 +699,7 @@ static int get_current_frame(AVCodecContext *avctx) } global_motion_params(s); + skip_mode_params(s); return ret; } @@ -740,6 +827,8 @@ static int av1_decode_frame(AVCodecContext *avctx, void *frame, s->cur_frame.spatial_id = header->spatial_id; s->cur_frame.temporal_id = header->temporal_id; + s->cur_frame.order_hint = s->raw_frame_header->order_hint; + if (avctx->hwaccel) { ret = avctx->hwaccel->start_frame(avctx, unit->data, unit->data_size); diff --git a/libavcodec/av1dec.h b/libavcodec/av1dec.h index b58bc53961..8cf50f0d59 100644 --- a/libavcodec/av1dec.h +++ b/libavcodec/av1dec.h @@ -41,6 +41,9 @@ typedef struct AV1Frame { uint8_t gm_type[AV1_NUM_REF_FRAMES]; int32_t gm_params[AV1_NUM_REF_FRAMES][6]; + + uint8_t order_hint; + uint8_t skip_mode_frame_idx[2]; } AV1Frame; typedef struct TileGroupInfo { From patchwork Mon Nov 9 16:30:55 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Timo Rothenpieler X-Patchwork-Id: 23447 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 2D15C44B7B1 for ; Mon, 9 Nov 2020 18:31:19 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 0BEB068BD20; Mon, 9 Nov 2020 18:31:19 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from btbn.de (btbn.de [5.9.118.179]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id F081968BD0B for ; Mon, 9 Nov 2020 18:31:09 +0200 (EET) Received: from Kryux.localdomain (muedsl-82-207-204-068.citykom.de [82.207.204.68]) by btbn.de (Postfix) with ESMTPSA id 70DD322F336; Mon, 9 Nov 2020 17:31:09 +0100 (CET) From: Timo Rothenpieler To: ffmpeg-devel@ffmpeg.org Date: Mon, 9 Nov 2020 17:30:55 +0100 Message-Id: <20201109163057.18005-3-timo@rothenpieler.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201109163057.18005-1-timo@rothenpieler.org> References: <20201109163057.18005-1-timo@rothenpieler.org> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/4] avcodec/av1dec: expose coded_lossless X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Timo Rothenpieler , James Almer Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Timo Rothenpieler Co-authored-by: James Almer --- libavcodec/av1dec.c | 32 +++++++++++++++++++++++++++++++- libavcodec/av1dec.h | 2 ++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/libavcodec/av1dec.c b/libavcodec/av1dec.c index 83295699e1..bde1124434 100644 --- a/libavcodec/av1dec.c +++ b/libavcodec/av1dec.c @@ -226,6 +226,34 @@ static void skip_mode_params(AV1DecContext *s) AV1_REF_FRAME_LAST + FFMAX(forward_idx, second_forward_idx); } +static void coded_lossless_param(AV1DecContext *s) +{ + const AV1RawFrameHeader *header = s->raw_frame_header; + int i; + + if (header->delta_q_y_dc || header->delta_q_u_ac || + header->delta_q_u_dc || header->delta_q_v_ac || + header->delta_q_v_dc) + return; + + s->cur_frame.coded_lossless = 1; + for (i = 0; i < AV1_MAX_SEGMENTS; i++) { + int qindex; + if (header->feature_enabled[i][AV1_SEG_LVL_ALT_Q]) { + qindex = (header->base_q_idx + + header->feature_value[i][AV1_SEG_LVL_ALT_Q]); + } else { + qindex = header->base_q_idx; + } + qindex = av_clip_uintp2(qindex, 8); + + if (qindex) { + s->cur_frame.coded_lossless = 0; + return; + } + } +} + static int init_tile_data(AV1DecContext *s) { @@ -398,7 +426,7 @@ static void av1_frame_unref(AVCodecContext *avctx, AV1Frame *f) ff_thread_release_buffer(avctx, &f->tf); av_buffer_unref(&f->hwaccel_priv_buf); f->hwaccel_picture_private = NULL; - f->spatial_id = f->temporal_id = 0; + f->spatial_id = f->temporal_id = f->coded_lossless = 0; f->order_hint = 0; } @@ -419,6 +447,7 @@ static int av1_frame_ref(AVCodecContext *avctx, AV1Frame *dst, const AV1Frame *s dst->spatial_id = src->spatial_id; dst->temporal_id = src->temporal_id; + dst->coded_lossless = src->coded_lossless; memcpy(dst->skip_mode_frame_idx, src->skip_mode_frame_idx, 2 * sizeof(uint8_t)); @@ -700,6 +729,7 @@ static int get_current_frame(AVCodecContext *avctx) global_motion_params(s); skip_mode_params(s); + coded_lossless_param(s); return ret; } diff --git a/libavcodec/av1dec.h b/libavcodec/av1dec.h index 8cf50f0d59..32a77425cd 100644 --- a/libavcodec/av1dec.h +++ b/libavcodec/av1dec.h @@ -44,6 +44,8 @@ typedef struct AV1Frame { uint8_t order_hint; uint8_t skip_mode_frame_idx[2]; + + int coded_lossless; } AV1Frame; typedef struct TileGroupInfo { From patchwork Mon Nov 9 16:30:56 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Timo Rothenpieler X-Patchwork-Id: 23448 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id E173844B7B1 for ; Mon, 9 Nov 2020 18:31:19 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id BBFA068BD25; Mon, 9 Nov 2020 18:31:19 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from btbn.de (btbn.de [5.9.118.179]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 2772068BD0B for ; Mon, 9 Nov 2020 18:31:10 +0200 (EET) Received: from Kryux.localdomain (muedsl-82-207-204-068.citykom.de [82.207.204.68]) by btbn.de (Postfix) with ESMTPSA id 952C922F339; Mon, 9 Nov 2020 17:31:09 +0100 (CET) From: Timo Rothenpieler To: ffmpeg-devel@ffmpeg.org Date: Mon, 9 Nov 2020 17:30:56 +0100 Message-Id: <20201109163057.18005-4-timo@rothenpieler.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201109163057.18005-1-timo@rothenpieler.org> References: <20201109163057.18005-1-timo@rothenpieler.org> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 3/4] avcodec/nvdec: add support for separate reference frame X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Timo Rothenpieler Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Timo Rothenpieler --- libavcodec/nvdec.c | 40 ++++++++++++++++++++++++++++++++++++++-- libavcodec/nvdec.h | 3 +++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/libavcodec/nvdec.c b/libavcodec/nvdec.c index 6168a7e3f1..52a431c54d 100644 --- a/libavcodec/nvdec.c +++ b/libavcodec/nvdec.c @@ -441,6 +441,7 @@ static void nvdec_fdd_priv_free(void *priv) av_buffer_unref(&cf->idx_ref); av_buffer_unref(&cf->decoder_ref); + av_buffer_unref(&cf->ref_idx_ref); av_freep(&priv); } @@ -465,6 +466,7 @@ static void nvdec_unmap_mapped_frame(void *opaque, uint8_t *data) finish: av_buffer_unref(&unmap_data->idx_ref); av_buffer_unref(&unmap_data->decoder_ref); + av_buffer_unref(&unmap_data->ref_idx_ref); av_free(unmap_data); } @@ -576,7 +578,7 @@ int ff_nvdec_start_frame(AVCodecContext *avctx, AVFrame *frame) ret = AVERROR(ENOMEM); goto fail; } - cf->idx = *(unsigned int*)cf->idx_ref->data; + cf->ref_idx = cf->idx = *(unsigned int*)cf->idx_ref->data; fdd->hwaccel_priv = cf; fdd->hwaccel_priv_free = nvdec_fdd_priv_free; @@ -589,6 +591,40 @@ fail: } +int ff_nvdec_start_frame_sep_ref(AVCodecContext *avctx, AVFrame *frame, int has_sep_ref) +{ + NVDECContext *ctx = avctx->internal->hwaccel_priv_data; + FrameDecodeData *fdd = (FrameDecodeData*)frame->private_ref->data; + NVDECFrame *cf; + int ret; + + ret = ff_nvdec_start_frame(avctx, frame); + if (ret < 0) + return ret; + + cf = fdd->hwaccel_priv; + + if (has_sep_ref) { + if (!cf->ref_idx_ref) { + cf->ref_idx_ref = av_buffer_pool_get(ctx->decoder_pool); + if (!cf->ref_idx_ref) { + av_log(avctx, AV_LOG_ERROR, "No decoder surfaces left\n"); + ret = AVERROR(ENOMEM); + goto fail; + } + } + cf->ref_idx = *(unsigned int*)cf->ref_idx_ref->data; + } else { + av_buffer_unref(&cf->ref_idx_ref); + cf->ref_idx = cf->idx; + } + + return 0; +fail: + nvdec_fdd_priv_free(cf); + return ret; +} + int ff_nvdec_end_frame(AVCodecContext *avctx) { NVDECContext *ctx = avctx->internal->hwaccel_priv_data; @@ -714,5 +750,5 @@ int ff_nvdec_get_ref_idx(AVFrame *frame) if (!cf) return -1; - return cf->idx; + return cf->ref_idx; } diff --git a/libavcodec/nvdec.h b/libavcodec/nvdec.h index 09ae8c37e6..22b66d0bc8 100644 --- a/libavcodec/nvdec.h +++ b/libavcodec/nvdec.h @@ -43,7 +43,9 @@ typedef struct NVDECFrame { unsigned int idx; + unsigned int ref_idx; AVBufferRef *idx_ref; + AVBufferRef *ref_idx_ref; AVBufferRef *decoder_ref; } NVDECFrame; @@ -68,6 +70,7 @@ typedef struct NVDECContext { int ff_nvdec_decode_init(AVCodecContext *avctx); int ff_nvdec_decode_uninit(AVCodecContext *avctx); int ff_nvdec_start_frame(AVCodecContext *avctx, AVFrame *frame); +int ff_nvdec_start_frame_sep_ref(AVCodecContext *avctx, AVFrame *frame, int has_sep_ref); int ff_nvdec_end_frame(AVCodecContext *avctx); int ff_nvdec_simple_end_frame(AVCodecContext *avctx); int ff_nvdec_simple_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, From patchwork Mon Nov 9 16:30:57 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Timo Rothenpieler X-Patchwork-Id: 23449 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id CE79F44B7B1 for ; Mon, 9 Nov 2020 18:31:20 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 8AD4068BD2E; Mon, 9 Nov 2020 18:31:20 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from btbn.de (btbn.de [5.9.118.179]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 4104168BD0C for ; Mon, 9 Nov 2020 18:31:10 +0200 (EET) Received: from Kryux.localdomain (muedsl-82-207-204-068.citykom.de [82.207.204.68]) by btbn.de (Postfix) with ESMTPSA id B76A422F33C; Mon, 9 Nov 2020 17:31:09 +0100 (CET) From: Timo Rothenpieler To: ffmpeg-devel@ffmpeg.org Date: Mon, 9 Nov 2020 17:30:57 +0100 Message-Id: <20201109163057.18005-5-timo@rothenpieler.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201109163057.18005-1-timo@rothenpieler.org> References: <20201109163057.18005-1-timo@rothenpieler.org> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 4/4] avcodec/nvdec: add av1 hwaccel X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Timo Rothenpieler , James Almer Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Timo Rothenpieler Co-authored-by: James Almer --- Changelog | 1 + configure | 2 + libavcodec/Makefile | 1 + libavcodec/av1dec.c | 11 +- libavcodec/hwaccels.h | 1 + libavcodec/nvdec.c | 4 + libavcodec/nvdec.h | 1 + libavcodec/nvdec_av1.c | 347 +++++++++++++++++++++++++++++++++++++++++ libavcodec/version.h | 2 +- 9 files changed, 368 insertions(+), 2 deletions(-) create mode 100644 libavcodec/nvdec_av1.c diff --git a/Changelog b/Changelog index 3aeada17a4..8f5b319ef1 100644 --- a/Changelog +++ b/Changelog @@ -43,6 +43,7 @@ version : - adenorm filter - ADPCM IMA AMV encoder - AMV muxer +- NVDEC AV1 hwaccel version 4.3: diff --git a/configure b/configure index 8a9e9b3cd7..c92824c376 100755 --- a/configure +++ b/configure @@ -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" diff --git a/libavcodec/Makefile b/libavcodec/Makefile index 9d75dd68af..0c8aee39b3 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -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 diff --git a/libavcodec/av1dec.c b/libavcodec/av1dec.c index bde1124434..62c26160fa 100644 --- a/libavcodec/av1dec.c +++ b/libavcodec/av1dec.c @@ -324,7 +324,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) @@ -387,11 +387,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 @@ -972,6 +978,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 diff --git a/libavcodec/hwaccels.h b/libavcodec/hwaccels.h index 18e9079c55..6c698f6764 100644 --- a/libavcodec/hwaccels.h +++ b/libavcodec/hwaccels.h @@ -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; diff --git a/libavcodec/nvdec.c b/libavcodec/nvdec.c index 52a431c54d..48281293ce 100644 --- a/libavcodec/nvdec.c +++ b/libavcodec/nvdec.c @@ -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; diff --git a/libavcodec/nvdec.h b/libavcodec/nvdec.h index 22b66d0bc8..66f3ca59e7 100644 --- a/libavcodec/nvdec.h +++ b/libavcodec/nvdec.h @@ -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; diff --git a/libavcodec/nvdec_av1.c b/libavcodec/nvdec_av1.c new file mode 100644 index 0000000000..89eb43da84 --- /dev/null +++ b/libavcodec/nvdec_av1.c @@ -0,0 +1,347 @@ +/* + * VP9 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 = s->cur_frame.skip_mode_frame_idx[0], + .SkipModeFrame1 = s->cur_frame.skip_mode_frame_idx[1], + + /* 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), +}; diff --git a/libavcodec/version.h b/libavcodec/version.h index 5173d0f090..a595e32832 100644 --- a/libavcodec/version.h +++ b/libavcodec/version.h @@ -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, \