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