external_frame_buffer_test.cc 10.3 KB
Newer Older
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
 *  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 <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/test_vectors.h"
#include "test/util.h"
#include "test/webm_video_source.h"

namespace {

const int kVideoNameParam = 1;
const char kVP9TestFile[] = "vp90-2-02-size-lf-1920x1080.webm";

// 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;
}

// Callback will not allocate data for frame buffer.
int zero_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 = NULL;
  fb->size = new_size;
  return 0;
}

// Callback will allocate one less byte.
int one_less_byte_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;

  const size_t error_size = new_size - 1;
  fb->data = new uint8_t[error_size];
  fb->size = error_size;
  return 0;
}

// Class for testing passing in external frame buffers to libvpx.
class ExternalFrameBufferMD5Test
    : public ::libvpx_test::DecoderTest,
      public ::libvpx_test::CodecTestWithParam<const char*> {
 protected:
  ExternalFrameBufferMD5Test()
      : DecoderTest(GET_PARAM(::libvpx_test::kCodecFactoryParam)),
        md5_file_(NULL),
        num_buffers_(0),
        frame_buffers_(NULL) {}

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

    if (md5_file_ != NULL)
      fclose(md5_file_);
  }

  virtual void PreDecodeFrameHook(
      const libvpx_test::CompressedVideoSource &video,
      libvpx_test::Decoder *decoder) {
    if (num_buffers_ > 0 && video.frame_number() == 0) {
      // 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_);

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

  void OpenMD5File(const std::string &md5_file_name_) {
    md5_file_ = libvpx_test::OpenTestDataFile(md5_file_name_);
    ASSERT_TRUE(md5_file_ != NULL) << "Md5 file open failed. Filename: "
        << md5_file_name_;
  }

  virtual void DecompressedFrameHook(const vpx_image_t &img,
                                     const unsigned int frame_number) {
    ASSERT_TRUE(md5_file_ != NULL);
    char expected_md5[33];
    char junk[128];

    // Read correct md5 checksums.
    const int res = fscanf(md5_file_, "%s  %s", expected_md5, junk);
    ASSERT_NE(EOF, res) << "Read md5 data failed";
    expected_md5[32] = '\0';

    ::libvpx_test::MD5 md5_res;
    md5_res.Add(&img);
    const char *const actual_md5 = md5_res.Get();

    // Check md5 match.
    ASSERT_STREQ(expected_md5, actual_md5)
        << "Md5 checksums don't match: frame number = " << frame_number;
  }

  void set_num_buffers(int num_buffers) { num_buffers_ = num_buffers; }
  int num_buffers() const { return num_buffers_; }

 private:
  FILE *md5_file_;
  int num_buffers_;
  vpx_codec_frame_buffer_t *frame_buffers_;
};

class ExternalFrameBufferTest : public ::testing::Test {
 protected:
  ExternalFrameBufferTest()
      : video_(NULL),
        decoder_(NULL),
        num_buffers_(0),
        frame_buffers_(NULL) {}

  virtual void SetUp() {
    video_ = new libvpx_test::WebMVideoSource(kVP9TestFile);
    video_->Init();
    video_->Begin();

    vpx_codec_dec_cfg_t cfg = {0};
    decoder_ = new libvpx_test::VP9Decoder(cfg, 0);
  }

  virtual void TearDown() {
    for (int i = 0; i < num_buffers_; ++i) {
      delete [] frame_buffers_[i].data;
    }
    delete [] frame_buffers_;
    delete decoder_;
    delete video_;
  }

  // Passes the external frame buffer information to libvpx.
  vpx_codec_err_t SetExternalFrameBuffers(
      int num_buffers,
      vpx_realloc_frame_buffer_cb_fn_t cb) {
    if (num_buffers > 0) {
      num_buffers_ = num_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_);
    }

    return decoder_->SetExternalFrameBuffers(frame_buffers_, num_buffers_,
                                             cb, NULL);
  }

  // Pass Null frame buffer list to libvpx.
  vpx_codec_err_t SetNullFrameBuffers(
      int num_buffers,
      vpx_realloc_frame_buffer_cb_fn_t cb) {
    return decoder_->SetExternalFrameBuffers(NULL, num_buffers,
                                             cb, NULL);
  }

  vpx_codec_err_t DecodeOneFrame() {
    const vpx_codec_err_t res =
        decoder_->DecodeFrame(video_->cxdata(), video_->frame_size());
    if (res == VPX_CODEC_OK)
      video_->Next();
    return res;
  }

  vpx_codec_err_t DecodeRemainingFrames() {
    for (; video_->cxdata(); video_->Next()) {
      const vpx_codec_err_t res =
          decoder_->DecodeFrame(video_->cxdata(), video_->frame_size());
      if (res != VPX_CODEC_OK)
        return res;

      libvpx_test::DxDataIterator dec_iter = decoder_->GetDxData();
      const vpx_image_t *img = NULL;

      // Get decompressed data
      while ((img = dec_iter.Next())) {
      }
    }
    return VPX_CODEC_OK;
  }

  libvpx_test::WebMVideoSource *video_;
  libvpx_test::VP9Decoder *decoder_;
  int num_buffers_;
  vpx_codec_frame_buffer_t *frame_buffers_;
};


// This test runs through the 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 in the video file.
// If md5 checksums match the correct md5 data, then the test is passed.
// Otherwise, the test failed.
TEST_P(ExternalFrameBufferMD5Test, ExtFBMD5Match) {
  const std::string filename = GET_PARAM(kVideoNameParam);
  libvpx_test::CompressedVideoSource *video = NULL;

  // Number of buffers equals number of possible reference buffers(8), plus
  // one working buffer, plus four jitter buffers.
  const int num_buffers = 13;
  set_num_buffers(num_buffers);

Johann's avatar
Johann committed
235
#if CONFIG_VP8_DECODER
236
237
  // Tell compiler we are not using kVP8TestVectors.
  (void)libvpx_test::kVP8TestVectors;
Johann's avatar
Johann committed
238
#endif
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256

  // Open compressed video file.
  if (filename.substr(filename.length() - 3, 3) == "ivf") {
    video = new libvpx_test::IVFVideoSource(filename);
  } else if (filename.substr(filename.length() - 4, 4) == "webm") {
    video = new libvpx_test::WebMVideoSource(filename);
  }
  video->Init();

  // Construct md5 file name.
  const std::string md5_filename = filename + ".md5";
  OpenMD5File(md5_filename);

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

257
258
259
260
TEST_F(ExternalFrameBufferTest, NineFrameBuffers) {
  // Minimum number of external frame buffers for VP9 is
  // #VP9_MAXIMUM_REF_BUFFERS + #VPX_MAXIMUM_WORK_BUFFERS.
  const int num_buffers = VP9_MAXIMUM_REF_BUFFERS + VPX_MAXIMUM_WORK_BUFFERS;
261
262
263
264
265
266
  ASSERT_EQ(VPX_CODEC_OK,
            SetExternalFrameBuffers(num_buffers, realloc_vp9_frame_buffer));
  ASSERT_EQ(VPX_CODEC_OK, DecodeRemainingFrames());
}

TEST_F(ExternalFrameBufferTest, EightJitterBuffers) {
267
268
269
270
271
  // Number of buffers equals #VP9_MAXIMUM_REF_BUFFERS +
  // #VPX_MAXIMUM_WORK_BUFFERS + eight jitter buffers.
  const int jitter_buffers = 8;
  const int num_buffers =
      VP9_MAXIMUM_REF_BUFFERS + VPX_MAXIMUM_WORK_BUFFERS + jitter_buffers;
272
273
274
275
276
277
  ASSERT_EQ(VPX_CODEC_OK,
            SetExternalFrameBuffers(num_buffers, realloc_vp9_frame_buffer));
  ASSERT_EQ(VPX_CODEC_OK, DecodeRemainingFrames());
}

TEST_F(ExternalFrameBufferTest, NotEnoughBuffers) {
278
279
280
281
  // Minimum number of external frame buffers for VP9 is
  // #VP9_MAXIMUM_REF_BUFFERS + #VPX_MAXIMUM_WORK_BUFFERS. Set one less.
  const int num_buffers =
      VP9_MAXIMUM_REF_BUFFERS + VPX_MAXIMUM_WORK_BUFFERS - 1;
282
283
284
285
286
  ASSERT_EQ(VPX_CODEC_INVALID_PARAM,
            SetExternalFrameBuffers(num_buffers, realloc_vp9_frame_buffer));
}

TEST_F(ExternalFrameBufferTest, NullFrameBufferList) {
287
288
289
290
291
  // Number of buffers equals #VP9_MAXIMUM_REF_BUFFERS +
  // #VPX_MAXIMUM_WORK_BUFFERS + four jitter buffers.
  const int jitter_buffers = 4;
  const int num_buffers =
      VP9_MAXIMUM_REF_BUFFERS + VPX_MAXIMUM_WORK_BUFFERS + jitter_buffers;
292
293
294
295
296
  ASSERT_EQ(VPX_CODEC_INVALID_PARAM,
            SetNullFrameBuffers(num_buffers, realloc_vp9_frame_buffer));
}

TEST_F(ExternalFrameBufferTest, NullRealloc) {
297
298
299
300
301
  // Number of buffers equals #VP9_MAXIMUM_REF_BUFFERS +
  // #VPX_MAXIMUM_WORK_BUFFERS + four jitter buffers.
  const int jitter_buffers = 4;
  const int num_buffers =
      VP9_MAXIMUM_REF_BUFFERS + VPX_MAXIMUM_WORK_BUFFERS + jitter_buffers;
302
303
304
305
306
307
308
  ASSERT_EQ(VPX_CODEC_OK,
            SetExternalFrameBuffers(num_buffers,
                                    zero_realloc_vp9_frame_buffer));
  ASSERT_EQ(VPX_CODEC_MEM_ERROR, DecodeOneFrame());
}

TEST_F(ExternalFrameBufferTest, ReallocOneLessByte) {
309
310
311
312
313
  // Number of buffers equals #VP9_MAXIMUM_REF_BUFFERS +
  // #VPX_MAXIMUM_WORK_BUFFERS + four jitter buffers.
  const int jitter_buffers = 4;
  const int num_buffers =
      VP9_MAXIMUM_REF_BUFFERS + VPX_MAXIMUM_WORK_BUFFERS + jitter_buffers;
314
315
316
317
318
319
320
321
322
  ASSERT_EQ(VPX_CODEC_OK,
            SetExternalFrameBuffers(num_buffers,
                                    one_less_byte_realloc_vp9_frame_buffer));
  ASSERT_EQ(VPX_CODEC_MEM_ERROR, DecodeOneFrame());
}

VP9_INSTANTIATE_TEST_CASE(ExternalFrameBufferMD5Test,
                          ::testing::ValuesIn(libvpx_test::kVP9TestVectors));
}  // namespace