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