1 /* 2 mkvmerge -- utility for splicing together matroska files 3 from component media subtypes 4 5 Distributed under the GPL v2 6 see the file COPYING for details 7 or visit https://www.gnu.org/licenses/old-licenses/gpl-2.0.html 8 9 AV1 parser code 10 11 Written by Moritz Bunkus <moritz@bunkus.org>. 12 */ 13 14 #pragma once 15 16 #include "common/common_pch.h" 17 18 #include "common/math_fwd.h" 19 20 namespace mtx { 21 22 namespace bits { 23 class reader_c; 24 } 25 26 namespace av1 { 27 28 class exception: public mtx::exception { 29 public: what()30 virtual char const *what() const throw() override { 31 return "OBU exception base class"; 32 } 33 }; 34 35 class obu_without_size_unsupported_x: public exception { 36 public: what()37 virtual char const *what() const throw() override { 38 return Y("Raw OBUs without a size field are not supported."); 39 } 40 }; 41 42 class obu_invalid_structure_x: public exception { 43 public: what()44 virtual char const *what() const throw() override { 45 return Y("Encountered OBUs with invalid data."); 46 } 47 }; 48 49 namespace { 50 51 unsigned int constexpr OBU_SEQUENCE_HEADER = 1; 52 unsigned int constexpr OBU_TEMPORAL_DELIMITER = 2; 53 unsigned int constexpr OBU_FRAME_HEADER = 3; 54 unsigned int constexpr OBU_TILE_GROUP = 4; 55 unsigned int constexpr OBU_METADATA = 5; 56 unsigned int constexpr OBU_FRAME = 6; 57 unsigned int constexpr OBU_REDUNDANT_FRAME_HEADER = 7; 58 unsigned int constexpr OBU_TILE_LIST = 8; 59 unsigned int constexpr OBU_PADDING = 15; 60 61 unsigned int constexpr FRAME_TYPE_KEY = 0; 62 unsigned int constexpr FRAME_TYPE_INTER = 1; 63 unsigned int constexpr FRAME_TYPE_INTRA_ONLY = 2; 64 unsigned int constexpr FRAME_TYPE_SWITCH = 3; 65 66 unsigned int constexpr SELECT_SCREEN_CONTENT_TOOLS = 2; 67 68 unsigned int constexpr CP_BT_709 = 1; 69 unsigned int constexpr CP_UNSPECIFIED = 2; 70 71 unsigned int constexpr TC_UNSPECIFIED = 2; 72 unsigned int constexpr TC_SRGB = 13; 73 74 unsigned int constexpr MC_IDENTITY = 0; 75 unsigned int constexpr MC_UNSPECIFIED = 2; 76 77 } 78 79 struct frame_t { 80 memory_cptr mem; 81 uint64_t timestamp{}; 82 bool is_keyframe{}; 83 }; 84 85 class parser_private_c; 86 class parser_c { 87 protected: 88 std::unique_ptr<parser_private_c> const p; 89 90 public: 91 parser_c(); 92 ~parser_c(); 93 94 void set_default_duration(mtx_mp_rational_t default_duration); 95 void set_parse_sequence_header_obus_only(bool parse_sequence_header_obus_only); 96 97 void parse(unsigned char const *buffer, uint64_t buffer_size); 98 void parse(memory_c const &buffer); 99 100 void flush(); 101 bool frame_available() const; 102 frame_t get_next_frame(); 103 104 bool is_keyframe(unsigned char const *buffer, uint64_t buffer_size); 105 bool is_keyframe(memory_c const &buffer); 106 107 void debug_obu_types(unsigned char const *buffer, uint64_t buffer_size); 108 void debug_obu_types(memory_c const &buffer); 109 110 std::pair<unsigned int, unsigned int> get_pixel_dimensions() const; 111 mtx_mp_rational_t get_frame_duration() const; 112 bool headers_parsed() const; 113 memory_cptr get_av1c() const; 114 115 public: 116 static char const *get_obu_type_name(unsigned int obu_type); 117 118 protected: 119 static uint64_t read_leb128(mtx::bits::reader_c &r); 120 static uint64_t read_uvlc(mtx::bits::reader_c &r); 121 122 std::optional<uint64_t> parse_obu_common_data(unsigned char const *buffer, uint64_t buffer_size); 123 std::optional<uint64_t> parse_obu_common_data(memory_c const &buffer); 124 std::optional<uint64_t> parse_obu_common_data(); 125 void parse_sequence_header_obu(mtx::bits::reader_c &r); 126 void parse_color_config(mtx::bits::reader_c &r); 127 void parse_timing_info(mtx::bits::reader_c &r); 128 void parse_decoder_model_info(mtx::bits::reader_c &r); 129 void parse_operating_parameters_info(mtx::bits::reader_c &r); 130 void parse_frame_header_obu(mtx::bits::reader_c &r); 131 bool parse_obu(); 132 133 uint64_t get_next_timestamp(); 134 }; 135 136 }} 137