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 too as free.
101 // Returns < 0 on an error.
102 int ReturnFrameBuffer(vpx_codec_frame_buffer_t *fb) {
103 EXPECT_TRUE(fb != NULL);
104 ExternalFrameBuffer *const ext_fb =
105 reinterpret_cast<ExternalFrameBuffer*>(fb->priv);
106 EXPECT_TRUE(ext_fb != NULL);
107 EXPECT_EQ(1, ext_fb->in_use);
112 // Checks that the ximage data is contained within the external frame buffer
113 // private data passed back in the ximage.
114 void CheckXImageFrameBuffer(const vpx_image_t *img) {
115 if (img->fb_priv != NULL) {
116 const struct ExternalFrameBuffer *const ext_fb =
117 reinterpret_cast<ExternalFrameBuffer*>(img->fb_priv);
119 ASSERT_TRUE(img->planes[0] >= ext_fb->data &&
120 img->planes[0] < (ext_fb->data + ext_fb->size));
125 // Returns the index of the first free frame buffer. Returns |num_buffers_|
126 // if there are no free frame buffers.
127 int FindFreeBufferIndex() {
129 // Find a free frame buffer.
130 for (i = 0; i < num_buffers_; ++i) {
131 if (!ext_fb_list_[i].in_use)
137 // Sets |fb| to an external frame buffer. idx is the index into the frame
139 void SetFrameBuffer(int idx, vpx_codec_frame_buffer_t *fb) {
140 ASSERT_TRUE(fb != NULL);
141 fb->data = ext_fb_list_[idx].data;
142 fb->size = ext_fb_list_[idx].size;
143 ASSERT_EQ(0, ext_fb_list_[idx].in_use);
144 ext_fb_list_[idx].in_use = 1;
145 fb->priv = &ext_fb_list_[idx];
149 ExternalFrameBuffer *ext_fb_list_;
152 // Callback used by libvpx to request the application to return a frame
153 // buffer of at least |min_size| in bytes.
154 int get_vp9_frame_buffer(void *user_priv, size_t min_size,
155 vpx_codec_frame_buffer_t *fb) {
156 ExternalFrameBufferList *const fb_list =
157 reinterpret_cast<ExternalFrameBufferList*>(user_priv);
158 return fb_list->GetFreeFrameBuffer(min_size, fb);
161 // Callback used by libvpx to tell the application that |fb| is not needed
163 int release_vp9_frame_buffer(void *user_priv,
164 vpx_codec_frame_buffer_t *fb) {
165 ExternalFrameBufferList *const fb_list =
166 reinterpret_cast<ExternalFrameBufferList*>(user_priv);
167 return fb_list->ReturnFrameBuffer(fb);
170 // Callback will not allocate data for frame buffer.
171 int get_vp9_zero_frame_buffer(void *user_priv, size_t min_size,
172 vpx_codec_frame_buffer_t *fb) {
173 ExternalFrameBufferList *const fb_list =
174 reinterpret_cast<ExternalFrameBufferList*>(user_priv);
175 return fb_list->GetZeroFrameBuffer(min_size, fb);
178 // Callback will allocate one less byte than |min_size|.
179 int get_vp9_one_less_byte_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->GetFreeFrameBuffer(min_size - 1, fb);
186 // Callback will not release the external frame buffer.
187 int do_not_release_vp9_frame_buffer(void *user_priv,
188 vpx_codec_frame_buffer_t *fb) {
194 // Class for testing passing in external frame buffers to libvpx.
195 class ExternalFrameBufferMD5Test
196 : public ::libvpx_test::DecoderTest,
197 public ::libvpx_test::CodecTestWithParam<const char*> {
199 ExternalFrameBufferMD5Test()
200 : DecoderTest(GET_PARAM(::libvpx_test::kCodecFactoryParam)),
204 virtual ~ExternalFrameBufferMD5Test() {
205 if (md5_file_ != NULL)
209 virtual void PreDecodeFrameHook(
210 const libvpx_test::CompressedVideoSource &video,
211 libvpx_test::Decoder *decoder) {
212 if (num_buffers_ > 0 && video.frame_number() == 0) {
213 // Have libvpx use frame buffers we create.
214 ASSERT_TRUE(fb_list_.CreateBufferList(num_buffers_));
215 ASSERT_EQ(VPX_CODEC_OK,
216 decoder->SetFrameBufferFunctions(
217 GetVP9FrameBuffer, ReleaseVP9FrameBuffer, this));
221 void OpenMD5File(const std::string &md5_file_name_) {
222 md5_file_ = libvpx_test::OpenTestDataFile(md5_file_name_);
223 ASSERT_TRUE(md5_file_ != NULL) << "Md5 file open failed. Filename: "
227 virtual void DecompressedFrameHook(const vpx_image_t &img,
228 const unsigned int frame_number) {
229 ASSERT_TRUE(md5_file_ != NULL);
230 char expected_md5[33];
233 // Read correct md5 checksums.
234 const int res = fscanf(md5_file_, "%s %s", expected_md5, junk);
235 ASSERT_NE(EOF, res) << "Read md5 data failed";
236 expected_md5[32] = '\0';
238 ::libvpx_test::MD5 md5_res;
240 const char *const actual_md5 = md5_res.Get();
243 ASSERT_STREQ(expected_md5, actual_md5)
244 << "Md5 checksums don't match: frame number = " << frame_number;
247 // Callback to get a free external frame buffer. Return value < 0 is an
249 static int GetVP9FrameBuffer(void *user_priv, size_t min_size,
250 vpx_codec_frame_buffer_t *fb) {
251 ExternalFrameBufferMD5Test *const md5Test =
252 reinterpret_cast<ExternalFrameBufferMD5Test*>(user_priv);
253 return md5Test->fb_list_.GetFreeFrameBuffer(min_size, fb);
256 // Callback to release an external frame buffer. Return value < 0 is an
258 static int ReleaseVP9FrameBuffer(void *user_priv,
259 vpx_codec_frame_buffer_t *fb) {
260 ExternalFrameBufferMD5Test *const md5Test =
261 reinterpret_cast<ExternalFrameBufferMD5Test*>(user_priv);
262 return md5Test->fb_list_.ReturnFrameBuffer(fb);
265 void set_num_buffers(int num_buffers) { num_buffers_ = num_buffers; }
266 int num_buffers() const { return num_buffers_; }
271 ExternalFrameBufferList fb_list_;
275 // Class for testing passing in external frame buffers to libvpx.
276 class ExternalFrameBufferTest : public ::testing::Test {
278 ExternalFrameBufferTest()
283 virtual void SetUp() {
284 video_ = new libvpx_test::WebMVideoSource(kVP9TestFile);
285 ASSERT_TRUE(video_ != NULL);
289 vpx_codec_dec_cfg_t cfg = vpx_codec_dec_cfg_t();
290 decoder_ = new libvpx_test::VP9Decoder(cfg, 0);
291 ASSERT_TRUE(decoder_ != NULL);
294 virtual void TearDown() {
299 // Passes the external frame buffer information to libvpx.
300 vpx_codec_err_t SetFrameBufferFunctions(
302 vpx_get_frame_buffer_cb_fn_t cb_get,
303 vpx_release_frame_buffer_cb_fn_t cb_release) {
304 if (num_buffers > 0) {
305 num_buffers_ = num_buffers;
306 EXPECT_TRUE(fb_list_.CreateBufferList(num_buffers_));
309 return decoder_->SetFrameBufferFunctions(cb_get, cb_release, &fb_list_);
312 vpx_codec_err_t DecodeOneFrame() {
313 const vpx_codec_err_t res =
314 decoder_->DecodeFrame(video_->cxdata(), video_->frame_size());
315 CheckDecodedFrames();
316 if (res == VPX_CODEC_OK)
321 vpx_codec_err_t DecodeRemainingFrames() {
322 for (; video_->cxdata() != NULL; video_->Next()) {
323 const vpx_codec_err_t res =
324 decoder_->DecodeFrame(video_->cxdata(), video_->frame_size());
325 if (res != VPX_CODEC_OK)
327 CheckDecodedFrames();
333 void CheckDecodedFrames() {
334 libvpx_test::DxDataIterator dec_iter = decoder_->GetDxData();
335 const vpx_image_t *img = NULL;
337 // Get decompressed data
338 while ((img = dec_iter.Next()) != NULL) {
339 fb_list_.CheckXImageFrameBuffer(img);
343 libvpx_test::WebMVideoSource *video_;
344 libvpx_test::VP9Decoder *decoder_;
346 ExternalFrameBufferList fb_list_;
348 #endif // CONFIG_WEBM_IO
350 // This test runs through the set of test vectors, and decodes them.
351 // Libvpx will call into the application to allocate a frame buffer when
352 // needed. The md5 checksums are computed for each frame in the video file.
353 // If md5 checksums match the correct md5 data, then the test is passed.
354 // Otherwise, the test failed.
355 TEST_P(ExternalFrameBufferMD5Test, ExtFBMD5Match) {
356 const std::string filename = GET_PARAM(kVideoNameParam);
357 libvpx_test::CompressedVideoSource *video = NULL;
359 // Number of buffers equals #VP9_MAXIMUM_REF_BUFFERS +
360 // #VPX_MAXIMUM_WORK_BUFFERS + four jitter buffers.
361 const int jitter_buffers = 4;
362 const int num_buffers =
363 VP9_MAXIMUM_REF_BUFFERS + VPX_MAXIMUM_WORK_BUFFERS + jitter_buffers;
364 set_num_buffers(num_buffers);
366 #if CONFIG_VP8_DECODER
367 // Tell compiler we are not using kVP8TestVectors.
368 (void)libvpx_test::kVP8TestVectors;
371 // Open compressed video file.
372 if (filename.substr(filename.length() - 3, 3) == "ivf") {
373 video = new libvpx_test::IVFVideoSource(filename);
376 video = new libvpx_test::WebMVideoSource(filename);
378 fprintf(stderr, "WebM IO is disabled, skipping test vector %s\n",
383 ASSERT_TRUE(video != NULL);
386 // Construct md5 file name.
387 const std::string md5_filename = filename + ".md5";
388 OpenMD5File(md5_filename);
390 // Decode frame, and check the md5 matching.
391 ASSERT_NO_FATAL_FAILURE(RunLoop(video));
396 TEST_F(ExternalFrameBufferTest, MinFrameBuffers) {
397 // Minimum number of external frame buffers for VP9 is
398 // #VP9_MAXIMUM_REF_BUFFERS + #VPX_MAXIMUM_WORK_BUFFERS.
399 const int num_buffers = VP9_MAXIMUM_REF_BUFFERS + VPX_MAXIMUM_WORK_BUFFERS;
400 ASSERT_EQ(VPX_CODEC_OK,
401 SetFrameBufferFunctions(
402 num_buffers, get_vp9_frame_buffer, release_vp9_frame_buffer));
403 ASSERT_EQ(VPX_CODEC_OK, DecodeRemainingFrames());
406 TEST_F(ExternalFrameBufferTest, EightJitterBuffers) {
407 // Number of buffers equals #VP9_MAXIMUM_REF_BUFFERS +
408 // #VPX_MAXIMUM_WORK_BUFFERS + eight jitter buffers.
409 const int jitter_buffers = 8;
410 const int num_buffers =
411 VP9_MAXIMUM_REF_BUFFERS + VPX_MAXIMUM_WORK_BUFFERS + jitter_buffers;
412 ASSERT_EQ(VPX_CODEC_OK,
413 SetFrameBufferFunctions(
414 num_buffers, get_vp9_frame_buffer, release_vp9_frame_buffer));
415 ASSERT_EQ(VPX_CODEC_OK, DecodeRemainingFrames());
418 TEST_F(ExternalFrameBufferTest, NotEnoughBuffers) {
419 // Minimum number of external frame buffers for VP9 is
420 // #VP9_MAXIMUM_REF_BUFFERS + #VPX_MAXIMUM_WORK_BUFFERS. Most files will
421 // only use 5 frame buffers at one time.
422 const int num_buffers = 2;
423 ASSERT_EQ(VPX_CODEC_OK,
424 SetFrameBufferFunctions(
425 num_buffers, get_vp9_frame_buffer, release_vp9_frame_buffer));
426 ASSERT_EQ(VPX_CODEC_OK, DecodeOneFrame());
427 ASSERT_EQ(VPX_CODEC_MEM_ERROR, DecodeRemainingFrames());
430 TEST_F(ExternalFrameBufferTest, NoRelease) {
431 const int num_buffers = VP9_MAXIMUM_REF_BUFFERS + VPX_MAXIMUM_WORK_BUFFERS;
432 ASSERT_EQ(VPX_CODEC_OK,
433 SetFrameBufferFunctions(num_buffers, get_vp9_frame_buffer,
434 do_not_release_vp9_frame_buffer));
435 ASSERT_EQ(VPX_CODEC_OK, DecodeOneFrame());
436 ASSERT_EQ(VPX_CODEC_MEM_ERROR, DecodeRemainingFrames());
439 TEST_F(ExternalFrameBufferTest, NullRealloc) {
440 const int num_buffers = VP9_MAXIMUM_REF_BUFFERS + VPX_MAXIMUM_WORK_BUFFERS;
441 ASSERT_EQ(VPX_CODEC_OK,
442 SetFrameBufferFunctions(num_buffers, get_vp9_zero_frame_buffer,
443 release_vp9_frame_buffer));
444 ASSERT_EQ(VPX_CODEC_MEM_ERROR, DecodeOneFrame());
447 TEST_F(ExternalFrameBufferTest, ReallocOneLessByte) {
448 const int num_buffers = VP9_MAXIMUM_REF_BUFFERS + VPX_MAXIMUM_WORK_BUFFERS;
449 ASSERT_EQ(VPX_CODEC_OK,
450 SetFrameBufferFunctions(
451 num_buffers, get_vp9_one_less_byte_frame_buffer,
452 release_vp9_frame_buffer));
453 ASSERT_EQ(VPX_CODEC_MEM_ERROR, DecodeOneFrame());
456 TEST_F(ExternalFrameBufferTest, NullGetFunction) {
457 const int num_buffers = VP9_MAXIMUM_REF_BUFFERS + VPX_MAXIMUM_WORK_BUFFERS;
458 ASSERT_EQ(VPX_CODEC_INVALID_PARAM,
459 SetFrameBufferFunctions(num_buffers, NULL,
460 release_vp9_frame_buffer));
463 TEST_F(ExternalFrameBufferTest, NullReleaseFunction) {
464 const int num_buffers = VP9_MAXIMUM_REF_BUFFERS + VPX_MAXIMUM_WORK_BUFFERS;
465 ASSERT_EQ(VPX_CODEC_INVALID_PARAM,
466 SetFrameBufferFunctions(num_buffers, get_vp9_frame_buffer, NULL));
469 TEST_F(ExternalFrameBufferTest, SetAfterDecode) {
470 const int num_buffers = VP9_MAXIMUM_REF_BUFFERS + VPX_MAXIMUM_WORK_BUFFERS;
471 ASSERT_EQ(VPX_CODEC_OK, DecodeOneFrame());
472 ASSERT_EQ(VPX_CODEC_ERROR,
473 SetFrameBufferFunctions(
474 num_buffers, get_vp9_frame_buffer, release_vp9_frame_buffer));
476 #endif // CONFIG_WEBM_IO
478 VP9_INSTANTIATE_TEST_CASE(ExternalFrameBufferMD5Test,
479 ::testing::ValuesIn(libvpx_test::kVP9TestVectors,
480 libvpx_test::kVP9TestVectors +
481 libvpx_test::kNumVP9TestVectors));