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