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 30 IvfFileReader(const IvfFileReader&) = delete; 31 IvfFileReader& operator=(const IvfFileReader&) = delete; 32 33 // Reinitializes reader. Returns false if any error acquired. 34 bool Reset(); 35 36 // Returns codec type which was used to create this IVF file and which should 37 // be used to decode EncodedImages from this file. GetVideoCodecType()38 VideoCodecType GetVideoCodecType() const { return codec_type_; } 39 // Returns count of frames in this file. GetFramesCount()40 size_t GetFramesCount() const { return num_frames_; } 41 42 // Returns next frame or absl::nullopt if any error acquired. Always returns 43 // absl::nullopt after first error was spotted. 44 absl::optional<EncodedImage> NextFrame(); HasMoreFrames()45 bool HasMoreFrames() const { return num_read_frames_ < num_frames_; } HasError()46 bool HasError() const { return has_error_; } 47 GetFrameWidth()48 uint16_t GetFrameWidth() const { return width_; } GetFrameHeight()49 uint16_t GetFrameHeight() const { return height_; } 50 51 bool Close(); 52 53 private: 54 struct FrameHeader { 55 size_t frame_size; 56 int64_t timestamp; 57 }; 58 IvfFileReader(FileWrapper file)59 explicit IvfFileReader(FileWrapper file) : file_(std::move(file)) {} 60 61 // Parses codec type from specified position of the buffer. Codec type 62 // contains kCodecTypeBytesCount bytes and caller has to ensure that buffer 63 // won't overflow. 64 absl::optional<VideoCodecType> ParseCodecType(uint8_t* buffer, 65 size_t start_pos); 66 absl::optional<FrameHeader> ReadNextFrameHeader(); 67 68 VideoCodecType codec_type_; 69 size_t num_frames_; 70 size_t num_read_frames_; 71 uint16_t width_; 72 uint16_t height_; 73 uint32_t time_scale_; 74 FileWrapper file_; 75 76 absl::optional<FrameHeader> next_frame_header_; 77 bool has_error_; 78 }; 79 80 } // namespace webrtc 81 82 #endif // MODULES_VIDEO_CODING_UTILITY_IVF_FILE_READER_H_ 83