Deb Mukherjee | 50c59cd | 2014-10-15 16:40:12 -0700 | [diff] [blame] | 1 | /* |
| 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 Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame^] | 18 | #include "aom/vpx_image.h" |
Deb Mukherjee | 50c59cd | 2014-10-15 16:40:12 -0700 | [diff] [blame] | 19 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame^] | 20 | namespace libaom_test { |
Deb Mukherjee | 50c59cd | 2014-10-15 16:40:12 -0700 | [diff] [blame] | 21 | |
| 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. |
| 25 | class YUVVideoSource : public VideoSource { |
| 26 | public: |
| 27 | YUVVideoSource(const std::string &file_name, vpx_img_fmt format, |
clang-format | 3a826f1 | 2016-08-11 17:46:05 -0700 | [diff] [blame] | 28 | 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 Mukherjee | 50c59cd | 2014-10-15 16:40:12 -0700 | [diff] [blame] | 33 | 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-format | 3a826f1 | 2016-08-11 17:46:05 -0700 | [diff] [blame] | 40 | if (input_file_) fclose(input_file_); |
Deb Mukherjee | 50c59cd | 2014-10-15 16:40:12 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | virtual void Begin() { |
clang-format | 3a826f1 | 2016-08-11 17:46:05 -0700 | [diff] [blame] | 44 | if (input_file_) fclose(input_file_); |
Deb Mukherjee | 50c59cd | 2014-10-15 16:40:12 -0700 | [diff] [blame] | 45 | 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-format | 3a826f1 | 2016-08-11 17:46:05 -0700 | [diff] [blame] | 60 | virtual vpx_image_t *img() const { return (frame_ < limit_) ? img_ : NULL; } |
Deb Mukherjee | 50c59cd | 2014-10-15 16:40:12 -0700 | [diff] [blame] | 61 | |
| 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-format | 3a826f1 | 2016-08-11 17:46:05 -0700 | [diff] [blame] | 86 | 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 Mukherjee | 50c59cd | 2014-10-15 16:40:12 -0700 | [diff] [blame] | 95 | } |
| 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 Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame^] | 122 | } // namespace libaom_test |
Deb Mukherjee | 50c59cd | 2014-10-15 16:40:12 -0700 | [diff] [blame] | 123 | |
| 124 | #endif // TEST_YUV_VIDEO_SOURCE_H_ |