A real GOLDEN reference beside LAST
Browse files at this commit2f069ae4a17da33d971d179a30db52925bf7e750Parent: f5cb29a- feat
- a real GOLDEN reference beside LAST
4 changed files +176 -24
- crates/ec-av1/src/decode.rs +13 -2
- crates/ec-av1/src/encode.rs +136 -22
- crates/ec-av1/src/encoder.rs +8 -0
- crates/ec-av1/src/tile.rs +19 -0
crates/ec-av1/src/decode.rsmodified+13 -2
| @@ -35637,6 +35637,17 @@ mod tests { | |||
| 35637 | RefPix::ready(refs) | 35637 | RefPix::ready(refs) |
| 35638 | } | 35638 | } |
| 35639 | 35639 | ||
| 35640 | + /// [`last_only`] plus `GOLDEN_FRAME` (index 4 of the name-keyed array): | ||
| 35641 | + /// what a frame this encoder writes needs, since every inter frame's | ||
| 35642 | + /// `ref_frame_idx[3]` names the slot the key frame left behind and its | ||
| 35643 | + /// blocks may code `single_ref` out to it. | ||
| 35644 | + fn last_and_golden<'a>(last: &'a Picture, golden: &'a Picture) -> RefPix<'a> { | ||
| 35645 | + let mut refs: [Option<&Picture>; 8] = [None; 8]; | ||
| 35646 | + refs[1] = Some(last); | ||
| 35647 | + refs[4] = Some(golden); | ||
| 35648 | + RefPix::ready(refs) | ||
| 35649 | + } | ||
| 35650 | + | ||
| 35640 | 35651 | ||
| 35641 | /// lane-perf6 step 2: the CDEF kernel now reads a pre-gathered window and | 35652 | /// lane-perf6 step 2: the CDEF kernel now reads a pre-gathered window and |
| 35642 | /// a pre-resolved damping shift instead of a bounds-testing closure and a | 35653 | /// a pre-resolved damping shift instead of a bounds-testing closure and a |
| @@ -37164,7 +37175,7 @@ mod tests { | |||
| 37164 | frame.base_q_idx, | 37175 | frame.base_q_idx, |
| 37165 | width as u32, | 37176 | width as u32, |
| 37166 | height as u32, | 37177 | height as u32, |
| 37167 | - &last_only(&reference), | 37178 | + &last_and_golden(&reference, &encoded.frames[0].reconstruction), |
| 37168 | &frame.cdef, | 37179 | &frame.cdef, |
| 37169 | &frame.loop_filter, | 37180 | &frame.loop_filter, |
| 37170 | false, | 37181 | false, |
| @@ -37476,7 +37487,7 @@ mod tests { | |||
| 37476 | frame.base_q_idx, | 37487 | frame.base_q_idx, |
| 37477 | coded_w, | 37488 | coded_w, |
| 37478 | coded_h, | 37489 | coded_h, |
| 37479 | - &last_only(&reference), | 37490 | + &last_and_golden(&reference, &encoded.frames[0].reconstruction), |
| 37480 | &frame.cdef, | 37491 | &frame.cdef, |
| 37481 | &frame.loop_filter, | 37492 | &frame.loop_filter, |
| 37482 | false, | 37493 | false, |
crates/ec-av1/src/encode.rsmodified+136 -22
| @@ -713,6 +713,11 @@ pub(crate) fn key_frame_headers_colour( | |||
| 713 | Ok((seq, header)) | 713 | Ok((seq, header)) |
| 714 | } | 714 | } |
| 715 | 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 | + | ||
| 716 | /// The frame header for a shown inter frame that predicts from the single | 721 | /// The frame header for a shown inter frame that predicts from the single |
| 717 | /// slot `last_slot` (every `ref_frame_idx` entry names it) and refreshes that | 722 | /// slot `last_slot` (every `ref_frame_idx` entry names it) and refreshes that |
| 718 | /// same slot with itself once coded. The sequence header is [`key_frame_headers`]'s, | 723 | /// same slot with itself once coded. The sequence header is [`key_frame_headers`]'s, |
| @@ -742,7 +747,16 @@ pub fn inter_frame_headers( | |||
| 742 | order_hint, | 747 | order_hint, |
| 743 | primary_ref_frame: 0, | 748 | primary_ref_frame: 0, |
| 744 | refresh_frame_flags: 1 << last_slot, | 749 | refresh_frame_flags: 1 << last_slot, |
| 745 | - ref_frame_idx: [last_slot; ec_av1_syntax::REFS_PER_FRAME], | 750 | + // Every reference but `GOLDEN_FRAME` (index 3 of `ref_frame_idx`) |
| 751 | + // names the slot this frame both predicts from and refreshes; | ||
| 752 | + // GOLDEN names slot 1, which the key frame wrote when it refreshed | ||
| 753 | + // all eight and which no inter frame ever overwrites, so it holds | ||
| 754 | + // the key frame's own picture for the whole GOP. | ||
| 755 | + ref_frame_idx: { | ||
| 756 | + let mut idx = [last_slot; ec_av1_syntax::REFS_PER_FRAME]; | ||
| 757 | + idx[3] = GOLDEN_SLOT; | ||
| 758 | + idx | ||
| 759 | + }, | ||
| 746 | allow_high_precision_mv: false, | 760 | allow_high_precision_mv: false, |
| 747 | interpolation_filter: ec_av1_syntax::InterpolationFilter::Eighttap, | 761 | interpolation_filter: ec_av1_syntax::InterpolationFilter::Eighttap, |
| 748 | is_motion_mode_switchable: false, | 762 | is_motion_mode_switchable: false, |
| @@ -2411,7 +2425,7 @@ fn record_mi(grid: &mut MiGrid, mi_row: usize, mi_col: usize, size: u8, inter: O | |||
| 2411 | let info = match inter { | 2425 | let info = match inter { |
| 2412 | Some(info) => MiInfo { | 2426 | Some(info) => MiInfo { |
| 2413 | is_inter: true, | 2427 | is_inter: true, |
| 2414 | - ref_frame: LAST_FRAME, | 2428 | + ref_frame: info.ref_frame, |
| 2415 | ref_frame1: NO_REF1, | 2429 | ref_frame1: NO_REF1, |
| 2416 | mv1: (0, 0), | 2430 | mv1: (0, 0), |
| 2417 | mv: mv16(info.mv), | 2431 | mv: mv16(info.mv), |
| @@ -3388,7 +3402,13 @@ fn search_inter_block( | |||
| 3388 | search: &Search, | 3402 | search: &Search, |
| 3389 | mode_bits: &[f64; 13], | 3403 | mode_bits: &[f64; 13], |
| 3390 | reference: &Picture, | 3404 | reference: &Picture, |
| 3391 | - stack: &MvStack, fctx: &crate::decode::FrameCtx, | 3405 | + stack: &MvStack, |
| 3406 | + // `GOLDEN_FRAME`: the key frame's own reconstruction, held in DPB slot 1 | ||
| 3407 | + // for the whole GOP, with the stack this block derives against it. Only | ||
| 3408 | + // its two search-free modes are priced (`NEARESTMV`, `GLOBALMV`) -- a | ||
| 3409 | + // second motion search would double the wall of the stage that already | ||
| 3410 | + // owns most of it. | ||
| 3411 | + golden: Option<(&Picture, &MvStack)>, fctx: &crate::decode::FrameCtx, | ||
| 3392 | ) -> (BlockCoeffs, f64) { | 3412 | ) -> (BlockCoeffs, f64) { |
| 3393 | // `luma_set` is the INTRA candidates' table; the two inter candidates | 3413 | // `luma_set` is the INTRA candidates' table; the two inter candidates |
| 3394 | // below price their coefficients through `TxbSet::Luma32Inter`, which is | 3414 | // below price their coefficients through `TxbSet::Luma32Inter`, which is |
| @@ -3401,9 +3421,17 @@ fn search_inter_block( | |||
| 3401 | // function's doc comment. | 3421 | // function's doc comment. |
| 3402 | let skip_bits = |skip: bool| symbol_bits(&cdf::SKIP[0], usize::from(skip)); | 3422 | let skip_bits = |skip: bool| symbol_bits(&cdf::SKIP[0], usize::from(skip)); |
| 3403 | let intra_inter_bits = |inter: bool| symbol_bits(&cdf::INTRA_INTER[0], usize::from(inter)); | 3423 | let intra_inter_bits = |inter: bool| symbol_bits(&cdf::INTRA_INTER[0], usize::from(inter)); |
| 3404 | - let single_ref_bits = symbol_bits(&cdf::SINGLE_REF[0][0], 0) | 3424 | + // `write_single_ref`'s own tree, at the fixed context this pricer uses |
| 3405 | - + symbol_bits(&cdf::SINGLE_REF[0][2], 0) | 3425 | + // for every neighbour-derived symbol: LAST is p1=0,p3=0,p4=0 and GOLDEN |
| 3406 | - + symbol_bits(&cdf::SINGLE_REF[0][3], 0); | 3426 | + // p1=0,p3=1,p5=1. |
| 3427 | + let single_ref_bits = |ref_frame: i8| { | ||
| 3428 | + symbol_bits(&cdf::SINGLE_REF[0][0], 0) | ||
| 3429 | + + if ref_frame == crate::mvstack::GOLDEN_FRAME { | ||
| 3430 | + symbol_bits(&cdf::SINGLE_REF[0][2], 1) + symbol_bits(&cdf::SINGLE_REF[0][4], 1) | ||
| 3431 | + } else { | ||
| 3432 | + symbol_bits(&cdf::SINGLE_REF[0][2], 0) + symbol_bits(&cdf::SINGLE_REF[0][3], 0) | ||
| 3433 | + } | ||
| 3434 | + }; | ||
| 3407 | 3435 | ||
| 3408 | struct Candidate { | 3436 | struct Candidate { |
| 3409 | cost: f64, | 3437 | cost: f64, |
| @@ -3561,10 +3589,11 @@ fn search_inter_block( | |||
| 3561 | } | 3589 | } |
| 3562 | bits | 3590 | bits |
| 3563 | }; | 3591 | }; |
| 3592 | + let last_ref_bits = single_ref_bits(crate::mvstack::LAST_FRAME); | ||
| 3564 | let mut cands: Vec<((i32, i32), f64, InterInfo)> = vec![ | 3593 | let mut cands: Vec<((i32, i32), f64, InterInfo)> = vec![ |
| 3565 | ( | 3594 | ( |
| 3566 | stack.nearest_mv, | 3595 | stack.nearest_mv, |
| 3567 | - not_new + not_zero + symbol_bits(&cdf::REF_MV[stack.ref_mv_ctx], 0), | 3596 | + last_ref_bits + not_new + not_zero + symbol_bits(&cdf::REF_MV[stack.ref_mv_ctx], 0), |
| 3568 | InterInfo { | 3597 | InterInfo { |
| 3569 | ref_frame: crate::mvstack::LAST_FRAME, | 3598 | ref_frame: crate::mvstack::LAST_FRAME, |
| 3570 | mode: InterMode::NearestMv, | 3599 | mode: InterMode::NearestMv, |
| @@ -3577,7 +3606,7 @@ fn search_inter_block( | |||
| 3577 | // content where that vector is also the right one. | 3606 | // content where that vector is also the right one. |
| 3578 | ( | 3607 | ( |
| 3579 | (0, 0), | 3608 | (0, 0), |
| 3580 | - not_new + symbol_bits(&cdf::ZERO_MV[stack.zero_mv_ctx], 0), | 3609 | + last_ref_bits + not_new + symbol_bits(&cdf::ZERO_MV[stack.zero_mv_ctx], 0), |
| 3581 | InterInfo { | 3610 | InterInfo { |
| 3582 | ref_frame: crate::mvstack::LAST_FRAME, | 3611 | ref_frame: crate::mvstack::LAST_FRAME, |
| 3583 | mode: InterMode::GlobalMv, | 3612 | mode: InterMode::GlobalMv, |
| @@ -3586,7 +3615,8 @@ fn search_inter_block( | |||
| 3586 | }, | 3615 | }, |
| 3587 | ), | 3616 | ), |
| 3588 | ]; | 3617 | ]; |
| 3589 | - let near_bits = not_new + not_zero + symbol_bits(&cdf::REF_MV[stack.ref_mv_ctx], 1); | 3618 | + let near_bits = |
| 3619 | + last_ref_bits + not_new + not_zero + symbol_bits(&cdf::REF_MV[stack.ref_mv_ctx], 1); | ||
| 3590 | for idx in 1..=2usize { | 3620 | for idx in 1..=2usize { |
| 3591 | if let Some(e) = stack.entries.get(idx) { | 3621 | if let Some(e) = stack.entries.get(idx) { |
| 3592 | cands.push(( | 3622 | cands.push(( |
| @@ -3640,7 +3670,7 @@ fn search_inter_block( | |||
| 3640 | if let Some((bits, ref_mv_idx)) = best_new { | 3670 | if let Some((bits, ref_mv_idx)) = best_new { |
| 3641 | cands.push(( | 3671 | cands.push(( |
| 3642 | mv, | 3672 | mv, |
| 3643 | - symbol_bits(&cdf::NEW_MV[stack.new_mv_ctx], 0) + bits, | 3673 | + last_ref_bits + symbol_bits(&cdf::NEW_MV[stack.new_mv_ctx], 0) + bits, |
| 3644 | InterInfo { | 3674 | InterInfo { |
| 3645 | ref_frame: crate::mvstack::LAST_FRAME, | 3675 | ref_frame: crate::mvstack::LAST_FRAME, |
| 3646 | mode: InterMode::NewMv, | 3676 | mode: InterMode::NewMv, |
| @@ -3650,12 +3680,52 @@ fn search_inter_block( | |||
| 3650 | )); | 3680 | )); |
| 3651 | } | 3681 | } |
| 3652 | 3682 | ||
| 3653 | - // One trial set per distinct vector: sort by vector, cheapest syntax | 3683 | + if let Some((_, gstack)) = golden { |
| 3654 | - // first, and drop the rest of each vector's run. | 3684 | + let g_ref_bits = single_ref_bits(crate::mvstack::GOLDEN_FRAME); |
| 3655 | - cands.sort_by(|a, b| a.0.cmp(&b.0).then(a.1.total_cmp(&b.1))); | 3685 | + let g_not_new = symbol_bits(&cdf::NEW_MV[gstack.new_mv_ctx], 1); |
| 3656 | - cands.dedup_by_key(|c| c.0); | 3686 | + cands.push(( |
| 3687 | + gstack.nearest_mv, | ||
| 3688 | + g_ref_bits | ||
| 3689 | + + g_not_new | ||
| 3690 | + + symbol_bits(&cdf::ZERO_MV[gstack.zero_mv_ctx], 1) | ||
| 3691 | + + symbol_bits(&cdf::REF_MV[gstack.ref_mv_ctx], 0), | ||
| 3692 | + InterInfo { | ||
| 3693 | + ref_frame: crate::mvstack::GOLDEN_FRAME, | ||
| 3694 | + mode: InterMode::NearestMv, | ||
| 3695 | + mv: gstack.nearest_mv, | ||
| 3696 | + ref_mv_idx: 0, | ||
| 3697 | + }, | ||
| 3698 | + )); | ||
| 3699 | + cands.push(( | ||
| 3700 | + (0, 0), | ||
| 3701 | + g_ref_bits + g_not_new + symbol_bits(&cdf::ZERO_MV[gstack.zero_mv_ctx], 0), | ||
| 3702 | + InterInfo { | ||
| 3703 | + ref_frame: crate::mvstack::GOLDEN_FRAME, | ||
| 3704 | + mode: InterMode::GlobalMv, | ||
| 3705 | + mv: (0, 0), | ||
| 3706 | + ref_mv_idx: 0, | ||
| 3707 | + }, | ||
| 3708 | + )); | ||
| 3709 | + } | ||
| 3710 | + | ||
| 3711 | + // One trial set per distinct (reference, vector) pair: sort, cheapest | ||
| 3712 | + // syntax first, and drop the rest of each pair's run. | ||
| 3713 | + cands.sort_by(|a, b| { | ||
| 3714 | + (a.2.ref_frame, a.0) | ||
| 3715 | + .cmp(&(b.2.ref_frame, b.0)) | ||
| 3716 | + .then(a.1.total_cmp(&b.1)) | ||
| 3717 | + }); | ||
| 3718 | + cands.dedup_by_key(|c| (c.2.ref_frame, c.0)); | ||
| 3657 | { | 3719 | { |
| 3658 | for (mv, mode_bits_inter, info) in cands { | 3720 | for (mv, mode_bits_inter, info) in cands { |
| 3721 | + let (ref_luma, ref_u, ref_v) = match golden { | ||
| 3722 | + Some((g, _)) if info.ref_frame == crate::mvstack::GOLDEN_FRAME => ( | ||
| 3723 | + (&g.y, g.width, luma.true_width, luma.true_height), | ||
| 3724 | + (&g.u, g.width / 2, chroma[0].true_width, chroma[0].true_height), | ||
| 3725 | + (&g.v, g.width / 2, chroma[1].true_width, chroma[1].true_height), | ||
| 3726 | + ), | ||
| 3727 | + _ => (ref_luma, ref_u, ref_v), | ||
| 3728 | + }; | ||
| 3659 | let luma_trial = mc_trial( | 3729 | let luma_trial = mc_trial( |
| 3660 | luma, | 3730 | luma, |
| 3661 | x, | 3731 | x, |
| @@ -3713,7 +3783,6 @@ fn search_inter_block( | |||
| 3713 | + search.lambda | 3783 | + search.lambda |
| 3714 | * (skip_bits(skip) | 3784 | * (skip_bits(skip) |
| 3715 | + intra_inter_bits(true) | 3785 | + intra_inter_bits(true) |
| 3716 | - + single_ref_bits | ||
| 3717 | + mode_bits_inter | 3786 | + mode_bits_inter |
| 3718 | + if skip { | 3787 | + if skip { |
| 3719 | 0.0 | 3788 | 0.0 |
| @@ -3766,7 +3835,11 @@ pub(crate) fn encode_inter_frame( | |||
| 3766 | // stored into the slot this one's `primary_ref_frame` names. `None` | 3835 | // stored into the slot this one's `primary_ref_frame` names. `None` |
| 3767 | // starts from the defaults, which is right only for the frame straight | 3836 | // starts from the defaults, which is right only for the frame straight |
| 3768 | // after a key frame. | 3837 | // after a key frame. |
| 3769 | - start_cdfs: Option<&crate::cdf_state::Cdfs>, fctx: &crate::decode::FrameCtx, | 3838 | + start_cdfs: Option<&crate::cdf_state::Cdfs>, |
| 3839 | + // The key frame's own (padded) reconstruction, which every frame of the | ||
| 3840 | + // GOP still holds in DPB slot 1 -- `GOLDEN_FRAME`. `None` codes the frame | ||
| 3841 | + // with `LAST_FRAME` alone, exactly as before. | ||
| 3842 | + golden: Option<&Picture>, fctx: &crate::decode::FrameCtx, | ||
| 3770 | ) -> Result<Encoded> { | 3843 | ) -> Result<Encoded> { |
| 3771 | picture.check()?; | 3844 | picture.check()?; |
| 3772 | if !picture.width.is_multiple_of(SUPERBLOCK) || !picture.height.is_multiple_of(SUPERBLOCK) { | 3845 | if !picture.width.is_multiple_of(SUPERBLOCK) || !picture.height.is_multiple_of(SUPERBLOCK) { |
| @@ -3901,6 +3974,18 @@ pub(crate) fn encode_inter_frame( | |||
| 3901 | let (mi_row, mi_col) = (r32 * 8, c32 * 8); | 3974 | let (mi_row, mi_col) = (r32 * 8, c32 * 8); |
| 3902 | let stack = | 3975 | let stack = |
| 3903 | find_mv_stack(&grid, mi_row, mi_col, 8, 8, LAST_FRAME, mi_cols, mi_rows); | 3976 | find_mv_stack(&grid, mi_row, mi_col, 8, 8, LAST_FRAME, mi_cols, mi_rows); |
| 3977 | + let golden_stack = golden.map(|_| { | ||
| 3978 | + find_mv_stack( | ||
| 3979 | + &grid, | ||
| 3980 | + mi_row, | ||
| 3981 | + mi_col, | ||
| 3982 | + 8, | ||
| 3983 | + 8, | ||
| 3984 | + crate::mvstack::GOLDEN_FRAME, | ||
| 3985 | + mi_cols, | ||
| 3986 | + mi_rows, | ||
| 3987 | + ) | ||
| 3988 | + }); | ||
| 3904 | 3989 | ||
| 3905 | let base = snapshot(&luma, &chroma, (x, y), BLOCK); | 3990 | let base = snapshot(&luma, &chroma, (x, y), BLOCK); |
| 3906 | let (block, mut cost_whole) = search_inter_block( | 3991 | let (block, mut cost_whole) = search_inter_block( |
| @@ -3910,7 +3995,8 @@ pub(crate) fn encode_inter_frame( | |||
| 3910 | &search, | 3995 | &search, |
| 3911 | &mode_bits_table, | 3996 | &mode_bits_table, |
| 3912 | reference, | 3997 | reference, |
| 3913 | - &stack, fctx, | 3998 | + &stack, |
| 3999 | + golden.zip(golden_stack.as_ref()), fctx, | ||
| 3914 | ); | 4000 | ); |
| 3915 | cost_whole += search.lambda * partition_bits(BLOCK, false); | 4001 | cost_whole += search.lambda * partition_bits(BLOCK, false); |
| 3916 | let after_whole = snapshot(&luma, &chroma, (x, y), BLOCK); | 4002 | let after_whole = snapshot(&luma, &chroma, (x, y), BLOCK); |
| @@ -4266,8 +4352,25 @@ pub(crate) fn encode_inter_frame( | |||
| 4266 | v: crop_plane(&reference.v, reference.width / 2, fw / 2, fh / 2), | 4352 | v: crop_plane(&reference.v, reference.width / 2, fw / 2, fh / 2), |
| 4267 | } | 4353 | } |
| 4268 | }; | 4354 | }; |
| 4355 | + // `RefPix` is indexed by the reference NAME (`LAST_FRAME` = 1, | ||
| 4356 | + // `GOLDEN_FRAME` = 4), not by DPB slot: the trial decode of a frame whose | ||
| 4357 | + // blocks may name GOLDEN needs that plane too, cropped the same way. | ||
| 4358 | + let trial_golden = golden.map(|g| { | ||
| 4359 | + if (g.width, g.height) == (fw, fh) { | ||
| 4360 | + g.clone() | ||
| 4361 | + } else { | ||
| 4362 | + Picture { | ||
| 4363 | + width: fw, | ||
| 4364 | + height: fh, | ||
| 4365 | + y: crop_plane(&g.y, g.width, fw, fh), | ||
| 4366 | + u: crop_plane(&g.u, g.width / 2, fw / 2, fh / 2), | ||
| 4367 | + v: crop_plane(&g.v, g.width / 2, fw / 2, fh / 2), | ||
| 4368 | + } | ||
| 4369 | + } | ||
| 4370 | + }); | ||
| 4269 | let mut refs: [Option<&Picture>; 8] = [None; 8]; | 4371 | let mut refs: [Option<&Picture>; 8] = [None; 8]; |
| 4270 | refs[1] = Some(&trial_ref); | 4372 | refs[1] = Some(&trial_ref); |
| 4373 | + refs[4] = trial_golden.as_ref(); | ||
| 4271 | let refpix = crate::decode::RefPix::ready(refs); | 4374 | let refpix = crate::decode::RefPix::ready(refs); |
| 4272 | pick_and_apply_filters( | 4375 | pick_and_apply_filters( |
| 4273 | &mut header, | 4376 | &mut header, |
| @@ -4388,6 +4491,10 @@ pub(crate) fn encode_sequence_with_ctx( | |||
| 4388 | unspecified_color_config(), fctx, | 4491 | unspecified_color_config(), fctx, |
| 4389 | )?; | 4492 | )?; |
| 4390 | let mut stream = key.stream.clone(); | 4493 | let mut stream = key.stream.clone(); |
| 4494 | + // Slot 1 keeps the key frame's own reconstruction for the whole GOP (the | ||
| 4495 | + // key frame refreshes every slot, each inter frame only slot 0), which is | ||
| 4496 | + // what `GOLDEN_FRAME` names. | ||
| 4497 | + let golden = key.reconstruction.clone(); | ||
| 4391 | let mut carried = key.next_cdfs.clone(); | 4498 | let mut carried = key.next_cdfs.clone(); |
| 4392 | let mut reference = key.reconstruction.clone(); | 4499 | let mut reference = key.reconstruction.clone(); |
| 4393 | let mut frames = vec![crop_encoded(&key, render.0, render.1)]; | 4500 | let mut frames = vec![crop_encoded(&key, render.0, render.1)]; |
| @@ -4407,7 +4514,8 @@ pub(crate) fn encode_sequence_with_ctx( | |||
| 4407 | deadzone, | 4514 | deadzone, |
| 4408 | order_hint, | 4515 | order_hint, |
| 4409 | render, | 4516 | render, |
| 4410 | - Some(&carried.0), fctx, | 4517 | + Some(&carried.0), |
| 4518 | + Some(&golden), fctx, | ||
| 4411 | )?; | 4519 | )?; |
| 4412 | stream.extend_from_slice(&inter.stream); | 4520 | stream.extend_from_slice(&inter.stream); |
| 4413 | carried = inter.next_cdfs.clone(); | 4521 | carried = inter.next_cdfs.clone(); |
| @@ -6444,7 +6552,7 @@ mod tests { | |||
| 6444 | let padded_pic = picture.padded_to(SUPERBLOCK); | 6552 | let padded_pic = picture.padded_to(SUPERBLOCK); |
| 6445 | let t = Instant::now(); | 6553 | let t = Instant::now(); |
| 6446 | let inter = | 6554 | let inter = |
| 6447 | - encode_inter_frame(&padded_pic, &padded_ref, 100, 0.5, 1, (width, height), None, fctx).unwrap(); | 6555 | + encode_inter_frame(&padded_pic, &padded_ref, 100, 0.5, 1, (width, height), None, None, fctx).unwrap(); |
| 6448 | let inter_t = t.elapsed(); | 6556 | let inter_t = t.elapsed(); |
| 6449 | eprintln!( | 6557 | eprintln!( |
| 6450 | "inter frame vs its own key frame: {inter_t:>9.2?} ({} bytes, inter share \ | 6558 | "inter frame vs its own key frame: {inter_t:>9.2?} ({} bytes, inter share \ |
| @@ -6542,7 +6650,7 @@ mod tests { | |||
| 6542 | stage_reset(); | 6650 | stage_reset(); |
| 6543 | let t = Instant::now(); | 6651 | let t = Instant::now(); |
| 6544 | let inter = | 6652 | let inter = |
| 6545 | - encode_inter_frame(&padded_pic, &padded_ref, 100, 0.5, 1, (width, height), None, fctx).unwrap(); | 6653 | + encode_inter_frame(&padded_pic, &padded_ref, 100, 0.5, 1, (width, height), None, None, fctx).unwrap(); |
| 6546 | let total_t = t.elapsed(); | 6654 | let total_t = t.elapsed(); |
| 6547 | let [motion_search, interpolation, transform_quant, entropy] = stage_read(); | 6655 | let [motion_search, interpolation, transform_quant, entropy] = stage_read(); |
| 6548 | let accounted = motion_search + transform_quant + entropy; | 6656 | let accounted = motion_search + transform_quant + entropy; |
| @@ -6719,7 +6827,7 @@ mod tests { | |||
| 6719 | q, | 6827 | q, |
| 6720 | 0.5, | 6828 | 0.5, |
| 6721 | 1, | 6829 | 1, |
| 6722 | - (width, height), None, fctx, | 6830 | + (width, height), None, None, fctx, |
| 6723 | ) | 6831 | ) |
| 6724 | .unwrap(); | 6832 | .unwrap(); |
| 6725 | let baseline_psnr = psnr(&baseline.reconstruction.y, &inter_source.y); | 6833 | let baseline_psnr = psnr(&baseline.reconstruction.y, &inter_source.y); |
| @@ -6736,7 +6844,7 @@ mod tests { | |||
| 6736 | q, | 6844 | q, |
| 6737 | 0.5, | 6845 | 0.5, |
| 6738 | 1, | 6846 | 1, |
| 6739 | - (width, height), None, fctx, | 6847 | + (width, height), None, None, fctx, |
| 6740 | ) | 6848 | ) |
| 6741 | .unwrap(); | 6849 | .unwrap(); |
| 6742 | let pruned_psnr = psnr(&pruned.reconstruction.y, &inter_source.y); | 6850 | let pruned_psnr = psnr(&pruned.reconstruction.y, &inter_source.y); |
| @@ -7249,6 +7357,7 @@ mod tests { | |||
| 7249 | let _ = take_uv_mode_hits(); | 7357 | let _ = take_uv_mode_hits(); |
| 7250 | let _ = crate::tile::take_inter_mode_hits(); | 7358 | let _ = crate::tile::take_inter_mode_hits(); |
| 7251 | let _ = crate::tile::take_drl_hits(); | 7359 | let _ = crate::tile::take_drl_hits(); |
| 7360 | + let _ = crate::tile::take_ref_hits(); | ||
| 7252 | let (ours, ours_wall) = our_ladder(&source, width, height, fctx); | 7361 | let (ours, ours_wall) = our_ladder(&source, width, height, fctx); |
| 7253 | // How often the inter split actually fired over this clip's four | 7362 | // How often the inter split actually fired over this clip's four |
| 7254 | // encodes, against the whole 32x32 that was the only outcome | 7363 | // encodes, against the whole 32x32 that was the only outcome |
| @@ -7259,6 +7368,7 @@ mod tests { | |||
| 7259 | // number cannot be crediting (gate-blind-to-feature). | 7368 | // number cannot be crediting (gate-blind-to-feature). |
| 7260 | let [nearest, near, global, new] = crate::tile::take_inter_mode_hits(); | 7369 | let [nearest, near, global, new] = crate::tile::take_inter_mode_hits(); |
| 7261 | let [drl0, drl1, drl2, drl3] = crate::tile::take_drl_hits(); | 7370 | let [drl0, drl1, drl2, drl3] = crate::tile::take_drl_hits(); |
| 7371 | + let refs = crate::tile::take_ref_hits(); | ||
| 7262 | let modes_total = (nearest + near + global + new).max(1); | 7372 | let modes_total = (nearest + near + global + new).max(1); |
| 7263 | eprintln!( | 7373 | eprintln!( |
| 7264 | "{name}: inter modes NEAREST {nearest} ({:.1}%) NEAR {near} ({:.1}%) \ | 7374 | "{name}: inter modes NEAREST {nearest} ({:.1}%) NEAR {near} ({:.1}%) \ |
| @@ -7269,6 +7379,10 @@ mod tests { | |||
| 7269 | 100.0 * global as f64 / modes_total as f64, | 7379 | 100.0 * global as f64 / modes_total as f64, |
| 7270 | 100.0 * new as f64 / modes_total as f64, | 7380 | 100.0 * new as f64 / modes_total as f64, |
| 7271 | ); | 7381 | ); |
| 7382 | + eprintln!( | ||
| 7383 | + "{name}: references LAST {} GOLDEN {} (LAST2 {} LAST3 {} BWD {} ALT2 {} ALT {})", | ||
| 7384 | + refs[0], refs[3], refs[1], refs[2], refs[4], refs[5], refs[6] | ||
| 7385 | + ); | ||
| 7272 | eprintln!( | 7386 | eprintln!( |
| 7273 | "{name}: inter 32x32 split into 16s {split32} of {} blocks ({:.1}%); \ | 7387 | "{name}: inter 32x32 split into 16s {split32} of {} blocks ({:.1}%); \ |
| 7274 | 16x16 leaves split into 8s {split16} of {} ({:.1}%)", | 7388 | 16x16 leaves split into 8s {split16} of {} ({:.1}%)", |
crates/ec-av1/src/encoder.rsmodified+8 -0
| @@ -231,6 +231,9 @@ pub struct Av1Encoder { | |||
| 231 | /// must start from, since the header leaves | 231 | /// must start from, since the header leaves |
| 232 | /// `disable_frame_end_update_cdf` off. `None` before the first frame. | 232 | /// `disable_frame_end_update_cdf` off. `None` before the first frame. |
| 233 | carried_cdfs: Option<crate::encode::CdfSnapshot>, | 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>, | ||
| 234 | } | 237 | } |
| 235 | 238 | ||
| 236 | /// The encoder stays `Send` now that it owns a `FrameCtx` (whose cells are | 239 | /// The encoder stays `Send` now that it owns a `FrameCtx` (whose cells are |
| @@ -257,6 +260,7 @@ impl Av1Encoder { | |||
| 257 | Picture::grey(config.width, config.height).check_even()?; | 260 | Picture::grey(config.width, config.height).check_even()?; |
| 258 | Ok(Self { | 261 | Ok(Self { |
| 259 | carried_cdfs: None, | 262 | carried_cdfs: None, |
| 263 | + golden: None, | ||
| 260 | color_config: config.colour.color_config(), | 264 | color_config: config.colour.color_config(), |
| 261 | config, | 265 | config, |
| 262 | reference: None, | 266 | reference: None, |
| @@ -362,11 +366,15 @@ impl Av1Encoder { | |||
| 362 | order as u32, | 366 | order as u32, |
| 363 | render, | 367 | render, |
| 364 | self.carried_cdfs.as_ref().map(|c| &c.0), | 368 | self.carried_cdfs.as_ref().map(|c| &c.0), |
| 369 | + self.golden.as_ref(), | ||
| 365 | &self.fctx, | 370 | &self.fctx, |
| 366 | )? | 371 | )? |
| 367 | }; | 372 | }; |
| 368 | 373 | ||
| 369 | self.carried_cdfs = Some(encoded.next_cdfs.clone()); | 374 | self.carried_cdfs = Some(encoded.next_cdfs.clone()); |
| 375 | + if is_key { | ||
| 376 | + self.golden = Some(encoded.reconstruction.clone()); | ||
| 377 | + } | ||
| 370 | self.reference = Some(encoded.reconstruction.clone()); | 378 | self.reference = Some(encoded.reconstruction.clone()); |
| 371 | self.next_index += 1; | 379 | self.next_index += 1; |
| 372 | let cropped = crop_encoded(&encoded, render.0, render.1); | 380 | let cropped = crop_encoded(&encoded, render.0, render.1); |
crates/ec-av1/src/tile.rsmodified+19 -0
| @@ -2695,6 +2695,7 @@ fn write_single_ref( | |||
| 2695 | above_ref: i8, | 2695 | above_ref: i8, |
| 2696 | left_ref: i8, | 2696 | left_ref: i8, |
| 2697 | ) { | 2697 | ) { |
| 2698 | + REF_HITS[(ref_frame.max(1) - 1) as usize % 7].fetch_add(1, std::sync::atomic::Ordering::Relaxed); | ||
| 2698 | use crate::mvstack::{ | 2699 | use crate::mvstack::{ |
| 2699 | ALTREF2_FRAME, ALTREF_FRAME, BWDREF_FRAME, GOLDEN_FRAME, LAST2_FRAME, LAST3_FRAME, | 2700 | ALTREF2_FRAME, ALTREF_FRAME, BWDREF_FRAME, GOLDEN_FRAME, LAST2_FRAME, LAST3_FRAME, |
| 2700 | single_ref_p1_ctx, single_ref_p2_ctx, single_ref_p3_ctx, single_ref_p4_ctx, | 2701 | single_ref_p1_ctx, single_ref_p2_ctx, single_ref_p3_ctx, single_ref_p4_ctx, |
| @@ -2757,6 +2758,24 @@ static DRL_HITS: [std::sync::atomic::AtomicUsize; 4] = [ | |||
| 2757 | std::sync::atomic::AtomicUsize::new(0), | 2758 | std::sync::atomic::AtomicUsize::new(0), |
| 2758 | ]; | 2759 | ]; |
| 2759 | 2760 | ||
| 2761 | +/// One counter per reference name (`LAST`..`ALTREF`), for the gate's own | ||
| 2762 | +/// census of what the encoder actually picks. | ||
| 2763 | +static REF_HITS: [std::sync::atomic::AtomicUsize; 7] = [ | ||
| 2764 | + std::sync::atomic::AtomicUsize::new(0), | ||
| 2765 | + std::sync::atomic::AtomicUsize::new(0), | ||
| 2766 | + std::sync::atomic::AtomicUsize::new(0), | ||
| 2767 | + std::sync::atomic::AtomicUsize::new(0), | ||
| 2768 | + std::sync::atomic::AtomicUsize::new(0), | ||
| 2769 | + std::sync::atomic::AtomicUsize::new(0), | ||
| 2770 | + std::sync::atomic::AtomicUsize::new(0), | ||
| 2771 | +]; | ||
| 2772 | + | ||
| 2773 | +/// Takes and clears the per-reference histogram. | ||
| 2774 | +#[cfg(test)] | ||
| 2775 | +pub(crate) fn take_ref_hits() -> [usize; 7] { | ||
| 2776 | + std::array::from_fn(|i| REF_HITS[i].swap(0, std::sync::atomic::Ordering::Relaxed)) | ||
| 2777 | +} | ||
| 2778 | + | ||
| 2760 | /// Bumps the two histograms for one coded inter block. | 2779 | /// Bumps the two histograms for one coded inter block. |
| 2761 | fn note_inter_mode(mode: usize, drl: usize) { | 2780 | fn note_inter_mode(mode: usize, drl: usize) { |
| 2762 | INTER_MODE_HITS[mode].fetch_add(1, std::sync::atomic::Ordering::Relaxed); | 2781 | INTER_MODE_HITS[mode].fetch_add(1, std::sync::atomic::Ordering::Relaxed); |