Deb Mukherjee | 50c59cd | 2014-10-15 16:40:12 -0700 | [diff] [blame] | 1 | /* |
Yaowu Xu | 2ab7ff0 | 2016-09-02 12:04:54 -0700 | [diff] [blame] | 2 | * Copyright (c) 2016, Alliance for Open Media. All rights reserved |
Deb Mukherjee | 50c59cd | 2014-10-15 16:40:12 -0700 | [diff] [blame] | 3 | * |
Yaowu Xu | 2ab7ff0 | 2016-09-02 12:04:54 -0700 | [diff] [blame] | 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. |
Deb Mukherjee | 50c59cd | 2014-10-15 16:40:12 -0700 | [diff] [blame] | 10 | */ |
| 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 Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 19 | #include "aom/aom_image.h" |
Deb Mukherjee | 50c59cd | 2014-10-15 16:40:12 -0700 | [diff] [blame] | 20 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 21 | namespace libaom_test { |
Deb Mukherjee | 50c59cd | 2014-10-15 16:40:12 -0700 | [diff] [blame] | 22 | |
| 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: |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 28 | YUVVideoSource(const std::string &file_name, aom_img_fmt format, |
clang-format | 3a826f1 | 2016-08-11 17:46:05 -0700 | [diff] [blame] | 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), |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 33 | format_(AOM_IMG_FMT_NONE), framerate_numerator_(rate_numerator), |
Deb Mukherjee | 50c59cd | 2014-10-15 16:40:12 -0700 | [diff] [blame] | 34 | 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 Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 40 | aom_img_free(img_); |
clang-format | 3a826f1 | 2016-08-11 17:46:05 -0700 | [diff] [blame] | 41 | if (input_file_) fclose(input_file_); |
Deb Mukherjee | 50c59cd | 2014-10-15 16:40:12 -0700 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | virtual void Begin() { |
clang-format | 3a826f1 | 2016-08-11 17:46:05 -0700 | [diff] [blame] | 45 | if (input_file_) fclose(input_file_); |
Deb Mukherjee | 50c59cd | 2014-10-15 16:40:12 -0700 | [diff] [blame] | 46 | 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 Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 61 | virtual aom_image_t *img() const { return (frame_ < limit_) ? img_ : NULL; } |
Deb Mukherjee | 50c59cd | 2014-10-15 16:40:12 -0700 | [diff] [blame] | 62 | |
| 63 | // Models a stream where Timebase = 1/FPS, so pts == frame. |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 64 | virtual aom_codec_pts_t pts() const { return frame_; } |
Deb Mukherjee | 50c59cd | 2014-10-15 16:40:12 -0700 | [diff] [blame] | 65 | |
| 66 | virtual unsigned long duration() const { return 1; } |
| 67 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 68 | virtual aom_rational_t timebase() const { |
| 69 | const aom_rational_t t = { framerate_denominator_, framerate_numerator_ }; |
Deb Mukherjee | 50c59cd | 2014-10-15 16:40:12 -0700 | [diff] [blame] | 70 | 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 Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 78 | aom_img_fmt format) { |
Deb Mukherjee | 50c59cd | 2014-10-15 16:40:12 -0700 | [diff] [blame] | 79 | if (width != width_ || height != height_ || format != format_) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 80 | aom_img_free(img_); |
| 81 | img_ = aom_img_alloc(NULL, format, width, height, 1); |
Deb Mukherjee | 50c59cd | 2014-10-15 16:40:12 -0700 | [diff] [blame] | 82 | ASSERT_TRUE(img_ != NULL); |
| 83 | width_ = width; |
| 84 | height_ = height; |
| 85 | format_ = format; |
| 86 | switch (format) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 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_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-format | 3a826f1 | 2016-08-11 17:46:05 -0700 | [diff] [blame] | 95 | default: ASSERT_TRUE(0); |
Deb Mukherjee | 50c59cd | 2014-10-15 16:40:12 -0700 | [diff] [blame] | 96 | } |
| 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 Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 111 | aom_image_t *img_; |
Deb Mukherjee | 50c59cd | 2014-10-15 16:40:12 -0700 | [diff] [blame] | 112 | 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 Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 118 | aom_img_fmt format_; |
Deb Mukherjee | 50c59cd | 2014-10-15 16:40:12 -0700 | [diff] [blame] | 119 | int framerate_numerator_; |
| 120 | int framerate_denominator_; |
| 121 | }; |
| 122 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 123 | } // namespace libaom_test |
Deb Mukherjee | 50c59cd | 2014-10-15 16:40:12 -0700 | [diff] [blame] | 124 | |
| 125 | #endif // TEST_YUV_VIDEO_SOURCE_H_ |