From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTP id 76D3F4AFB8 for ; Sat, 25 May 2024 16:18:51 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 0531668D558; Sat, 25 May 2024 19:18:49 +0300 (EEST) Received: from haasn.dev (haasn.dev [78.46.187.166]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 2BBFF68D288 for ; Sat, 25 May 2024 19:18:42 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=haasn.xyz; s=mail; t=1716653921; bh=DsKiazwkZi7+Do0BlDRILBHDKbLf2hvwPFGojPcacsQ=; h=From:To:Cc:Subject:Date:From; b=Z2hN46T8StwNJG2gGAm723Vx2aQ5mmW/A0b2flr9fQPiWv7gOrQV0RRTePjIL+SR7 O+jcBGAJdTJE6Da5WAcT8ojg4YKTbILwe6dleoNHv9sE78mrJ+p2HeyjcazEOzyZaq sgJDLyoY+liKX5ifUU3cjNmoJzyflpjtMATLGD44= Received: from haasn.dev (unknown [10.30.0.2]) by haasn.dev (Postfix) with ESMTP id DAC16406AD; Sat, 25 May 2024 18:18:41 +0200 (CEST) From: Niklas Haas To: ffmpeg-devel@ffmpeg.org Date: Sat, 25 May 2024 18:18:39 +0200 Message-ID: <20240525161839.34867-1-ffmpeg@haasn.xyz> X-Mailer: git-send-email 2.45.0 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] avdovi/dovi_rpu{enc, dec}: fix ms_weight handling 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 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: From: Niklas Haas The code as written was wrong. The l2.ms_weight value is coded as a *signed* integer, rather than a shifted unsigned integer. (And even so, the offset of 8192 would be too large, since 2^12 = 4096. Ditto for l8.ms_weight, which should have an offset of 2048, not 8192) In addition, because the l8.ms_weight struct member is unsigned, these shifting semantics ended up overflowing the field, leading to undefined behavior when transcoding. Fortunately, the damage was relatively contained in practice, because it just corrupts the coding of this field, which is ignored in practice in all implementations I have seen. For some reason, Dolby decided to add a sign bit to l2.ms_weight but not l3.ms_weight. In either case, it's quite probably that this is still a shifted unsigned integer in disguise, since all samples seem to have a value of 2048 for these fields. But without specs, that's guesswork, and the official DV validator also reports these fields as unsigned integers with a value of 2048. --- libavcodec/dovi_rpudec.c | 4 ++-- libavcodec/dovi_rpuenc.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/libavcodec/dovi_rpudec.c b/libavcodec/dovi_rpudec.c index 7c7eda9d09..a477dbd4e3 100644 --- a/libavcodec/dovi_rpudec.c +++ b/libavcodec/dovi_rpudec.c @@ -142,7 +142,7 @@ static void parse_ext_v1(DOVIContext *s, GetBitContext *gb, AVDOVIDmData *dm) dm->l2.trim_power = get_bits(gb, 12); dm->l2.trim_chroma_weight = get_bits(gb, 12); dm->l2.trim_saturation_gain = get_bits(gb, 12); - dm->l2.ms_weight = get_bits(gb, 13) - 8192; + dm->l2.ms_weight = get_sbits(gb, 13); break; case 4: dm->l4.anchor_pq = get_bits(gb, 12); @@ -197,7 +197,7 @@ static void parse_ext_v2(DOVIContext *s, GetBitContext *gb, AVDOVIDmData *dm, dm->l8.trim_power = get_bits(gb, 12); dm->l8.trim_chroma_weight = get_bits(gb, 12); dm->l8.trim_saturation_gain = get_bits(gb, 12); - dm->l8.ms_weight = get_bits(gb, 12) - 8192; + dm->l8.ms_weight = get_bits(gb, 12); if (ext_block_length < 12) break; dm->l8.target_mid_contrast = get_bits(gb, 12); diff --git a/libavcodec/dovi_rpuenc.c b/libavcodec/dovi_rpuenc.c index 3c3e0f84c0..dacb8b54e7 100644 --- a/libavcodec/dovi_rpuenc.c +++ b/libavcodec/dovi_rpuenc.c @@ -276,7 +276,7 @@ static void generate_ext_v1(PutBitContext *pb, const AVDOVIDmData *dm) put_bits(pb, 12, dm->l2.trim_power); put_bits(pb, 12, dm->l2.trim_chroma_weight); put_bits(pb, 12, dm->l2.trim_saturation_gain); - put_bits(pb, 13, dm->l2.ms_weight + 8192); + put_sbits(pb, 13, dm->l2.ms_weight); break; case 4: put_bits(pb, 12, dm->l4.anchor_pq); @@ -374,7 +374,7 @@ static void generate_ext_v2(PutBitContext *pb, const AVDOVIDmData *dm) put_bits(pb, 12, dm->l8.trim_power); put_bits(pb, 12, dm->l8.trim_chroma_weight); put_bits(pb, 12, dm->l8.trim_saturation_gain); - put_bits(pb, 12, dm->l8.ms_weight + 8192); + put_bits(pb, 12, dm->l8.ms_weight); if (ext_block_length < 12) break; put_bits(pb, 12, dm->l8.target_mid_contrast); -- 2.45.0 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".