Merge branch 'lane-hpmv' (8c75ea8a): the writer can code allow_high_precision_mv frames (mv_hp symbols, pricer, eighth-pel search stage); refuted as a default on both films, ships off
Browse files at this commit4 changed files +333 -18
- crates/ec-av1/src/encode.rs +144 -5
- crates/ec-av1/src/motion.rs +16 -3
- crates/ec-av1/src/tile.rs +65 -10
- lanes/hpmv.report.md +108 -0
crates/ec-av1/src/encode.rsmodified+144 -5
| @@ -2074,7 +2074,11 @@ pub fn inter_frame_headers_slots( | ||
| 2074 | 2074 | idx[6] = altref_slot; |
| 2075 | 2075 | idx |
| 2076 | 2076 | }, |
| 2077 | - allow_high_precision_mv: false, | |
| 2077 | + // lane-hpmv: libaom's own rule (`av1_pick_and_set_high_precision_mv`: | |
| 2078 | + // `use_hp = qindex < HIGH_PRECISION_MV_QTHRESH`), with this crate's | |
| 2079 | + // threshold in [`HP_MV_QTHRESH`]. `force_integer_mv` is false in | |
| 2080 | + // every frame this constructor builds, so no further guard is needed. | |
| 2081 | + allow_high_precision_mv: high_precision_mv_at(base_q_idx), | |
| 2078 | 2082 | // A FIXED frame-level filter, so no block codes an `interp_filter` |
| 2079 | 2083 | // symbol. lane-obmc3 instrumented whether that costs anything: a |
| 2080 | 2084 | // libaom `cpu-used 6` crf 41 stream of the long-GOP film B window |
| @@ -4482,6 +4486,13 @@ pub(crate) fn dump_newmv_census() { | ||
| 4482 | 4486 | v[7] as f64 / off / 64.0, |
| 4483 | 4487 | ); |
| 4484 | 4488 | } |
| 4489 | + // lane-hpmv: how many mv components this encode coded an eighth-pel bit | |
| 4490 | + // for, and how many of those named a vector no non-hp frame can code. | |
| 4491 | + let (hp_all, hp_eighth) = crate::tile::hp_symbols(); | |
| 4492 | + eprintln!( | |
| 4493 | + "NEWMVCENSUS hp symbols {hp_all}, of them eighth-pel {hp_eighth} ({:.1}%)", | |
| 4494 | + 100.0 * hp_eighth as f64 / hp_all.max(1) as f64, | |
| 4495 | + ); | |
| 4485 | 4496 | let g: [u64; 4] = std::array::from_fn(|i| NEWMV_CENSUS[32 + i].load(Relaxed)); |
| 4486 | 4497 | eprintln!( |
| 4487 | 4498 | "NEWMVCENSUS pre-RD margin skips: extra-reference {}/{} ({:.1}%), \ |
| @@ -8771,24 +8782,31 @@ fn mv_class_of(z: i32) -> usize { | ||
| 8771 | 8782 | /// through, not the tile writer's own adapting state, which this encoder has |
| 8772 | 8783 | /// no way to read without duplicating it block for block. |
| 8773 | 8784 | /// |
| 8774 | -/// Returns `None` when `diff` needs the eighth-pel precision this crate's | |
| 8775 | -/// frames never carry (`allow_high_precision_mv` is always off), which is | |
| 8776 | -/// what [`round_to_valid_mv`] exists to avoid ever happening. | |
| 8785 | +/// Returns `None` when `diff` needs eighth-pel precision in a frame coded | |
| 8786 | +/// without `allow_high_precision_mv`, which is what [`round_to_valid_mv`] | |
| 8787 | +/// exists to avoid ever happening. | |
| 8777 | 8788 | fn mv_component_bits(diff: i32) -> Option<f64> { |
| 8789 | + let hp_frame = crate::tile::high_precision_mv(); | |
| 8778 | 8790 | let mag = diff.unsigned_abs() as i32; |
| 8779 | 8791 | let z = mag - 1; |
| 8780 | - if z & 1 == 0 { | |
| 8792 | + if !hp_frame && z & 1 == 0 { | |
| 8781 | 8793 | return None; |
| 8782 | 8794 | } |
| 8783 | 8795 | let mut bits = symbol_bits(&cdf::MV_SIGN, usize::from(diff < 0)); |
| 8784 | 8796 | let class = mv_class_of(z); |
| 8785 | 8797 | bits += symbol_bits(&cdf::MV_CLASS, class); |
| 8786 | 8798 | let local = z - mv_class_base(class); |
| 8799 | + let hp = (local & 1) as usize; | |
| 8787 | 8800 | if class == 0 { |
| 8788 | 8801 | let bit = (local >> 3) & 1; |
| 8789 | 8802 | let fr = (local >> 1) & 3; |
| 8790 | 8803 | bits += symbol_bits(&cdf::MV_CLASS0_BIT, bit as usize); |
| 8791 | 8804 | bits += symbol_bits(&cdf::MV_CLASS0_FR[bit as usize], fr as usize); |
| 8805 | + // lane-hpmv: the writer codes the eighth-pel bit in a high-precision | |
| 8806 | + // frame, so the pricer charges for it there and only there. | |
| 8807 | + if hp_frame { | |
| 8808 | + bits += symbol_bits(&cdf::MV_CLASS0_HP, hp); | |
| 8809 | + } | |
| 8792 | 8810 | } else { |
| 8793 | 8811 | let d = local >> 3; |
| 8794 | 8812 | let fr = (local >> 1) & 3; |
| @@ -8796,6 +8814,9 @@ fn mv_component_bits(diff: i32) -> Option<f64> { | ||
| 8796 | 8814 | bits += symbol_bits(&cdf::MV_BIT[i], ((d >> i) & 1) as usize); |
| 8797 | 8815 | } |
| 8798 | 8816 | bits += symbol_bits(&cdf::MV_FR, fr as usize); |
| 8817 | + if hp_frame { | |
| 8818 | + bits += symbol_bits(&cdf::MV_HP, hp); | |
| 8819 | + } | |
| 8799 | 8820 | } |
| 8800 | 8821 | Some(bits) |
| 8801 | 8822 | } |
| @@ -8951,7 +8972,51 @@ fn mv_residual_bits(mv: (i32, i32), pred: (i32, i32)) -> Option<f64> { | ||
| 8951 | 8972 | /// down keeps every coded MV — and so every `nearest_mv` a later block's |
| 8952 | 8973 | /// stack votes with — built from an even displacement from `(0, 0)`, so the |
| 8953 | 8974 | /// invariant holds without this function seeing the whole stack. |
| 8975 | +/// A PROCESS-GLOBAL [`high_precision_mv_at`] override that wins over both | |
| 8976 | +/// the threshold and the environment (`u8::MAX` = unset, `0` = never, `1` = | |
| 8977 | +/// always), the shape [`set_frame_interp`] has: the witness that has to see | |
| 8978 | +/// `mv_hp` symbols coded turns it on through this while holding | |
| 8979 | +/// [`crate::speed::knob_write`]. | |
| 8980 | +static HP_MV_OVERRIDE: std::sync::atomic::AtomicU8 = std::sync::atomic::AtomicU8::new(u8::MAX); | |
| 8981 | + | |
| 8982 | +/// Sets [`HP_MV_OVERRIDE`]; `None` clears it. | |
| 8983 | +#[allow(dead_code)] // set only from the `#[cfg(test)]` gates | |
| 8984 | +pub(crate) fn set_high_precision_mv(on: Option<bool>) { | |
| 8985 | + HP_MV_OVERRIDE.store(on.map_or(u8::MAX, u8::from), std::sync::atomic::Ordering::Relaxed); | |
| 8986 | +} | |
| 8987 | + | |
| 8988 | +/// The qindex below which an inter frame codes `allow_high_precision_mv` | |
| 8989 | +/// (lane-hpmv). libaom's `HIGH_PRECISION_MV_QTHRESH` is 128; `0` is this | |
| 8990 | +/// crate's shipped value, i.e. high-precision motion vectors off in every | |
| 8991 | +/// frame -- see `lanes/hpmv.report.md` for the sweep that chose it. | |
| 8992 | +pub(crate) const HP_MV_QTHRESH: u8 = 0; | |
| 8993 | + | |
| 8994 | +/// Whether an inter frame at `base_q_idx` carries `allow_high_precision_mv`: | |
| 8995 | +/// [`HP_MV_QTHRESH`], overridable by `EC_AV1_HP_MV` (`0`/`off` never, `all` | |
| 8996 | +/// always, any number as the threshold) so the BD gate can sweep the policy | |
| 8997 | +/// without a rebuild. | |
| 8998 | +pub(crate) fn high_precision_mv_at(base_q_idx: u8) -> bool { | |
| 8999 | + match HP_MV_OVERRIDE.load(std::sync::atomic::Ordering::Relaxed) { | |
| 9000 | + 0 => return false, | |
| 9001 | + 1 => return true, | |
| 9002 | + _ => {} | |
| 9003 | + } | |
| 9004 | + static T: std::sync::OnceLock<u16> = std::sync::OnceLock::new(); | |
| 9005 | + let threshold = *T.get_or_init(|| match std::env::var("EC_AV1_HP_MV").ok().as_deref() { | |
| 9006 | + Some("all" | "always") => 256, | |
| 9007 | + Some("0" | "off") => 0, | |
| 9008 | + Some(v) => v.parse().unwrap_or(u16::from(HP_MV_QTHRESH)), | |
| 9009 | + None => u16::from(HP_MV_QTHRESH), | |
| 9010 | + }); | |
| 9011 | + u16::from(base_q_idx) < threshold | |
| 9012 | +} | |
| 9013 | + | |
| 8954 | 9014 | fn round_to_valid_mv(mv: (i32, i32), pred: (i32, i32)) -> (i32, i32) { |
| 9015 | + // lane-hpmv: with `allow_high_precision_mv` on, every eighth-pel | |
| 9016 | + // residual is codable and there is nothing to round away. | |
| 9017 | + if crate::tile::high_precision_mv() { | |
| 9018 | + return mv; | |
| 9019 | + } | |
| 8955 | 9020 | let round = |m: i32, p: i32| { |
| 8956 | 9021 | let diff = m - p; |
| 8957 | 9022 | let mag = diff.unsigned_abs() as i32; |
| @@ -11324,6 +11389,10 @@ pub(crate) fn encode_inter_frame( | ||
| 11324 | 11389 | // read `(0, 0, [0; 7])` and came out 1 for every reference -- the knob |
| 11325 | 11390 | // never reached the tool). |
| 11326 | 11391 | crate::tile::arm_order_hints(seq.order_hint_bits, order_hint, order_hints); |
| 11392 | + // lane-hpmv: armed here for the same reason the order hints are -- the | |
| 11393 | + // block SEARCH reads it (its eighth-pel stage) long before the writer. | |
| 11394 | + let hp_mv = header.allow_high_precision_mv; | |
| 11395 | + crate::tile::arm_high_precision_mv(hp_mv); | |
| 11327 | 11396 | if let Some(p) = pyramid { |
| 11328 | 11397 | header.show_frame = p.show_frame; |
| 11329 | 11398 | header.showable_frame = !p.show_frame; |
| @@ -11580,6 +11649,7 @@ pub(crate) fn encode_inter_frame( | ||
| 11580 | 11649 | // search step) is read by the SEARCH, so every worker arms it or the |
| 11581 | 11650 | // tile's bits depend on the thread count. |
| 11582 | 11651 | crate::tile::arm_order_hints(seq.order_hint_bits, order_hint, order_hints); |
| 11652 | + crate::tile::arm_high_precision_mv(hp_mv); | |
| 11583 | 11653 | crate::tile::arm_pricing_cdfs(start_cdfs, screen); |
| 11584 | 11654 | let rect = layout.rect(index); |
| 11585 | 11655 | let mut luma = fresh_plane(&picture_y8, frame_width, frame_height, true_width, true_height); |
| @@ -12265,6 +12335,7 @@ pub(crate) fn encode_inter_frame( | ||
| 12265 | 12335 | crate::tile::arm_motion_mode(switchable_motion_mode); |
| 12266 | 12336 | crate::tile::arm_warped_motion(warped_motion); |
| 12267 | 12337 | crate::tile::arm_order_hints(order_hint_bits, order_hint, order_hints); |
| 12338 | + crate::tile::arm_high_precision_mv(hp_mv); | |
| 12268 | 12339 | crate::tile::arm_screen(screen); |
| 12269 | 12340 | crate::tile::arm_filter_intra(filter_intra_on()); |
| 12270 | 12341 | let mut cdfs = start_cdfs.0.clone(); |
| @@ -18531,4 +18602,72 @@ mod tests { | ||
| 18531 | 18602 | } |
| 18532 | 18603 | } |
| 18533 | 18604 | |
| 18605 | + | |
| 18606 | + /// lane-hpmv: THE EIGHTH-PEL VECTOR REACHES THE STREAM. With | |
| 18607 | + /// `allow_high_precision_mv` off the writer refuses an odd eighth-pel | |
| 18608 | + /// residual outright (`round_to_valid_mv` rounds it away first), so the | |
| 18609 | + /// finest vector the encoder could commit to was a quarter pel. With the | |
| 18610 | + /// header bit on, the writer codes an `mv_hp`/`mv_class0_hp` symbol per | |
| 18611 | + /// non-zero component and the search's fourth stage refines to the | |
| 18612 | + /// eighth: this asserts ZERO hp symbols in the off arm, non-zero in the | |
| 18613 | + /// on arm, and -- the part that makes it a signal rather than a symbol | |
| 18614 | + /// count (class `fixture proves the symbol, not the signal`) -- that some | |
| 18615 | + /// of them come out ZERO, i.e. name a vector the off arm cannot code at | |
| 18616 | + /// all. Both streams are decoded by ffmpeg AND by this crate's own | |
| 18617 | + /// decoder and asserted sample-exact against the encoder's own | |
| 18618 | + /// reconstruction, which is what proves the header bit and the symbols | |
| 18619 | + /// agree. | |
| 18620 | + #[test] | |
| 18621 | + fn a_high_precision_frame_codes_eighth_pel_motion_vectors_both_decoders_read() { | |
| 18622 | + let _knobs = crate::speed::knob_write(); | |
| 18623 | + if !have_ffmpeg() { | |
| 18624 | + eprintln!("SKIP a_high_precision_frame_codes_eighth_pel_motion_vectors: no ffmpeg"); | |
| 18625 | + return; | |
| 18626 | + } | |
| 18627 | + let (width, height) = (256usize, 128usize); | |
| 18628 | + let pictures: Vec<Picture> = (0..3).map(|i| panned_test_card(width, height, i * 3)).collect(); | |
| 18629 | + let mut arms = Vec::new(); | |
| 18630 | + for hp in [false, true] { | |
| 18631 | + set_high_precision_mv(Some(hp)); | |
| 18632 | + let _ = crate::tile::take_hp_symbols(); | |
| 18633 | + let fctx = &crate::decode::FrameCtx::for_encoder(); | |
| 18634 | + let encoded = encode_sequence_with_ctx(&pictures, 100, 0.5, fctx) | |
| 18635 | + .unwrap_or_else(|e| panic!("hp={hp}: {e}")); | |
| 18636 | + let (symbols, eighth) = crate::tile::take_hp_symbols(); | |
| 18637 | + let decoded = ffmpeg_decode_sequence(&encoded.stream, width, height, pictures.len()); | |
| 18638 | + let ours = crate::stream::decode_stream(&encoded.stream).expect("our decoder"); | |
| 18639 | + assert_eq!(decoded.len(), pictures.len(), "hp={hp}: ffmpeg frame count"); | |
| 18640 | + assert_eq!(ours.len(), pictures.len(), "hp={hp}: our decoder frame count"); | |
| 18641 | + for (i, e) in encoded.frames.iter().enumerate() { | |
| 18642 | + for (who, got) in [("ffmpeg", &decoded[i]), ("our decoder", &ours[i])] { | |
| 18643 | + if let Some((plane, s, got, want)) = first_plane_mismatch(got, &e.reconstruction) | |
| 18644 | + { | |
| 18645 | + panic!( | |
| 18646 | + "hp={hp} frame {i} plane {plane} sample {s}: {who} decoded {got}, \ | |
| 18647 | + the encoder reconstructed {want}" | |
| 18648 | + ); | |
| 18649 | + } | |
| 18650 | + } | |
| 18651 | + } | |
| 18652 | + arms.push((symbols, eighth, encoded.stream)); | |
| 18653 | + } | |
| 18654 | + set_high_precision_mv(None); | |
| 18655 | + let (off, on) = (&arms[0], &arms[1]); | |
| 18656 | + assert_eq!(off.0, 0, "a frame without the header bit codes no hp symbol"); | |
| 18657 | + assert!(on.0 > 0, "a high-precision frame codes hp symbols (got {})", on.0); | |
| 18658 | + assert!( | |
| 18659 | + on.1 > 0, | |
| 18660 | + "a high-precision frame codes eighth-pel vectors ({} hp symbols, none of them zero)", | |
| 18661 | + on.0 | |
| 18662 | + ); | |
| 18663 | + assert_ne!(off.2, on.2, "the two arms coded the same bytes"); | |
| 18664 | + println!( | |
| 18665 | + "hp witness: off {} hp symbols / {} B, on {} hp symbols ({} eighth-pel) / {} B", | |
| 18666 | + off.0, | |
| 18667 | + off.2.len(), | |
| 18668 | + on.0, | |
| 18669 | + on.1, | |
| 18670 | + on.2.len() | |
| 18671 | + ); | |
| 18672 | + } | |
| 18534 | 18673 | } |
crates/ec-av1/src/motion.rsmodified+16 -3
| @@ -36,6 +36,13 @@ const HALF_PEL_Q3: i32 = 4; | ||
| 36 | 36 | /// refines by. Fixed by the unit, not swept. |
| 37 | 37 | const QUARTER_PEL_Q3: i32 = 2; |
| 38 | 38 | |
| 39 | +/// One eighth sample — the step of the fourth refinement stage, which runs | |
| 40 | +/// only in a frame coded with `allow_high_precision_mv` (lane-hpmv): without | |
| 41 | +/// that flag the writer cannot code an odd eighth-pel residual at all | |
| 42 | +/// (`encode::round_to_valid_mv` rounds it away), so refining to it would only | |
| 43 | +/// spend evaluations on vectors the stream cannot name. | |
| 44 | +const EIGHTH_PEL_Q3: i32 = 1; | |
| 45 | + | |
| 39 | 46 | /// The widest step the integer-pel search starts its log search at, in whole |
| 40 | 47 | /// samples. |
| 41 | 48 | /// |
| @@ -565,11 +572,17 @@ fn search_traced_from_step( | ||
| 565 | 572 | step_pel /= 2; |
| 566 | 573 | } |
| 567 | 574 | |
| 568 | - // Stages 2 and 3: one ±1-step refinement each at half-pel then | |
| 569 | - // quarter-pel, through the same cost function (so the refinement is | |
| 575 | + // Stages 2..4: one ±1-step refinement each at half-pel, quarter-pel and | |
| 576 | + // (high-precision frames only) eighth-pel, through the same cost function (so the refinement is | |
| 570 | 577 | // costed at the precision it commits to, not the integer-pel SAD alone). |
| 571 | 578 | let integer_evals = evals.get(); |
| 572 | - for step in [HALF_PEL_Q3, QUARTER_PEL_Q3] { | |
| 579 | + // lane-hpmv: the eighth-pel stage runs only in a high-precision frame. | |
| 580 | + let stages: &[i32] = if crate::tile::high_precision_mv() { | |
| 581 | + &[HALF_PEL_Q3, QUARTER_PEL_Q3, EIGHTH_PEL_Q3] | |
| 582 | + } else { | |
| 583 | + &[HALF_PEL_Q3, QUARTER_PEL_Q3] | |
| 584 | + }; | |
| 585 | + for &step in stages { | |
| 573 | 586 | // lane-newmv, class `search bounded by its heuristic`: ONE round per |
| 574 | 587 | // step means a subpel winner two half-pels away from the integer |
| 575 | 588 | // centre is unreachable. `EC_AV1_MV_SUBPEL_ITERS` runs the round |
crates/ec-av1/src/tile.rsmodified+65 -10
| @@ -819,6 +819,45 @@ pub(crate) fn order_hints() -> (u32, u32, [u32; 7]) { | ||
| 819 | 819 | ORDER_HINTS.with(std::cell::Cell::get) |
| 820 | 820 | } |
| 821 | 821 | |
| 822 | +thread_local! { | |
| 823 | + /// lane-hpmv: this frame's `allow_high_precision_mv`, armed per worker | |
| 824 | + /// beside [`arm_order_hints`] -- the writer's eighth-pel symbol, the | |
| 825 | + /// pricer's mv bits and the motion search's finest step all read it. | |
| 826 | + static HIGH_PRECISION_MV: std::cell::Cell<bool> = const { std::cell::Cell::new(false) }; | |
| 827 | +} | |
| 828 | + | |
| 829 | +/// Arms this frame's `allow_high_precision_mv` for the next tile written -- | |
| 830 | +/// or searched -- on this thread. | |
| 831 | +pub(crate) fn arm_high_precision_mv(on: bool) { | |
| 832 | + HIGH_PRECISION_MV.with(|c| c.set(on)); | |
| 833 | +} | |
| 834 | + | |
| 835 | +/// What [`arm_high_precision_mv`] last armed on this thread. | |
| 836 | +pub(crate) fn high_precision_mv() -> bool { | |
| 837 | + HIGH_PRECISION_MV.with(std::cell::Cell::get) | |
| 838 | +} | |
| 839 | + | |
| 840 | +/// lane-hpmv's witness counters: `[0]` counts every `mv_hp`/`mv_class0_hp` | |
| 841 | +/// symbol this process's writers coded (zero without | |
| 842 | +/// `allow_high_precision_mv`, one per non-zero mv component with it), `[1]` | |
| 843 | +/// counts the ones that came out ZERO -- an eighth-pel vector, i.e. one a | |
| 844 | +/// frame without the flag cannot name at all. | |
| 845 | +static HP_SYMBOLS: [std::sync::atomic::AtomicU64; 2] = | |
| 846 | + [std::sync::atomic::AtomicU64::new(0), std::sync::atomic::AtomicU64::new(0)]; | |
| 847 | + | |
| 848 | +/// Reads and clears [`HP_SYMBOLS`]: `(hp symbols coded, eighth-pel ones)`. | |
| 849 | +#[allow(dead_code)] // read only from the `#[cfg(test)]` witness | |
| 850 | +pub(crate) fn take_hp_symbols() -> (u64, u64) { | |
| 851 | + let all = HP_SYMBOLS[0].swap(0, std::sync::atomic::Ordering::Relaxed); | |
| 852 | + (all, HP_SYMBOLS[1].swap(0, std::sync::atomic::Ordering::Relaxed)) | |
| 853 | +} | |
| 854 | + | |
| 855 | +/// [`take_hp_symbols`] without clearing -- what the NEWMV census prints. | |
| 856 | +pub(crate) fn hp_symbols() -> (u64, u64) { | |
| 857 | + use std::sync::atomic::Ordering::Relaxed; | |
| 858 | + (HP_SYMBOLS[0].load(Relaxed), HP_SYMBOLS[1].load(Relaxed)) | |
| 859 | +} | |
| 860 | + | |
| 822 | 861 | /// Writer-side counterpart of decode.rs `read_compound_ref_frames` (spec |
| 823 | 862 | /// 5.11.25's `comp_reference_type`/`uni_comp_ref`/`comp_ref`/`comp_bwdref` |
| 824 | 863 | /// trees), symbol for symbol at the same contexts -- which are, as that |
| @@ -5765,22 +5804,24 @@ fn mv_class_of(z: i32) -> usize { | ||
| 5765 | 5804 | |
| 5766 | 5805 | /// Writes one motion vector component's non-zero diff (spec 5.11.32 |
| 5767 | 5806 | /// `read_mv_component`, run backwards): sign, class, then the class's own |
| 5768 | -/// bits. `allow_high_precision_mv` is off in every frame this writer codes, | |
| 5769 | -/// so the eighth-pel bit is always inferred as one rather than coded — which | |
| 5770 | -/// means only diffs whose eighth-pel bit really is one are representable; | |
| 5771 | -/// anything else is refused rather than rounded, since rounding would silently | |
| 5772 | -/// code a different vector than the caller asked for. | |
| 5807 | +/// bits, and (lane-hpmv) the eighth-pel bit when the frame armed through | |
| 5808 | +/// [`arm_high_precision_mv`] carries `allow_high_precision_mv`. Without that | |
| 5809 | +/// flag the eighth-pel bit is inferred as one rather than coded, so only | |
| 5810 | +/// diffs whose eighth-pel bit really is one are representable; anything else | |
| 5811 | +/// is refused rather than rounded, since rounding would silently code a | |
| 5812 | +/// different vector than the caller asked for. | |
| 5773 | 5813 | /// |
| 5774 | 5814 | /// # Errors |
| 5775 | -/// Returns an error when `diff` needs the eighth-pel precision this writer's | |
| 5776 | -/// frames do not carry. | |
| 5815 | +/// Returns an error when `diff` needs eighth-pel precision in a frame coded | |
| 5816 | +/// without `allow_high_precision_mv`. | |
| 5777 | 5817 | fn write_mv_component(enc: &mut SymbolEncoder, c: &mut MvComponentCdfs, diff: i32) -> Result<()> { |
| 5778 | 5818 | debug_assert_ne!(diff, 0); |
| 5819 | + let hp_frame = high_precision_mv(); | |
| 5779 | 5820 | let sign = diff < 0; |
| 5780 | 5821 | enc.symbol(usize::from(sign), &mut c.sign); |
| 5781 | 5822 | let mag = diff.unsigned_abs() as i32; |
| 5782 | 5823 | let z = mag - 1; |
| 5783 | - if z & 1 == 0 { | |
| 5824 | + if !hp_frame && z & 1 == 0 { | |
| 5784 | 5825 | return Err(Error::unsupported( |
| 5785 | 5826 | "AV1 tile", |
| 5786 | 5827 | "a motion vector component needs eighth-pel precision, which \ |
| @@ -5790,11 +5831,18 @@ fn write_mv_component(enc: &mut SymbolEncoder, c: &mut MvComponentCdfs, diff: i3 | ||
| 5790 | 5831 | let class = mv_class_of(z); |
| 5791 | 5832 | enc.symbol(class, &mut c.class); |
| 5792 | 5833 | let local = z - mv_class_base(class); |
| 5834 | + let hp = (local & 1) as usize; | |
| 5793 | 5835 | if class == 0 { |
| 5794 | 5836 | let bit = (local >> 3) & 1; |
| 5795 | 5837 | let fr = (local >> 1) & 3; |
| 5796 | 5838 | enc.symbol(bit as usize, &mut c.class0_bit); |
| 5797 | 5839 | enc.symbol(fr as usize, &mut c.class0_fr[bit as usize]); |
| 5840 | + // spec 5.11.32: the eighth-pel bit is CODED when the frame allows | |
| 5841 | + // high-precision motion vectors, and inferred as one otherwise ("if | |
| 5842 | + // (allow_high_precision_mv) mv_class0_hp ... else mv_class0_hp = 1"). | |
| 5843 | + if hp_frame { | |
| 5844 | + enc.symbol(hp, &mut c.class0_hp); | |
| 5845 | + } | |
| 5798 | 5846 | } else { |
| 5799 | 5847 | let d = local >> 3; |
| 5800 | 5848 | let fr = (local >> 1) & 3; |
| @@ -5802,9 +5850,16 @@ fn write_mv_component(enc: &mut SymbolEncoder, c: &mut MvComponentCdfs, diff: i3 | ||
| 5802 | 5850 | enc.symbol(((d >> i) & 1) as usize, &mut c.bit[i]); |
| 5803 | 5851 | } |
| 5804 | 5852 | enc.symbol(fr as usize, &mut c.fr); |
| 5853 | + if hp_frame { | |
| 5854 | + enc.symbol(hp, &mut c.hp); | |
| 5855 | + } | |
| 5856 | + } | |
| 5857 | + if hp_frame { | |
| 5858 | + HP_SYMBOLS[0].fetch_add(1, std::sync::atomic::Ordering::Relaxed); | |
| 5859 | + if hp == 0 { | |
| 5860 | + HP_SYMBOLS[1].fetch_add(1, std::sync::atomic::Ordering::Relaxed); | |
| 5861 | + } | |
| 5805 | 5862 | } |
| 5806 | - // The eighth-pel bit itself is inferred, not coded (spec 5.11.32: "if | |
| 5807 | - // (allow_high_precision_mv) mv_class0_hp ... else mv_class0_hp = 1"). | |
| 5808 | 5863 | Ok(()) |
| 5809 | 5864 | } |
| 5810 | 5865 |
lanes/hpmv.report.mdadded+108 -0
| @@ -0,0 +1,108 @@ | ||
| 1 | +# lane-hpmv — eighth-pel motion vectors: the capability, and why it does not ship on | |
| 2 | + | |
| 3 | +Worktree `edith_codecs-hpmv`, branch `lane-hpmv` off main 12c99b84. Every BD | |
| 4 | +row is the release lib-test binary (`bd_rate_screen_native`, 12 pictures, | |
| 5 | +`gop 12`, four quantizers, native `gate_crop`), each row detached under its own | |
| 6 | +`systemd-run` unit and read off the log's own clip line. | |
| 7 | + | |
| 8 | +## 1. What was missing | |
| 9 | + | |
| 10 | +`allow_high_precision_mv` was off in every frame this writer coded, so | |
| 11 | +`tile::write_mv_component` REFUSED any residual whose eighth-pel bit is zero | |
| 12 | +and `encode::round_to_valid_mv` rounded the search's winner down to an even | |
| 13 | +1/8-pel count first — the finest vector the encoder could commit to was a | |
| 14 | +quarter pel. The DECODER already read `mv_hp`/`mv_class0_hp` (it decodes | |
| 15 | +libaom's low-q streams exactly), so this lane is writer-side only. | |
| 16 | + | |
| 17 | +Built (commit `a2cc1120`): | |
| 18 | + | |
| 19 | +* the header bit, from libaom's own rule — `av1_pick_and_set_high_precision_mv` | |
| 20 | + (`mv_prec.c`): `use_hp = qindex < HIGH_PRECISION_MV_QTHRESH` (128). Ours is | |
| 21 | + `encode::HP_MV_QTHRESH`, swept through `EC_AV1_HP_MV` | |
| 22 | + (`0`/`off`, `all`, or a threshold). | |
| 23 | +* `write_mv_component`: the `mv_class0_hp` / `mv_hp` symbol per non-zero | |
| 24 | + component in a high-precision frame, nothing coded otherwise (spec 5.11.32). | |
| 25 | +* `encode::mv_component_bits`: the same symbol priced, and no `None` refusal | |
| 26 | + in a high-precision frame; `round_to_valid_mv` returns `mv` untouched there. | |
| 27 | +* `motion::search`: a FOURTH refinement stage at `EIGHTH_PEL_Q3`, run only in | |
| 28 | + a high-precision frame. | |
| 29 | +* the frame's bit armed per worker beside the order hints | |
| 30 | + (`tile::arm_high_precision_mv`), because the SEARCH reads it before the | |
| 31 | + writer does. | |
| 32 | + | |
| 33 | +Not needed: `lower_mv_precision`. The spec applies it to the GLOBAL and | |
| 34 | +TEMPORAL (tpl) candidates only; this encoder writes `use_ref_frame_mvs: false` | |
| 35 | +and identity global motion, and `mvstack`'s tpl entry points already take the | |
| 36 | +flag. Spatial stack candidates keep their own precision either way. | |
| 37 | + | |
| 38 | +## 2. Witness (red before, green after) | |
| 39 | + | |
| 40 | +`encode::tests::a_high_precision_frame_codes_eighth_pel_motion_vectors_both_decoders_read` | |
| 41 | +— three frames of a panned test card, encoded twice with | |
| 42 | +`set_high_precision_mv(Some(false|true))`: | |
| 43 | + | |
| 44 | +``` | |
| 45 | +hp witness: off 0 hp symbols / 862 B, on 6 hp symbols (5 eighth-pel) / 867 B | |
| 46 | +test ... ok | |
| 47 | +``` | |
| 48 | + | |
| 49 | +Both arms decode sample-exact (Y, U, V, every frame) through ffmpeg AND | |
| 50 | +`crate::stream::decode_stream`. RED BEFORE by construction: before this commit | |
| 51 | +no frame could carry the bit at all (`allow_high_precision_mv: false`, no | |
| 52 | +setter, and `write_mv_component` erroring on the eighth-pel residual), so the | |
| 53 | +`on` arm could not be encoded — the count could only ever be 0. The | |
| 54 | +"5 of 6 hp symbols are ZERO" assertion is the signal, not the symbol: a zero | |
| 55 | +hp bit names a vector a non-hp frame cannot code at all. | |
| 56 | + | |
| 57 | +## 3. The policy sweep — film B, 12 frames (the deciding row) | |
| 58 | + | |
| 59 | +| arm | vs libaom | vs rav1e | hp symbols | of them eighth-pel | | |
| 60 | +|---|---|---|---|---| | |
| 61 | +| **control (off)** | **+24.6** | **-2.0** | 0 | — | | |
| 62 | +| `qindex < 96` | +25.0 | -1.8 | 58019 | 52.6% | | |
| 63 | +| `qindex < 128` (libaom's rule) | +25.1 | -1.8 | — | — | | |
| 64 | +| always on | +26.6 | -0.3 | 108001 | 53.9% | | |
| 65 | + | |
| 66 | +Monotone worse in how much high precision is armed, on BOTH columns. The | |
| 67 | +capability fires: over half the hp symbols coded are ZERO bits, i.e. vectors | |
| 68 | +the control cannot name. | |
| 69 | + | |
| 70 | +NEWMV census, same clip and quantizers, control -> always-on: | |
| 71 | + | |
| 72 | +| side | won% | mv bits | mv==nearest | | |
| 73 | +|---|---|---|---| | |
| 74 | +| 8 | 31.5 -> 29.4 | 8.29 -> 9.56 | 36.7 -> 32.4 | | |
| 75 | +| 16 | 41.1 -> 40.7 | 7.99 -> 9.48 | 30.3 -> 26.0 | | |
| 76 | +| 32 | 24.3 -> 24.9 | 7.07 -> 8.38 | 35.5 -> 32.4 | | |
| 77 | + | |
| 78 | +Search evals/call 81.3 -> 93.6 (the fourth stage). So the finer vector is | |
| 79 | +FOUND and CODED; it just costs 1.2-1.5 more mv bits per coded `NEWMV` than the | |
| 80 | +extra prediction accuracy earns back on this content, and the NEWMV share | |
| 81 | +falls with it. This is not a flat/inert result — it is a priced loss. | |
| 82 | + | |
| 83 | +## 4. Film A, 12 frames (best arm only) | |
| 84 | + | |
| 85 | +| arm | vs libaom | vs rav1e | | |
| 86 | +|---|---|---| | |
| 87 | +| control | +20.3 | -5.0 | | |
| 88 | +| `qindex < 128` | +20.3 | -4.9 | | |
| 89 | + | |
| 90 | +Flat on libaom, 0.1 worse on rav1e. With film B 0.4-0.5 worse on libaom at | |
| 91 | +every threshold, the keep rule ("both film rows improve on both columns, or | |
| 92 | +one >=0.5 down and the other flat +-0.3") cannot be met by any arm, so the | |
| 93 | +screen row and the long-GOP arm were not run — no candidate could have shipped | |
| 94 | +on their result. | |
| 95 | + | |
| 96 | +## 5. What ships | |
| 97 | + | |
| 98 | +The CAPABILITY, default OFF: `HP_MV_QTHRESH = 0`. `EC_AV1_HP_MV` sweeps it | |
| 99 | +without a rebuild, `set_high_precision_mv` arms it from a test. No default | |
| 100 | +stream moved, so no byte pin moved: | |
| 101 | +`the_encoders_own_streams_are_byte_identical_to_their_pins` is green UNCHANGED | |
| 102 | +(`(150, 8252, ...), (60, 33053, ...)` still the pinned bytes). | |
| 103 | + | |
| 104 | +Worth re-testing when the block-level mv pricing changes: the loss is entirely | |
| 105 | +a rate loss on the mv syntax, so a lane that moves `MV_LAMBDA`/the DRL pricing | |
| 106 | +or ships a smarter `NEWMV` share may flip its sign. libaom's own rule reads | |
| 107 | +`qindex < 128` on OUR ladder as "the two lowest-q points", i.e. it is the | |
| 108 | +low-q end that pays for it there. |