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_Y4M_VIDEO_SOURCE_H_
11 #define TEST_Y4M_VIDEO_SOURCE_H_
12 #include <string>
13 
14 #include "test/video_source.h"
15 #include "./y4minput.h"
16 
17 namespace libvpx_test {
18 
19 // This class extends VideoSource to allow parsing of raw yv12
20 // so that we can do actual file encodes.
21 class Y4mVideoSource : public VideoSource {
22  public:
Y4mVideoSource(const std::string & file_name,unsigned int start,int limit)23   Y4mVideoSource(const std::string &file_name,
24                   unsigned int start, int limit)
25       : file_name_(file_name),
26         input_file_(NULL),
27         img_(new vpx_image_t()),
28         start_(start),
29         limit_(limit),
30         frame_(0),
31         framerate_numerator_(0),
32         framerate_denominator_(0),
33         y4m_() {
34   }
35 
~Y4mVideoSource()36   virtual ~Y4mVideoSource() {
37     vpx_img_free(img_.get());
38     CloseSource();
39   }
40 
Begin()41   virtual void Begin() {
42     CloseSource();
43     input_file_ = OpenTestDataFile(file_name_);
44     ASSERT_TRUE(input_file_ != NULL) << "Input file open failed. Filename: "
45         << file_name_;
46 
47     y4m_input_open(&y4m_, input_file_, NULL, 0, 0);
48     framerate_numerator_ = y4m_.fps_n;
49     framerate_denominator_ = y4m_.fps_d;
50 
51     frame_ = 0;
52     for (unsigned int i = 0; i < start_; i++) {
53         Next();
54     }
55 
56     FillFrame();
57   }
58 
Next()59   virtual void Next() {
60     ++frame_;
61     FillFrame();
62   }
63 
img()64   virtual vpx_image_t *img() const {
65     return (frame_ < limit_) ? img_.get() : NULL;
66   }
67 
68   // Models a stream where Timebase = 1/FPS, so pts == frame.
pts()69   virtual vpx_codec_pts_t pts() const { return frame_; }
70 
duration()71   virtual unsigned long duration() const { return 1; }
72 
timebase()73   virtual vpx_rational_t timebase() const {
74     const vpx_rational_t t = { framerate_denominator_, framerate_numerator_ };
75     return t;
76   }
77 
frame()78   virtual unsigned int frame() const { return frame_; }
79 
limit()80   virtual unsigned int limit() const { return limit_; }
81 
FillFrame()82   virtual void FillFrame() {
83     ASSERT_TRUE(input_file_ != NULL);
84     // Read a frame from input_file.
85     y4m_input_fetch_frame(&y4m_, input_file_, img_.get());
86   }
87 
88  protected:
CloseSource()89   void CloseSource() {
90     y4m_input_close(&y4m_);
91     y4m_ = y4m_input();
92     if (input_file_ != NULL) {
93       fclose(input_file_);
94       input_file_ = NULL;
95     }
96   }
97 
98   std::string file_name_;
99   FILE *input_file_;
100   testing::internal::scoped_ptr<vpx_image_t> img_;
101   unsigned int start_;
102   unsigned int limit_;
103   unsigned int frame_;
104   int framerate_numerator_;
105   int framerate_denominator_;
106   y4m_input y4m_;
107 };
108 
109 }  // namespace libvpx_test
110 
111 #endif  // TEST_Y4M_VIDEO_SOURCE_H_
112