Skip to content

Spectral blocks rounding errors in jpeg encoder #1751

Closed

Description

@br3aker

Prerequisites

  • I have written a descriptive issue title
  • I have verified that I am running the latest version of ImageSharp
  • I have verified if the problem exist in both DEBUG and RELEASE mode
  • I have searched open and closed issues to ensure it has not already been reported

Description

Jpeg encoder spectral block quantization code has rounding errors:

if (Avx.IsSupported)
{
var vnegOne = Vector256.Create(-1f);
var vadd = Vector256.Create(.5F);
var vone = Vector256.Create(1f);
for (int i = 0; i < RowCount; i++)
{
ref Vector256<float> aRow = ref Unsafe.Add(ref a.V0, i);
ref Vector256<float> bRow = ref Unsafe.Add(ref b.V0, i);
Vector256<float> voff = Avx.Multiply(Avx.Min(Avx.Max(vnegOne, aRow), vone), vadd);
aRow = Avx.Add(Avx.Divide(aRow, bRow), voff);
}
}

Let's say all values of aRow are -0.6 and all values of bRow are 1:

aRow = -0.6
bRow = 1.0

// voff calculation
max(-1, -0.6) = -0.6
min(-0.6, 1) = -0.6
mul(-0.6, 0.5) = -0.3

// division and 'rounding'
div(-0.6, 1) = -0.6
add(-0.6, -0.3) = -0.9

Ouput value is -0.9 while expected rounded value is -1 and that's a problem because actual encoding code truncates these:

And we get 0 instead of -1. This error makes current implementation non-complient to the spec. Good news are that only values in range (-1..1) are affected by this.

Steps to Reproduce

None. Changing it to a correct rounding intrinsic vroundps for example shows a significant difference in binary size. In the debugger you can actually spot the difference in the WriteBlock call values.

Activity

  1. br3aker commented on Aug 26, 2021

    @br3aker
    ContributorAuthor

    On top of that current implementation might be slower than actually calling single precision rounding avx intrinsic.

    Intrinsic all alone latency is 8 at max. Current implementation has a dependency chain of min -> max -> mul calls which has a total latency of 4 + 4 + 4 = 12 at max and 9 at min depending on the architecture. This can be improved by the loop unrolling possibly, but in the vacuum intrinsic seems to be faster by a tiny bit - at least it's not slower.

    I'm already working on it as part of some optimizations to the jpeg encoder, I've opened this issue just to inform that the problem exists.

  2. JimBobSquarePants commented on Aug 26, 2021

    @JimBobSquarePants
    Member

    How did you even discover this?! 🤯

  3. br3aker commented on Aug 26, 2021

    @br3aker
    ContributorAuthor

    How did you even discover this?! 🤯

    Long story short, I am trying to SIMD'ify entire jpeg encoding thingy and for zigzag to work I converted floats to shorts via intrinsic with built-in rounding which yielded quite the difference in binary size - up to 7% for quality=100. After resulting binary comparison I've discovered that intrinsic rounding writes more -1/1 values when current implementation does not.

  4. br3aker commented on Sep 7, 2021

    @br3aker
    ContributorAuthor

    Same problem for automatic vectorization Vector4 fallback code:

    private static Vector4 DivideRound(Vector4 dividend, Vector4 divisor)
    {
    var neg = new Vector4(-1);
    var add = new Vector4(.5F);
    // sign(dividend) = max(min(dividend, 1), -1)
    Vector4 sign = Numerics.Clamp(dividend, neg, Vector4.One);
    // AlmostRound(dividend/divisor) = dividend/divisor + 0.5*sign(dividend)
    return (dividend / divisor) + (sign * add);
    }

    add must be multiplied by either -1 or 1, Clamp returns any value between -1 and 1.

  5. JimBobSquarePants commented on Sep 9, 2021

    @JimBobSquarePants
    Member

    You absolutely rock mate. 🤘

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions