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.
13 #include "./vpx_config.h"
14 #include "test/codec_factory.h"
15 #include "test/decode_test_driver.h"
16 #include "test/ivf_video_source.h"
17 #include "test/md5_helper.h"
18 #include "test/test_vectors.h"
19 #include "test/util.h"
21 #include "test/webm_video_source.h"
26 const int kVideoNameParam = 1;
27 const char kVP9TestFile[] = "vp90-2-02-size-lf-1920x1080.webm";
29 struct ExternalFrameBuffer {
35 // Class to manipulate a list of external frame buffers.
36 class ExternalFrameBufferList {
38 ExternalFrameBufferList()
42 virtual ~ExternalFrameBufferList() {
43 for (int i = 0; i < num_buffers_; ++i) {
44 delete [] ext_fb_list_[i].data;
46 delete [] ext_fb_list_;
49 // Creates the list to hold the external buffers. Returns true on success.
50 bool CreateBufferList(int num_buffers) {
54 num_buffers_ = num_buffers;
55 ext_fb_list_ = new ExternalFrameBuffer[num_buffers_];
56 EXPECT_TRUE(ext_fb_list_ != NULL);
57 memset(ext_fb_list_, 0, sizeof(ext_fb_list_[0]) * num_buffers_);
61 // Searches the frame buffer list for a free frame buffer. Makes sure
62 // that the frame buffer is at least |min_size| in bytes. Marks that the
63 // frame buffer is in use by libvpx. Finally sets |fb| to point to the
64 // external frame buffer. Returns < 0 on an error.
65 int GetFreeFrameBuffer(size_t min_size, vpx_codec_frame_buffer_t *fb) {
66 EXPECT_TRUE(fb != NULL);
67 const int idx = FindFreeBufferIndex();
68 if (idx == num_buffers_)
71 if (ext_fb_list_[idx].size < min_size) {
72 delete [] ext_fb_list_[idx].data;
73 ext_fb_list_[idx].data = new uint8_t[min_size];
74 memset(ext_fb_list_[idx].data, 0, min_size);
75 ext_fb_list_[idx].size = min_size;
78 SetFrameBuffer(idx, fb);
82 // Test function that will not allocate any data for the frame buffer.
83 // Returns < 0 on an error.
84 int GetZeroFrameBuffer(size_t min_size, vpx_codec_frame_buffer_t *fb) {
85 EXPECT_TRUE(fb != NULL);
86 const int idx = FindFreeBufferIndex();
87 if (idx == num_buffers_)
90 if (ext_fb_list_[idx].size < min_size) {
91 delete [] ext_fb_list_[idx].data;
92 ext_fb_list_[idx].data = NULL;
93 ext_fb_list_[idx].size = min_size;
96 SetFrameBuffer(idx, fb);
100 // Marks the external frame buffer that |fb| is pointing to as free.
101 // Returns < 0 on an error.
102 int ReturnFrameBuffer(vpx_codec_frame_buffer_t *fb) {
104 EXPECT_TRUE(fb != NULL);
107 ExternalFrameBuffer *const ext_fb =
108 reinterpret_cast<ExternalFrameBuffer*>(fb->priv);
109 if (ext_fb == NULL) {
110 EXPECT_TRUE(ext_fb != NULL);
113 EXPECT_EQ(1, ext_fb->in_use);
118 // Checks that the ximage data is contained within the external frame buffer
119 // private data passed back in the ximage.
120 void CheckXImageFrameBuffer(const vpx_image_t *img) {
121 if (img->fb_priv != NULL) {
122 const struct ExternalFrameBuffer *const ext_fb =
123 reinterpret_cast<ExternalFrameBuffer*>(img->fb_priv);
125 ASSERT_TRUE(img->planes[0] >= ext_fb->data &&
126 img->planes[0] < (ext_fb->data + ext_fb->size));
131 // Returns the index of the first free frame buffer. Returns |num_buffers_|
132 // if there are no free frame buffers.
133 int FindFreeBufferIndex() {
135 // Find a free frame buffer.
136 for (i = 0; i < num_buffers_; ++i) {
137 if (!ext_fb_list_[i].in_use)
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_TRUE(fb != NULL);
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 ExternalFrameBuffer *ext_fb_list_;
158 // Callback used by libvpx to request the application to return a frame
159 // buffer of at least |min_size| in bytes.
160 int get_vp9_frame_buffer(void *user_priv, size_t min_size,
161 vpx_codec_frame_buffer_t *fb) {
162 ExternalFrameBufferList *const fb_list =
163 reinterpret_cast<ExternalFrameBufferList*>(user_priv);
164 return fb_list->GetFreeFrameBuffer(min_size, fb);
167 // Callback used by libvpx to tell the application that |fb| is not needed
169 int release_vp9_frame_buffer(void *user_priv,
170 vpx_codec_frame_buffer_t *fb) {
171 ExternalFrameBufferList *const fb_list =
172 reinterpret_cast<ExternalFrameBufferList*>(user_priv);
173 return fb_list->ReturnFrameBuffer(fb);
176 // Callback will not allocate data for frame buffer.
177 int get_vp9_zero_frame_buffer(void *user_priv, size_t min_size,
178 vpx_codec_frame_buffer_t *fb) {
179 ExternalFrameBufferList *const fb_list =
180 reinterpret_cast<ExternalFrameBufferList*>(user_priv);
181 return fb_list->GetZeroFrameBuffer(min_size, fb);
184 // Callback will allocate one less byte than |min_size|.
185 int get_vp9_one_less_byte_frame_buffer(void *user_priv, size_t min_size,
186 vpx_codec_frame_buffer_t *fb) {
187 ExternalFrameBufferList *const fb_list =
188 reinterpret_cast<ExternalFrameBufferList*>(user_priv);
189 return fb_list->GetFreeFrameBuffer(min_size - 1, fb);
192 // Callback will not release the external frame buffer.
193 int do_not_release_vp9_frame_buffer(void *user_priv,
194 vpx_codec_frame_buffer_t *fb) {
200 // Class for testing passing in external frame buffers to libvpx.
201 class ExternalFrameBufferMD5Test
202 : public ::libvpx_test::DecoderTest,
203 public ::libvpx_test::CodecTestWithParam<const char*> {
205 ExternalFrameBufferMD5Test()
206 : DecoderTest(GET_PARAM(::libvpx_test::kCodecFactoryParam)),
210 virtual ~ExternalFrameBufferMD5Test() {
211 if (md5_file_ != NULL)
215 virtual void PreDecodeFrameHook(
216 const libvpx_test::CompressedVideoSource &video,
217 libvpx_test::Decoder *decoder) {
218 if (num_buffers_ > 0 && video.frame_number() == 0) {
219 // Have libvpx use frame buffers we create.
220 ASSERT_TRUE(fb_list_.CreateBufferList(num_buffers_));
221 ASSERT_EQ(VPX_CODEC_OK,
222 decoder->SetFrameBufferFunctions(
223 GetVP9FrameBuffer, ReleaseVP9FrameBuffer, this));
227 void OpenMD5File(const std::string &md5_file_name_) {
228 md5_file_ = libvpx_test::OpenTestDataFile(md5_file_name_);
229 ASSERT_TRUE(md5_file_ != NULL) << "Md5 file open failed. Filename: "
233 virtual void DecompressedFrameHook(const vpx_image_t &img,
234 const unsigned int frame_number) {
235 ASSERT_TRUE(md5_file_ != NULL);
236 char expected_md5[33];
239 // Read correct md5 checksums.
240 const int res = fscanf(md5_file_, "%s %s", expected_md5, junk);
241 ASSERT_NE(EOF, res) << "Read md5 data failed";
242 expected_md5[32] = '\0';
244 ::libvpx_test::MD5 md5_res;
246 const char *const actual_md5 = md5_res.Get();
249 ASSERT_STREQ(expected_md5, actual_md5)
250 << "Md5 checksums don't match: frame number = " << frame_number;
253 // Callback to get a free external frame buffer. Return value < 0 is an
255 static int GetVP9FrameBuffer(void *user_priv, size_t min_size,
256 vpx_codec_frame_buffer_t *fb) {
257 ExternalFrameBufferMD5Test *const md5Test =
258 reinterpret_cast<ExternalFrameBufferMD5Test*>(user_priv);
259 return md5Test->fb_list_.GetFreeFrameBuffer(min_size, fb);
262 // Callback to release an external frame buffer. Return value < 0 is an
264 static int ReleaseVP9FrameBuffer(void *user_priv,
265 vpx_codec_frame_buffer_t *fb) {
266 ExternalFrameBufferMD5Test *const md5Test =
267 reinterpret_cast<ExternalFrameBufferMD5Test*>(user_priv);
268 return md5Test->fb_list_.ReturnFrameBuffer(fb);
271 void set_num_buffers(int num_buffers) { num_buffers_ = num_buffers; }
272 int num_buffers() const { return num_buffers_; }
277 ExternalFrameBufferList fb_list_;
281 // Class for testing passing in external frame buffers to libvpx.
282 class ExternalFrameBufferTest : public ::testing::Test {
284 ExternalFrameBufferTest()
289 virtual void SetUp() {
290 video_ = new libvpx_test::WebMVideoSource(kVP9TestFile);
291 ASSERT_TRUE(video_ != NULL);
295 vpx_codec_dec_cfg_t cfg = vpx_codec_dec_cfg_t();
296 decoder_ = new libvpx_test::VP9Decoder(cfg, 0);
297 ASSERT_TRUE(decoder_ != NULL);
300 virtual void TearDown() {
305 // Passes the external frame buffer information to libvpx.
306 vpx_codec_err_t SetFrameBufferFunctions(
308 vpx_get_frame_buffer_cb_fn_t cb_get,
309 vpx_release_frame_buffer_cb_fn_t cb_release) {
310 if (num_buffers > 0) {
311 num_buffers_ = num_buffers;
312 EXPECT_TRUE(fb_list_.CreateBufferList(num_buffers_));
315 return decoder_->SetFrameBufferFunctions(cb_get, cb_release, &fb_list_);
318 vpx_codec_err_t DecodeOneFrame() {
319 const vpx_codec_err_t res =
320 decoder_->DecodeFrame(video_->cxdata(), video_->frame_size());
321 CheckDecodedFrames();
322 if (res == VPX_CODEC_OK)
327 vpx_codec_err_t DecodeRemainingFrames() {
328 for (; video_->cxdata() != NULL; video_->Next()) {
329 const vpx_codec_err_t res =
330 decoder_->DecodeFrame(video_->cxdata(), video_->frame_size());
331 if (res != VPX_CODEC_OK)
333 CheckDecodedFrames();
339 void CheckDecodedFrames() {
340 libvpx_test::DxDataIterator dec_iter = decoder_->GetDxData();
341 const vpx_image_t *img = NULL;
343 // Get decompressed data
344 while ((img = dec_iter.Next()) != NULL) {
345 fb_list_.CheckXImageFrameBuffer(img);
349 libvpx_test::WebMVideoSource *video_;
350 libvpx_test::VP9Decoder *decoder_;
352 ExternalFrameBufferList fb_list_;
354 #endif // CONFIG_WEBM_IO
356 // This test runs through the set of test vectors, and decodes them.
357 // Libvpx will call into the application to allocate a frame buffer when
358 // needed. The md5 checksums are computed for each frame in the video file.
359 // If md5 checksums match the correct md5 data, then the test is passed.
360 // Otherwise, the test failed.
361 TEST_P(ExternalFrameBufferMD5Test, ExtFBMD5Match) {
362 const std::string filename = GET_PARAM(kVideoNameParam);
363 libvpx_test::CompressedVideoSource *video = NULL;
365 // Number of buffers equals #VP9_MAXIMUM_REF_BUFFERS +
366 // #VPX_MAXIMUM_WORK_BUFFERS + four jitter buffers.
367 const int jitter_buffers = 4;
368 const int num_buffers =
369 VP9_MAXIMUM_REF_BUFFERS + VPX_MAXIMUM_WORK_BUFFERS + jitter_buffers;
370 set_num_buffers(num_buffers);
372 #if CONFIG_VP8_DECODER
373 // Tell compiler we are not using kVP8TestVectors.
374 (void)libvpx_test::kVP8TestVectors;
377 // Open compressed video file.
378 if (filename.substr(filename.length() - 3, 3) == "ivf") {
379 video = new libvpx_test::IVFVideoSource(filename);
382 video = new libvpx_test::WebMVideoSource(filename);
384 fprintf(stderr, "WebM IO is disabled, skipping test vector %s\n",
389 ASSERT_TRUE(video != NULL);
392 // Construct md5 file name.
393 const std::string md5_filename = filename + ".md5";
394 OpenMD5File(md5_filename);
396 // Decode frame, and check the md5 matching.
397 ASSERT_NO_FATAL_FAILURE(RunLoop(video));
402 TEST_F(ExternalFrameBufferTest, MinFrameBuffers) {
403 // Minimum number of external frame buffers for VP9 is
404 // #VP9_MAXIMUM_REF_BUFFERS + #VPX_MAXIMUM_WORK_BUFFERS.
405 const int num_buffers = VP9_MAXIMUM_REF_BUFFERS + VPX_MAXIMUM_WORK_BUFFERS;
406 ASSERT_EQ(VPX_CODEC_OK,
407 SetFrameBufferFunctions(
408 num_buffers, get_vp9_frame_buffer, release_vp9_frame_buffer));
409 ASSERT_EQ(VPX_CODEC_OK, DecodeRemainingFrames());
412 TEST_F(ExternalFrameBufferTest, EightJitterBuffers) {
413 // Number of buffers equals #VP9_MAXIMUM_REF_BUFFERS +
414 // #VPX_MAXIMUM_WORK_BUFFERS + eight jitter buffers.
415 const int jitter_buffers = 8;
416 const int num_buffers =
417 VP9_MAXIMUM_REF_BUFFERS + VPX_MAXIMUM_WORK_BUFFERS + jitter_buffers;
418 ASSERT_EQ(VPX_CODEC_OK,
419 SetFrameBufferFunctions(
420 num_buffers, get_vp9_frame_buffer, release_vp9_frame_buffer));
421 ASSERT_EQ(VPX_CODEC_OK, DecodeRemainingFrames());
424 TEST_F(ExternalFrameBufferTest, NotEnoughBuffers) {
425 // Minimum number of external frame buffers for VP9 is
426 // #VP9_MAXIMUM_REF_BUFFERS + #VPX_MAXIMUM_WORK_BUFFERS. Most files will
427 // only use 5 frame buffers at one time.
428 const int num_buffers = 2;
429 ASSERT_EQ(VPX_CODEC_OK,
430 SetFrameBufferFunctions(
431 num_buffers, get_vp9_frame_buffer, release_vp9_frame_buffer));
432 ASSERT_EQ(VPX_CODEC_OK, DecodeOneFrame());
433 ASSERT_EQ(VPX_CODEC_MEM_ERROR, DecodeRemainingFrames());
436 TEST_F(ExternalFrameBufferTest, NoRelease) {
437 const int num_buffers = VP9_MAXIMUM_REF_BUFFERS + VPX_MAXIMUM_WORK_BUFFERS;
438 ASSERT_EQ(VPX_CODEC_OK,
439 SetFrameBufferFunctions(num_buffers, get_vp9_frame_buffer,
440 do_not_release_vp9_frame_buffer));
441 ASSERT_EQ(VPX_CODEC_OK, DecodeOneFrame());
442 ASSERT_EQ(VPX_CODEC_MEM_ERROR, DecodeRemainingFrames());
445 TEST_F(ExternalFrameBufferTest, NullRealloc) {
446 const int num_buffers = VP9_MAXIMUM_REF_BUFFERS + VPX_MAXIMUM_WORK_BUFFERS;
447 ASSERT_EQ(VPX_CODEC_OK,
448 SetFrameBufferFunctions(num_buffers, get_vp9_zero_frame_buffer,
449 release_vp9_frame_buffer));
450 ASSERT_EQ(VPX_CODEC_MEM_ERROR, DecodeOneFrame());
453 TEST_F(ExternalFrameBufferTest, ReallocOneLessByte) {
454 const int num_buffers = VP9_MAXIMUM_REF_BUFFERS + VPX_MAXIMUM_WORK_BUFFERS;
455 ASSERT_EQ(VPX_CODEC_OK,
456 SetFrameBufferFunctions(
457 num_buffers, get_vp9_one_less_byte_frame_buffer,
458 release_vp9_frame_buffer));
459 ASSERT_EQ(VPX_CODEC_MEM_ERROR, DecodeOneFrame());
462 TEST_F(ExternalFrameBufferTest, NullGetFunction) {
463 const int num_buffers = VP9_MAXIMUM_REF_BUFFERS + VPX_MAXIMUM_WORK_BUFFERS;
464 ASSERT_EQ(VPX_CODEC_INVALID_PARAM,
465 SetFrameBufferFunctions(num_buffers, NULL,
466 release_vp9_frame_buffer));
469 TEST_F(ExternalFrameBufferTest, NullReleaseFunction) {
470 const int num_buffers = VP9_MAXIMUM_REF_BUFFERS + VPX_MAXIMUM_WORK_BUFFERS;
471 ASSERT_EQ(VPX_CODEC_INVALID_PARAM,
472 SetFrameBufferFunctions(num_buffers, get_vp9_frame_buffer, NULL));
475 TEST_F(ExternalFrameBufferTest, SetAfterDecode) {
476 const int num_buffers = VP9_MAXIMUM_REF_BUFFERS + VPX_MAXIMUM_WORK_BUFFERS;
477 ASSERT_EQ(VPX_CODEC_OK, DecodeOneFrame());
478 ASSERT_EQ(VPX_CODEC_ERROR,
479 SetFrameBufferFunctions(
480 num_buffers, get_vp9_frame_buffer, release_vp9_frame_buffer));
482 #endif // CONFIG_WEBM_IO
484 VP9_INSTANTIATE_TEST_CASE(ExternalFrameBufferMD5Test,
485 ::testing::ValuesIn(libvpx_test::kVP9TestVectors,
486 libvpx_test::kVP9TestVectors +
487 libvpx_test::kNumVP9TestVectors));