Sebastien Alaiwan | 14cb860 | 2017-07-11 11:27:19 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016, 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 <vector> |
| 13 | #include "third_party/googletest/src/googletest/include/gtest/gtest.h" |
| 14 | #include "test/acm_random.h" |
| 15 | |
Tom Finegan | 60e653d | 2018-05-22 11:34:58 -0700 | [diff] [blame] | 16 | #include "config/aom_config.h" |
Sebastien Alaiwan | 14cb860 | 2017-07-11 11:27:19 +0200 | [diff] [blame] | 17 | |
Sebastien Alaiwan | 14cb860 | 2017-07-11 11:27:19 +0200 | [diff] [blame] | 18 | #include "aom/aomcx.h" |
| 19 | #include "aom/aomdx.h" |
| 20 | #include "aom/aom_encoder.h" |
| 21 | #include "aom/aom_decoder.h" |
| 22 | |
Wan-Teh Chang | 7ec919d | 2020-04-25 16:27:19 -0700 | [diff] [blame] | 23 | #define NELEMENTS(x) static_cast<int>(sizeof(x) / sizeof(x[0])) |
| 24 | |
Sebastien Alaiwan | 14cb860 | 2017-07-11 11:27:19 +0200 | [diff] [blame] | 25 | using libaom_test::ACMRandom; |
| 26 | namespace { |
| 27 | |
Sebastien Alaiwan | 15a1122 | 2017-07-27 09:32:06 +0200 | [diff] [blame] | 28 | class CompressedSource { |
| 29 | public: |
| 30 | explicit CompressedSource(int seed) : rnd_(seed), frame_count_(0) { |
Ilie Halip | 0e41f76 | 2018-07-16 21:01:43 +0300 | [diff] [blame] | 31 | aom_codec_iface_t *algo = aom_codec_av1_cx(); |
Sebastien Alaiwan | 14cb860 | 2017-07-11 11:27:19 +0200 | [diff] [blame] | 32 | |
| 33 | aom_codec_enc_cfg_t cfg; |
Jerome Jiang | 7a3c91f | 2021-04-16 10:34:49 -0700 | [diff] [blame] | 34 | #if CONFIG_REALTIME_ONLY |
| 35 | aom_codec_enc_config_default(algo, &cfg, 1); |
| 36 | #else |
Sebastien Alaiwan | 14cb860 | 2017-07-11 11:27:19 +0200 | [diff] [blame] | 37 | aom_codec_enc_config_default(algo, &cfg, 0); |
Jerome Jiang | 7a3c91f | 2021-04-16 10:34:49 -0700 | [diff] [blame] | 38 | #endif |
Sebastien Alaiwan | 14cb860 | 2017-07-11 11:27:19 +0200 | [diff] [blame] | 39 | |
Sebastien Alaiwan | 403cd01 | 2017-08-17 16:33:53 +0200 | [diff] [blame] | 40 | // force the quantizer, to reduce the sensitivity on encoding choices. |
| 41 | // e.g, we don't want this test to break when the rate control is modified. |
| 42 | { |
| 43 | const int max_q = cfg.rc_max_quantizer; |
| 44 | const int min_q = cfg.rc_min_quantizer; |
| 45 | const int q = rnd_.PseudoUniform(max_q - min_q + 1) + min_q; |
Sebastien Alaiwan | 14cb860 | 2017-07-11 11:27:19 +0200 | [diff] [blame] | 46 | |
Sebastien Alaiwan | 403cd01 | 2017-08-17 16:33:53 +0200 | [diff] [blame] | 47 | cfg.rc_end_usage = AOM_Q; |
| 48 | cfg.rc_max_quantizer = q; |
| 49 | cfg.rc_min_quantizer = q; |
| 50 | } |
Sebastien Alaiwan | 439b21c | 2017-08-17 16:28:44 +0200 | [diff] [blame] | 51 | |
| 52 | // choose the picture size |
| 53 | { |
| 54 | width_ = rnd_.PseudoUniform(kWidth - 8) + 8; |
| 55 | height_ = rnd_.PseudoUniform(kHeight - 8) + 8; |
| 56 | } |
| 57 | |
Sebastien Alaiwan | 9362a51 | 2017-08-17 16:31:58 +0200 | [diff] [blame] | 58 | // choose the chroma subsampling |
| 59 | { |
| 60 | const aom_img_fmt_t fmts[] = { |
Johann | f152ff6 | 2018-02-08 14:33:07 -0800 | [diff] [blame] | 61 | AOM_IMG_FMT_I420, |
| 62 | AOM_IMG_FMT_I422, |
| 63 | AOM_IMG_FMT_I444, |
Sebastien Alaiwan | 9362a51 | 2017-08-17 16:31:58 +0200 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | format_ = fmts[rnd_.PseudoUniform(NELEMENTS(fmts))]; |
| 67 | } |
| 68 | |
Sebastien Alaiwan | 439b21c | 2017-08-17 16:28:44 +0200 | [diff] [blame] | 69 | cfg.g_w = width_; |
| 70 | cfg.g_h = height_; |
Sebastien Alaiwan | 14cb860 | 2017-07-11 11:27:19 +0200 | [diff] [blame] | 71 | cfg.g_lag_in_frames = 0; |
Debargha Mukherjee | f9a50ea | 2018-01-09 22:28:20 -0800 | [diff] [blame] | 72 | if (format_ == AOM_IMG_FMT_I420) |
| 73 | cfg.g_profile = 0; |
| 74 | else if (format_ == AOM_IMG_FMT_I444) |
| 75 | cfg.g_profile = 1; |
| 76 | else if (format_ == AOM_IMG_FMT_I422) |
| 77 | cfg.g_profile = 2; |
Sebastien Alaiwan | 14cb860 | 2017-07-11 11:27:19 +0200 | [diff] [blame] | 78 | |
| 79 | aom_codec_enc_init(&enc_, algo, &cfg, 0); |
| 80 | } |
| 81 | |
| 82 | ~CompressedSource() { aom_codec_destroy(&enc_); } |
| 83 | |
Sebastien Alaiwan | 15a1122 | 2017-07-27 09:32:06 +0200 | [diff] [blame] | 84 | const aom_codec_cx_pkt_t *ReadFrame() { |
Sebastien Alaiwan | 9362a51 | 2017-08-17 16:31:58 +0200 | [diff] [blame] | 85 | uint8_t buf[kWidth * kHeight * 3] = { 0 }; |
Sebastien Alaiwan | 14cb860 | 2017-07-11 11:27:19 +0200 | [diff] [blame] | 86 | |
| 87 | // render regular pattern |
| 88 | const int period = rnd_.Rand8() % 32 + 1; |
| 89 | const int phase = rnd_.Rand8() % period; |
| 90 | |
| 91 | const int val_a = rnd_.Rand8(); |
| 92 | const int val_b = rnd_.Rand8(); |
Sebastien Alaiwan | 15a1122 | 2017-07-27 09:32:06 +0200 | [diff] [blame] | 93 | |
Sebastien Alaiwan | 14cb860 | 2017-07-11 11:27:19 +0200 | [diff] [blame] | 94 | for (int i = 0; i < (int)sizeof buf; ++i) |
| 95 | buf[i] = (i + phase) % period < period / 2 ? val_a : val_b; |
| 96 | |
| 97 | aom_image_t img; |
Sebastien Alaiwan | 9362a51 | 2017-08-17 16:31:58 +0200 | [diff] [blame] | 98 | aom_img_wrap(&img, format_, width_, height_, 0, buf); |
Sean DuBois | 47cc255 | 2018-01-23 07:44:16 +0000 | [diff] [blame] | 99 | aom_codec_encode(&enc_, &img, frame_count_++, 1, 0); |
Sebastien Alaiwan | 14cb860 | 2017-07-11 11:27:19 +0200 | [diff] [blame] | 100 | |
James Zern | 664f04d | 2022-05-24 17:30:58 -0700 | [diff] [blame] | 101 | aom_codec_iter_t iter = nullptr; |
Angie Chiang | 7496d66 | 2017-08-08 11:30:46 -0700 | [diff] [blame] | 102 | |
James Zern | 664f04d | 2022-05-24 17:30:58 -0700 | [diff] [blame] | 103 | const aom_codec_cx_pkt_t *pkt = nullptr; |
Angie Chiang | 7496d66 | 2017-08-08 11:30:46 -0700 | [diff] [blame] | 104 | |
| 105 | do { |
| 106 | pkt = aom_codec_get_cx_data(&enc_, &iter); |
| 107 | } while (pkt && pkt->kind != AOM_CODEC_CX_FRAME_PKT); |
| 108 | |
| 109 | return pkt; |
Sebastien Alaiwan | 14cb860 | 2017-07-11 11:27:19 +0200 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | private: |
Sebastien Alaiwan | 439b21c | 2017-08-17 16:28:44 +0200 | [diff] [blame] | 113 | static const int kWidth = 128; |
| 114 | static const int kHeight = 128; |
Sebastien Alaiwan | 15a1122 | 2017-07-27 09:32:06 +0200 | [diff] [blame] | 115 | |
Sebastien Alaiwan | 14cb860 | 2017-07-11 11:27:19 +0200 | [diff] [blame] | 116 | ACMRandom rnd_; |
Sebastien Alaiwan | 9362a51 | 2017-08-17 16:31:58 +0200 | [diff] [blame] | 117 | aom_img_fmt_t format_; |
Sebastien Alaiwan | 14cb860 | 2017-07-11 11:27:19 +0200 | [diff] [blame] | 118 | aom_codec_ctx_t enc_; |
| 119 | int frame_count_; |
Sebastien Alaiwan | 439b21c | 2017-08-17 16:28:44 +0200 | [diff] [blame] | 120 | int width_, height_; |
Sebastien Alaiwan | 14cb860 | 2017-07-11 11:27:19 +0200 | [diff] [blame] | 121 | }; |
| 122 | |
| 123 | // lowers an aom_image_t to a easily comparable/printable form |
Sebastien Alaiwan | 15a1122 | 2017-07-27 09:32:06 +0200 | [diff] [blame] | 124 | std::vector<int16_t> Serialize(const aom_image_t *img) { |
Sebastien Alaiwan | 14cb860 | 2017-07-11 11:27:19 +0200 | [diff] [blame] | 125 | std::vector<int16_t> bytes; |
| 126 | bytes.reserve(img->d_w * img->d_h * 3); |
Sebastien Alaiwan | 15a1122 | 2017-07-27 09:32:06 +0200 | [diff] [blame] | 127 | for (int plane = 0; plane < 3; ++plane) { |
Sebastien Alaiwan | aeb1e48 | 2017-09-21 11:36:56 +0200 | [diff] [blame] | 128 | const int w = aom_img_plane_width(img, plane); |
| 129 | const int h = aom_img_plane_height(img, plane); |
| 130 | |
| 131 | for (int r = 0; r < h; ++r) { |
| 132 | for (int c = 0; c < w; ++c) { |
Sebastien Alaiwan | a85a2b9 | 2017-09-21 11:45:26 +0200 | [diff] [blame] | 133 | unsigned char *row = img->planes[plane] + r * img->stride[plane]; |
Sebastien Alaiwan | 14cb860 | 2017-07-11 11:27:19 +0200 | [diff] [blame] | 134 | if (img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) |
Sebastien Alaiwan | a85a2b9 | 2017-09-21 11:45:26 +0200 | [diff] [blame] | 135 | bytes.push_back(row[c * 2]); |
Sebastien Alaiwan | 14cb860 | 2017-07-11 11:27:19 +0200 | [diff] [blame] | 136 | else |
Sebastien Alaiwan | a85a2b9 | 2017-09-21 11:45:26 +0200 | [diff] [blame] | 137 | bytes.push_back(row[c]); |
Sebastien Alaiwan | 14cb860 | 2017-07-11 11:27:19 +0200 | [diff] [blame] | 138 | } |
Sebastien Alaiwan | 15a1122 | 2017-07-27 09:32:06 +0200 | [diff] [blame] | 139 | } |
| 140 | } |
Sebastien Alaiwan | 14cb860 | 2017-07-11 11:27:19 +0200 | [diff] [blame] | 141 | |
| 142 | return bytes; |
| 143 | } |
| 144 | |
Sebastien Alaiwan | 15a1122 | 2017-07-27 09:32:06 +0200 | [diff] [blame] | 145 | class Decoder { |
| 146 | public: |
Sebastien Alaiwan | 14cb860 | 2017-07-11 11:27:19 +0200 | [diff] [blame] | 147 | explicit Decoder(int allowLowbitdepth) { |
Ilie Halip | 0e41f76 | 2018-07-16 21:01:43 +0300 | [diff] [blame] | 148 | aom_codec_iface_t *algo = aom_codec_av1_dx(); |
Sebastien Alaiwan | 14cb860 | 2017-07-11 11:27:19 +0200 | [diff] [blame] | 149 | |
James Zern | 78e5ecf | 2017-08-15 16:45:58 -0700 | [diff] [blame] | 150 | aom_codec_dec_cfg_t cfg = aom_codec_dec_cfg_t(); |
Sebastien Alaiwan | 14cb860 | 2017-07-11 11:27:19 +0200 | [diff] [blame] | 151 | cfg.allow_lowbitdepth = allowLowbitdepth; |
| 152 | |
| 153 | aom_codec_dec_init(&dec_, algo, &cfg, 0); |
| 154 | } |
| 155 | |
| 156 | ~Decoder() { aom_codec_destroy(&dec_); } |
| 157 | |
| 158 | std::vector<int16_t> decode(const aom_codec_cx_pkt_t *pkt) { |
Sebastien Alaiwan | 15a1122 | 2017-07-27 09:32:06 +0200 | [diff] [blame] | 159 | aom_codec_decode(&dec_, static_cast<uint8_t *>(pkt->data.frame.buf), |
James Zern | 664f04d | 2022-05-24 17:30:58 -0700 | [diff] [blame] | 160 | pkt->data.frame.sz, nullptr); |
Sebastien Alaiwan | 14cb860 | 2017-07-11 11:27:19 +0200 | [diff] [blame] | 161 | |
James Zern | 664f04d | 2022-05-24 17:30:58 -0700 | [diff] [blame] | 162 | aom_codec_iter_t iter = nullptr; |
Sebastien Alaiwan | 15a1122 | 2017-07-27 09:32:06 +0200 | [diff] [blame] | 163 | return Serialize(aom_codec_get_frame(&dec_, &iter)); |
Sebastien Alaiwan | 14cb860 | 2017-07-11 11:27:19 +0200 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | private: |
| 167 | aom_codec_ctx_t dec_; |
| 168 | }; |
| 169 | |
| 170 | // Try to reveal a mismatch between LBD and HBD coding paths. |
| 171 | TEST(CodingPathSync, SearchForHbdLbdMismatch) { |
Sebastien Alaiwan | dd4f0fa | 2017-10-12 15:53:15 +0200 | [diff] [blame] | 172 | const int count_tests = 10; |
Sebastien Alaiwan | 14cb860 | 2017-07-11 11:27:19 +0200 | [diff] [blame] | 173 | for (int i = 0; i < count_tests; ++i) { |
Sebastien Alaiwan | 15a1122 | 2017-07-27 09:32:06 +0200 | [diff] [blame] | 174 | Decoder dec_hbd(0); |
| 175 | Decoder dec_lbd(1); |
Sebastien Alaiwan | 14cb860 | 2017-07-11 11:27:19 +0200 | [diff] [blame] | 176 | |
| 177 | CompressedSource enc(i); |
Sebastien Alaiwan | 67876e7 | 2017-08-10 10:58:31 +0200 | [diff] [blame] | 178 | |
Sebastien Alaiwan | dd4f0fa | 2017-10-12 15:53:15 +0200 | [diff] [blame] | 179 | for (int k = 0; k < 3; ++k) { |
| 180 | const aom_codec_cx_pkt_t *frame = enc.ReadFrame(); |
Sebastien Alaiwan | 67876e7 | 2017-08-10 10:58:31 +0200 | [diff] [blame] | 181 | |
Sebastien Alaiwan | dd4f0fa | 2017-10-12 15:53:15 +0200 | [diff] [blame] | 182 | std::vector<int16_t> lbd_yuv = dec_lbd.decode(frame); |
| 183 | std::vector<int16_t> hbd_yuv = dec_hbd.decode(frame); |
| 184 | |
| 185 | ASSERT_EQ(lbd_yuv, hbd_yuv); |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | TEST(CodingPathSyncLarge, SearchForHbdLbdMismatchLarge) { |
| 191 | const int count_tests = 100; |
| 192 | const int seed = 1234; |
| 193 | for (int i = 0; i < count_tests; ++i) { |
| 194 | Decoder dec_hbd(0); |
| 195 | Decoder dec_lbd(1); |
| 196 | |
| 197 | CompressedSource enc(seed + i); |
| 198 | |
| 199 | for (int k = 0; k < 5; ++k) { |
| 200 | const aom_codec_cx_pkt_t *frame = enc.ReadFrame(); |
| 201 | |
| 202 | std::vector<int16_t> lbd_yuv = dec_lbd.decode(frame); |
| 203 | std::vector<int16_t> hbd_yuv = dec_hbd.decode(frame); |
| 204 | |
| 205 | ASSERT_EQ(lbd_yuv, hbd_yuv); |
| 206 | } |
Sebastien Alaiwan | 14cb860 | 2017-07-11 11:27:19 +0200 | [diff] [blame] | 207 | } |
| 208 | } |
| 209 | |
| 210 | } // namespace |