Sebastien Alaiwan | 14cb860 | 2017-07-11 11:27:19 +0200 | [diff] [blame^] | 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 | #if CONFIG_AV1_ENCODER && CONFIG_AV1_DECODER |
| 19 | |
| 20 | #include "aom_ports/mem.h" // ROUND_POWER_OF_TWO |
| 21 | #include "aom/aomcx.h" |
| 22 | #include "aom/aomdx.h" |
| 23 | #include "aom/aom_encoder.h" |
| 24 | #include "aom/aom_decoder.h" |
| 25 | |
| 26 | using libaom_test::ACMRandom; |
| 27 | namespace { |
| 28 | |
| 29 | struct CompressedSource { |
| 30 | explicit CompressedSource(int seed) : rnd_(seed) { |
| 31 | frame_count_ = 0; |
| 32 | aom_codec_iface_t *algo = &aom_codec_av1_cx_algo; |
| 33 | |
| 34 | aom_codec_enc_cfg_t cfg; |
| 35 | aom_codec_enc_config_default(algo, &cfg, 0); |
| 36 | |
| 37 | const int max_q = cfg.rc_max_quantizer; |
| 38 | |
| 39 | cfg.rc_end_usage = AOM_CQ; |
| 40 | cfg.rc_max_quantizer = max_q; |
| 41 | cfg.rc_min_quantizer = max_q; |
| 42 | cfg.g_w = WIDTH; |
| 43 | cfg.g_h = HEIGHT; |
| 44 | cfg.g_lag_in_frames = 0; |
| 45 | |
| 46 | aom_codec_enc_init(&enc_, algo, &cfg, 0); |
| 47 | } |
| 48 | |
| 49 | ~CompressedSource() { aom_codec_destroy(&enc_); } |
| 50 | |
| 51 | const aom_codec_cx_pkt_t *readFrame() { |
| 52 | uint8_t buf[WIDTH * HEIGHT * 3 / 2] = { 0 }; |
| 53 | |
| 54 | // render regular pattern |
| 55 | const int period = rnd_.Rand8() % 32 + 1; |
| 56 | const int phase = rnd_.Rand8() % period; |
| 57 | |
| 58 | const int val_a = rnd_.Rand8(); |
| 59 | const int val_b = rnd_.Rand8(); |
| 60 | for (int i = 0; i < (int)sizeof buf; ++i) |
| 61 | buf[i] = (i + phase) % period < period / 2 ? val_a : val_b; |
| 62 | |
| 63 | aom_image_t img; |
| 64 | aom_img_wrap(&img, AOM_IMG_FMT_I420, WIDTH, HEIGHT, 0, buf); |
| 65 | aom_codec_encode(&enc_, &img, frame_count_++, 1, 0, 0); |
| 66 | |
| 67 | aom_codec_iter_t iter = NULL; |
| 68 | return aom_codec_get_cx_data(&enc_, &iter); |
| 69 | } |
| 70 | |
| 71 | private: |
| 72 | ACMRandom rnd_; |
| 73 | aom_codec_ctx_t enc_; |
| 74 | int frame_count_; |
| 75 | static const int WIDTH = 32; |
| 76 | static const int HEIGHT = 32; |
| 77 | }; |
| 78 | |
| 79 | // lowers an aom_image_t to a easily comparable/printable form |
| 80 | std::vector<int16_t> serialize(const aom_image_t *img) { |
| 81 | const int w_uv = ROUND_POWER_OF_TWO(img->d_w, img->x_chroma_shift); |
| 82 | const int h_uv = ROUND_POWER_OF_TWO(img->d_h, img->y_chroma_shift); |
| 83 | const int w[] = { (int)img->d_w, w_uv, w_uv }; |
| 84 | const int h[] = { (int)img->d_h, h_uv, h_uv }; |
| 85 | |
| 86 | std::vector<int16_t> bytes; |
| 87 | bytes.reserve(img->d_w * img->d_h * 3); |
| 88 | for (int plane = 0; plane < 3; ++plane) |
| 89 | for (int r = 0; r < h[plane]; ++r) |
| 90 | for (int c = 0; c < w[plane]; ++c) { |
| 91 | const int offset = r * img->stride[plane] + c; |
| 92 | if (img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) |
| 93 | bytes.push_back(img->planes[plane][offset * 2]); |
| 94 | else |
| 95 | bytes.push_back(img->planes[plane][offset]); |
| 96 | } |
| 97 | |
| 98 | return bytes; |
| 99 | } |
| 100 | |
| 101 | struct Decoder { |
| 102 | explicit Decoder(int allowLowbitdepth) { |
| 103 | aom_codec_iface_t *algo = &aom_codec_av1_dx_algo; |
| 104 | |
| 105 | aom_codec_dec_cfg cfg = { 0 }; |
| 106 | cfg.allow_lowbitdepth = allowLowbitdepth; |
| 107 | |
| 108 | aom_codec_dec_init(&dec_, algo, &cfg, 0); |
| 109 | } |
| 110 | |
| 111 | ~Decoder() { aom_codec_destroy(&dec_); } |
| 112 | |
| 113 | std::vector<int16_t> decode(const aom_codec_cx_pkt_t *pkt) { |
| 114 | aom_codec_decode(&dec_, (uint8_t *)pkt->data.frame.buf, pkt->data.frame.sz, |
| 115 | NULL, 0); |
| 116 | |
| 117 | aom_codec_iter_t iter = NULL; |
| 118 | return serialize(aom_codec_get_frame(&dec_, &iter)); |
| 119 | } |
| 120 | |
| 121 | private: |
| 122 | aom_codec_ctx_t dec_; |
| 123 | }; |
| 124 | |
| 125 | // Try to reveal a mismatch between LBD and HBD coding paths. |
| 126 | TEST(CodingPathSync, SearchForHbdLbdMismatch) { |
| 127 | // disable test. Re-enable it locally to help diagnosing LBD/HBD mismatches. |
| 128 | // And re-enable it once both coding paths match |
| 129 | // so they don't diverge anymore. |
| 130 | return; |
| 131 | |
| 132 | const int count_tests = 100; |
| 133 | for (int i = 0; i < count_tests; ++i) { |
| 134 | Decoder dec_HBD(0); |
| 135 | Decoder dec_LBD(1); |
| 136 | |
| 137 | CompressedSource enc(i); |
| 138 | const aom_codec_cx_pkt_t *frame = enc.readFrame(); |
| 139 | ASSERT_EQ(dec_LBD.decode(frame), dec_HBD.decode(frame)); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | } // namespace |
| 144 | |
| 145 | #endif |