xref: /aosp_15_r20/external/libgav1/examples/file_reader.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_READER_H_
18 #define LIBGAV1_EXAMPLES_FILE_READER_H_
19 
20 #include <cstddef>
21 #include <cstdint>
22 #include <cstdio>
23 #include <memory>
24 #include <string>
25 #include <vector>
26 
27 #include "examples/file_reader_interface.h"
28 
29 namespace libgav1 {
30 
31 // Temporal Unit based file reader class. Currently supports only IVF files.
32 class FileReader : public FileReaderInterface {
33  public:
34   enum FileType {
35     kFileTypeUnknown,
36     kFileTypeIvf,
37   };
38 
39   // Creates and returns a FileReader that reads from |file_name|.
40   // If |error_tolerant| is true format and read errors are ignored,
41   // ReadTemporalUnit() may return truncated data.
42   // Returns nullptr when the file does not exist, cannot be read, or is not an
43   // IVF file.
44   static std::unique_ptr<FileReaderInterface> Open(const std::string& file_name,
45                                                    bool error_tolerant = false);
46 
47   FileReader() = delete;
48   FileReader(const FileReader&) = delete;
49   FileReader& operator=(const FileReader&) = delete;
50 
51   // Closes |file_|.
52   ~FileReader() override;
53 
54   // Reads a temporal unit from |file_| and writes the data to |tu_data|.
55   // Returns true when:
56   // - A temporal unit is read successfully, or
57   // - At end of file.
58   // When ReadTemporalUnit() is called at the end of the file, it will return
59   // true without writing any data to |tu_data|.
60   //
61   // The |timestamp| pointer is optional: callers not interested in timestamps
62   // can pass nullptr. When |timestamp| is not a nullptr, this function returns
63   // the presentation timestamp from the IVF frame header.
64   /*LIBGAV1_MUST_USE_RESULT*/ bool ReadTemporalUnit(
65       std::vector<uint8_t>* tu_data, int64_t* timestamp) override;
66 
IsEndOfFile()67   /*LIBGAV1_MUST_USE_RESULT*/ bool IsEndOfFile() const override {
68     return feof(file_) != 0;
69   }
70 
71   // The values returned by these accessors are strictly informative. No
72   // validation is performed when they are read from the IVF file header.
width()73   size_t width() const override { return width_; }
height()74   size_t height() const override { return height_; }
frame_rate()75   size_t frame_rate() const override { return frame_rate_; }
time_scale()76   size_t time_scale() const override { return time_scale_; }
77 
78  private:
FileReader(FILE * file,bool owns_file,bool error_tolerant)79   FileReader(FILE* file, bool owns_file, bool error_tolerant)
80       : file_(file), owns_file_(owns_file), error_tolerant_(error_tolerant) {}
81 
82   bool ReadIvfFileHeader();
83 
84   FILE* file_ = nullptr;
85   size_t width_ = 0;
86   size_t height_ = 0;
87   size_t frame_rate_ = 0;
88   size_t time_scale_ = 0;
89   FileType type_ = kFileTypeUnknown;
90   // True if this object owns file_ and is responsible for closing it when
91   // done.
92   const bool owns_file_;
93   const bool error_tolerant_;
94 
95   static bool registered_in_factory_;
96 };
97 
98 }  // namespace libgav1
99 
100 #endif  // LIBGAV1_EXAMPLES_FILE_READER_H_
101