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