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 2/4] avcodec/dovi_rpu: implement T.35 payload synthesis
Date: Tue, 19 Mar 2024 20:16:40 +0100
Message-ID: <20240319191642.95217-2-ffmpeg@haasn.xyz> (raw)
In-Reply-To: <20240319191642.95217-1-ffmpeg@haasn.xyz>

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 --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 */
-- 
2.44.0

_______________________________________________
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".

  reply	other threads:[~2024-03-19 19:16 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-19 19:16 [FFmpeg-devel] [PATCH 1/4] fftools/ffmpeg_enc: strip DOVI config record for AV1 Niklas Haas
2024-03-19 19:16 ` Niklas Haas [this message]
2024-03-19 19:16 ` [FFmpeg-devel] [PATCH 3/4] avcodec/libaomenc: encode dovi RPUs as T.35 metadata Niklas Haas
2024-03-19 19:16 ` [FFmpeg-devel] [PATCH 4/4] avcodec/libx265: encode dovi RPUs Niklas Haas
2024-03-19 21:39   ` Derek Buitenhuis
     [not found]     ` <9183F034-A7F5-4683-A932-273E15B9886C@cosmin.at>
2024-03-19 21:59       ` Cosmin Stejerean via ffmpeg-devel
2024-03-19 23:04         ` Niklas Haas
2024-03-19 23:19           ` Vittorio Giovara
2024-03-21 12:09             ` Niklas Haas
2024-03-20 19:30   ` Michael Niedermayer
2024-03-20 21:22     ` Jan Ekström
2024-03-21 12:02       ` Niklas Haas
2024-03-21 10:16 ` [FFmpeg-devel] [PATCH 1/4] fftools/ffmpeg_enc: strip DOVI config record for AV1 Anton Khirnov
2024-03-21 12:11   ` Niklas Haas
2024-03-22  9:41     ` Anton Khirnov
2024-03-22 13:08       ` Niklas Haas
2024-03-22 17:04         ` Niklas Haas
2024-03-23 17:45 ` Niklas Haas

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=20240319191642.95217-2-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