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