diff --git a/crates/coreav-h264-encoder/src/api.rs b/crates/coreav-h264-encoder/src/api.rs index 19f16cae..3a340cf5 100644 --- a/crates/coreav-h264-encoder/src/api.rs +++ b/crates/coreav-h264-encoder/src/api.rs @@ -280,6 +280,7 @@ pub struct Encoder { pub(crate) coded_height: usize, pub(crate) reference: Option<[codec::FramePlane; 3]>, pub(crate) previous_mv_count: u8, + pub(crate) previous_motion: Vec, pub(crate) threads: usize, next_idr_id: u32, next_frame_num: u8, @@ -341,6 +342,7 @@ impl Encoder { coded_height, reference: None, previous_mv_count: 0, + previous_motion: Vec::new(), threads, next_idr_id: 0, next_frame_num: 0, @@ -391,6 +393,7 @@ impl Encoder { None }; self.previous_mv_count = encoded.previous_mv_count; + self.previous_motion = encoded.motion; Ok(AccessUnit { nals, reconstruction: encoded.reconstruction, diff --git a/crates/coreav-h264-encoder/src/codec.rs b/crates/coreav-h264-encoder/src/codec.rs index d1efef02..dec2f45e 100644 --- a/crates/coreav-h264-encoder/src/codec.rs +++ b/crates/coreav-h264-encoder/src/codec.rs @@ -1107,12 +1107,14 @@ pub(crate) struct EncodedPicture { pub(crate) reconstruction: Vec, pub(crate) planes: [FramePlane; 3], pub(crate) previous_mv_count: u8, + pub(crate) motion: Vec, } struct PictureSyntax { writer: Bits, pending_skip: u32, previous_mv_count: u8, + motion: Vec, } impl PictureSyntax { @@ -1148,6 +1150,7 @@ impl PictureSyntax { } macroblock(&mut self.writer, luma, chroma, inter.as_ref(), context)?; self.previous_mv_count = inter.as_ref().map_or(0, |i| i.len as u8); + self.motion.push(candidate.state.motion); if let Some(planes) = planes.as_deref_mut() { for (component, plane) in planes.iter_mut().enumerate() { plane.commit_candidate(candidate, component, mx, my)?; @@ -1210,12 +1213,24 @@ pub(crate) fn picture( ]; let mb_width = e.coded_width / 16; let mb_height = e.coded_height / 16; + let macroblocks = mb_width * mb_height; + #[cfg(test)] + let macroblocks = if crate::tests::MOTION_RESERVE_REFUSED.get() { + usize::MAX + } else { + macroblocks + }; + let mut motion = Vec::new(); + motion + .try_reserve_exact(macroblocks) + .map_err(|_| Error::Allocation)?; let mut writer = Bits::new(e.limits.access_unit_bytes); syntax::slice(&mut writer, e, slice)?; let mut output = PictureSyntax { writer, pending_skip: 0, previous_mv_count: e.previous_mv_count, + motion, }; let planes = if e.threads == 1 { let mut planes = planes; @@ -1300,6 +1315,7 @@ pub(crate) fn picture( reconstruction, planes, previous_mv_count: output.previous_mv_count, + motion: output.motion, }) } diff --git a/crates/coreav-h264-encoder/src/inter.rs b/crates/coreav-h264-encoder/src/inter.rs index c105fed9..cf3ab9c4 100644 --- a/crates/coreav-h264-encoder/src/inter.rs +++ b/crates/coreav-h264-encoder/src/inter.rs @@ -6,6 +6,8 @@ use coreav_kernels_h264::Block; const SPLIT_SATD: u64 = 64; const SETTLED_SPLIT_SATD: u64 = 2 * SPLIT_SATD; +const HEXAGON_ROUNDS: usize = 16; +const NEAR_DIVISOR: u64 = 8; #[derive(Clone, PartialEq, Eq)] pub(super) struct Inter { @@ -131,6 +133,7 @@ pub(super) struct Search<'a> { pub(super) mx: usize, pub(super) my: usize, pub(super) previous: u8, + pub(super) colocated: Option<&'a MotionMb>, } type Ranked = (u64, usize, Mv); @@ -649,21 +652,44 @@ impl Search<'_> { Ok(result) } - fn compact(&self, part: Part, predictor: Mv) -> Result { + fn compact(&self, part: Part, predictor: Mv, seeds: &[Option]) -> Result { let mut motions = Motions::new(); - for mv in [ - Mv::default(), - predictor, - Mv { - x: (predictor.x + 2).div_euclid(4) * 4, - y: (predictor.y + 2).div_euclid(4) * 4, - }, - ] { + let rounded = |mv: Mv| Mv { + x: (mv.x + 2).div_euclid(4) * 4, + y: (mv.y + 2).div_euclid(4) * 4, + }; + for mv in [Mv::default(), predictor, rounded(predictor)] + .into_iter() + .chain(seeds.iter().flatten().map(|&mv| rounded(mv))) + { self.score(part, mv, predictor, &mut motions)?; } - for step in [16, 8, 4] { + let quantum = (part.width * part.height) as u64 * (1u64 << (self.decision.qp / 6)); + let near = |motions: &Motions| { + motions + .integer + .is_some_and(|best| best.0 * NEAR_DIVISOR <= quantum) + }; + if !near(&motions) { + for _ in 0..HEXAGON_ROUNDS { + let origin = motions.integer.ok_or(Error::Internal)?.2; + for (dx, dy) in [(8, 0), (-8, 0), (4, 8), (-4, 8), (4, -8), (-4, -8)] { + self.score( + part, + Mv { + x: origin.x + dx, + y: origin.y + dy, + }, + predictor, + &mut motions, + )?; + } + if motions.integer.ok_or(Error::Internal)?.2 == origin { + break; + } + } let origin = motions.integer.ok_or(Error::Internal)?.2; - for (dx, dy) in [(step, 0), (-step, 0), (0, step), (0, -step)] { + for (dx, dy) in [(4, 0), (-4, 0), (0, 4), (0, -4)] { self.score( part, Mv { @@ -676,7 +702,8 @@ impl Search<'_> { } } let mut centre = motions.integer.ok_or(Error::Internal)?; - for step in [2, 1] { + let steps: &[i32] = if near(&motions) { &[2] } else { &[2, 1] }; + for &step in steps { let origin = centre.2; for (dx, dy) in [(step, 0), (-step, 0), (0, step), (0, -step)] { if let Some(value) = self.score( @@ -775,7 +802,12 @@ impl Search<'_> { self.decision.cost(0, prefix.len + bits) } - fn propose(&self, layout: Layout, ceiling: Option) -> Result> { + fn propose( + &self, + layout: Layout, + ceiling: Option, + seed: Option, + ) -> Result> { let mut inter = Inter::new(layout)?; if !motion::admitted(self.previous, inter.len) { return Ok(None); @@ -793,7 +825,12 @@ impl Search<'_> { let mut rank = 0; for part in inter.parts.iter_mut().take(inter.len) { let predictor = self.border.predictor(&grid, *part, layout)?; - let (cost, _, mv) = self.compact(*part, predictor)?; + let [a, b, c] = self.border.neighbour_vectors(&grid, *part)?; + let colocated = self + .colocated + .filter(|mb| !mb.intra) + .and_then(|mb| mb.vectors.get(part.y / 4 * 4 + part.x / 4).copied()); + let (cost, _, mv) = self.compact(*part, predictor, &[a, b, c, colocated, seed])?; rank += cost; part.mv = mv; motion::commit(&mut grid, *part)?; @@ -869,7 +906,7 @@ impl Search<'_> { } } let ceiling = best.as_ref().map(|candidate| candidate.key.cost); - let whole = self.propose(Layout::Whole, ceiling)?; + let whole = self.propose(Layout::Whole, ceiling, None)?; let bound = if settled { SETTLED_SPLIT_SATD } else { @@ -879,7 +916,14 @@ impl Search<'_> { .as_ref() .is_none_or(|plan| plan.2 > bound << (self.decision.qp / 6)) { - self.propose(Layout::Sub([0; 4]), ceiling)? + self.propose( + Layout::Sub([0; 4]), + ceiling, + whole + .as_ref() + .and_then(|plan| plan.1.parts.first()) + .map(|part| part.mv), + )? } else { None }; @@ -991,6 +1035,7 @@ mod tests { mx: 0, my: 0, previous: 0, + colocated: None, }; let part = Inter::new(Layout::Whole).unwrap().parts[0]; for predictor in [Mv::default(), Mv { x: 3, y: -7 }, Mv { x: 8191, y: 2047 }] { @@ -998,10 +1043,10 @@ mod tests { for _ in 0..2 { PROBES.with(|count| count.set(0)); RESCORES.with(|count| count.set(0)); - let value = search.compact(part, predictor).unwrap(); + let value = search.compact(part, predictor, &[]).unwrap(); assert!(value.2.valid()); assert!(value.2.difference(predictor).is_ok()); - assert!(PROBES.with(|count| count.get()) <= 23); + assert!(PROBES.with(|count| count.get()) <= PART_PROBES); assert!((1..=3).contains(&RESCORES.with(|count| count.get()))); if let Some(old) = selected { assert_eq!(value, old); @@ -1061,6 +1106,7 @@ mod tests { mx: origin, my: origin, previous: 12, + colocated: None, }; for (vectors, layout) in [ ([a, a, a, a], Layout::Whole), @@ -1143,8 +1189,9 @@ mod tests { mx: 0, my: 0, previous: 0, + colocated: None, }; - let whole = search.propose(Layout::Whole, None).unwrap().unwrap(); + let whole = search.propose(Layout::Whole, None, None).unwrap().unwrap(); assert_eq!(whole.2 > SPLIT_SATD << (28 / 6), searches == 2); SEARCHES.with(|count| count.set(0)); search @@ -1203,8 +1250,9 @@ mod tests { mx: 0, my: 0, previous: 0, + colocated: None, }; - let whole = search.propose(Layout::Whole, None).unwrap().unwrap(); + let whole = search.propose(Layout::Whole, None, None).unwrap().unwrap(); SEARCHES.with(|count| count.set(0)); let scope = search .candidates(&mut None, &mut Error::Coefficient) @@ -1257,6 +1305,7 @@ mod tests { mx: 0, my: 0, previous: 0, + colocated: None, }; EVALUATIONS.with(|count| count.set(0)); SEARCHES.with(|count| count.set(0)); @@ -1283,13 +1332,13 @@ mod tests { let floor = search.syntax_floor(3).unwrap(); assert!( search - .propose(Layout::Whole, Some(floor - 1)) + .propose(Layout::Whole, Some(floor - 1), None) .unwrap() .is_none() ); assert!( search - .propose(Layout::Whole, Some(floor)) + .propose(Layout::Whole, Some(floor), None) .unwrap() .is_some() ); @@ -1342,6 +1391,7 @@ mod tests { mx: 0, my: 0, previous: 0, + colocated: None, }; let mut skip = Inter::new(Layout::Whole).unwrap(); skip.skip = true; @@ -1380,6 +1430,67 @@ mod tests { } } + const PART_PROBES: usize = 3 + 5 + 6 * HEXAGON_ROUNDS + 4 + 8; + + #[test] + fn colocated_and_whole_block_seeds_reach_motion_beyond_the_pattern() { + let (width, height) = (160, 32); + let mut reference = [ + FramePlane::new(width, height).unwrap(), + FramePlane::new(width / 2, height / 2).unwrap(), + FramePlane::new(width / 2, height / 2).unwrap(), + ]; + let mut seed = 0x2545_f491u32; + for sample in &mut reference[0].data { + seed ^= seed << 13; + seed ^= seed >> 17; + seed ^= seed << 5; + *sample = (seed >> 24) as u8; + } + let far = Mv { x: 4 * 96, y: 0 }; + let mut source = Tile::empty(); + for y in 0..16 { + for x in 0..16 { + source.0[Tile::at(x, y)] = reference[0].data[y * width + 16 + 96 + x]; + } + } + let csources = [Tile::empty(), Tile::empty()]; + let decision = Decision { + source: &source, + csources: &csources, + context: MbContext { + top: None, + left: None, + p_slice: true, + }, + qp: 28, + pending_skip: 0, + }; + let moving = MotionMb { + intra: false, + vectors: [far; 16], + }; + let intra = MotionMb::default(); + let chosen = |colocated, layout, seed| { + let search = Search { + reference: &reference, + decision: &decision, + border: Border::default(), + mx: 1, + my: 0, + previous: 0, + colocated, + }; + let (_, inter, _) = search.propose(layout, None, seed).unwrap().unwrap(); + inter.motion().unwrap().vectors + }; + assert_eq!(chosen(Some(&moving), Layout::Whole, None), [far; 16]); + assert_ne!(chosen(None, Layout::Whole, None), [far; 16]); + assert_ne!(chosen(Some(&intra), Layout::Whole, None), [far; 16]); + assert_eq!(chosen(None, Layout::Sub([0; 4]), Some(far)), [far; 16]); + assert_ne!(chosen(None, Layout::Sub([0; 4]), None), [far; 16]); + } + #[test] fn compact_search_bounds_probes_and_complete_inter_evaluations() { let reference = [ @@ -1408,6 +1519,7 @@ mod tests { mx: 0, my: 0, previous, + colocated: None, }; PROBES.with(|count| count.set(0)); EVALUATIONS.with(|count| count.set(0)); @@ -1415,7 +1527,7 @@ mod tests { search .candidates(&mut best, &mut Error::Coefficient) .unwrap(); - assert!(PROBES.with(|count| count.get()) <= 115); + assert!(PROBES.with(|count| count.get()) <= 5 * PART_PROBES); assert!(EVALUATIONS.with(|count| count.get()) <= 3); assert_eq!(best.is_some(), previous < 16); if let Some(best) = best { @@ -1439,6 +1551,7 @@ mod tests { mx: 0, my: 0, previous: 0, + colocated: None, }; let part = Inter::new(Layout::Whole).unwrap().parts[0]; for predictor in [ @@ -1448,10 +1561,10 @@ mod tests { Mv { x: 8191, y: 2047 }, ] { PROBES.with(|count| count.set(0)); - let selected = search.compact(part, predictor).unwrap(); + let selected = search.compact(part, predictor, &[]).unwrap(); assert_eq!(selected.2, predictor); - assert!(PROBES.with(|count| count.get()) <= 23); - assert_eq!(search.compact(part, predictor).unwrap(), selected); + assert!(PROBES.with(|count| count.get()) <= PART_PROBES); + assert_eq!(search.compact(part, predictor, &[]).unwrap(), selected); } } @@ -1607,6 +1720,7 @@ mod tests { mx: 0, my: 0, previous: 0, + colocated: None, }; let mut skip = Inter::new(Layout::Whole).unwrap(); skip.skip = true; @@ -1706,6 +1820,7 @@ mod tests { mx: 0, my: 0, previous: 0, + colocated: None, }; let mut best = None; SEARCHES.with(|count| count.set(0)); @@ -1827,6 +1942,7 @@ mod tests { mx: origin, my: origin, previous: 0, + colocated: None, }; for x in [-8192, -64, -4, 0, 4, 64, 8188] { for y in [-2048, -64, -4, 0, 4, 64, 2044] { @@ -1899,6 +2015,7 @@ mod tests { mx: origin, my: origin, previous: 0, + colocated: None, }; for (width, height) in [(16, 16), (16, 8), (8, 16), (8, 8), (8, 4), (4, 8), (4, 4)] @@ -1984,6 +2101,7 @@ mod tests { mx: 0, my: 0, previous: 0, + colocated: None, }; let part = Inter::new(Layout::Whole).unwrap().parts[0]; let mut motions = Motions::new(); @@ -2187,6 +2305,7 @@ mod tests { mx: 0, my: 0, previous: 0, + colocated: None, }; let part = Inter::new(Layout::Whole).unwrap().parts[0]; for predictor in [ @@ -2260,6 +2379,7 @@ mod tests { mx: corner, my: corner, previous: 0, + colocated: None, }; for layout in [ Layout::Whole, @@ -2539,6 +2659,7 @@ mod tests { mx: 0, my: 0, previous: 0, + colocated: None, }; let mut inter = Inter::new(layout).unwrap(); for (i, p) in inter.parts.iter_mut().take(inter.len).enumerate() { @@ -2748,6 +2869,7 @@ mod tests { mx: 0, my: 0, previous: 0, + colocated: None, }; let a = IntraAvail::default(); let intra = decision @@ -2764,6 +2886,7 @@ mod tests { assert!(cheap.key < intra.key); let forbidden = Search { previous: 16, + colocated: None, ..search }; assert!(matches!(forbidden.evaluate(skip), Err(Error::Syntax))); @@ -2778,6 +2901,7 @@ mod tests { assert!(matches!( Search { previous: 1, + colocated: None, ..search } .evaluate(fine), @@ -2830,6 +2954,7 @@ mod tests { mx: 0, my: 0, previous: 0, + colocated: None, }; let base = Inter::new(Layout::Whole).unwrap(); let best = search diff --git a/crates/coreav-h264-encoder/src/motion.rs b/crates/coreav-h264-encoder/src/motion.rs index 3b9ecccd..f728d860 100644 --- a/crates/coreav-h264-encoder/src/motion.rs +++ b/crates/coreav-h264-encoder/src/motion.rs @@ -33,7 +33,7 @@ impl Mv { } } -#[derive(Clone, Copy)] +#[derive(Clone, Copy, Debug, PartialEq, Eq)] pub(crate) struct MotionMb { pub(crate) intra: bool, pub(crate) vectors: [Mv; 16], @@ -205,6 +205,16 @@ impl Border<'_> { }) } + pub(crate) fn neighbour_vectors( + self, + grid: &[Option; 16], + part: Part, + ) -> Result<[Option; 3]> { + Ok(self + .neighbours(grid, part)? + .map(|sample| sample.reference.then_some(sample.mv))) + } + pub(crate) fn skip(self) -> Result { let part = Part { width: 16, diff --git a/crates/coreav-h264-encoder/src/rows.rs b/crates/coreav-h264-encoder/src/rows.rs index 5bf53e83..ccd541a4 100644 --- a/crates/coreav-h264-encoder/src/rows.rs +++ b/crates/coreav-h264-encoder/src/rows.rs @@ -182,6 +182,7 @@ pub(super) fn decide( mx, my, previous, + colocated: e.previous_motion.get(address), } .candidates(&mut best, &mut refusal)? }; diff --git a/crates/coreav-h264-encoder/src/tests.rs b/crates/coreav-h264-encoder/src/tests.rs index 6b86d309..9323bc84 100644 --- a/crates/coreav-h264-encoder/src/tests.rs +++ b/crates/coreav-h264-encoder/src/tests.rs @@ -4,6 +4,10 @@ use super::*; use bits::Bits; +std::thread_local! { + pub(crate) static MOTION_RESERVE_REFUSED: std::cell::Cell = const { std::cell::Cell::new(false) }; +} + fn settings(width: usize, height: usize, qp: u8) -> Settings { Settings { frame_rate: FrameRate::default(), @@ -801,6 +805,37 @@ fn p_failure_retains_reference_identity_frame_number_and_carried_count() { assert!(api::zeros::(usize::MAX).is_err()); } +#[test] +fn refused_motion_field_reservation_fails_without_advancing_sequence_state() { + let mut s = settings(16, 16, 34); + s.gop = Gop::Fixed { idr_interval: 3 }; + let y = [104; 256]; + let cb = [92; 64]; + let cr = [156; 64]; + let picture = Picture { + y: Plane::new(&y, 16, 16, 16).unwrap(), + cb: Plane::new(&cb, 8, 8, 8).unwrap(), + cr: Plane::new(&cr, 8, 8, 8).unwrap(), + }; + let mut encoder = Encoder::new(s, Limits::default()).unwrap(); + let mut reference = Encoder::new(s, Limits::default()).unwrap(); + for _ in 0..4 { + let motion = encoder.previous_motion.clone(); + let count = encoder.previous_mv_count; + MOTION_RESERVE_REFUSED.set(true); + let refused = encoder.encode(picture); + MOTION_RESERVE_REFUSED.set(false); + assert_eq!(refused.unwrap_err(), Error::Allocation); + assert_eq!(encoder.previous_motion, motion); + assert_eq!(encoder.previous_mv_count, count); + let retried = encoder.encode(picture).unwrap(); + let fresh = reference.encode(picture).unwrap(); + assert_eq!(retried.nals, fresh.nals); + assert_eq!(retried.reconstruction, fresh.reconstruction); + assert_eq!(encoder.previous_motion, reference.previous_motion); + } +} + #[test] fn complete_skip_sequence_wraps_frame_numbers_then_resets_at_idr() { let mut s = settings(16, 16, 34); @@ -1043,7 +1078,16 @@ fn thread_counts_preserve_streams_reconstruction_and_sequence_rollback() { assert_eq!(outputs[0].nals, other.nals); assert_eq!(outputs[0].reconstruction, other.reconstruction); } + let motion = &control.previous_motion; + assert_eq!(motion.len(), width.div_ceil(16) * height.div_ceil(16)); + if frame % 3 == 0 { + assert!(motion.iter().all(|mb| mb.intra)); + } + for encoder in &encoders { + assert_eq!(&encoder.previous_motion, motion); + } for encoder in &mut encoders { + let motion = encoder.previous_motion.clone(); let previous = encoder.previous_mv_count; let old = encoder.limits.access_unit_bytes; encoder.limits.access_unit_bytes = 1; @@ -1054,6 +1098,7 @@ fn thread_counts_preserve_streams_reconstruction_and_sequence_rollback() { encoder.limits.total_bytes = 0; assert!(matches!(encoder.encode(picture), Err(Error::Limit))); assert_eq!(encoder.previous_mv_count, previous); + assert_eq!(encoder.previous_motion, motion); encoder.limits.total_bytes = old; } } diff --git a/docs/design/h264-encoder-search.md b/docs/design/h264-encoder-search.md index ece753f1..2c295d8b 100644 --- a/docs/design/h264-encoder-search.md +++ b/docs/design/h264-encoder-search.md @@ -20,7 +20,7 @@ Keep the opt-in feature, safe scalar Rust, fixed QP, GOP, colour signalling, fil Search only P_L0_16x16 and P_8x8 with four 8x8 partitions in normal mode decision. Do not search 16x8, 8x16, or smaller subpartitions. Those tools remain representable and independently tested, but no longer multiply the ordinary search tree. -For each partition, score zero, the normative predictor, and its rounded integer position. Starting at the best integer seed, test four axial neighbours at integer steps of four, two, and one luma samples, with one fixed-centre pass per step. Then test four axial half-sample neighbours and four axial quarter-sample neighbours. Keep the best result after each stage. This is at most 23 probe calls per partition, or 115 across both layouts, including seeds and duplicates. Predictors outside the local range remain legal seeds. Reject, never clip, invalid vectors or differences. Retain deterministic cost, motion-length, and vector ties. +For each partition, score zero, the normative predictor, and its rounded integer position, then the rounded vectors of the admitted left, top and top-right (or top-left) neighbours, the co-located vector of the previous picture, and, for the 8x8 regions, the whole-block proposal. These are search seeds, not changes to normative vector prediction. The previous picture is complete before the current one starts, so the co-located seed is identical at every worker count. Unless the best integer score is already at most one eighth of the QP's SAD scale per sample, repeat a six-point hexagon of two-sample radius around the best integer vector until its centre wins, for at most 16 rounds, then test four axial one-sample neighbours. Then test four axial half-sample neighbours, and four axial quarter-sample neighbours unless the integer score met the same bound. Keep the best result after each stage. This is at most 116 probe calls per partition, including seeds and duplicates, and at most 71 distinct scored vectors. The section "Seeded hexagon search" records the measured change from the earlier fixed 23-probe plus pattern. Predictors outside the local range remain legal seeds. Reject, never clip, invalid vectors or differences. Retain deterministic cost, motion-length, and vector ties. Build each layout greedily in syntax order with a private motion map. Rank complete layouts by luma SAD plus the existing SAD-scale penalty times complete inter-prediction syntax length. Count that syntax after deriving all motion differences. Do not double-count motion lengths already present in partition scores. This is a cheap proxy, not full RDO or a lower bound on it. @@ -198,3 +198,34 @@ The final operating points are: All inputs are already lossy. A's preparation is unchanged. C is resized from 960x812 to 480x406, padded at the bottom to 480x416, and converted from 1440/49 to 30 pictures per second. Its sixty prepared pictures contain 59 distinct images; mean adjacent luma absolute difference is 0.724 and the maximum is 4.530. The prepared digest and exact conversion dimensions are in the raw record. B remains a low-motion sanity check only: at QP 28, final preserves the bounded encoder's complete stream, 126.776 kbit/s, 44.152652 dB PSNR and 0.992678 SSIM. Its single-call timing is not part of the paired comparison. Every newly encoded native curve and sanity cell matches pinned FFmpeg reconstruction exactly across all sixty pictures, and the repeated timing outputs match their verified cells. x264 uses the corrected input-before-output colour declaration, with digest-matched raw passthrough. Independent raw squared error confirms every PSNR; both metrics retain sixty per-picture records. The raw record names the implementation commit and executable digests; x264 has no encoder-side reconstruction check, so its `oracle_exact` field is null rather than a failed comparison. No new source study, SIMD, unsafe code, or default-build dependency was introduced. + +## Seeded hexagon search + +The fixed plus pattern reached at most seven samples from its best seed. Shortening its steps cost one real-motion input over 40% BD-rate, and dropping the quarter-sample stage cost another 35%. Both losses point to seeds that start too far from real motion, not to too few probes. This step adds the neighbour, co-located and whole-block seeds, replaces the plus with the repeated hexagon, and adds the integer early stop described in step 1. It was chosen from eleven measured alternatives. Adding the seeds alone gives most of the compression gain. Stopping the whole sub-sample stage at the same bound saved about 0.5 points more instructions but cost one input +0.43% U; a bound of a quarter of the SAD scale cost that input +3.9% Y. + +Reference measurement against main at `ba38f93ee523` on one GitHub-hosted x86-64 runner (Intel Xeon Platinum 8573C, four logical CPUs, two threads per core; rustc 1.99.1). The inputs are the five existing lossy reference inputs A–E, 60 pictures each, GOP 32, with deblocking enabled. BD-rate uses QP 22, 28, 34 and 40, PSNR from squared error, and PCHIP on log rate over the common interval without extrapolation. Timing uses QP 22 and 28, five retained calls per arm and cell after one warm-up, alternating arm order and `taskset` pinning; only `Encoder::encode` is timed. Instruction counts come from Callgrind over the encode call. Every stream and reconstruction matches between one and four worker threads and across timed repeats (450 checks), and every stream decodes with the pinned FFmpeg 9 oracle to the reconstruction (60 decodes). The implementation reproduces the measured prototype byte for byte at all 20 curve points. These are shared-runner reference figures, not a fixed-host result. + +| Input | Y | U | V | +|---|---:|---:|---:| +| A | −0.41% | −0.40% | −2.02% | +| B | −0.02% | −0.03% | −0.02% | +| C | −0.21% | −0.06% | −0.22% | +| D | −14.16% | −14.68% | −15.09% | +| E | −7.14% | −7.53% | −6.79% | +| Mean | −4.39% | −4.54% | −4.83% | + +| Cell | Instructions removed | One thread: main s | One thread: change s | Saved | Four threads: main s | Four threads: change s | Saved | +|---|---:|---:|---:|---:|---:|---:|---:| +| A-22 | 4.90% | 0.3587 | 0.3476 | 3.09% | 0.2195 | 0.2155 | 1.83% | +| A-28 | 5.20% | 0.3534 | 0.3528 | 0.15% | 0.2235 | 0.2192 | 1.90% | +| B-22 | 2.50% | 0.3090 | 0.3052 | 1.23% | 0.1992 | 0.1979 | 0.68% | +| B-28 | 1.66% | 0.2640 | 0.2623 | 0.65% | 0.1777 | 0.1712 | 3.63% | +| C-22 | 1.56% | 0.5804 | 0.5718 | 1.48% | 0.2866 | 0.2852 | 0.49% | +| C-28 | 1.40% | 0.3955 | 0.3894 | 1.55% | 0.2118 | 0.2113 | 0.24% | +| D-22 | 3.31% | 1.6427 | 1.5981 | 2.72% | 0.7418 | 0.7296 | 1.63% | +| D-28 | 5.18% | 1.2162 | 1.1700 | 3.79% | 0.5645 | 0.5420 | 3.98% | +| E-22 | 3.01% | 2.2394 | 2.1798 | 2.66% | 0.9821 | 0.9603 | 2.22% | +| E-28 | 3.17% | 1.7174 | 1.6891 | 1.65% | 0.7761 | 0.7574 | 2.42% | +| Mean | 3.19% | | | 1.90% | | | 1.90% | + +Times are medians in seconds. On these shared runners an output-identical control measured within about ±1.5% of main, so the instruction counts are the stable figure. A per-picture half-sample plane precomputation was also measured: it kept every byte, but added 9.1% instructions, because quiet skips and early stops leave few sub-sample probes per macroblock to pay for it.