1 // Copyright 2024 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 use crate::image::YuvRange; 16 use crate::internal_utils::stream::*; 17 use crate::internal_utils::*; 18 use crate::parser::mp4box::Av1CodecConfiguration; 19 use crate::*; 20 21 #[derive(Debug)] 22 struct ObuHeader { 23 obu_type: u8, 24 size: u32, 25 } 26 27 #[derive(Debug, Default)] 28 pub struct Av1SequenceHeader { 29 reduced_still_picture_header: bool, 30 max_width: u32, 31 max_height: u32, 32 bit_depth: u8, 33 yuv_format: PixelFormat, 34 #[allow(unused)] 35 chroma_sample_position: ChromaSamplePosition, 36 pub color_primaries: ColorPrimaries, 37 pub transfer_characteristics: TransferCharacteristics, 38 pub matrix_coefficients: MatrixCoefficients, 39 pub yuv_range: YuvRange, 40 config: Av1CodecConfiguration, 41 } 42 43 impl Av1SequenceHeader { parse_profile(&mut self, bits: &mut IBitStream) -> AvifResult<()>44 fn parse_profile(&mut self, bits: &mut IBitStream) -> AvifResult<()> { 45 self.config.seq_profile = bits.read(3)? as u8; 46 if self.config.seq_profile > 2 { 47 return Err(AvifError::BmffParseFailed("invalid seq_profile".into())); 48 } 49 let still_picture = bits.read_bool()?; 50 self.reduced_still_picture_header = bits.read_bool()?; 51 if self.reduced_still_picture_header && !still_picture { 52 return Err(AvifError::BmffParseFailed( 53 "invalid reduced_still_picture_header".into(), 54 )); 55 } 56 if self.reduced_still_picture_header { 57 self.config.seq_level_idx0 = bits.read(5)? as u8; 58 } else { 59 let mut buffer_delay_length = 0; 60 let mut decoder_model_info_present_flag = false; 61 let timing_info_present_flag = bits.read_bool()?; 62 if timing_info_present_flag { 63 // num_units_in_display_tick 64 bits.skip(32)?; 65 // time_scale 66 bits.skip(32)?; 67 let equal_picture_interval = bits.read_bool()?; 68 if equal_picture_interval { 69 // num_ticks_per_picture_minus_1 70 bits.skip_uvlc()?; 71 } 72 decoder_model_info_present_flag = bits.read_bool()?; 73 if decoder_model_info_present_flag { 74 let buffer_delay_length_minus_1 = bits.read(5)?; 75 buffer_delay_length = buffer_delay_length_minus_1 + 1; 76 // num_units_in_decoding_tick 77 bits.skip(32)?; 78 // buffer_removal_time_length_minus_1 79 bits.skip(5)?; 80 // frame_presentation_time_length_minus_1 81 bits.skip(5)?; 82 } 83 } 84 let initial_display_delay_present_flag = bits.read_bool()?; 85 let operating_points_cnt_minus_1 = bits.read(5)?; 86 let operating_points_cnt = operating_points_cnt_minus_1 + 1; 87 for i in 0..operating_points_cnt { 88 // operating_point_idc 89 bits.skip(12)?; 90 let seq_level_idx = bits.read(5)?; 91 if i == 0 { 92 self.config.seq_level_idx0 = seq_level_idx as u8; 93 } 94 if seq_level_idx > 7 { 95 let seq_tier = bits.read(1)?; 96 if i == 0 { 97 self.config.seq_tier0 = seq_tier as u8; 98 } 99 } 100 if decoder_model_info_present_flag { 101 let decoder_model_present_for_this_op = bits.read_bool()?; 102 if decoder_model_present_for_this_op { 103 // decoder_buffer_delay 104 bits.skip(buffer_delay_length as usize)?; 105 // encoder_buffer_delay 106 bits.skip(buffer_delay_length as usize)?; 107 // low_delay_mode_flag 108 bits.skip(1)?; 109 } 110 } 111 if initial_display_delay_present_flag { 112 let initial_display_delay_present_for_this_op = bits.read_bool()?; 113 if initial_display_delay_present_for_this_op { 114 // initial_display_delay_minus_1 115 bits.skip(4)?; 116 } 117 } 118 } 119 } 120 Ok(()) 121 } 122 parse_frame_max_dimensions(&mut self, bits: &mut IBitStream) -> AvifResult<()>123 fn parse_frame_max_dimensions(&mut self, bits: &mut IBitStream) -> AvifResult<()> { 124 let frame_width_bits_minus_1 = bits.read(4)?; 125 let frame_height_bits_minus_1 = bits.read(4)?; 126 let max_frame_width_minus_1 = bits.read(frame_width_bits_minus_1 as usize + 1)?; 127 let max_frame_height_minus_1 = bits.read(frame_height_bits_minus_1 as usize + 1)?; 128 self.max_width = checked_add!(max_frame_width_minus_1, 1)?; 129 self.max_height = checked_add!(max_frame_height_minus_1, 1)?; 130 let frame_id_numbers_present_flag = 131 if self.reduced_still_picture_header { false } else { bits.read_bool()? }; 132 if frame_id_numbers_present_flag { 133 // delta_frame_id_length_minus_2 134 bits.skip(4)?; 135 // additional_frame_id_length_minus_1 136 bits.skip(3)?; 137 } 138 Ok(()) 139 } 140 parse_enabled_features(&mut self, bits: &mut IBitStream) -> AvifResult<()>141 fn parse_enabled_features(&mut self, bits: &mut IBitStream) -> AvifResult<()> { 142 // use_128x128_superblock 143 bits.skip(1)?; 144 // enable_filter_intra 145 bits.skip(1)?; 146 // enable_intra_edge_filter 147 bits.skip(1)?; 148 if self.reduced_still_picture_header { 149 return Ok(()); 150 } 151 // enable_interintra_compound 152 bits.skip(1)?; 153 // enable_masked_compound 154 bits.skip(1)?; 155 // enable_warped_motion 156 bits.skip(1)?; 157 // enable_dual_filter 158 bits.skip(1)?; 159 let enable_order_hint = bits.read_bool()?; 160 if enable_order_hint { 161 // enable_jnt_comp 162 bits.skip(1)?; 163 // enable_ref_frame_mvs 164 bits.skip(1)?; 165 } 166 let seq_choose_screen_content_tools = bits.read_bool()?; 167 let seq_force_screen_content_tools = if seq_choose_screen_content_tools { 168 2 // SELECT_SCREEN_CONTENT_TOOLS 169 } else { 170 bits.read(1)? 171 }; 172 if seq_force_screen_content_tools > 0 { 173 let seq_choose_integer_mv = bits.read_bool()?; 174 if !seq_choose_integer_mv { 175 // seq_force_integer_mv 176 bits.skip(1)?; 177 } 178 } 179 if enable_order_hint { 180 // order_hint_bits_minus_1 181 bits.skip(3)?; 182 } 183 Ok(()) 184 } 185 parse_color_config(&mut self, bits: &mut IBitStream) -> AvifResult<()>186 fn parse_color_config(&mut self, bits: &mut IBitStream) -> AvifResult<()> { 187 self.config.high_bitdepth = bits.read_bool()?; 188 if self.config.seq_profile == 2 && self.config.high_bitdepth { 189 self.config.twelve_bit = bits.read_bool()?; 190 self.bit_depth = if self.config.twelve_bit { 12 } else { 10 }; 191 } else { 192 self.bit_depth = if self.config.high_bitdepth { 10 } else { 8 }; 193 } 194 if self.config.seq_profile != 1 { 195 self.config.monochrome = bits.read_bool()?; 196 } 197 let color_description_present_flag = bits.read_bool()?; 198 if color_description_present_flag { 199 self.color_primaries = (bits.read(8)? as u16).into(); 200 self.transfer_characteristics = (bits.read(8)? as u16).into(); 201 self.matrix_coefficients = (bits.read(8)? as u16).into(); 202 } else { 203 self.color_primaries = ColorPrimaries::Unspecified; 204 self.transfer_characteristics = TransferCharacteristics::Unspecified; 205 self.matrix_coefficients = MatrixCoefficients::Unspecified; 206 } 207 if self.config.monochrome { 208 let color_range = bits.read_bool()?; 209 self.yuv_range = if color_range { YuvRange::Full } else { YuvRange::Limited }; 210 self.config.chroma_subsampling_x = 1; 211 self.config.chroma_subsampling_y = 1; 212 self.yuv_format = PixelFormat::Yuv400; 213 return Ok(()); 214 } else if self.color_primaries == ColorPrimaries::Bt709 215 && self.transfer_characteristics == TransferCharacteristics::Srgb 216 && self.matrix_coefficients == MatrixCoefficients::Identity 217 { 218 self.yuv_range = YuvRange::Full; 219 self.yuv_format = PixelFormat::Yuv444; 220 } else { 221 let color_range = bits.read_bool()?; 222 self.yuv_range = if color_range { YuvRange::Full } else { YuvRange::Limited }; 223 match self.config.seq_profile { 224 0 => { 225 self.config.chroma_subsampling_x = 1; 226 self.config.chroma_subsampling_y = 1; 227 self.yuv_format = PixelFormat::Yuv420; 228 } 229 1 => { 230 self.yuv_format = PixelFormat::Yuv444; 231 } 232 2 => { 233 if self.bit_depth == 12 { 234 self.config.chroma_subsampling_x = bits.read(1)? as u8; 235 if self.config.chroma_subsampling_x == 1 { 236 self.config.chroma_subsampling_y = bits.read(1)? as u8; 237 } 238 } else { 239 self.config.chroma_subsampling_x = 1; 240 } 241 self.yuv_format = if self.config.chroma_subsampling_x == 1 { 242 if self.config.chroma_subsampling_y == 1 { 243 PixelFormat::Yuv420 244 } else { 245 PixelFormat::Yuv422 246 } 247 } else { 248 PixelFormat::Yuv444 249 }; 250 } 251 _ => {} // Not reached. 252 } 253 if self.config.chroma_subsampling_x == 1 && self.config.chroma_subsampling_y == 1 { 254 self.config.chroma_sample_position = bits.read(2)?.into(); 255 } 256 } 257 // separate_uv_delta_q 258 bits.skip(1)?; 259 Ok(()) 260 } 261 parse_obu_header(stream: &mut IStream) -> AvifResult<ObuHeader>262 fn parse_obu_header(stream: &mut IStream) -> AvifResult<ObuHeader> { 263 let mut bits = stream.sub_bit_stream(1)?; 264 265 // Section 5.3.2 of AV1 specification. 266 // https://aomediacodec.github.io/av1-spec/#obu-header-syntax 267 let obu_forbidden_bit = bits.read(1)?; 268 if obu_forbidden_bit != 0 { 269 return Err(AvifError::BmffParseFailed( 270 "invalid obu_forbidden_bit".into(), 271 )); 272 } 273 let obu_type = bits.read(4)? as u8; 274 let obu_extension_flag = bits.read_bool()?; 275 let obu_has_size_field = bits.read_bool()?; 276 // obu_reserved_1bit 277 bits.skip(1)?; // "The value is ignored by a decoder." 278 279 if obu_extension_flag { 280 let mut bits = stream.sub_bit_stream(1)?; 281 // temporal_id 282 bits.skip(3)?; 283 // spatial_id 284 bits.skip(2)?; 285 // extension_header_reserved_3bits 286 bits.skip(3)?; 287 } 288 289 let size = if obu_has_size_field { 290 stream.read_uleb128()? 291 } else { 292 u32_from_usize(stream.bytes_left()?)? // sz - 1 - obu_extension_flag 293 }; 294 295 Ok(ObuHeader { obu_type, size }) 296 } 297 parse_from_obus(data: &[u8]) -> AvifResult<Self>298 pub fn parse_from_obus(data: &[u8]) -> AvifResult<Self> { 299 let mut stream = IStream::create(data); 300 301 while stream.has_bytes_left()? { 302 let obu = Self::parse_obu_header(&mut stream)?; 303 if obu.obu_type != /*OBU_SEQUENCE_HEADER=*/1 { 304 // Not a sequence header. Skip this obu. 305 stream.skip(usize_from_u32(obu.size)?)?; 306 continue; 307 } 308 let mut bits = stream.sub_bit_stream(usize_from_u32(obu.size)?)?; 309 let mut sequence_header = Av1SequenceHeader::default(); 310 sequence_header.parse_profile(&mut bits)?; 311 sequence_header.parse_frame_max_dimensions(&mut bits)?; 312 sequence_header.parse_enabled_features(&mut bits)?; 313 // enable_superres 314 bits.skip(1)?; 315 // enable_cdef 316 bits.skip(1)?; 317 // enable_restoration 318 bits.skip(1)?; 319 sequence_header.parse_color_config(&mut bits)?; 320 // film_grain_params_present 321 bits.skip(1)?; 322 return Ok(sequence_header); 323 } 324 Err(AvifError::BmffParseFailed( 325 "could not parse sequence header".into(), 326 )) 327 } 328 } 329