blob: 33a31d450fe5714f5ddffa73deb09b39347c8087 [file] [log] [blame]
Deb Mukherjee50c59cd2014-10-15 16:40:12 -07001/*
2 * Copyright (c) 2014 The WebM project authors. All Rights Reserved.
3 *
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.
9 */
10#ifndef TEST_YUV_VIDEO_SOURCE_H_
11#define TEST_YUV_VIDEO_SOURCE_H_
12
13#include <cstdio>
14#include <cstdlib>
15#include <string>
16
17#include "test/video_source.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070018#include "aom/vpx_image.h"
Deb Mukherjee50c59cd2014-10-15 16:40:12 -070019
Yaowu Xuc27fc142016-08-22 16:08:15 -070020namespace libaom_test {
Deb Mukherjee50c59cd2014-10-15 16:40:12 -070021
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.
25class YUVVideoSource : public VideoSource {
26 public:
27 YUVVideoSource(const std::string &file_name, vpx_img_fmt format,
clang-format3a826f12016-08-11 17:46:05 -070028 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),
Deb Mukherjee50c59cd2014-10-15 16:40:12 -070033 framerate_denominator_(rate_denominator) {
34 // This initializes format_, raw_size_, width_, height_ and allocates img.
35 SetSize(width, height, format);
36 }
37
38 virtual ~YUVVideoSource() {
39 vpx_img_free(img_);
clang-format3a826f12016-08-11 17:46:05 -070040 if (input_file_) fclose(input_file_);
Deb Mukherjee50c59cd2014-10-15 16:40:12 -070041 }
42
43 virtual void Begin() {
clang-format3a826f12016-08-11 17:46:05 -070044 if (input_file_) fclose(input_file_);
Deb Mukherjee50c59cd2014-10-15 16:40:12 -070045 input_file_ = OpenTestDataFile(file_name_);
46 ASSERT_TRUE(input_file_ != NULL) << "Input file open failed. Filename: "
47 << file_name_;
48 if (start_)
49 fseek(input_file_, static_cast<unsigned>(raw_size_) * start_, SEEK_SET);
50
51 frame_ = start_;
52 FillFrame();
53 }
54
55 virtual void Next() {
56 ++frame_;
57 FillFrame();
58 }
59
clang-format3a826f12016-08-11 17:46:05 -070060 virtual vpx_image_t *img() const { return (frame_ < limit_) ? img_ : NULL; }
Deb Mukherjee50c59cd2014-10-15 16:40:12 -070061
62 // Models a stream where Timebase = 1/FPS, so pts == frame.
63 virtual vpx_codec_pts_t pts() const { return frame_; }
64
65 virtual unsigned long duration() const { return 1; }
66
67 virtual vpx_rational_t timebase() const {
68 const vpx_rational_t t = { framerate_denominator_, framerate_numerator_ };
69 return t;
70 }
71
72 virtual unsigned int frame() const { return frame_; }
73
74 virtual unsigned int limit() const { return limit_; }
75
76 virtual void SetSize(unsigned int width, unsigned int height,
77 vpx_img_fmt format) {
78 if (width != width_ || height != height_ || format != format_) {
79 vpx_img_free(img_);
80 img_ = vpx_img_alloc(NULL, format, width, height, 1);
81 ASSERT_TRUE(img_ != NULL);
82 width_ = width;
83 height_ = height;
84 format_ = format;
85 switch (format) {
clang-format3a826f12016-08-11 17:46:05 -070086 case VPX_IMG_FMT_I420: raw_size_ = width * height * 3 / 2; break;
87 case VPX_IMG_FMT_I422: raw_size_ = width * height * 2; break;
88 case VPX_IMG_FMT_I440: raw_size_ = width * height * 2; break;
89 case VPX_IMG_FMT_I444: raw_size_ = width * height * 3; break;
90 case VPX_IMG_FMT_I42016: raw_size_ = width * height * 3; break;
91 case VPX_IMG_FMT_I42216: raw_size_ = width * height * 4; break;
92 case VPX_IMG_FMT_I44016: raw_size_ = width * height * 4; break;
93 case VPX_IMG_FMT_I44416: raw_size_ = width * height * 6; break;
94 default: ASSERT_TRUE(0);
Deb Mukherjee50c59cd2014-10-15 16:40:12 -070095 }
96 }
97 }
98
99 virtual void FillFrame() {
100 ASSERT_TRUE(input_file_ != NULL);
101 // Read a frame from input_file.
102 if (fread(img_->img_data, raw_size_, 1, input_file_) == 0) {
103 limit_ = frame_;
104 }
105 }
106
107 protected:
108 std::string file_name_;
109 FILE *input_file_;
110 vpx_image_t *img_;
111 size_t raw_size_;
112 unsigned int start_;
113 unsigned int limit_;
114 unsigned int frame_;
115 unsigned int width_;
116 unsigned int height_;
117 vpx_img_fmt format_;
118 int framerate_numerator_;
119 int framerate_denominator_;
120};
121
Yaowu Xuc27fc142016-08-22 16:08:15 -0700122} // namespace libaom_test
Deb Mukherjee50c59cd2014-10-15 16:40:12 -0700123
124#endif // TEST_YUV_VIDEO_SOURCE_H_