tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

av1_scale_test.cc (12127B)


      1 /*
      2 *  Copyright (c) 2017 The WebM project authors. All Rights Reserved.
      3 *  Copyright (c) 2024, Alliance for Open Media. All rights reserved.
      4 *
      5 *  Use of this source code is governed by a BSD-style license
      6 *  that can be found in the LICENSE file in the root of the source
      7 *  tree. An additional intellectual property rights grant can be found
      8 *  in the file PATENTS.  All contributing project authors may
      9 *  be found in the AUTHORS file in the root of the source tree.
     10 */
     11 
     12 #include <assert.h>
     13 #include <string.h>
     14 
     15 #include <tuple>
     16 
     17 #include "gtest/gtest.h"
     18 
     19 #include "common/av1_config.h"
     20 #include "config/av1_rtcd.h"
     21 
     22 #include "config/aom_config.h"
     23 #include "config/aom_dsp_rtcd.h"
     24 
     25 #include "aom_dsp/aom_dsp_common.h"
     26 #include "aom_dsp/aom_filter.h"
     27 #include "aom_mem/aom_mem.h"
     28 #include "aom_ports/aom_timer.h"
     29 #include "aom_ports/mem.h"
     30 #include "av1/common/filter.h"
     31 #include "test/acm_random.h"
     32 #include "test/register_state_check.h"
     33 #include "test/util.h"
     34 
     35 namespace {
     36 
     37 using ResizeFrameFunc = void (*)(const YV12_BUFFER_CONFIG *src,
     38                                 YV12_BUFFER_CONFIG *dst,
     39                                 const InterpFilter filter, const int phase,
     40                                 const int num_planes);
     41 
     42 class ResizeAndExtendTest : public ::testing::TestWithParam<ResizeFrameFunc> {
     43 public:
     44  ResizeAndExtendTest() { resize_fn_ = GetParam(); }
     45  ~ResizeAndExtendTest() override = default;
     46 
     47 protected:
     48  const int kBufFiller = 123;
     49  const int kBufMax = kBufFiller - 1;
     50 
     51  void FillPlane(uint8_t *const buf, const int width, const int height,
     52                 const int stride) {
     53    for (int y = 0; y < height; ++y) {
     54      for (int x = 0; x < width; ++x) {
     55        buf[x + (y * stride)] = (x + (width * y)) % kBufMax;
     56      }
     57    }
     58  }
     59 
     60  void ResetResizeImage(YV12_BUFFER_CONFIG *const img, const int width,
     61                        const int height, const int border) {
     62    memset(img, 0, sizeof(*img));
     63    ASSERT_EQ(0, aom_alloc_frame_buffer(img, width, height, 1, 1, 0, border, 16,
     64                                        false, 0));
     65    memset(img->buffer_alloc, kBufFiller, img->frame_size);
     66  }
     67 
     68  void ResetResizeImages(const int src_width, const int src_height,
     69                         const int dst_width, const int dst_height,
     70                         const int dst_border) {
     71    ResetResizeImage(&img_, src_width, src_height, AOM_BORDER_IN_PIXELS);
     72    ResetResizeImage(&ref_img_, dst_width, dst_height, dst_border);
     73    ResetResizeImage(&dst_img_, dst_width, dst_height, dst_border);
     74    FillPlane(img_.y_buffer, img_.y_crop_width, img_.y_crop_height,
     75              img_.y_stride);
     76    FillPlane(img_.u_buffer, img_.uv_crop_width, img_.uv_crop_height,
     77              img_.uv_stride);
     78    FillPlane(img_.v_buffer, img_.uv_crop_width, img_.uv_crop_height,
     79              img_.uv_stride);
     80  }
     81 
     82  void DeallocResizeImages() {
     83    aom_free_frame_buffer(&img_);
     84    aom_free_frame_buffer(&ref_img_);
     85    aom_free_frame_buffer(&dst_img_);
     86  }
     87 
     88  void RunTest(InterpFilter filter_type) {
     89    static const int kNumSizesToTest = 22;
     90    static const int kNumScaleFactorsToTest = 4;
     91    static const int kNumDstBordersToTest = 2;
     92    static const int kSizesToTest[] = { 1,  2,  3,  4,  6,   8,  10, 12,
     93                                        14, 16, 18, 20, 22,  24, 26, 28,
     94                                        30, 32, 34, 68, 128, 134 };
     95    static const int kScaleFactors[] = { 1, 2, 3, 4 };
     96    static const int kDstBorders[] = { 0, AOM_BORDER_IN_PIXELS };
     97    for (int border = 0; border < kNumDstBordersToTest; ++border) {
     98      const int dst_border = kDstBorders[border];
     99      for (int phase_scaler = 0; phase_scaler < 16; ++phase_scaler) {
    100        for (int h = 0; h < kNumSizesToTest; ++h) {
    101          const int src_height = kSizesToTest[h];
    102          for (int w = 0; w < kNumSizesToTest; ++w) {
    103            const int src_width = kSizesToTest[w];
    104            for (int sf_up_idx = 0; sf_up_idx < kNumScaleFactorsToTest;
    105                 ++sf_up_idx) {
    106              const int sf_up = kScaleFactors[sf_up_idx];
    107              for (int sf_down_idx = 0; sf_down_idx < kNumScaleFactorsToTest;
    108                   ++sf_down_idx) {
    109                const int sf_down = kScaleFactors[sf_down_idx];
    110                const int dst_width = src_width * sf_up / sf_down;
    111                const int dst_height = src_height * sf_up / sf_down;
    112                // TODO: bug aomedia:363916152 - Enable unit tests for 4 to 3
    113                // scaling when Neon and SSSE3 implementation of
    114                // av1_resize_and_extend_frame do not differ from scalar version
    115                if (sf_down == 4 && sf_up == 3) {
    116                  continue;
    117                }
    118 
    119                if (sf_up == sf_down && sf_up != 1) {
    120                  continue;
    121                }
    122                // I420 frame width and height must be even.
    123                if (!dst_width || !dst_height || dst_width & 1 ||
    124                    dst_height & 1) {
    125                  continue;
    126                }
    127                // aom_convolve8_c() has restriction on the step which cannot
    128                // exceed 64 (ratio 1 to 4).
    129                if (src_width > 4 * dst_width || src_height > 4 * dst_height) {
    130                  continue;
    131                }
    132                ASSERT_NO_FATAL_FAILURE(ResetResizeImages(
    133                    src_width, src_height, dst_width, dst_height, dst_border));
    134 
    135                av1_resize_and_extend_frame_c(&img_, &ref_img_, filter_type,
    136                                              phase_scaler, 1);
    137                resize_fn_(&img_, &dst_img_, filter_type, phase_scaler, 1);
    138 
    139                if (memcmp(dst_img_.buffer_alloc, ref_img_.buffer_alloc,
    140                           ref_img_.frame_size)) {
    141                  printf(
    142                      "filter_type = %d, phase_scaler = %d, src_width = %4d, "
    143                      "src_height = %4d, dst_width = %4d, dst_height = %4d, "
    144                      "scale factor = %d:%d\n",
    145                      filter_type, phase_scaler, src_width, src_height,
    146                      dst_width, dst_height, sf_down, sf_up);
    147                  PrintDiff();
    148                }
    149 
    150                EXPECT_EQ(ref_img_.frame_size, dst_img_.frame_size);
    151                EXPECT_EQ(0,
    152                          memcmp(ref_img_.buffer_alloc, dst_img_.buffer_alloc,
    153                                 ref_img_.frame_size));
    154 
    155                DeallocResizeImages();
    156              }
    157            }
    158          }
    159        }
    160      }
    161    }
    162  }
    163 
    164  void PrintDiffComponent(const uint8_t *const ref, const uint8_t *const opt,
    165                          const int stride, const int width, const int height,
    166                          const int plane_idx) const {
    167    for (int y = 0; y < height; y++) {
    168      for (int x = 0; x < width; x++) {
    169        if (ref[y * stride + x] != opt[y * stride + x]) {
    170          printf("Plane %d pixel[%d][%d] diff:%6d (ref),%6d (opt)\n", plane_idx,
    171                 y, x, ref[y * stride + x], opt[y * stride + x]);
    172          break;
    173        }
    174      }
    175    }
    176  }
    177 
    178  void PrintDiff() const {
    179    assert(ref_img_.y_stride == dst_img_.y_stride);
    180    assert(ref_img_.y_width == dst_img_.y_width);
    181    assert(ref_img_.y_height == dst_img_.y_height);
    182    assert(ref_img_.uv_stride == dst_img_.uv_stride);
    183    assert(ref_img_.uv_width == dst_img_.uv_width);
    184    assert(ref_img_.uv_height == dst_img_.uv_height);
    185 
    186    if (memcmp(dst_img_.buffer_alloc, ref_img_.buffer_alloc,
    187               ref_img_.frame_size)) {
    188      PrintDiffComponent(ref_img_.y_buffer, dst_img_.y_buffer,
    189                         ref_img_.y_stride, ref_img_.y_width, ref_img_.y_height,
    190                         0);
    191      PrintDiffComponent(ref_img_.u_buffer, dst_img_.u_buffer,
    192                         ref_img_.uv_stride, ref_img_.uv_width,
    193                         ref_img_.uv_height, 1);
    194      PrintDiffComponent(ref_img_.v_buffer, dst_img_.v_buffer,
    195                         ref_img_.uv_stride, ref_img_.uv_width,
    196                         ref_img_.uv_height, 2);
    197    }
    198  }
    199 
    200  void SpeedTest() {
    201    static const int kCountSpeedTestBlock = 100;
    202    static const int kNumScaleFactorsToTest = 4;
    203    static const int kNumInterpFiltersToTest = 3;
    204    static const int kScaleFactors[] = { 1, 2, 3, 4 };
    205    static const int kInterpFilters[] = { 0, 1, 3 };
    206    const int src_width = 1280;
    207    const int src_height = 720;
    208    for (int filter = 0; filter < kNumInterpFiltersToTest; ++filter) {
    209      const InterpFilter filter_type =
    210          static_cast<InterpFilter>(kInterpFilters[filter]);
    211      for (int phase_scaler = 0; phase_scaler < 2; ++phase_scaler) {
    212        for (int sf_up_idx = 0; sf_up_idx < kNumScaleFactorsToTest;
    213             ++sf_up_idx) {
    214          const int sf_up = kScaleFactors[sf_up_idx];
    215          for (int sf_down_idx = 0; sf_down_idx < kNumScaleFactorsToTest;
    216               ++sf_down_idx) {
    217            const int sf_down = kScaleFactors[sf_down_idx];
    218            const int dst_width = src_width * sf_up / sf_down;
    219            const int dst_height = src_height * sf_up / sf_down;
    220            // TODO: bug aomedia:363916152 - Enable unit tests for 4 to 3
    221            // scaling when Neon and SSSE3 implementation of
    222            // av1_resize_and_extend_frame do not differ from scalar version
    223            if (sf_down == 4 && sf_up == 3) {
    224              continue;
    225            }
    226 
    227            if (sf_up == sf_down && sf_up != 1) {
    228              continue;
    229            }
    230            // I420 frame width and height must be even.
    231            if (dst_width & 1 || dst_height & 1) {
    232              continue;
    233            }
    234            ASSERT_NO_FATAL_FAILURE(ResetResizeImages(src_width, src_height,
    235                                                      dst_width, dst_height,
    236                                                      AOM_BORDER_IN_PIXELS));
    237 
    238            aom_usec_timer ref_timer;
    239            aom_usec_timer_start(&ref_timer);
    240            for (int i = 0; i < kCountSpeedTestBlock; ++i)
    241              av1_resize_and_extend_frame_c(&img_, &ref_img_, filter_type,
    242                                            phase_scaler, 1);
    243            aom_usec_timer_mark(&ref_timer);
    244            const int64_t ref_time = aom_usec_timer_elapsed(&ref_timer);
    245 
    246            aom_usec_timer tst_timer;
    247            aom_usec_timer_start(&tst_timer);
    248            for (int i = 0; i < kCountSpeedTestBlock; ++i)
    249              resize_fn_(&img_, &dst_img_, filter_type, phase_scaler, 1);
    250            aom_usec_timer_mark(&tst_timer);
    251            const int64_t tst_time = aom_usec_timer_elapsed(&tst_timer);
    252            DeallocResizeImages();
    253 
    254            std::cout << "[          ] C time = " << ref_time / 1000
    255                      << " ms, SIMD time = " << tst_time / 1000 << " ms\n";
    256          }
    257        }
    258      }
    259    }
    260  }
    261 
    262  YV12_BUFFER_CONFIG img_;
    263  YV12_BUFFER_CONFIG ref_img_;
    264  YV12_BUFFER_CONFIG dst_img_;
    265  ResizeFrameFunc resize_fn_;
    266 };
    267 
    268 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(ResizeAndExtendTest);
    269 
    270 TEST_P(ResizeAndExtendTest, ResizeFrame_EightTap) { RunTest(EIGHTTAP_REGULAR); }
    271 TEST_P(ResizeAndExtendTest, ResizeFrame_EightTapSmooth) {
    272  RunTest(EIGHTTAP_SMOOTH);
    273 }
    274 TEST_P(ResizeAndExtendTest, ResizeFrame_Bilinear) { RunTest(BILINEAR); }
    275 TEST_P(ResizeAndExtendTest, DISABLED_Speed) { SpeedTest(); }
    276 
    277 // TODO: bug aomedia:363916152 - Enable SSSE3 unit tests when implementation of
    278 // av1_resize_and_extend_frame does not differ from scalar version
    279 #if HAVE_SSSE3
    280 INSTANTIATE_TEST_SUITE_P(DISABLED_SSSE3, ResizeAndExtendTest,
    281                         ::testing::Values(av1_resize_and_extend_frame_ssse3));
    282 #endif  // HAVE_SSSE3
    283 
    284 #if HAVE_NEON
    285 INSTANTIATE_TEST_SUITE_P(NEON, ResizeAndExtendTest,
    286                         ::testing::Values(av1_resize_and_extend_frame_neon));
    287 #endif  // HAVE_NEON
    288 
    289 #if HAVE_NEON_DOTPROD
    290 INSTANTIATE_TEST_SUITE_P(
    291    NEON_DOTPROD, ResizeAndExtendTest,
    292    ::testing::Values(av1_resize_and_extend_frame_neon_dotprod));
    293 
    294 #endif  // HAVE_NEON_DOTPROD
    295 
    296 #if HAVE_NEON_I8MM
    297 INSTANTIATE_TEST_SUITE_P(
    298    NEON_I8MM, ResizeAndExtendTest,
    299    ::testing::Values(av1_resize_and_extend_frame_neon_i8mm));
    300 
    301 #endif  // HAVE_NEON_I8MM
    302 
    303 }  // namespace