Line data Source code
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 : #include <assert.h>
13 : #include <math.h>
14 : #include <stdio.h>
15 :
16 : #include "./av1_rtcd.h"
17 :
18 : #include "aom_dsp/aom_dsp_common.h"
19 : #include "aom_mem/aom_mem.h"
20 : #include "aom_ports/bitops.h"
21 : #include "aom_ports/mem.h"
22 : #include "aom_ports/system_state.h"
23 :
24 : #include "av1/common/common.h"
25 : #include "av1/common/entropy.h"
26 : #include "av1/common/entropymode.h"
27 : #include "av1/common/mvref_common.h"
28 : #include "av1/common/pred_common.h"
29 : #include "av1/common/quant_common.h"
30 : #include "av1/common/reconinter.h"
31 : #include "av1/common/reconintra.h"
32 : #include "av1/common/seg_common.h"
33 :
34 : #include "av1/encoder/av1_quantize.h"
35 : #include "av1/encoder/cost.h"
36 : #include "av1/encoder/encodemb.h"
37 : #include "av1/encoder/encodemv.h"
38 : #include "av1/encoder/encoder.h"
39 : #include "av1/encoder/mcomp.h"
40 : #include "av1/encoder/ratectrl.h"
41 : #include "av1/encoder/rd.h"
42 : #include "av1/encoder/tokenize.h"
43 :
44 : #define RD_THRESH_POW 1.25
45 :
46 : // Factor to weigh the rate for switchable interp filters.
47 : #define SWITCHABLE_INTERP_RATE_FACTOR 1
48 :
49 : // The baseline rd thresholds for breaking out of the rd loop for
50 : // certain modes are assumed to be based on 8x8 blocks.
51 : // This table is used to correct for block size.
52 : // The factors here are << 2 (2 = x0.5, 32 = x8 etc).
53 : static const uint8_t rd_thresh_block_size_factor[BLOCK_SIZES] = {
54 : #if CONFIG_CB4X4
55 : 2, 2, 2,
56 : #endif
57 : 2, 3, 3, 4, 6, 6, 8, 12, 12, 16, 24, 24, 32,
58 : #if CONFIG_EXT_PARTITION
59 : 48, 48, 64
60 : #endif // CONFIG_EXT_PARTITION
61 : };
62 :
63 0 : static void fill_mode_costs(AV1_COMP *cpi) {
64 0 : const FRAME_CONTEXT *const fc = cpi->common.fc;
65 : int i, j;
66 :
67 0 : for (i = 0; i < INTRA_MODES; ++i)
68 0 : for (j = 0; j < INTRA_MODES; ++j)
69 0 : av1_cost_tokens(cpi->y_mode_costs[i][j], av1_kf_y_mode_prob[i][j],
70 : av1_intra_mode_tree);
71 :
72 0 : for (i = 0; i < BLOCK_SIZE_GROUPS; ++i)
73 0 : av1_cost_tokens(cpi->mbmode_cost[i], fc->y_mode_prob[i],
74 : av1_intra_mode_tree);
75 :
76 0 : for (i = 0; i < INTRA_MODES; ++i)
77 0 : av1_cost_tokens(cpi->intra_uv_mode_cost[i], fc->uv_mode_prob[i],
78 : av1_intra_mode_tree);
79 :
80 0 : for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; ++i)
81 0 : av1_cost_tokens(cpi->switchable_interp_costs[i],
82 0 : fc->switchable_interp_prob[i], av1_switchable_interp_tree);
83 :
84 : #if CONFIG_PALETTE
85 0 : for (i = 0; i < PALETTE_BLOCK_SIZES; ++i) {
86 0 : av1_cost_tokens(cpi->palette_y_size_cost[i],
87 0 : av1_default_palette_y_size_prob[i], av1_palette_size_tree);
88 0 : av1_cost_tokens(cpi->palette_uv_size_cost[i],
89 0 : av1_default_palette_uv_size_prob[i], av1_palette_size_tree);
90 : }
91 :
92 0 : for (i = 0; i < PALETTE_SIZES; ++i) {
93 0 : for (j = 0; j < PALETTE_COLOR_INDEX_CONTEXTS; ++j) {
94 0 : av1_cost_tokens(cpi->palette_y_color_cost[i][j],
95 0 : av1_default_palette_y_color_index_prob[i][j],
96 0 : av1_palette_color_index_tree[i]);
97 0 : av1_cost_tokens(cpi->palette_uv_color_cost[i][j],
98 0 : av1_default_palette_uv_color_index_prob[i][j],
99 0 : av1_palette_color_index_tree[i]);
100 : }
101 : }
102 : #endif // CONFIG_PALETTE
103 :
104 0 : for (i = 0; i < MAX_TX_DEPTH; ++i)
105 0 : for (j = 0; j < TX_SIZE_CONTEXTS; ++j)
106 0 : av1_cost_tokens(cpi->tx_size_cost[i][j], fc->tx_size_probs[i][j],
107 0 : av1_tx_size_tree[i]);
108 :
109 : #if CONFIG_EXT_TX
110 0 : for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
111 : int s;
112 0 : for (s = 1; s < EXT_TX_SETS_INTER; ++s) {
113 0 : if (use_inter_ext_tx_for_txsize[s][i]) {
114 0 : av1_cost_tokens(cpi->inter_tx_type_costs[s][i],
115 0 : fc->inter_ext_tx_prob[s][i], av1_ext_tx_inter_tree[s]);
116 : }
117 : }
118 0 : for (s = 1; s < EXT_TX_SETS_INTRA; ++s) {
119 0 : if (use_intra_ext_tx_for_txsize[s][i]) {
120 0 : for (j = 0; j < INTRA_MODES; ++j)
121 0 : av1_cost_tokens(cpi->intra_tx_type_costs[s][i][j],
122 0 : fc->intra_ext_tx_prob[s][i][j],
123 0 : av1_ext_tx_intra_tree[s]);
124 : }
125 : }
126 : }
127 : #else
128 : for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
129 : for (j = 0; j < TX_TYPES; ++j)
130 : av1_cost_tokens(cpi->intra_tx_type_costs[i][j],
131 : fc->intra_ext_tx_prob[i][j], av1_ext_tx_tree);
132 : }
133 : for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
134 : av1_cost_tokens(cpi->inter_tx_type_costs[i], fc->inter_ext_tx_prob[i],
135 : av1_ext_tx_tree);
136 : }
137 : #endif // CONFIG_EXT_TX
138 : #if CONFIG_EXT_INTRA
139 : #if CONFIG_INTRA_INTERP
140 : for (i = 0; i < INTRA_FILTERS + 1; ++i)
141 : av1_cost_tokens(cpi->intra_filter_cost[i], fc->intra_filter_probs[i],
142 : av1_intra_filter_tree);
143 : #endif // CONFIG_INTRA_INTERP
144 : #endif // CONFIG_EXT_INTRA
145 : #if CONFIG_LOOP_RESTORATION
146 : av1_cost_tokens(cpi->switchable_restore_cost, fc->switchable_restore_prob,
147 : av1_switchable_restore_tree);
148 : #endif // CONFIG_LOOP_RESTORATION
149 : #if CONFIG_GLOBAL_MOTION
150 0 : av1_cost_tokens(cpi->gmtype_cost, fc->global_motion_types_prob,
151 : av1_global_motion_types_tree);
152 : #endif // CONFIG_GLOBAL_MOTION
153 0 : }
154 :
155 0 : void av1_fill_token_costs(av1_coeff_cost *c,
156 : av1_coeff_probs_model (*p)[PLANE_TYPES]) {
157 : int i, j, k, l;
158 : TX_SIZE t;
159 0 : for (t = 0; t < TX_SIZES; ++t)
160 0 : for (i = 0; i < PLANE_TYPES; ++i)
161 0 : for (j = 0; j < REF_TYPES; ++j)
162 0 : for (k = 0; k < COEF_BANDS; ++k)
163 0 : for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l) {
164 : aom_prob probs[ENTROPY_NODES];
165 0 : av1_model_to_full_probs(p[t][i][j][k][l], probs);
166 0 : av1_cost_tokens((int *)c[t][i][j][k][0][l], probs, av1_coef_tree);
167 0 : av1_cost_tokens_skip((int *)c[t][i][j][k][1][l], probs,
168 : av1_coef_tree);
169 0 : assert(c[t][i][j][k][0][l][EOB_TOKEN] ==
170 : c[t][i][j][k][1][l][EOB_TOKEN]);
171 : }
172 0 : }
173 :
174 : // Values are now correlated to quantizer.
175 : static int sad_per_bit16lut_8[QINDEX_RANGE];
176 : static int sad_per_bit4lut_8[QINDEX_RANGE];
177 :
178 : #if CONFIG_HIGHBITDEPTH
179 : static int sad_per_bit16lut_10[QINDEX_RANGE];
180 : static int sad_per_bit4lut_10[QINDEX_RANGE];
181 : static int sad_per_bit16lut_12[QINDEX_RANGE];
182 : static int sad_per_bit4lut_12[QINDEX_RANGE];
183 : #endif
184 :
185 0 : static void init_me_luts_bd(int *bit16lut, int *bit4lut, int range,
186 : aom_bit_depth_t bit_depth) {
187 : int i;
188 : // Initialize the sad lut tables using a formulaic calculation for now.
189 : // This is to make it easier to resolve the impact of experimental changes
190 : // to the quantizer tables.
191 0 : for (i = 0; i < range; i++) {
192 0 : const double q = av1_convert_qindex_to_q(i, bit_depth);
193 0 : bit16lut[i] = (int)(0.0418 * q + 2.4107);
194 0 : bit4lut[i] = (int)(0.063 * q + 2.742);
195 : }
196 0 : }
197 :
198 0 : void av1_init_me_luts(void) {
199 0 : init_me_luts_bd(sad_per_bit16lut_8, sad_per_bit4lut_8, QINDEX_RANGE,
200 : AOM_BITS_8);
201 : #if CONFIG_HIGHBITDEPTH
202 0 : init_me_luts_bd(sad_per_bit16lut_10, sad_per_bit4lut_10, QINDEX_RANGE,
203 : AOM_BITS_10);
204 0 : init_me_luts_bd(sad_per_bit16lut_12, sad_per_bit4lut_12, QINDEX_RANGE,
205 : AOM_BITS_12);
206 : #endif
207 0 : }
208 :
209 : static const int rd_boost_factor[16] = { 64, 32, 32, 32, 24, 16, 12, 12,
210 : 8, 8, 4, 4, 2, 2, 1, 0 };
211 : static const int rd_frame_type_factor[FRAME_UPDATE_TYPES] = {
212 : 128, 144, 128, 128, 144,
213 : #if CONFIG_EXT_REFS
214 : // TODO(zoeliu): To adjust further following factor values.
215 : 128, 128, 128
216 : // TODO(weitinglin): We should investigate if the values should be the same
217 : // as the value used by OVERLAY frame
218 : ,
219 : 144
220 : #endif // CONFIG_EXT_REFS
221 : };
222 :
223 0 : int av1_compute_rd_mult(const AV1_COMP *cpi, int qindex) {
224 0 : const int64_t q = av1_dc_quant(qindex, 0, cpi->common.bit_depth);
225 : #if CONFIG_HIGHBITDEPTH
226 0 : int64_t rdmult = 0;
227 0 : switch (cpi->common.bit_depth) {
228 0 : case AOM_BITS_8: rdmult = 88 * q * q / 24; break;
229 0 : case AOM_BITS_10: rdmult = ROUND_POWER_OF_TWO(88 * q * q / 24, 4); break;
230 0 : case AOM_BITS_12: rdmult = ROUND_POWER_OF_TWO(88 * q * q / 24, 8); break;
231 : default:
232 0 : assert(0 && "bit_depth should be AOM_BITS_8, AOM_BITS_10 or AOM_BITS_12");
233 : return -1;
234 : }
235 : #else
236 : int64_t rdmult = 88 * q * q / 24;
237 : #endif // CONFIG_HIGHBITDEPTH
238 0 : if (cpi->oxcf.pass == 2 && (cpi->common.frame_type != KEY_FRAME)) {
239 0 : const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
240 0 : const FRAME_UPDATE_TYPE frame_type = gf_group->update_type[gf_group->index];
241 0 : const int boost_index = AOMMIN(15, (cpi->rc.gfu_boost / 100));
242 :
243 0 : rdmult = (rdmult * rd_frame_type_factor[frame_type]) >> 7;
244 0 : rdmult += ((rdmult * rd_boost_factor[boost_index]) >> 7);
245 : }
246 0 : if (rdmult < 1) rdmult = 1;
247 0 : return (int)rdmult;
248 : }
249 :
250 0 : static int compute_rd_thresh_factor(int qindex, aom_bit_depth_t bit_depth) {
251 : double q;
252 : #if CONFIG_HIGHBITDEPTH
253 0 : switch (bit_depth) {
254 0 : case AOM_BITS_8: q = av1_dc_quant(qindex, 0, AOM_BITS_8) / 4.0; break;
255 0 : case AOM_BITS_10: q = av1_dc_quant(qindex, 0, AOM_BITS_10) / 16.0; break;
256 0 : case AOM_BITS_12: q = av1_dc_quant(qindex, 0, AOM_BITS_12) / 64.0; break;
257 : default:
258 0 : assert(0 && "bit_depth should be AOM_BITS_8, AOM_BITS_10 or AOM_BITS_12");
259 : return -1;
260 : }
261 : #else
262 : (void)bit_depth;
263 : q = av1_dc_quant(qindex, 0, AOM_BITS_8) / 4.0;
264 : #endif // CONFIG_HIGHBITDEPTH
265 : // TODO(debargha): Adjust the function below.
266 0 : return AOMMAX((int)(pow(q, RD_THRESH_POW) * 5.12), 8);
267 : }
268 :
269 0 : void av1_initialize_me_consts(const AV1_COMP *cpi, MACROBLOCK *x, int qindex) {
270 : #if CONFIG_HIGHBITDEPTH
271 0 : switch (cpi->common.bit_depth) {
272 : case AOM_BITS_8:
273 0 : x->sadperbit16 = sad_per_bit16lut_8[qindex];
274 0 : x->sadperbit4 = sad_per_bit4lut_8[qindex];
275 0 : break;
276 : case AOM_BITS_10:
277 0 : x->sadperbit16 = sad_per_bit16lut_10[qindex];
278 0 : x->sadperbit4 = sad_per_bit4lut_10[qindex];
279 0 : break;
280 : case AOM_BITS_12:
281 0 : x->sadperbit16 = sad_per_bit16lut_12[qindex];
282 0 : x->sadperbit4 = sad_per_bit4lut_12[qindex];
283 0 : break;
284 : default:
285 0 : assert(0 && "bit_depth should be AOM_BITS_8, AOM_BITS_10 or AOM_BITS_12");
286 : }
287 : #else
288 : (void)cpi;
289 : x->sadperbit16 = sad_per_bit16lut_8[qindex];
290 : x->sadperbit4 = sad_per_bit4lut_8[qindex];
291 : #endif // CONFIG_HIGHBITDEPTH
292 0 : }
293 :
294 0 : static void set_block_thresholds(const AV1_COMMON *cm, RD_OPT *rd) {
295 : int i, bsize, segment_id;
296 :
297 0 : for (segment_id = 0; segment_id < MAX_SEGMENTS; ++segment_id) {
298 0 : const int qindex =
299 0 : clamp(av1_get_qindex(&cm->seg, segment_id, cm->base_qindex) +
300 0 : cm->y_dc_delta_q,
301 : 0, MAXQ);
302 0 : const int q = compute_rd_thresh_factor(qindex, cm->bit_depth);
303 :
304 0 : for (bsize = 0; bsize < BLOCK_SIZES; ++bsize) {
305 : // Threshold here seems unnecessarily harsh but fine given actual
306 : // range of values used for cpi->sf.thresh_mult[].
307 0 : const int t = q * rd_thresh_block_size_factor[bsize];
308 0 : const int thresh_max = INT_MAX / t;
309 :
310 : #if CONFIG_CB4X4
311 0 : for (i = 0; i < MAX_MODES; ++i)
312 0 : rd->threshes[segment_id][bsize][i] = rd->thresh_mult[i] < thresh_max
313 0 : ? rd->thresh_mult[i] * t / 4
314 0 : : INT_MAX;
315 : #else
316 : if (bsize >= BLOCK_8X8) {
317 : for (i = 0; i < MAX_MODES; ++i)
318 : rd->threshes[segment_id][bsize][i] = rd->thresh_mult[i] < thresh_max
319 : ? rd->thresh_mult[i] * t / 4
320 : : INT_MAX;
321 : } else {
322 : for (i = 0; i < MAX_REFS; ++i)
323 : rd->threshes[segment_id][bsize][i] =
324 : rd->thresh_mult_sub8x8[i] < thresh_max
325 : ? rd->thresh_mult_sub8x8[i] * t / 4
326 : : INT_MAX;
327 : }
328 : #endif
329 : }
330 : }
331 0 : }
332 :
333 0 : void av1_set_mvcost(MACROBLOCK *x, MV_REFERENCE_FRAME ref_frame, int ref,
334 : int ref_mv_idx) {
335 0 : MB_MODE_INFO_EXT *mbmi_ext = x->mbmi_ext;
336 0 : int8_t rf_type = av1_ref_frame_type(x->e_mbd.mi[0]->mbmi.ref_frame);
337 0 : int nmv_ctx = av1_nmv_ctx(mbmi_ext->ref_mv_count[rf_type],
338 0 : mbmi_ext->ref_mv_stack[rf_type], ref, ref_mv_idx);
339 : (void)ref_frame;
340 0 : x->mvcost = x->mv_cost_stack[nmv_ctx];
341 0 : x->nmvjointcost = x->nmv_vec_cost[nmv_ctx];
342 0 : }
343 :
344 0 : void av1_initialize_rd_consts(AV1_COMP *cpi) {
345 0 : AV1_COMMON *const cm = &cpi->common;
346 0 : MACROBLOCK *const x = &cpi->td.mb;
347 0 : RD_OPT *const rd = &cpi->rd;
348 : int i;
349 : int nmv_ctx;
350 :
351 0 : aom_clear_system_state();
352 :
353 0 : rd->RDDIV = RDDIV_BITS; // In bits (to multiply D by 128).
354 0 : rd->RDMULT = av1_compute_rd_mult(cpi, cm->base_qindex + cm->y_dc_delta_q);
355 :
356 0 : set_error_per_bit(x, rd->RDMULT);
357 :
358 0 : set_block_thresholds(cm, rd);
359 :
360 0 : for (nmv_ctx = 0; nmv_ctx < NMV_CONTEXTS; ++nmv_ctx) {
361 0 : av1_build_nmv_cost_table(
362 0 : x->nmv_vec_cost[nmv_ctx],
363 0 : cm->allow_high_precision_mv ? x->nmvcost_hp[nmv_ctx]
364 : : x->nmvcost[nmv_ctx],
365 0 : &cm->fc->nmvc[nmv_ctx], cm->allow_high_precision_mv);
366 : }
367 0 : x->mvcost = x->mv_cost_stack[0];
368 0 : x->nmvjointcost = x->nmv_vec_cost[0];
369 :
370 0 : if (cpi->oxcf.pass != 1) {
371 0 : av1_fill_token_costs(x->token_costs, cm->fc->coef_probs);
372 :
373 0 : if (cm->frame_type == KEY_FRAME) {
374 : #if CONFIG_EXT_PARTITION_TYPES
375 : for (i = 0; i < PARTITION_PLOFFSET; ++i)
376 : av1_cost_tokens(cpi->partition_cost[i], cm->fc->partition_prob[i],
377 : av1_partition_tree);
378 : for (; i < PARTITION_CONTEXTS_PRIMARY; ++i)
379 : av1_cost_tokens(cpi->partition_cost[i], cm->fc->partition_prob[i],
380 : av1_ext_partition_tree);
381 : #else
382 0 : for (i = 0; i < PARTITION_CONTEXTS_PRIMARY; ++i)
383 0 : av1_cost_tokens(cpi->partition_cost[i], cm->fc->partition_prob[i],
384 : av1_partition_tree);
385 : #endif // CONFIG_EXT_PARTITION_TYPES
386 : #if CONFIG_UNPOISON_PARTITION_CTX
387 : for (; i < PARTITION_CONTEXTS_PRIMARY + PARTITION_BLOCK_SIZES; ++i) {
388 : aom_prob p = cm->fc->partition_prob[i][PARTITION_VERT];
389 : assert(p > 0);
390 : cpi->partition_cost[i][PARTITION_NONE] = INT_MAX;
391 : cpi->partition_cost[i][PARTITION_HORZ] = INT_MAX;
392 : cpi->partition_cost[i][PARTITION_VERT] = av1_cost_bit(p, 0);
393 : cpi->partition_cost[i][PARTITION_SPLIT] = av1_cost_bit(p, 1);
394 : }
395 : for (; i < PARTITION_CONTEXTS_PRIMARY + 2 * PARTITION_BLOCK_SIZES; ++i) {
396 : aom_prob p = cm->fc->partition_prob[i][PARTITION_HORZ];
397 : assert(p > 0);
398 : cpi->partition_cost[i][PARTITION_NONE] = INT_MAX;
399 : cpi->partition_cost[i][PARTITION_HORZ] = av1_cost_bit(p, 0);
400 : cpi->partition_cost[i][PARTITION_VERT] = INT_MAX;
401 : cpi->partition_cost[i][PARTITION_SPLIT] = av1_cost_bit(p, 1);
402 : }
403 : cpi->partition_cost[PARTITION_CONTEXTS][PARTITION_NONE] = INT_MAX;
404 : cpi->partition_cost[PARTITION_CONTEXTS][PARTITION_HORZ] = INT_MAX;
405 : cpi->partition_cost[PARTITION_CONTEXTS][PARTITION_VERT] = INT_MAX;
406 : cpi->partition_cost[PARTITION_CONTEXTS][PARTITION_SPLIT] = 0;
407 : #endif // CONFIG_UNPOISON_PARTITION_CTX
408 : }
409 :
410 0 : fill_mode_costs(cpi);
411 :
412 0 : if (!frame_is_intra_only(cm)) {
413 0 : for (i = 0; i < NEWMV_MODE_CONTEXTS; ++i) {
414 0 : cpi->newmv_mode_cost[i][0] = av1_cost_bit(cm->fc->newmv_prob[i], 0);
415 0 : cpi->newmv_mode_cost[i][1] = av1_cost_bit(cm->fc->newmv_prob[i], 1);
416 : }
417 :
418 0 : for (i = 0; i < ZEROMV_MODE_CONTEXTS; ++i) {
419 0 : cpi->zeromv_mode_cost[i][0] = av1_cost_bit(cm->fc->zeromv_prob[i], 0);
420 0 : cpi->zeromv_mode_cost[i][1] = av1_cost_bit(cm->fc->zeromv_prob[i], 1);
421 : }
422 :
423 0 : for (i = 0; i < REFMV_MODE_CONTEXTS; ++i) {
424 0 : cpi->refmv_mode_cost[i][0] = av1_cost_bit(cm->fc->refmv_prob[i], 0);
425 0 : cpi->refmv_mode_cost[i][1] = av1_cost_bit(cm->fc->refmv_prob[i], 1);
426 : }
427 :
428 0 : for (i = 0; i < DRL_MODE_CONTEXTS; ++i) {
429 0 : cpi->drl_mode_cost0[i][0] = av1_cost_bit(cm->fc->drl_prob[i], 0);
430 0 : cpi->drl_mode_cost0[i][1] = av1_cost_bit(cm->fc->drl_prob[i], 1);
431 : }
432 : #if CONFIG_EXT_INTER
433 0 : for (i = 0; i < INTER_MODE_CONTEXTS; ++i)
434 0 : av1_cost_tokens((int *)cpi->inter_compound_mode_cost[i],
435 0 : cm->fc->inter_compound_mode_probs[i],
436 : av1_inter_compound_mode_tree);
437 : #if CONFIG_INTERINTRA
438 0 : for (i = 0; i < BLOCK_SIZE_GROUPS; ++i)
439 0 : av1_cost_tokens((int *)cpi->interintra_mode_cost[i],
440 0 : cm->fc->interintra_mode_prob[i],
441 : av1_interintra_mode_tree);
442 : #endif // CONFIG_INTERINTRA
443 : #endif // CONFIG_EXT_INTER
444 : #if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
445 0 : for (i = BLOCK_8X8; i < BLOCK_SIZES; i++) {
446 0 : av1_cost_tokens((int *)cpi->motion_mode_cost[i],
447 0 : cm->fc->motion_mode_prob[i], av1_motion_mode_tree);
448 : }
449 : #if CONFIG_MOTION_VAR && CONFIG_WARPED_MOTION
450 0 : for (i = BLOCK_8X8; i < BLOCK_SIZES; i++) {
451 0 : cpi->motion_mode_cost1[i][0] = av1_cost_bit(cm->fc->obmc_prob[i], 0);
452 0 : cpi->motion_mode_cost1[i][1] = av1_cost_bit(cm->fc->obmc_prob[i], 1);
453 : }
454 : #endif // CONFIG_MOTION_VAR && CONFIG_WARPED_MOTION
455 : #endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
456 : }
457 : }
458 0 : }
459 :
460 0 : static void model_rd_norm(int xsq_q10, int *r_q10, int *d_q10) {
461 : // NOTE: The tables below must be of the same size.
462 :
463 : // The functions described below are sampled at the four most significant
464 : // bits of x^2 + 8 / 256.
465 :
466 : // Normalized rate:
467 : // This table models the rate for a Laplacian source with given variance
468 : // when quantized with a uniform quantizer with given stepsize. The
469 : // closed form expression is:
470 : // Rn(x) = H(sqrt(r)) + sqrt(r)*[1 + H(r)/(1 - r)],
471 : // where r = exp(-sqrt(2) * x) and x = qpstep / sqrt(variance),
472 : // and H(x) is the binary entropy function.
473 : static const int rate_tab_q10[] = {
474 : 65536, 6086, 5574, 5275, 5063, 4899, 4764, 4651, 4553, 4389, 4255, 4142,
475 : 4044, 3958, 3881, 3811, 3748, 3635, 3538, 3453, 3376, 3307, 3244, 3186,
476 : 3133, 3037, 2952, 2877, 2809, 2747, 2690, 2638, 2589, 2501, 2423, 2353,
477 : 2290, 2232, 2179, 2130, 2084, 2001, 1928, 1862, 1802, 1748, 1698, 1651,
478 : 1608, 1530, 1460, 1398, 1342, 1290, 1243, 1199, 1159, 1086, 1021, 963,
479 : 911, 864, 821, 781, 745, 680, 623, 574, 530, 490, 455, 424,
480 : 395, 345, 304, 269, 239, 213, 190, 171, 154, 126, 104, 87,
481 : 73, 61, 52, 44, 38, 28, 21, 16, 12, 10, 8, 6,
482 : 5, 3, 2, 1, 1, 1, 0, 0,
483 : };
484 : // Normalized distortion:
485 : // This table models the normalized distortion for a Laplacian source
486 : // with given variance when quantized with a uniform quantizer
487 : // with given stepsize. The closed form expression is:
488 : // Dn(x) = 1 - 1/sqrt(2) * x / sinh(x/sqrt(2))
489 : // where x = qpstep / sqrt(variance).
490 : // Note the actual distortion is Dn * variance.
491 : static const int dist_tab_q10[] = {
492 : 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 4, 5,
493 : 5, 6, 7, 7, 8, 9, 11, 12, 13, 15, 16, 17,
494 : 18, 21, 24, 26, 29, 31, 34, 36, 39, 44, 49, 54,
495 : 59, 64, 69, 73, 78, 88, 97, 106, 115, 124, 133, 142,
496 : 151, 167, 184, 200, 215, 231, 245, 260, 274, 301, 327, 351,
497 : 375, 397, 418, 439, 458, 495, 528, 559, 587, 613, 637, 659,
498 : 680, 717, 749, 777, 801, 823, 842, 859, 874, 899, 919, 936,
499 : 949, 960, 969, 977, 983, 994, 1001, 1006, 1010, 1013, 1015, 1017,
500 : 1018, 1020, 1022, 1022, 1023, 1023, 1023, 1024,
501 : };
502 : static const int xsq_iq_q10[] = {
503 : 0, 4, 8, 12, 16, 20, 24, 28, 32,
504 : 40, 48, 56, 64, 72, 80, 88, 96, 112,
505 : 128, 144, 160, 176, 192, 208, 224, 256, 288,
506 : 320, 352, 384, 416, 448, 480, 544, 608, 672,
507 : 736, 800, 864, 928, 992, 1120, 1248, 1376, 1504,
508 : 1632, 1760, 1888, 2016, 2272, 2528, 2784, 3040, 3296,
509 : 3552, 3808, 4064, 4576, 5088, 5600, 6112, 6624, 7136,
510 : 7648, 8160, 9184, 10208, 11232, 12256, 13280, 14304, 15328,
511 : 16352, 18400, 20448, 22496, 24544, 26592, 28640, 30688, 32736,
512 : 36832, 40928, 45024, 49120, 53216, 57312, 61408, 65504, 73696,
513 : 81888, 90080, 98272, 106464, 114656, 122848, 131040, 147424, 163808,
514 : 180192, 196576, 212960, 229344, 245728,
515 : };
516 0 : const int tmp = (xsq_q10 >> 2) + 8;
517 0 : const int k = get_msb(tmp) - 3;
518 0 : const int xq = (k << 3) + ((tmp >> k) & 0x7);
519 0 : const int one_q10 = 1 << 10;
520 0 : const int a_q10 = ((xsq_q10 - xsq_iq_q10[xq]) << 10) >> (2 + k);
521 0 : const int b_q10 = one_q10 - a_q10;
522 0 : *r_q10 = (rate_tab_q10[xq] * b_q10 + rate_tab_q10[xq + 1] * a_q10) >> 10;
523 0 : *d_q10 = (dist_tab_q10[xq] * b_q10 + dist_tab_q10[xq + 1] * a_q10) >> 10;
524 0 : }
525 :
526 0 : void av1_model_rd_from_var_lapndz(int64_t var, unsigned int n_log2,
527 : unsigned int qstep, int *rate,
528 : int64_t *dist) {
529 : // This function models the rate and distortion for a Laplacian
530 : // source with given variance when quantized with a uniform quantizer
531 : // with given stepsize. The closed form expressions are in:
532 : // Hang and Chen, "Source Model for transform video coder and its
533 : // application - Part I: Fundamental Theory", IEEE Trans. Circ.
534 : // Sys. for Video Tech., April 1997.
535 0 : if (var == 0) {
536 0 : *rate = 0;
537 0 : *dist = 0;
538 : } else {
539 : int d_q10, r_q10;
540 : static const uint32_t MAX_XSQ_Q10 = 245727;
541 0 : const uint64_t xsq_q10_64 =
542 0 : (((uint64_t)qstep * qstep << (n_log2 + 10)) + (var >> 1)) / var;
543 0 : const int xsq_q10 = (int)AOMMIN(xsq_q10_64, MAX_XSQ_Q10);
544 0 : model_rd_norm(xsq_q10, &r_q10, &d_q10);
545 0 : *rate = ROUND_POWER_OF_TWO(r_q10 << n_log2, 10 - AV1_PROB_COST_SHIFT);
546 0 : *dist = (var * (int64_t)d_q10 + 512) >> 10;
547 : }
548 0 : }
549 :
550 0 : static void get_entropy_contexts_plane(
551 : BLOCK_SIZE plane_bsize, TX_SIZE tx_size, const struct macroblockd_plane *pd,
552 : ENTROPY_CONTEXT t_above[2 * MAX_MIB_SIZE],
553 : ENTROPY_CONTEXT t_left[2 * MAX_MIB_SIZE]) {
554 0 : const int num_4x4_w = block_size_wide[plane_bsize] >> tx_size_wide_log2[0];
555 0 : const int num_4x4_h = block_size_high[plane_bsize] >> tx_size_high_log2[0];
556 0 : const ENTROPY_CONTEXT *const above = pd->above_context;
557 0 : const ENTROPY_CONTEXT *const left = pd->left_context;
558 :
559 : #if CONFIG_LV_MAP
560 : memcpy(t_above, above, sizeof(ENTROPY_CONTEXT) * num_4x4_w);
561 : memcpy(t_left, left, sizeof(ENTROPY_CONTEXT) * num_4x4_h);
562 : return;
563 : #endif // CONFIG_LV_MAP
564 :
565 : int i;
566 :
567 : #if CONFIG_CHROMA_2X2
568 : switch (tx_size) {
569 : case TX_2X2:
570 : memcpy(t_above, above, sizeof(ENTROPY_CONTEXT) * num_4x4_w);
571 : memcpy(t_left, left, sizeof(ENTROPY_CONTEXT) * num_4x4_h);
572 : break;
573 : case TX_4X4:
574 : for (i = 0; i < num_4x4_w; i += 2)
575 : t_above[i] = !!*(const uint16_t *)&above[i];
576 : for (i = 0; i < num_4x4_h; i += 2)
577 : t_left[i] = !!*(const uint16_t *)&left[i];
578 : break;
579 : case TX_8X8:
580 : for (i = 0; i < num_4x4_w; i += 4)
581 : t_above[i] = !!*(const uint32_t *)&above[i];
582 : for (i = 0; i < num_4x4_h; i += 4)
583 : t_left[i] = !!*(const uint32_t *)&left[i];
584 : break;
585 : case TX_16X16:
586 : for (i = 0; i < num_4x4_w; i += 8)
587 : t_above[i] = !!*(const uint64_t *)&above[i];
588 : for (i = 0; i < num_4x4_h; i += 8)
589 : t_left[i] = !!*(const uint64_t *)&left[i];
590 : break;
591 : case TX_32X32:
592 : for (i = 0; i < num_4x4_w; i += 16)
593 : t_above[i] =
594 : !!(*(const uint64_t *)&above[i] | *(const uint64_t *)&above[i + 8]);
595 : for (i = 0; i < num_4x4_h; i += 16)
596 : t_left[i] =
597 : !!(*(const uint64_t *)&left[i] | *(const uint64_t *)&left[i + 8]);
598 : break;
599 : #if CONFIG_TX64X64
600 : case TX_64X64:
601 : for (i = 0; i < num_4x4_w; i += 32)
602 : t_above[i] =
603 : !!(*(const uint64_t *)&above[i] | *(const uint64_t *)&above[i + 8] |
604 : *(const uint64_t *)&above[i + 16] |
605 : *(const uint64_t *)&above[i + 24]);
606 : for (i = 0; i < num_4x4_h; i += 32)
607 : t_left[i] =
608 : !!(*(const uint64_t *)&left[i] | *(const uint64_t *)&left[i + 8] |
609 : *(const uint64_t *)&left[i + 16] |
610 : *(const uint64_t *)&left[i + 24]);
611 : break;
612 : #endif // CONFIG_TX64X64
613 : case TX_4X8:
614 : for (i = 0; i < num_4x4_w; i += 2)
615 : t_above[i] = !!*(const uint16_t *)&above[i];
616 : for (i = 0; i < num_4x4_h; i += 4)
617 : t_left[i] = !!*(const uint32_t *)&left[i];
618 : break;
619 : case TX_8X4:
620 : for (i = 0; i < num_4x4_w; i += 4)
621 : t_above[i] = !!*(const uint32_t *)&above[i];
622 : for (i = 0; i < num_4x4_h; i += 2)
623 : t_left[i] = !!*(const uint16_t *)&left[i];
624 : break;
625 : case TX_8X16:
626 : for (i = 0; i < num_4x4_w; i += 4)
627 : t_above[i] = !!*(const uint32_t *)&above[i];
628 : for (i = 0; i < num_4x4_h; i += 8)
629 : t_left[i] = !!*(const uint64_t *)&left[i];
630 : break;
631 : case TX_16X8:
632 : for (i = 0; i < num_4x4_w; i += 8)
633 : t_above[i] = !!*(const uint64_t *)&above[i];
634 : for (i = 0; i < num_4x4_h; i += 4)
635 : t_left[i] = !!*(const uint32_t *)&left[i];
636 : break;
637 : case TX_16X32:
638 : for (i = 0; i < num_4x4_w; i += 8)
639 : t_above[i] = !!*(const uint64_t *)&above[i];
640 : for (i = 0; i < num_4x4_h; i += 16)
641 : t_left[i] =
642 : !!(*(const uint64_t *)&left[i] | *(const uint64_t *)&left[i + 8]);
643 : break;
644 : case TX_32X16:
645 : for (i = 0; i < num_4x4_w; i += 16)
646 : t_above[i] =
647 : !!(*(const uint64_t *)&above[i] | *(const uint64_t *)&above[i + 8]);
648 : for (i = 0; i < num_4x4_h; i += 8)
649 : t_left[i] = !!*(const uint64_t *)&left[i];
650 : break;
651 : #if CONFIG_EXT_TX && CONFIG_RECT_TX && CONFIG_RECT_TX_EXT
652 : case TX_4X16:
653 : for (i = 0; i < num_4x4_w; i += 2)
654 : t_above[i] = !!*(const uint16_t *)&above[i];
655 : for (i = 0; i < num_4x4_h; i += 8)
656 : t_left[i] = !!*(const uint64_t *)&left[i];
657 : break;
658 : case TX_16X4:
659 : for (i = 0; i < num_4x4_w; i += 8)
660 : t_above[i] = !!*(const uint64_t *)&above[i];
661 : for (i = 0; i < num_4x4_h; i += 2)
662 : t_left[i] = !!*(const uint16_t *)&left[i];
663 : break;
664 : case TX_8X32:
665 : for (i = 0; i < num_4x4_w; i += 4)
666 : t_above[i] = !!*(const uint32_t *)&above[i];
667 : for (i = 0; i < num_4x4_h; i += 16)
668 : t_left[i] =
669 : !!(*(const uint64_t *)&left[i] | *(const uint64_t *)&left[i + 8]);
670 : break;
671 : case TX_32X8:
672 : for (i = 0; i < num_4x4_w; i += 16)
673 : t_above[i] =
674 : !!(*(const uint64_t *)&above[i] | *(const uint64_t *)&above[i + 8]);
675 : for (i = 0; i < num_4x4_h; i += 4)
676 : t_left[i] = !!*(const uint32_t *)&left[i];
677 : break;
678 : #endif // CONFIG_EXT_TX && CONFIG_RECT_TX && CONFIG_RECT_TX_EXT
679 :
680 : default: assert(0 && "Invalid transform size."); break;
681 : }
682 : return;
683 : #endif // CONFIG_CHROMA_2X2
684 :
685 0 : switch (tx_size) {
686 : case TX_4X4:
687 0 : memcpy(t_above, above, sizeof(ENTROPY_CONTEXT) * num_4x4_w);
688 0 : memcpy(t_left, left, sizeof(ENTROPY_CONTEXT) * num_4x4_h);
689 0 : break;
690 : case TX_8X8:
691 0 : for (i = 0; i < num_4x4_w; i += 2)
692 0 : t_above[i] = !!*(const uint16_t *)&above[i];
693 0 : for (i = 0; i < num_4x4_h; i += 2)
694 0 : t_left[i] = !!*(const uint16_t *)&left[i];
695 0 : break;
696 : case TX_16X16:
697 0 : for (i = 0; i < num_4x4_w; i += 4)
698 0 : t_above[i] = !!*(const uint32_t *)&above[i];
699 0 : for (i = 0; i < num_4x4_h; i += 4)
700 0 : t_left[i] = !!*(const uint32_t *)&left[i];
701 0 : break;
702 : case TX_32X32:
703 0 : for (i = 0; i < num_4x4_w; i += 8)
704 0 : t_above[i] = !!*(const uint64_t *)&above[i];
705 0 : for (i = 0; i < num_4x4_h; i += 8)
706 0 : t_left[i] = !!*(const uint64_t *)&left[i];
707 0 : break;
708 : #if CONFIG_TX64X64
709 : case TX_64X64:
710 : for (i = 0; i < num_4x4_w; i += 16)
711 : t_above[i] =
712 : !!(*(const uint64_t *)&above[i] | *(const uint64_t *)&above[i + 8]);
713 : for (i = 0; i < num_4x4_h; i += 16)
714 : t_left[i] =
715 : !!(*(const uint64_t *)&left[i] | *(const uint64_t *)&left[i + 8]);
716 : break;
717 : #endif // CONFIG_TX64X64
718 : case TX_4X8:
719 0 : memcpy(t_above, above, sizeof(ENTROPY_CONTEXT) * num_4x4_w);
720 0 : for (i = 0; i < num_4x4_h; i += 2)
721 0 : t_left[i] = !!*(const uint16_t *)&left[i];
722 0 : break;
723 : case TX_8X4:
724 0 : for (i = 0; i < num_4x4_w; i += 2)
725 0 : t_above[i] = !!*(const uint16_t *)&above[i];
726 0 : memcpy(t_left, left, sizeof(ENTROPY_CONTEXT) * num_4x4_h);
727 0 : break;
728 : case TX_8X16:
729 0 : for (i = 0; i < num_4x4_w; i += 2)
730 0 : t_above[i] = !!*(const uint16_t *)&above[i];
731 0 : for (i = 0; i < num_4x4_h; i += 4)
732 0 : t_left[i] = !!*(const uint32_t *)&left[i];
733 0 : break;
734 : case TX_16X8:
735 0 : for (i = 0; i < num_4x4_w; i += 4)
736 0 : t_above[i] = !!*(const uint32_t *)&above[i];
737 0 : for (i = 0; i < num_4x4_h; i += 2)
738 0 : t_left[i] = !!*(const uint16_t *)&left[i];
739 0 : break;
740 : case TX_16X32:
741 0 : for (i = 0; i < num_4x4_w; i += 4)
742 0 : t_above[i] = !!*(const uint32_t *)&above[i];
743 0 : for (i = 0; i < num_4x4_h; i += 8)
744 0 : t_left[i] = !!*(const uint64_t *)&left[i];
745 0 : break;
746 : case TX_32X16:
747 0 : for (i = 0; i < num_4x4_w; i += 8)
748 0 : t_above[i] = !!*(const uint64_t *)&above[i];
749 0 : for (i = 0; i < num_4x4_h; i += 4)
750 0 : t_left[i] = !!*(const uint32_t *)&left[i];
751 0 : break;
752 : #if CONFIG_EXT_TX && CONFIG_RECT_TX && CONFIG_RECT_TX_EXT
753 : case TX_4X16:
754 : memcpy(t_above, above, sizeof(ENTROPY_CONTEXT) * num_4x4_w);
755 : for (i = 0; i < num_4x4_h; i += 4)
756 : t_left[i] = !!*(const uint32_t *)&left[i];
757 : break;
758 : case TX_16X4:
759 : for (i = 0; i < num_4x4_w; i += 4)
760 : t_above[i] = !!*(const uint32_t *)&above[i];
761 : memcpy(t_left, left, sizeof(ENTROPY_CONTEXT) * num_4x4_h);
762 : break;
763 : case TX_8X32:
764 : for (i = 0; i < num_4x4_w; i += 2)
765 : t_above[i] = !!*(const uint16_t *)&above[i];
766 : for (i = 0; i < num_4x4_h; i += 8)
767 : t_left[i] = !!*(const uint64_t *)&left[i];
768 : break;
769 : case TX_32X8:
770 : for (i = 0; i < num_4x4_w; i += 8)
771 : t_above[i] = !!*(const uint64_t *)&above[i];
772 : for (i = 0; i < num_4x4_h; i += 2)
773 : t_left[i] = !!*(const uint16_t *)&left[i];
774 : break;
775 : #endif // CONFIG_EXT_TX && CONFIG_RECT_TX && CONFIG_RECT_TX_EXT
776 0 : default: assert(0 && "Invalid transform size."); break;
777 : }
778 0 : }
779 :
780 0 : void av1_get_entropy_contexts(BLOCK_SIZE bsize, TX_SIZE tx_size,
781 : const struct macroblockd_plane *pd,
782 : ENTROPY_CONTEXT t_above[2 * MAX_MIB_SIZE],
783 : ENTROPY_CONTEXT t_left[2 * MAX_MIB_SIZE]) {
784 : #if CONFIG_CB4X4 && !CONFIG_CHROMA_2X2
785 0 : const BLOCK_SIZE plane_bsize =
786 0 : AOMMAX(BLOCK_4X4, get_plane_block_size(bsize, pd));
787 : #else
788 : const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
789 : #endif
790 0 : get_entropy_contexts_plane(plane_bsize, tx_size, pd, t_above, t_left);
791 0 : }
792 :
793 0 : void av1_mv_pred(const AV1_COMP *cpi, MACROBLOCK *x, uint8_t *ref_y_buffer,
794 : int ref_y_stride, int ref_frame, BLOCK_SIZE block_size) {
795 : int i;
796 0 : int zero_seen = 0;
797 0 : int best_index = 0;
798 0 : int best_sad = INT_MAX;
799 0 : int this_sad = INT_MAX;
800 0 : int max_mv = 0;
801 0 : uint8_t *src_y_ptr = x->plane[0].src.buf;
802 : uint8_t *ref_y_ptr;
803 : MV pred_mv[MAX_MV_REF_CANDIDATES + 1];
804 0 : int num_mv_refs = 0;
805 :
806 0 : pred_mv[num_mv_refs++] = x->mbmi_ext->ref_mvs[ref_frame][0].as_mv;
807 0 : if (x->mbmi_ext->ref_mvs[ref_frame][0].as_int !=
808 0 : x->mbmi_ext->ref_mvs[ref_frame][1].as_int) {
809 0 : pred_mv[num_mv_refs++] = x->mbmi_ext->ref_mvs[ref_frame][1].as_mv;
810 : }
811 0 : if (cpi->sf.adaptive_motion_search && block_size < x->max_partition_size)
812 0 : pred_mv[num_mv_refs++] = x->pred_mv[ref_frame];
813 :
814 0 : assert(num_mv_refs <= (int)(sizeof(pred_mv) / sizeof(pred_mv[0])));
815 :
816 : // Get the sad for each candidate reference mv.
817 0 : for (i = 0; i < num_mv_refs; ++i) {
818 0 : const MV *this_mv = &pred_mv[i];
819 : int fp_row, fp_col;
820 0 : fp_row = (this_mv->row + 3 + (this_mv->row >= 0)) >> 3;
821 0 : fp_col = (this_mv->col + 3 + (this_mv->col >= 0)) >> 3;
822 0 : max_mv = AOMMAX(max_mv, AOMMAX(abs(this_mv->row), abs(this_mv->col)) >> 3);
823 :
824 0 : if (fp_row == 0 && fp_col == 0 && zero_seen) continue;
825 0 : zero_seen |= (fp_row == 0 && fp_col == 0);
826 :
827 0 : ref_y_ptr = &ref_y_buffer[ref_y_stride * fp_row + fp_col];
828 : // Find sad for current vector.
829 0 : this_sad = cpi->fn_ptr[block_size].sdf(src_y_ptr, x->plane[0].src.stride,
830 : ref_y_ptr, ref_y_stride);
831 : // Note if it is the best so far.
832 0 : if (this_sad < best_sad) {
833 0 : best_sad = this_sad;
834 0 : best_index = i;
835 : }
836 : }
837 :
838 : // Note the index of the mv that worked best in the reference list.
839 0 : x->mv_best_ref_index[ref_frame] = best_index;
840 0 : x->max_mv_context[ref_frame] = max_mv;
841 0 : x->pred_mv_sad[ref_frame] = best_sad;
842 0 : }
843 :
844 0 : void av1_setup_pred_block(const MACROBLOCKD *xd,
845 : struct buf_2d dst[MAX_MB_PLANE],
846 : const YV12_BUFFER_CONFIG *src, int mi_row, int mi_col,
847 : const struct scale_factors *scale,
848 : const struct scale_factors *scale_uv) {
849 : int i;
850 :
851 0 : dst[0].buf = src->y_buffer;
852 0 : dst[0].stride = src->y_stride;
853 0 : dst[1].buf = src->u_buffer;
854 0 : dst[2].buf = src->v_buffer;
855 0 : dst[1].stride = dst[2].stride = src->uv_stride;
856 :
857 0 : for (i = 0; i < MAX_MB_PLANE; ++i) {
858 0 : setup_pred_plane(dst + i, xd->mi[0]->mbmi.sb_type, dst[i].buf,
859 : i ? src->uv_crop_width : src->y_crop_width,
860 : i ? src->uv_crop_height : src->y_crop_height,
861 0 : dst[i].stride, mi_row, mi_col, i ? scale_uv : scale,
862 : xd->plane[i].subsampling_x, xd->plane[i].subsampling_y);
863 : }
864 0 : }
865 :
866 0 : int av1_raster_block_offset(BLOCK_SIZE plane_bsize, int raster_block,
867 : int stride) {
868 0 : const int bw = b_width_log2_lookup[plane_bsize];
869 0 : const int y = 4 * (raster_block >> bw);
870 0 : const int x = 4 * (raster_block & ((1 << bw) - 1));
871 0 : return y * stride + x;
872 : }
873 :
874 0 : int16_t *av1_raster_block_offset_int16(BLOCK_SIZE plane_bsize, int raster_block,
875 : int16_t *base) {
876 0 : const int stride = block_size_wide[plane_bsize];
877 0 : return base + av1_raster_block_offset(plane_bsize, raster_block, stride);
878 : }
879 :
880 0 : YV12_BUFFER_CONFIG *av1_get_scaled_ref_frame(const AV1_COMP *cpi,
881 : int ref_frame) {
882 0 : const AV1_COMMON *const cm = &cpi->common;
883 0 : const int scaled_idx = cpi->scaled_ref_idx[ref_frame - 1];
884 0 : const int ref_idx = get_ref_frame_buf_idx(cpi, ref_frame);
885 0 : return (scaled_idx != ref_idx && scaled_idx != INVALID_IDX)
886 0 : ? &cm->buffer_pool->frame_bufs[scaled_idx].buf
887 0 : : NULL;
888 : }
889 :
890 : #if CONFIG_DUAL_FILTER
891 0 : int av1_get_switchable_rate(const AV1_COMP *cpi, const MACROBLOCKD *xd) {
892 0 : const AV1_COMMON *const cm = &cpi->common;
893 0 : if (cm->interp_filter == SWITCHABLE) {
894 0 : const MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
895 0 : int inter_filter_cost = 0;
896 : int dir;
897 :
898 0 : for (dir = 0; dir < 2; ++dir) {
899 0 : if (has_subpel_mv_component(xd->mi[0], xd, dir) ||
900 0 : (mbmi->ref_frame[1] > INTRA_FRAME &&
901 0 : has_subpel_mv_component(xd->mi[0], xd, dir + 2))) {
902 0 : const int ctx = av1_get_pred_context_switchable_interp(xd, dir);
903 0 : inter_filter_cost +=
904 0 : cpi->switchable_interp_costs[ctx][mbmi->interp_filter[dir]];
905 : }
906 : }
907 0 : return SWITCHABLE_INTERP_RATE_FACTOR * inter_filter_cost;
908 : } else {
909 0 : return 0;
910 : }
911 : }
912 : #else
913 : int av1_get_switchable_rate(const AV1_COMP *cpi, const MACROBLOCKD *xd) {
914 : const AV1_COMMON *const cm = &cpi->common;
915 : if (cm->interp_filter == SWITCHABLE) {
916 : const MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
917 : const int ctx = av1_get_pred_context_switchable_interp(xd);
918 : return SWITCHABLE_INTERP_RATE_FACTOR *
919 : cpi->switchable_interp_costs[ctx][mbmi->interp_filter];
920 : }
921 : return 0;
922 : }
923 : #endif
924 :
925 0 : void av1_set_rd_speed_thresholds(AV1_COMP *cpi) {
926 : int i;
927 0 : RD_OPT *const rd = &cpi->rd;
928 0 : SPEED_FEATURES *const sf = &cpi->sf;
929 :
930 : // Set baseline threshold values.
931 0 : for (i = 0; i < MAX_MODES; ++i) rd->thresh_mult[i] = cpi->oxcf.mode == 0;
932 :
933 0 : if (sf->adaptive_rd_thresh) {
934 0 : rd->thresh_mult[THR_NEARESTMV] = 300;
935 : #if CONFIG_EXT_REFS
936 0 : rd->thresh_mult[THR_NEARESTL2] = 300;
937 0 : rd->thresh_mult[THR_NEARESTL3] = 300;
938 0 : rd->thresh_mult[THR_NEARESTB] = 300;
939 : #endif // CONFIG_EXT_REFS
940 0 : rd->thresh_mult[THR_NEARESTA] = 300;
941 0 : rd->thresh_mult[THR_NEARESTG] = 300;
942 : } else {
943 0 : rd->thresh_mult[THR_NEARESTMV] = 0;
944 : #if CONFIG_EXT_REFS
945 0 : rd->thresh_mult[THR_NEARESTL2] = 0;
946 0 : rd->thresh_mult[THR_NEARESTL3] = 0;
947 0 : rd->thresh_mult[THR_NEARESTB] = 0;
948 : #endif // CONFIG_EXT_REFS
949 0 : rd->thresh_mult[THR_NEARESTA] = 0;
950 0 : rd->thresh_mult[THR_NEARESTG] = 0;
951 : }
952 :
953 0 : rd->thresh_mult[THR_DC] += 1000;
954 :
955 0 : rd->thresh_mult[THR_NEWMV] += 1000;
956 : #if CONFIG_EXT_REFS
957 0 : rd->thresh_mult[THR_NEWL2] += 1000;
958 0 : rd->thresh_mult[THR_NEWL3] += 1000;
959 0 : rd->thresh_mult[THR_NEWB] += 1000;
960 : #endif // CONFIG_EXT_REFS
961 0 : rd->thresh_mult[THR_NEWA] += 1000;
962 0 : rd->thresh_mult[THR_NEWG] += 1000;
963 :
964 0 : rd->thresh_mult[THR_NEARMV] += 1000;
965 : #if CONFIG_EXT_REFS
966 0 : rd->thresh_mult[THR_NEARL2] += 1000;
967 0 : rd->thresh_mult[THR_NEARL3] += 1000;
968 0 : rd->thresh_mult[THR_NEARB] += 1000;
969 : #endif // CONFIG_EXT_REFS
970 0 : rd->thresh_mult[THR_NEARA] += 1000;
971 0 : rd->thresh_mult[THR_NEARG] += 1000;
972 :
973 0 : rd->thresh_mult[THR_ZEROMV] += 2000;
974 : #if CONFIG_EXT_REFS
975 0 : rd->thresh_mult[THR_ZEROL2] += 2000;
976 0 : rd->thresh_mult[THR_ZEROL3] += 2000;
977 0 : rd->thresh_mult[THR_ZEROB] += 2000;
978 : #endif // CONFIG_EXT_REFS
979 0 : rd->thresh_mult[THR_ZEROG] += 2000;
980 0 : rd->thresh_mult[THR_ZEROA] += 2000;
981 :
982 0 : rd->thresh_mult[THR_TM] += 1000;
983 :
984 : #if CONFIG_EXT_INTER
985 :
986 0 : rd->thresh_mult[THR_COMP_NEAREST_NEARESTLA] += 1000;
987 : #if CONFIG_EXT_REFS
988 0 : rd->thresh_mult[THR_COMP_NEAREST_NEARESTL2A] += 1000;
989 0 : rd->thresh_mult[THR_COMP_NEAREST_NEARESTL3A] += 1000;
990 : #endif // CONFIG_EXT_REFS
991 0 : rd->thresh_mult[THR_COMP_NEAREST_NEARESTGA] += 1000;
992 : #if CONFIG_EXT_REFS
993 0 : rd->thresh_mult[THR_COMP_NEAREST_NEARESTLB] += 1000;
994 0 : rd->thresh_mult[THR_COMP_NEAREST_NEARESTL2B] += 1000;
995 0 : rd->thresh_mult[THR_COMP_NEAREST_NEARESTL3B] += 1000;
996 0 : rd->thresh_mult[THR_COMP_NEAREST_NEARESTGB] += 1000;
997 : #endif // CONFIG_EXT_REFS
998 :
999 : #else // CONFIG_EXT_INTER
1000 :
1001 : rd->thresh_mult[THR_COMP_NEARESTLA] += 1000;
1002 : #if CONFIG_EXT_REFS
1003 : rd->thresh_mult[THR_COMP_NEARESTL2A] += 1000;
1004 : rd->thresh_mult[THR_COMP_NEARESTL3A] += 1000;
1005 : #endif // CONFIG_EXT_REFS
1006 : rd->thresh_mult[THR_COMP_NEARESTGA] += 1000;
1007 : #if CONFIG_EXT_REFS
1008 : rd->thresh_mult[THR_COMP_NEARESTLB] += 1000;
1009 : rd->thresh_mult[THR_COMP_NEARESTL2B] += 1000;
1010 : rd->thresh_mult[THR_COMP_NEARESTL3B] += 1000;
1011 : rd->thresh_mult[THR_COMP_NEARESTGB] += 1000;
1012 : #endif // CONFIG_EXT_REFS
1013 :
1014 : #endif // CONFIG_EXT_INTER
1015 :
1016 : #if CONFIG_EXT_INTER
1017 :
1018 0 : rd->thresh_mult[THR_COMP_NEAR_NEARLA] += 1200;
1019 0 : rd->thresh_mult[THR_COMP_NEAREST_NEWLA] += 1500;
1020 0 : rd->thresh_mult[THR_COMP_NEW_NEARESTLA] += 1500;
1021 0 : rd->thresh_mult[THR_COMP_NEAR_NEWLA] += 1700;
1022 0 : rd->thresh_mult[THR_COMP_NEW_NEARLA] += 1700;
1023 0 : rd->thresh_mult[THR_COMP_NEW_NEWLA] += 2000;
1024 0 : rd->thresh_mult[THR_COMP_ZERO_ZEROLA] += 2500;
1025 :
1026 : #if CONFIG_EXT_REFS
1027 0 : rd->thresh_mult[THR_COMP_NEAR_NEARL2A] += 1200;
1028 0 : rd->thresh_mult[THR_COMP_NEAREST_NEWL2A] += 1500;
1029 0 : rd->thresh_mult[THR_COMP_NEW_NEARESTL2A] += 1500;
1030 0 : rd->thresh_mult[THR_COMP_NEAR_NEWL2A] += 1700;
1031 0 : rd->thresh_mult[THR_COMP_NEW_NEARL2A] += 1700;
1032 0 : rd->thresh_mult[THR_COMP_NEW_NEWL2A] += 2000;
1033 0 : rd->thresh_mult[THR_COMP_ZERO_ZEROL2A] += 2500;
1034 :
1035 0 : rd->thresh_mult[THR_COMP_NEAR_NEARL3A] += 1200;
1036 0 : rd->thresh_mult[THR_COMP_NEAREST_NEWL3A] += 1500;
1037 0 : rd->thresh_mult[THR_COMP_NEW_NEARESTL3A] += 1500;
1038 0 : rd->thresh_mult[THR_COMP_NEAR_NEWL3A] += 1700;
1039 0 : rd->thresh_mult[THR_COMP_NEW_NEARL3A] += 1700;
1040 0 : rd->thresh_mult[THR_COMP_NEW_NEWL3A] += 2000;
1041 0 : rd->thresh_mult[THR_COMP_ZERO_ZEROL3A] += 2500;
1042 : #endif // CONFIG_EXT_REFS
1043 :
1044 0 : rd->thresh_mult[THR_COMP_NEAR_NEARGA] += 1200;
1045 0 : rd->thresh_mult[THR_COMP_NEAREST_NEWGA] += 1500;
1046 0 : rd->thresh_mult[THR_COMP_NEW_NEARESTGA] += 1500;
1047 0 : rd->thresh_mult[THR_COMP_NEAR_NEWGA] += 1700;
1048 0 : rd->thresh_mult[THR_COMP_NEW_NEARGA] += 1700;
1049 0 : rd->thresh_mult[THR_COMP_NEW_NEWGA] += 2000;
1050 0 : rd->thresh_mult[THR_COMP_ZERO_ZEROGA] += 2500;
1051 :
1052 : #if CONFIG_EXT_REFS
1053 0 : rd->thresh_mult[THR_COMP_NEAR_NEARLB] += 1200;
1054 0 : rd->thresh_mult[THR_COMP_NEAREST_NEWLB] += 1500;
1055 0 : rd->thresh_mult[THR_COMP_NEW_NEARESTLB] += 1500;
1056 0 : rd->thresh_mult[THR_COMP_NEAR_NEWLB] += 1700;
1057 0 : rd->thresh_mult[THR_COMP_NEW_NEARLB] += 1700;
1058 0 : rd->thresh_mult[THR_COMP_NEW_NEWLB] += 2000;
1059 0 : rd->thresh_mult[THR_COMP_ZERO_ZEROLB] += 2500;
1060 :
1061 0 : rd->thresh_mult[THR_COMP_NEAR_NEARL2B] += 1200;
1062 0 : rd->thresh_mult[THR_COMP_NEAREST_NEWL2B] += 1500;
1063 0 : rd->thresh_mult[THR_COMP_NEW_NEARESTL2B] += 1500;
1064 0 : rd->thresh_mult[THR_COMP_NEAR_NEWL2B] += 1700;
1065 0 : rd->thresh_mult[THR_COMP_NEW_NEARL2B] += 1700;
1066 0 : rd->thresh_mult[THR_COMP_NEW_NEWL2B] += 2000;
1067 0 : rd->thresh_mult[THR_COMP_ZERO_ZEROL2B] += 2500;
1068 :
1069 0 : rd->thresh_mult[THR_COMP_NEAR_NEARL3B] += 1200;
1070 0 : rd->thresh_mult[THR_COMP_NEAREST_NEWL3B] += 1500;
1071 0 : rd->thresh_mult[THR_COMP_NEW_NEARESTL3B] += 1500;
1072 0 : rd->thresh_mult[THR_COMP_NEAR_NEWL3B] += 1700;
1073 0 : rd->thresh_mult[THR_COMP_NEW_NEARL3B] += 1700;
1074 0 : rd->thresh_mult[THR_COMP_NEW_NEWL3B] += 2000;
1075 0 : rd->thresh_mult[THR_COMP_ZERO_ZEROL3B] += 2500;
1076 :
1077 0 : rd->thresh_mult[THR_COMP_NEAR_NEARGB] += 1200;
1078 0 : rd->thresh_mult[THR_COMP_NEAREST_NEWGB] += 1500;
1079 0 : rd->thresh_mult[THR_COMP_NEW_NEARESTGB] += 1500;
1080 0 : rd->thresh_mult[THR_COMP_NEAR_NEWGB] += 1700;
1081 0 : rd->thresh_mult[THR_COMP_NEW_NEARGB] += 1700;
1082 0 : rd->thresh_mult[THR_COMP_NEW_NEWGB] += 2000;
1083 0 : rd->thresh_mult[THR_COMP_ZERO_ZEROGB] += 2500;
1084 : #endif // CONFIG_EXT_REFS
1085 :
1086 : #else // CONFIG_EXT_INTER
1087 :
1088 : rd->thresh_mult[THR_COMP_NEARLA] += 1500;
1089 : rd->thresh_mult[THR_COMP_NEWLA] += 2000;
1090 : #if CONFIG_EXT_REFS
1091 : rd->thresh_mult[THR_COMP_NEARL2A] += 1500;
1092 : rd->thresh_mult[THR_COMP_NEWL2A] += 2000;
1093 : rd->thresh_mult[THR_COMP_NEARL3A] += 1500;
1094 : rd->thresh_mult[THR_COMP_NEWL3A] += 2000;
1095 : #endif // CONFIG_EXT_REFS
1096 : rd->thresh_mult[THR_COMP_NEARGA] += 1500;
1097 : rd->thresh_mult[THR_COMP_NEWGA] += 2000;
1098 :
1099 : #if CONFIG_EXT_REFS
1100 : rd->thresh_mult[THR_COMP_NEARLB] += 1500;
1101 : rd->thresh_mult[THR_COMP_NEWLB] += 2000;
1102 : rd->thresh_mult[THR_COMP_NEARL2B] += 1500;
1103 : rd->thresh_mult[THR_COMP_NEWL2B] += 2000;
1104 : rd->thresh_mult[THR_COMP_NEARL3B] += 1500;
1105 : rd->thresh_mult[THR_COMP_NEWL3B] += 2000;
1106 : rd->thresh_mult[THR_COMP_NEARGB] += 1500;
1107 : rd->thresh_mult[THR_COMP_NEWGB] += 2000;
1108 : #endif // CONFIG_EXT_REFS
1109 :
1110 : rd->thresh_mult[THR_COMP_ZEROLA] += 2500;
1111 : #if CONFIG_EXT_REFS
1112 : rd->thresh_mult[THR_COMP_ZEROL2A] += 2500;
1113 : rd->thresh_mult[THR_COMP_ZEROL3A] += 2500;
1114 : #endif // CONFIG_EXT_REFS
1115 : rd->thresh_mult[THR_COMP_ZEROGA] += 2500;
1116 :
1117 : #if CONFIG_EXT_REFS
1118 : rd->thresh_mult[THR_COMP_ZEROLB] += 2500;
1119 : rd->thresh_mult[THR_COMP_ZEROL2B] += 2500;
1120 : rd->thresh_mult[THR_COMP_ZEROL3B] += 2500;
1121 : rd->thresh_mult[THR_COMP_ZEROGB] += 2500;
1122 : #endif // CONFIG_EXT_REFS
1123 :
1124 : #endif // CONFIG_EXT_INTER
1125 :
1126 0 : rd->thresh_mult[THR_H_PRED] += 2000;
1127 0 : rd->thresh_mult[THR_V_PRED] += 2000;
1128 0 : rd->thresh_mult[THR_D135_PRED] += 2500;
1129 0 : rd->thresh_mult[THR_D207_PRED] += 2500;
1130 0 : rd->thresh_mult[THR_D153_PRED] += 2500;
1131 0 : rd->thresh_mult[THR_D63_PRED] += 2500;
1132 0 : rd->thresh_mult[THR_D117_PRED] += 2500;
1133 0 : rd->thresh_mult[THR_D45_PRED] += 2500;
1134 :
1135 : #if CONFIG_EXT_INTER
1136 0 : rd->thresh_mult[THR_COMP_INTERINTRA_ZEROL] += 1500;
1137 0 : rd->thresh_mult[THR_COMP_INTERINTRA_NEARESTL] += 1500;
1138 0 : rd->thresh_mult[THR_COMP_INTERINTRA_NEARL] += 1500;
1139 0 : rd->thresh_mult[THR_COMP_INTERINTRA_NEWL] += 2000;
1140 :
1141 : #if CONFIG_EXT_REFS
1142 0 : rd->thresh_mult[THR_COMP_INTERINTRA_ZEROL2] += 1500;
1143 0 : rd->thresh_mult[THR_COMP_INTERINTRA_NEARESTL2] += 1500;
1144 0 : rd->thresh_mult[THR_COMP_INTERINTRA_NEARL2] += 1500;
1145 0 : rd->thresh_mult[THR_COMP_INTERINTRA_NEWL2] += 2000;
1146 :
1147 0 : rd->thresh_mult[THR_COMP_INTERINTRA_ZEROL3] += 1500;
1148 0 : rd->thresh_mult[THR_COMP_INTERINTRA_NEARESTL3] += 1500;
1149 0 : rd->thresh_mult[THR_COMP_INTERINTRA_NEARL3] += 1500;
1150 0 : rd->thresh_mult[THR_COMP_INTERINTRA_NEWL3] += 2000;
1151 : #endif // CONFIG_EXT_REFS
1152 :
1153 0 : rd->thresh_mult[THR_COMP_INTERINTRA_ZEROG] += 1500;
1154 0 : rd->thresh_mult[THR_COMP_INTERINTRA_NEARESTG] += 1500;
1155 0 : rd->thresh_mult[THR_COMP_INTERINTRA_NEARG] += 1500;
1156 0 : rd->thresh_mult[THR_COMP_INTERINTRA_NEWG] += 2000;
1157 :
1158 : #if CONFIG_EXT_REFS
1159 0 : rd->thresh_mult[THR_COMP_INTERINTRA_ZEROB] += 1500;
1160 0 : rd->thresh_mult[THR_COMP_INTERINTRA_NEARESTB] += 1500;
1161 0 : rd->thresh_mult[THR_COMP_INTERINTRA_NEARB] += 1500;
1162 0 : rd->thresh_mult[THR_COMP_INTERINTRA_NEWB] += 2000;
1163 : #endif // CONFIG_EXT_REFS
1164 :
1165 0 : rd->thresh_mult[THR_COMP_INTERINTRA_ZEROA] += 1500;
1166 0 : rd->thresh_mult[THR_COMP_INTERINTRA_NEARESTA] += 1500;
1167 0 : rd->thresh_mult[THR_COMP_INTERINTRA_NEARA] += 1500;
1168 0 : rd->thresh_mult[THR_COMP_INTERINTRA_NEWA] += 2000;
1169 : #endif // CONFIG_EXT_INTER
1170 0 : }
1171 :
1172 0 : void av1_set_rd_speed_thresholds_sub8x8(AV1_COMP *cpi) {
1173 : static const int thresh_mult[MAX_REFS] = {
1174 : #if CONFIG_EXT_REFS
1175 : 2500,
1176 : 2500,
1177 : 2500,
1178 : 2500,
1179 : 2500,
1180 : 2500,
1181 : 4500,
1182 : 4500,
1183 : 4500,
1184 : 4500,
1185 : 4500,
1186 : 4500,
1187 : 4500,
1188 : 4500,
1189 : 2500
1190 : #else
1191 : 2500,
1192 : 2500,
1193 : 2500,
1194 : 4500,
1195 : 4500,
1196 : 2500
1197 : #endif // CONFIG_EXT_REFS
1198 : };
1199 0 : RD_OPT *const rd = &cpi->rd;
1200 0 : memcpy(rd->thresh_mult_sub8x8, thresh_mult, sizeof(thresh_mult));
1201 0 : }
1202 :
1203 0 : void av1_update_rd_thresh_fact(const AV1_COMMON *const cm,
1204 : int (*factor_buf)[MAX_MODES], int rd_thresh,
1205 : int bsize, int best_mode_index) {
1206 0 : if (rd_thresh > 0) {
1207 : #if CONFIG_CB4X4
1208 0 : const int top_mode = MAX_MODES;
1209 : #else
1210 : const int top_mode = bsize < BLOCK_8X8 ? MAX_REFS : MAX_MODES;
1211 : #endif
1212 : int mode;
1213 0 : for (mode = 0; mode < top_mode; ++mode) {
1214 0 : const BLOCK_SIZE min_size = AOMMAX(bsize - 1, BLOCK_4X4);
1215 0 : const BLOCK_SIZE max_size = AOMMIN(bsize + 2, (int)cm->sb_size);
1216 : BLOCK_SIZE bs;
1217 0 : for (bs = min_size; bs <= max_size; ++bs) {
1218 0 : int *const fact = &factor_buf[bs][mode];
1219 0 : if (mode == best_mode_index) {
1220 0 : *fact -= (*fact >> 4);
1221 : } else {
1222 0 : *fact = AOMMIN(*fact + RD_THRESH_INC, rd_thresh * RD_THRESH_MAX_FACT);
1223 : }
1224 : }
1225 : }
1226 : }
1227 0 : }
1228 :
1229 0 : int av1_get_intra_cost_penalty(int qindex, int qdelta,
1230 : aom_bit_depth_t bit_depth) {
1231 0 : const int q = av1_dc_quant(qindex, qdelta, bit_depth);
1232 : #if CONFIG_HIGHBITDEPTH
1233 0 : switch (bit_depth) {
1234 0 : case AOM_BITS_8: return 20 * q;
1235 0 : case AOM_BITS_10: return 5 * q;
1236 0 : case AOM_BITS_12: return ROUND_POWER_OF_TWO(5 * q, 2);
1237 : default:
1238 0 : assert(0 && "bit_depth should be AOM_BITS_8, AOM_BITS_10 or AOM_BITS_12");
1239 : return -1;
1240 : }
1241 : #else
1242 : return 20 * q;
1243 : #endif // CONFIG_HIGHBITDEPTH
1244 : }
|