• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "config/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/encodetxb.h"
40 #include "av1/encoder/mcomp.h"
41 #include "av1/encoder/ratectrl.h"
42 #include "av1/encoder/rd.h"
43 #include "av1/encoder/tokenize.h"
44 
45 #define RD_THRESH_POW 1.25
46 
47 // The baseline rd thresholds for breaking out of the rd loop for
48 // certain modes are assumed to be based on 8x8 blocks.
49 // This table is used to correct for block size.
50 // The factors here are << 2 (2 = x0.5, 32 = x8 etc).
51 static const uint8_t rd_thresh_block_size_factor[BLOCK_SIZES_ALL] = {
52   2, 3, 3, 4, 6, 6, 8, 12, 12, 16, 24, 24, 32, 48, 48, 64, 4, 4, 8, 8, 16, 16
53 };
54 
55 static const int use_intra_ext_tx_for_txsize[EXT_TX_SETS_INTRA][EXT_TX_SIZES] =
56     {
57       { 1, 1, 1, 1 },  // unused
58       { 1, 1, 0, 0 },
59       { 0, 0, 1, 0 },
60     };
61 
62 static const int use_inter_ext_tx_for_txsize[EXT_TX_SETS_INTER][EXT_TX_SIZES] =
63     {
64       { 1, 1, 1, 1 },  // unused
65       { 1, 1, 0, 0 },
66       { 0, 0, 1, 0 },
67       { 0, 0, 0, 1 },
68     };
69 
70 static const int av1_ext_tx_set_idx_to_type[2][AOMMAX(EXT_TX_SETS_INTRA,
71                                                       EXT_TX_SETS_INTER)] = {
72   {
73       // Intra
74       EXT_TX_SET_DCTONLY,
75       EXT_TX_SET_DTT4_IDTX_1DDCT,
76       EXT_TX_SET_DTT4_IDTX,
77   },
78   {
79       // Inter
80       EXT_TX_SET_DCTONLY,
81       EXT_TX_SET_ALL16,
82       EXT_TX_SET_DTT9_IDTX_1DDCT,
83       EXT_TX_SET_DCT_IDTX,
84   },
85 };
86 
av1_fill_mode_rates(AV1_COMMON * const cm,MACROBLOCK * x,FRAME_CONTEXT * fc)87 void av1_fill_mode_rates(AV1_COMMON *const cm, MACROBLOCK *x,
88                          FRAME_CONTEXT *fc) {
89   int i, j;
90 
91   for (i = 0; i < PARTITION_CONTEXTS; ++i)
92     av1_cost_tokens_from_cdf(x->partition_cost[i], fc->partition_cdf[i], NULL);
93 
94   if (cm->current_frame.skip_mode_info.skip_mode_flag) {
95     for (i = 0; i < SKIP_CONTEXTS; ++i) {
96       av1_cost_tokens_from_cdf(x->skip_mode_cost[i], fc->skip_mode_cdfs[i],
97                                NULL);
98     }
99   }
100 
101   for (i = 0; i < SKIP_CONTEXTS; ++i) {
102     av1_cost_tokens_from_cdf(x->skip_cost[i], fc->skip_cdfs[i], NULL);
103   }
104 
105   for (i = 0; i < KF_MODE_CONTEXTS; ++i)
106     for (j = 0; j < KF_MODE_CONTEXTS; ++j)
107       av1_cost_tokens_from_cdf(x->y_mode_costs[i][j], fc->kf_y_cdf[i][j], NULL);
108 
109   for (i = 0; i < BLOCK_SIZE_GROUPS; ++i)
110     av1_cost_tokens_from_cdf(x->mbmode_cost[i], fc->y_mode_cdf[i], NULL);
111   for (i = 0; i < CFL_ALLOWED_TYPES; ++i)
112     for (j = 0; j < INTRA_MODES; ++j)
113       av1_cost_tokens_from_cdf(x->intra_uv_mode_cost[i][j],
114                                fc->uv_mode_cdf[i][j], NULL);
115 
116   av1_cost_tokens_from_cdf(x->filter_intra_mode_cost, fc->filter_intra_mode_cdf,
117                            NULL);
118   for (i = 0; i < BLOCK_SIZES_ALL; ++i) {
119     if (av1_filter_intra_allowed_bsize(cm, i))
120       av1_cost_tokens_from_cdf(x->filter_intra_cost[i],
121                                fc->filter_intra_cdfs[i], NULL);
122   }
123 
124   for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; ++i)
125     av1_cost_tokens_from_cdf(x->switchable_interp_costs[i],
126                              fc->switchable_interp_cdf[i], NULL);
127 
128   for (i = 0; i < PALATTE_BSIZE_CTXS; ++i) {
129     av1_cost_tokens_from_cdf(x->palette_y_size_cost[i],
130                              fc->palette_y_size_cdf[i], NULL);
131     av1_cost_tokens_from_cdf(x->palette_uv_size_cost[i],
132                              fc->palette_uv_size_cdf[i], NULL);
133     for (j = 0; j < PALETTE_Y_MODE_CONTEXTS; ++j) {
134       av1_cost_tokens_from_cdf(x->palette_y_mode_cost[i][j],
135                                fc->palette_y_mode_cdf[i][j], NULL);
136     }
137   }
138 
139   for (i = 0; i < PALETTE_UV_MODE_CONTEXTS; ++i) {
140     av1_cost_tokens_from_cdf(x->palette_uv_mode_cost[i],
141                              fc->palette_uv_mode_cdf[i], NULL);
142   }
143 
144   for (i = 0; i < PALETTE_SIZES; ++i) {
145     for (j = 0; j < PALETTE_COLOR_INDEX_CONTEXTS; ++j) {
146       av1_cost_tokens_from_cdf(x->palette_y_color_cost[i][j],
147                                fc->palette_y_color_index_cdf[i][j], NULL);
148       av1_cost_tokens_from_cdf(x->palette_uv_color_cost[i][j],
149                                fc->palette_uv_color_index_cdf[i][j], NULL);
150     }
151   }
152 
153   int sign_cost[CFL_JOINT_SIGNS];
154   av1_cost_tokens_from_cdf(sign_cost, fc->cfl_sign_cdf, NULL);
155   for (int joint_sign = 0; joint_sign < CFL_JOINT_SIGNS; joint_sign++) {
156     int *cost_u = x->cfl_cost[joint_sign][CFL_PRED_U];
157     int *cost_v = x->cfl_cost[joint_sign][CFL_PRED_V];
158     if (CFL_SIGN_U(joint_sign) == CFL_SIGN_ZERO) {
159       memset(cost_u, 0, CFL_ALPHABET_SIZE * sizeof(*cost_u));
160     } else {
161       const aom_cdf_prob *cdf_u = fc->cfl_alpha_cdf[CFL_CONTEXT_U(joint_sign)];
162       av1_cost_tokens_from_cdf(cost_u, cdf_u, NULL);
163     }
164     if (CFL_SIGN_V(joint_sign) == CFL_SIGN_ZERO) {
165       memset(cost_v, 0, CFL_ALPHABET_SIZE * sizeof(*cost_v));
166     } else {
167       const aom_cdf_prob *cdf_v = fc->cfl_alpha_cdf[CFL_CONTEXT_V(joint_sign)];
168       av1_cost_tokens_from_cdf(cost_v, cdf_v, NULL);
169     }
170     for (int u = 0; u < CFL_ALPHABET_SIZE; u++)
171       cost_u[u] += sign_cost[joint_sign];
172   }
173 
174   for (i = 0; i < MAX_TX_CATS; ++i)
175     for (j = 0; j < TX_SIZE_CONTEXTS; ++j)
176       av1_cost_tokens_from_cdf(x->tx_size_cost[i][j], fc->tx_size_cdf[i][j],
177                                NULL);
178 
179   for (i = 0; i < TXFM_PARTITION_CONTEXTS; ++i) {
180     av1_cost_tokens_from_cdf(x->txfm_partition_cost[i],
181                              fc->txfm_partition_cdf[i], NULL);
182   }
183 
184   for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
185     int s;
186     for (s = 1; s < EXT_TX_SETS_INTER; ++s) {
187       if (use_inter_ext_tx_for_txsize[s][i]) {
188         av1_cost_tokens_from_cdf(
189             x->inter_tx_type_costs[s][i], fc->inter_ext_tx_cdf[s][i],
190             av1_ext_tx_inv[av1_ext_tx_set_idx_to_type[1][s]]);
191       }
192     }
193     for (s = 1; s < EXT_TX_SETS_INTRA; ++s) {
194       if (use_intra_ext_tx_for_txsize[s][i]) {
195         for (j = 0; j < INTRA_MODES; ++j) {
196           av1_cost_tokens_from_cdf(
197               x->intra_tx_type_costs[s][i][j], fc->intra_ext_tx_cdf[s][i][j],
198               av1_ext_tx_inv[av1_ext_tx_set_idx_to_type[0][s]]);
199         }
200       }
201     }
202   }
203   for (i = 0; i < DIRECTIONAL_MODES; ++i) {
204     av1_cost_tokens_from_cdf(x->angle_delta_cost[i], fc->angle_delta_cdf[i],
205                              NULL);
206   }
207   av1_cost_tokens_from_cdf(x->switchable_restore_cost,
208                            fc->switchable_restore_cdf, NULL);
209   av1_cost_tokens_from_cdf(x->wiener_restore_cost, fc->wiener_restore_cdf,
210                            NULL);
211   av1_cost_tokens_from_cdf(x->sgrproj_restore_cost, fc->sgrproj_restore_cdf,
212                            NULL);
213   av1_cost_tokens_from_cdf(x->intrabc_cost, fc->intrabc_cdf, NULL);
214 
215   if (!frame_is_intra_only(cm)) {
216     for (i = 0; i < COMP_INTER_CONTEXTS; ++i) {
217       av1_cost_tokens_from_cdf(x->comp_inter_cost[i], fc->comp_inter_cdf[i],
218                                NULL);
219     }
220 
221     for (i = 0; i < REF_CONTEXTS; ++i) {
222       for (j = 0; j < SINGLE_REFS - 1; ++j) {
223         av1_cost_tokens_from_cdf(x->single_ref_cost[i][j],
224                                  fc->single_ref_cdf[i][j], NULL);
225       }
226     }
227 
228     for (i = 0; i < COMP_REF_TYPE_CONTEXTS; ++i) {
229       av1_cost_tokens_from_cdf(x->comp_ref_type_cost[i],
230                                fc->comp_ref_type_cdf[i], NULL);
231     }
232 
233     for (i = 0; i < UNI_COMP_REF_CONTEXTS; ++i) {
234       for (j = 0; j < UNIDIR_COMP_REFS - 1; ++j) {
235         av1_cost_tokens_from_cdf(x->uni_comp_ref_cost[i][j],
236                                  fc->uni_comp_ref_cdf[i][j], NULL);
237       }
238     }
239 
240     for (i = 0; i < REF_CONTEXTS; ++i) {
241       for (j = 0; j < FWD_REFS - 1; ++j) {
242         av1_cost_tokens_from_cdf(x->comp_ref_cost[i][j], fc->comp_ref_cdf[i][j],
243                                  NULL);
244       }
245     }
246 
247     for (i = 0; i < REF_CONTEXTS; ++i) {
248       for (j = 0; j < BWD_REFS - 1; ++j) {
249         av1_cost_tokens_from_cdf(x->comp_bwdref_cost[i][j],
250                                  fc->comp_bwdref_cdf[i][j], NULL);
251       }
252     }
253 
254     for (i = 0; i < INTRA_INTER_CONTEXTS; ++i) {
255       av1_cost_tokens_from_cdf(x->intra_inter_cost[i], fc->intra_inter_cdf[i],
256                                NULL);
257     }
258 
259     for (i = 0; i < NEWMV_MODE_CONTEXTS; ++i) {
260       av1_cost_tokens_from_cdf(x->newmv_mode_cost[i], fc->newmv_cdf[i], NULL);
261     }
262 
263     for (i = 0; i < GLOBALMV_MODE_CONTEXTS; ++i) {
264       av1_cost_tokens_from_cdf(x->zeromv_mode_cost[i], fc->zeromv_cdf[i], NULL);
265     }
266 
267     for (i = 0; i < REFMV_MODE_CONTEXTS; ++i) {
268       av1_cost_tokens_from_cdf(x->refmv_mode_cost[i], fc->refmv_cdf[i], NULL);
269     }
270 
271     for (i = 0; i < DRL_MODE_CONTEXTS; ++i) {
272       av1_cost_tokens_from_cdf(x->drl_mode_cost0[i], fc->drl_cdf[i], NULL);
273     }
274     for (i = 0; i < INTER_MODE_CONTEXTS; ++i)
275       av1_cost_tokens_from_cdf(x->inter_compound_mode_cost[i],
276                                fc->inter_compound_mode_cdf[i], NULL);
277     for (i = 0; i < BLOCK_SIZES_ALL; ++i)
278       av1_cost_tokens_from_cdf(x->compound_type_cost[i],
279                                fc->compound_type_cdf[i], NULL);
280     for (i = 0; i < BLOCK_SIZES_ALL; ++i) {
281       if (get_interinter_wedge_bits(i)) {
282         av1_cost_tokens_from_cdf(x->wedge_idx_cost[i], fc->wedge_idx_cdf[i],
283                                  NULL);
284       }
285     }
286     for (i = 0; i < BLOCK_SIZE_GROUPS; ++i) {
287       av1_cost_tokens_from_cdf(x->interintra_cost[i], fc->interintra_cdf[i],
288                                NULL);
289       av1_cost_tokens_from_cdf(x->interintra_mode_cost[i],
290                                fc->interintra_mode_cdf[i], NULL);
291     }
292     for (i = 0; i < BLOCK_SIZES_ALL; ++i) {
293       av1_cost_tokens_from_cdf(x->wedge_interintra_cost[i],
294                                fc->wedge_interintra_cdf[i], NULL);
295     }
296     for (i = BLOCK_8X8; i < BLOCK_SIZES_ALL; i++) {
297       av1_cost_tokens_from_cdf(x->motion_mode_cost[i], fc->motion_mode_cdf[i],
298                                NULL);
299     }
300     for (i = BLOCK_8X8; i < BLOCK_SIZES_ALL; i++) {
301       av1_cost_tokens_from_cdf(x->motion_mode_cost1[i], fc->obmc_cdf[i], NULL);
302     }
303     for (i = 0; i < COMP_INDEX_CONTEXTS; ++i) {
304       av1_cost_tokens_from_cdf(x->comp_idx_cost[i], fc->compound_index_cdf[i],
305                                NULL);
306     }
307     for (i = 0; i < COMP_GROUP_IDX_CONTEXTS; ++i) {
308       av1_cost_tokens_from_cdf(x->comp_group_idx_cost[i],
309                                fc->comp_group_idx_cdf[i], NULL);
310     }
311   }
312 }
313 
314 // Values are now correlated to quantizer.
315 static int sad_per_bit16lut_8[QINDEX_RANGE];
316 static int sad_per_bit4lut_8[QINDEX_RANGE];
317 static int sad_per_bit16lut_10[QINDEX_RANGE];
318 static int sad_per_bit4lut_10[QINDEX_RANGE];
319 static int sad_per_bit16lut_12[QINDEX_RANGE];
320 static int sad_per_bit4lut_12[QINDEX_RANGE];
321 
init_me_luts_bd(int * bit16lut,int * bit4lut,int range,aom_bit_depth_t bit_depth)322 static void init_me_luts_bd(int *bit16lut, int *bit4lut, int range,
323                             aom_bit_depth_t bit_depth) {
324   int i;
325   // Initialize the sad lut tables using a formulaic calculation for now.
326   // This is to make it easier to resolve the impact of experimental changes
327   // to the quantizer tables.
328   for (i = 0; i < range; i++) {
329     const double q = av1_convert_qindex_to_q(i, bit_depth);
330     bit16lut[i] = (int)(0.0418 * q + 2.4107);
331     bit4lut[i] = (int)(0.063 * q + 2.742);
332   }
333 }
334 
av1_init_me_luts(void)335 void av1_init_me_luts(void) {
336   init_me_luts_bd(sad_per_bit16lut_8, sad_per_bit4lut_8, QINDEX_RANGE,
337                   AOM_BITS_8);
338   init_me_luts_bd(sad_per_bit16lut_10, sad_per_bit4lut_10, QINDEX_RANGE,
339                   AOM_BITS_10);
340   init_me_luts_bd(sad_per_bit16lut_12, sad_per_bit4lut_12, QINDEX_RANGE,
341                   AOM_BITS_12);
342 }
343 
344 static const int rd_boost_factor[16] = { 64, 32, 32, 32, 24, 16, 12, 12,
345                                          8,  8,  4,  4,  2,  2,  1,  0 };
346 static const int rd_frame_type_factor[FRAME_UPDATE_TYPES] = {
347   128, 144, 128, 128, 144, 144, 128
348 };
349 
av1_compute_rd_mult_based_on_qindex(const AV1_COMP * cpi,int qindex)350 int av1_compute_rd_mult_based_on_qindex(const AV1_COMP *cpi, int qindex) {
351   const int q = av1_dc_quant_Q3(qindex, 0, cpi->common.seq_params.bit_depth);
352   int rdmult = q * q;
353   rdmult = rdmult * 3 + (rdmult * 2 / 3);
354   switch (cpi->common.seq_params.bit_depth) {
355     case AOM_BITS_8: break;
356     case AOM_BITS_10: rdmult = ROUND_POWER_OF_TWO(rdmult, 4); break;
357     case AOM_BITS_12: rdmult = ROUND_POWER_OF_TWO(rdmult, 8); break;
358     default:
359       assert(0 && "bit_depth should be AOM_BITS_8, AOM_BITS_10 or AOM_BITS_12");
360       return -1;
361   }
362   return rdmult > 0 ? rdmult : 1;
363 }
364 
av1_compute_rd_mult(const AV1_COMP * cpi,int qindex)365 int av1_compute_rd_mult(const AV1_COMP *cpi, int qindex) {
366   int64_t rdmult = av1_compute_rd_mult_based_on_qindex(cpi, qindex);
367   if (cpi->oxcf.pass == 2 &&
368       (cpi->common.current_frame.frame_type != KEY_FRAME)) {
369     const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
370     const FRAME_UPDATE_TYPE frame_type = gf_group->update_type[gf_group->index];
371     const int boost_index = AOMMIN(15, (cpi->rc.gfu_boost / 100));
372 
373     rdmult = (rdmult * rd_frame_type_factor[frame_type]) >> 7;
374     rdmult += ((rdmult * rd_boost_factor[boost_index]) >> 7);
375   }
376   return (int)rdmult;
377 }
378 
av1_get_adaptive_rdmult(const AV1_COMP * cpi,double beta)379 int av1_get_adaptive_rdmult(const AV1_COMP *cpi, double beta) {
380   const AV1_COMMON *cm = &cpi->common;
381   int64_t q =
382       av1_dc_quant_Q3(cm->base_qindex, 0, cpi->common.seq_params.bit_depth);
383   int64_t rdmult = 0;
384 
385   switch (cpi->common.seq_params.bit_depth) {
386     case AOM_BITS_8: rdmult = (int)((88 * q * q / beta) / 24); break;
387     case AOM_BITS_10:
388       rdmult = ROUND_POWER_OF_TWO((int)((88 * q * q / beta) / 24), 4);
389       break;
390     default:
391       assert(cpi->common.seq_params.bit_depth == AOM_BITS_12);
392       rdmult = ROUND_POWER_OF_TWO((int)((88 * q * q / beta) / 24), 8);
393       break;
394   }
395 
396   if (cpi->oxcf.pass == 2 &&
397       (cpi->common.current_frame.frame_type != KEY_FRAME)) {
398     const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
399     const FRAME_UPDATE_TYPE frame_type = gf_group->update_type[gf_group->index];
400     const int boost_index = AOMMIN(15, (cpi->rc.gfu_boost / 100));
401 
402     rdmult = (rdmult * rd_frame_type_factor[frame_type]) >> 7;
403     rdmult += ((rdmult * rd_boost_factor[boost_index]) >> 7);
404   }
405   if (rdmult < 1) rdmult = 1;
406   return (int)rdmult;
407 }
408 
compute_rd_thresh_factor(int qindex,aom_bit_depth_t bit_depth)409 static int compute_rd_thresh_factor(int qindex, aom_bit_depth_t bit_depth) {
410   double q;
411   switch (bit_depth) {
412     case AOM_BITS_8: q = av1_dc_quant_Q3(qindex, 0, AOM_BITS_8) / 4.0; break;
413     case AOM_BITS_10: q = av1_dc_quant_Q3(qindex, 0, AOM_BITS_10) / 16.0; break;
414     case AOM_BITS_12: q = av1_dc_quant_Q3(qindex, 0, AOM_BITS_12) / 64.0; break;
415     default:
416       assert(0 && "bit_depth should be AOM_BITS_8, AOM_BITS_10 or AOM_BITS_12");
417       return -1;
418   }
419   // TODO(debargha): Adjust the function below.
420   return AOMMAX((int)(pow(q, RD_THRESH_POW) * 5.12), 8);
421 }
422 
av1_initialize_me_consts(const AV1_COMP * cpi,MACROBLOCK * x,int qindex)423 void av1_initialize_me_consts(const AV1_COMP *cpi, MACROBLOCK *x, int qindex) {
424   switch (cpi->common.seq_params.bit_depth) {
425     case AOM_BITS_8:
426       x->sadperbit16 = sad_per_bit16lut_8[qindex];
427       x->sadperbit4 = sad_per_bit4lut_8[qindex];
428       break;
429     case AOM_BITS_10:
430       x->sadperbit16 = sad_per_bit16lut_10[qindex];
431       x->sadperbit4 = sad_per_bit4lut_10[qindex];
432       break;
433     case AOM_BITS_12:
434       x->sadperbit16 = sad_per_bit16lut_12[qindex];
435       x->sadperbit4 = sad_per_bit4lut_12[qindex];
436       break;
437     default:
438       assert(0 && "bit_depth should be AOM_BITS_8, AOM_BITS_10 or AOM_BITS_12");
439   }
440 }
441 
set_block_thresholds(const AV1_COMMON * cm,RD_OPT * rd)442 static void set_block_thresholds(const AV1_COMMON *cm, RD_OPT *rd) {
443   int i, bsize, segment_id;
444 
445   for (segment_id = 0; segment_id < MAX_SEGMENTS; ++segment_id) {
446     const int qindex =
447         clamp(av1_get_qindex(&cm->seg, segment_id, cm->base_qindex) +
448                   cm->y_dc_delta_q,
449               0, MAXQ);
450     const int q = compute_rd_thresh_factor(qindex, cm->seq_params.bit_depth);
451 
452     for (bsize = 0; bsize < BLOCK_SIZES_ALL; ++bsize) {
453       // Threshold here seems unnecessarily harsh but fine given actual
454       // range of values used for cpi->sf.thresh_mult[].
455       const int t = q * rd_thresh_block_size_factor[bsize];
456       const int thresh_max = INT_MAX / t;
457 
458       for (i = 0; i < MAX_MODES; ++i)
459         rd->threshes[segment_id][bsize][i] = rd->thresh_mult[i] < thresh_max
460                                                  ? rd->thresh_mult[i] * t / 4
461                                                  : INT_MAX;
462     }
463   }
464 }
465 
av1_fill_coeff_costs(MACROBLOCK * x,FRAME_CONTEXT * fc,const int num_planes)466 void av1_fill_coeff_costs(MACROBLOCK *x, FRAME_CONTEXT *fc,
467                           const int num_planes) {
468   const int nplanes = AOMMIN(num_planes, PLANE_TYPES);
469   for (int eob_multi_size = 0; eob_multi_size < 7; ++eob_multi_size) {
470     for (int plane = 0; plane < nplanes; ++plane) {
471       LV_MAP_EOB_COST *pcost = &x->eob_costs[eob_multi_size][plane];
472 
473       for (int ctx = 0; ctx < 2; ++ctx) {
474         aom_cdf_prob *pcdf;
475         switch (eob_multi_size) {
476           case 0: pcdf = fc->eob_flag_cdf16[plane][ctx]; break;
477           case 1: pcdf = fc->eob_flag_cdf32[plane][ctx]; break;
478           case 2: pcdf = fc->eob_flag_cdf64[plane][ctx]; break;
479           case 3: pcdf = fc->eob_flag_cdf128[plane][ctx]; break;
480           case 4: pcdf = fc->eob_flag_cdf256[plane][ctx]; break;
481           case 5: pcdf = fc->eob_flag_cdf512[plane][ctx]; break;
482           case 6:
483           default: pcdf = fc->eob_flag_cdf1024[plane][ctx]; break;
484         }
485         av1_cost_tokens_from_cdf(pcost->eob_cost[ctx], pcdf, NULL);
486       }
487     }
488   }
489   for (int tx_size = 0; tx_size < TX_SIZES; ++tx_size) {
490     for (int plane = 0; plane < nplanes; ++plane) {
491       LV_MAP_COEFF_COST *pcost = &x->coeff_costs[tx_size][plane];
492 
493       for (int ctx = 0; ctx < TXB_SKIP_CONTEXTS; ++ctx)
494         av1_cost_tokens_from_cdf(pcost->txb_skip_cost[ctx],
495                                  fc->txb_skip_cdf[tx_size][ctx], NULL);
496 
497       for (int ctx = 0; ctx < SIG_COEF_CONTEXTS_EOB; ++ctx)
498         av1_cost_tokens_from_cdf(pcost->base_eob_cost[ctx],
499                                  fc->coeff_base_eob_cdf[tx_size][plane][ctx],
500                                  NULL);
501       for (int ctx = 0; ctx < SIG_COEF_CONTEXTS; ++ctx)
502         av1_cost_tokens_from_cdf(pcost->base_cost[ctx],
503                                  fc->coeff_base_cdf[tx_size][plane][ctx], NULL);
504 
505       for (int ctx = 0; ctx < SIG_COEF_CONTEXTS; ++ctx) {
506         pcost->base_cost[ctx][4] = 0;
507         pcost->base_cost[ctx][5] = pcost->base_cost[ctx][1] +
508                                    av1_cost_literal(1) -
509                                    pcost->base_cost[ctx][0];
510         pcost->base_cost[ctx][6] =
511             pcost->base_cost[ctx][2] - pcost->base_cost[ctx][1];
512         pcost->base_cost[ctx][7] =
513             pcost->base_cost[ctx][3] - pcost->base_cost[ctx][2];
514       }
515 
516       for (int ctx = 0; ctx < EOB_COEF_CONTEXTS; ++ctx)
517         av1_cost_tokens_from_cdf(pcost->eob_extra_cost[ctx],
518                                  fc->eob_extra_cdf[tx_size][plane][ctx], NULL);
519 
520       for (int ctx = 0; ctx < DC_SIGN_CONTEXTS; ++ctx)
521         av1_cost_tokens_from_cdf(pcost->dc_sign_cost[ctx],
522                                  fc->dc_sign_cdf[plane][ctx], NULL);
523 
524       for (int ctx = 0; ctx < LEVEL_CONTEXTS; ++ctx) {
525         int br_rate[BR_CDF_SIZE];
526         int prev_cost = 0;
527         int i, j;
528         av1_cost_tokens_from_cdf(br_rate, fc->coeff_br_cdf[tx_size][plane][ctx],
529                                  NULL);
530         // printf("br_rate: ");
531         // for(j = 0; j < BR_CDF_SIZE; j++)
532         //  printf("%4d ", br_rate[j]);
533         // printf("\n");
534         for (i = 0; i < COEFF_BASE_RANGE; i += BR_CDF_SIZE - 1) {
535           for (j = 0; j < BR_CDF_SIZE - 1; j++) {
536             pcost->lps_cost[ctx][i + j] = prev_cost + br_rate[j];
537           }
538           prev_cost += br_rate[j];
539         }
540         pcost->lps_cost[ctx][i] = prev_cost;
541         // printf("lps_cost: %d %d %2d : ", tx_size, plane, ctx);
542         // for (i = 0; i <= COEFF_BASE_RANGE; i++)
543         //  printf("%5d ", pcost->lps_cost[ctx][i]);
544         // printf("\n");
545       }
546       for (int ctx = 0; ctx < LEVEL_CONTEXTS; ++ctx) {
547         pcost->lps_cost[ctx][0 + COEFF_BASE_RANGE + 1] =
548             pcost->lps_cost[ctx][0];
549         for (int i = 1; i <= COEFF_BASE_RANGE; ++i) {
550           pcost->lps_cost[ctx][i + COEFF_BASE_RANGE + 1] =
551               pcost->lps_cost[ctx][i] - pcost->lps_cost[ctx][i - 1];
552         }
553       }
554     }
555   }
556 }
557 
av1_initialize_cost_tables(const AV1_COMMON * const cm,MACROBLOCK * x)558 void av1_initialize_cost_tables(const AV1_COMMON *const cm, MACROBLOCK *x) {
559   if (cm->cur_frame_force_integer_mv) {
560     av1_build_nmv_cost_table(x->nmv_vec_cost, x->nmvcost, &cm->fc->nmvc,
561                              MV_SUBPEL_NONE);
562   } else {
563     av1_build_nmv_cost_table(
564         x->nmv_vec_cost,
565         cm->allow_high_precision_mv ? x->nmvcost_hp : x->nmvcost, &cm->fc->nmvc,
566         cm->allow_high_precision_mv);
567   }
568 }
569 
av1_initialize_rd_consts(AV1_COMP * cpi)570 void av1_initialize_rd_consts(AV1_COMP *cpi) {
571   AV1_COMMON *const cm = &cpi->common;
572   MACROBLOCK *const x = &cpi->td.mb;
573   RD_OPT *const rd = &cpi->rd;
574 
575   aom_clear_system_state();
576 
577   rd->RDMULT = av1_compute_rd_mult(cpi, cm->base_qindex + cm->y_dc_delta_q);
578 
579   set_error_per_bit(x, rd->RDMULT);
580 
581   set_block_thresholds(cm, rd);
582 
583   av1_initialize_cost_tables(cm, x);
584 
585   if (frame_is_intra_only(cm) && cm->allow_screen_content_tools &&
586       cpi->oxcf.pass != 1) {
587     int *dvcost[2] = { &cpi->dv_cost[0][MV_MAX], &cpi->dv_cost[1][MV_MAX] };
588     av1_build_nmv_cost_table(cpi->dv_joint_cost, dvcost, &cm->fc->ndvc,
589                              MV_SUBPEL_NONE);
590   }
591 
592   if (cpi->oxcf.pass != 1) {
593     for (int i = 0; i < TRANS_TYPES; ++i)
594       // IDENTITY: 1 bit
595       // TRANSLATION: 3 bits
596       // ROTZOOM: 2 bits
597       // AFFINE: 3 bits
598       cpi->gmtype_cost[i] = (1 + (i > 0 ? (i == ROTZOOM ? 1 : 2) : 0))
599                             << AV1_PROB_COST_SHIFT;
600   }
601 }
602 
model_rd_norm(int xsq_q10,int * r_q10,int * d_q10)603 static void model_rd_norm(int xsq_q10, int *r_q10, int *d_q10) {
604   // NOTE: The tables below must be of the same size.
605 
606   // The functions described below are sampled at the four most significant
607   // bits of x^2 + 8 / 256.
608 
609   // Normalized rate:
610   // This table models the rate for a Laplacian source with given variance
611   // when quantized with a uniform quantizer with given stepsize. The
612   // closed form expression is:
613   // Rn(x) = H(sqrt(r)) + sqrt(r)*[1 + H(r)/(1 - r)],
614   // where r = exp(-sqrt(2) * x) and x = qpstep / sqrt(variance),
615   // and H(x) is the binary entropy function.
616   static const int rate_tab_q10[] = {
617     65536, 6086, 5574, 5275, 5063, 4899, 4764, 4651, 4553, 4389, 4255, 4142,
618     4044,  3958, 3881, 3811, 3748, 3635, 3538, 3453, 3376, 3307, 3244, 3186,
619     3133,  3037, 2952, 2877, 2809, 2747, 2690, 2638, 2589, 2501, 2423, 2353,
620     2290,  2232, 2179, 2130, 2084, 2001, 1928, 1862, 1802, 1748, 1698, 1651,
621     1608,  1530, 1460, 1398, 1342, 1290, 1243, 1199, 1159, 1086, 1021, 963,
622     911,   864,  821,  781,  745,  680,  623,  574,  530,  490,  455,  424,
623     395,   345,  304,  269,  239,  213,  190,  171,  154,  126,  104,  87,
624     73,    61,   52,   44,   38,   28,   21,   16,   12,   10,   8,    6,
625     5,     3,    2,    1,    1,    1,    0,    0,
626   };
627   // Normalized distortion:
628   // This table models the normalized distortion for a Laplacian source
629   // with given variance when quantized with a uniform quantizer
630   // with given stepsize. The closed form expression is:
631   // Dn(x) = 1 - 1/sqrt(2) * x / sinh(x/sqrt(2))
632   // where x = qpstep / sqrt(variance).
633   // Note the actual distortion is Dn * variance.
634   static const int dist_tab_q10[] = {
635     0,    0,    1,    1,    1,    2,    2,    2,    3,    3,    4,    5,
636     5,    6,    7,    7,    8,    9,    11,   12,   13,   15,   16,   17,
637     18,   21,   24,   26,   29,   31,   34,   36,   39,   44,   49,   54,
638     59,   64,   69,   73,   78,   88,   97,   106,  115,  124,  133,  142,
639     151,  167,  184,  200,  215,  231,  245,  260,  274,  301,  327,  351,
640     375,  397,  418,  439,  458,  495,  528,  559,  587,  613,  637,  659,
641     680,  717,  749,  777,  801,  823,  842,  859,  874,  899,  919,  936,
642     949,  960,  969,  977,  983,  994,  1001, 1006, 1010, 1013, 1015, 1017,
643     1018, 1020, 1022, 1022, 1023, 1023, 1023, 1024,
644   };
645   static const int xsq_iq_q10[] = {
646     0,      4,      8,      12,     16,     20,     24,     28,     32,
647     40,     48,     56,     64,     72,     80,     88,     96,     112,
648     128,    144,    160,    176,    192,    208,    224,    256,    288,
649     320,    352,    384,    416,    448,    480,    544,    608,    672,
650     736,    800,    864,    928,    992,    1120,   1248,   1376,   1504,
651     1632,   1760,   1888,   2016,   2272,   2528,   2784,   3040,   3296,
652     3552,   3808,   4064,   4576,   5088,   5600,   6112,   6624,   7136,
653     7648,   8160,   9184,   10208,  11232,  12256,  13280,  14304,  15328,
654     16352,  18400,  20448,  22496,  24544,  26592,  28640,  30688,  32736,
655     36832,  40928,  45024,  49120,  53216,  57312,  61408,  65504,  73696,
656     81888,  90080,  98272,  106464, 114656, 122848, 131040, 147424, 163808,
657     180192, 196576, 212960, 229344, 245728,
658   };
659   const int tmp = (xsq_q10 >> 2) + 8;
660   const int k = get_msb(tmp) - 3;
661   const int xq = (k << 3) + ((tmp >> k) & 0x7);
662   const int one_q10 = 1 << 10;
663   const int a_q10 = ((xsq_q10 - xsq_iq_q10[xq]) << 10) >> (2 + k);
664   const int b_q10 = one_q10 - a_q10;
665   *r_q10 = (rate_tab_q10[xq] * b_q10 + rate_tab_q10[xq + 1] * a_q10) >> 10;
666   *d_q10 = (dist_tab_q10[xq] * b_q10 + dist_tab_q10[xq + 1] * a_q10) >> 10;
667 }
668 
av1_model_rd_from_var_lapndz(int64_t var,unsigned int n_log2,unsigned int qstep,int * rate,int64_t * dist)669 void av1_model_rd_from_var_lapndz(int64_t var, unsigned int n_log2,
670                                   unsigned int qstep, int *rate,
671                                   int64_t *dist) {
672   // This function models the rate and distortion for a Laplacian
673   // source with given variance when quantized with a uniform quantizer
674   // with given stepsize. The closed form expressions are in:
675   // Hang and Chen, "Source Model for transform video coder and its
676   // application - Part I: Fundamental Theory", IEEE Trans. Circ.
677   // Sys. for Video Tech., April 1997.
678   if (var == 0) {
679     *rate = 0;
680     *dist = 0;
681   } else {
682     int d_q10, r_q10;
683     static const uint32_t MAX_XSQ_Q10 = 245727;
684     const uint64_t xsq_q10_64 =
685         (((uint64_t)qstep * qstep << (n_log2 + 10)) + (var >> 1)) / var;
686     const int xsq_q10 = (int)AOMMIN(xsq_q10_64, MAX_XSQ_Q10);
687     model_rd_norm(xsq_q10, &r_q10, &d_q10);
688     *rate = ROUND_POWER_OF_TWO(r_q10 << n_log2, 10 - AV1_PROB_COST_SHIFT);
689     *dist = (var * (int64_t)d_q10 + 512) >> 10;
690   }
691 }
692 
interp_cubic(const double * p,double x)693 static double interp_cubic(const double *p, double x) {
694   return p[1] + 0.5 * x *
695                     (p[2] - p[0] +
696                      x * (2.0 * p[0] - 5.0 * p[1] + 4.0 * p[2] - p[3] +
697                           x * (3.0 * (p[1] - p[2]) + p[3] - p[0])));
698 }
699 
700 /*
701 static double interp_bicubic(const double *p, int p_stride, double x,
702                              double y) {
703   double q[4];
704   q[0] = interp_cubic(p, x);
705   q[1] = interp_cubic(p + p_stride, x);
706   q[2] = interp_cubic(p + 2 * p_stride, x);
707   q[3] = interp_cubic(p + 3 * p_stride, x);
708   return interp_cubic(q, y);
709 }
710 */
711 
712 static const uint8_t bsize_curvfit_model_cat_lookup[BLOCK_SIZES_ALL] = {
713   0, 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 0, 0, 1, 1, 2, 2
714 };
715 
sse_norm_curvfit_model_cat_lookup(double sse_norm)716 static int sse_norm_curvfit_model_cat_lookup(double sse_norm) {
717   return (sse_norm > 16.0);
718 }
719 
720 // Models distortion by sse using a logistic function on
721 // l = log2(sse / q^2) as:
722 // dbysse = 16 / (1 + k exp(l + c))
get_dbysse_logistic(double l,double c,double k)723 static double get_dbysse_logistic(double l, double c, double k) {
724   const double A = 16.0;
725   const double dbysse = A / (1 + k * exp(l + c));
726   return dbysse;
727 }
728 
729 // Models rate using a clamped linear function on
730 // l = log2(sse / q^2) as:
731 // rate = max(0, a + b * l)
get_rate_clamplinear(double l,double a,double b)732 static double get_rate_clamplinear(double l, double a, double b) {
733   const double rate = a + b * l;
734   return (rate < 0 ? 0 : rate);
735 }
736 
737 static const uint8_t bsize_surffit_model_cat_lookup[BLOCK_SIZES_ALL] = {
738   0, 0, 0, 0, 1, 1, 2, 3, 3, 4, 5, 5, 6, 7, 7, 8, 0, 0, 2, 2, 4, 4
739 };
740 
741 static const double surffit_rate_params[9][4] = {
742   {
743       638.390212,
744       2.253108,
745       166.585650,
746       -3.939401,
747   },
748   {
749       5.256905,
750       81.997240,
751       -1.321771,
752       17.694216,
753   },
754   {
755       -74.193045,
756       72.431868,
757       -19.033152,
758       15.407276,
759   },
760   {
761       416.770113,
762       14.794188,
763       167.686830,
764       -6.997756,
765   },
766   {
767       378.511276,
768       9.558376,
769       154.658843,
770       -6.635663,
771   },
772   {
773       277.818787,
774       4.413180,
775       150.317637,
776       -9.893038,
777   },
778   {
779       142.212132,
780       11.542038,
781       94.393964,
782       -5.518517,
783   },
784   {
785       219.100256,
786       4.007421,
787       108.932852,
788       -6.981310,
789   },
790   {
791       222.261971,
792       3.251049,
793       95.972916,
794       -5.609789,
795   },
796 };
797 
798 static const double surffit_dist_params[7] = {
799   1.475844, 4.328362, -5.680233, -0.500994, 0.554585, 4.839478, -0.695837
800 };
801 
rate_surffit_model_params_lookup(BLOCK_SIZE bsize,double xm,double * rpar)802 static void rate_surffit_model_params_lookup(BLOCK_SIZE bsize, double xm,
803                                              double *rpar) {
804   const int cat = bsize_surffit_model_cat_lookup[bsize];
805   rpar[0] = surffit_rate_params[cat][0] + surffit_rate_params[cat][1] * xm;
806   rpar[1] = surffit_rate_params[cat][2] + surffit_rate_params[cat][3] * xm;
807 }
808 
dist_surffit_model_params_lookup(BLOCK_SIZE bsize,double xm,double * dpar)809 static void dist_surffit_model_params_lookup(BLOCK_SIZE bsize, double xm,
810                                              double *dpar) {
811   (void)bsize;
812   const double *params = surffit_dist_params;
813   dpar[0] = params[0] + params[1] / (1 + exp((xm + params[2]) * params[3]));
814   dpar[1] = params[4] + params[5] * exp(params[6] * xm);
815 }
816 
av1_model_rd_surffit(BLOCK_SIZE bsize,double sse_norm,double xm,double yl,double * rate_f,double * distbysse_f)817 void av1_model_rd_surffit(BLOCK_SIZE bsize, double sse_norm, double xm,
818                           double yl, double *rate_f, double *distbysse_f) {
819   (void)sse_norm;
820   double rpar[2], dpar[2];
821   rate_surffit_model_params_lookup(bsize, xm, rpar);
822   dist_surffit_model_params_lookup(bsize, xm, dpar);
823 
824   *rate_f = get_rate_clamplinear(yl, rpar[0], rpar[1]);
825   *distbysse_f = get_dbysse_logistic(yl, dpar[0], dpar[1]);
826 }
827 
828 static const double interp_rgrid_curv[4][65] = {
829   {
830       0.000000,    0.000000,    0.000000,    0.000000,    0.000000,
831       0.000000,    0.000000,    0.000000,    0.000000,    0.000000,
832       0.000000,    23.801499,   28.387688,   33.388795,   42.298282,
833       41.525408,   51.597692,   49.566271,   54.632979,   60.321507,
834       67.730678,   75.766165,   85.324032,   96.600012,   120.839562,
835       173.917577,  255.974908,  354.107573,  458.063476,  562.345966,
836       668.568424,  772.072881,  878.598490,  982.202274,  1082.708946,
837       1188.037853, 1287.702240, 1395.588773, 1490.825830, 1584.231230,
838       1691.386090, 1766.822555, 1869.630904, 1926.743565, 2002.949495,
839       2047.431137, 2138.486068, 2154.743767, 2209.242472, 2277.593051,
840       2290.996432, 2307.452938, 2343.567091, 2397.654644, 2469.425868,
841       2558.591037, 2664.860422, 2787.944296, 2927.552932, 3083.396602,
842       3255.185579, 3442.630134, 3645.440541, 3863.327072, 4096.000000,
843   },
844   {
845       0.000000,    0.000000,    0.000000,    0.000000,    0.000000,
846       0.000000,    0.000000,    0.000000,    0.000000,    0.000000,
847       0.000000,    8.998436,    9.439592,    9.731837,    10.865931,
848       11.561347,   12.578139,   14.205101,   16.770584,   19.094853,
849       21.330863,   23.298907,   26.901921,   34.501017,   57.891733,
850       112.234763,  194.853189,  288.302032,  380.499422,  472.625309,
851       560.226809,  647.928463,  734.155122,  817.489721,  906.265783,
852       999.260562,  1094.489206, 1197.062998, 1293.296825, 1378.926484,
853       1472.760990, 1552.663779, 1635.196884, 1692.451951, 1759.741063,
854       1822.162720, 1916.515921, 1966.686071, 2031.647506, 2033.700134,
855       2087.847688, 2161.688858, 2242.536028, 2334.023491, 2436.337802,
856       2549.665519, 2674.193198, 2810.107395, 2957.594666, 3116.841567,
857       3288.034655, 3471.360486, 3667.005616, 3875.156602, 4096.000000,
858   },
859   {
860       0.000000,    0.000000,    0.000000,    0.000000,    0.000000,
861       0.000000,    0.000000,    0.000000,    0.000000,    0.000000,
862       0.000000,    2.377584,    2.557185,    2.732445,    2.851114,
863       3.281800,    3.765589,    4.342578,    5.145582,    5.611038,
864       6.642238,    7.945977,    11.800522,   17.346624,   37.501413,
865       87.216800,   165.860942,  253.865564,  332.039345,  408.518863,
866       478.120452,  547.268590,  616.067676,  680.022540,  753.863541,
867       834.529973,  919.489191,  1008.264989, 1092.230318, 1173.971886,
868       1249.514122, 1330.510941, 1399.523249, 1466.923387, 1530.533471,
869       1586.515722, 1695.197774, 1746.648696, 1837.136959, 1909.075485,
870       1975.074651, 2060.159200, 2155.335095, 2259.762505, 2373.710437,
871       2497.447898, 2631.243895, 2775.367434, 2930.087523, 3095.673170,
872       3272.393380, 3460.517161, 3660.313520, 3872.051464, 4096.000000,
873   },
874   {
875       0.000000,    0.000000,    0.000000,    0.000000,    0.000000,
876       0.000000,    0.000000,    0.000000,    0.000000,    0.000000,
877       0.000000,    0.296997,    0.342545,    0.403097,    0.472889,
878       0.614483,    0.842937,    1.050824,    1.326663,    1.717750,
879       2.530591,    3.582302,    6.995373,    9.973335,    24.042464,
880       56.598240,   113.680735,  180.018689,  231.050567,  266.101082,
881       294.957934,  323.326511,  349.434429,  380.443211,  408.171987,
882       441.214916,  475.716772,  512.900000,  551.186939,  592.364455,
883       624.527378,  661.940693,  679.185473,  724.800679,  764.781792,
884       873.050019,  950.299001,  939.292954,  1052.406153, 1033.893184,
885       1112.182406, 1219.174326, 1337.296681, 1471.648357, 1622.492809,
886       1790.093491, 1974.713858, 2176.617364, 2396.067465, 2633.327614,
887       2888.661266, 3162.331876, 3454.602899, 3765.737789, 4096.000000,
888   },
889 };
890 
891 static const double interp_dgrid_curv[2][65] = {
892   {
893       16.000000, 15.962891, 15.925174, 15.886888, 15.848074, 15.808770,
894       15.769015, 15.728850, 15.688313, 15.647445, 15.606284, 15.564870,
895       15.525918, 15.483820, 15.373330, 15.126844, 14.637442, 14.184387,
896       13.560070, 12.880717, 12.165995, 11.378144, 10.438769, 9.130790,
897       7.487633,  5.688649,  4.267515,  3.196300,  2.434201,  1.834064,
898       1.369920,  1.035921,  0.775279,  0.574895,  0.427232,  0.314123,
899       0.233236,  0.171440,  0.128188,  0.092762,  0.067569,  0.049324,
900       0.036330,  0.027008,  0.019853,  0.015539,  0.011093,  0.008733,
901       0.007624,  0.008105,  0.005427,  0.004065,  0.003427,  0.002848,
902       0.002328,  0.001865,  0.001457,  0.001103,  0.000801,  0.000550,
903       0.000348,  0.000193,  0.000085,  0.000021,  0.000000,
904   },
905   {
906       16.000000, 15.996116, 15.984769, 15.966413, 15.941505, 15.910501,
907       15.873856, 15.832026, 15.785466, 15.734633, 15.679981, 15.621967,
908       15.560961, 15.460157, 15.288367, 15.052462, 14.466922, 13.921212,
909       13.073692, 12.222005, 11.237799, 9.985848,  8.898823,  7.423519,
910       5.995325,  4.773152,  3.744032,  2.938217,  2.294526,  1.762412,
911       1.327145,  1.020728,  0.765535,  0.570548,  0.425833,  0.313825,
912       0.232959,  0.171324,  0.128174,  0.092750,  0.067558,  0.049319,
913       0.036330,  0.027008,  0.019853,  0.015539,  0.011093,  0.008733,
914       0.007624,  0.008105,  0.005427,  0.004065,  0.003427,  0.002848,
915       0.002328,  0.001865,  0.001457,  0.001103,  0.000801,  0.000550,
916       0.000348,  0.000193,  0.000085,  0.000021,  -0.000000,
917   },
918 };
919 
av1_model_rd_curvfit(BLOCK_SIZE bsize,double sse_norm,double xqr,double * rate_f,double * distbysse_f)920 void av1_model_rd_curvfit(BLOCK_SIZE bsize, double sse_norm, double xqr,
921                           double *rate_f, double *distbysse_f) {
922   const double x_start = -15.5;
923   const double x_end = 16.5;
924   const double x_step = 0.5;
925   const double epsilon = 1e-6;
926   const int rcat = bsize_curvfit_model_cat_lookup[bsize];
927   const int dcat = sse_norm_curvfit_model_cat_lookup(sse_norm);
928   (void)x_end;
929 
930   xqr = AOMMAX(xqr, x_start + x_step + epsilon);
931   xqr = AOMMIN(xqr, x_end - x_step - epsilon);
932   const double x = (xqr - x_start) / x_step;
933   const int xi = (int)floor(x);
934   const double xo = x - xi;
935 
936   assert(xi > 0);
937 
938   const double *prate = &interp_rgrid_curv[rcat][(xi - 1)];
939   *rate_f = interp_cubic(prate, xo);
940   const double *pdist = &interp_dgrid_curv[dcat][(xi - 1)];
941   *distbysse_f = interp_cubic(pdist, xo);
942 }
943 
get_entropy_contexts_plane(BLOCK_SIZE plane_bsize,const struct macroblockd_plane * pd,ENTROPY_CONTEXT t_above[MAX_MIB_SIZE],ENTROPY_CONTEXT t_left[MAX_MIB_SIZE])944 static void get_entropy_contexts_plane(BLOCK_SIZE plane_bsize,
945                                        const struct macroblockd_plane *pd,
946                                        ENTROPY_CONTEXT t_above[MAX_MIB_SIZE],
947                                        ENTROPY_CONTEXT t_left[MAX_MIB_SIZE]) {
948   const int num_4x4_w = block_size_wide[plane_bsize] >> tx_size_wide_log2[0];
949   const int num_4x4_h = block_size_high[plane_bsize] >> tx_size_high_log2[0];
950   const ENTROPY_CONTEXT *const above = pd->above_context;
951   const ENTROPY_CONTEXT *const left = pd->left_context;
952 
953   memcpy(t_above, above, sizeof(ENTROPY_CONTEXT) * num_4x4_w);
954   memcpy(t_left, left, sizeof(ENTROPY_CONTEXT) * num_4x4_h);
955 }
956 
av1_get_entropy_contexts(BLOCK_SIZE bsize,const struct macroblockd_plane * pd,ENTROPY_CONTEXT t_above[MAX_MIB_SIZE],ENTROPY_CONTEXT t_left[MAX_MIB_SIZE])957 void av1_get_entropy_contexts(BLOCK_SIZE bsize,
958                               const struct macroblockd_plane *pd,
959                               ENTROPY_CONTEXT t_above[MAX_MIB_SIZE],
960                               ENTROPY_CONTEXT t_left[MAX_MIB_SIZE]) {
961   const BLOCK_SIZE plane_bsize =
962       get_plane_block_size(bsize, pd->subsampling_x, pd->subsampling_y);
963   get_entropy_contexts_plane(plane_bsize, pd, t_above, t_left);
964 }
965 
av1_mv_pred(const AV1_COMP * cpi,MACROBLOCK * x,uint8_t * ref_y_buffer,int ref_y_stride,int ref_frame,BLOCK_SIZE block_size)966 void av1_mv_pred(const AV1_COMP *cpi, MACROBLOCK *x, uint8_t *ref_y_buffer,
967                  int ref_y_stride, int ref_frame, BLOCK_SIZE block_size) {
968   int i;
969   int zero_seen = 0;
970   int best_sad = INT_MAX;
971   int this_sad = INT_MAX;
972   int max_mv = 0;
973   uint8_t *src_y_ptr = x->plane[0].src.buf;
974   uint8_t *ref_y_ptr;
975   MV pred_mv[MAX_MV_REF_CANDIDATES + 1];
976   int num_mv_refs = 0;
977   const MV_REFERENCE_FRAME ref_frames[2] = { ref_frame, NONE_FRAME };
978   const int_mv ref_mv =
979       av1_get_ref_mv_from_stack(0, ref_frames, 0, x->mbmi_ext);
980   const int_mv ref_mv1 =
981       av1_get_ref_mv_from_stack(0, ref_frames, 1, x->mbmi_ext);
982 
983   pred_mv[num_mv_refs++] = ref_mv.as_mv;
984   if (ref_mv.as_int != ref_mv1.as_int) {
985     pred_mv[num_mv_refs++] = ref_mv1.as_mv;
986   }
987   if (cpi->sf.adaptive_motion_search && block_size < x->max_partition_size)
988     pred_mv[num_mv_refs++] = x->pred_mv[ref_frame];
989 
990   assert(num_mv_refs <= (int)(sizeof(pred_mv) / sizeof(pred_mv[0])));
991 
992   // Get the sad for each candidate reference mv.
993   for (i = 0; i < num_mv_refs; ++i) {
994     const MV *this_mv = &pred_mv[i];
995     int fp_row, fp_col;
996     fp_row = (this_mv->row + 3 + (this_mv->row >= 0)) >> 3;
997     fp_col = (this_mv->col + 3 + (this_mv->col >= 0)) >> 3;
998     max_mv = AOMMAX(max_mv, AOMMAX(abs(this_mv->row), abs(this_mv->col)) >> 3);
999 
1000     if (fp_row == 0 && fp_col == 0 && zero_seen) continue;
1001     zero_seen |= (fp_row == 0 && fp_col == 0);
1002 
1003     ref_y_ptr = &ref_y_buffer[ref_y_stride * fp_row + fp_col];
1004     // Find sad for current vector.
1005     this_sad = cpi->fn_ptr[block_size].sdf(src_y_ptr, x->plane[0].src.stride,
1006                                            ref_y_ptr, ref_y_stride);
1007     // Note if it is the best so far.
1008     if (this_sad < best_sad) {
1009       best_sad = this_sad;
1010     }
1011   }
1012 
1013   // Note the index of the mv that worked best in the reference list.
1014   x->max_mv_context[ref_frame] = max_mv;
1015   x->pred_mv_sad[ref_frame] = best_sad;
1016 }
1017 
av1_setup_pred_block(const MACROBLOCKD * xd,struct buf_2d dst[MAX_MB_PLANE],const YV12_BUFFER_CONFIG * src,int mi_row,int mi_col,const struct scale_factors * scale,const struct scale_factors * scale_uv,const int num_planes)1018 void av1_setup_pred_block(const MACROBLOCKD *xd,
1019                           struct buf_2d dst[MAX_MB_PLANE],
1020                           const YV12_BUFFER_CONFIG *src, int mi_row, int mi_col,
1021                           const struct scale_factors *scale,
1022                           const struct scale_factors *scale_uv,
1023                           const int num_planes) {
1024   int i;
1025 
1026   dst[0].buf = src->y_buffer;
1027   dst[0].stride = src->y_stride;
1028   dst[1].buf = src->u_buffer;
1029   dst[2].buf = src->v_buffer;
1030   dst[1].stride = dst[2].stride = src->uv_stride;
1031 
1032   for (i = 0; i < num_planes; ++i) {
1033     setup_pred_plane(dst + i, xd->mi[0]->sb_type, dst[i].buf,
1034                      i ? src->uv_crop_width : src->y_crop_width,
1035                      i ? src->uv_crop_height : src->y_crop_height,
1036                      dst[i].stride, mi_row, mi_col, i ? scale_uv : scale,
1037                      xd->plane[i].subsampling_x, xd->plane[i].subsampling_y);
1038   }
1039 }
1040 
av1_raster_block_offset(BLOCK_SIZE plane_bsize,int raster_block,int stride)1041 int av1_raster_block_offset(BLOCK_SIZE plane_bsize, int raster_block,
1042                             int stride) {
1043   const int bw = mi_size_wide_log2[plane_bsize];
1044   const int y = 4 * (raster_block >> bw);
1045   const int x = 4 * (raster_block & ((1 << bw) - 1));
1046   return y * stride + x;
1047 }
1048 
av1_raster_block_offset_int16(BLOCK_SIZE plane_bsize,int raster_block,int16_t * base)1049 int16_t *av1_raster_block_offset_int16(BLOCK_SIZE plane_bsize, int raster_block,
1050                                        int16_t *base) {
1051   const int stride = block_size_wide[plane_bsize];
1052   return base + av1_raster_block_offset(plane_bsize, raster_block, stride);
1053 }
1054 
av1_get_scaled_ref_frame(const AV1_COMP * cpi,int ref_frame)1055 YV12_BUFFER_CONFIG *av1_get_scaled_ref_frame(const AV1_COMP *cpi,
1056                                              int ref_frame) {
1057   assert(ref_frame >= LAST_FRAME && ref_frame <= ALTREF_FRAME);
1058   RefCntBuffer *const scaled_buf = cpi->scaled_ref_buf[ref_frame - 1];
1059   const RefCntBuffer *const ref_buf =
1060       get_ref_frame_buf(&cpi->common, ref_frame);
1061   return (scaled_buf != ref_buf && scaled_buf != NULL) ? &scaled_buf->buf
1062                                                        : NULL;
1063 }
1064 
av1_get_switchable_rate(const AV1_COMMON * const cm,MACROBLOCK * x,const MACROBLOCKD * xd)1065 int av1_get_switchable_rate(const AV1_COMMON *const cm, MACROBLOCK *x,
1066                             const MACROBLOCKD *xd) {
1067   if (cm->interp_filter == SWITCHABLE) {
1068     const MB_MODE_INFO *const mbmi = xd->mi[0];
1069     int inter_filter_cost = 0;
1070     int dir;
1071 
1072     for (dir = 0; dir < 2; ++dir) {
1073       const int ctx = av1_get_pred_context_switchable_interp(xd, dir);
1074       const InterpFilter filter =
1075           av1_extract_interp_filter(mbmi->interp_filters, dir);
1076       inter_filter_cost += x->switchable_interp_costs[ctx][filter];
1077     }
1078     return SWITCHABLE_INTERP_RATE_FACTOR * inter_filter_cost;
1079   } else {
1080     return 0;
1081   }
1082 }
1083 
av1_set_rd_speed_thresholds(AV1_COMP * cpi)1084 void av1_set_rd_speed_thresholds(AV1_COMP *cpi) {
1085   int i;
1086   RD_OPT *const rd = &cpi->rd;
1087   SPEED_FEATURES *const sf = &cpi->sf;
1088 
1089   // Set baseline threshold values.
1090   for (i = 0; i < MAX_MODES; ++i) rd->thresh_mult[i] = cpi->oxcf.mode == 0;
1091 
1092   if (sf->adaptive_rd_thresh) {
1093     rd->thresh_mult[THR_NEARESTMV] = 300;
1094     rd->thresh_mult[THR_NEARESTL2] = 300;
1095     rd->thresh_mult[THR_NEARESTL3] = 300;
1096     rd->thresh_mult[THR_NEARESTB] = 300;
1097     rd->thresh_mult[THR_NEARESTA2] = 300;
1098     rd->thresh_mult[THR_NEARESTA] = 300;
1099     rd->thresh_mult[THR_NEARESTG] = 300;
1100   } else {
1101     rd->thresh_mult[THR_NEARESTMV] = 0;
1102     rd->thresh_mult[THR_NEARESTL2] = 0;
1103     rd->thresh_mult[THR_NEARESTL3] = 100;
1104     rd->thresh_mult[THR_NEARESTB] = 0;
1105     rd->thresh_mult[THR_NEARESTA2] = 0;
1106     rd->thresh_mult[THR_NEARESTA] = 0;
1107     rd->thresh_mult[THR_NEARESTG] = 0;
1108   }
1109 
1110   rd->thresh_mult[THR_NEWMV] += 1000;
1111   rd->thresh_mult[THR_NEWL2] += 1000;
1112   rd->thresh_mult[THR_NEWL3] += 1000;
1113   rd->thresh_mult[THR_NEWB] += 1000;
1114   rd->thresh_mult[THR_NEWA2] = 1100;
1115   rd->thresh_mult[THR_NEWA] += 1000;
1116   rd->thresh_mult[THR_NEWG] += 1000;
1117 
1118   rd->thresh_mult[THR_NEARMV] += 1000;
1119   rd->thresh_mult[THR_NEARL2] += 1000;
1120   rd->thresh_mult[THR_NEARL3] += 1000;
1121   rd->thresh_mult[THR_NEARB] += 1000;
1122   rd->thresh_mult[THR_NEARA2] = 1000;
1123   rd->thresh_mult[THR_NEARA] += 1000;
1124   rd->thresh_mult[THR_NEARG] += 1000;
1125 
1126   rd->thresh_mult[THR_GLOBALMV] += 2200;
1127   rd->thresh_mult[THR_GLOBALL2] += 2000;
1128   rd->thresh_mult[THR_GLOBALL3] += 2000;
1129   rd->thresh_mult[THR_GLOBALB] += 2400;
1130   rd->thresh_mult[THR_GLOBALA2] = 2000;
1131   rd->thresh_mult[THR_GLOBALG] += 2000;
1132   rd->thresh_mult[THR_GLOBALA] += 2400;
1133 
1134   rd->thresh_mult[THR_COMP_NEAREST_NEARESTLA] += 1100;
1135   rd->thresh_mult[THR_COMP_NEAREST_NEARESTL2A] += 1000;
1136   rd->thresh_mult[THR_COMP_NEAREST_NEARESTL3A] += 800;
1137   rd->thresh_mult[THR_COMP_NEAREST_NEARESTGA] += 900;
1138   rd->thresh_mult[THR_COMP_NEAREST_NEARESTLB] += 1000;
1139   rd->thresh_mult[THR_COMP_NEAREST_NEARESTL2B] += 1000;
1140   rd->thresh_mult[THR_COMP_NEAREST_NEARESTL3B] += 1000;
1141   rd->thresh_mult[THR_COMP_NEAREST_NEARESTGB] += 1000;
1142   rd->thresh_mult[THR_COMP_NEAREST_NEARESTLA2] += 1000;
1143   rd->thresh_mult[THR_COMP_NEAREST_NEARESTL2A2] += 1000;
1144   rd->thresh_mult[THR_COMP_NEAREST_NEARESTL3A2] += 1000;
1145   rd->thresh_mult[THR_COMP_NEAREST_NEARESTGA2] += 1000;
1146 
1147   rd->thresh_mult[THR_COMP_NEAREST_NEARESTLL2] += 2000;
1148   rd->thresh_mult[THR_COMP_NEAREST_NEARESTLL3] += 2000;
1149   rd->thresh_mult[THR_COMP_NEAREST_NEARESTLG] += 2000;
1150   rd->thresh_mult[THR_COMP_NEAREST_NEARESTBA] += 2000;
1151 
1152   rd->thresh_mult[THR_COMP_NEAR_NEARLA] += 1200;
1153   rd->thresh_mult[THR_COMP_NEAREST_NEWLA] += 1500;
1154   rd->thresh_mult[THR_COMP_NEW_NEARESTLA] += 1500;
1155   rd->thresh_mult[THR_COMP_NEAR_NEWLA] += 1530;
1156   rd->thresh_mult[THR_COMP_NEW_NEARLA] += 1870;
1157   rd->thresh_mult[THR_COMP_NEW_NEWLA] += 2400;
1158   rd->thresh_mult[THR_COMP_GLOBAL_GLOBALLA] += 2750;
1159 
1160   rd->thresh_mult[THR_COMP_NEAR_NEARL2A] += 1200;
1161   rd->thresh_mult[THR_COMP_NEAREST_NEWL2A] += 1500;
1162   rd->thresh_mult[THR_COMP_NEW_NEARESTL2A] += 1500;
1163   rd->thresh_mult[THR_COMP_NEAR_NEWL2A] += 1870;
1164   rd->thresh_mult[THR_COMP_NEW_NEARL2A] += 1700;
1165   rd->thresh_mult[THR_COMP_NEW_NEWL2A] += 1800;
1166   rd->thresh_mult[THR_COMP_GLOBAL_GLOBALL2A] += 2500;
1167 
1168   rd->thresh_mult[THR_COMP_NEAR_NEARL3A] += 1200;
1169   rd->thresh_mult[THR_COMP_NEAREST_NEWL3A] += 1500;
1170   rd->thresh_mult[THR_COMP_NEW_NEARESTL3A] += 1500;
1171   rd->thresh_mult[THR_COMP_NEAR_NEWL3A] += 1700;
1172   rd->thresh_mult[THR_COMP_NEW_NEARL3A] += 1700;
1173   rd->thresh_mult[THR_COMP_NEW_NEWL3A] += 2000;
1174   rd->thresh_mult[THR_COMP_GLOBAL_GLOBALL3A] += 3000;
1175 
1176   rd->thresh_mult[THR_COMP_NEAR_NEARGA] += 1320;
1177   rd->thresh_mult[THR_COMP_NEAREST_NEWGA] += 1500;
1178   rd->thresh_mult[THR_COMP_NEW_NEARESTGA] += 1500;
1179   rd->thresh_mult[THR_COMP_NEAR_NEWGA] += 2040;
1180   rd->thresh_mult[THR_COMP_NEW_NEARGA] += 1700;
1181   rd->thresh_mult[THR_COMP_NEW_NEWGA] += 2000;
1182   rd->thresh_mult[THR_COMP_GLOBAL_GLOBALGA] += 2250;
1183 
1184   rd->thresh_mult[THR_COMP_NEAR_NEARLB] += 1200;
1185   rd->thresh_mult[THR_COMP_NEAREST_NEWLB] += 1500;
1186   rd->thresh_mult[THR_COMP_NEW_NEARESTLB] += 1500;
1187   rd->thresh_mult[THR_COMP_NEAR_NEWLB] += 1360;
1188   rd->thresh_mult[THR_COMP_NEW_NEARLB] += 1700;
1189   rd->thresh_mult[THR_COMP_NEW_NEWLB] += 2400;
1190   rd->thresh_mult[THR_COMP_GLOBAL_GLOBALLB] += 2250;
1191 
1192   rd->thresh_mult[THR_COMP_NEAR_NEARL2B] += 1200;
1193   rd->thresh_mult[THR_COMP_NEAREST_NEWL2B] += 1500;
1194   rd->thresh_mult[THR_COMP_NEW_NEARESTL2B] += 1500;
1195   rd->thresh_mult[THR_COMP_NEAR_NEWL2B] += 1700;
1196   rd->thresh_mult[THR_COMP_NEW_NEARL2B] += 1700;
1197   rd->thresh_mult[THR_COMP_NEW_NEWL2B] += 2000;
1198   rd->thresh_mult[THR_COMP_GLOBAL_GLOBALL2B] += 2500;
1199 
1200   rd->thresh_mult[THR_COMP_NEAR_NEARL3B] += 1200;
1201   rd->thresh_mult[THR_COMP_NEAREST_NEWL3B] += 1500;
1202   rd->thresh_mult[THR_COMP_NEW_NEARESTL3B] += 1500;
1203   rd->thresh_mult[THR_COMP_NEAR_NEWL3B] += 1870;
1204   rd->thresh_mult[THR_COMP_NEW_NEARL3B] += 1700;
1205   rd->thresh_mult[THR_COMP_NEW_NEWL3B] += 2000;
1206   rd->thresh_mult[THR_COMP_GLOBAL_GLOBALL3B] += 2500;
1207 
1208   rd->thresh_mult[THR_COMP_NEAR_NEARGB] += 1200;
1209   rd->thresh_mult[THR_COMP_NEAREST_NEWGB] += 1500;
1210   rd->thresh_mult[THR_COMP_NEW_NEARESTGB] += 1500;
1211   rd->thresh_mult[THR_COMP_NEAR_NEWGB] += 1700;
1212   rd->thresh_mult[THR_COMP_NEW_NEARGB] += 1700;
1213   rd->thresh_mult[THR_COMP_NEW_NEWGB] += 2000;
1214   rd->thresh_mult[THR_COMP_GLOBAL_GLOBALGB] += 2500;
1215 
1216   rd->thresh_mult[THR_COMP_NEAR_NEARLA2] += 1200;
1217   rd->thresh_mult[THR_COMP_NEAREST_NEWLA2] += 1800;
1218   rd->thresh_mult[THR_COMP_NEW_NEARESTLA2] += 1500;
1219   rd->thresh_mult[THR_COMP_NEAR_NEWLA2] += 1700;
1220   rd->thresh_mult[THR_COMP_NEW_NEARLA2] += 1700;
1221   rd->thresh_mult[THR_COMP_NEW_NEWLA2] += 2000;
1222   rd->thresh_mult[THR_COMP_GLOBAL_GLOBALLA2] += 2500;
1223 
1224   rd->thresh_mult[THR_COMP_NEAR_NEARL2A2] += 1200;
1225   rd->thresh_mult[THR_COMP_NEAREST_NEWL2A2] += 1500;
1226   rd->thresh_mult[THR_COMP_NEW_NEARESTL2A2] += 1500;
1227   rd->thresh_mult[THR_COMP_NEAR_NEWL2A2] += 1700;
1228   rd->thresh_mult[THR_COMP_NEW_NEARL2A2] += 1700;
1229   rd->thresh_mult[THR_COMP_NEW_NEWL2A2] += 2000;
1230   rd->thresh_mult[THR_COMP_GLOBAL_GLOBALL2A2] += 2500;
1231 
1232   rd->thresh_mult[THR_COMP_NEAR_NEARL3A2] += 1440;
1233   rd->thresh_mult[THR_COMP_NEAREST_NEWL3A2] += 1500;
1234   rd->thresh_mult[THR_COMP_NEW_NEARESTL3A2] += 1500;
1235   rd->thresh_mult[THR_COMP_NEAR_NEWL3A2] += 1700;
1236   rd->thresh_mult[THR_COMP_NEW_NEARL3A2] += 1700;
1237   rd->thresh_mult[THR_COMP_NEW_NEWL3A2] += 2000;
1238   rd->thresh_mult[THR_COMP_GLOBAL_GLOBALL3A2] += 2500;
1239 
1240   rd->thresh_mult[THR_COMP_NEAR_NEARGA2] += 1200;
1241   rd->thresh_mult[THR_COMP_NEAREST_NEWGA2] += 1500;
1242   rd->thresh_mult[THR_COMP_NEW_NEARESTGA2] += 1500;
1243   rd->thresh_mult[THR_COMP_NEAR_NEWGA2] += 1700;
1244   rd->thresh_mult[THR_COMP_NEW_NEARGA2] += 1700;
1245   rd->thresh_mult[THR_COMP_NEW_NEWGA2] += 2000;
1246   rd->thresh_mult[THR_COMP_GLOBAL_GLOBALGA2] += 2750;
1247 
1248   rd->thresh_mult[THR_COMP_NEAR_NEARLL2] += 1600;
1249   rd->thresh_mult[THR_COMP_NEAREST_NEWLL2] += 2000;
1250   rd->thresh_mult[THR_COMP_NEW_NEARESTLL2] += 2000;
1251   rd->thresh_mult[THR_COMP_NEAR_NEWLL2] += 2640;
1252   rd->thresh_mult[THR_COMP_NEW_NEARLL2] += 2200;
1253   rd->thresh_mult[THR_COMP_NEW_NEWLL2] += 2400;
1254   rd->thresh_mult[THR_COMP_GLOBAL_GLOBALLL2] += 3200;
1255 
1256   rd->thresh_mult[THR_COMP_NEAR_NEARLL3] += 1600;
1257   rd->thresh_mult[THR_COMP_NEAREST_NEWLL3] += 2000;
1258   rd->thresh_mult[THR_COMP_NEW_NEARESTLL3] += 1800;
1259   rd->thresh_mult[THR_COMP_NEAR_NEWLL3] += 2200;
1260   rd->thresh_mult[THR_COMP_NEW_NEARLL3] += 2200;
1261   rd->thresh_mult[THR_COMP_NEW_NEWLL3] += 2400;
1262   rd->thresh_mult[THR_COMP_GLOBAL_GLOBALLL3] += 3200;
1263 
1264   rd->thresh_mult[THR_COMP_NEAR_NEARLG] += 1760;
1265   rd->thresh_mult[THR_COMP_NEAREST_NEWLG] += 2400;
1266   rd->thresh_mult[THR_COMP_NEW_NEARESTLG] += 2000;
1267   rd->thresh_mult[THR_COMP_NEAR_NEWLG] += 1760;
1268   rd->thresh_mult[THR_COMP_NEW_NEARLG] += 2640;
1269   rd->thresh_mult[THR_COMP_NEW_NEWLG] += 2400;
1270   rd->thresh_mult[THR_COMP_GLOBAL_GLOBALLG] += 3200;
1271 
1272   rd->thresh_mult[THR_COMP_NEAR_NEARBA] += 1600;
1273   rd->thresh_mult[THR_COMP_NEAREST_NEWBA] += 2000;
1274   rd->thresh_mult[THR_COMP_NEW_NEARESTBA] += 2000;
1275   rd->thresh_mult[THR_COMP_NEAR_NEWBA] += 2200;
1276   rd->thresh_mult[THR_COMP_NEW_NEARBA] += 1980;
1277   rd->thresh_mult[THR_COMP_NEW_NEWBA] += 2640;
1278   rd->thresh_mult[THR_COMP_GLOBAL_GLOBALBA] += 3200;
1279 
1280   rd->thresh_mult[THR_DC] += 1000;
1281   rd->thresh_mult[THR_PAETH] += 1000;
1282   rd->thresh_mult[THR_SMOOTH] += 2200;
1283   rd->thresh_mult[THR_SMOOTH_V] += 2000;
1284   rd->thresh_mult[THR_SMOOTH_H] += 2000;
1285   rd->thresh_mult[THR_H_PRED] += 2000;
1286   rd->thresh_mult[THR_V_PRED] += 1800;
1287   rd->thresh_mult[THR_D135_PRED] += 2500;
1288   rd->thresh_mult[THR_D203_PRED] += 2000;
1289   rd->thresh_mult[THR_D157_PRED] += 2500;
1290   rd->thresh_mult[THR_D67_PRED] += 2000;
1291   rd->thresh_mult[THR_D113_PRED] += 2500;
1292   rd->thresh_mult[THR_D45_PRED] += 2500;
1293 }
1294 
av1_update_rd_thresh_fact(const AV1_COMMON * const cm,int (* factor_buf)[MAX_MODES],int rd_thresh,int bsize,int best_mode_index)1295 void av1_update_rd_thresh_fact(const AV1_COMMON *const cm,
1296                                int (*factor_buf)[MAX_MODES], int rd_thresh,
1297                                int bsize, int best_mode_index) {
1298   if (rd_thresh > 0) {
1299     const int top_mode = MAX_MODES;
1300     int mode;
1301     for (mode = 0; mode < top_mode; ++mode) {
1302       const BLOCK_SIZE min_size = AOMMAX(bsize - 1, BLOCK_4X4);
1303       const BLOCK_SIZE max_size =
1304           AOMMIN(bsize + 2, (int)cm->seq_params.sb_size);
1305       BLOCK_SIZE bs;
1306       for (bs = min_size; bs <= max_size; ++bs) {
1307         int *const fact = &factor_buf[bs][mode];
1308         if (mode == best_mode_index) {
1309           *fact -= (*fact >> 4);
1310         } else {
1311           *fact = AOMMIN(*fact + RD_THRESH_INC, rd_thresh * RD_THRESH_MAX_FACT);
1312         }
1313       }
1314     }
1315   }
1316 }
1317 
av1_get_intra_cost_penalty(int qindex,int qdelta,aom_bit_depth_t bit_depth)1318 int av1_get_intra_cost_penalty(int qindex, int qdelta,
1319                                aom_bit_depth_t bit_depth) {
1320   const int q = av1_dc_quant_Q3(qindex, qdelta, bit_depth);
1321   switch (bit_depth) {
1322     case AOM_BITS_8: return 20 * q;
1323     case AOM_BITS_10: return 5 * q;
1324     case AOM_BITS_12: return ROUND_POWER_OF_TWO(5 * q, 2);
1325     default:
1326       assert(0 && "bit_depth should be AOM_BITS_8, AOM_BITS_10 or AOM_BITS_12");
1327       return -1;
1328   }
1329 }
1330