yuv_video_source.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright (c) 2014 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_YUV_VIDEO_SOURCE_H_
  11. #define VPX_TEST_YUV_VIDEO_SOURCE_H_
  12. #include <cstdio>
  13. #include <cstdlib>
  14. #include <string>
  15. #include "test/video_source.h"
  16. #include "vpx/vpx_image.h"
  17. namespace libvpx_test {
  18. // This class extends VideoSource to allow parsing of raw YUV
  19. // formats of various color sampling and bit-depths so that we can
  20. // do actual file encodes.
  21. class YUVVideoSource : public VideoSource {
  22. public:
  23. YUVVideoSource(const std::string &file_name, vpx_img_fmt format,
  24. unsigned int width, unsigned int height, int rate_numerator,
  25. int rate_denominator, unsigned int start, int limit)
  26. : file_name_(file_name), input_file_(NULL), img_(NULL), start_(start),
  27. limit_(limit), frame_(0), width_(0), height_(0),
  28. format_(VPX_IMG_FMT_NONE), framerate_numerator_(rate_numerator),
  29. framerate_denominator_(rate_denominator) {
  30. // This initializes format_, raw_size_, width_, height_ and allocates img.
  31. SetSize(width, height, format);
  32. }
  33. virtual ~YUVVideoSource() {
  34. vpx_img_free(img_);
  35. if (input_file_) fclose(input_file_);
  36. }
  37. virtual void Begin() {
  38. if (input_file_) fclose(input_file_);
  39. input_file_ = OpenTestDataFile(file_name_);
  40. ASSERT_TRUE(input_file_ != NULL)
  41. << "Input file open failed. Filename: " << file_name_;
  42. if (start_) {
  43. fseek(input_file_, static_cast<unsigned>(raw_size_) * start_, SEEK_SET);
  44. }
  45. frame_ = start_;
  46. FillFrame();
  47. }
  48. virtual void Next() {
  49. ++frame_;
  50. FillFrame();
  51. }
  52. virtual vpx_image_t *img() const { return (frame_ < limit_) ? img_ : NULL; }
  53. // Models a stream where Timebase = 1/FPS, so pts == frame.
  54. virtual vpx_codec_pts_t pts() const { return frame_; }
  55. virtual unsigned long duration() const { return 1; }
  56. virtual vpx_rational_t timebase() const {
  57. const vpx_rational_t t = { framerate_denominator_, framerate_numerator_ };
  58. return t;
  59. }
  60. virtual unsigned int frame() const { return frame_; }
  61. virtual unsigned int limit() const { return limit_; }
  62. virtual void SetSize(unsigned int width, unsigned int height,
  63. vpx_img_fmt format) {
  64. if (width != width_ || height != height_ || format != format_) {
  65. vpx_img_free(img_);
  66. img_ = vpx_img_alloc(NULL, format, width, height, 1);
  67. ASSERT_TRUE(img_ != NULL);
  68. width_ = width;
  69. height_ = height;
  70. format_ = format;
  71. switch (format) {
  72. case VPX_IMG_FMT_I420: raw_size_ = width * height * 3 / 2; break;
  73. case VPX_IMG_FMT_I422: raw_size_ = width * height * 2; break;
  74. case VPX_IMG_FMT_I440: raw_size_ = width * height * 2; break;
  75. case VPX_IMG_FMT_I444: raw_size_ = width * height * 3; break;
  76. case VPX_IMG_FMT_I42016: raw_size_ = width * height * 3; break;
  77. case VPX_IMG_FMT_I42216: raw_size_ = width * height * 4; break;
  78. case VPX_IMG_FMT_I44016: raw_size_ = width * height * 4; break;
  79. case VPX_IMG_FMT_I44416: raw_size_ = width * height * 6; break;
  80. default: ASSERT_TRUE(0);
  81. }
  82. }
  83. }
  84. virtual void FillFrame() {
  85. ASSERT_TRUE(input_file_ != NULL);
  86. // Read a frame from input_file.
  87. if (fread(img_->img_data, raw_size_, 1, input_file_) == 0) {
  88. limit_ = frame_;
  89. }
  90. }
  91. protected:
  92. std::string file_name_;
  93. FILE *input_file_;
  94. vpx_image_t *img_;
  95. size_t raw_size_;
  96. unsigned int start_;
  97. unsigned int limit_;
  98. unsigned int frame_;
  99. unsigned int width_;
  100. unsigned int height_;
  101. vpx_img_fmt format_;
  102. int framerate_numerator_;
  103. int framerate_denominator_;
  104. };
  105. } // namespace libvpx_test
  106. #endif // VPX_TEST_YUV_VIDEO_SOURCE_H_