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