From 43035256d784c5e5d3ed25b67f2bffa68311d2da Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Wed, 2 Sep 2026 22:19:25 +1000 Subject: [PATCH] Correct AV1 distortion cost scaling --- .../Formats/Heif/Av1/Entropy/Av1RateDistortion.cs | 2 +- tests/ImageSharp.Tests/Formats/Heif/Av1/Av1EntropyTests.cs | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/ImageSharp/Formats/Heif/Av1/Entropy/Av1RateDistortion.cs b/src/ImageSharp/Formats/Heif/Av1/Entropy/Av1RateDistortion.cs index c0edfb1ccd..d25f972d7c 100644 --- a/src/ImageSharp/Formats/Heif/Av1/Entropy/Av1RateDistortion.cs +++ b/src/ImageSharp/Formats/Heif/Av1/Entropy/Av1RateDistortion.cs @@ -19,6 +19,6 @@ internal static class Av1RateDistortion { long weightedRate = (long)rate * rateMultiplier; long roundedRate = (weightedRate + (1 << (Av1ProbabilityCost.CostShift - 1))) >> Av1ProbabilityCost.CostShift; - return roundedRate + distortion; + return roundedRate + (distortion << 7); } } diff --git a/tests/ImageSharp.Tests/Formats/Heif/Av1/Av1EntropyTests.cs b/tests/ImageSharp.Tests/Formats/Heif/Av1/Av1EntropyTests.cs index 966d78d8df..cbde2bf2d3 100644 --- a/tests/ImageSharp.Tests/Formats/Heif/Av1/Av1EntropyTests.cs +++ b/tests/ImageSharp.Tests/Formats/Heif/Av1/Av1EntropyTests.cs @@ -522,9 +522,9 @@ public class Av1EntropyTests [Theory] [InlineData(1, 255, 0L, 0L)] [InlineData(1, 256, 0L, 1L)] - [InlineData(128, 512, 1000L, 1128L)] - [InlineData(512, 512, 1000L, 1512L)] - [InlineData(64, 1024, 4_000_000_000L, 4_000_000_128L)] + [InlineData(128, 512, 1000L, 128_128L)] + [InlineData(512, 512, 1000L, 128_512L)] + [InlineData(64, 1024, 4_000_000_000L, 512_000_000_128L)] public void RateDistortionCostMatchesCurrentLibaom( int rateMultiplier, int rate,