summaryrefslogtreecommitdiff
path: root/test/lru_frame_buffer_test.cc
blob: cd6b432d89929062d7c6a7c611dd825c063b0d33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
 *  Copyright (c) 2013 The WebM project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include <queue>
#include <string>

#include "test/codec_factory.h"
#include "test/decode_test_driver.h"
#include "test/ivf_video_source.h"
#include "test/md5_helper.h"
#include "test/util.h"
#include "test/webm_video_source.h"

namespace {

const int kVideoNameParam = 1;

const char *kLRUTestVectors[] = {
  "vp90-2-02-size-lf-1920x1080.webm",
  "vp90-2-05-resize.ivf",
};

// Callback used by libvpx to request the application to allocate a frame
// buffer of at least |new_size| in bytes.
int realloc_vp9_frame_buffer(void *user_priv, size_t new_size,
                             vpx_codec_frame_buffer_t *fb) {
  (void)user_priv;
  if (fb == NULL)
    return -1;

  delete [] fb->data;
  fb->data = new uint8_t[new_size];
  fb->size = new_size;

  return 0;
}

// Class for testing libvpx is using the least recently
// used frame buffer when a new buffer is requested.
class LRUFrameBufferTest
    : public ::libvpx_test::DecoderTest,
      public ::libvpx_test::CodecTestWithParam<const char*> {
 protected:
  struct FrameBufferMD5Sum {
    int frame_buffer_index;
    vpx_image_t img;
    std::string md5;
  };

  LRUFrameBufferTest()
      : DecoderTest(GET_PARAM(::libvpx_test::kCodecFactoryParam)),
        num_buffers_(0),
        num_jitter_buffers_(0),
        frame_buffers_(NULL) {}

  virtual ~LRUFrameBufferTest() {
    for (int i = 0; i < num_buffers_; ++i) {
      delete [] frame_buffers_[i].data;
    }
    delete [] frame_buffers_;
  }

  virtual void PreDecodeFrameHook(
      const libvpx_test::CompressedVideoSource &video,
      libvpx_test::Decoder *decoder) {
    // Use external buffers for testing jitter buffers.
    if (num_jitter_buffers_ > 0 && video.frame_number() == 0) {
      const int max_reference_buffers = 8;

      // Add 1 for a work buffer.
      num_buffers_ = max_reference_buffers + 1 + num_jitter_buffers_;

      // Have libvpx use frame buffers we create.
      frame_buffers_ = new vpx_codec_frame_buffer_t[num_buffers_];
      memset(frame_buffers_, 0, sizeof(frame_buffers_[0]) * num_buffers_);

      decoder->SetExternalFrameBuffers(frame_buffers_, num_buffers_,
                                       realloc_vp9_frame_buffer, NULL);
    }

    // Turn on frame buffer LRU cache.
    decoder->Control(VP9D_SET_FRAME_BUFFER_LRU_CACHE, 1);
  }

  virtual void DecompressedFrameHook(const vpx_image_t &img,
                                     const unsigned int frame_number) {
    const uint32_t ximg_y_plane = 0;
    const uint8_t *const y_buffer = img.planes[ximg_y_plane];

    // Find which external buffer contains the y_buffer.
    int i = 0;
    for (i = 0; i < num_buffers_; ++i) {
      if (y_buffer >= frame_buffers_[i].data &&
          y_buffer < (frame_buffers_[i].data + frame_buffers_[i].size)) {
        break;
      }
    }

    FrameBufferMD5Sum fb_md5;
    fb_md5.frame_buffer_index = i;
    fb_md5.img = img;

    libvpx_test::MD5 md5;
    md5.Add(&img);
    fb_md5.md5 = md5.Get();
    jitter_buffer_md5_sums_.push(fb_md5);

    // Check to see if any of the reconstructed image changed.
    if (jitter_buffer_md5_sums_.size() >
        static_cast<size_t>(num_jitter_buffers_)) {
      fb_md5 = jitter_buffer_md5_sums_.front();

      libvpx_test::MD5 md5;
      md5.Add(&fb_md5.img);
      const std::string check_str = md5.Get();

      ASSERT_EQ(fb_md5.md5, check_str);
      jitter_buffer_md5_sums_.pop();
    }
  }

  libvpx_test::CompressedVideoSource *OpenCompressedFile(
      const std::string &filename) {
    if (filename.substr(filename.length() - 3, 3) == "ivf") {
      return new libvpx_test::IVFVideoSource(filename);
    } else if (filename.substr(filename.length() - 4, 4) == "webm") {
      return new libvpx_test::WebMVideoSource(filename);
    }
    return NULL;
  }

  void set_num_jitter_buffers(int num_buffers) {
    num_jitter_buffers_ = num_buffers;
  }

 private:
  // Total number of external frame buffers.
  int num_buffers_;
  int num_jitter_buffers_;

  // External frame buffers used by libvpx.
  vpx_codec_frame_buffer_t *frame_buffers_;

  // Save the md5 checksums for later comparison.
  std::queue<FrameBufferMD5Sum> jitter_buffer_md5_sums_;
};

// This test runs through a set of test vectors, and decodes them.
// Libvpx will call into the application to allocate a frame buffer when
// needed. The md5 checksums are computed for each frame after it is
// decoded and stored to be checked later. After a jitter frame buffer
// has expired, the md5 checksum is computed again for the expired jitter
// buffer frame and checked against the md5 checksum after the frame was
// decoded. If md5 checksums match, then the test is passed. Otherwise,
// the test failed.
TEST_P(LRUFrameBufferTest, CheckLRUOneJitterBuffer) {
  const std::string filename = GET_PARAM(kVideoNameParam);

  set_num_jitter_buffers(1);

  libvpx_test::CompressedVideoSource *const video =
      OpenCompressedFile(filename);
  video->Init();

  // Decode frame, and check the md5 matching.
  ASSERT_NO_FATAL_FAILURE(RunLoop(video));
  delete video;
}

TEST_P(LRUFrameBufferTest, CheckLRUFourJitterBuffers) {
  const std::string filename = GET_PARAM(kVideoNameParam);

  set_num_jitter_buffers(4);

  libvpx_test::CompressedVideoSource *const video =
      OpenCompressedFile(filename);
  video->Init();

  // Decode frame, and check the md5 matching.
  ASSERT_NO_FATAL_FAILURE(RunLoop(video));
  delete video;
}

TEST_P(LRUFrameBufferTest, CheckLRUEightJitterBuffers) {
  const std::string filename = GET_PARAM(kVideoNameParam);

  set_num_jitter_buffers(8);

  libvpx_test::CompressedVideoSource *const video =
      OpenCompressedFile(filename);
  video->Init();

  // Decode frame, and check the md5 matching.
  ASSERT_NO_FATAL_FAILURE(RunLoop(video));
  delete video;
}

VP9_INSTANTIATE_TEST_CASE(LRUFrameBufferTest,
                          ::testing::ValuesIn(kLRUTestVectors));
}  // namespace