2 * Copyright (c) 2012 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.
10 #ifndef VPX_TEST_VIDEO_SOURCE_H_
11 #define VPX_TEST_VIDEO_SOURCE_H_
16 #ifndef WIN32_LEAN_AND_MEAN
17 #define WIN32_LEAN_AND_MEAN
27 #include "test/acm_random.h"
29 #include "third_party/googletest/src/include/gtest/gtest.h"
31 #include "vpx/vpx_encoder.h"
33 namespace libvpx_test {
35 // Helper macros to ensure LIBVPX_TEST_DATA_PATH is a quoted string.
36 // These are undefined right below GetDataPath
37 // NOTE: LIBVPX_TEST_DATA_PATH MUST NOT be a quoted string before
38 // Stringification or the GetDataPath will fail at runtime
39 #define TO_STRING(S) #S
40 #define STRINGIFY(S) TO_STRING(S)
42 // A simple function to encapsulate cross platform retrieval of test data path
43 static std::string GetDataPath() {
44 const char *const data_path = getenv("LIBVPX_TEST_DATA_PATH");
45 if (data_path == nullptr) {
46 #ifdef LIBVPX_TEST_DATA_PATH
47 // In some environments, we cannot set environment variables
48 // Instead, we set the data path by using a preprocessor symbol
49 // which can be set from make files
50 return STRINGIFY(LIBVPX_TEST_DATA_PATH);
58 // Undefining stringification macros because they are not used elsewhere
62 inline FILE *OpenTestDataFile(const std::string &file_name) {
63 const std::string path_to_source = GetDataPath() + "/" + file_name;
64 return fopen(path_to_source.c_str(), "rb");
67 static FILE *GetTempOutFile(std::string *file_name) {
71 char tmppath[MAX_PATH];
72 if (GetTempPathA(MAX_PATH, tmppath)) {
73 // Assume for now that the filename generated is unique per process
74 if (GetTempFileNameA(tmppath, "lvx", 0, fname)) {
75 file_name->assign(fname);
76 return fopen(fname, "wb+");
81 std::string temp_dir = testing::TempDir();
82 if (temp_dir.empty()) return nullptr;
83 // Versions of testing::TempDir() prior to release-1.11.0-214-g5e6a5336 may
84 // use the value of an environment variable without checking for a trailing
86 if (temp_dir[temp_dir.size() - 1] != '/') temp_dir += '/';
87 const char name_template[] = "libvpxtest.XXXXXX";
88 std::unique_ptr<char[]> temp_file_name(
89 new char[temp_dir.size() + sizeof(name_template)]);
90 if (temp_file_name == nullptr) return nullptr;
91 memcpy(temp_file_name.get(), temp_dir.data(), temp_dir.size());
92 memcpy(temp_file_name.get() + temp_dir.size(), name_template,
93 sizeof(name_template));
94 const int fd = mkstemp(temp_file_name.get());
95 if (fd == -1) return nullptr;
96 *file_name = temp_file_name.get();
97 return fdopen(fd, "wb+");
103 TempOutFile() { file_ = GetTempOutFile(&file_name_); }
106 if (!file_name_.empty()) {
107 EXPECT_EQ(0, remove(file_name_.c_str()));
110 FILE *file() { return file_; }
111 const std::string &file_name() { return file_name_; }
121 std::string file_name_;
124 // Abstract base class for test video sources, which provide a stream of
125 // vpx_image_t images with associated timestamps and duration.
128 virtual ~VideoSource() {}
130 // Prepare the stream for reading, rewind/open as necessary.
131 virtual void Begin() = 0;
133 // Advance the cursor to the next frame
134 virtual void Next() = 0;
136 // Get the current video frame, or nullptr on End-Of-Stream.
137 virtual vpx_image_t *img() const = 0;
139 // Get the presentation timestamp of the current frame.
140 virtual vpx_codec_pts_t pts() const = 0;
142 // Get the current frame's duration
143 virtual unsigned long duration() const = 0;
145 // Get the timebase for the stream
146 virtual vpx_rational_t timebase() const = 0;
148 // Get the current frame counter, starting at 0.
149 virtual unsigned int frame() const = 0;
151 // Get the current file limit.
152 virtual unsigned int limit() const = 0;
155 class DummyVideoSource : public VideoSource {
158 : img_(nullptr), limit_(100), width_(80), height_(64),
159 format_(VPX_IMG_FMT_I420) {
163 virtual ~DummyVideoSource() { vpx_img_free(img_); }
165 virtual void Begin() {
170 virtual void Next() {
175 virtual vpx_image_t *img() const {
176 return (frame_ < limit_) ? img_ : nullptr;
179 // Models a stream where Timebase = 1/FPS, so pts == frame.
180 virtual vpx_codec_pts_t pts() const { return frame_; }
182 virtual unsigned long duration() const { return 1; }
184 virtual vpx_rational_t timebase() const {
185 const vpx_rational_t t = { 1, 30 };
189 virtual unsigned int frame() const { return frame_; }
191 virtual unsigned int limit() const { return limit_; }
193 void set_limit(unsigned int limit) { limit_ = limit; }
195 void SetSize(unsigned int width, unsigned int height) {
196 if (width != width_ || height != height_) {
203 void SetImageFormat(vpx_img_fmt_t format) {
204 if (format_ != format) {
211 virtual void FillFrame() {
212 if (img_) memset(img_->img_data, 0, raw_sz_);
215 void ReallocImage() {
217 img_ = vpx_img_alloc(nullptr, format_, width_, height_, 32);
218 ASSERT_NE(img_, nullptr);
219 raw_sz_ = ((img_->w + 31) & ~31u) * img_->h * img_->bps / 8;
227 unsigned int height_;
228 vpx_img_fmt_t format_;
231 class RandomVideoSource : public DummyVideoSource {
233 RandomVideoSource(int seed = ACMRandom::DeterministicSeed())
234 : rnd_(seed), seed_(seed) {}
237 // Reset the RNG to get a matching stream for the second pass
238 virtual void Begin() {
244 // 15 frames of noise, followed by 15 static frames. Reset to 0 rather
245 // than holding previous frames to encourage keyframes to be thrown.
246 virtual void FillFrame() {
248 if (frame_ % 30 < 15) {
249 for (size_t i = 0; i < raw_sz_; ++i) img_->img_data[i] = rnd_.Rand8();
251 memset(img_->img_data, 0, raw_sz_);
260 // Abstract base class for test video sources, which provide a stream of
261 // decompressed images to the decoder.
262 class CompressedVideoSource {
264 virtual ~CompressedVideoSource() {}
266 virtual void Init() = 0;
268 // Prepare the stream for reading, rewind/open as necessary.
269 virtual void Begin() = 0;
271 // Advance the cursor to the next frame
272 virtual void Next() = 0;
274 virtual const uint8_t *cxdata() const = 0;
276 virtual size_t frame_size() const = 0;
278 virtual unsigned int frame_number() const = 0;
281 } // namespace libvpx_test
283 #endif // VPX_TEST_VIDEO_SOURCE_H_