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