From patchwork Thu Mar 2 21:43:29 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: 40569 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:d046:b0:cd:afd7:272c with SMTP id hv6csp179128pzb; Thu, 2 Mar 2023 13:43:43 -0800 (PST) X-Google-Smtp-Source: AK7set+8oATMtLIdZfe/+tExtYjBjb0KJZWPS6E39dcsxBtlETQboc/t1y78XGSDWV0ZnjP/3FeO X-Received: by 2002:aa7:d98a:0:b0:4bf:5981:e5ca with SMTP id u10-20020aa7d98a000000b004bf5981e5camr3639618eds.7.1677793423267; Thu, 02 Mar 2023 13:43:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677793423; cv=none; d=google.com; s=arc-20160816; b=OPpdItrZMHQ1XpLiiBeOf42BHoC9wOxYI/jq5MUQBL6RNmqEwUf8MjiEoYei+IE6wD onpECauoqfkMiIEG8O8O8p7/2rccEKCg2pFH7e0MNhI9eto0ucml/P9U9ld1cyOSkGnM on5RlsVgnrludUH6mGnaCIHb526tgm538GUtmBmJ4M7ePlEGb2rfZW1DAuG9KgChXROX pqjBG83HOIuO4McGSaonjEm7QuGi689HakN/ct+bf05rOCXYXF/6IH3pRDI7WWykokLM ZihNO6OjhjcYCxzR9NcFHXVsGVPeiEz2OFcqLaOV6o4DMeG3CcGq2GfEIO+jvk41Vc9n ibpQ== 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=jHF3bYi0nTMdZupMouBkXIa9oeM5cDM5JxJusF5QGQ8=; b=nI2+omTqZvllQmDN5cjEqs3BDVWlXt6zXY0Ocm4ZELIVP/P19d7lsJgxU42yDjw/Al O+q7YeTLghOnLnwQhV1pfpQ5WDfp3+1k2x6Y3ZUBLJ28J1jwwAoMoy0CUkWNSjq0KdgN mtRYelcJku3395mOnaAKXOB9w4+7KlpKoJirbHRloKDtVEpmQQoGgO632VNVKmeIUCk7 oeKUFWa5G1OZoS9E8dM+kAaUIPtyWrxH197aZMaScr/Ok4U7bofJUpVbigQXSzodlVg3 CamHRh/kLJAcG+hezPHO3FfmpI3b1i1hNIuM6XGNmyoRxhFVc0F7inrbkaIQUS6e6ZgS iAHQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@vimeo.com header.s=google header.b=UInpVpx1; 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 y11-20020aa7d50b000000b004acba1d42f1si705078edq.507.2023.03.02.13.43.42; Thu, 02 Mar 2023 13:43:43 -0800 (PST) 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=UInpVpx1; 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 0EFF168B48D; Thu, 2 Mar 2023 23:43:39 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-qv1-f43.google.com (mail-qv1-f43.google.com [209.85.219.43]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 315C168AD5C for ; Thu, 2 Mar 2023 23:43:33 +0200 (EET) Received: by mail-qv1-f43.google.com with SMTP id ev13so496369qvb.10 for ; Thu, 02 Mar 2023 13:43:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vimeo.com; s=google; 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=MimT6bbJTWDFV8UqpcrzadR/ubsfzYq3/PCpBOdqN4E=; b=UInpVpx1Gm9dPmT9bp8cwUpQgS3YtWaIxwlD1hSpsGAb50YDlinVWlaIIhtoe6MGY5 w8v7grgbBTdyPAyFMGl2cBAmyBYJg/BZoYOajiR1S0eAMjGje0svkDkZNcPvv0faYG/f ybt5l/cFPyBvieIJG/waV9vnJcvK6xe9ouVoQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=MimT6bbJTWDFV8UqpcrzadR/ubsfzYq3/PCpBOdqN4E=; b=tSKwMJdivoN8XJi90zUQD3Oxrcz2XfpKLeZY/UdPeuATGrZtaiSUwJMPPwTNr6RLzM INjNnTUbA4IK5ZC7OzX5AvMiye53sYMNchDmPEZmv7PC/E8ZMeLXXTGopdfS2ntqVhEV eYZyB2Bi3Hq4N5TAKXmar658PvCA2+WUxM5gxQFpDMlmJzp6SHGFkqaOoo4o8HyaiSvL vViLXl8SrfWE+N3UcEQD8C/5Us/6R06bAFHr9eLimlBNn5O/X+U+AOCxNLwhcSjrB/i0 PcRtzqgH9LBTX0O7DuRHDAQT14wrjM79KRX5TVwtDgBX9iOcAft+eR+NoX6tudrSCIR5 XPWg== X-Gm-Message-State: AO0yUKWI0brnkDRvf2+t5a7VUTuZXjd9+5bEak5+XhpHKC71AY2qd0Ky n0kNMHdS0Aca6C6zrylQecruumRPBMULcCq++aE= X-Received: by 2002:a05:6214:21a5:b0:56e:9f05:6278 with SMTP id t5-20020a05621421a500b0056e9f056278mr20292023qvc.33.1677793410767; Thu, 02 Mar 2023 13:43:30 -0800 (PST) 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 136-20020a37088e000000b00742a252ba06sm399271qki.135.2023.03.02.13.43.30 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 02 Mar 2023 13:43:30 -0800 (PST) Message-ID: <3bd88490-9f9f-39e0-756e-9d15d2e78d88@vimeo.com> Date: Thu, 2 Mar 2023 16:43:29 -0500 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 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: vijWsvmI6ZaK Fixed brace style and moved inline buffer size calculation comments to a single block at the top. Signed-off-by: Raphaƫl Zumer --- libavutil/hdr_dynamic_metadata.c | 142 +++++++++++++++++++++++++++++++ libavutil/hdr_dynamic_metadata.h | 11 +++ libavutil/version.h | 2 +- 3 files changed, 154 insertions(+), 1 deletion(-) diff --git a/libavutil/hdr_dynamic_metadata.c b/libavutil/hdr_dynamic_metadata.c index 98f399b032..24dd3dab2d 100644 --- a/libavutil/hdr_dynamic_metadata.c +++ b/libavutil/hdr_dynamic_metadata.c @@ -225,3 +225,145 @@ int av_dynamic_hdr_plus_from_t35(AVDynamicHDRPlus *s, const uint8_t *data, return 0; } + +AVBufferRef *av_dynamic_hdr_plus_to_t35(AVDynamicHDRPlus *s) +{ + AVBufferRef *buf; + size_t size_bits, size_bytes; + PutBitContext pbc, *pb = &pbc; + + if (!s) + return NULL; + + /** + * 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; + + buf = av_buffer_alloc(size_bytes); + if (!buf) + return NULL; + + init_put_bits(pb, buf->data, 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); + return buf; +} diff --git a/libavutil/hdr_dynamic_metadata.h b/libavutil/hdr_dynamic_metadata.h index 1f953ef1f5..797a5c64ae 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,14 @@ AVDynamicHDRPlus *av_dynamic_hdr_plus_create_side_data(AVFrame *frame); int av_dynamic_hdr_plus_from_t35(AVDynamicHDRPlus *s, const uint8_t *data, int 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. + * + * @return Pointer to an AVBufferRef containing the raw ITU-T T.35 representation + * of the HDR10+ metadata if succeed, or NULL if buffer allocation fails. + */ +AVBufferRef *av_dynamic_hdr_plus_to_t35(AVDynamicHDRPlus *s); + #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, \