15c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)/*
25c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
35c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *
45c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *  Use of this source code is governed by a BSD-style license
55c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *  that can be found in the LICENSE file in the root of the source
65c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *  tree. An additional intellectual property rights grant can be found
75c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *  in the file PATENTS.  All contributing project authors may
85c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *  be found in the AUTHORS file in the root of the source tree.
95c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) */
105c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
115c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include <math.h>
125c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include <stdio.h>
135c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include <limits.h>
145c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
155c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "./vpx_config.h"
165c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "./vpx_scale_rtcd.h"
175c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "vpx/internal/vpx_psnr.h"
185c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "vpx_ports/vpx_timer.h"
195c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
205c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "vp9/common/vp9_alloccommon.h"
215c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "vp9/common/vp9_filter.h"
225c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "vp9/common/vp9_idct.h"
235c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#if CONFIG_VP9_POSTPROC
245c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "vp9/common/vp9_postproc.h"
255c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#endif
265c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "vp9/common/vp9_reconinter.h"
275c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "vp9/common/vp9_reconintra.h"
285c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "vp9/common/vp9_systemdependent.h"
29d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)#include "vp9/common/vp9_tile_common.h"
3010f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch
315c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "vp9/encoder/vp9_aq_complexity.h"
32c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)#include "vp9/encoder/vp9_aq_cyclicrefresh.h"
335c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "vp9/encoder/vp9_aq_variance.h"
345c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "vp9/encoder/vp9_bitstream.h"
355c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "vp9/encoder/vp9_context_tree.h"
3609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)#include "vp9/encoder/vp9_encodeframe.h"
377242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci#include "vp9/encoder/vp9_encodemv.h"
385c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "vp9/encoder/vp9_firstpass.h"
3909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)#include "vp9/encoder/vp9_mbgraph.h"
4007a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch#include "vp9/encoder/vp9_encoder.h"
415c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "vp9/encoder/vp9_picklpf.h"
42a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch#include "vp9/encoder/vp9_ratectrl.h"
435c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "vp9/encoder/vp9_rd.h"
4407a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch#include "vp9/encoder/vp9_segmentation.h"
455c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "vp9/encoder/vp9_speed_features.h"
46a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch#if CONFIG_INTERNAL_STATS
475c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "vp9/encoder/vp9_ssim.h"
485c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#endif
495c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "vp9/encoder/vp9_temporal_filter.h"
505c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "vp9/encoder/vp9_resize.h"
515c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "vp9/encoder/vp9_svc_layercontext.h"
525c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
5353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)void vp9_coef_tree_initialize();
5453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
5553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#define SHARP_FILTER_QTHRESH 0          /* Q threshold for 8-tap sharp filter */
5653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
5753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#define ALTREF_HIGH_PRECISION_MV 1      // Whether to use high precision mv
5809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)                                         //  for altref computation.
595c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#define HIGH_PRECISION_MV_QTHRESH 200   // Q threshold for high precision
60d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)                                         // mv. Choose a very high value for
61d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)                                         // now so that HIGH_PRECISION is always
625c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)                                         // chosen.
635c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
645c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// #define OUTPUT_YUV_REC
655c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
667242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci#ifdef OUTPUT_YUV_DENOISED
675c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)FILE *yuv_denoised_file = NULL;
685c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#endif
6909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)#ifdef OUTPUT_YUV_REC
701e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)FILE *yuv_rec_file;
71c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)#endif
725c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
735c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#if 0
74FILE *framepsnr;
75FILE *kf_list;
76FILE *keyfile;
77#endif
78
79static 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
105void 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
117static 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
142void vp9_initialize_enc() {
143  static int init_done = 0;
144
145  if (!init_done) {
146    vp9_rtcd();
147    vp9_init_neighbors();
148    vp9_init_intra_predictors();
149    vp9_coef_tree_initialize();
150    vp9_tokenize_initialize();
151    vp9_init_me_luts();
152    vp9_rc_init_minq_luts();
153    vp9_entropy_mv_init();
154    vp9_entropy_mode_init();
155    vp9_temporal_filter_init();
156    init_done = 1;
157  }
158}
159
160static void dealloc_compressor_data(VP9_COMP *cpi) {
161  VP9_COMMON *const cm = &cpi->common;
162  int i;
163
164  // Delete sementation map
165  vpx_free(cpi->segmentation_map);
166  cpi->segmentation_map = NULL;
167  vpx_free(cm->last_frame_seg_map);
168  cm->last_frame_seg_map = NULL;
169  vpx_free(cpi->coding_context.last_frame_seg_map_copy);
170  cpi->coding_context.last_frame_seg_map_copy = NULL;
171
172  vpx_free(cpi->complexity_map);
173  cpi->complexity_map = NULL;
174
175  vpx_free(cpi->nmvcosts[0]);
176  vpx_free(cpi->nmvcosts[1]);
177  cpi->nmvcosts[0] = NULL;
178  cpi->nmvcosts[1] = NULL;
179
180  vpx_free(cpi->nmvcosts_hp[0]);
181  vpx_free(cpi->nmvcosts_hp[1]);
182  cpi->nmvcosts_hp[0] = NULL;
183  cpi->nmvcosts_hp[1] = NULL;
184
185  vpx_free(cpi->nmvsadcosts[0]);
186  vpx_free(cpi->nmvsadcosts[1]);
187  cpi->nmvsadcosts[0] = NULL;
188  cpi->nmvsadcosts[1] = NULL;
189
190  vpx_free(cpi->nmvsadcosts_hp[0]);
191  vpx_free(cpi->nmvsadcosts_hp[1]);
192  cpi->nmvsadcosts_hp[0] = NULL;
193  cpi->nmvsadcosts_hp[1] = NULL;
194
195  vp9_cyclic_refresh_free(cpi->cyclic_refresh);
196  cpi->cyclic_refresh = NULL;
197
198  vp9_free_ref_frame_buffers(cm);
199  vp9_free_context_buffers(cm);
200
201  vp9_free_frame_buffer(&cpi->last_frame_uf);
202  vp9_free_frame_buffer(&cpi->scaled_source);
203  vp9_free_frame_buffer(&cpi->scaled_last_source);
204  vp9_free_frame_buffer(&cpi->alt_ref_buffer);
205  vp9_lookahead_destroy(cpi->lookahead);
206
207  vpx_free(cpi->tok);
208  cpi->tok = 0;
209
210  vp9_free_pc_tree(cpi);
211
212  for (i = 0; i < cpi->svc.number_spatial_layers; ++i) {
213    LAYER_CONTEXT *const lc = &cpi->svc.layer_context[i];
214    vpx_free(lc->rc_twopass_stats_in.buf);
215    lc->rc_twopass_stats_in.buf = NULL;
216    lc->rc_twopass_stats_in.sz = 0;
217  }
218
219  if (cpi->source_diff_var != NULL) {
220    vpx_free(cpi->source_diff_var);
221    cpi->source_diff_var = NULL;
222  }
223
224  for (i = 0; i < MAX_LAG_BUFFERS; ++i) {
225    vp9_free_frame_buffer(&cpi->svc.scaled_frames[i]);
226  }
227  vpx_memset(&cpi->svc.scaled_frames[0], 0,
228             MAX_LAG_BUFFERS * sizeof(cpi->svc.scaled_frames[0]));
229}
230
231static void save_coding_context(VP9_COMP *cpi) {
232  CODING_CONTEXT *const cc = &cpi->coding_context;
233  VP9_COMMON *cm = &cpi->common;
234
235  // Stores a snapshot of key state variables which can subsequently be
236  // restored with a call to vp9_restore_coding_context. These functions are
237  // intended for use in a re-code loop in vp9_compress_frame where the
238  // quantizer value is adjusted between loop iterations.
239  vp9_copy(cc->nmvjointcost,  cpi->mb.nmvjointcost);
240
241  vpx_memcpy(cc->nmvcosts[0], cpi->nmvcosts[0],
242             MV_VALS * sizeof(*cpi->nmvcosts[0]));
243  vpx_memcpy(cc->nmvcosts[1], cpi->nmvcosts[1],
244             MV_VALS * sizeof(*cpi->nmvcosts[1]));
245  vpx_memcpy(cc->nmvcosts_hp[0], cpi->nmvcosts_hp[0],
246             MV_VALS * sizeof(*cpi->nmvcosts_hp[0]));
247  vpx_memcpy(cc->nmvcosts_hp[1], cpi->nmvcosts_hp[1],
248             MV_VALS * sizeof(*cpi->nmvcosts_hp[1]));
249
250  vp9_copy(cc->segment_pred_probs, cm->seg.pred_probs);
251
252  vpx_memcpy(cpi->coding_context.last_frame_seg_map_copy,
253             cm->last_frame_seg_map, (cm->mi_rows * cm->mi_cols));
254
255  vp9_copy(cc->last_ref_lf_deltas, cm->lf.last_ref_deltas);
256  vp9_copy(cc->last_mode_lf_deltas, cm->lf.last_mode_deltas);
257
258  cc->fc = cm->fc;
259}
260
261static void restore_coding_context(VP9_COMP *cpi) {
262  CODING_CONTEXT *const cc = &cpi->coding_context;
263  VP9_COMMON *cm = &cpi->common;
264
265  // Restore key state variables to the snapshot state stored in the
266  // previous call to vp9_save_coding_context.
267  vp9_copy(cpi->mb.nmvjointcost, cc->nmvjointcost);
268
269  vpx_memcpy(cpi->nmvcosts[0], cc->nmvcosts[0],
270             MV_VALS * sizeof(*cc->nmvcosts[0]));
271  vpx_memcpy(cpi->nmvcosts[1], cc->nmvcosts[1],
272             MV_VALS * sizeof(*cc->nmvcosts[1]));
273  vpx_memcpy(cpi->nmvcosts_hp[0], cc->nmvcosts_hp[0],
274             MV_VALS * sizeof(*cc->nmvcosts_hp[0]));
275  vpx_memcpy(cpi->nmvcosts_hp[1], cc->nmvcosts_hp[1],
276             MV_VALS * sizeof(*cc->nmvcosts_hp[1]));
277
278  vp9_copy(cm->seg.pred_probs, cc->segment_pred_probs);
279
280  vpx_memcpy(cm->last_frame_seg_map,
281             cpi->coding_context.last_frame_seg_map_copy,
282             (cm->mi_rows * cm->mi_cols));
283
284  vp9_copy(cm->lf.last_ref_deltas, cc->last_ref_lf_deltas);
285  vp9_copy(cm->lf.last_mode_deltas, cc->last_mode_lf_deltas);
286
287  cm->fc = cc->fc;
288}
289
290static void configure_static_seg_features(VP9_COMP *cpi) {
291  VP9_COMMON *const cm = &cpi->common;
292  const RATE_CONTROL *const rc = &cpi->rc;
293  struct segmentation *const seg = &cm->seg;
294
295  int high_q = (int)(rc->avg_q > 48.0);
296  int qi_delta;
297
298  // Disable and clear down for KF
299  if (cm->frame_type == KEY_FRAME) {
300    // Clear down the global segmentation map
301    vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
302    seg->update_map = 0;
303    seg->update_data = 0;
304    cpi->static_mb_pct = 0;
305
306    // Disable segmentation
307    vp9_disable_segmentation(seg);
308
309    // Clear down the segment features.
310    vp9_clearall_segfeatures(seg);
311  } else if (cpi->refresh_alt_ref_frame) {
312    // If this is an alt ref frame
313    // Clear down the global segmentation map
314    vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
315    seg->update_map = 0;
316    seg->update_data = 0;
317    cpi->static_mb_pct = 0;
318
319    // Disable segmentation and individual segment features by default
320    vp9_disable_segmentation(seg);
321    vp9_clearall_segfeatures(seg);
322
323    // Scan frames from current to arf frame.
324    // This function re-enables segmentation if appropriate.
325    vp9_update_mbgraph_stats(cpi);
326
327    // If segmentation was enabled set those features needed for the
328    // arf itself.
329    if (seg->enabled) {
330      seg->update_map = 1;
331      seg->update_data = 1;
332
333      qi_delta = vp9_compute_qdelta(rc, rc->avg_q, rc->avg_q * 0.875,
334                                    cm->bit_depth);
335      vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta - 2);
336      vp9_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
337
338      vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
339      vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
340
341      // Where relevant assume segment data is delta data
342      seg->abs_delta = SEGMENT_DELTADATA;
343    }
344  } else if (seg->enabled) {
345    // All other frames if segmentation has been enabled
346
347    // First normal frame in a valid gf or alt ref group
348    if (rc->frames_since_golden == 0) {
349      // Set up segment features for normal frames in an arf group
350      if (rc->source_alt_ref_active) {
351        seg->update_map = 0;
352        seg->update_data = 1;
353        seg->abs_delta = SEGMENT_DELTADATA;
354
355        qi_delta = vp9_compute_qdelta(rc, rc->avg_q, rc->avg_q * 1.125,
356                                      cm->bit_depth);
357        vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta + 2);
358        vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
359
360        vp9_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
361        vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
362
363        // Segment coding disabled for compred testing
364        if (high_q || (cpi->static_mb_pct == 100)) {
365          vp9_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
366          vp9_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
367          vp9_enable_segfeature(seg, 1, SEG_LVL_SKIP);
368        }
369      } else {
370        // Disable segmentation and clear down features if alt ref
371        // is not active for this group
372
373        vp9_disable_segmentation(seg);
374
375        vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
376
377        seg->update_map = 0;
378        seg->update_data = 0;
379
380        vp9_clearall_segfeatures(seg);
381      }
382    } else if (rc->is_src_frame_alt_ref) {
383      // Special case where we are coding over the top of a previous
384      // alt ref frame.
385      // Segment coding disabled for compred testing
386
387      // Enable ref frame features for segment 0 as well
388      vp9_enable_segfeature(seg, 0, SEG_LVL_REF_FRAME);
389      vp9_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
390
391      // All mbs should use ALTREF_FRAME
392      vp9_clear_segdata(seg, 0, SEG_LVL_REF_FRAME);
393      vp9_set_segdata(seg, 0, SEG_LVL_REF_FRAME, ALTREF_FRAME);
394      vp9_clear_segdata(seg, 1, SEG_LVL_REF_FRAME);
395      vp9_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
396
397      // Skip all MBs if high Q (0,0 mv and skip coeffs)
398      if (high_q) {
399        vp9_enable_segfeature(seg, 0, SEG_LVL_SKIP);
400        vp9_enable_segfeature(seg, 1, SEG_LVL_SKIP);
401      }
402      // Enable data update
403      seg->update_data = 1;
404    } else {
405      // All other frames.
406
407      // No updates.. leave things as they are.
408      seg->update_map = 0;
409      seg->update_data = 0;
410    }
411  }
412}
413
414static void update_reference_segmentation_map(VP9_COMP *cpi) {
415  VP9_COMMON *const cm = &cpi->common;
416  MODE_INFO *mi_8x8_ptr = cm->mi;
417  uint8_t *cache_ptr = cm->last_frame_seg_map;
418  int row, col;
419
420  for (row = 0; row < cm->mi_rows; row++) {
421    MODE_INFO *mi_8x8 = mi_8x8_ptr;
422    uint8_t *cache = cache_ptr;
423    for (col = 0; col < cm->mi_cols; col++, mi_8x8++, cache++)
424      cache[0] = mi_8x8[0].src_mi->mbmi.segment_id;
425    mi_8x8_ptr += cm->mi_stride;
426    cache_ptr += cm->mi_cols;
427  }
428}
429
430static void alloc_raw_frame_buffers(VP9_COMP *cpi) {
431  VP9_COMMON *cm = &cpi->common;
432  const VP9EncoderConfig *oxcf = &cpi->oxcf;
433
434  cpi->lookahead = vp9_lookahead_init(oxcf->width, oxcf->height,
435                                      cm->subsampling_x, cm->subsampling_y,
436#if CONFIG_VP9_HIGHBITDEPTH
437                                      cm->use_highbitdepth,
438#endif
439                                      oxcf->lag_in_frames);
440  if (!cpi->lookahead)
441    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
442                       "Failed to allocate lag buffers");
443
444  if (vp9_realloc_frame_buffer(&cpi->alt_ref_buffer,
445                               oxcf->width, oxcf->height,
446                               cm->subsampling_x, cm->subsampling_y,
447#if CONFIG_VP9_HIGHBITDEPTH
448                               cm->use_highbitdepth,
449#endif
450                               VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL))
451    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
452                       "Failed to allocate altref buffer");
453}
454
455static void alloc_ref_frame_buffers(VP9_COMP *cpi) {
456  VP9_COMMON *const cm = &cpi->common;
457  if (vp9_alloc_ref_frame_buffers(cm, cm->width, cm->height))
458    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
459                       "Failed to allocate frame buffers");
460}
461
462static void alloc_util_frame_buffers(VP9_COMP *cpi) {
463  VP9_COMMON *const cm = &cpi->common;
464  if (vp9_realloc_frame_buffer(&cpi->last_frame_uf,
465                               cm->width, cm->height,
466                               cm->subsampling_x, cm->subsampling_y,
467#if CONFIG_VP9_HIGHBITDEPTH
468                               cm->use_highbitdepth,
469#endif
470                               VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL))
471    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
472                       "Failed to allocate last frame buffer");
473
474  if (vp9_realloc_frame_buffer(&cpi->scaled_source,
475                               cm->width, cm->height,
476                               cm->subsampling_x, cm->subsampling_y,
477#if CONFIG_VP9_HIGHBITDEPTH
478                               cm->use_highbitdepth,
479#endif
480                               VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL))
481    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
482                       "Failed to allocate scaled source buffer");
483
484  if (vp9_realloc_frame_buffer(&cpi->scaled_last_source,
485                               cm->width, cm->height,
486                               cm->subsampling_x, cm->subsampling_y,
487#if CONFIG_VP9_HIGHBITDEPTH
488                               cm->use_highbitdepth,
489#endif
490                               VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL))
491    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
492                       "Failed to allocate scaled last source buffer");
493}
494
495void vp9_alloc_compressor_data(VP9_COMP *cpi) {
496  VP9_COMMON *cm = &cpi->common;
497
498  vp9_alloc_context_buffers(cm, cm->width, cm->height);
499
500  vpx_free(cpi->tok);
501
502  {
503    unsigned int tokens = get_token_alloc(cm->mb_rows, cm->mb_cols);
504    CHECK_MEM_ERROR(cm, cpi->tok, vpx_calloc(tokens, sizeof(*cpi->tok)));
505  }
506
507  vp9_setup_pc_tree(&cpi->common, cpi);
508}
509
510static void update_frame_size(VP9_COMP *cpi) {
511  VP9_COMMON *const cm = &cpi->common;
512  MACROBLOCKD *const xd = &cpi->mb.e_mbd;
513
514  vp9_set_mb_mi(cm, cm->width, cm->height);
515  vp9_init_context_buffers(cm);
516  init_macroblockd(cm, xd);
517
518  if (is_two_pass_svc(cpi)) {
519    if (vp9_realloc_frame_buffer(&cpi->alt_ref_buffer,
520                                 cm->width, cm->height,
521                                 cm->subsampling_x, cm->subsampling_y,
522#if CONFIG_VP9_HIGHBITDEPTH
523                                 cm->use_highbitdepth,
524#endif
525                                 VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL))
526      vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
527                         "Failed to reallocate alt_ref_buffer");
528  }
529}
530
531void vp9_new_framerate(VP9_COMP *cpi, double framerate) {
532  cpi->framerate = framerate < 0.1 ? 30 : framerate;
533  vp9_rc_update_framerate(cpi);
534}
535
536static void set_tile_limits(VP9_COMP *cpi) {
537  VP9_COMMON *const cm = &cpi->common;
538
539  int min_log2_tile_cols, max_log2_tile_cols;
540  vp9_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
541
542  cm->log2_tile_cols = clamp(cpi->oxcf.tile_columns,
543                             min_log2_tile_cols, max_log2_tile_cols);
544  cm->log2_tile_rows = cpi->oxcf.tile_rows;
545}
546
547static void init_buffer_indices(VP9_COMP *cpi) {
548  cpi->lst_fb_idx = 0;
549  cpi->gld_fb_idx = 1;
550  cpi->alt_fb_idx = 2;
551}
552
553static void init_config(struct VP9_COMP *cpi, VP9EncoderConfig *oxcf) {
554  VP9_COMMON *const cm = &cpi->common;
555
556  cpi->oxcf = *oxcf;
557  cpi->framerate = oxcf->init_framerate;
558
559  cm->profile = oxcf->profile;
560  cm->bit_depth = oxcf->bit_depth;
561#if CONFIG_VP9_HIGHBITDEPTH
562  cm->use_highbitdepth = oxcf->use_highbitdepth;
563#endif
564  cm->color_space = UNKNOWN;
565
566  cm->width = oxcf->width;
567  cm->height = oxcf->height;
568  vp9_alloc_compressor_data(cpi);
569
570  // Spatial scalability.
571  cpi->svc.number_spatial_layers = oxcf->ss_number_layers;
572  // Temporal scalability.
573  cpi->svc.number_temporal_layers = oxcf->ts_number_layers;
574
575  if ((cpi->svc.number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) ||
576      ((cpi->svc.number_temporal_layers > 1 ||
577        cpi->svc.number_spatial_layers > 1) &&
578       cpi->oxcf.pass == 2)) {
579    vp9_init_layer_context(cpi);
580  }
581
582  // change includes all joint functionality
583  vp9_change_config(cpi, oxcf);
584
585  cpi->static_mb_pct = 0;
586  cpi->ref_frame_flags = 0;
587
588  init_buffer_indices(cpi);
589
590  set_tile_limits(cpi);
591}
592
593static void set_rc_buffer_sizes(RATE_CONTROL *rc,
594                                const VP9EncoderConfig *oxcf) {
595  const int64_t bandwidth = oxcf->target_bandwidth;
596  const int64_t starting = oxcf->starting_buffer_level_ms;
597  const int64_t optimal = oxcf->optimal_buffer_level_ms;
598  const int64_t maximum = oxcf->maximum_buffer_size_ms;
599
600  rc->starting_buffer_level = starting * bandwidth / 1000;
601  rc->optimal_buffer_level = (optimal == 0) ? bandwidth / 8
602                                            : optimal * bandwidth / 1000;
603  rc->maximum_buffer_size = (maximum == 0) ? bandwidth / 8
604                                           : maximum * bandwidth / 1000;
605}
606
607void vp9_change_config(struct VP9_COMP *cpi, const VP9EncoderConfig *oxcf) {
608  VP9_COMMON *const cm = &cpi->common;
609  RATE_CONTROL *const rc = &cpi->rc;
610
611  if (cm->profile != oxcf->profile)
612    cm->profile = oxcf->profile;
613  cm->bit_depth = oxcf->bit_depth;
614
615  if (cm->profile <= PROFILE_1)
616    assert(cm->bit_depth == VPX_BITS_8);
617  else
618    assert(cm->bit_depth > VPX_BITS_8);
619
620  cpi->oxcf = *oxcf;
621#if CONFIG_VP9_HIGHBITDEPTH
622  if (cpi->oxcf.use_highbitdepth) {
623    cpi->mb.e_mbd.bd = (int)cm->bit_depth;
624  }
625#endif
626
627  rc->baseline_gf_interval = DEFAULT_GF_INTERVAL;
628
629  cpi->refresh_golden_frame = 0;
630  cpi->refresh_last_frame = 1;
631  cm->refresh_frame_context = 1;
632  cm->reset_frame_context = 0;
633
634  vp9_reset_segment_features(&cm->seg);
635  vp9_set_high_precision_mv(cpi, 0);
636
637  {
638    int i;
639
640    for (i = 0; i < MAX_SEGMENTS; i++)
641      cpi->segment_encode_breakout[i] = cpi->oxcf.encode_breakout;
642  }
643  cpi->encode_breakout = cpi->oxcf.encode_breakout;
644
645  set_rc_buffer_sizes(rc, &cpi->oxcf);
646
647  // Under a configuration change, where maximum_buffer_size may change,
648  // keep buffer level clipped to the maximum allowed buffer size.
649  rc->bits_off_target = MIN(rc->bits_off_target, rc->maximum_buffer_size);
650  rc->buffer_level = MIN(rc->buffer_level, rc->maximum_buffer_size);
651
652  // Set up frame rate and related parameters rate control values.
653  vp9_new_framerate(cpi, cpi->framerate);
654
655  // Set absolute upper and lower quality limits
656  rc->worst_quality = cpi->oxcf.worst_allowed_q;
657  rc->best_quality = cpi->oxcf.best_allowed_q;
658
659  cm->interp_filter = cpi->sf.default_interp_filter;
660
661  cm->display_width = cpi->oxcf.width;
662  cm->display_height = cpi->oxcf.height;
663
664  if (cpi->initial_width) {
665    // Increasing the size of the frame beyond the first seen frame, or some
666    // otherwise signaled maximum size, is not supported.
667    // TODO(jkoleszar): exit gracefully.
668    assert(cm->width <= cpi->initial_width);
669    assert(cm->height <= cpi->initial_height);
670  }
671  update_frame_size(cpi);
672
673  if ((cpi->svc.number_temporal_layers > 1 &&
674      cpi->oxcf.rc_mode == VPX_CBR) ||
675      ((cpi->svc.number_temporal_layers > 1 ||
676        cpi->svc.number_spatial_layers > 1) &&
677       cpi->oxcf.pass == 2)) {
678    vp9_update_layer_context_change_config(cpi,
679                                           (int)cpi->oxcf.target_bandwidth);
680  }
681
682  cpi->alt_ref_source = NULL;
683  rc->is_src_frame_alt_ref = 0;
684
685#if 0
686  // Experimental RD Code
687  cpi->frame_distortion = 0;
688  cpi->last_frame_distortion = 0;
689#endif
690
691  set_tile_limits(cpi);
692
693  cpi->ext_refresh_frame_flags_pending = 0;
694  cpi->ext_refresh_frame_context_pending = 0;
695
696#if CONFIG_VP9_TEMPORAL_DENOISING
697  if (cpi->oxcf.noise_sensitivity > 0) {
698    vp9_denoiser_alloc(&(cpi->denoiser), cm->width, cm->height,
699                       cm->subsampling_x, cm->subsampling_y,
700#if CONFIG_VP9_HIGHBITDEPTH
701                       cm->use_highbitdepth,
702#endif
703                       VP9_ENC_BORDER_IN_PIXELS);
704  }
705#endif
706}
707
708#ifndef M_LOG2_E
709#define M_LOG2_E 0.693147180559945309417
710#endif
711#define log2f(x) (log (x) / (float) M_LOG2_E)
712
713static void cal_nmvjointsadcost(int *mvjointsadcost) {
714  mvjointsadcost[0] = 600;
715  mvjointsadcost[1] = 300;
716  mvjointsadcost[2] = 300;
717  mvjointsadcost[3] = 300;
718}
719
720static void cal_nmvsadcosts(int *mvsadcost[2]) {
721  int i = 1;
722
723  mvsadcost[0][0] = 0;
724  mvsadcost[1][0] = 0;
725
726  do {
727    double z = 256 * (2 * (log2f(8 * i) + .6));
728    mvsadcost[0][i] = (int)z;
729    mvsadcost[1][i] = (int)z;
730    mvsadcost[0][-i] = (int)z;
731    mvsadcost[1][-i] = (int)z;
732  } while (++i <= MV_MAX);
733}
734
735static void cal_nmvsadcosts_hp(int *mvsadcost[2]) {
736  int i = 1;
737
738  mvsadcost[0][0] = 0;
739  mvsadcost[1][0] = 0;
740
741  do {
742    double z = 256 * (2 * (log2f(8 * i) + .6));
743    mvsadcost[0][i] = (int)z;
744    mvsadcost[1][i] = (int)z;
745    mvsadcost[0][-i] = (int)z;
746    mvsadcost[1][-i] = (int)z;
747  } while (++i <= MV_MAX);
748}
749
750
751VP9_COMP *vp9_create_compressor(VP9EncoderConfig *oxcf) {
752  unsigned int i, j;
753  VP9_COMP *const cpi = vpx_memalign(32, sizeof(VP9_COMP));
754  VP9_COMMON *const cm = cpi != NULL ? &cpi->common : NULL;
755
756  if (!cm)
757    return NULL;
758
759  vp9_zero(*cpi);
760
761  if (setjmp(cm->error.jmp)) {
762    cm->error.setjmp = 0;
763    vp9_remove_compressor(cpi);
764    return 0;
765  }
766
767  cm->error.setjmp = 1;
768
769  cpi->use_svc = 0;
770
771  init_config(cpi, oxcf);
772  vp9_rc_init(&cpi->oxcf, oxcf->pass, &cpi->rc);
773
774  cm->current_video_frame = 0;
775  cpi->skippable_frame = 0;
776
777  // Create the encoder segmentation map and set all entries to 0
778  CHECK_MEM_ERROR(cm, cpi->segmentation_map,
779                  vpx_calloc(cm->mi_rows * cm->mi_cols, 1));
780
781  // Create a complexity map used for rd adjustment
782  CHECK_MEM_ERROR(cm, cpi->complexity_map,
783                  vpx_calloc(cm->mi_rows * cm->mi_cols, 1));
784
785  // Create a map used for cyclic background refresh.
786  CHECK_MEM_ERROR(cm, cpi->cyclic_refresh,
787                  vp9_cyclic_refresh_alloc(cm->mi_rows, cm->mi_cols));
788
789  // And a place holder structure is the coding context
790  // for use if we want to save and restore it
791  CHECK_MEM_ERROR(cm, cpi->coding_context.last_frame_seg_map_copy,
792                  vpx_calloc(cm->mi_rows * cm->mi_cols, 1));
793
794  CHECK_MEM_ERROR(cm, cpi->nmvcosts[0],
795                  vpx_calloc(MV_VALS, sizeof(*cpi->nmvcosts[0])));
796  CHECK_MEM_ERROR(cm, cpi->nmvcosts[1],
797                  vpx_calloc(MV_VALS, sizeof(*cpi->nmvcosts[1])));
798  CHECK_MEM_ERROR(cm, cpi->nmvcosts_hp[0],
799                  vpx_calloc(MV_VALS, sizeof(*cpi->nmvcosts_hp[0])));
800  CHECK_MEM_ERROR(cm, cpi->nmvcosts_hp[1],
801                  vpx_calloc(MV_VALS, sizeof(*cpi->nmvcosts_hp[1])));
802  CHECK_MEM_ERROR(cm, cpi->nmvsadcosts[0],
803                  vpx_calloc(MV_VALS, sizeof(*cpi->nmvsadcosts[0])));
804  CHECK_MEM_ERROR(cm, cpi->nmvsadcosts[1],
805                  vpx_calloc(MV_VALS, sizeof(*cpi->nmvsadcosts[1])));
806  CHECK_MEM_ERROR(cm, cpi->nmvsadcosts_hp[0],
807                  vpx_calloc(MV_VALS, sizeof(*cpi->nmvsadcosts_hp[0])));
808  CHECK_MEM_ERROR(cm, cpi->nmvsadcosts_hp[1],
809                  vpx_calloc(MV_VALS, sizeof(*cpi->nmvsadcosts_hp[1])));
810
811  for (i = 0; i < (sizeof(cpi->mbgraph_stats) /
812                   sizeof(cpi->mbgraph_stats[0])); i++) {
813    CHECK_MEM_ERROR(cm, cpi->mbgraph_stats[i].mb_stats,
814                    vpx_calloc(cm->MBs *
815                               sizeof(*cpi->mbgraph_stats[i].mb_stats), 1));
816  }
817
818#if CONFIG_FP_MB_STATS
819  cpi->use_fp_mb_stats = 0;
820  if (cpi->use_fp_mb_stats) {
821    // a place holder used to store the first pass mb stats in the first pass
822    CHECK_MEM_ERROR(cm, cpi->twopass.frame_mb_stats_buf,
823                    vpx_calloc(cm->MBs * sizeof(uint8_t), 1));
824  } else {
825    cpi->twopass.frame_mb_stats_buf = NULL;
826  }
827#endif
828
829  cpi->refresh_alt_ref_frame = 0;
830
831  // Note that at the moment multi_arf will not work with svc.
832  // For the current check in all the execution paths are defaulted to 0
833  // pending further tuning and testing. The code is left in place here
834  // as a place holder in regard to the required paths.
835  cpi->multi_arf_last_grp_enabled = 0;
836  if (oxcf->pass == 2) {
837    if (cpi->use_svc) {
838      cpi->multi_arf_allowed = 0;
839      cpi->multi_arf_enabled = 0;
840    } else {
841      // Disable by default for now.
842      cpi->multi_arf_allowed = 0;
843      cpi->multi_arf_enabled = 0;
844    }
845  } else {
846    cpi->multi_arf_allowed = 0;
847    cpi->multi_arf_enabled = 0;
848  }
849
850  cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
851#if CONFIG_INTERNAL_STATS
852  cpi->b_calculate_ssimg = 0;
853
854  cpi->count = 0;
855  cpi->bytes = 0;
856
857  if (cpi->b_calculate_psnr) {
858    cpi->total_y = 0.0;
859    cpi->total_u = 0.0;
860    cpi->total_v = 0.0;
861    cpi->total = 0.0;
862    cpi->total_sq_error = 0;
863    cpi->total_samples = 0;
864
865    cpi->totalp_y = 0.0;
866    cpi->totalp_u = 0.0;
867    cpi->totalp_v = 0.0;
868    cpi->totalp = 0.0;
869    cpi->totalp_sq_error = 0;
870    cpi->totalp_samples = 0;
871
872    cpi->tot_recode_hits = 0;
873    cpi->summed_quality = 0;
874    cpi->summed_weights = 0;
875    cpi->summedp_quality = 0;
876    cpi->summedp_weights = 0;
877  }
878
879  if (cpi->b_calculate_ssimg) {
880    cpi->total_ssimg_y = 0;
881    cpi->total_ssimg_u = 0;
882    cpi->total_ssimg_v = 0;
883    cpi->total_ssimg_all = 0;
884  }
885
886#endif
887
888  cpi->first_time_stamp_ever = INT64_MAX;
889
890  cal_nmvjointsadcost(cpi->mb.nmvjointsadcost);
891  cpi->mb.nmvcost[0] = &cpi->nmvcosts[0][MV_MAX];
892  cpi->mb.nmvcost[1] = &cpi->nmvcosts[1][MV_MAX];
893  cpi->mb.nmvsadcost[0] = &cpi->nmvsadcosts[0][MV_MAX];
894  cpi->mb.nmvsadcost[1] = &cpi->nmvsadcosts[1][MV_MAX];
895  cal_nmvsadcosts(cpi->mb.nmvsadcost);
896
897  cpi->mb.nmvcost_hp[0] = &cpi->nmvcosts_hp[0][MV_MAX];
898  cpi->mb.nmvcost_hp[1] = &cpi->nmvcosts_hp[1][MV_MAX];
899  cpi->mb.nmvsadcost_hp[0] = &cpi->nmvsadcosts_hp[0][MV_MAX];
900  cpi->mb.nmvsadcost_hp[1] = &cpi->nmvsadcosts_hp[1][MV_MAX];
901  cal_nmvsadcosts_hp(cpi->mb.nmvsadcost_hp);
902
903#if CONFIG_VP9_TEMPORAL_DENOISING
904#ifdef OUTPUT_YUV_DENOISED
905  yuv_denoised_file = fopen("denoised.yuv", "ab");
906#endif
907#endif
908#ifdef OUTPUT_YUV_REC
909  yuv_rec_file = fopen("rec.yuv", "wb");
910#endif
911
912#if 0
913  framepsnr = fopen("framepsnr.stt", "a");
914  kf_list = fopen("kf_list.stt", "w");
915#endif
916
917  cpi->allow_encode_breakout = ENCODE_BREAKOUT_ENABLED;
918
919  if (oxcf->pass == 1) {
920    vp9_init_first_pass(cpi);
921  } else if (oxcf->pass == 2) {
922    const size_t packet_sz = sizeof(FIRSTPASS_STATS);
923    const int packets = (int)(oxcf->two_pass_stats_in.sz / packet_sz);
924
925    if (cpi->svc.number_spatial_layers > 1
926        || cpi->svc.number_temporal_layers > 1) {
927      FIRSTPASS_STATS *const stats = oxcf->two_pass_stats_in.buf;
928      FIRSTPASS_STATS *stats_copy[VPX_SS_MAX_LAYERS] = {0};
929      int i;
930
931      for (i = 0; i < oxcf->ss_number_layers; ++i) {
932        FIRSTPASS_STATS *const last_packet_for_layer =
933            &stats[packets - oxcf->ss_number_layers + i];
934        const int layer_id = (int)last_packet_for_layer->spatial_layer_id;
935        const int packets_in_layer = (int)last_packet_for_layer->count + 1;
936        if (layer_id >= 0 && layer_id < oxcf->ss_number_layers) {
937          LAYER_CONTEXT *const lc = &cpi->svc.layer_context[layer_id];
938
939          vpx_free(lc->rc_twopass_stats_in.buf);
940
941          lc->rc_twopass_stats_in.sz = packets_in_layer * packet_sz;
942          CHECK_MEM_ERROR(cm, lc->rc_twopass_stats_in.buf,
943                          vpx_malloc(lc->rc_twopass_stats_in.sz));
944          lc->twopass.stats_in_start = lc->rc_twopass_stats_in.buf;
945          lc->twopass.stats_in = lc->twopass.stats_in_start;
946          lc->twopass.stats_in_end = lc->twopass.stats_in_start
947                                     + packets_in_layer - 1;
948          stats_copy[layer_id] = lc->rc_twopass_stats_in.buf;
949        }
950      }
951
952      for (i = 0; i < packets; ++i) {
953        const int layer_id = (int)stats[i].spatial_layer_id;
954        if (layer_id >= 0 && layer_id < oxcf->ss_number_layers
955            && stats_copy[layer_id] != NULL) {
956          *stats_copy[layer_id] = stats[i];
957          ++stats_copy[layer_id];
958        }
959      }
960
961      vp9_init_second_pass_spatial_svc(cpi);
962    } else {
963#if CONFIG_FP_MB_STATS
964      if (cpi->use_fp_mb_stats) {
965        const size_t psz = cpi->common.MBs * sizeof(uint8_t);
966        const int ps = (int)(oxcf->firstpass_mb_stats_in.sz / psz);
967
968        cpi->twopass.firstpass_mb_stats.mb_stats_start =
969            oxcf->firstpass_mb_stats_in.buf;
970        cpi->twopass.firstpass_mb_stats.mb_stats_end =
971            cpi->twopass.firstpass_mb_stats.mb_stats_start +
972            (ps - 1) * cpi->common.MBs * sizeof(uint8_t);
973      }
974#endif
975
976      cpi->twopass.stats_in_start = oxcf->two_pass_stats_in.buf;
977      cpi->twopass.stats_in = cpi->twopass.stats_in_start;
978      cpi->twopass.stats_in_end = &cpi->twopass.stats_in[packets - 1];
979
980      vp9_init_second_pass(cpi);
981    }
982  }
983
984  vp9_set_speed_features(cpi);
985
986  // Allocate memory to store variances for a frame.
987  CHECK_MEM_ERROR(cm, cpi->source_diff_var,
988                  vpx_calloc(cm->MBs, sizeof(diff)));
989  cpi->source_var_thresh = 0;
990  cpi->frames_till_next_var_check = 0;
991
992  // Default rd threshold factors for mode selection
993  for (i = 0; i < BLOCK_SIZES; ++i) {
994    for (j = 0; j < MAX_MODES; ++j) {
995      cpi->rd.thresh_freq_fact[i][j] = 32;
996      cpi->rd.mode_map[i][j] = j;
997    }
998  }
999
1000#define BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX3F, SDX8F, SDX4DF)\
1001    cpi->fn_ptr[BT].sdf            = SDF; \
1002    cpi->fn_ptr[BT].sdaf           = SDAF; \
1003    cpi->fn_ptr[BT].vf             = VF; \
1004    cpi->fn_ptr[BT].svf            = SVF; \
1005    cpi->fn_ptr[BT].svaf           = SVAF; \
1006    cpi->fn_ptr[BT].sdx3f          = SDX3F; \
1007    cpi->fn_ptr[BT].sdx8f          = SDX8F; \
1008    cpi->fn_ptr[BT].sdx4df         = SDX4DF;
1009
1010  BFP(BLOCK_32X16, vp9_sad32x16, vp9_sad32x16_avg,
1011      vp9_variance32x16, vp9_sub_pixel_variance32x16,
1012      vp9_sub_pixel_avg_variance32x16, NULL, NULL, vp9_sad32x16x4d)
1013
1014  BFP(BLOCK_16X32, vp9_sad16x32, vp9_sad16x32_avg,
1015      vp9_variance16x32, vp9_sub_pixel_variance16x32,
1016      vp9_sub_pixel_avg_variance16x32, NULL, NULL, vp9_sad16x32x4d)
1017
1018  BFP(BLOCK_64X32, vp9_sad64x32, vp9_sad64x32_avg,
1019      vp9_variance64x32, vp9_sub_pixel_variance64x32,
1020      vp9_sub_pixel_avg_variance64x32, NULL, NULL, vp9_sad64x32x4d)
1021
1022  BFP(BLOCK_32X64, vp9_sad32x64, vp9_sad32x64_avg,
1023      vp9_variance32x64, vp9_sub_pixel_variance32x64,
1024      vp9_sub_pixel_avg_variance32x64, NULL, NULL, vp9_sad32x64x4d)
1025
1026  BFP(BLOCK_32X32, vp9_sad32x32, vp9_sad32x32_avg,
1027      vp9_variance32x32, vp9_sub_pixel_variance32x32,
1028      vp9_sub_pixel_avg_variance32x32, vp9_sad32x32x3, vp9_sad32x32x8,
1029      vp9_sad32x32x4d)
1030
1031  BFP(BLOCK_64X64, vp9_sad64x64, vp9_sad64x64_avg,
1032      vp9_variance64x64, vp9_sub_pixel_variance64x64,
1033      vp9_sub_pixel_avg_variance64x64, vp9_sad64x64x3, vp9_sad64x64x8,
1034      vp9_sad64x64x4d)
1035
1036  BFP(BLOCK_16X16, vp9_sad16x16, vp9_sad16x16_avg,
1037      vp9_variance16x16, vp9_sub_pixel_variance16x16,
1038      vp9_sub_pixel_avg_variance16x16, vp9_sad16x16x3, vp9_sad16x16x8,
1039      vp9_sad16x16x4d)
1040
1041  BFP(BLOCK_16X8, vp9_sad16x8, vp9_sad16x8_avg,
1042      vp9_variance16x8, vp9_sub_pixel_variance16x8,
1043      vp9_sub_pixel_avg_variance16x8,
1044      vp9_sad16x8x3, vp9_sad16x8x8, vp9_sad16x8x4d)
1045
1046  BFP(BLOCK_8X16, vp9_sad8x16, vp9_sad8x16_avg,
1047      vp9_variance8x16, vp9_sub_pixel_variance8x16,
1048      vp9_sub_pixel_avg_variance8x16,
1049      vp9_sad8x16x3, vp9_sad8x16x8, vp9_sad8x16x4d)
1050
1051  BFP(BLOCK_8X8, vp9_sad8x8, vp9_sad8x8_avg,
1052      vp9_variance8x8, vp9_sub_pixel_variance8x8,
1053      vp9_sub_pixel_avg_variance8x8,
1054      vp9_sad8x8x3, vp9_sad8x8x8, vp9_sad8x8x4d)
1055
1056  BFP(BLOCK_8X4, vp9_sad8x4, vp9_sad8x4_avg,
1057      vp9_variance8x4, vp9_sub_pixel_variance8x4,
1058      vp9_sub_pixel_avg_variance8x4, NULL, vp9_sad8x4x8, vp9_sad8x4x4d)
1059
1060  BFP(BLOCK_4X8, vp9_sad4x8, vp9_sad4x8_avg,
1061      vp9_variance4x8, vp9_sub_pixel_variance4x8,
1062      vp9_sub_pixel_avg_variance4x8, NULL, vp9_sad4x8x8, vp9_sad4x8x4d)
1063
1064  BFP(BLOCK_4X4, vp9_sad4x4, vp9_sad4x4_avg,
1065      vp9_variance4x4, vp9_sub_pixel_variance4x4,
1066      vp9_sub_pixel_avg_variance4x4,
1067      vp9_sad4x4x3, vp9_sad4x4x8, vp9_sad4x4x4d)
1068
1069  /* vp9_init_quantizer() is first called here. Add check in
1070   * vp9_frame_init_quantizer() so that vp9_init_quantizer is only
1071   * called later when needed. This will avoid unnecessary calls of
1072   * vp9_init_quantizer() for every frame.
1073   */
1074  vp9_init_quantizer(cpi);
1075
1076  vp9_loop_filter_init(cm);
1077
1078  cm->error.setjmp = 0;
1079
1080  return cpi;
1081}
1082
1083void vp9_remove_compressor(VP9_COMP *cpi) {
1084  unsigned int i;
1085
1086  if (!cpi)
1087    return;
1088
1089  if (cpi && (cpi->common.current_video_frame > 0)) {
1090#if CONFIG_INTERNAL_STATS
1091
1092    vp9_clear_system_state();
1093
1094    // printf("\n8x8-4x4:%d-%d\n", cpi->t8x8_count, cpi->t4x4_count);
1095    if (cpi->oxcf.pass != 1) {
1096      FILE *f = fopen("opsnr.stt", "a");
1097      double time_encoded = (cpi->last_end_time_stamp_seen
1098                             - cpi->first_time_stamp_ever) / 10000000.000;
1099      double total_encode_time = (cpi->time_receive_data +
1100                                  cpi->time_compress_data)   / 1000.000;
1101      double dr = (double)cpi->bytes * (double) 8 / (double)1000
1102                  / time_encoded;
1103
1104      if (cpi->b_calculate_psnr) {
1105        const double total_psnr =
1106            vpx_sse_to_psnr((double)cpi->total_samples, 255.0,
1107                            (double)cpi->total_sq_error);
1108        const double totalp_psnr =
1109            vpx_sse_to_psnr((double)cpi->totalp_samples, 255.0,
1110                            (double)cpi->totalp_sq_error);
1111        const double total_ssim = 100 * pow(cpi->summed_quality /
1112                                                cpi->summed_weights, 8.0);
1113        const double totalp_ssim = 100 * pow(cpi->summedp_quality /
1114                                                cpi->summedp_weights, 8.0);
1115
1116        fprintf(f, "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\t"
1117                "VPXSSIM\tVPSSIMP\t  Time(ms)\n");
1118        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",
1119                dr, cpi->total / cpi->count, total_psnr,
1120                cpi->totalp / cpi->count, totalp_psnr, total_ssim, totalp_ssim,
1121                total_encode_time);
1122      }
1123
1124      if (cpi->b_calculate_ssimg) {
1125        fprintf(f, "BitRate\tSSIM_Y\tSSIM_U\tSSIM_V\tSSIM_A\t  Time(ms)\n");
1126        fprintf(f, "%7.2f\t%6.4f\t%6.4f\t%6.4f\t%6.4f\t%8.0f\n", dr,
1127                cpi->total_ssimg_y / cpi->count,
1128                cpi->total_ssimg_u / cpi->count,
1129                cpi->total_ssimg_v / cpi->count,
1130                cpi->total_ssimg_all / cpi->count, total_encode_time);
1131      }
1132
1133      fclose(f);
1134    }
1135
1136#endif
1137
1138#if 0
1139    {
1140      printf("\n_pick_loop_filter_level:%d\n", cpi->time_pick_lpf / 1000);
1141      printf("\n_frames recive_data encod_mb_row compress_frame  Total\n");
1142      printf("%6d %10ld %10ld %10ld %10ld\n", cpi->common.current_video_frame,
1143             cpi->time_receive_data / 1000, cpi->time_encode_sb_row / 1000,
1144             cpi->time_compress_data / 1000,
1145             (cpi->time_receive_data + cpi->time_compress_data) / 1000);
1146    }
1147#endif
1148  }
1149
1150#if CONFIG_VP9_TEMPORAL_DENOISING
1151  if (cpi->oxcf.noise_sensitivity > 0) {
1152    vp9_denoiser_free(&(cpi->denoiser));
1153  }
1154#endif
1155
1156  dealloc_compressor_data(cpi);
1157  vpx_free(cpi->tok);
1158
1159  for (i = 0; i < sizeof(cpi->mbgraph_stats) /
1160                  sizeof(cpi->mbgraph_stats[0]); ++i) {
1161    vpx_free(cpi->mbgraph_stats[i].mb_stats);
1162  }
1163
1164#if CONFIG_FP_MB_STATS
1165  if (cpi->use_fp_mb_stats) {
1166    vpx_free(cpi->twopass.frame_mb_stats_buf);
1167    cpi->twopass.frame_mb_stats_buf = NULL;
1168  }
1169#endif
1170
1171  vp9_remove_common(&cpi->common);
1172  vpx_free(cpi);
1173
1174#if CONFIG_VP9_TEMPORAL_DENOISING
1175#ifdef OUTPUT_YUV_DENOISED
1176  fclose(yuv_denoised_file);
1177#endif
1178#endif
1179#ifdef OUTPUT_YUV_REC
1180  fclose(yuv_rec_file);
1181#endif
1182
1183#if 0
1184
1185  if (keyfile)
1186    fclose(keyfile);
1187
1188  if (framepsnr)
1189    fclose(framepsnr);
1190
1191  if (kf_list)
1192    fclose(kf_list);
1193
1194#endif
1195}
1196static int64_t get_sse(const uint8_t *a, int a_stride,
1197                       const uint8_t *b, int b_stride,
1198                       int width, int height) {
1199  const int dw = width % 16;
1200  const int dh = height % 16;
1201  int64_t total_sse = 0;
1202  unsigned int sse = 0;
1203  int sum = 0;
1204  int x, y;
1205
1206  if (dw > 0) {
1207    variance(&a[width - dw], a_stride, &b[width - dw], b_stride,
1208             dw, height, &sse, &sum);
1209    total_sse += sse;
1210  }
1211
1212  if (dh > 0) {
1213    variance(&a[(height - dh) * a_stride], a_stride,
1214             &b[(height - dh) * b_stride], b_stride,
1215             width - dw, dh, &sse, &sum);
1216    total_sse += sse;
1217  }
1218
1219  for (y = 0; y < height / 16; ++y) {
1220    const uint8_t *pa = a;
1221    const uint8_t *pb = b;
1222    for (x = 0; x < width / 16; ++x) {
1223      vp9_mse16x16(pa, a_stride, pb, b_stride, &sse);
1224      total_sse += sse;
1225
1226      pa += 16;
1227      pb += 16;
1228    }
1229
1230    a += 16 * a_stride;
1231    b += 16 * b_stride;
1232  }
1233
1234  return total_sse;
1235}
1236
1237typedef struct {
1238  double psnr[4];       // total/y/u/v
1239  uint64_t sse[4];      // total/y/u/v
1240  uint32_t samples[4];  // total/y/u/v
1241} PSNR_STATS;
1242
1243static void calc_psnr(const YV12_BUFFER_CONFIG *a, const YV12_BUFFER_CONFIG *b,
1244                      PSNR_STATS *psnr) {
1245  const int widths[3]        = {a->y_width,  a->uv_width,  a->uv_width };
1246  const int heights[3]       = {a->y_height, a->uv_height, a->uv_height};
1247  const uint8_t *a_planes[3] = {a->y_buffer, a->u_buffer,  a->v_buffer };
1248  const int a_strides[3]     = {a->y_stride, a->uv_stride, a->uv_stride};
1249  const uint8_t *b_planes[3] = {b->y_buffer, b->u_buffer,  b->v_buffer };
1250  const int b_strides[3]     = {b->y_stride, b->uv_stride, b->uv_stride};
1251  int i;
1252  uint64_t total_sse = 0;
1253  uint32_t total_samples = 0;
1254
1255  for (i = 0; i < 3; ++i) {
1256    const int w = widths[i];
1257    const int h = heights[i];
1258    const uint32_t samples = w * h;
1259    const uint64_t sse = get_sse(a_planes[i], a_strides[i],
1260                                 b_planes[i], b_strides[i],
1261                                 w, h);
1262    psnr->sse[1 + i] = sse;
1263    psnr->samples[1 + i] = samples;
1264    psnr->psnr[1 + i] = vpx_sse_to_psnr(samples, 255.0, (double)sse);
1265
1266    total_sse += sse;
1267    total_samples += samples;
1268  }
1269
1270  psnr->sse[0] = total_sse;
1271  psnr->samples[0] = total_samples;
1272  psnr->psnr[0] = vpx_sse_to_psnr((double)total_samples, 255.0,
1273                                  (double)total_sse);
1274}
1275
1276static void generate_psnr_packet(VP9_COMP *cpi) {
1277  struct vpx_codec_cx_pkt pkt;
1278  int i;
1279  PSNR_STATS psnr;
1280  calc_psnr(cpi->Source, cpi->common.frame_to_show, &psnr);
1281  for (i = 0; i < 4; ++i) {
1282    pkt.data.psnr.samples[i] = psnr.samples[i];
1283    pkt.data.psnr.sse[i] = psnr.sse[i];
1284    pkt.data.psnr.psnr[i] = psnr.psnr[i];
1285  }
1286  pkt.kind = VPX_CODEC_PSNR_PKT;
1287  if (is_two_pass_svc(cpi))
1288    cpi->svc.layer_context[cpi->svc.spatial_layer_id].psnr_pkt = pkt.data.psnr;
1289  else
1290    vpx_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
1291}
1292
1293int vp9_use_as_reference(VP9_COMP *cpi, int ref_frame_flags) {
1294  if (ref_frame_flags > 7)
1295    return -1;
1296
1297  cpi->ref_frame_flags = ref_frame_flags;
1298  return 0;
1299}
1300
1301void vp9_update_reference(VP9_COMP *cpi, int ref_frame_flags) {
1302  cpi->ext_refresh_golden_frame = (ref_frame_flags & VP9_GOLD_FLAG) != 0;
1303  cpi->ext_refresh_alt_ref_frame = (ref_frame_flags & VP9_ALT_FLAG) != 0;
1304  cpi->ext_refresh_last_frame = (ref_frame_flags & VP9_LAST_FLAG) != 0;
1305  cpi->ext_refresh_frame_flags_pending = 1;
1306}
1307
1308static YV12_BUFFER_CONFIG *get_vp9_ref_frame_buffer(VP9_COMP *cpi,
1309                                VP9_REFFRAME ref_frame_flag) {
1310  MV_REFERENCE_FRAME ref_frame = NONE;
1311  if (ref_frame_flag == VP9_LAST_FLAG)
1312    ref_frame = LAST_FRAME;
1313  else if (ref_frame_flag == VP9_GOLD_FLAG)
1314    ref_frame = GOLDEN_FRAME;
1315  else if (ref_frame_flag == VP9_ALT_FLAG)
1316    ref_frame = ALTREF_FRAME;
1317
1318  return ref_frame == NONE ? NULL : get_ref_frame_buffer(cpi, ref_frame);
1319}
1320
1321int vp9_copy_reference_enc(VP9_COMP *cpi, VP9_REFFRAME ref_frame_flag,
1322                           YV12_BUFFER_CONFIG *sd) {
1323  YV12_BUFFER_CONFIG *cfg = get_vp9_ref_frame_buffer(cpi, ref_frame_flag);
1324  if (cfg) {
1325    vp8_yv12_copy_frame(cfg, sd);
1326    return 0;
1327  } else {
1328    return -1;
1329  }
1330}
1331
1332int vp9_set_reference_enc(VP9_COMP *cpi, VP9_REFFRAME ref_frame_flag,
1333                          YV12_BUFFER_CONFIG *sd) {
1334  YV12_BUFFER_CONFIG *cfg = get_vp9_ref_frame_buffer(cpi, ref_frame_flag);
1335  if (cfg) {
1336    vp8_yv12_copy_frame(sd, cfg);
1337    return 0;
1338  } else {
1339    return -1;
1340  }
1341}
1342
1343int vp9_update_entropy(VP9_COMP * cpi, int update) {
1344  cpi->ext_refresh_frame_context = update;
1345  cpi->ext_refresh_frame_context_pending = 1;
1346  return 0;
1347}
1348
1349#if CONFIG_VP9_TEMPORAL_DENOISING
1350#if defined(OUTPUT_YUV_DENOISED)
1351// The denoiser buffer is allocated as a YUV 440 buffer. This function writes it
1352// as YUV 420. We simply use the top-left pixels of the UV buffers, since we do
1353// not denoise the UV channels at this time. If ever we implement UV channel
1354// denoising we will have to modify this.
1355void vp9_write_yuv_frame_420(YV12_BUFFER_CONFIG *s, FILE *f) {
1356  uint8_t *src = s->y_buffer;
1357  int h = s->y_height;
1358
1359  do {
1360    fwrite(src, s->y_width, 1, f);
1361    src += s->y_stride;
1362  } while (--h);
1363
1364  src = s->u_buffer;
1365  h = s->uv_height / 2;
1366
1367  do {
1368    fwrite(src, s->uv_width / 2, 1, f);
1369    src += s->uv_stride + s->uv_width / 2;
1370  } while (--h);
1371
1372  src = s->v_buffer;
1373  h = s->uv_height / 2;
1374
1375  do {
1376    fwrite(src, s->uv_width / 2, 1, f);
1377    src += s->uv_stride + s->uv_width / 2;
1378  } while (--h);
1379}
1380#endif
1381#endif
1382
1383#ifdef OUTPUT_YUV_REC
1384void vp9_write_yuv_rec_frame(VP9_COMMON *cm) {
1385  YV12_BUFFER_CONFIG *s = cm->frame_to_show;
1386  uint8_t *src = s->y_buffer;
1387  int h = cm->height;
1388
1389  do {
1390    fwrite(src, s->y_width, 1,  yuv_rec_file);
1391    src += s->y_stride;
1392  } while (--h);
1393
1394  src = s->u_buffer;
1395  h = s->uv_height;
1396
1397  do {
1398    fwrite(src, s->uv_width, 1,  yuv_rec_file);
1399    src += s->uv_stride;
1400  } while (--h);
1401
1402  src = s->v_buffer;
1403  h = s->uv_height;
1404
1405  do {
1406    fwrite(src, s->uv_width, 1, yuv_rec_file);
1407    src += s->uv_stride;
1408  } while (--h);
1409
1410  fflush(yuv_rec_file);
1411}
1412#endif
1413
1414static void scale_and_extend_frame_nonnormative(const YV12_BUFFER_CONFIG *src,
1415                                                YV12_BUFFER_CONFIG *dst) {
1416  // TODO(dkovalev): replace YV12_BUFFER_CONFIG with vpx_image_t
1417  int i;
1418  const uint8_t *const srcs[3] = {src->y_buffer, src->u_buffer, src->v_buffer};
1419  const int src_strides[3] = {src->y_stride, src->uv_stride, src->uv_stride};
1420  const int src_widths[3] = {src->y_crop_width, src->uv_crop_width,
1421                             src->uv_crop_width };
1422  const int src_heights[3] = {src->y_crop_height, src->uv_crop_height,
1423                              src->uv_crop_height};
1424  uint8_t *const dsts[3] = {dst->y_buffer, dst->u_buffer, dst->v_buffer};
1425  const int dst_strides[3] = {dst->y_stride, dst->uv_stride, dst->uv_stride};
1426  const int dst_widths[3] = {dst->y_crop_width, dst->uv_crop_width,
1427                             dst->uv_crop_width};
1428  const int dst_heights[3] = {dst->y_crop_height, dst->uv_crop_height,
1429                              dst->uv_crop_height};
1430
1431  for (i = 0; i < MAX_MB_PLANE; ++i)
1432    vp9_resize_plane(srcs[i], src_heights[i], src_widths[i], src_strides[i],
1433                     dsts[i], dst_heights[i], dst_widths[i], dst_strides[i]);
1434
1435  vp9_extend_frame_borders(dst);
1436}
1437
1438static void scale_and_extend_frame(const YV12_BUFFER_CONFIG *src,
1439                                   YV12_BUFFER_CONFIG *dst) {
1440  const int src_w = src->y_crop_width;
1441  const int src_h = src->y_crop_height;
1442  const int dst_w = dst->y_crop_width;
1443  const int dst_h = dst->y_crop_height;
1444  const uint8_t *const srcs[3] = {src->y_buffer, src->u_buffer, src->v_buffer};
1445  const int src_strides[3] = {src->y_stride, src->uv_stride, src->uv_stride};
1446  uint8_t *const dsts[3] = {dst->y_buffer, dst->u_buffer, dst->v_buffer};
1447  const int dst_strides[3] = {dst->y_stride, dst->uv_stride, dst->uv_stride};
1448  const InterpKernel *const kernel = vp9_get_interp_kernel(EIGHTTAP);
1449  int x, y, i;
1450
1451  for (y = 0; y < dst_h; y += 16) {
1452    for (x = 0; x < dst_w; x += 16) {
1453      for (i = 0; i < MAX_MB_PLANE; ++i) {
1454        const int factor = (i == 0 || i == 3 ? 1 : 2);
1455        const int x_q4 = x * (16 / factor) * src_w / dst_w;
1456        const int y_q4 = y * (16 / factor) * src_h / dst_h;
1457        const int src_stride = src_strides[i];
1458        const int dst_stride = dst_strides[i];
1459        const uint8_t *src_ptr = srcs[i] + (y / factor) * src_h / dst_h *
1460                                     src_stride + (x / factor) * src_w / dst_w;
1461        uint8_t *dst_ptr = dsts[i] + (y / factor) * dst_stride + (x / factor);
1462
1463        vp9_convolve8(src_ptr, src_stride, dst_ptr, dst_stride,
1464                      kernel[x_q4 & 0xf], 16 * src_w / dst_w,
1465                      kernel[y_q4 & 0xf], 16 * src_h / dst_h,
1466                      16 / factor, 16 / factor);
1467      }
1468    }
1469  }
1470
1471  vp9_extend_frame_borders(dst);
1472}
1473
1474// Function to test for conditions that indicate we should loop
1475// back and recode a frame.
1476static int recode_loop_test(const VP9_COMP *cpi,
1477                            int high_limit, int low_limit,
1478                            int q, int maxq, int minq) {
1479  const VP9_COMMON *const cm = &cpi->common;
1480  const RATE_CONTROL *const rc = &cpi->rc;
1481  const VP9EncoderConfig *const oxcf = &cpi->oxcf;
1482  int force_recode = 0;
1483
1484  // Special case trap if maximum allowed frame size exceeded.
1485  if (rc->projected_frame_size > rc->max_frame_bandwidth) {
1486    force_recode = 1;
1487
1488  // Is frame recode allowed.
1489  // Yes if either recode mode 1 is selected or mode 2 is selected
1490  // and the frame is a key frame, golden frame or alt_ref_frame
1491  } else if ((cpi->sf.recode_loop == ALLOW_RECODE) ||
1492             ((cpi->sf.recode_loop == ALLOW_RECODE_KFARFGF) &&
1493              (cm->frame_type == KEY_FRAME ||
1494               cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame))) {
1495    // General over and under shoot tests
1496    if ((rc->projected_frame_size > high_limit && q < maxq) ||
1497        (rc->projected_frame_size < low_limit && q > minq)) {
1498      force_recode = 1;
1499    } else if (cpi->oxcf.rc_mode == VPX_CQ) {
1500      // Deal with frame undershoot and whether or not we are
1501      // below the automatically set cq level.
1502      if (q > oxcf->cq_level &&
1503          rc->projected_frame_size < ((rc->this_frame_target * 7) >> 3)) {
1504        force_recode = 1;
1505      }
1506    }
1507  }
1508  return force_recode;
1509}
1510
1511void vp9_update_reference_frames(VP9_COMP *cpi) {
1512  VP9_COMMON * const cm = &cpi->common;
1513
1514  // At this point the new frame has been encoded.
1515  // If any buffer copy / swapping is signaled it should be done here.
1516  if (cm->frame_type == KEY_FRAME) {
1517    ref_cnt_fb(cm->frame_bufs,
1518               &cm->ref_frame_map[cpi->gld_fb_idx], cm->new_fb_idx);
1519    ref_cnt_fb(cm->frame_bufs,
1520               &cm->ref_frame_map[cpi->alt_fb_idx], cm->new_fb_idx);
1521  } else if (vp9_preserve_existing_gf(cpi)) {
1522    // We have decided to preserve the previously existing golden frame as our
1523    // new ARF frame. However, in the short term in function
1524    // vp9_bitstream.c::get_refresh_mask() we left it in the GF slot and, if
1525    // we're updating the GF with the current decoded frame, we save it to the
1526    // ARF slot instead.
1527    // We now have to update the ARF with the current frame and swap gld_fb_idx
1528    // and alt_fb_idx so that, overall, we've stored the old GF in the new ARF
1529    // slot and, if we're updating the GF, the current frame becomes the new GF.
1530    int tmp;
1531
1532    ref_cnt_fb(cm->frame_bufs,
1533               &cm->ref_frame_map[cpi->alt_fb_idx], cm->new_fb_idx);
1534
1535    tmp = cpi->alt_fb_idx;
1536    cpi->alt_fb_idx = cpi->gld_fb_idx;
1537    cpi->gld_fb_idx = tmp;
1538
1539    if (is_two_pass_svc(cpi)) {
1540      cpi->svc.layer_context[0].gold_ref_idx = cpi->gld_fb_idx;
1541      cpi->svc.layer_context[0].alt_ref_idx = cpi->alt_fb_idx;
1542    }
1543  } else { /* For non key/golden frames */
1544    if (cpi->refresh_alt_ref_frame) {
1545      int arf_idx = cpi->alt_fb_idx;
1546      if ((cpi->oxcf.pass == 2) && cpi->multi_arf_allowed) {
1547        const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
1548        arf_idx = gf_group->arf_update_idx[gf_group->index];
1549      }
1550
1551      ref_cnt_fb(cm->frame_bufs,
1552                 &cm->ref_frame_map[arf_idx], cm->new_fb_idx);
1553      vpx_memcpy(cpi->interp_filter_selected[ALTREF_FRAME],
1554                 cpi->interp_filter_selected[0],
1555                 sizeof(cpi->interp_filter_selected[0]));
1556    }
1557
1558    if (cpi->refresh_golden_frame) {
1559      ref_cnt_fb(cm->frame_bufs,
1560                 &cm->ref_frame_map[cpi->gld_fb_idx], cm->new_fb_idx);
1561      if (!cpi->rc.is_src_frame_alt_ref)
1562        vpx_memcpy(cpi->interp_filter_selected[GOLDEN_FRAME],
1563                   cpi->interp_filter_selected[0],
1564                   sizeof(cpi->interp_filter_selected[0]));
1565      else
1566        vpx_memcpy(cpi->interp_filter_selected[GOLDEN_FRAME],
1567                   cpi->interp_filter_selected[ALTREF_FRAME],
1568                   sizeof(cpi->interp_filter_selected[ALTREF_FRAME]));
1569    }
1570  }
1571
1572  if (cpi->refresh_last_frame) {
1573    ref_cnt_fb(cm->frame_bufs,
1574               &cm->ref_frame_map[cpi->lst_fb_idx], cm->new_fb_idx);
1575    if (!cpi->rc.is_src_frame_alt_ref)
1576      vpx_memcpy(cpi->interp_filter_selected[LAST_FRAME],
1577                 cpi->interp_filter_selected[0],
1578                 sizeof(cpi->interp_filter_selected[0]));
1579  }
1580#if CONFIG_VP9_TEMPORAL_DENOISING
1581  if (cpi->oxcf.noise_sensitivity > 0) {
1582    vp9_denoiser_update_frame_info(&cpi->denoiser,
1583                                   *cpi->Source,
1584                                   cpi->common.frame_type,
1585                                   cpi->refresh_alt_ref_frame,
1586                                   cpi->refresh_golden_frame,
1587                                   cpi->refresh_last_frame);
1588  }
1589#endif
1590}
1591
1592static void loopfilter_frame(VP9_COMP *cpi, VP9_COMMON *cm) {
1593  MACROBLOCKD *xd = &cpi->mb.e_mbd;
1594  struct loopfilter *lf = &cm->lf;
1595  if (xd->lossless) {
1596      lf->filter_level = 0;
1597  } else {
1598    struct vpx_usec_timer timer;
1599
1600    vp9_clear_system_state();
1601
1602    vpx_usec_timer_start(&timer);
1603
1604    vp9_pick_filter_level(cpi->Source, cpi, cpi->sf.lpf_pick);
1605
1606    vpx_usec_timer_mark(&timer);
1607    cpi->time_pick_lpf += vpx_usec_timer_elapsed(&timer);
1608  }
1609
1610  if (lf->filter_level > 0) {
1611    vp9_loop_filter_frame(cm->frame_to_show, cm, xd, lf->filter_level, 0, 0);
1612  }
1613
1614  vp9_extend_frame_inner_borders(cm->frame_to_show);
1615}
1616
1617void vp9_scale_references(VP9_COMP *cpi) {
1618  VP9_COMMON *cm = &cpi->common;
1619  MV_REFERENCE_FRAME ref_frame;
1620  const VP9_REFFRAME ref_mask[3] = {VP9_LAST_FLAG, VP9_GOLD_FLAG, VP9_ALT_FLAG};
1621
1622  for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
1623    const int idx = cm->ref_frame_map[get_ref_frame_idx(cpi, ref_frame)];
1624    const YV12_BUFFER_CONFIG *const ref = &cm->frame_bufs[idx].buf;
1625
1626    // Need to convert from VP9_REFFRAME to index into ref_mask (subtract 1).
1627    if ((cpi->ref_frame_flags & ref_mask[ref_frame - 1]) &&
1628        (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height)) {
1629      const int new_fb = get_free_fb(cm);
1630      vp9_realloc_frame_buffer(&cm->frame_bufs[new_fb].buf,
1631                               cm->width, cm->height,
1632                               cm->subsampling_x, cm->subsampling_y,
1633#if CONFIG_VP9_HIGHBITDEPTH
1634                               cm->use_highbitdepth,
1635#endif
1636                               VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL);
1637      scale_and_extend_frame(ref, &cm->frame_bufs[new_fb].buf);
1638      cpi->scaled_ref_idx[ref_frame - 1] = new_fb;
1639    } else {
1640      cpi->scaled_ref_idx[ref_frame - 1] = idx;
1641      cm->frame_bufs[idx].ref_count++;
1642    }
1643  }
1644}
1645
1646static void release_scaled_references(VP9_COMP *cpi) {
1647  VP9_COMMON *cm = &cpi->common;
1648  int i;
1649
1650  for (i = 0; i < 3; i++)
1651    cm->frame_bufs[cpi->scaled_ref_idx[i]].ref_count--;
1652}
1653
1654static void full_to_model_count(unsigned int *model_count,
1655                                unsigned int *full_count) {
1656  int n;
1657  model_count[ZERO_TOKEN] = full_count[ZERO_TOKEN];
1658  model_count[ONE_TOKEN] = full_count[ONE_TOKEN];
1659  model_count[TWO_TOKEN] = full_count[TWO_TOKEN];
1660  for (n = THREE_TOKEN; n < EOB_TOKEN; ++n)
1661    model_count[TWO_TOKEN] += full_count[n];
1662  model_count[EOB_MODEL_TOKEN] = full_count[EOB_TOKEN];
1663}
1664
1665static void full_to_model_counts(vp9_coeff_count_model *model_count,
1666                                 vp9_coeff_count *full_count) {
1667  int i, j, k, l;
1668
1669  for (i = 0; i < PLANE_TYPES; ++i)
1670    for (j = 0; j < REF_TYPES; ++j)
1671      for (k = 0; k < COEF_BANDS; ++k)
1672        for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l)
1673          full_to_model_count(model_count[i][j][k][l], full_count[i][j][k][l]);
1674}
1675
1676#if 0 && CONFIG_INTERNAL_STATS
1677static void output_frame_level_debug_stats(VP9_COMP *cpi) {
1678  VP9_COMMON *const cm = &cpi->common;
1679  FILE *const f = fopen("tmp.stt", cm->current_video_frame ? "a" : "w");
1680  int recon_err;
1681
1682  vp9_clear_system_state();
1683
1684  recon_err = vp9_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
1685
1686  if (cpi->twopass.total_left_stats.coded_error != 0.0)
1687    fprintf(f, "%10u %10d %10d %10d %10d"
1688        "%10"PRId64" %10"PRId64" %10"PRId64" %10"PRId64" %10d "
1689        "%7.2lf %7.2lf %7.2lf %7.2lf %7.2lf"
1690        "%6d %6d %5d %5d %5d "
1691        "%10"PRId64" %10.3lf"
1692        "%10lf %8u %10d %10d %10d\n",
1693        cpi->common.current_video_frame, cpi->rc.this_frame_target,
1694        cpi->rc.projected_frame_size,
1695        cpi->rc.projected_frame_size / cpi->common.MBs,
1696        (cpi->rc.projected_frame_size - cpi->rc.this_frame_target),
1697        cpi->rc.vbr_bits_off_target,
1698        cpi->rc.total_target_vs_actual,
1699        (cpi->rc.starting_buffer_level - cpi->rc.bits_off_target),
1700        cpi->rc.total_actual_bits, cm->base_qindex,
1701        vp9_convert_qindex_to_q(cm->base_qindex),
1702        (double)vp9_dc_quant(cm->base_qindex, 0) / 4.0,
1703        vp9_convert_qindex_to_q(cpi->twopass.active_worst_quality),
1704        cpi->rc.avg_q,
1705        vp9_convert_qindex_to_q(cpi->oxcf.cq_level),
1706        cpi->refresh_last_frame, cpi->refresh_golden_frame,
1707        cpi->refresh_alt_ref_frame, cm->frame_type, cpi->rc.gfu_boost,
1708        cpi->twopass.bits_left,
1709        cpi->twopass.total_left_stats.coded_error,
1710        cpi->twopass.bits_left /
1711            (1 + cpi->twopass.total_left_stats.coded_error),
1712        cpi->tot_recode_hits, recon_err, cpi->rc.kf_boost,
1713        cpi->twopass.kf_zeromotion_pct);
1714
1715  fclose(f);
1716
1717  if (0) {
1718    FILE *const fmodes = fopen("Modes.stt", "a");
1719    int i;
1720
1721    fprintf(fmodes, "%6d:%1d:%1d:%1d ", cpi->common.current_video_frame,
1722            cm->frame_type, cpi->refresh_golden_frame,
1723            cpi->refresh_alt_ref_frame);
1724
1725    for (i = 0; i < MAX_MODES; ++i)
1726      fprintf(fmodes, "%5d ", cpi->mode_chosen_counts[i]);
1727
1728    fprintf(fmodes, "\n");
1729
1730    fclose(fmodes);
1731  }
1732}
1733#endif
1734
1735static void encode_without_recode_loop(VP9_COMP *cpi,
1736                                       int q) {
1737  VP9_COMMON *const cm = &cpi->common;
1738  vp9_clear_system_state();
1739  vp9_set_quantizer(cm, q);
1740  setup_frame(cpi);
1741  // Variance adaptive and in frame q adjustment experiments are mutually
1742  // exclusive.
1743  if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
1744    vp9_vaq_frame_setup(cpi);
1745  } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
1746    vp9_setup_in_frame_q_adj(cpi);
1747  } else if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) {
1748    vp9_cyclic_refresh_setup(cpi);
1749  }
1750  // transform / motion compensation build reconstruction frame
1751  vp9_encode_frame(cpi);
1752
1753  // Update the skip mb flag probabilities based on the distribution
1754  // seen in the last encoder iteration.
1755  // update_base_skip_probs(cpi);
1756  vp9_clear_system_state();
1757}
1758
1759static void encode_with_recode_loop(VP9_COMP *cpi,
1760                                    size_t *size,
1761                                    uint8_t *dest,
1762                                    int q,
1763                                    int bottom_index,
1764                                    int top_index) {
1765  VP9_COMMON *const cm = &cpi->common;
1766  RATE_CONTROL *const rc = &cpi->rc;
1767  int loop_count = 0;
1768  int loop = 0;
1769  int overshoot_seen = 0;
1770  int undershoot_seen = 0;
1771  int q_low = bottom_index, q_high = top_index;
1772  int frame_over_shoot_limit;
1773  int frame_under_shoot_limit;
1774
1775  // Decide frame size bounds
1776  vp9_rc_compute_frame_size_bounds(cpi, rc->this_frame_target,
1777                                   &frame_under_shoot_limit,
1778                                   &frame_over_shoot_limit);
1779
1780  do {
1781    vp9_clear_system_state();
1782
1783    vp9_set_quantizer(cm, q);
1784
1785    if (loop_count == 0)
1786      setup_frame(cpi);
1787
1788    // Variance adaptive and in frame q adjustment experiments are mutually
1789    // exclusive.
1790    if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
1791      vp9_vaq_frame_setup(cpi);
1792    } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
1793      vp9_setup_in_frame_q_adj(cpi);
1794    }
1795
1796    // transform / motion compensation build reconstruction frame
1797    vp9_encode_frame(cpi);
1798
1799    // Update the skip mb flag probabilities based on the distribution
1800    // seen in the last encoder iteration.
1801    // update_base_skip_probs(cpi);
1802
1803    vp9_clear_system_state();
1804
1805    // Dummy pack of the bitstream using up to date stats to get an
1806    // accurate estimate of output frame size to determine if we need
1807    // to recode.
1808    if (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF) {
1809      save_coding_context(cpi);
1810      if (!cpi->sf.use_nonrd_pick_mode)
1811        vp9_pack_bitstream(cpi, dest, size);
1812
1813      rc->projected_frame_size = (int)(*size) << 3;
1814      restore_coding_context(cpi);
1815
1816      if (frame_over_shoot_limit == 0)
1817        frame_over_shoot_limit = 1;
1818    }
1819
1820    if (cpi->oxcf.rc_mode == VPX_Q) {
1821      loop = 0;
1822    } else {
1823      if ((cm->frame_type == KEY_FRAME) &&
1824           rc->this_key_frame_forced &&
1825           (rc->projected_frame_size < rc->max_frame_bandwidth)) {
1826        int last_q = q;
1827        int kf_err = vp9_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
1828
1829        int high_err_target = cpi->ambient_err;
1830        int low_err_target = cpi->ambient_err >> 1;
1831
1832        // Prevent possible divide by zero error below for perfect KF
1833        kf_err += !kf_err;
1834
1835        // The key frame is not good enough or we can afford
1836        // to make it better without undue risk of popping.
1837        if ((kf_err > high_err_target &&
1838             rc->projected_frame_size <= frame_over_shoot_limit) ||
1839            (kf_err > low_err_target &&
1840             rc->projected_frame_size <= frame_under_shoot_limit)) {
1841          // Lower q_high
1842          q_high = q > q_low ? q - 1 : q_low;
1843
1844          // Adjust Q
1845          q = (q * high_err_target) / kf_err;
1846          q = MIN(q, (q_high + q_low) >> 1);
1847        } else if (kf_err < low_err_target &&
1848                   rc->projected_frame_size >= frame_under_shoot_limit) {
1849          // The key frame is much better than the previous frame
1850          // Raise q_low
1851          q_low = q < q_high ? q + 1 : q_high;
1852
1853          // Adjust Q
1854          q = (q * low_err_target) / kf_err;
1855          q = MIN(q, (q_high + q_low + 1) >> 1);
1856        }
1857
1858        // Clamp Q to upper and lower limits:
1859        q = clamp(q, q_low, q_high);
1860
1861        loop = q != last_q;
1862      } else if (recode_loop_test(
1863          cpi, frame_over_shoot_limit, frame_under_shoot_limit,
1864          q, MAX(q_high, top_index), bottom_index)) {
1865        // Is the projected frame size out of range and are we allowed
1866        // to attempt to recode.
1867        int last_q = q;
1868        int retries = 0;
1869
1870        // Frame size out of permitted range:
1871        // Update correction factor & compute new Q to try...
1872
1873        // Frame is too large
1874        if (rc->projected_frame_size > rc->this_frame_target) {
1875          // Special case if the projected size is > the max allowed.
1876          if (rc->projected_frame_size >= rc->max_frame_bandwidth)
1877            q_high = rc->worst_quality;
1878
1879          // Raise Qlow as to at least the current value
1880          q_low = q < q_high ? q + 1 : q_high;
1881
1882          if (undershoot_seen || loop_count > 1) {
1883            // Update rate_correction_factor unless
1884            vp9_rc_update_rate_correction_factors(cpi, 1);
1885
1886            q = (q_high + q_low + 1) / 2;
1887          } else {
1888            // Update rate_correction_factor unless
1889            vp9_rc_update_rate_correction_factors(cpi, 0);
1890
1891            q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
1892                                   bottom_index, MAX(q_high, top_index));
1893
1894            while (q < q_low && retries < 10) {
1895              vp9_rc_update_rate_correction_factors(cpi, 0);
1896              q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
1897                                     bottom_index, MAX(q_high, top_index));
1898              retries++;
1899            }
1900          }
1901
1902          overshoot_seen = 1;
1903        } else {
1904          // Frame is too small
1905          q_high = q > q_low ? q - 1 : q_low;
1906
1907          if (overshoot_seen || loop_count > 1) {
1908            vp9_rc_update_rate_correction_factors(cpi, 1);
1909            q = (q_high + q_low) / 2;
1910          } else {
1911            vp9_rc_update_rate_correction_factors(cpi, 0);
1912            q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
1913                                   bottom_index, top_index);
1914            // Special case reset for qlow for constrained quality.
1915            // This should only trigger where there is very substantial
1916            // undershoot on a frame and the auto cq level is above
1917            // the user passsed in value.
1918            if (cpi->oxcf.rc_mode == VPX_CQ &&
1919                q < q_low) {
1920              q_low = q;
1921            }
1922
1923            while (q > q_high && retries < 10) {
1924              vp9_rc_update_rate_correction_factors(cpi, 0);
1925              q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
1926                                     bottom_index, top_index);
1927              retries++;
1928            }
1929          }
1930
1931          undershoot_seen = 1;
1932        }
1933
1934        // Clamp Q to upper and lower limits:
1935        q = clamp(q, q_low, q_high);
1936
1937        loop = q != last_q;
1938      } else {
1939        loop = 0;
1940      }
1941    }
1942
1943    // Special case for overlay frame.
1944    if (rc->is_src_frame_alt_ref &&
1945        rc->projected_frame_size < rc->max_frame_bandwidth)
1946      loop = 0;
1947
1948    if (loop) {
1949      loop_count++;
1950
1951#if CONFIG_INTERNAL_STATS
1952      cpi->tot_recode_hits++;
1953#endif
1954    }
1955  } while (loop);
1956}
1957
1958static int get_ref_frame_flags(const VP9_COMP *cpi) {
1959  const int *const map = cpi->common.ref_frame_map;
1960  const int gold_is_last = map[cpi->gld_fb_idx] == map[cpi->lst_fb_idx];
1961  const int alt_is_last = map[cpi->alt_fb_idx] == map[cpi->lst_fb_idx];
1962  const int gold_is_alt = map[cpi->gld_fb_idx] == map[cpi->alt_fb_idx];
1963  int flags = VP9_ALT_FLAG | VP9_GOLD_FLAG | VP9_LAST_FLAG;
1964
1965  if (gold_is_last)
1966    flags &= ~VP9_GOLD_FLAG;
1967
1968  if (cpi->rc.frames_till_gf_update_due == INT_MAX && !is_two_pass_svc(cpi))
1969    flags &= ~VP9_GOLD_FLAG;
1970
1971  if (alt_is_last)
1972    flags &= ~VP9_ALT_FLAG;
1973
1974  if (gold_is_alt)
1975    flags &= ~VP9_ALT_FLAG;
1976
1977  return flags;
1978}
1979
1980static void set_ext_overrides(VP9_COMP *cpi) {
1981  // Overrides the defaults with the externally supplied values with
1982  // vp9_update_reference() and vp9_update_entropy() calls
1983  // Note: The overrides are valid only for the next frame passed
1984  // to encode_frame_to_data_rate() function
1985  if (cpi->ext_refresh_frame_context_pending) {
1986    cpi->common.refresh_frame_context = cpi->ext_refresh_frame_context;
1987    cpi->ext_refresh_frame_context_pending = 0;
1988  }
1989  if (cpi->ext_refresh_frame_flags_pending) {
1990    cpi->refresh_last_frame = cpi->ext_refresh_last_frame;
1991    cpi->refresh_golden_frame = cpi->ext_refresh_golden_frame;
1992    cpi->refresh_alt_ref_frame = cpi->ext_refresh_alt_ref_frame;
1993    cpi->ext_refresh_frame_flags_pending = 0;
1994  }
1995}
1996
1997YV12_BUFFER_CONFIG *vp9_scale_if_required(VP9_COMMON *cm,
1998                                          YV12_BUFFER_CONFIG *unscaled,
1999                                          YV12_BUFFER_CONFIG *scaled) {
2000  if (cm->mi_cols * MI_SIZE != unscaled->y_width ||
2001      cm->mi_rows * MI_SIZE != unscaled->y_height) {
2002    scale_and_extend_frame_nonnormative(unscaled, scaled);
2003    return scaled;
2004  } else {
2005    return unscaled;
2006  }
2007}
2008
2009static int is_skippable_frame(const VP9_COMP *cpi) {
2010  // If the current frame does not have non-zero motion vector detected in the
2011  // first  pass, and so do its previous and forward frames, then this frame
2012  // can be skipped for partition check, and the partition size is assigned
2013  // according to the variance
2014  const SVC *const svc = &cpi->svc;
2015  const TWO_PASS *const twopass = is_two_pass_svc(cpi) ?
2016      &svc->layer_context[svc->spatial_layer_id].twopass : &cpi->twopass;
2017
2018  return (!frame_is_intra_only(&cpi->common) &&
2019    twopass->stats_in - 2 > twopass->stats_in_start &&
2020    twopass->stats_in < twopass->stats_in_end &&
2021    (twopass->stats_in - 1)->pcnt_inter - (twopass->stats_in - 1)->pcnt_motion
2022    == 1 &&
2023    (twopass->stats_in - 2)->pcnt_inter - (twopass->stats_in - 2)->pcnt_motion
2024    == 1 &&
2025    twopass->stats_in->pcnt_inter - twopass->stats_in->pcnt_motion == 1);
2026}
2027
2028static void set_arf_sign_bias(VP9_COMP *cpi) {
2029  VP9_COMMON *const cm = &cpi->common;
2030  int arf_sign_bias;
2031
2032  if ((cpi->oxcf.pass == 2) && cpi->multi_arf_allowed) {
2033    const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
2034    arf_sign_bias = cpi->rc.source_alt_ref_active &&
2035                    (!cpi->refresh_alt_ref_frame ||
2036                     (gf_group->rf_level[gf_group->index] == GF_ARF_LOW));
2037  } else {
2038    arf_sign_bias =
2039      (cpi->rc.source_alt_ref_active && !cpi->refresh_alt_ref_frame);
2040  }
2041  cm->ref_frame_sign_bias[ALTREF_FRAME] = arf_sign_bias;
2042}
2043
2044static void set_mv_search_params(VP9_COMP *cpi) {
2045  const VP9_COMMON *const cm = &cpi->common;
2046  const unsigned int max_mv_def = MIN(cm->width, cm->height);
2047
2048  // Default based on max resolution.
2049  cpi->mv_step_param = vp9_init_search_range(max_mv_def);
2050
2051  if (cpi->sf.mv.auto_mv_step_size) {
2052    if (frame_is_intra_only(cm)) {
2053      // Initialize max_mv_magnitude for use in the first INTER frame
2054      // after a key/intra-only frame.
2055      cpi->max_mv_magnitude = max_mv_def;
2056    } else {
2057      if (cm->show_frame)
2058        // Allow mv_steps to correspond to twice the max mv magnitude found
2059        // in the previous frame, capped by the default max_mv_magnitude based
2060        // on resolution.
2061        cpi->mv_step_param =
2062            vp9_init_search_range(MIN(max_mv_def, 2 * cpi->max_mv_magnitude));
2063      cpi->max_mv_magnitude = 0;
2064    }
2065  }
2066}
2067
2068
2069int setup_interp_filter_search_mask(VP9_COMP *cpi) {
2070  INTERP_FILTER ifilter;
2071  int ref_total[MAX_REF_FRAMES] = {0};
2072  MV_REFERENCE_FRAME ref;
2073  int mask = 0;
2074  if (cpi->common.last_frame_type == KEY_FRAME ||
2075      cpi->refresh_alt_ref_frame)
2076    return mask;
2077  for (ref = LAST_FRAME; ref <= ALTREF_FRAME; ++ref)
2078    for (ifilter = EIGHTTAP; ifilter <= EIGHTTAP_SHARP; ++ifilter)
2079      ref_total[ref] += cpi->interp_filter_selected[ref][ifilter];
2080
2081  for (ifilter = EIGHTTAP; ifilter <= EIGHTTAP_SHARP; ++ifilter) {
2082    if ((ref_total[LAST_FRAME] &&
2083        cpi->interp_filter_selected[LAST_FRAME][ifilter] == 0) &&
2084        (ref_total[GOLDEN_FRAME] == 0 ||
2085         cpi->interp_filter_selected[GOLDEN_FRAME][ifilter] * 50
2086           < ref_total[GOLDEN_FRAME]) &&
2087        (ref_total[ALTREF_FRAME] == 0 ||
2088         cpi->interp_filter_selected[ALTREF_FRAME][ifilter] * 50
2089           < ref_total[ALTREF_FRAME]))
2090      mask |= 1 << ifilter;
2091  }
2092  return mask;
2093}
2094
2095static void encode_frame_to_data_rate(VP9_COMP *cpi,
2096                                      size_t *size,
2097                                      uint8_t *dest,
2098                                      unsigned int *frame_flags) {
2099  VP9_COMMON *const cm = &cpi->common;
2100  const VP9EncoderConfig *const oxcf = &cpi->oxcf;
2101  struct segmentation *const seg = &cm->seg;
2102  TX_SIZE t;
2103  int q;
2104  int top_index;
2105  int bottom_index;
2106
2107  set_ext_overrides(cpi);
2108
2109  cpi->Source = vp9_scale_if_required(cm, cpi->un_scaled_source,
2110                                      &cpi->scaled_source);
2111
2112  if (cpi->unscaled_last_source != NULL)
2113    cpi->Last_Source = vp9_scale_if_required(cm, cpi->unscaled_last_source,
2114                                             &cpi->scaled_last_source);
2115
2116  vp9_scale_references(cpi);
2117
2118  vp9_clear_system_state();
2119
2120  // Enable or disable mode based tweaking of the zbin.
2121  // For 2 pass only used where GF/ARF prediction quality
2122  // is above a threshold.
2123  cpi->zbin_mode_boost = 0;
2124  cpi->zbin_mode_boost_enabled = 0;
2125
2126  // Set the arf sign bias for this frame.
2127  set_arf_sign_bias(cpi);
2128
2129  // Set default state for segment based loop filter update flags.
2130  cm->lf.mode_ref_delta_update = 0;
2131
2132  set_mv_search_params(cpi);
2133
2134  if (cpi->oxcf.pass == 2 &&
2135      cpi->sf.adaptive_interp_filter_search)
2136    cpi->sf.interp_filter_search_mask =
2137        setup_interp_filter_search_mask(cpi);
2138
2139
2140  // Set various flags etc to special state if it is a key frame.
2141  if (frame_is_intra_only(cm)) {
2142    // Reset the loop filter deltas and segmentation map.
2143    vp9_reset_segment_features(&cm->seg);
2144
2145    // If segmentation is enabled force a map update for key frames.
2146    if (seg->enabled) {
2147      seg->update_map = 1;
2148      seg->update_data = 1;
2149    }
2150
2151    // The alternate reference frame cannot be active for a key frame.
2152    cpi->rc.source_alt_ref_active = 0;
2153
2154    cm->error_resilient_mode = oxcf->error_resilient_mode;
2155
2156    // By default, encoder assumes decoder can use prev_mi.
2157    if (cm->error_resilient_mode) {
2158      cm->frame_parallel_decoding_mode = 1;
2159      cm->reset_frame_context = 0;
2160      cm->refresh_frame_context = 0;
2161    } else if (cm->intra_only) {
2162      cm->frame_parallel_decoding_mode = oxcf->frame_parallel_decoding_mode;
2163      // Only reset the current context.
2164      cm->reset_frame_context = 2;
2165    }
2166  }
2167  if (is_two_pass_svc(cpi) && cm->error_resilient_mode == 0) {
2168    cm->frame_context_idx =
2169        cpi->svc.spatial_layer_id * cpi->svc.number_temporal_layers +
2170        cpi->svc.temporal_layer_id;
2171
2172    // The probs will be updated based on the frame type of its previous
2173    // frame if frame_parallel_decoding_mode is 0. The type may vary for
2174    // the frame after a key frame in base layer since we may drop enhancement
2175    // layers. So set frame_parallel_decoding_mode to 1 in this case.
2176    if (cpi->svc.number_temporal_layers == 1) {
2177      if (cpi->svc.spatial_layer_id == 0 &&
2178          cpi->svc.layer_context[0].last_frame_type == KEY_FRAME)
2179        cm->frame_parallel_decoding_mode = 1;
2180      else
2181        cm->frame_parallel_decoding_mode = 0;
2182    } else if (cpi->svc.spatial_layer_id == 0) {
2183      // Find the 2nd frame in temporal base layer and 1st frame in temporal
2184      // enhancement layers from the key frame.
2185      int i;
2186      for (i = 0; i < cpi->svc.number_temporal_layers; ++i) {
2187        if (cpi->svc.layer_context[0].frames_from_key_frame == 1 << i) {
2188          cm->frame_parallel_decoding_mode = 1;
2189          break;
2190        }
2191      }
2192      if (i == cpi->svc.number_temporal_layers)
2193        cm->frame_parallel_decoding_mode = 0;
2194    }
2195  }
2196
2197  // Configure experimental use of segmentation for enhanced coding of
2198  // static regions if indicated.
2199  // Only allowed in second pass of two pass (as requires lagged coding)
2200  // and if the relevant speed feature flag is set.
2201  if (oxcf->pass == 2 && cpi->sf.static_segmentation)
2202    configure_static_seg_features(cpi);
2203
2204  // Check if the current frame is skippable for the partition search in the
2205  // second pass according to the first pass stats
2206  if (oxcf->pass == 2 &&
2207      (!cpi->use_svc || is_two_pass_svc(cpi))) {
2208    cpi->skippable_frame = is_skippable_frame(cpi);
2209  }
2210
2211  // For 1 pass CBR, check if we are dropping this frame.
2212  // Never drop on key frame.
2213  if (oxcf->pass == 0 &&
2214      oxcf->rc_mode == VPX_CBR &&
2215      cm->frame_type != KEY_FRAME) {
2216    if (vp9_rc_drop_frame(cpi)) {
2217      vp9_rc_postencode_update_drop_frame(cpi);
2218      ++cm->current_video_frame;
2219      return;
2220    }
2221  }
2222
2223  vp9_clear_system_state();
2224
2225#if CONFIG_VP9_POSTPROC
2226  if (oxcf->noise_sensitivity > 0) {
2227    int l = 0;
2228    switch (oxcf->noise_sensitivity) {
2229      case 1:
2230        l = 20;
2231        break;
2232      case 2:
2233        l = 40;
2234        break;
2235      case 3:
2236        l = 60;
2237        break;
2238      case 4:
2239      case 5:
2240        l = 100;
2241        break;
2242      case 6:
2243        l = 150;
2244        break;
2245    }
2246    vp9_denoise(cpi->Source, cpi->Source, l);
2247  }
2248#endif
2249
2250#if CONFIG_INTERNAL_STATS
2251  {
2252    int i;
2253    for (i = 0; i < MAX_MODES; ++i)
2254      cpi->mode_chosen_counts[i] = 0;
2255  }
2256#endif
2257
2258  vp9_set_speed_features(cpi);
2259
2260  vp9_set_rd_speed_thresholds(cpi);
2261  vp9_set_rd_speed_thresholds_sub8x8(cpi);
2262
2263  // Decide q and q bounds.
2264  q = vp9_rc_pick_q_and_bounds(cpi, &bottom_index, &top_index);
2265
2266  if (!frame_is_intra_only(cm)) {
2267    cm->interp_filter = cpi->sf.default_interp_filter;
2268    /* TODO: Decide this more intelligently */
2269    vp9_set_high_precision_mv(cpi, q < HIGH_PRECISION_MV_QTHRESH);
2270  }
2271
2272  if (cpi->sf.recode_loop == DISALLOW_RECODE) {
2273    encode_without_recode_loop(cpi, q);
2274  } else {
2275    encode_with_recode_loop(cpi, size, dest, q, bottom_index, top_index);
2276  }
2277
2278#if CONFIG_VP9_TEMPORAL_DENOISING
2279#ifdef OUTPUT_YUV_DENOISED
2280  if (oxcf->noise_sensitivity > 0) {
2281    vp9_write_yuv_frame_420(&cpi->denoiser.running_avg_y[INTRA_FRAME],
2282                            yuv_denoised_file);
2283  }
2284#endif
2285#endif
2286
2287
2288  // Special case code to reduce pulsing when key frames are forced at a
2289  // fixed interval. Note the reconstruction error if it is the frame before
2290  // the force key frame
2291  if (cpi->rc.next_key_frame_forced && cpi->rc.frames_to_key == 1) {
2292    cpi->ambient_err = vp9_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
2293  }
2294
2295  // If the encoder forced a KEY_FRAME decision
2296  if (cm->frame_type == KEY_FRAME)
2297    cpi->refresh_last_frame = 1;
2298
2299  cm->frame_to_show = get_frame_new_buffer(cm);
2300
2301  // Pick the loop filter level for the frame.
2302  loopfilter_frame(cpi, cm);
2303
2304  // build the bitstream
2305  vp9_pack_bitstream(cpi, dest, size);
2306
2307  if (cm->seg.update_map)
2308    update_reference_segmentation_map(cpi);
2309
2310  release_scaled_references(cpi);
2311  vp9_update_reference_frames(cpi);
2312
2313  for (t = TX_4X4; t <= TX_32X32; t++)
2314    full_to_model_counts(cm->counts.coef[t], cpi->coef_counts[t]);
2315
2316  if (!cm->error_resilient_mode && !cm->frame_parallel_decoding_mode)
2317    vp9_adapt_coef_probs(cm);
2318
2319  if (!frame_is_intra_only(cm)) {
2320    if (!cm->error_resilient_mode && !cm->frame_parallel_decoding_mode) {
2321      vp9_adapt_mode_probs(cm);
2322      vp9_adapt_mv_probs(cm, cm->allow_high_precision_mv);
2323    }
2324  }
2325
2326  if (cpi->refresh_golden_frame == 1)
2327    cpi->frame_flags |= FRAMEFLAGS_GOLDEN;
2328  else
2329    cpi->frame_flags &= ~FRAMEFLAGS_GOLDEN;
2330
2331  if (cpi->refresh_alt_ref_frame == 1)
2332    cpi->frame_flags |= FRAMEFLAGS_ALTREF;
2333  else
2334    cpi->frame_flags &= ~FRAMEFLAGS_ALTREF;
2335
2336  cpi->ref_frame_flags = get_ref_frame_flags(cpi);
2337
2338  cm->last_frame_type = cm->frame_type;
2339  vp9_rc_postencode_update(cpi, *size);
2340
2341#if 0
2342  output_frame_level_debug_stats(cpi);
2343#endif
2344
2345  if (cm->frame_type == KEY_FRAME) {
2346    // Tell the caller that the frame was coded as a key frame
2347    *frame_flags = cpi->frame_flags | FRAMEFLAGS_KEY;
2348  } else {
2349    *frame_flags = cpi->frame_flags & ~FRAMEFLAGS_KEY;
2350  }
2351
2352  // Clear the one shot update flags for segmentation map and mode/ref loop
2353  // filter deltas.
2354  cm->seg.update_map = 0;
2355  cm->seg.update_data = 0;
2356  cm->lf.mode_ref_delta_update = 0;
2357
2358  // keep track of the last coded dimensions
2359  cm->last_width = cm->width;
2360  cm->last_height = cm->height;
2361
2362  // reset to normal state now that we are done.
2363  if (!cm->show_existing_frame) {
2364    if (is_two_pass_svc(cpi) && cm->error_resilient_mode == 0)
2365      cm->last_show_frame = 0;
2366    else
2367      cm->last_show_frame = cm->show_frame;
2368  }
2369
2370  if (cm->show_frame) {
2371    vp9_swap_mi_and_prev_mi(cm);
2372
2373    // Don't increment frame counters if this was an altref buffer
2374    // update not a real frame
2375    ++cm->current_video_frame;
2376    if (cpi->use_svc)
2377      vp9_inc_frame_in_layer(cpi);
2378  }
2379
2380  if (is_two_pass_svc(cpi))
2381    cpi->svc.layer_context[cpi->svc.spatial_layer_id].last_frame_type =
2382        cm->frame_type;
2383}
2384
2385static void SvcEncode(VP9_COMP *cpi, size_t *size, uint8_t *dest,
2386                      unsigned int *frame_flags) {
2387  vp9_rc_get_svc_params(cpi);
2388  encode_frame_to_data_rate(cpi, size, dest, frame_flags);
2389}
2390
2391static void Pass0Encode(VP9_COMP *cpi, size_t *size, uint8_t *dest,
2392                        unsigned int *frame_flags) {
2393  if (cpi->oxcf.rc_mode == VPX_CBR) {
2394    vp9_rc_get_one_pass_cbr_params(cpi);
2395  } else {
2396    vp9_rc_get_one_pass_vbr_params(cpi);
2397  }
2398  encode_frame_to_data_rate(cpi, size, dest, frame_flags);
2399}
2400
2401static void Pass2Encode(VP9_COMP *cpi, size_t *size,
2402                        uint8_t *dest, unsigned int *frame_flags) {
2403  cpi->allow_encode_breakout = ENCODE_BREAKOUT_ENABLED;
2404  encode_frame_to_data_rate(cpi, size, dest, frame_flags);
2405  vp9_twopass_postencode_update(cpi);
2406}
2407
2408static void init_motion_estimation(VP9_COMP *cpi) {
2409  int y_stride = cpi->scaled_source.y_stride;
2410
2411  if (cpi->sf.mv.search_method == NSTEP) {
2412    vp9_init3smotion_compensation(&cpi->ss_cfg, y_stride);
2413  } else if (cpi->sf.mv.search_method == DIAMOND) {
2414    vp9_init_dsmotion_compensation(&cpi->ss_cfg, y_stride);
2415  }
2416}
2417
2418static void check_initial_width(VP9_COMP *cpi, int subsampling_x,
2419                                int subsampling_y) {
2420  VP9_COMMON *const cm = &cpi->common;
2421
2422  if (!cpi->initial_width) {
2423    cm->subsampling_x = subsampling_x;
2424    cm->subsampling_y = subsampling_y;
2425
2426    alloc_raw_frame_buffers(cpi);
2427    alloc_ref_frame_buffers(cpi);
2428    alloc_util_frame_buffers(cpi);
2429
2430    init_motion_estimation(cpi);
2431
2432    cpi->initial_width = cm->width;
2433    cpi->initial_height = cm->height;
2434  }
2435}
2436
2437
2438int vp9_receive_raw_frame(VP9_COMP *cpi, unsigned int frame_flags,
2439                          YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
2440                          int64_t end_time) {
2441  VP9_COMMON *cm = &cpi->common;
2442  struct vpx_usec_timer timer;
2443  int res = 0;
2444  const int subsampling_x = sd->uv_width  < sd->y_width;
2445  const int subsampling_y = sd->uv_height < sd->y_height;
2446
2447  check_initial_width(cpi, subsampling_x, subsampling_y);
2448
2449  vpx_usec_timer_start(&timer);
2450
2451  if (vp9_lookahead_push(cpi->lookahead, sd, time_stamp, end_time, frame_flags))
2452    res = -1;
2453  vpx_usec_timer_mark(&timer);
2454  cpi->time_receive_data += vpx_usec_timer_elapsed(&timer);
2455
2456  if ((cm->profile == PROFILE_0 || cm->profile == PROFILE_2) &&
2457      (subsampling_x != 1 || subsampling_y != 1)) {
2458    vpx_internal_error(&cm->error, VPX_CODEC_INVALID_PARAM,
2459                       "Non-4:2:0 color space requires profile 1 or 3");
2460    res = -1;
2461  }
2462  if ((cm->profile == PROFILE_1 || cm->profile == PROFILE_3) &&
2463      (subsampling_x == 1 && subsampling_y == 1)) {
2464    vpx_internal_error(&cm->error, VPX_CODEC_INVALID_PARAM,
2465                       "4:2:0 color space requires profile 0 or 2");
2466    res = -1;
2467  }
2468
2469  return res;
2470}
2471
2472
2473static int frame_is_reference(const VP9_COMP *cpi) {
2474  const VP9_COMMON *cm = &cpi->common;
2475
2476  return cm->frame_type == KEY_FRAME ||
2477         cpi->refresh_last_frame ||
2478         cpi->refresh_golden_frame ||
2479         cpi->refresh_alt_ref_frame ||
2480         cm->refresh_frame_context ||
2481         cm->lf.mode_ref_delta_update ||
2482         cm->seg.update_map ||
2483         cm->seg.update_data;
2484}
2485
2486void adjust_frame_rate(VP9_COMP *cpi,
2487                       const struct lookahead_entry *source) {
2488  int64_t this_duration;
2489  int step = 0;
2490
2491  if (source->ts_start == cpi->first_time_stamp_ever) {
2492    this_duration = source->ts_end - source->ts_start;
2493    step = 1;
2494  } else {
2495    int64_t last_duration = cpi->last_end_time_stamp_seen
2496        - cpi->last_time_stamp_seen;
2497
2498    this_duration = source->ts_end - cpi->last_end_time_stamp_seen;
2499
2500    // do a step update if the duration changes by 10%
2501    if (last_duration)
2502      step = (int)((this_duration - last_duration) * 10 / last_duration);
2503  }
2504
2505  if (this_duration) {
2506    if (step) {
2507      vp9_new_framerate(cpi, 10000000.0 / this_duration);
2508    } else {
2509      // Average this frame's rate into the last second's average
2510      // frame rate. If we haven't seen 1 second yet, then average
2511      // over the whole interval seen.
2512      const double interval = MIN((double)(source->ts_end
2513                                   - cpi->first_time_stamp_ever), 10000000.0);
2514      double avg_duration = 10000000.0 / cpi->framerate;
2515      avg_duration *= (interval - avg_duration + this_duration);
2516      avg_duration /= interval;
2517
2518      vp9_new_framerate(cpi, 10000000.0 / avg_duration);
2519    }
2520  }
2521  cpi->last_time_stamp_seen = source->ts_start;
2522  cpi->last_end_time_stamp_seen = source->ts_end;
2523}
2524
2525// Returns 0 if this is not an alt ref else the offset of the source frame
2526// used as the arf midpoint.
2527static int get_arf_src_index(VP9_COMP *cpi) {
2528  RATE_CONTROL *const rc = &cpi->rc;
2529  int arf_src_index = 0;
2530  if (is_altref_enabled(cpi)) {
2531    if (cpi->oxcf.pass == 2) {
2532      const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
2533      if (gf_group->update_type[gf_group->index] == ARF_UPDATE) {
2534        arf_src_index = gf_group->arf_src_offset[gf_group->index];
2535      }
2536    } else if (rc->source_alt_ref_pending) {
2537      arf_src_index = rc->frames_till_gf_update_due;
2538    }
2539  }
2540  return arf_src_index;
2541}
2542
2543static void check_src_altref(VP9_COMP *cpi,
2544                             const struct lookahead_entry *source) {
2545  RATE_CONTROL *const rc = &cpi->rc;
2546
2547  if (cpi->oxcf.pass == 2) {
2548    const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
2549    rc->is_src_frame_alt_ref =
2550      (gf_group->update_type[gf_group->index] == OVERLAY_UPDATE);
2551  } else {
2552    rc->is_src_frame_alt_ref = cpi->alt_ref_source &&
2553                               (source == cpi->alt_ref_source);
2554  }
2555
2556  if (rc->is_src_frame_alt_ref) {
2557    // Current frame is an ARF overlay frame.
2558    cpi->alt_ref_source = NULL;
2559
2560    // Don't refresh the last buffer for an ARF overlay frame. It will
2561    // become the GF so preserve last as an alternative prediction option.
2562    cpi->refresh_last_frame = 0;
2563  }
2564}
2565
2566int vp9_get_compressed_data(VP9_COMP *cpi, unsigned int *frame_flags,
2567                            size_t *size, uint8_t *dest,
2568                            int64_t *time_stamp, int64_t *time_end, int flush) {
2569  const VP9EncoderConfig *const oxcf = &cpi->oxcf;
2570  VP9_COMMON *const cm = &cpi->common;
2571  MACROBLOCKD *const xd = &cpi->mb.e_mbd;
2572  RATE_CONTROL *const rc = &cpi->rc;
2573  struct vpx_usec_timer  cmptimer;
2574  YV12_BUFFER_CONFIG *force_src_buffer = NULL;
2575  struct lookahead_entry *last_source = NULL;
2576  struct lookahead_entry *source = NULL;
2577  MV_REFERENCE_FRAME ref_frame;
2578  int arf_src_index;
2579
2580  if (is_two_pass_svc(cpi)) {
2581#if CONFIG_SPATIAL_SVC
2582    vp9_svc_start_frame(cpi);
2583#endif
2584    if (oxcf->pass == 2)
2585      vp9_restore_layer_context(cpi);
2586  }
2587
2588  vpx_usec_timer_start(&cmptimer);
2589
2590  vp9_set_high_precision_mv(cpi, ALTREF_HIGH_PRECISION_MV);
2591
2592  // Normal defaults
2593  cm->reset_frame_context = 0;
2594  cm->refresh_frame_context = 1;
2595  cpi->refresh_last_frame = 1;
2596  cpi->refresh_golden_frame = 0;
2597  cpi->refresh_alt_ref_frame = 0;
2598
2599  // Should we encode an arf frame.
2600  arf_src_index = get_arf_src_index(cpi);
2601  if (arf_src_index) {
2602    assert(arf_src_index <= rc->frames_to_key);
2603
2604    if ((source = vp9_lookahead_peek(cpi->lookahead, arf_src_index)) != NULL) {
2605      cpi->alt_ref_source = source;
2606
2607#if CONFIG_SPATIAL_SVC
2608      if (is_two_pass_svc(cpi) && cpi->svc.spatial_layer_id > 0) {
2609        int i;
2610        // Reference a hidden frame from a lower layer
2611        for (i = cpi->svc.spatial_layer_id - 1; i >= 0; --i) {
2612          if (oxcf->ss_play_alternate[i]) {
2613            cpi->gld_fb_idx = cpi->svc.layer_context[i].alt_ref_idx;
2614            break;
2615          }
2616        }
2617      }
2618      cpi->svc.layer_context[cpi->svc.spatial_layer_id].has_alt_frame = 1;
2619#endif
2620
2621      if (oxcf->arnr_max_frames > 0) {
2622        // Produce the filtered ARF frame.
2623        vp9_temporal_filter(cpi, arf_src_index);
2624        vp9_extend_frame_borders(&cpi->alt_ref_buffer);
2625        force_src_buffer = &cpi->alt_ref_buffer;
2626      }
2627
2628      cm->show_frame = 0;
2629      cpi->refresh_alt_ref_frame = 1;
2630      cpi->refresh_golden_frame = 0;
2631      cpi->refresh_last_frame = 0;
2632      rc->is_src_frame_alt_ref = 0;
2633      rc->source_alt_ref_pending = 0;
2634    } else {
2635      rc->source_alt_ref_pending = 0;
2636    }
2637  }
2638
2639  if (!source) {
2640    // Get last frame source.
2641    if (cm->current_video_frame > 0) {
2642      if ((last_source = vp9_lookahead_peek(cpi->lookahead, -1)) == NULL)
2643        return -1;
2644    }
2645
2646    // Read in the source frame.
2647#if CONFIG_SPATIAL_SVC
2648    if (is_two_pass_svc(cpi))
2649      source = vp9_svc_lookahead_pop(cpi, cpi->lookahead, flush);
2650    else
2651#endif
2652      source = vp9_lookahead_pop(cpi->lookahead, flush);
2653    if (source != NULL) {
2654      cm->show_frame = 1;
2655      cm->intra_only = 0;
2656
2657      // Check to see if the frame should be encoded as an arf overlay.
2658      check_src_altref(cpi, source);
2659    }
2660  }
2661
2662  if (source) {
2663    cpi->un_scaled_source = cpi->Source = force_src_buffer ? force_src_buffer
2664                                                           : &source->img;
2665
2666    cpi->unscaled_last_source = last_source != NULL ? &last_source->img : NULL;
2667
2668    *time_stamp = source->ts_start;
2669    *time_end = source->ts_end;
2670    *frame_flags = (source->flags & VPX_EFLAG_FORCE_KF) ? FRAMEFLAGS_KEY : 0;
2671
2672  } else {
2673    *size = 0;
2674    if (flush && oxcf->pass == 1 && !cpi->twopass.first_pass_done) {
2675      vp9_end_first_pass(cpi);    /* get last stats packet */
2676      cpi->twopass.first_pass_done = 1;
2677    }
2678    return -1;
2679  }
2680
2681  if (source->ts_start < cpi->first_time_stamp_ever) {
2682    cpi->first_time_stamp_ever = source->ts_start;
2683    cpi->last_end_time_stamp_seen = source->ts_start;
2684  }
2685
2686  // Clear down mmx registers
2687  vp9_clear_system_state();
2688
2689  // adjust frame rates based on timestamps given
2690  if (cm->show_frame) {
2691    adjust_frame_rate(cpi, source);
2692  }
2693
2694  if (cpi->svc.number_temporal_layers > 1 &&
2695      oxcf->rc_mode == VPX_CBR) {
2696    vp9_update_temporal_layer_framerate(cpi);
2697    vp9_restore_layer_context(cpi);
2698  }
2699
2700  // start with a 0 size frame
2701  *size = 0;
2702
2703  /* find a free buffer for the new frame, releasing the reference previously
2704   * held.
2705   */
2706  cm->frame_bufs[cm->new_fb_idx].ref_count--;
2707  cm->new_fb_idx = get_free_fb(cm);
2708
2709  // For two pass encodes analyse the first pass stats and determine
2710  // the bit allocation and other parameters for this frame / group of frames.
2711  if ((oxcf->pass == 2) && (!cpi->use_svc || is_two_pass_svc(cpi))) {
2712    vp9_rc_get_second_pass_params(cpi);
2713  }
2714
2715  if (!cpi->use_svc && cpi->multi_arf_allowed) {
2716    if (cm->frame_type == KEY_FRAME) {
2717      init_buffer_indices(cpi);
2718    } else if (oxcf->pass == 2) {
2719      const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
2720      cpi->alt_fb_idx = gf_group->arf_ref_idx[gf_group->index];
2721    }
2722  }
2723
2724  cpi->frame_flags = *frame_flags;
2725
2726  if (oxcf->pass == 2 &&
2727      cm->current_video_frame == 0 &&
2728      oxcf->allow_spatial_resampling &&
2729      oxcf->rc_mode == VPX_VBR) {
2730    // Internal scaling is triggered on the first frame.
2731    vp9_set_size_literal(cpi, oxcf->scaled_frame_width,
2732                         oxcf->scaled_frame_height);
2733  }
2734
2735  // Reset the frame pointers to the current frame size
2736  vp9_realloc_frame_buffer(get_frame_new_buffer(cm),
2737                           cm->width, cm->height,
2738                           cm->subsampling_x, cm->subsampling_y,
2739#if CONFIG_VP9_HIGHBITDEPTH
2740                           cm->use_highbitdepth,
2741#endif
2742                           VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL);
2743
2744  alloc_util_frame_buffers(cpi);
2745  init_motion_estimation(cpi);
2746
2747  for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
2748    const int idx = cm->ref_frame_map[get_ref_frame_idx(cpi, ref_frame)];
2749    YV12_BUFFER_CONFIG *const buf = &cm->frame_bufs[idx].buf;
2750    RefBuffer *const ref_buf = &cm->frame_refs[ref_frame - 1];
2751    ref_buf->buf = buf;
2752    ref_buf->idx = idx;
2753#if CONFIG_VP9_HIGHBITDEPTH
2754    vp9_setup_scale_factors_for_frame(&ref_buf->sf,
2755                                      buf->y_crop_width, buf->y_crop_height,
2756                                      cm->width, cm->height,
2757                                      (buf->flags & YV12_FLAG_HIGHBITDEPTH) ?
2758                                          1 : 0);
2759#else
2760    vp9_setup_scale_factors_for_frame(&ref_buf->sf,
2761                                      buf->y_crop_width, buf->y_crop_height,
2762                                      cm->width, cm->height);
2763#endif
2764    if (vp9_is_scaled(&ref_buf->sf))
2765      vp9_extend_frame_borders(buf);
2766  }
2767
2768  set_ref_ptrs(cm, xd, LAST_FRAME, LAST_FRAME);
2769
2770  if (oxcf->aq_mode == VARIANCE_AQ) {
2771    vp9_vaq_init();
2772  }
2773
2774  if (oxcf->pass == 1 &&
2775      (!cpi->use_svc || is_two_pass_svc(cpi))) {
2776    const int lossless = is_lossless_requested(oxcf);
2777#if CONFIG_VP9_HIGHBITDEPTH
2778    if (cpi->oxcf.use_highbitdepth)
2779      cpi->mb.fwd_txm4x4 = lossless ? vp9_high_fwht4x4 : vp9_high_fdct4x4;
2780    else
2781      cpi->mb.fwd_txm4x4 = lossless ? vp9_fwht4x4 : vp9_fdct4x4;
2782    cpi->mb.high_itxm_add = lossless ? vp9_high_iwht4x4_add :
2783                                       vp9_high_idct4x4_add;
2784#else
2785    cpi->mb.fwd_txm4x4 = lossless ? vp9_fwht4x4 : vp9_fdct4x4;
2786#endif
2787    cpi->mb.itxm_add = lossless ? vp9_iwht4x4_add : vp9_idct4x4_add;
2788    vp9_first_pass(cpi, source);
2789  } else if (oxcf->pass == 2 &&
2790      (!cpi->use_svc || is_two_pass_svc(cpi))) {
2791    Pass2Encode(cpi, size, dest, frame_flags);
2792  } else if (cpi->use_svc) {
2793    SvcEncode(cpi, size, dest, frame_flags);
2794  } else {
2795    // One pass encode
2796    Pass0Encode(cpi, size, dest, frame_flags);
2797  }
2798
2799  if (cm->refresh_frame_context)
2800    cm->frame_contexts[cm->frame_context_idx] = cm->fc;
2801
2802  // Frame was dropped, release scaled references.
2803  if (*size == 0) {
2804    release_scaled_references(cpi);
2805  }
2806
2807  if (*size > 0) {
2808    cpi->droppable = !frame_is_reference(cpi);
2809  }
2810
2811  // Save layer specific state.
2812  if ((cpi->svc.number_temporal_layers > 1 &&
2813       oxcf->rc_mode == VPX_CBR) ||
2814      ((cpi->svc.number_temporal_layers > 1 ||
2815        cpi->svc.number_spatial_layers > 1) &&
2816       oxcf->pass == 2)) {
2817    vp9_save_layer_context(cpi);
2818  }
2819
2820  vpx_usec_timer_mark(&cmptimer);
2821  cpi->time_compress_data += vpx_usec_timer_elapsed(&cmptimer);
2822
2823  if (cpi->b_calculate_psnr && oxcf->pass != 1 && cm->show_frame)
2824    generate_psnr_packet(cpi);
2825
2826#if CONFIG_INTERNAL_STATS
2827
2828  if (oxcf->pass != 1) {
2829    cpi->bytes += (int)(*size);
2830
2831    if (cm->show_frame) {
2832      cpi->count++;
2833
2834      if (cpi->b_calculate_psnr) {
2835        YV12_BUFFER_CONFIG *orig = cpi->Source;
2836        YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
2837        YV12_BUFFER_CONFIG *pp = &cm->post_proc_buffer;
2838        PSNR_STATS psnr;
2839        calc_psnr(orig, recon, &psnr);
2840
2841        cpi->total += psnr.psnr[0];
2842        cpi->total_y += psnr.psnr[1];
2843        cpi->total_u += psnr.psnr[2];
2844        cpi->total_v += psnr.psnr[3];
2845        cpi->total_sq_error += psnr.sse[0];
2846        cpi->total_samples += psnr.samples[0];
2847
2848        {
2849          PSNR_STATS psnr2;
2850          double frame_ssim2 = 0, weight = 0;
2851#if CONFIG_VP9_POSTPROC
2852          // TODO(agrange) Add resizing of post-proc buffer in here when the
2853          // encoder is changed to use on-demand buffer allocation.
2854          vp9_deblock(cm->frame_to_show, &cm->post_proc_buffer,
2855                      cm->lf.filter_level * 10 / 6);
2856#endif
2857          vp9_clear_system_state();
2858
2859          calc_psnr(orig, pp, &psnr2);
2860
2861          cpi->totalp += psnr2.psnr[0];
2862          cpi->totalp_y += psnr2.psnr[1];
2863          cpi->totalp_u += psnr2.psnr[2];
2864          cpi->totalp_v += psnr2.psnr[3];
2865          cpi->totalp_sq_error += psnr2.sse[0];
2866          cpi->totalp_samples += psnr2.samples[0];
2867
2868          frame_ssim2 = vp9_calc_ssim(orig, recon, &weight);
2869
2870          cpi->summed_quality += frame_ssim2 * weight;
2871          cpi->summed_weights += weight;
2872
2873          frame_ssim2 = vp9_calc_ssim(orig, &cm->post_proc_buffer, &weight);
2874
2875          cpi->summedp_quality += frame_ssim2 * weight;
2876          cpi->summedp_weights += weight;
2877#if 0
2878          {
2879            FILE *f = fopen("q_used.stt", "a");
2880            fprintf(f, "%5d : Y%f7.3:U%f7.3:V%f7.3:F%f7.3:S%7.3f\n",
2881                    cpi->common.current_video_frame, y2, u2, v2,
2882                    frame_psnr2, frame_ssim2);
2883            fclose(f);
2884          }
2885#endif
2886        }
2887      }
2888
2889
2890      if (cpi->b_calculate_ssimg) {
2891        double y, u, v, frame_all;
2892        frame_all = vp9_calc_ssimg(cpi->Source, cm->frame_to_show, &y, &u, &v);
2893        cpi->total_ssimg_y += y;
2894        cpi->total_ssimg_u += u;
2895        cpi->total_ssimg_v += v;
2896        cpi->total_ssimg_all += frame_all;
2897      }
2898    }
2899  }
2900
2901#endif
2902
2903  if (is_two_pass_svc(cpi) && cm->show_frame) {
2904    ++cpi->svc.spatial_layer_to_encode;
2905    if (cpi->svc.spatial_layer_to_encode >= cpi->svc.number_spatial_layers)
2906      cpi->svc.spatial_layer_to_encode = 0;
2907  }
2908  return 0;
2909}
2910
2911int vp9_get_preview_raw_frame(VP9_COMP *cpi, YV12_BUFFER_CONFIG *dest,
2912                              vp9_ppflags_t *flags) {
2913  VP9_COMMON *cm = &cpi->common;
2914#if !CONFIG_VP9_POSTPROC
2915  (void)flags;
2916#endif
2917
2918  if (!cm->show_frame) {
2919    return -1;
2920  } else {
2921    int ret;
2922#if CONFIG_VP9_POSTPROC
2923    ret = vp9_post_proc_frame(cm, dest, flags);
2924#else
2925    if (cm->frame_to_show) {
2926      *dest = *cm->frame_to_show;
2927      dest->y_width = cm->width;
2928      dest->y_height = cm->height;
2929      dest->uv_width = cm->width >> cm->subsampling_x;
2930      dest->uv_height = cm->height >> cm->subsampling_y;
2931      ret = 0;
2932    } else {
2933      ret = -1;
2934    }
2935#endif  // !CONFIG_VP9_POSTPROC
2936    vp9_clear_system_state();
2937    return ret;
2938  }
2939}
2940
2941int vp9_set_active_map(VP9_COMP *cpi, unsigned char *map, int rows, int cols) {
2942  if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols) {
2943    const int mi_rows = cpi->common.mi_rows;
2944    const int mi_cols = cpi->common.mi_cols;
2945    if (map) {
2946      int r, c;
2947      for (r = 0; r < mi_rows; r++) {
2948        for (c = 0; c < mi_cols; c++) {
2949          cpi->segmentation_map[r * mi_cols + c] =
2950              !map[(r >> 1) * cols + (c >> 1)];
2951        }
2952      }
2953      vp9_enable_segfeature(&cpi->common.seg, 1, SEG_LVL_SKIP);
2954      vp9_enable_segmentation(&cpi->common.seg);
2955    } else {
2956      vp9_disable_segmentation(&cpi->common.seg);
2957    }
2958    return 0;
2959  } else {
2960    return -1;
2961  }
2962}
2963
2964int vp9_set_internal_size(VP9_COMP *cpi,
2965                          VPX_SCALING horiz_mode, VPX_SCALING vert_mode) {
2966  VP9_COMMON *cm = &cpi->common;
2967  int hr = 0, hs = 0, vr = 0, vs = 0;
2968
2969  if (horiz_mode > ONETWO || vert_mode > ONETWO)
2970    return -1;
2971
2972  Scale2Ratio(horiz_mode, &hr, &hs);
2973  Scale2Ratio(vert_mode, &vr, &vs);
2974
2975  // always go to the next whole number
2976  cm->width = (hs - 1 + cpi->oxcf.width * hr) / hs;
2977  cm->height = (vs - 1 + cpi->oxcf.height * vr) / vs;
2978  assert(cm->width <= cpi->initial_width);
2979  assert(cm->height <= cpi->initial_height);
2980
2981  update_frame_size(cpi);
2982
2983  return 0;
2984}
2985
2986int vp9_set_size_literal(VP9_COMP *cpi, unsigned int width,
2987                         unsigned int height) {
2988  VP9_COMMON *cm = &cpi->common;
2989
2990  check_initial_width(cpi, 1, 1);
2991
2992  if (width) {
2993    cm->width = width;
2994    if (cm->width * 5 < cpi->initial_width) {
2995      cm->width = cpi->initial_width / 5 + 1;
2996      printf("Warning: Desired width too small, changed to %d\n", cm->width);
2997    }
2998    if (cm->width > cpi->initial_width) {
2999      cm->width = cpi->initial_width;
3000      printf("Warning: Desired width too large, changed to %d\n", cm->width);
3001    }
3002  }
3003
3004  if (height) {
3005    cm->height = height;
3006    if (cm->height * 5 < cpi->initial_height) {
3007      cm->height = cpi->initial_height / 5 + 1;
3008      printf("Warning: Desired height too small, changed to %d\n", cm->height);
3009    }
3010    if (cm->height > cpi->initial_height) {
3011      cm->height = cpi->initial_height;
3012      printf("Warning: Desired height too large, changed to %d\n", cm->height);
3013    }
3014  }
3015  assert(cm->width <= cpi->initial_width);
3016  assert(cm->height <= cpi->initial_height);
3017
3018  update_frame_size(cpi);
3019
3020  return 0;
3021}
3022
3023void vp9_set_svc(VP9_COMP *cpi, int use_svc) {
3024  cpi->use_svc = use_svc;
3025  return;
3026}
3027
3028int vp9_get_y_sse(const YV12_BUFFER_CONFIG *a, const YV12_BUFFER_CONFIG *b) {
3029  assert(a->y_crop_width == b->y_crop_width);
3030  assert(a->y_crop_height == b->y_crop_height);
3031
3032  return (int)get_sse(a->y_buffer, a->y_stride, b->y_buffer, b->y_stride,
3033                      a->y_crop_width, a->y_crop_height);
3034}
3035
3036
3037int vp9_get_quantizer(VP9_COMP *cpi) {
3038  return cpi->common.base_qindex;
3039}
3040
3041void vp9_apply_encoding_flags(VP9_COMP *cpi, vpx_enc_frame_flags_t flags) {
3042  if (flags & (VP8_EFLAG_NO_REF_LAST | VP8_EFLAG_NO_REF_GF |
3043               VP8_EFLAG_NO_REF_ARF)) {
3044    int ref = 7;
3045
3046    if (flags & VP8_EFLAG_NO_REF_LAST)
3047      ref ^= VP9_LAST_FLAG;
3048
3049    if (flags & VP8_EFLAG_NO_REF_GF)
3050      ref ^= VP9_GOLD_FLAG;
3051
3052    if (flags & VP8_EFLAG_NO_REF_ARF)
3053      ref ^= VP9_ALT_FLAG;
3054
3055    vp9_use_as_reference(cpi, ref);
3056  }
3057
3058  if (flags & (VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF |
3059               VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_FORCE_GF |
3060               VP8_EFLAG_FORCE_ARF)) {
3061    int upd = 7;
3062
3063    if (flags & VP8_EFLAG_NO_UPD_LAST)
3064      upd ^= VP9_LAST_FLAG;
3065
3066    if (flags & VP8_EFLAG_NO_UPD_GF)
3067      upd ^= VP9_GOLD_FLAG;
3068
3069    if (flags & VP8_EFLAG_NO_UPD_ARF)
3070      upd ^= VP9_ALT_FLAG;
3071
3072    vp9_update_reference(cpi, upd);
3073  }
3074
3075  if (flags & VP8_EFLAG_NO_UPD_ENTROPY) {
3076    vp9_update_entropy(cpi, 0);
3077  }
3078}
3079