1 /* 2 * Copyright (c) 2014 The WebM project authors. All Rights Reserved. 3 * 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. 9 */ 10 11 #include <string> 12 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" 20 #if CONFIG_WEBM_IO 21 #include "test/webm_video_source.h" 22 #endif 23 24 namespace { 25 26 const int kVideoNameParam = 1; 27 const char kVP9TestFile[] = "vp90-2-02-size-lf-1920x1080.webm"; 28 29 struct ExternalFrameBuffer { 30 uint8_t *data; 31 size_t size; 32 int in_use; 33 }; 34 35 // Class to manipulate a list of external frame buffers. 36 class ExternalFrameBufferList { 37 public: 38 ExternalFrameBufferList() 39 : num_buffers_(0), 40 ext_fb_list_(NULL) {} 41 42 virtual ~ExternalFrameBufferList() { 43 for (int i = 0; i < num_buffers_; ++i) { 44 delete [] ext_fb_list_[i].data; 45 } 46 delete [] ext_fb_list_; 47 } 48 49 // Creates the list to hold the external buffers. Returns true on success. 50 bool CreateBufferList(int num_buffers) { 51 if (num_buffers < 0) 52 return false; 53 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_); 58 return true; 59 } 60 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_) 69 return -1; 70 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; 76 } 77 78 SetFrameBuffer(idx, fb); 79 return 0; 80 } 81 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_) 88 return -1; 89 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; 94 } 95 96 SetFrameBuffer(idx, fb); 97 return 0; 98 } 99 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) { 103 if (fb == NULL) { 104 EXPECT_TRUE(fb != NULL); 105 return -1; 106 } 107 ExternalFrameBuffer *const ext_fb = 108 reinterpret_cast<ExternalFrameBuffer*>(fb->priv); 109 if (ext_fb == NULL) { 110 EXPECT_TRUE(ext_fb != NULL); 111 return -1; 112 } 113 EXPECT_EQ(1, ext_fb->in_use); 114 ext_fb->in_use = 0; 115 return 0; 116 } 117 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); 124 125 ASSERT_TRUE(img->planes[0] >= ext_fb->data && 126 img->planes[0] < (ext_fb->data + ext_fb->size)); 127 } 128 } 129 130 private: 131 // Returns the index of the first free frame buffer. Returns |num_buffers_| 132 // if there are no free frame buffers. 133 int FindFreeBufferIndex() { 134 int i; 135 // Find a free frame buffer. 136 for (i = 0; i < num_buffers_; ++i) { 137 if (!ext_fb_list_[i].in_use) 138 break; 139 } 140 return i; 141 } 142 143 // Sets |fb| to an external frame buffer. idx is the index into the frame 144 // buffer list. 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]; 152 } 153 154 int num_buffers_; 155 ExternalFrameBuffer *ext_fb_list_; 156 }; 157 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); 165 } 166 167 // Callback used by libvpx to tell the application that |fb| is not needed 168 // anymore. 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); 174 } 175 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); 182 } 183 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); 190 } 191 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) { 195 (void)user_priv; 196 (void)fb; 197 return 0; 198 } 199 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*> { 204 protected: 205 ExternalFrameBufferMD5Test() 206 : DecoderTest(GET_PARAM(::libvpx_test::kCodecFactoryParam)), 207 md5_file_(NULL), 208 num_buffers_(0) {} 209 210 virtual ~ExternalFrameBufferMD5Test() { 211 if (md5_file_ != NULL) 212 fclose(md5_file_); 213 } 214 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)); 224 } 225 } 226 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: " 230 << md5_file_name_; 231 } 232 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]; 237 char junk[128]; 238 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'; 243 244 ::libvpx_test::MD5 md5_res; 245 md5_res.Add(&img); 246 const char *const actual_md5 = md5_res.Get(); 247 248 // Check md5 match. 249 ASSERT_STREQ(expected_md5, actual_md5) 250 << "Md5 checksums don't match: frame number = " << frame_number; 251 } 252 253 // Callback to get a free external frame buffer. Return value < 0 is an 254 // error. 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); 260 } 261 262 // Callback to release an external frame buffer. Return value < 0 is an 263 // error. 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); 269 } 270 271 void set_num_buffers(int num_buffers) { num_buffers_ = num_buffers; } 272 int num_buffers() const { return num_buffers_; } 273 274 private: 275 FILE *md5_file_; 276 int num_buffers_; 277 ExternalFrameBufferList fb_list_; 278 }; 279 280 #if CONFIG_WEBM_IO 281 // Class for testing passing in external frame buffers to libvpx. 282 class ExternalFrameBufferTest : public ::testing::Test { 283 protected: 284 ExternalFrameBufferTest() 285 : video_(NULL), 286 decoder_(NULL), 287 num_buffers_(0) {} 288 289 virtual void SetUp() { 290 video_ = new libvpx_test::WebMVideoSource(kVP9TestFile); 291 ASSERT_TRUE(video_ != NULL); 292 video_->Init(); 293 video_->Begin(); 294 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); 298 } 299 300 virtual void TearDown() { 301 delete decoder_; 302 delete video_; 303 } 304 305 // Passes the external frame buffer information to libvpx. 306 vpx_codec_err_t SetFrameBufferFunctions( 307 int num_buffers, 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_)); 313 } 314 315 return decoder_->SetFrameBufferFunctions(cb_get, cb_release, &fb_list_); 316 } 317 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) 323 video_->Next(); 324 return res; 325 } 326 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) 332 return res; 333 CheckDecodedFrames(); 334 } 335 return VPX_CODEC_OK; 336 } 337 338 private: 339 void CheckDecodedFrames() { 340 libvpx_test::DxDataIterator dec_iter = decoder_->GetDxData(); 341 const vpx_image_t *img = NULL; 342 343 // Get decompressed data 344 while ((img = dec_iter.Next()) != NULL) { 345 fb_list_.CheckXImageFrameBuffer(img); 346 } 347 } 348 349 libvpx_test::WebMVideoSource *video_; 350 libvpx_test::VP9Decoder *decoder_; 351 int num_buffers_; 352 ExternalFrameBufferList fb_list_; 353 }; 354 #endif // CONFIG_WEBM_IO 355 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; 364 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); 371 372 #if CONFIG_VP8_DECODER 373 // Tell compiler we are not using kVP8TestVectors. 374 (void)libvpx_test::kVP8TestVectors; 375 #endif 376 377 // Open compressed video file. 378 if (filename.substr(filename.length() - 3, 3) == "ivf") { 379 video = new libvpx_test::IVFVideoSource(filename); 380 } else { 381 #if CONFIG_WEBM_IO 382 video = new libvpx_test::WebMVideoSource(filename); 383 #else 384 fprintf(stderr, "WebM IO is disabled, skipping test vector %s\n", 385 filename.c_str()); 386 return; 387 #endif 388 } 389 ASSERT_TRUE(video != NULL); 390 video->Init(); 391 392 // Construct md5 file name. 393 const std::string md5_filename = filename + ".md5"; 394 OpenMD5File(md5_filename); 395 396 // Decode frame, and check the md5 matching. 397 ASSERT_NO_FATAL_FAILURE(RunLoop(video)); 398 delete video; 399 } 400 401 #if CONFIG_WEBM_IO 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()); 410 } 411 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()); 422 } 423 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()); 434 } 435 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()); 443 } 444 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()); 451 } 452 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()); 460 } 461 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)); 467 } 468 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)); 473 } 474 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)); 481 } 482 #endif // CONFIG_WEBM_IO 483 484 VP9_INSTANTIATE_TEST_CASE(ExternalFrameBufferMD5Test, 485 ::testing::ValuesIn(libvpx_test::kVP9TestVectors, 486 libvpx_test::kVP9TestVectors + 487 libvpx_test::kNumVP9TestVectors)); 488 } // namespace 489