Linfeng Zhang | 0ba23e8 | 2017-12-20 16:27:28 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017, Alliance for Open Media. All rights reserved |
| 3 | * |
| 4 | * This source code is subject to the terms of the BSD 2 Clause License and |
| 5 | * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License |
| 6 | * was not distributed with this source code in the LICENSE file, you can |
| 7 | * obtain it at www.aomedia.org/license/software. If the Alliance for Open |
| 8 | * Media Patent License 1.0 was not distributed with this source code in the |
| 9 | * PATENTS file, you can obtain it at www.aomedia.org/license/patent. |
| 10 | */ |
| 11 | |
| 12 | #include <stdint.h> |
| 13 | #include <stdio.h> |
| 14 | #include <string.h> |
| 15 | |
| 16 | #include "third_party/googletest/src/googletest/include/gtest/gtest.h" |
| 17 | |
| 18 | #include "./aom_config.h" |
| 19 | #include "./av1_rtcd.h" |
| 20 | #include "aom_ports/aom_timer.h" |
| 21 | #include "aom_ports/mem.h" |
| 22 | #include "av1/common/idct.h" |
| 23 | #include "av1/common/onyxc_int.h" |
| 24 | #include "av1/common/scan.h" |
| 25 | #include "av1/common/txb_common.h" |
Linfeng Zhang | 0ba23e8 | 2017-12-20 16:27:28 -0800 | [diff] [blame] | 26 | #include "test/acm_random.h" |
| 27 | #include "test/clear_system_state.h" |
| 28 | #include "test/register_state_check.h" |
| 29 | #include "test/util.h" |
| 30 | |
| 31 | namespace { |
| 32 | using libaom_test::ACMRandom; |
| 33 | |
| 34 | typedef void (*GetNzMapContextsFunc)(const uint8_t *const levels, |
| 35 | const int16_t *const scan, |
| 36 | const uint16_t eob, const TX_SIZE tx_size, |
| 37 | const TX_TYPE tx_type, |
| 38 | int8_t *const coeff_contexts); |
| 39 | |
| 40 | class EncodeTxbTest : public ::testing::TestWithParam<GetNzMapContextsFunc> { |
| 41 | public: |
| 42 | EncodeTxbTest() : get_nz_map_contexts_func_(GetParam()) {} |
| 43 | |
| 44 | virtual ~EncodeTxbTest() {} |
| 45 | |
| 46 | virtual void SetUp() { |
| 47 | coeff_contexts_ref_ = reinterpret_cast<int8_t *>( |
| 48 | aom_memalign(16, sizeof(*coeff_contexts_ref_) * MAX_TX_SQUARE)); |
| 49 | ASSERT_TRUE(coeff_contexts_ref_ != NULL); |
| 50 | coeff_contexts_ = reinterpret_cast<int8_t *>( |
| 51 | aom_memalign(16, sizeof(*coeff_contexts_) * MAX_TX_SQUARE)); |
| 52 | ASSERT_TRUE(coeff_contexts_ != NULL); |
| 53 | } |
| 54 | |
| 55 | virtual void TearDown() { |
| 56 | aom_free(coeff_contexts_ref_); |
| 57 | aom_free(coeff_contexts_); |
| 58 | libaom_test::ClearSystemState(); |
| 59 | } |
| 60 | |
| 61 | void GetNzMapContextsRun() { |
| 62 | const int kNumTests = 10; |
| 63 | int result = 0; |
| 64 | |
| 65 | for (int is_inter = 0; is_inter < 2; ++is_inter) { |
| 66 | for (int tx_type = DCT_DCT; tx_type < TX_TYPES; ++tx_type) { |
| 67 | for (int tx_size = TX_4X4; tx_size < TX_SIZES_ALL; ++tx_size) { |
| 68 | const int bwl = get_txb_bwl((TX_SIZE)tx_size); |
| 69 | const int width = get_txb_wide((TX_SIZE)tx_size); |
| 70 | const int height = get_txb_high((TX_SIZE)tx_size); |
| 71 | const int real_width = tx_size_wide[tx_size]; |
| 72 | const int real_height = tx_size_high[tx_size]; |
| 73 | const int16_t *const scan = |
| 74 | is_inter ? av1_inter_scan_orders[tx_size][tx_type].scan |
| 75 | : av1_intra_scan_orders[tx_size][tx_type].scan; |
| 76 | |
| 77 | levels_ = set_levels(levels_buf_, width); |
| 78 | for (int i = 0; i < kNumTests && !result; ++i) { |
| 79 | for (int eob = 1; eob <= width * height && !result; ++eob) { |
| 80 | InitDataWithEob(scan, bwl, eob); |
| 81 | |
| 82 | av1_get_nz_map_contexts_c(levels_, scan, eob, (TX_SIZE)tx_size, |
| 83 | (TX_TYPE)tx_type, coeff_contexts_ref_); |
| 84 | get_nz_map_contexts_func_(levels_, scan, eob, (TX_SIZE)tx_size, |
| 85 | (TX_TYPE)tx_type, coeff_contexts_); |
| 86 | |
| 87 | result = Compare(scan, eob); |
| 88 | |
| 89 | EXPECT_EQ(result, 0) |
| 90 | << " tx_class " << tx_type_to_class[tx_type] << " width " |
| 91 | << real_width << " height " << real_height << " eob " << eob; |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | void SpeedTestGetNzMapContextsRun() { |
| 100 | const int kNumTests = 2000000000; |
| 101 | aom_usec_timer timer; |
| 102 | |
| 103 | printf("Note: Only test the largest possible eob case!\n"); |
| 104 | for (int tx_size = TX_4X4; tx_size < TX_SIZES_ALL; ++tx_size) { |
| 105 | const int bwl = get_txb_bwl((TX_SIZE)tx_size); |
| 106 | const int width = get_txb_wide((TX_SIZE)tx_size); |
| 107 | const int height = get_txb_high((TX_SIZE)tx_size); |
| 108 | const int real_width = tx_size_wide[tx_size]; |
| 109 | const int real_height = tx_size_high[tx_size]; |
| 110 | const TX_TYPE tx_type = DCT_DCT; |
| 111 | const int16_t *const scan = av1_inter_scan_orders[tx_size][tx_type].scan; |
| 112 | const int eob = width * height; |
| 113 | const int numTests = kNumTests / (width * height); |
| 114 | |
| 115 | levels_ = set_levels(levels_buf_, width); |
| 116 | InitDataWithEob(scan, bwl, eob); |
| 117 | |
| 118 | aom_usec_timer_start(&timer); |
| 119 | for (int i = 0; i < numTests; ++i) { |
| 120 | get_nz_map_contexts_func_(levels_, scan, eob, (TX_SIZE)tx_size, tx_type, |
| 121 | coeff_contexts_); |
| 122 | } |
| 123 | aom_usec_timer_mark(&timer); |
| 124 | |
| 125 | const int elapsed_time = static_cast<int>(aom_usec_timer_elapsed(&timer)); |
| 126 | printf("get_nz_map_contexts_%2dx%2d: %7.1f ms\n", real_width, real_height, |
| 127 | elapsed_time / 1000.0); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | private: |
| 132 | void InitDataWithEob(const int16_t *const scan, const int bwl, |
| 133 | const int eob) { |
| 134 | memset(levels_buf_, 0, sizeof(levels_buf_)); |
| 135 | memset(coeff_contexts_, 0, sizeof(*coeff_contexts_) * MAX_TX_SQUARE); |
| 136 | |
| 137 | for (int c = 0; c < eob; ++c) { |
| 138 | levels_[get_padded_idx(scan[c], bwl)] = |
| 139 | static_cast<uint8_t>(clamp(rnd_.Rand8(), 0, INT8_MAX)); |
| 140 | coeff_contexts_[scan[c]] = rnd_.Rand16() >> 1; |
| 141 | } |
| 142 | |
| 143 | memcpy(coeff_contexts_ref_, coeff_contexts_, |
| 144 | sizeof(*coeff_contexts_) * MAX_TX_SQUARE); |
| 145 | } |
| 146 | |
| 147 | bool Compare(const int16_t *const scan, const int eob) const { |
| 148 | bool result = false; |
| 149 | if (memcmp(coeff_contexts_, coeff_contexts_ref_, |
| 150 | sizeof(*coeff_contexts_ref_) * MAX_TX_SQUARE)) { |
| 151 | for (int i = 0; i < eob; i++) { |
| 152 | const int pos = scan[i]; |
| 153 | if (coeff_contexts_ref_[pos] != coeff_contexts_[pos]) { |
| 154 | printf("coeff_contexts_[%d] diff:%6d (ref),%6d (opt)\n", pos, |
| 155 | coeff_contexts_ref_[pos], coeff_contexts_[pos]); |
| 156 | result = true; |
| 157 | break; |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | return result; |
| 162 | } |
| 163 | |
| 164 | GetNzMapContextsFunc get_nz_map_contexts_func_; |
| 165 | ACMRandom rnd_; |
| 166 | uint8_t levels_buf_[TX_PAD_2D]; |
| 167 | uint8_t *levels_; |
| 168 | int8_t *coeff_contexts_ref_; |
| 169 | int8_t *coeff_contexts_; |
| 170 | }; |
| 171 | |
| 172 | TEST_P(EncodeTxbTest, GetNzMapContexts) { GetNzMapContextsRun(); } |
| 173 | |
| 174 | TEST_P(EncodeTxbTest, DISABLED_SpeedTestGetNzMapContexts) { |
| 175 | SpeedTestGetNzMapContextsRun(); |
| 176 | } |
| 177 | |
| 178 | #if HAVE_SSE2 |
| 179 | INSTANTIATE_TEST_CASE_P(SSE2, EncodeTxbTest, |
| 180 | ::testing::Values(av1_get_nz_map_contexts_sse2)); |
| 181 | #endif |
| 182 | } // namespace |