2 * Copyright (c) 2013 The WebM project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
13 #include "third_party/googletest/src/include/gtest/gtest.h"
14 #include "./vpx_config.h"
15 #include "test/codec_factory.h"
16 #include "test/decode_test_driver.h"
17 #include "test/md5_helper.h"
19 #include "test/webm_video_source.h"
21 #include "vpx_util/vpx_thread.h"
27 class VPxWorkerThreadTest : public ::testing::TestWithParam<bool> {
29 virtual ~VPxWorkerThreadTest() {}
30 virtual void SetUp() { vpx_get_worker_interface()->init(&worker_); }
32 virtual void TearDown() { vpx_get_worker_interface()->end(&worker_); }
34 void Run(VPxWorker *worker) {
35 const bool synchronous = GetParam();
37 vpx_get_worker_interface()->execute(worker);
39 vpx_get_worker_interface()->launch(worker);
46 int ThreadHook(void *data, void *return_value) {
47 int *const hook_data = reinterpret_cast<int *>(data);
49 return *reinterpret_cast<int *>(return_value);
52 TEST_P(VPxWorkerThreadTest, HookSuccess) {
54 EXPECT_NE(vpx_get_worker_interface()->sync(&worker_), 0);
56 for (int i = 0; i < 2; ++i) {
57 EXPECT_NE(vpx_get_worker_interface()->reset(&worker_), 0);
60 int return_value = 1; // return successfully from the hook
61 worker_.hook = ThreadHook;
62 worker_.data1 = &hook_data;
63 worker_.data2 = &return_value;
66 EXPECT_NE(vpx_get_worker_interface()->sync(&worker_), 0);
67 EXPECT_FALSE(worker_.had_error);
68 EXPECT_EQ(5, hook_data);
71 EXPECT_NE(vpx_get_worker_interface()->sync(&worker_), 0);
75 TEST_P(VPxWorkerThreadTest, HookFailure) {
76 EXPECT_NE(vpx_get_worker_interface()->reset(&worker_), 0);
79 int return_value = 0; // return failure from the hook
80 worker_.hook = ThreadHook;
81 worker_.data1 = &hook_data;
82 worker_.data2 = &return_value;
85 EXPECT_FALSE(vpx_get_worker_interface()->sync(&worker_));
86 EXPECT_EQ(1, worker_.had_error);
88 // Ensure _reset() clears the error and _launch() can be called again.
90 EXPECT_NE(vpx_get_worker_interface()->reset(&worker_), 0);
91 EXPECT_FALSE(worker_.had_error);
92 vpx_get_worker_interface()->launch(&worker_);
93 EXPECT_NE(vpx_get_worker_interface()->sync(&worker_), 0);
94 EXPECT_FALSE(worker_.had_error);
97 TEST_P(VPxWorkerThreadTest, EndWithoutSync) {
98 // Create a large number of threads to increase the chances of detecting a
99 // race. Doing more work in the hook is no guarantee as any race would occur
100 // post hook execution in the main thread loop driver.
101 static const int kNumWorkers = 64;
102 VPxWorker workers[kNumWorkers];
103 int hook_data[kNumWorkers];
104 int return_value[kNumWorkers];
106 for (int n = 0; n < kNumWorkers; ++n) {
107 vpx_get_worker_interface()->init(&workers[n]);
108 return_value[n] = 1; // return successfully from the hook
109 workers[n].hook = ThreadHook;
110 workers[n].data1 = &hook_data[n];
111 workers[n].data2 = &return_value[n];
114 for (int i = 0; i < 2; ++i) {
115 for (int n = 0; n < kNumWorkers; ++n) {
116 EXPECT_NE(vpx_get_worker_interface()->reset(&workers[n]), 0);
120 for (int n = 0; n < kNumWorkers; ++n) {
124 for (int n = kNumWorkers - 1; n >= 0; --n) {
125 vpx_get_worker_interface()->end(&workers[n]);
130 TEST(VPxWorkerThreadTest, TestInterfaceAPI) {
131 EXPECT_EQ(0, vpx_set_worker_interface(nullptr));
132 EXPECT_NE(vpx_get_worker_interface(), nullptr);
133 for (int i = 0; i < 6; ++i) {
134 VPxWorkerInterface winterface = *vpx_get_worker_interface();
137 case 0: winterface.init = nullptr; break;
138 case 1: winterface.reset = nullptr; break;
139 case 2: winterface.sync = nullptr; break;
140 case 3: winterface.launch = nullptr; break;
141 case 4: winterface.execute = nullptr; break;
142 case 5: winterface.end = nullptr; break;
144 EXPECT_EQ(0, vpx_set_worker_interface(&winterface));
148 // -----------------------------------------------------------------------------
149 // Multi-threaded decode tests
151 // Decodes |filename| with |num_threads|. Returns the md5 of the decoded frames.
152 string DecodeFile(const string &filename, int num_threads) {
153 libvpx_test::WebMVideoSource video(filename);
156 vpx_codec_dec_cfg_t cfg = vpx_codec_dec_cfg_t();
157 cfg.threads = num_threads;
158 libvpx_test::VP9Decoder decoder(cfg, 0);
160 libvpx_test::MD5 md5;
161 for (video.Begin(); video.cxdata(); video.Next()) {
162 const vpx_codec_err_t res =
163 decoder.DecodeFrame(video.cxdata(), video.frame_size());
164 if (res != VPX_CODEC_OK) {
165 EXPECT_EQ(VPX_CODEC_OK, res) << decoder.DecodeError();
169 libvpx_test::DxDataIterator dec_iter = decoder.GetDxData();
170 const vpx_image_t *img = nullptr;
172 // Get decompressed data
173 while ((img = dec_iter.Next())) {
177 return string(md5.Get());
180 // Trivial serialized thread worker interface implementation.
181 // Note any worker that requires synchronization between other workers will
186 void Init(VPxWorker *const worker) { memset(worker, 0, sizeof(*worker)); }
187 int Reset(VPxWorker *const /*worker*/) { return 1; }
188 int Sync(VPxWorker *const worker) { return !worker->had_error; }
190 void Execute(VPxWorker *const worker) {
191 worker->had_error |= !worker->hook(worker->data1, worker->data2);
194 void Launch(VPxWorker *const worker) { Execute(worker); }
195 void End(VPxWorker *const /*worker*/) {}
200 TEST(VPxWorkerThreadTest, TestSerialInterface) {
201 static const VPxWorkerInterface serial_interface = {
202 impl::Init, impl::Reset, impl::Sync, impl::Launch, impl::Execute, impl::End
204 static const char expected_md5[] = "b35a1b707b28e82be025d960aba039bc";
205 static const char filename[] = "vp90-2-03-size-226x226.webm";
206 VPxWorkerInterface default_interface = *vpx_get_worker_interface();
208 EXPECT_NE(vpx_set_worker_interface(&serial_interface), 0);
209 EXPECT_EQ(expected_md5, DecodeFile(filename, 2));
211 // Reset the interface.
212 EXPECT_NE(vpx_set_worker_interface(&default_interface), 0);
213 EXPECT_EQ(expected_md5, DecodeFile(filename, 2));
218 const char *expected_md5;
219 friend std::ostream &operator<<(std::ostream &os, const FileParam ¶m) {
220 return os << "file name: " << param.name
221 << " digest: " << param.expected_md5;
225 class VP9DecodeMultiThreadedTest : public ::testing::TestWithParam<FileParam> {
228 TEST_P(VP9DecodeMultiThreadedTest, Decode) {
229 for (int t = 1; t <= 8; ++t) {
230 EXPECT_EQ(GetParam().expected_md5, DecodeFile(GetParam().name, t))
231 << "threads = " << t;
235 const FileParam kNoTilesNonFrameParallelFiles[] = {
236 { "vp90-2-03-size-226x226.webm", "b35a1b707b28e82be025d960aba039bc" }
239 const FileParam kFrameParallelFiles[] = {
240 { "vp90-2-08-tile_1x2_frame_parallel.webm",
241 "68ede6abd66bae0a2edf2eb9232241b6" },
242 { "vp90-2-08-tile_1x4_frame_parallel.webm",
243 "368ebc6ebf3a5e478d85b2c3149b2848" },
244 { "vp90-2-08-tile_1x8_frame_parallel.webm",
245 "17e439da2388aff3a0f69cb22579c6c1" },
248 const FileParam kFrameParallelResizeFiles[] = {
249 { "vp90-2-14-resize-fp-tiles-1-16.webm", "0cd5e632c326297e975f38949c31ea94" },
250 { "vp90-2-14-resize-fp-tiles-1-2-4-8-16.webm",
251 "5c78a96a42e7f4a4f6b2edcdb791e44c" },
252 { "vp90-2-14-resize-fp-tiles-1-2.webm", "e030450ae85c3277be2a418769df98e2" },
253 { "vp90-2-14-resize-fp-tiles-1-4.webm", "312eed4e2b64eb7a4e7f18916606a430" },
254 { "vp90-2-14-resize-fp-tiles-16-1.webm", "1755c16d8af16a9cb3fe7338d90abe52" },
255 { "vp90-2-14-resize-fp-tiles-16-2.webm", "500300592d3fcb6f12fab25e48aaf4df" },
256 { "vp90-2-14-resize-fp-tiles-16-4.webm", "47c48379fa6331215d91c67648e1af6e" },
257 { "vp90-2-14-resize-fp-tiles-16-8-4-2-1.webm",
258 "eecf17290739bc708506fa4827665989" },
259 { "vp90-2-14-resize-fp-tiles-16-8.webm", "29b6bb54e4c26b5ca85d5de5fed94e76" },
260 { "vp90-2-14-resize-fp-tiles-1-8.webm", "1b6f175e08cd82cf84bb800ac6d1caa3" },
261 { "vp90-2-14-resize-fp-tiles-2-16.webm", "ca3b03e4197995d8d5444ede7a6c0804" },
262 { "vp90-2-14-resize-fp-tiles-2-1.webm", "99aec065369d70bbb78ccdff65afed3f" },
263 { "vp90-2-14-resize-fp-tiles-2-4.webm", "22d0ebdb49b87d2920a85aea32e1afd5" },
264 { "vp90-2-14-resize-fp-tiles-2-8.webm", "c2115cf051c62e0f7db1d4a783831541" },
265 { "vp90-2-14-resize-fp-tiles-4-16.webm", "c690d7e1719b31367564cac0af0939cb" },
266 { "vp90-2-14-resize-fp-tiles-4-1.webm", "a926020b2cc3e15ad4cc271853a0ff26" },
267 { "vp90-2-14-resize-fp-tiles-4-2.webm", "42699063d9e581f1993d0cf890c2be78" },
268 { "vp90-2-14-resize-fp-tiles-4-8.webm", "7f76d96036382f45121e3d5aa6f8ec52" },
269 { "vp90-2-14-resize-fp-tiles-8-16.webm", "76a43fcdd7e658542913ea43216ec55d" },
270 { "vp90-2-14-resize-fp-tiles-8-1.webm", "8e3fbe89486ca60a59299dea9da91378" },
271 { "vp90-2-14-resize-fp-tiles-8-2.webm", "ae96f21f21b6370cc0125621b441fc52" },
272 { "vp90-2-14-resize-fp-tiles-8-4.webm", "3eb4f24f10640d42218f7fd7b9fd30d4" },
275 const FileParam kNonFrameParallelFiles[] = {
276 { "vp90-2-08-tile_1x2.webm", "570b4a5d5a70d58b5359671668328a16" },
277 { "vp90-2-08-tile_1x4.webm", "988d86049e884c66909d2d163a09841a" },
278 { "vp90-2-08-tile_1x8.webm", "0941902a52e9092cb010905eab16364c" },
279 { "vp90-2-08-tile-4x1.webm", "06505aade6647c583c8e00a2f582266f" },
280 { "vp90-2-08-tile-4x4.webm", "85c2299892460d76e2c600502d52bfe2" },
283 INSTANTIATE_TEST_SUITE_P(NoTilesNonFrameParallel, VP9DecodeMultiThreadedTest,
284 ::testing::ValuesIn(kNoTilesNonFrameParallelFiles));
285 INSTANTIATE_TEST_SUITE_P(FrameParallel, VP9DecodeMultiThreadedTest,
286 ::testing::ValuesIn(kFrameParallelFiles));
287 INSTANTIATE_TEST_SUITE_P(FrameParallelResize, VP9DecodeMultiThreadedTest,
288 ::testing::ValuesIn(kFrameParallelResizeFiles));
289 INSTANTIATE_TEST_SUITE_P(NonFrameParallel, VP9DecodeMultiThreadedTest,
290 ::testing::ValuesIn(kNonFrameParallelFiles));
291 #endif // CONFIG_WEBM_IO
293 INSTANTIATE_TEST_SUITE_P(Synchronous, VPxWorkerThreadTest, ::testing::Bool());