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