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_(NULL), img_(NULL), start_(start),
31 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_TRUE(input_file_ != NULL)
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 { return (frame_ < limit_) ? img_ : NULL; }
63 // Models a stream where Timebase = 1/FPS, so pts == frame.
64 virtual vpx_codec_pts_t pts() const { return frame_; }
66 virtual unsigned long duration() const { return 1; }
68 virtual vpx_rational_t timebase() const {
69 const vpx_rational_t t = { framerate_denominator_, framerate_numerator_ };
73 virtual unsigned int frame() const { return frame_; }
75 virtual unsigned int limit() const { return limit_; }
77 virtual void SetSize(unsigned int width, unsigned int height,
79 if (width != width_ || height != height_ || format != format_) {
81 img_ = vpx_img_alloc(NULL, format, width, height, 1);
82 ASSERT_TRUE(img_ != NULL);
87 case VPX_IMG_FMT_NV12:
88 case VPX_IMG_FMT_I420: raw_size_ = width * height * 3 / 2; break;
89 case VPX_IMG_FMT_I422: raw_size_ = width * height * 2; break;
90 case VPX_IMG_FMT_I440: raw_size_ = width * height * 2; break;
91 case VPX_IMG_FMT_I444: raw_size_ = width * height * 3; break;
92 case VPX_IMG_FMT_I42016: raw_size_ = width * height * 3; break;
93 case VPX_IMG_FMT_I42216: raw_size_ = width * height * 4; break;
94 case VPX_IMG_FMT_I44016: raw_size_ = width * height * 4; break;
95 case VPX_IMG_FMT_I44416: raw_size_ = width * height * 6; break;
96 default: ASSERT_TRUE(0);
101 virtual void FillFrame() {
102 ASSERT_TRUE(input_file_ != NULL);
103 // Read a frame from input_file.
104 if (fread(img_->img_data, raw_size_, 1, input_file_) == 0) {
110 std::string file_name_;
118 unsigned int height_;
120 int framerate_numerator_;
121 int framerate_denominator_;
124 } // namespace libvpx_test
126 #endif // VPX_TEST_YUV_VIDEO_SOURCE_H_