vp9_thread_test.cc 6.87 KB
Newer Older
James Zern's avatar
James Zern committed
1 2 3 4 5 6 7 8 9 10
/*
 *  Copyright (c) 2013 The WebM project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

11
#include <string>
James Zern's avatar
James Zern committed
12 13

#include "third_party/googletest/src/include/gtest/gtest.h"
14
#include "./vpx_config.h"
15 16 17
#include "test/codec_factory.h"
#include "test/decode_test_driver.h"
#include "test/md5_helper.h"
18
#if CONFIG_WEBM_IO
19
#include "test/webm_video_source.h"
20
#endif
21
#include "vp9/decoder/vp9_thread.h"
James Zern's avatar
James Zern committed
22 23 24

namespace {

25 26
using std::string;

27
class VP9WorkerThreadTest : public ::testing::TestWithParam<bool> {
James Zern's avatar
James Zern committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
 protected:
  virtual ~VP9WorkerThreadTest() {}
  virtual void SetUp() {
    vp9_worker_init(&worker_);
  }

  virtual void TearDown() {
    vp9_worker_end(&worker_);
  }

  VP9Worker worker_;
};

int ThreadHook(void* data, void* return_value) {
  int* const hook_data = reinterpret_cast<int*>(data);
  *hook_data = 5;
  return *reinterpret_cast<int*>(return_value);
}

47
TEST_P(VP9WorkerThreadTest, HookSuccess) {
48
  EXPECT_NE(vp9_worker_sync(&worker_), 0);  // should be a no-op.
James Zern's avatar
James Zern committed
49 50

  for (int i = 0; i < 2; ++i) {
51
    EXPECT_NE(vp9_worker_reset(&worker_), 0);
James Zern's avatar
James Zern committed
52 53 54 55 56 57 58

    int hook_data = 0;
    int return_value = 1;  // return successfully from the hook
    worker_.hook = ThreadHook;
    worker_.data1 = &hook_data;
    worker_.data2 = &return_value;

59 60 61 62 63 64
    const bool synchronous = GetParam();
    if (synchronous) {
      vp9_worker_execute(&worker_);
    } else {
      vp9_worker_launch(&worker_);
    }
65
    EXPECT_NE(vp9_worker_sync(&worker_), 0);
James Zern's avatar
James Zern committed
66 67 68
    EXPECT_FALSE(worker_.had_error);
    EXPECT_EQ(5, hook_data);

69
    EXPECT_NE(vp9_worker_sync(&worker_), 0);  // should be a no-op.
James Zern's avatar
James Zern committed
70 71 72
  }
}

73
TEST_P(VP9WorkerThreadTest, HookFailure) {
74
  EXPECT_NE(vp9_worker_reset(&worker_), 0);
James Zern's avatar
James Zern committed
75 76 77 78 79 80 81

  int hook_data = 0;
  int return_value = 0;  // return failure from the hook
  worker_.hook = ThreadHook;
  worker_.data1 = &hook_data;
  worker_.data2 = &return_value;

82 83 84 85 86 87
  const bool synchronous = GetParam();
  if (synchronous) {
    vp9_worker_execute(&worker_);
  } else {
    vp9_worker_launch(&worker_);
  }
James Zern's avatar
James Zern committed
88
  EXPECT_FALSE(vp9_worker_sync(&worker_));
89
  EXPECT_EQ(1, worker_.had_error);
James Zern's avatar
James Zern committed
90 91 92

  // Ensure _reset() clears the error and _launch() can be called again.
  return_value = 1;
93
  EXPECT_NE(vp9_worker_reset(&worker_), 0);
James Zern's avatar
James Zern committed
94 95
  EXPECT_FALSE(worker_.had_error);
  vp9_worker_launch(&worker_);
96
  EXPECT_NE(vp9_worker_sync(&worker_), 0);
James Zern's avatar
James Zern committed
97 98 99
  EXPECT_FALSE(worker_.had_error);
}

100 101 102
// -----------------------------------------------------------------------------
// Multi-threaded decode tests

103
#if CONFIG_WEBM_IO
104 105 106
// Decodes |filename| with |num_threads|. Returns the md5 of the decoded frames.
string DecodeFile(const string& filename, int num_threads) {
  libvpx_test::WebMVideoSource video(filename);
107 108 109
  video.Init();

  vpx_codec_dec_cfg_t cfg = {0};
110
  cfg.threads = num_threads;
111 112 113 114 115 116
  libvpx_test::VP9Decoder decoder(cfg, 0);

  libvpx_test::MD5 md5;
  for (video.Begin(); video.cxdata(); video.Next()) {
    const vpx_codec_err_t res =
        decoder.DecodeFrame(video.cxdata(), video.frame_size());
117 118 119 120
    if (res != VPX_CODEC_OK) {
      EXPECT_EQ(VPX_CODEC_OK, res) << decoder.DecodeError();
      break;
    }
121 122 123 124 125 126 127 128 129

    libvpx_test::DxDataIterator dec_iter = decoder.GetDxData();
    const vpx_image_t *img = NULL;

    // Get decompressed data
    while ((img = dec_iter.Next())) {
      md5.Add(img);
    }
  }
130 131 132
  return string(md5.Get());
}

133
TEST(VP9DecodeMultiThreadedTest, Decode) {
134 135 136 137 138
  // no tiles or frame parallel; this exercises loop filter threading.
  EXPECT_STREQ("b35a1b707b28e82be025d960aba039bc",
               DecodeFile("vp90-2-03-size-226x226.webm", 2).c_str());
}

139
TEST(VP9DecodeMultiThreadedTest, Decode2) {
140 141 142 143 144 145 146 147
  static const struct {
    const char *name;
    const char *expected_md5;
  } files[] = {
    { "vp90-2-08-tile_1x2_frame_parallel.webm",
      "68ede6abd66bae0a2edf2eb9232241b6" },
    { "vp90-2-08-tile_1x4_frame_parallel.webm",
      "368ebc6ebf3a5e478d85b2c3149b2848" },
James Zern's avatar
James Zern committed
148 149
    { "vp90-2-08-tile_1x8_frame_parallel.webm",
      "17e439da2388aff3a0f69cb22579c6c1" },
150 151 152
  };

  for (int i = 0; i < static_cast<int>(sizeof(files) / sizeof(files[0])); ++i) {
James Zern's avatar
James Zern committed
153
    for (int t = 2; t <= 8; ++t) {
154 155 156 157
      EXPECT_STREQ(files[i].expected_md5, DecodeFile(files[i].name, t).c_str())
          << "threads = " << t;
    }
  }
158 159
}

160
// Test tile quantity changes within one file.
161
TEST(VP9DecodeMultiThreadedTest, Decode3) {
162 163 164 165
  static const struct {
    const char *name;
    const char *expected_md5;
  } files[] = {
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
    { "vp90-2-14-resize-fp-tiles-1-16.webm",
      "0cd5e632c326297e975f38949c31ea94" },
    { "vp90-2-14-resize-fp-tiles-1-2-4-8-16.webm",
      "5c78a96a42e7f4a4f6b2edcdb791e44c" },
    { "vp90-2-14-resize-fp-tiles-1-2.webm",
      "e030450ae85c3277be2a418769df98e2" },
    { "vp90-2-14-resize-fp-tiles-1-4.webm",
      "312eed4e2b64eb7a4e7f18916606a430" },
    { "vp90-2-14-resize-fp-tiles-16-1.webm",
      "1755c16d8af16a9cb3fe7338d90abe52" },
    { "vp90-2-14-resize-fp-tiles-16-2.webm",
      "500300592d3fcb6f12fab25e48aaf4df" },
    { "vp90-2-14-resize-fp-tiles-16-4.webm",
      "47c48379fa6331215d91c67648e1af6e" },
    { "vp90-2-14-resize-fp-tiles-16-8-4-2-1.webm",
      "eecf17290739bc708506fa4827665989" },
    { "vp90-2-14-resize-fp-tiles-16-8.webm",
      "29b6bb54e4c26b5ca85d5de5fed94e76" },
    { "vp90-2-14-resize-fp-tiles-1-8.webm",
      "1b6f175e08cd82cf84bb800ac6d1caa3" },
    { "vp90-2-14-resize-fp-tiles-2-16.webm",
      "ca3b03e4197995d8d5444ede7a6c0804" },
    { "vp90-2-14-resize-fp-tiles-2-1.webm",
      "99aec065369d70bbb78ccdff65afed3f" },
    { "vp90-2-14-resize-fp-tiles-2-4.webm",
      "22d0ebdb49b87d2920a85aea32e1afd5" },
    { "vp90-2-14-resize-fp-tiles-2-8.webm",
      "c2115cf051c62e0f7db1d4a783831541" },
    { "vp90-2-14-resize-fp-tiles-4-16.webm",
      "c690d7e1719b31367564cac0af0939cb" },
    { "vp90-2-14-resize-fp-tiles-4-1.webm",
      "a926020b2cc3e15ad4cc271853a0ff26" },
    { "vp90-2-14-resize-fp-tiles-4-2.webm",
      "42699063d9e581f1993d0cf890c2be78" },
    { "vp90-2-14-resize-fp-tiles-4-8.webm",
      "7f76d96036382f45121e3d5aa6f8ec52" },
    { "vp90-2-14-resize-fp-tiles-8-16.webm",
      "76a43fcdd7e658542913ea43216ec55d" },
    { "vp90-2-14-resize-fp-tiles-8-1.webm",
      "8e3fbe89486ca60a59299dea9da91378" },
    { "vp90-2-14-resize-fp-tiles-8-2.webm",
      "ae96f21f21b6370cc0125621b441fc52" },
    { "vp90-2-14-resize-fp-tiles-8-4.webm",
      "3eb4f24f10640d42218f7fd7b9fd30d4" },
210 211 212 213 214 215 216 217 218
  };

  for (int i = 0; i < static_cast<int>(sizeof(files) / sizeof(files[0])); ++i) {
    for (int t = 2; t <= 8; ++t) {
      EXPECT_STREQ(files[i].expected_md5, DecodeFile(files[i].name, t).c_str())
          << "threads = " << t;
    }
  }
}
219
#endif  // CONFIG_WEBM_IO
220

221 222
INSTANTIATE_TEST_CASE_P(Synchronous, VP9WorkerThreadTest, ::testing::Bool());

James Zern's avatar
James Zern committed
223
}  // namespace