blob: 2279d7970e3021b809f308351db3e3da6b3311ac [file] [log] [blame]
Alex Converse8a0b0a02014-01-16 15:17:41 -08001/*
Yaowu Xu2ab7ff02016-09-02 12:04:54 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Alex Converse8a0b0a02014-01-16 15:17:41 -08003 *
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.
Alex Converse8a0b0a02014-01-16 15:17:41 -080010 */
11#ifndef TEST_Y4M_VIDEO_SOURCE_H_
12#define TEST_Y4M_VIDEO_SOURCE_H_
Alex Converse35fb3442015-09-15 21:18:32 -070013#include <algorithm>
Alex Converse8a0b0a02014-01-16 15:17:41 -080014#include <string>
15
16#include "test/video_source.h"
Alex Converse8a0b0a02014-01-16 15:17:41 -080017#include "./y4minput.h"
Alex Converse8a0b0a02014-01-16 15:17:41 -080018
Yaowu Xuc27fc142016-08-22 16:08:15 -070019namespace libaom_test {
Alex Converse8a0b0a02014-01-16 15:17:41 -080020
21// This class extends VideoSource to allow parsing of raw yv12
22// so that we can do actual file encodes.
23class Y4mVideoSource : public VideoSource {
24 public:
clang-format3a826f12016-08-11 17:46:05 -070025 Y4mVideoSource(const std::string &file_name, unsigned int start, int limit)
Yaowu Xuf883b422016-08-30 14:01:10 -070026 : file_name_(file_name), input_file_(NULL), img_(new aom_image_t()),
clang-format3a826f12016-08-11 17:46:05 -070027 start_(start), limit_(limit), frame_(0), framerate_numerator_(0),
28 framerate_denominator_(0), y4m_() {}
Alex Converse8a0b0a02014-01-16 15:17:41 -080029
30 virtual ~Y4mVideoSource() {
Yaowu Xuf883b422016-08-30 14:01:10 -070031 aom_img_free(img_.get());
James Zernf651bcb2014-02-26 23:27:17 -080032 CloseSource();
Alex Converse8a0b0a02014-01-16 15:17:41 -080033 }
34
Deb Mukherjee5820c5d2014-06-12 16:53:13 -070035 virtual void OpenSource() {
James Zernf651bcb2014-02-26 23:27:17 -080036 CloseSource();
Alex Converse8a0b0a02014-01-16 15:17:41 -080037 input_file_ = OpenTestDataFile(file_name_);
38 ASSERT_TRUE(input_file_ != NULL) << "Input file open failed. Filename: "
Deb Mukherjee5820c5d2014-06-12 16:53:13 -070039 << file_name_;
40 }
Alex Converse8a0b0a02014-01-16 15:17:41 -080041
Deb Mukherjee5820c5d2014-06-12 16:53:13 -070042 virtual void ReadSourceToStart() {
43 ASSERT_TRUE(input_file_ != NULL);
44 ASSERT_FALSE(y4m_input_open(&y4m_, input_file_, NULL, 0, 0));
Alex Converse8a0b0a02014-01-16 15:17:41 -080045 framerate_numerator_ = y4m_.fps_n;
46 framerate_denominator_ = y4m_.fps_d;
Alex Converse8a0b0a02014-01-16 15:17:41 -080047 frame_ = 0;
48 for (unsigned int i = 0; i < start_; i++) {
Deb Mukherjee5820c5d2014-06-12 16:53:13 -070049 Next();
Alex Converse8a0b0a02014-01-16 15:17:41 -080050 }
Dmitry Kovalev79199e42014-07-02 22:23:38 -070051 FillFrame();
Deb Mukherjee82dc1332014-06-12 16:53:13 -070052 }
53
Deb Mukherjee5820c5d2014-06-12 16:53:13 -070054 virtual void Begin() {
55 OpenSource();
56 ReadSourceToStart();
57 }
58
Alex Converse8a0b0a02014-01-16 15:17:41 -080059 virtual void Next() {
60 ++frame_;
61 FillFrame();
62 }
63
Yaowu Xuf883b422016-08-30 14:01:10 -070064 virtual aom_image_t *img() const {
Alex Converse8a0b0a02014-01-16 15:17:41 -080065 return (frame_ < limit_) ? img_.get() : NULL;
66 }
67
68 // Models a stream where Timebase = 1/FPS, so pts == frame.
Yaowu Xuf883b422016-08-30 14:01:10 -070069 virtual aom_codec_pts_t pts() const { return frame_; }
Alex Converse8a0b0a02014-01-16 15:17:41 -080070
71 virtual unsigned long duration() const { return 1; }
72
Yaowu Xuf883b422016-08-30 14:01:10 -070073 virtual aom_rational_t timebase() const {
74 const aom_rational_t t = { framerate_denominator_, framerate_numerator_ };
Alex Converse8a0b0a02014-01-16 15:17:41 -080075 return t;
76 }
77
78 virtual unsigned int frame() const { return frame_; }
79
80 virtual unsigned int limit() const { return limit_; }
81
82 virtual void FillFrame() {
83 ASSERT_TRUE(input_file_ != NULL);
84 // Read a frame from input_file.
85 y4m_input_fetch_frame(&y4m_, input_file_, img_.get());
86 }
87
Alex Converse35fb3442015-09-15 21:18:32 -070088 // Swap buffers with another y4m source. This allows reading a new frame
89 // while keeping the old frame around. A whole Y4mSource is required and
Yaowu Xuf883b422016-08-30 14:01:10 -070090 // not just a aom_image_t because of how the y4m reader manipulates
91 // aom_image_t internals,
Alex Converse35fb3442015-09-15 21:18:32 -070092 void SwapBuffers(Y4mVideoSource *other) {
93 std::swap(other->y4m_.dst_buf, y4m_.dst_buf);
Yaowu Xuf883b422016-08-30 14:01:10 -070094 aom_image_t *tmp;
Alex Converse35fb3442015-09-15 21:18:32 -070095 tmp = other->img_.release();
96 other->img_.reset(img_.release());
97 img_.reset(tmp);
98 }
99
Alex Converse8a0b0a02014-01-16 15:17:41 -0800100 protected:
James Zernf651bcb2014-02-26 23:27:17 -0800101 void CloseSource() {
102 y4m_input_close(&y4m_);
103 y4m_ = y4m_input();
104 if (input_file_ != NULL) {
105 fclose(input_file_);
106 input_file_ = NULL;
107 }
108 }
109
Alex Converse8a0b0a02014-01-16 15:17:41 -0800110 std::string file_name_;
111 FILE *input_file_;
Yaowu Xuf883b422016-08-30 14:01:10 -0700112 testing::internal::scoped_ptr<aom_image_t> img_;
Alex Converse8a0b0a02014-01-16 15:17:41 -0800113 unsigned int start_;
114 unsigned int limit_;
115 unsigned int frame_;
116 int framerate_numerator_;
117 int framerate_denominator_;
118 y4m_input y4m_;
119};
120
Yaowu Xuc27fc142016-08-22 16:08:15 -0700121} // namespace libaom_test
Alex Converse8a0b0a02014-01-16 15:17:41 -0800122
123#endif // TEST_Y4M_VIDEO_SOURCE_H_