Merge branch 'lane-av1s3' (91f6923f): NEARMV/GLOBALMV with a real DRL index, GOLDEN/ALTREF references, CDF carry across frames
Browse files at this commit6 changed files +833 -324
- crates/ec-av1/src/decode.rs +35 -3
- crates/ec-av1/src/encode.rs +468 -202
- crates/ec-av1/src/encoder.rs +24 -0
- crates/ec-av1/src/mvstack.rs +1 -0
- crates/ec-av1/src/stream.rs +8 -1
- crates/ec-av1/src/tile.rs +297 -118
crates/ec-av1/src/decode.rsmodified+35 -3
| @@ -32774,6 +32774,11 @@ pub(crate) fn decode_inter_frame_tile( | ||
| 32774 | 32774 | // inter frames coded Select by default (class: test asserts against a |
| 32775 | 32775 | // stale header, second instance). |
| 32776 | 32776 | tx_select: bool, |
| 32777 | + // The CDF state this frame's tile was WRITTEN from (`Encoded::start_cdfs`): | |
| 32778 | + // an inter frame whose header leaves `disable_frame_end_update_cdf` off | |
| 32779 | + // starts from what the previous frame stored, not from the defaults. Same | |
| 32780 | + // class as `tx_select` above, third instance. | |
| 32781 | + initial_cdfs: Option<Cdfs>, | |
| 32777 | 32782 | fctx: &crate::decode::FrameCtx, |
| 32778 | 32783 | ) -> Result<Picture> { |
| 32779 | 32784 | let single_tile = TileInfo { |
| @@ -32794,7 +32799,7 @@ pub(crate) fn decode_inter_frame_tile( | ||
| 32794 | 32799 | cdef, |
| 32795 | 32800 | loop_filter, |
| 32796 | 32801 | &LoopRestorationParams::default(), |
| 32797 | - None, | |
| 32802 | + initial_cdfs, | |
| 32798 | 32803 | allow_high_precision_mv, |
| 32799 | 32804 | force_integer_mv, |
| 32800 | 32805 | NO_SIGN_BIAS, |
| @@ -35789,6 +35794,22 @@ mod tests { | ||
| 35789 | 35794 | RefPix::ready(refs) |
| 35790 | 35795 | } |
| 35791 | 35796 | |
| 35797 | + /// [`last_only`] plus `GOLDEN_FRAME` (index 4 of the name-keyed array) | |
| 35798 | + /// and `ALTREF_FRAME` (index 7, the frame two back, which the encoder | |
| 35799 | + /// keeps in the slot the current frame refreshes): what a frame this | |
| 35800 | + /// encoder writes needs, since its blocks may name any of the three. | |
| 35801 | + fn last_golden_altref<'a>( | |
| 35802 | + last: &'a Picture, | |
| 35803 | + golden: &'a Picture, | |
| 35804 | + altref: Option<&'a Picture>, | |
| 35805 | + ) -> RefPix<'a> { | |
| 35806 | + let mut refs: [Option<&Picture>; 8] = [None; 8]; | |
| 35807 | + refs[1] = Some(last); | |
| 35808 | + refs[4] = Some(golden); | |
| 35809 | + refs[7] = altref; | |
| 35810 | + RefPix::ready(refs) | |
| 35811 | + } | |
| 35812 | + | |
| 35792 | 35813 | |
| 35793 | 35814 | /// lane-perf6 step 2: the CDEF kernel now reads a pre-gathered window and |
| 35794 | 35815 | /// a pre-resolved damping shift instead of a bounds-testing closure and a |
| @@ -36928,6 +36949,7 @@ mod tests { | ||
| 36928 | 36949 | false, |
| 36929 | 36950 | false, |
| 36930 | 36951 | false, |
| 36952 | + None, | |
| 36931 | 36953 | fctx, |
| 36932 | 36954 | ) |
| 36933 | 36955 | .unwrap_err(); |
| @@ -37315,7 +37337,11 @@ mod tests { | ||
| 37315 | 37337 | frame.base_q_idx, |
| 37316 | 37338 | width as u32, |
| 37317 | 37339 | height as u32, |
| 37318 | - &last_only(&reference), | |
| 37340 | + &last_golden_altref( | |
| 37341 | + &reference, | |
| 37342 | + &encoded.frames[0].reconstruction, | |
| 37343 | + (i >= 2).then(|| &encoded.frames[i - 2].reconstruction), | |
| 37344 | + ), | |
| 37319 | 37345 | &frame.cdef, |
| 37320 | 37346 | &frame.loop_filter, |
| 37321 | 37347 | false, |
| @@ -37324,6 +37350,7 @@ mod tests { | ||
| 37324 | 37350 | false, |
| 37325 | 37351 | false, |
| 37326 | 37352 | frame.tx_select, |
| 37353 | + Some(frame.start_cdfs.0.clone()), | |
| 37327 | 37354 | fctx, |
| 37328 | 37355 | ) |
| 37329 | 37356 | .unwrap(); |
| @@ -37626,7 +37653,11 @@ mod tests { | ||
| 37626 | 37653 | frame.base_q_idx, |
| 37627 | 37654 | coded_w, |
| 37628 | 37655 | coded_h, |
| 37629 | - &last_only(&reference), | |
| 37656 | + &last_golden_altref( | |
| 37657 | + &reference, | |
| 37658 | + &encoded.frames[0].reconstruction, | |
| 37659 | + (i >= 2).then(|| &encoded.frames[i - 2].reconstruction), | |
| 37660 | + ), | |
| 37630 | 37661 | &frame.cdef, |
| 37631 | 37662 | &frame.loop_filter, |
| 37632 | 37663 | false, |
| @@ -37635,6 +37666,7 @@ mod tests { | ||
| 37635 | 37666 | false, |
| 37636 | 37667 | false, |
| 37637 | 37668 | frame.tx_select, |
| 37669 | + Some(frame.start_cdfs.0.clone()), | |
| 37638 | 37670 | fctx, |
| 37639 | 37671 | ) |
| 37640 | 37672 | .unwrap(); |
crates/ec-av1/src/encode.rsmodified+468 -202
| @@ -477,11 +477,24 @@ pub(crate) fn crop_encoded(encoded: &Encoded, width: usize, height: usize) -> En | ||
| 477 | 477 | mi_rows: encoded.mi_rows, |
| 478 | 478 | base_q_idx: encoded.base_q_idx, |
| 479 | 479 | tx_select: encoded.tx_select, |
| 480 | + start_cdfs: encoded.start_cdfs.clone(), | |
| 481 | + next_cdfs: encoded.next_cdfs.clone(), | |
| 480 | 482 | loop_filter: encoded.loop_filter, |
| 481 | 483 | cdef: encoded.cdef, |
| 482 | 484 | } |
| 483 | 485 | } |
| 484 | 486 | |
| 487 | +/// A `Cdfs` inside a `Debug` type: the tables are tens of kilobytes of | |
| 488 | +/// numbers nobody wants printed, and [`Encoded`] derives `Debug`. | |
| 489 | +#[derive(Clone)] | |
| 490 | +pub(crate) struct CdfSnapshot(pub(crate) crate::cdf_state::Cdfs); | |
| 491 | + | |
| 492 | +impl std::fmt::Debug for CdfSnapshot { | |
| 493 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | |
| 494 | + f.write_str("CdfSnapshot") | |
| 495 | + } | |
| 496 | +} | |
| 497 | + | |
| 485 | 498 | /// What one call to [`encode_key_frame`] produced. |
| 486 | 499 | #[derive(Clone, Debug)] |
| 487 | 500 | pub struct Encoded { |
| @@ -515,6 +528,18 @@ pub struct Encoded { | ||
| 515 | 528 | /// a `tx_depth` frame as a `TxMode::Largest` one and desyncs at the first |
| 516 | 529 | /// block. |
| 517 | 530 | pub(crate) tx_select: bool, |
| 531 | + /// The CDF state this frame's tile writer started from -- the defaults | |
| 532 | + /// for a key frame and for the first inter frame, and the previous | |
| 533 | + /// frame's stored tables after that. A test that decodes `tile` directly | |
| 534 | + /// must feed the decoder this, exactly as `decode_stream` feeds it what | |
| 535 | + /// the frame's `primary_ref_frame` slot holds (same class as | |
| 536 | + /// `tx_select`: a header bit a raw tile decode cannot guess). | |
| 537 | + pub(crate) start_cdfs: CdfSnapshot, | |
| 538 | + /// What this frame stores into the slots it refreshes (spec 7.20, | |
| 539 | + /// `crate::stream::stored_cdfs_for`): its end-of-tile tables with the | |
| 540 | + /// counts reset when `disable_frame_end_update_cdf` is off, and what it | |
| 541 | + /// started from when it is on. | |
| 542 | + pub(crate) next_cdfs: CdfSnapshot, | |
| 518 | 543 | /// This frame header's chosen deblocking parameters |
| 519 | 544 | /// ([`crate::filter_search::pick_deblock`]) — `reconstruction` is the |
| 520 | 545 | /// picture the decoder produces UNDER them, so a test that decodes |
| @@ -688,6 +713,11 @@ pub(crate) fn key_frame_headers_colour( | ||
| 688 | 713 | Ok((seq, header)) |
| 689 | 714 | } |
| 690 | 715 | |
| 716 | +/// The DPB slot `GOLDEN_FRAME` names in every frame this encoder writes: the | |
| 717 | +/// key frame refreshes all eight slots, each inter frame only slot 0, so slot | |
| 718 | +/// 1 still holds the key frame however long the GOP runs. | |
| 719 | +pub const GOLDEN_SLOT: u8 = 1; | |
| 720 | + | |
| 691 | 721 | /// The frame header for a shown inter frame that predicts from the single |
| 692 | 722 | /// slot `last_slot` (every `ref_frame_idx` entry names it) and refreshes that |
| 693 | 723 | /// same slot with itself once coded. The sequence header is [`key_frame_headers`]'s, |
| @@ -701,6 +731,26 @@ pub fn inter_frame_headers( | ||
| 701 | 731 | base_q_idx: u8, |
| 702 | 732 | order_hint: u32, |
| 703 | 733 | last_slot: u8, |
| 734 | +) -> Result<(SequenceHeader, FrameHeader)> { | |
| 735 | + inter_frame_headers_slots(width, height, base_q_idx, order_hint, last_slot, last_slot, last_slot) | |
| 736 | +} | |
| 737 | + | |
| 738 | +/// [`inter_frame_headers`] with the three slots named apart: the one read as | |
| 739 | +/// `LAST_FRAME`, the one this frame refreshes, and the one read as | |
| 740 | +/// `ALTREF_FRAME`. The encoder alternates the refreshed slot between 0 and 2 | |
| 741 | +/// so that the frame two back is still intact in the slot this one is about | |
| 742 | +/// to overwrite -- read before write, spec 7.20's decode order. | |
| 743 | +/// | |
| 744 | +/// # Errors | |
| 745 | +/// As [`inter_frame_headers`]. | |
| 746 | +pub fn inter_frame_headers_slots( | |
| 747 | + width: usize, | |
| 748 | + height: usize, | |
| 749 | + base_q_idx: u8, | |
| 750 | + order_hint: u32, | |
| 751 | + last_slot: u8, | |
| 752 | + self_slot: u8, | |
| 753 | + altref_slot: u8, | |
| 704 | 754 | ) -> Result<(SequenceHeader, FrameHeader)> { |
| 705 | 755 | let (seq, key) = key_frame_headers(width, height, base_q_idx)?; |
| 706 | 756 | let header = FrameHeader { |
| @@ -709,12 +759,25 @@ pub fn inter_frame_headers( | ||
| 709 | 759 | show_frame: true, |
| 710 | 760 | error_resilient_mode: false, |
| 711 | 761 | disable_cdf_update: false, |
| 712 | - disable_frame_end_update_cdf: true, | |
| 762 | + // The tile writer hands its end-of-tile tables to the next frame | |
| 763 | + // (`encode_sequence_with_ctx`), so the frame stores them the way the | |
| 764 | + // decoder will load them (spec 7.20). | |
| 765 | + disable_frame_end_update_cdf: false, | |
| 713 | 766 | force_integer_mv: false, |
| 714 | 767 | order_hint, |
| 715 | 768 | primary_ref_frame: 0, |
| 716 | - refresh_frame_flags: 1 << last_slot, | |
| 717 | - ref_frame_idx: [last_slot; ec_av1_syntax::REFS_PER_FRAME], | |
| 769 | + refresh_frame_flags: 1 << self_slot, | |
| 770 | + // Every reference but `GOLDEN_FRAME` (index 3 of `ref_frame_idx`) | |
| 771 | + // names the slot this frame both predicts from and refreshes; | |
| 772 | + // GOLDEN names slot 1, which the key frame wrote when it refreshed | |
| 773 | + // all eight and which no inter frame ever overwrites, so it holds | |
| 774 | + // the key frame's own picture for the whole GOP. | |
| 775 | + ref_frame_idx: { | |
| 776 | + let mut idx = [last_slot; ec_av1_syntax::REFS_PER_FRAME]; | |
| 777 | + idx[3] = GOLDEN_SLOT; | |
| 778 | + idx[6] = altref_slot; | |
| 779 | + idx | |
| 780 | + }, | |
| 718 | 781 | allow_high_precision_mv: false, |
| 719 | 782 | interpolation_filter: ec_av1_syntax::InterpolationFilter::Eighttap, |
| 720 | 783 | is_motion_mode_switchable: false, |
| @@ -2337,8 +2400,10 @@ fn code_square_inter( | ||
| 2337 | 2400 | eight: None, |
| 2338 | 2401 | tx_depth, |
| 2339 | 2402 | inter: Some(InterInfo { |
| 2403 | + ref_frame: crate::mvstack::LAST_FRAME, | |
| 2340 | 2404 | mode: InterMode::NewMv, |
| 2341 | 2405 | mv: new_mv, |
| 2406 | + ref_mv_idx: 0, | |
| 2342 | 2407 | }), |
| 2343 | 2408 | }, |
| 2344 | 2409 | cost + dcost, |
| @@ -2362,8 +2427,10 @@ fn code_square_inter( | ||
| 2362 | 2427 | eight: None, |
| 2363 | 2428 | tx_depth, |
| 2364 | 2429 | inter: Some(InterInfo { |
| 2430 | + ref_frame: crate::mvstack::LAST_FRAME, | |
| 2365 | 2431 | mode: InterMode::NearestMv, |
| 2366 | 2432 | mv, |
| 2433 | + ref_mv_idx: 0, | |
| 2367 | 2434 | }), |
| 2368 | 2435 | }, inter_cost + dcost) |
| 2369 | 2436 | } else { |
| @@ -2379,7 +2446,7 @@ fn record_mi(grid: &mut MiGrid, mi_row: usize, mi_col: usize, size: u8, inter: O | ||
| 2379 | 2446 | let info = match inter { |
| 2380 | 2447 | Some(info) => MiInfo { |
| 2381 | 2448 | is_inter: true, |
| 2382 | - ref_frame: LAST_FRAME, | |
| 2449 | + ref_frame: info.ref_frame, | |
| 2383 | 2450 | ref_frame1: NO_REF1, |
| 2384 | 2451 | mv1: (0, 0), |
| 2385 | 2452 | mv: mv16(info.mv), |
| @@ -3024,12 +3091,20 @@ pub(crate) fn encode_key_frame_inner( | ||
| 3024 | 3091 | |
| 3025 | 3092 | #[cfg(test)] |
| 3026 | 3093 | record_predicted_bits(crate::tile::predicted_coeff_bits_sb(&superblocks, base_q_idx)); |
| 3027 | - let tile = crate::tile::sb_coeff_key_frame_tile_tx( | |
| 3094 | + // A key frame always starts from the defaults (`primary_ref_frame` is | |
| 3095 | + // `PRIMARY_REF_NONE`), and its header keeps `disable_frame_end_update_cdf` | |
| 3096 | + // set, so what it stores into the slots it refreshes is exactly those | |
| 3097 | + // defaults again (spec 7.20's `started_from` arm) -- the first inter | |
| 3098 | + // frame after it therefore starts from the defaults too. | |
| 3099 | + let mut cdfs = crate::cdf_state::Cdfs::new(crate::tile::q_ctx_of(base_q_idx)); | |
| 3100 | + let start_cdfs = CdfSnapshot(cdfs.clone()); | |
| 3101 | + let tile = crate::tile::sb_coeff_key_frame_tile_cdfs( | |
| 3028 | 3102 | header.mi_cols, |
| 3029 | 3103 | header.mi_rows, |
| 3030 | 3104 | base_q_idx, |
| 3031 | 3105 | &superblocks, |
| 3032 | 3106 | tx_select, |
| 3107 | + &mut cdfs, | |
| 3033 | 3108 | )?; |
| 3034 | 3109 | // spec 7.14 deblocking: pick this frame's `loop_filter_level` by handing |
| 3035 | 3110 | // the tile just coded back to the decoder under each candidate (see |
| @@ -3083,6 +3158,8 @@ pub(crate) fn encode_key_frame_inner( | ||
| 3083 | 3158 | mi_rows: header.mi_rows, |
| 3084 | 3159 | base_q_idx, |
| 3085 | 3160 | tx_select, |
| 3161 | + next_cdfs: start_cdfs.clone(), | |
| 3162 | + start_cdfs, | |
| 3086 | 3163 | loop_filter: header.loop_filter, |
| 3087 | 3164 | cdef: header.cdef, |
| 3088 | 3165 | }) |
| @@ -3360,7 +3437,13 @@ fn search_inter_block( | ||
| 3360 | 3437 | search: &Search, |
| 3361 | 3438 | mode_bits: &[f64; 13], |
| 3362 | 3439 | reference: &Picture, |
| 3363 | - stack: &MvStack, fctx: &crate::decode::FrameCtx, | |
| 3440 | + stack: &MvStack, | |
| 3441 | + // `GOLDEN_FRAME`: the key frame's own reconstruction, held in DPB slot 1 | |
| 3442 | + // for the whole GOP, with the stack this block derives against it. Only | |
| 3443 | + // its two search-free modes are priced (`NEARESTMV`, `GLOBALMV`) -- a | |
| 3444 | + // second motion search would double the wall of the stage that already | |
| 3445 | + // owns most of it. | |
| 3446 | + extra: &[(i8, &Picture, &MvStack)], fctx: &crate::decode::FrameCtx, | |
| 3364 | 3447 | ) -> (BlockCoeffs, f64) { |
| 3365 | 3448 | // `luma_set` is the INTRA candidates' table; the two inter candidates |
| 3366 | 3449 | // below price their coefficients through `TxbSet::Luma32Inter`, which is |
| @@ -3373,9 +3456,17 @@ fn search_inter_block( | ||
| 3373 | 3456 | // function's doc comment. |
| 3374 | 3457 | let skip_bits = |skip: bool| symbol_bits(&cdf::SKIP[0], usize::from(skip)); |
| 3375 | 3458 | let intra_inter_bits = |inter: bool| symbol_bits(&cdf::INTRA_INTER[0], usize::from(inter)); |
| 3376 | - let single_ref_bits = symbol_bits(&cdf::SINGLE_REF[0][0], 0) | |
| 3377 | - + symbol_bits(&cdf::SINGLE_REF[0][2], 0) | |
| 3378 | - + symbol_bits(&cdf::SINGLE_REF[0][3], 0); | |
| 3459 | + // `write_single_ref`'s own tree, at the fixed context this pricer uses | |
| 3460 | + // for every neighbour-derived symbol: LAST is p1=0,p3=0,p4=0 and GOLDEN | |
| 3461 | + // p1=0,p3=1,p5=1. | |
| 3462 | + let single_ref_bits = |ref_frame: i8| { | |
| 3463 | + symbol_bits(&cdf::SINGLE_REF[0][0], 0) | |
| 3464 | + + if ref_frame == crate::mvstack::GOLDEN_FRAME { | |
| 3465 | + symbol_bits(&cdf::SINGLE_REF[0][2], 1) + symbol_bits(&cdf::SINGLE_REF[0][4], 1) | |
| 3466 | + } else { | |
| 3467 | + symbol_bits(&cdf::SINGLE_REF[0][2], 0) + symbol_bits(&cdf::SINGLE_REF[0][3], 0) | |
| 3468 | + } | |
| 3469 | + }; | |
| 3379 | 3470 | |
| 3380 | 3471 | struct Candidate { |
| 3381 | 3472 | cost: f64, |
| @@ -3508,100 +3599,72 @@ fn search_inter_block( | ||
| 3508 | 3599 | chroma[1].true_width, |
| 3509 | 3600 | chroma[1].true_height, |
| 3510 | 3601 | ); |
| 3511 | - let mode_bits_inter = symbol_bits(&cdf::NEW_MV[stack.new_mv_ctx], 1) // not NEWMV | |
| 3512 | - + symbol_bits(&cdf::ZERO_MV[stack.zero_mv_ctx], 1) // not zero | |
| 3513 | - + symbol_bits(&cdf::REF_MV[stack.ref_mv_ctx], 0); // NEARESTMV | |
| 3514 | - | |
| 3515 | - // The NEARESTMV candidate: `skip` is decided below from whether its | |
| 3516 | - // trial actually found a nonzero level, once its residual is priced | |
| 3517 | - // against the same CDFs the tile writer codes it with. | |
| 3518 | - // | |
| 3519 | - // lane-av1speed: its three trials are kept (rather than moved straight | |
| 3520 | - // into the candidate) so that a NEWMV landing on the very same vector -- | |
| 3521 | - // common on static content, where the stack's nearest vector is also | |
| 3522 | - // what the search finds -- reuses them instead of running the same | |
| 3523 | - // motion compensation, transform, quantize and reconstruct again. The | |
| 3524 | - // reused trials are the same values, so the choice is bit-identical. | |
| 3525 | - let nearest_trials; | |
| 3526 | - { | |
| 3527 | - let mv = stack.nearest_mv; | |
| 3528 | - let luma_trial = mc_trial( | |
| 3529 | - luma, | |
| 3530 | - x, | |
| 3531 | - y, | |
| 3532 | - BLOCK, | |
| 3533 | - mv, | |
| 3534 | - true, | |
| 3535 | - ref_luma.0, | |
| 3536 | - ref_luma.1, | |
| 3537 | - ref_luma.2, | |
| 3538 | - ref_luma.3, | |
| 3539 | - false, | |
| 3540 | - search.base_q_idx, | |
| 3541 | - search.deadzone, | |
| 3542 | - TxbSet::Luma32Inter, fctx, | |
| 3543 | - ); | |
| 3544 | - let u = mc_trial( | |
| 3545 | - &chroma[0], | |
| 3546 | - x / 2, | |
| 3547 | - y / 2, | |
| 3548 | - BLOCK / 2, | |
| 3549 | - mv, | |
| 3550 | - false, | |
| 3551 | - ref_u.0, | |
| 3552 | - ref_u.1, | |
| 3553 | - ref_u.2, | |
| 3554 | - ref_u.3, | |
| 3555 | - false, | |
| 3556 | - search.base_q_idx, | |
| 3557 | - search.deadzone, | |
| 3558 | - chroma_set, fctx, | |
| 3559 | - ); | |
| 3560 | - let v = mc_trial( | |
| 3561 | - &chroma[1], | |
| 3562 | - x / 2, | |
| 3563 | - y / 2, | |
| 3564 | - BLOCK / 2, | |
| 3565 | - mv, | |
| 3566 | - false, | |
| 3567 | - ref_v.0, | |
| 3568 | - ref_v.1, | |
| 3569 | - ref_v.2, | |
| 3570 | - ref_v.3, | |
| 3571 | - false, | |
| 3572 | - search.base_q_idx, | |
| 3573 | - search.deadzone, | |
| 3574 | - chroma_set, fctx, | |
| 3575 | - ); | |
| 3576 | - let skip = luma_trial.levels.iter().all(|&l| l == 0) | |
| 3577 | - && u.levels.iter().all(|&l| l == 0) | |
| 3578 | - && v.levels.iter().all(|&l| l == 0); | |
| 3579 | - let cost = luma_trial.sse | |
| 3580 | - + u.sse | |
| 3581 | - + v.sse | |
| 3582 | - + search.lambda | |
| 3583 | - * (skip_bits(skip) | |
| 3584 | - + intra_inter_bits(true) | |
| 3585 | - + single_ref_bits | |
| 3586 | - + mode_bits_inter | |
| 3587 | - + if skip { | |
| 3588 | - 0.0 | |
| 3589 | - } else { | |
| 3590 | - luma_trial.bits + u.bits + v.bits | |
| 3591 | - }); | |
| 3592 | - nearest_trials = (luma_trial.clone(), u.clone(), v.clone()); | |
| 3593 | - consider(Candidate { | |
| 3594 | - cost, | |
| 3595 | - luma: luma_trial, | |
| 3596 | - u, | |
| 3597 | - v, | |
| 3598 | - mode: DC_PRED, | |
| 3599 | - skip, | |
| 3600 | - inter: Some(InterInfo { | |
| 3602 | + // Every single-reference mode the writer can code (spec 5.11.24's symbol | |
| 3603 | + // chain, mirrored by `tile::write_inter_mode`), priced with the static | |
| 3604 | + // CDFs at this block's own stack contexts. A candidate is a motion | |
| 3605 | + // vector plus the cheapest syntax that reaches it, so two modes landing | |
| 3606 | + // on the same vector share one set of trials: the added modes cost | |
| 3607 | + // mode-bit arithmetic, not another motion compensation. | |
| 3608 | + let not_new = symbol_bits(&cdf::NEW_MV[stack.new_mv_ctx], 1); | |
| 3609 | + let not_zero = symbol_bits(&cdf::ZERO_MV[stack.zero_mv_ctx], 1); | |
| 3610 | + // `tile::write_drl_idx`'s cost: one `drl_mode` symbol per stack entry | |
| 3611 | + // past `start`, `1` to advance and `0` to stop, at most two. A `target` | |
| 3612 | + // the loop cannot reach is never offered below -- every candidate's | |
| 3613 | + // index is one this same loop walks to. | |
| 3614 | + let drl_bits = |start: usize, target: usize| { | |
| 3615 | + let mut bits = 0.0; | |
| 3616 | + let mut idx = start; | |
| 3617 | + while idx < start + 2 && stack.entries.len() > idx + 1 { | |
| 3618 | + let advance = idx < target; | |
| 3619 | + bits += symbol_bits(&cdf::DRL_MODE[stack.drl_ctx[idx]], usize::from(advance)); | |
| 3620 | + if !advance { | |
| 3621 | + break; | |
| 3622 | + } | |
| 3623 | + idx += 1; | |
| 3624 | + } | |
| 3625 | + bits | |
| 3626 | + }; | |
| 3627 | + let last_ref_bits = single_ref_bits(crate::mvstack::LAST_FRAME); | |
| 3628 | + let mut cands: Vec<((i32, i32), f64, InterInfo)> = vec![ | |
| 3629 | + ( | |
| 3630 | + stack.nearest_mv, | |
| 3631 | + last_ref_bits + not_new + not_zero + symbol_bits(&cdf::REF_MV[stack.ref_mv_ctx], 0), | |
| 3632 | + InterInfo { | |
| 3633 | + ref_frame: crate::mvstack::LAST_FRAME, | |
| 3601 | 3634 | mode: InterMode::NearestMv, |
| 3602 | - mv, | |
| 3603 | - }), | |
| 3604 | - }); | |
| 3635 | + mv: stack.nearest_mv, | |
| 3636 | + ref_mv_idx: 0, | |
| 3637 | + }, | |
| 3638 | + ), | |
| 3639 | + // GLOBALMV is the identity model's zero vector -- this encoder writes | |
| 3640 | + // no `gm_params` -- and the cheapest syntax of the four on the static | |
| 3641 | + // content where that vector is also the right one. | |
| 3642 | + ( | |
| 3643 | + (0, 0), | |
| 3644 | + last_ref_bits + not_new + symbol_bits(&cdf::ZERO_MV[stack.zero_mv_ctx], 0), | |
| 3645 | + InterInfo { | |
| 3646 | + ref_frame: crate::mvstack::LAST_FRAME, | |
| 3647 | + mode: InterMode::GlobalMv, | |
| 3648 | + mv: (0, 0), | |
| 3649 | + ref_mv_idx: 0, | |
| 3650 | + }, | |
| 3651 | + ), | |
| 3652 | + ]; | |
| 3653 | + let near_bits = | |
| 3654 | + last_ref_bits + not_new + not_zero + symbol_bits(&cdf::REF_MV[stack.ref_mv_ctx], 1); | |
| 3655 | + for idx in 1..=2usize { | |
| 3656 | + if let Some(e) = stack.entries.get(idx) { | |
| 3657 | + cands.push(( | |
| 3658 | + e.mv, | |
| 3659 | + near_bits + drl_bits(1, idx), | |
| 3660 | + InterInfo { | |
| 3661 | + ref_frame: crate::mvstack::LAST_FRAME, | |
| 3662 | + mode: InterMode::NearMv, | |
| 3663 | + mv: e.mv, | |
| 3664 | + ref_mv_idx: idx as u8, | |
| 3665 | + }, | |
| 3666 | + )); | |
| 3667 | + } | |
| 3605 | 3668 | } |
| 3606 | 3669 | |
| 3607 | 3670 | let source_block = luma.source_block(x, y, BLOCK); |
| @@ -3623,96 +3686,157 @@ fn search_inter_block( | ||
| 3623 | 3686 | #[cfg(test)] |
| 3624 | 3687 | stage_add(0, t.elapsed()); |
| 3625 | 3688 | let mv = round_to_valid_mv(found.mv, stack.pred_mv); |
| 3626 | - if let Some(mv_bits) = mv_residual_bits(mv, stack.pred_mv) { | |
| 3627 | - let (luma_trial, u, v) = if mv == stack.nearest_mv { | |
| 3628 | - nearest_trials | |
| 3629 | - } else { | |
| 3630 | - ( | |
| 3631 | - mc_trial( | |
| 3632 | - luma, | |
| 3633 | - x, | |
| 3634 | - y, | |
| 3635 | - BLOCK, | |
| 3636 | - mv, | |
| 3637 | - true, | |
| 3638 | - ref_luma.0, | |
| 3639 | - ref_luma.1, | |
| 3640 | - ref_luma.2, | |
| 3641 | - ref_luma.3, | |
| 3642 | - false, | |
| 3643 | - search.base_q_idx, | |
| 3644 | - search.deadzone, | |
| 3645 | - TxbSet::Luma32Inter, fctx, | |
| 3646 | - ), | |
| 3647 | - mc_trial( | |
| 3648 | - &chroma[0], | |
| 3649 | - x / 2, | |
| 3650 | - y / 2, | |
| 3651 | - BLOCK / 2, | |
| 3652 | - mv, | |
| 3653 | - false, | |
| 3654 | - ref_u.0, | |
| 3655 | - ref_u.1, | |
| 3656 | - ref_u.2, | |
| 3657 | - ref_u.3, | |
| 3658 | - false, | |
| 3659 | - search.base_q_idx, | |
| 3660 | - search.deadzone, | |
| 3661 | - chroma_set, fctx, | |
| 3662 | - ), | |
| 3663 | - mc_trial( | |
| 3664 | - &chroma[1], | |
| 3665 | - x / 2, | |
| 3666 | - y / 2, | |
| 3667 | - BLOCK / 2, | |
| 3668 | - mv, | |
| 3669 | - false, | |
| 3670 | - ref_v.0, | |
| 3671 | - ref_v.1, | |
| 3672 | - ref_v.2, | |
| 3673 | - ref_v.3, | |
| 3674 | - false, | |
| 3675 | - search.base_q_idx, | |
| 3676 | - search.deadzone, | |
| 3677 | - chroma_set, fctx, | |
| 3678 | - ), | |
| 3679 | - ) | |
| 3680 | - }; | |
| 3681 | - let drl_bits = if stack.entries.len() > 1 { | |
| 3682 | - symbol_bits(&cdf::DRL_MODE[stack.drl_ctx[0]], 0) | |
| 3683 | - } else { | |
| 3684 | - 0.0 | |
| 3685 | - }; | |
| 3686 | - let skip = luma_trial.levels.iter().all(|&l| l == 0) | |
| 3687 | - && u.levels.iter().all(|&l| l == 0) | |
| 3688 | - && v.levels.iter().all(|&l| l == 0); | |
| 3689 | - let cost = luma_trial.sse | |
| 3690 | - + u.sse | |
| 3691 | - + v.sse | |
| 3692 | - + search.lambda | |
| 3693 | - * (skip_bits(skip) | |
| 3694 | - + intra_inter_bits(true) | |
| 3695 | - + single_ref_bits | |
| 3696 | - + symbol_bits(&cdf::NEW_MV[stack.new_mv_ctx], 0) | |
| 3697 | - + drl_bits | |
| 3698 | - + mv_bits | |
| 3699 | - + if skip { | |
| 3700 | - 0.0 | |
| 3701 | - } else { | |
| 3702 | - luma_trial.bits + u.bits + v.bits | |
| 3703 | - }); | |
| 3704 | - consider(Candidate { | |
| 3705 | - cost, | |
| 3706 | - luma: luma_trial, | |
| 3707 | - u, | |
| 3708 | - v, | |
| 3709 | - mode: DC_PRED, | |
| 3710 | - skip, | |
| 3711 | - inter: Some(InterInfo { | |
| 3689 | + // Each DRL entry is a different `PredMv` (spec 7.10.2.10 `assign_mv`), so | |
| 3690 | + // the same vector costs a different residual from each: take the index | |
| 3691 | + // whose residual plus DRL bits is cheapest, not entry 0 by default. | |
| 3692 | + let mut best_new: Option<(f64, u8)> = None; | |
| 3693 | + for idx in 0..3usize { | |
| 3694 | + if idx > 0 && stack.entries.len() <= idx { | |
| 3695 | + break; | |
| 3696 | + } | |
| 3697 | + let base = stack.entries.get(idx).map_or(stack.pred_mv, |e| e.mv); | |
| 3698 | + if let Some(b) = mv_residual_bits(mv, base) { | |
| 3699 | + let total = b + drl_bits(0, idx); | |
| 3700 | + if best_new.is_none_or(|(t, _)| total < t) { | |
| 3701 | + best_new = Some((total, idx as u8)); | |
| 3702 | + } | |
| 3703 | + } | |
| 3704 | + } | |
| 3705 | + if let Some((bits, ref_mv_idx)) = best_new { | |
| 3706 | + cands.push(( | |
| 3707 | + mv, | |
| 3708 | + last_ref_bits + symbol_bits(&cdf::NEW_MV[stack.new_mv_ctx], 0) + bits, | |
| 3709 | + InterInfo { | |
| 3710 | + ref_frame: crate::mvstack::LAST_FRAME, | |
| 3712 | 3711 | mode: InterMode::NewMv, |
| 3713 | 3712 | mv, |
| 3714 | - }), | |
| 3715 | - }); | |
| 3713 | + ref_mv_idx, | |
| 3714 | + }, | |
| 3715 | + )); | |
| 3716 | + } | |
| 3717 | + | |
| 3718 | + for &(ref_frame, _, gstack) in extra { | |
| 3719 | + let g_ref_bits = single_ref_bits(ref_frame); | |
| 3720 | + let g_not_new = symbol_bits(&cdf::NEW_MV[gstack.new_mv_ctx], 1); | |
| 3721 | + cands.push(( | |
| 3722 | + gstack.nearest_mv, | |
| 3723 | + g_ref_bits | |
| 3724 | + + g_not_new | |
| 3725 | + + symbol_bits(&cdf::ZERO_MV[gstack.zero_mv_ctx], 1) | |
| 3726 | + + symbol_bits(&cdf::REF_MV[gstack.ref_mv_ctx], 0), | |
| 3727 | + InterInfo { | |
| 3728 | + ref_frame, | |
| 3729 | + mode: InterMode::NearestMv, | |
| 3730 | + mv: gstack.nearest_mv, | |
| 3731 | + ref_mv_idx: 0, | |
| 3732 | + }, | |
| 3733 | + )); | |
| 3734 | + cands.push(( | |
| 3735 | + (0, 0), | |
| 3736 | + g_ref_bits + g_not_new + symbol_bits(&cdf::ZERO_MV[gstack.zero_mv_ctx], 0), | |
| 3737 | + InterInfo { | |
| 3738 | + ref_frame, | |
| 3739 | + mode: InterMode::GlobalMv, | |
| 3740 | + mv: (0, 0), | |
| 3741 | + ref_mv_idx: 0, | |
| 3742 | + }, | |
| 3743 | + )); | |
| 3744 | + } | |
| 3745 | + | |
| 3746 | + // One trial set per distinct (reference, vector) pair: sort, cheapest | |
| 3747 | + // syntax first, and drop the rest of each pair's run. | |
| 3748 | + cands.sort_by(|a, b| { | |
| 3749 | + (a.2.ref_frame, a.0) | |
| 3750 | + .cmp(&(b.2.ref_frame, b.0)) | |
| 3751 | + .then(a.1.total_cmp(&b.1)) | |
| 3752 | + }); | |
| 3753 | + cands.dedup_by_key(|c| (c.2.ref_frame, c.0)); | |
| 3754 | + { | |
| 3755 | + for (mv, mode_bits_inter, info) in cands { | |
| 3756 | + let (ref_luma, ref_u, ref_v) = match extra | |
| 3757 | + .iter() | |
| 3758 | + .find(|(r, _, _)| *r == info.ref_frame) | |
| 3759 | + { | |
| 3760 | + Some(&(_, g, _)) => ( | |
| 3761 | + (&g.y, g.width, luma.true_width, luma.true_height), | |
| 3762 | + (&g.u, g.width / 2, chroma[0].true_width, chroma[0].true_height), | |
| 3763 | + (&g.v, g.width / 2, chroma[1].true_width, chroma[1].true_height), | |
| 3764 | + ), | |
| 3765 | + None => (ref_luma, ref_u, ref_v), | |
| 3766 | + }; | |
| 3767 | + let luma_trial = mc_trial( | |
| 3768 | + luma, | |
| 3769 | + x, | |
| 3770 | + y, | |
| 3771 | + BLOCK, | |
| 3772 | + mv, | |
| 3773 | + true, | |
| 3774 | + ref_luma.0, | |
| 3775 | + ref_luma.1, | |
| 3776 | + ref_luma.2, | |
| 3777 | + ref_luma.3, | |
| 3778 | + false, | |
| 3779 | + search.base_q_idx, | |
| 3780 | + search.deadzone, | |
| 3781 | + TxbSet::Luma32Inter, fctx, | |
| 3782 | + ); | |
| 3783 | + let u = mc_trial( | |
| 3784 | + &chroma[0], | |
| 3785 | + x / 2, | |
| 3786 | + y / 2, | |
| 3787 | + BLOCK / 2, | |
| 3788 | + mv, | |
| 3789 | + false, | |
| 3790 | + ref_u.0, | |
| 3791 | + ref_u.1, | |
| 3792 | + ref_u.2, | |
| 3793 | + ref_u.3, | |
| 3794 | + false, | |
| 3795 | + search.base_q_idx, | |
| 3796 | + search.deadzone, | |
| 3797 | + chroma_set, fctx, | |
| 3798 | + ); | |
| 3799 | + let v = mc_trial( | |
| 3800 | + &chroma[1], | |
| 3801 | + x / 2, | |
| 3802 | + y / 2, | |
| 3803 | + BLOCK / 2, | |
| 3804 | + mv, | |
| 3805 | + false, | |
| 3806 | + ref_v.0, | |
| 3807 | + ref_v.1, | |
| 3808 | + ref_v.2, | |
| 3809 | + ref_v.3, | |
| 3810 | + false, | |
| 3811 | + search.base_q_idx, | |
| 3812 | + search.deadzone, | |
| 3813 | + chroma_set, fctx, | |
| 3814 | + ); | |
| 3815 | + let skip = luma_trial.levels.iter().all(|&l| l == 0) | |
| 3816 | + && u.levels.iter().all(|&l| l == 0) | |
| 3817 | + && v.levels.iter().all(|&l| l == 0); | |
| 3818 | + let cost = luma_trial.sse | |
| 3819 | + + u.sse | |
| 3820 | + + v.sse | |
| 3821 | + + search.lambda | |
| 3822 | + * (skip_bits(skip) | |
| 3823 | + + intra_inter_bits(true) | |
| 3824 | + + mode_bits_inter | |
| 3825 | + + if skip { | |
| 3826 | + 0.0 | |
| 3827 | + } else { | |
| 3828 | + luma_trial.bits + u.bits + v.bits | |
| 3829 | + }); | |
| 3830 | + consider(Candidate { | |
| 3831 | + cost, | |
| 3832 | + luma: luma_trial, | |
| 3833 | + u, | |
| 3834 | + v, | |
| 3835 | + mode: DC_PRED, | |
| 3836 | + skip, | |
| 3837 | + inter: Some(info), | |
| 3838 | + }); | |
| 3839 | + } | |
| 3716 | 3840 | } |
| 3717 | 3841 | |
| 3718 | 3842 | let best = best.expect("the search offers at least the intra modes"); |
| @@ -3744,7 +3868,20 @@ pub(crate) fn encode_inter_frame( | ||
| 3744 | 3868 | base_q_idx: u8, |
| 3745 | 3869 | deadzone: f64, |
| 3746 | 3870 | order_hint: u32, |
| 3747 | - render: (usize, usize), fctx: &crate::decode::FrameCtx, | |
| 3871 | + render: (usize, usize), | |
| 3872 | + // The tables this frame's writer starts from: what the previous frame | |
| 3873 | + // stored into the slot this one's `primary_ref_frame` names. `None` | |
| 3874 | + // starts from the defaults, which is right only for the frame straight | |
| 3875 | + // after a key frame. | |
| 3876 | + start_cdfs: Option<&crate::cdf_state::Cdfs>, | |
| 3877 | + // The key frame's own (padded) reconstruction, which every frame of the | |
| 3878 | + // GOP still holds in DPB slot 1 -- `GOLDEN_FRAME`. `None` codes the frame | |
| 3879 | + // with `LAST_FRAME` alone, exactly as before. | |
| 3880 | + golden: Option<&Picture>, | |
| 3881 | + // The frame two back, held in the slot this frame is about to refresh | |
| 3882 | + // (the encoder alternates 0/2): `ALTREF_FRAME`. `None` for the first two | |
| 3883 | + // inter frames of a GOP, where the frame two back IS the key frame. | |
| 3884 | + altref: Option<&Picture>, fctx: &crate::decode::FrameCtx, | |
| 3748 | 3885 | ) -> Result<Encoded> { |
| 3749 | 3886 | picture.check()?; |
| 3750 | 3887 | if !picture.width.is_multiple_of(SUPERBLOCK) || !picture.height.is_multiple_of(SUPERBLOCK) { |
| @@ -3767,14 +3904,33 @@ pub(crate) fn encode_inter_frame( | ||
| 3767 | 3904 | // overwrites slot 0, so the frame just coded is always what the next one |
| 3768 | 3905 | // predicts against. |
| 3769 | 3906 | const LAST_SLOT: u8 = 0; |
| 3907 | + // Slot 0 and slot 2 alternate so the frame two back survives one more | |
| 3908 | + // frame: frame n writes A(n) = 0 for odd n, 2 for even, so A(n) = A(n-2) | |
| 3909 | + // and the picture this frame names as `ALTREF_FRAME` is the one sitting | |
| 3910 | + // in the very slot it will overwrite. | |
| 3911 | + let self_slot = if order_hint % 2 == 1 { 0 } else { 2 }; | |
| 3912 | + let last_slot = if order_hint <= 1 { | |
| 3913 | + LAST_SLOT | |
| 3914 | + } else if (order_hint - 1) % 2 == 1 { | |
| 3915 | + 0 | |
| 3916 | + } else { | |
| 3917 | + 2 | |
| 3918 | + }; | |
| 3770 | 3919 | // `render`, not `picture.width`/`picture.height`: `picture` here is |
| 3771 | 3920 | // already padded to a whole number of superblocks (`encode_sequence`'s |
| 3772 | 3921 | // `padded_to(SUPERBLOCK)`), and the header's `mi_cols`/`mi_rows` (and so |
| 3773 | 3922 | // every true-edge clamp downstream) must come from the frame's real, |
| 3774 | 3923 | // unpadded size, same as `key_frame_headers_colour` takes `render` and |
| 3775 | 3924 | // not the padded picture in `encode_key_frame_inner`. |
| 3776 | - let (seq, mut header) = | |
| 3777 | - inter_frame_headers(render.0, render.1, base_q_idx, order_hint, LAST_SLOT)?; | |
| 3925 | + let (seq, mut header) = inter_frame_headers_slots( | |
| 3926 | + render.0, | |
| 3927 | + render.1, | |
| 3928 | + base_q_idx, | |
| 3929 | + order_hint, | |
| 3930 | + last_slot, | |
| 3931 | + self_slot, | |
| 3932 | + if altref.is_some() { self_slot } else { GOLDEN_SLOT }, | |
| 3933 | + )?; | |
| 3778 | 3934 | header.render_width = render.0 as u32; |
| 3779 | 3935 | header.render_height = render.1 as u32; |
| 3780 | 3936 | // `TxMode::Select` here too: an inter block then codes one `txfm_split` |
| @@ -3879,6 +4035,25 @@ pub(crate) fn encode_inter_frame( | ||
| 3879 | 4035 | let (mi_row, mi_col) = (r32 * 8, c32 * 8); |
| 3880 | 4036 | let stack = |
| 3881 | 4037 | find_mv_stack(&grid, mi_row, mi_col, 8, 8, LAST_FRAME, mi_cols, mi_rows); |
| 4038 | + let extra_stacks: Vec<(i8, &Picture, MvStack)> = [ | |
| 4039 | + (crate::mvstack::GOLDEN_FRAME, golden), | |
| 4040 | + (crate::mvstack::ALTREF_FRAME, altref), | |
| 4041 | + ] | |
| 4042 | + .into_iter() | |
| 4043 | + .filter_map(|(r, pic)| { | |
| 4044 | + pic.map(|p| { | |
| 4045 | + ( | |
| 4046 | + r, | |
| 4047 | + p, | |
| 4048 | + find_mv_stack(&grid, mi_row, mi_col, 8, 8, r, mi_cols, mi_rows), | |
| 4049 | + ) | |
| 4050 | + }) | |
| 4051 | + }) | |
| 4052 | + .collect(); | |
| 4053 | + let extra: Vec<(i8, &Picture, &MvStack)> = extra_stacks | |
| 4054 | + .iter() | |
| 4055 | + .map(|(r, p, st)| (*r, *p, st)) | |
| 4056 | + .collect(); | |
| 3882 | 4057 | |
| 3883 | 4058 | let base = snapshot(&luma, &chroma, (x, y), BLOCK); |
| 3884 | 4059 | let (block, mut cost_whole) = search_inter_block( |
| @@ -3888,7 +4063,8 @@ pub(crate) fn encode_inter_frame( | ||
| 3888 | 4063 | &search, |
| 3889 | 4064 | &mode_bits_table, |
| 3890 | 4065 | reference, |
| 3891 | - &stack, fctx, | |
| 4066 | + &stack, | |
| 4067 | + &extra, fctx, | |
| 3892 | 4068 | ); |
| 3893 | 4069 | cost_whole += search.lambda * partition_bits(BLOCK, false); |
| 3894 | 4070 | let after_whole = snapshot(&luma, &chroma, (x, y), BLOCK); |
| @@ -4208,12 +4384,21 @@ pub(crate) fn encode_inter_frame( | ||
| 4208 | 4384 | / modes.len() as f64; |
| 4209 | 4385 | #[cfg(test)] |
| 4210 | 4386 | record_predicted_bits(crate::tile::predicted_coeff_bits(&blocks, base_q_idx)); |
| 4211 | - let tile = crate::tile::sb_coeff_inter_frame_tile_tx( | |
| 4387 | + // This frame's header leaves `disable_frame_end_update_cdf` off, so it | |
| 4388 | + // stores its end-of-tile tables with the counts reset (spec 7.20) and the | |
| 4389 | + // next frame's writer starts from them -- exactly what | |
| 4390 | + // `crate::stream::stored_cdfs_for` hands the decoder. | |
| 4391 | + let mut cdfs = start_cdfs | |
| 4392 | + .cloned() | |
| 4393 | + .unwrap_or_else(|| crate::cdf_state::Cdfs::new(crate::tile::q_ctx_of(base_q_idx))); | |
| 4394 | + let start_cdfs = CdfSnapshot(cdfs.clone()); | |
| 4395 | + let tile = crate::tile::sb_coeff_inter_frame_tile_cdfs( | |
| 4212 | 4396 | header.mi_cols, |
| 4213 | 4397 | header.mi_rows, |
| 4214 | 4398 | base_q_idx, |
| 4215 | 4399 | &blocks, |
| 4216 | 4400 | tx_select, |
| 4401 | + &mut cdfs, | |
| 4217 | 4402 | )?; |
| 4218 | 4403 | // Deblocking, as in `encode_key_frame_inner`: the decoder reconstructs |
| 4219 | 4404 | // this tile against the same single-slot reference this encoder predicted |
| @@ -4235,8 +4420,39 @@ pub(crate) fn encode_inter_frame( | ||
| 4235 | 4420 | v: crop_plane(&reference.v, reference.width / 2, fw / 2, fh / 2), |
| 4236 | 4421 | } |
| 4237 | 4422 | }; |
| 4423 | + // `RefPix` is indexed by the reference NAME (`LAST_FRAME` = 1, | |
| 4424 | + // `GOLDEN_FRAME` = 4), not by DPB slot: the trial decode of a frame whose | |
| 4425 | + // blocks may name GOLDEN needs that plane too, cropped the same way. | |
| 4426 | + let trial_golden = golden.map(|g| { | |
| 4427 | + if (g.width, g.height) == (fw, fh) { | |
| 4428 | + g.clone() | |
| 4429 | + } else { | |
| 4430 | + Picture { | |
| 4431 | + width: fw, | |
| 4432 | + height: fh, | |
| 4433 | + y: crop_plane(&g.y, g.width, fw, fh), | |
| 4434 | + u: crop_plane(&g.u, g.width / 2, fw / 2, fh / 2), | |
| 4435 | + v: crop_plane(&g.v, g.width / 2, fw / 2, fh / 2), | |
| 4436 | + } | |
| 4437 | + } | |
| 4438 | + }); | |
| 4439 | + let trial_altref = altref.map(|a| { | |
| 4440 | + if (a.width, a.height) == (fw, fh) { | |
| 4441 | + a.clone() | |
| 4442 | + } else { | |
| 4443 | + Picture { | |
| 4444 | + width: fw, | |
| 4445 | + height: fh, | |
| 4446 | + y: crop_plane(&a.y, a.width, fw, fh), | |
| 4447 | + u: crop_plane(&a.u, a.width / 2, fw / 2, fh / 2), | |
| 4448 | + v: crop_plane(&a.v, a.width / 2, fw / 2, fh / 2), | |
| 4449 | + } | |
| 4450 | + } | |
| 4451 | + }); | |
| 4238 | 4452 | let mut refs: [Option<&Picture>; 8] = [None; 8]; |
| 4239 | 4453 | refs[1] = Some(&trial_ref); |
| 4454 | + refs[4] = trial_golden.as_ref(); | |
| 4455 | + refs[7] = trial_altref.as_ref(); | |
| 4240 | 4456 | let refpix = crate::decode::RefPix::ready(refs); |
| 4241 | 4457 | pick_and_apply_filters( |
| 4242 | 4458 | &mut header, |
| @@ -4266,10 +4482,16 @@ pub(crate) fn encode_inter_frame( | ||
| 4266 | 4482 | seq.enable_dual_filter, |
| 4267 | 4483 | h.reference_select, |
| 4268 | 4484 | tx_select, |
| 4485 | + // The trial decode re-reads the tile the writer just wrote, | |
| 4486 | + // so it starts from the same tables the writer did (this | |
| 4487 | + // frame's `start_cdfs`), not from the defaults. | |
| 4488 | + Some(start_cdfs.0.clone()), | |
| 4269 | 4489 | fctx, |
| 4270 | 4490 | ) |
| 4271 | 4491 | }, |
| 4272 | 4492 | )?; |
| 4493 | + cdfs.reset_counts(); | |
| 4494 | + let next_cdfs = CdfSnapshot(cdfs); | |
| 4273 | 4495 | let mut stream = temporal_delimiter(); |
| 4274 | 4496 | stream.extend_from_slice(&frame_obu(&seq, &header, &tile)?); |
| 4275 | 4497 | |
| @@ -4290,6 +4512,8 @@ pub(crate) fn encode_inter_frame( | ||
| 4290 | 4512 | mi_rows: header.mi_rows, |
| 4291 | 4513 | base_q_idx, |
| 4292 | 4514 | tx_select, |
| 4515 | + start_cdfs, | |
| 4516 | + next_cdfs, | |
| 4293 | 4517 | loop_filter: header.loop_filter, |
| 4294 | 4518 | cdef: header.cdef, |
| 4295 | 4519 | }) |
| @@ -4350,6 +4574,14 @@ pub(crate) fn encode_sequence_with_ctx( | ||
| 4350 | 4574 | unspecified_color_config(), fctx, |
| 4351 | 4575 | )?; |
| 4352 | 4576 | let mut stream = key.stream.clone(); |
| 4577 | + // Slot 1 keeps the key frame's own reconstruction for the whole GOP (the | |
| 4578 | + // key frame refreshes every slot, each inter frame only slot 0), which is | |
| 4579 | + // what `GOLDEN_FRAME` names. | |
| 4580 | + let golden = key.reconstruction.clone(); | |
| 4581 | + // The frame two back, once there is one that is not the key frame | |
| 4582 | + // itself (which `GOLDEN_FRAME` already names). | |
| 4583 | + let mut prev2: Option<Picture> = None; | |
| 4584 | + let mut carried = key.next_cdfs.clone(); | |
| 4353 | 4585 | let mut reference = key.reconstruction.clone(); |
| 4354 | 4586 | let mut frames = vec![crop_encoded(&key, render.0, render.1)]; |
| 4355 | 4587 | for (i, picture) in rest.iter().enumerate() { |
| @@ -4367,10 +4599,17 @@ pub(crate) fn encode_sequence_with_ctx( | ||
| 4367 | 4599 | base_q_idx, |
| 4368 | 4600 | deadzone, |
| 4369 | 4601 | order_hint, |
| 4370 | - render, fctx, | |
| 4602 | + render, | |
| 4603 | + Some(&carried.0), | |
| 4604 | + Some(&golden), | |
| 4605 | + prev2.as_ref(), fctx, | |
| 4371 | 4606 | )?; |
| 4372 | 4607 | stream.extend_from_slice(&inter.stream); |
| 4373 | - reference = inter.reconstruction.clone(); | |
| 4608 | + carried = inter.next_cdfs.clone(); | |
| 4609 | + prev2 = Some(std::mem::replace( | |
| 4610 | + &mut reference, | |
| 4611 | + inter.reconstruction.clone(), | |
| 4612 | + )); | |
| 4374 | 4613 | frames.push(crop_encoded(&inter, render.0, render.1)); |
| 4375 | 4614 | } |
| 4376 | 4615 | Ok(EncodedSequence { stream, frames }) |
| @@ -4780,15 +5019,19 @@ mod tests { | ||
| 4780 | 5019 | eight: None, |
| 4781 | 5020 | tx_depth: 0, |
| 4782 | 5021 | inter: Some(InterInfo { |
| 5022 | + ref_frame: crate::mvstack::LAST_FRAME, | |
| 4783 | 5023 | mode: InterMode::NearestMv, |
| 4784 | 5024 | mv: (0, 0), |
| 5025 | + ref_mv_idx: 0, | |
| 4785 | 5026 | }), |
| 4786 | 5027 | }; |
| 4787 | 5028 | let skipped_block = BlockCoeffs { |
| 4788 | 5029 | skip: true, |
| 4789 | 5030 | inter: Some(InterInfo { |
| 5031 | + ref_frame: crate::mvstack::LAST_FRAME, | |
| 4790 | 5032 | mode: InterMode::NearestMv, |
| 4791 | 5033 | mv: (0, 0), |
| 5034 | + ref_mv_idx: 0, | |
| 4792 | 5035 | }), |
| 4793 | 5036 | ..BlockCoeffs::default() |
| 4794 | 5037 | }; |
| @@ -6399,7 +6642,7 @@ mod tests { | ||
| 6399 | 6642 | let padded_pic = picture.padded_to(SUPERBLOCK); |
| 6400 | 6643 | let t = Instant::now(); |
| 6401 | 6644 | let inter = |
| 6402 | - encode_inter_frame(&padded_pic, &padded_ref, 100, 0.5, 1, (width, height), fctx).unwrap(); | |
| 6645 | + encode_inter_frame(&padded_pic, &padded_ref, 100, 0.5, 1, (width, height), None, None, None, fctx).unwrap(); | |
| 6403 | 6646 | let inter_t = t.elapsed(); |
| 6404 | 6647 | eprintln!( |
| 6405 | 6648 | "inter frame vs its own key frame: {inter_t:>9.2?} ({} bytes, inter share \ |
| @@ -6497,7 +6740,7 @@ mod tests { | ||
| 6497 | 6740 | stage_reset(); |
| 6498 | 6741 | let t = Instant::now(); |
| 6499 | 6742 | let inter = |
| 6500 | - encode_inter_frame(&padded_pic, &padded_ref, 100, 0.5, 1, (width, height), fctx).unwrap(); | |
| 6743 | + encode_inter_frame(&padded_pic, &padded_ref, 100, 0.5, 1, (width, height), None, None, None, fctx).unwrap(); | |
| 6501 | 6744 | let total_t = t.elapsed(); |
| 6502 | 6745 | let [motion_search, interpolation, transform_quant, entropy] = stage_read(); |
| 6503 | 6746 | let accounted = motion_search + transform_quant + entropy; |
| @@ -6674,7 +6917,7 @@ mod tests { | ||
| 6674 | 6917 | q, |
| 6675 | 6918 | 0.5, |
| 6676 | 6919 | 1, |
| 6677 | - (width, height), fctx, | |
| 6920 | + (width, height), None, None, None, fctx, | |
| 6678 | 6921 | ) |
| 6679 | 6922 | .unwrap(); |
| 6680 | 6923 | let baseline_psnr = psnr(&baseline.reconstruction.y, &inter_source.y); |
| @@ -6691,7 +6934,7 @@ mod tests { | ||
| 6691 | 6934 | q, |
| 6692 | 6935 | 0.5, |
| 6693 | 6936 | 1, |
| 6694 | - (width, height), fctx, | |
| 6937 | + (width, height), None, None, None, fctx, | |
| 6695 | 6938 | ) |
| 6696 | 6939 | .unwrap(); |
| 6697 | 6940 | let pruned_psnr = psnr(&pruned.reconstruction.y, &inter_source.y); |
| @@ -7100,7 +7343,7 @@ mod tests { | ||
| 7100 | 7343 | }) |
| 7101 | 7344 | }; |
| 7102 | 7345 | let pins: [(u8, usize, u64); 2] = |
| 7103 | - [(150, 7823, 0x81db_18bb_91be_0d89), (60, 28189, 0x25fb_2fce_1bd1_e000)]; | |
| 7346 | + [(150, 7416, 0x75632db72a63d5f1), (60, 27927, 0xb63a984f521b7341)]; | |
| 7104 | 7347 | for (q, bytes, hash) in pins { |
| 7105 | 7348 | let encoded = encode_sequence(&source, q, 0.5).unwrap(); |
| 7106 | 7349 | assert_eq!( |
| @@ -7261,11 +7504,34 @@ mod tests { | ||
| 7261 | 7504 | let source = clip_frames(path.to_str().unwrap(), "0", width, height, frames); |
| 7262 | 7505 | let _ = take_partition_hits(); |
| 7263 | 7506 | let _ = take_uv_mode_hits(); |
| 7507 | + let _ = crate::tile::take_inter_mode_hits(); | |
| 7508 | + let _ = crate::tile::take_drl_hits(); | |
| 7509 | + let _ = crate::tile::take_ref_hits(); | |
| 7264 | 7510 | let (ours, ours_wall) = our_ladder(&source, width, height, fctx); |
| 7265 | 7511 | // How often the inter split actually fired over this clip's four |
| 7266 | 7512 | // encodes, against the whole 32x32 that was the only outcome |
| 7267 | 7513 | // before (gate-blind-to-feature). |
| 7268 | 7514 | let [whole32, split32, whole16, split16] = take_partition_hits(); |
| 7515 | + // Which inter modes actually fired over this clip's four encodes, | |
| 7516 | + // and at which DRL index -- a mode nobody picks is a mode the BD | |
| 7517 | + // number cannot be crediting (gate-blind-to-feature). | |
| 7518 | + let [nearest, near, global, new] = crate::tile::take_inter_mode_hits(); | |
| 7519 | + let [drl0, drl1, drl2, drl3] = crate::tile::take_drl_hits(); | |
| 7520 | + let refs = crate::tile::take_ref_hits(); | |
| 7521 | + let modes_total = (nearest + near + global + new).max(1); | |
| 7522 | + eprintln!( | |
| 7523 | + "{name}: inter modes NEAREST {nearest} ({:.1}%) NEAR {near} ({:.1}%) \ | |
| 7524 | + GLOBAL {global} ({:.1}%) NEW {new} ({:.1}%); drl idx 0/1/2/3+ \ | |
| 7525 | + {drl0}/{drl1}/{drl2}/{drl3}", | |
| 7526 | + 100.0 * nearest as f64 / modes_total as f64, | |
| 7527 | + 100.0 * near as f64 / modes_total as f64, | |
| 7528 | + 100.0 * global as f64 / modes_total as f64, | |
| 7529 | + 100.0 * new as f64 / modes_total as f64, | |
| 7530 | + ); | |
| 7531 | + eprintln!( | |
| 7532 | + "{name}: references LAST {} GOLDEN {} (LAST2 {} LAST3 {} BWD {} ALT2 {} ALT {})", | |
| 7533 | + refs[0], refs[3], refs[1], refs[2], refs[4], refs[5], refs[6] | |
| 7534 | + ); | |
| 7269 | 7535 | eprintln!( |
| 7270 | 7536 | "{name}: inter 32x32 split into 16s {split32} of {} blocks ({:.1}%); \ |
| 7271 | 7537 | 16x16 leaves split into 8s {split16} of {} ({:.1}%)", |
crates/ec-av1/src/encoder.rsmodified+24 -0
| @@ -226,6 +226,17 @@ pub struct Av1Encoder { | ||
| 226 | 226 | /// thread-local. One per encoder, never per frame: several of its fields |
| 227 | 227 | /// (the inter-frame inheritance guards) carry state ACROSS frames. |
| 228 | 228 | fctx: crate::decode::FrameCtx, |
| 229 | + /// What the last coded frame stored into the reference slot the next | |
| 230 | + /// inter frame reads (spec 7.20): the tables that frame's tile writer | |
| 231 | + /// must start from, since the header leaves | |
| 232 | + /// `disable_frame_end_update_cdf` off. `None` before the first frame. | |
| 233 | + carried_cdfs: Option<crate::encode::CdfSnapshot>, | |
| 234 | + /// The last key frame's own (padded) reconstruction: `GOLDEN_FRAME`, | |
| 235 | + /// which stays in DPB slot 1 until the next key frame refreshes it. | |
| 236 | + golden: Option<Picture>, | |
| 237 | + /// The frame two back: `ALTREF_FRAME`, in the slot the next frame is | |
| 238 | + /// about to refresh (`encode_inter_frame`'s 0/2 alternation). | |
| 239 | + prev2: Option<Picture>, | |
| 229 | 240 | } |
| 230 | 241 | |
| 231 | 242 | /// The encoder stays `Send` now that it owns a `FrameCtx` (whose cells are |
| @@ -251,6 +262,9 @@ impl Av1Encoder { | ||
| 251 | 262 | // then. |
| 252 | 263 | Picture::grey(config.width, config.height).check_even()?; |
| 253 | 264 | Ok(Self { |
| 265 | + carried_cdfs: None, | |
| 266 | + golden: None, | |
| 267 | + prev2: None, | |
| 254 | 268 | color_config: config.colour.color_config(), |
| 255 | 269 | config, |
| 256 | 270 | reference: None, |
| @@ -355,10 +369,20 @@ impl Av1Encoder { | ||
| 355 | 369 | DEADZONE, |
| 356 | 370 | order as u32, |
| 357 | 371 | render, |
| 372 | + self.carried_cdfs.as_ref().map(|c| &c.0), | |
| 373 | + self.golden.as_ref(), | |
| 374 | + self.prev2.as_ref(), | |
| 358 | 375 | &self.fctx, |
| 359 | 376 | )? |
| 360 | 377 | }; |
| 361 | 378 | |
| 379 | + self.carried_cdfs = Some(encoded.next_cdfs.clone()); | |
| 380 | + if is_key { | |
| 381 | + self.golden = Some(encoded.reconstruction.clone()); | |
| 382 | + self.prev2 = None; | |
| 383 | + } else { | |
| 384 | + self.prev2 = self.reference.take(); | |
| 385 | + } | |
| 362 | 386 | self.reference = Some(encoded.reconstruction.clone()); |
| 363 | 387 | self.next_index += 1; |
| 364 | 388 | let cropped = crop_encoded(&encoded, render.0, render.1); |
crates/ec-av1/src/mvstack.rsmodified+1 -0
| @@ -2399,6 +2399,7 @@ pub struct CompoundTplArgs<'a> { | ||
| 2399 | 2399 | /// two-valued test: "is there an inter neighbour at all". `has_inter` |
| 2400 | 2400 | /// gathers that over the block's whole span, the way spec 5.11.39 reads any |
| 2401 | 2401 | /// neighbour-derived context. |
| 2402 | +#[cfg(test)] | |
| 2402 | 2403 | pub(crate) fn single_ref_ctx(has_inter_neighbour: bool) -> usize { |
| 2403 | 2404 | if has_inter_neighbour { 2 } else { 1 } |
| 2404 | 2405 | } |
crates/ec-av1/src/stream.rsmodified+8 -1
| @@ -3395,7 +3395,13 @@ mod tests { | ||
| 3395 | 3395 | |
| 3396 | 3396 | // Frame 1: hand-coded, intra-only, refreshes slot 0 with a flat |
| 3397 | 3397 | // 128 block (no neighbours to predict `DC_PRED` from). |
| 3398 | - let (_, header1) = inter_frame_headers(width, height, base_q_idx, 1, 0).unwrap(); | |
| 3398 | + let (_, mut header1) = inter_frame_headers(width, height, base_q_idx, 1, 0).unwrap(); | |
| 3399 | + // These two tiles are hand-coded from a FRESH `Cdfs`, so the | |
| 3400 | + // frames must not carry CDF state the way the encoder's own inter | |
| 3401 | + // frames now do (spec 7.20): with the bit set, what each frame | |
| 3402 | + // stores is what it started from -- the defaults -- which is | |
| 3403 | + // exactly what the next hand-coded tile assumes. | |
| 3404 | + header1.disable_frame_end_update_cdf = true; | |
| 3399 | 3405 | let tile1 = { |
| 3400 | 3406 | let mut cdfs = Cdfs::new(q_ctx_of(base_q_idx)); |
| 3401 | 3407 | let mut enc = SymbolEncoder::new(); |
| @@ -3413,6 +3419,7 @@ mod tests { | ||
| 3413 | 3419 | // Frame 2: hand-coded, single_ref all the way to GOLDEN_FRAME, |
| 3414 | 3420 | // skip zero-MV NEARESTMV -- a direct copy of slot 3 (frame 0). |
| 3415 | 3421 | let (_, mut header2) = inter_frame_headers(width, height, base_q_idx, 2, 0).unwrap(); |
| 3422 | + header2.disable_frame_end_update_cdf = true; | |
| 3416 | 3423 | header2.ref_frame_idx[3] = 3; // GOLDEN_FRAME's own slot: frame 0's, untouched by frame 1 |
| 3417 | 3424 | let tile2 = { |
| 3418 | 3425 | let mut cdfs = Cdfs::new(q_ctx_of(base_q_idx)); |
crates/ec-av1/src/tile.rsmodified+297 -118
| @@ -18,7 +18,7 @@ use crate::cdf; | ||
| 18 | 18 | use crate::cdf_state::{Cdfs, MvComponentCdfs, TxbSet, TxbTables}; |
| 19 | 19 | use crate::decode::{TXFM_CTX_INIT, txfm_partition_ctx_rect}; |
| 20 | 20 | use crate::msac::SymbolEncoder; |
| 21 | -use crate::mvstack::{MiGrid, MiInfo, NO_REF1, find_mv_stack, mv16, single_ref_ctx}; | |
| 21 | +use crate::mvstack::{MiGrid, MiInfo, NO_REF1, find_mv_stack, mv16}; | |
| 22 | 22 | |
| 23 | 23 | /// round-4 av1-truesize debugging aid: prints `msg()` to stderr when the |
| 24 | 24 | /// `EC_RNG` environment variable is set, mirroring the `EC_PART`/`EC_TOK` |
| @@ -136,7 +136,7 @@ const MAX_LEVEL: i32 = MAX_BR_LEVEL + (1 << 19); | ||
| 136 | 136 | /// The coefficient q-context (spec 8.3.2's `Get_Qctx`, `Default_..._Cdf`'s |
| 137 | 137 | /// leading index) a frame's `base_q_idx` picks its default CDFs from. |
| 138 | 138 | /// [`crate::cdf`] carries all four, one constant set per context. |
| 139 | -fn q_ctx_of(base_q_idx: u8) -> usize { | |
| 139 | +pub(crate) fn q_ctx_of(base_q_idx: u8) -> usize { | |
| 140 | 140 | match base_q_idx { |
| 141 | 141 | 0..=20 => 0, |
| 142 | 142 | 21..=60 => 1, |
| @@ -488,16 +488,21 @@ pub struct BlockCoeffs { | ||
| 488 | 488 | pub tx_depth: u8, |
| 489 | 489 | } |
| 490 | 490 | |
| 491 | -/// One inter mode [`sb_coeff_inter_frame_tile`]'s blocks may take (spec | |
| 492 | -/// 5.11.24 `read_inter_mode`), reduced to the two branches its symbol chain | |
| 493 | -/// needs to prove out: the stack's own top candidate outright, or a coded | |
| 494 | -/// residual against it. `NEARMV`/`GLOBALMV` are never written. | |
| 491 | +/// One inter mode [`sb_coeff_inter_frame_tile`]'s blocks may take: the whole | |
| 492 | +/// single-reference set of spec 5.11.24 `read_inter_mode`, each written by | |
| 493 | +/// [`write_inter_mode`] with the decoder's own contexts. | |
| 495 | 494 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 496 | 495 | pub enum InterMode { |
| 497 | 496 | /// `NEARESTMV`: takes `MvStack::nearest_mv` outright, no residual, no DRL. |
| 498 | 497 | NearestMv, |
| 499 | - /// `NEWMV`: codes `mv` as a residual against `MvStack::pred_mv`, through | |
| 500 | - /// a DRL index this writer always leaves at zero. | |
| 498 | + /// `NEARMV`: takes the stack entry [`InterInfo::ref_mv_idx`] names | |
| 499 | + /// (clamped to at least 1, the spec's own `RefMvIdx` base for this mode). | |
| 500 | + NearMv, | |
| 501 | + /// `GLOBALMV`: the identity global model's zero vector, two symbols and | |
| 502 | + /// no MV at all. | |
| 503 | + GlobalMv, | |
| 504 | + /// `NEWMV`: codes `mv` as a residual against the stack entry | |
| 505 | + /// [`InterInfo::ref_mv_idx`] names. | |
| 501 | 506 | NewMv, |
| 502 | 507 | } |
| 503 | 508 | |
| @@ -511,6 +516,14 @@ pub struct InterInfo { | ||
| 511 | 516 | pub mode: InterMode, |
| 512 | 517 | /// The motion vector a [`InterMode::NewMv`] block's residual targets. |
| 513 | 518 | pub mv: (i32, i32), |
| 519 | + /// The reference this block predicts from (`LAST_FRAME`..=`ALTREF_FRAME`, | |
| 520 | + /// spec 6.10.24's alphabet), written by [`write_single_ref`]. | |
| 521 | + pub ref_frame: i8, | |
| 522 | + /// The block's `RefMvIdx` (spec 5.11.24 `read_drl_idx`): which | |
| 523 | + /// `MvStack::entries` slot `NEWMV` codes its residual against, or | |
| 524 | + /// `NEARMV` takes outright. Ignored by the two modes that code no DRL | |
| 525 | + /// index (`NEARESTMV`, `GLOBALMV`). | |
| 526 | + pub ref_mv_idx: u8, | |
| 514 | 527 | } |
| 515 | 528 | |
| 516 | 529 | impl From<Vec<Coeff>> for BlockCoeffs { |
| @@ -676,6 +689,12 @@ struct Neighbours { | ||
| 676 | 689 | above_inter: Vec<bool>, |
| 677 | 690 | /// The same, down the left edge. |
| 678 | 691 | left_inter: Vec<bool>, |
| 692 | + /// The reference each neighbour named, `-1` for an intra or unavailable | |
| 693 | + /// one -- what the decoder keeps in its own `Neighbours::above_ref` | |
| 694 | + /// (decode.rs) and feeds `read_single_ref`'s context functions. | |
| 695 | + above_ref: Vec<i8>, | |
| 696 | + /// The same, down the left edge. | |
| 697 | + left_ref: Vec<i8>, | |
| 679 | 698 | /// The frame's true (unpadded) width and height, in 4x4 mode-info units, |
| 680 | 699 | /// which is what clamps [`Self::above`]/[`Self::left`] at the edge (spec |
| 681 | 700 | /// `av1_set_entropy_contexts`): a block whose row or column run spills |
| @@ -719,6 +738,8 @@ impl Neighbours { | ||
| 719 | 738 | left_skip: vec![false; rows * (SUB / MI)], |
| 720 | 739 | above_inter: vec![false; cols * (SUB / MI)], |
| 721 | 740 | left_inter: vec![false; rows * (SUB / MI)], |
| 741 | + above_ref: vec![-1; cols * (SUB / MI)], | |
| 742 | + left_ref: vec![-1; rows * (SUB / MI)], | |
| 722 | 743 | above_tx: vec![0; cols * (SUB / MI)], |
| 723 | 744 | left_tx: vec![0; rows * (SUB / MI)], |
| 724 | 745 | above_txfm: vec![TXFM_CTX_INIT; cols * (SUB / MI)], |
| @@ -736,6 +757,7 @@ impl Neighbours { | ||
| 736 | 757 | self.left_side_mi.iter_mut().for_each(|s| *s = SB); |
| 737 | 758 | self.left_skip.iter_mut().for_each(|s| *s = false); |
| 738 | 759 | self.left_inter.iter_mut().for_each(|i| *i = false); |
| 760 | + self.left_ref.iter_mut().for_each(|r| *r = -1); | |
| 739 | 761 | self.left_tx.iter_mut().for_each(|t| *t = 0); |
| 740 | 762 | self.left_txfm.iter_mut().for_each(|t| *t = TXFM_CTX_INIT); |
| 741 | 763 | } |
| @@ -935,19 +957,21 @@ impl Neighbours { | ||
| 935 | 957 | /// Writes one inter-frame block's skip flag and inter/intra state into |
| 936 | 958 | /// every 16x16 column and row it covers, the same span [`Self::record`] |
| 937 | 959 | /// fills for the coefficient and mode state. |
| 938 | - fn record_inter(&mut self, at: (usize, usize), side: usize, skip: bool, is_inter: bool) { | |
| 960 | + fn record_inter(&mut self, at: (usize, usize), side: usize, skip: bool, is_inter: bool, ref_frame: i8) { | |
| 939 | 961 | let (r, c) = at; |
| 940 | - self.record_inter_mi((r * (SUB / MI), c * (SUB / MI)), side, skip, is_inter); | |
| 962 | + self.record_inter_mi((r * (SUB / MI), c * (SUB / MI)), side, skip, is_inter, ref_frame); | |
| 941 | 963 | } |
| 942 | 964 | |
| 943 | 965 | /// [`Self::record_inter`] taking the block's position directly in 4x4 |
| 944 | 966 | /// mode-info units, for a block finer than one [`SUB`] slot. |
| 945 | - fn record_inter_mi(&mut self, (mi_r, mi_c): (usize, usize), side: usize, skip: bool, is_inter: bool) { | |
| 967 | + fn record_inter_mi(&mut self, (mi_r, mi_c): (usize, usize), side: usize, skip: bool, is_inter: bool, ref_frame: i8) { | |
| 946 | 968 | for cell in 0..side / MI { |
| 947 | 969 | self.above_skip[mi_c + cell] = skip; |
| 948 | 970 | self.left_skip[mi_r + cell] = skip; |
| 949 | 971 | self.above_inter[mi_c + cell] = is_inter; |
| 950 | 972 | self.left_inter[mi_r + cell] = is_inter; |
| 973 | + self.above_ref[mi_c + cell] = ref_frame; | |
| 974 | + self.left_ref[mi_r + cell] = ref_frame; | |
| 951 | 975 | } |
| 952 | 976 | } |
| 953 | 977 | |
| @@ -1040,6 +1064,26 @@ pub fn sb_coeff_key_frame_tile_tx( | ||
| 1040 | 1064 | base_q_idx: u8, |
| 1041 | 1065 | superblocks: &[Superblock], |
| 1042 | 1066 | tx_select: bool, |
| 1067 | +) -> Result<Vec<u8>> { | |
| 1068 | + let mut cdfs = Cdfs::new(q_ctx_of(base_q_idx)); | |
| 1069 | + sb_coeff_key_frame_tile_cdfs(mi_cols, mi_rows, base_q_idx, superblocks, tx_select, &mut cdfs) | |
| 1070 | +} | |
| 1071 | + | |
| 1072 | +/// [`sb_coeff_key_frame_tile_tx`] starting from -- and leaving behind -- the | |
| 1073 | +/// caller's own CDF state, which is what a frame whose | |
| 1074 | +/// `disable_frame_end_update_cdf` is off needs: the next frame's writer must | |
| 1075 | +/// start where this tile ended (spec 7.20, mirrored by | |
| 1076 | +/// `crate::stream::stored_cdfs_for`). | |
| 1077 | +/// | |
| 1078 | +/// # Errors | |
| 1079 | +/// As [`sb_coeff_key_frame_tile_tx`]. | |
| 1080 | +pub(crate) fn sb_coeff_key_frame_tile_cdfs( | |
| 1081 | + mi_cols: u32, | |
| 1082 | + mi_rows: u32, | |
| 1083 | + _base_q_idx: u8, | |
| 1084 | + superblocks: &[Superblock], | |
| 1085 | + tx_select: bool, | |
| 1086 | + cdfs: &mut Cdfs, | |
| 1043 | 1087 | ) -> Result<Vec<u8>> { |
| 1044 | 1088 | check_blocks(mi_cols, mi_rows)?; |
| 1045 | 1089 | let (cols, rows) = block_grid(mi_cols, mi_rows); |
| @@ -1099,7 +1143,7 @@ pub fn sb_coeff_key_frame_tile_tx( | ||
| 1099 | 1143 | // The tile adapts every non-literal CDF it writes, exactly as the decoder |
| 1100 | 1144 | // adapts the ones it reads, so the frame header leaves `disable_cdf_update` |
| 1101 | 1145 | // off. |
| 1102 | - let mut cdfs = Cdfs::new(q_ctx_of(base_q_idx)); | |
| 1146 | + let mut cdfs = cdfs; | |
| 1103 | 1147 | let mut enc = SymbolEncoder::new(); |
| 1104 | 1148 | for sb_r in 0..sb_rows { |
| 1105 | 1149 | neighbours.start_row(); |
| @@ -2666,6 +2710,205 @@ fn write_mv( | ||
| 2666 | 2710 | Ok(()) |
| 2667 | 2711 | } |
| 2668 | 2712 | |
| 2713 | +/// What a block leaves in the neighbour reference bands: its own reference, | |
| 2714 | +/// or `-1` when it is intra (decode.rs `Neighbours::above_ref`'s convention). | |
| 2715 | +fn block_ref(block: &BlockCoeffs) -> i8 { | |
| 2716 | + block.inter.map_or(-1, |i| i.ref_frame) | |
| 2717 | +} | |
| 2718 | + | |
| 2719 | +/// Writer-side counterpart of decode.rs `read_single_ref` (spec 5.11.25's | |
| 2720 | +/// `single_ref_p1`..`p6` tree): codes `ref_frame` bit by bit, each symbol at | |
| 2721 | +/// the context its own `single_ref_p*_ctx` derives from the immediate | |
| 2722 | +/// above/left neighbours' references -- the same functions the decoder calls, | |
| 2723 | +/// with `-1` standing for an intra or unavailable neighbour and no second | |
| 2724 | +/// reference (this writer codes no compound block). | |
| 2725 | +fn write_single_ref( | |
| 2726 | + enc: &mut SymbolEncoder, | |
| 2727 | + cdfs: &mut Cdfs, | |
| 2728 | + ref_frame: i8, | |
| 2729 | + above_ref: i8, | |
| 2730 | + left_ref: i8, | |
| 2731 | +) { | |
| 2732 | + REF_HITS[(ref_frame.max(1) - 1) as usize % 7].fetch_add(1, std::sync::atomic::Ordering::Relaxed); | |
| 2733 | + use crate::mvstack::{ | |
| 2734 | + ALTREF2_FRAME, ALTREF_FRAME, BWDREF_FRAME, GOLDEN_FRAME, LAST2_FRAME, LAST3_FRAME, | |
| 2735 | + single_ref_p1_ctx, single_ref_p2_ctx, single_ref_p3_ctx, single_ref_p4_ctx, | |
| 2736 | + single_ref_p5_ctx, single_ref_p6_ctx, | |
| 2737 | + }; | |
| 2738 | + let above = (above_ref > 0).then_some(above_ref); | |
| 2739 | + let left = (left_ref > 0).then_some(left_ref); | |
| 2740 | + let backward = ref_frame >= BWDREF_FRAME; | |
| 2741 | + enc.symbol( | |
| 2742 | + usize::from(backward), | |
| 2743 | + &mut cdfs.single_ref[single_ref_p1_ctx(above, None, left, None)][0], | |
| 2744 | + ); | |
| 2745 | + if backward { | |
| 2746 | + let is_altref = ref_frame == ALTREF_FRAME; | |
| 2747 | + enc.symbol( | |
| 2748 | + usize::from(is_altref), | |
| 2749 | + &mut cdfs.single_ref[single_ref_p2_ctx(above, None, left, None)][1], | |
| 2750 | + ); | |
| 2751 | + if !is_altref { | |
| 2752 | + enc.symbol( | |
| 2753 | + usize::from(ref_frame == ALTREF2_FRAME), | |
| 2754 | + &mut cdfs.single_ref[single_ref_p6_ctx(above, None, left, None)][5], | |
| 2755 | + ); | |
| 2756 | + } | |
| 2757 | + return; | |
| 2758 | + } | |
| 2759 | + let far = ref_frame == LAST3_FRAME || ref_frame == GOLDEN_FRAME; | |
| 2760 | + enc.symbol( | |
| 2761 | + usize::from(far), | |
| 2762 | + &mut cdfs.single_ref[single_ref_p3_ctx(above, None, left, None)][2], | |
| 2763 | + ); | |
| 2764 | + if far { | |
| 2765 | + enc.symbol( | |
| 2766 | + usize::from(ref_frame == GOLDEN_FRAME), | |
| 2767 | + &mut cdfs.single_ref[single_ref_p5_ctx(above, None, left, None)][4], | |
| 2768 | + ); | |
| 2769 | + } else { | |
| 2770 | + enc.symbol( | |
| 2771 | + usize::from(ref_frame == LAST2_FRAME), | |
| 2772 | + &mut cdfs.single_ref[single_ref_p4_ctx(above, None, left, None)][3], | |
| 2773 | + ); | |
| 2774 | + } | |
| 2775 | +} | |
| 2776 | + | |
| 2777 | +/// Per-mode fire counts of [`write_inter_mode`], indexed | |
| 2778 | +/// `NEARESTMV`/`NEARMV`/`GLOBALMV`/`NEWMV`, and the DRL index each block | |
| 2779 | +/// coded (bucket 3 collects any index past 2). Read by the BD gate, which | |
| 2780 | +/// is the only thing that proves a newly added mode ever fires | |
| 2781 | +/// (gate-blind-to-feature). | |
| 2782 | +static INTER_MODE_HITS: [std::sync::atomic::AtomicUsize; 4] = [ | |
| 2783 | + std::sync::atomic::AtomicUsize::new(0), | |
| 2784 | + std::sync::atomic::AtomicUsize::new(0), | |
| 2785 | + std::sync::atomic::AtomicUsize::new(0), | |
| 2786 | + std::sync::atomic::AtomicUsize::new(0), | |
| 2787 | +]; | |
| 2788 | +static DRL_HITS: [std::sync::atomic::AtomicUsize; 4] = [ | |
| 2789 | + std::sync::atomic::AtomicUsize::new(0), | |
| 2790 | + std::sync::atomic::AtomicUsize::new(0), | |
| 2791 | + std::sync::atomic::AtomicUsize::new(0), | |
| 2792 | + std::sync::atomic::AtomicUsize::new(0), | |
| 2793 | +]; | |
| 2794 | + | |
| 2795 | +/// One counter per reference name (`LAST`..`ALTREF`), for the gate's own | |
| 2796 | +/// census of what the encoder actually picks. | |
| 2797 | +static REF_HITS: [std::sync::atomic::AtomicUsize; 7] = [ | |
| 2798 | + std::sync::atomic::AtomicUsize::new(0), | |
| 2799 | + std::sync::atomic::AtomicUsize::new(0), | |
| 2800 | + std::sync::atomic::AtomicUsize::new(0), | |
| 2801 | + std::sync::atomic::AtomicUsize::new(0), | |
| 2802 | + std::sync::atomic::AtomicUsize::new(0), | |
| 2803 | + std::sync::atomic::AtomicUsize::new(0), | |
| 2804 | + std::sync::atomic::AtomicUsize::new(0), | |
| 2805 | +]; | |
| 2806 | + | |
| 2807 | +/// Takes and clears the per-reference histogram. | |
| 2808 | +#[cfg(test)] | |
| 2809 | +pub(crate) fn take_ref_hits() -> [usize; 7] { | |
| 2810 | + std::array::from_fn(|i| REF_HITS[i].swap(0, std::sync::atomic::Ordering::Relaxed)) | |
| 2811 | +} | |
| 2812 | + | |
| 2813 | +/// Bumps the two histograms for one coded inter block. | |
| 2814 | +fn note_inter_mode(mode: usize, drl: usize) { | |
| 2815 | + INTER_MODE_HITS[mode].fetch_add(1, std::sync::atomic::Ordering::Relaxed); | |
| 2816 | + DRL_HITS[drl.min(3)].fetch_add(1, std::sync::atomic::Ordering::Relaxed); | |
| 2817 | +} | |
| 2818 | + | |
| 2819 | +/// Takes and clears the inter-mode histogram (NEAREST/NEAR/GLOBAL/NEW). | |
| 2820 | +#[cfg(test)] | |
| 2821 | +pub(crate) fn take_inter_mode_hits() -> [usize; 4] { | |
| 2822 | + [0, 1, 2, 3].map(|i| INTER_MODE_HITS[i].swap(0, std::sync::atomic::Ordering::Relaxed)) | |
| 2823 | +} | |
| 2824 | + | |
| 2825 | +/// Takes and clears the DRL-index histogram (0, 1, 2, 3+). | |
| 2826 | +#[cfg(test)] | |
| 2827 | +pub(crate) fn take_drl_hits() -> [usize; 4] { | |
| 2828 | + [0, 1, 2, 3].map(|i| DRL_HITS[i].swap(0, std::sync::atomic::Ordering::Relaxed)) | |
| 2829 | +} | |
| 2830 | + | |
| 2831 | +/// Writes the DRL index of one block: the loop the decoder runs (spec | |
| 2832 | +/// 5.11.24 `read_drl_idx`, decode.rs' two copies at ~26655 and ~26695), one | |
| 2833 | +/// `drl_mode` bit per stack entry past `start`, `1` to advance and `0` to | |
| 2834 | +/// stop, at most two bits. `drl_ctx[idx]` is the context between | |
| 2835 | +/// `entries[idx]` and `entries[idx + 1]`, exactly the pair the bit chooses | |
| 2836 | +/// between. | |
| 2837 | +fn write_drl_idx( | |
| 2838 | + enc: &mut SymbolEncoder, | |
| 2839 | + cdfs: &mut Cdfs, | |
| 2840 | + stack: &crate::mvstack::MvStack, | |
| 2841 | + start: usize, | |
| 2842 | + target: usize, | |
| 2843 | +) { | |
| 2844 | + let mut idx = start; | |
| 2845 | + while idx < start + 2 && stack.entries.len() > idx + 1 { | |
| 2846 | + let advance = idx < target; | |
| 2847 | + enc.symbol(usize::from(advance), &mut cdfs.drl_mode[stack.drl_ctx[idx]]); | |
| 2848 | + if !advance { | |
| 2849 | + return; | |
| 2850 | + } | |
| 2851 | + idx += 1; | |
| 2852 | + } | |
| 2853 | +} | |
| 2854 | + | |
| 2855 | +/// Writer-side counterpart of the decoder's single-reference | |
| 2856 | +/// `read_inter_mode`/`assign_mv` chain (decode.rs ~26645): writes `info`'s | |
| 2857 | +/// mode symbols against `stack`'s own contexts and, for `NEWMV`, the MV | |
| 2858 | +/// residual against the DRL-selected predictor. Returns the motion vector | |
| 2859 | +/// the decoder will derive for the block (what the MI grid must record) and | |
| 2860 | +/// whether it is `NEWMV`. | |
| 2861 | +/// | |
| 2862 | +/// `GLOBALMV` resolves to `(0, 0)` because this encoder writes no global | |
| 2863 | +/// motion parameters at all, so every `gm_get_motion_vector` the decoder | |
| 2864 | +/// runs is the identity model's zero vector; a lane that starts writing | |
| 2865 | +/// real `gm_params` has to feed the same vector in here. | |
| 2866 | +/// | |
| 2867 | +/// # Errors | |
| 2868 | +/// As [`write_mv`]: a `NEWMV` residual needing eighth-pel precision. | |
| 2869 | +fn write_inter_mode( | |
| 2870 | + enc: &mut SymbolEncoder, | |
| 2871 | + cdfs: &mut Cdfs, | |
| 2872 | + info: InterInfo, | |
| 2873 | + stack: &crate::mvstack::MvStack, | |
| 2874 | +) -> Result<((i32, i32), bool)> { | |
| 2875 | + let idx = usize::from(info.ref_mv_idx); | |
| 2876 | + match info.mode { | |
| 2877 | + InterMode::NewMv => { | |
| 2878 | + enc.symbol(0, &mut cdfs.new_mv[stack.new_mv_ctx]); | |
| 2879 | + write_drl_idx(enc, cdfs, stack, 0, idx); | |
| 2880 | + let base = stack.entries.get(idx).map_or(stack.pred_mv, |e| e.mv); | |
| 2881 | + write_mv(enc, &mut cdfs.mv_comp, &mut cdfs.mv_joint, info.mv, base)?; | |
| 2882 | + note_inter_mode(3, idx); | |
| 2883 | + Ok((info.mv, true)) | |
| 2884 | + } | |
| 2885 | + InterMode::GlobalMv => { | |
| 2886 | + enc.symbol(1, &mut cdfs.new_mv[stack.new_mv_ctx]); // not NEWMV | |
| 2887 | + enc.symbol(0, &mut cdfs.zero_mv[stack.zero_mv_ctx]); // GLOBALMV | |
| 2888 | + note_inter_mode(2, 0); | |
| 2889 | + Ok(((0, 0), false)) | |
| 2890 | + } | |
| 2891 | + InterMode::NearestMv => { | |
| 2892 | + enc.symbol(1, &mut cdfs.new_mv[stack.new_mv_ctx]); // not NEWMV | |
| 2893 | + enc.symbol(1, &mut cdfs.zero_mv[stack.zero_mv_ctx]); // not GLOBALMV | |
| 2894 | + enc.symbol(0, &mut cdfs.ref_mv[stack.ref_mv_ctx]); // NEARESTMV | |
| 2895 | + note_inter_mode(0, 0); | |
| 2896 | + Ok((stack.nearest_mv, false)) | |
| 2897 | + } | |
| 2898 | + InterMode::NearMv => { | |
| 2899 | + enc.symbol(1, &mut cdfs.new_mv[stack.new_mv_ctx]); // not NEWMV | |
| 2900 | + enc.symbol(1, &mut cdfs.zero_mv[stack.zero_mv_ctx]); // not GLOBALMV | |
| 2901 | + enc.symbol(1, &mut cdfs.ref_mv[stack.ref_mv_ctx]); // NEARMV | |
| 2902 | + // `RefMvIdx` starts at 1 for NEARMV (spec 5.11.24). | |
| 2903 | + let idx = idx.max(1); | |
| 2904 | + write_drl_idx(enc, cdfs, stack, 1, idx); | |
| 2905 | + let mv = stack.entries.get(idx).map_or(stack.near_mv, |e| e.mv); | |
| 2906 | + note_inter_mode(1, idx); | |
| 2907 | + Ok((mv, false)) | |
| 2908 | + } | |
| 2909 | + } | |
| 2910 | +} | |
| 2911 | + | |
| 2669 | 2912 | /// Writes the payload of a one-tile inter frame built from `blocks`, one |
| 2670 | 2913 | /// 32x32 block per entry in raster order across the frame — every superblock |
| 2671 | 2914 | /// is split into its four quadrants the way [`split_coeff_key_frame_tile`] |
| @@ -2718,6 +2961,23 @@ pub fn sb_coeff_inter_frame_tile_tx( | ||
| 2718 | 2961 | base_q_idx: u8, |
| 2719 | 2962 | blocks: &[Quadrant], |
| 2720 | 2963 | tx_select: bool, |
| 2964 | +) -> Result<Vec<u8>> { | |
| 2965 | + let mut cdfs = Cdfs::new(q_ctx_of(base_q_idx)); | |
| 2966 | + sb_coeff_inter_frame_tile_cdfs(mi_cols, mi_rows, base_q_idx, blocks, tx_select, &mut cdfs) | |
| 2967 | +} | |
| 2968 | + | |
| 2969 | +/// [`sb_coeff_inter_frame_tile_tx`] starting from -- and leaving behind -- | |
| 2970 | +/// the caller's own CDF state; see [`sb_coeff_key_frame_tile_cdfs`]. | |
| 2971 | +/// | |
| 2972 | +/// # Errors | |
| 2973 | +/// As [`sb_coeff_inter_frame_tile_tx`]. | |
| 2974 | +pub(crate) fn sb_coeff_inter_frame_tile_cdfs( | |
| 2975 | + mi_cols: u32, | |
| 2976 | + mi_rows: u32, | |
| 2977 | + _base_q_idx: u8, | |
| 2978 | + blocks: &[Quadrant], | |
| 2979 | + tx_select: bool, | |
| 2980 | + cdfs: &mut Cdfs, | |
| 2721 | 2981 | ) -> Result<Vec<u8>> { |
| 2722 | 2982 | check_blocks(mi_cols, mi_rows)?; |
| 2723 | 2983 | // `block_grid`'s ceiling, not a plain division: a true frame size that is |
| @@ -2748,9 +3008,6 @@ pub fn sb_coeff_inter_frame_tile_tx( | ||
| 2748 | 3008 | /// `Y_MODE`'s size group (spec `Size_Group`) for a 32x32 block, the only |
| 2749 | 3009 | /// size this writer's inter branch codes. |
| 2750 | 3010 | const SIZE_GROUP_32: usize = 3; |
| 2751 | - /// `Ref_Frame_List`'s `LAST_FRAME` (spec 3): the only reference this | |
| 2752 | - /// writer's single-reference chain ever names. | |
| 2753 | - const LAST_FRAME: i8 = 1; | |
| 2754 | 3011 | |
| 2755 | 3012 | let (sb_cols, sb_rows) = (cols.div_ceil(2), rows.div_ceil(2)); |
| 2756 | 3013 | let mut neighbours = Neighbours::new( |
| @@ -2760,7 +3017,7 @@ pub fn sb_coeff_inter_frame_tile_tx( | ||
| 2760 | 3017 | mi_rows as usize, |
| 2761 | 3018 | ); |
| 2762 | 3019 | let mut grid = MiGrid::new(mi_cols as usize, mi_rows as usize); |
| 2763 | - let mut cdfs = Cdfs::new(q_ctx_of(base_q_idx)); | |
| 3020 | + let mut cdfs = cdfs; | |
| 2764 | 3021 | let mut enc = SymbolEncoder::new(); |
| 2765 | 3022 | // An `is_inter` block's 32x32 luma transform reads a different |
| 2766 | 3023 | // `tx_type` set than an intra block's (`get_tx_set`, spec 5.11.48; see |
| @@ -3045,10 +3302,7 @@ pub fn sb_coeff_inter_frame_tile_tx( | ||
| 3045 | 3302 | |
| 3046 | 3303 | let mode_for_tx; |
| 3047 | 3304 | if let Some(info) = block.inter { |
| 3048 | - let sr_ctx = single_ref_ctx(above_inter || left_inter); | |
| 3049 | - enc.symbol(0, &mut cdfs.single_ref[sr_ctx][0]); // p1: forward | |
| 3050 | - enc.symbol(0, &mut cdfs.single_ref[sr_ctx][2]); // p3: LAST/LAST2 | |
| 3051 | - enc.symbol(0, &mut cdfs.single_ref[sr_ctx][3]); // p4: LAST | |
| 3305 | + write_single_ref(&mut enc, &mut cdfs, info.ref_frame, neighbours.above_ref[mi_c], neighbours.left_ref[mi_r]); | |
| 3052 | 3306 | |
| 3053 | 3307 | let (mi_row, mi_col) = (r32 as usize * 8, c32 as usize * 8); |
| 3054 | 3308 | let stack = find_mv_stack( |
| @@ -3057,43 +3311,18 @@ pub fn sb_coeff_inter_frame_tile_tx( | ||
| 3057 | 3311 | mi_col, |
| 3058 | 3312 | 8, |
| 3059 | 3313 | 8, |
| 3060 | - LAST_FRAME, | |
| 3314 | + info.ref_frame, | |
| 3061 | 3315 | mi_cols as usize, |
| 3062 | 3316 | mi_rows as usize, |
| 3063 | 3317 | ); |
| 3064 | 3318 | |
| 3065 | - let is_new_mv = matches!(info.mode, InterMode::NewMv); | |
| 3066 | - if is_new_mv { | |
| 3067 | - enc.symbol(0, &mut cdfs.new_mv[stack.new_mv_ctx]); // NEWMV | |
| 3068 | - } else { | |
| 3069 | - enc.symbol(1, &mut cdfs.new_mv[stack.new_mv_ctx]); // not new | |
| 3070 | - enc.symbol(1, &mut cdfs.zero_mv[stack.zero_mv_ctx]); // not zero | |
| 3071 | - enc.symbol(0, &mut cdfs.ref_mv[stack.ref_mv_ctx]); // NEARESTMV | |
| 3072 | - } | |
| 3073 | - // This writer always keeps the first DRL candidate, so the | |
| 3074 | - // loop (spec 5.11.24 `read_drl_idx`) only ever runs its | |
| 3075 | - // first, breaking iteration. | |
| 3076 | - if is_new_mv && stack.entries.len() > 1 { | |
| 3077 | - enc.symbol(0, &mut cdfs.drl_mode[stack.drl_ctx[0]]); | |
| 3078 | - } | |
| 3079 | - let mv = if is_new_mv { | |
| 3080 | - write_mv( | |
| 3081 | - &mut enc, | |
| 3082 | - &mut cdfs.mv_comp, | |
| 3083 | - &mut cdfs.mv_joint, | |
| 3084 | - info.mv, | |
| 3085 | - stack.pred_mv, | |
| 3086 | - )?; | |
| 3087 | - info.mv | |
| 3088 | - } else { | |
| 3089 | - stack.nearest_mv | |
| 3090 | - }; | |
| 3319 | + let (mv, is_new_mv) = write_inter_mode(&mut enc, &mut cdfs, info, &stack)?; | |
| 3091 | 3320 | grid.set( |
| 3092 | 3321 | mi_row, |
| 3093 | 3322 | mi_col, |
| 3094 | 3323 | MiInfo { |
| 3095 | 3324 | is_inter: true, |
| 3096 | - ref_frame: LAST_FRAME, | |
| 3325 | + ref_frame: info.ref_frame, | |
| 3097 | 3326 | ref_frame1: NO_REF1, |
| 3098 | 3327 | mv1: (0, 0), |
| 3099 | 3328 | mv: mv16(mv), |
| @@ -3114,7 +3343,7 @@ pub fn sb_coeff_inter_frame_tile_tx( | ||
| 3114 | 3343 | mi_col + dc, |
| 3115 | 3344 | MiInfo { |
| 3116 | 3345 | is_inter: true, |
| 3117 | - ref_frame: LAST_FRAME, | |
| 3346 | + ref_frame: info.ref_frame, | |
| 3118 | 3347 | ref_frame1: NO_REF1, |
| 3119 | 3348 | mv1: (0, 0), |
| 3120 | 3349 | mv: mv16(mv), |
| @@ -3219,7 +3448,7 @@ pub fn sb_coeff_inter_frame_tile_tx( | ||
| 3219 | 3448 | ); |
| 3220 | 3449 | neighbours.record_planes(at, BLOCK, mode_for_tx, &grids, !split); |
| 3221 | 3450 | } |
| 3222 | - neighbours.record_inter(at, BLOCK, block.skip, is_inter); | |
| 3451 | + neighbours.record_inter(at, BLOCK, block.skip, is_inter, block_ref(block)); | |
| 3223 | 3452 | } |
| 3224 | 3453 | } |
| 3225 | 3454 | } |
| @@ -3263,9 +3492,6 @@ fn write_inter_frame_leaf( | ||
| 3263 | 3492 | /// `Y_MODE`'s size group (spec `Size_Group`, `common_data.h`'s |
| 3264 | 3493 | /// `size_group_lookup[BLOCK_16X16]`) for this leaf's own size. |
| 3265 | 3494 | const SIZE_GROUP_16: usize = 2; |
| 3266 | - /// `Ref_Frame_List`'s `LAST_FRAME` (spec 3): the only reference this | |
| 3267 | - /// writer's single-reference chain ever names, same as the 32x32 branch. | |
| 3268 | - const LAST_FRAME: i8 = 1; | |
| 3269 | 3495 | |
| 3270 | 3496 | let (r, c) = at; |
| 3271 | 3497 | let (mi_r, mi_c) = (r * (SUB / MI), c * (SUB / MI)); |
| @@ -3280,10 +3506,7 @@ fn write_inter_frame_leaf( | ||
| 3280 | 3506 | |
| 3281 | 3507 | let mode_for_tx; |
| 3282 | 3508 | if let Some(info) = block.inter { |
| 3283 | - let sr_ctx = single_ref_ctx(above_inter || left_inter); | |
| 3284 | - enc.symbol(0, &mut cdfs.single_ref[sr_ctx][0]); // p1: forward | |
| 3285 | - enc.symbol(0, &mut cdfs.single_ref[sr_ctx][2]); // p3: LAST/LAST2 | |
| 3286 | - enc.symbol(0, &mut cdfs.single_ref[sr_ctx][3]); // p4: LAST | |
| 3509 | + write_single_ref(enc, cdfs, info.ref_frame, neighbours.above_ref[mi_c], neighbours.left_ref[mi_r]); | |
| 3287 | 3510 | |
| 3288 | 3511 | let (mi_row, mi_col) = (r * SUB_MI as usize, c * SUB_MI as usize); |
| 3289 | 3512 | let stack = find_mv_stack( |
| @@ -3292,34 +3515,12 @@ fn write_inter_frame_leaf( | ||
| 3292 | 3515 | mi_col, |
| 3293 | 3516 | SUB_MI as usize, |
| 3294 | 3517 | SUB_MI as usize, |
| 3295 | - LAST_FRAME, | |
| 3518 | + info.ref_frame, | |
| 3296 | 3519 | mi_cols as usize, |
| 3297 | 3520 | mi_rows as usize, |
| 3298 | 3521 | ); |
| 3299 | 3522 | |
| 3300 | - let is_new_mv = matches!(info.mode, InterMode::NewMv); | |
| 3301 | - if is_new_mv { | |
| 3302 | - enc.symbol(0, &mut cdfs.new_mv[stack.new_mv_ctx]); // NEWMV | |
| 3303 | - } else { | |
| 3304 | - enc.symbol(1, &mut cdfs.new_mv[stack.new_mv_ctx]); // not new | |
| 3305 | - enc.symbol(1, &mut cdfs.zero_mv[stack.zero_mv_ctx]); // not zero | |
| 3306 | - enc.symbol(0, &mut cdfs.ref_mv[stack.ref_mv_ctx]); // NEARESTMV | |
| 3307 | - } | |
| 3308 | - if is_new_mv && stack.entries.len() > 1 { | |
| 3309 | - enc.symbol(0, &mut cdfs.drl_mode[stack.drl_ctx[0]]); | |
| 3310 | - } | |
| 3311 | - let mv = if is_new_mv { | |
| 3312 | - write_mv( | |
| 3313 | - enc, | |
| 3314 | - &mut cdfs.mv_comp, | |
| 3315 | - &mut cdfs.mv_joint, | |
| 3316 | - info.mv, | |
| 3317 | - stack.pred_mv, | |
| 3318 | - )?; | |
| 3319 | - info.mv | |
| 3320 | - } else { | |
| 3321 | - stack.nearest_mv | |
| 3322 | - }; | |
| 3523 | + let (mv, is_new_mv) = write_inter_mode(enc, cdfs, info, &stack)?; | |
| 3323 | 3524 | for dr in 0..SUB_MI as usize { |
| 3324 | 3525 | for dc in 0..SUB_MI as usize { |
| 3325 | 3526 | grid.set( |
| @@ -3327,7 +3528,7 @@ fn write_inter_frame_leaf( | ||
| 3327 | 3528 | mi_col + dc, |
| 3328 | 3529 | MiInfo { |
| 3329 | 3530 | is_inter: true, |
| 3330 | - ref_frame: LAST_FRAME, | |
| 3531 | + ref_frame: info.ref_frame, | |
| 3331 | 3532 | ref_frame1: NO_REF1, |
| 3332 | 3533 | mv1: (0, 0), |
| 3333 | 3534 | mv: mv16(mv), |
| @@ -3428,7 +3629,7 @@ fn write_inter_frame_leaf( | ||
| 3428 | 3629 | ); |
| 3429 | 3630 | neighbours.record_planes(at, SUB, mode_for_tx, &grids, !split); |
| 3430 | 3631 | } |
| 3431 | - neighbours.record_inter(at, SUB, block.skip, is_inter); | |
| 3632 | + neighbours.record_inter(at, SUB, block.skip, is_inter, block_ref(block)); | |
| 3432 | 3633 | Ok(()) |
| 3433 | 3634 | } |
| 3434 | 3635 | |
| @@ -3476,9 +3677,6 @@ fn write_inter_frame_leaf8( | ||
| 3476 | 3677 | /// `Y_MODE`'s size group (spec `Size_Group`, `common_data.h`'s |
| 3477 | 3678 | /// `size_group_lookup[BLOCK_8X8]`) for this leaf's own size. |
| 3478 | 3679 | const SIZE_GROUP_8: usize = 1; |
| 3479 | - /// `Ref_Frame_List`'s `LAST_FRAME` (spec 3), same as the 16x16/32x32 | |
| 3480 | - /// branches. | |
| 3481 | - const LAST_FRAME: i8 = 1; | |
| 3482 | 3680 | |
| 3483 | 3681 | // This leaf's own 4x4 mode-info cells, not the enclosing 16x16 slot's: |
| 3484 | 3682 | // under a real PARTITION_SPLIT all four leaves are coded, so the |
| @@ -3500,10 +3698,7 @@ fn write_inter_frame_leaf8( | ||
| 3500 | 3698 | |
| 3501 | 3699 | let mode_for_tx; |
| 3502 | 3700 | if let Some(info) = block.inter { |
| 3503 | - let sr_ctx = single_ref_ctx(above_inter || left_inter); | |
| 3504 | - enc.symbol(0, &mut cdfs.single_ref[sr_ctx][0]); // p1: forward | |
| 3505 | - enc.symbol(0, &mut cdfs.single_ref[sr_ctx][2]); // p3: LAST/LAST2 | |
| 3506 | - enc.symbol(0, &mut cdfs.single_ref[sr_ctx][3]); // p4: LAST | |
| 3701 | + write_single_ref(enc, cdfs, info.ref_frame, neighbours.above_ref[leaf_mi.1], neighbours.left_ref[leaf_mi.0]); | |
| 3507 | 3702 | |
| 3508 | 3703 | let (mi_row, mi_col) = leaf_mi; |
| 3509 | 3704 | let stack = find_mv_stack( |
| @@ -3512,34 +3707,12 @@ fn write_inter_frame_leaf8( | ||
| 3512 | 3707 | mi_col, |
| 3513 | 3708 | 2, |
| 3514 | 3709 | 2, |
| 3515 | - LAST_FRAME, | |
| 3710 | + info.ref_frame, | |
| 3516 | 3711 | mi_cols as usize, |
| 3517 | 3712 | mi_rows as usize, |
| 3518 | 3713 | ); |
| 3519 | 3714 | |
| 3520 | - let is_new_mv = matches!(info.mode, InterMode::NewMv); | |
| 3521 | - if is_new_mv { | |
| 3522 | - enc.symbol(0, &mut cdfs.new_mv[stack.new_mv_ctx]); // NEWMV | |
| 3523 | - } else { | |
| 3524 | - enc.symbol(1, &mut cdfs.new_mv[stack.new_mv_ctx]); // not new | |
| 3525 | - enc.symbol(1, &mut cdfs.zero_mv[stack.zero_mv_ctx]); // not zero | |
| 3526 | - enc.symbol(0, &mut cdfs.ref_mv[stack.ref_mv_ctx]); // NEARESTMV | |
| 3527 | - } | |
| 3528 | - if is_new_mv && stack.entries.len() > 1 { | |
| 3529 | - enc.symbol(0, &mut cdfs.drl_mode[stack.drl_ctx[0]]); | |
| 3530 | - } | |
| 3531 | - let mv = if is_new_mv { | |
| 3532 | - write_mv( | |
| 3533 | - enc, | |
| 3534 | - &mut cdfs.mv_comp, | |
| 3535 | - &mut cdfs.mv_joint, | |
| 3536 | - info.mv, | |
| 3537 | - stack.pred_mv, | |
| 3538 | - )?; | |
| 3539 | - info.mv | |
| 3540 | - } else { | |
| 3541 | - stack.nearest_mv | |
| 3542 | - }; | |
| 3715 | + let (mv, is_new_mv) = write_inter_mode(enc, cdfs, info, &stack)?; | |
| 3543 | 3716 | for dr in 0..2 { |
| 3544 | 3717 | for dc in 0..2 { |
| 3545 | 3718 | grid.set( |
| @@ -3547,7 +3720,7 @@ fn write_inter_frame_leaf8( | ||
| 3547 | 3720 | mi_col + dc, |
| 3548 | 3721 | MiInfo { |
| 3549 | 3722 | is_inter: true, |
| 3550 | - ref_frame: LAST_FRAME, | |
| 3723 | + ref_frame: info.ref_frame, | |
| 3551 | 3724 | ref_frame1: NO_REF1, |
| 3552 | 3725 | mv1: (0, 0), |
| 3553 | 3726 | mv: mv16(mv), |
| @@ -3625,7 +3798,7 @@ fn write_inter_frame_leaf8( | ||
| 3625 | 3798 | vec![0i32; TX4 * TX4], |
| 3626 | 3799 | ]; |
| 3627 | 3800 | neighbours.record_mi(leaf_mi, 8, &zero_grids); |
| 3628 | - neighbours.record_inter_mi(leaf_mi, 8, block.skip, block.inter.is_some()); | |
| 3801 | + neighbours.record_inter_mi(leaf_mi, 8, block.skip, block.inter.is_some(), block_ref(block)); | |
| 3629 | 3802 | } else { |
| 3630 | 3803 | let grids = [ |
| 3631 | 3804 | level_grid(&block.luma, TX8)?, |
| @@ -3648,7 +3821,7 @@ fn write_inter_frame_leaf8( | ||
| 3648 | 3821 | split, |
| 3649 | 3822 | ); |
| 3650 | 3823 | neighbours.record_mi_planes(leaf_mi, 8, &grids, !split); |
| 3651 | - neighbours.record_inter_mi(leaf_mi, 8, block.skip, is_inter); | |
| 3824 | + neighbours.record_inter_mi(leaf_mi, 8, block.skip, is_inter, block_ref(block)); | |
| 3652 | 3825 | } |
| 3653 | 3826 | Ok((block.skip, is_inter)) |
| 3654 | 3827 | } |
| @@ -5816,7 +5989,7 @@ mod tests { | ||
| 5816 | 5989 | let is_inter = dec.symbol(&mut cdfs.intra_inter[ii_ctx]) == 1; |
| 5817 | 5990 | |
| 5818 | 5991 | let (mode, mv) = if is_inter { |
| 5819 | - let sr_ctx = single_ref_ctx(above_inter[c32] || left_inter[r32]); | |
| 5992 | + let sr_ctx = crate::mvstack::single_ref_ctx(above_inter[c32] || left_inter[r32]); | |
| 5820 | 5993 | count += 3; |
| 5821 | 5994 | assert_eq!( |
| 5822 | 5995 | dec.symbol(&mut cdfs.single_ref[sr_ctx][0]), |
| @@ -5930,8 +6103,10 @@ mod tests { | ||
| 5930 | 6103 | fn an_all_skip_inter_superblock_is_small_and_reads_back() { |
| 5931 | 6104 | let block = BlockCoeffs { |
| 5932 | 6105 | inter: Some(InterInfo { |
| 6106 | + ref_frame: crate::mvstack::LAST_FRAME, | |
| 5933 | 6107 | mode: InterMode::NearestMv, |
| 5934 | 6108 | mv: (0, 0), |
| 6109 | + ref_mv_idx: 0, | |
| 5935 | 6110 | }), |
| 5936 | 6111 | skip: true, |
| 5937 | 6112 | ..BlockCoeffs::default() |
| @@ -5971,16 +6146,20 @@ mod tests { | ||
| 5971 | 6146 | fn a_newmv_block_reads_back_the_exact_symbol_sequence_it_was_written_with() { |
| 5972 | 6147 | let nearest = BlockCoeffs { |
| 5973 | 6148 | inter: Some(InterInfo { |
| 6149 | + ref_frame: crate::mvstack::LAST_FRAME, | |
| 5974 | 6150 | mode: InterMode::NearestMv, |
| 5975 | 6151 | mv: (0, 0), |
| 6152 | + ref_mv_idx: 0, | |
| 5976 | 6153 | }), |
| 5977 | 6154 | skip: true, |
| 5978 | 6155 | ..BlockCoeffs::default() |
| 5979 | 6156 | }; |
| 5980 | 6157 | let newmv = BlockCoeffs { |
| 5981 | 6158 | inter: Some(InterInfo { |
| 6159 | + ref_frame: crate::mvstack::LAST_FRAME, | |
| 5982 | 6160 | mode: InterMode::NewMv, |
| 5983 | 6161 | mv: (0, 2), |
| 6162 | + ref_mv_idx: 0, | |
| 5984 | 6163 | }), |
| 5985 | 6164 | skip: true, |
| 5986 | 6165 | ..BlockCoeffs::default() |