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 "test/codec_factory.h"
14 #include "test/decode_test_driver.h"
15 #include "test/ivf_video_source.h"
16 #include "test/md5_helper.h"
17 #include "test/test_vectors.h"
18 #include "test/util.h"
19 #include "test/webm_video_source.h"
23 const int kVideoNameParam = 1;
24 const char kVP9TestFile[] = "vp90-2-02-size-lf-1920x1080.webm";
26 struct ExternalFrameBuffer {
32 // Class to manipulate a list of external frame buffers.
33 class ExternalFrameBufferList {
35 ExternalFrameBufferList()
39 virtual ~ExternalFrameBufferList() {
40 for (int i = 0; i < num_buffers_; ++i) {
41 delete [] ext_fb_list_[i].data;
43 delete [] ext_fb_list_;
46 // Creates the list to hold the external buffers. Returns true on success.
47 bool CreateBufferList(int num_buffers) {
51 num_buffers_ = num_buffers;
52 ext_fb_list_ = new ExternalFrameBuffer[num_buffers_];
53 EXPECT_TRUE(ext_fb_list_ != NULL);
54 memset(ext_fb_list_, 0, sizeof(ext_fb_list_[0]) * num_buffers_);
58 // Searches the frame buffer list for a free frame buffer. Makes sure
59 // that the frame buffer is at least |min_size| in bytes. Marks that the
60 // frame buffer is in use by libvpx. Finally sets |fb| to point to the
61 // external frame buffer. Returns < 0 on an error.
62 int GetFreeFrameBuffer(size_t min_size, vpx_codec_frame_buffer_t *fb) {
63 EXPECT_TRUE(fb != NULL);
64 const int idx = FindFreeBufferIndex();
65 if (idx == num_buffers_)
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 ext_fb_list_[idx].size = min_size;
74 SetFrameBuffer(idx, fb);
78 // Test function that will not allocate any data for the frame buffer.
79 // Returns < 0 on an error.
80 int GetZeroFrameBuffer(size_t min_size, vpx_codec_frame_buffer_t *fb) {
81 EXPECT_TRUE(fb != NULL);
82 const int idx = FindFreeBufferIndex();
83 if (idx == num_buffers_)
86 if (ext_fb_list_[idx].size < min_size) {
87 delete [] ext_fb_list_[idx].data;
88 ext_fb_list_[idx].data = NULL;
89 ext_fb_list_[idx].size = min_size;
92 SetFrameBuffer(idx, fb);
96 // Marks the external frame buffer that |fb| is pointing too as free.
97 // Returns < 0 on an error.
98 int ReturnFrameBuffer(vpx_codec_frame_buffer_t *fb) {
99 EXPECT_TRUE(fb != NULL);
100 ExternalFrameBuffer *const ext_fb =
101 reinterpret_cast<ExternalFrameBuffer*>(fb->priv);
102 EXPECT_TRUE(ext_fb != NULL);
103 EXPECT_EQ(1, ext_fb->in_use);
108 // Checks that the ximage data is contained within the external frame buffer
109 // private data passed back in the ximage.
110 void CheckXImageFrameBuffer(const vpx_image_t *img) {
111 if (img->fb_priv != NULL) {
112 const struct ExternalFrameBuffer *const ext_fb =
113 reinterpret_cast<ExternalFrameBuffer*>(img->fb_priv);
115 ASSERT_TRUE(img->planes[0] >= ext_fb->data &&
116 img->planes[0] < (ext_fb->data + ext_fb->size));
121 // Returns the index of the first free frame buffer. Returns |num_buffers_|
122 // if there are no free frame buffers.
123 int FindFreeBufferIndex() {
125 // Find a free frame buffer.
126 for (i = 0; i < num_buffers_; ++i) {
127 if (!ext_fb_list_[i].in_use)
133 // Sets |fb| to an external frame buffer. idx is the index into the frame
135 void SetFrameBuffer(int idx, vpx_codec_frame_buffer_t *fb) {
136 ASSERT_TRUE(fb != NULL);
137 fb->data = ext_fb_list_[idx].data;
138 fb->size = ext_fb_list_[idx].size;
139 ASSERT_EQ(0, ext_fb_list_[idx].in_use);
140 ext_fb_list_[idx].in_use = 1;
141 fb->priv = &ext_fb_list_[idx];
145 ExternalFrameBuffer *ext_fb_list_;
148 // Callback used by libvpx to request the application to return a frame
149 // buffer of at least |min_size| in bytes.
150 int get_vp9_frame_buffer(void *user_priv, size_t min_size,
151 vpx_codec_frame_buffer_t *fb) {
152 ExternalFrameBufferList *const fb_list =
153 reinterpret_cast<ExternalFrameBufferList*>(user_priv);
154 return fb_list->GetFreeFrameBuffer(min_size, fb);
157 // Callback used by libvpx to tell the application that |fb| is not needed
159 int release_vp9_frame_buffer(void *user_priv,
160 vpx_codec_frame_buffer_t *fb) {
161 ExternalFrameBufferList *const fb_list =
162 reinterpret_cast<ExternalFrameBufferList*>(user_priv);
163 return fb_list->ReturnFrameBuffer(fb);
166 // Callback will not allocate data for frame buffer.
167 int get_vp9_zero_frame_buffer(void *user_priv, size_t min_size,
168 vpx_codec_frame_buffer_t *fb) {
169 ExternalFrameBufferList *const fb_list =
170 reinterpret_cast<ExternalFrameBufferList*>(user_priv);
171 return fb_list->GetZeroFrameBuffer(min_size, fb);
174 // Callback will allocate one less byte than |min_size|.
175 int get_vp9_one_less_byte_frame_buffer(void *user_priv, size_t min_size,
176 vpx_codec_frame_buffer_t *fb) {
177 ExternalFrameBufferList *const fb_list =
178 reinterpret_cast<ExternalFrameBufferList*>(user_priv);
179 return fb_list->GetFreeFrameBuffer(min_size - 1, fb);
182 // Callback will not release the external frame buffer.
183 int do_not_release_vp9_frame_buffer(void *user_priv,
184 vpx_codec_frame_buffer_t *fb) {
190 // Class for testing passing in external frame buffers to libvpx.
191 class ExternalFrameBufferMD5Test
192 : public ::libvpx_test::DecoderTest,
193 public ::libvpx_test::CodecTestWithParam<const char*> {
195 ExternalFrameBufferMD5Test()
196 : DecoderTest(GET_PARAM(::libvpx_test::kCodecFactoryParam)),
200 virtual ~ExternalFrameBufferMD5Test() {
201 if (md5_file_ != NULL)
205 virtual void PreDecodeFrameHook(
206 const libvpx_test::CompressedVideoSource &video,
207 libvpx_test::Decoder *decoder) {
208 if (num_buffers_ > 0 && video.frame_number() == 0) {
209 // Have libvpx use frame buffers we create.
210 ASSERT_TRUE(fb_list_.CreateBufferList(num_buffers_));
211 ASSERT_EQ(VPX_CODEC_OK,
212 decoder->SetFrameBufferFunctions(
213 GetVp9FrameBuffer, ReleaseVP9FrameBuffer, this));
217 void OpenMD5File(const std::string &md5_file_name_) {
218 md5_file_ = libvpx_test::OpenTestDataFile(md5_file_name_);
219 ASSERT_TRUE(md5_file_ != NULL) << "Md5 file open failed. Filename: "
223 virtual void DecompressedFrameHook(const vpx_image_t &img,
224 const unsigned int frame_number) {
225 ASSERT_TRUE(md5_file_ != NULL);
226 char expected_md5[33];
229 // Read correct md5 checksums.
230 const int res = fscanf(md5_file_, "%s %s", expected_md5, junk);
231 ASSERT_NE(EOF, res) << "Read md5 data failed";
232 expected_md5[32] = '\0';
234 ::libvpx_test::MD5 md5_res;
236 const char *const actual_md5 = md5_res.Get();
239 ASSERT_STREQ(expected_md5, actual_md5)
240 << "Md5 checksums don't match: frame number = " << frame_number;
243 // Callback to get a free external frame buffer. Return value < 0 is an
245 static int GetVp9FrameBuffer(void *user_priv, size_t min_size,
246 vpx_codec_frame_buffer_t *fb) {
247 ExternalFrameBufferMD5Test *const md5Test =
248 reinterpret_cast<ExternalFrameBufferMD5Test*>(user_priv);
249 return md5Test->fb_list_.GetFreeFrameBuffer(min_size, fb);
252 // Callback to release an external frame buffer. Return value < 0 is an
254 static int ReleaseVP9FrameBuffer(void *user_priv,
255 vpx_codec_frame_buffer_t *fb) {
256 ExternalFrameBufferMD5Test *const md5Test =
257 reinterpret_cast<ExternalFrameBufferMD5Test*>(user_priv);
258 return md5Test->fb_list_.ReturnFrameBuffer(fb);
261 void set_num_buffers(int num_buffers) { num_buffers_ = num_buffers; }
262 int num_buffers() const { return num_buffers_; }
267 ExternalFrameBufferList fb_list_;
270 // Class for testing passing in external frame buffers to libvpx.
271 class ExternalFrameBufferTest : public ::testing::Test {
273 ExternalFrameBufferTest()
278 virtual void SetUp() {
279 video_ = new libvpx_test::WebMVideoSource(kVP9TestFile);
280 ASSERT_TRUE(video_ != NULL);
284 vpx_codec_dec_cfg_t cfg = {0};
285 decoder_ = new libvpx_test::VP9Decoder(cfg, 0);
286 ASSERT_TRUE(decoder_ != NULL);
289 virtual void TearDown() {
294 // Passes the external frame buffer information to libvpx.
295 vpx_codec_err_t SetFrameBufferFunctions(
297 vpx_get_frame_buffer_cb_fn_t cb_get,
298 vpx_release_frame_buffer_cb_fn_t cb_release) {
299 if (num_buffers > 0) {
300 num_buffers_ = num_buffers;
301 EXPECT_TRUE(fb_list_.CreateBufferList(num_buffers_));
304 return decoder_->SetFrameBufferFunctions(cb_get, cb_release, &fb_list_);
307 vpx_codec_err_t DecodeOneFrame() {
308 const vpx_codec_err_t res =
309 decoder_->DecodeFrame(video_->cxdata(), video_->frame_size());
310 CheckDecodedFrames();
311 if (res == VPX_CODEC_OK)
316 vpx_codec_err_t DecodeRemainingFrames() {
317 for (; video_->cxdata() != NULL; video_->Next()) {
318 const vpx_codec_err_t res =
319 decoder_->DecodeFrame(video_->cxdata(), video_->frame_size());
320 if (res != VPX_CODEC_OK)
322 CheckDecodedFrames();
328 void CheckDecodedFrames() {
329 libvpx_test::DxDataIterator dec_iter = decoder_->GetDxData();
330 const vpx_image_t *img = NULL;
332 // Get decompressed data
333 while ((img = dec_iter.Next()) != NULL) {
334 fb_list_.CheckXImageFrameBuffer(img);
338 libvpx_test::WebMVideoSource *video_;
339 libvpx_test::VP9Decoder *decoder_;
341 ExternalFrameBufferList fb_list_;
344 // This test runs through the set of test vectors, and decodes them.
345 // Libvpx will call into the application to allocate a frame buffer when
346 // needed. The md5 checksums are computed for each frame in the video file.
347 // If md5 checksums match the correct md5 data, then the test is passed.
348 // Otherwise, the test failed.
349 TEST_P(ExternalFrameBufferMD5Test, ExtFBMD5Match) {
350 const std::string filename = GET_PARAM(kVideoNameParam);
351 libvpx_test::CompressedVideoSource *video = NULL;
353 // Number of buffers equals #VP9_MAXIMUM_REF_BUFFERS +
354 // #VPX_MAXIMUM_WORK_BUFFERS + four jitter buffers.
355 const int jitter_buffers = 4;
356 const int num_buffers =
357 VP9_MAXIMUM_REF_BUFFERS + VPX_MAXIMUM_WORK_BUFFERS + jitter_buffers;
358 set_num_buffers(num_buffers);
360 #if CONFIG_VP8_DECODER
361 // Tell compiler we are not using kVP8TestVectors.
362 (void)libvpx_test::kVP8TestVectors;
365 // Open compressed video file.
366 if (filename.substr(filename.length() - 3, 3) == "ivf") {
367 video = new libvpx_test::IVFVideoSource(filename);
369 video = new libvpx_test::WebMVideoSource(filename);
371 ASSERT_TRUE(video != NULL);
374 // Construct md5 file name.
375 const std::string md5_filename = filename + ".md5";
376 OpenMD5File(md5_filename);
378 // Decode frame, and check the md5 matching.
379 ASSERT_NO_FATAL_FAILURE(RunLoop(video));
383 TEST_F(ExternalFrameBufferTest, MinFrameBuffers) {
384 // Minimum number of external frame buffers for VP9 is
385 // #VP9_MAXIMUM_REF_BUFFERS + #VPX_MAXIMUM_WORK_BUFFERS.
386 const int num_buffers = VP9_MAXIMUM_REF_BUFFERS + VPX_MAXIMUM_WORK_BUFFERS;
387 ASSERT_EQ(VPX_CODEC_OK,
388 SetFrameBufferFunctions(
389 num_buffers, get_vp9_frame_buffer, release_vp9_frame_buffer));
390 ASSERT_EQ(VPX_CODEC_OK, DecodeRemainingFrames());
393 TEST_F(ExternalFrameBufferTest, EightJitterBuffers) {
394 // Number of buffers equals #VP9_MAXIMUM_REF_BUFFERS +
395 // #VPX_MAXIMUM_WORK_BUFFERS + eight jitter buffers.
396 const int jitter_buffers = 8;
397 const int num_buffers =
398 VP9_MAXIMUM_REF_BUFFERS + VPX_MAXIMUM_WORK_BUFFERS + jitter_buffers;
399 ASSERT_EQ(VPX_CODEC_OK,
400 SetFrameBufferFunctions(
401 num_buffers, get_vp9_frame_buffer, release_vp9_frame_buffer));
402 ASSERT_EQ(VPX_CODEC_OK, DecodeRemainingFrames());
405 TEST_F(ExternalFrameBufferTest, NotEnoughBuffers) {
406 // Minimum number of external frame buffers for VP9 is
407 // #VP9_MAXIMUM_REF_BUFFERS + #VPX_MAXIMUM_WORK_BUFFERS. Most files will
408 // only use 5 frame buffers at one time.
409 const int num_buffers = 2;
410 ASSERT_EQ(VPX_CODEC_OK,
411 SetFrameBufferFunctions(
412 num_buffers, get_vp9_frame_buffer, release_vp9_frame_buffer));
413 ASSERT_EQ(VPX_CODEC_OK, DecodeOneFrame());
414 ASSERT_EQ(VPX_CODEC_MEM_ERROR, DecodeRemainingFrames());
417 TEST_F(ExternalFrameBufferTest, NoRelease) {
418 const int num_buffers = VP9_MAXIMUM_REF_BUFFERS + VPX_MAXIMUM_WORK_BUFFERS;
419 ASSERT_EQ(VPX_CODEC_OK,
420 SetFrameBufferFunctions(num_buffers, get_vp9_frame_buffer,
421 do_not_release_vp9_frame_buffer));
422 ASSERT_EQ(VPX_CODEC_OK, DecodeOneFrame());
423 ASSERT_EQ(VPX_CODEC_MEM_ERROR, DecodeRemainingFrames());
426 TEST_F(ExternalFrameBufferTest, NullRealloc) {
427 const int num_buffers = VP9_MAXIMUM_REF_BUFFERS + VPX_MAXIMUM_WORK_BUFFERS;
428 ASSERT_EQ(VPX_CODEC_OK,
429 SetFrameBufferFunctions(num_buffers, get_vp9_zero_frame_buffer,
430 release_vp9_frame_buffer));
431 ASSERT_EQ(VPX_CODEC_MEM_ERROR, DecodeOneFrame());
434 TEST_F(ExternalFrameBufferTest, ReallocOneLessByte) {
435 const int num_buffers = VP9_MAXIMUM_REF_BUFFERS + VPX_MAXIMUM_WORK_BUFFERS;
436 ASSERT_EQ(VPX_CODEC_OK,
437 SetFrameBufferFunctions(
438 num_buffers, get_vp9_one_less_byte_frame_buffer,
439 release_vp9_frame_buffer));
440 ASSERT_EQ(VPX_CODEC_MEM_ERROR, DecodeOneFrame());
443 TEST_F(ExternalFrameBufferTest, NullGetFunction) {
444 const int num_buffers = VP9_MAXIMUM_REF_BUFFERS + VPX_MAXIMUM_WORK_BUFFERS;
445 ASSERT_EQ(VPX_CODEC_INVALID_PARAM,
446 SetFrameBufferFunctions(num_buffers, NULL,
447 release_vp9_frame_buffer));
450 TEST_F(ExternalFrameBufferTest, NullReleaseFunction) {
451 const int num_buffers = VP9_MAXIMUM_REF_BUFFERS + VPX_MAXIMUM_WORK_BUFFERS;
452 ASSERT_EQ(VPX_CODEC_INVALID_PARAM,
453 SetFrameBufferFunctions(num_buffers, get_vp9_frame_buffer, NULL));
456 TEST_F(ExternalFrameBufferTest, SetAfterDecode) {
457 const int num_buffers = VP9_MAXIMUM_REF_BUFFERS + VPX_MAXIMUM_WORK_BUFFERS;
458 ASSERT_EQ(VPX_CODEC_OK, DecodeOneFrame());
459 ASSERT_EQ(VPX_CODEC_ERROR,
460 SetFrameBufferFunctions(
461 num_buffers, get_vp9_frame_buffer, release_vp9_frame_buffer));
464 VP9_INSTANTIATE_TEST_CASE(ExternalFrameBufferMD5Test,
465 ::testing::ValuesIn(libvpx_test::kVP9TestVectors));