Backed out 3 changesets (bug 1790375) for causing wd failures on fetch_error.py....
[gecko.git] / third_party / aom / test / yuv_video_source.h
blob774ecc00863a3293869c008e4f93a33bf61df4fa
1 /*
2 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
4 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
11 #ifndef AOM_TEST_YUV_VIDEO_SOURCE_H_
12 #define AOM_TEST_YUV_VIDEO_SOURCE_H_
14 #include <cstdio>
15 #include <cstdlib>
16 #include <string>
18 #include "test/video_source.h"
19 #include "aom/aom_image.h"
21 namespace libaom_test {
23 // This class extends VideoSource to allow parsing of raw YUV
24 // formats of various color sampling and bit-depths so that we can
25 // do actual file encodes.
26 class YUVVideoSource : public VideoSource {
27 public:
28 YUVVideoSource(const std::string &file_name, aom_img_fmt format,
29 unsigned int width, unsigned int height, int rate_numerator,
30 int rate_denominator, unsigned int start, int limit)
31 : file_name_(file_name), input_file_(NULL), img_(NULL), start_(start),
32 limit_(limit), frame_(0), width_(0), height_(0),
33 format_(AOM_IMG_FMT_NONE), framerate_numerator_(rate_numerator),
34 framerate_denominator_(rate_denominator) {
35 // This initializes format_, raw_size_, width_, height_ and allocates img.
36 SetSize(width, height, format);
39 virtual ~YUVVideoSource() {
40 aom_img_free(img_);
41 if (input_file_) fclose(input_file_);
44 virtual void Begin() {
45 if (input_file_) fclose(input_file_);
46 input_file_ = OpenTestDataFile(file_name_);
47 ASSERT_TRUE(input_file_ != NULL)
48 << "Input file open failed. Filename: " << file_name_;
49 if (start_)
50 fseek(input_file_, static_cast<unsigned>(raw_size_) * start_, SEEK_SET);
52 frame_ = start_;
53 FillFrame();
56 virtual void Next() {
57 ++frame_;
58 FillFrame();
61 virtual aom_image_t *img() const { return (frame_ < limit_) ? img_ : NULL; }
63 // Models a stream where Timebase = 1/FPS, so pts == frame.
64 virtual aom_codec_pts_t pts() const { return frame_; }
66 virtual unsigned long duration() const { return 1; }
68 virtual aom_rational_t timebase() const {
69 const aom_rational_t t = { framerate_denominator_, framerate_numerator_ };
70 return t;
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,
78 aom_img_fmt format) {
79 if (width != width_ || height != height_ || format != format_) {
80 aom_img_free(img_);
81 img_ = aom_img_alloc(NULL, format, width, height, 1);
82 ASSERT_TRUE(img_ != NULL);
83 width_ = width;
84 height_ = height;
85 format_ = format;
86 switch (format) {
87 case AOM_IMG_FMT_I420: raw_size_ = width * height * 3 / 2; break;
88 case AOM_IMG_FMT_I422: raw_size_ = width * height * 2; break;
89 case AOM_IMG_FMT_I444: raw_size_ = width * height * 3; break;
90 case AOM_IMG_FMT_I42016: raw_size_ = width * height * 3; break;
91 case AOM_IMG_FMT_I42216: raw_size_ = width * height * 4; break;
92 case AOM_IMG_FMT_I44416: raw_size_ = width * height * 6; break;
93 default: ASSERT_TRUE(0);
98 virtual void FillFrame() {
99 ASSERT_TRUE(input_file_ != NULL);
100 // Read a frame from input_file.
101 if (fread(img_->img_data, raw_size_, 1, input_file_) == 0) {
102 limit_ = frame_;
106 protected:
107 std::string file_name_;
108 FILE *input_file_;
109 aom_image_t *img_;
110 size_t raw_size_;
111 unsigned int start_;
112 unsigned int limit_;
113 unsigned int frame_;
114 unsigned int width_;
115 unsigned int height_;
116 aom_img_fmt format_;
117 int framerate_numerator_;
118 int framerate_denominator_;
121 } // namespace libaom_test
123 #endif // AOM_TEST_YUV_VIDEO_SOURCE_H_