Upstream version 5.34.98.0
[platform/framework/web/crosswalk.git] / src / third_party / libvpx / source / libvpx / test / lru_frame_buffer_test.cc
1 /*
2  *  Copyright (c) 2013 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 <queue>
12 #include <string>
13
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/util.h"
19 #include "test/webm_video_source.h"
20
21 namespace {
22
23 const int kVideoNameParam = 1;
24
25 const char *kLRUTestVectors[] = {
26   "vp90-2-02-size-lf-1920x1080.webm",
27   "vp90-2-05-resize.ivf",
28 };
29
30 // Callback used by libvpx to request the application to allocate a frame
31 // buffer of at least |new_size| in bytes.
32 int realloc_vp9_frame_buffer(void *user_priv, size_t new_size,
33                              vpx_codec_frame_buffer_t *fb) {
34   (void)user_priv;
35   if (fb == NULL)
36     return -1;
37
38   delete [] fb->data;
39   fb->data = new uint8_t[new_size];
40   fb->size = new_size;
41
42   return 0;
43 }
44
45 // Class for testing libvpx is using the least recently
46 // used frame buffer when a new buffer is requested.
47 class LRUFrameBufferTest
48     : public ::libvpx_test::DecoderTest,
49       public ::libvpx_test::CodecTestWithParam<const char*> {
50  protected:
51   struct FrameBufferMD5Sum {
52     int frame_buffer_index;
53     vpx_image_t img;
54     std::string md5;
55   };
56
57   LRUFrameBufferTest()
58       : DecoderTest(GET_PARAM(::libvpx_test::kCodecFactoryParam)),
59         num_buffers_(0),
60         num_jitter_buffers_(0),
61         frame_buffers_(NULL) {}
62
63   virtual ~LRUFrameBufferTest() {
64     for (int i = 0; i < num_buffers_; ++i) {
65       delete [] frame_buffers_[i].data;
66     }
67     delete [] frame_buffers_;
68   }
69
70   virtual void PreDecodeFrameHook(
71       const libvpx_test::CompressedVideoSource &video,
72       libvpx_test::Decoder *decoder) {
73     // Use external buffers for testing jitter buffers.
74     if (num_jitter_buffers_ > 0 && video.frame_number() == 0) {
75       const int max_reference_buffers = 8;
76
77       // Add 1 for a work buffer.
78       num_buffers_ = max_reference_buffers + 1 + num_jitter_buffers_;
79
80       // Have libvpx use frame buffers we create.
81       frame_buffers_ = new vpx_codec_frame_buffer_t[num_buffers_];
82       memset(frame_buffers_, 0, sizeof(frame_buffers_[0]) * num_buffers_);
83
84       decoder->SetExternalFrameBuffers(frame_buffers_, num_buffers_,
85                                        realloc_vp9_frame_buffer, NULL);
86     }
87
88     // Turn on frame buffer LRU cache.
89     decoder->Control(VP9D_SET_FRAME_BUFFER_LRU_CACHE, 1);
90   }
91
92   virtual void DecompressedFrameHook(const vpx_image_t &img,
93                                      const unsigned int frame_number) {
94     const uint32_t ximg_y_plane = 0;
95     const uint8_t *const y_buffer = img.planes[ximg_y_plane];
96
97     // Find which external buffer contains the y_buffer.
98     int i = 0;
99     for (i = 0; i < num_buffers_; ++i) {
100       if (y_buffer >= frame_buffers_[i].data &&
101           y_buffer < (frame_buffers_[i].data + frame_buffers_[i].size)) {
102         break;
103       }
104     }
105
106     FrameBufferMD5Sum fb_md5;
107     fb_md5.frame_buffer_index = i;
108     fb_md5.img = img;
109
110     libvpx_test::MD5 md5;
111     md5.Add(&img);
112     fb_md5.md5 = md5.Get();
113     jitter_buffer_md5_sums_.push(fb_md5);
114
115     // Check to see if any of the reconstructed image changed.
116     if (jitter_buffer_md5_sums_.size() >
117         static_cast<size_t>(num_jitter_buffers_)) {
118       fb_md5 = jitter_buffer_md5_sums_.front();
119
120       libvpx_test::MD5 md5;
121       md5.Add(&fb_md5.img);
122       const std::string check_str = md5.Get();
123
124       ASSERT_EQ(fb_md5.md5, check_str);
125       jitter_buffer_md5_sums_.pop();
126     }
127   }
128
129   libvpx_test::CompressedVideoSource *OpenCompressedFile(
130       const std::string &filename) {
131     if (filename.substr(filename.length() - 3, 3) == "ivf") {
132       return new libvpx_test::IVFVideoSource(filename);
133     } else if (filename.substr(filename.length() - 4, 4) == "webm") {
134       return new libvpx_test::WebMVideoSource(filename);
135     }
136     return NULL;
137   }
138
139   void set_num_jitter_buffers(int num_buffers) {
140     num_jitter_buffers_ = num_buffers;
141   }
142
143  private:
144   // Total number of external frame buffers.
145   int num_buffers_;
146   int num_jitter_buffers_;
147
148   // External frame buffers used by libvpx.
149   vpx_codec_frame_buffer_t *frame_buffers_;
150
151   // Save the md5 checksums for later comparison.
152   std::queue<FrameBufferMD5Sum> jitter_buffer_md5_sums_;
153 };
154
155 // This test runs through a set of test vectors, and decodes them.
156 // Libvpx will call into the application to allocate a frame buffer when
157 // needed. The md5 checksums are computed for each frame after it is
158 // decoded and stored to be checked later. After a jitter frame buffer
159 // has expired, the md5 checksum is computed again for the expired jitter
160 // buffer frame and checked against the md5 checksum after the frame was
161 // decoded. If md5 checksums match, then the test is passed. Otherwise,
162 // the test failed.
163 TEST_P(LRUFrameBufferTest, CheckLRUOneJitterBuffer) {
164   const std::string filename = GET_PARAM(kVideoNameParam);
165
166   set_num_jitter_buffers(1);
167
168   libvpx_test::CompressedVideoSource *const video =
169       OpenCompressedFile(filename);
170   video->Init();
171
172   // Decode frame, and check the md5 matching.
173   ASSERT_NO_FATAL_FAILURE(RunLoop(video));
174   delete video;
175 }
176
177 TEST_P(LRUFrameBufferTest, CheckLRUFourJitterBuffers) {
178   const std::string filename = GET_PARAM(kVideoNameParam);
179
180   set_num_jitter_buffers(4);
181
182   libvpx_test::CompressedVideoSource *const video =
183       OpenCompressedFile(filename);
184   video->Init();
185
186   // Decode frame, and check the md5 matching.
187   ASSERT_NO_FATAL_FAILURE(RunLoop(video));
188   delete video;
189 }
190
191 TEST_P(LRUFrameBufferTest, CheckLRUEightJitterBuffers) {
192   const std::string filename = GET_PARAM(kVideoNameParam);
193
194   set_num_jitter_buffers(8);
195
196   libvpx_test::CompressedVideoSource *const video =
197       OpenCompressedFile(filename);
198   video->Init();
199
200   // Decode frame, and check the md5 matching.
201   ASSERT_NO_FATAL_FAILURE(RunLoop(video));
202   delete video;
203 }
204
205 VP9_INSTANTIATE_TEST_CASE(LRUFrameBufferTest,
206                           ::testing::ValuesIn(kLRUTestVectors));
207 }  // namespace