Skip to content

libvmaf/adm: keep the adm_dwt2_16 vertical low-pass sum inside int32_t - #1730

Open
lusoris wants to merge 1 commit into
Netflix:masterfrom
vidiko:fix/adm-dwt2-16-vertical-sum
Open

lusoris wants to merge 1 commit into
Netflix:masterfrom
vidiko:fix/adm-dwt2-16-vertical-sum

Conversation

@lusoris

@lusoris lusoris commented Nov 18, 2026

Copy link
Copy Markdown

The vertical pass of adm_dwt2_16() adds four tap products in an int32_t and subtracts the rounding offset last. With 16-bit samples of 42456 or more the sum passes INT32_MAX after the third tap, which is signed integer overflow. This is the second of the two concerns PR 1601 mixed together; the tap-shift change is in a separate PR.

Fix

Start the accumulator from the offset, then add the four taps. The low-pass taps are {15826, 27411, 7345, -4240} and the offset is 46342 * 2^(bits-1), so at 16 bits the partial sums now stay within -1,796,403,056 to 1,796,356,714. The old order reached 3,314,891,370. The final value is the same.

The same order is applied at every site with this sum:

  • adm_dwt2_16() and adm_dwt2_16_lo() in integer_adm.c
  • the vertical pass of adm_dwt2_16_avx2() and adm_dwt2_16_avx512()
  • the CUDA kernel adm_dwt2_8_vert_hori_kernel in adm_dwt2.cu, which is also instantiated for uint16_t

The high-pass sum (at most 27411 * 65535) and the horizontal passes (int16 inputs) stay inside int32_t and are not changed.

Before and after

UBSan build (-Db_sanitize=undefined), 16-bit 576x324 pair src01_hrc00/src01_hrc01 (yuv420p16le, from the clips that lived in python/test/resource/yuv before bac8b607), --feature adm:

master cf677baa this branch
"signed integer overflow" reports, AVX-512 path (default dispatch on the test host) 4 0
same, AVX2 path (--cpumask 16) 4 0
ADM scores, release build reference byte-equal JSON

Tests

test_adm_dwt2_16 runs each available dispatch over inputs that cover the full 16-bit sample range and compares the output against a 64-bit reference.

Overflow wraps back to the right value on the usual targets, so comparing values alone cannot fail on the old code. The test therefore builds its own copies of integer_adm.c, x86/adm_avx2.c and x86/adm_avx512.c with -fsanitize=signed-integer-overflow -fno-sanitize-recover=signed-integer-overflow when the compiler accepts and links those flags; the rest of the library comes from the regular build. Where the flags are not available the test still does the value comparison. This is most of the test/meson.build change.

With the fix reverted in one file at a time the test fails at that file's site (integer_adm.c:2626, adm_avx2.c:2942, adm_avx512.c:3085), with GCC 16.2.1 and Clang 23.1.5.

Validation

  • x86-64 Linux, GCC 16.2.1, release build: the libvmaf unit suite passes 28 of 28.
  • The CUDA change is compiled with nvcc and not run on a GPU; integer_adm_cuda.c marks the uint16_t kernel as untested.
  • CI on a fork branch with this commit plus one fork-only commit that pins the workflow actions to commit SHAs (the fork requires pinned actions): libvmaf, Windows, Docker, ffmpeg, checkasm, all green.

The vertical pass adds four tap products in an int32_t and subtracts
the rounding offset last. With 16-bit samples of 42456 or more the sum
passes INT32_MAX after the third tap, which is signed overflow. Start
the accumulator from the offset instead: the partial sums then stay
within +/-1796403056 and the result is unchanged.

Applies to adm_dwt2_16 and adm_dwt2_16_lo, the vertical pass of
adm_dwt2_16_avx2 and adm_dwt2_16_avx512, and the CUDA vertical kernel
in its uint16_t instance.

test_adm_dwt2_16 compares each dispatch against a 64-bit reference and
builds the three C files with -fsanitize=signed-integer-overflow where
the compiler supports it, so the old order fails the test.

Signed-off-by: Lusoris <lusoris@pm.me>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant