xref: /aosp_15_r20/external/libvpx/test/video_source.h (revision fb1b10ab9aebc7c7068eedab379b749d7e3900be)
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 #if !defined(_WIN32)
28 #include "gtest/gtest.h"
29 #endif
30 #include "test/acm_random.h"
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
GetDataPath()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 
OpenTestDataFile(const std::string & file_name)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 
GetTempOutFile(std::string * file_name,const char * io_mode)67 static FILE *GetTempOutFile(std::string *file_name, const char *io_mode) {
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, io_mode);
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, io_mode);
98 #endif
99 }
100 
101 class TempOutFile {
102  public:
TempOutFile()103   TempOutFile() { file_ = GetTempOutFile(&file_name_, "wb+"); }
TempOutFile(const char * io_mode)104   TempOutFile(const char *io_mode) {
105     file_ = GetTempOutFile(&file_name_, io_mode);
106   }
~TempOutFile()107   ~TempOutFile() {
108     CloseFile();
109     if (!file_name_.empty()) {
110       EXPECT_EQ(0, remove(file_name_.c_str()));
111     }
112   }
file()113   FILE *file() { return file_; }
file_name()114   const std::string &file_name() { return file_name_; }
115 
116  protected:
CloseFile()117   void CloseFile() {
118     if (file_) {
119       fclose(file_);
120       file_ = nullptr;
121     }
122   }
123   FILE *file_;
124   std::string file_name_;
125 };
126 
127 // Abstract base class for test video sources, which provide a stream of
128 // vpx_image_t images with associated timestamps and duration.
129 class VideoSource {
130  public:
~VideoSource()131   virtual ~VideoSource() {}
132 
133   // Prepare the stream for reading, rewind/open as necessary.
134   virtual void Begin() = 0;
135 
136   // Advance the cursor to the next frame
137   virtual void Next() = 0;
138 
139   // Get the current video frame, or nullptr on End-Of-Stream.
140   virtual vpx_image_t *img() const = 0;
141 
142   // Get the presentation timestamp of the current frame.
143   virtual vpx_codec_pts_t pts() const = 0;
144 
145   // Get the current frame's duration
146   virtual unsigned long duration() const = 0;
147 
148   // Get the timebase for the stream
149   virtual vpx_rational_t timebase() const = 0;
150 
151   // Get the current frame counter, starting at 0.
152   virtual unsigned int frame() const = 0;
153 
154   // Get the current file limit.
155   virtual unsigned int limit() const = 0;
156 };
157 
158 class DummyVideoSource : public VideoSource {
159  public:
DummyVideoSource()160   DummyVideoSource()
161       : img_(nullptr), limit_(100), width_(80), height_(64),
162         format_(VPX_IMG_FMT_I420) {
163     ReallocImage();
164   }
165 
~DummyVideoSource()166   ~DummyVideoSource() override { vpx_img_free(img_); }
167 
Begin()168   void Begin() override {
169     frame_ = 0;
170     FillFrame();
171   }
172 
Next()173   void Next() override {
174     ++frame_;
175     FillFrame();
176   }
177 
img()178   vpx_image_t *img() const override {
179     return (frame_ < limit_) ? img_ : nullptr;
180   }
181 
182   // Models a stream where Timebase = 1/FPS, so pts == frame.
pts()183   vpx_codec_pts_t pts() const override { return frame_; }
184 
duration()185   unsigned long duration() const override { return 1; }
186 
timebase()187   vpx_rational_t timebase() const override {
188     const vpx_rational_t t = { 1, 30 };
189     return t;
190   }
191 
frame()192   unsigned int frame() const override { return frame_; }
193 
limit()194   unsigned int limit() const override { return limit_; }
195 
set_limit(unsigned int limit)196   void set_limit(unsigned int limit) { limit_ = limit; }
197 
SetSize(unsigned int width,unsigned int height)198   void SetSize(unsigned int width, unsigned int height) {
199     if (width != width_ || height != height_) {
200       width_ = width;
201       height_ = height;
202       ReallocImage();
203     }
204   }
205 
SetImageFormat(vpx_img_fmt_t format)206   void SetImageFormat(vpx_img_fmt_t format) {
207     if (format_ != format) {
208       format_ = format;
209       ReallocImage();
210     }
211   }
212 
213  protected:
FillFrame()214   virtual void FillFrame() {
215     if (img_) memset(img_->img_data, 0, raw_sz_);
216   }
217 
ReallocImage()218   void ReallocImage() {
219     vpx_img_free(img_);
220     img_ = vpx_img_alloc(nullptr, format_, width_, height_, 32);
221     ASSERT_NE(img_, nullptr);
222     raw_sz_ = ((img_->w + 31) & ~31u) * img_->h * img_->bps / 8;
223   }
224 
225   vpx_image_t *img_;
226   size_t raw_sz_;
227   unsigned int limit_;
228   unsigned int frame_;
229   unsigned int width_;
230   unsigned int height_;
231   vpx_img_fmt_t format_;
232 };
233 
234 class RandomVideoSource : public DummyVideoSource {
235  public:
236   RandomVideoSource(int seed = ACMRandom::DeterministicSeed())
rnd_(seed)237       : rnd_(seed), seed_(seed) {}
238 
239   // Reset the RNG to get a matching stream for the second pass
Begin()240   void Begin() override {
241     frame_ = 0;
242     rnd_.Reset(seed_);
243     FillFrame();
244   }
245 
246  protected:
247   // 15 frames of noise, followed by 15 static frames. Reset to 0 rather
248   // than holding previous frames to encourage keyframes to be thrown.
FillFrame()249   void FillFrame() override {
250     if (img_) {
251       if (frame_ % 30 < 15) {
252         for (size_t i = 0; i < raw_sz_; ++i) img_->img_data[i] = rnd_.Rand8();
253       } else {
254         memset(img_->img_data, 0, raw_sz_);
255       }
256     }
257   }
258 
259   ACMRandom rnd_;
260   int seed_;
261 };
262 
263 // Abstract base class for test video sources, which provide a stream of
264 // decompressed images to the decoder.
265 class CompressedVideoSource {
266  public:
~CompressedVideoSource()267   virtual ~CompressedVideoSource() {}
268 
269   virtual void Init() = 0;
270 
271   // Prepare the stream for reading, rewind/open as necessary.
272   virtual void Begin() = 0;
273 
274   // Advance the cursor to the next frame
275   virtual void Next() = 0;
276 
277   virtual const uint8_t *cxdata() const = 0;
278 
279   virtual size_t frame_size() const = 0;
280 
281   virtual unsigned int frame_number() const = 0;
282 };
283 
284 }  // namespace libvpx_test
285 
286 #endif  // VPX_TEST_VIDEO_SOURCE_H_
287