diff mbox series

[FFmpeg-devel,6/8] avcodec/dovi_rpuenc: disable metadata compression by default

Message ID 20240618194221.26073-7-ffmpeg@haasn.xyz
State New
Headers show
Series DoVi metadata compression | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Niklas Haas June 18, 2024, 7:35 p.m. UTC
From: Niklas Haas <git@haasn.dev>

Keyframes must reset the metadata compression state, so we cannot enable
metadata compression inside the encoders. Solve this by adding a new
flag, rather than removing it entirely, because I plan on adding
a bitstream filter for metadata compression.
---
 libavcodec/dovi_rpu.h    |  3 +++
 libavcodec/dovi_rpuenc.c | 27 +++++++++++++++++++--------
 2 files changed, 22 insertions(+), 8 deletions(-)
diff mbox series

Patch

diff --git a/libavcodec/dovi_rpu.h b/libavcodec/dovi_rpu.h
index 226a769bff..f0d9c24379 100644
--- a/libavcodec/dovi_rpu.h
+++ b/libavcodec/dovi_rpu.h
@@ -126,6 +126,9 @@  int ff_dovi_configure(DOVIContext *s, AVCodecContext *avctx);
 enum {
     FF_DOVI_WRAP_NAL        = 1 << 0, ///< wrap inside NAL RBSP
     FF_DOVI_WRAP_T35        = 1 << 1, ///< wrap inside T.35+EMDF
+
+    FF_DOVI_COMPRESS_VDR    = 1 << 2, ///< enable VDR RPU compression
+    FF_DOVI_COMPRESS_ALL    = FF_DOVI_COMPRESS_VDR,
 };
 
 /**
diff --git a/libavcodec/dovi_rpuenc.c b/libavcodec/dovi_rpuenc.c
index 65c36ed0ae..631733c620 100644
--- a/libavcodec/dovi_rpuenc.c
+++ b/libavcodec/dovi_rpuenc.c
@@ -20,6 +20,8 @@ 
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include <stdbool.h>
+
 #include "libavutil/avassert.h"
 #include "libavutil/crc.h"
 #include "libavutil/mem.h"
@@ -441,9 +443,10 @@  int ff_dovi_rpu_generate(DOVIContext *s, const AVDOVIMetadata *metadata,
     const AVDOVIRpuDataHeader *hdr;
     const AVDOVIDataMapping *mapping;
     const AVDOVIColorMetadata *color;
-    int vdr_dm_metadata_present, vdr_rpu_id, use_prev_vdr_rpu, profile,
+    int vdr_dm_metadata_present, vdr_rpu_id, profile,
         buffer_size, rpu_size, pad, zero_run;
     int num_ext_blocks_v1, num_ext_blocks_v2;
+    int use_prev_vdr_rpu = false;
     uint32_t crc;
     uint8_t *dst;
     if (!metadata) {
@@ -464,12 +467,21 @@  int ff_dovi_rpu_generate(DOVIContext *s, const AVDOVIMetadata *metadata,
     }
 
     vdr_rpu_id = mapping->vdr_rpu_id;
-    for (int i = 0; i <= DOVI_MAX_DM_ID; i++) {
-        if (s->vdr[i] && !memcmp(s->vdr[i], mapping, sizeof(*mapping))) {
-            vdr_rpu_id = i;
-            break;
-        } else if (s->vdr[vdr_rpu_id] && !s->vdr[i]) {
-            vdr_rpu_id = i;
+    if (flags & FF_DOVI_COMPRESS_VDR) {
+        for (int i = 0; i <= DOVI_MAX_DM_ID; i++) {
+            if (s->vdr[i] && !memcmp(s->vdr[i], mapping, sizeof(*mapping))) {
+                use_prev_vdr_rpu = true;
+                vdr_rpu_id = i;
+                break;
+            } else if (s->vdr[vdr_rpu_id] && !s->vdr[i]) {
+                vdr_rpu_id = i;
+            }
+        }
+    } else {
+        /* Flush VDRs to avoid leaking old state after keyframe */
+        for (int i = 0; i <= DOVI_MAX_DM_ID; i++) {
+            if (i != vdr_rpu_id)
+                ff_refstruct_unref(&s->vdr[i]);
         }
     }
 
@@ -513,7 +525,6 @@  int ff_dovi_rpu_generate(DOVIContext *s, const AVDOVIMetadata *metadata,
     }
 
     vdr_dm_metadata_present = memcmp(color, &ff_dovi_color_default, sizeof(*color));
-    use_prev_vdr_rpu = !memcmp(s->vdr[vdr_rpu_id], mapping, sizeof(*mapping));
     if (num_ext_blocks_v1 || num_ext_blocks_v2)
         vdr_dm_metadata_present = 1;