Coverage Report

Created: 2026-11-12 08:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/aom/av1/encoder/intra_mode_search.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2020, Alliance for Open Media. All rights reserved.
3
 *
4
 * This source code is subject to the terms of the BSD 2 Clause License and
5
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6
 * was not distributed with this source code in the LICENSE file, you can
7
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8
 * Media Patent License 1.0 was not distributed with this source code in the
9
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10
 */
11
12
#include <stdbool.h>
13
14
#include "av1/common/av1_common_int.h"
15
#include "av1/common/cfl.h"
16
#include "av1/common/reconintra.h"
17
18
#include "av1/encoder/intra_mode_search.h"
19
#include "av1/encoder/intra_mode_search_utils.h"
20
#include "av1/encoder/palette.h"
21
#include "av1/encoder/speed_features.h"
22
#include "av1/encoder/tx_search.h"
23
24
// Even though there are 7 delta angles, this macro is set to 9 to facilitate
25
// the rd threshold check to prune -3 and 3 delta angles.
26
0
#define SIZE_OF_ANGLE_DELTA_RD_COST_ARRAY (2 * MAX_ANGLE_DELTA + 3)
27
28
/*!\cond */
29
static const PREDICTION_MODE intra_rd_search_mode_order[INTRA_MODES] = {
30
  DC_PRED,       H_PRED,        V_PRED,    SMOOTH_PRED, PAETH_PRED,
31
  SMOOTH_V_PRED, SMOOTH_H_PRED, D135_PRED, D203_PRED,   D157_PRED,
32
  D67_PRED,      D113_PRED,     D45_PRED,
33
};
34
35
static const UV_PREDICTION_MODE uv_rd_search_mode_order[UV_INTRA_MODES] = {
36
  UV_DC_PRED,     UV_CFL_PRED,   UV_H_PRED,        UV_V_PRED,
37
  UV_SMOOTH_PRED, UV_PAETH_PRED, UV_SMOOTH_V_PRED, UV_SMOOTH_H_PRED,
38
  UV_D135_PRED,   UV_D203_PRED,  UV_D157_PRED,     UV_D67_PRED,
39
  UV_D113_PRED,   UV_D45_PRED,
40
};
41
42
// The bitmask corresponds to the filter intra modes as defined in enums.h
43
// FILTER_INTRA_MODE enumeration type. Setting a bit to 0 in the mask means to
44
// disable the evaluation of corresponding filter intra mode. The table
45
// av1_derived_filter_intra_mode_used_flag is used when speed feature
46
// prune_filter_intra_level is 1. The evaluated filter intra modes are union
47
// of the following:
48
// 1) FILTER_DC_PRED
49
// 2) mode that corresponds to best mode so far of DC_PRED, V_PRED, H_PRED,
50
// D157_PRED and PAETH_PRED. (Eg: FILTER_V_PRED if best mode so far is V_PRED).
51
static const uint8_t av1_derived_filter_intra_mode_used_flag[INTRA_MODES] = {
52
  0x01,  // DC_PRED:           0000 0001
53
  0x03,  // V_PRED:            0000 0011
54
  0x05,  // H_PRED:            0000 0101
55
  0x01,  // D45_PRED:          0000 0001
56
  0x01,  // D135_PRED:         0000 0001
57
  0x01,  // D113_PRED:         0000 0001
58
  0x09,  // D157_PRED:         0000 1001
59
  0x01,  // D203_PRED:         0000 0001
60
  0x01,  // D67_PRED:          0000 0001
61
  0x01,  // SMOOTH_PRED:       0000 0001
62
  0x01,  // SMOOTH_V_PRED:     0000 0001
63
  0x01,  // SMOOTH_H_PRED:     0000 0001
64
  0x11   // PAETH_PRED:        0001 0001
65
};
66
67
// The bitmask corresponds to the chroma intra modes as defined in enums.h
68
// UV_PREDICTION_MODE enumeration type. Setting a bit to 0 in the mask means to
69
// disable the evaluation of corresponding chroma intra mode. The table
70
// av1_derived_chroma_intra_mode_used_flag is used when speed feature
71
// prune_chroma_modes_using_luma_winner is enabled. The evaluated chroma
72
// intra modes are union of the following:
73
// 1) UV_DC_PRED
74
// 2) UV_SMOOTH_PRED
75
// 3) UV_CFL_PRED
76
// 4) mode that corresponds to luma intra mode winner (Eg : UV_V_PRED if luma
77
// intra mode winner is V_PRED).
78
static const uint16_t av1_derived_chroma_intra_mode_used_flag[INTRA_MODES] = {
79
  0x2201,  // DC_PRED:           0010 0010 0000 0001
80
  0x2203,  // V_PRED:            0010 0010 0000 0011
81
  0x2205,  // H_PRED:            0010 0010 0000 0101
82
  0x2209,  // D45_PRED:          0010 0010 0000 1001
83
  0x2211,  // D135_PRED:         0010 0010 0001 0001
84
  0x2221,  // D113_PRED:         0010 0010 0010 0001
85
  0x2241,  // D157_PRED:         0010 0010 0100 0001
86
  0x2281,  // D203_PRED:         0010 0010 1000 0001
87
  0x2301,  // D67_PRED:          0010 0011 0000 0001
88
  0x2201,  // SMOOTH_PRED:       0010 0010 0000 0001
89
  0x2601,  // SMOOTH_V_PRED:     0010 0110 0000 0001
90
  0x2a01,  // SMOOTH_H_PRED:     0010 1010 0000 0001
91
  0x3201   // PAETH_PRED:        0011 0010 0000 0001
92
};
93
94
DECLARE_ALIGNED(16, static const uint8_t, all_zeros[MAX_SB_SIZE]) = { 0 };
95
DECLARE_ALIGNED(16, static const uint16_t,
96
                highbd_all_zeros[MAX_SB_SIZE]) = { 0 };
97
98
int av1_calc_normalized_variance(aom_variance_fn_t vf, const uint8_t *const buf,
99
0
                                 const int stride, const int is_hbd) {
100
0
  unsigned int sse;
101
102
0
  if (is_hbd)
103
0
    return vf(buf, stride, CONVERT_TO_BYTEPTR(highbd_all_zeros), 0, &sse);
104
0
  else
105
0
    return vf(buf, stride, all_zeros, 0, &sse);
106
0
}
107
108
// Computes average of log(1 + variance) across 4x4 sub-blocks for source and
109
// reconstructed blocks.
110
static void compute_avg_log_variance(const AV1_COMP *const cpi, MACROBLOCK *x,
111
                                     const BLOCK_SIZE bs,
112
                                     double *avg_log_src_variance,
113
0
                                     double *avg_log_recon_variance) {
114
0
  const MACROBLOCKD *const xd = &x->e_mbd;
115
0
  const BLOCK_SIZE sb_size = cpi->common.seq_params->sb_size;
116
0
  const int mi_row_in_sb = x->e_mbd.mi_row & (mi_size_high[sb_size] - 1);
117
0
  const int mi_col_in_sb = x->e_mbd.mi_col & (mi_size_wide[sb_size] - 1);
118
0
  const int right_overflow =
119
0
      (xd->mb_to_right_edge < 0) ? ((-xd->mb_to_right_edge) >> 3) : 0;
120
0
  const int bottom_overflow =
121
0
      (xd->mb_to_bottom_edge < 0) ? ((-xd->mb_to_bottom_edge) >> 3) : 0;
122
0
  const int bw = (MI_SIZE * mi_size_wide[bs] - right_overflow);
123
0
  const int bh = (MI_SIZE * mi_size_high[bs] - bottom_overflow);
124
0
  const int is_hbd = is_cur_buf_hbd(xd);
125
126
0
  aom_variance_fn_t vf = cpi->ppi->fn_ptr[BLOCK_4X4].vf;
127
0
  for (int i = 0; i < bh; i += MI_SIZE) {
128
0
    const int r = mi_row_in_sb + (i >> MI_SIZE_LOG2);
129
0
    for (int j = 0; j < bw; j += MI_SIZE) {
130
0
      const int c = mi_col_in_sb + (j >> MI_SIZE_LOG2);
131
0
      const int mi_offset = r * mi_size_wide[sb_size] + c;
132
0
      Block4x4VarInfo *block_4x4_var_info =
133
0
          &x->src_var_info_of_4x4_sub_blocks[mi_offset];
134
0
      int src_var = block_4x4_var_info->var;
135
0
      double log_src_var = block_4x4_var_info->log_var;
136
      // Compute average of log(1 + variance) for the source block from 4x4
137
      // sub-block variance values. Calculate and store 4x4 sub-block variance
138
      // and log(1 + variance), if the values present in
139
      // src_var_of_4x4_sub_blocks are invalid. Reuse the same if it is readily
140
      // available with valid values.
141
0
      if (src_var < 0) {
142
0
        src_var = av1_calc_normalized_variance(
143
0
            vf, x->plane[0].src.buf + i * x->plane[0].src.stride + j,
144
0
            x->plane[0].src.stride, is_hbd);
145
0
        block_4x4_var_info->var = src_var;
146
0
        log_src_var = log1p(src_var / 16.0);
147
0
        block_4x4_var_info->log_var = log_src_var;
148
0
      } else {
149
        // When source variance is already calculated and available for
150
        // retrieval, check if log(1 + variance) is also available. If it is
151
        // available, then retrieve from buffer. Else, calculate the same and
152
        // store to the buffer.
153
0
        if (log_src_var < 0) {
154
0
          log_src_var = log1p(src_var / 16.0);
155
0
          block_4x4_var_info->log_var = log_src_var;
156
0
        }
157
0
      }
158
0
      *avg_log_src_variance += log_src_var;
159
160
0
      const int recon_var = av1_calc_normalized_variance(
161
0
          vf, xd->plane[0].dst.buf + i * xd->plane[0].dst.stride + j,
162
0
          xd->plane[0].dst.stride, is_hbd);
163
0
      *avg_log_recon_variance += log1p(recon_var / 16.0);
164
0
    }
165
0
  }
166
167
0
  const int blocks = (bw * bh) / 16;
168
0
  *avg_log_src_variance /= (double)blocks;
169
0
  *avg_log_recon_variance /= (double)blocks;
170
0
}
171
172
// Returns a factor to be applied to the RD value based on how well the
173
// reconstructed block variance matches the source variance.
174
static double intra_rd_variance_factor(const AV1_COMP *cpi, MACROBLOCK *x,
175
0
                                       BLOCK_SIZE bs) {
176
0
  double threshold = INTRA_RD_VAR_THRESH(cpi->oxcf.speed);
177
  // For non-positive threshold values, the comparison of source and
178
  // reconstructed variances with threshold evaluates to false
179
  // (src_var < threshold/rec_var < threshold) as these metrics are greater than
180
  // than 0. Hence further calculations are skipped.
181
0
  if (threshold <= 0) return 1.0;
182
183
0
  double variance_rd_factor = 1.0;
184
0
  double avg_log_src_variance = 0.0;
185
0
  double avg_log_recon_variance = 0.0;
186
0
  double var_diff = 0.0;
187
188
0
  compute_avg_log_variance(cpi, x, bs, &avg_log_src_variance,
189
0
                           &avg_log_recon_variance);
190
191
  // Dont allow 0 to prevent / 0 below.
192
0
  avg_log_src_variance += 0.000001;
193
0
  avg_log_recon_variance += 0.000001;
194
195
0
  if (avg_log_src_variance >= avg_log_recon_variance) {
196
0
    var_diff = (avg_log_src_variance - avg_log_recon_variance);
197
0
    if ((var_diff > 0.5) && (avg_log_recon_variance < threshold)) {
198
0
      variance_rd_factor = 1.0 + ((var_diff * 2) / avg_log_src_variance);
199
0
    }
200
0
  } else {
201
0
    var_diff = (avg_log_recon_variance - avg_log_src_variance);
202
0
    if ((var_diff > 0.5) && (avg_log_src_variance < threshold)) {
203
0
      variance_rd_factor = 1.0 + (var_diff / (2 * avg_log_src_variance));
204
0
    }
205
0
  }
206
207
  // Limit adjustment;
208
0
  variance_rd_factor = AOMMIN(3.0, variance_rd_factor);
209
210
0
  return variance_rd_factor;
211
0
}
212
/*!\endcond */
213
214
/*!\brief Search for the best filter_intra mode when coding intra frame.
215
 *
216
 * \ingroup intra_mode_search
217
 * \callergraph
218
 * This function loops through all filter_intra modes to find the best one.
219
 *
220
 * \return Returns 1 if a new filter_intra mode is selected; 0 otherwise.
221
 */
222
static int rd_pick_filter_intra_sby(const AV1_COMP *const cpi, MACROBLOCK *x,
223
                                    int *rate, int *rate_tokenonly,
224
                                    int64_t *distortion, uint8_t *skippable,
225
                                    BLOCK_SIZE bsize, int mode_cost,
226
                                    PREDICTION_MODE best_mode_so_far,
227
                                    int64_t *best_rd, int64_t *best_model_rd,
228
0
                                    PICK_MODE_CONTEXT *ctx) {
229
  // Skip the evaluation of filter intra modes.
230
0
  if (cpi->sf.intra_sf.prune_filter_intra_level == 2) return 0;
231
232
0
  MACROBLOCKD *const xd = &x->e_mbd;
233
0
  MB_MODE_INFO *mbmi = xd->mi[0];
234
0
  int filter_intra_selected_flag = 0;
235
0
  FILTER_INTRA_MODE mode;
236
0
  TX_SIZE best_tx_size = TX_8X8;
237
0
  FILTER_INTRA_MODE_INFO filter_intra_mode_info;
238
0
  uint8_t best_tx_type_map[MAX_MIB_SIZE * MAX_MIB_SIZE];
239
0
  av1_zero(filter_intra_mode_info);
240
0
  mbmi->filter_intra_mode_info.use_filter_intra = 1;
241
0
  mbmi->mode = DC_PRED;
242
0
  mbmi->palette_mode_info.palette_size[0] = 0;
243
244
  // Skip the evaluation of filter-intra if cached MB_MODE_INFO does not have
245
  // filter-intra as winner.
246
0
  if (x->use_mb_mode_cache &&
247
0
      !x->mb_mode_cache->filter_intra_mode_info.use_filter_intra)
248
0
    return 0;
249
250
0
  for (mode = 0; mode < FILTER_INTRA_MODES; ++mode) {
251
0
    int64_t this_rd;
252
0
    RD_STATS tokenonly_rd_stats;
253
0
    mbmi->filter_intra_mode_info.filter_intra_mode = mode;
254
255
0
    if ((cpi->sf.intra_sf.prune_filter_intra_level == 1) &&
256
0
        !(av1_derived_filter_intra_mode_used_flag[best_mode_so_far] &
257
0
          (1 << mode)))
258
0
      continue;
259
260
    // Skip the evaluation of modes that do not match with the winner mode in
261
    // x->mb_mode_cache.
262
0
    if (x->use_mb_mode_cache &&
263
0
        mode != x->mb_mode_cache->filter_intra_mode_info.filter_intra_mode)
264
0
      continue;
265
266
0
    if (model_intra_yrd_and_prune(cpi, x, bsize, best_model_rd)) {
267
0
      continue;
268
0
    }
269
0
    av1_pick_uniform_tx_size_type_yrd(cpi, x, &tokenonly_rd_stats, bsize,
270
0
                                      *best_rd);
271
0
    if (tokenonly_rd_stats.rate == INT_MAX) continue;
272
0
    const int this_rate =
273
0
        tokenonly_rd_stats.rate +
274
0
        intra_mode_info_cost_y(cpi, x, mbmi, bsize, mode_cost, 0);
275
0
    this_rd = RDCOST(x->rdmult, this_rate, tokenonly_rd_stats.dist);
276
277
    // Visual quality adjustment based on recon vs source variance.
278
0
    if ((cpi->oxcf.mode == ALLINTRA) && (this_rd != INT64_MAX)) {
279
0
      this_rd = (int64_t)(this_rd * intra_rd_variance_factor(cpi, x, bsize));
280
0
    }
281
282
    // Collect mode stats for multiwinner mode processing
283
0
    const int txfm_search_done = 1;
284
0
    store_winner_mode_stats(
285
0
        cpi, x, mbmi, NULL, NULL, NULL, 0, NULL, bsize, this_rd,
286
0
        cpi->sf.winner_mode_sf.multi_winner_mode_type, txfm_search_done);
287
0
    if (this_rd < *best_rd) {
288
0
      *best_rd = this_rd;
289
0
      best_tx_size = mbmi->tx_size;
290
0
      filter_intra_mode_info = mbmi->filter_intra_mode_info;
291
0
      av1_copy_array(best_tx_type_map, xd->tx_type_map, ctx->num_4x4_blk);
292
0
      *rate = this_rate;
293
0
      *rate_tokenonly = tokenonly_rd_stats.rate;
294
0
      *distortion = tokenonly_rd_stats.dist;
295
0
      *skippable = tokenonly_rd_stats.skip_txfm;
296
0
      filter_intra_selected_flag = 1;
297
0
    }
298
0
  }
299
300
0
  if (filter_intra_selected_flag) {
301
0
    mbmi->mode = DC_PRED;
302
0
    mbmi->tx_size = best_tx_size;
303
0
    mbmi->filter_intra_mode_info = filter_intra_mode_info;
304
0
    av1_copy_array(ctx->tx_type_map, best_tx_type_map, ctx->num_4x4_blk);
305
0
    return 1;
306
0
  } else {
307
0
    return 0;
308
0
  }
309
0
}
310
311
void av1_count_colors(const uint8_t *src, int stride, int rows, int cols,
312
0
                      int *val_count, int *num_colors) {
313
0
  const int max_pix_val = 1 << 8;
314
0
  memset(val_count, 0, max_pix_val * sizeof(val_count[0]));
315
0
  for (int r = 0; r < rows; ++r) {
316
0
    for (int c = 0; c < cols; ++c) {
317
0
      const int this_val = src[r * stride + c];
318
0
      assert(this_val < max_pix_val);
319
0
      ++val_count[this_val];
320
0
    }
321
0
  }
322
0
  int n = 0;
323
0
  for (int i = 0; i < max_pix_val; ++i) {
324
0
    if (val_count[i]) ++n;
325
0
  }
326
0
  *num_colors = n;
327
0
}
328
329
void av1_count_colors_highbd(const uint8_t *src8, int stride, int rows,
330
                             int cols, int bit_depth, int *val_count,
331
                             int *bin_val_count, int *num_color_bins,
332
0
                             int *num_colors) {
333
0
  assert(bit_depth <= 12);
334
0
  const int max_bin_val = 1 << 8;
335
0
  const int max_pix_val = 1 << bit_depth;
336
0
  const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
337
0
  memset(bin_val_count, 0, max_bin_val * sizeof(val_count[0]));
338
0
  if (val_count != NULL)
339
0
    memset(val_count, 0, max_pix_val * sizeof(val_count[0]));
340
0
  for (int r = 0; r < rows; ++r) {
341
0
    for (int c = 0; c < cols; ++c) {
342
      /*
343
       * Down-convert the pixels to 8-bit domain before counting.
344
       * This provides consistency of behavior for palette search
345
       * between lbd and hbd encodes. This down-converted pixels
346
       * are only used for calculating the threshold (n).
347
       */
348
0
      const int this_val = ((src[r * stride + c]) >> (bit_depth - 8));
349
0
      assert(this_val < max_bin_val);
350
0
      if (this_val >= max_bin_val) continue;
351
0
      ++bin_val_count[this_val];
352
0
      if (val_count != NULL) ++val_count[(src[r * stride + c])];
353
0
    }
354
0
  }
355
0
  int n = 0;
356
  // Count the colors based on 8-bit domain used to gate the palette path
357
0
  for (int i = 0; i < max_bin_val; ++i) {
358
0
    if (bin_val_count[i]) ++n;
359
0
  }
360
0
  *num_color_bins = n;
361
362
  // Count the actual hbd colors used to create top_colors
363
0
  n = 0;
364
0
  if (val_count != NULL) {
365
0
    for (int i = 0; i < max_pix_val; ++i) {
366
0
      if (val_count[i]) ++n;
367
0
    }
368
0
    *num_colors = n;
369
0
  }
370
0
}
371
372
bool av1_count_colors_with_threshold(const uint8_t *src, int stride, int rows,
373
                                     int cols, int num_colors_threshold,
374
0
                                     int *num_colors) {
375
0
  bool has_color[1 << 8] = { false };
376
0
  *num_colors = 0;
377
378
0
  for (int r = 0; r < rows; ++r) {
379
0
    for (int c = 0; c < cols; ++c) {
380
0
      const int this_val = src[r * stride + c];
381
0
      if (!has_color[this_val]) {
382
0
        has_color[this_val] = true;
383
0
        (*num_colors)++;
384
0
        if (*num_colors > num_colors_threshold) {
385
          // We're over the threshold, so we can exit early
386
0
          return false;
387
0
        }
388
0
      }
389
0
    }
390
0
  }
391
0
  return true;
392
0
}
393
394
void set_y_mode_and_delta_angle(const int mode_idx, MB_MODE_INFO *const mbmi,
395
0
                                int reorder_delta_angle_eval) {
396
0
  if (mode_idx < INTRA_MODE_END) {
397
0
    mbmi->mode = intra_rd_search_mode_order[mode_idx];
398
0
    mbmi->angle_delta[PLANE_TYPE_Y] = 0;
399
0
  } else {
400
    // Precomputed lookup tables for directional mode and delta angle indexed by
401
    // (mode_idx - INTRA_MODE_END) to avoid division/modulo by 6.
402
0
    static const uint8_t mode_div6[48] = {
403
0
      V_PRED,    V_PRED,    V_PRED,    V_PRED,    V_PRED,    V_PRED,
404
0
      H_PRED,    H_PRED,    H_PRED,    H_PRED,    H_PRED,    H_PRED,
405
0
      D45_PRED,  D45_PRED,  D45_PRED,  D45_PRED,  D45_PRED,  D45_PRED,
406
0
      D135_PRED, D135_PRED, D135_PRED, D135_PRED, D135_PRED, D135_PRED,
407
0
      D113_PRED, D113_PRED, D113_PRED, D113_PRED, D113_PRED, D113_PRED,
408
0
      D157_PRED, D157_PRED, D157_PRED, D157_PRED, D157_PRED, D157_PRED,
409
0
      D203_PRED, D203_PRED, D203_PRED, D203_PRED, D203_PRED, D203_PRED,
410
0
      D67_PRED,  D67_PRED,  D67_PRED,  D67_PRED,  D67_PRED,  D67_PRED,
411
0
    };
412
0
    static const int8_t angle_mod6[2][48] = {
413
0
      {
414
0
          -3, -2, -1, 1,  2,  3,  -3, -2, -1, 1,  2,  3,  -3, -2, -1, 1,
415
0
          2,  3,  -3, -2, -1, 1,  2,  3,  -3, -2, -1, 1,  2,  3,  -3, -2,
416
0
          -1, 1,  2,  3,  -3, -2, -1, 1,  2,  3,  -3, -2, -1, 1,  2,  3,
417
0
      },
418
0
      {
419
0
          -2, 2,  -3, -1, 1,  3,  -2, 2,  -3, -1, 1,  3,  -2, 2,  -3, -1,
420
0
          1,  3,  -2, 2,  -3, -1, 1,  3,  -2, 2,  -3, -1, 1,  3,  -2, 2,
421
0
          -3, -1, 1,  3,  -2, 2,  -3, -1, 1,  3,  -2, 2,  -3, -1, 1,  3,
422
0
      },
423
0
    };
424
0
    const int offset = mode_idx - INTRA_MODE_END;
425
0
    mbmi->mode = (PREDICTION_MODE)mode_div6[offset];
426
0
    mbmi->angle_delta[PLANE_TYPE_Y] =
427
0
        angle_mod6[reorder_delta_angle_eval != 0][offset];
428
0
  }
429
0
}
430
431
static inline int get_model_rd_index_for_pruning(
432
    const MACROBLOCK *const x,
433
0
    const INTRA_MODE_SPEED_FEATURES *const intra_sf) {
434
0
  const int top_intra_model_count_allowed =
435
0
      intra_sf->top_intra_model_count_allowed;
436
0
  if (!intra_sf->adapt_top_model_rd_count_using_neighbors)
437
0
    return top_intra_model_count_allowed - 1;
438
439
0
  const MACROBLOCKD *const xd = &x->e_mbd;
440
0
  const PREDICTION_MODE mode = xd->mi[0]->mode;
441
0
  int model_rd_index_for_pruning = top_intra_model_count_allowed - 1;
442
0
  int is_left_mode_neq_cur_mode = 0, is_above_mode_neq_cur_mode = 0;
443
0
  if (xd->left_available)
444
0
    is_left_mode_neq_cur_mode = xd->left_mbmi->mode != mode;
445
0
  if (xd->up_available)
446
0
    is_above_mode_neq_cur_mode = xd->above_mbmi->mode != mode;
447
  // The pruning of luma intra modes is made more aggressive at lower quantizers
448
  // and vice versa. The value for model_rd_index_for_pruning is derived as
449
  // follows.
450
  // qidx 0 to 127: Reduce the index of a candidate used for comparison only if
451
  // the current mode does not match either of the available neighboring modes.
452
  // qidx 128 to 255: Reduce the index of a candidate used for comparison only
453
  // if the current mode does not match both the available neighboring modes.
454
0
  if (x->qindex <= 127) {
455
0
    if (is_left_mode_neq_cur_mode || is_above_mode_neq_cur_mode)
456
0
      model_rd_index_for_pruning = AOMMAX(model_rd_index_for_pruning - 1, 0);
457
0
  } else {
458
0
    if (is_left_mode_neq_cur_mode && is_above_mode_neq_cur_mode)
459
0
      model_rd_index_for_pruning = AOMMAX(model_rd_index_for_pruning - 1, 0);
460
0
  }
461
0
  return model_rd_index_for_pruning;
462
0
}
463
464
/*! \brief prune luma intra mode based on the model rd.
465
 * \param[in]    this_model_rd              model rd for current mode.
466
 * \param[in]    best_model_rd              Best model RD seen for this block so
467
 *                                          far.
468
 * \param[in]    top_intra_model_rd         Top intra model RD seen for this
469
 *                                          block so far.
470
 * \param[in]    max_model_cnt_allowed      The maximum number of top intra
471
 *                                          model RD allowed.
472
 * \param[in]    model_rd_index_for_pruning Index of the candidate used for
473
 *                                          pruning based on model rd.
474
 */
475
static int prune_intra_y_mode(int64_t this_model_rd, int64_t *best_model_rd,
476
                              int64_t top_intra_model_rd[],
477
                              int max_model_cnt_allowed,
478
0
                              int model_rd_index_for_pruning) {
479
0
  const double thresh_best = 1.50;
480
0
  const double thresh_top = 1.00;
481
0
  for (int i = 0; i < max_model_cnt_allowed; i++) {
482
0
    if (this_model_rd < top_intra_model_rd[i]) {
483
0
      for (int j = max_model_cnt_allowed - 1; j > i; j--) {
484
0
        top_intra_model_rd[j] = top_intra_model_rd[j - 1];
485
0
      }
486
0
      top_intra_model_rd[i] = this_model_rd;
487
0
      break;
488
0
    }
489
0
  }
490
0
  if (top_intra_model_rd[model_rd_index_for_pruning] != INT64_MAX &&
491
0
      this_model_rd >
492
0
          thresh_top * top_intra_model_rd[model_rd_index_for_pruning])
493
0
    return 1;
494
495
0
  if (this_model_rd != INT64_MAX &&
496
0
      this_model_rd > thresh_best * (*best_model_rd))
497
0
    return 1;
498
0
  if (this_model_rd < *best_model_rd) *best_model_rd = this_model_rd;
499
0
  return 0;
500
0
}
501
502
// Run RD calculation with given chroma intra prediction angle., and return
503
// the RD cost. Update the best mode info. if the RD cost is the best so far.
504
static int64_t pick_intra_angle_routine_sbuv(
505
    const AV1_COMP *const cpi, MACROBLOCK *x, BLOCK_SIZE bsize,
506
    int rate_overhead, int64_t best_rd_in, int *rate, RD_STATS *rd_stats,
507
0
    int *best_angle_delta, int64_t *best_rd) {
508
0
  MB_MODE_INFO *mbmi = x->e_mbd.mi[0];
509
0
  assert(!is_inter_block(mbmi));
510
0
  int this_rate;
511
0
  int64_t this_rd;
512
0
  RD_STATS tokenonly_rd_stats;
513
514
0
  if (!av1_txfm_uvrd(cpi, x, &tokenonly_rd_stats, bsize, best_rd_in))
515
0
    return INT64_MAX;
516
0
  this_rate = tokenonly_rd_stats.rate +
517
0
              intra_mode_info_cost_uv(cpi, x, mbmi, bsize, rate_overhead);
518
0
  this_rd = RDCOST(x->rdmult, this_rate, tokenonly_rd_stats.dist);
519
0
  if (this_rd < *best_rd) {
520
0
    *best_rd = this_rd;
521
0
    *best_angle_delta = mbmi->angle_delta[PLANE_TYPE_UV];
522
0
    *rate = this_rate;
523
0
    rd_stats->rate = tokenonly_rd_stats.rate;
524
0
    rd_stats->dist = tokenonly_rd_stats.dist;
525
0
    rd_stats->skip_txfm = tokenonly_rd_stats.skip_txfm;
526
0
  }
527
0
  return this_rd;
528
0
}
529
530
/*!\brief Search for the best angle delta for chroma prediction
531
 *
532
 * \ingroup intra_mode_search
533
 * \callergraph
534
 * Given a chroma directional intra prediction mode, this function will try to
535
 * estimate the best delta_angle.
536
 *
537
 * \returns Return if there is a new mode with smaller rdcost than best_rd.
538
 */
539
static int rd_pick_intra_angle_sbuv(const AV1_COMP *const cpi, MACROBLOCK *x,
540
                                    BLOCK_SIZE bsize, int rate_overhead,
541
                                    int64_t best_rd, int *rate,
542
0
                                    RD_STATS *rd_stats) {
543
0
  MACROBLOCKD *const xd = &x->e_mbd;
544
0
  MB_MODE_INFO *mbmi = xd->mi[0];
545
0
  assert(!is_inter_block(mbmi));
546
0
  int i, angle_delta, best_angle_delta = 0;
547
0
  int64_t this_rd, best_rd_in, rd_cost[2 * (MAX_ANGLE_DELTA + 2)];
548
549
0
  rd_stats->rate = INT_MAX;
550
0
  rd_stats->skip_txfm = 0;
551
0
  rd_stats->dist = INT64_MAX;
552
0
  for (i = 0; i < 2 * (MAX_ANGLE_DELTA + 2); ++i) rd_cost[i] = INT64_MAX;
553
554
0
  for (angle_delta = 0; angle_delta <= MAX_ANGLE_DELTA; angle_delta += 2) {
555
0
    for (i = 0; i < 2; ++i) {
556
0
      best_rd_in = (best_rd == INT64_MAX)
557
0
                       ? INT64_MAX
558
0
                       : (best_rd + (best_rd >> ((angle_delta == 0) ? 3 : 5)));
559
0
      mbmi->angle_delta[PLANE_TYPE_UV] = (1 - 2 * i) * angle_delta;
560
0
      this_rd = pick_intra_angle_routine_sbuv(cpi, x, bsize, rate_overhead,
561
0
                                              best_rd_in, rate, rd_stats,
562
0
                                              &best_angle_delta, &best_rd);
563
0
      rd_cost[2 * angle_delta + i] = this_rd;
564
0
      if (angle_delta == 0) {
565
0
        if (this_rd == INT64_MAX) return 0;
566
0
        rd_cost[1] = this_rd;
567
0
        break;
568
0
      }
569
0
    }
570
0
  }
571
572
0
  assert(best_rd != INT64_MAX);
573
0
  for (angle_delta = 1; angle_delta <= MAX_ANGLE_DELTA; angle_delta += 2) {
574
0
    int64_t rd_thresh;
575
0
    for (i = 0; i < 2; ++i) {
576
0
      int skip_search = 0;
577
0
      rd_thresh = best_rd + (best_rd >> 5);
578
0
      if (rd_cost[2 * (angle_delta + 1) + i] > rd_thresh &&
579
0
          rd_cost[2 * (angle_delta - 1) + i] > rd_thresh)
580
0
        skip_search = 1;
581
0
      if (!skip_search) {
582
0
        mbmi->angle_delta[PLANE_TYPE_UV] = (1 - 2 * i) * angle_delta;
583
0
        pick_intra_angle_routine_sbuv(cpi, x, bsize, rate_overhead, best_rd,
584
0
                                      rate, rd_stats, &best_angle_delta,
585
0
                                      &best_rd);
586
0
      }
587
0
    }
588
0
  }
589
590
0
  mbmi->angle_delta[PLANE_TYPE_UV] = best_angle_delta;
591
0
  return rd_stats->rate != INT_MAX;
592
0
}
593
594
#define PLANE_SIGN_TO_JOINT_SIGN(plane, a, b) \
595
0
  (plane == CFL_PRED_U ? a * CFL_SIGNS + b - 1 : b * CFL_SIGNS + a - 1)
596
597
static void cfl_idx_to_sign_and_alpha(int cfl_idx, CFL_SIGN_TYPE *cfl_sign,
598
0
                                      int *cfl_alpha) {
599
0
  int cfl_linear_idx = cfl_idx - CFL_INDEX_ZERO;
600
0
  if (cfl_linear_idx == 0) {
601
0
    *cfl_sign = CFL_SIGN_ZERO;
602
0
    *cfl_alpha = 0;
603
0
  } else {
604
0
    *cfl_sign = cfl_linear_idx > 0 ? CFL_SIGN_POS : CFL_SIGN_NEG;
605
0
    *cfl_alpha = abs(cfl_linear_idx) - 1;
606
0
  }
607
0
}
608
609
static int64_t cfl_compute_rd(const AV1_COMP *const cpi, MACROBLOCK *x,
610
                              int plane, TX_SIZE tx_size,
611
                              BLOCK_SIZE plane_bsize, int cfl_idx,
612
0
                              int fast_mode, RD_STATS *rd_stats) {
613
0
  assert(IMPLIES(fast_mode, rd_stats == NULL));
614
0
  const AV1_COMMON *const cm = &cpi->common;
615
0
  MACROBLOCKD *const xd = &x->e_mbd;
616
0
  MB_MODE_INFO *const mbmi = xd->mi[0];
617
0
  int cfl_plane = get_cfl_pred_type(plane);
618
0
  CFL_SIGN_TYPE cfl_sign;
619
0
  int cfl_alpha;
620
0
  cfl_idx_to_sign_and_alpha(cfl_idx, &cfl_sign, &cfl_alpha);
621
  // We conly build CFL for a given plane, the other plane's sign is dummy
622
0
  int dummy_sign = CFL_SIGN_NEG;
623
0
  const int8_t orig_cfl_alpha_signs = mbmi->cfl_alpha_signs;
624
0
  const uint8_t orig_cfl_alpha_idx = mbmi->cfl_alpha_idx;
625
0
  mbmi->cfl_alpha_signs =
626
0
      PLANE_SIGN_TO_JOINT_SIGN(cfl_plane, cfl_sign, dummy_sign);
627
0
  mbmi->cfl_alpha_idx = (cfl_alpha << CFL_ALPHABET_SIZE_LOG2) + cfl_alpha;
628
0
  int64_t cfl_cost;
629
0
  if (fast_mode) {
630
0
    cfl_cost = intra_model_rd(cm, x, plane, plane_bsize, tx_size,
631
0
                              /*use_hadamard=*/0, cpi->do_border_pad);
632
0
  } else {
633
0
    av1_init_rd_stats(rd_stats);
634
0
    av1_txfm_rd_in_plane(x, cpi, rd_stats, INT64_MAX, 0, plane, plane_bsize,
635
0
                         tx_size, FTXS_NONE);
636
0
    av1_rd_cost_update(x->rdmult, rd_stats);
637
0
    cfl_cost = rd_stats->rdcost;
638
0
  }
639
0
  mbmi->cfl_alpha_signs = orig_cfl_alpha_signs;
640
0
  mbmi->cfl_alpha_idx = orig_cfl_alpha_idx;
641
0
  return cfl_cost;
642
0
}
643
644
static const int cfl_dir_ls[2] = { 1, -1 };
645
646
// If cfl_search_range is CFL_MAGS_SIZE, return zero. Otherwise return the index
647
// of the best alpha found using intra_model_rd().
648
static int cfl_pick_plane_parameter(const AV1_COMP *const cpi, MACROBLOCK *x,
649
                                    int plane, TX_SIZE tx_size,
650
0
                                    int cfl_search_range) {
651
0
  assert(cfl_search_range >= 1 && cfl_search_range <= CFL_MAGS_SIZE);
652
653
0
  if (cfl_search_range == CFL_MAGS_SIZE) return CFL_INDEX_ZERO;
654
655
0
  const MACROBLOCKD *const xd = &x->e_mbd;
656
0
  const MB_MODE_INFO *const mbmi = xd->mi[0];
657
0
  assert(mbmi->uv_mode == UV_CFL_PRED);
658
0
  const MACROBLOCKD_PLANE *pd = &xd->plane[plane];
659
0
  const BLOCK_SIZE plane_bsize =
660
0
      get_plane_block_size(mbmi->bsize, pd->subsampling_x, pd->subsampling_y);
661
662
0
  int est_best_cfl_idx = CFL_INDEX_ZERO;
663
0
  int fast_mode = 1;
664
0
  int start_cfl_idx = CFL_INDEX_ZERO;
665
0
  int64_t best_cfl_cost = cfl_compute_rd(cpi, x, plane, tx_size, plane_bsize,
666
0
                                         start_cfl_idx, fast_mode, NULL);
667
0
  for (int si = 0; si < 2; ++si) {
668
0
    const int dir = cfl_dir_ls[si];
669
0
    for (int i = 1; i < CFL_MAGS_SIZE; ++i) {
670
0
      int cfl_idx = start_cfl_idx + dir * i;
671
0
      if (cfl_idx < 0 || cfl_idx >= CFL_MAGS_SIZE) break;
672
0
      int64_t cfl_cost = cfl_compute_rd(cpi, x, plane, tx_size, plane_bsize,
673
0
                                        cfl_idx, fast_mode, NULL);
674
0
      if (cfl_cost < best_cfl_cost) {
675
0
        best_cfl_cost = cfl_cost;
676
0
        est_best_cfl_idx = cfl_idx;
677
0
      } else {
678
0
        break;
679
0
      }
680
0
    }
681
0
  }
682
0
  return est_best_cfl_idx;
683
0
}
684
685
static inline void set_invalid_cfl_parameters(uint8_t *best_cfl_alpha_idx,
686
0
                                              int8_t *best_cfl_alpha_signs) {
687
0
  *best_cfl_alpha_idx = 0;
688
0
  *best_cfl_alpha_signs = 0;
689
0
}
690
691
static void cfl_pick_plane_rd(const AV1_COMP *const cpi, MACROBLOCK *x,
692
                              int plane, TX_SIZE tx_size, int cfl_search_range,
693
                              RD_STATS cfl_rd_arr[CFL_MAGS_SIZE],
694
0
                              int est_best_cfl_idx) {
695
0
  assert(cfl_search_range >= 1 && cfl_search_range <= CFL_MAGS_SIZE);
696
0
  const MACROBLOCKD *const xd = &x->e_mbd;
697
0
  const MB_MODE_INFO *const mbmi = xd->mi[0];
698
0
  assert(mbmi->uv_mode == UV_CFL_PRED);
699
0
  const MACROBLOCKD_PLANE *pd = &xd->plane[plane];
700
0
  const BLOCK_SIZE plane_bsize =
701
0
      get_plane_block_size(mbmi->bsize, pd->subsampling_x, pd->subsampling_y);
702
703
0
  for (int cfl_idx = 0; cfl_idx < CFL_MAGS_SIZE; ++cfl_idx) {
704
0
    av1_invalid_rd_stats(&cfl_rd_arr[cfl_idx]);
705
0
  }
706
707
0
  int fast_mode = 0;
708
0
  int start_cfl_idx = est_best_cfl_idx;
709
0
  cfl_compute_rd(cpi, x, plane, tx_size, plane_bsize, start_cfl_idx, fast_mode,
710
0
                 &cfl_rd_arr[start_cfl_idx]);
711
712
0
  if (cfl_search_range == 1) return;
713
714
0
  for (int si = 0; si < 2; ++si) {
715
0
    const int dir = cfl_dir_ls[si];
716
0
    for (int i = 1; i < cfl_search_range; ++i) {
717
0
      int cfl_idx = start_cfl_idx + dir * i;
718
0
      if (cfl_idx < 0 || cfl_idx >= CFL_MAGS_SIZE) break;
719
0
      cfl_compute_rd(cpi, x, plane, tx_size, plane_bsize, cfl_idx, fast_mode,
720
0
                     &cfl_rd_arr[cfl_idx]);
721
0
    }
722
0
  }
723
0
}
724
725
/*!\brief Pick the optimal parameters for Chroma to Luma (CFL) component
726
 *
727
 * \ingroup intra_mode_search
728
 * \callergraph
729
 *
730
 * This function will use DCT_DCT followed by computing SATD (sum of absolute
731
 * transformed differences) to estimate the RD score and find the best possible
732
 * CFL parameter.
733
 *
734
 * Then the function will apply a full RD search near the best possible CFL
735
 * parameter to find the best actual CFL parameter.
736
 *
737
 * Side effect:
738
 * We use ths buffers in x->plane[] and xd->plane[] as throw-away buffers for RD
739
 * search.
740
 *
741
 * \param[in] x                Encoder prediction block structure.
742
 * \param[in] cpi              Top-level encoder instance structure.
743
 * \param[in] tx_size          Transform size.
744
 * \param[in] ref_best_rd      Reference best RD.
745
 * \param[in] cfl_search_range The search range of full RD search near the
746
 *                             estimated best CFL parameter.
747
 *
748
 * \param[out]   best_rd_stats          RD stats of the best CFL parameter
749
 * \param[out]   best_cfl_alpha_idx     Best CFL alpha index
750
 * \param[out]   best_cfl_alpha_signs   Best CFL joint signs
751
 *
752
 */
753
static int cfl_rd_pick_alpha(MACROBLOCK *const x, const AV1_COMP *const cpi,
754
                             TX_SIZE tx_size, int64_t ref_best_rd,
755
                             int cfl_search_range, RD_STATS *best_rd_stats,
756
                             uint8_t *best_cfl_alpha_idx,
757
0
                             int8_t *best_cfl_alpha_signs) {
758
0
  assert(cfl_search_range >= 1 && cfl_search_range <= CFL_MAGS_SIZE);
759
0
  const ModeCosts *mode_costs = &x->mode_costs;
760
0
  RD_STATS cfl_rd_arr_u[CFL_MAGS_SIZE];
761
0
  RD_STATS cfl_rd_arr_v[CFL_MAGS_SIZE];
762
0
  MACROBLOCKD *const xd = &x->e_mbd;
763
0
  int est_best_cfl_idx_u, est_best_cfl_idx_v;
764
765
0
  av1_invalid_rd_stats(best_rd_stats);
766
767
  // As the dc pred data is same for different values of alpha, enable the
768
  // caching of dc pred data. Call clear_cfl_dc_pred_cache_flags() before
769
  // returning to avoid the unintentional usage of cached dc pred data.
770
0
  xd->cfl.use_dc_pred_cache = true;
771
  // Evaluate alpha parameter of each chroma plane.
772
0
  est_best_cfl_idx_u =
773
0
      cfl_pick_plane_parameter(cpi, x, 1, tx_size, cfl_search_range);
774
0
  est_best_cfl_idx_v =
775
0
      cfl_pick_plane_parameter(cpi, x, 2, tx_size, cfl_search_range);
776
777
0
  if (cfl_search_range == 1) {
778
    // For cfl_search_range=1, further refinement of alpha is not enabled. Hence
779
    // CfL index=0 for both the chroma planes implies invalid CfL mode.
780
0
    if (est_best_cfl_idx_u == CFL_INDEX_ZERO &&
781
0
        est_best_cfl_idx_v == CFL_INDEX_ZERO) {
782
0
      set_invalid_cfl_parameters(best_cfl_alpha_idx, best_cfl_alpha_signs);
783
0
      clear_cfl_dc_pred_cache_flags(&xd->cfl);
784
0
      return 0;
785
0
    }
786
787
0
    int cfl_alpha_u, cfl_alpha_v;
788
0
    CFL_SIGN_TYPE cfl_sign_u, cfl_sign_v;
789
0
    const MB_MODE_INFO *mbmi = xd->mi[0];
790
0
    cfl_idx_to_sign_and_alpha(est_best_cfl_idx_u, &cfl_sign_u, &cfl_alpha_u);
791
0
    cfl_idx_to_sign_and_alpha(est_best_cfl_idx_v, &cfl_sign_v, &cfl_alpha_v);
792
0
    const int joint_sign = cfl_sign_u * CFL_SIGNS + cfl_sign_v - 1;
793
    // Compute alpha and mode signaling rate.
794
0
    const int rate_overhead =
795
0
        mode_costs->cfl_cost[joint_sign][CFL_PRED_U][cfl_alpha_u] +
796
0
        mode_costs->cfl_cost[joint_sign][CFL_PRED_V][cfl_alpha_v] +
797
0
        mode_costs
798
0
            ->intra_uv_mode_cost[is_cfl_allowed(xd)][mbmi->mode][UV_CFL_PRED];
799
    // Skip the CfL mode evaluation if the RD cost derived using the rate needed
800
    // to signal the CfL mode and alpha parameter exceeds the ref_best_rd.
801
0
    if (RDCOST(x->rdmult, rate_overhead, 0) > ref_best_rd) {
802
0
      set_invalid_cfl_parameters(best_cfl_alpha_idx, best_cfl_alpha_signs);
803
0
      clear_cfl_dc_pred_cache_flags(&xd->cfl);
804
0
      return 0;
805
0
    }
806
0
  }
807
808
  // Compute the rd cost of each chroma plane using the alpha parameters which
809
  // were already evaluated.
810
0
  cfl_pick_plane_rd(cpi, x, 1, tx_size, cfl_search_range, cfl_rd_arr_u,
811
0
                    est_best_cfl_idx_u);
812
0
  cfl_pick_plane_rd(cpi, x, 2, tx_size, cfl_search_range, cfl_rd_arr_v,
813
0
                    est_best_cfl_idx_v);
814
815
0
  clear_cfl_dc_pred_cache_flags(&xd->cfl);
816
817
0
  for (int ui = 0; ui < CFL_MAGS_SIZE; ++ui) {
818
0
    if (cfl_rd_arr_u[ui].rate == INT_MAX) continue;
819
0
    int cfl_alpha_u;
820
0
    CFL_SIGN_TYPE cfl_sign_u;
821
0
    cfl_idx_to_sign_and_alpha(ui, &cfl_sign_u, &cfl_alpha_u);
822
0
    for (int vi = 0; vi < CFL_MAGS_SIZE; ++vi) {
823
0
      if (cfl_rd_arr_v[vi].rate == INT_MAX) continue;
824
0
      int cfl_alpha_v;
825
0
      CFL_SIGN_TYPE cfl_sign_v;
826
0
      cfl_idx_to_sign_and_alpha(vi, &cfl_sign_v, &cfl_alpha_v);
827
      // cfl_sign_u == CFL_SIGN_ZERO && cfl_sign_v == CFL_SIGN_ZERO is not a
828
      // valid parameter for CFL
829
0
      if (cfl_sign_u == CFL_SIGN_ZERO && cfl_sign_v == CFL_SIGN_ZERO) continue;
830
0
      int joint_sign = cfl_sign_u * CFL_SIGNS + cfl_sign_v - 1;
831
0
      RD_STATS rd_stats = cfl_rd_arr_u[ui];
832
0
      av1_merge_rd_stats(&rd_stats, &cfl_rd_arr_v[vi]);
833
0
      if (rd_stats.rate != INT_MAX) {
834
0
        rd_stats.rate +=
835
0
            mode_costs->cfl_cost[joint_sign][CFL_PRED_U][cfl_alpha_u];
836
0
        rd_stats.rate +=
837
0
            mode_costs->cfl_cost[joint_sign][CFL_PRED_V][cfl_alpha_v];
838
0
      }
839
0
      av1_rd_cost_update(x->rdmult, &rd_stats);
840
0
      if (rd_stats.rdcost < best_rd_stats->rdcost) {
841
0
        *best_rd_stats = rd_stats;
842
0
        *best_cfl_alpha_idx =
843
0
            (cfl_alpha_u << CFL_ALPHABET_SIZE_LOG2) + cfl_alpha_v;
844
0
        *best_cfl_alpha_signs = joint_sign;
845
0
      }
846
0
    }
847
0
  }
848
0
  if (best_rd_stats->rdcost >= ref_best_rd) {
849
0
    av1_invalid_rd_stats(best_rd_stats);
850
    // Set invalid CFL parameters here since the rdcost is not better than
851
    // ref_best_rd.
852
0
    set_invalid_cfl_parameters(best_cfl_alpha_idx, best_cfl_alpha_signs);
853
0
    return 0;
854
0
  }
855
0
  return 1;
856
0
}
857
858
static bool should_prune_chroma_smooth_pred_based_on_source_variance(
859
0
    const AV1_COMP *cpi, const MACROBLOCK *x, BLOCK_SIZE bsize) {
860
0
  if (!cpi->sf.intra_sf.prune_smooth_intra_mode_for_chroma) return false;
861
862
  // If the source variance of both chroma planes is less than 20 (empirically
863
  // derived), prune UV_SMOOTH_PRED.
864
0
  for (int i = AOM_PLANE_U; i < av1_num_planes(&cpi->common); i++) {
865
0
    const unsigned int variance = av1_get_perpixel_variance_facade(
866
0
        cpi, &x->e_mbd, &x->plane[i].src, bsize, i);
867
0
    if (variance >= 20) return false;
868
0
  }
869
0
  return true;
870
0
}
871
872
int64_t av1_rd_pick_intra_sbuv_mode(const AV1_COMP *const cpi, MACROBLOCK *x,
873
                                    int *rate, int *rate_tokenonly,
874
                                    int64_t *distortion, uint8_t *skippable,
875
0
                                    BLOCK_SIZE bsize, TX_SIZE max_tx_size) {
876
0
  const AV1_COMMON *const cm = &cpi->common;
877
0
  MACROBLOCKD *xd = &x->e_mbd;
878
0
  MB_MODE_INFO *mbmi = xd->mi[0];
879
0
  assert(!is_inter_block(mbmi));
880
0
  MB_MODE_INFO best_mbmi = *mbmi;
881
0
  int64_t best_rd = INT64_MAX, this_rd;
882
0
  const ModeCosts *mode_costs = &x->mode_costs;
883
0
  const IntraModeCfg *const intra_mode_cfg = &cpi->oxcf.intra_mode_cfg;
884
885
0
  init_sbuv_mode(mbmi);
886
887
  // Return if the current block does not correspond to a chroma block.
888
0
  if (!xd->is_chroma_ref) {
889
0
    *rate = 0;
890
0
    *rate_tokenonly = 0;
891
0
    *distortion = 0;
892
0
    *skippable = 1;
893
0
    return INT64_MAX;
894
0
  }
895
896
  // Only store reconstructed luma when there's chroma RDO. When there's no
897
  // chroma RDO, the reconstructed luma will be stored in encode_superblock().
898
0
  xd->cfl.store_y = store_cfl_required_rdo(cm, x);
899
0
  if (xd->cfl.store_y) {
900
    // Restore reconstructed luma values.
901
    // TODO(chiyotsai@google.com): right now we are re-computing the txfm in
902
    // this function everytime we search through uv modes. There is some
903
    // potential speed up here if we cache the result to avoid redundant
904
    // computation.
905
0
    av1_encode_intra_block_plane(cpi, x, mbmi->bsize, AOM_PLANE_Y,
906
0
                                 DRY_RUN_NORMAL,
907
0
                                 cpi->optimize_seg_arr[mbmi->segment_id]);
908
0
    xd->cfl.store_y = 0;
909
0
  }
910
0
  IntraModeSearchState intra_search_state;
911
0
  init_intra_mode_search_state(&intra_search_state);
912
0
  const CFL_ALLOWED_TYPE cfl_allowed = is_cfl_allowed(xd);
913
914
  // Search through all non-palette modes.
915
0
  for (int mode_idx = 0; mode_idx < UV_INTRA_MODES; ++mode_idx) {
916
0
    int this_rate;
917
0
    RD_STATS tokenonly_rd_stats;
918
0
    UV_PREDICTION_MODE uv_mode = uv_rd_search_mode_order[mode_idx];
919
920
    // Skip the current mode evaluation if the RD cost derived using the mode
921
    // signaling rate exceeds the best_rd so far.
922
0
    const int mode_rate =
923
0
        mode_costs->intra_uv_mode_cost[cfl_allowed][mbmi->mode][uv_mode];
924
0
    if (RDCOST(x->rdmult, mode_rate, 0) > best_rd) continue;
925
926
0
    PREDICTION_MODE intra_mode = get_uv_mode(uv_mode);
927
0
    const int is_diagonal_mode = av1_is_diagonal_mode(intra_mode);
928
0
    const int is_directional_mode = av1_is_directional_mode(intra_mode);
929
930
0
    if (is_diagonal_mode && !cpi->oxcf.intra_mode_cfg.enable_diagonal_intra)
931
0
      continue;
932
0
    if (is_directional_mode &&
933
0
        !cpi->oxcf.intra_mode_cfg.enable_directional_intra)
934
0
      continue;
935
936
0
    if (!(cpi->sf.intra_sf.intra_uv_mode_mask[txsize_sqr_up_map[max_tx_size]] &
937
0
          (1 << uv_mode)))
938
0
      continue;
939
0
    if (!intra_mode_cfg->enable_smooth_intra && uv_mode >= UV_SMOOTH_PRED &&
940
0
        uv_mode <= UV_SMOOTH_H_PRED)
941
0
      continue;
942
943
0
    if (!intra_mode_cfg->enable_paeth_intra && uv_mode == UV_PAETH_PRED)
944
0
      continue;
945
946
0
    assert(mbmi->mode < INTRA_MODES);
947
0
    if (cpi->sf.intra_sf.prune_chroma_modes_using_luma_winner &&
948
0
        !(av1_derived_chroma_intra_mode_used_flag[mbmi->mode] & (1 << uv_mode)))
949
0
      continue;
950
951
0
    mbmi->uv_mode = uv_mode;
952
953
    // Init variables for cfl and angle delta
954
0
    const SPEED_FEATURES *sf = &cpi->sf;
955
0
    mbmi->angle_delta[PLANE_TYPE_UV] = 0;
956
0
    if (uv_mode == UV_CFL_PRED) {
957
0
      if (!cfl_allowed || !intra_mode_cfg->enable_cfl_intra) continue;
958
0
      assert(!is_directional_mode);
959
0
      const TX_SIZE uv_tx_size = av1_get_tx_size(AOM_PLANE_U, xd);
960
0
      if (!cfl_rd_pick_alpha(x, cpi, uv_tx_size, best_rd,
961
0
                             sf->intra_sf.cfl_search_range, &tokenonly_rd_stats,
962
0
                             &mbmi->cfl_alpha_idx, &mbmi->cfl_alpha_signs)) {
963
0
        continue;
964
0
      }
965
0
    } else if (is_directional_mode && av1_use_angle_delta(mbmi->bsize) &&
966
0
               intra_mode_cfg->enable_angle_delta) {
967
0
      if (sf->intra_sf.chroma_intra_pruning_with_hog &&
968
0
          !intra_search_state.dir_mode_skip_mask_ready) {
969
0
        static const float thresh[2][4] = {
970
0
          { -1.2f, 0.0f, 0.0f, 1.2f },    // Interframe
971
0
          { -1.2f, -1.2f, -0.6f, 0.4f },  // Intraframe
972
0
        };
973
0
        const int is_chroma = 1;
974
0
        const int is_intra_frame = frame_is_intra_only(cm);
975
0
        prune_intra_mode_with_hog(
976
0
            x, bsize, cm->seq_params->sb_size,
977
0
            thresh[is_intra_frame]
978
0
                  [sf->intra_sf.chroma_intra_pruning_with_hog - 1],
979
0
            intra_search_state.directional_mode_skip_mask, is_chroma);
980
0
        intra_search_state.dir_mode_skip_mask_ready = 1;
981
0
      }
982
0
      if (intra_search_state.directional_mode_skip_mask[uv_mode]) {
983
0
        continue;
984
0
      }
985
986
      // Search through angle delta
987
0
      const int rate_overhead =
988
0
          mode_costs->intra_uv_mode_cost[cfl_allowed][mbmi->mode][uv_mode];
989
0
      if (!rd_pick_intra_angle_sbuv(cpi, x, bsize, rate_overhead, best_rd,
990
0
                                    &this_rate, &tokenonly_rd_stats))
991
0
        continue;
992
0
    } else {
993
0
      if (uv_mode == UV_SMOOTH_PRED &&
994
0
          should_prune_chroma_smooth_pred_based_on_source_variance(cpi, x,
995
0
                                                                   bsize))
996
0
        continue;
997
998
      // Predict directly if we don't need to search for angle delta.
999
0
      if (!av1_txfm_uvrd(cpi, x, &tokenonly_rd_stats, bsize, best_rd)) {
1000
0
        continue;
1001
0
      }
1002
0
    }
1003
0
    const int mode_cost =
1004
0
        mode_costs->intra_uv_mode_cost[cfl_allowed][mbmi->mode][uv_mode];
1005
0
    this_rate = tokenonly_rd_stats.rate +
1006
0
                intra_mode_info_cost_uv(cpi, x, mbmi, bsize, mode_cost);
1007
0
    this_rd = RDCOST(x->rdmult, this_rate, tokenonly_rd_stats.dist);
1008
1009
0
#if CONFIG_AV1_HIGHBITDEPTH
1010
0
    if (xd->bd > 8 && cpi->oxcf.algo_cfg.sharpness == 3) {
1011
0
      const int is_smooth_uv_mode =
1012
0
          (uv_mode == UV_SMOOTH_PRED || uv_mode == UV_SMOOTH_V_PRED ||
1013
0
           uv_mode == UV_SMOOTH_H_PRED);
1014
0
      if (is_smooth_uv_mode) {
1015
0
        if (this_rd < INT64_MAX) {
1016
0
          this_rd += (this_rd >> 2);
1017
0
        }
1018
0
      }
1019
0
    }
1020
0
#endif
1021
1022
0
    if (this_rd < best_rd) {
1023
0
      best_mbmi = *mbmi;
1024
0
      best_rd = this_rd;
1025
0
      *rate = this_rate;
1026
0
      *rate_tokenonly = tokenonly_rd_stats.rate;
1027
0
      *distortion = tokenonly_rd_stats.dist;
1028
0
      *skippable = tokenonly_rd_stats.skip_txfm;
1029
0
    }
1030
0
  }
1031
1032
  // Search palette mode
1033
0
  const int try_palette =
1034
0
      cpi->oxcf.tool_cfg.enable_palette &&
1035
0
      av1_allow_palette(cpi->common.features.allow_screen_content_tools,
1036
0
                        mbmi->bsize);
1037
0
  if (try_palette) {
1038
0
    uint8_t *best_palette_color_map = x->palette_buffer->best_palette_color_map;
1039
0
    av1_rd_pick_palette_intra_sbuv(
1040
0
        cpi, x,
1041
0
        mode_costs->intra_uv_mode_cost[cfl_allowed][mbmi->mode][UV_DC_PRED],
1042
0
        best_palette_color_map, &best_mbmi, &best_rd, rate, rate_tokenonly,
1043
0
        distortion, skippable);
1044
0
  }
1045
1046
0
  *mbmi = best_mbmi;
1047
  // Make sure we actually chose a mode
1048
0
  assert(best_rd < INT64_MAX);
1049
0
  return best_rd;
1050
0
}
1051
1052
// Searches palette mode for luma channel in inter frame.
1053
int av1_search_palette_mode(IntraModeSearchState *intra_search_state,
1054
                            const AV1_COMP *cpi, MACROBLOCK *x,
1055
                            BLOCK_SIZE bsize, unsigned int ref_frame_cost,
1056
                            PICK_MODE_CONTEXT *ctx, RD_STATS *this_rd_cost,
1057
0
                            int64_t best_rd) {
1058
0
  const AV1_COMMON *const cm = &cpi->common;
1059
0
  MB_MODE_INFO *const mbmi = x->e_mbd.mi[0];
1060
0
  PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info;
1061
0
  const int num_planes = av1_num_planes(cm);
1062
0
  MACROBLOCKD *const xd = &x->e_mbd;
1063
0
  int rate2 = 0;
1064
0
  int64_t distortion2 = 0, best_rd_palette = best_rd, this_rd;
1065
0
  int skippable = 0;
1066
0
  uint8_t *const best_palette_color_map =
1067
0
      x->palette_buffer->best_palette_color_map;
1068
0
  uint8_t *const color_map = xd->plane[0].color_index_map;
1069
0
  MB_MODE_INFO best_mbmi_palette = *mbmi;
1070
0
  uint8_t best_tx_type_map[MAX_MIB_SIZE * MAX_MIB_SIZE];
1071
0
  const ModeCosts *mode_costs = &x->mode_costs;
1072
0
  const int *const intra_mode_cost =
1073
0
      mode_costs->mbmode_cost[size_group_lookup[bsize]];
1074
0
  const int rows = block_size_high[bsize];
1075
0
  const int cols = block_size_wide[bsize];
1076
1077
0
  mbmi->mode = DC_PRED;
1078
0
  mbmi->uv_mode = UV_DC_PRED;
1079
0
  mbmi->ref_frame[0] = INTRA_FRAME;
1080
0
  mbmi->ref_frame[1] = NONE_FRAME;
1081
0
  av1_zero(pmi->palette_size);
1082
1083
0
  RD_STATS rd_stats_y;
1084
0
  av1_invalid_rd_stats(&rd_stats_y);
1085
0
  av1_rd_pick_palette_intra_sby(
1086
0
      cpi, x, bsize, intra_mode_cost[DC_PRED], &best_mbmi_palette,
1087
0
      best_palette_color_map, &best_rd_palette, &rd_stats_y.rate, NULL,
1088
0
      &rd_stats_y.dist, &rd_stats_y.skip_txfm, NULL, ctx, best_tx_type_map);
1089
0
  if (rd_stats_y.rate == INT_MAX || pmi->palette_size[0] == 0) {
1090
0
    this_rd_cost->rdcost = INT64_MAX;
1091
0
    return skippable;
1092
0
  }
1093
1094
0
  av1_copy_array(xd->tx_type_map, best_tx_type_map, ctx->num_4x4_blk);
1095
0
  memcpy(color_map, best_palette_color_map,
1096
0
         rows * cols * sizeof(best_palette_color_map[0]));
1097
1098
0
  skippable = rd_stats_y.skip_txfm;
1099
0
  distortion2 = rd_stats_y.dist;
1100
0
  rate2 = rd_stats_y.rate + ref_frame_cost;
1101
0
  if (num_planes > 1) {
1102
0
    if (intra_search_state->rate_uv_intra == INT_MAX) {
1103
      // We have not found any good uv mode yet, so we need to search for it.
1104
0
      TX_SIZE uv_tx = av1_get_tx_size(AOM_PLANE_U, xd);
1105
0
      av1_rd_pick_intra_sbuv_mode(cpi, x, &intra_search_state->rate_uv_intra,
1106
0
                                  &intra_search_state->rate_uv_tokenonly,
1107
0
                                  &intra_search_state->dist_uvs,
1108
0
                                  &intra_search_state->skip_uvs, bsize, uv_tx);
1109
0
      intra_search_state->mode_uv = mbmi->uv_mode;
1110
0
      intra_search_state->pmi_uv = *pmi;
1111
0
      intra_search_state->uv_angle_delta = mbmi->angle_delta[PLANE_TYPE_UV];
1112
0
    }
1113
1114
    // We have found at least one good uv mode before, so copy and paste it
1115
    // over.
1116
0
    mbmi->uv_mode = intra_search_state->mode_uv;
1117
0
    pmi->palette_size[1] = intra_search_state->pmi_uv.palette_size[1];
1118
0
    if (pmi->palette_size[1] > 0) {
1119
0
      memcpy(pmi->palette_colors + PALETTE_MAX_SIZE,
1120
0
             intra_search_state->pmi_uv.palette_colors + PALETTE_MAX_SIZE,
1121
0
             2 * PALETTE_MAX_SIZE * sizeof(pmi->palette_colors[0]));
1122
0
    }
1123
0
    mbmi->angle_delta[PLANE_TYPE_UV] = intra_search_state->uv_angle_delta;
1124
0
    skippable = skippable && intra_search_state->skip_uvs;
1125
0
    distortion2 += intra_search_state->dist_uvs;
1126
0
    rate2 += intra_search_state->rate_uv_intra;
1127
0
  }
1128
1129
0
  if (skippable) {
1130
0
    rate2 -= rd_stats_y.rate;
1131
0
    if (num_planes > 1) rate2 -= intra_search_state->rate_uv_tokenonly;
1132
0
    rate2 += mode_costs->skip_txfm_cost[av1_get_skip_txfm_context(xd)][1];
1133
0
  } else {
1134
0
    rate2 += mode_costs->skip_txfm_cost[av1_get_skip_txfm_context(xd)][0];
1135
0
  }
1136
0
  this_rd = RDCOST(x->rdmult, rate2, distortion2);
1137
0
  this_rd_cost->rate = rate2;
1138
0
  this_rd_cost->dist = distortion2;
1139
0
  this_rd_cost->rdcost = this_rd;
1140
0
  return skippable;
1141
0
}
1142
1143
void av1_search_palette_mode_luma(const AV1_COMP *cpi, MACROBLOCK *x,
1144
                                  BLOCK_SIZE bsize, unsigned int ref_frame_cost,
1145
                                  PICK_MODE_CONTEXT *ctx,
1146
0
                                  RD_STATS *this_rd_cost, int64_t best_rd) {
1147
0
  MB_MODE_INFO *const mbmi = x->e_mbd.mi[0];
1148
0
  PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info;
1149
0
  MACROBLOCKD *const xd = &x->e_mbd;
1150
0
  int64_t best_rd_palette = best_rd, this_rd;
1151
0
  uint8_t *const best_palette_color_map =
1152
0
      x->palette_buffer->best_palette_color_map;
1153
0
  uint8_t *const color_map = xd->plane[0].color_index_map;
1154
0
  MB_MODE_INFO best_mbmi_palette = *mbmi;
1155
0
  uint8_t best_tx_type_map[MAX_MIB_SIZE * MAX_MIB_SIZE];
1156
0
  const ModeCosts *mode_costs = &x->mode_costs;
1157
0
  const int *const intra_mode_cost =
1158
0
      mode_costs->mbmode_cost[size_group_lookup[bsize]];
1159
0
  const int rows = block_size_high[bsize];
1160
0
  const int cols = block_size_wide[bsize];
1161
1162
0
  mbmi->mode = DC_PRED;
1163
0
  mbmi->uv_mode = UV_DC_PRED;
1164
0
  mbmi->ref_frame[0] = INTRA_FRAME;
1165
0
  mbmi->ref_frame[1] = NONE_FRAME;
1166
0
  av1_zero(pmi->palette_size);
1167
1168
0
  RD_STATS rd_stats_y;
1169
0
  av1_invalid_rd_stats(&rd_stats_y);
1170
0
  av1_rd_pick_palette_intra_sby(
1171
0
      cpi, x, bsize, intra_mode_cost[DC_PRED], &best_mbmi_palette,
1172
0
      best_palette_color_map, &best_rd_palette, &rd_stats_y.rate, NULL,
1173
0
      &rd_stats_y.dist, &rd_stats_y.skip_txfm, NULL, ctx, best_tx_type_map);
1174
0
  if (rd_stats_y.rate == INT_MAX || pmi->palette_size[0] == 0) {
1175
0
    this_rd_cost->rdcost = INT64_MAX;
1176
0
    return;
1177
0
  }
1178
1179
0
  av1_copy_array(xd->tx_type_map, best_tx_type_map, ctx->num_4x4_blk);
1180
0
  memcpy(color_map, best_palette_color_map,
1181
0
         rows * cols * sizeof(best_palette_color_map[0]));
1182
1183
0
  rd_stats_y.rate += ref_frame_cost;
1184
1185
0
  if (rd_stats_y.skip_txfm) {
1186
0
    rd_stats_y.rate =
1187
0
        ref_frame_cost +
1188
0
        mode_costs->skip_txfm_cost[av1_get_skip_txfm_context(xd)][1];
1189
0
  } else {
1190
0
    rd_stats_y.rate +=
1191
0
        mode_costs->skip_txfm_cost[av1_get_skip_txfm_context(xd)][0];
1192
0
  }
1193
0
  this_rd = RDCOST(x->rdmult, rd_stats_y.rate, rd_stats_y.dist);
1194
0
  this_rd_cost->rate = rd_stats_y.rate;
1195
0
  this_rd_cost->dist = rd_stats_y.dist;
1196
0
  this_rd_cost->rdcost = this_rd;
1197
0
  this_rd_cost->skip_txfm = rd_stats_y.skip_txfm;
1198
0
}
1199
1200
/*!\brief Get the intra prediction by searching through tx_type and tx_size.
1201
 *
1202
 * \ingroup intra_mode_search
1203
 * \callergraph
1204
 * Currently this function is only used in the intra frame code path for
1205
 * winner-mode processing.
1206
 *
1207
 * \return Returns whether the current mode is an improvement over best_rd.
1208
 */
1209
static inline int intra_block_yrd(const AV1_COMP *const cpi, MACROBLOCK *x,
1210
                                  BLOCK_SIZE bsize, const int *bmode_costs,
1211
                                  int64_t *best_rd, int *rate,
1212
                                  int *rate_tokenonly, int64_t *distortion,
1213
                                  uint8_t *skippable, MB_MODE_INFO *best_mbmi,
1214
0
                                  PICK_MODE_CONTEXT *ctx) {
1215
0
  MACROBLOCKD *const xd = &x->e_mbd;
1216
0
  MB_MODE_INFO *const mbmi = xd->mi[0];
1217
0
  RD_STATS rd_stats;
1218
  // In order to improve txfm search, avoid rd based breakouts during winner
1219
  // mode evaluation. Hence passing ref_best_rd as INT64_MAX by default when the
1220
  // speed feature use_rd_based_breakout_for_intra_tx_search is disabled.
1221
0
  int64_t ref_best_rd = cpi->sf.tx_sf.use_rd_based_breakout_for_intra_tx_search
1222
0
                            ? *best_rd
1223
0
                            : INT64_MAX;
1224
0
  av1_pick_uniform_tx_size_type_yrd(cpi, x, &rd_stats, bsize, ref_best_rd);
1225
0
  if (rd_stats.rate == INT_MAX) return 0;
1226
0
  int this_rate_tokenonly = rd_stats.rate;
1227
0
  if (!xd->lossless[mbmi->segment_id] && block_signals_txsize(mbmi->bsize)) {
1228
    // av1_pick_uniform_tx_size_type_yrd above includes the cost of the tx_size
1229
    // in the tokenonly rate, but for intra blocks, tx_size is always coded
1230
    // (prediction granularity), so we account for it in the full rate,
1231
    // not the tokenonly rate.
1232
0
    this_rate_tokenonly -= tx_size_cost(x, bsize, mbmi->tx_size);
1233
0
  }
1234
0
  const int this_rate =
1235
0
      rd_stats.rate +
1236
0
      intra_mode_info_cost_y(cpi, x, mbmi, bsize, bmode_costs[mbmi->mode], 0);
1237
0
  const int64_t this_rd = RDCOST(x->rdmult, this_rate, rd_stats.dist);
1238
0
  if (this_rd < *best_rd) {
1239
0
    *best_mbmi = *mbmi;
1240
0
    *best_rd = this_rd;
1241
0
    *rate = this_rate;
1242
0
    *rate_tokenonly = this_rate_tokenonly;
1243
0
    *distortion = rd_stats.dist;
1244
0
    *skippable = rd_stats.skip_txfm;
1245
0
    av1_copy_array(ctx->tx_type_map, xd->tx_type_map, ctx->num_4x4_blk);
1246
0
    return 1;
1247
0
  }
1248
0
  return 0;
1249
0
}
1250
1251
/*!\brief Search for the best filter_intra mode when coding inter frame.
1252
 *
1253
 * \ingroup intra_mode_search
1254
 * \callergraph
1255
 * This function loops through all filter_intra modes to find the best one.
1256
 *
1257
 * \remark Returns nothing, but updates the mbmi and rd_stats.
1258
 */
1259
static inline void handle_filter_intra_mode(const AV1_COMP *cpi, MACROBLOCK *x,
1260
                                            BLOCK_SIZE bsize,
1261
                                            const PICK_MODE_CONTEXT *ctx,
1262
                                            RD_STATS *rd_stats_y, int mode_cost,
1263
                                            int64_t best_rd,
1264
0
                                            int64_t best_rd_so_far) {
1265
0
  MACROBLOCKD *const xd = &x->e_mbd;
1266
0
  MB_MODE_INFO *const mbmi = xd->mi[0];
1267
0
  assert(mbmi->mode == DC_PRED &&
1268
0
         av1_filter_intra_allowed_bsize(&cpi->common, bsize));
1269
1270
0
  RD_STATS rd_stats_y_fi;
1271
0
  int filter_intra_selected_flag = 0;
1272
0
  TX_SIZE best_tx_size = mbmi->tx_size;
1273
0
  FILTER_INTRA_MODE best_fi_mode = FILTER_DC_PRED;
1274
0
  uint8_t best_tx_type_map[MAX_MIB_SIZE * MAX_MIB_SIZE];
1275
0
  av1_copy_array(best_tx_type_map, xd->tx_type_map, ctx->num_4x4_blk);
1276
0
  mbmi->filter_intra_mode_info.use_filter_intra = 1;
1277
0
  for (FILTER_INTRA_MODE fi_mode = FILTER_DC_PRED; fi_mode < FILTER_INTRA_MODES;
1278
0
       ++fi_mode) {
1279
0
    mbmi->filter_intra_mode_info.filter_intra_mode = fi_mode;
1280
0
    av1_pick_uniform_tx_size_type_yrd(cpi, x, &rd_stats_y_fi, bsize, best_rd);
1281
0
    if (rd_stats_y_fi.rate == INT_MAX) continue;
1282
0
    const int this_rate_tmp =
1283
0
        rd_stats_y_fi.rate +
1284
0
        intra_mode_info_cost_y(cpi, x, mbmi, bsize, mode_cost, 0);
1285
0
    const int64_t this_rd_tmp =
1286
0
        RDCOST(x->rdmult, this_rate_tmp, rd_stats_y_fi.dist);
1287
1288
0
    if (this_rd_tmp != INT64_MAX && this_rd_tmp / 2 > best_rd) {
1289
0
      break;
1290
0
    }
1291
0
    if (this_rd_tmp < best_rd_so_far) {
1292
0
      best_tx_size = mbmi->tx_size;
1293
0
      av1_copy_array(best_tx_type_map, xd->tx_type_map, ctx->num_4x4_blk);
1294
0
      best_fi_mode = fi_mode;
1295
0
      *rd_stats_y = rd_stats_y_fi;
1296
0
      filter_intra_selected_flag = 1;
1297
0
      best_rd_so_far = this_rd_tmp;
1298
0
    }
1299
0
  }
1300
1301
0
  mbmi->tx_size = best_tx_size;
1302
0
  av1_copy_array(xd->tx_type_map, best_tx_type_map, ctx->num_4x4_blk);
1303
1304
0
  if (filter_intra_selected_flag) {
1305
0
    mbmi->filter_intra_mode_info.use_filter_intra = 1;
1306
0
    mbmi->filter_intra_mode_info.filter_intra_mode = best_fi_mode;
1307
0
  } else {
1308
0
    mbmi->filter_intra_mode_info.use_filter_intra = 0;
1309
0
  }
1310
0
}
1311
1312
// Evaluate a given luma intra-mode in inter frames.
1313
int av1_handle_intra_y_mode(IntraModeSearchState *intra_search_state,
1314
                            const AV1_COMP *cpi, MACROBLOCK *x,
1315
                            BLOCK_SIZE bsize, unsigned int ref_frame_cost,
1316
                            const PICK_MODE_CONTEXT *ctx, RD_STATS *rd_stats_y,
1317
                            int64_t best_rd, int *mode_cost_y, int64_t *rd_y,
1318
                            int64_t *best_model_rd,
1319
0
                            int64_t top_intra_model_rd[]) {
1320
0
  const AV1_COMMON *cm = &cpi->common;
1321
0
  const INTRA_MODE_SPEED_FEATURES *const intra_sf = &cpi->sf.intra_sf;
1322
0
  MACROBLOCKD *const xd = &x->e_mbd;
1323
0
  MB_MODE_INFO *const mbmi = xd->mi[0];
1324
0
  assert(mbmi->ref_frame[0] == INTRA_FRAME);
1325
0
  const PREDICTION_MODE mode = mbmi->mode;
1326
0
  const ModeCosts *mode_costs = &x->mode_costs;
1327
0
  const int mode_cost =
1328
0
      mode_costs->mbmode_cost[size_group_lookup[bsize]][mode] + ref_frame_cost;
1329
0
  const int skip_ctx = av1_get_skip_txfm_context(xd);
1330
0
  int known_rate = mode_cost + mode_costs->skip_txfm_cost[skip_ctx][0];
1331
0
  const int64_t known_rd = RDCOST(x->rdmult, known_rate, 0);
1332
0
  if (known_rd > best_rd) {
1333
0
    intra_search_state->skip_intra_modes = 1;
1334
0
    return 0;
1335
0
  }
1336
1337
0
  const int is_directional_mode = av1_is_directional_mode(mode);
1338
0
  if (is_directional_mode && av1_use_angle_delta(bsize) &&
1339
0
      cpi->oxcf.intra_mode_cfg.enable_angle_delta) {
1340
0
    if (intra_sf->intra_pruning_with_hog &&
1341
0
        !intra_search_state->dir_mode_skip_mask_ready) {
1342
0
      const float thresh[4] = { -1.2f, 0.0f, 0.0f, 1.2f };
1343
0
      const int is_chroma = 0;
1344
0
      prune_intra_mode_with_hog(x, bsize, cm->seq_params->sb_size,
1345
0
                                thresh[intra_sf->intra_pruning_with_hog - 1],
1346
0
                                intra_search_state->directional_mode_skip_mask,
1347
0
                                is_chroma);
1348
0
      intra_search_state->dir_mode_skip_mask_ready = 1;
1349
0
    }
1350
0
    if (intra_search_state->directional_mode_skip_mask[mode]) return 0;
1351
0
  }
1352
0
  const TX_SIZE tx_size = AOMMIN(TX_32X32, max_txsize_lookup[bsize]);
1353
0
  const int64_t this_model_rd =
1354
0
      intra_model_rd(&cpi->common, x, 0, bsize, tx_size, /*use_hadamard=*/1,
1355
0
                     cpi->do_border_pad);
1356
1357
0
  const int model_rd_index_for_pruning =
1358
0
      get_model_rd_index_for_pruning(x, intra_sf);
1359
1360
0
  if (prune_intra_y_mode(this_model_rd, best_model_rd, top_intra_model_rd,
1361
0
                         intra_sf->top_intra_model_count_allowed,
1362
0
                         model_rd_index_for_pruning))
1363
0
    return 0;
1364
0
  av1_init_rd_stats(rd_stats_y);
1365
0
  av1_pick_uniform_tx_size_type_yrd(cpi, x, rd_stats_y, bsize, best_rd);
1366
1367
  // Pick filter intra modes.
1368
0
  if (mode == DC_PRED && av1_filter_intra_allowed_bsize(cm, bsize)) {
1369
0
    int try_filter_intra = 1;
1370
0
    int64_t best_rd_so_far = INT64_MAX;
1371
0
    if (rd_stats_y->rate != INT_MAX) {
1372
      // best_rd_so_far is the rdcost of DC_PRED without using filter_intra.
1373
      // Later, in filter intra search, best_rd_so_far is used for comparison.
1374
0
      mbmi->filter_intra_mode_info.use_filter_intra = 0;
1375
0
      const int tmp_rate =
1376
0
          rd_stats_y->rate +
1377
0
          intra_mode_info_cost_y(cpi, x, mbmi, bsize, mode_cost, 0);
1378
0
      best_rd_so_far = RDCOST(x->rdmult, tmp_rate, rd_stats_y->dist);
1379
0
      try_filter_intra = (best_rd_so_far / 2) <= best_rd;
1380
0
    } else if (intra_sf->skip_filter_intra_in_inter_frames >= 1) {
1381
      // As rd cost of luma intra dc mode is more than best_rd (i.e.,
1382
      // rd_stats_y->rate = INT_MAX), skip the evaluation of filter intra modes.
1383
0
      try_filter_intra = 0;
1384
0
    }
1385
1386
0
    if (try_filter_intra) {
1387
0
      handle_filter_intra_mode(cpi, x, bsize, ctx, rd_stats_y, mode_cost,
1388
0
                               best_rd, best_rd_so_far);
1389
0
    }
1390
0
  }
1391
1392
0
  if (rd_stats_y->rate == INT_MAX) return 0;
1393
1394
0
  *mode_cost_y = intra_mode_info_cost_y(cpi, x, mbmi, bsize, mode_cost, 0);
1395
0
  const int rate_y = rd_stats_y->skip_txfm
1396
0
                         ? mode_costs->skip_txfm_cost[skip_ctx][1]
1397
0
                         : rd_stats_y->rate;
1398
0
  *rd_y = RDCOST(x->rdmult, rate_y + *mode_cost_y, rd_stats_y->dist);
1399
0
  if (best_rd < (INT64_MAX / 2) && *rd_y > (best_rd + (best_rd >> 2))) {
1400
0
    intra_search_state->skip_intra_modes = 1;
1401
0
    return 0;
1402
0
  }
1403
1404
0
  return 1;
1405
0
}
1406
1407
int av1_search_intra_uv_modes_in_interframe(
1408
    IntraModeSearchState *intra_search_state, const AV1_COMP *cpi,
1409
    MACROBLOCK *x, BLOCK_SIZE bsize, RD_STATS *rd_stats,
1410
0
    const RD_STATS *rd_stats_y, RD_STATS *rd_stats_uv, int64_t best_rd) {
1411
0
  const AV1_COMMON *cm = &cpi->common;
1412
0
  MACROBLOCKD *const xd = &x->e_mbd;
1413
0
  MB_MODE_INFO *const mbmi = xd->mi[0];
1414
0
  assert(mbmi->ref_frame[0] == INTRA_FRAME);
1415
1416
  // TODO(chiyotsai@google.com): Consolidate the chroma search code here with
1417
  // the one in av1_search_palette_mode.
1418
0
  PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info;
1419
0
  const int try_palette =
1420
0
      cpi->oxcf.tool_cfg.enable_palette &&
1421
0
      av1_allow_palette(cm->features.allow_screen_content_tools, mbmi->bsize);
1422
0
  assert(intra_search_state->rate_uv_intra == INT_MAX);
1423
  // If no good uv-predictor had been found, search for it.
1424
0
  const TX_SIZE uv_tx = av1_get_tx_size(AOM_PLANE_U, xd);
1425
0
  av1_rd_pick_intra_sbuv_mode(cpi, x, &intra_search_state->rate_uv_intra,
1426
0
                              &intra_search_state->rate_uv_tokenonly,
1427
0
                              &intra_search_state->dist_uvs,
1428
0
                              &intra_search_state->skip_uvs, bsize, uv_tx);
1429
0
  intra_search_state->mode_uv = mbmi->uv_mode;
1430
0
  if (try_palette) intra_search_state->pmi_uv = *pmi;
1431
0
  intra_search_state->uv_angle_delta = mbmi->angle_delta[PLANE_TYPE_UV];
1432
1433
0
  const int uv_rate = intra_search_state->rate_uv_tokenonly;
1434
0
  const int64_t uv_dist = intra_search_state->dist_uvs;
1435
0
  const int64_t uv_rd = RDCOST(x->rdmult, uv_rate, uv_dist);
1436
0
  if (uv_rd > best_rd) {
1437
    // If there is no good intra uv-mode available, we can skip all intra
1438
    // modes.
1439
0
    intra_search_state->skip_intra_modes = 1;
1440
0
    return 0;
1441
0
  }
1442
1443
  // If we are here, then the encoder has found at least one good intra uv
1444
  // predictor, so we can directly copy its statistics over.
1445
  // TODO(any): the stats here is not right if the best uv mode is CFL but the
1446
  // best y mode is palette.
1447
0
  rd_stats_uv->rate = intra_search_state->rate_uv_tokenonly;
1448
0
  rd_stats_uv->dist = intra_search_state->dist_uvs;
1449
0
  rd_stats_uv->skip_txfm = intra_search_state->skip_uvs;
1450
0
  rd_stats->skip_txfm = rd_stats_y->skip_txfm && rd_stats_uv->skip_txfm;
1451
0
  mbmi->uv_mode = intra_search_state->mode_uv;
1452
0
  if (try_palette) {
1453
0
    pmi->palette_size[1] = intra_search_state->pmi_uv.palette_size[1];
1454
0
    memcpy(pmi->palette_colors + PALETTE_MAX_SIZE,
1455
0
           intra_search_state->pmi_uv.palette_colors + PALETTE_MAX_SIZE,
1456
0
           2 * PALETTE_MAX_SIZE * sizeof(pmi->palette_colors[0]));
1457
0
  }
1458
0
  mbmi->angle_delta[PLANE_TYPE_UV] = intra_search_state->uv_angle_delta;
1459
1460
0
  return 1;
1461
0
}
1462
1463
// Checks if odd delta angles can be pruned based on rdcosts of even delta
1464
// angles of the corresponding directional mode.
1465
static inline int prune_luma_odd_delta_angles_using_rd_cost(
1466
    const MB_MODE_INFO *const mbmi, const int64_t *const intra_modes_rd_cost,
1467
0
    int64_t best_rd, int prune_luma_odd_delta_angles_in_intra) {
1468
0
  const int luma_delta_angle = mbmi->angle_delta[PLANE_TYPE_Y];
1469
0
  if (!prune_luma_odd_delta_angles_in_intra ||
1470
0
      !av1_is_directional_mode(mbmi->mode) || !(abs(luma_delta_angle) & 1) ||
1471
0
      best_rd == INT64_MAX)
1472
0
    return 0;
1473
1474
0
  const int64_t rd_thresh = best_rd + (best_rd >> 3);
1475
1476
  // Neighbour rdcosts are considered for pruning of odd delta angles as
1477
  // mentioned below:
1478
  // Delta angle      Delta angle rdcost
1479
  // to be pruned     to be considered
1480
  //    -3                   -2
1481
  //    -1                -2, 0
1482
  //     1                 0, 2
1483
  //     3                    2
1484
0
  return intra_modes_rd_cost[luma_delta_angle + MAX_ANGLE_DELTA] > rd_thresh &&
1485
0
         intra_modes_rd_cost[luma_delta_angle + MAX_ANGLE_DELTA + 2] >
1486
0
             rd_thresh;
1487
0
}
1488
1489
// Finds the best non-intrabc mode on an intra frame.
1490
int64_t av1_rd_pick_intra_sby_mode(const AV1_COMP *const cpi, MACROBLOCK *x,
1491
                                   int *rate, int *rate_tokenonly,
1492
                                   int64_t *distortion, uint8_t *skippable,
1493
                                   BLOCK_SIZE bsize, int64_t best_rd,
1494
0
                                   PICK_MODE_CONTEXT *ctx) {
1495
0
  MACROBLOCKD *const xd = &x->e_mbd;
1496
0
  MB_MODE_INFO *const mbmi = xd->mi[0];
1497
0
  assert(!is_inter_block(mbmi));
1498
0
  int64_t best_model_rd = INT64_MAX;
1499
0
  uint8_t directional_mode_skip_mask[INTRA_MODES] = { 0 };
1500
  // Flag to check rd of any intra mode is better than best_rd passed to this
1501
  // function
1502
0
  int beat_best_rd = 0;
1503
0
  const int *bmode_costs;
1504
0
  const IntraModeCfg *const intra_mode_cfg = &cpi->oxcf.intra_mode_cfg;
1505
0
  PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info;
1506
0
  const int try_palette =
1507
0
      cpi->oxcf.tool_cfg.enable_palette &&
1508
0
      av1_allow_palette(cpi->common.features.allow_screen_content_tools,
1509
0
                        mbmi->bsize);
1510
0
  uint8_t *best_palette_color_map =
1511
0
      try_palette ? x->palette_buffer->best_palette_color_map : NULL;
1512
0
  const MB_MODE_INFO *above_mi = xd->above_mbmi;
1513
0
  const MB_MODE_INFO *left_mi = xd->left_mbmi;
1514
0
  const PREDICTION_MODE A = av1_above_block_mode(above_mi);
1515
0
  const PREDICTION_MODE L = av1_left_block_mode(left_mi);
1516
0
  const int above_ctx = intra_mode_context[A];
1517
0
  const int left_ctx = intra_mode_context[L];
1518
0
  bmode_costs = x->mode_costs.y_mode_costs[above_ctx][left_ctx];
1519
1520
0
  mbmi->angle_delta[PLANE_TYPE_Y] = 0;
1521
0
  const INTRA_MODE_SPEED_FEATURES *const intra_sf = &cpi->sf.intra_sf;
1522
0
  if (intra_sf->intra_pruning_with_hog) {
1523
    // Less aggressive thresholds are used here than those used in inter frame
1524
    // encoding in av1_handle_intra_y_mode() because we want key frames/intra
1525
    // frames to have higher quality.
1526
0
    const float thresh[4] = { -1.2f, -1.2f, -0.6f, 0.4f };
1527
0
    const int is_chroma = 0;
1528
0
    prune_intra_mode_with_hog(x, bsize, cpi->common.seq_params->sb_size,
1529
0
                              thresh[intra_sf->intra_pruning_with_hog - 1],
1530
0
                              directional_mode_skip_mask, is_chroma);
1531
0
  }
1532
0
  mbmi->filter_intra_mode_info.use_filter_intra = 0;
1533
0
  pmi->palette_size[0] = 0;
1534
1535
  // Set params for mode evaluation
1536
0
  set_mode_eval_params(cpi, x, MODE_EVAL);
1537
1538
0
  MB_MODE_INFO best_mbmi = *mbmi;
1539
0
  x->winner_mode_count = 0;
1540
1541
  // Searches the intra-modes except for intrabc, palette, and filter_intra.
1542
0
  int64_t top_intra_model_rd[TOP_INTRA_MODEL_COUNT];
1543
0
  for (int i = 0; i < TOP_INTRA_MODEL_COUNT; i++) {
1544
0
    top_intra_model_rd[i] = INT64_MAX;
1545
0
  }
1546
1547
0
  const int use_angle_delta =
1548
0
      av1_use_angle_delta(bsize) && intra_mode_cfg->enable_angle_delta;
1549
0
  const int max_mode_idx =
1550
0
      (use_angle_delta && intra_mode_cfg->enable_directional_intra)
1551
0
          ? LUMA_MODE_COUNT
1552
0
          : INTRA_MODE_END;
1553
1554
0
  uint16_t allowed_mode_mask =
1555
0
      intra_sf->intra_y_mode_mask[max_txsize_lookup[bsize]];
1556
0
  if (!intra_mode_cfg->enable_diagonal_intra)
1557
0
    allowed_mode_mask &=
1558
0
        ~((1 << D45_PRED) | (1 << D135_PRED) | (1 << D113_PRED) |
1559
0
          (1 << D157_PRED) | (1 << D203_PRED) | (1 << D67_PRED));
1560
0
  if (!intra_mode_cfg->enable_directional_intra)
1561
0
    allowed_mode_mask &=
1562
0
        ~((1 << V_PRED) | (1 << H_PRED) | (1 << D45_PRED) | (1 << D135_PRED) |
1563
0
          (1 << D113_PRED) | (1 << D157_PRED) | (1 << D203_PRED) |
1564
0
          (1 << D67_PRED));
1565
0
  if (!intra_mode_cfg->enable_smooth_intra || intra_sf->disable_smooth_intra) {
1566
0
    allowed_mode_mask &= ~((1 << SMOOTH_H_PRED) | (1 << SMOOTH_V_PRED));
1567
0
    if (!intra_mode_cfg->enable_smooth_intra ||
1568
0
        intra_sf->prune_filter_intra_level == 0)
1569
0
      allowed_mode_mask &= ~(1 << SMOOTH_PRED);
1570
0
  }
1571
0
  if (!intra_mode_cfg->enable_paeth_intra)
1572
0
    allowed_mode_mask &= ~(1 << PAETH_PRED);
1573
0
  if (x->use_mb_mode_cache)
1574
0
    allowed_mode_mask &= (uint16_t)(1u << x->mb_mode_cache->mode);
1575
0
  if (intra_sf->intra_pruning_with_hog) {
1576
0
    for (int m = V_PRED; m <= D67_PRED; ++m) {
1577
0
      if (directional_mode_skip_mask[m]) allowed_mode_mask &= ~(1 << m);
1578
0
    }
1579
0
  }
1580
1581
  // Initialize the rdcost corresponding to directional intra modes when odd
1582
  // delta angle pruning is active.
1583
0
  int64_t intra_modes_rd_cost[INTRA_MODE_END]
1584
0
                             [SIZE_OF_ANGLE_DELTA_RD_COST_ARRAY];
1585
0
  if (use_angle_delta && intra_sf->prune_luma_odd_delta_angles_in_intra) {
1586
0
    for (int i = V_PRED; i <= D67_PRED; i++) {
1587
0
      for (int j = 0; j < SIZE_OF_ANGLE_DELTA_RD_COST_ARRAY; j++) {
1588
0
        intra_modes_rd_cost[i][j] = INT64_MAX;
1589
0
      }
1590
0
    }
1591
0
  }
1592
1593
0
  for (int mode_idx = INTRA_MODE_START; mode_idx < max_mode_idx; ++mode_idx) {
1594
0
    set_y_mode_and_delta_angle(mode_idx, mbmi,
1595
0
                               intra_sf->prune_luma_odd_delta_angles_in_intra);
1596
0
    if (!(allowed_mode_mask & (1 << mbmi->mode))) continue;
1597
1598
0
    RD_STATS this_rd_stats;
1599
0
    int this_rate, this_rate_tokenonly, s;
1600
0
    int64_t this_distortion, this_rd;
1601
0
    const int luma_delta_angle = mbmi->angle_delta[PLANE_TYPE_Y];
1602
1603
0
    if ((luma_delta_angle & 1) &&
1604
0
        prune_luma_odd_delta_angles_using_rd_cost(
1605
0
            mbmi, intra_modes_rd_cost[mbmi->mode], best_rd,
1606
0
            intra_sf->prune_luma_odd_delta_angles_in_intra))
1607
0
      continue;
1608
1609
0
    const TX_SIZE tx_size = AOMMIN(TX_32X32, max_txsize_lookup[bsize]);
1610
0
    const int64_t this_model_rd =
1611
0
        intra_model_rd(&cpi->common, x, 0, bsize, tx_size, /*use_hadamard=*/1,
1612
0
                       cpi->do_border_pad);
1613
1614
0
    const int model_rd_index_for_pruning =
1615
0
        get_model_rd_index_for_pruning(x, intra_sf);
1616
1617
0
    if (prune_intra_y_mode(this_model_rd, &best_model_rd, top_intra_model_rd,
1618
0
                           intra_sf->top_intra_model_count_allowed,
1619
0
                           model_rd_index_for_pruning))
1620
0
      continue;
1621
1622
    // Builds the actual prediction. The prediction from
1623
    // model_intra_yrd_and_prune was just an estimation that did not take into
1624
    // account the effect of txfm pipeline, so we need to redo it for real
1625
    // here.
1626
0
    av1_pick_uniform_tx_size_type_yrd(cpi, x, &this_rd_stats, bsize, best_rd);
1627
0
    this_rate_tokenonly = this_rd_stats.rate;
1628
0
    this_distortion = this_rd_stats.dist;
1629
0
    s = this_rd_stats.skip_txfm;
1630
1631
0
    if (this_rate_tokenonly == INT_MAX) continue;
1632
1633
0
    if (!xd->lossless[mbmi->segment_id] && block_signals_txsize(mbmi->bsize)) {
1634
      // av1_pick_uniform_tx_size_type_yrd above includes the cost of the
1635
      // tx_size in the tokenonly rate, but for intra blocks, tx_size is always
1636
      // coded (prediction granularity), so we account for it in the full rate,
1637
      // not the tokenonly rate.
1638
0
      this_rate_tokenonly -= tx_size_cost(x, bsize, mbmi->tx_size);
1639
0
    }
1640
0
    this_rate =
1641
0
        this_rd_stats.rate +
1642
0
        intra_mode_info_cost_y(cpi, x, mbmi, bsize, bmode_costs[mbmi->mode], 0);
1643
0
    this_rd = RDCOST(x->rdmult, this_rate, this_distortion);
1644
1645
    // Visual quality adjustment based on recon vs source variance.
1646
0
    if ((cpi->oxcf.mode == ALLINTRA) && (this_rd != INT64_MAX)) {
1647
0
      this_rd = (int64_t)(this_rd * intra_rd_variance_factor(cpi, x, bsize));
1648
0
    }
1649
1650
0
    intra_modes_rd_cost[mbmi->mode][luma_delta_angle + MAX_ANGLE_DELTA + 1] =
1651
0
        this_rd;
1652
1653
    // Collect mode stats for multiwinner mode processing
1654
0
    const int txfm_search_done = 1;
1655
0
    store_winner_mode_stats(
1656
0
        cpi, x, mbmi, NULL, NULL, NULL, 0, NULL, bsize, this_rd,
1657
0
        cpi->sf.winner_mode_sf.multi_winner_mode_type, txfm_search_done);
1658
0
    if (this_rd < best_rd) {
1659
0
      best_mbmi = *mbmi;
1660
0
      best_rd = this_rd;
1661
      // Setting beat_best_rd flag because current mode rd is better than
1662
      // best_rd passed to this function
1663
0
      beat_best_rd = 1;
1664
0
      *rate = this_rate;
1665
0
      *rate_tokenonly = this_rate_tokenonly;
1666
0
      *distortion = this_distortion;
1667
0
      *skippable = s;
1668
0
      av1_copy_array(ctx->tx_type_map, xd->tx_type_map, ctx->num_4x4_blk);
1669
0
    }
1670
0
  }
1671
1672
  // Searches palette
1673
0
  if (try_palette) {
1674
0
    av1_rd_pick_palette_intra_sby(cpi, x, bsize, bmode_costs[DC_PRED],
1675
0
                                  &best_mbmi, best_palette_color_map, &best_rd,
1676
0
                                  rate, rate_tokenonly, distortion, skippable,
1677
0
                                  &beat_best_rd, ctx, ctx->tx_type_map);
1678
0
  }
1679
1680
  // Searches filter_intra
1681
0
  if (beat_best_rd && av1_filter_intra_allowed_bsize(&cpi->common, bsize)) {
1682
0
    if (rd_pick_filter_intra_sby(cpi, x, rate, rate_tokenonly, distortion,
1683
0
                                 skippable, bsize, bmode_costs[DC_PRED],
1684
0
                                 best_mbmi.mode, &best_rd, &best_model_rd,
1685
0
                                 ctx)) {
1686
0
      best_mbmi = *mbmi;
1687
0
    }
1688
0
  }
1689
1690
  // No mode is identified with less rd value than best_rd passed to this
1691
  // function. In such cases winner mode processing is not necessary and return
1692
  // best_rd as INT64_MAX to indicate best mode is not identified
1693
0
  if (!beat_best_rd) return INT64_MAX;
1694
1695
  // In multi-winner mode processing, perform tx search for few best modes
1696
  // identified during mode evaluation. Winner mode processing uses best tx
1697
  // configuration for tx search.
1698
0
  if (cpi->sf.winner_mode_sf.multi_winner_mode_type) {
1699
0
    int best_mode_idx = 0;
1700
0
    int block_width, block_height;
1701
0
    uint8_t *color_map_dst = xd->plane[PLANE_TYPE_Y].color_index_map;
1702
0
    av1_get_block_dimensions(bsize, AOM_PLANE_Y, xd, &block_width,
1703
0
                             &block_height, NULL, NULL);
1704
1705
0
    for (int mode_idx = 0; mode_idx < x->winner_mode_count; mode_idx++) {
1706
0
      *mbmi = x->winner_mode_stats[mode_idx].mbmi;
1707
0
      if (is_winner_mode_processing_enabled(cpi, x, mbmi, 0)) {
1708
        // Restore color_map of palette mode before winner mode processing
1709
0
        if (mbmi->palette_mode_info.palette_size[0] > 0) {
1710
0
          uint8_t *color_map_src =
1711
0
              x->winner_mode_stats[mode_idx].color_index_map;
1712
0
          memcpy(color_map_dst, color_map_src,
1713
0
                 block_width * block_height * sizeof(*color_map_src));
1714
0
        }
1715
        // Set params for winner mode evaluation
1716
0
        set_mode_eval_params(cpi, x, WINNER_MODE_EVAL);
1717
1718
        // Winner mode processing
1719
        // If previous searches use only the default tx type/no R-D optimization
1720
        // of quantized coeffs, do an extra search for the best tx type/better
1721
        // R-D optimization of quantized coeffs
1722
0
        if (intra_block_yrd(cpi, x, bsize, bmode_costs, &best_rd, rate,
1723
0
                            rate_tokenonly, distortion, skippable, &best_mbmi,
1724
0
                            ctx))
1725
0
          best_mode_idx = mode_idx;
1726
0
      }
1727
0
    }
1728
    // Copy color_map of palette mode for final winner mode
1729
0
    if (best_mbmi.palette_mode_info.palette_size[0] > 0) {
1730
0
      uint8_t *color_map_src =
1731
0
          x->winner_mode_stats[best_mode_idx].color_index_map;
1732
0
      memcpy(color_map_dst, color_map_src,
1733
0
             block_width * block_height * sizeof(*color_map_src));
1734
0
    }
1735
0
  } else {
1736
    // If previous searches use only the default tx type/no R-D optimization of
1737
    // quantized coeffs, do an extra search for the best tx type/better R-D
1738
    // optimization of quantized coeffs
1739
0
    if (is_winner_mode_processing_enabled(cpi, x, mbmi, 0)) {
1740
      // Set params for winner mode evaluation
1741
0
      set_mode_eval_params(cpi, x, WINNER_MODE_EVAL);
1742
0
      *mbmi = best_mbmi;
1743
0
      intra_block_yrd(cpi, x, bsize, bmode_costs, &best_rd, rate,
1744
0
                      rate_tokenonly, distortion, skippable, &best_mbmi, ctx);
1745
0
    }
1746
0
  }
1747
0
  *mbmi = best_mbmi;
1748
0
  av1_copy_array(xd->tx_type_map, ctx->tx_type_map, ctx->num_4x4_blk);
1749
0
  return best_rd;
1750
0
}
1751
1752
void av1_lowbd_generate_hog_c(const uint8_t *src, int stride, int rows,
1753
0
                              int cols, float *hist) {
1754
0
  lowbd_generate_hog(src, stride, rows, cols, hist, get_hist_bin_idx);
1755
0
}
1756
1757
void av1_lowbd_compute_gradient_info_sb_c(MACROBLOCK *const x,
1758
                                          BLOCK_SIZE sb_size,
1759
0
                                          PLANE_TYPE plane) {
1760
0
  lowbd_compute_gradient_info_sb(x, sb_size, plane, get_hist_bin_idx);
1761
0
}
1762
1763
#if CONFIG_AV1_HIGHBITDEPTH
1764
void av1_highbd_generate_hog_c(const uint8_t *src8, int stride, int rows,
1765
0
                               int cols, float *hist) {
1766
0
  highbd_generate_hog(src8, stride, rows, cols, hist, get_hist_bin_idx);
1767
0
}
1768
1769
void av1_highbd_compute_gradient_info_sb_c(MACROBLOCK *const x,
1770
                                           BLOCK_SIZE sb_size,
1771
0
                                           PLANE_TYPE plane) {
1772
0
  highbd_compute_gradient_info_sb(x, sb_size, plane, get_hist_bin_idx);
1773
0
}
1774
#endif  // CONFIG_AV1_HIGHBITDEPTH