]> granicus.if.org Git - libvpx/blob - vp9/encoder/vp9_encoder.c
Fix the decoder seg fault when frame is corrupted.
[libvpx] / vp9 / encoder / vp9_encoder.c
1 /*
2  * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10
11 #include <math.h>
12 #include <stdio.h>
13 #include <limits.h>
14
15 #include "./vp9_rtcd.h"
16 #include "./vpx_config.h"
17 #include "./vpx_dsp_rtcd.h"
18 #include "./vpx_scale_rtcd.h"
19 #include "vpx_dsp/psnr.h"
20 #include "vpx_dsp/vpx_dsp_common.h"
21 #include "vpx_dsp/vpx_filter.h"
22 #if CONFIG_INTERNAL_STATS
23 #include "vpx_dsp/ssim.h"
24 #endif
25 #include "vpx_ports/mem.h"
26 #include "vpx_ports/system_state.h"
27 #include "vpx_ports/vpx_timer.h"
28
29 #include "vp9/common/vp9_alloccommon.h"
30 #include "vp9/common/vp9_filter.h"
31 #include "vp9/common/vp9_idct.h"
32 #if CONFIG_VP9_POSTPROC
33 #include "vp9/common/vp9_postproc.h"
34 #endif
35 #include "vp9/common/vp9_reconinter.h"
36 #include "vp9/common/vp9_reconintra.h"
37 #include "vp9/common/vp9_tile_common.h"
38
39 #include "vp9/encoder/vp9_alt_ref_aq.h"
40 #include "vp9/encoder/vp9_aq_360.h"
41 #include "vp9/encoder/vp9_aq_complexity.h"
42 #include "vp9/encoder/vp9_aq_cyclicrefresh.h"
43 #include "vp9/encoder/vp9_aq_variance.h"
44 #include "vp9/encoder/vp9_bitstream.h"
45 #include "vp9/encoder/vp9_context_tree.h"
46 #include "vp9/encoder/vp9_encodeframe.h"
47 #include "vp9/encoder/vp9_encodemv.h"
48 #include "vp9/encoder/vp9_encoder.h"
49 #include "vp9/encoder/vp9_extend.h"
50 #include "vp9/encoder/vp9_ethread.h"
51 #include "vp9/encoder/vp9_firstpass.h"
52 #include "vp9/encoder/vp9_mbgraph.h"
53 #include "vp9/encoder/vp9_multi_thread.h"
54 #include "vp9/encoder/vp9_noise_estimate.h"
55 #include "vp9/encoder/vp9_picklpf.h"
56 #include "vp9/encoder/vp9_ratectrl.h"
57 #include "vp9/encoder/vp9_rd.h"
58 #include "vp9/encoder/vp9_resize.h"
59 #include "vp9/encoder/vp9_segmentation.h"
60 #include "vp9/encoder/vp9_skin_detection.h"
61 #include "vp9/encoder/vp9_speed_features.h"
62 #include "vp9/encoder/vp9_svc_layercontext.h"
63 #include "vp9/encoder/vp9_temporal_filter.h"
64
65 #define AM_SEGMENT_ID_INACTIVE 7
66 #define AM_SEGMENT_ID_ACTIVE 0
67
68 #define ALTREF_HIGH_PRECISION_MV 1     // Whether to use high precision mv
69                                        //  for altref computation.
70 #define HIGH_PRECISION_MV_QTHRESH 200  // Q threshold for high precision
71                                        // mv. Choose a very high value for
72                                        // now so that HIGH_PRECISION is always
73                                        // chosen.
74 // #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->content_state_sb_fd);
467   cpi->content_state_sb_fd = 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 static void alloc_copy_partition_data(VP9_COMP *cpi) {
1449   VP9_COMMON *const cm = &cpi->common;
1450   if (cpi->prev_partition == NULL) {
1451     CHECK_MEM_ERROR(cm, cpi->prev_partition,
1452                     (BLOCK_SIZE *)vpx_calloc(cm->mi_stride * cm->mi_rows,
1453                                              sizeof(*cpi->prev_partition)));
1454   }
1455   if (cpi->prev_segment_id == NULL) {
1456     CHECK_MEM_ERROR(
1457         cm, cpi->prev_segment_id,
1458         (int8_t *)vpx_calloc((cm->mi_stride >> 3) * ((cm->mi_rows >> 3) + 1),
1459                              sizeof(*cpi->prev_segment_id)));
1460   }
1461   if (cpi->prev_variance_low == NULL) {
1462     CHECK_MEM_ERROR(cm, cpi->prev_variance_low,
1463                     (uint8_t *)vpx_calloc(
1464                         (cm->mi_stride >> 3) * ((cm->mi_rows >> 3) + 1) * 25,
1465                         sizeof(*cpi->prev_variance_low)));
1466   }
1467   if (cpi->copied_frame_cnt == NULL) {
1468     CHECK_MEM_ERROR(
1469         cm, cpi->copied_frame_cnt,
1470         (uint8_t *)vpx_calloc((cm->mi_stride >> 3) * ((cm->mi_rows >> 3) + 1),
1471                               sizeof(*cpi->copied_frame_cnt)));
1472   }
1473 }
1474
1475 void vp9_change_config(struct VP9_COMP *cpi, const VP9EncoderConfig *oxcf) {
1476   VP9_COMMON *const cm = &cpi->common;
1477   RATE_CONTROL *const rc = &cpi->rc;
1478   int last_w = cpi->oxcf.width;
1479   int last_h = cpi->oxcf.height;
1480
1481   if (cm->profile != oxcf->profile) cm->profile = oxcf->profile;
1482   cm->bit_depth = oxcf->bit_depth;
1483   cm->color_space = oxcf->color_space;
1484   cm->color_range = oxcf->color_range;
1485
1486   cpi->target_level = oxcf->target_level;
1487   cpi->keep_level_stats = oxcf->target_level != LEVEL_MAX;
1488   set_level_constraint(&cpi->level_constraint,
1489                        get_level_index(cpi->target_level));
1490
1491   if (cm->profile <= PROFILE_1)
1492     assert(cm->bit_depth == VPX_BITS_8);
1493   else
1494     assert(cm->bit_depth > VPX_BITS_8);
1495
1496   cpi->oxcf = *oxcf;
1497 #if CONFIG_VP9_HIGHBITDEPTH
1498   cpi->td.mb.e_mbd.bd = (int)cm->bit_depth;
1499 #endif  // CONFIG_VP9_HIGHBITDEPTH
1500
1501   if ((oxcf->pass == 0) && (oxcf->rc_mode == VPX_Q)) {
1502     rc->baseline_gf_interval = FIXED_GF_INTERVAL;
1503   } else {
1504     rc->baseline_gf_interval = (MIN_GF_INTERVAL + MAX_GF_INTERVAL) / 2;
1505   }
1506
1507   cpi->refresh_golden_frame = 0;
1508   cpi->refresh_last_frame = 1;
1509   cm->refresh_frame_context = 1;
1510   cm->reset_frame_context = 0;
1511
1512   vp9_reset_segment_features(&cm->seg);
1513   vp9_set_high_precision_mv(cpi, 0);
1514
1515   {
1516     int i;
1517
1518     for (i = 0; i < MAX_SEGMENTS; i++)
1519       cpi->segment_encode_breakout[i] = cpi->oxcf.encode_breakout;
1520   }
1521   cpi->encode_breakout = cpi->oxcf.encode_breakout;
1522
1523   set_rc_buffer_sizes(rc, &cpi->oxcf);
1524
1525   // Under a configuration change, where maximum_buffer_size may change,
1526   // keep buffer level clipped to the maximum allowed buffer size.
1527   rc->bits_off_target = VPXMIN(rc->bits_off_target, rc->maximum_buffer_size);
1528   rc->buffer_level = VPXMIN(rc->buffer_level, rc->maximum_buffer_size);
1529
1530   // Set up frame rate and related parameters rate control values.
1531   vp9_new_framerate(cpi, cpi->framerate);
1532
1533   // Set absolute upper and lower quality limits
1534   rc->worst_quality = cpi->oxcf.worst_allowed_q;
1535   rc->best_quality = cpi->oxcf.best_allowed_q;
1536
1537   cm->interp_filter = cpi->sf.default_interp_filter;
1538
1539   if (cpi->oxcf.render_width > 0 && cpi->oxcf.render_height > 0) {
1540     cm->render_width = cpi->oxcf.render_width;
1541     cm->render_height = cpi->oxcf.render_height;
1542   } else {
1543     cm->render_width = cpi->oxcf.width;
1544     cm->render_height = cpi->oxcf.height;
1545   }
1546   if (last_w != cpi->oxcf.width || last_h != cpi->oxcf.height) {
1547     cm->width = cpi->oxcf.width;
1548     cm->height = cpi->oxcf.height;
1549     cpi->external_resize = 1;
1550   }
1551
1552   if (cpi->initial_width) {
1553     int new_mi_size = 0;
1554     vp9_set_mb_mi(cm, cm->width, cm->height);
1555     new_mi_size = cm->mi_stride * calc_mi_size(cm->mi_rows);
1556     if (cm->mi_alloc_size < new_mi_size) {
1557       vp9_free_context_buffers(cm);
1558       alloc_compressor_data(cpi);
1559       realloc_segmentation_maps(cpi);
1560       cpi->initial_width = cpi->initial_height = 0;
1561       cpi->external_resize = 0;
1562     } else if (cm->mi_alloc_size == new_mi_size &&
1563                (cpi->oxcf.width > last_w || cpi->oxcf.height > last_h)) {
1564       vp9_alloc_loop_filter(cm);
1565     }
1566   }
1567
1568   if (cm->current_video_frame == 0 || last_w != cpi->oxcf.width ||
1569       last_h != cpi->oxcf.height)
1570     update_frame_size(cpi);
1571
1572   if (last_w != cpi->oxcf.width || last_h != cpi->oxcf.height) {
1573     memset(cpi->consec_zero_mv, 0,
1574            cm->mi_rows * cm->mi_cols * sizeof(*cpi->consec_zero_mv));
1575     if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ)
1576       vp9_cyclic_refresh_reset_resize(cpi);
1577   }
1578
1579   if ((cpi->svc.number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) ||
1580       ((cpi->svc.number_temporal_layers > 1 ||
1581         cpi->svc.number_spatial_layers > 1) &&
1582        cpi->oxcf.pass != 1)) {
1583     vp9_update_layer_context_change_config(cpi,
1584                                            (int)cpi->oxcf.target_bandwidth);
1585   }
1586
1587   cpi->alt_ref_source = NULL;
1588   rc->is_src_frame_alt_ref = 0;
1589
1590 #if 0
1591   // Experimental RD Code
1592   cpi->frame_distortion = 0;
1593   cpi->last_frame_distortion = 0;
1594 #endif
1595
1596   set_tile_limits(cpi);
1597
1598   cpi->ext_refresh_frame_flags_pending = 0;
1599   cpi->ext_refresh_frame_context_pending = 0;
1600
1601 #if CONFIG_VP9_HIGHBITDEPTH
1602   highbd_set_var_fns(cpi);
1603 #endif
1604
1605   vp9_set_row_mt(cpi);
1606 }
1607
1608 #ifndef M_LOG2_E
1609 #define M_LOG2_E 0.693147180559945309417
1610 #endif
1611 #define log2f(x) (log(x) / (float)M_LOG2_E)
1612
1613 /***********************************************************************
1614  * Read before modifying 'cal_nmvjointsadcost' or 'cal_nmvsadcosts'    *
1615  ***********************************************************************
1616  * The following 2 functions ('cal_nmvjointsadcost' and                *
1617  * 'cal_nmvsadcosts') are used to calculate cost lookup tables         *
1618  * used by 'vp9_diamond_search_sad'. The C implementation of the       *
1619  * function is generic, but the AVX intrinsics optimised version       *
1620  * relies on the following properties of the computed tables:          *
1621  * For cal_nmvjointsadcost:                                            *
1622  *   - mvjointsadcost[1] == mvjointsadcost[2] == mvjointsadcost[3]     *
1623  * For cal_nmvsadcosts:                                                *
1624  *   - For all i: mvsadcost[0][i] == mvsadcost[1][i]                   *
1625  *         (Equal costs for both components)                           *
1626  *   - For all i: mvsadcost[0][i] == mvsadcost[0][-i]                  *
1627  *         (Cost function is even)                                     *
1628  * If these do not hold, then the AVX optimised version of the         *
1629  * 'vp9_diamond_search_sad' function cannot be used as it is, in which *
1630  * case you can revert to using the C function instead.                *
1631  ***********************************************************************/
1632
1633 static void cal_nmvjointsadcost(int *mvjointsadcost) {
1634   /*********************************************************************
1635    * Warning: Read the comments above before modifying this function   *
1636    *********************************************************************/
1637   mvjointsadcost[0] = 600;
1638   mvjointsadcost[1] = 300;
1639   mvjointsadcost[2] = 300;
1640   mvjointsadcost[3] = 300;
1641 }
1642
1643 static void cal_nmvsadcosts(int *mvsadcost[2]) {
1644   /*********************************************************************
1645    * Warning: Read the comments above before modifying this function   *
1646    *********************************************************************/
1647   int i = 1;
1648
1649   mvsadcost[0][0] = 0;
1650   mvsadcost[1][0] = 0;
1651
1652   do {
1653     double z = 256 * (2 * (log2f(8 * i) + .6));
1654     mvsadcost[0][i] = (int)z;
1655     mvsadcost[1][i] = (int)z;
1656     mvsadcost[0][-i] = (int)z;
1657     mvsadcost[1][-i] = (int)z;
1658   } while (++i <= MV_MAX);
1659 }
1660
1661 static void cal_nmvsadcosts_hp(int *mvsadcost[2]) {
1662   int i = 1;
1663
1664   mvsadcost[0][0] = 0;
1665   mvsadcost[1][0] = 0;
1666
1667   do {
1668     double z = 256 * (2 * (log2f(8 * i) + .6));
1669     mvsadcost[0][i] = (int)z;
1670     mvsadcost[1][i] = (int)z;
1671     mvsadcost[0][-i] = (int)z;
1672     mvsadcost[1][-i] = (int)z;
1673   } while (++i <= MV_MAX);
1674 }
1675
1676 VP9_COMP *vp9_create_compressor(VP9EncoderConfig *oxcf,
1677                                 BufferPool *const pool) {
1678   unsigned int i;
1679   VP9_COMP *volatile const cpi = vpx_memalign(32, sizeof(VP9_COMP));
1680   VP9_COMMON *volatile const cm = cpi != NULL ? &cpi->common : NULL;
1681
1682   if (!cm) return NULL;
1683
1684   vp9_zero(*cpi);
1685
1686   if (setjmp(cm->error.jmp)) {
1687     cm->error.setjmp = 0;
1688     vp9_remove_compressor(cpi);
1689     return 0;
1690   }
1691
1692   cm->error.setjmp = 1;
1693   cm->alloc_mi = vp9_enc_alloc_mi;
1694   cm->free_mi = vp9_enc_free_mi;
1695   cm->setup_mi = vp9_enc_setup_mi;
1696
1697   CHECK_MEM_ERROR(cm, cm->fc, (FRAME_CONTEXT *)vpx_calloc(1, sizeof(*cm->fc)));
1698   CHECK_MEM_ERROR(
1699       cm, cm->frame_contexts,
1700       (FRAME_CONTEXT *)vpx_calloc(FRAME_CONTEXTS, sizeof(*cm->frame_contexts)));
1701
1702   cpi->use_svc = 0;
1703   cpi->resize_state = ORIG;
1704   cpi->external_resize = 0;
1705   cpi->resize_avg_qp = 0;
1706   cpi->resize_buffer_underflow = 0;
1707   cpi->use_skin_detection = 0;
1708   cpi->common.buffer_pool = pool;
1709
1710   cpi->force_update_segmentation = 0;
1711
1712   init_config(cpi, oxcf);
1713   vp9_rc_init(&cpi->oxcf, oxcf->pass, &cpi->rc);
1714
1715   cm->current_video_frame = 0;
1716   cpi->partition_search_skippable_frame = 0;
1717   cpi->tile_data = NULL;
1718
1719   realloc_segmentation_maps(cpi);
1720
1721   CHECK_MEM_ERROR(cm, cpi->alt_ref_aq, vp9_alt_ref_aq_create());
1722
1723   CHECK_MEM_ERROR(
1724       cm, cpi->consec_zero_mv,
1725       vpx_calloc(cm->mi_rows * cm->mi_cols, sizeof(*cpi->consec_zero_mv)));
1726
1727   CHECK_MEM_ERROR(cm, cpi->nmvcosts[0],
1728                   vpx_calloc(MV_VALS, sizeof(*cpi->nmvcosts[0])));
1729   CHECK_MEM_ERROR(cm, cpi->nmvcosts[1],
1730                   vpx_calloc(MV_VALS, sizeof(*cpi->nmvcosts[1])));
1731   CHECK_MEM_ERROR(cm, cpi->nmvcosts_hp[0],
1732                   vpx_calloc(MV_VALS, sizeof(*cpi->nmvcosts_hp[0])));
1733   CHECK_MEM_ERROR(cm, cpi->nmvcosts_hp[1],
1734                   vpx_calloc(MV_VALS, sizeof(*cpi->nmvcosts_hp[1])));
1735   CHECK_MEM_ERROR(cm, cpi->nmvsadcosts[0],
1736                   vpx_calloc(MV_VALS, sizeof(*cpi->nmvsadcosts[0])));
1737   CHECK_MEM_ERROR(cm, cpi->nmvsadcosts[1],
1738                   vpx_calloc(MV_VALS, sizeof(*cpi->nmvsadcosts[1])));
1739   CHECK_MEM_ERROR(cm, cpi->nmvsadcosts_hp[0],
1740                   vpx_calloc(MV_VALS, sizeof(*cpi->nmvsadcosts_hp[0])));
1741   CHECK_MEM_ERROR(cm, cpi->nmvsadcosts_hp[1],
1742                   vpx_calloc(MV_VALS, sizeof(*cpi->nmvsadcosts_hp[1])));
1743
1744   for (i = 0; i < (sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]));
1745        i++) {
1746     CHECK_MEM_ERROR(
1747         cm, cpi->mbgraph_stats[i].mb_stats,
1748         vpx_calloc(cm->MBs * sizeof(*cpi->mbgraph_stats[i].mb_stats), 1));
1749   }
1750
1751 #if CONFIG_FP_MB_STATS
1752   cpi->use_fp_mb_stats = 0;
1753   if (cpi->use_fp_mb_stats) {
1754     // a place holder used to store the first pass mb stats in the first pass
1755     CHECK_MEM_ERROR(cm, cpi->twopass.frame_mb_stats_buf,
1756                     vpx_calloc(cm->MBs * sizeof(uint8_t), 1));
1757   } else {
1758     cpi->twopass.frame_mb_stats_buf = NULL;
1759   }
1760 #endif
1761
1762   cpi->refresh_alt_ref_frame = 0;
1763   cpi->multi_arf_last_grp_enabled = 0;
1764
1765   cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
1766
1767   init_level_info(&cpi->level_info);
1768   init_level_constraint(&cpi->level_constraint);
1769
1770 #if CONFIG_INTERNAL_STATS
1771   cpi->b_calculate_blockiness = 1;
1772   cpi->b_calculate_consistency = 1;
1773   cpi->total_inconsistency = 0;
1774   cpi->psnr.worst = 100.0;
1775   cpi->worst_ssim = 100.0;
1776
1777   cpi->count = 0;
1778   cpi->bytes = 0;
1779
1780   if (cpi->b_calculate_psnr) {
1781     cpi->total_sq_error = 0;
1782     cpi->total_samples = 0;
1783
1784     cpi->totalp_sq_error = 0;
1785     cpi->totalp_samples = 0;
1786
1787     cpi->tot_recode_hits = 0;
1788     cpi->summed_quality = 0;
1789     cpi->summed_weights = 0;
1790     cpi->summedp_quality = 0;
1791     cpi->summedp_weights = 0;
1792   }
1793
1794   cpi->fastssim.worst = 100.0;
1795
1796   cpi->psnrhvs.worst = 100.0;
1797
1798   if (cpi->b_calculate_blockiness) {
1799     cpi->total_blockiness = 0;
1800     cpi->worst_blockiness = 0.0;
1801   }
1802
1803   if (cpi->b_calculate_consistency) {
1804     CHECK_MEM_ERROR(cm, cpi->ssim_vars,
1805                     vpx_malloc(sizeof(*cpi->ssim_vars) * 4 *
1806                                cpi->common.mi_rows * cpi->common.mi_cols));
1807     cpi->worst_consistency = 100.0;
1808   }
1809
1810 #endif
1811
1812   cpi->first_time_stamp_ever = INT64_MAX;
1813
1814   /*********************************************************************
1815    * Warning: Read the comments around 'cal_nmvjointsadcost' and       *
1816    * 'cal_nmvsadcosts' before modifying how these tables are computed. *
1817    *********************************************************************/
1818   cal_nmvjointsadcost(cpi->td.mb.nmvjointsadcost);
1819   cpi->td.mb.nmvcost[0] = &cpi->nmvcosts[0][MV_MAX];
1820   cpi->td.mb.nmvcost[1] = &cpi->nmvcosts[1][MV_MAX];
1821   cpi->td.mb.nmvsadcost[0] = &cpi->nmvsadcosts[0][MV_MAX];
1822   cpi->td.mb.nmvsadcost[1] = &cpi->nmvsadcosts[1][MV_MAX];
1823   cal_nmvsadcosts(cpi->td.mb.nmvsadcost);
1824
1825   cpi->td.mb.nmvcost_hp[0] = &cpi->nmvcosts_hp[0][MV_MAX];
1826   cpi->td.mb.nmvcost_hp[1] = &cpi->nmvcosts_hp[1][MV_MAX];
1827   cpi->td.mb.nmvsadcost_hp[0] = &cpi->nmvsadcosts_hp[0][MV_MAX];
1828   cpi->td.mb.nmvsadcost_hp[1] = &cpi->nmvsadcosts_hp[1][MV_MAX];
1829   cal_nmvsadcosts_hp(cpi->td.mb.nmvsadcost_hp);
1830
1831 #if CONFIG_VP9_TEMPORAL_DENOISING
1832 #ifdef OUTPUT_YUV_DENOISED
1833   yuv_denoised_file = fopen("denoised.yuv", "ab");
1834 #endif
1835 #endif
1836 #ifdef OUTPUT_YUV_SKINMAP
1837   yuv_skinmap_file = fopen("skinmap.yuv", "ab");
1838 #endif
1839 #ifdef OUTPUT_YUV_REC
1840   yuv_rec_file = fopen("rec.yuv", "wb");
1841 #endif
1842
1843 #if 0
1844   framepsnr = fopen("framepsnr.stt", "a");
1845   kf_list = fopen("kf_list.stt", "w");
1846 #endif
1847
1848   cpi->allow_encode_breakout = ENCODE_BREAKOUT_ENABLED;
1849
1850   if (oxcf->pass == 1) {
1851     vp9_init_first_pass(cpi);
1852   } else if (oxcf->pass == 2) {
1853     const size_t packet_sz = sizeof(FIRSTPASS_STATS);
1854     const int packets = (int)(oxcf->two_pass_stats_in.sz / packet_sz);
1855
1856     if (cpi->svc.number_spatial_layers > 1 ||
1857         cpi->svc.number_temporal_layers > 1) {
1858       FIRSTPASS_STATS *const stats = oxcf->two_pass_stats_in.buf;
1859       FIRSTPASS_STATS *stats_copy[VPX_SS_MAX_LAYERS] = { 0 };
1860       int i;
1861
1862       for (i = 0; i < oxcf->ss_number_layers; ++i) {
1863         FIRSTPASS_STATS *const last_packet_for_layer =
1864             &stats[packets - oxcf->ss_number_layers + i];
1865         const int layer_id = (int)last_packet_for_layer->spatial_layer_id;
1866         const int packets_in_layer = (int)last_packet_for_layer->count + 1;
1867         if (layer_id >= 0 && layer_id < oxcf->ss_number_layers) {
1868           LAYER_CONTEXT *const lc = &cpi->svc.layer_context[layer_id];
1869
1870           vpx_free(lc->rc_twopass_stats_in.buf);
1871
1872           lc->rc_twopass_stats_in.sz = packets_in_layer * packet_sz;
1873           CHECK_MEM_ERROR(cm, lc->rc_twopass_stats_in.buf,
1874                           vpx_malloc(lc->rc_twopass_stats_in.sz));
1875           lc->twopass.stats_in_start = lc->rc_twopass_stats_in.buf;
1876           lc->twopass.stats_in = lc->twopass.stats_in_start;
1877           lc->twopass.stats_in_end =
1878               lc->twopass.stats_in_start + packets_in_layer - 1;
1879           stats_copy[layer_id] = lc->rc_twopass_stats_in.buf;
1880         }
1881       }
1882
1883       for (i = 0; i < packets; ++i) {
1884         const int layer_id = (int)stats[i].spatial_layer_id;
1885         if (layer_id >= 0 && layer_id < oxcf->ss_number_layers &&
1886             stats_copy[layer_id] != NULL) {
1887           *stats_copy[layer_id] = stats[i];
1888           ++stats_copy[layer_id];
1889         }
1890       }
1891
1892       vp9_init_second_pass_spatial_svc(cpi);
1893     } else {
1894 #if CONFIG_FP_MB_STATS
1895       if (cpi->use_fp_mb_stats) {
1896         const size_t psz = cpi->common.MBs * sizeof(uint8_t);
1897         const int ps = (int)(oxcf->firstpass_mb_stats_in.sz / psz);
1898
1899         cpi->twopass.firstpass_mb_stats.mb_stats_start =
1900             oxcf->firstpass_mb_stats_in.buf;
1901         cpi->twopass.firstpass_mb_stats.mb_stats_end =
1902             cpi->twopass.firstpass_mb_stats.mb_stats_start +
1903             (ps - 1) * cpi->common.MBs * sizeof(uint8_t);
1904       }
1905 #endif
1906
1907       cpi->twopass.stats_in_start = oxcf->two_pass_stats_in.buf;
1908       cpi->twopass.stats_in = cpi->twopass.stats_in_start;
1909       cpi->twopass.stats_in_end = &cpi->twopass.stats_in[packets - 1];
1910
1911       vp9_init_second_pass(cpi);
1912     }
1913   }
1914
1915   vp9_set_speed_features_framesize_independent(cpi);
1916   vp9_set_speed_features_framesize_dependent(cpi);
1917
1918   // Allocate memory to store variances for a frame.
1919   CHECK_MEM_ERROR(cm, cpi->source_diff_var, vpx_calloc(cm->MBs, sizeof(diff)));
1920   cpi->source_var_thresh = 0;
1921   cpi->frames_till_next_var_check = 0;
1922
1923 #define BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX3F, SDX8F, SDX4DF) \
1924   cpi->fn_ptr[BT].sdf = SDF;                                    \
1925   cpi->fn_ptr[BT].sdaf = SDAF;                                  \
1926   cpi->fn_ptr[BT].vf = VF;                                      \
1927   cpi->fn_ptr[BT].svf = SVF;                                    \
1928   cpi->fn_ptr[BT].svaf = SVAF;                                  \
1929   cpi->fn_ptr[BT].sdx3f = SDX3F;                                \
1930   cpi->fn_ptr[BT].sdx8f = SDX8F;                                \
1931   cpi->fn_ptr[BT].sdx4df = SDX4DF;
1932
1933   BFP(BLOCK_32X16, vpx_sad32x16, vpx_sad32x16_avg, vpx_variance32x16,
1934       vpx_sub_pixel_variance32x16, vpx_sub_pixel_avg_variance32x16, NULL, NULL,
1935       vpx_sad32x16x4d)
1936
1937   BFP(BLOCK_16X32, vpx_sad16x32, vpx_sad16x32_avg, vpx_variance16x32,
1938       vpx_sub_pixel_variance16x32, vpx_sub_pixel_avg_variance16x32, NULL, NULL,
1939       vpx_sad16x32x4d)
1940
1941   BFP(BLOCK_64X32, vpx_sad64x32, vpx_sad64x32_avg, vpx_variance64x32,
1942       vpx_sub_pixel_variance64x32, vpx_sub_pixel_avg_variance64x32, NULL, NULL,
1943       vpx_sad64x32x4d)
1944
1945   BFP(BLOCK_32X64, vpx_sad32x64, vpx_sad32x64_avg, vpx_variance32x64,
1946       vpx_sub_pixel_variance32x64, vpx_sub_pixel_avg_variance32x64, NULL, NULL,
1947       vpx_sad32x64x4d)
1948
1949   BFP(BLOCK_32X32, vpx_sad32x32, vpx_sad32x32_avg, vpx_variance32x32,
1950       vpx_sub_pixel_variance32x32, vpx_sub_pixel_avg_variance32x32,
1951       vpx_sad32x32x3, vpx_sad32x32x8, vpx_sad32x32x4d)
1952
1953   BFP(BLOCK_64X64, vpx_sad64x64, vpx_sad64x64_avg, vpx_variance64x64,
1954       vpx_sub_pixel_variance64x64, vpx_sub_pixel_avg_variance64x64,
1955       vpx_sad64x64x3, vpx_sad64x64x8, vpx_sad64x64x4d)
1956
1957   BFP(BLOCK_16X16, vpx_sad16x16, vpx_sad16x16_avg, vpx_variance16x16,
1958       vpx_sub_pixel_variance16x16, vpx_sub_pixel_avg_variance16x16,
1959       vpx_sad16x16x3, vpx_sad16x16x8, vpx_sad16x16x4d)
1960
1961   BFP(BLOCK_16X8, vpx_sad16x8, vpx_sad16x8_avg, vpx_variance16x8,
1962       vpx_sub_pixel_variance16x8, vpx_sub_pixel_avg_variance16x8, vpx_sad16x8x3,
1963       vpx_sad16x8x8, vpx_sad16x8x4d)
1964
1965   BFP(BLOCK_8X16, vpx_sad8x16, vpx_sad8x16_avg, vpx_variance8x16,
1966       vpx_sub_pixel_variance8x16, vpx_sub_pixel_avg_variance8x16, vpx_sad8x16x3,
1967       vpx_sad8x16x8, vpx_sad8x16x4d)
1968
1969   BFP(BLOCK_8X8, vpx_sad8x8, vpx_sad8x8_avg, vpx_variance8x8,
1970       vpx_sub_pixel_variance8x8, vpx_sub_pixel_avg_variance8x8, vpx_sad8x8x3,
1971       vpx_sad8x8x8, vpx_sad8x8x4d)
1972
1973   BFP(BLOCK_8X4, vpx_sad8x4, vpx_sad8x4_avg, vpx_variance8x4,
1974       vpx_sub_pixel_variance8x4, vpx_sub_pixel_avg_variance8x4, NULL,
1975       vpx_sad8x4x8, vpx_sad8x4x4d)
1976
1977   BFP(BLOCK_4X8, vpx_sad4x8, vpx_sad4x8_avg, vpx_variance4x8,
1978       vpx_sub_pixel_variance4x8, vpx_sub_pixel_avg_variance4x8, NULL,
1979       vpx_sad4x8x8, vpx_sad4x8x4d)
1980
1981   BFP(BLOCK_4X4, vpx_sad4x4, vpx_sad4x4_avg, vpx_variance4x4,
1982       vpx_sub_pixel_variance4x4, vpx_sub_pixel_avg_variance4x4, vpx_sad4x4x3,
1983       vpx_sad4x4x8, vpx_sad4x4x4d)
1984
1985 #if CONFIG_VP9_HIGHBITDEPTH
1986   highbd_set_var_fns(cpi);
1987 #endif
1988
1989   /* vp9_init_quantizer() is first called here. Add check in
1990    * vp9_frame_init_quantizer() so that vp9_init_quantizer is only
1991    * called later when needed. This will avoid unnecessary calls of
1992    * vp9_init_quantizer() for every frame.
1993    */
1994   vp9_init_quantizer(cpi);
1995
1996   vp9_loop_filter_init(cm);
1997
1998   cm->error.setjmp = 0;
1999
2000   return cpi;
2001 }
2002
2003 #if CONFIG_INTERNAL_STATS
2004 #define SNPRINT(H, T) snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T))
2005
2006 #define SNPRINT2(H, T, V) \
2007   snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T), (V))
2008 #endif  // CONFIG_INTERNAL_STATS
2009
2010 void vp9_remove_compressor(VP9_COMP *cpi) {
2011   VP9_COMMON *cm;
2012   unsigned int i;
2013   int t;
2014
2015   if (!cpi) return;
2016
2017   cm = &cpi->common;
2018   if (cm->current_video_frame > 0) {
2019 #if CONFIG_INTERNAL_STATS
2020     vpx_clear_system_state();
2021
2022     if (cpi->oxcf.pass != 1) {
2023       char headings[512] = { 0 };
2024       char results[512] = { 0 };
2025       FILE *f = fopen("opsnr.stt", "a");
2026       double time_encoded =
2027           (cpi->last_end_time_stamp_seen - cpi->first_time_stamp_ever) /
2028           10000000.000;
2029       double total_encode_time =
2030           (cpi->time_receive_data + cpi->time_compress_data) / 1000.000;
2031       const double dr =
2032           (double)cpi->bytes * (double)8 / (double)1000 / time_encoded;
2033       const double peak = (double)((1 << cpi->oxcf.input_bit_depth) - 1);
2034       const double target_rate = (double)cpi->oxcf.target_bandwidth / 1000;
2035       const double rate_err = ((100.0 * (dr - target_rate)) / target_rate);
2036
2037       if (cpi->b_calculate_psnr) {
2038         const double total_psnr = vpx_sse_to_psnr(
2039             (double)cpi->total_samples, peak, (double)cpi->total_sq_error);
2040         const double totalp_psnr = vpx_sse_to_psnr(
2041             (double)cpi->totalp_samples, peak, (double)cpi->totalp_sq_error);
2042         const double total_ssim =
2043             100 * pow(cpi->summed_quality / cpi->summed_weights, 8.0);
2044         const double totalp_ssim =
2045             100 * pow(cpi->summedp_quality / cpi->summedp_weights, 8.0);
2046
2047         snprintf(headings, sizeof(headings),
2048                  "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\t"
2049                  "VPXSSIM\tVPSSIMP\tFASTSIM\tPSNRHVS\t"
2050                  "WstPsnr\tWstSsim\tWstFast\tWstHVS");
2051         snprintf(results, sizeof(results),
2052                  "%7.2f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
2053                  "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
2054                  "%7.3f\t%7.3f\t%7.3f\t%7.3f",
2055                  dr, cpi->psnr.stat[ALL] / cpi->count, total_psnr,
2056                  cpi->psnrp.stat[ALL] / cpi->count, totalp_psnr, total_ssim,
2057                  totalp_ssim, cpi->fastssim.stat[ALL] / cpi->count,
2058                  cpi->psnrhvs.stat[ALL] / cpi->count, cpi->psnr.worst,
2059                  cpi->worst_ssim, cpi->fastssim.worst, cpi->psnrhvs.worst);
2060
2061         if (cpi->b_calculate_blockiness) {
2062           SNPRINT(headings, "\t  Block\tWstBlck");
2063           SNPRINT2(results, "\t%7.3f", cpi->total_blockiness / cpi->count);
2064           SNPRINT2(results, "\t%7.3f", cpi->worst_blockiness);
2065         }
2066
2067         if (cpi->b_calculate_consistency) {
2068           double consistency =
2069               vpx_sse_to_psnr((double)cpi->totalp_samples, peak,
2070                               (double)cpi->total_inconsistency);
2071
2072           SNPRINT(headings, "\tConsist\tWstCons");
2073           SNPRINT2(results, "\t%7.3f", consistency);
2074           SNPRINT2(results, "\t%7.3f", cpi->worst_consistency);
2075         }
2076
2077         fprintf(f, "%s\t    Time\tRcErr\tAbsErr\n", headings);
2078         fprintf(f, "%s\t%8.0f\t%7.2f\t%7.2f\n", results, total_encode_time,
2079                 rate_err, fabs(rate_err));
2080       }
2081
2082       fclose(f);
2083     }
2084
2085 #endif
2086
2087 #if 0
2088     {
2089       printf("\n_pick_loop_filter_level:%d\n", cpi->time_pick_lpf / 1000);
2090       printf("\n_frames recive_data encod_mb_row compress_frame  Total\n");
2091       printf("%6d %10ld %10ld %10ld %10ld\n", cpi->common.current_video_frame,
2092              cpi->time_receive_data / 1000, cpi->time_encode_sb_row / 1000,
2093              cpi->time_compress_data / 1000,
2094              (cpi->time_receive_data + cpi->time_compress_data) / 1000);
2095     }
2096 #endif
2097   }
2098
2099 #if CONFIG_VP9_TEMPORAL_DENOISING
2100   vp9_denoiser_free(&(cpi->denoiser));
2101 #endif
2102
2103   for (t = 0; t < cpi->num_workers; ++t) {
2104     VPxWorker *const worker = &cpi->workers[t];
2105     EncWorkerData *const thread_data = &cpi->tile_thr_data[t];
2106
2107     // Deallocate allocated threads.
2108     vpx_get_worker_interface()->end(worker);
2109
2110     // Deallocate allocated thread data.
2111     if (t < cpi->num_workers - 1) {
2112       vpx_free(thread_data->td->counts);
2113       vp9_free_pc_tree(thread_data->td);
2114       vpx_free(thread_data->td);
2115     }
2116   }
2117   vpx_free(cpi->tile_thr_data);
2118   vpx_free(cpi->workers);
2119   vp9_row_mt_mem_dealloc(cpi);
2120
2121   if (cpi->num_workers > 1) {
2122     vp9_loop_filter_dealloc(&cpi->lf_row_sync);
2123     vp9_bitstream_encode_tiles_buffer_dealloc(cpi);
2124   }
2125
2126   vp9_alt_ref_aq_destroy(cpi->alt_ref_aq);
2127
2128   dealloc_compressor_data(cpi);
2129
2130   for (i = 0; i < sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]);
2131        ++i) {
2132     vpx_free(cpi->mbgraph_stats[i].mb_stats);
2133   }
2134
2135 #if CONFIG_FP_MB_STATS
2136   if (cpi->use_fp_mb_stats) {
2137     vpx_free(cpi->twopass.frame_mb_stats_buf);
2138     cpi->twopass.frame_mb_stats_buf = NULL;
2139   }
2140 #endif
2141
2142   vp9_remove_common(cm);
2143   vp9_free_ref_frame_buffers(cm->buffer_pool);
2144 #if CONFIG_VP9_POSTPROC
2145   vp9_free_postproc_buffers(cm);
2146 #endif
2147   vpx_free(cpi);
2148
2149 #if CONFIG_VP9_TEMPORAL_DENOISING
2150 #ifdef OUTPUT_YUV_DENOISED
2151   fclose(yuv_denoised_file);
2152 #endif
2153 #endif
2154 #ifdef OUTPUT_YUV_SKINMAP
2155   fclose(yuv_skinmap_file);
2156 #endif
2157 #ifdef OUTPUT_YUV_REC
2158   fclose(yuv_rec_file);
2159 #endif
2160
2161 #if 0
2162
2163   if (keyfile)
2164     fclose(keyfile);
2165
2166   if (framepsnr)
2167     fclose(framepsnr);
2168
2169   if (kf_list)
2170     fclose(kf_list);
2171
2172 #endif
2173 }
2174
2175 static void generate_psnr_packet(VP9_COMP *cpi) {
2176   struct vpx_codec_cx_pkt pkt;
2177   int i;
2178   PSNR_STATS psnr;
2179 #if CONFIG_VP9_HIGHBITDEPTH
2180   vpx_calc_highbd_psnr(cpi->raw_source_frame, cpi->common.frame_to_show, &psnr,
2181                        cpi->td.mb.e_mbd.bd, cpi->oxcf.input_bit_depth);
2182 #else
2183   vpx_calc_psnr(cpi->raw_source_frame, cpi->common.frame_to_show, &psnr);
2184 #endif
2185
2186   for (i = 0; i < 4; ++i) {
2187     pkt.data.psnr.samples[i] = psnr.samples[i];
2188     pkt.data.psnr.sse[i] = psnr.sse[i];
2189     pkt.data.psnr.psnr[i] = psnr.psnr[i];
2190   }
2191   pkt.kind = VPX_CODEC_PSNR_PKT;
2192   if (cpi->use_svc)
2193     cpi->svc
2194         .layer_context[cpi->svc.spatial_layer_id *
2195                        cpi->svc.number_temporal_layers]
2196         .psnr_pkt = pkt.data.psnr;
2197   else
2198     vpx_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
2199 }
2200
2201 int vp9_use_as_reference(VP9_COMP *cpi, int ref_frame_flags) {
2202   if (ref_frame_flags > 7) return -1;
2203
2204   cpi->ref_frame_flags = ref_frame_flags;
2205   return 0;
2206 }
2207
2208 void vp9_update_reference(VP9_COMP *cpi, int ref_frame_flags) {
2209   cpi->ext_refresh_golden_frame = (ref_frame_flags & VP9_GOLD_FLAG) != 0;
2210   cpi->ext_refresh_alt_ref_frame = (ref_frame_flags & VP9_ALT_FLAG) != 0;
2211   cpi->ext_refresh_last_frame = (ref_frame_flags & VP9_LAST_FLAG) != 0;
2212   cpi->ext_refresh_frame_flags_pending = 1;
2213 }
2214
2215 static YV12_BUFFER_CONFIG *get_vp9_ref_frame_buffer(
2216     VP9_COMP *cpi, VP9_REFFRAME ref_frame_flag) {
2217   MV_REFERENCE_FRAME ref_frame = NONE;
2218   if (ref_frame_flag == VP9_LAST_FLAG)
2219     ref_frame = LAST_FRAME;
2220   else if (ref_frame_flag == VP9_GOLD_FLAG)
2221     ref_frame = GOLDEN_FRAME;
2222   else if (ref_frame_flag == VP9_ALT_FLAG)
2223     ref_frame = ALTREF_FRAME;
2224
2225   return ref_frame == NONE ? NULL : get_ref_frame_buffer(cpi, ref_frame);
2226 }
2227
2228 int vp9_copy_reference_enc(VP9_COMP *cpi, VP9_REFFRAME ref_frame_flag,
2229                            YV12_BUFFER_CONFIG *sd) {
2230   YV12_BUFFER_CONFIG *cfg = get_vp9_ref_frame_buffer(cpi, ref_frame_flag);
2231   if (cfg) {
2232     vp8_yv12_copy_frame(cfg, sd);
2233     return 0;
2234   } else {
2235     return -1;
2236   }
2237 }
2238
2239 int vp9_set_reference_enc(VP9_COMP *cpi, VP9_REFFRAME ref_frame_flag,
2240                           YV12_BUFFER_CONFIG *sd) {
2241   YV12_BUFFER_CONFIG *cfg = get_vp9_ref_frame_buffer(cpi, ref_frame_flag);
2242   if (cfg) {
2243     vp8_yv12_copy_frame(sd, cfg);
2244     return 0;
2245   } else {
2246     return -1;
2247   }
2248 }
2249
2250 int vp9_update_entropy(VP9_COMP *cpi, int update) {
2251   cpi->ext_refresh_frame_context = update;
2252   cpi->ext_refresh_frame_context_pending = 1;
2253   return 0;
2254 }
2255
2256 #if defined(OUTPUT_YUV_DENOISED) || defined(OUTPUT_YUV_SKINMAP)
2257 // The denoiser buffer is allocated as a YUV 440 buffer. This function writes it
2258 // as YUV 420. We simply use the top-left pixels of the UV buffers, since we do
2259 // not denoise the UV channels at this time. If ever we implement UV channel
2260 // denoising we will have to modify this.
2261 void vp9_write_yuv_frame_420(YV12_BUFFER_CONFIG *s, FILE *f) {
2262   uint8_t *src = s->y_buffer;
2263   int h = s->y_height;
2264
2265   do {
2266     fwrite(src, s->y_width, 1, f);
2267     src += s->y_stride;
2268   } while (--h);
2269
2270   src = s->u_buffer;
2271   h = s->uv_height;
2272
2273   do {
2274     fwrite(src, s->uv_width, 1, f);
2275     src += s->uv_stride;
2276   } while (--h);
2277
2278   src = s->v_buffer;
2279   h = s->uv_height;
2280
2281   do {
2282     fwrite(src, s->uv_width, 1, f);
2283     src += s->uv_stride;
2284   } while (--h);
2285 }
2286 #endif
2287
2288 #ifdef OUTPUT_YUV_REC
2289 void vp9_write_yuv_rec_frame(VP9_COMMON *cm) {
2290   YV12_BUFFER_CONFIG *s = cm->frame_to_show;
2291   uint8_t *src = s->y_buffer;
2292   int h = cm->height;
2293
2294 #if CONFIG_VP9_HIGHBITDEPTH
2295   if (s->flags & YV12_FLAG_HIGHBITDEPTH) {
2296     uint16_t *src16 = CONVERT_TO_SHORTPTR(s->y_buffer);
2297
2298     do {
2299       fwrite(src16, s->y_width, 2, yuv_rec_file);
2300       src16 += s->y_stride;
2301     } while (--h);
2302
2303     src16 = CONVERT_TO_SHORTPTR(s->u_buffer);
2304     h = s->uv_height;
2305
2306     do {
2307       fwrite(src16, s->uv_width, 2, yuv_rec_file);
2308       src16 += s->uv_stride;
2309     } while (--h);
2310
2311     src16 = CONVERT_TO_SHORTPTR(s->v_buffer);
2312     h = s->uv_height;
2313
2314     do {
2315       fwrite(src16, s->uv_width, 2, yuv_rec_file);
2316       src16 += s->uv_stride;
2317     } while (--h);
2318
2319     fflush(yuv_rec_file);
2320     return;
2321   }
2322 #endif  // CONFIG_VP9_HIGHBITDEPTH
2323
2324   do {
2325     fwrite(src, s->y_width, 1, yuv_rec_file);
2326     src += s->y_stride;
2327   } while (--h);
2328
2329   src = s->u_buffer;
2330   h = s->uv_height;
2331
2332   do {
2333     fwrite(src, s->uv_width, 1, yuv_rec_file);
2334     src += s->uv_stride;
2335   } while (--h);
2336
2337   src = s->v_buffer;
2338   h = s->uv_height;
2339
2340   do {
2341     fwrite(src, s->uv_width, 1, yuv_rec_file);
2342     src += s->uv_stride;
2343   } while (--h);
2344
2345   fflush(yuv_rec_file);
2346 }
2347 #endif
2348
2349 #if CONFIG_VP9_HIGHBITDEPTH
2350 static void scale_and_extend_frame_nonnormative(const YV12_BUFFER_CONFIG *src,
2351                                                 YV12_BUFFER_CONFIG *dst,
2352                                                 int bd) {
2353 #else
2354 static void scale_and_extend_frame_nonnormative(const YV12_BUFFER_CONFIG *src,
2355                                                 YV12_BUFFER_CONFIG *dst) {
2356 #endif  // CONFIG_VP9_HIGHBITDEPTH
2357   // TODO(dkovalev): replace YV12_BUFFER_CONFIG with vpx_image_t
2358   int i;
2359   const uint8_t *const srcs[3] = { src->y_buffer, src->u_buffer,
2360                                    src->v_buffer };
2361   const int src_strides[3] = { src->y_stride, src->uv_stride, src->uv_stride };
2362   const int src_widths[3] = { src->y_crop_width, src->uv_crop_width,
2363                               src->uv_crop_width };
2364   const int src_heights[3] = { src->y_crop_height, src->uv_crop_height,
2365                                src->uv_crop_height };
2366   uint8_t *const dsts[3] = { dst->y_buffer, dst->u_buffer, dst->v_buffer };
2367   const int dst_strides[3] = { dst->y_stride, dst->uv_stride, dst->uv_stride };
2368   const int dst_widths[3] = { dst->y_crop_width, dst->uv_crop_width,
2369                               dst->uv_crop_width };
2370   const int dst_heights[3] = { dst->y_crop_height, dst->uv_crop_height,
2371                                dst->uv_crop_height };
2372
2373   for (i = 0; i < MAX_MB_PLANE; ++i) {
2374 #if CONFIG_VP9_HIGHBITDEPTH
2375     if (src->flags & YV12_FLAG_HIGHBITDEPTH) {
2376       vp9_highbd_resize_plane(srcs[i], src_heights[i], src_widths[i],
2377                               src_strides[i], dsts[i], dst_heights[i],
2378                               dst_widths[i], dst_strides[i], bd);
2379     } else {
2380       vp9_resize_plane(srcs[i], src_heights[i], src_widths[i], src_strides[i],
2381                        dsts[i], dst_heights[i], dst_widths[i], dst_strides[i]);
2382     }
2383 #else
2384     vp9_resize_plane(srcs[i], src_heights[i], src_widths[i], src_strides[i],
2385                      dsts[i], dst_heights[i], dst_widths[i], dst_strides[i]);
2386 #endif  // CONFIG_VP9_HIGHBITDEPTH
2387   }
2388   vpx_extend_frame_borders(dst);
2389 }
2390
2391 #if CONFIG_VP9_HIGHBITDEPTH
2392 static void scale_and_extend_frame(const YV12_BUFFER_CONFIG *src,
2393                                    YV12_BUFFER_CONFIG *dst, int bd,
2394                                    int phase_scaler) {
2395   const int src_w = src->y_crop_width;
2396   const int src_h = src->y_crop_height;
2397   const int dst_w = dst->y_crop_width;
2398   const int dst_h = dst->y_crop_height;
2399   const uint8_t *const srcs[3] = { src->y_buffer, src->u_buffer,
2400                                    src->v_buffer };
2401   const int src_strides[3] = { src->y_stride, src->uv_stride, src->uv_stride };
2402   uint8_t *const dsts[3] = { dst->y_buffer, dst->u_buffer, dst->v_buffer };
2403   const int dst_strides[3] = { dst->y_stride, dst->uv_stride, dst->uv_stride };
2404   const InterpKernel *const kernel = vp9_filter_kernels[EIGHTTAP];
2405   int x, y, i;
2406
2407   for (i = 0; i < MAX_MB_PLANE; ++i) {
2408     const int factor = (i == 0 || i == 3 ? 1 : 2);
2409     const int src_stride = src_strides[i];
2410     const int dst_stride = dst_strides[i];
2411     for (y = 0; y < dst_h; y += 16) {
2412       const int y_q4 = y * (16 / factor) * src_h / dst_h + phase_scaler;
2413       for (x = 0; x < dst_w; x += 16) {
2414         const int x_q4 = x * (16 / factor) * src_w / dst_w + phase_scaler;
2415         const uint8_t *src_ptr = srcs[i] +
2416                                  (y / factor) * src_h / dst_h * src_stride +
2417                                  (x / factor) * src_w / dst_w;
2418         uint8_t *dst_ptr = dsts[i] + (y / factor) * dst_stride + (x / factor);
2419
2420         if (src->flags & YV12_FLAG_HIGHBITDEPTH) {
2421           vpx_highbd_convolve8(
2422               CAST_TO_BYTEPTR(CONVERT_TO_SHORTPTR(src_ptr)), src_stride,
2423               CAST_TO_BYTEPTR(CONVERT_TO_SHORTPTR(dst_ptr)), dst_stride,
2424               kernel[x_q4 & 0xf], 16 * src_w / dst_w, kernel[y_q4 & 0xf],
2425               16 * src_h / dst_h, 16 / factor, 16 / factor, bd);
2426         } else {
2427           vpx_scaled_2d(src_ptr, src_stride, dst_ptr, dst_stride,
2428                         kernel[x_q4 & 0xf], 16 * src_w / dst_w,
2429                         kernel[y_q4 & 0xf], 16 * src_h / dst_h, 16 / factor,
2430                         16 / factor);
2431         }
2432       }
2433     }
2434   }
2435
2436   vpx_extend_frame_borders(dst);
2437 }
2438 #endif  // CONFIG_VP9_HIGHBITDEPTH
2439
2440 static int scale_down(VP9_COMP *cpi, int q) {
2441   RATE_CONTROL *const rc = &cpi->rc;
2442   GF_GROUP *const gf_group = &cpi->twopass.gf_group;
2443   int scale = 0;
2444   assert(frame_is_kf_gf_arf(cpi));
2445
2446   if (rc->frame_size_selector == UNSCALED &&
2447       q >= rc->rf_level_maxq[gf_group->rf_level[gf_group->index]]) {
2448     const int max_size_thresh =
2449         (int)(rate_thresh_mult[SCALE_STEP1] *
2450               VPXMAX(rc->this_frame_target, rc->avg_frame_bandwidth));
2451     scale = rc->projected_frame_size > max_size_thresh ? 1 : 0;
2452   }
2453   return scale;
2454 }
2455
2456 static int big_rate_miss(VP9_COMP *cpi, int high_limit, int low_limit) {
2457   const RATE_CONTROL *const rc = &cpi->rc;
2458
2459   return (rc->projected_frame_size > ((high_limit * 3) / 2)) ||
2460          (rc->projected_frame_size < (low_limit / 2));
2461 }
2462
2463 // test in two pass for the first
2464 static int two_pass_first_group_inter(VP9_COMP *cpi) {
2465   TWO_PASS *const twopass = &cpi->twopass;
2466   GF_GROUP *const gf_group = &twopass->gf_group;
2467   if ((cpi->oxcf.pass == 2) &&
2468       (gf_group->index == gf_group->first_inter_index)) {
2469     return 1;
2470   } else {
2471     return 0;
2472   }
2473 }
2474
2475 // Function to test for conditions that indicate we should loop
2476 // back and recode a frame.
2477 static int recode_loop_test(VP9_COMP *cpi, int high_limit, int low_limit, int q,
2478                             int maxq, int minq) {
2479   const RATE_CONTROL *const rc = &cpi->rc;
2480   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
2481   const int frame_is_kfgfarf = frame_is_kf_gf_arf(cpi);
2482   int force_recode = 0;
2483
2484   if ((rc->projected_frame_size >= rc->max_frame_bandwidth) ||
2485       big_rate_miss(cpi, high_limit, low_limit) ||
2486       (cpi->sf.recode_loop == ALLOW_RECODE) ||
2487       (two_pass_first_group_inter(cpi) &&
2488        (cpi->sf.recode_loop == ALLOW_RECODE_FIRST)) ||
2489       (frame_is_kfgfarf && (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF))) {
2490     if (frame_is_kfgfarf && (oxcf->resize_mode == RESIZE_DYNAMIC) &&
2491         scale_down(cpi, q)) {
2492       // Code this group at a lower resolution.
2493       cpi->resize_pending = 1;
2494       return 1;
2495     }
2496     // Force recode if projected_frame_size > max_frame_bandwidth
2497     if (rc->projected_frame_size >= rc->max_frame_bandwidth) return 1;
2498
2499     // TODO(agrange) high_limit could be greater than the scale-down threshold.
2500     if ((rc->projected_frame_size > high_limit && q < maxq) ||
2501         (rc->projected_frame_size < low_limit && q > minq)) {
2502       force_recode = 1;
2503     } else if (cpi->oxcf.rc_mode == VPX_CQ) {
2504       // Deal with frame undershoot and whether or not we are
2505       // below the automatically set cq level.
2506       if (q > oxcf->cq_level &&
2507           rc->projected_frame_size < ((rc->this_frame_target * 7) >> 3)) {
2508         force_recode = 1;
2509       }
2510     }
2511   }
2512   return force_recode;
2513 }
2514
2515 void vp9_update_reference_frames(VP9_COMP *cpi) {
2516   VP9_COMMON *const cm = &cpi->common;
2517   BufferPool *const pool = cm->buffer_pool;
2518
2519   // At this point the new frame has been encoded.
2520   // If any buffer copy / swapping is signaled it should be done here.
2521   if (cm->frame_type == KEY_FRAME) {
2522     ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->gld_fb_idx],
2523                cm->new_fb_idx);
2524     ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->alt_fb_idx],
2525                cm->new_fb_idx);
2526   } else if (vp9_preserve_existing_gf(cpi)) {
2527     // We have decided to preserve the previously existing golden frame as our
2528     // new ARF frame. However, in the short term in function
2529     // vp9_get_refresh_mask() we left it in the GF slot and, if
2530     // we're updating the GF with the current decoded frame, we save it to the
2531     // ARF slot instead.
2532     // We now have to update the ARF with the current frame and swap gld_fb_idx
2533     // and alt_fb_idx so that, overall, we've stored the old GF in the new ARF
2534     // slot and, if we're updating the GF, the current frame becomes the new GF.
2535     int tmp;
2536
2537     ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->alt_fb_idx],
2538                cm->new_fb_idx);
2539
2540     tmp = cpi->alt_fb_idx;
2541     cpi->alt_fb_idx = cpi->gld_fb_idx;
2542     cpi->gld_fb_idx = tmp;
2543
2544     if (is_two_pass_svc(cpi)) {
2545       cpi->svc.layer_context[0].gold_ref_idx = cpi->gld_fb_idx;
2546       cpi->svc.layer_context[0].alt_ref_idx = cpi->alt_fb_idx;
2547     }
2548   } else { /* For non key/golden frames */
2549     if (cpi->refresh_alt_ref_frame) {
2550       int arf_idx = cpi->alt_fb_idx;
2551       if ((cpi->oxcf.pass == 2) && cpi->multi_arf_allowed) {
2552         const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
2553         arf_idx = gf_group->arf_update_idx[gf_group->index];
2554       }
2555
2556       ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[arf_idx], cm->new_fb_idx);
2557       memcpy(cpi->interp_filter_selected[ALTREF_FRAME],
2558              cpi->interp_filter_selected[0],
2559              sizeof(cpi->interp_filter_selected[0]));
2560     }
2561
2562     if (cpi->refresh_golden_frame) {
2563       ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->gld_fb_idx],
2564                  cm->new_fb_idx);
2565       if (!cpi->rc.is_src_frame_alt_ref)
2566         memcpy(cpi->interp_filter_selected[GOLDEN_FRAME],
2567                cpi->interp_filter_selected[0],
2568                sizeof(cpi->interp_filter_selected[0]));
2569       else
2570         memcpy(cpi->interp_filter_selected[GOLDEN_FRAME],
2571                cpi->interp_filter_selected[ALTREF_FRAME],
2572                sizeof(cpi->interp_filter_selected[ALTREF_FRAME]));
2573     }
2574   }
2575
2576   if (cpi->refresh_last_frame) {
2577     ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->lst_fb_idx],
2578                cm->new_fb_idx);
2579     if (!cpi->rc.is_src_frame_alt_ref)
2580       memcpy(cpi->interp_filter_selected[LAST_FRAME],
2581              cpi->interp_filter_selected[0],
2582              sizeof(cpi->interp_filter_selected[0]));
2583   }
2584 #if CONFIG_VP9_TEMPORAL_DENOISING
2585   if (cpi->oxcf.noise_sensitivity > 0 && denoise_svc(cpi) &&
2586       cpi->denoiser.denoising_level > kDenLowLow) {
2587     int svc_base_is_key = 0;
2588     if (cpi->use_svc) {
2589       int layer = LAYER_IDS_TO_IDX(cpi->svc.spatial_layer_id,
2590                                    cpi->svc.temporal_layer_id,
2591                                    cpi->svc.number_temporal_layers);
2592       LAYER_CONTEXT *lc = &cpi->svc.layer_context[layer];
2593       svc_base_is_key = lc->is_key_frame;
2594     }
2595     vp9_denoiser_update_frame_info(
2596         &cpi->denoiser, *cpi->Source, cpi->common.frame_type,
2597         cpi->refresh_alt_ref_frame, cpi->refresh_golden_frame,
2598         cpi->refresh_last_frame, cpi->resize_pending, svc_base_is_key);
2599   }
2600 #endif
2601   if (is_one_pass_cbr_svc(cpi)) {
2602     // Keep track of frame index for each reference frame.
2603     SVC *const svc = &cpi->svc;
2604     if (cm->frame_type == KEY_FRAME) {
2605       svc->ref_frame_index[cpi->lst_fb_idx] = svc->current_superframe;
2606       svc->ref_frame_index[cpi->gld_fb_idx] = svc->current_superframe;
2607       svc->ref_frame_index[cpi->alt_fb_idx] = svc->current_superframe;
2608     } else {
2609       if (cpi->refresh_last_frame)
2610         svc->ref_frame_index[cpi->lst_fb_idx] = svc->current_superframe;
2611       if (cpi->refresh_golden_frame)
2612         svc->ref_frame_index[cpi->gld_fb_idx] = svc->current_superframe;
2613       if (cpi->refresh_alt_ref_frame)
2614         svc->ref_frame_index[cpi->alt_fb_idx] = svc->current_superframe;
2615     }
2616   }
2617 }
2618
2619 static void loopfilter_frame(VP9_COMP *cpi, VP9_COMMON *cm) {
2620   MACROBLOCKD *xd = &cpi->td.mb.e_mbd;
2621   struct loopfilter *lf = &cm->lf;
2622
2623   const int is_reference_frame =
2624       (cm->frame_type == KEY_FRAME || cpi->refresh_last_frame ||
2625        cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame);
2626
2627   if (xd->lossless) {
2628     lf->filter_level = 0;
2629     lf->last_filt_level = 0;
2630   } else {
2631     struct vpx_usec_timer timer;
2632
2633     vpx_clear_system_state();
2634
2635     vpx_usec_timer_start(&timer);
2636
2637     if (!cpi->rc.is_src_frame_alt_ref) {
2638       if ((cpi->common.frame_type == KEY_FRAME) &&
2639           (!cpi->rc.this_key_frame_forced)) {
2640         lf->last_filt_level = 0;
2641       }
2642       vp9_pick_filter_level(cpi->Source, cpi, cpi->sf.lpf_pick);
2643       lf->last_filt_level = lf->filter_level;
2644     } else {
2645       lf->filter_level = 0;
2646     }
2647
2648     vpx_usec_timer_mark(&timer);
2649     cpi->time_pick_lpf += vpx_usec_timer_elapsed(&timer);
2650   }
2651
2652   if (lf->filter_level > 0 && is_reference_frame) {
2653     vp9_build_mask_frame(cm, lf->filter_level, 0);
2654
2655     if (cpi->num_workers > 1)
2656       vp9_loop_filter_frame_mt(cm->frame_to_show, cm, xd->plane,
2657                                lf->filter_level, 0, 0, cpi->workers,
2658                                cpi->num_workers, &cpi->lf_row_sync);
2659     else
2660       vp9_loop_filter_frame(cm->frame_to_show, cm, xd, lf->filter_level, 0, 0);
2661   }
2662
2663   vpx_extend_frame_inner_borders(cm->frame_to_show);
2664 }
2665
2666 static INLINE void alloc_frame_mvs(VP9_COMMON *const cm, int buffer_idx) {
2667   RefCntBuffer *const new_fb_ptr = &cm->buffer_pool->frame_bufs[buffer_idx];
2668   if (new_fb_ptr->mvs == NULL || new_fb_ptr->mi_rows < cm->mi_rows ||
2669       new_fb_ptr->mi_cols < cm->mi_cols) {
2670     vpx_free(new_fb_ptr->mvs);
2671     CHECK_MEM_ERROR(cm, new_fb_ptr->mvs,
2672                     (MV_REF *)vpx_calloc(cm->mi_rows * cm->mi_cols,
2673                                          sizeof(*new_fb_ptr->mvs)));
2674     new_fb_ptr->mi_rows = cm->mi_rows;
2675     new_fb_ptr->mi_cols = cm->mi_cols;
2676   }
2677 }
2678
2679 void vp9_scale_references(VP9_COMP *cpi) {
2680   VP9_COMMON *cm = &cpi->common;
2681   MV_REFERENCE_FRAME ref_frame;
2682   const VP9_REFFRAME ref_mask[3] = { VP9_LAST_FLAG, VP9_GOLD_FLAG,
2683                                      VP9_ALT_FLAG };
2684
2685   for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
2686     // Need to convert from VP9_REFFRAME to index into ref_mask (subtract 1).
2687     if (cpi->ref_frame_flags & ref_mask[ref_frame - 1]) {
2688       BufferPool *const pool = cm->buffer_pool;
2689       const YV12_BUFFER_CONFIG *const ref =
2690           get_ref_frame_buffer(cpi, ref_frame);
2691
2692       if (ref == NULL) {
2693         cpi->scaled_ref_idx[ref_frame - 1] = INVALID_IDX;
2694         continue;
2695       }
2696
2697 #if CONFIG_VP9_HIGHBITDEPTH
2698       if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height) {
2699         RefCntBuffer *new_fb_ptr = NULL;
2700         int force_scaling = 0;
2701         int new_fb = cpi->scaled_ref_idx[ref_frame - 1];
2702         if (new_fb == INVALID_IDX) {
2703           new_fb = get_free_fb(cm);
2704           force_scaling = 1;
2705         }
2706         if (new_fb == INVALID_IDX) return;
2707         new_fb_ptr = &pool->frame_bufs[new_fb];
2708         if (force_scaling || new_fb_ptr->buf.y_crop_width != cm->width ||
2709             new_fb_ptr->buf.y_crop_height != cm->height) {
2710           if (vpx_realloc_frame_buffer(&new_fb_ptr->buf, cm->width, cm->height,
2711                                        cm->subsampling_x, cm->subsampling_y,
2712                                        cm->use_highbitdepth,
2713                                        VP9_ENC_BORDER_IN_PIXELS,
2714                                        cm->byte_alignment, NULL, NULL, NULL))
2715             vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
2716                                "Failed to allocate frame buffer");
2717           scale_and_extend_frame(ref, &new_fb_ptr->buf, (int)cm->bit_depth, 0);
2718           cpi->scaled_ref_idx[ref_frame - 1] = new_fb;
2719           alloc_frame_mvs(cm, new_fb);
2720         }
2721 #else
2722       if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height) {
2723         RefCntBuffer *new_fb_ptr = NULL;
2724         int force_scaling = 0;
2725         int new_fb = cpi->scaled_ref_idx[ref_frame - 1];
2726         if (new_fb == INVALID_IDX) {
2727           new_fb = get_free_fb(cm);
2728           force_scaling = 1;
2729         }
2730         if (new_fb == INVALID_IDX) return;
2731         new_fb_ptr = &pool->frame_bufs[new_fb];
2732         if (force_scaling || new_fb_ptr->buf.y_crop_width != cm->width ||
2733             new_fb_ptr->buf.y_crop_height != cm->height) {
2734           if (vpx_realloc_frame_buffer(&new_fb_ptr->buf, cm->width, cm->height,
2735                                        cm->subsampling_x, cm->subsampling_y,
2736                                        VP9_ENC_BORDER_IN_PIXELS,
2737                                        cm->byte_alignment, NULL, NULL, NULL))
2738             vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
2739                                "Failed to allocate frame buffer");
2740           vp9_scale_and_extend_frame(ref, &new_fb_ptr->buf, 0);
2741           cpi->scaled_ref_idx[ref_frame - 1] = new_fb;
2742           alloc_frame_mvs(cm, new_fb);
2743         }
2744 #endif  // CONFIG_VP9_HIGHBITDEPTH
2745       } else {
2746         int buf_idx;
2747         RefCntBuffer *buf = NULL;
2748         if (cpi->oxcf.pass == 0 && !cpi->use_svc) {
2749           // Check for release of scaled reference.
2750           buf_idx = cpi->scaled_ref_idx[ref_frame - 1];
2751           buf = (buf_idx != INVALID_IDX) ? &pool->frame_bufs[buf_idx] : NULL;
2752           if (buf != NULL) {
2753             --buf->ref_count;
2754             cpi->scaled_ref_idx[ref_frame - 1] = INVALID_IDX;
2755           }
2756         }
2757         buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
2758         buf = &pool->frame_bufs[buf_idx];
2759         buf->buf.y_crop_width = ref->y_crop_width;
2760         buf->buf.y_crop_height = ref->y_crop_height;
2761         cpi->scaled_ref_idx[ref_frame - 1] = buf_idx;
2762         ++buf->ref_count;
2763       }
2764     } else {
2765       if (cpi->oxcf.pass != 0 || cpi->use_svc)
2766         cpi->scaled_ref_idx[ref_frame - 1] = INVALID_IDX;
2767     }
2768   }
2769 }
2770
2771 static void release_scaled_references(VP9_COMP *cpi) {
2772   VP9_COMMON *cm = &cpi->common;
2773   int i;
2774   if (cpi->oxcf.pass == 0 && !cpi->use_svc) {
2775     // Only release scaled references under certain conditions:
2776     // if reference will be updated, or if scaled reference has same resolution.
2777     int refresh[3];
2778     refresh[0] = (cpi->refresh_last_frame) ? 1 : 0;
2779     refresh[1] = (cpi->refresh_golden_frame) ? 1 : 0;
2780     refresh[2] = (cpi->refresh_alt_ref_frame) ? 1 : 0;
2781     for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
2782       const int idx = cpi->scaled_ref_idx[i - 1];
2783       RefCntBuffer *const buf =
2784           idx != INVALID_IDX ? &cm->buffer_pool->frame_bufs[idx] : NULL;
2785       const YV12_BUFFER_CONFIG *const ref = get_ref_frame_buffer(cpi, i);
2786       if (buf != NULL &&
2787           (refresh[i - 1] || (buf->buf.y_crop_width == ref->y_crop_width &&
2788                               buf->buf.y_crop_height == ref->y_crop_height))) {
2789         --buf->ref_count;
2790         cpi->scaled_ref_idx[i - 1] = INVALID_IDX;
2791       }
2792     }
2793   } else {
2794     for (i = 0; i < MAX_REF_FRAMES; ++i) {
2795       const int idx = cpi->scaled_ref_idx[i];
2796       RefCntBuffer *const buf =
2797           idx != INVALID_IDX ? &cm->buffer_pool->frame_bufs[idx] : NULL;
2798       if (buf != NULL) {
2799         --buf->ref_count;
2800         cpi->scaled_ref_idx[i] = INVALID_IDX;
2801       }
2802     }
2803   }
2804 }
2805
2806 static void full_to_model_count(unsigned int *model_count,
2807                                 unsigned int *full_count) {
2808   int n;
2809   model_count[ZERO_TOKEN] = full_count[ZERO_TOKEN];
2810   model_count[ONE_TOKEN] = full_count[ONE_TOKEN];
2811   model_count[TWO_TOKEN] = full_count[TWO_TOKEN];
2812   for (n = THREE_TOKEN; n < EOB_TOKEN; ++n)
2813     model_count[TWO_TOKEN] += full_count[n];
2814   model_count[EOB_MODEL_TOKEN] = full_count[EOB_TOKEN];
2815 }
2816
2817 static void full_to_model_counts(vp9_coeff_count_model *model_count,
2818                                  vp9_coeff_count *full_count) {
2819   int i, j, k, l;
2820
2821   for (i = 0; i < PLANE_TYPES; ++i)
2822     for (j = 0; j < REF_TYPES; ++j)
2823       for (k = 0; k < COEF_BANDS; ++k)
2824         for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l)
2825           full_to_model_count(model_count[i][j][k][l], full_count[i][j][k][l]);
2826 }
2827
2828 #if 0 && CONFIG_INTERNAL_STATS
2829 static void output_frame_level_debug_stats(VP9_COMP *cpi) {
2830   VP9_COMMON *const cm = &cpi->common;
2831   FILE *const f = fopen("tmp.stt", cm->current_video_frame ? "a" : "w");
2832   int64_t recon_err;
2833
2834   vpx_clear_system_state();
2835
2836 #if CONFIG_VP9_HIGHBITDEPTH
2837   if (cm->use_highbitdepth) {
2838     recon_err = vpx_highbd_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
2839   } else {
2840     recon_err = vpx_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
2841   }
2842 #else
2843   recon_err = vpx_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
2844 #endif  // CONFIG_VP9_HIGHBITDEPTH
2845
2846
2847   if (cpi->twopass.total_left_stats.coded_error != 0.0) {
2848     double dc_quant_devisor;
2849 #if CONFIG_VP9_HIGHBITDEPTH
2850     switch (cm->bit_depth) {
2851       case VPX_BITS_8:
2852         dc_quant_devisor = 4.0;
2853         break;
2854       case VPX_BITS_10:
2855         dc_quant_devisor = 16.0;
2856         break;
2857       case VPX_BITS_12:
2858         dc_quant_devisor = 64.0;
2859         break;
2860       default:
2861         assert(0 && "bit_depth must be VPX_BITS_8, VPX_BITS_10 or VPX_BITS_12");
2862         break;
2863     }
2864 #else
2865     dc_quant_devisor = 4.0;
2866 #endif
2867
2868     fprintf(f, "%10u %dx%d %d %d %10d %10d %10d %10d"
2869        "%10"PRId64" %10"PRId64" %5d %5d %10"PRId64" "
2870        "%10"PRId64" %10"PRId64" %10d "
2871        "%7.2lf %7.2lf %7.2lf %7.2lf %7.2lf"
2872         "%6d %6d %5d %5d %5d "
2873         "%10"PRId64" %10.3lf"
2874         "%10lf %8u %10"PRId64" %10d %10d %10d %10d %10d\n",
2875         cpi->common.current_video_frame,
2876         cm->width, cm->height,
2877         cpi->rc.source_alt_ref_pending,
2878         cpi->rc.source_alt_ref_active,
2879         cpi->rc.this_frame_target,
2880         cpi->rc.projected_frame_size,
2881         cpi->rc.projected_frame_size / cpi->common.MBs,
2882         (cpi->rc.projected_frame_size - cpi->rc.this_frame_target),
2883         cpi->rc.vbr_bits_off_target,
2884         cpi->rc.vbr_bits_off_target_fast,
2885         cpi->twopass.extend_minq,
2886         cpi->twopass.extend_minq_fast,
2887         cpi->rc.total_target_vs_actual,
2888         (cpi->rc.starting_buffer_level - cpi->rc.bits_off_target),
2889         cpi->rc.total_actual_bits, cm->base_qindex,
2890         vp9_convert_qindex_to_q(cm->base_qindex, cm->bit_depth),
2891         (double)vp9_dc_quant(cm->base_qindex, 0, cm->bit_depth) /
2892             dc_quant_devisor,
2893         vp9_convert_qindex_to_q(cpi->twopass.active_worst_quality,
2894                                 cm->bit_depth),
2895         cpi->rc.avg_q,
2896         vp9_convert_qindex_to_q(cpi->oxcf.cq_level, cm->bit_depth),
2897         cpi->refresh_last_frame, cpi->refresh_golden_frame,
2898         cpi->refresh_alt_ref_frame, cm->frame_type, cpi->rc.gfu_boost,
2899         cpi->twopass.bits_left,
2900         cpi->twopass.total_left_stats.coded_error,
2901         cpi->twopass.bits_left /
2902             (1 + cpi->twopass.total_left_stats.coded_error),
2903         cpi->tot_recode_hits, recon_err, cpi->rc.kf_boost,
2904         cpi->twopass.kf_zeromotion_pct,
2905         cpi->twopass.fr_content_type,
2906         cm->lf.filter_level,
2907         cm->seg.aq_av_offset);
2908   }
2909   fclose(f);
2910
2911   if (0) {
2912     FILE *const fmodes = fopen("Modes.stt", "a");
2913     int i;
2914
2915     fprintf(fmodes, "%6d:%1d:%1d:%1d ", cpi->common.current_video_frame,
2916             cm->frame_type, cpi->refresh_golden_frame,
2917             cpi->refresh_alt_ref_frame);
2918
2919     for (i = 0; i < MAX_MODES; ++i)
2920       fprintf(fmodes, "%5d ", cpi->mode_chosen_counts[i]);
2921
2922     fprintf(fmodes, "\n");
2923
2924     fclose(fmodes);
2925   }
2926 }
2927 #endif
2928
2929 static void set_mv_search_params(VP9_COMP *cpi) {
2930   const VP9_COMMON *const cm = &cpi->common;
2931   const unsigned int max_mv_def = VPXMIN(cm->width, cm->height);
2932
2933   // Default based on max resolution.
2934   cpi->mv_step_param = vp9_init_search_range(max_mv_def);
2935
2936   if (cpi->sf.mv.auto_mv_step_size) {
2937     if (frame_is_intra_only(cm)) {
2938       // Initialize max_mv_magnitude for use in the first INTER frame
2939       // after a key/intra-only frame.
2940       cpi->max_mv_magnitude = max_mv_def;
2941     } else {
2942       if (cm->show_frame) {
2943         // Allow mv_steps to correspond to twice the max mv magnitude found
2944         // in the previous frame, capped by the default max_mv_magnitude based
2945         // on resolution.
2946         cpi->mv_step_param = vp9_init_search_range(
2947             VPXMIN(max_mv_def, 2 * cpi->max_mv_magnitude));
2948       }
2949       cpi->max_mv_magnitude = 0;
2950     }
2951   }
2952 }
2953
2954 static void set_size_independent_vars(VP9_COMP *cpi) {
2955   vp9_set_speed_features_framesize_independent(cpi);
2956   vp9_set_rd_speed_thresholds(cpi);
2957   vp9_set_rd_speed_thresholds_sub8x8(cpi);
2958   cpi->common.interp_filter = cpi->sf.default_interp_filter;
2959 }
2960
2961 static void set_size_dependent_vars(VP9_COMP *cpi, int *q, int *bottom_index,
2962                                     int *top_index) {
2963   VP9_COMMON *const cm = &cpi->common;
2964   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
2965
2966   // Setup variables that depend on the dimensions of the frame.
2967   vp9_set_speed_features_framesize_dependent(cpi);
2968
2969   // Decide q and q bounds.
2970   *q = vp9_rc_pick_q_and_bounds(cpi, bottom_index, top_index);
2971
2972   if (!frame_is_intra_only(cm)) {
2973     vp9_set_high_precision_mv(cpi, (*q) < HIGH_PRECISION_MV_QTHRESH);
2974   }
2975
2976   // Configure experimental use of segmentation for enhanced coding of
2977   // static regions if indicated.
2978   // Only allowed in the second pass of a two pass encode, as it requires
2979   // lagged coding, and if the relevant speed feature flag is set.
2980   if (oxcf->pass == 2 && cpi->sf.static_segmentation)
2981     configure_static_seg_features(cpi);
2982
2983 #if CONFIG_VP9_POSTPROC && !(CONFIG_VP9_TEMPORAL_DENOISING)
2984   if (oxcf->noise_sensitivity > 0) {
2985     int l = 0;
2986     switch (oxcf->noise_sensitivity) {
2987       case 1: l = 20; break;
2988       case 2: l = 40; break;
2989       case 3: l = 60; break;
2990       case 4:
2991       case 5: l = 100; break;
2992       case 6: l = 150; break;
2993     }
2994     if (!cpi->common.postproc_state.limits) {
2995       cpi->common.postproc_state.limits =
2996           vpx_calloc(cpi->un_scaled_source->y_width,
2997                      sizeof(*cpi->common.postproc_state.limits));
2998     }
2999     vp9_denoise(cpi->Source, cpi->Source, l, cpi->common.postproc_state.limits);
3000   }
3001 #endif  // CONFIG_VP9_POSTPROC
3002 }
3003
3004 #if CONFIG_VP9_TEMPORAL_DENOISING
3005 static void setup_denoiser_buffer(VP9_COMP *cpi) {
3006   VP9_COMMON *const cm = &cpi->common;
3007   if (cpi->oxcf.noise_sensitivity > 0 &&
3008       !cpi->denoiser.frame_buffer_initialized) {
3009     if (vp9_denoiser_alloc(&cpi->denoiser, cm->width, cm->height,
3010                            cm->subsampling_x, cm->subsampling_y,
3011 #if CONFIG_VP9_HIGHBITDEPTH
3012                            cm->use_highbitdepth,
3013 #endif
3014                            VP9_ENC_BORDER_IN_PIXELS))
3015       vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
3016                          "Failed to allocate denoiser");
3017   }
3018 }
3019 #endif
3020
3021 static void init_motion_estimation(VP9_COMP *cpi) {
3022   int y_stride = cpi->scaled_source.y_stride;
3023
3024   if (cpi->sf.mv.search_method == NSTEP) {
3025     vp9_init3smotion_compensation(&cpi->ss_cfg, y_stride);
3026   } else if (cpi->sf.mv.search_method == DIAMOND) {
3027     vp9_init_dsmotion_compensation(&cpi->ss_cfg, y_stride);
3028   }
3029 }
3030
3031 static void set_frame_size(VP9_COMP *cpi) {
3032   int ref_frame;
3033   VP9_COMMON *const cm = &cpi->common;
3034   VP9EncoderConfig *const oxcf = &cpi->oxcf;
3035   MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
3036
3037   if (oxcf->pass == 2 && oxcf->rc_mode == VPX_VBR &&
3038       ((oxcf->resize_mode == RESIZE_FIXED && cm->current_video_frame == 0) ||
3039        (oxcf->resize_mode == RESIZE_DYNAMIC && cpi->resize_pending))) {
3040     calculate_coded_size(cpi, &oxcf->scaled_frame_width,
3041                          &oxcf->scaled_frame_height);
3042
3043     // There has been a change in frame size.
3044     vp9_set_size_literal(cpi, oxcf->scaled_frame_width,
3045                          oxcf->scaled_frame_height);
3046   }
3047
3048   if (oxcf->pass == 0 && oxcf->rc_mode == VPX_CBR && !cpi->use_svc &&
3049       oxcf->resize_mode == RESIZE_DYNAMIC && cpi->resize_pending != 0) {
3050     oxcf->scaled_frame_width =
3051         (oxcf->width * cpi->resize_scale_num) / cpi->resize_scale_den;
3052     oxcf->scaled_frame_height =
3053         (oxcf->height * cpi->resize_scale_num) / cpi->resize_scale_den;
3054     // There has been a change in frame size.
3055     vp9_set_size_literal(cpi, oxcf->scaled_frame_width,
3056                          oxcf->scaled_frame_height);
3057
3058     // TODO(agrange) Scale cpi->max_mv_magnitude if frame-size has changed.
3059     set_mv_search_params(cpi);
3060
3061     vp9_noise_estimate_init(&cpi->noise_estimate, cm->width, cm->height);
3062 #if CONFIG_VP9_TEMPORAL_DENOISING
3063     // Reset the denoiser on the resized frame.
3064     if (cpi->oxcf.noise_sensitivity > 0) {
3065       vp9_denoiser_free(&(cpi->denoiser));
3066       setup_denoiser_buffer(cpi);
3067       // Dynamic resize is only triggered for non-SVC, so we can force
3068       // golden frame update here as temporary fix to denoiser.
3069       cpi->refresh_golden_frame = 1;
3070     }
3071 #endif
3072   }
3073
3074   if ((oxcf->pass == 2) &&
3075       (!cpi->use_svc || (is_two_pass_svc(cpi) &&
3076                          cpi->svc.encode_empty_frame_state != ENCODING))) {
3077     vp9_set_target_rate(cpi);
3078   }
3079
3080   alloc_frame_mvs(cm, cm->new_fb_idx);
3081
3082   // Reset the frame pointers to the current frame size.
3083   if (vpx_realloc_frame_buffer(get_frame_new_buffer(cm), cm->width, cm->height,
3084                                cm->subsampling_x, cm->subsampling_y,
3085 #if CONFIG_VP9_HIGHBITDEPTH
3086                                cm->use_highbitdepth,
3087 #endif
3088                                VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment,
3089                                NULL, NULL, NULL))
3090     vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
3091                        "Failed to allocate frame buffer");
3092
3093   alloc_util_frame_buffers(cpi);
3094   init_motion_estimation(cpi);
3095
3096   for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
3097     RefBuffer *const ref_buf = &cm->frame_refs[ref_frame - 1];
3098     const int buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
3099
3100     ref_buf->idx = buf_idx;
3101
3102     if (buf_idx != INVALID_IDX) {
3103       YV12_BUFFER_CONFIG *const buf = &cm->buffer_pool->frame_bufs[buf_idx].buf;
3104       ref_buf->buf = buf;
3105 #if CONFIG_VP9_HIGHBITDEPTH
3106       vp9_setup_scale_factors_for_frame(
3107           &ref_buf->sf, buf->y_crop_width, buf->y_crop_height, cm->width,
3108           cm->height, (buf->flags & YV12_FLAG_HIGHBITDEPTH) ? 1 : 0);
3109 #else
3110       vp9_setup_scale_factors_for_frame(&ref_buf->sf, buf->y_crop_width,
3111                                         buf->y_crop_height, cm->width,
3112                                         cm->height);
3113 #endif  // CONFIG_VP9_HIGHBITDEPTH
3114       if (vp9_is_scaled(&ref_buf->sf)) vpx_extend_frame_borders(buf);
3115     } else {
3116       ref_buf->buf = NULL;
3117     }
3118   }
3119
3120   set_ref_ptrs(cm, xd, LAST_FRAME, LAST_FRAME);
3121 }
3122
3123 static void encode_without_recode_loop(VP9_COMP *cpi, size_t *size,
3124                                        uint8_t *dest) {
3125   VP9_COMMON *const cm = &cpi->common;
3126   int q = 0, bottom_index = 0, top_index = 0;  // Dummy variables.
3127   const int phase_scaler =
3128       (is_one_pass_cbr_svc(cpi) &&
3129        cpi->svc.filtertype_downsample_source[cpi->svc.spatial_layer_id])
3130           ? 8
3131           : 0;
3132
3133   // Flag to check if its valid to compute the source sad (used for
3134   // scene detection and for superblock content state in CBR mode).
3135   // The flag may get reset below based on SVC or resizing state.
3136   cpi->compute_source_sad_onepass =
3137       cpi->oxcf.mode == REALTIME && cpi->oxcf.speed >= 5 && cm->show_frame;
3138
3139   vpx_clear_system_state();
3140
3141   set_frame_size(cpi);
3142
3143   if (is_one_pass_cbr_svc(cpi) &&
3144       cpi->un_scaled_source->y_width == cm->width << 2 &&
3145       cpi->un_scaled_source->y_height == cm->height << 2 &&
3146       cpi->svc.scaled_temp.y_width == cm->width << 1 &&
3147       cpi->svc.scaled_temp.y_height == cm->height << 1) {
3148     // For svc, if it is a 1/4x1/4 downscaling, do a two-stage scaling to take
3149     // advantage of the 1:2 optimized scaler. In the process, the 1/2x1/2
3150     // result will be saved in scaled_temp and might be used later.
3151     int phase_scaler2 = (cpi->svc.filtertype_downsample_source[1]) ? 8 : 0;
3152     cpi->Source = vp9_svc_twostage_scale(
3153         cm, cpi->un_scaled_source, &cpi->scaled_source, &cpi->svc.scaled_temp,
3154         phase_scaler, phase_scaler2);
3155     cpi->svc.scaled_one_half = 1;
3156   } else if (is_one_pass_cbr_svc(cpi) &&
3157              cpi->un_scaled_source->y_width == cm->width << 1 &&
3158              cpi->un_scaled_source->y_height == cm->height << 1 &&
3159              cpi->svc.scaled_one_half) {
3160     // If the spatial layer is 1/2x1/2 and the scaling is already done in the
3161     // two-stage scaling, use the result directly.
3162     cpi->Source = &cpi->svc.scaled_temp;
3163     cpi->svc.scaled_one_half = 0;
3164   } else {
3165     cpi->Source =
3166         vp9_scale_if_required(cm, cpi->un_scaled_source, &cpi->scaled_source,
3167                               (cpi->oxcf.pass == 0), phase_scaler);
3168   }
3169   // Unfiltered raw source used in metrics calculation if the source
3170   // has been filtered.
3171   if (is_psnr_calc_enabled(cpi)) {
3172 #ifdef ENABLE_KF_DENOISE
3173     if (is_spatial_denoise_enabled(cpi)) {
3174       cpi->raw_source_frame = vp9_scale_if_required(
3175           cm, &cpi->raw_unscaled_source, &cpi->raw_scaled_source,
3176           (cpi->oxcf.pass == 0), phase_scaler);
3177     } else {
3178       cpi->raw_source_frame = cpi->Source;
3179     }
3180 #else
3181     cpi->raw_source_frame = cpi->Source;
3182 #endif
3183   }
3184
3185   if ((cpi->use_svc &&
3186        (cpi->svc.spatial_layer_id < cpi->svc.number_spatial_layers - 1 ||
3187         cpi->svc.current_superframe < 1)) ||
3188       cpi->resize_pending || cpi->resize_state || cpi->external_resize ||
3189       cpi->resize_state != ORIG) {
3190     cpi->compute_source_sad_onepass = 0;
3191     if (cpi->content_state_sb_fd != NULL)
3192       memset(cpi->content_state_sb_fd, 0,
3193              (cm->mi_stride >> 3) * ((cm->mi_rows >> 3) + 1) *
3194                  sizeof(*cpi->content_state_sb_fd));
3195   }
3196
3197   // Avoid scaling last_source unless its needed.
3198   // Last source is needed if avg_source_sad() is used, or if
3199   // partition_search_type == SOURCE_VAR_BASED_PARTITION, or if noise
3200   // estimation is enabled.
3201   if (cpi->unscaled_last_source != NULL &&
3202       (cpi->oxcf.content == VP9E_CONTENT_SCREEN ||
3203        (cpi->oxcf.pass == 0 && cpi->oxcf.rc_mode == VPX_VBR &&
3204         cpi->oxcf.mode == REALTIME && cpi->oxcf.speed >= 5) ||
3205        cpi->sf.partition_search_type == SOURCE_VAR_BASED_PARTITION ||
3206        (cpi->noise_estimate.enabled && !cpi->oxcf.noise_sensitivity) ||
3207        cpi->compute_source_sad_onepass))
3208     cpi->Last_Source = vp9_scale_if_required(cm, cpi->unscaled_last_source,
3209                                              &cpi->scaled_last_source,
3210                                              (cpi->oxcf.pass == 0), 0);
3211
3212   if (cpi->Last_Source == NULL ||
3213       cpi->Last_Source->y_width != cpi->Source->y_width ||
3214       cpi->Last_Source->y_height != cpi->Source->y_height)
3215     cpi->compute_source_sad_onepass = 0;
3216
3217   if (cm->frame_type == KEY_FRAME || cpi->resize_pending != 0) {
3218     memset(cpi->consec_zero_mv, 0,
3219            cm->mi_rows * cm->mi_cols * sizeof(*cpi->consec_zero_mv));
3220   }
3221
3222   vp9_update_noise_estimate(cpi);
3223
3224   // Scene detection is used for VBR mode or screen-content case.
3225   // Make sure compute_source_sad_onepass is set (which handles SVC case
3226   // and dynamic resize).
3227   if (cpi->compute_source_sad_onepass &&
3228       (cpi->oxcf.rc_mode == VPX_VBR ||
3229        cpi->oxcf.content == VP9E_CONTENT_SCREEN))
3230     vp9_scene_detection_onepass(cpi);
3231
3232   // For 1 pass SVC, since only ZEROMV is allowed for upsampled reference
3233   // frame (i.e, svc->force_zero_mode_spatial_ref = 0), we can avoid this
3234   // frame-level upsampling.
3235   if (frame_is_intra_only(cm) == 0 && !is_one_pass_cbr_svc(cpi)) {
3236     vp9_scale_references(cpi);
3237   }
3238
3239   set_size_independent_vars(cpi);
3240   set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
3241
3242   if (cpi->sf.copy_partition_flag) alloc_copy_partition_data(cpi);
3243
3244   if (cpi->oxcf.speed >= 5 && cpi->oxcf.pass == 0 &&
3245       cpi->oxcf.rc_mode == VPX_CBR &&
3246       cpi->oxcf.content != VP9E_CONTENT_SCREEN &&
3247       cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) {
3248     cpi->use_skin_detection = 1;
3249   }
3250
3251   vp9_set_quantizer(cm, q);
3252   vp9_set_variance_partition_thresholds(cpi, q, 0);
3253
3254   setup_frame(cpi);
3255
3256   suppress_active_map(cpi);
3257
3258   // Variance adaptive and in frame q adjustment experiments are mutually
3259   // exclusive.
3260   if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
3261     vp9_vaq_frame_setup(cpi);
3262   } else if (cpi->oxcf.aq_mode == EQUATOR360_AQ) {
3263     vp9_360aq_frame_setup(cpi);
3264   } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
3265     vp9_setup_in_frame_q_adj(cpi);
3266   } else if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) {
3267     vp9_cyclic_refresh_setup(cpi);
3268   } else if (cpi->oxcf.aq_mode == LOOKAHEAD_AQ) {
3269     // it may be pretty bad for rate-control,
3270     // and I should handle it somehow
3271     vp9_alt_ref_aq_setup_map(cpi->alt_ref_aq, cpi);
3272   }
3273
3274   apply_active_map(cpi);
3275
3276   vp9_encode_frame(cpi);
3277
3278   // Check if we should drop this frame because of high overshoot.
3279   // Only for frames where high temporal-source SAD is detected.
3280   if (cpi->oxcf.pass == 0 && cpi->oxcf.rc_mode == VPX_CBR &&
3281       cpi->resize_state == ORIG && cm->frame_type != KEY_FRAME &&
3282       cpi->oxcf.content == VP9E_CONTENT_SCREEN &&
3283       cpi->rc.high_source_sad == 1) {
3284     int frame_size = 0;
3285     // Get an estimate of the encoded frame size.
3286     save_coding_context(cpi);
3287     vp9_pack_bitstream(cpi, dest, size);
3288     restore_coding_context(cpi);
3289     frame_size = (int)(*size) << 3;
3290     // Check if encoded frame will overshoot too much, and if so, set the q and
3291     // adjust some rate control parameters, and return to re-encode the frame.
3292     if (vp9_encodedframe_overshoot(cpi, frame_size, &q)) {
3293       vpx_clear_system_state();
3294       vp9_set_quantizer(cm, q);
3295       vp9_set_variance_partition_thresholds(cpi, q, 0);
3296       suppress_active_map(cpi);
3297       // Turn-off cyclic refresh for re-encoded frame.
3298       if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) {
3299         unsigned char *const seg_map = cpi->segmentation_map;
3300         memset(seg_map, 0, cm->mi_rows * cm->mi_cols);
3301         vp9_disable_segmentation(&cm->seg);
3302       }
3303       apply_active_map(cpi);
3304       vp9_encode_frame(cpi);
3305     }
3306   }
3307
3308   // Update some stats from cyclic refresh, and check for golden frame update.
3309   if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->seg.enabled &&
3310       cm->frame_type != KEY_FRAME)
3311     vp9_cyclic_refresh_postencode(cpi);
3312
3313   // Update the skip mb flag probabilities based on the distribution
3314   // seen in the last encoder iteration.
3315   // update_base_skip_probs(cpi);
3316   vpx_clear_system_state();
3317 }
3318
3319 #define MAX_QSTEP_ADJ 4
3320 static int get_qstep_adj(int rate_excess, int rate_limit) {
3321   int qstep =
3322       rate_limit ? ((rate_excess + rate_limit / 2) / rate_limit) : INT_MAX;
3323   return VPXMIN(qstep, MAX_QSTEP_ADJ);
3324 }
3325
3326 static void encode_with_recode_loop(VP9_COMP *cpi, size_t *size,
3327                                     uint8_t *dest) {
3328   VP9_COMMON *const cm = &cpi->common;
3329   RATE_CONTROL *const rc = &cpi->rc;
3330   int bottom_index, top_index;
3331   int loop_count = 0;
3332   int loop_at_this_size = 0;
3333   int loop = 0;
3334   int overshoot_seen = 0;
3335   int undershoot_seen = 0;
3336   int frame_over_shoot_limit;
3337   int frame_under_shoot_limit;
3338   int q = 0, q_low = 0, q_high = 0;
3339   int enable_acl;
3340 #ifdef AGGRESSIVE_VBR
3341   int qrange_adj = 1;
3342 #endif
3343
3344   set_size_independent_vars(cpi);
3345
3346   enable_acl = cpi->sf.allow_acl
3347                    ? (cm->frame_type == KEY_FRAME) || (cm->show_frame == 0)
3348                    : 0;
3349
3350   do {
3351     vpx_clear_system_state();
3352
3353     set_frame_size(cpi);
3354
3355     if (loop_count == 0 || cpi->resize_pending != 0) {
3356       set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
3357
3358 #ifdef AGGRESSIVE_VBR
3359       if (two_pass_first_group_inter(cpi)) {
3360         // Adjustment limits for min and max q
3361         qrange_adj = VPXMAX(1, (top_index - bottom_index) / 2);
3362
3363         bottom_index =
3364             VPXMAX(bottom_index - qrange_adj / 2, cpi->oxcf.best_allowed_q);
3365         top_index =
3366             VPXMIN(cpi->oxcf.worst_allowed_q, top_index + qrange_adj / 2);
3367       }
3368 #endif
3369       // TODO(agrange) Scale cpi->max_mv_magnitude if frame-size has changed.
3370       set_mv_search_params(cpi);
3371
3372       // Reset the loop state for new frame size.
3373       overshoot_seen = 0;
3374       undershoot_seen = 0;
3375
3376       // Reconfiguration for change in frame size has concluded.
3377       cpi->resize_pending = 0;
3378
3379       q_low = bottom_index;
3380       q_high = top_index;
3381
3382       loop_at_this_size = 0;
3383     }
3384
3385     // Decide frame size bounds first time through.
3386     if (loop_count == 0) {
3387       vp9_rc_compute_frame_size_bounds(cpi, rc->this_frame_target,
3388                                        &frame_under_shoot_limit,
3389                                        &frame_over_shoot_limit);
3390     }
3391
3392     cpi->Source =
3393         vp9_scale_if_required(cm, cpi->un_scaled_source, &cpi->scaled_source,
3394                               (cpi->oxcf.pass == 0), 0);
3395
3396     // Unfiltered raw source used in metrics calculation if the source
3397     // has been filtered.
3398     if (is_psnr_calc_enabled(cpi)) {
3399 #ifdef ENABLE_KF_DENOISE
3400       if (is_spatial_denoise_enabled(cpi)) {
3401         cpi->raw_source_frame = vp9_scale_if_required(
3402             cm, &cpi->raw_unscaled_source, &cpi->raw_scaled_source,
3403             (cpi->oxcf.pass == 0), 0);
3404       } else {
3405         cpi->raw_source_frame = cpi->Source;
3406       }
3407 #else
3408       cpi->raw_source_frame = cpi->Source;
3409 #endif
3410     }
3411
3412     if (cpi->unscaled_last_source != NULL)
3413       cpi->Last_Source = vp9_scale_if_required(cm, cpi->unscaled_last_source,
3414                                                &cpi->scaled_last_source,
3415                                                (cpi->oxcf.pass == 0), 0);
3416
3417     if (frame_is_intra_only(cm) == 0) {
3418       if (loop_count > 0) {
3419         release_scaled_references(cpi);
3420       }
3421       vp9_scale_references(cpi);
3422     }
3423
3424     vp9_set_quantizer(cm, q);
3425
3426     if (loop_count == 0) setup_frame(cpi);
3427
3428     // Variance adaptive and in frame q adjustment experiments are mutually
3429     // exclusive.
3430     if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
3431       vp9_vaq_frame_setup(cpi);
3432     } else if (cpi->oxcf.aq_mode == EQUATOR360_AQ) {
3433       vp9_360aq_frame_setup(cpi);
3434     } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
3435       vp9_setup_in_frame_q_adj(cpi);
3436     } else if (cpi->oxcf.aq_mode == LOOKAHEAD_AQ) {
3437       vp9_alt_ref_aq_setup_map(cpi->alt_ref_aq, cpi);
3438     }
3439
3440     vp9_encode_frame(cpi);
3441
3442     // Update the skip mb flag probabilities based on the distribution
3443     // seen in the last encoder iteration.
3444     // update_base_skip_probs(cpi);
3445
3446     vpx_clear_system_state();
3447
3448     // Dummy pack of the bitstream using up to date stats to get an
3449     // accurate estimate of output frame size to determine if we need
3450     // to recode.
3451     if (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF) {
3452       save_coding_context(cpi);
3453       if (!cpi->sf.use_nonrd_pick_mode) vp9_pack_bitstream(cpi, dest, size);
3454
3455       rc->projected_frame_size = (int)(*size) << 3;
3456
3457       if (frame_over_shoot_limit == 0) frame_over_shoot_limit = 1;
3458     }
3459
3460     if (cpi->oxcf.rc_mode == VPX_Q) {
3461       loop = 0;
3462     } else {
3463       if ((cm->frame_type == KEY_FRAME) && rc->this_key_frame_forced &&
3464           (rc->projected_frame_size < rc->max_frame_bandwidth)) {
3465         int last_q = q;
3466         int64_t kf_err;
3467
3468         int64_t high_err_target = cpi->ambient_err;
3469         int64_t low_err_target = cpi->ambient_err >> 1;
3470
3471 #if CONFIG_VP9_HIGHBITDEPTH
3472         if (cm->use_highbitdepth) {
3473           kf_err = vpx_highbd_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
3474         } else {
3475           kf_err = vpx_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
3476         }
3477 #else
3478         kf_err = vpx_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
3479 #endif  // CONFIG_VP9_HIGHBITDEPTH
3480
3481         // Prevent possible divide by zero error below for perfect KF
3482         kf_err += !kf_err;
3483
3484         // The key frame is not good enough or we can afford
3485         // to make it better without undue risk of popping.
3486         if ((kf_err > high_err_target &&
3487              rc->projected_frame_size <= frame_over_shoot_limit) ||
3488             (kf_err > low_err_target &&
3489              rc->projected_frame_size <= frame_under_shoot_limit)) {
3490           // Lower q_high
3491           q_high = q > q_low ? q - 1 : q_low;
3492
3493           // Adjust Q
3494           q = (int)((q * high_err_target) / kf_err);
3495           q = VPXMIN(q, (q_high + q_low) >> 1);
3496         } else if (kf_err < low_err_target &&
3497                    rc->projected_frame_size >= frame_under_shoot_limit) {
3498           // The key frame is much better than the previous frame
3499           // Raise q_low
3500           q_low = q < q_high ? q + 1 : q_high;
3501
3502           // Adjust Q
3503           q = (int)((q * low_err_target) / kf_err);
3504           q = VPXMIN(q, (q_high + q_low + 1) >> 1);
3505         }
3506
3507         // Clamp Q to upper and lower limits:
3508         q = clamp(q, q_low, q_high);
3509
3510         loop = q != last_q;
3511       } else if (recode_loop_test(cpi, frame_over_shoot_limit,
3512                                   frame_under_shoot_limit, q,
3513                                   VPXMAX(q_high, top_index), bottom_index)) {
3514         // Is the projected frame size out of range and are we allowed
3515         // to attempt to recode.
3516         int last_q = q;
3517         int retries = 0;
3518         int qstep;
3519
3520         if (cpi->resize_pending == 1) {
3521           // Change in frame size so go back around the recode loop.
3522           cpi->rc.frame_size_selector =
3523               SCALE_STEP1 - cpi->rc.frame_size_selector;
3524           cpi->rc.next_frame_size_selector = cpi->rc.frame_size_selector;
3525
3526 #if CONFIG_INTERNAL_STATS
3527           ++cpi->tot_recode_hits;
3528 #endif
3529           ++loop_count;
3530           loop = 1;
3531           continue;
3532         }
3533
3534         // Frame size out of permitted range:
3535         // Update correction factor & compute new Q to try...
3536
3537         // Frame is too large
3538         if (rc->projected_frame_size > rc->this_frame_target) {
3539           // Special case if the projected size is > the max allowed.
3540           if (rc->projected_frame_size >= rc->max_frame_bandwidth) {
3541             double q_val_high;
3542             q_val_high = vp9_convert_qindex_to_q(q_high, cm->bit_depth);
3543             q_val_high = q_val_high * ((double)rc->projected_frame_size /
3544                                        rc->max_frame_bandwidth);
3545             q_high = vp9_convert_q_to_qindex(q_val_high, cm->bit_depth);
3546             q_high = clamp(q_high, rc->best_quality, rc->worst_quality);
3547           }
3548
3549           // Raise Qlow as to at least the current value
3550           qstep =
3551               get_qstep_adj(rc->projected_frame_size, rc->this_frame_target);
3552           q_low = VPXMIN(q + qstep, q_high);
3553           // q_low = q < q_high ? q + 1 : q_high;
3554
3555           if (undershoot_seen || loop_at_this_size > 1) {
3556             // Update rate_correction_factor unless
3557             vp9_rc_update_rate_correction_factors(cpi);
3558
3559             q = (q_high + q_low + 1) / 2;
3560           } else {
3561             // Update rate_correction_factor unless
3562             vp9_rc_update_rate_correction_factors(cpi);
3563
3564             q = vp9_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
3565                                   VPXMAX(q_high, top_index));
3566
3567             while (q < q_low && retries < 10) {
3568               vp9_rc_update_rate_correction_factors(cpi);
3569               q = vp9_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
3570                                     VPXMAX(q_high, top_index));
3571               retries++;
3572             }
3573           }
3574
3575           overshoot_seen = 1;
3576         } else {
3577           // Frame is too small
3578           qstep =
3579               get_qstep_adj(rc->this_frame_target, rc->projected_frame_size);
3580           q_high = VPXMAX(q - qstep, q_low);
3581           // q_high = q > q_low ? q - 1 : q_low;
3582
3583           if (overshoot_seen || loop_at_this_size > 1) {
3584             vp9_rc_update_rate_correction_factors(cpi);
3585             q = (q_high + q_low) / 2;
3586           } else {
3587             vp9_rc_update_rate_correction_factors(cpi);
3588             q = vp9_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
3589                                   top_index);
3590             // Special case reset for qlow for constrained quality.
3591             // This should only trigger where there is very substantial
3592             // undershoot on a frame and the auto cq level is above
3593             // the user passsed in value.
3594             if (cpi->oxcf.rc_mode == VPX_CQ && q < q_low) {
3595               q_low = q;
3596             }
3597
3598             while (q > q_high && retries < 10) {
3599               vp9_rc_update_rate_correction_factors(cpi);
3600               q = vp9_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
3601                                     top_index);
3602               retries++;
3603             }
3604           }
3605
3606           undershoot_seen = 1;
3607         }
3608
3609         // Clamp Q to upper and lower limits:
3610         q = clamp(q, q_low, q_high);
3611
3612         loop = (q != last_q);
3613       } else {
3614         loop = 0;
3615       }
3616     }
3617
3618     // Special case for overlay frame.
3619     if (rc->is_src_frame_alt_ref &&
3620         rc->projected_frame_size < rc->max_frame_bandwidth)
3621       loop = 0;
3622
3623     if (loop) {
3624       ++loop_count;
3625       ++loop_at_this_size;
3626
3627 #if CONFIG_INTERNAL_STATS
3628       ++cpi->tot_recode_hits;
3629 #endif
3630     }
3631
3632     if (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF)
3633       if (loop || !enable_acl) restore_coding_context(cpi);
3634   } while (loop);
3635
3636 #ifdef AGGRESSIVE_VBR
3637   if (two_pass_first_group_inter(cpi)) {
3638     cpi->twopass.active_worst_quality =
3639         VPXMIN(q + qrange_adj, cpi->oxcf.worst_allowed_q);
3640   }
3641 #endif
3642
3643   if (enable_acl) {
3644     vp9_encode_frame(cpi);
3645     vpx_clear_system_state();
3646     restore_coding_context(cpi);
3647     vp9_pack_bitstream(cpi, dest, size);
3648
3649     vp9_encode_frame(cpi);
3650     vpx_clear_system_state();
3651
3652     restore_coding_context(cpi);
3653   }
3654 }
3655
3656 static int get_ref_frame_flags(const VP9_COMP *cpi) {
3657   const int *const map = cpi->common.ref_frame_map;
3658   const int gold_is_last = map[cpi->gld_fb_idx] == map[cpi->lst_fb_idx];
3659   const int alt_is_last = map[cpi->alt_fb_idx] == map[cpi->lst_fb_idx];
3660   const int gold_is_alt = map[cpi->gld_fb_idx] == map[cpi->alt_fb_idx];
3661   int flags = VP9_ALT_FLAG | VP9_GOLD_FLAG | VP9_LAST_FLAG;
3662
3663   if (gold_is_last) flags &= ~VP9_GOLD_FLAG;
3664
3665   if (cpi->rc.frames_till_gf_update_due == INT_MAX &&
3666       (cpi->svc.number_temporal_layers == 1 &&
3667        cpi->svc.number_spatial_layers == 1))
3668     flags &= ~VP9_GOLD_FLAG;
3669
3670   if (alt_is_last) flags &= ~VP9_ALT_FLAG;
3671
3672   if (gold_is_alt) flags &= ~VP9_ALT_FLAG;
3673
3674   return flags;
3675 }
3676
3677 static void set_ext_overrides(VP9_COMP *cpi) {
3678   // Overrides the defaults with the externally supplied values with
3679   // vp9_update_reference() and vp9_update_entropy() calls
3680   // Note: The overrides are valid only for the next frame passed
3681   // to encode_frame_to_data_rate() function
3682   if (cpi->ext_refresh_frame_context_pending) {
3683     cpi->common.refresh_frame_context = cpi->ext_refresh_frame_context;
3684     cpi->ext_refresh_frame_context_pending = 0;
3685   }
3686   if (cpi->ext_refresh_frame_flags_pending) {
3687     cpi->refresh_last_frame = cpi->ext_refresh_last_frame;
3688     cpi->refresh_golden_frame = cpi->ext_refresh_golden_frame;
3689     cpi->refresh_alt_ref_frame = cpi->ext_refresh_alt_ref_frame;
3690   }
3691 }
3692
3693 YV12_BUFFER_CONFIG *vp9_svc_twostage_scale(
3694     VP9_COMMON *cm, YV12_BUFFER_CONFIG *unscaled, YV12_BUFFER_CONFIG *scaled,
3695     YV12_BUFFER_CONFIG *scaled_temp, int phase_scaler, int phase_scaler2) {
3696   if (cm->mi_cols * MI_SIZE != unscaled->y_width ||
3697       cm->mi_rows * MI_SIZE != unscaled->y_height) {
3698 #if CONFIG_VP9_HIGHBITDEPTH
3699     if (cm->bit_depth == VPX_BITS_8) {
3700       vp9_scale_and_extend_frame(unscaled, scaled_temp, phase_scaler2);
3701       vp9_scale_and_extend_frame(scaled_temp, scaled, phase_scaler);
3702     } else {
3703       scale_and_extend_frame(unscaled, scaled_temp, (int)cm->bit_depth,
3704                              phase_scaler2);
3705       scale_and_extend_frame(scaled_temp, scaled, (int)cm->bit_depth,
3706                              phase_scaler);
3707     }
3708 #else
3709     vp9_scale_and_extend_frame(unscaled, scaled_temp, phase_scaler2);
3710     vp9_scale_and_extend_frame(scaled_temp, scaled, phase_scaler);
3711 #endif  // CONFIG_VP9_HIGHBITDEPTH
3712     return scaled;
3713   } else {
3714     return unscaled;
3715   }
3716 }
3717
3718 YV12_BUFFER_CONFIG *vp9_scale_if_required(VP9_COMMON *cm,
3719                                           YV12_BUFFER_CONFIG *unscaled,
3720                                           YV12_BUFFER_CONFIG *scaled,
3721                                           int use_normative_scaler,
3722                                           int phase_scaler) {
3723   if (cm->mi_cols * MI_SIZE != unscaled->y_width ||
3724       cm->mi_rows * MI_SIZE != unscaled->y_height) {
3725 #if CONFIG_VP9_HIGHBITDEPTH
3726     if (use_normative_scaler && unscaled->y_width <= (scaled->y_width << 1) &&
3727         unscaled->y_height <= (scaled->y_height << 1))
3728       if (cm->bit_depth == VPX_BITS_8)
3729         vp9_scale_and_extend_frame(unscaled, scaled, phase_scaler);
3730       else
3731         scale_and_extend_frame(unscaled, scaled, (int)cm->bit_depth,
3732                                phase_scaler);
3733     else
3734       scale_and_extend_frame_nonnormative(unscaled, scaled, (int)cm->bit_depth);
3735 #else
3736     if (use_normative_scaler && unscaled->y_width <= (scaled->y_width << 1) &&
3737         unscaled->y_height <= (scaled->y_height << 1))
3738       vp9_scale_and_extend_frame(unscaled, scaled, phase_scaler);
3739     else
3740       scale_and_extend_frame_nonnormative(unscaled, scaled);
3741 #endif  // CONFIG_VP9_HIGHBITDEPTH
3742     return scaled;
3743   } else {
3744     return unscaled;
3745   }
3746 }
3747
3748 static void set_arf_sign_bias(VP9_COMP *cpi) {
3749   VP9_COMMON *const cm = &cpi->common;
3750   int arf_sign_bias;
3751
3752   if ((cpi->oxcf.pass == 2) && cpi->multi_arf_allowed) {
3753     const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
3754     arf_sign_bias = cpi->rc.source_alt_ref_active &&
3755                     (!cpi->refresh_alt_ref_frame ||
3756                      (gf_group->rf_level[gf_group->index] == GF_ARF_LOW));
3757   } else {
3758     arf_sign_bias =
3759         (cpi->rc.source_alt_ref_active && !cpi->refresh_alt_ref_frame);
3760   }
3761   cm->ref_frame_sign_bias[ALTREF_FRAME] = arf_sign_bias;
3762 }
3763
3764 static int setup_interp_filter_search_mask(VP9_COMP *cpi) {
3765   INTERP_FILTER ifilter;
3766   int ref_total[MAX_REF_FRAMES] = { 0 };
3767   MV_REFERENCE_FRAME ref;
3768   int mask = 0;
3769   if (cpi->common.last_frame_type == KEY_FRAME || cpi->refresh_alt_ref_frame)
3770     return mask;
3771   for (ref = LAST_FRAME; ref <= ALTREF_FRAME; ++ref)
3772     for (ifilter = EIGHTTAP; ifilter <= EIGHTTAP_SHARP; ++ifilter)
3773       ref_total[ref] += cpi->interp_filter_selected[ref][ifilter];
3774
3775   for (ifilter = EIGHTTAP; ifilter <= EIGHTTAP_SHARP; ++ifilter) {
3776     if ((ref_total[LAST_FRAME] &&
3777          cpi->interp_filter_selected[LAST_FRAME][ifilter] == 0) &&
3778         (ref_total[GOLDEN_FRAME] == 0 ||
3779          cpi->interp_filter_selected[GOLDEN_FRAME][ifilter] * 50 <
3780              ref_total[GOLDEN_FRAME]) &&
3781         (ref_total[ALTREF_FRAME] == 0 ||
3782          cpi->interp_filter_selected[ALTREF_FRAME][ifilter] * 50 <
3783              ref_total[ALTREF_FRAME]))
3784       mask |= 1 << ifilter;
3785   }
3786   return mask;
3787 }
3788
3789 #ifdef ENABLE_KF_DENOISE
3790 // Baseline Kernal weights for denoise
3791 static uint8_t dn_kernal_3[9] = { 1, 2, 1, 2, 4, 2, 1, 2, 1 };
3792 static uint8_t dn_kernal_5[25] = { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 2, 4,
3793                                    2, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1 };
3794
3795 static INLINE void add_denoise_point(int centre_val, int data_val, int thresh,
3796                                      uint8_t point_weight, int *sum_val,
3797                                      int *sum_weight) {
3798   if (abs(centre_val - data_val) <= thresh) {
3799     *sum_weight += point_weight;
3800     *sum_val += (int)data_val * (int)point_weight;
3801   }
3802 }
3803
3804 static void spatial_denoise_point(uint8_t *src_ptr, const int stride,
3805                                   const int strength) {
3806   int sum_weight = 0;
3807   int sum_val = 0;
3808   int thresh = strength;
3809   int kernal_size = 5;
3810   int half_k_size = 2;
3811   int i, j;
3812   int max_diff = 0;
3813   uint8_t *tmp_ptr;
3814   uint8_t *kernal_ptr;
3815
3816   // Find the maximum deviation from the source point in the locale.
3817   tmp_ptr = src_ptr - (stride * (half_k_size + 1)) - (half_k_size + 1);
3818   for (i = 0; i < kernal_size + 2; ++i) {
3819     for (j = 0; j < kernal_size + 2; ++j) {
3820       max_diff = VPXMAX(max_diff, abs((int)*src_ptr - (int)tmp_ptr[j]));
3821     }
3822     tmp_ptr += stride;
3823   }
3824
3825   // Select the kernal size.
3826   if (max_diff > (strength + (strength >> 1))) {
3827     kernal_size = 3;
3828     half_k_size = 1;
3829     thresh = thresh >> 1;
3830   }
3831   kernal_ptr = (kernal_size == 3) ? dn_kernal_3 : dn_kernal_5;
3832
3833   // Apply the kernal
3834   tmp_ptr = src_ptr - (stride * half_k_size) - half_k_size;
3835   for (i = 0; i < kernal_size; ++i) {
3836     for (j = 0; j < kernal_size; ++j) {
3837       add_denoise_point((int)*src_ptr, (int)tmp_ptr[j], thresh, *kernal_ptr,
3838                         &sum_val, &sum_weight);
3839       ++kernal_ptr;
3840     }
3841     tmp_ptr += stride;
3842   }
3843
3844   // Update the source value with the new filtered value
3845   *src_ptr = (uint8_t)((sum_val + (sum_weight >> 1)) / sum_weight);
3846 }
3847
3848 #if CONFIG_VP9_HIGHBITDEPTH
3849 static void highbd_spatial_denoise_point(uint16_t *src_ptr, const int stride,
3850                                          const int strength) {
3851   int sum_weight = 0;
3852   int sum_val = 0;
3853   int thresh = strength;
3854   int kernal_size = 5;
3855   int half_k_size = 2;
3856   int i, j;
3857   int max_diff = 0;
3858   uint16_t *tmp_ptr;
3859   uint8_t *kernal_ptr;
3860
3861   // Find the maximum deviation from the source point in the locale.
3862   tmp_ptr = src_ptr - (stride * (half_k_size + 1)) - (half_k_size + 1);
3863   for (i = 0; i < kernal_size + 2; ++i) {
3864     for (j = 0; j < kernal_size + 2; ++j) {
3865       max_diff = VPXMAX(max_diff, abs((int)src_ptr - (int)tmp_ptr[j]));
3866     }
3867     tmp_ptr += stride;
3868   }
3869
3870   // Select the kernal size.
3871   if (max_diff > (strength + (strength >> 1))) {
3872     kernal_size = 3;
3873     half_k_size = 1;
3874     thresh = thresh >> 1;
3875   }
3876   kernal_ptr = (kernal_size == 3) ? dn_kernal_3 : dn_kernal_5;
3877
3878   // Apply the kernal
3879   tmp_ptr = src_ptr - (stride * half_k_size) - half_k_size;
3880   for (i = 0; i < kernal_size; ++i) {
3881     for (j = 0; j < kernal_size; ++j) {
3882       add_denoise_point((int)*src_ptr, (int)tmp_ptr[j], thresh, *kernal_ptr,
3883                         &sum_val, &sum_weight);
3884       ++kernal_ptr;
3885     }
3886     tmp_ptr += stride;
3887   }
3888
3889   // Update the source value with the new filtered value
3890   *src_ptr = (uint16_t)((sum_val + (sum_weight >> 1)) / sum_weight);
3891 }
3892 #endif  // CONFIG_VP9_HIGHBITDEPTH
3893
3894 // Apply thresholded spatial noise supression to a given buffer.
3895 static void spatial_denoise_buffer(VP9_COMP *cpi, uint8_t *buffer,
3896                                    const int stride, const int width,
3897                                    const int height, const int strength) {
3898   VP9_COMMON *const cm = &cpi->common;
3899   uint8_t *src_ptr = buffer;
3900   int row;
3901   int col;
3902
3903   for (row = 0; row < height; ++row) {
3904     for (col = 0; col < width; ++col) {
3905 #if CONFIG_VP9_HIGHBITDEPTH
3906       if (cm->use_highbitdepth)
3907         highbd_spatial_denoise_point(CONVERT_TO_SHORTPTR(&src_ptr[col]), stride,
3908                                      strength);
3909       else
3910         spatial_denoise_point(&src_ptr[col], stride, strength);
3911 #else
3912       spatial_denoise_point(&src_ptr[col], stride, strength);
3913 #endif  // CONFIG_VP9_HIGHBITDEPTH
3914     }
3915     src_ptr += stride;
3916   }
3917 }
3918
3919 // Apply thresholded spatial noise supression to source.
3920 static void spatial_denoise_frame(VP9_COMP *cpi) {
3921   YV12_BUFFER_CONFIG *src = cpi->Source;
3922   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
3923   TWO_PASS *const twopass = &cpi->twopass;
3924   VP9_COMMON *const cm = &cpi->common;
3925
3926   // Base the filter strength on the current active max Q.
3927   const int q = (int)(vp9_convert_qindex_to_q(twopass->active_worst_quality,
3928                                               cm->bit_depth));
3929   int strength =
3930       VPXMAX(oxcf->arnr_strength >> 2, VPXMIN(oxcf->arnr_strength, (q >> 4)));
3931
3932   // Denoise each of Y,U and V buffers.
3933   spatial_denoise_buffer(cpi, src->y_buffer, src->y_stride, src->y_width,
3934                          src->y_height, strength);
3935
3936   strength += (strength >> 1);
3937   spatial_denoise_buffer(cpi, src->u_buffer, src->uv_stride, src->uv_width,
3938                          src->uv_height, strength << 1);
3939
3940   spatial_denoise_buffer(cpi, src->v_buffer, src->uv_stride, src->uv_width,
3941                          src->uv_height, strength << 1);
3942 }
3943 #endif  // ENABLE_KF_DENOISE
3944
3945 static void vp9_try_disable_lookahead_aq(VP9_COMP *cpi, size_t *size,
3946                                          uint8_t *dest) {
3947   if (cpi->common.seg.enabled)
3948     if (ALT_REF_AQ_PROTECT_GAIN) {
3949       size_t nsize = *size;
3950       int overhead;
3951
3952       // TODO(yuryg): optimize this, as
3953       // we don't really need to repack
3954
3955       save_coding_context(cpi);
3956       vp9_disable_segmentation(&cpi->common.seg);
3957       vp9_pack_bitstream(cpi, dest, &nsize);
3958       restore_coding_context(cpi);
3959
3960       overhead = (int)*size - (int)nsize;
3961
3962       if (vp9_alt_ref_aq_disable_if(cpi->alt_ref_aq, overhead, (int)*size))
3963         vp9_encode_frame(cpi);
3964       else
3965         vp9_enable_segmentation(&cpi->common.seg);
3966     }
3967 }
3968
3969 static void encode_frame_to_data_rate(VP9_COMP *cpi, size_t *size,
3970                                       uint8_t *dest,
3971                                       unsigned int *frame_flags) {
3972   VP9_COMMON *const cm = &cpi->common;
3973   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
3974   struct segmentation *const seg = &cm->seg;
3975   TX_SIZE t;
3976
3977   set_ext_overrides(cpi);
3978   vpx_clear_system_state();
3979
3980 #ifdef ENABLE_KF_DENOISE
3981   // Spatial denoise of key frame.
3982   if (is_spatial_denoise_enabled(cpi)) spatial_denoise_frame(cpi);
3983 #endif
3984
3985   // Set the arf sign bias for this frame.
3986   set_arf_sign_bias(cpi);
3987
3988   // Set default state for segment based loop filter update flags.
3989   cm->lf.mode_ref_delta_update = 0;
3990
3991   if (cpi->oxcf.pass == 2 && cpi->sf.adaptive_interp_filter_search)
3992     cpi->sf.interp_filter_search_mask = setup_interp_filter_search_mask(cpi);
3993
3994   // Set various flags etc to special state if it is a key frame.
3995   if (frame_is_intra_only(cm)) {
3996     // Reset the loop filter deltas and segmentation map.
3997     vp9_reset_segment_features(&cm->seg);
3998
3999     // If segmentation is enabled force a map update for key frames.
4000     if (seg->enabled) {
4001       seg->update_map = 1;
4002       seg->update_data = 1;
4003     }
4004
4005     // The alternate reference frame cannot be active for a key frame.
4006     cpi->rc.source_alt_ref_active = 0;
4007
4008     cm->error_resilient_mode = oxcf->error_resilient_mode;
4009     cm->frame_parallel_decoding_mode = oxcf->frame_parallel_decoding_mode;
4010
4011     // By default, encoder assumes decoder can use prev_mi.
4012     if (cm->error_resilient_mode) {
4013       cm->frame_parallel_decoding_mode = 1;
4014       cm->reset_frame_context = 0;
4015       cm->refresh_frame_context = 0;
4016     } else if (cm->intra_only) {
4017       // Only reset the current context.
4018       cm->reset_frame_context = 2;
4019     }
4020   }
4021   if (is_two_pass_svc(cpi) && cm->error_resilient_mode == 0) {
4022     // Use context 0 for intra only empty frame, but the last frame context
4023     // for other empty frames.
4024     if (cpi->svc.encode_empty_frame_state == ENCODING) {
4025       if (cpi->svc.encode_intra_empty_frame != 0)
4026         cm->frame_context_idx = 0;
4027       else
4028         cm->frame_context_idx = FRAME_CONTEXTS - 1;
4029     } else {
4030       cm->frame_context_idx =
4031           cpi->svc.spatial_layer_id * cpi->svc.number_temporal_layers +
4032           cpi->svc.temporal_layer_id;
4033     }
4034
4035     cm->frame_parallel_decoding_mode = oxcf->frame_parallel_decoding_mode;
4036
4037     // The probs will be updated based on the frame type of its previous
4038     // frame if frame_parallel_decoding_mode is 0. The type may vary for
4039     // the frame after a key frame in base layer since we may drop enhancement
4040     // layers. So set frame_parallel_decoding_mode to 1 in this case.
4041     if (cm->frame_parallel_decoding_mode == 0) {
4042       if (cpi->svc.number_temporal_layers == 1) {
4043         if (cpi->svc.spatial_layer_id == 0 &&
4044             cpi->svc.layer_context[0].last_frame_type == KEY_FRAME)
4045           cm->frame_parallel_decoding_mode = 1;
4046       } else if (cpi->svc.spatial_layer_id == 0) {
4047         // Find the 2nd frame in temporal base layer and 1st frame in temporal
4048         // enhancement layers from the key frame.
4049         int i;
4050         for (i = 0; i < cpi->svc.number_temporal_layers; ++i) {
4051           if (cpi->svc.layer_context[0].frames_from_key_frame == 1 << i) {
4052             cm->frame_parallel_decoding_mode = 1;
4053             break;
4054           }
4055         }
4056       }
4057     }
4058   }
4059
4060   // For 1 pass CBR, check if we are dropping this frame.
4061   // For spatial layers, for now only check for frame-dropping on first spatial
4062   // layer, and if decision is to drop, we drop whole super-frame.
4063   if (oxcf->pass == 0 && oxcf->rc_mode == VPX_CBR &&
4064       cm->frame_type != KEY_FRAME) {
4065     if (vp9_rc_drop_frame(cpi) ||
4066         (is_one_pass_cbr_svc(cpi) && cpi->svc.rc_drop_superframe == 1)) {
4067       vp9_rc_postencode_update_drop_frame(cpi);
4068       ++cm->current_video_frame;
4069       cpi->ext_refresh_frame_flags_pending = 0;
4070       cpi->svc.rc_drop_superframe = 1;
4071       // TODO(marpan): Advancing the svc counters on dropped frames can break
4072       // the referencing scheme for the fixed svc patterns defined in
4073       // vp9_one_pass_cbr_svc_start_layer(). Look into fixing this issue, but
4074       // for now, don't advance the svc frame counters on dropped frame.
4075       // if (cpi->use_svc)
4076       //   vp9_inc_frame_in_layer(cpi);
4077       return;
4078     }
4079   }
4080
4081   vpx_clear_system_state();
4082
4083 #if CONFIG_INTERNAL_STATS
4084   memset(cpi->mode_chosen_counts, 0,
4085          MAX_MODES * sizeof(*cpi->mode_chosen_counts));
4086 #endif
4087
4088   if (cpi->sf.recode_loop == DISALLOW_RECODE) {
4089     encode_without_recode_loop(cpi, size, dest);
4090   } else {
4091     encode_with_recode_loop(cpi, size, dest);
4092   }
4093
4094   // Disable segmentation if it decrease rate/distortion ratio
4095   if (cpi->oxcf.aq_mode == LOOKAHEAD_AQ)
4096     vp9_try_disable_lookahead_aq(cpi, size, dest);
4097
4098 #if CONFIG_VP9_TEMPORAL_DENOISING
4099 #ifdef OUTPUT_YUV_DENOISED
4100   if (oxcf->noise_sensitivity > 0 && denoise_svc(cpi)) {
4101     vp9_write_yuv_frame_420(&cpi->denoiser.running_avg_y[INTRA_FRAME],
4102                             yuv_denoised_file);
4103   }
4104 #endif
4105 #endif
4106 #ifdef OUTPUT_YUV_SKINMAP
4107   if (cpi->common.current_video_frame > 1) {
4108     vp9_compute_skin_map(cpi, yuv_skinmap_file);
4109   }
4110 #endif
4111
4112   // Special case code to reduce pulsing when key frames are forced at a
4113   // fixed interval. Note the reconstruction error if it is the frame before
4114   // the force key frame
4115   if (cpi->rc.next_key_frame_forced && cpi->rc.frames_to_key == 1) {
4116 #if CONFIG_VP9_HIGHBITDEPTH
4117     if (cm->use_highbitdepth) {
4118       cpi->ambient_err =
4119           vpx_highbd_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
4120     } else {
4121       cpi->ambient_err = vpx_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
4122     }
4123 #else
4124     cpi->ambient_err = vpx_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
4125 #endif  // CONFIG_VP9_HIGHBITDEPTH
4126   }
4127
4128   // If the encoder forced a KEY_FRAME decision
4129   if (cm->frame_type == KEY_FRAME) cpi->refresh_last_frame = 1;
4130
4131   cm->frame_to_show = get_frame_new_buffer(cm);
4132   cm->frame_to_show->color_space = cm->color_space;
4133   cm->frame_to_show->color_range = cm->color_range;
4134   cm->frame_to_show->render_width = cm->render_width;
4135   cm->frame_to_show->render_height = cm->render_height;
4136
4137   // Pick the loop filter level for the frame.
4138   loopfilter_frame(cpi, cm);
4139
4140   // build the bitstream
4141   vp9_pack_bitstream(cpi, dest, size);
4142
4143   if (cm->seg.update_map) update_reference_segmentation_map(cpi);
4144
4145   if (frame_is_intra_only(cm) == 0) {
4146     release_scaled_references(cpi);
4147   }
4148   vp9_update_reference_frames(cpi);
4149
4150   for (t = TX_4X4; t <= TX_32X32; t++)
4151     full_to_model_counts(cpi->td.counts->coef[t],
4152                          cpi->td.rd_counts.coef_counts[t]);
4153
4154   if (!cm->error_resilient_mode && !cm->frame_parallel_decoding_mode)
4155     vp9_adapt_coef_probs(cm);
4156
4157   if (!frame_is_intra_only(cm)) {
4158     if (!cm->error_resilient_mode && !cm->frame_parallel_decoding_mode) {
4159       vp9_adapt_mode_probs(cm);
4160       vp9_adapt_mv_probs(cm, cm->allow_high_precision_mv);
4161     }
4162   }
4163
4164   cpi->ext_refresh_frame_flags_pending = 0;
4165
4166   if (cpi->refresh_golden_frame == 1)
4167     cpi->frame_flags |= FRAMEFLAGS_GOLDEN;
4168   else
4169     cpi->frame_flags &= ~FRAMEFLAGS_GOLDEN;
4170
4171   if (cpi->refresh_alt_ref_frame == 1)
4172     cpi->frame_flags |= FRAMEFLAGS_ALTREF;
4173   else
4174     cpi->frame_flags &= ~FRAMEFLAGS_ALTREF;
4175
4176   cpi->ref_frame_flags = get_ref_frame_flags(cpi);
4177
4178   cm->last_frame_type = cm->frame_type;
4179
4180   if (!(is_two_pass_svc(cpi) && cpi->svc.encode_empty_frame_state == ENCODING))
4181     vp9_rc_postencode_update(cpi, *size);
4182
4183 #if 0
4184   output_frame_level_debug_stats(cpi);
4185 #endif
4186
4187   if (cm->frame_type == KEY_FRAME) {
4188     // Tell the caller that the frame was coded as a key frame
4189     *frame_flags = cpi->frame_flags | FRAMEFLAGS_KEY;
4190   } else {
4191     *frame_flags = cpi->frame_flags & ~FRAMEFLAGS_KEY;
4192   }
4193
4194   // Clear the one shot update flags for segmentation map and mode/ref loop
4195   // filter deltas.
4196   cm->seg.update_map = 0;
4197   cm->seg.update_data = 0;
4198   cm->lf.mode_ref_delta_update = 0;
4199
4200   // keep track of the last coded dimensions
4201   cm->last_width = cm->width;
4202   cm->last_height = cm->height;
4203
4204   // reset to normal state now that we are done.
4205   if (!cm->show_existing_frame) cm->last_show_frame = cm->show_frame;
4206
4207   if (cm->show_frame) {
4208     vp9_swap_mi_and_prev_mi(cm);
4209     // Don't increment frame counters if this was an altref buffer
4210     // update not a real frame
4211     ++cm->current_video_frame;
4212     if (cpi->use_svc) vp9_inc_frame_in_layer(cpi);
4213   }
4214   cm->prev_frame = cm->cur_frame;
4215
4216   if (cpi->use_svc)
4217     cpi->svc
4218         .layer_context[cpi->svc.spatial_layer_id *
4219                            cpi->svc.number_temporal_layers +
4220                        cpi->svc.temporal_layer_id]
4221         .last_frame_type = cm->frame_type;
4222
4223   cpi->force_update_segmentation = 0;
4224
4225   if (cpi->oxcf.aq_mode == LOOKAHEAD_AQ)
4226     vp9_alt_ref_aq_unset_all(cpi->alt_ref_aq, cpi);
4227 }
4228
4229 static void SvcEncode(VP9_COMP *cpi, size_t *size, uint8_t *dest,
4230                       unsigned int *frame_flags) {
4231   vp9_rc_get_svc_params(cpi);
4232   encode_frame_to_data_rate(cpi, size, dest, frame_flags);
4233 }
4234
4235 static void Pass0Encode(VP9_COMP *cpi, size_t *size, uint8_t *dest,
4236                         unsigned int *frame_flags) {
4237   if (cpi->oxcf.rc_mode == VPX_CBR) {
4238     vp9_rc_get_one_pass_cbr_params(cpi);
4239   } else {
4240     vp9_rc_get_one_pass_vbr_params(cpi);
4241   }
4242   encode_frame_to_data_rate(cpi, size, dest, frame_flags);
4243 }
4244
4245 static void Pass2Encode(VP9_COMP *cpi, size_t *size, uint8_t *dest,
4246                         unsigned int *frame_flags) {
4247   cpi->allow_encode_breakout = ENCODE_BREAKOUT_ENABLED;
4248   encode_frame_to_data_rate(cpi, size, dest, frame_flags);
4249
4250   if (!(is_two_pass_svc(cpi) && cpi->svc.encode_empty_frame_state == ENCODING))
4251     vp9_twopass_postencode_update(cpi);
4252 }
4253
4254 static void init_ref_frame_bufs(VP9_COMMON *cm) {
4255   int i;
4256   BufferPool *const pool = cm->buffer_pool;
4257   cm->new_fb_idx = INVALID_IDX;
4258   for (i = 0; i < REF_FRAMES; ++i) {
4259     cm->ref_frame_map[i] = INVALID_IDX;
4260     pool->frame_bufs[i].ref_count = 0;
4261   }
4262 }
4263
4264 static void check_initial_width(VP9_COMP *cpi,
4265 #if CONFIG_VP9_HIGHBITDEPTH
4266                                 int use_highbitdepth,
4267 #endif
4268                                 int subsampling_x, int subsampling_y) {
4269   VP9_COMMON *const cm = &cpi->common;
4270
4271   if (!cpi->initial_width ||
4272 #if CONFIG_VP9_HIGHBITDEPTH
4273       cm->use_highbitdepth != use_highbitdepth ||
4274 #endif
4275       cm->subsampling_x != subsampling_x ||
4276       cm->subsampling_y != subsampling_y) {
4277     cm->subsampling_x = subsampling_x;
4278     cm->subsampling_y = subsampling_y;
4279 #if CONFIG_VP9_HIGHBITDEPTH
4280     cm->use_highbitdepth = use_highbitdepth;
4281 #endif
4282
4283     alloc_raw_frame_buffers(cpi);
4284     init_ref_frame_bufs(cm);
4285     alloc_util_frame_buffers(cpi);
4286
4287     init_motion_estimation(cpi);  // TODO(agrange) This can be removed.
4288
4289     cpi->initial_width = cm->width;
4290     cpi->initial_height = cm->height;
4291     cpi->initial_mbs = cm->MBs;
4292   }
4293 }
4294
4295 int vp9_receive_raw_frame(VP9_COMP *cpi, vpx_enc_frame_flags_t frame_flags,
4296                           YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
4297                           int64_t end_time) {
4298   VP9_COMMON *const cm = &cpi->common;
4299   struct vpx_usec_timer timer;
4300   int res = 0;
4301   const int subsampling_x = sd->subsampling_x;
4302   const int subsampling_y = sd->subsampling_y;
4303 #if CONFIG_VP9_HIGHBITDEPTH
4304   const int use_highbitdepth = (sd->flags & YV12_FLAG_HIGHBITDEPTH) != 0;
4305 #endif
4306
4307 #if CONFIG_VP9_HIGHBITDEPTH
4308   check_initial_width(cpi, use_highbitdepth, subsampling_x, subsampling_y);
4309 #else
4310   check_initial_width(cpi, subsampling_x, subsampling_y);
4311 #endif  // CONFIG_VP9_HIGHBITDEPTH
4312
4313 #if CONFIG_VP9_TEMPORAL_DENOISING
4314   setup_denoiser_buffer(cpi);
4315 #endif
4316   vpx_usec_timer_start(&timer);
4317
4318   if (vp9_lookahead_push(cpi->lookahead, sd, time_stamp, end_time,
4319 #if CONFIG_VP9_HIGHBITDEPTH
4320                          use_highbitdepth,
4321 #endif  // CONFIG_VP9_HIGHBITDEPTH
4322                          frame_flags))
4323     res = -1;
4324   vpx_usec_timer_mark(&timer);
4325   cpi->time_receive_data += vpx_usec_timer_elapsed(&timer);
4326
4327   if ((cm->profile == PROFILE_0 || cm->profile == PROFILE_2) &&
4328       (subsampling_x != 1 || subsampling_y != 1)) {
4329     vpx_internal_error(&cm->error, VPX_CODEC_INVALID_PARAM,
4330                        "Non-4:2:0 color format requires profile 1 or 3");
4331     res = -1;
4332   }
4333   if ((cm->profile == PROFILE_1 || cm->profile == PROFILE_3) &&
4334       (subsampling_x == 1 && subsampling_y == 1)) {
4335     vpx_internal_error(&cm->error, VPX_CODEC_INVALID_PARAM,
4336                        "4:2:0 color format requires profile 0 or 2");
4337     res = -1;
4338   }
4339
4340   return res;
4341 }
4342
4343 static int frame_is_reference(const VP9_COMP *cpi) {
4344   const VP9_COMMON *cm = &cpi->common;
4345
4346   return cm->frame_type == KEY_FRAME || cpi->refresh_last_frame ||
4347          cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame ||
4348          cm->refresh_frame_context || cm->lf.mode_ref_delta_update ||
4349          cm->seg.update_map || cm->seg.update_data;
4350 }
4351
4352 static void adjust_frame_rate(VP9_COMP *cpi,
4353                               const struct lookahead_entry *source) {
4354   int64_t this_duration;
4355   int step = 0;
4356
4357   if (source->ts_start == cpi->first_time_stamp_ever) {
4358     this_duration = source->ts_end - source->ts_start;
4359     step = 1;
4360   } else {
4361     int64_t last_duration =
4362         cpi->last_end_time_stamp_seen - cpi->last_time_stamp_seen;
4363
4364     this_duration = source->ts_end - cpi->last_end_time_stamp_seen;
4365
4366     // do a step update if the duration changes by 10%
4367     if (last_duration)
4368       step = (int)((this_duration - last_duration) * 10 / last_duration);
4369   }
4370
4371   if (this_duration) {
4372     if (step) {
4373       vp9_new_framerate(cpi, 10000000.0 / this_duration);
4374     } else {
4375       // Average this frame's rate into the last second's average
4376       // frame rate. If we haven't seen 1 second yet, then average
4377       // over the whole interval seen.
4378       const double interval = VPXMIN(
4379           (double)(source->ts_end - cpi->first_time_stamp_ever), 10000000.0);
4380       double avg_duration = 10000000.0 / cpi->framerate;
4381       avg_duration *= (interval - avg_duration + this_duration);
4382       avg_duration /= interval;
4383
4384       vp9_new_framerate(cpi, 10000000.0 / avg_duration);
4385     }
4386   }
4387   cpi->last_time_stamp_seen = source->ts_start;
4388   cpi->last_end_time_stamp_seen = source->ts_end;
4389 }
4390
4391 // Returns 0 if this is not an alt ref else the offset of the source frame
4392 // used as the arf midpoint.
4393 static int get_arf_src_index(VP9_COMP *cpi) {
4394   RATE_CONTROL *const rc = &cpi->rc;
4395   int arf_src_index = 0;
4396   if (is_altref_enabled(cpi)) {
4397     if (cpi->oxcf.pass == 2) {
4398       const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
4399       if (gf_group->update_type[gf_group->index] == ARF_UPDATE) {
4400         arf_src_index = gf_group->arf_src_offset[gf_group->index];
4401       }
4402     } else if (rc->source_alt_ref_pending) {
4403       arf_src_index = rc->frames_till_gf_update_due;
4404     }
4405   }
4406   return arf_src_index;
4407 }
4408
4409 static void check_src_altref(VP9_COMP *cpi,
4410                              const struct lookahead_entry *source) {
4411   RATE_CONTROL *const rc = &cpi->rc;
4412
4413   if (cpi->oxcf.pass == 2) {
4414     const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
4415     rc->is_src_frame_alt_ref =
4416         (gf_group->update_type[gf_group->index] == OVERLAY_UPDATE);
4417   } else {
4418     rc->is_src_frame_alt_ref =
4419         cpi->alt_ref_source && (source == cpi->alt_ref_source);
4420   }
4421
4422   if (rc->is_src_frame_alt_ref) {
4423     // Current frame is an ARF overlay frame.
4424     cpi->alt_ref_source = NULL;
4425
4426     // Don't refresh the last buffer for an ARF overlay frame. It will
4427     // become the GF so preserve last as an alternative prediction option.
4428     cpi->refresh_last_frame = 0;
4429   }
4430 }
4431
4432 #if CONFIG_INTERNAL_STATS
4433 extern double vp9_get_blockiness(const uint8_t *img1, int img1_pitch,
4434                                  const uint8_t *img2, int img2_pitch, int width,
4435                                  int height);
4436
4437 static void adjust_image_stat(double y, double u, double v, double all,
4438                               ImageStat *s) {
4439   s->stat[Y] += y;
4440   s->stat[U] += u;
4441   s->stat[V] += v;
4442   s->stat[ALL] += all;
4443   s->worst = VPXMIN(s->worst, all);
4444 }
4445 #endif  // CONFIG_INTERNAL_STATS
4446
4447 // Adjust the maximum allowable frame size for the target level.
4448 static void level_rc_framerate(VP9_COMP *cpi, int arf_src_index) {
4449   RATE_CONTROL *const rc = &cpi->rc;
4450   LevelConstraint *const ls = &cpi->level_constraint;
4451   VP9_COMMON *const cm = &cpi->common;
4452   const double max_cpb_size = ls->max_cpb_size;
4453   vpx_clear_system_state();
4454   rc->max_frame_bandwidth = VPXMIN(rc->max_frame_bandwidth, ls->max_frame_size);
4455   if (frame_is_intra_only(cm)) {
4456     rc->max_frame_bandwidth =
4457         VPXMIN(rc->max_frame_bandwidth, (int)(max_cpb_size * 0.5));
4458   } else if (arf_src_index > 0) {
4459     rc->max_frame_bandwidth =
4460         VPXMIN(rc->max_frame_bandwidth, (int)(max_cpb_size * 0.4));
4461   } else {
4462     rc->max_frame_bandwidth =
4463         VPXMIN(rc->max_frame_bandwidth, (int)(max_cpb_size * 0.2));
4464   }
4465 }
4466
4467 static void update_level_info(VP9_COMP *cpi, size_t *size, int arf_src_index) {
4468   VP9_COMMON *const cm = &cpi->common;
4469   Vp9LevelInfo *const level_info = &cpi->level_info;
4470   Vp9LevelSpec *const level_spec = &level_info->level_spec;
4471   Vp9LevelStats *const level_stats = &level_info->level_stats;
4472   int i, idx;
4473   uint64_t luma_samples, dur_end;
4474   const uint32_t luma_pic_size = cm->width * cm->height;
4475   LevelConstraint *const level_constraint = &cpi->level_constraint;
4476   const int8_t level_index = level_constraint->level_index;
4477   double cpb_data_size;
4478
4479   vpx_clear_system_state();
4480
4481   // update level_stats
4482   level_stats->total_compressed_size += *size;
4483   if (cm->show_frame) {
4484     level_stats->total_uncompressed_size +=
4485         luma_pic_size +
4486         2 * (luma_pic_size >> (cm->subsampling_x + cm->subsampling_y));
4487     level_stats->time_encoded =
4488         (cpi->last_end_time_stamp_seen - cpi->first_time_stamp_ever) /
4489         (double)TICKS_PER_SEC;
4490   }
4491
4492   if (arf_src_index > 0) {
4493     if (!level_stats->seen_first_altref) {
4494       level_stats->seen_first_altref = 1;
4495     } else if (level_stats->frames_since_last_altref <
4496                level_spec->min_altref_distance) {
4497       level_spec->min_altref_distance = level_stats->frames_since_last_altref;
4498     }
4499     level_stats->frames_since_last_altref = 0;
4500   } else {
4501     ++level_stats->frames_since_last_altref;
4502   }
4503
4504   if (level_stats->frame_window_buffer.len < FRAME_WINDOW_SIZE - 1) {
4505     idx = (level_stats->frame_window_buffer.start +
4506            level_stats->frame_window_buffer.len++) %
4507           FRAME_WINDOW_SIZE;
4508   } else {
4509     idx = level_stats->frame_window_buffer.start;
4510     level_stats->frame_window_buffer.start = (idx + 1) % FRAME_WINDOW_SIZE;
4511   }
4512   level_stats->frame_window_buffer.buf[idx].ts = cpi->last_time_stamp_seen;
4513   level_stats->frame_window_buffer.buf[idx].size = (uint32_t)(*size);
4514   level_stats->frame_window_buffer.buf[idx].luma_samples = luma_pic_size;
4515
4516   if (cm->frame_type == KEY_FRAME) {
4517     level_stats->ref_refresh_map = 0;
4518   } else {
4519     int count = 0;
4520     level_stats->ref_refresh_map |= vp9_get_refresh_mask(cpi);
4521     // Also need to consider the case where the encoder refers to a buffer
4522     // that has been implicitly refreshed after encoding a keyframe.
4523     if (!cm->intra_only) {
4524       level_stats->ref_refresh_map |= (1 << cpi->lst_fb_idx);
4525       level_stats->ref_refresh_map |= (1 << cpi->gld_fb_idx);
4526       level_stats->ref_refresh_map |= (1 << cpi->alt_fb_idx);
4527     }
4528     for (i = 0; i < REF_FRAMES; ++i) {
4529       count += (level_stats->ref_refresh_map >> i) & 1;
4530     }
4531     if (count > level_spec->max_ref_frame_buffers) {
4532       level_spec->max_ref_frame_buffers = count;
4533     }
4534   }
4535
4536   // update average_bitrate
4537   level_spec->average_bitrate = (double)level_stats->total_compressed_size /
4538                                 125.0 / level_stats->time_encoded;
4539
4540   // update max_luma_sample_rate
4541   luma_samples = 0;
4542   for (i = 0; i < level_stats->frame_window_buffer.len; ++i) {
4543     idx = (level_stats->frame_window_buffer.start +
4544            level_stats->frame_window_buffer.len - 1 - i) %
4545           FRAME_WINDOW_SIZE;
4546     if (i == 0) {
4547       dur_end = level_stats->frame_window_buffer.buf[idx].ts;
4548     }
4549     if (dur_end - level_stats->frame_window_buffer.buf[idx].ts >=
4550         TICKS_PER_SEC) {
4551       break;
4552     }
4553     luma_samples += level_stats->frame_window_buffer.buf[idx].luma_samples;
4554   }
4555   if (luma_samples > level_spec->max_luma_sample_rate) {
4556     level_spec->max_luma_sample_rate = luma_samples;
4557   }
4558
4559   // update max_cpb_size
4560   cpb_data_size = 0;
4561   for (i = 0; i < CPB_WINDOW_SIZE; ++i) {
4562     if (i >= level_stats->frame_window_buffer.len) break;
4563     idx = (level_stats->frame_window_buffer.start +
4564            level_stats->frame_window_buffer.len - 1 - i) %
4565           FRAME_WINDOW_SIZE;
4566     cpb_data_size += level_stats->frame_window_buffer.buf[idx].size;
4567   }
4568   cpb_data_size = cpb_data_size / 125.0;
4569   if (cpb_data_size > level_spec->max_cpb_size) {
4570     level_spec->max_cpb_size = cpb_data_size;
4571   }
4572
4573   // update max_luma_picture_size
4574   if (luma_pic_size > level_spec->max_luma_picture_size) {
4575     level_spec->max_luma_picture_size = luma_pic_size;
4576   }
4577
4578   // update compression_ratio
4579   level_spec->compression_ratio = (double)level_stats->total_uncompressed_size *
4580                                   cm->bit_depth /
4581                                   level_stats->total_compressed_size / 8.0;
4582
4583   // update max_col_tiles
4584   if (level_spec->max_col_tiles < (1 << cm->log2_tile_cols)) {
4585     level_spec->max_col_tiles = (1 << cm->log2_tile_cols);
4586   }
4587
4588   if (level_index >= 0 && level_constraint->fail_flag == 0) {
4589     if (level_spec->max_luma_picture_size >
4590         vp9_level_defs[level_index].max_luma_picture_size) {
4591       level_constraint->fail_flag |= (1 << LUMA_PIC_SIZE_TOO_LARGE);
4592       vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
4593                          "Failed to encode to the target level %d. %s",
4594                          vp9_level_defs[level_index].level,
4595                          level_fail_messages[LUMA_PIC_SIZE_TOO_LARGE]);
4596     }
4597
4598     if ((double)level_spec->max_luma_sample_rate >
4599         (double)vp9_level_defs[level_index].max_luma_sample_rate *
4600             (1 + SAMPLE_RATE_GRACE_P)) {
4601       level_constraint->fail_flag |= (1 << LUMA_SAMPLE_RATE_TOO_LARGE);
4602       vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
4603                          "Failed to encode to the target level %d. %s",
4604                          vp9_level_defs[level_index].level,
4605                          level_fail_messages[LUMA_SAMPLE_RATE_TOO_LARGE]);
4606     }
4607
4608     if (level_spec->max_col_tiles > vp9_level_defs[level_index].max_col_tiles) {
4609       level_constraint->fail_flag |= (1 << TOO_MANY_COLUMN_TILE);
4610       vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
4611                          "Failed to encode to the target level %d. %s",
4612                          vp9_level_defs[level_index].level,
4613                          level_fail_messages[TOO_MANY_COLUMN_TILE]);
4614     }
4615
4616     if (level_spec->min_altref_distance <
4617         vp9_level_defs[level_index].min_altref_distance) {
4618       level_constraint->fail_flag |= (1 << ALTREF_DIST_TOO_SMALL);
4619       vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
4620                          "Failed to encode to the target level %d. %s",
4621                          vp9_level_defs[level_index].level,
4622                          level_fail_messages[ALTREF_DIST_TOO_SMALL]);
4623     }
4624
4625     if (level_spec->max_ref_frame_buffers >
4626         vp9_level_defs[level_index].max_ref_frame_buffers) {
4627       level_constraint->fail_flag |= (1 << TOO_MANY_REF_BUFFER);
4628       vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
4629                          "Failed to encode to the target level %d. %s",
4630                          vp9_level_defs[level_index].level,
4631                          level_fail_messages[TOO_MANY_REF_BUFFER]);
4632     }
4633
4634     if (level_spec->max_cpb_size > vp9_level_defs[level_index].max_cpb_size) {
4635       level_constraint->fail_flag |= (1 << CPB_TOO_LARGE);
4636       vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
4637                          "Failed to encode to the target level %d. %s",
4638                          vp9_level_defs[level_index].level,
4639                          level_fail_messages[CPB_TOO_LARGE]);
4640     }
4641
4642     // Set an upper bound for the next frame size. It will be used in
4643     // level_rc_framerate() before encoding the next frame.
4644     cpb_data_size = 0;
4645     for (i = 0; i < CPB_WINDOW_SIZE - 1; ++i) {
4646       if (i >= level_stats->frame_window_buffer.len) break;
4647       idx = (level_stats->frame_window_buffer.start +
4648              level_stats->frame_window_buffer.len - 1 - i) %
4649             FRAME_WINDOW_SIZE;
4650       cpb_data_size += level_stats->frame_window_buffer.buf[idx].size;
4651     }
4652     cpb_data_size = cpb_data_size / 125.0;
4653     level_constraint->max_frame_size =
4654         (int)((vp9_level_defs[level_index].max_cpb_size - cpb_data_size) *
4655               1000.0);
4656     if (level_stats->frame_window_buffer.len < CPB_WINDOW_SIZE - 1)
4657       level_constraint->max_frame_size >>= 1;
4658   }
4659 }
4660
4661 int vp9_get_compressed_data(VP9_COMP *cpi, unsigned int *frame_flags,
4662                             size_t *size, uint8_t *dest, int64_t *time_stamp,
4663                             int64_t *time_end, int flush) {
4664   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
4665   VP9_COMMON *const cm = &cpi->common;
4666   BufferPool *const pool = cm->buffer_pool;
4667   RATE_CONTROL *const rc = &cpi->rc;
4668   struct vpx_usec_timer cmptimer;
4669   YV12_BUFFER_CONFIG *force_src_buffer = NULL;
4670   struct lookahead_entry *last_source = NULL;
4671   struct lookahead_entry *source = NULL;
4672   int arf_src_index;
4673   int i;
4674
4675   if (is_two_pass_svc(cpi)) {
4676 #if CONFIG_SPATIAL_SVC
4677     vp9_svc_start_frame(cpi);
4678     // Use a small empty frame instead of a real frame
4679     if (cpi->svc.encode_empty_frame_state == ENCODING)
4680       source = &cpi->svc.empty_frame;
4681 #endif
4682     if (oxcf->pass == 2) vp9_restore_layer_context(cpi);
4683   } else if (is_one_pass_cbr_svc(cpi)) {
4684     vp9_one_pass_cbr_svc_start_layer(cpi);
4685   }
4686
4687   vpx_usec_timer_start(&cmptimer);
4688
4689   vp9_set_high_precision_mv(cpi, ALTREF_HIGH_PRECISION_MV);
4690
4691   // Is multi-arf enabled.
4692   // Note that at the moment multi_arf is only configured for 2 pass VBR and
4693   // will not work properly with svc.
4694   if ((oxcf->pass == 2) && !cpi->use_svc && (cpi->oxcf.enable_auto_arf > 1))
4695     cpi->multi_arf_allowed = 1;
4696   else
4697     cpi->multi_arf_allowed = 0;
4698
4699   // Normal defaults
4700   cm->reset_frame_context = 0;
4701   cm->refresh_frame_context = 1;
4702   if (!is_one_pass_cbr_svc(cpi)) {
4703     cpi->refresh_last_frame = 1;
4704     cpi->refresh_golden_frame = 0;
4705     cpi->refresh_alt_ref_frame = 0;
4706   }
4707
4708   // Should we encode an arf frame.
4709   arf_src_index = get_arf_src_index(cpi);
4710
4711   // Skip alt frame if we encode the empty frame
4712   if (is_two_pass_svc(cpi) && source != NULL) arf_src_index = 0;
4713
4714   if (arf_src_index) {
4715     for (i = 0; i <= arf_src_index; ++i) {
4716       struct lookahead_entry *e = vp9_lookahead_peek(cpi->lookahead, i);
4717       // Avoid creating an alt-ref if there's a forced keyframe pending.
4718       if (e == NULL) {
4719         break;
4720       } else if (e->flags == VPX_EFLAG_FORCE_KF) {
4721         arf_src_index = 0;
4722         flush = 1;
4723         break;
4724       }
4725     }
4726   }
4727
4728   if (arf_src_index) {
4729     assert(arf_src_index <= rc->frames_to_key);
4730
4731     if ((source = vp9_lookahead_peek(cpi->lookahead, arf_src_index)) != NULL) {
4732       cpi->alt_ref_source = source;
4733
4734 #if CONFIG_SPATIAL_SVC
4735       if (is_two_pass_svc(cpi) && cpi->svc.spatial_layer_id > 0) {
4736         int i;
4737         // Reference a hidden frame from a lower layer
4738         for (i = cpi->svc.spatial_layer_id - 1; i >= 0; --i) {
4739           if (oxcf->ss_enable_auto_arf[i]) {
4740             cpi->gld_fb_idx = cpi->svc.layer_context[i].alt_ref_idx;
4741             break;
4742           }
4743         }
4744       }
4745       cpi->svc.layer_context[cpi->svc.spatial_layer_id].has_alt_frame = 1;
4746 #endif
4747
4748       if ((oxcf->mode != REALTIME) && (oxcf->arnr_max_frames > 0) &&
4749           (oxcf->arnr_strength > 0)) {
4750         int bitrate = cpi->rc.avg_frame_bandwidth / 40;
4751         int not_low_bitrate = bitrate > ALT_REF_AQ_LOW_BITRATE_BOUNDARY;
4752
4753         int not_last_frame = (cpi->lookahead->sz - arf_src_index > 1);
4754         not_last_frame |= ALT_REF_AQ_APPLY_TO_LAST_FRAME;
4755
4756         // Produce the filtered ARF frame.
4757         vp9_temporal_filter(cpi, arf_src_index);
4758         vpx_extend_frame_borders(&cpi->alt_ref_buffer);
4759
4760         // for small bitrates segmentation overhead usually
4761         // eats all bitrate gain from enabling delta quantizers
4762         if (cpi->oxcf.alt_ref_aq != 0 && not_low_bitrate && not_last_frame)
4763           vp9_alt_ref_aq_setup_mode(cpi->alt_ref_aq, cpi);
4764
4765         force_src_buffer = &cpi->alt_ref_buffer;
4766       }
4767
4768       cm->show_frame = 0;
4769       cm->intra_only = 0;
4770       cpi->refresh_alt_ref_frame = 1;
4771       cpi->refresh_golden_frame = 0;
4772       cpi->refresh_last_frame = 0;
4773       rc->is_src_frame_alt_ref = 0;
4774       rc->source_alt_ref_pending = 0;
4775     } else {
4776       rc->source_alt_ref_pending = 0;
4777     }
4778   }
4779
4780   if (!source) {
4781     // Get last frame source.
4782     if (cm->current_video_frame > 0) {
4783       if ((last_source = vp9_lookahead_peek(cpi->lookahead, -1)) == NULL)
4784         return -1;
4785     }
4786
4787     // Read in the source frame.
4788     if (cpi->use_svc)
4789       source = vp9_svc_lookahead_pop(cpi, cpi->lookahead, flush);
4790     else
4791       source = vp9_lookahead_pop(cpi->lookahead, flush);
4792
4793     if (source != NULL) {
4794       cm->show_frame = 1;
4795       cm->intra_only = 0;
4796       // if the flags indicate intra frame, but if the current picture is for
4797       // non-zero spatial layer, it should not be an intra picture.
4798       // TODO(Won Kap): this needs to change if per-layer intra frame is
4799       // allowed.
4800       if ((source->flags & VPX_EFLAG_FORCE_KF) &&
4801           cpi->svc.spatial_layer_id > cpi->svc.first_spatial_layer_to_encode) {
4802         source->flags &= ~(unsigned int)(VPX_EFLAG_FORCE_KF);
4803       }
4804
4805       // Check to see if the frame should be encoded as an arf overlay.
4806       check_src_altref(cpi, source);
4807     }
4808   }
4809
4810   if (source) {
4811     cpi->un_scaled_source = cpi->Source =
4812         force_src_buffer ? force_src_buffer : &source->img;
4813
4814 #ifdef ENABLE_KF_DENOISE
4815     // Copy of raw source for metrics calculation.
4816     if (is_psnr_calc_enabled(cpi))
4817       vp9_copy_and_extend_frame(cpi->Source, &cpi->raw_unscaled_source);
4818 #endif
4819
4820     cpi->unscaled_last_source = last_source != NULL ? &last_source->img : NULL;
4821
4822     *time_stamp = source->ts_start;
4823     *time_end = source->ts_end;
4824     *frame_flags = (source->flags & VPX_EFLAG_FORCE_KF) ? FRAMEFLAGS_KEY : 0;
4825
4826   } else {
4827     *size = 0;
4828     if (flush && oxcf->pass == 1 && !cpi->twopass.first_pass_done) {
4829       vp9_end_first_pass(cpi); /* get last stats packet */
4830       cpi->twopass.first_pass_done = 1;
4831     }
4832     return -1;
4833   }
4834
4835   if (source->ts_start < cpi->first_time_stamp_ever) {
4836     cpi->first_time_stamp_ever = source->ts_start;
4837     cpi->last_end_time_stamp_seen = source->ts_start;
4838   }
4839
4840   // Clear down mmx registers
4841   vpx_clear_system_state();
4842
4843   // adjust frame rates based on timestamps given
4844   if (cm->show_frame) {
4845     adjust_frame_rate(cpi, source);
4846   }
4847
4848   if (is_one_pass_cbr_svc(cpi)) {
4849     vp9_update_temporal_layer_framerate(cpi);
4850     vp9_restore_layer_context(cpi);
4851   }
4852
4853   // Find a free buffer for the new frame, releasing the reference previously
4854   // held.
4855   if (cm->new_fb_idx != INVALID_IDX) {
4856     --pool->frame_bufs[cm->new_fb_idx].ref_count;
4857   }
4858   cm->new_fb_idx = get_free_fb(cm);
4859
4860   if (cm->new_fb_idx == INVALID_IDX) return -1;
4861
4862   cm->cur_frame = &pool->frame_bufs[cm->new_fb_idx];
4863
4864   if (!cpi->use_svc && cpi->multi_arf_allowed) {
4865     if (cm->frame_type == KEY_FRAME) {
4866       init_buffer_indices(cpi);
4867     } else if (oxcf->pass == 2) {
4868       const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
4869       cpi->alt_fb_idx = gf_group->arf_ref_idx[gf_group->index];
4870     }
4871   }
4872
4873   // Start with a 0 size frame.
4874   *size = 0;
4875
4876   cpi->frame_flags = *frame_flags;
4877
4878   if ((oxcf->pass == 2) &&
4879       (!cpi->use_svc || (is_two_pass_svc(cpi) &&
4880                          cpi->svc.encode_empty_frame_state != ENCODING))) {
4881     vp9_rc_get_second_pass_params(cpi);
4882   } else if (oxcf->pass == 1) {
4883     set_frame_size(cpi);
4884   }
4885
4886   if (oxcf->pass != 1 && cpi->level_constraint.level_index >= 0 &&
4887       cpi->level_constraint.fail_flag == 0)
4888     level_rc_framerate(cpi, arf_src_index);
4889
4890   if (cpi->oxcf.pass != 0 || cpi->use_svc || frame_is_intra_only(cm) == 1) {
4891     for (i = 0; i < MAX_REF_FRAMES; ++i) cpi->scaled_ref_idx[i] = INVALID_IDX;
4892   }
4893
4894   cpi->td.mb.fp_src_pred = 0;
4895   if (oxcf->pass == 1 && (!cpi->use_svc || is_two_pass_svc(cpi))) {
4896     const int lossless = is_lossless_requested(oxcf);
4897 #if CONFIG_VP9_HIGHBITDEPTH
4898     if (cpi->oxcf.use_highbitdepth)
4899       cpi->td.mb.fwd_txm4x4 =
4900           lossless ? vp9_highbd_fwht4x4 : vpx_highbd_fdct4x4;
4901     else
4902       cpi->td.mb.fwd_txm4x4 = lossless ? vp9_fwht4x4 : vpx_fdct4x4;
4903     cpi->td.mb.highbd_itxm_add =
4904         lossless ? vp9_highbd_iwht4x4_add : vp9_highbd_idct4x4_add;
4905 #else
4906     cpi->td.mb.fwd_txm4x4 = lossless ? vp9_fwht4x4 : vpx_fdct4x4;
4907 #endif  // CONFIG_VP9_HIGHBITDEPTH
4908     cpi->td.mb.itxm_add = lossless ? vp9_iwht4x4_add : vp9_idct4x4_add;
4909     vp9_first_pass(cpi, source);
4910   } else if (oxcf->pass == 2 && (!cpi->use_svc || is_two_pass_svc(cpi))) {
4911     Pass2Encode(cpi, size, dest, frame_flags);
4912   } else if (cpi->use_svc) {
4913     SvcEncode(cpi, size, dest, frame_flags);
4914   } else {
4915     // One pass encode
4916     Pass0Encode(cpi, size, dest, frame_flags);
4917   }
4918
4919   if (cm->refresh_frame_context)
4920     cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
4921
4922   // No frame encoded, or frame was dropped, release scaled references.
4923   if ((*size == 0) && (frame_is_intra_only(cm) == 0)) {
4924     release_scaled_references(cpi);
4925   }
4926
4927   if (*size > 0) {
4928     cpi->droppable = !frame_is_reference(cpi);
4929   }
4930
4931   // Save layer specific state.
4932   if (is_one_pass_cbr_svc(cpi) || ((cpi->svc.number_temporal_layers > 1 ||
4933                                     cpi->svc.number_spatial_layers > 1) &&
4934                                    oxcf->pass == 2)) {
4935     vp9_save_layer_context(cpi);
4936   }
4937
4938   vpx_usec_timer_mark(&cmptimer);
4939   cpi->time_compress_data += vpx_usec_timer_elapsed(&cmptimer);
4940
4941   // Should we calculate metrics for the frame.
4942   if (is_psnr_calc_enabled(cpi)) generate_psnr_packet(cpi);
4943
4944   if (cpi->keep_level_stats && oxcf->pass != 1)
4945     update_level_info(cpi, size, arf_src_index);
4946
4947 #if CONFIG_INTERNAL_STATS
4948
4949   if (oxcf->pass != 1) {
4950     double samples = 0.0;
4951     cpi->bytes += (int)(*size);
4952
4953     if (cm->show_frame) {
4954       uint32_t bit_depth = 8;
4955       uint32_t in_bit_depth = 8;
4956       cpi->count++;
4957 #if CONFIG_VP9_HIGHBITDEPTH
4958       if (cm->use_highbitdepth) {
4959         in_bit_depth = cpi->oxcf.input_bit_depth;
4960         bit_depth = cm->bit_depth;
4961       }
4962 #endif
4963
4964       if (cpi->b_calculate_psnr) {
4965         YV12_BUFFER_CONFIG *orig = cpi->raw_source_frame;
4966         YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
4967         YV12_BUFFER_CONFIG *pp = &cm->post_proc_buffer;
4968         PSNR_STATS psnr;
4969 #if CONFIG_VP9_HIGHBITDEPTH
4970         vpx_calc_highbd_psnr(orig, recon, &psnr, cpi->td.mb.e_mbd.bd,
4971                              in_bit_depth);
4972 #else
4973         vpx_calc_psnr(orig, recon, &psnr);
4974 #endif  // CONFIG_VP9_HIGHBITDEPTH
4975
4976         adjust_image_stat(psnr.psnr[1], psnr.psnr[2], psnr.psnr[3],
4977                           psnr.psnr[0], &cpi->psnr);
4978         cpi->total_sq_error += psnr.sse[0];
4979         cpi->total_samples += psnr.samples[0];
4980         samples = psnr.samples[0];
4981
4982         {
4983           PSNR_STATS psnr2;
4984           double frame_ssim2 = 0, weight = 0;
4985 #if CONFIG_VP9_POSTPROC
4986           if (vpx_alloc_frame_buffer(
4987                   pp, recon->y_crop_width, recon->y_crop_height,
4988                   cm->subsampling_x, cm->subsampling_y,
4989 #if CONFIG_VP9_HIGHBITDEPTH
4990                   cm->use_highbitdepth,
4991 #endif
4992                   VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment) < 0) {
4993             vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
4994                                "Failed to allocate post processing buffer");
4995           }
4996           {
4997             vp9_ppflags_t ppflags;
4998             ppflags.post_proc_flag = VP9D_DEBLOCK;
4999             ppflags.deblocking_level = 0;  // not used in vp9_post_proc_frame()
5000             ppflags.noise_level = 0;       // not used in vp9_post_proc_frame()
5001             vp9_post_proc_frame(cm, pp, &ppflags);
5002           }
5003 #endif
5004           vpx_clear_system_state();
5005
5006 #if CONFIG_VP9_HIGHBITDEPTH
5007           vpx_calc_highbd_psnr(orig, pp, &psnr2, cpi->td.mb.e_mbd.bd,
5008                                cpi->oxcf.input_bit_depth);
5009 #else
5010           vpx_calc_psnr(orig, pp, &psnr2);
5011 #endif  // CONFIG_VP9_HIGHBITDEPTH
5012
5013           cpi->totalp_sq_error += psnr2.sse[0];
5014           cpi->totalp_samples += psnr2.samples[0];
5015           adjust_image_stat(psnr2.psnr[1], psnr2.psnr[2], psnr2.psnr[3],
5016                             psnr2.psnr[0], &cpi->psnrp);
5017
5018 #if CONFIG_VP9_HIGHBITDEPTH
5019           if (cm->use_highbitdepth) {
5020             frame_ssim2 = vpx_highbd_calc_ssim(orig, recon, &weight, bit_depth,
5021                                                in_bit_depth);
5022           } else {
5023             frame_ssim2 = vpx_calc_ssim(orig, recon, &weight);
5024           }
5025 #else
5026           frame_ssim2 = vpx_calc_ssim(orig, recon, &weight);
5027 #endif  // CONFIG_VP9_HIGHBITDEPTH
5028
5029           cpi->worst_ssim = VPXMIN(cpi->worst_ssim, frame_ssim2);
5030           cpi->summed_quality += frame_ssim2 * weight;
5031           cpi->summed_weights += weight;
5032
5033 #if CONFIG_VP9_HIGHBITDEPTH
5034           if (cm->use_highbitdepth) {
5035             frame_ssim2 = vpx_highbd_calc_ssim(orig, pp, &weight, bit_depth,
5036                                                in_bit_depth);
5037           } else {
5038             frame_ssim2 = vpx_calc_ssim(orig, pp, &weight);
5039           }
5040 #else
5041           frame_ssim2 = vpx_calc_ssim(orig, pp, &weight);
5042 #endif  // CONFIG_VP9_HIGHBITDEPTH
5043
5044           cpi->summedp_quality += frame_ssim2 * weight;
5045           cpi->summedp_weights += weight;
5046 #if 0
5047           {
5048             FILE *f = fopen("q_used.stt", "a");
5049             fprintf(f, "%5d : Y%f7.3:U%f7.3:V%f7.3:F%f7.3:S%7.3f\n",
5050                     cpi->common.current_video_frame, y2, u2, v2,
5051                     frame_psnr2, frame_ssim2);
5052             fclose(f);
5053           }
5054 #endif
5055         }
5056       }
5057       if (cpi->b_calculate_blockiness) {
5058 #if CONFIG_VP9_HIGHBITDEPTH
5059         if (!cm->use_highbitdepth)
5060 #endif
5061         {
5062           double frame_blockiness = vp9_get_blockiness(
5063               cpi->Source->y_buffer, cpi->Source->y_stride,
5064               cm->frame_to_show->y_buffer, cm->frame_to_show->y_stride,
5065               cpi->Source->y_width, cpi->Source->y_height);
5066           cpi->worst_blockiness =
5067               VPXMAX(cpi->worst_blockiness, frame_blockiness);
5068           cpi->total_blockiness += frame_blockiness;
5069         }
5070       }
5071
5072       if (cpi->b_calculate_consistency) {
5073 #if CONFIG_VP9_HIGHBITDEPTH
5074         if (!cm->use_highbitdepth)
5075 #endif
5076         {
5077           double this_inconsistency = vpx_get_ssim_metrics(
5078               cpi->Source->y_buffer, cpi->Source->y_stride,
5079               cm->frame_to_show->y_buffer, cm->frame_to_show->y_stride,
5080               cpi->Source->y_width, cpi->Source->y_height, cpi->ssim_vars,
5081               &cpi->metrics, 1);
5082
5083           const double peak = (double)((1 << cpi->oxcf.input_bit_depth) - 1);
5084           double consistency =
5085               vpx_sse_to_psnr(samples, peak, (double)cpi->total_inconsistency);
5086           if (consistency > 0.0)
5087             cpi->worst_consistency =
5088                 VPXMIN(cpi->worst_consistency, consistency);
5089           cpi->total_inconsistency += this_inconsistency;
5090         }
5091       }
5092
5093       {
5094         double y, u, v, frame_all;
5095         frame_all = vpx_calc_fastssim(cpi->Source, cm->frame_to_show, &y, &u,
5096                                       &v, bit_depth, in_bit_depth);
5097         adjust_image_stat(y, u, v, frame_all, &cpi->fastssim);
5098       }
5099       {
5100         double y, u, v, frame_all;
5101         frame_all = vpx_psnrhvs(cpi->Source, cm->frame_to_show, &y, &u, &v,
5102                                 bit_depth, in_bit_depth);
5103         adjust_image_stat(y, u, v, frame_all, &cpi->psnrhvs);
5104       }
5105     }
5106   }
5107
5108 #endif
5109
5110   if (is_two_pass_svc(cpi)) {
5111     if (cpi->svc.encode_empty_frame_state == ENCODING) {
5112       cpi->svc.encode_empty_frame_state = ENCODED;
5113       cpi->svc.encode_intra_empty_frame = 0;
5114     }
5115
5116     if (cm->show_frame) {
5117       ++cpi->svc.spatial_layer_to_encode;
5118       if (cpi->svc.spatial_layer_to_encode >= cpi->svc.number_spatial_layers)
5119         cpi->svc.spatial_layer_to_encode = 0;
5120
5121       // May need the empty frame after an visible frame.
5122       cpi->svc.encode_empty_frame_state = NEED_TO_ENCODE;
5123     }
5124   } else if (is_one_pass_cbr_svc(cpi)) {
5125     if (cm->show_frame) {
5126       ++cpi->svc.spatial_layer_to_encode;
5127       if (cpi->svc.spatial_layer_to_encode >= cpi->svc.number_spatial_layers)
5128         cpi->svc.spatial_layer_to_encode = 0;
5129     }
5130   }
5131
5132   vpx_clear_system_state();
5133   return 0;
5134 }
5135
5136 int vp9_get_preview_raw_frame(VP9_COMP *cpi, YV12_BUFFER_CONFIG *dest,
5137                               vp9_ppflags_t *flags) {
5138   VP9_COMMON *cm = &cpi->common;
5139 #if !CONFIG_VP9_POSTPROC
5140   (void)flags;
5141 #endif
5142
5143   if (!cm->show_frame) {
5144     return -1;
5145   } else {
5146     int ret;
5147 #if CONFIG_VP9_POSTPROC
5148     ret = vp9_post_proc_frame(cm, dest, flags);
5149 #else
5150     if (cm->frame_to_show) {
5151       *dest = *cm->frame_to_show;
5152       dest->y_width = cm->width;
5153       dest->y_height = cm->height;
5154       dest->uv_width = cm->width >> cm->subsampling_x;
5155       dest->uv_height = cm->height >> cm->subsampling_y;
5156       ret = 0;
5157     } else {
5158       ret = -1;
5159     }
5160 #endif  // !CONFIG_VP9_POSTPROC
5161     vpx_clear_system_state();
5162     return ret;
5163   }
5164 }
5165
5166 int vp9_set_internal_size(VP9_COMP *cpi, VPX_SCALING horiz_mode,
5167                           VPX_SCALING vert_mode) {
5168   VP9_COMMON *cm = &cpi->common;
5169   int hr = 0, hs = 0, vr = 0, vs = 0;
5170
5171   if (horiz_mode > ONETWO || vert_mode > ONETWO) return -1;
5172
5173   Scale2Ratio(horiz_mode, &hr, &hs);
5174   Scale2Ratio(vert_mode, &vr, &vs);
5175
5176   // always go to the next whole number
5177   cm->width = (hs - 1 + cpi->oxcf.width * hr) / hs;
5178   cm->height = (vs - 1 + cpi->oxcf.height * vr) / vs;
5179   if (cm->current_video_frame) {
5180     assert(cm->width <= cpi->initial_width);
5181     assert(cm->height <= cpi->initial_height);
5182   }
5183
5184   update_frame_size(cpi);
5185
5186   return 0;
5187 }
5188
5189 int vp9_set_size_literal(VP9_COMP *cpi, unsigned int width,
5190                          unsigned int height) {
5191   VP9_COMMON *cm = &cpi->common;
5192 #if CONFIG_VP9_HIGHBITDEPTH
5193   check_initial_width(cpi, cm->use_highbitdepth, 1, 1);
5194 #else
5195   check_initial_width(cpi, 1, 1);
5196 #endif  // CONFIG_VP9_HIGHBITDEPTH
5197
5198 #if CONFIG_VP9_TEMPORAL_DENOISING
5199   setup_denoiser_buffer(cpi);
5200 #endif
5201
5202   if (width) {
5203     cm->width = width;
5204     if (cm->width > cpi->initial_width) {
5205       cm->width = cpi->initial_width;
5206       printf("Warning: Desired width too large, changed to %d\n", cm->width);
5207     }
5208   }
5209
5210   if (height) {
5211     cm->height = height;
5212     if (cm->height > cpi->initial_height) {
5213       cm->height = cpi->initial_height;
5214       printf("Warning: Desired height too large, changed to %d\n", cm->height);
5215     }
5216   }
5217   assert(cm->width <= cpi->initial_width);
5218   assert(cm->height <= cpi->initial_height);
5219
5220   update_frame_size(cpi);
5221
5222   return 0;
5223 }
5224
5225 void vp9_set_svc(VP9_COMP *cpi, int use_svc) {
5226   cpi->use_svc = use_svc;
5227   return;
5228 }
5229
5230 int vp9_get_quantizer(VP9_COMP *cpi) { return cpi->common.base_qindex; }
5231
5232 void vp9_apply_encoding_flags(VP9_COMP *cpi, vpx_enc_frame_flags_t flags) {
5233   if (flags &
5234       (VP8_EFLAG_NO_REF_LAST | VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF)) {
5235     int ref = 7;
5236
5237     if (flags & VP8_EFLAG_NO_REF_LAST) ref ^= VP9_LAST_FLAG;
5238
5239     if (flags & VP8_EFLAG_NO_REF_GF) ref ^= VP9_GOLD_FLAG;
5240
5241     if (flags & VP8_EFLAG_NO_REF_ARF) ref ^= VP9_ALT_FLAG;
5242
5243     vp9_use_as_reference(cpi, ref);
5244   }
5245
5246   if (flags &
5247       (VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF |
5248        VP8_EFLAG_FORCE_GF | VP8_EFLAG_FORCE_ARF)) {
5249     int upd = 7;
5250
5251     if (flags & VP8_EFLAG_NO_UPD_LAST) upd ^= VP9_LAST_FLAG;
5252
5253     if (flags & VP8_EFLAG_NO_UPD_GF) upd ^= VP9_GOLD_FLAG;
5254
5255     if (flags & VP8_EFLAG_NO_UPD_ARF) upd ^= VP9_ALT_FLAG;
5256
5257     vp9_update_reference(cpi, upd);
5258   }
5259
5260   if (flags & VP8_EFLAG_NO_UPD_ENTROPY) {
5261     vp9_update_entropy(cpi, 0);
5262   }
5263 }
5264
5265 void vp9_set_row_mt(VP9_COMP *cpi) {
5266   // Enable row based multi-threading for supported modes of encoding
5267   cpi->row_mt = 0;
5268   if (((cpi->oxcf.mode == GOOD || cpi->oxcf.mode == BEST) &&
5269        cpi->oxcf.speed < 5 && cpi->oxcf.pass == 1) &&
5270       cpi->oxcf.row_mt && !cpi->use_svc)
5271     cpi->row_mt = 1;
5272
5273   if (cpi->oxcf.mode == GOOD && cpi->oxcf.speed < 5 &&
5274       (cpi->oxcf.pass == 0 || cpi->oxcf.pass == 2) && cpi->oxcf.row_mt &&
5275       !cpi->use_svc)
5276     cpi->row_mt = 1;
5277
5278   // In realtime mode, enable row based multi-threading for all the speed levels
5279   // where non-rd path is used.
5280   if (cpi->oxcf.mode == REALTIME && cpi->oxcf.speed >= 5 && cpi->oxcf.row_mt) {
5281     cpi->row_mt = 1;
5282   }
5283 }