boolcoder_test
[profile/ivi/libvpx.git] / test / video_source.h
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 #include "vpx/vpx_encoder.h"
13
14 namespace libvpx_test {
15
16 // Abstract base class for test video sources, which provide a stream of
17 // vpx_image_t images with associated timestamps and duration.
18 class VideoSource {
19  public:
20   virtual ~VideoSource() {}
21
22   // Prepare the stream for reading, rewind/open as necessary.
23   virtual void Begin() = 0;
24
25   // Advance the cursor to the next frame
26   virtual void Next() = 0;
27
28   // Get the current video frame, or NULL on End-Of-Stream.
29   virtual vpx_image_t *img() const = 0;
30
31   // Get the presentation timestamp of the current frame.
32   virtual vpx_codec_pts_t pts() const = 0;
33
34   // Get the current frame's duration
35   virtual unsigned long duration() const = 0;
36
37   // Get the timebase for the stream
38   virtual vpx_rational_t timebase() const = 0;
39
40   // Get the current frame counter, starting at 0.
41   virtual unsigned int frame() const = 0;
42 };
43
44
45 class DummyVideoSource : public VideoSource {
46  public:
47   DummyVideoSource()
48     : img_(NULL), limit_(100) { SetSize(80, 64); }
49
50   virtual ~DummyVideoSource() { vpx_img_free(img_); }
51
52   virtual void Begin() {
53     frame_ = 0;
54     FillFrame();
55   }
56
57   virtual void Next() {
58     ++frame_;
59     FillFrame();
60   }
61
62   virtual vpx_image_t *img() const {
63     return (frame_ < limit_) ? img_ : NULL;
64   }
65
66   // Models a stream where Timebase = 1/FPS, so pts == frame.
67   virtual vpx_codec_pts_t pts() const { return frame_; }
68
69   virtual unsigned long duration() const { return 1; }
70
71   virtual vpx_rational_t timebase() const {
72     const vpx_rational_t t = {1, 30};
73     return t;
74   }
75
76   virtual unsigned int frame() const { return frame_; }
77
78   void SetSize(unsigned int width, unsigned int height) {
79     vpx_img_free(img_);
80     raw_sz_ = ((width + 31)&~31) * height * 3 / 2;
81     img_ = vpx_img_alloc(NULL, VPX_IMG_FMT_VPXI420, width, height, 32);
82   }
83
84  protected:
85   virtual void FillFrame() { memset(img_->img_data, 0, raw_sz_); }
86
87   vpx_image_t *img_;
88   size_t       raw_sz_;
89   unsigned int limit_;
90   unsigned int frame_;
91 };
92
93
94 class RandomVideoSource : public DummyVideoSource {
95  protected:
96   // 15 frames of noise, followed by 15 static frames. Reset to 0 rather
97   // than holding previous frames to encourage keyframes to be thrown.
98   virtual void FillFrame() {
99     if (frame_ % 30 < 15)
100       for (size_t i = 0; i < raw_sz_; ++i)
101         img_->img_data[i] = rand();
102     else
103       memset(img_->img_data, 0, raw_sz_);
104   }
105 };
106
107 }  // namespace libvpx_test
108
109 #endif  // TEST_VIDEO_SOURCE_H_