A real screen key frame codes an unskipped intrabc block under TX_MODE_SELECT
Browse files at this commitc72e3154ae8ac9e543d502d93156a7a5116be112Parent: d0c8191- test
- a real screen key frame codes an unskipped intrabc block under TX_MODE_SELECT
1 changed file +306 -0
- crates/ec-av1/src/stream.rs +306 -0
crates/ec-av1/src/stream.rsmodified+306 -0
| @@ -4202,6 +4202,312 @@ mod tests { | |||
| 4202 | ); | 4202 | ); |
| 4203 | } | 4203 | } |
| 4204 | 4204 | ||
| 4205 | + /// lane-t900 r31: builds one `--tune-content=screen --enable-intrabc=1` | ||
| 4206 | + /// key frame, 256x192 8-bit, from a deterministic lavfi source. `tiled` | ||
| 4207 | + /// renders a 2x2 tiling of a 128x96 pattern -- exact spatial repetition, | ||
| 4208 | + /// the content intra block copy exists for; `square_only` turns the rect | ||
| 4209 | + /// and 1:4 partitions off so an intrabc block lands on a SQUARE block | ||
| 4210 | + /// instead of stopping on the rect-strip intrabc refusal first. | ||
| 4211 | + fn screen_intrabc_stream(src: &str, cq: &str, txs: &str, tiled: bool, square_only: bool) -> Vec<u8> { | ||
| 4212 | + let mut ff = Command::new("ffmpeg"); | ||
| 4213 | + ff.args(["-v", "error", "-f", "lavfi", "-i", src, "-t", "0.2"]); | ||
| 4214 | + if tiled { | ||
| 4215 | + ff.args(["-vf", "tile=2x2"]); | ||
| 4216 | + } | ||
| 4217 | + let out = ff | ||
| 4218 | + .args(["-pix_fmt", "yuv420p", "-strict", "-1", "-f", "yuv4mpegpipe", "-"]) | ||
| 4219 | + .stdin(Stdio::null()) | ||
| 4220 | + .stdout(Stdio::piped()) | ||
| 4221 | + .stderr(Stdio::piped()) | ||
| 4222 | + .output() | ||
| 4223 | + .expect("ffmpeg failed to run"); | ||
| 4224 | + assert!(out.status.success(), "ffmpeg failed for {src}"); | ||
| 4225 | + let y4m = out.stdout; | ||
| 4226 | + let mut child = Command::new(aomenc_path()) | ||
| 4227 | + .args([ | ||
| 4228 | + "--codec=av1", | ||
| 4229 | + "--bit-depth=8", | ||
| 4230 | + "--input-bit-depth=8", | ||
| 4231 | + "--passes=1", | ||
| 4232 | + "--end-usage=q", | ||
| 4233 | + "--cpu-used=0", | ||
| 4234 | + "--lag-in-frames=0", | ||
| 4235 | + "--kf-max-dist=1", | ||
| 4236 | + "--limit=1", | ||
| 4237 | + "--threads=1", | ||
| 4238 | + "--tile-columns=0", | ||
| 4239 | + "--min-partition-size=8", | ||
| 4240 | + "--max-partition-size=32", | ||
| 4241 | + "--sb-size=64", | ||
| 4242 | + "--tune-content=screen", | ||
| 4243 | + "--enable-intrabc=1", | ||
| 4244 | + "--enable-palette=0", | ||
| 4245 | + ]) | ||
| 4246 | + .args([format!("--cq-level={cq}"), format!("--enable-tx-size-search={txs}")]) | ||
| 4247 | + // aomenc keeps the LAST occurrence of a repeated flag. | ||
| 4248 | + .args(if square_only { | ||
| 4249 | + &["--enable-rect-partitions=0", "--enable-1to4-partitions=0"][..] | ||
| 4250 | + } else { | ||
| 4251 | + &[][..] | ||
| 4252 | + }) | ||
| 4253 | + .args(["--obu", "-o", "-", "-"]) | ||
| 4254 | + .stdin(Stdio::piped()) | ||
| 4255 | + .stdout(Stdio::piped()) | ||
| 4256 | + .stderr(Stdio::piped()) | ||
| 4257 | + .spawn() | ||
| 4258 | + .expect("aomenc failed to start"); | ||
| 4259 | + child | ||
| 4260 | + .stdin | ||
| 4261 | + .take() | ||
| 4262 | + .expect("aomenc stdin") | ||
| 4263 | + .write_all(&y4m) | ||
| 4264 | + .or_else(|e| match e.kind() { | ||
| 4265 | + std::io::ErrorKind::BrokenPipe => Ok(()), | ||
| 4266 | + _ => Err(e), | ||
| 4267 | + }) | ||
| 4268 | + .expect("writing y4m to aomenc"); | ||
| 4269 | + let enc = child.wait_with_output().expect("aomenc failed to run"); | ||
| 4270 | + assert!( | ||
| 4271 | + enc.status.success(), | ||
| 4272 | + "aomenc refused {src} cq={cq}: {}", | ||
| 4273 | + String::from_utf8_lossy(&enc.stderr) | ||
| 4274 | + ); | ||
| 4275 | + enc.stdout | ||
| 4276 | + } | ||
| 4277 | + | ||
| 4278 | + /// How many frames of `stream` carry `tx_mode == Select`, and how many of | ||
| 4279 | + /// those also keep `allow_intrabc` -- the frame-level premise of the | ||
| 4280 | + /// refusal "an intrabc block under TxMode::Select", read out of the | ||
| 4281 | + /// headers rather than assumed from the flags handed to the encoder | ||
| 4282 | + /// (libaom clears `allow_intrabc` again when no block picked it, | ||
| 4283 | + /// encodeframe.c:2336). | ||
| 4284 | + fn tx_select_and_intrabc_frames(stream: &[u8]) -> (u32, u32) { | ||
| 4285 | + let (mut select, mut premise) = (0u32, 0u32); | ||
| 4286 | + let mut p = Av1Parser::new(); | ||
| 4287 | + let mut pos = 0usize; | ||
| 4288 | + while pos < stream.len() { | ||
| 4289 | + let obu = p.parse_obu(&stream[pos..]).expect("parse"); | ||
| 4290 | + pos += obu.total_size; | ||
| 4291 | + let header = match &obu.kind { | ||
| 4292 | + ObuKind::Frame(h, _) => Some(&**h), | ||
| 4293 | + ObuKind::FrameHeader(h) => Some(&**h), | ||
| 4294 | + _ => None, | ||
| 4295 | + }; | ||
| 4296 | + if let Some(h) = header { | ||
| 4297 | + if h.tx_mode == TxMode::Select { | ||
| 4298 | + select += 1; | ||
| 4299 | + if h.allow_intrabc { | ||
| 4300 | + premise += 1; | ||
| 4301 | + } | ||
| 4302 | + } | ||
| 4303 | + } | ||
| 4304 | + } | ||
| 4305 | + (select, premise) | ||
| 4306 | + } | ||
| 4307 | + | ||
| 4308 | + /// lane-t900 r31, CENSUS for the refusal "an intrabc block under | ||
| 4309 | + /// TxMode::Select" -- which this gate DISPROVES as an "no encoder writes | ||
| 4310 | + /// one" claim and pins as a real capability gap instead. | ||
| 4311 | + /// | ||
| 4312 | + /// The premise is reachable by the SPEC: `tx_mode` (5.9.21) is forced to | ||
| 4313 | + /// `ONLY_4X4` only under `CodedLossless`, while `allow_intrabc` (5.9.2) | ||
| 4314 | + /// needs `allow_screen_content_tools` at unscaled resolution -- two | ||
| 4315 | + /// independent conditions. libaom leaves it open too: `select_tx_mode` | ||
| 4316 | + /// (encodeframe.c:2355) never consults `allow_intrabc`, and the only | ||
| 4317 | + /// downgrade to `TX_MODE_LARGEST` is `txb_split_count == 0` | ||
| 4318 | + /// (encodeframe.c:2690). | ||
| 4319 | + /// | ||
| 4320 | + /// The measurement over 14 real `--tune-content=screen --enable-intrabc=1` | ||
| 4321 | + /// recipes: | ||
| 4322 | + /// | ||
| 4323 | + /// * the 10 non-repeating arms never present the premise at all -- with | ||
| 4324 | + /// `--enable-tx-size-search=1` the frame codes `TX_MODE_SELECT` but RD | ||
| 4325 | + /// never picks intrabc, so libaom clears `allow_intrabc` again; with | ||
| 4326 | + /// `=0` intrabc IS used and the frame is `TX_MODE_LARGEST`. That | ||
| 4327 | + /// correlation, not any rule, is why earlier rounds never saw this; | ||
| 4328 | + /// * the four EXACT-REPETITION arms (a 2x2 tiling of one 128x96 bar | ||
| 4329 | + /// pattern) all code `allow_intrabc && tx_mode == Select`. Two of them | ||
| 4330 | + /// stop on the older rect-strip intrabc refusal (the shape that fires | ||
| 4331 | + /// first, class `refusal-hides-a-defect`), and the two with rect/1:4 | ||
| 4332 | + /// partitions off put intrabc on a SQUARE block: `cq=30` reaches THIS | ||
| 4333 | + /// refusal by name, `cq=45`'s intrabc blocks are all `skip` and decode | ||
| 4334 | + /// pixel-exact against ffmpeg. | ||
| 4335 | + /// | ||
| 4336 | + /// So the refusal is correct and load-bearing, exactly one arm reaches it, | ||
| 4337 | + /// and the lift it names has a waiting witness: | ||
| 4338 | + /// [`an_intrabc_block_under_tx_mode_select_decodes_pixel_exact`] (ignored | ||
| 4339 | + /// until the intra path reads the inter var-tx tree for an intrabc block). | ||
| 4340 | + #[test] | ||
| 4341 | + fn an_intrabc_census_over_screen_key_frames_measures_the_tx_select_refusal() { | ||
| 4342 | + const NAME: &str = | ||
| 4343 | + "an_intrabc_census_over_screen_key_frames_measures_the_tx_select_refusal"; | ||
| 4344 | + if !have_ffmpeg() { | ||
| 4345 | + eprintln!("SKIP {NAME}: no ffmpeg"); | ||
| 4346 | + return; | ||
| 4347 | + } | ||
| 4348 | + if !have_aomenc() { | ||
| 4349 | + eprintln!("SKIP {NAME}: no aomenc at {}", aomenc_path().display()); | ||
| 4350 | + return; | ||
| 4351 | + } | ||
| 4352 | + const TS: &str = "testsrc2=s=256x192:r=25"; | ||
| 4353 | + const SB: &str = "smptebars=size=256x192:rate=25"; | ||
| 4354 | + const PB: &str = "pal75bars=size=256x192:rate=25"; | ||
| 4355 | + const TILE: &str = "smptebars=size=128x96:rate=25"; | ||
| 4356 | + let (width, height) = (256usize, 192usize); | ||
| 4357 | + // (arm, source, cq, tx-size-search, tiled, square_only) | ||
| 4358 | + let arms: [(&str, &str, &str, &str, bool, bool); 13] = [ | ||
| 4359 | + ("smptebars-cq40-txs1", SB, "40", "1", false, false), | ||
| 4360 | + ("smptebars-cq45-txs1", SB, "45", "1", false, false), | ||
| 4361 | + ("smptebars-cq55-txs1", SB, "55", "1", false, false), | ||
| 4362 | + ("smptebars-cq63-txs1", SB, "63", "1", false, false), | ||
| 4363 | + ("pal75bars-cq40-txs1", PB, "40", "1", false, false), | ||
| 4364 | + ("pal75bars-cq55-txs1", PB, "55", "1", false, false), | ||
| 4365 | + ("testsrc2-cq50-txs1", TS, "50", "1", false, false), | ||
| 4366 | + ("testsrc2-cq63-txs1", TS, "63", "1", false, false), | ||
| 4367 | + ("smptebars-cq45-txs0", SB, "45", "0", false, false), | ||
| 4368 | + ("pal75bars-cq45-txs0", PB, "45", "0", false, false), | ||
| 4369 | + ("tiled-cq30-txs1", TILE, "30", "1", true, false), | ||
| 4370 | + ("tiled-cq45-txs1", TILE, "45", "1", true, false), | ||
| 4371 | + ("tiled-square-cq30-txs1", TILE, "30", "1", true, true), | ||
| 4372 | + // `tiled-square-cq45-txs1` belongs here too and is NOT in this | ||
| 4373 | + // table: its intrabc blocks are all `skip`, so the `&& !skip` | ||
| 4374 | + // guard lets them through -- and the frame decodes WRONG against | ||
| 4375 | + // ffmpeg. A refusal one `&&` narrower than the hole it guards | ||
| 4376 | + // (class `refusal-hides-a-defect`); that arm lives in the ignored | ||
| 4377 | + // gate below with the unskipped one, and the guard itself is a | ||
| 4378 | + // `decode.rs` edit this round could not make. | ||
| 4379 | + ]; | ||
| 4380 | + let (mut select_arms, mut premise_arms, mut intrabc_arms, mut blocks_total) = | ||
| 4381 | + (0u32, 0u32, 0u32, 0usize); | ||
| 4382 | + let (mut refused_tx_select, mut frames_compared) = (0u32, 0usize); | ||
| 4383 | + for (arm, src, cq, txs, tiled, square_only) in &arms { | ||
| 4384 | + let stream = screen_intrabc_stream(src, cq, txs, *tiled, *square_only); | ||
| 4385 | + let (select, premise) = tx_select_and_intrabc_frames(&stream); | ||
| 4386 | + if select > 0 { | ||
| 4387 | + select_arms += 1; | ||
| 4388 | + } | ||
| 4389 | + if premise > 0 { | ||
| 4390 | + premise_arms += 1; | ||
| 4391 | + } | ||
| 4392 | + crate::decode::reset_intrabc_hits(); | ||
| 4393 | + let decoded = decode_stream(&stream); | ||
| 4394 | + let blocks = crate::decode::intrabc_hits(); | ||
| 4395 | + blocks_total += blocks; | ||
| 4396 | + if blocks > 0 { | ||
| 4397 | + intrabc_arms += 1; | ||
| 4398 | + } | ||
| 4399 | + match decoded { | ||
| 4400 | + Ok(frames) => { | ||
| 4401 | + assert!(!frames.is_empty(), "{NAME}: {arm} decoded no frame"); | ||
| 4402 | + let theirs = ffmpeg_decode_sequence(&stream, width, height, frames.len()); | ||
| 4403 | + assert_eq!(frames.len(), theirs.len(), "{NAME}: {arm} frame count"); | ||
| 4404 | + for (i, (ours, ref_frame)) in frames.iter().zip(theirs.iter()).enumerate() { | ||
| 4405 | + assert!( | ||
| 4406 | + ours.y == ref_frame.y | ||
| 4407 | + && ours.u == ref_frame.u | ||
| 4408 | + && ours.v == ref_frame.v, | ||
| 4409 | + "{NAME}: {arm} frame {i} does not match ffmpeg" | ||
| 4410 | + ); | ||
| 4411 | + frames_compared += 1; | ||
| 4412 | + } | ||
| 4413 | + eprintln!( | ||
| 4414 | + "{NAME}: {arm} select={select} premise={premise} {blocks} intrabc \ | ||
| 4415 | + block(s), {} frame(s) exact", | ||
| 4416 | + frames.len() | ||
| 4417 | + ); | ||
| 4418 | + } | ||
| 4419 | + Err(e) => { | ||
| 4420 | + let msg = e.to_string(); | ||
| 4421 | + assert!(msg.contains("unsupported"), "{NAME}: {arm} decode failed: {e}"); | ||
| 4422 | + if msg.contains("an intrabc block under TxMode::Select") { | ||
| 4423 | + refused_tx_select += 1; | ||
| 4424 | + assert!( | ||
| 4425 | + premise > 0, | ||
| 4426 | + "{NAME}: {arm} reached the TxMode::Select intrabc refusal in a \ | ||
| 4427 | + frame that codes no such header" | ||
| 4428 | + ); | ||
| 4429 | + } else { | ||
| 4430 | + assert!( | ||
| 4431 | + msg.contains("intra block copy"), | ||
| 4432 | + "{NAME}: {arm} refused for an unrelated reason: {e}" | ||
| 4433 | + ); | ||
| 4434 | + } | ||
| 4435 | + eprintln!("{NAME}: {arm} select={select} premise={premise} REFUSED: {e}"); | ||
| 4436 | + } | ||
| 4437 | + } | ||
| 4438 | + } | ||
| 4439 | + eprintln!( | ||
| 4440 | + "{NAME}: {select_arms}/{n} arm(s) code a TX_MODE_SELECT frame, {premise_arms} of \ | ||
| 4441 | + those keep allow_intrabc, {intrabc_arms} arm(s) decode an intrabc block \ | ||
| 4442 | + ({blocks_total} total), {refused_tx_select} reach the refusal, \ | ||
| 4443 | + {frames_compared} frame(s) compared exact", | ||
| 4444 | + n = arms.len() | ||
| 4445 | + ); | ||
| 4446 | + assert!( | ||
| 4447 | + intrabc_arms > 0 && blocks_total > 0, | ||
| 4448 | + "{NAME}: census is vacuous -- no arm decoded an intrabc block at all" | ||
| 4449 | + ); | ||
| 4450 | + assert!( | ||
| 4451 | + premise_arms > 0, | ||
| 4452 | + "{NAME}: census is vacuous -- no arm coded a frame with both allow_intrabc and \ | ||
| 4453 | + tx_mode == Select, so the refusal's own premise was never within reach" | ||
| 4454 | + ); | ||
| 4455 | + assert_eq!( | ||
| 4456 | + refused_tx_select, 1, | ||
| 4457 | + "{NAME}: exactly one arm (tiled-square-cq30-txs1) must reach the refusal -- if \ | ||
| 4458 | + none does, the census no longer measures it; if more do, say so here" | ||
| 4459 | + ); | ||
| 4460 | + assert!(frames_compared > 0, "{NAME}: no frame was compared pixel-exact"); | ||
| 4461 | + } | ||
| 4462 | + | ||
| 4463 | + /// lane-t900 r31, the WAITING witness for the capability | ||
| 4464 | + /// [`an_intrabc_census_over_screen_key_frames_measures_the_tx_select_refusal`] | ||
| 4465 | + /// pins: a real aomenc screen key frame whose intrabc block sits on a | ||
| 4466 | + /// square block of a `TX_MODE_SELECT` frame, unskipped. libaom's | ||
| 4467 | + /// `parse_decode_block` treats an intrabc block as `inter_block_tx`, so | ||
| 4468 | + /// its transform size comes off the inter var-tx partition tree | ||
| 4469 | + /// (`read_var_tx_size`), which this decoder reads nowhere on the intra | ||
| 4470 | + /// path -- reading the intra `tx_depth` symbol instead desyncs. | ||
| 4471 | + /// | ||
| 4472 | + /// Two arms of the same recipe: `cq=30`, whose intrabc block is unskipped | ||
| 4473 | + /// and refused by name, and `cq=45`, whose intrabc blocks are all `skip` | ||
| 4474 | + /// and therefore pass the `&& !skip` guard -- and decode WRONG (frame 0 | ||
| 4475 | + /// mismatches ffmpeg). So the refusal is one `&&` narrower than the hole: | ||
| 4476 | + /// until the var-tx read lands, the skipped case needs the same refusal. | ||
| 4477 | + #[test] | ||
| 4478 | + #[ignore = "capability gap: an intrabc block's tx size is coded by the inter var-tx tree"] | ||
| 4479 | + fn an_intrabc_block_under_tx_mode_select_decodes_pixel_exact() { | ||
| 4480 | + const NAME: &str = "an_intrabc_block_under_tx_mode_select_decodes_pixel_exact"; | ||
| 4481 | + if !have_ffmpeg() || !have_aomenc() { | ||
| 4482 | + eprintln!("SKIP {NAME}: no ffmpeg/aomenc"); | ||
| 4483 | + return; | ||
| 4484 | + } | ||
| 4485 | + let (width, height) = (256usize, 192usize); | ||
| 4486 | + for cq in ["30", "45"] { | ||
| 4487 | + let stream = screen_intrabc_stream("smptebars=size=128x96:rate=25", cq, "1", true, true); | ||
| 4488 | + let (select, premise) = tx_select_and_intrabc_frames(&stream); | ||
| 4489 | + assert!( | ||
| 4490 | + select > 0 && premise > 0, | ||
| 4491 | + "{NAME}: cq={cq} no longer codes allow_intrabc under TX_MODE_SELECT \ | ||
| 4492 | + (select={select} premise={premise})" | ||
| 4493 | + ); | ||
| 4494 | + crate::decode::reset_intrabc_hits(); | ||
| 4495 | + let frames = decode_stream(&stream).unwrap_or_else(|e| panic!("{NAME}: cq={cq}: {e}")); | ||
| 4496 | + assert!( | ||
| 4497 | + crate::decode::intrabc_hits() > 0, | ||
| 4498 | + "{NAME}: cq={cq} no longer decodes an intrabc block" | ||
| 4499 | + ); | ||
| 4500 | + let theirs = ffmpeg_decode_sequence(&stream, width, height, frames.len()); | ||
| 4501 | + assert_eq!(frames.len(), theirs.len(), "{NAME}: cq={cq} frame count"); | ||
| 4502 | + for (i, (ours, ref_frame)) in frames.iter().zip(theirs.iter()).enumerate() { | ||
| 4503 | + assert!( | ||
| 4504 | + ours.y == ref_frame.y && ours.u == ref_frame.u && ours.v == ref_frame.v, | ||
| 4505 | + "{NAME}: cq={cq} frame {i} does not match ffmpeg" | ||
| 4506 | + ); | ||
| 4507 | + } | ||
| 4508 | + } | ||
| 4509 | + } | ||
| 4510 | + | ||
| 4205 | /// lane-kf900 r4, the gate for the two lifted palette refusals ("a 1:4 | 4511 | /// lane-kf900 r4, the gate for the two lifted palette refusals ("a 1:4 |
| 4206 | /// rect strip that actually uses a palette (reconstruction is not ported | 4512 | /// rect strip that actually uses a palette (reconstruction is not ported |
| 4207 | /// at this shape)" and "a palette block on a HORZ/VERT intra strip below | 4513 | /// at this shape)" and "a palette block on a HORZ/VERT intra strip below |