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 VPX_TEST_YUV_VIDEO_SOURCE_H_ 11 #define VPX_TEST_YUV_VIDEO_SOURCE_H_ 12 13 #include <cstdio> 14 #include <cstdlib> 15 #include <string> 16 17 #include "test/video_source.h" 18 #include "vpx/vpx_image.h" 19 20 namespace libvpx_test { 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: YUVVideoSource(const std::string & file_name,vpx_img_fmt format,unsigned int width,unsigned int height,int rate_numerator,int rate_denominator,unsigned int start,int limit)27 YUVVideoSource(const std::string &file_name, vpx_img_fmt format, 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_(nullptr), img_(nullptr), 31 start_(start), limit_(limit), frame_(0), width_(0), height_(0), 32 format_(VPX_IMG_FMT_NONE), framerate_numerator_(rate_numerator), 33 framerate_denominator_(rate_denominator) { 34 // This initializes format_, raw_size_, width_, height_ and allocates img. 35 SetSize(width, height, format); 36 } 37 ~YUVVideoSource()38 ~YUVVideoSource() override { 39 vpx_img_free(img_); 40 if (input_file_) fclose(input_file_); 41 } 42 Begin()43 void Begin() override { 44 if (input_file_) fclose(input_file_); 45 input_file_ = OpenTestDataFile(file_name_); 46 ASSERT_NE(input_file_, nullptr) 47 << "Input file open failed. Filename: " << file_name_; 48 if (start_) { 49 fseek(input_file_, static_cast<unsigned>(raw_size_) * start_, SEEK_SET); 50 } 51 52 frame_ = start_; 53 FillFrame(); 54 } 55 Next()56 void Next() override { 57 ++frame_; 58 FillFrame(); 59 } 60 img()61 vpx_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 vpx_codec_pts_t pts() const override { return frame_; } 67 duration()68 unsigned long duration() const override { return 1; } 69 timebase()70 vpx_rational_t timebase() const override { 71 const vpx_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,vpx_img_fmt format)79 virtual void SetSize(unsigned int width, unsigned int height, 80 vpx_img_fmt format) { 81 if (width != width_ || height != height_ || format != format_) { 82 vpx_img_free(img_); 83 img_ = vpx_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 VPX_IMG_FMT_NV12: 90 case VPX_IMG_FMT_I420: raw_size_ = width * height * 3 / 2; break; 91 case VPX_IMG_FMT_I422: raw_size_ = width * height * 2; break; 92 case VPX_IMG_FMT_I440: raw_size_ = width * height * 2; break; 93 case VPX_IMG_FMT_I444: raw_size_ = width * height * 3; break; 94 case VPX_IMG_FMT_I42016: raw_size_ = width * height * 3; break; 95 case VPX_IMG_FMT_I42216: raw_size_ = width * height * 4; break; 96 case VPX_IMG_FMT_I44016: raw_size_ = width * height * 4; break; 97 case VPX_IMG_FMT_I44416: raw_size_ = width * height * 6; break; 98 default: ASSERT_TRUE(0); 99 } 100 } 101 } 102 FillFrame()103 virtual void FillFrame() { 104 ASSERT_NE(input_file_, nullptr); 105 // Read a frame from input_file. 106 if (fread(img_->img_data, raw_size_, 1, input_file_) == 0) { 107 limit_ = frame_; 108 } 109 } 110 111 protected: 112 std::string file_name_; 113 FILE *input_file_; 114 vpx_image_t *img_; 115 size_t raw_size_; 116 unsigned int start_; 117 unsigned int limit_; 118 unsigned int frame_; 119 unsigned int width_; 120 unsigned int height_; 121 vpx_img_fmt format_; 122 int framerate_numerator_; 123 int framerate_denominator_; 124 }; 125 126 } // namespace libvpx_test 127 128 #endif // VPX_TEST_YUV_VIDEO_SOURCE_H_ 129