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