blob: 05b1d79cdf5e9a042b18dc05814be94267f5392c [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
43 bool Close();
44
45 private:
46 struct FrameHeader {
47 size_t frame_size;
48 int64_t timestamp;
49 };
50
51 explicit IvfFileReader(FileWrapper file) : file_(std::move(file)) {}
52
53 // Parses codec type from specified position of the buffer. Codec type
54 // contains kCodecTypeBytesCount bytes and caller has to ensure that buffer
55 // won't overflow.
56 absl::optional<VideoCodecType> ParseCodecType(uint8_t* buffer,
57 size_t start_pos);
58 absl::optional<FrameHeader> ReadNextFrameHeader();
59
60 VideoCodecType codec_type_;
61 size_t num_frames_;
62 size_t num_read_frames_;
63 uint16_t width_;
64 uint16_t height_;
65 bool using_capture_timestamps_;
66 FileWrapper file_;
67
68 absl::optional<FrameHeader> next_frame_header_;
69 bool has_error_;
70
71 RTC_DISALLOW_COPY_AND_ASSIGN(IvfFileReader);
72};
73
74} // namespace webrtc
75
76#endif // MODULES_VIDEO_CODING_UTILITY_IVF_FILE_READER_H_