blob: 3826aa95f683222c4a917496d416a38b87ce6cd8 [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
16#include "./aom_config.h"
17
Sebastien Alaiwan14cb8602017-07-11 11:27:19 +020018#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
24using libaom_test::ACMRandom;
25namespace {
26
Sebastien Alaiwan15a11222017-07-27 09:32:06 +020027class CompressedSource {
28 public:
29 explicit CompressedSource(int seed) : rnd_(seed), frame_count_(0) {
Sebastien Alaiwan14cb8602017-07-11 11:27:19 +020030 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 Alaiwan403cd012017-08-17 16:33:53 +020035 // 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 Alaiwan14cb8602017-07-11 11:27:19 +020041
Sebastien Alaiwan403cd012017-08-17 16:33:53 +020042 cfg.rc_end_usage = AOM_Q;
43 cfg.rc_max_quantizer = q;
44 cfg.rc_min_quantizer = q;
45 }
Sebastien Alaiwan439b21c2017-08-17 16:28:44 +020046
47 // choose the picture size
48 {
49 width_ = rnd_.PseudoUniform(kWidth - 8) + 8;
50 height_ = rnd_.PseudoUniform(kHeight - 8) + 8;
51 }
52
Sebastien Alaiwan9362a512017-08-17 16:31:58 +020053 // 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 Alaiwan439b21c2017-08-17 16:28:44 +020062 cfg.g_w = width_;
63 cfg.g_h = height_;
Sebastien Alaiwan14cb8602017-07-11 11:27:19 +020064 cfg.g_lag_in_frames = 0;
Sebastien Alaiwan9362a512017-08-17 16:31:58 +020065 cfg.g_profile = format_ == AOM_IMG_FMT_I420 ? 0 : 1;
Sebastien Alaiwan14cb8602017-07-11 11:27:19 +020066
67 aom_codec_enc_init(&enc_, algo, &cfg, 0);
68 }
69
70 ~CompressedSource() { aom_codec_destroy(&enc_); }
71
Sebastien Alaiwan15a11222017-07-27 09:32:06 +020072 const aom_codec_cx_pkt_t *ReadFrame() {
Sebastien Alaiwan9362a512017-08-17 16:31:58 +020073 uint8_t buf[kWidth * kHeight * 3] = { 0 };
Sebastien Alaiwan14cb8602017-07-11 11:27:19 +020074
75 // render regular pattern
76 const int period = rnd_.Rand8() % 32 + 1;
77 const int phase = rnd_.Rand8() % period;
78
79 const int val_a = rnd_.Rand8();
80 const int val_b = rnd_.Rand8();
Sebastien Alaiwan15a11222017-07-27 09:32:06 +020081
Sebastien Alaiwan14cb8602017-07-11 11:27:19 +020082 for (int i = 0; i < (int)sizeof buf; ++i)
83 buf[i] = (i + phase) % period < period / 2 ? val_a : val_b;
84
85 aom_image_t img;
Sebastien Alaiwan9362a512017-08-17 16:31:58 +020086 aom_img_wrap(&img, format_, width_, height_, 0, buf);
Sebastien Alaiwan14cb8602017-07-11 11:27:19 +020087 aom_codec_encode(&enc_, &img, frame_count_++, 1, 0, 0);
88
89 aom_codec_iter_t iter = NULL;
Angie Chiang7496d662017-08-08 11:30:46 -070090
91 const aom_codec_cx_pkt_t *pkt = NULL;
92
93 do {
94 pkt = aom_codec_get_cx_data(&enc_, &iter);
95 } while (pkt && pkt->kind != AOM_CODEC_CX_FRAME_PKT);
96
97 return pkt;
Sebastien Alaiwan14cb8602017-07-11 11:27:19 +020098 }
99
100 private:
Sebastien Alaiwan439b21c2017-08-17 16:28:44 +0200101 static const int kWidth = 128;
102 static const int kHeight = 128;
Sebastien Alaiwan15a11222017-07-27 09:32:06 +0200103
Sebastien Alaiwan14cb8602017-07-11 11:27:19 +0200104 ACMRandom rnd_;
Sebastien Alaiwan9362a512017-08-17 16:31:58 +0200105 aom_img_fmt_t format_;
Sebastien Alaiwan14cb8602017-07-11 11:27:19 +0200106 aom_codec_ctx_t enc_;
107 int frame_count_;
Sebastien Alaiwan439b21c2017-08-17 16:28:44 +0200108 int width_, height_;
Sebastien Alaiwan14cb8602017-07-11 11:27:19 +0200109};
110
111// lowers an aom_image_t to a easily comparable/printable form
Sebastien Alaiwan15a11222017-07-27 09:32:06 +0200112std::vector<int16_t> Serialize(const aom_image_t *img) {
Sebastien Alaiwan14cb8602017-07-11 11:27:19 +0200113 std::vector<int16_t> bytes;
114 bytes.reserve(img->d_w * img->d_h * 3);
Sebastien Alaiwan15a11222017-07-27 09:32:06 +0200115 for (int plane = 0; plane < 3; ++plane) {
Sebastien Alaiwanaeb1e482017-09-21 11:36:56 +0200116 const int w = aom_img_plane_width(img, plane);
117 const int h = aom_img_plane_height(img, plane);
118
119 for (int r = 0; r < h; ++r) {
120 for (int c = 0; c < w; ++c) {
Sebastien Alaiwana85a2b92017-09-21 11:45:26 +0200121 unsigned char *row = img->planes[plane] + r * img->stride[plane];
Sebastien Alaiwan14cb8602017-07-11 11:27:19 +0200122 if (img->fmt & AOM_IMG_FMT_HIGHBITDEPTH)
Sebastien Alaiwana85a2b92017-09-21 11:45:26 +0200123 bytes.push_back(row[c * 2]);
Sebastien Alaiwan14cb8602017-07-11 11:27:19 +0200124 else
Sebastien Alaiwana85a2b92017-09-21 11:45:26 +0200125 bytes.push_back(row[c]);
Sebastien Alaiwan14cb8602017-07-11 11:27:19 +0200126 }
Sebastien Alaiwan15a11222017-07-27 09:32:06 +0200127 }
128 }
Sebastien Alaiwan14cb8602017-07-11 11:27:19 +0200129
130 return bytes;
131}
132
Sebastien Alaiwan15a11222017-07-27 09:32:06 +0200133class Decoder {
134 public:
Sebastien Alaiwan14cb8602017-07-11 11:27:19 +0200135 explicit Decoder(int allowLowbitdepth) {
136 aom_codec_iface_t *algo = &aom_codec_av1_dx_algo;
137
James Zern78e5ecf2017-08-15 16:45:58 -0700138 aom_codec_dec_cfg_t cfg = aom_codec_dec_cfg_t();
Sebastien Alaiwan14cb8602017-07-11 11:27:19 +0200139 cfg.allow_lowbitdepth = allowLowbitdepth;
140
141 aom_codec_dec_init(&dec_, algo, &cfg, 0);
142 }
143
144 ~Decoder() { aom_codec_destroy(&dec_); }
145
146 std::vector<int16_t> decode(const aom_codec_cx_pkt_t *pkt) {
Sebastien Alaiwan15a11222017-07-27 09:32:06 +0200147 aom_codec_decode(&dec_, static_cast<uint8_t *>(pkt->data.frame.buf),
148 static_cast<unsigned int>(pkt->data.frame.sz), NULL, 0);
Sebastien Alaiwan14cb8602017-07-11 11:27:19 +0200149
150 aom_codec_iter_t iter = NULL;
Sebastien Alaiwan15a11222017-07-27 09:32:06 +0200151 return Serialize(aom_codec_get_frame(&dec_, &iter));
Sebastien Alaiwan14cb8602017-07-11 11:27:19 +0200152 }
153
154 private:
155 aom_codec_ctx_t dec_;
156};
157
158// Try to reveal a mismatch between LBD and HBD coding paths.
159TEST(CodingPathSync, SearchForHbdLbdMismatch) {
Sebastien Alaiwan14cb8602017-07-11 11:27:19 +0200160 const int count_tests = 100;
161 for (int i = 0; i < count_tests; ++i) {
Sebastien Alaiwan15a11222017-07-27 09:32:06 +0200162 Decoder dec_hbd(0);
163 Decoder dec_lbd(1);
Sebastien Alaiwan14cb8602017-07-11 11:27:19 +0200164
165 CompressedSource enc(i);
Sebastien Alaiwan15a11222017-07-27 09:32:06 +0200166 const aom_codec_cx_pkt_t *frame = enc.ReadFrame();
Sebastien Alaiwan67876e72017-08-10 10:58:31 +0200167
168 std::vector<int16_t> lbd_yuv = dec_lbd.decode(frame);
169 std::vector<int16_t> hbd_yuv = dec_hbd.decode(frame);
170
Sebastien Alaiwan58011a72017-09-21 12:08:30 +0200171 ASSERT_EQ(lbd_yuv, hbd_yuv);
Sebastien Alaiwan14cb8602017-07-11 11:27:19 +0200172 }
173}
174
175} // namespace