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