vp9_thread_test.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. * Copyright (c) 2013 The WebM project authors. All Rights Reserved.
  3. *
  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.
  9. */
  10. #include <string>
  11. #include "third_party/googletest/src/include/gtest/gtest.h"
  12. #include "./vpx_config.h"
  13. #include "test/codec_factory.h"
  14. #include "test/decode_test_driver.h"
  15. #include "test/md5_helper.h"
  16. #if CONFIG_WEBM_IO
  17. #include "test/webm_video_source.h"
  18. #endif
  19. #include "vpx_util/vpx_thread.h"
  20. namespace {
  21. using std::string;
  22. class VPxWorkerThreadTest : public ::testing::TestWithParam<bool> {
  23. protected:
  24. virtual ~VPxWorkerThreadTest() {}
  25. virtual void SetUp() { vpx_get_worker_interface()->init(&worker_); }
  26. virtual void TearDown() { vpx_get_worker_interface()->end(&worker_); }
  27. void Run(VPxWorker *worker) {
  28. const bool synchronous = GetParam();
  29. if (synchronous) {
  30. vpx_get_worker_interface()->execute(worker);
  31. } else {
  32. vpx_get_worker_interface()->launch(worker);
  33. }
  34. }
  35. VPxWorker worker_;
  36. };
  37. int ThreadHook(void *data, void *return_value) {
  38. int *const hook_data = reinterpret_cast<int *>(data);
  39. *hook_data = 5;
  40. return *reinterpret_cast<int *>(return_value);
  41. }
  42. TEST_P(VPxWorkerThreadTest, HookSuccess) {
  43. // should be a no-op.
  44. EXPECT_NE(vpx_get_worker_interface()->sync(&worker_), 0);
  45. for (int i = 0; i < 2; ++i) {
  46. EXPECT_NE(vpx_get_worker_interface()->reset(&worker_), 0);
  47. int hook_data = 0;
  48. int return_value = 1; // return successfully from the hook
  49. worker_.hook = ThreadHook;
  50. worker_.data1 = &hook_data;
  51. worker_.data2 = &return_value;
  52. Run(&worker_);
  53. EXPECT_NE(vpx_get_worker_interface()->sync(&worker_), 0);
  54. EXPECT_FALSE(worker_.had_error);
  55. EXPECT_EQ(5, hook_data);
  56. // should be a no-op.
  57. EXPECT_NE(vpx_get_worker_interface()->sync(&worker_), 0);
  58. }
  59. }
  60. TEST_P(VPxWorkerThreadTest, HookFailure) {
  61. EXPECT_NE(vpx_get_worker_interface()->reset(&worker_), 0);
  62. int hook_data = 0;
  63. int return_value = 0; // return failure from the hook
  64. worker_.hook = ThreadHook;
  65. worker_.data1 = &hook_data;
  66. worker_.data2 = &return_value;
  67. Run(&worker_);
  68. EXPECT_FALSE(vpx_get_worker_interface()->sync(&worker_));
  69. EXPECT_EQ(1, worker_.had_error);
  70. // Ensure _reset() clears the error and _launch() can be called again.
  71. return_value = 1;
  72. EXPECT_NE(vpx_get_worker_interface()->reset(&worker_), 0);
  73. EXPECT_FALSE(worker_.had_error);
  74. vpx_get_worker_interface()->launch(&worker_);
  75. EXPECT_NE(vpx_get_worker_interface()->sync(&worker_), 0);
  76. EXPECT_FALSE(worker_.had_error);
  77. }
  78. TEST_P(VPxWorkerThreadTest, EndWithoutSync) {
  79. // Create a large number of threads to increase the chances of detecting a
  80. // race. Doing more work in the hook is no guarantee as any race would occur
  81. // post hook execution in the main thread loop driver.
  82. static const int kNumWorkers = 64;
  83. VPxWorker workers[kNumWorkers];
  84. int hook_data[kNumWorkers];
  85. int return_value[kNumWorkers];
  86. for (int n = 0; n < kNumWorkers; ++n) {
  87. vpx_get_worker_interface()->init(&workers[n]);
  88. return_value[n] = 1; // return successfully from the hook
  89. workers[n].hook = ThreadHook;
  90. workers[n].data1 = &hook_data[n];
  91. workers[n].data2 = &return_value[n];
  92. }
  93. for (int i = 0; i < 2; ++i) {
  94. for (int n = 0; n < kNumWorkers; ++n) {
  95. EXPECT_NE(vpx_get_worker_interface()->reset(&workers[n]), 0);
  96. hook_data[n] = 0;
  97. }
  98. for (int n = 0; n < kNumWorkers; ++n) {
  99. Run(&workers[n]);
  100. }
  101. for (int n = kNumWorkers - 1; n >= 0; --n) {
  102. vpx_get_worker_interface()->end(&workers[n]);
  103. }
  104. }
  105. }
  106. TEST(VPxWorkerThreadTest, TestInterfaceAPI) {
  107. EXPECT_EQ(0, vpx_set_worker_interface(NULL));
  108. EXPECT_TRUE(vpx_get_worker_interface() != NULL);
  109. for (int i = 0; i < 6; ++i) {
  110. VPxWorkerInterface winterface = *vpx_get_worker_interface();
  111. switch (i) {
  112. default:
  113. case 0: winterface.init = NULL; break;
  114. case 1: winterface.reset = NULL; break;
  115. case 2: winterface.sync = NULL; break;
  116. case 3: winterface.launch = NULL; break;
  117. case 4: winterface.execute = NULL; break;
  118. case 5: winterface.end = NULL; break;
  119. }
  120. EXPECT_EQ(0, vpx_set_worker_interface(&winterface));
  121. }
  122. }
  123. // -----------------------------------------------------------------------------
  124. // Multi-threaded decode tests
  125. #if CONFIG_WEBM_IO
  126. struct FileList {
  127. const char *name;
  128. const char *expected_md5;
  129. };
  130. // Decodes |filename| with |num_threads|. Returns the md5 of the decoded frames.
  131. string DecodeFile(const string &filename, int num_threads) {
  132. libvpx_test::WebMVideoSource video(filename);
  133. video.Init();
  134. vpx_codec_dec_cfg_t cfg = vpx_codec_dec_cfg_t();
  135. cfg.threads = num_threads;
  136. libvpx_test::VP9Decoder decoder(cfg, 0);
  137. libvpx_test::MD5 md5;
  138. for (video.Begin(); video.cxdata(); video.Next()) {
  139. const vpx_codec_err_t res =
  140. decoder.DecodeFrame(video.cxdata(), video.frame_size());
  141. if (res != VPX_CODEC_OK) {
  142. EXPECT_EQ(VPX_CODEC_OK, res) << decoder.DecodeError();
  143. break;
  144. }
  145. libvpx_test::DxDataIterator dec_iter = decoder.GetDxData();
  146. const vpx_image_t *img = NULL;
  147. // Get decompressed data
  148. while ((img = dec_iter.Next())) {
  149. md5.Add(img);
  150. }
  151. }
  152. return string(md5.Get());
  153. }
  154. void DecodeFiles(const FileList files[]) {
  155. for (const FileList *iter = files; iter->name != NULL; ++iter) {
  156. SCOPED_TRACE(iter->name);
  157. for (int t = 1; t <= 8; ++t) {
  158. EXPECT_EQ(iter->expected_md5, DecodeFile(iter->name, t))
  159. << "threads = " << t;
  160. }
  161. }
  162. }
  163. // Trivial serialized thread worker interface implementation.
  164. // Note any worker that requires synchronization between other workers will
  165. // hang.
  166. namespace impl {
  167. namespace {
  168. void Init(VPxWorker *const worker) { memset(worker, 0, sizeof(*worker)); }
  169. int Reset(VPxWorker *const /*worker*/) { return 1; }
  170. int Sync(VPxWorker *const worker) { return !worker->had_error; }
  171. void Execute(VPxWorker *const worker) {
  172. worker->had_error |= !worker->hook(worker->data1, worker->data2);
  173. }
  174. void Launch(VPxWorker *const worker) { Execute(worker); }
  175. void End(VPxWorker *const /*worker*/) {}
  176. } // namespace
  177. } // namespace impl
  178. TEST(VPxWorkerThreadTest, TestSerialInterface) {
  179. static const VPxWorkerInterface serial_interface = {
  180. impl::Init, impl::Reset, impl::Sync, impl::Launch, impl::Execute, impl::End
  181. };
  182. // TODO(jzern): Avoid using a file that will use the row-based thread
  183. // loopfilter, with the simple serialized implementation it will hang. This is
  184. // due to its expectation that rows will be run in parallel as they wait on
  185. // progress in the row above before proceeding.
  186. static const char expected_md5[] = "b35a1b707b28e82be025d960aba039bc";
  187. static const char filename[] = "vp90-2-03-size-226x226.webm";
  188. VPxWorkerInterface default_interface = *vpx_get_worker_interface();
  189. EXPECT_NE(vpx_set_worker_interface(&serial_interface), 0);
  190. EXPECT_EQ(expected_md5, DecodeFile(filename, 2));
  191. // Reset the interface.
  192. EXPECT_NE(vpx_set_worker_interface(&default_interface), 0);
  193. EXPECT_EQ(expected_md5, DecodeFile(filename, 2));
  194. }
  195. TEST(VP9DecodeMultiThreadedTest, NoTilesNonFrameParallel) {
  196. // no tiles or frame parallel; this exercises loop filter threading.
  197. EXPECT_EQ("b35a1b707b28e82be025d960aba039bc",
  198. DecodeFile("vp90-2-03-size-226x226.webm", 2));
  199. }
  200. TEST(VP9DecodeMultiThreadedTest, FrameParallel) {
  201. static const FileList files[] = { { "vp90-2-08-tile_1x2_frame_parallel.webm",
  202. "68ede6abd66bae0a2edf2eb9232241b6" },
  203. { "vp90-2-08-tile_1x4_frame_parallel.webm",
  204. "368ebc6ebf3a5e478d85b2c3149b2848" },
  205. { "vp90-2-08-tile_1x8_frame_parallel.webm",
  206. "17e439da2388aff3a0f69cb22579c6c1" },
  207. { NULL, NULL } };
  208. DecodeFiles(files);
  209. }
  210. TEST(VP9DecodeMultiThreadedTest, FrameParallelResize) {
  211. static const FileList files[] = {
  212. { "vp90-2-14-resize-fp-tiles-1-16.webm",
  213. "0cd5e632c326297e975f38949c31ea94" },
  214. { "vp90-2-14-resize-fp-tiles-1-2-4-8-16.webm",
  215. "5c78a96a42e7f4a4f6b2edcdb791e44c" },
  216. { "vp90-2-14-resize-fp-tiles-1-2.webm",
  217. "e030450ae85c3277be2a418769df98e2" },
  218. { "vp90-2-14-resize-fp-tiles-1-4.webm",
  219. "312eed4e2b64eb7a4e7f18916606a430" },
  220. { "vp90-2-14-resize-fp-tiles-16-1.webm",
  221. "1755c16d8af16a9cb3fe7338d90abe52" },
  222. { "vp90-2-14-resize-fp-tiles-16-2.webm",
  223. "500300592d3fcb6f12fab25e48aaf4df" },
  224. { "vp90-2-14-resize-fp-tiles-16-4.webm",
  225. "47c48379fa6331215d91c67648e1af6e" },
  226. { "vp90-2-14-resize-fp-tiles-16-8-4-2-1.webm",
  227. "eecf17290739bc708506fa4827665989" },
  228. { "vp90-2-14-resize-fp-tiles-16-8.webm",
  229. "29b6bb54e4c26b5ca85d5de5fed94e76" },
  230. { "vp90-2-14-resize-fp-tiles-1-8.webm",
  231. "1b6f175e08cd82cf84bb800ac6d1caa3" },
  232. { "vp90-2-14-resize-fp-tiles-2-16.webm",
  233. "ca3b03e4197995d8d5444ede7a6c0804" },
  234. { "vp90-2-14-resize-fp-tiles-2-1.webm",
  235. "99aec065369d70bbb78ccdff65afed3f" },
  236. { "vp90-2-14-resize-fp-tiles-2-4.webm",
  237. "22d0ebdb49b87d2920a85aea32e1afd5" },
  238. { "vp90-2-14-resize-fp-tiles-2-8.webm",
  239. "c2115cf051c62e0f7db1d4a783831541" },
  240. { "vp90-2-14-resize-fp-tiles-4-16.webm",
  241. "c690d7e1719b31367564cac0af0939cb" },
  242. { "vp90-2-14-resize-fp-tiles-4-1.webm",
  243. "a926020b2cc3e15ad4cc271853a0ff26" },
  244. { "vp90-2-14-resize-fp-tiles-4-2.webm",
  245. "42699063d9e581f1993d0cf890c2be78" },
  246. { "vp90-2-14-resize-fp-tiles-4-8.webm",
  247. "7f76d96036382f45121e3d5aa6f8ec52" },
  248. { "vp90-2-14-resize-fp-tiles-8-16.webm",
  249. "76a43fcdd7e658542913ea43216ec55d" },
  250. { "vp90-2-14-resize-fp-tiles-8-1.webm",
  251. "8e3fbe89486ca60a59299dea9da91378" },
  252. { "vp90-2-14-resize-fp-tiles-8-2.webm",
  253. "ae96f21f21b6370cc0125621b441fc52" },
  254. { "vp90-2-14-resize-fp-tiles-8-4.webm",
  255. "3eb4f24f10640d42218f7fd7b9fd30d4" },
  256. { NULL, NULL }
  257. };
  258. DecodeFiles(files);
  259. }
  260. TEST(VP9DecodeMultiThreadedTest, NonFrameParallel) {
  261. static const FileList files[] = {
  262. { "vp90-2-08-tile_1x2.webm", "570b4a5d5a70d58b5359671668328a16" },
  263. { "vp90-2-08-tile_1x4.webm", "988d86049e884c66909d2d163a09841a" },
  264. { "vp90-2-08-tile_1x8.webm", "0941902a52e9092cb010905eab16364c" },
  265. { "vp90-2-08-tile-4x1.webm", "06505aade6647c583c8e00a2f582266f" },
  266. { "vp90-2-08-tile-4x4.webm", "85c2299892460d76e2c600502d52bfe2" },
  267. { NULL, NULL }
  268. };
  269. DecodeFiles(files);
  270. }
  271. #endif // CONFIG_WEBM_IO
  272. INSTANTIATE_TEST_CASE_P(Synchronous, VPxWorkerThreadTest, ::testing::Bool());
  273. } // namespace