Merge branch 'lane-picalloc' (a7153af4): new frame planes allocated uninitialised instead of zero-filled; EC_AV1_PLANE_SENTINEL=1 fills 0xDEAD and hashes identically on every gate stream and film cut, proving every sample is written before read; -2% task-clock single-thread
Browse files at this commit1 changed file +38 -6
- crates/ec-av1/src/decode.rs +38 -6
crates/ec-av1/src/decode.rsmodified+38 -6
| @@ -12876,6 +12876,38 @@ fn dump_stage(var: &str, y: &PlaneBuf<'_>, u: &PlaneBuf<'_>, v: &PlaneBuf<'_>) { | ||
| 12876 | 12876 | } |
| 12877 | 12877 | } |
| 12878 | 12878 | |
| 12879 | +/// The value [`fresh_plane`] fills with under `EC_AV1_PLANE_SENTINEL`: no | |
| 12880 | +/// legal sample can equal it (reconstruction clamps to `(1 << bit_depth) - 1`, | |
| 12881 | +/// at most 4095), so a decode that hashes identically under the flag proves | |
| 12882 | +/// the output does not depend on the plane buffer's initial content. | |
| 12883 | +const PLANE_SENTINEL: u16 = 0xDEAD; | |
| 12884 | + | |
| 12885 | +/// A frame plane's own backing store, `n` samples, UNINITIALISED. | |
| 12886 | +/// | |
| 12887 | +/// (lane-picalloc) The SB-padded coding surface is written sample-for-sample | |
| 12888 | +/// by the tile walk's reconstruction before anything reads it, so the | |
| 12889 | +/// `vec![0u16; n]` this replaces spent ~3% of the frame thread's cycles | |
| 12890 | +/// zeroing 25 MB (at 4K) that the very next pass overwrites -- serially, at | |
| 12891 | +/// the frame's head, on the critical path. `EC_AV1_PLANE_SENTINEL=1` swaps the | |
| 12892 | +/// uninitialised buffer for a [`PLANE_SENTINEL`] fill; the gate hashes both | |
| 12893 | +/// ways (see the lane report) and the two agree, which is the proof that no | |
| 12894 | +/// unwritten sample reaches the output. | |
| 12895 | +fn fresh_plane(n: usize) -> Vec<u16> { | |
| 12896 | + if crate::envflags::env_flag!("EC_AV1_PLANE_SENTINEL") { | |
| 12897 | + return vec![PLANE_SENTINEL; n]; | |
| 12898 | + } | |
| 12899 | + let mut v: Vec<u16> = Vec::with_capacity(n); | |
| 12900 | + // SAFETY: `u16` has no invalid bit patterns and no `Drop`, and the | |
| 12901 | + // capacity was just reserved, so the vector owns `n` initialised-or-not | |
| 12902 | + // `u16` slots. Every one is written by reconstruction before it is read | |
| 12903 | + // (proven by the sentinel gate above). | |
| 12904 | + #[allow(unsafe_code)] | |
| 12905 | + unsafe { | |
| 12906 | + v.set_len(n); | |
| 12907 | + } | |
| 12908 | + v | |
| 12909 | +} | |
| 12910 | + | |
| 12879 | 12911 | #[derive(Clone)] |
| 12880 | 12912 | struct PlaneBuf<'a> { |
| 12881 | 12913 | data: std::borrow::Cow<'a, [u16]>, |
| @@ -19943,7 +19975,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( | ||
| 19943 | 19975 | let (width, height) = (cols32 as usize * BLOCK, rows32 as usize * BLOCK); |
| 19944 | 19976 | |
| 19945 | 19977 | let mut y = PlaneBuf { |
| 19946 | - data: std::borrow::Cow::Owned(vec![0u16; width * height]), | |
| 19978 | + data: std::borrow::Cow::Owned(fresh_plane(width * height)), | |
| 19947 | 19979 | width, |
| 19948 | 19980 | height, |
| 19949 | 19981 | true_width, |
| @@ -19954,7 +19986,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( | ||
| 19954 | 19986 | tile_y1: height, |
| 19955 | 19987 | }; |
| 19956 | 19988 | let mut u = PlaneBuf { |
| 19957 | - data: std::borrow::Cow::Owned(vec![0u16; width * height / 4]), | |
| 19989 | + data: std::borrow::Cow::Owned(fresh_plane(width * height / 4)), | |
| 19958 | 19990 | width: width / 2, |
| 19959 | 19991 | height: height / 2, |
| 19960 | 19992 | true_width: true_width / 2, |
| @@ -19965,7 +19997,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( | ||
| 19965 | 19997 | tile_y1: height / 2, |
| 19966 | 19998 | }; |
| 19967 | 19999 | let mut v = PlaneBuf { |
| 19968 | - data: std::borrow::Cow::Owned(vec![0u16; width * height / 4]), | |
| 20000 | + data: std::borrow::Cow::Owned(fresh_plane(width * height / 4)), | |
| 19969 | 20001 | width: width / 2, |
| 19970 | 20002 | height: height / 2, |
| 19971 | 20003 | true_width: true_width / 2, |
| @@ -31521,7 +31553,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 31521 | 31553 | std::array::from_fn(|i| owned_refs[i].as_ref().map(|(a, b, c)| (a, b, c))); |
| 31522 | 31554 | |
| 31523 | 31555 | let mut y = PlaneBuf { |
| 31524 | - data: std::borrow::Cow::Owned(vec![0u16; width * height]), | |
| 31556 | + data: std::borrow::Cow::Owned(fresh_plane(width * height)), | |
| 31525 | 31557 | width, |
| 31526 | 31558 | height, |
| 31527 | 31559 | true_width, |
| @@ -31532,7 +31564,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 31532 | 31564 | tile_y1: height, |
| 31533 | 31565 | }; |
| 31534 | 31566 | let mut u = PlaneBuf { |
| 31535 | - data: std::borrow::Cow::Owned(vec![0u16; width * height / 4]), | |
| 31567 | + data: std::borrow::Cow::Owned(fresh_plane(width * height / 4)), | |
| 31536 | 31568 | width: width / 2, |
| 31537 | 31569 | height: height / 2, |
| 31538 | 31570 | true_width: true_width / 2, |
| @@ -31543,7 +31575,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 31543 | 31575 | tile_y1: height / 2, |
| 31544 | 31576 | }; |
| 31545 | 31577 | let mut v = PlaneBuf { |
| 31546 | - data: std::borrow::Cow::Owned(vec![0u16; width * height / 4]), | |
| 31578 | + data: std::borrow::Cow::Owned(fresh_plane(width * height / 4)), | |
| 31547 | 31579 | width: width / 2, |
| 31548 | 31580 | height: height / 2, |
| 31549 | 31581 | true_width: true_width / 2, |