2 * Copyright (c) 2013 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.
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"
23 const int kVideoNameParam = 1;
25 const char *kLRUTestVectors[] = {
26 "vp90-2-02-size-lf-1920x1080.webm",
27 "vp90-2-05-resize.ivf",
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) {
39 fb->data = new uint8_t[new_size];
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*> {
51 struct FrameBufferMD5Sum {
52 int frame_buffer_index;
58 : DecoderTest(GET_PARAM(::libvpx_test::kCodecFactoryParam)),
60 num_jitter_buffers_(0),
61 frame_buffers_(NULL) {}
63 virtual ~LRUFrameBufferTest() {
64 for (int i = 0; i < num_buffers_; ++i) {
65 delete [] frame_buffers_[i].data;
67 delete [] frame_buffers_;
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;
77 // Add 1 for a work buffer.
78 num_buffers_ = max_reference_buffers + 1 + num_jitter_buffers_;
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_);
84 decoder->SetExternalFrameBuffers(frame_buffers_, num_buffers_,
85 realloc_vp9_frame_buffer, NULL);
88 // Turn on frame buffer LRU cache.
89 decoder->Control(VP9D_SET_FRAME_BUFFER_LRU_CACHE, 1);
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];
97 // Find which external buffer contains the y_buffer.
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)) {
106 FrameBufferMD5Sum fb_md5;
107 fb_md5.frame_buffer_index = i;
110 libvpx_test::MD5 md5;
112 fb_md5.md5 = md5.Get();
113 jitter_buffer_md5_sums_.push(fb_md5);
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();
120 libvpx_test::MD5 md5;
121 md5.Add(&fb_md5.img);
122 const std::string check_str = md5.Get();
124 ASSERT_EQ(fb_md5.md5, check_str);
125 jitter_buffer_md5_sums_.pop();
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);
139 void set_num_jitter_buffers(int num_buffers) {
140 num_jitter_buffers_ = num_buffers;
144 // Total number of external frame buffers.
146 int num_jitter_buffers_;
148 // External frame buffers used by libvpx.
149 vpx_codec_frame_buffer_t *frame_buffers_;
151 // Save the md5 checksums for later comparison.
152 std::queue<FrameBufferMD5Sum> jitter_buffer_md5_sums_;
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,
163 TEST_P(LRUFrameBufferTest, CheckLRUOneJitterBuffer) {
164 const std::string filename = GET_PARAM(kVideoNameParam);
166 set_num_jitter_buffers(1);
168 libvpx_test::CompressedVideoSource *const video =
169 OpenCompressedFile(filename);
172 // Decode frame, and check the md5 matching.
173 ASSERT_NO_FATAL_FAILURE(RunLoop(video));
177 TEST_P(LRUFrameBufferTest, CheckLRUFourJitterBuffers) {
178 const std::string filename = GET_PARAM(kVideoNameParam);
180 set_num_jitter_buffers(4);
182 libvpx_test::CompressedVideoSource *const video =
183 OpenCompressedFile(filename);
186 // Decode frame, and check the md5 matching.
187 ASSERT_NO_FATAL_FAILURE(RunLoop(video));
191 TEST_P(LRUFrameBufferTest, CheckLRUEightJitterBuffers) {
192 const std::string filename = GET_PARAM(kVideoNameParam);
194 set_num_jitter_buffers(8);
196 libvpx_test::CompressedVideoSource *const video =
197 OpenCompressedFile(filename);
200 // Decode frame, and check the md5 matching.
201 ASSERT_NO_FATAL_FAILURE(RunLoop(video));
205 VP9_INSTANTIATE_TEST_CASE(LRUFrameBufferTest,
206 ::testing::ValuesIn(kLRUTestVectors));