edith_codecs
Clone
https://git.tahinli.com/Tahinli/edith_codecs.git
0 watching·0 forks·Rust·370.3 MiB·9b67700·Tahinli·3 days ago·4021 commits
| 1 | //! Stateless AV1 decoding. |
| 2 | |
| 3 | use std::rc::Rc; |
| 4 | |
| 5 | use ec_av1_syntax::{ |
| 6 | Av1Parser, FilmGrainParams, FrameHeader, FrameType, NUM_REF_FRAMES, ObuKind, REFS_PER_FRAME, |
| 7 | SequenceHeader, Tile, |
| 8 | }; |
| 9 | use ec_va::caps::Profile; |
| 10 | use ec_va::va::BufferType; |
| 11 | use ec_va::{Display, picture}; |
| 12 | |
| 13 | use super::{ReadyFrames, Session, StreamInfo}; |
| 14 | use crate::ref_slots::RefSlots; |
| 15 | use crate::error::{Error, Result}; |
| 16 | use crate::frame::Frame; |
| 17 | use crate::params::av1::{ |
| 18 | FilmGrainInfoFields, FilmGrainStructAV1, LoopFilterInfoFields, LoopRestorationFields, |
| 19 | ModeControlFields, PicInfoFields, PictureParameterBufferAV1, QMatrixFields, SegmentInfoFields, |
| 20 | SeqInfoFields, SliceParameterBufferAV1, WarpedMotionParamsAV1, slice_buffer, |
| 21 | }; |
| 22 | use crate::pool::PooledSurface; |
| 23 | |
| 24 | /// Reference slots plus the picture being decoded plus caller-held frames. |
| 25 | const EXTRA_SURFACES: usize = 6; |
| 26 | |
| 27 | /// Grain outputs held alongside the reference slots, plus the one being |
| 28 | /// decoded: a grained picture is a second surface, so a stream with film grain |
| 29 | /// needs room for both. Zero for a stream without grain. |
| 30 | const GRAIN_SURFACES: usize = NUM_REF_FRAMES + 2; |
| 31 | |
| 32 | /// A stateless AV1 decoder. |
| 33 | pub struct Av1Decoder { |
| 34 | display: Rc<Display>, |
| 35 | parser: Av1Parser, |
| 36 | session: Option<Session>, |
| 37 | /// Each slot's grain-free reconstruction: what later frames predict from. |
| 38 | refs: RefSlots<Rc<PooledSurface>>, |
| 39 | /// Each slot's grained copy, when the frame in it was decoded with film |
| 40 | /// grain. A `show_existing_frame` outputs this copy (spec 7.21 reloads the |
| 41 | /// grain parameters of the frame it shows), so the driver's grain pass runs |
| 42 | /// once, at decode time, and the slot keeps the result. |
| 43 | grained: RefSlots<Rc<PooledSurface>>, |
| 44 | ready: ReadyFrames, |
| 45 | } |
| 46 | |
| 47 | impl Av1Decoder { |
| 48 | /// A decoder with no stream state. |
| 49 | pub fn new(display: &Rc<Display>) -> Av1Decoder { |
| 50 | Av1Decoder { |
| 51 | display: Rc::clone(display), |
| 52 | parser: Av1Parser::new(), |
| 53 | session: None, |
| 54 | refs: RefSlots::new(), |
| 55 | grained: RefSlots::new(), |
| 56 | ready: ReadyFrames::default(), |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /// Decode one temporal unit — every OBU of it, in order. |
| 61 | pub fn decode(&mut self, data: &[u8], timestamp: i64) -> Result<()> { |
| 62 | if data.is_empty() { |
| 63 | return Ok(()); |
| 64 | } |
| 65 | // Tile offsets in the parse result are relative to `data`, and `data` |
| 66 | // is what the driver is handed, so no rebasing is needed anywhere. |
| 67 | let obus = self.parser.parse_temporal_unit(data)?; |
| 68 | let mut pending: Option<Box<FrameHeader>> = None; |
| 69 | for obu in obus { |
| 70 | match obu.kind { |
| 71 | ObuKind::FrameHeader(header) => { |
| 72 | if header.show_existing_frame { |
| 73 | self.show_existing(&header, timestamp)?; |
| 74 | } else { |
| 75 | pending = Some(header); |
| 76 | } |
| 77 | } |
| 78 | ObuKind::TileGroup(tiles) => { |
| 79 | if let Some(header) = pending.take() { |
| 80 | self.decode_frame(&header, &tiles, data, timestamp)?; |
| 81 | } |
| 82 | } |
| 83 | ObuKind::Frame(header, tiles) => { |
| 84 | if header.show_existing_frame { |
| 85 | self.show_existing(&header, timestamp)?; |
| 86 | } else { |
| 87 | self.decode_frame(&header, &tiles, data, timestamp)?; |
| 88 | } |
| 89 | } |
| 90 | _ => {} |
| 91 | } |
| 92 | } |
| 93 | Ok(()) |
| 94 | } |
| 95 | |
| 96 | /// The next frame to show. |
| 97 | pub fn next_frame(&mut self) -> Option<Frame> { |
| 98 | self.ready.pop() |
| 99 | } |
| 100 | |
| 101 | /// Nothing is buffered beyond the reference slots. |
| 102 | pub fn flush(&mut self) {} |
| 103 | |
| 104 | /// Drop reference state after a seek. |
| 105 | pub fn reset(&mut self) { |
| 106 | self.parser = Av1Parser::new(); |
| 107 | self.refs = RefSlots::new(); |
| 108 | self.grained = RefSlots::new(); |
| 109 | self.ready.clear(); |
| 110 | } |
| 111 | |
| 112 | /// What the stream turned out to be. |
| 113 | pub fn stream_info(&self) -> Option<StreamInfo> { |
| 114 | self.session.as_ref().map(Session::info) |
| 115 | } |
| 116 | |
| 117 | fn show_existing(&mut self, header: &FrameHeader, timestamp: i64) -> Result<()> { |
| 118 | let slot = usize::from(header.frame_to_show_map_idx); |
| 119 | let (Some(surface), Some(session)) = (self.refs.get(slot).cloned(), self.session.as_ref()) |
| 120 | else { |
| 121 | return Err(Error::Stream(ec_core::Error::corrupt( |
| 122 | "AV1 show_existing_frame names an empty reference slot", |
| 123 | ))); |
| 124 | }; |
| 125 | // The grained copy when the shown frame carries grain, else the |
| 126 | // reconstruction itself. |
| 127 | let out = self |
| 128 | .grained |
| 129 | .get(slot) |
| 130 | .cloned() |
| 131 | .unwrap_or_else(|| Rc::clone(&surface)); |
| 132 | self.ready.push(Frame::new( |
| 133 | out, |
| 134 | Rc::clone(&self.display), |
| 135 | timestamp, |
| 136 | (header.render_width, header.render_height), |
| 137 | session.coded_size, |
| 138 | session.bit_depth, |
| 139 | None, |
| 140 | )); |
| 141 | // 7.21: showing a KEY frame again refreshes every reference slot with |
| 142 | // it, which the reference *surfaces* have to follow too. |
| 143 | if header.frame_type == FrameType::Key { |
| 144 | let grained = self.grained.get(slot).cloned(); |
| 145 | self.refs.refresh(u8::MAX, Some(Rc::clone(&surface))); |
| 146 | self.grained.refresh(u8::MAX, grained); |
| 147 | } |
| 148 | Ok(()) |
| 149 | } |
| 150 | |
| 151 | fn decode_frame( |
| 152 | &mut self, |
| 153 | header: &FrameHeader, |
| 154 | tiles: &[Tile], |
| 155 | data: &[u8], |
| 156 | timestamp: i64, |
| 157 | ) -> Result<()> { |
| 158 | let sequence = self |
| 159 | .parser |
| 160 | .sequence_header() |
| 161 | .cloned() |
| 162 | .ok_or_else(|| Error::config("AV1 frame before any sequence header"))?; |
| 163 | self.ensure_session(&sequence, header)?; |
| 164 | |
| 165 | // An inter frame predicts from the seven reference slots it names, and |
| 166 | // the spec requires each of them to hold a decoded frame (RefValid). |
| 167 | // A slot never filled, from a cut stream or a GOP that opens on an |
| 168 | // inter frame, has no picture for the driver to read, so refuse here |
| 169 | // rather than hand it VA_INVALID_SURFACE and output what it makes of it. |
| 170 | if matches!(header.frame_type, FrameType::Inter | FrameType::Switch) { |
| 171 | for (ref_index, &slot) in header.ref_frame_idx.iter().enumerate() { |
| 172 | if self.refs.get(usize::from(slot)).is_none() { |
| 173 | return Err(Error::Stream(ec_core::Error::corrupt(format!( |
| 174 | "AV1 inter frame names reference slot {slot} (ref {ref_index}), \ |
| 175 | which no decoded frame has filled" |
| 176 | )))); |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | let session = self |
| 182 | .session |
| 183 | .as_ref() |
| 184 | .ok_or_else(|| Error::config("AV1 frame before any session"))?; |
| 185 | let surface = session.acquire()?; |
| 186 | |
| 187 | // Film grain (spec 7.18.3) is applied by the driver onto a separate |
| 188 | // display surface, so the reconstruction the references point at stays |
| 189 | // grain-free and the grained copy is what gets shown. |
| 190 | let grained = if wants_grain(&sequence, header) { |
| 191 | Some(session.acquire()?) |
| 192 | } else { |
| 193 | None |
| 194 | }; |
| 195 | let display_id = grained.as_ref().map_or(surface.id(), |g| g.id()); |
| 196 | |
| 197 | let pic_param = picture_parameters( |
| 198 | &sequence, |
| 199 | header, |
| 200 | &slot_ids(self.refs.as_array()), |
| 201 | surface.id(), |
| 202 | display_id, |
| 203 | ); |
| 204 | let mut buffers = Vec::with_capacity(3); |
| 205 | buffers.push(picture::buffer(&session.context, pic_param.to_buffer())?); |
| 206 | if !tiles.is_empty() { |
| 207 | let slices: Vec<SliceParameterBufferAV1> = tiles.iter().map(tile_parameters).collect(); |
| 208 | buffers.push(picture::buffer(&session.context, slice_buffer(&slices))?); |
| 209 | } |
| 210 | buffers.push(picture::buffer( |
| 211 | &session.context, |
| 212 | BufferType::SliceData(data.to_vec()), |
| 213 | )?); |
| 214 | session.submit(&surface, buffers)?; |
| 215 | |
| 216 | self.refs |
| 217 | .refresh(header.refresh_frame_flags, Some(Rc::clone(&surface))); |
| 218 | self.grained |
| 219 | .refresh(header.refresh_frame_flags, grained.clone()); |
| 220 | |
| 221 | if header.show_frame { |
| 222 | let out = grained.unwrap_or_else(|| Rc::clone(&surface)); |
| 223 | self.ready.push(Frame::new( |
| 224 | out, |
| 225 | Rc::clone(&self.display), |
| 226 | timestamp, |
| 227 | (header.render_width, header.render_height), |
| 228 | session.coded_size, |
| 229 | session.bit_depth, |
| 230 | None, |
| 231 | )); |
| 232 | } |
| 233 | Ok(()) |
| 234 | } |
| 235 | |
| 236 | fn ensure_session(&mut self, seq: &SequenceHeader, header: &FrameHeader) -> Result<()> { |
| 237 | let bit_depth = seq.color_config.bit_depth; |
| 238 | let color = &seq.color_config; |
| 239 | if color.mono_chrome |
| 240 | || bit_depth == 12 |
| 241 | || color.subsampling_x != 1 |
| 242 | || color.subsampling_y != 1 |
| 243 | { |
| 244 | return Err(Error::unsupported( |
| 245 | "AV1 hardware decode", |
| 246 | "only 4:2:0 8- and 10-bit", |
| 247 | )); |
| 248 | } |
| 249 | let profile = match (seq.seq_profile, bit_depth) { |
| 250 | (0, _) => Profile::AV1Profile0, |
| 251 | (2, _) => Profile::AV1Profile2, |
| 252 | (other, _) => { |
| 253 | return Err(Error::unsupported( |
| 254 | format!("AV1 profile {other}"), |
| 255 | "only the 4:2:0 profiles 0 and 2 are decoded in hardware", |
| 256 | )); |
| 257 | } |
| 258 | }; |
| 259 | // Superblocks are 64x64 or 128x128; a surface holds whole ones. |
| 260 | let sb = if seq.use_128x128_superblock { 128 } else { 64 }; |
| 261 | let coded = ( |
| 262 | header.upscaled_width.next_multiple_of(sb).max(sb), |
| 263 | header.frame_height.next_multiple_of(sb).max(sb), |
| 264 | ); |
| 265 | if let Some(session) = &self.session |
| 266 | && session.coded_size.0 >= coded.0 |
| 267 | && session.coded_size.1 >= coded.1 |
| 268 | && session.bit_depth == bit_depth |
| 269 | && session.profile == profile |
| 270 | { |
| 271 | return Ok(()); |
| 272 | } |
| 273 | self.refs = RefSlots::new(); |
| 274 | self.grained = RefSlots::new(); |
| 275 | let grain_surfaces = if seq.film_grain_params_present { |
| 276 | GRAIN_SURFACES |
| 277 | } else { |
| 278 | 0 |
| 279 | }; |
| 280 | self.session = Some(Session::new( |
| 281 | &self.display, |
| 282 | profile, |
| 283 | coded, |
| 284 | (header.render_width, header.render_height), |
| 285 | bit_depth, |
| 286 | NUM_REF_FRAMES + EXTRA_SURFACES + grain_surfaces, |
| 287 | )?); |
| 288 | Ok(()) |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | fn picture_parameters( |
| 293 | seq: &SequenceHeader, |
| 294 | h: &FrameHeader, |
| 295 | ref_ids: &[Option<u32>; NUM_REF_FRAMES], |
| 296 | current: u32, |
| 297 | display: u32, |
| 298 | ) -> PictureParameterBufferAV1 { |
| 299 | let sb_shift = if seq.use_128x128_superblock { 5 } else { 4 }; |
| 300 | let mut p = PictureParameterBufferAV1 { |
| 301 | profile: seq.seq_profile, |
| 302 | order_hint_bits_minus_1: seq.order_hint_bits.saturating_sub(1) as u8, |
| 303 | bit_depth_idx: match seq.color_config.bit_depth { |
| 304 | 8 => 0, |
| 305 | 10 => 1, |
| 306 | _ => 2, |
| 307 | }, |
| 308 | matrix_coefficients: seq.color_config.matrix_coefficients, |
| 309 | current_frame: current, |
| 310 | current_display_picture: display, |
| 311 | frame_width_minus1: (h.upscaled_width.max(1) - 1) as u16, |
| 312 | frame_height_minus1: (h.frame_height.max(1) - 1) as u16, |
| 313 | primary_ref_frame: h.primary_ref_frame, |
| 314 | order_hint: h.order_hint as u8, |
| 315 | tile_cols: h.tile_info.cols.min(255) as u8, |
| 316 | tile_rows: h.tile_info.rows.min(255) as u8, |
| 317 | tile_count_minus_1: (h.tile_info.cols * h.tile_info.rows).max(1) as u16 - 1, |
| 318 | context_update_tile_id: h.tile_info.context_update_tile_id as u16, |
| 319 | superres_scale_denominator: h.superres_denom, |
| 320 | interp_filter: h.interpolation_filter as u8, |
| 321 | filter_level: [h.loop_filter.level[0], h.loop_filter.level[1]], |
| 322 | filter_level_u: h.loop_filter.level[2], |
| 323 | filter_level_v: h.loop_filter.level[3], |
| 324 | ref_deltas: h.loop_filter.ref_deltas, |
| 325 | mode_deltas: h.loop_filter.mode_deltas, |
| 326 | base_qindex: h.quantization.base_q_idx, |
| 327 | y_dc_delta_q: h.quantization.delta_q_y_dc, |
| 328 | u_dc_delta_q: h.quantization.delta_q_u_dc, |
| 329 | u_ac_delta_q: h.quantization.delta_q_u_ac, |
| 330 | v_dc_delta_q: h.quantization.delta_q_v_dc, |
| 331 | v_ac_delta_q: h.quantization.delta_q_v_ac, |
| 332 | cdef_damping_minus_3: h.cdef.damping.saturating_sub(3), |
| 333 | cdef_bits: h.cdef.bits, |
| 334 | cdef_y_strengths: h.cdef.y_strengths(), |
| 335 | cdef_uv_strengths: h.cdef.uv_strengths(), |
| 336 | ..PictureParameterBufferAV1::default() |
| 337 | }; |
| 338 | for (slot, id) in ref_ids.iter().enumerate() { |
| 339 | if let Some(id) = id { |
| 340 | p.ref_frame_map[slot] = *id; |
| 341 | } |
| 342 | } |
| 343 | for i in 0..REFS_PER_FRAME { |
| 344 | p.ref_frame_idx[i] = h.ref_frame_idx[i]; |
| 345 | } |
| 346 | for (i, sbs) in h |
| 347 | .tile_info |
| 348 | .width_in_sbs_minus_1(sb_shift) |
| 349 | .into_iter() |
| 350 | .take(p.width_in_sbs_minus_1.len()) |
| 351 | .enumerate() |
| 352 | { |
| 353 | p.width_in_sbs_minus_1[i] = sbs; |
| 354 | } |
| 355 | for (i, sbs) in h |
| 356 | .tile_info |
| 357 | .height_in_sbs_minus_1(sb_shift) |
| 358 | .into_iter() |
| 359 | .take(p.height_in_sbs_minus_1.len()) |
| 360 | .enumerate() |
| 361 | { |
| 362 | p.height_in_sbs_minus_1[i] = sbs; |
| 363 | } |
| 364 | p.seq_info_fields = SeqInfoFields { |
| 365 | still_picture: u32::from(seq.still_picture), |
| 366 | use_128x128_superblock: u32::from(seq.use_128x128_superblock), |
| 367 | enable_filter_intra: u32::from(seq.enable_filter_intra), |
| 368 | enable_intra_edge_filter: u32::from(seq.enable_intra_edge_filter), |
| 369 | enable_interintra_compound: u32::from(seq.enable_interintra_compound), |
| 370 | enable_masked_compound: u32::from(seq.enable_masked_compound), |
| 371 | enable_dual_filter: u32::from(seq.enable_dual_filter), |
| 372 | enable_order_hint: u32::from(seq.enable_order_hint), |
| 373 | enable_jnt_comp: u32::from(seq.enable_jnt_comp), |
| 374 | enable_cdef: u32::from(seq.enable_cdef), |
| 375 | mono_chrome: u32::from(seq.color_config.mono_chrome), |
| 376 | color_range: u32::from(seq.color_config.color_range), |
| 377 | subsampling_x: u32::from(seq.color_config.subsampling_x), |
| 378 | subsampling_y: u32::from(seq.color_config.subsampling_y), |
| 379 | chroma_sample_position: 0, |
| 380 | film_grain_params_present: u32::from(seq.film_grain_params_present), |
| 381 | }; |
| 382 | p.pic_info_fields = PicInfoFields { |
| 383 | frame_type: h.frame_type as u32, |
| 384 | show_frame: u32::from(h.show_frame), |
| 385 | showable_frame: u32::from(h.showable_frame), |
| 386 | error_resilient_mode: u32::from(h.error_resilient_mode), |
| 387 | disable_cdf_update: u32::from(h.disable_cdf_update), |
| 388 | allow_screen_content_tools: u32::from(h.allow_screen_content_tools), |
| 389 | force_integer_mv: u32::from(h.force_integer_mv), |
| 390 | allow_intrabc: u32::from(h.allow_intrabc), |
| 391 | use_superres: u32::from(h.use_superres), |
| 392 | allow_high_precision_mv: u32::from(h.allow_high_precision_mv), |
| 393 | is_motion_mode_switchable: u32::from(h.is_motion_mode_switchable), |
| 394 | use_ref_frame_mvs: u32::from(h.use_ref_frame_mvs), |
| 395 | disable_frame_end_update_cdf: u32::from(h.disable_frame_end_update_cdf), |
| 396 | uniform_tile_spacing_flag: u32::from(h.tile_info.uniform_spacing), |
| 397 | allow_warped_motion: u32::from(h.allow_warped_motion), |
| 398 | large_scale_tile: 0, |
| 399 | }; |
| 400 | p.loop_filter_info_fields = LoopFilterInfoFields { |
| 401 | sharpness_level: h.loop_filter.sharpness, |
| 402 | mode_ref_delta_enabled: u8::from(h.loop_filter.delta_enabled), |
| 403 | mode_ref_delta_update: u8::from(h.loop_filter.delta_update), |
| 404 | }; |
| 405 | p.qmatrix_fields = QMatrixFields { |
| 406 | using_qmatrix: u16::from(h.quantization.using_qmatrix), |
| 407 | qm_y: u16::from(h.quantization.qm_y), |
| 408 | qm_u: u16::from(h.quantization.qm_u), |
| 409 | qm_v: u16::from(h.quantization.qm_v), |
| 410 | }; |
| 411 | p.mode_control_fields = ModeControlFields { |
| 412 | delta_q_present_flag: u32::from(h.delta.q_present), |
| 413 | log2_delta_q_res: u32::from(h.delta.q_res), |
| 414 | delta_lf_present_flag: u32::from(h.delta.lf_present), |
| 415 | log2_delta_lf_res: u32::from(h.delta.lf_res), |
| 416 | delta_lf_multi: u32::from(h.delta.lf_multi), |
| 417 | tx_mode: h.tx_mode as u32, |
| 418 | reference_select: u32::from(h.reference_select), |
| 419 | reduced_tx_set_used: u32::from(h.reduced_tx_set), |
| 420 | skip_mode_present: u32::from(h.skip_mode_present), |
| 421 | }; |
| 422 | p.loop_restoration_fields = LoopRestorationFields { |
| 423 | yframe_restoration_type: h.loop_restoration.frame_restoration_type[0] as u16, |
| 424 | cbframe_restoration_type: h.loop_restoration.frame_restoration_type[1] as u16, |
| 425 | crframe_restoration_type: h.loop_restoration.frame_restoration_type[2] as u16, |
| 426 | lr_unit_shift: u16::from(h.loop_restoration.lr_unit_shift), |
| 427 | lr_uv_shift: u16::from(h.loop_restoration.lr_uv_shift), |
| 428 | }; |
| 429 | p.seg_info.segment_info_fields = SegmentInfoFields { |
| 430 | enabled: u32::from(h.segmentation.enabled), |
| 431 | update_map: u32::from(h.segmentation.update_map), |
| 432 | temporal_update: u32::from(h.segmentation.temporal_update), |
| 433 | update_data: u32::from(h.segmentation.update_data), |
| 434 | }; |
| 435 | for segment in 0..ec_av1_syntax::MAX_SEGMENTS { |
| 436 | p.seg_info.feature_data[segment] = h.segmentation.feature_data[segment]; |
| 437 | p.seg_info.feature_mask[segment] = h.segmentation.feature_mask(segment); |
| 438 | } |
| 439 | for i in 0..REFS_PER_FRAME { |
| 440 | let warp = &h.global_motion[i]; |
| 441 | let mut wm = WarpedMotionParamsAV1 { |
| 442 | wmtype: warp.model as u32, |
| 443 | invalid: u8::from(warp.invalid), |
| 444 | ..WarpedMotionParamsAV1::default() |
| 445 | }; |
| 446 | wm.wmmat[..6].copy_from_slice(&warp.params); |
| 447 | p.wm[i] = wm; |
| 448 | } |
| 449 | if wants_grain(seq, h) { |
| 450 | p.film_grain_info = film_grain_struct(&h.film_grain); |
| 451 | } |
| 452 | p |
| 453 | } |
| 454 | |
| 455 | /// The surface id in each reference slot, `None` for an empty one. |
| 456 | fn slot_ids(refs: &[Option<Rc<PooledSurface>>; NUM_REF_FRAMES]) -> [Option<u32>; NUM_REF_FRAMES] { |
| 457 | refs.each_ref().map(|s| s.as_ref().map(|s| s.id())) |
| 458 | } |
| 459 | |
| 460 | /// The slice parameters of one tile. |
| 461 | fn tile_parameters(tile: &Tile) -> SliceParameterBufferAV1 { |
| 462 | SliceParameterBufferAV1 { |
| 463 | slice_data_size: tile.size as u32, |
| 464 | slice_data_offset: tile.offset as u32, |
| 465 | slice_data_flag: 0, // VA_SLICE_DATA_FLAG_ALL |
| 466 | tile_row: tile.row as u16, |
| 467 | tile_column: tile.column as u16, |
| 468 | ..SliceParameterBufferAV1::default() |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | /// Feeds a run of AV1 temporal units (see `super::units`) through the |
| 473 | /// parameter builders, with the sequence, frame and tile OBUs the real parser |
| 474 | /// produces. One parser runs across the units, as the decoder's does, so a frame |
| 475 | /// sees the sequence header and reference state the earlier units left. No GPU. |
| 476 | /// Returns the number of frames built. |
| 477 | #[doc(hidden)] |
| 478 | pub fn fuzz_build_parameters(data: &[u8]) -> usize { |
| 479 | let mut parser = Av1Parser::new(); |
| 480 | let mut built = 0usize; |
| 481 | for unit in super::units(data) { |
| 482 | let Ok(obus) = parser.parse_temporal_unit(unit) else { |
| 483 | continue; |
| 484 | }; |
| 485 | let Some(seq) = parser.sequence_header().cloned() else { |
| 486 | continue; |
| 487 | }; |
| 488 | let mut pending: Option<Box<FrameHeader>> = None; |
| 489 | for obu in obus { |
| 490 | let (header, tiles) = match obu.kind { |
| 491 | ObuKind::FrameHeader(header) => { |
| 492 | pending = Some(header); |
| 493 | continue; |
| 494 | } |
| 495 | ObuKind::TileGroup(tiles) => match pending.take() { |
| 496 | Some(header) => (header, tiles), |
| 497 | None => continue, |
| 498 | }, |
| 499 | ObuKind::Frame(header, tiles) => (header, tiles), |
| 500 | _ => continue, |
| 501 | }; |
| 502 | if header.show_existing_frame { |
| 503 | continue; |
| 504 | } |
| 505 | let mut ids = [None; NUM_REF_FRAMES]; |
| 506 | for (slot, id) in ids.iter_mut().enumerate() { |
| 507 | if !(slot + data.len()).is_multiple_of(3) { |
| 508 | *id = Some(0x100 + slot as u32); |
| 509 | } |
| 510 | } |
| 511 | let pic = picture_parameters(&seq, &header, &ids, 0x200, 0x201); |
| 512 | let slices: Vec<SliceParameterBufferAV1> = tiles.iter().map(tile_parameters).collect(); |
| 513 | std::hint::black_box((pic.to_buffer(), slice_buffer(&slices))); |
| 514 | built += 1; |
| 515 | } |
| 516 | } |
| 517 | built |
| 518 | } |
| 519 | |
| 520 | /// Whether this frame's grained picture is produced: the stream carries film |
| 521 | /// grain, and the frame is shown now or later (`showable_frame`, shown through |
| 522 | /// `show_existing_frame`) with `apply_grain` set. Spec 5.9.30 reads the grain |
| 523 | /// parameters under the same condition. |
| 524 | fn wants_grain(seq: &SequenceHeader, h: &FrameHeader) -> bool { |
| 525 | seq.film_grain_params_present && (h.show_frame || h.showable_frame) && h.film_grain.apply_grain |
| 526 | } |
| 527 | |
| 528 | /// `VAFilmGrainStructAV1` from the frame header's `film_grain_params`. |
| 529 | fn film_grain_struct(fg: &FilmGrainParams) -> FilmGrainStructAV1 { |
| 530 | FilmGrainStructAV1 { |
| 531 | film_grain_info_fields: FilmGrainInfoFields { |
| 532 | apply_grain: 1, |
| 533 | chroma_scaling_from_luma: u32::from(fg.chroma_scaling_from_luma), |
| 534 | grain_scaling_minus_8: u32::from(fg.grain_scaling_minus_8), |
| 535 | ar_coeff_lag: u32::from(fg.ar_coeff_lag), |
| 536 | ar_coeff_shift_minus_6: u32::from(fg.ar_coeff_shift_minus_6), |
| 537 | grain_scale_shift: u32::from(fg.grain_scale_shift), |
| 538 | overlap_flag: u32::from(fg.overlap_flag), |
| 539 | clip_to_restricted_range: u32::from(fg.clip_to_restricted_range), |
| 540 | }, |
| 541 | grain_seed: fg.grain_seed, |
| 542 | num_y_points: fg.num_y_points, |
| 543 | point_y_value: fg.point_y_value, |
| 544 | point_y_scaling: fg.point_y_scaling, |
| 545 | num_cb_points: fg.num_cb_points, |
| 546 | point_cb_value: fg.point_cb_value, |
| 547 | point_cb_scaling: fg.point_cb_scaling, |
| 548 | num_cr_points: fg.num_cr_points, |
| 549 | point_cr_value: fg.point_cr_value, |
| 550 | point_cr_scaling: fg.point_cr_scaling, |
| 551 | ar_coeffs_y: fg.ar_coeffs_y, |
| 552 | ar_coeffs_cb: fg.ar_coeffs_cb, |
| 553 | ar_coeffs_cr: fg.ar_coeffs_cr, |
| 554 | cb_mult: fg.cb_mult, |
| 555 | cb_luma_mult: fg.cb_luma_mult, |
| 556 | cb_offset: fg.cb_offset, |
| 557 | cr_mult: fg.cr_mult, |
| 558 | cr_luma_mult: fg.cr_luma_mult, |
| 559 | cr_offset: fg.cr_offset, |
| 560 | } |
| 561 | } |