From patchwork Sun Jul 28 10:25:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Niklas Haas X-Patchwork-Id: 50800 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:612c:16f:b0:489:2eb3:e4c4 with SMTP id h47csp620886vqi; Sun, 28 Jul 2024 05:21:58 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCUCm+RejfH2cq+FA9aGaeunWvXDM0FtjM2nRI9/bWmG/ClVYNvJo/YMDuLfybOCu+g8nuga9u5wbvnbjFYRlCoHQRxIr+Wj9y2/TQ== X-Google-Smtp-Source: AGHT+IEn6JWfW6ze6O8KjfWd/HwlARcvYTNxt5PzVOWyq4F9NTz9jaAiNBy1ZbWentpCdMHdd+MO X-Received: by 2002:a05:6402:3508:b0:5a1:83c4:c5a8 with SMTP id 4fb4d7f45d1cf-5b020ba89fbmr3755924a12.14.1722169317904; Sun, 28 Jul 2024 05:21:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1722169317; cv=none; d=google.com; s=arc-20160816; b=qyh62xKgxP0sbV0R0VZbJjWoOFIirNIfdhQ90IiDU3mxNdpDhXiemKkTSixuTN4+tq AtbO1CghO9KiFb6YNI3tHMXZZkLxzl1Is+6TKEowV/UuDd875yGOrYyDKD90Rr2ifxOW q4z4VWNHuesV741uqvs6GA7a84Ywyz8tDFatyqJ96IAHaf0hhAO0hwbD4zLWlspvLvox HhLrN1BJMK2fI2d7zZaI4t4zDRsiNSCBUiAJgQKWC9AO5wikrITa2AUlsebKdNPM+E9M 4Vb0uXDIlet/QJzUsDIcSTdS8lc8gax49y0ItNHVx2HMVo32TbEsDMAKbf2mNXkGKaVv OIdQ== 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=eaOkqdi/FICeZahMjRGer3nj5BT2su3UxhOGI7F9m2w=; fh=xmAeKtysnShNOmkhiJmYkS30uw4Fu2hvBJ7qlIwukxQ=; b=YACXgq9BMyVOr3wsUs67AMg4bNk6SqCMgqveXJtSZoVrKTGS8LS4znCsLOGznFqfJV 03QikcJrqpDZ1yI/ST+lrTaBzIvSI0J9Ghydo/wBsjtIOrLzCMb/bnqdMNz64yKSQxIq 8elNWnDQgkzTOicxSWpLeZtcwFGEezYsxpvkMkRUMW6ALD3ReW8HHz4g7RWgD/7RUiQr N2OTVP3EEL2/z3YeTA0YrXpMQb2fUY2s9yTEOH2NIzWZLKruLq+9yOE2LIvGL+oyCM+f fWRf1tNc1oCrJ0bDYEntkmrJLQtADv1dET1IS3qMCfKjDDARJYwjOJYNqOV8EI8DSc4u CP3g==; 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=ByVgLYYz; 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 4fb4d7f45d1cf-5ac6339cf2esi5071321a12.108.2024.07.28.05.21.57; Sun, 28 Jul 2024 05:21:57 -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=ByVgLYYz; 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 C6A4D68D960; Sun, 28 Jul 2024 13:25:57 +0300 (EEST) 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 ED50668D773 for ; Sun, 28 Jul 2024 13:25:41 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=haasn.xyz; s=mail; t=1722162339; bh=GAdCJtpaMGEl3fP8giInbkPjWj5xhNvQJzNmKAlyxxI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ByVgLYYzz9D39ZnmGVDKWfl79nz9lqAnHUfO9zXNzbvds/xtYo+6h0hZjFta1/PCa WB2Eb5BX8WqFzOf03yd3pCDeOUReBlZ4W07jeick7MyIzgwKSo13HNMmqgHHiifK/X MewJzt7uSN7itzPDUWQ1fdFbewPE6hY+VXM/NxNE= Received: from haasn.dev (unknown [10.30.0.2]) by haasn.dev (Postfix) with ESMTP id 244C64366E; Sun, 28 Jul 2024 12:25:39 +0200 (CEST) From: Niklas Haas To: ffmpeg-devel@ffmpeg.org Date: Sun, 28 Jul 2024 12:25:23 +0200 Message-ID: <20240728102527.17991-18-ffmpeg@haasn.xyz> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240728102527.17991-1-ffmpeg@haasn.xyz> References: <20240728102527.17991-1-ffmpeg@haasn.xyz> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 18/22] avcodec/dovi_rpuenc: implement DM metadata compression 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: isGuuSu3sQYx From: Niklas Haas This implements limited metadata compression. To be a bit more lenient, we try and re-order the static extension blocks when testing for an exact match. For sanity, and to avoid producing bitstreams we couldn't ourselves decode, we don't accept partial matches - if some extension blocks change while others remain static, compression is disabled for the entire frame. This shouldn't be an issue in practice because static extension blocks are stated to remain constant throughout the entire sequence. --- libavcodec/dovi_rpuenc.c | 166 +++++++++++++++++++++++++++++---------- 1 file changed, 124 insertions(+), 42 deletions(-) diff --git a/libavcodec/dovi_rpuenc.c b/libavcodec/dovi_rpuenc.c index 7193e2d9c8..c73805037c 100644 --- a/libavcodec/dovi_rpuenc.c +++ b/libavcodec/dovi_rpuenc.c @@ -257,6 +257,62 @@ fail: return ret; } +/* Compares only the static DM metadata parts of AVDOVIColorMetadata (excluding + * dm_metadata_id and scene_refresh_flag) */ +static int cmp_dm_level0(const AVDOVIColorMetadata *dm1, + const AVDOVIColorMetadata *dm2) +{ + int ret; + for (int i = 0; i < FF_ARRAY_ELEMS(dm1->ycc_to_rgb_matrix); i++) { + if ((ret = av_cmp_q(dm1->ycc_to_rgb_matrix[i], dm2->ycc_to_rgb_matrix[i]))) + return ret; + } + + for (int i = 0; i < FF_ARRAY_ELEMS(dm1->ycc_to_rgb_offset); i++) { + if ((ret = av_cmp_q(dm1->ycc_to_rgb_offset[i], dm2->ycc_to_rgb_offset[i]))) + return ret; + } + + for (int i = 0; i < FF_ARRAY_ELEMS(dm1->rgb_to_lms_matrix); i++) { + if ((ret = av_cmp_q(dm1->rgb_to_lms_matrix[i], dm2->rgb_to_lms_matrix[i]))) + return ret; + } + + return memcmp(&dm1->signal_eotf, &dm2->signal_eotf, + sizeof(AVDOVIColorMetadata) -offsetof(AVDOVIColorMetadata, signal_eotf)); +} + +/* Tries to re-use the static ext blocks. May reorder `ext->dm_static` */ +static int try_reuse_ext(DOVIExt *ext, const AVDOVIMetadata *metadata) +{ + int i, j, idx = 0; + + for (i = 0; i < metadata->num_ext_blocks; i++) { + const AVDOVIDmData *dm = av_dovi_get_ext(metadata, i); + if (!ff_dovi_rpu_extension_is_static(dm->level)) + continue; + + /* Find the first matching ext block and move it to [idx] */ + for (j = idx; j < ext->num_static; j++) { + if (!memcmp(&ext->dm_static[j], dm, sizeof(*dm))) { + if (j != idx) + FFSWAP(AVDOVIDmData, ext->dm_static[j], ext->dm_static[idx]); + idx++; + break; + } + } + + if (j == ext->num_static) { + /* Found no matching ext block */ + return 0; + } + } + + /* If idx is less than ext->num_static, then there are extra unmatched + * ext blocks inside ext->dm_static */ + return idx == ext->num_static; +} + static inline void put_ue_coef(PutBitContext *pb, const AVDOVIRpuDataHeader *hdr, uint64_t coef) { @@ -498,7 +554,7 @@ int ff_dovi_rpu_generate(DOVIContext *s, const AVDOVIMetadata *metadata, const AVDOVIDataMapping *mapping; const AVDOVIColorMetadata *color; int vdr_dm_metadata_present, vdr_rpu_id, use_prev_vdr_rpu, profile, - buffer_size, rpu_size, pad, zero_run; + buffer_size, rpu_size, pad, zero_run, dm_compression; int num_ext_blocks_v1, num_ext_blocks_v2; int dv_md_compression = s->cfg.dv_md_compression; uint32_t crc; @@ -514,6 +570,11 @@ int ff_dovi_rpu_generate(DOVIContext *s, const AVDOVIMetadata *metadata, color = av_dovi_get_color(metadata); av_assert0(s->cfg.dv_profile); + if (!(flags & FF_DOVI_COMPRESS_RPU)) + dv_md_compression = AV_DOVI_COMPRESSION_NONE; + else if (dv_md_compression == AV_DOVI_COMPRESSION_RESERVED) + return AVERROR(EINVAL); + if (hdr->rpu_type != 2) { av_log(s->logctx, AV_LOG_ERROR, "Unhandled RPU type %"PRIu8"\n", hdr->rpu_type); @@ -555,9 +616,34 @@ int ff_dovi_rpu_generate(DOVIContext *s, const AVDOVIMetadata *metadata, } } + if (metadata->num_ext_blocks && !s->ext_blocks) { + s->ext_blocks = ff_refstruct_allocz(sizeof(*s->ext_blocks)); + if (!s->ext_blocks) + return AVERROR(ENOMEM); + } + + vdr_dm_metadata_present = memcmp(color, &ff_dovi_color_default, sizeof(*color)); + if (metadata->num_ext_blocks) + vdr_dm_metadata_present = 1; + + if (vdr_dm_metadata_present && !s->dm) { + s->dm = ff_refstruct_allocz(sizeof(AVDOVIColorMetadata)); + if (!s->dm) + return AVERROR(ENOMEM); + } + + dm_compression = 0; + if (dv_md_compression != AV_DOVI_COMPRESSION_NONE) { + if (!cmp_dm_level0(s->dm, color) && try_reuse_ext(s->ext_blocks, metadata)) + dm_compression = 1; + } + num_ext_blocks_v1 = num_ext_blocks_v2 = 0; for (int i = 0; i < metadata->num_ext_blocks; i++) { const AVDOVIDmData *dm = av_dovi_get_ext(metadata, i); + if (dm_compression && ff_dovi_rpu_extension_is_static(dm->level)) + continue; + switch (dm->level) { case 1: case 2: @@ -582,22 +668,6 @@ int ff_dovi_rpu_generate(DOVIContext *s, const AVDOVIMetadata *metadata, } } - if (metadata->num_ext_blocks && !s->ext_blocks) { - s->ext_blocks = ff_refstruct_allocz(sizeof(*s->ext_blocks)); - if (!s->ext_blocks) - return AVERROR(ENOMEM); - } - - vdr_dm_metadata_present = memcmp(color, &ff_dovi_color_default, sizeof(*color)); - if (num_ext_blocks_v1 || num_ext_blocks_v2) - vdr_dm_metadata_present = 1; - - if (vdr_dm_metadata_present && !s->dm) { - s->dm = ff_refstruct_allocz(sizeof(AVDOVIColorMetadata)); - if (!s->dm) - return AVERROR(ENOMEM); - } - buffer_size = 12 /* vdr seq info */ + 5 /* CRC32 + terminator */; buffer_size += num_ext_blocks_v1 * 13; buffer_size += num_ext_blocks_v2 * 28; @@ -638,7 +708,7 @@ int ff_dovi_rpu_generate(DOVIContext *s, const AVDOVIMetadata *metadata, set_ue_golomb(pb, (ext_mapping_idc << 8) | (hdr->el_bit_depth - 8)); set_ue_golomb(pb, hdr->vdr_bit_depth - 8); put_bits(pb, 1, hdr->spatial_resampling_filter_flag); - put_bits(pb, 3, 0); /* reserved_zero_3bits */ + put_bits(pb, 3, dm_compression); put_bits(pb, 1, hdr->el_spatial_resampling_filter_flag); put_bits(pb, 1, hdr->disable_residual_flag); } @@ -722,23 +792,25 @@ int ff_dovi_rpu_generate(DOVIContext *s, const AVDOVIMetadata *metadata, set_ue_golomb(pb, color->dm_metadata_id); /* affected_dm_id */ set_ue_golomb(pb, color->dm_metadata_id); /* current_dm_id */ set_ue_golomb(pb, color->scene_refresh_flag); - for (int i = 0; i < 9; i++) - put_sbits(pb, 16, av_q2den(color->ycc_to_rgb_matrix[i], 1 << 13)); - for (int i = 0; i < 3; i++) - put_bits32(pb, av_q2den(color->ycc_to_rgb_offset[i], denom)); - for (int i = 0; i < 9; i++) - put_sbits(pb, 16, av_q2den(color->rgb_to_lms_matrix[i], 1 << 14)); - put_bits(pb, 16, color->signal_eotf); - put_bits(pb, 16, color->signal_eotf_param0); - put_bits(pb, 16, color->signal_eotf_param1); - put_bits32(pb, color->signal_eotf_param2); - put_bits(pb, 5, color->signal_bit_depth); - put_bits(pb, 2, color->signal_color_space); - put_bits(pb, 2, color->signal_chroma_format); - put_bits(pb, 2, color->signal_full_range_flag); - put_bits(pb, 12, color->source_min_pq); - put_bits(pb, 12, color->source_max_pq); - put_bits(pb, 10, color->source_diagonal); + if (!dm_compression) { + for (int i = 0; i < 9; i++) + put_sbits(pb, 16, av_q2den(color->ycc_to_rgb_matrix[i], 1 << 13)); + for (int i = 0; i < 3; i++) + put_bits32(pb, av_q2den(color->ycc_to_rgb_offset[i], denom)); + for (int i = 0; i < 9; i++) + put_sbits(pb, 16, av_q2den(color->rgb_to_lms_matrix[i], 1 << 14)); + put_bits(pb, 16, color->signal_eotf); + put_bits(pb, 16, color->signal_eotf_param0); + put_bits(pb, 16, color->signal_eotf_param1); + put_bits32(pb, color->signal_eotf_param2); + put_bits(pb, 5, color->signal_bit_depth); + put_bits(pb, 2, color->signal_color_space); + put_bits(pb, 2, color->signal_chroma_format); + put_bits(pb, 2, color->signal_full_range_flag); + put_bits(pb, 12, color->source_min_pq); + put_bits(pb, 12, color->source_max_pq); + put_bits(pb, 10, color->source_diagonal); + } memcpy(s->dm, color, sizeof(*color)); s->color = s->dm; @@ -746,25 +818,35 @@ int ff_dovi_rpu_generate(DOVIContext *s, const AVDOVIMetadata *metadata, /* Extension blocks */ set_ue_golomb(pb, num_ext_blocks_v1); align_put_bits(pb); - for (int i = 0; i < metadata->num_ext_blocks; i++) - generate_ext_v1(pb, av_dovi_get_ext(metadata, i)); + for (int i = 0; i < metadata->num_ext_blocks; i++) { + const AVDOVIDmData *dm = av_dovi_get_ext(metadata, i); + if (dm_compression && ff_dovi_rpu_extension_is_static(dm->level)) + continue; + generate_ext_v1(pb, dm); + } if (num_ext_blocks_v2) { set_ue_golomb(pb, num_ext_blocks_v2); align_put_bits(pb); - for (int i = 0; i < metadata->num_ext_blocks; i++) + for (int i = 0; i < metadata->num_ext_blocks; i++) { + const AVDOVIDmData *dm = av_dovi_get_ext(metadata, i); + if (dm_compression && ff_dovi_rpu_extension_is_static(dm->level)) + continue; generate_ext_v2(pb, av_dovi_get_ext(metadata, i)); + } } if (ext) { size_t ext_sz = FFMIN(sizeof(AVDOVIDmData), metadata->ext_block_size); - ext->num_static = ext->num_dynamic = 0; + ext->num_dynamic = 0; + if (!dm_compression) + ext->num_static = 0; for (int i = 0; i < metadata->num_ext_blocks; i++) { const AVDOVIDmData *dm = av_dovi_get_ext(metadata, i); - if (ff_dovi_rpu_extension_is_static(dm->level)) - memcpy(&ext->dm_static[ext->num_static++], dm, ext_sz); - else + if (!ff_dovi_rpu_extension_is_static(dm->level)) memcpy(&ext->dm_dynamic[ext->num_dynamic++], dm, ext_sz); + else if (!dm_compression) + memcpy(&ext->dm_static[ext->num_static++], dm, ext_sz); } } } else {