ivf_file_reader.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * Copyright (c) 2019 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 MODULES_VIDEO_CODING_UTILITY_IVF_FILE_READER_H_
  11. #define MODULES_VIDEO_CODING_UTILITY_IVF_FILE_READER_H_
  12. #include <memory>
  13. #include <utility>
  14. #include "absl/types/optional.h"
  15. #include "api/video/encoded_image.h"
  16. #include "api/video_codecs/video_codec.h"
  17. #include "rtc_base/system/file_wrapper.h"
  18. namespace webrtc {
  19. class IvfFileReader {
  20. public:
  21. // Creates IvfFileReader. Returns nullptr if error acquired.
  22. static std::unique_ptr<IvfFileReader> Create(FileWrapper file);
  23. ~IvfFileReader();
  24. // Reinitializes reader. Returns false if any error acquired.
  25. bool Reset();
  26. // Returns codec type which was used to create this IVF file and which should
  27. // be used to decode EncodedImages from this file.
  28. VideoCodecType GetVideoCodecType() const { return codec_type_; }
  29. // Returns count of frames in this file.
  30. size_t GetFramesCount() const { return num_frames_; }
  31. // Returns next frame or absl::nullopt if any error acquired. Always returns
  32. // absl::nullopt after first error was spotted.
  33. absl::optional<EncodedImage> NextFrame();
  34. bool HasMoreFrames() const { return num_read_frames_ < num_frames_; }
  35. bool HasError() const { return has_error_; }
  36. uint16_t GetFrameWidth() const { return width_; }
  37. uint16_t GetFrameHeight() const { return height_; }
  38. bool Close();
  39. private:
  40. struct FrameHeader {
  41. size_t frame_size;
  42. int64_t timestamp;
  43. };
  44. explicit IvfFileReader(FileWrapper file) : file_(std::move(file)) {}
  45. // Parses codec type from specified position of the buffer. Codec type
  46. // contains kCodecTypeBytesCount bytes and caller has to ensure that buffer
  47. // won't overflow.
  48. absl::optional<VideoCodecType> ParseCodecType(uint8_t* buffer,
  49. size_t start_pos);
  50. absl::optional<FrameHeader> ReadNextFrameHeader();
  51. VideoCodecType codec_type_;
  52. size_t num_frames_;
  53. size_t num_read_frames_;
  54. uint16_t width_;
  55. uint16_t height_;
  56. bool using_capture_timestamps_;
  57. FileWrapper file_;
  58. absl::optional<FrameHeader> next_frame_header_;
  59. bool has_error_;
  60. RTC_DISALLOW_COPY_AND_ASSIGN(IvfFileReader);
  61. };
  62. } // namespace webrtc
  63. #endif // MODULES_VIDEO_CODING_UTILITY_IVF_FILE_READER_H_