Merge "Simplify rd_pick_intra_sby_mode()"
[platform/upstream/libvpx.git] / test / external_frame_buffer_test.cc
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 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);
108     ext_fb->in_use = 0;
109     return 0;
110   }
111
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);
118
119       ASSERT_TRUE(img->planes[0] >= ext_fb->data &&
120                   img->planes[0] < (ext_fb->data + ext_fb->size));
121     }
122   }
123
124  private:
125   // Returns the index of the first free frame buffer. Returns |num_buffers_|
126   // if there are no free frame buffers.
127   int FindFreeBufferIndex() {
128     int i;
129     // Find a free frame buffer.
130     for (i = 0; i < num_buffers_; ++i) {
131       if (!ext_fb_list_[i].in_use)
132         break;
133     }
134     return i;
135   }
136
137   // Sets |fb| to an external frame buffer. idx is the index into the frame
138   // buffer list.
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];
146   }
147
148   int num_buffers_;
149   ExternalFrameBuffer *ext_fb_list_;
150 };
151
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);
159 }
160
161 // Callback used by libvpx to tell the application that |fb| is not needed
162 // anymore.
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);
168 }
169
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);
176 }
177
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);
184 }
185
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) {
189   (void)user_priv;
190   (void)fb;
191   return 0;
192 }
193
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*> {
198  protected:
199   ExternalFrameBufferMD5Test()
200       : DecoderTest(GET_PARAM(::libvpx_test::kCodecFactoryParam)),
201         md5_file_(NULL),
202         num_buffers_(0) {}
203
204   virtual ~ExternalFrameBufferMD5Test() {
205     if (md5_file_ != NULL)
206       fclose(md5_file_);
207   }
208
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));
218     }
219   }
220
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: "
224         << md5_file_name_;
225   }
226
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];
231     char junk[128];
232
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';
237
238     ::libvpx_test::MD5 md5_res;
239     md5_res.Add(&img);
240     const char *const actual_md5 = md5_res.Get();
241
242     // Check md5 match.
243     ASSERT_STREQ(expected_md5, actual_md5)
244         << "Md5 checksums don't match: frame number = " << frame_number;
245   }
246
247   // Callback to get a free external frame buffer. Return value < 0 is an
248   // error.
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);
254   }
255
256   // Callback to release an external frame buffer. Return value < 0 is an
257   // error.
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);
263   }
264
265   void set_num_buffers(int num_buffers) { num_buffers_ = num_buffers; }
266   int num_buffers() const { return num_buffers_; }
267
268  private:
269   FILE *md5_file_;
270   int num_buffers_;
271   ExternalFrameBufferList fb_list_;
272 };
273
274 #if CONFIG_WEBM_IO
275 // Class for testing passing in external frame buffers to libvpx.
276 class ExternalFrameBufferTest : public ::testing::Test {
277  protected:
278   ExternalFrameBufferTest()
279       : video_(NULL),
280         decoder_(NULL),
281         num_buffers_(0) {}
282
283   virtual void SetUp() {
284     video_ = new libvpx_test::WebMVideoSource(kVP9TestFile);
285     ASSERT_TRUE(video_ != NULL);
286     video_->Init();
287     video_->Begin();
288
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);
292   }
293
294   virtual void TearDown() {
295     delete decoder_;
296     delete video_;
297   }
298
299   // Passes the external frame buffer information to libvpx.
300   vpx_codec_err_t SetFrameBufferFunctions(
301       int num_buffers,
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_));
307     }
308
309     return decoder_->SetFrameBufferFunctions(cb_get, cb_release, &fb_list_);
310   }
311
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)
317       video_->Next();
318     return res;
319   }
320
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)
326         return res;
327       CheckDecodedFrames();
328     }
329     return VPX_CODEC_OK;
330   }
331
332  private:
333   void CheckDecodedFrames() {
334     libvpx_test::DxDataIterator dec_iter = decoder_->GetDxData();
335     const vpx_image_t *img = NULL;
336
337     // Get decompressed data
338     while ((img = dec_iter.Next()) != NULL) {
339       fb_list_.CheckXImageFrameBuffer(img);
340     }
341   }
342
343   libvpx_test::WebMVideoSource *video_;
344   libvpx_test::VP9Decoder *decoder_;
345   int num_buffers_;
346   ExternalFrameBufferList fb_list_;
347 };
348 #endif  // CONFIG_WEBM_IO
349
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;
358
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);
365
366 #if CONFIG_VP8_DECODER
367   // Tell compiler we are not using kVP8TestVectors.
368   (void)libvpx_test::kVP8TestVectors;
369 #endif
370
371   // Open compressed video file.
372   if (filename.substr(filename.length() - 3, 3) == "ivf") {
373     video = new libvpx_test::IVFVideoSource(filename);
374   } else {
375 #if CONFIG_WEBM_IO
376     video = new libvpx_test::WebMVideoSource(filename);
377 #else
378     fprintf(stderr, "WebM IO is disabled, skipping test vector %s\n",
379             filename.c_str());
380     return;
381 #endif
382   }
383   ASSERT_TRUE(video != NULL);
384   video->Init();
385
386   // Construct md5 file name.
387   const std::string md5_filename = filename + ".md5";
388   OpenMD5File(md5_filename);
389
390   // Decode frame, and check the md5 matching.
391   ASSERT_NO_FATAL_FAILURE(RunLoop(video));
392   delete video;
393 }
394
395 #if CONFIG_WEBM_IO
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());
404 }
405
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());
416 }
417
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());
428 }
429
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());
437 }
438
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());
445 }
446
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());
454 }
455
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));
461 }
462
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));
467 }
468
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));
475 }
476 #endif  // CONFIG_WEBM_IO
477
478 VP9_INSTANTIATE_TEST_CASE(ExternalFrameBufferMD5Test,
479                           ::testing::ValuesIn(libvpx_test::kVP9TestVectors,
480                                               libvpx_test::kVP9TestVectors +
481                                               libvpx_test::kNumVP9TestVectors));
482 }  // namespace