From patchwork Thu Mar 2 19:25:22 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: 40568 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:d046:b0:cd:afd7:272c with SMTP id hv6csp85301pzb; Thu, 2 Mar 2023 11:25:34 -0800 (PST) X-Google-Smtp-Source: AK7set+2gQd2KDfi7WH5JfxsqRinoMWPgj6ZGXBQRLDUGewbabXsVY9qAipCMKbmX4uVmjuuqs9j X-Received: by 2002:a17:907:c20b:b0:906:2b5c:7390 with SMTP id ti11-20020a170907c20b00b009062b5c7390mr8043024ejc.16.1677785134589; Thu, 02 Mar 2023 11:25:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677785134; cv=none; d=google.com; s=arc-20160816; b=oSdjzllMRRON8VZNmrvyqX+XKjGfuOQbBSvJKh+jrTe6uBr4y5qXInpsKrYmk0uqKI PunkYvcmDpq/D829K/jUKI6uGOss9/EqyteyNbtBAFHqObvK6WVbnIJbKZvd9MBj6+O1 vBqcs58cbqKJOL90KoE3HNd/CPCZwo+7euiFFMXuZCWJI6Hu/M1i/rcbM+dx5l0OkzFn KjvPpQz7Fay9dUMh2F/AjjiLbpq3TYAbgod+xq26VpR+fRSWbCWQHyhHkTvdL7GN9b+u UNdh1AbLtIvie9+bXCF8HrBeLvBjCJUzveSEsNkcthpNb5MQoQAxYdoVpTWTtGPfcxwf TVww== 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=/k0SdzsnKhUZVWZ9Lz15dP0PQTTiwGAEZm7Ve8QZZZE=; b=yADGvP/f4JOmSEKNl8woadC6Ato7bEU2GEToSx+t2qFaHaNwmwqIOFFaGNxTtSLjXQ lPdtCJ0MCh2VzMAkNM537AD9MT+zrYZAwB3SIXnjP0pPgl2B9+BMc2O9Zw/dNlyGbOj4 cRROOZyxbcjLOF8hEdvr/FzwqmwOEwKxFDS+srId4lia3g46zxwtFEw6DbBWNvxEIYxo uGFNYPnQsapF+VpY9BCzFmEopHB1jHGWNOYhRLGOnr5xttVwy+VyGVsOP5IO0KMle8Wg ydQ+7pp7bkjWuL7GeWZpoJrbH8lcDd0WQ6AfuxK9t6YGy8zBNavpBv5saVb9RA3TpQzv 6fbg== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@vimeo.com header.s=google header.b=XM5KaKvH; 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 ga41-20020a1709070c2900b00909522c0a43si75277ejc.733.2023.03.02.11.25.34; Thu, 02 Mar 2023 11:25:34 -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=XM5KaKvH; 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 1ECBC68B41A; Thu, 2 Mar 2023 21:25:31 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-qt1-f175.google.com (mail-qt1-f175.google.com [209.85.160.175]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 1D103689C24 for ; Thu, 2 Mar 2023 21:25:25 +0200 (EET) Received: by mail-qt1-f175.google.com with SMTP id w23so431207qtn.6 for ; Thu, 02 Mar 2023 11:25:25 -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=ee1xRVRS2GWr537Lif0w5ChLVEXtCgCr1kbxWw8LtKw=; b=XM5KaKvHFO9H9XhJtV8j+21j9GH194OsP7dTJRSFZsBuo83V/iWHbXAFvA93R7UoUx BJy7KsgdvV3Z7YYD2ZjjBrW3nlzp+pCW45GlpFeodUviyZsQQ6VpLto+4MFlWpjj0gG/ 7ExGZeT6bECOj6VDXA5bBRORdn/MYIF+x/pIQ= 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=ee1xRVRS2GWr537Lif0w5ChLVEXtCgCr1kbxWw8LtKw=; b=aN1mDRuh778Wkj7YPb38Aj//vpzhRVk1DU2+mSDKt4DFS+kn6fsRoLSG373XAscV9D wXrdhqaCINB509pNbcKdAEih35Q8bKPx6yULnK8TKsBcs0JkHg7dFO5rbCUTdy5ivD6t tjdF4/RacI1RbBdXxjOSTB0Gtuc56X4cR7Nybsc9rSBSH7kE6wG0hpsbINsMk8OJdiEU 1yZ8xFjIvpLyFpXqotS7rvCTiT4huNNXEqTVAhqxhvVCJDj6i0OUTO5uvfh/IN8+577F 8U/aJSSMmhP493GTioCNXykTZPQ6VY1NjDFZ0lppKn/v94igLiySKLnl1f+6IIWvyV8k BH6g== X-Gm-Message-State: AO0yUKXuhdt2Uu3R66ZGx/795MgFczlw3WNd8nCRLweiW0Lkw+fCF066 6f5DXgTnPsyZ7MboYvXWESmIHcgorkz/N42a+q0= X-Received: by 2002:ac8:7f4e:0:b0:3bf:d71e:5af4 with SMTP id g14-20020ac87f4e000000b003bfd71e5af4mr19803301qtk.26.1677785123324; Thu, 02 Mar 2023 11:25:23 -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 d11-20020a05620a158b00b0073b8745fd39sm204280qkk.110.2023.03.02.11.25.22 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 02 Mar 2023 11:25:23 -0800 (PST) Message-ID: Date: Thu, 2 Mar 2023 14:25:22 -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: sZhymQRrKBZq Signed-off-by: Raphaƫl Zumer --- libavutil/hdr_dynamic_metadata.c | 146 +++++++++++++++++++++++++++++++ libavutil/hdr_dynamic_metadata.h | 11 +++ libavutil/version.h | 2 +- 3 files changed, 158 insertions(+), 1 deletion(-) diff --git a/libavutil/hdr_dynamic_metadata.c b/libavutil/hdr_dynamic_metadata.c index 98f399b032..39a7886a2e 100644 --- a/libavutil/hdr_dynamic_metadata.c +++ b/libavutil/hdr_dynamic_metadata.c @@ -225,3 +225,149 @@ 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: + size_bits = + // 56 bits for the header, minus 8-bit excluded country code + 48 + + // 2 bits for num_windows + 2 + + // 937 bits for window geometry for each window above 1 + FFMAX((s->num_windows - 1), 0) * 937 + + // 27 bits for targeted_system_display_maximum_luminance + 27 + + // 1-3855 bits for targeted system display peak luminance information + 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) + + // 0-442 bits for intra-window pixel distribution information + s->num_windows * 82; + for (int w = 0; w < s->num_windows; w++) { + size_bits += s->params[w].num_distribution_maxrgb_percentiles * 24; + } + // 1-3855 bits for mastering display peak luminance information + 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) + + // 0-537 bits for per-window tonemapping information + 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; + } + } + // 0-21 bits for per-window color saturation mapping information + 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, \