From patchwork Tue Mar 19 19:16:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Niklas Haas X-Patchwork-Id: 47227 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:958e:b0:1a3:31a3:7958 with SMTP id iu14csp1880713pzb; Tue, 19 Mar 2024 12:16:58 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCXDBwwDjcvsr9Wcm/VOPzStF3D9wt5rb1jb8eoGsOcKqqG51l0WTTEPDBfmmXhgZaSiyLCnEjQBleRmjsXE8ADLfZeTlXmzSv03BQ== X-Google-Smtp-Source: AGHT+IG+v4qsWIYOhjM2qvOf4Rtkt2Z5ogzuYTuBPw9YGoXfQOW0N0UspMR1dHgdxba3Nituk1qR X-Received: by 2002:a17:907:7284:b0:a46:9e7d:a3e4 with SMTP id dt4-20020a170907728400b00a469e7da3e4mr8404456ejc.46.1710875818161; Tue, 19 Mar 2024 12:16:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1710875818; cv=none; d=google.com; s=arc-20160816; b=tegNVpGqq/dCyh1HaA5C1cTU5oDkq049ot4BCFyGSv8NIYW4JTjwrKNd5dnjulPLfS WWs10thKuMaAkygxuDvfZwteLIGmscB4XqDTMhBn8MPITasmnQdGMfyOQNUf98b+RoVA +CUSwYHVr7W4+xIeRMqFleTxS51StDfkETz/L/EWgiT9pzYudo8i8K7FzgRyEF3jiZig 73d1r0g7YNtrZDpzCnlmp+/rONQLR6ZHWjyIO2iYpa6a30fPWCeqpXh85pCVdiU+CAlH AuTBzTZoO+0WMEMKn+znlRLZOaxfDnXBADIfLqlq8i/jccB5F0IisrVqbiit3NGrtquX lIHQ== 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=UHCq0qpWRhYjM+/g8CjmbiLM2nnYnw/aI7NfUduSQ2A=; fh=xmAeKtysnShNOmkhiJmYkS30uw4Fu2hvBJ7qlIwukxQ=; b=B/5eu7FqKSOqKyXHTPxecDLQLD3L/bUUj3i9KHhhD1rQbB0OhQ+r/uQLOts6mPzCeI yQhQ7Rc4/75I+Ywbi4CL3GV2EFm1TLgODpwJQxhZas5IUzaLJBMOW55eLDPVpeq5sVF9 cVpr04oI2BU4cO8HwD0iDkvUkyIfQaHuJXJKj0dE+7fVJRlpVtz4RqI/h7jt7ax3AErj 9cCguWpPcuWZgUXM6yuAVrbU8MaVr6l1JzBu9633zM9KDohPtU3sWk8AS1c7+at+zvz1 suLEceidANR3PbJAZ71SQGJt63z3RN9NwIUpJuEVJ9K6p344AEXEPpJlYBaqdtPNAE9P 0CqQ==; 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=poWq0uyO; 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 y10-20020a170906470a00b00a46a4a61562si3511075ejq.668.2024.03.19.12.16.57; Tue, 19 Mar 2024 12:16:58 -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=poWq0uyO; 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 C113B68D41F; Tue, 19 Mar 2024 21:16:54 +0200 (EET) 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 1331B68D316 for ; Tue, 19 Mar 2024 21:16:48 +0200 (EET) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=haasn.xyz; s=mail; t=1710875807; bh=BVUH1HVJ4JhNMtV5zkpWJx6wcHOdsomuQw88W0mbZ8A=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=poWq0uyO4XtZcAaBW++qrr1Pyls56bL5M+ioNeac5cIGluiSzptjk/60vSkB5PF8V shMEVuEXUHcfbpW/Tlx/sVAVCTTm8tVDkb0GZYQFlqeZ/6n9hCucXaxzSCC0YzKxQa f5lxdVpZ2+an1/X0/Wb0XGF4Ri4gnoNrR7fvQPzU= Received: from haasn.dev (unknown [10.30.0.2]) by haasn.dev (Postfix) with ESMTP id C0C284033C; Tue, 19 Mar 2024 20:16:47 +0100 (CET) From: Niklas Haas To: ffmpeg-devel@ffmpeg.org Date: Tue, 19 Mar 2024 20:16:40 +0100 Message-ID: <20240319191642.95217-2-ffmpeg@haasn.xyz> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240319191642.95217-1-ffmpeg@haasn.xyz> References: <20240319191642.95217-1-ffmpeg@haasn.xyz> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/4] avcodec/dovi_rpu: implement T.35 payload synthesis 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: 11SKFv6efUCM From: Niklas Haas 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 */