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