xref: /aosp_15_r20/external/libgav1/examples/file_writer.h (revision 095378508e87ed692bf8dfeb34008b65b3735891)
1 /*
2  * Copyright 2019 The libgav1 Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef LIBGAV1_EXAMPLES_FILE_WRITER_H_
18 #define LIBGAV1_EXAMPLES_FILE_WRITER_H_
19 
20 #include <cstddef>
21 #include <cstdint>
22 #include <cstdio>
23 #include <memory>
24 #include <string>
25 
26 #include "gav1/decoder_buffer.h"
27 
28 namespace libgav1 {
29 
30 // Frame based file writer class. Supports only Y4M (YUV4MPEG2) and RAW output.
31 class FileWriter {
32  public:
33   enum FileType : uint8_t {
34     kFileTypeRaw,
35     kFileTypeY4m,
36   };
37 
38   struct Y4mParameters {
39     Y4mParameters() = default;
Y4mParametersY4mParameters40     Y4mParameters(size_t width, size_t height, size_t frame_rate_numerator,
41                   size_t frame_rate_denominator,
42                   ChromaSamplePosition chroma_sample_position,
43                   ImageFormat image_format, size_t bitdepth)
44         : width(width),
45           height(height),
46           frame_rate_numerator(frame_rate_numerator),
47           frame_rate_denominator(frame_rate_denominator),
48           chroma_sample_position(chroma_sample_position),
49           image_format(image_format),
50           bitdepth(bitdepth) {}
51 
52     Y4mParameters(const Y4mParameters& rhs) = default;
53     Y4mParameters& operator=(const Y4mParameters& rhs) = default;
54     Y4mParameters(Y4mParameters&& rhs) = default;
55     Y4mParameters& operator=(Y4mParameters&& rhs) = default;
56 
57     size_t width = 0;
58     size_t height = 0;
59     size_t frame_rate_numerator = 30;
60     size_t frame_rate_denominator = 1;
61     ChromaSamplePosition chroma_sample_position = kChromaSamplePositionUnknown;
62     ImageFormat image_format = kImageFormatYuv420;
63     size_t bitdepth = 8;
64   };
65 
66   // Opens |file_name|. When |file_type| is kFileTypeY4m the Y4M file header is
67   // written out to |file_| before this method returns.
68   //
69   // Returns a FileWriter instance after the file is opened successfully for
70   // kFileTypeRaw files, and after the Y4M file header bytes are written for
71   // kFileTypeY4m files. Returns nullptr upon failure.
72   static std::unique_ptr<FileWriter> Open(const std::string& file_name,
73                                           FileType type,
74                                           const Y4mParameters* y4m_parameters);
75 
76   FileWriter() = delete;
77   FileWriter(const FileWriter&) = delete;
78   FileWriter& operator=(const FileWriter&) = delete;
79 
80   FileWriter(FileWriter&&) = default;
81   FileWriter& operator=(FileWriter&&) = default;
82 
83   // Closes |file_|.
84   ~FileWriter();
85 
86   // Writes the frame data in |frame_buffer| to |file_|. Returns true after
87   // successful write of |frame_buffer| data.
88   /*LIBGAV1_MUST_USE_RESULT*/ bool WriteFrame(
89       const DecoderBuffer& frame_buffer);
90 
91  private:
FileWriter(FILE * file)92   explicit FileWriter(FILE* file) : file_(file) {}
93 
94   bool WriteY4mFileHeader(const Y4mParameters& y4m_parameters);
95 
96   FILE* file_ = nullptr;
97   FileType file_type_ = kFileTypeRaw;
98 };
99 
100 }  // namespace libgav1
101 
102 #endif  // LIBGAV1_EXAMPLES_FILE_WRITER_H_
103