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_Y4M_VIDEO_SOURCE_H_ 12 #define AOM_TEST_Y4M_VIDEO_SOURCE_H_ 13 #include <algorithm> 14 #include <memory> 15 #include <string> 16 17 #include "common/y4minput.h" 18 #include "test/video_source.h" 19 20 namespace libaom_test { 21 22 // This class extends VideoSource to allow parsing of raw yv12 23 // so that we can do actual file encodes. 24 class Y4mVideoSource : public VideoSource { 25 public: Y4mVideoSource(const std::string & file_name,unsigned int start,int limit)26 Y4mVideoSource(const std::string &file_name, unsigned int start, int limit) 27 : file_name_(file_name), input_file_(nullptr), img_(new aom_image_t()), 28 start_(start), limit_(limit), frame_(0), framerate_numerator_(0), 29 framerate_denominator_(0), y4m_() {} 30 ~Y4mVideoSource()31 ~Y4mVideoSource() override { 32 aom_img_free(img_.get()); 33 CloseSource(); 34 } 35 OpenSource()36 virtual void OpenSource() { 37 CloseSource(); 38 input_file_ = OpenTestDataFile(file_name_); 39 ASSERT_NE(input_file_, nullptr) 40 << "Input file open failed. Filename: " << file_name_; 41 } 42 ReadSourceToStart()43 virtual void ReadSourceToStart() { 44 ASSERT_NE(input_file_, nullptr); 45 ASSERT_FALSE( 46 y4m_input_open(&y4m_, input_file_, nullptr, 0, AOM_CSP_UNKNOWN, 0)); 47 framerate_numerator_ = y4m_.fps_n; 48 framerate_denominator_ = y4m_.fps_d; 49 frame_ = 0; 50 for (unsigned int i = 0; i < start_; i++) { 51 Next(); 52 } 53 FillFrame(); 54 } 55 Begin()56 void Begin() override { 57 OpenSource(); 58 ReadSourceToStart(); 59 } 60 Next()61 void Next() override { 62 ++frame_; 63 FillFrame(); 64 } 65 img()66 aom_image_t *img() const override { 67 return (frame_ < limit_) ? img_.get() : nullptr; 68 } 69 70 // Models a stream where Timebase = 1/FPS, so pts == frame. pts()71 aom_codec_pts_t pts() const override { return frame_; } 72 duration()73 unsigned long duration() const override { return 1; } 74 timebase()75 aom_rational_t timebase() const override { 76 const aom_rational_t t = { framerate_denominator_, framerate_numerator_ }; 77 return t; 78 } 79 frame()80 unsigned int frame() const override { return frame_; } 81 limit()82 unsigned int limit() const override { return limit_; } 83 FillFrame()84 virtual void FillFrame() { 85 ASSERT_NE(input_file_, nullptr); 86 // Read a frame from input_file. 87 y4m_input_fetch_frame(&y4m_, input_file_, img_.get()); 88 } 89 90 // Swap buffers with another y4m source. This allows reading a new frame 91 // while keeping the old frame around. A whole Y4mSource is required and 92 // not just a aom_image_t because of how the y4m reader manipulates 93 // aom_image_t internals, SwapBuffers(Y4mVideoSource * other)94 void SwapBuffers(Y4mVideoSource *other) { 95 std::swap(other->y4m_.dst_buf, y4m_.dst_buf); 96 aom_image_t *tmp; 97 tmp = other->img_.release(); 98 other->img_.reset(img_.release()); 99 img_.reset(tmp); 100 } 101 102 protected: CloseSource()103 void CloseSource() { 104 y4m_input_close(&y4m_); 105 y4m_ = y4m_input(); 106 if (input_file_ != nullptr) { 107 fclose(input_file_); 108 input_file_ = nullptr; 109 } 110 } 111 112 std::string file_name_; 113 FILE *input_file_; 114 std::unique_ptr<aom_image_t> img_; 115 unsigned int start_; 116 unsigned int limit_; 117 unsigned int frame_; 118 int framerate_numerator_; 119 int framerate_denominator_; 120 y4m_input y4m_; 121 }; 122 123 } // namespace libaom_test 124 125 #endif // AOM_TEST_Y4M_VIDEO_SOURCE_H_ 126