Commit caa970cb37 for aom

commit caa970cb374d6cd9dcb77fc7ef51c025c318c752
Author: Ihor Buhaiov <freedbrt@gmail.com>
Date:   Sat Nov 28 17:46:58 2026 +0100

    rtc: Try IDTX on palettes that lose colors

    In the nonrd inter mode search the IDTX test of the best inter mode
    writes IDTX into ctx->tx_type_map. A palette that wins after that
    copies its own map only when the first entry is not DCT_DCT. From speed
    7 the palette tx search costs luma with DCT only
    (dct_only_palette_nonrd), so the IDTX of the inter mode stays and the
    block is coded with a transform type that was never costed. Encoder
    and decoder agree, so the stream is valid.

    Copying the map always removes the stale type but loses BD rate,
    because the stale IDTX mostly lands on palettes that can't hold every
    color of the block. Their residue is sharp and IDTX often codes it
    better than DCT.

    So copy the map always, and let the palette tx search try IDTX next to
    DCT for a luma palette of a block with more than 8 colors, on inter
    frames, 8 bit, with transforms up to 16x16. Lossless and the intra DCT
    only and flip and identity type controls keep DCT only. Rename the
    speed feature dct_only_palette_nonrd to dct_idtx_palette_nonrd, since
    it is no longer DCT only.

    Main 5381b9a7a6, base vs this change, realtime only build, tune
    screen, speed 11, active map, CDF update mode 1, 4 threads, decoder and
    recon compare on every run (2448 runs, 0 mismatches). Clips: 20 desktop
    screen recordings (1080p to 5K, up to 600 kept frames) and
    screendata.y4m and screendata.1920_1080.y4m from the AOM test data,
    equal frames dropped. BD rate is the mean over clips of a cubic
    fit of log bytes over PSNR of all planes (Y in brackets). Bytes are
    summed over the clips, PSNR changes are means over the clips.

                                  4:2:0            4:4:4
      q 9 to 15, 64x64 sb, bytes  -0.65%           -0.47%
      q 9 to 15, 64x64 sb, PSNR Y +0.36 dB         +0.36 dB
      BD q 8 to 18, 64x64 sb      -3.19% (-3.84%)  -2.36% (-3.82%)
      BD q 8 to 18, default sb    -3.20% (-3.86%)  -2.51% (-4.01%)
      BD q 8 to 20                -2.99% (-3.61%)  -2.19% (-3.58%)
      BD q 20 to 63               -0.16% (-0.19%)  -0.20% (-0.28%)

    Per clip at q 9 to 18: -8.22% to +0.16% (4:2:0), -6.08% to +0.40%
    (4:4:4); the macOS recordings barely move. From q 30 to 63 each q
    point changes the summed bytes by -0.86% to +0.10% and PSNR by -0.03
    to +0.00 dB.

    Rate control at 30 fps on 9 clips, speeds at fixed q on 5 clips:

                                  4:2:0            4:4:4
      CBR, BD over 3 rates        +0.49%           -0.40%
      CBR aq 3, BD over 2 rates   +0.56%           +0.05%
      VBR max q ladder, 2 rates   -0.70%           -0.42%
      VBR library defaults, no map  +0.93% bytes +0.03 dB, -0.23% +0.03 dB
      Key frame every 30, CBR     -0.22% +0.00 dB, +0.10% +0.01 dB
      BD q 8 to 63 at speed 7     -0.13%           -0.12%
                        speed 9   -0.76%           -0.59%
                        speed 10  -0.68%           -0.46%

    The lowest CBR rate runs at a mean q of 48 to 63. The rate control
    rows move per clip by up to +-4%, but the copy alone moves them as
    much (+6.0% to -7.7% per clip), and a single thread run (one tile
    column) turns the CBR means into -1.86% and -0.17%. The largest
    outlier, a window drag at q 52, codes one frame with 4.5 kB in one
    stream and 40 kB in the other.

    Camera clips (park_joy_90p 8 bit 4:2:0 and 4:4:4, niklas 720p,
    rush_hour 4:4:4; speeds 7, 9, 10 and 11; CBR, VBR and fixed q; 1, 2
    and 4 threads; default tune) give the same streams in a realtime only
    and a default build (132 of 132). 10 bit (park_joy_90p 10 bit and
    screendata at 640x480 and 1080p and a window drag coded as 10 bit, 4:2:0
    and 4:4:4) gives the same streams too (56 of 56). Monochrome and gray
    chroma (128) at 8 and 10 bit, 4:2:0 and 4:4:4: no assert in debug
    builds, no mismatch, 10 bit streams unchanged (85 cases). The Mac
    and the x86 machine give the same streams on all 280 cells compared.

    Encode time, VBR at q 9 to 15, 64x64 sb, IntraBC on, 4 of the screen
    recordings at 150 frames, median of 7 interleaved runs with a second
    base run for A/A, geomean:

                       1 thread         4 threads
      Apple M3 Pro, release -O3 without high bit depth
      4:2:0            -1.99% (A/A +0.51%)  -1.59% (A/A +0.04%)
      4:4:4            -1.09% (A/A +0.21%)  -1.14% (A/A -0.08%)
      AMD Ryzen 9 5900X, clang-cl 19.1.5 /O2
      4:2:0            -1.21% (A/A +0.09%)  -1.34% (A/A -0.00%)
      4:4:4            -1.09% (A/A +0.15%)  -1.10% (A/A +0.26%)
      same, gcc 13.3 -O3 in WSL2
      4:2:0            -1.80% (A/A -0.38%)  -1.21% (A/A -0.11%)
      4:4:4            -0.60% (A/A +0.66%)  -1.22% (A/A +0.86%)

    The window drag is 2.6% to 5.1% faster. The other clips move by -2.2%
    to +1.7%, A/A per clip -0.7% to +1.9%.

    EncodeAPI, ActiveMap, InactiveArea, ROIMap, IntraBC, ScreenContent,
    Kmeans, RTEndToEnd and realtime Datarate tests pass in a default and a
    realtime only build with asserts on, KeyFrameInterval in the default
    build.

    Bug: 591290399
    Change-Id: I93fb86e1470dd64ed368629b914196f56378d49c

diff --git a/av1/encoder/block.h b/av1/encoder/block.h
index ac2ad4e49d..24531ed27b 100644
--- a/av1/encoder/block.h
+++ b/av1/encoder/block.h
@@ -1391,6 +1391,9 @@ typedef struct macroblock {
   //! Threshold on the number of colors for testing palette mode.
   int color_palette_thresh;

+  //! Number of luma colors of the block, set by the palette search.
+  int palette_luma_colors;
+
   //! Used in REALTIME coding mode: flag to indicate if the color_sensitivity
   // should be checked at the coding block level.
   int force_color_check_block_level;
diff --git a/av1/encoder/nonrd_pickmode.c b/av1/encoder/nonrd_pickmode.c
index 84f6dcface..66da8268b9 100644
--- a/av1/encoder/nonrd_pickmode.c
+++ b/av1/encoder/nonrd_pickmode.c
@@ -3480,8 +3480,8 @@ static AOM_FORCE_INLINE void handle_screen_content_mode_nonrd(
         x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_V)])
       search_state->this_rdc.skip_txfm = 0;

-    if (xd->tx_type_map[0] != DCT_DCT)
-      av1_copy_array(ctx->tx_type_map, xd->tx_type_map, ctx->num_4x4_blk);
+    // The map may still hold the IDTX of an inter mode tested before.
+    av1_copy_array(ctx->tx_type_map, xd->tx_type_map, ctx->num_4x4_blk);
   }
 }

diff --git a/av1/encoder/palette.c b/av1/encoder/palette.c
index 9f3e6fdaa1..cd367b918e 100644
--- a/av1/encoder/palette.c
+++ b/av1/encoder/palette.c
@@ -571,6 +571,7 @@ void av1_rd_pick_palette_intra_sby(
     av1_count_colors(src, src_stride, rows, cols, count_buf, &colors);
     colors_threshold = colors;
   }
+  x->palette_luma_colors = colors;

   uint8_t *const color_map = xd->plane[0].color_index_map;
   // Relax the distinct-color threshold for static screen-content KF groups so
diff --git a/av1/encoder/speed_features.c b/av1/encoder/speed_features.c
index 31356bb449..5aff7167ed 100644
--- a/av1/encoder/speed_features.c
+++ b/av1/encoder/speed_features.c
@@ -2049,7 +2049,7 @@ static void set_rt_speed_feature_framesize_dependent(const AV1_COMP *const cpi,
       sf->mv_sf.use_bsize_dependent_search_method = 0;
       sf->rt_sf.skip_cdef_sb = 1;
       sf->rt_sf.increase_color_thresh_palette = 1;
-      if (!frame_is_intra_only(cm)) sf->rt_sf.dct_only_palette_nonrd = 1;
+      if (!frame_is_intra_only(cm)) sf->rt_sf.dct_idtx_palette_nonrd = 1;
     }
     if (speed >= 8) {
       sf->rt_sf.nonrd_check_partition_merge_mode = 3;
@@ -2077,7 +2077,7 @@ static void set_rt_speed_feature_framesize_dependent(const AV1_COMP *const cpi,
       sf->rt_sf.nonrd_aggressive_skip = 1;
       sf->rt_sf.thresh_active_maps_skip_lf_cdef = 90;
       sf->rt_sf.hybrid_intra_pickmode = 0;
-      sf->rt_sf.dct_only_palette_nonrd = 1;
+      sf->rt_sf.dct_idtx_palette_nonrd = 1;
       sf->rt_sf.prune_palette_search_nonrd = 1;
       sf->rt_sf.prune_intra_mode_using_best_sad_so_far = true;
       sf->rt_sf.rc_faster_convergence_static = 1;
@@ -2935,7 +2935,7 @@ static inline void init_rt_sf(REAL_TIME_SPEED_FEATURES *rt_sf) {
   rt_sf->use_rtc_tf = 0;
   rt_sf->use_idtx_nonrd = 0;
   rt_sf->prune_idtx_nonrd = 0;
-  rt_sf->dct_only_palette_nonrd = 0;
+  rt_sf->dct_idtx_palette_nonrd = 0;
   rt_sf->part_early_exit_zeromv = 0;
   rt_sf->sse_early_term_inter_search = EARLY_TERM_DISABLED;
   rt_sf->skip_lf_screen = 0;
diff --git a/av1/encoder/speed_features.h b/av1/encoder/speed_features.h
index aab3a781eb..102cecb9e5 100644
--- a/av1/encoder/speed_features.h
+++ b/av1/encoder/speed_features.h
@@ -2075,8 +2075,9 @@ typedef struct REAL_TIME_SPEED_FEATURES {
   // is not already set.
   int prune_idtx_nonrd;

-  // Force to only use dct for palette search in nonrd pickmode.
-  int dct_only_palette_nonrd;
+  // Use only DCT for palette search in nonrd pickmode, and also IDTX for
+  // luma palettes that can't hold every color of the block.
+  int dct_idtx_palette_nonrd;

   // Skip loopfilter, for static content after slide change
   // or key frame, once quality has ramped up.
diff --git a/av1/encoder/tx_search.c b/av1/encoder/tx_search.c
index 707a3f0ebf..fdb1349678 100644
--- a/av1/encoder/tx_search.c
+++ b/av1/encoder/tx_search.c
@@ -2091,6 +2091,21 @@ static inline void predict_dc_only_block(
   }
 }

+// A luma palette that can't hold every color of the block leaves a sharp
+// residue, which the identity transform often codes better than DCT.
+static inline bool palette_tries_idtx(const AV1_COMP *cpi, const MACROBLOCK *x,
+                                      int plane, TX_SIZE tx_size) {
+  const MACROBLOCKD *const xd = &x->e_mbd;
+  const MB_MODE_INFO *const mbmi = xd->mi[0];
+  const TxfmSizeTypeCfg *const txfm_cfg = &cpi->oxcf.txfm_cfg;
+  return plane == AOM_PLANE_Y && mbmi->palette_mode_info.palette_size[0] > 0 &&
+         x->palette_luma_colors > PALETTE_MAX_SIZE && xd->bd == 8 &&
+         !frame_is_intra_only(&cpi->common) &&
+         !xd->lossless[mbmi->segment_id] &&
+         txsize_sqr_up_map[tx_size] <= TX_16X16 && txfm_cfg->enable_flip_idtx &&
+         !txfm_cfg->use_intra_dct_only;
+}
+
 // Search for the best transform type for a given transform block.
 // This function can be used for both inter and intra, both luma and chroma.
 static void search_tx_type(const AV1_COMP *cpi, MACROBLOCK *x, int plane,
@@ -2164,12 +2179,16 @@ static void search_tx_type(const AV1_COMP *cpi, MACROBLOCK *x, int plane,
   uint16_t tx_mask;

   // Use DCT_DCT transform for DC only block.
-  if (dc_only_blk || cpi->sf.rt_sf.dct_only_palette_nonrd == 1)
+  if (dc_only_blk) {
     tx_mask = 1 << DCT_DCT;
-  else
+  } else if (cpi->sf.rt_sf.dct_idtx_palette_nonrd == 1) {
+    tx_mask = 1 << DCT_DCT;
+    if (palette_tries_idtx(cpi, x, plane, tx_size)) tx_mask |= 1 << IDTX;
+  } else {
     tx_mask = get_tx_mask(cpi, x, plane, block, blk_row, blk_col, plane_bsize,
                           tx_size, txb_ctx, ftxs_mode, ref_best_rd,
                           &txk_allowed, txk_map, &is_txk_map_populated);
+  }
   const uint16_t allowed_tx_mask = tx_mask;

   if (is_cur_buf_hbd(xd)) {