y4m_video_source.h 3.3 KB

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