y4m_video_source.h 3.37 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/*
 *  Copyright (c) 2012 The WebM project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */
#ifndef TEST_Y4M_VIDEO_SOURCE_H_
#define TEST_Y4M_VIDEO_SOURCE_H_
12
#include <algorithm>
13 14 15 16 17
#include <string>

#include "test/video_source.h"
#include "./y4minput.h"

Yaowu Xu's avatar
Yaowu Xu committed
18
namespace libaom_test {
19 20 21 22 23

// This class extends VideoSource to allow parsing of raw yv12
// so that we can do actual file encodes.
class Y4mVideoSource : public VideoSource {
 public:
clang-format's avatar
clang-format committed
24 25 26 27
  Y4mVideoSource(const std::string &file_name, unsigned int start, int limit)
      : file_name_(file_name), input_file_(NULL), img_(new vpx_image_t()),
        start_(start), limit_(limit), frame_(0), framerate_numerator_(0),
        framerate_denominator_(0), y4m_() {}
28 29 30

  virtual ~Y4mVideoSource() {
    vpx_img_free(img_.get());
James Zern's avatar
James Zern committed
31
    CloseSource();
32 33
  }

34
  virtual void OpenSource() {
James Zern's avatar
James Zern committed
35
    CloseSource();
36 37
    input_file_ = OpenTestDataFile(file_name_);
    ASSERT_TRUE(input_file_ != NULL) << "Input file open failed. Filename: "
38 39
                                     << file_name_;
  }
40

41 42 43
  virtual void ReadSourceToStart() {
    ASSERT_TRUE(input_file_ != NULL);
    ASSERT_FALSE(y4m_input_open(&y4m_, input_file_, NULL, 0, 0));
44 45 46 47
    framerate_numerator_ = y4m_.fps_n;
    framerate_denominator_ = y4m_.fps_d;
    frame_ = 0;
    for (unsigned int i = 0; i < start_; i++) {
48
      Next();
49
    }
50
    FillFrame();
51 52
  }

53 54 55 56 57
  virtual void Begin() {
    OpenSource();
    ReadSourceToStart();
  }

58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
  virtual void Next() {
    ++frame_;
    FillFrame();
  }

  virtual vpx_image_t *img() const {
    return (frame_ < limit_) ? img_.get() : NULL;
  }

  // Models a stream where Timebase = 1/FPS, so pts == frame.
  virtual vpx_codec_pts_t pts() const { return frame_; }

  virtual unsigned long duration() const { return 1; }

  virtual vpx_rational_t timebase() const {
    const vpx_rational_t t = { framerate_denominator_, framerate_numerator_ };
    return t;
  }

  virtual unsigned int frame() const { return frame_; }

  virtual unsigned int limit() const { return limit_; }

  virtual void FillFrame() {
    ASSERT_TRUE(input_file_ != NULL);
    // Read a frame from input_file.
    y4m_input_fetch_frame(&y4m_, input_file_, img_.get());
  }

87 88 89 90 91 92 93 94 95 96 97 98
  // Swap buffers with another y4m source. This allows reading a new frame
  // while keeping the old frame around. A whole Y4mSource is required and
  // not just a vpx_image_t because of how the y4m reader manipulates
  // vpx_image_t internals,
  void SwapBuffers(Y4mVideoSource *other) {
    std::swap(other->y4m_.dst_buf, y4m_.dst_buf);
    vpx_image_t *tmp;
    tmp = other->img_.release();
    other->img_.reset(img_.release());
    img_.reset(tmp);
  }

99
 protected:
James Zern's avatar
James Zern committed
100 101 102 103 104 105 106 107 108
  void CloseSource() {
    y4m_input_close(&y4m_);
    y4m_ = y4m_input();
    if (input_file_ != NULL) {
      fclose(input_file_);
      input_file_ = NULL;
    }
  }

109 110 111 112 113 114 115 116 117 118 119
  std::string file_name_;
  FILE *input_file_;
  testing::internal::scoped_ptr<vpx_image_t> img_;
  unsigned int start_;
  unsigned int limit_;
  unsigned int frame_;
  int framerate_numerator_;
  int framerate_denominator_;
  y4m_input y4m_;
};

Yaowu Xu's avatar
Yaowu Xu committed
120
}  // namespace libaom_test
121 122

#endif  // TEST_Y4M_VIDEO_SOURCE_H_