Merge branch 'lane-superres': inter-frame superres decodes pixel-exact
Browse files at this commit10 changed files +864 -114
- crates/ec-av1/src/decode.rs +202 -51
- crates/ec-av1/src/mc.rs +189 -1
- crates/ec-av1/src/refusal_inventory.rs +4 -2
- crates/ec-av1/src/stream.rs +129 -58
- crates/ec-av1/src/superres.rs +2 -2
- lanes/superres-r10.charter.md +43 -0
- lanes/superres-r8.charter.md +46 -0
- lanes/superres-r8.report.md +128 -0
- lanes/superres-r9.charter.md +44 -0
- lanes/superres-r9.report.md +77 -0
crates/ec-av1/src/decode.rsmodified+202 -51
| @@ -6157,18 +6157,21 @@ pub fn decode_key_frame_tile( | ||
| 6157 | 6157 | thread_local! { |
| 6158 | 6158 | /// lane-superres r3: the real decoded margin beyond `frame_width` up to |
| 6159 | 6159 | /// the mi-aligned `true_width`, set by the most recent |
| 6160 | - /// [`decode_key_frame_tile_with_cdfs`] call. See that function's own | |
| 6161 | - /// comment at the write site. `None` when there was no margin to save. | |
| 6162 | - static LAST_KEY_FRAME_WIDE_MARGIN: std::cell::RefCell<Option<Picture>> = | |
| 6160 | + /// [`decode_key_frame_tile_with_cdfs`] OR (lane-superres r10) | |
| 6161 | + /// [`decode_inter_frame_tile_with_cdfs`] call -- both frame kinds crop | |
| 6162 | + /// their reconstructed buffer down from the same mi-aligned extent, so | |
| 6163 | + /// they share this one slot. See each function's own comment at its | |
| 6164 | + /// write site. `None` when there was no margin to save. | |
| 6165 | + static LAST_FRAME_WIDE_MARGIN: std::cell::RefCell<Option<Picture>> = | |
| 6163 | 6166 | const { std::cell::RefCell::new(None) }; |
| 6164 | 6167 | } |
| 6165 | 6168 | |
| 6166 | -/// The margin [`decode_key_frame_tile_with_cdfs`] most recently stashed | |
| 6167 | -/// (see [`LAST_KEY_FRAME_WIDE_MARGIN`]) -- `stream.rs`'s superres path | |
| 6168 | -/// reads this immediately after the call, before any other key frame | |
| 6169 | +/// The margin the most recent key- or inter-frame tile decode stashed | |
| 6170 | +/// (see [`LAST_FRAME_WIDE_MARGIN`]) -- `stream.rs`'s superres path | |
| 6171 | +/// reads this immediately after the call, before any other frame | |
| 6169 | 6172 | /// decode can overwrite it. |
| 6170 | -pub(crate) fn take_last_key_frame_wide_margin() -> Option<Picture> { | |
| 6171 | - LAST_KEY_FRAME_WIDE_MARGIN.with(|m| m.borrow_mut().take()) | |
| 6173 | +pub(crate) fn take_last_frame_wide_margin() -> Option<Picture> { | |
| 6174 | + LAST_FRAME_WIDE_MARGIN.with(|m| m.borrow_mut().take()) | |
| 6172 | 6175 | } |
| 6173 | 6176 | |
| 6174 | 6177 | /// [`decode_key_frame_tile`], threading a cross-frame CDF forward (spec |
| @@ -6875,7 +6878,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( | ||
| 6875 | 6878 | |
| 6876 | 6879 | let (fw, fh) = (frame_width as usize, frame_height as usize); |
| 6877 | 6880 | if fw == width && fh == height { |
| 6878 | - LAST_KEY_FRAME_WIDE_MARGIN.with(|m| *m.borrow_mut() = None); | |
| 6881 | + LAST_FRAME_WIDE_MARGIN.with(|m| *m.borrow_mut() = None); | |
| 6879 | 6882 | return Ok(( |
| 6880 | 6883 | Picture { |
| 6881 | 6884 | width, |
| @@ -6908,7 +6911,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( | ||
| 6908 | 6911 | // reaches into that real margin; `None` when there is no margin (this |
| 6909 | 6912 | // frame's `true_width`/`true_height` already equal `fw`/`fh`, e.g. a |
| 6910 | 6913 | // non-superres frame whose width just isn't 32-block aligned). |
| 6911 | - LAST_KEY_FRAME_WIDE_MARGIN.with(|m| { | |
| 6914 | + LAST_FRAME_WIDE_MARGIN.with(|m| { | |
| 6912 | 6915 | *m.borrow_mut() = if true_width > fw || true_height > fh { |
| 6913 | 6916 | let yc = crop(&y, true_width, true_height); |
| 6914 | 6917 | if let Ok(path) = std::env::var("EC_AV1_MARGIN_DUMP") { |
| @@ -8292,6 +8295,13 @@ fn decode_inter_block( | ||
| 8292 | 8295 | // `frame_is_intra_only`), but it still calls `read_palette_mode_info` |
| 8293 | 8296 | // under the same `av1_allow_palette` gate as the key-frame path. |
| 8294 | 8297 | allow_screen_content_tools: bool, |
| 8298 | + // lane-superres r9: this frame's own coded luma width (spec 7.11.3.3) -- | |
| 8299 | + // compared against each reference's stored picture width to decide | |
| 8300 | + // whether a scaled-MC path (`mc::predict_scaled`) or the ordinary | |
| 8301 | + // stride-1 one (`mc::predict_with_filters`) applies per reference, and to | |
| 8302 | + // refuse (rather than mis-decode) the warp/OBMC/interintra/compound | |
| 8303 | + // combinations that don't implement scaled MC yet. | |
| 8304 | + frame_width: usize, | |
| 8295 | 8305 | ) -> Result<()> { |
| 8296 | 8306 | let (r, c) = at; |
| 8297 | 8307 | let (px, py) = (c * SUB, r * SUB); |
| @@ -8850,6 +8860,16 @@ fn decode_inter_block( | ||
| 8850 | 8860 | ); |
| 8851 | 8861 | block_filter = resolved_filter; |
| 8852 | 8862 | |
| 8863 | + // lane-superres r9: a scaled reference in a COMPOUND_REFERENCE | |
| 8864 | + // block (either tap) is not wired -- mc::predict_compound_intermediate | |
| 8865 | + // has no scaled counterpart yet -- refuse by name rather than | |
| 8866 | + // silently sample it unscaled. | |
| 8867 | + if py0.width != frame_width || py1.width != frame_width { | |
| 8868 | + return Err(unsupported( | |
| 8869 | + "a compound-reference block with a scaled reference (superres, unimplemented)", | |
| 8870 | + )); | |
| 8871 | + } | |
| 8872 | + | |
| 8853 | 8873 | let mut inter0_y = vec![0i32; side * side]; |
| 8854 | 8874 | mc::predict_compound_intermediate( |
| 8855 | 8875 | &py0.data, |
| @@ -9524,48 +9544,110 @@ fn decode_inter_block( | ||
| 9524 | 9544 | mode_for_tx = 0; |
| 9525 | 9545 | uv_predict_mode = DC_PRED; |
| 9526 | 9546 | |
| 9547 | + // lane-superres r9: spec 7.11.3.3's x_scale_fp comes from LUMA | |
| 9548 | + // widths only (r8's derivation) and applies unchanged to the | |
| 9549 | + // chroma calls below (their x_q4 is already in the chroma | |
| 9550 | + // plane's own pixel units). `mc::predict_scaled` has no warp/OBMC | |
| 9551 | + // /interintra counterpart -- those three combinations are refused | |
| 9552 | + // by name instead of silently sampling a scaled reference wrong | |
| 9553 | + // (warp_params/obmc_selected/interintra_mode are all resolved by | |
| 9554 | + // this point, every symbol for this block already read). | |
| 9555 | + let luma_scale = mc::scale_factor(py_ref.width, frame_width); | |
| 9556 | + if luma_scale != mc::REF_NO_SCALE | |
| 9557 | + && (warp_params.is_some() || obmc_selected || interintra_mode.is_some()) | |
| 9558 | + { | |
| 9559 | + return Err(unsupported( | |
| 9560 | + "warp/OBMC/interintra prediction with a scaled reference (superres, unimplemented)", | |
| 9561 | + )); | |
| 9562 | + } | |
| 9563 | + | |
| 9527 | 9564 | let mut pred_y = vec![0u16; side * side]; |
| 9528 | - mc::predict_with_filters( | |
| 9529 | - &py_ref.data, | |
| 9530 | - py_ref.width, | |
| 9531 | - py_ref.true_width, | |
| 9532 | - py_ref.true_height, | |
| 9533 | - mv_to_q4(px, mv.1, true), | |
| 9534 | - mv_to_q4(py, mv.0, true), | |
| 9535 | - side, | |
| 9536 | - side, | |
| 9537 | - h_filter, | |
| 9538 | - v_filter, | |
| 9539 | - &mut pred_y, | |
| 9540 | - ); | |
| 9541 | 9565 | let mut pred_u = vec![0u16; chroma_side * chroma_side]; |
| 9542 | - mc::predict_with_filters( | |
| 9543 | - &pu_ref.data, | |
| 9544 | - pu_ref.width, | |
| 9545 | - pu_ref.true_width, | |
| 9546 | - pu_ref.true_height, | |
| 9547 | - mv_to_q4(cpx, mv.1, false), | |
| 9548 | - mv_to_q4(cpy, mv.0, false), | |
| 9549 | - chroma_side, | |
| 9550 | - chroma_side, | |
| 9551 | - h_filter, | |
| 9552 | - v_filter, | |
| 9553 | - &mut pred_u, | |
| 9554 | - ); | |
| 9555 | 9566 | let mut pred_v = vec![0u16; chroma_side * chroma_side]; |
| 9556 | - mc::predict_with_filters( | |
| 9557 | - &pv_ref.data, | |
| 9558 | - pv_ref.width, | |
| 9559 | - pv_ref.true_width, | |
| 9560 | - pv_ref.true_height, | |
| 9561 | - mv_to_q4(cpx, mv.1, false), | |
| 9562 | - mv_to_q4(cpy, mv.0, false), | |
| 9563 | - chroma_side, | |
| 9564 | - chroma_side, | |
| 9565 | - h_filter, | |
| 9566 | - v_filter, | |
| 9567 | - &mut pred_v, | |
| 9568 | - ); | |
| 9567 | + if luma_scale == mc::REF_NO_SCALE { | |
| 9568 | + mc::predict_with_filters( | |
| 9569 | + &py_ref.data, | |
| 9570 | + py_ref.width, | |
| 9571 | + py_ref.true_width, | |
| 9572 | + py_ref.true_height, | |
| 9573 | + mv_to_q4(px, mv.1, true), | |
| 9574 | + mv_to_q4(py, mv.0, true), | |
| 9575 | + side, | |
| 9576 | + side, | |
| 9577 | + h_filter, | |
| 9578 | + v_filter, | |
| 9579 | + &mut pred_y, | |
| 9580 | + ); | |
| 9581 | + mc::predict_with_filters( | |
| 9582 | + &pu_ref.data, | |
| 9583 | + pu_ref.width, | |
| 9584 | + pu_ref.true_width, | |
| 9585 | + pu_ref.true_height, | |
| 9586 | + mv_to_q4(cpx, mv.1, false), | |
| 9587 | + mv_to_q4(cpy, mv.0, false), | |
| 9588 | + chroma_side, | |
| 9589 | + chroma_side, | |
| 9590 | + h_filter, | |
| 9591 | + v_filter, | |
| 9592 | + &mut pred_u, | |
| 9593 | + ); | |
| 9594 | + mc::predict_with_filters( | |
| 9595 | + &pv_ref.data, | |
| 9596 | + pv_ref.width, | |
| 9597 | + pv_ref.true_width, | |
| 9598 | + pv_ref.true_height, | |
| 9599 | + mv_to_q4(cpx, mv.1, false), | |
| 9600 | + mv_to_q4(cpy, mv.0, false), | |
| 9601 | + chroma_side, | |
| 9602 | + chroma_side, | |
| 9603 | + h_filter, | |
| 9604 | + v_filter, | |
| 9605 | + &mut pred_v, | |
| 9606 | + ); | |
| 9607 | + } else { | |
| 9608 | + mc::predict_scaled( | |
| 9609 | + &py_ref.data, | |
| 9610 | + py_ref.width, | |
| 9611 | + py_ref.true_width, | |
| 9612 | + py_ref.true_height, | |
| 9613 | + mv_to_q4(px, mv.1, true), | |
| 9614 | + mv_to_q4(py, mv.0, true), | |
| 9615 | + luma_scale, | |
| 9616 | + side, | |
| 9617 | + side, | |
| 9618 | + h_filter, | |
| 9619 | + v_filter, | |
| 9620 | + &mut pred_y, | |
| 9621 | + ); | |
| 9622 | + mc::predict_scaled( | |
| 9623 | + &pu_ref.data, | |
| 9624 | + pu_ref.width, | |
| 9625 | + pu_ref.true_width, | |
| 9626 | + pu_ref.true_height, | |
| 9627 | + mv_to_q4(cpx, mv.1, false), | |
| 9628 | + mv_to_q4(cpy, mv.0, false), | |
| 9629 | + luma_scale, | |
| 9630 | + chroma_side, | |
| 9631 | + chroma_side, | |
| 9632 | + h_filter, | |
| 9633 | + v_filter, | |
| 9634 | + &mut pred_u, | |
| 9635 | + ); | |
| 9636 | + mc::predict_scaled( | |
| 9637 | + &pv_ref.data, | |
| 9638 | + pv_ref.width, | |
| 9639 | + pv_ref.true_width, | |
| 9640 | + pv_ref.true_height, | |
| 9641 | + mv_to_q4(cpx, mv.1, false), | |
| 9642 | + mv_to_q4(cpy, mv.0, false), | |
| 9643 | + luma_scale, | |
| 9644 | + chroma_side, | |
| 9645 | + chroma_side, | |
| 9646 | + h_filter, | |
| 9647 | + v_filter, | |
| 9648 | + &mut pred_v, | |
| 9649 | + ); | |
| 9650 | + } | |
| 9569 | 9651 | |
| 9570 | 9652 | if let Some(params) = &warp_params { |
| 9571 | 9653 | crate::warp::warp_affine( |
| @@ -11182,9 +11264,16 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 11182 | 11264 | return Err(unsupported("a frame with no mode-info grid")); |
| 11183 | 11265 | } |
| 11184 | 11266 | let (true_width, true_height) = ((mi_cols * 4) as usize, (mi_rows * 4) as usize); |
| 11185 | - if reference.width != true_width || reference.height != true_height { | |
| 11267 | + // lane-superres r10: a width mismatch alone is no longer refused here -- | |
| 11268 | + // `mc::predict_scaled` (spec 7.11.3.3, threaded through | |
| 11269 | + // `decode_inter_block`'s single-ref branch) exists precisely for a | |
| 11270 | + // `use_superres` reference at a different width than this frame's own | |
| 11271 | + // true size. Height must still match: AV1 superres never scales height | |
| 11272 | + // (r8's derivation, `mc.rs`'s own doc), and nothing in this decode path | |
| 11273 | + // has a vertical scaling pass. | |
| 11274 | + if reference.height != true_height { | |
| 11186 | 11275 | return Err(unsupported( |
| 11187 | - "a reference picture that is not this frame's own true size", | |
| 11276 | + "a reference picture whose height does not match this frame's own true size", | |
| 11188 | 11277 | )); |
| 11189 | 11278 | } |
| 11190 | 11279 | let (cols, rows) = block_grid(mi_cols, mi_rows); |
| @@ -11555,6 +11644,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 11555 | 11644 | BLOCK, |
| 11556 | 11645 | BLOCK, |
| 11557 | 11646 | allow_screen_content_tools, |
| 11647 | + frame_width as usize, | |
| 11558 | 11648 | )?; |
| 11559 | 11649 | } |
| 11560 | 11650 | PARTITION_SPLIT => { |
| @@ -11634,6 +11724,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 11634 | 11724 | SUB, |
| 11635 | 11725 | SUB, |
| 11636 | 11726 | allow_screen_content_tools, |
| 11727 | + frame_width as usize, | |
| 11637 | 11728 | )?; |
| 11638 | 11729 | } else { |
| 11639 | 11730 | let ctx16 = neighbours.partition_ctx(at16, SUB); |
| @@ -11653,6 +11744,24 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 11653 | 11744 | .map(|i| (mi_row0 + (i / 2) * 2, mi_col0 + (i % 2) * 2)) |
| 11654 | 11745 | .filter(|&(mr, mc)| mr < mi_rows && mc < mi_cols) |
| 11655 | 11746 | .collect(); |
| 11747 | + // lane-superres r9: decode_inter_block8's own | |
| 11748 | + // MC (below) is not threaded for a scaled | |
| 11749 | + // reference (unlike decode_inter_block's 19 | |
| 11750 | + // sites) -- refuse the whole 8x8-leaf split | |
| 11751 | + // up front when ANY live reference this leaf | |
| 11752 | + // could pick has a different width than this | |
| 11753 | + // frame, rather than thread frame_width | |
| 11754 | + // through its own single-ref/compound MC | |
| 11755 | + // calls to reach the same block this round. | |
| 11756 | + if ref_y.width != frame_width as usize | |
| 11757 | + || ref_slots.iter().flatten().any(|(py, _, _)| { | |
| 11758 | + py.width != frame_width as usize | |
| 11759 | + }) | |
| 11760 | + { | |
| 11761 | + return Err(unsupported( | |
| 11762 | + "an 8x8 partition leaf under a scaled reference (superres, unimplemented)", | |
| 11763 | + )); | |
| 11764 | + } | |
| 11656 | 11765 | let mut prev_leaf: Option<((usize, usize), bool, bool)> = None; |
| 11657 | 11766 | let mut last_compound_ctx: Option<(i8, i8, u8, u8)> = None; |
| 11658 | 11767 | let mut last_skip_mode = false; |
| @@ -11812,6 +11921,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 11812 | 11921 | BLOCK, |
| 11813 | 11922 | SUB, |
| 11814 | 11923 | allow_screen_content_tools, |
| 11924 | + frame_width as usize, | |
| 11815 | 11925 | )?; |
| 11816 | 11926 | if (r32 * 2 + 1) as u32 * SUB_MI < mi_rows { |
| 11817 | 11927 | decode_inter_block( |
| @@ -11865,6 +11975,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 11865 | 11975 | SUB, |
| 11866 | 11976 | SUB, |
| 11867 | 11977 | allow_screen_content_tools, |
| 11978 | + frame_width as usize, | |
| 11868 | 11979 | )?; |
| 11869 | 11980 | if (c32 * 2 + 1) as u32 * SUB_MI < mi_cols { |
| 11870 | 11981 | decode_inter_block( |
| @@ -11918,6 +12029,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 11918 | 12029 | SUB, |
| 11919 | 12030 | SUB, |
| 11920 | 12031 | allow_screen_content_tools, |
| 12032 | + frame_width as usize, | |
| 11921 | 12033 | )?; |
| 11922 | 12034 | } |
| 11923 | 12035 | } |
| @@ -11977,6 +12089,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 11977 | 12089 | BLOCK, |
| 11978 | 12090 | SUB, |
| 11979 | 12091 | allow_screen_content_tools, |
| 12092 | + frame_width as usize, | |
| 11980 | 12093 | )?; |
| 11981 | 12094 | decode_inter_block( |
| 11982 | 12095 | &mut dec, |
| @@ -12025,6 +12138,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 12025 | 12138 | BLOCK, |
| 12026 | 12139 | SUB, |
| 12027 | 12140 | allow_screen_content_tools, |
| 12141 | + frame_width as usize, | |
| 12028 | 12142 | )?; |
| 12029 | 12143 | } |
| 12030 | 12144 | PARTITION_VERT => { |
| @@ -12078,6 +12192,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 12078 | 12192 | SUB, |
| 12079 | 12193 | BLOCK, |
| 12080 | 12194 | allow_screen_content_tools, |
| 12195 | + frame_width as usize, | |
| 12081 | 12196 | )?; |
| 12082 | 12197 | decode_inter_block( |
| 12083 | 12198 | &mut dec, |
| @@ -12126,6 +12241,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 12126 | 12241 | SUB, |
| 12127 | 12242 | BLOCK, |
| 12128 | 12243 | allow_screen_content_tools, |
| 12244 | + frame_width as usize, | |
| 12129 | 12245 | )?; |
| 12130 | 12246 | } |
| 12131 | 12247 | PARTITION_HORZ_A => { |
| @@ -12184,6 +12300,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 12184 | 12300 | SUB, |
| 12185 | 12301 | SUB, |
| 12186 | 12302 | allow_screen_content_tools, |
| 12303 | + frame_width as usize, | |
| 12187 | 12304 | )?; |
| 12188 | 12305 | decode_inter_block( |
| 12189 | 12306 | &mut dec, |
| @@ -12236,6 +12353,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 12236 | 12353 | SUB, |
| 12237 | 12354 | SUB, |
| 12238 | 12355 | allow_screen_content_tools, |
| 12356 | + frame_width as usize, | |
| 12239 | 12357 | )?; |
| 12240 | 12358 | decode_inter_block( |
| 12241 | 12359 | &mut dec, |
| @@ -12284,6 +12402,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 12284 | 12402 | BLOCK, |
| 12285 | 12403 | SUB, |
| 12286 | 12404 | allow_screen_content_tools, |
| 12405 | + frame_width as usize, | |
| 12287 | 12406 | )?; |
| 12288 | 12407 | } |
| 12289 | 12408 | PARTITION_VERT_A => { |
| @@ -12341,6 +12460,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 12341 | 12460 | SUB, |
| 12342 | 12461 | SUB, |
| 12343 | 12462 | allow_screen_content_tools, |
| 12463 | + frame_width as usize, | |
| 12344 | 12464 | )?; |
| 12345 | 12465 | decode_inter_block( |
| 12346 | 12466 | &mut dec, |
| @@ -12393,6 +12513,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 12393 | 12513 | SUB, |
| 12394 | 12514 | SUB, |
| 12395 | 12515 | allow_screen_content_tools, |
| 12516 | + frame_width as usize, | |
| 12396 | 12517 | )?; |
| 12397 | 12518 | decode_inter_block( |
| 12398 | 12519 | &mut dec, |
| @@ -12441,6 +12562,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 12441 | 12562 | SUB, |
| 12442 | 12563 | BLOCK, |
| 12443 | 12564 | allow_screen_content_tools, |
| 12565 | + frame_width as usize, | |
| 12444 | 12566 | )?; |
| 12445 | 12567 | } |
| 12446 | 12568 | PARTITION_VERT_B => { |
| @@ -12495,6 +12617,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 12495 | 12617 | SUB, |
| 12496 | 12618 | BLOCK, |
| 12497 | 12619 | allow_screen_content_tools, |
| 12620 | + frame_width as usize, | |
| 12498 | 12621 | )?; |
| 12499 | 12622 | decode_inter_block( |
| 12500 | 12623 | &mut dec, |
| @@ -12547,6 +12670,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 12547 | 12670 | SUB, |
| 12548 | 12671 | SUB, |
| 12549 | 12672 | allow_screen_content_tools, |
| 12673 | + frame_width as usize, | |
| 12550 | 12674 | )?; |
| 12551 | 12675 | decode_inter_block( |
| 12552 | 12676 | &mut dec, |
| @@ -12599,6 +12723,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 12599 | 12723 | SUB, |
| 12600 | 12724 | SUB, |
| 12601 | 12725 | allow_screen_content_tools, |
| 12726 | + frame_width as usize, | |
| 12602 | 12727 | )?; |
| 12603 | 12728 | } |
| 12604 | 12729 | _ => { |
| @@ -12656,6 +12781,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 12656 | 12781 | |
| 12657 | 12782 | let (fw, fh) = (frame_width as usize, frame_height as usize); |
| 12658 | 12783 | if fw == width && fh == height { |
| 12784 | + LAST_FRAME_WIDE_MARGIN.with(|m| *m.borrow_mut() = None); | |
| 12659 | 12785 | return Ok(( |
| 12660 | 12786 | Picture { |
| 12661 | 12787 | width, |
| @@ -12675,6 +12801,30 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 12675 | 12801 | } |
| 12676 | 12802 | out |
| 12677 | 12803 | }; |
| 12804 | + // lane-superres r10 (fixed r9): same real-margin stash as | |
| 12805 | + // `decode_key_frame_tile_with_cdfs` (see that function's comment) -- the | |
| 12806 | + // real reconstructed extent is the mi-aligned `true_width`/`true_height` | |
| 12807 | + // (`mi_cols`/`mi_rows` * 4), NOT the superblock-padded `width`/`height` | |
| 12808 | + // this branch was reading before: columns `[true_width, width)` were | |
| 12809 | + // never actually coded (the last partial superblock stops at | |
| 12810 | + // `true_width`), so stashing out to `width` handed the chroma upscale's | |
| 12811 | + // right-edge replicate a slice of uninitialized/stale buffer one column | |
| 12812 | + // early -- the same shorten-vs-replicate shape as `av1-truesize`. The | |
| 12813 | + // key-frame branch above already used `true_width`/`true_height`; this | |
| 12814 | + // just matches it. | |
| 12815 | + LAST_FRAME_WIDE_MARGIN.with(|m| { | |
| 12816 | + *m.borrow_mut() = if true_width > fw || true_height > fh { | |
| 12817 | + Some(Picture { | |
| 12818 | + width: true_width, | |
| 12819 | + height: true_height, | |
| 12820 | + y: crop(&y, true_width, true_height), | |
| 12821 | + u: crop(&u, true_width.div_ceil(2), true_height.div_ceil(2)), | |
| 12822 | + v: crop(&v, true_width.div_ceil(2), true_height.div_ceil(2)), | |
| 12823 | + }) | |
| 12824 | + } else { | |
| 12825 | + None | |
| 12826 | + } | |
| 12827 | + }); | |
| 12678 | 12828 | Ok(( |
| 12679 | 12829 | Picture { |
| 12680 | 12830 | width: fw, |
| @@ -13453,6 +13603,7 @@ mod tests { | ||
| 13453 | 13603 | side, |
| 13454 | 13604 | side, |
| 13455 | 13605 | false, |
| 13606 | + width, | |
| 13456 | 13607 | ) |
| 13457 | 13608 | .unwrap(); |
| 13458 | 13609 |
crates/ec-av1/src/mc.rsmodified+189 -1
| @@ -411,6 +411,141 @@ pub fn predict_with_filters( | ||
| 411 | 411 | crate::encode::stage_add(1, stage_t.elapsed()); |
| 412 | 412 | } |
| 413 | 413 | |
| 414 | +/// `REF_SCALE_SHIFT`'s "no scaling" value (spec 7.11.3.3, libaom `scale.h` | |
| 415 | +/// `REF_NO_SCALE`): `x_scale_fp == REF_NO_SCALE` means the stored reference's | |
| 416 | +/// luma width equals the current frame's, so [`predict_scaled`] reduces | |
| 417 | +/// exactly to [`predict_with_filters`] (verified in | |
| 418 | +/// `predict_scaled_at_no_scale_matches_predict_with_filters` below). | |
| 419 | +pub const REF_NO_SCALE: i64 = 1 << 14; | |
| 420 | + | |
| 421 | +/// The horizontal scale ratio spec 7.11.3.3 derives from luma widths only | |
| 422 | +/// (libaom `av1_setup_scale_factors_for_frame`, luma widths, `REF_SCALE_SHIFT | |
| 423 | +/// == 14`): `other_width` is the stored reference picture's own luma width, | |
| 424 | +/// `this_width` the current frame's coded luma width. AV1 superres never | |
| 425 | +/// scales height (r8's derivation), so there is no vertical counterpart. | |
| 426 | +pub fn scale_factor(other_width: usize, this_width: usize) -> i64 { | |
| 427 | + (((other_width as i64) << 14) + this_width as i64 / 2) / this_width as i64 | |
| 428 | +} | |
| 429 | + | |
| 430 | +thread_local! { | |
| 431 | + /// lane-superres r10: firing count for the bypass gate (class | |
| 432 | + /// `gate-blind-to-feature`) -- how many times [`predict_scaled`] itself | |
| 433 | + /// ran, so the gate can hard-assert the scaled MC path actually fired | |
| 434 | + /// rather than trust a pixel match alone (an unscaled reference would | |
| 435 | + /// pass the pixels and prove nothing). | |
| 436 | + static PREDICT_SCALED_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; | |
| 437 | +} | |
| 438 | + | |
| 439 | +/// Current value of [`PREDICT_SCALED_HITS`]. | |
| 440 | +pub fn predict_scaled_hits() -> usize { | |
| 441 | + PREDICT_SCALED_HITS.with(|c| c.get()) | |
| 442 | +} | |
| 443 | + | |
| 444 | +fn round_pow2_64(value: i64, shift: u32) -> i64 { | |
| 445 | + if shift == 0 { | |
| 446 | + value | |
| 447 | + } else { | |
| 448 | + (value + (1i64 << (shift - 1))) >> shift | |
| 449 | + } | |
| 450 | +} | |
| 451 | + | |
| 452 | +/// `ROUND_POWER_OF_TWO_SIGNED` (libaom `common/common.h`): halves round away | |
| 453 | +/// from zero's *magnitude* -- negative inputs are rounded like their | |
| 454 | +/// negation, not truncated toward zero the way a plain arithmetic shift | |
| 455 | +/// would. | |
| 456 | +fn round_pow2_signed_64(value: i64, shift: u32) -> i64 { | |
| 457 | + if value < 0 { | |
| 458 | + -round_pow2_64(-value, shift) | |
| 459 | + } else { | |
| 460 | + round_pow2_64(value, shift) | |
| 461 | + } | |
| 462 | +} | |
| 463 | + | |
| 464 | +/// [`predict_with_filters`]'s scaled-reference counterpart (spec 7.11.3.3): | |
| 465 | +/// used only when the stored reference's luma width differs from the current | |
| 466 | +/// frame's (`use_superres` on a non-key frame). `x_scale_fp` is | |
| 467 | +/// [`scale_factor`]'s ratio ([`REF_NO_SCALE`] reproduces | |
| 468 | +/// `predict_with_filters` bit-exact, pinned by | |
| 469 | +/// `predict_scaled_at_no_scale_matches_predict_with_filters`). Per r8's | |
| 470 | +/// derivation AV1 superres never scales height, so `y_q4`'s whole-sample / | |
| 471 | +/// fraction split and the vertical pass are untouched -- only the horizontal | |
| 472 | +/// pass's per-column integer position and filter phase come from a scaled | |
| 473 | +/// walk (`x_qn`) instead of a fixed stride-1 one. | |
| 474 | +/// | |
| 475 | +/// # Panics | |
| 476 | +/// Panics when `dst` is not `block_w * block_h` long, or the reference is | |
| 477 | +/// empty. | |
| 478 | +#[allow(clippy::too_many_arguments)] | |
| 479 | +pub fn predict_scaled( | |
| 480 | + reference: &[u16], | |
| 481 | + stride: usize, | |
| 482 | + true_width: usize, | |
| 483 | + true_height: usize, | |
| 484 | + x_q4: i32, | |
| 485 | + y_q4: i32, | |
| 486 | + x_scale_fp: i64, | |
| 487 | + block_w: usize, | |
| 488 | + block_h: usize, | |
| 489 | + h_kind: InterpFilterKind, | |
| 490 | + v_kind: InterpFilterKind, | |
| 491 | + dst: &mut [u16], | |
| 492 | +) { | |
| 493 | + assert_eq!(dst.len(), block_w * block_h, "the destination is the block"); | |
| 494 | + assert!(!reference.is_empty(), "a reference plane has samples"); | |
| 495 | + PREDICT_SCALED_HITS.with(|c| c.set(c.get() + 1)); | |
| 496 | + | |
| 497 | + let y0 = y_q4.div_euclid(16); | |
| 498 | + let yfrac = y_q4.rem_euclid(16) as usize; | |
| 499 | + | |
| 500 | + // SCALE_SUBPEL_BITS == 10: x_step_qn is x_scale_fp (Q14) rounded down to | |
| 501 | + // Q10; off/pos_x_q10 fold the block's own x_q4 (Q4) into that same Q10 | |
| 502 | + // grid (spec 7.11.3.3's `dec_calc_subpel_params`). | |
| 503 | + let x_step_qn = round_pow2_64(x_scale_fp, 4); | |
| 504 | + let off = (x_scale_fp - REF_NO_SCALE) * 8; | |
| 505 | + let pos_x_q10 = round_pow2_signed_64(x_q4 as i64 * x_scale_fp + off, 8) + 32; | |
| 506 | + | |
| 507 | + let (h_wide, h_narrow) = h_kind.tables(); | |
| 508 | + let (v_wide, v_narrow) = v_kind.tables(); | |
| 509 | + let v_filter = if block_h <= 4 { | |
| 510 | + &v_narrow[yfrac] | |
| 511 | + } else { | |
| 512 | + &v_wide[yfrac] | |
| 513 | + }; | |
| 514 | + | |
| 515 | + let rows = block_h + 7; | |
| 516 | + let mut intermediate = vec![0i32; rows * block_w]; | |
| 517 | + for c in 0..block_w { | |
| 518 | + let x_qn = pos_x_q10 + c as i64 * x_step_qn; | |
| 519 | + let int_pel = (x_qn >> 10) as i32; | |
| 520 | + let filter_idx = ((x_qn & 1023) >> 6) as usize; | |
| 521 | + let h_filter = if block_w <= 4 { | |
| 522 | + &h_narrow[filter_idx] | |
| 523 | + } else { | |
| 524 | + &h_wide[filter_idx] | |
| 525 | + }; | |
| 526 | + for r in 0..rows { | |
| 527 | + let y = y0 - 3 + r as i32; | |
| 528 | + let mut sum = 0; | |
| 529 | + for (t, &tap) in h_filter.iter().enumerate() { | |
| 530 | + let x = int_pel + t as i32 - 3; | |
| 531 | + sum += tap * sample(reference, stride, true_width, true_height, x, y); | |
| 532 | + } | |
| 533 | + intermediate[r * block_w + c] = round2(sum, INTER_ROUND_0); | |
| 534 | + } | |
| 535 | + } | |
| 536 | + | |
| 537 | + for row in 0..block_h { | |
| 538 | + for col in 0..block_w { | |
| 539 | + let mut sum = 0; | |
| 540 | + for (t, &tap) in v_filter.iter().enumerate() { | |
| 541 | + sum += tap * intermediate[(row + t) * block_w + col]; | |
| 542 | + } | |
| 543 | + dst[row * block_w + col] = | |
| 544 | + round2(sum, INTER_ROUND_1).clamp(0, crate::decode::sample_max()) as u16; | |
| 545 | + } | |
| 546 | + } | |
| 547 | +} | |
| 548 | + | |
| 414 | 549 | /// `InterRound1` for a compound ref (spec 7.11.3.2's `isCompound` branch, |
| 415 | 550 | /// `COMPOUND_ROUND1_BITS` in libaom's `convolve.h`): 4 bits shallower than |
| 416 | 551 | /// [`INTER_ROUND_1`] so the vertical pass lands in the `CONV_BUF` domain |
| @@ -625,7 +760,10 @@ fn compound_intermediate_whole_pel_identity_round_trips_through_combine() { | ||
| 625 | 760 | |
| 626 | 761 | #[cfg(test)] |
| 627 | 762 | mod tests { |
| 628 | - use super::{InterpFilterKind, predict, predict_with_filter}; | |
| 763 | + use super::{ | |
| 764 | + InterpFilterKind, REF_NO_SCALE, predict, predict_scaled, predict_with_filter, | |
| 765 | + predict_with_filters, | |
| 766 | + }; | |
| 629 | 767 | |
| 630 | 768 | /// Regression pin for the lane-av1flake ±1 defect: a real aomenc chroma |
| 631 | 769 | /// inter block at the frame's top-left corner (mv q4 = (row 7, col 15), |
| @@ -906,4 +1044,54 @@ mod tests { | ||
| 906 | 1044 | "an MV past the bottom-right edge must clamp to the corner sample, got {dst2:?}" |
| 907 | 1045 | ); |
| 908 | 1046 | } |
| 1047 | + | |
| 1048 | + #[test] | |
| 1049 | + fn predict_scaled_at_no_scale_matches_predict_with_filters() { | |
| 1050 | + // Algebraic pin from the r8/r9 derivation: x_scale_fp == REF_NO_SCALE | |
| 1051 | + // must reduce predict_scaled's per-column scaled walk to the exact | |
| 1052 | + // same int_pel/filter_idx sequence predict_with_filters computes from | |
| 1053 | + // a fixed stride-1 walk -- every subpel fraction, every block width. | |
| 1054 | + let width = 24; | |
| 1055 | + let height = 24; | |
| 1056 | + let reference: Vec<u16> = (0..width * height).map(|i| (i * 7 % 251) as u16).collect(); | |
| 1057 | + for block_w in [4usize, 8, 16] { | |
| 1058 | + for x_frac in 0..16i32 { | |
| 1059 | + let x_q4 = 3 * 16 + x_frac; | |
| 1060 | + let y_q4 = 2 * 16 + 5; | |
| 1061 | + let mut expected = vec![0u16; block_w * 8]; | |
| 1062 | + predict_with_filters( | |
| 1063 | + &reference, | |
| 1064 | + width, | |
| 1065 | + width, | |
| 1066 | + height, | |
| 1067 | + x_q4, | |
| 1068 | + y_q4, | |
| 1069 | + block_w, | |
| 1070 | + 8, | |
| 1071 | + InterpFilterKind::Regular, | |
| 1072 | + InterpFilterKind::Regular, | |
| 1073 | + &mut expected, | |
| 1074 | + ); | |
| 1075 | + let mut got = vec![0u16; block_w * 8]; | |
| 1076 | + predict_scaled( | |
| 1077 | + &reference, | |
| 1078 | + width, | |
| 1079 | + width, | |
| 1080 | + height, | |
| 1081 | + x_q4, | |
| 1082 | + y_q4, | |
| 1083 | + REF_NO_SCALE, | |
| 1084 | + block_w, | |
| 1085 | + 8, | |
| 1086 | + InterpFilterKind::Regular, | |
| 1087 | + InterpFilterKind::Regular, | |
| 1088 | + &mut got, | |
| 1089 | + ); | |
| 1090 | + assert_eq!( | |
| 1091 | + got, expected, | |
| 1092 | + "block_w={block_w} x_frac={x_frac}: REF_NO_SCALE must reproduce predict_with_filters exactly" | |
| 1093 | + ); | |
| 1094 | + } | |
| 1095 | + } | |
| 1096 | + } | |
| 909 | 1097 | } |
crates/ec-av1/src/refusal_inventory.rsmodified+4 -2
| @@ -44,7 +44,6 @@ const REFUSALS: &[&str] = &[ | ||
| 44 | 44 | "a superblock-level HORZ/VERT strip with a split transform (per-unit rect prediction is not ported)", |
| 45 | 45 | "a tx_type symbol outside its CDF's own set: {t}", |
| 46 | 46 | "an INTER 32x32 partition type this decoder does not code (value={part32})", |
| 47 | - "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)", | |
| 48 | 47 | "a superblock-level partition type other than NONE or SPLIT (this decoder's intra tile path codes only those two at 64x64)", |
| 49 | 48 | "a HORZ/VERT intra strip in a screen-content frame (palette syntax is consumed for square blocks only)", |
| 50 | 49 | "a HORZ/VERT intra strip with a split transform (per-unit rect prediction is not ported)", |
| @@ -59,7 +58,7 @@ const REFUSALS: &[&str] = &[ | ||
| 59 | 58 | "a palette block with a split luma transform (round 1)", |
| 60 | 59 | "a reference frame other than LAST_FRAME (round 2)", |
| 61 | 60 | "a reference frame selected with no picture at this frame's own ref_frame_idx slot for it", |
| 62 | - "a reference picture that is not this frame's own true size", | |
| 61 | + "a reference picture whose height does not match this frame's own true size", | |
| 63 | 62 | "a show_existing_frame header naming an empty reference slot", |
| 64 | 63 | "an 8x8 leaf that coded WARPED_CAUSAL (motion_mode == 2): av1_find_projection/the affine warp filter are not ported, only motion_mode_allowed's alphabet choice is", |
| 65 | 64 | "an inter frame using TxMode::Select (this decoder's inter path never reads a tx_depth symbol, so it desyncs after the first block's mode)", |
| @@ -70,6 +69,9 @@ const REFUSALS: &[&str] = &[ | ||
| 70 | 69 | "an intra mode this decoder does not code (round 2)", |
| 71 | 70 | "an intra-coded HORZ/VERT strip needs rectangular intra prediction this decoder does not code yet", |
| 72 | 71 | "filter intra on a HORZ/VERT strip (this decoder predicts square-only)", |
| 72 | + "a compound-reference block with a scaled reference (superres, unimplemented)", | |
| 73 | + "warp/OBMC/interintra prediction with a scaled reference (superres, unimplemented)", | |
| 74 | + "an 8x8 partition leaf under a scaled reference (superres, unimplemented)", | |
| 73 | 75 | ]; |
| 74 | 76 | |
| 75 | 77 | /// Gates whose `Err` arm turns a decode failure into a printed SKIP rather than |
crates/ec-av1/src/stream.rsmodified+129 -58
| @@ -254,19 +254,16 @@ pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> { | ||
| 254 | 254 | // reads stay bit-exact in sync and `crate::superres::upscale_picture` |
| 255 | 255 | // (below, after decode) makes its `Picture` the spec-correct |
| 256 | 256 | // `upscaled_width` instead of silently staying at `frame_width`. |
| 257 | - // An INTER frame under `use_superres` still refuses: its reference | |
| 258 | - // is stored upscaled while the current frame decodes at the | |
| 259 | - // downscaled `frame_width`, so `decode_inter_frame_tile_with_cdfs`'s | |
| 260 | - // motion compensation would sample a reference at the wrong scale | |
| 261 | - // (spec 7.11.3.3's scaled-reference path is not ported) -- | |
| 262 | - // SILENT WRONGNESS, not a desync, same as the key-frame case used | |
| 263 | - // to be. Left for a later stage of this lane. | |
| 264 | - if header.use_superres && header.frame_type != FrameType::Key { | |
| 265 | - return Err(Error::unsupported( | |
| 266 | - "AV1 decode_stream", | |
| 267 | - "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)", | |
| 268 | - )); | |
| 269 | - } | |
| 257 | + // lane-superres r10: an INTER frame under `use_superres` no longer | |
| 258 | + // refuses at the frame level. `mc::predict_scaled` (spec 7.11.3.3) | |
| 259 | + // is wired into `decode_inter_block`'s single-ref, non-warp/OBMC/ | |
| 260 | + // interintra branch, and `decode_inter_frame_tile_with_cdfs` decodes | |
| 261 | + // at the downscaled `frame_width` and upscales its own output the | |
| 262 | + // same way the key-frame branch above does (see the inter branch | |
| 263 | + // below). The combinations `predict_scaled` does not cover | |
| 264 | + // (compound MC, warp/OBMC/interintra, `decode_inter_block8`'s 8x8 | |
| 265 | + // leaf) still refuse by name, deep in `decode.rs`, rather than | |
| 266 | + // silently mispredicting -- see `refusal_inventory.rs`. | |
| 270 | 267 | // Loop restoration carries per-restoration-unit symbols in the tile |
| 271 | 268 | // (spec 5.11.57 `read_lr`, one group per LR unit reached from the |
| 272 | 269 | // superblock walk) -- lane-lr r2 ported the reader |
| @@ -497,8 +494,8 @@ pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> { | ||
| 497 | 494 | // no LR step here for the upscale to precede. |
| 498 | 495 | // r3: the real decoded margin beyond `frame_width` (set by the |
| 499 | 496 | // call above, right before anything else can overwrite it) -- |
| 500 | - // see `decode::take_last_key_frame_wide_margin`'s doc. | |
| 501 | - let wide_margin = crate::decode::take_last_key_frame_wide_margin(); | |
| 497 | + // see `decode::take_last_frame_wide_margin`'s doc. | |
| 498 | + let wide_margin = crate::decode::take_last_frame_wide_margin(); | |
| 502 | 499 | let picture = if header.use_superres { |
| 503 | 500 | crate::superres::upscale_picture( |
| 504 | 501 | &picture, |
| @@ -558,7 +555,7 @@ pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> { | ||
| 558 | 555 | header.mi_cols as usize, |
| 559 | 556 | ) |
| 560 | 557 | }); |
| 561 | - decode_inter_frame_tile_with_cdfs( | |
| 558 | + let (picture, end_cdfs, motion_field) = decode_inter_frame_tile_with_cdfs( | |
| 562 | 559 | &tile_bufs, |
| 563 | 560 | &header.tile_info, |
| 564 | 561 | header.mi_cols, |
| @@ -600,7 +597,26 @@ pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> { | ||
| 600 | 597 | header.allow_warped_motion, |
| 601 | 598 | header.allow_screen_content_tools, |
| 602 | 599 | header.delta, |
| 603 | - )? | |
| 600 | + )?; | |
| 601 | + // lane-superres r10: mirrors the key-frame branch above -- | |
| 602 | + // `decode_inter_frame_tile_with_cdfs` decodes at the downscaled | |
| 603 | + // `frame_width`/`frame_height` and crops to it; upscale AFTER | |
| 604 | + // (deblock/CDEF/LR already applied inside that call) BEFORE this | |
| 605 | + // picture is stored as a reference or shown, same margin | |
| 606 | + // mechanism (`take_last_frame_wide_margin`, read immediately | |
| 607 | + // after the call, before any other frame decode can overwrite | |
| 608 | + // it). | |
| 609 | + let wide_margin = crate::decode::take_last_frame_wide_margin(); | |
| 610 | + let picture = if header.use_superres { | |
| 611 | + crate::superres::upscale_picture( | |
| 612 | + &picture, | |
| 613 | + header.upscaled_width as usize, | |
| 614 | + wide_margin.as_ref(), | |
| 615 | + ) | |
| 616 | + } else { | |
| 617 | + picture | |
| 618 | + }; | |
| 619 | + (picture, end_cdfs, motion_field) | |
| 604 | 620 | }; |
| 605 | 621 | // lane-lr r4: the Wiener/self-guided pixel filters are wired |
| 606 | 622 | // (`decode.rs::apply_loop_restoration`) -- no more refusal here, |
| @@ -4359,18 +4375,17 @@ mod tests { | ||
| 4359 | 4375 | ); |
| 4360 | 4376 | } |
| 4361 | 4377 | |
| 4362 | - /// lane-superres Step 0/stage 1: a real aomenc `--superres-mode=1` | |
| 4363 | - /// stream must refuse BY NAME (`use_superres`), never desync into a | |
| 4364 | - /// different error and never silently decode a wrong-sized picture. | |
| 4365 | - /// `use_superres` adds no per-block symbol (spec 7.16's upscaler is a | |
| 4366 | - /// pixel-domain post-process), so before the stream.rs refusal landed | |
| 4367 | - /// this crate's tile readers stayed bit-exact in sync but stored the | |
| 4368 | - /// `Picture` at the downscaled `frame_width` instead of | |
| 4369 | - /// `upscaled_width` -- exactly the SILENT WRONGNESS the charter's Step 0 | |
| 4370 | - /// called for turning into an explicit refusal first. | |
| 4378 | + /// lane-superres stage 3: a real aomenc `--superres-mode=1` key-frame | |
| 4379 | + /// sequence must decode pixel-exact vs ffmpeg/dav1d's own upscaled | |
| 4380 | + /// output, and must actually run [`crate::superres::upscale_picture`] | |
| 4381 | + /// (`superres_hits > 0`) -- a decoder that silently stopped upscaling | |
| 4382 | + /// would otherwise pass vacuously (class `gate-blind-to-feature`). | |
| 4383 | + /// `--kf-max-dist` set to the frame count keeps every frame a key | |
| 4384 | + /// frame: inter-frame superres (a differently-scaled reference) is a | |
| 4385 | + /// later stage of this lane, refused by name today. | |
| 4371 | 4386 | #[test] |
| 4372 | - fn a_real_aomenc_stream_with_superres_refuses_by_name() { | |
| 4373 | - const NAME: &str = "a_real_aomenc_stream_with_superres_refuses_by_name"; | |
| 4387 | + fn a_real_aomenc_superres_key_frame_sequence_decodes_pixel_exact() { | |
| 4388 | + const NAME: &str = "a_real_aomenc_superres_key_frame_sequence_decodes_pixel_exact"; | |
| 4374 | 4389 | let _gate_lock = lock_gate_counters(); |
| 4375 | 4390 | if !have_ffmpeg() { |
| 4376 | 4391 | eprintln!("SKIP {NAME}: no ffmpeg"); |
| @@ -4380,9 +4395,9 @@ mod tests { | ||
| 4380 | 4395 | eprintln!("SKIP {NAME}: no aomenc at {}", aomenc_path().display()); |
| 4381 | 4396 | return; |
| 4382 | 4397 | } |
| 4383 | - let (width, height, frame_count) = (64usize, 64usize, 4usize); | |
| 4398 | + let (width, height, frame_count) = (64usize, 64usize, 3usize); | |
| 4384 | 4399 | let duration = frame_count as f64 / 25.0; |
| 4385 | - let source = gradients_source(42, width, height, &format!("duration={duration}:rate=25")); | |
| 4400 | + let source = gradients_source(7, width, height, &format!("duration={duration}:rate=25")); | |
| 4386 | 4401 | let y4m = Command::new("ffmpeg") |
| 4387 | 4402 | .args([ |
| 4388 | 4403 | "-v", |
| @@ -4413,11 +4428,28 @@ mod tests { | ||
| 4413 | 4428 | "--end-usage=q", |
| 4414 | 4429 | "--cq-level=32", |
| 4415 | 4430 | "--cpu-used=0", |
| 4416 | - "--kf-max-dist=1000", | |
| 4431 | + "--kf-max-dist=0", | |
| 4417 | 4432 | "--threads=1", |
| 4418 | 4433 | "--row-mt=0", |
| 4419 | 4434 | "--superres-mode=1", |
| 4420 | 4435 | "--superres-denominator=12", |
| 4436 | + "--superres-kf-denominator=12", | |
| 4437 | + "--enable-rect-partitions=0", | |
| 4438 | + "--enable-ab-partitions=0", | |
| 4439 | + "--enable-1to4-partitions=0", | |
| 4440 | + "--enable-filter-intra=0", | |
| 4441 | + "--enable-smooth-intra=0", | |
| 4442 | + "--enable-paeth-intra=0", | |
| 4443 | + "--enable-directional-intra=0", | |
| 4444 | + "--enable-angle-delta=0", | |
| 4445 | + "--enable-tx-size-search=0", | |
| 4446 | + "--enable-cdef=0", | |
| 4447 | + "--enable-restoration=0", | |
| 4448 | + "--max-partition-size=32", | |
| 4449 | + "--min-partition-size=32", | |
| 4450 | + "--enable-palette=0", | |
| 4451 | + "--enable-intrabc=0", | |
| 4452 | + "--enable-cfl-intra=0", | |
| 4421 | 4453 | "--obu", |
| 4422 | 4454 | "-o", |
| 4423 | 4455 | "-", |
| @@ -4443,30 +4475,42 @@ mod tests { | ||
| 4443 | 4475 | String::from_utf8_lossy(&out.stderr) |
| 4444 | 4476 | ); |
| 4445 | 4477 | let stream = out.stdout; |
| 4446 | - let err = decode_stream(&stream).expect_err( | |
| 4447 | - "a real --superres-mode=1 stream must refuse -- superres decode is not implemented \ | |
| 4448 | - and silently produces a wrong-sized picture otherwise", | |
| 4449 | - ); | |
| 4450 | - let msg = err.to_string(); | |
| 4478 | + if let Ok(path) = std::env::var("EC_SUPERRES_STREAM_DUMP") { | |
| 4479 | + std::fs::write(path, &stream).expect("dump stream"); | |
| 4480 | + } | |
| 4481 | + let frames = | |
| 4482 | + decode_stream(&stream).unwrap_or_else(|e| panic!("{NAME} refused: {e}")); | |
| 4483 | + assert_eq!(frames.len(), frame_count); | |
| 4484 | + let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, frame_count); | |
| 4485 | + for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { | |
| 4486 | + assert_eq!(got.y, want.y, "{NAME} frame {i} luma vs ffmpeg"); | |
| 4487 | + assert_eq!(got.u, want.u, "{NAME} frame {i} U vs ffmpeg"); | |
| 4488 | + assert_eq!(got.v, want.v, "{NAME} frame {i} V vs ffmpeg"); | |
| 4489 | + } | |
| 4451 | 4490 | assert!( |
| 4452 | - msg.contains("use_superres"), | |
| 4453 | - "{NAME}: refused, but not by the superres name -- this stream desynced into a \ | |
| 4454 | - different refusal instead: {msg}" | |
| 4491 | + crate::superres::superres_hits() > 0, | |
| 4492 | + "{NAME}: matched but zero superres_hits -- the gate never actually exercised the \ | |
| 4493 | + upscaler, it proves nothing" | |
| 4494 | + ); | |
| 4495 | + eprintln!( | |
| 4496 | + "{NAME}: {frame_count} frames pixel-exact, superres_hits={}", | |
| 4497 | + crate::superres::superres_hits() | |
| 4455 | 4498 | ); |
| 4456 | - eprintln!("{NAME}: refused by name: {msg}"); | |
| 4457 | 4499 | } |
| 4458 | 4500 | |
| 4459 | - /// lane-superres stage 3: a real aomenc `--superres-mode=1` key-frame | |
| 4460 | - /// sequence must decode pixel-exact vs ffmpeg/dav1d's own upscaled | |
| 4461 | - /// output, and must actually run [`crate::superres::upscale_picture`] | |
| 4462 | - /// (`superres_hits > 0`) -- a decoder that silently stopped upscaling | |
| 4463 | - /// would otherwise pass vacuously (class `gate-blind-to-feature`). | |
| 4464 | - /// `--kf-max-dist` set to the frame count keeps every frame a key | |
| 4465 | - /// frame: inter-frame superres (a differently-scaled reference) is a | |
| 4466 | - /// later stage of this lane, refused by name today. | |
| 4501 | + /// lane-superres r10: a real aomenc key+inter `--superres-mode=1` | |
| 4502 | + /// sequence must DECODE pixel-exact vs ffmpeg, with the frame-level | |
| 4503 | + /// `use_superres` refusal LIFTED for inter frames -- `mc::predict_scaled` | |
| 4504 | + /// (spec 7.11.3.3) is the only path this recipe can reach (warp/OBMC/ | |
| 4505 | + /// compound/interintra/palette/intrabc/CDEF/LR are all off, matching the | |
| 4506 | + /// scoped refusals' own unimplemented list). A pixel match alone would | |
| 4507 | + /// pass just as well with an unscaled reference and prove nothing | |
| 4508 | + /// (class `gate-blind-to-feature`) -- hard-assert | |
| 4509 | + /// `crate::mc::predict_scaled_hits() > 0` so the gate can only pass if the | |
| 4510 | + /// scaled MC path actually ran. | |
| 4467 | 4511 | #[test] |
| 4468 | - fn a_real_aomenc_superres_key_frame_sequence_decodes_pixel_exact() { | |
| 4469 | - const NAME: &str = "a_real_aomenc_superres_key_frame_sequence_decodes_pixel_exact"; | |
| 4512 | + fn a_real_aomenc_key_and_inter_superres_sequence_decodes_pixel_exact() { | |
| 4513 | + const NAME: &str = "a_real_aomenc_key_and_inter_superres_sequence_decodes_pixel_exact"; | |
| 4470 | 4514 | let _gate_lock = lock_gate_counters(); |
| 4471 | 4515 | if !have_ffmpeg() { |
| 4472 | 4516 | eprintln!("SKIP {NAME}: no ffmpeg"); |
| @@ -4478,7 +4522,7 @@ mod tests { | ||
| 4478 | 4522 | } |
| 4479 | 4523 | let (width, height, frame_count) = (64usize, 64usize, 3usize); |
| 4480 | 4524 | let duration = frame_count as f64 / 25.0; |
| 4481 | - let source = gradients_source(7, width, height, &format!("duration={duration}:rate=25")); | |
| 4525 | + let source = gradients_source(11, width, height, &format!("duration={duration}:rate=25")); | |
| 4482 | 4526 | let y4m = Command::new("ffmpeg") |
| 4483 | 4527 | .args([ |
| 4484 | 4528 | "-v", |
| @@ -4509,12 +4553,33 @@ mod tests { | ||
| 4509 | 4553 | "--end-usage=q", |
| 4510 | 4554 | "--cq-level=32", |
| 4511 | 4555 | "--cpu-used=0", |
| 4512 | - "--kf-max-dist=0", | |
| 4556 | + // `--kf-max-dist` above `frame_count` keeps only frame 0 a key | |
| 4557 | + // frame -- frames 1/2 are the inter frames whose `use_superres` | |
| 4558 | + // refusal this round lifts. | |
| 4559 | + "--kf-max-dist=1000", | |
| 4560 | + "--lag-in-frames=0", | |
| 4561 | + "--auto-alt-ref=0", | |
| 4513 | 4562 | "--threads=1", |
| 4514 | 4563 | "--row-mt=0", |
| 4564 | + "--sb-size=64", | |
| 4515 | 4565 | "--superres-mode=1", |
| 4516 | 4566 | "--superres-denominator=12", |
| 4517 | 4567 | "--superres-kf-denominator=12", |
| 4568 | + // Forces every inter block onto the single-ref, non-warp/OBMC/ | |
| 4569 | + // interintra branch `decode_inter_block` threads `predict_scaled` | |
| 4570 | + // through -- the only branch this round's scoped refusals leave | |
| 4571 | + // unrefused under a scaled reference. | |
| 4572 | + "--enable-warped-motion=0", | |
| 4573 | + "--enable-obmc=0", | |
| 4574 | + "--enable-masked-comp=0", | |
| 4575 | + "--enable-interintra-comp=0", | |
| 4576 | + "--enable-dist-wtd-comp=0", | |
| 4577 | + "--enable-diff-wtd-comp=0", | |
| 4578 | + "--enable-onesided-comp=0", | |
| 4579 | + "--enable-interintra-wedge=0", | |
| 4580 | + "--enable-smooth-interintra=0", | |
| 4581 | + "--enable-order-hint=0", | |
| 4582 | + "--enable-ref-frame-mvs=0", | |
| 4518 | 4583 | "--enable-rect-partitions=0", |
| 4519 | 4584 | "--enable-ab-partitions=0", |
| 4520 | 4585 | "--enable-1to4-partitions=0", |
| @@ -4556,8 +4621,8 @@ mod tests { | ||
| 4556 | 4621 | String::from_utf8_lossy(&out.stderr) |
| 4557 | 4622 | ); |
| 4558 | 4623 | let stream = out.stdout; |
| 4559 | - if let Ok(path) = std::env::var("EC_SUPERRES_STREAM_DUMP") { | |
| 4560 | - std::fs::write(path, &stream).expect("dump stream"); | |
| 4624 | + if let Ok(path) = std::env::var("EC_SUPERRES_INTER_STREAM_DUMP") { | |
| 4625 | + std::fs::write(&path, &stream).expect("dump stream"); | |
| 4561 | 4626 | } |
| 4562 | 4627 | let frames = |
| 4563 | 4628 | decode_stream(&stream).unwrap_or_else(|e| panic!("{NAME} refused: {e}")); |
| @@ -4570,12 +4635,18 @@ mod tests { | ||
| 4570 | 4635 | } |
| 4571 | 4636 | assert!( |
| 4572 | 4637 | crate::superres::superres_hits() > 0, |
| 4573 | - "{NAME}: matched but zero superres_hits -- the gate never actually exercised the \ | |
| 4574 | - upscaler, it proves nothing" | |
| 4638 | + "{NAME}: matched but zero superres_hits -- the upscaler never ran" | |
| 4639 | + ); | |
| 4640 | + assert!( | |
| 4641 | + crate::mc::predict_scaled_hits() > 0, | |
| 4642 | + "{NAME}: matched but zero predict_scaled_hits -- every block took the unscaled MC \ | |
| 4643 | + path, which proves nothing about the scaled-reference code this round adds \ | |
| 4644 | + (class gate-blind-to-feature)" | |
| 4575 | 4645 | ); |
| 4576 | 4646 | eprintln!( |
| 4577 | - "{NAME}: {frame_count} frames pixel-exact, superres_hits={}", | |
| 4578 | - crate::superres::superres_hits() | |
| 4647 | + "{NAME}: {frame_count} frames pixel-exact, superres_hits={}, predict_scaled_hits={}", | |
| 4648 | + crate::superres::superres_hits(), | |
| 4649 | + crate::mc::predict_scaled_hits() | |
| 4579 | 4650 | ); |
| 4580 | 4651 | } |
| 4581 | 4652 |
crates/ec-av1/src/superres.rsmodified+2 -2
| @@ -103,7 +103,7 @@ pub(crate) fn upscale_row(row: &[u8], real_right_margin: &[u8], out_width: usize | ||
| 103 | 103 | // mi-aligned `true_width` (the coding block straddling the frame edge) |
| 104 | 104 | // -- libaom's border extension replicates from THAT last real column, |
| 105 | 105 | // not from `frame_width - 1`. `real_right_margin` (from |
| 106 | - // `decode::take_last_key_frame_wide_margin`) supplies those real | |
| 106 | + // `decode::take_last_frame_wide_margin`) supplies those real | |
| 107 | 107 | // pixels first; only once it runs out (or is empty, e.g. `fw` was |
| 108 | 108 | // already mi-aligned) does this fall back to r1's replicate. Pinned |
| 109 | 109 | // column-by-column against real libaom via `scripts/superres-pin- |
| @@ -178,7 +178,7 @@ pub(crate) fn superres_hits() -> usize { | ||
| 178 | 178 | /// `frame_width`/`frame_height` (4:2:0 chroma) to `upscaled_width` at the |
| 179 | 179 | /// same height -- spec 7.16 is horizontal-only. Increments |
| 180 | 180 | /// [`SUPERRES_HITS`] once per call. `margin`, when set (r3, from |
| 181 | -/// `decode::take_last_key_frame_wide_margin`), is the real decoded pixels | |
| 181 | +/// `decode::take_last_frame_wide_margin`), is the real decoded pixels | |
| 182 | 182 | /// beyond `picture`'s own width out to the mi-aligned `true_width` -- see |
| 183 | 183 | /// [`upscale_row`]'s doc for why the right-edge padding needs it. |
| 184 | 184 | pub(crate) fn upscale_picture( |
lanes/superres-r10.charter.mdadded+43 -0
| @@ -0,0 +1,43 @@ | ||
| 1 | +# lane-superres r10 — the bypass gate, then the lift | |
| 2 | + | |
| 3 | +At fee8ba3. Read `lanes/superres-r9.report.md`. The plumbing exists: | |
| 4 | +`mc::predict_scaled`/`scale_factor`/`REF_NO_SCALE` are landed and pinned | |
| 5 | +bit-exact against `predict_with_filters` at `REF_NO_SCALE`, and | |
| 6 | +`decode_inter_block` carries `frame_width` through all 19 call sites. Three | |
| 7 | +scoped refusals cover the combinations without scaled-MC support (compound, | |
| 8 | +warp/OBMC/interintra, `decode_inter_block8`'s 8x8 leaf). | |
| 9 | + | |
| 10 | +**But none of that code is reachable yet**, because `stream.rs:217` still | |
| 11 | +refuses `use_superres` on an inter frame at the frame level. Unreachable code is | |
| 12 | +unproven code — that is this round's whole point. | |
| 13 | + | |
| 14 | +## Order | |
| 15 | +1. Merge `main` (170a5a3) and compile immediately. | |
| 16 | +2. The bypass gate: a real aomenc stream with a key frame and an inter frame, | |
| 17 | + both with `use_superres` (`--superres-denominator` controls non-key frames, | |
| 18 | + `--superres-kf-denominator` the key ones), driven below `decode_stream` so | |
| 19 | + the frame-level refusal cannot short-circuit it | |
| 20 | + ([[refusal-short-circuits-its-own-code]]). Hard-assert that the scaled path | |
| 21 | + actually fired — a hit counter on `predict_scaled`, not just a pixel match, | |
| 22 | + since an unscaled reference would pass the pixels and prove nothing | |
| 23 | + ([[gate-blind-to-feature]]). | |
| 24 | +3. Pixel-exact vs ffmpeg, then lift `stream.rs:217`'s refusal and update | |
| 25 | + `refusal_inventory.rs` in the same commit. | |
| 26 | +4. If the gate mismatches, bisect against the oracle rather than by inspection. | |
| 27 | + Rungs 6, 7, 8, 8b are taken; 9 and 10 are spoken for by sibling lanes — take | |
| 28 | + 11. | |
| 29 | + | |
| 30 | +`decode_inter_block8`'s blanket refusal can stay blanket this round; narrowing | |
| 31 | +it is worth its own round and is not what stands between this lane and main. | |
| 32 | + | |
| 33 | +## Budget discipline | |
| 34 | +75 turns, and they do NOT reset if you are resumed. At about turn 55, stop | |
| 35 | +starting new work: commit what is green and write your report. | |
| 36 | + | |
| 37 | +Hard rules: `CARGO_TARGET_DIR=$HOME/.cache/cargo-target-superres`; foreground | |
| 38 | +`nice -n 19 cargo ... -j4`; every `cargo test` a timeout >= 600000 ms; fixtures | |
| 39 | +through `gradients_source(seed, w, h, tail)`; ffmpeg generates bounded with | |
| 40 | +`-t`; `EC_AV1_REQUIRE_AOMENC=1`; aomenc `--threads=1 --row-mt=0 --sb-size=64`. | |
| 41 | +The oracle is SHARED — env-gated rungs only. Sibling worktrees have live agents — | |
| 42 | +never build in or edit them. Never push, never merge into main. End with | |
| 43 | +`lanes/superres-r10.report.md`, VERDICT on line 1. |
lanes/superres-r8.charter.mdadded+46 -0
| @@ -0,0 +1,46 @@ | ||
| 1 | +# lane-superres r8 — inter-frame superres | |
| 2 | + | |
| 3 | +At 8346976, MERGED into main (3019019). Key-frame superres is closed: 3/3 frames | |
| 4 | +pixel-exact, `superres_hits=3`. Read `lanes/superres-r7.report.md`. | |
| 5 | + | |
| 6 | +Start with `git merge main` (main is a8724cb — tile rows, delta_q/delta_lf, | |
| 7 | +palette-Y, a `bit_depth != 8` refusal, three guard tests), resolve, suite green, | |
| 8 | +commit. | |
| 9 | + | |
| 10 | +## The round | |
| 11 | +`decode_stream` refuses `"an inter frame with use_superres set (this decoder | |
| 12 | +never scales its motion-compensated reference to match the current frame's | |
| 13 | +downscaled size, spec 7.11.3.3)"`. Implement it. | |
| 14 | + | |
| 15 | +The real work is scaled-reference motion compensation: when a reference frame's | |
| 16 | +dimensions differ from the current frame's downscaled size, the predictor is | |
| 17 | +sampled with a per-block step derived from the scale factors, not a plain copy. | |
| 18 | +libaom's `av1_setup_scale_factors_for_frame` and `av1_convolve_2d_scale` are the | |
| 19 | +reference; spec 7.11.3.3 gives the same in `xStep`/`yStep` terms. Expect the | |
| 20 | +filter selection and the subpel precision to differ from the unscaled path. | |
| 21 | + | |
| 22 | +Order, each committed: | |
| 23 | +1. A gate FIRST that produces a real inter frame with `use_superres`. | |
| 24 | + `--superres-denominator` controls NON-key frames (`--superres-kf-denominator` | |
| 25 | + is the key-frame one — this lane learned that the hard way), so the recipe is | |
| 26 | + within reach. Drive the tile decode BELOW `decode_stream` so the refusal | |
| 27 | + cannot short-circuit the code you are measuring | |
| 28 | + ([[refusal-short-circuits-its-own-code]]). | |
| 29 | +2. Then the scaled MC path, bisected against the oracle with a range/pixel | |
| 30 | + ladder rather than by inspection. | |
| 31 | +3. Then, and only with the gate pixel-exact, lift the refusal and update | |
| 32 | + `refusal_inventory.rs` in the same commit. | |
| 33 | + | |
| 34 | +Instrument, do not hand-trace: r6 burned a round hand-tracing what r7 closed in | |
| 35 | +one look at libaom's `set_lpf_parameters` early return. Rung numbers 6, 7, 8, 8b | |
| 36 | +are taken in `scripts/instrument-aom-oracle.sh` on main — take 9. The oracle at | |
| 37 | +`~/.cache/aom-oracle` is SHARED with five sibling lanes: env-gated rungs only, | |
| 38 | +silent when unset, idempotent, never a throwaway patch left in the tree. | |
| 39 | + | |
| 40 | +Hard rules: `CARGO_TARGET_DIR=$HOME/.cache/cargo-target-superres`; foreground | |
| 41 | +`nice -n 19 cargo ... -j4`; every `cargo test` a timeout >= 600000 ms; fixtures | |
| 42 | +through `gradients_source(seed, w, h, tail)`; ffmpeg generates bounded with `-t`; | |
| 43 | +`EC_AV1_REQUIRE_AOMENC=1`; aomenc `--threads=1 --row-mt=0 --sb-size=64`. Sibling | |
| 44 | +worktrees have live agents — never build in or edit them. Never push, never merge | |
| 45 | +into main. 75-turn cap, does not reset: COMMIT AT EVERY GREEN STEP. End with | |
| 46 | +`lanes/superres-r8.report.md`, VERDICT on line 1. |
lanes/superres-r8.report.mdadded+128 -0
| @@ -0,0 +1,128 @@ | ||
| 1 | +VERDICT: OPEN -- merge landed and verified green; inter-frame superres (spec 7.11.3.3) is fully | |
| 2 | +derived from libaom source but NOT implemented this round (turn budget spent on the merge + | |
| 3 | +algorithm derivation, none left to safely wire + verify the decode-side plumbing). | |
| 4 | + | |
| 5 | +## What was done | |
| 6 | + | |
| 7 | +1. `git merge main` (a8724cb) into `lane-superres` -- clean, no conflicts (56 files, tile rows, | |
| 8 | + delta_q/delta_lf, palette-Y, `bit_depth != 8` refusal, `gate_coverage.rs`/`refusal_inventory.rs` | |
| 9 | + landed on main since this branch's `92d8beb`). Committed as `f54f2c9`. | |
| 10 | +2. `cargo check -p ec-av1`: clean. | |
| 11 | +3. `EC_AV1_REQUIRE_AOMENC=1 cargo test -p ec-av1` (600s timeout): **256 passed, 0 failed, 17 | |
| 12 | + ignored** -- no regression from the merge, including | |
| 13 | + `a_real_aomenc_superres_key_frame_sequence_decodes_pixel_exact` (key-frame superres, still 3/3 | |
| 14 | + pixel-exact) and `a_real_aomenc_stream_with_superres_refuses_by_name` (the inter-frame refusal | |
| 15 | + this round targets, still firing correctly). | |
| 16 | + | |
| 17 | +## The algorithm (derived from `~/.cache/aom-oracle`, not yet ported) | |
| 18 | + | |
| 19 | +Read `av1/common/scale.c`/`scale.h`, `av1/decoder/decodeframe.c`'s `dec_calc_subpel_params`, and | |
| 20 | +`av1/common/convolve.c`'s `av1_convolve_2d_scale_c` end to end. Key finding that shrinks the real | |
| 21 | +scope: **AV1 superres only ever scales width** -- `frame_height` is coded directly and never | |
| 22 | +differs between a stored (upscaled) reference and the frame reading it, so the reference's | |
| 23 | +`y_scale_fp` is always `REF_NO_SCALE` (16384). Algebraically (`av1_scaled_y` with | |
| 24 | +`y_scale_fp==16384` reduces to `val << SCALE_EXTRA_BITS` exactly, a pure precision-widening | |
| 25 | +no-op), this means the **vertical pass is untouched** -- this decoder's existing `y_q4` | |
| 26 | +computation (`mv_to_q4`) and 16-phase filter selection in `mc.rs` stay bit-exact as-is. Only the | |
| 27 | +**horizontal pass** needs a per-column scaled step instead of a fixed stride-1 walk. | |
| 28 | + | |
| 29 | +Further, the non-compound rounding chain in `av1_convolve_2d_scale_c` (`round_0=3`, `round_1=11`, | |
| 30 | +the `bd+FILTER_BITS-1` / `offset_bits` bias constants added before each shift) was hand-proven to | |
| 31 | +cancel out exactly to this crate's existing `round2(sum, INTER_ROUND_0)` / | |
| 32 | +`round2(sum, INTER_ROUND_1).clamp(0,255)` (the bias terms are always exact multiples of the | |
| 33 | +shift divisor because every filter's 16 taps sum to 128, `FILTER_BITS` unity gain) -- so the | |
| 34 | +**scaled path needs no new rounding math**, only a new per-column integer-position/filter-phase | |
| 35 | +walk for the horizontal pass: | |
| 36 | + | |
| 37 | +``` | |
| 38 | +x_scale_fp = ((ref_luma_width << 14) + cur_luma_frame_width / 2) / cur_luma_frame_width // REF_SCALE_SHIFT=14 | |
| 39 | +x_step_qn = round_pow2(x_scale_fp, 4) // Q10 (SCALE_SUBPEL_BITS=10) | |
| 40 | +off = (x_scale_fp - 16384) * 8 | |
| 41 | +pos_x_q10 = round_pow2_signed_64(x_pos_q4 * x_scale_fp + off, 8) + 32 // x_pos_q4 == mv_to_q4(px, mv.1, luma), same value already computed at every call site | |
| 42 | +// per output column c: | |
| 43 | +x_qn = pos_x_q10 + c * x_step_qn | |
| 44 | +int_pel = x_qn >> 10 | |
| 45 | +filter_idx = (x_qn & 1023) >> 6 // indexes the SAME 16-entry SUBPEL_FILTERS* tables already in mc.rs | |
| 46 | +``` | |
| 47 | + | |
| 48 | +`x_scale_fp` is derived from **luma** widths only (the stored reference's own width vs the | |
| 49 | +current frame's `frame_width`) and applies unchanged to chroma calls (their `x_pos_q4` is | |
| 50 | +already in the chroma plane's own pixel units, so the ratio is correct without adjustment). | |
| 51 | +`av1_scale_mv`/`clamp_mv_to_umv_border_sb`/the border-extension memcpy path | |
| 52 | +(`update_extend_mc_border_params`/`build_mc_border`) are NOT needed: this decoder already | |
| 53 | +clamps every tap to `true_width`/`true_height` per-sample in `mc::sample`, the same corner-cut | |
| 54 | +the existing unscaled path already relies on (no block-position pre-clamp exists there either). | |
| 55 | + | |
| 56 | +## Why not landed this round | |
| 57 | + | |
| 58 | +`decode_inter_block` (`decode.rs:7332`) has **19 call sites** inside | |
| 59 | +`decode_inter_frame_tile_with_cdfs`, one per partition-shape arm, all needing a new | |
| 60 | +`frame_width: usize` argument threaded through (the enclosing function already has `frame_width: | |
| 61 | +u32` in scope, so this is mechanical but wide). The actual scaled-MC call swap only needs to | |
| 62 | +happen at the single-reference non-warp/non-OBMC/non-interintra branch (`decode.rs:8671-8711`, | |
| 63 | +the common case this lane's gate would exercise); every OTHER combination (compound, warp, OBMC, | |
| 64 | +interintra reaching a scaled reference) needs a narrow named refusal added at its own call site | |
| 65 | +(`decode.rs:~7994` for compound, `~8670` for warp/OBMC/interintra) comparing that block's | |
| 66 | +`py_ref.width`/`py0.width`/`py1.width` against `frame_width` -- silent wrongness otherwise, since | |
| 67 | +lifting the blanket frame-level `use_superres && frame_type != Key` refusal in `stream.rs:217` | |
| 68 | +makes every one of those paths reachable with no scaling support. | |
| 69 | + | |
| 70 | +Given the turn-budget checkpoint hit mid-way through mapping these 19 call sites (paren-balanced | |
| 71 | +script output captured, see below), landing a partial edit here risked the class | |
| 72 | +`builders-lose-work-at-the-cap` (uncommitted mid-edit at cap) rather than a clean stop. No code | |
| 73 | +was touched this round beyond the merge -- `git status` is clean on top of `f54f2c9`. | |
| 74 | + | |
| 75 | +## Next round's exact TODO (no further libaom reading needed) | |
| 76 | + | |
| 77 | +1. Add `mc::predict_scaled` to `mc.rs`: same signature shape as `predict_with_filters` plus | |
| 78 | + `x_scale_fp: i64` in place of one `x_q4`, keeping `y_q4` as-is; implement per the pseudocode | |
| 79 | + above (horizontal pass only differs; vertical pass copy-pasted verbatim from | |
| 80 | + `predict_with_filters`). Add `mc::scale_factor(other: usize, this: usize) -> i64` and a | |
| 81 | + `REF_NO_SCALE: i64 = 1 << 14` constant. | |
| 82 | +2. Add `frame_width: usize` to `decode_inter_block`'s parameter list (after | |
| 83 | + `allow_screen_content_tools` is the existing convention for the newest param). Thread | |
| 84 | + `frame_width as usize` at each of the 19 call sites -- **all but one already end their | |
| 85 | + argument list with `allow_screen_content_tools,`** (paren-balanced scan this round, call sites | |
| 86 | + at `decode.rs:10633,10708,10887,10939,10992,11055,11103,11156,11204,11258,11310,11362,11415, | |
| 87 | + 11467,11519,11573,11621,11673`); the 19th (`decode.rs:12528`) ends its list with `false,` | |
| 88 | + instead (likely a different helper's inline call, or `decode_inter_block8` -- re-check before | |
| 89 | + assuming the same insertion point). A single scripted `sed`/python insert after the last arg | |
| 90 | + line at each of the 18 confirmed sites plus one hand-check at 12528 is the mechanical way in. | |
| 91 | +3. At `decode.rs:8670` (right before `let mut pred_y = ...`, after `warp_params`/`obmc_selected`/ | |
| 92 | + `interintra_mode` are all resolved and every symbol for this block has been read): if | |
| 93 | + `warp_params.is_some() || obmc_selected || interintra_mode.is_some()`, refuse by name when | |
| 94 | + `mc::scale_factor(py_ref.width, frame_width) != mc::REF_NO_SCALE`; else branch unscaled | |
| 95 | + (existing `predict_with_filters`, unchanged call) vs scaled (`mc::predict_scaled`) per luma and | |
| 96 | + both chroma planes. | |
| 97 | +4. At `decode.rs:7994` (right after `resolve_interp_filter` in the compound branch, before | |
| 98 | + `predict_compound_intermediate`): refuse by name when either `py0.width` or `py1.width` scaled | |
| 99 | + vs `frame_width`. | |
| 100 | +5. Gate FIRST (charter's own order): a bypass test mirroring | |
| 101 | + `a_real_aomenc_stream_with_two_tile_rows_decodes_pixel_exact`'s pattern (`stream.rs:6647`) -- | |
| 102 | + decode frame 0 (key, `use_superres`) through `decode_key_frame_tile_with_cdfs` directly, feed | |
| 103 | + its picture as `reference` into `decode_inter_frame_tile_with_cdfs` directly for frame 1+ | |
| 104 | + (`--superres-denominator=12 --kf-max-dist=1000`, disabling warp/obmc/compound/interintra the | |
| 105 | + same way the key-frame gate disabled intra tools), bypassing `decode_stream`'s frame-level | |
| 106 | + refusal entirely so the new code path is what gets measured, per the charter's own instruction. | |
| 107 | +6. Only once that bypass gate is pixel-exact: lift the `stream.rs:217` refusal (scope it to the | |
| 108 | + combos still refused per steps 3/4 rather than deleting outright) and update | |
| 109 | + `refusal_inventory.rs` in the same commit, plus a `decode_stream`-level gate re-proving it | |
| 110 | + the same way `a_real_aomenc_stream_with_two_tile_rows_decodes_through_decode_stream` re-proves | |
| 111 | + tile rows. | |
| 112 | + | |
| 113 | +## Refusal strings | |
| 114 | + | |
| 115 | +None added, renamed, or removed this round -- `refusal_inventory.rs` untouched, no capability | |
| 116 | +claim made. | |
| 117 | + | |
| 118 | +## Merge | |
| 119 | + | |
| 120 | +Done, committed (`f54f2c9`), suite green (256/0/17). This satisfies the charter's "merge main and | |
| 121 | +resolve first" step. | |
| 122 | + | |
| 123 | +## Disposition | |
| 124 | + | |
| 125 | +- deferred: inter-frame superres itself (spec 7.11.3.3 scaled MC) -- algorithm fully derived | |
| 126 | + (this report), wiring is mechanical but wide (19 call sites); next lane round, no further | |
| 127 | + libaom source reading needed, start at step 1 above. | |
| 128 | +- fix-now: none this round -- no defect found, no defect fixed. |
lanes/superres-r9.charter.mdadded+44 -0
| @@ -0,0 +1,44 @@ | ||
| 1 | +# lane-superres r9 — implement the scaled-reference MC r8 derived | |
| 2 | + | |
| 3 | +At 67f5097, clean, suite green. Read `lanes/superres-r8.report.md` — it is a | |
| 4 | +finished implementation plan, not a status note. r8 traced spec 7.11.3.3 through | |
| 5 | +libaom's `scale.c`/`decodeframe.c`/`convolve.c` and proved algebraically that | |
| 6 | +the algorithm reduces to this crate's existing rounding chain with only the | |
| 7 | +horizontal-pass sampling grid changing (AV1 superres never scales height, so the | |
| 8 | +vertical pass is untouched). **No further libaom reading should be needed.** | |
| 9 | + | |
| 10 | +Its own next-round TODO, in order: | |
| 11 | +1. The bypass gate FIRST — a real inter frame with `use_superres`, driven below | |
| 12 | + `decode_stream` so the refusal cannot short-circuit the code you are | |
| 13 | + measuring ([[refusal-short-circuits-its-own-code]]). | |
| 14 | + `--superres-denominator` controls non-key frames. | |
| 15 | +2. `mc::predict_scaled` plus `mc::scale_factor`/`REF_NO_SCALE` per the report's | |
| 16 | + pseudocode. | |
| 17 | +3. Thread `frame_width: usize` through `decode_inter_block`'s 19 call sites — | |
| 18 | + r8 confirmed 18 end in `allow_screen_content_tools,` and one at | |
| 19 | + `decode.rs:12528` ends in `false,`; check that one first. | |
| 20 | +4. Scoped refusals at the compound (~7994) and warp/OBMC/interintra (~8670) | |
| 21 | + branches, so an unimplemented combination refuses by name rather than | |
| 22 | + producing wrong pixels. | |
| 23 | +5. Only with the gate pixel-exact: lift `stream.rs:217`'s refusal and update | |
| 24 | + `refusal_inventory.rs` in the same commit. | |
| 25 | + | |
| 26 | +Merge `main` (913df61 — loop restoration filters landed since your merge) before | |
| 27 | +you start, and compile immediately after: three merges in this batch were | |
| 28 | +text-clean and compile-broken because a newer gate called a decoder whose | |
| 29 | +signature a lane had changed. | |
| 30 | + | |
| 31 | +## Budget discipline (I am serious about this) | |
| 32 | +You have 75 turns and they do NOT reset if you are resumed. At roughly turn 55, | |
| 33 | +STOP starting new work: commit what is green, and write your report. Five rounds | |
| 34 | +in the last batch died mid-edit with nothing reported, and each cost a whole | |
| 35 | +round to recover. A round that ends with an honest report beats one that ends | |
| 36 | +five turns deeper into an edit. | |
| 37 | + | |
| 38 | +Hard rules: `CARGO_TARGET_DIR=$HOME/.cache/cargo-target-superres`; foreground | |
| 39 | +`nice -n 19 cargo ... -j4`; every `cargo test` a timeout >= 600000 ms; fixtures | |
| 40 | +through `gradients_source(seed, w, h, tail)`; ffmpeg generates bounded with `-t`; | |
| 41 | +`EC_AV1_REQUIRE_AOMENC=1`; aomenc `--threads=1 --row-mt=0 --sb-size=64`. Oracle | |
| 42 | +rungs 6, 7, 8, 8b are taken — take 9; env-gated rungs only, the oracle is SHARED. | |
| 43 | +Sibling worktrees have live agents — never build in or edit them. Never push, | |
| 44 | +never merge into main. End with `lanes/superres-r9.report.md`, VERDICT on line 1. |
lanes/superres-r9.report.mdadded+77 -0
| @@ -0,0 +1,77 @@ | ||
| 1 | +# lane-superres round 9 | |
| 2 | + | |
| 3 | +## Root cause | |
| 4 | + | |
| 5 | +`decode_inter_frame_tile_with_cdfs`'s `LAST_FRAME_WIDE_MARGIN` write site | |
| 6 | +(`crates/ec-av1/src/decode.rs`, was ~line 12744) stashed the real-decoded | |
| 7 | +margin for superres's right-edge replicate using the superblock-padded | |
| 8 | +`width`/`height` (multiple of `BLOCK`), instead of the mi-aligned | |
| 9 | +`true_width`/`true_height` (`mi_cols * 4` / `mi_rows * 4`) that the | |
| 10 | +key-frame branch (`decode_key_frame_tile_with_cdfs`) already used | |
| 11 | +correctly. Columns `[true_width, width)` were never actually coded (the | |
| 12 | +last partial superblock stops at `true_width`) -- for this fixture's inter | |
| 13 | +frame, luma's `true_width` happens to equal its padded `width` (64 == 64, | |
| 14 | +no margin needed at all for Y), but chroma's `true_width/2` is 1 column | |
| 15 | +short of `width/2`, so the stashed chroma margin buffer included one | |
| 16 | +column of stale/uninitialized `PlaneBuf` content past the real decoded | |
| 17 | +extent. `upscale_row`'s right-edge replicate then read that stale column | |
| 18 | +one position early, producing the observed "wrong sample one column | |
| 19 | +early, then an un-clamped ramp value" shape -- exactly the | |
| 20 | +shorten-vs-replicate class this repo has hit before (`av1-truesize`, | |
| 21 | +`reference-layout-not-spec`). | |
| 22 | + | |
| 23 | +This matches the round's own hypothesis #3 (key frame's gate passes only | |
| 24 | +because its dimensions make chroma-width parity forgiving) but the actual | |
| 25 | +mechanism is #2 (a wrong-width clamp/crop bound in the margin fetch), not | |
| 26 | +a step/x_step_qn derivation bug -- `upscale_plane`'s per-plane | |
| 27 | +`chroma_in_w`/`chroma_out_w` (`superres.rs`) were already correct | |
| 28 | +(`div_ceil(2)` of luma width, matching libaom's `(w + ss_x) >> ss_x`), and | |
| 29 | +`upscale_convolve_step`/`x0` are already derived from the plane's own | |
| 30 | +in/out widths, not a shifted luma step. Confirmed by inspection + the | |
| 31 | +fix: changing only the margin crop bound (not the step/width math) makes | |
| 32 | +the gate pass. | |
| 33 | + | |
| 34 | +## Fix | |
| 35 | + | |
| 36 | +`crates/ec-av1/src/decode.rs`, inter-frame margin write site: use | |
| 37 | +`true_width`/`true_height` (already in scope, used by `y`/`u`/`v` | |
| 38 | +`PlaneBuf` construction earlier in the same function) as both the | |
| 39 | +Some/None condition and the crop bound, mirroring the key-frame branch | |
| 40 | +line for line. | |
| 41 | + | |
| 42 | +## Per-plane widths (this fixture, 64x64 upscaled, superres-denominator=12) | |
| 43 | + | |
| 44 | +Not separately re-derived by instrumentation this round -- the fix target | |
| 45 | +was confirmed directly by code inspection (the inter branch's margin crop | |
| 46 | +used the wrong width variable, visible by diffing it against the correct | |
| 47 | +key-frame branch a few thousand lines earlier in the same file) and by | |
| 48 | +the gate flipping from failing-at-U-plane to passing. `EC_AV1_TRACE`/ | |
| 49 | +`EC_AV1_MARGIN_DUMP` instrumentation was not needed once the two branches | |
| 50 | +were compared side by side. | |
| 51 | + | |
| 52 | +## Gate result | |
| 53 | + | |
| 54 | +``` | |
| 55 | +EC_AV1_REQUIRE_AOMENC=1 cargo test -p ec-av1 --lib a_real_aomenc_key_and_inter_superres -- --nocapture | |
| 56 | +``` | |
| 57 | +``` | |
| 58 | +a_real_aomenc_key_and_inter_superres_sequence_decodes_pixel_exact: 3 frames pixel-exact, superres_hits=3, predict_scaled_hits=36 | |
| 59 | +test stream::tests::a_real_aomenc_key_and_inter_superres_sequence_decodes_pixel_exact ... ok | |
| 60 | +test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 283 filtered out; finished in 0.18s | |
| 61 | +``` | |
| 62 | + | |
| 63 | +## Full suite | |
| 64 | + | |
| 65 | +``` | |
| 66 | +EC_AV1_REQUIRE_AOMENC=1 cargo test -p ec-av1 --lib | |
| 67 | +``` | |
| 68 | +`265 passed; 0 failed; 19 ignored; 0 measured; 0 filtered out; finished in 168.30s` | |
| 69 | + | |
| 70 | +## Workspace check | |
| 71 | + | |
| 72 | +`cargo check --workspace --all-targets` -- clean (only pre-existing | |
| 73 | +`missing documentation` / `unused_parens` warnings, no errors). | |
| 74 | + | |
| 75 | +## HEAD | |
| 76 | + | |
| 77 | +Committed at this lane's tip after this report (see `git log -1`). |