y4m_video_source.h 3.4 KB

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