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 "av1/common/av1_common_int.h"
13 #include "av1/common/reconintra.h"
14
15 #include "av1/encoder/intra_mode_search.h"
16 #include "av1/encoder/intra_mode_search_utils.h"
17 #include "av1/encoder/palette.h"
18 #include "av1/encoder/speed_features.h"
19 #include "av1/encoder/tx_search.h"
20
21 /*!\cond */
22 static const PREDICTION_MODE intra_rd_search_mode_order[INTRA_MODES] = {
23 DC_PRED, H_PRED, V_PRED, SMOOTH_PRED, PAETH_PRED,
24 SMOOTH_V_PRED, SMOOTH_H_PRED, D135_PRED, D203_PRED, D157_PRED,
25 D67_PRED, D113_PRED, D45_PRED,
26 };
27
28 static const UV_PREDICTION_MODE uv_rd_search_mode_order[UV_INTRA_MODES] = {
29 UV_DC_PRED, UV_CFL_PRED, UV_H_PRED, UV_V_PRED,
30 UV_SMOOTH_PRED, UV_PAETH_PRED, UV_SMOOTH_V_PRED, UV_SMOOTH_H_PRED,
31 UV_D135_PRED, UV_D203_PRED, UV_D157_PRED, UV_D67_PRED,
32 UV_D113_PRED, UV_D45_PRED,
33 };
34
35 // The bitmask corresponds to the filter intra modes as defined in enums.h
36 // FILTER_INTRA_MODE enumeration type. Setting a bit to 0 in the mask means to
37 // disable the evaluation of corresponding filter intra mode. The table
38 // av1_derived_filter_intra_mode_used_flag is used when speed feature
39 // prune_filter_intra_level is 1. The evaluated filter intra modes are union
40 // of the following:
41 // 1) FILTER_DC_PRED
42 // 2) mode that corresponds to best mode so far of DC_PRED, V_PRED, H_PRED,
43 // D157_PRED and PAETH_PRED. (Eg: FILTER_V_PRED if best mode so far is V_PRED).
44 static const uint8_t av1_derived_filter_intra_mode_used_flag[INTRA_MODES] = {
45 0x01, // DC_PRED: 0000 0001
46 0x03, // V_PRED: 0000 0011
47 0x05, // H_PRED: 0000 0101
48 0x01, // D45_PRED: 0000 0001
49 0x01, // D135_PRED: 0000 0001
50 0x01, // D113_PRED: 0000 0001
51 0x09, // D157_PRED: 0000 1001
52 0x01, // D203_PRED: 0000 0001
53 0x01, // D67_PRED: 0000 0001
54 0x01, // SMOOTH_PRED: 0000 0001
55 0x01, // SMOOTH_V_PRED: 0000 0001
56 0x01, // SMOOTH_H_PRED: 0000 0001
57 0x11 // PAETH_PRED: 0001 0001
58 };
59
60 // The bitmask corresponds to the chroma intra modes as defined in enums.h
61 // UV_PREDICTION_MODE enumeration type. Setting a bit to 0 in the mask means to
62 // disable the evaluation of corresponding chroma intra mode. The table
63 // av1_derived_chroma_intra_mode_used_flag is used when speed feature
64 // prune_chroma_modes_using_luma_winner is enabled. The evaluated chroma
65 // intra modes are union of the following:
66 // 1) UV_DC_PRED
67 // 2) UV_SMOOTH_PRED
68 // 3) UV_CFL_PRED
69 // 4) mode that corresponds to luma intra mode winner (Eg : UV_V_PRED if luma
70 // intra mode winner is V_PRED).
71 static const uint16_t av1_derived_chroma_intra_mode_used_flag[INTRA_MODES] = {
72 0x2201, // DC_PRED: 0010 0010 0000 0001
73 0x2203, // V_PRED: 0010 0010 0000 0011
74 0x2205, // H_PRED: 0010 0010 0000 0101
75 0x2209, // D45_PRED: 0010 0010 0000 1001
76 0x2211, // D135_PRED: 0010 0010 0001 0001
77 0x2221, // D113_PRED: 0010 0010 0010 0001
78 0x2241, // D157_PRED: 0010 0010 0100 0001
79 0x2281, // D203_PRED: 0010 0010 1000 0001
80 0x2301, // D67_PRED: 0010 0011 0000 0001
81 0x2201, // SMOOTH_PRED: 0010 0010 0000 0001
82 0x2601, // SMOOTH_V_PRED: 0010 0110 0000 0001
83 0x2a01, // SMOOTH_H_PRED: 0010 1010 0000 0001
84 0x3201 // PAETH_PRED: 0011 0010 0000 0001
85 };
86 /*!\endcond */
87
88 /*!\brief Search for the best filter_intra mode when coding intra frame.
89 *
90 * \ingroup intra_mode_search
91 * \callergraph
92 * This function loops through all filter_intra modes to find the best one.
93 *
94 * \return Returns 1 if a new filter_intra mode is selected; 0 otherwise.
95 */
rd_pick_filter_intra_sby(const AV1_COMP * const cpi,MACROBLOCK * x,int * rate,int * rate_tokenonly,int64_t * distortion,int * skippable,BLOCK_SIZE bsize,int mode_cost,PREDICTION_MODE best_mode_so_far,int64_t * best_rd,int64_t * best_model_rd,PICK_MODE_CONTEXT * ctx)96 static int rd_pick_filter_intra_sby(const AV1_COMP *const cpi, MACROBLOCK *x,
97 int *rate, int *rate_tokenonly,
98 int64_t *distortion, int *skippable,
99 BLOCK_SIZE bsize, int mode_cost,
100 PREDICTION_MODE best_mode_so_far,
101 int64_t *best_rd, int64_t *best_model_rd,
102 PICK_MODE_CONTEXT *ctx) {
103 // Skip the evaluation of filter intra modes.
104 if (cpi->sf.intra_sf.prune_filter_intra_level == 2) return 0;
105
106 MACROBLOCKD *const xd = &x->e_mbd;
107 MB_MODE_INFO *mbmi = xd->mi[0];
108 int filter_intra_selected_flag = 0;
109 FILTER_INTRA_MODE mode;
110 TX_SIZE best_tx_size = TX_8X8;
111 FILTER_INTRA_MODE_INFO filter_intra_mode_info;
112 uint8_t best_tx_type_map[MAX_MIB_SIZE * MAX_MIB_SIZE];
113 av1_zero(filter_intra_mode_info);
114 mbmi->filter_intra_mode_info.use_filter_intra = 1;
115 mbmi->mode = DC_PRED;
116 mbmi->palette_mode_info.palette_size[0] = 0;
117
118 // Skip the evaluation of filter-intra if cached MB_MODE_INFO does not have
119 // filter-intra as winner.
120 if (x->use_mb_mode_cache &&
121 !x->mb_mode_cache->filter_intra_mode_info.use_filter_intra)
122 return 0;
123
124 for (mode = 0; mode < FILTER_INTRA_MODES; ++mode) {
125 int64_t this_rd;
126 RD_STATS tokenonly_rd_stats;
127 mbmi->filter_intra_mode_info.filter_intra_mode = mode;
128
129 if ((cpi->sf.intra_sf.prune_filter_intra_level == 1) &&
130 !(av1_derived_filter_intra_mode_used_flag[best_mode_so_far] &
131 (1 << mode)))
132 continue;
133
134 // Skip the evaluation of modes that do not match with the winner mode in
135 // x->mb_mode_cache.
136 if (x->use_mb_mode_cache &&
137 mode != x->mb_mode_cache->filter_intra_mode_info.filter_intra_mode)
138 continue;
139
140 if (model_intra_yrd_and_prune(cpi, x, bsize, best_model_rd)) {
141 continue;
142 }
143 av1_pick_uniform_tx_size_type_yrd(cpi, x, &tokenonly_rd_stats, bsize,
144 *best_rd);
145 if (tokenonly_rd_stats.rate == INT_MAX) continue;
146 const int this_rate =
147 tokenonly_rd_stats.rate +
148 intra_mode_info_cost_y(cpi, x, mbmi, bsize, mode_cost);
149 this_rd = RDCOST(x->rdmult, this_rate, tokenonly_rd_stats.dist);
150
151 // Collect mode stats for multiwinner mode processing
152 const int txfm_search_done = 1;
153 store_winner_mode_stats(
154 &cpi->common, x, mbmi, NULL, NULL, NULL, 0, NULL, bsize, this_rd,
155 cpi->sf.winner_mode_sf.multi_winner_mode_type, txfm_search_done);
156 if (this_rd < *best_rd) {
157 *best_rd = this_rd;
158 best_tx_size = mbmi->tx_size;
159 filter_intra_mode_info = mbmi->filter_intra_mode_info;
160 av1_copy_array(best_tx_type_map, xd->tx_type_map, ctx->num_4x4_blk);
161 memcpy(ctx->blk_skip, x->txfm_search_info.blk_skip,
162 sizeof(x->txfm_search_info.blk_skip[0]) * ctx->num_4x4_blk);
163 *rate = this_rate;
164 *rate_tokenonly = tokenonly_rd_stats.rate;
165 *distortion = tokenonly_rd_stats.dist;
166 *skippable = tokenonly_rd_stats.skip_txfm;
167 filter_intra_selected_flag = 1;
168 }
169 }
170
171 if (filter_intra_selected_flag) {
172 mbmi->mode = DC_PRED;
173 mbmi->tx_size = best_tx_size;
174 mbmi->filter_intra_mode_info = filter_intra_mode_info;
175 av1_copy_array(ctx->tx_type_map, best_tx_type_map, ctx->num_4x4_blk);
176 return 1;
177 } else {
178 return 0;
179 }
180 }
181
av1_count_colors(const uint8_t * src,int stride,int rows,int cols,int * val_count,int * num_colors)182 void av1_count_colors(const uint8_t *src, int stride, int rows, int cols,
183 int *val_count, int *num_colors) {
184 const int max_pix_val = 1 << 8;
185 memset(val_count, 0, max_pix_val * sizeof(val_count[0]));
186 for (int r = 0; r < rows; ++r) {
187 for (int c = 0; c < cols; ++c) {
188 const int this_val = src[r * stride + c];
189 assert(this_val < max_pix_val);
190 ++val_count[this_val];
191 }
192 }
193 int n = 0;
194 for (int i = 0; i < max_pix_val; ++i) {
195 if (val_count[i]) ++n;
196 }
197 *num_colors = n;
198 }
199
av1_count_colors_highbd(const uint8_t * src8,int stride,int rows,int cols,int bit_depth,int * val_count,int * bin_val_count,int * num_color_bins,int * num_colors)200 void av1_count_colors_highbd(const uint8_t *src8, int stride, int rows,
201 int cols, int bit_depth, int *val_count,
202 int *bin_val_count, int *num_color_bins,
203 int *num_colors) {
204 assert(bit_depth <= 12);
205 const int max_bin_val = 1 << 8;
206 const int max_pix_val = 1 << bit_depth;
207 const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
208 memset(bin_val_count, 0, max_bin_val * sizeof(val_count[0]));
209 if (val_count != NULL)
210 memset(val_count, 0, max_pix_val * sizeof(val_count[0]));
211 for (int r = 0; r < rows; ++r) {
212 for (int c = 0; c < cols; ++c) {
213 /*
214 * Down-convert the pixels to 8-bit domain before counting.
215 * This provides consistency of behavior for palette search
216 * between lbd and hbd encodes. This down-converted pixels
217 * are only used for calculating the threshold (n).
218 */
219 const int this_val = ((src[r * stride + c]) >> (bit_depth - 8));
220 assert(this_val < max_bin_val);
221 if (this_val >= max_bin_val) continue;
222 ++bin_val_count[this_val];
223 if (val_count != NULL) ++val_count[(src[r * stride + c])];
224 }
225 }
226 int n = 0;
227 // Count the colors based on 8-bit domain used to gate the palette path
228 for (int i = 0; i < max_bin_val; ++i) {
229 if (bin_val_count[i]) ++n;
230 }
231 *num_color_bins = n;
232
233 // Count the actual hbd colors used to create top_colors
234 n = 0;
235 if (val_count != NULL) {
236 for (int i = 0; i < max_pix_val; ++i) {
237 if (val_count[i]) ++n;
238 }
239 *num_colors = n;
240 }
241 }
242
set_y_mode_and_delta_angle(const int mode_idx,MB_MODE_INFO * const mbmi)243 void set_y_mode_and_delta_angle(const int mode_idx, MB_MODE_INFO *const mbmi) {
244 if (mode_idx < INTRA_MODE_END) {
245 mbmi->mode = intra_rd_search_mode_order[mode_idx];
246 mbmi->angle_delta[PLANE_TYPE_Y] = 0;
247 } else {
248 mbmi->mode = (mode_idx - INTRA_MODE_END) / (MAX_ANGLE_DELTA * 2) + V_PRED;
249 int angle_delta = (mode_idx - INTRA_MODE_END) % (MAX_ANGLE_DELTA * 2);
250 mbmi->angle_delta[PLANE_TYPE_Y] =
251 (angle_delta < 3 ? (angle_delta - 3) : (angle_delta - 2));
252 }
253 }
254
prune_intra_y_mode(int64_t this_model_rd,int64_t * best_model_rd,int64_t top_intra_model_rd[],int model_cnt_allowed)255 int prune_intra_y_mode(int64_t this_model_rd, int64_t *best_model_rd,
256 int64_t top_intra_model_rd[], int model_cnt_allowed) {
257 const double thresh_best = 1.50;
258 const double thresh_top = 1.00;
259 for (int i = 0; i < model_cnt_allowed; i++) {
260 if (this_model_rd < top_intra_model_rd[i]) {
261 for (int j = model_cnt_allowed - 1; j > i; j--) {
262 top_intra_model_rd[j] = top_intra_model_rd[j - 1];
263 }
264 top_intra_model_rd[i] = this_model_rd;
265 break;
266 }
267 }
268 if (top_intra_model_rd[model_cnt_allowed - 1] != INT64_MAX &&
269 this_model_rd > thresh_top * top_intra_model_rd[model_cnt_allowed - 1])
270 return 1;
271
272 if (this_model_rd != INT64_MAX &&
273 this_model_rd > thresh_best * (*best_model_rd))
274 return 1;
275 if (this_model_rd < *best_model_rd) *best_model_rd = this_model_rd;
276 return 0;
277 }
278
279 // Run RD calculation with given chroma intra prediction angle., and return
280 // the RD cost. Update the best mode info. if the RD cost is the best so far.
pick_intra_angle_routine_sbuv(const AV1_COMP * const cpi,MACROBLOCK * x,BLOCK_SIZE bsize,int rate_overhead,int64_t best_rd_in,int * rate,RD_STATS * rd_stats,int * best_angle_delta,int64_t * best_rd)281 static int64_t pick_intra_angle_routine_sbuv(
282 const AV1_COMP *const cpi, MACROBLOCK *x, BLOCK_SIZE bsize,
283 int rate_overhead, int64_t best_rd_in, int *rate, RD_STATS *rd_stats,
284 int *best_angle_delta, int64_t *best_rd) {
285 MB_MODE_INFO *mbmi = x->e_mbd.mi[0];
286 assert(!is_inter_block(mbmi));
287 int this_rate;
288 int64_t this_rd;
289 RD_STATS tokenonly_rd_stats;
290
291 if (!av1_txfm_uvrd(cpi, x, &tokenonly_rd_stats, bsize, best_rd_in))
292 return INT64_MAX;
293 this_rate = tokenonly_rd_stats.rate +
294 intra_mode_info_cost_uv(cpi, x, mbmi, bsize, rate_overhead);
295 this_rd = RDCOST(x->rdmult, this_rate, tokenonly_rd_stats.dist);
296 if (this_rd < *best_rd) {
297 *best_rd = this_rd;
298 *best_angle_delta = mbmi->angle_delta[PLANE_TYPE_UV];
299 *rate = this_rate;
300 rd_stats->rate = tokenonly_rd_stats.rate;
301 rd_stats->dist = tokenonly_rd_stats.dist;
302 rd_stats->skip_txfm = tokenonly_rd_stats.skip_txfm;
303 }
304 return this_rd;
305 }
306
307 /*!\brief Search for the best angle delta for chroma prediction
308 *
309 * \ingroup intra_mode_search
310 * \callergraph
311 * Given a chroma directional intra prediction mode, this function will try to
312 * estimate the best delta_angle.
313 *
314 * \returns Return if there is a new mode with smaller rdcost than best_rd.
315 */
rd_pick_intra_angle_sbuv(const AV1_COMP * const cpi,MACROBLOCK * x,BLOCK_SIZE bsize,int rate_overhead,int64_t best_rd,int * rate,RD_STATS * rd_stats)316 static int rd_pick_intra_angle_sbuv(const AV1_COMP *const cpi, MACROBLOCK *x,
317 BLOCK_SIZE bsize, int rate_overhead,
318 int64_t best_rd, int *rate,
319 RD_STATS *rd_stats) {
320 MACROBLOCKD *const xd = &x->e_mbd;
321 MB_MODE_INFO *mbmi = xd->mi[0];
322 assert(!is_inter_block(mbmi));
323 int i, angle_delta, best_angle_delta = 0;
324 int64_t this_rd, best_rd_in, rd_cost[2 * (MAX_ANGLE_DELTA + 2)];
325
326 rd_stats->rate = INT_MAX;
327 rd_stats->skip_txfm = 0;
328 rd_stats->dist = INT64_MAX;
329 for (i = 0; i < 2 * (MAX_ANGLE_DELTA + 2); ++i) rd_cost[i] = INT64_MAX;
330
331 for (angle_delta = 0; angle_delta <= MAX_ANGLE_DELTA; angle_delta += 2) {
332 for (i = 0; i < 2; ++i) {
333 best_rd_in = (best_rd == INT64_MAX)
334 ? INT64_MAX
335 : (best_rd + (best_rd >> ((angle_delta == 0) ? 3 : 5)));
336 mbmi->angle_delta[PLANE_TYPE_UV] = (1 - 2 * i) * angle_delta;
337 this_rd = pick_intra_angle_routine_sbuv(cpi, x, bsize, rate_overhead,
338 best_rd_in, rate, rd_stats,
339 &best_angle_delta, &best_rd);
340 rd_cost[2 * angle_delta + i] = this_rd;
341 if (angle_delta == 0) {
342 if (this_rd == INT64_MAX) return 0;
343 rd_cost[1] = this_rd;
344 break;
345 }
346 }
347 }
348
349 assert(best_rd != INT64_MAX);
350 for (angle_delta = 1; angle_delta <= MAX_ANGLE_DELTA; angle_delta += 2) {
351 int64_t rd_thresh;
352 for (i = 0; i < 2; ++i) {
353 int skip_search = 0;
354 rd_thresh = best_rd + (best_rd >> 5);
355 if (rd_cost[2 * (angle_delta + 1) + i] > rd_thresh &&
356 rd_cost[2 * (angle_delta - 1) + i] > rd_thresh)
357 skip_search = 1;
358 if (!skip_search) {
359 mbmi->angle_delta[PLANE_TYPE_UV] = (1 - 2 * i) * angle_delta;
360 pick_intra_angle_routine_sbuv(cpi, x, bsize, rate_overhead, best_rd,
361 rate, rd_stats, &best_angle_delta,
362 &best_rd);
363 }
364 }
365 }
366
367 mbmi->angle_delta[PLANE_TYPE_UV] = best_angle_delta;
368 return rd_stats->rate != INT_MAX;
369 }
370
371 #define PLANE_SIGN_TO_JOINT_SIGN(plane, a, b) \
372 (plane == CFL_PRED_U ? a * CFL_SIGNS + b - 1 : b * CFL_SIGNS + a - 1)
373
cfl_idx_to_sign_and_alpha(int cfl_idx,CFL_SIGN_TYPE * cfl_sign,int * cfl_alpha)374 static void cfl_idx_to_sign_and_alpha(int cfl_idx, CFL_SIGN_TYPE *cfl_sign,
375 int *cfl_alpha) {
376 int cfl_linear_idx = cfl_idx - CFL_INDEX_ZERO;
377 if (cfl_linear_idx == 0) {
378 *cfl_sign = CFL_SIGN_ZERO;
379 *cfl_alpha = 0;
380 } else {
381 *cfl_sign = cfl_linear_idx > 0 ? CFL_SIGN_POS : CFL_SIGN_NEG;
382 *cfl_alpha = abs(cfl_linear_idx) - 1;
383 }
384 }
385
cfl_compute_rd(const AV1_COMP * const cpi,MACROBLOCK * x,int plane,TX_SIZE tx_size,BLOCK_SIZE plane_bsize,int cfl_idx,int fast_mode,RD_STATS * rd_stats)386 static int64_t cfl_compute_rd(const AV1_COMP *const cpi, MACROBLOCK *x,
387 int plane, TX_SIZE tx_size,
388 BLOCK_SIZE plane_bsize, int cfl_idx,
389 int fast_mode, RD_STATS *rd_stats) {
390 assert(IMPLIES(fast_mode, rd_stats == NULL));
391 const AV1_COMMON *const cm = &cpi->common;
392 MACROBLOCKD *const xd = &x->e_mbd;
393 MB_MODE_INFO *const mbmi = xd->mi[0];
394 int cfl_plane = get_cfl_pred_type(plane);
395 CFL_SIGN_TYPE cfl_sign;
396 int cfl_alpha;
397 cfl_idx_to_sign_and_alpha(cfl_idx, &cfl_sign, &cfl_alpha);
398 // We conly build CFL for a given plane, the other plane's sign is dummy
399 int dummy_sign = CFL_SIGN_NEG;
400 const int8_t orig_cfl_alpha_signs = mbmi->cfl_alpha_signs;
401 const uint8_t orig_cfl_alpha_idx = mbmi->cfl_alpha_idx;
402 mbmi->cfl_alpha_signs =
403 PLANE_SIGN_TO_JOINT_SIGN(cfl_plane, cfl_sign, dummy_sign);
404 mbmi->cfl_alpha_idx = (cfl_alpha << CFL_ALPHABET_SIZE_LOG2) + cfl_alpha;
405 int64_t cfl_cost;
406 if (fast_mode) {
407 cfl_cost =
408 intra_model_rd(cm, x, plane, plane_bsize, tx_size, /*use_hadamard=*/0);
409 } else {
410 av1_init_rd_stats(rd_stats);
411 av1_txfm_rd_in_plane(x, cpi, rd_stats, INT64_MAX, 0, plane, plane_bsize,
412 tx_size, FTXS_NONE, 0);
413 av1_rd_cost_update(x->rdmult, rd_stats);
414 cfl_cost = rd_stats->rdcost;
415 }
416 mbmi->cfl_alpha_signs = orig_cfl_alpha_signs;
417 mbmi->cfl_alpha_idx = orig_cfl_alpha_idx;
418 return cfl_cost;
419 }
420
cfl_pick_plane_parameter(const AV1_COMP * const cpi,MACROBLOCK * x,int plane,TX_SIZE tx_size,int cfl_search_range,RD_STATS cfl_rd_arr[CFL_MAGS_SIZE])421 static void cfl_pick_plane_parameter(const AV1_COMP *const cpi, MACROBLOCK *x,
422 int plane, TX_SIZE tx_size,
423 int cfl_search_range,
424 RD_STATS cfl_rd_arr[CFL_MAGS_SIZE]) {
425 assert(cfl_search_range >= 1 && cfl_search_range <= CFL_MAGS_SIZE);
426 MACROBLOCKD *const xd = &x->e_mbd;
427
428 xd->cfl.use_dc_pred_cache = 1;
429
430 MB_MODE_INFO *const mbmi = xd->mi[0];
431 assert(mbmi->uv_mode == UV_CFL_PRED);
432 const MACROBLOCKD_PLANE *pd = &xd->plane[plane];
433 const BLOCK_SIZE plane_bsize =
434 get_plane_block_size(mbmi->bsize, pd->subsampling_x, pd->subsampling_y);
435
436 const int dir_ls[2] = { 1, -1 };
437
438 int est_best_cfl_idx = CFL_INDEX_ZERO;
439 if (cfl_search_range < CFL_MAGS_SIZE) {
440 int fast_mode = 1;
441 int start_cfl_idx = CFL_INDEX_ZERO;
442 int64_t best_cfl_cost = cfl_compute_rd(cpi, x, plane, tx_size, plane_bsize,
443 start_cfl_idx, fast_mode, NULL);
444 for (int si = 0; si < 2; ++si) {
445 const int dir = dir_ls[si];
446 for (int i = 1; i < CFL_MAGS_SIZE; ++i) {
447 int cfl_idx = start_cfl_idx + dir * i;
448 if (cfl_idx < 0 || cfl_idx >= CFL_MAGS_SIZE) break;
449 int64_t cfl_cost = cfl_compute_rd(cpi, x, plane, tx_size, plane_bsize,
450 cfl_idx, fast_mode, NULL);
451 if (cfl_cost < best_cfl_cost) {
452 best_cfl_cost = cfl_cost;
453 est_best_cfl_idx = cfl_idx;
454 } else {
455 break;
456 }
457 }
458 }
459 }
460
461 for (int cfl_idx = 0; cfl_idx < CFL_MAGS_SIZE; ++cfl_idx) {
462 av1_invalid_rd_stats(&cfl_rd_arr[cfl_idx]);
463 }
464
465 int fast_mode = 0;
466 int start_cfl_idx = est_best_cfl_idx;
467 cfl_compute_rd(cpi, x, plane, tx_size, plane_bsize, start_cfl_idx, fast_mode,
468 &cfl_rd_arr[start_cfl_idx]);
469 for (int si = 0; si < 2; ++si) {
470 const int dir = dir_ls[si];
471 for (int i = 1; i < cfl_search_range; ++i) {
472 int cfl_idx = start_cfl_idx + dir * i;
473 if (cfl_idx < 0 || cfl_idx >= CFL_MAGS_SIZE) break;
474 cfl_compute_rd(cpi, x, plane, tx_size, plane_bsize, cfl_idx, fast_mode,
475 &cfl_rd_arr[cfl_idx]);
476 }
477 }
478 xd->cfl.use_dc_pred_cache = 0;
479 xd->cfl.dc_pred_is_cached[0] = 0;
480 xd->cfl.dc_pred_is_cached[1] = 0;
481 }
482
483 /*!\brief Pick the optimal parameters for Chroma to Luma (CFL) component
484 *
485 * \ingroup intra_mode_search
486 * \callergraph
487 *
488 * This function will use DCT_DCT followed by computing SATD (sum of absolute
489 * transformed differences) to estimate the RD score and find the best possible
490 * CFL parameter.
491 *
492 * Then the function will apply a full RD search near the best possible CFL
493 * parameter to find the best actual CFL parameter.
494 *
495 * Side effect:
496 * We use ths buffers in x->plane[] and xd->plane[] as throw-away buffers for RD
497 * search.
498 *
499 * \param[in] x Encoder prediction block structure.
500 * \param[in] cpi Top-level encoder instance structure.
501 * \param[in] tx_size Transform size.
502 * \param[in] ref_best_rd Reference best RD.
503 * \param[in] cfl_search_range The search range of full RD search near the
504 * estimated best CFL parameter.
505 *
506 * \param[out] best_rd_stats RD stats of the best CFL parameter
507 * \param[out] best_cfl_alpha_idx Best CFL alpha index
508 * \param[out] best_cfl_alpha_signs Best CFL joint signs
509 *
510 */
cfl_rd_pick_alpha(MACROBLOCK * const x,const AV1_COMP * const cpi,TX_SIZE tx_size,int64_t ref_best_rd,int cfl_search_range,RD_STATS * best_rd_stats,uint8_t * best_cfl_alpha_idx,int8_t * best_cfl_alpha_signs)511 static int cfl_rd_pick_alpha(MACROBLOCK *const x, const AV1_COMP *const cpi,
512 TX_SIZE tx_size, int64_t ref_best_rd,
513 int cfl_search_range, RD_STATS *best_rd_stats,
514 uint8_t *best_cfl_alpha_idx,
515 int8_t *best_cfl_alpha_signs) {
516 assert(cfl_search_range >= 1 && cfl_search_range <= CFL_MAGS_SIZE);
517 const ModeCosts *mode_costs = &x->mode_costs;
518 RD_STATS cfl_rd_arr_u[CFL_MAGS_SIZE];
519 RD_STATS cfl_rd_arr_v[CFL_MAGS_SIZE];
520
521 av1_invalid_rd_stats(best_rd_stats);
522
523 cfl_pick_plane_parameter(cpi, x, 1, tx_size, cfl_search_range, cfl_rd_arr_u);
524 cfl_pick_plane_parameter(cpi, x, 2, tx_size, cfl_search_range, cfl_rd_arr_v);
525
526 for (int ui = 0; ui < CFL_MAGS_SIZE; ++ui) {
527 if (cfl_rd_arr_u[ui].rate == INT_MAX) continue;
528 int cfl_alpha_u;
529 CFL_SIGN_TYPE cfl_sign_u;
530 cfl_idx_to_sign_and_alpha(ui, &cfl_sign_u, &cfl_alpha_u);
531 for (int vi = 0; vi < CFL_MAGS_SIZE; ++vi) {
532 if (cfl_rd_arr_v[vi].rate == INT_MAX) continue;
533 int cfl_alpha_v;
534 CFL_SIGN_TYPE cfl_sign_v;
535 cfl_idx_to_sign_and_alpha(vi, &cfl_sign_v, &cfl_alpha_v);
536 // cfl_sign_u == CFL_SIGN_ZERO && cfl_sign_v == CFL_SIGN_ZERO is not a
537 // valid parameter for CFL
538 if (cfl_sign_u == CFL_SIGN_ZERO && cfl_sign_v == CFL_SIGN_ZERO) continue;
539 int joint_sign = cfl_sign_u * CFL_SIGNS + cfl_sign_v - 1;
540 RD_STATS rd_stats = cfl_rd_arr_u[ui];
541 av1_merge_rd_stats(&rd_stats, &cfl_rd_arr_v[vi]);
542 if (rd_stats.rate != INT_MAX) {
543 rd_stats.rate +=
544 mode_costs->cfl_cost[joint_sign][CFL_PRED_U][cfl_alpha_u];
545 rd_stats.rate +=
546 mode_costs->cfl_cost[joint_sign][CFL_PRED_V][cfl_alpha_v];
547 }
548 av1_rd_cost_update(x->rdmult, &rd_stats);
549 if (rd_stats.rdcost < best_rd_stats->rdcost) {
550 *best_rd_stats = rd_stats;
551 *best_cfl_alpha_idx =
552 (cfl_alpha_u << CFL_ALPHABET_SIZE_LOG2) + cfl_alpha_v;
553 *best_cfl_alpha_signs = joint_sign;
554 }
555 }
556 }
557 if (best_rd_stats->rdcost >= ref_best_rd) {
558 av1_invalid_rd_stats(best_rd_stats);
559 // Set invalid CFL parameters here since the rdcost is not better than
560 // ref_best_rd.
561 *best_cfl_alpha_idx = 0;
562 *best_cfl_alpha_signs = 0;
563 return 0;
564 }
565 return 1;
566 }
567
av1_rd_pick_intra_sbuv_mode(const AV1_COMP * const cpi,MACROBLOCK * x,int * rate,int * rate_tokenonly,int64_t * distortion,int * skippable,BLOCK_SIZE bsize,TX_SIZE max_tx_size)568 int64_t av1_rd_pick_intra_sbuv_mode(const AV1_COMP *const cpi, MACROBLOCK *x,
569 int *rate, int *rate_tokenonly,
570 int64_t *distortion, int *skippable,
571 BLOCK_SIZE bsize, TX_SIZE max_tx_size) {
572 const AV1_COMMON *const cm = &cpi->common;
573 MACROBLOCKD *xd = &x->e_mbd;
574 MB_MODE_INFO *mbmi = xd->mi[0];
575 assert(!is_inter_block(mbmi));
576 MB_MODE_INFO best_mbmi = *mbmi;
577 int64_t best_rd = INT64_MAX, this_rd;
578 const ModeCosts *mode_costs = &x->mode_costs;
579 const IntraModeCfg *const intra_mode_cfg = &cpi->oxcf.intra_mode_cfg;
580
581 init_sbuv_mode(mbmi);
582
583 // Return if the current block does not correspond to a chroma block.
584 if (!xd->is_chroma_ref) {
585 *rate = 0;
586 *rate_tokenonly = 0;
587 *distortion = 0;
588 *skippable = 1;
589 return INT64_MAX;
590 }
591
592 // Only store reconstructed luma when there's chroma RDO. When there's no
593 // chroma RDO, the reconstructed luma will be stored in encode_superblock().
594 xd->cfl.store_y = store_cfl_required_rdo(cm, x);
595 if (xd->cfl.store_y) {
596 // Restore reconstructed luma values.
597 // TODO(chiyotsai@google.com): right now we are re-computing the txfm in
598 // this function everytime we search through uv modes. There is some
599 // potential speed up here if we cache the result to avoid redundant
600 // computation.
601 av1_encode_intra_block_plane(cpi, x, mbmi->bsize, AOM_PLANE_Y,
602 DRY_RUN_NORMAL,
603 cpi->optimize_seg_arr[mbmi->segment_id]);
604 xd->cfl.store_y = 0;
605 }
606 IntraModeSearchState intra_search_state;
607 init_intra_mode_search_state(&intra_search_state);
608
609 // Search through all non-palette modes.
610 for (int mode_idx = 0; mode_idx < UV_INTRA_MODES; ++mode_idx) {
611 int this_rate;
612 RD_STATS tokenonly_rd_stats;
613 UV_PREDICTION_MODE mode = uv_rd_search_mode_order[mode_idx];
614 const int is_diagonal_mode = av1_is_diagonal_mode(get_uv_mode(mode));
615 const int is_directional_mode = av1_is_directional_mode(get_uv_mode(mode));
616
617 if (is_diagonal_mode && !cpi->oxcf.intra_mode_cfg.enable_diagonal_intra)
618 continue;
619 if (is_directional_mode &&
620 !cpi->oxcf.intra_mode_cfg.enable_directional_intra)
621 continue;
622
623 if (!(cpi->sf.intra_sf.intra_uv_mode_mask[txsize_sqr_up_map[max_tx_size]] &
624 (1 << mode)))
625 continue;
626 if (!intra_mode_cfg->enable_smooth_intra && mode >= UV_SMOOTH_PRED &&
627 mode <= UV_SMOOTH_H_PRED)
628 continue;
629
630 if (!intra_mode_cfg->enable_paeth_intra && mode == UV_PAETH_PRED) continue;
631
632 assert(mbmi->mode < INTRA_MODES);
633 if (cpi->sf.intra_sf.prune_chroma_modes_using_luma_winner &&
634 !(av1_derived_chroma_intra_mode_used_flag[mbmi->mode] & (1 << mode)))
635 continue;
636
637 mbmi->uv_mode = mode;
638
639 // Init variables for cfl and angle delta
640 const SPEED_FEATURES *sf = &cpi->sf;
641 mbmi->angle_delta[PLANE_TYPE_UV] = 0;
642 if (mode == UV_CFL_PRED) {
643 if (!is_cfl_allowed(xd) || !intra_mode_cfg->enable_cfl_intra) continue;
644 assert(!is_directional_mode);
645 const TX_SIZE uv_tx_size = av1_get_tx_size(AOM_PLANE_U, xd);
646 if (!cfl_rd_pick_alpha(x, cpi, uv_tx_size, best_rd,
647 sf->intra_sf.cfl_search_range, &tokenonly_rd_stats,
648 &mbmi->cfl_alpha_idx, &mbmi->cfl_alpha_signs)) {
649 continue;
650 }
651 } else if (is_directional_mode && av1_use_angle_delta(mbmi->bsize) &&
652 intra_mode_cfg->enable_angle_delta) {
653 if (sf->intra_sf.chroma_intra_pruning_with_hog &&
654 !intra_search_state.dir_mode_skip_mask_ready) {
655 static const float thresh[2][4] = {
656 { -1.2f, 0.0f, 0.0f, 1.2f }, // Interframe
657 { -1.2f, -1.2f, -0.6f, 0.4f }, // Intraframe
658 };
659 const int is_chroma = 1;
660 const int is_intra_frame = frame_is_intra_only(cm);
661 prune_intra_mode_with_hog(
662 x, bsize, cm->seq_params->sb_size,
663 thresh[is_intra_frame]
664 [sf->intra_sf.chroma_intra_pruning_with_hog - 1],
665 intra_search_state.directional_mode_skip_mask, is_chroma);
666 intra_search_state.dir_mode_skip_mask_ready = 1;
667 }
668 if (intra_search_state.directional_mode_skip_mask[mode]) {
669 continue;
670 }
671
672 // Search through angle delta
673 const int rate_overhead =
674 mode_costs->intra_uv_mode_cost[is_cfl_allowed(xd)][mbmi->mode][mode];
675 if (!rd_pick_intra_angle_sbuv(cpi, x, bsize, rate_overhead, best_rd,
676 &this_rate, &tokenonly_rd_stats))
677 continue;
678 } else {
679 // Predict directly if we don't need to search for angle delta.
680 if (!av1_txfm_uvrd(cpi, x, &tokenonly_rd_stats, bsize, best_rd)) {
681 continue;
682 }
683 }
684 const int mode_cost =
685 mode_costs->intra_uv_mode_cost[is_cfl_allowed(xd)][mbmi->mode][mode];
686 this_rate = tokenonly_rd_stats.rate +
687 intra_mode_info_cost_uv(cpi, x, mbmi, bsize, mode_cost);
688 this_rd = RDCOST(x->rdmult, this_rate, tokenonly_rd_stats.dist);
689
690 if (this_rd < best_rd) {
691 best_mbmi = *mbmi;
692 best_rd = this_rd;
693 *rate = this_rate;
694 *rate_tokenonly = tokenonly_rd_stats.rate;
695 *distortion = tokenonly_rd_stats.dist;
696 *skippable = tokenonly_rd_stats.skip_txfm;
697 }
698 }
699
700 // Search palette mode
701 const int try_palette =
702 cpi->oxcf.tool_cfg.enable_palette &&
703 av1_allow_palette(cpi->common.features.allow_screen_content_tools,
704 mbmi->bsize);
705 if (try_palette) {
706 uint8_t *best_palette_color_map = x->palette_buffer->best_palette_color_map;
707 av1_rd_pick_palette_intra_sbuv(
708 cpi, x,
709 mode_costs
710 ->intra_uv_mode_cost[is_cfl_allowed(xd)][mbmi->mode][UV_DC_PRED],
711 best_palette_color_map, &best_mbmi, &best_rd, rate, rate_tokenonly,
712 distortion, skippable);
713 }
714
715 *mbmi = best_mbmi;
716 // Make sure we actually chose a mode
717 assert(best_rd < INT64_MAX);
718 return best_rd;
719 }
720
721 // Searches palette mode for luma channel in inter frame.
av1_search_palette_mode(IntraModeSearchState * intra_search_state,const AV1_COMP * cpi,MACROBLOCK * x,BLOCK_SIZE bsize,unsigned int ref_frame_cost,PICK_MODE_CONTEXT * ctx,RD_STATS * this_rd_cost,int64_t best_rd)722 int av1_search_palette_mode(IntraModeSearchState *intra_search_state,
723 const AV1_COMP *cpi, MACROBLOCK *x,
724 BLOCK_SIZE bsize, unsigned int ref_frame_cost,
725 PICK_MODE_CONTEXT *ctx, RD_STATS *this_rd_cost,
726 int64_t best_rd) {
727 const AV1_COMMON *const cm = &cpi->common;
728 MB_MODE_INFO *const mbmi = x->e_mbd.mi[0];
729 PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info;
730 const int num_planes = av1_num_planes(cm);
731 MACROBLOCKD *const xd = &x->e_mbd;
732 int rate2 = 0;
733 int64_t distortion2 = 0, best_rd_palette = best_rd, this_rd;
734 int skippable = 0;
735 uint8_t *const best_palette_color_map =
736 x->palette_buffer->best_palette_color_map;
737 uint8_t *const color_map = xd->plane[0].color_index_map;
738 MB_MODE_INFO best_mbmi_palette = *mbmi;
739 uint8_t best_blk_skip[MAX_MIB_SIZE * MAX_MIB_SIZE];
740 uint8_t best_tx_type_map[MAX_MIB_SIZE * MAX_MIB_SIZE];
741 const ModeCosts *mode_costs = &x->mode_costs;
742 const int *const intra_mode_cost =
743 mode_costs->mbmode_cost[size_group_lookup[bsize]];
744 const int rows = block_size_high[bsize];
745 const int cols = block_size_wide[bsize];
746
747 mbmi->mode = DC_PRED;
748 mbmi->uv_mode = UV_DC_PRED;
749 mbmi->ref_frame[0] = INTRA_FRAME;
750 mbmi->ref_frame[1] = NONE_FRAME;
751 av1_zero(pmi->palette_size);
752
753 RD_STATS rd_stats_y;
754 av1_invalid_rd_stats(&rd_stats_y);
755 av1_rd_pick_palette_intra_sby(cpi, x, bsize, intra_mode_cost[DC_PRED],
756 &best_mbmi_palette, best_palette_color_map,
757 &best_rd_palette, &rd_stats_y.rate, NULL,
758 &rd_stats_y.dist, &rd_stats_y.skip_txfm, NULL,
759 ctx, best_blk_skip, best_tx_type_map);
760 if (rd_stats_y.rate == INT_MAX || pmi->palette_size[0] == 0) {
761 this_rd_cost->rdcost = INT64_MAX;
762 return skippable;
763 }
764
765 memcpy(x->txfm_search_info.blk_skip, best_blk_skip,
766 sizeof(best_blk_skip[0]) * bsize_to_num_blk(bsize));
767 av1_copy_array(xd->tx_type_map, best_tx_type_map, ctx->num_4x4_blk);
768 memcpy(color_map, best_palette_color_map,
769 rows * cols * sizeof(best_palette_color_map[0]));
770
771 skippable = rd_stats_y.skip_txfm;
772 distortion2 = rd_stats_y.dist;
773 rate2 = rd_stats_y.rate + ref_frame_cost;
774 if (num_planes > 1) {
775 if (intra_search_state->rate_uv_intra == INT_MAX) {
776 // We have not found any good uv mode yet, so we need to search for it.
777 TX_SIZE uv_tx = av1_get_tx_size(AOM_PLANE_U, xd);
778 av1_rd_pick_intra_sbuv_mode(cpi, x, &intra_search_state->rate_uv_intra,
779 &intra_search_state->rate_uv_tokenonly,
780 &intra_search_state->dist_uvs,
781 &intra_search_state->skip_uvs, bsize, uv_tx);
782 intra_search_state->mode_uv = mbmi->uv_mode;
783 intra_search_state->pmi_uv = *pmi;
784 intra_search_state->uv_angle_delta = mbmi->angle_delta[PLANE_TYPE_UV];
785 }
786
787 // We have found at least one good uv mode before, so copy and paste it
788 // over.
789 mbmi->uv_mode = intra_search_state->mode_uv;
790 pmi->palette_size[1] = intra_search_state->pmi_uv.palette_size[1];
791 if (pmi->palette_size[1] > 0) {
792 memcpy(pmi->palette_colors + PALETTE_MAX_SIZE,
793 intra_search_state->pmi_uv.palette_colors + PALETTE_MAX_SIZE,
794 2 * PALETTE_MAX_SIZE * sizeof(pmi->palette_colors[0]));
795 }
796 mbmi->angle_delta[PLANE_TYPE_UV] = intra_search_state->uv_angle_delta;
797 skippable = skippable && intra_search_state->skip_uvs;
798 distortion2 += intra_search_state->dist_uvs;
799 rate2 += intra_search_state->rate_uv_intra;
800 }
801
802 if (skippable) {
803 rate2 -= rd_stats_y.rate;
804 if (num_planes > 1) rate2 -= intra_search_state->rate_uv_tokenonly;
805 rate2 += mode_costs->skip_txfm_cost[av1_get_skip_txfm_context(xd)][1];
806 } else {
807 rate2 += mode_costs->skip_txfm_cost[av1_get_skip_txfm_context(xd)][0];
808 }
809 this_rd = RDCOST(x->rdmult, rate2, distortion2);
810 this_rd_cost->rate = rate2;
811 this_rd_cost->dist = distortion2;
812 this_rd_cost->rdcost = this_rd;
813 return skippable;
814 }
815
816 /*!\brief Get the intra prediction by searching through tx_type and tx_size.
817 *
818 * \ingroup intra_mode_search
819 * \callergraph
820 * Currently this function is only used in the intra frame code path for
821 * winner-mode processing.
822 *
823 * \return Returns whether the current mode is an improvement over best_rd.
824 */
intra_block_yrd(const AV1_COMP * const cpi,MACROBLOCK * x,BLOCK_SIZE bsize,const int * bmode_costs,int64_t * best_rd,int * rate,int * rate_tokenonly,int64_t * distortion,int * skippable,MB_MODE_INFO * best_mbmi,PICK_MODE_CONTEXT * ctx)825 static AOM_INLINE int intra_block_yrd(const AV1_COMP *const cpi, MACROBLOCK *x,
826 BLOCK_SIZE bsize, const int *bmode_costs,
827 int64_t *best_rd, int *rate,
828 int *rate_tokenonly, int64_t *distortion,
829 int *skippable, MB_MODE_INFO *best_mbmi,
830 PICK_MODE_CONTEXT *ctx) {
831 MACROBLOCKD *const xd = &x->e_mbd;
832 MB_MODE_INFO *const mbmi = xd->mi[0];
833 RD_STATS rd_stats;
834 // In order to improve txfm search avoid rd based breakouts during winner
835 // mode evaluation. Hence passing ref_best_rd as a maximum value
836 av1_pick_uniform_tx_size_type_yrd(cpi, x, &rd_stats, bsize, INT64_MAX);
837 if (rd_stats.rate == INT_MAX) return 0;
838 int this_rate_tokenonly = rd_stats.rate;
839 if (!xd->lossless[mbmi->segment_id] && block_signals_txsize(mbmi->bsize)) {
840 // av1_pick_uniform_tx_size_type_yrd above includes the cost of the tx_size
841 // in the tokenonly rate, but for intra blocks, tx_size is always coded
842 // (prediction granularity), so we account for it in the full rate,
843 // not the tokenonly rate.
844 this_rate_tokenonly -= tx_size_cost(x, bsize, mbmi->tx_size);
845 }
846 const int this_rate =
847 rd_stats.rate +
848 intra_mode_info_cost_y(cpi, x, mbmi, bsize, bmode_costs[mbmi->mode]);
849 const int64_t this_rd = RDCOST(x->rdmult, this_rate, rd_stats.dist);
850 if (this_rd < *best_rd) {
851 *best_mbmi = *mbmi;
852 *best_rd = this_rd;
853 *rate = this_rate;
854 *rate_tokenonly = this_rate_tokenonly;
855 *distortion = rd_stats.dist;
856 *skippable = rd_stats.skip_txfm;
857 av1_copy_array(ctx->blk_skip, x->txfm_search_info.blk_skip,
858 ctx->num_4x4_blk);
859 av1_copy_array(ctx->tx_type_map, xd->tx_type_map, ctx->num_4x4_blk);
860 return 1;
861 }
862 return 0;
863 }
864
865 /*!\brief Search for the best filter_intra mode when coding inter frame.
866 *
867 * \ingroup intra_mode_search
868 * \callergraph
869 * This function loops through all filter_intra modes to find the best one.
870 *
871 * \return Returns nothing, but updates the mbmi and rd_stats.
872 */
handle_filter_intra_mode(const AV1_COMP * cpi,MACROBLOCK * x,BLOCK_SIZE bsize,const PICK_MODE_CONTEXT * ctx,RD_STATS * rd_stats_y,int mode_cost,int64_t best_rd,int64_t best_rd_so_far)873 static INLINE void handle_filter_intra_mode(const AV1_COMP *cpi, MACROBLOCK *x,
874 BLOCK_SIZE bsize,
875 const PICK_MODE_CONTEXT *ctx,
876 RD_STATS *rd_stats_y, int mode_cost,
877 int64_t best_rd,
878 int64_t best_rd_so_far) {
879 MACROBLOCKD *const xd = &x->e_mbd;
880 MB_MODE_INFO *const mbmi = xd->mi[0];
881 assert(mbmi->mode == DC_PRED &&
882 av1_filter_intra_allowed_bsize(&cpi->common, bsize));
883
884 RD_STATS rd_stats_y_fi;
885 int filter_intra_selected_flag = 0;
886 TX_SIZE best_tx_size = mbmi->tx_size;
887 FILTER_INTRA_MODE best_fi_mode = FILTER_DC_PRED;
888 uint8_t best_blk_skip[MAX_MIB_SIZE * MAX_MIB_SIZE];
889 memcpy(best_blk_skip, x->txfm_search_info.blk_skip,
890 sizeof(best_blk_skip[0]) * ctx->num_4x4_blk);
891 uint8_t best_tx_type_map[MAX_MIB_SIZE * MAX_MIB_SIZE];
892 av1_copy_array(best_tx_type_map, xd->tx_type_map, ctx->num_4x4_blk);
893 mbmi->filter_intra_mode_info.use_filter_intra = 1;
894 for (FILTER_INTRA_MODE fi_mode = FILTER_DC_PRED; fi_mode < FILTER_INTRA_MODES;
895 ++fi_mode) {
896 mbmi->filter_intra_mode_info.filter_intra_mode = fi_mode;
897 av1_pick_uniform_tx_size_type_yrd(cpi, x, &rd_stats_y_fi, bsize, best_rd);
898 if (rd_stats_y_fi.rate == INT_MAX) continue;
899 const int this_rate_tmp =
900 rd_stats_y_fi.rate +
901 intra_mode_info_cost_y(cpi, x, mbmi, bsize, mode_cost);
902 const int64_t this_rd_tmp =
903 RDCOST(x->rdmult, this_rate_tmp, rd_stats_y_fi.dist);
904
905 if (this_rd_tmp != INT64_MAX && this_rd_tmp / 2 > best_rd) {
906 break;
907 }
908 if (this_rd_tmp < best_rd_so_far) {
909 best_tx_size = mbmi->tx_size;
910 av1_copy_array(best_tx_type_map, xd->tx_type_map, ctx->num_4x4_blk);
911 memcpy(best_blk_skip, x->txfm_search_info.blk_skip,
912 sizeof(best_blk_skip[0]) * ctx->num_4x4_blk);
913 best_fi_mode = fi_mode;
914 *rd_stats_y = rd_stats_y_fi;
915 filter_intra_selected_flag = 1;
916 best_rd_so_far = this_rd_tmp;
917 }
918 }
919
920 mbmi->tx_size = best_tx_size;
921 av1_copy_array(xd->tx_type_map, best_tx_type_map, ctx->num_4x4_blk);
922 memcpy(x->txfm_search_info.blk_skip, best_blk_skip,
923 sizeof(x->txfm_search_info.blk_skip[0]) * ctx->num_4x4_blk);
924
925 if (filter_intra_selected_flag) {
926 mbmi->filter_intra_mode_info.use_filter_intra = 1;
927 mbmi->filter_intra_mode_info.filter_intra_mode = best_fi_mode;
928 } else {
929 mbmi->filter_intra_mode_info.use_filter_intra = 0;
930 }
931 }
932
933 // Evaluate a given luma intra-mode in inter frames.
av1_handle_intra_y_mode(IntraModeSearchState * intra_search_state,const AV1_COMP * cpi,MACROBLOCK * x,BLOCK_SIZE bsize,unsigned int ref_frame_cost,const PICK_MODE_CONTEXT * ctx,RD_STATS * rd_stats_y,int64_t best_rd,int * mode_cost_y,int64_t * rd_y,int64_t * best_model_rd,int64_t top_intra_model_rd[])934 int av1_handle_intra_y_mode(IntraModeSearchState *intra_search_state,
935 const AV1_COMP *cpi, MACROBLOCK *x,
936 BLOCK_SIZE bsize, unsigned int ref_frame_cost,
937 const PICK_MODE_CONTEXT *ctx, RD_STATS *rd_stats_y,
938 int64_t best_rd, int *mode_cost_y, int64_t *rd_y,
939 int64_t *best_model_rd,
940 int64_t top_intra_model_rd[]) {
941 const AV1_COMMON *cm = &cpi->common;
942 const SPEED_FEATURES *const sf = &cpi->sf;
943 MACROBLOCKD *const xd = &x->e_mbd;
944 MB_MODE_INFO *const mbmi = xd->mi[0];
945 assert(mbmi->ref_frame[0] == INTRA_FRAME);
946 const PREDICTION_MODE mode = mbmi->mode;
947 const ModeCosts *mode_costs = &x->mode_costs;
948 const int mode_cost =
949 mode_costs->mbmode_cost[size_group_lookup[bsize]][mode] + ref_frame_cost;
950 const int skip_ctx = av1_get_skip_txfm_context(xd);
951
952 int known_rate = mode_cost;
953 const int intra_cost_penalty = av1_get_intra_cost_penalty(
954 cm->quant_params.base_qindex, cm->quant_params.y_dc_delta_q,
955 cm->seq_params->bit_depth);
956
957 if (mode != DC_PRED && mode != PAETH_PRED) known_rate += intra_cost_penalty;
958 known_rate += AOMMIN(mode_costs->skip_txfm_cost[skip_ctx][0],
959 mode_costs->skip_txfm_cost[skip_ctx][1]);
960 const int64_t known_rd = RDCOST(x->rdmult, known_rate, 0);
961 if (known_rd > best_rd) {
962 intra_search_state->skip_intra_modes = 1;
963 return 0;
964 }
965
966 const int is_directional_mode = av1_is_directional_mode(mode);
967 if (is_directional_mode && av1_use_angle_delta(bsize) &&
968 cpi->oxcf.intra_mode_cfg.enable_angle_delta) {
969 if (sf->intra_sf.intra_pruning_with_hog &&
970 !intra_search_state->dir_mode_skip_mask_ready) {
971 const float thresh[4] = { -1.2f, 0.0f, 0.0f, 1.2f };
972 const int is_chroma = 0;
973 prune_intra_mode_with_hog(x, bsize, cm->seq_params->sb_size,
974 thresh[sf->intra_sf.intra_pruning_with_hog - 1],
975 intra_search_state->directional_mode_skip_mask,
976 is_chroma);
977 intra_search_state->dir_mode_skip_mask_ready = 1;
978 }
979 if (intra_search_state->directional_mode_skip_mask[mode]) return 0;
980 }
981 const TX_SIZE tx_size = AOMMIN(TX_32X32, max_txsize_lookup[bsize]);
982 const int64_t this_model_rd =
983 intra_model_rd(&cpi->common, x, 0, bsize, tx_size, /*use_hadamard=*/1);
984 if (prune_intra_y_mode(this_model_rd, best_model_rd, top_intra_model_rd,
985 sf->intra_sf.top_intra_model_count_allowed))
986 return 0;
987 av1_init_rd_stats(rd_stats_y);
988 av1_pick_uniform_tx_size_type_yrd(cpi, x, rd_stats_y, bsize, best_rd);
989
990 // Pick filter intra modes.
991 if (mode == DC_PRED && av1_filter_intra_allowed_bsize(cm, bsize)) {
992 int try_filter_intra = 1;
993 int64_t best_rd_so_far = INT64_MAX;
994 if (rd_stats_y->rate != INT_MAX) {
995 // best_rd_so_far is the rdcost of DC_PRED without using filter_intra.
996 // Later, in filter intra search, best_rd_so_far is used for comparison.
997 mbmi->filter_intra_mode_info.use_filter_intra = 0;
998 const int tmp_rate =
999 rd_stats_y->rate +
1000 intra_mode_info_cost_y(cpi, x, mbmi, bsize, mode_cost);
1001 best_rd_so_far = RDCOST(x->rdmult, tmp_rate, rd_stats_y->dist);
1002 try_filter_intra = (best_rd_so_far / 2) <= best_rd;
1003 } else if (sf->intra_sf.skip_filter_intra_in_inter_frames >= 1) {
1004 // As rd cost of luma intra dc mode is more than best_rd (i.e.,
1005 // rd_stats_y->rate = INT_MAX), skip the evaluation of filter intra modes.
1006 try_filter_intra = 0;
1007 }
1008
1009 if (try_filter_intra) {
1010 handle_filter_intra_mode(cpi, x, bsize, ctx, rd_stats_y, mode_cost,
1011 best_rd, best_rd_so_far);
1012 }
1013 }
1014
1015 if (rd_stats_y->rate == INT_MAX) return 0;
1016
1017 *mode_cost_y = intra_mode_info_cost_y(cpi, x, mbmi, bsize, mode_cost);
1018 const int rate_y = rd_stats_y->skip_txfm
1019 ? mode_costs->skip_txfm_cost[skip_ctx][1]
1020 : rd_stats_y->rate;
1021 *rd_y = RDCOST(x->rdmult, rate_y + *mode_cost_y, rd_stats_y->dist);
1022 if (best_rd < (INT64_MAX / 2) && *rd_y > (best_rd + (best_rd >> 2))) {
1023 intra_search_state->skip_intra_modes = 1;
1024 return 0;
1025 }
1026
1027 return 1;
1028 }
1029
av1_search_intra_uv_modes_in_interframe(IntraModeSearchState * intra_search_state,const AV1_COMP * cpi,MACROBLOCK * x,BLOCK_SIZE bsize,RD_STATS * rd_stats,const RD_STATS * rd_stats_y,RD_STATS * rd_stats_uv,int64_t best_rd)1030 int av1_search_intra_uv_modes_in_interframe(
1031 IntraModeSearchState *intra_search_state, const AV1_COMP *cpi,
1032 MACROBLOCK *x, BLOCK_SIZE bsize, RD_STATS *rd_stats,
1033 const RD_STATS *rd_stats_y, RD_STATS *rd_stats_uv, int64_t best_rd) {
1034 const AV1_COMMON *cm = &cpi->common;
1035 MACROBLOCKD *const xd = &x->e_mbd;
1036 MB_MODE_INFO *const mbmi = xd->mi[0];
1037 assert(mbmi->ref_frame[0] == INTRA_FRAME);
1038
1039 // TODO(chiyotsai@google.com): Consolidate the chroma search code here with
1040 // the one in av1_search_palette_mode.
1041 PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info;
1042 const int try_palette =
1043 cpi->oxcf.tool_cfg.enable_palette &&
1044 av1_allow_palette(cm->features.allow_screen_content_tools, mbmi->bsize);
1045
1046 assert(intra_search_state->rate_uv_intra == INT_MAX);
1047 if (intra_search_state->rate_uv_intra == INT_MAX) {
1048 // If no good uv-predictor had been found, search for it.
1049 const TX_SIZE uv_tx = av1_get_tx_size(AOM_PLANE_U, xd);
1050 av1_rd_pick_intra_sbuv_mode(cpi, x, &intra_search_state->rate_uv_intra,
1051 &intra_search_state->rate_uv_tokenonly,
1052 &intra_search_state->dist_uvs,
1053 &intra_search_state->skip_uvs, bsize, uv_tx);
1054 intra_search_state->mode_uv = mbmi->uv_mode;
1055 if (try_palette) intra_search_state->pmi_uv = *pmi;
1056 intra_search_state->uv_angle_delta = mbmi->angle_delta[PLANE_TYPE_UV];
1057
1058 const int uv_rate = intra_search_state->rate_uv_tokenonly;
1059 const int64_t uv_dist = intra_search_state->dist_uvs;
1060 const int64_t uv_rd = RDCOST(x->rdmult, uv_rate, uv_dist);
1061 if (uv_rd > best_rd) {
1062 // If there is no good intra uv-mode available, we can skip all intra
1063 // modes.
1064 intra_search_state->skip_intra_modes = 1;
1065 return 0;
1066 }
1067 }
1068
1069 // If we are here, then the encoder has found at least one good intra uv
1070 // predictor, so we can directly copy its statistics over.
1071 // TODO(any): the stats here is not right if the best uv mode is CFL but the
1072 // best y mode is palette.
1073 rd_stats_uv->rate = intra_search_state->rate_uv_tokenonly;
1074 rd_stats_uv->dist = intra_search_state->dist_uvs;
1075 rd_stats_uv->skip_txfm = intra_search_state->skip_uvs;
1076 rd_stats->skip_txfm = rd_stats_y->skip_txfm && rd_stats_uv->skip_txfm;
1077 mbmi->uv_mode = intra_search_state->mode_uv;
1078 if (try_palette) {
1079 pmi->palette_size[1] = intra_search_state->pmi_uv.palette_size[1];
1080 memcpy(pmi->palette_colors + PALETTE_MAX_SIZE,
1081 intra_search_state->pmi_uv.palette_colors + PALETTE_MAX_SIZE,
1082 2 * PALETTE_MAX_SIZE * sizeof(pmi->palette_colors[0]));
1083 }
1084 mbmi->angle_delta[PLANE_TYPE_UV] = intra_search_state->uv_angle_delta;
1085
1086 return 1;
1087 }
1088
1089 DECLARE_ALIGNED(16, static const uint8_t, all_zeros[MAX_SB_SIZE]) = { 0 };
1090 DECLARE_ALIGNED(16, static const uint16_t,
1091 highbd_all_zeros[MAX_SB_SIZE]) = { 0 };
1092 // Returns a factor to be applied to the RD value based on how well the
1093 // reconstructed block variance matches the source variance.
intra_rd_variance_factor(const AV1_COMP * cpi,MACROBLOCK * x,BLOCK_SIZE bs)1094 static double intra_rd_variance_factor(const AV1_COMP *cpi, MACROBLOCK *x,
1095 BLOCK_SIZE bs) {
1096 MACROBLOCKD *xd = &x->e_mbd;
1097 double variance_rd_factor = 1.0;
1098 double src_var = 0.0;
1099 double rec_var = 0.0;
1100 double var_diff = 0.0;
1101 double threshold = 1.0 - (0.25 * cpi->oxcf.speed);
1102 unsigned int sse;
1103 int i, j;
1104 int right_overflow =
1105 (xd->mb_to_right_edge < 0) ? ((-xd->mb_to_right_edge) >> 3) : 0;
1106 int bottom_overflow =
1107 (xd->mb_to_bottom_edge < 0) ? ((-xd->mb_to_bottom_edge) >> 3) : 0;
1108
1109 const int bw = MI_SIZE * mi_size_wide[bs] - right_overflow;
1110 const int bh = MI_SIZE * mi_size_high[bs] - bottom_overflow;
1111 const int blocks = (bw * bh) / 16;
1112
1113 for (i = 0; i < bh; i += 4) {
1114 for (j = 0; j < bw; j += 4) {
1115 if (is_cur_buf_hbd(xd)) {
1116 src_var +=
1117 log(1.0 + cpi->ppi->fn_ptr[BLOCK_4X4].vf(
1118 x->plane[0].src.buf + i * x->plane[0].src.stride + j,
1119 x->plane[0].src.stride,
1120 CONVERT_TO_BYTEPTR(highbd_all_zeros), 0, &sse) /
1121 16);
1122 rec_var += log(
1123 1.0 + cpi->ppi->fn_ptr[BLOCK_4X4].vf(
1124 xd->plane[0].dst.buf + i * xd->plane[0].dst.stride + j,
1125 xd->plane[0].dst.stride,
1126 CONVERT_TO_BYTEPTR(highbd_all_zeros), 0, &sse) /
1127 16);
1128 } else {
1129 src_var +=
1130 log(1.0 + cpi->ppi->fn_ptr[BLOCK_4X4].vf(
1131 x->plane[0].src.buf + i * x->plane[0].src.stride + j,
1132 x->plane[0].src.stride, all_zeros, 0, &sse) /
1133 16);
1134 rec_var += log(
1135 1.0 + cpi->ppi->fn_ptr[BLOCK_4X4].vf(
1136 xd->plane[0].dst.buf + i * xd->plane[0].dst.stride + j,
1137 xd->plane[0].dst.stride, all_zeros, 0, &sse) /
1138 16);
1139 }
1140 }
1141 }
1142 src_var /= (double)blocks;
1143 rec_var /= (double)blocks;
1144
1145 // Only take action when the spatial complexity is low
1146 if ((rec_var < threshold) || (src_var < threshold)) {
1147 // Dont allow 0 to prevent / 0 below.
1148 src_var += 0.000001;
1149 rec_var += 0.000001;
1150
1151 // Heavier weigth if the reconstruction has lower variance.
1152 if (src_var >= rec_var) {
1153 var_diff = (src_var - rec_var) * 2;
1154 variance_rd_factor = 1.0 + (var_diff / src_var);
1155 } else {
1156 var_diff = (rec_var - src_var) / 2;
1157 variance_rd_factor = 1.0 + (var_diff / src_var);
1158 }
1159
1160 // Limit adjustment;
1161 variance_rd_factor = AOMMIN(3.0, variance_rd_factor);
1162 }
1163
1164 return variance_rd_factor;
1165 }
1166
1167 // Finds the best non-intrabc mode on an intra frame.
av1_rd_pick_intra_sby_mode(const AV1_COMP * const cpi,MACROBLOCK * x,int * rate,int * rate_tokenonly,int64_t * distortion,int * skippable,BLOCK_SIZE bsize,int64_t best_rd,PICK_MODE_CONTEXT * ctx)1168 int64_t av1_rd_pick_intra_sby_mode(const AV1_COMP *const cpi, MACROBLOCK *x,
1169 int *rate, int *rate_tokenonly,
1170 int64_t *distortion, int *skippable,
1171 BLOCK_SIZE bsize, int64_t best_rd,
1172 PICK_MODE_CONTEXT *ctx) {
1173 MACROBLOCKD *const xd = &x->e_mbd;
1174 MB_MODE_INFO *const mbmi = xd->mi[0];
1175 assert(!is_inter_block(mbmi));
1176 int64_t best_model_rd = INT64_MAX;
1177 int is_directional_mode;
1178 uint8_t directional_mode_skip_mask[INTRA_MODES] = { 0 };
1179 // Flag to check rd of any intra mode is better than best_rd passed to this
1180 // function
1181 int beat_best_rd = 0;
1182 const int *bmode_costs;
1183 PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info;
1184 const int try_palette =
1185 cpi->oxcf.tool_cfg.enable_palette &&
1186 av1_allow_palette(cpi->common.features.allow_screen_content_tools,
1187 mbmi->bsize);
1188 uint8_t *best_palette_color_map =
1189 try_palette ? x->palette_buffer->best_palette_color_map : NULL;
1190 const MB_MODE_INFO *above_mi = xd->above_mbmi;
1191 const MB_MODE_INFO *left_mi = xd->left_mbmi;
1192 const PREDICTION_MODE A = av1_above_block_mode(above_mi);
1193 const PREDICTION_MODE L = av1_left_block_mode(left_mi);
1194 const int above_ctx = intra_mode_context[A];
1195 const int left_ctx = intra_mode_context[L];
1196 bmode_costs = x->mode_costs.y_mode_costs[above_ctx][left_ctx];
1197
1198 mbmi->angle_delta[PLANE_TYPE_Y] = 0;
1199 if (cpi->sf.intra_sf.intra_pruning_with_hog) {
1200 // Less aggressive thresholds are used here than those used in inter frame
1201 // encoding in av1_handle_intra_y_mode() because we want key frames/intra
1202 // frames to have higher quality.
1203 const float thresh[4] = { -1.2f, -1.2f, -0.6f, 0.4f };
1204 const int is_chroma = 0;
1205 prune_intra_mode_with_hog(
1206 x, bsize, cpi->common.seq_params->sb_size,
1207 thresh[cpi->sf.intra_sf.intra_pruning_with_hog - 1],
1208 directional_mode_skip_mask, is_chroma);
1209 }
1210 mbmi->filter_intra_mode_info.use_filter_intra = 0;
1211 pmi->palette_size[0] = 0;
1212
1213 // Set params for mode evaluation
1214 set_mode_eval_params(cpi, x, MODE_EVAL);
1215
1216 MB_MODE_INFO best_mbmi = *mbmi;
1217 zero_winner_mode_stats(bsize, MAX_WINNER_MODE_COUNT_INTRA,
1218 x->winner_mode_stats);
1219 x->winner_mode_count = 0;
1220
1221 // Searches the intra-modes except for intrabc, palette, and filter_intra.
1222 int64_t top_intra_model_rd[TOP_INTRA_MODEL_COUNT];
1223 for (int i = 0; i < TOP_INTRA_MODEL_COUNT; i++) {
1224 top_intra_model_rd[i] = INT64_MAX;
1225 }
1226 for (int mode_idx = INTRA_MODE_START; mode_idx < LUMA_MODE_COUNT;
1227 ++mode_idx) {
1228 set_y_mode_and_delta_angle(mode_idx, mbmi);
1229 RD_STATS this_rd_stats;
1230 int this_rate, this_rate_tokenonly, s;
1231 int is_diagonal_mode;
1232 int64_t this_distortion, this_rd;
1233
1234 is_diagonal_mode = av1_is_diagonal_mode(mbmi->mode);
1235 if (is_diagonal_mode && !cpi->oxcf.intra_mode_cfg.enable_diagonal_intra)
1236 continue;
1237 if (av1_is_directional_mode(mbmi->mode) &&
1238 !cpi->oxcf.intra_mode_cfg.enable_directional_intra)
1239 continue;
1240
1241 // The smooth prediction mode appears to be more frequently picked
1242 // than horizontal / vertical smooth prediction modes. Hence treat
1243 // them differently in speed features.
1244 if ((!cpi->oxcf.intra_mode_cfg.enable_smooth_intra ||
1245 cpi->sf.intra_sf.disable_smooth_intra) &&
1246 (mbmi->mode == SMOOTH_H_PRED || mbmi->mode == SMOOTH_V_PRED))
1247 continue;
1248 if (!cpi->oxcf.intra_mode_cfg.enable_smooth_intra &&
1249 mbmi->mode == SMOOTH_PRED)
1250 continue;
1251
1252 // The functionality of filter intra modes and smooth prediction
1253 // overlap. Hence smooth prediction is pruned only if all the
1254 // filter intra modes are enabled.
1255 if (cpi->sf.intra_sf.disable_smooth_intra &&
1256 cpi->sf.intra_sf.prune_filter_intra_level == 0 &&
1257 mbmi->mode == SMOOTH_PRED)
1258 continue;
1259 if (!cpi->oxcf.intra_mode_cfg.enable_paeth_intra &&
1260 mbmi->mode == PAETH_PRED)
1261 continue;
1262
1263 // Skip the evaluation of modes that do not match with the winner mode in
1264 // x->mb_mode_cache.
1265 if (x->use_mb_mode_cache && mbmi->mode != x->mb_mode_cache->mode) continue;
1266
1267 is_directional_mode = av1_is_directional_mode(mbmi->mode);
1268 if (is_directional_mode && directional_mode_skip_mask[mbmi->mode]) continue;
1269 if (is_directional_mode && av1_use_angle_delta(bsize) == 0 &&
1270 mbmi->angle_delta[PLANE_TYPE_Y] != 0)
1271 continue;
1272
1273 // Use intra_y_mode_mask speed feature to skip intra mode evaluation.
1274 if (!(cpi->sf.intra_sf.intra_y_mode_mask[max_txsize_lookup[bsize]] &
1275 (1 << mbmi->mode)))
1276 continue;
1277
1278 const TX_SIZE tx_size = AOMMIN(TX_32X32, max_txsize_lookup[bsize]);
1279 const int64_t this_model_rd =
1280 intra_model_rd(&cpi->common, x, 0, bsize, tx_size, /*use_hadamard=*/1);
1281 if (prune_intra_y_mode(this_model_rd, &best_model_rd, top_intra_model_rd,
1282 cpi->sf.intra_sf.top_intra_model_count_allowed))
1283 continue;
1284
1285 // Builds the actual prediction. The prediction from
1286 // model_intra_yrd_and_prune was just an estimation that did not take into
1287 // account the effect of txfm pipeline, so we need to redo it for real
1288 // here.
1289 av1_pick_uniform_tx_size_type_yrd(cpi, x, &this_rd_stats, bsize, best_rd);
1290 this_rate_tokenonly = this_rd_stats.rate;
1291 this_distortion = this_rd_stats.dist;
1292 s = this_rd_stats.skip_txfm;
1293
1294 if (this_rate_tokenonly == INT_MAX) continue;
1295
1296 if (!xd->lossless[mbmi->segment_id] && block_signals_txsize(mbmi->bsize)) {
1297 // av1_pick_uniform_tx_size_type_yrd above includes the cost of the
1298 // tx_size in the tokenonly rate, but for intra blocks, tx_size is always
1299 // coded (prediction granularity), so we account for it in the full rate,
1300 // not the tokenonly rate.
1301 this_rate_tokenonly -= tx_size_cost(x, bsize, mbmi->tx_size);
1302 }
1303 this_rate =
1304 this_rd_stats.rate +
1305 intra_mode_info_cost_y(cpi, x, mbmi, bsize, bmode_costs[mbmi->mode]);
1306 this_rd = RDCOST(x->rdmult, this_rate, this_distortion);
1307
1308 // Visual quality adjustment based on recon vs source variance.
1309 if ((cpi->oxcf.mode == ALLINTRA) && (this_rd != INT64_MAX)) {
1310 this_rd = (int64_t)(this_rd * intra_rd_variance_factor(cpi, x, bsize));
1311 }
1312
1313 // Collect mode stats for multiwinner mode processing
1314 const int txfm_search_done = 1;
1315 store_winner_mode_stats(
1316 &cpi->common, x, mbmi, NULL, NULL, NULL, 0, NULL, bsize, this_rd,
1317 cpi->sf.winner_mode_sf.multi_winner_mode_type, txfm_search_done);
1318 if (this_rd < best_rd) {
1319 best_mbmi = *mbmi;
1320 best_rd = this_rd;
1321 // Setting beat_best_rd flag because current mode rd is better than
1322 // best_rd passed to this function
1323 beat_best_rd = 1;
1324 *rate = this_rate;
1325 *rate_tokenonly = this_rate_tokenonly;
1326 *distortion = this_distortion;
1327 *skippable = s;
1328 memcpy(ctx->blk_skip, x->txfm_search_info.blk_skip,
1329 sizeof(x->txfm_search_info.blk_skip[0]) * ctx->num_4x4_blk);
1330 av1_copy_array(ctx->tx_type_map, xd->tx_type_map, ctx->num_4x4_blk);
1331 }
1332 }
1333
1334 // Searches palette
1335 if (try_palette) {
1336 av1_rd_pick_palette_intra_sby(
1337 cpi, x, bsize, bmode_costs[DC_PRED], &best_mbmi, best_palette_color_map,
1338 &best_rd, rate, rate_tokenonly, distortion, skippable, &beat_best_rd,
1339 ctx, ctx->blk_skip, ctx->tx_type_map);
1340 }
1341
1342 // Searches filter_intra
1343 if (beat_best_rd && av1_filter_intra_allowed_bsize(&cpi->common, bsize)) {
1344 if (rd_pick_filter_intra_sby(cpi, x, rate, rate_tokenonly, distortion,
1345 skippable, bsize, bmode_costs[DC_PRED],
1346 best_mbmi.mode, &best_rd, &best_model_rd,
1347 ctx)) {
1348 best_mbmi = *mbmi;
1349 }
1350 }
1351
1352 // No mode is identified with less rd value than best_rd passed to this
1353 // function. In such cases winner mode processing is not necessary and return
1354 // best_rd as INT64_MAX to indicate best mode is not identified
1355 if (!beat_best_rd) return INT64_MAX;
1356
1357 // In multi-winner mode processing, perform tx search for few best modes
1358 // identified during mode evaluation. Winner mode processing uses best tx
1359 // configuration for tx search.
1360 if (cpi->sf.winner_mode_sf.multi_winner_mode_type) {
1361 int best_mode_idx = 0;
1362 int block_width, block_height;
1363 uint8_t *color_map_dst = xd->plane[PLANE_TYPE_Y].color_index_map;
1364 av1_get_block_dimensions(bsize, AOM_PLANE_Y, xd, &block_width,
1365 &block_height, NULL, NULL);
1366
1367 for (int mode_idx = 0; mode_idx < x->winner_mode_count; mode_idx++) {
1368 *mbmi = x->winner_mode_stats[mode_idx].mbmi;
1369 if (is_winner_mode_processing_enabled(cpi, mbmi, mbmi->mode)) {
1370 // Restore color_map of palette mode before winner mode processing
1371 if (mbmi->palette_mode_info.palette_size[0] > 0) {
1372 uint8_t *color_map_src =
1373 x->winner_mode_stats[mode_idx].color_index_map;
1374 memcpy(color_map_dst, color_map_src,
1375 block_width * block_height * sizeof(*color_map_src));
1376 }
1377 // Set params for winner mode evaluation
1378 set_mode_eval_params(cpi, x, WINNER_MODE_EVAL);
1379
1380 // Winner mode processing
1381 // If previous searches use only the default tx type/no R-D optimization
1382 // of quantized coeffs, do an extra search for the best tx type/better
1383 // R-D optimization of quantized coeffs
1384 if (intra_block_yrd(cpi, x, bsize, bmode_costs, &best_rd, rate,
1385 rate_tokenonly, distortion, skippable, &best_mbmi,
1386 ctx))
1387 best_mode_idx = mode_idx;
1388 }
1389 }
1390 // Copy color_map of palette mode for final winner mode
1391 if (best_mbmi.palette_mode_info.palette_size[0] > 0) {
1392 uint8_t *color_map_src =
1393 x->winner_mode_stats[best_mode_idx].color_index_map;
1394 memcpy(color_map_dst, color_map_src,
1395 block_width * block_height * sizeof(*color_map_src));
1396 }
1397 } else {
1398 // If previous searches use only the default tx type/no R-D optimization of
1399 // quantized coeffs, do an extra search for the best tx type/better R-D
1400 // optimization of quantized coeffs
1401 if (is_winner_mode_processing_enabled(cpi, mbmi, best_mbmi.mode)) {
1402 // Set params for winner mode evaluation
1403 set_mode_eval_params(cpi, x, WINNER_MODE_EVAL);
1404 *mbmi = best_mbmi;
1405 intra_block_yrd(cpi, x, bsize, bmode_costs, &best_rd, rate,
1406 rate_tokenonly, distortion, skippable, &best_mbmi, ctx);
1407 }
1408 }
1409 *mbmi = best_mbmi;
1410 av1_copy_array(xd->tx_type_map, ctx->tx_type_map, ctx->num_4x4_blk);
1411 return best_rd;
1412 }
1413