wip(av1): lane-superres stage-3 gate in progress, committed at the 75-turn cap
Browse files at this commit0e6258a5fb5a5d8bb781e15837976ca1795cb464Parent: c262edf2 changed files +159 -12
- crates/ec-av1/src/stream.rs +129 -12
- crates/ec-av1/src/superres.rs +30 -0
crates/ec-av1/src/stream.rsmodified+129 -12
| @@ -185,20 +185,24 @@ pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> { | |||
| 185 | "a frame with segmentation enabled (this decoder never reads a per-block segment_id symbol)", | 185 | "a frame with segmentation enabled (this decoder never reads a per-block segment_id symbol)", |
| 186 | )); | 186 | )); |
| 187 | } | 187 | } |
| 188 | - // lane-superres Step 0: `use_superres` adds/removes no per-block | 188 | + // lane-superres stage 2/3: `use_superres` adds/removes no per-block |
| 189 | - // symbol (spec 7.16's upscaling is a pixel-domain post-process run | 189 | + // symbol (spec 7.16's upscaling is a pixel-domain post-process |
| 190 | - // after loop restoration, `av1_upscale_normative_*` in libaom's | 190 | + // libaom's `decodeframe.c` runs between CDEF and loop restoration, |
| 191 | - // `resize.c`), so this crate's tile readers stay bit-exact-in-sync | 191 | + // `av1_upscale_normative_*` in `resize.c`), so a key frame's tile |
| 192 | - // even without support. But every decode path below reads and | 192 | + // reads stay bit-exact in sync and `crate::superres::upscale_picture` |
| 193 | - // stores the frame at `header.frame_width` (the downscaled coded | 193 | + // (below, after decode) makes its `Picture` the spec-correct |
| 194 | - // width) and never runs the upscaler, so a superres stream would | 194 | + // `upscaled_width` instead of silently staying at `frame_width`. |
| 195 | - // silently produce a `Picture` sized `frame_width` instead of the | 195 | + // An INTER frame under `use_superres` still refuses: its reference |
| 196 | - // spec's `upscaled_width` -- SILENT WRONGNESS, not a desync. Refuse | 196 | + // is stored upscaled while the current frame decodes at the |
| 197 | - // by name until the upscaler (lane-superres) lands. | 197 | + // downscaled `frame_width`, so `decode_inter_frame_tile_with_cdfs`'s |
| 198 | - if header.use_superres { | 198 | + // motion compensation would sample a reference at the wrong scale |
| 199 | + // (spec 7.11.3.3's scaled-reference path is not ported) -- | ||
| 200 | + // SILENT WRONGNESS, not a desync, same as the key-frame case used | ||
| 201 | + // to be. Left for a later stage of this lane. | ||
| 202 | + if header.use_superres && header.frame_type != FrameType::Key { | ||
| 199 | return Err(Error::unsupported( | 203 | return Err(Error::unsupported( |
| 200 | "AV1 decode_stream", | 204 | "AV1 decode_stream", |
| 201 | - "a frame with use_superres set (this decoder never runs the spec 7.16 upscaler; it would silently output the frame at the downscaled frame_width instead of upscaled_width)", | 205 | + "an inter frame with use_superres set (this decoder never scales its motion-compensated reference to match the current frame's downscaled size, spec 7.11.3.3)", |
| 202 | )); | 206 | )); |
| 203 | } | 207 | } |
| 204 | // Loop restoration carries per-restoration-unit symbols in the tile | 208 | // Loop restoration carries per-restoration-unit symbols in the tile |
| @@ -392,6 +396,16 @@ pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> { | |||
| 392 | header.allow_screen_content_tools, | 396 | header.allow_screen_content_tools, |
| 393 | header.allow_intrabc, | 397 | header.allow_intrabc, |
| 394 | )?; | 398 | )?; |
| 399 | + // lane-superres stage 3, spec 7.16: upscale AFTER deblock+CDEF | ||
| 400 | + // (both already applied inside `decode_key_frame_tile_with_cdfs`) | ||
| 401 | + // and BEFORE this picture is stored as a reference or handed to | ||
| 402 | + // the caller -- loop restoration is not yet ported, so there is | ||
| 403 | + // no LR step here for the upscale to precede. | ||
| 404 | + let picture = if header.use_superres { | ||
| 405 | + crate::superres::upscale_picture(&picture, header.upscaled_width as usize) | ||
| 406 | + } else { | ||
| 407 | + picture | ||
| 408 | + }; | ||
| 395 | // A key frame codes no inter blocks -- its own saved motion | 409 | // A key frame codes no inter blocks -- its own saved motion |
| 396 | // field has no cells set, matching libaom's own "intra frame | 410 | // field has no cells set, matching libaom's own "intra frame |
| 397 | // contributes nothing to a later projection" behaviour, but | 411 | // contributes nothing to a later projection" behaviour, but |
| @@ -3796,6 +3810,109 @@ mod tests { | |||
| 3796 | eprintln!("{NAME}: refused by name: {msg}"); | 3810 | eprintln!("{NAME}: refused by name: {msg}"); |
| 3797 | } | 3811 | } |
| 3798 | 3812 | ||
| 3813 | + /// lane-superres stage 3: a real aomenc `--superres-mode=1` key-frame | ||
| 3814 | + /// sequence must decode pixel-exact vs ffmpeg/dav1d's own upscaled | ||
| 3815 | + /// output, and must actually run [`crate::superres::upscale_picture`] | ||
| 3816 | + /// (`superres_hits > 0`) -- a decoder that silently stopped upscaling | ||
| 3817 | + /// would otherwise pass vacuously (class `gate-blind-to-feature`). | ||
| 3818 | + /// `--kf-max-dist` set to the frame count keeps every frame a key | ||
| 3819 | + /// frame: inter-frame superres (a differently-scaled reference) is a | ||
| 3820 | + /// later stage of this lane, refused by name today. | ||
| 3821 | + #[test] | ||
| 3822 | + fn a_real_aomenc_superres_key_frame_sequence_decodes_pixel_exact() { | ||
| 3823 | + const NAME: &str = "a_real_aomenc_superres_key_frame_sequence_decodes_pixel_exact"; | ||
| 3824 | + let _gate_lock = lock_gate_counters(); | ||
| 3825 | + if !have_ffmpeg() { | ||
| 3826 | + eprintln!("SKIP {NAME}: no ffmpeg"); | ||
| 3827 | + return; | ||
| 3828 | + } | ||
| 3829 | + if !have_aomenc() { | ||
| 3830 | + eprintln!("SKIP {NAME}: no aomenc at {}", aomenc_path().display()); | ||
| 3831 | + return; | ||
| 3832 | + } | ||
| 3833 | + let (width, height, frame_count) = (64usize, 64usize, 3usize); | ||
| 3834 | + let duration = frame_count as f64 / 25.0; | ||
| 3835 | + let source = gradients_source(7, width, height, &format!("duration={duration}:rate=25")); | ||
| 3836 | + let y4m = Command::new("ffmpeg") | ||
| 3837 | + .args([ | ||
| 3838 | + "-v", | ||
| 3839 | + "error", | ||
| 3840 | + "-f", | ||
| 3841 | + "lavfi", | ||
| 3842 | + "-i", | ||
| 3843 | + &source, | ||
| 3844 | + "-pix_fmt", | ||
| 3845 | + "yuv420p", | ||
| 3846 | + "-f", | ||
| 3847 | + "yuv4mpegpipe", | ||
| 3848 | + "-", | ||
| 3849 | + ]) | ||
| 3850 | + .stdin(Stdio::null()) | ||
| 3851 | + .stdout(Stdio::piped()) | ||
| 3852 | + .stderr(Stdio::piped()) | ||
| 3853 | + .output() | ||
| 3854 | + .expect("ffmpeg failed to run"); | ||
| 3855 | + assert!( | ||
| 3856 | + y4m.status.success(), | ||
| 3857 | + "ffmpeg fixture: {}", | ||
| 3858 | + String::from_utf8_lossy(&y4m.stderr) | ||
| 3859 | + ); | ||
| 3860 | + let args: Vec<&str> = vec![ | ||
| 3861 | + "--codec=av1", | ||
| 3862 | + "--passes=1", | ||
| 3863 | + "--end-usage=q", | ||
| 3864 | + "--cq-level=32", | ||
| 3865 | + "--cpu-used=0", | ||
| 3866 | + "--kf-max-dist=0", | ||
| 3867 | + "--threads=1", | ||
| 3868 | + "--row-mt=0", | ||
| 3869 | + "--superres-mode=1", | ||
| 3870 | + "--superres-denominator=12", | ||
| 3871 | + "--obu", | ||
| 3872 | + "-o", | ||
| 3873 | + "-", | ||
| 3874 | + "-", | ||
| 3875 | + ]; | ||
| 3876 | + let mut child = Command::new(aomenc_path()) | ||
| 3877 | + .args(&args) | ||
| 3878 | + .stdin(Stdio::piped()) | ||
| 3879 | + .stdout(Stdio::piped()) | ||
| 3880 | + .stderr(Stdio::piped()) | ||
| 3881 | + .spawn() | ||
| 3882 | + .expect("aomenc failed to start"); | ||
| 3883 | + child | ||
| 3884 | + .stdin | ||
| 3885 | + .take() | ||
| 3886 | + .expect("aomenc stdin") | ||
| 3887 | + .write_all(&y4m.stdout) | ||
| 3888 | + .expect("writing y4m to aomenc"); | ||
| 3889 | + let out = child.wait_with_output().expect("aomenc failed to run"); | ||
| 3890 | + assert!( | ||
| 3891 | + out.status.success(), | ||
| 3892 | + "aomenc refused the fixture: {}", | ||
| 3893 | + String::from_utf8_lossy(&out.stderr) | ||
| 3894 | + ); | ||
| 3895 | + let stream = out.stdout; | ||
| 3896 | + let frames = | ||
| 3897 | + decode_stream(&stream).unwrap_or_else(|e| panic!("{NAME} refused: {e}")); | ||
| 3898 | + assert_eq!(frames.len(), frame_count); | ||
| 3899 | + let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, frame_count); | ||
| 3900 | + for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { | ||
| 3901 | + assert_eq!(got.y, want.y, "{NAME} frame {i} luma vs ffmpeg"); | ||
| 3902 | + assert_eq!(got.u, want.u, "{NAME} frame {i} U vs ffmpeg"); | ||
| 3903 | + assert_eq!(got.v, want.v, "{NAME} frame {i} V vs ffmpeg"); | ||
| 3904 | + } | ||
| 3905 | + assert!( | ||
| 3906 | + crate::superres::superres_hits() > 0, | ||
| 3907 | + "{NAME}: matched but zero superres_hits -- the gate never actually exercised the \ | ||
| 3908 | + upscaler, it proves nothing" | ||
| 3909 | + ); | ||
| 3910 | + eprintln!( | ||
| 3911 | + "{NAME}: {frame_count} frames pixel-exact, superres_hits={}", | ||
| 3912 | + crate::superres::superres_hits() | ||
| 3913 | + ); | ||
| 3914 | + } | ||
| 3915 | + | ||
| 3799 | /// lane-interintra r1: `--enable-interintra-comp=1` streams must DECODE | 3916 | /// lane-interintra r1: `--enable-interintra-comp=1` streams must DECODE |
| 3800 | /// pixel-exact -- the interintra flag/mode/wedge-flag syntax and the | 3917 | /// pixel-exact -- the interintra flag/mode/wedge-flag syntax and the |
| 3801 | /// non-wedge smooth blend are ported. A refusal naming interintra fails | 3918 | /// non-wedge smooth blend are ported. A refusal naming interintra fails |
crates/ec-av1/src/superres.rsmodified+30 -0
| @@ -142,6 +142,36 @@ pub(crate) fn upscale_plane(rows: &[u8], height: usize, in_width: usize, out_wid | |||
| 142 | out | 142 | out |
| 143 | } | 143 | } |
| 144 | 144 | ||
| 145 | +thread_local! { | ||
| 146 | + /// Firing count for the superres gate (class `gate-blind-to-feature`): | ||
| 147 | + /// how many whole pictures actually ran through [`upscale_plane`]. | ||
| 148 | + static SUPERRES_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; | ||
| 149 | +} | ||
| 150 | + | ||
| 151 | +/// Current value of [`SUPERRES_HITS`]. | ||
| 152 | +pub(crate) fn superres_hits() -> usize { | ||
| 153 | + SUPERRES_HITS.with(|c| c.get()) | ||
| 154 | +} | ||
| 155 | + | ||
| 156 | +/// A whole [`crate::encode::Picture`]'s Y/U/V planes, upscaled from | ||
| 157 | +/// `frame_width`/`frame_height` (4:2:0 chroma) to `upscaled_width` at the | ||
| 158 | +/// same height -- spec 7.16 is horizontal-only. Increments | ||
| 159 | +/// [`SUPERRES_HITS`] once per call. | ||
| 160 | +pub(crate) fn upscale_picture(picture: &crate::encode::Picture, upscaled_width: usize) -> crate::encode::Picture { | ||
| 161 | + SUPERRES_HITS.with(|c| c.set(c.get() + 1)); | ||
| 162 | + let height = picture.height; | ||
| 163 | + let chroma_in_w = picture.width.div_ceil(2); | ||
| 164 | + let chroma_out_w = upscaled_width.div_ceil(2); | ||
| 165 | + let chroma_h = height.div_ceil(2); | ||
| 166 | + crate::encode::Picture { | ||
| 167 | + width: upscaled_width, | ||
| 168 | + height, | ||
| 169 | + y: upscale_plane(&picture.y, height, picture.width, upscaled_width), | ||
| 170 | + u: upscale_plane(&picture.u, chroma_h, chroma_in_w, chroma_out_w), | ||
| 171 | + v: upscale_plane(&picture.v, chroma_h, chroma_in_w, chroma_out_w), | ||
| 172 | + } | ||
| 173 | +} | ||
| 174 | + | ||
| 145 | #[cfg(test)] | 175 | #[cfg(test)] |
| 146 | mod tests { | 176 | mod tests { |
| 147 | use super::*; | 177 | use super::*; |