coding_path_sync.cc 4.49 KB
Newer Older
Sebastien Alaiwan's avatar
Sebastien Alaiwan committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/*
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
 *
 * This source code is subject to the terms of the BSD 2 Clause License and
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
 * was not distributed with this source code in the LICENSE file, you can
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
 * Media Patent License 1.0 was not distributed with this source code in the
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
 */

#include <vector>
#include "third_party/googletest/src/googletest/include/gtest/gtest.h"
#include "test/acm_random.h"

#include "./aom_config.h"

#include "aom_ports/mem.h"  // ROUND_POWER_OF_TWO
#include "aom/aomcx.h"
#include "aom/aomdx.h"
#include "aom/aom_encoder.h"
#include "aom/aom_decoder.h"

using libaom_test::ACMRandom;
namespace {

27
28
29
class CompressedSource {
 public:
  explicit CompressedSource(int seed) : rnd_(seed), frame_count_(0) {
Sebastien Alaiwan's avatar
Sebastien Alaiwan committed
30
31
32
33
34
35
36
37
38
39
    aom_codec_iface_t *algo = &aom_codec_av1_cx_algo;

    aom_codec_enc_cfg_t cfg;
    aom_codec_enc_config_default(algo, &cfg, 0);

    const int max_q = cfg.rc_max_quantizer;

    cfg.rc_end_usage = AOM_CQ;
    cfg.rc_max_quantizer = max_q;
    cfg.rc_min_quantizer = max_q;
40
41
42
43
44
45
46
47
48

    // choose the picture size
    {
      width_ = rnd_.PseudoUniform(kWidth - 8) + 8;
      height_ = rnd_.PseudoUniform(kHeight - 8) + 8;
    }

    cfg.g_w = width_;
    cfg.g_h = height_;
Sebastien Alaiwan's avatar
Sebastien Alaiwan committed
49
50
51
52
53
54
55
    cfg.g_lag_in_frames = 0;

    aom_codec_enc_init(&enc_, algo, &cfg, 0);
  }

  ~CompressedSource() { aom_codec_destroy(&enc_); }

56
57
  const aom_codec_cx_pkt_t *ReadFrame() {
    uint8_t buf[kWidth * kHeight * 3 / 2] = { 0 };
Sebastien Alaiwan's avatar
Sebastien Alaiwan committed
58
59
60
61
62
63
64

    // render regular pattern
    const int period = rnd_.Rand8() % 32 + 1;
    const int phase = rnd_.Rand8() % period;

    const int val_a = rnd_.Rand8();
    const int val_b = rnd_.Rand8();
65

Sebastien Alaiwan's avatar
Sebastien Alaiwan committed
66
67
68
69
    for (int i = 0; i < (int)sizeof buf; ++i)
      buf[i] = (i + phase) % period < period / 2 ? val_a : val_b;

    aom_image_t img;
70
    aom_img_wrap(&img, AOM_IMG_FMT_I420, width_, height_, 0, buf);
Sebastien Alaiwan's avatar
Sebastien Alaiwan committed
71
72
73
    aom_codec_encode(&enc_, &img, frame_count_++, 1, 0, 0);

    aom_codec_iter_t iter = NULL;
74
75
76
77
78
79
80
81

    const aom_codec_cx_pkt_t *pkt = NULL;

    do {
      pkt = aom_codec_get_cx_data(&enc_, &iter);
    } while (pkt && pkt->kind != AOM_CODEC_CX_FRAME_PKT);

    return pkt;
Sebastien Alaiwan's avatar
Sebastien Alaiwan committed
82
83
84
  }

 private:
85
86
  static const int kWidth = 128;
  static const int kHeight = 128;
87

Sebastien Alaiwan's avatar
Sebastien Alaiwan committed
88
89
90
  ACMRandom rnd_;
  aom_codec_ctx_t enc_;
  int frame_count_;
91
  int width_, height_;
Sebastien Alaiwan's avatar
Sebastien Alaiwan committed
92
93
94
};

// lowers an aom_image_t to a easily comparable/printable form
95
std::vector<int16_t> Serialize(const aom_image_t *img) {
Sebastien Alaiwan's avatar
Sebastien Alaiwan committed
96
97
  std::vector<int16_t> bytes;
  bytes.reserve(img->d_w * img->d_h * 3);
98
  for (int plane = 0; plane < 3; ++plane) {
99
100
101
102
103
    const int w = aom_img_plane_width(img, plane);
    const int h = aom_img_plane_height(img, plane);

    for (int r = 0; r < h; ++r) {
      for (int c = 0; c < w; ++c) {
104
        unsigned char *row = img->planes[plane] + r * img->stride[plane];
Sebastien Alaiwan's avatar
Sebastien Alaiwan committed
105
        if (img->fmt & AOM_IMG_FMT_HIGHBITDEPTH)
106
          bytes.push_back(row[c * 2]);
Sebastien Alaiwan's avatar
Sebastien Alaiwan committed
107
        else
108
          bytes.push_back(row[c]);
Sebastien Alaiwan's avatar
Sebastien Alaiwan committed
109
      }
110
111
    }
  }
Sebastien Alaiwan's avatar
Sebastien Alaiwan committed
112
113
114
115

  return bytes;
}

116
117
class Decoder {
 public:
Sebastien Alaiwan's avatar
Sebastien Alaiwan committed
118
119
120
  explicit Decoder(int allowLowbitdepth) {
    aom_codec_iface_t *algo = &aom_codec_av1_dx_algo;

121
    aom_codec_dec_cfg_t cfg = aom_codec_dec_cfg_t();
Sebastien Alaiwan's avatar
Sebastien Alaiwan committed
122
123
124
125
126
127
128
129
    cfg.allow_lowbitdepth = allowLowbitdepth;

    aom_codec_dec_init(&dec_, algo, &cfg, 0);
  }

  ~Decoder() { aom_codec_destroy(&dec_); }

  std::vector<int16_t> decode(const aom_codec_cx_pkt_t *pkt) {
130
131
    aom_codec_decode(&dec_, static_cast<uint8_t *>(pkt->data.frame.buf),
                     static_cast<unsigned int>(pkt->data.frame.sz), NULL, 0);
Sebastien Alaiwan's avatar
Sebastien Alaiwan committed
132
133

    aom_codec_iter_t iter = NULL;
134
    return Serialize(aom_codec_get_frame(&dec_, &iter));
Sebastien Alaiwan's avatar
Sebastien Alaiwan committed
135
136
137
138
139
140
141
142
143
144
  }

 private:
  aom_codec_ctx_t dec_;
};

// Try to reveal a mismatch between LBD and HBD coding paths.
TEST(CodingPathSync, SearchForHbdLbdMismatch) {
  const int count_tests = 100;
  for (int i = 0; i < count_tests; ++i) {
145
146
    Decoder dec_hbd(0);
    Decoder dec_lbd(1);
Sebastien Alaiwan's avatar
Sebastien Alaiwan committed
147
148

    CompressedSource enc(i);
149
    const aom_codec_cx_pkt_t *frame = enc.ReadFrame();
150
151
152
153
154

    std::vector<int16_t> lbd_yuv = dec_lbd.decode(frame);
    std::vector<int16_t> hbd_yuv = dec_hbd.decode(frame);

    // TODO(aomedia:39): re-enable the comparison when both coding paths match,
155
    // so they don't diverge anymore.
156
157
    // Until then, keep doing the decoding to prevent crashes from creeping in,
    // making this test harder to re-enable.
158
    if (0) {
159
      ASSERT_EQ(lbd_yuv, hbd_yuv);
160
    }
Sebastien Alaiwan's avatar
Sebastien Alaiwan committed
161
162
163
164
  }
}

}  // namespace