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 "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" 20 21 namespace { 22 23 const int kVideoNameParam = 1; 24 const char kVP9TestFile[] = "vp90-2-02-size-lf-1920x1080.webm"; 25 26 struct ExternalFrameBuffer { 27 uint8_t *data; 28 size_t size; 29 int in_use; 30 }; 31 32 // Class to manipulate a list of external frame buffers. 33 class ExternalFrameBufferList { 34 public: 35 ExternalFrameBufferList() 36 : num_buffers_(0), 37 ext_fb_list_(NULL) {} 38 39 virtual ~ExternalFrameBufferList() { 40 for (int i = 0; i < num_buffers_; ++i) { 41 delete [] ext_fb_list_[i].data; 42 } 43 delete [] ext_fb_list_; 44 } 45 46 // Creates the list to hold the external buffers. Returns true on success. 47 bool CreateBufferList(int num_buffers) { 48 if (num_buffers < 0) 49 return false; 50 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_); 55 return true; 56 } 57 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_) 66 return -1; 67 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; 72 } 73 74 SetFrameBuffer(idx, fb); 75 return 0; 76 } 77 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_) 84 return -1; 85 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; 90 } 91 92 SetFrameBuffer(idx, fb); 93 return 0; 94 } 95 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); 104 ext_fb->in_use = 0; 105 return 0; 106 } 107 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); 114 115 ASSERT_TRUE(img->planes[0] >= ext_fb->data && 116 img->planes[0] < (ext_fb->data + ext_fb->size)); 117 } 118 } 119 120 private: 121 // Returns the index of the first free frame buffer. Returns |num_buffers_| 122 // if there are no free frame buffers. 123 int FindFreeBufferIndex() { 124 int i; 125 // Find a free frame buffer. 126 for (i = 0; i < num_buffers_; ++i) { 127 if (!ext_fb_list_[i].in_use) 128 break; 129 } 130 return i; 131 } 132 133 // Sets |fb| to an external frame buffer. idx is the index into the frame 134 // buffer list. 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]; 142 } 143 144 int num_buffers_; 145 ExternalFrameBuffer *ext_fb_list_; 146 }; 147 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); 155 } 156 157 // Callback used by libvpx to tell the application that |fb| is not needed 158 // anymore. 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); 164 } 165 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); 172 } 173 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); 180 } 181 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) { 185 (void)user_priv; 186 (void)fb; 187 return 0; 188 } 189 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*> { 194 protected: 195 ExternalFrameBufferMD5Test() 196 : DecoderTest(GET_PARAM(::libvpx_test::kCodecFactoryParam)), 197 md5_file_(NULL), 198 num_buffers_(0) {} 199 200 virtual ~ExternalFrameBufferMD5Test() { 201 if (md5_file_ != NULL) 202 fclose(md5_file_); 203 } 204 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)); 214 } 215 } 216 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: " 220 << md5_file_name_; 221 } 222 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]; 227 char junk[128]; 228 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'; 233 234 ::libvpx_test::MD5 md5_res; 235 md5_res.Add(&img); 236 const char *const actual_md5 = md5_res.Get(); 237 238 // Check md5 match. 239 ASSERT_STREQ(expected_md5, actual_md5) 240 << "Md5 checksums don't match: frame number = " << frame_number; 241 } 242 243 // Callback to get a free external frame buffer. Return value < 0 is an 244 // error. 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); 250 } 251 252 // Callback to release an external frame buffer. Return value < 0 is an 253 // error. 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); 259 } 260 261 void set_num_buffers(int num_buffers) { num_buffers_ = num_buffers; } 262 int num_buffers() const { return num_buffers_; } 263 264 private: 265 FILE *md5_file_; 266 int num_buffers_; 267 ExternalFrameBufferList fb_list_; 268 }; 269 270 // Class for testing passing in external frame buffers to libvpx. 271 class ExternalFrameBufferTest : public ::testing::Test { 272 protected: 273 ExternalFrameBufferTest() 274 : video_(NULL), 275 decoder_(NULL), 276 num_buffers_(0) {} 277 278 virtual void SetUp() { 279 video_ = new libvpx_test::WebMVideoSource(kVP9TestFile); 280 ASSERT_TRUE(video_ != NULL); 281 video_->Init(); 282 video_->Begin(); 283 284 vpx_codec_dec_cfg_t cfg = {0}; 285 decoder_ = new libvpx_test::VP9Decoder(cfg, 0); 286 ASSERT_TRUE(decoder_ != NULL); 287 } 288 289 virtual void TearDown() { 290 delete decoder_; 291 delete video_; 292 } 293 294 // Passes the external frame buffer information to libvpx. 295 vpx_codec_err_t SetFrameBufferFunctions( 296 int num_buffers, 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_)); 302 } 303 304 return decoder_->SetFrameBufferFunctions(cb_get, cb_release, &fb_list_); 305 } 306 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) 312 video_->Next(); 313 return res; 314 } 315 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) 321 return res; 322 CheckDecodedFrames(); 323 } 324 return VPX_CODEC_OK; 325 } 326 327 private: 328 void CheckDecodedFrames() { 329 libvpx_test::DxDataIterator dec_iter = decoder_->GetDxData(); 330 const vpx_image_t *img = NULL; 331 332 // Get decompressed data 333 while ((img = dec_iter.Next()) != NULL) { 334 fb_list_.CheckXImageFrameBuffer(img); 335 } 336 } 337 338 libvpx_test::WebMVideoSource *video_; 339 libvpx_test::VP9Decoder *decoder_; 340 int num_buffers_; 341 ExternalFrameBufferList fb_list_; 342 }; 343 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; 352 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); 359 360 #if CONFIG_VP8_DECODER 361 // Tell compiler we are not using kVP8TestVectors. 362 (void)libvpx_test::kVP8TestVectors; 363 #endif 364 365 // Open compressed video file. 366 if (filename.substr(filename.length() - 3, 3) == "ivf") { 367 video = new libvpx_test::IVFVideoSource(filename); 368 } else { 369 video = new libvpx_test::WebMVideoSource(filename); 370 } 371 ASSERT_TRUE(video != NULL); 372 video->Init(); 373 374 // Construct md5 file name. 375 const std::string md5_filename = filename + ".md5"; 376 OpenMD5File(md5_filename); 377 378 // Decode frame, and check the md5 matching. 379 ASSERT_NO_FATAL_FAILURE(RunLoop(video)); 380 delete video; 381 } 382 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()); 391 } 392 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()); 403 } 404 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()); 415 } 416 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()); 424 } 425 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()); 432 } 433 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()); 441 } 442 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)); 448 } 449 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)); 454 } 455 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)); 462 } 463 464 VP9_INSTANTIATE_TEST_CASE(ExternalFrameBufferMD5Test, 465 ::testing::ValuesIn(libvpx_test::kVP9TestVectors, 466 libvpx_test::kVP9TestVectors + 467 libvpx_test::kNumVP9TestVectors)); 468 } // namespace 469