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