1 /* 2 * Copyright (c) 2016, Alliance for Open Media. All rights reserved. 3 * 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. 10 */ 11 #ifndef AOM_TEST_YUV_VIDEO_SOURCE_H_ 12 #define AOM_TEST_YUV_VIDEO_SOURCE_H_ 13 14 #include <cstdio> 15 #include <cstdlib> 16 #include <string> 17 18 #include "test/video_source.h" 19 #include "aom/aom_image.h" 20 21 namespace libaom_test { 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: YUVVideoSource(const std::string & file_name,aom_img_fmt format,unsigned int width,unsigned int height,int rate_numerator,int rate_denominator,unsigned int start,int limit)28 YUVVideoSource(const std::string &file_name, aom_img_fmt format, 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_(nullptr), img_(nullptr), 32 start_(start), limit_(limit), frame_(0), width_(0), height_(0), 33 format_(AOM_IMG_FMT_NONE), framerate_numerator_(rate_numerator), 34 framerate_denominator_(rate_denominator) { 35 // This initializes format_, raw_size_, width_, height_ and allocates img. 36 SetSize(width, height, format); 37 } 38 ~YUVVideoSource()39 ~YUVVideoSource() override { 40 aom_img_free(img_); 41 if (input_file_) fclose(input_file_); 42 } 43 Begin()44 void Begin() override { 45 if (input_file_) fclose(input_file_); 46 input_file_ = OpenTestDataFile(file_name_); 47 ASSERT_NE(input_file_, nullptr) 48 << "Input file open failed. Filename: " << 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 Next()56 void Next() override { 57 ++frame_; 58 FillFrame(); 59 } 60 img()61 aom_image_t *img() const override { 62 return (frame_ < limit_) ? img_ : nullptr; 63 } 64 65 // Models a stream where Timebase = 1/FPS, so pts == frame. pts()66 aom_codec_pts_t pts() const override { return frame_; } 67 duration()68 unsigned long duration() const override { return 1; } 69 timebase()70 aom_rational_t timebase() const override { 71 const aom_rational_t t = { framerate_denominator_, framerate_numerator_ }; 72 return t; 73 } 74 frame()75 unsigned int frame() const override { return frame_; } 76 limit()77 unsigned int limit() const override { return limit_; } 78 SetSize(unsigned int width,unsigned int height,aom_img_fmt format)79 virtual void SetSize(unsigned int width, unsigned int height, 80 aom_img_fmt format) { 81 if (width != width_ || height != height_ || format != format_) { 82 aom_img_free(img_); 83 img_ = aom_img_alloc(nullptr, format, width, height, 1); 84 ASSERT_NE(img_, nullptr); 85 width_ = width; 86 height_ = height; 87 format_ = format; 88 switch (format) { 89 case AOM_IMG_FMT_NV12: 90 case AOM_IMG_FMT_I420: raw_size_ = width * height * 3 / 2; break; 91 case AOM_IMG_FMT_I422: raw_size_ = width * height * 2; break; 92 case AOM_IMG_FMT_I444: raw_size_ = width * height * 3; break; 93 case AOM_IMG_FMT_I42016: raw_size_ = width * height * 3; break; 94 case AOM_IMG_FMT_I42216: raw_size_ = width * height * 4; break; 95 case AOM_IMG_FMT_I44416: raw_size_ = width * height * 6; break; 96 default: ASSERT_TRUE(0); 97 } 98 } 99 } 100 FillFrame()101 virtual void FillFrame() { 102 ASSERT_NE(input_file_, nullptr); 103 // Read a frame from input_file. 104 if (fread(img_->img_data, raw_size_, 1, input_file_) == 0) { 105 limit_ = frame_; 106 } 107 } 108 109 protected: 110 std::string file_name_; 111 FILE *input_file_; 112 aom_image_t *img_; 113 size_t raw_size_; 114 unsigned int start_; 115 unsigned int limit_; 116 unsigned int frame_; 117 unsigned int width_; 118 unsigned int height_; 119 aom_img_fmt format_; 120 int framerate_numerator_; 121 int framerate_denominator_; 122 }; 123 124 } // namespace libaom_test 125 126 #endif // AOM_TEST_YUV_VIDEO_SOURCE_H_ 127