ALTREF as the frame two back, on an alternating slot
Browse files at this commit931098164d723070530a397176eddb5ff3e1a290Parent: 2f069ae- feat
- ALTREF as the frame two back, on an alternating slot
3 changed files +135 -33
- crates/ec-av1/src/decode.rs +21 -2
- crates/ec-av1/src/encode.rs +106 -31
- crates/ec-av1/src/encoder.rs +8 -0
crates/ec-av1/src/decode.rsmodified+21 -2
| @@ -35642,9 +35642,20 @@ mod tests { | ||
| 35642 | 35642 | /// `ref_frame_idx[3]` names the slot the key frame left behind and its |
| 35643 | 35643 | /// blocks may code `single_ref` out to it. |
| 35644 | 35644 | fn last_and_golden<'a>(last: &'a Picture, golden: &'a Picture) -> RefPix<'a> { |
| 35645 | + last_golden_altref(last, golden, None) | |
| 35646 | + } | |
| 35647 | + | |
| 35648 | + /// [`last_and_golden`] plus `ALTREF_FRAME` (index 7): the frame two back, | |
| 35649 | + /// which the encoder keeps in the slot the current frame refreshes. | |
| 35650 | + fn last_golden_altref<'a>( | |
| 35651 | + last: &'a Picture, | |
| 35652 | + golden: &'a Picture, | |
| 35653 | + altref: Option<&'a Picture>, | |
| 35654 | + ) -> RefPix<'a> { | |
| 35645 | 35655 | let mut refs: [Option<&Picture>; 8] = [None; 8]; |
| 35646 | 35656 | refs[1] = Some(last); |
| 35647 | 35657 | refs[4] = Some(golden); |
| 35658 | + refs[7] = altref; | |
| 35648 | 35659 | RefPix::ready(refs) |
| 35649 | 35660 | } |
| 35650 | 35661 | |
| @@ -37175,7 +37186,11 @@ mod tests { | ||
| 37175 | 37186 | frame.base_q_idx, |
| 37176 | 37187 | width as u32, |
| 37177 | 37188 | height as u32, |
| 37178 | - &last_and_golden(&reference, &encoded.frames[0].reconstruction), | |
| 37189 | + &last_golden_altref( | |
| 37190 | + &reference, | |
| 37191 | + &encoded.frames[0].reconstruction, | |
| 37192 | + (i >= 2).then(|| &encoded.frames[i - 2].reconstruction), | |
| 37193 | + ), | |
| 37179 | 37194 | &frame.cdef, |
| 37180 | 37195 | &frame.loop_filter, |
| 37181 | 37196 | false, |
| @@ -37487,7 +37502,11 @@ mod tests { | ||
| 37487 | 37502 | frame.base_q_idx, |
| 37488 | 37503 | coded_w, |
| 37489 | 37504 | coded_h, |
| 37490 | - &last_and_golden(&reference, &encoded.frames[0].reconstruction), | |
| 37505 | + &last_golden_altref( | |
| 37506 | + &reference, | |
| 37507 | + &encoded.frames[0].reconstruction, | |
| 37508 | + (i >= 2).then(|| &encoded.frames[i - 2].reconstruction), | |
| 37509 | + ), | |
| 37491 | 37510 | &frame.cdef, |
| 37492 | 37511 | &frame.loop_filter, |
| 37493 | 37512 | false, |
crates/ec-av1/src/encode.rsmodified+106 -31
| @@ -731,6 +731,26 @@ pub fn inter_frame_headers( | ||
| 731 | 731 | base_q_idx: u8, |
| 732 | 732 | order_hint: u32, |
| 733 | 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, | |
| 734 | 754 | ) -> Result<(SequenceHeader, FrameHeader)> { |
| 735 | 755 | let (seq, key) = key_frame_headers(width, height, base_q_idx)?; |
| 736 | 756 | let header = FrameHeader { |
| @@ -746,7 +766,7 @@ pub fn inter_frame_headers( | ||
| 746 | 766 | force_integer_mv: false, |
| 747 | 767 | order_hint, |
| 748 | 768 | primary_ref_frame: 0, |
| 749 | - refresh_frame_flags: 1 << last_slot, | |
| 769 | + refresh_frame_flags: 1 << self_slot, | |
| 750 | 770 | // Every reference but `GOLDEN_FRAME` (index 3 of `ref_frame_idx`) |
| 751 | 771 | // names the slot this frame both predicts from and refreshes; |
| 752 | 772 | // GOLDEN names slot 1, which the key frame wrote when it refreshed |
| @@ -755,6 +775,7 @@ pub fn inter_frame_headers( | ||
| 755 | 775 | ref_frame_idx: { |
| 756 | 776 | let mut idx = [last_slot; ec_av1_syntax::REFS_PER_FRAME]; |
| 757 | 777 | idx[3] = GOLDEN_SLOT; |
| 778 | + idx[6] = altref_slot; | |
| 758 | 779 | idx |
| 759 | 780 | }, |
| 760 | 781 | allow_high_precision_mv: false, |
| @@ -3408,7 +3429,7 @@ fn search_inter_block( | ||
| 3408 | 3429 | // its two search-free modes are priced (`NEARESTMV`, `GLOBALMV`) -- a |
| 3409 | 3430 | // second motion search would double the wall of the stage that already |
| 3410 | 3431 | // owns most of it. |
| 3411 | - golden: Option<(&Picture, &MvStack)>, fctx: &crate::decode::FrameCtx, | |
| 3432 | + extra: &[(i8, &Picture, &MvStack)], fctx: &crate::decode::FrameCtx, | |
| 3412 | 3433 | ) -> (BlockCoeffs, f64) { |
| 3413 | 3434 | // `luma_set` is the INTRA candidates' table; the two inter candidates |
| 3414 | 3435 | // below price their coefficients through `TxbSet::Luma32Inter`, which is |
| @@ -3680,8 +3701,8 @@ fn search_inter_block( | ||
| 3680 | 3701 | )); |
| 3681 | 3702 | } |
| 3682 | 3703 | |
| 3683 | - if let Some((_, gstack)) = golden { | |
| 3684 | - let g_ref_bits = single_ref_bits(crate::mvstack::GOLDEN_FRAME); | |
| 3704 | + for &(ref_frame, _, gstack) in extra { | |
| 3705 | + let g_ref_bits = single_ref_bits(ref_frame); | |
| 3685 | 3706 | let g_not_new = symbol_bits(&cdf::NEW_MV[gstack.new_mv_ctx], 1); |
| 3686 | 3707 | cands.push(( |
| 3687 | 3708 | gstack.nearest_mv, |
| @@ -3690,7 +3711,7 @@ fn search_inter_block( | ||
| 3690 | 3711 | + symbol_bits(&cdf::ZERO_MV[gstack.zero_mv_ctx], 1) |
| 3691 | 3712 | + symbol_bits(&cdf::REF_MV[gstack.ref_mv_ctx], 0), |
| 3692 | 3713 | InterInfo { |
| 3693 | - ref_frame: crate::mvstack::GOLDEN_FRAME, | |
| 3714 | + ref_frame, | |
| 3694 | 3715 | mode: InterMode::NearestMv, |
| 3695 | 3716 | mv: gstack.nearest_mv, |
| 3696 | 3717 | ref_mv_idx: 0, |
| @@ -3700,7 +3721,7 @@ fn search_inter_block( | ||
| 3700 | 3721 | (0, 0), |
| 3701 | 3722 | g_ref_bits + g_not_new + symbol_bits(&cdf::ZERO_MV[gstack.zero_mv_ctx], 0), |
| 3702 | 3723 | InterInfo { |
| 3703 | - ref_frame: crate::mvstack::GOLDEN_FRAME, | |
| 3724 | + ref_frame, | |
| 3704 | 3725 | mode: InterMode::GlobalMv, |
| 3705 | 3726 | mv: (0, 0), |
| 3706 | 3727 | ref_mv_idx: 0, |
| @@ -3718,13 +3739,16 @@ fn search_inter_block( | ||
| 3718 | 3739 | cands.dedup_by_key(|c| (c.2.ref_frame, c.0)); |
| 3719 | 3740 | { |
| 3720 | 3741 | 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 => ( | |
| 3742 | + let (ref_luma, ref_u, ref_v) = match extra | |
| 3743 | + .iter() | |
| 3744 | + .find(|(r, _, _)| *r == info.ref_frame) | |
| 3745 | + { | |
| 3746 | + Some(&(_, g, _)) => ( | |
| 3723 | 3747 | (&g.y, g.width, luma.true_width, luma.true_height), |
| 3724 | 3748 | (&g.u, g.width / 2, chroma[0].true_width, chroma[0].true_height), |
| 3725 | 3749 | (&g.v, g.width / 2, chroma[1].true_width, chroma[1].true_height), |
| 3726 | 3750 | ), |
| 3727 | - _ => (ref_luma, ref_u, ref_v), | |
| 3751 | + None => (ref_luma, ref_u, ref_v), | |
| 3728 | 3752 | }; |
| 3729 | 3753 | let luma_trial = mc_trial( |
| 3730 | 3754 | luma, |
| @@ -3839,7 +3863,11 @@ pub(crate) fn encode_inter_frame( | ||
| 3839 | 3863 | // The key frame's own (padded) reconstruction, which every frame of the |
| 3840 | 3864 | // GOP still holds in DPB slot 1 -- `GOLDEN_FRAME`. `None` codes the frame |
| 3841 | 3865 | // with `LAST_FRAME` alone, exactly as before. |
| 3842 | - golden: Option<&Picture>, fctx: &crate::decode::FrameCtx, | |
| 3866 | + golden: Option<&Picture>, | |
| 3867 | + // The frame two back, held in the slot this frame is about to refresh | |
| 3868 | + // (the encoder alternates 0/2): `ALTREF_FRAME`. `None` for the first two | |
| 3869 | + // inter frames of a GOP, where the frame two back IS the key frame. | |
| 3870 | + altref: Option<&Picture>, fctx: &crate::decode::FrameCtx, | |
| 3843 | 3871 | ) -> Result<Encoded> { |
| 3844 | 3872 | picture.check()?; |
| 3845 | 3873 | if !picture.width.is_multiple_of(SUPERBLOCK) || !picture.height.is_multiple_of(SUPERBLOCK) { |
| @@ -3862,14 +3890,33 @@ pub(crate) fn encode_inter_frame( | ||
| 3862 | 3890 | // overwrites slot 0, so the frame just coded is always what the next one |
| 3863 | 3891 | // predicts against. |
| 3864 | 3892 | const LAST_SLOT: u8 = 0; |
| 3893 | + // Slot 0 and slot 2 alternate so the frame two back survives one more | |
| 3894 | + // frame: frame n writes A(n) = 0 for odd n, 2 for even, so A(n) = A(n-2) | |
| 3895 | + // and the picture this frame names as `ALTREF_FRAME` is the one sitting | |
| 3896 | + // in the very slot it will overwrite. | |
| 3897 | + let self_slot = if order_hint % 2 == 1 { 0 } else { 2 }; | |
| 3898 | + let last_slot = if order_hint <= 1 { | |
| 3899 | + LAST_SLOT | |
| 3900 | + } else if (order_hint - 1) % 2 == 1 { | |
| 3901 | + 0 | |
| 3902 | + } else { | |
| 3903 | + 2 | |
| 3904 | + }; | |
| 3865 | 3905 | // `render`, not `picture.width`/`picture.height`: `picture` here is |
| 3866 | 3906 | // already padded to a whole number of superblocks (`encode_sequence`'s |
| 3867 | 3907 | // `padded_to(SUPERBLOCK)`), and the header's `mi_cols`/`mi_rows` (and so |
| 3868 | 3908 | // every true-edge clamp downstream) must come from the frame's real, |
| 3869 | 3909 | // unpadded size, same as `key_frame_headers_colour` takes `render` and |
| 3870 | 3910 | // not the padded picture in `encode_key_frame_inner`. |
| 3871 | - let (seq, mut header) = | |
| 3872 | - inter_frame_headers(render.0, render.1, base_q_idx, order_hint, LAST_SLOT)?; | |
| 3911 | + let (seq, mut header) = inter_frame_headers_slots( | |
| 3912 | + render.0, | |
| 3913 | + render.1, | |
| 3914 | + base_q_idx, | |
| 3915 | + order_hint, | |
| 3916 | + last_slot, | |
| 3917 | + self_slot, | |
| 3918 | + if altref.is_some() { self_slot } else { GOLDEN_SLOT }, | |
| 3919 | + )?; | |
| 3873 | 3920 | header.render_width = render.0 as u32; |
| 3874 | 3921 | header.render_height = render.1 as u32; |
| 3875 | 3922 | // `TxMode::Select` here too: an inter block then codes one `txfm_split` |
| @@ -3974,18 +4021,25 @@ pub(crate) fn encode_inter_frame( | ||
| 3974 | 4021 | let (mi_row, mi_col) = (r32 * 8, c32 * 8); |
| 3975 | 4022 | let stack = |
| 3976 | 4023 | 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 | - }); | |
| 4024 | + let extra_stacks: Vec<(i8, &Picture, MvStack)> = [ | |
| 4025 | + (crate::mvstack::GOLDEN_FRAME, golden), | |
| 4026 | + (crate::mvstack::ALTREF_FRAME, altref), | |
| 4027 | + ] | |
| 4028 | + .into_iter() | |
| 4029 | + .filter_map(|(r, pic)| { | |
| 4030 | + pic.map(|p| { | |
| 4031 | + ( | |
| 4032 | + r, | |
| 4033 | + p, | |
| 4034 | + find_mv_stack(&grid, mi_row, mi_col, 8, 8, r, mi_cols, mi_rows), | |
| 4035 | + ) | |
| 4036 | + }) | |
| 4037 | + }) | |
| 4038 | + .collect(); | |
| 4039 | + let extra: Vec<(i8, &Picture, &MvStack)> = extra_stacks | |
| 4040 | + .iter() | |
| 4041 | + .map(|(r, p, st)| (*r, *p, st)) | |
| 4042 | + .collect(); | |
| 3989 | 4043 | |
| 3990 | 4044 | let base = snapshot(&luma, &chroma, (x, y), BLOCK); |
| 3991 | 4045 | let (block, mut cost_whole) = search_inter_block( |
| @@ -3996,7 +4050,7 @@ pub(crate) fn encode_inter_frame( | ||
| 3996 | 4050 | &mode_bits_table, |
| 3997 | 4051 | reference, |
| 3998 | 4052 | &stack, |
| 3999 | - golden.zip(golden_stack.as_ref()), fctx, | |
| 4053 | + &extra, fctx, | |
| 4000 | 4054 | ); |
| 4001 | 4055 | cost_whole += search.lambda * partition_bits(BLOCK, false); |
| 4002 | 4056 | let after_whole = snapshot(&luma, &chroma, (x, y), BLOCK); |
| @@ -4368,9 +4422,23 @@ pub(crate) fn encode_inter_frame( | ||
| 4368 | 4422 | } |
| 4369 | 4423 | } |
| 4370 | 4424 | }); |
| 4425 | + let trial_altref = altref.map(|a| { | |
| 4426 | + if (a.width, a.height) == (fw, fh) { | |
| 4427 | + a.clone() | |
| 4428 | + } else { | |
| 4429 | + Picture { | |
| 4430 | + width: fw, | |
| 4431 | + height: fh, | |
| 4432 | + y: crop_plane(&a.y, a.width, fw, fh), | |
| 4433 | + u: crop_plane(&a.u, a.width / 2, fw / 2, fh / 2), | |
| 4434 | + v: crop_plane(&a.v, a.width / 2, fw / 2, fh / 2), | |
| 4435 | + } | |
| 4436 | + } | |
| 4437 | + }); | |
| 4371 | 4438 | let mut refs: [Option<&Picture>; 8] = [None; 8]; |
| 4372 | 4439 | refs[1] = Some(&trial_ref); |
| 4373 | 4440 | refs[4] = trial_golden.as_ref(); |
| 4441 | + refs[7] = trial_altref.as_ref(); | |
| 4374 | 4442 | let refpix = crate::decode::RefPix::ready(refs); |
| 4375 | 4443 | pick_and_apply_filters( |
| 4376 | 4444 | &mut header, |
| @@ -4495,6 +4563,9 @@ pub(crate) fn encode_sequence_with_ctx( | ||
| 4495 | 4563 | // key frame refreshes every slot, each inter frame only slot 0), which is |
| 4496 | 4564 | // what `GOLDEN_FRAME` names. |
| 4497 | 4565 | let golden = key.reconstruction.clone(); |
| 4566 | + // The frame two back, once there is one that is not the key frame | |
| 4567 | + // itself (which `GOLDEN_FRAME` already names). | |
| 4568 | + let mut prev2: Option<Picture> = None; | |
| 4498 | 4569 | let mut carried = key.next_cdfs.clone(); |
| 4499 | 4570 | let mut reference = key.reconstruction.clone(); |
| 4500 | 4571 | let mut frames = vec![crop_encoded(&key, render.0, render.1)]; |
| @@ -4515,11 +4586,15 @@ pub(crate) fn encode_sequence_with_ctx( | ||
| 4515 | 4586 | order_hint, |
| 4516 | 4587 | render, |
| 4517 | 4588 | Some(&carried.0), |
| 4518 | - Some(&golden), fctx, | |
| 4589 | + Some(&golden), | |
| 4590 | + prev2.as_ref(), fctx, | |
| 4519 | 4591 | )?; |
| 4520 | 4592 | stream.extend_from_slice(&inter.stream); |
| 4521 | 4593 | carried = inter.next_cdfs.clone(); |
| 4522 | - reference = inter.reconstruction.clone(); | |
| 4594 | + prev2 = Some(std::mem::replace( | |
| 4595 | + &mut reference, | |
| 4596 | + inter.reconstruction.clone(), | |
| 4597 | + )); | |
| 4523 | 4598 | frames.push(crop_encoded(&inter, render.0, render.1)); |
| 4524 | 4599 | } |
| 4525 | 4600 | Ok(EncodedSequence { stream, frames }) |
| @@ -6552,7 +6627,7 @@ mod tests { | ||
| 6552 | 6627 | let padded_pic = picture.padded_to(SUPERBLOCK); |
| 6553 | 6628 | let t = Instant::now(); |
| 6554 | 6629 | let inter = |
| 6555 | - encode_inter_frame(&padded_pic, &padded_ref, 100, 0.5, 1, (width, height), None, None, fctx).unwrap(); | |
| 6630 | + encode_inter_frame(&padded_pic, &padded_ref, 100, 0.5, 1, (width, height), None, None, None, fctx).unwrap(); | |
| 6556 | 6631 | let inter_t = t.elapsed(); |
| 6557 | 6632 | eprintln!( |
| 6558 | 6633 | "inter frame vs its own key frame: {inter_t:>9.2?} ({} bytes, inter share \ |
| @@ -6650,7 +6725,7 @@ mod tests { | ||
| 6650 | 6725 | stage_reset(); |
| 6651 | 6726 | let t = Instant::now(); |
| 6652 | 6727 | let inter = |
| 6653 | - encode_inter_frame(&padded_pic, &padded_ref, 100, 0.5, 1, (width, height), None, None, fctx).unwrap(); | |
| 6728 | + encode_inter_frame(&padded_pic, &padded_ref, 100, 0.5, 1, (width, height), None, None, None, fctx).unwrap(); | |
| 6654 | 6729 | let total_t = t.elapsed(); |
| 6655 | 6730 | let [motion_search, interpolation, transform_quant, entropy] = stage_read(); |
| 6656 | 6731 | let accounted = motion_search + transform_quant + entropy; |
| @@ -6827,7 +6902,7 @@ mod tests { | ||
| 6827 | 6902 | q, |
| 6828 | 6903 | 0.5, |
| 6829 | 6904 | 1, |
| 6830 | - (width, height), None, None, fctx, | |
| 6905 | + (width, height), None, None, None, fctx, | |
| 6831 | 6906 | ) |
| 6832 | 6907 | .unwrap(); |
| 6833 | 6908 | let baseline_psnr = psnr(&baseline.reconstruction.y, &inter_source.y); |
| @@ -6844,7 +6919,7 @@ mod tests { | ||
| 6844 | 6919 | q, |
| 6845 | 6920 | 0.5, |
| 6846 | 6921 | 1, |
| 6847 | - (width, height), None, None, fctx, | |
| 6922 | + (width, height), None, None, None, fctx, | |
| 6848 | 6923 | ) |
| 6849 | 6924 | .unwrap(); |
| 6850 | 6925 | let pruned_psnr = psnr(&pruned.reconstruction.y, &inter_source.y); |
crates/ec-av1/src/encoder.rsmodified+8 -0
| @@ -234,6 +234,9 @@ pub struct Av1Encoder { | ||
| 234 | 234 | /// The last key frame's own (padded) reconstruction: `GOLDEN_FRAME`, |
| 235 | 235 | /// which stays in DPB slot 1 until the next key frame refreshes it. |
| 236 | 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>, | |
| 237 | 240 | } |
| 238 | 241 | |
| 239 | 242 | /// The encoder stays `Send` now that it owns a `FrameCtx` (whose cells are |
| @@ -261,6 +264,7 @@ impl Av1Encoder { | ||
| 261 | 264 | Ok(Self { |
| 262 | 265 | carried_cdfs: None, |
| 263 | 266 | golden: None, |
| 267 | + prev2: None, | |
| 264 | 268 | color_config: config.colour.color_config(), |
| 265 | 269 | config, |
| 266 | 270 | reference: None, |
| @@ -367,6 +371,7 @@ impl Av1Encoder { | ||
| 367 | 371 | render, |
| 368 | 372 | self.carried_cdfs.as_ref().map(|c| &c.0), |
| 369 | 373 | self.golden.as_ref(), |
| 374 | + self.prev2.as_ref(), | |
| 370 | 375 | &self.fctx, |
| 371 | 376 | )? |
| 372 | 377 | }; |
| @@ -374,6 +379,9 @@ impl Av1Encoder { | ||
| 374 | 379 | self.carried_cdfs = Some(encoded.next_cdfs.clone()); |
| 375 | 380 | if is_key { |
| 376 | 381 | self.golden = Some(encoded.reconstruction.clone()); |
| 382 | + self.prev2 = None; | |
| 383 | + } else { | |
| 384 | + self.prev2 = self.reference.take(); | |
| 377 | 385 | } |
| 378 | 386 | self.reference = Some(encoded.reconstruction.clone()); |
| 379 | 387 | self.next_index += 1; |