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