Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: Niklas Haas <ffmpeg@haasn.xyz>
To: ffmpeg-devel@ffmpeg.org
Cc: Niklas Haas <git@haasn.dev>
Subject: [FFmpeg-devel] [PATCH 10/22] avcodec/dovi_rpuenc: add ff_dovi_configure_ext()
Date: Sun, 28 Jul 2024 12:25:15 +0200
Message-ID: <20240728102527.17991-10-ffmpeg@haasn.xyz> (raw)
In-Reply-To: <20240728102527.17991-1-ffmpeg@haasn.xyz>

From: Niklas Haas <git@haasn.dev>

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)
 {
-- 
2.45.2

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

  parent reply	other threads:[~2024-07-28 10:28 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-28 10:25 [FFmpeg-devel] [PATCH 01/22] avutil/dovi_meta: document static vs dynamic ext blocks Niklas Haas
2024-07-28 10:25 ` [FFmpeg-devel] [PATCH 02/22] avcodec/dovi_rpudec: implement validation for compression Niklas Haas
2024-07-28 10:25 ` [FFmpeg-devel] [PATCH 03/22] avcodec/dovi_rpuenc: also copy ext blocks to dovi ctx Niklas Haas
2024-07-28 10:25 ` [FFmpeg-devel] [PATCH 04/22] avcodec/dovi_rpuenc: eliminate unnecessary loop Niklas Haas
2024-07-28 10:25 ` [FFmpeg-devel] [PATCH 05/22] avcodec/dovi_rpuenc: respect dv_md_compression Niklas Haas
2024-07-28 10:25 ` [FFmpeg-devel] [PATCH 06/22] avcodec/dovi_rpuenc: add `flags` to ff_dovi_rpu_generate() Niklas Haas
2024-07-28 10:25 ` [FFmpeg-devel] [PATCH 07/22] avcodec/dovi_rpuenc: make encapsulation optional Niklas Haas
2024-07-28 10:25 ` [FFmpeg-devel] [PATCH 08/22] avcodec/dovi_rpuenc: add a flag to enable compression Niklas Haas
2024-07-28 10:25 ` [FFmpeg-devel] [PATCH 09/22] avcodec/dovi_rpu: add ff_dovi_get_metadata() Niklas Haas
2024-07-28 10:25 ` Niklas Haas [this message]
2024-07-28 10:25 ` [FFmpeg-devel] [PATCH 11/22] avcodec/dovi_rpuenc: add configuration for compression Niklas Haas
2024-07-28 10:25 ` [FFmpeg-devel] [PATCH 12/22] avcodec/bsf/dovi_rpu: add new bitstream filter Niklas Haas
2024-07-28 10:25 ` [FFmpeg-devel] [PATCH 13/22] avcodec/dovi_rpu: move ext blocks into dedicated struct Niklas Haas
2024-07-28 10:25 ` [FFmpeg-devel] [PATCH 14/22] avcodec/dovi_rpu: separate static ext blocks Niklas Haas
2024-07-28 10:25 ` [FFmpeg-devel] [PATCH 15/22] avcodec/dovi_rpudec: don't unnecessarily allocate DOVIExt Niklas Haas
2024-07-28 10:25 ` [FFmpeg-devel] [PATCH 16/22] avcodec/dovi_rpudec: implement limited DM decompression Niklas Haas
2024-07-28 10:25 ` [FFmpeg-devel] [PATCH 17/22] avcodec/dovi_rpudec: sanitize DM data before decoding Niklas Haas
2024-07-28 10:25 ` [FFmpeg-devel] [PATCH 18/22] avcodec/dovi_rpuenc: implement DM metadata compression Niklas Haas
2024-07-28 10:25 ` [FFmpeg-devel] [PATCH 19/22] avcodec/dovi_rpuenc: slightly improve profile autodetection Niklas Haas
2024-07-28 10:25 ` [FFmpeg-devel] [PATCH 20/22] avcodec/dovi_rpudec: error out on strange RPU formats Niklas Haas
2024-07-28 10:25 ` [FFmpeg-devel] [PATCH 21/22] avcodec/libsvtav1: raise strictness of missing DV error Niklas Haas
2024-08-03 18:08   ` Andreas Rheinhardt
2024-08-04 16:31     ` Niklas Haas
2024-07-28 10:25 ` [FFmpeg-devel] [PATCH 22/22] avcodec/libx265: " Niklas Haas
2024-07-31 12:14 ` [FFmpeg-devel] [PATCH 01/22] avutil/dovi_meta: document static vs dynamic ext blocks Niklas Haas
     [not found]   ` <D964CDCD-7D3D-4373-9F37-0404669324FE@cosmin.at>
2024-08-03  3:53     ` Cosmin Stejerean via ffmpeg-devel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240728102527.17991-10-ffmpeg@haasn.xyz \
    --to=ffmpeg@haasn.xyz \
    --cc=ffmpeg-devel@ffmpeg.org \
    --cc=git@haasn.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git