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 
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 
27 class CompressedSource {
28  public:
CompressedSource(int seed)29   explicit CompressedSource(int seed) : rnd_(seed), frame_count_(0) {
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 
35     const int max_q = cfg.rc_max_quantizer;
36 
37     cfg.rc_end_usage = AOM_CQ;
38     cfg.rc_max_quantizer = max_q;
39     cfg.rc_min_quantizer = max_q;
40 
41     // choose the picture size
42     {
43       width_ = rnd_.PseudoUniform(kWidth - 8) + 8;
44       height_ = rnd_.PseudoUniform(kHeight - 8) + 8;
45     }
46 
47     cfg.g_w = width_;
48     cfg.g_h = height_;
49     cfg.g_lag_in_frames = 0;
50 
51     aom_codec_enc_init(&enc_, algo, &cfg, 0);
52   }
53 
~CompressedSource()54   ~CompressedSource() { aom_codec_destroy(&enc_); }
55 
ReadFrame()56   const aom_codec_cx_pkt_t *ReadFrame() {
57     uint8_t buf[kWidth * kHeight * 3 / 2] = { 0 };
58 
59     // render regular pattern
60     const int period = rnd_.Rand8() % 32 + 1;
61     const int phase = rnd_.Rand8() % period;
62 
63     const int val_a = rnd_.Rand8();
64     const int val_b = rnd_.Rand8();
65 
66     for (int i = 0; i < (int)sizeof buf; ++i)
67       buf[i] = (i + phase) % period < period / 2 ? val_a : val_b;
68 
69     aom_image_t img;
70     aom_img_wrap(&img, AOM_IMG_FMT_I420, width_, height_, 0, buf);
71     aom_codec_encode(&enc_, &img, frame_count_++, 1, 0, 0);
72 
73     aom_codec_iter_t iter = NULL;
74 
75     const aom_codec_cx_pkt_t *pkt = NULL;
76 
77     do {
78       pkt = aom_codec_get_cx_data(&enc_, &iter);
79     } while (pkt && pkt->kind != AOM_CODEC_CX_FRAME_PKT);
80 
81     return pkt;
82   }
83 
84  private:
85   static const int kWidth = 128;
86   static const int kHeight = 128;
87 
88   ACMRandom rnd_;
89   aom_codec_ctx_t enc_;
90   int frame_count_;
91   int width_, height_;
92 };
93 
94 // lowers an aom_image_t to a easily comparable/printable form
Serialize(const aom_image_t * img)95 std::vector<int16_t> Serialize(const aom_image_t *img) {
96   std::vector<int16_t> bytes;
97   bytes.reserve(img->d_w * img->d_h * 3);
98   for (int plane = 0; plane < 3; ++plane) {
99     const int w = aom_img_plane_width(img, plane);
100     const int h = aom_img_plane_height(img, plane);
101 
102     for (int r = 0; r < h; ++r) {
103       for (int c = 0; c < w; ++c) {
104         unsigned char *row = img->planes[plane] + r * img->stride[plane];
105         if (img->fmt & AOM_IMG_FMT_HIGHBITDEPTH)
106           bytes.push_back(row[c * 2]);
107         else
108           bytes.push_back(row[c]);
109       }
110     }
111   }
112 
113   return bytes;
114 }
115 
116 class Decoder {
117  public:
Decoder(int allowLowbitdepth)118   explicit Decoder(int allowLowbitdepth) {
119     aom_codec_iface_t *algo = &aom_codec_av1_dx_algo;
120 
121     aom_codec_dec_cfg_t cfg = aom_codec_dec_cfg_t();
122     cfg.allow_lowbitdepth = allowLowbitdepth;
123 
124     aom_codec_dec_init(&dec_, algo, &cfg, 0);
125   }
126 
~Decoder()127   ~Decoder() { aom_codec_destroy(&dec_); }
128 
decode(const aom_codec_cx_pkt_t * pkt)129   std::vector<int16_t> decode(const aom_codec_cx_pkt_t *pkt) {
130     aom_codec_decode(&dec_, static_cast<uint8_t *>(pkt->data.frame.buf),
131                      static_cast<unsigned int>(pkt->data.frame.sz), NULL, 0);
132 
133     aom_codec_iter_t iter = NULL;
134     return Serialize(aom_codec_get_frame(&dec_, &iter));
135   }
136 
137  private:
138   aom_codec_ctx_t dec_;
139 };
140 
141 // Try to reveal a mismatch between LBD and HBD coding paths.
TEST(CodingPathSync,SearchForHbdLbdMismatch)142 TEST(CodingPathSync, SearchForHbdLbdMismatch) {
143   const int count_tests = 100;
144   for (int i = 0; i < count_tests; ++i) {
145     Decoder dec_hbd(0);
146     Decoder dec_lbd(1);
147 
148     CompressedSource enc(i);
149     const aom_codec_cx_pkt_t *frame = enc.ReadFrame();
150 
151     std::vector<int16_t> lbd_yuv = dec_lbd.decode(frame);
152     std::vector<int16_t> hbd_yuv = dec_hbd.decode(frame);
153 
154     ASSERT_EQ(lbd_yuv, hbd_yuv);
155   }
156 }
157 
158 }  // namespace
159