1/* 2 * Copyright (c) 2012 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_Y4M_VIDEO_SOURCE_H_ 11#define TEST_Y4M_VIDEO_SOURCE_H_ 12#include <string> 13 14#include "test/video_source.h" 15#include "./y4minput.h" 16 17namespace libvpx_test { 18 19// This class extends VideoSource to allow parsing of raw yv12 20// so that we can do actual file encodes. 21class Y4mVideoSource : public VideoSource { 22 public: 23 Y4mVideoSource(const std::string &file_name, 24 unsigned int start, int limit) 25 : file_name_(file_name), 26 input_file_(NULL), 27 img_(new vpx_image_t()), 28 start_(start), 29 limit_(limit), 30 frame_(0), 31 framerate_numerator_(0), 32 framerate_denominator_(0), 33 y4m_() { 34 } 35 36 virtual ~Y4mVideoSource() { 37 vpx_img_free(img_.get()); 38 CloseSource(); 39 } 40 41 virtual void OpenSource() { 42 CloseSource(); 43 input_file_ = OpenTestDataFile(file_name_); 44 ASSERT_TRUE(input_file_ != NULL) << "Input file open failed. Filename: " 45 << file_name_; 46 } 47 48 virtual void ReadSourceToStart() { 49 ASSERT_TRUE(input_file_ != NULL); 50 ASSERT_FALSE(y4m_input_open(&y4m_, input_file_, NULL, 0, 0)); 51 framerate_numerator_ = y4m_.fps_n; 52 framerate_denominator_ = y4m_.fps_d; 53 frame_ = 0; 54 for (unsigned int i = 0; i < start_; i++) { 55 Next(); 56 } 57 FillFrame(); 58 } 59 60 virtual void Begin() { 61 OpenSource(); 62 ReadSourceToStart(); 63 } 64 65 virtual void Next() { 66 ++frame_; 67 FillFrame(); 68 } 69 70 virtual vpx_image_t *img() const { 71 return (frame_ < limit_) ? img_.get() : NULL; 72 } 73 74 // Models a stream where Timebase = 1/FPS, so pts == frame. 75 virtual vpx_codec_pts_t pts() const { return frame_; } 76 77 virtual unsigned long duration() const { return 1; } 78 79 virtual vpx_rational_t timebase() const { 80 const vpx_rational_t t = { framerate_denominator_, framerate_numerator_ }; 81 return t; 82 } 83 84 virtual unsigned int frame() const { return frame_; } 85 86 virtual unsigned int limit() const { return limit_; } 87 88 virtual void FillFrame() { 89 ASSERT_TRUE(input_file_ != NULL); 90 // Read a frame from input_file. 91 y4m_input_fetch_frame(&y4m_, input_file_, img_.get()); 92 } 93 94 protected: 95 void CloseSource() { 96 y4m_input_close(&y4m_); 97 y4m_ = y4m_input(); 98 if (input_file_ != NULL) { 99 fclose(input_file_); 100 input_file_ = NULL; 101 } 102 } 103 104 std::string file_name_; 105 FILE *input_file_; 106 testing::internal::scoped_ptr<vpx_image_t> img_; 107 unsigned int start_; 108 unsigned int limit_; 109 unsigned int frame_; 110 int framerate_numerator_; 111 int framerate_denominator_; 112 y4m_input y4m_; 113}; 114 115} // namespace libvpx_test 116 117#endif // TEST_Y4M_VIDEO_SOURCE_H_ 118