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