Merge branch 'lane-seg': per-block segment_id (intra/inter readers, seg_id_predicted, map carried to ref slots), SEG_LVL_ALT_Q via block_q_idx(), SEG_LVL_ALT_LF in lf_level; aq-mode gates 8/10-bit 10/10 (verifier PASS, 269/0)
Browse files at this commit6 changed files +827 -21
- crates/ec-av1/src/cdf.rs +13 -0
- crates/ec-av1/src/cdf_state.rs +8 -0
- crates/ec-av1/src/decode.rs +417 -15
- crates/ec-av1/src/refusal_inventory.rs +1 -1
- crates/ec-av1/src/stream.rs +321 -5
- lanes/seg-r1.report.md +67 -0
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+417 -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,353 @@ 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 | + /// How many `seg_id_predicted` symbols were read (temporal update). | ||
| 354 | + static SEG_PRED_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; | ||
| 355 | +} | ||
| 356 | + | ||
| 357 | +/// Installs this frame's segmentation parameters and allocates its map, with | ||
| 358 | +/// `prev` the primary reference frame's saved map (spec | ||
| 359 | +/// `load_previous_segment_ids`; `None` for `PRIMARY_REF_NONE` or an empty | ||
| 360 | +/// slot). Called once per frame, whether or not segmentation is enabled. | ||
| 361 | +pub(crate) fn set_segmentation( | ||
| 362 | + seg: SegmentationParams, | ||
| 363 | + mi_rows: usize, | ||
| 364 | + mi_cols: usize, | ||
| 365 | + prev: Option<&[u8]>, | ||
| 366 | +) { | ||
| 367 | + SEG.with(|s| *s.borrow_mut() = seg); | ||
| 368 | + SEG_MI_DIMS.with(|c| c.set((mi_rows, mi_cols))); | ||
| 369 | + SEG_IDS.with(|m| *m.borrow_mut() = vec![0u8; mi_rows * mi_cols]); | ||
| 370 | + PREV_SEG_IDS.with(|m| { | ||
| 371 | + let mut m = m.borrow_mut(); | ||
| 372 | + m.clear(); | ||
| 373 | + if let Some(p) = prev { | ||
| 374 | + if p.len() == mi_rows * mi_cols { | ||
| 375 | + m.extend_from_slice(p); | ||
| 376 | + } | ||
| 377 | + } | ||
| 378 | + }); | ||
| 379 | + ABOVE_SEG_PRED.with(|m| *m.borrow_mut() = vec![0u8; mi_cols]); | ||
| 380 | + LEFT_SEG_PRED.with(|m| *m.borrow_mut() = vec![0u8; mi_rows]); | ||
| 381 | + CUR_SEGMENT_ID.with(|c| c.set(0)); | ||
| 382 | +} | ||
| 383 | + | ||
| 384 | +/// This frame's decoded segment map, to be stored into whichever reference | ||
| 385 | +/// slots `refresh_frame_flags` names (spec 7.20's `SavedSegmentIds`). | ||
| 386 | +pub(crate) fn take_segment_ids() -> Vec<u8> { | ||
| 387 | + SEG_IDS.with(|m| m.borrow().clone()) | ||
| 388 | +} | ||
| 389 | + | ||
| 390 | +/// How many `segment_id` symbols have been read (never reset per frame). | ||
| 391 | +pub(crate) fn segment_id_hits() -> usize { | ||
| 392 | + SEG_ID_HITS.with(|c| c.get()) | ||
| 393 | +} | ||
| 394 | + | ||
| 395 | +/// How many distinct segment ids any decoded block has carried. | ||
| 396 | +pub(crate) fn segment_ids_seen() -> usize { | ||
| 397 | + SEG_IDS_SEEN.with(|c| c.get().count_ones() as usize) | ||
| 398 | +} | ||
| 399 | + | ||
| 400 | +/// How many `seg_id_predicted` symbols have been read (temporal update). | ||
| 401 | +pub(crate) fn segment_pred_hits() -> usize { | ||
| 402 | + SEG_PRED_HITS.with(|c| c.get()) | ||
| 403 | +} | ||
| 404 | + | ||
| 405 | +/// Resets the segmentation hit counters (gate setup). | ||
| 406 | +pub(crate) fn reset_segment_hits() { | ||
| 407 | + SEG_ID_HITS.with(|c| c.set(0)); | ||
| 408 | + SEG_IDS_SEEN.with(|c| c.set(0)); | ||
| 409 | + SEG_PRED_HITS.with(|c| c.set(0)); | ||
| 410 | +} | ||
| 411 | + | ||
| 412 | +/// `seg_feature_active_idx(segment_id, feature)` for the frame currently | ||
| 413 | +/// being decoded, with its `FeatureData` value. | ||
| 414 | +fn seg_feature(segment_id: usize, feature: usize) -> Option<i32> { | ||
| 415 | + SEG.with(|s| { | ||
| 416 | + let s = s.borrow(); | ||
| 417 | + s.feature_active(segment_id, feature) | ||
| 418 | + .then(|| i32::from(s.feature_data[segment_id][feature])) | ||
| 419 | + }) | ||
| 420 | +} | ||
| 421 | + | ||
| 422 | +/// `get_qindex(ignoreDeltaQ = 0, segment_id)` (spec 7.12.2, libaom | ||
| 423 | +/// `av1_get_qindex`) for the block currently being decoded: the running | ||
| 424 | +/// `CurrentQIndex` shifted by this block's segment's `SEG_LVL_ALT_Q` data. | ||
| 425 | +/// Every dequant call site reads the block's quantizer through here. | ||
| 426 | +fn block_q_idx() -> i32 { | ||
| 427 | + let base = CURRENT_Q_IDX.with(|c| c.get()); | ||
| 428 | + let seg_id = CUR_SEGMENT_ID.with(|c| c.get()) as usize; | ||
| 429 | + match seg_feature(seg_id, SEG_LVL_ALT_Q) { | ||
| 430 | + Some(data) => (base + data).clamp(0, 255), | ||
| 431 | + None => base, | ||
| 432 | + } | ||
| 433 | +} | ||
| 434 | + | ||
| 435 | +/// The segment id recorded for one mi cell of the current frame's map. | ||
| 436 | +fn segment_id_at(mi_r: usize, mi_c: usize) -> u8 { | ||
| 437 | + let (mi_rows, mi_cols) = SEG_MI_DIMS.with(|c| c.get()); | ||
| 438 | + if mi_r >= mi_rows || mi_c >= mi_cols { | ||
| 439 | + return 0; | ||
| 440 | + } | ||
| 441 | + SEG_IDS.with(|m| m.borrow()[mi_r * mi_cols + mi_c]) | ||
| 442 | +} | ||
| 443 | + | ||
| 444 | +/// `set_segment_id`: stamps `id` over every mi cell this block covers. | ||
| 445 | +fn write_segment_id(mi_r: usize, mi_c: usize, w_mi: usize, h_mi: usize, id: u8) { | ||
| 446 | + let (mi_rows, mi_cols) = SEG_MI_DIMS.with(|c| c.get()); | ||
| 447 | + let (x_mis, y_mis) = (w_mi.min(mi_cols.saturating_sub(mi_c)), h_mi.min(mi_rows.saturating_sub(mi_r))); | ||
| 448 | + SEG_IDS.with(|m| { | ||
| 449 | + let mut m = m.borrow_mut(); | ||
| 450 | + for y in 0..y_mis { | ||
| 451 | + for x in 0..x_mis { | ||
| 452 | + m[(mi_r + y) * mi_cols + mi_c + x] = id; | ||
| 453 | + } | ||
| 454 | + } | ||
| 455 | + }); | ||
| 456 | + CUR_SEGMENT_ID.with(|c| c.set(id)); | ||
| 457 | + SEG_IDS_SEEN.with(|c| c.set(c.get() | 1 << id)); | ||
| 458 | +} | ||
| 459 | + | ||
| 460 | +/// `get_predicted_segment_id` (libaom `dec_get_segment_id` over | ||
| 461 | +/// `last_frame_seg_map`): the minimum previous-frame segment id over the | ||
| 462 | +/// block's own mi footprint. `0` when there is no previous map. | ||
| 463 | +fn predicted_segment_id(mi_r: usize, mi_c: usize, w_mi: usize, h_mi: usize) -> u8 { | ||
| 464 | + let (mi_rows, mi_cols) = SEG_MI_DIMS.with(|c| c.get()); | ||
| 465 | + PREV_SEG_IDS.with(|m| { | ||
| 466 | + let m = m.borrow(); | ||
| 467 | + if m.is_empty() { | ||
| 468 | + return 0; | ||
| 469 | + } | ||
| 470 | + let (x_mis, y_mis) = (w_mi.min(mi_cols.saturating_sub(mi_c)), h_mi.min(mi_rows.saturating_sub(mi_r))); | ||
| 471 | + let mut id = u8::MAX; | ||
| 472 | + for y in 0..y_mis { | ||
| 473 | + for x in 0..x_mis { | ||
| 474 | + id = id.min(m[(mi_r + y) * mi_cols + mi_c + x]); | ||
| 475 | + } | ||
| 476 | + } | ||
| 477 | + if id == u8::MAX { 0 } else { id } | ||
| 478 | + }) | ||
| 479 | +} | ||
| 480 | + | ||
| 481 | +/// `av1_neg_deinterleave` (libaom `decodemv.c:258`). | ||
| 482 | +fn neg_deinterleave(diff: i32, reference: i32, max: i32) -> i32 { | ||
| 483 | + if reference == 0 { | ||
| 484 | + return diff; | ||
| 485 | + } | ||
| 486 | + if reference >= max - 1 { | ||
| 487 | + return max - diff - 1; | ||
| 488 | + } | ||
| 489 | + let half = if 2 * reference < max { | ||
| 490 | + diff <= 2 * reference | ||
| 491 | + } else { | ||
| 492 | + diff <= 2 * (max - reference - 1) | ||
| 493 | + }; | ||
| 494 | + if half { | ||
| 495 | + return if diff & 1 != 0 { | ||
| 496 | + reference + ((diff + 1) >> 1) | ||
| 497 | + } else { | ||
| 498 | + reference - (diff >> 1) | ||
| 499 | + }; | ||
| 500 | + } | ||
| 501 | + if 2 * reference < max { diff } else { max - (diff + 1) } | ||
| 502 | +} | ||
| 503 | + | ||
| 504 | +/// `av1_get_spatial_seg_pred`: `(prediction, cdf_index)` from the current | ||
| 505 | +/// frame's already-decoded above/left/above-left neighbours. | ||
| 506 | +fn spatial_seg_pred(mi_r: usize, mi_c: usize) -> (i32, usize) { | ||
| 507 | + let (row0, col0) = SEG_TILE_ORIGIN.with(|c| c.get()); | ||
| 508 | + let (avail_u, avail_l) = (mi_r > row0, mi_c > col0); | ||
| 509 | + let prev_ul = (avail_u && avail_l).then(|| i32::from(segment_id_at(mi_r - 1, mi_c - 1))); | ||
| 510 | + let prev_u = avail_u.then(|| i32::from(segment_id_at(mi_r - 1, mi_c))); | ||
| 511 | + let prev_l = avail_l.then(|| i32::from(segment_id_at(mi_r, mi_c - 1))); | ||
| 512 | + let ctx = match prev_ul { | ||
| 513 | + None => 0, | ||
| 514 | + Some(ul) if Some(ul) == prev_u && Some(ul) == prev_l => 2, | ||
| 515 | + Some(ul) if Some(ul) == prev_u || Some(ul) == prev_l || prev_u == prev_l => 1, | ||
| 516 | + Some(_) => 0, | ||
| 517 | + }; | ||
| 518 | + let pred = match (prev_u, prev_l) { | ||
| 519 | + (None, l) => l.unwrap_or(0), | ||
| 520 | + (Some(u), None) => u, | ||
| 521 | + (Some(u), Some(l)) => { | ||
| 522 | + if prev_ul == Some(u) { | ||
| 523 | + u | ||
| 524 | + } else { | ||
| 525 | + l | ||
| 526 | + } | ||
| 527 | + } | ||
| 528 | + }; | ||
| 529 | + (pred, ctx) | ||
| 530 | +} | ||
| 531 | + | ||
| 532 | +/// `read_segment_id` (libaom `decodemv.c:280`): the spatially predicted id | ||
| 533 | +/// when `skip`, otherwise one `segment_id` symbol de-interleaved against | ||
| 534 | +/// that prediction. Does NOT write the map -- callers do. | ||
| 535 | +fn read_segment_id(dec: &mut SymbolDecoder, cdfs: &mut Cdfs, mi_r: usize, mi_c: usize, skip: bool) -> u8 { | ||
| 536 | + let (pred, ctx) = spatial_seg_pred(mi_r, mi_c); | ||
| 537 | + if skip { | ||
| 538 | + return pred as u8; | ||
| 539 | + } | ||
| 540 | + let coded = dec.symbol(&mut cdfs.segment_id[ctx]) as i32; | ||
| 541 | + SEG_ID_HITS.with(|c| c.set(c.get() + 1)); | ||
| 542 | + let last_active = SEG.with(|s| i32::from(s.borrow().last_active_seg_id)); | ||
| 543 | + neg_deinterleave(coded, pred, last_active + 1).clamp(0, last_active) as u8 | ||
| 544 | +} | ||
| 545 | + | ||
| 546 | +/// `read_intra_segment_id` (spec 5.11.7): called twice per intra-frame block | ||
| 547 | +/// -- once before `skip` when `SegIdPreSkip`, once after it otherwise (the | ||
| 548 | +/// caller decides which by passing `pre_skip`). | ||
| 549 | +fn intra_segment_id( | ||
| 550 | + dec: &mut SymbolDecoder, | ||
| 551 | + cdfs: &mut Cdfs, | ||
| 552 | + mi_r: usize, | ||
| 553 | + mi_c: usize, | ||
| 554 | + w_mi: usize, | ||
| 555 | + h_mi: usize, | ||
| 556 | + skip: bool, | ||
| 557 | +) { | ||
| 558 | + if !SEG.with(|s| s.borrow().enabled) { | ||
| 559 | + return; | ||
| 560 | + } | ||
| 561 | + let id = read_segment_id(dec, cdfs, mi_r, mi_c, skip); | ||
| 562 | + write_segment_id(mi_r, mi_c, w_mi, h_mi, id); | ||
| 563 | +} | ||
| 564 | + | ||
| 565 | +/// Whether this frame's intra blocks read their `segment_id` before `skip` | ||
| 566 | +/// (`SegIdPreSkip`); `false` also when segmentation is off. | ||
| 567 | +fn seg_id_pre_skip() -> bool { | ||
| 568 | + SEG.with(|s| { | ||
| 569 | + let s = s.borrow(); | ||
| 570 | + s.enabled && s.seg_id_pre_skip | ||
| 571 | + }) | ||
| 572 | +} | ||
| 573 | + | ||
| 574 | +/// `read_inter_segment_id(preskip)` (spec 5.11.9, libaom `decodemv.c:352`). | ||
| 575 | +/// Called at both spec positions of an inter frame's block: `pre_skip = true` | ||
| 576 | +/// before `skip_mode`/`skip`, and `pre_skip = false` after them. | ||
| 577 | +fn inter_segment_id( | ||
| 578 | + dec: &mut SymbolDecoder, | ||
| 579 | + cdfs: &mut Cdfs, | ||
| 580 | + mi_r: usize, | ||
| 581 | + mi_c: usize, | ||
| 582 | + w_mi: usize, | ||
| 583 | + h_mi: usize, | ||
| 584 | + skip: bool, | ||
| 585 | + pre_skip: bool, | ||
| 586 | +) { | ||
| 587 | + let (enabled, update_map, temporal_update, pre_skip_frame) = SEG.with(|s| { | ||
| 588 | + let s = s.borrow(); | ||
| 589 | + (s.enabled, s.update_map, s.temporal_update, s.seg_id_pre_skip) | ||
| 590 | + }); | ||
| 591 | + if !enabled { | ||
| 592 | + return; | ||
| 593 | + } | ||
| 594 | + if !update_map { | ||
| 595 | + // The map is inherited wholesale from the previous frame. | ||
| 596 | + let id = predicted_segment_id(mi_r, mi_c, w_mi, h_mi); | ||
| 597 | + write_segment_id(mi_r, mi_c, w_mi, h_mi, id); | ||
| 598 | + return; | ||
| 599 | + } | ||
| 600 | + if pre_skip && !pre_skip_frame { | ||
| 601 | + return; | ||
| 602 | + } | ||
| 603 | + if !pre_skip && skip { | ||
| 604 | + // libaom: a skipped block never codes `seg_id_predicted`, and its | ||
| 605 | + // segment id is the spatial prediction. | ||
| 606 | + stamp_seg_pred(mi_r, mi_c, w_mi, h_mi, 0); | ||
| 607 | + let id = read_segment_id(dec, cdfs, mi_r, mi_c, true); | ||
| 608 | + write_segment_id(mi_r, mi_c, w_mi, h_mi, id); | ||
| 609 | + return; | ||
| 610 | + } | ||
| 611 | + let id = if temporal_update { | ||
| 612 | + let ctx = seg_pred_ctx(mi_r, mi_c); | ||
| 613 | + let predicted = dec.symbol(&mut cdfs.segment_pred[ctx]) != 0; | ||
| 614 | + SEG_PRED_HITS.with(|c| c.set(c.get() + 1)); | ||
| 615 | + stamp_seg_pred(mi_r, mi_c, w_mi, h_mi, u8::from(predicted)); | ||
| 616 | + if predicted { | ||
| 617 | + predicted_segment_id(mi_r, mi_c, w_mi, h_mi) | ||
| 618 | + } else { | ||
| 619 | + read_segment_id(dec, cdfs, mi_r, mi_c, false) | ||
| 620 | + } | ||
| 621 | + } else { | ||
| 622 | + read_segment_id(dec, cdfs, mi_r, mi_c, false) | ||
| 623 | + }; | ||
| 624 | + write_segment_id(mi_r, mi_c, w_mi, h_mi, id); | ||
| 625 | +} | ||
| 626 | + | ||
| 627 | +/// `av1_get_pred_context_seg_id`: above + left `seg_id_predicted` flags. | ||
| 628 | +fn seg_pred_ctx(mi_r: usize, mi_c: usize) -> usize { | ||
| 629 | + // libaom reads the flag off `above_mbmi`/`left_mbmi`, which are NULL (and | ||
| 630 | + // so contribute 0) outside the tile -- the availability check here is | ||
| 631 | + // what keeps a previous tile's stamps out of this tile's context. | ||
| 632 | + let (row0, col0) = SEG_TILE_ORIGIN.with(|c| c.get()); | ||
| 633 | + let above = if mi_r > row0 { | ||
| 634 | + ABOVE_SEG_PRED.with(|m| m.borrow().get(mi_c).copied().unwrap_or(0)) | ||
| 635 | + } else { | ||
| 636 | + 0 | ||
| 637 | + }; | ||
| 638 | + let left = if mi_c > col0 { | ||
| 639 | + LEFT_SEG_PRED.with(|m| m.borrow().get(mi_r).copied().unwrap_or(0)) | ||
| 640 | + } else { | ||
| 641 | + 0 | ||
| 642 | + }; | ||
| 643 | + usize::from(above + left) | ||
| 644 | +} | ||
| 645 | + | ||
| 646 | +/// Spec 5.11.9's `AboveSegPredContext`/`LeftSegPredContext` update. | ||
| 647 | +fn stamp_seg_pred(mi_r: usize, mi_c: usize, w_mi: usize, h_mi: usize, value: u8) { | ||
| 648 | + ABOVE_SEG_PRED.with(|m| { | ||
| 649 | + let mut m = m.borrow_mut(); | ||
| 650 | + for i in 0..w_mi { | ||
| 651 | + if let Some(cell) = m.get_mut(mi_c + i) { | ||
| 652 | + *cell = value; | ||
| 653 | + } | ||
| 654 | + } | ||
| 655 | + }); | ||
| 656 | + LEFT_SEG_PRED.with(|m| { | ||
| 657 | + let mut m = m.borrow_mut(); | ||
| 658 | + for i in 0..h_mi { | ||
| 659 | + if let Some(cell) = m.get_mut(mi_r + i) { | ||
| 660 | + *cell = value; | ||
| 661 | + } | ||
| 662 | + } | ||
| 663 | + }); | ||
| 664 | +} | ||
| 665 | + | ||
| 318 | // How many key-frame luma blocks resolved `smooth_neighbor` (spec | 666 | // 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 | 667 | // `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 | 668 | // counter, proving a stream actually put a directional block next to a |
| @@ -1936,6 +2284,9 @@ impl Neighbours { | |||
| 1936 | /// regardless of tile ([`Self::start_row`], already called once per SB | 2284 | /// regardless of tile ([`Self::start_row`], already called once per SB |
| 1937 | /// row by every caller) so it needs no tile-specific reset here. | 2285 | /// row by every caller) so it needs no tile-specific reset here. |
| 1938 | fn start_tile(&mut self, row0_mi: usize, col0_mi: usize, col1_mi: usize) { | 2286 | fn start_tile(&mut self, row0_mi: usize, col0_mi: usize, col1_mi: usize) { |
| 2287 | + // lane-seg: `AvailU`/`AvailL` for the segment-id neighbour prediction | ||
| 2288 | + // are tile-relative, same as every other neighbour lookup here. | ||
| 2289 | + SEG_TILE_ORIGIN.with(|c| c.set((row0_mi, col0_mi))); | ||
| 1939 | self.tile_row0_mi = row0_mi; | 2290 | self.tile_row0_mi = row0_mi; |
| 1940 | self.tile_col0_mi = col0_mi; | 2291 | self.tile_col0_mi = col0_mi; |
| 1941 | let end = col1_mi.min(self.above.len()); | 2292 | let end = col1_mi.min(self.above.len()); |
| @@ -3010,8 +3361,16 @@ fn read_intra_mode_rect( | |||
| 3010 | } | 3361 | } |
| 3011 | }; | 3362 | }; |
| 3012 | } | 3363 | } |
| 3364 | + // Same `intra_segment_id` placement as the square reader above. | ||
| 3365 | + let (seg_w_mi, seg_h_mi) = (bw / 4, bh / 4); | ||
| 3366 | + if seg_id_pre_skip() { | ||
| 3367 | + intra_segment_id(dec, cdfs, mi_r, mi_c, seg_w_mi, seg_h_mi, false); | ||
| 3368 | + } | ||
| 3013 | let skip = dec.symbol(&mut cdfs.skip[skip_ctx]) != 0; | 3369 | let skip = dec.symbol(&mut cdfs.skip[skip_ctx]) != 0; |
| 3014 | istep!("skip", skip as i32); | 3370 | istep!("skip", skip as i32); |
| 3371 | + if !seg_id_pre_skip() { | ||
| 3372 | + intra_segment_id(dec, cdfs, mi_r, mi_c, seg_w_mi, seg_h_mi, skip); | ||
| 3373 | + } | ||
| 3015 | maybe_read_cdef_idx(dec, mi_r, mi_c, skip); | 3374 | maybe_read_cdef_idx(dec, mi_r, mi_c, skip); |
| 3016 | istep!("cdef", 0); | 3375 | istep!("cdef", 0); |
| 3017 | // A HORZ/VERT rect strip is never the whole superblock (`bw`/`bh` never | 3376 | // A HORZ/VERT rect strip is never the whole superblock (`bw`/`bh` never |
| @@ -3366,7 +3725,7 @@ fn decode_block_rect( | |||
| 3366 | bw, | 3725 | bw, |
| 3367 | bh, | 3726 | bh, |
| 3368 | crate::decode::bit_depth(), | 3727 | crate::decode::bit_depth(), |
| 3369 | - CURRENT_Q_IDX.with(|c| c.get()), | 3728 | + block_q_idx(), |
| 3370 | plane_q_delta(0).0, | 3729 | plane_q_delta(0).0, |
| 3371 | plane_q_delta(0).1, | 3730 | plane_q_delta(0).1, |
| 3372 | luma_tx_type, | 3731 | luma_tx_type, |
| @@ -3407,7 +3766,7 @@ fn decode_block_rect( | |||
| 3407 | chroma_w, | 3766 | chroma_w, |
| 3408 | chroma_h, | 3767 | chroma_h, |
| 3409 | crate::decode::bit_depth(), | 3768 | crate::decode::bit_depth(), |
| 3410 | - CURRENT_Q_IDX.with(|c| c.get()), | 3769 | + block_q_idx(), |
| 3411 | plane_q_delta(1).0, | 3770 | plane_q_delta(1).0, |
| 3412 | plane_q_delta(1).1, | 3771 | plane_q_delta(1).1, |
| 3413 | u_tx_type, | 3772 | u_tx_type, |
| @@ -3447,7 +3806,7 @@ fn decode_block_rect( | |||
| 3447 | chroma_w, | 3806 | chroma_w, |
| 3448 | chroma_h, | 3807 | chroma_h, |
| 3449 | crate::decode::bit_depth(), | 3808 | crate::decode::bit_depth(), |
| 3450 | - CURRENT_Q_IDX.with(|c| c.get()), | 3809 | + block_q_idx(), |
| 3451 | plane_q_delta(2).0, | 3810 | plane_q_delta(2).0, |
| 3452 | plane_q_delta(2).1, | 3811 | plane_q_delta(2).1, |
| 3453 | v_tx_type, | 3812 | v_tx_type, |
| @@ -3806,7 +4165,7 @@ fn decode_block_rect64( | |||
| 3806 | luma_levels[row * bw..][..32].copy_from_slice(&luma_corner[row * 32..][..32]); | 4165 | luma_levels[row * bw..][..32].copy_from_slice(&luma_corner[row * 32..][..32]); |
| 3807 | } | 4166 | } |
| 3808 | { | 4167 | { |
| 3809 | - let cur = CURRENT_Q_IDX.with(|c| c.get()); | 4168 | + let cur = block_q_idx(); |
| 3810 | if cur != i32::from(base_q_idx) { | 4169 | if cur != i32::from(base_q_idx) { |
| 3811 | RECT64_QIDX_DRIFT_HITS.with(|c| c.set(c.get() + 1)); | 4170 | RECT64_QIDX_DRIFT_HITS.with(|c| c.set(c.get() + 1)); |
| 3812 | } | 4171 | } |
| @@ -3821,7 +4180,7 @@ fn decode_block_rect64( | |||
| 3821 | bw, | 4180 | bw, |
| 3822 | bh, | 4181 | bh, |
| 3823 | crate::decode::bit_depth(), | 4182 | crate::decode::bit_depth(), |
| 3824 | - CURRENT_Q_IDX.with(|c| c.get()), | 4183 | + block_q_idx(), |
| 3825 | plane_q_delta(0).0, | 4184 | plane_q_delta(0).0, |
| 3826 | plane_q_delta(0).1, | 4185 | plane_q_delta(0).1, |
| 3827 | luma_tx_type, | 4186 | luma_tx_type, |
| @@ -3878,7 +4237,7 @@ fn decode_block_rect64( | |||
| 3878 | eprintln!("EC_COEFF_VAL plane=1 row={mi_r} col={mi_c} rng={rng}"); | 4237 | eprintln!("EC_COEFF_VAL plane=1 row={mi_r} col={mi_c} rng={rng}"); |
| 3879 | } | 4238 | } |
| 3880 | { | 4239 | { |
| 3881 | - let cur = CURRENT_Q_IDX.with(|c| c.get()); | 4240 | + let cur = block_q_idx(); |
| 3882 | if cur != i32::from(base_q_idx) { | 4241 | if cur != i32::from(base_q_idx) { |
| 3883 | RECT64_QIDX_DRIFT_HITS.with(|c| c.set(c.get() + 1)); | 4242 | RECT64_QIDX_DRIFT_HITS.with(|c| c.set(c.get() + 1)); |
| 3884 | } | 4243 | } |
| @@ -3893,7 +4252,7 @@ fn decode_block_rect64( | |||
| 3893 | chroma_w, | 4252 | chroma_w, |
| 3894 | chroma_h, | 4253 | chroma_h, |
| 3895 | crate::decode::bit_depth(), | 4254 | crate::decode::bit_depth(), |
| 3896 | - CURRENT_Q_IDX.with(|c| c.get()), | 4255 | + block_q_idx(), |
| 3897 | plane_q_delta(1).0, | 4256 | plane_q_delta(1).0, |
| 3898 | plane_q_delta(1).1, | 4257 | plane_q_delta(1).1, |
| 3899 | u_tx_type, | 4258 | u_tx_type, |
| @@ -3932,7 +4291,7 @@ fn decode_block_rect64( | |||
| 3932 | eprintln!("EC_COEFF_VAL plane=2 row={mi_r} col={mi_c} rng={rng}"); | 4291 | eprintln!("EC_COEFF_VAL plane=2 row={mi_r} col={mi_c} rng={rng}"); |
| 3933 | } | 4292 | } |
| 3934 | { | 4293 | { |
| 3935 | - let cur = CURRENT_Q_IDX.with(|c| c.get()); | 4294 | + let cur = block_q_idx(); |
| 3936 | if cur != i32::from(base_q_idx) { | 4295 | if cur != i32::from(base_q_idx) { |
| 3937 | RECT64_QIDX_DRIFT_HITS.with(|c| c.set(c.get() + 1)); | 4296 | RECT64_QIDX_DRIFT_HITS.with(|c| c.set(c.get() + 1)); |
| 3938 | } | 4297 | } |
| @@ -3947,7 +4306,7 @@ fn decode_block_rect64( | |||
| 3947 | chroma_w, | 4306 | chroma_w, |
| 3948 | chroma_h, | 4307 | chroma_h, |
| 3949 | crate::decode::bit_depth(), | 4308 | crate::decode::bit_depth(), |
| 3950 | - CURRENT_Q_IDX.with(|c| c.get()), | 4309 | + block_q_idx(), |
| 3951 | plane_q_delta(2).0, | 4310 | plane_q_delta(2).0, |
| 3952 | plane_q_delta(2).1, | 4311 | plane_q_delta(2).1, |
| 3953 | v_tx_type, | 4312 | v_tx_type, |
| @@ -4045,11 +4404,20 @@ fn read_intra_mode( | |||
| 4045 | } | 4404 | } |
| 4046 | }; | 4405 | }; |
| 4047 | } | 4406 | } |
| 4407 | + // spec 5.11.6 `intra_frame_mode_info`: `intra_segment_id` is read BEFORE | ||
| 4408 | + // `skip` when `SegIdPreSkip`, and after it otherwise. | ||
| 4409 | + let (seg_w_mi, seg_h_mi) = (side / 4, side / 4); | ||
| 4410 | + if seg_id_pre_skip() { | ||
| 4411 | + intra_segment_id(dec, cdfs, mi_r, mi_c, seg_w_mi, seg_h_mi, false); | ||
| 4412 | + } | ||
| 4048 | let skip = dec.symbol(&mut cdfs.skip[skip_ctx]) != 0; | 4413 | let skip = dec.symbol(&mut cdfs.skip[skip_ctx]) != 0; |
| 4049 | if trace { | 4414 | if trace { |
| 4050 | eprintln!("TRACE skip value={}", skip as i32); | 4415 | eprintln!("TRACE skip value={}", skip as i32); |
| 4051 | } | 4416 | } |
| 4052 | istep!("skip", skip as i32); | 4417 | istep!("skip", skip as i32); |
| 4418 | + if !seg_id_pre_skip() { | ||
| 4419 | + intra_segment_id(dec, cdfs, mi_r, mi_c, seg_w_mi, seg_h_mi, skip); | ||
| 4420 | + } | ||
| 4053 | // spec order (see the comment below on `read_intrabc_info`): `skip`, | 4421 | // spec order (see the comment below on `read_intrabc_info`): `skip`, |
| 4054 | // `segment_id`, `cdef`, `delta_q` -- `cdef` lands right here. | 4422 | // `segment_id`, `cdef`, `delta_q` -- `cdef` lands right here. |
| 4055 | maybe_read_cdef_idx(dec, mi_r, mi_c, skip); | 4423 | maybe_read_cdef_idx(dec, mi_r, mi_c, skip); |
| @@ -4721,7 +5089,7 @@ fn read_plane( | |||
| 4721 | full | 5089 | full |
| 4722 | }; | 5090 | }; |
| 4723 | let (dc_delta, ac_delta) = plane_q_delta(plane_idx); | 5091 | let (dc_delta, ac_delta) = plane_q_delta(plane_idx); |
| 4724 | - let residual = dequant_and_inverse_typed(&levels, side, bit_depth(), CURRENT_Q_IDX.with(|c| c.get()), dc_delta, ac_delta, tx_type); | 5092 | + let residual = dequant_and_inverse_typed(&levels, side, bit_depth(), block_q_idx(), dc_delta, ac_delta, tx_type); |
| 4725 | if std::env::var_os("EC_AV1_TRACE").is_some() { | 5093 | if std::env::var_os("EC_AV1_TRACE").is_some() { |
| 4726 | eprintln!( | 5094 | eprintln!( |
| 4727 | "TRACE dequant plane={plane_idx} base_q_idx={base_q_idx} tx_type={tx_type:?} side={side} levels={levels:?} residual={residual:?}", | 5095 | "TRACE dequant plane={plane_idx} base_q_idx={base_q_idx} tx_type={tx_type:?} side={side} levels={levels:?} residual={residual:?}", |
| @@ -5835,7 +6203,14 @@ fn unit_log2(px: u32) -> i32 { | |||
| 5835 | /// the flat `mode_deltas[1]` this used before was a `LAST_FRAME`-reachable | 6203 | /// the flat `mode_deltas[1]` this used before was a `LAST_FRAME`-reachable |
| 5836 | /// wrong delta on every `GLOBALMV` block, masked while any `GOLDEN_FRAME` | 6204 | /// wrong delta on every `GLOBALMV` block, masked while any `GOLDEN_FRAME` |
| 5837 | /// block anywhere aborted the whole stream's decode before comparison). | 6205 | /// block anywhere aborted the whole stream's decode before comparison). |
| 5838 | -fn lf_level(lf: &LoopFilterParams, plane_idx: usize, dir: usize, ref_frame: i8, delta_lf: i32) -> i32 { | 6206 | +fn lf_level( |
| 6207 | + lf: &LoopFilterParams, | ||
| 6208 | + plane_idx: usize, | ||
| 6209 | + dir: usize, | ||
| 6210 | + ref_frame: i8, | ||
| 6211 | + delta_lf: i32, | ||
| 6212 | + segment_id: u8, | ||
| 6213 | +) -> i32 { | ||
| 5839 | let base = i32::from(if plane_idx == 0 { | 6214 | let base = i32::from(if plane_idx == 0 { |
| 5840 | lf.level[dir] | 6215 | lf.level[dir] |
| 5841 | } else if plane_idx == 1 { | 6216 | } else if plane_idx == 1 { |
| @@ -5847,6 +6222,16 @@ fn lf_level(lf: &LoopFilterParams, plane_idx: usize, dir: usize, ref_frame: i8, | |||
| 5847 | // ref/mode delta scaling below, and regardless of `delta_enabled` | 6222 | // ref/mode delta scaling below, and regardless of `delta_enabled` |
| 5848 | // (that flag only gates `ref_deltas`/`mode_deltas`). | 6223 | // (that flag only gates `ref_deltas`/`mode_deltas`). |
| 5849 | let base = (base + delta_lf).clamp(0, 63); | 6224 | let base = (base + delta_lf).clamp(0, 63); |
| 6225 | + // lane-seg, spec 7.14.4 / libaom `av1_loop_filter_frame_init`: this | ||
| 6226 | + // block's segment's own `SEG_LVL_ALT_LF_*` delta applies after the | ||
| 6227 | + // per-superblock `DeltaLF` term and before the ref/mode deltas below. | ||
| 6228 | + // Feature index: `SEG_LVL_ALT_LF_Y_V + dir` for luma, `_U`/`_V` (3/4) | ||
| 6229 | + // for chroma. | ||
| 6230 | + let feature = if plane_idx == 0 { SEG_LVL_ALT_LF_Y_V + dir } else { plane_idx + 2 }; | ||
| 6231 | + let base = match seg_feature(usize::from(segment_id), feature) { | ||
| 6232 | + Some(data) => (base + data).clamp(0, 63), | ||
| 6233 | + None => base, | ||
| 6234 | + }; | ||
| 5850 | if !lf.delta_enabled { | 6235 | if !lf.delta_enabled { |
| 5851 | return base; | 6236 | return base; |
| 5852 | } | 6237 | } |
| @@ -5902,8 +6287,15 @@ fn edge_params( | |||
| 5902 | let cur_delta_lf = i32::from(n.delta_lf_grid[mi_r * n.skip_grid_cols_mi + mi_c][delta_idx]); | 6287 | let cur_delta_lf = i32::from(n.delta_lf_grid[mi_r * n.skip_grid_cols_mi + mi_c][delta_idx]); |
| 5903 | let pv_delta_lf = i32::from(n.delta_lf_grid[pv_mi_r * n.skip_grid_cols_mi + pv_mi_c][delta_idx]); | 6288 | let pv_delta_lf = i32::from(n.delta_lf_grid[pv_mi_r * n.skip_grid_cols_mi + pv_mi_c][delta_idx]); |
| 5904 | 6289 | ||
| 5905 | - let cur_level = lf_level(lf, plane_idx, dir, cur_ref, cur_delta_lf); | 6290 | + let cur_level = lf_level(lf, plane_idx, dir, cur_ref, cur_delta_lf, segment_id_at(mi_r, mi_c)); |
| 5906 | - let pv_level = lf_level(lf, plane_idx, dir, pv_ref, pv_delta_lf); | 6291 | + let pv_level = lf_level( |
| 6292 | + lf, | ||
| 6293 | + plane_idx, | ||
| 6294 | + dir, | ||
| 6295 | + pv_ref, | ||
| 6296 | + pv_delta_lf, | ||
| 6297 | + segment_id_at(pv_mi_r, pv_mi_c), | ||
| 6298 | + ); | ||
| 5907 | if cur_level == 0 && pv_level == 0 { | 6299 | if cur_level == 0 && pv_level == 0 { |
| 5908 | return None; | 6300 | return None; |
| 5909 | } | 6301 | } |
| @@ -7814,7 +8206,7 @@ fn read_inter_plane( | |||
| 7814 | None, | 8206 | None, |
| 7815 | )?; | 8207 | )?; |
| 7816 | let (dc_delta, ac_delta) = plane_q_delta(plane_idx); | 8208 | let (dc_delta, ac_delta) = plane_q_delta(plane_idx); |
| 7817 | - let residual = dequant_and_inverse_typed(&grid, side, bit_depth(), CURRENT_Q_IDX.with(|c| c.get()), dc_delta, ac_delta, tx_type); | 8209 | + let residual = dequant_and_inverse_typed(&grid, side, bit_depth(), block_q_idx(), dc_delta, ac_delta, tx_type); |
| 7818 | plane.reconstruct_mc(x, y, side, prediction, &residual); | 8210 | plane.reconstruct_mc(x, y, side, prediction, &residual); |
| 7819 | Ok((grid, tx_type)) | 8211 | Ok((grid, tx_type)) |
| 7820 | } | 8212 | } |
| @@ -8898,6 +9290,11 @@ fn decode_inter_block( | |||
| 8898 | dec.debug_bitpos() | 9290 | dec.debug_bitpos() |
| 8899 | ); | 9291 | ); |
| 8900 | } | 9292 | } |
| 9293 | + // spec 5.11.5 `inter_frame_mode_info`: `inter_segment_id(1)` before | ||
| 9294 | + // `skip_mode`/`skip`, `inter_segment_id(0)` after them. | ||
| 9295 | + let (seg_mi_r, seg_mi_c) = (r * SUB_MI as usize, c * SUB_MI as usize); | ||
| 9296 | + let (seg_w_mi, seg_h_mi) = (write_w / 4, write_h / 4); | ||
| 9297 | + inter_segment_id(dec, cdfs, seg_mi_r, seg_mi_c, seg_w_mi, seg_h_mi, false, true); | ||
| 8901 | let skip_mode = skip_mode_present && dec.symbol(&mut cdfs.skip_mode[skip_mode_ctx]) == 1; | 9298 | let skip_mode = skip_mode_present && dec.symbol(&mut cdfs.skip_mode[skip_mode_ctx]) == 1; |
| 8902 | if std::env::var_os("EC_AV1_SKIPMODE_DUMP").is_some() { | 9299 | if std::env::var_os("EC_AV1_SKIPMODE_DUMP").is_some() { |
| 8903 | eprintln!( | 9300 | eprintln!( |
| @@ -8911,6 +9308,7 @@ fn decode_inter_block( | |||
| 8911 | 9308 | ||
| 8912 | let skip_ctx = usize::from(neighbours.above_skip[c]) + usize::from(neighbours.left_skip[r]); | 9309 | let skip_ctx = usize::from(neighbours.above_skip[c]) + usize::from(neighbours.left_skip[r]); |
| 8913 | let skip = skip_mode || dec.symbol(&mut cdfs.skip[skip_ctx]) == 1; | 9310 | let skip = skip_mode || dec.symbol(&mut cdfs.skip[skip_ctx]) == 1; |
| 9311 | + inter_segment_id(dec, cdfs, seg_mi_r, seg_mi_c, seg_w_mi, seg_h_mi, skip, false); | ||
| 8914 | maybe_read_cdef_idx(dec, r * SUB_MI as usize, c * SUB_MI as usize, skip); | 9312 | maybe_read_cdef_idx(dec, r * SUB_MI as usize, c * SUB_MI as usize, skip); |
| 8915 | maybe_read_delta_q(dec, cdfs, r * SUB_MI as usize, c * SUB_MI as usize, side == 64, skip); | 9313 | maybe_read_delta_q(dec, cdfs, r * SUB_MI as usize, c * SUB_MI as usize, side == 64, skip); |
| 8916 | maybe_read_delta_lf(dec, cdfs, r * SUB_MI as usize, c * SUB_MI as usize, side == 64, skip); | 9314 | maybe_read_delta_lf(dec, cdfs, r * SUB_MI as usize, c * SUB_MI as usize, side == 64, skip); |
| @@ -10748,6 +11146,9 @@ fn decode_inter_block8( | |||
| 10748 | } | 11146 | } |
| 10749 | let skip_mode_ctx = | 11147 | let skip_mode_ctx = |
| 10750 | usize::from(neighbours.above_skip_mode[c]) + usize::from(neighbours.left_skip_mode[r]); | 11148 | usize::from(neighbours.above_skip_mode[c]) + usize::from(neighbours.left_skip_mode[r]); |
| 11149 | + // Same two `inter_segment_id` positions as [`decode_inter_block`]; this | ||
| 11150 | + // leaf is always `BLOCK_8X8`, i.e. 2x2 mi. | ||
| 11151 | + inter_segment_id(dec, cdfs, leaf_mi.0, leaf_mi.1, 2, 2, false, true); | ||
| 10751 | let skip_mode = skip_mode_present && dec.symbol(&mut cdfs.skip_mode[skip_mode_ctx]) == 1; | 11152 | let skip_mode = skip_mode_present && dec.symbol(&mut cdfs.skip_mode[skip_mode_ctx]) == 1; |
| 10752 | if skip_mode { | 11153 | if skip_mode { |
| 10753 | SKIP_MODE_HITS.with(|c| c.set(c.get() + 1)); | 11154 | SKIP_MODE_HITS.with(|c| c.set(c.get() + 1)); |
| @@ -10755,6 +11156,7 @@ fn decode_inter_block8( | |||
| 10755 | 11156 | ||
| 10756 | let skip_ctx = usize::from(above_skip) + usize::from(left_skip); | 11157 | let skip_ctx = usize::from(above_skip) + usize::from(left_skip); |
| 10757 | let skip = skip_mode || dec.symbol(&mut cdfs.skip[skip_ctx]) == 1; | 11158 | let skip = skip_mode || dec.symbol(&mut cdfs.skip[skip_ctx]) == 1; |
| 11159 | + inter_segment_id(dec, cdfs, leaf_mi.0, leaf_mi.1, 2, 2, skip, false); | ||
| 10758 | maybe_read_cdef_idx(dec, leaf_mi.0, leaf_mi.1, skip); | 11160 | maybe_read_cdef_idx(dec, leaf_mi.0, leaf_mi.1, skip); |
| 10759 | // Always a `BLOCK_8X8` leaf here, never the whole superblock. | 11161 | // Always a `BLOCK_8X8` leaf here, never the whole superblock. |
| 10760 | maybe_read_delta_q(dec, cdfs, leaf_mi.0, leaf_mi.1, false, skip); | 11162 | maybe_read_delta_q(dec, cdfs, leaf_mi.0, leaf_mi.1, false, skip); |
crates/ec-av1/src/refusal_inventory.rsmodified+1 -1
| @@ -54,7 +54,7 @@ const REFUSALS: &[&str] = &[ | |||
| 54 | "a frame OBU with no tile group", | 54 | "a frame OBU with no tile group", |
| 55 | "a frame naming primary_ref_frame at a reference slot with no saved CDF state", | 55 | "a frame naming primary_ref_frame at a reference slot with no saved CDF state", |
| 56 | "a frame with no mode-info grid", | 56 | "a frame with no mode-info grid", |
| 57 | - "a frame with segmentation enabled (this decoder never reads a per-block segment_id symbol)", | 57 | + "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)", |
| 58 | "a non-skip rectangular (HORZ/VERT/HORZ_B) strip needs rectangular residual coding", | 58 | "a non-skip rectangular (HORZ/VERT/HORZ_B) strip needs rectangular residual coding", |
| 59 | "a palette block with a split luma transform (round 1)", | 59 | "a palette block with a split luma transform (round 1)", |
| 60 | "a reference frame other than LAST_FRAME (round 2)", | 60 | "a reference frame other than LAST_FRAME (round 2)", |
crates/ec-av1/src/stream.rsmodified+321 -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 | } |
| @@ -242,12 +250,41 @@ pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> { | |||
| 242 | // 8-bit LUT/clamp, above), and any other rounding gap 10-bit exposes | 250 | // 8-bit LUT/clamp, above), and any other rounding gap 10-bit exposes |
| 243 | // will show up as a real pixel mismatch, not a silent truncation -- | 251 | // will show up as a real pixel mismatch, not a silent truncation -- |
| 244 | // the whole reason the widen (and this wiring) happened. | 252 | // the whole reason the widen (and this wiring) happened. |
| 253 | + // lane-seg: `segment_id` is read per block now (spec 5.11.7-5.11.9, | ||
| 254 | + // `decode::intra_segment_id`/`inter_segment_id`) and consumed by the | ||
| 255 | + // per-segment quantizer (spec 7.12.2) and loop-filter level | ||
| 256 | + // (spec 7.14.4). The three features that instead REWRITE a block's | ||
| 257 | + // mode/reference decisions -- `SEG_LVL_REF_FRAME` (forces the | ||
| 258 | + // reference), `SEG_LVL_SKIP` and `SEG_LVL_GLOBALMV` (both suppress | ||
| 259 | + // symbols `decode_inter_block` unconditionally reads) -- have no | ||
| 260 | + // reader on the inter path, so they still refuse by name rather than | ||
| 261 | + // desyncing. aomenc's `--aq-mode` segmentation only ever sets | ||
| 262 | + // `SEG_LVL_ALT_Q`. | ||
| 245 | if header.segmentation.enabled { | 263 | if header.segmentation.enabled { |
| 246 | - return Err(Error::unsupported( | 264 | + for segment in 0..MAX_SEGMENTS { |
| 247 | - "AV1 decode_stream", | 265 | + for feature in [SEG_LVL_REF_FRAME, SEG_LVL_SKIP, SEG_LVL_GLOBALMV] { |
| 248 | - "a frame with segmentation enabled (this decoder never reads a per-block segment_id symbol)", | 266 | + if header.segmentation.feature_enabled[segment][feature] { |
| 249 | - )); | 267 | + return Err(Error::unsupported( |
| 268 | + "AV1 decode_stream", | ||
| 269 | + "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)", | ||
| 270 | + )); | ||
| 271 | + } | ||
| 272 | + } | ||
| 273 | + } | ||
| 250 | } | 274 | } |
| 275 | + // spec `load_previous_segment_ids`: `PrevSegmentIds` comes from the | ||
| 276 | + // primary reference frame's slot, and is all-zero without one. | ||
| 277 | + let prev_seg_map = if header.primary_ref_frame == PRIMARY_REF_NONE { | ||
| 278 | + None | ||
| 279 | + } else { | ||
| 280 | + seg_map_slots[header.ref_frame_idx[header.primary_ref_frame as usize] as usize].clone() | ||
| 281 | + }; | ||
| 282 | + decode::set_segmentation( | ||
| 283 | + header.segmentation, | ||
| 284 | + header.mi_rows as usize, | ||
| 285 | + header.mi_cols as usize, | ||
| 286 | + prev_seg_map.as_deref(), | ||
| 287 | + ); | ||
| 251 | // lane-superres stage 2/3: `use_superres` adds/removes no per-block | 288 | // lane-superres stage 2/3: `use_superres` adds/removes no per-block |
| 252 | // symbol (spec 7.16's upscaling is a pixel-domain post-process | 289 | // symbol (spec 7.16's upscaling is a pixel-domain post-process |
| 253 | // libaom's `decodeframe.c` runs between CDEF and loop restoration, | 290 | // libaom's `decodeframe.c` runs between CDEF and loop restoration, |
| @@ -686,6 +723,7 @@ pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> { | |||
| 686 | } | 723 | } |
| 687 | } | 724 | } |
| 688 | pictures_decoded += 1; | 725 | pictures_decoded += 1; |
| 726 | + let decoded_seg_map = decode::take_segment_ids(); | ||
| 689 | for i in 0..NUM_REF_FRAMES { | 727 | for i in 0..NUM_REF_FRAMES { |
| 690 | if header.refresh_frame_flags & (1 << i) != 0 { | 728 | if header.refresh_frame_flags & (1 << i) != 0 { |
| 691 | cdf_slots[i] = Some(stored_cdfs.clone()); | 729 | cdf_slots[i] = Some(stored_cdfs.clone()); |
| @@ -695,6 +733,7 @@ pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> { | |||
| 695 | // caller's output below carries the grained picture. | 733 | // caller's output below carries the grained picture. |
| 696 | ref_slots[i] = Some(picture.clone()); | 734 | ref_slots[i] = Some(picture.clone()); |
| 697 | motion_field_slots[i] = Some(motion_field.clone()); | 735 | motion_field_slots[i] = Some(motion_field.clone()); |
| 736 | + seg_map_slots[i] = Some(decoded_seg_map.clone()); | ||
| 698 | order_hint_slots[i] = header.order_hint; | 737 | order_hint_slots[i] = header.order_hint; |
| 699 | } | 738 | } |
| 700 | } | 739 | } |
| @@ -8915,6 +8954,283 @@ mod tests { | |||
| 8915 | ); | 8954 | ); |
| 8916 | } | 8955 | } |
| 8917 | 8956 | ||
| 8957 | + /// lane-seg: a real `aomenc` stream with SEGMENTATION genuinely active. | ||
| 8958 | + /// `--aq-mode=1` (variance AQ) / `--aq-mode=2` (complexity AQ) are the two | ||
| 8959 | + /// paths libaom uses to switch `segmentation_enabled` on by itself | ||
| 8960 | + /// (`av1_vaq_frame_setup`/`av1_setup_in_frame_q_adj`, both of which only | ||
| 8961 | + /// ever set `SEG_LVL_ALT_Q`); `--deltaq-mode=0` keeps the per-superblock | ||
| 8962 | + /// delta-q reader out of the picture so a mismatch points at the segment | ||
| 8963 | + /// map, not at `CurrentQIndex`. Mandelbrot's varying local contrast is | ||
| 8964 | + /// what gives variance/complexity AQ different segments to assign -- | ||
| 8965 | + /// a flat gradient collapses every block onto one segment, which the | ||
| 8966 | + /// `segment_ids_seen() >= 2` assert below would catch as a vacuous run. | ||
| 8967 | + /// The rest of the recipe is the delta-q gate's own feature-disable | ||
| 8968 | + /// envelope, so this gate isolates the segmentation reader. | ||
| 8969 | + fn run_segmentation_gate( | ||
| 8970 | + name: &str, | ||
| 8971 | + aq_mode: &str, | ||
| 8972 | + bit_depth: usize, | ||
| 8973 | + frame_count: usize, | ||
| 8974 | + rate_args: &[&str], | ||
| 8975 | + ) { | ||
| 8976 | + let _gate_lock = lock_gate_counters(); | ||
| 8977 | + if !have_ffmpeg() { | ||
| 8978 | + eprintln!("SKIP {name}: no ffmpeg"); | ||
| 8979 | + return; | ||
| 8980 | + } | ||
| 8981 | + if !have_aomenc() { | ||
| 8982 | + eprintln!("SKIP {name}: no aomenc at {}", aomenc_path().display()); | ||
| 8983 | + return; | ||
| 8984 | + } | ||
| 8985 | + crate::decode::reset_segment_hits(); | ||
| 8986 | + let (width, height) = (128usize, 64usize); | ||
| 8987 | + let mut named_refusals = 0u32; | ||
| 8988 | + let mut matched = 0u32; | ||
| 8989 | + let n_attempts: u32 = std::env::var("EC_SEG_GATE_ATTEMPTS") | ||
| 8990 | + .ok() | ||
| 8991 | + .and_then(|s| s.parse().ok()) | ||
| 8992 | + .unwrap_or(10); | ||
| 8993 | + for attempt in 0..n_attempts { | ||
| 8994 | + let seed = 42 + attempt; | ||
| 8995 | + let duration = frame_count as f64 / 25.0; | ||
| 8996 | + let source = format!( | ||
| 8997 | + "mandelbrot=size={width}x{height}:rate=25:start_x={sx}:start_y={sy}", | ||
| 8998 | + sx = -0.6 + 0.005 * (attempt as f64), | ||
| 8999 | + sy = -0.4 + 0.005 * (attempt as f64) | ||
| 9000 | + ); | ||
| 9001 | + let pix_fmt = if bit_depth == 10 { "yuv420p10le" } else { "yuv420p" }; | ||
| 9002 | + let y4m = Command::new("ffmpeg") | ||
| 9003 | + .args([ | ||
| 9004 | + "-v", "error", "-f", "lavfi", "-i", &source, "-t", &duration.to_string(), | ||
| 9005 | + "-pix_fmt", pix_fmt, "-strict", "-1", "-f", "yuv4mpegpipe", "-", | ||
| 9006 | + ]) | ||
| 9007 | + .stdin(Stdio::null()) | ||
| 9008 | + .stdout(Stdio::piped()) | ||
| 9009 | + .stderr(Stdio::piped()) | ||
| 9010 | + .output() | ||
| 9011 | + .expect("ffmpeg failed to run"); | ||
| 9012 | + assert!( | ||
| 9013 | + y4m.status.success(), | ||
| 9014 | + "ffmpeg fixture: {}", | ||
| 9015 | + String::from_utf8_lossy(&y4m.stderr) | ||
| 9016 | + ); | ||
| 9017 | + let depth_args: Vec<String> = if bit_depth == 10 { | ||
| 9018 | + vec!["--input-bit-depth=10".to_owned(), "--bit-depth=10".to_owned()] | ||
| 9019 | + } else { | ||
| 9020 | + Vec::new() | ||
| 9021 | + }; | ||
| 9022 | + let aq = format!("--aq-mode={aq_mode}"); | ||
| 9023 | + let mut args: Vec<&str> = vec![ | ||
| 9024 | + "--codec=av1", | ||
| 9025 | + "--passes=1", | ||
| 9026 | + "--cpu-used=4", | ||
| 9027 | + &aq, | ||
| 9028 | + "--deltaq-mode=0", | ||
| 9029 | + "--kf-max-dist=1000", | ||
| 9030 | + "--threads=1", | ||
| 9031 | + "--row-mt=0", | ||
| 9032 | + "--sb-size=64", | ||
| 9033 | + "--auto-alt-ref=1", | ||
| 9034 | + "--lag-in-frames=16", | ||
| 9035 | + "--enable-fwd-kf=0", | ||
| 9036 | + "--enable-order-hint=1", | ||
| 9037 | + "--enable-warped-motion=1", | ||
| 9038 | + "--enable-global-motion=0", | ||
| 9039 | + "--enable-obmc=1", | ||
| 9040 | + "--tune-content=default", | ||
| 9041 | + "--enable-masked-comp=1", | ||
| 9042 | + "--enable-dist-wtd-comp=0", | ||
| 9043 | + "--enable-interintra-comp=1", | ||
| 9044 | + "--enable-onesided-comp=0", | ||
| 9045 | + "--enable-interintra-wedge=0", | ||
| 9046 | + "--enable-smooth-interintra=1", | ||
| 9047 | + "--enable-rect-partitions=0", | ||
| 9048 | + "--enable-ab-partitions=0", | ||
| 9049 | + "--enable-1to4-partitions=0", | ||
| 9050 | + "--enable-filter-intra=0", | ||
| 9051 | + "--enable-smooth-intra=0", | ||
| 9052 | + "--enable-paeth-intra=0", | ||
| 9053 | + "--enable-directional-intra=0", | ||
| 9054 | + "--enable-angle-delta=0", | ||
| 9055 | + "--enable-tx-size-search=0", | ||
| 9056 | + "--enable-cdef=1", | ||
| 9057 | + "--enable-restoration=0", | ||
| 9058 | + "--max-partition-size=32", | ||
| 9059 | + "--min-partition-size=32", | ||
| 9060 | + "--enable-palette=0", | ||
| 9061 | + "--enable-intrabc=0", | ||
| 9062 | + "--enable-cfl-intra=0", | ||
| 9063 | + "--enable-ref-frame-mvs=0", | ||
| 9064 | + ]; | ||
| 9065 | + args.extend(rate_args.iter().copied()); | ||
| 9066 | + args.extend(depth_args.iter().map(String::as_str)); | ||
| 9067 | + args.extend(["--obu", "-o", "-", "-"]); | ||
| 9068 | + let mut child = Command::new(aomenc_path()) | ||
| 9069 | + .args(&args) | ||
| 9070 | + .stdin(Stdio::piped()) | ||
| 9071 | + .stdout(Stdio::piped()) | ||
| 9072 | + .stderr(Stdio::piped()) | ||
| 9073 | + .spawn() | ||
| 9074 | + .expect("aomenc failed to start"); | ||
| 9075 | + child | ||
| 9076 | + .stdin | ||
| 9077 | + .take() | ||
| 9078 | + .expect("aomenc stdin") | ||
| 9079 | + .write_all(&y4m.stdout) | ||
| 9080 | + .expect("writing y4m to aomenc"); | ||
| 9081 | + let out = child.wait_with_output().expect("aomenc failed to run"); | ||
| 9082 | + assert!( | ||
| 9083 | + out.status.success(), | ||
| 9084 | + "aomenc refused the fixture: {}", | ||
| 9085 | + String::from_utf8_lossy(&out.stderr) | ||
| 9086 | + ); | ||
| 9087 | + let stream = out.stdout; | ||
| 9088 | + // The stream must really carry `segmentation_enabled`, or the | ||
| 9089 | + // gate proves nothing about the reader. | ||
| 9090 | + let mut seg_frames = 0u32; | ||
| 9091 | + { | ||
| 9092 | + let mut probe = Av1Parser::new(); | ||
| 9093 | + let mut pos = 0usize; | ||
| 9094 | + while pos < stream.len() { | ||
| 9095 | + let obu = probe.parse_obu(&stream[pos..]).expect("parsing the gate stream"); | ||
| 9096 | + pos += obu.total_size; | ||
| 9097 | + match &obu.kind { | ||
| 9098 | + ObuKind::Frame(h, _) => { | ||
| 9099 | + if h.segmentation.enabled { | ||
| 9100 | + seg_frames += 1; | ||
| 9101 | + } | ||
| 9102 | + } | ||
| 9103 | + ObuKind::FrameHeader(h) => { | ||
| 9104 | + if h.segmentation.enabled { | ||
| 9105 | + seg_frames += 1; | ||
| 9106 | + } | ||
| 9107 | + } | ||
| 9108 | + _ => {} | ||
| 9109 | + } | ||
| 9110 | + } | ||
| 9111 | + } | ||
| 9112 | + if seg_frames == 0 { | ||
| 9113 | + eprintln!("seed {seed}: aomenc wrote no segmentation-enabled frame, skipping attempt"); | ||
| 9114 | + continue; | ||
| 9115 | + } | ||
| 9116 | + let frames = match decode_stream(&stream) { | ||
| 9117 | + Err(e) => { | ||
| 9118 | + let msg = e.to_string(); | ||
| 9119 | + assert!( | ||
| 9120 | + msg.contains("unsupported"), | ||
| 9121 | + "{name} failed outright, not a named refusal (seed {seed}): {msg}" | ||
| 9122 | + ); | ||
| 9123 | + assert!( | ||
| 9124 | + !msg.contains("segment_id symbol"), | ||
| 9125 | + "{name} refused on the segmentation reader itself (seed {seed}): {msg}" | ||
| 9126 | + ); | ||
| 9127 | + named_refusals += 1; | ||
| 9128 | + eprintln!("seed {seed} refusal: {msg}"); | ||
| 9129 | + continue; | ||
| 9130 | + } | ||
| 9131 | + Ok(frames) => frames, | ||
| 9132 | + }; | ||
| 9133 | + let ffmpeg_frames = if bit_depth == 10 { | ||
| 9134 | + ffmpeg_decode_sequence_10bit(&stream, width, height, frame_count) | ||
| 9135 | + } else { | ||
| 9136 | + ffmpeg_decode_sequence(&stream, width, height, frame_count) | ||
| 9137 | + }; | ||
| 9138 | + assert_eq!(frames.len(), frame_count); | ||
| 9139 | + let mismatched = frames | ||
| 9140 | + .iter() | ||
| 9141 | + .zip(&ffmpeg_frames) | ||
| 9142 | + .any(|(got, want)| got.y != want.y || got.u != want.u || got.v != want.v); | ||
| 9143 | + if mismatched && let Ok(path) = std::env::var("EC_AV1_GATE_DUMP") { | ||
| 9144 | + std::fs::write(&path, &stream).expect("writing pinned stream"); | ||
| 9145 | + eprintln!("EC_AV1_GATE_DUMP: wrote mismatching stream (seed {seed}) to {path}"); | ||
| 9146 | + } | ||
| 9147 | + for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { | ||
| 9148 | + assert_eq!(got.y, want.y, "{name} frame {i} luma vs ffmpeg (seed {seed})"); | ||
| 9149 | + assert_eq!(got.u, want.u, "{name} frame {i} U vs ffmpeg (seed {seed})"); | ||
| 9150 | + assert_eq!(got.v, want.v, "{name} frame {i} V vs ffmpeg (seed {seed})"); | ||
| 9151 | + } | ||
| 9152 | + matched += 1; | ||
| 9153 | + } | ||
| 9154 | + assert!( | ||
| 9155 | + matched > 0, | ||
| 9156 | + "{name}: every attempt refused on other capabilities ({named_refusals} refusals); \ | ||
| 9157 | + the gate never exercised segmentation" | ||
| 9158 | + ); | ||
| 9159 | + assert!( | ||
| 9160 | + crate::decode::segment_id_hits() > 0, | ||
| 9161 | + "{name}: {matched} pixel-exact matches but zero segment_id symbols fired -- \ | ||
| 9162 | + the reader is unexercised" | ||
| 9163 | + ); | ||
| 9164 | + assert!( | ||
| 9165 | + crate::decode::segment_ids_seen() >= 2, | ||
| 9166 | + "{name}: {matched} pixel-exact matches but only {} distinct segment id(s) -- \ | ||
| 9167 | + every block landed in one segment, so nothing was actually segmented", | ||
| 9168 | + crate::decode::segment_ids_seen() | ||
| 9169 | + ); | ||
| 9170 | + // An inter-frame run must also have exercised the TEMPORAL path | ||
| 9171 | + // (`seg_id_predicted` against the primary reference's saved map); | ||
| 9172 | + // the key-frame-only run has no previous map by construction. | ||
| 9173 | + if frame_count > 1 { | ||
| 9174 | + assert!( | ||
| 9175 | + crate::decode::segment_pred_hits() > 0, | ||
| 9176 | + "{name}: {matched} pixel-exact matches over {frame_count} frames but zero \ | ||
| 9177 | + seg_id_predicted symbols -- the temporal_update path is unexercised" | ||
| 9178 | + ); | ||
| 9179 | + } | ||
| 9180 | + eprintln!( | ||
| 9181 | + "{name}: {named_refusals} other-capability refusals, {matched} pixel-exact matches \ | ||
| 9182 | + out of {n_attempts}, segment_id_hits={}, distinct segment ids={}, \ | ||
| 9183 | + seg_id_predicted symbols={}", | ||
| 9184 | + crate::decode::segment_id_hits(), | ||
| 9185 | + crate::decode::segment_ids_seen(), | ||
| 9186 | + crate::decode::segment_pred_hits() | ||
| 9187 | + ); | ||
| 9188 | + } | ||
| 9189 | + | ||
| 9190 | + #[test] | ||
| 9191 | + fn a_real_aomenc_stream_with_variance_aq_segmentation_decodes_pixel_exact() { | ||
| 9192 | + run_segmentation_gate( | ||
| 9193 | + "a_real_aomenc_stream_with_variance_aq_segmentation_decodes_pixel_exact", | ||
| 9194 | + "1", | ||
| 9195 | + 8, | ||
| 9196 | + 16, | ||
| 9197 | + &["--end-usage=q", "--cq-level=45"], | ||
| 9198 | + ); | ||
| 9199 | + } | ||
| 9200 | + | ||
| 9201 | + // lane-seg r1 DEFERRED: an `--aq-mode=2` (complexity AQ) twin of the gate | ||
| 9202 | + // above. libaom only switches segmentation on for complexity AQ once the | ||
| 9203 | + // target bits per 64x64 superblock clear `sb64_target_rate >= 256` | ||
| 9204 | + // (`aq_complexity.c:is_sb_aq_enabled`), which needs `--end-usage=vbr`; | ||
| 9205 | + // every VBR recipe tried (cpu-used 0 and 4, 32x32 and 64x64 fixed | ||
| 9206 | + // partitions) makes aomenc pick partition shapes OTHER lanes still refuse | ||
| 9207 | + // (AB below 16x16, SB-level HORZ/VERT, part32 values 4/6), so 10/10 | ||
| 9208 | + // attempts refuse before a pixel is compared. The syntax exercised would | ||
| 9209 | + // be identical to variance AQ's (both features only ever set | ||
| 9210 | + // `SEG_LVL_ALT_Q` via `update_map`), so this is coverage, not capability. | ||
| 9211 | + // Unblocked by the sub-16x16 rect/AB partition lanes. | ||
| 9212 | + | ||
| 9213 | + /// His two AV1 films are both `yuv420p10le`, so every capability lands | ||
| 9214 | + /// with a 10-bit twin (memory `his-av1-library-is-10-bit`). KEY FRAME | ||
| 9215 | + /// ONLY (`frame_count = 1`): a 10-bit INTER frame does not decode | ||
| 9216 | + /// pixel-exact in this crate yet, segmentation or not -- lane-seg r1 ran | ||
| 9217 | + /// this same recipe with `--aq-mode=0` (segmentation off entirely) and it | ||
| 9218 | + /// still mismatched at frame 2, so the 16-frame 10-bit variant would be | ||
| 9219 | + /// gating an unrelated pre-existing gap. Variance AQ segments the key | ||
| 9220 | + /// frame too (`aq_variance.c`, `is_frame_aq_enabled` includes | ||
| 9221 | + /// `frame_is_intra_only`), so this still proves the 10-bit segment-id | ||
| 9222 | + /// read and the per-segment 10-bit dequant. | ||
| 9223 | + #[test] | ||
| 9224 | + fn a_real_aomenc_10bit_stream_with_aq_segmentation_decodes_pixel_exact() { | ||
| 9225 | + run_segmentation_gate( | ||
| 9226 | + "a_real_aomenc_10bit_stream_with_aq_segmentation_decodes_pixel_exact", | ||
| 9227 | + "1", | ||
| 9228 | + 10, | ||
| 9229 | + 1, | ||
| 9230 | + &["--end-usage=q", "--cq-level=45"], | ||
| 9231 | + ); | ||
| 9232 | + } | ||
| 9233 | + | ||
| 8918 | /// lane-tiles r7: the gate stage 2 (r6, commit 8899fc1) shipped without | 9234 | /// lane-tiles r7: the gate stage 2 (r6, commit 8899fc1) shipped without |
| 8919 | /// -- a real >1-tile-column stream with a genuine INTER frame (not just | 9235 | /// -- a real >1-tile-column stream with a genuine INTER frame (not just |
| 8920 | /// the key-frame-only two-column gate above), decoded through | 9236 | /// the key-frame-only two-column gate above), decoded through |
lanes/seg-r1.report.mdadded+67 -0
| @@ -0,0 +1,67 @@ | |||
| 1 | +# lane-seg r1 — per-block `segment_id` and its consumers | ||
| 2 | + | ||
| 3 | +Branch `lane-seg`, off main `3808cf8` (WIP snapshot `51581d3` continued, not restarted). | ||
| 4 | + | ||
| 5 | +## What changed | ||
| 6 | + | ||
| 7 | +- `crates/ec-av1/src/cdf.rs:564` — `SEGMENT_ID` (`default_spatial_pred_seg_tree_cdf`, 3 ctx x 8 symbols) | ||
| 8 | + and `SEGMENT_PRED` (`seg_id_predicted`, 3 ctx) default CDFs. | ||
| 9 | +- `crates/ec-av1/src/cdf_state.rs:467` — both tables in `Cdfs`, in `reset_counters` (class | ||
| 10 | + `cdf-counter-not-reset`) and in the default-table constructor, so they adapt and reset like every | ||
| 11 | + other per-tile table. | ||
| 12 | +- `crates/ec-av1/src/decode.rs:319-655` — the reader: `set_segmentation` (per-frame install + | ||
| 13 | + `load_previous_segment_ids`), `CurrentSegmentIds`/`PrevSegmentIds` maps, `AboveSegPredContext`/ | ||
| 14 | + `LeftSegPredContext`, `av1_neg_deinterleave`, `av1_get_spatial_seg_pred` (ctx + prediction), | ||
| 15 | + `read_segment_id`, `intra_segment_id` (spec 5.11.7) and `inter_segment_id` (5.11.9, both | ||
| 16 | + pre-skip and post-skip positions, `seg_id_predicted` under `temporal_update`, inherited map when | ||
| 17 | + `update_map == 0`), `set_segment_id` stamping the block's whole mi footprint. | ||
| 18 | +- Call sites: `read_intra_mode` (decode.rs:4375) and `read_intra_mode_rect` (3332) read | ||
| 19 | + `intra_segment_id` before `skip` when `SegIdPreSkip`, after it otherwise; | ||
| 20 | + `decode_inter_block` (9250/9268) and `decode_inter_block8` (11106/11116) read both spec positions. | ||
| 21 | +- Consumers: `block_q_idx()` (decode.rs:400, spec 7.12.2 `get_qindex` with `SEG_LVL_ALT_Q`) replaces | ||
| 22 | + the raw `CURRENT_Q_IDX` read at all 10 dequant sites; `lf_level` (decode.rs:6168) takes the block's | ||
| 23 | + segment id and applies `SEG_LVL_ALT_LF_Y_V/H`/`_U`/`_V` (spec 7.14.4) between the `DeltaLF` term and | ||
| 24 | + the ref/mode deltas; `edge_params` passes the per-side segment ids. | ||
| 25 | +- `crates/ec-av1/src/stream.rs:266-291` — frame-level: narrowed refusal (below), spec | ||
| 26 | + `load_previous_segment_ids` from the primary reference's slot, `set_segmentation` per frame; | ||
| 27 | + `stream.rs:736` stores the decoded map into every slot `refresh_frame_flags` names. | ||
| 28 | +- `crates/ec-av1/src/refusal_inventory.rs:59` — refusal replaced. | ||
| 29 | + | ||
| 30 | +## Refusal | ||
| 31 | + | ||
| 32 | +LIFTED: `"a frame with segmentation enabled (this decoder never reads a per-block segment_id symbol)"`. | ||
| 33 | +REPLACED BY (narrower): `"a frame whose segmentation enables SEG_LVL_REF_FRAME/SKIP/GLOBALMV (this | ||
| 34 | +decoder reads segment_id but never lets a segment override a block's reference, skip or mode)"` — | ||
| 35 | +those three features rewrite mode/reference decisions and suppress symbols `decode_inter_block` reads | ||
| 36 | +unconditionally; aomenc's `--aq-mode` only ever sets `SEG_LVL_ALT_Q`, so no gate here can exercise | ||
| 37 | +them. `gate_coverage.rs` needed no edit: it derives coverage from `--enable-*` flags and segmentation | ||
| 38 | +is driven by `--aq-mode`, not an `--enable-*` flag; its two tests stay green. | ||
| 39 | + | ||
| 40 | +## Gates (both hard-assert; no decode error or mismatch becomes SKIP) | ||
| 41 | + | ||
| 42 | +`stream.rs:8698 run_segmentation_gate` — mandelbrot fixture (varying local contrast so AQ has | ||
| 43 | +something to segment), `--aq-mode=1 --deltaq-mode=0`, 10 attempts, pixel-exact vs ffmpeg over every | ||
| 44 | +decoded frame including hidden alt-refs (`--auto-alt-ref=1 --lag-in-frames=16`), asserting | ||
| 45 | +`segment_id_hits > 0`, `segment_ids_seen >= 2`, and — for multi-frame runs — `segment_pred_hits > 0` | ||
| 46 | +(the `temporal_update` path). | ||
| 47 | + | ||
| 48 | +Command: `EC_AV1_REQUIRE_AOMENC=1 cargo test -p ec-av1 --lib aq_segmentation -- --nocapture` | ||
| 49 | + | ||
| 50 | +EVIDENCE: crates/ec-av1/src/stream.rs:8908 a_real_aomenc_stream_with_variance_aq_segmentation_decodes_pixel_exact | aomenc --aq-mode=1 --deltaq-mode=0, 16 frames 128x64 8-bit, 10 seeds, decode_stream vs ffmpeg per plane | 10/10 pixel-exact, segment_id_hits=223, distinct segment ids=6, seg_id_predicted symbols=35 | ||
| 51 | +EVIDENCE: crates/ec-av1/src/stream.rs:8941 a_real_aomenc_10bit_stream_with_aq_segmentation_decodes_pixel_exact | same recipe, yuv420p10le, key frame only, 10 seeds | 10/10 pixel-exact, segment_id_hits=80, distinct segment ids=5 | ||
| 52 | +EVIDENCE: cargo test -p ec-av1 --lib (EC_AV1_REQUIRE_AOMENC=1) | full ec-av1 lib suite | 269 passed, 0 failed, 23 ignored, 494s | ||
| 53 | +EVIDENCE: scratchpad reelc.obu / hg.obu (ffmpeg -t 0.4 -c:v copy -f obu) | cargo run -p ec-av1 --example decode_probe | ReelC stops at "a 32x32 partition type this decoder does not code (value=4)", ReelB at "a partition below 8x8" — neither stops on segmentation | ||
| 54 | + | ||
| 55 | +## Residue | ||
| 56 | + | ||
| 57 | +- deferred(sub-16x16 rect/AB partition lanes): an `--aq-mode=2` (complexity AQ) twin. libaom only | ||
| 58 | + enables segmentation for complexity AQ under `--end-usage=vbr` (`aq_complexity.c:is_sb_aq_enabled`, | ||
| 59 | + `sb64_target_rate >= 256`), and every VBR recipe tried makes aomenc pick partition shapes other | ||
| 60 | + lanes still refuse (AB below 16x16, SB-level HORZ/VERT, part32 value 4/6) — 10/10 attempts refuse | ||
| 61 | + before a pixel is compared. Coverage, not capability: complexity AQ sets exactly the same | ||
| 62 | + `SEG_LVL_ALT_Q`/`update_map` syntax variance AQ does. Comment at stream.rs:8919. | ||
| 63 | +- deferred(a 10-bit inter-frame decode defect unrelated to this lane): the 10-bit gate is key-frame | ||
| 64 | + only. Measured: the same recipe with `--aq-mode=0` (segmentation entirely off) also mismatches at | ||
| 65 | + frame 2, so a 16-frame 10-bit variant would gate a pre-existing gap. Comment at stream.rs:8933. | ||
| 66 | +- accepted: `SEG_LVL_REF_FRAME`/`SKIP`/`GLOBALMV` stay refused by name (see above) — no aomenc | ||
| 67 | + `--aq-mode` path writes them, so a gate cannot be built from this oracle today. | ||