video_source.h revision 1b362b15af34006e6a11974088a46d42b903418e
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#include <cstdio> 14#include <cstdlib> 15#include <string> 16#include "test/acm_random.h" 17#include "vpx/vpx_encoder.h" 18 19namespace libvpx_test { 20 21static FILE *OpenTestDataFile(const std::string& file_name) { 22 std::string path_to_source = file_name; 23 const char *kDataPath = getenv("LIBVPX_TEST_DATA_PATH"); 24 25 if (kDataPath) { 26 path_to_source = kDataPath; 27 path_to_source += "/"; 28 path_to_source += file_name; 29 } 30 31 return fopen(path_to_source.c_str(), "rb"); 32} 33 34// Abstract base class for test video sources, which provide a stream of 35// vpx_image_t images with associated timestamps and duration. 36class VideoSource { 37 public: 38 virtual ~VideoSource() {} 39 40 // Prepare the stream for reading, rewind/open as necessary. 41 virtual void Begin() = 0; 42 43 // Advance the cursor to the next frame 44 virtual void Next() = 0; 45 46 // Get the current video frame, or NULL on End-Of-Stream. 47 virtual vpx_image_t *img() const = 0; 48 49 // Get the presentation timestamp of the current frame. 50 virtual vpx_codec_pts_t pts() const = 0; 51 52 // Get the current frame's duration 53 virtual unsigned long duration() const = 0; 54 55 // Get the timebase for the stream 56 virtual vpx_rational_t timebase() const = 0; 57 58 // Get the current frame counter, starting at 0. 59 virtual unsigned int frame() const = 0; 60 61 // Get the current file limit. 62 virtual unsigned int limit() const = 0; 63}; 64 65 66class DummyVideoSource : public VideoSource { 67 public: 68 DummyVideoSource() : img_(NULL), limit_(100), width_(0), height_(0) { 69 SetSize(80, 64); 70 } 71 72 virtual ~DummyVideoSource() { vpx_img_free(img_); } 73 74 virtual void Begin() { 75 frame_ = 0; 76 FillFrame(); 77 } 78 79 virtual void Next() { 80 ++frame_; 81 FillFrame(); 82 } 83 84 virtual vpx_image_t *img() const { 85 return (frame_ < limit_) ? img_ : NULL; 86 } 87 88 // Models a stream where Timebase = 1/FPS, so pts == frame. 89 virtual vpx_codec_pts_t pts() const { return frame_; } 90 91 virtual unsigned long duration() const { return 1; } 92 93 virtual vpx_rational_t timebase() const { 94 const vpx_rational_t t = {1, 30}; 95 return t; 96 } 97 98 virtual unsigned int frame() const { return frame_; } 99 100 virtual unsigned int limit() const { return limit_; } 101 102 void SetSize(unsigned int width, unsigned int height) { 103 if (width != width_ || height != height_) { 104 vpx_img_free(img_); 105 raw_sz_ = ((width + 31)&~31) * height * 3 / 2; 106 img_ = vpx_img_alloc(NULL, VPX_IMG_FMT_VPXI420, width, height, 32); 107 width_ = width; 108 height_ = height; 109 } 110 } 111 112 protected: 113 virtual void FillFrame() { memset(img_->img_data, 0, raw_sz_); } 114 115 vpx_image_t *img_; 116 size_t raw_sz_; 117 unsigned int limit_; 118 unsigned int frame_; 119 unsigned int width_; 120 unsigned int height_; 121}; 122 123 124class RandomVideoSource : public DummyVideoSource { 125 public: 126 RandomVideoSource(int seed = ACMRandom::DeterministicSeed()) 127 : rnd_(seed), 128 seed_(seed) { } 129 130 protected: 131 // Reset the RNG to get a matching stream for the second pass 132 virtual void Begin() { 133 frame_ = 0; 134 rnd_.Reset(seed_); 135 FillFrame(); 136 } 137 138 // 15 frames of noise, followed by 15 static frames. Reset to 0 rather 139 // than holding previous frames to encourage keyframes to be thrown. 140 virtual void FillFrame() { 141 if (frame_ % 30 < 15) 142 for (size_t i = 0; i < raw_sz_; ++i) 143 img_->img_data[i] = rnd_.Rand8(); 144 else 145 memset(img_->img_data, 0, raw_sz_); 146 } 147 148 ACMRandom rnd_; 149 int seed_; 150}; 151 152// Abstract base class for test video sources, which provide a stream of 153// decompressed images to the decoder. 154class CompressedVideoSource { 155 public: 156 virtual ~CompressedVideoSource() {} 157 158 virtual void Init() = 0; 159 160 // Prepare the stream for reading, rewind/open as necessary. 161 virtual void Begin() = 0; 162 163 // Advance the cursor to the next frame 164 virtual void Next() = 0; 165 166 virtual const uint8_t *cxdata() const = 0; 167 168 virtual const unsigned int frame_size() const = 0; 169 170 virtual const unsigned int frame_number() const = 0; 171}; 172 173} // namespace libvpx_test 174 175#endif // TEST_VIDEO_SOURCE_H_ 176