]> granicus.if.org Git - libvpx/blob - vp9/encoder/vp9_encoder.c
subpel variance neon: add mixed sizes
[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 tentative).
125   { LEVEL_5_2, 1176502272,  8912896,  180000, 90000,  8, 8,  10, 4 },
126   { LEVEL_6,   1176502272,  35651584, 180000, 90000,  8, 16, 10, 4 },
127   { LEVEL_6_1, 2353004544u, 35651584, 240000, 180000, 8, 16, 10, 4 },
128   { LEVEL_6_2, 4706009088u, 35651584, 480000, 360000, 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(CONVERT_TO_SHORTPTR(src_ptr), src_stride,
2422                                CONVERT_TO_SHORTPTR(dst_ptr), dst_stride,
2423                                kernel[x_q4 & 0xf], 16 * src_w / dst_w,
2424                                kernel[y_q4 & 0xf], 16 * src_h / dst_h,
2425                                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 CBR SVC, only ZEROMV is allowed for spatial reference frame
3233   // when svc->force_zero_mode_spatial_ref = 1. Under those conditions we can
3234   // avoid this frame-level upsampling (for non intra_only frames).
3235   if (frame_is_intra_only(cm) == 0 &&
3236       !(is_one_pass_cbr_svc(cpi) && cpi->svc.force_zero_mode_spatial_ref)) {
3237     vp9_scale_references(cpi);
3238   }
3239
3240   set_size_independent_vars(cpi);
3241   set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
3242
3243   if (cpi->sf.copy_partition_flag) alloc_copy_partition_data(cpi);
3244
3245   if (cpi->oxcf.speed >= 5 && cpi->oxcf.pass == 0 &&
3246       cpi->oxcf.rc_mode == VPX_CBR &&
3247       cpi->oxcf.content != VP9E_CONTENT_SCREEN &&
3248       cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) {
3249     cpi->use_skin_detection = 1;
3250   }
3251
3252   vp9_set_quantizer(cm, q);
3253   vp9_set_variance_partition_thresholds(cpi, q, 0);
3254
3255   setup_frame(cpi);
3256
3257   suppress_active_map(cpi);
3258
3259   // Variance adaptive and in frame q adjustment experiments are mutually
3260   // exclusive.
3261   if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
3262     vp9_vaq_frame_setup(cpi);
3263   } else if (cpi->oxcf.aq_mode == EQUATOR360_AQ) {
3264     vp9_360aq_frame_setup(cpi);
3265   } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
3266     vp9_setup_in_frame_q_adj(cpi);
3267   } else if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) {
3268     vp9_cyclic_refresh_setup(cpi);
3269   } else if (cpi->oxcf.aq_mode == LOOKAHEAD_AQ) {
3270     // it may be pretty bad for rate-control,
3271     // and I should handle it somehow
3272     vp9_alt_ref_aq_setup_map(cpi->alt_ref_aq, cpi);
3273   }
3274
3275   apply_active_map(cpi);
3276
3277   vp9_encode_frame(cpi);
3278
3279   // Check if we should drop this frame because of high overshoot.
3280   // Only for frames where high temporal-source SAD is detected.
3281   if (cpi->oxcf.pass == 0 && cpi->oxcf.rc_mode == VPX_CBR &&
3282       cpi->resize_state == ORIG && cm->frame_type != KEY_FRAME &&
3283       cpi->oxcf.content == VP9E_CONTENT_SCREEN &&
3284       cpi->rc.high_source_sad == 1) {
3285     int frame_size = 0;
3286     // Get an estimate of the encoded frame size.
3287     save_coding_context(cpi);
3288     vp9_pack_bitstream(cpi, dest, size);
3289     restore_coding_context(cpi);
3290     frame_size = (int)(*size) << 3;
3291     // Check if encoded frame will overshoot too much, and if so, set the q and
3292     // adjust some rate control parameters, and return to re-encode the frame.
3293     if (vp9_encodedframe_overshoot(cpi, frame_size, &q)) {
3294       vpx_clear_system_state();
3295       vp9_set_quantizer(cm, q);
3296       vp9_set_variance_partition_thresholds(cpi, q, 0);
3297       suppress_active_map(cpi);
3298       // Turn-off cyclic refresh for re-encoded frame.
3299       if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) {
3300         unsigned char *const seg_map = cpi->segmentation_map;
3301         memset(seg_map, 0, cm->mi_rows * cm->mi_cols);
3302         vp9_disable_segmentation(&cm->seg);
3303       }
3304       apply_active_map(cpi);
3305       vp9_encode_frame(cpi);
3306     }
3307   }
3308
3309   // Update some stats from cyclic refresh, and check for golden frame update.
3310   if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->seg.enabled &&
3311       cm->frame_type != KEY_FRAME)
3312     vp9_cyclic_refresh_postencode(cpi);
3313
3314   // Update the skip mb flag probabilities based on the distribution
3315   // seen in the last encoder iteration.
3316   // update_base_skip_probs(cpi);
3317   vpx_clear_system_state();
3318 }
3319
3320 #define MAX_QSTEP_ADJ 4
3321 static int get_qstep_adj(int rate_excess, int rate_limit) {
3322   int qstep =
3323       rate_limit ? ((rate_excess + rate_limit / 2) / rate_limit) : INT_MAX;
3324   return VPXMIN(qstep, MAX_QSTEP_ADJ);
3325 }
3326
3327 static void encode_with_recode_loop(VP9_COMP *cpi, size_t *size,
3328                                     uint8_t *dest) {
3329   VP9_COMMON *const cm = &cpi->common;
3330   RATE_CONTROL *const rc = &cpi->rc;
3331   int bottom_index, top_index;
3332   int loop_count = 0;
3333   int loop_at_this_size = 0;
3334   int loop = 0;
3335   int overshoot_seen = 0;
3336   int undershoot_seen = 0;
3337   int frame_over_shoot_limit;
3338   int frame_under_shoot_limit;
3339   int q = 0, q_low = 0, q_high = 0;
3340   int enable_acl;
3341 #ifdef AGGRESSIVE_VBR
3342   int qrange_adj = 1;
3343 #endif
3344
3345   set_size_independent_vars(cpi);
3346
3347   enable_acl = cpi->sf.allow_acl
3348                    ? (cm->frame_type == KEY_FRAME) || (cm->show_frame == 0)
3349                    : 0;
3350
3351   do {
3352     vpx_clear_system_state();
3353
3354     set_frame_size(cpi);
3355
3356     if (loop_count == 0 || cpi->resize_pending != 0) {
3357       set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
3358
3359 #ifdef AGGRESSIVE_VBR
3360       if (two_pass_first_group_inter(cpi)) {
3361         // Adjustment limits for min and max q
3362         qrange_adj = VPXMAX(1, (top_index - bottom_index) / 2);
3363
3364         bottom_index =
3365             VPXMAX(bottom_index - qrange_adj / 2, cpi->oxcf.best_allowed_q);
3366         top_index =
3367             VPXMIN(cpi->oxcf.worst_allowed_q, top_index + qrange_adj / 2);
3368       }
3369 #endif
3370       // TODO(agrange) Scale cpi->max_mv_magnitude if frame-size has changed.
3371       set_mv_search_params(cpi);
3372
3373       // Reset the loop state for new frame size.
3374       overshoot_seen = 0;
3375       undershoot_seen = 0;
3376
3377       // Reconfiguration for change in frame size has concluded.
3378       cpi->resize_pending = 0;
3379
3380       q_low = bottom_index;
3381       q_high = top_index;
3382
3383       loop_at_this_size = 0;
3384     }
3385
3386     // Decide frame size bounds first time through.
3387     if (loop_count == 0) {
3388       vp9_rc_compute_frame_size_bounds(cpi, rc->this_frame_target,
3389                                        &frame_under_shoot_limit,
3390                                        &frame_over_shoot_limit);
3391     }
3392
3393     cpi->Source =
3394         vp9_scale_if_required(cm, cpi->un_scaled_source, &cpi->scaled_source,
3395                               (cpi->oxcf.pass == 0), 0);
3396
3397     // Unfiltered raw source used in metrics calculation if the source
3398     // has been filtered.
3399     if (is_psnr_calc_enabled(cpi)) {
3400 #ifdef ENABLE_KF_DENOISE
3401       if (is_spatial_denoise_enabled(cpi)) {
3402         cpi->raw_source_frame = vp9_scale_if_required(
3403             cm, &cpi->raw_unscaled_source, &cpi->raw_scaled_source,
3404             (cpi->oxcf.pass == 0), 0);
3405       } else {
3406         cpi->raw_source_frame = cpi->Source;
3407       }
3408 #else
3409       cpi->raw_source_frame = cpi->Source;
3410 #endif
3411     }
3412
3413     if (cpi->unscaled_last_source != NULL)
3414       cpi->Last_Source = vp9_scale_if_required(cm, cpi->unscaled_last_source,
3415                                                &cpi->scaled_last_source,
3416                                                (cpi->oxcf.pass == 0), 0);
3417
3418     if (frame_is_intra_only(cm) == 0) {
3419       if (loop_count > 0) {
3420         release_scaled_references(cpi);
3421       }
3422       vp9_scale_references(cpi);
3423     }
3424
3425     vp9_set_quantizer(cm, q);
3426
3427     if (loop_count == 0) setup_frame(cpi);
3428
3429     // Variance adaptive and in frame q adjustment experiments are mutually
3430     // exclusive.
3431     if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
3432       vp9_vaq_frame_setup(cpi);
3433     } else if (cpi->oxcf.aq_mode == EQUATOR360_AQ) {
3434       vp9_360aq_frame_setup(cpi);
3435     } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
3436       vp9_setup_in_frame_q_adj(cpi);
3437     } else if (cpi->oxcf.aq_mode == LOOKAHEAD_AQ) {
3438       vp9_alt_ref_aq_setup_map(cpi->alt_ref_aq, cpi);
3439     }
3440
3441     vp9_encode_frame(cpi);
3442
3443     // Update the skip mb flag probabilities based on the distribution
3444     // seen in the last encoder iteration.
3445     // update_base_skip_probs(cpi);
3446
3447     vpx_clear_system_state();
3448
3449     // Dummy pack of the bitstream using up to date stats to get an
3450     // accurate estimate of output frame size to determine if we need
3451     // to recode.
3452     if (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF) {
3453       save_coding_context(cpi);
3454       if (!cpi->sf.use_nonrd_pick_mode) vp9_pack_bitstream(cpi, dest, size);
3455
3456       rc->projected_frame_size = (int)(*size) << 3;
3457
3458       if (frame_over_shoot_limit == 0) frame_over_shoot_limit = 1;
3459     }
3460
3461     if (cpi->oxcf.rc_mode == VPX_Q) {
3462       loop = 0;
3463     } else {
3464       if ((cm->frame_type == KEY_FRAME) && rc->this_key_frame_forced &&
3465           (rc->projected_frame_size < rc->max_frame_bandwidth)) {
3466         int last_q = q;
3467         int64_t kf_err;
3468
3469         int64_t high_err_target = cpi->ambient_err;
3470         int64_t low_err_target = cpi->ambient_err >> 1;
3471
3472 #if CONFIG_VP9_HIGHBITDEPTH
3473         if (cm->use_highbitdepth) {
3474           kf_err = vpx_highbd_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
3475         } else {
3476           kf_err = vpx_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
3477         }
3478 #else
3479         kf_err = vpx_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
3480 #endif  // CONFIG_VP9_HIGHBITDEPTH
3481
3482         // Prevent possible divide by zero error below for perfect KF
3483         kf_err += !kf_err;
3484
3485         // The key frame is not good enough or we can afford
3486         // to make it better without undue risk of popping.
3487         if ((kf_err > high_err_target &&
3488              rc->projected_frame_size <= frame_over_shoot_limit) ||
3489             (kf_err > low_err_target &&
3490              rc->projected_frame_size <= frame_under_shoot_limit)) {
3491           // Lower q_high
3492           q_high = q > q_low ? q - 1 : q_low;
3493
3494           // Adjust Q
3495           q = (int)((q * high_err_target) / kf_err);
3496           q = VPXMIN(q, (q_high + q_low) >> 1);
3497         } else if (kf_err < low_err_target &&
3498                    rc->projected_frame_size >= frame_under_shoot_limit) {
3499           // The key frame is much better than the previous frame
3500           // Raise q_low
3501           q_low = q < q_high ? q + 1 : q_high;
3502
3503           // Adjust Q
3504           q = (int)((q * low_err_target) / kf_err);
3505           q = VPXMIN(q, (q_high + q_low + 1) >> 1);
3506         }
3507
3508         // Clamp Q to upper and lower limits:
3509         q = clamp(q, q_low, q_high);
3510
3511         loop = q != last_q;
3512       } else if (recode_loop_test(cpi, frame_over_shoot_limit,
3513                                   frame_under_shoot_limit, q,
3514                                   VPXMAX(q_high, top_index), bottom_index)) {
3515         // Is the projected frame size out of range and are we allowed
3516         // to attempt to recode.
3517         int last_q = q;
3518         int retries = 0;
3519         int qstep;
3520
3521         if (cpi->resize_pending == 1) {
3522           // Change in frame size so go back around the recode loop.
3523           cpi->rc.frame_size_selector =
3524               SCALE_STEP1 - cpi->rc.frame_size_selector;
3525           cpi->rc.next_frame_size_selector = cpi->rc.frame_size_selector;
3526
3527 #if CONFIG_INTERNAL_STATS
3528           ++cpi->tot_recode_hits;
3529 #endif
3530           ++loop_count;
3531           loop = 1;
3532           continue;
3533         }
3534
3535         // Frame size out of permitted range:
3536         // Update correction factor & compute new Q to try...
3537
3538         // Frame is too large
3539         if (rc->projected_frame_size > rc->this_frame_target) {
3540           // Special case if the projected size is > the max allowed.
3541           if (rc->projected_frame_size >= rc->max_frame_bandwidth) {
3542             double q_val_high;
3543             q_val_high = vp9_convert_qindex_to_q(q_high, cm->bit_depth);
3544             q_val_high = q_val_high * ((double)rc->projected_frame_size /
3545                                        rc->max_frame_bandwidth);
3546             q_high = vp9_convert_q_to_qindex(q_val_high, cm->bit_depth);
3547             q_high = clamp(q_high, rc->best_quality, rc->worst_quality);
3548           }
3549
3550           // Raise Qlow as to at least the current value
3551           qstep =
3552               get_qstep_adj(rc->projected_frame_size, rc->this_frame_target);
3553           q_low = VPXMIN(q + qstep, q_high);
3554           // q_low = q < q_high ? q + 1 : q_high;
3555
3556           if (undershoot_seen || loop_at_this_size > 1) {
3557             // Update rate_correction_factor unless
3558             vp9_rc_update_rate_correction_factors(cpi);
3559
3560             q = (q_high + q_low + 1) / 2;
3561           } else {
3562             // Update rate_correction_factor unless
3563             vp9_rc_update_rate_correction_factors(cpi);
3564
3565             q = vp9_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
3566                                   VPXMAX(q_high, top_index));
3567
3568             while (q < q_low && retries < 10) {
3569               vp9_rc_update_rate_correction_factors(cpi);
3570               q = vp9_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
3571                                     VPXMAX(q_high, top_index));
3572               retries++;
3573             }
3574           }
3575
3576           overshoot_seen = 1;
3577         } else {
3578           // Frame is too small
3579           qstep =
3580               get_qstep_adj(rc->this_frame_target, rc->projected_frame_size);
3581           q_high = VPXMAX(q - qstep, q_low);
3582           // q_high = q > q_low ? q - 1 : q_low;
3583
3584           if (overshoot_seen || loop_at_this_size > 1) {
3585             vp9_rc_update_rate_correction_factors(cpi);
3586             q = (q_high + q_low) / 2;
3587           } else {
3588             vp9_rc_update_rate_correction_factors(cpi);
3589             q = vp9_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
3590                                   top_index);
3591             // Special case reset for qlow for constrained quality.
3592             // This should only trigger where there is very substantial
3593             // undershoot on a frame and the auto cq level is above
3594             // the user passsed in value.
3595             if (cpi->oxcf.rc_mode == VPX_CQ && q < q_low) {
3596               q_low = q;
3597             }
3598
3599             while (q > q_high && retries < 10) {
3600               vp9_rc_update_rate_correction_factors(cpi);
3601               q = vp9_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
3602                                     top_index);
3603               retries++;
3604             }
3605           }
3606
3607           undershoot_seen = 1;
3608         }
3609
3610         // Clamp Q to upper and lower limits:
3611         q = clamp(q, q_low, q_high);
3612
3613         loop = (q != last_q);
3614       } else {
3615         loop = 0;
3616       }
3617     }
3618
3619     // Special case for overlay frame.
3620     if (rc->is_src_frame_alt_ref &&
3621         rc->projected_frame_size < rc->max_frame_bandwidth)
3622       loop = 0;
3623
3624     if (loop) {
3625       ++loop_count;
3626       ++loop_at_this_size;
3627
3628 #if CONFIG_INTERNAL_STATS
3629       ++cpi->tot_recode_hits;
3630 #endif
3631     }
3632
3633     if (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF)
3634       if (loop || !enable_acl) restore_coding_context(cpi);
3635   } while (loop);
3636
3637 #ifdef AGGRESSIVE_VBR
3638   if (two_pass_first_group_inter(cpi)) {
3639     cpi->twopass.active_worst_quality =
3640         VPXMIN(q + qrange_adj, cpi->oxcf.worst_allowed_q);
3641   }
3642 #endif
3643
3644   if (enable_acl) {
3645     vp9_encode_frame(cpi);
3646     vpx_clear_system_state();
3647     restore_coding_context(cpi);
3648     vp9_pack_bitstream(cpi, dest, size);
3649
3650     vp9_encode_frame(cpi);
3651     vpx_clear_system_state();
3652
3653     restore_coding_context(cpi);
3654   }
3655 }
3656
3657 static int get_ref_frame_flags(const VP9_COMP *cpi) {
3658   const int *const map = cpi->common.ref_frame_map;
3659   const int gold_is_last = map[cpi->gld_fb_idx] == map[cpi->lst_fb_idx];
3660   const int alt_is_last = map[cpi->alt_fb_idx] == map[cpi->lst_fb_idx];
3661   const int gold_is_alt = map[cpi->gld_fb_idx] == map[cpi->alt_fb_idx];
3662   int flags = VP9_ALT_FLAG | VP9_GOLD_FLAG | VP9_LAST_FLAG;
3663
3664   if (gold_is_last) flags &= ~VP9_GOLD_FLAG;
3665
3666   if (cpi->rc.frames_till_gf_update_due == INT_MAX &&
3667       (cpi->svc.number_temporal_layers == 1 &&
3668        cpi->svc.number_spatial_layers == 1))
3669     flags &= ~VP9_GOLD_FLAG;
3670
3671   if (alt_is_last) flags &= ~VP9_ALT_FLAG;
3672
3673   if (gold_is_alt) flags &= ~VP9_ALT_FLAG;
3674
3675   return flags;
3676 }
3677
3678 static void set_ext_overrides(VP9_COMP *cpi) {
3679   // Overrides the defaults with the externally supplied values with
3680   // vp9_update_reference() and vp9_update_entropy() calls
3681   // Note: The overrides are valid only for the next frame passed
3682   // to encode_frame_to_data_rate() function
3683   if (cpi->ext_refresh_frame_context_pending) {
3684     cpi->common.refresh_frame_context = cpi->ext_refresh_frame_context;
3685     cpi->ext_refresh_frame_context_pending = 0;
3686   }
3687   if (cpi->ext_refresh_frame_flags_pending) {
3688     cpi->refresh_last_frame = cpi->ext_refresh_last_frame;
3689     cpi->refresh_golden_frame = cpi->ext_refresh_golden_frame;
3690     cpi->refresh_alt_ref_frame = cpi->ext_refresh_alt_ref_frame;
3691   }
3692 }
3693
3694 YV12_BUFFER_CONFIG *vp9_svc_twostage_scale(
3695     VP9_COMMON *cm, YV12_BUFFER_CONFIG *unscaled, YV12_BUFFER_CONFIG *scaled,
3696     YV12_BUFFER_CONFIG *scaled_temp, int phase_scaler, int phase_scaler2) {
3697   if (cm->mi_cols * MI_SIZE != unscaled->y_width ||
3698       cm->mi_rows * MI_SIZE != unscaled->y_height) {
3699 #if CONFIG_VP9_HIGHBITDEPTH
3700     if (cm->bit_depth == VPX_BITS_8) {
3701       vp9_scale_and_extend_frame(unscaled, scaled_temp, phase_scaler2);
3702       vp9_scale_and_extend_frame(scaled_temp, scaled, phase_scaler);
3703     } else {
3704       scale_and_extend_frame(unscaled, scaled_temp, (int)cm->bit_depth,
3705                              phase_scaler2);
3706       scale_and_extend_frame(scaled_temp, scaled, (int)cm->bit_depth,
3707                              phase_scaler);
3708     }
3709 #else
3710     vp9_scale_and_extend_frame(unscaled, scaled_temp, phase_scaler2);
3711     vp9_scale_and_extend_frame(scaled_temp, scaled, phase_scaler);
3712 #endif  // CONFIG_VP9_HIGHBITDEPTH
3713     return scaled;
3714   } else {
3715     return unscaled;
3716   }
3717 }
3718
3719 YV12_BUFFER_CONFIG *vp9_scale_if_required(VP9_COMMON *cm,
3720                                           YV12_BUFFER_CONFIG *unscaled,
3721                                           YV12_BUFFER_CONFIG *scaled,
3722                                           int use_normative_scaler,
3723                                           int phase_scaler) {
3724   if (cm->mi_cols * MI_SIZE != unscaled->y_width ||
3725       cm->mi_rows * MI_SIZE != unscaled->y_height) {
3726 #if CONFIG_VP9_HIGHBITDEPTH
3727     if (use_normative_scaler && unscaled->y_width <= (scaled->y_width << 1) &&
3728         unscaled->y_height <= (scaled->y_height << 1))
3729       if (cm->bit_depth == VPX_BITS_8)
3730         vp9_scale_and_extend_frame(unscaled, scaled, phase_scaler);
3731       else
3732         scale_and_extend_frame(unscaled, scaled, (int)cm->bit_depth,
3733                                phase_scaler);
3734     else
3735       scale_and_extend_frame_nonnormative(unscaled, scaled, (int)cm->bit_depth);
3736 #else
3737     if (use_normative_scaler && unscaled->y_width <= (scaled->y_width << 1) &&
3738         unscaled->y_height <= (scaled->y_height << 1))
3739       vp9_scale_and_extend_frame(unscaled, scaled, phase_scaler);
3740     else
3741       scale_and_extend_frame_nonnormative(unscaled, scaled);
3742 #endif  // CONFIG_VP9_HIGHBITDEPTH
3743     return scaled;
3744   } else {
3745     return unscaled;
3746   }
3747 }
3748
3749 static void set_arf_sign_bias(VP9_COMP *cpi) {
3750   VP9_COMMON *const cm = &cpi->common;
3751   int arf_sign_bias;
3752
3753   if ((cpi->oxcf.pass == 2) && cpi->multi_arf_allowed) {
3754     const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
3755     arf_sign_bias = cpi->rc.source_alt_ref_active &&
3756                     (!cpi->refresh_alt_ref_frame ||
3757                      (gf_group->rf_level[gf_group->index] == GF_ARF_LOW));
3758   } else {
3759     arf_sign_bias =
3760         (cpi->rc.source_alt_ref_active && !cpi->refresh_alt_ref_frame);
3761   }
3762   cm->ref_frame_sign_bias[ALTREF_FRAME] = arf_sign_bias;
3763 }
3764
3765 static int setup_interp_filter_search_mask(VP9_COMP *cpi) {
3766   INTERP_FILTER ifilter;
3767   int ref_total[MAX_REF_FRAMES] = { 0 };
3768   MV_REFERENCE_FRAME ref;
3769   int mask = 0;
3770   if (cpi->common.last_frame_type == KEY_FRAME || cpi->refresh_alt_ref_frame)
3771     return mask;
3772   for (ref = LAST_FRAME; ref <= ALTREF_FRAME; ++ref)
3773     for (ifilter = EIGHTTAP; ifilter <= EIGHTTAP_SHARP; ++ifilter)
3774       ref_total[ref] += cpi->interp_filter_selected[ref][ifilter];
3775
3776   for (ifilter = EIGHTTAP; ifilter <= EIGHTTAP_SHARP; ++ifilter) {
3777     if ((ref_total[LAST_FRAME] &&
3778          cpi->interp_filter_selected[LAST_FRAME][ifilter] == 0) &&
3779         (ref_total[GOLDEN_FRAME] == 0 ||
3780          cpi->interp_filter_selected[GOLDEN_FRAME][ifilter] * 50 <
3781              ref_total[GOLDEN_FRAME]) &&
3782         (ref_total[ALTREF_FRAME] == 0 ||
3783          cpi->interp_filter_selected[ALTREF_FRAME][ifilter] * 50 <
3784              ref_total[ALTREF_FRAME]))
3785       mask |= 1 << ifilter;
3786   }
3787   return mask;
3788 }
3789
3790 #ifdef ENABLE_KF_DENOISE
3791 // Baseline Kernal weights for denoise
3792 static uint8_t dn_kernal_3[9] = { 1, 2, 1, 2, 4, 2, 1, 2, 1 };
3793 static uint8_t dn_kernal_5[25] = { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 2, 4,
3794                                    2, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1 };
3795
3796 static INLINE void add_denoise_point(int centre_val, int data_val, int thresh,
3797                                      uint8_t point_weight, int *sum_val,
3798                                      int *sum_weight) {
3799   if (abs(centre_val - data_val) <= thresh) {
3800     *sum_weight += point_weight;
3801     *sum_val += (int)data_val * (int)point_weight;
3802   }
3803 }
3804
3805 static void spatial_denoise_point(uint8_t *src_ptr, const int stride,
3806                                   const int strength) {
3807   int sum_weight = 0;
3808   int sum_val = 0;
3809   int thresh = strength;
3810   int kernal_size = 5;
3811   int half_k_size = 2;
3812   int i, j;
3813   int max_diff = 0;
3814   uint8_t *tmp_ptr;
3815   uint8_t *kernal_ptr;
3816
3817   // Find the maximum deviation from the source point in the locale.
3818   tmp_ptr = src_ptr - (stride * (half_k_size + 1)) - (half_k_size + 1);
3819   for (i = 0; i < kernal_size + 2; ++i) {
3820     for (j = 0; j < kernal_size + 2; ++j) {
3821       max_diff = VPXMAX(max_diff, abs((int)*src_ptr - (int)tmp_ptr[j]));
3822     }
3823     tmp_ptr += stride;
3824   }
3825
3826   // Select the kernal size.
3827   if (max_diff > (strength + (strength >> 1))) {
3828     kernal_size = 3;
3829     half_k_size = 1;
3830     thresh = thresh >> 1;
3831   }
3832   kernal_ptr = (kernal_size == 3) ? dn_kernal_3 : dn_kernal_5;
3833
3834   // Apply the kernal
3835   tmp_ptr = src_ptr - (stride * half_k_size) - half_k_size;
3836   for (i = 0; i < kernal_size; ++i) {
3837     for (j = 0; j < kernal_size; ++j) {
3838       add_denoise_point((int)*src_ptr, (int)tmp_ptr[j], thresh, *kernal_ptr,
3839                         &sum_val, &sum_weight);
3840       ++kernal_ptr;
3841     }
3842     tmp_ptr += stride;
3843   }
3844
3845   // Update the source value with the new filtered value
3846   *src_ptr = (uint8_t)((sum_val + (sum_weight >> 1)) / sum_weight);
3847 }
3848
3849 #if CONFIG_VP9_HIGHBITDEPTH
3850 static void highbd_spatial_denoise_point(uint16_t *src_ptr, const int stride,
3851                                          const int strength) {
3852   int sum_weight = 0;
3853   int sum_val = 0;
3854   int thresh = strength;
3855   int kernal_size = 5;
3856   int half_k_size = 2;
3857   int i, j;
3858   int max_diff = 0;
3859   uint16_t *tmp_ptr;
3860   uint8_t *kernal_ptr;
3861
3862   // Find the maximum deviation from the source point in the locale.
3863   tmp_ptr = src_ptr - (stride * (half_k_size + 1)) - (half_k_size + 1);
3864   for (i = 0; i < kernal_size + 2; ++i) {
3865     for (j = 0; j < kernal_size + 2; ++j) {
3866       max_diff = VPXMAX(max_diff, abs((int)src_ptr - (int)tmp_ptr[j]));
3867     }
3868     tmp_ptr += stride;
3869   }
3870
3871   // Select the kernal size.
3872   if (max_diff > (strength + (strength >> 1))) {
3873     kernal_size = 3;
3874     half_k_size = 1;
3875     thresh = thresh >> 1;
3876   }
3877   kernal_ptr = (kernal_size == 3) ? dn_kernal_3 : dn_kernal_5;
3878
3879   // Apply the kernal
3880   tmp_ptr = src_ptr - (stride * half_k_size) - half_k_size;
3881   for (i = 0; i < kernal_size; ++i) {
3882     for (j = 0; j < kernal_size; ++j) {
3883       add_denoise_point((int)*src_ptr, (int)tmp_ptr[j], thresh, *kernal_ptr,
3884                         &sum_val, &sum_weight);
3885       ++kernal_ptr;
3886     }
3887     tmp_ptr += stride;
3888   }
3889
3890   // Update the source value with the new filtered value
3891   *src_ptr = (uint16_t)((sum_val + (sum_weight >> 1)) / sum_weight);
3892 }
3893 #endif  // CONFIG_VP9_HIGHBITDEPTH
3894
3895 // Apply thresholded spatial noise supression to a given buffer.
3896 static void spatial_denoise_buffer(VP9_COMP *cpi, uint8_t *buffer,
3897                                    const int stride, const int width,
3898                                    const int height, const int strength) {
3899   VP9_COMMON *const cm = &cpi->common;
3900   uint8_t *src_ptr = buffer;
3901   int row;
3902   int col;
3903
3904   for (row = 0; row < height; ++row) {
3905     for (col = 0; col < width; ++col) {
3906 #if CONFIG_VP9_HIGHBITDEPTH
3907       if (cm->use_highbitdepth)
3908         highbd_spatial_denoise_point(CONVERT_TO_SHORTPTR(&src_ptr[col]), stride,
3909                                      strength);
3910       else
3911         spatial_denoise_point(&src_ptr[col], stride, strength);
3912 #else
3913       spatial_denoise_point(&src_ptr[col], stride, strength);
3914 #endif  // CONFIG_VP9_HIGHBITDEPTH
3915     }
3916     src_ptr += stride;
3917   }
3918 }
3919
3920 // Apply thresholded spatial noise supression to source.
3921 static void spatial_denoise_frame(VP9_COMP *cpi) {
3922   YV12_BUFFER_CONFIG *src = cpi->Source;
3923   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
3924   TWO_PASS *const twopass = &cpi->twopass;
3925   VP9_COMMON *const cm = &cpi->common;
3926
3927   // Base the filter strength on the current active max Q.
3928   const int q = (int)(vp9_convert_qindex_to_q(twopass->active_worst_quality,
3929                                               cm->bit_depth));
3930   int strength =
3931       VPXMAX(oxcf->arnr_strength >> 2, VPXMIN(oxcf->arnr_strength, (q >> 4)));
3932
3933   // Denoise each of Y,U and V buffers.
3934   spatial_denoise_buffer(cpi, src->y_buffer, src->y_stride, src->y_width,
3935                          src->y_height, strength);
3936
3937   strength += (strength >> 1);
3938   spatial_denoise_buffer(cpi, src->u_buffer, src->uv_stride, src->uv_width,
3939                          src->uv_height, strength << 1);
3940
3941   spatial_denoise_buffer(cpi, src->v_buffer, src->uv_stride, src->uv_width,
3942                          src->uv_height, strength << 1);
3943 }
3944 #endif  // ENABLE_KF_DENOISE
3945
3946 static void vp9_try_disable_lookahead_aq(VP9_COMP *cpi, size_t *size,
3947                                          uint8_t *dest) {
3948   if (cpi->common.seg.enabled)
3949     if (ALT_REF_AQ_PROTECT_GAIN) {
3950       size_t nsize = *size;
3951       int overhead;
3952
3953       // TODO(yuryg): optimize this, as
3954       // we don't really need to repack
3955
3956       save_coding_context(cpi);
3957       vp9_disable_segmentation(&cpi->common.seg);
3958       vp9_pack_bitstream(cpi, dest, &nsize);
3959       restore_coding_context(cpi);
3960
3961       overhead = (int)*size - (int)nsize;
3962
3963       if (vp9_alt_ref_aq_disable_if(cpi->alt_ref_aq, overhead, (int)*size))
3964         vp9_encode_frame(cpi);
3965       else
3966         vp9_enable_segmentation(&cpi->common.seg);
3967     }
3968 }
3969
3970 static void encode_frame_to_data_rate(VP9_COMP *cpi, size_t *size,
3971                                       uint8_t *dest,
3972                                       unsigned int *frame_flags) {
3973   VP9_COMMON *const cm = &cpi->common;
3974   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
3975   struct segmentation *const seg = &cm->seg;
3976   TX_SIZE t;
3977
3978   set_ext_overrides(cpi);
3979   vpx_clear_system_state();
3980
3981 #ifdef ENABLE_KF_DENOISE
3982   // Spatial denoise of key frame.
3983   if (is_spatial_denoise_enabled(cpi)) spatial_denoise_frame(cpi);
3984 #endif
3985
3986   // Set the arf sign bias for this frame.
3987   set_arf_sign_bias(cpi);
3988
3989   // Set default state for segment based loop filter update flags.
3990   cm->lf.mode_ref_delta_update = 0;
3991
3992   if (cpi->oxcf.pass == 2 && cpi->sf.adaptive_interp_filter_search)
3993     cpi->sf.interp_filter_search_mask = setup_interp_filter_search_mask(cpi);
3994
3995   // Set various flags etc to special state if it is a key frame.
3996   if (frame_is_intra_only(cm)) {
3997     // Reset the loop filter deltas and segmentation map.
3998     vp9_reset_segment_features(&cm->seg);
3999
4000     // If segmentation is enabled force a map update for key frames.
4001     if (seg->enabled) {
4002       seg->update_map = 1;
4003       seg->update_data = 1;
4004     }
4005
4006     // The alternate reference frame cannot be active for a key frame.
4007     cpi->rc.source_alt_ref_active = 0;
4008
4009     cm->error_resilient_mode = oxcf->error_resilient_mode;
4010     cm->frame_parallel_decoding_mode = oxcf->frame_parallel_decoding_mode;
4011
4012     // By default, encoder assumes decoder can use prev_mi.
4013     if (cm->error_resilient_mode) {
4014       cm->frame_parallel_decoding_mode = 1;
4015       cm->reset_frame_context = 0;
4016       cm->refresh_frame_context = 0;
4017     } else if (cm->intra_only) {
4018       // Only reset the current context.
4019       cm->reset_frame_context = 2;
4020     }
4021   }
4022   if (is_two_pass_svc(cpi) && cm->error_resilient_mode == 0) {
4023     // Use context 0 for intra only empty frame, but the last frame context
4024     // for other empty frames.
4025     if (cpi->svc.encode_empty_frame_state == ENCODING) {
4026       if (cpi->svc.encode_intra_empty_frame != 0)
4027         cm->frame_context_idx = 0;
4028       else
4029         cm->frame_context_idx = FRAME_CONTEXTS - 1;
4030     } else {
4031       cm->frame_context_idx =
4032           cpi->svc.spatial_layer_id * cpi->svc.number_temporal_layers +
4033           cpi->svc.temporal_layer_id;
4034     }
4035
4036     cm->frame_parallel_decoding_mode = oxcf->frame_parallel_decoding_mode;
4037
4038     // The probs will be updated based on the frame type of its previous
4039     // frame if frame_parallel_decoding_mode is 0. The type may vary for
4040     // the frame after a key frame in base layer since we may drop enhancement
4041     // layers. So set frame_parallel_decoding_mode to 1 in this case.
4042     if (cm->frame_parallel_decoding_mode == 0) {
4043       if (cpi->svc.number_temporal_layers == 1) {
4044         if (cpi->svc.spatial_layer_id == 0 &&
4045             cpi->svc.layer_context[0].last_frame_type == KEY_FRAME)
4046           cm->frame_parallel_decoding_mode = 1;
4047       } else if (cpi->svc.spatial_layer_id == 0) {
4048         // Find the 2nd frame in temporal base layer and 1st frame in temporal
4049         // enhancement layers from the key frame.
4050         int i;
4051         for (i = 0; i < cpi->svc.number_temporal_layers; ++i) {
4052           if (cpi->svc.layer_context[0].frames_from_key_frame == 1 << i) {
4053             cm->frame_parallel_decoding_mode = 1;
4054             break;
4055           }
4056         }
4057       }
4058     }
4059   }
4060
4061   // For 1 pass CBR, check if we are dropping this frame.
4062   // For spatial layers, for now only check for frame-dropping on first spatial
4063   // layer, and if decision is to drop, we drop whole super-frame.
4064   if (oxcf->pass == 0 && oxcf->rc_mode == VPX_CBR &&
4065       cm->frame_type != KEY_FRAME) {
4066     if (vp9_rc_drop_frame(cpi) ||
4067         (is_one_pass_cbr_svc(cpi) && cpi->svc.rc_drop_superframe == 1)) {
4068       vp9_rc_postencode_update_drop_frame(cpi);
4069       ++cm->current_video_frame;
4070       cpi->ext_refresh_frame_flags_pending = 0;
4071       cpi->svc.rc_drop_superframe = 1;
4072       // TODO(marpan): Advancing the svc counters on dropped frames can break
4073       // the referencing scheme for the fixed svc patterns defined in
4074       // vp9_one_pass_cbr_svc_start_layer(). Look into fixing this issue, but
4075       // for now, don't advance the svc frame counters on dropped frame.
4076       // if (cpi->use_svc)
4077       //   vp9_inc_frame_in_layer(cpi);
4078       return;
4079     }
4080   }
4081
4082   vpx_clear_system_state();
4083
4084 #if CONFIG_INTERNAL_STATS
4085   memset(cpi->mode_chosen_counts, 0,
4086          MAX_MODES * sizeof(*cpi->mode_chosen_counts));
4087 #endif
4088
4089   if (cpi->sf.recode_loop == DISALLOW_RECODE) {
4090     encode_without_recode_loop(cpi, size, dest);
4091   } else {
4092     encode_with_recode_loop(cpi, size, dest);
4093   }
4094
4095   // Disable segmentation if it decrease rate/distortion ratio
4096   if (cpi->oxcf.aq_mode == LOOKAHEAD_AQ)
4097     vp9_try_disable_lookahead_aq(cpi, size, dest);
4098
4099 #if CONFIG_VP9_TEMPORAL_DENOISING
4100 #ifdef OUTPUT_YUV_DENOISED
4101   if (oxcf->noise_sensitivity > 0 && denoise_svc(cpi)) {
4102     vp9_write_yuv_frame_420(&cpi->denoiser.running_avg_y[INTRA_FRAME],
4103                             yuv_denoised_file);
4104   }
4105 #endif
4106 #endif
4107 #ifdef OUTPUT_YUV_SKINMAP
4108   if (cpi->common.current_video_frame > 1) {
4109     vp9_compute_skin_map(cpi, yuv_skinmap_file);
4110   }
4111 #endif
4112
4113   // Special case code to reduce pulsing when key frames are forced at a
4114   // fixed interval. Note the reconstruction error if it is the frame before
4115   // the force key frame
4116   if (cpi->rc.next_key_frame_forced && cpi->rc.frames_to_key == 1) {
4117 #if CONFIG_VP9_HIGHBITDEPTH
4118     if (cm->use_highbitdepth) {
4119       cpi->ambient_err =
4120           vpx_highbd_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
4121     } else {
4122       cpi->ambient_err = vpx_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
4123     }
4124 #else
4125     cpi->ambient_err = vpx_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
4126 #endif  // CONFIG_VP9_HIGHBITDEPTH
4127   }
4128
4129   // If the encoder forced a KEY_FRAME decision
4130   if (cm->frame_type == KEY_FRAME) cpi->refresh_last_frame = 1;
4131
4132   cm->frame_to_show = get_frame_new_buffer(cm);
4133   cm->frame_to_show->color_space = cm->color_space;
4134   cm->frame_to_show->color_range = cm->color_range;
4135   cm->frame_to_show->render_width = cm->render_width;
4136   cm->frame_to_show->render_height = cm->render_height;
4137
4138   // Pick the loop filter level for the frame.
4139   loopfilter_frame(cpi, cm);
4140
4141   // build the bitstream
4142   vp9_pack_bitstream(cpi, dest, size);
4143
4144   if (cm->seg.update_map) update_reference_segmentation_map(cpi);
4145
4146   if (frame_is_intra_only(cm) == 0) {
4147     release_scaled_references(cpi);
4148   }
4149   vp9_update_reference_frames(cpi);
4150
4151   for (t = TX_4X4; t <= TX_32X32; t++)
4152     full_to_model_counts(cpi->td.counts->coef[t],
4153                          cpi->td.rd_counts.coef_counts[t]);
4154
4155   if (!cm->error_resilient_mode && !cm->frame_parallel_decoding_mode)
4156     vp9_adapt_coef_probs(cm);
4157
4158   if (!frame_is_intra_only(cm)) {
4159     if (!cm->error_resilient_mode && !cm->frame_parallel_decoding_mode) {
4160       vp9_adapt_mode_probs(cm);
4161       vp9_adapt_mv_probs(cm, cm->allow_high_precision_mv);
4162     }
4163   }
4164
4165   cpi->ext_refresh_frame_flags_pending = 0;
4166
4167   if (cpi->refresh_golden_frame == 1)
4168     cpi->frame_flags |= FRAMEFLAGS_GOLDEN;
4169   else
4170     cpi->frame_flags &= ~FRAMEFLAGS_GOLDEN;
4171
4172   if (cpi->refresh_alt_ref_frame == 1)
4173     cpi->frame_flags |= FRAMEFLAGS_ALTREF;
4174   else
4175     cpi->frame_flags &= ~FRAMEFLAGS_ALTREF;
4176
4177   cpi->ref_frame_flags = get_ref_frame_flags(cpi);
4178
4179   cm->last_frame_type = cm->frame_type;
4180
4181   if (!(is_two_pass_svc(cpi) && cpi->svc.encode_empty_frame_state == ENCODING))
4182     vp9_rc_postencode_update(cpi, *size);
4183
4184 #if 0
4185   output_frame_level_debug_stats(cpi);
4186 #endif
4187
4188   if (cm->frame_type == KEY_FRAME) {
4189     // Tell the caller that the frame was coded as a key frame
4190     *frame_flags = cpi->frame_flags | FRAMEFLAGS_KEY;
4191   } else {
4192     *frame_flags = cpi->frame_flags & ~FRAMEFLAGS_KEY;
4193   }
4194
4195   // Clear the one shot update flags for segmentation map and mode/ref loop
4196   // filter deltas.
4197   cm->seg.update_map = 0;
4198   cm->seg.update_data = 0;
4199   cm->lf.mode_ref_delta_update = 0;
4200
4201   // keep track of the last coded dimensions
4202   cm->last_width = cm->width;
4203   cm->last_height = cm->height;
4204
4205   // reset to normal state now that we are done.
4206   if (!cm->show_existing_frame) cm->last_show_frame = cm->show_frame;
4207
4208   if (cm->show_frame) {
4209     vp9_swap_mi_and_prev_mi(cm);
4210     // Don't increment frame counters if this was an altref buffer
4211     // update not a real frame
4212     ++cm->current_video_frame;
4213     if (cpi->use_svc) vp9_inc_frame_in_layer(cpi);
4214   }
4215   cm->prev_frame = cm->cur_frame;
4216
4217   if (cpi->use_svc)
4218     cpi->svc
4219         .layer_context[cpi->svc.spatial_layer_id *
4220                            cpi->svc.number_temporal_layers +
4221                        cpi->svc.temporal_layer_id]
4222         .last_frame_type = cm->frame_type;
4223
4224   cpi->force_update_segmentation = 0;
4225
4226   if (cpi->oxcf.aq_mode == LOOKAHEAD_AQ)
4227     vp9_alt_ref_aq_unset_all(cpi->alt_ref_aq, cpi);
4228 }
4229
4230 static void SvcEncode(VP9_COMP *cpi, size_t *size, uint8_t *dest,
4231                       unsigned int *frame_flags) {
4232   vp9_rc_get_svc_params(cpi);
4233   encode_frame_to_data_rate(cpi, size, dest, frame_flags);
4234 }
4235
4236 static void Pass0Encode(VP9_COMP *cpi, size_t *size, uint8_t *dest,
4237                         unsigned int *frame_flags) {
4238   if (cpi->oxcf.rc_mode == VPX_CBR) {
4239     vp9_rc_get_one_pass_cbr_params(cpi);
4240   } else {
4241     vp9_rc_get_one_pass_vbr_params(cpi);
4242   }
4243   encode_frame_to_data_rate(cpi, size, dest, frame_flags);
4244 }
4245
4246 static void Pass2Encode(VP9_COMP *cpi, size_t *size, uint8_t *dest,
4247                         unsigned int *frame_flags) {
4248   cpi->allow_encode_breakout = ENCODE_BREAKOUT_ENABLED;
4249   encode_frame_to_data_rate(cpi, size, dest, frame_flags);
4250
4251   if (!(is_two_pass_svc(cpi) && cpi->svc.encode_empty_frame_state == ENCODING))
4252     vp9_twopass_postencode_update(cpi);
4253 }
4254
4255 static void init_ref_frame_bufs(VP9_COMMON *cm) {
4256   int i;
4257   BufferPool *const pool = cm->buffer_pool;
4258   cm->new_fb_idx = INVALID_IDX;
4259   for (i = 0; i < REF_FRAMES; ++i) {
4260     cm->ref_frame_map[i] = INVALID_IDX;
4261     pool->frame_bufs[i].ref_count = 0;
4262   }
4263 }
4264
4265 static void check_initial_width(VP9_COMP *cpi,
4266 #if CONFIG_VP9_HIGHBITDEPTH
4267                                 int use_highbitdepth,
4268 #endif
4269                                 int subsampling_x, int subsampling_y) {
4270   VP9_COMMON *const cm = &cpi->common;
4271
4272   if (!cpi->initial_width ||
4273 #if CONFIG_VP9_HIGHBITDEPTH
4274       cm->use_highbitdepth != use_highbitdepth ||
4275 #endif
4276       cm->subsampling_x != subsampling_x ||
4277       cm->subsampling_y != subsampling_y) {
4278     cm->subsampling_x = subsampling_x;
4279     cm->subsampling_y = subsampling_y;
4280 #if CONFIG_VP9_HIGHBITDEPTH
4281     cm->use_highbitdepth = use_highbitdepth;
4282 #endif
4283
4284     alloc_raw_frame_buffers(cpi);
4285     init_ref_frame_bufs(cm);
4286     alloc_util_frame_buffers(cpi);
4287
4288     init_motion_estimation(cpi);  // TODO(agrange) This can be removed.
4289
4290     cpi->initial_width = cm->width;
4291     cpi->initial_height = cm->height;
4292     cpi->initial_mbs = cm->MBs;
4293   }
4294 }
4295
4296 int vp9_receive_raw_frame(VP9_COMP *cpi, vpx_enc_frame_flags_t frame_flags,
4297                           YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
4298                           int64_t end_time) {
4299   VP9_COMMON *const cm = &cpi->common;
4300   struct vpx_usec_timer timer;
4301   int res = 0;
4302   const int subsampling_x = sd->subsampling_x;
4303   const int subsampling_y = sd->subsampling_y;
4304 #if CONFIG_VP9_HIGHBITDEPTH
4305   const int use_highbitdepth = (sd->flags & YV12_FLAG_HIGHBITDEPTH) != 0;
4306 #endif
4307
4308 #if CONFIG_VP9_HIGHBITDEPTH
4309   check_initial_width(cpi, use_highbitdepth, subsampling_x, subsampling_y);
4310 #else
4311   check_initial_width(cpi, subsampling_x, subsampling_y);
4312 #endif  // CONFIG_VP9_HIGHBITDEPTH
4313
4314 #if CONFIG_VP9_TEMPORAL_DENOISING
4315   setup_denoiser_buffer(cpi);
4316 #endif
4317   vpx_usec_timer_start(&timer);
4318
4319   if (vp9_lookahead_push(cpi->lookahead, sd, time_stamp, end_time,
4320 #if CONFIG_VP9_HIGHBITDEPTH
4321                          use_highbitdepth,
4322 #endif  // CONFIG_VP9_HIGHBITDEPTH
4323                          frame_flags))
4324     res = -1;
4325   vpx_usec_timer_mark(&timer);
4326   cpi->time_receive_data += vpx_usec_timer_elapsed(&timer);
4327
4328   if ((cm->profile == PROFILE_0 || cm->profile == PROFILE_2) &&
4329       (subsampling_x != 1 || subsampling_y != 1)) {
4330     vpx_internal_error(&cm->error, VPX_CODEC_INVALID_PARAM,
4331                        "Non-4:2:0 color format requires profile 1 or 3");
4332     res = -1;
4333   }
4334   if ((cm->profile == PROFILE_1 || cm->profile == PROFILE_3) &&
4335       (subsampling_x == 1 && subsampling_y == 1)) {
4336     vpx_internal_error(&cm->error, VPX_CODEC_INVALID_PARAM,
4337                        "4:2:0 color format requires profile 0 or 2");
4338     res = -1;
4339   }
4340
4341   return res;
4342 }
4343
4344 static int frame_is_reference(const VP9_COMP *cpi) {
4345   const VP9_COMMON *cm = &cpi->common;
4346
4347   return cm->frame_type == KEY_FRAME || cpi->refresh_last_frame ||
4348          cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame ||
4349          cm->refresh_frame_context || cm->lf.mode_ref_delta_update ||
4350          cm->seg.update_map || cm->seg.update_data;
4351 }
4352
4353 static void adjust_frame_rate(VP9_COMP *cpi,
4354                               const struct lookahead_entry *source) {
4355   int64_t this_duration;
4356   int step = 0;
4357
4358   if (source->ts_start == cpi->first_time_stamp_ever) {
4359     this_duration = source->ts_end - source->ts_start;
4360     step = 1;
4361   } else {
4362     int64_t last_duration =
4363         cpi->last_end_time_stamp_seen - cpi->last_time_stamp_seen;
4364
4365     this_duration = source->ts_end - cpi->last_end_time_stamp_seen;
4366
4367     // do a step update if the duration changes by 10%
4368     if (last_duration)
4369       step = (int)((this_duration - last_duration) * 10 / last_duration);
4370   }
4371
4372   if (this_duration) {
4373     if (step) {
4374       vp9_new_framerate(cpi, 10000000.0 / this_duration);
4375     } else {
4376       // Average this frame's rate into the last second's average
4377       // frame rate. If we haven't seen 1 second yet, then average
4378       // over the whole interval seen.
4379       const double interval = VPXMIN(
4380           (double)(source->ts_end - cpi->first_time_stamp_ever), 10000000.0);
4381       double avg_duration = 10000000.0 / cpi->framerate;
4382       avg_duration *= (interval - avg_duration + this_duration);
4383       avg_duration /= interval;
4384
4385       vp9_new_framerate(cpi, 10000000.0 / avg_duration);
4386     }
4387   }
4388   cpi->last_time_stamp_seen = source->ts_start;
4389   cpi->last_end_time_stamp_seen = source->ts_end;
4390 }
4391
4392 // Returns 0 if this is not an alt ref else the offset of the source frame
4393 // used as the arf midpoint.
4394 static int get_arf_src_index(VP9_COMP *cpi) {
4395   RATE_CONTROL *const rc = &cpi->rc;
4396   int arf_src_index = 0;
4397   if (is_altref_enabled(cpi)) {
4398     if (cpi->oxcf.pass == 2) {
4399       const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
4400       if (gf_group->update_type[gf_group->index] == ARF_UPDATE) {
4401         arf_src_index = gf_group->arf_src_offset[gf_group->index];
4402       }
4403     } else if (rc->source_alt_ref_pending) {
4404       arf_src_index = rc->frames_till_gf_update_due;
4405     }
4406   }
4407   return arf_src_index;
4408 }
4409
4410 static void check_src_altref(VP9_COMP *cpi,
4411                              const struct lookahead_entry *source) {
4412   RATE_CONTROL *const rc = &cpi->rc;
4413
4414   if (cpi->oxcf.pass == 2) {
4415     const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
4416     rc->is_src_frame_alt_ref =
4417         (gf_group->update_type[gf_group->index] == OVERLAY_UPDATE);
4418   } else {
4419     rc->is_src_frame_alt_ref =
4420         cpi->alt_ref_source && (source == cpi->alt_ref_source);
4421   }
4422
4423   if (rc->is_src_frame_alt_ref) {
4424     // Current frame is an ARF overlay frame.
4425     cpi->alt_ref_source = NULL;
4426
4427     // Don't refresh the last buffer for an ARF overlay frame. It will
4428     // become the GF so preserve last as an alternative prediction option.
4429     cpi->refresh_last_frame = 0;
4430   }
4431 }
4432
4433 #if CONFIG_INTERNAL_STATS
4434 extern double vp9_get_blockiness(const uint8_t *img1, int img1_pitch,
4435                                  const uint8_t *img2, int img2_pitch, int width,
4436                                  int height);
4437
4438 static void adjust_image_stat(double y, double u, double v, double all,
4439                               ImageStat *s) {
4440   s->stat[Y] += y;
4441   s->stat[U] += u;
4442   s->stat[V] += v;
4443   s->stat[ALL] += all;
4444   s->worst = VPXMIN(s->worst, all);
4445 }
4446 #endif  // CONFIG_INTERNAL_STATS
4447
4448 // Adjust the maximum allowable frame size for the target level.
4449 static void level_rc_framerate(VP9_COMP *cpi, int arf_src_index) {
4450   RATE_CONTROL *const rc = &cpi->rc;
4451   LevelConstraint *const ls = &cpi->level_constraint;
4452   VP9_COMMON *const cm = &cpi->common;
4453   const double max_cpb_size = ls->max_cpb_size;
4454   vpx_clear_system_state();
4455   rc->max_frame_bandwidth = VPXMIN(rc->max_frame_bandwidth, ls->max_frame_size);
4456   if (frame_is_intra_only(cm)) {
4457     rc->max_frame_bandwidth =
4458         VPXMIN(rc->max_frame_bandwidth, (int)(max_cpb_size * 0.5));
4459   } else if (arf_src_index > 0) {
4460     rc->max_frame_bandwidth =
4461         VPXMIN(rc->max_frame_bandwidth, (int)(max_cpb_size * 0.4));
4462   } else {
4463     rc->max_frame_bandwidth =
4464         VPXMIN(rc->max_frame_bandwidth, (int)(max_cpb_size * 0.2));
4465   }
4466 }
4467
4468 static void update_level_info(VP9_COMP *cpi, size_t *size, int arf_src_index) {
4469   VP9_COMMON *const cm = &cpi->common;
4470   Vp9LevelInfo *const level_info = &cpi->level_info;
4471   Vp9LevelSpec *const level_spec = &level_info->level_spec;
4472   Vp9LevelStats *const level_stats = &level_info->level_stats;
4473   int i, idx;
4474   uint64_t luma_samples, dur_end;
4475   const uint32_t luma_pic_size = cm->width * cm->height;
4476   LevelConstraint *const level_constraint = &cpi->level_constraint;
4477   const int8_t level_index = level_constraint->level_index;
4478   double cpb_data_size;
4479
4480   vpx_clear_system_state();
4481
4482   // update level_stats
4483   level_stats->total_compressed_size += *size;
4484   if (cm->show_frame) {
4485     level_stats->total_uncompressed_size +=
4486         luma_pic_size +
4487         2 * (luma_pic_size >> (cm->subsampling_x + cm->subsampling_y));
4488     level_stats->time_encoded =
4489         (cpi->last_end_time_stamp_seen - cpi->first_time_stamp_ever) /
4490         (double)TICKS_PER_SEC;
4491   }
4492
4493   if (arf_src_index > 0) {
4494     if (!level_stats->seen_first_altref) {
4495       level_stats->seen_first_altref = 1;
4496     } else if (level_stats->frames_since_last_altref <
4497                level_spec->min_altref_distance) {
4498       level_spec->min_altref_distance = level_stats->frames_since_last_altref;
4499     }
4500     level_stats->frames_since_last_altref = 0;
4501   } else {
4502     ++level_stats->frames_since_last_altref;
4503   }
4504
4505   if (level_stats->frame_window_buffer.len < FRAME_WINDOW_SIZE - 1) {
4506     idx = (level_stats->frame_window_buffer.start +
4507            level_stats->frame_window_buffer.len++) %
4508           FRAME_WINDOW_SIZE;
4509   } else {
4510     idx = level_stats->frame_window_buffer.start;
4511     level_stats->frame_window_buffer.start = (idx + 1) % FRAME_WINDOW_SIZE;
4512   }
4513   level_stats->frame_window_buffer.buf[idx].ts = cpi->last_time_stamp_seen;
4514   level_stats->frame_window_buffer.buf[idx].size = (uint32_t)(*size);
4515   level_stats->frame_window_buffer.buf[idx].luma_samples = luma_pic_size;
4516
4517   if (cm->frame_type == KEY_FRAME) {
4518     level_stats->ref_refresh_map = 0;
4519   } else {
4520     int count = 0;
4521     level_stats->ref_refresh_map |= vp9_get_refresh_mask(cpi);
4522     // Also need to consider the case where the encoder refers to a buffer
4523     // that has been implicitly refreshed after encoding a keyframe.
4524     if (!cm->intra_only) {
4525       level_stats->ref_refresh_map |= (1 << cpi->lst_fb_idx);
4526       level_stats->ref_refresh_map |= (1 << cpi->gld_fb_idx);
4527       level_stats->ref_refresh_map |= (1 << cpi->alt_fb_idx);
4528     }
4529     for (i = 0; i < REF_FRAMES; ++i) {
4530       count += (level_stats->ref_refresh_map >> i) & 1;
4531     }
4532     if (count > level_spec->max_ref_frame_buffers) {
4533       level_spec->max_ref_frame_buffers = count;
4534     }
4535   }
4536
4537   // update average_bitrate
4538   level_spec->average_bitrate = (double)level_stats->total_compressed_size /
4539                                 125.0 / level_stats->time_encoded;
4540
4541   // update max_luma_sample_rate
4542   luma_samples = 0;
4543   for (i = 0; i < level_stats->frame_window_buffer.len; ++i) {
4544     idx = (level_stats->frame_window_buffer.start +
4545            level_stats->frame_window_buffer.len - 1 - i) %
4546           FRAME_WINDOW_SIZE;
4547     if (i == 0) {
4548       dur_end = level_stats->frame_window_buffer.buf[idx].ts;
4549     }
4550     if (dur_end - level_stats->frame_window_buffer.buf[idx].ts >=
4551         TICKS_PER_SEC) {
4552       break;
4553     }
4554     luma_samples += level_stats->frame_window_buffer.buf[idx].luma_samples;
4555   }
4556   if (luma_samples > level_spec->max_luma_sample_rate) {
4557     level_spec->max_luma_sample_rate = luma_samples;
4558   }
4559
4560   // update max_cpb_size
4561   cpb_data_size = 0;
4562   for (i = 0; i < CPB_WINDOW_SIZE; ++i) {
4563     if (i >= level_stats->frame_window_buffer.len) break;
4564     idx = (level_stats->frame_window_buffer.start +
4565            level_stats->frame_window_buffer.len - 1 - i) %
4566           FRAME_WINDOW_SIZE;
4567     cpb_data_size += level_stats->frame_window_buffer.buf[idx].size;
4568   }
4569   cpb_data_size = cpb_data_size / 125.0;
4570   if (cpb_data_size > level_spec->max_cpb_size) {
4571     level_spec->max_cpb_size = cpb_data_size;
4572   }
4573
4574   // update max_luma_picture_size
4575   if (luma_pic_size > level_spec->max_luma_picture_size) {
4576     level_spec->max_luma_picture_size = luma_pic_size;
4577   }
4578
4579   // update compression_ratio
4580   level_spec->compression_ratio = (double)level_stats->total_uncompressed_size *
4581                                   cm->bit_depth /
4582                                   level_stats->total_compressed_size / 8.0;
4583
4584   // update max_col_tiles
4585   if (level_spec->max_col_tiles < (1 << cm->log2_tile_cols)) {
4586     level_spec->max_col_tiles = (1 << cm->log2_tile_cols);
4587   }
4588
4589   if (level_index >= 0 && level_constraint->fail_flag == 0) {
4590     if (level_spec->max_luma_picture_size >
4591         vp9_level_defs[level_index].max_luma_picture_size) {
4592       level_constraint->fail_flag |= (1 << LUMA_PIC_SIZE_TOO_LARGE);
4593       vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
4594                          "Failed to encode to the target level %d. %s",
4595                          vp9_level_defs[level_index].level,
4596                          level_fail_messages[LUMA_PIC_SIZE_TOO_LARGE]);
4597     }
4598
4599     if ((double)level_spec->max_luma_sample_rate >
4600         (double)vp9_level_defs[level_index].max_luma_sample_rate *
4601             (1 + SAMPLE_RATE_GRACE_P)) {
4602       level_constraint->fail_flag |= (1 << LUMA_SAMPLE_RATE_TOO_LARGE);
4603       vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
4604                          "Failed to encode to the target level %d. %s",
4605                          vp9_level_defs[level_index].level,
4606                          level_fail_messages[LUMA_SAMPLE_RATE_TOO_LARGE]);
4607     }
4608
4609     if (level_spec->max_col_tiles > vp9_level_defs[level_index].max_col_tiles) {
4610       level_constraint->fail_flag |= (1 << TOO_MANY_COLUMN_TILE);
4611       vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
4612                          "Failed to encode to the target level %d. %s",
4613                          vp9_level_defs[level_index].level,
4614                          level_fail_messages[TOO_MANY_COLUMN_TILE]);
4615     }
4616
4617     if (level_spec->min_altref_distance <
4618         vp9_level_defs[level_index].min_altref_distance) {
4619       level_constraint->fail_flag |= (1 << ALTREF_DIST_TOO_SMALL);
4620       vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
4621                          "Failed to encode to the target level %d. %s",
4622                          vp9_level_defs[level_index].level,
4623                          level_fail_messages[ALTREF_DIST_TOO_SMALL]);
4624     }
4625
4626     if (level_spec->max_ref_frame_buffers >
4627         vp9_level_defs[level_index].max_ref_frame_buffers) {
4628       level_constraint->fail_flag |= (1 << TOO_MANY_REF_BUFFER);
4629       vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
4630                          "Failed to encode to the target level %d. %s",
4631                          vp9_level_defs[level_index].level,
4632                          level_fail_messages[TOO_MANY_REF_BUFFER]);
4633     }
4634
4635     if (level_spec->max_cpb_size > vp9_level_defs[level_index].max_cpb_size) {
4636       level_constraint->fail_flag |= (1 << CPB_TOO_LARGE);
4637       vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
4638                          "Failed to encode to the target level %d. %s",
4639                          vp9_level_defs[level_index].level,
4640                          level_fail_messages[CPB_TOO_LARGE]);
4641     }
4642
4643     // Set an upper bound for the next frame size. It will be used in
4644     // level_rc_framerate() before encoding the next frame.
4645     cpb_data_size = 0;
4646     for (i = 0; i < CPB_WINDOW_SIZE - 1; ++i) {
4647       if (i >= level_stats->frame_window_buffer.len) break;
4648       idx = (level_stats->frame_window_buffer.start +
4649              level_stats->frame_window_buffer.len - 1 - i) %
4650             FRAME_WINDOW_SIZE;
4651       cpb_data_size += level_stats->frame_window_buffer.buf[idx].size;
4652     }
4653     cpb_data_size = cpb_data_size / 125.0;
4654     level_constraint->max_frame_size =
4655         (int)((vp9_level_defs[level_index].max_cpb_size - cpb_data_size) *
4656               1000.0);
4657     if (level_stats->frame_window_buffer.len < CPB_WINDOW_SIZE - 1)
4658       level_constraint->max_frame_size >>= 1;
4659   }
4660 }
4661
4662 int vp9_get_compressed_data(VP9_COMP *cpi, unsigned int *frame_flags,
4663                             size_t *size, uint8_t *dest, int64_t *time_stamp,
4664                             int64_t *time_end, int flush) {
4665   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
4666   VP9_COMMON *const cm = &cpi->common;
4667   BufferPool *const pool = cm->buffer_pool;
4668   RATE_CONTROL *const rc = &cpi->rc;
4669   struct vpx_usec_timer cmptimer;
4670   YV12_BUFFER_CONFIG *force_src_buffer = NULL;
4671   struct lookahead_entry *last_source = NULL;
4672   struct lookahead_entry *source = NULL;
4673   int arf_src_index;
4674   int i;
4675
4676   if (is_two_pass_svc(cpi)) {
4677 #if CONFIG_SPATIAL_SVC
4678     vp9_svc_start_frame(cpi);
4679     // Use a small empty frame instead of a real frame
4680     if (cpi->svc.encode_empty_frame_state == ENCODING)
4681       source = &cpi->svc.empty_frame;
4682 #endif
4683     if (oxcf->pass == 2) vp9_restore_layer_context(cpi);
4684   } else if (is_one_pass_cbr_svc(cpi)) {
4685     vp9_one_pass_cbr_svc_start_layer(cpi);
4686   }
4687
4688   vpx_usec_timer_start(&cmptimer);
4689
4690   vp9_set_high_precision_mv(cpi, ALTREF_HIGH_PRECISION_MV);
4691
4692   // Is multi-arf enabled.
4693   // Note that at the moment multi_arf is only configured for 2 pass VBR and
4694   // will not work properly with svc.
4695   if ((oxcf->pass == 2) && !cpi->use_svc && (cpi->oxcf.enable_auto_arf > 1))
4696     cpi->multi_arf_allowed = 1;
4697   else
4698     cpi->multi_arf_allowed = 0;
4699
4700   // Normal defaults
4701   cm->reset_frame_context = 0;
4702   cm->refresh_frame_context = 1;
4703   if (!is_one_pass_cbr_svc(cpi)) {
4704     cpi->refresh_last_frame = 1;
4705     cpi->refresh_golden_frame = 0;
4706     cpi->refresh_alt_ref_frame = 0;
4707   }
4708
4709   // Should we encode an arf frame.
4710   arf_src_index = get_arf_src_index(cpi);
4711
4712   // Skip alt frame if we encode the empty frame
4713   if (is_two_pass_svc(cpi) && source != NULL) arf_src_index = 0;
4714
4715   if (arf_src_index) {
4716     for (i = 0; i <= arf_src_index; ++i) {
4717       struct lookahead_entry *e = vp9_lookahead_peek(cpi->lookahead, i);
4718       // Avoid creating an alt-ref if there's a forced keyframe pending.
4719       if (e == NULL) {
4720         break;
4721       } else if (e->flags == VPX_EFLAG_FORCE_KF) {
4722         arf_src_index = 0;
4723         flush = 1;
4724         break;
4725       }
4726     }
4727   }
4728
4729   if (arf_src_index) {
4730     assert(arf_src_index <= rc->frames_to_key);
4731
4732     if ((source = vp9_lookahead_peek(cpi->lookahead, arf_src_index)) != NULL) {
4733       cpi->alt_ref_source = source;
4734
4735 #if CONFIG_SPATIAL_SVC
4736       if (is_two_pass_svc(cpi) && cpi->svc.spatial_layer_id > 0) {
4737         int i;
4738         // Reference a hidden frame from a lower layer
4739         for (i = cpi->svc.spatial_layer_id - 1; i >= 0; --i) {
4740           if (oxcf->ss_enable_auto_arf[i]) {
4741             cpi->gld_fb_idx = cpi->svc.layer_context[i].alt_ref_idx;
4742             break;
4743           }
4744         }
4745       }
4746       cpi->svc.layer_context[cpi->svc.spatial_layer_id].has_alt_frame = 1;
4747 #endif
4748
4749       if ((oxcf->mode != REALTIME) && (oxcf->arnr_max_frames > 0) &&
4750           (oxcf->arnr_strength > 0)) {
4751         int bitrate = cpi->rc.avg_frame_bandwidth / 40;
4752         int not_low_bitrate = bitrate > ALT_REF_AQ_LOW_BITRATE_BOUNDARY;
4753
4754         int not_last_frame = (cpi->lookahead->sz - arf_src_index > 1);
4755         not_last_frame |= ALT_REF_AQ_APPLY_TO_LAST_FRAME;
4756
4757         // Produce the filtered ARF frame.
4758         vp9_temporal_filter(cpi, arf_src_index);
4759         vpx_extend_frame_borders(&cpi->alt_ref_buffer);
4760
4761         // for small bitrates segmentation overhead usually
4762         // eats all bitrate gain from enabling delta quantizers
4763         if (cpi->oxcf.alt_ref_aq != 0 && not_low_bitrate && not_last_frame)
4764           vp9_alt_ref_aq_setup_mode(cpi->alt_ref_aq, cpi);
4765
4766         force_src_buffer = &cpi->alt_ref_buffer;
4767       }
4768
4769       cm->show_frame = 0;
4770       cm->intra_only = 0;
4771       cpi->refresh_alt_ref_frame = 1;
4772       cpi->refresh_golden_frame = 0;
4773       cpi->refresh_last_frame = 0;
4774       rc->is_src_frame_alt_ref = 0;
4775       rc->source_alt_ref_pending = 0;
4776     } else {
4777       rc->source_alt_ref_pending = 0;
4778     }
4779   }
4780
4781   if (!source) {
4782     // Get last frame source.
4783     if (cm->current_video_frame > 0) {
4784       if ((last_source = vp9_lookahead_peek(cpi->lookahead, -1)) == NULL)
4785         return -1;
4786     }
4787
4788     // Read in the source frame.
4789     if (cpi->use_svc)
4790       source = vp9_svc_lookahead_pop(cpi, cpi->lookahead, flush);
4791     else
4792       source = vp9_lookahead_pop(cpi->lookahead, flush);
4793
4794     if (source != NULL) {
4795       cm->show_frame = 1;
4796       cm->intra_only = 0;
4797       // if the flags indicate intra frame, but if the current picture is for
4798       // non-zero spatial layer, it should not be an intra picture.
4799       // TODO(Won Kap): this needs to change if per-layer intra frame is
4800       // allowed.
4801       if ((source->flags & VPX_EFLAG_FORCE_KF) &&
4802           cpi->svc.spatial_layer_id > cpi->svc.first_spatial_layer_to_encode) {
4803         source->flags &= ~(unsigned int)(VPX_EFLAG_FORCE_KF);
4804       }
4805
4806       // Check to see if the frame should be encoded as an arf overlay.
4807       check_src_altref(cpi, source);
4808     }
4809   }
4810
4811   if (source) {
4812     cpi->un_scaled_source = cpi->Source =
4813         force_src_buffer ? force_src_buffer : &source->img;
4814
4815 #ifdef ENABLE_KF_DENOISE
4816     // Copy of raw source for metrics calculation.
4817     if (is_psnr_calc_enabled(cpi))
4818       vp9_copy_and_extend_frame(cpi->Source, &cpi->raw_unscaled_source);
4819 #endif
4820
4821     cpi->unscaled_last_source = last_source != NULL ? &last_source->img : NULL;
4822
4823     *time_stamp = source->ts_start;
4824     *time_end = source->ts_end;
4825     *frame_flags = (source->flags & VPX_EFLAG_FORCE_KF) ? FRAMEFLAGS_KEY : 0;
4826
4827   } else {
4828     *size = 0;
4829     if (flush && oxcf->pass == 1 && !cpi->twopass.first_pass_done) {
4830       vp9_end_first_pass(cpi); /* get last stats packet */
4831       cpi->twopass.first_pass_done = 1;
4832     }
4833     return -1;
4834   }
4835
4836   if (source->ts_start < cpi->first_time_stamp_ever) {
4837     cpi->first_time_stamp_ever = source->ts_start;
4838     cpi->last_end_time_stamp_seen = source->ts_start;
4839   }
4840
4841   // Clear down mmx registers
4842   vpx_clear_system_state();
4843
4844   // adjust frame rates based on timestamps given
4845   if (cm->show_frame) {
4846     adjust_frame_rate(cpi, source);
4847   }
4848
4849   if (is_one_pass_cbr_svc(cpi)) {
4850     vp9_update_temporal_layer_framerate(cpi);
4851     vp9_restore_layer_context(cpi);
4852   }
4853
4854   // Find a free buffer for the new frame, releasing the reference previously
4855   // held.
4856   if (cm->new_fb_idx != INVALID_IDX) {
4857     --pool->frame_bufs[cm->new_fb_idx].ref_count;
4858   }
4859   cm->new_fb_idx = get_free_fb(cm);
4860
4861   if (cm->new_fb_idx == INVALID_IDX) return -1;
4862
4863   cm->cur_frame = &pool->frame_bufs[cm->new_fb_idx];
4864
4865   if (!cpi->use_svc && cpi->multi_arf_allowed) {
4866     if (cm->frame_type == KEY_FRAME) {
4867       init_buffer_indices(cpi);
4868     } else if (oxcf->pass == 2) {
4869       const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
4870       cpi->alt_fb_idx = gf_group->arf_ref_idx[gf_group->index];
4871     }
4872   }
4873
4874   // Start with a 0 size frame.
4875   *size = 0;
4876
4877   cpi->frame_flags = *frame_flags;
4878
4879   if ((oxcf->pass == 2) &&
4880       (!cpi->use_svc || (is_two_pass_svc(cpi) &&
4881                          cpi->svc.encode_empty_frame_state != ENCODING))) {
4882     vp9_rc_get_second_pass_params(cpi);
4883   } else if (oxcf->pass == 1) {
4884     set_frame_size(cpi);
4885   }
4886
4887   if (oxcf->pass != 1 && cpi->level_constraint.level_index >= 0 &&
4888       cpi->level_constraint.fail_flag == 0)
4889     level_rc_framerate(cpi, arf_src_index);
4890
4891   if (cpi->oxcf.pass != 0 || cpi->use_svc || frame_is_intra_only(cm) == 1) {
4892     for (i = 0; i < MAX_REF_FRAMES; ++i) cpi->scaled_ref_idx[i] = INVALID_IDX;
4893   }
4894
4895   cpi->td.mb.fp_src_pred = 0;
4896   if (oxcf->pass == 1 && (!cpi->use_svc || is_two_pass_svc(cpi))) {
4897     const int lossless = is_lossless_requested(oxcf);
4898 #if CONFIG_VP9_HIGHBITDEPTH
4899     if (cpi->oxcf.use_highbitdepth)
4900       cpi->td.mb.fwd_txm4x4 =
4901           lossless ? vp9_highbd_fwht4x4 : vpx_highbd_fdct4x4;
4902     else
4903       cpi->td.mb.fwd_txm4x4 = lossless ? vp9_fwht4x4 : vpx_fdct4x4;
4904     cpi->td.mb.highbd_itxm_add =
4905         lossless ? vp9_highbd_iwht4x4_add : vp9_highbd_idct4x4_add;
4906 #else
4907     cpi->td.mb.fwd_txm4x4 = lossless ? vp9_fwht4x4 : vpx_fdct4x4;
4908 #endif  // CONFIG_VP9_HIGHBITDEPTH
4909     cpi->td.mb.itxm_add = lossless ? vp9_iwht4x4_add : vp9_idct4x4_add;
4910     vp9_first_pass(cpi, source);
4911   } else if (oxcf->pass == 2 && (!cpi->use_svc || is_two_pass_svc(cpi))) {
4912     Pass2Encode(cpi, size, dest, frame_flags);
4913   } else if (cpi->use_svc) {
4914     SvcEncode(cpi, size, dest, frame_flags);
4915   } else {
4916     // One pass encode
4917     Pass0Encode(cpi, size, dest, frame_flags);
4918   }
4919
4920   if (cm->refresh_frame_context)
4921     cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
4922
4923   // No frame encoded, or frame was dropped, release scaled references.
4924   if ((*size == 0) && (frame_is_intra_only(cm) == 0)) {
4925     release_scaled_references(cpi);
4926   }
4927
4928   if (*size > 0) {
4929     cpi->droppable = !frame_is_reference(cpi);
4930   }
4931
4932   // Save layer specific state.
4933   if (is_one_pass_cbr_svc(cpi) || ((cpi->svc.number_temporal_layers > 1 ||
4934                                     cpi->svc.number_spatial_layers > 1) &&
4935                                    oxcf->pass == 2)) {
4936     vp9_save_layer_context(cpi);
4937   }
4938
4939   vpx_usec_timer_mark(&cmptimer);
4940   cpi->time_compress_data += vpx_usec_timer_elapsed(&cmptimer);
4941
4942   // Should we calculate metrics for the frame.
4943   if (is_psnr_calc_enabled(cpi)) generate_psnr_packet(cpi);
4944
4945   if (cpi->keep_level_stats && oxcf->pass != 1)
4946     update_level_info(cpi, size, arf_src_index);
4947
4948 #if CONFIG_INTERNAL_STATS
4949
4950   if (oxcf->pass != 1) {
4951     double samples = 0.0;
4952     cpi->bytes += (int)(*size);
4953
4954     if (cm->show_frame) {
4955       uint32_t bit_depth = 8;
4956       uint32_t in_bit_depth = 8;
4957       cpi->count++;
4958 #if CONFIG_VP9_HIGHBITDEPTH
4959       if (cm->use_highbitdepth) {
4960         in_bit_depth = cpi->oxcf.input_bit_depth;
4961         bit_depth = cm->bit_depth;
4962       }
4963 #endif
4964
4965       if (cpi->b_calculate_psnr) {
4966         YV12_BUFFER_CONFIG *orig = cpi->raw_source_frame;
4967         YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
4968         YV12_BUFFER_CONFIG *pp = &cm->post_proc_buffer;
4969         PSNR_STATS psnr;
4970 #if CONFIG_VP9_HIGHBITDEPTH
4971         vpx_calc_highbd_psnr(orig, recon, &psnr, cpi->td.mb.e_mbd.bd,
4972                              in_bit_depth);
4973 #else
4974         vpx_calc_psnr(orig, recon, &psnr);
4975 #endif  // CONFIG_VP9_HIGHBITDEPTH
4976
4977         adjust_image_stat(psnr.psnr[1], psnr.psnr[2], psnr.psnr[3],
4978                           psnr.psnr[0], &cpi->psnr);
4979         cpi->total_sq_error += psnr.sse[0];
4980         cpi->total_samples += psnr.samples[0];
4981         samples = psnr.samples[0];
4982
4983         {
4984           PSNR_STATS psnr2;
4985           double frame_ssim2 = 0, weight = 0;
4986 #if CONFIG_VP9_POSTPROC
4987           if (vpx_alloc_frame_buffer(
4988                   pp, recon->y_crop_width, recon->y_crop_height,
4989                   cm->subsampling_x, cm->subsampling_y,
4990 #if CONFIG_VP9_HIGHBITDEPTH
4991                   cm->use_highbitdepth,
4992 #endif
4993                   VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment) < 0) {
4994             vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
4995                                "Failed to allocate post processing buffer");
4996           }
4997           {
4998             vp9_ppflags_t ppflags;
4999             ppflags.post_proc_flag = VP9D_DEBLOCK;
5000             ppflags.deblocking_level = 0;  // not used in vp9_post_proc_frame()
5001             ppflags.noise_level = 0;       // not used in vp9_post_proc_frame()
5002             vp9_post_proc_frame(cm, pp, &ppflags);
5003           }
5004 #endif
5005           vpx_clear_system_state();
5006
5007 #if CONFIG_VP9_HIGHBITDEPTH
5008           vpx_calc_highbd_psnr(orig, pp, &psnr2, cpi->td.mb.e_mbd.bd,
5009                                cpi->oxcf.input_bit_depth);
5010 #else
5011           vpx_calc_psnr(orig, pp, &psnr2);
5012 #endif  // CONFIG_VP9_HIGHBITDEPTH
5013
5014           cpi->totalp_sq_error += psnr2.sse[0];
5015           cpi->totalp_samples += psnr2.samples[0];
5016           adjust_image_stat(psnr2.psnr[1], psnr2.psnr[2], psnr2.psnr[3],
5017                             psnr2.psnr[0], &cpi->psnrp);
5018
5019 #if CONFIG_VP9_HIGHBITDEPTH
5020           if (cm->use_highbitdepth) {
5021             frame_ssim2 = vpx_highbd_calc_ssim(orig, recon, &weight, bit_depth,
5022                                                in_bit_depth);
5023           } else {
5024             frame_ssim2 = vpx_calc_ssim(orig, recon, &weight);
5025           }
5026 #else
5027           frame_ssim2 = vpx_calc_ssim(orig, recon, &weight);
5028 #endif  // CONFIG_VP9_HIGHBITDEPTH
5029
5030           cpi->worst_ssim = VPXMIN(cpi->worst_ssim, frame_ssim2);
5031           cpi->summed_quality += frame_ssim2 * weight;
5032           cpi->summed_weights += weight;
5033
5034 #if CONFIG_VP9_HIGHBITDEPTH
5035           if (cm->use_highbitdepth) {
5036             frame_ssim2 = vpx_highbd_calc_ssim(orig, pp, &weight, bit_depth,
5037                                                in_bit_depth);
5038           } else {
5039             frame_ssim2 = vpx_calc_ssim(orig, pp, &weight);
5040           }
5041 #else
5042           frame_ssim2 = vpx_calc_ssim(orig, pp, &weight);
5043 #endif  // CONFIG_VP9_HIGHBITDEPTH
5044
5045           cpi->summedp_quality += frame_ssim2 * weight;
5046           cpi->summedp_weights += weight;
5047 #if 0
5048           {
5049             FILE *f = fopen("q_used.stt", "a");
5050             fprintf(f, "%5d : Y%f7.3:U%f7.3:V%f7.3:F%f7.3:S%7.3f\n",
5051                     cpi->common.current_video_frame, y2, u2, v2,
5052                     frame_psnr2, frame_ssim2);
5053             fclose(f);
5054           }
5055 #endif
5056         }
5057       }
5058       if (cpi->b_calculate_blockiness) {
5059 #if CONFIG_VP9_HIGHBITDEPTH
5060         if (!cm->use_highbitdepth)
5061 #endif
5062         {
5063           double frame_blockiness = vp9_get_blockiness(
5064               cpi->Source->y_buffer, cpi->Source->y_stride,
5065               cm->frame_to_show->y_buffer, cm->frame_to_show->y_stride,
5066               cpi->Source->y_width, cpi->Source->y_height);
5067           cpi->worst_blockiness =
5068               VPXMAX(cpi->worst_blockiness, frame_blockiness);
5069           cpi->total_blockiness += frame_blockiness;
5070         }
5071       }
5072
5073       if (cpi->b_calculate_consistency) {
5074 #if CONFIG_VP9_HIGHBITDEPTH
5075         if (!cm->use_highbitdepth)
5076 #endif
5077         {
5078           double this_inconsistency = vpx_get_ssim_metrics(
5079               cpi->Source->y_buffer, cpi->Source->y_stride,
5080               cm->frame_to_show->y_buffer, cm->frame_to_show->y_stride,
5081               cpi->Source->y_width, cpi->Source->y_height, cpi->ssim_vars,
5082               &cpi->metrics, 1);
5083
5084           const double peak = (double)((1 << cpi->oxcf.input_bit_depth) - 1);
5085           double consistency =
5086               vpx_sse_to_psnr(samples, peak, (double)cpi->total_inconsistency);
5087           if (consistency > 0.0)
5088             cpi->worst_consistency =
5089                 VPXMIN(cpi->worst_consistency, consistency);
5090           cpi->total_inconsistency += this_inconsistency;
5091         }
5092       }
5093
5094       {
5095         double y, u, v, frame_all;
5096         frame_all = vpx_calc_fastssim(cpi->Source, cm->frame_to_show, &y, &u,
5097                                       &v, bit_depth, in_bit_depth);
5098         adjust_image_stat(y, u, v, frame_all, &cpi->fastssim);
5099       }
5100       {
5101         double y, u, v, frame_all;
5102         frame_all = vpx_psnrhvs(cpi->Source, cm->frame_to_show, &y, &u, &v,
5103                                 bit_depth, in_bit_depth);
5104         adjust_image_stat(y, u, v, frame_all, &cpi->psnrhvs);
5105       }
5106     }
5107   }
5108
5109 #endif
5110
5111   if (is_two_pass_svc(cpi)) {
5112     if (cpi->svc.encode_empty_frame_state == ENCODING) {
5113       cpi->svc.encode_empty_frame_state = ENCODED;
5114       cpi->svc.encode_intra_empty_frame = 0;
5115     }
5116
5117     if (cm->show_frame) {
5118       ++cpi->svc.spatial_layer_to_encode;
5119       if (cpi->svc.spatial_layer_to_encode >= cpi->svc.number_spatial_layers)
5120         cpi->svc.spatial_layer_to_encode = 0;
5121
5122       // May need the empty frame after an visible frame.
5123       cpi->svc.encode_empty_frame_state = NEED_TO_ENCODE;
5124     }
5125   } else if (is_one_pass_cbr_svc(cpi)) {
5126     if (cm->show_frame) {
5127       ++cpi->svc.spatial_layer_to_encode;
5128       if (cpi->svc.spatial_layer_to_encode >= cpi->svc.number_spatial_layers)
5129         cpi->svc.spatial_layer_to_encode = 0;
5130     }
5131   }
5132
5133   vpx_clear_system_state();
5134   return 0;
5135 }
5136
5137 int vp9_get_preview_raw_frame(VP9_COMP *cpi, YV12_BUFFER_CONFIG *dest,
5138                               vp9_ppflags_t *flags) {
5139   VP9_COMMON *cm = &cpi->common;
5140 #if !CONFIG_VP9_POSTPROC
5141   (void)flags;
5142 #endif
5143
5144   if (!cm->show_frame) {
5145     return -1;
5146   } else {
5147     int ret;
5148 #if CONFIG_VP9_POSTPROC
5149     ret = vp9_post_proc_frame(cm, dest, flags);
5150 #else
5151     if (cm->frame_to_show) {
5152       *dest = *cm->frame_to_show;
5153       dest->y_width = cm->width;
5154       dest->y_height = cm->height;
5155       dest->uv_width = cm->width >> cm->subsampling_x;
5156       dest->uv_height = cm->height >> cm->subsampling_y;
5157       ret = 0;
5158     } else {
5159       ret = -1;
5160     }
5161 #endif  // !CONFIG_VP9_POSTPROC
5162     vpx_clear_system_state();
5163     return ret;
5164   }
5165 }
5166
5167 int vp9_set_internal_size(VP9_COMP *cpi, VPX_SCALING horiz_mode,
5168                           VPX_SCALING vert_mode) {
5169   VP9_COMMON *cm = &cpi->common;
5170   int hr = 0, hs = 0, vr = 0, vs = 0;
5171
5172   if (horiz_mode > ONETWO || vert_mode > ONETWO) return -1;
5173
5174   Scale2Ratio(horiz_mode, &hr, &hs);
5175   Scale2Ratio(vert_mode, &vr, &vs);
5176
5177   // always go to the next whole number
5178   cm->width = (hs - 1 + cpi->oxcf.width * hr) / hs;
5179   cm->height = (vs - 1 + cpi->oxcf.height * vr) / vs;
5180   if (cm->current_video_frame) {
5181     assert(cm->width <= cpi->initial_width);
5182     assert(cm->height <= cpi->initial_height);
5183   }
5184
5185   update_frame_size(cpi);
5186
5187   return 0;
5188 }
5189
5190 int vp9_set_size_literal(VP9_COMP *cpi, unsigned int width,
5191                          unsigned int height) {
5192   VP9_COMMON *cm = &cpi->common;
5193 #if CONFIG_VP9_HIGHBITDEPTH
5194   check_initial_width(cpi, cm->use_highbitdepth, 1, 1);
5195 #else
5196   check_initial_width(cpi, 1, 1);
5197 #endif  // CONFIG_VP9_HIGHBITDEPTH
5198
5199 #if CONFIG_VP9_TEMPORAL_DENOISING
5200   setup_denoiser_buffer(cpi);
5201 #endif
5202
5203   if (width) {
5204     cm->width = width;
5205     if (cm->width > cpi->initial_width) {
5206       cm->width = cpi->initial_width;
5207       printf("Warning: Desired width too large, changed to %d\n", cm->width);
5208     }
5209   }
5210
5211   if (height) {
5212     cm->height = height;
5213     if (cm->height > cpi->initial_height) {
5214       cm->height = cpi->initial_height;
5215       printf("Warning: Desired height too large, changed to %d\n", cm->height);
5216     }
5217   }
5218   assert(cm->width <= cpi->initial_width);
5219   assert(cm->height <= cpi->initial_height);
5220
5221   update_frame_size(cpi);
5222
5223   return 0;
5224 }
5225
5226 void vp9_set_svc(VP9_COMP *cpi, int use_svc) {
5227   cpi->use_svc = use_svc;
5228   return;
5229 }
5230
5231 int vp9_get_quantizer(VP9_COMP *cpi) { return cpi->common.base_qindex; }
5232
5233 void vp9_apply_encoding_flags(VP9_COMP *cpi, vpx_enc_frame_flags_t flags) {
5234   if (flags &
5235       (VP8_EFLAG_NO_REF_LAST | VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF)) {
5236     int ref = 7;
5237
5238     if (flags & VP8_EFLAG_NO_REF_LAST) ref ^= VP9_LAST_FLAG;
5239
5240     if (flags & VP8_EFLAG_NO_REF_GF) ref ^= VP9_GOLD_FLAG;
5241
5242     if (flags & VP8_EFLAG_NO_REF_ARF) ref ^= VP9_ALT_FLAG;
5243
5244     vp9_use_as_reference(cpi, ref);
5245   }
5246
5247   if (flags &
5248       (VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF |
5249        VP8_EFLAG_FORCE_GF | VP8_EFLAG_FORCE_ARF)) {
5250     int upd = 7;
5251
5252     if (flags & VP8_EFLAG_NO_UPD_LAST) upd ^= VP9_LAST_FLAG;
5253
5254     if (flags & VP8_EFLAG_NO_UPD_GF) upd ^= VP9_GOLD_FLAG;
5255
5256     if (flags & VP8_EFLAG_NO_UPD_ARF) upd ^= VP9_ALT_FLAG;
5257
5258     vp9_update_reference(cpi, upd);
5259   }
5260
5261   if (flags & VP8_EFLAG_NO_UPD_ENTROPY) {
5262     vp9_update_entropy(cpi, 0);
5263   }
5264 }
5265
5266 void vp9_set_row_mt(VP9_COMP *cpi) {
5267   // Enable row based multi-threading for supported modes of encoding
5268   cpi->row_mt = 0;
5269   if (((cpi->oxcf.mode == GOOD || cpi->oxcf.mode == BEST) &&
5270        cpi->oxcf.speed < 5 && cpi->oxcf.pass == 1) &&
5271       cpi->oxcf.row_mt && !cpi->use_svc)
5272     cpi->row_mt = 1;
5273
5274   if (cpi->oxcf.mode == GOOD && cpi->oxcf.speed < 5 &&
5275       (cpi->oxcf.pass == 0 || cpi->oxcf.pass == 2) && cpi->oxcf.row_mt &&
5276       !cpi->use_svc)
5277     cpi->row_mt = 1;
5278
5279   // In realtime mode, enable row based multi-threading for all the speed levels
5280   // where non-rd path is used.
5281   if (cpi->oxcf.mode == REALTIME && cpi->oxcf.speed >= 5 && cpi->oxcf.row_mt) {
5282     cpi->row_mt = 1;
5283   }
5284
5285   if (cpi->row_mt && cpi->oxcf.max_threads > 1)
5286     cpi->row_mt_bit_exact = 1;
5287   else
5288     cpi->row_mt_bit_exact = 0;
5289 }