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