1 /*
2  * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include <math.h>
12 #include <stdio.h>
13 #include <limits.h>
14 
15 #include "./vpx_config.h"
16 #include "./vpx_scale_rtcd.h"
17 #include "vpx/internal/vpx_psnr.h"
18 #include "vpx_ports/vpx_timer.h"
19 
20 #include "vp9/common/vp9_alloccommon.h"
21 #include "vp9/common/vp9_filter.h"
22 #include "vp9/common/vp9_idct.h"
23 #if CONFIG_VP9_POSTPROC
24 #include "vp9/common/vp9_postproc.h"
25 #endif
26 #include "vp9/common/vp9_reconinter.h"
27 #include "vp9/common/vp9_systemdependent.h"
28 #include "vp9/common/vp9_tile_common.h"
29 
30 #include "vp9/encoder/vp9_aq_complexity.h"
31 #include "vp9/encoder/vp9_aq_cyclicrefresh.h"
32 #include "vp9/encoder/vp9_aq_variance.h"
33 #include "vp9/encoder/vp9_bitstream.h"
34 #include "vp9/encoder/vp9_encodeframe.h"
35 #include "vp9/encoder/vp9_encodemv.h"
36 #include "vp9/encoder/vp9_firstpass.h"
37 #include "vp9/encoder/vp9_mbgraph.h"
38 #include "vp9/encoder/vp9_onyx_int.h"
39 #include "vp9/encoder/vp9_picklpf.h"
40 #include "vp9/encoder/vp9_ratectrl.h"
41 #include "vp9/encoder/vp9_rdopt.h"
42 #include "vp9/encoder/vp9_segmentation.h"
43 #include "vp9/encoder/vp9_speed_features.h"
44 #include "vp9/encoder/vp9_temporal_filter.h"
45 #include "vp9/encoder/vp9_resize.h"
46 #include "vp9/encoder/vp9_svc_layercontext.h"
47 
48 void vp9_coef_tree_initialize();
49 
50 #define DEFAULT_INTERP_FILTER SWITCHABLE
51 
52 #define SHARP_FILTER_QTHRESH 0          /* Q threshold for 8-tap sharp filter */
53 
54 #define ALTREF_HIGH_PRECISION_MV 1      // Whether to use high precision mv
55                                          //  for altref computation.
56 #define HIGH_PRECISION_MV_QTHRESH 200   // Q threshold for high precision
57                                          // mv. Choose a very high value for
58                                          // now so that HIGH_PRECISION is always
59                                          // chosen.
60 
61 // Max rate target for 1080P and below encodes under normal circumstances
62 // (1920 * 1080 / (16 * 16)) * MAX_MB_RATE bits per MB
63 #define MAX_MB_RATE 250
64 #define MAXRATE_1080P 2025000
65 
66 #if CONFIG_INTERNAL_STATS
67 extern double vp9_calc_ssim(YV12_BUFFER_CONFIG *source,
68                             YV12_BUFFER_CONFIG *dest, int lumamask,
69                             double *weight);
70 
71 
72 extern double vp9_calc_ssimg(YV12_BUFFER_CONFIG *source,
73                              YV12_BUFFER_CONFIG *dest, double *ssim_y,
74                              double *ssim_u, double *ssim_v);
75 
76 
77 #endif
78 
79 // #define OUTPUT_YUV_REC
80 
81 #ifdef OUTPUT_YUV_SRC
82 FILE *yuv_file;
83 #endif
84 #ifdef OUTPUT_YUV_REC
85 FILE *yuv_rec_file;
86 #endif
87 
88 #if 0
89 FILE *framepsnr;
90 FILE *kf_list;
91 FILE *keyfile;
92 #endif
93 
94 void vp9_init_quantizer(VP9_COMP *cpi);
95 
Scale2Ratio(VPX_SCALING mode,int * hr,int * hs)96 static INLINE void Scale2Ratio(VPX_SCALING mode, int *hr, int *hs) {
97   switch (mode) {
98     case NORMAL:
99       *hr = 1;
100       *hs = 1;
101       break;
102     case FOURFIVE:
103       *hr = 4;
104       *hs = 5;
105       break;
106     case THREEFIVE:
107       *hr = 3;
108       *hs = 5;
109     break;
110     case ONETWO:
111       *hr = 1;
112       *hs = 2;
113     break;
114     default:
115       *hr = 1;
116       *hs = 1;
117        assert(0);
118       break;
119   }
120 }
121 
set_high_precision_mv(VP9_COMP * cpi,int allow_high_precision_mv)122 static void set_high_precision_mv(VP9_COMP *cpi, int allow_high_precision_mv) {
123   MACROBLOCK *const mb = &cpi->mb;
124   cpi->common.allow_high_precision_mv = allow_high_precision_mv;
125   if (cpi->common.allow_high_precision_mv) {
126     mb->mvcost = mb->nmvcost_hp;
127     mb->mvsadcost = mb->nmvsadcost_hp;
128   } else {
129     mb->mvcost = mb->nmvcost;
130     mb->mvsadcost = mb->nmvsadcost;
131   }
132 }
133 
setup_key_frame(VP9_COMP * cpi)134 static void setup_key_frame(VP9_COMP *cpi) {
135   vp9_setup_past_independence(&cpi->common);
136 
137   // All buffers are implicitly updated on key frames.
138   cpi->refresh_golden_frame = 1;
139   cpi->refresh_alt_ref_frame = 1;
140 }
141 
setup_inter_frame(VP9_COMMON * cm)142 static void setup_inter_frame(VP9_COMMON *cm) {
143   if (cm->error_resilient_mode || cm->intra_only)
144     vp9_setup_past_independence(cm);
145 
146   assert(cm->frame_context_idx < FRAME_CONTEXTS);
147   cm->fc = cm->frame_contexts[cm->frame_context_idx];
148 }
149 
vp9_initialize_enc()150 void vp9_initialize_enc() {
151   static int init_done = 0;
152 
153   if (!init_done) {
154     vp9_init_neighbors();
155     vp9_init_quant_tables();
156 
157     vp9_coef_tree_initialize();
158     vp9_tokenize_initialize();
159     vp9_init_me_luts();
160     vp9_rc_init_minq_luts();
161     vp9_entropy_mv_init();
162     vp9_entropy_mode_init();
163     init_done = 1;
164   }
165 }
166 
dealloc_compressor_data(VP9_COMP * cpi)167 static void dealloc_compressor_data(VP9_COMP *cpi) {
168   VP9_COMMON *const cm = &cpi->common;
169   int i;
170 
171   // Delete sementation map
172   vpx_free(cpi->segmentation_map);
173   cpi->segmentation_map = NULL;
174   vpx_free(cm->last_frame_seg_map);
175   cm->last_frame_seg_map = NULL;
176   vpx_free(cpi->coding_context.last_frame_seg_map_copy);
177   cpi->coding_context.last_frame_seg_map_copy = NULL;
178 
179   vpx_free(cpi->complexity_map);
180   cpi->complexity_map = NULL;
181 
182   vp9_cyclic_refresh_free(cpi->cyclic_refresh);
183   cpi->cyclic_refresh = NULL;
184 
185   vpx_free(cpi->active_map);
186   cpi->active_map = NULL;
187 
188   vp9_free_frame_buffers(cm);
189 
190   vp9_free_frame_buffer(&cpi->last_frame_uf);
191   vp9_free_frame_buffer(&cpi->scaled_source);
192   vp9_free_frame_buffer(&cpi->scaled_last_source);
193   vp9_free_frame_buffer(&cpi->alt_ref_buffer);
194   vp9_lookahead_destroy(cpi->lookahead);
195 
196   vpx_free(cpi->tok);
197   cpi->tok = 0;
198 
199   // Activity mask based per mb zbin adjustments
200   vpx_free(cpi->mb_activity_map);
201   cpi->mb_activity_map = 0;
202   vpx_free(cpi->mb_norm_activity_map);
203   cpi->mb_norm_activity_map = 0;
204 
205   for (i = 0; i < cpi->svc.number_spatial_layers; ++i) {
206     LAYER_CONTEXT *const lc = &cpi->svc.layer_context[i];
207     vpx_free(lc->rc_twopass_stats_in.buf);
208     lc->rc_twopass_stats_in.buf = NULL;
209     lc->rc_twopass_stats_in.sz = 0;
210   }
211 }
212 
213 // Computes a q delta (in "q index" terms) to get from a starting q value
214 // to a target q value
vp9_compute_qdelta(const VP9_COMP * cpi,double qstart,double qtarget)215 int vp9_compute_qdelta(const VP9_COMP *cpi, double qstart, double qtarget) {
216   const RATE_CONTROL *const rc = &cpi->rc;
217   int start_index = rc->worst_quality;
218   int target_index = rc->worst_quality;
219   int i;
220 
221   // Convert the average q value to an index.
222   for (i = rc->best_quality; i < rc->worst_quality; ++i) {
223     start_index = i;
224     if (vp9_convert_qindex_to_q(i) >= qstart)
225       break;
226   }
227 
228   // Convert the q target to an index
229   for (i = rc->best_quality; i < rc->worst_quality; ++i) {
230     target_index = i;
231     if (vp9_convert_qindex_to_q(i) >= qtarget)
232       break;
233   }
234 
235   return target_index - start_index;
236 }
237 
238 // Computes a q delta (in "q index" terms) to get from a starting q value
239 // to a value that should equate to the given rate ratio.
vp9_compute_qdelta_by_rate(VP9_COMP * cpi,int qindex,double rate_target_ratio)240 int vp9_compute_qdelta_by_rate(VP9_COMP *cpi, int qindex,
241                                double rate_target_ratio) {
242   const FRAME_TYPE frame_type = cpi->common.frame_type;
243   const RATE_CONTROL *const rc = &cpi->rc;
244   int target_index = rc->worst_quality;
245   int i;
246 
247   // Look up the current projected bits per block for the base index
248   const int base_bits_per_mb = vp9_rc_bits_per_mb(frame_type, qindex, 1.0);
249 
250   // Find the target bits per mb based on the base value and given ratio.
251   const int target_bits_per_mb = (int)(rate_target_ratio * base_bits_per_mb);
252 
253   // Convert the q target to an index
254   for (i = rc->best_quality; i < rc->worst_quality; ++i) {
255     target_index = i;
256     if (vp9_rc_bits_per_mb(frame_type, i, 1.0) <= target_bits_per_mb )
257       break;
258   }
259 
260   return target_index - qindex;
261 }
262 
configure_static_seg_features(VP9_COMP * cpi)263 static void configure_static_seg_features(VP9_COMP *cpi) {
264   VP9_COMMON *const cm = &cpi->common;
265   const RATE_CONTROL *const rc = &cpi->rc;
266   struct segmentation *const seg = &cm->seg;
267 
268   int high_q = (int)(rc->avg_q > 48.0);
269   int qi_delta;
270 
271   // Disable and clear down for KF
272   if (cm->frame_type == KEY_FRAME) {
273     // Clear down the global segmentation map
274     vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
275     seg->update_map = 0;
276     seg->update_data = 0;
277     cpi->static_mb_pct = 0;
278 
279     // Disable segmentation
280     vp9_disable_segmentation(seg);
281 
282     // Clear down the segment features.
283     vp9_clearall_segfeatures(seg);
284   } else if (cpi->refresh_alt_ref_frame) {
285     // If this is an alt ref frame
286     // Clear down the global segmentation map
287     vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
288     seg->update_map = 0;
289     seg->update_data = 0;
290     cpi->static_mb_pct = 0;
291 
292     // Disable segmentation and individual segment features by default
293     vp9_disable_segmentation(seg);
294     vp9_clearall_segfeatures(seg);
295 
296     // Scan frames from current to arf frame.
297     // This function re-enables segmentation if appropriate.
298     vp9_update_mbgraph_stats(cpi);
299 
300     // If segmentation was enabled set those features needed for the
301     // arf itself.
302     if (seg->enabled) {
303       seg->update_map = 1;
304       seg->update_data = 1;
305 
306       qi_delta = vp9_compute_qdelta(cpi, rc->avg_q, rc->avg_q * 0.875);
307       vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta - 2);
308       vp9_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
309 
310       vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
311       vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
312 
313       // Where relevant assume segment data is delta data
314       seg->abs_delta = SEGMENT_DELTADATA;
315     }
316   } else if (seg->enabled) {
317     // All other frames if segmentation has been enabled
318 
319     // First normal frame in a valid gf or alt ref group
320     if (rc->frames_since_golden == 0) {
321       // Set up segment features for normal frames in an arf group
322       if (rc->source_alt_ref_active) {
323         seg->update_map = 0;
324         seg->update_data = 1;
325         seg->abs_delta = SEGMENT_DELTADATA;
326 
327         qi_delta = vp9_compute_qdelta(cpi, rc->avg_q, rc->avg_q * 1.125);
328         vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta + 2);
329         vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
330 
331         vp9_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
332         vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
333 
334         // Segment coding disabled for compred testing
335         if (high_q || (cpi->static_mb_pct == 100)) {
336           vp9_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
337           vp9_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
338           vp9_enable_segfeature(seg, 1, SEG_LVL_SKIP);
339         }
340       } else {
341         // Disable segmentation and clear down features if alt ref
342         // is not active for this group
343 
344         vp9_disable_segmentation(seg);
345 
346         vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
347 
348         seg->update_map = 0;
349         seg->update_data = 0;
350 
351         vp9_clearall_segfeatures(seg);
352       }
353     } else if (rc->is_src_frame_alt_ref) {
354       // Special case where we are coding over the top of a previous
355       // alt ref frame.
356       // Segment coding disabled for compred testing
357 
358       // Enable ref frame features for segment 0 as well
359       vp9_enable_segfeature(seg, 0, SEG_LVL_REF_FRAME);
360       vp9_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
361 
362       // All mbs should use ALTREF_FRAME
363       vp9_clear_segdata(seg, 0, SEG_LVL_REF_FRAME);
364       vp9_set_segdata(seg, 0, SEG_LVL_REF_FRAME, ALTREF_FRAME);
365       vp9_clear_segdata(seg, 1, SEG_LVL_REF_FRAME);
366       vp9_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
367 
368       // Skip all MBs if high Q (0,0 mv and skip coeffs)
369       if (high_q) {
370         vp9_enable_segfeature(seg, 0, SEG_LVL_SKIP);
371         vp9_enable_segfeature(seg, 1, SEG_LVL_SKIP);
372       }
373       // Enable data update
374       seg->update_data = 1;
375     } else {
376       // All other frames.
377 
378       // No updates.. leave things as they are.
379       seg->update_map = 0;
380       seg->update_data = 0;
381     }
382   }
383 }
384 
385 // DEBUG: Print out the segment id of each MB in the current frame.
print_seg_map(VP9_COMP * cpi)386 static void print_seg_map(VP9_COMP *cpi) {
387   VP9_COMMON *cm = &cpi->common;
388   int row, col;
389   int map_index = 0;
390   FILE *statsfile = fopen("segmap.stt", "a");
391 
392   fprintf(statsfile, "%10d\n", cm->current_video_frame);
393 
394   for (row = 0; row < cpi->common.mi_rows; row++) {
395     for (col = 0; col < cpi->common.mi_cols; col++) {
396       fprintf(statsfile, "%10d", cpi->segmentation_map[map_index]);
397       map_index++;
398     }
399     fprintf(statsfile, "\n");
400   }
401   fprintf(statsfile, "\n");
402 
403   fclose(statsfile);
404 }
405 
update_reference_segmentation_map(VP9_COMP * cpi)406 static void update_reference_segmentation_map(VP9_COMP *cpi) {
407   VP9_COMMON *const cm = &cpi->common;
408   MODE_INFO **mi_8x8_ptr = cm->mi_grid_visible;
409   uint8_t *cache_ptr = cm->last_frame_seg_map;
410   int row, col;
411 
412   for (row = 0; row < cm->mi_rows; row++) {
413     MODE_INFO **mi_8x8 = mi_8x8_ptr;
414     uint8_t *cache = cache_ptr;
415     for (col = 0; col < cm->mi_cols; col++, mi_8x8++, cache++)
416       cache[0] = mi_8x8[0]->mbmi.segment_id;
417     mi_8x8_ptr += cm->mi_stride;
418     cache_ptr += cm->mi_cols;
419   }
420 }
is_slowest_mode(int mode)421 static int is_slowest_mode(int mode) {
422   return (mode == MODE_SECONDPASS_BEST || mode == MODE_BESTQUALITY);
423 }
424 
set_rd_speed_thresholds(VP9_COMP * cpi)425 static void set_rd_speed_thresholds(VP9_COMP *cpi) {
426   int i;
427 
428   // Set baseline threshold values
429   for (i = 0; i < MAX_MODES; ++i)
430   cpi->rd_thresh_mult[i] = is_slowest_mode(cpi->oxcf.mode) ? -500 : 0;
431 
432   cpi->rd_thresh_mult[THR_NEARESTMV] = 0;
433   cpi->rd_thresh_mult[THR_NEARESTG] = 0;
434   cpi->rd_thresh_mult[THR_NEARESTA] = 0;
435 
436   cpi->rd_thresh_mult[THR_DC] += 1000;
437 
438   cpi->rd_thresh_mult[THR_NEWMV] += 1000;
439   cpi->rd_thresh_mult[THR_NEWA] += 1000;
440   cpi->rd_thresh_mult[THR_NEWG] += 1000;
441 
442   cpi->rd_thresh_mult[THR_NEARMV] += 1000;
443   cpi->rd_thresh_mult[THR_NEARA] += 1000;
444   cpi->rd_thresh_mult[THR_COMP_NEARESTLA] += 1000;
445   cpi->rd_thresh_mult[THR_COMP_NEARESTGA] += 1000;
446 
447   cpi->rd_thresh_mult[THR_TM] += 1000;
448 
449   cpi->rd_thresh_mult[THR_COMP_NEARLA] += 1500;
450   cpi->rd_thresh_mult[THR_COMP_NEWLA] += 2000;
451   cpi->rd_thresh_mult[THR_NEARG] += 1000;
452   cpi->rd_thresh_mult[THR_COMP_NEARGA] += 1500;
453   cpi->rd_thresh_mult[THR_COMP_NEWGA] += 2000;
454 
455   cpi->rd_thresh_mult[THR_ZEROMV] += 2000;
456   cpi->rd_thresh_mult[THR_ZEROG] += 2000;
457   cpi->rd_thresh_mult[THR_ZEROA] += 2000;
458   cpi->rd_thresh_mult[THR_COMP_ZEROLA] += 2500;
459   cpi->rd_thresh_mult[THR_COMP_ZEROGA] += 2500;
460 
461   cpi->rd_thresh_mult[THR_H_PRED] += 2000;
462   cpi->rd_thresh_mult[THR_V_PRED] += 2000;
463   cpi->rd_thresh_mult[THR_D45_PRED ] += 2500;
464   cpi->rd_thresh_mult[THR_D135_PRED] += 2500;
465   cpi->rd_thresh_mult[THR_D117_PRED] += 2500;
466   cpi->rd_thresh_mult[THR_D153_PRED] += 2500;
467   cpi->rd_thresh_mult[THR_D207_PRED] += 2500;
468   cpi->rd_thresh_mult[THR_D63_PRED] += 2500;
469 
470   /* disable frame modes if flags not set */
471   if (!(cpi->ref_frame_flags & VP9_LAST_FLAG)) {
472     cpi->rd_thresh_mult[THR_NEWMV    ] = INT_MAX;
473     cpi->rd_thresh_mult[THR_NEARESTMV] = INT_MAX;
474     cpi->rd_thresh_mult[THR_ZEROMV   ] = INT_MAX;
475     cpi->rd_thresh_mult[THR_NEARMV   ] = INT_MAX;
476   }
477   if (!(cpi->ref_frame_flags & VP9_GOLD_FLAG)) {
478     cpi->rd_thresh_mult[THR_NEARESTG ] = INT_MAX;
479     cpi->rd_thresh_mult[THR_ZEROG    ] = INT_MAX;
480     cpi->rd_thresh_mult[THR_NEARG    ] = INT_MAX;
481     cpi->rd_thresh_mult[THR_NEWG     ] = INT_MAX;
482   }
483   if (!(cpi->ref_frame_flags & VP9_ALT_FLAG)) {
484     cpi->rd_thresh_mult[THR_NEARESTA ] = INT_MAX;
485     cpi->rd_thresh_mult[THR_ZEROA    ] = INT_MAX;
486     cpi->rd_thresh_mult[THR_NEARA    ] = INT_MAX;
487     cpi->rd_thresh_mult[THR_NEWA     ] = INT_MAX;
488   }
489 
490   if ((cpi->ref_frame_flags & (VP9_LAST_FLAG | VP9_ALT_FLAG)) !=
491       (VP9_LAST_FLAG | VP9_ALT_FLAG)) {
492     cpi->rd_thresh_mult[THR_COMP_ZEROLA   ] = INT_MAX;
493     cpi->rd_thresh_mult[THR_COMP_NEARESTLA] = INT_MAX;
494     cpi->rd_thresh_mult[THR_COMP_NEARLA   ] = INT_MAX;
495     cpi->rd_thresh_mult[THR_COMP_NEWLA    ] = INT_MAX;
496   }
497   if ((cpi->ref_frame_flags & (VP9_GOLD_FLAG | VP9_ALT_FLAG)) !=
498       (VP9_GOLD_FLAG | VP9_ALT_FLAG)) {
499     cpi->rd_thresh_mult[THR_COMP_ZEROGA   ] = INT_MAX;
500     cpi->rd_thresh_mult[THR_COMP_NEARESTGA] = INT_MAX;
501     cpi->rd_thresh_mult[THR_COMP_NEARGA   ] = INT_MAX;
502     cpi->rd_thresh_mult[THR_COMP_NEWGA    ] = INT_MAX;
503   }
504 }
505 
set_rd_speed_thresholds_sub8x8(VP9_COMP * cpi)506 static void set_rd_speed_thresholds_sub8x8(VP9_COMP *cpi) {
507   const SPEED_FEATURES *const sf = &cpi->sf;
508   int i;
509 
510   for (i = 0; i < MAX_REFS; ++i)
511     cpi->rd_thresh_mult_sub8x8[i] = is_slowest_mode(cpi->oxcf.mode)  ? -500 : 0;
512 
513   cpi->rd_thresh_mult_sub8x8[THR_LAST] += 2500;
514   cpi->rd_thresh_mult_sub8x8[THR_GOLD] += 2500;
515   cpi->rd_thresh_mult_sub8x8[THR_ALTR] += 2500;
516   cpi->rd_thresh_mult_sub8x8[THR_INTRA] += 2500;
517   cpi->rd_thresh_mult_sub8x8[THR_COMP_LA] += 4500;
518   cpi->rd_thresh_mult_sub8x8[THR_COMP_GA] += 4500;
519 
520   // Check for masked out split cases.
521   for (i = 0; i < MAX_REFS; i++)
522     if (sf->disable_split_mask & (1 << i))
523       cpi->rd_thresh_mult_sub8x8[i] = INT_MAX;
524 
525   // disable mode test if frame flag is not set
526   if (!(cpi->ref_frame_flags & VP9_LAST_FLAG))
527     cpi->rd_thresh_mult_sub8x8[THR_LAST] = INT_MAX;
528   if (!(cpi->ref_frame_flags & VP9_GOLD_FLAG))
529     cpi->rd_thresh_mult_sub8x8[THR_GOLD] = INT_MAX;
530   if (!(cpi->ref_frame_flags & VP9_ALT_FLAG))
531     cpi->rd_thresh_mult_sub8x8[THR_ALTR] = INT_MAX;
532   if ((cpi->ref_frame_flags & (VP9_LAST_FLAG | VP9_ALT_FLAG)) !=
533       (VP9_LAST_FLAG | VP9_ALT_FLAG))
534     cpi->rd_thresh_mult_sub8x8[THR_COMP_LA] = INT_MAX;
535   if ((cpi->ref_frame_flags & (VP9_GOLD_FLAG | VP9_ALT_FLAG)) !=
536       (VP9_GOLD_FLAG | VP9_ALT_FLAG))
537     cpi->rd_thresh_mult_sub8x8[THR_COMP_GA] = INT_MAX;
538 }
539 
set_speed_features(VP9_COMP * cpi)540 static void set_speed_features(VP9_COMP *cpi) {
541 #if CONFIG_INTERNAL_STATS
542   int i;
543   for (i = 0; i < MAX_MODES; ++i)
544     cpi->mode_chosen_counts[i] = 0;
545 #endif
546 
547   vp9_set_speed_features(cpi);
548 
549   // Set rd thresholds based on mode and speed setting
550   set_rd_speed_thresholds(cpi);
551   set_rd_speed_thresholds_sub8x8(cpi);
552 
553   cpi->mb.fwd_txm4x4 = vp9_fdct4x4;
554   if (cpi->oxcf.lossless || cpi->mb.e_mbd.lossless) {
555     cpi->mb.fwd_txm4x4 = vp9_fwht4x4;
556   }
557 }
558 
alloc_raw_frame_buffers(VP9_COMP * cpi)559 static void alloc_raw_frame_buffers(VP9_COMP *cpi) {
560   VP9_COMMON *cm = &cpi->common;
561   const VP9_CONFIG *oxcf = &cpi->oxcf;
562 
563   cpi->lookahead = vp9_lookahead_init(oxcf->width, oxcf->height,
564                                       cm->subsampling_x, cm->subsampling_y,
565                                       oxcf->lag_in_frames);
566   if (!cpi->lookahead)
567     vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
568                        "Failed to allocate lag buffers");
569 
570   if (vp9_realloc_frame_buffer(&cpi->alt_ref_buffer,
571                                oxcf->width, oxcf->height,
572                                cm->subsampling_x, cm->subsampling_y,
573                                VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL))
574     vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
575                        "Failed to allocate altref buffer");
576 }
577 
vp9_alloc_compressor_data(VP9_COMP * cpi)578 void vp9_alloc_compressor_data(VP9_COMP *cpi) {
579   VP9_COMMON *cm = &cpi->common;
580 
581   if (vp9_alloc_frame_buffers(cm, cm->width, cm->height))
582     vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
583                        "Failed to allocate frame buffers");
584 
585   if (vp9_alloc_frame_buffer(&cpi->last_frame_uf,
586                              cm->width, cm->height,
587                              cm->subsampling_x, cm->subsampling_y,
588                              VP9_ENC_BORDER_IN_PIXELS))
589     vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
590                        "Failed to allocate last frame buffer");
591 
592   if (vp9_alloc_frame_buffer(&cpi->scaled_source,
593                              cm->width, cm->height,
594                              cm->subsampling_x, cm->subsampling_y,
595                              VP9_ENC_BORDER_IN_PIXELS))
596     vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
597                        "Failed to allocate scaled source buffer");
598 
599   if (vp9_alloc_frame_buffer(&cpi->scaled_last_source,
600                              cm->width, cm->height,
601                              cm->subsampling_x, cm->subsampling_y,
602                              VP9_ENC_BORDER_IN_PIXELS))
603     vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
604                        "Failed to allocate scaled last source buffer");
605 
606   vpx_free(cpi->tok);
607 
608   {
609     unsigned int tokens = get_token_alloc(cm->mb_rows, cm->mb_cols);
610 
611     CHECK_MEM_ERROR(cm, cpi->tok, vpx_calloc(tokens, sizeof(*cpi->tok)));
612   }
613 
614   vpx_free(cpi->mb_activity_map);
615   CHECK_MEM_ERROR(cm, cpi->mb_activity_map,
616                   vpx_calloc(sizeof(unsigned int),
617                              cm->mb_rows * cm->mb_cols));
618 
619   vpx_free(cpi->mb_norm_activity_map);
620   CHECK_MEM_ERROR(cm, cpi->mb_norm_activity_map,
621                   vpx_calloc(sizeof(unsigned int),
622                              cm->mb_rows * cm->mb_cols));
623 }
624 
625 
update_frame_size(VP9_COMP * cpi)626 static void update_frame_size(VP9_COMP *cpi) {
627   VP9_COMMON *const cm = &cpi->common;
628   MACROBLOCKD *const xd = &cpi->mb.e_mbd;
629 
630   vp9_update_frame_size(cm);
631 
632   // Update size of buffers local to this frame
633   if (vp9_realloc_frame_buffer(&cpi->last_frame_uf,
634                                cm->width, cm->height,
635                                cm->subsampling_x, cm->subsampling_y,
636                                VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL))
637     vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
638                        "Failed to reallocate last frame buffer");
639 
640   if (vp9_realloc_frame_buffer(&cpi->scaled_source,
641                                cm->width, cm->height,
642                                cm->subsampling_x, cm->subsampling_y,
643                                VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL))
644     vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
645                        "Failed to reallocate scaled source buffer");
646 
647   if (vp9_realloc_frame_buffer(&cpi->scaled_last_source,
648                                cm->width, cm->height,
649                                cm->subsampling_x, cm->subsampling_y,
650                                VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL))
651     vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
652                        "Failed to reallocate scaled last source buffer");
653 
654   {
655     int y_stride = cpi->scaled_source.y_stride;
656 
657     if (cpi->sf.search_method == NSTEP) {
658       vp9_init3smotion_compensation(&cpi->mb, y_stride);
659     } else if (cpi->sf.search_method == DIAMOND) {
660       vp9_init_dsmotion_compensation(&cpi->mb, y_stride);
661     }
662   }
663 
664   init_macroblockd(cm, xd);
665 }
666 
667 // Table that converts 0-63 Q range values passed in outside to the Qindex
668 // range used internally.
669 const int q_trans[] = {
670   0,    4,   8,  12,  16,  20,  24,  28,
671   32,   36,  40,  44,  48,  52,  56,  60,
672   64,   68,  72,  76,  80,  84,  88,  92,
673   96,  100, 104, 108, 112, 116, 120, 124,
674   128, 132, 136, 140, 144, 148, 152, 156,
675   160, 164, 168, 172, 176, 180, 184, 188,
676   192, 196, 200, 204, 208, 212, 216, 220,
677   224, 228, 232, 236, 240, 244, 249, 255,
678 };
679 
vp9_reverse_trans(int x)680 int vp9_reverse_trans(int x) {
681   int i;
682 
683   for (i = 0; i < 64; i++)
684     if (q_trans[i] >= x)
685       return i;
686 
687   return 63;
688 };
689 
vp9_new_framerate(VP9_COMP * cpi,double framerate)690 void vp9_new_framerate(VP9_COMP *cpi, double framerate) {
691   VP9_COMMON *const cm = &cpi->common;
692   RATE_CONTROL *const rc = &cpi->rc;
693   VP9_CONFIG *const oxcf = &cpi->oxcf;
694   int vbr_max_bits;
695 
696   oxcf->framerate = framerate < 0.1 ? 30 : framerate;
697   cpi->output_framerate = cpi->oxcf.framerate;
698   rc->av_per_frame_bandwidth = (int)(oxcf->target_bandwidth /
699                                      cpi->output_framerate);
700   rc->min_frame_bandwidth = (int)(rc->av_per_frame_bandwidth *
701                                   oxcf->two_pass_vbrmin_section / 100);
702 
703   rc->min_frame_bandwidth = MAX(rc->min_frame_bandwidth, FRAME_OVERHEAD_BITS);
704 
705   // A maximum bitrate for a frame is defined.
706   // The baseline for this aligns with HW implementations that
707   // can support decode of 1080P content up to a bitrate of MAX_MB_RATE bits
708   // per 16x16 MB (averaged over a frame). However this limit is extended if
709   // a very high rate is given on the command line or the the rate cannnot
710   // be acheived because of a user specificed max q (e.g. when the user
711   // specifies lossless encode.
712   //
713   vbr_max_bits = (int)(((int64_t)rc->av_per_frame_bandwidth *
714       oxcf->two_pass_vbrmax_section) / 100);
715   rc->max_frame_bandwidth = MAX(MAX((cm->MBs * MAX_MB_RATE), MAXRATE_1080P),
716                                 vbr_max_bits);
717 
718   // Set Maximum gf/arf interval
719   rc->max_gf_interval = 16;
720 
721   // Extended interval for genuinely static scenes
722   rc->static_scene_max_gf_interval = cpi->key_frame_frequency >> 1;
723 
724   // Special conditions when alt ref frame enabled in lagged compress mode
725   if (oxcf->play_alternate && oxcf->lag_in_frames) {
726     if (rc->max_gf_interval > oxcf->lag_in_frames - 1)
727       rc->max_gf_interval = oxcf->lag_in_frames - 1;
728 
729     if (rc->static_scene_max_gf_interval > oxcf->lag_in_frames - 1)
730       rc->static_scene_max_gf_interval = oxcf->lag_in_frames - 1;
731   }
732 
733   if (rc->max_gf_interval > rc->static_scene_max_gf_interval)
734     rc->max_gf_interval = rc->static_scene_max_gf_interval;
735 }
736 
vp9_rescale(int64_t val,int64_t num,int denom)737 int64_t vp9_rescale(int64_t val, int64_t num, int denom) {
738   int64_t llnum = num;
739   int64_t llden = denom;
740   int64_t llval = val;
741 
742   return (llval * llnum / llden);
743 }
744 
set_tile_limits(VP9_COMP * cpi)745 static void set_tile_limits(VP9_COMP *cpi) {
746   VP9_COMMON *const cm = &cpi->common;
747 
748   int min_log2_tile_cols, max_log2_tile_cols;
749   vp9_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
750 
751   cm->log2_tile_cols = clamp(cpi->oxcf.tile_columns,
752                              min_log2_tile_cols, max_log2_tile_cols);
753   cm->log2_tile_rows = cpi->oxcf.tile_rows;
754 }
755 
init_rate_control(const VP9_CONFIG * oxcf,int pass,RATE_CONTROL * rc)756 static void init_rate_control(const VP9_CONFIG *oxcf, int pass,
757                               RATE_CONTROL *rc) {
758   if (pass == 0 && oxcf->end_usage == USAGE_STREAM_FROM_SERVER) {
759     rc->avg_frame_qindex[0] = oxcf->worst_allowed_q;
760     rc->avg_frame_qindex[1] = oxcf->worst_allowed_q;
761     rc->avg_frame_qindex[2] = oxcf->worst_allowed_q;
762   } else {
763     rc->avg_frame_qindex[0] = (oxcf->worst_allowed_q +
764                                    oxcf->best_allowed_q) / 2;
765     rc->avg_frame_qindex[1] = (oxcf->worst_allowed_q +
766                                    oxcf->best_allowed_q) / 2;
767     rc->avg_frame_qindex[2] = (oxcf->worst_allowed_q +
768                                    oxcf->best_allowed_q) / 2;
769   }
770 
771   rc->last_q[0] = oxcf->best_allowed_q;
772   rc->last_q[1] = oxcf->best_allowed_q;
773   rc->last_q[2] = oxcf->best_allowed_q;
774 
775   rc->buffer_level =    oxcf->starting_buffer_level;
776   rc->bits_off_target = oxcf->starting_buffer_level;
777 
778   rc->rolling_target_bits      = rc->av_per_frame_bandwidth;
779   rc->rolling_actual_bits      = rc->av_per_frame_bandwidth;
780   rc->long_rolling_target_bits = rc->av_per_frame_bandwidth;
781   rc->long_rolling_actual_bits = rc->av_per_frame_bandwidth;
782 
783   rc->total_actual_bits = 0;
784   rc->total_target_vs_actual = 0;
785 }
786 
init_config(struct VP9_COMP * cpi,VP9_CONFIG * oxcf)787 static void init_config(struct VP9_COMP *cpi, VP9_CONFIG *oxcf) {
788   VP9_COMMON *const cm = &cpi->common;
789   int i;
790 
791   cpi->oxcf = *oxcf;
792 
793   cm->version = oxcf->version;
794 
795   cm->width = oxcf->width;
796   cm->height = oxcf->height;
797   cm->subsampling_x = 0;
798   cm->subsampling_y = 0;
799   vp9_alloc_compressor_data(cpi);
800 
801   // Spatial scalability.
802   cpi->svc.number_spatial_layers = oxcf->ss_number_layers;
803   // Temporal scalability.
804   cpi->svc.number_temporal_layers = oxcf->ts_number_layers;
805 
806   if ((cpi->svc.number_temporal_layers > 1 &&
807       cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) ||
808       (cpi->svc.number_spatial_layers > 1 &&
809       cpi->oxcf.mode == MODE_SECONDPASS_BEST)) {
810     vp9_init_layer_context(cpi);
811   }
812 
813   // change includes all joint functionality
814   vp9_change_config(cpi, oxcf);
815 
816   cpi->static_mb_pct = 0;
817 
818   cpi->lst_fb_idx = 0;
819   cpi->gld_fb_idx = 1;
820   cpi->alt_fb_idx = 2;
821 
822   set_tile_limits(cpi);
823 
824   cpi->fixed_divide[0] = 0;
825   for (i = 1; i < 512; i++)
826     cpi->fixed_divide[i] = 0x80000 / i;
827 }
828 
vp9_change_config(struct VP9_COMP * cpi,const VP9_CONFIG * oxcf)829 void vp9_change_config(struct VP9_COMP *cpi, const VP9_CONFIG *oxcf) {
830   VP9_COMMON *const cm = &cpi->common;
831   RATE_CONTROL *const rc = &cpi->rc;
832 
833   if (cm->version != oxcf->version)
834     cm->version = oxcf->version;
835 
836   cpi->oxcf = *oxcf;
837 
838   if (cpi->oxcf.cpu_used == -6)
839     cpi->oxcf.play_alternate = 0;
840 
841   switch (cpi->oxcf.mode) {
842       // Real time and one pass deprecated in test code base
843     case MODE_GOODQUALITY:
844       cpi->pass = 0;
845       cpi->oxcf.cpu_used = clamp(cpi->oxcf.cpu_used, -5, 5);
846       break;
847 
848     case MODE_BESTQUALITY:
849       cpi->pass = 0;
850       break;
851 
852     case MODE_FIRSTPASS:
853       cpi->pass = 1;
854       break;
855 
856     case MODE_SECONDPASS:
857       cpi->pass = 2;
858       cpi->oxcf.cpu_used = clamp(cpi->oxcf.cpu_used, -5, 5);
859       break;
860 
861     case MODE_SECONDPASS_BEST:
862       cpi->pass = 2;
863       break;
864 
865     case MODE_REALTIME:
866       cpi->pass = 0;
867       break;
868   }
869 
870   cpi->oxcf.worst_allowed_q = q_trans[oxcf->worst_allowed_q];
871   cpi->oxcf.best_allowed_q = q_trans[oxcf->best_allowed_q];
872   cpi->oxcf.cq_level = q_trans[cpi->oxcf.cq_level];
873 
874   cpi->oxcf.lossless = oxcf->lossless;
875   if (cpi->oxcf.lossless) {
876     // In lossless mode, make sure right quantizer range and correct transform
877     // is set.
878     cpi->oxcf.worst_allowed_q = 0;
879     cpi->oxcf.best_allowed_q = 0;
880     cpi->mb.e_mbd.itxm_add = vp9_iwht4x4_add;
881   } else {
882     cpi->mb.e_mbd.itxm_add = vp9_idct4x4_add;
883   }
884   rc->baseline_gf_interval = DEFAULT_GF_INTERVAL;
885   cpi->ref_frame_flags = VP9_ALT_FLAG | VP9_GOLD_FLAG | VP9_LAST_FLAG;
886 
887   cpi->refresh_golden_frame = 0;
888   cpi->refresh_last_frame = 1;
889   cm->refresh_frame_context = 1;
890   cm->reset_frame_context = 0;
891 
892   vp9_reset_segment_features(&cm->seg);
893   set_high_precision_mv(cpi, 0);
894 
895   {
896     int i;
897 
898     for (i = 0; i < MAX_SEGMENTS; i++)
899       cpi->segment_encode_breakout[i] = cpi->oxcf.encode_breakout;
900   }
901   cpi->encode_breakout = cpi->oxcf.encode_breakout;
902 
903   // local file playback mode == really big buffer
904   if (cpi->oxcf.end_usage == USAGE_LOCAL_FILE_PLAYBACK) {
905     cpi->oxcf.starting_buffer_level   = 60000;
906     cpi->oxcf.optimal_buffer_level    = 60000;
907     cpi->oxcf.maximum_buffer_size     = 240000;
908   }
909 
910   // Convert target bandwidth from Kbit/s to Bit/s
911   cpi->oxcf.target_bandwidth       *= 1000;
912 
913   cpi->oxcf.starting_buffer_level =
914       vp9_rescale(cpi->oxcf.starting_buffer_level,
915                   cpi->oxcf.target_bandwidth, 1000);
916 
917   // Set or reset optimal and maximum buffer levels.
918   if (cpi->oxcf.optimal_buffer_level == 0)
919     cpi->oxcf.optimal_buffer_level = cpi->oxcf.target_bandwidth / 8;
920   else
921     cpi->oxcf.optimal_buffer_level =
922         vp9_rescale(cpi->oxcf.optimal_buffer_level,
923                     cpi->oxcf.target_bandwidth, 1000);
924 
925   if (cpi->oxcf.maximum_buffer_size == 0)
926     cpi->oxcf.maximum_buffer_size = cpi->oxcf.target_bandwidth / 8;
927   else
928     cpi->oxcf.maximum_buffer_size =
929         vp9_rescale(cpi->oxcf.maximum_buffer_size,
930                     cpi->oxcf.target_bandwidth, 1000);
931   // Under a configuration change, where maximum_buffer_size may change,
932   // keep buffer level clipped to the maximum allowed buffer size.
933   rc->bits_off_target = MIN(rc->bits_off_target, cpi->oxcf.maximum_buffer_size);
934   rc->buffer_level = MIN(rc->buffer_level, cpi->oxcf.maximum_buffer_size);
935 
936   // Set up frame rate and related parameters rate control values.
937   vp9_new_framerate(cpi, cpi->oxcf.framerate);
938 
939   // Set absolute upper and lower quality limits
940   rc->worst_quality = cpi->oxcf.worst_allowed_q;
941   rc->best_quality = cpi->oxcf.best_allowed_q;
942 
943   // active values should only be modified if out of new range
944 
945   cpi->cq_target_quality = cpi->oxcf.cq_level;
946 
947   cm->interp_filter = DEFAULT_INTERP_FILTER;
948 
949   cm->display_width = cpi->oxcf.width;
950   cm->display_height = cpi->oxcf.height;
951 
952   // VP8 sharpness level mapping 0-7 (vs 0-10 in general VPx dialogs)
953   cpi->oxcf.sharpness = MIN(7, cpi->oxcf.sharpness);
954 
955   cpi->common.lf.sharpness_level = cpi->oxcf.sharpness;
956 
957   if (cpi->initial_width) {
958     // Increasing the size of the frame beyond the first seen frame, or some
959     // otherwise signaled maximum size, is not supported.
960     // TODO(jkoleszar): exit gracefully.
961     assert(cm->width <= cpi->initial_width);
962     assert(cm->height <= cpi->initial_height);
963   }
964   update_frame_size(cpi);
965 
966   if ((cpi->svc.number_temporal_layers > 1 &&
967       cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) ||
968       (cpi->svc.number_spatial_layers > 1 && cpi->pass == 2)) {
969     vp9_update_layer_context_change_config(cpi,
970                                            (int)cpi->oxcf.target_bandwidth);
971   }
972 
973   cpi->speed = abs(cpi->oxcf.cpu_used);
974 
975   // Limit on lag buffers as these are not currently dynamically allocated.
976   if (cpi->oxcf.lag_in_frames > MAX_LAG_BUFFERS)
977     cpi->oxcf.lag_in_frames = MAX_LAG_BUFFERS;
978 
979 #if CONFIG_MULTIPLE_ARF
980   vp9_zero(cpi->alt_ref_source);
981 #else
982   cpi->alt_ref_source = NULL;
983 #endif
984   rc->is_src_frame_alt_ref = 0;
985 
986 #if 0
987   // Experimental RD Code
988   cpi->frame_distortion = 0;
989   cpi->last_frame_distortion = 0;
990 #endif
991 
992   set_tile_limits(cpi);
993 
994   cpi->ext_refresh_frame_flags_pending = 0;
995   cpi->ext_refresh_frame_context_pending = 0;
996 }
997 
998 #define M_LOG2_E 0.693147180559945309417
999 #define log2f(x) (log (x) / (float) M_LOG2_E)
1000 
cal_nmvjointsadcost(int * mvjointsadcost)1001 static void cal_nmvjointsadcost(int *mvjointsadcost) {
1002   mvjointsadcost[0] = 600;
1003   mvjointsadcost[1] = 300;
1004   mvjointsadcost[2] = 300;
1005   mvjointsadcost[3] = 300;
1006 }
1007 
cal_nmvsadcosts(int * mvsadcost[2])1008 static void cal_nmvsadcosts(int *mvsadcost[2]) {
1009   int i = 1;
1010 
1011   mvsadcost[0][0] = 0;
1012   mvsadcost[1][0] = 0;
1013 
1014   do {
1015     double z = 256 * (2 * (log2f(8 * i) + .6));
1016     mvsadcost[0][i] = (int)z;
1017     mvsadcost[1][i] = (int)z;
1018     mvsadcost[0][-i] = (int)z;
1019     mvsadcost[1][-i] = (int)z;
1020   } while (++i <= MV_MAX);
1021 }
1022 
cal_nmvsadcosts_hp(int * mvsadcost[2])1023 static void cal_nmvsadcosts_hp(int *mvsadcost[2]) {
1024   int i = 1;
1025 
1026   mvsadcost[0][0] = 0;
1027   mvsadcost[1][0] = 0;
1028 
1029   do {
1030     double z = 256 * (2 * (log2f(8 * i) + .6));
1031     mvsadcost[0][i] = (int)z;
1032     mvsadcost[1][i] = (int)z;
1033     mvsadcost[0][-i] = (int)z;
1034     mvsadcost[1][-i] = (int)z;
1035   } while (++i <= MV_MAX);
1036 }
1037 
alloc_mode_context(VP9_COMMON * cm,int num_4x4_blk,PICK_MODE_CONTEXT * ctx)1038 static void alloc_mode_context(VP9_COMMON *cm, int num_4x4_blk,
1039                                PICK_MODE_CONTEXT *ctx) {
1040   int num_pix = num_4x4_blk << 4;
1041   int i, k;
1042   ctx->num_4x4_blk = num_4x4_blk;
1043 
1044   CHECK_MEM_ERROR(cm, ctx->zcoeff_blk,
1045                   vpx_calloc(num_4x4_blk, sizeof(uint8_t)));
1046   for (i = 0; i < MAX_MB_PLANE; ++i) {
1047     for (k = 0; k < 3; ++k) {
1048       CHECK_MEM_ERROR(cm, ctx->coeff[i][k],
1049                       vpx_memalign(16, num_pix * sizeof(int16_t)));
1050       CHECK_MEM_ERROR(cm, ctx->qcoeff[i][k],
1051                       vpx_memalign(16, num_pix * sizeof(int16_t)));
1052       CHECK_MEM_ERROR(cm, ctx->dqcoeff[i][k],
1053                       vpx_memalign(16, num_pix * sizeof(int16_t)));
1054       CHECK_MEM_ERROR(cm, ctx->eobs[i][k],
1055                       vpx_memalign(16, num_pix * sizeof(uint16_t)));
1056       ctx->coeff_pbuf[i][k]   = ctx->coeff[i][k];
1057       ctx->qcoeff_pbuf[i][k]  = ctx->qcoeff[i][k];
1058       ctx->dqcoeff_pbuf[i][k] = ctx->dqcoeff[i][k];
1059       ctx->eobs_pbuf[i][k]    = ctx->eobs[i][k];
1060     }
1061   }
1062 }
1063 
free_mode_context(PICK_MODE_CONTEXT * ctx)1064 static void free_mode_context(PICK_MODE_CONTEXT *ctx) {
1065   int i, k;
1066   vpx_free(ctx->zcoeff_blk);
1067   ctx->zcoeff_blk = 0;
1068   for (i = 0; i < MAX_MB_PLANE; ++i) {
1069     for (k = 0; k < 3; ++k) {
1070       vpx_free(ctx->coeff[i][k]);
1071       ctx->coeff[i][k] = 0;
1072       vpx_free(ctx->qcoeff[i][k]);
1073       ctx->qcoeff[i][k] = 0;
1074       vpx_free(ctx->dqcoeff[i][k]);
1075       ctx->dqcoeff[i][k] = 0;
1076       vpx_free(ctx->eobs[i][k]);
1077       ctx->eobs[i][k] = 0;
1078     }
1079   }
1080 }
1081 
init_pick_mode_context(VP9_COMP * cpi)1082 static void init_pick_mode_context(VP9_COMP *cpi) {
1083   int i;
1084   VP9_COMMON *const cm = &cpi->common;
1085   MACROBLOCK *const x  = &cpi->mb;
1086 
1087   for (i = 0; i < BLOCK_SIZES; ++i) {
1088     const int num_4x4_w = num_4x4_blocks_wide_lookup[i];
1089     const int num_4x4_h = num_4x4_blocks_high_lookup[i];
1090     const int num_4x4_blk = MAX(4, num_4x4_w * num_4x4_h);
1091     if (i < BLOCK_16X16) {
1092       for (x->sb_index = 0; x->sb_index < 4; ++x->sb_index) {
1093         for (x->mb_index = 0; x->mb_index < 4; ++x->mb_index) {
1094           for (x->b_index = 0; x->b_index < 16 / num_4x4_blk; ++x->b_index) {
1095             PICK_MODE_CONTEXT *ctx = get_block_context(x, i);
1096             alloc_mode_context(cm, num_4x4_blk, ctx);
1097           }
1098         }
1099       }
1100     } else if (i < BLOCK_32X32) {
1101       for (x->sb_index = 0; x->sb_index < 4; ++x->sb_index) {
1102         for (x->mb_index = 0; x->mb_index < 64 / num_4x4_blk; ++x->mb_index) {
1103           PICK_MODE_CONTEXT *ctx = get_block_context(x, i);
1104           ctx->num_4x4_blk = num_4x4_blk;
1105           alloc_mode_context(cm, num_4x4_blk, ctx);
1106         }
1107       }
1108     } else if (i < BLOCK_64X64) {
1109       for (x->sb_index = 0; x->sb_index < 256 / num_4x4_blk; ++x->sb_index) {
1110         PICK_MODE_CONTEXT *ctx = get_block_context(x, i);
1111         ctx->num_4x4_blk = num_4x4_blk;
1112         alloc_mode_context(cm, num_4x4_blk, ctx);
1113       }
1114     } else {
1115       PICK_MODE_CONTEXT *ctx = get_block_context(x, i);
1116       ctx->num_4x4_blk = num_4x4_blk;
1117       alloc_mode_context(cm, num_4x4_blk, ctx);
1118     }
1119   }
1120 }
1121 
free_pick_mode_context(MACROBLOCK * x)1122 static void free_pick_mode_context(MACROBLOCK *x) {
1123   int i;
1124 
1125   for (i = 0; i < BLOCK_SIZES; ++i) {
1126     const int num_4x4_w = num_4x4_blocks_wide_lookup[i];
1127     const int num_4x4_h = num_4x4_blocks_high_lookup[i];
1128     const int num_4x4_blk = MAX(4, num_4x4_w * num_4x4_h);
1129     if (i < BLOCK_16X16) {
1130       for (x->sb_index = 0; x->sb_index < 4; ++x->sb_index) {
1131         for (x->mb_index = 0; x->mb_index < 4; ++x->mb_index) {
1132           for (x->b_index = 0; x->b_index < 16 / num_4x4_blk; ++x->b_index) {
1133             PICK_MODE_CONTEXT *ctx = get_block_context(x, i);
1134             free_mode_context(ctx);
1135           }
1136         }
1137       }
1138     } else if (i < BLOCK_32X32) {
1139       for (x->sb_index = 0; x->sb_index < 4; ++x->sb_index) {
1140         for (x->mb_index = 0; x->mb_index < 64 / num_4x4_blk; ++x->mb_index) {
1141           PICK_MODE_CONTEXT *ctx = get_block_context(x, i);
1142           free_mode_context(ctx);
1143         }
1144       }
1145     } else if (i < BLOCK_64X64) {
1146       for (x->sb_index = 0; x->sb_index < 256 / num_4x4_blk; ++x->sb_index) {
1147         PICK_MODE_CONTEXT *ctx = get_block_context(x, i);
1148         free_mode_context(ctx);
1149       }
1150     } else {
1151       PICK_MODE_CONTEXT *ctx = get_block_context(x, i);
1152       free_mode_context(ctx);
1153     }
1154   }
1155 }
1156 
vp9_create_compressor(VP9_CONFIG * oxcf)1157 VP9_COMP *vp9_create_compressor(VP9_CONFIG *oxcf) {
1158   int i, j;
1159   VP9_COMP *const cpi = vpx_memalign(32, sizeof(VP9_COMP));
1160   VP9_COMMON *const cm = cpi != NULL ? &cpi->common : NULL;
1161   RATE_CONTROL *const rc = cpi != NULL ? &cpi->rc : NULL;
1162 
1163   if (!cm)
1164     return NULL;
1165 
1166   vp9_zero(*cpi);
1167 
1168   if (setjmp(cm->error.jmp)) {
1169     cm->error.setjmp = 0;
1170     vp9_remove_compressor(cpi);
1171     return 0;
1172   }
1173 
1174   cm->error.setjmp = 1;
1175 
1176   CHECK_MEM_ERROR(cm, cpi->mb.ss, vpx_calloc(sizeof(search_site),
1177                                              (MAX_MVSEARCH_STEPS * 8) + 1));
1178 
1179   vp9_rtcd();
1180 
1181   cpi->use_svc = 0;
1182 
1183   init_config(cpi, oxcf);
1184   init_rate_control(&cpi->oxcf, cpi->pass, &cpi->rc);
1185   init_pick_mode_context(cpi);
1186 
1187   cm->current_video_frame = 0;
1188 
1189   // Set reference frame sign bias for ALTREF frame to 1 (for now)
1190   cm->ref_frame_sign_bias[ALTREF_FRAME] = 1;
1191 
1192   rc->baseline_gf_interval = DEFAULT_GF_INTERVAL;
1193 
1194   cpi->gold_is_last = 0;
1195   cpi->alt_is_last = 0;
1196   cpi->gold_is_alt = 0;
1197 
1198   // Create the encoder segmentation map and set all entries to 0
1199   CHECK_MEM_ERROR(cm, cpi->segmentation_map,
1200                   vpx_calloc(cm->mi_rows * cm->mi_cols, 1));
1201 
1202   // Create a complexity map used for rd adjustment
1203   CHECK_MEM_ERROR(cm, cpi->complexity_map,
1204                   vpx_calloc(cm->mi_rows * cm->mi_cols, 1));
1205 
1206   // Create a map used for cyclic background refresh.
1207   CHECK_MEM_ERROR(cm, cpi->cyclic_refresh,
1208                   vp9_cyclic_refresh_alloc(cm->mi_rows, cm->mi_cols));
1209 
1210   // And a place holder structure is the coding context
1211   // for use if we want to save and restore it
1212   CHECK_MEM_ERROR(cm, cpi->coding_context.last_frame_seg_map_copy,
1213                   vpx_calloc(cm->mi_rows * cm->mi_cols, 1));
1214 
1215   CHECK_MEM_ERROR(cm, cpi->active_map, vpx_calloc(cm->MBs, 1));
1216   vpx_memset(cpi->active_map, 1, cm->MBs);
1217   cpi->active_map_enabled = 0;
1218 
1219   for (i = 0; i < (sizeof(cpi->mbgraph_stats) /
1220                    sizeof(cpi->mbgraph_stats[0])); i++) {
1221     CHECK_MEM_ERROR(cm, cpi->mbgraph_stats[i].mb_stats,
1222                     vpx_calloc(cm->MBs *
1223                                sizeof(*cpi->mbgraph_stats[i].mb_stats), 1));
1224   }
1225 
1226   /*Initialize the feed-forward activity masking.*/
1227   cpi->activity_avg = 90 << 12;
1228   cpi->key_frame_frequency = cpi->oxcf.key_freq;
1229 
1230   rc->frames_since_key = 8;  // Sensible default for first frame.
1231   rc->this_key_frame_forced = 0;
1232   rc->next_key_frame_forced = 0;
1233 
1234   rc->source_alt_ref_pending = 0;
1235   rc->source_alt_ref_active = 0;
1236   cpi->refresh_alt_ref_frame = 0;
1237 
1238 #if CONFIG_MULTIPLE_ARF
1239   // Turn multiple ARF usage on/off. This is a quick hack for the initial test
1240   // version. It should eventually be set via the codec API.
1241   cpi->multi_arf_enabled = 1;
1242 
1243   if (cpi->multi_arf_enabled) {
1244     cpi->sequence_number = 0;
1245     cpi->frame_coding_order_period = 0;
1246     vp9_zero(cpi->frame_coding_order);
1247     vp9_zero(cpi->arf_buffer_idx);
1248   }
1249 #endif
1250 
1251   cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
1252 #if CONFIG_INTERNAL_STATS
1253   cpi->b_calculate_ssimg = 0;
1254 
1255   cpi->count = 0;
1256   cpi->bytes = 0;
1257 
1258   if (cpi->b_calculate_psnr) {
1259     cpi->total_y = 0.0;
1260     cpi->total_u = 0.0;
1261     cpi->total_v = 0.0;
1262     cpi->total = 0.0;
1263     cpi->total_sq_error = 0;
1264     cpi->total_samples = 0;
1265 
1266     cpi->totalp_y = 0.0;
1267     cpi->totalp_u = 0.0;
1268     cpi->totalp_v = 0.0;
1269     cpi->totalp = 0.0;
1270     cpi->totalp_sq_error = 0;
1271     cpi->totalp_samples = 0;
1272 
1273     cpi->tot_recode_hits = 0;
1274     cpi->summed_quality = 0;
1275     cpi->summed_weights = 0;
1276     cpi->summedp_quality = 0;
1277     cpi->summedp_weights = 0;
1278   }
1279 
1280   if (cpi->b_calculate_ssimg) {
1281     cpi->total_ssimg_y = 0;
1282     cpi->total_ssimg_u = 0;
1283     cpi->total_ssimg_v = 0;
1284     cpi->total_ssimg_all = 0;
1285   }
1286 
1287 #endif
1288 
1289   cpi->first_time_stamp_ever = INT64_MAX;
1290 
1291   rc->frames_till_gf_update_due = 0;
1292 
1293   rc->ni_av_qi = cpi->oxcf.worst_allowed_q;
1294   rc->ni_tot_qi = 0;
1295   rc->ni_frames = 0;
1296   rc->tot_q = 0.0;
1297   rc->avg_q = vp9_convert_qindex_to_q(cpi->oxcf.worst_allowed_q);
1298 
1299   rc->rate_correction_factor = 1.0;
1300   rc->key_frame_rate_correction_factor = 1.0;
1301   rc->gf_rate_correction_factor = 1.0;
1302 
1303   cal_nmvjointsadcost(cpi->mb.nmvjointsadcost);
1304   cpi->mb.nmvcost[0] = &cpi->mb.nmvcosts[0][MV_MAX];
1305   cpi->mb.nmvcost[1] = &cpi->mb.nmvcosts[1][MV_MAX];
1306   cpi->mb.nmvsadcost[0] = &cpi->mb.nmvsadcosts[0][MV_MAX];
1307   cpi->mb.nmvsadcost[1] = &cpi->mb.nmvsadcosts[1][MV_MAX];
1308   cal_nmvsadcosts(cpi->mb.nmvsadcost);
1309 
1310   cpi->mb.nmvcost_hp[0] = &cpi->mb.nmvcosts_hp[0][MV_MAX];
1311   cpi->mb.nmvcost_hp[1] = &cpi->mb.nmvcosts_hp[1][MV_MAX];
1312   cpi->mb.nmvsadcost_hp[0] = &cpi->mb.nmvsadcosts_hp[0][MV_MAX];
1313   cpi->mb.nmvsadcost_hp[1] = &cpi->mb.nmvsadcosts_hp[1][MV_MAX];
1314   cal_nmvsadcosts_hp(cpi->mb.nmvsadcost_hp);
1315 
1316 #ifdef OUTPUT_YUV_SRC
1317   yuv_file = fopen("bd.yuv", "ab");
1318 #endif
1319 #ifdef OUTPUT_YUV_REC
1320   yuv_rec_file = fopen("rec.yuv", "wb");
1321 #endif
1322 
1323 #if 0
1324   framepsnr = fopen("framepsnr.stt", "a");
1325   kf_list = fopen("kf_list.stt", "w");
1326 #endif
1327 
1328   cpi->output_pkt_list = oxcf->output_pkt_list;
1329 
1330   cpi->allow_encode_breakout = ENCODE_BREAKOUT_ENABLED;
1331 
1332   if (cpi->pass == 1) {
1333     vp9_init_first_pass(cpi);
1334   } else if (cpi->pass == 2) {
1335     const size_t packet_sz = sizeof(FIRSTPASS_STATS);
1336     const int packets = (int)(oxcf->two_pass_stats_in.sz / packet_sz);
1337 
1338     if (cpi->svc.number_spatial_layers > 1
1339         && cpi->svc.number_temporal_layers == 1) {
1340       FIRSTPASS_STATS *const stats = oxcf->two_pass_stats_in.buf;
1341       FIRSTPASS_STATS *stats_copy[VPX_SS_MAX_LAYERS] = {0};
1342       int i;
1343 
1344       for (i = 0; i < oxcf->ss_number_layers; ++i) {
1345         FIRSTPASS_STATS *const last_packet_for_layer =
1346             &stats[packets - oxcf->ss_number_layers + i];
1347         const int layer_id = (int)last_packet_for_layer->spatial_layer_id;
1348         const int packets_in_layer = (int)last_packet_for_layer->count + 1;
1349         if (layer_id >= 0 && layer_id < oxcf->ss_number_layers) {
1350           LAYER_CONTEXT *const lc = &cpi->svc.layer_context[layer_id];
1351 
1352           vpx_free(lc->rc_twopass_stats_in.buf);
1353 
1354           lc->rc_twopass_stats_in.sz = packets_in_layer * packet_sz;
1355           CHECK_MEM_ERROR(cm, lc->rc_twopass_stats_in.buf,
1356                           vpx_malloc(lc->rc_twopass_stats_in.sz));
1357           lc->twopass.stats_in_start = lc->rc_twopass_stats_in.buf;
1358           lc->twopass.stats_in = lc->twopass.stats_in_start;
1359           lc->twopass.stats_in_end = lc->twopass.stats_in_start
1360                                      + packets_in_layer - 1;
1361           stats_copy[layer_id] = lc->rc_twopass_stats_in.buf;
1362         }
1363       }
1364 
1365       for (i = 0; i < packets; ++i) {
1366         const int layer_id = (int)stats[i].spatial_layer_id;
1367         if (layer_id >= 0 && layer_id < oxcf->ss_number_layers
1368             && stats_copy[layer_id] != NULL) {
1369           *stats_copy[layer_id] = stats[i];
1370           ++stats_copy[layer_id];
1371         }
1372       }
1373 
1374       vp9_init_second_pass_spatial_svc(cpi);
1375     } else {
1376       cpi->twopass.stats_in_start = oxcf->two_pass_stats_in.buf;
1377       cpi->twopass.stats_in = cpi->twopass.stats_in_start;
1378       cpi->twopass.stats_in_end = &cpi->twopass.stats_in[packets - 1];
1379 
1380       vp9_init_second_pass(cpi);
1381     }
1382   }
1383 
1384   set_speed_features(cpi);
1385 
1386   // Default rd threshold factors for mode selection
1387   for (i = 0; i < BLOCK_SIZES; ++i) {
1388     for (j = 0; j < MAX_MODES; ++j)
1389       cpi->rd_thresh_freq_fact[i][j] = 32;
1390     for (j = 0; j < MAX_REFS; ++j)
1391       cpi->rd_thresh_freq_sub8x8[i][j] = 32;
1392   }
1393 
1394 #define BFP(BT, SDF, SDAF, VF, SVF, SVAF, SVFHH, SVFHV, SVFHHV, \
1395             SDX3F, SDX8F, SDX4DF)\
1396     cpi->fn_ptr[BT].sdf            = SDF; \
1397     cpi->fn_ptr[BT].sdaf           = SDAF; \
1398     cpi->fn_ptr[BT].vf             = VF; \
1399     cpi->fn_ptr[BT].svf            = SVF; \
1400     cpi->fn_ptr[BT].svaf           = SVAF; \
1401     cpi->fn_ptr[BT].svf_halfpix_h  = SVFHH; \
1402     cpi->fn_ptr[BT].svf_halfpix_v  = SVFHV; \
1403     cpi->fn_ptr[BT].svf_halfpix_hv = SVFHHV; \
1404     cpi->fn_ptr[BT].sdx3f          = SDX3F; \
1405     cpi->fn_ptr[BT].sdx8f          = SDX8F; \
1406     cpi->fn_ptr[BT].sdx4df         = SDX4DF;
1407 
1408   BFP(BLOCK_32X16, vp9_sad32x16, vp9_sad32x16_avg,
1409       vp9_variance32x16, vp9_sub_pixel_variance32x16,
1410       vp9_sub_pixel_avg_variance32x16, NULL, NULL,
1411       NULL, NULL, NULL,
1412       vp9_sad32x16x4d)
1413 
1414   BFP(BLOCK_16X32, vp9_sad16x32, vp9_sad16x32_avg,
1415       vp9_variance16x32, vp9_sub_pixel_variance16x32,
1416       vp9_sub_pixel_avg_variance16x32, NULL, NULL,
1417       NULL, NULL, NULL,
1418       vp9_sad16x32x4d)
1419 
1420   BFP(BLOCK_64X32, vp9_sad64x32, vp9_sad64x32_avg,
1421       vp9_variance64x32, vp9_sub_pixel_variance64x32,
1422       vp9_sub_pixel_avg_variance64x32, NULL, NULL,
1423       NULL, NULL, NULL,
1424       vp9_sad64x32x4d)
1425 
1426   BFP(BLOCK_32X64, vp9_sad32x64, vp9_sad32x64_avg,
1427       vp9_variance32x64, vp9_sub_pixel_variance32x64,
1428       vp9_sub_pixel_avg_variance32x64, NULL, NULL,
1429       NULL, NULL, NULL,
1430       vp9_sad32x64x4d)
1431 
1432   BFP(BLOCK_32X32, vp9_sad32x32, vp9_sad32x32_avg,
1433       vp9_variance32x32, vp9_sub_pixel_variance32x32,
1434       vp9_sub_pixel_avg_variance32x32, vp9_variance_halfpixvar32x32_h,
1435       vp9_variance_halfpixvar32x32_v,
1436       vp9_variance_halfpixvar32x32_hv, vp9_sad32x32x3, vp9_sad32x32x8,
1437       vp9_sad32x32x4d)
1438 
1439   BFP(BLOCK_64X64, vp9_sad64x64, vp9_sad64x64_avg,
1440       vp9_variance64x64, vp9_sub_pixel_variance64x64,
1441       vp9_sub_pixel_avg_variance64x64, vp9_variance_halfpixvar64x64_h,
1442       vp9_variance_halfpixvar64x64_v,
1443       vp9_variance_halfpixvar64x64_hv, vp9_sad64x64x3, vp9_sad64x64x8,
1444       vp9_sad64x64x4d)
1445 
1446   BFP(BLOCK_16X16, vp9_sad16x16, vp9_sad16x16_avg,
1447       vp9_variance16x16, vp9_sub_pixel_variance16x16,
1448       vp9_sub_pixel_avg_variance16x16, vp9_variance_halfpixvar16x16_h,
1449       vp9_variance_halfpixvar16x16_v,
1450       vp9_variance_halfpixvar16x16_hv, vp9_sad16x16x3, vp9_sad16x16x8,
1451       vp9_sad16x16x4d)
1452 
1453   BFP(BLOCK_16X8, vp9_sad16x8, vp9_sad16x8_avg,
1454       vp9_variance16x8, vp9_sub_pixel_variance16x8,
1455       vp9_sub_pixel_avg_variance16x8, NULL, NULL, NULL,
1456       vp9_sad16x8x3, vp9_sad16x8x8, vp9_sad16x8x4d)
1457 
1458   BFP(BLOCK_8X16, vp9_sad8x16, vp9_sad8x16_avg,
1459       vp9_variance8x16, vp9_sub_pixel_variance8x16,
1460       vp9_sub_pixel_avg_variance8x16, NULL, NULL, NULL,
1461       vp9_sad8x16x3, vp9_sad8x16x8, vp9_sad8x16x4d)
1462 
1463   BFP(BLOCK_8X8, vp9_sad8x8, vp9_sad8x8_avg,
1464       vp9_variance8x8, vp9_sub_pixel_variance8x8,
1465       vp9_sub_pixel_avg_variance8x8, NULL, NULL, NULL,
1466       vp9_sad8x8x3, vp9_sad8x8x8, vp9_sad8x8x4d)
1467 
1468   BFP(BLOCK_8X4, vp9_sad8x4, vp9_sad8x4_avg,
1469       vp9_variance8x4, vp9_sub_pixel_variance8x4,
1470       vp9_sub_pixel_avg_variance8x4, NULL, NULL,
1471       NULL, NULL, vp9_sad8x4x8,
1472       vp9_sad8x4x4d)
1473 
1474   BFP(BLOCK_4X8, vp9_sad4x8, vp9_sad4x8_avg,
1475       vp9_variance4x8, vp9_sub_pixel_variance4x8,
1476       vp9_sub_pixel_avg_variance4x8, NULL, NULL,
1477       NULL, NULL, vp9_sad4x8x8,
1478       vp9_sad4x8x4d)
1479 
1480   BFP(BLOCK_4X4, vp9_sad4x4, vp9_sad4x4_avg,
1481       vp9_variance4x4, vp9_sub_pixel_variance4x4,
1482       vp9_sub_pixel_avg_variance4x4, NULL, NULL, NULL,
1483       vp9_sad4x4x3, vp9_sad4x4x8, vp9_sad4x4x4d)
1484 
1485   cpi->full_search_sad = vp9_full_search_sad;
1486   cpi->diamond_search_sad = vp9_diamond_search_sad;
1487   cpi->refining_search_sad = vp9_refining_search_sad;
1488 
1489   /* vp9_init_quantizer() is first called here. Add check in
1490    * vp9_frame_init_quantizer() so that vp9_init_quantizer is only
1491    * called later when needed. This will avoid unnecessary calls of
1492    * vp9_init_quantizer() for every frame.
1493    */
1494   vp9_init_quantizer(cpi);
1495 
1496   vp9_loop_filter_init(cm);
1497 
1498   cm->error.setjmp = 0;
1499 
1500   vp9_zero(cpi->common.counts.uv_mode);
1501 
1502 #ifdef MODE_TEST_HIT_STATS
1503   vp9_zero(cpi->mode_test_hits);
1504 #endif
1505 
1506   return cpi;
1507 }
1508 
vp9_remove_compressor(VP9_COMP * cpi)1509 void vp9_remove_compressor(VP9_COMP *cpi) {
1510   int i;
1511 
1512   if (!cpi)
1513     return;
1514 
1515   if (cpi && (cpi->common.current_video_frame > 0)) {
1516 #if CONFIG_INTERNAL_STATS
1517 
1518     vp9_clear_system_state();
1519 
1520     // printf("\n8x8-4x4:%d-%d\n", cpi->t8x8_count, cpi->t4x4_count);
1521     if (cpi->pass != 1) {
1522       FILE *f = fopen("opsnr.stt", "a");
1523       double time_encoded = (cpi->last_end_time_stamp_seen
1524                              - cpi->first_time_stamp_ever) / 10000000.000;
1525       double total_encode_time = (cpi->time_receive_data +
1526                                   cpi->time_compress_data)   / 1000.000;
1527       double dr = (double)cpi->bytes * (double) 8 / (double)1000
1528                   / time_encoded;
1529 
1530       if (cpi->b_calculate_psnr) {
1531         const double total_psnr =
1532             vpx_sse_to_psnr((double)cpi->total_samples, 255.0,
1533                             (double)cpi->total_sq_error);
1534         const double totalp_psnr =
1535             vpx_sse_to_psnr((double)cpi->totalp_samples, 255.0,
1536                             (double)cpi->totalp_sq_error);
1537         const double total_ssim = 100 * pow(cpi->summed_quality /
1538                                                 cpi->summed_weights, 8.0);
1539         const double totalp_ssim = 100 * pow(cpi->summedp_quality /
1540                                                 cpi->summedp_weights, 8.0);
1541 
1542         fprintf(f, "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\t"
1543                 "VPXSSIM\tVPSSIMP\t  Time(ms)\n");
1544         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",
1545                 dr, cpi->total / cpi->count, total_psnr,
1546                 cpi->totalp / cpi->count, totalp_psnr, total_ssim, totalp_ssim,
1547                 total_encode_time);
1548       }
1549 
1550       if (cpi->b_calculate_ssimg) {
1551         fprintf(f, "BitRate\tSSIM_Y\tSSIM_U\tSSIM_V\tSSIM_A\t  Time(ms)\n");
1552         fprintf(f, "%7.2f\t%6.4f\t%6.4f\t%6.4f\t%6.4f\t%8.0f\n", dr,
1553                 cpi->total_ssimg_y / cpi->count,
1554                 cpi->total_ssimg_u / cpi->count,
1555                 cpi->total_ssimg_v / cpi->count,
1556                 cpi->total_ssimg_all / cpi->count, total_encode_time);
1557       }
1558 
1559       fclose(f);
1560     }
1561 
1562 #endif
1563 
1564 #ifdef MODE_TEST_HIT_STATS
1565     if (cpi->pass != 1) {
1566       double norm_per_pixel_mode_tests = 0;
1567       double norm_counts[BLOCK_SIZES];
1568       int i;
1569       int sb64_per_frame;
1570       int norm_factors[BLOCK_SIZES] =
1571         {256, 128, 128, 64, 32, 32, 16, 8, 8, 4, 2, 2, 1};
1572       FILE *f = fopen("mode_hit_stats.stt", "a");
1573 
1574       // On average, how many mode tests do we do
1575       for (i = 0; i < BLOCK_SIZES; ++i) {
1576         norm_counts[i] = (double)cpi->mode_test_hits[i] /
1577                          (double)norm_factors[i];
1578         norm_per_pixel_mode_tests += norm_counts[i];
1579       }
1580       // Convert to a number per 64x64 and per frame
1581       sb64_per_frame = ((cpi->common.height + 63) / 64) *
1582                        ((cpi->common.width + 63) / 64);
1583       norm_per_pixel_mode_tests =
1584         norm_per_pixel_mode_tests /
1585         (double)(cpi->common.current_video_frame * sb64_per_frame);
1586 
1587       fprintf(f, "%6.4f\n", norm_per_pixel_mode_tests);
1588       fclose(f);
1589     }
1590 #endif
1591 
1592 #if 0
1593     {
1594       printf("\n_pick_loop_filter_level:%d\n", cpi->time_pick_lpf / 1000);
1595       printf("\n_frames recive_data encod_mb_row compress_frame  Total\n");
1596       printf("%6d %10ld %10ld %10ld %10ld\n", cpi->common.current_video_frame,
1597              cpi->time_receive_data / 1000, cpi->time_encode_sb_row / 1000,
1598              cpi->time_compress_data / 1000,
1599              (cpi->time_receive_data + cpi->time_compress_data) / 1000);
1600     }
1601 #endif
1602   }
1603 
1604   free_pick_mode_context(&cpi->mb);
1605   dealloc_compressor_data(cpi);
1606   vpx_free(cpi->mb.ss);
1607   vpx_free(cpi->tok);
1608 
1609   for (i = 0; i < sizeof(cpi->mbgraph_stats) /
1610                   sizeof(cpi->mbgraph_stats[0]); ++i) {
1611     vpx_free(cpi->mbgraph_stats[i].mb_stats);
1612   }
1613 
1614   vp9_remove_common(&cpi->common);
1615   vpx_free(cpi);
1616 
1617 #ifdef OUTPUT_YUV_SRC
1618   fclose(yuv_file);
1619 #endif
1620 #ifdef OUTPUT_YUV_REC
1621   fclose(yuv_rec_file);
1622 #endif
1623 
1624 #if 0
1625 
1626   if (keyfile)
1627     fclose(keyfile);
1628 
1629   if (framepsnr)
1630     fclose(framepsnr);
1631 
1632   if (kf_list)
1633     fclose(kf_list);
1634 
1635 #endif
1636 }
1637 
1638 
calc_plane_error(const uint8_t * orig,int orig_stride,const uint8_t * recon,int recon_stride,unsigned int cols,unsigned int rows)1639 static uint64_t calc_plane_error(const uint8_t *orig, int orig_stride,
1640                                  const uint8_t *recon, int recon_stride,
1641                                  unsigned int cols, unsigned int rows) {
1642   unsigned int row, col;
1643   uint64_t total_sse = 0;
1644   int diff;
1645 
1646   for (row = 0; row + 16 <= rows; row += 16) {
1647     for (col = 0; col + 16 <= cols; col += 16) {
1648       unsigned int sse;
1649 
1650       vp9_mse16x16(orig + col, orig_stride, recon + col, recon_stride, &sse);
1651       total_sse += sse;
1652     }
1653 
1654     /* Handle odd-sized width */
1655     if (col < cols) {
1656       unsigned int border_row, border_col;
1657       const uint8_t *border_orig = orig;
1658       const uint8_t *border_recon = recon;
1659 
1660       for (border_row = 0; border_row < 16; border_row++) {
1661         for (border_col = col; border_col < cols; border_col++) {
1662           diff = border_orig[border_col] - border_recon[border_col];
1663           total_sse += diff * diff;
1664         }
1665 
1666         border_orig += orig_stride;
1667         border_recon += recon_stride;
1668       }
1669     }
1670 
1671     orig += orig_stride * 16;
1672     recon += recon_stride * 16;
1673   }
1674 
1675   /* Handle odd-sized height */
1676   for (; row < rows; row++) {
1677     for (col = 0; col < cols; col++) {
1678       diff = orig[col] - recon[col];
1679       total_sse += diff * diff;
1680     }
1681 
1682     orig += orig_stride;
1683     recon += recon_stride;
1684   }
1685 
1686   return total_sse;
1687 }
1688 
1689 typedef struct {
1690   double psnr[4];       // total/y/u/v
1691   uint64_t sse[4];      // total/y/u/v
1692   uint32_t samples[4];  // total/y/u/v
1693 } PSNR_STATS;
1694 
calc_psnr(const YV12_BUFFER_CONFIG * a,const YV12_BUFFER_CONFIG * b,PSNR_STATS * psnr)1695 static void calc_psnr(const YV12_BUFFER_CONFIG *a, const YV12_BUFFER_CONFIG *b,
1696                       PSNR_STATS *psnr) {
1697   const int widths[3]        = {a->y_width,  a->uv_width,  a->uv_width };
1698   const int heights[3]       = {a->y_height, a->uv_height, a->uv_height};
1699   const uint8_t *a_planes[3] = {a->y_buffer, a->u_buffer,  a->v_buffer };
1700   const int a_strides[3]     = {a->y_stride, a->uv_stride, a->uv_stride};
1701   const uint8_t *b_planes[3] = {b->y_buffer, b->u_buffer,  b->v_buffer };
1702   const int b_strides[3]     = {b->y_stride, b->uv_stride, b->uv_stride};
1703   int i;
1704   uint64_t total_sse = 0;
1705   uint32_t total_samples = 0;
1706 
1707   for (i = 0; i < 3; ++i) {
1708     const int w = widths[i];
1709     const int h = heights[i];
1710     const uint32_t samples = w * h;
1711     const uint64_t sse = calc_plane_error(a_planes[i], a_strides[i],
1712                                           b_planes[i], b_strides[i],
1713                                           w, h);
1714     psnr->sse[1 + i] = sse;
1715     psnr->samples[1 + i] = samples;
1716     psnr->psnr[1 + i] = vpx_sse_to_psnr(samples, 255.0, (double)sse);
1717 
1718     total_sse += sse;
1719     total_samples += samples;
1720   }
1721 
1722   psnr->sse[0] = total_sse;
1723   psnr->samples[0] = total_samples;
1724   psnr->psnr[0] = vpx_sse_to_psnr((double)total_samples, 255.0,
1725                                   (double)total_sse);
1726 }
1727 
generate_psnr_packet(VP9_COMP * cpi)1728 static void generate_psnr_packet(VP9_COMP *cpi) {
1729   struct vpx_codec_cx_pkt pkt;
1730   int i;
1731   PSNR_STATS psnr;
1732   calc_psnr(cpi->Source, cpi->common.frame_to_show, &psnr);
1733   for (i = 0; i < 4; ++i) {
1734     pkt.data.psnr.samples[i] = psnr.samples[i];
1735     pkt.data.psnr.sse[i] = psnr.sse[i];
1736     pkt.data.psnr.psnr[i] = psnr.psnr[i];
1737   }
1738   pkt.kind = VPX_CODEC_PSNR_PKT;
1739   vpx_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
1740 }
1741 
vp9_use_as_reference(VP9_COMP * cpi,int ref_frame_flags)1742 int vp9_use_as_reference(VP9_COMP *cpi, int ref_frame_flags) {
1743   if (ref_frame_flags > 7)
1744     return -1;
1745 
1746   cpi->ref_frame_flags = ref_frame_flags;
1747   return 0;
1748 }
1749 
vp9_update_reference(VP9_COMP * cpi,int ref_frame_flags)1750 void vp9_update_reference(VP9_COMP *cpi, int ref_frame_flags) {
1751   cpi->ext_refresh_golden_frame = (ref_frame_flags & VP9_GOLD_FLAG) != 0;
1752   cpi->ext_refresh_alt_ref_frame = (ref_frame_flags & VP9_ALT_FLAG) != 0;
1753   cpi->ext_refresh_last_frame = (ref_frame_flags & VP9_LAST_FLAG) != 0;
1754   cpi->ext_refresh_frame_flags_pending = 1;
1755 }
1756 
get_vp9_ref_frame_buffer(VP9_COMP * cpi,VP9_REFFRAME ref_frame_flag)1757 static YV12_BUFFER_CONFIG *get_vp9_ref_frame_buffer(VP9_COMP *cpi,
1758                                 VP9_REFFRAME ref_frame_flag) {
1759   MV_REFERENCE_FRAME ref_frame = NONE;
1760   if (ref_frame_flag == VP9_LAST_FLAG)
1761     ref_frame = LAST_FRAME;
1762   else if (ref_frame_flag == VP9_GOLD_FLAG)
1763     ref_frame = GOLDEN_FRAME;
1764   else if (ref_frame_flag == VP9_ALT_FLAG)
1765     ref_frame = ALTREF_FRAME;
1766 
1767   return ref_frame == NONE ? NULL : get_ref_frame_buffer(cpi, ref_frame);
1768 }
1769 
vp9_copy_reference_enc(VP9_COMP * cpi,VP9_REFFRAME ref_frame_flag,YV12_BUFFER_CONFIG * sd)1770 int vp9_copy_reference_enc(VP9_COMP *cpi, VP9_REFFRAME ref_frame_flag,
1771                            YV12_BUFFER_CONFIG *sd) {
1772   YV12_BUFFER_CONFIG *cfg = get_vp9_ref_frame_buffer(cpi, ref_frame_flag);
1773   if (cfg) {
1774     vp8_yv12_copy_frame(cfg, sd);
1775     return 0;
1776   } else {
1777     return -1;
1778   }
1779 }
1780 
vp9_get_reference_enc(VP9_COMP * cpi,int index,YV12_BUFFER_CONFIG ** fb)1781 int vp9_get_reference_enc(VP9_COMP *cpi, int index, YV12_BUFFER_CONFIG **fb) {
1782   VP9_COMMON *cm = &cpi->common;
1783 
1784   if (index < 0 || index >= REF_FRAMES)
1785     return -1;
1786 
1787   *fb = &cm->frame_bufs[cm->ref_frame_map[index]].buf;
1788   return 0;
1789 }
1790 
vp9_set_reference_enc(VP9_COMP * cpi,VP9_REFFRAME ref_frame_flag,YV12_BUFFER_CONFIG * sd)1791 int vp9_set_reference_enc(VP9_COMP *cpi, VP9_REFFRAME ref_frame_flag,
1792                           YV12_BUFFER_CONFIG *sd) {
1793   YV12_BUFFER_CONFIG *cfg = get_vp9_ref_frame_buffer(cpi, ref_frame_flag);
1794   if (cfg) {
1795     vp8_yv12_copy_frame(sd, cfg);
1796     return 0;
1797   } else {
1798     return -1;
1799   }
1800 }
1801 
vp9_update_entropy(VP9_COMP * cpi,int update)1802 int vp9_update_entropy(VP9_COMP * cpi, int update) {
1803   cpi->ext_refresh_frame_context = update;
1804   cpi->ext_refresh_frame_context_pending = 1;
1805   return 0;
1806 }
1807 
1808 
1809 #ifdef OUTPUT_YUV_SRC
vp9_write_yuv_frame(YV12_BUFFER_CONFIG * s)1810 void vp9_write_yuv_frame(YV12_BUFFER_CONFIG *s) {
1811   uint8_t *src = s->y_buffer;
1812   int h = s->y_height;
1813 
1814   do {
1815     fwrite(src, s->y_width, 1,  yuv_file);
1816     src += s->y_stride;
1817   } while (--h);
1818 
1819   src = s->u_buffer;
1820   h = s->uv_height;
1821 
1822   do {
1823     fwrite(src, s->uv_width, 1,  yuv_file);
1824     src += s->uv_stride;
1825   } while (--h);
1826 
1827   src = s->v_buffer;
1828   h = s->uv_height;
1829 
1830   do {
1831     fwrite(src, s->uv_width, 1, yuv_file);
1832     src += s->uv_stride;
1833   } while (--h);
1834 }
1835 #endif
1836 
1837 #ifdef OUTPUT_YUV_REC
vp9_write_yuv_rec_frame(VP9_COMMON * cm)1838 void vp9_write_yuv_rec_frame(VP9_COMMON *cm) {
1839   YV12_BUFFER_CONFIG *s = cm->frame_to_show;
1840   uint8_t *src = s->y_buffer;
1841   int h = cm->height;
1842 
1843   do {
1844     fwrite(src, s->y_width, 1,  yuv_rec_file);
1845     src += s->y_stride;
1846   } while (--h);
1847 
1848   src = s->u_buffer;
1849   h = s->uv_height;
1850 
1851   do {
1852     fwrite(src, s->uv_width, 1,  yuv_rec_file);
1853     src += s->uv_stride;
1854   } while (--h);
1855 
1856   src = s->v_buffer;
1857   h = s->uv_height;
1858 
1859   do {
1860     fwrite(src, s->uv_width, 1, yuv_rec_file);
1861     src += s->uv_stride;
1862   } while (--h);
1863 
1864 #if CONFIG_ALPHA
1865   if (s->alpha_buffer) {
1866     src = s->alpha_buffer;
1867     h = s->alpha_height;
1868     do {
1869       fwrite(src, s->alpha_width, 1,  yuv_rec_file);
1870       src += s->alpha_stride;
1871     } while (--h);
1872   }
1873 #endif
1874 
1875   fflush(yuv_rec_file);
1876 }
1877 #endif
1878 
scale_and_extend_frame_nonnormative(YV12_BUFFER_CONFIG * src_fb,YV12_BUFFER_CONFIG * dst_fb)1879 static void scale_and_extend_frame_nonnormative(YV12_BUFFER_CONFIG *src_fb,
1880                                                 YV12_BUFFER_CONFIG *dst_fb) {
1881   const int in_w = src_fb->y_crop_width;
1882   const int in_h = src_fb->y_crop_height;
1883   const int out_w = dst_fb->y_crop_width;
1884   const int out_h = dst_fb->y_crop_height;
1885   const int in_w_uv = src_fb->uv_crop_width;
1886   const int in_h_uv = src_fb->uv_crop_height;
1887   const int out_w_uv = dst_fb->uv_crop_width;
1888   const int out_h_uv = dst_fb->uv_crop_height;
1889   int i;
1890 
1891   uint8_t *srcs[4] = {src_fb->y_buffer, src_fb->u_buffer, src_fb->v_buffer,
1892     src_fb->alpha_buffer};
1893   int src_strides[4] = {src_fb->y_stride, src_fb->uv_stride, src_fb->uv_stride,
1894     src_fb->alpha_stride};
1895 
1896   uint8_t *dsts[4] = {dst_fb->y_buffer, dst_fb->u_buffer, dst_fb->v_buffer,
1897     dst_fb->alpha_buffer};
1898   int dst_strides[4] = {dst_fb->y_stride, dst_fb->uv_stride, dst_fb->uv_stride,
1899     dst_fb->alpha_stride};
1900 
1901   for (i = 0; i < MAX_MB_PLANE; ++i) {
1902     if (i == 0 || i == 3) {
1903       // Y and alpha planes
1904       vp9_resize_plane(srcs[i], in_h, in_w, src_strides[i],
1905                        dsts[i], out_h, out_w, dst_strides[i]);
1906     } else {
1907       // Chroma planes
1908       vp9_resize_plane(srcs[i], in_h_uv, in_w_uv, src_strides[i],
1909                        dsts[i], out_h_uv, out_w_uv, dst_strides[i]);
1910     }
1911   }
1912   vp8_yv12_extend_frame_borders(dst_fb);
1913 }
1914 
scale_and_extend_frame(YV12_BUFFER_CONFIG * src_fb,YV12_BUFFER_CONFIG * dst_fb)1915 static void scale_and_extend_frame(YV12_BUFFER_CONFIG *src_fb,
1916                                    YV12_BUFFER_CONFIG *dst_fb) {
1917   const int in_w = src_fb->y_crop_width;
1918   const int in_h = src_fb->y_crop_height;
1919   const int out_w = dst_fb->y_crop_width;
1920   const int out_h = dst_fb->y_crop_height;
1921   int x, y, i;
1922 
1923   uint8_t *srcs[4] = {src_fb->y_buffer, src_fb->u_buffer, src_fb->v_buffer,
1924                       src_fb->alpha_buffer};
1925   int src_strides[4] = {src_fb->y_stride, src_fb->uv_stride, src_fb->uv_stride,
1926                         src_fb->alpha_stride};
1927 
1928   uint8_t *dsts[4] = {dst_fb->y_buffer, dst_fb->u_buffer, dst_fb->v_buffer,
1929                       dst_fb->alpha_buffer};
1930   int dst_strides[4] = {dst_fb->y_stride, dst_fb->uv_stride, dst_fb->uv_stride,
1931                         dst_fb->alpha_stride};
1932 
1933   for (y = 0; y < out_h; y += 16) {
1934     for (x = 0; x < out_w; x += 16) {
1935       for (i = 0; i < MAX_MB_PLANE; ++i) {
1936         const int factor = (i == 0 || i == 3 ? 1 : 2);
1937         const int x_q4 = x * (16 / factor) * in_w / out_w;
1938         const int y_q4 = y * (16 / factor) * in_h / out_h;
1939         const int src_stride = src_strides[i];
1940         const int dst_stride = dst_strides[i];
1941         uint8_t *src = srcs[i] + y / factor * in_h / out_h * src_stride +
1942                                  x / factor * in_w / out_w;
1943         uint8_t *dst = dsts[i] + y / factor * dst_stride + x / factor;
1944 
1945         vp9_convolve8(src, src_stride, dst, dst_stride,
1946                       vp9_sub_pel_filters_8[x_q4 & 0xf], 16 * in_w / out_w,
1947                       vp9_sub_pel_filters_8[y_q4 & 0xf], 16 * in_h / out_h,
1948                       16 / factor, 16 / factor);
1949       }
1950     }
1951   }
1952 
1953   vp8_yv12_extend_frame_borders(dst_fb);
1954 }
1955 
find_fp_qindex()1956 static int find_fp_qindex() {
1957   int i;
1958 
1959   for (i = 0; i < QINDEX_RANGE; i++) {
1960     if (vp9_convert_qindex_to_q(i) >= 30.0) {
1961       break;
1962     }
1963   }
1964 
1965   if (i == QINDEX_RANGE)
1966     i--;
1967 
1968   return i;
1969 }
1970 
1971 #define WRITE_RECON_BUFFER 0
1972 #if WRITE_RECON_BUFFER
write_cx_frame_to_file(YV12_BUFFER_CONFIG * frame,int this_frame)1973 void write_cx_frame_to_file(YV12_BUFFER_CONFIG *frame, int this_frame) {
1974   FILE *yframe;
1975   int i;
1976   char filename[255];
1977 
1978   snprintf(filename, sizeof(filename), "cx\\y%04d.raw", this_frame);
1979   yframe = fopen(filename, "wb");
1980 
1981   for (i = 0; i < frame->y_height; i++)
1982     fwrite(frame->y_buffer + i * frame->y_stride,
1983            frame->y_width, 1, yframe);
1984 
1985   fclose(yframe);
1986   snprintf(filename, sizeof(filename), "cx\\u%04d.raw", this_frame);
1987   yframe = fopen(filename, "wb");
1988 
1989   for (i = 0; i < frame->uv_height; i++)
1990     fwrite(frame->u_buffer + i * frame->uv_stride,
1991            frame->uv_width, 1, yframe);
1992 
1993   fclose(yframe);
1994   snprintf(filename, sizeof(filename), "cx\\v%04d.raw", this_frame);
1995   yframe = fopen(filename, "wb");
1996 
1997   for (i = 0; i < frame->uv_height; i++)
1998     fwrite(frame->v_buffer + i * frame->uv_stride,
1999            frame->uv_width, 1, yframe);
2000 
2001   fclose(yframe);
2002 }
2003 #endif
2004 
2005 // Function to test for conditions that indicate we should loop
2006 // back and recode a frame.
recode_loop_test(const VP9_COMP * cpi,int high_limit,int low_limit,int q,int maxq,int minq)2007 static int recode_loop_test(const VP9_COMP *cpi,
2008                             int high_limit, int low_limit,
2009                             int q, int maxq, int minq) {
2010   const VP9_COMMON *const cm = &cpi->common;
2011   const RATE_CONTROL *const rc = &cpi->rc;
2012   int force_recode = 0;
2013 
2014   // Special case trap if maximum allowed frame size exceeded.
2015   if (rc->projected_frame_size > rc->max_frame_bandwidth) {
2016     force_recode = 1;
2017 
2018   // Is frame recode allowed.
2019   // Yes if either recode mode 1 is selected or mode 2 is selected
2020   // and the frame is a key frame, golden frame or alt_ref_frame
2021   } else if ((cpi->sf.recode_loop == ALLOW_RECODE) ||
2022              ((cpi->sf.recode_loop == ALLOW_RECODE_KFARFGF) &&
2023               (cm->frame_type == KEY_FRAME ||
2024                cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame))) {
2025     // General over and under shoot tests
2026     if ((rc->projected_frame_size > high_limit && q < maxq) ||
2027         (rc->projected_frame_size < low_limit && q > minq)) {
2028       force_recode = 1;
2029     } else if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) {
2030       // Deal with frame undershoot and whether or not we are
2031       // below the automatically set cq level.
2032       if (q > cpi->cq_target_quality &&
2033           rc->projected_frame_size < ((rc->this_frame_target * 7) >> 3)) {
2034         force_recode = 1;
2035       }
2036     }
2037   }
2038   return force_recode;
2039 }
2040 
vp9_update_reference_frames(VP9_COMP * cpi)2041 void vp9_update_reference_frames(VP9_COMP *cpi) {
2042   VP9_COMMON * const cm = &cpi->common;
2043 
2044   // At this point the new frame has been encoded.
2045   // If any buffer copy / swapping is signaled it should be done here.
2046   if (cm->frame_type == KEY_FRAME) {
2047     ref_cnt_fb(cm->frame_bufs,
2048                &cm->ref_frame_map[cpi->gld_fb_idx], cm->new_fb_idx);
2049     ref_cnt_fb(cm->frame_bufs,
2050                &cm->ref_frame_map[cpi->alt_fb_idx], cm->new_fb_idx);
2051   }
2052 #if CONFIG_MULTIPLE_ARF
2053   else if (!cpi->multi_arf_enabled && cpi->refresh_golden_frame &&
2054       !cpi->refresh_alt_ref_frame) {
2055 #else
2056   else if (cpi->refresh_golden_frame && !cpi->refresh_alt_ref_frame &&
2057            !cpi->use_svc) {
2058 #endif
2059     /* Preserve the previously existing golden frame and update the frame in
2060      * the alt ref slot instead. This is highly specific to the current use of
2061      * alt-ref as a forward reference, and this needs to be generalized as
2062      * other uses are implemented (like RTC/temporal scaling)
2063      *
2064      * The update to the buffer in the alt ref slot was signaled in
2065      * vp9_pack_bitstream(), now swap the buffer pointers so that it's treated
2066      * as the golden frame next time.
2067      */
2068     int tmp;
2069 
2070     ref_cnt_fb(cm->frame_bufs,
2071                &cm->ref_frame_map[cpi->alt_fb_idx], cm->new_fb_idx);
2072 
2073     tmp = cpi->alt_fb_idx;
2074     cpi->alt_fb_idx = cpi->gld_fb_idx;
2075     cpi->gld_fb_idx = tmp;
2076   }  else { /* For non key/golden frames */
2077     if (cpi->refresh_alt_ref_frame) {
2078       int arf_idx = cpi->alt_fb_idx;
2079 #if CONFIG_MULTIPLE_ARF
2080       if (cpi->multi_arf_enabled) {
2081         arf_idx = cpi->arf_buffer_idx[cpi->sequence_number + 1];
2082       }
2083 #endif
2084       ref_cnt_fb(cm->frame_bufs,
2085                  &cm->ref_frame_map[arf_idx], cm->new_fb_idx);
2086     }
2087 
2088     if (cpi->refresh_golden_frame) {
2089       ref_cnt_fb(cm->frame_bufs,
2090                  &cm->ref_frame_map[cpi->gld_fb_idx], cm->new_fb_idx);
2091     }
2092   }
2093 
2094   if (cpi->refresh_last_frame) {
2095     ref_cnt_fb(cm->frame_bufs,
2096                &cm->ref_frame_map[cpi->lst_fb_idx], cm->new_fb_idx);
2097   }
2098 }
2099 
2100 static void loopfilter_frame(VP9_COMP *cpi, VP9_COMMON *cm) {
2101   MACROBLOCKD *xd = &cpi->mb.e_mbd;
2102   struct loopfilter *lf = &cm->lf;
2103   if (xd->lossless) {
2104       lf->filter_level = 0;
2105   } else {
2106     struct vpx_usec_timer timer;
2107 
2108     vp9_clear_system_state();
2109 
2110     vpx_usec_timer_start(&timer);
2111 
2112     vp9_pick_filter_level(cpi->Source, cpi, cpi->sf.lpf_pick);
2113 
2114     vpx_usec_timer_mark(&timer);
2115     cpi->time_pick_lpf += vpx_usec_timer_elapsed(&timer);
2116   }
2117 
2118   if (lf->filter_level > 0) {
2119     vp9_loop_filter_frame(cm, xd, lf->filter_level, 0, 0);
2120   }
2121 
2122   vp9_extend_frame_inner_borders(cm->frame_to_show);
2123 }
2124 
2125 void vp9_scale_references(VP9_COMP *cpi) {
2126   VP9_COMMON *cm = &cpi->common;
2127   MV_REFERENCE_FRAME ref_frame;
2128 
2129   for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
2130     const int idx = cm->ref_frame_map[get_ref_frame_idx(cpi, ref_frame)];
2131     YV12_BUFFER_CONFIG *const ref = &cm->frame_bufs[idx].buf;
2132 
2133     if (ref->y_crop_width != cm->width ||
2134         ref->y_crop_height != cm->height) {
2135       const int new_fb = get_free_fb(cm);
2136       vp9_realloc_frame_buffer(&cm->frame_bufs[new_fb].buf,
2137                                cm->width, cm->height,
2138                                cm->subsampling_x, cm->subsampling_y,
2139                                VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL);
2140       scale_and_extend_frame(ref, &cm->frame_bufs[new_fb].buf);
2141       cpi->scaled_ref_idx[ref_frame - 1] = new_fb;
2142     } else {
2143       cpi->scaled_ref_idx[ref_frame - 1] = idx;
2144       cm->frame_bufs[idx].ref_count++;
2145     }
2146   }
2147 }
2148 
2149 static void release_scaled_references(VP9_COMP *cpi) {
2150   VP9_COMMON *cm = &cpi->common;
2151   int i;
2152 
2153   for (i = 0; i < 3; i++)
2154     cm->frame_bufs[cpi->scaled_ref_idx[i]].ref_count--;
2155 }
2156 
2157 static void full_to_model_count(unsigned int *model_count,
2158                                 unsigned int *full_count) {
2159   int n;
2160   model_count[ZERO_TOKEN] = full_count[ZERO_TOKEN];
2161   model_count[ONE_TOKEN] = full_count[ONE_TOKEN];
2162   model_count[TWO_TOKEN] = full_count[TWO_TOKEN];
2163   for (n = THREE_TOKEN; n < EOB_TOKEN; ++n)
2164     model_count[TWO_TOKEN] += full_count[n];
2165   model_count[EOB_MODEL_TOKEN] = full_count[EOB_TOKEN];
2166 }
2167 
2168 static void full_to_model_counts(vp9_coeff_count_model *model_count,
2169                                  vp9_coeff_count *full_count) {
2170   int i, j, k, l;
2171 
2172   for (i = 0; i < PLANE_TYPES; ++i)
2173     for (j = 0; j < REF_TYPES; ++j)
2174       for (k = 0; k < COEF_BANDS; ++k)
2175         for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l)
2176           full_to_model_count(model_count[i][j][k][l], full_count[i][j][k][l]);
2177 }
2178 
2179 #if 0 && CONFIG_INTERNAL_STATS
2180 static void output_frame_level_debug_stats(VP9_COMP *cpi) {
2181   VP9_COMMON *const cm = &cpi->common;
2182   FILE *const f = fopen("tmp.stt", cm->current_video_frame ? "a" : "w");
2183   int recon_err;
2184 
2185   vp9_clear_system_state();
2186 
2187   recon_err = vp9_calc_ss_err(cpi->Source, get_frame_new_buffer(cm));
2188 
2189   if (cpi->twopass.total_left_stats.coded_error != 0.0)
2190     fprintf(f, "%10u %10d %10d %10d %10d %10d "
2191         "%10"PRId64" %10"PRId64" %10d "
2192         "%7.2lf %7.2lf %7.2lf %7.2lf %7.2lf"
2193         "%6d %6d %5d %5d %5d "
2194         "%10"PRId64" %10.3lf"
2195         "%10lf %8u %10d %10d %10d\n",
2196         cpi->common.current_video_frame, cpi->rc.this_frame_target,
2197         cpi->rc.projected_frame_size,
2198         cpi->rc.projected_frame_size / cpi->common.MBs,
2199         (cpi->rc.projected_frame_size - cpi->rc.this_frame_target),
2200         cpi->rc.total_target_vs_actual,
2201         (cpi->oxcf.starting_buffer_level - cpi->rc.bits_off_target),
2202         cpi->rc.total_actual_bits, cm->base_qindex,
2203         vp9_convert_qindex_to_q(cm->base_qindex),
2204         (double)vp9_dc_quant(cm->base_qindex, 0) / 4.0,
2205         cpi->rc.avg_q,
2206         vp9_convert_qindex_to_q(cpi->rc.ni_av_qi),
2207         vp9_convert_qindex_to_q(cpi->cq_target_quality),
2208         cpi->refresh_last_frame, cpi->refresh_golden_frame,
2209         cpi->refresh_alt_ref_frame, cm->frame_type, cpi->rc.gfu_boost,
2210         cpi->twopass.bits_left,
2211         cpi->twopass.total_left_stats.coded_error,
2212         cpi->twopass.bits_left /
2213             (1 + cpi->twopass.total_left_stats.coded_error),
2214         cpi->tot_recode_hits, recon_err, cpi->rc.kf_boost,
2215         cpi->twopass.kf_zeromotion_pct);
2216 
2217   fclose(f);
2218 
2219   if (0) {
2220     FILE *const fmodes = fopen("Modes.stt", "a");
2221     int i;
2222 
2223     fprintf(fmodes, "%6d:%1d:%1d:%1d ", cpi->common.current_video_frame,
2224             cm->frame_type, cpi->refresh_golden_frame,
2225             cpi->refresh_alt_ref_frame);
2226 
2227     for (i = 0; i < MAX_MODES; ++i)
2228       fprintf(fmodes, "%5d ", cpi->mode_chosen_counts[i]);
2229 
2230     fprintf(fmodes, "\n");
2231 
2232     fclose(fmodes);
2233   }
2234 }
2235 #endif
2236 
2237 static void encode_without_recode_loop(VP9_COMP *cpi,
2238                                        size_t *size,
2239                                        uint8_t *dest,
2240                                        int q) {
2241   VP9_COMMON *const cm = &cpi->common;
2242   vp9_clear_system_state();
2243   vp9_set_quantizer(cm, q);
2244 
2245   // Set up entropy context depending on frame type. The decoder mandates
2246   // the use of the default context, index 0, for keyframes and inter
2247   // frames where the error_resilient_mode or intra_only flag is set. For
2248   // other inter-frames the encoder currently uses only two contexts;
2249   // context 1 for ALTREF frames and context 0 for the others.
2250   if (cm->frame_type == KEY_FRAME) {
2251     setup_key_frame(cpi);
2252   } else {
2253     if (!cm->intra_only && !cm->error_resilient_mode && !cpi->use_svc)
2254       cm->frame_context_idx = cpi->refresh_alt_ref_frame;
2255 
2256     setup_inter_frame(cm);
2257   }
2258   // Variance adaptive and in frame q adjustment experiments are mutually
2259   // exclusive.
2260   if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
2261     vp9_vaq_frame_setup(cpi);
2262   } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
2263     vp9_setup_in_frame_q_adj(cpi);
2264   } else if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) {
2265     vp9_cyclic_refresh_setup(cpi);
2266   }
2267   // transform / motion compensation build reconstruction frame
2268   vp9_encode_frame(cpi);
2269 
2270   // Update the skip mb flag probabilities based on the distribution
2271   // seen in the last encoder iteration.
2272   // update_base_skip_probs(cpi);
2273   vp9_clear_system_state();
2274 }
2275 
2276 static void encode_with_recode_loop(VP9_COMP *cpi,
2277                                     size_t *size,
2278                                     uint8_t *dest,
2279                                     int q,
2280                                     int bottom_index,
2281                                     int top_index) {
2282   VP9_COMMON *const cm = &cpi->common;
2283   RATE_CONTROL *const rc = &cpi->rc;
2284   int loop_count = 0;
2285   int loop = 0;
2286   int overshoot_seen = 0;
2287   int undershoot_seen = 0;
2288   int q_low = bottom_index, q_high = top_index;
2289   int frame_over_shoot_limit;
2290   int frame_under_shoot_limit;
2291 
2292   // Decide frame size bounds
2293   vp9_rc_compute_frame_size_bounds(cpi, rc->this_frame_target,
2294                                    &frame_under_shoot_limit,
2295                                    &frame_over_shoot_limit);
2296 
2297   do {
2298     vp9_clear_system_state();
2299 
2300     vp9_set_quantizer(cm, q);
2301 
2302     if (loop_count == 0) {
2303       // Set up entropy context depending on frame type. The decoder mandates
2304       // the use of the default context, index 0, for keyframes and inter
2305       // frames where the error_resilient_mode or intra_only flag is set. For
2306       // other inter-frames the encoder currently uses only two contexts;
2307       // context 1 for ALTREF frames and context 0 for the others.
2308       if (cm->frame_type == KEY_FRAME) {
2309         setup_key_frame(cpi);
2310       } else {
2311         if (!cm->intra_only && !cm->error_resilient_mode && !cpi->use_svc)
2312           cpi->common.frame_context_idx = cpi->refresh_alt_ref_frame;
2313 
2314         setup_inter_frame(cm);
2315       }
2316     }
2317 
2318     // Variance adaptive and in frame q adjustment experiments are mutually
2319     // exclusive.
2320     if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
2321       vp9_vaq_frame_setup(cpi);
2322     } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
2323       vp9_setup_in_frame_q_adj(cpi);
2324     }
2325 
2326     // transform / motion compensation build reconstruction frame
2327     vp9_encode_frame(cpi);
2328 
2329     // Update the skip mb flag probabilities based on the distribution
2330     // seen in the last encoder iteration.
2331     // update_base_skip_probs(cpi);
2332 
2333     vp9_clear_system_state();
2334 
2335     // Dummy pack of the bitstream using up to date stats to get an
2336     // accurate estimate of output frame size to determine if we need
2337     // to recode.
2338     if (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF) {
2339       vp9_save_coding_context(cpi);
2340       cpi->dummy_packing = 1;
2341       if (!cpi->sf.use_nonrd_pick_mode)
2342         vp9_pack_bitstream(cpi, dest, size);
2343 
2344       rc->projected_frame_size = (int)(*size) << 3;
2345       vp9_restore_coding_context(cpi);
2346 
2347       if (frame_over_shoot_limit == 0)
2348         frame_over_shoot_limit = 1;
2349     }
2350 
2351     if (cpi->oxcf.end_usage == USAGE_CONSTANT_QUALITY) {
2352       loop = 0;
2353     } else {
2354       if ((cm->frame_type == KEY_FRAME) &&
2355            rc->this_key_frame_forced &&
2356            (rc->projected_frame_size < rc->max_frame_bandwidth)) {
2357         int last_q = q;
2358         int kf_err = vp9_calc_ss_err(cpi->Source, get_frame_new_buffer(cm));
2359 
2360         int high_err_target = cpi->ambient_err;
2361         int low_err_target = cpi->ambient_err >> 1;
2362 
2363         // Prevent possible divide by zero error below for perfect KF
2364         kf_err += !kf_err;
2365 
2366         // The key frame is not good enough or we can afford
2367         // to make it better without undue risk of popping.
2368         if ((kf_err > high_err_target &&
2369              rc->projected_frame_size <= frame_over_shoot_limit) ||
2370             (kf_err > low_err_target &&
2371              rc->projected_frame_size <= frame_under_shoot_limit)) {
2372           // Lower q_high
2373           q_high = q > q_low ? q - 1 : q_low;
2374 
2375           // Adjust Q
2376           q = (q * high_err_target) / kf_err;
2377           q = MIN(q, (q_high + q_low) >> 1);
2378         } else if (kf_err < low_err_target &&
2379                    rc->projected_frame_size >= frame_under_shoot_limit) {
2380           // The key frame is much better than the previous frame
2381           // Raise q_low
2382           q_low = q < q_high ? q + 1 : q_high;
2383 
2384           // Adjust Q
2385           q = (q * low_err_target) / kf_err;
2386           q = MIN(q, (q_high + q_low + 1) >> 1);
2387         }
2388 
2389         // Clamp Q to upper and lower limits:
2390         q = clamp(q, q_low, q_high);
2391 
2392         loop = q != last_q;
2393       } else if (recode_loop_test(
2394           cpi, frame_over_shoot_limit, frame_under_shoot_limit,
2395           q, MAX(q_high, top_index), bottom_index)) {
2396         // Is the projected frame size out of range and are we allowed
2397         // to attempt to recode.
2398         int last_q = q;
2399         int retries = 0;
2400 
2401         // Frame size out of permitted range:
2402         // Update correction factor & compute new Q to try...
2403 
2404         // Frame is too large
2405         if (rc->projected_frame_size > rc->this_frame_target) {
2406           // Special case if the projected size is > the max allowed.
2407           if (rc->projected_frame_size >= rc->max_frame_bandwidth)
2408             q_high = rc->worst_quality;
2409 
2410           // Raise Qlow as to at least the current value
2411           q_low = q < q_high ? q + 1 : q_high;
2412 
2413           if (undershoot_seen || loop_count > 1) {
2414             // Update rate_correction_factor unless
2415             vp9_rc_update_rate_correction_factors(cpi, 1);
2416 
2417             q = (q_high + q_low + 1) / 2;
2418           } else {
2419             // Update rate_correction_factor unless
2420             vp9_rc_update_rate_correction_factors(cpi, 0);
2421 
2422             q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
2423                                    bottom_index, MAX(q_high, top_index));
2424 
2425             while (q < q_low && retries < 10) {
2426               vp9_rc_update_rate_correction_factors(cpi, 0);
2427               q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
2428                                      bottom_index, MAX(q_high, top_index));
2429               retries++;
2430             }
2431           }
2432 
2433           overshoot_seen = 1;
2434         } else {
2435           // Frame is too small
2436           q_high = q > q_low ? q - 1 : q_low;
2437 
2438           if (overshoot_seen || loop_count > 1) {
2439             vp9_rc_update_rate_correction_factors(cpi, 1);
2440             q = (q_high + q_low) / 2;
2441           } else {
2442             vp9_rc_update_rate_correction_factors(cpi, 0);
2443             q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
2444                                    bottom_index, top_index);
2445             // Special case reset for qlow for constrained quality.
2446             // This should only trigger where there is very substantial
2447             // undershoot on a frame and the auto cq level is above
2448             // the user passsed in value.
2449             if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY &&
2450                 q < q_low) {
2451               q_low = q;
2452             }
2453 
2454             while (q > q_high && retries < 10) {
2455               vp9_rc_update_rate_correction_factors(cpi, 0);
2456               q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
2457                                      bottom_index, top_index);
2458               retries++;
2459             }
2460           }
2461 
2462           undershoot_seen = 1;
2463         }
2464 
2465         // Clamp Q to upper and lower limits:
2466         q = clamp(q, q_low, q_high);
2467 
2468         loop = q != last_q;
2469       } else {
2470         loop = 0;
2471       }
2472     }
2473 
2474     // Special case for overlay frame.
2475     if (rc->is_src_frame_alt_ref &&
2476         rc->projected_frame_size < rc->max_frame_bandwidth)
2477       loop = 0;
2478 
2479     if (loop) {
2480       loop_count++;
2481 
2482 #if CONFIG_INTERNAL_STATS
2483       cpi->tot_recode_hits++;
2484 #endif
2485     }
2486   } while (loop);
2487 }
2488 
2489 static void get_ref_frame_flags(VP9_COMP *cpi) {
2490   if (cpi->refresh_last_frame & cpi->refresh_golden_frame)
2491     cpi->gold_is_last = 1;
2492   else if (cpi->refresh_last_frame ^ cpi->refresh_golden_frame)
2493     cpi->gold_is_last = 0;
2494 
2495   if (cpi->refresh_last_frame & cpi->refresh_alt_ref_frame)
2496     cpi->alt_is_last = 1;
2497   else if (cpi->refresh_last_frame ^ cpi->refresh_alt_ref_frame)
2498     cpi->alt_is_last = 0;
2499 
2500   if (cpi->refresh_alt_ref_frame & cpi->refresh_golden_frame)
2501     cpi->gold_is_alt = 1;
2502   else if (cpi->refresh_alt_ref_frame ^ cpi->refresh_golden_frame)
2503     cpi->gold_is_alt = 0;
2504 
2505   cpi->ref_frame_flags = VP9_ALT_FLAG | VP9_GOLD_FLAG | VP9_LAST_FLAG;
2506 
2507   if (cpi->gold_is_last)
2508     cpi->ref_frame_flags &= ~VP9_GOLD_FLAG;
2509 
2510   if (cpi->rc.frames_till_gf_update_due == INT_MAX)
2511     cpi->ref_frame_flags &= ~VP9_GOLD_FLAG;
2512 
2513   if (cpi->alt_is_last)
2514     cpi->ref_frame_flags &= ~VP9_ALT_FLAG;
2515 
2516   if (cpi->gold_is_alt)
2517     cpi->ref_frame_flags &= ~VP9_ALT_FLAG;
2518 }
2519 
2520 static void set_ext_overrides(VP9_COMP *cpi) {
2521   // Overrides the defaults with the externally supplied values with
2522   // vp9_update_reference() and vp9_update_entropy() calls
2523   // Note: The overrides are valid only for the next frame passed
2524   // to encode_frame_to_data_rate() function
2525   if (cpi->ext_refresh_frame_context_pending) {
2526     cpi->common.refresh_frame_context = cpi->ext_refresh_frame_context;
2527     cpi->ext_refresh_frame_context_pending = 0;
2528   }
2529   if (cpi->ext_refresh_frame_flags_pending) {
2530     cpi->refresh_last_frame = cpi->ext_refresh_last_frame;
2531     cpi->refresh_golden_frame = cpi->ext_refresh_golden_frame;
2532     cpi->refresh_alt_ref_frame = cpi->ext_refresh_alt_ref_frame;
2533     cpi->ext_refresh_frame_flags_pending = 0;
2534   }
2535 }
2536 
2537 static void encode_frame_to_data_rate(VP9_COMP *cpi,
2538                                       size_t *size,
2539                                       uint8_t *dest,
2540                                       unsigned int *frame_flags) {
2541   VP9_COMMON *const cm = &cpi->common;
2542   TX_SIZE t;
2543   int q;
2544   int top_index;
2545   int bottom_index;
2546 
2547   const SPEED_FEATURES *const sf = &cpi->sf;
2548   const unsigned int max_mv_def = MIN(cm->width, cm->height);
2549   struct segmentation *const seg = &cm->seg;
2550 
2551   set_ext_overrides(cpi);
2552 
2553   /* Scale the source buffer, if required. */
2554   if (cm->mi_cols * MI_SIZE != cpi->un_scaled_source->y_width ||
2555       cm->mi_rows * MI_SIZE != cpi->un_scaled_source->y_height) {
2556     scale_and_extend_frame_nonnormative(cpi->un_scaled_source,
2557                                         &cpi->scaled_source);
2558     cpi->Source = &cpi->scaled_source;
2559   } else {
2560     cpi->Source = cpi->un_scaled_source;
2561   }
2562 
2563   // Scale the last source buffer, if required.
2564   if (cpi->unscaled_last_source != NULL) {
2565     if (cm->mi_cols * MI_SIZE != cpi->unscaled_last_source->y_width ||
2566         cm->mi_rows * MI_SIZE != cpi->unscaled_last_source->y_height) {
2567       scale_and_extend_frame_nonnormative(cpi->unscaled_last_source,
2568                                           &cpi->scaled_last_source);
2569       cpi->Last_Source = &cpi->scaled_last_source;
2570     } else {
2571       cpi->Last_Source = cpi->unscaled_last_source;
2572     }
2573   }
2574 
2575   vp9_scale_references(cpi);
2576 
2577   vp9_clear_system_state();
2578 
2579   // Enable or disable mode based tweaking of the zbin.
2580   // For 2 pass only used where GF/ARF prediction quality
2581   // is above a threshold.
2582   cpi->zbin_mode_boost = 0;
2583   cpi->zbin_mode_boost_enabled = 0;
2584 
2585   // Current default encoder behavior for the altref sign bias.
2586   cm->ref_frame_sign_bias[ALTREF_FRAME] = cpi->rc.source_alt_ref_active;
2587 
2588   // Set default state for segment based loop filter update flags.
2589   cm->lf.mode_ref_delta_update = 0;
2590 
2591   // Initialize cpi->mv_step_param to default based on max resolution.
2592   cpi->mv_step_param = vp9_init_search_range(cpi, max_mv_def);
2593   // Initialize cpi->max_mv_magnitude and cpi->mv_step_param if appropriate.
2594   if (sf->auto_mv_step_size) {
2595     if (frame_is_intra_only(cm)) {
2596       // Initialize max_mv_magnitude for use in the first INTER frame
2597       // after a key/intra-only frame.
2598       cpi->max_mv_magnitude = max_mv_def;
2599     } else {
2600       if (cm->show_frame)
2601         // Allow mv_steps to correspond to twice the max mv magnitude found
2602         // in the previous frame, capped by the default max_mv_magnitude based
2603         // on resolution.
2604         cpi->mv_step_param = vp9_init_search_range(cpi, MIN(max_mv_def, 2 *
2605                                  cpi->max_mv_magnitude));
2606       cpi->max_mv_magnitude = 0;
2607     }
2608   }
2609 
2610   // Set various flags etc to special state if it is a key frame.
2611   if (frame_is_intra_only(cm)) {
2612     setup_key_frame(cpi);
2613     // Reset the loop filter deltas and segmentation map.
2614     vp9_reset_segment_features(&cm->seg);
2615 
2616     // If segmentation is enabled force a map update for key frames.
2617     if (seg->enabled) {
2618       seg->update_map = 1;
2619       seg->update_data = 1;
2620     }
2621 
2622     // The alternate reference frame cannot be active for a key frame.
2623     cpi->rc.source_alt_ref_active = 0;
2624 
2625     cm->error_resilient_mode = (cpi->oxcf.error_resilient_mode != 0);
2626     cm->frame_parallel_decoding_mode =
2627       (cpi->oxcf.frame_parallel_decoding_mode != 0);
2628 
2629     // By default, encoder assumes decoder can use prev_mi.
2630     cm->coding_use_prev_mi = 1;
2631     if (cm->error_resilient_mode) {
2632       cm->coding_use_prev_mi = 0;
2633       cm->frame_parallel_decoding_mode = 1;
2634       cm->reset_frame_context = 0;
2635       cm->refresh_frame_context = 0;
2636     } else if (cm->intra_only) {
2637       // Only reset the current context.
2638       cm->reset_frame_context = 2;
2639     }
2640   }
2641 
2642   // Configure experimental use of segmentation for enhanced coding of
2643   // static regions if indicated.
2644   // Only allowed in second pass of two pass (as requires lagged coding)
2645   // and if the relevant speed feature flag is set.
2646   if (cpi->pass == 2 && cpi->sf.static_segmentation)
2647     configure_static_seg_features(cpi);
2648 
2649   // For 1 pass CBR, check if we are dropping this frame.
2650   // Never drop on key frame.
2651   if (cpi->pass == 0 &&
2652       cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER &&
2653       cm->frame_type != KEY_FRAME) {
2654     if (vp9_rc_drop_frame(cpi)) {
2655       vp9_rc_postencode_update_drop_frame(cpi);
2656       ++cm->current_video_frame;
2657       return;
2658     }
2659   }
2660 
2661   vp9_clear_system_state();
2662 
2663   vp9_zero(cpi->rd_tx_select_threshes);
2664 
2665 #if CONFIG_VP9_POSTPROC
2666   if (cpi->oxcf.noise_sensitivity > 0) {
2667     int l = 0;
2668     switch (cpi->oxcf.noise_sensitivity) {
2669       case 1:
2670         l = 20;
2671         break;
2672       case 2:
2673         l = 40;
2674         break;
2675       case 3:
2676         l = 60;
2677         break;
2678       case 4:
2679       case 5:
2680         l = 100;
2681         break;
2682       case 6:
2683         l = 150;
2684         break;
2685     }
2686     vp9_denoise(cpi->Source, cpi->Source, l);
2687   }
2688 #endif
2689 
2690 #ifdef OUTPUT_YUV_SRC
2691   vp9_write_yuv_frame(cpi->Source);
2692 #endif
2693 
2694   set_speed_features(cpi);
2695 
2696   // Decide q and q bounds.
2697   q = vp9_rc_pick_q_and_bounds(cpi, &bottom_index, &top_index);
2698 
2699   if (!frame_is_intra_only(cm)) {
2700     cm->interp_filter = DEFAULT_INTERP_FILTER;
2701     /* TODO: Decide this more intelligently */
2702     set_high_precision_mv(cpi, q < HIGH_PRECISION_MV_QTHRESH);
2703   }
2704 
2705   if (cpi->sf.recode_loop == DISALLOW_RECODE) {
2706     encode_without_recode_loop(cpi, size, dest, q);
2707   } else {
2708     encode_with_recode_loop(cpi, size, dest, q, bottom_index, top_index);
2709   }
2710 
2711   // Special case code to reduce pulsing when key frames are forced at a
2712   // fixed interval. Note the reconstruction error if it is the frame before
2713   // the force key frame
2714   if (cpi->rc.next_key_frame_forced && cpi->rc.frames_to_key == 1) {
2715     cpi->ambient_err = vp9_calc_ss_err(cpi->Source, get_frame_new_buffer(cm));
2716   }
2717 
2718   // If the encoder forced a KEY_FRAME decision
2719   if (cm->frame_type == KEY_FRAME)
2720     cpi->refresh_last_frame = 1;
2721 
2722   cm->frame_to_show = get_frame_new_buffer(cm);
2723 
2724 #if WRITE_RECON_BUFFER
2725   if (cm->show_frame)
2726     write_cx_frame_to_file(cm->frame_to_show,
2727                            cm->current_video_frame);
2728   else
2729     write_cx_frame_to_file(cm->frame_to_show,
2730                            cm->current_video_frame + 1000);
2731 #endif
2732 
2733   // Pick the loop filter level for the frame.
2734   loopfilter_frame(cpi, cm);
2735 
2736 #if WRITE_RECON_BUFFER
2737   if (cm->show_frame)
2738     write_cx_frame_to_file(cm->frame_to_show,
2739                            cm->current_video_frame + 2000);
2740   else
2741     write_cx_frame_to_file(cm->frame_to_show,
2742                            cm->current_video_frame + 3000);
2743 #endif
2744 
2745   // build the bitstream
2746   cpi->dummy_packing = 0;
2747   vp9_pack_bitstream(cpi, dest, size);
2748 
2749   if (cm->seg.update_map)
2750     update_reference_segmentation_map(cpi);
2751 
2752   release_scaled_references(cpi);
2753   vp9_update_reference_frames(cpi);
2754 
2755   for (t = TX_4X4; t <= TX_32X32; t++)
2756     full_to_model_counts(cm->counts.coef[t], cpi->coef_counts[t]);
2757 
2758   if (!cm->error_resilient_mode && !cm->frame_parallel_decoding_mode)
2759     vp9_adapt_coef_probs(cm);
2760 
2761   if (!frame_is_intra_only(cm)) {
2762     if (!cm->error_resilient_mode && !cm->frame_parallel_decoding_mode) {
2763       vp9_adapt_mode_probs(cm);
2764       vp9_adapt_mv_probs(cm, cm->allow_high_precision_mv);
2765     }
2766   }
2767 
2768 #if 0
2769   output_frame_level_debug_stats(cpi);
2770 #endif
2771   if (cpi->refresh_golden_frame == 1)
2772     cm->frame_flags |= FRAMEFLAGS_GOLDEN;
2773   else
2774     cm->frame_flags &= ~FRAMEFLAGS_GOLDEN;
2775 
2776   if (cpi->refresh_alt_ref_frame == 1)
2777     cm->frame_flags |= FRAMEFLAGS_ALTREF;
2778   else
2779     cm->frame_flags &= ~FRAMEFLAGS_ALTREF;
2780 
2781   get_ref_frame_flags(cpi);
2782 
2783   vp9_rc_postencode_update(cpi, *size);
2784 
2785   if (cm->frame_type == KEY_FRAME) {
2786     // Tell the caller that the frame was coded as a key frame
2787     *frame_flags = cm->frame_flags | FRAMEFLAGS_KEY;
2788 
2789 #if CONFIG_MULTIPLE_ARF
2790     // Reset the sequence number.
2791     if (cpi->multi_arf_enabled) {
2792       cpi->sequence_number = 0;
2793       cpi->frame_coding_order_period = cpi->new_frame_coding_order_period;
2794       cpi->new_frame_coding_order_period = -1;
2795     }
2796 #endif
2797   } else {
2798     *frame_flags = cm->frame_flags&~FRAMEFLAGS_KEY;
2799 
2800 #if CONFIG_MULTIPLE_ARF
2801     /* Increment position in the coded frame sequence. */
2802     if (cpi->multi_arf_enabled) {
2803       ++cpi->sequence_number;
2804       if (cpi->sequence_number >= cpi->frame_coding_order_period) {
2805         cpi->sequence_number = 0;
2806         cpi->frame_coding_order_period = cpi->new_frame_coding_order_period;
2807         cpi->new_frame_coding_order_period = -1;
2808       }
2809       cpi->this_frame_weight = cpi->arf_weight[cpi->sequence_number];
2810       assert(cpi->this_frame_weight >= 0);
2811     }
2812 #endif
2813   }
2814 
2815   // Clear the one shot update flags for segmentation map and mode/ref loop
2816   // filter deltas.
2817   cm->seg.update_map = 0;
2818   cm->seg.update_data = 0;
2819   cm->lf.mode_ref_delta_update = 0;
2820 
2821   // keep track of the last coded dimensions
2822   cm->last_width = cm->width;
2823   cm->last_height = cm->height;
2824 
2825   // reset to normal state now that we are done.
2826   if (!cm->show_existing_frame)
2827     cm->last_show_frame = cm->show_frame;
2828 
2829   if (cm->show_frame) {
2830     vp9_swap_mi_and_prev_mi(cm);
2831 
2832     // Don't increment frame counters if this was an altref buffer
2833     // update not a real frame
2834     ++cm->current_video_frame;
2835     if (cpi->use_svc) {
2836       LAYER_CONTEXT *lc;
2837       if (cpi->svc.number_temporal_layers > 1) {
2838         lc = &cpi->svc.layer_context[cpi->svc.temporal_layer_id];
2839       } else {
2840         lc = &cpi->svc.layer_context[cpi->svc.spatial_layer_id];
2841       }
2842       ++lc->current_video_frame_in_layer;
2843     }
2844   }
2845 
2846   // restore prev_mi
2847   cm->prev_mi = cm->prev_mip + cm->mi_stride + 1;
2848   cm->prev_mi_grid_visible = cm->prev_mi_grid_base + cm->mi_stride + 1;
2849 }
2850 
2851 static void SvcEncode(VP9_COMP *cpi, size_t *size, uint8_t *dest,
2852                       unsigned int *frame_flags) {
2853   vp9_rc_get_svc_params(cpi);
2854   encode_frame_to_data_rate(cpi, size, dest, frame_flags);
2855 }
2856 
2857 static void Pass0Encode(VP9_COMP *cpi, size_t *size, uint8_t *dest,
2858                         unsigned int *frame_flags) {
2859   if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
2860     vp9_rc_get_one_pass_cbr_params(cpi);
2861   } else {
2862     vp9_rc_get_one_pass_vbr_params(cpi);
2863   }
2864   encode_frame_to_data_rate(cpi, size, dest, frame_flags);
2865 }
2866 
2867 static void Pass1Encode(VP9_COMP *cpi, size_t *size, uint8_t *dest,
2868                         unsigned int *frame_flags) {
2869   (void) size;
2870   (void) dest;
2871   (void) frame_flags;
2872 
2873   vp9_rc_get_first_pass_params(cpi);
2874   vp9_set_quantizer(&cpi->common, find_fp_qindex());
2875   vp9_first_pass(cpi);
2876 }
2877 
2878 static void Pass2Encode(VP9_COMP *cpi, size_t *size,
2879                         uint8_t *dest, unsigned int *frame_flags) {
2880   cpi->allow_encode_breakout = ENCODE_BREAKOUT_ENABLED;
2881 
2882   vp9_rc_get_second_pass_params(cpi);
2883   encode_frame_to_data_rate(cpi, size, dest, frame_flags);
2884 
2885   vp9_twopass_postencode_update(cpi);
2886 }
2887 
2888 static void check_initial_width(VP9_COMP *cpi, int subsampling_x,
2889                                 int subsampling_y) {
2890   VP9_COMMON *const cm = &cpi->common;
2891 
2892   if (!cpi->initial_width) {
2893     cm->subsampling_x = subsampling_x;
2894     cm->subsampling_y = subsampling_y;
2895     alloc_raw_frame_buffers(cpi);
2896     cpi->initial_width = cm->width;
2897     cpi->initial_height = cm->height;
2898   }
2899 }
2900 
2901 
2902 int vp9_receive_raw_frame(VP9_COMP *cpi, unsigned int frame_flags,
2903                           YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
2904                           int64_t end_time) {
2905   VP9_COMMON *cm = &cpi->common;
2906   struct vpx_usec_timer timer;
2907   int res = 0;
2908   const int subsampling_x = sd->uv_width  < sd->y_width;
2909   const int subsampling_y = sd->uv_height < sd->y_height;
2910 
2911   check_initial_width(cpi, subsampling_x, subsampling_y);
2912   vpx_usec_timer_start(&timer);
2913   if (vp9_lookahead_push(cpi->lookahead,
2914                          sd, time_stamp, end_time, frame_flags))
2915     res = -1;
2916   vpx_usec_timer_mark(&timer);
2917   cpi->time_receive_data += vpx_usec_timer_elapsed(&timer);
2918 
2919   if (cm->version == 0 && (subsampling_x != 1 || subsampling_y != 1)) {
2920     vpx_internal_error(&cm->error, VPX_CODEC_INVALID_PARAM,
2921                        "Non-4:2:0 color space requires profile >= 1");
2922     res = -1;
2923   }
2924 
2925   return res;
2926 }
2927 
2928 
2929 static int frame_is_reference(const VP9_COMP *cpi) {
2930   const VP9_COMMON *cm = &cpi->common;
2931 
2932   return cm->frame_type == KEY_FRAME ||
2933          cpi->refresh_last_frame ||
2934          cpi->refresh_golden_frame ||
2935          cpi->refresh_alt_ref_frame ||
2936          cm->refresh_frame_context ||
2937          cm->lf.mode_ref_delta_update ||
2938          cm->seg.update_map ||
2939          cm->seg.update_data;
2940 }
2941 
2942 #if CONFIG_MULTIPLE_ARF
2943 int is_next_frame_arf(VP9_COMP *cpi) {
2944   // Negative entry in frame_coding_order indicates an ARF at this position.
2945   return cpi->frame_coding_order[cpi->sequence_number + 1] < 0 ? 1 : 0;
2946 }
2947 #endif
2948 
2949 void adjust_frame_rate(VP9_COMP *cpi) {
2950   int64_t this_duration;
2951   int step = 0;
2952 
2953   if (cpi->source->ts_start == cpi->first_time_stamp_ever) {
2954     this_duration = cpi->source->ts_end - cpi->source->ts_start;
2955     step = 1;
2956   } else {
2957     int64_t last_duration = cpi->last_end_time_stamp_seen
2958         - cpi->last_time_stamp_seen;
2959 
2960     this_duration = cpi->source->ts_end - cpi->last_end_time_stamp_seen;
2961 
2962     // do a step update if the duration changes by 10%
2963     if (last_duration)
2964       step = (int)((this_duration - last_duration) * 10 / last_duration);
2965   }
2966 
2967   if (this_duration) {
2968     if (step) {
2969       vp9_new_framerate(cpi, 10000000.0 / this_duration);
2970     } else {
2971       // Average this frame's rate into the last second's average
2972       // frame rate. If we haven't seen 1 second yet, then average
2973       // over the whole interval seen.
2974       const double interval = MIN((double)(cpi->source->ts_end
2975                                    - cpi->first_time_stamp_ever), 10000000.0);
2976       double avg_duration = 10000000.0 / cpi->oxcf.framerate;
2977       avg_duration *= (interval - avg_duration + this_duration);
2978       avg_duration /= interval;
2979 
2980       vp9_new_framerate(cpi, 10000000.0 / avg_duration);
2981     }
2982   }
2983   cpi->last_time_stamp_seen = cpi->source->ts_start;
2984   cpi->last_end_time_stamp_seen = cpi->source->ts_end;
2985 }
2986 
2987 int vp9_get_compressed_data(VP9_COMP *cpi, unsigned int *frame_flags,
2988                             size_t *size, uint8_t *dest,
2989                             int64_t *time_stamp, int64_t *time_end, int flush) {
2990   VP9_COMMON *const cm = &cpi->common;
2991   MACROBLOCKD *const xd = &cpi->mb.e_mbd;
2992   RATE_CONTROL *const rc = &cpi->rc;
2993   struct vpx_usec_timer  cmptimer;
2994   YV12_BUFFER_CONFIG *force_src_buffer = NULL;
2995   MV_REFERENCE_FRAME ref_frame;
2996 
2997   if (!cpi)
2998     return -1;
2999 
3000   if (cpi->svc.number_spatial_layers > 1 && cpi->pass == 2) {
3001     vp9_restore_layer_context(cpi);
3002   }
3003 
3004   vpx_usec_timer_start(&cmptimer);
3005 
3006   cpi->source = NULL;
3007   cpi->last_source = NULL;
3008 
3009   set_high_precision_mv(cpi, ALTREF_HIGH_PRECISION_MV);
3010 
3011   // Normal defaults
3012   cm->reset_frame_context = 0;
3013   cm->refresh_frame_context = 1;
3014   cpi->refresh_last_frame = 1;
3015   cpi->refresh_golden_frame = 0;
3016   cpi->refresh_alt_ref_frame = 0;
3017 
3018   // Should we code an alternate reference frame.
3019   if (cpi->oxcf.play_alternate && rc->source_alt_ref_pending) {
3020     int frames_to_arf;
3021 
3022 #if CONFIG_MULTIPLE_ARF
3023     assert(!cpi->multi_arf_enabled ||
3024            cpi->frame_coding_order[cpi->sequence_number] < 0);
3025 
3026     if (cpi->multi_arf_enabled && (cpi->pass == 2))
3027       frames_to_arf = (-cpi->frame_coding_order[cpi->sequence_number])
3028           - cpi->next_frame_in_order;
3029     else
3030 #endif
3031       frames_to_arf = rc->frames_till_gf_update_due;
3032 
3033     assert(frames_to_arf <= rc->frames_to_key);
3034 
3035     if ((cpi->source = vp9_lookahead_peek(cpi->lookahead, frames_to_arf))) {
3036 #if CONFIG_MULTIPLE_ARF
3037       cpi->alt_ref_source[cpi->arf_buffered] = cpi->source;
3038 #else
3039       cpi->alt_ref_source = cpi->source;
3040 #endif
3041 
3042       if (cpi->oxcf.arnr_max_frames > 0) {
3043         // Produce the filtered ARF frame.
3044         // TODO(agrange) merge these two functions.
3045         vp9_configure_arnr_filter(cpi, frames_to_arf, rc->gfu_boost);
3046         vp9_temporal_filter_prepare(cpi, frames_to_arf);
3047         vp9_extend_frame_borders(&cpi->alt_ref_buffer);
3048         force_src_buffer = &cpi->alt_ref_buffer;
3049       }
3050 
3051       cm->show_frame = 0;
3052       cpi->refresh_alt_ref_frame = 1;
3053       cpi->refresh_golden_frame = 0;
3054       cpi->refresh_last_frame = 0;
3055       rc->is_src_frame_alt_ref = 0;
3056 
3057 #if CONFIG_MULTIPLE_ARF
3058       if (!cpi->multi_arf_enabled)
3059 #endif
3060         rc->source_alt_ref_pending = 0;
3061     } else {
3062       rc->source_alt_ref_pending = 0;
3063     }
3064   }
3065 
3066   if (!cpi->source) {
3067 #if CONFIG_MULTIPLE_ARF
3068     int i;
3069 #endif
3070 
3071     // Get last frame source.
3072     if (cm->current_video_frame > 0) {
3073       if ((cpi->last_source = vp9_lookahead_peek(cpi->lookahead, -1)) == NULL)
3074         return -1;
3075     }
3076 
3077     if ((cpi->source = vp9_lookahead_pop(cpi->lookahead, flush))) {
3078       cm->show_frame = 1;
3079       cm->intra_only = 0;
3080 
3081 #if CONFIG_MULTIPLE_ARF
3082       // Is this frame the ARF overlay.
3083       rc->is_src_frame_alt_ref = 0;
3084       for (i = 0; i < cpi->arf_buffered; ++i) {
3085         if (cpi->source == cpi->alt_ref_source[i]) {
3086           rc->is_src_frame_alt_ref = 1;
3087           cpi->refresh_golden_frame = 1;
3088           break;
3089         }
3090       }
3091 #else
3092       rc->is_src_frame_alt_ref = cpi->alt_ref_source &&
3093                                  (cpi->source == cpi->alt_ref_source);
3094 #endif
3095       if (rc->is_src_frame_alt_ref) {
3096         // Current frame is an ARF overlay frame.
3097 #if CONFIG_MULTIPLE_ARF
3098         cpi->alt_ref_source[i] = NULL;
3099 #else
3100         cpi->alt_ref_source = NULL;
3101 #endif
3102         // Don't refresh the last buffer for an ARF overlay frame. It will
3103         // become the GF so preserve last as an alternative prediction option.
3104         cpi->refresh_last_frame = 0;
3105       }
3106 #if CONFIG_MULTIPLE_ARF
3107       ++cpi->next_frame_in_order;
3108 #endif
3109     }
3110   }
3111 
3112   if (cpi->source) {
3113     cpi->un_scaled_source = cpi->Source = force_src_buffer ? force_src_buffer
3114                                                            : &cpi->source->img;
3115 
3116   if (cpi->last_source != NULL) {
3117     cpi->unscaled_last_source = &cpi->last_source->img;
3118   } else {
3119     cpi->unscaled_last_source = NULL;
3120   }
3121 
3122     *time_stamp = cpi->source->ts_start;
3123     *time_end = cpi->source->ts_end;
3124     *frame_flags = cpi->source->flags;
3125 
3126 #if CONFIG_MULTIPLE_ARF
3127     if (cm->frame_type != KEY_FRAME && cpi->pass == 2)
3128       rc->source_alt_ref_pending = is_next_frame_arf(cpi);
3129 #endif
3130   } else {
3131     *size = 0;
3132     if (flush && cpi->pass == 1 && !cpi->twopass.first_pass_done) {
3133       vp9_end_first_pass(cpi);    /* get last stats packet */
3134       cpi->twopass.first_pass_done = 1;
3135     }
3136     return -1;
3137   }
3138 
3139   if (cpi->source->ts_start < cpi->first_time_stamp_ever) {
3140     cpi->first_time_stamp_ever = cpi->source->ts_start;
3141     cpi->last_end_time_stamp_seen = cpi->source->ts_start;
3142   }
3143 
3144   // adjust frame rates based on timestamps given
3145   if (cm->show_frame) {
3146     adjust_frame_rate(cpi);
3147   }
3148 
3149   if (cpi->svc.number_temporal_layers > 1 &&
3150       cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
3151     vp9_update_temporal_layer_framerate(cpi);
3152     vp9_restore_layer_context(cpi);
3153   }
3154 
3155   // start with a 0 size frame
3156   *size = 0;
3157 
3158   // Clear down mmx registers
3159   vp9_clear_system_state();
3160 
3161   /* find a free buffer for the new frame, releasing the reference previously
3162    * held.
3163    */
3164   cm->frame_bufs[cm->new_fb_idx].ref_count--;
3165   cm->new_fb_idx = get_free_fb(cm);
3166 
3167 #if CONFIG_MULTIPLE_ARF
3168   /* Set up the correct ARF frame. */
3169   if (cpi->refresh_alt_ref_frame) {
3170     ++cpi->arf_buffered;
3171   }
3172   if (cpi->multi_arf_enabled && (cm->frame_type != KEY_FRAME) &&
3173       (cpi->pass == 2)) {
3174     cpi->alt_fb_idx = cpi->arf_buffer_idx[cpi->sequence_number];
3175   }
3176 #endif
3177 
3178   cm->frame_flags = *frame_flags;
3179 
3180   // Reset the frame pointers to the current frame size
3181   vp9_realloc_frame_buffer(get_frame_new_buffer(cm),
3182                            cm->width, cm->height,
3183                            cm->subsampling_x, cm->subsampling_y,
3184                            VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL);
3185 
3186   for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
3187     const int idx = cm->ref_frame_map[get_ref_frame_idx(cpi, ref_frame)];
3188     YV12_BUFFER_CONFIG *const buf = &cm->frame_bufs[idx].buf;
3189     RefBuffer *const ref_buf = &cm->frame_refs[ref_frame - 1];
3190     ref_buf->buf = buf;
3191     ref_buf->idx = idx;
3192     vp9_setup_scale_factors_for_frame(&ref_buf->sf,
3193                                       buf->y_crop_width, buf->y_crop_height,
3194                                       cm->width, cm->height);
3195 
3196     if (vp9_is_scaled(&ref_buf->sf))
3197       vp9_extend_frame_borders(buf);
3198   }
3199 
3200   set_ref_ptrs(cm, xd, LAST_FRAME, LAST_FRAME);
3201 
3202   if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
3203     vp9_vaq_init();
3204   }
3205 
3206   if (cpi->pass == 1 &&
3207       (!cpi->use_svc || cpi->svc.number_temporal_layers == 1)) {
3208     Pass1Encode(cpi, size, dest, frame_flags);
3209   } else if (cpi->pass == 2 &&
3210       (!cpi->use_svc || cpi->svc.number_temporal_layers == 1)) {
3211     Pass2Encode(cpi, size, dest, frame_flags);
3212   } else if (cpi->use_svc) {
3213     SvcEncode(cpi, size, dest, frame_flags);
3214   } else {
3215     // One pass encode
3216     Pass0Encode(cpi, size, dest, frame_flags);
3217   }
3218 
3219   if (cm->refresh_frame_context)
3220     cm->frame_contexts[cm->frame_context_idx] = cm->fc;
3221 
3222   // Frame was dropped, release scaled references.
3223   if (*size == 0) {
3224     release_scaled_references(cpi);
3225   }
3226 
3227   if (*size > 0) {
3228     cpi->droppable = !frame_is_reference(cpi);
3229   }
3230 
3231   // Save layer specific state.
3232   if ((cpi->svc.number_temporal_layers > 1 &&
3233       cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) ||
3234       (cpi->svc.number_spatial_layers > 1 && cpi->pass == 2)) {
3235     vp9_save_layer_context(cpi);
3236   }
3237 
3238   vpx_usec_timer_mark(&cmptimer);
3239   cpi->time_compress_data += vpx_usec_timer_elapsed(&cmptimer);
3240 
3241   if (cpi->b_calculate_psnr && cpi->pass != 1 && cm->show_frame)
3242     generate_psnr_packet(cpi);
3243 
3244 #if CONFIG_INTERNAL_STATS
3245 
3246   if (cpi->pass != 1) {
3247     cpi->bytes += (int)(*size);
3248 
3249     if (cm->show_frame) {
3250       cpi->count++;
3251 
3252       if (cpi->b_calculate_psnr) {
3253         YV12_BUFFER_CONFIG *orig = cpi->Source;
3254         YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
3255         YV12_BUFFER_CONFIG *pp = &cm->post_proc_buffer;
3256         PSNR_STATS psnr;
3257         calc_psnr(orig, recon, &psnr);
3258 
3259         cpi->total += psnr.psnr[0];
3260         cpi->total_y += psnr.psnr[1];
3261         cpi->total_u += psnr.psnr[2];
3262         cpi->total_v += psnr.psnr[3];
3263         cpi->total_sq_error += psnr.sse[0];
3264         cpi->total_samples += psnr.samples[0];
3265 
3266         {
3267           PSNR_STATS psnr2;
3268           double frame_ssim2 = 0, weight = 0;
3269 #if CONFIG_VP9_POSTPROC
3270           vp9_deblock(cm->frame_to_show, &cm->post_proc_buffer,
3271                       cm->lf.filter_level * 10 / 6);
3272 #endif
3273           vp9_clear_system_state();
3274 
3275           calc_psnr(orig, pp, &psnr2);
3276 
3277           cpi->totalp += psnr2.psnr[0];
3278           cpi->totalp_y += psnr2.psnr[1];
3279           cpi->totalp_u += psnr2.psnr[2];
3280           cpi->totalp_v += psnr2.psnr[3];
3281           cpi->totalp_sq_error += psnr2.sse[0];
3282           cpi->totalp_samples += psnr2.samples[0];
3283 
3284           frame_ssim2 = vp9_calc_ssim(orig, recon, 1, &weight);
3285 
3286           cpi->summed_quality += frame_ssim2 * weight;
3287           cpi->summed_weights += weight;
3288 
3289           frame_ssim2 = vp9_calc_ssim(orig, &cm->post_proc_buffer, 1, &weight);
3290 
3291           cpi->summedp_quality += frame_ssim2 * weight;
3292           cpi->summedp_weights += weight;
3293 #if 0
3294           {
3295             FILE *f = fopen("q_used.stt", "a");
3296             fprintf(f, "%5d : Y%f7.3:U%f7.3:V%f7.3:F%f7.3:S%7.3f\n",
3297                     cpi->common.current_video_frame, y2, u2, v2,
3298                     frame_psnr2, frame_ssim2);
3299             fclose(f);
3300           }
3301 #endif
3302         }
3303       }
3304 
3305       if (cpi->b_calculate_ssimg) {
3306         double y, u, v, frame_all;
3307         frame_all = vp9_calc_ssimg(cpi->Source, cm->frame_to_show, &y, &u, &v);
3308         cpi->total_ssimg_y += y;
3309         cpi->total_ssimg_u += u;
3310         cpi->total_ssimg_v += v;
3311         cpi->total_ssimg_all += frame_all;
3312       }
3313     }
3314   }
3315 
3316 #endif
3317   return 0;
3318 }
3319 
3320 int vp9_get_preview_raw_frame(VP9_COMP *cpi, YV12_BUFFER_CONFIG *dest,
3321                               vp9_ppflags_t *flags) {
3322   VP9_COMMON *cm = &cpi->common;
3323 
3324   if (!cm->show_frame) {
3325     return -1;
3326   } else {
3327     int ret;
3328 #if CONFIG_VP9_POSTPROC
3329     ret = vp9_post_proc_frame(cm, dest, flags);
3330 #else
3331 
3332     if (cm->frame_to_show) {
3333       *dest = *cm->frame_to_show;
3334       dest->y_width = cm->width;
3335       dest->y_height = cm->height;
3336       dest->uv_width = cm->width >> cm->subsampling_x;
3337       dest->uv_height = cm->height >> cm->subsampling_y;
3338       ret = 0;
3339     } else {
3340       ret = -1;
3341     }
3342 
3343 #endif  // !CONFIG_VP9_POSTPROC
3344     vp9_clear_system_state();
3345     return ret;
3346   }
3347 }
3348 
3349 int vp9_set_roimap(VP9_COMP *cpi, unsigned char *map, unsigned int rows,
3350                    unsigned int cols, int delta_q[MAX_SEGMENTS],
3351                    int delta_lf[MAX_SEGMENTS],
3352                    unsigned int threshold[MAX_SEGMENTS]) {
3353   signed char feature_data[SEG_LVL_MAX][MAX_SEGMENTS];
3354   struct segmentation *seg = &cpi->common.seg;
3355   int i;
3356 
3357   if (cpi->common.mb_rows != rows || cpi->common.mb_cols != cols)
3358     return -1;
3359 
3360   if (!map) {
3361     vp9_disable_segmentation(seg);
3362     return 0;
3363   }
3364 
3365   // Set the segmentation Map
3366   vp9_set_segmentation_map(cpi, map);
3367 
3368   // Activate segmentation.
3369   vp9_enable_segmentation(seg);
3370 
3371   // Set up the quant, LF and breakout threshold segment data
3372   for (i = 0; i < MAX_SEGMENTS; i++) {
3373     feature_data[SEG_LVL_ALT_Q][i] = delta_q[i];
3374     feature_data[SEG_LVL_ALT_LF][i] = delta_lf[i];
3375     cpi->segment_encode_breakout[i] = threshold[i];
3376   }
3377 
3378   // Enable the loop and quant changes in the feature mask
3379   for (i = 0; i < MAX_SEGMENTS; i++) {
3380     if (delta_q[i])
3381       vp9_enable_segfeature(seg, i, SEG_LVL_ALT_Q);
3382     else
3383       vp9_disable_segfeature(seg, i, SEG_LVL_ALT_Q);
3384 
3385     if (delta_lf[i])
3386       vp9_enable_segfeature(seg, i, SEG_LVL_ALT_LF);
3387     else
3388       vp9_disable_segfeature(seg, i, SEG_LVL_ALT_LF);
3389   }
3390 
3391   // Initialize the feature data structure
3392   // SEGMENT_DELTADATA    0, SEGMENT_ABSDATA      1
3393   vp9_set_segment_data(seg, &feature_data[0][0], SEGMENT_DELTADATA);
3394 
3395   return 0;
3396 }
3397 
3398 int vp9_set_active_map(VP9_COMP *cpi, unsigned char *map,
3399                        unsigned int rows, unsigned int cols) {
3400   if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols) {
3401     if (map) {
3402       vpx_memcpy(cpi->active_map, map, rows * cols);
3403       cpi->active_map_enabled = 1;
3404     } else {
3405       cpi->active_map_enabled = 0;
3406     }
3407 
3408     return 0;
3409   } else {
3410     // cpi->active_map_enabled = 0;
3411     return -1;
3412   }
3413 }
3414 
3415 int vp9_set_internal_size(VP9_COMP *cpi,
3416                           VPX_SCALING horiz_mode, VPX_SCALING vert_mode) {
3417   VP9_COMMON *cm = &cpi->common;
3418   int hr = 0, hs = 0, vr = 0, vs = 0;
3419 
3420   if (horiz_mode > ONETWO || vert_mode > ONETWO)
3421     return -1;
3422 
3423   Scale2Ratio(horiz_mode, &hr, &hs);
3424   Scale2Ratio(vert_mode, &vr, &vs);
3425 
3426   // always go to the next whole number
3427   cm->width = (hs - 1 + cpi->oxcf.width * hr) / hs;
3428   cm->height = (vs - 1 + cpi->oxcf.height * vr) / vs;
3429 
3430   assert(cm->width <= cpi->initial_width);
3431   assert(cm->height <= cpi->initial_height);
3432   update_frame_size(cpi);
3433   return 0;
3434 }
3435 
3436 int vp9_set_size_literal(VP9_COMP *cpi, unsigned int width,
3437                          unsigned int height) {
3438   VP9_COMMON *cm = &cpi->common;
3439 
3440   check_initial_width(cpi, 1, 1);
3441 
3442   if (width) {
3443     cm->width = width;
3444     if (cm->width * 5 < cpi->initial_width) {
3445       cm->width = cpi->initial_width / 5 + 1;
3446       printf("Warning: Desired width too small, changed to %d\n", cm->width);
3447     }
3448     if (cm->width > cpi->initial_width) {
3449       cm->width = cpi->initial_width;
3450       printf("Warning: Desired width too large, changed to %d\n", cm->width);
3451     }
3452   }
3453 
3454   if (height) {
3455     cm->height = height;
3456     if (cm->height * 5 < cpi->initial_height) {
3457       cm->height = cpi->initial_height / 5 + 1;
3458       printf("Warning: Desired height too small, changed to %d\n", cm->height);
3459     }
3460     if (cm->height > cpi->initial_height) {
3461       cm->height = cpi->initial_height;
3462       printf("Warning: Desired height too large, changed to %d\n", cm->height);
3463     }
3464   }
3465 
3466   assert(cm->width <= cpi->initial_width);
3467   assert(cm->height <= cpi->initial_height);
3468   update_frame_size(cpi);
3469   return 0;
3470 }
3471 
3472 void vp9_set_svc(VP9_COMP *cpi, int use_svc) {
3473   cpi->use_svc = use_svc;
3474   return;
3475 }
3476 
3477 int vp9_calc_ss_err(const YV12_BUFFER_CONFIG *source,
3478                     const YV12_BUFFER_CONFIG *reference) {
3479   int i, j;
3480   int total = 0;
3481 
3482   const uint8_t *src = source->y_buffer;
3483   const uint8_t *ref = reference->y_buffer;
3484 
3485   // Loop through the Y plane raw and reconstruction data summing
3486   // (square differences)
3487   for (i = 0; i < source->y_height; i += 16) {
3488     for (j = 0; j < source->y_width; j += 16) {
3489       unsigned int sse;
3490       total += vp9_mse16x16(src + j, source->y_stride,
3491                             ref + j, reference->y_stride, &sse);
3492     }
3493 
3494     src += 16 * source->y_stride;
3495     ref += 16 * reference->y_stride;
3496   }
3497 
3498   return total;
3499 }
3500 
3501 
3502 int vp9_get_quantizer(VP9_COMP *cpi) {
3503   return cpi->common.base_qindex;
3504 }
3505