From 18a6c731c25a8da70b47dc1a46ecd3704cf8b0a1 Mon Sep 17 00:00:00 2001 From: Lusoris Date: Wed, 18 Nov 2026 03:34:20 +0100 Subject: [PATCH] libvmaf/adm: keep the adm_dwt2_16 vertical low-pass sum inside int32_t 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 --- .../src/feature/cuda/integer_adm/adm_dwt2.cu | 9 +- libvmaf/src/feature/integer_adm.c | 16 +- libvmaf/src/feature/x86/adm_avx2.c | 8 +- libvmaf/src/feature/x86/adm_avx512.c | 8 +- libvmaf/test/meson.build | 104 ++++++ libvmaf/test/test_adm_dwt2_16.c | 328 ++++++++++++++++++ 6 files changed, 454 insertions(+), 19 deletions(-) create mode 100644 libvmaf/test/test_adm_dwt2_16.c diff --git a/libvmaf/src/feature/cuda/integer_adm/adm_dwt2.cu b/libvmaf/src/feature/cuda/integer_adm/adm_dwt2.cu index 1497041755..b4c8d67254 100644 --- a/libvmaf/src/feature/cuda/integer_adm/adm_dwt2.cu +++ b/libvmaf/src/feature/cuda/integer_adm/adm_dwt2.cu @@ -204,15 +204,18 @@ __device__ __forceinline__ void adm_dwt2_8_vert_hori_kernel(const T * d_picture, break; } - int32_t accum_lo = 0, accum_hi = 0; + /* normalizing is done for range from(0 to N) to (-N/2 to N/2). + * The low-pass offset goes in first: subtracted last, three + * 16-bit samples of 42456 or more take the sum past INT32_MAX + * on the way. */ + int32_t accum_lo = -(params.dwt2_db2_coeffs_lo_sum * v_add_shift); + int32_t accum_hi = 0; #pragma unroll for (int i = 0;i< 4;++i) { accum_lo += filter_lo[i] * u_s[i + 2 * item]; accum_hi += filter_hi[i] * u_s[i + 2 * item]; } - /* normalizing is done for range from(0 to N) to (-N/2 to N/2) */ - accum_lo -= params.dwt2_db2_coeffs_lo_sum * v_add_shift; accum_hi -= params.dwt2_db2_coeffs_hi_sum * v_add_shift; s_tile[threadIdx.y * v_rows_per_thread + item][threadIdx.x] = make_short2((accum_lo + v_add_shift) >> v_shift, (accum_hi + v_add_shift) >> v_shift); diff --git a/libvmaf/src/feature/integer_adm.c b/libvmaf/src/feature/integer_adm.c index 11f2a304bd..fe4df94e4f 100644 --- a/libvmaf/src/feature/integer_adm.c +++ b/libvmaf/src/feature/integer_adm.c @@ -2561,15 +2561,15 @@ static void adm_dwt2_16_lo(const uint16_t *src, const adm_dwt_band_t *dst, AdmBu uint16_t u_s2 = src[ind_y[2][i] * src_stride + j]; uint16_t u_s3 = src[ind_y[3][i] * src_stride + j]; - accum = 0; + /* normalizing is done for range from(0 to N) to (-N/2 to N/2). + * The offset goes in first: subtracted last, three 16-bit samples + * of 42456 or more take the sum past INT32_MAX on the way. */ + accum = -((int32_t)dwt2_db2_coeffs_lo_sum * add_shift_VP); accum += (int32_t)filter_lo[0] * (int32_t)u_s0; accum += (int32_t)filter_lo[1] * (int32_t)u_s1; accum += (int32_t)filter_lo[2] * (int32_t)u_s2; accum += (int32_t)filter_lo[3] * (int32_t)u_s3; - /* normalizing is done for range from(0 to N) to (-N/2 to N/2) */ - accum -= (int32_t)dwt2_db2_coeffs_lo_sum * add_shift_VP; - tmplo[j] = (accum + add_shift_VP) >> shift_VP; } @@ -2621,15 +2621,15 @@ void adm_dwt2_16(const uint16_t *src, const adm_dwt_band_t *dst, AdmBuffer *buf, uint16_t u_s2 = src[ind_y[2][i] * src_stride + j]; uint16_t u_s3 = src[ind_y[3][i] * src_stride + j]; - accum = 0; + /* normalizing is done for range from(0 to N) to (-N/2 to N/2). + * The offset goes in first: subtracted last, three 16-bit samples + * of 42456 or more take the sum past INT32_MAX on the way. */ + accum = -((int32_t)dwt2_db2_coeffs_lo_sum * add_shift_VP); accum += (int32_t)filter_lo[0] * (int32_t)u_s0; accum += (int32_t)filter_lo[1] * (int32_t)u_s1; accum += (int32_t)filter_lo[2] * (int32_t)u_s2; accum += (int32_t)filter_lo[3] * (int32_t)u_s3; - /* normalizing is done for range from(0 to N) to (-N/2 to N/2) */ - accum -= (int32_t)dwt2_db2_coeffs_lo_sum * add_shift_VP; - tmplo[j] = (accum + add_shift_VP) >> shift_VP; accum = 0; diff --git a/libvmaf/src/feature/x86/adm_avx2.c b/libvmaf/src/feature/x86/adm_avx2.c index 27ba21481f..eb43dfb917 100644 --- a/libvmaf/src/feature/x86/adm_avx2.c +++ b/libvmaf/src/feature/x86/adm_avx2.c @@ -2937,15 +2937,15 @@ void adm_dwt2_16_avx2(const uint16_t *src, const adm_dwt_band_t *dst, AdmBuffer uint16_t u_s2 = src[ind_y[2][i] * src_stride + j]; uint16_t u_s3 = src[ind_y[3][i] * src_stride + j]; - accum = 0; + /* normalizing is done for range from(0 to N) to (-N/2 to N/2). + * The offset goes in first: subtracted last, three 16-bit samples + * of 42456 or more take the sum past INT32_MAX on the way. */ + accum = -((int32_t)dwt2_db2_coeffs_lo_sum * add_shift_VP); accum += (int32_t)filter_lo[0] * (int32_t)u_s0; accum += (int32_t)filter_lo[1] * (int32_t)u_s1; accum += (int32_t)filter_lo[2] * (int32_t)u_s2; accum += (int32_t)filter_lo[3] * (int32_t)u_s3; - /* normalizing is done for range from(0 to N) to (-N/2 to N/2) */ - accum -= (int32_t)dwt2_db2_coeffs_lo_sum * add_shift_VP; - tmplo[j] = (accum + add_shift_VP) >> shift_VP; accum = 0; diff --git a/libvmaf/src/feature/x86/adm_avx512.c b/libvmaf/src/feature/x86/adm_avx512.c index 93c546bc7f..eb80618198 100644 --- a/libvmaf/src/feature/x86/adm_avx512.c +++ b/libvmaf/src/feature/x86/adm_avx512.c @@ -3080,15 +3080,15 @@ void adm_dwt2_16_avx512(const uint16_t *src, const adm_dwt_band_t *dst, AdmBuffe uint16_t u_s2 = src[ind_y[2][i] * src_stride + j]; uint16_t u_s3 = src[ind_y[3][i] * src_stride + j]; - accum = 0; + /* normalizing is done for range from(0 to N) to (-N/2 to N/2). + * The offset goes in first: subtracted last, three 16-bit samples + * of 42456 or more take the sum past INT32_MAX on the way. */ + accum = -((int32_t)dwt2_db2_coeffs_lo_sum * add_shift_VP); accum += (int32_t)filter_lo[0] * (int32_t)u_s0; accum += (int32_t)filter_lo[1] * (int32_t)u_s1; accum += (int32_t)filter_lo[2] * (int32_t)u_s2; accum += (int32_t)filter_lo[3] * (int32_t)u_s3; - /* normalizing is done for range from(0 to N) to (-N/2 to N/2) */ - accum -= (int32_t)dwt2_db2_coeffs_lo_sum * add_shift_VP; - tmplo[j] = (accum + add_shift_VP) >> shift_VP; accum = 0; diff --git a/libvmaf/test/meson.build b/libvmaf/test/meson.build index 34bf36435d..823ac0b971 100644 --- a/libvmaf/test/meson.build +++ b/libvmaf/test/meson.build @@ -177,6 +177,109 @@ test_speed_filter = executable('test_speed_filter', ) test('test_speed_filter', test_speed_filter) +# The vertical pass of adm_dwt2_16() used to add up filter taps in an int32_t +# in an order that overflows for samples of 42456 and above. Signed overflow +# wraps back to the right value on the usual targets, so the output is correct +# either way and only an instrumented build can tell. Build the three +# translation units that hold the code under test (integer_adm.c, x86/adm_avx2.c +# and x86/adm_avx512.c) with signed integer overflow trapping, for this test +# only: the other objects come from the regular build, minus the regular +# objects of those three files so no symbol is defined twice. Without compiler +# support for the check the test still compares against the 64-bit reference. +adm_dwt2_16_trap_args = ['-fsanitize=signed-integer-overflow', + '-fno-sanitize-recover=signed-integer-overflow'] +adm_dwt2_16_trap = cc.has_multi_arguments(adm_dwt2_16_trap_args) and cc.links( + 'int main(void) { return 0; }', + args : adm_dwt2_16_trap_args, + name : 'with signed integer overflow trapping', +) +adm_dwt2_16_link_args = [] +adm_dwt2_16_objects = [ + common_cuda_objects, + libvmaf_cpu_static_lib.extract_all_objects(recursive: true), + libsvm_static_lib.extract_all_objects(recursive: true), +] + +if adm_dwt2_16_trap + adm_dwt2_16_link_args += adm_dwt2_16_trap_args + adm_dwt2_16_inc = [libvmaf_include, vmaf_include, include_directories('../src/cuda/')] + + adm_dwt2_16_feature_sources = [] + foreach src : libvmaf_feature_sources + if src != feature_src_dir + 'integer_adm.c' + adm_dwt2_16_feature_sources += src + endif + endforeach + adm_dwt2_16_trap_adm = static_library( + 'adm_dwt2_16_trap_adm', + '../src/feature/integer_adm.c', + include_directories : adm_dwt2_16_inc, + dependencies : [thread_lib, stdatomic_dependency, cuda_dependency, builtin_clz_dependency], + c_args : adm_dwt2_16_trap_args, + ) + adm_dwt2_16_objects += [ + libvmaf_feature_static_lib.extract_objects(adm_dwt2_16_feature_sources), + adm_dwt2_16_trap_adm.extract_all_objects(recursive: true), + ] + + if is_asm_enabled and host_machine.cpu_family().startswith('x86') + adm_dwt2_16_avx2_sources = [] + foreach src : x86_avx2_sources + if src != feature_src_dir + 'x86/adm_avx2.c' + adm_dwt2_16_avx2_sources += src + endif + endforeach + adm_dwt2_16_trap_avx2 = static_library( + 'adm_dwt2_16_trap_avx2', + '../src/feature/x86/adm_avx2.c', + include_directories : vmaf_base_include, + c_args : avx2_cflags + vmaf_cflags_common + adm_dwt2_16_trap_args, + dependencies : [builtin_clz_dependency], + ) + adm_dwt2_16_objects += [ + x86_avx2_static_lib.extract_objects(adm_dwt2_16_avx2_sources), + adm_dwt2_16_trap_avx2.extract_all_objects(recursive: true), + ] + + if is_avx512_enabled and is_avx512_supported + adm_dwt2_16_avx512_sources = [] + foreach src : x86_avx512_sources + if src != feature_src_dir + 'x86/adm_avx512.c' + adm_dwt2_16_avx512_sources += src + endif + endforeach + adm_dwt2_16_trap_avx512 = static_library( + 'adm_dwt2_16_trap_avx512', + '../src/feature/x86/adm_avx512.c', + include_directories : vmaf_base_include, + c_args : avx512_cflags + vmaf_cflags_common + adm_dwt2_16_trap_args, + dependencies : [builtin_clz_dependency], + ) + adm_dwt2_16_objects += [ + x86_avx512_static_lib.extract_objects(adm_dwt2_16_avx512_sources), + adm_dwt2_16_trap_avx512.extract_all_objects(recursive: true), + ] + endif + else + adm_dwt2_16_objects += platform_specific_cpu_objects + endif +else + adm_dwt2_16_objects += [ + platform_specific_cpu_objects, + libvmaf_feature_static_lib.extract_all_objects(recursive: true), + ] +endif + +test_adm_dwt2_16 = executable('test_adm_dwt2_16', + ['test.c', 'test_adm_dwt2_16.c', '../src/mem.c', '../src/picture.c', '../src/ref.c', + '../src/dict.c', '../src/opt.c', '../src/log.c', '../src/predict.c', + '../src/metadata_handler.c'], + include_directories : [libvmaf_inc, test_inc, include_directories('../src/')], + dependencies : [math_lib, stdatomic_dependency, thread_lib, cuda_dependency], + link_args : adm_dwt2_16_link_args, + objects : adm_dwt2_16_objects, +) + if float_enabled test_vif_tools = executable('test_vif_tools', ['test.c', 'test_vif_tools.c'], @@ -248,6 +351,7 @@ test('test_luminance_tools', test_luminance_tools) test('test_cli_parse', test_cli_parse) test('test_psnr', test_psnr) test('test_framesync', test_framesync) +test('test_adm_dwt2_16', test_adm_dwt2_16) test('test_propagate_metadata', test_propagate_metadata) test('test_adm_csf', test_adm_csf) test('test_barten_csf', test_barten_csf) diff --git a/libvmaf/test/test_adm_dwt2_16.c b/libvmaf/test/test_adm_dwt2_16.c new file mode 100644 index 0000000000..a851cd4710 --- /dev/null +++ b/libvmaf/test/test_adm_dwt2_16.c @@ -0,0 +1,328 @@ +/** + * + * Copyright 2026 Netflix, Inc. + * + * Licensed under the BSD+Patent License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSDplusPatent + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include +#include +#include +#include + +#include "test.h" +#include "config.h" +#include "cpu.h" +#include "feature/integer_adm.h" + +#if ARCH_X86 +#include "feature/x86/adm_avx2.h" +#if HAVE_AVX512 +#include "feature/x86/adm_avx512.h" +#endif +#endif + +typedef void (*adm_dwt2_16_fn)(const uint16_t *src, const adm_dwt_band_t *dst, + AdmBuffer *buf, int w, int h, int src_stride, + int dst_stride, int inp_size_bits); + +typedef enum { + FILL_RANDOM, /* pseudo-random values from low to high, inclusive */ + FILL_ONE_HIGH, /* one row in four at high, the rest at low */ + FILL_ONE_LOW, /* one row in four at low, the rest at high */ +} fill_kind; + +typedef struct { + fill_kind kind; + int phase; /* row of the period of four that stands out */ + uint32_t low; + uint32_t high; +} fill_t; + +static int16_t tap4(const int16_t *filter, const int64_t *s, int64_t offset, + int shift) +{ + int64_t accum = 0; + for (int k = 0; k < 4; k++) + accum += (int64_t)filter[k] * s[k]; + return (int16_t)((accum - offset + ((int64_t)1 << (shift - 1))) >> shift); +} + +/* The two passes of adm_dwt2_16() in 64-bit arithmetic. */ +static void reference_dwt2_16(const AdmBuffer *buf, const uint16_t *src, + int16_t *expected[4], int w, int h, int bpc) +{ + const int w_half = (w + 1) / 2; + const int h_half = (h + 1) / 2; + int16_t *tmplo = malloc(2 * w * sizeof(int16_t)); + int16_t *tmphi = tmplo + w; + const int64_t lo_offset = (int64_t)dwt2_db2_coeffs_lo_sum << (bpc - 1); + + for (int i = 0; i < h_half; i++) { + for (int j = 0; j < w; j++) { + int64_t s[4]; + for (int k = 0; k < 4; k++) + s[k] = src[buf->ind_y[k][i] * w + j]; + tmplo[j] = tap4(dwt2_db2_coeffs_lo, s, lo_offset, bpc); + tmphi[j] = tap4(dwt2_db2_coeffs_hi, s, 0, bpc); + } + for (int j = 0; j < w_half; j++) { + int64_t lo[4], hi[4]; + for (int k = 0; k < 4; k++) { + lo[k] = tmplo[buf->ind_x[k][j]]; + hi[k] = tmphi[buf->ind_x[k][j]]; + } + expected[0][i * w_half + j] = tap4(dwt2_db2_coeffs_lo, lo, 0, 16); + expected[1][i * w_half + j] = tap4(dwt2_db2_coeffs_hi, lo, 0, 16); + expected[2][i * w_half + j] = tap4(dwt2_db2_coeffs_lo, hi, 0, 16); + expected[3][i * w_half + j] = tap4(dwt2_db2_coeffs_hi, hi, 0, 16); + } + } + free(tmplo); +} + +static int matches_reference(adm_dwt2_16_fn fn, AdmBuffer *buf, + const uint16_t *src, int16_t *expected[4], + int w, int h, int bpc, const char *name) +{ + const int w_half = (w + 1) / 2; + const int h_half = (h + 1) / 2; + const int stride = (int)(buf->ind_size_x >> 2); + const adm_dwt_band_t *dst = &buf->ref_dwt2; + const int16_t *bands[4] = { dst->band_a, dst->band_v, dst->band_h, dst->band_d }; + + memset(dst->band_a, 0, h_half * stride * sizeof(int16_t)); + memset(dst->band_v, 0, h_half * stride * sizeof(int16_t)); + memset(dst->band_h, 0, h_half * stride * sizeof(int16_t)); + memset(dst->band_d, 0, h_half * stride * sizeof(int16_t)); + fn(src, dst, buf, w, h, w, stride, bpc); + + for (int b = 0; b < 4; b++) { + for (int i = 0; i < h_half; i++) { + for (int j = 0; j < w_half; j++) { + if (bands[b][i * stride + j] != expected[b][i * w_half + j]) { + fprintf(stderr, + "%s, %dx%d, %d bits, band %d (%d, %d): %d, expected %d\n", + name, w, h, bpc, b, i, j, bands[b][i * stride + j], + expected[b][i * w_half + j]); + return 0; + } + } + } + } + return 1; +} + +static void fill_source(uint16_t *src, int w, int h, const fill_t *fill) +{ + uint32_t lcg = 1; + const uint16_t high = (uint16_t)fill->high; + const uint16_t low = (uint16_t)fill->low; + + for (int y = 0; y < h; y++) { + for (int x = 0; x < w; x++) { + uint16_t v; + lcg = lcg * 1664525u + 1013904223u; + switch (fill->kind) { + case FILL_ONE_HIGH: + v = (y % 4 == fill->phase) ? high : low; + break; + case FILL_ONE_LOW: + v = (y % 4 == fill->phase) ? low : high; + break; + default: + v = (uint16_t)(low + (lcg >> 8) % (fill->high - fill->low + 1)); + break; + } + src[y * w + x] = v; + } + } +} + +static int run_case(int w, int h, int bpc, const fill_t *fill) +{ + AdmBuffer buf; + const int w_half = (w + 1) / 2; + const int h_half = (h + 1) / 2; + uint16_t *src = malloc((size_t)w * h * sizeof(uint16_t)); + int16_t *expected[4]; + int ok; + + for (int b = 0; b < 4; b++) + expected[b] = malloc((size_t)w_half * h_half * sizeof(int16_t)); + + fill_source(src, w, h, fill); + + if (adm_buffer_alloc(&buf, w, h)) { + ok = 0; + goto out; + } + dwt2_src_indices_filt(buf.ind_y, buf.ind_x, w, h); + reference_dwt2_16(&buf, src, expected, w, h, bpc); + + ok = matches_reference(adm_dwt2_16, &buf, src, expected, w, h, bpc, + "adm_dwt2_16"); +#if ARCH_X86 + const unsigned flags = vmaf_get_cpu_flags(); + if (ok && (flags & VMAF_X86_CPU_FLAG_AVX2)) + ok = matches_reference(adm_dwt2_16_avx2, &buf, src, expected, w, h, + bpc, "adm_dwt2_16_avx2"); +#if HAVE_AVX512 + if (ok && (flags & VMAF_X86_CPU_FLAG_AVX512)) + ok = matches_reference(adm_dwt2_16_avx512, &buf, src, expected, w, h, + bpc, "adm_dwt2_16_avx512"); +#endif +#endif + adm_buffer_free(&buf); +out: + for (int b = 0; b < 4; b++) + free(expected[b]); + free(src); + return ok; +} + +/* After the first output column, the AVX2 kernel takes steps of 16 columns + * over the largest multiple of 16 that fits in the row, and the AVX-512 kernel + * steps of 32 over the largest multiple of 64; the scalar loop finishes the + * row. With n = (w + 1) / 2 - 1 equal to 3, 15, 17, 31, 33, 47, 49, 63, 65, 95, + * 97, 127 and 129 the widths cover a row that is only remainder, rows + * with and without one, and one or more full steps. Odd and even widths cover + * both parities of the last column. + * + * Not covered: widths with n a positive multiple of 16, where the AVX2 kernel + * ends its last step on the final output column, and with n a positive + * multiple of 64 for the AVX-512 kernel. There the last column takes mirrored + * input in the scalar kernel and unmirrored input in the SIMD one. That + * horizontal pass difference is independent of the vertical pass tested here. */ +static const int widths[] = { + 8, 31, 32, 35, 36, 63, 64, 67, 68, 95, 96, 99, 100, 127, 128, 131, 132, + 191, 192, 195, 196, 255, 256, 259, 260 +}; +#define NUM_WIDTHS ((int)(sizeof(widths) / sizeof(widths[0]))) + +/* Rows: mirrored top and bottom, odd and even. */ +static const int heights[] = { 6, 7, 26 }; +#define NUM_HEIGHTS ((int)(sizeof(heights) / sizeof(heights[0]))) + +static int run_all_sizes(int bpc, const fill_t *fill) +{ + for (int hi = 0; hi < NUM_HEIGHTS; hi++) { + for (int wi = 0; wi < NUM_WIDTHS; wi++) { + if (!run_case(widths[wi], heights[hi], bpc, fill)) + return 0; + } + } + return 1; +} + +/* The first three low-pass taps add up to (lo[0] + lo[1] + lo[2]). A sample + * value v takes the sum of three equal samples past INT32_MAX when + * (lo[0] + lo[1] + lo[2]) * v does. Pin the value the boundary cases use. */ +#define OVERFLOW_SAMPLE 42456 + +static char *test_adm_dwt2_16_overflow_threshold() +{ + const int64_t taps = (int64_t)dwt2_db2_coeffs_lo[0] + + dwt2_db2_coeffs_lo[1] + dwt2_db2_coeffs_lo[2]; + mu_assert("42455 must stay within INT32_MAX in the first three taps", + taps * (OVERFLOW_SAMPLE - 1) <= INT32_MAX); + mu_assert("42456 must pass INT32_MAX in the first three taps", + taps * OVERFLOW_SAMPLE > INT32_MAX); + return NULL; +} + +/* Bright, dark and full-range random 16-bit input, and the extremes: all + * samples at the maximum is the worst case of the positive taps. */ +static char *test_adm_dwt2_16_sample_range() +{ + const fill_t bright = { FILL_RANDOM, 0, 57344, 65535 }; + const fill_t dark = { FILL_RANDOM, 0, 0, 8191 }; + const fill_t full = { FILL_RANDOM, 0, 0, 65535 }; + const fill_t maximum = { FILL_RANDOM, 0, 65535, 65535 }; + const fill_t zero = { FILL_RANDOM, 0, 0, 0 }; + + mu_assert("bright 16-bit input differs from the 64-bit reference", + run_all_sizes(16, &bright)); + mu_assert("dark 16-bit input differs from the 64-bit reference", + run_all_sizes(16, &dark)); + mu_assert("full-range 16-bit input differs from the 64-bit reference", + run_all_sizes(16, &full)); + mu_assert("maximum 16-bit input differs from the 64-bit reference", + run_all_sizes(16, &maximum)); + mu_assert("zero 16-bit input differs from the 64-bit reference", + run_all_sizes(16, &zero)); + return NULL; +} + +/* Constant input on both sides of the first sample value whose three-tap sum + * passes INT32_MAX, and row patterns that put one row of zero or of the value + * among rows of the other in every position of the vertical window. The + * window starts at row 4k + 3 for even and 4k + 1 for odd output rows, so the + * four phases cover each position for both. */ +static char *test_adm_dwt2_16_overflow_boundary() +{ + static const uint32_t values[] = { OVERFLOW_SAMPLE - 1, OVERFLOW_SAMPLE, + 65535 }; + const fill_t below = { FILL_RANDOM, 0, OVERFLOW_SAMPLE - 1, + OVERFLOW_SAMPLE - 1 }; + const fill_t at = { FILL_RANDOM, 0, OVERFLOW_SAMPLE, OVERFLOW_SAMPLE }; + + mu_assert("input of 42455 differs from the 64-bit reference", + run_all_sizes(16, &below)); + mu_assert("input of 42456 differs from the 64-bit reference", + run_all_sizes(16, &at)); + + for (int phase = 0; phase < 4; phase++) { + for (int v = 0; v < 3; v++) { + const fill_t one_high = { FILL_ONE_HIGH, phase, 0, values[v] }; + const fill_t one_low = { FILL_ONE_LOW, phase, 0, values[v] }; + + mu_assert("one row of the value among zero rows differs from " + "the 64-bit reference", + run_all_sizes(16, &one_high)); + mu_assert("one row of zero among rows of the value differs from " + "the 64-bit reference", + run_all_sizes(16, &one_low)); + } + } + return NULL; +} + +/* The other bit depths that take this path. */ +static char *test_adm_dwt2_16_bit_depths() +{ + static const int depths[] = { 10, 12 }; + + for (int d = 0; d < 2; d++) { + const uint32_t top = (1u << depths[d]) - 1; + const fill_t full = { FILL_RANDOM, 0, 0, top }; + const fill_t maximum = { FILL_RANDOM, 0, top, top }; + + mu_assert("full-range input differs from the 64-bit reference", + run_all_sizes(depths[d], &full)); + mu_assert("maximum input differs from the 64-bit reference", + run_all_sizes(depths[d], &maximum)); + } + return NULL; +} + +char *run_tests() +{ + vmaf_init_cpu(); + mu_run_test(test_adm_dwt2_16_overflow_threshold); + mu_run_test(test_adm_dwt2_16_sample_range); + mu_run_test(test_adm_dwt2_16_overflow_boundary); + mu_run_test(test_adm_dwt2_16_bit_depths); + return NULL; +}