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]
56                                             [EXT_TX_SIZES] = {
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]
63                                             [EXT_TX_SIZES] = {
64                                               { 1, 1, 1, 1 },  // unused
65                                               { 1, 1, 0, 0 },
66                                               { 0, 0, 1, 0 },
67                                               { 0, 1, 1, 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 (av1_is_wedge_used(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_bit_lut_8[QINDEX_RANGE];
316 static int sad_per_bit_lut_10[QINDEX_RANGE];
317 static int sad_per_bit_lut_12[QINDEX_RANGE];
318 
init_me_luts_bd(int * bit16lut,int range,aom_bit_depth_t bit_depth)319 static void init_me_luts_bd(int *bit16lut, int range,
320                             aom_bit_depth_t bit_depth) {
321   int i;
322   // Initialize the sad lut tables using a formulaic calculation for now.
323   // This is to make it easier to resolve the impact of experimental changes
324   // to the quantizer tables.
325   for (i = 0; i < range; i++) {
326     const double q = av1_convert_qindex_to_q(i, bit_depth);
327     bit16lut[i] = (int)(0.0418 * q + 2.4107);
328   }
329 }
330 
av1_init_me_luts(void)331 void av1_init_me_luts(void) {
332   init_me_luts_bd(sad_per_bit_lut_8, QINDEX_RANGE, AOM_BITS_8);
333   init_me_luts_bd(sad_per_bit_lut_10, QINDEX_RANGE, AOM_BITS_10);
334   init_me_luts_bd(sad_per_bit_lut_12, QINDEX_RANGE, AOM_BITS_12);
335 }
336 
337 static const int rd_boost_factor[16] = { 64, 32, 32, 32, 24, 16, 12, 12,
338                                          8,  8,  4,  4,  2,  2,  1,  0 };
339 static const int rd_frame_type_factor[FRAME_UPDATE_TYPES] = { 128, 144, 128,
340                                                               128, 144, 144,
341                                                               128 };
342 
av1_compute_rd_mult_based_on_qindex(const AV1_COMP * cpi,int qindex)343 int av1_compute_rd_mult_based_on_qindex(const AV1_COMP *cpi, int qindex) {
344   const int q = av1_dc_quant_QTX(qindex, 0, cpi->common.seq_params.bit_depth);
345   int rdmult = q * q;
346   rdmult = rdmult * 3 + (rdmult * 2 / 3);
347   switch (cpi->common.seq_params.bit_depth) {
348     case AOM_BITS_8: break;
349     case AOM_BITS_10: rdmult = ROUND_POWER_OF_TWO(rdmult, 4); break;
350     case AOM_BITS_12: rdmult = ROUND_POWER_OF_TWO(rdmult, 8); break;
351     default:
352       assert(0 && "bit_depth should be AOM_BITS_8, AOM_BITS_10 or AOM_BITS_12");
353       return -1;
354   }
355   return rdmult > 0 ? rdmult : 1;
356 }
357 
av1_compute_rd_mult(const AV1_COMP * cpi,int qindex)358 int av1_compute_rd_mult(const AV1_COMP *cpi, int qindex) {
359   int64_t rdmult = av1_compute_rd_mult_based_on_qindex(cpi, qindex);
360   if (is_stat_consumption_stage(cpi) &&
361       (cpi->common.current_frame.frame_type != KEY_FRAME)) {
362     const GF_GROUP *const gf_group = &cpi->gf_group;
363     const FRAME_UPDATE_TYPE frame_type = gf_group->update_type[gf_group->index];
364     const int boost_index = AOMMIN(15, (cpi->rc.gfu_boost / 100));
365 
366     rdmult = (rdmult * rd_frame_type_factor[frame_type]) >> 7;
367     rdmult += ((rdmult * rd_boost_factor[boost_index]) >> 7);
368   }
369   return (int)rdmult;
370 }
371 
av1_get_deltaq_offset(const AV1_COMP * cpi,int qindex,double beta)372 int av1_get_deltaq_offset(const AV1_COMP *cpi, int qindex, double beta) {
373   assert(beta > 0.0);
374   int q = av1_dc_quant_QTX(qindex, 0, cpi->common.seq_params.bit_depth);
375   int newq = (int)rint(q / sqrt(beta));
376   int orig_qindex = qindex;
377   if (newq < q) {
378     do {
379       qindex--;
380       q = av1_dc_quant_QTX(qindex, 0, cpi->common.seq_params.bit_depth);
381     } while (newq < q && qindex > 0);
382   } else {
383     do {
384       qindex++;
385       q = av1_dc_quant_QTX(qindex, 0, cpi->common.seq_params.bit_depth);
386     } while (newq > q && qindex < MAXQ);
387   }
388   return qindex - orig_qindex;
389 }
390 
av1_get_adaptive_rdmult(const AV1_COMP * cpi,double beta)391 int av1_get_adaptive_rdmult(const AV1_COMP *cpi, double beta) {
392   assert(beta > 0.0);
393   const AV1_COMMON *cm = &cpi->common;
394   int64_t q = av1_dc_quant_QTX(cm->quant_params.base_qindex, 0,
395                                cm->seq_params.bit_depth);
396   int64_t rdmult = 0;
397 
398   switch (cm->seq_params.bit_depth) {
399     case AOM_BITS_8: rdmult = (int)((88 * q * q / beta) / 24); break;
400     case AOM_BITS_10:
401       rdmult = ROUND_POWER_OF_TWO((int)((88 * q * q / beta) / 24), 4);
402       break;
403     default:
404       assert(cm->seq_params.bit_depth == AOM_BITS_12);
405       rdmult = ROUND_POWER_OF_TWO((int)((88 * q * q / beta) / 24), 8);
406       break;
407   }
408 
409   if (is_stat_consumption_stage(cpi) &&
410       (cm->current_frame.frame_type != KEY_FRAME)) {
411     const GF_GROUP *const gf_group = &cpi->gf_group;
412     const FRAME_UPDATE_TYPE frame_type = gf_group->update_type[gf_group->index];
413     const int boost_index = AOMMIN(15, (cpi->rc.gfu_boost / 100));
414 
415     rdmult = (rdmult * rd_frame_type_factor[frame_type]) >> 7;
416     rdmult += ((rdmult * rd_boost_factor[boost_index]) >> 7);
417   }
418   if (rdmult < 1) rdmult = 1;
419   return (int)rdmult;
420 }
421 
compute_rd_thresh_factor(int qindex,aom_bit_depth_t bit_depth)422 static int compute_rd_thresh_factor(int qindex, aom_bit_depth_t bit_depth) {
423   double q;
424   switch (bit_depth) {
425     case AOM_BITS_8: q = av1_dc_quant_QTX(qindex, 0, AOM_BITS_8) / 4.0; break;
426     case AOM_BITS_10:
427       q = av1_dc_quant_QTX(qindex, 0, AOM_BITS_10) / 16.0;
428       break;
429     case AOM_BITS_12:
430       q = av1_dc_quant_QTX(qindex, 0, AOM_BITS_12) / 64.0;
431       break;
432     default:
433       assert(0 && "bit_depth should be AOM_BITS_8, AOM_BITS_10 or AOM_BITS_12");
434       return -1;
435   }
436   // TODO(debargha): Adjust the function below.
437   return AOMMAX((int)(pow(q, RD_THRESH_POW) * 5.12), 8);
438 }
439 
av1_initialize_me_consts(const AV1_COMP * cpi,MACROBLOCK * x,int qindex)440 void av1_initialize_me_consts(const AV1_COMP *cpi, MACROBLOCK *x, int qindex) {
441   switch (cpi->common.seq_params.bit_depth) {
442     case AOM_BITS_8: x->sadperbit = sad_per_bit_lut_8[qindex]; break;
443     case AOM_BITS_10: x->sadperbit = sad_per_bit_lut_10[qindex]; break;
444     case AOM_BITS_12: x->sadperbit = sad_per_bit_lut_12[qindex]; break;
445     default:
446       assert(0 && "bit_depth should be AOM_BITS_8, AOM_BITS_10 or AOM_BITS_12");
447   }
448 }
449 
set_block_thresholds(const AV1_COMMON * cm,RD_OPT * rd)450 static void set_block_thresholds(const AV1_COMMON *cm, RD_OPT *rd) {
451   int i, bsize, segment_id;
452 
453   for (segment_id = 0; segment_id < MAX_SEGMENTS; ++segment_id) {
454     const int qindex = clamp(
455         av1_get_qindex(&cm->seg, segment_id, cm->quant_params.base_qindex) +
456             cm->quant_params.y_dc_delta_q,
457         0, MAXQ);
458     const int q = compute_rd_thresh_factor(qindex, cm->seq_params.bit_depth);
459 
460     for (bsize = 0; bsize < BLOCK_SIZES_ALL; ++bsize) {
461       // Threshold here seems unnecessarily harsh but fine given actual
462       // range of values used for cpi->sf.thresh_mult[].
463       const int t = q * rd_thresh_block_size_factor[bsize];
464       const int thresh_max = INT_MAX / t;
465 
466       for (i = 0; i < MAX_MODES; ++i)
467         rd->threshes[segment_id][bsize][i] = rd->thresh_mult[i] < thresh_max
468                                                  ? rd->thresh_mult[i] * t / 4
469                                                  : INT_MAX;
470     }
471   }
472 }
473 
av1_fill_coeff_costs(MACROBLOCK * x,FRAME_CONTEXT * fc,const int num_planes)474 void av1_fill_coeff_costs(MACROBLOCK *x, FRAME_CONTEXT *fc,
475                           const int num_planes) {
476   const int nplanes = AOMMIN(num_planes, PLANE_TYPES);
477   for (int eob_multi_size = 0; eob_multi_size < 7; ++eob_multi_size) {
478     for (int plane = 0; plane < nplanes; ++plane) {
479       LV_MAP_EOB_COST *pcost = &x->eob_costs[eob_multi_size][plane];
480 
481       for (int ctx = 0; ctx < 2; ++ctx) {
482         aom_cdf_prob *pcdf;
483         switch (eob_multi_size) {
484           case 0: pcdf = fc->eob_flag_cdf16[plane][ctx]; break;
485           case 1: pcdf = fc->eob_flag_cdf32[plane][ctx]; break;
486           case 2: pcdf = fc->eob_flag_cdf64[plane][ctx]; break;
487           case 3: pcdf = fc->eob_flag_cdf128[plane][ctx]; break;
488           case 4: pcdf = fc->eob_flag_cdf256[plane][ctx]; break;
489           case 5: pcdf = fc->eob_flag_cdf512[plane][ctx]; break;
490           case 6:
491           default: pcdf = fc->eob_flag_cdf1024[plane][ctx]; break;
492         }
493         av1_cost_tokens_from_cdf(pcost->eob_cost[ctx], pcdf, NULL);
494       }
495     }
496   }
497   for (int tx_size = 0; tx_size < TX_SIZES; ++tx_size) {
498     for (int plane = 0; plane < nplanes; ++plane) {
499       LV_MAP_COEFF_COST *pcost = &x->coeff_costs[tx_size][plane];
500 
501       for (int ctx = 0; ctx < TXB_SKIP_CONTEXTS; ++ctx)
502         av1_cost_tokens_from_cdf(pcost->txb_skip_cost[ctx],
503                                  fc->txb_skip_cdf[tx_size][ctx], NULL);
504 
505       for (int ctx = 0; ctx < SIG_COEF_CONTEXTS_EOB; ++ctx)
506         av1_cost_tokens_from_cdf(pcost->base_eob_cost[ctx],
507                                  fc->coeff_base_eob_cdf[tx_size][plane][ctx],
508                                  NULL);
509       for (int ctx = 0; ctx < SIG_COEF_CONTEXTS; ++ctx)
510         av1_cost_tokens_from_cdf(pcost->base_cost[ctx],
511                                  fc->coeff_base_cdf[tx_size][plane][ctx], NULL);
512 
513       for (int ctx = 0; ctx < SIG_COEF_CONTEXTS; ++ctx) {
514         pcost->base_cost[ctx][4] = 0;
515         pcost->base_cost[ctx][5] = pcost->base_cost[ctx][1] +
516                                    av1_cost_literal(1) -
517                                    pcost->base_cost[ctx][0];
518         pcost->base_cost[ctx][6] =
519             pcost->base_cost[ctx][2] - pcost->base_cost[ctx][1];
520         pcost->base_cost[ctx][7] =
521             pcost->base_cost[ctx][3] - pcost->base_cost[ctx][2];
522       }
523 
524       for (int ctx = 0; ctx < EOB_COEF_CONTEXTS; ++ctx)
525         av1_cost_tokens_from_cdf(pcost->eob_extra_cost[ctx],
526                                  fc->eob_extra_cdf[tx_size][plane][ctx], NULL);
527 
528       for (int ctx = 0; ctx < DC_SIGN_CONTEXTS; ++ctx)
529         av1_cost_tokens_from_cdf(pcost->dc_sign_cost[ctx],
530                                  fc->dc_sign_cdf[plane][ctx], NULL);
531 
532       for (int ctx = 0; ctx < LEVEL_CONTEXTS; ++ctx) {
533         int br_rate[BR_CDF_SIZE];
534         int prev_cost = 0;
535         int i, j;
536         av1_cost_tokens_from_cdf(
537             br_rate, fc->coeff_br_cdf[AOMMIN(tx_size, TX_32X32)][plane][ctx],
538             NULL);
539         // printf("br_rate: ");
540         // for(j = 0; j < BR_CDF_SIZE; j++)
541         //  printf("%4d ", br_rate[j]);
542         // printf("\n");
543         for (i = 0; i < COEFF_BASE_RANGE; i += BR_CDF_SIZE - 1) {
544           for (j = 0; j < BR_CDF_SIZE - 1; j++) {
545             pcost->lps_cost[ctx][i + j] = prev_cost + br_rate[j];
546           }
547           prev_cost += br_rate[j];
548         }
549         pcost->lps_cost[ctx][i] = prev_cost;
550         // printf("lps_cost: %d %d %2d : ", tx_size, plane, ctx);
551         // for (i = 0; i <= COEFF_BASE_RANGE; i++)
552         //  printf("%5d ", pcost->lps_cost[ctx][i]);
553         // printf("\n");
554       }
555       for (int ctx = 0; ctx < LEVEL_CONTEXTS; ++ctx) {
556         pcost->lps_cost[ctx][0 + COEFF_BASE_RANGE + 1] =
557             pcost->lps_cost[ctx][0];
558         for (int i = 1; i <= COEFF_BASE_RANGE; ++i) {
559           pcost->lps_cost[ctx][i + COEFF_BASE_RANGE + 1] =
560               pcost->lps_cost[ctx][i] - pcost->lps_cost[ctx][i - 1];
561         }
562       }
563     }
564   }
565 }
566 
av1_fill_mv_costs(const FRAME_CONTEXT * fc,int integer_mv,int usehp,MACROBLOCK * x)567 void av1_fill_mv_costs(const FRAME_CONTEXT *fc, int integer_mv, int usehp,
568                        MACROBLOCK *x) {
569   x->nmvcost[0] = &x->nmv_costs[0][MV_MAX];
570   x->nmvcost[1] = &x->nmv_costs[1][MV_MAX];
571   x->nmvcost_hp[0] = &x->nmv_costs_hp[0][MV_MAX];
572   x->nmvcost_hp[1] = &x->nmv_costs_hp[1][MV_MAX];
573   if (integer_mv) {
574     av1_build_nmv_cost_table(x->nmv_vec_cost, x->nmvcost, &fc->nmvc,
575                              MV_SUBPEL_NONE);
576     x->mv_cost_stack = (int **)&x->nmvcost;
577   } else {
578     int *(*src)[2] = usehp ? &x->nmvcost_hp : &x->nmvcost;
579     x->mv_cost_stack = *src;
580     av1_build_nmv_cost_table(
581         x->nmv_vec_cost, usehp ? x->nmvcost_hp : x->nmvcost, &fc->nmvc, usehp);
582   }
583 }
584 
av1_initialize_rd_consts(AV1_COMP * cpi)585 void av1_initialize_rd_consts(AV1_COMP *cpi) {
586   AV1_COMMON *const cm = &cpi->common;
587   MACROBLOCK *const x = &cpi->td.mb;
588   RD_OPT *const rd = &cpi->rd;
589 
590   aom_clear_system_state();
591 
592   rd->RDMULT = av1_compute_rd_mult(
593       cpi, cm->quant_params.base_qindex + cm->quant_params.y_dc_delta_q);
594 
595   set_error_per_bit(x, rd->RDMULT);
596 
597   set_block_thresholds(cm, rd);
598 
599   if ((!cpi->sf.rt_sf.use_nonrd_pick_mode &&
600        cpi->oxcf.mv_cost_upd_freq != COST_UPD_OFF) ||
601       frame_is_intra_only(cm) || (cm->current_frame.frame_number & 0x07) == 1)
602     av1_fill_mv_costs(cm->fc, cm->features.cur_frame_force_integer_mv,
603                       cm->features.allow_high_precision_mv, x);
604 
605   if (!cpi->sf.rt_sf.use_nonrd_pick_mode && frame_is_intra_only(cm) &&
606       cm->features.allow_screen_content_tools &&
607       !is_stat_generation_stage(cpi)) {
608     IntraBCMVCosts *const dv_costs = &cpi->dv_costs;
609     int *dvcost[2] = { &dv_costs->mv_component[0][MV_MAX],
610                        &dv_costs->mv_component[1][MV_MAX] };
611     av1_build_nmv_cost_table(dv_costs->joint_mv, dvcost, &cm->fc->ndvc,
612                              MV_SUBPEL_NONE);
613   }
614 
615   if (!is_stat_generation_stage(cpi)) {
616     for (int i = 0; i < TRANS_TYPES; ++i)
617       // IDENTITY: 1 bit
618       // TRANSLATION: 3 bits
619       // ROTZOOM: 2 bits
620       // AFFINE: 3 bits
621       cpi->gm_info.type_cost[i] = (1 + (i > 0 ? (i == ROTZOOM ? 1 : 2) : 0))
622                                   << AV1_PROB_COST_SHIFT;
623   }
624 }
625 
model_rd_norm(int xsq_q10,int * r_q10,int * d_q10)626 static void model_rd_norm(int xsq_q10, int *r_q10, int *d_q10) {
627   // NOTE: The tables below must be of the same size.
628 
629   // The functions described below are sampled at the four most significant
630   // bits of x^2 + 8 / 256.
631 
632   // Normalized rate:
633   // This table models the rate for a Laplacian source with given variance
634   // when quantized with a uniform quantizer with given stepsize. The
635   // closed form expression is:
636   // Rn(x) = H(sqrt(r)) + sqrt(r)*[1 + H(r)/(1 - r)],
637   // where r = exp(-sqrt(2) * x) and x = qpstep / sqrt(variance),
638   // and H(x) is the binary entropy function.
639   static const int rate_tab_q10[] = {
640     65536, 6086, 5574, 5275, 5063, 4899, 4764, 4651, 4553, 4389, 4255, 4142,
641     4044,  3958, 3881, 3811, 3748, 3635, 3538, 3453, 3376, 3307, 3244, 3186,
642     3133,  3037, 2952, 2877, 2809, 2747, 2690, 2638, 2589, 2501, 2423, 2353,
643     2290,  2232, 2179, 2130, 2084, 2001, 1928, 1862, 1802, 1748, 1698, 1651,
644     1608,  1530, 1460, 1398, 1342, 1290, 1243, 1199, 1159, 1086, 1021, 963,
645     911,   864,  821,  781,  745,  680,  623,  574,  530,  490,  455,  424,
646     395,   345,  304,  269,  239,  213,  190,  171,  154,  126,  104,  87,
647     73,    61,   52,   44,   38,   28,   21,   16,   12,   10,   8,    6,
648     5,     3,    2,    1,    1,    1,    0,    0,
649   };
650   // Normalized distortion:
651   // This table models the normalized distortion for a Laplacian source
652   // with given variance when quantized with a uniform quantizer
653   // with given stepsize. The closed form expression is:
654   // Dn(x) = 1 - 1/sqrt(2) * x / sinh(x/sqrt(2))
655   // where x = qpstep / sqrt(variance).
656   // Note the actual distortion is Dn * variance.
657   static const int dist_tab_q10[] = {
658     0,    0,    1,    1,    1,    2,    2,    2,    3,    3,    4,    5,
659     5,    6,    7,    7,    8,    9,    11,   12,   13,   15,   16,   17,
660     18,   21,   24,   26,   29,   31,   34,   36,   39,   44,   49,   54,
661     59,   64,   69,   73,   78,   88,   97,   106,  115,  124,  133,  142,
662     151,  167,  184,  200,  215,  231,  245,  260,  274,  301,  327,  351,
663     375,  397,  418,  439,  458,  495,  528,  559,  587,  613,  637,  659,
664     680,  717,  749,  777,  801,  823,  842,  859,  874,  899,  919,  936,
665     949,  960,  969,  977,  983,  994,  1001, 1006, 1010, 1013, 1015, 1017,
666     1018, 1020, 1022, 1022, 1023, 1023, 1023, 1024,
667   };
668   static const int xsq_iq_q10[] = {
669     0,      4,      8,      12,     16,     20,     24,     28,     32,
670     40,     48,     56,     64,     72,     80,     88,     96,     112,
671     128,    144,    160,    176,    192,    208,    224,    256,    288,
672     320,    352,    384,    416,    448,    480,    544,    608,    672,
673     736,    800,    864,    928,    992,    1120,   1248,   1376,   1504,
674     1632,   1760,   1888,   2016,   2272,   2528,   2784,   3040,   3296,
675     3552,   3808,   4064,   4576,   5088,   5600,   6112,   6624,   7136,
676     7648,   8160,   9184,   10208,  11232,  12256,  13280,  14304,  15328,
677     16352,  18400,  20448,  22496,  24544,  26592,  28640,  30688,  32736,
678     36832,  40928,  45024,  49120,  53216,  57312,  61408,  65504,  73696,
679     81888,  90080,  98272,  106464, 114656, 122848, 131040, 147424, 163808,
680     180192, 196576, 212960, 229344, 245728,
681   };
682   const int tmp = (xsq_q10 >> 2) + 8;
683   const int k = get_msb(tmp) - 3;
684   const int xq = (k << 3) + ((tmp >> k) & 0x7);
685   const int one_q10 = 1 << 10;
686   const int a_q10 = ((xsq_q10 - xsq_iq_q10[xq]) << 10) >> (2 + k);
687   const int b_q10 = one_q10 - a_q10;
688   *r_q10 = (rate_tab_q10[xq] * b_q10 + rate_tab_q10[xq + 1] * a_q10) >> 10;
689   *d_q10 = (dist_tab_q10[xq] * b_q10 + dist_tab_q10[xq + 1] * a_q10) >> 10;
690 }
691 
av1_model_rd_from_var_lapndz(int64_t var,unsigned int n_log2,unsigned int qstep,int * rate,int64_t * dist)692 void av1_model_rd_from_var_lapndz(int64_t var, unsigned int n_log2,
693                                   unsigned int qstep, int *rate,
694                                   int64_t *dist) {
695   // This function models the rate and distortion for a Laplacian
696   // source with given variance when quantized with a uniform quantizer
697   // with given stepsize. The closed form expressions are in:
698   // Hang and Chen, "Source Model for transform video coder and its
699   // application - Part I: Fundamental Theory", IEEE Trans. Circ.
700   // Sys. for Video Tech., April 1997.
701   if (var == 0) {
702     *rate = 0;
703     *dist = 0;
704   } else {
705     int d_q10, r_q10;
706     static const uint32_t MAX_XSQ_Q10 = 245727;
707     const uint64_t xsq_q10_64 =
708         (((uint64_t)qstep * qstep << (n_log2 + 10)) + (var >> 1)) / var;
709     const int xsq_q10 = (int)AOMMIN(xsq_q10_64, MAX_XSQ_Q10);
710     model_rd_norm(xsq_q10, &r_q10, &d_q10);
711     *rate = ROUND_POWER_OF_TWO(r_q10 << n_log2, 10 - AV1_PROB_COST_SHIFT);
712     *dist = (var * (int64_t)d_q10 + 512) >> 10;
713   }
714 }
715 
interp_cubic(const double * p,double x)716 static double interp_cubic(const double *p, double x) {
717   return p[1] + 0.5 * x *
718                     (p[2] - p[0] +
719                      x * (2.0 * p[0] - 5.0 * p[1] + 4.0 * p[2] - p[3] +
720                           x * (3.0 * (p[1] - p[2]) + p[3] - p[0])));
721 }
722 
723 /*
724 static double interp_bicubic(const double *p, int p_stride, double x,
725                              double y) {
726   double q[4];
727   q[0] = interp_cubic(p, x);
728   q[1] = interp_cubic(p + p_stride, x);
729   q[2] = interp_cubic(p + 2 * p_stride, x);
730   q[3] = interp_cubic(p + 3 * p_stride, x);
731   return interp_cubic(q, y);
732 }
733 */
734 
735 static const uint8_t bsize_curvfit_model_cat_lookup[BLOCK_SIZES_ALL] = {
736   0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 1, 1, 2, 2, 3, 3
737 };
738 
sse_norm_curvfit_model_cat_lookup(double sse_norm)739 static int sse_norm_curvfit_model_cat_lookup(double sse_norm) {
740   return (sse_norm > 16.0);
741 }
742 
743 // Models distortion by sse using a logistic function on
744 // l = log2(sse / q^2) as:
745 // dbysse = 16 / (1 + k exp(l + c))
get_dbysse_logistic(double l,double c,double k)746 static double get_dbysse_logistic(double l, double c, double k) {
747   const double A = 16.0;
748   const double dbysse = A / (1 + k * exp(l + c));
749   return dbysse;
750 }
751 
752 // Models rate using a clamped linear function on
753 // l = log2(sse / q^2) as:
754 // rate = max(0, a + b * l)
get_rate_clamplinear(double l,double a,double b)755 static double get_rate_clamplinear(double l, double a, double b) {
756   const double rate = a + b * l;
757   return (rate < 0 ? 0 : rate);
758 }
759 
760 static const uint8_t bsize_surffit_model_cat_lookup[BLOCK_SIZES_ALL] = {
761   0, 0, 0, 0, 1, 1, 2, 3, 3, 4, 5, 5, 6, 7, 7, 8, 0, 0, 2, 2, 4, 4
762 };
763 
764 static const double surffit_rate_params[9][4] = {
765   {
766       638.390212,
767       2.253108,
768       166.585650,
769       -3.939401,
770   },
771   {
772       5.256905,
773       81.997240,
774       -1.321771,
775       17.694216,
776   },
777   {
778       -74.193045,
779       72.431868,
780       -19.033152,
781       15.407276,
782   },
783   {
784       416.770113,
785       14.794188,
786       167.686830,
787       -6.997756,
788   },
789   {
790       378.511276,
791       9.558376,
792       154.658843,
793       -6.635663,
794   },
795   {
796       277.818787,
797       4.413180,
798       150.317637,
799       -9.893038,
800   },
801   {
802       142.212132,
803       11.542038,
804       94.393964,
805       -5.518517,
806   },
807   {
808       219.100256,
809       4.007421,
810       108.932852,
811       -6.981310,
812   },
813   {
814       222.261971,
815       3.251049,
816       95.972916,
817       -5.609789,
818   },
819 };
820 
821 static const double surffit_dist_params[7] = { 1.475844,  4.328362, -5.680233,
822                                                -0.500994, 0.554585, 4.839478,
823                                                -0.695837 };
824 
rate_surffit_model_params_lookup(BLOCK_SIZE bsize,double xm,double * rpar)825 static void rate_surffit_model_params_lookup(BLOCK_SIZE bsize, double xm,
826                                              double *rpar) {
827   const int cat = bsize_surffit_model_cat_lookup[bsize];
828   rpar[0] = surffit_rate_params[cat][0] + surffit_rate_params[cat][1] * xm;
829   rpar[1] = surffit_rate_params[cat][2] + surffit_rate_params[cat][3] * xm;
830 }
831 
dist_surffit_model_params_lookup(BLOCK_SIZE bsize,double xm,double * dpar)832 static void dist_surffit_model_params_lookup(BLOCK_SIZE bsize, double xm,
833                                              double *dpar) {
834   (void)bsize;
835   const double *params = surffit_dist_params;
836   dpar[0] = params[0] + params[1] / (1 + exp((xm + params[2]) * params[3]));
837   dpar[1] = params[4] + params[5] * exp(params[6] * xm);
838 }
839 
av1_model_rd_surffit(BLOCK_SIZE bsize,double sse_norm,double xm,double yl,double * rate_f,double * distbysse_f)840 void av1_model_rd_surffit(BLOCK_SIZE bsize, double sse_norm, double xm,
841                           double yl, double *rate_f, double *distbysse_f) {
842   (void)sse_norm;
843   double rpar[2], dpar[2];
844   rate_surffit_model_params_lookup(bsize, xm, rpar);
845   dist_surffit_model_params_lookup(bsize, xm, dpar);
846 
847   *rate_f = get_rate_clamplinear(yl, rpar[0], rpar[1]);
848   *distbysse_f = get_dbysse_logistic(yl, dpar[0], dpar[1]);
849 }
850 
851 static const double interp_rgrid_curv[4][65] = {
852   {
853       0.000000,    0.000000,    0.000000,    0.000000,    0.000000,
854       0.000000,    0.000000,    0.000000,    0.000000,    0.000000,
855       0.000000,    118.257702,  120.210658,  121.434853,  122.100487,
856       122.377758,  122.436865,  72.290102,   96.974289,   101.652727,
857       126.830141,  140.417377,  157.644879,  184.315291,  215.823873,
858       262.300169,  335.919859,  420.624173,  519.185032,  619.854243,
859       726.053595,  827.663369,  933.127475,  1037.988755, 1138.839609,
860       1233.342933, 1333.508064, 1428.760126, 1533.396364, 1616.952052,
861       1744.539319, 1803.413586, 1951.466618, 1994.227838, 2086.031680,
862       2148.635443, 2239.068450, 2222.590637, 2338.859809, 2402.929011,
863       2418.727875, 2435.342670, 2471.159469, 2523.187446, 2591.183827,
864       2674.905840, 2774.110714, 2888.555675, 3017.997952, 3162.194773,
865       3320.903365, 3493.880956, 3680.884773, 3881.672045, 4096.000000,
866   },
867   {
868       0.000000,    0.000000,    0.000000,    0.000000,    0.000000,
869       0.000000,    0.000000,    0.000000,    0.000000,    0.000000,
870       0.000000,    13.087244,   15.919735,   25.930313,   24.412411,
871       28.567417,   29.924194,   30.857010,   32.742979,   36.382570,
872       39.210386,   42.265690,   47.378572,   57.014850,   82.740067,
873       137.346562,  219.968084,  316.781856,  415.643773,  516.706538,
874       614.914364,  714.303763,  815.512135,  911.210485,  1008.501528,
875       1109.787854, 1213.772279, 1322.922561, 1414.752579, 1510.505641,
876       1615.741888, 1697.989032, 1780.123933, 1847.453790, 1913.742309,
877       1960.828122, 2047.500168, 2085.454095, 2129.230668, 2158.171824,
878       2182.231724, 2217.684864, 2269.589211, 2337.264824, 2420.618694,
879       2519.557814, 2633.989178, 2763.819779, 2908.956609, 3069.306660,
880       3244.776927, 3435.274401, 3640.706076, 3860.978945, 4096.000000,
881   },
882   {
883       0.000000,    0.000000,    0.000000,    0.000000,    0.000000,
884       0.000000,    0.000000,    0.000000,    0.000000,    0.000000,
885       0.000000,    4.656893,    5.123633,    5.594132,    6.162376,
886       6.918433,    7.768444,    8.739415,    10.105862,   11.477328,
887       13.236604,   15.421030,   19.093623,   25.801871,   46.724612,
888       98.841054,   181.113466,  272.586364,  359.499769,  445.546343,
889       525.944439,  605.188743,  681.793483,  756.668359,  838.486885,
890       926.950356,  1015.482542, 1113.353926, 1204.897193, 1288.871992,
891       1373.464145, 1455.746628, 1527.796460, 1588.475066, 1658.144771,
892       1710.302500, 1807.563351, 1863.197608, 1927.281616, 1964.450872,
893       2022.719898, 2100.041145, 2185.205712, 2280.993936, 2387.616216,
894       2505.282950, 2634.204540, 2774.591385, 2926.653884, 3090.602436,
895       3266.647443, 3454.999303, 3655.868416, 3869.465182, 4096.000000,
896   },
897   {
898       0.000000,    0.000000,    0.000000,    0.000000,    0.000000,
899       0.000000,    0.000000,    0.000000,    0.000000,    0.000000,
900       0.000000,    0.337370,    0.391916,    0.468839,    0.566334,
901       0.762564,    1.069225,    1.384361,    1.787581,    2.293948,
902       3.251909,    4.412991,    8.050068,    11.606073,   27.668092,
903       65.227758,   128.463938,  202.097653,  262.715851,  312.464873,
904       355.601398,  400.609054,  447.201352,  495.761568,  552.871938,
905       619.067625,  691.984883,  773.753288,  860.628503,  946.262808,
906       1019.805896, 1106.061360, 1178.422145, 1244.852258, 1302.173987,
907       1399.650266, 1548.092912, 1545.928652, 1670.817500, 1694.523823,
908       1779.195362, 1882.155494, 1990.662097, 2108.325181, 2235.456119,
909       2372.366287, 2519.367059, 2676.769812, 2844.885918, 3024.026754,
910       3214.503695, 3416.628115, 3630.711389, 3857.064892, 4096.000000,
911   },
912 };
913 
914 static const double interp_dgrid_curv[3][65] = {
915   {
916       16.000000, 15.962891, 15.925174, 15.886888, 15.848074, 15.808770,
917       15.769015, 15.728850, 15.688313, 15.647445, 15.606284, 15.564870,
918       15.525918, 15.483820, 15.373330, 15.126844, 14.637442, 14.184387,
919       13.560070, 12.880717, 12.165995, 11.378144, 10.438769, 9.130790,
920       7.487633,  5.688649,  4.267515,  3.196300,  2.434201,  1.834064,
921       1.369920,  1.035921,  0.775279,  0.574895,  0.427232,  0.314123,
922       0.233236,  0.171440,  0.128188,  0.092762,  0.067569,  0.049324,
923       0.036330,  0.027008,  0.019853,  0.015539,  0.011093,  0.008733,
924       0.007624,  0.008105,  0.005427,  0.004065,  0.003427,  0.002848,
925       0.002328,  0.001865,  0.001457,  0.001103,  0.000801,  0.000550,
926       0.000348,  0.000193,  0.000085,  0.000021,  0.000000,
927   },
928   {
929       16.000000, 15.996116, 15.984769, 15.966413, 15.941505, 15.910501,
930       15.873856, 15.832026, 15.785466, 15.734633, 15.679981, 15.621967,
931       15.560961, 15.460157, 15.288367, 15.052462, 14.466922, 13.921212,
932       13.073692, 12.222005, 11.237799, 9.985848,  8.898823,  7.423519,
933       5.995325,  4.773152,  3.744032,  2.938217,  2.294526,  1.762412,
934       1.327145,  1.020728,  0.765535,  0.570548,  0.425833,  0.313825,
935       0.232959,  0.171324,  0.128174,  0.092750,  0.067558,  0.049319,
936       0.036330,  0.027008,  0.019853,  0.015539,  0.011093,  0.008733,
937       0.007624,  0.008105,  0.005427,  0.004065,  0.003427,  0.002848,
938       0.002328,  0.001865,  0.001457,  0.001103,  0.000801,  0.000550,
939       0.000348,  0.000193,  0.000085,  0.000021,  -0.000000,
940   },
941 };
942 
av1_model_rd_curvfit(BLOCK_SIZE bsize,double sse_norm,double xqr,double * rate_f,double * distbysse_f)943 void av1_model_rd_curvfit(BLOCK_SIZE bsize, double sse_norm, double xqr,
944                           double *rate_f, double *distbysse_f) {
945   const double x_start = -15.5;
946   const double x_end = 16.5;
947   const double x_step = 0.5;
948   const double epsilon = 1e-6;
949   const int rcat = bsize_curvfit_model_cat_lookup[bsize];
950   const int dcat = sse_norm_curvfit_model_cat_lookup(sse_norm);
951   (void)x_end;
952 
953   xqr = AOMMAX(xqr, x_start + x_step + epsilon);
954   xqr = AOMMIN(xqr, x_end - x_step - epsilon);
955   const double x = (xqr - x_start) / x_step;
956   const int xi = (int)floor(x);
957   const double xo = x - xi;
958 
959   assert(xi > 0);
960 
961   const double *prate = &interp_rgrid_curv[rcat][(xi - 1)];
962   *rate_f = interp_cubic(prate, xo);
963   const double *pdist = &interp_dgrid_curv[dcat][(xi - 1)];
964   *distbysse_f = interp_cubic(pdist, xo);
965 }
966 
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])967 static void get_entropy_contexts_plane(BLOCK_SIZE plane_bsize,
968                                        const struct macroblockd_plane *pd,
969                                        ENTROPY_CONTEXT t_above[MAX_MIB_SIZE],
970                                        ENTROPY_CONTEXT t_left[MAX_MIB_SIZE]) {
971   const int num_4x4_w = mi_size_wide[plane_bsize];
972   const int num_4x4_h = mi_size_high[plane_bsize];
973   const ENTROPY_CONTEXT *const above = pd->above_entropy_context;
974   const ENTROPY_CONTEXT *const left = pd->left_entropy_context;
975 
976   memcpy(t_above, above, sizeof(ENTROPY_CONTEXT) * num_4x4_w);
977   memcpy(t_left, left, sizeof(ENTROPY_CONTEXT) * num_4x4_h);
978 }
979 
av1_get_entropy_contexts(BLOCK_SIZE plane_bsize,const struct macroblockd_plane * pd,ENTROPY_CONTEXT t_above[MAX_MIB_SIZE],ENTROPY_CONTEXT t_left[MAX_MIB_SIZE])980 void av1_get_entropy_contexts(BLOCK_SIZE plane_bsize,
981                               const struct macroblockd_plane *pd,
982                               ENTROPY_CONTEXT t_above[MAX_MIB_SIZE],
983                               ENTROPY_CONTEXT t_left[MAX_MIB_SIZE]) {
984   assert(plane_bsize < BLOCK_SIZES_ALL);
985   get_entropy_contexts_plane(plane_bsize, pd, t_above, t_left);
986 }
987 
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)988 void av1_mv_pred(const AV1_COMP *cpi, MACROBLOCK *x, uint8_t *ref_y_buffer,
989                  int ref_y_stride, int ref_frame, BLOCK_SIZE block_size) {
990   const MV_REFERENCE_FRAME ref_frames[2] = { ref_frame, NONE_FRAME };
991   const int_mv ref_mv =
992       av1_get_ref_mv_from_stack(0, ref_frames, 0, x->mbmi_ext);
993   const int_mv ref_mv1 =
994       av1_get_ref_mv_from_stack(0, ref_frames, 1, x->mbmi_ext);
995   MV pred_mv[MAX_MV_REF_CANDIDATES + 1];
996   int num_mv_refs = 0;
997   pred_mv[num_mv_refs++] = ref_mv.as_mv;
998   if (ref_mv.as_int != ref_mv1.as_int) {
999     pred_mv[num_mv_refs++] = ref_mv1.as_mv;
1000   }
1001   if (cpi->sf.mv_sf.adaptive_motion_search &&
1002       block_size < x->max_partition_size) {
1003     pred_mv[num_mv_refs++] = x->pred_mv[ref_frame];
1004   }
1005 
1006   assert(num_mv_refs <= (int)(sizeof(pred_mv) / sizeof(pred_mv[0])));
1007 
1008   const uint8_t *const src_y_ptr = x->plane[0].src.buf;
1009   int zero_seen = 0;
1010   int best_sad = INT_MAX;
1011   int max_mv = 0;
1012   // Get the sad for each candidate reference mv.
1013   for (int i = 0; i < num_mv_refs; ++i) {
1014     const MV *this_mv = &pred_mv[i];
1015     const int fp_row = (this_mv->row + 3 + (this_mv->row >= 0)) >> 3;
1016     const int fp_col = (this_mv->col + 3 + (this_mv->col >= 0)) >> 3;
1017     max_mv = AOMMAX(max_mv, AOMMAX(abs(this_mv->row), abs(this_mv->col)) >> 3);
1018 
1019     if (fp_row == 0 && fp_col == 0 && zero_seen) continue;
1020     zero_seen |= (fp_row == 0 && fp_col == 0);
1021 
1022     const uint8_t *const ref_y_ptr =
1023         &ref_y_buffer[ref_y_stride * fp_row + fp_col];
1024     // Find sad for current vector.
1025     const int this_sad = cpi->fn_ptr[block_size].sdf(
1026         src_y_ptr, x->plane[0].src.stride, ref_y_ptr, ref_y_stride);
1027     // Note if it is the best so far.
1028     if (this_sad < best_sad) {
1029       best_sad = this_sad;
1030     }
1031   }
1032 
1033   // Note the index of the mv that worked best in the reference list.
1034   x->max_mv_context[ref_frame] = max_mv;
1035   x->pred_mv_sad[ref_frame] = best_sad;
1036 }
1037 
av1_setup_pred_block(const MACROBLOCKD * xd,struct buf_2d dst[MAX_MB_PLANE],const YV12_BUFFER_CONFIG * src,const struct scale_factors * scale,const struct scale_factors * scale_uv,const int num_planes)1038 void av1_setup_pred_block(const MACROBLOCKD *xd,
1039                           struct buf_2d dst[MAX_MB_PLANE],
1040                           const YV12_BUFFER_CONFIG *src,
1041                           const struct scale_factors *scale,
1042                           const struct scale_factors *scale_uv,
1043                           const int num_planes) {
1044   dst[0].buf = src->y_buffer;
1045   dst[0].stride = src->y_stride;
1046   dst[1].buf = src->u_buffer;
1047   dst[2].buf = src->v_buffer;
1048   dst[1].stride = dst[2].stride = src->uv_stride;
1049 
1050   const int mi_row = xd->mi_row;
1051   const int mi_col = xd->mi_col;
1052   for (int i = 0; i < num_planes; ++i) {
1053     setup_pred_plane(dst + i, xd->mi[0]->sb_type, dst[i].buf,
1054                      i ? src->uv_crop_width : src->y_crop_width,
1055                      i ? src->uv_crop_height : src->y_crop_height,
1056                      dst[i].stride, mi_row, mi_col, i ? scale_uv : scale,
1057                      xd->plane[i].subsampling_x, xd->plane[i].subsampling_y);
1058   }
1059 }
1060 
av1_get_scaled_ref_frame(const AV1_COMP * cpi,int ref_frame)1061 YV12_BUFFER_CONFIG *av1_get_scaled_ref_frame(const AV1_COMP *cpi,
1062                                              int ref_frame) {
1063   assert(ref_frame >= LAST_FRAME && ref_frame <= ALTREF_FRAME);
1064   RefCntBuffer *const scaled_buf = cpi->scaled_ref_buf[ref_frame - 1];
1065   const RefCntBuffer *const ref_buf =
1066       get_ref_frame_buf(&cpi->common, ref_frame);
1067   return (scaled_buf != ref_buf && scaled_buf != NULL) ? &scaled_buf->buf
1068                                                        : NULL;
1069 }
1070 
av1_get_switchable_rate(const MACROBLOCK * x,const MACROBLOCKD * xd,InterpFilter interp_filter)1071 int av1_get_switchable_rate(const MACROBLOCK *x, const MACROBLOCKD *xd,
1072                             InterpFilter interp_filter) {
1073   if (interp_filter == SWITCHABLE) {
1074     const MB_MODE_INFO *const mbmi = xd->mi[0];
1075     int inter_filter_cost = 0;
1076     int dir;
1077 
1078     for (dir = 0; dir < 2; ++dir) {
1079       const int ctx = av1_get_pred_context_switchable_interp(xd, dir);
1080       const InterpFilter filter =
1081           av1_extract_interp_filter(mbmi->interp_filters, dir);
1082       inter_filter_cost += x->switchable_interp_costs[ctx][filter];
1083     }
1084     return SWITCHABLE_INTERP_RATE_FACTOR * inter_filter_cost;
1085   } else {
1086     return 0;
1087   }
1088 }
1089 
av1_set_rd_speed_thresholds(AV1_COMP * cpi)1090 void av1_set_rd_speed_thresholds(AV1_COMP *cpi) {
1091   RD_OPT *const rd = &cpi->rd;
1092 
1093   // Set baseline threshold values.
1094   av1_zero(rd->thresh_mult);
1095 
1096   rd->thresh_mult[THR_NEARESTMV] = 300;
1097   rd->thresh_mult[THR_NEARESTL2] = 300;
1098   rd->thresh_mult[THR_NEARESTL3] = 300;
1099   rd->thresh_mult[THR_NEARESTB] = 300;
1100   rd->thresh_mult[THR_NEARESTA2] = 300;
1101   rd->thresh_mult[THR_NEARESTA] = 300;
1102   rd->thresh_mult[THR_NEARESTG] = 300;
1103 
1104   rd->thresh_mult[THR_NEWMV] = 1000;
1105   rd->thresh_mult[THR_NEWL2] = 1000;
1106   rd->thresh_mult[THR_NEWL3] = 1000;
1107   rd->thresh_mult[THR_NEWB] = 1000;
1108   rd->thresh_mult[THR_NEWA2] = 1100;
1109   rd->thresh_mult[THR_NEWA] = 1000;
1110   rd->thresh_mult[THR_NEWG] = 1000;
1111 
1112   rd->thresh_mult[THR_NEARMV] = 1000;
1113   rd->thresh_mult[THR_NEARL2] = 1000;
1114   rd->thresh_mult[THR_NEARL3] = 1000;
1115   rd->thresh_mult[THR_NEARB] = 1000;
1116   rd->thresh_mult[THR_NEARA2] = 1000;
1117   rd->thresh_mult[THR_NEARA] = 1000;
1118   rd->thresh_mult[THR_NEARG] = 1000;
1119 
1120   rd->thresh_mult[THR_GLOBALMV] = 2200;
1121   rd->thresh_mult[THR_GLOBALL2] = 2000;
1122   rd->thresh_mult[THR_GLOBALL3] = 2000;
1123   rd->thresh_mult[THR_GLOBALB] = 2400;
1124   rd->thresh_mult[THR_GLOBALA2] = 2000;
1125   rd->thresh_mult[THR_GLOBALG] = 2000;
1126   rd->thresh_mult[THR_GLOBALA] = 2400;
1127 
1128   rd->thresh_mult[THR_COMP_NEAREST_NEARESTLA] = 1100;
1129   rd->thresh_mult[THR_COMP_NEAREST_NEARESTL2A] = 1000;
1130   rd->thresh_mult[THR_COMP_NEAREST_NEARESTL3A] = 800;
1131   rd->thresh_mult[THR_COMP_NEAREST_NEARESTGA] = 900;
1132   rd->thresh_mult[THR_COMP_NEAREST_NEARESTLB] = 1000;
1133   rd->thresh_mult[THR_COMP_NEAREST_NEARESTL2B] = 1000;
1134   rd->thresh_mult[THR_COMP_NEAREST_NEARESTL3B] = 1000;
1135   rd->thresh_mult[THR_COMP_NEAREST_NEARESTGB] = 1000;
1136   rd->thresh_mult[THR_COMP_NEAREST_NEARESTLA2] = 1000;
1137   rd->thresh_mult[THR_COMP_NEAREST_NEARESTL2A2] = 1000;
1138   rd->thresh_mult[THR_COMP_NEAREST_NEARESTL3A2] = 1000;
1139   rd->thresh_mult[THR_COMP_NEAREST_NEARESTGA2] = 1000;
1140 
1141   rd->thresh_mult[THR_COMP_NEAREST_NEARESTLL2] = 2000;
1142   rd->thresh_mult[THR_COMP_NEAREST_NEARESTLL3] = 2000;
1143   rd->thresh_mult[THR_COMP_NEAREST_NEARESTLG] = 2000;
1144   rd->thresh_mult[THR_COMP_NEAREST_NEARESTBA] = 2000;
1145 
1146   rd->thresh_mult[THR_COMP_NEAR_NEARLA] = 1200;
1147   rd->thresh_mult[THR_COMP_NEAREST_NEWLA] = 1500;
1148   rd->thresh_mult[THR_COMP_NEW_NEARESTLA] = 1500;
1149   rd->thresh_mult[THR_COMP_NEAR_NEWLA] = 1530;
1150   rd->thresh_mult[THR_COMP_NEW_NEARLA] = 1870;
1151   rd->thresh_mult[THR_COMP_NEW_NEWLA] = 2400;
1152   rd->thresh_mult[THR_COMP_GLOBAL_GLOBALLA] = 2750;
1153 
1154   rd->thresh_mult[THR_COMP_NEAR_NEARL2A] = 1200;
1155   rd->thresh_mult[THR_COMP_NEAREST_NEWL2A] = 1500;
1156   rd->thresh_mult[THR_COMP_NEW_NEARESTL2A] = 1500;
1157   rd->thresh_mult[THR_COMP_NEAR_NEWL2A] = 1870;
1158   rd->thresh_mult[THR_COMP_NEW_NEARL2A] = 1700;
1159   rd->thresh_mult[THR_COMP_NEW_NEWL2A] = 1800;
1160   rd->thresh_mult[THR_COMP_GLOBAL_GLOBALL2A] = 2500;
1161 
1162   rd->thresh_mult[THR_COMP_NEAR_NEARL3A] = 1200;
1163   rd->thresh_mult[THR_COMP_NEAREST_NEWL3A] = 1500;
1164   rd->thresh_mult[THR_COMP_NEW_NEARESTL3A] = 1500;
1165   rd->thresh_mult[THR_COMP_NEAR_NEWL3A] = 1700;
1166   rd->thresh_mult[THR_COMP_NEW_NEARL3A] = 1700;
1167   rd->thresh_mult[THR_COMP_NEW_NEWL3A] = 2000;
1168   rd->thresh_mult[THR_COMP_GLOBAL_GLOBALL3A] = 3000;
1169 
1170   rd->thresh_mult[THR_COMP_NEAR_NEARGA] = 1320;
1171   rd->thresh_mult[THR_COMP_NEAREST_NEWGA] = 1500;
1172   rd->thresh_mult[THR_COMP_NEW_NEARESTGA] = 1500;
1173   rd->thresh_mult[THR_COMP_NEAR_NEWGA] = 2040;
1174   rd->thresh_mult[THR_COMP_NEW_NEARGA] = 1700;
1175   rd->thresh_mult[THR_COMP_NEW_NEWGA] = 2000;
1176   rd->thresh_mult[THR_COMP_GLOBAL_GLOBALGA] = 2250;
1177 
1178   rd->thresh_mult[THR_COMP_NEAR_NEARLB] = 1200;
1179   rd->thresh_mult[THR_COMP_NEAREST_NEWLB] = 1500;
1180   rd->thresh_mult[THR_COMP_NEW_NEARESTLB] = 1500;
1181   rd->thresh_mult[THR_COMP_NEAR_NEWLB] = 1360;
1182   rd->thresh_mult[THR_COMP_NEW_NEARLB] = 1700;
1183   rd->thresh_mult[THR_COMP_NEW_NEWLB] = 2400;
1184   rd->thresh_mult[THR_COMP_GLOBAL_GLOBALLB] = 2250;
1185 
1186   rd->thresh_mult[THR_COMP_NEAR_NEARL2B] = 1200;
1187   rd->thresh_mult[THR_COMP_NEAREST_NEWL2B] = 1500;
1188   rd->thresh_mult[THR_COMP_NEW_NEARESTL2B] = 1500;
1189   rd->thresh_mult[THR_COMP_NEAR_NEWL2B] = 1700;
1190   rd->thresh_mult[THR_COMP_NEW_NEARL2B] = 1700;
1191   rd->thresh_mult[THR_COMP_NEW_NEWL2B] = 2000;
1192   rd->thresh_mult[THR_COMP_GLOBAL_GLOBALL2B] = 2500;
1193 
1194   rd->thresh_mult[THR_COMP_NEAR_NEARL3B] = 1200;
1195   rd->thresh_mult[THR_COMP_NEAREST_NEWL3B] = 1500;
1196   rd->thresh_mult[THR_COMP_NEW_NEARESTL3B] = 1500;
1197   rd->thresh_mult[THR_COMP_NEAR_NEWL3B] = 1870;
1198   rd->thresh_mult[THR_COMP_NEW_NEARL3B] = 1700;
1199   rd->thresh_mult[THR_COMP_NEW_NEWL3B] = 2000;
1200   rd->thresh_mult[THR_COMP_GLOBAL_GLOBALL3B] = 2500;
1201 
1202   rd->thresh_mult[THR_COMP_NEAR_NEARGB] = 1200;
1203   rd->thresh_mult[THR_COMP_NEAREST_NEWGB] = 1500;
1204   rd->thresh_mult[THR_COMP_NEW_NEARESTGB] = 1500;
1205   rd->thresh_mult[THR_COMP_NEAR_NEWGB] = 1700;
1206   rd->thresh_mult[THR_COMP_NEW_NEARGB] = 1700;
1207   rd->thresh_mult[THR_COMP_NEW_NEWGB] = 2000;
1208   rd->thresh_mult[THR_COMP_GLOBAL_GLOBALGB] = 2500;
1209 
1210   rd->thresh_mult[THR_COMP_NEAR_NEARLA2] = 1200;
1211   rd->thresh_mult[THR_COMP_NEAREST_NEWLA2] = 1800;
1212   rd->thresh_mult[THR_COMP_NEW_NEARESTLA2] = 1500;
1213   rd->thresh_mult[THR_COMP_NEAR_NEWLA2] = 1700;
1214   rd->thresh_mult[THR_COMP_NEW_NEARLA2] = 1700;
1215   rd->thresh_mult[THR_COMP_NEW_NEWLA2] = 2000;
1216   rd->thresh_mult[THR_COMP_GLOBAL_GLOBALLA2] = 2500;
1217 
1218   rd->thresh_mult[THR_COMP_NEAR_NEARL2A2] = 1200;
1219   rd->thresh_mult[THR_COMP_NEAREST_NEWL2A2] = 1500;
1220   rd->thresh_mult[THR_COMP_NEW_NEARESTL2A2] = 1500;
1221   rd->thresh_mult[THR_COMP_NEAR_NEWL2A2] = 1700;
1222   rd->thresh_mult[THR_COMP_NEW_NEARL2A2] = 1700;
1223   rd->thresh_mult[THR_COMP_NEW_NEWL2A2] = 2000;
1224   rd->thresh_mult[THR_COMP_GLOBAL_GLOBALL2A2] = 2500;
1225 
1226   rd->thresh_mult[THR_COMP_NEAR_NEARL3A2] = 1440;
1227   rd->thresh_mult[THR_COMP_NEAREST_NEWL3A2] = 1500;
1228   rd->thresh_mult[THR_COMP_NEW_NEARESTL3A2] = 1500;
1229   rd->thresh_mult[THR_COMP_NEAR_NEWL3A2] = 1700;
1230   rd->thresh_mult[THR_COMP_NEW_NEARL3A2] = 1700;
1231   rd->thresh_mult[THR_COMP_NEW_NEWL3A2] = 2000;
1232   rd->thresh_mult[THR_COMP_GLOBAL_GLOBALL3A2] = 2500;
1233 
1234   rd->thresh_mult[THR_COMP_NEAR_NEARGA2] = 1200;
1235   rd->thresh_mult[THR_COMP_NEAREST_NEWGA2] = 1500;
1236   rd->thresh_mult[THR_COMP_NEW_NEARESTGA2] = 1500;
1237   rd->thresh_mult[THR_COMP_NEAR_NEWGA2] = 1700;
1238   rd->thresh_mult[THR_COMP_NEW_NEARGA2] = 1700;
1239   rd->thresh_mult[THR_COMP_NEW_NEWGA2] = 2000;
1240   rd->thresh_mult[THR_COMP_GLOBAL_GLOBALGA2] = 2750;
1241 
1242   rd->thresh_mult[THR_COMP_NEAR_NEARLL2] = 1600;
1243   rd->thresh_mult[THR_COMP_NEAREST_NEWLL2] = 2000;
1244   rd->thresh_mult[THR_COMP_NEW_NEARESTLL2] = 2000;
1245   rd->thresh_mult[THR_COMP_NEAR_NEWLL2] = 2640;
1246   rd->thresh_mult[THR_COMP_NEW_NEARLL2] = 2200;
1247   rd->thresh_mult[THR_COMP_NEW_NEWLL2] = 2400;
1248   rd->thresh_mult[THR_COMP_GLOBAL_GLOBALLL2] = 3200;
1249 
1250   rd->thresh_mult[THR_COMP_NEAR_NEARLL3] = 1600;
1251   rd->thresh_mult[THR_COMP_NEAREST_NEWLL3] = 2000;
1252   rd->thresh_mult[THR_COMP_NEW_NEARESTLL3] = 1800;
1253   rd->thresh_mult[THR_COMP_NEAR_NEWLL3] = 2200;
1254   rd->thresh_mult[THR_COMP_NEW_NEARLL3] = 2200;
1255   rd->thresh_mult[THR_COMP_NEW_NEWLL3] = 2400;
1256   rd->thresh_mult[THR_COMP_GLOBAL_GLOBALLL3] = 3200;
1257 
1258   rd->thresh_mult[THR_COMP_NEAR_NEARLG] = 1760;
1259   rd->thresh_mult[THR_COMP_NEAREST_NEWLG] = 2400;
1260   rd->thresh_mult[THR_COMP_NEW_NEARESTLG] = 2000;
1261   rd->thresh_mult[THR_COMP_NEAR_NEWLG] = 1760;
1262   rd->thresh_mult[THR_COMP_NEW_NEARLG] = 2640;
1263   rd->thresh_mult[THR_COMP_NEW_NEWLG] = 2400;
1264   rd->thresh_mult[THR_COMP_GLOBAL_GLOBALLG] = 3200;
1265 
1266   rd->thresh_mult[THR_COMP_NEAR_NEARBA] = 1600;
1267   rd->thresh_mult[THR_COMP_NEAREST_NEWBA] = 2000;
1268   rd->thresh_mult[THR_COMP_NEW_NEARESTBA] = 2000;
1269   rd->thresh_mult[THR_COMP_NEAR_NEWBA] = 2200;
1270   rd->thresh_mult[THR_COMP_NEW_NEARBA] = 1980;
1271   rd->thresh_mult[THR_COMP_NEW_NEWBA] = 2640;
1272   rd->thresh_mult[THR_COMP_GLOBAL_GLOBALBA] = 3200;
1273 
1274   rd->thresh_mult[THR_DC] = 1000;
1275   rd->thresh_mult[THR_PAETH] = 1000;
1276   rd->thresh_mult[THR_SMOOTH] = 2200;
1277   rd->thresh_mult[THR_SMOOTH_V] = 2000;
1278   rd->thresh_mult[THR_SMOOTH_H] = 2000;
1279   rd->thresh_mult[THR_H_PRED] = 2000;
1280   rd->thresh_mult[THR_V_PRED] = 1800;
1281   rd->thresh_mult[THR_D135_PRED] = 2500;
1282   rd->thresh_mult[THR_D203_PRED] = 2000;
1283   rd->thresh_mult[THR_D157_PRED] = 2500;
1284   rd->thresh_mult[THR_D67_PRED] = 2000;
1285   rd->thresh_mult[THR_D113_PRED] = 2500;
1286   rd->thresh_mult[THR_D45_PRED] = 2500;
1287 }
1288 
av1_update_rd_thresh_fact(const AV1_COMMON * const cm,int (* factor_buf)[MAX_MODES],int use_adaptive_rd_thresh,BLOCK_SIZE bsize,THR_MODES best_mode_index)1289 void av1_update_rd_thresh_fact(const AV1_COMMON *const cm,
1290                                int (*factor_buf)[MAX_MODES],
1291                                int use_adaptive_rd_thresh, BLOCK_SIZE bsize,
1292                                THR_MODES best_mode_index) {
1293   assert(use_adaptive_rd_thresh > 0);
1294   const THR_MODES top_mode = MAX_MODES;
1295   const int max_rd_thresh_factor = use_adaptive_rd_thresh * RD_THRESH_MAX_FACT;
1296 
1297   const int bsize_is_1_to_4 = bsize > cm->seq_params.sb_size;
1298   BLOCK_SIZE min_size, max_size;
1299   if (bsize_is_1_to_4) {
1300     // This part handles block sizes with 1:4 and 4:1 aspect ratios
1301     // TODO(any): Experiment with threshold update for parent/child blocks
1302     min_size = bsize;
1303     max_size = bsize;
1304   } else {
1305     min_size = AOMMAX(bsize - 2, BLOCK_4X4);
1306     max_size = AOMMIN(bsize + 2, (int)cm->seq_params.sb_size);
1307   }
1308 
1309   for (THR_MODES mode = 0; mode < top_mode; ++mode) {
1310     for (BLOCK_SIZE bs = min_size; bs <= max_size; ++bs) {
1311       int *const fact = &factor_buf[bs][mode];
1312       if (mode == best_mode_index) {
1313         *fact -= (*fact >> RD_THRESH_LOG_DEC_FACTOR);
1314       } else {
1315         *fact = AOMMIN(*fact + RD_THRESH_INC, max_rd_thresh_factor);
1316       }
1317     }
1318   }
1319 }
1320 
av1_get_intra_cost_penalty(int qindex,int qdelta,aom_bit_depth_t bit_depth)1321 int av1_get_intra_cost_penalty(int qindex, int qdelta,
1322                                aom_bit_depth_t bit_depth) {
1323   const int q = av1_dc_quant_QTX(qindex, qdelta, bit_depth);
1324   switch (bit_depth) {
1325     case AOM_BITS_8: return 20 * q;
1326     case AOM_BITS_10: return 5 * q;
1327     case AOM_BITS_12: return ROUND_POWER_OF_TWO(5 * q, 2);
1328     default:
1329       assert(0 && "bit_depth should be AOM_BITS_8, AOM_BITS_10 or AOM_BITS_12");
1330       return -1;
1331   }
1332 }
1333