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