Fix 12-bit shift overflow in Highway SGR CalcAB - Compute z and b_res in CalcAB() using unsigned 32-bit lanes (uint32_tag) before ShiftRight, matching the C reference and SSE4.1/AVX2 logical right shifts (b_int + rnd_res exceeds INT32_MAX on 12-bit smooth regions with pixel values >= 2055). - Make RoundForShift() unsigned-clean to avoid sign-conversion warnings with uint32_tag. - Add ExtremeValuesTest and lowbd AVX-512 coverage in selfguided_filter_test.cc, plus Highbd12BitSelfguidedOverflow in encode_api_test.cc. TAG=agy CONV=56807471-9c51-4a72-a9ae-c0dee4bde47a Bug: 585314885 Change-Id: I42310349d9f977e391b768f6e2edeca408e912db
diff --git a/av1/common/selfguided_hwy.h b/av1/common/selfguided_hwy.h index 33433e4..22c3951d 100644 --- a/av1/common/selfguided_hwy.h +++ b/av1/common/selfguided_hwy.h
@@ -177,9 +177,8 @@ } template <typename D> -HWY_ATTR HWY_INLINE hn::VFromD<D> RoundForShift(D int32_tag, - unsigned int shift) { - return hn::Set(int32_tag, (1 << shift) >> 1); +HWY_ATTR HWY_INLINE hn::VFromD<D> RoundForShift(D tag, unsigned int shift) { + return hn::Set(tag, static_cast<hn::TFromD<D>>((1U << shift) >> 1)); } template <typename D> @@ -301,13 +300,14 @@ &av1_sgr_params[sgr_params_idx]; const int r = params->r[radius_idx]; const int n = (2 * r + 1) * (2 * r + 1); - const auto s = hn::Set(int32_tag, params->s[radius_idx]); + const auto s = + hn::Set(uint32_tag, static_cast<uint32_t>(params->s[radius_idx])); // one_over_n[n-1] is 2^12/n, so easily fits in an int16 const auto one_over_n = hn::BitCast(int16_tag, hn::Set(int32_tag, av1_one_by_x[n - 1])); - const auto rnd_z = RoundForShift(int32_tag, SGRPROJ_MTABLE_BITS); - const auto rnd_res = RoundForShift(int32_tag, SGRPROJ_RECIP_BITS); + const auto rnd_z = RoundForShift(uint32_tag, SGRPROJ_MTABLE_BITS); + const auto rnd_res = RoundForShift(uint32_tag, SGRPROJ_RECIP_BITS); // Set up masks const int max_lanes = static_cast<int>(hn::MaxLanes(int32_tag)); @@ -335,12 +335,13 @@ sum2 = hn::IfThenElseZero(mask[idx], sum2); } - const auto p = ComputeP(int32_tag, sum1, sum2, bit_depth, n); + const auto p = hn::BitCast(uint32_tag, + ComputeP(int32_tag, sum1, sum2, bit_depth, n)); const auto z = hn::BitCast( - int32_tag, hn::Min(hn::ShiftRight<SGRPROJ_MTABLE_BITS>(hn::BitCast( - uint32_tag, hn::MulAdd(p, s, rnd_z))), - hn::Set(uint32_tag, 255))); + int32_tag, + hn::Min(hn::ShiftRight<SGRPROJ_MTABLE_BITS>(hn::MulAdd(p, s, rnd_z)), + hn::Set(uint32_tag, 255))); const auto a_res = hn::GatherIndex(int32_tag, av1_x_by_xplus1, z); @@ -350,12 +351,17 @@ // sum1 might have lanes greater than 2^15, so we can't use madd to do // multiplication involving sum1. However, a_complement and one_over_n - // are both less than 256, so we can multiply them first. + // are both non-negative and less than 2^15, so we can multiply them + // first. const auto a_comp_over_n = hn::WidenMulPairwiseAdd( int32_tag, hn::BitCast(int16_tag, a_complement), one_over_n); - const auto b_int = hn::Mul(a_comp_over_n, sum1); - const auto b_res = - hn::ShiftRight<SGRPROJ_RECIP_BITS>(hn::Add(b_int, rnd_res)); + // Perform b_int and the right shift in unsigned 32-bit lanes because the + // intermediate product can exceed INT32_MAX (up to ~4.28e9 for 12-bit). + const auto b_int = hn::Mul(hn::BitCast(uint32_tag, a_comp_over_n), + hn::BitCast(uint32_tag, sum1)); + const auto b_res = hn::BitCast( + int32_tag, + hn::ShiftRight<SGRPROJ_RECIP_BITS>(hn::Add(b_int, rnd_res))); hn::StoreU(b_res, int32_tag, B + i * buf_stride + j); }
diff --git a/test/encode_api_test.cc b/test/encode_api_test.cc index edfa157..cb28bde 100644 --- a/test/encode_api_test.cc +++ b/test/encode_api_test.cc
@@ -5639,6 +5639,63 @@ aom_img_free(img0); ASSERT_EQ(aom_codec_destroy(&enc), AOM_CODEC_OK); } + +#if !CONFIG_REALTIME_ONLY +// Regression test for b/585314885: Signed 32-bit arithmetic right shift in +// selfguided CalcAB() for 12-bit bright flat regions corrupted flt0/flt1 and +// caused a 64-bit signed integer overflow in get_proj_subspace(). +TEST(EncodeAPI, Highbd12BitSelfguidedOverflow) { + aom_codec_iface_t *const iface = aom_codec_av1_cx(); + aom_codec_enc_cfg_t cfg; + ASSERT_EQ(aom_codec_enc_config_default(iface, &cfg, AOM_USAGE_GOOD_QUALITY), + AOM_CODEC_OK); + cfg.g_profile = 2; + cfg.g_w = 247; + cfg.g_h = 1; + cfg.g_bit_depth = AOM_BITS_12; + cfg.g_input_bit_depth = 12; + cfg.g_lag_in_frames = 0; + cfg.rc_end_usage = AOM_Q; + + aom_codec_ctx_t enc_ctx; + ASSERT_EQ( + aom_codec_enc_init(&enc_ctx, iface, &cfg, AOM_CODEC_USE_HIGHBITDEPTH), + AOM_CODEC_OK); + struct CodecDeleter { + void operator()(aom_codec_ctx_t *c) const { aom_codec_destroy(c); } + }; + std::unique_ptr<aom_codec_ctx_t, CodecDeleter> enc(&enc_ctx); + ASSERT_EQ(aom_codec_control(enc.get(), AOME_SET_CPUUSED, 4), AOM_CODEC_OK); + ASSERT_EQ(aom_codec_control(enc.get(), AOME_SET_CQ_LEVEL, 32), AOM_CODEC_OK); + ASSERT_EQ(aom_codec_control(enc.get(), AV1E_SET_ENABLE_RESTORATION, 1), + AOM_CODEC_OK); + + std::unique_ptr<aom_image_t, decltype(&aom_img_free)> image( + aom_img_alloc(nullptr, AOM_IMG_FMT_I44416, cfg.g_w, cfg.g_h, 1), + &aom_img_free); + ASSERT_NE(image.get(), nullptr); + for (int plane = 0; plane < 3; ++plane) { + const int w = aom_img_plane_width(image.get(), plane); + const int h = aom_img_plane_height(image.get(), plane); + for (int i = 0; i < h; ++i) { + Memset16(image->planes[plane] + i * image->stride[plane], 2313, + static_cast<size_t>(w)); + } + } + Memset16(image->planes[AOM_PLANE_Y], 1086, 1); + Memset16(image->planes[AOM_PLANE_Y] + sizeof(uint16_t), 0, 1); + + ASSERT_NO_FATAL_FAILURE(EncodeOne(enc.get(), image.get(), 0)); + image.reset(); + + ASSERT_EQ(aom_codec_encode(enc.get(), nullptr, 0, 0, 0), AOM_CODEC_OK); + aom_codec_iter_t iter = nullptr; + while (aom_codec_get_cx_data(enc.get(), &iter) != nullptr) { + } + + ASSERT_EQ(aom_codec_destroy(enc.release()), AOM_CODEC_OK); +} +#endif // !CONFIG_REALTIME_ONLY #endif // CONFIG_AV1_HIGHBITDEPTH #if CONFIG_AV1_DECODER
diff --git a/test/selfguided_filter_test.cc b/test/selfguided_filter_test.cc index 4a2d814..7ccde80 100644 --- a/test/selfguided_filter_test.cc +++ b/test/selfguided_filter_test.cc
@@ -9,7 +9,10 @@ * PATENTS file, you can obtain it at www.aomedia.org/license/patent. */ +#include <algorithm> +#include <cstring> #include <ctime> +#include <memory> #include <tuple> #include "gtest/gtest.h" @@ -35,6 +38,257 @@ int eps, const int *xqd, uint8_t *dst8, int dst_stride, int32_t *tmpbuf, int bit_depth, int highbd); +struct AomFree { + void operator()(void *p) const { aom_free(p); } +}; + +inline uint8_t *ToSgrBytePtr(uint8_t *p) { return p; } +#if CONFIG_AV1_HIGHBITDEPTH +inline uint8_t *ToSgrBytePtr(uint16_t *p) { return CONVERT_TO_BYTEPTR(p); } +#endif // CONFIG_AV1_HIGHBITDEPTH + +template <typename Pixel> +struct SelfguidedTestBuffers { + static constexpr int kBorder = 16; + int stride = 0; + int out_stride = 0; + std::unique_ptr<Pixel, AomFree> input_buf; + std::unique_ptr<Pixel, AomFree> output_buf; + std::unique_ptr<Pixel, AomFree> output2_buf; + std::unique_ptr<int32_t, AomFree> tmpbuf; + Pixel *input = nullptr; + Pixel *output = nullptr; + Pixel *output2 = nullptr; + + bool Init(int max_h, int in_stride, int out_s, bool allocate_output2) { + stride = in_stride; + out_stride = out_s; + const size_t in_count = + static_cast<size_t>(stride) * static_cast<size_t>(max_h + 2 * kBorder); + const size_t out_count = static_cast<size_t>(out_stride) * + static_cast<size_t>(max_h + 2 * kBorder); + + input_buf.reset( + static_cast<Pixel *>(aom_memalign(32, in_count * sizeof(Pixel)))); + if (!input_buf) return false; + output_buf.reset( + static_cast<Pixel *>(aom_memalign(32, out_count * sizeof(Pixel)))); + if (!output_buf) return false; + if (allocate_output2) { + output2_buf.reset( + static_cast<Pixel *>(aom_memalign(32, out_count * sizeof(Pixel)))); + if (!output2_buf) return false; + memset(output2_buf.get(), 0, out_count * sizeof(Pixel)); + output2 = output2_buf.get() + out_stride * kBorder + kBorder; + } + tmpbuf.reset( + static_cast<int32_t *>(aom_memalign(32, RESTORATION_TMPBUF_SIZE))); + if (!tmpbuf) return false; + + memset(input_buf.get(), 0, in_count * sizeof(Pixel)); + memset(output_buf.get(), 0, out_count * sizeof(Pixel)); + input = input_buf.get() + stride * kBorder + kBorder; + output = output_buf.get() + out_stride * kBorder + kBorder; + return true; + } +}; + +template <typename Pixel> +void VerifySelfguidedTiles(SgrFunc tst_fun, SelfguidedTestBuffers<Pixel> *bufs, + int width, int height, int eps, const int *xqd, + int bit_depth, int highbd) { + const int pu_width = RESTORATION_PROC_UNIT_SIZE; + const int pu_height = RESTORATION_PROC_UNIT_SIZE; + for (int k = 0; k < height; k += pu_height) { + for (int j = 0; j < width; j += pu_width) { + const int w = AOMMIN(pu_width, width - j); + const int h = AOMMIN(pu_height, height - k); + Pixel *input_p = bufs->input + k * bufs->stride + j; + Pixel *output_p = bufs->output + k * bufs->out_stride + j; + Pixel *output2_p = bufs->output2 + k * bufs->out_stride + j; + ASSERT_EQ(tst_fun(ToSgrBytePtr(input_p), w, h, bufs->stride, eps, xqd, + ToSgrBytePtr(output_p), bufs->out_stride, + bufs->tmpbuf.get(), bit_depth, highbd), + 0); + ASSERT_EQ(av1_apply_selfguided_restoration_c( + ToSgrBytePtr(input_p), w, h, bufs->stride, eps, xqd, + ToSgrBytePtr(output2_p), bufs->out_stride, + bufs->tmpbuf.get(), bit_depth, highbd), + 0); + } + } + + for (int j = 0; j < height; ++j) { + for (int k = 0; k < width; ++k) { + ASSERT_EQ(bufs->output[j * bufs->out_stride + k], + bufs->output2[j * bufs->out_stride + k]); + } + } +} + +template <typename Pixel> +void RunSelfguidedSpeedTest(SgrFunc tst_fun, int bit_depth, int highbd, + const char *suite_name) { + const int pu_width = RESTORATION_PROC_UNIT_SIZE; + const int pu_height = RESTORATION_PROC_UNIT_SIZE; + const int width = 256, height = 256, stride = 288, out_stride = 288; + const int NUM_ITERS = 2000; + const int mask = (1 << bit_depth) - 1; + + SelfguidedTestBuffers<Pixel> bufs; + ASSERT_TRUE(bufs.Init(height, stride, out_stride, false)); + + ACMRandom rnd(ACMRandom::DeterministicSeed()); + + for (int i = -16; i < height + 16; ++i) + for (int j = -16; j < width + 16; ++j) + bufs.input[i * stride + j] = static_cast<Pixel>(rnd.Rand16() & mask); + + int xqd[2] = { SGRPROJ_PRJ_MIN0 + + rnd.PseudoUniform(SGRPROJ_PRJ_MAX0 + 1 - SGRPROJ_PRJ_MIN0), + SGRPROJ_PRJ_MIN1 + rnd.PseudoUniform(SGRPROJ_PRJ_MAX1 + 1 - + SGRPROJ_PRJ_MIN1) }; + // Fix a parameter set, since the speed depends slightly on r. + // Change this to test different combinations of values of r. + int eps = 15; + + av1_loop_restoration_precal(); + + aom_usec_timer ref_timer; + aom_usec_timer_start(&ref_timer); + for (int i = 0; i < NUM_ITERS; ++i) { + for (int k = 0; k < height; k += pu_height) + for (int j = 0; j < width; j += pu_width) { + int w = AOMMIN(pu_width, width - j); + int h = AOMMIN(pu_height, height - k); + Pixel *input_p = bufs.input + k * stride + j; + Pixel *output_p = bufs.output + k * out_stride + j; + ASSERT_EQ(av1_apply_selfguided_restoration_c( + ToSgrBytePtr(input_p), w, h, stride, eps, xqd, + ToSgrBytePtr(output_p), out_stride, bufs.tmpbuf.get(), + bit_depth, highbd), + 0); + } + } + aom_usec_timer_mark(&ref_timer); + const int64_t ref_time = aom_usec_timer_elapsed(&ref_timer); + + aom_usec_timer tst_timer; + aom_usec_timer_start(&tst_timer); + for (int i = 0; i < NUM_ITERS; ++i) { + for (int k = 0; k < height; k += pu_height) + for (int j = 0; j < width; j += pu_width) { + int w = AOMMIN(pu_width, width - j); + int h = AOMMIN(pu_height, height - k); + Pixel *input_p = bufs.input + k * stride + j; + Pixel *output_p = bufs.output + k * out_stride + j; + ASSERT_EQ(tst_fun(ToSgrBytePtr(input_p), w, h, stride, eps, xqd, + ToSgrBytePtr(output_p), out_stride, bufs.tmpbuf.get(), + bit_depth, highbd), + 0); + } + } + aom_usec_timer_mark(&tst_timer); + const int64_t tst_time = aom_usec_timer_elapsed(&tst_timer); + + std::cout << "[ ] C time = " << ref_time / 1000 + << " ms, SIMD time = " << tst_time / 1000 << " ms\n"; + + EXPECT_GT(ref_time, tst_time) + << "Error: " << suite_name << ".SpeedTest, SIMD slower than C.\n" + << "C time: " << ref_time << " us\n" + << "SIMD time: " << tst_time << " us\n"; +} + +template <typename Pixel> +void RunSelfguidedCorrectnessTest(SgrFunc tst_fun, int bit_depth, int highbd) { + // Set the maximum width/height to test here. We actually test a small + // range of sizes *up to* this size, so that we can check, eg., + // the behaviour on tiles which are not a multiple of 4 wide. + const int max_w = 260, max_h = 260, stride = 672, out_stride = 672; + const int NUM_ITERS = 81; + const int mask = (1 << bit_depth) - 1; + + SelfguidedTestBuffers<Pixel> bufs; + ASSERT_TRUE(bufs.Init(max_h, stride, out_stride, true)); + + ACMRandom rnd(ACMRandom::DeterministicSeed()); + + av1_loop_restoration_precal(); + + for (int i = 0; i < NUM_ITERS; ++i) { + for (int j = -16; j < max_h + 16; ++j) + for (int k = -16; k < max_w + 16; ++k) + bufs.input[j * stride + k] = static_cast<Pixel>(rnd.Rand16() & mask); + + int xqd[2] = { SGRPROJ_PRJ_MIN0 + rnd.PseudoUniform(SGRPROJ_PRJ_MAX0 + 1 - + SGRPROJ_PRJ_MIN0), + SGRPROJ_PRJ_MIN1 + rnd.PseudoUniform(SGRPROJ_PRJ_MAX1 + 1 - + SGRPROJ_PRJ_MIN1) }; + int eps = rnd.PseudoUniform(1 << SGRPROJ_PARAMS_BITS); + + // Test various tile sizes around 256x256 + int test_w = max_w - (i / 9); + int test_h = max_h - (i % 9); + + ASSERT_NO_FATAL_FAILURE(VerifySelfguidedTiles( + tst_fun, &bufs, test_w, test_h, eps, xqd, bit_depth, highbd)); + } +} + +template <typename Pixel> +void RunSelfguidedExtremeValuesTest(SgrFunc tst_fun, int bit_depth, + int highbd) { + const int max_w = 247, max_h = 35, stride = 672, out_stride = 672; + const int max_val = (1 << bit_depth) - 1; + + SelfguidedTestBuffers<Pixel> bufs; + ASSERT_TRUE(bufs.Init(max_h, stride, out_stride, true)); + + av1_loop_restoration_precal(); + + // Cover boundary values around the 32-bit signed shift overflow threshold + // in 12-bit flat/nearly-flat regions (2055 for r=2, 2057 for r=1, 2313 from + // b/585314885, and max_val). + const int values[] = { + 0, + 1 << (bit_depth - 1), + std::min((1 << (bit_depth - 1)) + 9, max_val), + std::min(2313, max_val), + max_val, + }; + const struct { + int width; + int height; + } kSizes[] = { { 247, 1 }, { 67, 35 } }; + const int xqd[2] = { 16, 16 }; + + for (const int base_val : values) { + for (int pattern = 0; pattern < 2; ++pattern) { + for (int j = -16; j < max_h + 16; ++j) { + for (int k = -16; k < max_w + 16; ++k) { + const int delta = (pattern == 0) ? 0 : (((j ^ k) & 1) ? 1 : -1); + bufs.input[j * stride + k] = + static_cast<Pixel>(std::clamp(base_val + delta, 0, max_val)); + } + } + + for (const auto &sz : kSizes) { + for (int eps = 0; eps < (1 << SGRPROJ_PARAMS_BITS); ++eps) { + SCOPED_TRACE(testing::Message() + << "bit_depth=" << bit_depth << " highbd=" << highbd + << " base_val=" << base_val << " pattern=" << pattern + << " size=" << sz.width << "x" << sz.height + << " eps=" << eps); + ASSERT_NO_FATAL_FAILURE( + VerifySelfguidedTiles(tst_fun, &bufs, sz.width, sz.height, eps, + xqd, bit_depth, highbd)); + } + } + } + } +} + // Test parameter list: // <tst_fun_> using FilterTestParam = tuple<SgrFunc>; @@ -47,169 +301,23 @@ protected: void RunSpeedTest() { - tst_fun_ = GET_PARAM(0); - const int pu_width = RESTORATION_PROC_UNIT_SIZE; - const int pu_height = RESTORATION_PROC_UNIT_SIZE; - const int width = 256, height = 256, stride = 288, out_stride = 288; - const int NUM_ITERS = 2000; - int i, j, k; - - uint8_t *input_ = - (uint8_t *)aom_memalign(32, stride * (height + 32) * sizeof(uint8_t)); - ASSERT_NE(input_, nullptr); - uint8_t *output_ = (uint8_t *)aom_memalign( - 32, out_stride * (height + 32) * sizeof(uint8_t)); - ASSERT_NE(output_, nullptr); - int32_t *tmpbuf = (int32_t *)aom_memalign(32, RESTORATION_TMPBUF_SIZE); - ASSERT_NE(tmpbuf, nullptr); - uint8_t *input = input_ + stride * 16 + 16; - uint8_t *output = output_ + out_stride * 16 + 16; - - ACMRandom rnd(ACMRandom::DeterministicSeed()); - - for (i = -16; i < height + 16; ++i) - for (j = -16; j < width + 16; ++j) - input[i * stride + j] = rnd.Rand16() & 0xFF; - - int xqd[2] = { SGRPROJ_PRJ_MIN0 + rnd.PseudoUniform(SGRPROJ_PRJ_MAX0 + 1 - - SGRPROJ_PRJ_MIN0), - SGRPROJ_PRJ_MIN1 + rnd.PseudoUniform(SGRPROJ_PRJ_MAX1 + 1 - - SGRPROJ_PRJ_MIN1) }; - // Fix a parameter set, since the speed depends slightly on r. - // Change this to test different combinations of values of r. - int eps = 15; - - av1_loop_restoration_precal(); - - aom_usec_timer ref_timer; - aom_usec_timer_start(&ref_timer); - for (i = 0; i < NUM_ITERS; ++i) { - for (k = 0; k < height; k += pu_height) - for (j = 0; j < width; j += pu_width) { - int w = AOMMIN(pu_width, width - j); - int h = AOMMIN(pu_height, height - k); - uint8_t *input_p = input + k * stride + j; - uint8_t *output_p = output + k * out_stride + j; - const int ret_c = av1_apply_selfguided_restoration_c( - input_p, w, h, stride, eps, xqd, output_p, out_stride, tmpbuf, 8, - 0); - ASSERT_EQ(ret_c, 0); - } - } - aom_usec_timer_mark(&ref_timer); - const int64_t ref_time = aom_usec_timer_elapsed(&ref_timer); - - aom_usec_timer tst_timer; - aom_usec_timer_start(&tst_timer); - for (i = 0; i < NUM_ITERS; ++i) { - for (k = 0; k < height; k += pu_height) - for (j = 0; j < width; j += pu_width) { - int w = AOMMIN(pu_width, width - j); - int h = AOMMIN(pu_height, height - k); - uint8_t *input_p = input + k * stride + j; - uint8_t *output_p = output + k * out_stride + j; - const int ret_tst = tst_fun_(input_p, w, h, stride, eps, xqd, - output_p, out_stride, tmpbuf, 8, 0); - ASSERT_EQ(ret_tst, 0); - } - } - aom_usec_timer_mark(&tst_timer); - const int64_t tst_time = aom_usec_timer_elapsed(&tst_timer); - - std::cout << "[ ] C time = " << ref_time / 1000 - << " ms, SIMD time = " << tst_time / 1000 << " ms\n"; - - EXPECT_GT(ref_time, tst_time) - << "Error: AV1SelfguidedFilterTest.SpeedTest, SIMD slower than C.\n" - << "C time: " << ref_time << " us\n" - << "SIMD time: " << tst_time << " us\n"; - - aom_free(input_); - aom_free(output_); - aom_free(tmpbuf); + RunSelfguidedSpeedTest<uint8_t>(GET_PARAM(0), 8, 0, + "AV1SelfguidedFilterTest"); } void RunCorrectnessTest() { - tst_fun_ = GET_PARAM(0); - const int pu_width = RESTORATION_PROC_UNIT_SIZE; - const int pu_height = RESTORATION_PROC_UNIT_SIZE; - // Set the maximum width/height to test here. We actually test a small - // range of sizes *up to* this size, so that we can check, eg., - // the behaviour on tiles which are not a multiple of 4 wide. - const int max_w = 260, max_h = 260, stride = 672, out_stride = 672; - const int NUM_ITERS = 81; - int i, j, k; - - uint8_t *input_ = - (uint8_t *)aom_memalign(32, stride * (max_h + 32) * sizeof(uint8_t)); - ASSERT_NE(input_, nullptr); - uint8_t *output_ = (uint8_t *)aom_memalign( - 32, out_stride * (max_h + 32) * sizeof(uint8_t)); - ASSERT_NE(output_, nullptr); - uint8_t *output2_ = (uint8_t *)aom_memalign( - 32, out_stride * (max_h + 32) * sizeof(uint8_t)); - ASSERT_NE(output2_, nullptr); - int32_t *tmpbuf = (int32_t *)aom_memalign(32, RESTORATION_TMPBUF_SIZE); - ASSERT_NE(tmpbuf, nullptr); - - uint8_t *input = input_ + stride * 16 + 16; - uint8_t *output = output_ + out_stride * 16 + 16; - uint8_t *output2 = output2_ + out_stride * 16 + 16; - - ACMRandom rnd(ACMRandom::DeterministicSeed()); - - av1_loop_restoration_precal(); - - for (i = 0; i < NUM_ITERS; ++i) { - for (j = -16; j < max_h + 16; ++j) - for (k = -16; k < max_w + 16; ++k) - input[j * stride + k] = rnd.Rand16() & 0xFF; - - int xqd[2] = { SGRPROJ_PRJ_MIN0 + rnd.PseudoUniform(SGRPROJ_PRJ_MAX0 + 1 - - SGRPROJ_PRJ_MIN0), - SGRPROJ_PRJ_MIN1 + rnd.PseudoUniform(SGRPROJ_PRJ_MAX1 + 1 - - SGRPROJ_PRJ_MIN1) }; - int eps = rnd.PseudoUniform(1 << SGRPROJ_PARAMS_BITS); - - // Test various tile sizes around 256x256 - int test_w = max_w - (i / 9); - int test_h = max_h - (i % 9); - - for (k = 0; k < test_h; k += pu_height) - for (j = 0; j < test_w; j += pu_width) { - int w = AOMMIN(pu_width, test_w - j); - int h = AOMMIN(pu_height, test_h - k); - uint8_t *input_p = input + k * stride + j; - uint8_t *output_p = output + k * out_stride + j; - uint8_t *output2_p = output2 + k * out_stride + j; - const int ret_tst = tst_fun_(input_p, w, h, stride, eps, xqd, - output_p, out_stride, tmpbuf, 8, 0); - ASSERT_EQ(ret_tst, 0); - const int ret_c = av1_apply_selfguided_restoration_c( - input_p, w, h, stride, eps, xqd, output2_p, out_stride, tmpbuf, 8, - 0); - ASSERT_EQ(ret_c, 0); - } - - for (j = 0; j < test_h; ++j) - for (k = 0; k < test_w; ++k) { - ASSERT_EQ(output[j * out_stride + k], output2[j * out_stride + k]); - } - } - - aom_free(input_); - aom_free(output_); - aom_free(output2_); - aom_free(tmpbuf); + RunSelfguidedCorrectnessTest<uint8_t>(GET_PARAM(0), 8, 0); } - private: - SgrFunc tst_fun_; + void RunExtremeValuesTest() { + RunSelfguidedExtremeValuesTest<uint8_t>(GET_PARAM(0), 8, 0); + } }; GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(AV1SelfguidedFilterTest); TEST_P(AV1SelfguidedFilterTest, DISABLED_SpeedTest) { RunSpeedTest(); } TEST_P(AV1SelfguidedFilterTest, CorrectnessTest) { RunCorrectnessTest(); } +TEST_P(AV1SelfguidedFilterTest, ExtremeValuesTest) { RunExtremeValuesTest(); } #if HAVE_SSE4_1 INSTANTIATE_TEST_SUITE_P( @@ -223,6 +331,12 @@ ::testing::Values(av1_apply_selfguided_restoration_avx2)); #endif +#if HAVE_AVX512 && CONFIG_HIGHWAY +INSTANTIATE_TEST_SUITE_P( + AVX512, AV1SelfguidedFilterTest, + ::testing::Values(av1_apply_selfguided_restoration_avx512)); +#endif + #if HAVE_NEON INSTANTIATE_TEST_SUITE_P( NEON, AV1SelfguidedFilterTest, @@ -242,171 +356,25 @@ protected: void RunSpeedTest() { - tst_fun_ = GET_PARAM(0); - const int pu_width = RESTORATION_PROC_UNIT_SIZE; - const int pu_height = RESTORATION_PROC_UNIT_SIZE; - const int width = 256, height = 256, stride = 288, out_stride = 288; - const int NUM_ITERS = 2000; - int i, j, k; - int bit_depth = GET_PARAM(1); - int mask = (1 << bit_depth) - 1; - - uint16_t *input_ = - (uint16_t *)aom_memalign(32, stride * (height + 32) * sizeof(uint16_t)); - ASSERT_NE(input_, nullptr); - uint16_t *output_ = (uint16_t *)aom_memalign( - 32, out_stride * (height + 32) * sizeof(uint16_t)); - ASSERT_NE(output_, nullptr); - int32_t *tmpbuf = (int32_t *)aom_memalign(32, RESTORATION_TMPBUF_SIZE); - ASSERT_NE(tmpbuf, nullptr); - uint16_t *input = input_ + stride * 16 + 16; - uint16_t *output = output_ + out_stride * 16 + 16; - - ACMRandom rnd(ACMRandom::DeterministicSeed()); - - for (i = -16; i < height + 16; ++i) - for (j = -16; j < width + 16; ++j) - input[i * stride + j] = rnd.Rand16() & mask; - - int xqd[2] = { SGRPROJ_PRJ_MIN0 + rnd.PseudoUniform(SGRPROJ_PRJ_MAX0 + 1 - - SGRPROJ_PRJ_MIN0), - SGRPROJ_PRJ_MIN1 + rnd.PseudoUniform(SGRPROJ_PRJ_MAX1 + 1 - - SGRPROJ_PRJ_MIN1) }; - // Fix a parameter set, since the speed depends slightly on r. - // Change this to test different combinations of values of r. - int eps = 15; - - av1_loop_restoration_precal(); - - aom_usec_timer ref_timer; - aom_usec_timer_start(&ref_timer); - for (i = 0; i < NUM_ITERS; ++i) { - for (k = 0; k < height; k += pu_height) - for (j = 0; j < width; j += pu_width) { - int w = AOMMIN(pu_width, width - j); - int h = AOMMIN(pu_height, height - k); - uint16_t *input_p = input + k * stride + j; - uint16_t *output_p = output + k * out_stride + j; - av1_apply_selfguided_restoration_c( - CONVERT_TO_BYTEPTR(input_p), w, h, stride, eps, xqd, - CONVERT_TO_BYTEPTR(output_p), out_stride, tmpbuf, bit_depth, 1); - } - } - aom_usec_timer_mark(&ref_timer); - const int64_t ref_time = aom_usec_timer_elapsed(&ref_timer); - - aom_usec_timer tst_timer; - aom_usec_timer_start(&tst_timer); - for (i = 0; i < NUM_ITERS; ++i) { - for (k = 0; k < height; k += pu_height) - for (j = 0; j < width; j += pu_width) { - int w = AOMMIN(pu_width, width - j); - int h = AOMMIN(pu_height, height - k); - uint16_t *input_p = input + k * stride + j; - uint16_t *output_p = output + k * out_stride + j; - tst_fun_(CONVERT_TO_BYTEPTR(input_p), w, h, stride, eps, xqd, - CONVERT_TO_BYTEPTR(output_p), out_stride, tmpbuf, bit_depth, - 1); - } - } - aom_usec_timer_mark(&tst_timer); - const int64_t tst_time = aom_usec_timer_elapsed(&tst_timer); - - std::cout << "[ ] C time = " << ref_time / 1000 - << " ms, SIMD time = " << tst_time / 1000 << " ms\n"; - - EXPECT_GT(ref_time, tst_time) - << "Error: AV1HighbdSelfguidedFilterTest.SpeedTest, SIMD slower than " - "C.\n" - << "C time: " << ref_time << " us\n" - << "SIMD time: " << tst_time << " us\n"; - - aom_free(input_); - aom_free(output_); - aom_free(tmpbuf); + RunSelfguidedSpeedTest<uint16_t>(GET_PARAM(0), GET_PARAM(1), 1, + "AV1HighbdSelfguidedFilterTest"); } void RunCorrectnessTest() { - tst_fun_ = GET_PARAM(0); - const int pu_width = RESTORATION_PROC_UNIT_SIZE; - const int pu_height = RESTORATION_PROC_UNIT_SIZE; - // Set the maximum width/height to test here. We actually test a small - // range of sizes *up to* this size, so that we can check, eg., - // the behaviour on tiles which are not a multiple of 4 wide. - const int max_w = 260, max_h = 260, stride = 672, out_stride = 672; - const int NUM_ITERS = 81; - int i, j, k; - int bit_depth = GET_PARAM(1); - int mask = (1 << bit_depth) - 1; - - uint16_t *input_ = - (uint16_t *)aom_memalign(32, stride * (max_h + 32) * sizeof(uint16_t)); - ASSERT_NE(input_, nullptr); - uint16_t *output_ = (uint16_t *)aom_memalign( - 32, out_stride * (max_h + 32) * sizeof(uint16_t)); - ASSERT_NE(output_, nullptr); - uint16_t *output2_ = (uint16_t *)aom_memalign( - 32, out_stride * (max_h + 32) * sizeof(uint16_t)); - ASSERT_NE(output2_, nullptr); - int32_t *tmpbuf = (int32_t *)aom_memalign(32, RESTORATION_TMPBUF_SIZE); - ASSERT_NE(tmpbuf, nullptr); - - uint16_t *input = input_ + stride * 16 + 16; - uint16_t *output = output_ + out_stride * 16 + 16; - uint16_t *output2 = output2_ + out_stride * 16 + 16; - - ACMRandom rnd(ACMRandom::DeterministicSeed()); - - av1_loop_restoration_precal(); - - for (i = 0; i < NUM_ITERS; ++i) { - for (j = -16; j < max_h + 16; ++j) - for (k = -16; k < max_w + 16; ++k) - input[j * stride + k] = rnd.Rand16() & mask; - - int xqd[2] = { SGRPROJ_PRJ_MIN0 + rnd.PseudoUniform(SGRPROJ_PRJ_MAX0 + 1 - - SGRPROJ_PRJ_MIN0), - SGRPROJ_PRJ_MIN1 + rnd.PseudoUniform(SGRPROJ_PRJ_MAX1 + 1 - - SGRPROJ_PRJ_MIN1) }; - int eps = rnd.PseudoUniform(1 << SGRPROJ_PARAMS_BITS); - - // Test various tile sizes around 256x256 - int test_w = max_w - (i / 9); - int test_h = max_h - (i % 9); - - for (k = 0; k < test_h; k += pu_height) - for (j = 0; j < test_w; j += pu_width) { - int w = AOMMIN(pu_width, test_w - j); - int h = AOMMIN(pu_height, test_h - k); - uint16_t *input_p = input + k * stride + j; - uint16_t *output_p = output + k * out_stride + j; - uint16_t *output2_p = output2 + k * out_stride + j; - tst_fun_(CONVERT_TO_BYTEPTR(input_p), w, h, stride, eps, xqd, - CONVERT_TO_BYTEPTR(output_p), out_stride, tmpbuf, bit_depth, - 1); - av1_apply_selfguided_restoration_c( - CONVERT_TO_BYTEPTR(input_p), w, h, stride, eps, xqd, - CONVERT_TO_BYTEPTR(output2_p), out_stride, tmpbuf, bit_depth, 1); - } - - for (j = 0; j < test_h; ++j) - for (k = 0; k < test_w; ++k) - ASSERT_EQ(output[j * out_stride + k], output2[j * out_stride + k]); - } - - aom_free(input_); - aom_free(output_); - aom_free(output2_); - aom_free(tmpbuf); + RunSelfguidedCorrectnessTest<uint16_t>(GET_PARAM(0), GET_PARAM(1), 1); } - private: - SgrFunc tst_fun_; + void RunExtremeValuesTest() { + RunSelfguidedExtremeValuesTest<uint16_t>(GET_PARAM(0), GET_PARAM(1), 1); + } }; GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(AV1HighbdSelfguidedFilterTest); TEST_P(AV1HighbdSelfguidedFilterTest, DISABLED_SpeedTest) { RunSpeedTest(); } TEST_P(AV1HighbdSelfguidedFilterTest, CorrectnessTest) { RunCorrectnessTest(); } +TEST_P(AV1HighbdSelfguidedFilterTest, ExtremeValuesTest) { + RunExtremeValuesTest(); +} #if HAVE_SSE4_1 const int highbd_params_sse4_1[] = { 8, 10, 12 };