From patchwork Mon Mar 13 23:36:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Rapha=C3=ABl_Zumer?= X-Patchwork-Id: 40670 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:d046:b0:cd:afd7:272c with SMTP id hv6csp2152922pzb; Mon, 13 Mar 2023 16:36:41 -0700 (PDT) X-Google-Smtp-Source: AK7set9mUtxdH0wyC6OW/EQW+YyKO1kuCGnTV1rcinPpmulcFjFukRQCqtnMMMbrUvocB3DDLzoG X-Received: by 2002:a17:906:d970:b0:8b2:e6f6:c20a with SMTP id rp16-20020a170906d97000b008b2e6f6c20amr319911ejb.11.1678750601091; Mon, 13 Mar 2023 16:36:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1678750601; cv=none; d=google.com; s=arc-20160816; b=IL4Xfp9Q3KLUdcMQ4aho3P0xvjwqBk2tp1z66ax8ZI4b6zYTPhwMkPZ4ZR1wlsi4gy ZdqnldPsn7ufS/wdSYvEmjeMUw3f1ZZjP2Jia8rb5557vKqqKvHZUl9RZCdEh6RVkqg9 A1A4r1b3aTA08VYnrYlxD25nWqoeuOW5Bu/7OuIRQNRj4mC7P4QE15/ufS40Qd5mn83H jiSgAYdpAt7cOAXBWOMSi3+V1++r8SND1MnfjA9D/19+5C0ZXnttTqJ902U/dTQ2qTB8 5Z+BzK2AlcZX7LqLllSopQYDCCIzXJjAo8xlCBH1x+C3DO3+ycKuV1OeOBDpnX5LrWzC Vjng== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:content-language:to:from:user-agent:mime-version :date:message-id:dkim-signature:delivered-to; bh=A1QaknL62BaHEXFn3MrQ5w8EIBWYoYxsRixZdViKhHY=; b=CjbSY35gr6bpA9K+gjwBy5clqafSpBa2yGoHrlf7vthBK3jtusLUF5QsbnlQChSSDJ ah6n9FnUaBlhmnexZD4WuVJ41DjliH4t+VvlLPve8qfbTMCBBchKbVJRKtBtH2u2nwXk qo+w/p+VjCF3f94lf9xlcGuwGoH3lWA4z6W+mSZ0IHy2CetwW9CW9KKi0sAqVDTCoozP xTWBl36hGzA9M/Q3QhYDqBOMs31vgWOYLPYvEVOIjVOIedr5/Lw/aOIrP/IEnqVKiGC+ lgZVYrVT7Fc8R28RY/owIYUg1sGD0mCOCZ267eYYwXGs6+pOttruNZOCzWOwcxwR2z6U xnEw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@vimeo.com header.s=google header.b=gF6+uw7O; 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; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=vimeo.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id h9-20020a170906584900b00924a95bdf25si688007ejs.315.2023.03.13.16.36.40; Mon, 13 Mar 2023 16:36:41 -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=@vimeo.com header.s=google header.b=gF6+uw7O; 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; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=vimeo.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 5A7F468BD5B; Tue, 14 Mar 2023 01:36:37 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-qt1-f178.google.com (mail-qt1-f178.google.com [209.85.160.178]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 676D168A2CF for ; Tue, 14 Mar 2023 01:36:31 +0200 (EET) Received: by mail-qt1-f178.google.com with SMTP id h19so15064520qtk.7 for ; Mon, 13 Mar 2023 16:36:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vimeo.com; s=google; t=1678750589; h=content-transfer-encoding:content-language:to:subject:from :user-agent:mime-version:date:message-id:from:to:cc:subject:date :message-id:reply-to; bh=Y6BzXCQnFBtAz5rwI4U7p/iNMbOIUr8gnKO2/f5/JRs=; b=gF6+uw7Olk/27y+V+KxKM94iSjmVdmapd14Ilw/MObTy/EZniU0CHmj+lcKqjnPs3U LQq0rydwfBRVX8EYerSW93F2U0KNM6c5OZlPZQ3zC7wOvfkJq0+77yzs1TqfXcZU5Nbt KLee5mcchE38GilnMhGpfqTbMmEpub54RWlvk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678750589; h=content-transfer-encoding:content-language:to:subject:from :user-agent:mime-version:date:message-id:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=Y6BzXCQnFBtAz5rwI4U7p/iNMbOIUr8gnKO2/f5/JRs=; b=sy8BfKrvEXeyyaPh207w9OeBhMecNiSMpFt6979RInB/BahKfTPeMizD3vgsnw83Rr 8SfWWiNJAD9tJgg8oL1Yb1KrOzaIP3kzw4URNGvkOeWRsHq56mM3DrpqYaieDSJOQO1K FRvlDOK+mwAFZRLp2Rwx3MFH9Gmh/4zTYsJh+lRELIBuh5QXwKq+0y7FTB6GEWApewwJ csfs88wRs3GdmfG1HYfbkn7QLmJXFqPD7w4JqTLLqkFvlfO0FEbmPPrNWLKRmzRmCd+j 5gi0X+acJRTj2sGlz+QM+d04maqnTUmuR6iRRF/YGRyGoTKCvviowPSbOEseuBmNy9v4 8aow== X-Gm-Message-State: AO0yUKU9RQ3eObN9bxPexJwPwnG4539IrbpSKE1gA5NGMCPCQkzdukJV EtfXfAS14zUlsm2GnUu9VYZug+eehulDrJgSMtz+Gw== X-Received: by 2002:a05:622a:104c:b0:3b6:3995:2ec2 with SMTP id f12-20020a05622a104c00b003b639952ec2mr65160419qte.19.1678750589415; Mon, 13 Mar 2023 16:36:29 -0700 (PDT) Received: from ?IPV6:2600:4041:6c:7000:1f01:20c:8ba9:815b? ([2600:4041:6c:7000:1f01:20c:8ba9:815b]) by smtp.gmail.com with ESMTPSA id s135-20020a37a98d000000b007290be5557bsm633691qke.38.2023.03.13.16.36.28 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 13 Mar 2023 16:36:28 -0700 (PDT) Message-ID: <863605d4-1eb6-4ca2-645e-1add6da9bc38@vimeo.com> Date: Mon, 13 Mar 2023 19:36:27 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.2.2 From: =?utf-8?q?Rapha=C3=ABl_Zumer?= To: ffmpeg-devel@ffmpeg.org Content-Language: en-US, fr-FR Subject: [FFmpeg-devel] [PATCH v6 1/2] avcodec/avutil: move dynamic HDR10+ metadata parsing to libavutil 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: y8WcTmPIWBLy Signed-off-by: Raphaël Zumer --- libavcodec/Makefile | 6 +- libavcodec/av1dec.c | 6 +- libavcodec/dynamic_hdr10_plus.c | 198 ------------------------------- libavcodec/dynamic_hdr10_plus.h | 35 ------ libavcodec/h2645_sei.c | 6 +- libavcodec/libdav1d.c | 6 +- libavutil/hdr_dynamic_metadata.c | 180 ++++++++++++++++++++++++++++ libavutil/hdr_dynamic_metadata.h | 11 ++ 8 files changed, 203 insertions(+), 245 deletions(-) delete mode 100644 libavcodec/dynamic_hdr10_plus.c delete mode 100644 libavcodec/dynamic_hdr10_plus.h diff --git a/libavcodec/Makefile b/libavcodec/Makefile index abae4909d2..408ecd1e31 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -104,7 +104,7 @@ OBJS-$(CONFIG_H264_SEI) += h264_sei.o h2645_sei.o OBJS-$(CONFIG_HEVCPARSE) += hevc_parse.o hevc_ps.o hevc_data.o \ h2645data.o h2645_parse.o h2645_vui.o OBJS-$(CONFIG_HEVC_SEI) += hevc_sei.o h2645_sei.o \ - dynamic_hdr10_plus.o dynamic_hdr_vivid.o + dynamic_hdr_vivid.o OBJS-$(CONFIG_HPELDSP) += hpeldsp.o OBJS-$(CONFIG_HUFFMAN) += huffman.o OBJS-$(CONFIG_HUFFYUVDSP) += huffyuvdsp.o @@ -250,7 +250,7 @@ OBJS-$(CONFIG_ATRAC3PAL_DECODER) += atrac3plusdec.o atrac3plus.o \ OBJS-$(CONFIG_ATRAC9_DECODER) += atrac9dec.o OBJS-$(CONFIG_AURA_DECODER) += cyuv.o OBJS-$(CONFIG_AURA2_DECODER) += aura.o -OBJS-$(CONFIG_AV1_DECODER) += av1dec.o dynamic_hdr10_plus.o +OBJS-$(CONFIG_AV1_DECODER) += av1dec.o OBJS-$(CONFIG_AV1_CUVID_DECODER) += cuviddec.o OBJS-$(CONFIG_AV1_MEDIACODEC_DECODER) += mediacodecdec.o OBJS-$(CONFIG_AV1_NVENC_ENCODER) += nvenc_av1.o nvenc.o @@ -1082,7 +1082,7 @@ OBJS-$(CONFIG_LIBARIBB24_DECODER) += libaribb24.o ass.o OBJS-$(CONFIG_LIBCELT_DECODER) += libcelt_dec.o OBJS-$(CONFIG_LIBCODEC2_DECODER) += libcodec2.o OBJS-$(CONFIG_LIBCODEC2_ENCODER) += libcodec2.o -OBJS-$(CONFIG_LIBDAV1D_DECODER) += libdav1d.o dynamic_hdr10_plus.o +OBJS-$(CONFIG_LIBDAV1D_DECODER) += libdav1d.o OBJS-$(CONFIG_LIBDAVS2_DECODER) += libdavs2.o OBJS-$(CONFIG_LIBFDK_AAC_DECODER) += libfdk-aacdec.o OBJS-$(CONFIG_LIBFDK_AAC_ENCODER) += libfdk-aacenc.o diff --git a/libavcodec/av1dec.c b/libavcodec/av1dec.c index a80e37e33f..df393fe3d0 100644 --- a/libavcodec/av1dec.c +++ b/libavcodec/av1dec.c @@ -20,6 +20,7 @@ #include "config_components.h" +#include "libavutil/hdr_dynamic_metadata.h" #include "libavutil/film_grain_params.h" #include "libavutil/mastering_display_metadata.h" #include "libavutil/pixdesc.h" @@ -30,7 +31,6 @@ #include "bytestream.h" #include "codec_internal.h" #include "decode.h" -#include "dynamic_hdr10_plus.h" #include "hwconfig.h" #include "profiles.h" #include "thread.h" @@ -925,8 +925,8 @@ static int export_itut_t35(AVCodecContext *avctx, AVFrame *frame, if (!hdrplus) return AVERROR(ENOMEM); - ret = ff_parse_itu_t_t35_to_dynamic_hdr10_plus(hdrplus, gb.buffer, - bytestream2_get_bytes_left(&gb)); + ret = av_dynamic_hdr_plus_from_t35(hdrplus, gb.buffer, + bytestream2_get_bytes_left(&gb)); if (ret < 0) return ret; break; diff --git a/libavcodec/dynamic_hdr10_plus.c b/libavcodec/dynamic_hdr10_plus.c deleted file mode 100644 index 34a44aac65..0000000000 --- a/libavcodec/dynamic_hdr10_plus.c +++ /dev/null @@ -1,198 +0,0 @@ -/* - * This file is part of FFmpeg. - * - * FFmpeg is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * FFmpeg is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with FFmpeg; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#include "dynamic_hdr10_plus.h" -#include "get_bits.h" - -static const int64_t luminance_den = 1; -static const int32_t peak_luminance_den = 15; -static const int64_t rgb_den = 100000; -static const int32_t fraction_pixel_den = 1000; -static const int32_t knee_point_den = 4095; -static const int32_t bezier_anchor_den = 1023; -static const int32_t saturation_weight_den = 8; - -int ff_parse_itu_t_t35_to_dynamic_hdr10_plus(AVDynamicHDRPlus *s, const uint8_t *data, - int size) -{ - GetBitContext gbc, *gb = &gbc; - int ret; - - if (!s) - return AVERROR(ENOMEM); - - ret = init_get_bits8(gb, data, size); - if (ret < 0) - return ret; - - if (get_bits_left(gb) < 10) - return AVERROR_INVALIDDATA; - - s->application_version = get_bits(gb, 8); - s->num_windows = get_bits(gb, 2); - - if (s->num_windows < 1 || s->num_windows > 3) { - return AVERROR_INVALIDDATA; - } - - if (get_bits_left(gb) < ((19 * 8 + 1) * (s->num_windows - 1))) - return AVERROR_INVALIDDATA; - - for (int w = 1; w < s->num_windows; w++) { - // The corners are set to absolute coordinates here. They should be - // converted to the relative coordinates (in [0, 1]) in the decoder. - AVHDRPlusColorTransformParams *params = &s->params[w]; - params->window_upper_left_corner_x = - (AVRational){get_bits(gb, 16), 1}; - params->window_upper_left_corner_y = - (AVRational){get_bits(gb, 16), 1}; - params->window_lower_right_corner_x = - (AVRational){get_bits(gb, 16), 1}; - params->window_lower_right_corner_y = - (AVRational){get_bits(gb, 16), 1}; - - params->center_of_ellipse_x = get_bits(gb, 16); - params->center_of_ellipse_y = get_bits(gb, 16); - params->rotation_angle = get_bits(gb, 8); - params->semimajor_axis_internal_ellipse = get_bits(gb, 16); - params->semimajor_axis_external_ellipse = get_bits(gb, 16); - params->semiminor_axis_external_ellipse = get_bits(gb, 16); - params->overlap_process_option = get_bits1(gb); - } - - if (get_bits_left(gb) < 28) - return AVERROR_INVALIDDATA; - - s->targeted_system_display_maximum_luminance = - (AVRational){get_bits_long(gb, 27), luminance_den}; - s->targeted_system_display_actual_peak_luminance_flag = get_bits1(gb); - - if (s->targeted_system_display_actual_peak_luminance_flag) { - int rows, cols; - if (get_bits_left(gb) < 10) - return AVERROR_INVALIDDATA; - rows = get_bits(gb, 5); - cols = get_bits(gb, 5); - if (((rows < 2) || (rows > 25)) || ((cols < 2) || (cols > 25))) { - return AVERROR_INVALIDDATA; - } - s->num_rows_targeted_system_display_actual_peak_luminance = rows; - s->num_cols_targeted_system_display_actual_peak_luminance = cols; - - if (get_bits_left(gb) < (rows * cols * 4)) - return AVERROR_INVALIDDATA; - - for (int i = 0; i < rows; i++) { - for (int j = 0; j < cols; j++) { - s->targeted_system_display_actual_peak_luminance[i][j] = - (AVRational){get_bits(gb, 4), peak_luminance_den}; - } - } - } - for (int w = 0; w < s->num_windows; w++) { - AVHDRPlusColorTransformParams *params = &s->params[w]; - if (get_bits_left(gb) < (3 * 17 + 17 + 4)) - return AVERROR_INVALIDDATA; - - for (int i = 0; i < 3; i++) { - params->maxscl[i] = - (AVRational){get_bits(gb, 17), rgb_den}; - } - params->average_maxrgb = - (AVRational){get_bits(gb, 17), rgb_den}; - params->num_distribution_maxrgb_percentiles = get_bits(gb, 4); - - if (get_bits_left(gb) < - (params->num_distribution_maxrgb_percentiles * 24)) - return AVERROR_INVALIDDATA; - - for (int i = 0; i < params->num_distribution_maxrgb_percentiles; i++) { - params->distribution_maxrgb[i].percentage = get_bits(gb, 7); - params->distribution_maxrgb[i].percentile = - (AVRational){get_bits(gb, 17), rgb_den}; - } - - if (get_bits_left(gb) < 10) - return AVERROR_INVALIDDATA; - - params->fraction_bright_pixels = (AVRational){get_bits(gb, 10), fraction_pixel_den}; - } - if (get_bits_left(gb) < 1) - return AVERROR_INVALIDDATA; - s->mastering_display_actual_peak_luminance_flag = get_bits1(gb); - if (s->mastering_display_actual_peak_luminance_flag) { - int rows, cols; - if (get_bits_left(gb) < 10) - return AVERROR_INVALIDDATA; - rows = get_bits(gb, 5); - cols = get_bits(gb, 5); - if (((rows < 2) || (rows > 25)) || ((cols < 2) || (cols > 25))) { - return AVERROR_INVALIDDATA; - } - s->num_rows_mastering_display_actual_peak_luminance = rows; - s->num_cols_mastering_display_actual_peak_luminance = cols; - - if (get_bits_left(gb) < (rows * cols * 4)) - return AVERROR_INVALIDDATA; - - for (int i = 0; i < rows; i++) { - for (int j = 0; j < cols; j++) { - s->mastering_display_actual_peak_luminance[i][j] = - (AVRational){get_bits(gb, 4), peak_luminance_den}; - } - } - } - - for (int w = 0; w < s->num_windows; w++) { - AVHDRPlusColorTransformParams *params = &s->params[w]; - if (get_bits_left(gb) < 1) - return AVERROR_INVALIDDATA; - - params->tone_mapping_flag = get_bits1(gb); - if (params->tone_mapping_flag) { - if (get_bits_left(gb) < 28) - return AVERROR_INVALIDDATA; - - params->knee_point_x = - (AVRational){get_bits(gb, 12), knee_point_den}; - params->knee_point_y = - (AVRational){get_bits(gb, 12), knee_point_den}; - params->num_bezier_curve_anchors = get_bits(gb, 4); - - if (get_bits_left(gb) < (params->num_bezier_curve_anchors * 10)) - return AVERROR_INVALIDDATA; - - for (int i = 0; i < params->num_bezier_curve_anchors; i++) { - params->bezier_curve_anchors[i] = - (AVRational){get_bits(gb, 10), bezier_anchor_den}; - } - } - - if (get_bits_left(gb) < 1) - return AVERROR_INVALIDDATA; - params->color_saturation_mapping_flag = get_bits1(gb); - if (params->color_saturation_mapping_flag) { - if (get_bits_left(gb) < 6) - return AVERROR_INVALIDDATA; - params->color_saturation_weight = - (AVRational){get_bits(gb, 6), saturation_weight_den}; - } - } - - return 0; -} diff --git a/libavcodec/dynamic_hdr10_plus.h b/libavcodec/dynamic_hdr10_plus.h deleted file mode 100644 index cd7acf0432..0000000000 --- a/libavcodec/dynamic_hdr10_plus.h +++ /dev/null @@ -1,35 +0,0 @@ -/* - * This file is part of FFmpeg. - * - * FFmpeg is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * FFmpeg is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with FFmpeg; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#ifndef AVCODEC_DYNAMIC_HDR10_PLUS_H -#define AVCODEC_DYNAMIC_HDR10_PLUS_H - -#include "libavutil/hdr_dynamic_metadata.h" - -/** - * Parse the user data registered ITU-T T.35 to AVbuffer (AVDynamicHDRPlus). - * @param s A pointer containing the decoded AVDynamicHDRPlus structure. - * @param data The byte array containing the raw ITU-T T.35 data. - * @param size Size of the data array in bytes. - * - * @return 0 if succeed. Otherwise, returns the appropriate AVERROR. - */ -int ff_parse_itu_t_t35_to_dynamic_hdr10_plus(AVDynamicHDRPlus *s, const uint8_t *data, - int size); - -#endif /* AVCODEC_DYNAMIC_HDR10_PLUS_H */ diff --git a/libavcodec/h2645_sei.c b/libavcodec/h2645_sei.c index 6e4a9a1af2..63ab711bc9 100644 --- a/libavcodec/h2645_sei.c +++ b/libavcodec/h2645_sei.c @@ -27,13 +27,13 @@ #include "libavutil/ambient_viewing_environment.h" #include "libavutil/display.h" +#include "libavutil/hdr_dynamic_metadata.h" #include "libavutil/film_grain_params.h" #include "libavutil/pixdesc.h" #include "libavutil/stereo3d.h" #include "atsc_a53.h" #include "avcodec.h" -#include "dynamic_hdr10_plus.h" #include "dynamic_hdr_vivid.h" #include "get_bits.h" #include "golomb.h" @@ -52,8 +52,8 @@ static int decode_registered_user_data_dynamic_hdr_plus(HEVCSEIDynamicHDRPlus *s if (!metadata) return AVERROR(ENOMEM); - err = ff_parse_itu_t_t35_to_dynamic_hdr10_plus(metadata, gb->buffer, - bytestream2_get_bytes_left(gb)); + err = av_dynamic_hdr_plus_from_t35(metadata, gb->buffer, + bytestream2_get_bytes_left(gb)); if (err < 0) { av_free(metadata); return err; diff --git a/libavcodec/libdav1d.c b/libavcodec/libdav1d.c index eb1225ea1a..50c4ceee40 100644 --- a/libavcodec/libdav1d.c +++ b/libavcodec/libdav1d.c @@ -24,6 +24,7 @@ #include "libavutil/avassert.h" #include "libavutil/cpu.h" #include "libavutil/film_grain_params.h" +#include "libavutil/hdr_dynamic_metadata.h" #include "libavutil/mastering_display_metadata.h" #include "libavutil/imgutils.h" #include "libavutil/opt.h" @@ -33,7 +34,6 @@ #include "bytestream.h" #include "codec_internal.h" #include "decode.h" -#include "dynamic_hdr10_plus.h" #include "internal.h" #define FF_DAV1D_VERSION_AT_LEAST(x,y) \ @@ -555,8 +555,8 @@ FF_ENABLE_DEPRECATION_WARNINGS goto fail; } - res = ff_parse_itu_t_t35_to_dynamic_hdr10_plus(hdrplus, gb.buffer, - bytestream2_get_bytes_left(&gb)); + res = av_dynamic_hdr_plus_from_t35(hdrplus, gb.buffer, + bytestream2_get_bytes_left(&gb)); if (res < 0) goto fail; break; diff --git a/libavutil/hdr_dynamic_metadata.c b/libavutil/hdr_dynamic_metadata.c index 0fa1ee82de..223db2fd66 100644 --- a/libavutil/hdr_dynamic_metadata.c +++ b/libavutil/hdr_dynamic_metadata.c @@ -20,6 +20,16 @@ #include "hdr_dynamic_metadata.h" #include "mem.h" +#include "libavcodec/get_bits.h" +#include "libavcodec/put_bits.h" + +static const int64_t luminance_den = 1; +static const int32_t peak_luminance_den = 15; +static const int64_t rgb_den = 100000; +static const int32_t fraction_pixel_den = 1000; +static const int32_t knee_point_den = 4095; +static const int32_t bezier_anchor_den = 1023; +static const int32_t saturation_weight_den = 8; AVDynamicHDRPlus *av_dynamic_hdr_plus_alloc(size_t *size) { @@ -45,3 +55,173 @@ AVDynamicHDRPlus *av_dynamic_hdr_plus_create_side_data(AVFrame *frame) return (AVDynamicHDRPlus *)side_data->data; } + +int av_dynamic_hdr_plus_from_t35(AVDynamicHDRPlus *s, const uint8_t *data, + size_t size) +{ + GetBitContext gbc, *gb = &gbc; + int ret; + + if (!s) + return AVERROR(ENOMEM); + + ret = init_get_bits8(gb, data, size); + if (ret < 0) + return ret; + + if (get_bits_left(gb) < 10) + return AVERROR_INVALIDDATA; + + s->application_version = get_bits(gb, 8); + s->num_windows = get_bits(gb, 2); + + if (s->num_windows < 1 || s->num_windows > 3) { + return AVERROR_INVALIDDATA; + } + + if (get_bits_left(gb) < ((19 * 8 + 1) * (s->num_windows - 1))) + return AVERROR_INVALIDDATA; + + for (int w = 1; w < s->num_windows; w++) { + // The corners are set to absolute coordinates here. They should be + // converted to the relative coordinates (in [0, 1]) in the decoder. + AVHDRPlusColorTransformParams *params = &s->params[w]; + params->window_upper_left_corner_x = + (AVRational){get_bits(gb, 16), 1}; + params->window_upper_left_corner_y = + (AVRational){get_bits(gb, 16), 1}; + params->window_lower_right_corner_x = + (AVRational){get_bits(gb, 16), 1}; + params->window_lower_right_corner_y = + (AVRational){get_bits(gb, 16), 1}; + + params->center_of_ellipse_x = get_bits(gb, 16); + params->center_of_ellipse_y = get_bits(gb, 16); + params->rotation_angle = get_bits(gb, 8); + params->semimajor_axis_internal_ellipse = get_bits(gb, 16); + params->semimajor_axis_external_ellipse = get_bits(gb, 16); + params->semiminor_axis_external_ellipse = get_bits(gb, 16); + params->overlap_process_option = get_bits1(gb); + } + + if (get_bits_left(gb) < 28) + return AVERROR_INVALIDDATA; + + s->targeted_system_display_maximum_luminance = + (AVRational){get_bits_long(gb, 27), luminance_den}; + s->targeted_system_display_actual_peak_luminance_flag = get_bits1(gb); + + if (s->targeted_system_display_actual_peak_luminance_flag) { + int rows, cols; + if (get_bits_left(gb) < 10) + return AVERROR_INVALIDDATA; + rows = get_bits(gb, 5); + cols = get_bits(gb, 5); + if (((rows < 2) || (rows > 25)) || ((cols < 2) || (cols > 25))) { + return AVERROR_INVALIDDATA; + } + s->num_rows_targeted_system_display_actual_peak_luminance = rows; + s->num_cols_targeted_system_display_actual_peak_luminance = cols; + + if (get_bits_left(gb) < (rows * cols * 4)) + return AVERROR_INVALIDDATA; + + for (int i = 0; i < rows; i++) { + for (int j = 0; j < cols; j++) { + s->targeted_system_display_actual_peak_luminance[i][j] = + (AVRational){get_bits(gb, 4), peak_luminance_den}; + } + } + } + for (int w = 0; w < s->num_windows; w++) { + AVHDRPlusColorTransformParams *params = &s->params[w]; + if (get_bits_left(gb) < (3 * 17 + 17 + 4)) + return AVERROR_INVALIDDATA; + + for (int i = 0; i < 3; i++) { + params->maxscl[i] = + (AVRational){get_bits(gb, 17), rgb_den}; + } + params->average_maxrgb = + (AVRational){get_bits(gb, 17), rgb_den}; + params->num_distribution_maxrgb_percentiles = get_bits(gb, 4); + + if (get_bits_left(gb) < + (params->num_distribution_maxrgb_percentiles * 24)) + return AVERROR_INVALIDDATA; + + for (int i = 0; i < params->num_distribution_maxrgb_percentiles; i++) { + params->distribution_maxrgb[i].percentage = get_bits(gb, 7); + params->distribution_maxrgb[i].percentile = + (AVRational){get_bits(gb, 17), rgb_den}; + } + + if (get_bits_left(gb) < 10) + return AVERROR_INVALIDDATA; + + params->fraction_bright_pixels = (AVRational){get_bits(gb, 10), fraction_pixel_den}; + } + if (get_bits_left(gb) < 1) + return AVERROR_INVALIDDATA; + s->mastering_display_actual_peak_luminance_flag = get_bits1(gb); + if (s->mastering_display_actual_peak_luminance_flag) { + int rows, cols; + if (get_bits_left(gb) < 10) + return AVERROR_INVALIDDATA; + rows = get_bits(gb, 5); + cols = get_bits(gb, 5); + if (((rows < 2) || (rows > 25)) || ((cols < 2) || (cols > 25))) { + return AVERROR_INVALIDDATA; + } + s->num_rows_mastering_display_actual_peak_luminance = rows; + s->num_cols_mastering_display_actual_peak_luminance = cols; + + if (get_bits_left(gb) < (rows * cols * 4)) + return AVERROR_INVALIDDATA; + + for (int i = 0; i < rows; i++) { + for (int j = 0; j < cols; j++) { + s->mastering_display_actual_peak_luminance[i][j] = + (AVRational){get_bits(gb, 4), peak_luminance_den}; + } + } + } + + for (int w = 0; w < s->num_windows; w++) { + AVHDRPlusColorTransformParams *params = &s->params[w]; + if (get_bits_left(gb) < 1) + return AVERROR_INVALIDDATA; + + params->tone_mapping_flag = get_bits1(gb); + if (params->tone_mapping_flag) { + if (get_bits_left(gb) < 28) + return AVERROR_INVALIDDATA; + + params->knee_point_x = + (AVRational){get_bits(gb, 12), knee_point_den}; + params->knee_point_y = + (AVRational){get_bits(gb, 12), knee_point_den}; + params->num_bezier_curve_anchors = get_bits(gb, 4); + + if (get_bits_left(gb) < (params->num_bezier_curve_anchors * 10)) + return AVERROR_INVALIDDATA; + + for (int i = 0; i < params->num_bezier_curve_anchors; i++) { + params->bezier_curve_anchors[i] = + (AVRational){get_bits(gb, 10), bezier_anchor_den}; + } + } + + if (get_bits_left(gb) < 1) + return AVERROR_INVALIDDATA; + params->color_saturation_mapping_flag = get_bits1(gb); + if (params->color_saturation_mapping_flag) { + if (get_bits_left(gb) < 6) + return AVERROR_INVALIDDATA; + params->color_saturation_weight = + (AVRational){get_bits(gb, 6), saturation_weight_den}; + } + } + + return 0; +} diff --git a/libavutil/hdr_dynamic_metadata.h b/libavutil/hdr_dynamic_metadata.h index 2d72de56ae..3d327241c1 100644 --- a/libavutil/hdr_dynamic_metadata.h +++ b/libavutil/hdr_dynamic_metadata.h @@ -340,4 +340,15 @@ AVDynamicHDRPlus *av_dynamic_hdr_plus_alloc(size_t *size); */ AVDynamicHDRPlus *av_dynamic_hdr_plus_create_side_data(AVFrame *frame); +/** + * Parse the user data registered ITU-T T.35 to AVbuffer (AVDynamicHDRPlus). + * @param s A pointer containing the decoded AVDynamicHDRPlus structure. + * @param data The byte array containing the raw ITU-T T.35 data. + * @param size Size of the data array in bytes. + * + * @return 0 if succeed. Otherwise, returns the appropriate AVERROR. + */ +int av_dynamic_hdr_plus_from_t35(AVDynamicHDRPlus *s, const uint8_t *data, + size_t size); + #endif /* AVUTIL_HDR_DYNAMIC_METADATA_H */ From patchwork Mon Mar 13 23:37:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Rapha=C3=ABl_Zumer?= X-Patchwork-Id: 40671 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:d046:b0:cd:afd7:272c with SMTP id hv6csp2153273pzb; Mon, 13 Mar 2023 16:37:24 -0700 (PDT) X-Google-Smtp-Source: AK7set96Z51oXWWQEmZCZgfSTlWFxIDWKOreKipG/T+JRL5u+ba3u4vgXMQ0fZsYXGxMFi5u1N1L X-Received: by 2002:a17:906:8588:b0:926:4e16:a551 with SMTP id v8-20020a170906858800b009264e16a551mr244317ejx.30.1678750643993; Mon, 13 Mar 2023 16:37:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1678750643; cv=none; d=google.com; s=arc-20160816; b=raGm4/lXpxqxixyfnRBSPAuLcMISppUO5b5dgITKcKza7ppb/JgzUVolqm9AcsY7M+ i+yd6fVC3xVqy4QMQqhfgHZ7ZDYPtqNSj48P7IF9m3xbgdD/dVKrxTRaAepkMTGFd/RS cyX2PNPnyJ9PnG5HQ3QcK+NxUApAYnp5yE8KpRUP8sVkS0pfdPjrzLqATrvF+1wyqq22 LGlIQBcoG/2GM4NS5/uMo5XaeK19n7iOY6IMwmtj1/ukukrLtgVikuSIbXdLAbZ8FXQR KKViEe02AF5XPmQtZgtbEgz11uZrpIIiCZyMlvk4l2ZBNvCzLf3poVPKRFXeLu851CR6 vq0g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:content-language:to:from:user-agent:mime-version :date:message-id:dkim-signature:delivered-to; bh=33NNDTCfKCbgeil04lJp2o8/trt866H+2YMlarHeGow=; b=HctFmT3mM5XgreBoFKKITZveHohcIoiEJIRdl7FYsFK8izy/lnKv024XltEDtBJxky mzVn0PXFsvG70hIeC96/6WRX1++sEN9cv6b1LDnfSPuGYiGZisaVQ+9m4JW1U+XKBUwL LQlXFMHr+Dd8gKz5OBQDW5SnOzVZYbgQJ1HnShtCWgXRuOS/Q8XhDyKhCuRFMuKnkOjG 9CvmasrLQ2+t7m6gAzjjk+ViCIpdYhSVYxFX4yiCv3WzQJoGBXeWKXGNtmkVt9q7+o3K vtz1IZ9pMX66+xO5qVDCxERtKNLPoGDwPNfcmDnLDBn6cvLR8TPmGS8XrBdMpFrIMp4r YV+g== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@vimeo.com header.s=google header.b=F37X0Fmk; 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; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=vimeo.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id u17-20020a170906c41100b00922d1fdfadesi915696ejz.429.2023.03.13.16.37.23; Mon, 13 Mar 2023 16:37:23 -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=@vimeo.com header.s=google header.b=F37X0Fmk; 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; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=vimeo.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 942EC68BD6C; Tue, 14 Mar 2023 01:37:21 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-qt1-f181.google.com (mail-qt1-f181.google.com [209.85.160.181]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id C7C9968AFCE for ; Tue, 14 Mar 2023 01:37:15 +0200 (EET) Received: by mail-qt1-f181.google.com with SMTP id y10so15116549qtj.2 for ; Mon, 13 Mar 2023 16:37:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vimeo.com; s=google; t=1678750634; h=content-transfer-encoding:content-language:to:subject:from :user-agent:mime-version:date:message-id:from:to:cc:subject:date :message-id:reply-to; bh=ymfQf6Gk/GHOPMbCG3+2/6swfTW3NU6YXtFmn4p3aR0=; b=F37X0Fmk69Ejt08tI3uWiqvWQIANzePtAYzMWX/F+7Fi+6JeaTPnch21m6wZRIb4VB tVB35vMrV+t9vH5rfm5mXe5ZzCzT2FsG+X98BCr69XFjsA6Pn4pBLLSROMF0QvUP3Qiv 1lC4C7IXZfE14KxQtZ2RWDXpLS1n7+RSgdTzg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678750634; h=content-transfer-encoding:content-language:to:subject:from :user-agent:mime-version:date:message-id:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=ymfQf6Gk/GHOPMbCG3+2/6swfTW3NU6YXtFmn4p3aR0=; b=hFmYrAbd3BCgOc3zORydhgQxcLCg5wMxiMY+T1Hu4W76/6pXHCz4fOq9ZQmvcSiUqs /s6wOZxqwSohZfy1t36N3DyyPdNj8CElHgWIgx6tm/Da5xDrD6JiPwG+QwCGuCoKeGb/ R1+YfSEJlyM/zfzFK8/G8rnQcgVzdGpE3itYRYIJ2VCy4zGS0k66dhpcSL2SOnLMMQdu Oi/2bSXfCRA4U3eJ0NrcfS1CP+if2mW6csVYDGc1/Rn7pkNmsNqAPOkXeRWwcVrmV3V+ qmIs0pFPpWsS626RHNDR/BSSjzfgO9189FIkjMI3ojQ15Jlg1UkxcSSXR0CFNc4eDNRE griw== X-Gm-Message-State: AO0yUKX6FXDIfIrTlUrN2BJdfsmWrYd/tn6gDwA3X7ZTOB/1T95YSUXY jxOqwY+4d+SoX4MKIKA1F+TDskXL+qEgBNuWE+z7lA== X-Received: by 2002:ac8:5dd0:0:b0:3bf:b6ba:1c1f with SMTP id e16-20020ac85dd0000000b003bfb6ba1c1fmr63078032qtx.10.1678750634244; Mon, 13 Mar 2023 16:37:14 -0700 (PDT) Received: from ?IPV6:2600:4041:6c:7000:1f01:20c:8ba9:815b? ([2600:4041:6c:7000:1f01:20c:8ba9:815b]) by smtp.gmail.com with ESMTPSA id do51-20020a05620a2b3300b007435a646354sm690057qkb.0.2023.03.13.16.37.12 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 13 Mar 2023 16:37:13 -0700 (PDT) Message-ID: <346a1d99-111e-1fd8-c52c-37145c9b9214@vimeo.com> Date: Mon, 13 Mar 2023 19:37:12 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.2.2 From: =?utf-8?q?Rapha=C3=ABl_Zumer?= To: ffmpeg-devel@ffmpeg.org Content-Language: en-US, fr-FR Subject: [FFmpeg-devel] [PATCH v6 2/2] avutil: add HDR10+ dynamic metadata serialization function 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: 8eVbYDTtUAR0 Co-authored-by: Mohammad Izadi Signed-off-by: Raphaël Zumer --- doc/APIchanges | 5 ++ libavutil/hdr_dynamic_metadata.c | 148 +++++++++++++++++++++++++++++++ libavutil/hdr_dynamic_metadata.h | 13 +++ libavutil/version.h | 2 +- 4 files changed, 167 insertions(+), 1 deletion(-) diff --git a/doc/APIchanges b/doc/APIchanges index 14737223cb..3a61d61931 100644 --- a/doc/APIchanges +++ b/doc/APIchanges @@ -2,6 +2,11 @@ The last version increases of all libraries were on 2023-02-09 API changes, most recent first: +2023-03-13 - xxxxxxxxxx - lavu 58.4.100 - hdr_dynamic_metadata.h + Add av_dynamic_hdr_plus_from_t35() and av_dynamic_hdr_plus_to_t35() + functions to convert between raw T.35 payloads containing dynamic + HDR10+ metadata and their parsed representations as AVDynamicHDRPlus. + 2023-03-02 - xxxxxxxxxx - lavc 60.6.100 - avcodec.h Add FF_PROFILE_EAC3_DDP_ATMOS, FF_PROFILE_TRUEHD_ATMOS, FF_PROFILE_DTS_HD_MA_X and FF_PROFILE_DTS_HD_MA_X_IMAX. diff --git a/libavutil/hdr_dynamic_metadata.c b/libavutil/hdr_dynamic_metadata.c index 223db2fd66..da3c5f4947 100644 --- a/libavutil/hdr_dynamic_metadata.c +++ b/libavutil/hdr_dynamic_metadata.c @@ -20,6 +20,7 @@ #include "hdr_dynamic_metadata.h" #include "mem.h" +#include "libavcodec/defs.h" #include "libavcodec/get_bits.h" #include "libavcodec/put_bits.h" @@ -225,3 +226,150 @@ int av_dynamic_hdr_plus_from_t35(AVDynamicHDRPlus *s, const uint8_t *data, return 0; } + +int av_dynamic_hdr_plus_to_t35(const AVDynamicHDRPlus *s, uint8_t **data, size_t *size) +{ + uint8_t *buf; + size_t size_bits, size_bytes; + PutBitContext pbc, *pb = &pbc; + + if (!s || !data) + return AVERROR(EINVAL); + + /** + * Buffer size per CTA-861-H p.253-254: + * 48 bits for the header (56 minus excluded 8-bit country code) + * 2 bits for num_windows + * 937 bits for window geometry, for each window above 1 + * 27 bits for targeted_system_display_maximum_luminance + * 1-3855 bits for targeted system display peak luminance information + * 0-442 bits for intra-window pixel distribution information + * 1-3855 bits for mastering display peak luminance information + * 0-537 bits for per-window tonemapping information + * 0-21 bits for per-window color saturation mapping information + */ + size_bits = 48 + + 2 + + FFMAX((s->num_windows - 1), 0) * 937 + + 27 + + 1 + (s->targeted_system_display_actual_peak_luminance_flag ? 10 + + s->num_rows_targeted_system_display_actual_peak_luminance * + s->num_cols_targeted_system_display_actual_peak_luminance * 4 : 0) + + s->num_windows * 82; + + for (int w = 0; w < s->num_windows; w++) + size_bits += s->params[w].num_distribution_maxrgb_percentiles * 24; + + size_bits += 1 + (s->mastering_display_actual_peak_luminance_flag ? 10 + + s->num_rows_mastering_display_actual_peak_luminance * + s->num_cols_mastering_display_actual_peak_luminance * 4 : 0) + + s->num_windows * 1; + + for (int w = 0; w < s->num_windows; w++) { + if (s->params[w].tone_mapping_flag) + size_bits += 28 + s->params[w].num_bezier_curve_anchors * 10; + } + + size_bits += s->num_windows * 1; + for (int w = 0; w < s->num_windows; w++) { + if (s->params[w].color_saturation_mapping_flag) + size_bits += 6; + } + + size_bytes = (size_bits + 7) / 8; + + // Pad the allocated buffer to account for optimized readers that may use it directly. + buf = av_malloc(size_bytes + AV_INPUT_BUFFER_PADDING_SIZE); + if (!buf) + return AVERROR(ENOMEM); + + init_put_bits(pb, buf, size_bytes); + + // itu_t_t35_country_code shall be 0xB5 (USA) (excluded from the payload) + // itu_t_t35_terminal_provider_code shall be 0x003C + put_bits(pb, 16, 0x003C); + // itu_t_t35_terminal_provider_oriented_code is set to ST 2094-40 + put_bits(pb, 16, 0x0001); + // application_identifier shall be set to 4 + put_bits(pb, 8, 4); + // application_mode is set to Application Version 1 + put_bits(pb, 8, 1); + + // Payload as per CTA-861-H p.253-254 + put_bits(pb, 2, s->num_windows); + + for (int w = 1; w < s->num_windows; w++) { + put_bits(pb, 16, s->params[w].window_upper_left_corner_x.num / s->params[w].window_upper_left_corner_x.den); + put_bits(pb, 16, s->params[w].window_upper_left_corner_y.num / s->params[w].window_upper_left_corner_y.den); + put_bits(pb, 16, s->params[w].window_lower_right_corner_x.num / s->params[w].window_lower_right_corner_x.den); + put_bits(pb, 16, s->params[w].window_lower_right_corner_y.num / s->params[w].window_lower_right_corner_y.den); + put_bits(pb, 16, s->params[w].center_of_ellipse_x); + put_bits(pb, 16, s->params[w].center_of_ellipse_y); + put_bits(pb, 8, s->params[w].rotation_angle); + put_bits(pb, 16, s->params[w].semimajor_axis_internal_ellipse); + put_bits(pb, 16, s->params[w].semimajor_axis_external_ellipse); + put_bits(pb, 16, s->params[w].semiminor_axis_external_ellipse); + put_bits(pb, 1, s->params[w].overlap_process_option); + } + + put_bits(pb, 27, s->targeted_system_display_maximum_luminance.num * luminance_den / + s->targeted_system_display_maximum_luminance.den); + put_bits(pb, 1, s->targeted_system_display_actual_peak_luminance_flag); + if (s->targeted_system_display_actual_peak_luminance_flag) { + put_bits(pb, 5, s->num_rows_targeted_system_display_actual_peak_luminance); + put_bits(pb, 5, s->num_cols_targeted_system_display_actual_peak_luminance); + for (int i = 0; i < s->num_rows_targeted_system_display_actual_peak_luminance; i++) { + for (int j = 0; j < s->num_cols_targeted_system_display_actual_peak_luminance; j++) + put_bits(pb, 4, s->targeted_system_display_actual_peak_luminance[i][j].num * peak_luminance_den / + s->targeted_system_display_actual_peak_luminance[i][j].den); + } + } + + for (int w = 0; w < s->num_windows; w++) { + for (int i = 0; i < 3; i++) + put_bits(pb, 17, s->params[w].maxscl[i].num * rgb_den / s->params[w].maxscl[i].den); + put_bits(pb, 17, s->params[w].average_maxrgb.num * rgb_den / s->params[w].average_maxrgb.den); + put_bits(pb, 4, s->params[w].num_distribution_maxrgb_percentiles); + for (int i = 0; i < s->params[w].num_distribution_maxrgb_percentiles; i++) { + put_bits(pb, 7, s->params[w].distribution_maxrgb[i].percentage); + put_bits(pb, 17, s->params[w].distribution_maxrgb[i].percentile.num * rgb_den / + s->params[w].distribution_maxrgb[i].percentile.den); + } + put_bits(pb, 10, s->params[w].fraction_bright_pixels.num * fraction_pixel_den / + s->params[w].fraction_bright_pixels.den); + } + + put_bits(pb, 1, s->mastering_display_actual_peak_luminance_flag); + if (s->mastering_display_actual_peak_luminance_flag) { + put_bits(pb, 5, s->num_rows_mastering_display_actual_peak_luminance); + put_bits(pb, 5, s->num_cols_mastering_display_actual_peak_luminance); + for (int i = 0; i < s->num_rows_mastering_display_actual_peak_luminance; i++) { + for (int j = 0; j < s->num_cols_mastering_display_actual_peak_luminance; j++) + put_bits(pb, 4, s->mastering_display_actual_peak_luminance[i][j].num * peak_luminance_den / + s->mastering_display_actual_peak_luminance[i][j].den); + } + } + + for (int w = 0; w < s->num_windows; w++) { + put_bits(pb, 1, s->params[w].tone_mapping_flag); + if (s->params[w].tone_mapping_flag) { + put_bits(pb, 12, s->params[w].knee_point_x.num * knee_point_den / s->params[w].knee_point_x.den); + put_bits(pb, 12, s->params[w].knee_point_y.num * knee_point_den / s->params[w].knee_point_y.den); + put_bits(pb, 4, s->params[w].num_bezier_curve_anchors); + for (int i = 0; i < s->params[w].num_bezier_curve_anchors; i++) + put_bits(pb, 10, s->params[w].bezier_curve_anchors[i].num * bezier_anchor_den / + s->params[w].bezier_curve_anchors[i].den); + put_bits(pb, 1, s->params[w].color_saturation_mapping_flag); + if (s->params[w].color_saturation_mapping_flag) + put_bits(pb, 6, s->params[w].color_saturation_weight.num * saturation_weight_den / + s->params[w].color_saturation_weight.den); + } + } + + flush_put_bits(pb); + + *data = buf; + if (size) + *size = size_bytes; + return 0; +} diff --git a/libavutil/hdr_dynamic_metadata.h b/libavutil/hdr_dynamic_metadata.h index 3d327241c1..52b4660f7d 100644 --- a/libavutil/hdr_dynamic_metadata.h +++ b/libavutil/hdr_dynamic_metadata.h @@ -21,6 +21,7 @@ #ifndef AVUTIL_HDR_DYNAMIC_METADATA_H #define AVUTIL_HDR_DYNAMIC_METADATA_H +#include "buffer.h" #include "frame.h" #include "rational.h" @@ -351,4 +352,16 @@ AVDynamicHDRPlus *av_dynamic_hdr_plus_create_side_data(AVFrame *frame); int av_dynamic_hdr_plus_from_t35(AVDynamicHDRPlus *s, const uint8_t *data, size_t size); +/** + * Serialize dynamic HDR10+ metadata to a user data registered ITU-T T.35 buffer, + * excluding the country code and beginning with the terminal provider code. + * @param s A pointer containing the decoded AVDynamicHDRPlus structure. + * @param data A pointer to a byte buffer to be allocated and filled + * with the serialized metadata. + * @param size A pointer to a size to be set to the returned buffer's size (optional). + * + * @return 0 if succeed. Otherwise, returns the appropriate AVERROR. + */ +int av_dynamic_hdr_plus_to_t35(const AVDynamicHDRPlus *s, uint8_t **data, size_t *size); + #endif /* AVUTIL_HDR_DYNAMIC_METADATA_H */ diff --git a/libavutil/version.h b/libavutil/version.h index 900b798971..7635672985 100644 --- a/libavutil/version.h +++ b/libavutil/version.h @@ -79,7 +79,7 @@ */ #define LIBAVUTIL_VERSION_MAJOR 58 -#define LIBAVUTIL_VERSION_MINOR 3 +#define LIBAVUTIL_VERSION_MINOR 4 #define LIBAVUTIL_VERSION_MICRO 100 #define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \