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