blob: 75f2e3ac8cd12ee7dbee22dd07f231d861891ff3 [file] [log] [blame]
Artem Titov5831dda2019-11-20 13:30:19 +01001/*
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"
philipele6542f22020-07-17 15:19:40 +020019#include "api/video_codecs/video_codec.h"
Artem Titov5831dda2019-11-20 13:30:19 +010020#include "rtc_base/system/file_wrapper.h"
21
22namespace webrtc {
23
24class IvfFileReader {
25 public:
26 // Creates IvfFileReader. Returns nullptr if error acquired.
27 static std::unique_ptr<IvfFileReader> Create(FileWrapper file);
28 ~IvfFileReader();
Byoungchan Lee604fd2f2022-01-21 09:49:39 +090029
30 IvfFileReader(const IvfFileReader&) = delete;
31 IvfFileReader& operator=(const IvfFileReader&) = delete;
32
Artem Titov5831dda2019-11-20 13:30:19 +010033 // 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.
38 VideoCodecType GetVideoCodecType() const { return codec_type_; }
39 // Returns count of frames in this file.
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();
45 bool HasMoreFrames() const { return num_read_frames_ < num_frames_; }
46 bool HasError() const { return has_error_; }
47
Artem Titova101a4f2019-11-25 23:19:42 +010048 uint16_t GetFrameWidth() const { return width_; }
49 uint16_t GetFrameHeight() const { return height_; }
50
Artem Titov5831dda2019-11-20 13:30:19 +010051 bool Close();
52
53 private:
54 struct FrameHeader {
55 size_t frame_size;
56 int64_t timestamp;
57 };
58
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 bool using_capture_timestamps_;
74 FileWrapper file_;
75
76 absl::optional<FrameHeader> next_frame_header_;
77 bool has_error_;
Artem Titov5831dda2019-11-20 13:30:19 +010078};
79
80} // namespace webrtc
81
82#endif // MODULES_VIDEO_CODING_UTILITY_IVF_FILE_READER_H_