Skip to content

feature/cambi: size the reciprocal LUT for the window; don't rescale window_size on re-init - #1720

Open
scottlaird wants to merge 1 commit into
Netflix:masterfrom
scottlaird:cambi-8k-window
Open

scottlaird wants to merge 1 commit into
Netflix:masterfrom
scottlaird:cambi-8k-window

Conversation

@scottlaird

Copy link
Copy Markdown

Fixes #1719. Since d655cef, CAMBI rejects any scaled window over 65, which covers 4K at the default window_size and nothing larger. Because init scaled window_size in place and libvmaf retries a failed init on every picture, the window then doubled on each retry until max_window * max_window overflowed int. At that point the check passed, and extraction segfaulted.

Changes

  • set_reciprocal_lut() chooses the LUT at init:
    • Windows up to 65 use the static table, as now.
    • Larger windows get an allocated table, built from the static values plus 1.0f / i for the rest.
    • The pointer lives in CambiBuffers and is passed to c_value_pixel, calculate_c_values and calculate_c_values_avx2. The AVX2 row function already took it as a parameter; the C row function ignored its parameter and used the global.
  • enc_window_size holds the scaled encode window. The window_size option is no longer modified, so a retried init computes the same window.
  • The size bound is now the window's area against UINT16_MAX, in unsigned arithmetic. The histogram counts are uint16_t, so that's the real limit: 255 at most, which covers window_size 127 at 7680x4320.

Scores are unchanged where CAMBI worked

At 4K and below the static table is still used, so scores are bit-identical to master. On 4-frame 1080p and 4K pairs, the frame and pooled JSON output of vmaf --feature cambi=full_ref=true is identical before and after the patch.

At 8K they match d655cef^, which divided directly, to every printed digit:

build 4K cambi / full_ref 8K cambi / full_ref
d655cef^ (division) 0.008868 / 0.007268 0.036680 / 0.033219
master 0.008868 / 0.007268 fails
this PR 0.008868 / 0.007268 0.036680 / 0.033219

The extended entries are correctly rounded 1.0f / i. I left the static table as it is, although 42 of its 4226 entries are one ULP off a correctly rounded 1.0f / i (i = 82, 83, 244, 488, 504, ...). They look like the result of printing a double to 9 digits and then rounding to float. Regenerating it would change scores at 4K and below in the last digits, which doesn't belong in this fix.

Tests

  • test_adjust_window_size: adds 7680x4320 with window_size 65 → 131.
  • test_set_reciprocal_lut: covers the static table at 65, allocated tables at 131 and 255 (the second call replaces the first table instead of leaking it), and rejection at 257.
  • test_init_window_size: initializes the real extractor twice with window_size=127 at 2560x1440. It checks that the option stays 127, the windows are 85, and the LUT covers 85².
  • The existing tests are updated for the new parameter.

meson test passes:

  • macOS arm64 (clang): 26/26, C path.
  • Linux x86-64 (GCC 15.2, HAVE_ASM, AVX2 CPU): 25/25.

On x86-64, the AVX2 path and the C path (--cpumask 65535) give identical frame and pooled output at 1080p, 4K and 8K. The 8K case uses the larger LUT through the AVX2 gather.

Under -Db_sanitize=address,undefined, the 8K and window_size=127 runs show no ASan or leak reports, and test_cambi passes 22/22. UBSan does report one misaligned int store in opt.c and one load in feature_name.c. Those come from the uint16_t option fields that #1658 fixes, and master's tools report the same pair. test_init_window_size is the first CAMBI test that creates the extractor through option parsing, so with halt_on_error=1 (meson's default for sanitized tests) it aborts on that store until #1658 is in.

Conflicts with #1658. That PR changes window_size, src_window_size and adjust_window_size() to int. Whichever lands second needs enc_window_size changed to match, which is a few lines. I'm happy to rebase onto it.

Not addressed here: a failed init still leaks what it allocated before failing, on every retry. That's a separate issue in how failed inits are retried.

🤖 Generated with Claude Code

…window_size on re-init

Since d655cef ("feature/cambi: add cambi_reciprocal_lut.h"), init rejects any
scaled window whose area reaches the static LUT's 4226 entries, which covers
windows up to 65: 4K at the default window_size. 8K (window 131), and any
window_size option above 65 at 4K, now fail, though the options allow both
(enc/src up to 7680x4320, window_size up to 127).

The failure then compounds. init scaled s->window_size in place, and when
init fails libvmaf calls it again for the next picture, so each retry
doubled the window (131, 263, ... 65535). At 65535, max_window * max_window
overflows int, the check passes, and extraction runs with a 65535-pixel
window: ffmpeg's libvmaf filter with n_threads > 0 segfaults on 8K input.

Build a larger LUT at init when the window needs one, starting from the
static table so scores at 4K and below are bit-identical, and pass it down
to c_value_pixel and the AVX2 path. Scale copies of window_size rather than
the option, and bound the window's area by the uint16_t histogram counts
in unsigned arithmetic.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

cambi: 8K fails since the reciprocal LUT (window_size too large), and the retries end in a segfault

1 participant