• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include <limits.h>
12 #include <math.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 
16 #include "./vp9_rtcd.h"
17 #include "./vpx_config.h"
18 #include "./vpx_dsp_rtcd.h"
19 #include "./vpx_scale_rtcd.h"
20 #include "vpx_dsp/psnr.h"
21 #include "vpx_dsp/vpx_dsp_common.h"
22 #include "vpx_dsp/vpx_filter.h"
23 #if CONFIG_INTERNAL_STATS
24 #include "vpx_dsp/ssim.h"
25 #endif
26 #include "vpx_ports/mem.h"
27 #include "vpx_ports/system_state.h"
28 #include "vpx_ports/vpx_timer.h"
29 #if CONFIG_BITSTREAM_DEBUG || CONFIG_MISMATCH_DEBUG
30 #include "vpx_util/vpx_debug_util.h"
31 #endif  // CONFIG_BITSTREAM_DEBUG || CONFIG_MISMATCH_DEBUG
32 
33 #include "vp9/common/vp9_alloccommon.h"
34 #include "vp9/common/vp9_filter.h"
35 #include "vp9/common/vp9_idct.h"
36 #if CONFIG_NON_GREEDY_MV
37 #include "vp9/common/vp9_mvref_common.h"
38 #endif
39 #if CONFIG_VP9_POSTPROC
40 #include "vp9/common/vp9_postproc.h"
41 #endif
42 #include "vp9/common/vp9_reconinter.h"
43 #include "vp9/common/vp9_reconintra.h"
44 #include "vp9/common/vp9_tile_common.h"
45 #include "vp9/common/vp9_scan.h"
46 
47 #if !CONFIG_REALTIME_ONLY
48 #include "vp9/encoder/vp9_alt_ref_aq.h"
49 #include "vp9/encoder/vp9_aq_360.h"
50 #include "vp9/encoder/vp9_aq_complexity.h"
51 #endif
52 #include "vp9/encoder/vp9_aq_cyclicrefresh.h"
53 #if !CONFIG_REALTIME_ONLY
54 #include "vp9/encoder/vp9_aq_variance.h"
55 #endif
56 #include "vp9/encoder/vp9_bitstream.h"
57 #if CONFIG_INTERNAL_STATS
58 #include "vp9/encoder/vp9_blockiness.h"
59 #endif
60 #include "vp9/encoder/vp9_context_tree.h"
61 #include "vp9/encoder/vp9_encodeframe.h"
62 #include "vp9/encoder/vp9_encodemb.h"
63 #include "vp9/encoder/vp9_encodemv.h"
64 #include "vp9/encoder/vp9_encoder.h"
65 #include "vp9/encoder/vp9_ethread.h"
66 #include "vp9/encoder/vp9_extend.h"
67 #include "vp9/encoder/vp9_firstpass.h"
68 #include "vp9/encoder/vp9_mbgraph.h"
69 #if CONFIG_NON_GREEDY_MV
70 #include "vp9/encoder/vp9_mcomp.h"
71 #endif
72 #include "vp9/encoder/vp9_multi_thread.h"
73 #include "vp9/encoder/vp9_noise_estimate.h"
74 #include "vp9/encoder/vp9_picklpf.h"
75 #include "vp9/encoder/vp9_ratectrl.h"
76 #include "vp9/encoder/vp9_rd.h"
77 #include "vp9/encoder/vp9_resize.h"
78 #include "vp9/encoder/vp9_segmentation.h"
79 #include "vp9/encoder/vp9_skin_detection.h"
80 #include "vp9/encoder/vp9_speed_features.h"
81 #include "vp9/encoder/vp9_svc_layercontext.h"
82 #include "vp9/encoder/vp9_temporal_filter.h"
83 #include "vp9/vp9_cx_iface.h"
84 
85 #define AM_SEGMENT_ID_INACTIVE 7
86 #define AM_SEGMENT_ID_ACTIVE 0
87 
88 // Whether to use high precision mv for altref computation.
89 #define ALTREF_HIGH_PRECISION_MV 1
90 
91 // Q threshold for high precision mv. Choose a very high value for now so that
92 // HIGH_PRECISION is always chosen.
93 #define HIGH_PRECISION_MV_QTHRESH 200
94 
95 #define FRAME_SIZE_FACTOR 128  // empirical params for context model threshold
96 #define FRAME_RATE_FACTOR 8
97 
98 #ifdef OUTPUT_YUV_DENOISED
99 FILE *yuv_denoised_file = NULL;
100 #endif
101 #ifdef OUTPUT_YUV_SKINMAP
102 static FILE *yuv_skinmap_file = NULL;
103 #endif
104 #ifdef OUTPUT_YUV_REC
105 FILE *yuv_rec_file;
106 #endif
107 #ifdef OUTPUT_YUV_SVC_SRC
108 FILE *yuv_svc_src[3] = { NULL, NULL, NULL };
109 #endif
110 
111 #if 0
112 FILE *framepsnr;
113 FILE *kf_list;
114 FILE *keyfile;
115 #endif
116 
117 #ifdef ENABLE_KF_DENOISE
118 // Test condition for spatial denoise of source.
is_spatial_denoise_enabled(VP9_COMP * cpi)119 static int is_spatial_denoise_enabled(VP9_COMP *cpi) {
120   VP9_COMMON *const cm = &cpi->common;
121   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
122 
123   return (oxcf->pass != 1) && !is_lossless_requested(&cpi->oxcf) &&
124          frame_is_intra_only(cm);
125 }
126 #endif
127 
128 #if CONFIG_VP9_HIGHBITDEPTH
129 void highbd_wht_fwd_txfm(int16_t *src_diff, int bw, tran_low_t *coeff,
130                          TX_SIZE tx_size);
131 #endif
132 void wht_fwd_txfm(int16_t *src_diff, int bw, tran_low_t *coeff,
133                   TX_SIZE tx_size);
134 
135 #if !CONFIG_REALTIME_ONLY
136 // compute adaptive threshold for skip recoding
compute_context_model_thresh(const VP9_COMP * const cpi)137 static int compute_context_model_thresh(const VP9_COMP *const cpi) {
138   const VP9_COMMON *const cm = &cpi->common;
139   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
140   const int frame_size = (cm->width * cm->height) >> 10;
141   const int bitrate = (int)(oxcf->target_bandwidth >> 10);
142   const int qindex_factor = cm->base_qindex + (MAXQ >> 1);
143 
144   // This equation makes the threshold adaptive to frame size.
145   // Coding gain obtained by recoding comes from alternate frames of large
146   // content change. We skip recoding if the difference of previous and current
147   // frame context probability model is less than a certain threshold.
148   // The first component is the most critical part to guarantee adaptivity.
149   // Other parameters are estimated based on normal setting of hd resolution
150   // parameters. e.g frame_size = 1920x1080, bitrate = 8000, qindex_factor < 50
151   const int thresh =
152       ((FRAME_SIZE_FACTOR * frame_size - FRAME_RATE_FACTOR * bitrate) *
153        qindex_factor) >>
154       9;
155 
156   return thresh;
157 }
158 
159 // compute the total cost difference between current
160 // and previous frame context prob model.
compute_context_model_diff(const VP9_COMMON * const cm)161 static int compute_context_model_diff(const VP9_COMMON *const cm) {
162   const FRAME_CONTEXT *const pre_fc =
163       &cm->frame_contexts[cm->frame_context_idx];
164   const FRAME_CONTEXT *const cur_fc = cm->fc;
165   const FRAME_COUNTS *counts = &cm->counts;
166   vpx_prob pre_last_prob, cur_last_prob;
167   int diff = 0;
168   int i, j, k, l, m, n;
169 
170   // y_mode_prob
171   for (i = 0; i < BLOCK_SIZE_GROUPS; ++i) {
172     for (j = 0; j < INTRA_MODES - 1; ++j) {
173       diff += (int)counts->y_mode[i][j] *
174               (pre_fc->y_mode_prob[i][j] - cur_fc->y_mode_prob[i][j]);
175     }
176     pre_last_prob = MAX_PROB - pre_fc->y_mode_prob[i][INTRA_MODES - 2];
177     cur_last_prob = MAX_PROB - cur_fc->y_mode_prob[i][INTRA_MODES - 2];
178 
179     diff += (int)counts->y_mode[i][INTRA_MODES - 1] *
180             (pre_last_prob - cur_last_prob);
181   }
182 
183   // uv_mode_prob
184   for (i = 0; i < INTRA_MODES; ++i) {
185     for (j = 0; j < INTRA_MODES - 1; ++j) {
186       diff += (int)counts->uv_mode[i][j] *
187               (pre_fc->uv_mode_prob[i][j] - cur_fc->uv_mode_prob[i][j]);
188     }
189     pre_last_prob = MAX_PROB - pre_fc->uv_mode_prob[i][INTRA_MODES - 2];
190     cur_last_prob = MAX_PROB - cur_fc->uv_mode_prob[i][INTRA_MODES - 2];
191 
192     diff += (int)counts->uv_mode[i][INTRA_MODES - 1] *
193             (pre_last_prob - cur_last_prob);
194   }
195 
196   // partition_prob
197   for (i = 0; i < PARTITION_CONTEXTS; ++i) {
198     for (j = 0; j < PARTITION_TYPES - 1; ++j) {
199       diff += (int)counts->partition[i][j] *
200               (pre_fc->partition_prob[i][j] - cur_fc->partition_prob[i][j]);
201     }
202     pre_last_prob = MAX_PROB - pre_fc->partition_prob[i][PARTITION_TYPES - 2];
203     cur_last_prob = MAX_PROB - cur_fc->partition_prob[i][PARTITION_TYPES - 2];
204 
205     diff += (int)counts->partition[i][PARTITION_TYPES - 1] *
206             (pre_last_prob - cur_last_prob);
207   }
208 
209   // coef_probs
210   for (i = 0; i < TX_SIZES; ++i) {
211     for (j = 0; j < PLANE_TYPES; ++j) {
212       for (k = 0; k < REF_TYPES; ++k) {
213         for (l = 0; l < COEF_BANDS; ++l) {
214           for (m = 0; m < BAND_COEFF_CONTEXTS(l); ++m) {
215             for (n = 0; n < UNCONSTRAINED_NODES; ++n) {
216               diff += (int)counts->coef[i][j][k][l][m][n] *
217                       (pre_fc->coef_probs[i][j][k][l][m][n] -
218                        cur_fc->coef_probs[i][j][k][l][m][n]);
219             }
220 
221             pre_last_prob =
222                 MAX_PROB -
223                 pre_fc->coef_probs[i][j][k][l][m][UNCONSTRAINED_NODES - 1];
224             cur_last_prob =
225                 MAX_PROB -
226                 cur_fc->coef_probs[i][j][k][l][m][UNCONSTRAINED_NODES - 1];
227 
228             diff += (int)counts->coef[i][j][k][l][m][UNCONSTRAINED_NODES] *
229                     (pre_last_prob - cur_last_prob);
230           }
231         }
232       }
233     }
234   }
235 
236   // switchable_interp_prob
237   for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; ++i) {
238     for (j = 0; j < SWITCHABLE_FILTERS - 1; ++j) {
239       diff += (int)counts->switchable_interp[i][j] *
240               (pre_fc->switchable_interp_prob[i][j] -
241                cur_fc->switchable_interp_prob[i][j]);
242     }
243     pre_last_prob =
244         MAX_PROB - pre_fc->switchable_interp_prob[i][SWITCHABLE_FILTERS - 2];
245     cur_last_prob =
246         MAX_PROB - cur_fc->switchable_interp_prob[i][SWITCHABLE_FILTERS - 2];
247 
248     diff += (int)counts->switchable_interp[i][SWITCHABLE_FILTERS - 1] *
249             (pre_last_prob - cur_last_prob);
250   }
251 
252   // inter_mode_probs
253   for (i = 0; i < INTER_MODE_CONTEXTS; ++i) {
254     for (j = 0; j < INTER_MODES - 1; ++j) {
255       diff += (int)counts->inter_mode[i][j] *
256               (pre_fc->inter_mode_probs[i][j] - cur_fc->inter_mode_probs[i][j]);
257     }
258     pre_last_prob = MAX_PROB - pre_fc->inter_mode_probs[i][INTER_MODES - 2];
259     cur_last_prob = MAX_PROB - cur_fc->inter_mode_probs[i][INTER_MODES - 2];
260 
261     diff += (int)counts->inter_mode[i][INTER_MODES - 1] *
262             (pre_last_prob - cur_last_prob);
263   }
264 
265   // intra_inter_prob
266   for (i = 0; i < INTRA_INTER_CONTEXTS; ++i) {
267     diff += (int)counts->intra_inter[i][0] *
268             (pre_fc->intra_inter_prob[i] - cur_fc->intra_inter_prob[i]);
269 
270     pre_last_prob = MAX_PROB - pre_fc->intra_inter_prob[i];
271     cur_last_prob = MAX_PROB - cur_fc->intra_inter_prob[i];
272 
273     diff += (int)counts->intra_inter[i][1] * (pre_last_prob - cur_last_prob);
274   }
275 
276   // comp_inter_prob
277   for (i = 0; i < COMP_INTER_CONTEXTS; ++i) {
278     diff += (int)counts->comp_inter[i][0] *
279             (pre_fc->comp_inter_prob[i] - cur_fc->comp_inter_prob[i]);
280 
281     pre_last_prob = MAX_PROB - pre_fc->comp_inter_prob[i];
282     cur_last_prob = MAX_PROB - cur_fc->comp_inter_prob[i];
283 
284     diff += (int)counts->comp_inter[i][1] * (pre_last_prob - cur_last_prob);
285   }
286 
287   // single_ref_prob
288   for (i = 0; i < REF_CONTEXTS; ++i) {
289     for (j = 0; j < 2; ++j) {
290       diff += (int)counts->single_ref[i][j][0] *
291               (pre_fc->single_ref_prob[i][j] - cur_fc->single_ref_prob[i][j]);
292 
293       pre_last_prob = MAX_PROB - pre_fc->single_ref_prob[i][j];
294       cur_last_prob = MAX_PROB - cur_fc->single_ref_prob[i][j];
295 
296       diff +=
297           (int)counts->single_ref[i][j][1] * (pre_last_prob - cur_last_prob);
298     }
299   }
300 
301   // comp_ref_prob
302   for (i = 0; i < REF_CONTEXTS; ++i) {
303     diff += (int)counts->comp_ref[i][0] *
304             (pre_fc->comp_ref_prob[i] - cur_fc->comp_ref_prob[i]);
305 
306     pre_last_prob = MAX_PROB - pre_fc->comp_ref_prob[i];
307     cur_last_prob = MAX_PROB - cur_fc->comp_ref_prob[i];
308 
309     diff += (int)counts->comp_ref[i][1] * (pre_last_prob - cur_last_prob);
310   }
311 
312   // tx_probs
313   for (i = 0; i < TX_SIZE_CONTEXTS; ++i) {
314     // p32x32
315     for (j = 0; j < TX_SIZES - 1; ++j) {
316       diff += (int)counts->tx.p32x32[i][j] *
317               (pre_fc->tx_probs.p32x32[i][j] - cur_fc->tx_probs.p32x32[i][j]);
318     }
319     pre_last_prob = MAX_PROB - pre_fc->tx_probs.p32x32[i][TX_SIZES - 2];
320     cur_last_prob = MAX_PROB - cur_fc->tx_probs.p32x32[i][TX_SIZES - 2];
321 
322     diff += (int)counts->tx.p32x32[i][TX_SIZES - 1] *
323             (pre_last_prob - cur_last_prob);
324 
325     // p16x16
326     for (j = 0; j < TX_SIZES - 2; ++j) {
327       diff += (int)counts->tx.p16x16[i][j] *
328               (pre_fc->tx_probs.p16x16[i][j] - cur_fc->tx_probs.p16x16[i][j]);
329     }
330     pre_last_prob = MAX_PROB - pre_fc->tx_probs.p16x16[i][TX_SIZES - 3];
331     cur_last_prob = MAX_PROB - cur_fc->tx_probs.p16x16[i][TX_SIZES - 3];
332 
333     diff += (int)counts->tx.p16x16[i][TX_SIZES - 2] *
334             (pre_last_prob - cur_last_prob);
335 
336     // p8x8
337     for (j = 0; j < TX_SIZES - 3; ++j) {
338       diff += (int)counts->tx.p8x8[i][j] *
339               (pre_fc->tx_probs.p8x8[i][j] - cur_fc->tx_probs.p8x8[i][j]);
340     }
341     pre_last_prob = MAX_PROB - pre_fc->tx_probs.p8x8[i][TX_SIZES - 4];
342     cur_last_prob = MAX_PROB - cur_fc->tx_probs.p8x8[i][TX_SIZES - 4];
343 
344     diff +=
345         (int)counts->tx.p8x8[i][TX_SIZES - 3] * (pre_last_prob - cur_last_prob);
346   }
347 
348   // skip_probs
349   for (i = 0; i < SKIP_CONTEXTS; ++i) {
350     diff += (int)counts->skip[i][0] *
351             (pre_fc->skip_probs[i] - cur_fc->skip_probs[i]);
352 
353     pre_last_prob = MAX_PROB - pre_fc->skip_probs[i];
354     cur_last_prob = MAX_PROB - cur_fc->skip_probs[i];
355 
356     diff += (int)counts->skip[i][1] * (pre_last_prob - cur_last_prob);
357   }
358 
359   // mv
360   for (i = 0; i < MV_JOINTS - 1; ++i) {
361     diff += (int)counts->mv.joints[i] *
362             (pre_fc->nmvc.joints[i] - cur_fc->nmvc.joints[i]);
363   }
364   pre_last_prob = MAX_PROB - pre_fc->nmvc.joints[MV_JOINTS - 2];
365   cur_last_prob = MAX_PROB - cur_fc->nmvc.joints[MV_JOINTS - 2];
366 
367   diff +=
368       (int)counts->mv.joints[MV_JOINTS - 1] * (pre_last_prob - cur_last_prob);
369 
370   for (i = 0; i < 2; ++i) {
371     const nmv_component_counts *nmv_count = &counts->mv.comps[i];
372     const nmv_component *pre_nmv_prob = &pre_fc->nmvc.comps[i];
373     const nmv_component *cur_nmv_prob = &cur_fc->nmvc.comps[i];
374 
375     // sign
376     diff += (int)nmv_count->sign[0] * (pre_nmv_prob->sign - cur_nmv_prob->sign);
377 
378     pre_last_prob = MAX_PROB - pre_nmv_prob->sign;
379     cur_last_prob = MAX_PROB - cur_nmv_prob->sign;
380 
381     diff += (int)nmv_count->sign[1] * (pre_last_prob - cur_last_prob);
382 
383     // classes
384     for (j = 0; j < MV_CLASSES - 1; ++j) {
385       diff += (int)nmv_count->classes[j] *
386               (pre_nmv_prob->classes[j] - cur_nmv_prob->classes[j]);
387     }
388     pre_last_prob = MAX_PROB - pre_nmv_prob->classes[MV_CLASSES - 2];
389     cur_last_prob = MAX_PROB - cur_nmv_prob->classes[MV_CLASSES - 2];
390 
391     diff += (int)nmv_count->classes[MV_CLASSES - 1] *
392             (pre_last_prob - cur_last_prob);
393 
394     // class0
395     for (j = 0; j < CLASS0_SIZE - 1; ++j) {
396       diff += (int)nmv_count->class0[j] *
397               (pre_nmv_prob->class0[j] - cur_nmv_prob->class0[j]);
398     }
399     pre_last_prob = MAX_PROB - pre_nmv_prob->class0[CLASS0_SIZE - 2];
400     cur_last_prob = MAX_PROB - cur_nmv_prob->class0[CLASS0_SIZE - 2];
401 
402     diff += (int)nmv_count->class0[CLASS0_SIZE - 1] *
403             (pre_last_prob - cur_last_prob);
404 
405     // bits
406     for (j = 0; j < MV_OFFSET_BITS; ++j) {
407       diff += (int)nmv_count->bits[j][0] *
408               (pre_nmv_prob->bits[j] - cur_nmv_prob->bits[j]);
409 
410       pre_last_prob = MAX_PROB - pre_nmv_prob->bits[j];
411       cur_last_prob = MAX_PROB - cur_nmv_prob->bits[j];
412 
413       diff += (int)nmv_count->bits[j][1] * (pre_last_prob - cur_last_prob);
414     }
415 
416     // class0_fp
417     for (j = 0; j < CLASS0_SIZE; ++j) {
418       for (k = 0; k < MV_FP_SIZE - 1; ++k) {
419         diff += (int)nmv_count->class0_fp[j][k] *
420                 (pre_nmv_prob->class0_fp[j][k] - cur_nmv_prob->class0_fp[j][k]);
421       }
422       pre_last_prob = MAX_PROB - pre_nmv_prob->class0_fp[j][MV_FP_SIZE - 2];
423       cur_last_prob = MAX_PROB - cur_nmv_prob->class0_fp[j][MV_FP_SIZE - 2];
424 
425       diff += (int)nmv_count->class0_fp[j][MV_FP_SIZE - 1] *
426               (pre_last_prob - cur_last_prob);
427     }
428 
429     // fp
430     for (j = 0; j < MV_FP_SIZE - 1; ++j) {
431       diff +=
432           (int)nmv_count->fp[j] * (pre_nmv_prob->fp[j] - cur_nmv_prob->fp[j]);
433     }
434     pre_last_prob = MAX_PROB - pre_nmv_prob->fp[MV_FP_SIZE - 2];
435     cur_last_prob = MAX_PROB - cur_nmv_prob->fp[MV_FP_SIZE - 2];
436 
437     diff +=
438         (int)nmv_count->fp[MV_FP_SIZE - 1] * (pre_last_prob - cur_last_prob);
439 
440     // class0_hp
441     diff += (int)nmv_count->class0_hp[0] *
442             (pre_nmv_prob->class0_hp - cur_nmv_prob->class0_hp);
443 
444     pre_last_prob = MAX_PROB - pre_nmv_prob->class0_hp;
445     cur_last_prob = MAX_PROB - cur_nmv_prob->class0_hp;
446 
447     diff += (int)nmv_count->class0_hp[1] * (pre_last_prob - cur_last_prob);
448 
449     // hp
450     diff += (int)nmv_count->hp[0] * (pre_nmv_prob->hp - cur_nmv_prob->hp);
451 
452     pre_last_prob = MAX_PROB - pre_nmv_prob->hp;
453     cur_last_prob = MAX_PROB - cur_nmv_prob->hp;
454 
455     diff += (int)nmv_count->hp[1] * (pre_last_prob - cur_last_prob);
456   }
457 
458   return -diff;
459 }
460 #endif  // !CONFIG_REALTIME_ONLY
461 
462 // Test for whether to calculate metrics for the frame.
is_psnr_calc_enabled(const VP9_COMP * cpi)463 static int is_psnr_calc_enabled(const VP9_COMP *cpi) {
464   const VP9_COMMON *const cm = &cpi->common;
465   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
466 
467   return cpi->b_calculate_psnr && (oxcf->pass != 1) && cm->show_frame;
468 }
469 
470 /* clang-format off */
471 const Vp9LevelSpec vp9_level_defs[VP9_LEVELS] = {
472   //         sample rate    size   breadth  bitrate  cpb
473   { LEVEL_1,   829440,      36864,    512,   200,    400,    2, 1,  4,  8 },
474   { LEVEL_1_1, 2764800,     73728,    768,   800,    1000,   2, 1,  4,  8 },
475   { LEVEL_2,   4608000,     122880,   960,   1800,   1500,   2, 1,  4,  8 },
476   { LEVEL_2_1, 9216000,     245760,   1344,  3600,   2800,   2, 2,  4,  8 },
477   { LEVEL_3,   20736000,    552960,   2048,  7200,   6000,   2, 4,  4,  8 },
478   { LEVEL_3_1, 36864000,    983040,   2752,  12000,  10000,  2, 4,  4,  8 },
479   { LEVEL_4,   83558400,    2228224,  4160,  18000,  16000,  4, 4,  4,  8 },
480   { LEVEL_4_1, 160432128,   2228224,  4160,  30000,  18000,  4, 4,  5,  6 },
481   { LEVEL_5,   311951360,   8912896,  8384,  60000,  36000,  6, 8,  6,  4 },
482   { LEVEL_5_1, 588251136,   8912896,  8384,  120000, 46000,  8, 8,  10, 4 },
483   // TODO(huisu): update max_cpb_size for level 5_2 ~ 6_2 when
484   // they are finalized (currently tentative).
485   { LEVEL_5_2, 1176502272,  8912896,  8384,  180000, 90000,  8, 8,  10, 4 },
486   { LEVEL_6,   1176502272,  35651584, 16832, 180000, 90000,  8, 16, 10, 4 },
487   { LEVEL_6_1, 2353004544u, 35651584, 16832, 240000, 180000, 8, 16, 10, 4 },
488   { LEVEL_6_2, 4706009088u, 35651584, 16832, 480000, 360000, 8, 16, 10, 4 },
489 };
490 /* clang-format on */
491 
492 static const char *level_fail_messages[TARGET_LEVEL_FAIL_IDS] = {
493   "The average bit-rate is too high.",
494   "The picture size is too large.",
495   "The picture width/height is too large.",
496   "The luma sample rate is too large.",
497   "The CPB size is too large.",
498   "The compression ratio is too small",
499   "Too many column tiles are used.",
500   "The alt-ref distance is too small.",
501   "Too many reference buffers are used."
502 };
503 
Scale2Ratio(VPX_SCALING mode,int * hr,int * hs)504 static INLINE void Scale2Ratio(VPX_SCALING mode, int *hr, int *hs) {
505   switch (mode) {
506     case NORMAL:
507       *hr = 1;
508       *hs = 1;
509       break;
510     case FOURFIVE:
511       *hr = 4;
512       *hs = 5;
513       break;
514     case THREEFIVE:
515       *hr = 3;
516       *hs = 5;
517       break;
518     default:
519       assert(mode == ONETWO);
520       *hr = 1;
521       *hs = 2;
522       break;
523   }
524 }
525 
526 // Mark all inactive blocks as active. Other segmentation features may be set
527 // so memset cannot be used, instead only inactive blocks should be reset.
suppress_active_map(VP9_COMP * cpi)528 static void suppress_active_map(VP9_COMP *cpi) {
529   unsigned char *const seg_map = cpi->segmentation_map;
530 
531   if (cpi->active_map.enabled || cpi->active_map.update) {
532     const int rows = cpi->common.mi_rows;
533     const int cols = cpi->common.mi_cols;
534     int i;
535 
536     for (i = 0; i < rows * cols; ++i)
537       if (seg_map[i] == AM_SEGMENT_ID_INACTIVE)
538         seg_map[i] = AM_SEGMENT_ID_ACTIVE;
539   }
540 }
541 
apply_active_map(VP9_COMP * cpi)542 static void apply_active_map(VP9_COMP *cpi) {
543   struct segmentation *const seg = &cpi->common.seg;
544   unsigned char *const seg_map = cpi->segmentation_map;
545   const unsigned char *const active_map = cpi->active_map.map;
546   int i;
547 
548   assert(AM_SEGMENT_ID_ACTIVE == CR_SEGMENT_ID_BASE);
549 
550   if (frame_is_intra_only(&cpi->common)) {
551     cpi->active_map.enabled = 0;
552     cpi->active_map.update = 1;
553   }
554 
555   if (cpi->active_map.update) {
556     if (cpi->active_map.enabled) {
557       for (i = 0; i < cpi->common.mi_rows * cpi->common.mi_cols; ++i)
558         if (seg_map[i] == AM_SEGMENT_ID_ACTIVE) seg_map[i] = active_map[i];
559       vp9_enable_segmentation(seg);
560       vp9_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
561       vp9_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF);
562       // Setting the data to -MAX_LOOP_FILTER will result in the computed loop
563       // filter level being zero regardless of the value of seg->abs_delta.
564       vp9_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF,
565                       -MAX_LOOP_FILTER);
566     } else {
567       vp9_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
568       vp9_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF);
569       if (seg->enabled) {
570         seg->update_data = 1;
571         seg->update_map = 1;
572       }
573     }
574     cpi->active_map.update = 0;
575   }
576 }
577 
apply_roi_map(VP9_COMP * cpi)578 static void apply_roi_map(VP9_COMP *cpi) {
579   VP9_COMMON *cm = &cpi->common;
580   struct segmentation *const seg = &cm->seg;
581   vpx_roi_map_t *roi = &cpi->roi;
582   const int *delta_q = roi->delta_q;
583   const int *delta_lf = roi->delta_lf;
584   const int *skip = roi->skip;
585   int ref_frame[8];
586   int internal_delta_q[MAX_SEGMENTS];
587   int i;
588   static const int flag_list[4] = { 0, VP9_LAST_FLAG, VP9_GOLD_FLAG,
589                                     VP9_ALT_FLAG };
590 
591   // TODO(jianj): Investigate why ROI not working in speed < 5 or in non
592   // realtime mode.
593   if (cpi->oxcf.mode != REALTIME || cpi->oxcf.speed < 5) return;
594   if (!roi->enabled) return;
595 
596   memcpy(&ref_frame, roi->ref_frame, sizeof(ref_frame));
597 
598   vp9_enable_segmentation(seg);
599   vp9_clearall_segfeatures(seg);
600   // Select delta coding method;
601   seg->abs_delta = SEGMENT_DELTADATA;
602 
603   memcpy(cpi->segmentation_map, roi->roi_map, (cm->mi_rows * cm->mi_cols));
604 
605   for (i = 0; i < MAX_SEGMENTS; ++i) {
606     // Translate the external delta q values to internal values.
607     internal_delta_q[i] = vp9_quantizer_to_qindex(abs(delta_q[i]));
608     if (delta_q[i] < 0) internal_delta_q[i] = -internal_delta_q[i];
609     vp9_disable_segfeature(seg, i, SEG_LVL_ALT_Q);
610     vp9_disable_segfeature(seg, i, SEG_LVL_ALT_LF);
611     if (internal_delta_q[i] != 0) {
612       vp9_enable_segfeature(seg, i, SEG_LVL_ALT_Q);
613       vp9_set_segdata(seg, i, SEG_LVL_ALT_Q, internal_delta_q[i]);
614     }
615     if (delta_lf[i] != 0) {
616       vp9_enable_segfeature(seg, i, SEG_LVL_ALT_LF);
617       vp9_set_segdata(seg, i, SEG_LVL_ALT_LF, delta_lf[i]);
618     }
619     if (skip[i] != 0) {
620       vp9_enable_segfeature(seg, i, SEG_LVL_SKIP);
621       vp9_set_segdata(seg, i, SEG_LVL_SKIP, skip[i]);
622     }
623     if (ref_frame[i] >= 0) {
624       int valid_ref = 1;
625       // ALTREF is not used as reference for nonrd_pickmode with 0 lag.
626       if (ref_frame[i] == ALTREF_FRAME && cpi->sf.use_nonrd_pick_mode)
627         valid_ref = 0;
628       // If GOLDEN is selected, make sure it's set as reference.
629       if (ref_frame[i] == GOLDEN_FRAME &&
630           !(cpi->ref_frame_flags & flag_list[ref_frame[i]])) {
631         valid_ref = 0;
632       }
633       // GOLDEN was updated in previous encoded frame, so GOLDEN and LAST are
634       // same reference.
635       if (ref_frame[i] == GOLDEN_FRAME && cpi->rc.frames_since_golden == 0)
636         ref_frame[i] = LAST_FRAME;
637       if (valid_ref) {
638         vp9_enable_segfeature(seg, i, SEG_LVL_REF_FRAME);
639         vp9_set_segdata(seg, i, SEG_LVL_REF_FRAME, ref_frame[i]);
640       }
641     }
642   }
643   roi->enabled = 1;
644 }
645 
init_level_info(Vp9LevelInfo * level_info)646 static void init_level_info(Vp9LevelInfo *level_info) {
647   Vp9LevelStats *const level_stats = &level_info->level_stats;
648   Vp9LevelSpec *const level_spec = &level_info->level_spec;
649 
650   memset(level_stats, 0, sizeof(*level_stats));
651   memset(level_spec, 0, sizeof(*level_spec));
652   level_spec->level = LEVEL_UNKNOWN;
653   level_spec->min_altref_distance = INT_MAX;
654 }
655 
check_seg_range(int seg_data[8],int range)656 static int check_seg_range(int seg_data[8], int range) {
657   int i;
658   for (i = 0; i < 8; ++i) {
659     // Note abs() alone can't be used as the behavior of abs(INT_MIN) is
660     // undefined.
661     if (seg_data[i] > range || seg_data[i] < -range) {
662       return 0;
663     }
664   }
665   return 1;
666 }
667 
vp9_get_level(const Vp9LevelSpec * const level_spec)668 VP9_LEVEL vp9_get_level(const Vp9LevelSpec *const level_spec) {
669   int i;
670   const Vp9LevelSpec *this_level;
671 
672   vpx_clear_system_state();
673 
674   for (i = 0; i < VP9_LEVELS; ++i) {
675     this_level = &vp9_level_defs[i];
676     if ((double)level_spec->max_luma_sample_rate >
677             (double)this_level->max_luma_sample_rate *
678                 (1 + SAMPLE_RATE_GRACE_P) ||
679         level_spec->max_luma_picture_size > this_level->max_luma_picture_size ||
680         level_spec->max_luma_picture_breadth >
681             this_level->max_luma_picture_breadth ||
682         level_spec->average_bitrate > this_level->average_bitrate ||
683         level_spec->max_cpb_size > this_level->max_cpb_size ||
684         level_spec->compression_ratio < this_level->compression_ratio ||
685         level_spec->max_col_tiles > this_level->max_col_tiles ||
686         level_spec->min_altref_distance < this_level->min_altref_distance ||
687         level_spec->max_ref_frame_buffers > this_level->max_ref_frame_buffers)
688       continue;
689     break;
690   }
691   return (i == VP9_LEVELS) ? LEVEL_UNKNOWN : vp9_level_defs[i].level;
692 }
693 
vp9_set_roi_map(VP9_COMP * cpi,unsigned char * map,unsigned int rows,unsigned int cols,int delta_q[8],int delta_lf[8],int skip[8],int ref_frame[8])694 int vp9_set_roi_map(VP9_COMP *cpi, unsigned char *map, unsigned int rows,
695                     unsigned int cols, int delta_q[8], int delta_lf[8],
696                     int skip[8], int ref_frame[8]) {
697   VP9_COMMON *cm = &cpi->common;
698   vpx_roi_map_t *roi = &cpi->roi;
699   const int range = 63;
700   const int ref_frame_range = 3;  // Alt-ref
701   const int skip_range = 1;
702   const int frame_rows = cpi->common.mi_rows;
703   const int frame_cols = cpi->common.mi_cols;
704 
705   // Check number of rows and columns match
706   if (frame_rows != (int)rows || frame_cols != (int)cols) {
707     return -1;
708   }
709 
710   if (!check_seg_range(delta_q, range) || !check_seg_range(delta_lf, range) ||
711       !check_seg_range(ref_frame, ref_frame_range) ||
712       !check_seg_range(skip, skip_range))
713     return -1;
714 
715   // Also disable segmentation if no deltas are specified.
716   if (!map ||
717       (!(delta_q[0] | delta_q[1] | delta_q[2] | delta_q[3] | delta_q[4] |
718          delta_q[5] | delta_q[6] | delta_q[7] | delta_lf[0] | delta_lf[1] |
719          delta_lf[2] | delta_lf[3] | delta_lf[4] | delta_lf[5] | delta_lf[6] |
720          delta_lf[7] | skip[0] | skip[1] | skip[2] | skip[3] | skip[4] |
721          skip[5] | skip[6] | skip[7]) &&
722        (ref_frame[0] == -1 && ref_frame[1] == -1 && ref_frame[2] == -1 &&
723         ref_frame[3] == -1 && ref_frame[4] == -1 && ref_frame[5] == -1 &&
724         ref_frame[6] == -1 && ref_frame[7] == -1))) {
725     vp9_disable_segmentation(&cm->seg);
726     cpi->roi.enabled = 0;
727     return 0;
728   }
729 
730   if (roi->roi_map) {
731     vpx_free(roi->roi_map);
732     roi->roi_map = NULL;
733   }
734   CHECK_MEM_ERROR(cm, roi->roi_map, vpx_malloc(rows * cols));
735 
736   // Copy to ROI structure in the compressor.
737   memcpy(roi->roi_map, map, rows * cols);
738   memcpy(&roi->delta_q, delta_q, MAX_SEGMENTS * sizeof(delta_q[0]));
739   memcpy(&roi->delta_lf, delta_lf, MAX_SEGMENTS * sizeof(delta_lf[0]));
740   memcpy(&roi->skip, skip, MAX_SEGMENTS * sizeof(skip[0]));
741   memcpy(&roi->ref_frame, ref_frame, MAX_SEGMENTS * sizeof(ref_frame[0]));
742   roi->enabled = 1;
743   roi->rows = rows;
744   roi->cols = cols;
745 
746   return 0;
747 }
748 
vp9_set_active_map(VP9_COMP * cpi,unsigned char * new_map_16x16,int rows,int cols)749 int vp9_set_active_map(VP9_COMP *cpi, unsigned char *new_map_16x16, int rows,
750                        int cols) {
751   if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols) {
752     unsigned char *const active_map_8x8 = cpi->active_map.map;
753     const int mi_rows = cpi->common.mi_rows;
754     const int mi_cols = cpi->common.mi_cols;
755     cpi->active_map.update = 1;
756     if (new_map_16x16) {
757       int r, c;
758       for (r = 0; r < mi_rows; ++r) {
759         for (c = 0; c < mi_cols; ++c) {
760           active_map_8x8[r * mi_cols + c] =
761               new_map_16x16[(r >> 1) * cols + (c >> 1)]
762                   ? AM_SEGMENT_ID_ACTIVE
763                   : AM_SEGMENT_ID_INACTIVE;
764         }
765       }
766       cpi->active_map.enabled = 1;
767     } else {
768       cpi->active_map.enabled = 0;
769     }
770     return 0;
771   } else {
772     return -1;
773   }
774 }
775 
vp9_get_active_map(VP9_COMP * cpi,unsigned char * new_map_16x16,int rows,int cols)776 int vp9_get_active_map(VP9_COMP *cpi, unsigned char *new_map_16x16, int rows,
777                        int cols) {
778   if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols &&
779       new_map_16x16) {
780     unsigned char *const seg_map_8x8 = cpi->segmentation_map;
781     const int mi_rows = cpi->common.mi_rows;
782     const int mi_cols = cpi->common.mi_cols;
783     memset(new_map_16x16, !cpi->active_map.enabled, rows * cols);
784     if (cpi->active_map.enabled) {
785       int r, c;
786       for (r = 0; r < mi_rows; ++r) {
787         for (c = 0; c < mi_cols; ++c) {
788           // Cyclic refresh segments are considered active despite not having
789           // AM_SEGMENT_ID_ACTIVE
790           new_map_16x16[(r >> 1) * cols + (c >> 1)] |=
791               seg_map_8x8[r * mi_cols + c] != AM_SEGMENT_ID_INACTIVE;
792         }
793       }
794     }
795     return 0;
796   } else {
797     return -1;
798   }
799 }
800 
vp9_set_high_precision_mv(VP9_COMP * cpi,int allow_high_precision_mv)801 void vp9_set_high_precision_mv(VP9_COMP *cpi, int allow_high_precision_mv) {
802   MACROBLOCK *const mb = &cpi->td.mb;
803   cpi->common.allow_high_precision_mv = allow_high_precision_mv;
804   if (cpi->common.allow_high_precision_mv) {
805     mb->mvcost = mb->nmvcost_hp;
806     mb->mvsadcost = mb->nmvsadcost_hp;
807   } else {
808     mb->mvcost = mb->nmvcost;
809     mb->mvsadcost = mb->nmvsadcost;
810   }
811 }
812 
setup_frame(VP9_COMP * cpi)813 static void setup_frame(VP9_COMP *cpi) {
814   VP9_COMMON *const cm = &cpi->common;
815   // Set up entropy context depending on frame type. The decoder mandates
816   // the use of the default context, index 0, for keyframes and inter
817   // frames where the error_resilient_mode or intra_only flag is set. For
818   // other inter-frames the encoder currently uses only two contexts;
819   // context 1 for ALTREF frames and context 0 for the others.
820   if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
821     vp9_setup_past_independence(cm);
822   } else {
823     if (!cpi->use_svc) cm->frame_context_idx = cpi->refresh_alt_ref_frame;
824   }
825 
826   // TODO(jingning): Overwrite the frame_context_idx index in multi-layer ARF
827   // case. Need some further investigation on if we could apply this to single
828   // layer ARF case as well.
829   if (cpi->multi_layer_arf && !cpi->use_svc) {
830     GF_GROUP *const gf_group = &cpi->twopass.gf_group;
831     const int gf_group_index = gf_group->index;
832     const int boost_frame =
833         !cpi->rc.is_src_frame_alt_ref &&
834         (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame);
835 
836     // frame_context_idx           Frame Type
837     //        0              Intra only frame, base layer ARF
838     //        1              ARFs with layer depth = 2,3
839     //        2              ARFs with layer depth > 3
840     //        3              Non-boosted frames
841     if (frame_is_intra_only(cm)) {
842       cm->frame_context_idx = 0;
843     } else if (boost_frame) {
844       if (gf_group->rf_level[gf_group_index] == GF_ARF_STD)
845         cm->frame_context_idx = 0;
846       else if (gf_group->layer_depth[gf_group_index] <= 3)
847         cm->frame_context_idx = 1;
848       else
849         cm->frame_context_idx = 2;
850     } else {
851       cm->frame_context_idx = 3;
852     }
853   }
854 
855   if (cm->frame_type == KEY_FRAME) {
856     cpi->refresh_golden_frame = 1;
857     cpi->refresh_alt_ref_frame = 1;
858     vp9_zero(cpi->interp_filter_selected);
859   } else {
860     *cm->fc = cm->frame_contexts[cm->frame_context_idx];
861     vp9_zero(cpi->interp_filter_selected[0]);
862   }
863 }
864 
vp9_enc_setup_mi(VP9_COMMON * cm)865 static void vp9_enc_setup_mi(VP9_COMMON *cm) {
866   int i;
867   cm->mi = cm->mip + cm->mi_stride + 1;
868   memset(cm->mip, 0, cm->mi_stride * (cm->mi_rows + 1) * sizeof(*cm->mip));
869   cm->prev_mi = cm->prev_mip + cm->mi_stride + 1;
870   // Clear top border row
871   memset(cm->prev_mip, 0, sizeof(*cm->prev_mip) * cm->mi_stride);
872   // Clear left border column
873   for (i = 1; i < cm->mi_rows + 1; ++i)
874     memset(&cm->prev_mip[i * cm->mi_stride], 0, sizeof(*cm->prev_mip));
875 
876   cm->mi_grid_visible = cm->mi_grid_base + cm->mi_stride + 1;
877   cm->prev_mi_grid_visible = cm->prev_mi_grid_base + cm->mi_stride + 1;
878 
879   memset(cm->mi_grid_base, 0,
880          cm->mi_stride * (cm->mi_rows + 1) * sizeof(*cm->mi_grid_base));
881 }
882 
vp9_enc_alloc_mi(VP9_COMMON * cm,int mi_size)883 static int vp9_enc_alloc_mi(VP9_COMMON *cm, int mi_size) {
884   cm->mip = vpx_calloc(mi_size, sizeof(*cm->mip));
885   if (!cm->mip) return 1;
886   cm->prev_mip = vpx_calloc(mi_size, sizeof(*cm->prev_mip));
887   if (!cm->prev_mip) return 1;
888   cm->mi_alloc_size = mi_size;
889 
890   cm->mi_grid_base = (MODE_INFO **)vpx_calloc(mi_size, sizeof(MODE_INFO *));
891   if (!cm->mi_grid_base) return 1;
892   cm->prev_mi_grid_base =
893       (MODE_INFO **)vpx_calloc(mi_size, sizeof(MODE_INFO *));
894   if (!cm->prev_mi_grid_base) return 1;
895 
896   return 0;
897 }
898 
vp9_enc_free_mi(VP9_COMMON * cm)899 static void vp9_enc_free_mi(VP9_COMMON *cm) {
900   vpx_free(cm->mip);
901   cm->mip = NULL;
902   vpx_free(cm->prev_mip);
903   cm->prev_mip = NULL;
904   vpx_free(cm->mi_grid_base);
905   cm->mi_grid_base = NULL;
906   vpx_free(cm->prev_mi_grid_base);
907   cm->prev_mi_grid_base = NULL;
908   cm->mi_alloc_size = 0;
909 }
910 
vp9_swap_mi_and_prev_mi(VP9_COMMON * cm)911 static void vp9_swap_mi_and_prev_mi(VP9_COMMON *cm) {
912   // Current mip will be the prev_mip for the next frame.
913   MODE_INFO **temp_base = cm->prev_mi_grid_base;
914   MODE_INFO *temp = cm->prev_mip;
915 
916   // Skip update prev_mi frame in show_existing_frame mode.
917   if (cm->show_existing_frame) return;
918 
919   cm->prev_mip = cm->mip;
920   cm->mip = temp;
921 
922   // Update the upper left visible macroblock ptrs.
923   cm->mi = cm->mip + cm->mi_stride + 1;
924   cm->prev_mi = cm->prev_mip + cm->mi_stride + 1;
925 
926   cm->prev_mi_grid_base = cm->mi_grid_base;
927   cm->mi_grid_base = temp_base;
928   cm->mi_grid_visible = cm->mi_grid_base + cm->mi_stride + 1;
929   cm->prev_mi_grid_visible = cm->prev_mi_grid_base + cm->mi_stride + 1;
930 }
931 
vp9_initialize_enc(void)932 void vp9_initialize_enc(void) {
933   static volatile int init_done = 0;
934 
935   if (!init_done) {
936     vp9_rtcd();
937     vpx_dsp_rtcd();
938     vpx_scale_rtcd();
939     vp9_init_intra_predictors();
940     vp9_init_me_luts();
941     vp9_rc_init_minq_luts();
942     vp9_entropy_mv_init();
943 #if !CONFIG_REALTIME_ONLY
944     vp9_temporal_filter_init();
945 #endif
946     init_done = 1;
947   }
948 }
949 
dealloc_compressor_data(VP9_COMP * cpi)950 static void dealloc_compressor_data(VP9_COMP *cpi) {
951   VP9_COMMON *const cm = &cpi->common;
952   int i;
953 
954   vpx_free(cpi->mbmi_ext_base);
955   cpi->mbmi_ext_base = NULL;
956 
957   vpx_free(cpi->tile_data);
958   cpi->tile_data = NULL;
959 
960   vpx_free(cpi->segmentation_map);
961   cpi->segmentation_map = NULL;
962   vpx_free(cpi->coding_context.last_frame_seg_map_copy);
963   cpi->coding_context.last_frame_seg_map_copy = NULL;
964 
965   vpx_free(cpi->nmvcosts[0]);
966   vpx_free(cpi->nmvcosts[1]);
967   cpi->nmvcosts[0] = NULL;
968   cpi->nmvcosts[1] = NULL;
969 
970   vpx_free(cpi->nmvcosts_hp[0]);
971   vpx_free(cpi->nmvcosts_hp[1]);
972   cpi->nmvcosts_hp[0] = NULL;
973   cpi->nmvcosts_hp[1] = NULL;
974 
975   vpx_free(cpi->nmvsadcosts[0]);
976   vpx_free(cpi->nmvsadcosts[1]);
977   cpi->nmvsadcosts[0] = NULL;
978   cpi->nmvsadcosts[1] = NULL;
979 
980   vpx_free(cpi->nmvsadcosts_hp[0]);
981   vpx_free(cpi->nmvsadcosts_hp[1]);
982   cpi->nmvsadcosts_hp[0] = NULL;
983   cpi->nmvsadcosts_hp[1] = NULL;
984 
985   vpx_free(cpi->skin_map);
986   cpi->skin_map = NULL;
987 
988   vpx_free(cpi->prev_partition);
989   cpi->prev_partition = NULL;
990 
991   vpx_free(cpi->svc.prev_partition_svc);
992   cpi->svc.prev_partition_svc = NULL;
993 
994   vpx_free(cpi->prev_segment_id);
995   cpi->prev_segment_id = NULL;
996 
997   vpx_free(cpi->prev_variance_low);
998   cpi->prev_variance_low = NULL;
999 
1000   vpx_free(cpi->copied_frame_cnt);
1001   cpi->copied_frame_cnt = NULL;
1002 
1003   vpx_free(cpi->content_state_sb_fd);
1004   cpi->content_state_sb_fd = NULL;
1005 
1006   vpx_free(cpi->count_arf_frame_usage);
1007   cpi->count_arf_frame_usage = NULL;
1008   vpx_free(cpi->count_lastgolden_frame_usage);
1009   cpi->count_lastgolden_frame_usage = NULL;
1010 
1011   vp9_cyclic_refresh_free(cpi->cyclic_refresh);
1012   cpi->cyclic_refresh = NULL;
1013 
1014   vpx_free(cpi->active_map.map);
1015   cpi->active_map.map = NULL;
1016 
1017   vpx_free(cpi->roi.roi_map);
1018   cpi->roi.roi_map = NULL;
1019 
1020   vpx_free(cpi->consec_zero_mv);
1021   cpi->consec_zero_mv = NULL;
1022 
1023   vpx_free(cpi->mb_wiener_variance);
1024   cpi->mb_wiener_variance = NULL;
1025 
1026   vpx_free(cpi->mi_ssim_rdmult_scaling_factors);
1027   cpi->mi_ssim_rdmult_scaling_factors = NULL;
1028 
1029 #if CONFIG_RATE_CTRL
1030   if (cpi->oxcf.use_simple_encode_api) {
1031     free_partition_info(cpi);
1032     free_motion_vector_info(cpi);
1033     free_fp_motion_vector_info(cpi);
1034     free_tpl_stats_info(cpi);
1035   }
1036 #endif
1037 
1038   vp9_free_ref_frame_buffers(cm->buffer_pool);
1039 #if CONFIG_VP9_POSTPROC
1040   vp9_free_postproc_buffers(cm);
1041 #endif
1042   vp9_free_context_buffers(cm);
1043 
1044   vpx_free_frame_buffer(&cpi->last_frame_uf);
1045   vpx_free_frame_buffer(&cpi->scaled_source);
1046   vpx_free_frame_buffer(&cpi->scaled_last_source);
1047   vpx_free_frame_buffer(&cpi->alt_ref_buffer);
1048 #ifdef ENABLE_KF_DENOISE
1049   vpx_free_frame_buffer(&cpi->raw_unscaled_source);
1050   vpx_free_frame_buffer(&cpi->raw_scaled_source);
1051 #endif
1052 
1053   vp9_lookahead_destroy(cpi->lookahead);
1054 
1055   vpx_free(cpi->tile_tok[0][0]);
1056   cpi->tile_tok[0][0] = 0;
1057 
1058   vpx_free(cpi->tplist[0][0]);
1059   cpi->tplist[0][0] = NULL;
1060 
1061   vp9_free_pc_tree(&cpi->td);
1062 
1063   for (i = 0; i < cpi->svc.number_spatial_layers; ++i) {
1064     LAYER_CONTEXT *const lc = &cpi->svc.layer_context[i];
1065     vpx_free(lc->rc_twopass_stats_in.buf);
1066     lc->rc_twopass_stats_in.buf = NULL;
1067     lc->rc_twopass_stats_in.sz = 0;
1068   }
1069 
1070   if (cpi->source_diff_var != NULL) {
1071     vpx_free(cpi->source_diff_var);
1072     cpi->source_diff_var = NULL;
1073   }
1074 
1075   for (i = 0; i < MAX_LAG_BUFFERS; ++i) {
1076     vpx_free_frame_buffer(&cpi->svc.scaled_frames[i]);
1077   }
1078   memset(&cpi->svc.scaled_frames[0], 0,
1079          MAX_LAG_BUFFERS * sizeof(cpi->svc.scaled_frames[0]));
1080 
1081   vpx_free_frame_buffer(&cpi->svc.scaled_temp);
1082   memset(&cpi->svc.scaled_temp, 0, sizeof(cpi->svc.scaled_temp));
1083 
1084   vpx_free_frame_buffer(&cpi->svc.empty_frame.img);
1085   memset(&cpi->svc.empty_frame, 0, sizeof(cpi->svc.empty_frame));
1086 
1087   vp9_free_svc_cyclic_refresh(cpi);
1088 }
1089 
save_coding_context(VP9_COMP * cpi)1090 static void save_coding_context(VP9_COMP *cpi) {
1091   CODING_CONTEXT *const cc = &cpi->coding_context;
1092   VP9_COMMON *cm = &cpi->common;
1093 
1094   // Stores a snapshot of key state variables which can subsequently be
1095   // restored with a call to vp9_restore_coding_context. These functions are
1096   // intended for use in a re-code loop in vp9_compress_frame where the
1097   // quantizer value is adjusted between loop iterations.
1098   vp9_copy(cc->nmvjointcost, cpi->td.mb.nmvjointcost);
1099 
1100   memcpy(cc->nmvcosts[0], cpi->nmvcosts[0],
1101          MV_VALS * sizeof(*cpi->nmvcosts[0]));
1102   memcpy(cc->nmvcosts[1], cpi->nmvcosts[1],
1103          MV_VALS * sizeof(*cpi->nmvcosts[1]));
1104   memcpy(cc->nmvcosts_hp[0], cpi->nmvcosts_hp[0],
1105          MV_VALS * sizeof(*cpi->nmvcosts_hp[0]));
1106   memcpy(cc->nmvcosts_hp[1], cpi->nmvcosts_hp[1],
1107          MV_VALS * sizeof(*cpi->nmvcosts_hp[1]));
1108 
1109   vp9_copy(cc->segment_pred_probs, cm->seg.pred_probs);
1110 
1111   memcpy(cpi->coding_context.last_frame_seg_map_copy, cm->last_frame_seg_map,
1112          (cm->mi_rows * cm->mi_cols));
1113 
1114   vp9_copy(cc->last_ref_lf_deltas, cm->lf.last_ref_deltas);
1115   vp9_copy(cc->last_mode_lf_deltas, cm->lf.last_mode_deltas);
1116 
1117   cc->fc = *cm->fc;
1118 }
1119 
restore_coding_context(VP9_COMP * cpi)1120 static void restore_coding_context(VP9_COMP *cpi) {
1121   CODING_CONTEXT *const cc = &cpi->coding_context;
1122   VP9_COMMON *cm = &cpi->common;
1123 
1124   // Restore key state variables to the snapshot state stored in the
1125   // previous call to vp9_save_coding_context.
1126   vp9_copy(cpi->td.mb.nmvjointcost, cc->nmvjointcost);
1127 
1128   memcpy(cpi->nmvcosts[0], cc->nmvcosts[0], MV_VALS * sizeof(*cc->nmvcosts[0]));
1129   memcpy(cpi->nmvcosts[1], cc->nmvcosts[1], MV_VALS * sizeof(*cc->nmvcosts[1]));
1130   memcpy(cpi->nmvcosts_hp[0], cc->nmvcosts_hp[0],
1131          MV_VALS * sizeof(*cc->nmvcosts_hp[0]));
1132   memcpy(cpi->nmvcosts_hp[1], cc->nmvcosts_hp[1],
1133          MV_VALS * sizeof(*cc->nmvcosts_hp[1]));
1134 
1135   vp9_copy(cm->seg.pred_probs, cc->segment_pred_probs);
1136 
1137   memcpy(cm->last_frame_seg_map, cpi->coding_context.last_frame_seg_map_copy,
1138          (cm->mi_rows * cm->mi_cols));
1139 
1140   vp9_copy(cm->lf.last_ref_deltas, cc->last_ref_lf_deltas);
1141   vp9_copy(cm->lf.last_mode_deltas, cc->last_mode_lf_deltas);
1142 
1143   *cm->fc = cc->fc;
1144 }
1145 
1146 #if !CONFIG_REALTIME_ONLY
configure_static_seg_features(VP9_COMP * cpi)1147 static void configure_static_seg_features(VP9_COMP *cpi) {
1148   VP9_COMMON *const cm = &cpi->common;
1149   const RATE_CONTROL *const rc = &cpi->rc;
1150   struct segmentation *const seg = &cm->seg;
1151 
1152   int high_q = (int)(rc->avg_q > 48.0);
1153   int qi_delta;
1154 
1155   // Disable and clear down for KF
1156   if (cm->frame_type == KEY_FRAME) {
1157     // Clear down the global segmentation map
1158     memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
1159     seg->update_map = 0;
1160     seg->update_data = 0;
1161     cpi->static_mb_pct = 0;
1162 
1163     // Disable segmentation
1164     vp9_disable_segmentation(seg);
1165 
1166     // Clear down the segment features.
1167     vp9_clearall_segfeatures(seg);
1168   } else if (cpi->refresh_alt_ref_frame) {
1169     // If this is an alt ref frame
1170     // Clear down the global segmentation map
1171     memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
1172     seg->update_map = 0;
1173     seg->update_data = 0;
1174     cpi->static_mb_pct = 0;
1175 
1176     // Disable segmentation and individual segment features by default
1177     vp9_disable_segmentation(seg);
1178     vp9_clearall_segfeatures(seg);
1179 
1180     // Scan frames from current to arf frame.
1181     // This function re-enables segmentation if appropriate.
1182     vp9_update_mbgraph_stats(cpi);
1183 
1184     // If segmentation was enabled set those features needed for the
1185     // arf itself.
1186     if (seg->enabled) {
1187       seg->update_map = 1;
1188       seg->update_data = 1;
1189 
1190       qi_delta =
1191           vp9_compute_qdelta(rc, rc->avg_q, rc->avg_q * 0.875, cm->bit_depth);
1192       vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta - 2);
1193       vp9_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
1194 
1195       vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
1196       vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
1197 
1198       // Where relevant assume segment data is delta data
1199       seg->abs_delta = SEGMENT_DELTADATA;
1200     }
1201   } else if (seg->enabled) {
1202     // All other frames if segmentation has been enabled
1203 
1204     // First normal frame in a valid gf or alt ref group
1205     if (rc->frames_since_golden == 0) {
1206       // Set up segment features for normal frames in an arf group
1207       if (rc->source_alt_ref_active) {
1208         seg->update_map = 0;
1209         seg->update_data = 1;
1210         seg->abs_delta = SEGMENT_DELTADATA;
1211 
1212         qi_delta =
1213             vp9_compute_qdelta(rc, rc->avg_q, rc->avg_q * 1.125, cm->bit_depth);
1214         vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta + 2);
1215         vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
1216 
1217         vp9_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
1218         vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
1219 
1220         // Segment coding disabled for compred testing
1221         if (high_q || (cpi->static_mb_pct == 100)) {
1222           vp9_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
1223           vp9_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
1224           vp9_enable_segfeature(seg, 1, SEG_LVL_SKIP);
1225         }
1226       } else {
1227         // Disable segmentation and clear down features if alt ref
1228         // is not active for this group
1229 
1230         vp9_disable_segmentation(seg);
1231 
1232         memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
1233 
1234         seg->update_map = 0;
1235         seg->update_data = 0;
1236 
1237         vp9_clearall_segfeatures(seg);
1238       }
1239     } else if (rc->is_src_frame_alt_ref) {
1240       // Special case where we are coding over the top of a previous
1241       // alt ref frame.
1242       // Segment coding disabled for compred testing
1243 
1244       // Enable ref frame features for segment 0 as well
1245       vp9_enable_segfeature(seg, 0, SEG_LVL_REF_FRAME);
1246       vp9_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
1247 
1248       // All mbs should use ALTREF_FRAME
1249       vp9_clear_segdata(seg, 0, SEG_LVL_REF_FRAME);
1250       vp9_set_segdata(seg, 0, SEG_LVL_REF_FRAME, ALTREF_FRAME);
1251       vp9_clear_segdata(seg, 1, SEG_LVL_REF_FRAME);
1252       vp9_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
1253 
1254       // Skip all MBs if high Q (0,0 mv and skip coeffs)
1255       if (high_q) {
1256         vp9_enable_segfeature(seg, 0, SEG_LVL_SKIP);
1257         vp9_enable_segfeature(seg, 1, SEG_LVL_SKIP);
1258       }
1259       // Enable data update
1260       seg->update_data = 1;
1261     } else {
1262       // All other frames.
1263 
1264       // No updates.. leave things as they are.
1265       seg->update_map = 0;
1266       seg->update_data = 0;
1267     }
1268   }
1269 }
1270 #endif  // !CONFIG_REALTIME_ONLY
1271 
update_reference_segmentation_map(VP9_COMP * cpi)1272 static void update_reference_segmentation_map(VP9_COMP *cpi) {
1273   VP9_COMMON *const cm = &cpi->common;
1274   MODE_INFO **mi_8x8_ptr = cm->mi_grid_visible;
1275   uint8_t *cache_ptr = cm->last_frame_seg_map;
1276   int row, col;
1277 
1278   for (row = 0; row < cm->mi_rows; row++) {
1279     MODE_INFO **mi_8x8 = mi_8x8_ptr;
1280     uint8_t *cache = cache_ptr;
1281     for (col = 0; col < cm->mi_cols; col++, mi_8x8++, cache++)
1282       cache[0] = mi_8x8[0]->segment_id;
1283     mi_8x8_ptr += cm->mi_stride;
1284     cache_ptr += cm->mi_cols;
1285   }
1286 }
1287 
alloc_raw_frame_buffers(VP9_COMP * cpi)1288 static void alloc_raw_frame_buffers(VP9_COMP *cpi) {
1289   VP9_COMMON *cm = &cpi->common;
1290   const VP9EncoderConfig *oxcf = &cpi->oxcf;
1291 
1292   if (!cpi->lookahead)
1293     cpi->lookahead = vp9_lookahead_init(oxcf->width, oxcf->height,
1294                                         cm->subsampling_x, cm->subsampling_y,
1295 #if CONFIG_VP9_HIGHBITDEPTH
1296                                         cm->use_highbitdepth,
1297 #endif
1298                                         oxcf->lag_in_frames);
1299   if (!cpi->lookahead)
1300     vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
1301                        "Failed to allocate lag buffers");
1302 
1303   // TODO(agrange) Check if ARF is enabled and skip allocation if not.
1304   if (vpx_realloc_frame_buffer(&cpi->alt_ref_buffer, oxcf->width, oxcf->height,
1305                                cm->subsampling_x, cm->subsampling_y,
1306 #if CONFIG_VP9_HIGHBITDEPTH
1307                                cm->use_highbitdepth,
1308 #endif
1309                                VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment,
1310                                NULL, NULL, NULL))
1311     vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
1312                        "Failed to allocate altref buffer");
1313 }
1314 
alloc_util_frame_buffers(VP9_COMP * cpi)1315 static void alloc_util_frame_buffers(VP9_COMP *cpi) {
1316   VP9_COMMON *const cm = &cpi->common;
1317   if (vpx_realloc_frame_buffer(&cpi->last_frame_uf, cm->width, cm->height,
1318                                cm->subsampling_x, cm->subsampling_y,
1319 #if CONFIG_VP9_HIGHBITDEPTH
1320                                cm->use_highbitdepth,
1321 #endif
1322                                VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment,
1323                                NULL, NULL, NULL))
1324     vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
1325                        "Failed to allocate last frame buffer");
1326 
1327   if (vpx_realloc_frame_buffer(&cpi->scaled_source, cm->width, cm->height,
1328                                cm->subsampling_x, cm->subsampling_y,
1329 #if CONFIG_VP9_HIGHBITDEPTH
1330                                cm->use_highbitdepth,
1331 #endif
1332                                VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment,
1333                                NULL, NULL, NULL))
1334     vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
1335                        "Failed to allocate scaled source buffer");
1336 
1337   // For 1 pass cbr: allocate scaled_frame that may be used as an intermediate
1338   // buffer for a 2 stage down-sampling: two stages of 1:2 down-sampling for a
1339   // target of 1/4x1/4. number_spatial_layers must be greater than 2.
1340   if (is_one_pass_cbr_svc(cpi) && !cpi->svc.scaled_temp_is_alloc &&
1341       cpi->svc.number_spatial_layers > 2) {
1342     cpi->svc.scaled_temp_is_alloc = 1;
1343     if (vpx_realloc_frame_buffer(
1344             &cpi->svc.scaled_temp, cm->width >> 1, cm->height >> 1,
1345             cm->subsampling_x, cm->subsampling_y,
1346 #if CONFIG_VP9_HIGHBITDEPTH
1347             cm->use_highbitdepth,
1348 #endif
1349             VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment, NULL, NULL, NULL))
1350       vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1351                          "Failed to allocate scaled_frame for svc ");
1352   }
1353 
1354   if (vpx_realloc_frame_buffer(&cpi->scaled_last_source, cm->width, cm->height,
1355                                cm->subsampling_x, cm->subsampling_y,
1356 #if CONFIG_VP9_HIGHBITDEPTH
1357                                cm->use_highbitdepth,
1358 #endif
1359                                VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment,
1360                                NULL, NULL, NULL))
1361     vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
1362                        "Failed to allocate scaled last source buffer");
1363 #ifdef ENABLE_KF_DENOISE
1364   if (vpx_realloc_frame_buffer(&cpi->raw_unscaled_source, cm->width, cm->height,
1365                                cm->subsampling_x, cm->subsampling_y,
1366 #if CONFIG_VP9_HIGHBITDEPTH
1367                                cm->use_highbitdepth,
1368 #endif
1369                                VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment,
1370                                NULL, NULL, NULL))
1371     vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
1372                        "Failed to allocate unscaled raw source frame buffer");
1373 
1374   if (vpx_realloc_frame_buffer(&cpi->raw_scaled_source, cm->width, cm->height,
1375                                cm->subsampling_x, cm->subsampling_y,
1376 #if CONFIG_VP9_HIGHBITDEPTH
1377                                cm->use_highbitdepth,
1378 #endif
1379                                VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment,
1380                                NULL, NULL, NULL))
1381     vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
1382                        "Failed to allocate scaled raw source frame buffer");
1383 #endif
1384 }
1385 
alloc_context_buffers_ext(VP9_COMP * cpi)1386 static int alloc_context_buffers_ext(VP9_COMP *cpi) {
1387   VP9_COMMON *cm = &cpi->common;
1388   int mi_size = cm->mi_cols * cm->mi_rows;
1389 
1390   cpi->mbmi_ext_base = vpx_calloc(mi_size, sizeof(*cpi->mbmi_ext_base));
1391   if (!cpi->mbmi_ext_base) return 1;
1392 
1393   return 0;
1394 }
1395 
alloc_compressor_data(VP9_COMP * cpi)1396 static void alloc_compressor_data(VP9_COMP *cpi) {
1397   VP9_COMMON *cm = &cpi->common;
1398   int sb_rows;
1399 
1400   vp9_alloc_context_buffers(cm, cm->width, cm->height);
1401 
1402   alloc_context_buffers_ext(cpi);
1403 
1404   vpx_free(cpi->tile_tok[0][0]);
1405 
1406   {
1407     unsigned int tokens = get_token_alloc(cm->mb_rows, cm->mb_cols);
1408     CHECK_MEM_ERROR(cm, cpi->tile_tok[0][0],
1409                     vpx_calloc(tokens, sizeof(*cpi->tile_tok[0][0])));
1410   }
1411 
1412   sb_rows = mi_cols_aligned_to_sb(cm->mi_rows) >> MI_BLOCK_SIZE_LOG2;
1413   vpx_free(cpi->tplist[0][0]);
1414   CHECK_MEM_ERROR(
1415       cm, cpi->tplist[0][0],
1416       vpx_calloc(sb_rows * 4 * (1 << 6), sizeof(*cpi->tplist[0][0])));
1417 
1418   vp9_setup_pc_tree(&cpi->common, &cpi->td);
1419 }
1420 
vp9_new_framerate(VP9_COMP * cpi,double framerate)1421 void vp9_new_framerate(VP9_COMP *cpi, double framerate) {
1422   cpi->framerate = framerate < 0.1 ? 30 : framerate;
1423   vp9_rc_update_framerate(cpi);
1424 }
1425 
set_tile_limits(VP9_COMP * cpi)1426 static void set_tile_limits(VP9_COMP *cpi) {
1427   VP9_COMMON *const cm = &cpi->common;
1428 
1429   int min_log2_tile_cols, max_log2_tile_cols;
1430   vp9_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
1431 
1432   cm->log2_tile_cols =
1433       clamp(cpi->oxcf.tile_columns, min_log2_tile_cols, max_log2_tile_cols);
1434   cm->log2_tile_rows = cpi->oxcf.tile_rows;
1435 
1436   if (cpi->oxcf.target_level == LEVEL_AUTO) {
1437     const int level_tile_cols =
1438         log_tile_cols_from_picsize_level(cpi->common.width, cpi->common.height);
1439     if (cm->log2_tile_cols > level_tile_cols) {
1440       cm->log2_tile_cols = VPXMAX(level_tile_cols, min_log2_tile_cols);
1441     }
1442   }
1443 }
1444 
update_frame_size(VP9_COMP * cpi)1445 static void update_frame_size(VP9_COMP *cpi) {
1446   VP9_COMMON *const cm = &cpi->common;
1447   MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
1448 
1449   vp9_set_mb_mi(cm, cm->width, cm->height);
1450   vp9_init_context_buffers(cm);
1451   vp9_init_macroblockd(cm, xd, NULL);
1452   cpi->td.mb.mbmi_ext_base = cpi->mbmi_ext_base;
1453   memset(cpi->mbmi_ext_base, 0,
1454          cm->mi_rows * cm->mi_cols * sizeof(*cpi->mbmi_ext_base));
1455 
1456   set_tile_limits(cpi);
1457 }
1458 
init_buffer_indices(VP9_COMP * cpi)1459 static void init_buffer_indices(VP9_COMP *cpi) {
1460   int ref_frame;
1461 
1462   for (ref_frame = 0; ref_frame < REF_FRAMES; ++ref_frame)
1463     cpi->ref_fb_idx[ref_frame] = ref_frame;
1464 
1465   cpi->lst_fb_idx = cpi->ref_fb_idx[LAST_FRAME - 1];
1466   cpi->gld_fb_idx = cpi->ref_fb_idx[GOLDEN_FRAME - 1];
1467   cpi->alt_fb_idx = cpi->ref_fb_idx[ALTREF_FRAME - 1];
1468 }
1469 
init_level_constraint(LevelConstraint * lc)1470 static void init_level_constraint(LevelConstraint *lc) {
1471   lc->level_index = -1;
1472   lc->max_cpb_size = INT_MAX;
1473   lc->max_frame_size = INT_MAX;
1474   lc->fail_flag = 0;
1475 }
1476 
set_level_constraint(LevelConstraint * ls,int8_t level_index)1477 static void set_level_constraint(LevelConstraint *ls, int8_t level_index) {
1478   vpx_clear_system_state();
1479   ls->level_index = level_index;
1480   if (level_index >= 0) {
1481     ls->max_cpb_size = vp9_level_defs[level_index].max_cpb_size * (double)1000;
1482   }
1483 }
1484 
init_config(struct VP9_COMP * cpi,const VP9EncoderConfig * oxcf)1485 static void init_config(struct VP9_COMP *cpi, const VP9EncoderConfig *oxcf) {
1486   VP9_COMMON *const cm = &cpi->common;
1487 
1488   cpi->oxcf = *oxcf;
1489   cpi->framerate = oxcf->init_framerate;
1490   cm->profile = oxcf->profile;
1491   cm->bit_depth = oxcf->bit_depth;
1492 #if CONFIG_VP9_HIGHBITDEPTH
1493   cm->use_highbitdepth = oxcf->use_highbitdepth;
1494 #endif
1495   cm->color_space = oxcf->color_space;
1496   cm->color_range = oxcf->color_range;
1497 
1498   cpi->target_level = oxcf->target_level;
1499   cpi->keep_level_stats = oxcf->target_level != LEVEL_MAX;
1500   set_level_constraint(&cpi->level_constraint,
1501                        get_level_index(cpi->target_level));
1502 
1503   cm->width = oxcf->width;
1504   cm->height = oxcf->height;
1505   alloc_compressor_data(cpi);
1506 
1507   cpi->svc.temporal_layering_mode = oxcf->temporal_layering_mode;
1508 
1509   // Single thread case: use counts in common.
1510   cpi->td.counts = &cm->counts;
1511 
1512   // Spatial scalability.
1513   cpi->svc.number_spatial_layers = oxcf->ss_number_layers;
1514   // Temporal scalability.
1515   cpi->svc.number_temporal_layers = oxcf->ts_number_layers;
1516 
1517   if ((cpi->svc.number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) ||
1518       ((cpi->svc.number_temporal_layers > 1 ||
1519         cpi->svc.number_spatial_layers > 1) &&
1520        cpi->oxcf.pass != 1)) {
1521     vp9_init_layer_context(cpi);
1522   }
1523 
1524   // change includes all joint functionality
1525   vp9_change_config(cpi, oxcf);
1526 
1527   cpi->static_mb_pct = 0;
1528   cpi->ref_frame_flags = 0;
1529 
1530   init_buffer_indices(cpi);
1531 
1532   vp9_noise_estimate_init(&cpi->noise_estimate, cm->width, cm->height);
1533 }
1534 
vp9_check_reset_rc_flag(VP9_COMP * cpi)1535 void vp9_check_reset_rc_flag(VP9_COMP *cpi) {
1536   RATE_CONTROL *rc = &cpi->rc;
1537 
1538   if (cpi->common.current_video_frame >
1539       (unsigned int)cpi->svc.number_spatial_layers) {
1540     if (cpi->use_svc) {
1541       vp9_svc_check_reset_layer_rc_flag(cpi);
1542     } else {
1543       if (rc->avg_frame_bandwidth > (3 * rc->last_avg_frame_bandwidth >> 1) ||
1544           rc->avg_frame_bandwidth < (rc->last_avg_frame_bandwidth >> 1)) {
1545         rc->rc_1_frame = 0;
1546         rc->rc_2_frame = 0;
1547         rc->bits_off_target = rc->optimal_buffer_level;
1548         rc->buffer_level = rc->optimal_buffer_level;
1549       }
1550     }
1551   }
1552 }
1553 
vp9_set_rc_buffer_sizes(VP9_COMP * cpi)1554 void vp9_set_rc_buffer_sizes(VP9_COMP *cpi) {
1555   RATE_CONTROL *rc = &cpi->rc;
1556   const VP9EncoderConfig *oxcf = &cpi->oxcf;
1557 
1558   const int64_t bandwidth = oxcf->target_bandwidth;
1559   const int64_t starting = oxcf->starting_buffer_level_ms;
1560   const int64_t optimal = oxcf->optimal_buffer_level_ms;
1561   const int64_t maximum = oxcf->maximum_buffer_size_ms;
1562 
1563   rc->starting_buffer_level = starting * bandwidth / 1000;
1564   rc->optimal_buffer_level =
1565       (optimal == 0) ? bandwidth / 8 : optimal * bandwidth / 1000;
1566   rc->maximum_buffer_size =
1567       (maximum == 0) ? bandwidth / 8 : maximum * bandwidth / 1000;
1568 
1569   // Under a configuration change, where maximum_buffer_size may change,
1570   // keep buffer level clipped to the maximum allowed buffer size.
1571   rc->bits_off_target = VPXMIN(rc->bits_off_target, rc->maximum_buffer_size);
1572   rc->buffer_level = VPXMIN(rc->buffer_level, rc->maximum_buffer_size);
1573 }
1574 
1575 #if CONFIG_VP9_HIGHBITDEPTH
1576 // TODO(angiebird): make sdx8f available for highbitdepth if needed
1577 #define HIGHBD_BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX4DF) \
1578   cpi->fn_ptr[BT].sdf = SDF;                             \
1579   cpi->fn_ptr[BT].sdaf = SDAF;                           \
1580   cpi->fn_ptr[BT].vf = VF;                               \
1581   cpi->fn_ptr[BT].svf = SVF;                             \
1582   cpi->fn_ptr[BT].svaf = SVAF;                           \
1583   cpi->fn_ptr[BT].sdx4df = SDX4DF;                       \
1584   cpi->fn_ptr[BT].sdx8f = NULL;
1585 
1586 #define MAKE_BFP_SAD_WRAPPER(fnname)                                           \
1587   static unsigned int fnname##_bits8(const uint8_t *src_ptr,                   \
1588                                      int source_stride,                        \
1589                                      const uint8_t *ref_ptr, int ref_stride) { \
1590     return fnname(src_ptr, source_stride, ref_ptr, ref_stride);                \
1591   }                                                                            \
1592   static unsigned int fnname##_bits10(                                         \
1593       const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr,       \
1594       int ref_stride) {                                                        \
1595     return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 2;           \
1596   }                                                                            \
1597   static unsigned int fnname##_bits12(                                         \
1598       const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr,       \
1599       int ref_stride) {                                                        \
1600     return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 4;           \
1601   }
1602 
1603 #define MAKE_BFP_SADAVG_WRAPPER(fnname)                                        \
1604   static unsigned int fnname##_bits8(                                          \
1605       const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr,       \
1606       int ref_stride, const uint8_t *second_pred) {                            \
1607     return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred);   \
1608   }                                                                            \
1609   static unsigned int fnname##_bits10(                                         \
1610       const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr,       \
1611       int ref_stride, const uint8_t *second_pred) {                            \
1612     return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
1613            2;                                                                  \
1614   }                                                                            \
1615   static unsigned int fnname##_bits12(                                         \
1616       const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr,       \
1617       int ref_stride, const uint8_t *second_pred) {                            \
1618     return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
1619            4;                                                                  \
1620   }
1621 
1622 #define MAKE_BFP_SAD4D_WRAPPER(fnname)                                        \
1623   static void fnname##_bits8(const uint8_t *src_ptr, int source_stride,       \
1624                              const uint8_t *const ref_ptr[], int ref_stride,  \
1625                              unsigned int *sad_array) {                       \
1626     fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array);           \
1627   }                                                                           \
1628   static void fnname##_bits10(const uint8_t *src_ptr, int source_stride,      \
1629                               const uint8_t *const ref_ptr[], int ref_stride, \
1630                               unsigned int *sad_array) {                      \
1631     int i;                                                                    \
1632     fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array);           \
1633     for (i = 0; i < 4; i++) sad_array[i] >>= 2;                               \
1634   }                                                                           \
1635   static void fnname##_bits12(const uint8_t *src_ptr, int source_stride,      \
1636                               const uint8_t *const ref_ptr[], int ref_stride, \
1637                               unsigned int *sad_array) {                      \
1638     int i;                                                                    \
1639     fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array);           \
1640     for (i = 0; i < 4; i++) sad_array[i] >>= 4;                               \
1641   }
1642 
1643 MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad32x16)
MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad32x16_avg)1644 MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad32x16_avg)
1645 MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad32x16x4d)
1646 MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad16x32)
1647 MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad16x32_avg)
1648 MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad16x32x4d)
1649 MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad64x32)
1650 MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad64x32_avg)
1651 MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad64x32x4d)
1652 MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad32x64)
1653 MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad32x64_avg)
1654 MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad32x64x4d)
1655 MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad32x32)
1656 MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad32x32_avg)
1657 MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad32x32x4d)
1658 MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad64x64)
1659 MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad64x64_avg)
1660 MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad64x64x4d)
1661 MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad16x16)
1662 MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad16x16_avg)
1663 MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad16x16x4d)
1664 MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad16x8)
1665 MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad16x8_avg)
1666 MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad16x8x4d)
1667 MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad8x16)
1668 MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad8x16_avg)
1669 MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad8x16x4d)
1670 MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad8x8)
1671 MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad8x8_avg)
1672 MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad8x8x4d)
1673 MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad8x4)
1674 MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad8x4_avg)
1675 MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad8x4x4d)
1676 MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad4x8)
1677 MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad4x8_avg)
1678 MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad4x8x4d)
1679 MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad4x4)
1680 MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad4x4_avg)
1681 MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad4x4x4d)
1682 
1683 static void highbd_set_var_fns(VP9_COMP *const cpi) {
1684   VP9_COMMON *const cm = &cpi->common;
1685   if (cm->use_highbitdepth) {
1686     switch (cm->bit_depth) {
1687       case VPX_BITS_8:
1688         HIGHBD_BFP(BLOCK_32X16, vpx_highbd_sad32x16_bits8,
1689                    vpx_highbd_sad32x16_avg_bits8, vpx_highbd_8_variance32x16,
1690                    vpx_highbd_8_sub_pixel_variance32x16,
1691                    vpx_highbd_8_sub_pixel_avg_variance32x16,
1692                    vpx_highbd_sad32x16x4d_bits8)
1693 
1694         HIGHBD_BFP(BLOCK_16X32, vpx_highbd_sad16x32_bits8,
1695                    vpx_highbd_sad16x32_avg_bits8, vpx_highbd_8_variance16x32,
1696                    vpx_highbd_8_sub_pixel_variance16x32,
1697                    vpx_highbd_8_sub_pixel_avg_variance16x32,
1698                    vpx_highbd_sad16x32x4d_bits8)
1699 
1700         HIGHBD_BFP(BLOCK_64X32, vpx_highbd_sad64x32_bits8,
1701                    vpx_highbd_sad64x32_avg_bits8, vpx_highbd_8_variance64x32,
1702                    vpx_highbd_8_sub_pixel_variance64x32,
1703                    vpx_highbd_8_sub_pixel_avg_variance64x32,
1704                    vpx_highbd_sad64x32x4d_bits8)
1705 
1706         HIGHBD_BFP(BLOCK_32X64, vpx_highbd_sad32x64_bits8,
1707                    vpx_highbd_sad32x64_avg_bits8, vpx_highbd_8_variance32x64,
1708                    vpx_highbd_8_sub_pixel_variance32x64,
1709                    vpx_highbd_8_sub_pixel_avg_variance32x64,
1710                    vpx_highbd_sad32x64x4d_bits8)
1711 
1712         HIGHBD_BFP(BLOCK_32X32, vpx_highbd_sad32x32_bits8,
1713                    vpx_highbd_sad32x32_avg_bits8, vpx_highbd_8_variance32x32,
1714                    vpx_highbd_8_sub_pixel_variance32x32,
1715                    vpx_highbd_8_sub_pixel_avg_variance32x32,
1716                    vpx_highbd_sad32x32x4d_bits8)
1717 
1718         HIGHBD_BFP(BLOCK_64X64, vpx_highbd_sad64x64_bits8,
1719                    vpx_highbd_sad64x64_avg_bits8, vpx_highbd_8_variance64x64,
1720                    vpx_highbd_8_sub_pixel_variance64x64,
1721                    vpx_highbd_8_sub_pixel_avg_variance64x64,
1722                    vpx_highbd_sad64x64x4d_bits8)
1723 
1724         HIGHBD_BFP(BLOCK_16X16, vpx_highbd_sad16x16_bits8,
1725                    vpx_highbd_sad16x16_avg_bits8, vpx_highbd_8_variance16x16,
1726                    vpx_highbd_8_sub_pixel_variance16x16,
1727                    vpx_highbd_8_sub_pixel_avg_variance16x16,
1728                    vpx_highbd_sad16x16x4d_bits8)
1729 
1730         HIGHBD_BFP(BLOCK_16X8, vpx_highbd_sad16x8_bits8,
1731                    vpx_highbd_sad16x8_avg_bits8, vpx_highbd_8_variance16x8,
1732                    vpx_highbd_8_sub_pixel_variance16x8,
1733                    vpx_highbd_8_sub_pixel_avg_variance16x8,
1734                    vpx_highbd_sad16x8x4d_bits8)
1735 
1736         HIGHBD_BFP(BLOCK_8X16, vpx_highbd_sad8x16_bits8,
1737                    vpx_highbd_sad8x16_avg_bits8, vpx_highbd_8_variance8x16,
1738                    vpx_highbd_8_sub_pixel_variance8x16,
1739                    vpx_highbd_8_sub_pixel_avg_variance8x16,
1740                    vpx_highbd_sad8x16x4d_bits8)
1741 
1742         HIGHBD_BFP(
1743             BLOCK_8X8, vpx_highbd_sad8x8_bits8, vpx_highbd_sad8x8_avg_bits8,
1744             vpx_highbd_8_variance8x8, vpx_highbd_8_sub_pixel_variance8x8,
1745             vpx_highbd_8_sub_pixel_avg_variance8x8, vpx_highbd_sad8x8x4d_bits8)
1746 
1747         HIGHBD_BFP(
1748             BLOCK_8X4, vpx_highbd_sad8x4_bits8, vpx_highbd_sad8x4_avg_bits8,
1749             vpx_highbd_8_variance8x4, vpx_highbd_8_sub_pixel_variance8x4,
1750             vpx_highbd_8_sub_pixel_avg_variance8x4, vpx_highbd_sad8x4x4d_bits8)
1751 
1752         HIGHBD_BFP(
1753             BLOCK_4X8, vpx_highbd_sad4x8_bits8, vpx_highbd_sad4x8_avg_bits8,
1754             vpx_highbd_8_variance4x8, vpx_highbd_8_sub_pixel_variance4x8,
1755             vpx_highbd_8_sub_pixel_avg_variance4x8, vpx_highbd_sad4x8x4d_bits8)
1756 
1757         HIGHBD_BFP(
1758             BLOCK_4X4, vpx_highbd_sad4x4_bits8, vpx_highbd_sad4x4_avg_bits8,
1759             vpx_highbd_8_variance4x4, vpx_highbd_8_sub_pixel_variance4x4,
1760             vpx_highbd_8_sub_pixel_avg_variance4x4, vpx_highbd_sad4x4x4d_bits8)
1761         break;
1762 
1763       case VPX_BITS_10:
1764         HIGHBD_BFP(BLOCK_32X16, vpx_highbd_sad32x16_bits10,
1765                    vpx_highbd_sad32x16_avg_bits10, vpx_highbd_10_variance32x16,
1766                    vpx_highbd_10_sub_pixel_variance32x16,
1767                    vpx_highbd_10_sub_pixel_avg_variance32x16,
1768                    vpx_highbd_sad32x16x4d_bits10)
1769 
1770         HIGHBD_BFP(BLOCK_16X32, vpx_highbd_sad16x32_bits10,
1771                    vpx_highbd_sad16x32_avg_bits10, vpx_highbd_10_variance16x32,
1772                    vpx_highbd_10_sub_pixel_variance16x32,
1773                    vpx_highbd_10_sub_pixel_avg_variance16x32,
1774                    vpx_highbd_sad16x32x4d_bits10)
1775 
1776         HIGHBD_BFP(BLOCK_64X32, vpx_highbd_sad64x32_bits10,
1777                    vpx_highbd_sad64x32_avg_bits10, vpx_highbd_10_variance64x32,
1778                    vpx_highbd_10_sub_pixel_variance64x32,
1779                    vpx_highbd_10_sub_pixel_avg_variance64x32,
1780                    vpx_highbd_sad64x32x4d_bits10)
1781 
1782         HIGHBD_BFP(BLOCK_32X64, vpx_highbd_sad32x64_bits10,
1783                    vpx_highbd_sad32x64_avg_bits10, vpx_highbd_10_variance32x64,
1784                    vpx_highbd_10_sub_pixel_variance32x64,
1785                    vpx_highbd_10_sub_pixel_avg_variance32x64,
1786                    vpx_highbd_sad32x64x4d_bits10)
1787 
1788         HIGHBD_BFP(BLOCK_32X32, vpx_highbd_sad32x32_bits10,
1789                    vpx_highbd_sad32x32_avg_bits10, vpx_highbd_10_variance32x32,
1790                    vpx_highbd_10_sub_pixel_variance32x32,
1791                    vpx_highbd_10_sub_pixel_avg_variance32x32,
1792                    vpx_highbd_sad32x32x4d_bits10)
1793 
1794         HIGHBD_BFP(BLOCK_64X64, vpx_highbd_sad64x64_bits10,
1795                    vpx_highbd_sad64x64_avg_bits10, vpx_highbd_10_variance64x64,
1796                    vpx_highbd_10_sub_pixel_variance64x64,
1797                    vpx_highbd_10_sub_pixel_avg_variance64x64,
1798                    vpx_highbd_sad64x64x4d_bits10)
1799 
1800         HIGHBD_BFP(BLOCK_16X16, vpx_highbd_sad16x16_bits10,
1801                    vpx_highbd_sad16x16_avg_bits10, vpx_highbd_10_variance16x16,
1802                    vpx_highbd_10_sub_pixel_variance16x16,
1803                    vpx_highbd_10_sub_pixel_avg_variance16x16,
1804                    vpx_highbd_sad16x16x4d_bits10)
1805 
1806         HIGHBD_BFP(BLOCK_16X8, vpx_highbd_sad16x8_bits10,
1807                    vpx_highbd_sad16x8_avg_bits10, vpx_highbd_10_variance16x8,
1808                    vpx_highbd_10_sub_pixel_variance16x8,
1809                    vpx_highbd_10_sub_pixel_avg_variance16x8,
1810                    vpx_highbd_sad16x8x4d_bits10)
1811 
1812         HIGHBD_BFP(BLOCK_8X16, vpx_highbd_sad8x16_bits10,
1813                    vpx_highbd_sad8x16_avg_bits10, vpx_highbd_10_variance8x16,
1814                    vpx_highbd_10_sub_pixel_variance8x16,
1815                    vpx_highbd_10_sub_pixel_avg_variance8x16,
1816                    vpx_highbd_sad8x16x4d_bits10)
1817 
1818         HIGHBD_BFP(BLOCK_8X8, vpx_highbd_sad8x8_bits10,
1819                    vpx_highbd_sad8x8_avg_bits10, vpx_highbd_10_variance8x8,
1820                    vpx_highbd_10_sub_pixel_variance8x8,
1821                    vpx_highbd_10_sub_pixel_avg_variance8x8,
1822                    vpx_highbd_sad8x8x4d_bits10)
1823 
1824         HIGHBD_BFP(BLOCK_8X4, vpx_highbd_sad8x4_bits10,
1825                    vpx_highbd_sad8x4_avg_bits10, vpx_highbd_10_variance8x4,
1826                    vpx_highbd_10_sub_pixel_variance8x4,
1827                    vpx_highbd_10_sub_pixel_avg_variance8x4,
1828                    vpx_highbd_sad8x4x4d_bits10)
1829 
1830         HIGHBD_BFP(BLOCK_4X8, vpx_highbd_sad4x8_bits10,
1831                    vpx_highbd_sad4x8_avg_bits10, vpx_highbd_10_variance4x8,
1832                    vpx_highbd_10_sub_pixel_variance4x8,
1833                    vpx_highbd_10_sub_pixel_avg_variance4x8,
1834                    vpx_highbd_sad4x8x4d_bits10)
1835 
1836         HIGHBD_BFP(BLOCK_4X4, vpx_highbd_sad4x4_bits10,
1837                    vpx_highbd_sad4x4_avg_bits10, vpx_highbd_10_variance4x4,
1838                    vpx_highbd_10_sub_pixel_variance4x4,
1839                    vpx_highbd_10_sub_pixel_avg_variance4x4,
1840                    vpx_highbd_sad4x4x4d_bits10)
1841         break;
1842 
1843       default:
1844         assert(cm->bit_depth == VPX_BITS_12);
1845         HIGHBD_BFP(BLOCK_32X16, vpx_highbd_sad32x16_bits12,
1846                    vpx_highbd_sad32x16_avg_bits12, vpx_highbd_12_variance32x16,
1847                    vpx_highbd_12_sub_pixel_variance32x16,
1848                    vpx_highbd_12_sub_pixel_avg_variance32x16,
1849                    vpx_highbd_sad32x16x4d_bits12)
1850 
1851         HIGHBD_BFP(BLOCK_16X32, vpx_highbd_sad16x32_bits12,
1852                    vpx_highbd_sad16x32_avg_bits12, vpx_highbd_12_variance16x32,
1853                    vpx_highbd_12_sub_pixel_variance16x32,
1854                    vpx_highbd_12_sub_pixel_avg_variance16x32,
1855                    vpx_highbd_sad16x32x4d_bits12)
1856 
1857         HIGHBD_BFP(BLOCK_64X32, vpx_highbd_sad64x32_bits12,
1858                    vpx_highbd_sad64x32_avg_bits12, vpx_highbd_12_variance64x32,
1859                    vpx_highbd_12_sub_pixel_variance64x32,
1860                    vpx_highbd_12_sub_pixel_avg_variance64x32,
1861                    vpx_highbd_sad64x32x4d_bits12)
1862 
1863         HIGHBD_BFP(BLOCK_32X64, vpx_highbd_sad32x64_bits12,
1864                    vpx_highbd_sad32x64_avg_bits12, vpx_highbd_12_variance32x64,
1865                    vpx_highbd_12_sub_pixel_variance32x64,
1866                    vpx_highbd_12_sub_pixel_avg_variance32x64,
1867                    vpx_highbd_sad32x64x4d_bits12)
1868 
1869         HIGHBD_BFP(BLOCK_32X32, vpx_highbd_sad32x32_bits12,
1870                    vpx_highbd_sad32x32_avg_bits12, vpx_highbd_12_variance32x32,
1871                    vpx_highbd_12_sub_pixel_variance32x32,
1872                    vpx_highbd_12_sub_pixel_avg_variance32x32,
1873                    vpx_highbd_sad32x32x4d_bits12)
1874 
1875         HIGHBD_BFP(BLOCK_64X64, vpx_highbd_sad64x64_bits12,
1876                    vpx_highbd_sad64x64_avg_bits12, vpx_highbd_12_variance64x64,
1877                    vpx_highbd_12_sub_pixel_variance64x64,
1878                    vpx_highbd_12_sub_pixel_avg_variance64x64,
1879                    vpx_highbd_sad64x64x4d_bits12)
1880 
1881         HIGHBD_BFP(BLOCK_16X16, vpx_highbd_sad16x16_bits12,
1882                    vpx_highbd_sad16x16_avg_bits12, vpx_highbd_12_variance16x16,
1883                    vpx_highbd_12_sub_pixel_variance16x16,
1884                    vpx_highbd_12_sub_pixel_avg_variance16x16,
1885                    vpx_highbd_sad16x16x4d_bits12)
1886 
1887         HIGHBD_BFP(BLOCK_16X8, vpx_highbd_sad16x8_bits12,
1888                    vpx_highbd_sad16x8_avg_bits12, vpx_highbd_12_variance16x8,
1889                    vpx_highbd_12_sub_pixel_variance16x8,
1890                    vpx_highbd_12_sub_pixel_avg_variance16x8,
1891                    vpx_highbd_sad16x8x4d_bits12)
1892 
1893         HIGHBD_BFP(BLOCK_8X16, vpx_highbd_sad8x16_bits12,
1894                    vpx_highbd_sad8x16_avg_bits12, vpx_highbd_12_variance8x16,
1895                    vpx_highbd_12_sub_pixel_variance8x16,
1896                    vpx_highbd_12_sub_pixel_avg_variance8x16,
1897                    vpx_highbd_sad8x16x4d_bits12)
1898 
1899         HIGHBD_BFP(BLOCK_8X8, vpx_highbd_sad8x8_bits12,
1900                    vpx_highbd_sad8x8_avg_bits12, vpx_highbd_12_variance8x8,
1901                    vpx_highbd_12_sub_pixel_variance8x8,
1902                    vpx_highbd_12_sub_pixel_avg_variance8x8,
1903                    vpx_highbd_sad8x8x4d_bits12)
1904 
1905         HIGHBD_BFP(BLOCK_8X4, vpx_highbd_sad8x4_bits12,
1906                    vpx_highbd_sad8x4_avg_bits12, vpx_highbd_12_variance8x4,
1907                    vpx_highbd_12_sub_pixel_variance8x4,
1908                    vpx_highbd_12_sub_pixel_avg_variance8x4,
1909                    vpx_highbd_sad8x4x4d_bits12)
1910 
1911         HIGHBD_BFP(BLOCK_4X8, vpx_highbd_sad4x8_bits12,
1912                    vpx_highbd_sad4x8_avg_bits12, vpx_highbd_12_variance4x8,
1913                    vpx_highbd_12_sub_pixel_variance4x8,
1914                    vpx_highbd_12_sub_pixel_avg_variance4x8,
1915                    vpx_highbd_sad4x8x4d_bits12)
1916 
1917         HIGHBD_BFP(BLOCK_4X4, vpx_highbd_sad4x4_bits12,
1918                    vpx_highbd_sad4x4_avg_bits12, vpx_highbd_12_variance4x4,
1919                    vpx_highbd_12_sub_pixel_variance4x4,
1920                    vpx_highbd_12_sub_pixel_avg_variance4x4,
1921                    vpx_highbd_sad4x4x4d_bits12)
1922         break;
1923     }
1924   }
1925 }
1926 #endif  // CONFIG_VP9_HIGHBITDEPTH
1927 
realloc_segmentation_maps(VP9_COMP * cpi)1928 static void realloc_segmentation_maps(VP9_COMP *cpi) {
1929   VP9_COMMON *const cm = &cpi->common;
1930 
1931   // Create the encoder segmentation map and set all entries to 0
1932   vpx_free(cpi->segmentation_map);
1933   CHECK_MEM_ERROR(cm, cpi->segmentation_map,
1934                   vpx_calloc(cm->mi_rows * cm->mi_cols, 1));
1935 
1936   // Create a map used for cyclic background refresh.
1937   if (cpi->cyclic_refresh) vp9_cyclic_refresh_free(cpi->cyclic_refresh);
1938   CHECK_MEM_ERROR(cm, cpi->cyclic_refresh,
1939                   vp9_cyclic_refresh_alloc(cm->mi_rows, cm->mi_cols));
1940 
1941   // Create a map used to mark inactive areas.
1942   vpx_free(cpi->active_map.map);
1943   CHECK_MEM_ERROR(cm, cpi->active_map.map,
1944                   vpx_calloc(cm->mi_rows * cm->mi_cols, 1));
1945 
1946   // And a place holder structure is the coding context
1947   // for use if we want to save and restore it
1948   vpx_free(cpi->coding_context.last_frame_seg_map_copy);
1949   CHECK_MEM_ERROR(cm, cpi->coding_context.last_frame_seg_map_copy,
1950                   vpx_calloc(cm->mi_rows * cm->mi_cols, 1));
1951 }
1952 
alloc_copy_partition_data(VP9_COMP * cpi)1953 static void alloc_copy_partition_data(VP9_COMP *cpi) {
1954   VP9_COMMON *const cm = &cpi->common;
1955   if (cpi->prev_partition == NULL) {
1956     CHECK_MEM_ERROR(cm, cpi->prev_partition,
1957                     (BLOCK_SIZE *)vpx_calloc(cm->mi_stride * cm->mi_rows,
1958                                              sizeof(*cpi->prev_partition)));
1959   }
1960   if (cpi->prev_segment_id == NULL) {
1961     CHECK_MEM_ERROR(
1962         cm, cpi->prev_segment_id,
1963         (int8_t *)vpx_calloc((cm->mi_stride >> 3) * ((cm->mi_rows >> 3) + 1),
1964                              sizeof(*cpi->prev_segment_id)));
1965   }
1966   if (cpi->prev_variance_low == NULL) {
1967     CHECK_MEM_ERROR(cm, cpi->prev_variance_low,
1968                     (uint8_t *)vpx_calloc(
1969                         (cm->mi_stride >> 3) * ((cm->mi_rows >> 3) + 1) * 25,
1970                         sizeof(*cpi->prev_variance_low)));
1971   }
1972   if (cpi->copied_frame_cnt == NULL) {
1973     CHECK_MEM_ERROR(
1974         cm, cpi->copied_frame_cnt,
1975         (uint8_t *)vpx_calloc((cm->mi_stride >> 3) * ((cm->mi_rows >> 3) + 1),
1976                               sizeof(*cpi->copied_frame_cnt)));
1977   }
1978 }
1979 
vp9_change_config(struct VP9_COMP * cpi,const VP9EncoderConfig * oxcf)1980 void vp9_change_config(struct VP9_COMP *cpi, const VP9EncoderConfig *oxcf) {
1981   VP9_COMMON *const cm = &cpi->common;
1982   RATE_CONTROL *const rc = &cpi->rc;
1983   int last_w = cpi->oxcf.width;
1984   int last_h = cpi->oxcf.height;
1985 
1986   vp9_init_quantizer(cpi);
1987   if (cm->profile != oxcf->profile) cm->profile = oxcf->profile;
1988   cm->bit_depth = oxcf->bit_depth;
1989   cm->color_space = oxcf->color_space;
1990   cm->color_range = oxcf->color_range;
1991 
1992   cpi->target_level = oxcf->target_level;
1993   cpi->keep_level_stats = oxcf->target_level != LEVEL_MAX;
1994   set_level_constraint(&cpi->level_constraint,
1995                        get_level_index(cpi->target_level));
1996 
1997   if (cm->profile <= PROFILE_1)
1998     assert(cm->bit_depth == VPX_BITS_8);
1999   else
2000     assert(cm->bit_depth > VPX_BITS_8);
2001 
2002   cpi->oxcf = *oxcf;
2003 #if CONFIG_VP9_HIGHBITDEPTH
2004   cpi->td.mb.e_mbd.bd = (int)cm->bit_depth;
2005 #endif  // CONFIG_VP9_HIGHBITDEPTH
2006 
2007   if ((oxcf->pass == 0) && (oxcf->rc_mode == VPX_Q)) {
2008     rc->baseline_gf_interval = FIXED_GF_INTERVAL;
2009   } else {
2010     rc->baseline_gf_interval = (MIN_GF_INTERVAL + MAX_GF_INTERVAL) / 2;
2011   }
2012 
2013   cpi->refresh_golden_frame = 0;
2014   cpi->refresh_last_frame = 1;
2015   cm->refresh_frame_context = 1;
2016   cm->reset_frame_context = 0;
2017 
2018   vp9_reset_segment_features(&cm->seg);
2019   vp9_set_high_precision_mv(cpi, 0);
2020 
2021   {
2022     int i;
2023 
2024     for (i = 0; i < MAX_SEGMENTS; i++)
2025       cpi->segment_encode_breakout[i] = cpi->oxcf.encode_breakout;
2026   }
2027   cpi->encode_breakout = cpi->oxcf.encode_breakout;
2028 
2029   vp9_set_rc_buffer_sizes(cpi);
2030 
2031   // Set up frame rate and related parameters rate control values.
2032   vp9_new_framerate(cpi, cpi->framerate);
2033 
2034   // Set absolute upper and lower quality limits
2035   rc->worst_quality = cpi->oxcf.worst_allowed_q;
2036   rc->best_quality = cpi->oxcf.best_allowed_q;
2037 
2038   cm->interp_filter = cpi->sf.default_interp_filter;
2039 
2040   if (cpi->oxcf.render_width > 0 && cpi->oxcf.render_height > 0) {
2041     cm->render_width = cpi->oxcf.render_width;
2042     cm->render_height = cpi->oxcf.render_height;
2043   } else {
2044     cm->render_width = cpi->oxcf.width;
2045     cm->render_height = cpi->oxcf.height;
2046   }
2047   if (last_w != cpi->oxcf.width || last_h != cpi->oxcf.height) {
2048     cm->width = cpi->oxcf.width;
2049     cm->height = cpi->oxcf.height;
2050     cpi->external_resize = 1;
2051   }
2052 
2053   if (cpi->initial_width) {
2054     int new_mi_size = 0;
2055     vp9_set_mb_mi(cm, cm->width, cm->height);
2056     new_mi_size = cm->mi_stride * calc_mi_size(cm->mi_rows);
2057     if (cm->mi_alloc_size < new_mi_size) {
2058       vp9_free_context_buffers(cm);
2059       alloc_compressor_data(cpi);
2060       realloc_segmentation_maps(cpi);
2061       cpi->initial_width = cpi->initial_height = 0;
2062       cpi->external_resize = 0;
2063     } else if (cm->mi_alloc_size == new_mi_size &&
2064                (cpi->oxcf.width > last_w || cpi->oxcf.height > last_h)) {
2065       vp9_alloc_loop_filter(cm);
2066     }
2067   }
2068 
2069   if (cm->current_video_frame == 0 || last_w != cpi->oxcf.width ||
2070       last_h != cpi->oxcf.height)
2071     update_frame_size(cpi);
2072 
2073   if (last_w != cpi->oxcf.width || last_h != cpi->oxcf.height) {
2074     memset(cpi->consec_zero_mv, 0,
2075            cm->mi_rows * cm->mi_cols * sizeof(*cpi->consec_zero_mv));
2076     if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ)
2077       vp9_cyclic_refresh_reset_resize(cpi);
2078     rc->rc_1_frame = 0;
2079     rc->rc_2_frame = 0;
2080   }
2081 
2082   if ((cpi->svc.number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) ||
2083       ((cpi->svc.number_temporal_layers > 1 ||
2084         cpi->svc.number_spatial_layers > 1) &&
2085        cpi->oxcf.pass != 1)) {
2086     vp9_update_layer_context_change_config(cpi,
2087                                            (int)cpi->oxcf.target_bandwidth);
2088   }
2089 
2090   vp9_check_reset_rc_flag(cpi);
2091 
2092   cpi->alt_ref_source = NULL;
2093   rc->is_src_frame_alt_ref = 0;
2094 
2095 #if 0
2096   // Experimental RD Code
2097   cpi->frame_distortion = 0;
2098   cpi->last_frame_distortion = 0;
2099 #endif
2100 
2101   set_tile_limits(cpi);
2102 
2103   cpi->ext_refresh_frame_flags_pending = 0;
2104   cpi->ext_refresh_frame_context_pending = 0;
2105 
2106 #if CONFIG_VP9_HIGHBITDEPTH
2107   highbd_set_var_fns(cpi);
2108 #endif
2109 
2110   vp9_set_row_mt(cpi);
2111 }
2112 
2113 #ifndef M_LOG2_E
2114 #define M_LOG2_E 0.693147180559945309417
2115 #endif
2116 #define log2f(x) (log(x) / (float)M_LOG2_E)
2117 
2118 /***********************************************************************
2119  * Read before modifying 'cal_nmvjointsadcost' or 'cal_nmvsadcosts'    *
2120  ***********************************************************************
2121  * The following 2 functions ('cal_nmvjointsadcost' and                *
2122  * 'cal_nmvsadcosts') are used to calculate cost lookup tables         *
2123  * used by 'vp9_diamond_search_sad'. The C implementation of the       *
2124  * function is generic, but the AVX intrinsics optimised version       *
2125  * relies on the following properties of the computed tables:          *
2126  * For cal_nmvjointsadcost:                                            *
2127  *   - mvjointsadcost[1] == mvjointsadcost[2] == mvjointsadcost[3]     *
2128  * For cal_nmvsadcosts:                                                *
2129  *   - For all i: mvsadcost[0][i] == mvsadcost[1][i]                   *
2130  *         (Equal costs for both components)                           *
2131  *   - For all i: mvsadcost[0][i] == mvsadcost[0][-i]                  *
2132  *         (Cost function is even)                                     *
2133  * If these do not hold, then the AVX optimised version of the         *
2134  * 'vp9_diamond_search_sad' function cannot be used as it is, in which *
2135  * case you can revert to using the C function instead.                *
2136  ***********************************************************************/
2137 
cal_nmvjointsadcost(int * mvjointsadcost)2138 static void cal_nmvjointsadcost(int *mvjointsadcost) {
2139   /*********************************************************************
2140    * Warning: Read the comments above before modifying this function   *
2141    *********************************************************************/
2142   mvjointsadcost[0] = 600;
2143   mvjointsadcost[1] = 300;
2144   mvjointsadcost[2] = 300;
2145   mvjointsadcost[3] = 300;
2146 }
2147 
cal_nmvsadcosts(int * mvsadcost[2])2148 static void cal_nmvsadcosts(int *mvsadcost[2]) {
2149   /*********************************************************************
2150    * Warning: Read the comments above before modifying this function   *
2151    *********************************************************************/
2152   int i = 1;
2153 
2154   mvsadcost[0][0] = 0;
2155   mvsadcost[1][0] = 0;
2156 
2157   do {
2158     double z = 256 * (2 * (log2f(8 * i) + .6));
2159     mvsadcost[0][i] = (int)z;
2160     mvsadcost[1][i] = (int)z;
2161     mvsadcost[0][-i] = (int)z;
2162     mvsadcost[1][-i] = (int)z;
2163   } while (++i <= MV_MAX);
2164 }
2165 
cal_nmvsadcosts_hp(int * mvsadcost[2])2166 static void cal_nmvsadcosts_hp(int *mvsadcost[2]) {
2167   int i = 1;
2168 
2169   mvsadcost[0][0] = 0;
2170   mvsadcost[1][0] = 0;
2171 
2172   do {
2173     double z = 256 * (2 * (log2f(8 * i) + .6));
2174     mvsadcost[0][i] = (int)z;
2175     mvsadcost[1][i] = (int)z;
2176     mvsadcost[0][-i] = (int)z;
2177     mvsadcost[1][-i] = (int)z;
2178   } while (++i <= MV_MAX);
2179 }
2180 
init_ref_frame_bufs(VP9_COMMON * cm)2181 static void init_ref_frame_bufs(VP9_COMMON *cm) {
2182   int i;
2183   BufferPool *const pool = cm->buffer_pool;
2184   cm->new_fb_idx = INVALID_IDX;
2185   for (i = 0; i < REF_FRAMES; ++i) {
2186     cm->ref_frame_map[i] = INVALID_IDX;
2187   }
2188   for (i = 0; i < FRAME_BUFFERS; ++i) {
2189     pool->frame_bufs[i].ref_count = 0;
2190   }
2191 }
2192 
update_initial_width(VP9_COMP * cpi,int use_highbitdepth,int subsampling_x,int subsampling_y)2193 static void update_initial_width(VP9_COMP *cpi, int use_highbitdepth,
2194                                  int subsampling_x, int subsampling_y) {
2195   VP9_COMMON *const cm = &cpi->common;
2196 #if !CONFIG_VP9_HIGHBITDEPTH
2197   (void)use_highbitdepth;
2198   assert(use_highbitdepth == 0);
2199 #endif
2200 
2201   if (!cpi->initial_width ||
2202 #if CONFIG_VP9_HIGHBITDEPTH
2203       cm->use_highbitdepth != use_highbitdepth ||
2204 #endif
2205       cm->subsampling_x != subsampling_x ||
2206       cm->subsampling_y != subsampling_y) {
2207     cm->subsampling_x = subsampling_x;
2208     cm->subsampling_y = subsampling_y;
2209 #if CONFIG_VP9_HIGHBITDEPTH
2210     cm->use_highbitdepth = use_highbitdepth;
2211 #endif
2212     alloc_util_frame_buffers(cpi);
2213     cpi->initial_width = cm->width;
2214     cpi->initial_height = cm->height;
2215     cpi->initial_mbs = cm->MBs;
2216   }
2217 }
2218 
2219 // TODO(angiebird): Check whether we can move this function to vpx_image.c
vpx_img_chroma_subsampling(vpx_img_fmt_t fmt,unsigned int * subsampling_x,unsigned int * subsampling_y)2220 static INLINE void vpx_img_chroma_subsampling(vpx_img_fmt_t fmt,
2221                                               unsigned int *subsampling_x,
2222                                               unsigned int *subsampling_y) {
2223   switch (fmt) {
2224     case VPX_IMG_FMT_I420:
2225     case VPX_IMG_FMT_YV12:
2226     case VPX_IMG_FMT_I422:
2227     case VPX_IMG_FMT_I42016:
2228     case VPX_IMG_FMT_I42216: *subsampling_x = 1; break;
2229     default: *subsampling_x = 0; break;
2230   }
2231 
2232   switch (fmt) {
2233     case VPX_IMG_FMT_I420:
2234     case VPX_IMG_FMT_I440:
2235     case VPX_IMG_FMT_YV12:
2236     case VPX_IMG_FMT_I42016:
2237     case VPX_IMG_FMT_I44016: *subsampling_y = 1; break;
2238     default: *subsampling_y = 0; break;
2239   }
2240 }
2241 
2242 // TODO(angiebird): Check whether we can move this function to vpx_image.c
vpx_img_use_highbitdepth(vpx_img_fmt_t fmt)2243 static INLINE int vpx_img_use_highbitdepth(vpx_img_fmt_t fmt) {
2244   return fmt & VPX_IMG_FMT_HIGHBITDEPTH;
2245 }
2246 
2247 #if CONFIG_VP9_TEMPORAL_DENOISING
setup_denoiser_buffer(VP9_COMP * cpi)2248 static void setup_denoiser_buffer(VP9_COMP *cpi) {
2249   VP9_COMMON *const cm = &cpi->common;
2250   if (cpi->oxcf.noise_sensitivity > 0 &&
2251       !cpi->denoiser.frame_buffer_initialized) {
2252     if (vp9_denoiser_alloc(cm, &cpi->svc, &cpi->denoiser, cpi->use_svc,
2253                            cpi->oxcf.noise_sensitivity, cm->width, cm->height,
2254                            cm->subsampling_x, cm->subsampling_y,
2255 #if CONFIG_VP9_HIGHBITDEPTH
2256                            cm->use_highbitdepth,
2257 #endif
2258                            VP9_ENC_BORDER_IN_PIXELS))
2259       vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
2260                          "Failed to allocate denoiser");
2261   }
2262 }
2263 #endif
2264 
vp9_update_compressor_with_img_fmt(VP9_COMP * cpi,vpx_img_fmt_t img_fmt)2265 void vp9_update_compressor_with_img_fmt(VP9_COMP *cpi, vpx_img_fmt_t img_fmt) {
2266   const VP9EncoderConfig *oxcf = &cpi->oxcf;
2267   unsigned int subsampling_x, subsampling_y;
2268   const int use_highbitdepth = vpx_img_use_highbitdepth(img_fmt);
2269   vpx_img_chroma_subsampling(img_fmt, &subsampling_x, &subsampling_y);
2270 
2271   update_initial_width(cpi, use_highbitdepth, subsampling_x, subsampling_y);
2272 #if CONFIG_VP9_TEMPORAL_DENOISING
2273   setup_denoiser_buffer(cpi);
2274 #endif
2275 
2276   assert(cpi->lookahead == NULL);
2277   cpi->lookahead = vp9_lookahead_init(oxcf->width, oxcf->height, subsampling_x,
2278                                       subsampling_y,
2279 #if CONFIG_VP9_HIGHBITDEPTH
2280                                       use_highbitdepth,
2281 #endif
2282                                       oxcf->lag_in_frames);
2283   alloc_raw_frame_buffers(cpi);
2284 }
2285 
vp9_create_compressor(const VP9EncoderConfig * oxcf,BufferPool * const pool)2286 VP9_COMP *vp9_create_compressor(const VP9EncoderConfig *oxcf,
2287                                 BufferPool *const pool) {
2288   unsigned int i;
2289   VP9_COMP *volatile const cpi = vpx_memalign(32, sizeof(VP9_COMP));
2290   VP9_COMMON *volatile const cm = cpi != NULL ? &cpi->common : NULL;
2291 
2292   if (!cm) return NULL;
2293 
2294   vp9_zero(*cpi);
2295 
2296   if (setjmp(cm->error.jmp)) {
2297     cm->error.setjmp = 0;
2298     vp9_remove_compressor(cpi);
2299     return 0;
2300   }
2301 
2302   cm->error.setjmp = 1;
2303   cm->alloc_mi = vp9_enc_alloc_mi;
2304   cm->free_mi = vp9_enc_free_mi;
2305   cm->setup_mi = vp9_enc_setup_mi;
2306 
2307   CHECK_MEM_ERROR(cm, cm->fc, (FRAME_CONTEXT *)vpx_calloc(1, sizeof(*cm->fc)));
2308   CHECK_MEM_ERROR(
2309       cm, cm->frame_contexts,
2310       (FRAME_CONTEXT *)vpx_calloc(FRAME_CONTEXTS, sizeof(*cm->frame_contexts)));
2311 
2312   cpi->compute_frame_low_motion_onepass = 1;
2313   cpi->use_svc = 0;
2314   cpi->resize_state = ORIG;
2315   cpi->external_resize = 0;
2316   cpi->resize_avg_qp = 0;
2317   cpi->resize_buffer_underflow = 0;
2318   cpi->use_skin_detection = 0;
2319   cpi->common.buffer_pool = pool;
2320   init_ref_frame_bufs(cm);
2321 
2322   cpi->force_update_segmentation = 0;
2323 
2324   init_config(cpi, oxcf);
2325   cpi->frame_info = vp9_get_frame_info(oxcf);
2326 
2327   vp9_rc_init(&cpi->oxcf, oxcf->pass, &cpi->rc);
2328   vp9_init_rd_parameters(cpi);
2329 
2330   init_frame_indexes(cm);
2331   cpi->tile_data = NULL;
2332 
2333   realloc_segmentation_maps(cpi);
2334 
2335   CHECK_MEM_ERROR(
2336       cm, cpi->skin_map,
2337       vpx_calloc(cm->mi_rows * cm->mi_cols, sizeof(cpi->skin_map[0])));
2338 
2339 #if !CONFIG_REALTIME_ONLY
2340   CHECK_MEM_ERROR(cm, cpi->alt_ref_aq, vp9_alt_ref_aq_create());
2341 #endif
2342 
2343   CHECK_MEM_ERROR(
2344       cm, cpi->consec_zero_mv,
2345       vpx_calloc(cm->mi_rows * cm->mi_cols, sizeof(*cpi->consec_zero_mv)));
2346 
2347   CHECK_MEM_ERROR(cm, cpi->nmvcosts[0],
2348                   vpx_calloc(MV_VALS, sizeof(*cpi->nmvcosts[0])));
2349   CHECK_MEM_ERROR(cm, cpi->nmvcosts[1],
2350                   vpx_calloc(MV_VALS, sizeof(*cpi->nmvcosts[1])));
2351   CHECK_MEM_ERROR(cm, cpi->nmvcosts_hp[0],
2352                   vpx_calloc(MV_VALS, sizeof(*cpi->nmvcosts_hp[0])));
2353   CHECK_MEM_ERROR(cm, cpi->nmvcosts_hp[1],
2354                   vpx_calloc(MV_VALS, sizeof(*cpi->nmvcosts_hp[1])));
2355   CHECK_MEM_ERROR(cm, cpi->nmvsadcosts[0],
2356                   vpx_calloc(MV_VALS, sizeof(*cpi->nmvsadcosts[0])));
2357   CHECK_MEM_ERROR(cm, cpi->nmvsadcosts[1],
2358                   vpx_calloc(MV_VALS, sizeof(*cpi->nmvsadcosts[1])));
2359   CHECK_MEM_ERROR(cm, cpi->nmvsadcosts_hp[0],
2360                   vpx_calloc(MV_VALS, sizeof(*cpi->nmvsadcosts_hp[0])));
2361   CHECK_MEM_ERROR(cm, cpi->nmvsadcosts_hp[1],
2362                   vpx_calloc(MV_VALS, sizeof(*cpi->nmvsadcosts_hp[1])));
2363 
2364   for (i = 0; i < (sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]));
2365        i++) {
2366     CHECK_MEM_ERROR(
2367         cm, cpi->mbgraph_stats[i].mb_stats,
2368         vpx_calloc(cm->MBs * sizeof(*cpi->mbgraph_stats[i].mb_stats), 1));
2369   }
2370 
2371   cpi->refresh_alt_ref_frame = 0;
2372   cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
2373 
2374   init_level_info(&cpi->level_info);
2375   init_level_constraint(&cpi->level_constraint);
2376 
2377 #if CONFIG_INTERNAL_STATS
2378   cpi->b_calculate_blockiness = 1;
2379   cpi->b_calculate_consistency = 1;
2380   cpi->total_inconsistency = 0;
2381   cpi->psnr.worst = 100.0;
2382   cpi->worst_ssim = 100.0;
2383 
2384   cpi->count = 0;
2385   cpi->bytes = 0;
2386 
2387   if (cpi->b_calculate_psnr) {
2388     cpi->total_sq_error = 0;
2389     cpi->total_samples = 0;
2390 
2391     cpi->totalp_sq_error = 0;
2392     cpi->totalp_samples = 0;
2393 
2394     cpi->tot_recode_hits = 0;
2395     cpi->summed_quality = 0;
2396     cpi->summed_weights = 0;
2397     cpi->summedp_quality = 0;
2398     cpi->summedp_weights = 0;
2399   }
2400 
2401   cpi->fastssim.worst = 100.0;
2402 
2403   cpi->psnrhvs.worst = 100.0;
2404 
2405   if (cpi->b_calculate_blockiness) {
2406     cpi->total_blockiness = 0;
2407     cpi->worst_blockiness = 0.0;
2408   }
2409 
2410   if (cpi->b_calculate_consistency) {
2411     CHECK_MEM_ERROR(cm, cpi->ssim_vars,
2412                     vpx_calloc(cpi->common.mi_rows * cpi->common.mi_cols,
2413                                sizeof(*cpi->ssim_vars) * 4));
2414     cpi->worst_consistency = 100.0;
2415   } else {
2416     cpi->ssim_vars = NULL;
2417   }
2418 
2419 #endif
2420 
2421   cpi->first_time_stamp_ever = INT64_MAX;
2422 
2423   /*********************************************************************
2424    * Warning: Read the comments around 'cal_nmvjointsadcost' and       *
2425    * 'cal_nmvsadcosts' before modifying how these tables are computed. *
2426    *********************************************************************/
2427   cal_nmvjointsadcost(cpi->td.mb.nmvjointsadcost);
2428   cpi->td.mb.nmvcost[0] = &cpi->nmvcosts[0][MV_MAX];
2429   cpi->td.mb.nmvcost[1] = &cpi->nmvcosts[1][MV_MAX];
2430   cpi->td.mb.nmvsadcost[0] = &cpi->nmvsadcosts[0][MV_MAX];
2431   cpi->td.mb.nmvsadcost[1] = &cpi->nmvsadcosts[1][MV_MAX];
2432   cal_nmvsadcosts(cpi->td.mb.nmvsadcost);
2433 
2434   cpi->td.mb.nmvcost_hp[0] = &cpi->nmvcosts_hp[0][MV_MAX];
2435   cpi->td.mb.nmvcost_hp[1] = &cpi->nmvcosts_hp[1][MV_MAX];
2436   cpi->td.mb.nmvsadcost_hp[0] = &cpi->nmvsadcosts_hp[0][MV_MAX];
2437   cpi->td.mb.nmvsadcost_hp[1] = &cpi->nmvsadcosts_hp[1][MV_MAX];
2438   cal_nmvsadcosts_hp(cpi->td.mb.nmvsadcost_hp);
2439 
2440 #if CONFIG_VP9_TEMPORAL_DENOISING
2441 #ifdef OUTPUT_YUV_DENOISED
2442   yuv_denoised_file = fopen("denoised.yuv", "ab");
2443 #endif
2444 #endif
2445 #ifdef OUTPUT_YUV_SKINMAP
2446   yuv_skinmap_file = fopen("skinmap.yuv", "wb");
2447 #endif
2448 #ifdef OUTPUT_YUV_REC
2449   yuv_rec_file = fopen("rec.yuv", "wb");
2450 #endif
2451 #ifdef OUTPUT_YUV_SVC_SRC
2452   yuv_svc_src[0] = fopen("svc_src_0.yuv", "wb");
2453   yuv_svc_src[1] = fopen("svc_src_1.yuv", "wb");
2454   yuv_svc_src[2] = fopen("svc_src_2.yuv", "wb");
2455 #endif
2456 
2457 #if 0
2458   framepsnr = fopen("framepsnr.stt", "a");
2459   kf_list = fopen("kf_list.stt", "w");
2460 #endif
2461 
2462   cpi->allow_encode_breakout = ENCODE_BREAKOUT_ENABLED;
2463 
2464   {
2465     vpx_codec_err_t codec_status = vp9_extrc_init(&cpi->ext_ratectrl);
2466     if (codec_status != VPX_CODEC_OK) {
2467       vpx_internal_error(&cm->error, codec_status, "vp9_extrc_init() failed");
2468     }
2469   }
2470 
2471 #if !CONFIG_REALTIME_ONLY
2472   if (oxcf->pass == 1) {
2473     vp9_init_first_pass(cpi);
2474   } else if (oxcf->pass == 2) {
2475     const size_t packet_sz = sizeof(FIRSTPASS_STATS);
2476     const int packets = (int)(oxcf->two_pass_stats_in.sz / packet_sz);
2477 
2478     if (cpi->svc.number_spatial_layers > 1 ||
2479         cpi->svc.number_temporal_layers > 1) {
2480       FIRSTPASS_STATS *const stats = oxcf->two_pass_stats_in.buf;
2481       FIRSTPASS_STATS *stats_copy[VPX_SS_MAX_LAYERS] = { 0 };
2482       int i;
2483 
2484       for (i = 0; i < oxcf->ss_number_layers; ++i) {
2485         FIRSTPASS_STATS *const last_packet_for_layer =
2486             &stats[packets - oxcf->ss_number_layers + i];
2487         const int layer_id = (int)last_packet_for_layer->spatial_layer_id;
2488         const int packets_in_layer = (int)last_packet_for_layer->count + 1;
2489         if (layer_id >= 0 && layer_id < oxcf->ss_number_layers) {
2490           int num_frames;
2491           LAYER_CONTEXT *const lc = &cpi->svc.layer_context[layer_id];
2492 
2493           vpx_free(lc->rc_twopass_stats_in.buf);
2494 
2495           lc->rc_twopass_stats_in.sz = packets_in_layer * packet_sz;
2496           CHECK_MEM_ERROR(cm, lc->rc_twopass_stats_in.buf,
2497                           vpx_malloc(lc->rc_twopass_stats_in.sz));
2498           lc->twopass.stats_in_start = lc->rc_twopass_stats_in.buf;
2499           lc->twopass.stats_in = lc->twopass.stats_in_start;
2500           lc->twopass.stats_in_end =
2501               lc->twopass.stats_in_start + packets_in_layer - 1;
2502           // Note the last packet is cumulative first pass stats.
2503           // So the number of frames is packet number minus one
2504           num_frames = packets_in_layer - 1;
2505           fps_init_first_pass_info(&lc->twopass.first_pass_info,
2506                                    lc->rc_twopass_stats_in.buf, num_frames);
2507           stats_copy[layer_id] = lc->rc_twopass_stats_in.buf;
2508         }
2509       }
2510 
2511       for (i = 0; i < packets; ++i) {
2512         const int layer_id = (int)stats[i].spatial_layer_id;
2513         if (layer_id >= 0 && layer_id < oxcf->ss_number_layers &&
2514             stats_copy[layer_id] != NULL) {
2515           *stats_copy[layer_id] = stats[i];
2516           ++stats_copy[layer_id];
2517         }
2518       }
2519 
2520       vp9_init_second_pass_spatial_svc(cpi);
2521     } else {
2522       int num_frames;
2523 
2524       cpi->twopass.stats_in_start = oxcf->two_pass_stats_in.buf;
2525       cpi->twopass.stats_in = cpi->twopass.stats_in_start;
2526       cpi->twopass.stats_in_end = &cpi->twopass.stats_in[packets - 1];
2527       // Note the last packet is cumulative first pass stats.
2528       // So the number of frames is packet number minus one
2529       num_frames = packets - 1;
2530       fps_init_first_pass_info(&cpi->twopass.first_pass_info,
2531                                oxcf->two_pass_stats_in.buf, num_frames);
2532 
2533       vp9_init_second_pass(cpi);
2534     }
2535   }
2536 #endif  // !CONFIG_REALTIME_ONLY
2537 
2538   cpi->mb_wiener_var_cols = 0;
2539   cpi->mb_wiener_var_rows = 0;
2540   cpi->mb_wiener_variance = NULL;
2541 
2542   vp9_set_speed_features_framesize_independent(cpi, oxcf->speed);
2543   vp9_set_speed_features_framesize_dependent(cpi, oxcf->speed);
2544 
2545   {
2546     const int bsize = BLOCK_16X16;
2547     const int w = num_8x8_blocks_wide_lookup[bsize];
2548     const int h = num_8x8_blocks_high_lookup[bsize];
2549     const int num_cols = (cm->mi_cols + w - 1) / w;
2550     const int num_rows = (cm->mi_rows + h - 1) / h;
2551     CHECK_MEM_ERROR(cm, cpi->mi_ssim_rdmult_scaling_factors,
2552                     vpx_calloc(num_rows * num_cols,
2553                                sizeof(*cpi->mi_ssim_rdmult_scaling_factors)));
2554   }
2555 
2556   cpi->kmeans_data_arr_alloc = 0;
2557 #if CONFIG_NON_GREEDY_MV
2558   cpi->tpl_ready = 0;
2559 #endif  // CONFIG_NON_GREEDY_MV
2560   for (i = 0; i < MAX_ARF_GOP_SIZE; ++i) cpi->tpl_stats[i].tpl_stats_ptr = NULL;
2561 
2562   // Allocate memory to store variances for a frame.
2563   CHECK_MEM_ERROR(cm, cpi->source_diff_var, vpx_calloc(cm->MBs, sizeof(diff)));
2564   cpi->source_var_thresh = 0;
2565   cpi->frames_till_next_var_check = 0;
2566 #define BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX4DF, SDX8F) \
2567   cpi->fn_ptr[BT].sdf = SDF;                             \
2568   cpi->fn_ptr[BT].sdaf = SDAF;                           \
2569   cpi->fn_ptr[BT].vf = VF;                               \
2570   cpi->fn_ptr[BT].svf = SVF;                             \
2571   cpi->fn_ptr[BT].svaf = SVAF;                           \
2572   cpi->fn_ptr[BT].sdx4df = SDX4DF;                       \
2573   cpi->fn_ptr[BT].sdx8f = SDX8F;
2574 
2575   // TODO(angiebird): make sdx8f available for every block size
2576   BFP(BLOCK_32X16, vpx_sad32x16, vpx_sad32x16_avg, vpx_variance32x16,
2577       vpx_sub_pixel_variance32x16, vpx_sub_pixel_avg_variance32x16,
2578       vpx_sad32x16x4d, NULL)
2579 
2580   BFP(BLOCK_16X32, vpx_sad16x32, vpx_sad16x32_avg, vpx_variance16x32,
2581       vpx_sub_pixel_variance16x32, vpx_sub_pixel_avg_variance16x32,
2582       vpx_sad16x32x4d, NULL)
2583 
2584   BFP(BLOCK_64X32, vpx_sad64x32, vpx_sad64x32_avg, vpx_variance64x32,
2585       vpx_sub_pixel_variance64x32, vpx_sub_pixel_avg_variance64x32,
2586       vpx_sad64x32x4d, NULL)
2587 
2588   BFP(BLOCK_32X64, vpx_sad32x64, vpx_sad32x64_avg, vpx_variance32x64,
2589       vpx_sub_pixel_variance32x64, vpx_sub_pixel_avg_variance32x64,
2590       vpx_sad32x64x4d, NULL)
2591 
2592   BFP(BLOCK_32X32, vpx_sad32x32, vpx_sad32x32_avg, vpx_variance32x32,
2593       vpx_sub_pixel_variance32x32, vpx_sub_pixel_avg_variance32x32,
2594       vpx_sad32x32x4d, vpx_sad32x32x8)
2595 
2596   BFP(BLOCK_64X64, vpx_sad64x64, vpx_sad64x64_avg, vpx_variance64x64,
2597       vpx_sub_pixel_variance64x64, vpx_sub_pixel_avg_variance64x64,
2598       vpx_sad64x64x4d, NULL)
2599 
2600   BFP(BLOCK_16X16, vpx_sad16x16, vpx_sad16x16_avg, vpx_variance16x16,
2601       vpx_sub_pixel_variance16x16, vpx_sub_pixel_avg_variance16x16,
2602       vpx_sad16x16x4d, vpx_sad16x16x8)
2603 
2604   BFP(BLOCK_16X8, vpx_sad16x8, vpx_sad16x8_avg, vpx_variance16x8,
2605       vpx_sub_pixel_variance16x8, vpx_sub_pixel_avg_variance16x8,
2606       vpx_sad16x8x4d, vpx_sad16x8x8)
2607 
2608   BFP(BLOCK_8X16, vpx_sad8x16, vpx_sad8x16_avg, vpx_variance8x16,
2609       vpx_sub_pixel_variance8x16, vpx_sub_pixel_avg_variance8x16,
2610       vpx_sad8x16x4d, vpx_sad8x16x8)
2611 
2612   BFP(BLOCK_8X8, vpx_sad8x8, vpx_sad8x8_avg, vpx_variance8x8,
2613       vpx_sub_pixel_variance8x8, vpx_sub_pixel_avg_variance8x8, vpx_sad8x8x4d,
2614       vpx_sad8x8x8)
2615 
2616   BFP(BLOCK_8X4, vpx_sad8x4, vpx_sad8x4_avg, vpx_variance8x4,
2617       vpx_sub_pixel_variance8x4, vpx_sub_pixel_avg_variance8x4, vpx_sad8x4x4d,
2618       NULL)
2619 
2620   BFP(BLOCK_4X8, vpx_sad4x8, vpx_sad4x8_avg, vpx_variance4x8,
2621       vpx_sub_pixel_variance4x8, vpx_sub_pixel_avg_variance4x8, vpx_sad4x8x4d,
2622       NULL)
2623 
2624   BFP(BLOCK_4X4, vpx_sad4x4, vpx_sad4x4_avg, vpx_variance4x4,
2625       vpx_sub_pixel_variance4x4, vpx_sub_pixel_avg_variance4x4, vpx_sad4x4x4d,
2626       vpx_sad4x4x8)
2627 
2628 #if CONFIG_VP9_HIGHBITDEPTH
2629   highbd_set_var_fns(cpi);
2630 #endif
2631 
2632   /* vp9_init_quantizer() is first called here. Add check in
2633    * vp9_frame_init_quantizer() so that vp9_init_quantizer is only
2634    * called later when needed. This will avoid unnecessary calls of
2635    * vp9_init_quantizer() for every frame.
2636    */
2637   vp9_init_quantizer(cpi);
2638 
2639   vp9_loop_filter_init(cm);
2640 
2641   // Set up the unit scaling factor used during motion search.
2642 #if CONFIG_VP9_HIGHBITDEPTH
2643   vp9_setup_scale_factors_for_frame(&cpi->me_sf, cm->width, cm->height,
2644                                     cm->width, cm->height,
2645                                     cm->use_highbitdepth);
2646 #else
2647   vp9_setup_scale_factors_for_frame(&cpi->me_sf, cm->width, cm->height,
2648                                     cm->width, cm->height);
2649 #endif  // CONFIG_VP9_HIGHBITDEPTH
2650   cpi->td.mb.me_sf = &cpi->me_sf;
2651 
2652   cm->error.setjmp = 0;
2653 
2654 #if CONFIG_RATE_CTRL
2655   encode_command_init(&cpi->encode_command);
2656   if (oxcf->use_simple_encode_api) {
2657     partition_info_init(cpi);
2658     motion_vector_info_init(cpi);
2659     fp_motion_vector_info_init(cpi);
2660     tpl_stats_info_init(cpi);
2661   }
2662 #endif
2663 
2664   return cpi;
2665 }
2666 
2667 #if CONFIG_INTERNAL_STATS
2668 #define SNPRINT(H, T) snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T))
2669 
2670 #define SNPRINT2(H, T, V) \
2671   snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T), (V))
2672 #endif  // CONFIG_INTERNAL_STATS
2673 
2674 static void free_tpl_buffer(VP9_COMP *cpi);
2675 
vp9_remove_compressor(VP9_COMP * cpi)2676 void vp9_remove_compressor(VP9_COMP *cpi) {
2677   VP9_COMMON *cm;
2678   unsigned int i;
2679   int t;
2680 
2681   if (!cpi) return;
2682 
2683 #if CONFIG_INTERNAL_STATS
2684   vpx_free(cpi->ssim_vars);
2685 #endif
2686 
2687   cm = &cpi->common;
2688   if (cm->current_video_frame > 0) {
2689 #if CONFIG_INTERNAL_STATS
2690     vpx_clear_system_state();
2691 
2692     if (cpi->oxcf.pass != 1) {
2693       char headings[512] = { 0 };
2694       char results[512] = { 0 };
2695       FILE *f = fopen("opsnr.stt", "a");
2696       double time_encoded =
2697           (cpi->last_end_time_stamp_seen - cpi->first_time_stamp_ever) /
2698           10000000.000;
2699       double total_encode_time =
2700           (cpi->time_receive_data + cpi->time_compress_data) / 1000.000;
2701       const double dr =
2702           (double)cpi->bytes * (double)8 / (double)1000 / time_encoded;
2703       const double peak = (double)((1 << cpi->oxcf.input_bit_depth) - 1);
2704       const double target_rate = (double)cpi->oxcf.target_bandwidth / 1000;
2705       const double rate_err = ((100.0 * (dr - target_rate)) / target_rate);
2706 
2707       if (cpi->b_calculate_psnr) {
2708         const double total_psnr = vpx_sse_to_psnr(
2709             (double)cpi->total_samples, peak, (double)cpi->total_sq_error);
2710         const double totalp_psnr = vpx_sse_to_psnr(
2711             (double)cpi->totalp_samples, peak, (double)cpi->totalp_sq_error);
2712         const double total_ssim =
2713             100 * pow(cpi->summed_quality / cpi->summed_weights, 8.0);
2714         const double totalp_ssim =
2715             100 * pow(cpi->summedp_quality / cpi->summedp_weights, 8.0);
2716 
2717         snprintf(headings, sizeof(headings),
2718                  "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\t"
2719                  "VPXSSIM\tVPSSIMP\tFASTSIM\tPSNRHVS\t"
2720                  "WstPsnr\tWstSsim\tWstFast\tWstHVS\t"
2721                  "AVPsnrY\tAPsnrCb\tAPsnrCr");
2722         snprintf(results, sizeof(results),
2723                  "%7.2f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
2724                  "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
2725                  "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
2726                  "%7.3f\t%7.3f\t%7.3f",
2727                  dr, cpi->psnr.stat[ALL] / cpi->count, total_psnr,
2728                  cpi->psnrp.stat[ALL] / cpi->count, totalp_psnr, total_ssim,
2729                  totalp_ssim, cpi->fastssim.stat[ALL] / cpi->count,
2730                  cpi->psnrhvs.stat[ALL] / cpi->count, cpi->psnr.worst,
2731                  cpi->worst_ssim, cpi->fastssim.worst, cpi->psnrhvs.worst,
2732                  cpi->psnr.stat[Y] / cpi->count, cpi->psnr.stat[U] / cpi->count,
2733                  cpi->psnr.stat[V] / cpi->count);
2734 
2735         if (cpi->b_calculate_blockiness) {
2736           SNPRINT(headings, "\t  Block\tWstBlck");
2737           SNPRINT2(results, "\t%7.3f", cpi->total_blockiness / cpi->count);
2738           SNPRINT2(results, "\t%7.3f", cpi->worst_blockiness);
2739         }
2740 
2741         if (cpi->b_calculate_consistency) {
2742           double consistency =
2743               vpx_sse_to_psnr((double)cpi->totalp_samples, peak,
2744                               (double)cpi->total_inconsistency);
2745 
2746           SNPRINT(headings, "\tConsist\tWstCons");
2747           SNPRINT2(results, "\t%7.3f", consistency);
2748           SNPRINT2(results, "\t%7.3f", cpi->worst_consistency);
2749         }
2750 
2751         SNPRINT(headings, "\t    Time\tRcErr\tAbsErr");
2752         SNPRINT2(results, "\t%8.0f", total_encode_time);
2753         SNPRINT2(results, "\t%7.2f", rate_err);
2754         SNPRINT2(results, "\t%7.2f", fabs(rate_err));
2755 
2756         fprintf(f, "%s\tAPsnr611\n", headings);
2757         fprintf(
2758             f, "%s\t%7.3f\n", results,
2759             (6 * cpi->psnr.stat[Y] + cpi->psnr.stat[U] + cpi->psnr.stat[V]) /
2760                 (cpi->count * 8));
2761       }
2762 
2763       fclose(f);
2764     }
2765 #endif
2766 
2767 #if 0
2768     {
2769       printf("\n_pick_loop_filter_level:%d\n", cpi->time_pick_lpf / 1000);
2770       printf("\n_frames recive_data encod_mb_row compress_frame  Total\n");
2771       printf("%6d %10ld %10ld %10ld %10ld\n", cpi->common.current_video_frame,
2772              cpi->time_receive_data / 1000, cpi->time_encode_sb_row / 1000,
2773              cpi->time_compress_data / 1000,
2774              (cpi->time_receive_data + cpi->time_compress_data) / 1000);
2775     }
2776 #endif
2777   }
2778 
2779 #if CONFIG_VP9_TEMPORAL_DENOISING
2780   vp9_denoiser_free(&(cpi->denoiser));
2781 #endif
2782 
2783   if (cpi->kmeans_data_arr_alloc) {
2784 #if CONFIG_MULTITHREAD
2785     pthread_mutex_destroy(&cpi->kmeans_mutex);
2786 #endif
2787     vpx_free(cpi->kmeans_data_arr);
2788   }
2789 
2790   free_tpl_buffer(cpi);
2791 
2792   for (t = 0; t < cpi->num_workers; ++t) {
2793     VPxWorker *const worker = &cpi->workers[t];
2794     EncWorkerData *const thread_data = &cpi->tile_thr_data[t];
2795 
2796     // Deallocate allocated threads.
2797     vpx_get_worker_interface()->end(worker);
2798 
2799     // Deallocate allocated thread data.
2800     if (t < cpi->num_workers - 1) {
2801       vpx_free(thread_data->td->counts);
2802       vp9_free_pc_tree(thread_data->td);
2803       vpx_free(thread_data->td);
2804     }
2805   }
2806   vpx_free(cpi->tile_thr_data);
2807   vpx_free(cpi->workers);
2808   vp9_row_mt_mem_dealloc(cpi);
2809 
2810   if (cpi->num_workers > 1) {
2811     vp9_loop_filter_dealloc(&cpi->lf_row_sync);
2812     vp9_bitstream_encode_tiles_buffer_dealloc(cpi);
2813   }
2814 
2815 #if !CONFIG_REALTIME_ONLY
2816   vp9_alt_ref_aq_destroy(cpi->alt_ref_aq);
2817 #endif
2818 
2819   dealloc_compressor_data(cpi);
2820 
2821   for (i = 0; i < sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]);
2822        ++i) {
2823     vpx_free(cpi->mbgraph_stats[i].mb_stats);
2824   }
2825 
2826   vp9_extrc_delete(&cpi->ext_ratectrl);
2827 
2828   vp9_remove_common(cm);
2829   vp9_free_ref_frame_buffers(cm->buffer_pool);
2830 #if CONFIG_VP9_POSTPROC
2831   vp9_free_postproc_buffers(cm);
2832 #endif
2833   vpx_free(cpi);
2834 
2835 #if CONFIG_VP9_TEMPORAL_DENOISING
2836 #ifdef OUTPUT_YUV_DENOISED
2837   fclose(yuv_denoised_file);
2838 #endif
2839 #endif
2840 #ifdef OUTPUT_YUV_SKINMAP
2841   fclose(yuv_skinmap_file);
2842 #endif
2843 #ifdef OUTPUT_YUV_REC
2844   fclose(yuv_rec_file);
2845 #endif
2846 #ifdef OUTPUT_YUV_SVC_SRC
2847   fclose(yuv_svc_src[0]);
2848   fclose(yuv_svc_src[1]);
2849   fclose(yuv_svc_src[2]);
2850 #endif
2851 
2852 #if 0
2853 
2854   if (keyfile)
2855     fclose(keyfile);
2856 
2857   if (framepsnr)
2858     fclose(framepsnr);
2859 
2860   if (kf_list)
2861     fclose(kf_list);
2862 
2863 #endif
2864 }
2865 
vp9_get_psnr(const VP9_COMP * cpi,PSNR_STATS * psnr)2866 int vp9_get_psnr(const VP9_COMP *cpi, PSNR_STATS *psnr) {
2867   if (is_psnr_calc_enabled(cpi)) {
2868 #if CONFIG_VP9_HIGHBITDEPTH
2869     vpx_calc_highbd_psnr(cpi->raw_source_frame, cpi->common.frame_to_show, psnr,
2870                          cpi->td.mb.e_mbd.bd, cpi->oxcf.input_bit_depth);
2871 #else
2872     vpx_calc_psnr(cpi->raw_source_frame, cpi->common.frame_to_show, psnr);
2873 #endif
2874     return 1;
2875   } else {
2876     vp9_zero(*psnr);
2877     return 0;
2878   }
2879 }
2880 
vp9_use_as_reference(VP9_COMP * cpi,int ref_frame_flags)2881 int vp9_use_as_reference(VP9_COMP *cpi, int ref_frame_flags) {
2882   if (ref_frame_flags > 7) return -1;
2883 
2884   cpi->ref_frame_flags = ref_frame_flags;
2885   return 0;
2886 }
2887 
vp9_update_reference(VP9_COMP * cpi,int ref_frame_flags)2888 void vp9_update_reference(VP9_COMP *cpi, int ref_frame_flags) {
2889   cpi->ext_refresh_golden_frame = (ref_frame_flags & VP9_GOLD_FLAG) != 0;
2890   cpi->ext_refresh_alt_ref_frame = (ref_frame_flags & VP9_ALT_FLAG) != 0;
2891   cpi->ext_refresh_last_frame = (ref_frame_flags & VP9_LAST_FLAG) != 0;
2892   cpi->ext_refresh_frame_flags_pending = 1;
2893 }
2894 
get_vp9_ref_frame_buffer(VP9_COMP * cpi,VP9_REFFRAME ref_frame_flag)2895 static YV12_BUFFER_CONFIG *get_vp9_ref_frame_buffer(
2896     VP9_COMP *cpi, VP9_REFFRAME ref_frame_flag) {
2897   MV_REFERENCE_FRAME ref_frame = NONE;
2898   if (ref_frame_flag == VP9_LAST_FLAG)
2899     ref_frame = LAST_FRAME;
2900   else if (ref_frame_flag == VP9_GOLD_FLAG)
2901     ref_frame = GOLDEN_FRAME;
2902   else if (ref_frame_flag == VP9_ALT_FLAG)
2903     ref_frame = ALTREF_FRAME;
2904 
2905   return ref_frame == NONE ? NULL : get_ref_frame_buffer(cpi, ref_frame);
2906 }
2907 
vp9_copy_reference_enc(VP9_COMP * cpi,VP9_REFFRAME ref_frame_flag,YV12_BUFFER_CONFIG * sd)2908 int vp9_copy_reference_enc(VP9_COMP *cpi, VP9_REFFRAME ref_frame_flag,
2909                            YV12_BUFFER_CONFIG *sd) {
2910   YV12_BUFFER_CONFIG *cfg = get_vp9_ref_frame_buffer(cpi, ref_frame_flag);
2911   if (cfg) {
2912     vpx_yv12_copy_frame(cfg, sd);
2913     return 0;
2914   } else {
2915     return -1;
2916   }
2917 }
2918 
vp9_set_reference_enc(VP9_COMP * cpi,VP9_REFFRAME ref_frame_flag,YV12_BUFFER_CONFIG * sd)2919 int vp9_set_reference_enc(VP9_COMP *cpi, VP9_REFFRAME ref_frame_flag,
2920                           YV12_BUFFER_CONFIG *sd) {
2921   YV12_BUFFER_CONFIG *cfg = get_vp9_ref_frame_buffer(cpi, ref_frame_flag);
2922   if (cfg) {
2923     vpx_yv12_copy_frame(sd, cfg);
2924     return 0;
2925   } else {
2926     return -1;
2927   }
2928 }
2929 
vp9_update_entropy(VP9_COMP * cpi,int update)2930 int vp9_update_entropy(VP9_COMP *cpi, int update) {
2931   cpi->ext_refresh_frame_context = update;
2932   cpi->ext_refresh_frame_context_pending = 1;
2933   return 0;
2934 }
2935 
2936 #ifdef OUTPUT_YUV_REC
vp9_write_yuv_rec_frame(VP9_COMMON * cm)2937 void vp9_write_yuv_rec_frame(VP9_COMMON *cm) {
2938   YV12_BUFFER_CONFIG *s = cm->frame_to_show;
2939   uint8_t *src = s->y_buffer;
2940   int h = cm->height;
2941 
2942 #if CONFIG_VP9_HIGHBITDEPTH
2943   if (s->flags & YV12_FLAG_HIGHBITDEPTH) {
2944     uint16_t *src16 = CONVERT_TO_SHORTPTR(s->y_buffer);
2945 
2946     do {
2947       fwrite(src16, s->y_width, 2, yuv_rec_file);
2948       src16 += s->y_stride;
2949     } while (--h);
2950 
2951     src16 = CONVERT_TO_SHORTPTR(s->u_buffer);
2952     h = s->uv_height;
2953 
2954     do {
2955       fwrite(src16, s->uv_width, 2, yuv_rec_file);
2956       src16 += s->uv_stride;
2957     } while (--h);
2958 
2959     src16 = CONVERT_TO_SHORTPTR(s->v_buffer);
2960     h = s->uv_height;
2961 
2962     do {
2963       fwrite(src16, s->uv_width, 2, yuv_rec_file);
2964       src16 += s->uv_stride;
2965     } while (--h);
2966 
2967     fflush(yuv_rec_file);
2968     return;
2969   }
2970 #endif  // CONFIG_VP9_HIGHBITDEPTH
2971 
2972   do {
2973     fwrite(src, s->y_width, 1, yuv_rec_file);
2974     src += s->y_stride;
2975   } while (--h);
2976 
2977   src = s->u_buffer;
2978   h = s->uv_height;
2979 
2980   do {
2981     fwrite(src, s->uv_width, 1, yuv_rec_file);
2982     src += s->uv_stride;
2983   } while (--h);
2984 
2985   src = s->v_buffer;
2986   h = s->uv_height;
2987 
2988   do {
2989     fwrite(src, s->uv_width, 1, yuv_rec_file);
2990     src += s->uv_stride;
2991   } while (--h);
2992 
2993   fflush(yuv_rec_file);
2994 }
2995 #endif
2996 
2997 #if CONFIG_VP9_HIGHBITDEPTH
scale_and_extend_frame_nonnormative(const YV12_BUFFER_CONFIG * src,YV12_BUFFER_CONFIG * dst,int bd)2998 static void scale_and_extend_frame_nonnormative(const YV12_BUFFER_CONFIG *src,
2999                                                 YV12_BUFFER_CONFIG *dst,
3000                                                 int bd) {
3001 #else
3002 static void scale_and_extend_frame_nonnormative(const YV12_BUFFER_CONFIG *src,
3003                                                 YV12_BUFFER_CONFIG *dst) {
3004 #endif  // CONFIG_VP9_HIGHBITDEPTH
3005   // TODO(dkovalev): replace YV12_BUFFER_CONFIG with vpx_image_t
3006   int i;
3007   const uint8_t *const srcs[3] = { src->y_buffer, src->u_buffer,
3008                                    src->v_buffer };
3009   const int src_strides[3] = { src->y_stride, src->uv_stride, src->uv_stride };
3010   const int src_widths[3] = { src->y_crop_width, src->uv_crop_width,
3011                               src->uv_crop_width };
3012   const int src_heights[3] = { src->y_crop_height, src->uv_crop_height,
3013                                src->uv_crop_height };
3014   uint8_t *const dsts[3] = { dst->y_buffer, dst->u_buffer, dst->v_buffer };
3015   const int dst_strides[3] = { dst->y_stride, dst->uv_stride, dst->uv_stride };
3016   const int dst_widths[3] = { dst->y_crop_width, dst->uv_crop_width,
3017                               dst->uv_crop_width };
3018   const int dst_heights[3] = { dst->y_crop_height, dst->uv_crop_height,
3019                                dst->uv_crop_height };
3020 
3021   for (i = 0; i < MAX_MB_PLANE; ++i) {
3022 #if CONFIG_VP9_HIGHBITDEPTH
3023     if (src->flags & YV12_FLAG_HIGHBITDEPTH) {
3024       vp9_highbd_resize_plane(srcs[i], src_heights[i], src_widths[i],
3025                               src_strides[i], dsts[i], dst_heights[i],
3026                               dst_widths[i], dst_strides[i], bd);
3027     } else {
3028       vp9_resize_plane(srcs[i], src_heights[i], src_widths[i], src_strides[i],
3029                        dsts[i], dst_heights[i], dst_widths[i], dst_strides[i]);
3030     }
3031 #else
3032     vp9_resize_plane(srcs[i], src_heights[i], src_widths[i], src_strides[i],
3033                      dsts[i], dst_heights[i], dst_widths[i], dst_strides[i]);
3034 #endif  // CONFIG_VP9_HIGHBITDEPTH
3035   }
3036   vpx_extend_frame_borders(dst);
3037 }
3038 
3039 #if CONFIG_VP9_HIGHBITDEPTH
3040 static void scale_and_extend_frame(const YV12_BUFFER_CONFIG *src,
3041                                    YV12_BUFFER_CONFIG *dst, int bd,
3042                                    INTERP_FILTER filter_type,
3043                                    int phase_scaler) {
3044   const int src_w = src->y_crop_width;
3045   const int src_h = src->y_crop_height;
3046   const int dst_w = dst->y_crop_width;
3047   const int dst_h = dst->y_crop_height;
3048   const uint8_t *const srcs[3] = { src->y_buffer, src->u_buffer,
3049                                    src->v_buffer };
3050   const int src_strides[3] = { src->y_stride, src->uv_stride, src->uv_stride };
3051   uint8_t *const dsts[3] = { dst->y_buffer, dst->u_buffer, dst->v_buffer };
3052   const int dst_strides[3] = { dst->y_stride, dst->uv_stride, dst->uv_stride };
3053   const InterpKernel *const kernel = vp9_filter_kernels[filter_type];
3054   int x, y, i;
3055 
3056   for (i = 0; i < MAX_MB_PLANE; ++i) {
3057     const int factor = (i == 0 || i == 3 ? 1 : 2);
3058     const int src_stride = src_strides[i];
3059     const int dst_stride = dst_strides[i];
3060     for (y = 0; y < dst_h; y += 16) {
3061       const int y_q4 = y * (16 / factor) * src_h / dst_h + phase_scaler;
3062       for (x = 0; x < dst_w; x += 16) {
3063         const int x_q4 = x * (16 / factor) * src_w / dst_w + phase_scaler;
3064         const uint8_t *src_ptr = srcs[i] +
3065                                  (y / factor) * src_h / dst_h * src_stride +
3066                                  (x / factor) * src_w / dst_w;
3067         uint8_t *dst_ptr = dsts[i] + (y / factor) * dst_stride + (x / factor);
3068 
3069         if (src->flags & YV12_FLAG_HIGHBITDEPTH) {
3070           vpx_highbd_convolve8(CONVERT_TO_SHORTPTR(src_ptr), src_stride,
3071                                CONVERT_TO_SHORTPTR(dst_ptr), dst_stride, kernel,
3072                                x_q4 & 0xf, 16 * src_w / dst_w, y_q4 & 0xf,
3073                                16 * src_h / dst_h, 16 / factor, 16 / factor,
3074                                bd);
3075         } else {
3076           vpx_scaled_2d(src_ptr, src_stride, dst_ptr, dst_stride, kernel,
3077                         x_q4 & 0xf, 16 * src_w / dst_w, y_q4 & 0xf,
3078                         16 * src_h / dst_h, 16 / factor, 16 / factor);
3079         }
3080       }
3081     }
3082   }
3083 
3084   vpx_extend_frame_borders(dst);
3085 }
3086 #endif  // CONFIG_VP9_HIGHBITDEPTH
3087 
3088 #if !CONFIG_REALTIME_ONLY
3089 static int scale_down(VP9_COMP *cpi, int q) {
3090   RATE_CONTROL *const rc = &cpi->rc;
3091   GF_GROUP *const gf_group = &cpi->twopass.gf_group;
3092   int scale = 0;
3093   assert(frame_is_kf_gf_arf(cpi));
3094 
3095   if (rc->frame_size_selector == UNSCALED &&
3096       q >= rc->rf_level_maxq[gf_group->rf_level[gf_group->index]]) {
3097     const int max_size_thresh =
3098         (int)(rate_thresh_mult[SCALE_STEP1] *
3099               VPXMAX(rc->this_frame_target, rc->avg_frame_bandwidth));
3100     scale = rc->projected_frame_size > max_size_thresh ? 1 : 0;
3101   }
3102   return scale;
3103 }
3104 
3105 static int big_rate_miss_high_threshold(VP9_COMP *cpi) {
3106   const RATE_CONTROL *const rc = &cpi->rc;
3107   int big_miss_high;
3108 
3109   if (frame_is_kf_gf_arf(cpi))
3110     big_miss_high = rc->this_frame_target * 3 / 2;
3111   else
3112     big_miss_high = rc->this_frame_target * 2;
3113 
3114   return big_miss_high;
3115 }
3116 
3117 static int big_rate_miss(VP9_COMP *cpi) {
3118   const RATE_CONTROL *const rc = &cpi->rc;
3119   int big_miss_high;
3120   int big_miss_low;
3121 
3122   // Ignore for overlay frames
3123   if (rc->is_src_frame_alt_ref) {
3124     return 0;
3125   } else {
3126     big_miss_low = (rc->this_frame_target / 2);
3127     big_miss_high = big_rate_miss_high_threshold(cpi);
3128 
3129     return (rc->projected_frame_size > big_miss_high) ||
3130            (rc->projected_frame_size < big_miss_low);
3131   }
3132 }
3133 
3134 // test in two pass for the first
3135 static int two_pass_first_group_inter(VP9_COMP *cpi) {
3136   if (cpi->oxcf.pass == 2) {
3137     TWO_PASS *const twopass = &cpi->twopass;
3138     GF_GROUP *const gf_group = &twopass->gf_group;
3139     const int gfg_index = gf_group->index;
3140 
3141     if (gfg_index == 0) return gf_group->update_type[gfg_index] == LF_UPDATE;
3142     return gf_group->update_type[gfg_index - 1] != LF_UPDATE &&
3143            gf_group->update_type[gfg_index] == LF_UPDATE;
3144   } else {
3145     return 0;
3146   }
3147 }
3148 
3149 // Function to test for conditions that indicate we should loop
3150 // back and recode a frame.
3151 static int recode_loop_test(VP9_COMP *cpi, int high_limit, int low_limit, int q,
3152                             int maxq, int minq) {
3153   const RATE_CONTROL *const rc = &cpi->rc;
3154   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
3155   const int frame_is_kfgfarf = frame_is_kf_gf_arf(cpi);
3156   int force_recode = 0;
3157 
3158   if ((rc->projected_frame_size >= rc->max_frame_bandwidth) ||
3159       big_rate_miss(cpi) || (cpi->sf.recode_loop == ALLOW_RECODE) ||
3160       (two_pass_first_group_inter(cpi) &&
3161        (cpi->sf.recode_loop == ALLOW_RECODE_FIRST)) ||
3162       (frame_is_kfgfarf && (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF))) {
3163     if (frame_is_kfgfarf && (oxcf->resize_mode == RESIZE_DYNAMIC) &&
3164         scale_down(cpi, q)) {
3165       // Code this group at a lower resolution.
3166       cpi->resize_pending = 1;
3167       return 1;
3168     }
3169 
3170     // Force recode for extreme overshoot.
3171     if ((rc->projected_frame_size >= rc->max_frame_bandwidth) ||
3172         (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF &&
3173          rc->projected_frame_size >= big_rate_miss_high_threshold(cpi))) {
3174       return 1;
3175     }
3176 
3177     // TODO(agrange) high_limit could be greater than the scale-down threshold.
3178     if ((rc->projected_frame_size > high_limit && q < maxq) ||
3179         (rc->projected_frame_size < low_limit && q > minq)) {
3180       force_recode = 1;
3181     } else if (cpi->oxcf.rc_mode == VPX_CQ) {
3182       // Deal with frame undershoot and whether or not we are
3183       // below the automatically set cq level.
3184       if (q > oxcf->cq_level &&
3185           rc->projected_frame_size < ((rc->this_frame_target * 7) >> 3)) {
3186         force_recode = 1;
3187       }
3188     }
3189   }
3190   return force_recode;
3191 }
3192 #endif  // !CONFIG_REALTIME_ONLY
3193 
3194 static void update_ref_frames(VP9_COMP *cpi) {
3195   VP9_COMMON *const cm = &cpi->common;
3196   BufferPool *const pool = cm->buffer_pool;
3197   GF_GROUP *const gf_group = &cpi->twopass.gf_group;
3198 
3199   if (cpi->rc.show_arf_as_gld) {
3200     int tmp = cpi->alt_fb_idx;
3201     cpi->alt_fb_idx = cpi->gld_fb_idx;
3202     cpi->gld_fb_idx = tmp;
3203   } else if (cm->show_existing_frame) {
3204     // Pop ARF.
3205     cpi->lst_fb_idx = cpi->alt_fb_idx;
3206     cpi->alt_fb_idx =
3207         stack_pop(gf_group->arf_index_stack, gf_group->stack_size);
3208     --gf_group->stack_size;
3209   }
3210 
3211   // At this point the new frame has been encoded.
3212   // If any buffer copy / swapping is signaled it should be done here.
3213   if (cm->frame_type == KEY_FRAME) {
3214     ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->gld_fb_idx],
3215                cm->new_fb_idx);
3216     ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->alt_fb_idx],
3217                cm->new_fb_idx);
3218   } else if (vp9_preserve_existing_gf(cpi)) {
3219     // We have decided to preserve the previously existing golden frame as our
3220     // new ARF frame. However, in the short term in function
3221     // vp9_get_refresh_mask() we left it in the GF slot and, if
3222     // we're updating the GF with the current decoded frame, we save it to the
3223     // ARF slot instead.
3224     // We now have to update the ARF with the current frame and swap gld_fb_idx
3225     // and alt_fb_idx so that, overall, we've stored the old GF in the new ARF
3226     // slot and, if we're updating the GF, the current frame becomes the new GF.
3227     int tmp;
3228 
3229     ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->alt_fb_idx],
3230                cm->new_fb_idx);
3231 
3232     tmp = cpi->alt_fb_idx;
3233     cpi->alt_fb_idx = cpi->gld_fb_idx;
3234     cpi->gld_fb_idx = tmp;
3235   } else { /* For non key/golden frames */
3236     if (cpi->refresh_alt_ref_frame) {
3237       int arf_idx = gf_group->top_arf_idx;
3238 
3239       // Push new ARF into stack.
3240       stack_push(gf_group->arf_index_stack, cpi->alt_fb_idx,
3241                  gf_group->stack_size);
3242       ++gf_group->stack_size;
3243 
3244       assert(arf_idx < REF_FRAMES);
3245 
3246       ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[arf_idx], cm->new_fb_idx);
3247       memcpy(cpi->interp_filter_selected[ALTREF_FRAME],
3248              cpi->interp_filter_selected[0],
3249              sizeof(cpi->interp_filter_selected[0]));
3250 
3251       cpi->alt_fb_idx = arf_idx;
3252     }
3253 
3254     if (cpi->refresh_golden_frame) {
3255       ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->gld_fb_idx],
3256                  cm->new_fb_idx);
3257       if (!cpi->rc.is_src_frame_alt_ref)
3258         memcpy(cpi->interp_filter_selected[GOLDEN_FRAME],
3259                cpi->interp_filter_selected[0],
3260                sizeof(cpi->interp_filter_selected[0]));
3261       else
3262         memcpy(cpi->interp_filter_selected[GOLDEN_FRAME],
3263                cpi->interp_filter_selected[ALTREF_FRAME],
3264                sizeof(cpi->interp_filter_selected[ALTREF_FRAME]));
3265     }
3266   }
3267 
3268   if (cpi->refresh_last_frame) {
3269     ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->lst_fb_idx],
3270                cm->new_fb_idx);
3271     if (!cpi->rc.is_src_frame_alt_ref)
3272       memcpy(cpi->interp_filter_selected[LAST_FRAME],
3273              cpi->interp_filter_selected[0],
3274              sizeof(cpi->interp_filter_selected[0]));
3275   }
3276 
3277   if (gf_group->update_type[gf_group->index] == MID_OVERLAY_UPDATE) {
3278     cpi->alt_fb_idx =
3279         stack_pop(gf_group->arf_index_stack, gf_group->stack_size);
3280     --gf_group->stack_size;
3281   }
3282 }
3283 
3284 void vp9_update_reference_frames(VP9_COMP *cpi) {
3285   update_ref_frames(cpi);
3286 
3287 #if CONFIG_VP9_TEMPORAL_DENOISING
3288   vp9_denoiser_update_ref_frame(cpi);
3289 #endif
3290 
3291   if (is_one_pass_cbr_svc(cpi)) vp9_svc_update_ref_frame(cpi);
3292 }
3293 
3294 static void loopfilter_frame(VP9_COMP *cpi, VP9_COMMON *cm) {
3295   MACROBLOCKD *xd = &cpi->td.mb.e_mbd;
3296   struct loopfilter *lf = &cm->lf;
3297   int is_reference_frame =
3298       (cm->frame_type == KEY_FRAME || cpi->refresh_last_frame ||
3299        cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame);
3300   if (cpi->use_svc &&
3301       cpi->svc.temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_BYPASS)
3302     is_reference_frame = !cpi->svc.non_reference_frame;
3303 
3304   // Skip loop filter in show_existing_frame mode.
3305   if (cm->show_existing_frame) {
3306     lf->filter_level = 0;
3307     return;
3308   }
3309 
3310   if (cpi->loopfilter_ctrl == NO_LOOPFILTER ||
3311       (!is_reference_frame && cpi->loopfilter_ctrl == LOOPFILTER_REFERENCE)) {
3312     lf->filter_level = 0;
3313     vpx_extend_frame_inner_borders(cm->frame_to_show);
3314     return;
3315   }
3316 
3317   if (xd->lossless) {
3318     lf->filter_level = 0;
3319     lf->last_filt_level = 0;
3320   } else {
3321     struct vpx_usec_timer timer;
3322 
3323     vpx_clear_system_state();
3324 
3325     vpx_usec_timer_start(&timer);
3326 
3327     if (!cpi->rc.is_src_frame_alt_ref) {
3328       if ((cpi->common.frame_type == KEY_FRAME) &&
3329           (!cpi->rc.this_key_frame_forced)) {
3330         lf->last_filt_level = 0;
3331       }
3332       vp9_pick_filter_level(cpi->Source, cpi, cpi->sf.lpf_pick);
3333       lf->last_filt_level = lf->filter_level;
3334     } else {
3335       lf->filter_level = 0;
3336     }
3337 
3338     vpx_usec_timer_mark(&timer);
3339     cpi->time_pick_lpf += vpx_usec_timer_elapsed(&timer);
3340   }
3341 
3342   if (lf->filter_level > 0 && is_reference_frame) {
3343     vp9_build_mask_frame(cm, lf->filter_level, 0);
3344 
3345     if (cpi->num_workers > 1)
3346       vp9_loop_filter_frame_mt(cm->frame_to_show, cm, xd->plane,
3347                                lf->filter_level, 0, 0, cpi->workers,
3348                                cpi->num_workers, &cpi->lf_row_sync);
3349     else
3350       vp9_loop_filter_frame(cm->frame_to_show, cm, xd, lf->filter_level, 0, 0);
3351   }
3352 
3353   vpx_extend_frame_inner_borders(cm->frame_to_show);
3354 }
3355 
3356 static INLINE void alloc_frame_mvs(VP9_COMMON *const cm, int buffer_idx) {
3357   RefCntBuffer *const new_fb_ptr = &cm->buffer_pool->frame_bufs[buffer_idx];
3358   if (new_fb_ptr->mvs == NULL || new_fb_ptr->mi_rows < cm->mi_rows ||
3359       new_fb_ptr->mi_cols < cm->mi_cols) {
3360     vpx_free(new_fb_ptr->mvs);
3361     CHECK_MEM_ERROR(cm, new_fb_ptr->mvs,
3362                     (MV_REF *)vpx_calloc(cm->mi_rows * cm->mi_cols,
3363                                          sizeof(*new_fb_ptr->mvs)));
3364     new_fb_ptr->mi_rows = cm->mi_rows;
3365     new_fb_ptr->mi_cols = cm->mi_cols;
3366   }
3367 }
3368 
3369 void vp9_scale_references(VP9_COMP *cpi) {
3370   VP9_COMMON *cm = &cpi->common;
3371   MV_REFERENCE_FRAME ref_frame;
3372   const VP9_REFFRAME ref_mask[3] = { VP9_LAST_FLAG, VP9_GOLD_FLAG,
3373                                      VP9_ALT_FLAG };
3374 
3375   for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
3376     // Need to convert from VP9_REFFRAME to index into ref_mask (subtract 1).
3377     if (cpi->ref_frame_flags & ref_mask[ref_frame - 1]) {
3378       BufferPool *const pool = cm->buffer_pool;
3379       const YV12_BUFFER_CONFIG *const ref =
3380           get_ref_frame_buffer(cpi, ref_frame);
3381 
3382       if (ref == NULL) {
3383         cpi->scaled_ref_idx[ref_frame - 1] = INVALID_IDX;
3384         continue;
3385       }
3386 
3387 #if CONFIG_VP9_HIGHBITDEPTH
3388       if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height) {
3389         RefCntBuffer *new_fb_ptr = NULL;
3390         int force_scaling = 0;
3391         int new_fb = cpi->scaled_ref_idx[ref_frame - 1];
3392         if (new_fb == INVALID_IDX) {
3393           new_fb = get_free_fb(cm);
3394           force_scaling = 1;
3395         }
3396         if (new_fb == INVALID_IDX) return;
3397         new_fb_ptr = &pool->frame_bufs[new_fb];
3398         if (force_scaling || new_fb_ptr->buf.y_crop_width != cm->width ||
3399             new_fb_ptr->buf.y_crop_height != cm->height) {
3400           if (vpx_realloc_frame_buffer(&new_fb_ptr->buf, cm->width, cm->height,
3401                                        cm->subsampling_x, cm->subsampling_y,
3402                                        cm->use_highbitdepth,
3403                                        VP9_ENC_BORDER_IN_PIXELS,
3404                                        cm->byte_alignment, NULL, NULL, NULL))
3405             vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
3406                                "Failed to allocate frame buffer");
3407           scale_and_extend_frame(ref, &new_fb_ptr->buf, (int)cm->bit_depth,
3408                                  EIGHTTAP, 0);
3409           cpi->scaled_ref_idx[ref_frame - 1] = new_fb;
3410           alloc_frame_mvs(cm, new_fb);
3411         }
3412 #else
3413       if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height) {
3414         RefCntBuffer *new_fb_ptr = NULL;
3415         int force_scaling = 0;
3416         int new_fb = cpi->scaled_ref_idx[ref_frame - 1];
3417         if (new_fb == INVALID_IDX) {
3418           new_fb = get_free_fb(cm);
3419           force_scaling = 1;
3420         }
3421         if (new_fb == INVALID_IDX) return;
3422         new_fb_ptr = &pool->frame_bufs[new_fb];
3423         if (force_scaling || new_fb_ptr->buf.y_crop_width != cm->width ||
3424             new_fb_ptr->buf.y_crop_height != cm->height) {
3425           if (vpx_realloc_frame_buffer(&new_fb_ptr->buf, cm->width, cm->height,
3426                                        cm->subsampling_x, cm->subsampling_y,
3427                                        VP9_ENC_BORDER_IN_PIXELS,
3428                                        cm->byte_alignment, NULL, NULL, NULL))
3429             vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
3430                                "Failed to allocate frame buffer");
3431           vp9_scale_and_extend_frame(ref, &new_fb_ptr->buf, EIGHTTAP, 0);
3432           cpi->scaled_ref_idx[ref_frame - 1] = new_fb;
3433           alloc_frame_mvs(cm, new_fb);
3434         }
3435 #endif  // CONFIG_VP9_HIGHBITDEPTH
3436       } else {
3437         int buf_idx;
3438         RefCntBuffer *buf = NULL;
3439         if (cpi->oxcf.pass == 0 && !cpi->use_svc) {
3440           // Check for release of scaled reference.
3441           buf_idx = cpi->scaled_ref_idx[ref_frame - 1];
3442           if (buf_idx != INVALID_IDX) {
3443             buf = &pool->frame_bufs[buf_idx];
3444             --buf->ref_count;
3445             cpi->scaled_ref_idx[ref_frame - 1] = INVALID_IDX;
3446           }
3447         }
3448         buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
3449         buf = &pool->frame_bufs[buf_idx];
3450         buf->buf.y_crop_width = ref->y_crop_width;
3451         buf->buf.y_crop_height = ref->y_crop_height;
3452         cpi->scaled_ref_idx[ref_frame - 1] = buf_idx;
3453         ++buf->ref_count;
3454       }
3455     } else {
3456       if (cpi->oxcf.pass != 0 || cpi->use_svc)
3457         cpi->scaled_ref_idx[ref_frame - 1] = INVALID_IDX;
3458     }
3459   }
3460 }
3461 
3462 static void release_scaled_references(VP9_COMP *cpi) {
3463   VP9_COMMON *cm = &cpi->common;
3464   int i;
3465   if (cpi->oxcf.pass == 0 && !cpi->use_svc) {
3466     // Only release scaled references under certain conditions:
3467     // if reference will be updated, or if scaled reference has same resolution.
3468     int refresh[3];
3469     refresh[0] = (cpi->refresh_last_frame) ? 1 : 0;
3470     refresh[1] = (cpi->refresh_golden_frame) ? 1 : 0;
3471     refresh[2] = (cpi->refresh_alt_ref_frame) ? 1 : 0;
3472     for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
3473       const int idx = cpi->scaled_ref_idx[i - 1];
3474       if (idx != INVALID_IDX) {
3475         RefCntBuffer *const buf = &cm->buffer_pool->frame_bufs[idx];
3476         const YV12_BUFFER_CONFIG *const ref = get_ref_frame_buffer(cpi, i);
3477         if (refresh[i - 1] || (buf->buf.y_crop_width == ref->y_crop_width &&
3478                                buf->buf.y_crop_height == ref->y_crop_height)) {
3479           --buf->ref_count;
3480           cpi->scaled_ref_idx[i - 1] = INVALID_IDX;
3481         }
3482       }
3483     }
3484   } else {
3485     for (i = 0; i < REFS_PER_FRAME; ++i) {
3486       const int idx = cpi->scaled_ref_idx[i];
3487       if (idx != INVALID_IDX) {
3488         RefCntBuffer *const buf = &cm->buffer_pool->frame_bufs[idx];
3489         --buf->ref_count;
3490         cpi->scaled_ref_idx[i] = INVALID_IDX;
3491       }
3492     }
3493   }
3494 }
3495 
3496 static void full_to_model_count(unsigned int *model_count,
3497                                 unsigned int *full_count) {
3498   int n;
3499   model_count[ZERO_TOKEN] = full_count[ZERO_TOKEN];
3500   model_count[ONE_TOKEN] = full_count[ONE_TOKEN];
3501   model_count[TWO_TOKEN] = full_count[TWO_TOKEN];
3502   for (n = THREE_TOKEN; n < EOB_TOKEN; ++n)
3503     model_count[TWO_TOKEN] += full_count[n];
3504   model_count[EOB_MODEL_TOKEN] = full_count[EOB_TOKEN];
3505 }
3506 
3507 static void full_to_model_counts(vp9_coeff_count_model *model_count,
3508                                  vp9_coeff_count *full_count) {
3509   int i, j, k, l;
3510 
3511   for (i = 0; i < PLANE_TYPES; ++i)
3512     for (j = 0; j < REF_TYPES; ++j)
3513       for (k = 0; k < COEF_BANDS; ++k)
3514         for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l)
3515           full_to_model_count(model_count[i][j][k][l], full_count[i][j][k][l]);
3516 }
3517 
3518 #if 0 && CONFIG_INTERNAL_STATS
3519 static void output_frame_level_debug_stats(VP9_COMP *cpi) {
3520   VP9_COMMON *const cm = &cpi->common;
3521   FILE *const f = fopen("tmp.stt", cm->current_video_frame ? "a" : "w");
3522   int64_t recon_err;
3523 
3524   vpx_clear_system_state();
3525 
3526 #if CONFIG_VP9_HIGHBITDEPTH
3527   if (cm->use_highbitdepth) {
3528     recon_err = vpx_highbd_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
3529   } else {
3530     recon_err = vpx_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
3531   }
3532 #else
3533   recon_err = vpx_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
3534 #endif  // CONFIG_VP9_HIGHBITDEPTH
3535 
3536 
3537   if (cpi->twopass.total_left_stats.coded_error != 0.0) {
3538     double dc_quant_devisor;
3539 #if CONFIG_VP9_HIGHBITDEPTH
3540     switch (cm->bit_depth) {
3541       case VPX_BITS_8:
3542         dc_quant_devisor = 4.0;
3543         break;
3544       case VPX_BITS_10:
3545         dc_quant_devisor = 16.0;
3546         break;
3547       default:
3548         assert(cm->bit_depth == VPX_BITS_12);
3549         dc_quant_devisor = 64.0;
3550         break;
3551     }
3552 #else
3553     dc_quant_devisor = 4.0;
3554 #endif
3555 
3556     if (!cm->current_video_frame) {
3557       fprintf(f, "frame, width, height, last ts, last end ts, "
3558           "source_alt_ref_pending, source_alt_ref_active, "
3559           "this_frame_target, projected_frame_size, "
3560           "projected_frame_size / MBs, "
3561           "projected_frame_size - this_frame_target, "
3562           "vbr_bits_off_target, vbr_bits_off_target_fast, "
3563           "twopass.extend_minq, twopass.extend_minq_fast, "
3564           "total_target_vs_actual, "
3565           "starting_buffer_level - bits_off_target, "
3566           "total_actual_bits, base_qindex, q for base_qindex, "
3567           "dc quant, q for active_worst_quality, avg_q, q for oxcf.cq_level, "
3568           "refresh_last_frame, refresh_golden_frame, refresh_alt_ref_frame, "
3569           "frame_type, gfu_boost, "
3570           "twopass.bits_left, "
3571           "twopass.total_left_stats.coded_error, "
3572           "twopass.bits_left / (1 + twopass.total_left_stats.coded_error), "
3573           "tot_recode_hits, recon_err, kf_boost, "
3574           "twopass.kf_zeromotion_pct, twopass.fr_content_type, "
3575           "filter_level, seg.aq_av_offset\n");
3576     }
3577 
3578     fprintf(f, "%10u, %d, %d, %10"PRId64", %10"PRId64", %d, %d, %10d, %10d, "
3579         "%10d, %10d, %10"PRId64", %10"PRId64", %5d, %5d, %10"PRId64", "
3580         "%10"PRId64", %10"PRId64", %10d, %7.2lf, %7.2lf, %7.2lf, %7.2lf, "
3581         "%7.2lf, %6d, %6d, %5d, %5d, %5d, %10"PRId64", %10.3lf, %10lf, %8u, "
3582         "%10"PRId64", %10d, %10d, %10d, %10d, %10d\n",
3583         cpi->common.current_video_frame,
3584         cm->width, cm->height,
3585         cpi->last_time_stamp_seen,
3586         cpi->last_end_time_stamp_seen,
3587         cpi->rc.source_alt_ref_pending,
3588         cpi->rc.source_alt_ref_active,
3589         cpi->rc.this_frame_target,
3590         cpi->rc.projected_frame_size,
3591         cpi->rc.projected_frame_size / cpi->common.MBs,
3592         (cpi->rc.projected_frame_size - cpi->rc.this_frame_target),
3593         cpi->rc.vbr_bits_off_target,
3594         cpi->rc.vbr_bits_off_target_fast,
3595         cpi->twopass.extend_minq,
3596         cpi->twopass.extend_minq_fast,
3597         cpi->rc.total_target_vs_actual,
3598         (cpi->rc.starting_buffer_level - cpi->rc.bits_off_target),
3599         cpi->rc.total_actual_bits, cm->base_qindex,
3600         vp9_convert_qindex_to_q(cm->base_qindex, cm->bit_depth),
3601         (double)vp9_dc_quant(cm->base_qindex, 0, cm->bit_depth) /
3602             dc_quant_devisor,
3603         vp9_convert_qindex_to_q(cpi->twopass.active_worst_quality,
3604                                 cm->bit_depth),
3605         cpi->rc.avg_q,
3606         vp9_convert_qindex_to_q(cpi->oxcf.cq_level, cm->bit_depth),
3607         cpi->refresh_last_frame, cpi->refresh_golden_frame,
3608         cpi->refresh_alt_ref_frame, cm->frame_type, cpi->rc.gfu_boost,
3609         cpi->twopass.bits_left,
3610         cpi->twopass.total_left_stats.coded_error,
3611         cpi->twopass.bits_left /
3612             (1 + cpi->twopass.total_left_stats.coded_error),
3613         cpi->tot_recode_hits, recon_err, cpi->rc.kf_boost,
3614         cpi->twopass.kf_zeromotion_pct,
3615         cpi->twopass.fr_content_type,
3616         cm->lf.filter_level,
3617         cm->seg.aq_av_offset);
3618   }
3619   fclose(f);
3620 
3621   if (0) {
3622     FILE *const fmodes = fopen("Modes.stt", "a");
3623     int i;
3624 
3625     fprintf(fmodes, "%6d:%1d:%1d:%1d ", cpi->common.current_video_frame,
3626             cm->frame_type, cpi->refresh_golden_frame,
3627             cpi->refresh_alt_ref_frame);
3628 
3629     for (i = 0; i < MAX_MODES; ++i)
3630       fprintf(fmodes, "%5d ", cpi->mode_chosen_counts[i]);
3631 
3632     fprintf(fmodes, "\n");
3633 
3634     fclose(fmodes);
3635   }
3636 }
3637 #endif
3638 
3639 static void set_mv_search_params(VP9_COMP *cpi) {
3640   const VP9_COMMON *const cm = &cpi->common;
3641   const unsigned int max_mv_def = VPXMIN(cm->width, cm->height);
3642 
3643   // Default based on max resolution.
3644   cpi->mv_step_param = vp9_init_search_range(max_mv_def);
3645 
3646   if (cpi->sf.mv.auto_mv_step_size) {
3647     if (frame_is_intra_only(cm)) {
3648       // Initialize max_mv_magnitude for use in the first INTER frame
3649       // after a key/intra-only frame.
3650       cpi->max_mv_magnitude = max_mv_def;
3651     } else {
3652       if (cm->show_frame) {
3653         // Allow mv_steps to correspond to twice the max mv magnitude found
3654         // in the previous frame, capped by the default max_mv_magnitude based
3655         // on resolution.
3656         cpi->mv_step_param = vp9_init_search_range(
3657             VPXMIN(max_mv_def, 2 * cpi->max_mv_magnitude));
3658       }
3659       cpi->max_mv_magnitude = 0;
3660     }
3661   }
3662 }
3663 
3664 static void set_size_independent_vars(VP9_COMP *cpi) {
3665   vp9_set_speed_features_framesize_independent(cpi, cpi->oxcf.speed);
3666   vp9_set_rd_speed_thresholds(cpi);
3667   vp9_set_rd_speed_thresholds_sub8x8(cpi);
3668   cpi->common.interp_filter = cpi->sf.default_interp_filter;
3669 }
3670 
3671 static void set_size_dependent_vars(VP9_COMP *cpi, int *q, int *bottom_index,
3672                                     int *top_index) {
3673   VP9_COMMON *const cm = &cpi->common;
3674 
3675   // Setup variables that depend on the dimensions of the frame.
3676   vp9_set_speed_features_framesize_dependent(cpi, cpi->oxcf.speed);
3677 
3678   // Decide q and q bounds.
3679   *q = vp9_rc_pick_q_and_bounds(cpi, bottom_index, top_index);
3680 
3681   if (cpi->oxcf.rc_mode == VPX_CBR && cpi->rc.force_max_q) {
3682     *q = cpi->rc.worst_quality;
3683     cpi->rc.force_max_q = 0;
3684   }
3685 
3686   if (cpi->use_svc) {
3687     cpi->svc.base_qindex[cpi->svc.spatial_layer_id] = *q;
3688   }
3689 
3690   if (!frame_is_intra_only(cm)) {
3691     vp9_set_high_precision_mv(cpi, (*q) < HIGH_PRECISION_MV_QTHRESH);
3692   }
3693 
3694 #if !CONFIG_REALTIME_ONLY
3695   // Configure experimental use of segmentation for enhanced coding of
3696   // static regions if indicated.
3697   // Only allowed in the second pass of a two pass encode, as it requires
3698   // lagged coding, and if the relevant speed feature flag is set.
3699   if (cpi->oxcf.pass == 2 && cpi->sf.static_segmentation)
3700     configure_static_seg_features(cpi);
3701 #endif  // !CONFIG_REALTIME_ONLY
3702 
3703 #if CONFIG_VP9_POSTPROC && !(CONFIG_VP9_TEMPORAL_DENOISING)
3704   if (cpi->oxcf.noise_sensitivity > 0) {
3705     int l = 0;
3706     switch (cpi->oxcf.noise_sensitivity) {
3707       case 1: l = 20; break;
3708       case 2: l = 40; break;
3709       case 3: l = 60; break;
3710       case 4:
3711       case 5: l = 100; break;
3712       case 6: l = 150; break;
3713     }
3714     if (!cpi->common.postproc_state.limits) {
3715       cpi->common.postproc_state.limits =
3716           vpx_calloc(cpi->un_scaled_source->y_width,
3717                      sizeof(*cpi->common.postproc_state.limits));
3718     }
3719     vp9_denoise(&cpi->common, cpi->Source, cpi->Source, l,
3720                 cpi->common.postproc_state.limits);
3721   }
3722 #endif  // CONFIG_VP9_POSTPROC
3723 }
3724 
3725 static void init_motion_estimation(VP9_COMP *cpi) {
3726   int y_stride = cpi->scaled_source.y_stride;
3727 
3728   if (cpi->sf.mv.search_method == NSTEP) {
3729     vp9_init3smotion_compensation(&cpi->ss_cfg, y_stride);
3730   } else if (cpi->sf.mv.search_method == DIAMOND) {
3731     vp9_init_dsmotion_compensation(&cpi->ss_cfg, y_stride);
3732   }
3733 }
3734 
3735 static void set_frame_size(VP9_COMP *cpi) {
3736   int ref_frame;
3737   VP9_COMMON *const cm = &cpi->common;
3738   VP9EncoderConfig *const oxcf = &cpi->oxcf;
3739   MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
3740 
3741 #if !CONFIG_REALTIME_ONLY
3742   if (oxcf->pass == 2 && oxcf->rc_mode == VPX_VBR &&
3743       ((oxcf->resize_mode == RESIZE_FIXED && cm->current_video_frame == 0) ||
3744        (oxcf->resize_mode == RESIZE_DYNAMIC && cpi->resize_pending))) {
3745     calculate_coded_size(cpi, &oxcf->scaled_frame_width,
3746                          &oxcf->scaled_frame_height);
3747 
3748     // There has been a change in frame size.
3749     vp9_set_size_literal(cpi, oxcf->scaled_frame_width,
3750                          oxcf->scaled_frame_height);
3751   }
3752 #endif  // !CONFIG_REALTIME_ONLY
3753 
3754   if (oxcf->pass == 0 && oxcf->rc_mode == VPX_CBR &&
3755       oxcf->resize_mode == RESIZE_DYNAMIC && cpi->resize_pending != 0) {
3756     // For SVC scaled width/height will have been set (svc->resize_set=1)
3757     // in get_svc_params based on the layer width/height.
3758     if (!cpi->use_svc || !cpi->svc.resize_set) {
3759       oxcf->scaled_frame_width =
3760           (oxcf->width * cpi->resize_scale_num) / cpi->resize_scale_den;
3761       oxcf->scaled_frame_height =
3762           (oxcf->height * cpi->resize_scale_num) / cpi->resize_scale_den;
3763       // There has been a change in frame size.
3764       vp9_set_size_literal(cpi, oxcf->scaled_frame_width,
3765                            oxcf->scaled_frame_height);
3766     }
3767 
3768     // TODO(agrange) Scale cpi->max_mv_magnitude if frame-size has changed.
3769     set_mv_search_params(cpi);
3770 
3771     vp9_noise_estimate_init(&cpi->noise_estimate, cm->width, cm->height);
3772 #if CONFIG_VP9_TEMPORAL_DENOISING
3773     // Reset the denoiser on the resized frame.
3774     if (cpi->oxcf.noise_sensitivity > 0) {
3775       vp9_denoiser_free(&(cpi->denoiser));
3776       setup_denoiser_buffer(cpi);
3777       // Dynamic resize is only triggered for non-SVC, so we can force
3778       // golden frame update here as temporary fix to denoiser.
3779       cpi->refresh_golden_frame = 1;
3780     }
3781 #endif
3782   }
3783 
3784   if ((oxcf->pass == 2) && !cpi->use_svc) {
3785     vp9_set_target_rate(cpi);
3786   }
3787 
3788   alloc_frame_mvs(cm, cm->new_fb_idx);
3789 
3790   // Reset the frame pointers to the current frame size.
3791   if (vpx_realloc_frame_buffer(get_frame_new_buffer(cm), cm->width, cm->height,
3792                                cm->subsampling_x, cm->subsampling_y,
3793 #if CONFIG_VP9_HIGHBITDEPTH
3794                                cm->use_highbitdepth,
3795 #endif
3796                                VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment,
3797                                NULL, NULL, NULL))
3798     vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
3799                        "Failed to allocate frame buffer");
3800 
3801   alloc_util_frame_buffers(cpi);
3802   init_motion_estimation(cpi);
3803 
3804   for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
3805     RefBuffer *const ref_buf = &cm->frame_refs[ref_frame - 1];
3806     const int buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
3807 
3808     ref_buf->idx = buf_idx;
3809 
3810     if (buf_idx != INVALID_IDX) {
3811       YV12_BUFFER_CONFIG *const buf = &cm->buffer_pool->frame_bufs[buf_idx].buf;
3812       ref_buf->buf = buf;
3813 #if CONFIG_VP9_HIGHBITDEPTH
3814       vp9_setup_scale_factors_for_frame(
3815           &ref_buf->sf, buf->y_crop_width, buf->y_crop_height, cm->width,
3816           cm->height, (buf->flags & YV12_FLAG_HIGHBITDEPTH) ? 1 : 0);
3817 #else
3818       vp9_setup_scale_factors_for_frame(&ref_buf->sf, buf->y_crop_width,
3819                                         buf->y_crop_height, cm->width,
3820                                         cm->height);
3821 #endif  // CONFIG_VP9_HIGHBITDEPTH
3822       if (vp9_is_scaled(&ref_buf->sf)) vpx_extend_frame_borders(buf);
3823     } else {
3824       ref_buf->buf = NULL;
3825     }
3826   }
3827 
3828   set_ref_ptrs(cm, xd, LAST_FRAME, LAST_FRAME);
3829 }
3830 
3831 #if CONFIG_CONSISTENT_RECODE || CONFIG_RATE_CTRL
3832 static void save_encode_params(VP9_COMP *cpi) {
3833   VP9_COMMON *const cm = &cpi->common;
3834   const int tile_cols = 1 << cm->log2_tile_cols;
3835   const int tile_rows = 1 << cm->log2_tile_rows;
3836   int tile_col, tile_row;
3837   int i, j;
3838   RD_OPT *rd_opt = &cpi->rd;
3839   for (i = 0; i < MAX_REF_FRAMES; i++) {
3840     for (j = 0; j < REFERENCE_MODES; j++)
3841       rd_opt->prediction_type_threshes_prev[i][j] =
3842           rd_opt->prediction_type_threshes[i][j];
3843 
3844     for (j = 0; j < SWITCHABLE_FILTER_CONTEXTS; j++)
3845       rd_opt->filter_threshes_prev[i][j] = rd_opt->filter_threshes[i][j];
3846   }
3847 
3848   if (cpi->tile_data != NULL) {
3849     for (tile_row = 0; tile_row < tile_rows; ++tile_row)
3850       for (tile_col = 0; tile_col < tile_cols; ++tile_col) {
3851         TileDataEnc *tile_data =
3852             &cpi->tile_data[tile_row * tile_cols + tile_col];
3853         for (i = 0; i < BLOCK_SIZES; ++i) {
3854           for (j = 0; j < MAX_MODES; ++j) {
3855             tile_data->thresh_freq_fact_prev[i][j] =
3856                 tile_data->thresh_freq_fact[i][j];
3857           }
3858         }
3859       }
3860   }
3861 }
3862 #endif  // CONFIG_CONSISTENT_RECODE || CONFIG_RATE_CTRL
3863 
3864 static INLINE void set_raw_source_frame(VP9_COMP *cpi) {
3865 #ifdef ENABLE_KF_DENOISE
3866   if (is_spatial_denoise_enabled(cpi)) {
3867     cpi->raw_source_frame = vp9_scale_if_required(
3868         cm, &cpi->raw_unscaled_source, &cpi->raw_scaled_source,
3869         (oxcf->pass == 0), EIGHTTAP, 0);
3870   } else {
3871     cpi->raw_source_frame = cpi->Source;
3872   }
3873 #else
3874   cpi->raw_source_frame = cpi->Source;
3875 #endif
3876 }
3877 
3878 static int encode_without_recode_loop(VP9_COMP *cpi, size_t *size,
3879                                       uint8_t *dest) {
3880   VP9_COMMON *const cm = &cpi->common;
3881   SVC *const svc = &cpi->svc;
3882   int q = 0, bottom_index = 0, top_index = 0;
3883   int no_drop_scene_change = 0;
3884   const INTERP_FILTER filter_scaler =
3885       (is_one_pass_cbr_svc(cpi))
3886           ? svc->downsample_filter_type[svc->spatial_layer_id]
3887           : EIGHTTAP;
3888   const int phase_scaler =
3889       (is_one_pass_cbr_svc(cpi))
3890           ? svc->downsample_filter_phase[svc->spatial_layer_id]
3891           : 0;
3892 
3893   if (cm->show_existing_frame) {
3894     cpi->rc.this_frame_target = 0;
3895     if (is_psnr_calc_enabled(cpi)) set_raw_source_frame(cpi);
3896     return 1;
3897   }
3898 
3899   svc->time_stamp_prev[svc->spatial_layer_id] = svc->time_stamp_superframe;
3900 
3901   // Flag to check if its valid to compute the source sad (used for
3902   // scene detection and for superblock content state in CBR mode).
3903   // The flag may get reset below based on SVC or resizing state.
3904   cpi->compute_source_sad_onepass = cpi->oxcf.mode == REALTIME;
3905 
3906   vpx_clear_system_state();
3907 
3908   set_frame_size(cpi);
3909 
3910   if (is_one_pass_cbr_svc(cpi) &&
3911       cpi->un_scaled_source->y_width == cm->width << 2 &&
3912       cpi->un_scaled_source->y_height == cm->height << 2 &&
3913       svc->scaled_temp.y_width == cm->width << 1 &&
3914       svc->scaled_temp.y_height == cm->height << 1) {
3915     // For svc, if it is a 1/4x1/4 downscaling, do a two-stage scaling to take
3916     // advantage of the 1:2 optimized scaler. In the process, the 1/2x1/2
3917     // result will be saved in scaled_temp and might be used later.
3918     const INTERP_FILTER filter_scaler2 = svc->downsample_filter_type[1];
3919     const int phase_scaler2 = svc->downsample_filter_phase[1];
3920     cpi->Source = vp9_svc_twostage_scale(
3921         cm, cpi->un_scaled_source, &cpi->scaled_source, &svc->scaled_temp,
3922         filter_scaler, phase_scaler, filter_scaler2, phase_scaler2);
3923     svc->scaled_one_half = 1;
3924   } else if (is_one_pass_cbr_svc(cpi) &&
3925              cpi->un_scaled_source->y_width == cm->width << 1 &&
3926              cpi->un_scaled_source->y_height == cm->height << 1 &&
3927              svc->scaled_one_half) {
3928     // If the spatial layer is 1/2x1/2 and the scaling is already done in the
3929     // two-stage scaling, use the result directly.
3930     cpi->Source = &svc->scaled_temp;
3931     svc->scaled_one_half = 0;
3932   } else {
3933     cpi->Source = vp9_scale_if_required(
3934         cm, cpi->un_scaled_source, &cpi->scaled_source, (cpi->oxcf.pass == 0),
3935         filter_scaler, phase_scaler);
3936   }
3937 #ifdef OUTPUT_YUV_SVC_SRC
3938   // Write out at most 3 spatial layers.
3939   if (is_one_pass_cbr_svc(cpi) && svc->spatial_layer_id < 3) {
3940     vpx_write_yuv_frame(yuv_svc_src[svc->spatial_layer_id], cpi->Source);
3941   }
3942 #endif
3943   // Unfiltered raw source used in metrics calculation if the source
3944   // has been filtered.
3945   if (is_psnr_calc_enabled(cpi)) {
3946 #ifdef ENABLE_KF_DENOISE
3947     if (is_spatial_denoise_enabled(cpi)) {
3948       cpi->raw_source_frame = vp9_scale_if_required(
3949           cm, &cpi->raw_unscaled_source, &cpi->raw_scaled_source,
3950           (cpi->oxcf.pass == 0), EIGHTTAP, phase_scaler);
3951     } else {
3952       cpi->raw_source_frame = cpi->Source;
3953     }
3954 #else
3955     cpi->raw_source_frame = cpi->Source;
3956 #endif
3957   }
3958 
3959   if ((cpi->use_svc &&
3960        (svc->spatial_layer_id < svc->number_spatial_layers - 1 ||
3961         svc->temporal_layer_id < svc->number_temporal_layers - 1 ||
3962         svc->current_superframe < 1)) ||
3963       cpi->resize_pending || cpi->resize_state || cpi->external_resize ||
3964       cpi->resize_state != ORIG) {
3965     cpi->compute_source_sad_onepass = 0;
3966     if (cpi->content_state_sb_fd != NULL)
3967       memset(cpi->content_state_sb_fd, 0,
3968              (cm->mi_stride >> 3) * ((cm->mi_rows >> 3) + 1) *
3969                  sizeof(*cpi->content_state_sb_fd));
3970   }
3971 
3972   // Avoid scaling last_source unless its needed.
3973   // Last source is needed if avg_source_sad() is used, or if
3974   // partition_search_type == SOURCE_VAR_BASED_PARTITION, or if noise
3975   // estimation is enabled.
3976   if (cpi->unscaled_last_source != NULL &&
3977       (cpi->oxcf.content == VP9E_CONTENT_SCREEN ||
3978        (cpi->oxcf.pass == 0 && cpi->oxcf.rc_mode == VPX_VBR &&
3979         cpi->oxcf.mode == REALTIME && cpi->oxcf.speed >= 5) ||
3980        cpi->sf.partition_search_type == SOURCE_VAR_BASED_PARTITION ||
3981        (cpi->noise_estimate.enabled && !cpi->oxcf.noise_sensitivity) ||
3982        cpi->compute_source_sad_onepass))
3983     cpi->Last_Source = vp9_scale_if_required(
3984         cm, cpi->unscaled_last_source, &cpi->scaled_last_source,
3985         (cpi->oxcf.pass == 0), EIGHTTAP, 0);
3986 
3987   if (cpi->Last_Source == NULL ||
3988       cpi->Last_Source->y_width != cpi->Source->y_width ||
3989       cpi->Last_Source->y_height != cpi->Source->y_height)
3990     cpi->compute_source_sad_onepass = 0;
3991 
3992   if (frame_is_intra_only(cm) || cpi->resize_pending != 0) {
3993     memset(cpi->consec_zero_mv, 0,
3994            cm->mi_rows * cm->mi_cols * sizeof(*cpi->consec_zero_mv));
3995   }
3996 
3997 #if CONFIG_VP9_TEMPORAL_DENOISING
3998   if (cpi->oxcf.noise_sensitivity > 0 && cpi->use_svc)
3999     vp9_denoiser_reset_on_first_frame(cpi);
4000 #endif
4001 
4002   // Scene detection is always used for VBR mode or screen-content case.
4003   // For other cases (e.g., CBR mode) use it for 5 <= speed < 8 for now
4004   // (need to check encoding time cost for doing this for speed 8).
4005   cpi->rc.high_source_sad = 0;
4006   cpi->rc.hybrid_intra_scene_change = 0;
4007   cpi->rc.re_encode_maxq_scene_change = 0;
4008   if (cm->show_frame && cpi->oxcf.mode == REALTIME &&
4009       (cpi->oxcf.rc_mode == VPX_VBR ||
4010        cpi->oxcf.content == VP9E_CONTENT_SCREEN ||
4011        (cpi->oxcf.speed >= 5 && cpi->oxcf.speed < 8)))
4012     vp9_scene_detection_onepass(cpi);
4013 
4014   if (svc->spatial_layer_id == svc->first_spatial_layer_to_encode) {
4015     svc->high_source_sad_superframe = cpi->rc.high_source_sad;
4016     svc->high_num_blocks_with_motion = cpi->rc.high_num_blocks_with_motion;
4017     // On scene change reset temporal layer pattern to TL0.
4018     // Note that if the base/lower spatial layers are skipped: instead of
4019     // inserting base layer here, we force max-q for the next superframe
4020     // with lower spatial layers: this is done in vp9_encodedframe_overshoot()
4021     // when max-q is decided for the current layer.
4022     // Only do this reset for bypass/flexible mode.
4023     if (svc->high_source_sad_superframe && svc->temporal_layer_id > 0 &&
4024         svc->temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_BYPASS) {
4025       // rc->high_source_sad will get reset so copy it to restore it.
4026       int tmp_high_source_sad = cpi->rc.high_source_sad;
4027       vp9_svc_reset_temporal_layers(cpi, cm->frame_type == KEY_FRAME);
4028       cpi->rc.high_source_sad = tmp_high_source_sad;
4029     }
4030   }
4031 
4032   vp9_update_noise_estimate(cpi);
4033 
4034   // For 1 pass CBR, check if we are dropping this frame.
4035   // Never drop on key frame, if base layer is key for svc,
4036   // on scene change, or if superframe has layer sync.
4037   if ((cpi->rc.high_source_sad || svc->high_source_sad_superframe) &&
4038       !(cpi->rc.use_post_encode_drop && svc->last_layer_dropped[0]))
4039     no_drop_scene_change = 1;
4040   if (cpi->oxcf.pass == 0 && cpi->oxcf.rc_mode == VPX_CBR &&
4041       !frame_is_intra_only(cm) && !no_drop_scene_change &&
4042       !svc->superframe_has_layer_sync &&
4043       (!cpi->use_svc ||
4044        !svc->layer_context[svc->temporal_layer_id].is_key_frame)) {
4045     if (vp9_rc_drop_frame(cpi)) return 0;
4046   }
4047 
4048   // For 1 pass CBR SVC, only ZEROMV is allowed for spatial reference frame
4049   // when svc->force_zero_mode_spatial_ref = 1. Under those conditions we can
4050   // avoid this frame-level upsampling (for non intra_only frames).
4051   // For SVC single_layer mode, dynamic resize is allowed and we need to
4052   // scale references for this case.
4053   if (frame_is_intra_only(cm) == 0 &&
4054       ((svc->single_layer_svc && cpi->oxcf.resize_mode == RESIZE_DYNAMIC) ||
4055        !(is_one_pass_cbr_svc(cpi) && svc->force_zero_mode_spatial_ref))) {
4056     vp9_scale_references(cpi);
4057   }
4058 
4059   set_size_independent_vars(cpi);
4060   set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
4061 
4062   // search method and step parameter might be changed in speed settings.
4063   init_motion_estimation(cpi);
4064 
4065   if (cpi->sf.copy_partition_flag) alloc_copy_partition_data(cpi);
4066 
4067   if (cpi->sf.svc_use_lowres_part &&
4068       svc->spatial_layer_id == svc->number_spatial_layers - 2) {
4069     if (svc->prev_partition_svc == NULL) {
4070       CHECK_MEM_ERROR(
4071           cm, svc->prev_partition_svc,
4072           (BLOCK_SIZE *)vpx_calloc(cm->mi_stride * cm->mi_rows,
4073                                    sizeof(*svc->prev_partition_svc)));
4074     }
4075   }
4076 
4077   // TODO(jianj): Look into issue of skin detection with high bitdepth.
4078   if (cm->bit_depth == 8 && cpi->oxcf.speed >= 5 && cpi->oxcf.pass == 0 &&
4079       cpi->oxcf.rc_mode == VPX_CBR &&
4080       cpi->oxcf.content != VP9E_CONTENT_SCREEN &&
4081       cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) {
4082     cpi->use_skin_detection = 1;
4083   }
4084 
4085   // Enable post encode frame dropping for CBR on non key frame, when
4086   // ext_use_post_encode_drop is specified by user.
4087   cpi->rc.use_post_encode_drop = cpi->rc.ext_use_post_encode_drop &&
4088                                  cpi->oxcf.rc_mode == VPX_CBR &&
4089                                  cm->frame_type != KEY_FRAME;
4090 
4091   vp9_set_quantizer(cpi, q);
4092   vp9_set_variance_partition_thresholds(cpi, q, 0);
4093 
4094   setup_frame(cpi);
4095 
4096   suppress_active_map(cpi);
4097 
4098   if (cpi->use_svc) {
4099     // On non-zero spatial layer, check for disabling inter-layer
4100     // prediction.
4101     if (svc->spatial_layer_id > 0) vp9_svc_constrain_inter_layer_pred(cpi);
4102     vp9_svc_assert_constraints_pattern(cpi);
4103   }
4104 
4105   if (cpi->rc.last_post_encode_dropped_scene_change) {
4106     cpi->rc.high_source_sad = 1;
4107     svc->high_source_sad_superframe = 1;
4108     // For now disable use_source_sad since Last_Source will not be the previous
4109     // encoded but the dropped one.
4110     cpi->sf.use_source_sad = 0;
4111     cpi->rc.last_post_encode_dropped_scene_change = 0;
4112   }
4113   // Check if this high_source_sad (scene/slide change) frame should be
4114   // encoded at high/max QP, and if so, set the q and adjust some rate
4115   // control parameters.
4116   if (cpi->sf.overshoot_detection_cbr_rt == FAST_DETECTION_MAXQ &&
4117       (cpi->rc.high_source_sad ||
4118        (cpi->use_svc && svc->high_source_sad_superframe))) {
4119     if (vp9_encodedframe_overshoot(cpi, -1, &q)) {
4120       vp9_set_quantizer(cpi, q);
4121       vp9_set_variance_partition_thresholds(cpi, q, 0);
4122     }
4123   }
4124 
4125 #if !CONFIG_REALTIME_ONLY
4126   // Variance adaptive and in frame q adjustment experiments are mutually
4127   // exclusive.
4128   if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
4129     vp9_vaq_frame_setup(cpi);
4130   } else if (cpi->oxcf.aq_mode == EQUATOR360_AQ) {
4131     vp9_360aq_frame_setup(cpi);
4132   } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
4133     vp9_setup_in_frame_q_adj(cpi);
4134   } else if (cpi->oxcf.aq_mode == LOOKAHEAD_AQ) {
4135     // it may be pretty bad for rate-control,
4136     // and I should handle it somehow
4137     vp9_alt_ref_aq_setup_map(cpi->alt_ref_aq, cpi);
4138   } else {
4139 #endif
4140     if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) {
4141       vp9_cyclic_refresh_setup(cpi);
4142     } else if (cpi->roi.enabled && !frame_is_intra_only(cm)) {
4143       apply_roi_map(cpi);
4144     }
4145 #if !CONFIG_REALTIME_ONLY
4146   }
4147 #endif
4148 
4149   apply_active_map(cpi);
4150 
4151   vp9_encode_frame(cpi);
4152 
4153   // Check if we should re-encode this frame at high Q because of high
4154   // overshoot based on the encoded frame size. Only for frames where
4155   // high temporal-source SAD is detected.
4156   // For SVC: all spatial layers are checked for re-encoding.
4157   if (cpi->sf.overshoot_detection_cbr_rt == RE_ENCODE_MAXQ &&
4158       (cpi->rc.high_source_sad ||
4159        (cpi->use_svc && svc->high_source_sad_superframe))) {
4160     int frame_size = 0;
4161     // Get an estimate of the encoded frame size.
4162     save_coding_context(cpi);
4163     vp9_pack_bitstream(cpi, dest, size);
4164     restore_coding_context(cpi);
4165     frame_size = (int)(*size) << 3;
4166     // Check if encoded frame will overshoot too much, and if so, set the q and
4167     // adjust some rate control parameters, and return to re-encode the frame.
4168     if (vp9_encodedframe_overshoot(cpi, frame_size, &q)) {
4169       vpx_clear_system_state();
4170       vp9_set_quantizer(cpi, q);
4171       vp9_set_variance_partition_thresholds(cpi, q, 0);
4172       suppress_active_map(cpi);
4173       // Turn-off cyclic refresh for re-encoded frame.
4174       if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) {
4175         CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
4176         unsigned char *const seg_map = cpi->segmentation_map;
4177         memset(seg_map, 0, cm->mi_rows * cm->mi_cols);
4178         memset(cr->last_coded_q_map, MAXQ,
4179                cm->mi_rows * cm->mi_cols * sizeof(*cr->last_coded_q_map));
4180         cr->sb_index = 0;
4181         vp9_disable_segmentation(&cm->seg);
4182       }
4183       apply_active_map(cpi);
4184       vp9_encode_frame(cpi);
4185     }
4186   }
4187 
4188   // Update some stats from cyclic refresh, and check for golden frame update.
4189   if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->seg.enabled &&
4190       !frame_is_intra_only(cm) && cpi->cyclic_refresh->content_mode)
4191     vp9_cyclic_refresh_postencode(cpi);
4192 
4193   // Update the skip mb flag probabilities based on the distribution
4194   // seen in the last encoder iteration.
4195   // update_base_skip_probs(cpi);
4196   vpx_clear_system_state();
4197   return 1;
4198 }
4199 
4200 static int get_ref_frame_flags(const VP9_COMP *cpi) {
4201   const int *const map = cpi->common.ref_frame_map;
4202   const int gold_is_last = map[cpi->gld_fb_idx] == map[cpi->lst_fb_idx];
4203   const int alt_is_last = map[cpi->alt_fb_idx] == map[cpi->lst_fb_idx];
4204   const int gold_is_alt = map[cpi->gld_fb_idx] == map[cpi->alt_fb_idx];
4205   int flags = VP9_ALT_FLAG | VP9_GOLD_FLAG | VP9_LAST_FLAG;
4206 
4207   if (gold_is_last) flags &= ~VP9_GOLD_FLAG;
4208 
4209   if (cpi->rc.frames_till_gf_update_due == INT_MAX &&
4210       (cpi->svc.number_temporal_layers == 1 &&
4211        cpi->svc.number_spatial_layers == 1))
4212     flags &= ~VP9_GOLD_FLAG;
4213 
4214   if (alt_is_last) flags &= ~VP9_ALT_FLAG;
4215 
4216   if (gold_is_alt) flags &= ~VP9_ALT_FLAG;
4217 
4218   return flags;
4219 }
4220 
4221 #if !CONFIG_REALTIME_ONLY
4222 #define MAX_QSTEP_ADJ 4
4223 static int get_qstep_adj(int rate_excess, int rate_limit) {
4224   int qstep =
4225       rate_limit ? ((rate_excess + rate_limit / 2) / rate_limit) : INT_MAX;
4226   return VPXMIN(qstep, MAX_QSTEP_ADJ);
4227 }
4228 
4229 #if CONFIG_RATE_CTRL
4230 static void init_rq_history(RATE_QINDEX_HISTORY *rq_history) {
4231   rq_history->recode_count = 0;
4232   rq_history->q_index_high = 255;
4233   rq_history->q_index_low = 0;
4234 }
4235 
4236 static void update_rq_history(RATE_QINDEX_HISTORY *rq_history, int target_bits,
4237                               int actual_bits, int q_index) {
4238   rq_history->q_index_history[rq_history->recode_count] = q_index;
4239   rq_history->rate_history[rq_history->recode_count] = actual_bits;
4240   if (actual_bits <= target_bits) {
4241     rq_history->q_index_high = q_index;
4242   }
4243   if (actual_bits >= target_bits) {
4244     rq_history->q_index_low = q_index;
4245   }
4246   rq_history->recode_count += 1;
4247 }
4248 
4249 static int guess_q_index_from_model(const RATE_QSTEP_MODEL *rq_model,
4250                                     int target_bits) {
4251   // The model predicts bits as follows.
4252   // target_bits = bias - ratio * log2(q_step)
4253   // Given the target_bits, we compute the q_step as follows.
4254   double q_step;
4255   assert(rq_model->ratio > 0);
4256   q_step = pow(2.0, (rq_model->bias - target_bits) / rq_model->ratio);
4257   // TODO(angiebird): Make this function support highbitdepth.
4258   return vp9_convert_q_to_qindex(q_step, VPX_BITS_8);
4259 }
4260 
4261 static int guess_q_index_linear(int prev_q_index, int target_bits,
4262                                 int actual_bits, int gap) {
4263   int q_index = prev_q_index;
4264   if (actual_bits < target_bits) {
4265     q_index -= gap;
4266     q_index = VPXMAX(q_index, 0);
4267   } else {
4268     q_index += gap;
4269     q_index = VPXMIN(q_index, 255);
4270   }
4271   return q_index;
4272 }
4273 
4274 static double get_bits_percent_diff(int target_bits, int actual_bits) {
4275   double diff;
4276   target_bits = VPXMAX(target_bits, 1);
4277   diff = abs(target_bits - actual_bits) * 1. / target_bits;
4278   return diff * 100;
4279 }
4280 
4281 static int rq_model_predict_q_index(const RATE_QSTEP_MODEL *rq_model,
4282                                     const RATE_QINDEX_HISTORY *rq_history,
4283                                     int target_bits) {
4284   int q_index = 128;
4285   if (rq_history->recode_count > 0) {
4286     const int actual_bits =
4287         rq_history->rate_history[rq_history->recode_count - 1];
4288     const int prev_q_index =
4289         rq_history->q_index_history[rq_history->recode_count - 1];
4290     const double percent_diff = get_bits_percent_diff(target_bits, actual_bits);
4291     if (percent_diff > 50) {
4292       // Binary search.
4293       // When the actual_bits and target_bits are far apart, binary search
4294       // q_index is faster.
4295       q_index = (rq_history->q_index_low + rq_history->q_index_high) / 2;
4296     } else {
4297       if (rq_model->ready) {
4298         q_index = guess_q_index_from_model(rq_model, target_bits);
4299       } else {
4300         // TODO(angiebird): Find a better way to set the gap.
4301         q_index =
4302             guess_q_index_linear(prev_q_index, target_bits, actual_bits, 20);
4303       }
4304     }
4305   } else {
4306     if (rq_model->ready) {
4307       q_index = guess_q_index_from_model(rq_model, target_bits);
4308     }
4309   }
4310 
4311   assert(rq_history->q_index_low <= rq_history->q_index_high);
4312   if (q_index <= rq_history->q_index_low) {
4313     q_index = rq_history->q_index_low + 1;
4314   }
4315   if (q_index >= rq_history->q_index_high) {
4316     q_index = rq_history->q_index_high - 1;
4317   }
4318   return q_index;
4319 }
4320 
4321 static void rq_model_update(const RATE_QINDEX_HISTORY *rq_history,
4322                             int target_bits, RATE_QSTEP_MODEL *rq_model) {
4323   const int recode_count = rq_history->recode_count;
4324   const double delta = 0.00001;
4325   if (recode_count >= 2) {
4326     const int q_index1 = rq_history->q_index_history[recode_count - 2];
4327     const int q_index2 = rq_history->q_index_history[recode_count - 1];
4328     const int r1 = rq_history->rate_history[recode_count - 2];
4329     const int r2 = rq_history->rate_history[recode_count - 1];
4330     int valid = 0;
4331     // lower q_index should yield higher bit rate
4332     if (q_index1 < q_index2) {
4333       valid = r1 > r2;
4334     } else if (q_index1 > q_index2) {
4335       valid = r1 < r2;
4336     }
4337     // Only update the model when the q_index and rate behave normally.
4338     if (valid) {
4339       // Fit the ratio and bias of rq_model based on last two recode histories.
4340       const double s1 = vp9_convert_qindex_to_q(q_index1, VPX_BITS_8);
4341       const double s2 = vp9_convert_qindex_to_q(q_index2, VPX_BITS_8);
4342       if (fabs(log2(s1) - log2(s2)) > delta) {
4343         rq_model->ratio = (r2 - r1) / (log2(s1) - log2(s2));
4344         rq_model->bias = r1 + (rq_model->ratio) * log2(s1);
4345         if (rq_model->ratio > delta && rq_model->bias > delta) {
4346           rq_model->ready = 1;
4347         }
4348       }
4349     }
4350   } else if (recode_count == 1) {
4351     if (rq_model->ready) {
4352       // Update the ratio only when the initial model exists and we only have
4353       // one recode history.
4354       const int prev_q = rq_history->q_index_history[recode_count - 1];
4355       const double prev_q_step = vp9_convert_qindex_to_q(prev_q, VPX_BITS_8);
4356       if (fabs(log2(prev_q_step)) > delta) {
4357         const int actual_bits = rq_history->rate_history[recode_count - 1];
4358         rq_model->ratio =
4359             rq_model->ratio + (target_bits - actual_bits) / log2(prev_q_step);
4360       }
4361     }
4362   }
4363 }
4364 #endif  // CONFIG_RATE_CTRL
4365 
4366 static void encode_with_recode_loop(VP9_COMP *cpi, size_t *size, uint8_t *dest
4367 #if CONFIG_RATE_CTRL
4368                                     ,
4369                                     RATE_QINDEX_HISTORY *rq_history
4370 #endif  // CONFIG_RATE_CTRL
4371 ) {
4372   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
4373   VP9_COMMON *const cm = &cpi->common;
4374   RATE_CONTROL *const rc = &cpi->rc;
4375   int bottom_index, top_index;
4376   int loop_count = 0;
4377   int loop_at_this_size = 0;
4378   int loop = 0;
4379   int overshoot_seen = 0;
4380   int undershoot_seen = 0;
4381   int frame_over_shoot_limit;
4382   int frame_under_shoot_limit;
4383   int q = 0, q_low = 0, q_high = 0;
4384   int last_q_attempt = 0;
4385   int enable_acl;
4386 #ifdef AGGRESSIVE_VBR
4387   int qrange_adj = 1;
4388 #endif
4389 
4390   // A flag which indicates whether we are recoding the current frame
4391   // when the current frame size is larger than the max frame size in the
4392   // external rate control model.
4393   // This flag doesn't have any impact when external rate control is not used.
4394   int ext_rc_recode = 0;
4395   // Maximal frame size allowed by the external rate control.
4396   // case: 0, we ignore the max frame size limit, and encode with the qindex
4397   // passed in by the external rate control model.
4398   // case: -1, we take VP9's decision for the max frame size.
4399   int ext_rc_max_frame_size = 0;
4400   const int orig_rc_max_frame_bandwidth = rc->max_frame_bandwidth;
4401 
4402 #if CONFIG_RATE_CTRL
4403   const FRAME_UPDATE_TYPE update_type =
4404       cpi->twopass.gf_group.update_type[cpi->twopass.gf_group.index];
4405   const ENCODE_FRAME_TYPE frame_type = get_encode_frame_type(update_type);
4406   RATE_QSTEP_MODEL *rq_model = &cpi->rq_model[frame_type];
4407   init_rq_history(rq_history);
4408 #endif  // CONFIG_RATE_CTRL
4409 
4410   if (cm->show_existing_frame) {
4411     rc->this_frame_target = 0;
4412     if (is_psnr_calc_enabled(cpi)) set_raw_source_frame(cpi);
4413     return;
4414   }
4415 
4416   set_size_independent_vars(cpi);
4417 
4418   enable_acl = cpi->sf.allow_acl ? (cm->frame_type == KEY_FRAME) ||
4419                                        (cpi->twopass.gf_group.index == 1)
4420                                  : 0;
4421 
4422   do {
4423     vpx_clear_system_state();
4424 
4425     set_frame_size(cpi);
4426 
4427     if (loop_count == 0 || cpi->resize_pending != 0) {
4428       set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
4429 
4430 #ifdef AGGRESSIVE_VBR
4431       if (two_pass_first_group_inter(cpi)) {
4432         // Adjustment limits for min and max q
4433         qrange_adj = VPXMAX(1, (top_index - bottom_index) / 2);
4434 
4435         bottom_index =
4436             VPXMAX(bottom_index - qrange_adj / 2, oxcf->best_allowed_q);
4437         top_index = VPXMIN(oxcf->worst_allowed_q, top_index + qrange_adj / 2);
4438       }
4439 #endif
4440       // TODO(agrange) Scale cpi->max_mv_magnitude if frame-size has changed.
4441       set_mv_search_params(cpi);
4442 
4443       // Reset the loop state for new frame size.
4444       overshoot_seen = 0;
4445       undershoot_seen = 0;
4446 
4447       // Reconfiguration for change in frame size has concluded.
4448       cpi->resize_pending = 0;
4449 
4450       q_low = bottom_index;
4451       q_high = top_index;
4452 
4453       loop_at_this_size = 0;
4454     }
4455 
4456     // Decide frame size bounds first time through.
4457     if (loop_count == 0) {
4458       vp9_rc_compute_frame_size_bounds(cpi, rc->this_frame_target,
4459                                        &frame_under_shoot_limit,
4460                                        &frame_over_shoot_limit);
4461     }
4462 
4463     cpi->Source =
4464         vp9_scale_if_required(cm, cpi->un_scaled_source, &cpi->scaled_source,
4465                               (oxcf->pass == 0), EIGHTTAP, 0);
4466 
4467     // Unfiltered raw source used in metrics calculation if the source
4468     // has been filtered.
4469     if (is_psnr_calc_enabled(cpi)) {
4470 #ifdef ENABLE_KF_DENOISE
4471       if (is_spatial_denoise_enabled(cpi)) {
4472         cpi->raw_source_frame = vp9_scale_if_required(
4473             cm, &cpi->raw_unscaled_source, &cpi->raw_scaled_source,
4474             (oxcf->pass == 0), EIGHTTAP, 0);
4475       } else {
4476         cpi->raw_source_frame = cpi->Source;
4477       }
4478 #else
4479       cpi->raw_source_frame = cpi->Source;
4480 #endif
4481     }
4482 
4483     if (cpi->unscaled_last_source != NULL)
4484       cpi->Last_Source = vp9_scale_if_required(cm, cpi->unscaled_last_source,
4485                                                &cpi->scaled_last_source,
4486                                                (oxcf->pass == 0), EIGHTTAP, 0);
4487 
4488     if (frame_is_intra_only(cm) == 0) {
4489       if (loop_count > 0) {
4490         release_scaled_references(cpi);
4491       }
4492       vp9_scale_references(cpi);
4493     }
4494 
4495 #if CONFIG_RATE_CTRL
4496     // TODO(angiebird): This is a hack for making sure the encoder use the
4497     // external_quantize_index exactly. Avoid this kind of hack later.
4498     if (cpi->oxcf.use_simple_encode_api) {
4499       if (cpi->encode_command.use_external_target_frame_bits) {
4500         q = rq_model_predict_q_index(rq_model, rq_history,
4501                                      rc->this_frame_target);
4502       }
4503       if (cpi->encode_command.use_external_quantize_index) {
4504         q = cpi->encode_command.external_quantize_index;
4505       }
4506     }
4507 #endif  // CONFIG_RATE_CTRL
4508     if (cpi->ext_ratectrl.ready && !ext_rc_recode) {
4509       vpx_codec_err_t codec_status;
4510       const GF_GROUP *gf_group = &cpi->twopass.gf_group;
4511       vpx_rc_encodeframe_decision_t encode_frame_decision;
4512       FRAME_UPDATE_TYPE update_type = gf_group->update_type[gf_group->index];
4513       const int ref_frame_flags = get_ref_frame_flags(cpi);
4514       RefCntBuffer *ref_frame_bufs[MAX_INTER_REF_FRAMES];
4515       const RefCntBuffer *curr_frame_buf =
4516           get_ref_cnt_buffer(cm, cm->new_fb_idx);
4517       get_ref_frame_bufs(cpi, ref_frame_bufs);
4518       codec_status = vp9_extrc_get_encodeframe_decision(
4519           &cpi->ext_ratectrl, curr_frame_buf->frame_index,
4520           cm->current_frame_coding_index, gf_group->index, update_type,
4521           ref_frame_bufs, ref_frame_flags, &encode_frame_decision);
4522       if (codec_status != VPX_CODEC_OK) {
4523         vpx_internal_error(&cm->error, codec_status,
4524                            "vp9_extrc_get_encodeframe_decision() failed");
4525       }
4526       q = encode_frame_decision.q_index;
4527       ext_rc_max_frame_size = encode_frame_decision.max_frame_size;
4528     }
4529 
4530     vp9_set_quantizer(cpi, q);
4531 
4532     if (loop_count == 0) setup_frame(cpi);
4533 
4534     // Variance adaptive and in frame q adjustment experiments are mutually
4535     // exclusive.
4536     if (oxcf->aq_mode == VARIANCE_AQ) {
4537       vp9_vaq_frame_setup(cpi);
4538     } else if (oxcf->aq_mode == EQUATOR360_AQ) {
4539       vp9_360aq_frame_setup(cpi);
4540     } else if (oxcf->aq_mode == COMPLEXITY_AQ) {
4541       vp9_setup_in_frame_q_adj(cpi);
4542     } else if (oxcf->aq_mode == LOOKAHEAD_AQ) {
4543       vp9_alt_ref_aq_setup_map(cpi->alt_ref_aq, cpi);
4544     } else if (oxcf->aq_mode == PSNR_AQ) {
4545       vp9_psnr_aq_mode_setup(&cm->seg);
4546     }
4547 
4548     vp9_encode_frame(cpi);
4549 
4550     // Update the skip mb flag probabilities based on the distribution
4551     // seen in the last encoder iteration.
4552     // update_base_skip_probs(cpi);
4553 
4554     vpx_clear_system_state();
4555 
4556     // Dummy pack of the bitstream using up to date stats to get an
4557     // accurate estimate of output frame size to determine if we need
4558     // to recode.
4559     if (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF) {
4560       save_coding_context(cpi);
4561       if (!cpi->sf.use_nonrd_pick_mode) vp9_pack_bitstream(cpi, dest, size);
4562 
4563       rc->projected_frame_size = (int)(*size) << 3;
4564 
4565       if (frame_over_shoot_limit == 0) frame_over_shoot_limit = 1;
4566     }
4567 
4568     if (cpi->ext_ratectrl.ready) {
4569       last_q_attempt = q;
4570       // In general, for the external rate control, we take the qindex provided
4571       // as input and encode the frame with this qindex faithfully. However,
4572       // in some extreme scenarios, the provided qindex leads to a massive
4573       // overshoot of frame size. In this case, we fall back to VP9's decision
4574       // to pick a new qindex and recode the frame. We return the new qindex
4575       // through the API to the external model.
4576       if (ext_rc_max_frame_size == 0) {
4577         break;
4578       } else if (ext_rc_max_frame_size == -1) {
4579         if (rc->projected_frame_size < rc->max_frame_bandwidth) {
4580           break;
4581         }
4582       } else {
4583         if (rc->projected_frame_size < ext_rc_max_frame_size) {
4584           break;
4585         }
4586       }
4587       rc->max_frame_bandwidth = ext_rc_max_frame_size;
4588       // If the current frame size exceeds the ext_rc_max_frame_size,
4589       // we adjust the worst qindex to meet the frame size constraint.
4590       q_high = 255;
4591       ext_rc_recode = 1;
4592     }
4593 #if CONFIG_RATE_CTRL
4594     if (cpi->oxcf.use_simple_encode_api) {
4595       // This part needs to be after save_coding_context() because
4596       // restore_coding_context will be called in the end of this function.
4597       // TODO(angiebird): This is a hack for making sure the encoder use the
4598       // external_quantize_index exactly. Avoid this kind of hack later.
4599       if (cpi->encode_command.use_external_quantize_index) {
4600         break;
4601       }
4602 
4603       if (cpi->encode_command.use_external_target_frame_bits) {
4604         const double percent_diff = get_bits_percent_diff(
4605             rc->this_frame_target, rc->projected_frame_size);
4606         update_rq_history(rq_history, rc->this_frame_target,
4607                           rc->projected_frame_size, q);
4608         loop_count += 1;
4609 
4610         rq_model_update(rq_history, rc->this_frame_target, rq_model);
4611 
4612         // Check if we hit the target bitrate.
4613         if (percent_diff <=
4614                 cpi->encode_command.target_frame_bits_error_percent ||
4615             rq_history->recode_count >= RATE_CTRL_MAX_RECODE_NUM ||
4616             rq_history->q_index_low >= rq_history->q_index_high) {
4617           break;
4618         }
4619 
4620         loop = 1;
4621         restore_coding_context(cpi);
4622         continue;
4623       }
4624     }
4625 #endif  // CONFIG_RATE_CTRL
4626 
4627     if (oxcf->rc_mode == VPX_Q) {
4628       loop = 0;
4629     } else {
4630       if ((cm->frame_type == KEY_FRAME) && rc->this_key_frame_forced &&
4631           (rc->projected_frame_size < rc->max_frame_bandwidth)) {
4632         int last_q = q;
4633         int64_t kf_err;
4634 
4635         int64_t high_err_target = cpi->ambient_err;
4636         int64_t low_err_target = cpi->ambient_err >> 1;
4637 
4638 #if CONFIG_VP9_HIGHBITDEPTH
4639         if (cm->use_highbitdepth) {
4640           kf_err = vpx_highbd_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
4641         } else {
4642           kf_err = vpx_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
4643         }
4644 #else
4645         kf_err = vpx_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
4646 #endif  // CONFIG_VP9_HIGHBITDEPTH
4647 
4648         // Prevent possible divide by zero error below for perfect KF
4649         kf_err += !kf_err;
4650 
4651         // The key frame is not good enough or we can afford
4652         // to make it better without undue risk of popping.
4653         if ((kf_err > high_err_target &&
4654              rc->projected_frame_size <= frame_over_shoot_limit) ||
4655             (kf_err > low_err_target &&
4656              rc->projected_frame_size <= frame_under_shoot_limit)) {
4657           // Lower q_high
4658           q_high = q > q_low ? q - 1 : q_low;
4659 
4660           // Adjust Q
4661           q = (int)((q * high_err_target) / kf_err);
4662           q = VPXMIN(q, (q_high + q_low) >> 1);
4663         } else if (kf_err < low_err_target &&
4664                    rc->projected_frame_size >= frame_under_shoot_limit) {
4665           // The key frame is much better than the previous frame
4666           // Raise q_low
4667           q_low = q < q_high ? q + 1 : q_high;
4668 
4669           // Adjust Q
4670           q = (int)((q * low_err_target) / kf_err);
4671           q = VPXMIN(q, (q_high + q_low + 1) >> 1);
4672         }
4673 
4674         // Clamp Q to upper and lower limits:
4675         q = clamp(q, q_low, q_high);
4676 
4677         loop = q != last_q;
4678       } else if (recode_loop_test(cpi, frame_over_shoot_limit,
4679                                   frame_under_shoot_limit, q,
4680                                   VPXMAX(q_high, top_index), bottom_index)) {
4681         // Is the projected frame size out of range and are we allowed
4682         // to attempt to recode.
4683         int last_q = q;
4684         int retries = 0;
4685         int qstep;
4686 
4687         if (cpi->resize_pending == 1) {
4688           // Change in frame size so go back around the recode loop.
4689           cpi->rc.frame_size_selector =
4690               SCALE_STEP1 - cpi->rc.frame_size_selector;
4691           cpi->rc.next_frame_size_selector = cpi->rc.frame_size_selector;
4692 
4693 #if CONFIG_INTERNAL_STATS
4694           ++cpi->tot_recode_hits;
4695 #endif
4696           ++loop_count;
4697           loop = 1;
4698           continue;
4699         }
4700 
4701         // Frame size out of permitted range:
4702         // Update correction factor & compute new Q to try...
4703 
4704         // Frame is too large
4705         if (rc->projected_frame_size > rc->this_frame_target) {
4706           // Special case if the projected size is > the max allowed.
4707           if ((q == q_high) &&
4708               ((rc->projected_frame_size >= rc->max_frame_bandwidth) ||
4709                (!rc->is_src_frame_alt_ref &&
4710                 (rc->projected_frame_size >=
4711                  big_rate_miss_high_threshold(cpi))))) {
4712             int max_rate = VPXMAX(1, VPXMIN(rc->max_frame_bandwidth,
4713                                             big_rate_miss_high_threshold(cpi)));
4714             double q_val_high;
4715             q_val_high = vp9_convert_qindex_to_q(q_high, cm->bit_depth);
4716             q_val_high =
4717                 q_val_high * ((double)rc->projected_frame_size / max_rate);
4718             q_high = vp9_convert_q_to_qindex(q_val_high, cm->bit_depth);
4719             q_high = clamp(q_high, rc->best_quality, rc->worst_quality);
4720           }
4721 
4722           // Raise Qlow as to at least the current value
4723           qstep =
4724               get_qstep_adj(rc->projected_frame_size, rc->this_frame_target);
4725           q_low = VPXMIN(q + qstep, q_high);
4726 
4727           if (undershoot_seen || loop_at_this_size > 1) {
4728             // Update rate_correction_factor unless
4729             vp9_rc_update_rate_correction_factors(cpi);
4730 
4731             q = (q_high + q_low + 1) / 2;
4732           } else {
4733             // Update rate_correction_factor unless
4734             vp9_rc_update_rate_correction_factors(cpi);
4735 
4736             q = vp9_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4737                                   VPXMAX(q_high, top_index));
4738 
4739             while (q < q_low && retries < 10) {
4740               vp9_rc_update_rate_correction_factors(cpi);
4741               q = vp9_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4742                                     VPXMAX(q_high, top_index));
4743               retries++;
4744             }
4745           }
4746 
4747           overshoot_seen = 1;
4748         } else {
4749           // Frame is too small
4750           qstep =
4751               get_qstep_adj(rc->this_frame_target, rc->projected_frame_size);
4752           q_high = VPXMAX(q - qstep, q_low);
4753 
4754           if (overshoot_seen || loop_at_this_size > 1) {
4755             vp9_rc_update_rate_correction_factors(cpi);
4756             q = (q_high + q_low) / 2;
4757           } else {
4758             vp9_rc_update_rate_correction_factors(cpi);
4759             q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
4760                                   VPXMIN(q_low, bottom_index), top_index);
4761             // Special case reset for qlow for constrained quality.
4762             // This should only trigger where there is very substantial
4763             // undershoot on a frame and the auto cq level is above
4764             // the user passed in value.
4765             if (oxcf->rc_mode == VPX_CQ && q < q_low) {
4766               q_low = q;
4767             }
4768 
4769             while (q > q_high && retries < 10) {
4770               vp9_rc_update_rate_correction_factors(cpi);
4771               q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
4772                                     VPXMIN(q_low, bottom_index), top_index);
4773               retries++;
4774             }
4775           }
4776           undershoot_seen = 1;
4777         }
4778 
4779         // Clamp Q to upper and lower limits:
4780         q = clamp(q, q_low, q_high);
4781 
4782         loop = (q != last_q);
4783       } else {
4784         loop = 0;
4785       }
4786     }
4787 
4788     // Special case for overlay frame.
4789     if (rc->is_src_frame_alt_ref &&
4790         rc->projected_frame_size < rc->max_frame_bandwidth)
4791       loop = 0;
4792 
4793     // Special handling of external max frame size constraint
4794     if (ext_rc_recode) {
4795       // If the largest q is not able to meet the max frame size limit,
4796       // do nothing.
4797       if (rc->projected_frame_size > ext_rc_max_frame_size &&
4798           last_q_attempt == 255) {
4799         break;
4800       }
4801       // If VP9's q selection leads to a smaller q, we force it to use
4802       // a larger q to better approximate the external max frame size
4803       // constraint.
4804       if (rc->projected_frame_size > ext_rc_max_frame_size &&
4805           q <= last_q_attempt) {
4806         q = VPXMIN(255, last_q_attempt + 1);
4807       }
4808     }
4809 
4810     if (loop) {
4811       ++loop_count;
4812       ++loop_at_this_size;
4813 
4814 #if CONFIG_INTERNAL_STATS
4815       ++cpi->tot_recode_hits;
4816 #endif
4817     }
4818 
4819     if (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF)
4820       if (loop) restore_coding_context(cpi);
4821   } while (loop);
4822 
4823   rc->max_frame_bandwidth = orig_rc_max_frame_bandwidth;
4824 
4825 #ifdef AGGRESSIVE_VBR
4826   if (two_pass_first_group_inter(cpi)) {
4827     cpi->twopass.active_worst_quality =
4828         VPXMIN(q + qrange_adj, oxcf->worst_allowed_q);
4829   } else if (!frame_is_kf_gf_arf(cpi)) {
4830 #else
4831   if (!frame_is_kf_gf_arf(cpi)) {
4832 #endif
4833     // Have we been forced to adapt Q outside the expected range by an extreme
4834     // rate miss. If so adjust the active maxQ for the subsequent frames.
4835     if (!rc->is_src_frame_alt_ref && (q > cpi->twopass.active_worst_quality)) {
4836       cpi->twopass.active_worst_quality = q;
4837     } else if (oxcf->vbr_corpus_complexity && q == q_low &&
4838                rc->projected_frame_size < rc->this_frame_target) {
4839       cpi->twopass.active_worst_quality =
4840           VPXMAX(q, cpi->twopass.active_worst_quality - 1);
4841     }
4842   }
4843 
4844   if (enable_acl) {
4845     // Skip recoding, if model diff is below threshold
4846     const int thresh = compute_context_model_thresh(cpi);
4847     const int diff = compute_context_model_diff(cm);
4848     if (diff >= thresh) {
4849       vp9_encode_frame(cpi);
4850     }
4851   }
4852   if (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF) {
4853     vpx_clear_system_state();
4854     restore_coding_context(cpi);
4855   }
4856 }
4857 #endif  // !CONFIG_REALTIME_ONLY
4858 
4859 static void set_ext_overrides(VP9_COMP *cpi) {
4860   // Overrides the defaults with the externally supplied values with
4861   // vp9_update_reference() and vp9_update_entropy() calls
4862   // Note: The overrides are valid only for the next frame passed
4863   // to encode_frame_to_data_rate() function
4864   if (cpi->ext_refresh_frame_context_pending) {
4865     cpi->common.refresh_frame_context = cpi->ext_refresh_frame_context;
4866     cpi->ext_refresh_frame_context_pending = 0;
4867   }
4868   if (cpi->ext_refresh_frame_flags_pending) {
4869     cpi->refresh_last_frame = cpi->ext_refresh_last_frame;
4870     cpi->refresh_golden_frame = cpi->ext_refresh_golden_frame;
4871     cpi->refresh_alt_ref_frame = cpi->ext_refresh_alt_ref_frame;
4872   }
4873 }
4874 
4875 YV12_BUFFER_CONFIG *vp9_svc_twostage_scale(
4876     VP9_COMMON *cm, YV12_BUFFER_CONFIG *unscaled, YV12_BUFFER_CONFIG *scaled,
4877     YV12_BUFFER_CONFIG *scaled_temp, INTERP_FILTER filter_type,
4878     int phase_scaler, INTERP_FILTER filter_type2, int phase_scaler2) {
4879   if (cm->mi_cols * MI_SIZE != unscaled->y_width ||
4880       cm->mi_rows * MI_SIZE != unscaled->y_height) {
4881 #if CONFIG_VP9_HIGHBITDEPTH
4882     if (cm->bit_depth == VPX_BITS_8) {
4883       vp9_scale_and_extend_frame(unscaled, scaled_temp, filter_type2,
4884                                  phase_scaler2);
4885       vp9_scale_and_extend_frame(scaled_temp, scaled, filter_type,
4886                                  phase_scaler);
4887     } else {
4888       scale_and_extend_frame(unscaled, scaled_temp, (int)cm->bit_depth,
4889                              filter_type2, phase_scaler2);
4890       scale_and_extend_frame(scaled_temp, scaled, (int)cm->bit_depth,
4891                              filter_type, phase_scaler);
4892     }
4893 #else
4894     vp9_scale_and_extend_frame(unscaled, scaled_temp, filter_type2,
4895                                phase_scaler2);
4896     vp9_scale_and_extend_frame(scaled_temp, scaled, filter_type, phase_scaler);
4897 #endif  // CONFIG_VP9_HIGHBITDEPTH
4898     return scaled;
4899   } else {
4900     return unscaled;
4901   }
4902 }
4903 
4904 YV12_BUFFER_CONFIG *vp9_scale_if_required(
4905     VP9_COMMON *cm, YV12_BUFFER_CONFIG *unscaled, YV12_BUFFER_CONFIG *scaled,
4906     int use_normative_scaler, INTERP_FILTER filter_type, int phase_scaler) {
4907   if (cm->mi_cols * MI_SIZE != unscaled->y_width ||
4908       cm->mi_rows * MI_SIZE != unscaled->y_height) {
4909 #if CONFIG_VP9_HIGHBITDEPTH
4910     if (use_normative_scaler && unscaled->y_width <= (scaled->y_width << 1) &&
4911         unscaled->y_height <= (scaled->y_height << 1))
4912       if (cm->bit_depth == VPX_BITS_8)
4913         vp9_scale_and_extend_frame(unscaled, scaled, filter_type, phase_scaler);
4914       else
4915         scale_and_extend_frame(unscaled, scaled, (int)cm->bit_depth,
4916                                filter_type, phase_scaler);
4917     else
4918       scale_and_extend_frame_nonnormative(unscaled, scaled, (int)cm->bit_depth);
4919 #else
4920     if (use_normative_scaler && unscaled->y_width <= (scaled->y_width << 1) &&
4921         unscaled->y_height <= (scaled->y_height << 1))
4922       vp9_scale_and_extend_frame(unscaled, scaled, filter_type, phase_scaler);
4923     else
4924       scale_and_extend_frame_nonnormative(unscaled, scaled);
4925 #endif  // CONFIG_VP9_HIGHBITDEPTH
4926     return scaled;
4927   } else {
4928     return unscaled;
4929   }
4930 }
4931 
4932 static void set_ref_sign_bias(VP9_COMP *cpi) {
4933   VP9_COMMON *const cm = &cpi->common;
4934   RefCntBuffer *const ref_buffer = get_ref_cnt_buffer(cm, cm->new_fb_idx);
4935   const int cur_frame_index = ref_buffer->frame_index;
4936   MV_REFERENCE_FRAME ref_frame;
4937 
4938   for (ref_frame = LAST_FRAME; ref_frame < MAX_REF_FRAMES; ++ref_frame) {
4939     const int buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
4940     const RefCntBuffer *const ref_cnt_buf =
4941         get_ref_cnt_buffer(&cpi->common, buf_idx);
4942     if (ref_cnt_buf) {
4943       cm->ref_frame_sign_bias[ref_frame] =
4944           cur_frame_index < ref_cnt_buf->frame_index;
4945     }
4946   }
4947 }
4948 
4949 static int setup_interp_filter_search_mask(VP9_COMP *cpi) {
4950   INTERP_FILTER ifilter;
4951   int ref_total[MAX_REF_FRAMES] = { 0 };
4952   MV_REFERENCE_FRAME ref;
4953   int mask = 0;
4954   if (cpi->common.last_frame_type == KEY_FRAME || cpi->refresh_alt_ref_frame)
4955     return mask;
4956   for (ref = LAST_FRAME; ref <= ALTREF_FRAME; ++ref)
4957     for (ifilter = EIGHTTAP; ifilter <= EIGHTTAP_SHARP; ++ifilter)
4958       ref_total[ref] += cpi->interp_filter_selected[ref][ifilter];
4959 
4960   for (ifilter = EIGHTTAP; ifilter <= EIGHTTAP_SHARP; ++ifilter) {
4961     if ((ref_total[LAST_FRAME] &&
4962          cpi->interp_filter_selected[LAST_FRAME][ifilter] == 0) &&
4963         (ref_total[GOLDEN_FRAME] == 0 ||
4964          cpi->interp_filter_selected[GOLDEN_FRAME][ifilter] * 50 <
4965              ref_total[GOLDEN_FRAME]) &&
4966         (ref_total[ALTREF_FRAME] == 0 ||
4967          cpi->interp_filter_selected[ALTREF_FRAME][ifilter] * 50 <
4968              ref_total[ALTREF_FRAME]))
4969       mask |= 1 << ifilter;
4970   }
4971   return mask;
4972 }
4973 
4974 #ifdef ENABLE_KF_DENOISE
4975 // Baseline kernel weights for denoise
4976 static uint8_t dn_kernal_3[9] = { 1, 2, 1, 2, 4, 2, 1, 2, 1 };
4977 static uint8_t dn_kernal_5[25] = { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 2, 4,
4978                                    2, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1 };
4979 
4980 static INLINE void add_denoise_point(int centre_val, int data_val, int thresh,
4981                                      uint8_t point_weight, int *sum_val,
4982                                      int *sum_weight) {
4983   if (abs(centre_val - data_val) <= thresh) {
4984     *sum_weight += point_weight;
4985     *sum_val += (int)data_val * (int)point_weight;
4986   }
4987 }
4988 
4989 static void spatial_denoise_point(uint8_t *src_ptr, const int stride,
4990                                   const int strength) {
4991   int sum_weight = 0;
4992   int sum_val = 0;
4993   int thresh = strength;
4994   int kernal_size = 5;
4995   int half_k_size = 2;
4996   int i, j;
4997   int max_diff = 0;
4998   uint8_t *tmp_ptr;
4999   uint8_t *kernal_ptr;
5000 
5001   // Find the maximum deviation from the source point in the locale.
5002   tmp_ptr = src_ptr - (stride * (half_k_size + 1)) - (half_k_size + 1);
5003   for (i = 0; i < kernal_size + 2; ++i) {
5004     for (j = 0; j < kernal_size + 2; ++j) {
5005       max_diff = VPXMAX(max_diff, abs((int)*src_ptr - (int)tmp_ptr[j]));
5006     }
5007     tmp_ptr += stride;
5008   }
5009 
5010   // Select the kernel size.
5011   if (max_diff > (strength + (strength >> 1))) {
5012     kernal_size = 3;
5013     half_k_size = 1;
5014     thresh = thresh >> 1;
5015   }
5016   kernal_ptr = (kernal_size == 3) ? dn_kernal_3 : dn_kernal_5;
5017 
5018   // Apply the kernel
5019   tmp_ptr = src_ptr - (stride * half_k_size) - half_k_size;
5020   for (i = 0; i < kernal_size; ++i) {
5021     for (j = 0; j < kernal_size; ++j) {
5022       add_denoise_point((int)*src_ptr, (int)tmp_ptr[j], thresh, *kernal_ptr,
5023                         &sum_val, &sum_weight);
5024       ++kernal_ptr;
5025     }
5026     tmp_ptr += stride;
5027   }
5028 
5029   // Update the source value with the new filtered value
5030   *src_ptr = (uint8_t)((sum_val + (sum_weight >> 1)) / sum_weight);
5031 }
5032 
5033 #if CONFIG_VP9_HIGHBITDEPTH
5034 static void highbd_spatial_denoise_point(uint16_t *src_ptr, const int stride,
5035                                          const int strength) {
5036   int sum_weight = 0;
5037   int sum_val = 0;
5038   int thresh = strength;
5039   int kernal_size = 5;
5040   int half_k_size = 2;
5041   int i, j;
5042   int max_diff = 0;
5043   uint16_t *tmp_ptr;
5044   uint8_t *kernal_ptr;
5045 
5046   // Find the maximum deviation from the source point in the locale.
5047   tmp_ptr = src_ptr - (stride * (half_k_size + 1)) - (half_k_size + 1);
5048   for (i = 0; i < kernal_size + 2; ++i) {
5049     for (j = 0; j < kernal_size + 2; ++j) {
5050       max_diff = VPXMAX(max_diff, abs((int)src_ptr - (int)tmp_ptr[j]));
5051     }
5052     tmp_ptr += stride;
5053   }
5054 
5055   // Select the kernel size.
5056   if (max_diff > (strength + (strength >> 1))) {
5057     kernal_size = 3;
5058     half_k_size = 1;
5059     thresh = thresh >> 1;
5060   }
5061   kernal_ptr = (kernal_size == 3) ? dn_kernal_3 : dn_kernal_5;
5062 
5063   // Apply the kernel
5064   tmp_ptr = src_ptr - (stride * half_k_size) - half_k_size;
5065   for (i = 0; i < kernal_size; ++i) {
5066     for (j = 0; j < kernal_size; ++j) {
5067       add_denoise_point((int)*src_ptr, (int)tmp_ptr[j], thresh, *kernal_ptr,
5068                         &sum_val, &sum_weight);
5069       ++kernal_ptr;
5070     }
5071     tmp_ptr += stride;
5072   }
5073 
5074   // Update the source value with the new filtered value
5075   *src_ptr = (uint16_t)((sum_val + (sum_weight >> 1)) / sum_weight);
5076 }
5077 #endif  // CONFIG_VP9_HIGHBITDEPTH
5078 
5079 // Apply thresholded spatial noise suppression to a given buffer.
5080 static void spatial_denoise_buffer(VP9_COMP *cpi, uint8_t *buffer,
5081                                    const int stride, const int width,
5082                                    const int height, const int strength) {
5083   VP9_COMMON *const cm = &cpi->common;
5084   uint8_t *src_ptr = buffer;
5085   int row;
5086   int col;
5087 
5088   for (row = 0; row < height; ++row) {
5089     for (col = 0; col < width; ++col) {
5090 #if CONFIG_VP9_HIGHBITDEPTH
5091       if (cm->use_highbitdepth)
5092         highbd_spatial_denoise_point(CONVERT_TO_SHORTPTR(&src_ptr[col]), stride,
5093                                      strength);
5094       else
5095         spatial_denoise_point(&src_ptr[col], stride, strength);
5096 #else
5097       spatial_denoise_point(&src_ptr[col], stride, strength);
5098 #endif  // CONFIG_VP9_HIGHBITDEPTH
5099     }
5100     src_ptr += stride;
5101   }
5102 }
5103 
5104 // Apply thresholded spatial noise suppression to source.
5105 static void spatial_denoise_frame(VP9_COMP *cpi) {
5106   YV12_BUFFER_CONFIG *src = cpi->Source;
5107   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
5108   TWO_PASS *const twopass = &cpi->twopass;
5109   VP9_COMMON *const cm = &cpi->common;
5110 
5111   // Base the filter strength on the current active max Q.
5112   const int q = (int)(vp9_convert_qindex_to_q(twopass->active_worst_quality,
5113                                               cm->bit_depth));
5114   int strength =
5115       VPXMAX(oxcf->arnr_strength >> 2, VPXMIN(oxcf->arnr_strength, (q >> 4)));
5116 
5117   // Denoise each of Y,U and V buffers.
5118   spatial_denoise_buffer(cpi, src->y_buffer, src->y_stride, src->y_width,
5119                          src->y_height, strength);
5120 
5121   strength += (strength >> 1);
5122   spatial_denoise_buffer(cpi, src->u_buffer, src->uv_stride, src->uv_width,
5123                          src->uv_height, strength << 1);
5124 
5125   spatial_denoise_buffer(cpi, src->v_buffer, src->uv_stride, src->uv_width,
5126                          src->uv_height, strength << 1);
5127 }
5128 #endif  // ENABLE_KF_DENOISE
5129 
5130 #if !CONFIG_REALTIME_ONLY
5131 static void vp9_try_disable_lookahead_aq(VP9_COMP *cpi, size_t *size,
5132                                          uint8_t *dest) {
5133   if (cpi->common.seg.enabled)
5134     if (ALT_REF_AQ_PROTECT_GAIN) {
5135       size_t nsize = *size;
5136       int overhead;
5137 
5138       // TODO(yuryg): optimize this, as
5139       // we don't really need to repack
5140 
5141       save_coding_context(cpi);
5142       vp9_disable_segmentation(&cpi->common.seg);
5143       vp9_pack_bitstream(cpi, dest, &nsize);
5144       restore_coding_context(cpi);
5145 
5146       overhead = (int)*size - (int)nsize;
5147 
5148       if (vp9_alt_ref_aq_disable_if(cpi->alt_ref_aq, overhead, (int)*size))
5149         vp9_encode_frame(cpi);
5150       else
5151         vp9_enable_segmentation(&cpi->common.seg);
5152     }
5153 }
5154 #endif
5155 
5156 static void set_frame_index(VP9_COMP *cpi, VP9_COMMON *cm) {
5157   RefCntBuffer *const ref_buffer = get_ref_cnt_buffer(cm, cm->new_fb_idx);
5158 
5159   if (ref_buffer) {
5160     const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
5161     ref_buffer->frame_index =
5162         cm->current_video_frame + gf_group->arf_src_offset[gf_group->index];
5163     ref_buffer->frame_coding_index = cm->current_frame_coding_index;
5164   }
5165 }
5166 
5167 static void set_mb_ssim_rdmult_scaling(VP9_COMP *cpi) {
5168   VP9_COMMON *cm = &cpi->common;
5169   ThreadData *td = &cpi->td;
5170   MACROBLOCK *x = &td->mb;
5171   MACROBLOCKD *xd = &x->e_mbd;
5172   uint8_t *y_buffer = cpi->Source->y_buffer;
5173   const int y_stride = cpi->Source->y_stride;
5174   const int block_size = BLOCK_16X16;
5175 
5176   const int num_8x8_w = num_8x8_blocks_wide_lookup[block_size];
5177   const int num_8x8_h = num_8x8_blocks_high_lookup[block_size];
5178   const int num_cols = (cm->mi_cols + num_8x8_w - 1) / num_8x8_w;
5179   const int num_rows = (cm->mi_rows + num_8x8_h - 1) / num_8x8_h;
5180   double log_sum = 0.0;
5181   int row, col;
5182 
5183   // Loop through each 64x64 block.
5184   for (row = 0; row < num_rows; ++row) {
5185     for (col = 0; col < num_cols; ++col) {
5186       int mi_row, mi_col;
5187       double var = 0.0, num_of_var = 0.0;
5188       const int index = row * num_cols + col;
5189 
5190       for (mi_row = row * num_8x8_h;
5191            mi_row < cm->mi_rows && mi_row < (row + 1) * num_8x8_h; ++mi_row) {
5192         for (mi_col = col * num_8x8_w;
5193              mi_col < cm->mi_cols && mi_col < (col + 1) * num_8x8_w; ++mi_col) {
5194           struct buf_2d buf;
5195           const int row_offset_y = mi_row << 3;
5196           const int col_offset_y = mi_col << 3;
5197 
5198           buf.buf = y_buffer + row_offset_y * y_stride + col_offset_y;
5199           buf.stride = y_stride;
5200 
5201           // In order to make SSIM_VAR_SCALE in a same scale for both 8 bit
5202           // and high bit videos, the variance needs to be divided by 2.0 or
5203           // 64.0 separately.
5204           // TODO(sdeng): need to tune for 12bit videos.
5205 #if CONFIG_VP9_HIGHBITDEPTH
5206           if (cpi->Source->flags & YV12_FLAG_HIGHBITDEPTH)
5207             var += vp9_high_get_sby_variance(cpi, &buf, BLOCK_8X8, xd->bd);
5208           else
5209 #endif
5210             var += vp9_get_sby_variance(cpi, &buf, BLOCK_8X8);
5211 
5212           num_of_var += 1.0;
5213         }
5214       }
5215       var = var / num_of_var / 64.0;
5216 
5217       // Curve fitting with an exponential model on all 16x16 blocks from the
5218       // Midres dataset.
5219       var = 67.035434 * (1 - exp(-0.0021489 * var)) + 17.492222;
5220       cpi->mi_ssim_rdmult_scaling_factors[index] = var;
5221       log_sum += log(var);
5222     }
5223   }
5224   log_sum = exp(log_sum / (double)(num_rows * num_cols));
5225 
5226   for (row = 0; row < num_rows; ++row) {
5227     for (col = 0; col < num_cols; ++col) {
5228       const int index = row * num_cols + col;
5229       cpi->mi_ssim_rdmult_scaling_factors[index] /= log_sum;
5230     }
5231   }
5232 
5233   (void)xd;
5234 }
5235 
5236 // Process the wiener variance in 16x16 block basis.
5237 static int qsort_comp(const void *elem1, const void *elem2) {
5238   int a = *((const int *)elem1);
5239   int b = *((const int *)elem2);
5240   if (a > b) return 1;
5241   if (a < b) return -1;
5242   return 0;
5243 }
5244 
5245 static void init_mb_wiener_var_buffer(VP9_COMP *cpi) {
5246   VP9_COMMON *cm = &cpi->common;
5247 
5248   if (cpi->mb_wiener_variance && cpi->mb_wiener_var_rows >= cm->mb_rows &&
5249       cpi->mb_wiener_var_cols >= cm->mb_cols)
5250     return;
5251 
5252   vpx_free(cpi->mb_wiener_variance);
5253   cpi->mb_wiener_variance = NULL;
5254 
5255   CHECK_MEM_ERROR(
5256       cm, cpi->mb_wiener_variance,
5257       vpx_calloc(cm->mb_rows * cm->mb_cols, sizeof(*cpi->mb_wiener_variance)));
5258   cpi->mb_wiener_var_rows = cm->mb_rows;
5259   cpi->mb_wiener_var_cols = cm->mb_cols;
5260 }
5261 
5262 static void set_mb_wiener_variance(VP9_COMP *cpi) {
5263   VP9_COMMON *cm = &cpi->common;
5264   uint8_t *buffer = cpi->Source->y_buffer;
5265   int buf_stride = cpi->Source->y_stride;
5266 
5267 #if CONFIG_VP9_HIGHBITDEPTH
5268   ThreadData *td = &cpi->td;
5269   MACROBLOCK *x = &td->mb;
5270   MACROBLOCKD *xd = &x->e_mbd;
5271   DECLARE_ALIGNED(16, uint16_t, zero_pred16[32 * 32]);
5272   DECLARE_ALIGNED(16, uint8_t, zero_pred8[32 * 32]);
5273   uint8_t *zero_pred;
5274 #else
5275   DECLARE_ALIGNED(16, uint8_t, zero_pred[32 * 32]);
5276 #endif
5277 
5278   DECLARE_ALIGNED(16, int16_t, src_diff[32 * 32]);
5279   DECLARE_ALIGNED(16, tran_low_t, coeff[32 * 32]);
5280 
5281   int mb_row, mb_col, count = 0;
5282   // Hard coded operating block size
5283   const int block_size = 16;
5284   const int coeff_count = block_size * block_size;
5285   const TX_SIZE tx_size = TX_16X16;
5286 
5287 #if CONFIG_VP9_HIGHBITDEPTH
5288   xd->cur_buf = cpi->Source;
5289   if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
5290     zero_pred = CONVERT_TO_BYTEPTR(zero_pred16);
5291     memset(zero_pred16, 0, sizeof(*zero_pred16) * coeff_count);
5292   } else {
5293     zero_pred = zero_pred8;
5294     memset(zero_pred8, 0, sizeof(*zero_pred8) * coeff_count);
5295   }
5296 #else
5297   memset(zero_pred, 0, sizeof(*zero_pred) * coeff_count);
5298 #endif
5299 
5300   cpi->norm_wiener_variance = 0;
5301 
5302   for (mb_row = 0; mb_row < cm->mb_rows; ++mb_row) {
5303     for (mb_col = 0; mb_col < cm->mb_cols; ++mb_col) {
5304       int idx;
5305       int16_t median_val = 0;
5306       uint8_t *mb_buffer =
5307           buffer + mb_row * block_size * buf_stride + mb_col * block_size;
5308       int64_t wiener_variance = 0;
5309 
5310 #if CONFIG_VP9_HIGHBITDEPTH
5311       if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
5312         vpx_highbd_subtract_block(block_size, block_size, src_diff, block_size,
5313                                   mb_buffer, buf_stride, zero_pred, block_size,
5314                                   xd->bd);
5315         highbd_wht_fwd_txfm(src_diff, block_size, coeff, tx_size);
5316       } else {
5317         vpx_subtract_block(block_size, block_size, src_diff, block_size,
5318                            mb_buffer, buf_stride, zero_pred, block_size);
5319         wht_fwd_txfm(src_diff, block_size, coeff, tx_size);
5320       }
5321 #else
5322       vpx_subtract_block(block_size, block_size, src_diff, block_size,
5323                          mb_buffer, buf_stride, zero_pred, block_size);
5324       wht_fwd_txfm(src_diff, block_size, coeff, tx_size);
5325 #endif  // CONFIG_VP9_HIGHBITDEPTH
5326 
5327       coeff[0] = 0;
5328       for (idx = 1; idx < coeff_count; ++idx) coeff[idx] = abs(coeff[idx]);
5329 
5330       qsort(coeff, coeff_count - 1, sizeof(*coeff), qsort_comp);
5331 
5332       // Noise level estimation
5333       median_val = coeff[coeff_count / 2];
5334 
5335       // Wiener filter
5336       for (idx = 1; idx < coeff_count; ++idx) {
5337         int64_t sqr_coeff = (int64_t)coeff[idx] * coeff[idx];
5338         int64_t tmp_coeff = (int64_t)coeff[idx];
5339         if (median_val) {
5340           tmp_coeff = (sqr_coeff * coeff[idx]) /
5341                       (sqr_coeff + (int64_t)median_val * median_val);
5342         }
5343         wiener_variance += tmp_coeff * tmp_coeff;
5344       }
5345       cpi->mb_wiener_variance[mb_row * cm->mb_cols + mb_col] =
5346           wiener_variance / coeff_count;
5347       cpi->norm_wiener_variance +=
5348           cpi->mb_wiener_variance[mb_row * cm->mb_cols + mb_col];
5349       ++count;
5350     }
5351   }
5352 
5353   if (count) cpi->norm_wiener_variance /= count;
5354   cpi->norm_wiener_variance = VPXMAX(1, cpi->norm_wiener_variance);
5355 }
5356 
5357 #if !CONFIG_REALTIME_ONLY
5358 static void update_encode_frame_result_basic(
5359     FRAME_UPDATE_TYPE update_type, int show_idx, int quantize_index,
5360     ENCODE_FRAME_RESULT *encode_frame_result) {
5361   encode_frame_result->show_idx = show_idx;
5362   encode_frame_result->update_type = update_type;
5363   encode_frame_result->quantize_index = quantize_index;
5364 }
5365 
5366 #if CONFIG_RATE_CTRL
5367 static void yv12_buffer_to_image_buffer(const YV12_BUFFER_CONFIG *yv12_buffer,
5368                                         IMAGE_BUFFER *image_buffer) {
5369   const uint8_t *src_buf_ls[3] = { yv12_buffer->y_buffer, yv12_buffer->u_buffer,
5370                                    yv12_buffer->v_buffer };
5371   const int src_stride_ls[3] = { yv12_buffer->y_stride, yv12_buffer->uv_stride,
5372                                  yv12_buffer->uv_stride };
5373   const int w_ls[3] = { yv12_buffer->y_crop_width, yv12_buffer->uv_crop_width,
5374                         yv12_buffer->uv_crop_width };
5375   const int h_ls[3] = { yv12_buffer->y_crop_height, yv12_buffer->uv_crop_height,
5376                         yv12_buffer->uv_crop_height };
5377   int plane;
5378   for (plane = 0; plane < 3; ++plane) {
5379     const int src_stride = src_stride_ls[plane];
5380     const int w = w_ls[plane];
5381     const int h = h_ls[plane];
5382     const uint8_t *src_buf = src_buf_ls[plane];
5383     uint8_t *dst_buf = image_buffer->plane_buffer[plane];
5384     int r;
5385     assert(image_buffer->plane_width[plane] == w);
5386     assert(image_buffer->plane_height[plane] == h);
5387     for (r = 0; r < h; ++r) {
5388       memcpy(dst_buf, src_buf, sizeof(*src_buf) * w);
5389       src_buf += src_stride;
5390       dst_buf += w;
5391     }
5392   }
5393 }
5394 // This function will update extra information specific for simple_encode APIs
5395 static void update_encode_frame_result_simple_encode(
5396     int ref_frame_flags, FRAME_UPDATE_TYPE update_type,
5397     const YV12_BUFFER_CONFIG *source_frame, const RefCntBuffer *coded_frame_buf,
5398     RefCntBuffer *ref_frame_bufs[MAX_INTER_REF_FRAMES], int quantize_index,
5399     uint32_t bit_depth, uint32_t input_bit_depth, const FRAME_COUNTS *counts,
5400     const PARTITION_INFO *partition_info,
5401     const MOTION_VECTOR_INFO *motion_vector_info,
5402     const TplDepStats *tpl_stats_info,
5403     ENCODE_FRAME_RESULT *encode_frame_result) {
5404   PSNR_STATS psnr;
5405   update_encode_frame_result_basic(update_type, coded_frame_buf->frame_index,
5406                                    quantize_index, encode_frame_result);
5407 #if CONFIG_VP9_HIGHBITDEPTH
5408   vpx_calc_highbd_psnr(source_frame, &coded_frame_buf->buf, &psnr, bit_depth,
5409                        input_bit_depth);
5410 #else   // CONFIG_VP9_HIGHBITDEPTH
5411   (void)bit_depth;
5412   (void)input_bit_depth;
5413   vpx_calc_psnr(source_frame, &coded_frame_buf->buf, &psnr);
5414 #endif  // CONFIG_VP9_HIGHBITDEPTH
5415   encode_frame_result->frame_coding_index = coded_frame_buf->frame_coding_index;
5416 
5417   vp9_get_ref_frame_info(update_type, ref_frame_flags, ref_frame_bufs,
5418                          encode_frame_result->ref_frame_coding_indexes,
5419                          encode_frame_result->ref_frame_valid_list);
5420 
5421   encode_frame_result->psnr = psnr.psnr[0];
5422   encode_frame_result->sse = psnr.sse[0];
5423   encode_frame_result->frame_counts = *counts;
5424   encode_frame_result->partition_info = partition_info;
5425   encode_frame_result->motion_vector_info = motion_vector_info;
5426   encode_frame_result->tpl_stats_info = tpl_stats_info;
5427   if (encode_frame_result->coded_frame.allocated) {
5428     yv12_buffer_to_image_buffer(&coded_frame_buf->buf,
5429                                 &encode_frame_result->coded_frame);
5430   }
5431 }
5432 #endif  // CONFIG_RATE_CTRL
5433 #endif  // !CONFIG_REALTIME_ONLY
5434 
5435 static void encode_frame_to_data_rate(
5436     VP9_COMP *cpi, size_t *size, uint8_t *dest, unsigned int *frame_flags,
5437     ENCODE_FRAME_RESULT *encode_frame_result) {
5438   VP9_COMMON *const cm = &cpi->common;
5439   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
5440   struct segmentation *const seg = &cm->seg;
5441   TX_SIZE t;
5442 
5443   // SVC: skip encoding of enhancement layer if the layer target bandwidth = 0.
5444   // No need to set svc.skip_enhancement_layer if whole superframe will be
5445   // dropped.
5446   if (cpi->use_svc && cpi->svc.spatial_layer_id > 0 &&
5447       cpi->oxcf.target_bandwidth == 0 &&
5448       !(cpi->svc.framedrop_mode != LAYER_DROP &&
5449         (cpi->svc.framedrop_mode != CONSTRAINED_FROM_ABOVE_DROP ||
5450          cpi->svc
5451              .force_drop_constrained_from_above[cpi->svc.number_spatial_layers -
5452                                                 1]) &&
5453         cpi->svc.drop_spatial_layer[0])) {
5454     cpi->svc.skip_enhancement_layer = 1;
5455     vp9_rc_postencode_update_drop_frame(cpi);
5456     cpi->ext_refresh_frame_flags_pending = 0;
5457     cpi->last_frame_dropped = 1;
5458     cpi->svc.last_layer_dropped[cpi->svc.spatial_layer_id] = 1;
5459     cpi->svc.drop_spatial_layer[cpi->svc.spatial_layer_id] = 1;
5460     vp9_inc_frame_in_layer(cpi);
5461     return;
5462   }
5463 
5464   set_ext_overrides(cpi);
5465   vpx_clear_system_state();
5466 
5467 #ifdef ENABLE_KF_DENOISE
5468   // Spatial denoise of key frame.
5469   if (is_spatial_denoise_enabled(cpi)) spatial_denoise_frame(cpi);
5470 #endif
5471 
5472   if (cm->show_existing_frame == 0) {
5473     // Update frame index
5474     set_frame_index(cpi, cm);
5475 
5476     // Set the arf sign bias for this frame.
5477     set_ref_sign_bias(cpi);
5478   }
5479 
5480   // Set default state for segment based loop filter update flags.
5481   cm->lf.mode_ref_delta_update = 0;
5482 
5483   if (cpi->oxcf.pass == 2 && cpi->sf.adaptive_interp_filter_search)
5484     cpi->sf.interp_filter_search_mask = setup_interp_filter_search_mask(cpi);
5485 
5486   // Set various flags etc to special state if it is a key frame.
5487   if (frame_is_intra_only(cm)) {
5488     // Reset the loop filter deltas and segmentation map.
5489     vp9_reset_segment_features(&cm->seg);
5490 
5491     // If segmentation is enabled force a map update for key frames.
5492     if (seg->enabled) {
5493       seg->update_map = 1;
5494       seg->update_data = 1;
5495     }
5496 
5497     // The alternate reference frame cannot be active for a key frame.
5498     cpi->rc.source_alt_ref_active = 0;
5499 
5500     cm->error_resilient_mode = oxcf->error_resilient_mode;
5501     cm->frame_parallel_decoding_mode = oxcf->frame_parallel_decoding_mode;
5502 
5503     // By default, encoder assumes decoder can use prev_mi.
5504     if (cm->error_resilient_mode) {
5505       cm->frame_parallel_decoding_mode = 1;
5506       cm->reset_frame_context = 0;
5507       cm->refresh_frame_context = 0;
5508     } else if (cm->intra_only) {
5509       // Only reset the current context.
5510       cm->reset_frame_context = 2;
5511     }
5512   }
5513 
5514   if (oxcf->tuning == VP8_TUNE_SSIM) set_mb_ssim_rdmult_scaling(cpi);
5515 
5516   if (oxcf->aq_mode == PERCEPTUAL_AQ) {
5517     init_mb_wiener_var_buffer(cpi);
5518     set_mb_wiener_variance(cpi);
5519   }
5520 
5521   vpx_clear_system_state();
5522 
5523 #if CONFIG_INTERNAL_STATS
5524   memset(cpi->mode_chosen_counts, 0,
5525          MAX_MODES * sizeof(*cpi->mode_chosen_counts));
5526 #endif
5527 #if CONFIG_CONSISTENT_RECODE
5528   // Backup to ensure consistency between recodes
5529   save_encode_params(cpi);
5530 #elif CONFIG_RATE_CTRL
5531   if (cpi->oxcf.use_simple_encode_api) {
5532     save_encode_params(cpi);
5533   }
5534 #endif
5535 
5536   if (cpi->sf.recode_loop == DISALLOW_RECODE) {
5537     if (!encode_without_recode_loop(cpi, size, dest)) return;
5538   } else {
5539 #if !CONFIG_REALTIME_ONLY
5540 #if CONFIG_RATE_CTRL
5541     encode_with_recode_loop(cpi, size, dest, &encode_frame_result->rq_history);
5542 #else   // CONFIG_RATE_CTRL
5543     encode_with_recode_loop(cpi, size, dest);
5544 #endif  // CONFIG_RATE_CTRL
5545 #endif  // !CONFIG_REALTIME_ONLY
5546   }
5547 
5548   // TODO(jingning): When using show existing frame mode, we assume that the
5549   // current ARF will be directly used as the final reconstructed frame. This is
5550   // an encoder control scheme. One could in principle explore other
5551   // possibilities to arrange the reference frame buffer and their coding order.
5552   if (cm->show_existing_frame) {
5553     ref_cnt_fb(cm->buffer_pool->frame_bufs, &cm->new_fb_idx,
5554                cm->ref_frame_map[cpi->alt_fb_idx]);
5555   }
5556 
5557 #if !CONFIG_REALTIME_ONLY
5558   // Disable segmentation if it decrease rate/distortion ratio
5559   if (cpi->oxcf.aq_mode == LOOKAHEAD_AQ)
5560     vp9_try_disable_lookahead_aq(cpi, size, dest);
5561 #endif
5562 
5563 #if CONFIG_VP9_TEMPORAL_DENOISING
5564 #ifdef OUTPUT_YUV_DENOISED
5565   if (oxcf->noise_sensitivity > 0 && denoise_svc(cpi)) {
5566     vpx_write_yuv_frame(yuv_denoised_file,
5567                         &cpi->denoiser.running_avg_y[INTRA_FRAME]);
5568   }
5569 #endif
5570 #endif
5571 #ifdef OUTPUT_YUV_SKINMAP
5572   if (cpi->common.current_video_frame > 1) {
5573     vp9_output_skin_map(cpi, yuv_skinmap_file);
5574   }
5575 #endif
5576 
5577   // Special case code to reduce pulsing when key frames are forced at a
5578   // fixed interval. Note the reconstruction error if it is the frame before
5579   // the force key frame
5580   if (cpi->rc.next_key_frame_forced && cpi->rc.frames_to_key == 1) {
5581 #if CONFIG_VP9_HIGHBITDEPTH
5582     if (cm->use_highbitdepth) {
5583       cpi->ambient_err =
5584           vpx_highbd_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
5585     } else {
5586       cpi->ambient_err = vpx_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
5587     }
5588 #else
5589     cpi->ambient_err = vpx_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
5590 #endif  // CONFIG_VP9_HIGHBITDEPTH
5591   }
5592 
5593   // If the encoder forced a KEY_FRAME decision
5594   if (cm->frame_type == KEY_FRAME) cpi->refresh_last_frame = 1;
5595 
5596   cm->frame_to_show = get_frame_new_buffer(cm);
5597   cm->frame_to_show->color_space = cm->color_space;
5598   cm->frame_to_show->color_range = cm->color_range;
5599   cm->frame_to_show->render_width = cm->render_width;
5600   cm->frame_to_show->render_height = cm->render_height;
5601 
5602   // Pick the loop filter level for the frame.
5603   loopfilter_frame(cpi, cm);
5604 
5605   if (cpi->rc.use_post_encode_drop) save_coding_context(cpi);
5606 
5607   // build the bitstream
5608   vp9_pack_bitstream(cpi, dest, size);
5609 
5610   {
5611     const RefCntBuffer *coded_frame_buf =
5612         get_ref_cnt_buffer(cm, cm->new_fb_idx);
5613     vpx_codec_err_t codec_status = vp9_extrc_update_encodeframe_result(
5614         &cpi->ext_ratectrl, (*size) << 3, cpi->Source, &coded_frame_buf->buf,
5615         cm->bit_depth, cpi->oxcf.input_bit_depth, cm->base_qindex);
5616     if (codec_status != VPX_CODEC_OK) {
5617       vpx_internal_error(&cm->error, codec_status,
5618                          "vp9_extrc_update_encodeframe_result() failed");
5619     }
5620   }
5621 #if CONFIG_REALTIME_ONLY
5622   (void)encode_frame_result;
5623   assert(encode_frame_result == NULL);
5624 #else  // CONFIG_REALTIME_ONLY
5625   if (encode_frame_result != NULL) {
5626     const RefCntBuffer *coded_frame_buf =
5627         get_ref_cnt_buffer(cm, cm->new_fb_idx);
5628     RefCntBuffer *ref_frame_bufs[MAX_INTER_REF_FRAMES];
5629     FRAME_UPDATE_TYPE update_type =
5630         cpi->twopass.gf_group.update_type[cpi->twopass.gf_group.index];
5631     int quantize_index = vp9_get_quantizer(cpi);
5632     get_ref_frame_bufs(cpi, ref_frame_bufs);
5633     // update_encode_frame_result() depends on twopass.gf_group.index and
5634     // cm->new_fb_idx, cpi->Source, cpi->lst_fb_idx, cpi->gld_fb_idx and
5635     // cpi->alt_fb_idx are updated for current frame and have
5636     // not been updated for the next frame yet.
5637     // The update locations are as follows.
5638     // 1) twopass.gf_group.index is initialized at define_gf_group by vp9_zero()
5639     // for the first frame in the gf_group and is updated for the next frame at
5640     // vp9_twopass_postencode_update().
5641     // 2) cpi->Source is updated at the beginning of vp9_get_compressed_data()
5642     // 3) cm->new_fb_idx is updated at the beginning of
5643     // vp9_get_compressed_data() by get_free_fb(cm).
5644     // 4) cpi->lst_fb_idx/gld_fb_idx/alt_fb_idx will be updated for the next
5645     // frame at vp9_update_reference_frames().
5646     // This function needs to be called before vp9_update_reference_frames().
5647     // TODO(angiebird): Improve the codebase to make the update of frame
5648     // dependent variables more robust.
5649 
5650     update_encode_frame_result_basic(update_type, coded_frame_buf->frame_index,
5651                                      quantize_index, encode_frame_result);
5652 #if CONFIG_RATE_CTRL
5653     if (cpi->oxcf.use_simple_encode_api) {
5654       const int ref_frame_flags = get_ref_frame_flags(cpi);
5655       update_encode_frame_result_simple_encode(
5656           ref_frame_flags,
5657           cpi->twopass.gf_group.update_type[cpi->twopass.gf_group.index],
5658           cpi->Source, coded_frame_buf, ref_frame_bufs, quantize_index,
5659           cm->bit_depth, cpi->oxcf.input_bit_depth, cpi->td.counts,
5660           cpi->partition_info, cpi->motion_vector_info, cpi->tpl_stats_info,
5661           encode_frame_result);
5662     }
5663 #endif  // CONFIG_RATE_CTRL
5664   }
5665 #endif  // CONFIG_REALTIME_ONLY
5666 
5667   if (cpi->rc.use_post_encode_drop && cm->base_qindex < cpi->rc.worst_quality &&
5668       cpi->svc.spatial_layer_id == 0 && post_encode_drop_cbr(cpi, size)) {
5669     restore_coding_context(cpi);
5670     return;
5671   }
5672 
5673   cpi->last_frame_dropped = 0;
5674   cpi->svc.last_layer_dropped[cpi->svc.spatial_layer_id] = 0;
5675   if (cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1)
5676     cpi->svc.num_encoded_top_layer++;
5677 
5678   // Keep track of the frame buffer index updated/refreshed for the
5679   // current encoded TL0 superframe.
5680   if (cpi->svc.temporal_layer_id == 0) {
5681     if (cpi->refresh_last_frame)
5682       cpi->svc.fb_idx_upd_tl0[cpi->svc.spatial_layer_id] = cpi->lst_fb_idx;
5683     else if (cpi->refresh_golden_frame)
5684       cpi->svc.fb_idx_upd_tl0[cpi->svc.spatial_layer_id] = cpi->gld_fb_idx;
5685     else if (cpi->refresh_alt_ref_frame)
5686       cpi->svc.fb_idx_upd_tl0[cpi->svc.spatial_layer_id] = cpi->alt_fb_idx;
5687   }
5688 
5689   if (cm->seg.update_map) update_reference_segmentation_map(cpi);
5690 
5691   if (frame_is_intra_only(cm) == 0) {
5692     release_scaled_references(cpi);
5693   }
5694   vp9_update_reference_frames(cpi);
5695 
5696   if (!cm->show_existing_frame) {
5697     for (t = TX_4X4; t <= TX_32X32; ++t) {
5698       full_to_model_counts(cpi->td.counts->coef[t],
5699                            cpi->td.rd_counts.coef_counts[t]);
5700     }
5701 
5702     if (!cm->error_resilient_mode && !cm->frame_parallel_decoding_mode) {
5703       if (!frame_is_intra_only(cm)) {
5704         vp9_adapt_mode_probs(cm);
5705         vp9_adapt_mv_probs(cm, cm->allow_high_precision_mv);
5706       }
5707       vp9_adapt_coef_probs(cm);
5708     }
5709   }
5710 
5711   cpi->ext_refresh_frame_flags_pending = 0;
5712 
5713   if (cpi->refresh_golden_frame == 1)
5714     cpi->frame_flags |= FRAMEFLAGS_GOLDEN;
5715   else
5716     cpi->frame_flags &= ~FRAMEFLAGS_GOLDEN;
5717 
5718   if (cpi->refresh_alt_ref_frame == 1)
5719     cpi->frame_flags |= FRAMEFLAGS_ALTREF;
5720   else
5721     cpi->frame_flags &= ~FRAMEFLAGS_ALTREF;
5722 
5723   cpi->ref_frame_flags = get_ref_frame_flags(cpi);
5724 
5725   cm->last_frame_type = cm->frame_type;
5726 
5727   vp9_rc_postencode_update(cpi, *size);
5728 
5729   if (cpi->compute_frame_low_motion_onepass && oxcf->pass == 0 &&
5730       !frame_is_intra_only(cm) &&
5731       (!cpi->use_svc ||
5732        (cpi->use_svc &&
5733         !cpi->svc.layer_context[cpi->svc.temporal_layer_id].is_key_frame &&
5734         cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1))) {
5735     vp9_compute_frame_low_motion(cpi);
5736   }
5737 
5738   *size = VPXMAX(1, *size);
5739 
5740 #if 0
5741   output_frame_level_debug_stats(cpi);
5742 #endif
5743 
5744   if (cm->frame_type == KEY_FRAME) {
5745     // Tell the caller that the frame was coded as a key frame
5746     *frame_flags = cpi->frame_flags | FRAMEFLAGS_KEY;
5747   } else {
5748     *frame_flags = cpi->frame_flags & ~FRAMEFLAGS_KEY;
5749   }
5750 
5751   // Clear the one shot update flags for segmentation map and mode/ref loop
5752   // filter deltas.
5753   cm->seg.update_map = 0;
5754   cm->seg.update_data = 0;
5755   cm->lf.mode_ref_delta_update = 0;
5756 
5757   // keep track of the last coded dimensions
5758   cm->last_width = cm->width;
5759   cm->last_height = cm->height;
5760 
5761   // reset to normal state now that we are done.
5762   if (!cm->show_existing_frame) {
5763     cm->last_show_frame = cm->show_frame;
5764     cm->prev_frame = cm->cur_frame;
5765   }
5766 
5767   if (cm->show_frame) {
5768     vp9_swap_mi_and_prev_mi(cm);
5769     if (cpi->use_svc) vp9_inc_frame_in_layer(cpi);
5770   }
5771   update_frame_indexes(cm, cm->show_frame);
5772 
5773   if (cpi->use_svc) {
5774     cpi->svc
5775         .layer_context[cpi->svc.spatial_layer_id *
5776                            cpi->svc.number_temporal_layers +
5777                        cpi->svc.temporal_layer_id]
5778         .last_frame_type = cm->frame_type;
5779     // Reset layer_sync back to 0 for next frame.
5780     cpi->svc.spatial_layer_sync[cpi->svc.spatial_layer_id] = 0;
5781   }
5782 
5783   cpi->force_update_segmentation = 0;
5784 
5785 #if !CONFIG_REALTIME_ONLY
5786   if (cpi->oxcf.aq_mode == LOOKAHEAD_AQ)
5787     vp9_alt_ref_aq_unset_all(cpi->alt_ref_aq, cpi);
5788 #endif
5789 
5790   cpi->svc.previous_frame_is_intra_only = cm->intra_only;
5791   cpi->svc.set_intra_only_frame = 0;
5792 }
5793 
5794 static void SvcEncode(VP9_COMP *cpi, size_t *size, uint8_t *dest,
5795                       unsigned int *frame_flags) {
5796   vp9_rc_get_svc_params(cpi);
5797   encode_frame_to_data_rate(cpi, size, dest, frame_flags,
5798                             /*encode_frame_result = */ NULL);
5799 }
5800 
5801 static void Pass0Encode(VP9_COMP *cpi, size_t *size, uint8_t *dest,
5802                         unsigned int *frame_flags) {
5803   if (cpi->oxcf.rc_mode == VPX_CBR) {
5804     vp9_rc_get_one_pass_cbr_params(cpi);
5805   } else {
5806     vp9_rc_get_one_pass_vbr_params(cpi);
5807   }
5808   encode_frame_to_data_rate(cpi, size, dest, frame_flags,
5809                             /*encode_frame_result = */ NULL);
5810 }
5811 
5812 #if !CONFIG_REALTIME_ONLY
5813 static void Pass2Encode(VP9_COMP *cpi, size_t *size, uint8_t *dest,
5814                         unsigned int *frame_flags,
5815                         ENCODE_FRAME_RESULT *encode_frame_result) {
5816   cpi->allow_encode_breakout = ENCODE_BREAKOUT_ENABLED;
5817 
5818   if (cpi->common.current_frame_coding_index == 0) {
5819     VP9_COMMON *cm = &cpi->common;
5820     const vpx_codec_err_t codec_status = vp9_extrc_send_firstpass_stats(
5821         &cpi->ext_ratectrl, &cpi->twopass.first_pass_info);
5822     if (codec_status != VPX_CODEC_OK) {
5823       vpx_internal_error(&cm->error, codec_status,
5824                          "vp9_extrc_send_firstpass_stats() failed");
5825     }
5826   }
5827 #if CONFIG_MISMATCH_DEBUG
5828   mismatch_move_frame_idx_w();
5829 #endif
5830   encode_frame_to_data_rate(cpi, size, dest, frame_flags, encode_frame_result);
5831 }
5832 #endif  // !CONFIG_REALTIME_ONLY
5833 
5834 int vp9_receive_raw_frame(VP9_COMP *cpi, vpx_enc_frame_flags_t frame_flags,
5835                           YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
5836                           int64_t end_time) {
5837   VP9_COMMON *const cm = &cpi->common;
5838   struct vpx_usec_timer timer;
5839   int res = 0;
5840   const int subsampling_x = sd->subsampling_x;
5841   const int subsampling_y = sd->subsampling_y;
5842 #if CONFIG_VP9_HIGHBITDEPTH
5843   const int use_highbitdepth = (sd->flags & YV12_FLAG_HIGHBITDEPTH) != 0;
5844 #else
5845   const int use_highbitdepth = 0;
5846 #endif
5847 
5848   update_initial_width(cpi, use_highbitdepth, subsampling_x, subsampling_y);
5849 #if CONFIG_VP9_TEMPORAL_DENOISING
5850   setup_denoiser_buffer(cpi);
5851 #endif
5852 
5853   alloc_raw_frame_buffers(cpi);
5854 
5855   vpx_usec_timer_start(&timer);
5856 
5857   if (vp9_lookahead_push(cpi->lookahead, sd, time_stamp, end_time,
5858                          use_highbitdepth, frame_flags))
5859     res = -1;
5860   vpx_usec_timer_mark(&timer);
5861   cpi->time_receive_data += vpx_usec_timer_elapsed(&timer);
5862 
5863   if ((cm->profile == PROFILE_0 || cm->profile == PROFILE_2) &&
5864       (subsampling_x != 1 || subsampling_y != 1)) {
5865     vpx_internal_error(&cm->error, VPX_CODEC_INVALID_PARAM,
5866                        "Non-4:2:0 color format requires profile 1 or 3");
5867     res = -1;
5868   }
5869   if ((cm->profile == PROFILE_1 || cm->profile == PROFILE_3) &&
5870       (subsampling_x == 1 && subsampling_y == 1)) {
5871     vpx_internal_error(&cm->error, VPX_CODEC_INVALID_PARAM,
5872                        "4:2:0 color format requires profile 0 or 2");
5873     res = -1;
5874   }
5875 
5876   return res;
5877 }
5878 
5879 static int frame_is_reference(const VP9_COMP *cpi) {
5880   const VP9_COMMON *cm = &cpi->common;
5881 
5882   return cm->frame_type == KEY_FRAME || cpi->refresh_last_frame ||
5883          cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame ||
5884          cm->refresh_frame_context || cm->lf.mode_ref_delta_update ||
5885          cm->seg.update_map || cm->seg.update_data;
5886 }
5887 
5888 static void adjust_frame_rate(VP9_COMP *cpi,
5889                               const struct lookahead_entry *source) {
5890   int64_t this_duration;
5891   int step = 0;
5892 
5893   if (source->ts_start == cpi->first_time_stamp_ever) {
5894     this_duration = source->ts_end - source->ts_start;
5895     step = 1;
5896   } else {
5897     int64_t last_duration =
5898         cpi->last_end_time_stamp_seen - cpi->last_time_stamp_seen;
5899 
5900     this_duration = source->ts_end - cpi->last_end_time_stamp_seen;
5901 
5902     // do a step update if the duration changes by 10%
5903     if (last_duration)
5904       step = (int)((this_duration - last_duration) * 10 / last_duration);
5905   }
5906 
5907   if (this_duration) {
5908     if (step) {
5909       vp9_new_framerate(cpi, 10000000.0 / this_duration);
5910     } else {
5911       // Average this frame's rate into the last second's average
5912       // frame rate. If we haven't seen 1 second yet, then average
5913       // over the whole interval seen.
5914       const double interval = VPXMIN(
5915           (double)(source->ts_end - cpi->first_time_stamp_ever), 10000000.0);
5916       double avg_duration = 10000000.0 / cpi->framerate;
5917       avg_duration *= (interval - avg_duration + this_duration);
5918       avg_duration /= interval;
5919 
5920       vp9_new_framerate(cpi, 10000000.0 / avg_duration);
5921     }
5922   }
5923   cpi->last_time_stamp_seen = source->ts_start;
5924   cpi->last_end_time_stamp_seen = source->ts_end;
5925 }
5926 
5927 // Returns 0 if this is not an alt ref else the offset of the source frame
5928 // used as the arf midpoint.
5929 static int get_arf_src_index(VP9_COMP *cpi) {
5930   RATE_CONTROL *const rc = &cpi->rc;
5931   int arf_src_index = 0;
5932   if (is_altref_enabled(cpi)) {
5933     if (cpi->oxcf.pass == 2) {
5934       const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
5935       if (gf_group->update_type[gf_group->index] == ARF_UPDATE) {
5936         arf_src_index = gf_group->arf_src_offset[gf_group->index];
5937       }
5938     } else if (rc->source_alt_ref_pending) {
5939       arf_src_index = rc->frames_till_gf_update_due;
5940     }
5941   }
5942   return arf_src_index;
5943 }
5944 
5945 static void check_src_altref(VP9_COMP *cpi,
5946                              const struct lookahead_entry *source) {
5947   RATE_CONTROL *const rc = &cpi->rc;
5948 
5949   if (cpi->oxcf.pass == 2) {
5950     const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
5951     rc->is_src_frame_alt_ref =
5952         (gf_group->update_type[gf_group->index] == OVERLAY_UPDATE);
5953   } else {
5954     rc->is_src_frame_alt_ref =
5955         cpi->alt_ref_source && (source == cpi->alt_ref_source);
5956   }
5957 
5958   if (rc->is_src_frame_alt_ref) {
5959     // Current frame is an ARF overlay frame.
5960     cpi->alt_ref_source = NULL;
5961 
5962     // Don't refresh the last buffer for an ARF overlay frame. It will
5963     // become the GF so preserve last as an alternative prediction option.
5964     cpi->refresh_last_frame = 0;
5965   }
5966 }
5967 
5968 #if CONFIG_INTERNAL_STATS
5969 static void adjust_image_stat(double y, double u, double v, double all,
5970                               ImageStat *s) {
5971   s->stat[Y] += y;
5972   s->stat[U] += u;
5973   s->stat[V] += v;
5974   s->stat[ALL] += all;
5975   s->worst = VPXMIN(s->worst, all);
5976 }
5977 #endif  // CONFIG_INTERNAL_STATS
5978 
5979 // Adjust the maximum allowable frame size for the target level.
5980 static void level_rc_framerate(VP9_COMP *cpi, int arf_src_index) {
5981   RATE_CONTROL *const rc = &cpi->rc;
5982   LevelConstraint *const ls = &cpi->level_constraint;
5983   VP9_COMMON *const cm = &cpi->common;
5984   const double max_cpb_size = ls->max_cpb_size;
5985   vpx_clear_system_state();
5986   rc->max_frame_bandwidth = VPXMIN(rc->max_frame_bandwidth, ls->max_frame_size);
5987   if (frame_is_intra_only(cm)) {
5988     rc->max_frame_bandwidth =
5989         VPXMIN(rc->max_frame_bandwidth, (int)(max_cpb_size * 0.5));
5990   } else if (arf_src_index > 0) {
5991     rc->max_frame_bandwidth =
5992         VPXMIN(rc->max_frame_bandwidth, (int)(max_cpb_size * 0.4));
5993   } else {
5994     rc->max_frame_bandwidth =
5995         VPXMIN(rc->max_frame_bandwidth, (int)(max_cpb_size * 0.2));
5996   }
5997 }
5998 
5999 static void update_level_info(VP9_COMP *cpi, size_t *size, int arf_src_index) {
6000   VP9_COMMON *const cm = &cpi->common;
6001   Vp9LevelInfo *const level_info = &cpi->level_info;
6002   Vp9LevelSpec *const level_spec = &level_info->level_spec;
6003   Vp9LevelStats *const level_stats = &level_info->level_stats;
6004   int i, idx;
6005   uint64_t luma_samples, dur_end;
6006   const uint32_t luma_pic_size = cm->width * cm->height;
6007   const uint32_t luma_pic_breadth = VPXMAX(cm->width, cm->height);
6008   LevelConstraint *const level_constraint = &cpi->level_constraint;
6009   const int8_t level_index = level_constraint->level_index;
6010   double cpb_data_size;
6011 
6012   vpx_clear_system_state();
6013 
6014   // update level_stats
6015   level_stats->total_compressed_size += *size;
6016   if (cm->show_frame) {
6017     level_stats->total_uncompressed_size +=
6018         luma_pic_size +
6019         2 * (luma_pic_size >> (cm->subsampling_x + cm->subsampling_y));
6020     level_stats->time_encoded =
6021         (cpi->last_end_time_stamp_seen - cpi->first_time_stamp_ever) /
6022         (double)TICKS_PER_SEC;
6023   }
6024 
6025   if (arf_src_index > 0) {
6026     if (!level_stats->seen_first_altref) {
6027       level_stats->seen_first_altref = 1;
6028     } else if (level_stats->frames_since_last_altref <
6029                level_spec->min_altref_distance) {
6030       level_spec->min_altref_distance = level_stats->frames_since_last_altref;
6031     }
6032     level_stats->frames_since_last_altref = 0;
6033   } else {
6034     ++level_stats->frames_since_last_altref;
6035   }
6036 
6037   if (level_stats->frame_window_buffer.len < FRAME_WINDOW_SIZE - 1) {
6038     idx = (level_stats->frame_window_buffer.start +
6039            level_stats->frame_window_buffer.len++) %
6040           FRAME_WINDOW_SIZE;
6041   } else {
6042     idx = level_stats->frame_window_buffer.start;
6043     level_stats->frame_window_buffer.start = (idx + 1) % FRAME_WINDOW_SIZE;
6044   }
6045   level_stats->frame_window_buffer.buf[idx].ts = cpi->last_time_stamp_seen;
6046   level_stats->frame_window_buffer.buf[idx].size = (uint32_t)(*size);
6047   level_stats->frame_window_buffer.buf[idx].luma_samples = luma_pic_size;
6048 
6049   if (cm->frame_type == KEY_FRAME) {
6050     level_stats->ref_refresh_map = 0;
6051   } else {
6052     int count = 0;
6053     level_stats->ref_refresh_map |= vp9_get_refresh_mask(cpi);
6054     // Also need to consider the case where the encoder refers to a buffer
6055     // that has been implicitly refreshed after encoding a keyframe.
6056     if (!cm->intra_only) {
6057       level_stats->ref_refresh_map |= (1 << cpi->lst_fb_idx);
6058       level_stats->ref_refresh_map |= (1 << cpi->gld_fb_idx);
6059       level_stats->ref_refresh_map |= (1 << cpi->alt_fb_idx);
6060     }
6061     for (i = 0; i < REF_FRAMES; ++i) {
6062       count += (level_stats->ref_refresh_map >> i) & 1;
6063     }
6064     if (count > level_spec->max_ref_frame_buffers) {
6065       level_spec->max_ref_frame_buffers = count;
6066     }
6067   }
6068 
6069   // update average_bitrate
6070   level_spec->average_bitrate = (double)level_stats->total_compressed_size /
6071                                 125.0 / level_stats->time_encoded;
6072 
6073   // update max_luma_sample_rate
6074   luma_samples = 0;
6075   for (i = 0; i < level_stats->frame_window_buffer.len; ++i) {
6076     idx = (level_stats->frame_window_buffer.start +
6077            level_stats->frame_window_buffer.len - 1 - i) %
6078           FRAME_WINDOW_SIZE;
6079     if (i == 0) {
6080       dur_end = level_stats->frame_window_buffer.buf[idx].ts;
6081     }
6082     if (dur_end - level_stats->frame_window_buffer.buf[idx].ts >=
6083         TICKS_PER_SEC) {
6084       break;
6085     }
6086     luma_samples += level_stats->frame_window_buffer.buf[idx].luma_samples;
6087   }
6088   if (luma_samples > level_spec->max_luma_sample_rate) {
6089     level_spec->max_luma_sample_rate = luma_samples;
6090   }
6091 
6092   // update max_cpb_size
6093   cpb_data_size = 0;
6094   for (i = 0; i < CPB_WINDOW_SIZE; ++i) {
6095     if (i >= level_stats->frame_window_buffer.len) break;
6096     idx = (level_stats->frame_window_buffer.start +
6097            level_stats->frame_window_buffer.len - 1 - i) %
6098           FRAME_WINDOW_SIZE;
6099     cpb_data_size += level_stats->frame_window_buffer.buf[idx].size;
6100   }
6101   cpb_data_size = cpb_data_size / 125.0;
6102   if (cpb_data_size > level_spec->max_cpb_size) {
6103     level_spec->max_cpb_size = cpb_data_size;
6104   }
6105 
6106   // update max_luma_picture_size
6107   if (luma_pic_size > level_spec->max_luma_picture_size) {
6108     level_spec->max_luma_picture_size = luma_pic_size;
6109   }
6110 
6111   // update max_luma_picture_breadth
6112   if (luma_pic_breadth > level_spec->max_luma_picture_breadth) {
6113     level_spec->max_luma_picture_breadth = luma_pic_breadth;
6114   }
6115 
6116   // update compression_ratio
6117   level_spec->compression_ratio = (double)level_stats->total_uncompressed_size *
6118                                   cm->bit_depth /
6119                                   level_stats->total_compressed_size / 8.0;
6120 
6121   // update max_col_tiles
6122   if (level_spec->max_col_tiles < (1 << cm->log2_tile_cols)) {
6123     level_spec->max_col_tiles = (1 << cm->log2_tile_cols);
6124   }
6125 
6126   if (level_index >= 0 && level_constraint->fail_flag == 0) {
6127     if (level_spec->max_luma_picture_size >
6128         vp9_level_defs[level_index].max_luma_picture_size) {
6129       level_constraint->fail_flag |= (1 << LUMA_PIC_SIZE_TOO_LARGE);
6130       vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
6131                          "Failed to encode to the target level %d. %s",
6132                          vp9_level_defs[level_index].level,
6133                          level_fail_messages[LUMA_PIC_SIZE_TOO_LARGE]);
6134     }
6135 
6136     if (level_spec->max_luma_picture_breadth >
6137         vp9_level_defs[level_index].max_luma_picture_breadth) {
6138       level_constraint->fail_flag |= (1 << LUMA_PIC_BREADTH_TOO_LARGE);
6139       vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
6140                          "Failed to encode to the target level %d. %s",
6141                          vp9_level_defs[level_index].level,
6142                          level_fail_messages[LUMA_PIC_BREADTH_TOO_LARGE]);
6143     }
6144 
6145     if ((double)level_spec->max_luma_sample_rate >
6146         (double)vp9_level_defs[level_index].max_luma_sample_rate *
6147             (1 + SAMPLE_RATE_GRACE_P)) {
6148       level_constraint->fail_flag |= (1 << LUMA_SAMPLE_RATE_TOO_LARGE);
6149       vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
6150                          "Failed to encode to the target level %d. %s",
6151                          vp9_level_defs[level_index].level,
6152                          level_fail_messages[LUMA_SAMPLE_RATE_TOO_LARGE]);
6153     }
6154 
6155     if (level_spec->max_col_tiles > vp9_level_defs[level_index].max_col_tiles) {
6156       level_constraint->fail_flag |= (1 << TOO_MANY_COLUMN_TILE);
6157       vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
6158                          "Failed to encode to the target level %d. %s",
6159                          vp9_level_defs[level_index].level,
6160                          level_fail_messages[TOO_MANY_COLUMN_TILE]);
6161     }
6162 
6163     if (level_spec->min_altref_distance <
6164         vp9_level_defs[level_index].min_altref_distance) {
6165       level_constraint->fail_flag |= (1 << ALTREF_DIST_TOO_SMALL);
6166       vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
6167                          "Failed to encode to the target level %d. %s",
6168                          vp9_level_defs[level_index].level,
6169                          level_fail_messages[ALTREF_DIST_TOO_SMALL]);
6170     }
6171 
6172     if (level_spec->max_ref_frame_buffers >
6173         vp9_level_defs[level_index].max_ref_frame_buffers) {
6174       level_constraint->fail_flag |= (1 << TOO_MANY_REF_BUFFER);
6175       vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
6176                          "Failed to encode to the target level %d. %s",
6177                          vp9_level_defs[level_index].level,
6178                          level_fail_messages[TOO_MANY_REF_BUFFER]);
6179     }
6180 
6181     if (level_spec->max_cpb_size > vp9_level_defs[level_index].max_cpb_size) {
6182       level_constraint->fail_flag |= (1 << CPB_TOO_LARGE);
6183       vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
6184                          "Failed to encode to the target level %d. %s",
6185                          vp9_level_defs[level_index].level,
6186                          level_fail_messages[CPB_TOO_LARGE]);
6187     }
6188 
6189     // Set an upper bound for the next frame size. It will be used in
6190     // level_rc_framerate() before encoding the next frame.
6191     cpb_data_size = 0;
6192     for (i = 0; i < CPB_WINDOW_SIZE - 1; ++i) {
6193       if (i >= level_stats->frame_window_buffer.len) break;
6194       idx = (level_stats->frame_window_buffer.start +
6195              level_stats->frame_window_buffer.len - 1 - i) %
6196             FRAME_WINDOW_SIZE;
6197       cpb_data_size += level_stats->frame_window_buffer.buf[idx].size;
6198     }
6199     cpb_data_size = cpb_data_size / 125.0;
6200     level_constraint->max_frame_size =
6201         (int)((vp9_level_defs[level_index].max_cpb_size - cpb_data_size) *
6202               1000.0);
6203     if (level_stats->frame_window_buffer.len < CPB_WINDOW_SIZE - 1)
6204       level_constraint->max_frame_size >>= 1;
6205   }
6206 }
6207 
6208 typedef struct GF_PICTURE {
6209   YV12_BUFFER_CONFIG *frame;
6210   int ref_frame[3];
6211   FRAME_UPDATE_TYPE update_type;
6212 } GF_PICTURE;
6213 
6214 static void init_gop_frames(VP9_COMP *cpi, GF_PICTURE *gf_picture,
6215                             const GF_GROUP *gf_group, int *tpl_group_frames) {
6216   VP9_COMMON *cm = &cpi->common;
6217   int frame_idx = 0;
6218   int i;
6219   int gld_index = -1;
6220   int alt_index = -1;
6221   int lst_index = -1;
6222   int arf_index_stack[MAX_ARF_LAYERS];
6223   int arf_stack_size = 0;
6224   int extend_frame_count = 0;
6225   int pframe_qindex = cpi->tpl_stats[2].base_qindex;
6226   int frame_gop_offset = 0;
6227 
6228   RefCntBuffer *frame_bufs = cm->buffer_pool->frame_bufs;
6229   int8_t recon_frame_index[REFS_PER_FRAME + MAX_ARF_LAYERS];
6230 
6231   memset(recon_frame_index, -1, sizeof(recon_frame_index));
6232   stack_init(arf_index_stack, MAX_ARF_LAYERS);
6233 
6234   // TODO(jingning): To be used later for gf frame type parsing.
6235   (void)gf_group;
6236 
6237   for (i = 0; i < FRAME_BUFFERS; ++i) {
6238     if (frame_bufs[i].ref_count == 0) {
6239       alloc_frame_mvs(cm, i);
6240       if (vpx_realloc_frame_buffer(&frame_bufs[i].buf, cm->width, cm->height,
6241                                    cm->subsampling_x, cm->subsampling_y,
6242 #if CONFIG_VP9_HIGHBITDEPTH
6243                                    cm->use_highbitdepth,
6244 #endif
6245                                    VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment,
6246                                    NULL, NULL, NULL))
6247         vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
6248                            "Failed to allocate frame buffer");
6249 
6250       recon_frame_index[frame_idx] = i;
6251       ++frame_idx;
6252 
6253       if (frame_idx >= REFS_PER_FRAME + cpi->oxcf.enable_auto_arf) break;
6254     }
6255   }
6256 
6257   for (i = 0; i < REFS_PER_FRAME + 1; ++i) {
6258     assert(recon_frame_index[i] >= 0);
6259     cpi->tpl_recon_frames[i] = &frame_bufs[recon_frame_index[i]].buf;
6260   }
6261 
6262   *tpl_group_frames = 0;
6263 
6264   // Initialize Golden reference frame.
6265   gf_picture[0].frame = get_ref_frame_buffer(cpi, GOLDEN_FRAME);
6266   for (i = 0; i < 3; ++i) gf_picture[0].ref_frame[i] = -1;
6267   gf_picture[0].update_type = gf_group->update_type[0];
6268   gld_index = 0;
6269   ++*tpl_group_frames;
6270 
6271   // Initialize base layer ARF frame
6272   gf_picture[1].frame = cpi->Source;
6273   gf_picture[1].ref_frame[0] = gld_index;
6274   gf_picture[1].ref_frame[1] = lst_index;
6275   gf_picture[1].ref_frame[2] = alt_index;
6276   gf_picture[1].update_type = gf_group->update_type[1];
6277   alt_index = 1;
6278   ++*tpl_group_frames;
6279 
6280   // Initialize P frames
6281   for (frame_idx = 2; frame_idx < MAX_ARF_GOP_SIZE; ++frame_idx) {
6282     struct lookahead_entry *buf;
6283     frame_gop_offset = gf_group->frame_gop_index[frame_idx];
6284     buf = vp9_lookahead_peek(cpi->lookahead, frame_gop_offset - 1);
6285 
6286     if (buf == NULL) break;
6287 
6288     gf_picture[frame_idx].frame = &buf->img;
6289     gf_picture[frame_idx].ref_frame[0] = gld_index;
6290     gf_picture[frame_idx].ref_frame[1] = lst_index;
6291     gf_picture[frame_idx].ref_frame[2] = alt_index;
6292     gf_picture[frame_idx].update_type = gf_group->update_type[frame_idx];
6293 
6294     switch (gf_group->update_type[frame_idx]) {
6295       case ARF_UPDATE:
6296         stack_push(arf_index_stack, alt_index, arf_stack_size);
6297         ++arf_stack_size;
6298         alt_index = frame_idx;
6299         break;
6300       case LF_UPDATE: lst_index = frame_idx; break;
6301       case OVERLAY_UPDATE:
6302         gld_index = frame_idx;
6303         alt_index = stack_pop(arf_index_stack, arf_stack_size);
6304         --arf_stack_size;
6305         break;
6306       case USE_BUF_FRAME:
6307         lst_index = alt_index;
6308         alt_index = stack_pop(arf_index_stack, arf_stack_size);
6309         --arf_stack_size;
6310         break;
6311       default: break;
6312     }
6313 
6314     ++*tpl_group_frames;
6315 
6316     // The length of group of pictures is baseline_gf_interval, plus the
6317     // beginning golden frame from last GOP, plus the last overlay frame in
6318     // the same GOP.
6319     if (frame_idx == gf_group->gf_group_size) break;
6320   }
6321 
6322   alt_index = -1;
6323   ++frame_idx;
6324   ++frame_gop_offset;
6325 
6326   // Extend two frames outside the current gf group.
6327   for (; frame_idx < MAX_LAG_BUFFERS && extend_frame_count < 2; ++frame_idx) {
6328     struct lookahead_entry *buf =
6329         vp9_lookahead_peek(cpi->lookahead, frame_gop_offset - 1);
6330 
6331     if (buf == NULL) break;
6332 
6333     cpi->tpl_stats[frame_idx].base_qindex = pframe_qindex;
6334 
6335     gf_picture[frame_idx].frame = &buf->img;
6336     gf_picture[frame_idx].ref_frame[0] = gld_index;
6337     gf_picture[frame_idx].ref_frame[1] = lst_index;
6338     gf_picture[frame_idx].ref_frame[2] = alt_index;
6339     gf_picture[frame_idx].update_type = LF_UPDATE;
6340     lst_index = frame_idx;
6341     ++*tpl_group_frames;
6342     ++extend_frame_count;
6343     ++frame_gop_offset;
6344   }
6345 }
6346 
6347 static void init_tpl_stats(VP9_COMP *cpi) {
6348   int frame_idx;
6349   for (frame_idx = 0; frame_idx < MAX_ARF_GOP_SIZE; ++frame_idx) {
6350     TplDepFrame *tpl_frame = &cpi->tpl_stats[frame_idx];
6351     memset(tpl_frame->tpl_stats_ptr, 0,
6352            tpl_frame->height * tpl_frame->width *
6353                sizeof(*tpl_frame->tpl_stats_ptr));
6354     tpl_frame->is_valid = 0;
6355   }
6356 }
6357 
6358 #if CONFIG_NON_GREEDY_MV
6359 static uint32_t full_pixel_motion_search(VP9_COMP *cpi, ThreadData *td,
6360                                          MotionField *motion_field,
6361                                          int frame_idx, uint8_t *cur_frame_buf,
6362                                          uint8_t *ref_frame_buf, int stride,
6363                                          BLOCK_SIZE bsize, int mi_row,
6364                                          int mi_col, MV *mv) {
6365   MACROBLOCK *const x = &td->mb;
6366   MACROBLOCKD *const xd = &x->e_mbd;
6367   MV_SPEED_FEATURES *const mv_sf = &cpi->sf.mv;
6368   int step_param;
6369   uint32_t bestsme = UINT_MAX;
6370   const MvLimits tmp_mv_limits = x->mv_limits;
6371   // lambda is used to adjust the importance of motion vector consistency.
6372   // TODO(angiebird): Figure out lambda's proper value.
6373   const int lambda = cpi->tpl_stats[frame_idx].lambda;
6374   int_mv nb_full_mvs[NB_MVS_NUM];
6375   int nb_full_mv_num;
6376 
6377   MV best_ref_mv1 = { 0, 0 };
6378   MV best_ref_mv1_full; /* full-pixel value of best_ref_mv1 */
6379 
6380   best_ref_mv1_full.col = best_ref_mv1.col >> 3;
6381   best_ref_mv1_full.row = best_ref_mv1.row >> 3;
6382 
6383   // Setup frame pointers
6384   x->plane[0].src.buf = cur_frame_buf;
6385   x->plane[0].src.stride = stride;
6386   xd->plane[0].pre[0].buf = ref_frame_buf;
6387   xd->plane[0].pre[0].stride = stride;
6388 
6389   step_param = mv_sf->reduce_first_step_size;
6390   step_param = VPXMIN(step_param, MAX_MVSEARCH_STEPS - 2);
6391 
6392   vp9_set_mv_search_range(&x->mv_limits, &best_ref_mv1);
6393 
6394   nb_full_mv_num =
6395       vp9_prepare_nb_full_mvs(motion_field, mi_row, mi_col, nb_full_mvs);
6396   vp9_full_pixel_diamond_new(cpi, x, bsize, &best_ref_mv1_full, step_param,
6397                              lambda, 1, nb_full_mvs, nb_full_mv_num, mv);
6398 
6399   /* restore UMV window */
6400   x->mv_limits = tmp_mv_limits;
6401 
6402   return bestsme;
6403 }
6404 
6405 static uint32_t sub_pixel_motion_search(VP9_COMP *cpi, ThreadData *td,
6406                                         uint8_t *cur_frame_buf,
6407                                         uint8_t *ref_frame_buf, int stride,
6408                                         BLOCK_SIZE bsize, MV *mv) {
6409   MACROBLOCK *const x = &td->mb;
6410   MACROBLOCKD *const xd = &x->e_mbd;
6411   MV_SPEED_FEATURES *const mv_sf = &cpi->sf.mv;
6412   uint32_t bestsme = UINT_MAX;
6413   uint32_t distortion;
6414   uint32_t sse;
6415   int cost_list[5];
6416 
6417   MV best_ref_mv1 = { 0, 0 };
6418 
6419   // Setup frame pointers
6420   x->plane[0].src.buf = cur_frame_buf;
6421   x->plane[0].src.stride = stride;
6422   xd->plane[0].pre[0].buf = ref_frame_buf;
6423   xd->plane[0].pre[0].stride = stride;
6424 
6425   // TODO(yunqing): may use higher tap interp filter than 2 taps.
6426   // Ignore mv costing by sending NULL pointer instead of cost array
6427   bestsme = cpi->find_fractional_mv_step(
6428       x, mv, &best_ref_mv1, cpi->common.allow_high_precision_mv, x->errorperbit,
6429       &cpi->fn_ptr[bsize], 0, mv_sf->subpel_search_level,
6430       cond_cost_list(cpi, cost_list), NULL, NULL, &distortion, &sse, NULL, 0, 0,
6431       USE_2_TAPS);
6432 
6433   return bestsme;
6434 }
6435 
6436 #else  // CONFIG_NON_GREEDY_MV
6437 static uint32_t motion_compensated_prediction(VP9_COMP *cpi, ThreadData *td,
6438                                               uint8_t *cur_frame_buf,
6439                                               uint8_t *ref_frame_buf,
6440                                               int stride, BLOCK_SIZE bsize,
6441                                               MV *mv) {
6442   MACROBLOCK *const x = &td->mb;
6443   MACROBLOCKD *const xd = &x->e_mbd;
6444   MV_SPEED_FEATURES *const mv_sf = &cpi->sf.mv;
6445   const SEARCH_METHODS search_method = NSTEP;
6446   int step_param;
6447   int sadpb = x->sadperbit16;
6448   uint32_t bestsme = UINT_MAX;
6449   uint32_t distortion;
6450   uint32_t sse;
6451   int cost_list[5];
6452   const MvLimits tmp_mv_limits = x->mv_limits;
6453 
6454   MV best_ref_mv1 = { 0, 0 };
6455   MV best_ref_mv1_full; /* full-pixel value of best_ref_mv1 */
6456 
6457   best_ref_mv1_full.col = best_ref_mv1.col >> 3;
6458   best_ref_mv1_full.row = best_ref_mv1.row >> 3;
6459 
6460   // Setup frame pointers
6461   x->plane[0].src.buf = cur_frame_buf;
6462   x->plane[0].src.stride = stride;
6463   xd->plane[0].pre[0].buf = ref_frame_buf;
6464   xd->plane[0].pre[0].stride = stride;
6465 
6466   step_param = mv_sf->reduce_first_step_size;
6467   step_param = VPXMIN(step_param, MAX_MVSEARCH_STEPS - 2);
6468 
6469   vp9_set_mv_search_range(&x->mv_limits, &best_ref_mv1);
6470 
6471   vp9_full_pixel_search(cpi, x, bsize, &best_ref_mv1_full, step_param,
6472                         search_method, sadpb, cond_cost_list(cpi, cost_list),
6473                         &best_ref_mv1, mv, 0, 0);
6474 
6475   /* restore UMV window */
6476   x->mv_limits = tmp_mv_limits;
6477 
6478   // TODO(yunqing): may use higher tap interp filter than 2 taps.
6479   // Ignore mv costing by sending NULL pointer instead of cost array
6480   bestsme = cpi->find_fractional_mv_step(
6481       x, mv, &best_ref_mv1, cpi->common.allow_high_precision_mv, x->errorperbit,
6482       &cpi->fn_ptr[bsize], 0, mv_sf->subpel_search_level,
6483       cond_cost_list(cpi, cost_list), NULL, NULL, &distortion, &sse, NULL, 0, 0,
6484       USE_2_TAPS);
6485 
6486   return bestsme;
6487 }
6488 #endif
6489 
6490 static int get_overlap_area(int grid_pos_row, int grid_pos_col, int ref_pos_row,
6491                             int ref_pos_col, int block, BLOCK_SIZE bsize) {
6492   int width = 0, height = 0;
6493   int bw = 4 << b_width_log2_lookup[bsize];
6494   int bh = 4 << b_height_log2_lookup[bsize];
6495 
6496   switch (block) {
6497     case 0:
6498       width = grid_pos_col + bw - ref_pos_col;
6499       height = grid_pos_row + bh - ref_pos_row;
6500       break;
6501     case 1:
6502       width = ref_pos_col + bw - grid_pos_col;
6503       height = grid_pos_row + bh - ref_pos_row;
6504       break;
6505     case 2:
6506       width = grid_pos_col + bw - ref_pos_col;
6507       height = ref_pos_row + bh - grid_pos_row;
6508       break;
6509     case 3:
6510       width = ref_pos_col + bw - grid_pos_col;
6511       height = ref_pos_row + bh - grid_pos_row;
6512       break;
6513     default: assert(0);
6514   }
6515 
6516   return width * height;
6517 }
6518 
6519 static int round_floor(int ref_pos, int bsize_pix) {
6520   int round;
6521   if (ref_pos < 0)
6522     round = -(1 + (-ref_pos - 1) / bsize_pix);
6523   else
6524     round = ref_pos / bsize_pix;
6525 
6526   return round;
6527 }
6528 
6529 static void tpl_model_store(TplDepStats *tpl_stats, int mi_row, int mi_col,
6530                             BLOCK_SIZE bsize, int stride) {
6531   const int mi_height = num_8x8_blocks_high_lookup[bsize];
6532   const int mi_width = num_8x8_blocks_wide_lookup[bsize];
6533   const TplDepStats *src_stats = &tpl_stats[mi_row * stride + mi_col];
6534   int idx, idy;
6535 
6536   for (idy = 0; idy < mi_height; ++idy) {
6537     for (idx = 0; idx < mi_width; ++idx) {
6538       TplDepStats *tpl_ptr = &tpl_stats[(mi_row + idy) * stride + mi_col + idx];
6539       const int64_t mc_flow = tpl_ptr->mc_flow;
6540       const int64_t mc_ref_cost = tpl_ptr->mc_ref_cost;
6541       *tpl_ptr = *src_stats;
6542       tpl_ptr->mc_flow = mc_flow;
6543       tpl_ptr->mc_ref_cost = mc_ref_cost;
6544       tpl_ptr->mc_dep_cost = tpl_ptr->intra_cost + tpl_ptr->mc_flow;
6545     }
6546   }
6547 }
6548 
6549 static void tpl_model_update_b(TplDepFrame *tpl_frame, TplDepStats *tpl_stats,
6550                                int mi_row, int mi_col, const BLOCK_SIZE bsize) {
6551   TplDepFrame *ref_tpl_frame = &tpl_frame[tpl_stats->ref_frame_index];
6552   TplDepStats *ref_stats = ref_tpl_frame->tpl_stats_ptr;
6553   MV mv = tpl_stats->mv.as_mv;
6554   int mv_row = mv.row >> 3;
6555   int mv_col = mv.col >> 3;
6556 
6557   int ref_pos_row = mi_row * MI_SIZE + mv_row;
6558   int ref_pos_col = mi_col * MI_SIZE + mv_col;
6559 
6560   const int bw = 4 << b_width_log2_lookup[bsize];
6561   const int bh = 4 << b_height_log2_lookup[bsize];
6562   const int mi_height = num_8x8_blocks_high_lookup[bsize];
6563   const int mi_width = num_8x8_blocks_wide_lookup[bsize];
6564   const int pix_num = bw * bh;
6565 
6566   // top-left on grid block location in pixel
6567   int grid_pos_row_base = round_floor(ref_pos_row, bh) * bh;
6568   int grid_pos_col_base = round_floor(ref_pos_col, bw) * bw;
6569   int block;
6570 
6571   for (block = 0; block < 4; ++block) {
6572     int grid_pos_row = grid_pos_row_base + bh * (block >> 1);
6573     int grid_pos_col = grid_pos_col_base + bw * (block & 0x01);
6574 
6575     if (grid_pos_row >= 0 && grid_pos_row < ref_tpl_frame->mi_rows * MI_SIZE &&
6576         grid_pos_col >= 0 && grid_pos_col < ref_tpl_frame->mi_cols * MI_SIZE) {
6577       int overlap_area = get_overlap_area(
6578           grid_pos_row, grid_pos_col, ref_pos_row, ref_pos_col, block, bsize);
6579       int ref_mi_row = round_floor(grid_pos_row, bh) * mi_height;
6580       int ref_mi_col = round_floor(grid_pos_col, bw) * mi_width;
6581 
6582       int64_t mc_flow = tpl_stats->mc_dep_cost -
6583                         (tpl_stats->mc_dep_cost * tpl_stats->inter_cost) /
6584                             tpl_stats->intra_cost;
6585 
6586       int idx, idy;
6587 
6588       for (idy = 0; idy < mi_height; ++idy) {
6589         for (idx = 0; idx < mi_width; ++idx) {
6590           TplDepStats *des_stats =
6591               &ref_stats[(ref_mi_row + idy) * ref_tpl_frame->stride +
6592                          (ref_mi_col + idx)];
6593 
6594           des_stats->mc_flow += (mc_flow * overlap_area) / pix_num;
6595           des_stats->mc_ref_cost +=
6596               ((tpl_stats->intra_cost - tpl_stats->inter_cost) * overlap_area) /
6597               pix_num;
6598           assert(overlap_area >= 0);
6599         }
6600       }
6601     }
6602   }
6603 }
6604 
6605 static void tpl_model_update(TplDepFrame *tpl_frame, TplDepStats *tpl_stats,
6606                              int mi_row, int mi_col, const BLOCK_SIZE bsize) {
6607   int idx, idy;
6608   const int mi_height = num_8x8_blocks_high_lookup[bsize];
6609   const int mi_width = num_8x8_blocks_wide_lookup[bsize];
6610 
6611   for (idy = 0; idy < mi_height; ++idy) {
6612     for (idx = 0; idx < mi_width; ++idx) {
6613       TplDepStats *tpl_ptr =
6614           &tpl_stats[(mi_row + idy) * tpl_frame->stride + (mi_col + idx)];
6615       tpl_model_update_b(tpl_frame, tpl_ptr, mi_row + idy, mi_col + idx,
6616                          BLOCK_8X8);
6617     }
6618   }
6619 }
6620 
6621 static void get_quantize_error(MACROBLOCK *x, int plane, tran_low_t *coeff,
6622                                tran_low_t *qcoeff, tran_low_t *dqcoeff,
6623                                TX_SIZE tx_size, int64_t *recon_error,
6624                                int64_t *sse) {
6625   MACROBLOCKD *const xd = &x->e_mbd;
6626   const struct macroblock_plane *const p = &x->plane[plane];
6627   const struct macroblockd_plane *const pd = &xd->plane[plane];
6628   const scan_order *const scan_order = &vp9_default_scan_orders[tx_size];
6629   uint16_t eob;
6630   int pix_num = 1 << num_pels_log2_lookup[txsize_to_bsize[tx_size]];
6631   const int shift = tx_size == TX_32X32 ? 0 : 2;
6632 
6633 #if CONFIG_VP9_HIGHBITDEPTH
6634   if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
6635     vp9_highbd_quantize_fp_32x32(coeff, pix_num, x->skip_block, p->round_fp,
6636                                  p->quant_fp, qcoeff, dqcoeff, pd->dequant,
6637                                  &eob, scan_order->scan, scan_order->iscan);
6638   } else {
6639     vp9_quantize_fp_32x32(coeff, pix_num, x->skip_block, p->round_fp,
6640                           p->quant_fp, qcoeff, dqcoeff, pd->dequant, &eob,
6641                           scan_order->scan, scan_order->iscan);
6642   }
6643 #else
6644   vp9_quantize_fp_32x32(coeff, pix_num, x->skip_block, p->round_fp, p->quant_fp,
6645                         qcoeff, dqcoeff, pd->dequant, &eob, scan_order->scan,
6646                         scan_order->iscan);
6647 #endif  // CONFIG_VP9_HIGHBITDEPTH
6648 
6649   *recon_error = vp9_block_error(coeff, dqcoeff, pix_num, sse) >> shift;
6650   *recon_error = VPXMAX(*recon_error, 1);
6651 
6652   *sse = (*sse) >> shift;
6653   *sse = VPXMAX(*sse, 1);
6654 }
6655 
6656 #if CONFIG_VP9_HIGHBITDEPTH
6657 void highbd_wht_fwd_txfm(int16_t *src_diff, int bw, tran_low_t *coeff,
6658                          TX_SIZE tx_size) {
6659   // TODO(sdeng): Implement SIMD based high bit-depth Hadamard transforms.
6660   switch (tx_size) {
6661     case TX_8X8: vpx_highbd_hadamard_8x8(src_diff, bw, coeff); break;
6662     case TX_16X16: vpx_highbd_hadamard_16x16(src_diff, bw, coeff); break;
6663     case TX_32X32: vpx_highbd_hadamard_32x32(src_diff, bw, coeff); break;
6664     default: assert(0);
6665   }
6666 }
6667 #endif  // CONFIG_VP9_HIGHBITDEPTH
6668 
6669 void wht_fwd_txfm(int16_t *src_diff, int bw, tran_low_t *coeff,
6670                   TX_SIZE tx_size) {
6671   switch (tx_size) {
6672     case TX_8X8: vpx_hadamard_8x8(src_diff, bw, coeff); break;
6673     case TX_16X16: vpx_hadamard_16x16(src_diff, bw, coeff); break;
6674     case TX_32X32: vpx_hadamard_32x32(src_diff, bw, coeff); break;
6675     default: assert(0);
6676   }
6677 }
6678 
6679 static void set_mv_limits(const VP9_COMMON *cm, MACROBLOCK *x, int mi_row,
6680                           int mi_col) {
6681   x->mv_limits.row_min = -((mi_row * MI_SIZE) + (17 - 2 * VP9_INTERP_EXTEND));
6682   x->mv_limits.row_max =
6683       (cm->mi_rows - 1 - mi_row) * MI_SIZE + (17 - 2 * VP9_INTERP_EXTEND);
6684   x->mv_limits.col_min = -((mi_col * MI_SIZE) + (17 - 2 * VP9_INTERP_EXTEND));
6685   x->mv_limits.col_max =
6686       ((cm->mi_cols - 1 - mi_col) * MI_SIZE) + (17 - 2 * VP9_INTERP_EXTEND);
6687 }
6688 
6689 static void mode_estimation(VP9_COMP *cpi, MACROBLOCK *x, MACROBLOCKD *xd,
6690                             struct scale_factors *sf, GF_PICTURE *gf_picture,
6691                             int frame_idx, TplDepFrame *tpl_frame,
6692                             int16_t *src_diff, tran_low_t *coeff,
6693                             tran_low_t *qcoeff, tran_low_t *dqcoeff, int mi_row,
6694                             int mi_col, BLOCK_SIZE bsize, TX_SIZE tx_size,
6695                             YV12_BUFFER_CONFIG *ref_frame[], uint8_t *predictor,
6696                             int64_t *recon_error, int64_t *sse) {
6697   VP9_COMMON *cm = &cpi->common;
6698   ThreadData *td = &cpi->td;
6699 
6700   const int bw = 4 << b_width_log2_lookup[bsize];
6701   const int bh = 4 << b_height_log2_lookup[bsize];
6702   const int pix_num = bw * bh;
6703   int best_rf_idx = -1;
6704   int_mv best_mv;
6705   int64_t best_inter_cost = INT64_MAX;
6706   int64_t inter_cost;
6707   int rf_idx;
6708   const InterpKernel *const kernel = vp9_filter_kernels[EIGHTTAP];
6709 
6710   int64_t best_intra_cost = INT64_MAX;
6711   int64_t intra_cost;
6712   PREDICTION_MODE mode;
6713   int mb_y_offset = mi_row * MI_SIZE * xd->cur_buf->y_stride + mi_col * MI_SIZE;
6714   MODE_INFO mi_above, mi_left;
6715   const int mi_height = num_8x8_blocks_high_lookup[bsize];
6716   const int mi_width = num_8x8_blocks_wide_lookup[bsize];
6717   TplDepStats *tpl_stats =
6718       &tpl_frame->tpl_stats_ptr[mi_row * tpl_frame->stride + mi_col];
6719 
6720   xd->mb_to_top_edge = -((mi_row * MI_SIZE) * 8);
6721   xd->mb_to_bottom_edge = ((cm->mi_rows - 1 - mi_row) * MI_SIZE) * 8;
6722   xd->mb_to_left_edge = -((mi_col * MI_SIZE) * 8);
6723   xd->mb_to_right_edge = ((cm->mi_cols - 1 - mi_col) * MI_SIZE) * 8;
6724   xd->above_mi = (mi_row > 0) ? &mi_above : NULL;
6725   xd->left_mi = (mi_col > 0) ? &mi_left : NULL;
6726 
6727   // Intra prediction search
6728   for (mode = DC_PRED; mode <= TM_PRED; ++mode) {
6729     uint8_t *src, *dst;
6730     int src_stride, dst_stride;
6731 
6732     src = xd->cur_buf->y_buffer + mb_y_offset;
6733     src_stride = xd->cur_buf->y_stride;
6734 
6735     dst = &predictor[0];
6736     dst_stride = bw;
6737 
6738     xd->mi[0]->sb_type = bsize;
6739     xd->mi[0]->ref_frame[0] = INTRA_FRAME;
6740 
6741     vp9_predict_intra_block(xd, b_width_log2_lookup[bsize], tx_size, mode, src,
6742                             src_stride, dst, dst_stride, 0, 0, 0);
6743 
6744 #if CONFIG_VP9_HIGHBITDEPTH
6745     if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
6746       vpx_highbd_subtract_block(bh, bw, src_diff, bw, src, src_stride, dst,
6747                                 dst_stride, xd->bd);
6748       highbd_wht_fwd_txfm(src_diff, bw, coeff, tx_size);
6749       intra_cost = vpx_highbd_satd(coeff, pix_num);
6750     } else {
6751       vpx_subtract_block(bh, bw, src_diff, bw, src, src_stride, dst,
6752                          dst_stride);
6753       wht_fwd_txfm(src_diff, bw, coeff, tx_size);
6754       intra_cost = vpx_satd(coeff, pix_num);
6755     }
6756 #else
6757     vpx_subtract_block(bh, bw, src_diff, bw, src, src_stride, dst, dst_stride);
6758     wht_fwd_txfm(src_diff, bw, coeff, tx_size);
6759     intra_cost = vpx_satd(coeff, pix_num);
6760 #endif  // CONFIG_VP9_HIGHBITDEPTH
6761 
6762     if (intra_cost < best_intra_cost) best_intra_cost = intra_cost;
6763   }
6764 
6765   // Motion compensated prediction
6766   best_mv.as_int = 0;
6767 
6768   set_mv_limits(cm, x, mi_row, mi_col);
6769 
6770   for (rf_idx = 0; rf_idx < MAX_INTER_REF_FRAMES; ++rf_idx) {
6771     int_mv mv;
6772 #if CONFIG_NON_GREEDY_MV
6773     MotionField *motion_field;
6774 #endif
6775     if (ref_frame[rf_idx] == NULL) continue;
6776 
6777 #if CONFIG_NON_GREEDY_MV
6778     (void)td;
6779     motion_field = vp9_motion_field_info_get_motion_field(
6780         &cpi->motion_field_info, frame_idx, rf_idx, bsize);
6781     mv = vp9_motion_field_mi_get_mv(motion_field, mi_row, mi_col);
6782 #else
6783     motion_compensated_prediction(cpi, td, xd->cur_buf->y_buffer + mb_y_offset,
6784                                   ref_frame[rf_idx]->y_buffer + mb_y_offset,
6785                                   xd->cur_buf->y_stride, bsize, &mv.as_mv);
6786 #endif
6787 
6788 #if CONFIG_VP9_HIGHBITDEPTH
6789     if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
6790       vp9_highbd_build_inter_predictor(
6791           CONVERT_TO_SHORTPTR(ref_frame[rf_idx]->y_buffer + mb_y_offset),
6792           ref_frame[rf_idx]->y_stride, CONVERT_TO_SHORTPTR(&predictor[0]), bw,
6793           &mv.as_mv, sf, bw, bh, 0, kernel, MV_PRECISION_Q3, mi_col * MI_SIZE,
6794           mi_row * MI_SIZE, xd->bd);
6795       vpx_highbd_subtract_block(
6796           bh, bw, src_diff, bw, xd->cur_buf->y_buffer + mb_y_offset,
6797           xd->cur_buf->y_stride, &predictor[0], bw, xd->bd);
6798       highbd_wht_fwd_txfm(src_diff, bw, coeff, tx_size);
6799       inter_cost = vpx_highbd_satd(coeff, pix_num);
6800     } else {
6801       vp9_build_inter_predictor(
6802           ref_frame[rf_idx]->y_buffer + mb_y_offset,
6803           ref_frame[rf_idx]->y_stride, &predictor[0], bw, &mv.as_mv, sf, bw, bh,
6804           0, kernel, MV_PRECISION_Q3, mi_col * MI_SIZE, mi_row * MI_SIZE);
6805       vpx_subtract_block(bh, bw, src_diff, bw,
6806                          xd->cur_buf->y_buffer + mb_y_offset,
6807                          xd->cur_buf->y_stride, &predictor[0], bw);
6808       wht_fwd_txfm(src_diff, bw, coeff, tx_size);
6809       inter_cost = vpx_satd(coeff, pix_num);
6810     }
6811 #else
6812     vp9_build_inter_predictor(ref_frame[rf_idx]->y_buffer + mb_y_offset,
6813                               ref_frame[rf_idx]->y_stride, &predictor[0], bw,
6814                               &mv.as_mv, sf, bw, bh, 0, kernel, MV_PRECISION_Q3,
6815                               mi_col * MI_SIZE, mi_row * MI_SIZE);
6816     vpx_subtract_block(bh, bw, src_diff, bw,
6817                        xd->cur_buf->y_buffer + mb_y_offset,
6818                        xd->cur_buf->y_stride, &predictor[0], bw);
6819     wht_fwd_txfm(src_diff, bw, coeff, tx_size);
6820     inter_cost = vpx_satd(coeff, pix_num);
6821 #endif
6822 
6823     if (inter_cost < best_inter_cost) {
6824       best_rf_idx = rf_idx;
6825       best_inter_cost = inter_cost;
6826       best_mv.as_int = mv.as_int;
6827       get_quantize_error(x, 0, coeff, qcoeff, dqcoeff, tx_size, recon_error,
6828                          sse);
6829     }
6830   }
6831   best_intra_cost = VPXMAX(best_intra_cost, 1);
6832   best_inter_cost = VPXMIN(best_intra_cost, best_inter_cost);
6833   tpl_stats->inter_cost = VPXMAX(
6834       1, (best_inter_cost << TPL_DEP_COST_SCALE_LOG2) / (mi_height * mi_width));
6835   tpl_stats->intra_cost = VPXMAX(
6836       1, (best_intra_cost << TPL_DEP_COST_SCALE_LOG2) / (mi_height * mi_width));
6837   tpl_stats->ref_frame_index = gf_picture[frame_idx].ref_frame[best_rf_idx];
6838   tpl_stats->mv.as_int = best_mv.as_int;
6839 }
6840 
6841 #if CONFIG_NON_GREEDY_MV
6842 static int get_block_src_pred_buf(MACROBLOCKD *xd, GF_PICTURE *gf_picture,
6843                                   int frame_idx, int rf_idx, int mi_row,
6844                                   int mi_col, struct buf_2d *src,
6845                                   struct buf_2d *pre) {
6846   const int mb_y_offset =
6847       mi_row * MI_SIZE * xd->cur_buf->y_stride + mi_col * MI_SIZE;
6848   YV12_BUFFER_CONFIG *ref_frame = NULL;
6849   int ref_frame_idx = gf_picture[frame_idx].ref_frame[rf_idx];
6850   if (ref_frame_idx != -1) {
6851     ref_frame = gf_picture[ref_frame_idx].frame;
6852     src->buf = xd->cur_buf->y_buffer + mb_y_offset;
6853     src->stride = xd->cur_buf->y_stride;
6854     pre->buf = ref_frame->y_buffer + mb_y_offset;
6855     pre->stride = ref_frame->y_stride;
6856     assert(src->stride == pre->stride);
6857     return 1;
6858   } else {
6859     printf("invalid ref_frame_idx");
6860     assert(ref_frame_idx != -1);
6861     return 0;
6862   }
6863 }
6864 
6865 #define kMvPreCheckLines 5
6866 #define kMvPreCheckSize 15
6867 
6868 #define MV_REF_POS_NUM 3
6869 POSITION mv_ref_pos[MV_REF_POS_NUM] = {
6870   { -1, 0 },
6871   { 0, -1 },
6872   { -1, -1 },
6873 };
6874 
6875 static int_mv *get_select_mv(VP9_COMP *cpi, TplDepFrame *tpl_frame, int mi_row,
6876                              int mi_col) {
6877   return &cpi->select_mv_arr[mi_row * tpl_frame->stride + mi_col];
6878 }
6879 
6880 static int_mv find_ref_mv(int mv_mode, VP9_COMP *cpi, TplDepFrame *tpl_frame,
6881                           BLOCK_SIZE bsize, int mi_row, int mi_col) {
6882   int i;
6883   const int mi_height = num_8x8_blocks_high_lookup[bsize];
6884   const int mi_width = num_8x8_blocks_wide_lookup[bsize];
6885   int_mv nearest_mv, near_mv, invalid_mv;
6886   nearest_mv.as_int = INVALID_MV;
6887   near_mv.as_int = INVALID_MV;
6888   invalid_mv.as_int = INVALID_MV;
6889   for (i = 0; i < MV_REF_POS_NUM; ++i) {
6890     int nb_row = mi_row + mv_ref_pos[i].row * mi_height;
6891     int nb_col = mi_col + mv_ref_pos[i].col * mi_width;
6892     assert(mv_ref_pos[i].row <= 0);
6893     assert(mv_ref_pos[i].col <= 0);
6894     if (nb_row >= 0 && nb_col >= 0) {
6895       if (nearest_mv.as_int == INVALID_MV) {
6896         nearest_mv = *get_select_mv(cpi, tpl_frame, nb_row, nb_col);
6897       } else {
6898         int_mv mv = *get_select_mv(cpi, tpl_frame, nb_row, nb_col);
6899         if (mv.as_int == nearest_mv.as_int) {
6900           continue;
6901         } else {
6902           near_mv = mv;
6903           break;
6904         }
6905       }
6906     }
6907   }
6908   if (nearest_mv.as_int == INVALID_MV) {
6909     nearest_mv.as_mv.row = 0;
6910     nearest_mv.as_mv.col = 0;
6911   }
6912   if (near_mv.as_int == INVALID_MV) {
6913     near_mv.as_mv.row = 0;
6914     near_mv.as_mv.col = 0;
6915   }
6916   if (mv_mode == NEAREST_MV_MODE) {
6917     return nearest_mv;
6918   }
6919   if (mv_mode == NEAR_MV_MODE) {
6920     return near_mv;
6921   }
6922   assert(0);
6923   return invalid_mv;
6924 }
6925 
6926 static int_mv get_mv_from_mv_mode(int mv_mode, VP9_COMP *cpi,
6927                                   MotionField *motion_field,
6928                                   TplDepFrame *tpl_frame, BLOCK_SIZE bsize,
6929                                   int mi_row, int mi_col) {
6930   int_mv mv;
6931   switch (mv_mode) {
6932     case ZERO_MV_MODE:
6933       mv.as_mv.row = 0;
6934       mv.as_mv.col = 0;
6935       break;
6936     case NEW_MV_MODE:
6937       mv = vp9_motion_field_mi_get_mv(motion_field, mi_row, mi_col);
6938       break;
6939     case NEAREST_MV_MODE:
6940       mv = find_ref_mv(mv_mode, cpi, tpl_frame, bsize, mi_row, mi_col);
6941       break;
6942     case NEAR_MV_MODE:
6943       mv = find_ref_mv(mv_mode, cpi, tpl_frame, bsize, mi_row, mi_col);
6944       break;
6945     default:
6946       mv.as_int = INVALID_MV;
6947       assert(0);
6948       break;
6949   }
6950   return mv;
6951 }
6952 
6953 static double get_mv_dist(int mv_mode, VP9_COMP *cpi, MACROBLOCKD *xd,
6954                           GF_PICTURE *gf_picture, MotionField *motion_field,
6955                           int frame_idx, TplDepFrame *tpl_frame, int rf_idx,
6956                           BLOCK_SIZE bsize, int mi_row, int mi_col,
6957                           int_mv *mv) {
6958   uint32_t sse;
6959   struct buf_2d src;
6960   struct buf_2d pre;
6961   MV full_mv;
6962   *mv = get_mv_from_mv_mode(mv_mode, cpi, motion_field, tpl_frame, bsize,
6963                             mi_row, mi_col);
6964   full_mv = get_full_mv(&mv->as_mv);
6965   if (get_block_src_pred_buf(xd, gf_picture, frame_idx, rf_idx, mi_row, mi_col,
6966                              &src, &pre)) {
6967     // TODO(angiebird): Consider subpixel when computing the sse.
6968     cpi->fn_ptr[bsize].vf(src.buf, src.stride, get_buf_from_mv(&pre, &full_mv),
6969                           pre.stride, &sse);
6970     return (double)(sse << VP9_DIST_SCALE_LOG2);
6971   } else {
6972     assert(0);
6973     return 0;
6974   }
6975 }
6976 
6977 static int get_mv_mode_cost(int mv_mode) {
6978   // TODO(angiebird): The probabilities are roughly inferred from
6979   // default_inter_mode_probs. Check if there is a better way to set the
6980   // probabilities.
6981   const int zero_mv_prob = 16;
6982   const int new_mv_prob = 24 * 1;
6983   const int ref_mv_prob = 256 - zero_mv_prob - new_mv_prob;
6984   assert(zero_mv_prob + new_mv_prob + ref_mv_prob == 256);
6985   switch (mv_mode) {
6986     case ZERO_MV_MODE: return vp9_prob_cost[zero_mv_prob]; break;
6987     case NEW_MV_MODE: return vp9_prob_cost[new_mv_prob]; break;
6988     case NEAREST_MV_MODE: return vp9_prob_cost[ref_mv_prob]; break;
6989     case NEAR_MV_MODE: return vp9_prob_cost[ref_mv_prob]; break;
6990     default: assert(0); return -1;
6991   }
6992 }
6993 
6994 static INLINE double get_mv_diff_cost(MV *new_mv, MV *ref_mv) {
6995   double mv_diff_cost = log2(1 + abs(new_mv->row - ref_mv->row)) +
6996                         log2(1 + abs(new_mv->col - ref_mv->col));
6997   mv_diff_cost *= (1 << VP9_PROB_COST_SHIFT);
6998   return mv_diff_cost;
6999 }
7000 static double get_mv_cost(int mv_mode, VP9_COMP *cpi, MotionField *motion_field,
7001                           TplDepFrame *tpl_frame, BLOCK_SIZE bsize, int mi_row,
7002                           int mi_col) {
7003   double mv_cost = get_mv_mode_cost(mv_mode);
7004   if (mv_mode == NEW_MV_MODE) {
7005     MV new_mv = get_mv_from_mv_mode(mv_mode, cpi, motion_field, tpl_frame,
7006                                     bsize, mi_row, mi_col)
7007                     .as_mv;
7008     MV nearest_mv = get_mv_from_mv_mode(NEAREST_MV_MODE, cpi, motion_field,
7009                                         tpl_frame, bsize, mi_row, mi_col)
7010                         .as_mv;
7011     MV near_mv = get_mv_from_mv_mode(NEAR_MV_MODE, cpi, motion_field, tpl_frame,
7012                                      bsize, mi_row, mi_col)
7013                      .as_mv;
7014     double nearest_cost = get_mv_diff_cost(&new_mv, &nearest_mv);
7015     double near_cost = get_mv_diff_cost(&new_mv, &near_mv);
7016     mv_cost += nearest_cost < near_cost ? nearest_cost : near_cost;
7017   }
7018   return mv_cost;
7019 }
7020 
7021 static double eval_mv_mode(int mv_mode, VP9_COMP *cpi, MACROBLOCK *x,
7022                            GF_PICTURE *gf_picture, MotionField *motion_field,
7023                            int frame_idx, TplDepFrame *tpl_frame, int rf_idx,
7024                            BLOCK_SIZE bsize, int mi_row, int mi_col,
7025                            int_mv *mv) {
7026   MACROBLOCKD *xd = &x->e_mbd;
7027   double mv_dist =
7028       get_mv_dist(mv_mode, cpi, xd, gf_picture, motion_field, frame_idx,
7029                   tpl_frame, rf_idx, bsize, mi_row, mi_col, mv);
7030   double mv_cost =
7031       get_mv_cost(mv_mode, cpi, motion_field, tpl_frame, bsize, mi_row, mi_col);
7032   double mult = 180;
7033 
7034   return mv_cost + mult * log2f(1 + mv_dist);
7035 }
7036 
7037 static int find_best_ref_mv_mode(VP9_COMP *cpi, MACROBLOCK *x,
7038                                  GF_PICTURE *gf_picture,
7039                                  MotionField *motion_field, int frame_idx,
7040                                  TplDepFrame *tpl_frame, int rf_idx,
7041                                  BLOCK_SIZE bsize, int mi_row, int mi_col,
7042                                  double *rd, int_mv *mv) {
7043   int best_mv_mode = ZERO_MV_MODE;
7044   int update = 0;
7045   int mv_mode;
7046   *rd = 0;
7047   for (mv_mode = 0; mv_mode < MAX_MV_MODE; ++mv_mode) {
7048     double this_rd;
7049     int_mv this_mv;
7050     if (mv_mode == NEW_MV_MODE) {
7051       continue;
7052     }
7053     this_rd = eval_mv_mode(mv_mode, cpi, x, gf_picture, motion_field, frame_idx,
7054                            tpl_frame, rf_idx, bsize, mi_row, mi_col, &this_mv);
7055     if (update == 0) {
7056       *rd = this_rd;
7057       *mv = this_mv;
7058       best_mv_mode = mv_mode;
7059       update = 1;
7060     } else {
7061       if (this_rd < *rd) {
7062         *rd = this_rd;
7063         *mv = this_mv;
7064         best_mv_mode = mv_mode;
7065       }
7066     }
7067   }
7068   return best_mv_mode;
7069 }
7070 
7071 static void predict_mv_mode(VP9_COMP *cpi, MACROBLOCK *x,
7072                             GF_PICTURE *gf_picture, MotionField *motion_field,
7073                             int frame_idx, TplDepFrame *tpl_frame, int rf_idx,
7074                             BLOCK_SIZE bsize, int mi_row, int mi_col) {
7075   const int mi_height = num_8x8_blocks_high_lookup[bsize];
7076   const int mi_width = num_8x8_blocks_wide_lookup[bsize];
7077   int tmp_mv_mode_arr[kMvPreCheckSize];
7078   int *mv_mode_arr = tpl_frame->mv_mode_arr[rf_idx];
7079   double *rd_diff_arr = tpl_frame->rd_diff_arr[rf_idx];
7080   int_mv *select_mv_arr = cpi->select_mv_arr;
7081   int_mv tmp_select_mv_arr[kMvPreCheckSize];
7082   int stride = tpl_frame->stride;
7083   double new_mv_rd = 0;
7084   double no_new_mv_rd = 0;
7085   double this_new_mv_rd = 0;
7086   double this_no_new_mv_rd = 0;
7087   int idx;
7088   int tmp_idx;
7089   assert(kMvPreCheckSize == (kMvPreCheckLines * (kMvPreCheckLines + 1)) >> 1);
7090 
7091   // no new mv
7092   // diagonal scan order
7093   tmp_idx = 0;
7094   for (idx = 0; idx < kMvPreCheckLines; ++idx) {
7095     int r;
7096     for (r = 0; r <= idx; ++r) {
7097       int c = idx - r;
7098       int nb_row = mi_row + r * mi_height;
7099       int nb_col = mi_col + c * mi_width;
7100       if (nb_row < tpl_frame->mi_rows && nb_col < tpl_frame->mi_cols) {
7101         double this_rd;
7102         int_mv *mv = &select_mv_arr[nb_row * stride + nb_col];
7103         mv_mode_arr[nb_row * stride + nb_col] = find_best_ref_mv_mode(
7104             cpi, x, gf_picture, motion_field, frame_idx, tpl_frame, rf_idx,
7105             bsize, nb_row, nb_col, &this_rd, mv);
7106         if (r == 0 && c == 0) {
7107           this_no_new_mv_rd = this_rd;
7108         }
7109         no_new_mv_rd += this_rd;
7110         tmp_mv_mode_arr[tmp_idx] = mv_mode_arr[nb_row * stride + nb_col];
7111         tmp_select_mv_arr[tmp_idx] = select_mv_arr[nb_row * stride + nb_col];
7112         ++tmp_idx;
7113       }
7114     }
7115   }
7116 
7117   // new mv
7118   mv_mode_arr[mi_row * stride + mi_col] = NEW_MV_MODE;
7119   this_new_mv_rd = eval_mv_mode(
7120       NEW_MV_MODE, cpi, x, gf_picture, motion_field, frame_idx, tpl_frame,
7121       rf_idx, bsize, mi_row, mi_col, &select_mv_arr[mi_row * stride + mi_col]);
7122   new_mv_rd = this_new_mv_rd;
7123   // We start from idx = 1 because idx = 0 is evaluated as NEW_MV_MODE
7124   // beforehand.
7125   for (idx = 1; idx < kMvPreCheckLines; ++idx) {
7126     int r;
7127     for (r = 0; r <= idx; ++r) {
7128       int c = idx - r;
7129       int nb_row = mi_row + r * mi_height;
7130       int nb_col = mi_col + c * mi_width;
7131       if (nb_row < tpl_frame->mi_rows && nb_col < tpl_frame->mi_cols) {
7132         double this_rd;
7133         int_mv *mv = &select_mv_arr[nb_row * stride + nb_col];
7134         mv_mode_arr[nb_row * stride + nb_col] = find_best_ref_mv_mode(
7135             cpi, x, gf_picture, motion_field, frame_idx, tpl_frame, rf_idx,
7136             bsize, nb_row, nb_col, &this_rd, mv);
7137         new_mv_rd += this_rd;
7138       }
7139     }
7140   }
7141 
7142   // update best_mv_mode
7143   tmp_idx = 0;
7144   if (no_new_mv_rd < new_mv_rd) {
7145     for (idx = 0; idx < kMvPreCheckLines; ++idx) {
7146       int r;
7147       for (r = 0; r <= idx; ++r) {
7148         int c = idx - r;
7149         int nb_row = mi_row + r * mi_height;
7150         int nb_col = mi_col + c * mi_width;
7151         if (nb_row < tpl_frame->mi_rows && nb_col < tpl_frame->mi_cols) {
7152           mv_mode_arr[nb_row * stride + nb_col] = tmp_mv_mode_arr[tmp_idx];
7153           select_mv_arr[nb_row * stride + nb_col] = tmp_select_mv_arr[tmp_idx];
7154           ++tmp_idx;
7155         }
7156       }
7157     }
7158     rd_diff_arr[mi_row * stride + mi_col] = 0;
7159   } else {
7160     rd_diff_arr[mi_row * stride + mi_col] =
7161         (no_new_mv_rd - this_no_new_mv_rd) - (new_mv_rd - this_new_mv_rd);
7162   }
7163 }
7164 
7165 static void predict_mv_mode_arr(VP9_COMP *cpi, MACROBLOCK *x,
7166                                 GF_PICTURE *gf_picture,
7167                                 MotionField *motion_field, int frame_idx,
7168                                 TplDepFrame *tpl_frame, int rf_idx,
7169                                 BLOCK_SIZE bsize) {
7170   const int mi_height = num_8x8_blocks_high_lookup[bsize];
7171   const int mi_width = num_8x8_blocks_wide_lookup[bsize];
7172   const int unit_rows = tpl_frame->mi_rows / mi_height;
7173   const int unit_cols = tpl_frame->mi_cols / mi_width;
7174   const int max_diagonal_lines = unit_rows + unit_cols - 1;
7175   int idx;
7176   for (idx = 0; idx < max_diagonal_lines; ++idx) {
7177     int r;
7178     for (r = VPXMAX(idx - unit_cols + 1, 0); r <= VPXMIN(idx, unit_rows - 1);
7179          ++r) {
7180       int c = idx - r;
7181       int mi_row = r * mi_height;
7182       int mi_col = c * mi_width;
7183       assert(c >= 0 && c < unit_cols);
7184       assert(mi_row >= 0 && mi_row < tpl_frame->mi_rows);
7185       assert(mi_col >= 0 && mi_col < tpl_frame->mi_cols);
7186       predict_mv_mode(cpi, x, gf_picture, motion_field, frame_idx, tpl_frame,
7187                       rf_idx, bsize, mi_row, mi_col);
7188     }
7189   }
7190 }
7191 
7192 static void do_motion_search(VP9_COMP *cpi, ThreadData *td,
7193                              MotionField *motion_field, int frame_idx,
7194                              YV12_BUFFER_CONFIG *ref_frame, BLOCK_SIZE bsize,
7195                              int mi_row, int mi_col) {
7196   VP9_COMMON *cm = &cpi->common;
7197   MACROBLOCK *x = &td->mb;
7198   MACROBLOCKD *xd = &x->e_mbd;
7199   const int mb_y_offset =
7200       mi_row * MI_SIZE * xd->cur_buf->y_stride + mi_col * MI_SIZE;
7201   assert(ref_frame != NULL);
7202   set_mv_limits(cm, x, mi_row, mi_col);
7203   {
7204     int_mv mv = vp9_motion_field_mi_get_mv(motion_field, mi_row, mi_col);
7205     uint8_t *cur_frame_buf = xd->cur_buf->y_buffer + mb_y_offset;
7206     uint8_t *ref_frame_buf = ref_frame->y_buffer + mb_y_offset;
7207     const int stride = xd->cur_buf->y_stride;
7208     full_pixel_motion_search(cpi, td, motion_field, frame_idx, cur_frame_buf,
7209                              ref_frame_buf, stride, bsize, mi_row, mi_col,
7210                              &mv.as_mv);
7211     sub_pixel_motion_search(cpi, td, cur_frame_buf, ref_frame_buf, stride,
7212                             bsize, &mv.as_mv);
7213     vp9_motion_field_mi_set_mv(motion_field, mi_row, mi_col, mv);
7214   }
7215 }
7216 
7217 static void build_motion_field(
7218     VP9_COMP *cpi, int frame_idx,
7219     YV12_BUFFER_CONFIG *ref_frame[MAX_INTER_REF_FRAMES], BLOCK_SIZE bsize) {
7220   VP9_COMMON *cm = &cpi->common;
7221   ThreadData *td = &cpi->td;
7222   TplDepFrame *tpl_frame = &cpi->tpl_stats[frame_idx];
7223   const int mi_height = num_8x8_blocks_high_lookup[bsize];
7224   const int mi_width = num_8x8_blocks_wide_lookup[bsize];
7225   const int pw = num_4x4_blocks_wide_lookup[bsize] << 2;
7226   const int ph = num_4x4_blocks_high_lookup[bsize] << 2;
7227   int mi_row, mi_col;
7228   int rf_idx;
7229 
7230   tpl_frame->lambda = (pw * ph) >> 2;
7231   assert(pw * ph == tpl_frame->lambda << 2);
7232 
7233   for (rf_idx = 0; rf_idx < MAX_INTER_REF_FRAMES; ++rf_idx) {
7234     MotionField *motion_field = vp9_motion_field_info_get_motion_field(
7235         &cpi->motion_field_info, frame_idx, rf_idx, bsize);
7236     if (ref_frame[rf_idx] == NULL) {
7237       continue;
7238     }
7239     vp9_motion_field_reset_mvs(motion_field);
7240     for (mi_row = 0; mi_row < cm->mi_rows; mi_row += mi_height) {
7241       for (mi_col = 0; mi_col < cm->mi_cols; mi_col += mi_width) {
7242         do_motion_search(cpi, td, motion_field, frame_idx, ref_frame[rf_idx],
7243                          bsize, mi_row, mi_col);
7244       }
7245     }
7246   }
7247 }
7248 #endif  // CONFIG_NON_GREEDY_MV
7249 
7250 static void mc_flow_dispenser(VP9_COMP *cpi, GF_PICTURE *gf_picture,
7251                               int frame_idx, BLOCK_SIZE bsize) {
7252   TplDepFrame *tpl_frame = &cpi->tpl_stats[frame_idx];
7253   YV12_BUFFER_CONFIG *this_frame = gf_picture[frame_idx].frame;
7254   YV12_BUFFER_CONFIG *ref_frame[MAX_INTER_REF_FRAMES] = { NULL, NULL, NULL };
7255 
7256   VP9_COMMON *cm = &cpi->common;
7257   struct scale_factors sf;
7258   int rdmult, idx;
7259   ThreadData *td = &cpi->td;
7260   MACROBLOCK *x = &td->mb;
7261   MACROBLOCKD *xd = &x->e_mbd;
7262   int mi_row, mi_col;
7263 
7264 #if CONFIG_VP9_HIGHBITDEPTH
7265   DECLARE_ALIGNED(16, uint16_t, predictor16[32 * 32 * 3]);
7266   DECLARE_ALIGNED(16, uint8_t, predictor8[32 * 32 * 3]);
7267   uint8_t *predictor;
7268 #else
7269   DECLARE_ALIGNED(16, uint8_t, predictor[32 * 32 * 3]);
7270 #endif
7271   DECLARE_ALIGNED(16, int16_t, src_diff[32 * 32]);
7272   DECLARE_ALIGNED(16, tran_low_t, coeff[32 * 32]);
7273   DECLARE_ALIGNED(16, tran_low_t, qcoeff[32 * 32]);
7274   DECLARE_ALIGNED(16, tran_low_t, dqcoeff[32 * 32]);
7275 
7276   const TX_SIZE tx_size = max_txsize_lookup[bsize];
7277   const int mi_height = num_8x8_blocks_high_lookup[bsize];
7278   const int mi_width = num_8x8_blocks_wide_lookup[bsize];
7279   int64_t recon_error, sse;
7280 #if CONFIG_NON_GREEDY_MV
7281   int square_block_idx;
7282   int rf_idx;
7283 #endif
7284 
7285   // Setup scaling factor
7286 #if CONFIG_VP9_HIGHBITDEPTH
7287   vp9_setup_scale_factors_for_frame(
7288       &sf, this_frame->y_crop_width, this_frame->y_crop_height,
7289       this_frame->y_crop_width, this_frame->y_crop_height,
7290       cpi->common.use_highbitdepth);
7291 
7292   if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH)
7293     predictor = CONVERT_TO_BYTEPTR(predictor16);
7294   else
7295     predictor = predictor8;
7296 #else
7297   vp9_setup_scale_factors_for_frame(
7298       &sf, this_frame->y_crop_width, this_frame->y_crop_height,
7299       this_frame->y_crop_width, this_frame->y_crop_height);
7300 #endif  // CONFIG_VP9_HIGHBITDEPTH
7301 
7302   // Prepare reference frame pointers. If any reference frame slot is
7303   // unavailable, the pointer will be set to Null.
7304   for (idx = 0; idx < MAX_INTER_REF_FRAMES; ++idx) {
7305     int rf_idx = gf_picture[frame_idx].ref_frame[idx];
7306     if (rf_idx != -1) ref_frame[idx] = gf_picture[rf_idx].frame;
7307   }
7308 
7309   xd->mi = cm->mi_grid_visible;
7310   xd->mi[0] = cm->mi;
7311   xd->cur_buf = this_frame;
7312 
7313   // Get rd multiplier set up.
7314   rdmult = vp9_compute_rd_mult_based_on_qindex(cpi, tpl_frame->base_qindex);
7315   set_error_per_bit(&cpi->td.mb, rdmult);
7316   vp9_initialize_me_consts(cpi, &cpi->td.mb, tpl_frame->base_qindex);
7317 
7318   tpl_frame->is_valid = 1;
7319 
7320   cm->base_qindex = tpl_frame->base_qindex;
7321   vp9_frame_init_quantizer(cpi);
7322 
7323 #if CONFIG_NON_GREEDY_MV
7324   for (square_block_idx = 0; square_block_idx < SQUARE_BLOCK_SIZES;
7325        ++square_block_idx) {
7326     BLOCK_SIZE square_bsize = square_block_idx_to_bsize(square_block_idx);
7327     build_motion_field(cpi, frame_idx, ref_frame, square_bsize);
7328   }
7329   for (rf_idx = 0; rf_idx < MAX_INTER_REF_FRAMES; ++rf_idx) {
7330     int ref_frame_idx = gf_picture[frame_idx].ref_frame[rf_idx];
7331     if (ref_frame_idx != -1) {
7332       MotionField *motion_field = vp9_motion_field_info_get_motion_field(
7333           &cpi->motion_field_info, frame_idx, rf_idx, bsize);
7334       predict_mv_mode_arr(cpi, x, gf_picture, motion_field, frame_idx,
7335                           tpl_frame, rf_idx, bsize);
7336     }
7337   }
7338 #endif
7339 
7340   for (mi_row = 0; mi_row < cm->mi_rows; mi_row += mi_height) {
7341     for (mi_col = 0; mi_col < cm->mi_cols; mi_col += mi_width) {
7342       mode_estimation(cpi, x, xd, &sf, gf_picture, frame_idx, tpl_frame,
7343                       src_diff, coeff, qcoeff, dqcoeff, mi_row, mi_col, bsize,
7344                       tx_size, ref_frame, predictor, &recon_error, &sse);
7345       // Motion flow dependency dispenser.
7346       tpl_model_store(tpl_frame->tpl_stats_ptr, mi_row, mi_col, bsize,
7347                       tpl_frame->stride);
7348 
7349       tpl_model_update(cpi->tpl_stats, tpl_frame->tpl_stats_ptr, mi_row, mi_col,
7350                        bsize);
7351     }
7352   }
7353 }
7354 
7355 #if CONFIG_NON_GREEDY_MV
7356 #define DUMP_TPL_STATS 0
7357 #if DUMP_TPL_STATS
7358 static void dump_buf(uint8_t *buf, int stride, int row, int col, int h, int w) {
7359   int i, j;
7360   printf("%d %d\n", h, w);
7361   for (i = 0; i < h; ++i) {
7362     for (j = 0; j < w; ++j) {
7363       printf("%d ", buf[(row + i) * stride + col + j]);
7364     }
7365   }
7366   printf("\n");
7367 }
7368 
7369 static void dump_frame_buf(const YV12_BUFFER_CONFIG *frame_buf) {
7370   dump_buf(frame_buf->y_buffer, frame_buf->y_stride, 0, 0, frame_buf->y_height,
7371            frame_buf->y_width);
7372   dump_buf(frame_buf->u_buffer, frame_buf->uv_stride, 0, 0,
7373            frame_buf->uv_height, frame_buf->uv_width);
7374   dump_buf(frame_buf->v_buffer, frame_buf->uv_stride, 0, 0,
7375            frame_buf->uv_height, frame_buf->uv_width);
7376 }
7377 
7378 static void dump_tpl_stats(const VP9_COMP *cpi, int tpl_group_frames,
7379                            const GF_GROUP *gf_group,
7380                            const GF_PICTURE *gf_picture, BLOCK_SIZE bsize) {
7381   int frame_idx;
7382   const VP9_COMMON *cm = &cpi->common;
7383   int rf_idx;
7384   for (frame_idx = 1; frame_idx < tpl_group_frames; ++frame_idx) {
7385     for (rf_idx = 0; rf_idx < MAX_INTER_REF_FRAMES; ++rf_idx) {
7386       const TplDepFrame *tpl_frame = &cpi->tpl_stats[frame_idx];
7387       int mi_row, mi_col;
7388       int ref_frame_idx;
7389       const int mi_height = num_8x8_blocks_high_lookup[bsize];
7390       const int mi_width = num_8x8_blocks_wide_lookup[bsize];
7391       ref_frame_idx = gf_picture[frame_idx].ref_frame[rf_idx];
7392       if (ref_frame_idx != -1) {
7393         YV12_BUFFER_CONFIG *ref_frame_buf = gf_picture[ref_frame_idx].frame;
7394         const int gf_frame_offset = gf_group->frame_gop_index[frame_idx];
7395         const int ref_gf_frame_offset =
7396             gf_group->frame_gop_index[ref_frame_idx];
7397         printf("=\n");
7398         printf(
7399             "frame_idx %d mi_rows %d mi_cols %d bsize %d ref_frame_idx %d "
7400             "rf_idx %d gf_frame_offset %d ref_gf_frame_offset %d\n",
7401             frame_idx, cm->mi_rows, cm->mi_cols, mi_width * MI_SIZE,
7402             ref_frame_idx, rf_idx, gf_frame_offset, ref_gf_frame_offset);
7403         for (mi_row = 0; mi_row < cm->mi_rows; ++mi_row) {
7404           for (mi_col = 0; mi_col < cm->mi_cols; ++mi_col) {
7405             if ((mi_row % mi_height) == 0 && (mi_col % mi_width) == 0) {
7406               int_mv mv = vp9_motion_field_info_get_mv(&cpi->motion_field_info,
7407                                                        frame_idx, rf_idx, bsize,
7408                                                        mi_row, mi_col);
7409               printf("%d %d %d %d\n", mi_row, mi_col, mv.as_mv.row,
7410                      mv.as_mv.col);
7411             }
7412           }
7413         }
7414         for (mi_row = 0; mi_row < cm->mi_rows; ++mi_row) {
7415           for (mi_col = 0; mi_col < cm->mi_cols; ++mi_col) {
7416             if ((mi_row % mi_height) == 0 && (mi_col % mi_width) == 0) {
7417               const TplDepStats *tpl_ptr =
7418                   &tpl_frame
7419                        ->tpl_stats_ptr[mi_row * tpl_frame->stride + mi_col];
7420               printf("%f ", tpl_ptr->feature_score);
7421             }
7422           }
7423         }
7424         printf("\n");
7425 
7426         for (mi_row = 0; mi_row < cm->mi_rows; mi_row += mi_height) {
7427           for (mi_col = 0; mi_col < cm->mi_cols; mi_col += mi_width) {
7428             const int mv_mode =
7429                 tpl_frame
7430                     ->mv_mode_arr[rf_idx][mi_row * tpl_frame->stride + mi_col];
7431             printf("%d ", mv_mode);
7432           }
7433         }
7434         printf("\n");
7435 
7436         dump_frame_buf(gf_picture[frame_idx].frame);
7437         dump_frame_buf(ref_frame_buf);
7438       }
7439     }
7440   }
7441 }
7442 #endif  // DUMP_TPL_STATS
7443 #endif  // CONFIG_NON_GREEDY_MV
7444 
7445 static void init_tpl_buffer(VP9_COMP *cpi) {
7446   VP9_COMMON *cm = &cpi->common;
7447   int frame;
7448 
7449   const int mi_cols = mi_cols_aligned_to_sb(cm->mi_cols);
7450   const int mi_rows = mi_cols_aligned_to_sb(cm->mi_rows);
7451 #if CONFIG_NON_GREEDY_MV
7452   int rf_idx;
7453 
7454   vpx_free(cpi->select_mv_arr);
7455   CHECK_MEM_ERROR(
7456       cm, cpi->select_mv_arr,
7457       vpx_calloc(mi_rows * mi_cols * 4, sizeof(*cpi->select_mv_arr)));
7458 #endif
7459 
7460   // TODO(jingning): Reduce the actual memory use for tpl model build up.
7461   for (frame = 0; frame < MAX_ARF_GOP_SIZE; ++frame) {
7462     if (cpi->tpl_stats[frame].width >= mi_cols &&
7463         cpi->tpl_stats[frame].height >= mi_rows &&
7464         cpi->tpl_stats[frame].tpl_stats_ptr)
7465       continue;
7466 
7467 #if CONFIG_NON_GREEDY_MV
7468     for (rf_idx = 0; rf_idx < MAX_INTER_REF_FRAMES; ++rf_idx) {
7469       vpx_free(cpi->tpl_stats[frame].mv_mode_arr[rf_idx]);
7470       CHECK_MEM_ERROR(
7471           cm, cpi->tpl_stats[frame].mv_mode_arr[rf_idx],
7472           vpx_calloc(mi_rows * mi_cols * 4,
7473                      sizeof(*cpi->tpl_stats[frame].mv_mode_arr[rf_idx])));
7474       vpx_free(cpi->tpl_stats[frame].rd_diff_arr[rf_idx]);
7475       CHECK_MEM_ERROR(
7476           cm, cpi->tpl_stats[frame].rd_diff_arr[rf_idx],
7477           vpx_calloc(mi_rows * mi_cols * 4,
7478                      sizeof(*cpi->tpl_stats[frame].rd_diff_arr[rf_idx])));
7479     }
7480 #endif
7481     vpx_free(cpi->tpl_stats[frame].tpl_stats_ptr);
7482     CHECK_MEM_ERROR(cm, cpi->tpl_stats[frame].tpl_stats_ptr,
7483                     vpx_calloc(mi_rows * mi_cols,
7484                                sizeof(*cpi->tpl_stats[frame].tpl_stats_ptr)));
7485     cpi->tpl_stats[frame].is_valid = 0;
7486     cpi->tpl_stats[frame].width = mi_cols;
7487     cpi->tpl_stats[frame].height = mi_rows;
7488     cpi->tpl_stats[frame].stride = mi_cols;
7489     cpi->tpl_stats[frame].mi_rows = cm->mi_rows;
7490     cpi->tpl_stats[frame].mi_cols = cm->mi_cols;
7491   }
7492 
7493   for (frame = 0; frame < REF_FRAMES; ++frame) {
7494     cpi->enc_frame_buf[frame].mem_valid = 0;
7495     cpi->enc_frame_buf[frame].released = 1;
7496   }
7497 }
7498 
7499 static void free_tpl_buffer(VP9_COMP *cpi) {
7500   int frame;
7501 #if CONFIG_NON_GREEDY_MV
7502   vp9_free_motion_field_info(&cpi->motion_field_info);
7503   vpx_free(cpi->select_mv_arr);
7504 #endif
7505   for (frame = 0; frame < MAX_ARF_GOP_SIZE; ++frame) {
7506 #if CONFIG_NON_GREEDY_MV
7507     int rf_idx;
7508     for (rf_idx = 0; rf_idx < MAX_INTER_REF_FRAMES; ++rf_idx) {
7509       vpx_free(cpi->tpl_stats[frame].mv_mode_arr[rf_idx]);
7510       vpx_free(cpi->tpl_stats[frame].rd_diff_arr[rf_idx]);
7511     }
7512 #endif
7513     vpx_free(cpi->tpl_stats[frame].tpl_stats_ptr);
7514     cpi->tpl_stats[frame].is_valid = 0;
7515   }
7516 }
7517 
7518 #if CONFIG_RATE_CTRL
7519 static void accumulate_frame_tpl_stats(VP9_COMP *cpi) {
7520   VP9_COMMON *const cm = &cpi->common;
7521   const GF_GROUP *gf_group = &cpi->twopass.gf_group;
7522   int show_frame_count = 0;
7523   int frame_idx;
7524   // Accumulate tpl stats for each frame in the current group of picture.
7525   for (frame_idx = 1; frame_idx < gf_group->gf_group_size; ++frame_idx) {
7526     TplDepFrame *tpl_frame = &cpi->tpl_stats[frame_idx];
7527     TplDepStats *tpl_stats = tpl_frame->tpl_stats_ptr;
7528     const int tpl_stride = tpl_frame->stride;
7529     int64_t intra_cost_base = 0;
7530     int64_t inter_cost_base = 0;
7531     int64_t mc_dep_cost_base = 0;
7532     int64_t mc_ref_cost_base = 0;
7533     int64_t mc_flow_base = 0;
7534     int row, col;
7535 
7536     if (!tpl_frame->is_valid) continue;
7537 
7538     for (row = 0; row < cm->mi_rows && tpl_frame->is_valid; ++row) {
7539       for (col = 0; col < cm->mi_cols; ++col) {
7540         TplDepStats *this_stats = &tpl_stats[row * tpl_stride + col];
7541         intra_cost_base += this_stats->intra_cost;
7542         inter_cost_base += this_stats->inter_cost;
7543         mc_dep_cost_base += this_stats->mc_dep_cost;
7544         mc_ref_cost_base += this_stats->mc_ref_cost;
7545         mc_flow_base += this_stats->mc_flow;
7546       }
7547     }
7548 
7549     cpi->tpl_stats_info[show_frame_count].intra_cost = intra_cost_base;
7550     cpi->tpl_stats_info[show_frame_count].inter_cost = inter_cost_base;
7551     cpi->tpl_stats_info[show_frame_count].mc_dep_cost = mc_dep_cost_base;
7552     cpi->tpl_stats_info[show_frame_count].mc_ref_cost = mc_ref_cost_base;
7553     cpi->tpl_stats_info[show_frame_count].mc_flow = mc_flow_base;
7554 
7555     ++show_frame_count;
7556   }
7557 }
7558 #endif  // CONFIG_RATE_CTRL
7559 
7560 static void setup_tpl_stats(VP9_COMP *cpi) {
7561   GF_PICTURE gf_picture[MAX_ARF_GOP_SIZE];
7562   const GF_GROUP *gf_group = &cpi->twopass.gf_group;
7563   int tpl_group_frames = 0;
7564   int frame_idx;
7565   cpi->tpl_bsize = BLOCK_32X32;
7566 
7567   init_gop_frames(cpi, gf_picture, gf_group, &tpl_group_frames);
7568 
7569   init_tpl_stats(cpi);
7570 
7571   // Backward propagation from tpl_group_frames to 1.
7572   for (frame_idx = tpl_group_frames - 1; frame_idx > 0; --frame_idx) {
7573     if (gf_picture[frame_idx].update_type == USE_BUF_FRAME) continue;
7574     mc_flow_dispenser(cpi, gf_picture, frame_idx, cpi->tpl_bsize);
7575   }
7576 #if CONFIG_NON_GREEDY_MV
7577   cpi->tpl_ready = 1;
7578 #if DUMP_TPL_STATS
7579   dump_tpl_stats(cpi, tpl_group_frames, gf_group, gf_picture, cpi->tpl_bsize);
7580 #endif  // DUMP_TPL_STATS
7581 #endif  // CONFIG_NON_GREEDY_MV
7582 
7583 #if CONFIG_RATE_CTRL
7584   if (cpi->oxcf.use_simple_encode_api) {
7585     accumulate_frame_tpl_stats(cpi);
7586   }
7587 #endif  // CONFIG_RATE_CTRL
7588 }
7589 
7590 void vp9_get_ref_frame_info(FRAME_UPDATE_TYPE update_type, int ref_frame_flags,
7591                             RefCntBuffer *ref_frame_bufs[MAX_INTER_REF_FRAMES],
7592                             int *ref_frame_coding_indexes,
7593                             int *ref_frame_valid_list) {
7594   if (update_type != KF_UPDATE) {
7595     const VP9_REFFRAME inter_ref_flags[MAX_INTER_REF_FRAMES] = { VP9_LAST_FLAG,
7596                                                                  VP9_GOLD_FLAG,
7597                                                                  VP9_ALT_FLAG };
7598     int i;
7599     for (i = 0; i < MAX_INTER_REF_FRAMES; ++i) {
7600       assert(ref_frame_bufs[i] != NULL);
7601       ref_frame_coding_indexes[i] = ref_frame_bufs[i]->frame_coding_index;
7602       ref_frame_valid_list[i] = (ref_frame_flags & inter_ref_flags[i]) != 0;
7603     }
7604   } else {
7605     // No reference frame is available when this is a key frame.
7606     int i;
7607     for (i = 0; i < MAX_INTER_REF_FRAMES; ++i) {
7608       ref_frame_coding_indexes[i] = -1;
7609       ref_frame_valid_list[i] = 0;
7610     }
7611   }
7612 }
7613 
7614 void vp9_init_encode_frame_result(ENCODE_FRAME_RESULT *encode_frame_result) {
7615   encode_frame_result->show_idx = -1;  // Actual encoding doesn't happen.
7616 #if CONFIG_RATE_CTRL
7617   encode_frame_result->frame_coding_index = -1;
7618   vp9_zero(encode_frame_result->coded_frame);
7619   encode_frame_result->coded_frame.allocated = 0;
7620   init_rq_history(&encode_frame_result->rq_history);
7621 #endif  // CONFIG_RATE_CTRL
7622 }
7623 
7624 int vp9_get_compressed_data(VP9_COMP *cpi, unsigned int *frame_flags,
7625                             size_t *size, uint8_t *dest, int64_t *time_stamp,
7626                             int64_t *time_end, int flush,
7627                             ENCODE_FRAME_RESULT *encode_frame_result) {
7628   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
7629   VP9_COMMON *const cm = &cpi->common;
7630   BufferPool *const pool = cm->buffer_pool;
7631   RATE_CONTROL *const rc = &cpi->rc;
7632   struct vpx_usec_timer cmptimer;
7633   YV12_BUFFER_CONFIG *force_src_buffer = NULL;
7634   struct lookahead_entry *last_source = NULL;
7635   struct lookahead_entry *source = NULL;
7636   int arf_src_index;
7637   const int gf_group_index = cpi->twopass.gf_group.index;
7638   int i;
7639 
7640   if (is_one_pass_cbr_svc(cpi)) {
7641     vp9_one_pass_cbr_svc_start_layer(cpi);
7642   }
7643 
7644   vpx_usec_timer_start(&cmptimer);
7645 
7646   vp9_set_high_precision_mv(cpi, ALTREF_HIGH_PRECISION_MV);
7647 
7648   // Is multi-arf enabled.
7649   // Note that at the moment multi_arf is only configured for 2 pass VBR and
7650   // will not work properly with svc.
7651   // Enable the Jingning's new "multi_layer_arf" code if "enable_auto_arf"
7652   // is greater than or equal to 2.
7653   if ((oxcf->pass == 2) && !cpi->use_svc && (cpi->oxcf.enable_auto_arf >= 2))
7654     cpi->multi_layer_arf = 1;
7655   else
7656     cpi->multi_layer_arf = 0;
7657 
7658   // Normal defaults
7659   cm->reset_frame_context = 0;
7660   cm->refresh_frame_context = 1;
7661   if (!is_one_pass_cbr_svc(cpi)) {
7662     cpi->refresh_last_frame = 1;
7663     cpi->refresh_golden_frame = 0;
7664     cpi->refresh_alt_ref_frame = 0;
7665   }
7666 
7667   // Should we encode an arf frame.
7668   arf_src_index = get_arf_src_index(cpi);
7669 
7670   if (arf_src_index) {
7671     for (i = 0; i <= arf_src_index; ++i) {
7672       struct lookahead_entry *e = vp9_lookahead_peek(cpi->lookahead, i);
7673       // Avoid creating an alt-ref if there's a forced keyframe pending.
7674       if (e == NULL) {
7675         break;
7676       } else if (e->flags == VPX_EFLAG_FORCE_KF) {
7677         arf_src_index = 0;
7678         flush = 1;
7679         break;
7680       }
7681     }
7682   }
7683 
7684   // Clear arf index stack before group of pictures processing starts.
7685   if (gf_group_index == 1) {
7686     stack_init(cpi->twopass.gf_group.arf_index_stack, MAX_LAG_BUFFERS * 2);
7687     cpi->twopass.gf_group.stack_size = 0;
7688   }
7689 
7690   if (arf_src_index) {
7691     assert(arf_src_index <= rc->frames_to_key);
7692     if ((source = vp9_lookahead_peek(cpi->lookahead, arf_src_index)) != NULL) {
7693       cpi->alt_ref_source = source;
7694 
7695 #if !CONFIG_REALTIME_ONLY
7696       if ((oxcf->mode != REALTIME) && (oxcf->arnr_max_frames > 0) &&
7697           (oxcf->arnr_strength > 0)) {
7698         int bitrate = cpi->rc.avg_frame_bandwidth / 40;
7699         int not_low_bitrate = bitrate > ALT_REF_AQ_LOW_BITRATE_BOUNDARY;
7700 
7701         int not_last_frame = (cpi->lookahead->sz - arf_src_index > 1);
7702         not_last_frame |= ALT_REF_AQ_APPLY_TO_LAST_FRAME;
7703 
7704         // Produce the filtered ARF frame.
7705         vp9_temporal_filter(cpi, arf_src_index);
7706         vpx_extend_frame_borders(&cpi->alt_ref_buffer);
7707 
7708         // for small bitrates segmentation overhead usually
7709         // eats all bitrate gain from enabling delta quantizers
7710         if (cpi->oxcf.alt_ref_aq != 0 && not_low_bitrate && not_last_frame)
7711           vp9_alt_ref_aq_setup_mode(cpi->alt_ref_aq, cpi);
7712 
7713         force_src_buffer = &cpi->alt_ref_buffer;
7714       }
7715 #endif
7716       cm->show_frame = 0;
7717       cm->intra_only = 0;
7718       cpi->refresh_alt_ref_frame = 1;
7719       cpi->refresh_golden_frame = 0;
7720       cpi->refresh_last_frame = 0;
7721       rc->is_src_frame_alt_ref = 0;
7722       rc->source_alt_ref_pending = 0;
7723     } else {
7724       rc->source_alt_ref_pending = 0;
7725     }
7726   }
7727 
7728   if (!source) {
7729     // Get last frame source.
7730     if (cm->current_video_frame > 0) {
7731       if ((last_source = vp9_lookahead_peek(cpi->lookahead, -1)) == NULL)
7732         return -1;
7733     }
7734 
7735     // Read in the source frame.
7736     if (cpi->use_svc || cpi->svc.set_intra_only_frame)
7737       source = vp9_svc_lookahead_pop(cpi, cpi->lookahead, flush);
7738     else
7739       source = vp9_lookahead_pop(cpi->lookahead, flush);
7740 
7741     if (source != NULL) {
7742       cm->show_frame = 1;
7743       cm->intra_only = 0;
7744       // If the flags indicate intra frame, but if the current picture is for
7745       // spatial layer above first_spatial_layer_to_encode, it should not be an
7746       // intra picture.
7747       if ((source->flags & VPX_EFLAG_FORCE_KF) && cpi->use_svc &&
7748           cpi->svc.spatial_layer_id > cpi->svc.first_spatial_layer_to_encode) {
7749         source->flags &= ~(unsigned int)(VPX_EFLAG_FORCE_KF);
7750       }
7751 
7752       // Check to see if the frame should be encoded as an arf overlay.
7753       check_src_altref(cpi, source);
7754     }
7755   }
7756 
7757   if (source) {
7758     cpi->un_scaled_source = cpi->Source =
7759         force_src_buffer ? force_src_buffer : &source->img;
7760 
7761 #ifdef ENABLE_KF_DENOISE
7762     // Copy of raw source for metrics calculation.
7763     if (is_psnr_calc_enabled(cpi))
7764       vp9_copy_and_extend_frame(cpi->Source, &cpi->raw_unscaled_source);
7765 #endif
7766 
7767     cpi->unscaled_last_source = last_source != NULL ? &last_source->img : NULL;
7768 
7769     *time_stamp = source->ts_start;
7770     *time_end = source->ts_end;
7771     *frame_flags = (source->flags & VPX_EFLAG_FORCE_KF) ? FRAMEFLAGS_KEY : 0;
7772   } else {
7773     *size = 0;
7774     return -1;
7775   }
7776 
7777   if (source->ts_start < cpi->first_time_stamp_ever) {
7778     cpi->first_time_stamp_ever = source->ts_start;
7779     cpi->last_end_time_stamp_seen = source->ts_start;
7780   }
7781 
7782   // Clear down mmx registers
7783   vpx_clear_system_state();
7784 
7785   // adjust frame rates based on timestamps given
7786   if (cm->show_frame) {
7787     if (cpi->use_svc && cpi->svc.use_set_ref_frame_config &&
7788         cpi->svc.duration[cpi->svc.spatial_layer_id] > 0)
7789       vp9_svc_adjust_frame_rate(cpi);
7790     else
7791       adjust_frame_rate(cpi, source);
7792   }
7793 
7794   if (is_one_pass_cbr_svc(cpi)) {
7795     vp9_update_temporal_layer_framerate(cpi);
7796     vp9_restore_layer_context(cpi);
7797   }
7798 
7799   // Find a free buffer for the new frame, releasing the reference previously
7800   // held.
7801   if (cm->new_fb_idx != INVALID_IDX) {
7802     --pool->frame_bufs[cm->new_fb_idx].ref_count;
7803   }
7804   cm->new_fb_idx = get_free_fb(cm);
7805 
7806   if (cm->new_fb_idx == INVALID_IDX) return -1;
7807   cm->cur_frame = &pool->frame_bufs[cm->new_fb_idx];
7808   // If the frame buffer for current frame is the same as previous frame, MV in
7809   // the base layer shouldn't be used as it'll cause data race.
7810   if (cpi->svc.spatial_layer_id > 0 && cm->cur_frame == cm->prev_frame) {
7811     cpi->svc.use_base_mv = 0;
7812   }
7813   // Start with a 0 size frame.
7814   *size = 0;
7815 
7816   cpi->frame_flags = *frame_flags;
7817 
7818 #if !CONFIG_REALTIME_ONLY
7819   if ((oxcf->pass == 2) && !cpi->use_svc) {
7820     vp9_rc_get_second_pass_params(cpi);
7821   } else if (oxcf->pass == 1) {
7822     set_frame_size(cpi);
7823   }
7824 #endif  // !CONFIG_REALTIME_ONLY
7825 
7826   if (oxcf->pass != 1 && cpi->level_constraint.level_index >= 0 &&
7827       cpi->level_constraint.fail_flag == 0)
7828     level_rc_framerate(cpi, arf_src_index);
7829 
7830   if (cpi->oxcf.pass != 0 || cpi->use_svc || frame_is_intra_only(cm) == 1) {
7831     for (i = 0; i < REFS_PER_FRAME; ++i) cpi->scaled_ref_idx[i] = INVALID_IDX;
7832   }
7833 
7834   if (cpi->kmeans_data_arr_alloc == 0) {
7835     const int mi_cols = mi_cols_aligned_to_sb(cm->mi_cols);
7836     const int mi_rows = mi_cols_aligned_to_sb(cm->mi_rows);
7837 #if CONFIG_MULTITHREAD
7838     pthread_mutex_init(&cpi->kmeans_mutex, NULL);
7839 #endif
7840     CHECK_MEM_ERROR(
7841         cm, cpi->kmeans_data_arr,
7842         vpx_calloc(mi_rows * mi_cols, sizeof(*cpi->kmeans_data_arr)));
7843     cpi->kmeans_data_stride = mi_cols;
7844     cpi->kmeans_data_arr_alloc = 1;
7845   }
7846 
7847 #if CONFIG_NON_GREEDY_MV
7848   {
7849     const int mi_cols = mi_cols_aligned_to_sb(cm->mi_cols);
7850     const int mi_rows = mi_cols_aligned_to_sb(cm->mi_rows);
7851     Status status = vp9_alloc_motion_field_info(
7852         &cpi->motion_field_info, MAX_ARF_GOP_SIZE, mi_rows, mi_cols);
7853     if (status == STATUS_FAILED) {
7854       vpx_internal_error(&(cm)->error, VPX_CODEC_MEM_ERROR,
7855                          "vp9_alloc_motion_field_info failed");
7856     }
7857   }
7858 #endif  // CONFIG_NON_GREEDY_MV
7859 
7860   if (gf_group_index == 1 &&
7861       cpi->twopass.gf_group.update_type[gf_group_index] == ARF_UPDATE &&
7862       cpi->sf.enable_tpl_model) {
7863     init_tpl_buffer(cpi);
7864     vp9_estimate_qp_gop(cpi);
7865     setup_tpl_stats(cpi);
7866   }
7867 
7868 #if CONFIG_BITSTREAM_DEBUG
7869   assert(cpi->oxcf.max_threads == 0 &&
7870          "bitstream debug tool does not support multithreading");
7871   bitstream_queue_record_write();
7872 #endif
7873 #if CONFIG_BITSTREAM_DEBUG || CONFIG_MISMATCH_DEBUG
7874   bitstream_queue_set_frame_write(cm->current_video_frame * 2 + cm->show_frame);
7875 #endif
7876 
7877   cpi->td.mb.fp_src_pred = 0;
7878 #if CONFIG_REALTIME_ONLY
7879   (void)encode_frame_result;
7880   if (cpi->use_svc) {
7881     SvcEncode(cpi, size, dest, frame_flags);
7882   } else {
7883     // One pass encode
7884     Pass0Encode(cpi, size, dest, frame_flags);
7885   }
7886 #else  // !CONFIG_REALTIME_ONLY
7887   if (oxcf->pass == 1 && !cpi->use_svc) {
7888     const int lossless = is_lossless_requested(oxcf);
7889 #if CONFIG_VP9_HIGHBITDEPTH
7890     if (cpi->oxcf.use_highbitdepth)
7891       cpi->td.mb.fwd_txfm4x4 =
7892           lossless ? vp9_highbd_fwht4x4 : vpx_highbd_fdct4x4;
7893     else
7894       cpi->td.mb.fwd_txfm4x4 = lossless ? vp9_fwht4x4 : vpx_fdct4x4;
7895     cpi->td.mb.highbd_inv_txfm_add =
7896         lossless ? vp9_highbd_iwht4x4_add : vp9_highbd_idct4x4_add;
7897 #else
7898     cpi->td.mb.fwd_txfm4x4 = lossless ? vp9_fwht4x4 : vpx_fdct4x4;
7899 #endif  // CONFIG_VP9_HIGHBITDEPTH
7900     cpi->td.mb.inv_txfm_add = lossless ? vp9_iwht4x4_add : vp9_idct4x4_add;
7901     vp9_first_pass(cpi, source);
7902   } else if (oxcf->pass == 2 && !cpi->use_svc) {
7903     Pass2Encode(cpi, size, dest, frame_flags, encode_frame_result);
7904     vp9_twopass_postencode_update(cpi);
7905   } else if (cpi->use_svc) {
7906     SvcEncode(cpi, size, dest, frame_flags);
7907   } else {
7908     // One pass encode
7909     Pass0Encode(cpi, size, dest, frame_flags);
7910   }
7911 #endif  // CONFIG_REALTIME_ONLY
7912 
7913   if (cm->show_frame) cm->cur_show_frame_fb_idx = cm->new_fb_idx;
7914 
7915   if (cm->refresh_frame_context)
7916     cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
7917 
7918   // No frame encoded, or frame was dropped, release scaled references.
7919   if ((*size == 0) && (frame_is_intra_only(cm) == 0)) {
7920     release_scaled_references(cpi);
7921   }
7922 
7923   if (*size > 0) {
7924     cpi->droppable = !frame_is_reference(cpi);
7925   }
7926 
7927   // Save layer specific state.
7928   if (is_one_pass_cbr_svc(cpi) || ((cpi->svc.number_temporal_layers > 1 ||
7929                                     cpi->svc.number_spatial_layers > 1) &&
7930                                    oxcf->pass == 2)) {
7931     vp9_save_layer_context(cpi);
7932   }
7933 
7934   vpx_usec_timer_mark(&cmptimer);
7935   cpi->time_compress_data += vpx_usec_timer_elapsed(&cmptimer);
7936 
7937   if (cpi->keep_level_stats && oxcf->pass != 1)
7938     update_level_info(cpi, size, arf_src_index);
7939 
7940 #if CONFIG_INTERNAL_STATS
7941 
7942   if (oxcf->pass != 1) {
7943     double samples = 0.0;
7944     cpi->bytes += (int)(*size);
7945 
7946     if (cm->show_frame) {
7947       uint32_t bit_depth = 8;
7948       uint32_t in_bit_depth = 8;
7949       cpi->count++;
7950 #if CONFIG_VP9_HIGHBITDEPTH
7951       if (cm->use_highbitdepth) {
7952         in_bit_depth = cpi->oxcf.input_bit_depth;
7953         bit_depth = cm->bit_depth;
7954       }
7955 #endif
7956 
7957       if (cpi->b_calculate_psnr) {
7958         YV12_BUFFER_CONFIG *orig = cpi->raw_source_frame;
7959         YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
7960         YV12_BUFFER_CONFIG *pp = &cm->post_proc_buffer;
7961         PSNR_STATS psnr;
7962 #if CONFIG_VP9_HIGHBITDEPTH
7963         vpx_calc_highbd_psnr(orig, recon, &psnr, cpi->td.mb.e_mbd.bd,
7964                              in_bit_depth);
7965 #else
7966         vpx_calc_psnr(orig, recon, &psnr);
7967 #endif  // CONFIG_VP9_HIGHBITDEPTH
7968 
7969         adjust_image_stat(psnr.psnr[1], psnr.psnr[2], psnr.psnr[3],
7970                           psnr.psnr[0], &cpi->psnr);
7971         cpi->total_sq_error += psnr.sse[0];
7972         cpi->total_samples += psnr.samples[0];
7973         samples = psnr.samples[0];
7974 
7975         {
7976           PSNR_STATS psnr2;
7977           double frame_ssim2 = 0, weight = 0;
7978 #if CONFIG_VP9_POSTPROC
7979           if (vpx_alloc_frame_buffer(
7980                   pp, recon->y_crop_width, recon->y_crop_height,
7981                   cm->subsampling_x, cm->subsampling_y,
7982 #if CONFIG_VP9_HIGHBITDEPTH
7983                   cm->use_highbitdepth,
7984 #endif
7985                   VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment) < 0) {
7986             vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
7987                                "Failed to allocate post processing buffer");
7988           }
7989           {
7990             vp9_ppflags_t ppflags;
7991             ppflags.post_proc_flag = VP9D_DEBLOCK;
7992             ppflags.deblocking_level = 0;  // not used in vp9_post_proc_frame()
7993             ppflags.noise_level = 0;       // not used in vp9_post_proc_frame()
7994             vp9_post_proc_frame(cm, pp, &ppflags,
7995                                 cpi->un_scaled_source->y_width);
7996           }
7997 #endif
7998           vpx_clear_system_state();
7999 
8000 #if CONFIG_VP9_HIGHBITDEPTH
8001           vpx_calc_highbd_psnr(orig, pp, &psnr2, cpi->td.mb.e_mbd.bd,
8002                                cpi->oxcf.input_bit_depth);
8003 #else
8004           vpx_calc_psnr(orig, pp, &psnr2);
8005 #endif  // CONFIG_VP9_HIGHBITDEPTH
8006 
8007           cpi->totalp_sq_error += psnr2.sse[0];
8008           cpi->totalp_samples += psnr2.samples[0];
8009           adjust_image_stat(psnr2.psnr[1], psnr2.psnr[2], psnr2.psnr[3],
8010                             psnr2.psnr[0], &cpi->psnrp);
8011 
8012 #if CONFIG_VP9_HIGHBITDEPTH
8013           if (cm->use_highbitdepth) {
8014             frame_ssim2 = vpx_highbd_calc_ssim(orig, recon, &weight, bit_depth,
8015                                                in_bit_depth);
8016           } else {
8017             frame_ssim2 = vpx_calc_ssim(orig, recon, &weight);
8018           }
8019 #else
8020           frame_ssim2 = vpx_calc_ssim(orig, recon, &weight);
8021 #endif  // CONFIG_VP9_HIGHBITDEPTH
8022 
8023           cpi->worst_ssim = VPXMIN(cpi->worst_ssim, frame_ssim2);
8024           cpi->summed_quality += frame_ssim2 * weight;
8025           cpi->summed_weights += weight;
8026 
8027 #if CONFIG_VP9_HIGHBITDEPTH
8028           if (cm->use_highbitdepth) {
8029             frame_ssim2 = vpx_highbd_calc_ssim(orig, pp, &weight, bit_depth,
8030                                                in_bit_depth);
8031           } else {
8032             frame_ssim2 = vpx_calc_ssim(orig, pp, &weight);
8033           }
8034 #else
8035           frame_ssim2 = vpx_calc_ssim(orig, pp, &weight);
8036 #endif  // CONFIG_VP9_HIGHBITDEPTH
8037 
8038           cpi->summedp_quality += frame_ssim2 * weight;
8039           cpi->summedp_weights += weight;
8040 #if 0
8041           if (cm->show_frame) {
8042             FILE *f = fopen("q_used.stt", "a");
8043             fprintf(f, "%5d : Y%f7.3:U%f7.3:V%f7.3:F%f7.3:S%7.3f\n",
8044                     cpi->common.current_video_frame, psnr2.psnr[1],
8045                     psnr2.psnr[2], psnr2.psnr[3], psnr2.psnr[0], frame_ssim2);
8046             fclose(f);
8047           }
8048 #endif
8049         }
8050       }
8051       if (cpi->b_calculate_blockiness) {
8052 #if CONFIG_VP9_HIGHBITDEPTH
8053         if (!cm->use_highbitdepth)
8054 #endif
8055         {
8056           double frame_blockiness = vp9_get_blockiness(
8057               cpi->Source->y_buffer, cpi->Source->y_stride,
8058               cm->frame_to_show->y_buffer, cm->frame_to_show->y_stride,
8059               cpi->Source->y_width, cpi->Source->y_height);
8060           cpi->worst_blockiness =
8061               VPXMAX(cpi->worst_blockiness, frame_blockiness);
8062           cpi->total_blockiness += frame_blockiness;
8063         }
8064       }
8065 
8066       if (cpi->b_calculate_consistency) {
8067 #if CONFIG_VP9_HIGHBITDEPTH
8068         if (!cm->use_highbitdepth)
8069 #endif
8070         {
8071           double this_inconsistency = vpx_get_ssim_metrics(
8072               cpi->Source->y_buffer, cpi->Source->y_stride,
8073               cm->frame_to_show->y_buffer, cm->frame_to_show->y_stride,
8074               cpi->Source->y_width, cpi->Source->y_height, cpi->ssim_vars,
8075               &cpi->metrics, 1);
8076 
8077           const double peak = (double)((1 << cpi->oxcf.input_bit_depth) - 1);
8078           double consistency =
8079               vpx_sse_to_psnr(samples, peak, (double)cpi->total_inconsistency);
8080           if (consistency > 0.0)
8081             cpi->worst_consistency =
8082                 VPXMIN(cpi->worst_consistency, consistency);
8083           cpi->total_inconsistency += this_inconsistency;
8084         }
8085       }
8086 
8087       {
8088         double y, u, v, frame_all;
8089         frame_all = vpx_calc_fastssim(cpi->Source, cm->frame_to_show, &y, &u,
8090                                       &v, bit_depth, in_bit_depth);
8091         adjust_image_stat(y, u, v, frame_all, &cpi->fastssim);
8092       }
8093       {
8094         double y, u, v, frame_all;
8095         frame_all = vpx_psnrhvs(cpi->Source, cm->frame_to_show, &y, &u, &v,
8096                                 bit_depth, in_bit_depth);
8097         adjust_image_stat(y, u, v, frame_all, &cpi->psnrhvs);
8098       }
8099     }
8100   }
8101 
8102 #endif
8103 
8104   if (is_one_pass_cbr_svc(cpi)) {
8105     if (cm->show_frame) {
8106       ++cpi->svc.spatial_layer_to_encode;
8107       if (cpi->svc.spatial_layer_to_encode >= cpi->svc.number_spatial_layers)
8108         cpi->svc.spatial_layer_to_encode = 0;
8109     }
8110   }
8111 
8112   vpx_clear_system_state();
8113   return 0;
8114 }
8115 
8116 int vp9_get_preview_raw_frame(VP9_COMP *cpi, YV12_BUFFER_CONFIG *dest,
8117                               vp9_ppflags_t *flags) {
8118   VP9_COMMON *cm = &cpi->common;
8119 #if !CONFIG_VP9_POSTPROC
8120   (void)flags;
8121 #endif
8122 
8123   if (!cm->show_frame) {
8124     return -1;
8125   } else {
8126     int ret;
8127 #if CONFIG_VP9_POSTPROC
8128     ret = vp9_post_proc_frame(cm, dest, flags, cpi->un_scaled_source->y_width);
8129 #else
8130     if (cm->frame_to_show) {
8131       *dest = *cm->frame_to_show;
8132       dest->y_width = cm->width;
8133       dest->y_height = cm->height;
8134       dest->uv_width = cm->width >> cm->subsampling_x;
8135       dest->uv_height = cm->height >> cm->subsampling_y;
8136       ret = 0;
8137     } else {
8138       ret = -1;
8139     }
8140 #endif  // !CONFIG_VP9_POSTPROC
8141     vpx_clear_system_state();
8142     return ret;
8143   }
8144 }
8145 
8146 int vp9_set_internal_size(VP9_COMP *cpi, VPX_SCALING horiz_mode,
8147                           VPX_SCALING vert_mode) {
8148   VP9_COMMON *cm = &cpi->common;
8149   int hr = 0, hs = 0, vr = 0, vs = 0;
8150 
8151   if (horiz_mode > ONETWO || vert_mode > ONETWO) return -1;
8152 
8153   Scale2Ratio(horiz_mode, &hr, &hs);
8154   Scale2Ratio(vert_mode, &vr, &vs);
8155 
8156   // always go to the next whole number
8157   cm->width = (hs - 1 + cpi->oxcf.width * hr) / hs;
8158   cm->height = (vs - 1 + cpi->oxcf.height * vr) / vs;
8159   if (cm->current_video_frame) {
8160     assert(cm->width <= cpi->initial_width);
8161     assert(cm->height <= cpi->initial_height);
8162   }
8163 
8164   update_frame_size(cpi);
8165 
8166   return 0;
8167 }
8168 
8169 int vp9_set_size_literal(VP9_COMP *cpi, unsigned int width,
8170                          unsigned int height) {
8171   VP9_COMMON *cm = &cpi->common;
8172 #if CONFIG_VP9_HIGHBITDEPTH
8173   update_initial_width(cpi, cm->use_highbitdepth, 1, 1);
8174 #else
8175   update_initial_width(cpi, 0, 1, 1);
8176 #endif  // CONFIG_VP9_HIGHBITDEPTH
8177 
8178 #if CONFIG_VP9_TEMPORAL_DENOISING
8179   setup_denoiser_buffer(cpi);
8180 #endif
8181   alloc_raw_frame_buffers(cpi);
8182   if (width) {
8183     cm->width = width;
8184     if (cm->width > cpi->initial_width) {
8185       cm->width = cpi->initial_width;
8186       printf("Warning: Desired width too large, changed to %d\n", cm->width);
8187     }
8188   }
8189 
8190   if (height) {
8191     cm->height = height;
8192     if (cm->height > cpi->initial_height) {
8193       cm->height = cpi->initial_height;
8194       printf("Warning: Desired height too large, changed to %d\n", cm->height);
8195     }
8196   }
8197   assert(cm->width <= cpi->initial_width);
8198   assert(cm->height <= cpi->initial_height);
8199 
8200   update_frame_size(cpi);
8201 
8202   return 0;
8203 }
8204 
8205 void vp9_set_svc(VP9_COMP *cpi, int use_svc) {
8206   cpi->use_svc = use_svc;
8207   return;
8208 }
8209 
8210 int vp9_get_quantizer(const VP9_COMP *cpi) { return cpi->common.base_qindex; }
8211 
8212 void vp9_apply_encoding_flags(VP9_COMP *cpi, vpx_enc_frame_flags_t flags) {
8213   if (flags &
8214       (VP8_EFLAG_NO_REF_LAST | VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF)) {
8215     int ref = 7;
8216 
8217     if (flags & VP8_EFLAG_NO_REF_LAST) ref ^= VP9_LAST_FLAG;
8218 
8219     if (flags & VP8_EFLAG_NO_REF_GF) ref ^= VP9_GOLD_FLAG;
8220 
8221     if (flags & VP8_EFLAG_NO_REF_ARF) ref ^= VP9_ALT_FLAG;
8222 
8223     vp9_use_as_reference(cpi, ref);
8224   }
8225 
8226   if (flags &
8227       (VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF |
8228        VP8_EFLAG_FORCE_GF | VP8_EFLAG_FORCE_ARF)) {
8229     int upd = 7;
8230 
8231     if (flags & VP8_EFLAG_NO_UPD_LAST) upd ^= VP9_LAST_FLAG;
8232 
8233     if (flags & VP8_EFLAG_NO_UPD_GF) upd ^= VP9_GOLD_FLAG;
8234 
8235     if (flags & VP8_EFLAG_NO_UPD_ARF) upd ^= VP9_ALT_FLAG;
8236 
8237     vp9_update_reference(cpi, upd);
8238   }
8239 
8240   if (flags & VP8_EFLAG_NO_UPD_ENTROPY) {
8241     vp9_update_entropy(cpi, 0);
8242   }
8243 }
8244 
8245 void vp9_set_row_mt(VP9_COMP *cpi) {
8246   // Enable row based multi-threading for supported modes of encoding
8247   cpi->row_mt = 0;
8248   if (((cpi->oxcf.mode == GOOD || cpi->oxcf.mode == BEST) &&
8249        cpi->oxcf.speed < 5 && cpi->oxcf.pass == 1) &&
8250       cpi->oxcf.row_mt && !cpi->use_svc)
8251     cpi->row_mt = 1;
8252 
8253   if (cpi->oxcf.mode == GOOD && cpi->oxcf.speed < 5 &&
8254       (cpi->oxcf.pass == 0 || cpi->oxcf.pass == 2) && cpi->oxcf.row_mt &&
8255       !cpi->use_svc)
8256     cpi->row_mt = 1;
8257 
8258   // In realtime mode, enable row based multi-threading for all the speed levels
8259   // where non-rd path is used.
8260   if (cpi->oxcf.mode == REALTIME && cpi->oxcf.speed >= 5 && cpi->oxcf.row_mt) {
8261     cpi->row_mt = 1;
8262   }
8263 
8264   if (cpi->row_mt)
8265     cpi->row_mt_bit_exact = 1;
8266   else
8267     cpi->row_mt_bit_exact = 0;
8268 }
8269