1 /*
2 * Copyright (c) 2016, 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
13 #include <assert.h>
14 #include <limits.h>
15 #include <math.h>
16 #include <stdio.h>
17
18 #include "config/aom_dsp_rtcd.h"
19 #include "config/av1_rtcd.h"
20
21 #include "aom_dsp/aom_dsp_common.h"
22 #include "aom_dsp/blend.h"
23 #include "aom_mem/aom_mem.h"
24 #include "aom_ports/aom_timer.h"
25 #include "aom_ports/mem.h"
26 #include "aom_ports/system_state.h"
27
28 #include "av1/encoder/model_rd.h"
29 #include "av1/common/mvref_common.h"
30 #include "av1/common/pred_common.h"
31 #include "av1/common/reconinter.h"
32 #include "av1/common/reconintra.h"
33
34 #include "av1/encoder/encodemv.h"
35 #include "av1/encoder/rdopt.h"
36 #include "av1/encoder/reconinter_enc.h"
37
38 extern int g_pick_inter_mode_cnt;
39 typedef struct {
40 uint8_t *data;
41 int stride;
42 int in_use;
43 } PRED_BUFFER;
44
45 typedef struct {
46 PRED_BUFFER *best_pred;
47 PREDICTION_MODE best_mode;
48 TX_SIZE best_tx_size;
49 TX_SIZE best_intra_tx_size;
50 MV_REFERENCE_FRAME best_ref_frame;
51 MV_REFERENCE_FRAME best_second_ref_frame;
52 uint8_t best_mode_skip_txfm;
53 int_interpfilters best_pred_filter;
54 } BEST_PICKMODE;
55
56 typedef struct {
57 MV_REFERENCE_FRAME ref_frame;
58 PREDICTION_MODE pred_mode;
59 } REF_MODE;
60
61 static const int pos_shift_16x16[4][4] = {
62 { 9, 10, 13, 14 }, { 11, 12, 15, 16 }, { 17, 18, 21, 22 }, { 19, 20, 23, 24 }
63 };
64
65 #define RT_INTER_MODES 9
66 static const REF_MODE ref_mode_set[RT_INTER_MODES] = {
67 { LAST_FRAME, NEARESTMV }, { LAST_FRAME, NEARMV },
68 { LAST_FRAME, NEWMV }, { GOLDEN_FRAME, NEARESTMV },
69 { GOLDEN_FRAME, NEARMV }, { GOLDEN_FRAME, NEWMV },
70 { ALTREF_FRAME, NEARESTMV }, { ALTREF_FRAME, NEARMV },
71 { ALTREF_FRAME, NEWMV }
72 };
73
74 static const THR_MODES mode_idx[REF_FRAMES][4] = {
75 { THR_DC, THR_V_PRED, THR_H_PRED, THR_SMOOTH },
76 { THR_NEARESTMV, THR_NEARMV, THR_GLOBALMV, THR_NEWMV },
77 { THR_NEARESTL2, THR_NEARL2, THR_GLOBALL2, THR_NEWL2 },
78 { THR_NEARESTL3, THR_NEARL3, THR_GLOBALL3, THR_NEWL3 },
79 { THR_NEARESTG, THR_NEARG, THR_GLOBALMV, THR_NEWG },
80 };
81
82 static const PREDICTION_MODE intra_mode_list[] = { DC_PRED, V_PRED, H_PRED,
83 SMOOTH_PRED };
84
mode_offset(const PREDICTION_MODE mode)85 static INLINE int mode_offset(const PREDICTION_MODE mode) {
86 if (mode >= NEARESTMV) {
87 return INTER_OFFSET(mode);
88 } else {
89 switch (mode) {
90 case DC_PRED: return 0;
91 case V_PRED: return 1;
92 case H_PRED: return 2;
93 case SMOOTH_PRED: return 3;
94 default: assert(0); return -1;
95 }
96 }
97 }
98
99 enum {
100 // INTER_ALL = (1 << NEARESTMV) | (1 << NEARMV) | (1 << NEWMV),
101 INTER_NEAREST = (1 << NEARESTMV),
102 INTER_NEAREST_NEW = (1 << NEARESTMV) | (1 << NEWMV),
103 INTER_NEAREST_NEAR = (1 << NEARESTMV) | (1 << NEARMV),
104 INTER_NEAR_NEW = (1 << NEARMV) | (1 << NEWMV),
105 };
106
init_best_pickmode(BEST_PICKMODE * bp)107 static INLINE void init_best_pickmode(BEST_PICKMODE *bp) {
108 bp->best_mode = NEARESTMV;
109 bp->best_ref_frame = LAST_FRAME;
110 bp->best_tx_size = TX_8X8;
111 bp->best_intra_tx_size = TX_8X8;
112 bp->best_pred_filter = av1_broadcast_interp_filter(EIGHTTAP_REGULAR);
113 bp->best_mode_skip_txfm = 0;
114 bp->best_second_ref_frame = NONE_FRAME;
115 bp->best_pred = NULL;
116 }
117
combined_motion_search(AV1_COMP * cpi,MACROBLOCK * x,BLOCK_SIZE bsize,int mi_row,int mi_col,int_mv * tmp_mv,int * rate_mv,int64_t best_rd_sofar,int use_base_mv)118 static int combined_motion_search(AV1_COMP *cpi, MACROBLOCK *x,
119 BLOCK_SIZE bsize, int mi_row, int mi_col,
120 int_mv *tmp_mv, int *rate_mv,
121 int64_t best_rd_sofar, int use_base_mv) {
122 MACROBLOCKD *xd = &x->e_mbd;
123 const AV1_COMMON *cm = &cpi->common;
124 const int num_planes = av1_num_planes(cm);
125 MB_MODE_INFO *mi = xd->mi[0];
126 struct buf_2d backup_yv12[MAX_MB_PLANE] = { { 0, 0, 0, 0, 0 } };
127 int step_param = cpi->mv_search_params.mv_step_param;
128 FULLPEL_MV start_mv;
129 const int ref = mi->ref_frame[0];
130 const MV ref_mv = av1_get_ref_mv(x, mi->ref_mv_idx).as_mv;
131 MV center_mv;
132 int dis;
133 int rv = 0;
134 int cost_list[5];
135 int search_subpel = 1;
136 const YV12_BUFFER_CONFIG *scaled_ref_frame =
137 av1_get_scaled_ref_frame(cpi, ref);
138
139 if (scaled_ref_frame) {
140 int i;
141 // Swap out the reference frame for a version that's been scaled to
142 // match the resolution of the current frame, allowing the existing
143 // motion search code to be used without additional modifications.
144 for (i = 0; i < MAX_MB_PLANE; i++) backup_yv12[i] = xd->plane[i].pre[0];
145 av1_setup_pre_planes(xd, 0, scaled_ref_frame, mi_row, mi_col, NULL,
146 num_planes);
147 }
148
149 start_mv = get_fullmv_from_mv(&ref_mv);
150
151 if (!use_base_mv)
152 center_mv = ref_mv;
153 else
154 center_mv = tmp_mv->as_mv;
155
156 const search_site_config *src_search_sites =
157 &cpi->mv_search_params.ss_cfg[SS_CFG_SRC];
158 FULLPEL_MOTION_SEARCH_PARAMS full_ms_params;
159 av1_make_default_fullpel_ms_params(&full_ms_params, cpi, x, bsize, ¢er_mv,
160 src_search_sites);
161
162 av1_full_pixel_search(start_mv, &full_ms_params, step_param,
163 cond_cost_list(cpi, cost_list), &tmp_mv->as_fullmv,
164 NULL);
165
166 // calculate the bit cost on motion vector
167 MV mvp_full = get_mv_from_fullmv(&tmp_mv->as_fullmv);
168
169 *rate_mv = av1_mv_bit_cost(&mvp_full, &ref_mv, x->nmv_vec_cost,
170 x->mv_cost_stack, MV_COST_WEIGHT);
171
172 // TODO(kyslov) Account for Rate Mode!
173 rv = !(RDCOST(x->rdmult, (*rate_mv), 0) > best_rd_sofar);
174
175 if (rv && search_subpel) {
176 SUBPEL_MOTION_SEARCH_PARAMS ms_params;
177 av1_make_default_subpel_ms_params(&ms_params, cpi, x, bsize, &ref_mv,
178 cost_list);
179 MV subpel_start_mv = get_mv_from_fullmv(&tmp_mv->as_fullmv);
180 cpi->mv_search_params.find_fractional_mv_step(
181 xd, cm, &ms_params, subpel_start_mv, &tmp_mv->as_mv, &dis,
182 &x->pred_sse[ref], NULL);
183
184 *rate_mv = av1_mv_bit_cost(&tmp_mv->as_mv, &ref_mv, x->nmv_vec_cost,
185 x->mv_cost_stack, MV_COST_WEIGHT);
186 }
187
188 if (scaled_ref_frame) {
189 int i;
190 for (i = 0; i < MAX_MB_PLANE; i++) xd->plane[i].pre[0] = backup_yv12[i];
191 }
192 return rv;
193 }
194
search_new_mv(AV1_COMP * cpi,MACROBLOCK * x,int_mv frame_mv[][REF_FRAMES],MV_REFERENCE_FRAME ref_frame,int gf_temporal_ref,BLOCK_SIZE bsize,int mi_row,int mi_col,int best_pred_sad,int * rate_mv,RD_STATS * best_rdc)195 static int search_new_mv(AV1_COMP *cpi, MACROBLOCK *x,
196 int_mv frame_mv[][REF_FRAMES],
197 MV_REFERENCE_FRAME ref_frame, int gf_temporal_ref,
198 BLOCK_SIZE bsize, int mi_row, int mi_col,
199 int best_pred_sad, int *rate_mv, RD_STATS *best_rdc) {
200 MACROBLOCKD *const xd = &x->e_mbd;
201 MB_MODE_INFO *const mi = xd->mi[0];
202 AV1_COMMON *cm = &cpi->common;
203 if (ref_frame > LAST_FRAME && gf_temporal_ref &&
204 cpi->oxcf.rc_mode == AOM_CBR) {
205 int tmp_sad;
206 int dis;
207 int cost_list[5] = { INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX };
208
209 if (bsize < BLOCK_16X16) return -1;
210
211 tmp_sad = av1_int_pro_motion_estimation(
212 cpi, x, bsize, mi_row, mi_col,
213 &x->mbmi_ext->ref_mv_stack[ref_frame][0].this_mv.as_mv);
214
215 if (tmp_sad > x->pred_mv_sad[LAST_FRAME]) return -1;
216 if (tmp_sad + (num_pels_log2_lookup[bsize] << 4) > best_pred_sad) return -1;
217
218 frame_mv[NEWMV][ref_frame].as_int = mi->mv[0].as_int;
219 int_mv best_mv = mi->mv[0];
220 best_mv.as_mv.row >>= 3;
221 best_mv.as_mv.col >>= 3;
222 MV ref_mv = av1_get_ref_mv(x, 0).as_mv;
223
224 *rate_mv =
225 av1_mv_bit_cost(&frame_mv[NEWMV][ref_frame].as_mv, &ref_mv,
226 x->nmv_vec_cost, x->mv_cost_stack, MV_COST_WEIGHT);
227 frame_mv[NEWMV][ref_frame].as_mv.row >>= 3;
228 frame_mv[NEWMV][ref_frame].as_mv.col >>= 3;
229
230 SUBPEL_MOTION_SEARCH_PARAMS ms_params;
231 av1_make_default_subpel_ms_params(&ms_params, cpi, x, bsize, &ref_mv,
232 cost_list);
233 MV start_mv = get_mv_from_fullmv(&best_mv.as_fullmv);
234 cpi->mv_search_params.find_fractional_mv_step(
235 xd, cm, &ms_params, start_mv, &best_mv.as_mv, &dis,
236 &x->pred_sse[ref_frame], NULL);
237 frame_mv[NEWMV][ref_frame].as_int = best_mv.as_int;
238 } else if (!combined_motion_search(cpi, x, bsize, mi_row, mi_col,
239 &frame_mv[NEWMV][ref_frame], rate_mv,
240 best_rdc->rdcost, 0)) {
241 return -1;
242 }
243
244 return 0;
245 }
246
find_predictors(AV1_COMP * cpi,MACROBLOCK * x,MV_REFERENCE_FRAME ref_frame,int_mv frame_mv[MB_MODE_COUNT][REF_FRAMES],int * ref_frame_skip_mask,const int flag_list[4],TileDataEnc * tile_data,struct buf_2d yv12_mb[8][MAX_MB_PLANE],BLOCK_SIZE bsize,int force_skip_low_temp_var)247 static INLINE void find_predictors(
248 AV1_COMP *cpi, MACROBLOCK *x, MV_REFERENCE_FRAME ref_frame,
249 int_mv frame_mv[MB_MODE_COUNT][REF_FRAMES], int *ref_frame_skip_mask,
250 const int flag_list[4], TileDataEnc *tile_data,
251 struct buf_2d yv12_mb[8][MAX_MB_PLANE], BLOCK_SIZE bsize,
252 int force_skip_low_temp_var) {
253 AV1_COMMON *const cm = &cpi->common;
254 MACROBLOCKD *const xd = &x->e_mbd;
255 MB_MODE_INFO *const mbmi = xd->mi[0];
256 MB_MODE_INFO_EXT *const mbmi_ext = x->mbmi_ext;
257 const YV12_BUFFER_CONFIG *yv12 = get_ref_frame_yv12_buf(cm, ref_frame);
258 const int num_planes = av1_num_planes(cm);
259 (void)tile_data;
260
261 x->pred_mv_sad[ref_frame] = INT_MAX;
262 frame_mv[NEWMV][ref_frame].as_int = INVALID_MV;
263 // TODO(kyslov) this needs various further optimizations. to be continued..
264 if ((cpi->ref_frame_flags & flag_list[ref_frame]) && (yv12 != NULL)) {
265 const struct scale_factors *const sf =
266 get_ref_scale_factors_const(cm, ref_frame);
267 av1_setup_pred_block(xd, yv12_mb[ref_frame], yv12, sf, sf, num_planes);
268 av1_find_mv_refs(cm, xd, mbmi, ref_frame, mbmi_ext->ref_mv_count,
269 xd->ref_mv_stack, xd->weight, NULL, mbmi_ext->global_mvs,
270 mbmi_ext->mode_context);
271 // TODO(Ravi): Populate mbmi_ext->ref_mv_stack[ref_frame][4] and
272 // mbmi_ext->weight[ref_frame][4] inside av1_find_mv_refs.
273 av1_copy_usable_ref_mv_stack_and_weight(xd, mbmi_ext, ref_frame);
274 av1_find_best_ref_mvs_from_stack(
275 cm->features.allow_high_precision_mv, mbmi_ext, ref_frame,
276 &frame_mv[NEARESTMV][ref_frame], &frame_mv[NEARMV][ref_frame], 0);
277 // Early exit for non-LAST frame if force_skip_low_temp_var is set.
278 if (!av1_is_scaled(sf) && bsize >= BLOCK_8X8 &&
279 !(force_skip_low_temp_var && ref_frame != LAST_FRAME)) {
280 av1_mv_pred(cpi, x, yv12_mb[ref_frame][0].buf, yv12->y_stride, ref_frame,
281 bsize);
282 }
283 } else {
284 *ref_frame_skip_mask |= (1 << ref_frame);
285 }
286 av1_count_overlappable_neighbors(cm, xd);
287 mbmi->num_proj_ref = 1;
288 }
289
estimate_single_ref_frame_costs(const AV1_COMMON * cm,const MACROBLOCKD * xd,const MACROBLOCK * x,int segment_id,unsigned int * ref_costs_single)290 static void estimate_single_ref_frame_costs(const AV1_COMMON *cm,
291 const MACROBLOCKD *xd,
292 const MACROBLOCK *x, int segment_id,
293 unsigned int *ref_costs_single) {
294 int seg_ref_active =
295 segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME);
296 if (seg_ref_active) {
297 memset(ref_costs_single, 0, REF_FRAMES * sizeof(*ref_costs_single));
298 } else {
299 int intra_inter_ctx = av1_get_intra_inter_context(xd);
300 ref_costs_single[INTRA_FRAME] = x->intra_inter_cost[intra_inter_ctx][0];
301 unsigned int base_cost = x->intra_inter_cost[intra_inter_ctx][1];
302
303 for (int i = LAST_FRAME; i <= ALTREF_FRAME; ++i)
304 ref_costs_single[i] = base_cost;
305
306 const int ctx_p1 = av1_get_pred_context_single_ref_p1(xd);
307 const int ctx_p2 = av1_get_pred_context_single_ref_p2(xd);
308 const int ctx_p3 = av1_get_pred_context_single_ref_p3(xd);
309 const int ctx_p4 = av1_get_pred_context_single_ref_p4(xd);
310 const int ctx_p5 = av1_get_pred_context_single_ref_p5(xd);
311 const int ctx_p6 = av1_get_pred_context_single_ref_p6(xd);
312
313 // Determine cost of a single ref frame, where frame types are represented
314 // by a tree:
315 // Level 0: add cost whether this ref is a forward or backward ref
316 ref_costs_single[LAST_FRAME] += x->single_ref_cost[ctx_p1][0][0];
317 ref_costs_single[LAST2_FRAME] += x->single_ref_cost[ctx_p1][0][0];
318 ref_costs_single[LAST3_FRAME] += x->single_ref_cost[ctx_p1][0][0];
319 ref_costs_single[GOLDEN_FRAME] += x->single_ref_cost[ctx_p1][0][0];
320 ref_costs_single[BWDREF_FRAME] += x->single_ref_cost[ctx_p1][0][1];
321 ref_costs_single[ALTREF2_FRAME] += x->single_ref_cost[ctx_p1][0][1];
322 ref_costs_single[ALTREF_FRAME] += x->single_ref_cost[ctx_p1][0][1];
323
324 // Level 1: if this ref is forward ref,
325 // add cost whether it is last/last2 or last3/golden
326 ref_costs_single[LAST_FRAME] += x->single_ref_cost[ctx_p3][2][0];
327 ref_costs_single[LAST2_FRAME] += x->single_ref_cost[ctx_p3][2][0];
328 ref_costs_single[LAST3_FRAME] += x->single_ref_cost[ctx_p3][2][1];
329 ref_costs_single[GOLDEN_FRAME] += x->single_ref_cost[ctx_p3][2][1];
330
331 // Level 1: if this ref is backward ref
332 // then add cost whether this ref is altref or backward ref
333 ref_costs_single[BWDREF_FRAME] += x->single_ref_cost[ctx_p2][1][0];
334 ref_costs_single[ALTREF2_FRAME] += x->single_ref_cost[ctx_p2][1][0];
335 ref_costs_single[ALTREF_FRAME] += x->single_ref_cost[ctx_p2][1][1];
336
337 // Level 2: further add cost whether this ref is last or last2
338 ref_costs_single[LAST_FRAME] += x->single_ref_cost[ctx_p4][3][0];
339 ref_costs_single[LAST2_FRAME] += x->single_ref_cost[ctx_p4][3][1];
340
341 // Level 2: last3 or golden
342 ref_costs_single[LAST3_FRAME] += x->single_ref_cost[ctx_p5][4][0];
343 ref_costs_single[GOLDEN_FRAME] += x->single_ref_cost[ctx_p5][4][1];
344
345 // Level 2: bwdref or altref2
346 ref_costs_single[BWDREF_FRAME] += x->single_ref_cost[ctx_p6][5][0];
347 ref_costs_single[ALTREF2_FRAME] += x->single_ref_cost[ctx_p6][5][1];
348 }
349 }
350
estimate_comp_ref_frame_costs(const AV1_COMMON * cm,const MACROBLOCKD * xd,const MACROBLOCK * x,int segment_id,unsigned int (* ref_costs_comp)[REF_FRAMES])351 static void estimate_comp_ref_frame_costs(
352 const AV1_COMMON *cm, const MACROBLOCKD *xd, const MACROBLOCK *x,
353 int segment_id, unsigned int (*ref_costs_comp)[REF_FRAMES]) {
354 if (segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME)) {
355 for (int ref_frame = 0; ref_frame < REF_FRAMES; ++ref_frame)
356 memset(ref_costs_comp[ref_frame], 0,
357 REF_FRAMES * sizeof((*ref_costs_comp)[0]));
358 } else {
359 int intra_inter_ctx = av1_get_intra_inter_context(xd);
360 unsigned int base_cost = x->intra_inter_cost[intra_inter_ctx][1];
361
362 if (cm->current_frame.reference_mode != SINGLE_REFERENCE) {
363 // Similar to single ref, determine cost of compound ref frames.
364 // cost_compound_refs = cost_first_ref + cost_second_ref
365 const int bwdref_comp_ctx_p = av1_get_pred_context_comp_bwdref_p(xd);
366 const int bwdref_comp_ctx_p1 = av1_get_pred_context_comp_bwdref_p1(xd);
367 const int ref_comp_ctx_p = av1_get_pred_context_comp_ref_p(xd);
368 const int ref_comp_ctx_p1 = av1_get_pred_context_comp_ref_p1(xd);
369 const int ref_comp_ctx_p2 = av1_get_pred_context_comp_ref_p2(xd);
370
371 const int comp_ref_type_ctx = av1_get_comp_reference_type_context(xd);
372 unsigned int ref_bicomp_costs[REF_FRAMES] = { 0 };
373
374 ref_bicomp_costs[LAST_FRAME] = ref_bicomp_costs[LAST2_FRAME] =
375 ref_bicomp_costs[LAST3_FRAME] = ref_bicomp_costs[GOLDEN_FRAME] =
376 base_cost + x->comp_ref_type_cost[comp_ref_type_ctx][1];
377 ref_bicomp_costs[BWDREF_FRAME] = ref_bicomp_costs[ALTREF2_FRAME] = 0;
378 ref_bicomp_costs[ALTREF_FRAME] = 0;
379
380 // cost of first ref frame
381 ref_bicomp_costs[LAST_FRAME] += x->comp_ref_cost[ref_comp_ctx_p][0][0];
382 ref_bicomp_costs[LAST2_FRAME] += x->comp_ref_cost[ref_comp_ctx_p][0][0];
383 ref_bicomp_costs[LAST3_FRAME] += x->comp_ref_cost[ref_comp_ctx_p][0][1];
384 ref_bicomp_costs[GOLDEN_FRAME] += x->comp_ref_cost[ref_comp_ctx_p][0][1];
385
386 ref_bicomp_costs[LAST_FRAME] += x->comp_ref_cost[ref_comp_ctx_p1][1][0];
387 ref_bicomp_costs[LAST2_FRAME] += x->comp_ref_cost[ref_comp_ctx_p1][1][1];
388
389 ref_bicomp_costs[LAST3_FRAME] += x->comp_ref_cost[ref_comp_ctx_p2][2][0];
390 ref_bicomp_costs[GOLDEN_FRAME] += x->comp_ref_cost[ref_comp_ctx_p2][2][1];
391
392 // cost of second ref frame
393 ref_bicomp_costs[BWDREF_FRAME] +=
394 x->comp_bwdref_cost[bwdref_comp_ctx_p][0][0];
395 ref_bicomp_costs[ALTREF2_FRAME] +=
396 x->comp_bwdref_cost[bwdref_comp_ctx_p][0][0];
397 ref_bicomp_costs[ALTREF_FRAME] +=
398 x->comp_bwdref_cost[bwdref_comp_ctx_p][0][1];
399
400 ref_bicomp_costs[BWDREF_FRAME] +=
401 x->comp_bwdref_cost[bwdref_comp_ctx_p1][1][0];
402 ref_bicomp_costs[ALTREF2_FRAME] +=
403 x->comp_bwdref_cost[bwdref_comp_ctx_p1][1][1];
404
405 // cost: if one ref frame is forward ref, the other ref is backward ref
406 for (int ref0 = LAST_FRAME; ref0 <= GOLDEN_FRAME; ++ref0) {
407 for (int ref1 = BWDREF_FRAME; ref1 <= ALTREF_FRAME; ++ref1) {
408 ref_costs_comp[ref0][ref1] =
409 ref_bicomp_costs[ref0] + ref_bicomp_costs[ref1];
410 }
411 }
412
413 // cost: if both ref frames are the same side.
414 const int uni_comp_ref_ctx_p = av1_get_pred_context_uni_comp_ref_p(xd);
415 const int uni_comp_ref_ctx_p1 = av1_get_pred_context_uni_comp_ref_p1(xd);
416 const int uni_comp_ref_ctx_p2 = av1_get_pred_context_uni_comp_ref_p2(xd);
417 ref_costs_comp[LAST_FRAME][LAST2_FRAME] =
418 base_cost + x->comp_ref_type_cost[comp_ref_type_ctx][0] +
419 x->uni_comp_ref_cost[uni_comp_ref_ctx_p][0][0] +
420 x->uni_comp_ref_cost[uni_comp_ref_ctx_p1][1][0];
421 ref_costs_comp[LAST_FRAME][LAST3_FRAME] =
422 base_cost + x->comp_ref_type_cost[comp_ref_type_ctx][0] +
423 x->uni_comp_ref_cost[uni_comp_ref_ctx_p][0][0] +
424 x->uni_comp_ref_cost[uni_comp_ref_ctx_p1][1][1] +
425 x->uni_comp_ref_cost[uni_comp_ref_ctx_p2][2][0];
426 ref_costs_comp[LAST_FRAME][GOLDEN_FRAME] =
427 base_cost + x->comp_ref_type_cost[comp_ref_type_ctx][0] +
428 x->uni_comp_ref_cost[uni_comp_ref_ctx_p][0][0] +
429 x->uni_comp_ref_cost[uni_comp_ref_ctx_p1][1][1] +
430 x->uni_comp_ref_cost[uni_comp_ref_ctx_p2][2][1];
431 ref_costs_comp[BWDREF_FRAME][ALTREF_FRAME] =
432 base_cost + x->comp_ref_type_cost[comp_ref_type_ctx][0] +
433 x->uni_comp_ref_cost[uni_comp_ref_ctx_p][0][1];
434 } else {
435 for (int ref0 = LAST_FRAME; ref0 <= GOLDEN_FRAME; ++ref0) {
436 for (int ref1 = BWDREF_FRAME; ref1 <= ALTREF_FRAME; ++ref1)
437 ref_costs_comp[ref0][ref1] = 512;
438 }
439 ref_costs_comp[LAST_FRAME][LAST2_FRAME] = 512;
440 ref_costs_comp[LAST_FRAME][LAST3_FRAME] = 512;
441 ref_costs_comp[LAST_FRAME][GOLDEN_FRAME] = 512;
442 ref_costs_comp[BWDREF_FRAME][ALTREF_FRAME] = 512;
443 }
444 }
445 }
446
calculate_tx_size(const AV1_COMP * const cpi,BLOCK_SIZE bsize,MACROBLOCK * const x,unsigned int var,unsigned int sse)447 static TX_SIZE calculate_tx_size(const AV1_COMP *const cpi, BLOCK_SIZE bsize,
448 MACROBLOCK *const x, unsigned int var,
449 unsigned int sse) {
450 MACROBLOCKD *const xd = &x->e_mbd;
451 TX_SIZE tx_size;
452 if (x->tx_mode_search_type == TX_MODE_SELECT) {
453 if (sse > (var << 2))
454 tx_size = AOMMIN(max_txsize_lookup[bsize],
455 tx_mode_to_biggest_tx_size[x->tx_mode_search_type]);
456 else
457 tx_size = TX_8X8;
458
459 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ &&
460 cyclic_refresh_segment_id_boosted(xd->mi[0]->segment_id))
461 tx_size = TX_8X8;
462 else if (tx_size > TX_16X16)
463 tx_size = TX_16X16;
464 } else {
465 tx_size = AOMMIN(max_txsize_lookup[bsize],
466 tx_mode_to_biggest_tx_size[x->tx_mode_search_type]);
467 }
468
469 if (x->tx_mode_search_type != ONLY_4X4 && bsize > BLOCK_32X32)
470 tx_size = TX_16X16;
471
472 return AOMMIN(tx_size, TX_16X16);
473 }
474
475 static const uint8_t b_width_log2_lookup[BLOCK_SIZES] = { 0, 0, 1, 1, 1, 2,
476 2, 2, 3, 3, 3, 4,
477 4, 4, 5, 5 };
478 static const uint8_t b_height_log2_lookup[BLOCK_SIZES] = { 0, 1, 0, 1, 2, 1,
479 2, 3, 2, 3, 4, 3,
480 4, 5, 4, 5 };
481
block_variance(const uint8_t * src,int src_stride,const uint8_t * ref,int ref_stride,int w,int h,unsigned int * sse,int * sum,int block_size,uint32_t * sse8x8,int * sum8x8,uint32_t * var8x8)482 static void block_variance(const uint8_t *src, int src_stride,
483 const uint8_t *ref, int ref_stride, int w, int h,
484 unsigned int *sse, int *sum, int block_size,
485 uint32_t *sse8x8, int *sum8x8, uint32_t *var8x8) {
486 int i, j, k = 0;
487
488 *sse = 0;
489 *sum = 0;
490
491 for (i = 0; i < h; i += block_size) {
492 for (j = 0; j < w; j += block_size) {
493 aom_get8x8var(src + src_stride * i + j, src_stride,
494 ref + ref_stride * i + j, ref_stride, &sse8x8[k],
495 &sum8x8[k]);
496 *sse += sse8x8[k];
497 *sum += sum8x8[k];
498 var8x8[k] = sse8x8[k] - (uint32_t)(((int64_t)sum8x8[k] * sum8x8[k]) >> 6);
499 k++;
500 }
501 }
502 }
503
calculate_variance(int bw,int bh,TX_SIZE tx_size,unsigned int * sse_i,int * sum_i,unsigned int * var_o,unsigned int * sse_o,int * sum_o)504 static void calculate_variance(int bw, int bh, TX_SIZE tx_size,
505 unsigned int *sse_i, int *sum_i,
506 unsigned int *var_o, unsigned int *sse_o,
507 int *sum_o) {
508 const BLOCK_SIZE unit_size = txsize_to_bsize[tx_size];
509 const int nw = 1 << (bw - b_width_log2_lookup[unit_size]);
510 const int nh = 1 << (bh - b_height_log2_lookup[unit_size]);
511 int i, j, k = 0;
512
513 for (i = 0; i < nh; i += 2) {
514 for (j = 0; j < nw; j += 2) {
515 sse_o[k] = sse_i[i * nw + j] + sse_i[i * nw + j + 1] +
516 sse_i[(i + 1) * nw + j] + sse_i[(i + 1) * nw + j + 1];
517 sum_o[k] = sum_i[i * nw + j] + sum_i[i * nw + j + 1] +
518 sum_i[(i + 1) * nw + j] + sum_i[(i + 1) * nw + j + 1];
519 var_o[k] = sse_o[k] - (uint32_t)(((int64_t)sum_o[k] * sum_o[k]) >>
520 (b_width_log2_lookup[unit_size] +
521 b_height_log2_lookup[unit_size] + 6));
522 k++;
523 }
524 }
525 }
526
527 // Adjust the ac_thr according to speed, width, height and normalized sum
ac_thr_factor(const int speed,const int width,const int height,const int norm_sum)528 static int ac_thr_factor(const int speed, const int width, const int height,
529 const int norm_sum) {
530 if (speed >= 8 && norm_sum < 5) {
531 if (width <= 640 && height <= 480)
532 return 4;
533 else
534 return 2;
535 }
536 return 1;
537 }
538
model_skip_for_sb_y_large(AV1_COMP * cpi,BLOCK_SIZE bsize,int mi_row,int mi_col,MACROBLOCK * x,MACROBLOCKD * xd,int * out_rate,int64_t * out_dist,unsigned int * var_y,unsigned int * sse_y,int * early_term,int calculate_rd)539 static void model_skip_for_sb_y_large(AV1_COMP *cpi, BLOCK_SIZE bsize,
540 int mi_row, int mi_col, MACROBLOCK *x,
541 MACROBLOCKD *xd, int *out_rate,
542 int64_t *out_dist, unsigned int *var_y,
543 unsigned int *sse_y, int *early_term,
544 int calculate_rd) {
545 // Note our transform coeffs are 8 times an orthogonal transform.
546 // Hence quantizer step is also 8 times. To get effective quantizer
547 // we need to divide by 8 before sending to modeling function.
548 unsigned int sse;
549 struct macroblock_plane *const p = &x->plane[0];
550 struct macroblockd_plane *const pd = &xd->plane[0];
551 const uint32_t dc_quant = p->dequant_QTX[0];
552 const uint32_t ac_quant = p->dequant_QTX[1];
553 const int64_t dc_thr = dc_quant * dc_quant >> 6;
554 int64_t ac_thr = ac_quant * ac_quant >> 6;
555 unsigned int var;
556 int sum;
557
558 const int bw = b_width_log2_lookup[bsize];
559 const int bh = b_height_log2_lookup[bsize];
560 const int num8x8 = 1 << (bw + bh - 2);
561 unsigned int sse8x8[256] = { 0 };
562 int sum8x8[256] = { 0 };
563 unsigned int var8x8[256] = { 0 };
564 TX_SIZE tx_size;
565 int k;
566 // Calculate variance for whole partition, and also save 8x8 blocks' variance
567 // to be used in following transform skipping test.
568 block_variance(p->src.buf, p->src.stride, pd->dst.buf, pd->dst.stride,
569 4 << bw, 4 << bh, &sse, &sum, 8, sse8x8, sum8x8, var8x8);
570 var = sse - (unsigned int)(((int64_t)sum * sum) >> (bw + bh + 4));
571
572 *var_y = var;
573 *sse_y = sse;
574
575 ac_thr *= ac_thr_factor(cpi->oxcf.speed, cpi->common.width,
576 cpi->common.height, abs(sum) >> (bw + bh));
577
578 tx_size = calculate_tx_size(cpi, bsize, x, var, sse);
579 // The code below for setting skip flag assumes tranform size of at least 8x8,
580 // so force this lower limit on transform.
581 if (tx_size < TX_8X8) tx_size = TX_8X8;
582 xd->mi[0]->tx_size = tx_size;
583
584 // Evaluate if the partition block is a skippable block in Y plane.
585 {
586 unsigned int sse16x16[64] = { 0 };
587 int sum16x16[64] = { 0 };
588 unsigned int var16x16[64] = { 0 };
589 const int num16x16 = num8x8 >> 2;
590
591 unsigned int sse32x32[16] = { 0 };
592 int sum32x32[16] = { 0 };
593 unsigned int var32x32[16] = { 0 };
594 const int num32x32 = num8x8 >> 4;
595
596 int ac_test = 1;
597 int dc_test = 1;
598 const int num = (tx_size == TX_8X8)
599 ? num8x8
600 : ((tx_size == TX_16X16) ? num16x16 : num32x32);
601 const unsigned int *sse_tx =
602 (tx_size == TX_8X8) ? sse8x8
603 : ((tx_size == TX_16X16) ? sse16x16 : sse32x32);
604 const unsigned int *var_tx =
605 (tx_size == TX_8X8) ? var8x8
606 : ((tx_size == TX_16X16) ? var16x16 : var32x32);
607
608 // Calculate variance if tx_size > TX_8X8
609 if (tx_size >= TX_16X16)
610 calculate_variance(bw, bh, TX_8X8, sse8x8, sum8x8, var16x16, sse16x16,
611 sum16x16);
612 if (tx_size == TX_32X32)
613 calculate_variance(bw, bh, TX_16X16, sse16x16, sum16x16, var32x32,
614 sse32x32, sum32x32);
615
616 // Skipping test
617 *early_term = 0;
618 for (k = 0; k < num; k++)
619 // Check if all ac coefficients can be quantized to zero.
620 if (!(var_tx[k] < ac_thr || var == 0)) {
621 ac_test = 0;
622 break;
623 }
624
625 for (k = 0; k < num; k++)
626 // Check if dc coefficient can be quantized to zero.
627 if (!(sse_tx[k] - var_tx[k] < dc_thr || sse == var)) {
628 dc_test = 0;
629 break;
630 }
631
632 if (ac_test && dc_test) {
633 int skip_uv[2] = { 0 };
634 unsigned int var_uv[2];
635 unsigned int sse_uv[2];
636 AV1_COMMON *const cm = &cpi->common;
637 // Transform skipping test in UV planes.
638 for (int i = 1; i <= 2; i++) {
639 int j = i - 1;
640 skip_uv[j] = 1;
641 if (x->color_sensitivity[j]) {
642 skip_uv[j] = 0;
643 struct macroblock_plane *const puv = &x->plane[i];
644 struct macroblockd_plane *const puvd = &xd->plane[i];
645 const BLOCK_SIZE uv_bsize = get_plane_block_size(
646 bsize, puvd->subsampling_x, puvd->subsampling_y);
647 // Adjust these thresholds for UV.
648 const int64_t uv_dc_thr =
649 (puv->dequant_QTX[0] * puv->dequant_QTX[0]) >> 3;
650 const int64_t uv_ac_thr =
651 (puv->dequant_QTX[1] * puv->dequant_QTX[1]) >> 3;
652 av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize, i,
653 i);
654 var_uv[j] = cpi->fn_ptr[uv_bsize].vf(puv->src.buf, puv->src.stride,
655 puvd->dst.buf, puvd->dst.stride,
656 &sse_uv[j]);
657 if ((var_uv[j] < uv_ac_thr || var_uv[j] == 0) &&
658 (sse_uv[j] - var_uv[j] < uv_dc_thr || sse_uv[j] == var_uv[j]))
659 skip_uv[j] = 1;
660 else
661 break;
662 }
663 }
664 if (skip_uv[0] & skip_uv[1]) {
665 *early_term = 1;
666 }
667 }
668 }
669 if (calculate_rd && out_dist != NULL && out_rate != NULL) {
670 if (!*early_term) {
671 const int bwide = block_size_wide[bsize];
672 const int bhigh = block_size_high[bsize];
673
674 model_rd_with_curvfit(cpi, x, bsize, AOM_PLANE_Y, sse, bwide * bhigh,
675 out_rate, out_dist);
676 }
677
678 if (*early_term) {
679 *out_rate = 0;
680 *out_dist = sse << 4;
681 }
682 }
683 }
684
model_rd_for_sb_y(const AV1_COMP * const cpi,BLOCK_SIZE bsize,MACROBLOCK * x,MACROBLOCKD * xd,int * out_rate_sum,int64_t * out_dist_sum,int * skip_txfm_sb,int64_t * skip_sse_sb,unsigned int * var_y,unsigned int * sse_y,int calculate_rd)685 static void model_rd_for_sb_y(const AV1_COMP *const cpi, BLOCK_SIZE bsize,
686 MACROBLOCK *x, MACROBLOCKD *xd, int *out_rate_sum,
687 int64_t *out_dist_sum, int *skip_txfm_sb,
688 int64_t *skip_sse_sb, unsigned int *var_y,
689 unsigned int *sse_y, int calculate_rd) {
690 // Note our transform coeffs are 8 times an orthogonal transform.
691 // Hence quantizer step is also 8 times. To get effective quantizer
692 // we need to divide by 8 before sending to modeling function.
693 const int ref = xd->mi[0]->ref_frame[0];
694
695 assert(bsize < BLOCK_SIZES_ALL);
696
697 struct macroblock_plane *const p = &x->plane[0];
698 struct macroblockd_plane *const pd = &xd->plane[0];
699 unsigned int sse;
700 int rate;
701 int64_t dist;
702
703 unsigned int var = cpi->fn_ptr[bsize].vf(p->src.buf, p->src.stride,
704 pd->dst.buf, pd->dst.stride, &sse);
705 xd->mi[0]->tx_size = calculate_tx_size(cpi, bsize, x, var, sse);
706
707 if (calculate_rd) {
708 const int bwide = block_size_wide[bsize];
709 const int bhigh = block_size_high[bsize];
710 model_rd_with_curvfit(cpi, x, bsize, AOM_PLANE_Y, sse, bwide * bhigh, &rate,
711 &dist);
712 } else {
713 rate = INT_MAX; // this will be overwritten later with block_yrd
714 dist = INT_MAX;
715 }
716 *var_y = var;
717 *sse_y = sse;
718 x->pred_sse[ref] = (unsigned int)AOMMIN(sse, UINT_MAX);
719
720 assert(rate >= 0);
721
722 if (skip_txfm_sb) *skip_txfm_sb = rate == 0;
723 if (skip_sse_sb) *skip_sse_sb = sse << 4;
724 rate = AOMMIN(rate, INT_MAX);
725 *out_rate_sum = (int)rate;
726 *out_dist_sum = dist;
727 }
728
block_yrd(AV1_COMP * cpi,MACROBLOCK * x,int mi_row,int mi_col,RD_STATS * this_rdc,int * skippable,int64_t * sse,BLOCK_SIZE bsize,TX_SIZE tx_size)729 static void block_yrd(AV1_COMP *cpi, MACROBLOCK *x, int mi_row, int mi_col,
730 RD_STATS *this_rdc, int *skippable, int64_t *sse,
731 BLOCK_SIZE bsize, TX_SIZE tx_size) {
732 MACROBLOCKD *xd = &x->e_mbd;
733 const struct macroblockd_plane *pd = &xd->plane[0];
734 struct macroblock_plane *const p = &x->plane[0];
735 const int num_4x4_w = mi_size_wide[bsize];
736 const int num_4x4_h = mi_size_high[bsize];
737 const int step = 1 << (tx_size << 1);
738 const int block_step = (1 << tx_size);
739 int block = 0;
740 const int max_blocks_wide =
741 num_4x4_w + (xd->mb_to_right_edge >= 0 ? 0 : xd->mb_to_right_edge >> 5);
742 const int max_blocks_high =
743 num_4x4_h + (xd->mb_to_bottom_edge >= 0 ? 0 : xd->mb_to_bottom_edge >> 5);
744 int eob_cost = 0;
745 const int bw = 4 * num_4x4_w;
746 const int bh = 4 * num_4x4_h;
747
748 (void)mi_row;
749 (void)mi_col;
750 (void)cpi;
751
752 #if CONFIG_AV1_HIGHBITDEPTH
753 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
754 aom_highbd_subtract_block(bh, bw, p->src_diff, bw, p->src.buf,
755 p->src.stride, pd->dst.buf, pd->dst.stride,
756 x->e_mbd.bd);
757 } else {
758 aom_subtract_block(bh, bw, p->src_diff, bw, p->src.buf, p->src.stride,
759 pd->dst.buf, pd->dst.stride);
760 }
761 #else
762 aom_subtract_block(bh, bw, p->src_diff, bw, p->src.buf, p->src.stride,
763 pd->dst.buf, pd->dst.stride);
764 #endif
765
766 *skippable = 1;
767 // Keep track of the row and column of the blocks we use so that we know
768 // if we are in the unrestricted motion border.
769 for (int r = 0; r < max_blocks_high; r += block_step) {
770 for (int c = 0; c < num_4x4_w; c += block_step) {
771 if (c < max_blocks_wide) {
772 const SCAN_ORDER *const scan_order = &av1_default_scan_orders[tx_size];
773 const int block_offset = BLOCK_OFFSET(block);
774 #if CONFIG_AV1_HIGHBITDEPTH
775 tran_low_t *const coeff = p->coeff + block_offset;
776 tran_low_t *const qcoeff = p->qcoeff + block_offset;
777 tran_low_t *const dqcoeff = pd->dqcoeff + block_offset;
778 #else
779 int16_t *const low_coeff = (int16_t *)p->coeff + block_offset;
780 int16_t *const low_qcoeff = (int16_t *)p->qcoeff + block_offset;
781 int16_t *const low_dqcoeff = (int16_t *)pd->dqcoeff + block_offset;
782 #endif
783 uint16_t *const eob = &p->eobs[block];
784 const int diff_stride = bw;
785 const int16_t *src_diff;
786 src_diff = &p->src_diff[(r * diff_stride + c) << 2];
787
788 switch (tx_size) {
789 case TX_64X64:
790 assert(0); // Not implemented
791 break;
792 case TX_32X32:
793 assert(0); // Not used
794 break;
795 #if CONFIG_AV1_HIGHBITDEPTH
796 case TX_16X16:
797 aom_hadamard_16x16(src_diff, diff_stride, coeff);
798 av1_quantize_fp(coeff, 16 * 16, p->zbin_QTX, p->round_fp_QTX,
799 p->quant_fp_QTX, p->quant_shift_QTX, qcoeff,
800 dqcoeff, p->dequant_QTX, eob, scan_order->scan,
801 scan_order->iscan);
802 break;
803 case TX_8X8:
804 aom_hadamard_8x8(src_diff, diff_stride, coeff);
805 av1_quantize_fp(coeff, 8 * 8, p->zbin_QTX, p->round_fp_QTX,
806 p->quant_fp_QTX, p->quant_shift_QTX, qcoeff,
807 dqcoeff, p->dequant_QTX, eob, scan_order->scan,
808 scan_order->iscan);
809 break;
810 #else
811 case TX_16X16:
812 aom_hadamard_lp_16x16(src_diff, diff_stride, low_coeff);
813 av1_quantize_lp(low_coeff, 16 * 16, p->round_fp_QTX,
814 p->quant_fp_QTX, low_qcoeff, low_dqcoeff,
815 p->dequant_QTX, eob, scan_order->scan);
816 break;
817 case TX_8X8:
818 aom_hadamard_lp_8x8(src_diff, diff_stride, low_coeff);
819 av1_quantize_lp(low_coeff, 8 * 8, p->round_fp_QTX, p->quant_fp_QTX,
820 low_qcoeff, low_dqcoeff, p->dequant_QTX, eob,
821 scan_order->scan);
822 break;
823 default:
824 assert(tx_size == TX_4X4);
825 x->fwd_txfm4x4(src_diff, low_coeff, diff_stride);
826 av1_quantize_lp(low_coeff, 4 * 4, p->round_fp_QTX, p->quant_fp_QTX,
827 low_qcoeff, low_dqcoeff, p->dequant_QTX, eob,
828 scan_order->scan);
829 break;
830 #endif
831 }
832 *skippable &= (*eob == 0);
833 eob_cost += 1;
834 }
835 block += step;
836 }
837 }
838 this_rdc->skip = *skippable;
839 this_rdc->rate = 0;
840 if (*sse < INT64_MAX) {
841 *sse = (*sse << 6) >> 2;
842 if (*skippable) {
843 this_rdc->dist = *sse;
844 return;
845 }
846 }
847
848 block = 0;
849 this_rdc->dist = 0;
850 for (int r = 0; r < max_blocks_high; r += block_step) {
851 for (int c = 0; c < num_4x4_w; c += block_step) {
852 if (c < max_blocks_wide) {
853 const int block_offset = BLOCK_OFFSET(block);
854 uint16_t *const eob = &p->eobs[block];
855 #if CONFIG_AV1_HIGHBITDEPTH
856 int64_t dummy;
857 tran_low_t *const coeff = p->coeff + block_offset;
858 tran_low_t *const qcoeff = p->qcoeff + block_offset;
859 tran_low_t *const dqcoeff = pd->dqcoeff + block_offset;
860
861 if (*eob == 1)
862 this_rdc->rate += (int)abs(qcoeff[0]);
863 else if (*eob > 1)
864 this_rdc->rate += aom_satd(qcoeff, step << 4);
865
866 this_rdc->dist +=
867 av1_block_error(coeff, dqcoeff, step << 4, &dummy) >> 2;
868 #else
869 int16_t *const low_coeff = (int16_t *)p->coeff + block_offset;
870 int16_t *const low_qcoeff = (int16_t *)p->qcoeff + block_offset;
871 int16_t *const low_dqcoeff = (int16_t *)pd->dqcoeff + block_offset;
872
873 if (*eob == 1)
874 this_rdc->rate += (int)abs(low_qcoeff[0]);
875 else if (*eob > 1)
876 this_rdc->rate += aom_satd_lp(low_qcoeff, step << 4);
877
878 this_rdc->dist +=
879 av1_block_error_lp(low_coeff, low_dqcoeff, step << 4) >> 2;
880 #endif
881 }
882 block += step;
883 }
884 }
885
886 // If skippable is set, rate gets clobbered later.
887 this_rdc->rate <<= (2 + AV1_PROB_COST_SHIFT);
888 this_rdc->rate += (eob_cost << AV1_PROB_COST_SHIFT);
889 }
890
init_mbmi(MB_MODE_INFO * mbmi,PREDICTION_MODE pred_mode,MV_REFERENCE_FRAME ref_frame0,MV_REFERENCE_FRAME ref_frame1,const AV1_COMMON * cm)891 static INLINE void init_mbmi(MB_MODE_INFO *mbmi, PREDICTION_MODE pred_mode,
892 MV_REFERENCE_FRAME ref_frame0,
893 MV_REFERENCE_FRAME ref_frame1,
894 const AV1_COMMON *cm) {
895 PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info;
896 mbmi->ref_mv_idx = 0;
897 mbmi->mode = pred_mode;
898 mbmi->uv_mode = UV_DC_PRED;
899 mbmi->ref_frame[0] = ref_frame0;
900 mbmi->ref_frame[1] = ref_frame1;
901 pmi->palette_size[0] = 0;
902 pmi->palette_size[1] = 0;
903 mbmi->filter_intra_mode_info.use_filter_intra = 0;
904 mbmi->mv[0].as_int = mbmi->mv[1].as_int = 0;
905 mbmi->motion_mode = SIMPLE_TRANSLATION;
906 mbmi->num_proj_ref = 1;
907 mbmi->interintra_mode = 0;
908 set_default_interp_filters(mbmi, cm->features.interp_filter);
909 }
910
911 #if CONFIG_INTERNAL_STATS
store_coding_context(MACROBLOCK * x,PICK_MODE_CONTEXT * ctx,int mode_index)912 static void store_coding_context(MACROBLOCK *x, PICK_MODE_CONTEXT *ctx,
913 int mode_index) {
914 #else
915 static void store_coding_context(MACROBLOCK *x, PICK_MODE_CONTEXT *ctx) {
916 #endif // CONFIG_INTERNAL_STATS
917 MACROBLOCKD *const xd = &x->e_mbd;
918
919 // Take a snapshot of the coding context so it can be
920 // restored if we decide to encode this way
921 ctx->rd_stats.skip = x->force_skip;
922 memset(ctx->blk_skip, 0, sizeof(ctx->blk_skip[0]) * ctx->num_4x4_blk);
923 memset(ctx->tx_type_map, DCT_DCT,
924 sizeof(ctx->tx_type_map[0]) * ctx->num_4x4_blk);
925 ctx->skippable = x->force_skip;
926 #if CONFIG_INTERNAL_STATS
927 ctx->best_mode_index = mode_index;
928 #endif // CONFIG_INTERNAL_STATS
929 ctx->mic = *xd->mi[0];
930 ctx->skippable = x->force_skip;
931 av1_copy_mbmi_ext_to_mbmi_ext_frame(&ctx->mbmi_ext_best, x->mbmi_ext,
932 av1_ref_frame_type(xd->mi[0]->ref_frame));
933 ctx->comp_pred_diff = 0;
934 ctx->hybrid_pred_diff = 0;
935 ctx->single_pred_diff = 0;
936 }
937
938 static int get_pred_buffer(PRED_BUFFER *p, int len) {
939 for (int i = 0; i < len; i++) {
940 if (!p[i].in_use) {
941 p[i].in_use = 1;
942 return i;
943 }
944 }
945 return -1;
946 }
947
948 static void free_pred_buffer(PRED_BUFFER *p) {
949 if (p != NULL) p->in_use = 0;
950 }
951
952 static int cost_mv_ref(const MACROBLOCK *const x, PREDICTION_MODE mode,
953 int16_t mode_context) {
954 if (is_inter_compound_mode(mode)) {
955 return x
956 ->inter_compound_mode_cost[mode_context][INTER_COMPOUND_OFFSET(mode)];
957 }
958
959 int mode_cost = 0;
960 int16_t mode_ctx = mode_context & NEWMV_CTX_MASK;
961
962 assert(is_inter_mode(mode));
963
964 if (mode == NEWMV) {
965 mode_cost = x->newmv_mode_cost[mode_ctx][0];
966 return mode_cost;
967 } else {
968 mode_cost = x->newmv_mode_cost[mode_ctx][1];
969 mode_ctx = (mode_context >> GLOBALMV_OFFSET) & GLOBALMV_CTX_MASK;
970
971 if (mode == GLOBALMV) {
972 mode_cost += x->zeromv_mode_cost[mode_ctx][0];
973 return mode_cost;
974 } else {
975 mode_cost += x->zeromv_mode_cost[mode_ctx][1];
976 mode_ctx = (mode_context >> REFMV_OFFSET) & REFMV_CTX_MASK;
977 mode_cost += x->refmv_mode_cost[mode_ctx][mode != NEARESTMV];
978 return mode_cost;
979 }
980 }
981 }
982
983 static void newmv_diff_bias(MACROBLOCKD *xd, PREDICTION_MODE this_mode,
984 RD_STATS *this_rdc, BLOCK_SIZE bsize, int mv_row,
985 int mv_col, int speed, uint32_t spatial_variance) {
986 // Bias against MVs associated with NEWMV mode that are very different from
987 // top/left neighbors.
988 if (this_mode == NEWMV) {
989 int al_mv_average_row;
990 int al_mv_average_col;
991 int left_row, left_col;
992 int row_diff, col_diff;
993 int above_mv_valid = 0;
994 int left_mv_valid = 0;
995 int above_row = 0;
996 int above_col = 0;
997
998 if (xd->above_mbmi) {
999 above_mv_valid = xd->above_mbmi->mv[0].as_int != INVALID_MV;
1000 above_row = xd->above_mbmi->mv[0].as_mv.row;
1001 above_col = xd->above_mbmi->mv[0].as_mv.col;
1002 }
1003 if (xd->left_mbmi) {
1004 left_mv_valid = xd->left_mbmi->mv[0].as_int != INVALID_MV;
1005 left_row = xd->left_mbmi->mv[0].as_mv.row;
1006 left_col = xd->left_mbmi->mv[0].as_mv.col;
1007 }
1008 if (above_mv_valid && left_mv_valid) {
1009 al_mv_average_row = (above_row + left_row + 1) >> 1;
1010 al_mv_average_col = (above_col + left_col + 1) >> 1;
1011 } else if (above_mv_valid) {
1012 al_mv_average_row = above_row;
1013 al_mv_average_col = above_col;
1014 } else if (left_mv_valid) {
1015 al_mv_average_row = left_row;
1016 al_mv_average_col = left_col;
1017 } else {
1018 al_mv_average_row = al_mv_average_col = 0;
1019 }
1020 row_diff = al_mv_average_row - mv_row;
1021 col_diff = al_mv_average_col - mv_col;
1022 if (row_diff > 80 || row_diff < -80 || col_diff > 80 || col_diff < -80) {
1023 if (bsize >= BLOCK_32X32)
1024 this_rdc->rdcost = this_rdc->rdcost << 1;
1025 else
1026 this_rdc->rdcost = 5 * this_rdc->rdcost >> 2;
1027 }
1028 } else {
1029 // Bias for speed >= 8 for low spatial variance.
1030 if (speed >= 8 && spatial_variance < 150 &&
1031 (mv_row > 64 || mv_row < -64 || mv_col > 64 || mv_col < -64))
1032 this_rdc->rdcost = 5 * this_rdc->rdcost >> 2;
1033 }
1034 }
1035
1036 static void model_rd_for_sb_uv(AV1_COMP *cpi, BLOCK_SIZE plane_bsize,
1037 MACROBLOCK *x, MACROBLOCKD *xd,
1038 RD_STATS *this_rdc, unsigned int *var_y,
1039 unsigned int *sse_y, int start_plane,
1040 int stop_plane) {
1041 // Note our transform coeffs are 8 times an orthogonal transform.
1042 // Hence quantizer step is also 8 times. To get effective quantizer
1043 // we need to divide by 8 before sending to modeling function.
1044 unsigned int sse;
1045 int rate;
1046 int64_t dist;
1047 int i;
1048 uint32_t tot_var = *var_y;
1049 uint32_t tot_sse = *sse_y;
1050
1051 this_rdc->rate = 0;
1052 this_rdc->dist = 0;
1053 this_rdc->skip = 0;
1054
1055 for (i = start_plane; i <= stop_plane; ++i) {
1056 struct macroblock_plane *const p = &x->plane[i];
1057 struct macroblockd_plane *const pd = &xd->plane[i];
1058 const uint32_t dc_quant = p->dequant_QTX[0];
1059 const uint32_t ac_quant = p->dequant_QTX[1];
1060 const BLOCK_SIZE bs = plane_bsize;
1061 unsigned int var;
1062 if (!x->color_sensitivity[i - 1]) continue;
1063
1064 var = cpi->fn_ptr[bs].vf(p->src.buf, p->src.stride, pd->dst.buf,
1065 pd->dst.stride, &sse);
1066 assert(sse >= var);
1067 tot_var += var;
1068 tot_sse += sse;
1069
1070 av1_model_rd_from_var_lapndz(sse - var, num_pels_log2_lookup[bs],
1071 dc_quant >> 3, &rate, &dist);
1072
1073 this_rdc->rate += rate >> 1;
1074 this_rdc->dist += dist << 3;
1075
1076 av1_model_rd_from_var_lapndz(var, num_pels_log2_lookup[bs], ac_quant >> 3,
1077 &rate, &dist);
1078
1079 this_rdc->rate += rate;
1080 this_rdc->dist += dist << 4;
1081 }
1082
1083 if (this_rdc->rate == 0) {
1084 this_rdc->skip = 1;
1085 }
1086
1087 if (RDCOST(x->rdmult, this_rdc->rate, this_rdc->dist) >=
1088 RDCOST(x->rdmult, 0, ((int64_t)tot_sse) << 4)) {
1089 this_rdc->rate = 0;
1090 this_rdc->dist = tot_sse << 4;
1091 this_rdc->skip = 1;
1092 }
1093
1094 *var_y = tot_var;
1095 *sse_y = tot_sse;
1096 }
1097
1098 struct estimate_block_intra_args {
1099 AV1_COMP *cpi;
1100 MACROBLOCK *x;
1101 PREDICTION_MODE mode;
1102 int skippable;
1103 RD_STATS *rdc;
1104 };
1105
1106 static void estimate_block_intra(int plane, int block, int row, int col,
1107 BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
1108 void *arg) {
1109 struct estimate_block_intra_args *const args = arg;
1110 AV1_COMP *const cpi = args->cpi;
1111 AV1_COMMON *const cm = &cpi->common;
1112 MACROBLOCK *const x = args->x;
1113 MACROBLOCKD *const xd = &x->e_mbd;
1114 struct macroblock_plane *const p = &x->plane[plane];
1115 struct macroblockd_plane *const pd = &xd->plane[plane];
1116 const BLOCK_SIZE bsize_tx = txsize_to_bsize[tx_size];
1117 uint8_t *const src_buf_base = p->src.buf;
1118 uint8_t *const dst_buf_base = pd->dst.buf;
1119 const int64_t src_stride = p->src.stride;
1120 const int64_t dst_stride = pd->dst.stride;
1121 RD_STATS this_rdc;
1122
1123 (void)block;
1124
1125 p->src.buf = &src_buf_base[4 * (row * src_stride + col)];
1126 pd->dst.buf = &dst_buf_base[4 * (row * dst_stride + col)];
1127
1128 av1_predict_intra_block_facade(cm, xd, plane, col, row, tx_size);
1129
1130 if (plane == 0) {
1131 int64_t this_sse = INT64_MAX;
1132 block_yrd(cpi, x, 0, 0, &this_rdc, &args->skippable, &this_sse, bsize_tx,
1133 AOMMIN(tx_size, TX_16X16));
1134 } else {
1135 unsigned int var = 0;
1136 unsigned int sse = 0;
1137 model_rd_for_sb_uv(cpi, plane_bsize, x, xd, &this_rdc, &var, &sse, plane,
1138 plane);
1139 }
1140
1141 p->src.buf = src_buf_base;
1142 pd->dst.buf = dst_buf_base;
1143 args->rdc->rate += this_rdc.rate;
1144 args->rdc->dist += this_rdc.dist;
1145 }
1146
1147 static INLINE void update_thresh_freq_fact(AV1_COMP *cpi, MACROBLOCK *x,
1148 BLOCK_SIZE bsize,
1149 MV_REFERENCE_FRAME ref_frame,
1150 THR_MODES best_mode_idx,
1151 PREDICTION_MODE mode) {
1152 THR_MODES thr_mode_idx = mode_idx[ref_frame][mode_offset(mode)];
1153 int *freq_fact = &x->thresh_freq_fact[bsize][thr_mode_idx];
1154 if (thr_mode_idx == best_mode_idx) {
1155 *freq_fact -= (*freq_fact >> 4);
1156 } else {
1157 *freq_fact =
1158 AOMMIN(*freq_fact + RD_THRESH_INC,
1159 cpi->sf.inter_sf.adaptive_rd_thresh * RD_THRESH_MAX_FACT);
1160 }
1161 }
1162
1163 static INLINE int get_force_skip_low_temp_var_small_sb(uint8_t *variance_low,
1164 int mi_row, int mi_col,
1165 BLOCK_SIZE bsize) {
1166 // Relative indices of MB inside the superblock.
1167 const int mi_x = mi_row & 0xF;
1168 const int mi_y = mi_col & 0xF;
1169 // Relative indices of 16x16 block inside the superblock.
1170 const int i = mi_x >> 2;
1171 const int j = mi_y >> 2;
1172 int force_skip_low_temp_var = 0;
1173 // Set force_skip_low_temp_var based on the block size and block offset.
1174 switch (bsize) {
1175 case BLOCK_64X64: force_skip_low_temp_var = variance_low[0]; break;
1176 case BLOCK_64X32:
1177 if (!mi_y && !mi_x) {
1178 force_skip_low_temp_var = variance_low[1];
1179 } else if (!mi_y && mi_x) {
1180 force_skip_low_temp_var = variance_low[2];
1181 }
1182 break;
1183 case BLOCK_32X64:
1184 if (!mi_y && !mi_x) {
1185 force_skip_low_temp_var = variance_low[3];
1186 } else if (mi_y && !mi_x) {
1187 force_skip_low_temp_var = variance_low[4];
1188 }
1189 break;
1190 case BLOCK_32X32:
1191 if (!mi_y && !mi_x) {
1192 force_skip_low_temp_var = variance_low[5];
1193 } else if (mi_y && !mi_x) {
1194 force_skip_low_temp_var = variance_low[6];
1195 } else if (!mi_y && mi_x) {
1196 force_skip_low_temp_var = variance_low[7];
1197 } else if (mi_y && mi_x) {
1198 force_skip_low_temp_var = variance_low[8];
1199 }
1200 break;
1201 case BLOCK_32X16:
1202 case BLOCK_16X32:
1203 case BLOCK_16X16:
1204 force_skip_low_temp_var = variance_low[pos_shift_16x16[i][j]];
1205 break;
1206 default: break;
1207 }
1208
1209 return force_skip_low_temp_var;
1210 }
1211
1212 static INLINE int get_force_skip_low_temp_var(uint8_t *variance_low, int mi_row,
1213 int mi_col, BLOCK_SIZE bsize) {
1214 int force_skip_low_temp_var = 0;
1215 int x, y;
1216 x = (mi_col & 0x1F) >> 4;
1217 // y = (mi_row & 0x1F) >> 4;
1218 // const int idx64 = (y << 1) + x;
1219 y = (mi_row & 0x17) >> 3;
1220 const int idx64 = y + x;
1221
1222 x = (mi_col & 0xF) >> 3;
1223 // y = (mi_row & 0xF) >> 3;
1224 // const int idx32 = (y << 1) + x;
1225 y = (mi_row & 0xB) >> 2;
1226 const int idx32 = y + x;
1227
1228 x = (mi_col & 0x7) >> 2;
1229 // y = (mi_row & 0x7) >> 2;
1230 // const int idx16 = (y << 1) + x;
1231 y = (mi_row & 0x5) >> 1;
1232 const int idx16 = y + x;
1233 // Set force_skip_low_temp_var based on the block size and block offset.
1234 switch (bsize) {
1235 case BLOCK_128X128: force_skip_low_temp_var = variance_low[0]; break;
1236 case BLOCK_128X64:
1237 assert((mi_col & 0x1F) == 0);
1238 force_skip_low_temp_var = variance_low[1 + ((mi_row & 0x1F) != 0)];
1239 break;
1240 case BLOCK_64X128:
1241 assert((mi_row & 0x1F) == 0);
1242 force_skip_low_temp_var = variance_low[3 + ((mi_col & 0x1F) != 0)];
1243 break;
1244 case BLOCK_64X64:
1245 // Location of this 64x64 block inside the 128x128 superblock
1246 force_skip_low_temp_var = variance_low[5 + idx64];
1247 break;
1248 case BLOCK_64X32:
1249 x = (mi_col & 0x1F) >> 4;
1250 y = (mi_row & 0x1F) >> 3;
1251 /*
1252 .---------------.---------------.
1253 | x=0,y=0,idx=0 | x=0,y=0,idx=2 |
1254 :---------------+---------------:
1255 | x=0,y=1,idx=1 | x=1,y=1,idx=3 |
1256 :---------------+---------------:
1257 | x=0,y=2,idx=4 | x=1,y=2,idx=6 |
1258 :---------------+---------------:
1259 | x=0,y=3,idx=5 | x=1,y=3,idx=7 |
1260 '---------------'---------------'
1261 */
1262 const int idx64x32 = (x << 1) + (y % 2) + ((y >> 1) << 2);
1263 force_skip_low_temp_var = variance_low[9 + idx64x32];
1264 break;
1265 case BLOCK_32X64:
1266 x = (mi_col & 0x1F) >> 3;
1267 y = (mi_row & 0x1F) >> 4;
1268 const int idx32x64 = (y << 2) + x;
1269 force_skip_low_temp_var = variance_low[17 + idx32x64];
1270 break;
1271 case BLOCK_32X32:
1272 force_skip_low_temp_var = variance_low[25 + (idx64 << 2) + idx32];
1273 break;
1274 case BLOCK_32X16:
1275 case BLOCK_16X32:
1276 case BLOCK_16X16:
1277 force_skip_low_temp_var =
1278 variance_low[41 + (idx64 << 4) + (idx32 << 2) + idx16];
1279 break;
1280 default: break;
1281 }
1282 return force_skip_low_temp_var;
1283 }
1284
1285 #define FILTER_SEARCH_SIZE 2
1286 static void search_filter_ref(AV1_COMP *cpi, MACROBLOCK *x, RD_STATS *this_rdc,
1287 int mi_row, int mi_col, PRED_BUFFER *tmp,
1288 BLOCK_SIZE bsize, int reuse_inter_pred,
1289 PRED_BUFFER **this_mode_pred, unsigned int *var_y,
1290 unsigned int *sse_y, int *this_early_term,
1291 int use_model_yrd_large, int64_t *sse_block_yrd) {
1292 AV1_COMMON *const cm = &cpi->common;
1293 MACROBLOCKD *const xd = &x->e_mbd;
1294 struct macroblockd_plane *const pd = &xd->plane[0];
1295 MB_MODE_INFO *const mi = xd->mi[0];
1296 const int bw = block_size_wide[bsize];
1297 int pf_rate[FILTER_SEARCH_SIZE] = { 0 };
1298 int64_t pf_dist[FILTER_SEARCH_SIZE] = { 0 };
1299 unsigned int pf_var[FILTER_SEARCH_SIZE] = { 0 };
1300 unsigned int pf_sse[FILTER_SEARCH_SIZE] = { 0 };
1301 int64_t pf_sse_block_yrd[FILTER_SEARCH_SIZE] = { 0 };
1302 TX_SIZE pf_tx_size[FILTER_SEARCH_SIZE] = { 0 };
1303 PRED_BUFFER *current_pred = *this_mode_pred;
1304 int skip_txfm[FILTER_SEARCH_SIZE] = { 0 };
1305 int best_skip = 0;
1306 int best_early_term = 0;
1307 int64_t best_cost = INT64_MAX;
1308 int best_filter_index = -1;
1309 InterpFilter filters[FILTER_SEARCH_SIZE] = { EIGHTTAP_REGULAR,
1310 EIGHTTAP_SMOOTH };
1311 int i;
1312 for (i = 0; i < FILTER_SEARCH_SIZE; ++i) {
1313 int64_t cost;
1314 InterpFilter filter = filters[i];
1315 mi->interp_filters = av1_broadcast_interp_filter(filter);
1316 av1_enc_build_inter_predictor_y(xd, mi_row, mi_col);
1317 if (use_model_yrd_large)
1318 model_skip_for_sb_y_large(cpi, bsize, mi_row, mi_col, x, xd, &pf_rate[i],
1319 &pf_dist[i], &pf_var[i], &pf_sse[i],
1320 this_early_term, 1);
1321 else
1322 model_rd_for_sb_y(cpi, bsize, x, xd, &pf_rate[i], &pf_dist[i],
1323 &skip_txfm[i], NULL, &pf_var[i], &pf_sse[i], 1);
1324 pf_rate[i] += av1_get_switchable_rate(x, xd, cm->features.interp_filter);
1325 cost = RDCOST(x->rdmult, pf_rate[i], pf_dist[i]);
1326 pf_tx_size[i] = mi->tx_size;
1327 if (cost < best_cost) {
1328 best_filter_index = i;
1329 best_cost = cost;
1330 best_skip = skip_txfm[i];
1331 best_early_term = *this_early_term;
1332 if (reuse_inter_pred) {
1333 if (*this_mode_pred != current_pred) {
1334 free_pred_buffer(*this_mode_pred);
1335 *this_mode_pred = current_pred;
1336 }
1337 current_pred = &tmp[get_pred_buffer(tmp, 3)];
1338 pd->dst.buf = current_pred->data;
1339 pd->dst.stride = bw;
1340 }
1341 }
1342 }
1343 assert(best_filter_index >= 0 && best_filter_index < FILTER_SEARCH_SIZE);
1344 if (reuse_inter_pred && *this_mode_pred != current_pred)
1345 free_pred_buffer(current_pred);
1346
1347 mi->interp_filters = av1_broadcast_interp_filter(filters[best_filter_index]);
1348 mi->tx_size = pf_tx_size[best_filter_index];
1349 this_rdc->rate = pf_rate[best_filter_index];
1350 this_rdc->dist = pf_dist[best_filter_index];
1351 *var_y = pf_var[best_filter_index];
1352 *sse_y = pf_sse[best_filter_index];
1353 *sse_block_yrd = pf_sse_block_yrd[best_filter_index];
1354 this_rdc->skip = (best_skip || best_early_term);
1355 *this_early_term = best_early_term;
1356 if (reuse_inter_pred) {
1357 pd->dst.buf = (*this_mode_pred)->data;
1358 pd->dst.stride = (*this_mode_pred)->stride;
1359 } else if (best_filter_index < FILTER_SEARCH_SIZE - 1) {
1360 av1_enc_build_inter_predictor_y(xd, mi_row, mi_col);
1361 }
1362 }
1363
1364 #define COLLECT_PICK_MODE_STAT 0
1365
1366 #if COLLECT_PICK_MODE_STAT
1367 typedef struct _mode_search_stat {
1368 int32_t num_blocks[BLOCK_SIZES];
1369 int64_t avg_block_times[BLOCK_SIZES];
1370 int32_t num_searches[BLOCK_SIZES][MB_MODE_COUNT];
1371 int32_t num_nonskipped_searches[BLOCK_SIZES][MB_MODE_COUNT];
1372 int64_t search_times[BLOCK_SIZES][MB_MODE_COUNT];
1373 int64_t nonskipped_search_times[BLOCK_SIZES][MB_MODE_COUNT];
1374 struct aom_usec_timer timer1;
1375 struct aom_usec_timer timer2;
1376 } mode_search_stat;
1377 #endif // COLLECT_PICK_MODE_STAT
1378
1379 static void compute_intra_yprediction(const AV1_COMMON *cm,
1380 PREDICTION_MODE mode, BLOCK_SIZE bsize,
1381 MACROBLOCK *x, MACROBLOCKD *xd) {
1382 struct macroblockd_plane *const pd = &xd->plane[0];
1383 struct macroblock_plane *const p = &x->plane[0];
1384 uint8_t *const src_buf_base = p->src.buf;
1385 uint8_t *const dst_buf_base = pd->dst.buf;
1386 const int src_stride = p->src.stride;
1387 const int dst_stride = pd->dst.stride;
1388 int plane = 0;
1389 int row, col;
1390 // block and transform sizes, in number of 4x4 blocks log 2 ("*_b")
1391 // 4x4=0, 8x8=2, 16x16=4, 32x32=6, 64x64=8
1392 // transform size varies per plane, look it up in a common way.
1393 const TX_SIZE tx_size = max_txsize_lookup[bsize];
1394 const BLOCK_SIZE plane_bsize =
1395 get_plane_block_size(bsize, pd->subsampling_x, pd->subsampling_y);
1396 // If mb_to_right_edge is < 0 we are in a situation in which
1397 // the current block size extends into the UMV and we won't
1398 // visit the sub blocks that are wholly within the UMV.
1399 const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
1400 const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
1401 // Keep track of the row and column of the blocks we use so that we know
1402 // if we are in the unrestricted motion border.
1403 for (row = 0; row < max_blocks_high; row += (1 << tx_size)) {
1404 // Skip visiting the sub blocks that are wholly within the UMV.
1405 for (col = 0; col < max_blocks_wide; col += (1 << tx_size)) {
1406 p->src.buf = &src_buf_base[4 * (row * (int64_t)src_stride + col)];
1407 pd->dst.buf = &dst_buf_base[4 * (row * (int64_t)dst_stride + col)];
1408 av1_predict_intra_block(cm, xd, block_size_wide[bsize],
1409 block_size_high[bsize], tx_size, mode, 0, 0,
1410 FILTER_INTRA_MODES, pd->dst.buf, dst_stride,
1411 pd->dst.buf, dst_stride, 0, 0, plane);
1412 }
1413 }
1414 p->src.buf = src_buf_base;
1415 pd->dst.buf = dst_buf_base;
1416 }
1417
1418 void av1_pick_intra_mode(AV1_COMP *cpi, MACROBLOCK *x, RD_STATS *rd_cost,
1419 BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx) {
1420 AV1_COMMON *const cm = &cpi->common;
1421 MACROBLOCKD *const xd = &x->e_mbd;
1422 MB_MODE_INFO *const mi = xd->mi[0];
1423 RD_STATS this_rdc, best_rdc;
1424 struct estimate_block_intra_args args = { cpi, x, DC_PRED, 1, 0 };
1425 const TX_SIZE intra_tx_size =
1426 AOMMIN(max_txsize_lookup[bsize],
1427 tx_mode_to_biggest_tx_size[x->tx_mode_search_type]);
1428 int *bmode_costs;
1429 const MB_MODE_INFO *above_mi = xd->above_mbmi;
1430 const MB_MODE_INFO *left_mi = xd->left_mbmi;
1431 const PREDICTION_MODE A = av1_above_block_mode(above_mi);
1432 const PREDICTION_MODE L = av1_left_block_mode(left_mi);
1433 bmode_costs = x->y_mode_costs[A][L];
1434
1435 av1_invalid_rd_stats(&best_rdc);
1436 av1_invalid_rd_stats(&this_rdc);
1437
1438 init_mbmi(mi, DC_PRED, INTRA_FRAME, NONE_FRAME, cm);
1439 mi->mv[0].as_int = mi->mv[1].as_int = INVALID_MV;
1440
1441 // Change the limit of this loop to add other intra prediction
1442 // mode tests.
1443 for (int i = 0; i < 4; ++i) {
1444 PREDICTION_MODE this_mode = intra_mode_list[i];
1445 this_rdc.dist = this_rdc.rate = 0;
1446 args.mode = this_mode;
1447 args.skippable = 1;
1448 args.rdc = &this_rdc;
1449 mi->tx_size = intra_tx_size;
1450 av1_foreach_transformed_block_in_plane(xd, bsize, 0, estimate_block_intra,
1451 &args);
1452 if (args.skippable) {
1453 this_rdc.rate = av1_cost_symbol(av1_get_skip_cdf(xd)[1]);
1454 } else {
1455 this_rdc.rate += av1_cost_symbol(av1_get_skip_cdf(xd)[0]);
1456 }
1457 this_rdc.rate += bmode_costs[this_mode];
1458 this_rdc.rdcost = RDCOST(x->rdmult, this_rdc.rate, this_rdc.dist);
1459
1460 if (this_rdc.rdcost < best_rdc.rdcost) {
1461 best_rdc = this_rdc;
1462 mi->mode = this_mode;
1463 }
1464 }
1465
1466 *rd_cost = best_rdc;
1467
1468 #if CONFIG_INTERNAL_STATS
1469 store_coding_context(x, ctx, mi->mode);
1470 #else
1471 store_coding_context(x, ctx);
1472 #endif // CONFIG_INTERNAL_STATS
1473 }
1474
1475 void av1_nonrd_pick_inter_mode_sb(AV1_COMP *cpi, TileDataEnc *tile_data,
1476 MACROBLOCK *x, RD_STATS *rd_cost,
1477 BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx,
1478 int64_t best_rd_so_far) {
1479 AV1_COMMON *const cm = &cpi->common;
1480 MACROBLOCKD *const xd = &x->e_mbd;
1481 MB_MODE_INFO *const mi = xd->mi[0];
1482 struct macroblockd_plane *const pd = &xd->plane[0];
1483
1484 BEST_PICKMODE best_pickmode;
1485 int inter_mode_mask[BLOCK_SIZES];
1486 #if COLLECT_PICK_MODE_STAT
1487 static mode_search_stat ms_stat;
1488 #endif
1489 MV_REFERENCE_FRAME ref_frame;
1490 MV_REFERENCE_FRAME usable_ref_frame, second_ref_frame;
1491 int_mv frame_mv[MB_MODE_COUNT][REF_FRAMES];
1492 uint8_t mode_checked[MB_MODE_COUNT][REF_FRAMES];
1493 struct buf_2d yv12_mb[8][MAX_MB_PLANE];
1494 static const int flag_list[8] = { 0, AOM_LAST_FLAG, 0, 0, AOM_GOLD_FLAG, 0,
1495 0, AOM_ALT_FLAG };
1496 RD_STATS this_rdc, best_rdc;
1497 // var_y and sse_y are saved to be used in skipping checking
1498 unsigned int sse_y = UINT_MAX;
1499 unsigned int var_y = UINT_MAX;
1500 const int *const rd_threshes = cpi->rd.threshes[mi->segment_id][bsize];
1501 const int *const rd_thresh_freq_fact = x->thresh_freq_fact[bsize];
1502 InterpFilter filter_ref;
1503 int ref_frame_skip_mask = 0;
1504 int best_pred_sad = INT_MAX;
1505 int best_early_term = 0;
1506 unsigned int ref_costs_single[REF_FRAMES],
1507 ref_costs_comp[REF_FRAMES][REF_FRAMES];
1508 int force_skip_low_temp_var = 0;
1509 int skip_ref_find_pred[8] = { 0 };
1510 unsigned int sse_zeromv_norm = UINT_MAX;
1511 const unsigned int thresh_skip_golden = 500;
1512 int gf_temporal_ref = 0;
1513 const struct segmentation *const seg = &cm->seg;
1514 int num_inter_modes = RT_INTER_MODES;
1515 unsigned char segment_id = mi->segment_id;
1516 PRED_BUFFER tmp[4];
1517 DECLARE_ALIGNED(16, uint8_t, pred_buf[3 * 128 * 128]);
1518 PRED_BUFFER *this_mode_pred = NULL;
1519 const int reuse_inter_pred =
1520 cpi->sf.rt_sf.reuse_inter_pred_nonrd && cm->seq_params.bit_depth == 8;
1521 const int bh = block_size_high[bsize];
1522 const int bw = block_size_wide[bsize];
1523 const int pixels_in_block = bh * bw;
1524 struct buf_2d orig_dst = pd->dst;
1525 const CommonQuantParams *quant_params = &cm->quant_params;
1526 #if COLLECT_PICK_MODE_STAT
1527 aom_usec_timer_start(&ms_stat.timer2);
1528 #endif
1529 int intra_cost_penalty = av1_get_intra_cost_penalty(
1530 quant_params->base_qindex, quant_params->y_dc_delta_q,
1531 cm->seq_params.bit_depth);
1532 int64_t inter_mode_thresh = RDCOST(x->rdmult, intra_cost_penalty, 0);
1533 const int perform_intra_pred = cpi->sf.rt_sf.check_intra_pred_nonrd;
1534 int use_modeled_non_rd_cost = 0;
1535 int enable_filter_search = 0;
1536 InterpFilter default_interp_filter = EIGHTTAP_REGULAR;
1537 int64_t thresh_sad_pred = INT64_MAX;
1538
1539 (void)best_rd_so_far;
1540
1541 init_best_pickmode(&best_pickmode);
1542
1543 for (int i = 0; i < BLOCK_SIZES; ++i) inter_mode_mask[i] = INTER_ALL;
1544
1545 // TODO(kyslov) Move this to Speed Features
1546 inter_mode_mask[BLOCK_128X128] = INTER_NEAREST_NEAR;
1547
1548 struct scale_factors *const sf_last = get_ref_scale_factors(cm, LAST_FRAME);
1549 struct scale_factors *const sf_golden =
1550 get_ref_scale_factors(cm, GOLDEN_FRAME);
1551 gf_temporal_ref = 1;
1552 // For temporal long term prediction, check that the golden reference
1553 // is same scale as last reference, otherwise disable.
1554 if ((sf_last->x_scale_fp != sf_golden->x_scale_fp) ||
1555 (sf_last->y_scale_fp != sf_golden->y_scale_fp)) {
1556 gf_temporal_ref = 0;
1557 }
1558
1559 av1_collect_neighbors_ref_counts(xd);
1560
1561 estimate_single_ref_frame_costs(cm, xd, x, segment_id, ref_costs_single);
1562 if (cpi->sf.rt_sf.use_comp_ref_nonrd)
1563 estimate_comp_ref_frame_costs(cm, xd, x, segment_id, ref_costs_comp);
1564
1565 memset(&mode_checked[0][0], 0, MB_MODE_COUNT * REF_FRAMES);
1566 if (reuse_inter_pred) {
1567 for (int i = 0; i < 3; i++) {
1568 tmp[i].data = &pred_buf[pixels_in_block * i];
1569 tmp[i].stride = bw;
1570 tmp[i].in_use = 0;
1571 }
1572 tmp[3].data = pd->dst.buf;
1573 tmp[3].stride = pd->dst.stride;
1574 tmp[3].in_use = 0;
1575 }
1576
1577 x->force_skip = 0;
1578
1579 // Instead of using av1_get_pred_context_switchable_interp(xd) to assign
1580 // filter_ref, we use a less strict condition on assigning filter_ref.
1581 // This is to reduce the probabily of entering the flow of not assigning
1582 // filter_ref and then skip filter search.
1583 filter_ref = cm->features.interp_filter;
1584
1585 // initialize mode decisions
1586 av1_invalid_rd_stats(&best_rdc);
1587 av1_invalid_rd_stats(&this_rdc);
1588 av1_invalid_rd_stats(rd_cost);
1589 mi->sb_type = bsize;
1590 mi->ref_frame[0] = NONE_FRAME;
1591 mi->ref_frame[1] = NONE_FRAME;
1592
1593 usable_ref_frame =
1594 cpi->sf.rt_sf.use_nonrd_altref_frame ? ALTREF_FRAME : GOLDEN_FRAME;
1595
1596 if (cpi->rc.frames_since_golden == 0 && gf_temporal_ref) {
1597 skip_ref_find_pred[GOLDEN_FRAME] = 1;
1598 if (!cpi->sf.rt_sf.use_nonrd_altref_frame) usable_ref_frame = LAST_FRAME;
1599 }
1600
1601 const int mi_row = xd->mi_row;
1602 const int mi_col = xd->mi_col;
1603 const int is_small_sb = (cm->seq_params.sb_size == BLOCK_64X64);
1604 if (cpi->sf.rt_sf.short_circuit_low_temp_var &&
1605 x->nonrd_prune_ref_frame_search) {
1606 if (is_small_sb)
1607 force_skip_low_temp_var = get_force_skip_low_temp_var_small_sb(
1608 &x->variance_low[0], mi_row, mi_col, bsize);
1609 else
1610 force_skip_low_temp_var = get_force_skip_low_temp_var(
1611 &x->variance_low[0], mi_row, mi_col, bsize);
1612 // If force_skip_low_temp_var is set, skip golden reference.
1613 if (force_skip_low_temp_var) {
1614 usable_ref_frame = LAST_FRAME;
1615 }
1616 }
1617
1618 // If the segment reference frame feature is enabled and it's set to GOLDEN
1619 // reference, then make sure we don't skip checking GOLDEN, this is to
1620 // prevent possibility of not picking any mode.
1621 if (segfeature_active(seg, mi->segment_id, SEG_LVL_REF_FRAME) &&
1622 get_segdata(seg, mi->segment_id, SEG_LVL_REF_FRAME) == GOLDEN_FRAME) {
1623 usable_ref_frame = GOLDEN_FRAME;
1624 skip_ref_find_pred[GOLDEN_FRAME] = 0;
1625 }
1626
1627 for (MV_REFERENCE_FRAME ref_frame_iter = LAST_FRAME;
1628 ref_frame_iter <= usable_ref_frame; ++ref_frame_iter) {
1629 // Skip find_predictor if the reference frame is not in the
1630 // ref_frame_flags (i.e., not used as a reference for this frame).
1631 skip_ref_find_pred[ref_frame_iter] =
1632 !(cpi->ref_frame_flags & flag_list[ref_frame_iter]);
1633 if (!skip_ref_find_pred[ref_frame_iter]) {
1634 find_predictors(cpi, x, ref_frame_iter, frame_mv, &ref_frame_skip_mask,
1635 flag_list, tile_data, yv12_mb, bsize,
1636 force_skip_low_temp_var);
1637 }
1638 }
1639
1640 thresh_sad_pred = ((int64_t)x->pred_mv_sad[LAST_FRAME]) << 1;
1641 // Increase threshold for less agressive pruning.
1642 if (cpi->sf.rt_sf.nonrd_prune_ref_frame_search == 1)
1643 thresh_sad_pred += (x->pred_mv_sad[LAST_FRAME] >> 2);
1644
1645 const int large_block = bsize >= BLOCK_32X32;
1646 const int use_model_yrd_large =
1647 cpi->oxcf.rc_mode == AOM_CBR && large_block &&
1648 !cyclic_refresh_segment_id_boosted(xd->mi[0]->segment_id) &&
1649 quant_params->base_qindex && cm->seq_params.bit_depth == 8;
1650
1651 #if COLLECT_PICK_MODE_STAT
1652 ms_stat.num_blocks[bsize]++;
1653 #endif
1654 init_mbmi(mi, DC_PRED, NONE_FRAME, NONE_FRAME, cm);
1655 mi->tx_size =
1656 AOMMIN(AOMMIN(max_txsize_lookup[bsize],
1657 tx_mode_to_biggest_tx_size[x->tx_mode_search_type]),
1658 TX_16X16);
1659
1660 // TODO(marpan): Look into reducing these conditions. For now constrain
1661 // it to avoid significant bdrate loss.
1662 if (cpi->sf.rt_sf.use_modeled_non_rd_cost &&
1663 quant_params->base_qindex > 120 && x->source_variance > 100 &&
1664 bsize <= BLOCK_16X16 && x->content_state_sb != kLowVarHighSumdiff &&
1665 x->content_state_sb != kHighSad)
1666 use_modeled_non_rd_cost = 1;
1667
1668 if (cpi->sf.rt_sf.use_nonrd_filter_search) {
1669 enable_filter_search = 1;
1670 if (cpi->sf.interp_sf.cb_pred_filter_search) {
1671 const int bsl = mi_size_wide_log2[bsize];
1672 enable_filter_search =
1673 (((mi_row + mi_col) >> bsl) +
1674 get_chessboard_index(cm->current_frame.frame_number)) &
1675 0x1;
1676 }
1677 if (x->source_variance <=
1678 cpi->sf.interp_sf.disable_filter_search_var_thresh)
1679 enable_filter_search = 0;
1680 }
1681
1682 for (int idx = 0; idx < num_inter_modes; ++idx) {
1683 int rate_mv = 0;
1684 int mode_rd_thresh;
1685 int mode_index;
1686 int64_t this_sse;
1687 int is_skippable;
1688 int this_early_term = 0;
1689 int skip_this_mv = 0;
1690 int comp_pred = 0;
1691 int force_mv_inter_layer = 0;
1692 PREDICTION_MODE this_mode;
1693 MB_MODE_INFO_EXT *const mbmi_ext = x->mbmi_ext;
1694 second_ref_frame = NONE_FRAME;
1695
1696 this_mode = ref_mode_set[idx].pred_mode;
1697 ref_frame = ref_mode_set[idx].ref_frame;
1698
1699 #if COLLECT_PICK_MODE_STAT
1700 aom_usec_timer_start(&ms_stat.timer1);
1701 ms_stat.num_searches[bsize][this_mode]++;
1702 #endif
1703 mi->mode = this_mode;
1704 mi->ref_frame[0] = ref_frame;
1705
1706 if (ref_frame > usable_ref_frame) continue;
1707 if (skip_ref_find_pred[ref_frame]) continue;
1708
1709 // Skip non-zero motion for SVC if skip_nonzeromv_ref is set.
1710 if (cpi->use_svc && frame_mv[this_mode][ref_frame].as_int != 0) {
1711 if (ref_frame == LAST_FRAME && cpi->svc.skip_nonzeromv_last)
1712 continue;
1713 else if (ref_frame == GOLDEN_FRAME && cpi->svc.skip_nonzeromv_gf)
1714 continue;
1715 }
1716
1717 // If the segment reference frame feature is enabled then do nothing if the
1718 // current ref frame is not allowed.
1719 if (segfeature_active(seg, mi->segment_id, SEG_LVL_REF_FRAME) &&
1720 get_segdata(seg, mi->segment_id, SEG_LVL_REF_FRAME) != (int)ref_frame)
1721 continue;
1722
1723 if (ref_frame != LAST_FRAME && cpi->oxcf.rc_mode == AOM_CBR &&
1724 sse_zeromv_norm < thresh_skip_golden && this_mode == NEWMV)
1725 continue;
1726
1727 if (!(cpi->ref_frame_flags & flag_list[ref_frame])) continue;
1728
1729 if (!(inter_mode_mask[bsize] & (1 << this_mode))) continue;
1730
1731 // Skip testing non-LAST if this flag is set.
1732 if (x->nonrd_prune_ref_frame_search) {
1733 if (x->nonrd_prune_ref_frame_search > 1 && ref_frame != LAST_FRAME &&
1734 (bsize > BLOCK_64X64 || (bsize > BLOCK_16X16 && this_mode == NEWMV)))
1735 continue;
1736
1737 if (ref_frame != LAST_FRAME && this_mode == NEARMV) continue;
1738 }
1739
1740 // Skip non-zeromv mode search for non-LAST frame if force_skip_low_temp_var
1741 // is set. If nearestmv for golden frame is 0, zeromv mode will be skipped
1742 // later.
1743 if (!force_mv_inter_layer && force_skip_low_temp_var &&
1744 ref_frame != LAST_FRAME && frame_mv[this_mode][ref_frame].as_int != 0) {
1745 continue;
1746 }
1747
1748 #if 0
1749 if (x->content_state_sb != kVeryHighSad &&
1750 (cpi->sf.short_circuit_low_temp_var >= 2 ||
1751 (cpi->sf.short_circuit_low_temp_var == 1 && bsize == BLOCK_64X64))
1752 && force_skip_low_temp_var && ref_frame == LAST_FRAME && this_mode ==
1753 NEWMV) {
1754 continue;
1755 }
1756 #endif
1757
1758 // Disable this drop out case if the ref frame segment level feature is
1759 // enabled for this segment. This is to prevent the possibility that we
1760 // end up unable to pick any mode.
1761 if (!segfeature_active(seg, mi->segment_id, SEG_LVL_REF_FRAME)) {
1762 // Check for skipping GOLDEN and ALTREF based pred_mv_sad.
1763 if (cpi->sf.rt_sf.nonrd_prune_ref_frame_search > 0 &&
1764 x->pred_mv_sad[ref_frame] != INT_MAX && ref_frame != LAST_FRAME) {
1765 if ((int64_t)(x->pred_mv_sad[ref_frame]) > thresh_sad_pred)
1766 ref_frame_skip_mask |= (1 << ref_frame);
1767 }
1768 if (ref_frame_skip_mask & (1 << ref_frame)) continue;
1769 }
1770
1771 // Select prediction reference frames.
1772 for (int i = 0; i < MAX_MB_PLANE; i++) {
1773 xd->plane[i].pre[0] = yv12_mb[ref_frame][i];
1774 }
1775
1776 mi->ref_frame[0] = ref_frame;
1777 mi->ref_frame[1] = second_ref_frame;
1778 set_ref_ptrs(cm, xd, ref_frame, second_ref_frame);
1779
1780 mode_index = mode_idx[ref_frame][INTER_OFFSET(this_mode)];
1781 mode_rd_thresh = best_pickmode.best_mode_skip_txfm
1782 ? rd_threshes[mode_index] << 1
1783 : rd_threshes[mode_index];
1784
1785 // Increase mode_rd_thresh value for non-LAST for improved encoding
1786 // speed
1787 if (ref_frame != LAST_FRAME) {
1788 mode_rd_thresh = mode_rd_thresh << 1;
1789 if (ref_frame == GOLDEN_FRAME && cpi->rc.frames_since_golden > 4)
1790 mode_rd_thresh = mode_rd_thresh << 1;
1791 }
1792
1793 if (rd_less_than_thresh(best_rdc.rdcost, mode_rd_thresh,
1794 rd_thresh_freq_fact[mode_index]))
1795 if (frame_mv[this_mode][ref_frame].as_int != 0) continue;
1796
1797 if (this_mode == NEWMV && !force_mv_inter_layer) {
1798 if (search_new_mv(cpi, x, frame_mv, ref_frame, gf_temporal_ref, bsize,
1799 mi_row, mi_col, best_pred_sad, &rate_mv, &best_rdc))
1800 continue;
1801 }
1802
1803 for (PREDICTION_MODE inter_mv_mode = NEARESTMV; inter_mv_mode <= NEWMV;
1804 inter_mv_mode++) {
1805 if (inter_mv_mode == this_mode || comp_pred) continue;
1806 if (mode_checked[inter_mv_mode][ref_frame] &&
1807 frame_mv[this_mode][ref_frame].as_int ==
1808 frame_mv[inter_mv_mode][ref_frame].as_int) {
1809 skip_this_mv = 1;
1810 break;
1811 }
1812 }
1813
1814 if (skip_this_mv) continue;
1815
1816 mi->mode = this_mode;
1817 mi->mv[0].as_int = frame_mv[this_mode][ref_frame].as_int;
1818 mi->mv[1].as_int = 0;
1819 if (reuse_inter_pred) {
1820 if (!this_mode_pred) {
1821 this_mode_pred = &tmp[3];
1822 } else {
1823 this_mode_pred = &tmp[get_pred_buffer(tmp, 3)];
1824 pd->dst.buf = this_mode_pred->data;
1825 pd->dst.stride = bw;
1826 }
1827 }
1828 #if COLLECT_PICK_MODE_STAT
1829 ms_stat.num_nonskipped_searches[bsize][this_mode]++;
1830 #endif
1831 if (enable_filter_search &&
1832 ((mi->mv[0].as_mv.row & 0x07) || (mi->mv[0].as_mv.col & 0x07)) &&
1833 (ref_frame == LAST_FRAME || !x->nonrd_prune_ref_frame_search)) {
1834 search_filter_ref(cpi, x, &this_rdc, mi_row, mi_col, tmp, bsize,
1835 reuse_inter_pred, &this_mode_pred, &var_y, &sse_y,
1836 &this_early_term, use_model_yrd_large, &this_sse);
1837 } else {
1838 mi->interp_filters =
1839 (filter_ref == SWITCHABLE)
1840 ? av1_broadcast_interp_filter(default_interp_filter)
1841 : av1_broadcast_interp_filter(filter_ref);
1842 av1_enc_build_inter_predictor_y(xd, mi_row, mi_col);
1843 if (use_model_yrd_large) {
1844 model_skip_for_sb_y_large(cpi, bsize, mi_row, mi_col, x, xd, NULL, NULL,
1845 &var_y, &sse_y, &this_early_term,
1846 use_modeled_non_rd_cost);
1847 } else {
1848 model_rd_for_sb_y(cpi, bsize, x, xd, &this_rdc.rate, &this_rdc.dist,
1849 &this_rdc.skip, NULL, &var_y, &sse_y,
1850 use_modeled_non_rd_cost);
1851 }
1852 }
1853
1854 if (ref_frame == LAST_FRAME && frame_mv[this_mode][ref_frame].as_int == 0) {
1855 sse_zeromv_norm =
1856 sse_y >> (b_width_log2_lookup[bsize] + b_height_log2_lookup[bsize]);
1857 }
1858
1859 const int skip_ctx = av1_get_skip_context(xd);
1860 const int skip_cost = x->skip_cost[skip_ctx][1];
1861 const int no_skip_cost = x->skip_cost[skip_ctx][0];
1862 if (!this_early_term) {
1863 if (use_modeled_non_rd_cost) {
1864 if (this_rdc.skip) {
1865 this_rdc.rate = skip_cost;
1866 } else {
1867 this_rdc.rate += no_skip_cost;
1868 }
1869 } else {
1870 this_sse = (int64_t)sse_y;
1871 block_yrd(cpi, x, mi_row, mi_col, &this_rdc, &is_skippable, &this_sse,
1872 bsize, mi->tx_size);
1873 if (this_rdc.skip) {
1874 this_rdc.rate = skip_cost;
1875 } else {
1876 if (RDCOST(x->rdmult, this_rdc.rate, this_rdc.dist) >=
1877 RDCOST(x->rdmult, 0,
1878 this_sse)) { // this_sse already multiplied by 16 in
1879 // block_yrd
1880 this_rdc.skip = 1;
1881 this_rdc.rate = skip_cost;
1882 this_rdc.dist = this_sse;
1883 } else {
1884 this_rdc.rate += no_skip_cost;
1885 }
1886 }
1887 }
1888 } else {
1889 this_rdc.skip = 1;
1890 this_rdc.rate = skip_cost;
1891 this_rdc.dist = sse_y << 4;
1892 }
1893
1894 if (!this_early_term &&
1895 (x->color_sensitivity[0] || x->color_sensitivity[1])) {
1896 RD_STATS rdc_uv;
1897 const BLOCK_SIZE uv_bsize = get_plane_block_size(
1898 bsize, xd->plane[1].subsampling_x, xd->plane[1].subsampling_y);
1899 if (x->color_sensitivity[0]) {
1900 av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize,
1901 AOM_PLANE_U, AOM_PLANE_U);
1902 }
1903 if (x->color_sensitivity[1]) {
1904 av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize,
1905 AOM_PLANE_V, AOM_PLANE_V);
1906 }
1907 model_rd_for_sb_uv(cpi, uv_bsize, x, xd, &rdc_uv, &var_y, &sse_y, 1, 2);
1908 this_rdc.rate += rdc_uv.rate;
1909 this_rdc.dist += rdc_uv.dist;
1910 this_rdc.skip = this_rdc.skip && rdc_uv.skip;
1911 }
1912
1913 // TODO(kyslov) account for UV prediction cost
1914 this_rdc.rate += rate_mv;
1915 const int16_t mode_ctx =
1916 av1_mode_context_analyzer(mbmi_ext->mode_context, mi->ref_frame);
1917 this_rdc.rate += cost_mv_ref(x, this_mode, mode_ctx);
1918
1919 this_rdc.rate += ref_costs_single[ref_frame];
1920
1921 this_rdc.rdcost = RDCOST(x->rdmult, this_rdc.rate, this_rdc.dist);
1922 if (cpi->oxcf.rc_mode == AOM_CBR) {
1923 newmv_diff_bias(xd, this_mode, &this_rdc, bsize,
1924 frame_mv[this_mode][ref_frame].as_mv.row,
1925 frame_mv[this_mode][ref_frame].as_mv.col, cpi->speed,
1926 x->source_variance);
1927 }
1928
1929 mode_checked[this_mode][ref_frame] = 1;
1930 #if COLLECT_PICK_MODE_STAT
1931 aom_usec_timer_mark(&ms_stat.timer1);
1932 ms_stat.nonskipped_search_times[bsize][this_mode] +=
1933 aom_usec_timer_elapsed(&ms_stat.timer1);
1934 #endif
1935 if (this_rdc.rdcost < best_rdc.rdcost) {
1936 best_rdc = this_rdc;
1937 best_early_term = this_early_term;
1938 best_pickmode.best_mode = this_mode;
1939 best_pickmode.best_pred_filter = mi->interp_filters;
1940 best_pickmode.best_tx_size = mi->tx_size;
1941 best_pickmode.best_ref_frame = ref_frame;
1942 best_pickmode.best_mode_skip_txfm = this_rdc.skip;
1943 best_pickmode.best_second_ref_frame = second_ref_frame;
1944 if (reuse_inter_pred) {
1945 free_pred_buffer(best_pickmode.best_pred);
1946 best_pickmode.best_pred = this_mode_pred;
1947 }
1948 } else {
1949 if (reuse_inter_pred) free_pred_buffer(this_mode_pred);
1950 }
1951 if (best_early_term && idx > 0) {
1952 x->force_skip = 1;
1953 break;
1954 }
1955 }
1956
1957 mi->mode = best_pickmode.best_mode;
1958 mi->interp_filters = best_pickmode.best_pred_filter;
1959 mi->tx_size = best_pickmode.best_tx_size;
1960 memset(mi->inter_tx_size, mi->tx_size, sizeof(mi->inter_tx_size));
1961 mi->ref_frame[0] = best_pickmode.best_ref_frame;
1962 mi->mv[0].as_int =
1963 frame_mv[best_pickmode.best_mode][best_pickmode.best_ref_frame].as_int;
1964 mi->ref_frame[1] = best_pickmode.best_second_ref_frame;
1965 x->force_skip = best_rdc.skip;
1966
1967 // Perform intra prediction search, if the best SAD is above a certain
1968 // threshold.
1969 mi->angle_delta[PLANE_TYPE_Y] = 0;
1970 mi->angle_delta[PLANE_TYPE_UV] = 0;
1971 mi->filter_intra_mode_info.use_filter_intra = 0;
1972
1973 uint32_t spatial_var_thresh = 50;
1974 int motion_thresh = 32;
1975 // Adjust thresholds to make intra mode likely tested if the other
1976 // references (golden, alt) are skipped/not checked.
1977 if (cpi->sf.rt_sf.use_nonrd_altref_frame == 0 &&
1978 cpi->sf.rt_sf.nonrd_prune_ref_frame_search > 0) {
1979 spatial_var_thresh = 150;
1980 motion_thresh = 0;
1981 }
1982 int do_early_exit_rdthresh = 1;
1983 // Some adjustments to checking intra mode based on source variance.
1984 if (x->source_variance < spatial_var_thresh) {
1985 // If the best inter mode is large motion or non-LAST ref reduce intra cost
1986 // penalty, so intra mode is more likely tested.
1987 if (best_pickmode.best_ref_frame != LAST_FRAME ||
1988 abs(mi->mv[0].as_mv.row) >= motion_thresh ||
1989 abs(mi->mv[0].as_mv.col) >= motion_thresh) {
1990 intra_cost_penalty = intra_cost_penalty >> 2;
1991 inter_mode_thresh = RDCOST(x->rdmult, intra_cost_penalty, 0);
1992 do_early_exit_rdthresh = 0;
1993 }
1994 // For big blocks worth checking intra (since only DC will be checked),
1995 // even if best_early_term is set.
1996 if (bsize >= BLOCK_32X32) best_early_term = 0;
1997 }
1998
1999 if (best_rdc.rdcost == INT64_MAX ||
2000 (perform_intra_pred && !best_early_term &&
2001 best_rdc.rdcost > inter_mode_thresh &&
2002 bsize <= cpi->sf.part_sf.max_intra_bsize)) {
2003 int64_t this_sse = INT64_MAX;
2004 struct estimate_block_intra_args args = { cpi, x, DC_PRED, 1, 0 };
2005 PRED_BUFFER *const best_pred = best_pickmode.best_pred;
2006 TX_SIZE intra_tx_size =
2007 AOMMIN(AOMMIN(max_txsize_lookup[bsize],
2008 tx_mode_to_biggest_tx_size[x->tx_mode_search_type]),
2009 TX_16X16);
2010
2011 if (reuse_inter_pred && best_pred != NULL) {
2012 if (best_pred->data == orig_dst.buf) {
2013 this_mode_pred = &tmp[get_pred_buffer(tmp, 3)];
2014 aom_convolve_copy(best_pred->data, best_pred->stride,
2015 this_mode_pred->data, this_mode_pred->stride, 0, 0, 0,
2016 0, bw, bh);
2017 best_pickmode.best_pred = this_mode_pred;
2018 }
2019 }
2020 pd->dst = orig_dst;
2021
2022 for (int i = 0; i < 4; ++i) {
2023 const PREDICTION_MODE this_mode = intra_mode_list[i];
2024 const THR_MODES mode_index =
2025 mode_idx[INTRA_FRAME][mode_offset(this_mode)];
2026 const int mode_rd_thresh = rd_threshes[mode_index];
2027
2028 // Only check DC for blocks >= 32X32.
2029 if (this_mode > 0 && bsize >= BLOCK_32X32) continue;
2030
2031 if (rd_less_than_thresh(best_rdc.rdcost, mode_rd_thresh,
2032 rd_thresh_freq_fact[mode_index]) &&
2033 (do_early_exit_rdthresh || this_mode == SMOOTH_PRED)) {
2034 continue;
2035 }
2036 const BLOCK_SIZE uv_bsize = get_plane_block_size(
2037 bsize, xd->plane[1].subsampling_x, xd->plane[1].subsampling_y);
2038
2039 mi->mode = this_mode;
2040 mi->ref_frame[0] = INTRA_FRAME;
2041 mi->ref_frame[1] = NONE_FRAME;
2042
2043 this_rdc.dist = this_rdc.rate = 0;
2044 args.mode = this_mode;
2045 args.skippable = 1;
2046 args.rdc = &this_rdc;
2047 mi->tx_size = intra_tx_size;
2048 compute_intra_yprediction(cm, this_mode, bsize, x, xd);
2049 // Look into selecting tx_size here, based on prediction residual.
2050 if (use_modeled_non_rd_cost)
2051 model_rd_for_sb_y(cpi, bsize, x, xd, &this_rdc.rate, &this_rdc.dist,
2052 &this_rdc.skip, NULL, &var_y, &sse_y, 1);
2053 else
2054 block_yrd(cpi, x, mi_row, mi_col, &this_rdc, &args.skippable, &this_sse,
2055 bsize, mi->tx_size);
2056 // TODO(kyslov@) Need to account for skippable
2057 if (x->color_sensitivity[0]) {
2058 av1_foreach_transformed_block_in_plane(xd, uv_bsize, 1,
2059 estimate_block_intra, &args);
2060 }
2061 if (x->color_sensitivity[1]) {
2062 av1_foreach_transformed_block_in_plane(xd, uv_bsize, 2,
2063 estimate_block_intra, &args);
2064 }
2065
2066 int mode_cost = 0;
2067 if (av1_is_directional_mode(this_mode) && av1_use_angle_delta(bsize)) {
2068 mode_cost += x->angle_delta_cost[this_mode - V_PRED]
2069 [MAX_ANGLE_DELTA +
2070 mi->angle_delta[PLANE_TYPE_Y]];
2071 }
2072 if (this_mode == DC_PRED && av1_filter_intra_allowed_bsize(cm, bsize)) {
2073 mode_cost += x->filter_intra_cost[bsize][0];
2074 }
2075 this_rdc.rate += ref_costs_single[INTRA_FRAME];
2076 this_rdc.rate += intra_cost_penalty;
2077 this_rdc.rate += mode_cost;
2078 this_rdc.rdcost = RDCOST(x->rdmult, this_rdc.rate, this_rdc.dist);
2079
2080 if (this_rdc.rdcost < best_rdc.rdcost) {
2081 best_rdc = this_rdc;
2082 best_pickmode.best_mode = this_mode;
2083 best_pickmode.best_intra_tx_size = mi->tx_size;
2084 best_pickmode.best_ref_frame = INTRA_FRAME;
2085 best_pickmode.best_second_ref_frame = NONE_FRAME;
2086 mi->uv_mode = this_mode;
2087 mi->mv[0].as_int = INVALID_MV;
2088 mi->mv[1].as_int = INVALID_MV;
2089 }
2090 }
2091
2092 // Reset mb_mode_info to the best inter mode.
2093 if (best_pickmode.best_ref_frame != INTRA_FRAME) {
2094 mi->tx_size = best_pickmode.best_tx_size;
2095 } else {
2096 mi->tx_size = best_pickmode.best_intra_tx_size;
2097 }
2098 }
2099
2100 pd->dst = orig_dst;
2101 mi->mode = best_pickmode.best_mode;
2102 mi->ref_frame[0] = best_pickmode.best_ref_frame;
2103 mi->ref_frame[1] = best_pickmode.best_second_ref_frame;
2104
2105 if (!is_inter_block(mi)) {
2106 mi->interp_filters = av1_broadcast_interp_filter(SWITCHABLE_FILTERS);
2107 }
2108
2109 if (reuse_inter_pred && best_pickmode.best_pred != NULL) {
2110 PRED_BUFFER *const best_pred = best_pickmode.best_pred;
2111 if (best_pred->data != orig_dst.buf && is_inter_mode(mi->mode)) {
2112 aom_convolve_copy(best_pred->data, best_pred->stride, pd->dst.buf,
2113 pd->dst.stride, 0, 0, 0, 0, bw, bh);
2114 }
2115 }
2116 if (cpi->sf.inter_sf.adaptive_rd_thresh) {
2117 THR_MODES best_mode_idx =
2118 mode_idx[best_pickmode.best_ref_frame][mode_offset(mi->mode)];
2119 if (best_pickmode.best_ref_frame == INTRA_FRAME) {
2120 // Only consider the modes that are included in the intra_mode_list.
2121 int intra_modes = sizeof(intra_mode_list) / sizeof(PREDICTION_MODE);
2122 for (int i = 0; i < intra_modes; i++) {
2123 update_thresh_freq_fact(cpi, x, bsize, INTRA_FRAME, best_mode_idx,
2124 intra_mode_list[i]);
2125 }
2126 } else {
2127 for (ref_frame = LAST_FRAME; ref_frame <= usable_ref_frame; ++ref_frame) {
2128 PREDICTION_MODE this_mode;
2129 if (best_pickmode.best_ref_frame != ref_frame) continue;
2130 for (this_mode = NEARESTMV; this_mode <= NEWMV; ++this_mode) {
2131 update_thresh_freq_fact(cpi, x, bsize, ref_frame, best_mode_idx,
2132 this_mode);
2133 }
2134 }
2135 }
2136 }
2137
2138 #if CONFIG_INTERNAL_STATS
2139 store_coding_context(x, ctx, mi->mode);
2140 #else
2141 store_coding_context(x, ctx);
2142 #endif // CONFIG_INTERNAL_STATS
2143 #if COLLECT_PICK_MODE_STAT
2144 aom_usec_timer_mark(&ms_stat.timer2);
2145 ms_stat.avg_block_times[bsize] += aom_usec_timer_elapsed(&ms_stat.timer2);
2146 //
2147 if ((mi_row + mi_size_high[bsize] >= (cpi->common.mi_params.mi_rows)) &&
2148 (mi_col + mi_size_wide[bsize] >= (cpi->common.mi_params.mi_cols))) {
2149 int i, j;
2150 PREDICTION_MODE used_modes[3] = { NEARESTMV, NEARMV, NEWMV };
2151 BLOCK_SIZE bss[5] = { BLOCK_8X8, BLOCK_16X16, BLOCK_32X32, BLOCK_64X64,
2152 BLOCK_128X128 };
2153 int64_t total_time = 0l;
2154 int32_t total_blocks = 0;
2155
2156 printf("\n");
2157 for (i = 0; i < 5; i++) {
2158 printf("BS(%d) Num %d, Avg_time %f: ", bss[i], ms_stat.num_blocks[bss[i]],
2159 ms_stat.num_blocks[bss[i]] > 0
2160 ? (float)ms_stat.avg_block_times[bss[i]] /
2161 ms_stat.num_blocks[bss[i]]
2162 : 0);
2163 total_time += ms_stat.avg_block_times[bss[i]];
2164 total_blocks += ms_stat.num_blocks[bss[i]];
2165 for (j = 0; j < 3; j++) {
2166 printf("Mode %d, %d/%d tps %f ", used_modes[j],
2167 ms_stat.num_nonskipped_searches[bss[i]][used_modes[j]],
2168 ms_stat.num_searches[bss[i]][used_modes[j]],
2169 ms_stat.num_nonskipped_searches[bss[i]][used_modes[j]] > 0
2170 ? (float)ms_stat
2171 .nonskipped_search_times[bss[i]][used_modes[j]] /
2172 ms_stat.num_nonskipped_searches[bss[i]][used_modes[j]]
2173 : 0l);
2174 }
2175 printf("\n");
2176 }
2177 printf("Total time = %ld. Total blocks = %d\n", total_time, total_blocks);
2178 }
2179 //
2180 #endif // COLLECT_PICK_MODE_STAT
2181 *rd_cost = best_rdc;
2182 }
2183