diff mbox series

[FFmpeg-devel,2/4] avcodec/dovi_rpu: implement T.35 payload synthesis

Message ID 20240319191642.95217-2-ffmpeg@haasn.xyz
State New
Headers show
Series [FFmpeg-devel,1/4] fftools/ffmpeg_enc: strip DOVI config record for AV1 | 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 March 19, 2024, 7:16 p.m. UTC
From: Niklas Haas <git@haasn.dev>

This function converts from DOVI_RPU_BUFFER (raw RPU NAL) to the more
standardized T.35 syntax introduced with Dolby Vision Profile 10.
---
 libavcodec/Makefile   |  2 +-
 libavcodec/dovi_rpu.c | 91 +++++++++++++++++++++++++++++++++++++++++++
 libavcodec/dovi_rpu.h | 10 +++++
 3 files changed, 102 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 708434ac76c..0de2fc085ef 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -85,7 +85,7 @@  OBJS-$(CONFIG_CBS_MPEG2)               += cbs_mpeg2.o
 OBJS-$(CONFIG_CBS_VP8)                 += cbs_vp8.o vp8data.o
 OBJS-$(CONFIG_CBS_VP9)                 += cbs_vp9.o
 OBJS-$(CONFIG_DEFLATE_WRAPPER)         += zlib_wrapper.o
-OBJS-$(CONFIG_DOVI_RPU)                += dovi_rpu.o
+OBJS-$(CONFIG_DOVI_RPU)                += dovi_rpu.o h2645_parse.o
 OBJS-$(CONFIG_ERROR_RESILIENCE)        += error_resilience.o
 OBJS-$(CONFIG_EVCPARSE)                += evc_parse.o evc_ps.o
 OBJS-$(CONFIG_EXIF)                    += exif.o tiff_common.o
diff --git a/libavcodec/dovi_rpu.c b/libavcodec/dovi_rpu.c
index 529062be30a..28b414a81e0 100644
--- a/libavcodec/dovi_rpu.c
+++ b/libavcodec/dovi_rpu.c
@@ -23,9 +23,13 @@ 
 
 #include "libavutil/buffer.h"
 
+#include "avcodec.h"
 #include "dovi_rpu.h"
 #include "golomb.h"
 #include "get_bits.h"
+#include "h2645_parse.h"
+#include "itut35.h"
+#include "put_bits.h"
 #include "refstruct.h"
 
 enum {
@@ -478,3 +482,90 @@  fail:
     ff_dovi_ctx_unref(s); /* don't leak potentially invalid state */
     return AVERROR(EINVAL);
 }
+
+int ff_dovi_rpu_to_t35(void *logctx, const uint8_t *raw_rpu, int raw_rpu_size,
+                       AVBufferRef **out_t35)
+{
+    PutBitContext *pb = &(PutBitContext){0};
+    H2645RBSP rbsp = {0};
+    H2645NAL nal = {0};
+    AVBufferRef *t35 = NULL;
+    int ret = AVERROR_INVALIDDATA;
+    const uint8_t *rpu;
+    int rpu_size, pad;
+
+    av_fast_padded_malloc(&rbsp.rbsp_buffer, &rbsp.rbsp_buffer_alloc_size,
+                          raw_rpu_size);
+    if (!rbsp.rbsp_buffer)
+        return AVERROR(ENOMEM);
+
+    ret = ff_h2645_extract_rbsp(raw_rpu, raw_rpu_size, &rbsp, &nal, 1);
+    if (ret < 0)
+        goto error;
+
+    rpu = rbsp.rbsp_buffer;
+    rpu_size = rbsp.rbsp_buffer_size;
+    if (rpu_size < 2 || rpu_size > 512) {
+        av_log(logctx, AV_LOG_ERROR, "Invalid DOVI RPU size: %d\n", rpu_size);
+        goto error;
+    }
+
+    /* Skip NAL prefix */
+    if (rpu[0] != 25) {
+        av_log(logctx, AV_LOG_ERROR, "Invalid NAL prefix: %d\n", rpu[0]);
+        goto error;
+    }
+    rpu++;
+    rpu_size--;
+
+    /* Skip trailing NUL bytes */
+    while (rpu_size && rpu[rpu_size - 1] == 0)
+        rpu_size--;
+
+    /* Validate trailing byte is the expected 0x80 */
+    if (!rpu_size || rpu[rpu_size - 1] != 0x80) {
+        av_log(logctx, AV_LOG_ERROR, "Missing RPU RBSP terminator, "
+               "possibly truncated?\n");
+        goto error;
+    }
+
+    /* Fixed T.35+EMDF header is 11 bytes, EMDF payload size is up to 3 bytes,
+     * and trailing footer is up to 4 bytes */
+    t35 = av_buffer_alloc(rpu_size + 18);
+    if (!t35) {
+        ret = AVERROR(ENOMEM);
+        goto error;
+    }
+
+    init_put_bits(pb, t35->data, t35->size);
+    put_bits(pb,  8, ITU_T_T35_COUNTRY_CODE_US);
+    put_bits(pb, 16, ITU_T_T35_PROVIDER_CODE_DOLBY);
+    put_bits32(pb, 0x800); /* provider_oriented_code */
+    put_bits(pb, 27, 0x01be6841u); /* EMDF header, see above */
+    if (rpu_size > 0xFF) {
+        av_assert2(rpu_size <= 0x10000);
+        put_bits(pb, 8, (rpu_size >> 8) - 1);
+        put_bits(pb, 1, 1); /* read_more */
+        put_bits(pb, 8, rpu_size & 0xFF);
+        put_bits(pb, 1, 0);
+    } else {
+        put_bits(pb, 8, rpu_size);
+        put_bits(pb, 1, 0);
+    }
+    ff_copy_bits(pb, rpu, rpu_size * 8); /* rpu rbsp payload */
+    put_bits(pb, 17, 0x400); /* emdf payload id + emdf_protection */
+
+    pad = pb->bit_left & 7;
+    put_bits(pb, pad, (1 << pad) - 1); /* pad to next byte with 1 bits */
+    t35->size -= put_bytes_left(pb, 0); /* trim unused space */
+    flush_put_bits(pb);
+
+    *out_t35 = t35;
+    ret = 0;
+
+error:
+    av_freep(&rbsp.rbsp_buffer);
+    if (ret)
+        av_buffer_unref(&t35);
+    return ret;
+}
diff --git a/libavcodec/dovi_rpu.h b/libavcodec/dovi_rpu.h
index 51c5fdbb879..0e75689b136 100644
--- a/libavcodec/dovi_rpu.h
+++ b/libavcodec/dovi_rpu.h
@@ -84,4 +84,14 @@  int ff_dovi_rpu_parse(DOVIContext *s, const uint8_t *rpu, size_t rpu_size);
  */
 int ff_dovi_attach_side_data(DOVIContext *s, AVFrame *frame);
 
+/**
+ * Generate a T.35 payload (including country and provider code) from a given
+ * AV_FRAME_DATA_DOVI_RPU_BUFFER, and set *out_t35 to a new reference on
+ * success.
+ *
+ * Returns 0 or a negative error code.
+ */
+int ff_dovi_rpu_to_t35(void *logctx, const uint8_t *raw_rpu, int raw_rpu_size,
+                       AVBufferRef **out_t35);
+
 #endif /* AVCODEC_DOVI_RPU_H */