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 #if CONFIG_REALTIME_ONLY
35 aom_codec_enc_config_default(algo, &cfg, 1);
36 #else
37 aom_codec_enc_config_default(algo, &cfg, 0);
38 #endif
39
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;
46
47 cfg.rc_end_usage = AOM_Q;
48 cfg.rc_max_quantizer = q;
49 cfg.rc_min_quantizer = q;
50 }
51
52 // choose the picture size
53 {
54 width_ = rnd_.PseudoUniform(kWidth - 8) + 8;
55 height_ = rnd_.PseudoUniform(kHeight - 8) + 8;
56 }
57
58 // choose the chroma subsampling
59 {
60 const aom_img_fmt_t fmts[] = {
61 AOM_IMG_FMT_I420,
62 AOM_IMG_FMT_I422,
63 AOM_IMG_FMT_I444,
64 };
65
66 format_ = fmts[rnd_.PseudoUniform(NELEMENTS(fmts))];
67 }
68
69 cfg.g_w = width_;
70 cfg.g_h = height_;
71 cfg.g_lag_in_frames = 0;
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;
78
79 aom_codec_enc_init(&enc_, algo, &cfg, 0);
80 }
81
~CompressedSource()82 ~CompressedSource() { aom_codec_destroy(&enc_); }
83
ReadFrame()84 const aom_codec_cx_pkt_t *ReadFrame() {
85 uint8_t buf[kWidth * kHeight * 3] = { 0 };
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();
93
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;
98 aom_img_wrap(&img, format_, width_, height_, 0, buf);
99 aom_codec_encode(&enc_, &img, frame_count_++, 1, 0);
100
101 aom_codec_iter_t iter = NULL;
102
103 const aom_codec_cx_pkt_t *pkt = NULL;
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;
110 }
111
112 private:
113 static const int kWidth = 128;
114 static const int kHeight = 128;
115
116 ACMRandom rnd_;
117 aom_img_fmt_t format_;
118 aom_codec_ctx_t enc_;
119 int frame_count_;
120 int width_, height_;
121 };
122
123 // lowers an aom_image_t to a easily comparable/printable form
Serialize(const aom_image_t * img)124 std::vector<int16_t> Serialize(const aom_image_t *img) {
125 std::vector<int16_t> bytes;
126 bytes.reserve(img->d_w * img->d_h * 3);
127 for (int plane = 0; plane < 3; ++plane) {
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) {
133 unsigned char *row = img->planes[plane] + r * img->stride[plane];
134 if (img->fmt & AOM_IMG_FMT_HIGHBITDEPTH)
135 bytes.push_back(row[c * 2]);
136 else
137 bytes.push_back(row[c]);
138 }
139 }
140 }
141
142 return bytes;
143 }
144
145 class Decoder {
146 public:
Decoder(int allowLowbitdepth)147 explicit Decoder(int allowLowbitdepth) {
148 aom_codec_iface_t *algo = aom_codec_av1_dx();
149
150 aom_codec_dec_cfg_t cfg = aom_codec_dec_cfg_t();
151 cfg.allow_lowbitdepth = allowLowbitdepth;
152
153 aom_codec_dec_init(&dec_, algo, &cfg, 0);
154 }
155
~Decoder()156 ~Decoder() { aom_codec_destroy(&dec_); }
157
decode(const aom_codec_cx_pkt_t * pkt)158 std::vector<int16_t> decode(const aom_codec_cx_pkt_t *pkt) {
159 aom_codec_decode(&dec_, static_cast<uint8_t *>(pkt->data.frame.buf),
160 pkt->data.frame.sz, NULL);
161
162 aom_codec_iter_t iter = NULL;
163 return Serialize(aom_codec_get_frame(&dec_, &iter));
164 }
165
166 private:
167 aom_codec_ctx_t dec_;
168 };
169
170 // Try to reveal a mismatch between LBD and HBD coding paths.
TEST(CodingPathSync,SearchForHbdLbdMismatch)171 TEST(CodingPathSync, SearchForHbdLbdMismatch) {
172 const int count_tests = 10;
173 for (int i = 0; i < count_tests; ++i) {
174 Decoder dec_hbd(0);
175 Decoder dec_lbd(1);
176
177 CompressedSource enc(i);
178
179 for (int k = 0; k < 3; ++k) {
180 const aom_codec_cx_pkt_t *frame = enc.ReadFrame();
181
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
TEST(CodingPathSyncLarge,SearchForHbdLbdMismatchLarge)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 }
207 }
208 }
209
210 } // namespace
211