From patchwork Sun Jul 28 10:25:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Niklas Haas X-Patchwork-Id: 50790 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:612c:16f:b0:489:2eb3:e4c4 with SMTP id h47csp585075vqi; Sun, 28 Jul 2024 03:36:55 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCVMmrAklUhh1y3VA+nZveP04nLW0E7CjMS7bFdhkBfIeosfF0L2TQQbLHEQT7sv1n3TAGbIhI9f9gSGAESoF5FrZz8cWYpByNBrng== X-Google-Smtp-Source: AGHT+IEeAOdBpusJ0Eetba/+kgMWEnoZBXbfrdqk8fwnuAhNJCjAVBXfpJifWYHsBiWYAoJCGxI9 X-Received: by 2002:a05:6512:b21:b0:52c:d56f:b2cc with SMTP id 2adb3069b0e04-5309b2e0b0bmr2825209e87.58.1722163014751; Sun, 28 Jul 2024 03:36:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1722163014; cv=none; d=google.com; s=arc-20160816; b=eZsy1uSZS5B/WGO5E4gGtvQ0t+zCDYI5leXW2yVVY1G0s4ZRqLtZ4O3pxPh6uoHjHJ Hhavgt6nM1hkJTaGfj1cS6xxNDOYkONg5XC6gC9c2LI2V3VYxmRVr9VkyEDd/ePboB9b E+wpzGyGZSmIOLvmD9aG63HXeV0sdLr5JAEXwL2bX/LOsD6YmRoh36cACqcdfL45Uqim oLqkf6Yib/QQoosk5tzPlp1C+u2N4ErHDwp/HVTb9gFjuUaaq4XltVcjeO1h6GTbH2oj OQn/B1ZA6cWHFUOK7rmFmKXVOnVwOI2grQHkF7gP/NSISeAIaMkSjp6DPW6OAhnqZWaZ S/Xg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:mime-version:references:in-reply-to :message-id:date:to:from:dkim-signature:delivered-to; bh=ztU1jdFcG8MU5g7Eoax2JJzELmC8mnAffaT0GfajOuk=; fh=xmAeKtysnShNOmkhiJmYkS30uw4Fu2hvBJ7qlIwukxQ=; b=YRG4a8bnsSplLKmuO4outFR6tUe4GgQ7gVBEUaQpx2eyZz4LNOneiS3qkSVuCwWhSj LTWUGAXHvdEsUdnagtmvptUw6ko/InFC/cw7MSR88wB1ll/p3YPI6vRi0RwQpI/VcqBp ewdn2JiJVXf8SkiaDVU4svqePglQu7ZK5Fr3GDOCVJqgihgo0iAwehCys5nzpU+MnStp x51rOK23U8wdyCt8iqIWrP6Nb87k2e64bxxOrrC8gc7May+uv2hKtHaZ/w4bVTvKLiPi iqbInRD/aiNTDhZ5yIdDYumrXICggS6H2TDnWf37NtIzy58OVhNiXytOcjJZxhn4y2BG amfA==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@haasn.xyz header.s=mail header.b=tJEFk5de; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id 2adb3069b0e04-52fd5b94bd0si2128805e87.61.2024.07.28.03.36.54; Sun, 28 Jul 2024 03:36:54 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@haasn.xyz header.s=mail header.b=tJEFk5de; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 21E9F68D97E; Sun, 28 Jul 2024 13:26:04 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from haasn.dev (haasn.dev [78.46.187.166]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 87C9B68D90D for ; Sun, 28 Jul 2024 13:25:41 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=haasn.xyz; s=mail; t=1722162337; bh=NvQtT/puK+Vu/dbp03nS539Kw2n6tq1/DZuf5eIMZgc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=tJEFk5deLQpVfv4T0WVA3DIy0Wke4SPAP2+vNmtPOxXmUdM8i6wjUeRaxHF9jYYI3 Vk22EVE/AZxlvsRTCTQ36/CX468uGzRV1dFUSCP0f/7/EGzOzS/lHra31xNQCt22+G Ll7kgDu0umq2/TvvPxolgKdzkv6XYXA14NK17Oh8= Received: from haasn.dev (unknown [10.30.0.2]) by haasn.dev (Postfix) with ESMTP id 5E3F642AFE; Sun, 28 Jul 2024 12:25:37 +0200 (CEST) From: Niklas Haas To: ffmpeg-devel@ffmpeg.org Date: Sun, 28 Jul 2024 12:25:15 +0200 Message-ID: <20240728102527.17991-10-ffmpeg@haasn.xyz> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240728102527.17991-1-ffmpeg@haasn.xyz> References: <20240728102527.17991-1-ffmpeg@haasn.xyz> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 10/22] avcodec/dovi_rpuenc: add ff_dovi_configure_ext() X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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: Niklas Haas Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: n1SDEW8GhPrD From: Niklas Haas More flexible version of ff_dovi_configure() which does not require an AVCodecContext. Usable, for example, inside a bitstream filter. --- libavcodec/dovi_rpu.h | 16 +++++++- libavcodec/dovi_rpuenc.c | 85 +++++++++++++++++++++++++++------------- 2 files changed, 72 insertions(+), 29 deletions(-) diff --git a/libavcodec/dovi_rpu.h b/libavcodec/dovi_rpu.h index 4eb4bc0873..1bbc6ef02c 100644 --- a/libavcodec/dovi_rpu.h +++ b/libavcodec/dovi_rpu.h @@ -26,7 +26,9 @@ #include "libavutil/dovi_meta.h" #include "libavutil/frame.h" + #include "avcodec.h" +#include "codec_par.h" #define DOVI_MAX_DM_ID 15 typedef struct DOVIContext { @@ -125,11 +127,23 @@ int ff_dovi_attach_side_data(DOVIContext *s, AVFrame *frame); /** * Configure the encoder for Dolby Vision encoding. Generates a configuration * record in s->cfg, and attaches it to avctx->coded_side_data. Sets the correct - * profile and compatibility ID based on the tagged AVCodecContext colorspace + * profile and compatibility ID based on the tagged AVCodecParameters colorspace * metadata, and the correct level based on the resolution and tagged framerate. * + * `metadata` should point to the first frame's RPU, if available. If absent, + * auto-detection will be performed, but this can sometimes lead to inaccurate + * results (in particular for HEVC streams with enhancement layers). + * * Returns 0 or a negative error code. */ +int ff_dovi_configure_ext(DOVIContext *s, AVCodecParameters *codecpar, + const AVDOVIMetadata *metadata, + int strict_std_compliance); + +/** + * Helper wrapper around `ff_dovi_configure_ext` which infers the codec + * parameters from an AVCodecContext. + */ int ff_dovi_configure(DOVIContext *s, AVCodecContext *avctx); enum { diff --git a/libavcodec/dovi_rpuenc.c b/libavcodec/dovi_rpuenc.c index 0d4e613a72..ad161809a9 100644 --- a/libavcodec/dovi_rpuenc.c +++ b/libavcodec/dovi_rpuenc.c @@ -52,11 +52,12 @@ static struct { [13] = {7680*4320*120u, 7680, 240, 800}, }; -int ff_dovi_configure(DOVIContext *s, AVCodecContext *avctx) +int ff_dovi_configure_ext(DOVIContext *s, AVCodecParameters *codecpar, + const AVDOVIMetadata *metadata, + int strict_std_compliance) { AVDOVIDecoderConfigurationRecord *cfg; const AVDOVIRpuDataHeader *hdr = NULL; - const AVFrameSideData *sd; int dv_profile, dv_level, bl_compat_id = -1; size_t cfg_size; uint64_t pps; @@ -64,16 +65,13 @@ int ff_dovi_configure(DOVIContext *s, AVCodecContext *avctx) if (!s->enable) goto skip; - sd = av_frame_side_data_get(avctx->decoded_side_data, - avctx->nb_decoded_side_data, AV_FRAME_DATA_DOVI_METADATA); - - if (sd) - hdr = av_dovi_get_header((const AVDOVIMetadata *) sd->data); + if (metadata) + hdr = av_dovi_get_header(metadata); if (s->enable == FF_DOVI_AUTOMATIC && !hdr) goto skip; - switch (avctx->codec_id) { + switch (codecpar->codec_id) { case AV_CODEC_ID_AV1: dv_profile = 10; break; case AV_CODEC_ID_H264: dv_profile = 9; break; case AV_CODEC_ID_HEVC: dv_profile = hdr ? ff_dovi_guess_profile_hevc(hdr) : 8; break; @@ -83,12 +81,12 @@ int ff_dovi_configure(DOVIContext *s, AVCodecContext *avctx) return AVERROR_BUG; } - if (avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL) { + if (strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL) { if (dv_profile == 9) { - if (avctx->pix_fmt != AV_PIX_FMT_YUV420P) + if (codecpar->format != AV_PIX_FMT_YUV420P) dv_profile = 0; } else { - if (avctx->pix_fmt != AV_PIX_FMT_YUV420P10) + if (codecpar->format != AV_PIX_FMT_YUV420P10) dv_profile = 0; } } @@ -115,17 +113,17 @@ int ff_dovi_configure(DOVIContext *s, AVCodecContext *avctx) } /* fall through */ case 8: /* HEVC (or AV1) with BL compatibility */ - if (avctx->colorspace == AVCOL_SPC_BT2020_NCL && - avctx->color_primaries == AVCOL_PRI_BT2020 && - avctx->color_trc == AVCOL_TRC_SMPTE2084) { + if (codecpar->color_space == AVCOL_SPC_BT2020_NCL && + codecpar->color_primaries == AVCOL_PRI_BT2020 && + codecpar->color_trc == AVCOL_TRC_SMPTE2084) { bl_compat_id = 1; - } else if (avctx->colorspace == AVCOL_SPC_BT2020_NCL && - avctx->color_primaries == AVCOL_PRI_BT2020 && - avctx->color_trc == AVCOL_TRC_ARIB_STD_B67) { + } else if (codecpar->color_space == AVCOL_SPC_BT2020_NCL && + codecpar->color_primaries == AVCOL_PRI_BT2020 && + codecpar->color_trc == AVCOL_TRC_ARIB_STD_B67) { bl_compat_id = 4; - } else if (avctx->colorspace == AVCOL_SPC_BT709 && - avctx->color_primaries == AVCOL_PRI_BT709 && - avctx->color_trc == AVCOL_TRC_BT709) { + } else if (codecpar->color_space == AVCOL_SPC_BT709 && + codecpar->color_primaries == AVCOL_PRI_BT709 && + codecpar->color_trc == AVCOL_TRC_BT709) { bl_compat_id = 2; } } @@ -140,9 +138,9 @@ int ff_dovi_configure(DOVIContext *s, AVCodecContext *avctx) goto skip; } - pps = avctx->width * avctx->height; - if (avctx->framerate.num) { - pps = pps * avctx->framerate.num / avctx->framerate.den; + pps = codecpar->width * codecpar->height; + if (codecpar->framerate.num) { + pps = pps * codecpar->framerate.num / codecpar->framerate.den; } else { pps *= 25; /* sanity fallback */ } @@ -151,7 +149,7 @@ int ff_dovi_configure(DOVIContext *s, AVCodecContext *avctx) for (int i = 1; i < FF_ARRAY_ELEMS(dv_levels); i++) { if (pps > dv_levels[i].pps) continue; - if (avctx->width > dv_levels[i].width) + if (codecpar->width > dv_levels[i].width) continue; /* In theory, we should also test the bitrate when known, and * distinguish between main and high tier. In practice, just ignore @@ -162,14 +160,14 @@ int ff_dovi_configure(DOVIContext *s, AVCodecContext *avctx) } if (!dv_level) { - if (avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT) { + if (strict_std_compliance >= FF_COMPLIANCE_STRICT) { av_log(s->logctx, AV_LOG_ERROR, "Coded PPS (%"PRIu64") and width (%d) " - "exceed Dolby Vision limitations\n", pps, avctx->width); + "exceed Dolby Vision limitations\n", pps, codecpar->width); return AVERROR(EINVAL); } else { av_log(s->logctx, AV_LOG_WARNING, "Coded PPS (%"PRIu64") and width (%d) " "exceed Dolby Vision limitations. Ignoring, resulting file " - "may be non-conforming.\n", pps, avctx->width); + "may be non-conforming.\n", pps, codecpar->width); dv_level = FF_ARRAY_ELEMS(dv_levels) - 1; } } @@ -178,7 +176,8 @@ int ff_dovi_configure(DOVIContext *s, AVCodecContext *avctx) if (!cfg) return AVERROR(ENOMEM); - if (!av_packet_side_data_add(&avctx->coded_side_data, &avctx->nb_coded_side_data, + if (!av_packet_side_data_add(&codecpar->coded_side_data, + &codecpar->nb_coded_side_data, AV_PKT_DATA_DOVI_CONF, cfg, cfg_size, 0)) { av_free(cfg); return AVERROR(ENOMEM); @@ -201,6 +200,36 @@ skip: return 0; } +int ff_dovi_configure(DOVIContext *s, AVCodecContext *avctx) +{ + int ret; + const AVFrameSideData *sd; + const AVDOVIMetadata *metadata = NULL; + AVCodecParameters *codecpar = avcodec_parameters_alloc(); + if (!codecpar) + return AVERROR(ENOMEM); + + ret = avcodec_parameters_from_context(codecpar, avctx); + if (ret < 0) + goto fail; + + sd = av_frame_side_data_get(avctx->decoded_side_data, + avctx->nb_decoded_side_data, + AV_FRAME_DATA_DOVI_METADATA); + if (sd) + metadata = (const AVDOVIMetadata *) sd->data; + + ret = ff_dovi_configure_ext(s, codecpar, metadata, avctx->strict_std_compliance); + if (ret < 0) + goto fail; + + ret = avcodec_parameters_to_context(avctx, codecpar); + +fail: + avcodec_parameters_free(&codecpar); + return ret; +} + static inline void put_ue_coef(PutBitContext *pb, const AVDOVIRpuDataHeader *hdr, uint64_t coef) {