blob: 9ff76a8d8f441bc08409ae609dd461ca818e2c8a [file] [log] [blame]
Deb Mukherjee50c59cd2014-10-15 16:40:12 -07001/*
Yaowu Xu2ab7ff02016-09-02 12:04:54 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Deb Mukherjee50c59cd2014-10-15 16:40:12 -07003 *
Yaowu Xu2ab7ff02016-09-02 12:04:54 -07004 * 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.
Deb Mukherjee50c59cd2014-10-15 16:40:12 -070010 */
11#ifndef TEST_YUV_VIDEO_SOURCE_H_
12#define TEST_YUV_VIDEO_SOURCE_H_
13
14#include <cstdio>
15#include <cstdlib>
16#include <string>
17
18#include "test/video_source.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070019#include "aom/aom_image.h"
Deb Mukherjee50c59cd2014-10-15 16:40:12 -070020
Yaowu Xuc27fc142016-08-22 16:08:15 -070021namespace libaom_test {
Deb Mukherjee50c59cd2014-10-15 16:40:12 -070022
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.
26class YUVVideoSource : public VideoSource {
27 public:
Yaowu Xuf883b422016-08-30 14:01:10 -070028 YUVVideoSource(const std::string &file_name, aom_img_fmt format,
clang-format3a826f12016-08-11 17:46:05 -070029 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),
Yaowu Xuf883b422016-08-30 14:01:10 -070033 format_(AOM_IMG_FMT_NONE), framerate_numerator_(rate_numerator),
Deb Mukherjee50c59cd2014-10-15 16:40:12 -070034 framerate_denominator_(rate_denominator) {
35 // This initializes format_, raw_size_, width_, height_ and allocates img.
36 SetSize(width, height, format);
37 }
38
39 virtual ~YUVVideoSource() {
Yaowu Xuf883b422016-08-30 14:01:10 -070040 aom_img_free(img_);
clang-format3a826f12016-08-11 17:46:05 -070041 if (input_file_) fclose(input_file_);
Deb Mukherjee50c59cd2014-10-15 16:40:12 -070042 }
43
44 virtual void Begin() {
clang-format3a826f12016-08-11 17:46:05 -070045 if (input_file_) fclose(input_file_);
Deb Mukherjee50c59cd2014-10-15 16:40:12 -070046 input_file_ = OpenTestDataFile(file_name_);
47 ASSERT_TRUE(input_file_ != NULL) << "Input file open failed. Filename: "
48 << file_name_;
49 if (start_)
50 fseek(input_file_, static_cast<unsigned>(raw_size_) * start_, SEEK_SET);
51
52 frame_ = start_;
53 FillFrame();
54 }
55
56 virtual void Next() {
57 ++frame_;
58 FillFrame();
59 }
60
Yaowu Xuf883b422016-08-30 14:01:10 -070061 virtual aom_image_t *img() const { return (frame_ < limit_) ? img_ : NULL; }
Deb Mukherjee50c59cd2014-10-15 16:40:12 -070062
63 // Models a stream where Timebase = 1/FPS, so pts == frame.
Yaowu Xuf883b422016-08-30 14:01:10 -070064 virtual aom_codec_pts_t pts() const { return frame_; }
Deb Mukherjee50c59cd2014-10-15 16:40:12 -070065
66 virtual unsigned long duration() const { return 1; }
67
Yaowu Xuf883b422016-08-30 14:01:10 -070068 virtual aom_rational_t timebase() const {
69 const aom_rational_t t = { framerate_denominator_, framerate_numerator_ };
Deb Mukherjee50c59cd2014-10-15 16:40:12 -070070 return t;
71 }
72
73 virtual unsigned int frame() const { return frame_; }
74
75 virtual unsigned int limit() const { return limit_; }
76
77 virtual void SetSize(unsigned int width, unsigned int height,
Yaowu Xuf883b422016-08-30 14:01:10 -070078 aom_img_fmt format) {
Deb Mukherjee50c59cd2014-10-15 16:40:12 -070079 if (width != width_ || height != height_ || format != format_) {
Yaowu Xuf883b422016-08-30 14:01:10 -070080 aom_img_free(img_);
81 img_ = aom_img_alloc(NULL, format, width, height, 1);
Deb Mukherjee50c59cd2014-10-15 16:40:12 -070082 ASSERT_TRUE(img_ != NULL);
83 width_ = width;
84 height_ = height;
85 format_ = format;
86 switch (format) {
Yaowu Xuf883b422016-08-30 14:01:10 -070087 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_I440: raw_size_ = width * height * 2; break;
90 case AOM_IMG_FMT_I444: raw_size_ = width * height * 3; break;
91 case AOM_IMG_FMT_I42016: raw_size_ = width * height * 3; break;
92 case AOM_IMG_FMT_I42216: raw_size_ = width * height * 4; break;
93 case AOM_IMG_FMT_I44016: raw_size_ = width * height * 4; break;
94 case AOM_IMG_FMT_I44416: raw_size_ = width * height * 6; break;
clang-format3a826f12016-08-11 17:46:05 -070095 default: ASSERT_TRUE(0);
Deb Mukherjee50c59cd2014-10-15 16:40:12 -070096 }
97 }
98 }
99
100 virtual void FillFrame() {
101 ASSERT_TRUE(input_file_ != NULL);
102 // Read a frame from input_file.
103 if (fread(img_->img_data, raw_size_, 1, input_file_) == 0) {
104 limit_ = frame_;
105 }
106 }
107
108 protected:
109 std::string file_name_;
110 FILE *input_file_;
Yaowu Xuf883b422016-08-30 14:01:10 -0700111 aom_image_t *img_;
Deb Mukherjee50c59cd2014-10-15 16:40:12 -0700112 size_t raw_size_;
113 unsigned int start_;
114 unsigned int limit_;
115 unsigned int frame_;
116 unsigned int width_;
117 unsigned int height_;
Yaowu Xuf883b422016-08-30 14:01:10 -0700118 aom_img_fmt format_;
Deb Mukherjee50c59cd2014-10-15 16:40:12 -0700119 int framerate_numerator_;
120 int framerate_denominator_;
121};
122
Yaowu Xuc27fc142016-08-22 16:08:15 -0700123} // namespace libaom_test
Deb Mukherjee50c59cd2014-10-15 16:40:12 -0700124
125#endif // TEST_YUV_VIDEO_SOURCE_H_