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