John Koleszar | b9180fc | 2012-05-16 15:27:00 -0700 | [diff] [blame] | 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_ |
Johann | e3e63fb | 2012-07-23 14:55:32 -0700 | [diff] [blame] | 12 | |
| 13 | #include "test/acm_random.h" |
John Koleszar | b9180fc | 2012-05-16 15:27:00 -0700 | [diff] [blame] | 14 | #include "vpx/vpx_encoder.h" |
| 15 | |
| 16 | namespace libvpx_test { |
| 17 | |
| 18 | // Abstract base class for test video sources, which provide a stream of |
| 19 | // vpx_image_t images with associated timestamps and duration. |
| 20 | class VideoSource { |
| 21 | public: |
| 22 | virtual ~VideoSource() {} |
| 23 | |
| 24 | // Prepare the stream for reading, rewind/open as necessary. |
| 25 | virtual void Begin() = 0; |
| 26 | |
| 27 | // Advance the cursor to the next frame |
| 28 | virtual void Next() = 0; |
| 29 | |
| 30 | // Get the current video frame, or NULL on End-Of-Stream. |
| 31 | virtual vpx_image_t *img() const = 0; |
| 32 | |
| 33 | // Get the presentation timestamp of the current frame. |
| 34 | virtual vpx_codec_pts_t pts() const = 0; |
| 35 | |
| 36 | // Get the current frame's duration |
| 37 | virtual unsigned long duration() const = 0; |
| 38 | |
| 39 | // Get the timebase for the stream |
| 40 | virtual vpx_rational_t timebase() const = 0; |
| 41 | |
| 42 | // Get the current frame counter, starting at 0. |
| 43 | virtual unsigned int frame() const = 0; |
| 44 | }; |
| 45 | |
| 46 | |
| 47 | class DummyVideoSource : public VideoSource { |
| 48 | public: |
John Koleszar | 2fb29ff | 2012-05-23 12:55:27 -0700 | [diff] [blame] | 49 | DummyVideoSource() : img_(NULL), limit_(100), width_(0), height_(0) { |
| 50 | SetSize(80, 64); |
| 51 | } |
John Koleszar | b9180fc | 2012-05-16 15:27:00 -0700 | [diff] [blame] | 52 | |
| 53 | virtual ~DummyVideoSource() { vpx_img_free(img_); } |
| 54 | |
| 55 | virtual void Begin() { |
| 56 | frame_ = 0; |
| 57 | FillFrame(); |
| 58 | } |
| 59 | |
| 60 | virtual void Next() { |
| 61 | ++frame_; |
| 62 | FillFrame(); |
| 63 | } |
| 64 | |
| 65 | virtual vpx_image_t *img() const { |
| 66 | return (frame_ < limit_) ? img_ : NULL; |
| 67 | } |
| 68 | |
| 69 | // Models a stream where Timebase = 1/FPS, so pts == frame. |
| 70 | virtual vpx_codec_pts_t pts() const { return frame_; } |
| 71 | |
| 72 | virtual unsigned long duration() const { return 1; } |
| 73 | |
| 74 | virtual vpx_rational_t timebase() const { |
| 75 | const vpx_rational_t t = {1, 30}; |
| 76 | return t; |
| 77 | } |
| 78 | |
| 79 | virtual unsigned int frame() const { return frame_; } |
| 80 | |
| 81 | void SetSize(unsigned int width, unsigned int height) { |
John Koleszar | 2fb29ff | 2012-05-23 12:55:27 -0700 | [diff] [blame] | 82 | if (width != width_ || height != height_) { |
| 83 | vpx_img_free(img_); |
| 84 | raw_sz_ = ((width + 31)&~31) * height * 3 / 2; |
| 85 | img_ = vpx_img_alloc(NULL, VPX_IMG_FMT_VPXI420, width, height, 32); |
| 86 | width_ = width; |
| 87 | height_ = height; |
| 88 | } |
John Koleszar | b9180fc | 2012-05-16 15:27:00 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | protected: |
| 92 | virtual void FillFrame() { memset(img_->img_data, 0, raw_sz_); } |
| 93 | |
| 94 | vpx_image_t *img_; |
| 95 | size_t raw_sz_; |
| 96 | unsigned int limit_; |
| 97 | unsigned int frame_; |
John Koleszar | 2fb29ff | 2012-05-23 12:55:27 -0700 | [diff] [blame] | 98 | unsigned int width_; |
| 99 | unsigned int height_; |
John Koleszar | b9180fc | 2012-05-16 15:27:00 -0700 | [diff] [blame] | 100 | }; |
| 101 | |
| 102 | |
| 103 | class RandomVideoSource : public DummyVideoSource { |
Johann | e3e63fb | 2012-07-23 14:55:32 -0700 | [diff] [blame] | 104 | public: |
Johann | 9ec2552 | 2012-07-23 15:06:12 -0700 | [diff] [blame^] | 105 | RandomVideoSource(int seed = ACMRandom::DeterministicSeed()) |
| 106 | : rnd_(seed), |
| 107 | seed_(seed) { } |
Johann | e3e63fb | 2012-07-23 14:55:32 -0700 | [diff] [blame] | 108 | |
John Koleszar | b9180fc | 2012-05-16 15:27:00 -0700 | [diff] [blame] | 109 | protected: |
Johann | e3e63fb | 2012-07-23 14:55:32 -0700 | [diff] [blame] | 110 | // Reset the RNG to get a matching stream for the second pass |
| 111 | virtual void Begin() { |
| 112 | frame_ = 0; |
Johann | 9ec2552 | 2012-07-23 15:06:12 -0700 | [diff] [blame^] | 113 | rnd_.Reset(seed_); |
Johann | e3e63fb | 2012-07-23 14:55:32 -0700 | [diff] [blame] | 114 | FillFrame(); |
| 115 | } |
| 116 | |
John Koleszar | b9180fc | 2012-05-16 15:27:00 -0700 | [diff] [blame] | 117 | // 15 frames of noise, followed by 15 static frames. Reset to 0 rather |
| 118 | // than holding previous frames to encourage keyframes to be thrown. |
| 119 | virtual void FillFrame() { |
| 120 | if (frame_ % 30 < 15) |
| 121 | for (size_t i = 0; i < raw_sz_; ++i) |
Johann | e3e63fb | 2012-07-23 14:55:32 -0700 | [diff] [blame] | 122 | img_->img_data[i] = rnd_.Rand8(); |
John Koleszar | b9180fc | 2012-05-16 15:27:00 -0700 | [diff] [blame] | 123 | else |
| 124 | memset(img_->img_data, 0, raw_sz_); |
| 125 | } |
Johann | e3e63fb | 2012-07-23 14:55:32 -0700 | [diff] [blame] | 126 | |
| 127 | ACMRandom rnd_; |
Johann | 9ec2552 | 2012-07-23 15:06:12 -0700 | [diff] [blame^] | 128 | int seed_; |
John Koleszar | b9180fc | 2012-05-16 15:27:00 -0700 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | } // namespace libvpx_test |
| 132 | |
| 133 | #endif // TEST_VIDEO_SOURCE_H_ |