vp9_cx_iface: set default cpu_used=5 w/CONFIG_REALTIME_ONLY
[platform/upstream/libvpx.git] / test / video_source.h
1 /*
2  *  Copyright (c) 2012 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 #ifndef VPX_TEST_VIDEO_SOURCE_H_
11 #define VPX_TEST_VIDEO_SOURCE_H_
12
13 #if defined(_WIN32)
14 #undef NOMINMAX
15 #define NOMINMAX
16 #ifndef WIN32_LEAN_AND_MEAN
17 #define WIN32_LEAN_AND_MEAN
18 #endif
19 #include <windows.h>
20 #endif
21 #include <cstdio>
22 #include <cstdlib>
23 #include <cstring>
24 #include <memory>
25 #include <string>
26
27 #include "test/acm_random.h"
28 #if !defined(_WIN32)
29 #include "third_party/googletest/src/include/gtest/gtest.h"
30 #endif
31 #include "vpx/vpx_encoder.h"
32
33 namespace libvpx_test {
34
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)
41
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);
51 #else
52     return ".";
53 #endif
54   }
55   return data_path;
56 }
57
58 // Undefining stringification macros because they are not used elsewhere
59 #undef TO_STRING
60 #undef STRINGIFY
61
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");
65 }
66
67 static FILE *GetTempOutFile(std::string *file_name) {
68   file_name->clear();
69 #if defined(_WIN32)
70   char fname[MAX_PATH];
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+");
77     }
78   }
79   return nullptr;
80 #else
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
85   // path delimiter.
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+");
98 #endif
99 }
100
101 class TempOutFile {
102  public:
103   TempOutFile() { file_ = GetTempOutFile(&file_name_); }
104   ~TempOutFile() {
105     CloseFile();
106     if (!file_name_.empty()) {
107       EXPECT_EQ(0, remove(file_name_.c_str()));
108     }
109   }
110   FILE *file() { return file_; }
111   const std::string &file_name() { return file_name_; }
112
113  protected:
114   void CloseFile() {
115     if (file_) {
116       fclose(file_);
117       file_ = nullptr;
118     }
119   }
120   FILE *file_;
121   std::string file_name_;
122 };
123
124 // Abstract base class for test video sources, which provide a stream of
125 // vpx_image_t images with associated timestamps and duration.
126 class VideoSource {
127  public:
128   virtual ~VideoSource() {}
129
130   // Prepare the stream for reading, rewind/open as necessary.
131   virtual void Begin() = 0;
132
133   // Advance the cursor to the next frame
134   virtual void Next() = 0;
135
136   // Get the current video frame, or nullptr on End-Of-Stream.
137   virtual vpx_image_t *img() const = 0;
138
139   // Get the presentation timestamp of the current frame.
140   virtual vpx_codec_pts_t pts() const = 0;
141
142   // Get the current frame's duration
143   virtual unsigned long duration() const = 0;
144
145   // Get the timebase for the stream
146   virtual vpx_rational_t timebase() const = 0;
147
148   // Get the current frame counter, starting at 0.
149   virtual unsigned int frame() const = 0;
150
151   // Get the current file limit.
152   virtual unsigned int limit() const = 0;
153 };
154
155 class DummyVideoSource : public VideoSource {
156  public:
157   DummyVideoSource()
158       : img_(nullptr), limit_(100), width_(80), height_(64),
159         format_(VPX_IMG_FMT_I420) {
160     ReallocImage();
161   }
162
163   virtual ~DummyVideoSource() { vpx_img_free(img_); }
164
165   virtual void Begin() {
166     frame_ = 0;
167     FillFrame();
168   }
169
170   virtual void Next() {
171     ++frame_;
172     FillFrame();
173   }
174
175   virtual vpx_image_t *img() const {
176     return (frame_ < limit_) ? img_ : nullptr;
177   }
178
179   // Models a stream where Timebase = 1/FPS, so pts == frame.
180   virtual vpx_codec_pts_t pts() const { return frame_; }
181
182   virtual unsigned long duration() const { return 1; }
183
184   virtual vpx_rational_t timebase() const {
185     const vpx_rational_t t = { 1, 30 };
186     return t;
187   }
188
189   virtual unsigned int frame() const { return frame_; }
190
191   virtual unsigned int limit() const { return limit_; }
192
193   void set_limit(unsigned int limit) { limit_ = limit; }
194
195   void SetSize(unsigned int width, unsigned int height) {
196     if (width != width_ || height != height_) {
197       width_ = width;
198       height_ = height;
199       ReallocImage();
200     }
201   }
202
203   void SetImageFormat(vpx_img_fmt_t format) {
204     if (format_ != format) {
205       format_ = format;
206       ReallocImage();
207     }
208   }
209
210  protected:
211   virtual void FillFrame() {
212     if (img_) memset(img_->img_data, 0, raw_sz_);
213   }
214
215   void ReallocImage() {
216     vpx_img_free(img_);
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;
220   }
221
222   vpx_image_t *img_;
223   size_t raw_sz_;
224   unsigned int limit_;
225   unsigned int frame_;
226   unsigned int width_;
227   unsigned int height_;
228   vpx_img_fmt_t format_;
229 };
230
231 class RandomVideoSource : public DummyVideoSource {
232  public:
233   RandomVideoSource(int seed = ACMRandom::DeterministicSeed())
234       : rnd_(seed), seed_(seed) {}
235
236  protected:
237   // Reset the RNG to get a matching stream for the second pass
238   virtual void Begin() {
239     frame_ = 0;
240     rnd_.Reset(seed_);
241     FillFrame();
242   }
243
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() {
247     if (img_) {
248       if (frame_ % 30 < 15) {
249         for (size_t i = 0; i < raw_sz_; ++i) img_->img_data[i] = rnd_.Rand8();
250       } else {
251         memset(img_->img_data, 0, raw_sz_);
252       }
253     }
254   }
255
256   ACMRandom rnd_;
257   int seed_;
258 };
259
260 // Abstract base class for test video sources, which provide a stream of
261 // decompressed images to the decoder.
262 class CompressedVideoSource {
263  public:
264   virtual ~CompressedVideoSource() {}
265
266   virtual void Init() = 0;
267
268   // Prepare the stream for reading, rewind/open as necessary.
269   virtual void Begin() = 0;
270
271   // Advance the cursor to the next frame
272   virtual void Next() = 0;
273
274   virtual const uint8_t *cxdata() const = 0;
275
276   virtual size_t frame_size() const = 0;
277
278   virtual unsigned int frame_number() const = 0;
279 };
280
281 }  // namespace libvpx_test
282
283 #endif  // VPX_TEST_VIDEO_SOURCE_H_