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