Merge "Use signed variables in the lookahead."
[aom.git] / test / y4m_video_source.h
blob03d9388db84758683e4735892bbc02d615646a33
1 /*
2 * Copyright (c) 2012 The WebM project authors. All Rights Reserved.
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_Y4M_VIDEO_SOURCE_H_
11 #define TEST_Y4M_VIDEO_SOURCE_H_
12 #include <algorithm>
13 #include <string>
15 #include "test/video_source.h"
16 #include "./y4minput.h"
18 namespace libvpx_test {
20 // This class extends VideoSource to allow parsing of raw yv12
21 // so that we can do actual file encodes.
22 class Y4mVideoSource : public VideoSource {
23 public:
24 Y4mVideoSource(const std::string &file_name,
25 unsigned int start, int limit)
26 : file_name_(file_name),
27 input_file_(NULL),
28 img_(new vpx_image_t()),
29 start_(start),
30 limit_(limit),
31 frame_(0),
32 framerate_numerator_(0),
33 framerate_denominator_(0),
34 y4m_() {
37 virtual ~Y4mVideoSource() {
38 vpx_img_free(img_.get());
39 CloseSource();
42 virtual void OpenSource() {
43 CloseSource();
44 input_file_ = OpenTestDataFile(file_name_);
45 ASSERT_TRUE(input_file_ != NULL) << "Input file open failed. Filename: "
46 << file_name_;
49 virtual void ReadSourceToStart() {
50 ASSERT_TRUE(input_file_ != NULL);
51 ASSERT_FALSE(y4m_input_open(&y4m_, input_file_, NULL, 0, 0));
52 framerate_numerator_ = y4m_.fps_n;
53 framerate_denominator_ = y4m_.fps_d;
54 frame_ = 0;
55 for (unsigned int i = 0; i < start_; i++) {
56 Next();
58 FillFrame();
61 virtual void Begin() {
62 OpenSource();
63 ReadSourceToStart();
66 virtual void Next() {
67 ++frame_;
68 FillFrame();
71 virtual vpx_image_t *img() const {
72 return (frame_ < limit_) ? img_.get() : NULL;
75 // Models a stream where Timebase = 1/FPS, so pts == frame.
76 virtual vpx_codec_pts_t pts() const { return frame_; }
78 virtual unsigned long duration() const { return 1; }
80 virtual vpx_rational_t timebase() const {
81 const vpx_rational_t t = { framerate_denominator_, framerate_numerator_ };
82 return t;
85 virtual unsigned int frame() const { return frame_; }
87 virtual unsigned int limit() const { return limit_; }
89 virtual void FillFrame() {
90 ASSERT_TRUE(input_file_ != NULL);
91 // Read a frame from input_file.
92 y4m_input_fetch_frame(&y4m_, input_file_, img_.get());
95 // Swap buffers with another y4m source. This allows reading a new frame
96 // while keeping the old frame around. A whole Y4mSource is required and
97 // not just a vpx_image_t because of how the y4m reader manipulates
98 // vpx_image_t internals,
99 void SwapBuffers(Y4mVideoSource *other) {
100 std::swap(other->y4m_.dst_buf, y4m_.dst_buf);
101 vpx_image_t *tmp;
102 tmp = other->img_.release();
103 other->img_.reset(img_.release());
104 img_.reset(tmp);
107 protected:
108 void CloseSource() {
109 y4m_input_close(&y4m_);
110 y4m_ = y4m_input();
111 if (input_file_ != NULL) {
112 fclose(input_file_);
113 input_file_ = NULL;
117 std::string file_name_;
118 FILE *input_file_;
119 testing::internal::scoped_ptr<vpx_image_t> img_;
120 unsigned int start_;
121 unsigned int limit_;
122 unsigned int frame_;
123 int framerate_numerator_;
124 int framerate_denominator_;
125 y4m_input y4m_;
128 } // namespace libvpx_test
130 #endif // TEST_Y4M_VIDEO_SOURCE_H_