xref: /aosp_15_r20/external/webrtc/modules/video_coding/utility/ivf_file_writer.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2016 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_WRITER_H_
12 #define MODULES_VIDEO_CODING_UTILITY_IVF_FILE_WRITER_H_
13 
14 #include <stddef.h>
15 #include <stdint.h>
16 
17 #include <memory>
18 
19 #include "api/video/encoded_image.h"
20 #include "api/video/video_codec_type.h"
21 #include "rtc_base/system/file_wrapper.h"
22 #include "rtc_base/time_utils.h"
23 
24 namespace webrtc {
25 
26 class IvfFileWriter {
27  public:
28   // Takes ownership of the file, which will be closed either through
29   // Close or ~IvfFileWriter. If writing a frame would take the file above the
30   // `byte_limit` the file will be closed, the write (and all future writes)
31   // will fail. A `byte_limit` of 0 is equivalent to no limit.
32   static std::unique_ptr<IvfFileWriter> Wrap(FileWrapper file,
33                                              size_t byte_limit);
34   ~IvfFileWriter();
35 
36   IvfFileWriter(const IvfFileWriter&) = delete;
37   IvfFileWriter& operator=(const IvfFileWriter&) = delete;
38 
39   bool WriteFrame(const EncodedImage& encoded_image, VideoCodecType codec_type);
40   bool Close();
41 
42  private:
43   explicit IvfFileWriter(FileWrapper file, size_t byte_limit);
44 
45   bool WriteHeader();
46   bool InitFromFirstFrame(const EncodedImage& encoded_image,
47                           VideoCodecType codec_type);
48   bool WriteOneSpatialLayer(int64_t timestamp,
49                             const uint8_t* data,
50                             size_t size);
51 
52   VideoCodecType codec_type_;
53   size_t bytes_written_;
54   size_t byte_limit_;
55   size_t num_frames_;
56   uint16_t width_;
57   uint16_t height_;
58   int64_t last_timestamp_;
59   bool using_capture_timestamps_;
60   rtc::TimestampWrapAroundHandler wrap_handler_;
61   FileWrapper file_;
62 };
63 
64 }  // namespace webrtc
65 
66 #endif  // MODULES_VIDEO_CODING_UTILITY_IVF_FILE_WRITER_H_
67