frame_reader.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright (c) 2011 The WebRTC 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_TESTSUPPORT_FRAME_READER_H_
  11. #define TEST_TESTSUPPORT_FRAME_READER_H_
  12. #include <stdio.h>
  13. #include <string>
  14. #include "api/scoped_refptr.h"
  15. namespace webrtc {
  16. class I420Buffer;
  17. namespace test {
  18. // Handles reading of I420 frames from video files.
  19. class FrameReader {
  20. public:
  21. virtual ~FrameReader() {}
  22. // Initializes the frame reader, i.e. opens the input file.
  23. // This must be called before reading of frames has started.
  24. // Returns false if an error has occurred, in addition to printing to stderr.
  25. virtual bool Init() = 0;
  26. // Reads a frame from the input file. On success, returns the frame.
  27. // Returns nullptr if encountering end of file or a read error.
  28. virtual rtc::scoped_refptr<I420Buffer> ReadFrame() = 0;
  29. // Closes the input file if open. Essentially makes this class impossible
  30. // to use anymore. Will also be invoked by the destructor.
  31. virtual void Close() = 0;
  32. // Frame length in bytes of a single frame image.
  33. virtual size_t FrameLength() = 0;
  34. // Total number of frames in the input video source.
  35. virtual int NumberOfFrames() = 0;
  36. };
  37. class YuvFrameReaderImpl : public FrameReader {
  38. public:
  39. // Creates a file handler. The input file is assumed to exist and be readable.
  40. // Parameters:
  41. // input_filename The file to read from.
  42. // width, height Size of each frame to read.
  43. YuvFrameReaderImpl(std::string input_filename, int width, int height);
  44. ~YuvFrameReaderImpl() override;
  45. bool Init() override;
  46. rtc::scoped_refptr<I420Buffer> ReadFrame() override;
  47. void Close() override;
  48. size_t FrameLength() override;
  49. int NumberOfFrames() override;
  50. protected:
  51. const std::string input_filename_;
  52. // It is not const, so subclasses will be able to add frame header size.
  53. size_t frame_length_in_bytes_;
  54. const int width_;
  55. const int height_;
  56. int number_of_frames_;
  57. FILE* input_file_;
  58. };
  59. class Y4mFrameReaderImpl : public YuvFrameReaderImpl {
  60. public:
  61. // Creates a file handler. The input file is assumed to exist and be readable.
  62. // Parameters:
  63. // input_filename The file to read from.
  64. // width, height Size of each frame to read.
  65. Y4mFrameReaderImpl(std::string input_filename, int width, int height);
  66. ~Y4mFrameReaderImpl() override;
  67. bool Init() override;
  68. rtc::scoped_refptr<I420Buffer> ReadFrame() override;
  69. private:
  70. // Buffer that is used to read file and frame headers.
  71. uint8_t* buffer_;
  72. };
  73. } // namespace test
  74. } // namespace webrtc
  75. #endif // TEST_TESTSUPPORT_FRAME_READER_H_