Merge branch 'lane-t900' (b57adfd8): superres upscale moved between CDEF and loop restoration (spec 7.16); LR grid, unit corners and stripe boundaries in upscaled coordinates; superres key frame with CDEF+LR gated byte-exact
Browse files at this commit6 changed files +280 -114
- crates/ec-av1/fixtures/superres_alltools_sb128_320x180.obu +0 -0
- crates/ec-av1/fixtures/superres_kf_cdef_lr_64x64.obu +0 -0
- crates/ec-av1/src/decode.rs +137 -6
- crates/ec-av1/src/restoration.rs +11 -2
- crates/ec-av1/src/stream.rs +109 -38
- crates/ec-av1/src/superres.rs +23 -68
crates/ec-av1/fixtures/superres_alltools_sb128_320x180.obuadded+0 -0
Binary file changed
crates/ec-av1/fixtures/superres_kf_cdef_lr_64x64.obuadded+0 -0
Binary file changed
crates/ec-av1/src/decode.rsmodified+137 -6
| @@ -275,6 +275,33 @@ pub(crate) fn bit_depth() -> u8 { | ||
| 275 | 275 | pub(crate) fn set_bit_depth(bit_depth: u8) { |
| 276 | 276 | BIT_DEPTH.with(|c| c.set(bit_depth)); |
| 277 | 277 | } |
| 278 | + | |
| 279 | +thread_local! { | |
| 280 | + /// This frame's superres state: `(upscaled_width, superres_denom)`, or | |
| 281 | + /// `(0, 8)` when the frame is not scaled. Spec 7.16's upscale runs | |
| 282 | + /// between CDEF and loop restoration, so LOOP RESTORATION -- unit grid, | |
| 283 | + /// `read_lr`'s unit corners and the pixel filters themselves -- works in | |
| 284 | + /// UPSCALED coordinates while deblock and CDEF work in downscaled ones | |
| 285 | + /// (libaom `av1_superres_upscale` in `decodeframe.c`, and | |
| 286 | + /// `av1_loop_restoration_corners_in_sb`'s own `mi_to_num_x`/`denom_x` | |
| 287 | + /// superres arms). Set once per frame from [`crate::stream`]'s decode | |
| 288 | + /// loop, like [`BIT_DEPTH`], and reset to `(0, 8)` on an unscaled frame | |
| 289 | + /// so a later frame never inherits it. | |
| 290 | + static SUPERRES: std::cell::Cell<(u32, u32)> = const { std::cell::Cell::new((0, 8)) }; | |
| 291 | +} | |
| 292 | + | |
| 293 | +/// Sets [`SUPERRES`] for the current thread. `upscaled_width` is 0 (and | |
| 294 | +/// `denom` 8) for a frame that is not superres-scaled. | |
| 295 | +pub(crate) fn set_superres(upscaled_width: u32, denom: u32) { | |
| 296 | + SUPERRES.with(|c| c.set((upscaled_width, denom))); | |
| 297 | +} | |
| 298 | + | |
| 299 | +/// `(upscaled_width, superres_denom)` when this frame is superres-scaled | |
| 300 | +/// (libaom `av1_superres_scaled`), else `None`. | |
| 301 | +pub(crate) fn superres() -> Option<(u32, u32)> { | |
| 302 | + let (w, d) = SUPERRES.with(std::cell::Cell::get); | |
| 303 | + (w > 0 && d != 8).then_some((w, d)) | |
| 304 | +} | |
| 278 | 305 | // Whether this superblock's single CDEF unit (this decoder has no 128x128 |
| 279 | 306 | // SB, so the SB IS the CDEF unit) has already had its `cdef_idx` literal |
| 280 | 307 | // read -- reset to `false` at the top of each superblock in the two tile |
| @@ -16940,7 +16967,14 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( | ||
| 16940 | 16967 | mi_cols as usize, |
| 16941 | 16968 | mi_rows as usize, |
| 16942 | 16969 | ); |
| 16943 | - let mut lr_grid = crate::restoration::RestorationGrid::new(lr, frame_width, frame_height); | |
| 16970 | + // spec 7.16/libaom `av1_alloc_restoration_struct`: the unit grid is | |
| 16971 | + // sized from `av1_get_upsampled_plane_size`, i.e. the UPSCALED width -- | |
| 16972 | + // loop restoration runs after the superres upscale. | |
| 16973 | + let mut lr_grid = crate::restoration::RestorationGrid::new( | |
| 16974 | + lr, | |
| 16975 | + superres().map_or(frame_width, |(w, _)| w), | |
| 16976 | + frame_height, | |
| 16977 | + ); | |
| 16944 | 16978 | |
| 16945 | 16979 | // lane-tiles r2: each tile gets its own fresh `SymbolDecoder` over its |
| 16946 | 16980 | // own byte range and its own fresh copy of the frame's initial CDFs |
| @@ -18835,12 +18869,57 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( | ||
| 18835 | 18869 | ); |
| 18836 | 18870 | dump_stage("EC_AV1_POSTCDEF_DUMP", &y, &u, &v); |
| 18837 | 18871 | dump_stage16("EC_AV1_POSTCDEF_DUMP16", &y, &u, &v, frame_width as usize, frame_height as usize); |
| 18872 | + // spec 7.16 (libaom `superres_post_decode`, decodeframe.c: called | |
| 18873 | + // between `av1_cdef_frame` and `av1_loop_restoration_filter_frame`): | |
| 18874 | + // the horizontal upscale runs HERE -- after CDEF, before loop | |
| 18875 | + // restoration, not after the whole filter chain. Both the post-CDEF | |
| 18876 | + // frame and the post-deblock frame that LR substitutes at its stripe | |
| 18877 | + // boundaries are upscaled (`save_deblock_boundary_lines` runs | |
| 18878 | + // `av1_upscale_normative_rows` over its saved rows when the frame is | |
| 18879 | + // scaled), so every LR coordinate from here on is in upscaled space. | |
| 18880 | + let (mut deblocked_y, mut deblocked_u, mut deblocked_v) = | |
| 18881 | + (deblocked_y, deblocked_u, deblocked_v); | |
| 18882 | + let mut lr_w = frame_width as usize; | |
| 18883 | + if let Some((upscaled_w, _)) = superres() { | |
| 18884 | + let bd = u32::from(bit_depth()); | |
| 18885 | + let (uw, fw, fh) = (upscaled_w as usize, frame_width as usize, frame_height as usize); | |
| 18886 | + let up = |p: &PlaneBuf, in_w: usize, in_h: usize, out_w: usize| PlaneBuf { | |
| 18887 | + data: crate::superres::upscale_plane_strided( | |
| 18888 | + &p.data, p.width, in_w, in_h, p.true_width, out_w, bd, | |
| 18889 | + ), | |
| 18890 | + width: out_w, | |
| 18891 | + height: in_h, | |
| 18892 | + true_width: out_w, | |
| 18893 | + true_height: in_h, | |
| 18894 | + tile_x0: 0, | |
| 18895 | + tile_y0: 0, | |
| 18896 | + tile_x1: out_w, | |
| 18897 | + tile_y1: in_h, | |
| 18898 | + }; | |
| 18899 | + let (cw, ch, cuw) = (fw.div_ceil(2), fh.div_ceil(2), uw.div_ceil(2)); | |
| 18900 | + y = up(&y, fw, fh, uw); | |
| 18901 | + u = up(&u, cw, ch, cuw); | |
| 18902 | + v = up(&v, cw, ch, cuw); | |
| 18903 | + deblocked_y = up(&deblocked_y, fw, fh, uw); | |
| 18904 | + deblocked_u = up(&deblocked_u, cw, ch, cuw); | |
| 18905 | + deblocked_v = up(&deblocked_v, cw, ch, cuw); | |
| 18906 | + lr_w = uw; | |
| 18907 | + crate::superres::note_upscaled_picture(); | |
| 18908 | + } | |
| 18838 | 18909 | apply_loop_restoration( |
| 18839 | 18910 | &mut y, &mut u, &mut v, &deblocked_y, &deblocked_u, &deblocked_v, lr, &lr_grid, |
| 18840 | - (frame_width as usize, frame_height as usize), | |
| 18911 | + (lr_w, frame_height as usize), | |
| 18841 | 18912 | ); |
| 18842 | 18913 | |
| 18843 | - let (fw, fh) = (frame_width as usize, frame_height as usize); | |
| 18914 | + let (fw, fh) = (lr_w, frame_height as usize); | |
| 18915 | + // An upscaled frame is already at its final size with no mi-aligned | |
| 18916 | + // margin beyond it (the upscale consumed those columns), so the | |
| 18917 | + // wide-margin stash below has nothing left to hand out. | |
| 18918 | + let (true_width, true_height) = if superres().is_some() { | |
| 18919 | + (fw, fh) | |
| 18920 | + } else { | |
| 18921 | + (true_width, true_height) | |
| 18922 | + }; | |
| 18844 | 18923 | if fw == width && fh == height { |
| 18845 | 18924 | LAST_FRAME_WIDE_MARGIN.with(|m| *m.borrow_mut() = None); |
| 18846 | 18925 | return Ok(( |
| @@ -28297,7 +28376,14 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 28297 | 28376 | mi_rows as usize, |
| 28298 | 28377 | ); |
| 28299 | 28378 | let mut grid = MiGrid::new(mi_cols as usize, mi_rows as usize); |
| 28300 | - let mut lr_grid = crate::restoration::RestorationGrid::new(lr, frame_width, frame_height); | |
| 28379 | + // spec 7.16/libaom `av1_alloc_restoration_struct`: the unit grid is | |
| 28380 | + // sized from `av1_get_upsampled_plane_size`, i.e. the UPSCALED width -- | |
| 28381 | + // loop restoration runs after the superres upscale. | |
| 28382 | + let mut lr_grid = crate::restoration::RestorationGrid::new( | |
| 28383 | + lr, | |
| 28384 | + superres().map_or(frame_width, |(w, _)| w), | |
| 28385 | + frame_height, | |
| 28386 | + ); | |
| 28301 | 28387 | |
| 28302 | 28388 | // lane-tiles r6: mirrors decode_key_frame_tile_with_cdfs's own per-tile |
| 28303 | 28389 | // loop (spec 5.11.2 decode_tile / 7.20 exit_symbol) -- each tile gets |
| @@ -30792,9 +30878,46 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 30792 | 30878 | ); |
| 30793 | 30879 | dump_stage("EC_AV1_POSTCDEF_DUMP", &y, &u, &v); |
| 30794 | 30880 | dump_stage16("EC_AV1_POSTCDEF_DUMP16", &y, &u, &v, frame_width as usize, frame_height as usize); |
| 30881 | + // spec 7.16 (libaom `superres_post_decode`, decodeframe.c: called | |
| 30882 | + // between `av1_cdef_frame` and `av1_loop_restoration_filter_frame`): | |
| 30883 | + // the horizontal upscale runs HERE -- after CDEF, before loop | |
| 30884 | + // restoration, not after the whole filter chain. Both the post-CDEF | |
| 30885 | + // frame and the post-deblock frame that LR substitutes at its stripe | |
| 30886 | + // boundaries are upscaled (`save_deblock_boundary_lines` runs | |
| 30887 | + // `av1_upscale_normative_rows` over its saved rows when the frame is | |
| 30888 | + // scaled), so every LR coordinate from here on is in upscaled space. | |
| 30889 | + let (mut deblocked_y, mut deblocked_u, mut deblocked_v) = | |
| 30890 | + (deblocked_y, deblocked_u, deblocked_v); | |
| 30891 | + let mut lr_w = frame_width as usize; | |
| 30892 | + if let Some((upscaled_w, _)) = superres() { | |
| 30893 | + let bd = u32::from(bit_depth()); | |
| 30894 | + let (uw, fw, fh) = (upscaled_w as usize, frame_width as usize, frame_height as usize); | |
| 30895 | + let up = |p: &PlaneBuf, in_w: usize, in_h: usize, out_w: usize| PlaneBuf { | |
| 30896 | + data: crate::superres::upscale_plane_strided( | |
| 30897 | + &p.data, p.width, in_w, in_h, p.true_width, out_w, bd, | |
| 30898 | + ), | |
| 30899 | + width: out_w, | |
| 30900 | + height: in_h, | |
| 30901 | + true_width: out_w, | |
| 30902 | + true_height: in_h, | |
| 30903 | + tile_x0: 0, | |
| 30904 | + tile_y0: 0, | |
| 30905 | + tile_x1: out_w, | |
| 30906 | + tile_y1: in_h, | |
| 30907 | + }; | |
| 30908 | + let (cw, ch, cuw) = (fw.div_ceil(2), fh.div_ceil(2), uw.div_ceil(2)); | |
| 30909 | + y = up(&y, fw, fh, uw); | |
| 30910 | + u = up(&u, cw, ch, cuw); | |
| 30911 | + v = up(&v, cw, ch, cuw); | |
| 30912 | + deblocked_y = up(&deblocked_y, fw, fh, uw); | |
| 30913 | + deblocked_u = up(&deblocked_u, cw, ch, cuw); | |
| 30914 | + deblocked_v = up(&deblocked_v, cw, ch, cuw); | |
| 30915 | + lr_w = uw; | |
| 30916 | + crate::superres::note_upscaled_picture(); | |
| 30917 | + } | |
| 30795 | 30918 | apply_loop_restoration( |
| 30796 | 30919 | &mut y, &mut u, &mut v, &deblocked_y, &deblocked_u, &deblocked_v, lr, &lr_grid, |
| 30797 | - (frame_width as usize, frame_height as usize), | |
| 30920 | + (lr_w, frame_height as usize), | |
| 30798 | 30921 | ); |
| 30799 | 30922 | |
| 30800 | 30923 | let motion_field = build_motion_field( |
| @@ -30806,7 +30929,15 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 30806 | 30929 | ref_order_hints, |
| 30807 | 30930 | ); |
| 30808 | 30931 | |
| 30809 | - let (fw, fh) = (frame_width as usize, frame_height as usize); | |
| 30932 | + let (fw, fh) = (lr_w, frame_height as usize); | |
| 30933 | + // An upscaled frame is already at its final size with no mi-aligned | |
| 30934 | + // margin beyond it (the upscale consumed those columns), so the | |
| 30935 | + // wide-margin stash below has nothing left to hand out. | |
| 30936 | + let (true_width, true_height) = if superres().is_some() { | |
| 30937 | + (fw, fh) | |
| 30938 | + } else { | |
| 30939 | + (true_width, true_height) | |
| 30940 | + }; | |
| 30810 | 30941 | if fw == width && fh == height { |
| 30811 | 30942 | LAST_FRAME_WIDE_MARGIN.with(|m| *m.borrow_mut() = None); |
| 30812 | 30943 | return Ok(( |
crates/ec-av1/src/restoration.rsmodified+11 -2
| @@ -340,8 +340,17 @@ pub(crate) fn read_lr( | ||
| 340 | 340 | let unit_size = lr.loop_restoration_size[plane]; |
| 341 | 341 | let horz_units = grid.horz_units[plane] as u32; |
| 342 | 342 | let vert_units = grid.vert_units[plane] as u32; |
| 343 | - let rcol0 = ceil_div(mi_col * mi_size, unit_size); | |
| 344 | - let rcol1 = ceil_div((mi_col + sb_mi) * mi_size, unit_size).min(horz_units); | |
| 343 | + // `av1_loop_restoration_corners_in_sb` (restoration.c:1314): under | |
| 344 | + // superres the mi column is in DOWNSCALED units while the unit grid | |
| 345 | + // is in upscaled ones, so the column division scales by | |
| 346 | + // `superres_denom / SCALE_NUMERATOR` (`mi_to_num_x`/`denom_x`). Rows | |
| 347 | + // are never scaled (spec 7.16 widens columns only). | |
| 348 | + let (num_x, denom_x) = match crate::decode::superres() { | |
| 349 | + Some((_, denom)) => (mi_size * denom, unit_size * 8), | |
| 350 | + None => (mi_size, unit_size), | |
| 351 | + }; | |
| 352 | + let rcol0 = ceil_div(mi_col * num_x, denom_x); | |
| 353 | + let rcol1 = ceil_div((mi_col + sb_mi) * num_x, denom_x).min(horz_units); | |
| 345 | 354 | let rrow0 = ceil_div(mi_row * mi_size, unit_size); |
| 346 | 355 | let rrow1 = ceil_div((mi_row + sb_mi) * mi_size, unit_size).min(vert_units); |
| 347 | 356 | let chroma = plane != 0; |
crates/ec-av1/src/stream.rsmodified+109 -38
| @@ -701,6 +701,14 @@ pub fn decode_stream_with( | ||
| 701 | 701 | )); |
| 702 | 702 | } |
| 703 | 703 | crate::decode::set_bit_depth(bit_depth); |
| 704 | + // spec 7.16: this frame's superres state, for the filter chain -- | |
| 705 | + // `decode.rs` upscales between CDEF and loop restoration and sizes | |
| 706 | + // the LR unit grid in upscaled coordinates. Cleared (0, 8) on an | |
| 707 | + // unscaled frame so a later frame never inherits it. | |
| 708 | + crate::decode::set_superres( | |
| 709 | + if header.use_superres { header.upscaled_width } else { 0 }, | |
| 710 | + u32::from(header.superres_denom), | |
| 711 | + ); | |
| 704 | 712 | // lane-av1comp: `comp_group_idx`/`compound_idx`'s own gating bits. |
| 705 | 713 | let enable_masked_compound = parser |
| 706 | 714 | .sequence_header() |
| @@ -802,25 +810,6 @@ pub fn decode_stream_with( | ||
| 802 | 810 | header.allow_intrabc, |
| 803 | 811 | header.delta, |
| 804 | 812 | )?; |
| 805 | - // lane-superres stage 3, spec 7.16: upscale AFTER deblock+CDEF | |
| 806 | - // (both already applied inside `decode_key_frame_tile_with_cdfs`) | |
| 807 | - // and BEFORE this picture is stored as a reference or handed to | |
| 808 | - // the caller -- loop restoration is not yet ported, so there is | |
| 809 | - // no LR step here for the upscale to precede. | |
| 810 | - // r3: the real decoded margin beyond `frame_width` (set by the | |
| 811 | - // call above, right before anything else can overwrite it) -- | |
| 812 | - // see `decode::take_last_frame_wide_margin`'s doc. | |
| 813 | - let wide_margin = crate::decode::take_last_frame_wide_margin(); | |
| 814 | - let picture = if header.use_superres { | |
| 815 | - crate::superres::upscale_picture( | |
| 816 | - &picture, | |
| 817 | - header.upscaled_width as usize, | |
| 818 | - wide_margin.as_ref(), | |
| 819 | - bit_depth as u32, | |
| 820 | - ) | |
| 821 | - } else { | |
| 822 | - picture | |
| 823 | - }; | |
| 824 | 813 | // A key frame codes no inter blocks -- its own saved motion |
| 825 | 814 | // field has no cells set, matching libaom's own "intra frame |
| 826 | 815 | // contributes nothing to a later projection" behaviour, but |
| @@ -932,25 +921,6 @@ pub fn decode_stream_with( | ||
| 932 | 921 | header.delta, |
| 933 | 922 | enable_filter_intra, |
| 934 | 923 | )?; |
| 935 | - // lane-superres r10: mirrors the key-frame branch above -- | |
| 936 | - // `decode_inter_frame_tile_with_cdfs` decodes at the downscaled | |
| 937 | - // `frame_width`/`frame_height` and crops to it; upscale AFTER | |
| 938 | - // (deblock/CDEF/LR already applied inside that call) BEFORE this | |
| 939 | - // picture is stored as a reference or shown, same margin | |
| 940 | - // mechanism (`take_last_frame_wide_margin`, read immediately | |
| 941 | - // after the call, before any other frame decode can overwrite | |
| 942 | - // it). | |
| 943 | - let wide_margin = crate::decode::take_last_frame_wide_margin(); | |
| 944 | - let picture = if header.use_superres { | |
| 945 | - crate::superres::upscale_picture( | |
| 946 | - &picture, | |
| 947 | - header.upscaled_width as usize, | |
| 948 | - wide_margin.as_ref(), | |
| 949 | - bit_depth as u32, | |
| 950 | - ) | |
| 951 | - } else { | |
| 952 | - picture | |
| 953 | - }; | |
| 954 | 924 | (picture, end_cdfs, motion_field) |
| 955 | 925 | }; |
| 956 | 926 | // lane-lr r4: the Wiener/self-guided pixel filters are wired |
| @@ -4823,6 +4793,107 @@ mod tests { | ||
| 4823 | 4793 | eprintln!("{NAME}: pixel-exact, grain_hits={}", crate::film_grain::grain_hits()); |
| 4824 | 4794 | } |
| 4825 | 4795 | |
| 4796 | + /// lane-t900 r29: `fixtures/superres_kf_cdef_lr_64x64.obu` is a real | |
| 4797 | + /// aomenc key frame (64x64 upscaled from a coded 43x64, | |
| 4798 | + /// `--superres-mode=1 --superres-denominator=12 --cpu-used=0 --sb-size=64 | |
| 4799 | + /// --enable-cdef=1 --enable-restoration=1 --cq-level=32`) -- the first | |
| 4800 | + /// stream in this repo that runs BOTH in-loop filters under superres. | |
| 4801 | + /// | |
| 4802 | + /// The defect it pins: spec 7.16's upscale runs between CDEF and loop | |
| 4803 | + /// restoration (libaom `superres_post_decode`, `decodeframe.c`, called | |
| 4804 | + /// between `av1_cdef_frame` and `av1_loop_restoration_filter_frame`), | |
| 4805 | + /// but this decoder ran the whole filter chain on the DOWNSCALED frame | |
| 4806 | + /// and upscaled last, in `stream.rs`. Loop restoration therefore filtered | |
| 4807 | + /// downscaled pixels with a unit grid sized from the downscaled width | |
| 4808 | + /// (1873 wrong samples, max 7, on this very stream). Class | |
| 4809 | + /// `wrong-stage-width`: every plane width in the filter chain is either | |
| 4810 | + /// the downscaled one (deblock, CDEF, and the mi grid all three walk) or | |
| 4811 | + /// the upscaled one (loop restoration's `av1_get_upsampled_plane_size` | |
| 4812 | + /// unit grid, `av1_loop_restoration_corners_in_sb`'s `mi_to_num_x` | |
| 4813 | + /// superres arm, and the pixel filters themselves); the sweep is every | |
| 4814 | + /// caller of `apply_deblock`/`apply_cdef`/`apply_loop_restoration` plus | |
| 4815 | + /// `RestorationGrid::new` and `read_lr`. | |
| 4816 | + #[test] | |
| 4817 | + fn a_superres_key_frame_with_cdef_and_loop_restoration_decodes_pixel_exact() { | |
| 4818 | + const NAME: &str = | |
| 4819 | + "a_superres_key_frame_with_cdef_and_loop_restoration_decodes_pixel_exact"; | |
| 4820 | + let _gate_lock = lock_gate_counters(); | |
| 4821 | + if !have_ffmpeg() { | |
| 4822 | + eprintln!("SKIP {NAME}: no ffmpeg"); | |
| 4823 | + return; | |
| 4824 | + } | |
| 4825 | + let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")) | |
| 4826 | + .join("fixtures/superres_kf_cdef_lr_64x64.obu"); | |
| 4827 | + let stream = std::fs::read(&path) | |
| 4828 | + .unwrap_or_else(|e| panic!("{NAME}: reading {}: {e}", path.display())); | |
| 4829 | + let (width, height) = (64usize, 64usize); | |
| 4830 | + let before_lr = crate::restoration::wiener_hits() + crate::restoration::sgrproj_hits(); | |
| 4831 | + let frames = match decode_stream(&stream) { | |
| 4832 | + Ok(frames) => frames, | |
| 4833 | + Err(e) => panic!("{NAME}: decode_stream refused: {e}"), | |
| 4834 | + }; | |
| 4835 | + assert_eq!(frames.len(), 1, "{NAME}: one key frame"); | |
| 4836 | + assert_eq!((frames[0].width, frames[0].height), (width, height)); | |
| 4837 | + // class `gate-blind-to-feature`: the stream must actually upscale and | |
| 4838 | + // actually run a real restoration filter, or this gate proves nothing. | |
| 4839 | + assert!( | |
| 4840 | + crate::superres::superres_hits() > 0, | |
| 4841 | + "{NAME}: zero superres_hits -- the frame was never upscaled" | |
| 4842 | + ); | |
| 4843 | + assert!( | |
| 4844 | + crate::restoration::wiener_hits() + crate::restoration::sgrproj_hits() > before_lr, | |
| 4845 | + "{NAME}: zero restoration units fired -- LR never ran under superres" | |
| 4846 | + ); | |
| 4847 | + let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, 1); | |
| 4848 | + assert_eq!(frames[0].y, ffmpeg_frames[0].y, "{NAME}: luma vs ffmpeg"); | |
| 4849 | + assert_eq!(frames[0].u, ffmpeg_frames[0].u, "{NAME}: U vs ffmpeg"); | |
| 4850 | + assert_eq!(frames[0].v, ffmpeg_frames[0].v, "{NAME}: V vs ffmpeg"); | |
| 4851 | + } | |
| 4852 | + | |
| 4853 | + /// lane-t900 r29 RESIDUAL, ignored: `fixtures/superres_alltools_sb128_320x180.obu` | |
| 4854 | + /// is 10 frames of `--cpu-used=4 --sb-size=128 --superres-mode=1 | |
| 4855 | + /// --superres-denominator=12 --cq-level=32` with every tool aomenc turns | |
| 4856 | + /// on by default (CDEF, loop restoration, OBMC, compound, warp). | |
| 4857 | + /// | |
| 4858 | + /// FIRST BAD (measured 2026-09-04, after the r29 upscale-before-LR fix): | |
| 4859 | + /// display frame 2, plane U, (x=88, y=21), delta 1; the mismatch is | |
| 4860 | + /// CHROMA ONLY and grows with the reference chain (frame 9: 280 U + 331 | |
| 4861 | + /// V samples, max 9). LUMA IS EXACT IN ALL 10 FRAMES. It is NOT the | |
| 4862 | + /// filter chain: re-encoding the same recipe with `--enable-cdef=0 | |
| 4863 | + /// --enable-restoration=0` still mismatches (359 samples, max 5, first | |
| 4864 | + /// bad display frame 4 plane V (115, 31)), and the r29 fix took the | |
| 4865 | + /// filters-on numbers from 37315 samples/max 171 down to 1620/max 11. | |
| 4866 | + /// So the residual is a scaled-reference CHROMA prediction defect at | |
| 4867 | + /// sb128/cpu-used=4 block shapes, not superres filtering -- next round's | |
| 4868 | + /// ladder starts at `EC_AV1_PREFILT_DUMP16` on decode-order frame 4 of | |
| 4869 | + /// the filters-off arm. | |
| 4870 | + #[test] | |
| 4871 | + #[ignore = "lane-t900 r29 residual: scaled-reference chroma prediction, frame 2 U (88,21)"] | |
| 4872 | + fn a_superres_all_tools_sb128_stream_decodes_pixel_exact() { | |
| 4873 | + const NAME: &str = "a_superres_all_tools_sb128_stream_decodes_pixel_exact"; | |
| 4874 | + let _gate_lock = lock_gate_counters(); | |
| 4875 | + if !have_ffmpeg() { | |
| 4876 | + eprintln!("SKIP {NAME}: no ffmpeg"); | |
| 4877 | + return; | |
| 4878 | + } | |
| 4879 | + let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")) | |
| 4880 | + .join("fixtures/superres_alltools_sb128_320x180.obu"); | |
| 4881 | + let stream = std::fs::read(&path) | |
| 4882 | + .unwrap_or_else(|e| panic!("{NAME}: reading {}: {e}", path.display())); | |
| 4883 | + let (width, height, count) = (320usize, 180usize, 10usize); | |
| 4884 | + let frames = match decode_stream(&stream) { | |
| 4885 | + Ok(frames) => frames, | |
| 4886 | + Err(e) => panic!("{NAME}: decode_stream refused: {e}"), | |
| 4887 | + }; | |
| 4888 | + assert_eq!(frames.len(), count, "{NAME}: frame count"); | |
| 4889 | + let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, count); | |
| 4890 | + for (i, (ours, theirs)) in frames.iter().zip(&ffmpeg_frames).enumerate() { | |
| 4891 | + assert_eq!(ours.y, theirs.y, "{NAME}: frame {i} luma vs ffmpeg"); | |
| 4892 | + assert_eq!(ours.u, theirs.u, "{NAME}: frame {i} U vs ffmpeg"); | |
| 4893 | + assert_eq!(ours.v, theirs.v, "{NAME}: frame {i} V vs ffmpeg"); | |
| 4894 | + } | |
| 4895 | + } | |
| 4896 | + | |
| 4826 | 4897 | /// lane-golomb r1: THE FILM ITSELF. `crates/ec-av1/fixtures/reelb_head_key_frame.obu` |
| 4827 | 4898 | /// is the first frame of the user's ReelB AV1 release (3840x1608 |
| 4828 | 4899 | /// yuv420p10le, `ffmpeg -i <mkv> -c copy -frames:v 1 -f obu`, 147 bytes) -- |
crates/ec-av1/src/superres.rsmodified+23 -68
| @@ -145,91 +145,46 @@ pub(crate) fn upscale_row( | ||
| 145 | 145 | } |
| 146 | 146 | } |
| 147 | 147 | |
| 148 | -/// Spec 7.16, applied to a whole plane: `av1_upscale_normative_rows` | |
| 149 | -/// widened to every row (`height2 == height`, no vertical scaling -- the | |
| 150 | -/// spec only widens columns). `rows` is the plane's row-major byte buffer | |
| 151 | -/// at `in_width`; returns a new buffer at `out_width`. | |
| 152 | -pub(crate) fn upscale_plane( | |
| 153 | - rows: &[u16], | |
| 154 | - height: usize, | |
| 155 | - in_width: usize, | |
| 156 | - out_width: usize, | |
| 157 | - margin: Option<(&[u16], usize)>, | |
| 148 | +/// Every row of one decoded plane, upscaled by spec 7.16 from `in_w` to | |
| 149 | +/// `out_w` (`av1_upscale_normative_rows`; the process is horizontal-only, | |
| 150 | +/// so the row count is unchanged). `data`/`stride` are the decoder's own | |
| 151 | +/// reconstruction buffer, whose columns `[in_w, true_w)` hold the real | |
| 152 | +/// decoded samples of the coding block straddling the frame edge -- libaom's | |
| 153 | +/// border extension replicates from that last real column, so they are | |
| 154 | +/// handed to [`upscale_row`] as its right margin (see its doc). | |
| 155 | +pub(crate) fn upscale_plane_strided( | |
| 156 | + data: &[u16], | |
| 157 | + stride: usize, | |
| 158 | + in_w: usize, | |
| 159 | + in_h: usize, | |
| 160 | + true_w: usize, | |
| 161 | + out_w: usize, | |
| 158 | 162 | bit_depth: u32, |
| 159 | 163 | ) -> Vec<u16> { |
| 160 | - debug_assert_eq!(rows.len(), height * in_width); | |
| 161 | - let mut out = vec![0u16; height * out_width]; | |
| 162 | - for r in 0..height { | |
| 163 | - let src = &rows[r * in_width..(r + 1) * in_width]; | |
| 164 | - let dst = &mut out[r * out_width..(r + 1) * out_width]; | |
| 165 | - let real_right = match margin { | |
| 166 | - Some((margin_rows, margin_width)) if margin_width > in_width => { | |
| 167 | - &margin_rows[r * margin_width + in_width..(r + 1) * margin_width] | |
| 168 | - } | |
| 169 | - _ => &[], | |
| 170 | - }; | |
| 171 | - upscale_row(src, real_right, out_width, dst, bit_depth); | |
| 164 | + let mut out = vec![0u16; in_h * out_w]; | |
| 165 | + for r in 0..in_h { | |
| 166 | + let row = &data[r * stride..r * stride + in_w]; | |
| 167 | + let margin = &data[r * stride + in_w..r * stride + true_w.max(in_w)]; | |
| 168 | + upscale_row(row, margin, out_w, &mut out[r * out_w..(r + 1) * out_w], bit_depth); | |
| 172 | 169 | } |
| 173 | 170 | out |
| 174 | 171 | } |
| 175 | 172 | |
| 176 | 173 | thread_local! { |
| 177 | 174 | /// Firing count for the superres gate (class `gate-blind-to-feature`): |
| 178 | - /// how many whole pictures actually ran through [`upscale_plane`]. | |
| 175 | + /// how many whole pictures actually ran through [`upscale_plane_strided`]. | |
| 179 | 176 | static SUPERRES_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 180 | 177 | } |
| 181 | 178 | |
| 182 | 179 | /// Current value of [`SUPERRES_HITS`]. |
| 180 | +#[allow(dead_code)] | |
| 183 | 181 | pub(crate) fn superres_hits() -> usize { |
| 184 | 182 | SUPERRES_HITS.with(|c| c.get()) |
| 185 | 183 | } |
| 186 | 184 | |
| 187 | -/// A whole [`crate::encode::Picture`]'s Y/U/V planes, upscaled from | |
| 188 | -/// `frame_width`/`frame_height` (4:2:0 chroma) to `upscaled_width` at the | |
| 189 | -/// same height -- spec 7.16 is horizontal-only. Increments | |
| 190 | -/// [`SUPERRES_HITS`] once per call. `margin`, when set (r3, from | |
| 191 | -/// `decode::take_last_frame_wide_margin`), is the real decoded pixels | |
| 192 | -/// beyond `picture`'s own width out to the mi-aligned `true_width` -- see | |
| 193 | -/// [`upscale_row`]'s doc for why the right-edge padding needs it. | |
| 194 | -pub(crate) fn upscale_picture( | |
| 195 | - picture: &crate::encode::Picture, | |
| 196 | - upscaled_width: usize, | |
| 197 | - margin: Option<&crate::encode::Picture>, | |
| 198 | - bit_depth: u32, | |
| 199 | -) -> crate::encode::Picture { | |
| 185 | +/// Counts one whole upscaled picture (all three planes) for [`SUPERRES_HITS`]. | |
| 186 | +pub(crate) fn note_upscaled_picture() { | |
| 200 | 187 | SUPERRES_HITS.with(|c| c.set(c.get() + 1)); |
| 201 | - let height = picture.height; | |
| 202 | - let chroma_in_w = picture.width.div_ceil(2); | |
| 203 | - let chroma_out_w = upscaled_width.div_ceil(2); | |
| 204 | - let chroma_h = height.div_ceil(2); | |
| 205 | - crate::encode::Picture { | |
| 206 | - width: upscaled_width, | |
| 207 | - height, | |
| 208 | - y: upscale_plane( | |
| 209 | - &picture.y, | |
| 210 | - height, | |
| 211 | - picture.width, | |
| 212 | - upscaled_width, | |
| 213 | - margin.map(|m| (m.y.as_slice(), m.width)), | |
| 214 | - bit_depth, | |
| 215 | - ), | |
| 216 | - u: upscale_plane( | |
| 217 | - &picture.u, | |
| 218 | - chroma_h, | |
| 219 | - chroma_in_w, | |
| 220 | - chroma_out_w, | |
| 221 | - margin.map(|m| (m.u.as_slice(), m.width.div_ceil(2))), | |
| 222 | - bit_depth, | |
| 223 | - ), | |
| 224 | - v: upscale_plane( | |
| 225 | - &picture.v, | |
| 226 | - chroma_h, | |
| 227 | - chroma_in_w, | |
| 228 | - chroma_out_w, | |
| 229 | - margin.map(|m| (m.v.as_slice(), m.width.div_ceil(2))), | |
| 230 | - bit_depth, | |
| 231 | - ), | |
| 232 | - } | |
| 233 | 188 | } |
| 234 | 189 | |
| 235 | 190 | #[cfg(test)] |