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.
10 #ifndef VPX_TEST_Y4M_VIDEO_SOURCE_H_
11 #define VPX_TEST_Y4M_VIDEO_SOURCE_H_
16 #include "test/video_source.h"
17 #include "./y4minput.h"
19 namespace libvpx_test {
21 // This class extends VideoSource to allow parsing of raw yv12
22 // so that we can do actual file encodes.
23 class Y4mVideoSource : public VideoSource {
25 Y4mVideoSource(const std::string &file_name, unsigned int start, int limit)
26 : file_name_(file_name), input_file_(nullptr), img_(new vpx_image_t()),
27 start_(start), limit_(limit), frame_(0), framerate_numerator_(0),
28 framerate_denominator_(0), y4m_() {}
30 virtual ~Y4mVideoSource() {
31 vpx_img_free(img_.get());
35 virtual void OpenSource() {
37 input_file_ = OpenTestDataFile(file_name_);
38 ASSERT_NE(input_file_, nullptr)
39 << "Input file open failed. Filename: " << file_name_;
42 virtual void ReadSourceToStart() {
43 ASSERT_NE(input_file_, nullptr);
44 ASSERT_FALSE(y4m_input_open(&y4m_, input_file_, nullptr, 0, 0));
45 framerate_numerator_ = y4m_.fps_n;
46 framerate_denominator_ = y4m_.fps_d;
48 for (unsigned int i = 0; i < start_; i++) {
54 virtual void Begin() {
64 virtual vpx_image_t *img() const {
65 return (frame_ < limit_) ? img_.get() : nullptr;
68 // Models a stream where Timebase = 1/FPS, so pts == frame.
69 virtual vpx_codec_pts_t pts() const { return frame_; }
71 virtual unsigned long duration() const { return 1; }
73 virtual vpx_rational_t timebase() const {
74 const vpx_rational_t t = { framerate_denominator_, framerate_numerator_ };
78 virtual unsigned int frame() const { return frame_; }
80 virtual unsigned int limit() const { return limit_; }
82 virtual void FillFrame() {
83 ASSERT_NE(input_file_, nullptr);
84 // Read a frame from input_file.
85 y4m_input_fetch_frame(&y4m_, input_file_, img_.get());
88 // Swap buffers with another y4m source. This allows reading a new frame
89 // while keeping the old frame around. A whole Y4mSource is required and
90 // not just a vpx_image_t because of how the y4m reader manipulates
91 // vpx_image_t internals,
92 void SwapBuffers(Y4mVideoSource *other) {
93 std::swap(other->y4m_.dst_buf, y4m_.dst_buf);
95 tmp = other->img_.release();
96 other->img_.reset(img_.release());
102 y4m_input_close(&y4m_);
104 if (input_file_ != nullptr) {
106 input_file_ = nullptr;
110 std::string file_name_;
112 std::unique_ptr<vpx_image_t> img_;
116 int framerate_numerator_;
117 int framerate_denominator_;
121 } // namespace libvpx_test
123 #endif // VPX_TEST_Y4M_VIDEO_SOURCE_H_