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