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