blob: c3e51fd565d155af0043598038e044c89024c3e5 [file] [log] [blame]
Sebastien Alaiwan14cb8602017-07-11 11:27:19 +02001/*
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 Finegan60e653d2018-05-22 11:34:58 -070016#include "config/aom_config.h"
Sebastien Alaiwan14cb8602017-07-11 11:27:19 +020017
Sebastien Alaiwan14cb8602017-07-11 11:27:19 +020018#include "aom/aomcx.h"
19#include "aom/aomdx.h"
20#include "aom/aom_encoder.h"
21#include "aom/aom_decoder.h"
22
Wan-Teh Chang7ec919d2020-04-25 16:27:19 -070023#define NELEMENTS(x) static_cast<int>(sizeof(x) / sizeof(x[0]))
24
Sebastien Alaiwan14cb8602017-07-11 11:27:19 +020025using libaom_test::ACMRandom;
26namespace {
27
Sebastien Alaiwan15a11222017-07-27 09:32:06 +020028class CompressedSource {
29 public:
30 explicit CompressedSource(int seed) : rnd_(seed), frame_count_(0) {
Ilie Halip0e41f762018-07-16 21:01:43 +030031 aom_codec_iface_t *algo = aom_codec_av1_cx();
Sebastien Alaiwan14cb8602017-07-11 11:27:19 +020032
33 aom_codec_enc_cfg_t cfg;
Jerome Jiang7a3c91f2021-04-16 10:34:49 -070034#if CONFIG_REALTIME_ONLY
35 aom_codec_enc_config_default(algo, &cfg, 1);
36#else
Sebastien Alaiwan14cb8602017-07-11 11:27:19 +020037 aom_codec_enc_config_default(algo, &cfg, 0);
Jerome Jiang7a3c91f2021-04-16 10:34:49 -070038#endif
Sebastien Alaiwan14cb8602017-07-11 11:27:19 +020039
Sebastien Alaiwan403cd012017-08-17 16:33:53 +020040 // 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 Alaiwan14cb8602017-07-11 11:27:19 +020046
Sebastien Alaiwan403cd012017-08-17 16:33:53 +020047 cfg.rc_end_usage = AOM_Q;
48 cfg.rc_max_quantizer = q;
49 cfg.rc_min_quantizer = q;
50 }
Sebastien Alaiwan439b21c2017-08-17 16:28:44 +020051
52 // choose the picture size
53 {
54 width_ = rnd_.PseudoUniform(kWidth - 8) + 8;
55 height_ = rnd_.PseudoUniform(kHeight - 8) + 8;
56 }
57
Sebastien Alaiwan9362a512017-08-17 16:31:58 +020058 // choose the chroma subsampling
59 {
60 const aom_img_fmt_t fmts[] = {
Johannf152ff62018-02-08 14:33:07 -080061 AOM_IMG_FMT_I420,
62 AOM_IMG_FMT_I422,
63 AOM_IMG_FMT_I444,
Sebastien Alaiwan9362a512017-08-17 16:31:58 +020064 };
65
66 format_ = fmts[rnd_.PseudoUniform(NELEMENTS(fmts))];
67 }
68
Sebastien Alaiwan439b21c2017-08-17 16:28:44 +020069 cfg.g_w = width_;
70 cfg.g_h = height_;
Sebastien Alaiwan14cb8602017-07-11 11:27:19 +020071 cfg.g_lag_in_frames = 0;
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -080072 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 Alaiwan14cb8602017-07-11 11:27:19 +020078
79 aom_codec_enc_init(&enc_, algo, &cfg, 0);
80 }
81
82 ~CompressedSource() { aom_codec_destroy(&enc_); }
83
Sebastien Alaiwan15a11222017-07-27 09:32:06 +020084 const aom_codec_cx_pkt_t *ReadFrame() {
Sebastien Alaiwan9362a512017-08-17 16:31:58 +020085 uint8_t buf[kWidth * kHeight * 3] = { 0 };
Sebastien Alaiwan14cb8602017-07-11 11:27:19 +020086
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 Alaiwan15a11222017-07-27 09:32:06 +020093
Sebastien Alaiwan14cb8602017-07-11 11:27:19 +020094 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 Alaiwan9362a512017-08-17 16:31:58 +020098 aom_img_wrap(&img, format_, width_, height_, 0, buf);
Sean DuBois47cc2552018-01-23 07:44:16 +000099 aom_codec_encode(&enc_, &img, frame_count_++, 1, 0);
Sebastien Alaiwan14cb8602017-07-11 11:27:19 +0200100
James Zern664f04d2022-05-24 17:30:58 -0700101 aom_codec_iter_t iter = nullptr;
Angie Chiang7496d662017-08-08 11:30:46 -0700102
James Zern664f04d2022-05-24 17:30:58 -0700103 const aom_codec_cx_pkt_t *pkt = nullptr;
Angie Chiang7496d662017-08-08 11:30:46 -0700104
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 Alaiwan14cb8602017-07-11 11:27:19 +0200110 }
111
112 private:
Sebastien Alaiwan439b21c2017-08-17 16:28:44 +0200113 static const int kWidth = 128;
114 static const int kHeight = 128;
Sebastien Alaiwan15a11222017-07-27 09:32:06 +0200115
Sebastien Alaiwan14cb8602017-07-11 11:27:19 +0200116 ACMRandom rnd_;
Sebastien Alaiwan9362a512017-08-17 16:31:58 +0200117 aom_img_fmt_t format_;
Sebastien Alaiwan14cb8602017-07-11 11:27:19 +0200118 aom_codec_ctx_t enc_;
119 int frame_count_;
Sebastien Alaiwan439b21c2017-08-17 16:28:44 +0200120 int width_, height_;
Sebastien Alaiwan14cb8602017-07-11 11:27:19 +0200121};
122
123// lowers an aom_image_t to a easily comparable/printable form
Sebastien Alaiwan15a11222017-07-27 09:32:06 +0200124std::vector<int16_t> Serialize(const aom_image_t *img) {
Sebastien Alaiwan14cb8602017-07-11 11:27:19 +0200125 std::vector<int16_t> bytes;
126 bytes.reserve(img->d_w * img->d_h * 3);
Sebastien Alaiwan15a11222017-07-27 09:32:06 +0200127 for (int plane = 0; plane < 3; ++plane) {
Sebastien Alaiwanaeb1e482017-09-21 11:36:56 +0200128 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 Alaiwana85a2b92017-09-21 11:45:26 +0200133 unsigned char *row = img->planes[plane] + r * img->stride[plane];
Sebastien Alaiwan14cb8602017-07-11 11:27:19 +0200134 if (img->fmt & AOM_IMG_FMT_HIGHBITDEPTH)
Sebastien Alaiwana85a2b92017-09-21 11:45:26 +0200135 bytes.push_back(row[c * 2]);
Sebastien Alaiwan14cb8602017-07-11 11:27:19 +0200136 else
Sebastien Alaiwana85a2b92017-09-21 11:45:26 +0200137 bytes.push_back(row[c]);
Sebastien Alaiwan14cb8602017-07-11 11:27:19 +0200138 }
Sebastien Alaiwan15a11222017-07-27 09:32:06 +0200139 }
140 }
Sebastien Alaiwan14cb8602017-07-11 11:27:19 +0200141
142 return bytes;
143}
144
Sebastien Alaiwan15a11222017-07-27 09:32:06 +0200145class Decoder {
146 public:
Sebastien Alaiwan14cb8602017-07-11 11:27:19 +0200147 explicit Decoder(int allowLowbitdepth) {
Ilie Halip0e41f762018-07-16 21:01:43 +0300148 aom_codec_iface_t *algo = aom_codec_av1_dx();
Sebastien Alaiwan14cb8602017-07-11 11:27:19 +0200149
James Zern78e5ecf2017-08-15 16:45:58 -0700150 aom_codec_dec_cfg_t cfg = aom_codec_dec_cfg_t();
Sebastien Alaiwan14cb8602017-07-11 11:27:19 +0200151 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 Alaiwan15a11222017-07-27 09:32:06 +0200159 aom_codec_decode(&dec_, static_cast<uint8_t *>(pkt->data.frame.buf),
James Zern664f04d2022-05-24 17:30:58 -0700160 pkt->data.frame.sz, nullptr);
Sebastien Alaiwan14cb8602017-07-11 11:27:19 +0200161
James Zern664f04d2022-05-24 17:30:58 -0700162 aom_codec_iter_t iter = nullptr;
Sebastien Alaiwan15a11222017-07-27 09:32:06 +0200163 return Serialize(aom_codec_get_frame(&dec_, &iter));
Sebastien Alaiwan14cb8602017-07-11 11:27:19 +0200164 }
165
166 private:
167 aom_codec_ctx_t dec_;
168};
169
170// Try to reveal a mismatch between LBD and HBD coding paths.
171TEST(CodingPathSync, SearchForHbdLbdMismatch) {
Sebastien Alaiwandd4f0fa2017-10-12 15:53:15 +0200172 const int count_tests = 10;
Sebastien Alaiwan14cb8602017-07-11 11:27:19 +0200173 for (int i = 0; i < count_tests; ++i) {
Sebastien Alaiwan15a11222017-07-27 09:32:06 +0200174 Decoder dec_hbd(0);
175 Decoder dec_lbd(1);
Sebastien Alaiwan14cb8602017-07-11 11:27:19 +0200176
177 CompressedSource enc(i);
Sebastien Alaiwan67876e72017-08-10 10:58:31 +0200178
Sebastien Alaiwandd4f0fa2017-10-12 15:53:15 +0200179 for (int k = 0; k < 3; ++k) {
180 const aom_codec_cx_pkt_t *frame = enc.ReadFrame();
Sebastien Alaiwan67876e72017-08-10 10:58:31 +0200181
Sebastien Alaiwandd4f0fa2017-10-12 15:53:15 +0200182 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
190TEST(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 Alaiwan14cb8602017-07-11 11:27:19 +0200207 }
208}
209
210} // namespace