]> granicus.if.org Git - libvpx/blob - vp9/encoder/vp9_encoder.c
10b419966872f18c5fad6454dd304d3b81608959
[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 <math.h>
12 #include <stdio.h>
13 #include <limits.h>
14
15 #include "./vp9_rtcd.h"
16 #include "./vpx_config.h"
17 #include "./vpx_dsp_rtcd.h"
18 #include "./vpx_scale_rtcd.h"
19 #include "vpx_dsp/psnr.h"
20 #include "vpx_dsp/vpx_dsp_common.h"
21 #include "vpx_dsp/vpx_filter.h"
22 #if CONFIG_INTERNAL_STATS
23 #include "vpx_dsp/ssim.h"
24 #endif
25 #include "vpx_ports/mem.h"
26 #include "vpx_ports/system_state.h"
27 #include "vpx_ports/vpx_timer.h"
28
29 #include "vp9/common/vp9_alloccommon.h"
30 #include "vp9/common/vp9_filter.h"
31 #include "vp9/common/vp9_idct.h"
32 #if CONFIG_VP9_POSTPROC
33 #include "vp9/common/vp9_postproc.h"
34 #endif
35 #include "vp9/common/vp9_reconinter.h"
36 #include "vp9/common/vp9_reconintra.h"
37 #include "vp9/common/vp9_tile_common.h"
38
39 #include "vp9/encoder/vp9_alt_ref_aq.h"
40 #include "vp9/encoder/vp9_aq_360.h"
41 #include "vp9/encoder/vp9_aq_complexity.h"
42 #include "vp9/encoder/vp9_aq_cyclicrefresh.h"
43 #include "vp9/encoder/vp9_aq_variance.h"
44 #include "vp9/encoder/vp9_bitstream.h"
45 #include "vp9/encoder/vp9_context_tree.h"
46 #include "vp9/encoder/vp9_encodeframe.h"
47 #include "vp9/encoder/vp9_encodemv.h"
48 #include "vp9/encoder/vp9_encoder.h"
49 #include "vp9/encoder/vp9_extend.h"
50 #include "vp9/encoder/vp9_ethread.h"
51 #include "vp9/encoder/vp9_firstpass.h"
52 #include "vp9/encoder/vp9_mbgraph.h"
53 #include "vp9/encoder/vp9_multi_thread.h"
54 #include "vp9/encoder/vp9_noise_estimate.h"
55 #include "vp9/encoder/vp9_picklpf.h"
56 #include "vp9/encoder/vp9_ratectrl.h"
57 #include "vp9/encoder/vp9_rd.h"
58 #include "vp9/encoder/vp9_resize.h"
59 #include "vp9/encoder/vp9_segmentation.h"
60 #include "vp9/encoder/vp9_skin_detection.h"
61 #include "vp9/encoder/vp9_speed_features.h"
62 #include "vp9/encoder/vp9_svc_layercontext.h"
63 #include "vp9/encoder/vp9_temporal_filter.h"
64
65 #define AM_SEGMENT_ID_INACTIVE 7
66 #define AM_SEGMENT_ID_ACTIVE 0
67
68 #define ALTREF_HIGH_PRECISION_MV 1     // Whether to use high precision mv
69                                        //  for altref computation.
70 #define HIGH_PRECISION_MV_QTHRESH 200  // Q threshold for high precision
71                                        // mv. Choose a very high value for
72                                        // now so that HIGH_PRECISION is always
73                                        // chosen.
74
75 #define FRAME_SIZE_FACTOR 128  // empirical params for context model threshold
76 #define FRAME_RATE_FACTOR 8
77
78 #ifdef OUTPUT_YUV_DENOISED
79 FILE *yuv_denoised_file = NULL;
80 #endif
81 #ifdef OUTPUT_YUV_SKINMAP
82 static FILE *yuv_skinmap_file = NULL;
83 #endif
84 #ifdef OUTPUT_YUV_REC
85 FILE *yuv_rec_file;
86 #endif
87
88 #if 0
89 FILE *framepsnr;
90 FILE *kf_list;
91 FILE *keyfile;
92 #endif
93
94 #ifdef ENABLE_KF_DENOISE
95 // Test condition for spatial denoise of source.
96 static int is_spatial_denoise_enabled(VP9_COMP *cpi) {
97   VP9_COMMON *const cm = &cpi->common;
98   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
99
100   return (oxcf->pass != 1) && !is_lossless_requested(&cpi->oxcf) &&
101          frame_is_intra_only(cm);
102 }
103 #endif
104
105 // compute adaptive threshold for skip recoding
106 static int compute_context_model_thresh(const VP9_COMP *const cpi) {
107   const VP9_COMMON *const cm = &cpi->common;
108   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
109   const int frame_size = (cm->width * cm->height) >> 10;
110   const int bitrate = (int)(oxcf->target_bandwidth >> 10);
111   const int qindex_factor = cm->base_qindex + (MAXQ >> 1);
112
113   // This equation makes the threshold adaptive to frame size.
114   // Coding gain obtained by recoding comes from alternate frames of large
115   // content change. We skip recoding if the difference of previous and current
116   // frame context probability model is less than a certain threshold.
117   // The first component is the most critical part to guarantee adaptivity.
118   // Other parameters are estimated based on normal setting of hd resolution
119   // parameters. e.g frame_size = 1920x1080, bitrate = 8000, qindex_factor < 50
120   const int thresh =
121       ((FRAME_SIZE_FACTOR * frame_size - FRAME_RATE_FACTOR * bitrate) *
122        qindex_factor) >>
123       9;
124
125   return thresh;
126 }
127
128 // compute the total cost difference between current
129 // and previous frame context prob model.
130 static int compute_context_model_diff(const VP9_COMMON *const cm) {
131   const FRAME_CONTEXT *const pre_fc =
132       &cm->frame_contexts[cm->frame_context_idx];
133   const FRAME_CONTEXT *const cur_fc = cm->fc;
134   const FRAME_COUNTS *counts = &cm->counts;
135   vpx_prob pre_last_prob, cur_last_prob;
136   int diff = 0;
137   int i, j, k, l, m, n;
138
139   // y_mode_prob
140   for (i = 0; i < BLOCK_SIZE_GROUPS; ++i) {
141     for (j = 0; j < INTRA_MODES - 1; ++j) {
142       diff += (int)counts->y_mode[i][j] *
143               (pre_fc->y_mode_prob[i][j] - cur_fc->y_mode_prob[i][j]);
144     }
145     pre_last_prob = MAX_PROB - pre_fc->y_mode_prob[i][INTRA_MODES - 2];
146     cur_last_prob = MAX_PROB - cur_fc->y_mode_prob[i][INTRA_MODES - 2];
147
148     diff += (int)counts->y_mode[i][INTRA_MODES - 1] *
149             (pre_last_prob - cur_last_prob);
150   }
151
152   // uv_mode_prob
153   for (i = 0; i < INTRA_MODES; ++i) {
154     for (j = 0; j < INTRA_MODES - 1; ++j) {
155       diff += (int)counts->uv_mode[i][j] *
156               (pre_fc->uv_mode_prob[i][j] - cur_fc->uv_mode_prob[i][j]);
157     }
158     pre_last_prob = MAX_PROB - pre_fc->uv_mode_prob[i][INTRA_MODES - 2];
159     cur_last_prob = MAX_PROB - cur_fc->uv_mode_prob[i][INTRA_MODES - 2];
160
161     diff += (int)counts->uv_mode[i][INTRA_MODES - 1] *
162             (pre_last_prob - cur_last_prob);
163   }
164
165   // partition_prob
166   for (i = 0; i < PARTITION_CONTEXTS; ++i) {
167     for (j = 0; j < PARTITION_TYPES - 1; ++j) {
168       diff += (int)counts->partition[i][j] *
169               (pre_fc->partition_prob[i][j] - cur_fc->partition_prob[i][j]);
170     }
171     pre_last_prob = MAX_PROB - pre_fc->partition_prob[i][PARTITION_TYPES - 2];
172     cur_last_prob = MAX_PROB - cur_fc->partition_prob[i][PARTITION_TYPES - 2];
173
174     diff += (int)counts->partition[i][PARTITION_TYPES - 1] *
175             (pre_last_prob - cur_last_prob);
176   }
177
178   // coef_probs
179   for (i = 0; i < TX_SIZES; ++i) {
180     for (j = 0; j < PLANE_TYPES; ++j) {
181       for (k = 0; k < REF_TYPES; ++k) {
182         for (l = 0; l < COEF_BANDS; ++l) {
183           for (m = 0; m < BAND_COEFF_CONTEXTS(l); ++m) {
184             for (n = 0; n < UNCONSTRAINED_NODES; ++n) {
185               diff += (int)counts->coef[i][j][k][l][m][n] *
186                       (pre_fc->coef_probs[i][j][k][l][m][n] -
187                        cur_fc->coef_probs[i][j][k][l][m][n]);
188             }
189
190             pre_last_prob =
191                 MAX_PROB -
192                 pre_fc->coef_probs[i][j][k][l][m][UNCONSTRAINED_NODES - 1];
193             cur_last_prob =
194                 MAX_PROB -
195                 cur_fc->coef_probs[i][j][k][l][m][UNCONSTRAINED_NODES - 1];
196
197             diff += (int)counts->coef[i][j][k][l][m][UNCONSTRAINED_NODES] *
198                     (pre_last_prob - cur_last_prob);
199           }
200         }
201       }
202     }
203   }
204
205   // switchable_interp_prob
206   for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; ++i) {
207     for (j = 0; j < SWITCHABLE_FILTERS - 1; ++j) {
208       diff += (int)counts->switchable_interp[i][j] *
209               (pre_fc->switchable_interp_prob[i][j] -
210                cur_fc->switchable_interp_prob[i][j]);
211     }
212     pre_last_prob =
213         MAX_PROB - pre_fc->switchable_interp_prob[i][SWITCHABLE_FILTERS - 2];
214     cur_last_prob =
215         MAX_PROB - cur_fc->switchable_interp_prob[i][SWITCHABLE_FILTERS - 2];
216
217     diff += (int)counts->switchable_interp[i][SWITCHABLE_FILTERS - 1] *
218             (pre_last_prob - cur_last_prob);
219   }
220
221   // inter_mode_probs
222   for (i = 0; i < INTER_MODE_CONTEXTS; ++i) {
223     for (j = 0; j < INTER_MODES - 1; ++j) {
224       diff += (int)counts->inter_mode[i][j] *
225               (pre_fc->inter_mode_probs[i][j] - cur_fc->inter_mode_probs[i][j]);
226     }
227     pre_last_prob = MAX_PROB - pre_fc->inter_mode_probs[i][INTER_MODES - 2];
228     cur_last_prob = MAX_PROB - cur_fc->inter_mode_probs[i][INTER_MODES - 2];
229
230     diff += (int)counts->inter_mode[i][INTER_MODES - 1] *
231             (pre_last_prob - cur_last_prob);
232   }
233
234   // intra_inter_prob
235   for (i = 0; i < INTRA_INTER_CONTEXTS; ++i) {
236     diff += (int)counts->intra_inter[i][0] *
237             (pre_fc->intra_inter_prob[i] - cur_fc->intra_inter_prob[i]);
238
239     pre_last_prob = MAX_PROB - pre_fc->intra_inter_prob[i];
240     cur_last_prob = MAX_PROB - cur_fc->intra_inter_prob[i];
241
242     diff += (int)counts->intra_inter[i][1] * (pre_last_prob - cur_last_prob);
243   }
244
245   // comp_inter_prob
246   for (i = 0; i < COMP_INTER_CONTEXTS; ++i) {
247     diff += (int)counts->comp_inter[i][0] *
248             (pre_fc->comp_inter_prob[i] - cur_fc->comp_inter_prob[i]);
249
250     pre_last_prob = MAX_PROB - pre_fc->comp_inter_prob[i];
251     cur_last_prob = MAX_PROB - cur_fc->comp_inter_prob[i];
252
253     diff += (int)counts->comp_inter[i][1] * (pre_last_prob - cur_last_prob);
254   }
255
256   // single_ref_prob
257   for (i = 0; i < REF_CONTEXTS; ++i) {
258     for (j = 0; j < 2; ++j) {
259       diff += (int)counts->single_ref[i][j][0] *
260               (pre_fc->single_ref_prob[i][j] - cur_fc->single_ref_prob[i][j]);
261
262       pre_last_prob = MAX_PROB - pre_fc->single_ref_prob[i][j];
263       cur_last_prob = MAX_PROB - cur_fc->single_ref_prob[i][j];
264
265       diff +=
266           (int)counts->single_ref[i][j][1] * (pre_last_prob - cur_last_prob);
267     }
268   }
269
270   // comp_ref_prob
271   for (i = 0; i < REF_CONTEXTS; ++i) {
272     diff += (int)counts->comp_ref[i][0] *
273             (pre_fc->comp_ref_prob[i] - cur_fc->comp_ref_prob[i]);
274
275     pre_last_prob = MAX_PROB - pre_fc->comp_ref_prob[i];
276     cur_last_prob = MAX_PROB - cur_fc->comp_ref_prob[i];
277
278     diff += (int)counts->comp_ref[i][1] * (pre_last_prob - cur_last_prob);
279   }
280
281   // tx_probs
282   for (i = 0; i < TX_SIZE_CONTEXTS; ++i) {
283     // p32x32
284     for (j = 0; j < TX_SIZES - 1; ++j) {
285       diff += (int)counts->tx.p32x32[i][j] *
286               (pre_fc->tx_probs.p32x32[i][j] - cur_fc->tx_probs.p32x32[i][j]);
287     }
288     pre_last_prob = MAX_PROB - pre_fc->tx_probs.p32x32[i][TX_SIZES - 2];
289     cur_last_prob = MAX_PROB - cur_fc->tx_probs.p32x32[i][TX_SIZES - 2];
290
291     diff += (int)counts->tx.p32x32[i][TX_SIZES - 1] *
292             (pre_last_prob - cur_last_prob);
293
294     // p16x16
295     for (j = 0; j < TX_SIZES - 2; ++j) {
296       diff += (int)counts->tx.p16x16[i][j] *
297               (pre_fc->tx_probs.p16x16[i][j] - cur_fc->tx_probs.p16x16[i][j]);
298     }
299     pre_last_prob = MAX_PROB - pre_fc->tx_probs.p16x16[i][TX_SIZES - 3];
300     cur_last_prob = MAX_PROB - cur_fc->tx_probs.p16x16[i][TX_SIZES - 3];
301
302     diff += (int)counts->tx.p16x16[i][TX_SIZES - 2] *
303             (pre_last_prob - cur_last_prob);
304
305     // p8x8
306     for (j = 0; j < TX_SIZES - 3; ++j) {
307       diff += (int)counts->tx.p8x8[i][j] *
308               (pre_fc->tx_probs.p8x8[i][j] - cur_fc->tx_probs.p8x8[i][j]);
309     }
310     pre_last_prob = MAX_PROB - pre_fc->tx_probs.p8x8[i][TX_SIZES - 4];
311     cur_last_prob = MAX_PROB - cur_fc->tx_probs.p8x8[i][TX_SIZES - 4];
312
313     diff +=
314         (int)counts->tx.p8x8[i][TX_SIZES - 3] * (pre_last_prob - cur_last_prob);
315   }
316
317   // skip_probs
318   for (i = 0; i < SKIP_CONTEXTS; ++i) {
319     diff += (int)counts->skip[i][0] *
320             (pre_fc->skip_probs[i] - cur_fc->skip_probs[i]);
321
322     pre_last_prob = MAX_PROB - pre_fc->skip_probs[i];
323     cur_last_prob = MAX_PROB - cur_fc->skip_probs[i];
324
325     diff += (int)counts->skip[i][1] * (pre_last_prob - cur_last_prob);
326   }
327
328   // mv
329   for (i = 0; i < MV_JOINTS - 1; ++i) {
330     diff += (int)counts->mv.joints[i] *
331             (pre_fc->nmvc.joints[i] - cur_fc->nmvc.joints[i]);
332   }
333   pre_last_prob = MAX_PROB - pre_fc->nmvc.joints[MV_JOINTS - 2];
334   cur_last_prob = MAX_PROB - cur_fc->nmvc.joints[MV_JOINTS - 2];
335
336   diff +=
337       (int)counts->mv.joints[MV_JOINTS - 1] * (pre_last_prob - cur_last_prob);
338
339   for (i = 0; i < 2; ++i) {
340     const nmv_component_counts *nmv_count = &counts->mv.comps[i];
341     const nmv_component *pre_nmv_prob = &pre_fc->nmvc.comps[i];
342     const nmv_component *cur_nmv_prob = &cur_fc->nmvc.comps[i];
343
344     // sign
345     diff += (int)nmv_count->sign[0] * (pre_nmv_prob->sign - cur_nmv_prob->sign);
346
347     pre_last_prob = MAX_PROB - pre_nmv_prob->sign;
348     cur_last_prob = MAX_PROB - cur_nmv_prob->sign;
349
350     diff += (int)nmv_count->sign[1] * (pre_last_prob - cur_last_prob);
351
352     // classes
353     for (j = 0; j < MV_CLASSES - 1; ++j) {
354       diff += (int)nmv_count->classes[j] *
355               (pre_nmv_prob->classes[j] - cur_nmv_prob->classes[j]);
356     }
357     pre_last_prob = MAX_PROB - pre_nmv_prob->classes[MV_CLASSES - 2];
358     cur_last_prob = MAX_PROB - cur_nmv_prob->classes[MV_CLASSES - 2];
359
360     diff += (int)nmv_count->classes[MV_CLASSES - 1] *
361             (pre_last_prob - cur_last_prob);
362
363     // class0
364     for (j = 0; j < CLASS0_SIZE - 1; ++j) {
365       diff += (int)nmv_count->class0[j] *
366               (pre_nmv_prob->class0[j] - cur_nmv_prob->class0[j]);
367     }
368     pre_last_prob = MAX_PROB - pre_nmv_prob->class0[CLASS0_SIZE - 2];
369     cur_last_prob = MAX_PROB - cur_nmv_prob->class0[CLASS0_SIZE - 2];
370
371     diff += (int)nmv_count->class0[CLASS0_SIZE - 1] *
372             (pre_last_prob - cur_last_prob);
373
374     // bits
375     for (j = 0; j < MV_OFFSET_BITS; ++j) {
376       diff += (int)nmv_count->bits[j][0] *
377               (pre_nmv_prob->bits[j] - cur_nmv_prob->bits[j]);
378
379       pre_last_prob = MAX_PROB - pre_nmv_prob->bits[j];
380       cur_last_prob = MAX_PROB - cur_nmv_prob->bits[j];
381
382       diff += (int)nmv_count->bits[j][1] * (pre_last_prob - cur_last_prob);
383     }
384
385     // class0_fp
386     for (j = 0; j < CLASS0_SIZE; ++j) {
387       for (k = 0; k < MV_FP_SIZE - 1; ++k) {
388         diff += (int)nmv_count->class0_fp[j][k] *
389                 (pre_nmv_prob->class0_fp[j][k] - cur_nmv_prob->class0_fp[j][k]);
390       }
391       pre_last_prob = MAX_PROB - pre_nmv_prob->class0_fp[j][MV_FP_SIZE - 2];
392       cur_last_prob = MAX_PROB - cur_nmv_prob->class0_fp[j][MV_FP_SIZE - 2];
393
394       diff += (int)nmv_count->class0_fp[j][MV_FP_SIZE - 1] *
395               (pre_last_prob - cur_last_prob);
396     }
397
398     // fp
399     for (j = 0; j < MV_FP_SIZE - 1; ++j) {
400       diff +=
401           (int)nmv_count->fp[j] * (pre_nmv_prob->fp[j] - cur_nmv_prob->fp[j]);
402     }
403     pre_last_prob = MAX_PROB - pre_nmv_prob->fp[MV_FP_SIZE - 2];
404     cur_last_prob = MAX_PROB - cur_nmv_prob->fp[MV_FP_SIZE - 2];
405
406     diff +=
407         (int)nmv_count->fp[MV_FP_SIZE - 1] * (pre_last_prob - cur_last_prob);
408
409     // class0_hp
410     diff += (int)nmv_count->class0_hp[0] *
411             (pre_nmv_prob->class0_hp - cur_nmv_prob->class0_hp);
412
413     pre_last_prob = MAX_PROB - pre_nmv_prob->class0_hp;
414     cur_last_prob = MAX_PROB - cur_nmv_prob->class0_hp;
415
416     diff += (int)nmv_count->class0_hp[1] * (pre_last_prob - cur_last_prob);
417
418     // hp
419     diff += (int)nmv_count->hp[0] * (pre_nmv_prob->hp - cur_nmv_prob->hp);
420
421     pre_last_prob = MAX_PROB - pre_nmv_prob->hp;
422     cur_last_prob = MAX_PROB - cur_nmv_prob->hp;
423
424     diff += (int)nmv_count->hp[1] * (pre_last_prob - cur_last_prob);
425   }
426
427   return -diff;
428 }
429
430 // Test for whether to calculate metrics for the frame.
431 static int is_psnr_calc_enabled(VP9_COMP *cpi) {
432   VP9_COMMON *const cm = &cpi->common;
433   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
434
435   return cpi->b_calculate_psnr && (oxcf->pass != 1) && cm->show_frame;
436 }
437
438 /* clang-format off */
439 const Vp9LevelSpec vp9_level_defs[VP9_LEVELS] = {
440   { LEVEL_1,   829440,      36864,    200,    400,    2, 1,  4,  8 },
441   { LEVEL_1_1, 2764800,     73728,    800,    1000,   2, 1,  4,  8 },
442   { LEVEL_2,   4608000,     122880,   1800,   1500,   2, 1,  4,  8 },
443   { LEVEL_2_1, 9216000,     245760,   3600,   2800,   2, 2,  4,  8 },
444   { LEVEL_3,   20736000,    552960,   7200,   6000,   2, 4,  4,  8 },
445   { LEVEL_3_1, 36864000,    983040,   12000,  10000,  2, 4,  4,  8 },
446   { LEVEL_4,   83558400,    2228224,  18000,  16000,  4, 4,  4,  8 },
447   { LEVEL_4_1, 160432128,   2228224,  30000,  18000,  4, 4,  5,  6 },
448   { LEVEL_5,   311951360,   8912896,  60000,  36000,  6, 8,  6,  4 },
449   { LEVEL_5_1, 588251136,   8912896,  120000, 46000,  8, 8,  10, 4 },
450   // TODO(huisu): update max_cpb_size for level 5_2 ~ 6_2 when
451   // they are finalized (currently tentative).
452   { LEVEL_5_2, 1176502272,  8912896,  180000, 90000,  8, 8,  10, 4 },
453   { LEVEL_6,   1176502272,  35651584, 180000, 90000,  8, 16, 10, 4 },
454   { LEVEL_6_1, 2353004544u, 35651584, 240000, 180000, 8, 16, 10, 4 },
455   { LEVEL_6_2, 4706009088u, 35651584, 480000, 360000, 8, 16, 10, 4 },
456 };
457 /* clang-format on */
458
459 static const char *level_fail_messages[TARGET_LEVEL_FAIL_IDS] =
460     { "The average bit-rate is too high.",
461       "The picture size is too large.",
462       "The luma sample rate is too large.",
463       "The CPB size is too large.",
464       "The compression ratio is too small",
465       "Too many column tiles are used.",
466       "The alt-ref distance is too small.",
467       "Too many reference buffers are used." };
468
469 static INLINE void Scale2Ratio(VPX_SCALING mode, int *hr, int *hs) {
470   switch (mode) {
471     case NORMAL:
472       *hr = 1;
473       *hs = 1;
474       break;
475     case FOURFIVE:
476       *hr = 4;
477       *hs = 5;
478       break;
479     case THREEFIVE:
480       *hr = 3;
481       *hs = 5;
482       break;
483     case ONETWO:
484       *hr = 1;
485       *hs = 2;
486       break;
487     default:
488       *hr = 1;
489       *hs = 1;
490       assert(0);
491       break;
492   }
493 }
494
495 // Mark all inactive blocks as active. Other segmentation features may be set
496 // so memset cannot be used, instead only inactive blocks should be reset.
497 static void suppress_active_map(VP9_COMP *cpi) {
498   unsigned char *const seg_map = cpi->segmentation_map;
499
500   if (cpi->active_map.enabled || cpi->active_map.update) {
501     const int rows = cpi->common.mi_rows;
502     const int cols = cpi->common.mi_cols;
503     int i;
504
505     for (i = 0; i < rows * cols; ++i)
506       if (seg_map[i] == AM_SEGMENT_ID_INACTIVE)
507         seg_map[i] = AM_SEGMENT_ID_ACTIVE;
508   }
509 }
510
511 static void apply_active_map(VP9_COMP *cpi) {
512   struct segmentation *const seg = &cpi->common.seg;
513   unsigned char *const seg_map = cpi->segmentation_map;
514   const unsigned char *const active_map = cpi->active_map.map;
515   int i;
516
517   assert(AM_SEGMENT_ID_ACTIVE == CR_SEGMENT_ID_BASE);
518
519   if (frame_is_intra_only(&cpi->common)) {
520     cpi->active_map.enabled = 0;
521     cpi->active_map.update = 1;
522   }
523
524   if (cpi->active_map.update) {
525     if (cpi->active_map.enabled) {
526       for (i = 0; i < cpi->common.mi_rows * cpi->common.mi_cols; ++i)
527         if (seg_map[i] == AM_SEGMENT_ID_ACTIVE) seg_map[i] = active_map[i];
528       vp9_enable_segmentation(seg);
529       vp9_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
530       vp9_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF);
531       // Setting the data to -MAX_LOOP_FILTER will result in the computed loop
532       // filter level being zero regardless of the value of seg->abs_delta.
533       vp9_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF,
534                       -MAX_LOOP_FILTER);
535     } else {
536       vp9_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
537       vp9_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF);
538       if (seg->enabled) {
539         seg->update_data = 1;
540         seg->update_map = 1;
541       }
542     }
543     cpi->active_map.update = 0;
544   }
545 }
546
547 static void init_level_info(Vp9LevelInfo *level_info) {
548   Vp9LevelStats *const level_stats = &level_info->level_stats;
549   Vp9LevelSpec *const level_spec = &level_info->level_spec;
550
551   memset(level_stats, 0, sizeof(*level_stats));
552   memset(level_spec, 0, sizeof(*level_spec));
553   level_spec->level = LEVEL_UNKNOWN;
554   level_spec->min_altref_distance = INT_MAX;
555 }
556
557 VP9_LEVEL vp9_get_level(const Vp9LevelSpec *const level_spec) {
558   int i;
559   const Vp9LevelSpec *this_level;
560
561   vpx_clear_system_state();
562
563   for (i = 0; i < VP9_LEVELS; ++i) {
564     this_level = &vp9_level_defs[i];
565     if ((double)level_spec->max_luma_sample_rate >
566             (double)this_level->max_luma_sample_rate *
567                 (1 + SAMPLE_RATE_GRACE_P) ||
568         level_spec->max_luma_picture_size > this_level->max_luma_picture_size ||
569         level_spec->average_bitrate > this_level->average_bitrate ||
570         level_spec->max_cpb_size > this_level->max_cpb_size ||
571         level_spec->compression_ratio < this_level->compression_ratio ||
572         level_spec->max_col_tiles > this_level->max_col_tiles ||
573         level_spec->min_altref_distance < this_level->min_altref_distance ||
574         level_spec->max_ref_frame_buffers > this_level->max_ref_frame_buffers)
575       continue;
576     break;
577   }
578   return (i == VP9_LEVELS) ? LEVEL_UNKNOWN : vp9_level_defs[i].level;
579 }
580
581 int vp9_set_active_map(VP9_COMP *cpi, unsigned char *new_map_16x16, int rows,
582                        int cols) {
583   if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols) {
584     unsigned char *const active_map_8x8 = cpi->active_map.map;
585     const int mi_rows = cpi->common.mi_rows;
586     const int mi_cols = cpi->common.mi_cols;
587     cpi->active_map.update = 1;
588     if (new_map_16x16) {
589       int r, c;
590       for (r = 0; r < mi_rows; ++r) {
591         for (c = 0; c < mi_cols; ++c) {
592           active_map_8x8[r * mi_cols + c] =
593               new_map_16x16[(r >> 1) * cols + (c >> 1)]
594                   ? AM_SEGMENT_ID_ACTIVE
595                   : AM_SEGMENT_ID_INACTIVE;
596         }
597       }
598       cpi->active_map.enabled = 1;
599     } else {
600       cpi->active_map.enabled = 0;
601     }
602     return 0;
603   } else {
604     return -1;
605   }
606 }
607
608 int vp9_get_active_map(VP9_COMP *cpi, unsigned char *new_map_16x16, int rows,
609                        int cols) {
610   if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols &&
611       new_map_16x16) {
612     unsigned char *const seg_map_8x8 = cpi->segmentation_map;
613     const int mi_rows = cpi->common.mi_rows;
614     const int mi_cols = cpi->common.mi_cols;
615     memset(new_map_16x16, !cpi->active_map.enabled, rows * cols);
616     if (cpi->active_map.enabled) {
617       int r, c;
618       for (r = 0; r < mi_rows; ++r) {
619         for (c = 0; c < mi_cols; ++c) {
620           // Cyclic refresh segments are considered active despite not having
621           // AM_SEGMENT_ID_ACTIVE
622           new_map_16x16[(r >> 1) * cols + (c >> 1)] |=
623               seg_map_8x8[r * mi_cols + c] != AM_SEGMENT_ID_INACTIVE;
624         }
625       }
626     }
627     return 0;
628   } else {
629     return -1;
630   }
631 }
632
633 void vp9_set_high_precision_mv(VP9_COMP *cpi, int allow_high_precision_mv) {
634   MACROBLOCK *const mb = &cpi->td.mb;
635   cpi->common.allow_high_precision_mv = allow_high_precision_mv;
636   if (cpi->common.allow_high_precision_mv) {
637     mb->mvcost = mb->nmvcost_hp;
638     mb->mvsadcost = mb->nmvsadcost_hp;
639   } else {
640     mb->mvcost = mb->nmvcost;
641     mb->mvsadcost = mb->nmvsadcost;
642   }
643 }
644
645 static void setup_frame(VP9_COMP *cpi) {
646   VP9_COMMON *const cm = &cpi->common;
647   // Set up entropy context depending on frame type. The decoder mandates
648   // the use of the default context, index 0, for keyframes and inter
649   // frames where the error_resilient_mode or intra_only flag is set. For
650   // other inter-frames the encoder currently uses only two contexts;
651   // context 1 for ALTREF frames and context 0 for the others.
652   if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
653     vp9_setup_past_independence(cm);
654   } else {
655     if (!cpi->use_svc) cm->frame_context_idx = cpi->refresh_alt_ref_frame;
656   }
657
658   if (cm->frame_type == KEY_FRAME) {
659     if (!is_two_pass_svc(cpi)) cpi->refresh_golden_frame = 1;
660     cpi->refresh_alt_ref_frame = 1;
661     vp9_zero(cpi->interp_filter_selected);
662   } else {
663     *cm->fc = cm->frame_contexts[cm->frame_context_idx];
664     vp9_zero(cpi->interp_filter_selected[0]);
665   }
666 }
667
668 static void vp9_enc_setup_mi(VP9_COMMON *cm) {
669   int i;
670   cm->mi = cm->mip + cm->mi_stride + 1;
671   memset(cm->mip, 0, cm->mi_stride * (cm->mi_rows + 1) * sizeof(*cm->mip));
672   cm->prev_mi = cm->prev_mip + cm->mi_stride + 1;
673   // Clear top border row
674   memset(cm->prev_mip, 0, sizeof(*cm->prev_mip) * cm->mi_stride);
675   // Clear left border column
676   for (i = 1; i < cm->mi_rows + 1; ++i)
677     memset(&cm->prev_mip[i * cm->mi_stride], 0, sizeof(*cm->prev_mip));
678
679   cm->mi_grid_visible = cm->mi_grid_base + cm->mi_stride + 1;
680   cm->prev_mi_grid_visible = cm->prev_mi_grid_base + cm->mi_stride + 1;
681
682   memset(cm->mi_grid_base, 0,
683          cm->mi_stride * (cm->mi_rows + 1) * sizeof(*cm->mi_grid_base));
684 }
685
686 static int vp9_enc_alloc_mi(VP9_COMMON *cm, int mi_size) {
687   cm->mip = vpx_calloc(mi_size, sizeof(*cm->mip));
688   if (!cm->mip) return 1;
689   cm->prev_mip = vpx_calloc(mi_size, sizeof(*cm->prev_mip));
690   if (!cm->prev_mip) return 1;
691   cm->mi_alloc_size = mi_size;
692
693   cm->mi_grid_base = (MODE_INFO **)vpx_calloc(mi_size, sizeof(MODE_INFO *));
694   if (!cm->mi_grid_base) return 1;
695   cm->prev_mi_grid_base =
696       (MODE_INFO **)vpx_calloc(mi_size, sizeof(MODE_INFO *));
697   if (!cm->prev_mi_grid_base) return 1;
698
699   return 0;
700 }
701
702 static void vp9_enc_free_mi(VP9_COMMON *cm) {
703   vpx_free(cm->mip);
704   cm->mip = NULL;
705   vpx_free(cm->prev_mip);
706   cm->prev_mip = NULL;
707   vpx_free(cm->mi_grid_base);
708   cm->mi_grid_base = NULL;
709   vpx_free(cm->prev_mi_grid_base);
710   cm->prev_mi_grid_base = NULL;
711 }
712
713 static void vp9_swap_mi_and_prev_mi(VP9_COMMON *cm) {
714   // Current mip will be the prev_mip for the next frame.
715   MODE_INFO **temp_base = cm->prev_mi_grid_base;
716   MODE_INFO *temp = cm->prev_mip;
717   cm->prev_mip = cm->mip;
718   cm->mip = temp;
719
720   // Update the upper left visible macroblock ptrs.
721   cm->mi = cm->mip + cm->mi_stride + 1;
722   cm->prev_mi = cm->prev_mip + cm->mi_stride + 1;
723
724   cm->prev_mi_grid_base = cm->mi_grid_base;
725   cm->mi_grid_base = temp_base;
726   cm->mi_grid_visible = cm->mi_grid_base + cm->mi_stride + 1;
727   cm->prev_mi_grid_visible = cm->prev_mi_grid_base + cm->mi_stride + 1;
728 }
729
730 void vp9_initialize_enc(void) {
731   static volatile int init_done = 0;
732
733   if (!init_done) {
734     vp9_rtcd();
735     vpx_dsp_rtcd();
736     vpx_scale_rtcd();
737     vp9_init_intra_predictors();
738     vp9_init_me_luts();
739     vp9_rc_init_minq_luts();
740     vp9_entropy_mv_init();
741 #if !CONFIG_REALTIME_ONLY
742     vp9_temporal_filter_init();
743 #endif
744     init_done = 1;
745   }
746 }
747
748 static void dealloc_compressor_data(VP9_COMP *cpi) {
749   VP9_COMMON *const cm = &cpi->common;
750   int i;
751
752   vpx_free(cpi->mbmi_ext_base);
753   cpi->mbmi_ext_base = NULL;
754
755   vpx_free(cpi->tile_data);
756   cpi->tile_data = NULL;
757
758   vpx_free(cpi->segmentation_map);
759   cpi->segmentation_map = NULL;
760   vpx_free(cpi->coding_context.last_frame_seg_map_copy);
761   cpi->coding_context.last_frame_seg_map_copy = NULL;
762
763   vpx_free(cpi->nmvcosts[0]);
764   vpx_free(cpi->nmvcosts[1]);
765   cpi->nmvcosts[0] = NULL;
766   cpi->nmvcosts[1] = NULL;
767
768   vpx_free(cpi->nmvcosts_hp[0]);
769   vpx_free(cpi->nmvcosts_hp[1]);
770   cpi->nmvcosts_hp[0] = NULL;
771   cpi->nmvcosts_hp[1] = NULL;
772
773   vpx_free(cpi->nmvsadcosts[0]);
774   vpx_free(cpi->nmvsadcosts[1]);
775   cpi->nmvsadcosts[0] = NULL;
776   cpi->nmvsadcosts[1] = NULL;
777
778   vpx_free(cpi->nmvsadcosts_hp[0]);
779   vpx_free(cpi->nmvsadcosts_hp[1]);
780   cpi->nmvsadcosts_hp[0] = NULL;
781   cpi->nmvsadcosts_hp[1] = NULL;
782
783   vpx_free(cpi->skin_map);
784   cpi->skin_map = NULL;
785
786   vpx_free(cpi->prev_partition);
787   cpi->prev_partition = NULL;
788
789   vpx_free(cpi->prev_segment_id);
790   cpi->prev_segment_id = NULL;
791
792   vpx_free(cpi->prev_variance_low);
793   cpi->prev_variance_low = NULL;
794
795   vpx_free(cpi->copied_frame_cnt);
796   cpi->copied_frame_cnt = NULL;
797
798   vpx_free(cpi->content_state_sb_fd);
799   cpi->content_state_sb_fd = NULL;
800
801   vp9_cyclic_refresh_free(cpi->cyclic_refresh);
802   cpi->cyclic_refresh = NULL;
803
804   vpx_free(cpi->active_map.map);
805   cpi->active_map.map = NULL;
806
807   vpx_free(cpi->consec_zero_mv);
808   cpi->consec_zero_mv = NULL;
809
810   vp9_free_ref_frame_buffers(cm->buffer_pool);
811 #if CONFIG_VP9_POSTPROC
812   vp9_free_postproc_buffers(cm);
813 #endif
814   vp9_free_context_buffers(cm);
815
816   vpx_free_frame_buffer(&cpi->last_frame_uf);
817   vpx_free_frame_buffer(&cpi->scaled_source);
818   vpx_free_frame_buffer(&cpi->scaled_last_source);
819   vpx_free_frame_buffer(&cpi->alt_ref_buffer);
820 #ifdef ENABLE_KF_DENOISE
821   vpx_free_frame_buffer(&cpi->raw_unscaled_source);
822   vpx_free_frame_buffer(&cpi->raw_scaled_source);
823 #endif
824
825   vp9_lookahead_destroy(cpi->lookahead);
826
827   vpx_free(cpi->tile_tok[0][0]);
828   cpi->tile_tok[0][0] = 0;
829
830   vpx_free(cpi->tplist[0][0]);
831   cpi->tplist[0][0] = NULL;
832
833   vp9_free_pc_tree(&cpi->td);
834
835   for (i = 0; i < cpi->svc.number_spatial_layers; ++i) {
836     LAYER_CONTEXT *const lc = &cpi->svc.layer_context[i];
837     vpx_free(lc->rc_twopass_stats_in.buf);
838     lc->rc_twopass_stats_in.buf = NULL;
839     lc->rc_twopass_stats_in.sz = 0;
840   }
841
842   if (cpi->source_diff_var != NULL) {
843     vpx_free(cpi->source_diff_var);
844     cpi->source_diff_var = NULL;
845   }
846
847   for (i = 0; i < MAX_LAG_BUFFERS; ++i) {
848     vpx_free_frame_buffer(&cpi->svc.scaled_frames[i]);
849   }
850   memset(&cpi->svc.scaled_frames[0], 0,
851          MAX_LAG_BUFFERS * sizeof(cpi->svc.scaled_frames[0]));
852
853   vpx_free_frame_buffer(&cpi->svc.scaled_temp);
854   memset(&cpi->svc.scaled_temp, 0, sizeof(cpi->svc.scaled_temp));
855
856   vpx_free_frame_buffer(&cpi->svc.empty_frame.img);
857   memset(&cpi->svc.empty_frame, 0, sizeof(cpi->svc.empty_frame));
858
859   vp9_free_svc_cyclic_refresh(cpi);
860 }
861
862 static void save_coding_context(VP9_COMP *cpi) {
863   CODING_CONTEXT *const cc = &cpi->coding_context;
864   VP9_COMMON *cm = &cpi->common;
865
866   // Stores a snapshot of key state variables which can subsequently be
867   // restored with a call to vp9_restore_coding_context. These functions are
868   // intended for use in a re-code loop in vp9_compress_frame where the
869   // quantizer value is adjusted between loop iterations.
870   vp9_copy(cc->nmvjointcost, cpi->td.mb.nmvjointcost);
871
872   memcpy(cc->nmvcosts[0], cpi->nmvcosts[0],
873          MV_VALS * sizeof(*cpi->nmvcosts[0]));
874   memcpy(cc->nmvcosts[1], cpi->nmvcosts[1],
875          MV_VALS * sizeof(*cpi->nmvcosts[1]));
876   memcpy(cc->nmvcosts_hp[0], cpi->nmvcosts_hp[0],
877          MV_VALS * sizeof(*cpi->nmvcosts_hp[0]));
878   memcpy(cc->nmvcosts_hp[1], cpi->nmvcosts_hp[1],
879          MV_VALS * sizeof(*cpi->nmvcosts_hp[1]));
880
881   vp9_copy(cc->segment_pred_probs, cm->seg.pred_probs);
882
883   memcpy(cpi->coding_context.last_frame_seg_map_copy, cm->last_frame_seg_map,
884          (cm->mi_rows * cm->mi_cols));
885
886   vp9_copy(cc->last_ref_lf_deltas, cm->lf.last_ref_deltas);
887   vp9_copy(cc->last_mode_lf_deltas, cm->lf.last_mode_deltas);
888
889   cc->fc = *cm->fc;
890 }
891
892 static void restore_coding_context(VP9_COMP *cpi) {
893   CODING_CONTEXT *const cc = &cpi->coding_context;
894   VP9_COMMON *cm = &cpi->common;
895
896   // Restore key state variables to the snapshot state stored in the
897   // previous call to vp9_save_coding_context.
898   vp9_copy(cpi->td.mb.nmvjointcost, cc->nmvjointcost);
899
900   memcpy(cpi->nmvcosts[0], cc->nmvcosts[0], MV_VALS * sizeof(*cc->nmvcosts[0]));
901   memcpy(cpi->nmvcosts[1], cc->nmvcosts[1], MV_VALS * sizeof(*cc->nmvcosts[1]));
902   memcpy(cpi->nmvcosts_hp[0], cc->nmvcosts_hp[0],
903          MV_VALS * sizeof(*cc->nmvcosts_hp[0]));
904   memcpy(cpi->nmvcosts_hp[1], cc->nmvcosts_hp[1],
905          MV_VALS * sizeof(*cc->nmvcosts_hp[1]));
906
907   vp9_copy(cm->seg.pred_probs, cc->segment_pred_probs);
908
909   memcpy(cm->last_frame_seg_map, cpi->coding_context.last_frame_seg_map_copy,
910          (cm->mi_rows * cm->mi_cols));
911
912   vp9_copy(cm->lf.last_ref_deltas, cc->last_ref_lf_deltas);
913   vp9_copy(cm->lf.last_mode_deltas, cc->last_mode_lf_deltas);
914
915   *cm->fc = cc->fc;
916 }
917
918 #if !CONFIG_REALTIME_ONLY
919 static void configure_static_seg_features(VP9_COMP *cpi) {
920   VP9_COMMON *const cm = &cpi->common;
921   const RATE_CONTROL *const rc = &cpi->rc;
922   struct segmentation *const seg = &cm->seg;
923
924   int high_q = (int)(rc->avg_q > 48.0);
925   int qi_delta;
926
927   // Disable and clear down for KF
928   if (cm->frame_type == KEY_FRAME) {
929     // Clear down the global segmentation map
930     memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
931     seg->update_map = 0;
932     seg->update_data = 0;
933     cpi->static_mb_pct = 0;
934
935     // Disable segmentation
936     vp9_disable_segmentation(seg);
937
938     // Clear down the segment features.
939     vp9_clearall_segfeatures(seg);
940   } else if (cpi->refresh_alt_ref_frame) {
941     // If this is an alt ref frame
942     // Clear down the global segmentation map
943     memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
944     seg->update_map = 0;
945     seg->update_data = 0;
946     cpi->static_mb_pct = 0;
947
948     // Disable segmentation and individual segment features by default
949     vp9_disable_segmentation(seg);
950     vp9_clearall_segfeatures(seg);
951
952     // Scan frames from current to arf frame.
953     // This function re-enables segmentation if appropriate.
954     vp9_update_mbgraph_stats(cpi);
955
956     // If segmentation was enabled set those features needed for the
957     // arf itself.
958     if (seg->enabled) {
959       seg->update_map = 1;
960       seg->update_data = 1;
961
962       qi_delta =
963           vp9_compute_qdelta(rc, rc->avg_q, rc->avg_q * 0.875, cm->bit_depth);
964       vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta - 2);
965       vp9_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
966
967       vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
968       vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
969
970       // Where relevant assume segment data is delta data
971       seg->abs_delta = SEGMENT_DELTADATA;
972     }
973   } else if (seg->enabled) {
974     // All other frames if segmentation has been enabled
975
976     // First normal frame in a valid gf or alt ref group
977     if (rc->frames_since_golden == 0) {
978       // Set up segment features for normal frames in an arf group
979       if (rc->source_alt_ref_active) {
980         seg->update_map = 0;
981         seg->update_data = 1;
982         seg->abs_delta = SEGMENT_DELTADATA;
983
984         qi_delta =
985             vp9_compute_qdelta(rc, rc->avg_q, rc->avg_q * 1.125, cm->bit_depth);
986         vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta + 2);
987         vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
988
989         vp9_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
990         vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
991
992         // Segment coding disabled for compred testing
993         if (high_q || (cpi->static_mb_pct == 100)) {
994           vp9_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
995           vp9_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
996           vp9_enable_segfeature(seg, 1, SEG_LVL_SKIP);
997         }
998       } else {
999         // Disable segmentation and clear down features if alt ref
1000         // is not active for this group
1001
1002         vp9_disable_segmentation(seg);
1003
1004         memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
1005
1006         seg->update_map = 0;
1007         seg->update_data = 0;
1008
1009         vp9_clearall_segfeatures(seg);
1010       }
1011     } else if (rc->is_src_frame_alt_ref) {
1012       // Special case where we are coding over the top of a previous
1013       // alt ref frame.
1014       // Segment coding disabled for compred testing
1015
1016       // Enable ref frame features for segment 0 as well
1017       vp9_enable_segfeature(seg, 0, SEG_LVL_REF_FRAME);
1018       vp9_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
1019
1020       // All mbs should use ALTREF_FRAME
1021       vp9_clear_segdata(seg, 0, SEG_LVL_REF_FRAME);
1022       vp9_set_segdata(seg, 0, SEG_LVL_REF_FRAME, ALTREF_FRAME);
1023       vp9_clear_segdata(seg, 1, SEG_LVL_REF_FRAME);
1024       vp9_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
1025
1026       // Skip all MBs if high Q (0,0 mv and skip coeffs)
1027       if (high_q) {
1028         vp9_enable_segfeature(seg, 0, SEG_LVL_SKIP);
1029         vp9_enable_segfeature(seg, 1, SEG_LVL_SKIP);
1030       }
1031       // Enable data update
1032       seg->update_data = 1;
1033     } else {
1034       // All other frames.
1035
1036       // No updates.. leave things as they are.
1037       seg->update_map = 0;
1038       seg->update_data = 0;
1039     }
1040   }
1041 }
1042 #endif  // !CONFIG_REALTIME_ONLY
1043
1044 static void update_reference_segmentation_map(VP9_COMP *cpi) {
1045   VP9_COMMON *const cm = &cpi->common;
1046   MODE_INFO **mi_8x8_ptr = cm->mi_grid_visible;
1047   uint8_t *cache_ptr = cm->last_frame_seg_map;
1048   int row, col;
1049
1050   for (row = 0; row < cm->mi_rows; row++) {
1051     MODE_INFO **mi_8x8 = mi_8x8_ptr;
1052     uint8_t *cache = cache_ptr;
1053     for (col = 0; col < cm->mi_cols; col++, mi_8x8++, cache++)
1054       cache[0] = mi_8x8[0]->segment_id;
1055     mi_8x8_ptr += cm->mi_stride;
1056     cache_ptr += cm->mi_cols;
1057   }
1058 }
1059
1060 static void alloc_raw_frame_buffers(VP9_COMP *cpi) {
1061   VP9_COMMON *cm = &cpi->common;
1062   const VP9EncoderConfig *oxcf = &cpi->oxcf;
1063
1064   if (!cpi->lookahead)
1065     cpi->lookahead = vp9_lookahead_init(oxcf->width, oxcf->height,
1066                                         cm->subsampling_x, cm->subsampling_y,
1067 #if CONFIG_VP9_HIGHBITDEPTH
1068                                         cm->use_highbitdepth,
1069 #endif
1070                                         oxcf->lag_in_frames);
1071   if (!cpi->lookahead)
1072     vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
1073                        "Failed to allocate lag buffers");
1074
1075   // TODO(agrange) Check if ARF is enabled and skip allocation if not.
1076   if (vpx_realloc_frame_buffer(&cpi->alt_ref_buffer, oxcf->width, oxcf->height,
1077                                cm->subsampling_x, cm->subsampling_y,
1078 #if CONFIG_VP9_HIGHBITDEPTH
1079                                cm->use_highbitdepth,
1080 #endif
1081                                VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment,
1082                                NULL, NULL, NULL))
1083     vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
1084                        "Failed to allocate altref buffer");
1085 }
1086
1087 static void alloc_util_frame_buffers(VP9_COMP *cpi) {
1088   VP9_COMMON *const cm = &cpi->common;
1089   if (vpx_realloc_frame_buffer(&cpi->last_frame_uf, cm->width, cm->height,
1090                                cm->subsampling_x, cm->subsampling_y,
1091 #if CONFIG_VP9_HIGHBITDEPTH
1092                                cm->use_highbitdepth,
1093 #endif
1094                                VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment,
1095                                NULL, NULL, NULL))
1096     vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
1097                        "Failed to allocate last frame buffer");
1098
1099   if (vpx_realloc_frame_buffer(&cpi->scaled_source, cm->width, cm->height,
1100                                cm->subsampling_x, cm->subsampling_y,
1101 #if CONFIG_VP9_HIGHBITDEPTH
1102                                cm->use_highbitdepth,
1103 #endif
1104                                VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment,
1105                                NULL, NULL, NULL))
1106     vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
1107                        "Failed to allocate scaled source buffer");
1108
1109   // For 1 pass cbr: allocate scaled_frame that may be used as an intermediate
1110   // buffer for a 2 stage down-sampling: two stages of 1:2 down-sampling for a
1111   // target of 1/4x1/4.
1112   if (is_one_pass_cbr_svc(cpi) && !cpi->svc.scaled_temp_is_alloc) {
1113     cpi->svc.scaled_temp_is_alloc = 1;
1114     if (vpx_realloc_frame_buffer(
1115             &cpi->svc.scaled_temp, cm->width >> 1, cm->height >> 1,
1116             cm->subsampling_x, cm->subsampling_y,
1117 #if CONFIG_VP9_HIGHBITDEPTH
1118             cm->use_highbitdepth,
1119 #endif
1120             VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment, NULL, NULL, NULL))
1121       vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1122                          "Failed to allocate scaled_frame for svc ");
1123   }
1124
1125   if (vpx_realloc_frame_buffer(&cpi->scaled_last_source, cm->width, cm->height,
1126                                cm->subsampling_x, cm->subsampling_y,
1127 #if CONFIG_VP9_HIGHBITDEPTH
1128                                cm->use_highbitdepth,
1129 #endif
1130                                VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment,
1131                                NULL, NULL, NULL))
1132     vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
1133                        "Failed to allocate scaled last source buffer");
1134 #ifdef ENABLE_KF_DENOISE
1135   if (vpx_realloc_frame_buffer(&cpi->raw_unscaled_source, cm->width, cm->height,
1136                                cm->subsampling_x, cm->subsampling_y,
1137 #if CONFIG_VP9_HIGHBITDEPTH
1138                                cm->use_highbitdepth,
1139 #endif
1140                                VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment,
1141                                NULL, NULL, NULL))
1142     vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
1143                        "Failed to allocate unscaled raw source frame buffer");
1144
1145   if (vpx_realloc_frame_buffer(&cpi->raw_scaled_source, cm->width, cm->height,
1146                                cm->subsampling_x, cm->subsampling_y,
1147 #if CONFIG_VP9_HIGHBITDEPTH
1148                                cm->use_highbitdepth,
1149 #endif
1150                                VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment,
1151                                NULL, NULL, NULL))
1152     vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
1153                        "Failed to allocate scaled raw source frame buffer");
1154 #endif
1155 }
1156
1157 static int alloc_context_buffers_ext(VP9_COMP *cpi) {
1158   VP9_COMMON *cm = &cpi->common;
1159   int mi_size = cm->mi_cols * cm->mi_rows;
1160
1161   cpi->mbmi_ext_base = vpx_calloc(mi_size, sizeof(*cpi->mbmi_ext_base));
1162   if (!cpi->mbmi_ext_base) return 1;
1163
1164   return 0;
1165 }
1166
1167 static void alloc_compressor_data(VP9_COMP *cpi) {
1168   VP9_COMMON *cm = &cpi->common;
1169   int sb_rows;
1170
1171   vp9_alloc_context_buffers(cm, cm->width, cm->height);
1172
1173   alloc_context_buffers_ext(cpi);
1174
1175   vpx_free(cpi->tile_tok[0][0]);
1176
1177   {
1178     unsigned int tokens = get_token_alloc(cm->mb_rows, cm->mb_cols);
1179     CHECK_MEM_ERROR(cm, cpi->tile_tok[0][0],
1180                     vpx_calloc(tokens, sizeof(*cpi->tile_tok[0][0])));
1181   }
1182
1183   sb_rows = mi_cols_aligned_to_sb(cm->mi_rows) >> MI_BLOCK_SIZE_LOG2;
1184   vpx_free(cpi->tplist[0][0]);
1185   CHECK_MEM_ERROR(
1186       cm, cpi->tplist[0][0],
1187       vpx_calloc(sb_rows * 4 * (1 << 6), sizeof(*cpi->tplist[0][0])));
1188
1189   vp9_setup_pc_tree(&cpi->common, &cpi->td);
1190 }
1191
1192 void vp9_new_framerate(VP9_COMP *cpi, double framerate) {
1193   cpi->framerate = framerate < 0.1 ? 30 : framerate;
1194   vp9_rc_update_framerate(cpi);
1195 }
1196
1197 static void set_tile_limits(VP9_COMP *cpi) {
1198   VP9_COMMON *const cm = &cpi->common;
1199
1200   int min_log2_tile_cols, max_log2_tile_cols;
1201   vp9_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
1202
1203   if (is_two_pass_svc(cpi) && (cpi->svc.encode_empty_frame_state == ENCODING ||
1204                                cpi->svc.number_spatial_layers > 1)) {
1205     cm->log2_tile_cols = 0;
1206     cm->log2_tile_rows = 0;
1207   } else {
1208     cm->log2_tile_cols =
1209         clamp(cpi->oxcf.tile_columns, min_log2_tile_cols, max_log2_tile_cols);
1210     cm->log2_tile_rows = cpi->oxcf.tile_rows;
1211   }
1212
1213   if (cpi->oxcf.target_level == LEVEL_AUTO) {
1214     const uint32_t pic_size = cpi->common.width * cpi->common.height;
1215     const int level_tile_cols = log_tile_cols_from_picsize_level(pic_size);
1216     if (cm->log2_tile_cols > level_tile_cols) {
1217       cm->log2_tile_cols = VPXMAX(level_tile_cols, min_log2_tile_cols);
1218     }
1219   }
1220 }
1221
1222 static void update_frame_size(VP9_COMP *cpi) {
1223   VP9_COMMON *const cm = &cpi->common;
1224   MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
1225
1226   vp9_set_mb_mi(cm, cm->width, cm->height);
1227   vp9_init_context_buffers(cm);
1228   vp9_init_macroblockd(cm, xd, NULL);
1229   cpi->td.mb.mbmi_ext_base = cpi->mbmi_ext_base;
1230   memset(cpi->mbmi_ext_base, 0,
1231          cm->mi_rows * cm->mi_cols * sizeof(*cpi->mbmi_ext_base));
1232
1233   set_tile_limits(cpi);
1234
1235   if (is_two_pass_svc(cpi)) {
1236     if (vpx_realloc_frame_buffer(&cpi->alt_ref_buffer, cm->width, cm->height,
1237                                  cm->subsampling_x, cm->subsampling_y,
1238 #if CONFIG_VP9_HIGHBITDEPTH
1239                                  cm->use_highbitdepth,
1240 #endif
1241                                  VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment,
1242                                  NULL, NULL, NULL))
1243       vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
1244                          "Failed to reallocate alt_ref_buffer");
1245   }
1246 }
1247
1248 static void init_buffer_indices(VP9_COMP *cpi) {
1249   cpi->lst_fb_idx = 0;
1250   cpi->gld_fb_idx = 1;
1251   cpi->alt_fb_idx = 2;
1252 }
1253
1254 static void init_level_constraint(LevelConstraint *lc) {
1255   lc->level_index = -1;
1256   lc->max_cpb_size = INT_MAX;
1257   lc->max_frame_size = INT_MAX;
1258   lc->rc_config_updated = 0;
1259   lc->fail_flag = 0;
1260 }
1261
1262 static void set_level_constraint(LevelConstraint *ls, int8_t level_index) {
1263   vpx_clear_system_state();
1264   ls->level_index = level_index;
1265   if (level_index >= 0) {
1266     ls->max_cpb_size = vp9_level_defs[level_index].max_cpb_size * (double)1000;
1267   }
1268 }
1269
1270 static void init_config(struct VP9_COMP *cpi, VP9EncoderConfig *oxcf) {
1271   VP9_COMMON *const cm = &cpi->common;
1272
1273   cpi->oxcf = *oxcf;
1274   cpi->framerate = oxcf->init_framerate;
1275   cm->profile = oxcf->profile;
1276   cm->bit_depth = oxcf->bit_depth;
1277 #if CONFIG_VP9_HIGHBITDEPTH
1278   cm->use_highbitdepth = oxcf->use_highbitdepth;
1279 #endif
1280   cm->color_space = oxcf->color_space;
1281   cm->color_range = oxcf->color_range;
1282
1283   cpi->target_level = oxcf->target_level;
1284   cpi->keep_level_stats = oxcf->target_level != LEVEL_MAX;
1285   set_level_constraint(&cpi->level_constraint,
1286                        get_level_index(cpi->target_level));
1287
1288   cm->width = oxcf->width;
1289   cm->height = oxcf->height;
1290   alloc_compressor_data(cpi);
1291
1292   cpi->svc.temporal_layering_mode = oxcf->temporal_layering_mode;
1293
1294   // Single thread case: use counts in common.
1295   cpi->td.counts = &cm->counts;
1296
1297   // Spatial scalability.
1298   cpi->svc.number_spatial_layers = oxcf->ss_number_layers;
1299   // Temporal scalability.
1300   cpi->svc.number_temporal_layers = oxcf->ts_number_layers;
1301
1302   if ((cpi->svc.number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) ||
1303       ((cpi->svc.number_temporal_layers > 1 ||
1304         cpi->svc.number_spatial_layers > 1) &&
1305        cpi->oxcf.pass != 1)) {
1306     vp9_init_layer_context(cpi);
1307   }
1308
1309   // change includes all joint functionality
1310   vp9_change_config(cpi, oxcf);
1311
1312   cpi->static_mb_pct = 0;
1313   cpi->ref_frame_flags = 0;
1314
1315   init_buffer_indices(cpi);
1316
1317   vp9_noise_estimate_init(&cpi->noise_estimate, cm->width, cm->height);
1318 }
1319
1320 static void set_rc_buffer_sizes(RATE_CONTROL *rc,
1321                                 const VP9EncoderConfig *oxcf) {
1322   const int64_t bandwidth = oxcf->target_bandwidth;
1323   const int64_t starting = oxcf->starting_buffer_level_ms;
1324   const int64_t optimal = oxcf->optimal_buffer_level_ms;
1325   const int64_t maximum = oxcf->maximum_buffer_size_ms;
1326
1327   rc->starting_buffer_level = starting * bandwidth / 1000;
1328   rc->optimal_buffer_level =
1329       (optimal == 0) ? bandwidth / 8 : optimal * bandwidth / 1000;
1330   rc->maximum_buffer_size =
1331       (maximum == 0) ? bandwidth / 8 : maximum * bandwidth / 1000;
1332 }
1333
1334 #if CONFIG_VP9_HIGHBITDEPTH
1335 #define HIGHBD_BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX4DF) \
1336   cpi->fn_ptr[BT].sdf = SDF;                             \
1337   cpi->fn_ptr[BT].sdaf = SDAF;                           \
1338   cpi->fn_ptr[BT].vf = VF;                               \
1339   cpi->fn_ptr[BT].svf = SVF;                             \
1340   cpi->fn_ptr[BT].svaf = SVAF;                           \
1341   cpi->fn_ptr[BT].sdx4df = SDX4DF;
1342
1343 #define MAKE_BFP_SAD_WRAPPER(fnname)                                           \
1344   static unsigned int fnname##_bits8(const uint8_t *src_ptr,                   \
1345                                      int source_stride,                        \
1346                                      const uint8_t *ref_ptr, int ref_stride) { \
1347     return fnname(src_ptr, source_stride, ref_ptr, ref_stride);                \
1348   }                                                                            \
1349   static unsigned int fnname##_bits10(                                         \
1350       const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr,       \
1351       int ref_stride) {                                                        \
1352     return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 2;           \
1353   }                                                                            \
1354   static unsigned int fnname##_bits12(                                         \
1355       const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr,       \
1356       int ref_stride) {                                                        \
1357     return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 4;           \
1358   }
1359
1360 #define MAKE_BFP_SADAVG_WRAPPER(fnname)                                        \
1361   static unsigned int fnname##_bits8(                                          \
1362       const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr,       \
1363       int ref_stride, const uint8_t *second_pred) {                            \
1364     return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred);   \
1365   }                                                                            \
1366   static unsigned int fnname##_bits10(                                         \
1367       const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr,       \
1368       int ref_stride, const uint8_t *second_pred) {                            \
1369     return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
1370            2;                                                                  \
1371   }                                                                            \
1372   static unsigned int fnname##_bits12(                                         \
1373       const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr,       \
1374       int ref_stride, const uint8_t *second_pred) {                            \
1375     return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
1376            4;                                                                  \
1377   }
1378
1379 #define MAKE_BFP_SAD4D_WRAPPER(fnname)                                        \
1380   static void fnname##_bits8(const uint8_t *src_ptr, int source_stride,       \
1381                              const uint8_t *const ref_ptr[], int ref_stride,  \
1382                              unsigned int *sad_array) {                       \
1383     fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array);           \
1384   }                                                                           \
1385   static void fnname##_bits10(const uint8_t *src_ptr, int source_stride,      \
1386                               const uint8_t *const ref_ptr[], int ref_stride, \
1387                               unsigned int *sad_array) {                      \
1388     int i;                                                                    \
1389     fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array);           \
1390     for (i = 0; i < 4; i++) sad_array[i] >>= 2;                               \
1391   }                                                                           \
1392   static void fnname##_bits12(const uint8_t *src_ptr, int source_stride,      \
1393                               const uint8_t *const ref_ptr[], int ref_stride, \
1394                               unsigned int *sad_array) {                      \
1395     int i;                                                                    \
1396     fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array);           \
1397     for (i = 0; i < 4; i++) sad_array[i] >>= 4;                               \
1398   }
1399
1400 MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad32x16)
1401 MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad32x16_avg)
1402 MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad32x16x4d)
1403 MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad16x32)
1404 MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad16x32_avg)
1405 MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad16x32x4d)
1406 MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad64x32)
1407 MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad64x32_avg)
1408 MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad64x32x4d)
1409 MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad32x64)
1410 MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad32x64_avg)
1411 MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad32x64x4d)
1412 MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad32x32)
1413 MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad32x32_avg)
1414 MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad32x32x4d)
1415 MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad64x64)
1416 MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad64x64_avg)
1417 MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad64x64x4d)
1418 MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad16x16)
1419 MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad16x16_avg)
1420 MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad16x16x4d)
1421 MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad16x8)
1422 MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad16x8_avg)
1423 MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad16x8x4d)
1424 MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad8x16)
1425 MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad8x16_avg)
1426 MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad8x16x4d)
1427 MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad8x8)
1428 MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad8x8_avg)
1429 MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad8x8x4d)
1430 MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad8x4)
1431 MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad8x4_avg)
1432 MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad8x4x4d)
1433 MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad4x8)
1434 MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad4x8_avg)
1435 MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad4x8x4d)
1436 MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad4x4)
1437 MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad4x4_avg)
1438 MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad4x4x4d)
1439
1440 static void highbd_set_var_fns(VP9_COMP *const cpi) {
1441   VP9_COMMON *const cm = &cpi->common;
1442   if (cm->use_highbitdepth) {
1443     switch (cm->bit_depth) {
1444       case VPX_BITS_8:
1445         HIGHBD_BFP(BLOCK_32X16, vpx_highbd_sad32x16_bits8,
1446                    vpx_highbd_sad32x16_avg_bits8, vpx_highbd_8_variance32x16,
1447                    vpx_highbd_8_sub_pixel_variance32x16,
1448                    vpx_highbd_8_sub_pixel_avg_variance32x16,
1449                    vpx_highbd_sad32x16x4d_bits8)
1450
1451         HIGHBD_BFP(BLOCK_16X32, vpx_highbd_sad16x32_bits8,
1452                    vpx_highbd_sad16x32_avg_bits8, vpx_highbd_8_variance16x32,
1453                    vpx_highbd_8_sub_pixel_variance16x32,
1454                    vpx_highbd_8_sub_pixel_avg_variance16x32,
1455                    vpx_highbd_sad16x32x4d_bits8)
1456
1457         HIGHBD_BFP(BLOCK_64X32, vpx_highbd_sad64x32_bits8,
1458                    vpx_highbd_sad64x32_avg_bits8, vpx_highbd_8_variance64x32,
1459                    vpx_highbd_8_sub_pixel_variance64x32,
1460                    vpx_highbd_8_sub_pixel_avg_variance64x32,
1461                    vpx_highbd_sad64x32x4d_bits8)
1462
1463         HIGHBD_BFP(BLOCK_32X64, vpx_highbd_sad32x64_bits8,
1464                    vpx_highbd_sad32x64_avg_bits8, vpx_highbd_8_variance32x64,
1465                    vpx_highbd_8_sub_pixel_variance32x64,
1466                    vpx_highbd_8_sub_pixel_avg_variance32x64,
1467                    vpx_highbd_sad32x64x4d_bits8)
1468
1469         HIGHBD_BFP(BLOCK_32X32, vpx_highbd_sad32x32_bits8,
1470                    vpx_highbd_sad32x32_avg_bits8, vpx_highbd_8_variance32x32,
1471                    vpx_highbd_8_sub_pixel_variance32x32,
1472                    vpx_highbd_8_sub_pixel_avg_variance32x32,
1473                    vpx_highbd_sad32x32x4d_bits8)
1474
1475         HIGHBD_BFP(BLOCK_64X64, vpx_highbd_sad64x64_bits8,
1476                    vpx_highbd_sad64x64_avg_bits8, vpx_highbd_8_variance64x64,
1477                    vpx_highbd_8_sub_pixel_variance64x64,
1478                    vpx_highbd_8_sub_pixel_avg_variance64x64,
1479                    vpx_highbd_sad64x64x4d_bits8)
1480
1481         HIGHBD_BFP(BLOCK_16X16, vpx_highbd_sad16x16_bits8,
1482                    vpx_highbd_sad16x16_avg_bits8, vpx_highbd_8_variance16x16,
1483                    vpx_highbd_8_sub_pixel_variance16x16,
1484                    vpx_highbd_8_sub_pixel_avg_variance16x16,
1485                    vpx_highbd_sad16x16x4d_bits8)
1486
1487         HIGHBD_BFP(BLOCK_16X8, vpx_highbd_sad16x8_bits8,
1488                    vpx_highbd_sad16x8_avg_bits8, vpx_highbd_8_variance16x8,
1489                    vpx_highbd_8_sub_pixel_variance16x8,
1490                    vpx_highbd_8_sub_pixel_avg_variance16x8,
1491                    vpx_highbd_sad16x8x4d_bits8)
1492
1493         HIGHBD_BFP(BLOCK_8X16, vpx_highbd_sad8x16_bits8,
1494                    vpx_highbd_sad8x16_avg_bits8, vpx_highbd_8_variance8x16,
1495                    vpx_highbd_8_sub_pixel_variance8x16,
1496                    vpx_highbd_8_sub_pixel_avg_variance8x16,
1497                    vpx_highbd_sad8x16x4d_bits8)
1498
1499         HIGHBD_BFP(
1500             BLOCK_8X8, vpx_highbd_sad8x8_bits8, vpx_highbd_sad8x8_avg_bits8,
1501             vpx_highbd_8_variance8x8, vpx_highbd_8_sub_pixel_variance8x8,
1502             vpx_highbd_8_sub_pixel_avg_variance8x8, vpx_highbd_sad8x8x4d_bits8)
1503
1504         HIGHBD_BFP(
1505             BLOCK_8X4, vpx_highbd_sad8x4_bits8, vpx_highbd_sad8x4_avg_bits8,
1506             vpx_highbd_8_variance8x4, vpx_highbd_8_sub_pixel_variance8x4,
1507             vpx_highbd_8_sub_pixel_avg_variance8x4, vpx_highbd_sad8x4x4d_bits8)
1508
1509         HIGHBD_BFP(
1510             BLOCK_4X8, vpx_highbd_sad4x8_bits8, vpx_highbd_sad4x8_avg_bits8,
1511             vpx_highbd_8_variance4x8, vpx_highbd_8_sub_pixel_variance4x8,
1512             vpx_highbd_8_sub_pixel_avg_variance4x8, vpx_highbd_sad4x8x4d_bits8)
1513
1514         HIGHBD_BFP(
1515             BLOCK_4X4, vpx_highbd_sad4x4_bits8, vpx_highbd_sad4x4_avg_bits8,
1516             vpx_highbd_8_variance4x4, vpx_highbd_8_sub_pixel_variance4x4,
1517             vpx_highbd_8_sub_pixel_avg_variance4x4, vpx_highbd_sad4x4x4d_bits8)
1518         break;
1519
1520       case VPX_BITS_10:
1521         HIGHBD_BFP(BLOCK_32X16, vpx_highbd_sad32x16_bits10,
1522                    vpx_highbd_sad32x16_avg_bits10, vpx_highbd_10_variance32x16,
1523                    vpx_highbd_10_sub_pixel_variance32x16,
1524                    vpx_highbd_10_sub_pixel_avg_variance32x16,
1525                    vpx_highbd_sad32x16x4d_bits10)
1526
1527         HIGHBD_BFP(BLOCK_16X32, vpx_highbd_sad16x32_bits10,
1528                    vpx_highbd_sad16x32_avg_bits10, vpx_highbd_10_variance16x32,
1529                    vpx_highbd_10_sub_pixel_variance16x32,
1530                    vpx_highbd_10_sub_pixel_avg_variance16x32,
1531                    vpx_highbd_sad16x32x4d_bits10)
1532
1533         HIGHBD_BFP(BLOCK_64X32, vpx_highbd_sad64x32_bits10,
1534                    vpx_highbd_sad64x32_avg_bits10, vpx_highbd_10_variance64x32,
1535                    vpx_highbd_10_sub_pixel_variance64x32,
1536                    vpx_highbd_10_sub_pixel_avg_variance64x32,
1537                    vpx_highbd_sad64x32x4d_bits10)
1538
1539         HIGHBD_BFP(BLOCK_32X64, vpx_highbd_sad32x64_bits10,
1540                    vpx_highbd_sad32x64_avg_bits10, vpx_highbd_10_variance32x64,
1541                    vpx_highbd_10_sub_pixel_variance32x64,
1542                    vpx_highbd_10_sub_pixel_avg_variance32x64,
1543                    vpx_highbd_sad32x64x4d_bits10)
1544
1545         HIGHBD_BFP(BLOCK_32X32, vpx_highbd_sad32x32_bits10,
1546                    vpx_highbd_sad32x32_avg_bits10, vpx_highbd_10_variance32x32,
1547                    vpx_highbd_10_sub_pixel_variance32x32,
1548                    vpx_highbd_10_sub_pixel_avg_variance32x32,
1549                    vpx_highbd_sad32x32x4d_bits10)
1550
1551         HIGHBD_BFP(BLOCK_64X64, vpx_highbd_sad64x64_bits10,
1552                    vpx_highbd_sad64x64_avg_bits10, vpx_highbd_10_variance64x64,
1553                    vpx_highbd_10_sub_pixel_variance64x64,
1554                    vpx_highbd_10_sub_pixel_avg_variance64x64,
1555                    vpx_highbd_sad64x64x4d_bits10)
1556
1557         HIGHBD_BFP(BLOCK_16X16, vpx_highbd_sad16x16_bits10,
1558                    vpx_highbd_sad16x16_avg_bits10, vpx_highbd_10_variance16x16,
1559                    vpx_highbd_10_sub_pixel_variance16x16,
1560                    vpx_highbd_10_sub_pixel_avg_variance16x16,
1561                    vpx_highbd_sad16x16x4d_bits10)
1562
1563         HIGHBD_BFP(BLOCK_16X8, vpx_highbd_sad16x8_bits10,
1564                    vpx_highbd_sad16x8_avg_bits10, vpx_highbd_10_variance16x8,
1565                    vpx_highbd_10_sub_pixel_variance16x8,
1566                    vpx_highbd_10_sub_pixel_avg_variance16x8,
1567                    vpx_highbd_sad16x8x4d_bits10)
1568
1569         HIGHBD_BFP(BLOCK_8X16, vpx_highbd_sad8x16_bits10,
1570                    vpx_highbd_sad8x16_avg_bits10, vpx_highbd_10_variance8x16,
1571                    vpx_highbd_10_sub_pixel_variance8x16,
1572                    vpx_highbd_10_sub_pixel_avg_variance8x16,
1573                    vpx_highbd_sad8x16x4d_bits10)
1574
1575         HIGHBD_BFP(BLOCK_8X8, vpx_highbd_sad8x8_bits10,
1576                    vpx_highbd_sad8x8_avg_bits10, vpx_highbd_10_variance8x8,
1577                    vpx_highbd_10_sub_pixel_variance8x8,
1578                    vpx_highbd_10_sub_pixel_avg_variance8x8,
1579                    vpx_highbd_sad8x8x4d_bits10)
1580
1581         HIGHBD_BFP(BLOCK_8X4, vpx_highbd_sad8x4_bits10,
1582                    vpx_highbd_sad8x4_avg_bits10, vpx_highbd_10_variance8x4,
1583                    vpx_highbd_10_sub_pixel_variance8x4,
1584                    vpx_highbd_10_sub_pixel_avg_variance8x4,
1585                    vpx_highbd_sad8x4x4d_bits10)
1586
1587         HIGHBD_BFP(BLOCK_4X8, vpx_highbd_sad4x8_bits10,
1588                    vpx_highbd_sad4x8_avg_bits10, vpx_highbd_10_variance4x8,
1589                    vpx_highbd_10_sub_pixel_variance4x8,
1590                    vpx_highbd_10_sub_pixel_avg_variance4x8,
1591                    vpx_highbd_sad4x8x4d_bits10)
1592
1593         HIGHBD_BFP(BLOCK_4X4, vpx_highbd_sad4x4_bits10,
1594                    vpx_highbd_sad4x4_avg_bits10, vpx_highbd_10_variance4x4,
1595                    vpx_highbd_10_sub_pixel_variance4x4,
1596                    vpx_highbd_10_sub_pixel_avg_variance4x4,
1597                    vpx_highbd_sad4x4x4d_bits10)
1598         break;
1599
1600       case VPX_BITS_12:
1601         HIGHBD_BFP(BLOCK_32X16, vpx_highbd_sad32x16_bits12,
1602                    vpx_highbd_sad32x16_avg_bits12, vpx_highbd_12_variance32x16,
1603                    vpx_highbd_12_sub_pixel_variance32x16,
1604                    vpx_highbd_12_sub_pixel_avg_variance32x16,
1605                    vpx_highbd_sad32x16x4d_bits12)
1606
1607         HIGHBD_BFP(BLOCK_16X32, vpx_highbd_sad16x32_bits12,
1608                    vpx_highbd_sad16x32_avg_bits12, vpx_highbd_12_variance16x32,
1609                    vpx_highbd_12_sub_pixel_variance16x32,
1610                    vpx_highbd_12_sub_pixel_avg_variance16x32,
1611                    vpx_highbd_sad16x32x4d_bits12)
1612
1613         HIGHBD_BFP(BLOCK_64X32, vpx_highbd_sad64x32_bits12,
1614                    vpx_highbd_sad64x32_avg_bits12, vpx_highbd_12_variance64x32,
1615                    vpx_highbd_12_sub_pixel_variance64x32,
1616                    vpx_highbd_12_sub_pixel_avg_variance64x32,
1617                    vpx_highbd_sad64x32x4d_bits12)
1618
1619         HIGHBD_BFP(BLOCK_32X64, vpx_highbd_sad32x64_bits12,
1620                    vpx_highbd_sad32x64_avg_bits12, vpx_highbd_12_variance32x64,
1621                    vpx_highbd_12_sub_pixel_variance32x64,
1622                    vpx_highbd_12_sub_pixel_avg_variance32x64,
1623                    vpx_highbd_sad32x64x4d_bits12)
1624
1625         HIGHBD_BFP(BLOCK_32X32, vpx_highbd_sad32x32_bits12,
1626                    vpx_highbd_sad32x32_avg_bits12, vpx_highbd_12_variance32x32,
1627                    vpx_highbd_12_sub_pixel_variance32x32,
1628                    vpx_highbd_12_sub_pixel_avg_variance32x32,
1629                    vpx_highbd_sad32x32x4d_bits12)
1630
1631         HIGHBD_BFP(BLOCK_64X64, vpx_highbd_sad64x64_bits12,
1632                    vpx_highbd_sad64x64_avg_bits12, vpx_highbd_12_variance64x64,
1633                    vpx_highbd_12_sub_pixel_variance64x64,
1634                    vpx_highbd_12_sub_pixel_avg_variance64x64,
1635                    vpx_highbd_sad64x64x4d_bits12)
1636
1637         HIGHBD_BFP(BLOCK_16X16, vpx_highbd_sad16x16_bits12,
1638                    vpx_highbd_sad16x16_avg_bits12, vpx_highbd_12_variance16x16,
1639                    vpx_highbd_12_sub_pixel_variance16x16,
1640                    vpx_highbd_12_sub_pixel_avg_variance16x16,
1641                    vpx_highbd_sad16x16x4d_bits12)
1642
1643         HIGHBD_BFP(BLOCK_16X8, vpx_highbd_sad16x8_bits12,
1644                    vpx_highbd_sad16x8_avg_bits12, vpx_highbd_12_variance16x8,
1645                    vpx_highbd_12_sub_pixel_variance16x8,
1646                    vpx_highbd_12_sub_pixel_avg_variance16x8,
1647                    vpx_highbd_sad16x8x4d_bits12)
1648
1649         HIGHBD_BFP(BLOCK_8X16, vpx_highbd_sad8x16_bits12,
1650                    vpx_highbd_sad8x16_avg_bits12, vpx_highbd_12_variance8x16,
1651                    vpx_highbd_12_sub_pixel_variance8x16,
1652                    vpx_highbd_12_sub_pixel_avg_variance8x16,
1653                    vpx_highbd_sad8x16x4d_bits12)
1654
1655         HIGHBD_BFP(BLOCK_8X8, vpx_highbd_sad8x8_bits12,
1656                    vpx_highbd_sad8x8_avg_bits12, vpx_highbd_12_variance8x8,
1657                    vpx_highbd_12_sub_pixel_variance8x8,
1658                    vpx_highbd_12_sub_pixel_avg_variance8x8,
1659                    vpx_highbd_sad8x8x4d_bits12)
1660
1661         HIGHBD_BFP(BLOCK_8X4, vpx_highbd_sad8x4_bits12,
1662                    vpx_highbd_sad8x4_avg_bits12, vpx_highbd_12_variance8x4,
1663                    vpx_highbd_12_sub_pixel_variance8x4,
1664                    vpx_highbd_12_sub_pixel_avg_variance8x4,
1665                    vpx_highbd_sad8x4x4d_bits12)
1666
1667         HIGHBD_BFP(BLOCK_4X8, vpx_highbd_sad4x8_bits12,
1668                    vpx_highbd_sad4x8_avg_bits12, vpx_highbd_12_variance4x8,
1669                    vpx_highbd_12_sub_pixel_variance4x8,
1670                    vpx_highbd_12_sub_pixel_avg_variance4x8,
1671                    vpx_highbd_sad4x8x4d_bits12)
1672
1673         HIGHBD_BFP(BLOCK_4X4, vpx_highbd_sad4x4_bits12,
1674                    vpx_highbd_sad4x4_avg_bits12, vpx_highbd_12_variance4x4,
1675                    vpx_highbd_12_sub_pixel_variance4x4,
1676                    vpx_highbd_12_sub_pixel_avg_variance4x4,
1677                    vpx_highbd_sad4x4x4d_bits12)
1678         break;
1679
1680       default:
1681         assert(0 &&
1682                "cm->bit_depth should be VPX_BITS_8, "
1683                "VPX_BITS_10 or VPX_BITS_12");
1684     }
1685   }
1686 }
1687 #endif  // CONFIG_VP9_HIGHBITDEPTH
1688
1689 static void realloc_segmentation_maps(VP9_COMP *cpi) {
1690   VP9_COMMON *const cm = &cpi->common;
1691
1692   // Create the encoder segmentation map and set all entries to 0
1693   vpx_free(cpi->segmentation_map);
1694   CHECK_MEM_ERROR(cm, cpi->segmentation_map,
1695                   vpx_calloc(cm->mi_rows * cm->mi_cols, 1));
1696
1697   // Create a map used for cyclic background refresh.
1698   if (cpi->cyclic_refresh) vp9_cyclic_refresh_free(cpi->cyclic_refresh);
1699   CHECK_MEM_ERROR(cm, cpi->cyclic_refresh,
1700                   vp9_cyclic_refresh_alloc(cm->mi_rows, cm->mi_cols));
1701
1702   // Create a map used to mark inactive areas.
1703   vpx_free(cpi->active_map.map);
1704   CHECK_MEM_ERROR(cm, cpi->active_map.map,
1705                   vpx_calloc(cm->mi_rows * cm->mi_cols, 1));
1706
1707   // And a place holder structure is the coding context
1708   // for use if we want to save and restore it
1709   vpx_free(cpi->coding_context.last_frame_seg_map_copy);
1710   CHECK_MEM_ERROR(cm, cpi->coding_context.last_frame_seg_map_copy,
1711                   vpx_calloc(cm->mi_rows * cm->mi_cols, 1));
1712 }
1713
1714 static void alloc_copy_partition_data(VP9_COMP *cpi) {
1715   VP9_COMMON *const cm = &cpi->common;
1716   if (cpi->prev_partition == NULL) {
1717     CHECK_MEM_ERROR(cm, cpi->prev_partition,
1718                     (BLOCK_SIZE *)vpx_calloc(cm->mi_stride * cm->mi_rows,
1719                                              sizeof(*cpi->prev_partition)));
1720   }
1721   if (cpi->prev_segment_id == NULL) {
1722     CHECK_MEM_ERROR(
1723         cm, cpi->prev_segment_id,
1724         (int8_t *)vpx_calloc((cm->mi_stride >> 3) * ((cm->mi_rows >> 3) + 1),
1725                              sizeof(*cpi->prev_segment_id)));
1726   }
1727   if (cpi->prev_variance_low == NULL) {
1728     CHECK_MEM_ERROR(cm, cpi->prev_variance_low,
1729                     (uint8_t *)vpx_calloc(
1730                         (cm->mi_stride >> 3) * ((cm->mi_rows >> 3) + 1) * 25,
1731                         sizeof(*cpi->prev_variance_low)));
1732   }
1733   if (cpi->copied_frame_cnt == NULL) {
1734     CHECK_MEM_ERROR(
1735         cm, cpi->copied_frame_cnt,
1736         (uint8_t *)vpx_calloc((cm->mi_stride >> 3) * ((cm->mi_rows >> 3) + 1),
1737                               sizeof(*cpi->copied_frame_cnt)));
1738   }
1739 }
1740
1741 void vp9_change_config(struct VP9_COMP *cpi, const VP9EncoderConfig *oxcf) {
1742   VP9_COMMON *const cm = &cpi->common;
1743   RATE_CONTROL *const rc = &cpi->rc;
1744   int last_w = cpi->oxcf.width;
1745   int last_h = cpi->oxcf.height;
1746
1747   if (cm->profile != oxcf->profile) cm->profile = oxcf->profile;
1748   cm->bit_depth = oxcf->bit_depth;
1749   cm->color_space = oxcf->color_space;
1750   cm->color_range = oxcf->color_range;
1751
1752   cpi->target_level = oxcf->target_level;
1753   cpi->keep_level_stats = oxcf->target_level != LEVEL_MAX;
1754   set_level_constraint(&cpi->level_constraint,
1755                        get_level_index(cpi->target_level));
1756
1757   if (cm->profile <= PROFILE_1)
1758     assert(cm->bit_depth == VPX_BITS_8);
1759   else
1760     assert(cm->bit_depth > VPX_BITS_8);
1761
1762   cpi->oxcf = *oxcf;
1763 #if CONFIG_VP9_HIGHBITDEPTH
1764   cpi->td.mb.e_mbd.bd = (int)cm->bit_depth;
1765 #endif  // CONFIG_VP9_HIGHBITDEPTH
1766
1767   if ((oxcf->pass == 0) && (oxcf->rc_mode == VPX_Q)) {
1768     rc->baseline_gf_interval = FIXED_GF_INTERVAL;
1769   } else {
1770     rc->baseline_gf_interval = (MIN_GF_INTERVAL + MAX_GF_INTERVAL) / 2;
1771   }
1772
1773   cpi->refresh_golden_frame = 0;
1774   cpi->refresh_last_frame = 1;
1775   cm->refresh_frame_context = 1;
1776   cm->reset_frame_context = 0;
1777
1778   vp9_reset_segment_features(&cm->seg);
1779   vp9_set_high_precision_mv(cpi, 0);
1780
1781   {
1782     int i;
1783
1784     for (i = 0; i < MAX_SEGMENTS; i++)
1785       cpi->segment_encode_breakout[i] = cpi->oxcf.encode_breakout;
1786   }
1787   cpi->encode_breakout = cpi->oxcf.encode_breakout;
1788
1789   set_rc_buffer_sizes(rc, &cpi->oxcf);
1790
1791   // Under a configuration change, where maximum_buffer_size may change,
1792   // keep buffer level clipped to the maximum allowed buffer size.
1793   rc->bits_off_target = VPXMIN(rc->bits_off_target, rc->maximum_buffer_size);
1794   rc->buffer_level = VPXMIN(rc->buffer_level, rc->maximum_buffer_size);
1795
1796   // Set up frame rate and related parameters rate control values.
1797   vp9_new_framerate(cpi, cpi->framerate);
1798
1799   // Set absolute upper and lower quality limits
1800   rc->worst_quality = cpi->oxcf.worst_allowed_q;
1801   rc->best_quality = cpi->oxcf.best_allowed_q;
1802
1803   cm->interp_filter = cpi->sf.default_interp_filter;
1804
1805   if (cpi->oxcf.render_width > 0 && cpi->oxcf.render_height > 0) {
1806     cm->render_width = cpi->oxcf.render_width;
1807     cm->render_height = cpi->oxcf.render_height;
1808   } else {
1809     cm->render_width = cpi->oxcf.width;
1810     cm->render_height = cpi->oxcf.height;
1811   }
1812   if (last_w != cpi->oxcf.width || last_h != cpi->oxcf.height) {
1813     cm->width = cpi->oxcf.width;
1814     cm->height = cpi->oxcf.height;
1815     cpi->external_resize = 1;
1816   }
1817
1818   if (cpi->initial_width) {
1819     int new_mi_size = 0;
1820     vp9_set_mb_mi(cm, cm->width, cm->height);
1821     new_mi_size = cm->mi_stride * calc_mi_size(cm->mi_rows);
1822     if (cm->mi_alloc_size < new_mi_size) {
1823       vp9_free_context_buffers(cm);
1824       alloc_compressor_data(cpi);
1825       realloc_segmentation_maps(cpi);
1826       cpi->initial_width = cpi->initial_height = 0;
1827       cpi->external_resize = 0;
1828     } else if (cm->mi_alloc_size == new_mi_size &&
1829                (cpi->oxcf.width > last_w || cpi->oxcf.height > last_h)) {
1830       vp9_alloc_loop_filter(cm);
1831     }
1832   }
1833
1834   if (cm->current_video_frame == 0 || last_w != cpi->oxcf.width ||
1835       last_h != cpi->oxcf.height)
1836     update_frame_size(cpi);
1837
1838   if (last_w != cpi->oxcf.width || last_h != cpi->oxcf.height) {
1839     memset(cpi->consec_zero_mv, 0,
1840            cm->mi_rows * cm->mi_cols * sizeof(*cpi->consec_zero_mv));
1841     if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ)
1842       vp9_cyclic_refresh_reset_resize(cpi);
1843   }
1844
1845   if ((cpi->svc.number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) ||
1846       ((cpi->svc.number_temporal_layers > 1 ||
1847         cpi->svc.number_spatial_layers > 1) &&
1848        cpi->oxcf.pass != 1)) {
1849     vp9_update_layer_context_change_config(cpi,
1850                                            (int)cpi->oxcf.target_bandwidth);
1851   }
1852
1853   cpi->alt_ref_source = NULL;
1854   rc->is_src_frame_alt_ref = 0;
1855
1856 #if 0
1857   // Experimental RD Code
1858   cpi->frame_distortion = 0;
1859   cpi->last_frame_distortion = 0;
1860 #endif
1861
1862   set_tile_limits(cpi);
1863
1864   cpi->ext_refresh_frame_flags_pending = 0;
1865   cpi->ext_refresh_frame_context_pending = 0;
1866
1867 #if CONFIG_VP9_HIGHBITDEPTH
1868   highbd_set_var_fns(cpi);
1869 #endif
1870
1871   vp9_set_row_mt(cpi);
1872 }
1873
1874 #ifndef M_LOG2_E
1875 #define M_LOG2_E 0.693147180559945309417
1876 #endif
1877 #define log2f(x) (log(x) / (float)M_LOG2_E)
1878
1879 /***********************************************************************
1880  * Read before modifying 'cal_nmvjointsadcost' or 'cal_nmvsadcosts'    *
1881  ***********************************************************************
1882  * The following 2 functions ('cal_nmvjointsadcost' and                *
1883  * 'cal_nmvsadcosts') are used to calculate cost lookup tables         *
1884  * used by 'vp9_diamond_search_sad'. The C implementation of the       *
1885  * function is generic, but the AVX intrinsics optimised version       *
1886  * relies on the following properties of the computed tables:          *
1887  * For cal_nmvjointsadcost:                                            *
1888  *   - mvjointsadcost[1] == mvjointsadcost[2] == mvjointsadcost[3]     *
1889  * For cal_nmvsadcosts:                                                *
1890  *   - For all i: mvsadcost[0][i] == mvsadcost[1][i]                   *
1891  *         (Equal costs for both components)                           *
1892  *   - For all i: mvsadcost[0][i] == mvsadcost[0][-i]                  *
1893  *         (Cost function is even)                                     *
1894  * If these do not hold, then the AVX optimised version of the         *
1895  * 'vp9_diamond_search_sad' function cannot be used as it is, in which *
1896  * case you can revert to using the C function instead.                *
1897  ***********************************************************************/
1898
1899 static void cal_nmvjointsadcost(int *mvjointsadcost) {
1900   /*********************************************************************
1901    * Warning: Read the comments above before modifying this function   *
1902    *********************************************************************/
1903   mvjointsadcost[0] = 600;
1904   mvjointsadcost[1] = 300;
1905   mvjointsadcost[2] = 300;
1906   mvjointsadcost[3] = 300;
1907 }
1908
1909 static void cal_nmvsadcosts(int *mvsadcost[2]) {
1910   /*********************************************************************
1911    * Warning: Read the comments above before modifying this function   *
1912    *********************************************************************/
1913   int i = 1;
1914
1915   mvsadcost[0][0] = 0;
1916   mvsadcost[1][0] = 0;
1917
1918   do {
1919     double z = 256 * (2 * (log2f(8 * i) + .6));
1920     mvsadcost[0][i] = (int)z;
1921     mvsadcost[1][i] = (int)z;
1922     mvsadcost[0][-i] = (int)z;
1923     mvsadcost[1][-i] = (int)z;
1924   } while (++i <= MV_MAX);
1925 }
1926
1927 static void cal_nmvsadcosts_hp(int *mvsadcost[2]) {
1928   int i = 1;
1929
1930   mvsadcost[0][0] = 0;
1931   mvsadcost[1][0] = 0;
1932
1933   do {
1934     double z = 256 * (2 * (log2f(8 * i) + .6));
1935     mvsadcost[0][i] = (int)z;
1936     mvsadcost[1][i] = (int)z;
1937     mvsadcost[0][-i] = (int)z;
1938     mvsadcost[1][-i] = (int)z;
1939   } while (++i <= MV_MAX);
1940 }
1941
1942 VP9_COMP *vp9_create_compressor(VP9EncoderConfig *oxcf,
1943                                 BufferPool *const pool) {
1944   unsigned int i;
1945   VP9_COMP *volatile const cpi = vpx_memalign(32, sizeof(VP9_COMP));
1946   VP9_COMMON *volatile const cm = cpi != NULL ? &cpi->common : NULL;
1947
1948   if (!cm) return NULL;
1949
1950   vp9_zero(*cpi);
1951
1952   if (setjmp(cm->error.jmp)) {
1953     cm->error.setjmp = 0;
1954     vp9_remove_compressor(cpi);
1955     return 0;
1956   }
1957
1958   cm->error.setjmp = 1;
1959   cm->alloc_mi = vp9_enc_alloc_mi;
1960   cm->free_mi = vp9_enc_free_mi;
1961   cm->setup_mi = vp9_enc_setup_mi;
1962
1963   CHECK_MEM_ERROR(cm, cm->fc, (FRAME_CONTEXT *)vpx_calloc(1, sizeof(*cm->fc)));
1964   CHECK_MEM_ERROR(
1965       cm, cm->frame_contexts,
1966       (FRAME_CONTEXT *)vpx_calloc(FRAME_CONTEXTS, sizeof(*cm->frame_contexts)));
1967
1968   cpi->use_svc = 0;
1969   cpi->resize_state = ORIG;
1970   cpi->external_resize = 0;
1971   cpi->resize_avg_qp = 0;
1972   cpi->resize_buffer_underflow = 0;
1973   cpi->use_skin_detection = 0;
1974   cpi->common.buffer_pool = pool;
1975
1976   cpi->force_update_segmentation = 0;
1977
1978   init_config(cpi, oxcf);
1979   vp9_rc_init(&cpi->oxcf, oxcf->pass, &cpi->rc);
1980
1981   cm->current_video_frame = 0;
1982   cpi->partition_search_skippable_frame = 0;
1983   cpi->tile_data = NULL;
1984
1985   realloc_segmentation_maps(cpi);
1986
1987   CHECK_MEM_ERROR(cm, cpi->skin_map, vpx_calloc(cm->mi_rows * cm->mi_cols,
1988                                                 sizeof(cpi->skin_map[0])));
1989
1990   CHECK_MEM_ERROR(cm, cpi->alt_ref_aq, vp9_alt_ref_aq_create());
1991
1992   CHECK_MEM_ERROR(
1993       cm, cpi->consec_zero_mv,
1994       vpx_calloc(cm->mi_rows * cm->mi_cols, sizeof(*cpi->consec_zero_mv)));
1995
1996   CHECK_MEM_ERROR(cm, cpi->nmvcosts[0],
1997                   vpx_calloc(MV_VALS, sizeof(*cpi->nmvcosts[0])));
1998   CHECK_MEM_ERROR(cm, cpi->nmvcosts[1],
1999                   vpx_calloc(MV_VALS, sizeof(*cpi->nmvcosts[1])));
2000   CHECK_MEM_ERROR(cm, cpi->nmvcosts_hp[0],
2001                   vpx_calloc(MV_VALS, sizeof(*cpi->nmvcosts_hp[0])));
2002   CHECK_MEM_ERROR(cm, cpi->nmvcosts_hp[1],
2003                   vpx_calloc(MV_VALS, sizeof(*cpi->nmvcosts_hp[1])));
2004   CHECK_MEM_ERROR(cm, cpi->nmvsadcosts[0],
2005                   vpx_calloc(MV_VALS, sizeof(*cpi->nmvsadcosts[0])));
2006   CHECK_MEM_ERROR(cm, cpi->nmvsadcosts[1],
2007                   vpx_calloc(MV_VALS, sizeof(*cpi->nmvsadcosts[1])));
2008   CHECK_MEM_ERROR(cm, cpi->nmvsadcosts_hp[0],
2009                   vpx_calloc(MV_VALS, sizeof(*cpi->nmvsadcosts_hp[0])));
2010   CHECK_MEM_ERROR(cm, cpi->nmvsadcosts_hp[1],
2011                   vpx_calloc(MV_VALS, sizeof(*cpi->nmvsadcosts_hp[1])));
2012
2013   for (i = 0; i < (sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]));
2014        i++) {
2015     CHECK_MEM_ERROR(
2016         cm, cpi->mbgraph_stats[i].mb_stats,
2017         vpx_calloc(cm->MBs * sizeof(*cpi->mbgraph_stats[i].mb_stats), 1));
2018   }
2019
2020 #if CONFIG_FP_MB_STATS
2021   cpi->use_fp_mb_stats = 0;
2022   if (cpi->use_fp_mb_stats) {
2023     // a place holder used to store the first pass mb stats in the first pass
2024     CHECK_MEM_ERROR(cm, cpi->twopass.frame_mb_stats_buf,
2025                     vpx_calloc(cm->MBs * sizeof(uint8_t), 1));
2026   } else {
2027     cpi->twopass.frame_mb_stats_buf = NULL;
2028   }
2029 #endif
2030
2031   cpi->refresh_alt_ref_frame = 0;
2032   cpi->multi_arf_last_grp_enabled = 0;
2033
2034   cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
2035
2036   init_level_info(&cpi->level_info);
2037   init_level_constraint(&cpi->level_constraint);
2038
2039 #if CONFIG_INTERNAL_STATS
2040   cpi->b_calculate_blockiness = 1;
2041   cpi->b_calculate_consistency = 1;
2042   cpi->total_inconsistency = 0;
2043   cpi->psnr.worst = 100.0;
2044   cpi->worst_ssim = 100.0;
2045
2046   cpi->count = 0;
2047   cpi->bytes = 0;
2048
2049   if (cpi->b_calculate_psnr) {
2050     cpi->total_sq_error = 0;
2051     cpi->total_samples = 0;
2052
2053     cpi->totalp_sq_error = 0;
2054     cpi->totalp_samples = 0;
2055
2056     cpi->tot_recode_hits = 0;
2057     cpi->summed_quality = 0;
2058     cpi->summed_weights = 0;
2059     cpi->summedp_quality = 0;
2060     cpi->summedp_weights = 0;
2061   }
2062
2063   cpi->fastssim.worst = 100.0;
2064
2065   cpi->psnrhvs.worst = 100.0;
2066
2067   if (cpi->b_calculate_blockiness) {
2068     cpi->total_blockiness = 0;
2069     cpi->worst_blockiness = 0.0;
2070   }
2071
2072   if (cpi->b_calculate_consistency) {
2073     CHECK_MEM_ERROR(cm, cpi->ssim_vars,
2074                     vpx_malloc(sizeof(*cpi->ssim_vars) * 4 *
2075                                cpi->common.mi_rows * cpi->common.mi_cols));
2076     cpi->worst_consistency = 100.0;
2077   }
2078
2079 #endif
2080
2081   cpi->first_time_stamp_ever = INT64_MAX;
2082
2083   /*********************************************************************
2084    * Warning: Read the comments around 'cal_nmvjointsadcost' and       *
2085    * 'cal_nmvsadcosts' before modifying how these tables are computed. *
2086    *********************************************************************/
2087   cal_nmvjointsadcost(cpi->td.mb.nmvjointsadcost);
2088   cpi->td.mb.nmvcost[0] = &cpi->nmvcosts[0][MV_MAX];
2089   cpi->td.mb.nmvcost[1] = &cpi->nmvcosts[1][MV_MAX];
2090   cpi->td.mb.nmvsadcost[0] = &cpi->nmvsadcosts[0][MV_MAX];
2091   cpi->td.mb.nmvsadcost[1] = &cpi->nmvsadcosts[1][MV_MAX];
2092   cal_nmvsadcosts(cpi->td.mb.nmvsadcost);
2093
2094   cpi->td.mb.nmvcost_hp[0] = &cpi->nmvcosts_hp[0][MV_MAX];
2095   cpi->td.mb.nmvcost_hp[1] = &cpi->nmvcosts_hp[1][MV_MAX];
2096   cpi->td.mb.nmvsadcost_hp[0] = &cpi->nmvsadcosts_hp[0][MV_MAX];
2097   cpi->td.mb.nmvsadcost_hp[1] = &cpi->nmvsadcosts_hp[1][MV_MAX];
2098   cal_nmvsadcosts_hp(cpi->td.mb.nmvsadcost_hp);
2099
2100 #if CONFIG_VP9_TEMPORAL_DENOISING
2101 #ifdef OUTPUT_YUV_DENOISED
2102   yuv_denoised_file = fopen("denoised.yuv", "ab");
2103 #endif
2104 #endif
2105 #ifdef OUTPUT_YUV_SKINMAP
2106   yuv_skinmap_file = fopen("skinmap.yuv", "wb");
2107 #endif
2108 #ifdef OUTPUT_YUV_REC
2109   yuv_rec_file = fopen("rec.yuv", "wb");
2110 #endif
2111
2112 #if 0
2113   framepsnr = fopen("framepsnr.stt", "a");
2114   kf_list = fopen("kf_list.stt", "w");
2115 #endif
2116
2117   cpi->allow_encode_breakout = ENCODE_BREAKOUT_ENABLED;
2118
2119 #if !CONFIG_REALTIME_ONLY
2120   if (oxcf->pass == 1) {
2121     vp9_init_first_pass(cpi);
2122   } else if (oxcf->pass == 2) {
2123     const size_t packet_sz = sizeof(FIRSTPASS_STATS);
2124     const int packets = (int)(oxcf->two_pass_stats_in.sz / packet_sz);
2125
2126     if (cpi->svc.number_spatial_layers > 1 ||
2127         cpi->svc.number_temporal_layers > 1) {
2128       FIRSTPASS_STATS *const stats = oxcf->two_pass_stats_in.buf;
2129       FIRSTPASS_STATS *stats_copy[VPX_SS_MAX_LAYERS] = { 0 };
2130       int i;
2131
2132       for (i = 0; i < oxcf->ss_number_layers; ++i) {
2133         FIRSTPASS_STATS *const last_packet_for_layer =
2134             &stats[packets - oxcf->ss_number_layers + i];
2135         const int layer_id = (int)last_packet_for_layer->spatial_layer_id;
2136         const int packets_in_layer = (int)last_packet_for_layer->count + 1;
2137         if (layer_id >= 0 && layer_id < oxcf->ss_number_layers) {
2138           LAYER_CONTEXT *const lc = &cpi->svc.layer_context[layer_id];
2139
2140           vpx_free(lc->rc_twopass_stats_in.buf);
2141
2142           lc->rc_twopass_stats_in.sz = packets_in_layer * packet_sz;
2143           CHECK_MEM_ERROR(cm, lc->rc_twopass_stats_in.buf,
2144                           vpx_malloc(lc->rc_twopass_stats_in.sz));
2145           lc->twopass.stats_in_start = lc->rc_twopass_stats_in.buf;
2146           lc->twopass.stats_in = lc->twopass.stats_in_start;
2147           lc->twopass.stats_in_end =
2148               lc->twopass.stats_in_start + packets_in_layer - 1;
2149           stats_copy[layer_id] = lc->rc_twopass_stats_in.buf;
2150         }
2151       }
2152
2153       for (i = 0; i < packets; ++i) {
2154         const int layer_id = (int)stats[i].spatial_layer_id;
2155         if (layer_id >= 0 && layer_id < oxcf->ss_number_layers &&
2156             stats_copy[layer_id] != NULL) {
2157           *stats_copy[layer_id] = stats[i];
2158           ++stats_copy[layer_id];
2159         }
2160       }
2161
2162       vp9_init_second_pass_spatial_svc(cpi);
2163     } else {
2164 #if CONFIG_FP_MB_STATS
2165       if (cpi->use_fp_mb_stats) {
2166         const size_t psz = cpi->common.MBs * sizeof(uint8_t);
2167         const int ps = (int)(oxcf->firstpass_mb_stats_in.sz / psz);
2168
2169         cpi->twopass.firstpass_mb_stats.mb_stats_start =
2170             oxcf->firstpass_mb_stats_in.buf;
2171         cpi->twopass.firstpass_mb_stats.mb_stats_end =
2172             cpi->twopass.firstpass_mb_stats.mb_stats_start +
2173             (ps - 1) * cpi->common.MBs * sizeof(uint8_t);
2174       }
2175 #endif
2176
2177       cpi->twopass.stats_in_start = oxcf->two_pass_stats_in.buf;
2178       cpi->twopass.stats_in = cpi->twopass.stats_in_start;
2179       cpi->twopass.stats_in_end = &cpi->twopass.stats_in[packets - 1];
2180
2181       vp9_init_second_pass(cpi);
2182     }
2183   }
2184 #endif  // !CONFIG_REALTIME_ONLY
2185
2186   vp9_set_speed_features_framesize_independent(cpi);
2187   vp9_set_speed_features_framesize_dependent(cpi);
2188
2189   // Allocate memory to store variances for a frame.
2190   CHECK_MEM_ERROR(cm, cpi->source_diff_var, vpx_calloc(cm->MBs, sizeof(diff)));
2191   cpi->source_var_thresh = 0;
2192   cpi->frames_till_next_var_check = 0;
2193
2194 #define BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX4DF) \
2195   cpi->fn_ptr[BT].sdf = SDF;                      \
2196   cpi->fn_ptr[BT].sdaf = SDAF;                    \
2197   cpi->fn_ptr[BT].vf = VF;                        \
2198   cpi->fn_ptr[BT].svf = SVF;                      \
2199   cpi->fn_ptr[BT].svaf = SVAF;                    \
2200   cpi->fn_ptr[BT].sdx4df = SDX4DF;
2201
2202   BFP(BLOCK_32X16, vpx_sad32x16, vpx_sad32x16_avg, vpx_variance32x16,
2203       vpx_sub_pixel_variance32x16, vpx_sub_pixel_avg_variance32x16,
2204       vpx_sad32x16x4d)
2205
2206   BFP(BLOCK_16X32, vpx_sad16x32, vpx_sad16x32_avg, vpx_variance16x32,
2207       vpx_sub_pixel_variance16x32, vpx_sub_pixel_avg_variance16x32,
2208       vpx_sad16x32x4d)
2209
2210   BFP(BLOCK_64X32, vpx_sad64x32, vpx_sad64x32_avg, vpx_variance64x32,
2211       vpx_sub_pixel_variance64x32, vpx_sub_pixel_avg_variance64x32,
2212       vpx_sad64x32x4d)
2213
2214   BFP(BLOCK_32X64, vpx_sad32x64, vpx_sad32x64_avg, vpx_variance32x64,
2215       vpx_sub_pixel_variance32x64, vpx_sub_pixel_avg_variance32x64,
2216       vpx_sad32x64x4d)
2217
2218   BFP(BLOCK_32X32, vpx_sad32x32, vpx_sad32x32_avg, vpx_variance32x32,
2219       vpx_sub_pixel_variance32x32, vpx_sub_pixel_avg_variance32x32,
2220       vpx_sad32x32x4d)
2221
2222   BFP(BLOCK_64X64, vpx_sad64x64, vpx_sad64x64_avg, vpx_variance64x64,
2223       vpx_sub_pixel_variance64x64, vpx_sub_pixel_avg_variance64x64,
2224       vpx_sad64x64x4d)
2225
2226   BFP(BLOCK_16X16, vpx_sad16x16, vpx_sad16x16_avg, vpx_variance16x16,
2227       vpx_sub_pixel_variance16x16, vpx_sub_pixel_avg_variance16x16,
2228       vpx_sad16x16x4d)
2229
2230   BFP(BLOCK_16X8, vpx_sad16x8, vpx_sad16x8_avg, vpx_variance16x8,
2231       vpx_sub_pixel_variance16x8, vpx_sub_pixel_avg_variance16x8,
2232       vpx_sad16x8x4d)
2233
2234   BFP(BLOCK_8X16, vpx_sad8x16, vpx_sad8x16_avg, vpx_variance8x16,
2235       vpx_sub_pixel_variance8x16, vpx_sub_pixel_avg_variance8x16,
2236       vpx_sad8x16x4d)
2237
2238   BFP(BLOCK_8X8, vpx_sad8x8, vpx_sad8x8_avg, vpx_variance8x8,
2239       vpx_sub_pixel_variance8x8, vpx_sub_pixel_avg_variance8x8, vpx_sad8x8x4d)
2240
2241   BFP(BLOCK_8X4, vpx_sad8x4, vpx_sad8x4_avg, vpx_variance8x4,
2242       vpx_sub_pixel_variance8x4, vpx_sub_pixel_avg_variance8x4, vpx_sad8x4x4d)
2243
2244   BFP(BLOCK_4X8, vpx_sad4x8, vpx_sad4x8_avg, vpx_variance4x8,
2245       vpx_sub_pixel_variance4x8, vpx_sub_pixel_avg_variance4x8, vpx_sad4x8x4d)
2246
2247   BFP(BLOCK_4X4, vpx_sad4x4, vpx_sad4x4_avg, vpx_variance4x4,
2248       vpx_sub_pixel_variance4x4, vpx_sub_pixel_avg_variance4x4, vpx_sad4x4x4d)
2249
2250 #if CONFIG_VP9_HIGHBITDEPTH
2251   highbd_set_var_fns(cpi);
2252 #endif
2253
2254   /* vp9_init_quantizer() is first called here. Add check in
2255    * vp9_frame_init_quantizer() so that vp9_init_quantizer is only
2256    * called later when needed. This will avoid unnecessary calls of
2257    * vp9_init_quantizer() for every frame.
2258    */
2259   vp9_init_quantizer(cpi);
2260
2261   vp9_loop_filter_init(cm);
2262
2263   cm->error.setjmp = 0;
2264
2265   return cpi;
2266 }
2267
2268 #if CONFIG_INTERNAL_STATS
2269 #define SNPRINT(H, T) snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T))
2270
2271 #define SNPRINT2(H, T, V) \
2272   snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T), (V))
2273 #endif  // CONFIG_INTERNAL_STATS
2274
2275 void vp9_remove_compressor(VP9_COMP *cpi) {
2276   VP9_COMMON *cm;
2277   unsigned int i;
2278   int t;
2279
2280   if (!cpi) return;
2281
2282   cm = &cpi->common;
2283   if (cm->current_video_frame > 0) {
2284 #if CONFIG_INTERNAL_STATS
2285     vpx_clear_system_state();
2286
2287     if (cpi->oxcf.pass != 1) {
2288       char headings[512] = { 0 };
2289       char results[512] = { 0 };
2290       FILE *f = fopen("opsnr.stt", "a");
2291       double time_encoded =
2292           (cpi->last_end_time_stamp_seen - cpi->first_time_stamp_ever) /
2293           10000000.000;
2294       double total_encode_time =
2295           (cpi->time_receive_data + cpi->time_compress_data) / 1000.000;
2296       const double dr =
2297           (double)cpi->bytes * (double)8 / (double)1000 / time_encoded;
2298       const double peak = (double)((1 << cpi->oxcf.input_bit_depth) - 1);
2299       const double target_rate = (double)cpi->oxcf.target_bandwidth / 1000;
2300       const double rate_err = ((100.0 * (dr - target_rate)) / target_rate);
2301
2302       if (cpi->b_calculate_psnr) {
2303         const double total_psnr = vpx_sse_to_psnr(
2304             (double)cpi->total_samples, peak, (double)cpi->total_sq_error);
2305         const double totalp_psnr = vpx_sse_to_psnr(
2306             (double)cpi->totalp_samples, peak, (double)cpi->totalp_sq_error);
2307         const double total_ssim =
2308             100 * pow(cpi->summed_quality / cpi->summed_weights, 8.0);
2309         const double totalp_ssim =
2310             100 * pow(cpi->summedp_quality / cpi->summedp_weights, 8.0);
2311
2312         snprintf(headings, sizeof(headings),
2313                  "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\t"
2314                  "VPXSSIM\tVPSSIMP\tFASTSIM\tPSNRHVS\t"
2315                  "WstPsnr\tWstSsim\tWstFast\tWstHVS");
2316         snprintf(results, sizeof(results),
2317                  "%7.2f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
2318                  "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
2319                  "%7.3f\t%7.3f\t%7.3f\t%7.3f",
2320                  dr, cpi->psnr.stat[ALL] / cpi->count, total_psnr,
2321                  cpi->psnrp.stat[ALL] / cpi->count, totalp_psnr, total_ssim,
2322                  totalp_ssim, cpi->fastssim.stat[ALL] / cpi->count,
2323                  cpi->psnrhvs.stat[ALL] / cpi->count, cpi->psnr.worst,
2324                  cpi->worst_ssim, cpi->fastssim.worst, cpi->psnrhvs.worst);
2325
2326         if (cpi->b_calculate_blockiness) {
2327           SNPRINT(headings, "\t  Block\tWstBlck");
2328           SNPRINT2(results, "\t%7.3f", cpi->total_blockiness / cpi->count);
2329           SNPRINT2(results, "\t%7.3f", cpi->worst_blockiness);
2330         }
2331
2332         if (cpi->b_calculate_consistency) {
2333           double consistency =
2334               vpx_sse_to_psnr((double)cpi->totalp_samples, peak,
2335                               (double)cpi->total_inconsistency);
2336
2337           SNPRINT(headings, "\tConsist\tWstCons");
2338           SNPRINT2(results, "\t%7.3f", consistency);
2339           SNPRINT2(results, "\t%7.3f", cpi->worst_consistency);
2340         }
2341
2342         fprintf(f, "%s\t    Time\tRcErr\tAbsErr\n", headings);
2343         fprintf(f, "%s\t%8.0f\t%7.2f\t%7.2f\n", results, total_encode_time,
2344                 rate_err, fabs(rate_err));
2345       }
2346
2347       fclose(f);
2348     }
2349
2350 #endif
2351
2352 #if 0
2353     {
2354       printf("\n_pick_loop_filter_level:%d\n", cpi->time_pick_lpf / 1000);
2355       printf("\n_frames recive_data encod_mb_row compress_frame  Total\n");
2356       printf("%6d %10ld %10ld %10ld %10ld\n", cpi->common.current_video_frame,
2357              cpi->time_receive_data / 1000, cpi->time_encode_sb_row / 1000,
2358              cpi->time_compress_data / 1000,
2359              (cpi->time_receive_data + cpi->time_compress_data) / 1000);
2360     }
2361 #endif
2362   }
2363
2364 #if CONFIG_VP9_TEMPORAL_DENOISING
2365   vp9_denoiser_free(&(cpi->denoiser));
2366 #endif
2367
2368   for (t = 0; t < cpi->num_workers; ++t) {
2369     VPxWorker *const worker = &cpi->workers[t];
2370     EncWorkerData *const thread_data = &cpi->tile_thr_data[t];
2371
2372     // Deallocate allocated threads.
2373     vpx_get_worker_interface()->end(worker);
2374
2375     // Deallocate allocated thread data.
2376     if (t < cpi->num_workers - 1) {
2377       vpx_free(thread_data->td->counts);
2378       vp9_free_pc_tree(thread_data->td);
2379       vpx_free(thread_data->td);
2380     }
2381   }
2382   vpx_free(cpi->tile_thr_data);
2383   vpx_free(cpi->workers);
2384   vp9_row_mt_mem_dealloc(cpi);
2385
2386   if (cpi->num_workers > 1) {
2387     vp9_loop_filter_dealloc(&cpi->lf_row_sync);
2388     vp9_bitstream_encode_tiles_buffer_dealloc(cpi);
2389   }
2390
2391   vp9_alt_ref_aq_destroy(cpi->alt_ref_aq);
2392
2393   dealloc_compressor_data(cpi);
2394
2395   for (i = 0; i < sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]);
2396        ++i) {
2397     vpx_free(cpi->mbgraph_stats[i].mb_stats);
2398   }
2399
2400 #if CONFIG_FP_MB_STATS
2401   if (cpi->use_fp_mb_stats) {
2402     vpx_free(cpi->twopass.frame_mb_stats_buf);
2403     cpi->twopass.frame_mb_stats_buf = NULL;
2404   }
2405 #endif
2406
2407   vp9_remove_common(cm);
2408   vp9_free_ref_frame_buffers(cm->buffer_pool);
2409 #if CONFIG_VP9_POSTPROC
2410   vp9_free_postproc_buffers(cm);
2411 #endif
2412   vpx_free(cpi);
2413
2414 #if CONFIG_VP9_TEMPORAL_DENOISING
2415 #ifdef OUTPUT_YUV_DENOISED
2416   fclose(yuv_denoised_file);
2417 #endif
2418 #endif
2419 #ifdef OUTPUT_YUV_SKINMAP
2420   fclose(yuv_skinmap_file);
2421 #endif
2422 #ifdef OUTPUT_YUV_REC
2423   fclose(yuv_rec_file);
2424 #endif
2425
2426 #if 0
2427
2428   if (keyfile)
2429     fclose(keyfile);
2430
2431   if (framepsnr)
2432     fclose(framepsnr);
2433
2434   if (kf_list)
2435     fclose(kf_list);
2436
2437 #endif
2438 }
2439
2440 static void generate_psnr_packet(VP9_COMP *cpi) {
2441   struct vpx_codec_cx_pkt pkt;
2442   int i;
2443   PSNR_STATS psnr;
2444 #if CONFIG_VP9_HIGHBITDEPTH
2445   vpx_calc_highbd_psnr(cpi->raw_source_frame, cpi->common.frame_to_show, &psnr,
2446                        cpi->td.mb.e_mbd.bd, cpi->oxcf.input_bit_depth);
2447 #else
2448   vpx_calc_psnr(cpi->raw_source_frame, cpi->common.frame_to_show, &psnr);
2449 #endif
2450
2451   for (i = 0; i < 4; ++i) {
2452     pkt.data.psnr.samples[i] = psnr.samples[i];
2453     pkt.data.psnr.sse[i] = psnr.sse[i];
2454     pkt.data.psnr.psnr[i] = psnr.psnr[i];
2455   }
2456   pkt.kind = VPX_CODEC_PSNR_PKT;
2457   if (cpi->use_svc)
2458     cpi->svc
2459         .layer_context[cpi->svc.spatial_layer_id *
2460                        cpi->svc.number_temporal_layers]
2461         .psnr_pkt = pkt.data.psnr;
2462   else
2463     vpx_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
2464 }
2465
2466 int vp9_use_as_reference(VP9_COMP *cpi, int ref_frame_flags) {
2467   if (ref_frame_flags > 7) return -1;
2468
2469   cpi->ref_frame_flags = ref_frame_flags;
2470   return 0;
2471 }
2472
2473 void vp9_update_reference(VP9_COMP *cpi, int ref_frame_flags) {
2474   cpi->ext_refresh_golden_frame = (ref_frame_flags & VP9_GOLD_FLAG) != 0;
2475   cpi->ext_refresh_alt_ref_frame = (ref_frame_flags & VP9_ALT_FLAG) != 0;
2476   cpi->ext_refresh_last_frame = (ref_frame_flags & VP9_LAST_FLAG) != 0;
2477   cpi->ext_refresh_frame_flags_pending = 1;
2478 }
2479
2480 static YV12_BUFFER_CONFIG *get_vp9_ref_frame_buffer(
2481     VP9_COMP *cpi, VP9_REFFRAME ref_frame_flag) {
2482   MV_REFERENCE_FRAME ref_frame = NONE;
2483   if (ref_frame_flag == VP9_LAST_FLAG)
2484     ref_frame = LAST_FRAME;
2485   else if (ref_frame_flag == VP9_GOLD_FLAG)
2486     ref_frame = GOLDEN_FRAME;
2487   else if (ref_frame_flag == VP9_ALT_FLAG)
2488     ref_frame = ALTREF_FRAME;
2489
2490   return ref_frame == NONE ? NULL : get_ref_frame_buffer(cpi, ref_frame);
2491 }
2492
2493 int vp9_copy_reference_enc(VP9_COMP *cpi, VP9_REFFRAME ref_frame_flag,
2494                            YV12_BUFFER_CONFIG *sd) {
2495   YV12_BUFFER_CONFIG *cfg = get_vp9_ref_frame_buffer(cpi, ref_frame_flag);
2496   if (cfg) {
2497     vpx_yv12_copy_frame(cfg, sd);
2498     return 0;
2499   } else {
2500     return -1;
2501   }
2502 }
2503
2504 int vp9_set_reference_enc(VP9_COMP *cpi, VP9_REFFRAME ref_frame_flag,
2505                           YV12_BUFFER_CONFIG *sd) {
2506   YV12_BUFFER_CONFIG *cfg = get_vp9_ref_frame_buffer(cpi, ref_frame_flag);
2507   if (cfg) {
2508     vpx_yv12_copy_frame(sd, cfg);
2509     return 0;
2510   } else {
2511     return -1;
2512   }
2513 }
2514
2515 int vp9_update_entropy(VP9_COMP *cpi, int update) {
2516   cpi->ext_refresh_frame_context = update;
2517   cpi->ext_refresh_frame_context_pending = 1;
2518   return 0;
2519 }
2520
2521 #ifdef OUTPUT_YUV_REC
2522 void vp9_write_yuv_rec_frame(VP9_COMMON *cm) {
2523   YV12_BUFFER_CONFIG *s = cm->frame_to_show;
2524   uint8_t *src = s->y_buffer;
2525   int h = cm->height;
2526
2527 #if CONFIG_VP9_HIGHBITDEPTH
2528   if (s->flags & YV12_FLAG_HIGHBITDEPTH) {
2529     uint16_t *src16 = CONVERT_TO_SHORTPTR(s->y_buffer);
2530
2531     do {
2532       fwrite(src16, s->y_width, 2, yuv_rec_file);
2533       src16 += s->y_stride;
2534     } while (--h);
2535
2536     src16 = CONVERT_TO_SHORTPTR(s->u_buffer);
2537     h = s->uv_height;
2538
2539     do {
2540       fwrite(src16, s->uv_width, 2, yuv_rec_file);
2541       src16 += s->uv_stride;
2542     } while (--h);
2543
2544     src16 = CONVERT_TO_SHORTPTR(s->v_buffer);
2545     h = s->uv_height;
2546
2547     do {
2548       fwrite(src16, s->uv_width, 2, yuv_rec_file);
2549       src16 += s->uv_stride;
2550     } while (--h);
2551
2552     fflush(yuv_rec_file);
2553     return;
2554   }
2555 #endif  // CONFIG_VP9_HIGHBITDEPTH
2556
2557   do {
2558     fwrite(src, s->y_width, 1, yuv_rec_file);
2559     src += s->y_stride;
2560   } while (--h);
2561
2562   src = s->u_buffer;
2563   h = s->uv_height;
2564
2565   do {
2566     fwrite(src, s->uv_width, 1, yuv_rec_file);
2567     src += s->uv_stride;
2568   } while (--h);
2569
2570   src = s->v_buffer;
2571   h = s->uv_height;
2572
2573   do {
2574     fwrite(src, s->uv_width, 1, yuv_rec_file);
2575     src += s->uv_stride;
2576   } while (--h);
2577
2578   fflush(yuv_rec_file);
2579 }
2580 #endif
2581
2582 #if CONFIG_VP9_HIGHBITDEPTH
2583 static void scale_and_extend_frame_nonnormative(const YV12_BUFFER_CONFIG *src,
2584                                                 YV12_BUFFER_CONFIG *dst,
2585                                                 int bd) {
2586 #else
2587 static void scale_and_extend_frame_nonnormative(const YV12_BUFFER_CONFIG *src,
2588                                                 YV12_BUFFER_CONFIG *dst) {
2589 #endif  // CONFIG_VP9_HIGHBITDEPTH
2590   // TODO(dkovalev): replace YV12_BUFFER_CONFIG with vpx_image_t
2591   int i;
2592   const uint8_t *const srcs[3] = { src->y_buffer, src->u_buffer,
2593                                    src->v_buffer };
2594   const int src_strides[3] = { src->y_stride, src->uv_stride, src->uv_stride };
2595   const int src_widths[3] = { src->y_crop_width, src->uv_crop_width,
2596                               src->uv_crop_width };
2597   const int src_heights[3] = { src->y_crop_height, src->uv_crop_height,
2598                                src->uv_crop_height };
2599   uint8_t *const dsts[3] = { dst->y_buffer, dst->u_buffer, dst->v_buffer };
2600   const int dst_strides[3] = { dst->y_stride, dst->uv_stride, dst->uv_stride };
2601   const int dst_widths[3] = { dst->y_crop_width, dst->uv_crop_width,
2602                               dst->uv_crop_width };
2603   const int dst_heights[3] = { dst->y_crop_height, dst->uv_crop_height,
2604                                dst->uv_crop_height };
2605
2606   for (i = 0; i < MAX_MB_PLANE; ++i) {
2607 #if CONFIG_VP9_HIGHBITDEPTH
2608     if (src->flags & YV12_FLAG_HIGHBITDEPTH) {
2609       vp9_highbd_resize_plane(srcs[i], src_heights[i], src_widths[i],
2610                               src_strides[i], dsts[i], dst_heights[i],
2611                               dst_widths[i], dst_strides[i], bd);
2612     } else {
2613       vp9_resize_plane(srcs[i], src_heights[i], src_widths[i], src_strides[i],
2614                        dsts[i], dst_heights[i], dst_widths[i], dst_strides[i]);
2615     }
2616 #else
2617     vp9_resize_plane(srcs[i], src_heights[i], src_widths[i], src_strides[i],
2618                      dsts[i], dst_heights[i], dst_widths[i], dst_strides[i]);
2619 #endif  // CONFIG_VP9_HIGHBITDEPTH
2620   }
2621   vpx_extend_frame_borders(dst);
2622 }
2623
2624 #if CONFIG_VP9_HIGHBITDEPTH
2625 static void scale_and_extend_frame(const YV12_BUFFER_CONFIG *src,
2626                                    YV12_BUFFER_CONFIG *dst, int bd,
2627                                    INTERP_FILTER filter_type,
2628                                    int phase_scaler) {
2629   const int src_w = src->y_crop_width;
2630   const int src_h = src->y_crop_height;
2631   const int dst_w = dst->y_crop_width;
2632   const int dst_h = dst->y_crop_height;
2633   const uint8_t *const srcs[3] = { src->y_buffer, src->u_buffer,
2634                                    src->v_buffer };
2635   const int src_strides[3] = { src->y_stride, src->uv_stride, src->uv_stride };
2636   uint8_t *const dsts[3] = { dst->y_buffer, dst->u_buffer, dst->v_buffer };
2637   const int dst_strides[3] = { dst->y_stride, dst->uv_stride, dst->uv_stride };
2638   const InterpKernel *const kernel = vp9_filter_kernels[filter_type];
2639   int x, y, i;
2640
2641   for (i = 0; i < MAX_MB_PLANE; ++i) {
2642     const int factor = (i == 0 || i == 3 ? 1 : 2);
2643     const int src_stride = src_strides[i];
2644     const int dst_stride = dst_strides[i];
2645     for (y = 0; y < dst_h; y += 16) {
2646       const int y_q4 = y * (16 / factor) * src_h / dst_h + phase_scaler;
2647       for (x = 0; x < dst_w; x += 16) {
2648         const int x_q4 = x * (16 / factor) * src_w / dst_w + phase_scaler;
2649         const uint8_t *src_ptr = srcs[i] +
2650                                  (y / factor) * src_h / dst_h * src_stride +
2651                                  (x / factor) * src_w / dst_w;
2652         uint8_t *dst_ptr = dsts[i] + (y / factor) * dst_stride + (x / factor);
2653
2654         if (src->flags & YV12_FLAG_HIGHBITDEPTH) {
2655           vpx_highbd_convolve8(CONVERT_TO_SHORTPTR(src_ptr), src_stride,
2656                                CONVERT_TO_SHORTPTR(dst_ptr), dst_stride, kernel,
2657                                x_q4 & 0xf, 16 * src_w / dst_w, y_q4 & 0xf,
2658                                16 * src_h / dst_h, 16 / factor, 16 / factor,
2659                                bd);
2660         } else {
2661           vpx_scaled_2d(src_ptr, src_stride, dst_ptr, dst_stride, kernel,
2662                         x_q4 & 0xf, 16 * src_w / dst_w, y_q4 & 0xf,
2663                         16 * src_h / dst_h, 16 / factor, 16 / factor);
2664         }
2665       }
2666     }
2667   }
2668
2669   vpx_extend_frame_borders(dst);
2670 }
2671 #endif  // CONFIG_VP9_HIGHBITDEPTH
2672
2673 static int scale_down(VP9_COMP *cpi, int q) {
2674   RATE_CONTROL *const rc = &cpi->rc;
2675   GF_GROUP *const gf_group = &cpi->twopass.gf_group;
2676   int scale = 0;
2677   assert(frame_is_kf_gf_arf(cpi));
2678
2679   if (rc->frame_size_selector == UNSCALED &&
2680       q >= rc->rf_level_maxq[gf_group->rf_level[gf_group->index]]) {
2681     const int max_size_thresh =
2682         (int)(rate_thresh_mult[SCALE_STEP1] *
2683               VPXMAX(rc->this_frame_target, rc->avg_frame_bandwidth));
2684     scale = rc->projected_frame_size > max_size_thresh ? 1 : 0;
2685   }
2686   return scale;
2687 }
2688
2689 static int big_rate_miss_high_threshold(VP9_COMP *cpi) {
2690   const RATE_CONTROL *const rc = &cpi->rc;
2691   int big_miss_high;
2692
2693   if (frame_is_kf_gf_arf(cpi))
2694     big_miss_high = rc->this_frame_target * 3 / 2;
2695   else
2696     big_miss_high = rc->this_frame_target * 2;
2697
2698   return big_miss_high;
2699 }
2700
2701 static int big_rate_miss(VP9_COMP *cpi) {
2702   const RATE_CONTROL *const rc = &cpi->rc;
2703   int big_miss_high;
2704   int big_miss_low;
2705
2706   // Ignore for overlay frames
2707   if (rc->is_src_frame_alt_ref) {
2708     return 0;
2709   } else {
2710     big_miss_low = (rc->this_frame_target / 2);
2711     big_miss_high = big_rate_miss_high_threshold(cpi);
2712
2713     return (rc->projected_frame_size > big_miss_high) ||
2714            (rc->projected_frame_size < big_miss_low);
2715   }
2716 }
2717
2718 // test in two pass for the first
2719 static int two_pass_first_group_inter(VP9_COMP *cpi) {
2720   TWO_PASS *const twopass = &cpi->twopass;
2721   GF_GROUP *const gf_group = &twopass->gf_group;
2722   if ((cpi->oxcf.pass == 2) &&
2723       (gf_group->index == gf_group->first_inter_index)) {
2724     return 1;
2725   } else {
2726     return 0;
2727   }
2728 }
2729
2730 // Function to test for conditions that indicate we should loop
2731 // back and recode a frame.
2732 static int recode_loop_test(VP9_COMP *cpi, int high_limit, int low_limit, int q,
2733                             int maxq, int minq) {
2734   const RATE_CONTROL *const rc = &cpi->rc;
2735   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
2736   const int frame_is_kfgfarf = frame_is_kf_gf_arf(cpi);
2737   int force_recode = 0;
2738
2739   if ((rc->projected_frame_size >= rc->max_frame_bandwidth) ||
2740       big_rate_miss(cpi) || (cpi->sf.recode_loop == ALLOW_RECODE) ||
2741       (two_pass_first_group_inter(cpi) &&
2742        (cpi->sf.recode_loop == ALLOW_RECODE_FIRST)) ||
2743       (frame_is_kfgfarf && (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF))) {
2744     if (frame_is_kfgfarf && (oxcf->resize_mode == RESIZE_DYNAMIC) &&
2745         scale_down(cpi, q)) {
2746       // Code this group at a lower resolution.
2747       cpi->resize_pending = 1;
2748       return 1;
2749     }
2750
2751     // Force recode for extreme overshoot.
2752     if ((rc->projected_frame_size >= rc->max_frame_bandwidth) ||
2753         (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF &&
2754          rc->projected_frame_size >= big_rate_miss_high_threshold(cpi))) {
2755       return 1;
2756     }
2757
2758     // TODO(agrange) high_limit could be greater than the scale-down threshold.
2759     if ((rc->projected_frame_size > high_limit && q < maxq) ||
2760         (rc->projected_frame_size < low_limit && q > minq)) {
2761       force_recode = 1;
2762     } else if (cpi->oxcf.rc_mode == VPX_CQ) {
2763       // Deal with frame undershoot and whether or not we are
2764       // below the automatically set cq level.
2765       if (q > oxcf->cq_level &&
2766           rc->projected_frame_size < ((rc->this_frame_target * 7) >> 3)) {
2767         force_recode = 1;
2768       }
2769     }
2770   }
2771   return force_recode;
2772 }
2773
2774 void vp9_update_reference_frames(VP9_COMP *cpi) {
2775   VP9_COMMON *const cm = &cpi->common;
2776   BufferPool *const pool = cm->buffer_pool;
2777
2778   // At this point the new frame has been encoded.
2779   // If any buffer copy / swapping is signaled it should be done here.
2780   if (cm->frame_type == KEY_FRAME) {
2781     ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->gld_fb_idx],
2782                cm->new_fb_idx);
2783     ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->alt_fb_idx],
2784                cm->new_fb_idx);
2785   } else if (vp9_preserve_existing_gf(cpi)) {
2786     // We have decided to preserve the previously existing golden frame as our
2787     // new ARF frame. However, in the short term in function
2788     // vp9_get_refresh_mask() we left it in the GF slot and, if
2789     // we're updating the GF with the current decoded frame, we save it to the
2790     // ARF slot instead.
2791     // We now have to update the ARF with the current frame and swap gld_fb_idx
2792     // and alt_fb_idx so that, overall, we've stored the old GF in the new ARF
2793     // slot and, if we're updating the GF, the current frame becomes the new GF.
2794     int tmp;
2795
2796     ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->alt_fb_idx],
2797                cm->new_fb_idx);
2798
2799     tmp = cpi->alt_fb_idx;
2800     cpi->alt_fb_idx = cpi->gld_fb_idx;
2801     cpi->gld_fb_idx = tmp;
2802
2803     if (is_two_pass_svc(cpi)) {
2804       cpi->svc.layer_context[0].gold_ref_idx = cpi->gld_fb_idx;
2805       cpi->svc.layer_context[0].alt_ref_idx = cpi->alt_fb_idx;
2806     }
2807   } else { /* For non key/golden frames */
2808     if (cpi->refresh_alt_ref_frame) {
2809       int arf_idx = cpi->alt_fb_idx;
2810       if ((cpi->oxcf.pass == 2) && cpi->multi_arf_allowed) {
2811         const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
2812         arf_idx = gf_group->arf_update_idx[gf_group->index];
2813       }
2814
2815       ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[arf_idx], cm->new_fb_idx);
2816       memcpy(cpi->interp_filter_selected[ALTREF_FRAME],
2817              cpi->interp_filter_selected[0],
2818              sizeof(cpi->interp_filter_selected[0]));
2819     }
2820
2821     if (cpi->refresh_golden_frame) {
2822       ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->gld_fb_idx],
2823                  cm->new_fb_idx);
2824       if (!cpi->rc.is_src_frame_alt_ref)
2825         memcpy(cpi->interp_filter_selected[GOLDEN_FRAME],
2826                cpi->interp_filter_selected[0],
2827                sizeof(cpi->interp_filter_selected[0]));
2828       else
2829         memcpy(cpi->interp_filter_selected[GOLDEN_FRAME],
2830                cpi->interp_filter_selected[ALTREF_FRAME],
2831                sizeof(cpi->interp_filter_selected[ALTREF_FRAME]));
2832     }
2833   }
2834
2835   if (cpi->refresh_last_frame) {
2836     ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->lst_fb_idx],
2837                cm->new_fb_idx);
2838     if (!cpi->rc.is_src_frame_alt_ref)
2839       memcpy(cpi->interp_filter_selected[LAST_FRAME],
2840              cpi->interp_filter_selected[0],
2841              sizeof(cpi->interp_filter_selected[0]));
2842   }
2843 #if CONFIG_VP9_TEMPORAL_DENOISING
2844   if (cpi->oxcf.noise_sensitivity > 0 && denoise_svc(cpi) &&
2845       cpi->denoiser.denoising_level > kDenLowLow) {
2846     int svc_base_is_key = 0;
2847     if (cpi->use_svc) {
2848       int realloc_fail = 0;
2849       int layer = LAYER_IDS_TO_IDX(cpi->svc.spatial_layer_id,
2850                                    cpi->svc.temporal_layer_id,
2851                                    cpi->svc.number_temporal_layers);
2852       LAYER_CONTEXT *lc = &cpi->svc.layer_context[layer];
2853       svc_base_is_key = lc->is_key_frame;
2854
2855       // Check if we need to allocate extra buffers in the denoiser for
2856       // refreshed frames.
2857       realloc_fail = vp9_denoiser_realloc_svc(
2858           cm, &cpi->denoiser, cpi->refresh_alt_ref_frame,
2859           cpi->refresh_golden_frame, cpi->refresh_last_frame, cpi->alt_fb_idx,
2860           cpi->gld_fb_idx, cpi->lst_fb_idx);
2861       if (realloc_fail)
2862         vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
2863                            "Failed to re-allocate denoiser for SVC");
2864     }
2865     vp9_denoiser_update_frame_info(
2866         &cpi->denoiser, *cpi->Source, cpi->common.frame_type,
2867         cpi->refresh_alt_ref_frame, cpi->refresh_golden_frame,
2868         cpi->refresh_last_frame, cpi->alt_fb_idx, cpi->gld_fb_idx,
2869         cpi->lst_fb_idx, cpi->resize_pending, svc_base_is_key);
2870   }
2871 #endif
2872   if (is_one_pass_cbr_svc(cpi)) {
2873     // Keep track of frame index for each reference frame.
2874     SVC *const svc = &cpi->svc;
2875     if (cm->frame_type == KEY_FRAME) {
2876       svc->ref_frame_index[cpi->lst_fb_idx] = svc->current_superframe;
2877       svc->ref_frame_index[cpi->gld_fb_idx] = svc->current_superframe;
2878       svc->ref_frame_index[cpi->alt_fb_idx] = svc->current_superframe;
2879     } else {
2880       if (cpi->refresh_last_frame)
2881         svc->ref_frame_index[cpi->lst_fb_idx] = svc->current_superframe;
2882       if (cpi->refresh_golden_frame)
2883         svc->ref_frame_index[cpi->gld_fb_idx] = svc->current_superframe;
2884       if (cpi->refresh_alt_ref_frame)
2885         svc->ref_frame_index[cpi->alt_fb_idx] = svc->current_superframe;
2886     }
2887   }
2888 }
2889
2890 static void loopfilter_frame(VP9_COMP *cpi, VP9_COMMON *cm) {
2891   MACROBLOCKD *xd = &cpi->td.mb.e_mbd;
2892   struct loopfilter *lf = &cm->lf;
2893
2894   const int is_reference_frame =
2895       (cm->frame_type == KEY_FRAME || cpi->refresh_last_frame ||
2896        cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame);
2897
2898   if (xd->lossless) {
2899     lf->filter_level = 0;
2900     lf->last_filt_level = 0;
2901   } else {
2902     struct vpx_usec_timer timer;
2903
2904     vpx_clear_system_state();
2905
2906     vpx_usec_timer_start(&timer);
2907
2908     if (!cpi->rc.is_src_frame_alt_ref) {
2909       if ((cpi->common.frame_type == KEY_FRAME) &&
2910           (!cpi->rc.this_key_frame_forced)) {
2911         lf->last_filt_level = 0;
2912       }
2913       vp9_pick_filter_level(cpi->Source, cpi, cpi->sf.lpf_pick);
2914       lf->last_filt_level = lf->filter_level;
2915     } else {
2916       lf->filter_level = 0;
2917     }
2918
2919     vpx_usec_timer_mark(&timer);
2920     cpi->time_pick_lpf += vpx_usec_timer_elapsed(&timer);
2921   }
2922
2923   if (lf->filter_level > 0 && is_reference_frame) {
2924     vp9_build_mask_frame(cm, lf->filter_level, 0);
2925
2926     if (cpi->num_workers > 1)
2927       vp9_loop_filter_frame_mt(cm->frame_to_show, cm, xd->plane,
2928                                lf->filter_level, 0, 0, cpi->workers,
2929                                cpi->num_workers, &cpi->lf_row_sync);
2930     else
2931       vp9_loop_filter_frame(cm->frame_to_show, cm, xd, lf->filter_level, 0, 0);
2932   }
2933
2934   vpx_extend_frame_inner_borders(cm->frame_to_show);
2935 }
2936
2937 static INLINE void alloc_frame_mvs(VP9_COMMON *const cm, int buffer_idx) {
2938   RefCntBuffer *const new_fb_ptr = &cm->buffer_pool->frame_bufs[buffer_idx];
2939   if (new_fb_ptr->mvs == NULL || new_fb_ptr->mi_rows < cm->mi_rows ||
2940       new_fb_ptr->mi_cols < cm->mi_cols) {
2941     vpx_free(new_fb_ptr->mvs);
2942     CHECK_MEM_ERROR(cm, new_fb_ptr->mvs,
2943                     (MV_REF *)vpx_calloc(cm->mi_rows * cm->mi_cols,
2944                                          sizeof(*new_fb_ptr->mvs)));
2945     new_fb_ptr->mi_rows = cm->mi_rows;
2946     new_fb_ptr->mi_cols = cm->mi_cols;
2947   }
2948 }
2949
2950 void vp9_scale_references(VP9_COMP *cpi) {
2951   VP9_COMMON *cm = &cpi->common;
2952   MV_REFERENCE_FRAME ref_frame;
2953   const VP9_REFFRAME ref_mask[3] = { VP9_LAST_FLAG, VP9_GOLD_FLAG,
2954                                      VP9_ALT_FLAG };
2955
2956   for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
2957     // Need to convert from VP9_REFFRAME to index into ref_mask (subtract 1).
2958     if (cpi->ref_frame_flags & ref_mask[ref_frame - 1]) {
2959       BufferPool *const pool = cm->buffer_pool;
2960       const YV12_BUFFER_CONFIG *const ref =
2961           get_ref_frame_buffer(cpi, ref_frame);
2962
2963       if (ref == NULL) {
2964         cpi->scaled_ref_idx[ref_frame - 1] = INVALID_IDX;
2965         continue;
2966       }
2967
2968 #if CONFIG_VP9_HIGHBITDEPTH
2969       if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height) {
2970         RefCntBuffer *new_fb_ptr = NULL;
2971         int force_scaling = 0;
2972         int new_fb = cpi->scaled_ref_idx[ref_frame - 1];
2973         if (new_fb == INVALID_IDX) {
2974           new_fb = get_free_fb(cm);
2975           force_scaling = 1;
2976         }
2977         if (new_fb == INVALID_IDX) return;
2978         new_fb_ptr = &pool->frame_bufs[new_fb];
2979         if (force_scaling || new_fb_ptr->buf.y_crop_width != cm->width ||
2980             new_fb_ptr->buf.y_crop_height != cm->height) {
2981           if (vpx_realloc_frame_buffer(&new_fb_ptr->buf, cm->width, cm->height,
2982                                        cm->subsampling_x, cm->subsampling_y,
2983                                        cm->use_highbitdepth,
2984                                        VP9_ENC_BORDER_IN_PIXELS,
2985                                        cm->byte_alignment, NULL, NULL, NULL))
2986             vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
2987                                "Failed to allocate frame buffer");
2988           scale_and_extend_frame(ref, &new_fb_ptr->buf, (int)cm->bit_depth,
2989                                  EIGHTTAP, 0);
2990           cpi->scaled_ref_idx[ref_frame - 1] = new_fb;
2991           alloc_frame_mvs(cm, new_fb);
2992         }
2993 #else
2994       if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height) {
2995         RefCntBuffer *new_fb_ptr = NULL;
2996         int force_scaling = 0;
2997         int new_fb = cpi->scaled_ref_idx[ref_frame - 1];
2998         if (new_fb == INVALID_IDX) {
2999           new_fb = get_free_fb(cm);
3000           force_scaling = 1;
3001         }
3002         if (new_fb == INVALID_IDX) return;
3003         new_fb_ptr = &pool->frame_bufs[new_fb];
3004         if (force_scaling || new_fb_ptr->buf.y_crop_width != cm->width ||
3005             new_fb_ptr->buf.y_crop_height != cm->height) {
3006           if (vpx_realloc_frame_buffer(&new_fb_ptr->buf, cm->width, cm->height,
3007                                        cm->subsampling_x, cm->subsampling_y,
3008                                        VP9_ENC_BORDER_IN_PIXELS,
3009                                        cm->byte_alignment, NULL, NULL, NULL))
3010             vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
3011                                "Failed to allocate frame buffer");
3012           vp9_scale_and_extend_frame(ref, &new_fb_ptr->buf, EIGHTTAP, 0);
3013           cpi->scaled_ref_idx[ref_frame - 1] = new_fb;
3014           alloc_frame_mvs(cm, new_fb);
3015         }
3016 #endif  // CONFIG_VP9_HIGHBITDEPTH
3017       } else {
3018         int buf_idx;
3019         RefCntBuffer *buf = NULL;
3020         if (cpi->oxcf.pass == 0 && !cpi->use_svc) {
3021           // Check for release of scaled reference.
3022           buf_idx = cpi->scaled_ref_idx[ref_frame - 1];
3023           buf = (buf_idx != INVALID_IDX) ? &pool->frame_bufs[buf_idx] : NULL;
3024           if (buf != NULL) {
3025             --buf->ref_count;
3026             cpi->scaled_ref_idx[ref_frame - 1] = INVALID_IDX;
3027           }
3028         }
3029         buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
3030         buf = &pool->frame_bufs[buf_idx];
3031         buf->buf.y_crop_width = ref->y_crop_width;
3032         buf->buf.y_crop_height = ref->y_crop_height;
3033         cpi->scaled_ref_idx[ref_frame - 1] = buf_idx;
3034         ++buf->ref_count;
3035       }
3036     } else {
3037       if (cpi->oxcf.pass != 0 || cpi->use_svc)
3038         cpi->scaled_ref_idx[ref_frame - 1] = INVALID_IDX;
3039     }
3040   }
3041 }
3042
3043 static void release_scaled_references(VP9_COMP *cpi) {
3044   VP9_COMMON *cm = &cpi->common;
3045   int i;
3046   if (cpi->oxcf.pass == 0 && !cpi->use_svc) {
3047     // Only release scaled references under certain conditions:
3048     // if reference will be updated, or if scaled reference has same resolution.
3049     int refresh[3];
3050     refresh[0] = (cpi->refresh_last_frame) ? 1 : 0;
3051     refresh[1] = (cpi->refresh_golden_frame) ? 1 : 0;
3052     refresh[2] = (cpi->refresh_alt_ref_frame) ? 1 : 0;
3053     for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
3054       const int idx = cpi->scaled_ref_idx[i - 1];
3055       RefCntBuffer *const buf =
3056           idx != INVALID_IDX ? &cm->buffer_pool->frame_bufs[idx] : NULL;
3057       const YV12_BUFFER_CONFIG *const ref = get_ref_frame_buffer(cpi, i);
3058       if (buf != NULL &&
3059           (refresh[i - 1] || (buf->buf.y_crop_width == ref->y_crop_width &&
3060                               buf->buf.y_crop_height == ref->y_crop_height))) {
3061         --buf->ref_count;
3062         cpi->scaled_ref_idx[i - 1] = INVALID_IDX;
3063       }
3064     }
3065   } else {
3066     for (i = 0; i < MAX_REF_FRAMES; ++i) {
3067       const int idx = cpi->scaled_ref_idx[i];
3068       RefCntBuffer *const buf =
3069           idx != INVALID_IDX ? &cm->buffer_pool->frame_bufs[idx] : NULL;
3070       if (buf != NULL) {
3071         --buf->ref_count;
3072         cpi->scaled_ref_idx[i] = INVALID_IDX;
3073       }
3074     }
3075   }
3076 }
3077
3078 static void full_to_model_count(unsigned int *model_count,
3079                                 unsigned int *full_count) {
3080   int n;
3081   model_count[ZERO_TOKEN] = full_count[ZERO_TOKEN];
3082   model_count[ONE_TOKEN] = full_count[ONE_TOKEN];
3083   model_count[TWO_TOKEN] = full_count[TWO_TOKEN];
3084   for (n = THREE_TOKEN; n < EOB_TOKEN; ++n)
3085     model_count[TWO_TOKEN] += full_count[n];
3086   model_count[EOB_MODEL_TOKEN] = full_count[EOB_TOKEN];
3087 }
3088
3089 static void full_to_model_counts(vp9_coeff_count_model *model_count,
3090                                  vp9_coeff_count *full_count) {
3091   int i, j, k, l;
3092
3093   for (i = 0; i < PLANE_TYPES; ++i)
3094     for (j = 0; j < REF_TYPES; ++j)
3095       for (k = 0; k < COEF_BANDS; ++k)
3096         for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l)
3097           full_to_model_count(model_count[i][j][k][l], full_count[i][j][k][l]);
3098 }
3099
3100 #if 0 && CONFIG_INTERNAL_STATS
3101 static void output_frame_level_debug_stats(VP9_COMP *cpi) {
3102   VP9_COMMON *const cm = &cpi->common;
3103   FILE *const f = fopen("tmp.stt", cm->current_video_frame ? "a" : "w");
3104   int64_t recon_err;
3105
3106   vpx_clear_system_state();
3107
3108 #if CONFIG_VP9_HIGHBITDEPTH
3109   if (cm->use_highbitdepth) {
3110     recon_err = vpx_highbd_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
3111   } else {
3112     recon_err = vpx_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
3113   }
3114 #else
3115   recon_err = vpx_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
3116 #endif  // CONFIG_VP9_HIGHBITDEPTH
3117
3118
3119   if (cpi->twopass.total_left_stats.coded_error != 0.0) {
3120     double dc_quant_devisor;
3121 #if CONFIG_VP9_HIGHBITDEPTH
3122     switch (cm->bit_depth) {
3123       case VPX_BITS_8:
3124         dc_quant_devisor = 4.0;
3125         break;
3126       case VPX_BITS_10:
3127         dc_quant_devisor = 16.0;
3128         break;
3129       case VPX_BITS_12:
3130         dc_quant_devisor = 64.0;
3131         break;
3132       default:
3133         assert(0 && "bit_depth must be VPX_BITS_8, VPX_BITS_10 or VPX_BITS_12");
3134         break;
3135     }
3136 #else
3137     dc_quant_devisor = 4.0;
3138 #endif
3139
3140     if (!cm->current_video_frame) {
3141       fprintf(f, "frame, width, height, last ts, last end ts, "
3142           "source_alt_ref_pending, source_alt_ref_active, "
3143           "this_frame_target, projected_frame_size, "
3144           "projected_frame_size / MBs, "
3145           "projected_frame_size - this_frame_target, "
3146           "vbr_bits_off_target, vbr_bits_off_target_fast, "
3147           "twopass.extend_minq, twopass.extend_minq_fast, "
3148           "total_target_vs_actual, "
3149           "starting_buffer_level - bits_off_target, "
3150           "total_actual_bits, base_qindex, q for base_qindex, "
3151           "dc quant, q for active_worst_quality, avg_q, q for oxcf.cq_level, "
3152           "refresh_last_frame, refresh_golden_frame, refresh_alt_ref_frame, "
3153           "frame_type, gfu_boost, "
3154           "twopass.bits_left, "
3155           "twopass.total_left_stats.coded_error, "
3156           "twopass.bits_left / (1 + twopass.total_left_stats.coded_error), "
3157           "tot_recode_hits, recon_err, kf_boost, "
3158           "twopass.kf_zeromotion_pct, twopass.fr_content_type, "
3159           "filter_level, seg.aq_av_offset\n");
3160     }
3161
3162     fprintf(f, "%10u, %d, %d, %10"PRId64", %10"PRId64", %d, %d, %10d, %10d, "
3163         "%10d, %10d, %10"PRId64", %10"PRId64", %5d, %5d, %10"PRId64", "
3164         "%10"PRId64", %10"PRId64", %10d, %7.2lf, %7.2lf, %7.2lf, %7.2lf, "
3165         "%7.2lf, %6d, %6d, %5d, %5d, %5d, %10"PRId64", %10.3lf, %10lf, %8u, "
3166         "%10"PRId64", %10d, %10d, %10d, %10d, %10d\n",
3167         cpi->common.current_video_frame,
3168         cm->width, cm->height,
3169         cpi->last_time_stamp_seen,
3170         cpi->last_end_time_stamp_seen,
3171         cpi->rc.source_alt_ref_pending,
3172         cpi->rc.source_alt_ref_active,
3173         cpi->rc.this_frame_target,
3174         cpi->rc.projected_frame_size,
3175         cpi->rc.projected_frame_size / cpi->common.MBs,
3176         (cpi->rc.projected_frame_size - cpi->rc.this_frame_target),
3177         cpi->rc.vbr_bits_off_target,
3178         cpi->rc.vbr_bits_off_target_fast,
3179         cpi->twopass.extend_minq,
3180         cpi->twopass.extend_minq_fast,
3181         cpi->rc.total_target_vs_actual,
3182         (cpi->rc.starting_buffer_level - cpi->rc.bits_off_target),
3183         cpi->rc.total_actual_bits, cm->base_qindex,
3184         vp9_convert_qindex_to_q(cm->base_qindex, cm->bit_depth),
3185         (double)vp9_dc_quant(cm->base_qindex, 0, cm->bit_depth) /
3186             dc_quant_devisor,
3187         vp9_convert_qindex_to_q(cpi->twopass.active_worst_quality,
3188                                 cm->bit_depth),
3189         cpi->rc.avg_q,
3190         vp9_convert_qindex_to_q(cpi->oxcf.cq_level, cm->bit_depth),
3191         cpi->refresh_last_frame, cpi->refresh_golden_frame,
3192         cpi->refresh_alt_ref_frame, cm->frame_type, cpi->rc.gfu_boost,
3193         cpi->twopass.bits_left,
3194         cpi->twopass.total_left_stats.coded_error,
3195         cpi->twopass.bits_left /
3196             (1 + cpi->twopass.total_left_stats.coded_error),
3197         cpi->tot_recode_hits, recon_err, cpi->rc.kf_boost,
3198         cpi->twopass.kf_zeromotion_pct,
3199         cpi->twopass.fr_content_type,
3200         cm->lf.filter_level,
3201         cm->seg.aq_av_offset);
3202   }
3203   fclose(f);
3204
3205   if (0) {
3206     FILE *const fmodes = fopen("Modes.stt", "a");
3207     int i;
3208
3209     fprintf(fmodes, "%6d:%1d:%1d:%1d ", cpi->common.current_video_frame,
3210             cm->frame_type, cpi->refresh_golden_frame,
3211             cpi->refresh_alt_ref_frame);
3212
3213     for (i = 0; i < MAX_MODES; ++i)
3214       fprintf(fmodes, "%5d ", cpi->mode_chosen_counts[i]);
3215
3216     fprintf(fmodes, "\n");
3217
3218     fclose(fmodes);
3219   }
3220 }
3221 #endif
3222
3223 static void set_mv_search_params(VP9_COMP *cpi) {
3224   const VP9_COMMON *const cm = &cpi->common;
3225   const unsigned int max_mv_def = VPXMIN(cm->width, cm->height);
3226
3227   // Default based on max resolution.
3228   cpi->mv_step_param = vp9_init_search_range(max_mv_def);
3229
3230   if (cpi->sf.mv.auto_mv_step_size) {
3231     if (frame_is_intra_only(cm)) {
3232       // Initialize max_mv_magnitude for use in the first INTER frame
3233       // after a key/intra-only frame.
3234       cpi->max_mv_magnitude = max_mv_def;
3235     } else {
3236       if (cm->show_frame) {
3237         // Allow mv_steps to correspond to twice the max mv magnitude found
3238         // in the previous frame, capped by the default max_mv_magnitude based
3239         // on resolution.
3240         cpi->mv_step_param = vp9_init_search_range(
3241             VPXMIN(max_mv_def, 2 * cpi->max_mv_magnitude));
3242       }
3243       cpi->max_mv_magnitude = 0;
3244     }
3245   }
3246 }
3247
3248 static void set_size_independent_vars(VP9_COMP *cpi) {
3249   vp9_set_speed_features_framesize_independent(cpi);
3250   vp9_set_rd_speed_thresholds(cpi);
3251   vp9_set_rd_speed_thresholds_sub8x8(cpi);
3252   cpi->common.interp_filter = cpi->sf.default_interp_filter;
3253 }
3254
3255 static void set_size_dependent_vars(VP9_COMP *cpi, int *q, int *bottom_index,
3256                                     int *top_index) {
3257   VP9_COMMON *const cm = &cpi->common;
3258
3259   // Setup variables that depend on the dimensions of the frame.
3260   vp9_set_speed_features_framesize_dependent(cpi);
3261
3262   // Decide q and q bounds.
3263   *q = vp9_rc_pick_q_and_bounds(cpi, bottom_index, top_index);
3264
3265   if (!frame_is_intra_only(cm)) {
3266     vp9_set_high_precision_mv(cpi, (*q) < HIGH_PRECISION_MV_QTHRESH);
3267   }
3268
3269 #if !CONFIG_REALTIME_ONLY
3270   // Configure experimental use of segmentation for enhanced coding of
3271   // static regions if indicated.
3272   // Only allowed in the second pass of a two pass encode, as it requires
3273   // lagged coding, and if the relevant speed feature flag is set.
3274   if (cpi->oxcf.pass == 2 && cpi->sf.static_segmentation)
3275     configure_static_seg_features(cpi);
3276 #endif  // !CONFIG_REALTIME_ONLY
3277
3278 #if CONFIG_VP9_POSTPROC && !(CONFIG_VP9_TEMPORAL_DENOISING)
3279   if (cpi->oxcf.noise_sensitivity > 0) {
3280     int l = 0;
3281     switch (cpi->oxcf.noise_sensitivity) {
3282       case 1: l = 20; break;
3283       case 2: l = 40; break;
3284       case 3: l = 60; break;
3285       case 4:
3286       case 5: l = 100; break;
3287       case 6: l = 150; break;
3288     }
3289     if (!cpi->common.postproc_state.limits) {
3290       cpi->common.postproc_state.limits =
3291           vpx_calloc(cpi->un_scaled_source->y_width,
3292                      sizeof(*cpi->common.postproc_state.limits));
3293     }
3294     vp9_denoise(cpi->Source, cpi->Source, l, cpi->common.postproc_state.limits);
3295   }
3296 #endif  // CONFIG_VP9_POSTPROC
3297 }
3298
3299 #if CONFIG_VP9_TEMPORAL_DENOISING
3300 static void setup_denoiser_buffer(VP9_COMP *cpi) {
3301   VP9_COMMON *const cm = &cpi->common;
3302   if (cpi->oxcf.noise_sensitivity > 0 &&
3303       !cpi->denoiser.frame_buffer_initialized) {
3304     if (vp9_denoiser_alloc(cm, cpi->use_svc, &cpi->denoiser, cm->width,
3305                            cm->height, cm->subsampling_x, cm->subsampling_y,
3306 #if CONFIG_VP9_HIGHBITDEPTH
3307                            cm->use_highbitdepth,
3308 #endif
3309                            VP9_ENC_BORDER_IN_PIXELS))
3310       vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
3311                          "Failed to allocate denoiser");
3312   }
3313 }
3314 #endif
3315
3316 static void init_motion_estimation(VP9_COMP *cpi) {
3317   int y_stride = cpi->scaled_source.y_stride;
3318
3319   if (cpi->sf.mv.search_method == NSTEP) {
3320     vp9_init3smotion_compensation(&cpi->ss_cfg, y_stride);
3321   } else if (cpi->sf.mv.search_method == DIAMOND) {
3322     vp9_init_dsmotion_compensation(&cpi->ss_cfg, y_stride);
3323   }
3324 }
3325
3326 static void set_frame_size(VP9_COMP *cpi) {
3327   int ref_frame;
3328   VP9_COMMON *const cm = &cpi->common;
3329   VP9EncoderConfig *const oxcf = &cpi->oxcf;
3330   MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
3331
3332 #if !CONFIG_REALTIME_ONLY
3333   if (oxcf->pass == 2 && oxcf->rc_mode == VPX_VBR &&
3334       ((oxcf->resize_mode == RESIZE_FIXED && cm->current_video_frame == 0) ||
3335        (oxcf->resize_mode == RESIZE_DYNAMIC && cpi->resize_pending))) {
3336     calculate_coded_size(cpi, &oxcf->scaled_frame_width,
3337                          &oxcf->scaled_frame_height);
3338
3339     // There has been a change in frame size.
3340     vp9_set_size_literal(cpi, oxcf->scaled_frame_width,
3341                          oxcf->scaled_frame_height);
3342   }
3343 #endif  // !CONFIG_REALTIME_ONLY
3344
3345   if (oxcf->pass == 0 && oxcf->rc_mode == VPX_CBR && !cpi->use_svc &&
3346       oxcf->resize_mode == RESIZE_DYNAMIC && cpi->resize_pending != 0) {
3347     oxcf->scaled_frame_width =
3348         (oxcf->width * cpi->resize_scale_num) / cpi->resize_scale_den;
3349     oxcf->scaled_frame_height =
3350         (oxcf->height * cpi->resize_scale_num) / cpi->resize_scale_den;
3351     // There has been a change in frame size.
3352     vp9_set_size_literal(cpi, oxcf->scaled_frame_width,
3353                          oxcf->scaled_frame_height);
3354
3355     // TODO(agrange) Scale cpi->max_mv_magnitude if frame-size has changed.
3356     set_mv_search_params(cpi);
3357
3358     vp9_noise_estimate_init(&cpi->noise_estimate, cm->width, cm->height);
3359 #if CONFIG_VP9_TEMPORAL_DENOISING
3360     // Reset the denoiser on the resized frame.
3361     if (cpi->oxcf.noise_sensitivity > 0) {
3362       vp9_denoiser_free(&(cpi->denoiser));
3363       setup_denoiser_buffer(cpi);
3364       // Dynamic resize is only triggered for non-SVC, so we can force
3365       // golden frame update here as temporary fix to denoiser.
3366       cpi->refresh_golden_frame = 1;
3367     }
3368 #endif
3369   }
3370
3371   if ((oxcf->pass == 2) &&
3372       (!cpi->use_svc || (is_two_pass_svc(cpi) &&
3373                          cpi->svc.encode_empty_frame_state != ENCODING))) {
3374     vp9_set_target_rate(cpi);
3375   }
3376
3377   alloc_frame_mvs(cm, cm->new_fb_idx);
3378
3379   // Reset the frame pointers to the current frame size.
3380   if (vpx_realloc_frame_buffer(get_frame_new_buffer(cm), cm->width, cm->height,
3381                                cm->subsampling_x, cm->subsampling_y,
3382 #if CONFIG_VP9_HIGHBITDEPTH
3383                                cm->use_highbitdepth,
3384 #endif
3385                                VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment,
3386                                NULL, NULL, NULL))
3387     vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
3388                        "Failed to allocate frame buffer");
3389
3390   alloc_util_frame_buffers(cpi);
3391   init_motion_estimation(cpi);
3392
3393   for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
3394     RefBuffer *const ref_buf = &cm->frame_refs[ref_frame - 1];
3395     const int buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
3396
3397     ref_buf->idx = buf_idx;
3398
3399     if (buf_idx != INVALID_IDX) {
3400       YV12_BUFFER_CONFIG *const buf = &cm->buffer_pool->frame_bufs[buf_idx].buf;
3401       ref_buf->buf = buf;
3402 #if CONFIG_VP9_HIGHBITDEPTH
3403       vp9_setup_scale_factors_for_frame(
3404           &ref_buf->sf, buf->y_crop_width, buf->y_crop_height, cm->width,
3405           cm->height, (buf->flags & YV12_FLAG_HIGHBITDEPTH) ? 1 : 0);
3406 #else
3407       vp9_setup_scale_factors_for_frame(&ref_buf->sf, buf->y_crop_width,
3408                                         buf->y_crop_height, cm->width,
3409                                         cm->height);
3410 #endif  // CONFIG_VP9_HIGHBITDEPTH
3411       if (vp9_is_scaled(&ref_buf->sf)) vpx_extend_frame_borders(buf);
3412     } else {
3413       ref_buf->buf = NULL;
3414     }
3415   }
3416
3417   set_ref_ptrs(cm, xd, LAST_FRAME, LAST_FRAME);
3418 }
3419
3420 static void encode_without_recode_loop(VP9_COMP *cpi, size_t *size,
3421                                        uint8_t *dest) {
3422   VP9_COMMON *const cm = &cpi->common;
3423   int q = 0, bottom_index = 0, top_index = 0;  // Dummy variables.
3424   const INTERP_FILTER filter_scaler =
3425       (is_one_pass_cbr_svc(cpi))
3426           ? cpi->svc.downsample_filter_type[cpi->svc.spatial_layer_id]
3427           : EIGHTTAP;
3428   const int phase_scaler =
3429       (is_one_pass_cbr_svc(cpi))
3430           ? cpi->svc.downsample_filter_phase[cpi->svc.spatial_layer_id]
3431           : 0;
3432
3433   // Flag to check if its valid to compute the source sad (used for
3434   // scene detection and for superblock content state in CBR mode).
3435   // The flag may get reset below based on SVC or resizing state.
3436   cpi->compute_source_sad_onepass =
3437       cpi->oxcf.mode == REALTIME && cpi->oxcf.speed >= 5 && cm->show_frame;
3438
3439   vpx_clear_system_state();
3440
3441   set_frame_size(cpi);
3442
3443   if (is_one_pass_cbr_svc(cpi) &&
3444       cpi->un_scaled_source->y_width == cm->width << 2 &&
3445       cpi->un_scaled_source->y_height == cm->height << 2 &&
3446       cpi->svc.scaled_temp.y_width == cm->width << 1 &&
3447       cpi->svc.scaled_temp.y_height == cm->height << 1) {
3448     // For svc, if it is a 1/4x1/4 downscaling, do a two-stage scaling to take
3449     // advantage of the 1:2 optimized scaler. In the process, the 1/2x1/2
3450     // result will be saved in scaled_temp and might be used later.
3451     const INTERP_FILTER filter_scaler2 = cpi->svc.downsample_filter_type[1];
3452     const int phase_scaler2 = cpi->svc.downsample_filter_phase[1];
3453     cpi->Source = vp9_svc_twostage_scale(
3454         cm, cpi->un_scaled_source, &cpi->scaled_source, &cpi->svc.scaled_temp,
3455         filter_scaler, phase_scaler, filter_scaler2, phase_scaler2);
3456     cpi->svc.scaled_one_half = 1;
3457   } else if (is_one_pass_cbr_svc(cpi) &&
3458              cpi->un_scaled_source->y_width == cm->width << 1 &&
3459              cpi->un_scaled_source->y_height == cm->height << 1 &&
3460              cpi->svc.scaled_one_half) {
3461     // If the spatial layer is 1/2x1/2 and the scaling is already done in the
3462     // two-stage scaling, use the result directly.
3463     cpi->Source = &cpi->svc.scaled_temp;
3464     cpi->svc.scaled_one_half = 0;
3465   } else {
3466     cpi->Source = vp9_scale_if_required(
3467         cm, cpi->un_scaled_source, &cpi->scaled_source, (cpi->oxcf.pass == 0),
3468         filter_scaler, phase_scaler);
3469   }
3470   // Unfiltered raw source used in metrics calculation if the source
3471   // has been filtered.
3472   if (is_psnr_calc_enabled(cpi)) {
3473 #ifdef ENABLE_KF_DENOISE
3474     if (is_spatial_denoise_enabled(cpi)) {
3475       cpi->raw_source_frame = vp9_scale_if_required(
3476           cm, &cpi->raw_unscaled_source, &cpi->raw_scaled_source,
3477           (cpi->oxcf.pass == 0), EIGHTTAP, phase_scaler);
3478     } else {
3479       cpi->raw_source_frame = cpi->Source;
3480     }
3481 #else
3482     cpi->raw_source_frame = cpi->Source;
3483 #endif
3484   }
3485
3486   if ((cpi->use_svc &&
3487        (cpi->svc.spatial_layer_id < cpi->svc.number_spatial_layers - 1 ||
3488         cpi->svc.temporal_layer_id < cpi->svc.number_temporal_layers - 1 ||
3489         cpi->svc.current_superframe < 1)) ||
3490       cpi->resize_pending || cpi->resize_state || cpi->external_resize ||
3491       cpi->resize_state != ORIG) {
3492     cpi->compute_source_sad_onepass = 0;
3493     if (cpi->content_state_sb_fd != NULL)
3494       memset(cpi->content_state_sb_fd, 0,
3495              (cm->mi_stride >> 3) * ((cm->mi_rows >> 3) + 1) *
3496                  sizeof(*cpi->content_state_sb_fd));
3497   }
3498
3499   // Avoid scaling last_source unless its needed.
3500   // Last source is needed if avg_source_sad() is used, or if
3501   // partition_search_type == SOURCE_VAR_BASED_PARTITION, or if noise
3502   // estimation is enabled.
3503   if (cpi->unscaled_last_source != NULL &&
3504       (cpi->oxcf.content == VP9E_CONTENT_SCREEN ||
3505        (cpi->oxcf.pass == 0 && cpi->oxcf.rc_mode == VPX_VBR &&
3506         cpi->oxcf.mode == REALTIME && cpi->oxcf.speed >= 5) ||
3507        cpi->sf.partition_search_type == SOURCE_VAR_BASED_PARTITION ||
3508        (cpi->noise_estimate.enabled && !cpi->oxcf.noise_sensitivity) ||
3509        cpi->compute_source_sad_onepass))
3510     cpi->Last_Source = vp9_scale_if_required(
3511         cm, cpi->unscaled_last_source, &cpi->scaled_last_source,
3512         (cpi->oxcf.pass == 0), EIGHTTAP, 0);
3513
3514   if (cpi->Last_Source == NULL ||
3515       cpi->Last_Source->y_width != cpi->Source->y_width ||
3516       cpi->Last_Source->y_height != cpi->Source->y_height)
3517     cpi->compute_source_sad_onepass = 0;
3518
3519   if (cm->frame_type == KEY_FRAME || cpi->resize_pending != 0) {
3520     memset(cpi->consec_zero_mv, 0,
3521            cm->mi_rows * cm->mi_cols * sizeof(*cpi->consec_zero_mv));
3522   }
3523
3524   vp9_update_noise_estimate(cpi);
3525
3526   // Scene detection is always used for VBR mode or screen-content case.
3527   // For other cases (e.g., CBR mode) use it for 5 <= speed < 8 for now
3528   // (need to check encoding time cost for doing this for speed 8).
3529   if (cpi->compute_source_sad_onepass &&
3530       (cpi->oxcf.rc_mode == VPX_VBR ||
3531        cpi->oxcf.content == VP9E_CONTENT_SCREEN ||
3532        (cpi->oxcf.speed >= 5 && cpi->oxcf.speed < 8)))
3533     vp9_scene_detection_onepass(cpi);
3534
3535   // For 1 pass CBR SVC, only ZEROMV is allowed for spatial reference frame
3536   // when svc->force_zero_mode_spatial_ref = 1. Under those conditions we can
3537   // avoid this frame-level upsampling (for non intra_only frames).
3538   if (frame_is_intra_only(cm) == 0 &&
3539       !(is_one_pass_cbr_svc(cpi) && cpi->svc.force_zero_mode_spatial_ref)) {
3540     vp9_scale_references(cpi);
3541   }
3542
3543   set_size_independent_vars(cpi);
3544   set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
3545
3546   if (cpi->sf.copy_partition_flag) alloc_copy_partition_data(cpi);
3547
3548   if (cpi->oxcf.speed >= 5 && cpi->oxcf.pass == 0 &&
3549       cpi->oxcf.rc_mode == VPX_CBR &&
3550       cpi->oxcf.content != VP9E_CONTENT_SCREEN &&
3551       cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) {
3552     cpi->use_skin_detection = 1;
3553   }
3554
3555   vp9_set_quantizer(cm, q);
3556   vp9_set_variance_partition_thresholds(cpi, q, 0);
3557
3558   setup_frame(cpi);
3559
3560   suppress_active_map(cpi);
3561
3562   // Variance adaptive and in frame q adjustment experiments are mutually
3563   // exclusive.
3564   if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
3565     vp9_vaq_frame_setup(cpi);
3566   } else if (cpi->oxcf.aq_mode == EQUATOR360_AQ) {
3567     vp9_360aq_frame_setup(cpi);
3568   } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
3569     vp9_setup_in_frame_q_adj(cpi);
3570   } else if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) {
3571     vp9_cyclic_refresh_setup(cpi);
3572   } else if (cpi->oxcf.aq_mode == LOOKAHEAD_AQ) {
3573     // it may be pretty bad for rate-control,
3574     // and I should handle it somehow
3575     vp9_alt_ref_aq_setup_map(cpi->alt_ref_aq, cpi);
3576   }
3577
3578   apply_active_map(cpi);
3579
3580   vp9_encode_frame(cpi);
3581
3582   // Check if we should drop this frame because of high overshoot.
3583   // Only for frames where high temporal-source SAD is detected.
3584   if (cpi->oxcf.pass == 0 && cpi->oxcf.rc_mode == VPX_CBR &&
3585       cpi->resize_state == ORIG && cm->frame_type != KEY_FRAME &&
3586       cpi->oxcf.content == VP9E_CONTENT_SCREEN &&
3587       cpi->rc.high_source_sad == 1) {
3588     int frame_size = 0;
3589     // Get an estimate of the encoded frame size.
3590     save_coding_context(cpi);
3591     vp9_pack_bitstream(cpi, dest, size);
3592     restore_coding_context(cpi);
3593     frame_size = (int)(*size) << 3;
3594     // Check if encoded frame will overshoot too much, and if so, set the q and
3595     // adjust some rate control parameters, and return to re-encode the frame.
3596     if (vp9_encodedframe_overshoot(cpi, frame_size, &q)) {
3597       vpx_clear_system_state();
3598       vp9_set_quantizer(cm, q);
3599       vp9_set_variance_partition_thresholds(cpi, q, 0);
3600       suppress_active_map(cpi);
3601       // Turn-off cyclic refresh for re-encoded frame.
3602       if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) {
3603         unsigned char *const seg_map = cpi->segmentation_map;
3604         memset(seg_map, 0, cm->mi_rows * cm->mi_cols);
3605         vp9_disable_segmentation(&cm->seg);
3606       }
3607       apply_active_map(cpi);
3608       vp9_encode_frame(cpi);
3609     }
3610   }
3611
3612   // Update some stats from cyclic refresh, and check for golden frame update.
3613   if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->seg.enabled &&
3614       cm->frame_type != KEY_FRAME)
3615     vp9_cyclic_refresh_postencode(cpi);
3616
3617   // Update the skip mb flag probabilities based on the distribution
3618   // seen in the last encoder iteration.
3619   // update_base_skip_probs(cpi);
3620   vpx_clear_system_state();
3621 }
3622
3623 #define MAX_QSTEP_ADJ 4
3624 static int get_qstep_adj(int rate_excess, int rate_limit) {
3625   int qstep =
3626       rate_limit ? ((rate_excess + rate_limit / 2) / rate_limit) : INT_MAX;
3627   return VPXMIN(qstep, MAX_QSTEP_ADJ);
3628 }
3629
3630 static void encode_with_recode_loop(VP9_COMP *cpi, size_t *size,
3631                                     uint8_t *dest) {
3632   VP9_COMMON *const cm = &cpi->common;
3633   RATE_CONTROL *const rc = &cpi->rc;
3634   int bottom_index, top_index;
3635   int loop_count = 0;
3636   int loop_at_this_size = 0;
3637   int loop = 0;
3638   int overshoot_seen = 0;
3639   int undershoot_seen = 0;
3640   int frame_over_shoot_limit;
3641   int frame_under_shoot_limit;
3642   int q = 0, q_low = 0, q_high = 0;
3643   int enable_acl;
3644 #ifdef AGGRESSIVE_VBR
3645   int qrange_adj = 1;
3646 #endif
3647
3648   set_size_independent_vars(cpi);
3649
3650   enable_acl = cpi->sf.allow_acl
3651                    ? (cm->frame_type == KEY_FRAME) || (cm->show_frame == 0)
3652                    : 0;
3653
3654   do {
3655     vpx_clear_system_state();
3656
3657     set_frame_size(cpi);
3658
3659     if (loop_count == 0 || cpi->resize_pending != 0) {
3660       set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
3661
3662 #ifdef AGGRESSIVE_VBR
3663       if (two_pass_first_group_inter(cpi)) {
3664         // Adjustment limits for min and max q
3665         qrange_adj = VPXMAX(1, (top_index - bottom_index) / 2);
3666
3667         bottom_index =
3668             VPXMAX(bottom_index - qrange_adj / 2, cpi->oxcf.best_allowed_q);
3669         top_index =
3670             VPXMIN(cpi->oxcf.worst_allowed_q, top_index + qrange_adj / 2);
3671       }
3672 #endif
3673       // TODO(agrange) Scale cpi->max_mv_magnitude if frame-size has changed.
3674       set_mv_search_params(cpi);
3675
3676       // Reset the loop state for new frame size.
3677       overshoot_seen = 0;
3678       undershoot_seen = 0;
3679
3680       // Reconfiguration for change in frame size has concluded.
3681       cpi->resize_pending = 0;
3682
3683       q_low = bottom_index;
3684       q_high = top_index;
3685
3686       loop_at_this_size = 0;
3687     }
3688
3689     // Decide frame size bounds first time through.
3690     if (loop_count == 0) {
3691       vp9_rc_compute_frame_size_bounds(cpi, rc->this_frame_target,
3692                                        &frame_under_shoot_limit,
3693                                        &frame_over_shoot_limit);
3694     }
3695
3696     cpi->Source =
3697         vp9_scale_if_required(cm, cpi->un_scaled_source, &cpi->scaled_source,
3698                               (cpi->oxcf.pass == 0), EIGHTTAP, 0);
3699
3700     // Unfiltered raw source used in metrics calculation if the source
3701     // has been filtered.
3702     if (is_psnr_calc_enabled(cpi)) {
3703 #ifdef ENABLE_KF_DENOISE
3704       if (is_spatial_denoise_enabled(cpi)) {
3705         cpi->raw_source_frame = vp9_scale_if_required(
3706             cm, &cpi->raw_unscaled_source, &cpi->raw_scaled_source,
3707             (cpi->oxcf.pass == 0), EIGHTTAP, 0);
3708       } else {
3709         cpi->raw_source_frame = cpi->Source;
3710       }
3711 #else
3712       cpi->raw_source_frame = cpi->Source;
3713 #endif
3714     }
3715
3716     if (cpi->unscaled_last_source != NULL)
3717       cpi->Last_Source = vp9_scale_if_required(
3718           cm, cpi->unscaled_last_source, &cpi->scaled_last_source,
3719           (cpi->oxcf.pass == 0), EIGHTTAP, 0);
3720
3721     if (frame_is_intra_only(cm) == 0) {
3722       if (loop_count > 0) {
3723         release_scaled_references(cpi);
3724       }
3725       vp9_scale_references(cpi);
3726     }
3727
3728     vp9_set_quantizer(cm, q);
3729
3730     if (loop_count == 0) setup_frame(cpi);
3731
3732     // Variance adaptive and in frame q adjustment experiments are mutually
3733     // exclusive.
3734     if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
3735       vp9_vaq_frame_setup(cpi);
3736     } else if (cpi->oxcf.aq_mode == EQUATOR360_AQ) {
3737       vp9_360aq_frame_setup(cpi);
3738     } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
3739       vp9_setup_in_frame_q_adj(cpi);
3740     } else if (cpi->oxcf.aq_mode == LOOKAHEAD_AQ) {
3741       vp9_alt_ref_aq_setup_map(cpi->alt_ref_aq, cpi);
3742     }
3743
3744     vp9_encode_frame(cpi);
3745
3746     // Update the skip mb flag probabilities based on the distribution
3747     // seen in the last encoder iteration.
3748     // update_base_skip_probs(cpi);
3749
3750     vpx_clear_system_state();
3751
3752     // Dummy pack of the bitstream using up to date stats to get an
3753     // accurate estimate of output frame size to determine if we need
3754     // to recode.
3755     if (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF) {
3756       save_coding_context(cpi);
3757       if (!cpi->sf.use_nonrd_pick_mode) vp9_pack_bitstream(cpi, dest, size);
3758
3759       rc->projected_frame_size = (int)(*size) << 3;
3760
3761       if (frame_over_shoot_limit == 0) frame_over_shoot_limit = 1;
3762     }
3763
3764     if (cpi->oxcf.rc_mode == VPX_Q) {
3765       loop = 0;
3766     } else {
3767       if ((cm->frame_type == KEY_FRAME) && rc->this_key_frame_forced &&
3768           (rc->projected_frame_size < rc->max_frame_bandwidth)) {
3769         int last_q = q;
3770         int64_t kf_err;
3771
3772         int64_t high_err_target = cpi->ambient_err;
3773         int64_t low_err_target = cpi->ambient_err >> 1;
3774
3775 #if CONFIG_VP9_HIGHBITDEPTH
3776         if (cm->use_highbitdepth) {
3777           kf_err = vpx_highbd_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
3778         } else {
3779           kf_err = vpx_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
3780         }
3781 #else
3782         kf_err = vpx_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
3783 #endif  // CONFIG_VP9_HIGHBITDEPTH
3784
3785         // Prevent possible divide by zero error below for perfect KF
3786         kf_err += !kf_err;
3787
3788         // The key frame is not good enough or we can afford
3789         // to make it better without undue risk of popping.
3790         if ((kf_err > high_err_target &&
3791              rc->projected_frame_size <= frame_over_shoot_limit) ||
3792             (kf_err > low_err_target &&
3793              rc->projected_frame_size <= frame_under_shoot_limit)) {
3794           // Lower q_high
3795           q_high = q > q_low ? q - 1 : q_low;
3796
3797           // Adjust Q
3798           q = (int)((q * high_err_target) / kf_err);
3799           q = VPXMIN(q, (q_high + q_low) >> 1);
3800         } else if (kf_err < low_err_target &&
3801                    rc->projected_frame_size >= frame_under_shoot_limit) {
3802           // The key frame is much better than the previous frame
3803           // Raise q_low
3804           q_low = q < q_high ? q + 1 : q_high;
3805
3806           // Adjust Q
3807           q = (int)((q * low_err_target) / kf_err);
3808           q = VPXMIN(q, (q_high + q_low + 1) >> 1);
3809         }
3810
3811         // Clamp Q to upper and lower limits:
3812         q = clamp(q, q_low, q_high);
3813
3814         loop = q != last_q;
3815       } else if (recode_loop_test(cpi, frame_over_shoot_limit,
3816                                   frame_under_shoot_limit, q,
3817                                   VPXMAX(q_high, top_index), bottom_index)) {
3818         // Is the projected frame size out of range and are we allowed
3819         // to attempt to recode.
3820         int last_q = q;
3821         int retries = 0;
3822         int qstep;
3823
3824         if (cpi->resize_pending == 1) {
3825           // Change in frame size so go back around the recode loop.
3826           cpi->rc.frame_size_selector =
3827               SCALE_STEP1 - cpi->rc.frame_size_selector;
3828           cpi->rc.next_frame_size_selector = cpi->rc.frame_size_selector;
3829
3830 #if CONFIG_INTERNAL_STATS
3831           ++cpi->tot_recode_hits;
3832 #endif
3833           ++loop_count;
3834           loop = 1;
3835           continue;
3836         }
3837
3838         // Frame size out of permitted range:
3839         // Update correction factor & compute new Q to try...
3840
3841         // Frame is too large
3842         if (rc->projected_frame_size > rc->this_frame_target) {
3843           // Special case if the projected size is > the max allowed.
3844           if ((q == q_high) &&
3845               ((rc->projected_frame_size >= rc->max_frame_bandwidth) ||
3846                (rc->projected_frame_size >=
3847                 big_rate_miss_high_threshold(cpi)))) {
3848             int max_rate = VPXMAX(1, VPXMIN(rc->max_frame_bandwidth,
3849                                             big_rate_miss_high_threshold(cpi)));
3850             double q_val_high;
3851             q_val_high = vp9_convert_qindex_to_q(q_high, cm->bit_depth);
3852             q_val_high =
3853                 q_val_high * ((double)rc->projected_frame_size / max_rate);
3854             q_high = vp9_convert_q_to_qindex(q_val_high, cm->bit_depth);
3855             q_high = clamp(q_high, rc->best_quality, rc->worst_quality);
3856           }
3857
3858           // Raise Qlow as to at least the current value
3859           qstep =
3860               get_qstep_adj(rc->projected_frame_size, rc->this_frame_target);
3861           q_low = VPXMIN(q + qstep, q_high);
3862
3863           if (undershoot_seen || loop_at_this_size > 1) {
3864             // Update rate_correction_factor unless
3865             vp9_rc_update_rate_correction_factors(cpi);
3866
3867             q = (q_high + q_low + 1) / 2;
3868           } else {
3869             // Update rate_correction_factor unless
3870             vp9_rc_update_rate_correction_factors(cpi);
3871
3872             q = vp9_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
3873                                   VPXMAX(q_high, top_index));
3874
3875             while (q < q_low && retries < 10) {
3876               vp9_rc_update_rate_correction_factors(cpi);
3877               q = vp9_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
3878                                     VPXMAX(q_high, top_index));
3879               retries++;
3880             }
3881           }
3882
3883           overshoot_seen = 1;
3884         } else {
3885           // Frame is too small
3886           qstep =
3887               get_qstep_adj(rc->this_frame_target, rc->projected_frame_size);
3888           q_high = VPXMAX(q - qstep, q_low);
3889
3890           if (overshoot_seen || loop_at_this_size > 1) {
3891             vp9_rc_update_rate_correction_factors(cpi);
3892             q = (q_high + q_low) / 2;
3893           } else {
3894             vp9_rc_update_rate_correction_factors(cpi);
3895             q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
3896                                   VPXMIN(q_low, bottom_index), top_index);
3897             // Special case reset for qlow for constrained quality.
3898             // This should only trigger where there is very substantial
3899             // undershoot on a frame and the auto cq level is above
3900             // the user passsed in value.
3901             if (cpi->oxcf.rc_mode == VPX_CQ && q < q_low) {
3902               q_low = q;
3903             }
3904
3905             while (q > q_high && retries < 10) {
3906               vp9_rc_update_rate_correction_factors(cpi);
3907               q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
3908                                     VPXMIN(q_low, bottom_index), top_index);
3909               retries++;
3910             }
3911           }
3912           undershoot_seen = 1;
3913         }
3914
3915         // Clamp Q to upper and lower limits:
3916         q = clamp(q, q_low, q_high);
3917
3918         loop = (q != last_q);
3919       } else {
3920         loop = 0;
3921       }
3922     }
3923
3924     // Special case for overlay frame.
3925     if (rc->is_src_frame_alt_ref &&
3926         rc->projected_frame_size < rc->max_frame_bandwidth)
3927       loop = 0;
3928
3929     if (loop) {
3930       ++loop_count;
3931       ++loop_at_this_size;
3932
3933 #if CONFIG_INTERNAL_STATS
3934       ++cpi->tot_recode_hits;
3935 #endif
3936     }
3937
3938     if (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF)
3939       if (loop || !enable_acl) restore_coding_context(cpi);
3940   } while (loop);
3941
3942 #ifdef AGGRESSIVE_VBR
3943   if (two_pass_first_group_inter(cpi)) {
3944     cpi->twopass.active_worst_quality =
3945         VPXMIN(q + qrange_adj, cpi->oxcf.worst_allowed_q);
3946   } else if (!frame_is_kf_gf_arf(cpi)) {
3947 #else
3948   if (!frame_is_kf_gf_arf(cpi)) {
3949 #endif
3950     // Have we been forced to adapt Q outside the expected range by an extreme
3951     // rate miss. If so adjust the active maxQ for the subsequent frames.
3952     if (q > cpi->twopass.active_worst_quality) {
3953       cpi->twopass.active_worst_quality = q;
3954     }
3955   }
3956
3957   if (enable_acl) {
3958     // Skip recoding, if model diff is below threshold
3959     const int thresh = compute_context_model_thresh(cpi);
3960     const int diff = compute_context_model_diff(cm);
3961     if (diff < thresh) {
3962       vpx_clear_system_state();
3963       restore_coding_context(cpi);
3964       return;
3965     }
3966
3967     vp9_encode_frame(cpi);
3968     vpx_clear_system_state();
3969     restore_coding_context(cpi);
3970     vp9_pack_bitstream(cpi, dest, size);
3971
3972     vp9_encode_frame(cpi);
3973     vpx_clear_system_state();
3974
3975     restore_coding_context(cpi);
3976   }
3977 }
3978
3979 static int get_ref_frame_flags(const VP9_COMP *cpi) {
3980   const int *const map = cpi->common.ref_frame_map;
3981   const int gold_is_last = map[cpi->gld_fb_idx] == map[cpi->lst_fb_idx];
3982   const int alt_is_last = map[cpi->alt_fb_idx] == map[cpi->lst_fb_idx];
3983   const int gold_is_alt = map[cpi->gld_fb_idx] == map[cpi->alt_fb_idx];
3984   int flags = VP9_ALT_FLAG | VP9_GOLD_FLAG | VP9_LAST_FLAG;
3985
3986   if (gold_is_last) flags &= ~VP9_GOLD_FLAG;
3987
3988   if (cpi->rc.frames_till_gf_update_due == INT_MAX &&
3989       (cpi->svc.number_temporal_layers == 1 &&
3990        cpi->svc.number_spatial_layers == 1))
3991     flags &= ~VP9_GOLD_FLAG;
3992
3993   if (alt_is_last) flags &= ~VP9_ALT_FLAG;
3994
3995   if (gold_is_alt) flags &= ~VP9_ALT_FLAG;
3996
3997   return flags;
3998 }
3999
4000 static void set_ext_overrides(VP9_COMP *cpi) {
4001   // Overrides the defaults with the externally supplied values with
4002   // vp9_update_reference() and vp9_update_entropy() calls
4003   // Note: The overrides are valid only for the next frame passed
4004   // to encode_frame_to_data_rate() function
4005   if (cpi->ext_refresh_frame_context_pending) {
4006     cpi->common.refresh_frame_context = cpi->ext_refresh_frame_context;
4007     cpi->ext_refresh_frame_context_pending = 0;
4008   }
4009   if (cpi->ext_refresh_frame_flags_pending) {
4010     cpi->refresh_last_frame = cpi->ext_refresh_last_frame;
4011     cpi->refresh_golden_frame = cpi->ext_refresh_golden_frame;
4012     cpi->refresh_alt_ref_frame = cpi->ext_refresh_alt_ref_frame;
4013   }
4014 }
4015
4016 YV12_BUFFER_CONFIG *vp9_svc_twostage_scale(
4017     VP9_COMMON *cm, YV12_BUFFER_CONFIG *unscaled, YV12_BUFFER_CONFIG *scaled,
4018     YV12_BUFFER_CONFIG *scaled_temp, INTERP_FILTER filter_type,
4019     int phase_scaler, INTERP_FILTER filter_type2, int phase_scaler2) {
4020   if (cm->mi_cols * MI_SIZE != unscaled->y_width ||
4021       cm->mi_rows * MI_SIZE != unscaled->y_height) {
4022 #if CONFIG_VP9_HIGHBITDEPTH
4023     if (cm->bit_depth == VPX_BITS_8) {
4024       vp9_scale_and_extend_frame(unscaled, scaled_temp, filter_type2,
4025                                  phase_scaler2);
4026       vp9_scale_and_extend_frame(scaled_temp, scaled, filter_type,
4027                                  phase_scaler);
4028     } else {
4029       scale_and_extend_frame(unscaled, scaled_temp, (int)cm->bit_depth,
4030                              filter_type2, phase_scaler2);
4031       scale_and_extend_frame(scaled_temp, scaled, (int)cm->bit_depth,
4032                              filter_type, phase_scaler);
4033     }
4034 #else
4035     vp9_scale_and_extend_frame(unscaled, scaled_temp, filter_type2,
4036                                phase_scaler2);
4037     vp9_scale_and_extend_frame(scaled_temp, scaled, filter_type, phase_scaler);
4038 #endif  // CONFIG_VP9_HIGHBITDEPTH
4039     return scaled;
4040   } else {
4041     return unscaled;
4042   }
4043 }
4044
4045 YV12_BUFFER_CONFIG *vp9_scale_if_required(
4046     VP9_COMMON *cm, YV12_BUFFER_CONFIG *unscaled, YV12_BUFFER_CONFIG *scaled,
4047     int use_normative_scaler, INTERP_FILTER filter_type, int phase_scaler) {
4048   if (cm->mi_cols * MI_SIZE != unscaled->y_width ||
4049       cm->mi_rows * MI_SIZE != unscaled->y_height) {
4050 #if CONFIG_VP9_HIGHBITDEPTH
4051     if (use_normative_scaler && unscaled->y_width <= (scaled->y_width << 1) &&
4052         unscaled->y_height <= (scaled->y_height << 1))
4053       if (cm->bit_depth == VPX_BITS_8)
4054         vp9_scale_and_extend_frame(unscaled, scaled, filter_type, phase_scaler);
4055       else
4056         scale_and_extend_frame(unscaled, scaled, (int)cm->bit_depth,
4057                                filter_type, phase_scaler);
4058     else
4059       scale_and_extend_frame_nonnormative(unscaled, scaled, (int)cm->bit_depth);
4060 #else
4061     if (use_normative_scaler && unscaled->y_width <= (scaled->y_width << 1) &&
4062         unscaled->y_height <= (scaled->y_height << 1))
4063       vp9_scale_and_extend_frame(unscaled, scaled, filter_type, phase_scaler);
4064     else
4065       scale_and_extend_frame_nonnormative(unscaled, scaled);
4066 #endif  // CONFIG_VP9_HIGHBITDEPTH
4067     return scaled;
4068   } else {
4069     return unscaled;
4070   }
4071 }
4072
4073 static void set_arf_sign_bias(VP9_COMP *cpi) {
4074   VP9_COMMON *const cm = &cpi->common;
4075   int arf_sign_bias;
4076
4077   if ((cpi->oxcf.pass == 2) && cpi->multi_arf_allowed) {
4078     const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
4079     arf_sign_bias = cpi->rc.source_alt_ref_active &&
4080                     (!cpi->refresh_alt_ref_frame ||
4081                      (gf_group->rf_level[gf_group->index] == GF_ARF_LOW));
4082   } else {
4083     arf_sign_bias =
4084         (cpi->rc.source_alt_ref_active && !cpi->refresh_alt_ref_frame);
4085   }
4086   cm->ref_frame_sign_bias[ALTREF_FRAME] = arf_sign_bias;
4087 }
4088
4089 static int setup_interp_filter_search_mask(VP9_COMP *cpi) {
4090   INTERP_FILTER ifilter;
4091   int ref_total[MAX_REF_FRAMES] = { 0 };
4092   MV_REFERENCE_FRAME ref;
4093   int mask = 0;
4094   if (cpi->common.last_frame_type == KEY_FRAME || cpi->refresh_alt_ref_frame)
4095     return mask;
4096   for (ref = LAST_FRAME; ref <= ALTREF_FRAME; ++ref)
4097     for (ifilter = EIGHTTAP; ifilter <= EIGHTTAP_SHARP; ++ifilter)
4098       ref_total[ref] += cpi->interp_filter_selected[ref][ifilter];
4099
4100   for (ifilter = EIGHTTAP; ifilter <= EIGHTTAP_SHARP; ++ifilter) {
4101     if ((ref_total[LAST_FRAME] &&
4102          cpi->interp_filter_selected[LAST_FRAME][ifilter] == 0) &&
4103         (ref_total[GOLDEN_FRAME] == 0 ||
4104          cpi->interp_filter_selected[GOLDEN_FRAME][ifilter] * 50 <
4105              ref_total[GOLDEN_FRAME]) &&
4106         (ref_total[ALTREF_FRAME] == 0 ||
4107          cpi->interp_filter_selected[ALTREF_FRAME][ifilter] * 50 <
4108              ref_total[ALTREF_FRAME]))
4109       mask |= 1 << ifilter;
4110   }
4111   return mask;
4112 }
4113
4114 #ifdef ENABLE_KF_DENOISE
4115 // Baseline Kernal weights for denoise
4116 static uint8_t dn_kernal_3[9] = { 1, 2, 1, 2, 4, 2, 1, 2, 1 };
4117 static uint8_t dn_kernal_5[25] = { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 2, 4,
4118                                    2, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1 };
4119
4120 static INLINE void add_denoise_point(int centre_val, int data_val, int thresh,
4121                                      uint8_t point_weight, int *sum_val,
4122                                      int *sum_weight) {
4123   if (abs(centre_val - data_val) <= thresh) {
4124     *sum_weight += point_weight;
4125     *sum_val += (int)data_val * (int)point_weight;
4126   }
4127 }
4128
4129 static void spatial_denoise_point(uint8_t *src_ptr, const int stride,
4130                                   const int strength) {
4131   int sum_weight = 0;
4132   int sum_val = 0;
4133   int thresh = strength;
4134   int kernal_size = 5;
4135   int half_k_size = 2;
4136   int i, j;
4137   int max_diff = 0;
4138   uint8_t *tmp_ptr;
4139   uint8_t *kernal_ptr;
4140
4141   // Find the maximum deviation from the source point in the locale.
4142   tmp_ptr = src_ptr - (stride * (half_k_size + 1)) - (half_k_size + 1);
4143   for (i = 0; i < kernal_size + 2; ++i) {
4144     for (j = 0; j < kernal_size + 2; ++j) {
4145       max_diff = VPXMAX(max_diff, abs((int)*src_ptr - (int)tmp_ptr[j]));
4146     }
4147     tmp_ptr += stride;
4148   }
4149
4150   // Select the kernal size.
4151   if (max_diff > (strength + (strength >> 1))) {
4152     kernal_size = 3;
4153     half_k_size = 1;
4154     thresh = thresh >> 1;
4155   }
4156   kernal_ptr = (kernal_size == 3) ? dn_kernal_3 : dn_kernal_5;
4157
4158   // Apply the kernal
4159   tmp_ptr = src_ptr - (stride * half_k_size) - half_k_size;
4160   for (i = 0; i < kernal_size; ++i) {
4161     for (j = 0; j < kernal_size; ++j) {
4162       add_denoise_point((int)*src_ptr, (int)tmp_ptr[j], thresh, *kernal_ptr,
4163                         &sum_val, &sum_weight);
4164       ++kernal_ptr;
4165     }
4166     tmp_ptr += stride;
4167   }
4168
4169   // Update the source value with the new filtered value
4170   *src_ptr = (uint8_t)((sum_val + (sum_weight >> 1)) / sum_weight);
4171 }
4172
4173 #if CONFIG_VP9_HIGHBITDEPTH
4174 static void highbd_spatial_denoise_point(uint16_t *src_ptr, const int stride,
4175                                          const int strength) {
4176   int sum_weight = 0;
4177   int sum_val = 0;
4178   int thresh = strength;
4179   int kernal_size = 5;
4180   int half_k_size = 2;
4181   int i, j;
4182   int max_diff = 0;
4183   uint16_t *tmp_ptr;
4184   uint8_t *kernal_ptr;
4185
4186   // Find the maximum deviation from the source point in the locale.
4187   tmp_ptr = src_ptr - (stride * (half_k_size + 1)) - (half_k_size + 1);
4188   for (i = 0; i < kernal_size + 2; ++i) {
4189     for (j = 0; j < kernal_size + 2; ++j) {
4190       max_diff = VPXMAX(max_diff, abs((int)src_ptr - (int)tmp_ptr[j]));
4191     }
4192     tmp_ptr += stride;
4193   }
4194
4195   // Select the kernal size.
4196   if (max_diff > (strength + (strength >> 1))) {
4197     kernal_size = 3;
4198     half_k_size = 1;
4199     thresh = thresh >> 1;
4200   }
4201   kernal_ptr = (kernal_size == 3) ? dn_kernal_3 : dn_kernal_5;
4202
4203   // Apply the kernal
4204   tmp_ptr = src_ptr - (stride * half_k_size) - half_k_size;
4205   for (i = 0; i < kernal_size; ++i) {
4206     for (j = 0; j < kernal_size; ++j) {
4207       add_denoise_point((int)*src_ptr, (int)tmp_ptr[j], thresh, *kernal_ptr,
4208                         &sum_val, &sum_weight);
4209       ++kernal_ptr;
4210     }
4211     tmp_ptr += stride;
4212   }
4213
4214   // Update the source value with the new filtered value
4215   *src_ptr = (uint16_t)((sum_val + (sum_weight >> 1)) / sum_weight);
4216 }
4217 #endif  // CONFIG_VP9_HIGHBITDEPTH
4218
4219 // Apply thresholded spatial noise supression to a given buffer.
4220 static void spatial_denoise_buffer(VP9_COMP *cpi, uint8_t *buffer,
4221                                    const int stride, const int width,
4222                                    const int height, const int strength) {
4223   VP9_COMMON *const cm = &cpi->common;
4224   uint8_t *src_ptr = buffer;
4225   int row;
4226   int col;
4227
4228   for (row = 0; row < height; ++row) {
4229     for (col = 0; col < width; ++col) {
4230 #if CONFIG_VP9_HIGHBITDEPTH
4231       if (cm->use_highbitdepth)
4232         highbd_spatial_denoise_point(CONVERT_TO_SHORTPTR(&src_ptr[col]), stride,
4233                                      strength);
4234       else
4235         spatial_denoise_point(&src_ptr[col], stride, strength);
4236 #else
4237       spatial_denoise_point(&src_ptr[col], stride, strength);
4238 #endif  // CONFIG_VP9_HIGHBITDEPTH
4239     }
4240     src_ptr += stride;
4241   }
4242 }
4243
4244 // Apply thresholded spatial noise supression to source.
4245 static void spatial_denoise_frame(VP9_COMP *cpi) {
4246   YV12_BUFFER_CONFIG *src = cpi->Source;
4247   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
4248   TWO_PASS *const twopass = &cpi->twopass;
4249   VP9_COMMON *const cm = &cpi->common;
4250
4251   // Base the filter strength on the current active max Q.
4252   const int q = (int)(vp9_convert_qindex_to_q(twopass->active_worst_quality,
4253                                               cm->bit_depth));
4254   int strength =
4255       VPXMAX(oxcf->arnr_strength >> 2, VPXMIN(oxcf->arnr_strength, (q >> 4)));
4256
4257   // Denoise each of Y,U and V buffers.
4258   spatial_denoise_buffer(cpi, src->y_buffer, src->y_stride, src->y_width,
4259                          src->y_height, strength);
4260
4261   strength += (strength >> 1);
4262   spatial_denoise_buffer(cpi, src->u_buffer, src->uv_stride, src->uv_width,
4263                          src->uv_height, strength << 1);
4264
4265   spatial_denoise_buffer(cpi, src->v_buffer, src->uv_stride, src->uv_width,
4266                          src->uv_height, strength << 1);
4267 }
4268 #endif  // ENABLE_KF_DENOISE
4269
4270 static void vp9_try_disable_lookahead_aq(VP9_COMP *cpi, size_t *size,
4271                                          uint8_t *dest) {
4272   if (cpi->common.seg.enabled)
4273     if (ALT_REF_AQ_PROTECT_GAIN) {
4274       size_t nsize = *size;
4275       int overhead;
4276
4277       // TODO(yuryg): optimize this, as
4278       // we don't really need to repack
4279
4280       save_coding_context(cpi);
4281       vp9_disable_segmentation(&cpi->common.seg);
4282       vp9_pack_bitstream(cpi, dest, &nsize);
4283       restore_coding_context(cpi);
4284
4285       overhead = (int)*size - (int)nsize;
4286
4287       if (vp9_alt_ref_aq_disable_if(cpi->alt_ref_aq, overhead, (int)*size))
4288         vp9_encode_frame(cpi);
4289       else
4290         vp9_enable_segmentation(&cpi->common.seg);
4291     }
4292 }
4293
4294 static void encode_frame_to_data_rate(VP9_COMP *cpi, size_t *size,
4295                                       uint8_t *dest,
4296                                       unsigned int *frame_flags) {
4297   VP9_COMMON *const cm = &cpi->common;
4298   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
4299   struct segmentation *const seg = &cm->seg;
4300   TX_SIZE t;
4301
4302   set_ext_overrides(cpi);
4303   vpx_clear_system_state();
4304
4305 #ifdef ENABLE_KF_DENOISE
4306   // Spatial denoise of key frame.
4307   if (is_spatial_denoise_enabled(cpi)) spatial_denoise_frame(cpi);
4308 #endif
4309
4310   // Set the arf sign bias for this frame.
4311   set_arf_sign_bias(cpi);
4312
4313   // Set default state for segment based loop filter update flags.
4314   cm->lf.mode_ref_delta_update = 0;
4315
4316   if (cpi->oxcf.pass == 2 && cpi->sf.adaptive_interp_filter_search)
4317     cpi->sf.interp_filter_search_mask = setup_interp_filter_search_mask(cpi);
4318
4319   // Set various flags etc to special state if it is a key frame.
4320   if (frame_is_intra_only(cm)) {
4321     // Reset the loop filter deltas and segmentation map.
4322     vp9_reset_segment_features(&cm->seg);
4323
4324     // If segmentation is enabled force a map update for key frames.
4325     if (seg->enabled) {
4326       seg->update_map = 1;
4327       seg->update_data = 1;
4328     }
4329
4330     // The alternate reference frame cannot be active for a key frame.
4331     cpi->rc.source_alt_ref_active = 0;
4332
4333     cm->error_resilient_mode = oxcf->error_resilient_mode;
4334     cm->frame_parallel_decoding_mode = oxcf->frame_parallel_decoding_mode;
4335
4336     // By default, encoder assumes decoder can use prev_mi.
4337     if (cm->error_resilient_mode) {
4338       cm->frame_parallel_decoding_mode = 1;
4339       cm->reset_frame_context = 0;
4340       cm->refresh_frame_context = 0;
4341     } else if (cm->intra_only) {
4342       // Only reset the current context.
4343       cm->reset_frame_context = 2;
4344     }
4345   }
4346   if (is_two_pass_svc(cpi) && cm->error_resilient_mode == 0) {
4347     // Use context 0 for intra only empty frame, but the last frame context
4348     // for other empty frames.
4349     if (cpi->svc.encode_empty_frame_state == ENCODING) {
4350       if (cpi->svc.encode_intra_empty_frame != 0)
4351         cm->frame_context_idx = 0;
4352       else
4353         cm->frame_context_idx = FRAME_CONTEXTS - 1;
4354     } else {
4355       cm->frame_context_idx =
4356           cpi->svc.spatial_layer_id * cpi->svc.number_temporal_layers +
4357           cpi->svc.temporal_layer_id;
4358     }
4359
4360     cm->frame_parallel_decoding_mode = oxcf->frame_parallel_decoding_mode;
4361
4362     // The probs will be updated based on the frame type of its previous
4363     // frame if frame_parallel_decoding_mode is 0. The type may vary for
4364     // the frame after a key frame in base layer since we may drop enhancement
4365     // layers. So set frame_parallel_decoding_mode to 1 in this case.
4366     if (cm->frame_parallel_decoding_mode == 0) {
4367       if (cpi->svc.number_temporal_layers == 1) {
4368         if (cpi->svc.spatial_layer_id == 0 &&
4369             cpi->svc.layer_context[0].last_frame_type == KEY_FRAME)
4370           cm->frame_parallel_decoding_mode = 1;
4371       } else if (cpi->svc.spatial_layer_id == 0) {
4372         // Find the 2nd frame in temporal base layer and 1st frame in temporal
4373         // enhancement layers from the key frame.
4374         int i;
4375         for (i = 0; i < cpi->svc.number_temporal_layers; ++i) {
4376           if (cpi->svc.layer_context[0].frames_from_key_frame == 1 << i) {
4377             cm->frame_parallel_decoding_mode = 1;
4378             break;
4379           }
4380         }
4381       }
4382     }
4383   }
4384
4385   // For 1 pass CBR, check if we are dropping this frame.
4386   // For spatial layers, for now only check for frame-dropping on first spatial
4387   // layer, and if decision is to drop, we drop whole super-frame.
4388   if (oxcf->pass == 0 && oxcf->rc_mode == VPX_CBR &&
4389       cm->frame_type != KEY_FRAME) {
4390     if (vp9_rc_drop_frame(cpi) ||
4391         (is_one_pass_cbr_svc(cpi) && cpi->svc.rc_drop_superframe == 1)) {
4392       vp9_rc_postencode_update_drop_frame(cpi);
4393       ++cm->current_video_frame;
4394       cpi->ext_refresh_frame_flags_pending = 0;
4395       cpi->svc.rc_drop_superframe = 1;
4396       cpi->last_frame_dropped = 1;
4397       // TODO(marpan): Advancing the svc counters on dropped frames can break
4398       // the referencing scheme for the fixed svc patterns defined in
4399       // vp9_one_pass_cbr_svc_start_layer(). Look into fixing this issue, but
4400       // for now, don't advance the svc frame counters on dropped frame.
4401       // if (cpi->use_svc)
4402       //   vp9_inc_frame_in_layer(cpi);
4403
4404       return;
4405     }
4406   }
4407
4408   vpx_clear_system_state();
4409
4410 #if CONFIG_INTERNAL_STATS
4411   memset(cpi->mode_chosen_counts, 0,
4412          MAX_MODES * sizeof(*cpi->mode_chosen_counts));
4413 #endif
4414
4415   if (cpi->sf.recode_loop == DISALLOW_RECODE) {
4416     encode_without_recode_loop(cpi, size, dest);
4417   } else {
4418     encode_with_recode_loop(cpi, size, dest);
4419   }
4420
4421   cpi->last_frame_dropped = 0;
4422
4423   // Disable segmentation if it decrease rate/distortion ratio
4424   if (cpi->oxcf.aq_mode == LOOKAHEAD_AQ)
4425     vp9_try_disable_lookahead_aq(cpi, size, dest);
4426
4427 #if CONFIG_VP9_TEMPORAL_DENOISING
4428 #ifdef OUTPUT_YUV_DENOISED
4429   if (oxcf->noise_sensitivity > 0 && denoise_svc(cpi)) {
4430     vpx_write_yuv_frame(yuv_denoised_file,
4431                         &cpi->denoiser.running_avg_y[INTRA_FRAME]);
4432   }
4433 #endif
4434 #endif
4435 #ifdef OUTPUT_YUV_SKINMAP
4436   if (cpi->common.current_video_frame > 1) {
4437     vp9_output_skin_map(cpi, yuv_skinmap_file);
4438   }
4439 #endif
4440
4441   // Special case code to reduce pulsing when key frames are forced at a
4442   // fixed interval. Note the reconstruction error if it is the frame before
4443   // the force key frame
4444   if (cpi->rc.next_key_frame_forced && cpi->rc.frames_to_key == 1) {
4445 #if CONFIG_VP9_HIGHBITDEPTH
4446     if (cm->use_highbitdepth) {
4447       cpi->ambient_err =
4448           vpx_highbd_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
4449     } else {
4450       cpi->ambient_err = vpx_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
4451     }
4452 #else
4453     cpi->ambient_err = vpx_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
4454 #endif  // CONFIG_VP9_HIGHBITDEPTH
4455   }
4456
4457   // If the encoder forced a KEY_FRAME decision
4458   if (cm->frame_type == KEY_FRAME) cpi->refresh_last_frame = 1;
4459
4460   cm->frame_to_show = get_frame_new_buffer(cm);
4461   cm->frame_to_show->color_space = cm->color_space;
4462   cm->frame_to_show->color_range = cm->color_range;
4463   cm->frame_to_show->render_width = cm->render_width;
4464   cm->frame_to_show->render_height = cm->render_height;
4465
4466   // Pick the loop filter level for the frame.
4467   loopfilter_frame(cpi, cm);
4468
4469   // build the bitstream
4470   vp9_pack_bitstream(cpi, dest, size);
4471
4472   if (cm->seg.update_map) update_reference_segmentation_map(cpi);
4473
4474   if (frame_is_intra_only(cm) == 0) {
4475     release_scaled_references(cpi);
4476   }
4477   vp9_update_reference_frames(cpi);
4478
4479   for (t = TX_4X4; t <= TX_32X32; t++)
4480     full_to_model_counts(cpi->td.counts->coef[t],
4481                          cpi->td.rd_counts.coef_counts[t]);
4482
4483   if (!cm->error_resilient_mode && !cm->frame_parallel_decoding_mode)
4484     vp9_adapt_coef_probs(cm);
4485
4486   if (!frame_is_intra_only(cm)) {
4487     if (!cm->error_resilient_mode && !cm->frame_parallel_decoding_mode) {
4488       vp9_adapt_mode_probs(cm);
4489       vp9_adapt_mv_probs(cm, cm->allow_high_precision_mv);
4490     }
4491   }
4492
4493   cpi->ext_refresh_frame_flags_pending = 0;
4494
4495   if (cpi->refresh_golden_frame == 1)
4496     cpi->frame_flags |= FRAMEFLAGS_GOLDEN;
4497   else
4498     cpi->frame_flags &= ~FRAMEFLAGS_GOLDEN;
4499
4500   if (cpi->refresh_alt_ref_frame == 1)
4501     cpi->frame_flags |= FRAMEFLAGS_ALTREF;
4502   else
4503     cpi->frame_flags &= ~FRAMEFLAGS_ALTREF;
4504
4505   cpi->ref_frame_flags = get_ref_frame_flags(cpi);
4506
4507   cm->last_frame_type = cm->frame_type;
4508
4509   if (!(is_two_pass_svc(cpi) && cpi->svc.encode_empty_frame_state == ENCODING))
4510     vp9_rc_postencode_update(cpi, *size);
4511
4512 #if 0
4513   output_frame_level_debug_stats(cpi);
4514 #endif
4515
4516   if (cm->frame_type == KEY_FRAME) {
4517     // Tell the caller that the frame was coded as a key frame
4518     *frame_flags = cpi->frame_flags | FRAMEFLAGS_KEY;
4519   } else {
4520     *frame_flags = cpi->frame_flags & ~FRAMEFLAGS_KEY;
4521   }
4522
4523   // Clear the one shot update flags for segmentation map and mode/ref loop
4524   // filter deltas.
4525   cm->seg.update_map = 0;
4526   cm->seg.update_data = 0;
4527   cm->lf.mode_ref_delta_update = 0;
4528
4529   // keep track of the last coded dimensions
4530   cm->last_width = cm->width;
4531   cm->last_height = cm->height;
4532
4533   // reset to normal state now that we are done.
4534   if (!cm->show_existing_frame) cm->last_show_frame = cm->show_frame;
4535
4536   if (cm->show_frame) {
4537     vp9_swap_mi_and_prev_mi(cm);
4538     // Don't increment frame counters if this was an altref buffer
4539     // update not a real frame
4540     ++cm->current_video_frame;
4541     if (cpi->use_svc) vp9_inc_frame_in_layer(cpi);
4542   }
4543   cm->prev_frame = cm->cur_frame;
4544
4545   if (cpi->use_svc)
4546     cpi->svc
4547         .layer_context[cpi->svc.spatial_layer_id *
4548                            cpi->svc.number_temporal_layers +
4549                        cpi->svc.temporal_layer_id]
4550         .last_frame_type = cm->frame_type;
4551
4552   cpi->force_update_segmentation = 0;
4553
4554   if (cpi->oxcf.aq_mode == LOOKAHEAD_AQ)
4555     vp9_alt_ref_aq_unset_all(cpi->alt_ref_aq, cpi);
4556 }
4557
4558 static void SvcEncode(VP9_COMP *cpi, size_t *size, uint8_t *dest,
4559                       unsigned int *frame_flags) {
4560   vp9_rc_get_svc_params(cpi);
4561   encode_frame_to_data_rate(cpi, size, dest, frame_flags);
4562 }
4563
4564 static void Pass0Encode(VP9_COMP *cpi, size_t *size, uint8_t *dest,
4565                         unsigned int *frame_flags) {
4566   if (cpi->oxcf.rc_mode == VPX_CBR) {
4567     vp9_rc_get_one_pass_cbr_params(cpi);
4568   } else {
4569     vp9_rc_get_one_pass_vbr_params(cpi);
4570   }
4571   encode_frame_to_data_rate(cpi, size, dest, frame_flags);
4572 }
4573
4574 #if !CONFIG_REALTIME_ONLY
4575 static void Pass2Encode(VP9_COMP *cpi, size_t *size, uint8_t *dest,
4576                         unsigned int *frame_flags) {
4577   cpi->allow_encode_breakout = ENCODE_BREAKOUT_ENABLED;
4578   encode_frame_to_data_rate(cpi, size, dest, frame_flags);
4579
4580   if (!(is_two_pass_svc(cpi) && cpi->svc.encode_empty_frame_state == ENCODING))
4581     vp9_twopass_postencode_update(cpi);
4582 }
4583 #endif  // !CONFIG_REALTIME_ONLY
4584
4585 static void init_ref_frame_bufs(VP9_COMMON *cm) {
4586   int i;
4587   BufferPool *const pool = cm->buffer_pool;
4588   cm->new_fb_idx = INVALID_IDX;
4589   for (i = 0; i < REF_FRAMES; ++i) {
4590     cm->ref_frame_map[i] = INVALID_IDX;
4591     pool->frame_bufs[i].ref_count = 0;
4592   }
4593 }
4594
4595 static void check_initial_width(VP9_COMP *cpi,
4596 #if CONFIG_VP9_HIGHBITDEPTH
4597                                 int use_highbitdepth,
4598 #endif
4599                                 int subsampling_x, int subsampling_y) {
4600   VP9_COMMON *const cm = &cpi->common;
4601
4602   if (!cpi->initial_width ||
4603 #if CONFIG_VP9_HIGHBITDEPTH
4604       cm->use_highbitdepth != use_highbitdepth ||
4605 #endif
4606       cm->subsampling_x != subsampling_x ||
4607       cm->subsampling_y != subsampling_y) {
4608     cm->subsampling_x = subsampling_x;
4609     cm->subsampling_y = subsampling_y;
4610 #if CONFIG_VP9_HIGHBITDEPTH
4611     cm->use_highbitdepth = use_highbitdepth;
4612 #endif
4613
4614     alloc_raw_frame_buffers(cpi);
4615     init_ref_frame_bufs(cm);
4616     alloc_util_frame_buffers(cpi);
4617
4618     init_motion_estimation(cpi);  // TODO(agrange) This can be removed.
4619
4620     cpi->initial_width = cm->width;
4621     cpi->initial_height = cm->height;
4622     cpi->initial_mbs = cm->MBs;
4623   }
4624 }
4625
4626 int vp9_receive_raw_frame(VP9_COMP *cpi, vpx_enc_frame_flags_t frame_flags,
4627                           YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
4628                           int64_t end_time) {
4629   VP9_COMMON *const cm = &cpi->common;
4630   struct vpx_usec_timer timer;
4631   int res = 0;
4632   const int subsampling_x = sd->subsampling_x;
4633   const int subsampling_y = sd->subsampling_y;
4634 #if CONFIG_VP9_HIGHBITDEPTH
4635   const int use_highbitdepth = (sd->flags & YV12_FLAG_HIGHBITDEPTH) != 0;
4636 #endif
4637
4638 #if CONFIG_VP9_HIGHBITDEPTH
4639   check_initial_width(cpi, use_highbitdepth, subsampling_x, subsampling_y);
4640 #else
4641   check_initial_width(cpi, subsampling_x, subsampling_y);
4642 #endif  // CONFIG_VP9_HIGHBITDEPTH
4643
4644 #if CONFIG_VP9_TEMPORAL_DENOISING
4645   setup_denoiser_buffer(cpi);
4646 #endif
4647   vpx_usec_timer_start(&timer);
4648
4649   if (vp9_lookahead_push(cpi->lookahead, sd, time_stamp, end_time,
4650 #if CONFIG_VP9_HIGHBITDEPTH
4651                          use_highbitdepth,
4652 #endif  // CONFIG_VP9_HIGHBITDEPTH
4653                          frame_flags))
4654     res = -1;
4655   vpx_usec_timer_mark(&timer);
4656   cpi->time_receive_data += vpx_usec_timer_elapsed(&timer);
4657
4658   if ((cm->profile == PROFILE_0 || cm->profile == PROFILE_2) &&
4659       (subsampling_x != 1 || subsampling_y != 1)) {
4660     vpx_internal_error(&cm->error, VPX_CODEC_INVALID_PARAM,
4661                        "Non-4:2:0 color format requires profile 1 or 3");
4662     res = -1;
4663   }
4664   if ((cm->profile == PROFILE_1 || cm->profile == PROFILE_3) &&
4665       (subsampling_x == 1 && subsampling_y == 1)) {
4666     vpx_internal_error(&cm->error, VPX_CODEC_INVALID_PARAM,
4667                        "4:2:0 color format requires profile 0 or 2");
4668     res = -1;
4669   }
4670
4671   return res;
4672 }
4673
4674 static int frame_is_reference(const VP9_COMP *cpi) {
4675   const VP9_COMMON *cm = &cpi->common;
4676
4677   return cm->frame_type == KEY_FRAME || cpi->refresh_last_frame ||
4678          cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame ||
4679          cm->refresh_frame_context || cm->lf.mode_ref_delta_update ||
4680          cm->seg.update_map || cm->seg.update_data;
4681 }
4682
4683 static void adjust_frame_rate(VP9_COMP *cpi,
4684                               const struct lookahead_entry *source) {
4685   int64_t this_duration;
4686   int step = 0;
4687
4688   if (source->ts_start == cpi->first_time_stamp_ever) {
4689     this_duration = source->ts_end - source->ts_start;
4690     step = 1;
4691   } else {
4692     int64_t last_duration =
4693         cpi->last_end_time_stamp_seen - cpi->last_time_stamp_seen;
4694
4695     this_duration = source->ts_end - cpi->last_end_time_stamp_seen;
4696
4697     // do a step update if the duration changes by 10%
4698     if (last_duration)
4699       step = (int)((this_duration - last_duration) * 10 / last_duration);
4700   }
4701
4702   if (this_duration) {
4703     if (step) {
4704       vp9_new_framerate(cpi, 10000000.0 / this_duration);
4705     } else {
4706       // Average this frame's rate into the last second's average
4707       // frame rate. If we haven't seen 1 second yet, then average
4708       // over the whole interval seen.
4709       const double interval = VPXMIN(
4710           (double)(source->ts_end - cpi->first_time_stamp_ever), 10000000.0);
4711       double avg_duration = 10000000.0 / cpi->framerate;
4712       avg_duration *= (interval - avg_duration + this_duration);
4713       avg_duration /= interval;
4714
4715       vp9_new_framerate(cpi, 10000000.0 / avg_duration);
4716     }
4717   }
4718   cpi->last_time_stamp_seen = source->ts_start;
4719   cpi->last_end_time_stamp_seen = source->ts_end;
4720 }
4721
4722 // Returns 0 if this is not an alt ref else the offset of the source frame
4723 // used as the arf midpoint.
4724 static int get_arf_src_index(VP9_COMP *cpi) {
4725   RATE_CONTROL *const rc = &cpi->rc;
4726   int arf_src_index = 0;
4727   if (is_altref_enabled(cpi)) {
4728     if (cpi->oxcf.pass == 2) {
4729       const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
4730       if (gf_group->update_type[gf_group->index] == ARF_UPDATE) {
4731         arf_src_index = gf_group->arf_src_offset[gf_group->index];
4732       }
4733     } else if (rc->source_alt_ref_pending) {
4734       arf_src_index = rc->frames_till_gf_update_due;
4735     }
4736   }
4737   return arf_src_index;
4738 }
4739
4740 static void check_src_altref(VP9_COMP *cpi,
4741                              const struct lookahead_entry *source) {
4742   RATE_CONTROL *const rc = &cpi->rc;
4743
4744   if (cpi->oxcf.pass == 2) {
4745     const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
4746     rc->is_src_frame_alt_ref =
4747         (gf_group->update_type[gf_group->index] == OVERLAY_UPDATE);
4748   } else {
4749     rc->is_src_frame_alt_ref =
4750         cpi->alt_ref_source && (source == cpi->alt_ref_source);
4751   }
4752
4753   if (rc->is_src_frame_alt_ref) {
4754     // Current frame is an ARF overlay frame.
4755     cpi->alt_ref_source = NULL;
4756
4757     // Don't refresh the last buffer for an ARF overlay frame. It will
4758     // become the GF so preserve last as an alternative prediction option.
4759     cpi->refresh_last_frame = 0;
4760   }
4761 }
4762
4763 #if CONFIG_INTERNAL_STATS
4764 extern double vp9_get_blockiness(const uint8_t *img1, int img1_pitch,
4765                                  const uint8_t *img2, int img2_pitch, int width,
4766                                  int height);
4767
4768 static void adjust_image_stat(double y, double u, double v, double all,
4769                               ImageStat *s) {
4770   s->stat[Y] += y;
4771   s->stat[U] += u;
4772   s->stat[V] += v;
4773   s->stat[ALL] += all;
4774   s->worst = VPXMIN(s->worst, all);
4775 }
4776 #endif  // CONFIG_INTERNAL_STATS
4777
4778 // Adjust the maximum allowable frame size for the target level.
4779 static void level_rc_framerate(VP9_COMP *cpi, int arf_src_index) {
4780   RATE_CONTROL *const rc = &cpi->rc;
4781   LevelConstraint *const ls = &cpi->level_constraint;
4782   VP9_COMMON *const cm = &cpi->common;
4783   const double max_cpb_size = ls->max_cpb_size;
4784   vpx_clear_system_state();
4785   rc->max_frame_bandwidth = VPXMIN(rc->max_frame_bandwidth, ls->max_frame_size);
4786   if (frame_is_intra_only(cm)) {
4787     rc->max_frame_bandwidth =
4788         VPXMIN(rc->max_frame_bandwidth, (int)(max_cpb_size * 0.5));
4789   } else if (arf_src_index > 0) {
4790     rc->max_frame_bandwidth =
4791         VPXMIN(rc->max_frame_bandwidth, (int)(max_cpb_size * 0.4));
4792   } else {
4793     rc->max_frame_bandwidth =
4794         VPXMIN(rc->max_frame_bandwidth, (int)(max_cpb_size * 0.2));
4795   }
4796 }
4797
4798 static void update_level_info(VP9_COMP *cpi, size_t *size, int arf_src_index) {
4799   VP9_COMMON *const cm = &cpi->common;
4800   Vp9LevelInfo *const level_info = &cpi->level_info;
4801   Vp9LevelSpec *const level_spec = &level_info->level_spec;
4802   Vp9LevelStats *const level_stats = &level_info->level_stats;
4803   int i, idx;
4804   uint64_t luma_samples, dur_end;
4805   const uint32_t luma_pic_size = cm->width * cm->height;
4806   LevelConstraint *const level_constraint = &cpi->level_constraint;
4807   const int8_t level_index = level_constraint->level_index;
4808   double cpb_data_size;
4809
4810   vpx_clear_system_state();
4811
4812   // update level_stats
4813   level_stats->total_compressed_size += *size;
4814   if (cm->show_frame) {
4815     level_stats->total_uncompressed_size +=
4816         luma_pic_size +
4817         2 * (luma_pic_size >> (cm->subsampling_x + cm->subsampling_y));
4818     level_stats->time_encoded =
4819         (cpi->last_end_time_stamp_seen - cpi->first_time_stamp_ever) /
4820         (double)TICKS_PER_SEC;
4821   }
4822
4823   if (arf_src_index > 0) {
4824     if (!level_stats->seen_first_altref) {
4825       level_stats->seen_first_altref = 1;
4826     } else if (level_stats->frames_since_last_altref <
4827                level_spec->min_altref_distance) {
4828       level_spec->min_altref_distance = level_stats->frames_since_last_altref;
4829     }
4830     level_stats->frames_since_last_altref = 0;
4831   } else {
4832     ++level_stats->frames_since_last_altref;
4833   }
4834
4835   if (level_stats->frame_window_buffer.len < FRAME_WINDOW_SIZE - 1) {
4836     idx = (level_stats->frame_window_buffer.start +
4837            level_stats->frame_window_buffer.len++) %
4838           FRAME_WINDOW_SIZE;
4839   } else {
4840     idx = level_stats->frame_window_buffer.start;
4841     level_stats->frame_window_buffer.start = (idx + 1) % FRAME_WINDOW_SIZE;
4842   }
4843   level_stats->frame_window_buffer.buf[idx].ts = cpi->last_time_stamp_seen;
4844   level_stats->frame_window_buffer.buf[idx].size = (uint32_t)(*size);
4845   level_stats->frame_window_buffer.buf[idx].luma_samples = luma_pic_size;
4846
4847   if (cm->frame_type == KEY_FRAME) {
4848     level_stats->ref_refresh_map = 0;
4849   } else {
4850     int count = 0;
4851     level_stats->ref_refresh_map |= vp9_get_refresh_mask(cpi);
4852     // Also need to consider the case where the encoder refers to a buffer
4853     // that has been implicitly refreshed after encoding a keyframe.
4854     if (!cm->intra_only) {
4855       level_stats->ref_refresh_map |= (1 << cpi->lst_fb_idx);
4856       level_stats->ref_refresh_map |= (1 << cpi->gld_fb_idx);
4857       level_stats->ref_refresh_map |= (1 << cpi->alt_fb_idx);
4858     }
4859     for (i = 0; i < REF_FRAMES; ++i) {
4860       count += (level_stats->ref_refresh_map >> i) & 1;
4861     }
4862     if (count > level_spec->max_ref_frame_buffers) {
4863       level_spec->max_ref_frame_buffers = count;
4864     }
4865   }
4866
4867   // update average_bitrate
4868   level_spec->average_bitrate = (double)level_stats->total_compressed_size /
4869                                 125.0 / level_stats->time_encoded;
4870
4871   // update max_luma_sample_rate
4872   luma_samples = 0;
4873   for (i = 0; i < level_stats->frame_window_buffer.len; ++i) {
4874     idx = (level_stats->frame_window_buffer.start +
4875            level_stats->frame_window_buffer.len - 1 - i) %
4876           FRAME_WINDOW_SIZE;
4877     if (i == 0) {
4878       dur_end = level_stats->frame_window_buffer.buf[idx].ts;
4879     }
4880     if (dur_end - level_stats->frame_window_buffer.buf[idx].ts >=
4881         TICKS_PER_SEC) {
4882       break;
4883     }
4884     luma_samples += level_stats->frame_window_buffer.buf[idx].luma_samples;
4885   }
4886   if (luma_samples > level_spec->max_luma_sample_rate) {
4887     level_spec->max_luma_sample_rate = luma_samples;
4888   }
4889
4890   // update max_cpb_size
4891   cpb_data_size = 0;
4892   for (i = 0; i < CPB_WINDOW_SIZE; ++i) {
4893     if (i >= level_stats->frame_window_buffer.len) break;
4894     idx = (level_stats->frame_window_buffer.start +
4895            level_stats->frame_window_buffer.len - 1 - i) %
4896           FRAME_WINDOW_SIZE;
4897     cpb_data_size += level_stats->frame_window_buffer.buf[idx].size;
4898   }
4899   cpb_data_size = cpb_data_size / 125.0;
4900   if (cpb_data_size > level_spec->max_cpb_size) {
4901     level_spec->max_cpb_size = cpb_data_size;
4902   }
4903
4904   // update max_luma_picture_size
4905   if (luma_pic_size > level_spec->max_luma_picture_size) {
4906     level_spec->max_luma_picture_size = luma_pic_size;
4907   }
4908
4909   // update compression_ratio
4910   level_spec->compression_ratio = (double)level_stats->total_uncompressed_size *
4911                                   cm->bit_depth /
4912                                   level_stats->total_compressed_size / 8.0;
4913
4914   // update max_col_tiles
4915   if (level_spec->max_col_tiles < (1 << cm->log2_tile_cols)) {
4916     level_spec->max_col_tiles = (1 << cm->log2_tile_cols);
4917   }
4918
4919   if (level_index >= 0 && level_constraint->fail_flag == 0) {
4920     if (level_spec->max_luma_picture_size >
4921         vp9_level_defs[level_index].max_luma_picture_size) {
4922       level_constraint->fail_flag |= (1 << LUMA_PIC_SIZE_TOO_LARGE);
4923       vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
4924                          "Failed to encode to the target level %d. %s",
4925                          vp9_level_defs[level_index].level,
4926                          level_fail_messages[LUMA_PIC_SIZE_TOO_LARGE]);
4927     }
4928
4929     if ((double)level_spec->max_luma_sample_rate >
4930         (double)vp9_level_defs[level_index].max_luma_sample_rate *
4931             (1 + SAMPLE_RATE_GRACE_P)) {
4932       level_constraint->fail_flag |= (1 << LUMA_SAMPLE_RATE_TOO_LARGE);
4933       vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
4934                          "Failed to encode to the target level %d. %s",
4935                          vp9_level_defs[level_index].level,
4936                          level_fail_messages[LUMA_SAMPLE_RATE_TOO_LARGE]);
4937     }
4938
4939     if (level_spec->max_col_tiles > vp9_level_defs[level_index].max_col_tiles) {
4940       level_constraint->fail_flag |= (1 << TOO_MANY_COLUMN_TILE);
4941       vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
4942                          "Failed to encode to the target level %d. %s",
4943                          vp9_level_defs[level_index].level,
4944                          level_fail_messages[TOO_MANY_COLUMN_TILE]);
4945     }
4946
4947     if (level_spec->min_altref_distance <
4948         vp9_level_defs[level_index].min_altref_distance) {
4949       level_constraint->fail_flag |= (1 << ALTREF_DIST_TOO_SMALL);
4950       vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
4951                          "Failed to encode to the target level %d. %s",
4952                          vp9_level_defs[level_index].level,
4953                          level_fail_messages[ALTREF_DIST_TOO_SMALL]);
4954     }
4955
4956     if (level_spec->max_ref_frame_buffers >
4957         vp9_level_defs[level_index].max_ref_frame_buffers) {
4958       level_constraint->fail_flag |= (1 << TOO_MANY_REF_BUFFER);
4959       vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
4960                          "Failed to encode to the target level %d. %s",
4961                          vp9_level_defs[level_index].level,
4962                          level_fail_messages[TOO_MANY_REF_BUFFER]);
4963     }
4964
4965     if (level_spec->max_cpb_size > vp9_level_defs[level_index].max_cpb_size) {
4966       level_constraint->fail_flag |= (1 << CPB_TOO_LARGE);
4967       vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
4968                          "Failed to encode to the target level %d. %s",
4969                          vp9_level_defs[level_index].level,
4970                          level_fail_messages[CPB_TOO_LARGE]);
4971     }
4972
4973     // Set an upper bound for the next frame size. It will be used in
4974     // level_rc_framerate() before encoding the next frame.
4975     cpb_data_size = 0;
4976     for (i = 0; i < CPB_WINDOW_SIZE - 1; ++i) {
4977       if (i >= level_stats->frame_window_buffer.len) break;
4978       idx = (level_stats->frame_window_buffer.start +
4979              level_stats->frame_window_buffer.len - 1 - i) %
4980             FRAME_WINDOW_SIZE;
4981       cpb_data_size += level_stats->frame_window_buffer.buf[idx].size;
4982     }
4983     cpb_data_size = cpb_data_size / 125.0;
4984     level_constraint->max_frame_size =
4985         (int)((vp9_level_defs[level_index].max_cpb_size - cpb_data_size) *
4986               1000.0);
4987     if (level_stats->frame_window_buffer.len < CPB_WINDOW_SIZE - 1)
4988       level_constraint->max_frame_size >>= 1;
4989   }
4990 }
4991
4992 int vp9_get_compressed_data(VP9_COMP *cpi, unsigned int *frame_flags,
4993                             size_t *size, uint8_t *dest, int64_t *time_stamp,
4994                             int64_t *time_end, int flush) {
4995   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
4996   VP9_COMMON *const cm = &cpi->common;
4997   BufferPool *const pool = cm->buffer_pool;
4998   RATE_CONTROL *const rc = &cpi->rc;
4999   struct vpx_usec_timer cmptimer;
5000   YV12_BUFFER_CONFIG *force_src_buffer = NULL;
5001   struct lookahead_entry *last_source = NULL;
5002   struct lookahead_entry *source = NULL;
5003   int arf_src_index;
5004   int i;
5005
5006   if (is_two_pass_svc(cpi)) {
5007 #if CONFIG_SPATIAL_SVC
5008     vp9_svc_start_frame(cpi);
5009     // Use a small empty frame instead of a real frame
5010     if (cpi->svc.encode_empty_frame_state == ENCODING)
5011       source = &cpi->svc.empty_frame;
5012 #endif
5013     if (oxcf->pass == 2) vp9_restore_layer_context(cpi);
5014   } else if (is_one_pass_cbr_svc(cpi)) {
5015     vp9_one_pass_cbr_svc_start_layer(cpi);
5016   }
5017
5018   vpx_usec_timer_start(&cmptimer);
5019
5020   vp9_set_high_precision_mv(cpi, ALTREF_HIGH_PRECISION_MV);
5021
5022   // Is multi-arf enabled.
5023   // Note that at the moment multi_arf is only configured for 2 pass VBR and
5024   // will not work properly with svc.
5025   if ((oxcf->pass == 2) && !cpi->use_svc && (cpi->oxcf.enable_auto_arf > 1))
5026     cpi->multi_arf_allowed = 1;
5027   else
5028     cpi->multi_arf_allowed = 0;
5029
5030   // Normal defaults
5031   cm->reset_frame_context = 0;
5032   cm->refresh_frame_context = 1;
5033   if (!is_one_pass_cbr_svc(cpi)) {
5034     cpi->refresh_last_frame = 1;
5035     cpi->refresh_golden_frame = 0;
5036     cpi->refresh_alt_ref_frame = 0;
5037   }
5038
5039   // Should we encode an arf frame.
5040   arf_src_index = get_arf_src_index(cpi);
5041
5042   // Skip alt frame if we encode the empty frame
5043   if (is_two_pass_svc(cpi) && source != NULL) arf_src_index = 0;
5044
5045   if (arf_src_index) {
5046     for (i = 0; i <= arf_src_index; ++i) {
5047       struct lookahead_entry *e = vp9_lookahead_peek(cpi->lookahead, i);
5048       // Avoid creating an alt-ref if there's a forced keyframe pending.
5049       if (e == NULL) {
5050         break;
5051       } else if (e->flags == VPX_EFLAG_FORCE_KF) {
5052         arf_src_index = 0;
5053         flush = 1;
5054         break;
5055       }
5056     }
5057   }
5058
5059   if (arf_src_index) {
5060     assert(arf_src_index <= rc->frames_to_key);
5061
5062     if ((source = vp9_lookahead_peek(cpi->lookahead, arf_src_index)) != NULL) {
5063       cpi->alt_ref_source = source;
5064
5065 #if CONFIG_SPATIAL_SVC
5066       if (is_two_pass_svc(cpi) && cpi->svc.spatial_layer_id > 0) {
5067         int i;
5068         // Reference a hidden frame from a lower layer
5069         for (i = cpi->svc.spatial_layer_id - 1; i >= 0; --i) {
5070           if (oxcf->ss_enable_auto_arf[i]) {
5071             cpi->gld_fb_idx = cpi->svc.layer_context[i].alt_ref_idx;
5072             break;
5073           }
5074         }
5075       }
5076       cpi->svc.layer_context[cpi->svc.spatial_layer_id].has_alt_frame = 1;
5077 #endif
5078 #if !CONFIG_REALTIME_ONLY
5079       if ((oxcf->mode != REALTIME) && (oxcf->arnr_max_frames > 0) &&
5080           (oxcf->arnr_strength > 0)) {
5081         int bitrate = cpi->rc.avg_frame_bandwidth / 40;
5082         int not_low_bitrate = bitrate > ALT_REF_AQ_LOW_BITRATE_BOUNDARY;
5083
5084         int not_last_frame = (cpi->lookahead->sz - arf_src_index > 1);
5085         not_last_frame |= ALT_REF_AQ_APPLY_TO_LAST_FRAME;
5086
5087         // Produce the filtered ARF frame.
5088         vp9_temporal_filter(cpi, arf_src_index);
5089         vpx_extend_frame_borders(&cpi->alt_ref_buffer);
5090
5091         // for small bitrates segmentation overhead usually
5092         // eats all bitrate gain from enabling delta quantizers
5093         if (cpi->oxcf.alt_ref_aq != 0 && not_low_bitrate && not_last_frame)
5094           vp9_alt_ref_aq_setup_mode(cpi->alt_ref_aq, cpi);
5095
5096         force_src_buffer = &cpi->alt_ref_buffer;
5097       }
5098 #endif
5099       cm->show_frame = 0;
5100       cm->intra_only = 0;
5101       cpi->refresh_alt_ref_frame = 1;
5102       cpi->refresh_golden_frame = 0;
5103       cpi->refresh_last_frame = 0;
5104       rc->is_src_frame_alt_ref = 0;
5105       rc->source_alt_ref_pending = 0;
5106     } else {
5107       rc->source_alt_ref_pending = 0;
5108     }
5109   }
5110
5111   if (!source) {
5112     // Get last frame source.
5113     if (cm->current_video_frame > 0) {
5114       if ((last_source = vp9_lookahead_peek(cpi->lookahead, -1)) == NULL)
5115         return -1;
5116     }
5117
5118     // Read in the source frame.
5119     if (cpi->use_svc)
5120       source = vp9_svc_lookahead_pop(cpi, cpi->lookahead, flush);
5121     else
5122       source = vp9_lookahead_pop(cpi->lookahead, flush);
5123
5124     if (source != NULL) {
5125       cm->show_frame = 1;
5126       cm->intra_only = 0;
5127       // if the flags indicate intra frame, but if the current picture is for
5128       // non-zero spatial layer, it should not be an intra picture.
5129       // TODO(Won Kap): this needs to change if per-layer intra frame is
5130       // allowed.
5131       if ((source->flags & VPX_EFLAG_FORCE_KF) &&
5132           cpi->svc.spatial_layer_id > cpi->svc.first_spatial_layer_to_encode) {
5133         source->flags &= ~(unsigned int)(VPX_EFLAG_FORCE_KF);
5134       }
5135
5136       // Check to see if the frame should be encoded as an arf overlay.
5137       check_src_altref(cpi, source);
5138     }
5139   }
5140
5141   if (source) {
5142     cpi->un_scaled_source = cpi->Source =
5143         force_src_buffer ? force_src_buffer : &source->img;
5144
5145 #ifdef ENABLE_KF_DENOISE
5146     // Copy of raw source for metrics calculation.
5147     if (is_psnr_calc_enabled(cpi))
5148       vp9_copy_and_extend_frame(cpi->Source, &cpi->raw_unscaled_source);
5149 #endif
5150
5151     cpi->unscaled_last_source = last_source != NULL ? &last_source->img : NULL;
5152
5153     *time_stamp = source->ts_start;
5154     *time_end = source->ts_end;
5155     *frame_flags = (source->flags & VPX_EFLAG_FORCE_KF) ? FRAMEFLAGS_KEY : 0;
5156
5157   } else {
5158     *size = 0;
5159 #if !CONFIG_REALTIME_ONLY
5160     if (flush && oxcf->pass == 1 && !cpi->twopass.first_pass_done) {
5161       vp9_end_first_pass(cpi); /* get last stats packet */
5162       cpi->twopass.first_pass_done = 1;
5163     }
5164 #endif  // !CONFIG_REALTIME_ONLY
5165     return -1;
5166   }
5167
5168   if (source->ts_start < cpi->first_time_stamp_ever) {
5169     cpi->first_time_stamp_ever = source->ts_start;
5170     cpi->last_end_time_stamp_seen = source->ts_start;
5171   }
5172
5173   // Clear down mmx registers
5174   vpx_clear_system_state();
5175
5176   // adjust frame rates based on timestamps given
5177   if (cm->show_frame) {
5178     adjust_frame_rate(cpi, source);
5179   }
5180
5181   if (is_one_pass_cbr_svc(cpi)) {
5182     vp9_update_temporal_layer_framerate(cpi);
5183     vp9_restore_layer_context(cpi);
5184   }
5185
5186   // Find a free buffer for the new frame, releasing the reference previously
5187   // held.
5188   if (cm->new_fb_idx != INVALID_IDX) {
5189     --pool->frame_bufs[cm->new_fb_idx].ref_count;
5190   }
5191   cm->new_fb_idx = get_free_fb(cm);
5192
5193   if (cm->new_fb_idx == INVALID_IDX) return -1;
5194
5195   cm->cur_frame = &pool->frame_bufs[cm->new_fb_idx];
5196
5197   if (!cpi->use_svc && cpi->multi_arf_allowed) {
5198     if (cm->frame_type == KEY_FRAME) {
5199       init_buffer_indices(cpi);
5200     } else if (oxcf->pass == 2) {
5201       const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
5202       cpi->alt_fb_idx = gf_group->arf_ref_idx[gf_group->index];
5203     }
5204   }
5205
5206   // Start with a 0 size frame.
5207   *size = 0;
5208
5209   cpi->frame_flags = *frame_flags;
5210
5211 #if !CONFIG_REALTIME_ONLY
5212   if ((oxcf->pass == 2) &&
5213       (!cpi->use_svc || (is_two_pass_svc(cpi) &&
5214                          cpi->svc.encode_empty_frame_state != ENCODING))) {
5215     vp9_rc_get_second_pass_params(cpi);
5216   } else if (oxcf->pass == 1) {
5217     set_frame_size(cpi);
5218   }
5219 #endif  // !CONFIG_REALTIME_ONLY
5220
5221   if (oxcf->pass != 1 && cpi->level_constraint.level_index >= 0 &&
5222       cpi->level_constraint.fail_flag == 0)
5223     level_rc_framerate(cpi, arf_src_index);
5224
5225   if (cpi->oxcf.pass != 0 || cpi->use_svc || frame_is_intra_only(cm) == 1) {
5226     for (i = 0; i < MAX_REF_FRAMES; ++i) cpi->scaled_ref_idx[i] = INVALID_IDX;
5227   }
5228
5229   cpi->td.mb.fp_src_pred = 0;
5230 #if CONFIG_REALTIME_ONLY
5231   if (cpi->use_svc) {
5232     SvcEncode(cpi, size, dest, frame_flags);
5233   } else {
5234     // One pass encode
5235     Pass0Encode(cpi, size, dest, frame_flags);
5236   }
5237 #else  // !CONFIG_REALTIME_ONLY
5238   if (oxcf->pass == 1 && (!cpi->use_svc || is_two_pass_svc(cpi))) {
5239     const int lossless = is_lossless_requested(oxcf);
5240 #if CONFIG_VP9_HIGHBITDEPTH
5241     if (cpi->oxcf.use_highbitdepth)
5242       cpi->td.mb.fwd_txfm4x4 =
5243           lossless ? vp9_highbd_fwht4x4 : vpx_highbd_fdct4x4;
5244     else
5245       cpi->td.mb.fwd_txfm4x4 = lossless ? vp9_fwht4x4 : vpx_fdct4x4;
5246     cpi->td.mb.highbd_inv_txfm_add =
5247         lossless ? vp9_highbd_iwht4x4_add : vp9_highbd_idct4x4_add;
5248 #else
5249     cpi->td.mb.fwd_txfm4x4 = lossless ? vp9_fwht4x4 : vpx_fdct4x4;
5250 #endif  // CONFIG_VP9_HIGHBITDEPTH
5251     cpi->td.mb.inv_txfm_add = lossless ? vp9_iwht4x4_add : vp9_idct4x4_add;
5252     vp9_first_pass(cpi, source);
5253   } else if (oxcf->pass == 2 && (!cpi->use_svc || is_two_pass_svc(cpi))) {
5254     Pass2Encode(cpi, size, dest, frame_flags);
5255   } else if (cpi->use_svc) {
5256     SvcEncode(cpi, size, dest, frame_flags);
5257   } else {
5258     // One pass encode
5259     Pass0Encode(cpi, size, dest, frame_flags);
5260   }
5261 #endif  // CONFIG_REALTIME_ONLY
5262
5263   if (cm->refresh_frame_context)
5264     cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
5265
5266   // No frame encoded, or frame was dropped, release scaled references.
5267   if ((*size == 0) && (frame_is_intra_only(cm) == 0)) {
5268     release_scaled_references(cpi);
5269   }
5270
5271   if (*size > 0) {
5272     cpi->droppable = !frame_is_reference(cpi);
5273   }
5274
5275   // Save layer specific state.
5276   if (is_one_pass_cbr_svc(cpi) || ((cpi->svc.number_temporal_layers > 1 ||
5277                                     cpi->svc.number_spatial_layers > 1) &&
5278                                    oxcf->pass == 2)) {
5279     vp9_save_layer_context(cpi);
5280   }
5281
5282   vpx_usec_timer_mark(&cmptimer);
5283   cpi->time_compress_data += vpx_usec_timer_elapsed(&cmptimer);
5284
5285   // Should we calculate metrics for the frame.
5286   if (is_psnr_calc_enabled(cpi)) generate_psnr_packet(cpi);
5287
5288   if (cpi->keep_level_stats && oxcf->pass != 1)
5289     update_level_info(cpi, size, arf_src_index);
5290
5291 #if CONFIG_INTERNAL_STATS
5292
5293   if (oxcf->pass != 1) {
5294     double samples = 0.0;
5295     cpi->bytes += (int)(*size);
5296
5297     if (cm->show_frame) {
5298       uint32_t bit_depth = 8;
5299       uint32_t in_bit_depth = 8;
5300       cpi->count++;
5301 #if CONFIG_VP9_HIGHBITDEPTH
5302       if (cm->use_highbitdepth) {
5303         in_bit_depth = cpi->oxcf.input_bit_depth;
5304         bit_depth = cm->bit_depth;
5305       }
5306 #endif
5307
5308       if (cpi->b_calculate_psnr) {
5309         YV12_BUFFER_CONFIG *orig = cpi->raw_source_frame;
5310         YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
5311         YV12_BUFFER_CONFIG *pp = &cm->post_proc_buffer;
5312         PSNR_STATS psnr;
5313 #if CONFIG_VP9_HIGHBITDEPTH
5314         vpx_calc_highbd_psnr(orig, recon, &psnr, cpi->td.mb.e_mbd.bd,
5315                              in_bit_depth);
5316 #else
5317         vpx_calc_psnr(orig, recon, &psnr);
5318 #endif  // CONFIG_VP9_HIGHBITDEPTH
5319
5320         adjust_image_stat(psnr.psnr[1], psnr.psnr[2], psnr.psnr[3],
5321                           psnr.psnr[0], &cpi->psnr);
5322         cpi->total_sq_error += psnr.sse[0];
5323         cpi->total_samples += psnr.samples[0];
5324         samples = psnr.samples[0];
5325
5326         {
5327           PSNR_STATS psnr2;
5328           double frame_ssim2 = 0, weight = 0;
5329 #if CONFIG_VP9_POSTPROC
5330           if (vpx_alloc_frame_buffer(
5331                   pp, recon->y_crop_width, recon->y_crop_height,
5332                   cm->subsampling_x, cm->subsampling_y,
5333 #if CONFIG_VP9_HIGHBITDEPTH
5334                   cm->use_highbitdepth,
5335 #endif
5336                   VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment) < 0) {
5337             vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
5338                                "Failed to allocate post processing buffer");
5339           }
5340           {
5341             vp9_ppflags_t ppflags;
5342             ppflags.post_proc_flag = VP9D_DEBLOCK;
5343             ppflags.deblocking_level = 0;  // not used in vp9_post_proc_frame()
5344             ppflags.noise_level = 0;       // not used in vp9_post_proc_frame()
5345             vp9_post_proc_frame(cm, pp, &ppflags);
5346           }
5347 #endif
5348           vpx_clear_system_state();
5349
5350 #if CONFIG_VP9_HIGHBITDEPTH
5351           vpx_calc_highbd_psnr(orig, pp, &psnr2, cpi->td.mb.e_mbd.bd,
5352                                cpi->oxcf.input_bit_depth);
5353 #else
5354           vpx_calc_psnr(orig, pp, &psnr2);
5355 #endif  // CONFIG_VP9_HIGHBITDEPTH
5356
5357           cpi->totalp_sq_error += psnr2.sse[0];
5358           cpi->totalp_samples += psnr2.samples[0];
5359           adjust_image_stat(psnr2.psnr[1], psnr2.psnr[2], psnr2.psnr[3],
5360                             psnr2.psnr[0], &cpi->psnrp);
5361
5362 #if CONFIG_VP9_HIGHBITDEPTH
5363           if (cm->use_highbitdepth) {
5364             frame_ssim2 = vpx_highbd_calc_ssim(orig, recon, &weight, bit_depth,
5365                                                in_bit_depth);
5366           } else {
5367             frame_ssim2 = vpx_calc_ssim(orig, recon, &weight);
5368           }
5369 #else
5370           frame_ssim2 = vpx_calc_ssim(orig, recon, &weight);
5371 #endif  // CONFIG_VP9_HIGHBITDEPTH
5372
5373           cpi->worst_ssim = VPXMIN(cpi->worst_ssim, frame_ssim2);
5374           cpi->summed_quality += frame_ssim2 * weight;
5375           cpi->summed_weights += weight;
5376
5377 #if CONFIG_VP9_HIGHBITDEPTH
5378           if (cm->use_highbitdepth) {
5379             frame_ssim2 = vpx_highbd_calc_ssim(orig, pp, &weight, bit_depth,
5380                                                in_bit_depth);
5381           } else {
5382             frame_ssim2 = vpx_calc_ssim(orig, pp, &weight);
5383           }
5384 #else
5385           frame_ssim2 = vpx_calc_ssim(orig, pp, &weight);
5386 #endif  // CONFIG_VP9_HIGHBITDEPTH
5387
5388           cpi->summedp_quality += frame_ssim2 * weight;
5389           cpi->summedp_weights += weight;
5390 #if 0
5391           {
5392             FILE *f = fopen("q_used.stt", "a");
5393             fprintf(f, "%5d : Y%f7.3:U%f7.3:V%f7.3:F%f7.3:S%7.3f\n",
5394                     cpi->common.current_video_frame, y2, u2, v2,
5395                     frame_psnr2, frame_ssim2);
5396             fclose(f);
5397           }
5398 #endif
5399         }
5400       }
5401       if (cpi->b_calculate_blockiness) {
5402 #if CONFIG_VP9_HIGHBITDEPTH
5403         if (!cm->use_highbitdepth)
5404 #endif
5405         {
5406           double frame_blockiness = vp9_get_blockiness(
5407               cpi->Source->y_buffer, cpi->Source->y_stride,
5408               cm->frame_to_show->y_buffer, cm->frame_to_show->y_stride,
5409               cpi->Source->y_width, cpi->Source->y_height);
5410           cpi->worst_blockiness =
5411               VPXMAX(cpi->worst_blockiness, frame_blockiness);
5412           cpi->total_blockiness += frame_blockiness;
5413         }
5414       }
5415
5416       if (cpi->b_calculate_consistency) {
5417 #if CONFIG_VP9_HIGHBITDEPTH
5418         if (!cm->use_highbitdepth)
5419 #endif
5420         {
5421           double this_inconsistency = vpx_get_ssim_metrics(
5422               cpi->Source->y_buffer, cpi->Source->y_stride,
5423               cm->frame_to_show->y_buffer, cm->frame_to_show->y_stride,
5424               cpi->Source->y_width, cpi->Source->y_height, cpi->ssim_vars,
5425               &cpi->metrics, 1);
5426
5427           const double peak = (double)((1 << cpi->oxcf.input_bit_depth) - 1);
5428           double consistency =
5429               vpx_sse_to_psnr(samples, peak, (double)cpi->total_inconsistency);
5430           if (consistency > 0.0)
5431             cpi->worst_consistency =
5432                 VPXMIN(cpi->worst_consistency, consistency);
5433           cpi->total_inconsistency += this_inconsistency;
5434         }
5435       }
5436
5437       {
5438         double y, u, v, frame_all;
5439         frame_all = vpx_calc_fastssim(cpi->Source, cm->frame_to_show, &y, &u,
5440                                       &v, bit_depth, in_bit_depth);
5441         adjust_image_stat(y, u, v, frame_all, &cpi->fastssim);
5442       }
5443       {
5444         double y, u, v, frame_all;
5445         frame_all = vpx_psnrhvs(cpi->Source, cm->frame_to_show, &y, &u, &v,
5446                                 bit_depth, in_bit_depth);
5447         adjust_image_stat(y, u, v, frame_all, &cpi->psnrhvs);
5448       }
5449     }
5450   }
5451
5452 #endif
5453
5454   if (is_two_pass_svc(cpi)) {
5455     if (cpi->svc.encode_empty_frame_state == ENCODING) {
5456       cpi->svc.encode_empty_frame_state = ENCODED;
5457       cpi->svc.encode_intra_empty_frame = 0;
5458     }
5459
5460     if (cm->show_frame) {
5461       ++cpi->svc.spatial_layer_to_encode;
5462       if (cpi->svc.spatial_layer_to_encode >= cpi->svc.number_spatial_layers)
5463         cpi->svc.spatial_layer_to_encode = 0;
5464
5465       // May need the empty frame after an visible frame.
5466       cpi->svc.encode_empty_frame_state = NEED_TO_ENCODE;
5467     }
5468   } else if (is_one_pass_cbr_svc(cpi)) {
5469     if (cm->show_frame) {
5470       ++cpi->svc.spatial_layer_to_encode;
5471       if (cpi->svc.spatial_layer_to_encode >= cpi->svc.number_spatial_layers)
5472         cpi->svc.spatial_layer_to_encode = 0;
5473     }
5474   }
5475
5476   vpx_clear_system_state();
5477   return 0;
5478 }
5479
5480 int vp9_get_preview_raw_frame(VP9_COMP *cpi, YV12_BUFFER_CONFIG *dest,
5481                               vp9_ppflags_t *flags) {
5482   VP9_COMMON *cm = &cpi->common;
5483 #if !CONFIG_VP9_POSTPROC
5484   (void)flags;
5485 #endif
5486
5487   if (!cm->show_frame) {
5488     return -1;
5489   } else {
5490     int ret;
5491 #if CONFIG_VP9_POSTPROC
5492     ret = vp9_post_proc_frame(cm, dest, flags);
5493 #else
5494     if (cm->frame_to_show) {
5495       *dest = *cm->frame_to_show;
5496       dest->y_width = cm->width;
5497       dest->y_height = cm->height;
5498       dest->uv_width = cm->width >> cm->subsampling_x;
5499       dest->uv_height = cm->height >> cm->subsampling_y;
5500       ret = 0;
5501     } else {
5502       ret = -1;
5503     }
5504 #endif  // !CONFIG_VP9_POSTPROC
5505     vpx_clear_system_state();
5506     return ret;
5507   }
5508 }
5509
5510 int vp9_set_internal_size(VP9_COMP *cpi, VPX_SCALING horiz_mode,
5511                           VPX_SCALING vert_mode) {
5512   VP9_COMMON *cm = &cpi->common;
5513   int hr = 0, hs = 0, vr = 0, vs = 0;
5514
5515   if (horiz_mode > ONETWO || vert_mode > ONETWO) return -1;
5516
5517   Scale2Ratio(horiz_mode, &hr, &hs);
5518   Scale2Ratio(vert_mode, &vr, &vs);
5519
5520   // always go to the next whole number
5521   cm->width = (hs - 1 + cpi->oxcf.width * hr) / hs;
5522   cm->height = (vs - 1 + cpi->oxcf.height * vr) / vs;
5523   if (cm->current_video_frame) {
5524     assert(cm->width <= cpi->initial_width);
5525     assert(cm->height <= cpi->initial_height);
5526   }
5527
5528   update_frame_size(cpi);
5529
5530   return 0;
5531 }
5532
5533 int vp9_set_size_literal(VP9_COMP *cpi, unsigned int width,
5534                          unsigned int height) {
5535   VP9_COMMON *cm = &cpi->common;
5536 #if CONFIG_VP9_HIGHBITDEPTH
5537   check_initial_width(cpi, cm->use_highbitdepth, 1, 1);
5538 #else
5539   check_initial_width(cpi, 1, 1);
5540 #endif  // CONFIG_VP9_HIGHBITDEPTH
5541
5542 #if CONFIG_VP9_TEMPORAL_DENOISING
5543   setup_denoiser_buffer(cpi);
5544 #endif
5545
5546   if (width) {
5547     cm->width = width;
5548     if (cm->width > cpi->initial_width) {
5549       cm->width = cpi->initial_width;
5550       printf("Warning: Desired width too large, changed to %d\n", cm->width);
5551     }
5552   }
5553
5554   if (height) {
5555     cm->height = height;
5556     if (cm->height > cpi->initial_height) {
5557       cm->height = cpi->initial_height;
5558       printf("Warning: Desired height too large, changed to %d\n", cm->height);
5559     }
5560   }
5561   assert(cm->width <= cpi->initial_width);
5562   assert(cm->height <= cpi->initial_height);
5563
5564   update_frame_size(cpi);
5565
5566   return 0;
5567 }
5568
5569 void vp9_set_svc(VP9_COMP *cpi, int use_svc) {
5570   cpi->use_svc = use_svc;
5571   return;
5572 }
5573
5574 int vp9_get_quantizer(VP9_COMP *cpi) { return cpi->common.base_qindex; }
5575
5576 void vp9_apply_encoding_flags(VP9_COMP *cpi, vpx_enc_frame_flags_t flags) {
5577   if (flags &
5578       (VP8_EFLAG_NO_REF_LAST | VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF)) {
5579     int ref = 7;
5580
5581     if (flags & VP8_EFLAG_NO_REF_LAST) ref ^= VP9_LAST_FLAG;
5582
5583     if (flags & VP8_EFLAG_NO_REF_GF) ref ^= VP9_GOLD_FLAG;
5584
5585     if (flags & VP8_EFLAG_NO_REF_ARF) ref ^= VP9_ALT_FLAG;
5586
5587     vp9_use_as_reference(cpi, ref);
5588   }
5589
5590   if (flags &
5591       (VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF |
5592        VP8_EFLAG_FORCE_GF | VP8_EFLAG_FORCE_ARF)) {
5593     int upd = 7;
5594
5595     if (flags & VP8_EFLAG_NO_UPD_LAST) upd ^= VP9_LAST_FLAG;
5596
5597     if (flags & VP8_EFLAG_NO_UPD_GF) upd ^= VP9_GOLD_FLAG;
5598
5599     if (flags & VP8_EFLAG_NO_UPD_ARF) upd ^= VP9_ALT_FLAG;
5600
5601     vp9_update_reference(cpi, upd);
5602   }
5603
5604   if (flags & VP8_EFLAG_NO_UPD_ENTROPY) {
5605     vp9_update_entropy(cpi, 0);
5606   }
5607 }
5608
5609 void vp9_set_row_mt(VP9_COMP *cpi) {
5610   // Enable row based multi-threading for supported modes of encoding
5611   cpi->row_mt = 0;
5612   if (((cpi->oxcf.mode == GOOD || cpi->oxcf.mode == BEST) &&
5613        cpi->oxcf.speed < 5 && cpi->oxcf.pass == 1) &&
5614       cpi->oxcf.row_mt && !cpi->use_svc)
5615     cpi->row_mt = 1;
5616
5617   if (cpi->oxcf.mode == GOOD && cpi->oxcf.speed < 5 &&
5618       (cpi->oxcf.pass == 0 || cpi->oxcf.pass == 2) && cpi->oxcf.row_mt &&
5619       !cpi->use_svc)
5620     cpi->row_mt = 1;
5621
5622   // In realtime mode, enable row based multi-threading for all the speed levels
5623   // where non-rd path is used.
5624   if (cpi->oxcf.mode == REALTIME && cpi->oxcf.speed >= 5 && cpi->oxcf.row_mt) {
5625     cpi->row_mt = 1;
5626   }
5627
5628   if (cpi->row_mt)
5629     cpi->row_mt_bit_exact = 1;
5630   else
5631     cpi->row_mt_bit_exact = 0;
5632 }