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 <assert.h>
13 #include <string>
14 #include <vector>
15 #include "third_party/googletest/src/googletest/include/gtest/gtest.h"
16 #include "test/codec_factory.h"
17 #include "test/encode_test_driver.h"
18 #include "test/i420_video_source.h"
19 #include "test/md5_helper.h"
20 #include "test/util.h"
21
22 namespace {
23 // The number of frames to be encoded/decoded
24 const int kLimit = 8;
25 // Skip 1 frame to check the frame decoding independency.
26 const int kSkip = 5;
27 const int kTileSize = 1;
28 const int kTIleSizeInPixels = (kTileSize << 6);
29 // Fake width and height so that they can be multiples of the tile size.
30 const int kImgWidth = 704;
31 const int kImgHeight = 576;
32
33 // This test tests large scale tile coding case. Non-large-scale tile coding
34 // is tested by the tile_independence test.
35 class AV1ExtTileTest
36 : public ::libaom_test::CodecTestWith2Params<libaom_test::TestMode, int>,
37 public ::libaom_test::EncoderTest {
38 protected:
AV1ExtTileTest()39 AV1ExtTileTest()
40 : EncoderTest(GET_PARAM(0)), encoding_mode_(GET_PARAM(1)),
41 set_cpu_used_(GET_PARAM(2)) {
42 init_flags_ = AOM_CODEC_USE_PSNR;
43 aom_codec_dec_cfg_t cfg = aom_codec_dec_cfg_t();
44 cfg.w = kImgWidth;
45 cfg.h = kImgHeight;
46 cfg.allow_lowbitdepth = 1;
47
48 decoder_ = codec_->CreateDecoder(cfg, 0);
49 decoder_->Control(AV1_SET_TILE_MODE, 1);
50 decoder_->Control(AV1D_EXT_TILE_DEBUG, 1);
51 decoder_->Control(AV1_SET_DECODE_TILE_ROW, -1);
52 decoder_->Control(AV1_SET_DECODE_TILE_COL, -1);
53
54 // Allocate buffer to store tile image.
55 aom_img_alloc(&tile_img_, AOM_IMG_FMT_I420, kImgWidth, kImgHeight, 32);
56
57 md5_.clear();
58 tile_md5_.clear();
59 }
60
~AV1ExtTileTest()61 virtual ~AV1ExtTileTest() {
62 aom_img_free(&tile_img_);
63 delete decoder_;
64 }
65
SetUp()66 virtual void SetUp() {
67 InitializeConfig(encoding_mode_);
68
69 cfg_.g_lag_in_frames = 0;
70 cfg_.rc_end_usage = AOM_VBR;
71 cfg_.g_error_resilient = 1;
72
73 cfg_.rc_max_quantizer = 56;
74 cfg_.rc_min_quantizer = 0;
75 }
76
PreEncodeFrameHook(::libaom_test::VideoSource * video,::libaom_test::Encoder * encoder)77 virtual void PreEncodeFrameHook(::libaom_test::VideoSource *video,
78 ::libaom_test::Encoder *encoder) {
79 if (video->frame() == 0) {
80 // Encode setting
81 encoder->Control(AOME_SET_CPUUSED, set_cpu_used_);
82 encoder->Control(AOME_SET_ENABLEAUTOALTREF, 0);
83 encoder->Control(AV1E_SET_FRAME_PARALLEL_DECODING, 1);
84
85 // TODO(yunqingwang): test single_tile_decoding = 0.
86 encoder->Control(AV1E_SET_SINGLE_TILE_DECODING, 1);
87 // Always use 64x64 max partition.
88 encoder->Control(AV1E_SET_SUPERBLOCK_SIZE, AOM_SUPERBLOCK_SIZE_64X64);
89 // Set tile_columns and tile_rows to MAX values, which guarantees the tile
90 // size of 64 x 64 pixels(i.e. 1 SB) for <= 4k resolution.
91 encoder->Control(AV1E_SET_TILE_COLUMNS, 6);
92 encoder->Control(AV1E_SET_TILE_ROWS, 6);
93 }
94
95 if (video->frame() == 1) {
96 frame_flags_ =
97 AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_GF | AOM_EFLAG_NO_UPD_ARF;
98 }
99 }
100
DecompressedFrameHook(const aom_image_t & img,aom_codec_pts_t pts)101 virtual void DecompressedFrameHook(const aom_image_t &img,
102 aom_codec_pts_t pts) {
103 // Skip 1 already decoded frame to be consistent with the decoder in this
104 // test.
105 if (pts == (aom_codec_pts_t)kSkip) return;
106
107 // Calculate MD5 as the reference.
108 ::libaom_test::MD5 md5_res;
109 md5_res.Add(&img);
110 md5_.push_back(md5_res.Get());
111 }
112
FramePktHook(const aom_codec_cx_pkt_t * pkt)113 virtual void FramePktHook(const aom_codec_cx_pkt_t *pkt) {
114 // Skip decoding 1 frame.
115 if (pkt->data.frame.pts == (aom_codec_pts_t)kSkip) return;
116
117 bool IsLastFrame = (pkt->data.frame.pts == (aom_codec_pts_t)(kLimit - 1));
118
119 // Decode the first (kLimit - 1) frames as whole frame, and decode the last
120 // frame in single tiles.
121 for (int r = 0; r < kImgHeight / kTIleSizeInPixels; ++r) {
122 for (int c = 0; c < kImgWidth / kTIleSizeInPixels; ++c) {
123 if (!IsLastFrame) {
124 decoder_->Control(AV1_SET_DECODE_TILE_ROW, -1);
125 decoder_->Control(AV1_SET_DECODE_TILE_COL, -1);
126 } else {
127 decoder_->Control(AV1_SET_DECODE_TILE_ROW, r);
128 decoder_->Control(AV1_SET_DECODE_TILE_COL, c);
129 }
130
131 const aom_codec_err_t res = decoder_->DecodeFrame(
132 reinterpret_cast<uint8_t *>(pkt->data.frame.buf),
133 pkt->data.frame.sz);
134 if (res != AOM_CODEC_OK) {
135 abort_ = true;
136 ASSERT_EQ(AOM_CODEC_OK, res);
137 }
138 const aom_image_t *img = decoder_->GetDxData().Next();
139
140 if (!IsLastFrame) {
141 if (img) {
142 ::libaom_test::MD5 md5_res;
143 md5_res.Add(img);
144 tile_md5_.push_back(md5_res.Get());
145 }
146 break;
147 }
148
149 const int kMaxMBPlane = 3;
150 for (int plane = 0; plane < kMaxMBPlane; ++plane) {
151 const int shift = (plane == 0) ? 0 : 1;
152 int tile_height = kTIleSizeInPixels >> shift;
153 int tile_width = kTIleSizeInPixels >> shift;
154
155 for (int tr = 0; tr < tile_height; ++tr) {
156 memcpy(tile_img_.planes[plane] +
157 tile_img_.stride[plane] * (r * tile_height + tr) +
158 c * tile_width,
159 img->planes[plane] + img->stride[plane] * tr, tile_width);
160 }
161 }
162 }
163
164 if (!IsLastFrame) break;
165 }
166
167 if (IsLastFrame) {
168 ::libaom_test::MD5 md5_res;
169 md5_res.Add(&tile_img_);
170 tile_md5_.push_back(md5_res.Get());
171 }
172 }
173
TestRoundTrip()174 void TestRoundTrip() {
175 ::libaom_test::I420VideoSource video(
176 "hantro_collage_w352h288.yuv", kImgWidth, kImgHeight, 30, 1, 0, kLimit);
177 cfg_.rc_target_bitrate = 500;
178 cfg_.g_error_resilient = AOM_ERROR_RESILIENT_DEFAULT;
179 cfg_.large_scale_tile = 1;
180 cfg_.g_lag_in_frames = 0;
181 cfg_.g_threads = 1;
182
183 // Tile encoding
184 init_flags_ = AOM_CODEC_USE_PSNR;
185 ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
186
187 // Compare to check if two vectors are equal.
188 ASSERT_EQ(md5_, tile_md5_);
189 }
190
191 ::libaom_test::TestMode encoding_mode_;
192 int set_cpu_used_;
193 ::libaom_test::Decoder *decoder_;
194 aom_image_t tile_img_;
195 std::vector<std::string> md5_;
196 std::vector<std::string> tile_md5_;
197 };
198
TEST_P(AV1ExtTileTest,DecoderResultTest)199 TEST_P(AV1ExtTileTest, DecoderResultTest) { TestRoundTrip(); }
200
201 AV1_INSTANTIATE_TEST_SUITE(
202 // Now only test 2-pass mode.
203 AV1ExtTileTest, ::testing::Values(::libaom_test::kTwoPassGood),
204 ::testing::Range(1, 4));
205
206 class AV1ExtTileTestLarge : public AV1ExtTileTest {};
207
TEST_P(AV1ExtTileTestLarge,DecoderResultTest)208 TEST_P(AV1ExtTileTestLarge, DecoderResultTest) { TestRoundTrip(); }
209
210 AV1_INSTANTIATE_TEST_SUITE(
211 // Now only test 2-pass mode.
212 AV1ExtTileTestLarge, ::testing::Values(::libaom_test::kTwoPassGood),
213 ::testing::Range(0, 1));
214 } // namespace
215