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 TEST_VIDEO_SOURCE_H_
11 #define TEST_VIDEO_SOURCE_H_
12 
13 #if defined(_WIN32)
14 #undef NOMINMAX
15 #define NOMINMAX
16 #define WIN32_LEAN_AND_MEAN
17 #include <windows.h>
18 #endif
19 #include <cstdio>
20 #include <cstdlib>
21 #include <string>
22 #include "test/acm_random.h"
23 #include "vpx/vpx_encoder.h"
24 
25 namespace libvpx_test {
26 
27 // Helper macros to ensure LIBVPX_TEST_DATA_PATH is a quoted string.
28 // These are undefined right below GetDataPath
29 // NOTE: LIBVPX_TEST_DATA_PATH MUST NOT be a quoted string before
30 // Stringification or the GetDataPath will fail at runtime
31 #define TO_STRING(S) #S
32 #define STRINGIFY(S) TO_STRING(S)
33 
34 // A simple function to encapsulate cross platform retrieval of test data path
GetDataPath()35 static std::string GetDataPath() {
36   const char *const data_path = getenv("LIBVPX_TEST_DATA_PATH");
37   if (data_path == NULL) {
38 #ifdef LIBVPX_TEST_DATA_PATH
39     // In some environments, we cannot set environment variables
40     // Instead, we set the data path by using a preprocessor symbol
41     // which can be set from make files
42     return STRINGIFY(LIBVPX_TEST_DATA_PATH);
43 #else
44     return ".";
45 #endif
46   }
47   return data_path;
48 }
49 
50 // Undefining stringification macros because they are not used elsewhere
51 #undef TO_STRING
52 #undef STRINGIFY
53 
OpenTestDataFile(const std::string & file_name)54 inline FILE *OpenTestDataFile(const std::string& file_name) {
55   const std::string path_to_source = GetDataPath() + "/" + file_name;
56   return fopen(path_to_source.c_str(), "rb");
57 }
58 
GetTempOutFile(std::string * file_name)59 static FILE *GetTempOutFile(std::string *file_name) {
60   file_name->clear();
61 #if defined(_WIN32)
62   char fname[MAX_PATH];
63   char tmppath[MAX_PATH];
64   if (GetTempPathA(MAX_PATH, tmppath)) {
65     // Assume for now that the filename generated is unique per process
66     if (GetTempFileNameA(tmppath, "lvx", 0, fname)) {
67       file_name->assign(fname);
68       return fopen(fname, "wb+");
69     }
70   }
71   return NULL;
72 #else
73   return tmpfile();
74 #endif
75 }
76 
77 class TempOutFile {
78  public:
TempOutFile()79   TempOutFile() {
80     file_ = GetTempOutFile(&file_name_);
81   }
~TempOutFile()82   ~TempOutFile() {
83     CloseFile();
84     if (!file_name_.empty()) {
85       EXPECT_EQ(0, remove(file_name_.c_str()));
86     }
87   }
file()88   FILE *file() {
89     return file_;
90   }
file_name()91   const std::string& file_name() {
92     return file_name_;
93   }
94 
95  protected:
CloseFile()96   void CloseFile() {
97     if (file_) {
98       fclose(file_);
99       file_ = NULL;
100     }
101   }
102   FILE *file_;
103   std::string file_name_;
104 };
105 
106 // Abstract base class for test video sources, which provide a stream of
107 // vpx_image_t images with associated timestamps and duration.
108 class VideoSource {
109  public:
~VideoSource()110   virtual ~VideoSource() {}
111 
112   // Prepare the stream for reading, rewind/open as necessary.
113   virtual void Begin() = 0;
114 
115   // Advance the cursor to the next frame
116   virtual void Next() = 0;
117 
118   // Get the current video frame, or NULL on End-Of-Stream.
119   virtual vpx_image_t *img() const = 0;
120 
121   // Get the presentation timestamp of the current frame.
122   virtual vpx_codec_pts_t pts() const = 0;
123 
124   // Get the current frame's duration
125   virtual unsigned long duration() const = 0;
126 
127   // Get the timebase for the stream
128   virtual vpx_rational_t timebase() const = 0;
129 
130   // Get the current frame counter, starting at 0.
131   virtual unsigned int frame() const = 0;
132 
133   // Get the current file limit.
134   virtual unsigned int limit() const = 0;
135 };
136 
137 
138 class DummyVideoSource : public VideoSource {
139  public:
DummyVideoSource()140   DummyVideoSource()
141       : img_(NULL),
142         limit_(100),
143         width_(80),
144         height_(64),
145         format_(VPX_IMG_FMT_I420) {
146     ReallocImage();
147   }
148 
~DummyVideoSource()149   virtual ~DummyVideoSource() { vpx_img_free(img_); }
150 
Begin()151   virtual void Begin() {
152     frame_ = 0;
153     FillFrame();
154   }
155 
Next()156   virtual void Next() {
157     ++frame_;
158     FillFrame();
159   }
160 
img()161   virtual vpx_image_t *img() const {
162     return (frame_ < limit_) ? img_ : NULL;
163   }
164 
165   // Models a stream where Timebase = 1/FPS, so pts == frame.
pts()166   virtual vpx_codec_pts_t pts() const { return frame_; }
167 
duration()168   virtual unsigned long duration() const { return 1; }
169 
timebase()170   virtual vpx_rational_t timebase() const {
171     const vpx_rational_t t = {1, 30};
172     return t;
173   }
174 
frame()175   virtual unsigned int frame() const { return frame_; }
176 
limit()177   virtual unsigned int limit() const { return limit_; }
178 
set_limit(unsigned int limit)179   void set_limit(unsigned int limit) {
180     limit_ = limit;
181   }
182 
SetSize(unsigned int width,unsigned int height)183   void SetSize(unsigned int width, unsigned int height) {
184     if (width != width_ || height != height_) {
185       width_ = width;
186       height_ = height;
187       ReallocImage();
188     }
189   }
190 
SetImageFormat(vpx_img_fmt_t format)191   void SetImageFormat(vpx_img_fmt_t format) {
192     if (format_ != format) {
193       format_ = format;
194       ReallocImage();
195     }
196   }
197 
198  protected:
FillFrame()199   virtual void FillFrame() { if (img_) memset(img_->img_data, 0, raw_sz_); }
200 
ReallocImage()201   void ReallocImage() {
202     vpx_img_free(img_);
203     img_ = vpx_img_alloc(NULL, format_, width_, height_, 32);
204     raw_sz_ = ((img_->w + 31) & ~31) * img_->h * img_->bps / 8;
205   }
206 
207   vpx_image_t *img_;
208   size_t       raw_sz_;
209   unsigned int limit_;
210   unsigned int frame_;
211   unsigned int width_;
212   unsigned int height_;
213   vpx_img_fmt_t format_;
214 };
215 
216 
217 class RandomVideoSource : public DummyVideoSource {
218  public:
219   RandomVideoSource(int seed = ACMRandom::DeterministicSeed())
rnd_(seed)220       : rnd_(seed),
221         seed_(seed) { }
222 
223  protected:
224   // Reset the RNG to get a matching stream for the second pass
Begin()225   virtual void Begin() {
226     frame_ = 0;
227     rnd_.Reset(seed_);
228     FillFrame();
229   }
230 
231   // 15 frames of noise, followed by 15 static frames. Reset to 0 rather
232   // than holding previous frames to encourage keyframes to be thrown.
FillFrame()233   virtual void FillFrame() {
234     if (img_) {
235       if (frame_ % 30 < 15)
236         for (size_t i = 0; i < raw_sz_; ++i)
237           img_->img_data[i] = rnd_.Rand8();
238       else
239         memset(img_->img_data, 0, raw_sz_);
240     }
241   }
242 
243   ACMRandom rnd_;
244   int seed_;
245 };
246 
247 // Abstract base class for test video sources, which provide a stream of
248 // decompressed images to the decoder.
249 class CompressedVideoSource {
250  public:
~CompressedVideoSource()251   virtual ~CompressedVideoSource() {}
252 
253   virtual void Init() = 0;
254 
255   // Prepare the stream for reading, rewind/open as necessary.
256   virtual void Begin() = 0;
257 
258   // Advance the cursor to the next frame
259   virtual void Next() = 0;
260 
261   virtual const uint8_t *cxdata() const = 0;
262 
263   virtual size_t frame_size() const = 0;
264 
265   virtual unsigned int frame_number() const = 0;
266 };
267 
268 }  // namespace libvpx_test
269 
270 #endif  // TEST_VIDEO_SOURCE_H_
271