xref: /aosp_15_r20/external/libaom/test/video_source.h (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1 /*
2  * Copyright (c) 2016, Alliance for Open Media. All rights reserved.
3  *
4  * This source code is subject to the terms of the BSD 2 Clause License and
5  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6  * was not distributed with this source code in the LICENSE file, you can
7  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8  * Media Patent License 1.0 was not distributed with this source code in the
9  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10  */
11 #ifndef AOM_TEST_VIDEO_SOURCE_H_
12 #define AOM_TEST_VIDEO_SOURCE_H_
13 
14 #if defined(_WIN32)
15 #undef NOMINMAX
16 #define NOMINMAX
17 #undef WIN32_LEAN_AND_MEAN
18 #define WIN32_LEAN_AND_MEAN
19 #include <windows.h>
20 #endif
21 #include <cstdio>
22 #include <cstdlib>
23 #include <cstring>
24 #include <memory>
25 #include <string>
26 
27 #include "aom/aom_encoder.h"
28 #include "test/acm_random.h"
29 #if !defined(_WIN32)
30 #include "gtest/gtest.h"
31 #endif
32 
33 namespace libaom_test {
34 
35 // Helper macros to ensure LIBAOM_TEST_DATA_PATH is a quoted string.
36 // These are undefined right below GetDataPath
37 // NOTE: LIBAOM_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("LIBAOM_TEST_DATA_PATH");
45   if (data_path == nullptr) {
46 #ifdef LIBAOM_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(LIBAOM_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)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[] = "libaomtest.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:
TempOutFile()103   TempOutFile() { file_ = GetTempOutFile(&file_name_); }
~TempOutFile()104   ~TempOutFile() {
105     CloseFile();
106     if (!file_name_.empty()) {
107       EXPECT_EQ(0, remove(file_name_.c_str()));
108     }
109   }
file()110   FILE *file() { return file_; }
file_name()111   const std::string &file_name() { return file_name_; }
112 
113  protected:
CloseFile()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 // aom_image_t images with associated timestamps and duration.
126 class VideoSource {
127  public:
128   virtual ~VideoSource() = default;
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 aom_image_t *img() const = 0;
138 
139   // Get the presentation timestamp of the current frame.
140   virtual aom_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 aom_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:
DummyVideoSource()157   DummyVideoSource()
158       : img_(nullptr), limit_(100), width_(80), height_(64),
159         format_(AOM_IMG_FMT_I420) {
160     ReallocImage();
161   }
162 
~DummyVideoSource()163   ~DummyVideoSource() override { aom_img_free(img_); }
164 
Begin()165   void Begin() override {
166     frame_ = 0;
167     FillFrame();
168   }
169 
Next()170   void Next() override {
171     ++frame_;
172     FillFrame();
173   }
174 
img()175   aom_image_t *img() const override {
176     return (frame_ < limit_) ? img_ : nullptr;
177   }
178 
179   // Models a stream where Timebase = 1/FPS, so pts == frame.
pts()180   aom_codec_pts_t pts() const override { return frame_; }
181 
duration()182   unsigned long duration() const override { return 1; }
183 
timebase()184   aom_rational_t timebase() const override {
185     const aom_rational_t t = { 1, 30 };
186     return t;
187   }
188 
frame()189   unsigned int frame() const override { return frame_; }
190 
limit()191   unsigned int limit() const override { return limit_; }
192 
set_limit(unsigned int limit)193   void set_limit(unsigned int limit) { limit_ = limit; }
194 
SetSize(unsigned int width,unsigned int height)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 
SetImageFormat(aom_img_fmt_t format)203   void SetImageFormat(aom_img_fmt_t format) {
204     if (format_ != format) {
205       format_ = format;
206       ReallocImage();
207     }
208   }
209 
210  protected:
FillFrame()211   virtual void FillFrame() {
212     if (img_) memset(img_->img_data, 0, raw_sz_);
213   }
214 
ReallocImage()215   void ReallocImage() {
216     aom_img_free(img_);
217     img_ = aom_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   aom_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   aom_img_fmt_t format_;
229 };
230 
231 class RandomVideoSource : public DummyVideoSource {
232  public:
233   RandomVideoSource(int seed = ACMRandom::DeterministicSeed())
rnd_(seed)234       : rnd_(seed), seed_(seed) {}
235 
236   // Reset the RNG to get a matching stream for the second pass
Begin()237   void Begin() override {
238     frame_ = 0;
239     rnd_.Reset(seed_);
240     FillFrame();
241   }
242 
243  protected:
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.
FillFrame()246   void FillFrame() override {
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   ACMRandom rnd_;
256   int seed_;
257 };
258 
259 // Abstract base class for test video sources, which provide a stream of
260 // decompressed images to the decoder.
261 class CompressedVideoSource {
262  public:
263   virtual ~CompressedVideoSource() = default;
264 
265   virtual void Init() = 0;
266 
267   // Prepare the stream for reading, rewind/open as necessary.
268   virtual void Begin() = 0;
269 
270   // Advance the cursor to the next frame
271   virtual void Next() = 0;
272 
273   virtual const uint8_t *cxdata() const = 0;
274 
275   virtual size_t frame_size() const = 0;
276 
277   virtual unsigned int frame_number() const = 0;
278 };
279 
280 }  // namespace libaom_test
281 
282 #endif  // AOM_TEST_VIDEO_SOURCE_H_
283