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