2 * Copyright (c) 2014 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_YUV_VIDEO_SOURCE_H_
11 #define VPX_TEST_YUV_VIDEO_SOURCE_H_
17 #include "test/video_source.h"
18 #include "vpx/vpx_image.h"
20 namespace libvpx_test {
22 // This class extends VideoSource to allow parsing of raw YUV
23 // formats of various color sampling and bit-depths so that we can
24 // do actual file encodes.
25 class YUVVideoSource : public VideoSource {
27 YUVVideoSource(const std::string &file_name, vpx_img_fmt format,
28 unsigned int width, unsigned int height, int rate_numerator,
29 int rate_denominator, unsigned int start, int limit)
30 : file_name_(file_name), input_file_(nullptr), img_(nullptr),
31 start_(start), limit_(limit), frame_(0), width_(0), height_(0),
32 format_(VPX_IMG_FMT_NONE), framerate_numerator_(rate_numerator),
33 framerate_denominator_(rate_denominator) {
34 // This initializes format_, raw_size_, width_, height_ and allocates img.
35 SetSize(width, height, format);
38 virtual ~YUVVideoSource() {
40 if (input_file_) fclose(input_file_);
43 virtual void Begin() {
44 if (input_file_) fclose(input_file_);
45 input_file_ = OpenTestDataFile(file_name_);
46 ASSERT_NE(input_file_, nullptr)
47 << "Input file open failed. Filename: " << file_name_;
49 fseek(input_file_, static_cast<unsigned>(raw_size_) * start_, SEEK_SET);
61 virtual vpx_image_t *img() const {
62 return (frame_ < limit_) ? img_ : nullptr;
65 // Models a stream where Timebase = 1/FPS, so pts == frame.
66 virtual vpx_codec_pts_t pts() const { return frame_; }
68 virtual unsigned long duration() const { return 1; }
70 virtual vpx_rational_t timebase() const {
71 const vpx_rational_t t = { framerate_denominator_, framerate_numerator_ };
75 virtual unsigned int frame() const { return frame_; }
77 virtual unsigned int limit() const { return limit_; }
79 virtual void SetSize(unsigned int width, unsigned int height,
81 if (width != width_ || height != height_ || format != format_) {
83 img_ = vpx_img_alloc(nullptr, format, width, height, 1);
84 ASSERT_NE(img_, nullptr);
89 case VPX_IMG_FMT_NV12:
90 case VPX_IMG_FMT_I420: raw_size_ = width * height * 3 / 2; break;
91 case VPX_IMG_FMT_I422: raw_size_ = width * height * 2; break;
92 case VPX_IMG_FMT_I440: raw_size_ = width * height * 2; break;
93 case VPX_IMG_FMT_I444: raw_size_ = width * height * 3; break;
94 case VPX_IMG_FMT_I42016: raw_size_ = width * height * 3; break;
95 case VPX_IMG_FMT_I42216: raw_size_ = width * height * 4; break;
96 case VPX_IMG_FMT_I44016: raw_size_ = width * height * 4; break;
97 case VPX_IMG_FMT_I44416: raw_size_ = width * height * 6; break;
98 default: ASSERT_TRUE(0);
103 virtual void FillFrame() {
104 ASSERT_NE(input_file_, nullptr);
105 // Read a frame from input_file.
106 if (fread(img_->img_data, raw_size_, 1, input_file_) == 0) {
112 std::string file_name_;
120 unsigned int height_;
122 int framerate_numerator_;
123 int framerate_denominator_;
126 } // namespace libvpx_test
128 #endif // VPX_TEST_YUV_VIDEO_SOURCE_H_