diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index f2928bf6..42ce01a5 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -32897,7 +32897,7 @@ pub(crate) fn decode_inter_frame_tile( data, mi_cols, mi_rows, base_q_idx, frame_width, frame_height, refpix, cdef, loop_filter, allow_high_precision_mv, force_integer_mv, interp_fixed, enable_dual_filter, reference_select, tx_select, - &LoopRestorationParams::default(), initial_cdfs, NO_SIGN_BIAS, fctx, + &LoopRestorationParams::default(), initial_cdfs, NO_SIGN_BIAS, false, fctx, ) } @@ -32932,6 +32932,12 @@ pub(crate) fn decode_inter_frame_tile_lr( // as `tx_select`/`initial_cdfs` above: a header-derived value a raw tile // decode has to be told. sign_bias: crate::mvstack::SignBiasTable, + // This frame header's own `allow_screen_content_tools`: its intra blocks + // then carry the palette syntax (spec 5.11.46), which a raw tile decode + // that guessed `false` reads one symbol short of. Placed after + // `sign_bias` rather than beside the other bools on purpose (the + // `adjacent-same-typed-args` class). + allow_screen_content_tools: bool, fctx: &crate::decode::FrameCtx, ) -> Result { let single_tile = TileInfo { @@ -32973,7 +32979,7 @@ pub(crate) fn decode_inter_frame_tile_lr( tx_select, false, false, - false, + allow_screen_content_tools, DeltaParams::default(), // `enable_filter_intra`: this crate's own encoder never writes the // sequence bit (`encode.rs`), so no intra-in-inter block of a stream @@ -37517,6 +37523,9 @@ mod tests { &frame.loop_restoration, Some(frame.start_cdfs.0.clone()), NO_SIGN_BIAS, + // This frame's own `allow_screen_content_tools`, off the + // `Encoded` the encoder handed back (stale-header class). + frame.screen, fctx, ) .unwrap(); @@ -37841,6 +37850,9 @@ mod tests { &frame.loop_restoration, Some(frame.start_cdfs.0.clone()), NO_SIGN_BIAS, + // This frame's own `allow_screen_content_tools`, off the + // `Encoded` the encoder handed back (stale-header class). + frame.screen, fctx, ) .unwrap(); diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index 37a0e492..26353fed 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -5011,6 +5011,14 @@ pub(crate) fn encode_inter_frame( let picture_y8: Vec = picture.y.iter().map(|&v| v as u8).collect(); let picture_u8: Vec = picture.u.iter().map(|&v| v as u8).collect(); let picture_v8: Vec = picture.v.iter().map(|&v| v as u8).collect(); + // This frame's own `allow_screen_content_tools`. It can only be set when + // the SEQUENCE offers the bit at all, which is the key frame's decision + // ([`SEQ_SCREEN`]): a frame header that contradicts + // `seq_force_screen_content_tools` is a corrupt stream, not a tighter + // encode. + let screen = seq_screen() + && screen_content(&picture_y8, picture.width, true_width, true_height); + header.allow_screen_content_tools = screen; let mut luma = Plane { source: &picture_y8, reconstruction: vec![128; picture_y8.len()], @@ -5045,9 +5053,7 @@ pub(crate) fn encode_inter_frame( lambda: lambda_scale() * step * step, modes: &KEY_FRAME_MODES, top_k: prune_top_k_inter(), - // An inter frame never sets `allow_screen_content_tools` yet, so its - // intra blocks code no palette syntax (deferred, see the lane report). - screen: false, + screen, }; let mode_bits_table = inter_mode_bits(); @@ -5503,6 +5509,7 @@ pub(crate) fn encode_inter_frame( crate::tile::arm_sign_bias(sign_bias); crate::tile::arm_reference_select(header.reference_select); crate::tile::arm_order_hints(seq.order_hint_bits, order_hint, order_hints); + crate::tile::arm_screen(screen); let mut tile = crate::tile::sb_coeff_inter_frame_tile_cdfs( mi_cols, mi_rows, @@ -5595,6 +5602,7 @@ pub(crate) fn encode_inter_frame( crate::tile::arm_sign_bias(sign_bias); crate::tile::arm_reference_select(reference_select()); crate::tile::arm_order_hints(seq.order_hint_bits, order_hint, order_hints); + crate::tile::arm_screen(screen); let mut cdfs = start_cdfs.0.clone(); let coded = crate::tile::sb_coeff_inter_frame_tile_cdfs( mi_cols, @@ -5632,6 +5640,7 @@ pub(crate) fn encode_inter_frame( // frame's `start_cdfs`), not from the defaults. Some(start_cdfs.0.clone()), sign_bias, + h.allow_screen_content_tools, fctx, ) }, @@ -5659,9 +5668,7 @@ pub(crate) fn encode_inter_frame( mi_rows: header.mi_rows, base_q_idx, tx_select, - // An inter frame of this encoder never sets the bit (its intra blocks - // code no palette syntax yet -- see the lane report's deferred item). - screen: false, + screen, start_cdfs, next_cdfs, loop_filter: header.loop_filter, @@ -6190,6 +6197,54 @@ mod tests { assert_eq!(decoded.y, encoded.reconstruction.y, "ffmpeg: luma"); assert_eq!(decoded.u, encoded.reconstruction.u, "ffmpeg: U"); assert_eq!(decoded.v, encoded.reconstruction.v, "ffmpeg: V"); + // A whole GOP: the inter frames' own intra blocks carry the palette + // syntax too (the three intra arms of the inter writers), which only + // ffmpeg reading the frames back can prove. + let moved: Vec = (0..4) + .map(|f| { + let base = screen_card(width, height); + let mut out = Picture::grey(width, height); + for y in 0..height { + for x in 0..width { + let sx = (x + f * 7) % width; + out.y[y * width + x] = base.y[y * width + sx]; + } + } + for y in 0..height / 2 { + for x in 0..width / 2 { + let sx = (x + f * 3) % (width / 2); + out.u[y * (width / 2) + x] = base.u[y * (width / 2) + sx]; + out.v[y * (width / 2) + x] = base.v[y * (width / 2) + sx]; + } + } + out + }) + .collect(); + let _ = crate::tile::take_palette_hits(); + let gop = encode_sequence_with_ctx(&moved, 100, 0.5, fctx).unwrap(); + let gop_hits = crate::tile::take_palette_hits(); + let inter_screen = gop.frames.iter().skip(1).filter(|f| f.screen).count(); + eprintln!( + "GOP: {} frames with screen tools after the key frame, {} palette blocks", + inter_screen, gop_hits[0] + ); + assert!( + inter_screen > 0, + "no inter frame set allow_screen_content_tools" + ); + let gop_decoded = crate::stream::decode_stream(&gop.stream).unwrap(); + for (i, (ours, frame)) in gop_decoded.iter().zip(gop.frames.iter()).enumerate() { + assert_eq!(ours.y, frame.reconstruction.y, "our decoder: GOP frame {i} luma"); + } + if have_ffmpeg() { + let ff = ffmpeg_decode_sequence(&gop.stream, width, height, moved.len()); + for (i, (theirs, frame)) in ff.iter().zip(gop.frames.iter()).enumerate() { + assert_eq!(theirs.y, frame.reconstruction.y, "ffmpeg: GOP frame {i} luma"); + assert_eq!(theirs.u, frame.reconstruction.u, "ffmpeg: GOP frame {i} U"); + assert_eq!(theirs.v, frame.reconstruction.v, "ffmpeg: GOP frame {i} V"); + } + } + // The other half of the keep rule: film-shaped content leaves the // detector (and so the whole sequence header) alone. let film = encode_key_frame_with_ctx(&test_card(width, height), 100, 0.5, fctx).unwrap(); diff --git a/crates/ec-av1/src/tile.rs b/crates/ec-av1/src/tile.rs index 6f41d5a7..46b06f80 100644 --- a/crates/ec-av1/src/tile.rs +++ b/crates/ec-av1/src/tile.rs @@ -1235,6 +1235,13 @@ struct Neighbours { above_palette_size: Vec, /// The same, down the left edge. left_palette_size: Vec, + /// What the block being written right now will leave in the two palette + /// bands, published by [`Self::record_mi_planes`] -- the one point EVERY + /// block of either frame type passes through, so an inter block (or an + /// intra block that took no palette) clears the bands rather than letting + /// an earlier palette block's size stand (the neighbour-band hazard + /// lane-t900 r23 hit on the decoder side). + pending_palette: Option<(u8, [u16; 8])>, /// The colours beside [`Self::above_palette_size`]. above_palette_colors: Vec<[u16; 8]>, /// The same, down the left edge. @@ -1324,6 +1331,7 @@ impl Neighbours { left_txfm: vec![TXFM_CTX_INIT; rows * (SUB / MI)], above_palette_size: vec![0; cols * (SUB / MI)], left_palette_size: vec![0; rows * (SUB / MI)], + pending_palette: None, above_palette_colors: vec![[0; 8]; cols * (SUB / MI)], left_palette_colors: vec![[0; 8]; rows * (SUB / MI)], mi_cols, @@ -1568,6 +1576,10 @@ impl Neighbours { luma: bool, ) { let (mi_r, mi_c) = at_mi; + if screen_armed() { + let (size, colors) = self.pending_palette.take().unwrap_or((0, [0u16; 8])); + self.record_palette_y(at_mi, side, size, colors); + } let states: [Neighbour; 3] = std::array::from_fn(|plane| neighbour_state(&grids[plane])); let side_mi = side / MI; for cell in 0..side_mi { @@ -2424,47 +2436,61 @@ fn write_intra_mode( // `av1_get_palette_mode_ctx` counts palette NEIGHBOURS, and this writer // codes none -- exactly the ctx-0 read the decoder does under the same // condition. - if screen_armed() { - let palette = block.palette.as_ref().filter(|_| mode == DC_PRED); - if let Some(bsize_ctx) = crate::decode::palette_bsize_ctx(side) { - let (mode_ctx, cache) = neighbours.palette_ctx_and_cache(mi); - if mode == DC_PRED { - enc.symbol( - usize::from(palette.is_some()), - &mut cdfs.palette_y_mode[bsize_ctx][mode_ctx], - ); - } - if let Some(pal) = palette { - let n = usize::from(pal.size); - enc.symbol(n - 2, &mut cdfs.palette_y_size[bsize_ctx]); - write_palette_colors_y(enc, &pal.colors[..n], &cache); - } - // `read_palette_mode_info`'s UV half sits between the luma - // colours and the luma colour-index map: libaom defers both maps - // to `av1_visit_palette`, after the whole mode info. - if uv_mode == DC_PRED { - enc.symbol(0, &mut cdfs.palette_uv_mode[usize::from(palette.is_some())]); - } - if let Some(pal) = palette { - let n = usize::from(pal.size); - write_color_index_map( - enc, - &mut cdfs.palette_y_color_index[n - 2], - &pal.map, - side, - n, - ); - note_palette(n); - } + write_palette_syntax(enc, cdfs, neighbours, block.palette.as_ref(), mode, uv_mode, mi, side); + mode +} + +/// `read_palette_mode_info` (spec 5.11.46) written out, mirrored symbol for +/// symbol from the decoder's own read in `read_intra_mode`: on a frame whose +/// header set `allow_screen_content_tools`, a `DC_PRED` luma block and a +/// `UV_DC_PRED` chroma pair each carry a use-palette flag, and a luma palette +/// then codes its size, its colours (against the neighbour colour cache) and +/// -- after the chroma flag, exactly where libaom's `av1_visit_palette` runs +/// -- its colour-index map. Shared by the key frame's [`write_intra_mode`] and +/// by the three intra arms of the inter-frame writers, which read the same +/// syntax back through `decode_inter_block`/`decode_intra_rect_in_inter`. +/// +/// The band publication is deferred to [`Neighbours::record_mi_planes`] (the +/// one point every block passes through), so a block that takes no palette +/// still clears what the column held. +#[allow(clippy::too_many_arguments)] +fn write_palette_syntax( + enc: &mut SymbolEncoder, + cdfs: &mut Cdfs, + neighbours: &mut Neighbours, + palette: Option<&PaletteY>, + mode: usize, + uv_mode: usize, + mi: (usize, usize), + side: usize, +) { + if !screen_armed() { + return; + } + let palette = palette.filter(|_| mode == DC_PRED); + if let Some(bsize_ctx) = crate::decode::palette_bsize_ctx(side) { + let (mode_ctx, cache) = neighbours.palette_ctx_and_cache(mi); + if mode == DC_PRED { + enc.symbol( + usize::from(palette.is_some()), + &mut cdfs.palette_y_mode[bsize_ctx][mode_ctx], + ); + } + if let Some(pal) = palette { + let n = usize::from(pal.size); + enc.symbol(n - 2, &mut cdfs.palette_y_size[bsize_ctx]); + write_palette_colors_y(enc, &pal.colors[..n], &cache); + } + if uv_mode == DC_PRED { + enc.symbol(0, &mut cdfs.palette_uv_mode[usize::from(palette.is_some())]); + } + if let Some(pal) = palette { + let n = usize::from(pal.size); + write_color_index_map(enc, &mut cdfs.palette_y_color_index[n - 2], &pal.map, side, n); + note_palette(n); } - // Every block of a screen-content frame publishes its (possibly zero) - // palette state, or a neighbour column keeps an earlier block's size - // and reads a context the decoder does not. - let (size, colors) = - palette.map_or((0, [0u16; 8]), |p| (p.size, p.colors)); - neighbours.record_palette_y(mi, side, size, colors); } - mode + neighbours.pending_palette = palette.map(|p| (p.size, p.colors)); } /// How many blocks took a palette, and of what size -- the fire count a gate @@ -4368,6 +4394,16 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs( // Intra: no vote, but still a coded cell -- mvstack's // extended-scan coverage must see it (module doc). let (mi_row, mi_col) = (r32 as usize * 8, c32 as usize * 8); + write_palette_syntax( + &mut enc, + &mut cdfs, + &mut neighbours, + block.palette.as_ref(), + mode, + uv_mode, + (mi_row, mi_col), + BLOCK, + ); for dr in 0..8 { for dc in 0..8 { grid.set( @@ -4581,6 +4617,16 @@ fn write_inter_frame_leaf( // Intra: no vote, but still a coded cell -- mvstack's extended-scan // coverage must see it (module doc). let (mi_row, mi_col) = (r * SUB_MI as usize, c * SUB_MI as usize); + write_palette_syntax( + enc, + cdfs, + neighbours, + block.palette.as_ref(), + mode, + uv_mode, + (mi_row, mi_col), + SUB, + ); for dr in 0..SUB_MI as usize { for dc in 0..SUB_MI as usize { grid.set( @@ -4800,6 +4846,16 @@ fn write_inter_frame_leaf8( // Intra: no vote, but still a coded cell -- mvstack's extended-scan // coverage must see it (module doc). let (mi_row, mi_col) = leaf_mi; + write_palette_syntax( + enc, + cdfs, + neighbours, + block.palette.as_ref(), + mode, + uv_mode, + leaf_mi, + 8, + ); for dr in 0..2 { for dc in 0..2 { grid.set(