wip(seg): session restart -- uncommitted builder state preserved by orchestrator
Browse files at this commit32f07e4a143750497a8ae834a4da18117e10d30cParent: d21774e5 changed files +739 -21
- crates/ec-av1/src/cdf.rs +13 -0
- crates/ec-av1/src/cdf_state.rs +8 -0
- crates/ec-av1/src/decode.rs +408 -15
- crates/ec-av1/src/refusal_inventory.rs +1 -1
- crates/ec-av1/src/stream.rs +309 -5
crates/ec-av1/src/cdf.rsmodified+13 -0
| @@ -561,6 +561,19 @@ pub const RESTORE_SWITCHABLE: [u16; 4] = [9413, 22581, 32768, 0]; | |||
| 561 | /// 4-symbol `delta_q_abs` alphabet read once per superblock when | 561 | /// 4-symbol `delta_q_abs` alphabet read once per superblock when |
| 562 | /// `delta_q_present` is set. `default_delta_lf_cdf`/`default_delta_lf_multi_cdf` | 562 | /// `delta_q_present` is set. `default_delta_lf_cdf`/`default_delta_lf_multi_cdf` |
| 563 | /// share this same value (verified against libaom's real source). | 563 | /// share this same value (verified against libaom's real source). |
| 564 | +/// `Default_Segment_Id_Cdf` (spec 9.4, libaom | ||
| 565 | +/// `default_spatial_pred_seg_tree_cdf`): the `segment_id` symbol's eight | ||
| 566 | +/// segments, indexed by the spec 5.11.8 neighbour-agreement context. | ||
| 567 | +pub const SEGMENT_ID: [[u16; 9]; 3] = [ | ||
| 568 | + [5622, 7893, 16093, 18233, 27809, 28373, 32533, 32768, 0], | ||
| 569 | + [14274, 18230, 22557, 24935, 29980, 30851, 32344, 32768, 0], | ||
| 570 | + [27527, 28487, 28723, 28890, 32397, 32647, 32679, 32768, 0], | ||
| 571 | +]; | ||
| 572 | + | ||
| 573 | +/// `Default_Segment_Pred_Cdf` (spec 9.4, libaom `default_segment_pred_cdf`): | ||
| 574 | +/// `seg_id_predicted`, indexed by `AboveSegPredContext + LeftSegPredContext`. | ||
| 575 | +pub const SEGMENT_PRED: [[u16; 3]; 3] = [[16384, 32768, 0]; 3]; | ||
| 576 | + | ||
| 564 | pub const DELTA_Q: [u16; 5] = [28160, 32120, 32677, 32768, 0]; | 577 | pub const DELTA_Q: [u16; 5] = [28160, 32120, 32677, 32768, 0]; |
| 565 | /// `default_delta_lf_cdf` -- see [`DELTA_Q`]'s doc. | 578 | /// `default_delta_lf_cdf` -- see [`DELTA_Q`]'s doc. |
| 566 | pub const DELTA_LF: [u16; 5] = DELTA_Q; | 579 | pub const DELTA_LF: [u16; 5] = DELTA_Q; |
crates/ec-av1/src/cdf_state.rsmodified+8 -0
| @@ -464,6 +464,10 @@ pub(crate) struct Cdfs { | |||
| 464 | /// A `RESTORE_SWITCHABLE`-plane LR unit's `restoration_type` -- see | 464 | /// A `RESTORE_SWITCHABLE`-plane LR unit's `restoration_type` -- see |
| 465 | /// [`cdf::RESTORE_SWITCHABLE`]. | 465 | /// [`cdf::RESTORE_SWITCHABLE`]. |
| 466 | pub restore_switchable: [u16; 4], | 466 | pub restore_switchable: [u16; 4], |
| 467 | + /// `segment_id` (lane-seg, spec 5.11.8) -- see [`cdf::SEGMENT_ID`]. | ||
| 468 | + pub segment_id: [[u16; 9]; 3], | ||
| 469 | + /// `seg_id_predicted` (lane-seg, spec 5.11.9) -- see [`cdf::SEGMENT_PRED`]. | ||
| 470 | + pub segment_pred: [[u16; 3]; 3], | ||
| 467 | /// `delta_q_abs` (lane-realworld r4, spec 5.11.10) -- see [`cdf::DELTA_Q`]. | 471 | /// `delta_q_abs` (lane-realworld r4, spec 5.11.10) -- see [`cdf::DELTA_Q`]. |
| 468 | pub delta_q: [u16; 5], | 472 | pub delta_q: [u16; 5], |
| 469 | /// `delta_lf_abs`, the single-value form (spec 5.11.11) -- see | 473 | /// `delta_lf_abs`, the single-value form (spec 5.11.11) -- see |
| @@ -701,6 +705,8 @@ impl Cdfs { | |||
| 701 | reset1(&mut self.restore_wiener); | 705 | reset1(&mut self.restore_wiener); |
| 702 | reset1(&mut self.restore_sgrproj); | 706 | reset1(&mut self.restore_sgrproj); |
| 703 | reset1(&mut self.restore_switchable); | 707 | reset1(&mut self.restore_switchable); |
| 708 | + reset2(&mut self.segment_id); | ||
| 709 | + reset2(&mut self.segment_pred); | ||
| 704 | reset1(&mut self.delta_q); | 710 | reset1(&mut self.delta_q); |
| 705 | reset1(&mut self.delta_lf); | 711 | reset1(&mut self.delta_lf); |
| 706 | reset2(&mut self.delta_lf_multi); | 712 | reset2(&mut self.delta_lf_multi); |
| @@ -1172,6 +1178,8 @@ impl Cdfs { | |||
| 1172 | restore_wiener: cdf::RESTORE_WIENER, | 1178 | restore_wiener: cdf::RESTORE_WIENER, |
| 1173 | restore_sgrproj: cdf::RESTORE_SGRPROJ, | 1179 | restore_sgrproj: cdf::RESTORE_SGRPROJ, |
| 1174 | restore_switchable: cdf::RESTORE_SWITCHABLE, | 1180 | restore_switchable: cdf::RESTORE_SWITCHABLE, |
| 1181 | + segment_id: cdf::SEGMENT_ID, | ||
| 1182 | + segment_pred: cdf::SEGMENT_PRED, | ||
| 1175 | delta_q: cdf::DELTA_Q, | 1183 | delta_q: cdf::DELTA_Q, |
| 1176 | delta_lf: cdf::DELTA_LF, | 1184 | delta_lf: cdf::DELTA_LF, |
| 1177 | delta_lf_multi: [cdf::DELTA_LF; 4], | 1185 | delta_lf_multi: [cdf::DELTA_LF; 4], |
crates/ec-av1/src/decode.rsmodified+408 -15
| @@ -22,7 +22,8 @@ | |||
| 22 | //! silently miscoding. | 22 | //! silently miscoding. |
| 23 | 23 | ||
| 24 | use ec_av1_syntax::{ | 24 | use ec_av1_syntax::{ |
| 25 | - CdefParams, DeltaParams, LoopFilterParams, LoopRestorationParams, TileInfo, | 25 | + CdefParams, DeltaParams, LoopFilterParams, LoopRestorationParams, |
| 26 | + SEG_LVL_ALT_LF_Y_V, SEG_LVL_ALT_Q, SegmentationParams, TileInfo, | ||
| 26 | }; | 27 | }; |
| 27 | use ec_core::{Error, Result}; | 28 | use ec_core::{Error, Result}; |
| 28 | 29 | ||
| @@ -315,6 +316,344 @@ fn maybe_read_cdef_idx(dec: &mut SymbolDecoder, mi_r: usize, mi_c: usize, skip: | |||
| 315 | } | 316 | } |
| 316 | } | 317 | } |
| 317 | 318 | ||
| 319 | +// --------------------------------------------------------------------------- | ||
| 320 | +// lane-seg: segmentation. Header (spec 5.9.14) is parsed by `ec-av1-syntax`; | ||
| 321 | +// this block is the per-block `segment_id` reader (spec 5.11.7-5.11.9) plus | ||
| 322 | +// the two consumers a real stream exercises: the per-segment quantizer index | ||
| 323 | +// (spec 7.12.2 `get_qindex`) and the per-segment loop-filter level | ||
| 324 | +// (spec 7.14.4). Shapes follow libaom `decodemv.c:read_segment_id`/ | ||
| 325 | +// `read_intra_segment_id`/`read_inter_segment_id` and | ||
| 326 | +// `pred_common.h:av1_get_spatial_seg_pred` -- note libaom returns the SPATIAL | ||
| 327 | +// PREDICTION (not 0) from `read_segment_id` when the block is `skip`, which | ||
| 328 | +// is what the oracle every gate here compares against actually does. | ||
| 329 | +thread_local! { | ||
| 330 | + static SEG: std::cell::RefCell<SegmentationParams> = | ||
| 331 | + std::cell::RefCell::new(SegmentationParams::default()); | ||
| 332 | + /// `CurrentSegmentIds`: this frame's own map, `mi_rows * mi_cols` entries | ||
| 333 | + /// with stride `mi_cols` (NOT the padded `Neighbours` grid stride). | ||
| 334 | + static SEG_IDS: std::cell::RefCell<Vec<u8>> = const { std::cell::RefCell::new(Vec::new()) }; | ||
| 335 | + /// `PrevSegmentIds`: the primary reference frame's saved map, empty when | ||
| 336 | + /// this frame has no primary reference (spec `load_previous_segment_ids`). | ||
| 337 | + static PREV_SEG_IDS: std::cell::RefCell<Vec<u8>> = const { std::cell::RefCell::new(Vec::new()) }; | ||
| 338 | + /// `(mi_rows, mi_cols)` for both maps above. | ||
| 339 | + static SEG_MI_DIMS: std::cell::Cell<(usize, usize)> = const { std::cell::Cell::new((0, 0)) }; | ||
| 340 | + /// `AboveSegPredContext` (per mi column) / `LeftSegPredContext` (per mi | ||
| 341 | + /// row) -- spec 5.11.9's own `seg_id_predicted` neighbour state, which | ||
| 342 | + /// libaom keeps on the neighbouring `mbmi->seg_id_predicted` instead. | ||
| 343 | + static ABOVE_SEG_PRED: std::cell::RefCell<Vec<u8>> = const { std::cell::RefCell::new(Vec::new()) }; | ||
| 344 | + static LEFT_SEG_PRED: std::cell::RefCell<Vec<u8>> = const { std::cell::RefCell::new(Vec::new()) }; | ||
| 345 | + /// The block currently being decoded -- read back by [`block_q_idx`]. | ||
| 346 | + static CUR_SEGMENT_ID: std::cell::Cell<u8> = const { std::cell::Cell::new(0) }; | ||
| 347 | + /// The tile's own top-left mi position, for `AvailU`/`AvailL`. | ||
| 348 | + static SEG_TILE_ORIGIN: std::cell::Cell<(usize, usize)> = const { std::cell::Cell::new((0, 0)) }; | ||
| 349 | + /// How many real `segment_id` symbols were read, and the set of segment | ||
| 350 | + /// ids any block ended up with -- the gate's proof the feature fired. | ||
| 351 | + static SEG_ID_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; | ||
| 352 | + static SEG_IDS_SEEN: std::cell::Cell<u8> = const { std::cell::Cell::new(0) }; | ||
| 353 | +} | ||
| 354 | + | ||
| 355 | +/// Installs this frame's segmentation parameters and allocates its map, with | ||
| 356 | +/// `prev` the primary reference frame's saved map (spec | ||
| 357 | +/// `load_previous_segment_ids`; `None` for `PRIMARY_REF_NONE` or an empty | ||
| 358 | +/// slot). Called once per frame, whether or not segmentation is enabled. | ||
| 359 | +pub(crate) fn set_segmentation( | ||
| 360 | + seg: SegmentationParams, | ||
| 361 | + mi_rows: usize, | ||
| 362 | + mi_cols: usize, | ||
| 363 | + prev: Option<&[u8]>, | ||
| 364 | +) { | ||
| 365 | + SEG.with(|s| *s.borrow_mut() = seg); | ||
| 366 | + SEG_MI_DIMS.with(|c| c.set((mi_rows, mi_cols))); | ||
| 367 | + SEG_IDS.with(|m| *m.borrow_mut() = vec![0u8; mi_rows * mi_cols]); | ||
| 368 | + PREV_SEG_IDS.with(|m| { | ||
| 369 | + let mut m = m.borrow_mut(); | ||
| 370 | + m.clear(); | ||
| 371 | + if let Some(p) = prev { | ||
| 372 | + if p.len() == mi_rows * mi_cols { | ||
| 373 | + m.extend_from_slice(p); | ||
| 374 | + } | ||
| 375 | + } | ||
| 376 | + }); | ||
| 377 | + ABOVE_SEG_PRED.with(|m| *m.borrow_mut() = vec![0u8; mi_cols]); | ||
| 378 | + LEFT_SEG_PRED.with(|m| *m.borrow_mut() = vec![0u8; mi_rows]); | ||
| 379 | + CUR_SEGMENT_ID.with(|c| c.set(0)); | ||
| 380 | +} | ||
| 381 | + | ||
| 382 | +/// This frame's decoded segment map, to be stored into whichever reference | ||
| 383 | +/// slots `refresh_frame_flags` names (spec 7.20's `SavedSegmentIds`). | ||
| 384 | +pub(crate) fn take_segment_ids() -> Vec<u8> { | ||
| 385 | + SEG_IDS.with(|m| m.borrow().clone()) | ||
| 386 | +} | ||
| 387 | + | ||
| 388 | +/// How many `segment_id` symbols have been read (never reset per frame). | ||
| 389 | +pub(crate) fn segment_id_hits() -> usize { | ||
| 390 | + SEG_ID_HITS.with(|c| c.get()) | ||
| 391 | +} | ||
| 392 | + | ||
| 393 | +/// How many distinct segment ids any decoded block has carried. | ||
| 394 | +pub(crate) fn segment_ids_seen() -> usize { | ||
| 395 | + SEG_IDS_SEEN.with(|c| c.get().count_ones() as usize) | ||
| 396 | +} | ||
| 397 | + | ||
| 398 | +/// Resets both segmentation hit counters (gate setup). | ||
| 399 | +pub(crate) fn reset_segment_hits() { | ||
| 400 | + SEG_ID_HITS.with(|c| c.set(0)); | ||
| 401 | + SEG_IDS_SEEN.with(|c| c.set(0)); | ||
| 402 | +} | ||
| 403 | + | ||
| 404 | +/// `seg_feature_active_idx(segment_id, feature)` for the frame currently | ||
| 405 | +/// being decoded, with its `FeatureData` value. | ||
| 406 | +fn seg_feature(segment_id: usize, feature: usize) -> Option<i32> { | ||
| 407 | + SEG.with(|s| { | ||
| 408 | + let s = s.borrow(); | ||
| 409 | + s.feature_active(segment_id, feature) | ||
| 410 | + .then(|| i32::from(s.feature_data[segment_id][feature])) | ||
| 411 | + }) | ||
| 412 | +} | ||
| 413 | + | ||
| 414 | +/// `get_qindex(ignoreDeltaQ = 0, segment_id)` (spec 7.12.2, libaom | ||
| 415 | +/// `av1_get_qindex`) for the block currently being decoded: the running | ||
| 416 | +/// `CurrentQIndex` shifted by this block's segment's `SEG_LVL_ALT_Q` data. | ||
| 417 | +/// Every dequant call site reads the block's quantizer through here. | ||
| 418 | +fn block_q_idx() -> i32 { | ||
| 419 | + let base = CURRENT_Q_IDX.with(|c| c.get()); | ||
| 420 | + let seg_id = CUR_SEGMENT_ID.with(|c| c.get()) as usize; | ||
| 421 | + match seg_feature(seg_id, SEG_LVL_ALT_Q) { | ||
| 422 | + Some(data) => (base + data).clamp(0, 255), | ||
| 423 | + None => base, | ||
| 424 | + } | ||
| 425 | +} | ||
| 426 | + | ||
| 427 | +/// The segment id recorded for one mi cell of the current frame's map. | ||
| 428 | +fn segment_id_at(mi_r: usize, mi_c: usize) -> u8 { | ||
| 429 | + let (mi_rows, mi_cols) = SEG_MI_DIMS.with(|c| c.get()); | ||
| 430 | + if mi_r >= mi_rows || mi_c >= mi_cols { | ||
| 431 | + return 0; | ||
| 432 | + } | ||
| 433 | + SEG_IDS.with(|m| m.borrow()[mi_r * mi_cols + mi_c]) | ||
| 434 | +} | ||
| 435 | + | ||
| 436 | +/// `set_segment_id`: stamps `id` over every mi cell this block covers. | ||
| 437 | +fn write_segment_id(mi_r: usize, mi_c: usize, w_mi: usize, h_mi: usize, id: u8) { | ||
| 438 | + let (mi_rows, mi_cols) = SEG_MI_DIMS.with(|c| c.get()); | ||
| 439 | + let (x_mis, y_mis) = (w_mi.min(mi_cols.saturating_sub(mi_c)), h_mi.min(mi_rows.saturating_sub(mi_r))); | ||
| 440 | + SEG_IDS.with(|m| { | ||
| 441 | + let mut m = m.borrow_mut(); | ||
| 442 | + for y in 0..y_mis { | ||
| 443 | + for x in 0..x_mis { | ||
| 444 | + m[(mi_r + y) * mi_cols + mi_c + x] = id; | ||
| 445 | + } | ||
| 446 | + } | ||
| 447 | + }); | ||
| 448 | + CUR_SEGMENT_ID.with(|c| c.set(id)); | ||
| 449 | + SEG_IDS_SEEN.with(|c| c.set(c.get() | 1 << id)); | ||
| 450 | +} | ||
| 451 | + | ||
| 452 | +/// `get_predicted_segment_id` (libaom `dec_get_segment_id` over | ||
| 453 | +/// `last_frame_seg_map`): the minimum previous-frame segment id over the | ||
| 454 | +/// block's own mi footprint. `0` when there is no previous map. | ||
| 455 | +fn predicted_segment_id(mi_r: usize, mi_c: usize, w_mi: usize, h_mi: usize) -> u8 { | ||
| 456 | + let (mi_rows, mi_cols) = SEG_MI_DIMS.with(|c| c.get()); | ||
| 457 | + PREV_SEG_IDS.with(|m| { | ||
| 458 | + let m = m.borrow(); | ||
| 459 | + if m.is_empty() { | ||
| 460 | + return 0; | ||
| 461 | + } | ||
| 462 | + let (x_mis, y_mis) = (w_mi.min(mi_cols.saturating_sub(mi_c)), h_mi.min(mi_rows.saturating_sub(mi_r))); | ||
| 463 | + let mut id = u8::MAX; | ||
| 464 | + for y in 0..y_mis { | ||
| 465 | + for x in 0..x_mis { | ||
| 466 | + id = id.min(m[(mi_r + y) * mi_cols + mi_c + x]); | ||
| 467 | + } | ||
| 468 | + } | ||
| 469 | + if id == u8::MAX { 0 } else { id } | ||
| 470 | + }) | ||
| 471 | +} | ||
| 472 | + | ||
| 473 | +/// `av1_neg_deinterleave` (libaom `decodemv.c:258`). | ||
| 474 | +fn neg_deinterleave(diff: i32, reference: i32, max: i32) -> i32 { | ||
| 475 | + if reference == 0 { | ||
| 476 | + return diff; | ||
| 477 | + } | ||
| 478 | + if reference >= max - 1 { | ||
| 479 | + return max - diff - 1; | ||
| 480 | + } | ||
| 481 | + let half = if 2 * reference < max { | ||
| 482 | + diff <= 2 * reference | ||
| 483 | + } else { | ||
| 484 | + diff <= 2 * (max - reference - 1) | ||
| 485 | + }; | ||
| 486 | + if half { | ||
| 487 | + return if diff & 1 != 0 { | ||
| 488 | + reference + ((diff + 1) >> 1) | ||
| 489 | + } else { | ||
| 490 | + reference - (diff >> 1) | ||
| 491 | + }; | ||
| 492 | + } | ||
| 493 | + if 2 * reference < max { diff } else { max - (diff + 1) } | ||
| 494 | +} | ||
| 495 | + | ||
| 496 | +/// `av1_get_spatial_seg_pred`: `(prediction, cdf_index)` from the current | ||
| 497 | +/// frame's already-decoded above/left/above-left neighbours. | ||
| 498 | +fn spatial_seg_pred(mi_r: usize, mi_c: usize) -> (i32, usize) { | ||
| 499 | + let (row0, col0) = SEG_TILE_ORIGIN.with(|c| c.get()); | ||
| 500 | + let (avail_u, avail_l) = (mi_r > row0, mi_c > col0); | ||
| 501 | + let prev_ul = (avail_u && avail_l).then(|| i32::from(segment_id_at(mi_r - 1, mi_c - 1))); | ||
| 502 | + let prev_u = avail_u.then(|| i32::from(segment_id_at(mi_r - 1, mi_c))); | ||
| 503 | + let prev_l = avail_l.then(|| i32::from(segment_id_at(mi_r, mi_c - 1))); | ||
| 504 | + let ctx = match prev_ul { | ||
| 505 | + None => 0, | ||
| 506 | + Some(ul) if Some(ul) == prev_u && Some(ul) == prev_l => 2, | ||
| 507 | + Some(ul) if Some(ul) == prev_u || Some(ul) == prev_l || prev_u == prev_l => 1, | ||
| 508 | + Some(_) => 0, | ||
| 509 | + }; | ||
| 510 | + let pred = match (prev_u, prev_l) { | ||
| 511 | + (None, l) => l.unwrap_or(0), | ||
| 512 | + (Some(u), None) => u, | ||
| 513 | + (Some(u), Some(l)) => { | ||
| 514 | + if prev_ul == Some(u) { | ||
| 515 | + u | ||
| 516 | + } else { | ||
| 517 | + l | ||
| 518 | + } | ||
| 519 | + } | ||
| 520 | + }; | ||
| 521 | + (pred, ctx) | ||
| 522 | +} | ||
| 523 | + | ||
| 524 | +/// `read_segment_id` (libaom `decodemv.c:280`): the spatially predicted id | ||
| 525 | +/// when `skip`, otherwise one `segment_id` symbol de-interleaved against | ||
| 526 | +/// that prediction. Does NOT write the map -- callers do. | ||
| 527 | +fn read_segment_id(dec: &mut SymbolDecoder, cdfs: &mut Cdfs, mi_r: usize, mi_c: usize, skip: bool) -> u8 { | ||
| 528 | + let (pred, ctx) = spatial_seg_pred(mi_r, mi_c); | ||
| 529 | + if skip { | ||
| 530 | + return pred as u8; | ||
| 531 | + } | ||
| 532 | + let coded = dec.symbol(&mut cdfs.segment_id[ctx]) as i32; | ||
| 533 | + SEG_ID_HITS.with(|c| c.set(c.get() + 1)); | ||
| 534 | + let last_active = SEG.with(|s| i32::from(s.borrow().last_active_seg_id)); | ||
| 535 | + neg_deinterleave(coded, pred, last_active + 1).clamp(0, last_active) as u8 | ||
| 536 | +} | ||
| 537 | + | ||
| 538 | +/// `read_intra_segment_id` (spec 5.11.7): called twice per intra-frame block | ||
| 539 | +/// -- once before `skip` when `SegIdPreSkip`, once after it otherwise (the | ||
| 540 | +/// caller decides which by passing `pre_skip`). | ||
| 541 | +fn intra_segment_id( | ||
| 542 | + dec: &mut SymbolDecoder, | ||
| 543 | + cdfs: &mut Cdfs, | ||
| 544 | + mi_r: usize, | ||
| 545 | + mi_c: usize, | ||
| 546 | + w_mi: usize, | ||
| 547 | + h_mi: usize, | ||
| 548 | + skip: bool, | ||
| 549 | +) { | ||
| 550 | + if !SEG.with(|s| s.borrow().enabled) { | ||
| 551 | + return; | ||
| 552 | + } | ||
| 553 | + let id = read_segment_id(dec, cdfs, mi_r, mi_c, skip); | ||
| 554 | + write_segment_id(mi_r, mi_c, w_mi, h_mi, id); | ||
| 555 | +} | ||
| 556 | + | ||
| 557 | +/// Whether this frame's intra blocks read their `segment_id` before `skip` | ||
| 558 | +/// (`SegIdPreSkip`); `false` also when segmentation is off. | ||
| 559 | +fn seg_id_pre_skip() -> bool { | ||
| 560 | + SEG.with(|s| { | ||
| 561 | + let s = s.borrow(); | ||
| 562 | + s.enabled && s.seg_id_pre_skip | ||
| 563 | + }) | ||
| 564 | +} | ||
| 565 | + | ||
| 566 | +/// `read_inter_segment_id(preskip)` (spec 5.11.9, libaom `decodemv.c:352`). | ||
| 567 | +/// Called at both spec positions of an inter frame's block: `pre_skip = true` | ||
| 568 | +/// before `skip_mode`/`skip`, and `pre_skip = false` after them. | ||
| 569 | +fn inter_segment_id( | ||
| 570 | + dec: &mut SymbolDecoder, | ||
| 571 | + cdfs: &mut Cdfs, | ||
| 572 | + mi_r: usize, | ||
| 573 | + mi_c: usize, | ||
| 574 | + w_mi: usize, | ||
| 575 | + h_mi: usize, | ||
| 576 | + skip: bool, | ||
| 577 | + pre_skip: bool, | ||
| 578 | +) { | ||
| 579 | + let (enabled, update_map, temporal_update, pre_skip_frame) = SEG.with(|s| { | ||
| 580 | + let s = s.borrow(); | ||
| 581 | + (s.enabled, s.update_map, s.temporal_update, s.seg_id_pre_skip) | ||
| 582 | + }); | ||
| 583 | + if !enabled { | ||
| 584 | + return; | ||
| 585 | + } | ||
| 586 | + if !update_map { | ||
| 587 | + // The map is inherited wholesale from the previous frame. | ||
| 588 | + let id = predicted_segment_id(mi_r, mi_c, w_mi, h_mi); | ||
| 589 | + write_segment_id(mi_r, mi_c, w_mi, h_mi, id); | ||
| 590 | + return; | ||
| 591 | + } | ||
| 592 | + if pre_skip && !pre_skip_frame { | ||
| 593 | + return; | ||
| 594 | + } | ||
| 595 | + if !pre_skip && skip { | ||
| 596 | + // libaom: a skipped block never codes `seg_id_predicted`, and its | ||
| 597 | + // segment id is the spatial prediction. | ||
| 598 | + stamp_seg_pred(mi_r, mi_c, w_mi, h_mi, 0); | ||
| 599 | + let id = read_segment_id(dec, cdfs, mi_r, mi_c, true); | ||
| 600 | + write_segment_id(mi_r, mi_c, w_mi, h_mi, id); | ||
| 601 | + return; | ||
| 602 | + } | ||
| 603 | + let id = if temporal_update { | ||
| 604 | + let ctx = seg_pred_ctx(mi_r, mi_c); | ||
| 605 | + let predicted = dec.symbol(&mut cdfs.segment_pred[ctx]) != 0; | ||
| 606 | + stamp_seg_pred(mi_r, mi_c, w_mi, h_mi, u8::from(predicted)); | ||
| 607 | + if predicted { | ||
| 608 | + predicted_segment_id(mi_r, mi_c, w_mi, h_mi) | ||
| 609 | + } else { | ||
| 610 | + read_segment_id(dec, cdfs, mi_r, mi_c, false) | ||
| 611 | + } | ||
| 612 | + } else { | ||
| 613 | + read_segment_id(dec, cdfs, mi_r, mi_c, false) | ||
| 614 | + }; | ||
| 615 | + write_segment_id(mi_r, mi_c, w_mi, h_mi, id); | ||
| 616 | +} | ||
| 617 | + | ||
| 618 | +/// `av1_get_pred_context_seg_id`: above + left `seg_id_predicted` flags. | ||
| 619 | +fn seg_pred_ctx(mi_r: usize, mi_c: usize) -> usize { | ||
| 620 | + // libaom reads the flag off `above_mbmi`/`left_mbmi`, which are NULL (and | ||
| 621 | + // so contribute 0) outside the tile -- the availability check here is | ||
| 622 | + // what keeps a previous tile's stamps out of this tile's context. | ||
| 623 | + let (row0, col0) = SEG_TILE_ORIGIN.with(|c| c.get()); | ||
| 624 | + let above = if mi_r > row0 { | ||
| 625 | + ABOVE_SEG_PRED.with(|m| m.borrow().get(mi_c).copied().unwrap_or(0)) | ||
| 626 | + } else { | ||
| 627 | + 0 | ||
| 628 | + }; | ||
| 629 | + let left = if mi_c > col0 { | ||
| 630 | + LEFT_SEG_PRED.with(|m| m.borrow().get(mi_r).copied().unwrap_or(0)) | ||
| 631 | + } else { | ||
| 632 | + 0 | ||
| 633 | + }; | ||
| 634 | + usize::from(above + left) | ||
| 635 | +} | ||
| 636 | + | ||
| 637 | +/// Spec 5.11.9's `AboveSegPredContext`/`LeftSegPredContext` update. | ||
| 638 | +fn stamp_seg_pred(mi_r: usize, mi_c: usize, w_mi: usize, h_mi: usize, value: u8) { | ||
| 639 | + ABOVE_SEG_PRED.with(|m| { | ||
| 640 | + let mut m = m.borrow_mut(); | ||
| 641 | + for i in 0..w_mi { | ||
| 642 | + if let Some(cell) = m.get_mut(mi_c + i) { | ||
| 643 | + *cell = value; | ||
| 644 | + } | ||
| 645 | + } | ||
| 646 | + }); | ||
| 647 | + LEFT_SEG_PRED.with(|m| { | ||
| 648 | + let mut m = m.borrow_mut(); | ||
| 649 | + for i in 0..h_mi { | ||
| 650 | + if let Some(cell) = m.get_mut(mi_r + i) { | ||
| 651 | + *cell = value; | ||
| 652 | + } | ||
| 653 | + } | ||
| 654 | + }); | ||
| 655 | +} | ||
| 656 | + | ||
| 318 | // How many key-frame luma blocks resolved `smooth_neighbor` (spec | 657 | // How many key-frame luma blocks resolved `smooth_neighbor` (spec |
| 319 | // `get_intra_edge_filter_type`) to `true` -- lane-chroma r2's own before/after | 658 | // `get_intra_edge_filter_type`) to `true` -- lane-chroma r2's own before/after |
| 320 | // counter, proving a stream actually put a directional block next to a | 659 | // counter, proving a stream actually put a directional block next to a |
| @@ -1924,6 +2263,9 @@ impl Neighbours { | |||
| 1924 | /// regardless of tile ([`Self::start_row`], already called once per SB | 2263 | /// regardless of tile ([`Self::start_row`], already called once per SB |
| 1925 | /// row by every caller) so it needs no tile-specific reset here. | 2264 | /// row by every caller) so it needs no tile-specific reset here. |
| 1926 | fn start_tile(&mut self, row0_mi: usize, col0_mi: usize, col1_mi: usize) { | 2265 | fn start_tile(&mut self, row0_mi: usize, col0_mi: usize, col1_mi: usize) { |
| 2266 | + // lane-seg: `AvailU`/`AvailL` for the segment-id neighbour prediction | ||
| 2267 | + // are tile-relative, same as every other neighbour lookup here. | ||
| 2268 | + SEG_TILE_ORIGIN.with(|c| c.set((row0_mi, col0_mi))); | ||
| 1927 | self.tile_row0_mi = row0_mi; | 2269 | self.tile_row0_mi = row0_mi; |
| 1928 | self.tile_col0_mi = col0_mi; | 2270 | self.tile_col0_mi = col0_mi; |
| 1929 | let end = col1_mi.min(self.above.len()); | 2271 | let end = col1_mi.min(self.above.len()); |
| @@ -2987,8 +3329,16 @@ fn read_intra_mode_rect( | |||
| 2987 | } | 3329 | } |
| 2988 | }; | 3330 | }; |
| 2989 | } | 3331 | } |
| 3332 | + // Same `intra_segment_id` placement as the square reader above. | ||
| 3333 | + let (seg_w_mi, seg_h_mi) = (bw / 4, bh / 4); | ||
| 3334 | + if seg_id_pre_skip() { | ||
| 3335 | + intra_segment_id(dec, cdfs, mi_r, mi_c, seg_w_mi, seg_h_mi, false); | ||
| 3336 | + } | ||
| 2990 | let skip = dec.symbol(&mut cdfs.skip[skip_ctx]) != 0; | 3337 | let skip = dec.symbol(&mut cdfs.skip[skip_ctx]) != 0; |
| 2991 | istep!("skip", skip as i32); | 3338 | istep!("skip", skip as i32); |
| 3339 | + if !seg_id_pre_skip() { | ||
| 3340 | + intra_segment_id(dec, cdfs, mi_r, mi_c, seg_w_mi, seg_h_mi, skip); | ||
| 3341 | + } | ||
| 2992 | maybe_read_cdef_idx(dec, mi_r, mi_c, skip); | 3342 | maybe_read_cdef_idx(dec, mi_r, mi_c, skip); |
| 2993 | istep!("cdef", 0); | 3343 | istep!("cdef", 0); |
| 2994 | // A HORZ/VERT rect strip is never the whole superblock (`bw`/`bh` never | 3344 | // A HORZ/VERT rect strip is never the whole superblock (`bw`/`bh` never |
| @@ -3343,7 +3693,7 @@ fn decode_block_rect( | |||
| 3343 | bw, | 3693 | bw, |
| 3344 | bh, | 3694 | bh, |
| 3345 | crate::decode::bit_depth(), | 3695 | crate::decode::bit_depth(), |
| 3346 | - CURRENT_Q_IDX.with(|c| c.get()), | 3696 | + block_q_idx(), |
| 3347 | plane_q_delta(0).0, | 3697 | plane_q_delta(0).0, |
| 3348 | plane_q_delta(0).1, | 3698 | plane_q_delta(0).1, |
| 3349 | luma_tx_type, | 3699 | luma_tx_type, |
| @@ -3384,7 +3734,7 @@ fn decode_block_rect( | |||
| 3384 | chroma_w, | 3734 | chroma_w, |
| 3385 | chroma_h, | 3735 | chroma_h, |
| 3386 | crate::decode::bit_depth(), | 3736 | crate::decode::bit_depth(), |
| 3387 | - CURRENT_Q_IDX.with(|c| c.get()), | 3737 | + block_q_idx(), |
| 3388 | plane_q_delta(1).0, | 3738 | plane_q_delta(1).0, |
| 3389 | plane_q_delta(1).1, | 3739 | plane_q_delta(1).1, |
| 3390 | u_tx_type, | 3740 | u_tx_type, |
| @@ -3424,7 +3774,7 @@ fn decode_block_rect( | |||
| 3424 | chroma_w, | 3774 | chroma_w, |
| 3425 | chroma_h, | 3775 | chroma_h, |
| 3426 | crate::decode::bit_depth(), | 3776 | crate::decode::bit_depth(), |
| 3427 | - CURRENT_Q_IDX.with(|c| c.get()), | 3777 | + block_q_idx(), |
| 3428 | plane_q_delta(2).0, | 3778 | plane_q_delta(2).0, |
| 3429 | plane_q_delta(2).1, | 3779 | plane_q_delta(2).1, |
| 3430 | v_tx_type, | 3780 | v_tx_type, |
| @@ -3783,7 +4133,7 @@ fn decode_block_rect64( | |||
| 3783 | luma_levels[row * bw..][..32].copy_from_slice(&luma_corner[row * 32..][..32]); | 4133 | luma_levels[row * bw..][..32].copy_from_slice(&luma_corner[row * 32..][..32]); |
| 3784 | } | 4134 | } |
| 3785 | { | 4135 | { |
| 3786 | - let cur = CURRENT_Q_IDX.with(|c| c.get()); | 4136 | + let cur = block_q_idx(); |
| 3787 | if cur != i32::from(base_q_idx) { | 4137 | if cur != i32::from(base_q_idx) { |
| 3788 | RECT64_QIDX_DRIFT_HITS.with(|c| c.set(c.get() + 1)); | 4138 | RECT64_QIDX_DRIFT_HITS.with(|c| c.set(c.get() + 1)); |
| 3789 | } | 4139 | } |
| @@ -3798,7 +4148,7 @@ fn decode_block_rect64( | |||
| 3798 | bw, | 4148 | bw, |
| 3799 | bh, | 4149 | bh, |
| 3800 | crate::decode::bit_depth(), | 4150 | crate::decode::bit_depth(), |
| 3801 | - CURRENT_Q_IDX.with(|c| c.get()), | 4151 | + block_q_idx(), |
| 3802 | plane_q_delta(0).0, | 4152 | plane_q_delta(0).0, |
| 3803 | plane_q_delta(0).1, | 4153 | plane_q_delta(0).1, |
| 3804 | luma_tx_type, | 4154 | luma_tx_type, |
| @@ -3855,7 +4205,7 @@ fn decode_block_rect64( | |||
| 3855 | eprintln!("EC_COEFF_VAL plane=1 row={mi_r} col={mi_c} rng={rng}"); | 4205 | eprintln!("EC_COEFF_VAL plane=1 row={mi_r} col={mi_c} rng={rng}"); |
| 3856 | } | 4206 | } |
| 3857 | { | 4207 | { |
| 3858 | - let cur = CURRENT_Q_IDX.with(|c| c.get()); | 4208 | + let cur = block_q_idx(); |
| 3859 | if cur != i32::from(base_q_idx) { | 4209 | if cur != i32::from(base_q_idx) { |
| 3860 | RECT64_QIDX_DRIFT_HITS.with(|c| c.set(c.get() + 1)); | 4210 | RECT64_QIDX_DRIFT_HITS.with(|c| c.set(c.get() + 1)); |
| 3861 | } | 4211 | } |
| @@ -3870,7 +4220,7 @@ fn decode_block_rect64( | |||
| 3870 | chroma_w, | 4220 | chroma_w, |
| 3871 | chroma_h, | 4221 | chroma_h, |
| 3872 | crate::decode::bit_depth(), | 4222 | crate::decode::bit_depth(), |
| 3873 | - CURRENT_Q_IDX.with(|c| c.get()), | 4223 | + block_q_idx(), |
| 3874 | plane_q_delta(1).0, | 4224 | plane_q_delta(1).0, |
| 3875 | plane_q_delta(1).1, | 4225 | plane_q_delta(1).1, |
| 3876 | u_tx_type, | 4226 | u_tx_type, |
| @@ -3909,7 +4259,7 @@ fn decode_block_rect64( | |||
| 3909 | eprintln!("EC_COEFF_VAL plane=2 row={mi_r} col={mi_c} rng={rng}"); | 4259 | eprintln!("EC_COEFF_VAL plane=2 row={mi_r} col={mi_c} rng={rng}"); |
| 3910 | } | 4260 | } |
| 3911 | { | 4261 | { |
| 3912 | - let cur = CURRENT_Q_IDX.with(|c| c.get()); | 4262 | + let cur = block_q_idx(); |
| 3913 | if cur != i32::from(base_q_idx) { | 4263 | if cur != i32::from(base_q_idx) { |
| 3914 | RECT64_QIDX_DRIFT_HITS.with(|c| c.set(c.get() + 1)); | 4264 | RECT64_QIDX_DRIFT_HITS.with(|c| c.set(c.get() + 1)); |
| 3915 | } | 4265 | } |
| @@ -3924,7 +4274,7 @@ fn decode_block_rect64( | |||
| 3924 | chroma_w, | 4274 | chroma_w, |
| 3925 | chroma_h, | 4275 | chroma_h, |
| 3926 | crate::decode::bit_depth(), | 4276 | crate::decode::bit_depth(), |
| 3927 | - CURRENT_Q_IDX.with(|c| c.get()), | 4277 | + block_q_idx(), |
| 3928 | plane_q_delta(2).0, | 4278 | plane_q_delta(2).0, |
| 3929 | plane_q_delta(2).1, | 4279 | plane_q_delta(2).1, |
| 3930 | v_tx_type, | 4280 | v_tx_type, |
| @@ -4022,11 +4372,20 @@ fn read_intra_mode( | |||
| 4022 | } | 4372 | } |
| 4023 | }; | 4373 | }; |
| 4024 | } | 4374 | } |
| 4375 | + // spec 5.11.6 `intra_frame_mode_info`: `intra_segment_id` is read BEFORE | ||
| 4376 | + // `skip` when `SegIdPreSkip`, and after it otherwise. | ||
| 4377 | + let (seg_w_mi, seg_h_mi) = (side / 4, side / 4); | ||
| 4378 | + if seg_id_pre_skip() { | ||
| 4379 | + intra_segment_id(dec, cdfs, mi_r, mi_c, seg_w_mi, seg_h_mi, false); | ||
| 4380 | + } | ||
| 4025 | let skip = dec.symbol(&mut cdfs.skip[skip_ctx]) != 0; | 4381 | let skip = dec.symbol(&mut cdfs.skip[skip_ctx]) != 0; |
| 4026 | if trace { | 4382 | if trace { |
| 4027 | eprintln!("TRACE skip value={}", skip as i32); | 4383 | eprintln!("TRACE skip value={}", skip as i32); |
| 4028 | } | 4384 | } |
| 4029 | istep!("skip", skip as i32); | 4385 | istep!("skip", skip as i32); |
| 4386 | + if !seg_id_pre_skip() { | ||
| 4387 | + intra_segment_id(dec, cdfs, mi_r, mi_c, seg_w_mi, seg_h_mi, skip); | ||
| 4388 | + } | ||
| 4030 | // spec order (see the comment below on `read_intrabc_info`): `skip`, | 4389 | // spec order (see the comment below on `read_intrabc_info`): `skip`, |
| 4031 | // `segment_id`, `cdef`, `delta_q` -- `cdef` lands right here. | 4390 | // `segment_id`, `cdef`, `delta_q` -- `cdef` lands right here. |
| 4032 | maybe_read_cdef_idx(dec, mi_r, mi_c, skip); | 4391 | maybe_read_cdef_idx(dec, mi_r, mi_c, skip); |
| @@ -4684,7 +5043,7 @@ fn read_plane( | |||
| 4684 | full | 5043 | full |
| 4685 | }; | 5044 | }; |
| 4686 | let (dc_delta, ac_delta) = plane_q_delta(plane_idx); | 5045 | let (dc_delta, ac_delta) = plane_q_delta(plane_idx); |
| 4687 | - let residual = dequant_and_inverse_typed(&levels, side, bit_depth(), CURRENT_Q_IDX.with(|c| c.get()), dc_delta, ac_delta, tx_type); | 5046 | + let residual = dequant_and_inverse_typed(&levels, side, bit_depth(), block_q_idx(), dc_delta, ac_delta, tx_type); |
| 4688 | if std::env::var_os("EC_AV1_TRACE").is_some() { | 5047 | if std::env::var_os("EC_AV1_TRACE").is_some() { |
| 4689 | eprintln!( | 5048 | eprintln!( |
| 4690 | "TRACE dequant plane={plane_idx} base_q_idx={base_q_idx} tx_type={tx_type:?} side={side} levels={levels:?} residual={residual:?}", | 5049 | "TRACE dequant plane={plane_idx} base_q_idx={base_q_idx} tx_type={tx_type:?} side={side} levels={levels:?} residual={residual:?}", |
| @@ -5806,7 +6165,14 @@ fn unit_log2(px: u32) -> i32 { | |||
| 5806 | /// the flat `mode_deltas[1]` this used before was a `LAST_FRAME`-reachable | 6165 | /// the flat `mode_deltas[1]` this used before was a `LAST_FRAME`-reachable |
| 5807 | /// wrong delta on every `GLOBALMV` block, masked while any `GOLDEN_FRAME` | 6166 | /// wrong delta on every `GLOBALMV` block, masked while any `GOLDEN_FRAME` |
| 5808 | /// block anywhere aborted the whole stream's decode before comparison). | 6167 | /// block anywhere aborted the whole stream's decode before comparison). |
| 5809 | -fn lf_level(lf: &LoopFilterParams, plane_idx: usize, dir: usize, ref_frame: i8, delta_lf: i32) -> i32 { | 6168 | +fn lf_level( |
| 6169 | + lf: &LoopFilterParams, | ||
| 6170 | + plane_idx: usize, | ||
| 6171 | + dir: usize, | ||
| 6172 | + ref_frame: i8, | ||
| 6173 | + delta_lf: i32, | ||
| 6174 | + segment_id: u8, | ||
| 6175 | +) -> i32 { | ||
| 5810 | let base = i32::from(if plane_idx == 0 { | 6176 | let base = i32::from(if plane_idx == 0 { |
| 5811 | lf.level[dir] | 6177 | lf.level[dir] |
| 5812 | } else if plane_idx == 1 { | 6178 | } else if plane_idx == 1 { |
| @@ -5818,6 +6184,16 @@ fn lf_level(lf: &LoopFilterParams, plane_idx: usize, dir: usize, ref_frame: i8, | |||
| 5818 | // ref/mode delta scaling below, and regardless of `delta_enabled` | 6184 | // ref/mode delta scaling below, and regardless of `delta_enabled` |
| 5819 | // (that flag only gates `ref_deltas`/`mode_deltas`). | 6185 | // (that flag only gates `ref_deltas`/`mode_deltas`). |
| 5820 | let base = (base + delta_lf).clamp(0, 63); | 6186 | let base = (base + delta_lf).clamp(0, 63); |
| 6187 | + // lane-seg, spec 7.14.4 / libaom `av1_loop_filter_frame_init`: this | ||
| 6188 | + // block's segment's own `SEG_LVL_ALT_LF_*` delta applies after the | ||
| 6189 | + // per-superblock `DeltaLF` term and before the ref/mode deltas below. | ||
| 6190 | + // Feature index: `SEG_LVL_ALT_LF_Y_V + dir` for luma, `_U`/`_V` (3/4) | ||
| 6191 | + // for chroma. | ||
| 6192 | + let feature = if plane_idx == 0 { SEG_LVL_ALT_LF_Y_V + dir } else { plane_idx + 2 }; | ||
| 6193 | + let base = match seg_feature(usize::from(segment_id), feature) { | ||
| 6194 | + Some(data) => (base + data).clamp(0, 63), | ||
| 6195 | + None => base, | ||
| 6196 | + }; | ||
| 5821 | if !lf.delta_enabled { | 6197 | if !lf.delta_enabled { |
| 5822 | return base; | 6198 | return base; |
| 5823 | } | 6199 | } |
| @@ -5873,8 +6249,15 @@ fn edge_params( | |||
| 5873 | let cur_delta_lf = i32::from(n.delta_lf_grid[mi_r * n.skip_grid_cols_mi + mi_c][delta_idx]); | 6249 | let cur_delta_lf = i32::from(n.delta_lf_grid[mi_r * n.skip_grid_cols_mi + mi_c][delta_idx]); |
| 5874 | let pv_delta_lf = i32::from(n.delta_lf_grid[pv_mi_r * n.skip_grid_cols_mi + pv_mi_c][delta_idx]); | 6250 | let pv_delta_lf = i32::from(n.delta_lf_grid[pv_mi_r * n.skip_grid_cols_mi + pv_mi_c][delta_idx]); |
| 5875 | 6251 | ||
| 5876 | - let cur_level = lf_level(lf, plane_idx, dir, cur_ref, cur_delta_lf); | 6252 | + let cur_level = lf_level(lf, plane_idx, dir, cur_ref, cur_delta_lf, segment_id_at(mi_r, mi_c)); |
| 5877 | - let pv_level = lf_level(lf, plane_idx, dir, pv_ref, pv_delta_lf); | 6253 | + let pv_level = lf_level( |
| 6254 | + lf, | ||
| 6255 | + plane_idx, | ||
| 6256 | + dir, | ||
| 6257 | + pv_ref, | ||
| 6258 | + pv_delta_lf, | ||
| 6259 | + segment_id_at(pv_mi_r, pv_mi_c), | ||
| 6260 | + ); | ||
| 5878 | if cur_level == 0 && pv_level == 0 { | 6261 | if cur_level == 0 && pv_level == 0 { |
| 5879 | return None; | 6262 | return None; |
| 5880 | } | 6263 | } |
| @@ -7780,7 +8163,7 @@ fn read_inter_plane( | |||
| 7780 | None, | 8163 | None, |
| 7781 | )?; | 8164 | )?; |
| 7782 | let (dc_delta, ac_delta) = plane_q_delta(plane_idx); | 8165 | let (dc_delta, ac_delta) = plane_q_delta(plane_idx); |
| 7783 | - let residual = dequant_and_inverse_typed(&grid, side, bit_depth(), CURRENT_Q_IDX.with(|c| c.get()), dc_delta, ac_delta, tx_type); | 8166 | + let residual = dequant_and_inverse_typed(&grid, side, bit_depth(), block_q_idx(), dc_delta, ac_delta, tx_type); |
| 7784 | plane.reconstruct_mc(x, y, side, prediction, &residual); | 8167 | plane.reconstruct_mc(x, y, side, prediction, &residual); |
| 7785 | Ok((grid, tx_type)) | 8168 | Ok((grid, tx_type)) |
| 7786 | } | 8169 | } |
| @@ -8864,6 +9247,11 @@ fn decode_inter_block( | |||
| 8864 | dec.debug_bitpos() | 9247 | dec.debug_bitpos() |
| 8865 | ); | 9248 | ); |
| 8866 | } | 9249 | } |
| 9250 | + // spec 5.11.5 `inter_frame_mode_info`: `inter_segment_id(1)` before | ||
| 9251 | + // `skip_mode`/`skip`, `inter_segment_id(0)` after them. | ||
| 9252 | + let (seg_mi_r, seg_mi_c) = (r * SUB_MI as usize, c * SUB_MI as usize); | ||
| 9253 | + let (seg_w_mi, seg_h_mi) = (side / 4, side / 4); | ||
| 9254 | + inter_segment_id(dec, cdfs, seg_mi_r, seg_mi_c, seg_w_mi, seg_h_mi, false, true); | ||
| 8867 | let skip_mode = skip_mode_present && dec.symbol(&mut cdfs.skip_mode[skip_mode_ctx]) == 1; | 9255 | let skip_mode = skip_mode_present && dec.symbol(&mut cdfs.skip_mode[skip_mode_ctx]) == 1; |
| 8868 | if std::env::var_os("EC_AV1_SKIPMODE_DUMP").is_some() { | 9256 | if std::env::var_os("EC_AV1_SKIPMODE_DUMP").is_some() { |
| 8869 | eprintln!( | 9257 | eprintln!( |
| @@ -8877,6 +9265,7 @@ fn decode_inter_block( | |||
| 8877 | 9265 | ||
| 8878 | let skip_ctx = usize::from(neighbours.above_skip[c]) + usize::from(neighbours.left_skip[r]); | 9266 | let skip_ctx = usize::from(neighbours.above_skip[c]) + usize::from(neighbours.left_skip[r]); |
| 8879 | let skip = skip_mode || dec.symbol(&mut cdfs.skip[skip_ctx]) == 1; | 9267 | let skip = skip_mode || dec.symbol(&mut cdfs.skip[skip_ctx]) == 1; |
| 9268 | + inter_segment_id(dec, cdfs, seg_mi_r, seg_mi_c, seg_w_mi, seg_h_mi, skip, false); | ||
| 8880 | maybe_read_cdef_idx(dec, r * SUB_MI as usize, c * SUB_MI as usize, skip); | 9269 | maybe_read_cdef_idx(dec, r * SUB_MI as usize, c * SUB_MI as usize, skip); |
| 8881 | maybe_read_delta_q(dec, cdfs, r * SUB_MI as usize, c * SUB_MI as usize, side == 64, skip); | 9270 | maybe_read_delta_q(dec, cdfs, r * SUB_MI as usize, c * SUB_MI as usize, side == 64, skip); |
| 8882 | maybe_read_delta_lf(dec, cdfs, r * SUB_MI as usize, c * SUB_MI as usize, side == 64, skip); | 9271 | maybe_read_delta_lf(dec, cdfs, r * SUB_MI as usize, c * SUB_MI as usize, side == 64, skip); |
| @@ -10714,6 +11103,9 @@ fn decode_inter_block8( | |||
| 10714 | } | 11103 | } |
| 10715 | let skip_mode_ctx = | 11104 | let skip_mode_ctx = |
| 10716 | usize::from(neighbours.above_skip_mode[c]) + usize::from(neighbours.left_skip_mode[r]); | 11105 | usize::from(neighbours.above_skip_mode[c]) + usize::from(neighbours.left_skip_mode[r]); |
| 11106 | + // Same two `inter_segment_id` positions as [`decode_inter_block`]; this | ||
| 11107 | + // leaf is always `BLOCK_8X8`, i.e. 2x2 mi. | ||
| 11108 | + inter_segment_id(dec, cdfs, leaf_mi.0, leaf_mi.1, 2, 2, false, true); | ||
| 10717 | let skip_mode = skip_mode_present && dec.symbol(&mut cdfs.skip_mode[skip_mode_ctx]) == 1; | 11109 | let skip_mode = skip_mode_present && dec.symbol(&mut cdfs.skip_mode[skip_mode_ctx]) == 1; |
| 10718 | if skip_mode { | 11110 | if skip_mode { |
| 10719 | SKIP_MODE_HITS.with(|c| c.set(c.get() + 1)); | 11111 | SKIP_MODE_HITS.with(|c| c.set(c.get() + 1)); |
| @@ -10721,6 +11113,7 @@ fn decode_inter_block8( | |||
| 10721 | 11113 | ||
| 10722 | let skip_ctx = usize::from(above_skip) + usize::from(left_skip); | 11114 | let skip_ctx = usize::from(above_skip) + usize::from(left_skip); |
| 10723 | let skip = skip_mode || dec.symbol(&mut cdfs.skip[skip_ctx]) == 1; | 11115 | let skip = skip_mode || dec.symbol(&mut cdfs.skip[skip_ctx]) == 1; |
| 11116 | + inter_segment_id(dec, cdfs, leaf_mi.0, leaf_mi.1, 2, 2, skip, false); | ||
| 10724 | maybe_read_cdef_idx(dec, leaf_mi.0, leaf_mi.1, skip); | 11117 | maybe_read_cdef_idx(dec, leaf_mi.0, leaf_mi.1, skip); |
| 10725 | // Always a `BLOCK_8X8` leaf here, never the whole superblock. | 11118 | // Always a `BLOCK_8X8` leaf here, never the whole superblock. |
| 10726 | maybe_read_delta_q(dec, cdfs, leaf_mi.0, leaf_mi.1, false, skip); | 11119 | maybe_read_delta_q(dec, cdfs, leaf_mi.0, leaf_mi.1, false, skip); |
crates/ec-av1/src/refusal_inventory.rsmodified+1 -1
| @@ -56,7 +56,7 @@ const REFUSALS: &[&str] = &[ | |||
| 56 | "a frame OBU with no tile group", | 56 | "a frame OBU with no tile group", |
| 57 | "a frame naming primary_ref_frame at a reference slot with no saved CDF state", | 57 | "a frame naming primary_ref_frame at a reference slot with no saved CDF state", |
| 58 | "a frame with no mode-info grid", | 58 | "a frame with no mode-info grid", |
| 59 | - "a frame with segmentation enabled (this decoder never reads a per-block segment_id symbol)", | 59 | + "a frame whose segmentation enables SEG_LVL_REF_FRAME/SKIP/GLOBALMV (this decoder reads segment_id but never lets a segment override a block's reference, skip or mode)", |
| 60 | "a non-skip rectangular (HORZ/VERT/HORZ_B) strip needs rectangular residual coding", | 60 | "a non-skip rectangular (HORZ/VERT/HORZ_B) strip needs rectangular residual coding", |
| 61 | "a palette block with a split luma transform (round 1)", | 61 | "a palette block with a split luma transform (round 1)", |
| 62 | "a reference frame other than LAST_FRAME (round 2)", | 62 | "a reference frame other than LAST_FRAME (round 2)", |
crates/ec-av1/src/stream.rsmodified+309 -5
| @@ -16,7 +16,8 @@ | |||
| 16 | //! an implementation detail of how this crate happens to build it). | 16 | //! an implementation detail of how this crate happens to build it). |
| 17 | 17 | ||
| 18 | use ec_av1_syntax::{ | 18 | use ec_av1_syntax::{ |
| 19 | - Av1Parser, FrameHeader, FrameType, NUM_REF_FRAMES, ObuKind, PRIMARY_REF_NONE, Tile, TxMode, | 19 | + Av1Parser, FrameHeader, FrameType, MAX_SEGMENTS, NUM_REF_FRAMES, ObuKind, PRIMARY_REF_NONE, |
| 20 | + SEG_LVL_GLOBALMV, SEG_LVL_REF_FRAME, SEG_LVL_SKIP, Tile, TxMode, | ||
| 20 | WarpModel, | 21 | WarpModel, |
| 21 | }; | 22 | }; |
| 22 | use ec_core::{Error, Result}; | 23 | use ec_core::{Error, Result}; |
| @@ -74,6 +75,12 @@ pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> { | |||
| 74 | // own end-of-tile adapted table, or (when the frame set | 75 | // own end-of-tile adapted table, or (when the frame set |
| 75 | // `disable_frame_end_update_cdf`) the table it started from, unchanged. | 76 | // `disable_frame_end_update_cdf`) the table it started from, unchanged. |
| 76 | let mut cdf_slots: [Option<Cdfs>; NUM_REF_FRAMES] = std::array::from_fn(|_| None); | 77 | let mut cdf_slots: [Option<Cdfs>; NUM_REF_FRAMES] = std::array::from_fn(|_| None); |
| 78 | + // lane-seg, spec 7.20's `SavedSegmentIds`: each reference slot also | ||
| 79 | + // remembers the segment map the frame stored there decoded, which is what | ||
| 80 | + // a later frame's `primary_ref_frame` reads back as `PrevSegmentIds` | ||
| 81 | + // (spec `load_previous_segment_ids`) for temporal segment-id prediction | ||
| 82 | + // and for a `segmentation_update_map == 0` frame's inherited map. | ||
| 83 | + let mut seg_map_slots: [Option<Vec<u8>>; NUM_REF_FRAMES] = std::array::from_fn(|_| None); | ||
| 77 | 84 | ||
| 78 | let mut pos = 0usize; | 85 | let mut pos = 0usize; |
| 79 | // A frame whose tiles are split across several `OBU_TILE_GROUP`s (spec | 86 | // A frame whose tiles are split across several `OBU_TILE_GROUP`s (spec |
| @@ -124,6 +131,7 @@ pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> { | |||
| 124 | cdf_slots[i] = cdf_slots[slot].clone(); | 131 | cdf_slots[i] = cdf_slots[slot].clone(); |
| 125 | ref_slots[i] = Some(picture.clone()); | 132 | ref_slots[i] = Some(picture.clone()); |
| 126 | motion_field_slots[i] = motion_field_slots[slot].clone(); | 133 | motion_field_slots[i] = motion_field_slots[slot].clone(); |
| 134 | + seg_map_slots[i] = seg_map_slots[slot].clone(); | ||
| 127 | order_hint_slots[i] = order_hint_slots[slot]; | 135 | order_hint_slots[i] = order_hint_slots[slot]; |
| 128 | } | 136 | } |
| 129 | } | 137 | } |
| @@ -245,12 +253,41 @@ pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> { | |||
| 245 | // 8-bit LUT/clamp, above), and any other rounding gap 10-bit exposes | 253 | // 8-bit LUT/clamp, above), and any other rounding gap 10-bit exposes |
| 246 | // will show up as a real pixel mismatch, not a silent truncation -- | 254 | // will show up as a real pixel mismatch, not a silent truncation -- |
| 247 | // the whole reason the widen (and this wiring) happened. | 255 | // the whole reason the widen (and this wiring) happened. |
| 256 | + // lane-seg: `segment_id` is read per block now (spec 5.11.7-5.11.9, | ||
| 257 | + // `decode::intra_segment_id`/`inter_segment_id`) and consumed by the | ||
| 258 | + // per-segment quantizer (spec 7.12.2) and loop-filter level | ||
| 259 | + // (spec 7.14.4). The three features that instead REWRITE a block's | ||
| 260 | + // mode/reference decisions -- `SEG_LVL_REF_FRAME` (forces the | ||
| 261 | + // reference), `SEG_LVL_SKIP` and `SEG_LVL_GLOBALMV` (both suppress | ||
| 262 | + // symbols `decode_inter_block` unconditionally reads) -- have no | ||
| 263 | + // reader on the inter path, so they still refuse by name rather than | ||
| 264 | + // desyncing. aomenc's `--aq-mode` segmentation only ever sets | ||
| 265 | + // `SEG_LVL_ALT_Q`. | ||
| 248 | if header.segmentation.enabled { | 266 | if header.segmentation.enabled { |
| 249 | - return Err(Error::unsupported( | 267 | + for segment in 0..MAX_SEGMENTS { |
| 250 | - "AV1 decode_stream", | 268 | + for feature in [SEG_LVL_REF_FRAME, SEG_LVL_SKIP, SEG_LVL_GLOBALMV] { |
| 251 | - "a frame with segmentation enabled (this decoder never reads a per-block segment_id symbol)", | 269 | + if header.segmentation.feature_enabled[segment][feature] { |
| 252 | - )); | 270 | + return Err(Error::unsupported( |
| 271 | + "AV1 decode_stream", | ||
| 272 | + "a frame whose segmentation enables SEG_LVL_REF_FRAME/SKIP/GLOBALMV (this decoder reads segment_id but never lets a segment override a block's reference, skip or mode)", | ||
| 273 | + )); | ||
| 274 | + } | ||
| 275 | + } | ||
| 276 | + } | ||
| 253 | } | 277 | } |
| 278 | + // spec `load_previous_segment_ids`: `PrevSegmentIds` comes from the | ||
| 279 | + // primary reference frame's slot, and is all-zero without one. | ||
| 280 | + let prev_seg_map = if header.primary_ref_frame == PRIMARY_REF_NONE { | ||
| 281 | + None | ||
| 282 | + } else { | ||
| 283 | + seg_map_slots[header.ref_frame_idx[header.primary_ref_frame as usize] as usize].clone() | ||
| 284 | + }; | ||
| 285 | + decode::set_segmentation( | ||
| 286 | + header.segmentation, | ||
| 287 | + header.mi_rows as usize, | ||
| 288 | + header.mi_cols as usize, | ||
| 289 | + prev_seg_map.as_deref(), | ||
| 290 | + ); | ||
| 254 | // lane-superres stage 2/3: `use_superres` adds/removes no per-block | 291 | // lane-superres stage 2/3: `use_superres` adds/removes no per-block |
| 255 | // symbol (spec 7.16's upscaling is a pixel-domain post-process | 292 | // symbol (spec 7.16's upscaling is a pixel-domain post-process |
| 256 | // libaom's `decodeframe.c` runs between CDEF and loop restoration, | 293 | // libaom's `decodeframe.c` runs between CDEF and loop restoration, |
| @@ -696,6 +733,7 @@ pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> { | |||
| 696 | } | 733 | } |
| 697 | } | 734 | } |
| 698 | pictures_decoded += 1; | 735 | pictures_decoded += 1; |
| 736 | + let decoded_seg_map = decode::take_segment_ids(); | ||
| 699 | for i in 0..NUM_REF_FRAMES { | 737 | for i in 0..NUM_REF_FRAMES { |
| 700 | if header.refresh_frame_flags & (1 << i) != 0 { | 738 | if header.refresh_frame_flags & (1 << i) != 0 { |
| 701 | cdf_slots[i] = Some(stored_cdfs.clone()); | 739 | cdf_slots[i] = Some(stored_cdfs.clone()); |
| @@ -705,6 +743,7 @@ pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> { | |||
| 705 | // caller's output below carries the grained picture. | 743 | // caller's output below carries the grained picture. |
| 706 | ref_slots[i] = Some(picture.clone()); | 744 | ref_slots[i] = Some(picture.clone()); |
| 707 | motion_field_slots[i] = Some(motion_field.clone()); | 745 | motion_field_slots[i] = Some(motion_field.clone()); |
| 746 | + seg_map_slots[i] = Some(decoded_seg_map.clone()); | ||
| 708 | order_hint_slots[i] = header.order_hint; | 747 | order_hint_slots[i] = header.order_hint; |
| 709 | } | 748 | } |
| 710 | } | 749 | } |
| @@ -8644,6 +8683,271 @@ mod tests { | |||
| 8644 | ); | 8683 | ); |
| 8645 | } | 8684 | } |
| 8646 | 8685 | ||
| 8686 | + /// lane-seg: a real `aomenc` stream with SEGMENTATION genuinely active. | ||
| 8687 | + /// `--aq-mode=1` (variance AQ) / `--aq-mode=2` (complexity AQ) are the two | ||
| 8688 | + /// paths libaom uses to switch `segmentation_enabled` on by itself | ||
| 8689 | + /// (`av1_vaq_frame_setup`/`av1_setup_in_frame_q_adj`, both of which only | ||
| 8690 | + /// ever set `SEG_LVL_ALT_Q`); `--deltaq-mode=0` keeps the per-superblock | ||
| 8691 | + /// delta-q reader out of the picture so a mismatch points at the segment | ||
| 8692 | + /// map, not at `CurrentQIndex`. Mandelbrot's varying local contrast is | ||
| 8693 | + /// what gives variance/complexity AQ different segments to assign -- | ||
| 8694 | + /// a flat gradient collapses every block onto one segment, which the | ||
| 8695 | + /// `segment_ids_seen() >= 2` assert below would catch as a vacuous run. | ||
| 8696 | + /// The rest of the recipe is the delta-q gate's own feature-disable | ||
| 8697 | + /// envelope, so this gate isolates the segmentation reader. | ||
| 8698 | + fn run_segmentation_gate( | ||
| 8699 | + name: &str, | ||
| 8700 | + aq_mode: &str, | ||
| 8701 | + bit_depth: usize, | ||
| 8702 | + frame_count: usize, | ||
| 8703 | + rate_args: &[&str], | ||
| 8704 | + ) { | ||
| 8705 | + let _gate_lock = lock_gate_counters(); | ||
| 8706 | + if !have_ffmpeg() { | ||
| 8707 | + eprintln!("SKIP {name}: no ffmpeg"); | ||
| 8708 | + return; | ||
| 8709 | + } | ||
| 8710 | + if !have_aomenc() { | ||
| 8711 | + eprintln!("SKIP {name}: no aomenc at {}", aomenc_path().display()); | ||
| 8712 | + return; | ||
| 8713 | + } | ||
| 8714 | + crate::decode::reset_segment_hits(); | ||
| 8715 | + let (width, height) = (128usize, 64usize); | ||
| 8716 | + let mut named_refusals = 0u32; | ||
| 8717 | + let mut matched = 0u32; | ||
| 8718 | + let n_attempts: u32 = std::env::var("EC_SEG_GATE_ATTEMPTS") | ||
| 8719 | + .ok() | ||
| 8720 | + .and_then(|s| s.parse().ok()) | ||
| 8721 | + .unwrap_or(10); | ||
| 8722 | + for attempt in 0..n_attempts { | ||
| 8723 | + let seed = 42 + attempt; | ||
| 8724 | + let duration = frame_count as f64 / 25.0; | ||
| 8725 | + let source = format!( | ||
| 8726 | + "mandelbrot=size={width}x{height}:rate=25:start_x={sx}:start_y={sy}", | ||
| 8727 | + sx = -0.6 + 0.005 * (attempt as f64), | ||
| 8728 | + sy = -0.4 + 0.005 * (attempt as f64) | ||
| 8729 | + ); | ||
| 8730 | + let pix_fmt = if bit_depth == 10 { "yuv420p10le" } else { "yuv420p" }; | ||
| 8731 | + let y4m = Command::new("ffmpeg") | ||
| 8732 | + .args([ | ||
| 8733 | + "-v", "error", "-f", "lavfi", "-i", &source, "-t", &duration.to_string(), | ||
| 8734 | + "-pix_fmt", pix_fmt, "-strict", "-1", "-f", "yuv4mpegpipe", "-", | ||
| 8735 | + ]) | ||
| 8736 | + .stdin(Stdio::null()) | ||
| 8737 | + .stdout(Stdio::piped()) | ||
| 8738 | + .stderr(Stdio::piped()) | ||
| 8739 | + .output() | ||
| 8740 | + .expect("ffmpeg failed to run"); | ||
| 8741 | + assert!( | ||
| 8742 | + y4m.status.success(), | ||
| 8743 | + "ffmpeg fixture: {}", | ||
| 8744 | + String::from_utf8_lossy(&y4m.stderr) | ||
| 8745 | + ); | ||
| 8746 | + let depth_args: Vec<String> = if bit_depth == 10 { | ||
| 8747 | + vec!["--input-bit-depth=10".to_owned(), "--bit-depth=10".to_owned()] | ||
| 8748 | + } else { | ||
| 8749 | + Vec::new() | ||
| 8750 | + }; | ||
| 8751 | + let aq = format!("--aq-mode={aq_mode}"); | ||
| 8752 | + let mut args: Vec<&str> = vec![ | ||
| 8753 | + "--codec=av1", | ||
| 8754 | + "--passes=1", | ||
| 8755 | + "--cpu-used=4", | ||
| 8756 | + &aq, | ||
| 8757 | + "--deltaq-mode=0", | ||
| 8758 | + "--kf-max-dist=1000", | ||
| 8759 | + "--threads=1", | ||
| 8760 | + "--row-mt=0", | ||
| 8761 | + "--sb-size=64", | ||
| 8762 | + "--auto-alt-ref=1", | ||
| 8763 | + "--lag-in-frames=16", | ||
| 8764 | + "--enable-fwd-kf=0", | ||
| 8765 | + "--enable-order-hint=1", | ||
| 8766 | + "--enable-warped-motion=1", | ||
| 8767 | + "--enable-global-motion=0", | ||
| 8768 | + "--enable-obmc=1", | ||
| 8769 | + "--tune-content=default", | ||
| 8770 | + "--enable-masked-comp=1", | ||
| 8771 | + "--enable-dist-wtd-comp=0", | ||
| 8772 | + "--enable-interintra-comp=1", | ||
| 8773 | + "--enable-onesided-comp=0", | ||
| 8774 | + "--enable-interintra-wedge=0", | ||
| 8775 | + "--enable-smooth-interintra=1", | ||
| 8776 | + "--enable-rect-partitions=0", | ||
| 8777 | + "--enable-ab-partitions=0", | ||
| 8778 | + "--enable-1to4-partitions=0", | ||
| 8779 | + "--enable-filter-intra=0", | ||
| 8780 | + "--enable-smooth-intra=0", | ||
| 8781 | + "--enable-paeth-intra=0", | ||
| 8782 | + "--enable-directional-intra=0", | ||
| 8783 | + "--enable-angle-delta=0", | ||
| 8784 | + "--enable-tx-size-search=0", | ||
| 8785 | + "--enable-cdef=1", | ||
| 8786 | + "--enable-restoration=0", | ||
| 8787 | + "--max-partition-size=32", | ||
| 8788 | + "--min-partition-size=32", | ||
| 8789 | + "--enable-palette=0", | ||
| 8790 | + "--enable-intrabc=0", | ||
| 8791 | + "--enable-cfl-intra=0", | ||
| 8792 | + "--enable-ref-frame-mvs=0", | ||
| 8793 | + ]; | ||
| 8794 | + args.extend(rate_args.iter().copied()); | ||
| 8795 | + args.extend(depth_args.iter().map(String::as_str)); | ||
| 8796 | + args.extend(["--obu", "-o", "-", "-"]); | ||
| 8797 | + let mut child = Command::new(aomenc_path()) | ||
| 8798 | + .args(&args) | ||
| 8799 | + .stdin(Stdio::piped()) | ||
| 8800 | + .stdout(Stdio::piped()) | ||
| 8801 | + .stderr(Stdio::piped()) | ||
| 8802 | + .spawn() | ||
| 8803 | + .expect("aomenc failed to start"); | ||
| 8804 | + child | ||
| 8805 | + .stdin | ||
| 8806 | + .take() | ||
| 8807 | + .expect("aomenc stdin") | ||
| 8808 | + .write_all(&y4m.stdout) | ||
| 8809 | + .expect("writing y4m to aomenc"); | ||
| 8810 | + let out = child.wait_with_output().expect("aomenc failed to run"); | ||
| 8811 | + assert!( | ||
| 8812 | + out.status.success(), | ||
| 8813 | + "aomenc refused the fixture: {}", | ||
| 8814 | + String::from_utf8_lossy(&out.stderr) | ||
| 8815 | + ); | ||
| 8816 | + let stream = out.stdout; | ||
| 8817 | + // The stream must really carry `segmentation_enabled`, or the | ||
| 8818 | + // gate proves nothing about the reader. | ||
| 8819 | + let mut seg_frames = 0u32; | ||
| 8820 | + { | ||
| 8821 | + let mut probe = Av1Parser::new(); | ||
| 8822 | + let mut pos = 0usize; | ||
| 8823 | + while pos < stream.len() { | ||
| 8824 | + let obu = probe.parse_obu(&stream[pos..]).expect("parsing the gate stream"); | ||
| 8825 | + pos += obu.total_size; | ||
| 8826 | + match &obu.kind { | ||
| 8827 | + ObuKind::Frame(h, _) => { | ||
| 8828 | + if h.segmentation.enabled { | ||
| 8829 | + seg_frames += 1; | ||
| 8830 | + } | ||
| 8831 | + } | ||
| 8832 | + ObuKind::FrameHeader(h) => { | ||
| 8833 | + if h.segmentation.enabled { | ||
| 8834 | + seg_frames += 1; | ||
| 8835 | + } | ||
| 8836 | + } | ||
| 8837 | + _ => {} | ||
| 8838 | + } | ||
| 8839 | + } | ||
| 8840 | + } | ||
| 8841 | + if seg_frames == 0 { | ||
| 8842 | + eprintln!("seed {seed}: aomenc wrote no segmentation-enabled frame, skipping attempt"); | ||
| 8843 | + continue; | ||
| 8844 | + } | ||
| 8845 | + let frames = match decode_stream(&stream) { | ||
| 8846 | + Err(e) => { | ||
| 8847 | + let msg = e.to_string(); | ||
| 8848 | + assert!( | ||
| 8849 | + msg.contains("unsupported"), | ||
| 8850 | + "{name} failed outright, not a named refusal (seed {seed}): {msg}" | ||
| 8851 | + ); | ||
| 8852 | + assert!( | ||
| 8853 | + !msg.contains("segment_id symbol"), | ||
| 8854 | + "{name} refused on the segmentation reader itself (seed {seed}): {msg}" | ||
| 8855 | + ); | ||
| 8856 | + named_refusals += 1; | ||
| 8857 | + eprintln!("seed {seed} refusal: {msg}"); | ||
| 8858 | + continue; | ||
| 8859 | + } | ||
| 8860 | + Ok(frames) => frames, | ||
| 8861 | + }; | ||
| 8862 | + let ffmpeg_frames = if bit_depth == 10 { | ||
| 8863 | + ffmpeg_decode_sequence_10bit(&stream, width, height, frame_count) | ||
| 8864 | + } else { | ||
| 8865 | + ffmpeg_decode_sequence(&stream, width, height, frame_count) | ||
| 8866 | + }; | ||
| 8867 | + assert_eq!(frames.len(), frame_count); | ||
| 8868 | + let mismatched = frames | ||
| 8869 | + .iter() | ||
| 8870 | + .zip(&ffmpeg_frames) | ||
| 8871 | + .any(|(got, want)| got.y != want.y || got.u != want.u || got.v != want.v); | ||
| 8872 | + if mismatched && let Ok(path) = std::env::var("EC_AV1_GATE_DUMP") { | ||
| 8873 | + std::fs::write(&path, &stream).expect("writing pinned stream"); | ||
| 8874 | + eprintln!("EC_AV1_GATE_DUMP: wrote mismatching stream (seed {seed}) to {path}"); | ||
| 8875 | + } | ||
| 8876 | + for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { | ||
| 8877 | + assert_eq!(got.y, want.y, "{name} frame {i} luma vs ffmpeg (seed {seed})"); | ||
| 8878 | + assert_eq!(got.u, want.u, "{name} frame {i} U vs ffmpeg (seed {seed})"); | ||
| 8879 | + assert_eq!(got.v, want.v, "{name} frame {i} V vs ffmpeg (seed {seed})"); | ||
| 8880 | + } | ||
| 8881 | + matched += 1; | ||
| 8882 | + } | ||
| 8883 | + assert!( | ||
| 8884 | + matched > 0, | ||
| 8885 | + "{name}: every attempt refused on other capabilities ({named_refusals} refusals); \ | ||
| 8886 | + the gate never exercised segmentation" | ||
| 8887 | + ); | ||
| 8888 | + assert!( | ||
| 8889 | + crate::decode::segment_id_hits() > 0, | ||
| 8890 | + "{name}: {matched} pixel-exact matches but zero segment_id symbols fired -- \ | ||
| 8891 | + the reader is unexercised" | ||
| 8892 | + ); | ||
| 8893 | + assert!( | ||
| 8894 | + crate::decode::segment_ids_seen() >= 2, | ||
| 8895 | + "{name}: {matched} pixel-exact matches but only {} distinct segment id(s) -- \ | ||
| 8896 | + every block landed in one segment, so nothing was actually segmented", | ||
| 8897 | + crate::decode::segment_ids_seen() | ||
| 8898 | + ); | ||
| 8899 | + eprintln!( | ||
| 8900 | + "{name}: {named_refusals} other-capability refusals, {matched} pixel-exact matches \ | ||
| 8901 | + out of {n_attempts}, segment_id_hits={}, distinct segment ids={}", | ||
| 8902 | + crate::decode::segment_id_hits(), | ||
| 8903 | + crate::decode::segment_ids_seen() | ||
| 8904 | + ); | ||
| 8905 | + } | ||
| 8906 | + | ||
| 8907 | + #[test] | ||
| 8908 | + fn a_real_aomenc_stream_with_variance_aq_segmentation_decodes_pixel_exact() { | ||
| 8909 | + run_segmentation_gate( | ||
| 8910 | + "a_real_aomenc_stream_with_variance_aq_segmentation_decodes_pixel_exact", | ||
| 8911 | + "1", | ||
| 8912 | + 8, | ||
| 8913 | + 16, | ||
| 8914 | + &["--end-usage=q", "--cq-level=45"], | ||
| 8915 | + ); | ||
| 8916 | + } | ||
| 8917 | + | ||
| 8918 | + // lane-seg r1 DEFERRED: an `--aq-mode=2` (complexity AQ) twin of the gate | ||
| 8919 | + // above. libaom only switches segmentation on for complexity AQ once the | ||
| 8920 | + // target bits per 64x64 superblock clear `sb64_target_rate >= 256` | ||
| 8921 | + // (`aq_complexity.c:is_sb_aq_enabled`), which needs `--end-usage=vbr`; | ||
| 8922 | + // every VBR recipe tried (cpu-used 0 and 4, 32x32 and 64x64 fixed | ||
| 8923 | + // partitions) makes aomenc pick partition shapes OTHER lanes still refuse | ||
| 8924 | + // (AB below 16x16, SB-level HORZ/VERT, part32 values 4/6), so 10/10 | ||
| 8925 | + // attempts refuse before a pixel is compared. The syntax exercised would | ||
| 8926 | + // be identical to variance AQ's (both features only ever set | ||
| 8927 | + // `SEG_LVL_ALT_Q` via `update_map`), so this is coverage, not capability. | ||
| 8928 | + // Unblocked by the sub-16x16 rect/AB partition lanes. | ||
| 8929 | + | ||
| 8930 | + /// His two AV1 films are both `yuv420p10le`, so every capability lands | ||
| 8931 | + /// with a 10-bit twin (memory `his-av1-library-is-10-bit`). KEY FRAME | ||
| 8932 | + /// ONLY (`frame_count = 1`): a 10-bit INTER frame does not decode | ||
| 8933 | + /// pixel-exact in this crate yet, segmentation or not -- lane-seg r1 ran | ||
| 8934 | + /// this same recipe with `--aq-mode=0` (segmentation off entirely) and it | ||
| 8935 | + /// still mismatched at frame 2, so the 16-frame 10-bit variant would be | ||
| 8936 | + /// gating an unrelated pre-existing gap. Variance AQ segments the key | ||
| 8937 | + /// frame too (`aq_variance.c`, `is_frame_aq_enabled` includes | ||
| 8938 | + /// `frame_is_intra_only`), so this still proves the 10-bit segment-id | ||
| 8939 | + /// read and the per-segment 10-bit dequant. | ||
| 8940 | + #[test] | ||
| 8941 | + fn a_real_aomenc_10bit_stream_with_aq_segmentation_decodes_pixel_exact() { | ||
| 8942 | + run_segmentation_gate( | ||
| 8943 | + "a_real_aomenc_10bit_stream_with_aq_segmentation_decodes_pixel_exact", | ||
| 8944 | + "1", | ||
| 8945 | + 10, | ||
| 8946 | + 1, | ||
| 8947 | + &["--end-usage=q", "--cq-level=45"], | ||
| 8948 | + ); | ||
| 8949 | + } | ||
| 8950 | + | ||
| 8647 | /// lane-tiles r7: the gate stage 2 (r6, commit 8899fc1) shipped without | 8951 | /// lane-tiles r7: the gate stage 2 (r6, commit 8899fc1) shipped without |
| 8648 | /// -- a real >1-tile-column stream with a genuine INTER frame (not just | 8952 | /// -- a real >1-tile-column stream with a genuine INTER frame (not just |
| 8649 | /// the key-frame-only two-column gate above), decoded through | 8953 | /// the key-frame-only two-column gate above), decoded through |