2 * Copyright (c) 2014 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.
14 #include "./vpx_config.h"
15 #include "test/codec_factory.h"
16 #include "test/decode_test_driver.h"
17 #include "test/ivf_video_source.h"
18 #include "test/md5_helper.h"
19 #include "test/test_vectors.h"
20 #include "test/util.h"
22 #include "test/webm_video_source.h"
27 const int kVideoNameParam = 1;
29 struct ExternalFrameBuffer {
35 // Class to manipulate a list of external frame buffers.
36 class ExternalFrameBufferList {
38 ExternalFrameBufferList()
39 : num_buffers_(0), num_used_buffers_(0), ext_fb_list_(nullptr) {}
41 virtual ~ExternalFrameBufferList() {
42 for (int i = 0; i < num_buffers_; ++i) {
43 delete[] ext_fb_list_[i].data;
45 delete[] ext_fb_list_;
48 // Creates the list to hold the external buffers. Returns true on success.
49 bool CreateBufferList(int num_buffers) {
50 if (num_buffers < 0) return false;
52 num_buffers_ = num_buffers;
53 ext_fb_list_ = new ExternalFrameBuffer[num_buffers_];
54 EXPECT_NE(ext_fb_list_, nullptr);
55 memset(ext_fb_list_, 0, sizeof(ext_fb_list_[0]) * num_buffers_);
59 // Searches the frame buffer list for a free frame buffer. Makes sure
60 // that the frame buffer is at least |min_size| in bytes. Marks that the
61 // frame buffer is in use by libvpx. Finally sets |fb| to point to the
62 // external frame buffer. Returns < 0 on an error.
63 int GetFreeFrameBuffer(size_t min_size, vpx_codec_frame_buffer_t *fb) {
64 EXPECT_NE(fb, nullptr);
65 const int idx = FindFreeBufferIndex();
66 if (idx == num_buffers_) return -1;
68 if (ext_fb_list_[idx].size < min_size) {
69 delete[] ext_fb_list_[idx].data;
70 ext_fb_list_[idx].data = new uint8_t[min_size];
71 memset(ext_fb_list_[idx].data, 0, min_size);
72 ext_fb_list_[idx].size = min_size;
75 SetFrameBuffer(idx, fb);
81 // Test function that will not allocate any data for the frame buffer.
82 // Returns < 0 on an error.
83 int GetZeroFrameBuffer(size_t min_size, vpx_codec_frame_buffer_t *fb) {
84 EXPECT_NE(fb, nullptr);
85 const int idx = FindFreeBufferIndex();
86 if (idx == num_buffers_) return -1;
88 if (ext_fb_list_[idx].size < min_size) {
89 delete[] ext_fb_list_[idx].data;
90 ext_fb_list_[idx].data = nullptr;
91 ext_fb_list_[idx].size = min_size;
94 SetFrameBuffer(idx, fb);
98 // Marks the external frame buffer that |fb| is pointing to as free.
99 // Returns < 0 on an error.
100 int ReturnFrameBuffer(vpx_codec_frame_buffer_t *fb) {
102 EXPECT_NE(fb, nullptr);
105 ExternalFrameBuffer *const ext_fb =
106 reinterpret_cast<ExternalFrameBuffer *>(fb->priv);
107 if (ext_fb == nullptr) {
108 EXPECT_NE(ext_fb, nullptr);
111 EXPECT_EQ(1, ext_fb->in_use);
117 // Checks that the vpx_image_t data is contained within the external frame
118 // buffer private data passed back in the vpx_image_t.
119 void CheckImageFrameBuffer(const vpx_image_t *img) {
120 if (img->fb_priv != nullptr) {
121 const struct ExternalFrameBuffer *const ext_fb =
122 reinterpret_cast<ExternalFrameBuffer *>(img->fb_priv);
124 ASSERT_TRUE(img->planes[0] >= ext_fb->data &&
125 img->planes[0] < (ext_fb->data + ext_fb->size));
129 int num_used_buffers() const { return num_used_buffers_; }
132 // Returns the index of the first free frame buffer. Returns |num_buffers_|
133 // if there are no free frame buffers.
134 int FindFreeBufferIndex() {
136 // Find a free frame buffer.
137 for (i = 0; i < num_buffers_; ++i) {
138 if (!ext_fb_list_[i].in_use) break;
143 // Sets |fb| to an external frame buffer. idx is the index into the frame
145 void SetFrameBuffer(int idx, vpx_codec_frame_buffer_t *fb) {
146 ASSERT_NE(fb, nullptr);
147 fb->data = ext_fb_list_[idx].data;
148 fb->size = ext_fb_list_[idx].size;
149 ASSERT_EQ(0, ext_fb_list_[idx].in_use);
150 ext_fb_list_[idx].in_use = 1;
151 fb->priv = &ext_fb_list_[idx];
155 int num_used_buffers_;
156 ExternalFrameBuffer *ext_fb_list_;
161 // Callback used by libvpx to request the application to return a frame
162 // buffer of at least |min_size| in bytes.
163 int get_vp9_frame_buffer(void *user_priv, size_t min_size,
164 vpx_codec_frame_buffer_t *fb) {
165 ExternalFrameBufferList *const fb_list =
166 reinterpret_cast<ExternalFrameBufferList *>(user_priv);
167 return fb_list->GetFreeFrameBuffer(min_size, fb);
170 // Callback used by libvpx to tell the application that |fb| is not needed
172 int release_vp9_frame_buffer(void *user_priv, vpx_codec_frame_buffer_t *fb) {
173 ExternalFrameBufferList *const fb_list =
174 reinterpret_cast<ExternalFrameBufferList *>(user_priv);
175 return fb_list->ReturnFrameBuffer(fb);
178 // Callback will not allocate data for frame buffer.
179 int get_vp9_zero_frame_buffer(void *user_priv, size_t min_size,
180 vpx_codec_frame_buffer_t *fb) {
181 ExternalFrameBufferList *const fb_list =
182 reinterpret_cast<ExternalFrameBufferList *>(user_priv);
183 return fb_list->GetZeroFrameBuffer(min_size, fb);
186 // Callback will allocate one less byte than |min_size|.
187 int get_vp9_one_less_byte_frame_buffer(void *user_priv, size_t min_size,
188 vpx_codec_frame_buffer_t *fb) {
189 ExternalFrameBufferList *const fb_list =
190 reinterpret_cast<ExternalFrameBufferList *>(user_priv);
191 return fb_list->GetFreeFrameBuffer(min_size - 1, fb);
194 // Callback will not release the external frame buffer.
195 int do_not_release_vp9_frame_buffer(void *user_priv,
196 vpx_codec_frame_buffer_t *fb) {
202 #endif // CONFIG_WEBM_IO
204 // Class for testing passing in external frame buffers to libvpx.
205 class ExternalFrameBufferMD5Test
206 : public ::libvpx_test::DecoderTest,
207 public ::libvpx_test::CodecTestWithParam<const char *> {
209 ExternalFrameBufferMD5Test()
210 : DecoderTest(GET_PARAM(::libvpx_test::kCodecFactoryParam)),
211 md5_file_(nullptr), num_buffers_(0) {}
213 virtual ~ExternalFrameBufferMD5Test() {
214 if (md5_file_ != nullptr) fclose(md5_file_);
217 virtual void PreDecodeFrameHook(
218 const libvpx_test::CompressedVideoSource &video,
219 libvpx_test::Decoder *decoder) {
220 if (num_buffers_ > 0 && video.frame_number() == 0) {
221 // Have libvpx use frame buffers we create.
222 ASSERT_TRUE(fb_list_.CreateBufferList(num_buffers_));
223 ASSERT_EQ(VPX_CODEC_OK,
224 decoder->SetFrameBufferFunctions(GetVP9FrameBuffer,
225 ReleaseVP9FrameBuffer, this));
229 void OpenMD5File(const std::string &md5_file_name_) {
230 md5_file_ = libvpx_test::OpenTestDataFile(md5_file_name_);
231 ASSERT_NE(md5_file_, nullptr)
232 << "Md5 file open failed. Filename: " << md5_file_name_;
235 virtual void DecompressedFrameHook(const vpx_image_t &img,
236 const unsigned int frame_number) {
237 ASSERT_NE(md5_file_, nullptr);
238 char expected_md5[33];
241 // Read correct md5 checksums.
242 const int res = fscanf(md5_file_, "%s %s", expected_md5, junk);
243 ASSERT_NE(EOF, res) << "Read md5 data failed";
244 expected_md5[32] = '\0';
246 ::libvpx_test::MD5 md5_res;
248 const char *const actual_md5 = md5_res.Get();
251 ASSERT_STREQ(expected_md5, actual_md5)
252 << "Md5 checksums don't match: frame number = " << frame_number;
255 // Callback to get a free external frame buffer. Return value < 0 is an
257 static int GetVP9FrameBuffer(void *user_priv, size_t min_size,
258 vpx_codec_frame_buffer_t *fb) {
259 ExternalFrameBufferMD5Test *const md5Test =
260 reinterpret_cast<ExternalFrameBufferMD5Test *>(user_priv);
261 return md5Test->fb_list_.GetFreeFrameBuffer(min_size, fb);
264 // Callback to release an external frame buffer. Return value < 0 is an
266 static int ReleaseVP9FrameBuffer(void *user_priv,
267 vpx_codec_frame_buffer_t *fb) {
268 ExternalFrameBufferMD5Test *const md5Test =
269 reinterpret_cast<ExternalFrameBufferMD5Test *>(user_priv);
270 return md5Test->fb_list_.ReturnFrameBuffer(fb);
273 void set_num_buffers(int num_buffers) { num_buffers_ = num_buffers; }
274 int num_buffers() const { return num_buffers_; }
279 ExternalFrameBufferList fb_list_;
283 const char kVP9TestFile[] = "vp90-2-02-size-lf-1920x1080.webm";
284 const char kVP9NonRefTestFile[] = "vp90-2-22-svc_1280x720_1.webm";
286 // Class for testing passing in external frame buffers to libvpx.
287 class ExternalFrameBufferTest : public ::testing::Test {
289 ExternalFrameBufferTest()
290 : video_(nullptr), decoder_(nullptr), num_buffers_(0) {}
292 virtual void SetUp() {
293 video_ = new libvpx_test::WebMVideoSource(kVP9TestFile);
294 ASSERT_NE(video_, nullptr);
298 vpx_codec_dec_cfg_t cfg = vpx_codec_dec_cfg_t();
299 decoder_ = new libvpx_test::VP9Decoder(cfg, 0);
300 ASSERT_NE(decoder_, nullptr);
303 virtual void TearDown() {
310 // Passes the external frame buffer information to libvpx.
311 vpx_codec_err_t SetFrameBufferFunctions(
312 int num_buffers, vpx_get_frame_buffer_cb_fn_t cb_get,
313 vpx_release_frame_buffer_cb_fn_t cb_release) {
314 if (num_buffers > 0) {
315 num_buffers_ = num_buffers;
316 EXPECT_TRUE(fb_list_.CreateBufferList(num_buffers_));
319 return decoder_->SetFrameBufferFunctions(cb_get, cb_release, &fb_list_);
322 vpx_codec_err_t DecodeOneFrame() {
323 const vpx_codec_err_t res =
324 decoder_->DecodeFrame(video_->cxdata(), video_->frame_size());
325 CheckDecodedFrames();
326 if (res == VPX_CODEC_OK) video_->Next();
330 vpx_codec_err_t DecodeRemainingFrames() {
331 for (; video_->cxdata() != nullptr; video_->Next()) {
332 const vpx_codec_err_t res =
333 decoder_->DecodeFrame(video_->cxdata(), video_->frame_size());
334 if (res != VPX_CODEC_OK) return res;
335 CheckDecodedFrames();
340 void CheckDecodedFrames() {
341 libvpx_test::DxDataIterator dec_iter = decoder_->GetDxData();
342 const vpx_image_t *img = nullptr;
344 // Get decompressed data
345 while ((img = dec_iter.Next()) != nullptr) {
346 fb_list_.CheckImageFrameBuffer(img);
350 libvpx_test::WebMVideoSource *video_;
351 libvpx_test::VP9Decoder *decoder_;
353 ExternalFrameBufferList fb_list_;
356 class ExternalFrameBufferNonRefTest : public ExternalFrameBufferTest {
358 virtual void SetUp() {
359 video_ = new libvpx_test::WebMVideoSource(kVP9NonRefTestFile);
360 ASSERT_NE(video_, nullptr);
364 vpx_codec_dec_cfg_t cfg = vpx_codec_dec_cfg_t();
365 decoder_ = new libvpx_test::VP9Decoder(cfg, 0);
366 ASSERT_NE(decoder_, nullptr);
369 virtual void CheckFrameBufferRelease() {
371 ASSERT_EQ(0, fb_list_.num_used_buffers());
374 #endif // CONFIG_WEBM_IO
376 // This test runs through the set of test vectors, and decodes them.
377 // Libvpx will call into the application to allocate a frame buffer when
378 // needed. The md5 checksums are computed for each frame in the video file.
379 // If md5 checksums match the correct md5 data, then the test is passed.
380 // Otherwise, the test failed.
381 TEST_P(ExternalFrameBufferMD5Test, ExtFBMD5Match) {
382 const std::string filename = GET_PARAM(kVideoNameParam);
384 // Number of buffers equals #VP9_MAXIMUM_REF_BUFFERS +
385 // #VPX_MAXIMUM_WORK_BUFFERS + four jitter buffers.
386 const int jitter_buffers = 4;
387 const int num_buffers =
388 VP9_MAXIMUM_REF_BUFFERS + VPX_MAXIMUM_WORK_BUFFERS + jitter_buffers;
389 set_num_buffers(num_buffers);
391 #if CONFIG_VP8_DECODER
392 // Tell compiler we are not using kVP8TestVectors.
393 (void)libvpx_test::kVP8TestVectors;
396 // Open compressed video file.
397 std::unique_ptr<libvpx_test::CompressedVideoSource> video;
398 if (filename.substr(filename.length() - 3, 3) == "ivf") {
399 video.reset(new libvpx_test::IVFVideoSource(filename));
402 video.reset(new libvpx_test::WebMVideoSource(filename));
404 fprintf(stderr, "WebM IO is disabled, skipping test vector %s\n",
409 ASSERT_NE(video.get(), nullptr);
412 // Construct md5 file name.
413 const std::string md5_filename = filename + ".md5";
414 OpenMD5File(md5_filename);
416 // Decode frame, and check the md5 matching.
417 ASSERT_NO_FATAL_FAILURE(RunLoop(video.get()));
421 TEST_F(ExternalFrameBufferTest, MinFrameBuffers) {
422 // Minimum number of external frame buffers for VP9 is
423 // #VP9_MAXIMUM_REF_BUFFERS + #VPX_MAXIMUM_WORK_BUFFERS.
424 const int num_buffers = VP9_MAXIMUM_REF_BUFFERS + VPX_MAXIMUM_WORK_BUFFERS;
425 ASSERT_EQ(VPX_CODEC_OK,
426 SetFrameBufferFunctions(num_buffers, get_vp9_frame_buffer,
427 release_vp9_frame_buffer));
428 ASSERT_EQ(VPX_CODEC_OK, DecodeRemainingFrames());
431 TEST_F(ExternalFrameBufferTest, EightJitterBuffers) {
432 // Number of buffers equals #VP9_MAXIMUM_REF_BUFFERS +
433 // #VPX_MAXIMUM_WORK_BUFFERS + eight jitter buffers.
434 const int jitter_buffers = 8;
435 const int num_buffers =
436 VP9_MAXIMUM_REF_BUFFERS + VPX_MAXIMUM_WORK_BUFFERS + jitter_buffers;
437 ASSERT_EQ(VPX_CODEC_OK,
438 SetFrameBufferFunctions(num_buffers, get_vp9_frame_buffer,
439 release_vp9_frame_buffer));
440 ASSERT_EQ(VPX_CODEC_OK, DecodeRemainingFrames());
443 TEST_F(ExternalFrameBufferTest, NotEnoughBuffers) {
444 // Minimum number of external frame buffers for VP9 is
445 // #VP9_MAXIMUM_REF_BUFFERS + #VPX_MAXIMUM_WORK_BUFFERS. Most files will
446 // only use 5 frame buffers at one time.
447 const int num_buffers = 2;
448 ASSERT_EQ(VPX_CODEC_OK,
449 SetFrameBufferFunctions(num_buffers, get_vp9_frame_buffer,
450 release_vp9_frame_buffer));
451 ASSERT_EQ(VPX_CODEC_OK, DecodeOneFrame());
452 // Only run this on long clips. Decoding a very short clip will return
453 // VPX_CODEC_OK even with only 2 buffers.
454 ASSERT_EQ(VPX_CODEC_MEM_ERROR, DecodeRemainingFrames());
457 TEST_F(ExternalFrameBufferTest, NoRelease) {
458 const int num_buffers = VP9_MAXIMUM_REF_BUFFERS + VPX_MAXIMUM_WORK_BUFFERS;
459 ASSERT_EQ(VPX_CODEC_OK,
460 SetFrameBufferFunctions(num_buffers, get_vp9_frame_buffer,
461 do_not_release_vp9_frame_buffer));
462 ASSERT_EQ(VPX_CODEC_OK, DecodeOneFrame());
463 ASSERT_EQ(VPX_CODEC_MEM_ERROR, DecodeRemainingFrames());
466 TEST_F(ExternalFrameBufferTest, NullRealloc) {
467 const int num_buffers = VP9_MAXIMUM_REF_BUFFERS + VPX_MAXIMUM_WORK_BUFFERS;
468 ASSERT_EQ(VPX_CODEC_OK,
469 SetFrameBufferFunctions(num_buffers, get_vp9_zero_frame_buffer,
470 release_vp9_frame_buffer));
471 ASSERT_EQ(VPX_CODEC_MEM_ERROR, DecodeOneFrame());
474 TEST_F(ExternalFrameBufferTest, ReallocOneLessByte) {
475 const int num_buffers = VP9_MAXIMUM_REF_BUFFERS + VPX_MAXIMUM_WORK_BUFFERS;
476 ASSERT_EQ(VPX_CODEC_OK, SetFrameBufferFunctions(
477 num_buffers, get_vp9_one_less_byte_frame_buffer,
478 release_vp9_frame_buffer));
479 ASSERT_EQ(VPX_CODEC_MEM_ERROR, DecodeOneFrame());
482 TEST_F(ExternalFrameBufferTest, NullGetFunction) {
483 const int num_buffers = VP9_MAXIMUM_REF_BUFFERS + VPX_MAXIMUM_WORK_BUFFERS;
485 VPX_CODEC_INVALID_PARAM,
486 SetFrameBufferFunctions(num_buffers, nullptr, release_vp9_frame_buffer));
489 TEST_F(ExternalFrameBufferTest, NullReleaseFunction) {
490 const int num_buffers = VP9_MAXIMUM_REF_BUFFERS + VPX_MAXIMUM_WORK_BUFFERS;
492 VPX_CODEC_INVALID_PARAM,
493 SetFrameBufferFunctions(num_buffers, get_vp9_frame_buffer, nullptr));
496 TEST_F(ExternalFrameBufferTest, SetAfterDecode) {
497 const int num_buffers = VP9_MAXIMUM_REF_BUFFERS + VPX_MAXIMUM_WORK_BUFFERS;
498 ASSERT_EQ(VPX_CODEC_OK, DecodeOneFrame());
499 ASSERT_EQ(VPX_CODEC_ERROR,
500 SetFrameBufferFunctions(num_buffers, get_vp9_frame_buffer,
501 release_vp9_frame_buffer));
504 TEST_F(ExternalFrameBufferNonRefTest, ReleaseNonRefFrameBuffer) {
505 const int num_buffers = VP9_MAXIMUM_REF_BUFFERS + VPX_MAXIMUM_WORK_BUFFERS;
506 ASSERT_EQ(VPX_CODEC_OK,
507 SetFrameBufferFunctions(num_buffers, get_vp9_frame_buffer,
508 release_vp9_frame_buffer));
509 ASSERT_EQ(VPX_CODEC_OK, DecodeRemainingFrames());
510 CheckFrameBufferRelease();
512 #endif // CONFIG_WEBM_IO
514 VP9_INSTANTIATE_TEST_SUITE(
515 ExternalFrameBufferMD5Test,
516 ::testing::ValuesIn(libvpx_test::kVP9TestVectors,
517 libvpx_test::kVP9TestVectors +
518 libvpx_test::kNumVP9TestVectors));