]> granicus.if.org Git - libvpx/blob - vp9/encoder/vp9_encoder.c
vp9_ethread: modify VP9_COMP structure
[libvpx] / vp9 / encoder / vp9_encoder.c
1 /*
2  * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10
11 #include <math.h>
12 #include <stdio.h>
13 #include <limits.h>
14
15 #include "./vpx_config.h"
16 #include "./vpx_scale_rtcd.h"
17 #include "vpx/internal/vpx_psnr.h"
18 #include "vpx_ports/vpx_timer.h"
19
20 #include "vp9/common/vp9_alloccommon.h"
21 #include "vp9/common/vp9_filter.h"
22 #include "vp9/common/vp9_idct.h"
23 #if CONFIG_VP9_POSTPROC
24 #include "vp9/common/vp9_postproc.h"
25 #endif
26 #include "vp9/common/vp9_reconinter.h"
27 #include "vp9/common/vp9_reconintra.h"
28 #include "vp9/common/vp9_systemdependent.h"
29 #include "vp9/common/vp9_tile_common.h"
30
31 #include "vp9/encoder/vp9_aq_complexity.h"
32 #include "vp9/encoder/vp9_aq_cyclicrefresh.h"
33 #include "vp9/encoder/vp9_aq_variance.h"
34 #include "vp9/encoder/vp9_bitstream.h"
35 #include "vp9/encoder/vp9_context_tree.h"
36 #include "vp9/encoder/vp9_encodeframe.h"
37 #include "vp9/encoder/vp9_encodemv.h"
38 #include "vp9/encoder/vp9_firstpass.h"
39 #include "vp9/encoder/vp9_mbgraph.h"
40 #include "vp9/encoder/vp9_encoder.h"
41 #include "vp9/encoder/vp9_picklpf.h"
42 #include "vp9/encoder/vp9_ratectrl.h"
43 #include "vp9/encoder/vp9_rd.h"
44 #include "vp9/encoder/vp9_segmentation.h"
45 #include "vp9/encoder/vp9_speed_features.h"
46 #if CONFIG_INTERNAL_STATS
47 #include "vp9/encoder/vp9_ssim.h"
48 #endif
49 #include "vp9/encoder/vp9_temporal_filter.h"
50 #include "vp9/encoder/vp9_resize.h"
51 #include "vp9/encoder/vp9_svc_layercontext.h"
52
53 void vp9_coef_tree_initialize();
54
55 #define SHARP_FILTER_QTHRESH 0          /* Q threshold for 8-tap sharp filter */
56
57 #define ALTREF_HIGH_PRECISION_MV 1      // Whether to use high precision mv
58                                          //  for altref computation.
59 #define HIGH_PRECISION_MV_QTHRESH 200   // Q threshold for high precision
60                                          // mv. Choose a very high value for
61                                          // now so that HIGH_PRECISION is always
62                                          // chosen.
63
64 // #define OUTPUT_YUV_REC
65
66 #ifdef OUTPUT_YUV_DENOISED
67 FILE *yuv_denoised_file = NULL;
68 #endif
69 #ifdef OUTPUT_YUV_REC
70 FILE *yuv_rec_file;
71 #endif
72
73 #if 0
74 FILE *framepsnr;
75 FILE *kf_list;
76 FILE *keyfile;
77 #endif
78
79 static INLINE void Scale2Ratio(VPX_SCALING mode, int *hr, int *hs) {
80   switch (mode) {
81     case NORMAL:
82       *hr = 1;
83       *hs = 1;
84       break;
85     case FOURFIVE:
86       *hr = 4;
87       *hs = 5;
88       break;
89     case THREEFIVE:
90       *hr = 3;
91       *hs = 5;
92     break;
93     case ONETWO:
94       *hr = 1;
95       *hs = 2;
96     break;
97     default:
98       *hr = 1;
99       *hs = 1;
100        assert(0);
101       break;
102   }
103 }
104
105 void vp9_set_high_precision_mv(VP9_COMP *cpi, int allow_high_precision_mv) {
106   MACROBLOCK *const mb = &cpi->td.mb;
107   cpi->common.allow_high_precision_mv = allow_high_precision_mv;
108   if (cpi->common.allow_high_precision_mv) {
109     mb->mvcost = mb->nmvcost_hp;
110     mb->mvsadcost = mb->nmvsadcost_hp;
111   } else {
112     mb->mvcost = mb->nmvcost;
113     mb->mvsadcost = mb->nmvsadcost;
114   }
115 }
116
117 static void setup_frame(VP9_COMP *cpi) {
118   VP9_COMMON *const cm = &cpi->common;
119   // Set up entropy context depending on frame type. The decoder mandates
120   // the use of the default context, index 0, for keyframes and inter
121   // frames where the error_resilient_mode or intra_only flag is set. For
122   // other inter-frames the encoder currently uses only two contexts;
123   // context 1 for ALTREF frames and context 0 for the others.
124   if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
125     vp9_setup_past_independence(cm);
126   } else {
127     if (!cpi->use_svc)
128       cm->frame_context_idx = cpi->refresh_alt_ref_frame;
129   }
130
131   if (cm->frame_type == KEY_FRAME) {
132     if (!is_two_pass_svc(cpi))
133       cpi->refresh_golden_frame = 1;
134     cpi->refresh_alt_ref_frame = 1;
135     vp9_zero(cpi->interp_filter_selected);
136   } else {
137     *cm->fc = cm->frame_contexts[cm->frame_context_idx];
138     vp9_zero(cpi->interp_filter_selected[0]);
139   }
140 }
141
142 static void vp9_enc_setup_mi(VP9_COMMON *cm) {
143   int i;
144   cm->mi = cm->mip + cm->mi_stride + 1;
145   vpx_memset(cm->mip, 0, cm->mi_stride * (cm->mi_rows + 1) * sizeof(*cm->mip));
146   cm->prev_mi = cm->prev_mip + cm->mi_stride + 1;
147   // Clear top border row
148   vpx_memset(cm->prev_mip, 0, sizeof(*cm->prev_mip) * cm->mi_stride);
149   // Clear left border column
150   for (i = 1; i < cm->mi_rows + 1; ++i)
151     vpx_memset(&cm->prev_mip[i * cm->mi_stride], 0, sizeof(*cm->prev_mip));
152 }
153
154 static int vp9_enc_alloc_mi(VP9_COMMON *cm, int mi_size) {
155   cm->mip = vpx_calloc(mi_size, sizeof(*cm->mip));
156   if (!cm->mip)
157     return 1;
158   cm->prev_mip = vpx_calloc(mi_size, sizeof(*cm->prev_mip));
159   if (!cm->prev_mip)
160     return 1;
161   cm->mi_alloc_size = mi_size;
162   return 0;
163 }
164
165 static void vp9_enc_free_mi(VP9_COMMON *cm) {
166   vpx_free(cm->mip);
167   cm->mip = NULL;
168   vpx_free(cm->prev_mip);
169   cm->prev_mip = NULL;
170 }
171
172 static void vp9_swap_mi_and_prev_mi(VP9_COMMON *cm) {
173   // Current mip will be the prev_mip for the next frame.
174   MODE_INFO *temp = cm->prev_mip;
175   cm->prev_mip = cm->mip;
176   cm->mip = temp;
177
178   // Update the upper left visible macroblock ptrs.
179   cm->mi = cm->mip + cm->mi_stride + 1;
180   cm->prev_mi = cm->prev_mip + cm->mi_stride + 1;
181 }
182
183 void vp9_initialize_enc() {
184   static int init_done = 0;
185
186   if (!init_done) {
187     vp9_rtcd();
188     vp9_init_intra_predictors();
189     vp9_coef_tree_initialize();
190     vp9_tokenize_initialize();
191     vp9_init_me_luts();
192     vp9_rc_init_minq_luts();
193     vp9_entropy_mv_init();
194     vp9_entropy_mode_init();
195     vp9_temporal_filter_init();
196     init_done = 1;
197   }
198 }
199
200 static void dealloc_compressor_data(VP9_COMP *cpi) {
201   VP9_COMMON *const cm = &cpi->common;
202   int i;
203
204   vpx_free(cpi->tile_data);
205   cpi->tile_data = NULL;
206
207   // Delete sementation map
208   vpx_free(cpi->segmentation_map);
209   cpi->segmentation_map = NULL;
210   vpx_free(cm->last_frame_seg_map);
211   cm->last_frame_seg_map = NULL;
212   vpx_free(cpi->coding_context.last_frame_seg_map_copy);
213   cpi->coding_context.last_frame_seg_map_copy = NULL;
214
215   vpx_free(cpi->complexity_map);
216   cpi->complexity_map = NULL;
217
218   vpx_free(cpi->nmvcosts[0]);
219   vpx_free(cpi->nmvcosts[1]);
220   cpi->nmvcosts[0] = NULL;
221   cpi->nmvcosts[1] = NULL;
222
223   vpx_free(cpi->nmvcosts_hp[0]);
224   vpx_free(cpi->nmvcosts_hp[1]);
225   cpi->nmvcosts_hp[0] = NULL;
226   cpi->nmvcosts_hp[1] = NULL;
227
228   vpx_free(cpi->nmvsadcosts[0]);
229   vpx_free(cpi->nmvsadcosts[1]);
230   cpi->nmvsadcosts[0] = NULL;
231   cpi->nmvsadcosts[1] = NULL;
232
233   vpx_free(cpi->nmvsadcosts_hp[0]);
234   vpx_free(cpi->nmvsadcosts_hp[1]);
235   cpi->nmvsadcosts_hp[0] = NULL;
236   cpi->nmvsadcosts_hp[1] = NULL;
237
238   vp9_cyclic_refresh_free(cpi->cyclic_refresh);
239   cpi->cyclic_refresh = NULL;
240
241   vp9_free_ref_frame_buffers(cm);
242   vp9_free_context_buffers(cm);
243
244   vp9_free_frame_buffer(&cpi->last_frame_uf);
245   vp9_free_frame_buffer(&cpi->scaled_source);
246   vp9_free_frame_buffer(&cpi->scaled_last_source);
247   vp9_free_frame_buffer(&cpi->alt_ref_buffer);
248   vp9_lookahead_destroy(cpi->lookahead);
249
250   vpx_free(cpi->tok);
251   cpi->tok = 0;
252
253   vp9_free_pc_tree(&cpi->td);
254
255   for (i = 0; i < cpi->svc.number_spatial_layers; ++i) {
256     LAYER_CONTEXT *const lc = &cpi->svc.layer_context[i];
257     vpx_free(lc->rc_twopass_stats_in.buf);
258     lc->rc_twopass_stats_in.buf = NULL;
259     lc->rc_twopass_stats_in.sz = 0;
260   }
261
262   if (cpi->source_diff_var != NULL) {
263     vpx_free(cpi->source_diff_var);
264     cpi->source_diff_var = NULL;
265   }
266
267   for (i = 0; i < MAX_LAG_BUFFERS; ++i) {
268     vp9_free_frame_buffer(&cpi->svc.scaled_frames[i]);
269   }
270   vpx_memset(&cpi->svc.scaled_frames[0], 0,
271              MAX_LAG_BUFFERS * sizeof(cpi->svc.scaled_frames[0]));
272
273   vp9_free_frame_buffer(&cpi->svc.empty_frame.img);
274   vpx_memset(&cpi->svc.empty_frame, 0, sizeof(cpi->svc.empty_frame));
275 }
276
277 static void save_coding_context(VP9_COMP *cpi) {
278   CODING_CONTEXT *const cc = &cpi->coding_context;
279   VP9_COMMON *cm = &cpi->common;
280
281   // Stores a snapshot of key state variables which can subsequently be
282   // restored with a call to vp9_restore_coding_context. These functions are
283   // intended for use in a re-code loop in vp9_compress_frame where the
284   // quantizer value is adjusted between loop iterations.
285   vp9_copy(cc->nmvjointcost,  cpi->td.mb.nmvjointcost);
286
287   vpx_memcpy(cc->nmvcosts[0], cpi->nmvcosts[0],
288              MV_VALS * sizeof(*cpi->nmvcosts[0]));
289   vpx_memcpy(cc->nmvcosts[1], cpi->nmvcosts[1],
290              MV_VALS * sizeof(*cpi->nmvcosts[1]));
291   vpx_memcpy(cc->nmvcosts_hp[0], cpi->nmvcosts_hp[0],
292              MV_VALS * sizeof(*cpi->nmvcosts_hp[0]));
293   vpx_memcpy(cc->nmvcosts_hp[1], cpi->nmvcosts_hp[1],
294              MV_VALS * sizeof(*cpi->nmvcosts_hp[1]));
295
296   vp9_copy(cc->segment_pred_probs, cm->seg.pred_probs);
297
298   vpx_memcpy(cpi->coding_context.last_frame_seg_map_copy,
299              cm->last_frame_seg_map, (cm->mi_rows * cm->mi_cols));
300
301   vp9_copy(cc->last_ref_lf_deltas, cm->lf.last_ref_deltas);
302   vp9_copy(cc->last_mode_lf_deltas, cm->lf.last_mode_deltas);
303
304   cc->fc = *cm->fc;
305 }
306
307 static void restore_coding_context(VP9_COMP *cpi) {
308   CODING_CONTEXT *const cc = &cpi->coding_context;
309   VP9_COMMON *cm = &cpi->common;
310
311   // Restore key state variables to the snapshot state stored in the
312   // previous call to vp9_save_coding_context.
313   vp9_copy(cpi->td.mb.nmvjointcost, cc->nmvjointcost);
314
315   vpx_memcpy(cpi->nmvcosts[0], cc->nmvcosts[0],
316              MV_VALS * sizeof(*cc->nmvcosts[0]));
317   vpx_memcpy(cpi->nmvcosts[1], cc->nmvcosts[1],
318              MV_VALS * sizeof(*cc->nmvcosts[1]));
319   vpx_memcpy(cpi->nmvcosts_hp[0], cc->nmvcosts_hp[0],
320              MV_VALS * sizeof(*cc->nmvcosts_hp[0]));
321   vpx_memcpy(cpi->nmvcosts_hp[1], cc->nmvcosts_hp[1],
322              MV_VALS * sizeof(*cc->nmvcosts_hp[1]));
323
324   vp9_copy(cm->seg.pred_probs, cc->segment_pred_probs);
325
326   vpx_memcpy(cm->last_frame_seg_map,
327              cpi->coding_context.last_frame_seg_map_copy,
328              (cm->mi_rows * cm->mi_cols));
329
330   vp9_copy(cm->lf.last_ref_deltas, cc->last_ref_lf_deltas);
331   vp9_copy(cm->lf.last_mode_deltas, cc->last_mode_lf_deltas);
332
333   *cm->fc = cc->fc;
334 }
335
336 static void configure_static_seg_features(VP9_COMP *cpi) {
337   VP9_COMMON *const cm = &cpi->common;
338   const RATE_CONTROL *const rc = &cpi->rc;
339   struct segmentation *const seg = &cm->seg;
340
341   int high_q = (int)(rc->avg_q > 48.0);
342   int qi_delta;
343
344   // Disable and clear down for KF
345   if (cm->frame_type == KEY_FRAME) {
346     // Clear down the global segmentation map
347     vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
348     seg->update_map = 0;
349     seg->update_data = 0;
350     cpi->static_mb_pct = 0;
351
352     // Disable segmentation
353     vp9_disable_segmentation(seg);
354
355     // Clear down the segment features.
356     vp9_clearall_segfeatures(seg);
357   } else if (cpi->refresh_alt_ref_frame) {
358     // If this is an alt ref frame
359     // Clear down the global segmentation map
360     vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
361     seg->update_map = 0;
362     seg->update_data = 0;
363     cpi->static_mb_pct = 0;
364
365     // Disable segmentation and individual segment features by default
366     vp9_disable_segmentation(seg);
367     vp9_clearall_segfeatures(seg);
368
369     // Scan frames from current to arf frame.
370     // This function re-enables segmentation if appropriate.
371     vp9_update_mbgraph_stats(cpi);
372
373     // If segmentation was enabled set those features needed for the
374     // arf itself.
375     if (seg->enabled) {
376       seg->update_map = 1;
377       seg->update_data = 1;
378
379       qi_delta = vp9_compute_qdelta(rc, rc->avg_q, rc->avg_q * 0.875,
380                                     cm->bit_depth);
381       vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta - 2);
382       vp9_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
383
384       vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
385       vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
386
387       // Where relevant assume segment data is delta data
388       seg->abs_delta = SEGMENT_DELTADATA;
389     }
390   } else if (seg->enabled) {
391     // All other frames if segmentation has been enabled
392
393     // First normal frame in a valid gf or alt ref group
394     if (rc->frames_since_golden == 0) {
395       // Set up segment features for normal frames in an arf group
396       if (rc->source_alt_ref_active) {
397         seg->update_map = 0;
398         seg->update_data = 1;
399         seg->abs_delta = SEGMENT_DELTADATA;
400
401         qi_delta = vp9_compute_qdelta(rc, rc->avg_q, rc->avg_q * 1.125,
402                                       cm->bit_depth);
403         vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta + 2);
404         vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
405
406         vp9_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
407         vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
408
409         // Segment coding disabled for compred testing
410         if (high_q || (cpi->static_mb_pct == 100)) {
411           vp9_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
412           vp9_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
413           vp9_enable_segfeature(seg, 1, SEG_LVL_SKIP);
414         }
415       } else {
416         // Disable segmentation and clear down features if alt ref
417         // is not active for this group
418
419         vp9_disable_segmentation(seg);
420
421         vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
422
423         seg->update_map = 0;
424         seg->update_data = 0;
425
426         vp9_clearall_segfeatures(seg);
427       }
428     } else if (rc->is_src_frame_alt_ref) {
429       // Special case where we are coding over the top of a previous
430       // alt ref frame.
431       // Segment coding disabled for compred testing
432
433       // Enable ref frame features for segment 0 as well
434       vp9_enable_segfeature(seg, 0, SEG_LVL_REF_FRAME);
435       vp9_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
436
437       // All mbs should use ALTREF_FRAME
438       vp9_clear_segdata(seg, 0, SEG_LVL_REF_FRAME);
439       vp9_set_segdata(seg, 0, SEG_LVL_REF_FRAME, ALTREF_FRAME);
440       vp9_clear_segdata(seg, 1, SEG_LVL_REF_FRAME);
441       vp9_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
442
443       // Skip all MBs if high Q (0,0 mv and skip coeffs)
444       if (high_q) {
445         vp9_enable_segfeature(seg, 0, SEG_LVL_SKIP);
446         vp9_enable_segfeature(seg, 1, SEG_LVL_SKIP);
447       }
448       // Enable data update
449       seg->update_data = 1;
450     } else {
451       // All other frames.
452
453       // No updates.. leave things as they are.
454       seg->update_map = 0;
455       seg->update_data = 0;
456     }
457   }
458 }
459
460 static void update_reference_segmentation_map(VP9_COMP *cpi) {
461   VP9_COMMON *const cm = &cpi->common;
462   MODE_INFO *mi_8x8_ptr = cm->mi;
463   uint8_t *cache_ptr = cm->last_frame_seg_map;
464   int row, col;
465
466   for (row = 0; row < cm->mi_rows; row++) {
467     MODE_INFO *mi_8x8 = mi_8x8_ptr;
468     uint8_t *cache = cache_ptr;
469     for (col = 0; col < cm->mi_cols; col++, mi_8x8++, cache++)
470       cache[0] = mi_8x8[0].src_mi->mbmi.segment_id;
471     mi_8x8_ptr += cm->mi_stride;
472     cache_ptr += cm->mi_cols;
473   }
474 }
475
476 static void alloc_raw_frame_buffers(VP9_COMP *cpi) {
477   VP9_COMMON *cm = &cpi->common;
478   const VP9EncoderConfig *oxcf = &cpi->oxcf;
479
480   cpi->lookahead = vp9_lookahead_init(oxcf->width, oxcf->height,
481                                       cm->subsampling_x, cm->subsampling_y,
482 #if CONFIG_VP9_HIGHBITDEPTH
483                                       cm->use_highbitdepth,
484 #endif
485                                       oxcf->lag_in_frames);
486   if (!cpi->lookahead)
487     vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
488                        "Failed to allocate lag buffers");
489
490   if (vp9_realloc_frame_buffer(&cpi->alt_ref_buffer,
491                                oxcf->width, oxcf->height,
492                                cm->subsampling_x, cm->subsampling_y,
493 #if CONFIG_VP9_HIGHBITDEPTH
494                                cm->use_highbitdepth,
495 #endif
496                                VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL))
497     vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
498                        "Failed to allocate altref buffer");
499 }
500
501 static void alloc_ref_frame_buffers(VP9_COMP *cpi) {
502   VP9_COMMON *const cm = &cpi->common;
503   if (vp9_alloc_ref_frame_buffers(cm, cm->width, cm->height))
504     vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
505                        "Failed to allocate frame buffers");
506 }
507
508 static void alloc_util_frame_buffers(VP9_COMP *cpi) {
509   VP9_COMMON *const cm = &cpi->common;
510   if (vp9_realloc_frame_buffer(&cpi->last_frame_uf,
511                                cm->width, cm->height,
512                                cm->subsampling_x, cm->subsampling_y,
513 #if CONFIG_VP9_HIGHBITDEPTH
514                                cm->use_highbitdepth,
515 #endif
516                                VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL))
517     vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
518                        "Failed to allocate last frame buffer");
519
520   if (vp9_realloc_frame_buffer(&cpi->scaled_source,
521                                cm->width, cm->height,
522                                cm->subsampling_x, cm->subsampling_y,
523 #if CONFIG_VP9_HIGHBITDEPTH
524                                cm->use_highbitdepth,
525 #endif
526                                VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL))
527     vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
528                        "Failed to allocate scaled source buffer");
529
530   if (vp9_realloc_frame_buffer(&cpi->scaled_last_source,
531                                cm->width, cm->height,
532                                cm->subsampling_x, cm->subsampling_y,
533 #if CONFIG_VP9_HIGHBITDEPTH
534                                cm->use_highbitdepth,
535 #endif
536                                VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL))
537     vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
538                        "Failed to allocate scaled last source buffer");
539 }
540
541 void vp9_alloc_compressor_data(VP9_COMP *cpi) {
542   VP9_COMMON *cm = &cpi->common;
543
544   vp9_alloc_context_buffers(cm, cm->width, cm->height);
545
546   vpx_free(cpi->tok);
547
548   {
549     unsigned int tokens = get_token_alloc(cm->mb_rows, cm->mb_cols);
550     CHECK_MEM_ERROR(cm, cpi->tok, vpx_calloc(tokens, sizeof(*cpi->tok)));
551   }
552
553   vp9_setup_pc_tree(&cpi->common, &cpi->td);
554 }
555
556 static void update_frame_size(VP9_COMP *cpi) {
557   VP9_COMMON *const cm = &cpi->common;
558   MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
559
560   vp9_set_mb_mi(cm, cm->width, cm->height);
561   vp9_init_context_buffers(cm);
562   init_macroblockd(cm, xd);
563
564   if (is_two_pass_svc(cpi)) {
565     if (vp9_realloc_frame_buffer(&cpi->alt_ref_buffer,
566                                  cm->width, cm->height,
567                                  cm->subsampling_x, cm->subsampling_y,
568 #if CONFIG_VP9_HIGHBITDEPTH
569                                  cm->use_highbitdepth,
570 #endif
571                                  VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL))
572       vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
573                          "Failed to reallocate alt_ref_buffer");
574   }
575 }
576
577 void vp9_new_framerate(VP9_COMP *cpi, double framerate) {
578   cpi->framerate = framerate < 0.1 ? 30 : framerate;
579   vp9_rc_update_framerate(cpi);
580 }
581
582 static void set_tile_limits(VP9_COMP *cpi) {
583   VP9_COMMON *const cm = &cpi->common;
584
585   int min_log2_tile_cols, max_log2_tile_cols;
586   vp9_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
587
588   cm->log2_tile_cols = clamp(cpi->oxcf.tile_columns,
589                              min_log2_tile_cols, max_log2_tile_cols);
590   cm->log2_tile_rows = cpi->oxcf.tile_rows;
591 }
592
593 static void init_buffer_indices(VP9_COMP *cpi) {
594   cpi->lst_fb_idx = 0;
595   cpi->gld_fb_idx = 1;
596   cpi->alt_fb_idx = 2;
597 }
598
599 static void init_config(struct VP9_COMP *cpi, VP9EncoderConfig *oxcf) {
600   VP9_COMMON *const cm = &cpi->common;
601
602   cpi->oxcf = *oxcf;
603   cpi->framerate = oxcf->init_framerate;
604
605   cm->profile = oxcf->profile;
606   cm->bit_depth = oxcf->bit_depth;
607 #if CONFIG_VP9_HIGHBITDEPTH
608   cm->use_highbitdepth = oxcf->use_highbitdepth;
609 #endif
610   cm->color_space = UNKNOWN;
611
612   cm->width = oxcf->width;
613   cm->height = oxcf->height;
614   vp9_alloc_compressor_data(cpi);
615
616   // Single thread case: use counts in common.
617   cpi->td.counts = &cm->counts;
618
619   // Spatial scalability.
620   cpi->svc.number_spatial_layers = oxcf->ss_number_layers;
621   // Temporal scalability.
622   cpi->svc.number_temporal_layers = oxcf->ts_number_layers;
623
624   if ((cpi->svc.number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) ||
625       ((cpi->svc.number_temporal_layers > 1 ||
626         cpi->svc.number_spatial_layers > 1) &&
627        cpi->oxcf.pass != 1)) {
628     vp9_init_layer_context(cpi);
629   }
630
631   // change includes all joint functionality
632   vp9_change_config(cpi, oxcf);
633
634   cpi->static_mb_pct = 0;
635   cpi->ref_frame_flags = 0;
636
637   init_buffer_indices(cpi);
638 }
639
640 static void set_rc_buffer_sizes(RATE_CONTROL *rc,
641                                 const VP9EncoderConfig *oxcf) {
642   const int64_t bandwidth = oxcf->target_bandwidth;
643   const int64_t starting = oxcf->starting_buffer_level_ms;
644   const int64_t optimal = oxcf->optimal_buffer_level_ms;
645   const int64_t maximum = oxcf->maximum_buffer_size_ms;
646
647   rc->starting_buffer_level = starting * bandwidth / 1000;
648   rc->optimal_buffer_level = (optimal == 0) ? bandwidth / 8
649                                             : optimal * bandwidth / 1000;
650   rc->maximum_buffer_size = (maximum == 0) ? bandwidth / 8
651                                            : maximum * bandwidth / 1000;
652 }
653
654 #if CONFIG_VP9_HIGHBITDEPTH
655 #define HIGHBD_BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX3F, SDX8F, SDX4DF) \
656     cpi->fn_ptr[BT].sdf = SDF; \
657     cpi->fn_ptr[BT].sdaf = SDAF; \
658     cpi->fn_ptr[BT].vf = VF; \
659     cpi->fn_ptr[BT].svf = SVF; \
660     cpi->fn_ptr[BT].svaf = SVAF; \
661     cpi->fn_ptr[BT].sdx3f = SDX3F; \
662     cpi->fn_ptr[BT].sdx8f = SDX8F; \
663     cpi->fn_ptr[BT].sdx4df = SDX4DF;
664
665 #define MAKE_BFP_SAD_WRAPPER(fnname) \
666 static unsigned int fnname##_bits8(const uint8_t *src_ptr, \
667                                    int source_stride, \
668                                    const uint8_t *ref_ptr, \
669                                    int ref_stride) {  \
670   return fnname(src_ptr, source_stride, ref_ptr, ref_stride); \
671 } \
672 static unsigned int fnname##_bits10(const uint8_t *src_ptr, \
673                                     int source_stride, \
674                                     const uint8_t *ref_ptr, \
675                                     int ref_stride) {  \
676   return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 2; \
677 } \
678 static unsigned int fnname##_bits12(const uint8_t *src_ptr, \
679                                     int source_stride, \
680                                     const uint8_t *ref_ptr, \
681                                     int ref_stride) {  \
682   return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 4; \
683 }
684
685 #define MAKE_BFP_SADAVG_WRAPPER(fnname) static unsigned int \
686 fnname##_bits8(const uint8_t *src_ptr, \
687                int source_stride, \
688                const uint8_t *ref_ptr, \
689                int ref_stride, \
690                const uint8_t *second_pred) {  \
691   return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred); \
692 } \
693 static unsigned int fnname##_bits10(const uint8_t *src_ptr, \
694                                     int source_stride, \
695                                     const uint8_t *ref_ptr, \
696                                     int ref_stride, \
697                                     const uint8_t *second_pred) {  \
698   return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
699                 second_pred) >> 2; \
700 } \
701 static unsigned int fnname##_bits12(const uint8_t *src_ptr, \
702                                     int source_stride, \
703                                     const uint8_t *ref_ptr, \
704                                     int ref_stride, \
705                                     const uint8_t *second_pred) {  \
706   return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
707                 second_pred) >> 4; \
708 }
709
710 #define MAKE_BFP_SAD3_WRAPPER(fnname) \
711 static void fnname##_bits8(const uint8_t *src_ptr, \
712                            int source_stride, \
713                            const uint8_t *ref_ptr, \
714                            int  ref_stride, \
715                            unsigned int *sad_array) {  \
716   fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
717 } \
718 static void fnname##_bits10(const uint8_t *src_ptr, \
719                             int source_stride, \
720                             const uint8_t *ref_ptr, \
721                             int  ref_stride, \
722                             unsigned int *sad_array) {  \
723   int i; \
724   fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
725   for (i = 0; i < 3; i++) \
726     sad_array[i] >>= 2; \
727 } \
728 static void fnname##_bits12(const uint8_t *src_ptr, \
729                             int source_stride, \
730                             const uint8_t *ref_ptr, \
731                             int  ref_stride, \
732                             unsigned int *sad_array) {  \
733   int i; \
734   fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
735   for (i = 0; i < 3; i++) \
736     sad_array[i] >>= 4; \
737 }
738
739 #define MAKE_BFP_SAD8_WRAPPER(fnname) \
740 static void fnname##_bits8(const uint8_t *src_ptr, \
741                            int source_stride, \
742                            const uint8_t *ref_ptr, \
743                            int  ref_stride, \
744                            unsigned int *sad_array) {  \
745   fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
746 } \
747 static void fnname##_bits10(const uint8_t *src_ptr, \
748                             int source_stride, \
749                             const uint8_t *ref_ptr, \
750                             int  ref_stride, \
751                             unsigned int *sad_array) {  \
752   int i; \
753   fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
754   for (i = 0; i < 8; i++) \
755     sad_array[i] >>= 2; \
756 } \
757 static void fnname##_bits12(const uint8_t *src_ptr, \
758                             int source_stride, \
759                             const uint8_t *ref_ptr, \
760                             int  ref_stride, \
761                             unsigned int *sad_array) {  \
762   int i; \
763   fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
764   for (i = 0; i < 8; i++) \
765     sad_array[i] >>= 4; \
766 }
767 #define MAKE_BFP_SAD4D_WRAPPER(fnname) \
768 static void fnname##_bits8(const uint8_t *src_ptr, \
769                            int source_stride, \
770                            const uint8_t* const ref_ptr[], \
771                            int  ref_stride, \
772                            unsigned int *sad_array) {  \
773   fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
774 } \
775 static void fnname##_bits10(const uint8_t *src_ptr, \
776                             int source_stride, \
777                             const uint8_t* const ref_ptr[], \
778                             int  ref_stride, \
779                             unsigned int *sad_array) {  \
780   int i; \
781   fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
782   for (i = 0; i < 4; i++) \
783   sad_array[i] >>= 2; \
784 } \
785 static void fnname##_bits12(const uint8_t *src_ptr, \
786                             int source_stride, \
787                             const uint8_t* const ref_ptr[], \
788                             int  ref_stride, \
789                             unsigned int *sad_array) {  \
790   int i; \
791   fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
792   for (i = 0; i < 4; i++) \
793   sad_array[i] >>= 4; \
794 }
795
796 MAKE_BFP_SAD_WRAPPER(vp9_highbd_sad32x16)
797 MAKE_BFP_SADAVG_WRAPPER(vp9_highbd_sad32x16_avg)
798 MAKE_BFP_SAD4D_WRAPPER(vp9_highbd_sad32x16x4d)
799 MAKE_BFP_SAD_WRAPPER(vp9_highbd_sad16x32)
800 MAKE_BFP_SADAVG_WRAPPER(vp9_highbd_sad16x32_avg)
801 MAKE_BFP_SAD4D_WRAPPER(vp9_highbd_sad16x32x4d)
802 MAKE_BFP_SAD_WRAPPER(vp9_highbd_sad64x32)
803 MAKE_BFP_SADAVG_WRAPPER(vp9_highbd_sad64x32_avg)
804 MAKE_BFP_SAD4D_WRAPPER(vp9_highbd_sad64x32x4d)
805 MAKE_BFP_SAD_WRAPPER(vp9_highbd_sad32x64)
806 MAKE_BFP_SADAVG_WRAPPER(vp9_highbd_sad32x64_avg)
807 MAKE_BFP_SAD4D_WRAPPER(vp9_highbd_sad32x64x4d)
808 MAKE_BFP_SAD_WRAPPER(vp9_highbd_sad32x32)
809 MAKE_BFP_SADAVG_WRAPPER(vp9_highbd_sad32x32_avg)
810 MAKE_BFP_SAD3_WRAPPER(vp9_highbd_sad32x32x3)
811 MAKE_BFP_SAD8_WRAPPER(vp9_highbd_sad32x32x8)
812 MAKE_BFP_SAD4D_WRAPPER(vp9_highbd_sad32x32x4d)
813 MAKE_BFP_SAD_WRAPPER(vp9_highbd_sad64x64)
814 MAKE_BFP_SADAVG_WRAPPER(vp9_highbd_sad64x64_avg)
815 MAKE_BFP_SAD3_WRAPPER(vp9_highbd_sad64x64x3)
816 MAKE_BFP_SAD8_WRAPPER(vp9_highbd_sad64x64x8)
817 MAKE_BFP_SAD4D_WRAPPER(vp9_highbd_sad64x64x4d)
818 MAKE_BFP_SAD_WRAPPER(vp9_highbd_sad16x16)
819 MAKE_BFP_SADAVG_WRAPPER(vp9_highbd_sad16x16_avg)
820 MAKE_BFP_SAD3_WRAPPER(vp9_highbd_sad16x16x3)
821 MAKE_BFP_SAD8_WRAPPER(vp9_highbd_sad16x16x8)
822 MAKE_BFP_SAD4D_WRAPPER(vp9_highbd_sad16x16x4d)
823 MAKE_BFP_SAD_WRAPPER(vp9_highbd_sad16x8)
824 MAKE_BFP_SADAVG_WRAPPER(vp9_highbd_sad16x8_avg)
825 MAKE_BFP_SAD3_WRAPPER(vp9_highbd_sad16x8x3)
826 MAKE_BFP_SAD8_WRAPPER(vp9_highbd_sad16x8x8)
827 MAKE_BFP_SAD4D_WRAPPER(vp9_highbd_sad16x8x4d)
828 MAKE_BFP_SAD_WRAPPER(vp9_highbd_sad8x16)
829 MAKE_BFP_SADAVG_WRAPPER(vp9_highbd_sad8x16_avg)
830 MAKE_BFP_SAD3_WRAPPER(vp9_highbd_sad8x16x3)
831 MAKE_BFP_SAD8_WRAPPER(vp9_highbd_sad8x16x8)
832 MAKE_BFP_SAD4D_WRAPPER(vp9_highbd_sad8x16x4d)
833 MAKE_BFP_SAD_WRAPPER(vp9_highbd_sad8x8)
834 MAKE_BFP_SADAVG_WRAPPER(vp9_highbd_sad8x8_avg)
835 MAKE_BFP_SAD3_WRAPPER(vp9_highbd_sad8x8x3)
836 MAKE_BFP_SAD8_WRAPPER(vp9_highbd_sad8x8x8)
837 MAKE_BFP_SAD4D_WRAPPER(vp9_highbd_sad8x8x4d)
838 MAKE_BFP_SAD_WRAPPER(vp9_highbd_sad8x4)
839 MAKE_BFP_SADAVG_WRAPPER(vp9_highbd_sad8x4_avg)
840 MAKE_BFP_SAD8_WRAPPER(vp9_highbd_sad8x4x8)
841 MAKE_BFP_SAD4D_WRAPPER(vp9_highbd_sad8x4x4d)
842 MAKE_BFP_SAD_WRAPPER(vp9_highbd_sad4x8)
843 MAKE_BFP_SADAVG_WRAPPER(vp9_highbd_sad4x8_avg)
844 MAKE_BFP_SAD8_WRAPPER(vp9_highbd_sad4x8x8)
845 MAKE_BFP_SAD4D_WRAPPER(vp9_highbd_sad4x8x4d)
846 MAKE_BFP_SAD_WRAPPER(vp9_highbd_sad4x4)
847 MAKE_BFP_SADAVG_WRAPPER(vp9_highbd_sad4x4_avg)
848 MAKE_BFP_SAD3_WRAPPER(vp9_highbd_sad4x4x3)
849 MAKE_BFP_SAD8_WRAPPER(vp9_highbd_sad4x4x8)
850 MAKE_BFP_SAD4D_WRAPPER(vp9_highbd_sad4x4x4d)
851
852 static void  highbd_set_var_fns(VP9_COMP *const cpi) {
853   VP9_COMMON *const cm = &cpi->common;
854   if (cm->use_highbitdepth) {
855     switch (cm->bit_depth) {
856       case VPX_BITS_8:
857         HIGHBD_BFP(BLOCK_32X16,
858                    vp9_highbd_sad32x16_bits8,
859                    vp9_highbd_sad32x16_avg_bits8,
860                    vp9_highbd_variance32x16,
861                    vp9_highbd_sub_pixel_variance32x16,
862                    vp9_highbd_sub_pixel_avg_variance32x16,
863                    NULL,
864                    NULL,
865                    vp9_highbd_sad32x16x4d_bits8)
866
867         HIGHBD_BFP(BLOCK_16X32,
868                    vp9_highbd_sad16x32_bits8,
869                    vp9_highbd_sad16x32_avg_bits8,
870                    vp9_highbd_variance16x32,
871                    vp9_highbd_sub_pixel_variance16x32,
872                    vp9_highbd_sub_pixel_avg_variance16x32,
873                    NULL,
874                    NULL,
875                    vp9_highbd_sad16x32x4d_bits8)
876
877         HIGHBD_BFP(BLOCK_64X32,
878                    vp9_highbd_sad64x32_bits8,
879                    vp9_highbd_sad64x32_avg_bits8,
880                    vp9_highbd_variance64x32,
881                    vp9_highbd_sub_pixel_variance64x32,
882                    vp9_highbd_sub_pixel_avg_variance64x32,
883                    NULL,
884                    NULL,
885                    vp9_highbd_sad64x32x4d_bits8)
886
887         HIGHBD_BFP(BLOCK_32X64,
888                    vp9_highbd_sad32x64_bits8,
889                    vp9_highbd_sad32x64_avg_bits8,
890                    vp9_highbd_variance32x64,
891                    vp9_highbd_sub_pixel_variance32x64,
892                    vp9_highbd_sub_pixel_avg_variance32x64,
893                    NULL,
894                    NULL,
895                    vp9_highbd_sad32x64x4d_bits8)
896
897         HIGHBD_BFP(BLOCK_32X32,
898                    vp9_highbd_sad32x32_bits8,
899                    vp9_highbd_sad32x32_avg_bits8,
900                    vp9_highbd_variance32x32,
901                    vp9_highbd_sub_pixel_variance32x32,
902                    vp9_highbd_sub_pixel_avg_variance32x32,
903                    vp9_highbd_sad32x32x3_bits8,
904                    vp9_highbd_sad32x32x8_bits8,
905                    vp9_highbd_sad32x32x4d_bits8)
906
907         HIGHBD_BFP(BLOCK_64X64,
908                    vp9_highbd_sad64x64_bits8,
909                    vp9_highbd_sad64x64_avg_bits8,
910                    vp9_highbd_variance64x64,
911                    vp9_highbd_sub_pixel_variance64x64,
912                    vp9_highbd_sub_pixel_avg_variance64x64,
913                    vp9_highbd_sad64x64x3_bits8,
914                    vp9_highbd_sad64x64x8_bits8,
915                    vp9_highbd_sad64x64x4d_bits8)
916
917         HIGHBD_BFP(BLOCK_16X16,
918                    vp9_highbd_sad16x16_bits8,
919                    vp9_highbd_sad16x16_avg_bits8,
920                    vp9_highbd_variance16x16,
921                    vp9_highbd_sub_pixel_variance16x16,
922                    vp9_highbd_sub_pixel_avg_variance16x16,
923                    vp9_highbd_sad16x16x3_bits8,
924                    vp9_highbd_sad16x16x8_bits8,
925                    vp9_highbd_sad16x16x4d_bits8)
926
927         HIGHBD_BFP(BLOCK_16X8,
928                    vp9_highbd_sad16x8_bits8,
929                    vp9_highbd_sad16x8_avg_bits8,
930                    vp9_highbd_variance16x8,
931                    vp9_highbd_sub_pixel_variance16x8,
932                    vp9_highbd_sub_pixel_avg_variance16x8,
933                    vp9_highbd_sad16x8x3_bits8,
934                    vp9_highbd_sad16x8x8_bits8,
935                    vp9_highbd_sad16x8x4d_bits8)
936
937         HIGHBD_BFP(BLOCK_8X16,
938                    vp9_highbd_sad8x16_bits8,
939                    vp9_highbd_sad8x16_avg_bits8,
940                    vp9_highbd_variance8x16,
941                    vp9_highbd_sub_pixel_variance8x16,
942                    vp9_highbd_sub_pixel_avg_variance8x16,
943                    vp9_highbd_sad8x16x3_bits8,
944                    vp9_highbd_sad8x16x8_bits8,
945                    vp9_highbd_sad8x16x4d_bits8)
946
947         HIGHBD_BFP(BLOCK_8X8,
948                    vp9_highbd_sad8x8_bits8,
949                    vp9_highbd_sad8x8_avg_bits8,
950                    vp9_highbd_variance8x8,
951                    vp9_highbd_sub_pixel_variance8x8,
952                    vp9_highbd_sub_pixel_avg_variance8x8,
953                    vp9_highbd_sad8x8x3_bits8,
954                    vp9_highbd_sad8x8x8_bits8,
955                    vp9_highbd_sad8x8x4d_bits8)
956
957         HIGHBD_BFP(BLOCK_8X4,
958                    vp9_highbd_sad8x4_bits8,
959                    vp9_highbd_sad8x4_avg_bits8,
960                    vp9_highbd_variance8x4,
961                    vp9_highbd_sub_pixel_variance8x4,
962                    vp9_highbd_sub_pixel_avg_variance8x4,
963                    NULL,
964                    vp9_highbd_sad8x4x8_bits8,
965                    vp9_highbd_sad8x4x4d_bits8)
966
967         HIGHBD_BFP(BLOCK_4X8,
968                    vp9_highbd_sad4x8_bits8,
969                    vp9_highbd_sad4x8_avg_bits8,
970                    vp9_highbd_variance4x8,
971                    vp9_highbd_sub_pixel_variance4x8,
972                    vp9_highbd_sub_pixel_avg_variance4x8,
973                    NULL,
974                    vp9_highbd_sad4x8x8_bits8,
975                    vp9_highbd_sad4x8x4d_bits8)
976
977         HIGHBD_BFP(BLOCK_4X4,
978                    vp9_highbd_sad4x4_bits8,
979                    vp9_highbd_sad4x4_avg_bits8,
980                    vp9_highbd_variance4x4,
981                    vp9_highbd_sub_pixel_variance4x4,
982                    vp9_highbd_sub_pixel_avg_variance4x4,
983                    vp9_highbd_sad4x4x3_bits8,
984                    vp9_highbd_sad4x4x8_bits8,
985                    vp9_highbd_sad4x4x4d_bits8)
986         break;
987
988       case VPX_BITS_10:
989         HIGHBD_BFP(BLOCK_32X16,
990                    vp9_highbd_sad32x16_bits10,
991                    vp9_highbd_sad32x16_avg_bits10,
992                    vp9_highbd_10_variance32x16,
993                    vp9_highbd_10_sub_pixel_variance32x16,
994                    vp9_highbd_10_sub_pixel_avg_variance32x16,
995                    NULL,
996                    NULL,
997                    vp9_highbd_sad32x16x4d_bits10)
998
999         HIGHBD_BFP(BLOCK_16X32,
1000                    vp9_highbd_sad16x32_bits10,
1001                    vp9_highbd_sad16x32_avg_bits10,
1002                    vp9_highbd_10_variance16x32,
1003                    vp9_highbd_10_sub_pixel_variance16x32,
1004                    vp9_highbd_10_sub_pixel_avg_variance16x32,
1005                    NULL,
1006                    NULL,
1007                    vp9_highbd_sad16x32x4d_bits10)
1008
1009         HIGHBD_BFP(BLOCK_64X32,
1010                    vp9_highbd_sad64x32_bits10,
1011                    vp9_highbd_sad64x32_avg_bits10,
1012                    vp9_highbd_10_variance64x32,
1013                    vp9_highbd_10_sub_pixel_variance64x32,
1014                    vp9_highbd_10_sub_pixel_avg_variance64x32,
1015                    NULL,
1016                    NULL,
1017                    vp9_highbd_sad64x32x4d_bits10)
1018
1019         HIGHBD_BFP(BLOCK_32X64,
1020                    vp9_highbd_sad32x64_bits10,
1021                    vp9_highbd_sad32x64_avg_bits10,
1022                    vp9_highbd_10_variance32x64,
1023                    vp9_highbd_10_sub_pixel_variance32x64,
1024                    vp9_highbd_10_sub_pixel_avg_variance32x64,
1025                    NULL,
1026                    NULL,
1027                    vp9_highbd_sad32x64x4d_bits10)
1028
1029         HIGHBD_BFP(BLOCK_32X32,
1030                    vp9_highbd_sad32x32_bits10,
1031                    vp9_highbd_sad32x32_avg_bits10,
1032                    vp9_highbd_10_variance32x32,
1033                    vp9_highbd_10_sub_pixel_variance32x32,
1034                    vp9_highbd_10_sub_pixel_avg_variance32x32,
1035                    vp9_highbd_sad32x32x3_bits10,
1036                    vp9_highbd_sad32x32x8_bits10,
1037                    vp9_highbd_sad32x32x4d_bits10)
1038
1039         HIGHBD_BFP(BLOCK_64X64,
1040                    vp9_highbd_sad64x64_bits10,
1041                    vp9_highbd_sad64x64_avg_bits10,
1042                    vp9_highbd_10_variance64x64,
1043                    vp9_highbd_10_sub_pixel_variance64x64,
1044                    vp9_highbd_10_sub_pixel_avg_variance64x64,
1045                    vp9_highbd_sad64x64x3_bits10,
1046                    vp9_highbd_sad64x64x8_bits10,
1047                    vp9_highbd_sad64x64x4d_bits10)
1048
1049         HIGHBD_BFP(BLOCK_16X16,
1050                    vp9_highbd_sad16x16_bits10,
1051                    vp9_highbd_sad16x16_avg_bits10,
1052                    vp9_highbd_10_variance16x16,
1053                    vp9_highbd_10_sub_pixel_variance16x16,
1054                    vp9_highbd_10_sub_pixel_avg_variance16x16,
1055                    vp9_highbd_sad16x16x3_bits10,
1056                    vp9_highbd_sad16x16x8_bits10,
1057                    vp9_highbd_sad16x16x4d_bits10)
1058
1059         HIGHBD_BFP(BLOCK_16X8,
1060                    vp9_highbd_sad16x8_bits10,
1061                    vp9_highbd_sad16x8_avg_bits10,
1062                    vp9_highbd_10_variance16x8,
1063                    vp9_highbd_10_sub_pixel_variance16x8,
1064                    vp9_highbd_10_sub_pixel_avg_variance16x8,
1065                    vp9_highbd_sad16x8x3_bits10,
1066                    vp9_highbd_sad16x8x8_bits10,
1067                    vp9_highbd_sad16x8x4d_bits10)
1068
1069         HIGHBD_BFP(BLOCK_8X16,
1070                    vp9_highbd_sad8x16_bits10,
1071                    vp9_highbd_sad8x16_avg_bits10,
1072                    vp9_highbd_10_variance8x16,
1073                    vp9_highbd_10_sub_pixel_variance8x16,
1074                    vp9_highbd_10_sub_pixel_avg_variance8x16,
1075                    vp9_highbd_sad8x16x3_bits10,
1076                    vp9_highbd_sad8x16x8_bits10,
1077                    vp9_highbd_sad8x16x4d_bits10)
1078
1079         HIGHBD_BFP(BLOCK_8X8,
1080                    vp9_highbd_sad8x8_bits10,
1081                    vp9_highbd_sad8x8_avg_bits10,
1082                    vp9_highbd_10_variance8x8,
1083                    vp9_highbd_10_sub_pixel_variance8x8,
1084                    vp9_highbd_10_sub_pixel_avg_variance8x8,
1085                    vp9_highbd_sad8x8x3_bits10,
1086                    vp9_highbd_sad8x8x8_bits10,
1087                    vp9_highbd_sad8x8x4d_bits10)
1088
1089         HIGHBD_BFP(BLOCK_8X4,
1090                    vp9_highbd_sad8x4_bits10,
1091                    vp9_highbd_sad8x4_avg_bits10,
1092                    vp9_highbd_10_variance8x4,
1093                    vp9_highbd_10_sub_pixel_variance8x4,
1094                    vp9_highbd_10_sub_pixel_avg_variance8x4,
1095                    NULL,
1096                    vp9_highbd_sad8x4x8_bits10,
1097                    vp9_highbd_sad8x4x4d_bits10)
1098
1099         HIGHBD_BFP(BLOCK_4X8,
1100                    vp9_highbd_sad4x8_bits10,
1101                    vp9_highbd_sad4x8_avg_bits10,
1102                    vp9_highbd_10_variance4x8,
1103                    vp9_highbd_10_sub_pixel_variance4x8,
1104                    vp9_highbd_10_sub_pixel_avg_variance4x8,
1105                    NULL,
1106                    vp9_highbd_sad4x8x8_bits10,
1107                    vp9_highbd_sad4x8x4d_bits10)
1108
1109         HIGHBD_BFP(BLOCK_4X4,
1110                    vp9_highbd_sad4x4_bits10,
1111                    vp9_highbd_sad4x4_avg_bits10,
1112                    vp9_highbd_10_variance4x4,
1113                    vp9_highbd_10_sub_pixel_variance4x4,
1114                    vp9_highbd_10_sub_pixel_avg_variance4x4,
1115                    vp9_highbd_sad4x4x3_bits10,
1116                    vp9_highbd_sad4x4x8_bits10,
1117                    vp9_highbd_sad4x4x4d_bits10)
1118         break;
1119
1120       case VPX_BITS_12:
1121         HIGHBD_BFP(BLOCK_32X16,
1122                    vp9_highbd_sad32x16_bits12,
1123                    vp9_highbd_sad32x16_avg_bits12,
1124                    vp9_highbd_12_variance32x16,
1125                    vp9_highbd_12_sub_pixel_variance32x16,
1126                    vp9_highbd_12_sub_pixel_avg_variance32x16,
1127                    NULL,
1128                    NULL,
1129                    vp9_highbd_sad32x16x4d_bits12)
1130
1131         HIGHBD_BFP(BLOCK_16X32,
1132                    vp9_highbd_sad16x32_bits12,
1133                    vp9_highbd_sad16x32_avg_bits12,
1134                    vp9_highbd_12_variance16x32,
1135                    vp9_highbd_12_sub_pixel_variance16x32,
1136                    vp9_highbd_12_sub_pixel_avg_variance16x32,
1137                    NULL,
1138                    NULL,
1139                    vp9_highbd_sad16x32x4d_bits12)
1140
1141         HIGHBD_BFP(BLOCK_64X32,
1142                    vp9_highbd_sad64x32_bits12,
1143                    vp9_highbd_sad64x32_avg_bits12,
1144                    vp9_highbd_12_variance64x32,
1145                    vp9_highbd_12_sub_pixel_variance64x32,
1146                    vp9_highbd_12_sub_pixel_avg_variance64x32,
1147                    NULL,
1148                    NULL,
1149                    vp9_highbd_sad64x32x4d_bits12)
1150
1151         HIGHBD_BFP(BLOCK_32X64,
1152                    vp9_highbd_sad32x64_bits12,
1153                    vp9_highbd_sad32x64_avg_bits12,
1154                    vp9_highbd_12_variance32x64,
1155                    vp9_highbd_12_sub_pixel_variance32x64,
1156                    vp9_highbd_12_sub_pixel_avg_variance32x64,
1157                    NULL,
1158                    NULL,
1159                    vp9_highbd_sad32x64x4d_bits12)
1160
1161         HIGHBD_BFP(BLOCK_32X32,
1162                    vp9_highbd_sad32x32_bits12,
1163                    vp9_highbd_sad32x32_avg_bits12,
1164                    vp9_highbd_12_variance32x32,
1165                    vp9_highbd_12_sub_pixel_variance32x32,
1166                    vp9_highbd_12_sub_pixel_avg_variance32x32,
1167                    vp9_highbd_sad32x32x3_bits12,
1168                    vp9_highbd_sad32x32x8_bits12,
1169                    vp9_highbd_sad32x32x4d_bits12)
1170
1171         HIGHBD_BFP(BLOCK_64X64,
1172                    vp9_highbd_sad64x64_bits12,
1173                    vp9_highbd_sad64x64_avg_bits12,
1174                    vp9_highbd_12_variance64x64,
1175                    vp9_highbd_12_sub_pixel_variance64x64,
1176                    vp9_highbd_12_sub_pixel_avg_variance64x64,
1177                    vp9_highbd_sad64x64x3_bits12,
1178                    vp9_highbd_sad64x64x8_bits12,
1179                    vp9_highbd_sad64x64x4d_bits12)
1180
1181         HIGHBD_BFP(BLOCK_16X16,
1182                    vp9_highbd_sad16x16_bits12,
1183                    vp9_highbd_sad16x16_avg_bits12,
1184                    vp9_highbd_12_variance16x16,
1185                    vp9_highbd_12_sub_pixel_variance16x16,
1186                    vp9_highbd_12_sub_pixel_avg_variance16x16,
1187                    vp9_highbd_sad16x16x3_bits12,
1188                    vp9_highbd_sad16x16x8_bits12,
1189                    vp9_highbd_sad16x16x4d_bits12)
1190
1191         HIGHBD_BFP(BLOCK_16X8,
1192                    vp9_highbd_sad16x8_bits12,
1193                    vp9_highbd_sad16x8_avg_bits12,
1194                    vp9_highbd_12_variance16x8,
1195                    vp9_highbd_12_sub_pixel_variance16x8,
1196                    vp9_highbd_12_sub_pixel_avg_variance16x8,
1197                    vp9_highbd_sad16x8x3_bits12,
1198                    vp9_highbd_sad16x8x8_bits12,
1199                    vp9_highbd_sad16x8x4d_bits12)
1200
1201         HIGHBD_BFP(BLOCK_8X16,
1202                    vp9_highbd_sad8x16_bits12,
1203                    vp9_highbd_sad8x16_avg_bits12,
1204                    vp9_highbd_12_variance8x16,
1205                    vp9_highbd_12_sub_pixel_variance8x16,
1206                    vp9_highbd_12_sub_pixel_avg_variance8x16,
1207                    vp9_highbd_sad8x16x3_bits12,
1208                    vp9_highbd_sad8x16x8_bits12,
1209                    vp9_highbd_sad8x16x4d_bits12)
1210
1211         HIGHBD_BFP(BLOCK_8X8,
1212                    vp9_highbd_sad8x8_bits12,
1213                    vp9_highbd_sad8x8_avg_bits12,
1214                    vp9_highbd_12_variance8x8,
1215                    vp9_highbd_12_sub_pixel_variance8x8,
1216                    vp9_highbd_12_sub_pixel_avg_variance8x8,
1217                    vp9_highbd_sad8x8x3_bits12,
1218                    vp9_highbd_sad8x8x8_bits12,
1219                    vp9_highbd_sad8x8x4d_bits12)
1220
1221         HIGHBD_BFP(BLOCK_8X4,
1222                    vp9_highbd_sad8x4_bits12,
1223                    vp9_highbd_sad8x4_avg_bits12,
1224                    vp9_highbd_12_variance8x4,
1225                    vp9_highbd_12_sub_pixel_variance8x4,
1226                    vp9_highbd_12_sub_pixel_avg_variance8x4,
1227                    NULL,
1228                    vp9_highbd_sad8x4x8_bits12,
1229                    vp9_highbd_sad8x4x4d_bits12)
1230
1231         HIGHBD_BFP(BLOCK_4X8,
1232                    vp9_highbd_sad4x8_bits12,
1233                    vp9_highbd_sad4x8_avg_bits12,
1234                    vp9_highbd_12_variance4x8,
1235                    vp9_highbd_12_sub_pixel_variance4x8,
1236                    vp9_highbd_12_sub_pixel_avg_variance4x8,
1237                    NULL,
1238                    vp9_highbd_sad4x8x8_bits12,
1239                    vp9_highbd_sad4x8x4d_bits12)
1240
1241         HIGHBD_BFP(BLOCK_4X4,
1242                    vp9_highbd_sad4x4_bits12,
1243                    vp9_highbd_sad4x4_avg_bits12,
1244                    vp9_highbd_12_variance4x4,
1245                    vp9_highbd_12_sub_pixel_variance4x4,
1246                    vp9_highbd_12_sub_pixel_avg_variance4x4,
1247                    vp9_highbd_sad4x4x3_bits12,
1248                    vp9_highbd_sad4x4x8_bits12,
1249                    vp9_highbd_sad4x4x4d_bits12)
1250         break;
1251
1252       default:
1253         assert(0 && "cm->bit_depth should be VPX_BITS_8, "
1254                     "VPX_BITS_10 or VPX_BITS_12");
1255     }
1256   }
1257 }
1258 #endif  // CONFIG_VP9_HIGHBITDEPTH
1259
1260 void vp9_change_config(struct VP9_COMP *cpi, const VP9EncoderConfig *oxcf) {
1261   VP9_COMMON *const cm = &cpi->common;
1262   RATE_CONTROL *const rc = &cpi->rc;
1263
1264   if (cm->profile != oxcf->profile)
1265     cm->profile = oxcf->profile;
1266   cm->bit_depth = oxcf->bit_depth;
1267
1268   if (cm->profile <= PROFILE_1)
1269     assert(cm->bit_depth == VPX_BITS_8);
1270   else
1271     assert(cm->bit_depth > VPX_BITS_8);
1272
1273   cpi->oxcf = *oxcf;
1274 #if CONFIG_VP9_HIGHBITDEPTH
1275   cpi->td.mb.e_mbd.bd = (int)cm->bit_depth;
1276 #endif  // CONFIG_VP9_HIGHBITDEPTH
1277
1278   rc->baseline_gf_interval = DEFAULT_GF_INTERVAL;
1279
1280   cpi->refresh_golden_frame = 0;
1281   cpi->refresh_last_frame = 1;
1282   cm->refresh_frame_context = 1;
1283   cm->reset_frame_context = 0;
1284
1285   vp9_reset_segment_features(&cm->seg);
1286   vp9_set_high_precision_mv(cpi, 0);
1287
1288   {
1289     int i;
1290
1291     for (i = 0; i < MAX_SEGMENTS; i++)
1292       cpi->segment_encode_breakout[i] = cpi->oxcf.encode_breakout;
1293   }
1294   cpi->encode_breakout = cpi->oxcf.encode_breakout;
1295
1296   set_rc_buffer_sizes(rc, &cpi->oxcf);
1297
1298   // Under a configuration change, where maximum_buffer_size may change,
1299   // keep buffer level clipped to the maximum allowed buffer size.
1300   rc->bits_off_target = MIN(rc->bits_off_target, rc->maximum_buffer_size);
1301   rc->buffer_level = MIN(rc->buffer_level, rc->maximum_buffer_size);
1302
1303   // Set up frame rate and related parameters rate control values.
1304   vp9_new_framerate(cpi, cpi->framerate);
1305
1306   // Set absolute upper and lower quality limits
1307   rc->worst_quality = cpi->oxcf.worst_allowed_q;
1308   rc->best_quality = cpi->oxcf.best_allowed_q;
1309
1310   cm->interp_filter = cpi->sf.default_interp_filter;
1311
1312   cm->display_width = cpi->oxcf.width;
1313   cm->display_height = cpi->oxcf.height;
1314
1315   if (cpi->initial_width) {
1316     // Increasing the size of the frame beyond the first seen frame, or some
1317     // otherwise signaled maximum size, is not supported.
1318     // TODO(jkoleszar): exit gracefully.
1319     assert(cm->width <= cpi->initial_width);
1320     assert(cm->height <= cpi->initial_height);
1321   }
1322   update_frame_size(cpi);
1323
1324   if ((cpi->svc.number_temporal_layers > 1 &&
1325       cpi->oxcf.rc_mode == VPX_CBR) ||
1326       ((cpi->svc.number_temporal_layers > 1 ||
1327         cpi->svc.number_spatial_layers > 1) &&
1328        cpi->oxcf.pass != 1)) {
1329     vp9_update_layer_context_change_config(cpi,
1330                                            (int)cpi->oxcf.target_bandwidth);
1331   }
1332
1333   cpi->alt_ref_source = NULL;
1334   rc->is_src_frame_alt_ref = 0;
1335
1336 #if 0
1337   // Experimental RD Code
1338   cpi->frame_distortion = 0;
1339   cpi->last_frame_distortion = 0;
1340 #endif
1341
1342   set_tile_limits(cpi);
1343
1344   cpi->ext_refresh_frame_flags_pending = 0;
1345   cpi->ext_refresh_frame_context_pending = 0;
1346
1347 #if CONFIG_VP9_HIGHBITDEPTH
1348   highbd_set_var_fns(cpi);
1349 #endif
1350
1351 #if CONFIG_VP9_TEMPORAL_DENOISING
1352   if (cpi->oxcf.noise_sensitivity > 0) {
1353     vp9_denoiser_alloc(&(cpi->denoiser), cm->width, cm->height,
1354                        cm->subsampling_x, cm->subsampling_y,
1355 #if CONFIG_VP9_HIGHBITDEPTH
1356                        cm->use_highbitdepth,
1357 #endif
1358                        VP9_ENC_BORDER_IN_PIXELS);
1359   }
1360 #endif
1361 }
1362
1363 #ifndef M_LOG2_E
1364 #define M_LOG2_E 0.693147180559945309417
1365 #endif
1366 #define log2f(x) (log (x) / (float) M_LOG2_E)
1367
1368 static void cal_nmvjointsadcost(int *mvjointsadcost) {
1369   mvjointsadcost[0] = 600;
1370   mvjointsadcost[1] = 300;
1371   mvjointsadcost[2] = 300;
1372   mvjointsadcost[3] = 300;
1373 }
1374
1375 static void cal_nmvsadcosts(int *mvsadcost[2]) {
1376   int i = 1;
1377
1378   mvsadcost[0][0] = 0;
1379   mvsadcost[1][0] = 0;
1380
1381   do {
1382     double z = 256 * (2 * (log2f(8 * i) + .6));
1383     mvsadcost[0][i] = (int)z;
1384     mvsadcost[1][i] = (int)z;
1385     mvsadcost[0][-i] = (int)z;
1386     mvsadcost[1][-i] = (int)z;
1387   } while (++i <= MV_MAX);
1388 }
1389
1390 static void cal_nmvsadcosts_hp(int *mvsadcost[2]) {
1391   int i = 1;
1392
1393   mvsadcost[0][0] = 0;
1394   mvsadcost[1][0] = 0;
1395
1396   do {
1397     double z = 256 * (2 * (log2f(8 * i) + .6));
1398     mvsadcost[0][i] = (int)z;
1399     mvsadcost[1][i] = (int)z;
1400     mvsadcost[0][-i] = (int)z;
1401     mvsadcost[1][-i] = (int)z;
1402   } while (++i <= MV_MAX);
1403 }
1404
1405
1406 VP9_COMP *vp9_create_compressor(VP9EncoderConfig *oxcf) {
1407   unsigned int i;
1408   VP9_COMP *const cpi = vpx_memalign(32, sizeof(VP9_COMP));
1409   VP9_COMMON *const cm = cpi != NULL ? &cpi->common : NULL;
1410
1411   if (!cm)
1412     return NULL;
1413
1414   vp9_zero(*cpi);
1415
1416   if (setjmp(cm->error.jmp)) {
1417     cm->error.setjmp = 0;
1418     vp9_remove_compressor(cpi);
1419     return 0;
1420   }
1421
1422   cm->error.setjmp = 1;
1423   cm->alloc_mi = vp9_enc_alloc_mi;
1424   cm->free_mi = vp9_enc_free_mi;
1425   cm->setup_mi = vp9_enc_setup_mi;
1426
1427   CHECK_MEM_ERROR(cm, cm->fc,
1428                   (FRAME_CONTEXT *)vpx_calloc(1, sizeof(*cm->fc)));
1429   CHECK_MEM_ERROR(cm, cm->frame_contexts,
1430                   (FRAME_CONTEXT *)vpx_calloc(FRAME_CONTEXTS,
1431                   sizeof(*cm->frame_contexts)));
1432
1433   cpi->use_svc = 0;
1434
1435   init_config(cpi, oxcf);
1436   vp9_rc_init(&cpi->oxcf, oxcf->pass, &cpi->rc);
1437
1438   cm->current_video_frame = 0;
1439   cpi->partition_search_skippable_frame = 0;
1440   cpi->tile_data = NULL;
1441
1442   // Create the encoder segmentation map and set all entries to 0
1443   CHECK_MEM_ERROR(cm, cpi->segmentation_map,
1444                   vpx_calloc(cm->mi_rows * cm->mi_cols, 1));
1445
1446   // Create a complexity map used for rd adjustment
1447   CHECK_MEM_ERROR(cm, cpi->complexity_map,
1448                   vpx_calloc(cm->mi_rows * cm->mi_cols, 1));
1449
1450   // Create a map used for cyclic background refresh.
1451   CHECK_MEM_ERROR(cm, cpi->cyclic_refresh,
1452                   vp9_cyclic_refresh_alloc(cm->mi_rows, cm->mi_cols));
1453
1454   // And a place holder structure is the coding context
1455   // for use if we want to save and restore it
1456   CHECK_MEM_ERROR(cm, cpi->coding_context.last_frame_seg_map_copy,
1457                   vpx_calloc(cm->mi_rows * cm->mi_cols, 1));
1458
1459   CHECK_MEM_ERROR(cm, cpi->nmvcosts[0],
1460                   vpx_calloc(MV_VALS, sizeof(*cpi->nmvcosts[0])));
1461   CHECK_MEM_ERROR(cm, cpi->nmvcosts[1],
1462                   vpx_calloc(MV_VALS, sizeof(*cpi->nmvcosts[1])));
1463   CHECK_MEM_ERROR(cm, cpi->nmvcosts_hp[0],
1464                   vpx_calloc(MV_VALS, sizeof(*cpi->nmvcosts_hp[0])));
1465   CHECK_MEM_ERROR(cm, cpi->nmvcosts_hp[1],
1466                   vpx_calloc(MV_VALS, sizeof(*cpi->nmvcosts_hp[1])));
1467   CHECK_MEM_ERROR(cm, cpi->nmvsadcosts[0],
1468                   vpx_calloc(MV_VALS, sizeof(*cpi->nmvsadcosts[0])));
1469   CHECK_MEM_ERROR(cm, cpi->nmvsadcosts[1],
1470                   vpx_calloc(MV_VALS, sizeof(*cpi->nmvsadcosts[1])));
1471   CHECK_MEM_ERROR(cm, cpi->nmvsadcosts_hp[0],
1472                   vpx_calloc(MV_VALS, sizeof(*cpi->nmvsadcosts_hp[0])));
1473   CHECK_MEM_ERROR(cm, cpi->nmvsadcosts_hp[1],
1474                   vpx_calloc(MV_VALS, sizeof(*cpi->nmvsadcosts_hp[1])));
1475
1476   for (i = 0; i < (sizeof(cpi->mbgraph_stats) /
1477                    sizeof(cpi->mbgraph_stats[0])); i++) {
1478     CHECK_MEM_ERROR(cm, cpi->mbgraph_stats[i].mb_stats,
1479                     vpx_calloc(cm->MBs *
1480                                sizeof(*cpi->mbgraph_stats[i].mb_stats), 1));
1481   }
1482
1483 #if CONFIG_FP_MB_STATS
1484   cpi->use_fp_mb_stats = 0;
1485   if (cpi->use_fp_mb_stats) {
1486     // a place holder used to store the first pass mb stats in the first pass
1487     CHECK_MEM_ERROR(cm, cpi->twopass.frame_mb_stats_buf,
1488                     vpx_calloc(cm->MBs * sizeof(uint8_t), 1));
1489   } else {
1490     cpi->twopass.frame_mb_stats_buf = NULL;
1491   }
1492 #endif
1493
1494   cpi->refresh_alt_ref_frame = 0;
1495   cpi->multi_arf_last_grp_enabled = 0;
1496
1497   cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
1498 #if CONFIG_INTERNAL_STATS
1499   cpi->b_calculate_ssimg = 0;
1500
1501   cpi->count = 0;
1502   cpi->bytes = 0;
1503
1504   if (cpi->b_calculate_psnr) {
1505     cpi->total_y = 0.0;
1506     cpi->total_u = 0.0;
1507     cpi->total_v = 0.0;
1508     cpi->total = 0.0;
1509     cpi->total_sq_error = 0;
1510     cpi->total_samples = 0;
1511
1512     cpi->totalp_y = 0.0;
1513     cpi->totalp_u = 0.0;
1514     cpi->totalp_v = 0.0;
1515     cpi->totalp = 0.0;
1516     cpi->totalp_sq_error = 0;
1517     cpi->totalp_samples = 0;
1518
1519     cpi->tot_recode_hits = 0;
1520     cpi->summed_quality = 0;
1521     cpi->summed_weights = 0;
1522     cpi->summedp_quality = 0;
1523     cpi->summedp_weights = 0;
1524   }
1525
1526   if (cpi->b_calculate_ssimg) {
1527     cpi->total_ssimg_y = 0;
1528     cpi->total_ssimg_u = 0;
1529     cpi->total_ssimg_v = 0;
1530     cpi->total_ssimg_all = 0;
1531   }
1532
1533 #endif
1534
1535   cpi->first_time_stamp_ever = INT64_MAX;
1536
1537   cal_nmvjointsadcost(cpi->td.mb.nmvjointsadcost);
1538   cpi->td.mb.nmvcost[0] = &cpi->nmvcosts[0][MV_MAX];
1539   cpi->td.mb.nmvcost[1] = &cpi->nmvcosts[1][MV_MAX];
1540   cpi->td.mb.nmvsadcost[0] = &cpi->nmvsadcosts[0][MV_MAX];
1541   cpi->td.mb.nmvsadcost[1] = &cpi->nmvsadcosts[1][MV_MAX];
1542   cal_nmvsadcosts(cpi->td.mb.nmvsadcost);
1543
1544   cpi->td.mb.nmvcost_hp[0] = &cpi->nmvcosts_hp[0][MV_MAX];
1545   cpi->td.mb.nmvcost_hp[1] = &cpi->nmvcosts_hp[1][MV_MAX];
1546   cpi->td.mb.nmvsadcost_hp[0] = &cpi->nmvsadcosts_hp[0][MV_MAX];
1547   cpi->td.mb.nmvsadcost_hp[1] = &cpi->nmvsadcosts_hp[1][MV_MAX];
1548   cal_nmvsadcosts_hp(cpi->td.mb.nmvsadcost_hp);
1549
1550 #if CONFIG_VP9_TEMPORAL_DENOISING
1551 #ifdef OUTPUT_YUV_DENOISED
1552   yuv_denoised_file = fopen("denoised.yuv", "ab");
1553 #endif
1554 #endif
1555 #ifdef OUTPUT_YUV_REC
1556   yuv_rec_file = fopen("rec.yuv", "wb");
1557 #endif
1558
1559 #if 0
1560   framepsnr = fopen("framepsnr.stt", "a");
1561   kf_list = fopen("kf_list.stt", "w");
1562 #endif
1563
1564   cpi->allow_encode_breakout = ENCODE_BREAKOUT_ENABLED;
1565
1566   if (oxcf->pass == 1) {
1567     vp9_init_first_pass(cpi);
1568   } else if (oxcf->pass == 2) {
1569     const size_t packet_sz = sizeof(FIRSTPASS_STATS);
1570     const int packets = (int)(oxcf->two_pass_stats_in.sz / packet_sz);
1571
1572     if (cpi->svc.number_spatial_layers > 1
1573         || cpi->svc.number_temporal_layers > 1) {
1574       FIRSTPASS_STATS *const stats = oxcf->two_pass_stats_in.buf;
1575       FIRSTPASS_STATS *stats_copy[VPX_SS_MAX_LAYERS] = {0};
1576       int i;
1577
1578       for (i = 0; i < oxcf->ss_number_layers; ++i) {
1579         FIRSTPASS_STATS *const last_packet_for_layer =
1580             &stats[packets - oxcf->ss_number_layers + i];
1581         const int layer_id = (int)last_packet_for_layer->spatial_layer_id;
1582         const int packets_in_layer = (int)last_packet_for_layer->count + 1;
1583         if (layer_id >= 0 && layer_id < oxcf->ss_number_layers) {
1584           LAYER_CONTEXT *const lc = &cpi->svc.layer_context[layer_id];
1585
1586           vpx_free(lc->rc_twopass_stats_in.buf);
1587
1588           lc->rc_twopass_stats_in.sz = packets_in_layer * packet_sz;
1589           CHECK_MEM_ERROR(cm, lc->rc_twopass_stats_in.buf,
1590                           vpx_malloc(lc->rc_twopass_stats_in.sz));
1591           lc->twopass.stats_in_start = lc->rc_twopass_stats_in.buf;
1592           lc->twopass.stats_in = lc->twopass.stats_in_start;
1593           lc->twopass.stats_in_end = lc->twopass.stats_in_start
1594                                      + packets_in_layer - 1;
1595           stats_copy[layer_id] = lc->rc_twopass_stats_in.buf;
1596         }
1597       }
1598
1599       for (i = 0; i < packets; ++i) {
1600         const int layer_id = (int)stats[i].spatial_layer_id;
1601         if (layer_id >= 0 && layer_id < oxcf->ss_number_layers
1602             && stats_copy[layer_id] != NULL) {
1603           *stats_copy[layer_id] = stats[i];
1604           ++stats_copy[layer_id];
1605         }
1606       }
1607
1608       vp9_init_second_pass_spatial_svc(cpi);
1609     } else {
1610 #if CONFIG_FP_MB_STATS
1611       if (cpi->use_fp_mb_stats) {
1612         const size_t psz = cpi->common.MBs * sizeof(uint8_t);
1613         const int ps = (int)(oxcf->firstpass_mb_stats_in.sz / psz);
1614
1615         cpi->twopass.firstpass_mb_stats.mb_stats_start =
1616             oxcf->firstpass_mb_stats_in.buf;
1617         cpi->twopass.firstpass_mb_stats.mb_stats_end =
1618             cpi->twopass.firstpass_mb_stats.mb_stats_start +
1619             (ps - 1) * cpi->common.MBs * sizeof(uint8_t);
1620       }
1621 #endif
1622
1623       cpi->twopass.stats_in_start = oxcf->two_pass_stats_in.buf;
1624       cpi->twopass.stats_in = cpi->twopass.stats_in_start;
1625       cpi->twopass.stats_in_end = &cpi->twopass.stats_in[packets - 1];
1626
1627       vp9_init_second_pass(cpi);
1628     }
1629   }
1630
1631   vp9_set_speed_features_framesize_independent(cpi);
1632   vp9_set_speed_features_framesize_dependent(cpi);
1633
1634   // Allocate memory to store variances for a frame.
1635   CHECK_MEM_ERROR(cm, cpi->source_diff_var,
1636                   vpx_calloc(cm->MBs, sizeof(diff)));
1637   cpi->source_var_thresh = 0;
1638   cpi->frames_till_next_var_check = 0;
1639
1640 #define BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX3F, SDX8F, SDX4DF)\
1641     cpi->fn_ptr[BT].sdf            = SDF; \
1642     cpi->fn_ptr[BT].sdaf           = SDAF; \
1643     cpi->fn_ptr[BT].vf             = VF; \
1644     cpi->fn_ptr[BT].svf            = SVF; \
1645     cpi->fn_ptr[BT].svaf           = SVAF; \
1646     cpi->fn_ptr[BT].sdx3f          = SDX3F; \
1647     cpi->fn_ptr[BT].sdx8f          = SDX8F; \
1648     cpi->fn_ptr[BT].sdx4df         = SDX4DF;
1649
1650   BFP(BLOCK_32X16, vp9_sad32x16, vp9_sad32x16_avg,
1651       vp9_variance32x16, vp9_sub_pixel_variance32x16,
1652       vp9_sub_pixel_avg_variance32x16, NULL, NULL, vp9_sad32x16x4d)
1653
1654   BFP(BLOCK_16X32, vp9_sad16x32, vp9_sad16x32_avg,
1655       vp9_variance16x32, vp9_sub_pixel_variance16x32,
1656       vp9_sub_pixel_avg_variance16x32, NULL, NULL, vp9_sad16x32x4d)
1657
1658   BFP(BLOCK_64X32, vp9_sad64x32, vp9_sad64x32_avg,
1659       vp9_variance64x32, vp9_sub_pixel_variance64x32,
1660       vp9_sub_pixel_avg_variance64x32, NULL, NULL, vp9_sad64x32x4d)
1661
1662   BFP(BLOCK_32X64, vp9_sad32x64, vp9_sad32x64_avg,
1663       vp9_variance32x64, vp9_sub_pixel_variance32x64,
1664       vp9_sub_pixel_avg_variance32x64, NULL, NULL, vp9_sad32x64x4d)
1665
1666   BFP(BLOCK_32X32, vp9_sad32x32, vp9_sad32x32_avg,
1667       vp9_variance32x32, vp9_sub_pixel_variance32x32,
1668       vp9_sub_pixel_avg_variance32x32, vp9_sad32x32x3, vp9_sad32x32x8,
1669       vp9_sad32x32x4d)
1670
1671   BFP(BLOCK_64X64, vp9_sad64x64, vp9_sad64x64_avg,
1672       vp9_variance64x64, vp9_sub_pixel_variance64x64,
1673       vp9_sub_pixel_avg_variance64x64, vp9_sad64x64x3, vp9_sad64x64x8,
1674       vp9_sad64x64x4d)
1675
1676   BFP(BLOCK_16X16, vp9_sad16x16, vp9_sad16x16_avg,
1677       vp9_variance16x16, vp9_sub_pixel_variance16x16,
1678       vp9_sub_pixel_avg_variance16x16, vp9_sad16x16x3, vp9_sad16x16x8,
1679       vp9_sad16x16x4d)
1680
1681   BFP(BLOCK_16X8, vp9_sad16x8, vp9_sad16x8_avg,
1682       vp9_variance16x8, vp9_sub_pixel_variance16x8,
1683       vp9_sub_pixel_avg_variance16x8,
1684       vp9_sad16x8x3, vp9_sad16x8x8, vp9_sad16x8x4d)
1685
1686   BFP(BLOCK_8X16, vp9_sad8x16, vp9_sad8x16_avg,
1687       vp9_variance8x16, vp9_sub_pixel_variance8x16,
1688       vp9_sub_pixel_avg_variance8x16,
1689       vp9_sad8x16x3, vp9_sad8x16x8, vp9_sad8x16x4d)
1690
1691   BFP(BLOCK_8X8, vp9_sad8x8, vp9_sad8x8_avg,
1692       vp9_variance8x8, vp9_sub_pixel_variance8x8,
1693       vp9_sub_pixel_avg_variance8x8,
1694       vp9_sad8x8x3, vp9_sad8x8x8, vp9_sad8x8x4d)
1695
1696   BFP(BLOCK_8X4, vp9_sad8x4, vp9_sad8x4_avg,
1697       vp9_variance8x4, vp9_sub_pixel_variance8x4,
1698       vp9_sub_pixel_avg_variance8x4, NULL, vp9_sad8x4x8, vp9_sad8x4x4d)
1699
1700   BFP(BLOCK_4X8, vp9_sad4x8, vp9_sad4x8_avg,
1701       vp9_variance4x8, vp9_sub_pixel_variance4x8,
1702       vp9_sub_pixel_avg_variance4x8, NULL, vp9_sad4x8x8, vp9_sad4x8x4d)
1703
1704   BFP(BLOCK_4X4, vp9_sad4x4, vp9_sad4x4_avg,
1705       vp9_variance4x4, vp9_sub_pixel_variance4x4,
1706       vp9_sub_pixel_avg_variance4x4,
1707       vp9_sad4x4x3, vp9_sad4x4x8, vp9_sad4x4x4d)
1708
1709 #if CONFIG_VP9_HIGHBITDEPTH
1710   highbd_set_var_fns(cpi);
1711 #endif
1712
1713   /* vp9_init_quantizer() is first called here. Add check in
1714    * vp9_frame_init_quantizer() so that vp9_init_quantizer is only
1715    * called later when needed. This will avoid unnecessary calls of
1716    * vp9_init_quantizer() for every frame.
1717    */
1718   vp9_init_quantizer(cpi);
1719
1720   vp9_loop_filter_init(cm);
1721
1722   cm->error.setjmp = 0;
1723
1724   return cpi;
1725 }
1726
1727 void vp9_remove_compressor(VP9_COMP *cpi) {
1728   VP9_COMMON *const cm = &cpi->common;
1729   unsigned int i;
1730
1731   if (!cpi)
1732     return;
1733
1734   if (cpi && (cm->current_video_frame > 0)) {
1735 #if CONFIG_INTERNAL_STATS
1736
1737     vp9_clear_system_state();
1738
1739     // printf("\n8x8-4x4:%d-%d\n", cpi->t8x8_count, cpi->t4x4_count);
1740     if (cpi->oxcf.pass != 1) {
1741       FILE *f = fopen("opsnr.stt", "a");
1742       double time_encoded = (cpi->last_end_time_stamp_seen
1743                              - cpi->first_time_stamp_ever) / 10000000.000;
1744       double total_encode_time = (cpi->time_receive_data +
1745                                   cpi->time_compress_data)   / 1000.000;
1746       const double dr =
1747           (double)cpi->bytes * (double) 8 / (double)1000 / time_encoded;
1748       const double peak = (double)((1 << cpi->oxcf.input_bit_depth) - 1);
1749
1750       if (cpi->b_calculate_psnr) {
1751         const double total_psnr =
1752             vpx_sse_to_psnr((double)cpi->total_samples, peak,
1753                             (double)cpi->total_sq_error);
1754         const double totalp_psnr =
1755             vpx_sse_to_psnr((double)cpi->totalp_samples, peak,
1756                             (double)cpi->totalp_sq_error);
1757         const double total_ssim = 100 * pow(cpi->summed_quality /
1758                                                 cpi->summed_weights, 8.0);
1759         const double totalp_ssim = 100 * pow(cpi->summedp_quality /
1760                                                 cpi->summedp_weights, 8.0);
1761
1762         fprintf(f, "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\t"
1763                 "VPXSSIM\tVPSSIMP\t  Time(ms)\n");
1764         fprintf(f, "%7.2f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t%8.0f\n",
1765                 dr, cpi->total / cpi->count, total_psnr,
1766                 cpi->totalp / cpi->count, totalp_psnr, total_ssim, totalp_ssim,
1767                 total_encode_time);
1768       }
1769
1770       if (cpi->b_calculate_ssimg) {
1771         fprintf(f, "BitRate\tSSIM_Y\tSSIM_U\tSSIM_V\tSSIM_A\t  Time(ms)\n");
1772         fprintf(f, "%7.2f\t%6.4f\t%6.4f\t%6.4f\t%6.4f\t%8.0f\n", dr,
1773                 cpi->total_ssimg_y / cpi->count,
1774                 cpi->total_ssimg_u / cpi->count,
1775                 cpi->total_ssimg_v / cpi->count,
1776                 cpi->total_ssimg_all / cpi->count, total_encode_time);
1777       }
1778
1779       fclose(f);
1780     }
1781
1782 #endif
1783
1784 #if 0
1785     {
1786       printf("\n_pick_loop_filter_level:%d\n", cpi->time_pick_lpf / 1000);
1787       printf("\n_frames recive_data encod_mb_row compress_frame  Total\n");
1788       printf("%6d %10ld %10ld %10ld %10ld\n", cpi->common.current_video_frame,
1789              cpi->time_receive_data / 1000, cpi->time_encode_sb_row / 1000,
1790              cpi->time_compress_data / 1000,
1791              (cpi->time_receive_data + cpi->time_compress_data) / 1000);
1792     }
1793 #endif
1794   }
1795
1796 #if CONFIG_VP9_TEMPORAL_DENOISING
1797   if (cpi->oxcf.noise_sensitivity > 0) {
1798     vp9_denoiser_free(&(cpi->denoiser));
1799   }
1800 #endif
1801
1802   dealloc_compressor_data(cpi);
1803   vpx_free(cpi->tok);
1804
1805   for (i = 0; i < sizeof(cpi->mbgraph_stats) /
1806                   sizeof(cpi->mbgraph_stats[0]); ++i) {
1807     vpx_free(cpi->mbgraph_stats[i].mb_stats);
1808   }
1809
1810 #if CONFIG_FP_MB_STATS
1811   if (cpi->use_fp_mb_stats) {
1812     vpx_free(cpi->twopass.frame_mb_stats_buf);
1813     cpi->twopass.frame_mb_stats_buf = NULL;
1814   }
1815 #endif
1816
1817   vp9_remove_common(cm);
1818   vpx_free(cpi);
1819
1820 #if CONFIG_VP9_TEMPORAL_DENOISING
1821 #ifdef OUTPUT_YUV_DENOISED
1822   fclose(yuv_denoised_file);
1823 #endif
1824 #endif
1825 #ifdef OUTPUT_YUV_REC
1826   fclose(yuv_rec_file);
1827 #endif
1828
1829 #if 0
1830
1831   if (keyfile)
1832     fclose(keyfile);
1833
1834   if (framepsnr)
1835     fclose(framepsnr);
1836
1837   if (kf_list)
1838     fclose(kf_list);
1839
1840 #endif
1841 }
1842
1843 static int64_t get_sse(const uint8_t *a, int a_stride,
1844                        const uint8_t *b, int b_stride,
1845                        int width, int height) {
1846   const int dw = width % 16;
1847   const int dh = height % 16;
1848   int64_t total_sse = 0;
1849   unsigned int sse = 0;
1850   int sum = 0;
1851   int x, y;
1852
1853   if (dw > 0) {
1854     variance(&a[width - dw], a_stride, &b[width - dw], b_stride,
1855              dw, height, &sse, &sum);
1856     total_sse += sse;
1857   }
1858
1859   if (dh > 0) {
1860     variance(&a[(height - dh) * a_stride], a_stride,
1861              &b[(height - dh) * b_stride], b_stride,
1862              width - dw, dh, &sse, &sum);
1863     total_sse += sse;
1864   }
1865
1866   for (y = 0; y < height / 16; ++y) {
1867     const uint8_t *pa = a;
1868     const uint8_t *pb = b;
1869     for (x = 0; x < width / 16; ++x) {
1870       vp9_mse16x16(pa, a_stride, pb, b_stride, &sse);
1871       total_sse += sse;
1872
1873       pa += 16;
1874       pb += 16;
1875     }
1876
1877     a += 16 * a_stride;
1878     b += 16 * b_stride;
1879   }
1880
1881   return total_sse;
1882 }
1883
1884 #if CONFIG_VP9_HIGHBITDEPTH
1885 static int64_t highbd_get_sse_shift(const uint8_t *a8, int a_stride,
1886                                     const uint8_t *b8, int b_stride,
1887                                     int width, int height,
1888                                     unsigned int input_shift) {
1889   const uint16_t *a = CONVERT_TO_SHORTPTR(a8);
1890   const uint16_t *b = CONVERT_TO_SHORTPTR(b8);
1891   int64_t total_sse = 0;
1892   int x, y;
1893   for (y = 0; y < height; ++y) {
1894     for (x = 0; x < width; ++x) {
1895       int64_t diff;
1896       diff = (a[x] >> input_shift) - (b[x] >> input_shift);
1897       total_sse += diff * diff;
1898     }
1899     a += a_stride;
1900     b += b_stride;
1901   }
1902   return total_sse;
1903 }
1904
1905 static int64_t highbd_get_sse(const uint8_t *a, int a_stride,
1906                               const uint8_t *b, int b_stride,
1907                               int width, int height) {
1908   int64_t total_sse = 0;
1909   int x, y;
1910   const int dw = width % 16;
1911   const int dh = height % 16;
1912   unsigned int sse = 0;
1913   int sum = 0;
1914   if (dw > 0) {
1915     highbd_variance(&a[width - dw], a_stride, &b[width - dw], b_stride,
1916                     dw, height, &sse, &sum);
1917     total_sse += sse;
1918   }
1919   if (dh > 0) {
1920     highbd_variance(&a[(height - dh) * a_stride], a_stride,
1921                     &b[(height - dh) * b_stride], b_stride,
1922                     width - dw, dh, &sse, &sum);
1923     total_sse += sse;
1924   }
1925   for (y = 0; y < height / 16; ++y) {
1926     const uint8_t *pa = a;
1927     const uint8_t *pb = b;
1928     for (x = 0; x < width / 16; ++x) {
1929       vp9_highbd_mse16x16(pa, a_stride, pb, b_stride, &sse);
1930       total_sse += sse;
1931       pa += 16;
1932       pb += 16;
1933     }
1934     a += 16 * a_stride;
1935     b += 16 * b_stride;
1936   }
1937   return total_sse;
1938 }
1939 #endif  // CONFIG_VP9_HIGHBITDEPTH
1940
1941 typedef struct {
1942   double psnr[4];       // total/y/u/v
1943   uint64_t sse[4];      // total/y/u/v
1944   uint32_t samples[4];  // total/y/u/v
1945 } PSNR_STATS;
1946
1947 static void calc_psnr(const YV12_BUFFER_CONFIG *a, const YV12_BUFFER_CONFIG *b,
1948                       PSNR_STATS *psnr) {
1949   static const double peak = 255.0;
1950   const int widths[3]        = {a->y_width,  a->uv_width,  a->uv_width };
1951   const int heights[3]       = {a->y_height, a->uv_height, a->uv_height};
1952   const uint8_t *a_planes[3] = {a->y_buffer, a->u_buffer,  a->v_buffer };
1953   const int a_strides[3]     = {a->y_stride, a->uv_stride, a->uv_stride};
1954   const uint8_t *b_planes[3] = {b->y_buffer, b->u_buffer,  b->v_buffer };
1955   const int b_strides[3]     = {b->y_stride, b->uv_stride, b->uv_stride};
1956   int i;
1957   uint64_t total_sse = 0;
1958   uint32_t total_samples = 0;
1959
1960   for (i = 0; i < 3; ++i) {
1961     const int w = widths[i];
1962     const int h = heights[i];
1963     const uint32_t samples = w * h;
1964     const uint64_t sse = get_sse(a_planes[i], a_strides[i],
1965                                  b_planes[i], b_strides[i],
1966                                  w, h);
1967     psnr->sse[1 + i] = sse;
1968     psnr->samples[1 + i] = samples;
1969     psnr->psnr[1 + i] = vpx_sse_to_psnr(samples, peak, (double)sse);
1970
1971     total_sse += sse;
1972     total_samples += samples;
1973   }
1974
1975   psnr->sse[0] = total_sse;
1976   psnr->samples[0] = total_samples;
1977   psnr->psnr[0] = vpx_sse_to_psnr((double)total_samples, peak,
1978                                   (double)total_sse);
1979 }
1980
1981 #if CONFIG_VP9_HIGHBITDEPTH
1982 static void calc_highbd_psnr(const YV12_BUFFER_CONFIG *a,
1983                              const YV12_BUFFER_CONFIG *b,
1984                              PSNR_STATS *psnr,
1985                              unsigned int bit_depth,
1986                              unsigned int in_bit_depth) {
1987   const int widths[3] = {a->y_width,  a->uv_width,  a->uv_width };
1988   const int heights[3] = {a->y_height, a->uv_height, a->uv_height};
1989   const uint8_t *a_planes[3] = {a->y_buffer, a->u_buffer,  a->v_buffer };
1990   const int a_strides[3] = {a->y_stride, a->uv_stride, a->uv_stride};
1991   const uint8_t *b_planes[3] = {b->y_buffer, b->u_buffer,  b->v_buffer };
1992   const int b_strides[3] = {b->y_stride, b->uv_stride, b->uv_stride};
1993   int i;
1994   uint64_t total_sse = 0;
1995   uint32_t total_samples = 0;
1996   const double peak = (double)((1 << in_bit_depth) - 1);
1997   const unsigned int input_shift = bit_depth - in_bit_depth;
1998
1999   for (i = 0; i < 3; ++i) {
2000     const int w = widths[i];
2001     const int h = heights[i];
2002     const uint32_t samples = w * h;
2003     uint64_t sse;
2004     if (a->flags & YV12_FLAG_HIGHBITDEPTH) {
2005       if (input_shift) {
2006         sse = highbd_get_sse_shift(a_planes[i], a_strides[i],
2007                                    b_planes[i], b_strides[i], w, h,
2008                                    input_shift);
2009       } else {
2010         sse = highbd_get_sse(a_planes[i], a_strides[i],
2011                              b_planes[i], b_strides[i], w, h);
2012       }
2013     } else {
2014       sse = get_sse(a_planes[i], a_strides[i],
2015                     b_planes[i], b_strides[i],
2016                     w, h);
2017     }
2018     psnr->sse[1 + i] = sse;
2019     psnr->samples[1 + i] = samples;
2020     psnr->psnr[1 + i] = vpx_sse_to_psnr(samples, peak, (double)sse);
2021
2022     total_sse += sse;
2023     total_samples += samples;
2024   }
2025
2026   psnr->sse[0] = total_sse;
2027   psnr->samples[0] = total_samples;
2028   psnr->psnr[0] = vpx_sse_to_psnr((double)total_samples, peak,
2029                                   (double)total_sse);
2030 }
2031 #endif  // CONFIG_VP9_HIGHBITDEPTH
2032
2033 static void generate_psnr_packet(VP9_COMP *cpi) {
2034   struct vpx_codec_cx_pkt pkt;
2035   int i;
2036   PSNR_STATS psnr;
2037 #if CONFIG_VP9_HIGHBITDEPTH
2038   calc_highbd_psnr(cpi->Source, cpi->common.frame_to_show, &psnr,
2039                    cpi->td.mb.e_mbd.bd, cpi->oxcf.input_bit_depth);
2040 #else
2041   calc_psnr(cpi->Source, cpi->common.frame_to_show, &psnr);
2042 #endif
2043
2044   for (i = 0; i < 4; ++i) {
2045     pkt.data.psnr.samples[i] = psnr.samples[i];
2046     pkt.data.psnr.sse[i] = psnr.sse[i];
2047     pkt.data.psnr.psnr[i] = psnr.psnr[i];
2048   }
2049   pkt.kind = VPX_CODEC_PSNR_PKT;
2050   if (is_two_pass_svc(cpi))
2051     cpi->svc.layer_context[cpi->svc.spatial_layer_id].psnr_pkt = pkt.data.psnr;
2052   else
2053     vpx_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
2054 }
2055
2056 int vp9_use_as_reference(VP9_COMP *cpi, int ref_frame_flags) {
2057   if (ref_frame_flags > 7)
2058     return -1;
2059
2060   cpi->ref_frame_flags = ref_frame_flags;
2061   return 0;
2062 }
2063
2064 void vp9_update_reference(VP9_COMP *cpi, int ref_frame_flags) {
2065   cpi->ext_refresh_golden_frame = (ref_frame_flags & VP9_GOLD_FLAG) != 0;
2066   cpi->ext_refresh_alt_ref_frame = (ref_frame_flags & VP9_ALT_FLAG) != 0;
2067   cpi->ext_refresh_last_frame = (ref_frame_flags & VP9_LAST_FLAG) != 0;
2068   cpi->ext_refresh_frame_flags_pending = 1;
2069 }
2070
2071 static YV12_BUFFER_CONFIG *get_vp9_ref_frame_buffer(VP9_COMP *cpi,
2072                                 VP9_REFFRAME ref_frame_flag) {
2073   MV_REFERENCE_FRAME ref_frame = NONE;
2074   if (ref_frame_flag == VP9_LAST_FLAG)
2075     ref_frame = LAST_FRAME;
2076   else if (ref_frame_flag == VP9_GOLD_FLAG)
2077     ref_frame = GOLDEN_FRAME;
2078   else if (ref_frame_flag == VP9_ALT_FLAG)
2079     ref_frame = ALTREF_FRAME;
2080
2081   return ref_frame == NONE ? NULL : get_ref_frame_buffer(cpi, ref_frame);
2082 }
2083
2084 int vp9_copy_reference_enc(VP9_COMP *cpi, VP9_REFFRAME ref_frame_flag,
2085                            YV12_BUFFER_CONFIG *sd) {
2086   YV12_BUFFER_CONFIG *cfg = get_vp9_ref_frame_buffer(cpi, ref_frame_flag);
2087   if (cfg) {
2088     vp8_yv12_copy_frame(cfg, sd);
2089     return 0;
2090   } else {
2091     return -1;
2092   }
2093 }
2094
2095 int vp9_set_reference_enc(VP9_COMP *cpi, VP9_REFFRAME ref_frame_flag,
2096                           YV12_BUFFER_CONFIG *sd) {
2097   YV12_BUFFER_CONFIG *cfg = get_vp9_ref_frame_buffer(cpi, ref_frame_flag);
2098   if (cfg) {
2099     vp8_yv12_copy_frame(sd, cfg);
2100     return 0;
2101   } else {
2102     return -1;
2103   }
2104 }
2105
2106 int vp9_update_entropy(VP9_COMP * cpi, int update) {
2107   cpi->ext_refresh_frame_context = update;
2108   cpi->ext_refresh_frame_context_pending = 1;
2109   return 0;
2110 }
2111
2112 #if CONFIG_VP9_TEMPORAL_DENOISING
2113 #if defined(OUTPUT_YUV_DENOISED)
2114 // The denoiser buffer is allocated as a YUV 440 buffer. This function writes it
2115 // as YUV 420. We simply use the top-left pixels of the UV buffers, since we do
2116 // not denoise the UV channels at this time. If ever we implement UV channel
2117 // denoising we will have to modify this.
2118 void vp9_write_yuv_frame_420(YV12_BUFFER_CONFIG *s, FILE *f) {
2119   uint8_t *src = s->y_buffer;
2120   int h = s->y_height;
2121
2122   do {
2123     fwrite(src, s->y_width, 1, f);
2124     src += s->y_stride;
2125   } while (--h);
2126
2127   src = s->u_buffer;
2128   h = s->uv_height / 2;
2129
2130   do {
2131     fwrite(src, s->uv_width / 2, 1, f);
2132     src += s->uv_stride + s->uv_width / 2;
2133   } while (--h);
2134
2135   src = s->v_buffer;
2136   h = s->uv_height / 2;
2137
2138   do {
2139     fwrite(src, s->uv_width / 2, 1, f);
2140     src += s->uv_stride + s->uv_width / 2;
2141   } while (--h);
2142 }
2143 #endif
2144 #endif
2145
2146 #ifdef OUTPUT_YUV_REC
2147 void vp9_write_yuv_rec_frame(VP9_COMMON *cm) {
2148   YV12_BUFFER_CONFIG *s = cm->frame_to_show;
2149   uint8_t *src = s->y_buffer;
2150   int h = cm->height;
2151
2152 #if CONFIG_VP9_HIGHBITDEPTH
2153   if (s->flags & YV12_FLAG_HIGHBITDEPTH) {
2154     uint16_t *src16 = CONVERT_TO_SHORTPTR(s->y_buffer);
2155
2156     do {
2157       fwrite(src16, s->y_width, 2,  yuv_rec_file);
2158       src16 += s->y_stride;
2159     } while (--h);
2160
2161     src16 = CONVERT_TO_SHORTPTR(s->u_buffer);
2162     h = s->uv_height;
2163
2164     do {
2165       fwrite(src16, s->uv_width, 2,  yuv_rec_file);
2166       src16 += s->uv_stride;
2167     } while (--h);
2168
2169     src16 = CONVERT_TO_SHORTPTR(s->v_buffer);
2170     h = s->uv_height;
2171
2172     do {
2173       fwrite(src16, s->uv_width, 2, yuv_rec_file);
2174       src16 += s->uv_stride;
2175     } while (--h);
2176
2177     fflush(yuv_rec_file);
2178     return;
2179   }
2180 #endif  // CONFIG_VP9_HIGHBITDEPTH
2181
2182   do {
2183     fwrite(src, s->y_width, 1,  yuv_rec_file);
2184     src += s->y_stride;
2185   } while (--h);
2186
2187   src = s->u_buffer;
2188   h = s->uv_height;
2189
2190   do {
2191     fwrite(src, s->uv_width, 1,  yuv_rec_file);
2192     src += s->uv_stride;
2193   } while (--h);
2194
2195   src = s->v_buffer;
2196   h = s->uv_height;
2197
2198   do {
2199     fwrite(src, s->uv_width, 1, yuv_rec_file);
2200     src += s->uv_stride;
2201   } while (--h);
2202
2203   fflush(yuv_rec_file);
2204 }
2205 #endif
2206
2207 #if CONFIG_VP9_HIGHBITDEPTH
2208 static void scale_and_extend_frame_nonnormative(const YV12_BUFFER_CONFIG *src,
2209                                                 YV12_BUFFER_CONFIG *dst,
2210                                                 int bd) {
2211 #else
2212 static void scale_and_extend_frame_nonnormative(const YV12_BUFFER_CONFIG *src,
2213                                                 YV12_BUFFER_CONFIG *dst) {
2214 #endif  // CONFIG_VP9_HIGHBITDEPTH
2215   // TODO(dkovalev): replace YV12_BUFFER_CONFIG with vpx_image_t
2216   int i;
2217   const uint8_t *const srcs[3] = {src->y_buffer, src->u_buffer, src->v_buffer};
2218   const int src_strides[3] = {src->y_stride, src->uv_stride, src->uv_stride};
2219   const int src_widths[3] = {src->y_crop_width, src->uv_crop_width,
2220                              src->uv_crop_width };
2221   const int src_heights[3] = {src->y_crop_height, src->uv_crop_height,
2222                               src->uv_crop_height};
2223   uint8_t *const dsts[3] = {dst->y_buffer, dst->u_buffer, dst->v_buffer};
2224   const int dst_strides[3] = {dst->y_stride, dst->uv_stride, dst->uv_stride};
2225   const int dst_widths[3] = {dst->y_crop_width, dst->uv_crop_width,
2226                              dst->uv_crop_width};
2227   const int dst_heights[3] = {dst->y_crop_height, dst->uv_crop_height,
2228                               dst->uv_crop_height};
2229
2230   for (i = 0; i < MAX_MB_PLANE; ++i) {
2231 #if CONFIG_VP9_HIGHBITDEPTH
2232     if (src->flags & YV12_FLAG_HIGHBITDEPTH) {
2233       vp9_highbd_resize_plane(srcs[i], src_heights[i], src_widths[i],
2234                               src_strides[i], dsts[i], dst_heights[i],
2235                               dst_widths[i], dst_strides[i], bd);
2236     } else {
2237       vp9_resize_plane(srcs[i], src_heights[i], src_widths[i], src_strides[i],
2238                        dsts[i], dst_heights[i], dst_widths[i], dst_strides[i]);
2239     }
2240 #else
2241     vp9_resize_plane(srcs[i], src_heights[i], src_widths[i], src_strides[i],
2242                      dsts[i], dst_heights[i], dst_widths[i], dst_strides[i]);
2243 #endif  // CONFIG_VP9_HIGHBITDEPTH
2244   }
2245   vp9_extend_frame_borders(dst);
2246 }
2247
2248 #if CONFIG_VP9_HIGHBITDEPTH
2249 static void scale_and_extend_frame(const YV12_BUFFER_CONFIG *src,
2250                                    YV12_BUFFER_CONFIG *dst, int bd) {
2251 #else
2252 static void scale_and_extend_frame(const YV12_BUFFER_CONFIG *src,
2253                                    YV12_BUFFER_CONFIG *dst) {
2254 #endif  // CONFIG_VP9_HIGHBITDEPTH
2255   const int src_w = src->y_crop_width;
2256   const int src_h = src->y_crop_height;
2257   const int dst_w = dst->y_crop_width;
2258   const int dst_h = dst->y_crop_height;
2259   const uint8_t *const srcs[3] = {src->y_buffer, src->u_buffer, src->v_buffer};
2260   const int src_strides[3] = {src->y_stride, src->uv_stride, src->uv_stride};
2261   uint8_t *const dsts[3] = {dst->y_buffer, dst->u_buffer, dst->v_buffer};
2262   const int dst_strides[3] = {dst->y_stride, dst->uv_stride, dst->uv_stride};
2263   const InterpKernel *const kernel = vp9_get_interp_kernel(EIGHTTAP);
2264   int x, y, i;
2265
2266   for (y = 0; y < dst_h; y += 16) {
2267     for (x = 0; x < dst_w; x += 16) {
2268       for (i = 0; i < MAX_MB_PLANE; ++i) {
2269         const int factor = (i == 0 || i == 3 ? 1 : 2);
2270         const int x_q4 = x * (16 / factor) * src_w / dst_w;
2271         const int y_q4 = y * (16 / factor) * src_h / dst_h;
2272         const int src_stride = src_strides[i];
2273         const int dst_stride = dst_strides[i];
2274         const uint8_t *src_ptr = srcs[i] + (y / factor) * src_h / dst_h *
2275                                      src_stride + (x / factor) * src_w / dst_w;
2276         uint8_t *dst_ptr = dsts[i] + (y / factor) * dst_stride + (x / factor);
2277
2278 #if CONFIG_VP9_HIGHBITDEPTH
2279         if (src->flags & YV12_FLAG_HIGHBITDEPTH) {
2280           vp9_highbd_convolve8(src_ptr, src_stride, dst_ptr, dst_stride,
2281                                kernel[x_q4 & 0xf], 16 * src_w / dst_w,
2282                                kernel[y_q4 & 0xf], 16 * src_h / dst_h,
2283                                16 / factor, 16 / factor, bd);
2284         } else {
2285           vp9_convolve8(src_ptr, src_stride, dst_ptr, dst_stride,
2286                         kernel[x_q4 & 0xf], 16 * src_w / dst_w,
2287                         kernel[y_q4 & 0xf], 16 * src_h / dst_h,
2288                         16 / factor, 16 / factor);
2289         }
2290 #else
2291         vp9_convolve8(src_ptr, src_stride, dst_ptr, dst_stride,
2292                       kernel[x_q4 & 0xf], 16 * src_w / dst_w,
2293                       kernel[y_q4 & 0xf], 16 * src_h / dst_h,
2294                       16 / factor, 16 / factor);
2295 #endif  // CONFIG_VP9_HIGHBITDEPTH
2296       }
2297     }
2298   }
2299
2300   vp9_extend_frame_borders(dst);
2301 }
2302
2303 // Function to test for conditions that indicate we should loop
2304 // back and recode a frame.
2305 static int recode_loop_test(const VP9_COMP *cpi,
2306                             int high_limit, int low_limit,
2307                             int q, int maxq, int minq) {
2308   const RATE_CONTROL *const rc = &cpi->rc;
2309   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
2310   int force_recode = 0;
2311
2312   // Special case trap if maximum allowed frame size exceeded.
2313   if (rc->projected_frame_size > rc->max_frame_bandwidth) {
2314     force_recode = 1;
2315
2316   // Is frame recode allowed.
2317   // Yes if either recode mode 1 is selected or mode 2 is selected
2318   // and the frame is a key frame, golden frame or alt_ref_frame
2319   } else if ((cpi->sf.recode_loop == ALLOW_RECODE) ||
2320              ((cpi->sf.recode_loop == ALLOW_RECODE_KFARFGF) &&
2321                  frame_is_kf_gf_arf(cpi))) {
2322     // General over and under shoot tests
2323     if ((rc->projected_frame_size > high_limit && q < maxq) ||
2324         (rc->projected_frame_size < low_limit && q > minq)) {
2325       force_recode = 1;
2326     } else if (cpi->oxcf.rc_mode == VPX_CQ) {
2327       // Deal with frame undershoot and whether or not we are
2328       // below the automatically set cq level.
2329       if (q > oxcf->cq_level &&
2330           rc->projected_frame_size < ((rc->this_frame_target * 7) >> 3)) {
2331         force_recode = 1;
2332       }
2333     }
2334   }
2335   return force_recode;
2336 }
2337
2338 void vp9_update_reference_frames(VP9_COMP *cpi) {
2339   VP9_COMMON * const cm = &cpi->common;
2340
2341   // At this point the new frame has been encoded.
2342   // If any buffer copy / swapping is signaled it should be done here.
2343   if (cm->frame_type == KEY_FRAME) {
2344     ref_cnt_fb(cm->frame_bufs,
2345                &cm->ref_frame_map[cpi->gld_fb_idx], cm->new_fb_idx);
2346     ref_cnt_fb(cm->frame_bufs,
2347                &cm->ref_frame_map[cpi->alt_fb_idx], cm->new_fb_idx);
2348   } else if (vp9_preserve_existing_gf(cpi)) {
2349     // We have decided to preserve the previously existing golden frame as our
2350     // new ARF frame. However, in the short term in function
2351     // vp9_bitstream.c::get_refresh_mask() we left it in the GF slot and, if
2352     // we're updating the GF with the current decoded frame, we save it to the
2353     // ARF slot instead.
2354     // We now have to update the ARF with the current frame and swap gld_fb_idx
2355     // and alt_fb_idx so that, overall, we've stored the old GF in the new ARF
2356     // slot and, if we're updating the GF, the current frame becomes the new GF.
2357     int tmp;
2358
2359     ref_cnt_fb(cm->frame_bufs,
2360                &cm->ref_frame_map[cpi->alt_fb_idx], cm->new_fb_idx);
2361
2362     tmp = cpi->alt_fb_idx;
2363     cpi->alt_fb_idx = cpi->gld_fb_idx;
2364     cpi->gld_fb_idx = tmp;
2365
2366     if (is_two_pass_svc(cpi)) {
2367       cpi->svc.layer_context[0].gold_ref_idx = cpi->gld_fb_idx;
2368       cpi->svc.layer_context[0].alt_ref_idx = cpi->alt_fb_idx;
2369     }
2370   } else { /* For non key/golden frames */
2371     if (cpi->refresh_alt_ref_frame) {
2372       int arf_idx = cpi->alt_fb_idx;
2373       if ((cpi->oxcf.pass == 2) && cpi->multi_arf_allowed) {
2374         const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
2375         arf_idx = gf_group->arf_update_idx[gf_group->index];
2376       }
2377
2378       ref_cnt_fb(cm->frame_bufs,
2379                  &cm->ref_frame_map[arf_idx], cm->new_fb_idx);
2380       vpx_memcpy(cpi->interp_filter_selected[ALTREF_FRAME],
2381                  cpi->interp_filter_selected[0],
2382                  sizeof(cpi->interp_filter_selected[0]));
2383     }
2384
2385     if (cpi->refresh_golden_frame) {
2386       ref_cnt_fb(cm->frame_bufs,
2387                  &cm->ref_frame_map[cpi->gld_fb_idx], cm->new_fb_idx);
2388       if (!cpi->rc.is_src_frame_alt_ref)
2389         vpx_memcpy(cpi->interp_filter_selected[GOLDEN_FRAME],
2390                    cpi->interp_filter_selected[0],
2391                    sizeof(cpi->interp_filter_selected[0]));
2392       else
2393         vpx_memcpy(cpi->interp_filter_selected[GOLDEN_FRAME],
2394                    cpi->interp_filter_selected[ALTREF_FRAME],
2395                    sizeof(cpi->interp_filter_selected[ALTREF_FRAME]));
2396     }
2397   }
2398
2399   if (cpi->refresh_last_frame) {
2400     ref_cnt_fb(cm->frame_bufs,
2401                &cm->ref_frame_map[cpi->lst_fb_idx], cm->new_fb_idx);
2402     if (!cpi->rc.is_src_frame_alt_ref)
2403       vpx_memcpy(cpi->interp_filter_selected[LAST_FRAME],
2404                  cpi->interp_filter_selected[0],
2405                  sizeof(cpi->interp_filter_selected[0]));
2406   }
2407 #if CONFIG_VP9_TEMPORAL_DENOISING
2408   if (cpi->oxcf.noise_sensitivity > 0) {
2409     vp9_denoiser_update_frame_info(&cpi->denoiser,
2410                                    *cpi->Source,
2411                                    cpi->common.frame_type,
2412                                    cpi->refresh_alt_ref_frame,
2413                                    cpi->refresh_golden_frame,
2414                                    cpi->refresh_last_frame);
2415   }
2416 #endif
2417 }
2418
2419 static void loopfilter_frame(VP9_COMP *cpi, VP9_COMMON *cm) {
2420   MACROBLOCKD *xd = &cpi->td.mb.e_mbd;
2421   struct loopfilter *lf = &cm->lf;
2422   if (xd->lossless) {
2423       lf->filter_level = 0;
2424   } else {
2425     struct vpx_usec_timer timer;
2426
2427     vp9_clear_system_state();
2428
2429     vpx_usec_timer_start(&timer);
2430
2431     vp9_pick_filter_level(cpi->Source, cpi, cpi->sf.lpf_pick);
2432
2433     vpx_usec_timer_mark(&timer);
2434     cpi->time_pick_lpf += vpx_usec_timer_elapsed(&timer);
2435   }
2436
2437   if (lf->filter_level > 0) {
2438     vp9_loop_filter_frame(cm->frame_to_show, cm, xd, lf->filter_level, 0, 0);
2439   }
2440
2441   vp9_extend_frame_inner_borders(cm->frame_to_show);
2442 }
2443
2444 void vp9_scale_references(VP9_COMP *cpi) {
2445   VP9_COMMON *cm = &cpi->common;
2446   MV_REFERENCE_FRAME ref_frame;
2447   const VP9_REFFRAME ref_mask[3] = {VP9_LAST_FLAG, VP9_GOLD_FLAG, VP9_ALT_FLAG};
2448
2449   for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
2450     // Need to convert from VP9_REFFRAME to index into ref_mask (subtract 1).
2451     if (cpi->ref_frame_flags & ref_mask[ref_frame - 1]) {
2452       const int idx = cm->ref_frame_map[get_ref_frame_idx(cpi, ref_frame)];
2453       const YV12_BUFFER_CONFIG *const ref = &cm->frame_bufs[idx].buf;
2454
2455 #if CONFIG_VP9_HIGHBITDEPTH
2456       if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height) {
2457         const int new_fb = get_free_fb(cm);
2458         cm->cur_frame = &cm->frame_bufs[new_fb];
2459         vp9_realloc_frame_buffer(&cm->frame_bufs[new_fb].buf,
2460                                  cm->width, cm->height,
2461                                  cm->subsampling_x, cm->subsampling_y,
2462                                  cm->use_highbitdepth,
2463                                  VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL);
2464         scale_and_extend_frame(ref, &cm->frame_bufs[new_fb].buf,
2465                                (int)cm->bit_depth);
2466 #else
2467       if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height) {
2468         const int new_fb = get_free_fb(cm);
2469         vp9_realloc_frame_buffer(&cm->frame_bufs[new_fb].buf,
2470                                  cm->width, cm->height,
2471                                  cm->subsampling_x, cm->subsampling_y,
2472                                  VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL);
2473         scale_and_extend_frame(ref, &cm->frame_bufs[new_fb].buf);
2474 #endif  // CONFIG_VP9_HIGHBITDEPTH
2475         cpi->scaled_ref_idx[ref_frame - 1] = new_fb;
2476         if (cm->frame_bufs[new_fb].mvs == NULL ||
2477             cm->frame_bufs[new_fb].mi_rows < cm->mi_rows ||
2478             cm->frame_bufs[new_fb].mi_cols < cm->mi_cols) {
2479           cm->frame_bufs[new_fb].mvs =
2480             (MV_REF *)vpx_calloc(cm->mi_rows * cm->mi_cols,
2481                                  sizeof(*cm->frame_bufs[new_fb].mvs));
2482           cm->frame_bufs[new_fb].mi_rows = cm->mi_rows;
2483           cm->frame_bufs[new_fb].mi_cols = cm->mi_cols;
2484         }
2485       } else {
2486         cpi->scaled_ref_idx[ref_frame - 1] = idx;
2487         ++cm->frame_bufs[idx].ref_count;
2488       }
2489     } else {
2490       cpi->scaled_ref_idx[ref_frame - 1] = INVALID_REF_BUFFER_IDX;
2491     }
2492   }
2493 }
2494
2495 static void release_scaled_references(VP9_COMP *cpi) {
2496   VP9_COMMON *cm = &cpi->common;
2497   int i;
2498   for (i = 0; i < MAX_REF_FRAMES; ++i) {
2499     const int idx = cpi->scaled_ref_idx[i];
2500     RefCntBuffer *const buf =
2501         idx != INVALID_REF_BUFFER_IDX ? &cm->frame_bufs[idx] : NULL;
2502     if (buf != NULL) {
2503       --buf->ref_count;
2504       cpi->scaled_ref_idx[i] = INVALID_REF_BUFFER_IDX;
2505     }
2506   }
2507 }
2508
2509 static void full_to_model_count(unsigned int *model_count,
2510                                 unsigned int *full_count) {
2511   int n;
2512   model_count[ZERO_TOKEN] = full_count[ZERO_TOKEN];
2513   model_count[ONE_TOKEN] = full_count[ONE_TOKEN];
2514   model_count[TWO_TOKEN] = full_count[TWO_TOKEN];
2515   for (n = THREE_TOKEN; n < EOB_TOKEN; ++n)
2516     model_count[TWO_TOKEN] += full_count[n];
2517   model_count[EOB_MODEL_TOKEN] = full_count[EOB_TOKEN];
2518 }
2519
2520 static void full_to_model_counts(vp9_coeff_count_model *model_count,
2521                                  vp9_coeff_count *full_count) {
2522   int i, j, k, l;
2523
2524   for (i = 0; i < PLANE_TYPES; ++i)
2525     for (j = 0; j < REF_TYPES; ++j)
2526       for (k = 0; k < COEF_BANDS; ++k)
2527         for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l)
2528           full_to_model_count(model_count[i][j][k][l], full_count[i][j][k][l]);
2529 }
2530
2531 #if 0 && CONFIG_INTERNAL_STATS
2532 static void output_frame_level_debug_stats(VP9_COMP *cpi) {
2533   VP9_COMMON *const cm = &cpi->common;
2534   FILE *const f = fopen("tmp.stt", cm->current_video_frame ? "a" : "w");
2535   int recon_err;
2536
2537   vp9_clear_system_state();
2538
2539   recon_err = vp9_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
2540
2541   if (cpi->twopass.total_left_stats.coded_error != 0.0)
2542     fprintf(f, "%10u %10d %10d %10d %10d"
2543         "%10"PRId64" %10"PRId64" %10"PRId64" %10"PRId64" %10d "
2544         "%7.2lf %7.2lf %7.2lf %7.2lf %7.2lf"
2545         "%6d %6d %5d %5d %5d "
2546         "%10"PRId64" %10.3lf"
2547         "%10lf %8u %10d %10d %10d\n",
2548         cpi->common.current_video_frame, cpi->rc.this_frame_target,
2549         cpi->rc.projected_frame_size,
2550         cpi->rc.projected_frame_size / cpi->common.MBs,
2551         (cpi->rc.projected_frame_size - cpi->rc.this_frame_target),
2552         cpi->rc.vbr_bits_off_target,
2553         cpi->rc.total_target_vs_actual,
2554         (cpi->rc.starting_buffer_level - cpi->rc.bits_off_target),
2555         cpi->rc.total_actual_bits, cm->base_qindex,
2556         vp9_convert_qindex_to_q(cm->base_qindex, cm->bit_depth),
2557         (double)vp9_dc_quant(cm->base_qindex, 0, cm->bit_depth) / 4.0,
2558         vp9_convert_qindex_to_q(cpi->twopass.active_worst_quality,
2559                                 cm->bit_depth),
2560         cpi->rc.avg_q,
2561         vp9_convert_qindex_to_q(cpi->oxcf.cq_level, cm->bit_depth),
2562         cpi->refresh_last_frame, cpi->refresh_golden_frame,
2563         cpi->refresh_alt_ref_frame, cm->frame_type, cpi->rc.gfu_boost,
2564         cpi->twopass.bits_left,
2565         cpi->twopass.total_left_stats.coded_error,
2566         cpi->twopass.bits_left /
2567             (1 + cpi->twopass.total_left_stats.coded_error),
2568         cpi->tot_recode_hits, recon_err, cpi->rc.kf_boost,
2569         cpi->twopass.kf_zeromotion_pct);
2570
2571   fclose(f);
2572
2573   if (0) {
2574     FILE *const fmodes = fopen("Modes.stt", "a");
2575     int i;
2576
2577     fprintf(fmodes, "%6d:%1d:%1d:%1d ", cpi->common.current_video_frame,
2578             cm->frame_type, cpi->refresh_golden_frame,
2579             cpi->refresh_alt_ref_frame);
2580
2581     for (i = 0; i < MAX_MODES; ++i)
2582       fprintf(fmodes, "%5d ", cpi->mode_chosen_counts[i]);
2583
2584     fprintf(fmodes, "\n");
2585
2586     fclose(fmodes);
2587   }
2588 }
2589 #endif
2590
2591 static void set_mv_search_params(VP9_COMP *cpi) {
2592   const VP9_COMMON *const cm = &cpi->common;
2593   const unsigned int max_mv_def = MIN(cm->width, cm->height);
2594
2595   // Default based on max resolution.
2596   cpi->mv_step_param = vp9_init_search_range(max_mv_def);
2597
2598   if (cpi->sf.mv.auto_mv_step_size) {
2599     if (frame_is_intra_only(cm)) {
2600       // Initialize max_mv_magnitude for use in the first INTER frame
2601       // after a key/intra-only frame.
2602       cpi->max_mv_magnitude = max_mv_def;
2603     } else {
2604       if (cm->show_frame) {
2605         // Allow mv_steps to correspond to twice the max mv magnitude found
2606         // in the previous frame, capped by the default max_mv_magnitude based
2607         // on resolution.
2608         cpi->mv_step_param =
2609             vp9_init_search_range(MIN(max_mv_def, 2 * cpi->max_mv_magnitude));
2610       }
2611       cpi->max_mv_magnitude = 0;
2612     }
2613   }
2614 }
2615
2616 static void set_size_independent_vars(VP9_COMP *cpi) {
2617   vp9_set_speed_features_framesize_independent(cpi);
2618   vp9_set_rd_speed_thresholds(cpi);
2619   vp9_set_rd_speed_thresholds_sub8x8(cpi);
2620   cpi->common.interp_filter = cpi->sf.default_interp_filter;
2621 }
2622
2623 static void set_size_dependent_vars(VP9_COMP *cpi, int *q,
2624                                     int *bottom_index, int *top_index) {
2625   VP9_COMMON *const cm = &cpi->common;
2626   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
2627
2628   // Setup variables that depend on the dimensions of the frame.
2629   vp9_set_speed_features_framesize_dependent(cpi);
2630
2631   // Decide q and q bounds.
2632   *q = vp9_rc_pick_q_and_bounds(cpi, bottom_index, top_index);
2633
2634   if (!frame_is_intra_only(cm)) {
2635     vp9_set_high_precision_mv(cpi, (*q) < HIGH_PRECISION_MV_QTHRESH);
2636   }
2637
2638   // Configure experimental use of segmentation for enhanced coding of
2639   // static regions if indicated.
2640   // Only allowed in the second pass of a two pass encode, as it requires
2641   // lagged coding, and if the relevant speed feature flag is set.
2642   if (oxcf->pass == 2 && cpi->sf.static_segmentation)
2643     configure_static_seg_features(cpi);
2644
2645 #if CONFIG_VP9_POSTPROC
2646   if (oxcf->noise_sensitivity > 0) {
2647     int l = 0;
2648     switch (oxcf->noise_sensitivity) {
2649       case 1:
2650         l = 20;
2651         break;
2652       case 2:
2653         l = 40;
2654         break;
2655       case 3:
2656         l = 60;
2657         break;
2658       case 4:
2659       case 5:
2660         l = 100;
2661         break;
2662       case 6:
2663         l = 150;
2664         break;
2665     }
2666     vp9_denoise(cpi->Source, cpi->Source, l);
2667   }
2668 #endif  // CONFIG_VP9_POSTPROC
2669 }
2670
2671 static void init_motion_estimation(VP9_COMP *cpi) {
2672   int y_stride = cpi->scaled_source.y_stride;
2673
2674   if (cpi->sf.mv.search_method == NSTEP) {
2675     vp9_init3smotion_compensation(&cpi->ss_cfg, y_stride);
2676   } else if (cpi->sf.mv.search_method == DIAMOND) {
2677     vp9_init_dsmotion_compensation(&cpi->ss_cfg, y_stride);
2678   }
2679 }
2680
2681 void set_frame_size(VP9_COMP *cpi) {
2682   int ref_frame;
2683   VP9_COMMON *const cm = &cpi->common;
2684   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
2685   MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
2686
2687   if (oxcf->pass == 2 &&
2688       cm->current_video_frame == 0 &&
2689       oxcf->resize_mode == RESIZE_FIXED &&
2690       oxcf->rc_mode == VPX_VBR) {
2691     // Internal scaling is triggered on the first frame.
2692     vp9_set_size_literal(cpi, oxcf->scaled_frame_width,
2693                          oxcf->scaled_frame_height);
2694   }
2695
2696   if ((oxcf->pass == 2) &&
2697       (!cpi->use_svc ||
2698           (is_two_pass_svc(cpi) &&
2699               cpi->svc.encode_empty_frame_state != ENCODING))) {
2700     vp9_set_target_rate(cpi);
2701   }
2702
2703   // Reset the frame pointers to the current frame size.
2704   vp9_realloc_frame_buffer(get_frame_new_buffer(cm),
2705                            cm->width, cm->height,
2706                            cm->subsampling_x, cm->subsampling_y,
2707 #if CONFIG_VP9_HIGHBITDEPTH
2708                            cm->use_highbitdepth,
2709 #endif
2710                            VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL);
2711
2712   alloc_util_frame_buffers(cpi);
2713   init_motion_estimation(cpi);
2714
2715   for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
2716     const int idx = cm->ref_frame_map[get_ref_frame_idx(cpi, ref_frame)];
2717     YV12_BUFFER_CONFIG *const buf = &cm->frame_bufs[idx].buf;
2718     RefBuffer *const ref_buf = &cm->frame_refs[ref_frame - 1];
2719     ref_buf->buf = buf;
2720     ref_buf->idx = idx;
2721 #if CONFIG_VP9_HIGHBITDEPTH
2722     vp9_setup_scale_factors_for_frame(&ref_buf->sf,
2723                                       buf->y_crop_width, buf->y_crop_height,
2724                                       cm->width, cm->height,
2725                                       (buf->flags & YV12_FLAG_HIGHBITDEPTH) ?
2726                                           1 : 0);
2727 #else
2728     vp9_setup_scale_factors_for_frame(&ref_buf->sf,
2729                                       buf->y_crop_width, buf->y_crop_height,
2730                                       cm->width, cm->height);
2731 #endif  // CONFIG_VP9_HIGHBITDEPTH
2732     if (vp9_is_scaled(&ref_buf->sf))
2733       vp9_extend_frame_borders(buf);
2734   }
2735
2736   set_ref_ptrs(cm, xd, LAST_FRAME, LAST_FRAME);
2737 }
2738
2739 static void encode_without_recode_loop(VP9_COMP *cpi) {
2740   VP9_COMMON *const cm = &cpi->common;
2741   int q, bottom_index, top_index;  // Dummy variables.
2742
2743   vp9_clear_system_state();
2744
2745   set_frame_size(cpi);
2746
2747   cpi->Source = vp9_scale_if_required(cm, cpi->un_scaled_source,
2748                                       &cpi->scaled_source);
2749
2750   if (cpi->unscaled_last_source != NULL)
2751     cpi->Last_Source = vp9_scale_if_required(cm, cpi->unscaled_last_source,
2752                                              &cpi->scaled_last_source);
2753
2754   if (frame_is_intra_only(cm) == 0) {
2755     vp9_scale_references(cpi);
2756   }
2757
2758   set_size_independent_vars(cpi);
2759   set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
2760
2761   vp9_set_quantizer(cm, q);
2762   setup_frame(cpi);
2763   // Variance adaptive and in frame q adjustment experiments are mutually
2764   // exclusive.
2765   if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
2766     vp9_vaq_frame_setup(cpi);
2767   } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
2768     vp9_setup_in_frame_q_adj(cpi);
2769   } else if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) {
2770     vp9_cyclic_refresh_setup(cpi);
2771   }
2772   // transform / motion compensation build reconstruction frame
2773   vp9_encode_frame(cpi);
2774
2775   // Update the skip mb flag probabilities based on the distribution
2776   // seen in the last encoder iteration.
2777   // update_base_skip_probs(cpi);
2778   vp9_clear_system_state();
2779 }
2780
2781 static void encode_with_recode_loop(VP9_COMP *cpi,
2782                                     size_t *size,
2783                                     uint8_t *dest) {
2784   VP9_COMMON *const cm = &cpi->common;
2785   RATE_CONTROL *const rc = &cpi->rc;
2786   int bottom_index, top_index;
2787   int loop_count = 0;
2788   int loop = 0;
2789   int overshoot_seen = 0;
2790   int undershoot_seen = 0;
2791   int frame_over_shoot_limit;
2792   int frame_under_shoot_limit;
2793   int q = 0, q_low = 0, q_high = 0;
2794   int frame_size_changed = 0;
2795
2796   set_size_independent_vars(cpi);
2797
2798   do {
2799     vp9_clear_system_state();
2800
2801     set_frame_size(cpi);
2802
2803     if (loop_count == 0 || frame_size_changed != 0) {
2804       set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
2805       q_low = bottom_index;
2806       q_high = top_index;
2807
2808       // TODO(agrange) Scale cpi->max_mv_magnitude if frame-size has changed.
2809       set_mv_search_params(cpi);
2810     }
2811
2812     // Decide frame size bounds
2813     vp9_rc_compute_frame_size_bounds(cpi, rc->this_frame_target,
2814                                      &frame_under_shoot_limit,
2815                                      &frame_over_shoot_limit);
2816
2817     cpi->Source = vp9_scale_if_required(cm, cpi->un_scaled_source,
2818                                       &cpi->scaled_source);
2819
2820     if (cpi->unscaled_last_source != NULL)
2821       cpi->Last_Source = vp9_scale_if_required(cm, cpi->unscaled_last_source,
2822                                                &cpi->scaled_last_source);
2823
2824     if (frame_is_intra_only(cm) == 0) {
2825       if (loop_count > 0) {
2826         release_scaled_references(cpi);
2827       }
2828       vp9_scale_references(cpi);
2829     }
2830
2831     vp9_set_quantizer(cm, q);
2832
2833     if (loop_count == 0)
2834       setup_frame(cpi);
2835
2836     // Variance adaptive and in frame q adjustment experiments are mutually
2837     // exclusive.
2838     if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
2839       vp9_vaq_frame_setup(cpi);
2840     } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
2841       vp9_setup_in_frame_q_adj(cpi);
2842     }
2843
2844     // transform / motion compensation build reconstruction frame
2845     vp9_encode_frame(cpi);
2846
2847     // Update the skip mb flag probabilities based on the distribution
2848     // seen in the last encoder iteration.
2849     // update_base_skip_probs(cpi);
2850
2851     vp9_clear_system_state();
2852
2853     // Dummy pack of the bitstream using up to date stats to get an
2854     // accurate estimate of output frame size to determine if we need
2855     // to recode.
2856     if (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF) {
2857       save_coding_context(cpi);
2858       if (!cpi->sf.use_nonrd_pick_mode)
2859         vp9_pack_bitstream(cpi, dest, size);
2860
2861       rc->projected_frame_size = (int)(*size) << 3;
2862       restore_coding_context(cpi);
2863
2864       if (frame_over_shoot_limit == 0)
2865         frame_over_shoot_limit = 1;
2866     }
2867
2868     if (cpi->oxcf.rc_mode == VPX_Q) {
2869       loop = 0;
2870     } else {
2871       if ((cm->frame_type == KEY_FRAME) &&
2872            rc->this_key_frame_forced &&
2873            (rc->projected_frame_size < rc->max_frame_bandwidth)) {
2874         int last_q = q;
2875         int kf_err;
2876
2877         int high_err_target = cpi->ambient_err;
2878         int low_err_target = cpi->ambient_err >> 1;
2879
2880 #if CONFIG_VP9_HIGHBITDEPTH
2881         if (cm->use_highbitdepth) {
2882           kf_err = vp9_highbd_get_y_sse(cpi->Source, get_frame_new_buffer(cm),
2883                                         cm->bit_depth);
2884         } else {
2885           kf_err = vp9_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
2886         }
2887 #else
2888         kf_err = vp9_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
2889 #endif  // CONFIG_VP9_HIGHBITDEPTH
2890
2891         // Prevent possible divide by zero error below for perfect KF
2892         kf_err += !kf_err;
2893
2894         // The key frame is not good enough or we can afford
2895         // to make it better without undue risk of popping.
2896         if ((kf_err > high_err_target &&
2897              rc->projected_frame_size <= frame_over_shoot_limit) ||
2898             (kf_err > low_err_target &&
2899              rc->projected_frame_size <= frame_under_shoot_limit)) {
2900           // Lower q_high
2901           q_high = q > q_low ? q - 1 : q_low;
2902
2903           // Adjust Q
2904           q = (q * high_err_target) / kf_err;
2905           q = MIN(q, (q_high + q_low) >> 1);
2906         } else if (kf_err < low_err_target &&
2907                    rc->projected_frame_size >= frame_under_shoot_limit) {
2908           // The key frame is much better than the previous frame
2909           // Raise q_low
2910           q_low = q < q_high ? q + 1 : q_high;
2911
2912           // Adjust Q
2913           q = (q * low_err_target) / kf_err;
2914           q = MIN(q, (q_high + q_low + 1) >> 1);
2915         }
2916
2917         // Clamp Q to upper and lower limits:
2918         q = clamp(q, q_low, q_high);
2919
2920         loop = q != last_q;
2921       } else if (recode_loop_test(
2922           cpi, frame_over_shoot_limit, frame_under_shoot_limit,
2923           q, MAX(q_high, top_index), bottom_index)) {
2924         // Is the projected frame size out of range and are we allowed
2925         // to attempt to recode.
2926         int last_q = q;
2927         int retries = 0;
2928
2929         // Frame size out of permitted range:
2930         // Update correction factor & compute new Q to try...
2931
2932         // Frame is too large
2933         if (rc->projected_frame_size > rc->this_frame_target) {
2934           // Special case if the projected size is > the max allowed.
2935           if (rc->projected_frame_size >= rc->max_frame_bandwidth)
2936             q_high = rc->worst_quality;
2937
2938           // Raise Qlow as to at least the current value
2939           q_low = q < q_high ? q + 1 : q_high;
2940
2941           if (undershoot_seen || loop_count > 1) {
2942             // Update rate_correction_factor unless
2943             vp9_rc_update_rate_correction_factors(cpi, 1);
2944
2945             q = (q_high + q_low + 1) / 2;
2946           } else {
2947             // Update rate_correction_factor unless
2948             vp9_rc_update_rate_correction_factors(cpi, 0);
2949
2950             q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
2951                                    bottom_index, MAX(q_high, top_index));
2952
2953             while (q < q_low && retries < 10) {
2954               vp9_rc_update_rate_correction_factors(cpi, 0);
2955               q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
2956                                      bottom_index, MAX(q_high, top_index));
2957               retries++;
2958             }
2959           }
2960
2961           overshoot_seen = 1;
2962         } else {
2963           // Frame is too small
2964           q_high = q > q_low ? q - 1 : q_low;
2965
2966           if (overshoot_seen || loop_count > 1) {
2967             vp9_rc_update_rate_correction_factors(cpi, 1);
2968             q = (q_high + q_low) / 2;
2969           } else {
2970             vp9_rc_update_rate_correction_factors(cpi, 0);
2971             q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
2972                                    bottom_index, top_index);
2973             // Special case reset for qlow for constrained quality.
2974             // This should only trigger where there is very substantial
2975             // undershoot on a frame and the auto cq level is above
2976             // the user passsed in value.
2977             if (cpi->oxcf.rc_mode == VPX_CQ &&
2978                 q < q_low) {
2979               q_low = q;
2980             }
2981
2982             while (q > q_high && retries < 10) {
2983               vp9_rc_update_rate_correction_factors(cpi, 0);
2984               q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
2985                                      bottom_index, top_index);
2986               retries++;
2987             }
2988           }
2989
2990           undershoot_seen = 1;
2991         }
2992
2993         // Clamp Q to upper and lower limits:
2994         q = clamp(q, q_low, q_high);
2995
2996         loop = q != last_q;
2997       } else {
2998         loop = 0;
2999       }
3000     }
3001
3002     // Special case for overlay frame.
3003     if (rc->is_src_frame_alt_ref &&
3004         rc->projected_frame_size < rc->max_frame_bandwidth)
3005       loop = 0;
3006
3007     if (loop) {
3008       loop_count++;
3009
3010 #if CONFIG_INTERNAL_STATS
3011       cpi->tot_recode_hits++;
3012 #endif
3013     }
3014   } while (loop);
3015 }
3016
3017 static int get_ref_frame_flags(const VP9_COMP *cpi) {
3018   const int *const map = cpi->common.ref_frame_map;
3019   const int gold_is_last = map[cpi->gld_fb_idx] == map[cpi->lst_fb_idx];
3020   const int alt_is_last = map[cpi->alt_fb_idx] == map[cpi->lst_fb_idx];
3021   const int gold_is_alt = map[cpi->gld_fb_idx] == map[cpi->alt_fb_idx];
3022   int flags = VP9_ALT_FLAG | VP9_GOLD_FLAG | VP9_LAST_FLAG;
3023
3024   if (gold_is_last)
3025     flags &= ~VP9_GOLD_FLAG;
3026
3027   if (cpi->rc.frames_till_gf_update_due == INT_MAX &&
3028       (cpi->svc.number_temporal_layers == 1 &&
3029        cpi->svc.number_spatial_layers == 1))
3030     flags &= ~VP9_GOLD_FLAG;
3031
3032   if (alt_is_last)
3033     flags &= ~VP9_ALT_FLAG;
3034
3035   if (gold_is_alt)
3036     flags &= ~VP9_ALT_FLAG;
3037
3038   return flags;
3039 }
3040
3041 static void set_ext_overrides(VP9_COMP *cpi) {
3042   // Overrides the defaults with the externally supplied values with
3043   // vp9_update_reference() and vp9_update_entropy() calls
3044   // Note: The overrides are valid only for the next frame passed
3045   // to encode_frame_to_data_rate() function
3046   if (cpi->ext_refresh_frame_context_pending) {
3047     cpi->common.refresh_frame_context = cpi->ext_refresh_frame_context;
3048     cpi->ext_refresh_frame_context_pending = 0;
3049   }
3050   if (cpi->ext_refresh_frame_flags_pending) {
3051     cpi->refresh_last_frame = cpi->ext_refresh_last_frame;
3052     cpi->refresh_golden_frame = cpi->ext_refresh_golden_frame;
3053     cpi->refresh_alt_ref_frame = cpi->ext_refresh_alt_ref_frame;
3054     cpi->ext_refresh_frame_flags_pending = 0;
3055   }
3056 }
3057
3058 YV12_BUFFER_CONFIG *vp9_scale_if_required(VP9_COMMON *cm,
3059                                           YV12_BUFFER_CONFIG *unscaled,
3060                                           YV12_BUFFER_CONFIG *scaled) {
3061   if (cm->mi_cols * MI_SIZE != unscaled->y_width ||
3062       cm->mi_rows * MI_SIZE != unscaled->y_height) {
3063 #if CONFIG_VP9_HIGHBITDEPTH
3064     scale_and_extend_frame_nonnormative(unscaled, scaled, (int)cm->bit_depth);
3065 #else
3066     scale_and_extend_frame_nonnormative(unscaled, scaled);
3067 #endif  // CONFIG_VP9_HIGHBITDEPTH
3068     return scaled;
3069   } else {
3070     return unscaled;
3071   }
3072 }
3073
3074 static void set_arf_sign_bias(VP9_COMP *cpi) {
3075   VP9_COMMON *const cm = &cpi->common;
3076   int arf_sign_bias;
3077
3078   if ((cpi->oxcf.pass == 2) && cpi->multi_arf_allowed) {
3079     const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
3080     arf_sign_bias = cpi->rc.source_alt_ref_active &&
3081                     (!cpi->refresh_alt_ref_frame ||
3082                      (gf_group->rf_level[gf_group->index] == GF_ARF_LOW));
3083   } else {
3084     arf_sign_bias =
3085       (cpi->rc.source_alt_ref_active && !cpi->refresh_alt_ref_frame);
3086   }
3087   cm->ref_frame_sign_bias[ALTREF_FRAME] = arf_sign_bias;
3088 }
3089
3090 int setup_interp_filter_search_mask(VP9_COMP *cpi) {
3091   INTERP_FILTER ifilter;
3092   int ref_total[MAX_REF_FRAMES] = {0};
3093   MV_REFERENCE_FRAME ref;
3094   int mask = 0;
3095   if (cpi->common.last_frame_type == KEY_FRAME ||
3096       cpi->refresh_alt_ref_frame)
3097     return mask;
3098   for (ref = LAST_FRAME; ref <= ALTREF_FRAME; ++ref)
3099     for (ifilter = EIGHTTAP; ifilter <= EIGHTTAP_SHARP; ++ifilter)
3100       ref_total[ref] += cpi->interp_filter_selected[ref][ifilter];
3101
3102   for (ifilter = EIGHTTAP; ifilter <= EIGHTTAP_SHARP; ++ifilter) {
3103     if ((ref_total[LAST_FRAME] &&
3104         cpi->interp_filter_selected[LAST_FRAME][ifilter] == 0) &&
3105         (ref_total[GOLDEN_FRAME] == 0 ||
3106          cpi->interp_filter_selected[GOLDEN_FRAME][ifilter] * 50
3107            < ref_total[GOLDEN_FRAME]) &&
3108         (ref_total[ALTREF_FRAME] == 0 ||
3109          cpi->interp_filter_selected[ALTREF_FRAME][ifilter] * 50
3110            < ref_total[ALTREF_FRAME]))
3111       mask |= 1 << ifilter;
3112   }
3113   return mask;
3114 }
3115
3116 static void encode_frame_to_data_rate(VP9_COMP *cpi,
3117                                       size_t *size,
3118                                       uint8_t *dest,
3119                                       unsigned int *frame_flags) {
3120   VP9_COMMON *const cm = &cpi->common;
3121   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
3122   struct segmentation *const seg = &cm->seg;
3123   TX_SIZE t;
3124
3125   set_ext_overrides(cpi);
3126
3127   vp9_clear_system_state();
3128
3129   // Enable or disable mode based tweaking of the zbin.
3130   // For 2 pass only used where GF/ARF prediction quality
3131   // is above a threshold.
3132   cpi->zbin_mode_boost = 0;
3133   cpi->zbin_mode_boost_enabled = 0;
3134
3135   // Set the arf sign bias for this frame.
3136   set_arf_sign_bias(cpi);
3137
3138   // Set default state for segment based loop filter update flags.
3139   cm->lf.mode_ref_delta_update = 0;
3140
3141   if (cpi->oxcf.pass == 2 &&
3142       cpi->sf.adaptive_interp_filter_search)
3143     cpi->sf.interp_filter_search_mask =
3144         setup_interp_filter_search_mask(cpi);
3145
3146   // Set various flags etc to special state if it is a key frame.
3147   if (frame_is_intra_only(cm)) {
3148     // Reset the loop filter deltas and segmentation map.
3149     vp9_reset_segment_features(&cm->seg);
3150
3151     // If segmentation is enabled force a map update for key frames.
3152     if (seg->enabled) {
3153       seg->update_map = 1;
3154       seg->update_data = 1;
3155     }
3156
3157     // The alternate reference frame cannot be active for a key frame.
3158     cpi->rc.source_alt_ref_active = 0;
3159
3160     cm->error_resilient_mode = oxcf->error_resilient_mode;
3161
3162     // By default, encoder assumes decoder can use prev_mi.
3163     if (cm->error_resilient_mode) {
3164       cm->frame_parallel_decoding_mode = 1;
3165       cm->reset_frame_context = 0;
3166       cm->refresh_frame_context = 0;
3167     } else if (cm->intra_only) {
3168       cm->frame_parallel_decoding_mode = oxcf->frame_parallel_decoding_mode;
3169       // Only reset the current context.
3170       cm->reset_frame_context = 2;
3171     }
3172   }
3173   if (is_two_pass_svc(cpi) && cm->error_resilient_mode == 0) {
3174     // Use the last frame context for the empty frame.
3175     cm->frame_context_idx =
3176         (cpi->svc.encode_empty_frame_state == ENCODING) ? FRAME_CONTEXTS - 1 :
3177         cpi->svc.spatial_layer_id * cpi->svc.number_temporal_layers +
3178         cpi->svc.temporal_layer_id;
3179
3180     // The probs will be updated based on the frame type of its previous
3181     // frame if frame_parallel_decoding_mode is 0. The type may vary for
3182     // the frame after a key frame in base layer since we may drop enhancement
3183     // layers. So set frame_parallel_decoding_mode to 1 in this case.
3184     if (cpi->svc.number_temporal_layers == 1) {
3185       if (cpi->svc.spatial_layer_id == 0 &&
3186           cpi->svc.layer_context[0].last_frame_type == KEY_FRAME)
3187         cm->frame_parallel_decoding_mode = 1;
3188       else
3189         cm->frame_parallel_decoding_mode = 0;
3190     } else if (cpi->svc.spatial_layer_id == 0) {
3191       // Find the 2nd frame in temporal base layer and 1st frame in temporal
3192       // enhancement layers from the key frame.
3193       int i;
3194       for (i = 0; i < cpi->svc.number_temporal_layers; ++i) {
3195         if (cpi->svc.layer_context[0].frames_from_key_frame == 1 << i) {
3196           cm->frame_parallel_decoding_mode = 1;
3197           break;
3198         }
3199       }
3200       if (i == cpi->svc.number_temporal_layers)
3201         cm->frame_parallel_decoding_mode = 0;
3202     }
3203   }
3204
3205   // For 1 pass CBR, check if we are dropping this frame.
3206   // Never drop on key frame.
3207   if (oxcf->pass == 0 &&
3208       oxcf->rc_mode == VPX_CBR &&
3209       cm->frame_type != KEY_FRAME) {
3210     if (vp9_rc_drop_frame(cpi)) {
3211       vp9_rc_postencode_update_drop_frame(cpi);
3212       ++cm->current_video_frame;
3213       return;
3214     }
3215   }
3216
3217   vp9_clear_system_state();
3218
3219 #if CONFIG_INTERNAL_STATS
3220   {
3221     int i;
3222     for (i = 0; i < MAX_MODES; ++i)
3223       cpi->mode_chosen_counts[i] = 0;
3224   }
3225 #endif
3226
3227   if (cpi->sf.recode_loop == DISALLOW_RECODE) {
3228     encode_without_recode_loop(cpi);
3229   } else {
3230     encode_with_recode_loop(cpi, size, dest);
3231   }
3232
3233 #if CONFIG_VP9_TEMPORAL_DENOISING
3234 #ifdef OUTPUT_YUV_DENOISED
3235   if (oxcf->noise_sensitivity > 0) {
3236     vp9_write_yuv_frame_420(&cpi->denoiser.running_avg_y[INTRA_FRAME],
3237                             yuv_denoised_file);
3238   }
3239 #endif
3240 #endif
3241
3242
3243   // Special case code to reduce pulsing when key frames are forced at a
3244   // fixed interval. Note the reconstruction error if it is the frame before
3245   // the force key frame
3246   if (cpi->rc.next_key_frame_forced && cpi->rc.frames_to_key == 1) {
3247 #if CONFIG_VP9_HIGHBITDEPTH
3248     if (cm->use_highbitdepth) {
3249       cpi->ambient_err = vp9_highbd_get_y_sse(cpi->Source,
3250                                               get_frame_new_buffer(cm),
3251                                               cm->bit_depth);
3252     } else {
3253       cpi->ambient_err = vp9_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
3254     }
3255 #else
3256     cpi->ambient_err = vp9_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
3257 #endif  // CONFIG_VP9_HIGHBITDEPTH
3258   }
3259
3260   // If the encoder forced a KEY_FRAME decision
3261   if (cm->frame_type == KEY_FRAME)
3262     cpi->refresh_last_frame = 1;
3263
3264   cm->frame_to_show = get_frame_new_buffer(cm);
3265
3266   // Pick the loop filter level for the frame.
3267   loopfilter_frame(cpi, cm);
3268
3269   // build the bitstream
3270   vp9_pack_bitstream(cpi, dest, size);
3271
3272   if (cm->seg.update_map)
3273     update_reference_segmentation_map(cpi);
3274
3275   if (frame_is_intra_only(cm) == 0) {
3276     release_scaled_references(cpi);
3277   }
3278   vp9_update_reference_frames(cpi);
3279
3280   for (t = TX_4X4; t <= TX_32X32; t++)
3281     full_to_model_counts(cpi->td.counts->coef[t],
3282                          cpi->td.rd_counts.coef_counts[t]);
3283
3284   if (!cm->error_resilient_mode && !cm->frame_parallel_decoding_mode)
3285     vp9_adapt_coef_probs(cm);
3286
3287   if (!frame_is_intra_only(cm)) {
3288     if (!cm->error_resilient_mode && !cm->frame_parallel_decoding_mode) {
3289       vp9_adapt_mode_probs(cm);
3290       vp9_adapt_mv_probs(cm, cm->allow_high_precision_mv);
3291     }
3292   }
3293
3294   if (cpi->refresh_golden_frame == 1)
3295     cpi->frame_flags |= FRAMEFLAGS_GOLDEN;
3296   else
3297     cpi->frame_flags &= ~FRAMEFLAGS_GOLDEN;
3298
3299   if (cpi->refresh_alt_ref_frame == 1)
3300     cpi->frame_flags |= FRAMEFLAGS_ALTREF;
3301   else
3302     cpi->frame_flags &= ~FRAMEFLAGS_ALTREF;
3303
3304   cpi->ref_frame_flags = get_ref_frame_flags(cpi);
3305
3306   cm->last_frame_type = cm->frame_type;
3307
3308   if (!(is_two_pass_svc(cpi) && cpi->svc.encode_empty_frame_state == ENCODING))
3309     vp9_rc_postencode_update(cpi, *size);
3310
3311 #if 0
3312   output_frame_level_debug_stats(cpi);
3313 #endif
3314
3315   if (cm->frame_type == KEY_FRAME) {
3316     // Tell the caller that the frame was coded as a key frame
3317     *frame_flags = cpi->frame_flags | FRAMEFLAGS_KEY;
3318   } else {
3319     *frame_flags = cpi->frame_flags & ~FRAMEFLAGS_KEY;
3320   }
3321
3322   // Clear the one shot update flags for segmentation map and mode/ref loop
3323   // filter deltas.
3324   cm->seg.update_map = 0;
3325   cm->seg.update_data = 0;
3326   cm->lf.mode_ref_delta_update = 0;
3327
3328   // keep track of the last coded dimensions
3329   cm->last_width = cm->width;
3330   cm->last_height = cm->height;
3331
3332   // reset to normal state now that we are done.
3333   if (!cm->show_existing_frame)
3334     cm->last_show_frame = cm->show_frame;
3335
3336   if (cm->show_frame) {
3337     vp9_swap_mi_and_prev_mi(cm);
3338     // Don't increment frame counters if this was an altref buffer
3339     // update not a real frame
3340     ++cm->current_video_frame;
3341     if (cpi->use_svc)
3342       vp9_inc_frame_in_layer(cpi);
3343   }
3344   cm->prev_frame = cm->cur_frame;
3345
3346   if (is_two_pass_svc(cpi))
3347     cpi->svc.layer_context[cpi->svc.spatial_layer_id].last_frame_type =
3348         cm->frame_type;
3349 }
3350
3351 static void SvcEncode(VP9_COMP *cpi, size_t *size, uint8_t *dest,
3352                       unsigned int *frame_flags) {
3353   vp9_rc_get_svc_params(cpi);
3354   encode_frame_to_data_rate(cpi, size, dest, frame_flags);
3355 }
3356
3357 static void Pass0Encode(VP9_COMP *cpi, size_t *size, uint8_t *dest,
3358                         unsigned int *frame_flags) {
3359   if (cpi->oxcf.rc_mode == VPX_CBR) {
3360     vp9_rc_get_one_pass_cbr_params(cpi);
3361   } else {
3362     vp9_rc_get_one_pass_vbr_params(cpi);
3363   }
3364   encode_frame_to_data_rate(cpi, size, dest, frame_flags);
3365 }
3366
3367 static void Pass2Encode(VP9_COMP *cpi, size_t *size,
3368                         uint8_t *dest, unsigned int *frame_flags) {
3369   cpi->allow_encode_breakout = ENCODE_BREAKOUT_ENABLED;
3370   encode_frame_to_data_rate(cpi, size, dest, frame_flags);
3371
3372   if (!(is_two_pass_svc(cpi) && cpi->svc.encode_empty_frame_state == ENCODING))
3373     vp9_twopass_postencode_update(cpi);
3374 }
3375
3376 static void check_initial_width(VP9_COMP *cpi,
3377 #if CONFIG_VP9_HIGHBITDEPTH
3378                                 int use_highbitdepth,
3379 #endif
3380                                 int subsampling_x, int subsampling_y) {
3381   VP9_COMMON *const cm = &cpi->common;
3382
3383   if (!cpi->initial_width) {
3384     cm->subsampling_x = subsampling_x;
3385     cm->subsampling_y = subsampling_y;
3386 #if CONFIG_VP9_HIGHBITDEPTH
3387     cm->use_highbitdepth = use_highbitdepth;
3388 #endif
3389
3390     alloc_raw_frame_buffers(cpi);
3391     alloc_ref_frame_buffers(cpi);
3392     alloc_util_frame_buffers(cpi);
3393
3394     init_motion_estimation(cpi);  // TODO(agrange) This can be removed.
3395
3396     cpi->initial_width = cm->width;
3397     cpi->initial_height = cm->height;
3398     cpi->initial_mbs = cm->MBs;
3399   }
3400 }
3401
3402
3403 int vp9_receive_raw_frame(VP9_COMP *cpi, unsigned int frame_flags,
3404                           YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
3405                           int64_t end_time) {
3406   VP9_COMMON *cm = &cpi->common;
3407   struct vpx_usec_timer timer;
3408   int res = 0;
3409   const int subsampling_x = sd->subsampling_x;
3410   const int subsampling_y = sd->subsampling_y;
3411 #if CONFIG_VP9_HIGHBITDEPTH
3412   const int use_highbitdepth = sd->flags & YV12_FLAG_HIGHBITDEPTH;
3413   check_initial_width(cpi, use_highbitdepth, subsampling_x, subsampling_y);
3414 #else
3415   check_initial_width(cpi, subsampling_x, subsampling_y);
3416 #endif  // CONFIG_VP9_HIGHBITDEPTH
3417
3418   vpx_usec_timer_start(&timer);
3419
3420   if (vp9_lookahead_push(cpi->lookahead, sd, time_stamp, end_time, frame_flags))
3421     res = -1;
3422   vpx_usec_timer_mark(&timer);
3423   cpi->time_receive_data += vpx_usec_timer_elapsed(&timer);
3424
3425   if ((cm->profile == PROFILE_0 || cm->profile == PROFILE_2) &&
3426       (subsampling_x != 1 || subsampling_y != 1)) {
3427     vpx_internal_error(&cm->error, VPX_CODEC_INVALID_PARAM,
3428                        "Non-4:2:0 color space requires profile 1 or 3");
3429     res = -1;
3430   }
3431   if ((cm->profile == PROFILE_1 || cm->profile == PROFILE_3) &&
3432       (subsampling_x == 1 && subsampling_y == 1)) {
3433     vpx_internal_error(&cm->error, VPX_CODEC_INVALID_PARAM,
3434                        "4:2:0 color space requires profile 0 or 2");
3435     res = -1;
3436   }
3437
3438   return res;
3439 }
3440
3441
3442 static int frame_is_reference(const VP9_COMP *cpi) {
3443   const VP9_COMMON *cm = &cpi->common;
3444
3445   return cm->frame_type == KEY_FRAME ||
3446          cpi->refresh_last_frame ||
3447          cpi->refresh_golden_frame ||
3448          cpi->refresh_alt_ref_frame ||
3449          cm->refresh_frame_context ||
3450          cm->lf.mode_ref_delta_update ||
3451          cm->seg.update_map ||
3452          cm->seg.update_data;
3453 }
3454
3455 void adjust_frame_rate(VP9_COMP *cpi,
3456                        const struct lookahead_entry *source) {
3457   int64_t this_duration;
3458   int step = 0;
3459
3460   if (source->ts_start == cpi->first_time_stamp_ever) {
3461     this_duration = source->ts_end - source->ts_start;
3462     step = 1;
3463   } else {
3464     int64_t last_duration = cpi->last_end_time_stamp_seen
3465         - cpi->last_time_stamp_seen;
3466
3467     this_duration = source->ts_end - cpi->last_end_time_stamp_seen;
3468
3469     // do a step update if the duration changes by 10%
3470     if (last_duration)
3471       step = (int)((this_duration - last_duration) * 10 / last_duration);
3472   }
3473
3474   if (this_duration) {
3475     if (step) {
3476       vp9_new_framerate(cpi, 10000000.0 / this_duration);
3477     } else {
3478       // Average this frame's rate into the last second's average
3479       // frame rate. If we haven't seen 1 second yet, then average
3480       // over the whole interval seen.
3481       const double interval = MIN((double)(source->ts_end
3482                                    - cpi->first_time_stamp_ever), 10000000.0);
3483       double avg_duration = 10000000.0 / cpi->framerate;
3484       avg_duration *= (interval - avg_duration + this_duration);
3485       avg_duration /= interval;
3486
3487       vp9_new_framerate(cpi, 10000000.0 / avg_duration);
3488     }
3489   }
3490   cpi->last_time_stamp_seen = source->ts_start;
3491   cpi->last_end_time_stamp_seen = source->ts_end;
3492 }
3493
3494 // Returns 0 if this is not an alt ref else the offset of the source frame
3495 // used as the arf midpoint.
3496 static int get_arf_src_index(VP9_COMP *cpi) {
3497   RATE_CONTROL *const rc = &cpi->rc;
3498   int arf_src_index = 0;
3499   if (is_altref_enabled(cpi)) {
3500     if (cpi->oxcf.pass == 2) {
3501       const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
3502       if (gf_group->update_type[gf_group->index] == ARF_UPDATE) {
3503         arf_src_index = gf_group->arf_src_offset[gf_group->index];
3504       }
3505     } else if (rc->source_alt_ref_pending) {
3506       arf_src_index = rc->frames_till_gf_update_due;
3507     }
3508   }
3509   return arf_src_index;
3510 }
3511
3512 static void check_src_altref(VP9_COMP *cpi,
3513                              const struct lookahead_entry *source) {
3514   RATE_CONTROL *const rc = &cpi->rc;
3515
3516   if (cpi->oxcf.pass == 2) {
3517     const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
3518     rc->is_src_frame_alt_ref =
3519       (gf_group->update_type[gf_group->index] == OVERLAY_UPDATE);
3520   } else {
3521     rc->is_src_frame_alt_ref = cpi->alt_ref_source &&
3522                                (source == cpi->alt_ref_source);
3523   }
3524
3525   if (rc->is_src_frame_alt_ref) {
3526     // Current frame is an ARF overlay frame.
3527     cpi->alt_ref_source = NULL;
3528
3529     // Don't refresh the last buffer for an ARF overlay frame. It will
3530     // become the GF so preserve last as an alternative prediction option.
3531     cpi->refresh_last_frame = 0;
3532   }
3533 }
3534
3535 int vp9_get_compressed_data(VP9_COMP *cpi, unsigned int *frame_flags,
3536                             size_t *size, uint8_t *dest,
3537                             int64_t *time_stamp, int64_t *time_end, int flush) {
3538   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
3539   VP9_COMMON *const cm = &cpi->common;
3540   RATE_CONTROL *const rc = &cpi->rc;
3541   struct vpx_usec_timer  cmptimer;
3542   YV12_BUFFER_CONFIG *force_src_buffer = NULL;
3543   struct lookahead_entry *last_source = NULL;
3544   struct lookahead_entry *source = NULL;
3545   int arf_src_index;
3546   int i;
3547
3548   if (is_two_pass_svc(cpi)) {
3549 #if CONFIG_SPATIAL_SVC
3550     vp9_svc_start_frame(cpi);
3551     // Use a small empty frame instead of a real frame
3552     if (cpi->svc.encode_empty_frame_state == ENCODING)
3553       source = &cpi->svc.empty_frame;
3554 #endif
3555     if (oxcf->pass == 2)
3556       vp9_restore_layer_context(cpi);
3557   }
3558
3559   vpx_usec_timer_start(&cmptimer);
3560
3561   vp9_set_high_precision_mv(cpi, ALTREF_HIGH_PRECISION_MV);
3562
3563   // Is multi-arf enabled.
3564   // Note that at the moment multi_arf is only configured for 2 pass VBR and
3565   // will not work properly with svc.
3566   if ((oxcf->pass == 2) && !cpi->use_svc &&
3567       (cpi->oxcf.enable_auto_arf > 1))
3568     cpi->multi_arf_allowed = 1;
3569   else
3570     cpi->multi_arf_allowed = 0;
3571
3572   // Normal defaults
3573   cm->reset_frame_context = 0;
3574   cm->refresh_frame_context = 1;
3575   cpi->refresh_last_frame = 1;
3576   cpi->refresh_golden_frame = 0;
3577   cpi->refresh_alt_ref_frame = 0;
3578
3579   // Should we encode an arf frame.
3580   arf_src_index = get_arf_src_index(cpi);
3581
3582   // Skip alt frame if we encode the empty frame
3583   if (is_two_pass_svc(cpi) && source != NULL)
3584     arf_src_index = 0;
3585
3586   if (arf_src_index) {
3587     assert(arf_src_index <= rc->frames_to_key);
3588
3589     if ((source = vp9_lookahead_peek(cpi->lookahead, arf_src_index)) != NULL) {
3590       cpi->alt_ref_source = source;
3591
3592 #if CONFIG_SPATIAL_SVC
3593       if (is_two_pass_svc(cpi) && cpi->svc.spatial_layer_id > 0) {
3594         int i;
3595         // Reference a hidden frame from a lower layer
3596         for (i = cpi->svc.spatial_layer_id - 1; i >= 0; --i) {
3597           if (oxcf->ss_enable_auto_arf[i]) {
3598             cpi->gld_fb_idx = cpi->svc.layer_context[i].alt_ref_idx;
3599             break;
3600           }
3601         }
3602       }
3603       cpi->svc.layer_context[cpi->svc.spatial_layer_id].has_alt_frame = 1;
3604 #endif
3605
3606       if (oxcf->arnr_max_frames > 0) {
3607         // Produce the filtered ARF frame.
3608         vp9_temporal_filter(cpi, arf_src_index);
3609         vp9_extend_frame_borders(&cpi->alt_ref_buffer);
3610         force_src_buffer = &cpi->alt_ref_buffer;
3611       }
3612
3613       cm->show_frame = 0;
3614       cpi->refresh_alt_ref_frame = 1;
3615       cpi->refresh_golden_frame = 0;
3616       cpi->refresh_last_frame = 0;
3617       rc->is_src_frame_alt_ref = 0;
3618       rc->source_alt_ref_pending = 0;
3619     } else {
3620       rc->source_alt_ref_pending = 0;
3621     }
3622   }
3623
3624   if (!source) {
3625     // Get last frame source.
3626     if (cm->current_video_frame > 0) {
3627       if ((last_source = vp9_lookahead_peek(cpi->lookahead, -1)) == NULL)
3628         return -1;
3629     }
3630
3631     // Read in the source frame.
3632 #if CONFIG_SPATIAL_SVC
3633     if (is_two_pass_svc(cpi))
3634       source = vp9_svc_lookahead_pop(cpi, cpi->lookahead, flush);
3635     else
3636 #endif
3637       source = vp9_lookahead_pop(cpi->lookahead, flush);
3638     if (source != NULL) {
3639       cm->show_frame = 1;
3640       cm->intra_only = 0;
3641       // if the flags indicate intra frame, but if the current picture is for
3642       // non-zero spatial layer, it should not be an intra picture.
3643       // TODO(Won Kap): this needs to change if per-layer intra frame is
3644       // allowed.
3645       if ((source->flags | VPX_EFLAG_FORCE_KF) && cpi->svc.spatial_layer_id) {
3646         source->flags &= ~(unsigned int)(VPX_EFLAG_FORCE_KF);
3647       }
3648
3649       // Check to see if the frame should be encoded as an arf overlay.
3650       check_src_altref(cpi, source);
3651     }
3652   }
3653
3654   if (source) {
3655     cpi->un_scaled_source = cpi->Source = force_src_buffer ? force_src_buffer
3656                                                            : &source->img;
3657
3658     cpi->unscaled_last_source = last_source != NULL ? &last_source->img : NULL;
3659
3660     *time_stamp = source->ts_start;
3661     *time_end = source->ts_end;
3662     *frame_flags = (source->flags & VPX_EFLAG_FORCE_KF) ? FRAMEFLAGS_KEY : 0;
3663
3664   } else {
3665     *size = 0;
3666     if (flush && oxcf->pass == 1 && !cpi->twopass.first_pass_done) {
3667       vp9_end_first_pass(cpi);    /* get last stats packet */
3668       cpi->twopass.first_pass_done = 1;
3669     }
3670     return -1;
3671   }
3672
3673   if (source->ts_start < cpi->first_time_stamp_ever) {
3674     cpi->first_time_stamp_ever = source->ts_start;
3675     cpi->last_end_time_stamp_seen = source->ts_start;
3676   }
3677
3678   // Clear down mmx registers
3679   vp9_clear_system_state();
3680
3681   // adjust frame rates based on timestamps given
3682   if (cm->show_frame) {
3683     adjust_frame_rate(cpi, source);
3684   }
3685
3686   if (cpi->svc.number_temporal_layers > 1 &&
3687       oxcf->rc_mode == VPX_CBR) {
3688     vp9_update_temporal_layer_framerate(cpi);
3689     vp9_restore_layer_context(cpi);
3690   }
3691
3692   // Find a free buffer for the new frame, releasing the reference previously
3693   // held.
3694   cm->frame_bufs[cm->new_fb_idx].ref_count--;
3695   cm->new_fb_idx = get_free_fb(cm);
3696   cm->cur_frame = &cm->frame_bufs[cm->new_fb_idx];
3697
3698   if (!cpi->use_svc && cpi->multi_arf_allowed) {
3699     if (cm->frame_type == KEY_FRAME) {
3700       init_buffer_indices(cpi);
3701     } else if (oxcf->pass == 2) {
3702       const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
3703       cpi->alt_fb_idx = gf_group->arf_ref_idx[gf_group->index];
3704     }
3705   }
3706
3707   // Start with a 0 size frame.
3708   *size = 0;
3709
3710   cpi->frame_flags = *frame_flags;
3711
3712   if ((oxcf->pass == 2) &&
3713       (!cpi->use_svc ||
3714           (is_two_pass_svc(cpi) &&
3715               cpi->svc.encode_empty_frame_state != ENCODING))) {
3716     vp9_rc_get_second_pass_params(cpi);
3717   } else {
3718     set_frame_size(cpi);
3719   }
3720
3721   for (i = 0; i < MAX_REF_FRAMES; ++i)
3722     cpi->scaled_ref_idx[i] = INVALID_REF_BUFFER_IDX;
3723
3724   if (oxcf->pass == 1 &&
3725       (!cpi->use_svc || is_two_pass_svc(cpi))) {
3726     const int lossless = is_lossless_requested(oxcf);
3727 #if CONFIG_VP9_HIGHBITDEPTH
3728     if (cpi->oxcf.use_highbitdepth)
3729       cpi->td.mb.fwd_txm4x4 = lossless ?
3730           vp9_highbd_fwht4x4 : vp9_highbd_fdct4x4;
3731     else
3732       cpi->td.mb.fwd_txm4x4 = lossless ? vp9_fwht4x4 : vp9_fdct4x4;
3733     cpi->td.mb.highbd_itxm_add = lossless ? vp9_highbd_iwht4x4_add :
3734                                          vp9_highbd_idct4x4_add;
3735 #else
3736     cpi->td.mb.fwd_txm4x4 = lossless ? vp9_fwht4x4 : vp9_fdct4x4;
3737 #endif  // CONFIG_VP9_HIGHBITDEPTH
3738     cpi->td.mb.itxm_add = lossless ? vp9_iwht4x4_add : vp9_idct4x4_add;
3739     vp9_first_pass(cpi, source);
3740   } else if (oxcf->pass == 2 &&
3741       (!cpi->use_svc || is_two_pass_svc(cpi))) {
3742     Pass2Encode(cpi, size, dest, frame_flags);
3743   } else if (cpi->use_svc) {
3744     SvcEncode(cpi, size, dest, frame_flags);
3745   } else {
3746     // One pass encode
3747     Pass0Encode(cpi, size, dest, frame_flags);
3748   }
3749
3750   if (cm->refresh_frame_context)
3751     cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
3752
3753   // No frame encoded, or frame was dropped, release scaled references.
3754   if ((*size == 0) && (frame_is_intra_only(cm) == 0)) {
3755     release_scaled_references(cpi);
3756   }
3757
3758   if (*size > 0) {
3759     cpi->droppable = !frame_is_reference(cpi);
3760   }
3761
3762   // Save layer specific state.
3763   if ((cpi->svc.number_temporal_layers > 1 &&
3764        oxcf->rc_mode == VPX_CBR) ||
3765       ((cpi->svc.number_temporal_layers > 1 ||
3766         cpi->svc.number_spatial_layers > 1) &&
3767        oxcf->pass == 2)) {
3768     vp9_save_layer_context(cpi);
3769   }
3770
3771   vpx_usec_timer_mark(&cmptimer);
3772   cpi->time_compress_data += vpx_usec_timer_elapsed(&cmptimer);
3773
3774   if (cpi->b_calculate_psnr && oxcf->pass != 1 && cm->show_frame)
3775     generate_psnr_packet(cpi);
3776
3777 #if CONFIG_INTERNAL_STATS
3778
3779   if (oxcf->pass != 1) {
3780     cpi->bytes += (int)(*size);
3781
3782     if (cm->show_frame) {
3783       cpi->count++;
3784
3785       if (cpi->b_calculate_psnr) {
3786         YV12_BUFFER_CONFIG *orig = cpi->Source;
3787         YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
3788         YV12_BUFFER_CONFIG *pp = &cm->post_proc_buffer;
3789         PSNR_STATS psnr;
3790 #if CONFIG_VP9_HIGHBITDEPTH
3791         calc_highbd_psnr(orig, recon, &psnr, cpi->td.mb.e_mbd.bd,
3792                          cpi->oxcf.input_bit_depth);
3793 #else
3794         calc_psnr(orig, recon, &psnr);
3795 #endif  // CONFIG_VP9_HIGHBITDEPTH
3796
3797         cpi->total += psnr.psnr[0];
3798         cpi->total_y += psnr.psnr[1];
3799         cpi->total_u += psnr.psnr[2];
3800         cpi->total_v += psnr.psnr[3];
3801         cpi->total_sq_error += psnr.sse[0];
3802         cpi->total_samples += psnr.samples[0];
3803
3804         {
3805           PSNR_STATS psnr2;
3806           double frame_ssim2 = 0, weight = 0;
3807 #if CONFIG_VP9_POSTPROC
3808           // TODO(agrange) Add resizing of post-proc buffer in here when the
3809           // encoder is changed to use on-demand buffer allocation.
3810           vp9_deblock(cm->frame_to_show, &cm->post_proc_buffer,
3811                       cm->lf.filter_level * 10 / 6);
3812 #endif
3813           vp9_clear_system_state();
3814
3815 #if CONFIG_VP9_HIGHBITDEPTH
3816           calc_highbd_psnr(orig, pp, &psnr, cpi->td.mb.e_mbd.bd,
3817                            cpi->oxcf.input_bit_depth);
3818 #else
3819           calc_psnr(orig, pp, &psnr2);
3820 #endif  // CONFIG_VP9_HIGHBITDEPTH
3821
3822           cpi->totalp += psnr2.psnr[0];
3823           cpi->totalp_y += psnr2.psnr[1];
3824           cpi->totalp_u += psnr2.psnr[2];
3825           cpi->totalp_v += psnr2.psnr[3];
3826           cpi->totalp_sq_error += psnr2.sse[0];
3827           cpi->totalp_samples += psnr2.samples[0];
3828
3829 #if CONFIG_VP9_HIGHBITDEPTH
3830           if (cm->use_highbitdepth) {
3831             frame_ssim2 = vp9_highbd_calc_ssim(orig, recon, &weight, xd->bd);
3832           } else {
3833             frame_ssim2 = vp9_calc_ssim(orig, recon, &weight);
3834           }
3835 #else
3836           frame_ssim2 = vp9_calc_ssim(orig, recon, &weight);
3837 #endif  // CONFIG_VP9_HIGHBITDEPTH
3838
3839           cpi->summed_quality += frame_ssim2 * weight;
3840           cpi->summed_weights += weight;
3841
3842 #if CONFIG_VP9_HIGHBITDEPTH
3843           if (cm->use_highbitdepth) {
3844             frame_ssim2 = vp9_highbd_calc_ssim(
3845                 orig, &cm->post_proc_buffer, &weight, xd->bd);
3846           } else {
3847             frame_ssim2 = vp9_calc_ssim(orig, &cm->post_proc_buffer, &weight);
3848           }
3849 #else
3850           frame_ssim2 = vp9_calc_ssim(orig, &cm->post_proc_buffer, &weight);
3851 #endif  // CONFIG_VP9_HIGHBITDEPTH
3852
3853           cpi->summedp_quality += frame_ssim2 * weight;
3854           cpi->summedp_weights += weight;
3855 #if 0
3856           {
3857             FILE *f = fopen("q_used.stt", "a");
3858             fprintf(f, "%5d : Y%f7.3:U%f7.3:V%f7.3:F%f7.3:S%7.3f\n",
3859                     cpi->common.current_video_frame, y2, u2, v2,
3860                     frame_psnr2, frame_ssim2);
3861             fclose(f);
3862           }
3863 #endif
3864         }
3865       }
3866
3867
3868       if (cpi->b_calculate_ssimg) {
3869         double y, u, v, frame_all;
3870 #if CONFIG_VP9_HIGHBITDEPTH
3871         if (cm->use_highbitdepth) {
3872           frame_all = vp9_highbd_calc_ssimg(cpi->Source, cm->frame_to_show, &y,
3873                                             &u, &v, xd->bd);
3874         } else {
3875           frame_all = vp9_calc_ssimg(cpi->Source, cm->frame_to_show, &y, &u,
3876                                      &v);
3877         }
3878 #else
3879         frame_all = vp9_calc_ssimg(cpi->Source, cm->frame_to_show, &y, &u, &v);
3880 #endif  // CONFIG_VP9_HIGHBITDEPTH
3881         cpi->total_ssimg_y += y;
3882         cpi->total_ssimg_u += u;
3883         cpi->total_ssimg_v += v;
3884         cpi->total_ssimg_all += frame_all;
3885       }
3886     }
3887   }
3888
3889 #endif
3890
3891   if (is_two_pass_svc(cpi)) {
3892     if (cpi->svc.encode_empty_frame_state == ENCODING)
3893       cpi->svc.encode_empty_frame_state = ENCODED;
3894
3895     if (cm->show_frame) {
3896       ++cpi->svc.spatial_layer_to_encode;
3897       if (cpi->svc.spatial_layer_to_encode >= cpi->svc.number_spatial_layers)
3898         cpi->svc.spatial_layer_to_encode = 0;
3899
3900       // May need the empty frame after an visible frame.
3901       cpi->svc.encode_empty_frame_state = NEED_TO_ENCODE;
3902     }
3903   }
3904   return 0;
3905 }
3906
3907 int vp9_get_preview_raw_frame(VP9_COMP *cpi, YV12_BUFFER_CONFIG *dest,
3908                               vp9_ppflags_t *flags) {
3909   VP9_COMMON *cm = &cpi->common;
3910 #if !CONFIG_VP9_POSTPROC
3911   (void)flags;
3912 #endif
3913
3914   if (!cm->show_frame) {
3915     return -1;
3916   } else {
3917     int ret;
3918 #if CONFIG_VP9_POSTPROC
3919     ret = vp9_post_proc_frame(cm, dest, flags);
3920 #else
3921     if (cm->frame_to_show) {
3922       *dest = *cm->frame_to_show;
3923       dest->y_width = cm->width;
3924       dest->y_height = cm->height;
3925       dest->uv_width = cm->width >> cm->subsampling_x;
3926       dest->uv_height = cm->height >> cm->subsampling_y;
3927       ret = 0;
3928     } else {
3929       ret = -1;
3930     }
3931 #endif  // !CONFIG_VP9_POSTPROC
3932     vp9_clear_system_state();
3933     return ret;
3934   }
3935 }
3936
3937 int vp9_set_active_map(VP9_COMP *cpi, unsigned char *map, int rows, int cols) {
3938   if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols) {
3939     const int mi_rows = cpi->common.mi_rows;
3940     const int mi_cols = cpi->common.mi_cols;
3941     if (map) {
3942       int r, c;
3943       for (r = 0; r < mi_rows; r++) {
3944         for (c = 0; c < mi_cols; c++) {
3945           cpi->segmentation_map[r * mi_cols + c] =
3946               !map[(r >> 1) * cols + (c >> 1)];
3947         }
3948       }
3949       vp9_enable_segfeature(&cpi->common.seg, 1, SEG_LVL_SKIP);
3950       vp9_enable_segmentation(&cpi->common.seg);
3951     } else {
3952       vp9_disable_segmentation(&cpi->common.seg);
3953     }
3954     return 0;
3955   } else {
3956     return -1;
3957   }
3958 }
3959
3960 int vp9_set_internal_size(VP9_COMP *cpi,
3961                           VPX_SCALING horiz_mode, VPX_SCALING vert_mode) {
3962   VP9_COMMON *cm = &cpi->common;
3963   int hr = 0, hs = 0, vr = 0, vs = 0;
3964
3965   if (horiz_mode > ONETWO || vert_mode > ONETWO)
3966     return -1;
3967
3968   Scale2Ratio(horiz_mode, &hr, &hs);
3969   Scale2Ratio(vert_mode, &vr, &vs);
3970
3971   // always go to the next whole number
3972   cm->width = (hs - 1 + cpi->oxcf.width * hr) / hs;
3973   cm->height = (vs - 1 + cpi->oxcf.height * vr) / vs;
3974   assert(cm->width <= cpi->initial_width);
3975   assert(cm->height <= cpi->initial_height);
3976
3977   update_frame_size(cpi);
3978
3979   return 0;
3980 }
3981
3982 int vp9_set_size_literal(VP9_COMP *cpi, unsigned int width,
3983                          unsigned int height) {
3984   VP9_COMMON *cm = &cpi->common;
3985 #if CONFIG_VP9_HIGHBITDEPTH
3986   check_initial_width(cpi, 1, 1, cm->use_highbitdepth);
3987 #else
3988   check_initial_width(cpi, 1, 1);
3989 #endif  // CONFIG_VP9_HIGHBITDEPTH
3990
3991   if (width) {
3992     cm->width = width;
3993     if (cm->width > cpi->initial_width) {
3994       cm->width = cpi->initial_width;
3995       printf("Warning: Desired width too large, changed to %d\n", cm->width);
3996     }
3997   }
3998
3999   if (height) {
4000     cm->height = height;
4001     if (cm->height > cpi->initial_height) {
4002       cm->height = cpi->initial_height;
4003       printf("Warning: Desired height too large, changed to %d\n", cm->height);
4004     }
4005   }
4006   assert(cm->width <= cpi->initial_width);
4007   assert(cm->height <= cpi->initial_height);
4008
4009   update_frame_size(cpi);
4010
4011   return 0;
4012 }
4013
4014 void vp9_set_svc(VP9_COMP *cpi, int use_svc) {
4015   cpi->use_svc = use_svc;
4016   return;
4017 }
4018
4019 int vp9_get_y_sse(const YV12_BUFFER_CONFIG *a, const YV12_BUFFER_CONFIG *b) {
4020   assert(a->y_crop_width == b->y_crop_width);
4021   assert(a->y_crop_height == b->y_crop_height);
4022
4023   return (int)get_sse(a->y_buffer, a->y_stride, b->y_buffer, b->y_stride,
4024                       a->y_crop_width, a->y_crop_height);
4025 }
4026
4027 #if CONFIG_VP9_HIGHBITDEPTH
4028 int vp9_highbd_get_y_sse(const YV12_BUFFER_CONFIG *a,
4029                          const YV12_BUFFER_CONFIG *b,
4030                          vpx_bit_depth_t bit_depth) {
4031   unsigned int sse;
4032   int sum;
4033   assert(a->y_crop_width == b->y_crop_width);
4034   assert(a->y_crop_height == b->y_crop_height);
4035   assert((a->flags & YV12_FLAG_HIGHBITDEPTH) != 0);
4036   assert((b->flags & YV12_FLAG_HIGHBITDEPTH) != 0);
4037   switch (bit_depth) {
4038     case VPX_BITS_8:
4039       highbd_variance(a->y_buffer, a->y_stride, b->y_buffer, b->y_stride,
4040                       a->y_crop_width, a->y_crop_height, &sse, &sum);
4041       return (int) sse;
4042     case VPX_BITS_10:
4043       highbd_10_variance(a->y_buffer, a->y_stride, b->y_buffer, b->y_stride,
4044                          a->y_crop_width, a->y_crop_height, &sse, &sum);
4045       return (int) sse;
4046     case VPX_BITS_12:
4047       highbd_12_variance(a->y_buffer, a->y_stride, b->y_buffer, b->y_stride,
4048                          a->y_crop_width, a->y_crop_height, &sse, &sum);
4049       return (int) sse;
4050     default:
4051       assert(0 && "bit_depth should be VPX_BITS_8, VPX_BITS_10 or VPX_BITS_12");
4052       return -1;
4053   }
4054 }
4055 #endif  // CONFIG_VP9_HIGHBITDEPTH
4056
4057 int vp9_get_quantizer(VP9_COMP *cpi) {
4058   return cpi->common.base_qindex;
4059 }
4060
4061 void vp9_apply_encoding_flags(VP9_COMP *cpi, vpx_enc_frame_flags_t flags) {
4062   if (flags & (VP8_EFLAG_NO_REF_LAST | VP8_EFLAG_NO_REF_GF |
4063                VP8_EFLAG_NO_REF_ARF)) {
4064     int ref = 7;
4065
4066     if (flags & VP8_EFLAG_NO_REF_LAST)
4067       ref ^= VP9_LAST_FLAG;
4068
4069     if (flags & VP8_EFLAG_NO_REF_GF)
4070       ref ^= VP9_GOLD_FLAG;
4071
4072     if (flags & VP8_EFLAG_NO_REF_ARF)
4073       ref ^= VP9_ALT_FLAG;
4074
4075     vp9_use_as_reference(cpi, ref);
4076   }
4077
4078   if (flags & (VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF |
4079                VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_FORCE_GF |
4080                VP8_EFLAG_FORCE_ARF)) {
4081     int upd = 7;
4082
4083     if (flags & VP8_EFLAG_NO_UPD_LAST)
4084       upd ^= VP9_LAST_FLAG;
4085
4086     if (flags & VP8_EFLAG_NO_UPD_GF)
4087       upd ^= VP9_GOLD_FLAG;
4088
4089     if (flags & VP8_EFLAG_NO_UPD_ARF)
4090       upd ^= VP9_ALT_FLAG;
4091
4092     vp9_update_reference(cpi, upd);
4093   }
4094
4095   if (flags & VP8_EFLAG_NO_UPD_ENTROPY) {
4096     vp9_update_entropy(cpi, 0);
4097   }
4098 }