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