blob: 688e185fdaca8e45f1c18f9f3d39978297892d47 [file] [log] [blame]
John Koleszarb9180fc2012-05-16 15:27:00 -07001/*
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_
Johanne3e63fb2012-07-23 14:55:32 -070012
13#include "test/acm_random.h"
John Koleszarb9180fc2012-05-16 15:27:00 -070014#include "vpx/vpx_encoder.h"
15
16namespace 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.
20class 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
47class DummyVideoSource : public VideoSource {
48 public:
John Koleszar2fb29ff2012-05-23 12:55:27 -070049 DummyVideoSource() : img_(NULL), limit_(100), width_(0), height_(0) {
50 SetSize(80, 64);
51 }
John Koleszarb9180fc2012-05-16 15:27:00 -070052
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 Koleszar2fb29ff2012-05-23 12:55:27 -070082 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 Koleszarb9180fc2012-05-16 15:27:00 -070089 }
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 Koleszar2fb29ff2012-05-23 12:55:27 -070098 unsigned int width_;
99 unsigned int height_;
John Koleszarb9180fc2012-05-16 15:27:00 -0700100};
101
102
103class RandomVideoSource : public DummyVideoSource {
Johanne3e63fb2012-07-23 14:55:32 -0700104 public:
Johann9ec25522012-07-23 15:06:12 -0700105 RandomVideoSource(int seed = ACMRandom::DeterministicSeed())
106 : rnd_(seed),
107 seed_(seed) { }
Johanne3e63fb2012-07-23 14:55:32 -0700108
John Koleszarb9180fc2012-05-16 15:27:00 -0700109 protected:
Johanne3e63fb2012-07-23 14:55:32 -0700110 // Reset the RNG to get a matching stream for the second pass
111 virtual void Begin() {
112 frame_ = 0;
Johann9ec25522012-07-23 15:06:12 -0700113 rnd_.Reset(seed_);
Johanne3e63fb2012-07-23 14:55:32 -0700114 FillFrame();
115 }
116
John Koleszarb9180fc2012-05-16 15:27:00 -0700117 // 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)
Johanne3e63fb2012-07-23 14:55:32 -0700122 img_->img_data[i] = rnd_.Rand8();
John Koleszarb9180fc2012-05-16 15:27:00 -0700123 else
124 memset(img_->img_data, 0, raw_sz_);
125 }
Johanne3e63fb2012-07-23 14:55:32 -0700126
127 ACMRandom rnd_;
Johann9ec25522012-07-23 15:06:12 -0700128 int seed_;
John Koleszarb9180fc2012-05-16 15:27:00 -0700129};
130
131} // namespace libvpx_test
132
133#endif // TEST_VIDEO_SOURCE_H_