Lane-palette2 r6 -- gate the broad rect-screen-content case
Browse files at this commit083ca11adaac22fb56b175442dac7d5f5f773cfbParent: 09120e8- test
- lane-palette2 r6 -- gate the broad rect-screen-content case
- RED, root cause identified
2 changed files +176 -0
- crates/ec-av1/src/decode.rs +23 -0
- crates/ec-av1/src/stream.rs +153 -0
crates/ec-av1/src/decode.rsmodified+23 -0
| @@ -562,6 +562,25 @@ pub(crate) fn palette_rect_hits() -> usize { | |||
| 562 | PALETTE_RECT_HITS.with(|c| c.get()) | 562 | PALETTE_RECT_HITS.with(|c| c.get()) |
| 563 | } | 563 | } |
| 564 | 564 | ||
| 565 | +// lane-palette2 r6: how many HORZ/VERT rect intra strips ran the | ||
| 566 | +// [`palette_bsize_ctx_wh`]-gated code path in [`read_intra_mode_rect`] -- | ||
| 567 | +// i.e. `allow_screen_content_tools` was set and the strip's `bw`x`bh` was in | ||
| 568 | +// range, so `palette_y_mode`/`palette_uv_mode` were actually read (and, per | ||
| 569 | +// RD, almost always refused-on-use because the block doesn't end up USING a | ||
| 570 | +// palette -- see r5's report). This is the site the removed refusal | ||
| 571 | +// ("a HORZ/VERT intra strip in a screen-content frame") used to guard: it | ||
| 572 | +// fired here whether or not the block picked a palette, so this is the | ||
| 573 | +// counter that proves the case r5's narrower [`PALETTE_RECT_HITS`] almost | ||
| 574 | +// never observes. | ||
| 575 | +thread_local! { | ||
| 576 | + static RECT_SCREEN_CONTENT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; | ||
| 577 | +} | ||
| 578 | + | ||
| 579 | +/// Current value of [`RECT_SCREEN_CONTENT_HITS`]. | ||
| 580 | +pub(crate) fn rect_screen_content_hits() -> usize { | ||
| 581 | + RECT_SCREEN_CONTENT_HITS.with(|c| c.get()) | ||
| 582 | +} | ||
| 583 | + | ||
| 565 | // lane-palette2 r1: how many palette-Y blocks decoded with a split luma | 584 | // lane-palette2 r1: how many palette-Y blocks decoded with a split luma |
| 566 | // transform (`tx_select && logical_tx != side`) -- proves the per-TU index-map | 585 | // transform (`tx_select && logical_tx != side`) -- proves the per-TU index-map |
| 567 | // slicing actually ran, not just that the whole-block path stayed exact. | 586 | // slicing actually ran, not just that the whole-block path stayed exact. |
| @@ -3088,6 +3107,10 @@ fn read_intra_mode_rect( | |||
| 3088 | if allow_screen_content_tools | 3107 | if allow_screen_content_tools |
| 3089 | && let Some(bsize_ctx) = palette_bsize_ctx_wh(bw, bh) | 3108 | && let Some(bsize_ctx) = palette_bsize_ctx_wh(bw, bh) |
| 3090 | { | 3109 | { |
| 3110 | + // r6: this is the exact site the removed blanket refusal used to | ||
| 3111 | + // guard -- reached for EVERY rect strip in a screen-content frame, | ||
| 3112 | + // independent of whether the block ends up using a palette. | ||
| 3113 | + RECT_SCREEN_CONTENT_HITS.with(|c| c.set(c.get() + 1)); | ||
| 3091 | let (mode_ctx, cache) = palette.unwrap_or((0, &[])); | 3114 | let (mode_ctx, cache) = palette.unwrap_or((0, &[])); |
| 3092 | let use_palette_y = | 3115 | let use_palette_y = |
| 3093 | mode == DC_PRED && dec.symbol(&mut cdfs.palette_y_mode[bsize_ctx][mode_ctx]) != 0; | 3116 | mode == DC_PRED && dec.symbol(&mut cdfs.palette_y_mode[bsize_ctx][mode_ctx]) != 0; |
crates/ec-av1/src/stream.rsmodified+153 -0
| @@ -2216,6 +2216,159 @@ mod tests { | |||
| 2216 | ); | 2216 | ); |
| 2217 | } | 2217 | } |
| 2218 | 2218 | ||
| 2219 | + /// The BROAD case (lane-palette2 r6, per its charter): a HORZ/VERT intra | ||
| 2220 | + /// strip in a screen-content-tools frame that does NOT necessarily use a | ||
| 2221 | + /// palette. This is what the refusal r4 removed actually guarded -- | ||
| 2222 | + /// [`a_real_aomenc_stream_with_rect_palette_decodes_pixel_exact`] above | ||
| 2223 | + /// gates the rarer "and picks a palette" case, which r5 measured RD | ||
| 2224 | + /// almost never chooses -- 39/70 of that same sweep DID land here (a | ||
| 2225 | + /// clean rect-strip decode with `palette_rect_hits` unchanged), just | ||
| 2226 | + /// uncounted by r5's narrower assertion. Same `--min/max-partition-size=64` | ||
| 2227 | + /// recipe r5 measured live to dodge the 32x32-level AB-partition quirk | ||
| 2228 | + /// ([[lane-sbpart-r2]]), single key frame, size/cq sweep. HARD-asserts | ||
| 2229 | + /// [`decode::rect_screen_content_hits`] moved -- proof this stream | ||
| 2230 | + /// actually ran [`read_intra_mode_rect`]'s screen-content-tools branch -- | ||
| 2231 | + /// before comparing pixels. | ||
| 2232 | + #[test] | ||
| 2233 | + fn a_real_aomenc_stream_with_rect_screen_content_decodes_pixel_exact() { | ||
| 2234 | + const NAME: &str = "a_real_aomenc_stream_with_rect_screen_content_decodes_pixel_exact"; | ||
| 2235 | + let _gate_lock = lock_gate_counters(); | ||
| 2236 | + if !have_ffmpeg() { | ||
| 2237 | + eprintln!("SKIP {NAME}: no ffmpeg"); | ||
| 2238 | + return; | ||
| 2239 | + } | ||
| 2240 | + if !have_aomenc() { | ||
| 2241 | + eprintln!("SKIP {NAME}: no aomenc at {}", aomenc_path().display()); | ||
| 2242 | + return; | ||
| 2243 | + } | ||
| 2244 | + let mut matched = 0u32; | ||
| 2245 | + let mut named_refusals = 0u32; | ||
| 2246 | + let mut attempts = 0u32; | ||
| 2247 | + let sizes = [(64, 128), (128, 64), (128, 128), (128, 192), (192, 192)]; | ||
| 2248 | + let cqs = [5u32, 15, 25, 35, 45, 55, 63]; | ||
| 2249 | + for src_name in ["smptebars", "rgbtestsrc"] { | ||
| 2250 | + for (width, height) in sizes { | ||
| 2251 | + for cq in cqs { | ||
| 2252 | + attempts += 1; | ||
| 2253 | + let source = format!("{src_name}=size={width}x{height}:rate=25"); | ||
| 2254 | + let y4m = Command::new("ffmpeg") | ||
| 2255 | + .args([ | ||
| 2256 | + "-v", "error", "-f", "lavfi", "-i", &source, "-pix_fmt", "yuv420p", | ||
| 2257 | + "-t", "0.04", "-f", "yuv4mpegpipe", "-", | ||
| 2258 | + ]) | ||
| 2259 | + .stdin(Stdio::null()) | ||
| 2260 | + .stdout(Stdio::piped()) | ||
| 2261 | + .stderr(Stdio::piped()) | ||
| 2262 | + .output() | ||
| 2263 | + .expect("ffmpeg failed to run"); | ||
| 2264 | + assert!( | ||
| 2265 | + y4m.status.success(), | ||
| 2266 | + "ffmpeg fixture: {}", | ||
| 2267 | + String::from_utf8_lossy(&y4m.stderr) | ||
| 2268 | + ); | ||
| 2269 | + let cq_arg = format!("--cq-level={cq}"); | ||
| 2270 | + let mut child = Command::new(aomenc_path()) | ||
| 2271 | + .args([ | ||
| 2272 | + "--codec=av1", | ||
| 2273 | + "--passes=1", | ||
| 2274 | + "--end-usage=q", | ||
| 2275 | + &cq_arg, | ||
| 2276 | + "--cpu-used=4", | ||
| 2277 | + "--threads=1", | ||
| 2278 | + "--row-mt=0", | ||
| 2279 | + "--sb-size=64", | ||
| 2280 | + "--enable-palette=1", | ||
| 2281 | + "--min-partition-size=64", | ||
| 2282 | + "--max-partition-size=64", | ||
| 2283 | + "--obu", | ||
| 2284 | + "-o", | ||
| 2285 | + "-", | ||
| 2286 | + "-", | ||
| 2287 | + ]) | ||
| 2288 | + .stdin(Stdio::piped()) | ||
| 2289 | + .stdout(Stdio::piped()) | ||
| 2290 | + .stderr(Stdio::piped()) | ||
| 2291 | + .spawn() | ||
| 2292 | + .expect("aomenc failed to start"); | ||
| 2293 | + child | ||
| 2294 | + .stdin | ||
| 2295 | + .take() | ||
| 2296 | + .expect("aomenc stdin") | ||
| 2297 | + .write_all(&y4m.stdout) | ||
| 2298 | + .expect("writing y4m to aomenc"); | ||
| 2299 | + let out = child.wait_with_output().expect("aomenc failed to run"); | ||
| 2300 | + assert!( | ||
| 2301 | + out.status.success(), | ||
| 2302 | + "aomenc refused the fixture ({source} cq={cq}): {}", | ||
| 2303 | + String::from_utf8_lossy(&out.stderr) | ||
| 2304 | + ); | ||
| 2305 | + let stream = out.stdout; | ||
| 2306 | + let before = crate::decode::rect_screen_content_hits(); | ||
| 2307 | + let frames = match decode_stream(&stream) { | ||
| 2308 | + Err(e) => { | ||
| 2309 | + let msg = e.to_string(); | ||
| 2310 | + assert!( | ||
| 2311 | + msg.contains("unsupported"), | ||
| 2312 | + "{NAME} failed outright, not a named refusal \ | ||
| 2313 | + ({source} cq={cq}): {msg}" | ||
| 2314 | + ); | ||
| 2315 | + named_refusals += 1; | ||
| 2316 | + eprintln!("{source} cq={cq}: refusal: {msg}"); | ||
| 2317 | + continue; | ||
| 2318 | + } | ||
| 2319 | + Ok(frames) => frames, | ||
| 2320 | + }; | ||
| 2321 | + let after = crate::decode::rect_screen_content_hits(); | ||
| 2322 | + if after == before { | ||
| 2323 | + eprintln!( | ||
| 2324 | + "{NAME}: {source} cq={cq} decoded without ever running the rect \ | ||
| 2325 | + screen-content path (rect_screen_content_hits unchanged) -- not \ | ||
| 2326 | + counted" | ||
| 2327 | + ); | ||
| 2328 | + continue; | ||
| 2329 | + } | ||
| 2330 | + let frame_count = frames.len(); | ||
| 2331 | + let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, frame_count); | ||
| 2332 | + if frames.iter().zip(&ffmpeg_frames).any(|(got, want)| { | ||
| 2333 | + got.y != want.y || got.u != want.u || got.v != want.v | ||
| 2334 | + }) && let Ok(path) = std::env::var("EC_AV1_GATE_DUMP") | ||
| 2335 | + { | ||
| 2336 | + std::fs::write(&path, &stream).expect("writing pinned stream"); | ||
| 2337 | + eprintln!( | ||
| 2338 | + "EC_AV1_GATE_DUMP: wrote mismatching stream ({source} cq={cq}) to {path}" | ||
| 2339 | + ); | ||
| 2340 | + } | ||
| 2341 | + for i in 0..frame_count { | ||
| 2342 | + assert_eq!( | ||
| 2343 | + frames[i].y, ffmpeg_frames[i].y, | ||
| 2344 | + "{NAME}: {source} cq={cq} frame {i} luma vs ffmpeg" | ||
| 2345 | + ); | ||
| 2346 | + assert_eq!( | ||
| 2347 | + frames[i].u, ffmpeg_frames[i].u, | ||
| 2348 | + "{NAME}: {source} cq={cq} frame {i} U vs ffmpeg" | ||
| 2349 | + ); | ||
| 2350 | + assert_eq!( | ||
| 2351 | + frames[i].v, ffmpeg_frames[i].v, | ||
| 2352 | + "{NAME}: {source} cq={cq} frame {i} V vs ffmpeg" | ||
| 2353 | + ); | ||
| 2354 | + } | ||
| 2355 | + matched += 1; | ||
| 2356 | + } | ||
| 2357 | + } | ||
| 2358 | + } | ||
| 2359 | + assert!( | ||
| 2360 | + matched > 0, | ||
| 2361 | + "{NAME}: no attempt out of {attempts} ever landed a matching rect \ | ||
| 2362 | + screen-content block ({named_refusals} named refusals) -- \ | ||
| 2363 | + rect_screen_content_hits never proven pixel-exact" | ||
| 2364 | + ); | ||
| 2365 | + eprintln!( | ||
| 2366 | + "{NAME}: {matched}/{attempts} attempts matched pixel-exact, {named_refusals} \ | ||
| 2367 | + named refusals, rect_screen_content_hits={}", | ||
| 2368 | + crate::decode::rect_screen_content_hits() | ||
| 2369 | + ); | ||
| 2370 | + } | ||
| 2371 | + | ||
| 2219 | /// As [`a_real_aomenc_filter_intra_stream_decodes_pixel_exact`]'s | 2372 | /// As [`a_real_aomenc_filter_intra_stream_decodes_pixel_exact`]'s |
| 2220 | /// harness, but with the deblocking filter left *on* (no | 2373 | /// harness, but with the deblocking filter left *on* (no |
| 2221 | /// `--loopfilter-control=0`) at a crf high enough that libaom actually | 2374 | /// `--loopfilter-control=0`) at a crf high enough that libaom actually |