]> granicus.if.org Git - libvpx/blob - vp8/encoder/onyx_if.c
Merge "ppc: Add vpx_sadnxmx4d_vsx for n,m = {8, 16, 32 ,64}"
[libvpx] / vp8 / encoder / onyx_if.c
1 /*
2  *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10
11 #include "vpx_config.h"
12 #include "./vpx_scale_rtcd.h"
13 #include "./vpx_dsp_rtcd.h"
14 #include "./vp8_rtcd.h"
15 #include "vp8/common/onyxc_int.h"
16 #include "vp8/common/blockd.h"
17 #include "onyx_int.h"
18 #include "vp8/common/systemdependent.h"
19 #include "vp8/common/skin_detection.h"
20 #include "vp8/encoder/quantize.h"
21 #include "vp8/common/alloccommon.h"
22 #include "mcomp.h"
23 #include "firstpass.h"
24 #include "vpx_dsp/psnr.h"
25 #include "vpx_scale/vpx_scale.h"
26 #include "vp8/common/extend.h"
27 #include "ratectrl.h"
28 #include "vp8/common/quant_common.h"
29 #include "segmentation.h"
30 #if CONFIG_POSTPROC
31 #include "vp8/common/postproc.h"
32 #endif
33 #include "vpx_mem/vpx_mem.h"
34 #include "vp8/common/reconintra.h"
35 #include "vp8/common/swapyv12buffer.h"
36 #include "vp8/common/threading.h"
37 #include "vpx_ports/system_state.h"
38 #include "vpx_ports/vpx_timer.h"
39 #if ARCH_ARM
40 #include "vpx_ports/arm.h"
41 #endif
42 #if CONFIG_MULTI_RES_ENCODING
43 #include "mr_dissim.h"
44 #endif
45 #include "encodeframe.h"
46
47 #include <assert.h>
48 #include <math.h>
49 #include <stdio.h>
50 #include <limits.h>
51
52 #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
53 extern int vp8_update_coef_context(VP8_COMP *cpi);
54 extern void vp8_update_coef_probs(VP8_COMP *cpi);
55 #endif
56
57 extern void vp8cx_pick_filter_level_fast(YV12_BUFFER_CONFIG *sd, VP8_COMP *cpi);
58 extern void vp8cx_set_alt_lf_level(VP8_COMP *cpi, int filt_val);
59 extern void vp8cx_pick_filter_level(YV12_BUFFER_CONFIG *sd, VP8_COMP *cpi);
60
61 extern void vp8_deblock_frame(YV12_BUFFER_CONFIG *source,
62                               YV12_BUFFER_CONFIG *post, int filt_lvl,
63                               int low_var_thresh, int flag);
64 extern void print_parms(VP8_CONFIG *ocf, char *filenam);
65 extern unsigned int vp8_get_processor_freq();
66 extern void print_tree_update_probs();
67 extern int vp8cx_create_encoder_threads(VP8_COMP *cpi);
68 extern void vp8cx_remove_encoder_threads(VP8_COMP *cpi);
69
70 int vp8_estimate_entropy_savings(VP8_COMP *cpi);
71
72 int vp8_calc_ss_err(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *dest);
73
74 extern void vp8_temporal_filter_prepare_c(VP8_COMP *cpi, int distance);
75
76 static void set_default_lf_deltas(VP8_COMP *cpi);
77
78 extern const int vp8_gf_interval_table[101];
79
80 #if CONFIG_INTERNAL_STATS
81 #include "math.h"
82 #include "vpx_dsp/ssim.h"
83 #endif
84
85 #ifdef OUTPUT_YUV_SRC
86 FILE *yuv_file;
87 #endif
88 #ifdef OUTPUT_YUV_DENOISED
89 FILE *yuv_denoised_file;
90 #endif
91 #ifdef OUTPUT_YUV_SKINMAP
92 FILE *yuv_skinmap_file = NULL;
93 #endif
94
95 #if 0
96 FILE *framepsnr;
97 FILE *kf_list;
98 FILE *keyfile;
99 #endif
100
101 #if 0
102 extern int skip_true_count;
103 extern int skip_false_count;
104 #endif
105
106 #ifdef VP8_ENTROPY_STATS
107 extern int intra_mode_stats[10][10][10];
108 #endif
109
110 #ifdef SPEEDSTATS
111 unsigned int frames_at_speed[16] = { 0, 0, 0, 0, 0, 0, 0, 0,
112                                      0, 0, 0, 0, 0, 0, 0, 0 };
113 unsigned int tot_pm = 0;
114 unsigned int cnt_pm = 0;
115 unsigned int tot_ef = 0;
116 unsigned int cnt_ef = 0;
117 #endif
118
119 #ifdef MODE_STATS
120 extern unsigned __int64 Sectionbits[50];
121 extern int y_modes[5];
122 extern int uv_modes[4];
123 extern int b_modes[10];
124
125 extern int inter_y_modes[10];
126 extern int inter_uv_modes[4];
127 extern unsigned int inter_b_modes[15];
128 #endif
129
130 extern const int vp8_bits_per_mb[2][QINDEX_RANGE];
131
132 extern const int qrounding_factors[129];
133 extern const int qzbin_factors[129];
134 extern void vp8cx_init_quantizer(VP8_COMP *cpi);
135 extern const int vp8cx_base_skip_false_prob[128];
136
137 /* Tables relating active max Q to active min Q */
138 static const unsigned char kf_low_motion_minq[QINDEX_RANGE] = {
139   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
140   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
141   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  1,  1,  1,  1,  1,
142   1,  1,  1,  2,  2,  2,  2,  3,  3,  3,  3,  3,  3,  4,  4,  4,  5,  5,  5,
143   5,  5,  6,  6,  6,  6,  7,  7,  8,  8,  8,  8,  9,  9,  10, 10, 10, 10, 11,
144   11, 11, 11, 12, 12, 13, 13, 13, 13, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16,
145   17, 17, 18, 18, 18, 18, 19, 20, 20, 21, 21, 22, 23, 23
146 };
147 static const unsigned char kf_high_motion_minq[QINDEX_RANGE] = {
148   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
149   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  1,  1,  1,  1,  1,  1,
150   1,  1,  2,  2,  2,  2,  3,  3,  3,  3,  3,  3,  3,  3,  4,  4,  4,  4,  5,
151   5,  5,  5,  5,  5,  6,  6,  6,  6,  7,  7,  8,  8,  8,  8,  9,  9,  10, 10,
152   10, 10, 11, 11, 11, 11, 12, 12, 13, 13, 13, 13, 14, 14, 15, 15, 15, 15, 16,
153   16, 16, 16, 17, 17, 18, 18, 18, 18, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21,
154   22, 22, 23, 23, 24, 25, 25, 26, 26, 27, 28, 28, 29, 30
155 };
156 static const unsigned char gf_low_motion_minq[QINDEX_RANGE] = {
157   0,  0,  0,  0,  1,  1,  1,  1,  1,  1,  1,  1,  2,  2,  2,  2,  3,  3,  3,
158   3,  4,  4,  4,  4,  5,  5,  5,  5,  6,  6,  6,  6,  7,  7,  7,  7,  8,  8,
159   8,  8,  9,  9,  9,  9,  10, 10, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15,
160   15, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 23, 24, 24,
161   25, 25, 26, 26, 27, 27, 28, 28, 29, 29, 30, 30, 31, 31, 32, 32, 33, 33, 34,
162   34, 35, 35, 36, 36, 37, 37, 38, 38, 39, 39, 40, 40, 41, 41, 42, 42, 43, 44,
163   45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58
164 };
165 static const unsigned char gf_mid_motion_minq[QINDEX_RANGE] = {
166   0,  0,  0,  0,  1,  1,  1,  1,  1,  1,  2,  2,  3,  3,  3,  4,  4,  4,  5,
167   5,  5,  6,  6,  6,  7,  7,  7,  8,  8,  8,  9,  9,  9,  10, 10, 10, 10, 11,
168   11, 11, 12, 12, 12, 12, 13, 13, 13, 14, 14, 14, 15, 15, 16, 16, 17, 17, 18,
169   18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 23, 24, 24, 25, 25, 26, 26, 27, 27,
170   28, 28, 29, 29, 30, 30, 31, 31, 32, 32, 33, 33, 34, 34, 35, 35, 36, 36, 37,
171   37, 38, 39, 39, 40, 40, 41, 41, 42, 42, 43, 43, 44, 45, 46, 47, 48, 49, 50,
172   51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64
173 };
174 static const unsigned char gf_high_motion_minq[QINDEX_RANGE] = {
175   0,  0,  0,  0,  1,  1,  1,  1,  1,  2,  2,  2,  3,  3,  3,  4,  4,  4,  5,
176   5,  5,  6,  6,  6,  7,  7,  7,  8,  8,  8,  9,  9,  9,  10, 10, 10, 11, 11,
177   12, 12, 13, 13, 14, 14, 15, 15, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21,
178   21, 22, 22, 23, 23, 24, 24, 25, 25, 26, 26, 27, 27, 28, 28, 29, 29, 30, 30,
179   31, 31, 32, 32, 33, 33, 34, 34, 35, 35, 36, 36, 37, 37, 38, 38, 39, 39, 40,
180   40, 41, 41, 42, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56,
181   57, 58, 59, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80
182 };
183 static const unsigned char inter_minq[QINDEX_RANGE] = {
184   0,  0,  1,  1,  2,  3,  3,  4,  4,  5,  6,  6,  7,  8,  8,  9,  9,  10, 11,
185   11, 12, 13, 13, 14, 15, 15, 16, 17, 17, 18, 19, 20, 20, 21, 22, 22, 23, 24,
186   24, 25, 26, 27, 27, 28, 29, 30, 30, 31, 32, 33, 33, 34, 35, 36, 36, 37, 38,
187   39, 39, 40, 41, 42, 42, 43, 44, 45, 46, 46, 47, 48, 49, 50, 50, 51, 52, 53,
188   54, 55, 55, 56, 57, 58, 59, 60, 60, 61, 62, 63, 64, 65, 66, 67, 67, 68, 69,
189   70, 71, 72, 73, 74, 75, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 86,
190   87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100
191 };
192
193 #ifdef PACKET_TESTING
194 extern FILE *vpxlogc;
195 #endif
196
197 static void save_layer_context(VP8_COMP *cpi) {
198   LAYER_CONTEXT *lc = &cpi->layer_context[cpi->current_layer];
199
200   /* Save layer dependent coding state */
201   lc->target_bandwidth = cpi->target_bandwidth;
202   lc->starting_buffer_level = cpi->oxcf.starting_buffer_level;
203   lc->optimal_buffer_level = cpi->oxcf.optimal_buffer_level;
204   lc->maximum_buffer_size = cpi->oxcf.maximum_buffer_size;
205   lc->starting_buffer_level_in_ms = cpi->oxcf.starting_buffer_level_in_ms;
206   lc->optimal_buffer_level_in_ms = cpi->oxcf.optimal_buffer_level_in_ms;
207   lc->maximum_buffer_size_in_ms = cpi->oxcf.maximum_buffer_size_in_ms;
208   lc->buffer_level = cpi->buffer_level;
209   lc->bits_off_target = cpi->bits_off_target;
210   lc->total_actual_bits = cpi->total_actual_bits;
211   lc->worst_quality = cpi->worst_quality;
212   lc->active_worst_quality = cpi->active_worst_quality;
213   lc->best_quality = cpi->best_quality;
214   lc->active_best_quality = cpi->active_best_quality;
215   lc->ni_av_qi = cpi->ni_av_qi;
216   lc->ni_tot_qi = cpi->ni_tot_qi;
217   lc->ni_frames = cpi->ni_frames;
218   lc->avg_frame_qindex = cpi->avg_frame_qindex;
219   lc->rate_correction_factor = cpi->rate_correction_factor;
220   lc->key_frame_rate_correction_factor = cpi->key_frame_rate_correction_factor;
221   lc->gf_rate_correction_factor = cpi->gf_rate_correction_factor;
222   lc->zbin_over_quant = cpi->mb.zbin_over_quant;
223   lc->inter_frame_target = cpi->inter_frame_target;
224   lc->total_byte_count = cpi->total_byte_count;
225   lc->filter_level = cpi->common.filter_level;
226
227   lc->last_frame_percent_intra = cpi->last_frame_percent_intra;
228
229   memcpy(lc->count_mb_ref_frame_usage, cpi->mb.count_mb_ref_frame_usage,
230          sizeof(cpi->mb.count_mb_ref_frame_usage));
231 }
232
233 static void restore_layer_context(VP8_COMP *cpi, const int layer) {
234   LAYER_CONTEXT *lc = &cpi->layer_context[layer];
235
236   /* Restore layer dependent coding state */
237   cpi->current_layer = layer;
238   cpi->target_bandwidth = lc->target_bandwidth;
239   cpi->oxcf.target_bandwidth = lc->target_bandwidth;
240   cpi->oxcf.starting_buffer_level = lc->starting_buffer_level;
241   cpi->oxcf.optimal_buffer_level = lc->optimal_buffer_level;
242   cpi->oxcf.maximum_buffer_size = lc->maximum_buffer_size;
243   cpi->oxcf.starting_buffer_level_in_ms = lc->starting_buffer_level_in_ms;
244   cpi->oxcf.optimal_buffer_level_in_ms = lc->optimal_buffer_level_in_ms;
245   cpi->oxcf.maximum_buffer_size_in_ms = lc->maximum_buffer_size_in_ms;
246   cpi->buffer_level = lc->buffer_level;
247   cpi->bits_off_target = lc->bits_off_target;
248   cpi->total_actual_bits = lc->total_actual_bits;
249   cpi->active_worst_quality = lc->active_worst_quality;
250   cpi->active_best_quality = lc->active_best_quality;
251   cpi->ni_av_qi = lc->ni_av_qi;
252   cpi->ni_tot_qi = lc->ni_tot_qi;
253   cpi->ni_frames = lc->ni_frames;
254   cpi->avg_frame_qindex = lc->avg_frame_qindex;
255   cpi->rate_correction_factor = lc->rate_correction_factor;
256   cpi->key_frame_rate_correction_factor = lc->key_frame_rate_correction_factor;
257   cpi->gf_rate_correction_factor = lc->gf_rate_correction_factor;
258   cpi->mb.zbin_over_quant = lc->zbin_over_quant;
259   cpi->inter_frame_target = lc->inter_frame_target;
260   cpi->total_byte_count = lc->total_byte_count;
261   cpi->common.filter_level = lc->filter_level;
262
263   cpi->last_frame_percent_intra = lc->last_frame_percent_intra;
264
265   memcpy(cpi->mb.count_mb_ref_frame_usage, lc->count_mb_ref_frame_usage,
266          sizeof(cpi->mb.count_mb_ref_frame_usage));
267 }
268
269 static int rescale(int val, int num, int denom) {
270   int64_t llnum = num;
271   int64_t llden = denom;
272   int64_t llval = val;
273
274   return (int)(llval * llnum / llden);
275 }
276
277 static void init_temporal_layer_context(VP8_COMP *cpi, VP8_CONFIG *oxcf,
278                                         const int layer,
279                                         double prev_layer_framerate) {
280   LAYER_CONTEXT *lc = &cpi->layer_context[layer];
281
282   lc->framerate = cpi->output_framerate / cpi->oxcf.rate_decimator[layer];
283   lc->target_bandwidth = cpi->oxcf.target_bitrate[layer] * 1000;
284
285   lc->starting_buffer_level_in_ms = oxcf->starting_buffer_level;
286   lc->optimal_buffer_level_in_ms = oxcf->optimal_buffer_level;
287   lc->maximum_buffer_size_in_ms = oxcf->maximum_buffer_size;
288
289   lc->starting_buffer_level =
290       rescale((int)(oxcf->starting_buffer_level), lc->target_bandwidth, 1000);
291
292   if (oxcf->optimal_buffer_level == 0) {
293     lc->optimal_buffer_level = lc->target_bandwidth / 8;
294   } else {
295     lc->optimal_buffer_level =
296         rescale((int)(oxcf->optimal_buffer_level), lc->target_bandwidth, 1000);
297   }
298
299   if (oxcf->maximum_buffer_size == 0) {
300     lc->maximum_buffer_size = lc->target_bandwidth / 8;
301   } else {
302     lc->maximum_buffer_size =
303         rescale((int)(oxcf->maximum_buffer_size), lc->target_bandwidth, 1000);
304   }
305
306   /* Work out the average size of a frame within this layer */
307   if (layer > 0) {
308     lc->avg_frame_size_for_layer =
309         (int)((cpi->oxcf.target_bitrate[layer] -
310                cpi->oxcf.target_bitrate[layer - 1]) *
311               1000 / (lc->framerate - prev_layer_framerate));
312   }
313
314   lc->active_worst_quality = cpi->oxcf.worst_allowed_q;
315   lc->active_best_quality = cpi->oxcf.best_allowed_q;
316   lc->avg_frame_qindex = cpi->oxcf.worst_allowed_q;
317
318   lc->buffer_level = lc->starting_buffer_level;
319   lc->bits_off_target = lc->starting_buffer_level;
320
321   lc->total_actual_bits = 0;
322   lc->ni_av_qi = 0;
323   lc->ni_tot_qi = 0;
324   lc->ni_frames = 0;
325   lc->rate_correction_factor = 1.0;
326   lc->key_frame_rate_correction_factor = 1.0;
327   lc->gf_rate_correction_factor = 1.0;
328   lc->inter_frame_target = 0;
329 }
330
331 // Upon a run-time change in temporal layers, reset the layer context parameters
332 // for any "new" layers. For "existing" layers, let them inherit the parameters
333 // from the previous layer state (at the same layer #). In future we may want
334 // to better map the previous layer state(s) to the "new" ones.
335 static void reset_temporal_layer_change(VP8_COMP *cpi, VP8_CONFIG *oxcf,
336                                         const int prev_num_layers) {
337   int i;
338   double prev_layer_framerate = 0;
339   const int curr_num_layers = cpi->oxcf.number_of_layers;
340   // If the previous state was 1 layer, get current layer context from cpi.
341   // We need this to set the layer context for the new layers below.
342   if (prev_num_layers == 1) {
343     cpi->current_layer = 0;
344     save_layer_context(cpi);
345   }
346   for (i = 0; i < curr_num_layers; ++i) {
347     LAYER_CONTEXT *lc = &cpi->layer_context[i];
348     if (i >= prev_num_layers) {
349       init_temporal_layer_context(cpi, oxcf, i, prev_layer_framerate);
350     }
351     // The initial buffer levels are set based on their starting levels.
352     // We could set the buffer levels based on the previous state (normalized
353     // properly by the layer bandwidths) but we would need to keep track of
354     // the previous set of layer bandwidths (i.e., target_bitrate[i])
355     // before the layer change. For now, reset to the starting levels.
356     lc->buffer_level =
357         cpi->oxcf.starting_buffer_level_in_ms * cpi->oxcf.target_bitrate[i];
358     lc->bits_off_target = lc->buffer_level;
359     // TDOD(marpan): Should we set the rate_correction_factor and
360     // active_worst/best_quality to values derived from the previous layer
361     // state (to smooth-out quality dips/rate fluctuation at transition)?
362
363     // We need to treat the 1 layer case separately: oxcf.target_bitrate[i]
364     // is not set for 1 layer, and the restore_layer_context/save_context()
365     // are not called in the encoding loop, so we need to call it here to
366     // pass the layer context state to |cpi|.
367     if (curr_num_layers == 1) {
368       lc->target_bandwidth = cpi->oxcf.target_bandwidth;
369       lc->buffer_level =
370           cpi->oxcf.starting_buffer_level_in_ms * lc->target_bandwidth / 1000;
371       lc->bits_off_target = lc->buffer_level;
372       restore_layer_context(cpi, 0);
373     }
374     prev_layer_framerate = cpi->output_framerate / cpi->oxcf.rate_decimator[i];
375   }
376 }
377
378 static void setup_features(VP8_COMP *cpi) {
379   // If segmentation enabled set the update flags
380   if (cpi->mb.e_mbd.segmentation_enabled) {
381     cpi->mb.e_mbd.update_mb_segmentation_map = 1;
382     cpi->mb.e_mbd.update_mb_segmentation_data = 1;
383   } else {
384     cpi->mb.e_mbd.update_mb_segmentation_map = 0;
385     cpi->mb.e_mbd.update_mb_segmentation_data = 0;
386   }
387
388   cpi->mb.e_mbd.mode_ref_lf_delta_enabled = 0;
389   cpi->mb.e_mbd.mode_ref_lf_delta_update = 0;
390   memset(cpi->mb.e_mbd.ref_lf_deltas, 0, sizeof(cpi->mb.e_mbd.ref_lf_deltas));
391   memset(cpi->mb.e_mbd.mode_lf_deltas, 0, sizeof(cpi->mb.e_mbd.mode_lf_deltas));
392   memset(cpi->mb.e_mbd.last_ref_lf_deltas, 0,
393          sizeof(cpi->mb.e_mbd.ref_lf_deltas));
394   memset(cpi->mb.e_mbd.last_mode_lf_deltas, 0,
395          sizeof(cpi->mb.e_mbd.mode_lf_deltas));
396
397   set_default_lf_deltas(cpi);
398 }
399
400 static void dealloc_raw_frame_buffers(VP8_COMP *cpi);
401
402 void vp8_initialize_enc(void) {
403   static volatile int init_done = 0;
404
405   if (!init_done) {
406     vpx_dsp_rtcd();
407     vp8_init_intra_predictors();
408     init_done = 1;
409   }
410 }
411
412 static void dealloc_compressor_data(VP8_COMP *cpi) {
413   vpx_free(cpi->tplist);
414   cpi->tplist = NULL;
415
416   /* Delete last frame MV storage buffers */
417   vpx_free(cpi->lfmv);
418   cpi->lfmv = 0;
419
420   vpx_free(cpi->lf_ref_frame_sign_bias);
421   cpi->lf_ref_frame_sign_bias = 0;
422
423   vpx_free(cpi->lf_ref_frame);
424   cpi->lf_ref_frame = 0;
425
426   /* Delete sementation map */
427   vpx_free(cpi->segmentation_map);
428   cpi->segmentation_map = 0;
429
430   vpx_free(cpi->active_map);
431   cpi->active_map = 0;
432
433   vp8_de_alloc_frame_buffers(&cpi->common);
434
435   vp8_yv12_de_alloc_frame_buffer(&cpi->pick_lf_lvl_frame);
436   vp8_yv12_de_alloc_frame_buffer(&cpi->scaled_source);
437   dealloc_raw_frame_buffers(cpi);
438
439   vpx_free(cpi->tok);
440   cpi->tok = 0;
441
442   /* Structure used to monitor GF usage */
443   vpx_free(cpi->gf_active_flags);
444   cpi->gf_active_flags = 0;
445
446   /* Activity mask based per mb zbin adjustments */
447   vpx_free(cpi->mb_activity_map);
448   cpi->mb_activity_map = 0;
449
450   vpx_free(cpi->mb.pip);
451   cpi->mb.pip = 0;
452
453 #if CONFIG_MULTITHREAD
454   /* De-allocate mutex */
455   if (cpi->pmutex != NULL) {
456     VP8_COMMON *const pc = &cpi->common;
457     int i;
458
459     for (i = 0; i < pc->mb_rows; ++i) {
460       pthread_mutex_destroy(&cpi->pmutex[i]);
461     }
462     vpx_free(cpi->pmutex);
463     cpi->pmutex = NULL;
464   }
465
466   vpx_free(cpi->mt_current_mb_col);
467   cpi->mt_current_mb_col = NULL;
468 #endif
469 }
470
471 static void enable_segmentation(VP8_COMP *cpi) {
472   /* Set the appropriate feature bit */
473   cpi->mb.e_mbd.segmentation_enabled = 1;
474   cpi->mb.e_mbd.update_mb_segmentation_map = 1;
475   cpi->mb.e_mbd.update_mb_segmentation_data = 1;
476 }
477 static void disable_segmentation(VP8_COMP *cpi) {
478   /* Clear the appropriate feature bit */
479   cpi->mb.e_mbd.segmentation_enabled = 0;
480 }
481
482 /* Valid values for a segment are 0 to 3
483  * Segmentation map is arrange as [Rows][Columns]
484  */
485 static void set_segmentation_map(VP8_COMP *cpi,
486                                  unsigned char *segmentation_map) {
487   /* Copy in the new segmentation map */
488   memcpy(cpi->segmentation_map, segmentation_map,
489          (cpi->common.mb_rows * cpi->common.mb_cols));
490
491   /* Signal that the map should be updated. */
492   cpi->mb.e_mbd.update_mb_segmentation_map = 1;
493   cpi->mb.e_mbd.update_mb_segmentation_data = 1;
494 }
495
496 /* The values given for each segment can be either deltas (from the default
497  * value chosen for the frame) or absolute values.
498  *
499  * Valid range for abs values is:
500  *    (0-127 for MB_LVL_ALT_Q), (0-63 for SEGMENT_ALT_LF)
501  * Valid range for delta values are:
502  *    (+/-127 for MB_LVL_ALT_Q), (+/-63 for SEGMENT_ALT_LF)
503  *
504  * abs_delta = SEGMENT_DELTADATA (deltas)
505  * abs_delta = SEGMENT_ABSDATA (use the absolute values given).
506  *
507  */
508 static void set_segment_data(VP8_COMP *cpi, signed char *feature_data,
509                              unsigned char abs_delta) {
510   cpi->mb.e_mbd.mb_segement_abs_delta = abs_delta;
511   memcpy(cpi->segment_feature_data, feature_data,
512          sizeof(cpi->segment_feature_data));
513 }
514
515 /* A simple function to cyclically refresh the background at a lower Q */
516 static void cyclic_background_refresh(VP8_COMP *cpi, int Q, int lf_adjustment) {
517   unsigned char *seg_map = cpi->segmentation_map;
518   signed char feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS];
519   int i;
520   int block_count = cpi->cyclic_refresh_mode_max_mbs_perframe;
521   int mbs_in_frame = cpi->common.mb_rows * cpi->common.mb_cols;
522
523   cpi->cyclic_refresh_q = Q / 2;
524
525   if (cpi->oxcf.screen_content_mode) {
526     // Modify quality ramp-up based on Q. Above some Q level, increase the
527     // number of blocks to be refreshed, and reduce it below the thredhold.
528     // Turn-off under certain conditions (i.e., away from key frame, and if
529     // we are at good quality (low Q) and most of the blocks were
530     // skipped-encoded
531     // in previous frame.
532     int qp_thresh = (cpi->oxcf.screen_content_mode == 2) ? 80 : 100;
533     if (Q >= qp_thresh) {
534       cpi->cyclic_refresh_mode_max_mbs_perframe =
535           (cpi->common.mb_rows * cpi->common.mb_cols) / 10;
536     } else if (cpi->frames_since_key > 250 && Q < 20 &&
537                cpi->mb.skip_true_count > (int)(0.95 * mbs_in_frame)) {
538       cpi->cyclic_refresh_mode_max_mbs_perframe = 0;
539     } else {
540       cpi->cyclic_refresh_mode_max_mbs_perframe =
541           (cpi->common.mb_rows * cpi->common.mb_cols) / 20;
542     }
543     block_count = cpi->cyclic_refresh_mode_max_mbs_perframe;
544   }
545
546   // Set every macroblock to be eligible for update.
547   // For key frame this will reset seg map to 0.
548   memset(cpi->segmentation_map, 0, mbs_in_frame);
549
550   if (cpi->common.frame_type != KEY_FRAME && block_count > 0) {
551     /* Cycle through the macro_block rows */
552     /* MB loop to set local segmentation map */
553     i = cpi->cyclic_refresh_mode_index;
554     assert(i < mbs_in_frame);
555     do {
556       /* If the MB is as a candidate for clean up then mark it for
557        * possible boost/refresh (segment 1) The segment id may get
558        * reset to 0 later if the MB gets coded anything other than
559        * last frame 0,0 as only (last frame 0,0) MBs are eligable for
560        * refresh : that is to say Mbs likely to be background blocks.
561        */
562       if (cpi->cyclic_refresh_map[i] == 0) {
563         seg_map[i] = 1;
564         block_count--;
565       } else if (cpi->cyclic_refresh_map[i] < 0) {
566         cpi->cyclic_refresh_map[i]++;
567       }
568
569       i++;
570       if (i == mbs_in_frame) i = 0;
571
572     } while (block_count && i != cpi->cyclic_refresh_mode_index);
573
574     cpi->cyclic_refresh_mode_index = i;
575
576 #if CONFIG_TEMPORAL_DENOISING
577     if (cpi->oxcf.noise_sensitivity > 0) {
578       if (cpi->denoiser.denoiser_mode == kDenoiserOnYUVAggressive &&
579           Q < (int)cpi->denoiser.denoise_pars.qp_thresh &&
580           (cpi->frames_since_key >
581            2 * cpi->denoiser.denoise_pars.consec_zerolast)) {
582         // Under aggressive denoising, use segmentation to turn off loop
583         // filter below some qp thresh. The filter is reduced for all
584         // blocks that have been encoded as ZEROMV LAST x frames in a row,
585         // where x is set by cpi->denoiser.denoise_pars.consec_zerolast.
586         // This is to avoid "dot" artifacts that can occur from repeated
587         // loop filtering on noisy input source.
588         cpi->cyclic_refresh_q = Q;
589         // lf_adjustment = -MAX_LOOP_FILTER;
590         lf_adjustment = -40;
591         for (i = 0; i < mbs_in_frame; ++i) {
592           seg_map[i] = (cpi->consec_zero_last[i] >
593                         cpi->denoiser.denoise_pars.consec_zerolast)
594                            ? 1
595                            : 0;
596         }
597       }
598     }
599 #endif
600   }
601
602   /* Activate segmentation. */
603   cpi->mb.e_mbd.update_mb_segmentation_map = 1;
604   cpi->mb.e_mbd.update_mb_segmentation_data = 1;
605   enable_segmentation(cpi);
606
607   /* Set up the quant segment data */
608   feature_data[MB_LVL_ALT_Q][0] = 0;
609   feature_data[MB_LVL_ALT_Q][1] = (cpi->cyclic_refresh_q - Q);
610   feature_data[MB_LVL_ALT_Q][2] = 0;
611   feature_data[MB_LVL_ALT_Q][3] = 0;
612
613   /* Set up the loop segment data */
614   feature_data[MB_LVL_ALT_LF][0] = 0;
615   feature_data[MB_LVL_ALT_LF][1] = lf_adjustment;
616   feature_data[MB_LVL_ALT_LF][2] = 0;
617   feature_data[MB_LVL_ALT_LF][3] = 0;
618
619   /* Initialise the feature data structure */
620   set_segment_data(cpi, &feature_data[0][0], SEGMENT_DELTADATA);
621 }
622
623 static void set_default_lf_deltas(VP8_COMP *cpi) {
624   cpi->mb.e_mbd.mode_ref_lf_delta_enabled = 1;
625   cpi->mb.e_mbd.mode_ref_lf_delta_update = 1;
626
627   memset(cpi->mb.e_mbd.ref_lf_deltas, 0, sizeof(cpi->mb.e_mbd.ref_lf_deltas));
628   memset(cpi->mb.e_mbd.mode_lf_deltas, 0, sizeof(cpi->mb.e_mbd.mode_lf_deltas));
629
630   /* Test of ref frame deltas */
631   cpi->mb.e_mbd.ref_lf_deltas[INTRA_FRAME] = 2;
632   cpi->mb.e_mbd.ref_lf_deltas[LAST_FRAME] = 0;
633   cpi->mb.e_mbd.ref_lf_deltas[GOLDEN_FRAME] = -2;
634   cpi->mb.e_mbd.ref_lf_deltas[ALTREF_FRAME] = -2;
635
636   cpi->mb.e_mbd.mode_lf_deltas[0] = 4; /* BPRED */
637
638   if (cpi->oxcf.Mode == MODE_REALTIME) {
639     cpi->mb.e_mbd.mode_lf_deltas[1] = -12; /* Zero */
640   } else {
641     cpi->mb.e_mbd.mode_lf_deltas[1] = -2; /* Zero */
642   }
643
644   cpi->mb.e_mbd.mode_lf_deltas[2] = 2; /* New mv */
645   cpi->mb.e_mbd.mode_lf_deltas[3] = 4; /* Split mv */
646 }
647
648 /* Convenience macros for mapping speed and mode into a continuous
649  * range
650  */
651 #define GOOD(x) (x + 1)
652 #define RT(x) (x + 7)
653
654 static int speed_map(int speed, const int *map) {
655   int res;
656
657   do {
658     res = *map++;
659   } while (speed >= *map++);
660   return res;
661 }
662
663 static const int thresh_mult_map_znn[] = {
664   /* map common to zero, nearest, and near */
665   0, GOOD(2), 1500, GOOD(3), 2000, RT(0), 1000, RT(2), 2000, INT_MAX
666 };
667
668 static const int thresh_mult_map_vhpred[] = { 1000, GOOD(2), 1500,    GOOD(3),
669                                               2000, RT(0),   1000,    RT(1),
670                                               2000, RT(7),   INT_MAX, INT_MAX };
671
672 static const int thresh_mult_map_bpred[] = { 2000,    GOOD(0), 2500, GOOD(2),
673                                              5000,    GOOD(3), 7500, RT(0),
674                                              2500,    RT(1),   5000, RT(6),
675                                              INT_MAX, INT_MAX };
676
677 static const int thresh_mult_map_tm[] = { 1000,    GOOD(2), 1500, GOOD(3),
678                                           2000,    RT(0),   0,    RT(1),
679                                           1000,    RT(2),   2000, RT(7),
680                                           INT_MAX, INT_MAX };
681
682 static const int thresh_mult_map_new1[] = { 1000,  GOOD(2), 2000,
683                                             RT(0), 2000,    INT_MAX };
684
685 static const int thresh_mult_map_new2[] = { 1000, GOOD(2), 2000, GOOD(3),
686                                             2500, GOOD(5), 4000, RT(0),
687                                             2000, RT(2),   2500, RT(5),
688                                             4000, INT_MAX };
689
690 static const int thresh_mult_map_split1[] = {
691   2500,  GOOD(0), 1700,  GOOD(2), 10000, GOOD(3), 25000, GOOD(4), INT_MAX,
692   RT(0), 5000,    RT(1), 10000,   RT(2), 25000,   RT(3), INT_MAX, INT_MAX
693 };
694
695 static const int thresh_mult_map_split2[] = {
696   5000,  GOOD(0), 4500,  GOOD(2), 20000, GOOD(3), 50000, GOOD(4), INT_MAX,
697   RT(0), 10000,   RT(1), 20000,   RT(2), 50000,   RT(3), INT_MAX, INT_MAX
698 };
699
700 static const int mode_check_freq_map_zn2[] = {
701   /* {zero,nearest}{2,3} */
702   0, RT(10), 1 << 1, RT(11), 1 << 2, RT(12), 1 << 3, INT_MAX
703 };
704
705 static const int mode_check_freq_map_vhbpred[] = {
706   0, GOOD(5), 2, RT(0), 0, RT(3), 2, RT(5), 4, INT_MAX
707 };
708
709 static const int mode_check_freq_map_near2[] = {
710   0,      GOOD(5), 2,      RT(0),  0,      RT(3),  2,
711   RT(10), 1 << 2,  RT(11), 1 << 3, RT(12), 1 << 4, INT_MAX
712 };
713
714 static const int mode_check_freq_map_new1[] = {
715   0, RT(10), 1 << 1, RT(11), 1 << 2, RT(12), 1 << 3, INT_MAX
716 };
717
718 static const int mode_check_freq_map_new2[] = { 0,      GOOD(5), 4,      RT(0),
719                                                 0,      RT(3),   4,      RT(10),
720                                                 1 << 3, RT(11),  1 << 4, RT(12),
721                                                 1 << 5, INT_MAX };
722
723 static const int mode_check_freq_map_split1[] = {
724   0, GOOD(2), 2, GOOD(3), 7, RT(1), 2, RT(2), 7, INT_MAX
725 };
726
727 static const int mode_check_freq_map_split2[] = {
728   0, GOOD(1), 2, GOOD(2), 4, GOOD(3), 15, RT(1), 4, RT(2), 15, INT_MAX
729 };
730
731 void vp8_set_speed_features(VP8_COMP *cpi) {
732   SPEED_FEATURES *sf = &cpi->sf;
733   int Mode = cpi->compressor_speed;
734   int Speed = cpi->Speed;
735   int Speed2;
736   int i;
737   VP8_COMMON *cm = &cpi->common;
738   int last_improved_quant = sf->improved_quant;
739   int ref_frames;
740
741   /* Initialise default mode frequency sampling variables */
742   for (i = 0; i < MAX_MODES; ++i) {
743     cpi->mode_check_freq[i] = 0;
744   }
745
746   cpi->mb.mbs_tested_so_far = 0;
747   cpi->mb.mbs_zero_last_dot_suppress = 0;
748
749   /* best quality defaults */
750   sf->RD = 1;
751   sf->search_method = NSTEP;
752   sf->improved_quant = 1;
753   sf->improved_dct = 1;
754   sf->auto_filter = 1;
755   sf->recode_loop = 1;
756   sf->quarter_pixel_search = 1;
757   sf->half_pixel_search = 1;
758   sf->iterative_sub_pixel = 1;
759   sf->optimize_coefficients = 1;
760   sf->use_fastquant_for_pick = 0;
761   sf->no_skip_block4x4_search = 1;
762
763   sf->first_step = 0;
764   sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
765   sf->improved_mv_pred = 1;
766
767   /* default thresholds to 0 */
768   for (i = 0; i < MAX_MODES; ++i) sf->thresh_mult[i] = 0;
769
770   /* Count enabled references */
771   ref_frames = 1;
772   if (cpi->ref_frame_flags & VP8_LAST_FRAME) ref_frames++;
773   if (cpi->ref_frame_flags & VP8_GOLD_FRAME) ref_frames++;
774   if (cpi->ref_frame_flags & VP8_ALTR_FRAME) ref_frames++;
775
776   /* Convert speed to continuous range, with clamping */
777   if (Mode == 0) {
778     Speed = 0;
779   } else if (Mode == 2) {
780     Speed = RT(Speed);
781   } else {
782     if (Speed > 5) Speed = 5;
783     Speed = GOOD(Speed);
784   }
785
786   sf->thresh_mult[THR_ZERO1] = sf->thresh_mult[THR_NEAREST1] =
787       sf->thresh_mult[THR_NEAR1] = sf->thresh_mult[THR_DC] = 0; /* always */
788
789   sf->thresh_mult[THR_ZERO2] = sf->thresh_mult[THR_ZERO3] =
790       sf->thresh_mult[THR_NEAREST2] = sf->thresh_mult[THR_NEAREST3] =
791           sf->thresh_mult[THR_NEAR2] = sf->thresh_mult[THR_NEAR3] =
792               speed_map(Speed, thresh_mult_map_znn);
793
794   sf->thresh_mult[THR_V_PRED] = sf->thresh_mult[THR_H_PRED] =
795       speed_map(Speed, thresh_mult_map_vhpred);
796   sf->thresh_mult[THR_B_PRED] = speed_map(Speed, thresh_mult_map_bpred);
797   sf->thresh_mult[THR_TM] = speed_map(Speed, thresh_mult_map_tm);
798   sf->thresh_mult[THR_NEW1] = speed_map(Speed, thresh_mult_map_new1);
799   sf->thresh_mult[THR_NEW2] = sf->thresh_mult[THR_NEW3] =
800       speed_map(Speed, thresh_mult_map_new2);
801   sf->thresh_mult[THR_SPLIT1] = speed_map(Speed, thresh_mult_map_split1);
802   sf->thresh_mult[THR_SPLIT2] = sf->thresh_mult[THR_SPLIT3] =
803       speed_map(Speed, thresh_mult_map_split2);
804
805   // Special case for temporal layers.
806   // Reduce the thresholds for zero/nearest/near for GOLDEN, if GOLDEN is
807   // used as second reference. We don't modify thresholds for ALTREF case
808   // since ALTREF is usually used as long-term reference in temporal layers.
809   if ((cpi->Speed <= 6) && (cpi->oxcf.number_of_layers > 1) &&
810       (cpi->ref_frame_flags & VP8_LAST_FRAME) &&
811       (cpi->ref_frame_flags & VP8_GOLD_FRAME)) {
812     if (cpi->closest_reference_frame == GOLDEN_FRAME) {
813       sf->thresh_mult[THR_ZERO2] = sf->thresh_mult[THR_ZERO2] >> 3;
814       sf->thresh_mult[THR_NEAREST2] = sf->thresh_mult[THR_NEAREST2] >> 3;
815       sf->thresh_mult[THR_NEAR2] = sf->thresh_mult[THR_NEAR2] >> 3;
816     } else {
817       sf->thresh_mult[THR_ZERO2] = sf->thresh_mult[THR_ZERO2] >> 1;
818       sf->thresh_mult[THR_NEAREST2] = sf->thresh_mult[THR_NEAREST2] >> 1;
819       sf->thresh_mult[THR_NEAR2] = sf->thresh_mult[THR_NEAR2] >> 1;
820     }
821   }
822
823   cpi->mode_check_freq[THR_ZERO1] = cpi->mode_check_freq[THR_NEAREST1] =
824       cpi->mode_check_freq[THR_NEAR1] = cpi->mode_check_freq[THR_TM] =
825           cpi->mode_check_freq[THR_DC] = 0; /* always */
826
827   cpi->mode_check_freq[THR_ZERO2] = cpi->mode_check_freq[THR_ZERO3] =
828       cpi->mode_check_freq[THR_NEAREST2] = cpi->mode_check_freq[THR_NEAREST3] =
829           speed_map(Speed, mode_check_freq_map_zn2);
830
831   cpi->mode_check_freq[THR_NEAR2] = cpi->mode_check_freq[THR_NEAR3] =
832       speed_map(Speed, mode_check_freq_map_near2);
833
834   cpi->mode_check_freq[THR_V_PRED] = cpi->mode_check_freq[THR_H_PRED] =
835       cpi->mode_check_freq[THR_B_PRED] =
836           speed_map(Speed, mode_check_freq_map_vhbpred);
837
838   // For real-time mode at speed 10 keep the mode_check_freq threshold
839   // for NEW1 similar to that of speed 9.
840   Speed2 = Speed;
841   if (cpi->Speed == 10 && Mode == 2) Speed2 = RT(9);
842   cpi->mode_check_freq[THR_NEW1] = speed_map(Speed2, mode_check_freq_map_new1);
843
844   cpi->mode_check_freq[THR_NEW2] = cpi->mode_check_freq[THR_NEW3] =
845       speed_map(Speed, mode_check_freq_map_new2);
846
847   cpi->mode_check_freq[THR_SPLIT1] =
848       speed_map(Speed, mode_check_freq_map_split1);
849   cpi->mode_check_freq[THR_SPLIT2] = cpi->mode_check_freq[THR_SPLIT3] =
850       speed_map(Speed, mode_check_freq_map_split2);
851   Speed = cpi->Speed;
852   switch (Mode) {
853 #if !CONFIG_REALTIME_ONLY
854     case 0: /* best quality mode */
855       sf->first_step = 0;
856       sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
857       break;
858     case 1:
859     case 3:
860       if (Speed > 0) {
861         /* Disable coefficient optimization above speed 0 */
862         sf->optimize_coefficients = 0;
863         sf->use_fastquant_for_pick = 1;
864         sf->no_skip_block4x4_search = 0;
865
866         sf->first_step = 1;
867       }
868
869       if (Speed > 2) {
870         sf->improved_quant = 0;
871         sf->improved_dct = 0;
872
873         /* Only do recode loop on key frames, golden frames and
874          * alt ref frames
875          */
876         sf->recode_loop = 2;
877       }
878
879       if (Speed > 3) {
880         sf->auto_filter = 1;
881         sf->recode_loop = 0; /* recode loop off */
882         sf->RD = 0;          /* Turn rd off */
883       }
884
885       if (Speed > 4) {
886         sf->auto_filter = 0; /* Faster selection of loop filter */
887       }
888
889       break;
890 #endif
891     case 2:
892       sf->optimize_coefficients = 0;
893       sf->recode_loop = 0;
894       sf->auto_filter = 1;
895       sf->iterative_sub_pixel = 1;
896       sf->search_method = NSTEP;
897
898       if (Speed > 0) {
899         sf->improved_quant = 0;
900         sf->improved_dct = 0;
901
902         sf->use_fastquant_for_pick = 1;
903         sf->no_skip_block4x4_search = 0;
904         sf->first_step = 1;
905       }
906
907       if (Speed > 2) sf->auto_filter = 0; /* Faster selection of loop filter */
908
909       if (Speed > 3) {
910         sf->RD = 0;
911         sf->auto_filter = 1;
912       }
913
914       if (Speed > 4) {
915         sf->auto_filter = 0; /* Faster selection of loop filter */
916         sf->search_method = HEX;
917         sf->iterative_sub_pixel = 0;
918       }
919
920       if (Speed > 6) {
921         unsigned int sum = 0;
922         unsigned int total_mbs = cm->MBs;
923         int thresh;
924         unsigned int total_skip;
925
926         int min = 2000;
927
928         if (cpi->oxcf.encode_breakout > 2000) min = cpi->oxcf.encode_breakout;
929
930         min >>= 7;
931
932         for (i = 0; i < min; ++i) {
933           sum += cpi->mb.error_bins[i];
934         }
935
936         total_skip = sum;
937         sum = 0;
938
939         /* i starts from 2 to make sure thresh started from 2048 */
940         for (; i < 1024; ++i) {
941           sum += cpi->mb.error_bins[i];
942
943           if (10 * sum >=
944               (unsigned int)(cpi->Speed - 6) * (total_mbs - total_skip)) {
945             break;
946           }
947         }
948
949         i--;
950         thresh = (i << 7);
951
952         if (thresh < 2000) thresh = 2000;
953
954         if (ref_frames > 1) {
955           sf->thresh_mult[THR_NEW1] = thresh;
956           sf->thresh_mult[THR_NEAREST1] = thresh >> 1;
957           sf->thresh_mult[THR_NEAR1] = thresh >> 1;
958         }
959
960         if (ref_frames > 2) {
961           sf->thresh_mult[THR_NEW2] = thresh << 1;
962           sf->thresh_mult[THR_NEAREST2] = thresh;
963           sf->thresh_mult[THR_NEAR2] = thresh;
964         }
965
966         if (ref_frames > 3) {
967           sf->thresh_mult[THR_NEW3] = thresh << 1;
968           sf->thresh_mult[THR_NEAREST3] = thresh;
969           sf->thresh_mult[THR_NEAR3] = thresh;
970         }
971
972         sf->improved_mv_pred = 0;
973       }
974
975       if (Speed > 8) sf->quarter_pixel_search = 0;
976
977       if (cm->version == 0) {
978         cm->filter_type = NORMAL_LOOPFILTER;
979
980         if (Speed >= 14) cm->filter_type = SIMPLE_LOOPFILTER;
981       } else {
982         cm->filter_type = SIMPLE_LOOPFILTER;
983       }
984
985       /* This has a big hit on quality. Last resort */
986       if (Speed >= 15) sf->half_pixel_search = 0;
987
988       memset(cpi->mb.error_bins, 0, sizeof(cpi->mb.error_bins));
989
990   }; /* switch */
991
992   /* Slow quant, dct and trellis not worthwhile for first pass
993    * so make sure they are always turned off.
994    */
995   if (cpi->pass == 1) {
996     sf->improved_quant = 0;
997     sf->optimize_coefficients = 0;
998     sf->improved_dct = 0;
999   }
1000
1001   if (cpi->sf.search_method == NSTEP) {
1002     vp8_init3smotion_compensation(&cpi->mb,
1003                                   cm->yv12_fb[cm->lst_fb_idx].y_stride);
1004   } else if (cpi->sf.search_method == DIAMOND) {
1005     vp8_init_dsmotion_compensation(&cpi->mb,
1006                                    cm->yv12_fb[cm->lst_fb_idx].y_stride);
1007   }
1008
1009   if (cpi->sf.improved_dct) {
1010     cpi->mb.short_fdct8x4 = vp8_short_fdct8x4;
1011     cpi->mb.short_fdct4x4 = vp8_short_fdct4x4;
1012   } else {
1013     /* No fast FDCT defined for any platform at this time. */
1014     cpi->mb.short_fdct8x4 = vp8_short_fdct8x4;
1015     cpi->mb.short_fdct4x4 = vp8_short_fdct4x4;
1016   }
1017
1018   cpi->mb.short_walsh4x4 = vp8_short_walsh4x4;
1019
1020   if (cpi->sf.improved_quant) {
1021     cpi->mb.quantize_b = vp8_regular_quantize_b;
1022   } else {
1023     cpi->mb.quantize_b = vp8_fast_quantize_b;
1024   }
1025   if (cpi->sf.improved_quant != last_improved_quant) vp8cx_init_quantizer(cpi);
1026
1027   if (cpi->sf.iterative_sub_pixel == 1) {
1028     cpi->find_fractional_mv_step = vp8_find_best_sub_pixel_step_iteratively;
1029   } else if (cpi->sf.quarter_pixel_search) {
1030     cpi->find_fractional_mv_step = vp8_find_best_sub_pixel_step;
1031   } else if (cpi->sf.half_pixel_search) {
1032     cpi->find_fractional_mv_step = vp8_find_best_half_pixel_step;
1033   } else {
1034     cpi->find_fractional_mv_step = vp8_skip_fractional_mv_step;
1035   }
1036
1037   if (cpi->sf.optimize_coefficients == 1 && cpi->pass != 1) {
1038     cpi->mb.optimize = 1;
1039   } else {
1040     cpi->mb.optimize = 0;
1041   }
1042
1043   if (cpi->common.full_pixel) {
1044     cpi->find_fractional_mv_step = vp8_skip_fractional_mv_step;
1045   }
1046
1047 #ifdef SPEEDSTATS
1048   frames_at_speed[cpi->Speed]++;
1049 #endif
1050 }
1051 #undef GOOD
1052 #undef RT
1053
1054 static void alloc_raw_frame_buffers(VP8_COMP *cpi) {
1055 #if VP8_TEMPORAL_ALT_REF
1056   int width = (cpi->oxcf.Width + 15) & ~15;
1057   int height = (cpi->oxcf.Height + 15) & ~15;
1058 #endif
1059
1060   cpi->lookahead = vp8_lookahead_init(cpi->oxcf.Width, cpi->oxcf.Height,
1061                                       cpi->oxcf.lag_in_frames);
1062   if (!cpi->lookahead) {
1063     vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1064                        "Failed to allocate lag buffers");
1065   }
1066
1067 #if VP8_TEMPORAL_ALT_REF
1068
1069   if (vp8_yv12_alloc_frame_buffer(&cpi->alt_ref_buffer, width, height,
1070                                   VP8BORDERINPIXELS)) {
1071     vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1072                        "Failed to allocate altref buffer");
1073   }
1074
1075 #endif
1076 }
1077
1078 static void dealloc_raw_frame_buffers(VP8_COMP *cpi) {
1079 #if VP8_TEMPORAL_ALT_REF
1080   vp8_yv12_de_alloc_frame_buffer(&cpi->alt_ref_buffer);
1081 #endif
1082   vp8_lookahead_destroy(cpi->lookahead);
1083 }
1084
1085 static int vp8_alloc_partition_data(VP8_COMP *cpi) {
1086   vpx_free(cpi->mb.pip);
1087
1088   cpi->mb.pip =
1089       vpx_calloc((cpi->common.mb_cols + 1) * (cpi->common.mb_rows + 1),
1090                  sizeof(PARTITION_INFO));
1091   if (!cpi->mb.pip) return 1;
1092
1093   cpi->mb.pi = cpi->mb.pip + cpi->common.mode_info_stride + 1;
1094
1095   return 0;
1096 }
1097
1098 void vp8_alloc_compressor_data(VP8_COMP *cpi) {
1099   VP8_COMMON *cm = &cpi->common;
1100
1101   int width = cm->Width;
1102   int height = cm->Height;
1103 #if CONFIG_MULTITHREAD
1104   int prev_mb_rows = cm->mb_rows;
1105 #endif
1106
1107   if (vp8_alloc_frame_buffers(cm, width, height)) {
1108     vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1109                        "Failed to allocate frame buffers");
1110   }
1111
1112   if (vp8_alloc_partition_data(cpi)) {
1113     vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1114                        "Failed to allocate partition data");
1115   }
1116
1117   if ((width & 0xf) != 0) width += 16 - (width & 0xf);
1118
1119   if ((height & 0xf) != 0) height += 16 - (height & 0xf);
1120
1121   if (vp8_yv12_alloc_frame_buffer(&cpi->pick_lf_lvl_frame, width, height,
1122                                   VP8BORDERINPIXELS)) {
1123     vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1124                        "Failed to allocate last frame buffer");
1125   }
1126
1127   if (vp8_yv12_alloc_frame_buffer(&cpi->scaled_source, width, height,
1128                                   VP8BORDERINPIXELS)) {
1129     vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1130                        "Failed to allocate scaled source buffer");
1131   }
1132
1133   vpx_free(cpi->tok);
1134
1135   {
1136 #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
1137     unsigned int tokens = 8 * 24 * 16; /* one MB for each thread */
1138 #else
1139     unsigned int tokens = cm->mb_rows * cm->mb_cols * 24 * 16;
1140 #endif
1141     CHECK_MEM_ERROR(cpi->tok, vpx_calloc(tokens, sizeof(*cpi->tok)));
1142   }
1143
1144   /* Data used for real time vc mode to see if gf needs refreshing */
1145   cpi->zeromv_count = 0;
1146
1147   /* Structures used to monitor GF usage */
1148   vpx_free(cpi->gf_active_flags);
1149   CHECK_MEM_ERROR(
1150       cpi->gf_active_flags,
1151       vpx_calloc(sizeof(*cpi->gf_active_flags), cm->mb_rows * cm->mb_cols));
1152   cpi->gf_active_count = cm->mb_rows * cm->mb_cols;
1153
1154   vpx_free(cpi->mb_activity_map);
1155   CHECK_MEM_ERROR(
1156       cpi->mb_activity_map,
1157       vpx_calloc(sizeof(*cpi->mb_activity_map), cm->mb_rows * cm->mb_cols));
1158
1159   /* allocate memory for storing last frame's MVs for MV prediction. */
1160   vpx_free(cpi->lfmv);
1161   CHECK_MEM_ERROR(cpi->lfmv, vpx_calloc((cm->mb_rows + 2) * (cm->mb_cols + 2),
1162                                         sizeof(*cpi->lfmv)));
1163   vpx_free(cpi->lf_ref_frame_sign_bias);
1164   CHECK_MEM_ERROR(cpi->lf_ref_frame_sign_bias,
1165                   vpx_calloc((cm->mb_rows + 2) * (cm->mb_cols + 2),
1166                              sizeof(*cpi->lf_ref_frame_sign_bias)));
1167   vpx_free(cpi->lf_ref_frame);
1168   CHECK_MEM_ERROR(cpi->lf_ref_frame,
1169                   vpx_calloc((cm->mb_rows + 2) * (cm->mb_cols + 2),
1170                              sizeof(*cpi->lf_ref_frame)));
1171
1172   /* Create the encoder segmentation map and set all entries to 0 */
1173   vpx_free(cpi->segmentation_map);
1174   CHECK_MEM_ERROR(
1175       cpi->segmentation_map,
1176       vpx_calloc(cm->mb_rows * cm->mb_cols, sizeof(*cpi->segmentation_map)));
1177   cpi->cyclic_refresh_mode_index = 0;
1178   vpx_free(cpi->active_map);
1179   CHECK_MEM_ERROR(cpi->active_map, vpx_calloc(cm->mb_rows * cm->mb_cols,
1180                                               sizeof(*cpi->active_map)));
1181   memset(cpi->active_map, 1, (cm->mb_rows * cm->mb_cols));
1182
1183 #if CONFIG_MULTITHREAD
1184   if (width < 640) {
1185     cpi->mt_sync_range = 1;
1186   } else if (width <= 1280) {
1187     cpi->mt_sync_range = 4;
1188   } else if (width <= 2560) {
1189     cpi->mt_sync_range = 8;
1190   } else {
1191     cpi->mt_sync_range = 16;
1192   }
1193
1194   if (cpi->oxcf.multi_threaded > 1) {
1195     int i;
1196
1197     /* De-allocate and re-allocate mutex */
1198     if (cpi->pmutex != NULL) {
1199       for (i = 0; i < prev_mb_rows; ++i) {
1200         pthread_mutex_destroy(&cpi->pmutex[i]);
1201       }
1202       vpx_free(cpi->pmutex);
1203       cpi->pmutex = NULL;
1204     }
1205
1206     CHECK_MEM_ERROR(cpi->pmutex,
1207                     vpx_malloc(sizeof(*cpi->pmutex) * cm->mb_rows));
1208     if (cpi->pmutex) {
1209       for (i = 0; i < cm->mb_rows; ++i) {
1210         pthread_mutex_init(&cpi->pmutex[i], NULL);
1211       }
1212     }
1213
1214     vpx_free(cpi->mt_current_mb_col);
1215     CHECK_MEM_ERROR(cpi->mt_current_mb_col,
1216                     vpx_malloc(sizeof(*cpi->mt_current_mb_col) * cm->mb_rows));
1217   }
1218
1219 #endif
1220
1221   vpx_free(cpi->tplist);
1222   CHECK_MEM_ERROR(cpi->tplist, vpx_malloc(sizeof(TOKENLIST) * cm->mb_rows));
1223
1224 #if CONFIG_TEMPORAL_DENOISING
1225   if (cpi->oxcf.noise_sensitivity > 0) {
1226     vp8_denoiser_free(&cpi->denoiser);
1227     if (vp8_denoiser_allocate(&cpi->denoiser, width, height, cm->mb_rows,
1228                               cm->mb_cols, cpi->oxcf.noise_sensitivity)) {
1229       vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1230                          "Failed to allocate denoiser");
1231     }
1232   }
1233 #endif
1234 }
1235
1236 /* Quant MOD */
1237 static const int q_trans[] = {
1238   0,  1,  2,  3,  4,  5,  7,   8,   9,   10,  12,  13,  15,  17,  18,  19,
1239   20, 21, 23, 24, 25, 26, 27,  28,  29,  30,  31,  33,  35,  37,  39,  41,
1240   43, 45, 47, 49, 51, 53, 55,  57,  59,  61,  64,  67,  70,  73,  76,  79,
1241   82, 85, 88, 91, 94, 97, 100, 103, 106, 109, 112, 115, 118, 121, 124, 127,
1242 };
1243
1244 int vp8_reverse_trans(int x) {
1245   int i;
1246
1247   for (i = 0; i < 64; ++i) {
1248     if (q_trans[i] >= x) return i;
1249   }
1250
1251   return 63;
1252 }
1253 void vp8_new_framerate(VP8_COMP *cpi, double framerate) {
1254   if (framerate < .1) framerate = 30;
1255
1256   cpi->framerate = framerate;
1257   cpi->output_framerate = framerate;
1258   cpi->per_frame_bandwidth =
1259       (int)(cpi->oxcf.target_bandwidth / cpi->output_framerate);
1260   cpi->av_per_frame_bandwidth = cpi->per_frame_bandwidth;
1261   cpi->min_frame_bandwidth = (int)(cpi->av_per_frame_bandwidth *
1262                                    cpi->oxcf.two_pass_vbrmin_section / 100);
1263
1264   /* Set Maximum gf/arf interval */
1265   cpi->max_gf_interval = ((int)(cpi->output_framerate / 2.0) + 2);
1266
1267   if (cpi->max_gf_interval < 12) cpi->max_gf_interval = 12;
1268
1269   /* Extended interval for genuinely static scenes */
1270   cpi->twopass.static_scene_max_gf_interval = cpi->key_frame_frequency >> 1;
1271
1272   /* Special conditions when altr ref frame enabled in lagged compress mode */
1273   if (cpi->oxcf.play_alternate && cpi->oxcf.lag_in_frames) {
1274     if (cpi->max_gf_interval > cpi->oxcf.lag_in_frames - 1) {
1275       cpi->max_gf_interval = cpi->oxcf.lag_in_frames - 1;
1276     }
1277
1278     if (cpi->twopass.static_scene_max_gf_interval >
1279         cpi->oxcf.lag_in_frames - 1) {
1280       cpi->twopass.static_scene_max_gf_interval = cpi->oxcf.lag_in_frames - 1;
1281     }
1282   }
1283
1284   if (cpi->max_gf_interval > cpi->twopass.static_scene_max_gf_interval) {
1285     cpi->max_gf_interval = cpi->twopass.static_scene_max_gf_interval;
1286   }
1287 }
1288
1289 static void init_config(VP8_COMP *cpi, VP8_CONFIG *oxcf) {
1290   VP8_COMMON *cm = &cpi->common;
1291
1292   cpi->oxcf = *oxcf;
1293
1294   cpi->auto_gold = 1;
1295   cpi->auto_adjust_gold_quantizer = 1;
1296
1297   cm->version = oxcf->Version;
1298   vp8_setup_version(cm);
1299
1300   /* Frame rate is not available on the first frame, as it's derived from
1301    * the observed timestamps. The actual value used here doesn't matter
1302    * too much, as it will adapt quickly.
1303    */
1304   if (oxcf->timebase.num > 0) {
1305     cpi->framerate =
1306         (double)(oxcf->timebase.den) / (double)(oxcf->timebase.num);
1307   } else {
1308     cpi->framerate = 30;
1309   }
1310
1311   /* If the reciprocal of the timebase seems like a reasonable framerate,
1312    * then use that as a guess, otherwise use 30.
1313    */
1314   if (cpi->framerate > 180) cpi->framerate = 30;
1315
1316   cpi->ref_framerate = cpi->framerate;
1317
1318   cpi->ref_frame_flags = VP8_ALTR_FRAME | VP8_GOLD_FRAME | VP8_LAST_FRAME;
1319
1320   cm->refresh_golden_frame = 0;
1321   cm->refresh_last_frame = 1;
1322   cm->refresh_entropy_probs = 1;
1323
1324   /* change includes all joint functionality */
1325   vp8_change_config(cpi, oxcf);
1326
1327   /* Initialize active best and worst q and average q values. */
1328   cpi->active_worst_quality = cpi->oxcf.worst_allowed_q;
1329   cpi->active_best_quality = cpi->oxcf.best_allowed_q;
1330   cpi->avg_frame_qindex = cpi->oxcf.worst_allowed_q;
1331
1332   /* Initialise the starting buffer levels */
1333   cpi->buffer_level = cpi->oxcf.starting_buffer_level;
1334   cpi->bits_off_target = cpi->oxcf.starting_buffer_level;
1335
1336   cpi->rolling_target_bits = cpi->av_per_frame_bandwidth;
1337   cpi->rolling_actual_bits = cpi->av_per_frame_bandwidth;
1338   cpi->long_rolling_target_bits = cpi->av_per_frame_bandwidth;
1339   cpi->long_rolling_actual_bits = cpi->av_per_frame_bandwidth;
1340
1341   cpi->total_actual_bits = 0;
1342   cpi->total_target_vs_actual = 0;
1343
1344   /* Temporal scalabilty */
1345   if (cpi->oxcf.number_of_layers > 1) {
1346     unsigned int i;
1347     double prev_layer_framerate = 0;
1348
1349     for (i = 0; i < cpi->oxcf.number_of_layers; ++i) {
1350       init_temporal_layer_context(cpi, oxcf, i, prev_layer_framerate);
1351       prev_layer_framerate =
1352           cpi->output_framerate / cpi->oxcf.rate_decimator[i];
1353     }
1354   }
1355
1356 #if VP8_TEMPORAL_ALT_REF
1357   {
1358     int i;
1359
1360     cpi->fixed_divide[0] = 0;
1361
1362     for (i = 1; i < 512; ++i) cpi->fixed_divide[i] = 0x80000 / i;
1363   }
1364 #endif
1365 }
1366
1367 static void update_layer_contexts(VP8_COMP *cpi) {
1368   VP8_CONFIG *oxcf = &cpi->oxcf;
1369
1370   /* Update snapshots of the layer contexts to reflect new parameters */
1371   if (oxcf->number_of_layers > 1) {
1372     unsigned int i;
1373     double prev_layer_framerate = 0;
1374
1375     assert(oxcf->number_of_layers <= VPX_TS_MAX_LAYERS);
1376     for (i = 0; i < oxcf->number_of_layers && i < VPX_TS_MAX_LAYERS; ++i) {
1377       LAYER_CONTEXT *lc = &cpi->layer_context[i];
1378
1379       lc->framerate = cpi->ref_framerate / oxcf->rate_decimator[i];
1380       lc->target_bandwidth = oxcf->target_bitrate[i] * 1000;
1381
1382       lc->starting_buffer_level = rescale(
1383           (int)oxcf->starting_buffer_level_in_ms, lc->target_bandwidth, 1000);
1384
1385       if (oxcf->optimal_buffer_level == 0) {
1386         lc->optimal_buffer_level = lc->target_bandwidth / 8;
1387       } else {
1388         lc->optimal_buffer_level = rescale(
1389             (int)oxcf->optimal_buffer_level_in_ms, lc->target_bandwidth, 1000);
1390       }
1391
1392       if (oxcf->maximum_buffer_size == 0) {
1393         lc->maximum_buffer_size = lc->target_bandwidth / 8;
1394       } else {
1395         lc->maximum_buffer_size = rescale((int)oxcf->maximum_buffer_size_in_ms,
1396                                           lc->target_bandwidth, 1000);
1397       }
1398
1399       /* Work out the average size of a frame within this layer */
1400       if (i > 0) {
1401         lc->avg_frame_size_for_layer =
1402             (int)((oxcf->target_bitrate[i] - oxcf->target_bitrate[i - 1]) *
1403                   1000 / (lc->framerate - prev_layer_framerate));
1404       }
1405
1406       prev_layer_framerate = lc->framerate;
1407     }
1408   }
1409 }
1410
1411 void vp8_change_config(VP8_COMP *cpi, VP8_CONFIG *oxcf) {
1412   VP8_COMMON *cm = &cpi->common;
1413   int last_w, last_h;
1414   unsigned int prev_number_of_layers;
1415
1416   if (!cpi) return;
1417
1418   if (!oxcf) return;
1419
1420   if (cm->version != oxcf->Version) {
1421     cm->version = oxcf->Version;
1422     vp8_setup_version(cm);
1423   }
1424
1425   last_w = cpi->oxcf.Width;
1426   last_h = cpi->oxcf.Height;
1427   prev_number_of_layers = cpi->oxcf.number_of_layers;
1428
1429   cpi->oxcf = *oxcf;
1430
1431   switch (cpi->oxcf.Mode) {
1432     case MODE_REALTIME:
1433       cpi->pass = 0;
1434       cpi->compressor_speed = 2;
1435
1436       if (cpi->oxcf.cpu_used < -16) {
1437         cpi->oxcf.cpu_used = -16;
1438       }
1439
1440       if (cpi->oxcf.cpu_used > 16) cpi->oxcf.cpu_used = 16;
1441
1442       break;
1443
1444     case MODE_GOODQUALITY:
1445       cpi->pass = 0;
1446       cpi->compressor_speed = 1;
1447
1448       if (cpi->oxcf.cpu_used < -5) {
1449         cpi->oxcf.cpu_used = -5;
1450       }
1451
1452       if (cpi->oxcf.cpu_used > 5) cpi->oxcf.cpu_used = 5;
1453
1454       break;
1455
1456     case MODE_BESTQUALITY:
1457       cpi->pass = 0;
1458       cpi->compressor_speed = 0;
1459       break;
1460
1461     case MODE_FIRSTPASS:
1462       cpi->pass = 1;
1463       cpi->compressor_speed = 1;
1464       break;
1465     case MODE_SECONDPASS:
1466       cpi->pass = 2;
1467       cpi->compressor_speed = 1;
1468
1469       if (cpi->oxcf.cpu_used < -5) {
1470         cpi->oxcf.cpu_used = -5;
1471       }
1472
1473       if (cpi->oxcf.cpu_used > 5) cpi->oxcf.cpu_used = 5;
1474
1475       break;
1476     case MODE_SECONDPASS_BEST:
1477       cpi->pass = 2;
1478       cpi->compressor_speed = 0;
1479       break;
1480   }
1481
1482   if (cpi->pass == 0) cpi->auto_worst_q = 1;
1483
1484   cpi->oxcf.worst_allowed_q = q_trans[oxcf->worst_allowed_q];
1485   cpi->oxcf.best_allowed_q = q_trans[oxcf->best_allowed_q];
1486   cpi->oxcf.cq_level = q_trans[cpi->oxcf.cq_level];
1487
1488   if (oxcf->fixed_q >= 0) {
1489     if (oxcf->worst_allowed_q < 0) {
1490       cpi->oxcf.fixed_q = q_trans[0];
1491     } else {
1492       cpi->oxcf.fixed_q = q_trans[oxcf->worst_allowed_q];
1493     }
1494
1495     if (oxcf->alt_q < 0) {
1496       cpi->oxcf.alt_q = q_trans[0];
1497     } else {
1498       cpi->oxcf.alt_q = q_trans[oxcf->alt_q];
1499     }
1500
1501     if (oxcf->key_q < 0) {
1502       cpi->oxcf.key_q = q_trans[0];
1503     } else {
1504       cpi->oxcf.key_q = q_trans[oxcf->key_q];
1505     }
1506
1507     if (oxcf->gold_q < 0) {
1508       cpi->oxcf.gold_q = q_trans[0];
1509     } else {
1510       cpi->oxcf.gold_q = q_trans[oxcf->gold_q];
1511     }
1512   }
1513
1514   cpi->baseline_gf_interval =
1515       cpi->oxcf.alt_freq ? cpi->oxcf.alt_freq : DEFAULT_GF_INTERVAL;
1516
1517   // GF behavior for 1 pass CBR, used when error_resilience is off.
1518   if (!cpi->oxcf.error_resilient_mode &&
1519       cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER &&
1520       cpi->oxcf.Mode == MODE_REALTIME)
1521     cpi->baseline_gf_interval = cpi->gf_interval_onepass_cbr;
1522
1523 #if (CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
1524   cpi->oxcf.token_partitions = 3;
1525 #endif
1526
1527   if (cpi->oxcf.token_partitions >= 0 && cpi->oxcf.token_partitions <= 3) {
1528     cm->multi_token_partition = (TOKEN_PARTITION)cpi->oxcf.token_partitions;
1529   }
1530
1531   setup_features(cpi);
1532
1533   {
1534     int i;
1535
1536     for (i = 0; i < MAX_MB_SEGMENTS; ++i) {
1537       cpi->segment_encode_breakout[i] = cpi->oxcf.encode_breakout;
1538     }
1539   }
1540
1541   /* At the moment the first order values may not be > MAXQ */
1542   if (cpi->oxcf.fixed_q > MAXQ) cpi->oxcf.fixed_q = MAXQ;
1543
1544   /* local file playback mode == really big buffer */
1545   if (cpi->oxcf.end_usage == USAGE_LOCAL_FILE_PLAYBACK) {
1546     cpi->oxcf.starting_buffer_level = 60000;
1547     cpi->oxcf.optimal_buffer_level = 60000;
1548     cpi->oxcf.maximum_buffer_size = 240000;
1549     cpi->oxcf.starting_buffer_level_in_ms = 60000;
1550     cpi->oxcf.optimal_buffer_level_in_ms = 60000;
1551     cpi->oxcf.maximum_buffer_size_in_ms = 240000;
1552   }
1553
1554   /* Convert target bandwidth from Kbit/s to Bit/s */
1555   cpi->oxcf.target_bandwidth *= 1000;
1556
1557   cpi->oxcf.starting_buffer_level = rescale(
1558       (int)cpi->oxcf.starting_buffer_level, cpi->oxcf.target_bandwidth, 1000);
1559
1560   /* Set or reset optimal and maximum buffer levels. */
1561   if (cpi->oxcf.optimal_buffer_level == 0) {
1562     cpi->oxcf.optimal_buffer_level = cpi->oxcf.target_bandwidth / 8;
1563   } else {
1564     cpi->oxcf.optimal_buffer_level = rescale(
1565         (int)cpi->oxcf.optimal_buffer_level, cpi->oxcf.target_bandwidth, 1000);
1566   }
1567
1568   if (cpi->oxcf.maximum_buffer_size == 0) {
1569     cpi->oxcf.maximum_buffer_size = cpi->oxcf.target_bandwidth / 8;
1570   } else {
1571     cpi->oxcf.maximum_buffer_size = rescale((int)cpi->oxcf.maximum_buffer_size,
1572                                             cpi->oxcf.target_bandwidth, 1000);
1573   }
1574   // Under a configuration change, where maximum_buffer_size may change,
1575   // keep buffer level clipped to the maximum allowed buffer size.
1576   if (cpi->bits_off_target > cpi->oxcf.maximum_buffer_size) {
1577     cpi->bits_off_target = cpi->oxcf.maximum_buffer_size;
1578     cpi->buffer_level = cpi->bits_off_target;
1579   }
1580
1581   /* Set up frame rate and related parameters rate control values. */
1582   vp8_new_framerate(cpi, cpi->framerate);
1583
1584   /* Set absolute upper and lower quality limits */
1585   cpi->worst_quality = cpi->oxcf.worst_allowed_q;
1586   cpi->best_quality = cpi->oxcf.best_allowed_q;
1587
1588   /* active values should only be modified if out of new range */
1589   if (cpi->active_worst_quality > cpi->oxcf.worst_allowed_q) {
1590     cpi->active_worst_quality = cpi->oxcf.worst_allowed_q;
1591   }
1592   /* less likely */
1593   else if (cpi->active_worst_quality < cpi->oxcf.best_allowed_q) {
1594     cpi->active_worst_quality = cpi->oxcf.best_allowed_q;
1595   }
1596   if (cpi->active_best_quality < cpi->oxcf.best_allowed_q) {
1597     cpi->active_best_quality = cpi->oxcf.best_allowed_q;
1598   }
1599   /* less likely */
1600   else if (cpi->active_best_quality > cpi->oxcf.worst_allowed_q) {
1601     cpi->active_best_quality = cpi->oxcf.worst_allowed_q;
1602   }
1603
1604   cpi->buffered_mode = cpi->oxcf.optimal_buffer_level > 0;
1605
1606   cpi->cq_target_quality = cpi->oxcf.cq_level;
1607
1608   /* Only allow dropped frames in buffered mode */
1609   cpi->drop_frames_allowed = cpi->oxcf.allow_df && cpi->buffered_mode;
1610
1611   cpi->target_bandwidth = cpi->oxcf.target_bandwidth;
1612
1613   // Check if the number of temporal layers has changed, and if so reset the
1614   // pattern counter and set/initialize the temporal layer context for the
1615   // new layer configuration.
1616   if (cpi->oxcf.number_of_layers != prev_number_of_layers) {
1617     // If the number of temporal layers are changed we must start at the
1618     // base of the pattern cycle, so set the layer id to 0 and reset
1619     // the temporal pattern counter.
1620     if (cpi->temporal_layer_id > 0) {
1621       cpi->temporal_layer_id = 0;
1622     }
1623     cpi->temporal_pattern_counter = 0;
1624     reset_temporal_layer_change(cpi, oxcf, prev_number_of_layers);
1625   }
1626
1627   if (!cpi->initial_width) {
1628     cpi->initial_width = cpi->oxcf.Width;
1629     cpi->initial_height = cpi->oxcf.Height;
1630   }
1631
1632   cm->Width = cpi->oxcf.Width;
1633   cm->Height = cpi->oxcf.Height;
1634   assert(cm->Width <= cpi->initial_width);
1635   assert(cm->Height <= cpi->initial_height);
1636
1637   /* TODO(jkoleszar): if an internal spatial resampling is active,
1638    * and we downsize the input image, maybe we should clear the
1639    * internal scale immediately rather than waiting for it to
1640    * correct.
1641    */
1642
1643   /* VP8 sharpness level mapping 0-7 (vs 0-10 in general VPx dialogs) */
1644   if (cpi->oxcf.Sharpness > 7) cpi->oxcf.Sharpness = 7;
1645
1646   cm->sharpness_level = cpi->oxcf.Sharpness;
1647
1648   if (cm->horiz_scale != NORMAL || cm->vert_scale != NORMAL) {
1649     int hr, hs, vr, vs;
1650
1651     Scale2Ratio(cm->horiz_scale, &hr, &hs);
1652     Scale2Ratio(cm->vert_scale, &vr, &vs);
1653
1654     /* always go to the next whole number */
1655     cm->Width = (hs - 1 + cpi->oxcf.Width * hr) / hs;
1656     cm->Height = (vs - 1 + cpi->oxcf.Height * vr) / vs;
1657   }
1658
1659   if (last_w != cpi->oxcf.Width || last_h != cpi->oxcf.Height) {
1660     cpi->force_next_frame_intra = 1;
1661   }
1662
1663   if (((cm->Width + 15) & ~15) != cm->yv12_fb[cm->lst_fb_idx].y_width ||
1664       ((cm->Height + 15) & ~15) != cm->yv12_fb[cm->lst_fb_idx].y_height ||
1665       cm->yv12_fb[cm->lst_fb_idx].y_width == 0) {
1666     dealloc_raw_frame_buffers(cpi);
1667     alloc_raw_frame_buffers(cpi);
1668     vp8_alloc_compressor_data(cpi);
1669   }
1670
1671   if (cpi->oxcf.fixed_q >= 0) {
1672     cpi->last_q[0] = cpi->oxcf.fixed_q;
1673     cpi->last_q[1] = cpi->oxcf.fixed_q;
1674   }
1675
1676   cpi->Speed = cpi->oxcf.cpu_used;
1677
1678   /* force to allowlag to 0 if lag_in_frames is 0; */
1679   if (cpi->oxcf.lag_in_frames == 0) {
1680     cpi->oxcf.allow_lag = 0;
1681   }
1682   /* Limit on lag buffers as these are not currently dynamically allocated */
1683   else if (cpi->oxcf.lag_in_frames > MAX_LAG_BUFFERS) {
1684     cpi->oxcf.lag_in_frames = MAX_LAG_BUFFERS;
1685   }
1686
1687   /* YX Temp */
1688   cpi->alt_ref_source = NULL;
1689   cpi->is_src_frame_alt_ref = 0;
1690
1691 #if CONFIG_TEMPORAL_DENOISING
1692   if (cpi->oxcf.noise_sensitivity) {
1693     if (!cpi->denoiser.yv12_mc_running_avg.buffer_alloc) {
1694       int width = (cpi->oxcf.Width + 15) & ~15;
1695       int height = (cpi->oxcf.Height + 15) & ~15;
1696       if (vp8_denoiser_allocate(&cpi->denoiser, width, height, cm->mb_rows,
1697                                 cm->mb_cols, cpi->oxcf.noise_sensitivity)) {
1698         vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1699                            "Failed to allocate denoiser");
1700       }
1701     }
1702   }
1703 #endif
1704
1705 #if 0
1706     /* Experimental RD Code */
1707     cpi->frame_distortion = 0;
1708     cpi->last_frame_distortion = 0;
1709 #endif
1710 }
1711
1712 #ifndef M_LOG2_E
1713 #define M_LOG2_E 0.693147180559945309417
1714 #endif
1715 #define log2f(x) (log(x) / (float)M_LOG2_E)
1716
1717 static void cal_mvsadcosts(int *mvsadcost[2]) {
1718   int i = 1;
1719
1720   mvsadcost[0][0] = 300;
1721   mvsadcost[1][0] = 300;
1722
1723   do {
1724     double z = 256 * (2 * (log2f(8 * i) + .6));
1725     mvsadcost[0][i] = (int)z;
1726     mvsadcost[1][i] = (int)z;
1727     mvsadcost[0][-i] = (int)z;
1728     mvsadcost[1][-i] = (int)z;
1729   } while (++i <= mvfp_max);
1730 }
1731
1732 struct VP8_COMP *vp8_create_compressor(VP8_CONFIG *oxcf) {
1733   int i;
1734
1735   VP8_COMP *cpi;
1736   VP8_COMMON *cm;
1737
1738   cpi = vpx_memalign(32, sizeof(VP8_COMP));
1739   /* Check that the CPI instance is valid */
1740   if (!cpi) return 0;
1741
1742   cm = &cpi->common;
1743
1744   memset(cpi, 0, sizeof(VP8_COMP));
1745
1746   if (setjmp(cm->error.jmp)) {
1747     cpi->common.error.setjmp = 0;
1748     vp8_remove_compressor(&cpi);
1749     return 0;
1750   }
1751
1752   cpi->common.error.setjmp = 1;
1753
1754   CHECK_MEM_ERROR(cpi->mb.ss, vpx_calloc(sizeof(search_site),
1755                                          (MAX_MVSEARCH_STEPS * 8) + 1));
1756
1757   vp8_create_common(&cpi->common);
1758
1759   init_config(cpi, oxcf);
1760
1761   memcpy(cpi->base_skip_false_prob, vp8cx_base_skip_false_prob,
1762          sizeof(vp8cx_base_skip_false_prob));
1763   cpi->common.current_video_frame = 0;
1764   cpi->temporal_pattern_counter = 0;
1765   cpi->temporal_layer_id = -1;
1766   cpi->kf_overspend_bits = 0;
1767   cpi->kf_bitrate_adjustment = 0;
1768   cpi->frames_till_gf_update_due = 0;
1769   cpi->gf_overspend_bits = 0;
1770   cpi->non_gf_bitrate_adjustment = 0;
1771   cpi->prob_last_coded = 128;
1772   cpi->prob_gf_coded = 128;
1773   cpi->prob_intra_coded = 63;
1774
1775   /* Prime the recent reference frame usage counters.
1776    * Hereafter they will be maintained as a sort of moving average
1777    */
1778   cpi->recent_ref_frame_usage[INTRA_FRAME] = 1;
1779   cpi->recent_ref_frame_usage[LAST_FRAME] = 1;
1780   cpi->recent_ref_frame_usage[GOLDEN_FRAME] = 1;
1781   cpi->recent_ref_frame_usage[ALTREF_FRAME] = 1;
1782
1783   /* Set reference frame sign bias for ALTREF frame to 1 (for now) */
1784   cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = 1;
1785
1786   cpi->twopass.gf_decay_rate = 0;
1787   cpi->baseline_gf_interval = DEFAULT_GF_INTERVAL;
1788
1789   cpi->gold_is_last = 0;
1790   cpi->alt_is_last = 0;
1791   cpi->gold_is_alt = 0;
1792
1793   cpi->active_map_enabled = 0;
1794
1795 #if 0
1796     /* Experimental code for lagged and one pass */
1797     /* Initialise one_pass GF frames stats */
1798     /* Update stats used for GF selection */
1799     if (cpi->pass == 0)
1800     {
1801         cpi->one_pass_frame_index = 0;
1802
1803         for (i = 0; i < MAX_LAG_BUFFERS; ++i)
1804         {
1805             cpi->one_pass_frame_stats[i].frames_so_far = 0;
1806             cpi->one_pass_frame_stats[i].frame_intra_error = 0.0;
1807             cpi->one_pass_frame_stats[i].frame_coded_error = 0.0;
1808             cpi->one_pass_frame_stats[i].frame_pcnt_inter = 0.0;
1809             cpi->one_pass_frame_stats[i].frame_pcnt_motion = 0.0;
1810             cpi->one_pass_frame_stats[i].frame_mvr = 0.0;
1811             cpi->one_pass_frame_stats[i].frame_mvr_abs = 0.0;
1812             cpi->one_pass_frame_stats[i].frame_mvc = 0.0;
1813             cpi->one_pass_frame_stats[i].frame_mvc_abs = 0.0;
1814         }
1815     }
1816 #endif
1817
1818   cpi->mse_source_denoised = 0;
1819
1820   /* Should we use the cyclic refresh method.
1821    * Currently there is no external control for this.
1822    * Enable it for error_resilient_mode, or for 1 pass CBR mode.
1823    */
1824   cpi->cyclic_refresh_mode_enabled =
1825       (cpi->oxcf.error_resilient_mode ||
1826        (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER &&
1827         cpi->oxcf.Mode <= 2));
1828   cpi->cyclic_refresh_mode_max_mbs_perframe =
1829       (cpi->common.mb_rows * cpi->common.mb_cols) / 7;
1830   if (cpi->oxcf.number_of_layers == 1) {
1831     cpi->cyclic_refresh_mode_max_mbs_perframe =
1832         (cpi->common.mb_rows * cpi->common.mb_cols) / 20;
1833   } else if (cpi->oxcf.number_of_layers == 2) {
1834     cpi->cyclic_refresh_mode_max_mbs_perframe =
1835         (cpi->common.mb_rows * cpi->common.mb_cols) / 10;
1836   }
1837   cpi->cyclic_refresh_mode_index = 0;
1838   cpi->cyclic_refresh_q = 32;
1839
1840   // GF behavior for 1 pass CBR, used when error_resilience is off.
1841   cpi->gf_update_onepass_cbr = 0;
1842   cpi->gf_noboost_onepass_cbr = 0;
1843   if (!cpi->oxcf.error_resilient_mode &&
1844       cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER && cpi->oxcf.Mode <= 2) {
1845     cpi->gf_update_onepass_cbr = 1;
1846     cpi->gf_noboost_onepass_cbr = 1;
1847     cpi->gf_interval_onepass_cbr =
1848         cpi->cyclic_refresh_mode_max_mbs_perframe > 0
1849             ? (2 * (cpi->common.mb_rows * cpi->common.mb_cols) /
1850                cpi->cyclic_refresh_mode_max_mbs_perframe)
1851             : 10;
1852     cpi->gf_interval_onepass_cbr =
1853         VPXMIN(40, VPXMAX(6, cpi->gf_interval_onepass_cbr));
1854     cpi->baseline_gf_interval = cpi->gf_interval_onepass_cbr;
1855   }
1856
1857   if (cpi->cyclic_refresh_mode_enabled) {
1858     CHECK_MEM_ERROR(cpi->cyclic_refresh_map,
1859                     vpx_calloc((cpi->common.mb_rows * cpi->common.mb_cols), 1));
1860   } else {
1861     cpi->cyclic_refresh_map = (signed char *)NULL;
1862   }
1863
1864   CHECK_MEM_ERROR(cpi->consec_zero_last,
1865                   vpx_calloc(cm->mb_rows * cm->mb_cols, 1));
1866   CHECK_MEM_ERROR(cpi->consec_zero_last_mvbias,
1867                   vpx_calloc((cpi->common.mb_rows * cpi->common.mb_cols), 1));
1868
1869 #ifdef VP8_ENTROPY_STATS
1870   init_context_counters();
1871 #endif
1872
1873   /*Initialize the feed-forward activity masking.*/
1874   cpi->activity_avg = 90 << 12;
1875
1876   /* Give a sensible default for the first frame. */
1877   cpi->frames_since_key = 8;
1878   cpi->key_frame_frequency = cpi->oxcf.key_freq;
1879   cpi->this_key_frame_forced = 0;
1880   cpi->next_key_frame_forced = 0;
1881
1882   cpi->source_alt_ref_pending = 0;
1883   cpi->source_alt_ref_active = 0;
1884   cpi->common.refresh_alt_ref_frame = 0;
1885
1886   cpi->force_maxqp = 0;
1887
1888   cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
1889 #if CONFIG_INTERNAL_STATS
1890   cpi->b_calculate_ssimg = 0;
1891
1892   cpi->count = 0;
1893   cpi->bytes = 0;
1894
1895   if (cpi->b_calculate_psnr) {
1896     cpi->total_sq_error = 0.0;
1897     cpi->total_sq_error2 = 0.0;
1898     cpi->total_y = 0.0;
1899     cpi->total_u = 0.0;
1900     cpi->total_v = 0.0;
1901     cpi->total = 0.0;
1902     cpi->totalp_y = 0.0;
1903     cpi->totalp_u = 0.0;
1904     cpi->totalp_v = 0.0;
1905     cpi->totalp = 0.0;
1906     cpi->tot_recode_hits = 0;
1907     cpi->summed_quality = 0;
1908     cpi->summed_weights = 0;
1909   }
1910
1911 #endif
1912
1913   cpi->first_time_stamp_ever = 0x7FFFFFFF;
1914
1915   cpi->frames_till_gf_update_due = 0;
1916   cpi->key_frame_count = 1;
1917
1918   cpi->ni_av_qi = cpi->oxcf.worst_allowed_q;
1919   cpi->ni_tot_qi = 0;
1920   cpi->ni_frames = 0;
1921   cpi->total_byte_count = 0;
1922
1923   cpi->drop_frame = 0;
1924
1925   cpi->rate_correction_factor = 1.0;
1926   cpi->key_frame_rate_correction_factor = 1.0;
1927   cpi->gf_rate_correction_factor = 1.0;
1928   cpi->twopass.est_max_qcorrection_factor = 1.0;
1929
1930   for (i = 0; i < KEY_FRAME_CONTEXT; ++i) {
1931     cpi->prior_key_frame_distance[i] = (int)cpi->output_framerate;
1932   }
1933
1934 #ifdef OUTPUT_YUV_SRC
1935   yuv_file = fopen("bd.yuv", "ab");
1936 #endif
1937 #ifdef OUTPUT_YUV_DENOISED
1938   yuv_denoised_file = fopen("denoised.yuv", "ab");
1939 #endif
1940 #ifdef OUTPUT_YUV_SKINMAP
1941   yuv_skinmap_file = fopen("skinmap.yuv", "ab");
1942 #endif
1943
1944 #if 0
1945     framepsnr = fopen("framepsnr.stt", "a");
1946     kf_list = fopen("kf_list.stt", "w");
1947 #endif
1948
1949   cpi->output_pkt_list = oxcf->output_pkt_list;
1950
1951 #if !CONFIG_REALTIME_ONLY
1952
1953   if (cpi->pass == 1) {
1954     vp8_init_first_pass(cpi);
1955   } else if (cpi->pass == 2) {
1956     size_t packet_sz = sizeof(FIRSTPASS_STATS);
1957     int packets = (int)(oxcf->two_pass_stats_in.sz / packet_sz);
1958
1959     cpi->twopass.stats_in_start = oxcf->two_pass_stats_in.buf;
1960     cpi->twopass.stats_in = cpi->twopass.stats_in_start;
1961     cpi->twopass.stats_in_end =
1962         (void *)((char *)cpi->twopass.stats_in + (packets - 1) * packet_sz);
1963     vp8_init_second_pass(cpi);
1964   }
1965
1966 #endif
1967
1968   if (cpi->compressor_speed == 2) {
1969     cpi->avg_encode_time = 0;
1970     cpi->avg_pick_mode_time = 0;
1971   }
1972
1973   vp8_set_speed_features(cpi);
1974
1975   /* Set starting values of RD threshold multipliers (128 = *1) */
1976   for (i = 0; i < MAX_MODES; ++i) {
1977     cpi->mb.rd_thresh_mult[i] = 128;
1978   }
1979
1980 #ifdef VP8_ENTROPY_STATS
1981   init_mv_ref_counts();
1982 #endif
1983
1984 #if CONFIG_MULTITHREAD
1985   if (vp8cx_create_encoder_threads(cpi)) {
1986     vp8_remove_compressor(&cpi);
1987     return 0;
1988   }
1989 #endif
1990
1991   cpi->fn_ptr[BLOCK_16X16].sdf = vpx_sad16x16;
1992   cpi->fn_ptr[BLOCK_16X16].vf = vpx_variance16x16;
1993   cpi->fn_ptr[BLOCK_16X16].svf = vpx_sub_pixel_variance16x16;
1994   cpi->fn_ptr[BLOCK_16X16].sdx3f = vpx_sad16x16x3;
1995   cpi->fn_ptr[BLOCK_16X16].sdx8f = vpx_sad16x16x8;
1996   cpi->fn_ptr[BLOCK_16X16].sdx4df = vpx_sad16x16x4d;
1997
1998   cpi->fn_ptr[BLOCK_16X8].sdf = vpx_sad16x8;
1999   cpi->fn_ptr[BLOCK_16X8].vf = vpx_variance16x8;
2000   cpi->fn_ptr[BLOCK_16X8].svf = vpx_sub_pixel_variance16x8;
2001   cpi->fn_ptr[BLOCK_16X8].sdx3f = vpx_sad16x8x3;
2002   cpi->fn_ptr[BLOCK_16X8].sdx8f = vpx_sad16x8x8;
2003   cpi->fn_ptr[BLOCK_16X8].sdx4df = vpx_sad16x8x4d;
2004
2005   cpi->fn_ptr[BLOCK_8X16].sdf = vpx_sad8x16;
2006   cpi->fn_ptr[BLOCK_8X16].vf = vpx_variance8x16;
2007   cpi->fn_ptr[BLOCK_8X16].svf = vpx_sub_pixel_variance8x16;
2008   cpi->fn_ptr[BLOCK_8X16].sdx3f = vpx_sad8x16x3;
2009   cpi->fn_ptr[BLOCK_8X16].sdx8f = vpx_sad8x16x8;
2010   cpi->fn_ptr[BLOCK_8X16].sdx4df = vpx_sad8x16x4d;
2011
2012   cpi->fn_ptr[BLOCK_8X8].sdf = vpx_sad8x8;
2013   cpi->fn_ptr[BLOCK_8X8].vf = vpx_variance8x8;
2014   cpi->fn_ptr[BLOCK_8X8].svf = vpx_sub_pixel_variance8x8;
2015   cpi->fn_ptr[BLOCK_8X8].sdx3f = vpx_sad8x8x3;
2016   cpi->fn_ptr[BLOCK_8X8].sdx8f = vpx_sad8x8x8;
2017   cpi->fn_ptr[BLOCK_8X8].sdx4df = vpx_sad8x8x4d;
2018
2019   cpi->fn_ptr[BLOCK_4X4].sdf = vpx_sad4x4;
2020   cpi->fn_ptr[BLOCK_4X4].vf = vpx_variance4x4;
2021   cpi->fn_ptr[BLOCK_4X4].svf = vpx_sub_pixel_variance4x4;
2022   cpi->fn_ptr[BLOCK_4X4].sdx3f = vpx_sad4x4x3;
2023   cpi->fn_ptr[BLOCK_4X4].sdx8f = vpx_sad4x4x8;
2024   cpi->fn_ptr[BLOCK_4X4].sdx4df = vpx_sad4x4x4d;
2025
2026 #if ARCH_X86 || ARCH_X86_64
2027   cpi->fn_ptr[BLOCK_16X16].copymem = vp8_copy32xn;
2028   cpi->fn_ptr[BLOCK_16X8].copymem = vp8_copy32xn;
2029   cpi->fn_ptr[BLOCK_8X16].copymem = vp8_copy32xn;
2030   cpi->fn_ptr[BLOCK_8X8].copymem = vp8_copy32xn;
2031   cpi->fn_ptr[BLOCK_4X4].copymem = vp8_copy32xn;
2032 #endif
2033
2034   cpi->full_search_sad = vp8_full_search_sad;
2035   cpi->diamond_search_sad = vp8_diamond_search_sad;
2036   cpi->refining_search_sad = vp8_refining_search_sad;
2037
2038   /* make sure frame 1 is okay */
2039   cpi->mb.error_bins[0] = cpi->common.MBs;
2040
2041   /* vp8cx_init_quantizer() is first called here. Add check in
2042    * vp8cx_frame_init_quantizer() so that vp8cx_init_quantizer is only
2043    * called later when needed. This will avoid unnecessary calls of
2044    * vp8cx_init_quantizer() for every frame.
2045    */
2046   vp8cx_init_quantizer(cpi);
2047
2048   vp8_loop_filter_init(cm);
2049
2050   cpi->common.error.setjmp = 0;
2051
2052 #if CONFIG_MULTI_RES_ENCODING
2053
2054   /* Calculate # of MBs in a row in lower-resolution level image. */
2055   if (cpi->oxcf.mr_encoder_id > 0) vp8_cal_low_res_mb_cols(cpi);
2056
2057 #endif
2058
2059   /* setup RD costs to MACROBLOCK struct */
2060
2061   cpi->mb.mvcost[0] = &cpi->rd_costs.mvcosts[0][mv_max + 1];
2062   cpi->mb.mvcost[1] = &cpi->rd_costs.mvcosts[1][mv_max + 1];
2063   cpi->mb.mvsadcost[0] = &cpi->rd_costs.mvsadcosts[0][mvfp_max + 1];
2064   cpi->mb.mvsadcost[1] = &cpi->rd_costs.mvsadcosts[1][mvfp_max + 1];
2065
2066   cal_mvsadcosts(cpi->mb.mvsadcost);
2067
2068   cpi->mb.mbmode_cost = cpi->rd_costs.mbmode_cost;
2069   cpi->mb.intra_uv_mode_cost = cpi->rd_costs.intra_uv_mode_cost;
2070   cpi->mb.bmode_costs = cpi->rd_costs.bmode_costs;
2071   cpi->mb.inter_bmode_costs = cpi->rd_costs.inter_bmode_costs;
2072   cpi->mb.token_costs = cpi->rd_costs.token_costs;
2073
2074   /* setup block ptrs & offsets */
2075   vp8_setup_block_ptrs(&cpi->mb);
2076   vp8_setup_block_dptrs(&cpi->mb.e_mbd);
2077
2078   return cpi;
2079 }
2080
2081 void vp8_remove_compressor(VP8_COMP **ptr) {
2082   VP8_COMP *cpi = *ptr;
2083
2084   if (!cpi) return;
2085
2086   if (cpi && (cpi->common.current_video_frame > 0)) {
2087 #if !CONFIG_REALTIME_ONLY
2088
2089     if (cpi->pass == 2) {
2090       vp8_end_second_pass(cpi);
2091     }
2092
2093 #endif
2094
2095 #ifdef VP8_ENTROPY_STATS
2096     print_context_counters();
2097     print_tree_update_probs();
2098     print_mode_context();
2099 #endif
2100
2101 #if CONFIG_INTERNAL_STATS
2102
2103     if (cpi->pass != 1) {
2104       FILE *f = fopen("opsnr.stt", "a");
2105       double time_encoded =
2106           (cpi->last_end_time_stamp_seen - cpi->first_time_stamp_ever) /
2107           10000000.000;
2108       double dr = (double)cpi->bytes * 8.0 / 1000.0 / time_encoded;
2109
2110       if (cpi->b_calculate_psnr) {
2111         if (cpi->oxcf.number_of_layers > 1) {
2112           int i;
2113
2114           fprintf(f,
2115                   "Layer\tBitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\t"
2116                   "GLPsnrP\tVPXSSIM\n");
2117           for (i = 0; i < (int)cpi->oxcf.number_of_layers; ++i) {
2118             double dr =
2119                 (double)cpi->bytes_in_layer[i] * 8.0 / 1000.0 / time_encoded;
2120             double samples = 3.0 / 2 * cpi->frames_in_layer[i] *
2121                              cpi->common.Width * cpi->common.Height;
2122             double total_psnr =
2123                 vpx_sse_to_psnr(samples, 255.0, cpi->total_error2[i]);
2124             double total_psnr2 =
2125                 vpx_sse_to_psnr(samples, 255.0, cpi->total_error2_p[i]);
2126             double total_ssim =
2127                 100 * pow(cpi->sum_ssim[i] / cpi->sum_weights[i], 8.0);
2128
2129             fprintf(f,
2130                     "%5d\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
2131                     "%7.3f\t%7.3f\n",
2132                     i, dr, cpi->sum_psnr[i] / cpi->frames_in_layer[i],
2133                     total_psnr, cpi->sum_psnr_p[i] / cpi->frames_in_layer[i],
2134                     total_psnr2, total_ssim);
2135           }
2136         } else {
2137           double samples =
2138               3.0 / 2 * cpi->count * cpi->common.Width * cpi->common.Height;
2139           double total_psnr =
2140               vpx_sse_to_psnr(samples, 255.0, cpi->total_sq_error);
2141           double total_psnr2 =
2142               vpx_sse_to_psnr(samples, 255.0, cpi->total_sq_error2);
2143           double total_ssim =
2144               100 * pow(cpi->summed_quality / cpi->summed_weights, 8.0);
2145
2146           fprintf(f,
2147                   "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\t"
2148                   "GLPsnrP\tVPXSSIM\n");
2149           fprintf(f,
2150                   "%7.3f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
2151                   "%7.3f\n",
2152                   dr, cpi->total / cpi->count, total_psnr,
2153                   cpi->totalp / cpi->count, total_psnr2, total_ssim);
2154         }
2155       }
2156       fclose(f);
2157 #if 0
2158             f = fopen("qskip.stt", "a");
2159             fprintf(f, "minq:%d -maxq:%d skiptrue:skipfalse = %d:%d\n", cpi->oxcf.best_allowed_q, cpi->oxcf.worst_allowed_q, skiptruecount, skipfalsecount);
2160             fclose(f);
2161 #endif
2162     }
2163
2164 #endif
2165
2166 #ifdef SPEEDSTATS
2167
2168     if (cpi->compressor_speed == 2) {
2169       int i;
2170       FILE *f = fopen("cxspeed.stt", "a");
2171       cnt_pm /= cpi->common.MBs;
2172
2173       for (i = 0; i < 16; ++i) fprintf(f, "%5d", frames_at_speed[i]);
2174
2175       fprintf(f, "\n");
2176       fclose(f);
2177     }
2178
2179 #endif
2180
2181 #ifdef MODE_STATS
2182     {
2183       extern int count_mb_seg[4];
2184       FILE *f = fopen("modes.stt", "a");
2185       double dr = (double)cpi->framerate * (double)bytes * (double)8 /
2186                   (double)count / (double)1000;
2187       fprintf(f, "intra_mode in Intra Frames:\n");
2188       fprintf(f, "Y: %8d, %8d, %8d, %8d, %8d\n", y_modes[0], y_modes[1],
2189               y_modes[2], y_modes[3], y_modes[4]);
2190       fprintf(f, "UV:%8d, %8d, %8d, %8d\n", uv_modes[0], uv_modes[1],
2191               uv_modes[2], uv_modes[3]);
2192       fprintf(f, "B: ");
2193       {
2194         int i;
2195
2196         for (i = 0; i < 10; ++i) fprintf(f, "%8d, ", b_modes[i]);
2197
2198         fprintf(f, "\n");
2199       }
2200
2201       fprintf(f, "Modes in Inter Frames:\n");
2202       fprintf(f, "Y: %8d, %8d, %8d, %8d, %8d, %8d, %8d, %8d, %8d, %8d\n",
2203               inter_y_modes[0], inter_y_modes[1], inter_y_modes[2],
2204               inter_y_modes[3], inter_y_modes[4], inter_y_modes[5],
2205               inter_y_modes[6], inter_y_modes[7], inter_y_modes[8],
2206               inter_y_modes[9]);
2207       fprintf(f, "UV:%8d, %8d, %8d, %8d\n", inter_uv_modes[0],
2208               inter_uv_modes[1], inter_uv_modes[2], inter_uv_modes[3]);
2209       fprintf(f, "B: ");
2210       {
2211         int i;
2212
2213         for (i = 0; i < 15; ++i) fprintf(f, "%8d, ", inter_b_modes[i]);
2214
2215         fprintf(f, "\n");
2216       }
2217       fprintf(f, "P:%8d, %8d, %8d, %8d\n", count_mb_seg[0], count_mb_seg[1],
2218               count_mb_seg[2], count_mb_seg[3]);
2219       fprintf(f, "PB:%8d, %8d, %8d, %8d\n", inter_b_modes[LEFT4X4],
2220               inter_b_modes[ABOVE4X4], inter_b_modes[ZERO4X4],
2221               inter_b_modes[NEW4X4]);
2222
2223       fclose(f);
2224     }
2225 #endif
2226
2227 #ifdef VP8_ENTROPY_STATS
2228     {
2229       int i, j, k;
2230       FILE *fmode = fopen("modecontext.c", "w");
2231
2232       fprintf(fmode, "\n#include \"entropymode.h\"\n\n");
2233       fprintf(fmode, "const unsigned int vp8_kf_default_bmode_counts ");
2234       fprintf(fmode,
2235               "[VP8_BINTRAMODES] [VP8_BINTRAMODES] [VP8_BINTRAMODES] =\n{\n");
2236
2237       for (i = 0; i < 10; ++i) {
2238         fprintf(fmode, "    { /* Above Mode :  %d */\n", i);
2239
2240         for (j = 0; j < 10; ++j) {
2241           fprintf(fmode, "        {");
2242
2243           for (k = 0; k < 10; ++k) {
2244             if (!intra_mode_stats[i][j][k])
2245               fprintf(fmode, " %5d, ", 1);
2246             else
2247               fprintf(fmode, " %5d, ", intra_mode_stats[i][j][k]);
2248           }
2249
2250           fprintf(fmode, "}, /* left_mode %d */\n", j);
2251         }
2252
2253         fprintf(fmode, "    },\n");
2254       }
2255
2256       fprintf(fmode, "};\n");
2257       fclose(fmode);
2258     }
2259 #endif
2260
2261 #if defined(SECTIONBITS_OUTPUT)
2262
2263     if (0) {
2264       int i;
2265       FILE *f = fopen("tokenbits.stt", "a");
2266
2267       for (i = 0; i < 28; ++i) fprintf(f, "%8d", (int)(Sectionbits[i] / 256));
2268
2269       fprintf(f, "\n");
2270       fclose(f);
2271     }
2272
2273 #endif
2274
2275 #if 0
2276         {
2277             printf("\n_pick_loop_filter_level:%d\n", cpi->time_pick_lpf / 1000);
2278             printf("\n_frames recive_data encod_mb_row compress_frame  Total\n");
2279             printf("%6d %10ld %10ld %10ld %10ld\n", cpi->common.current_video_frame, cpi->time_receive_data / 1000, cpi->time_encode_mb_row / 1000, cpi->time_compress_data / 1000, (cpi->time_receive_data + cpi->time_compress_data) / 1000);
2280         }
2281 #endif
2282   }
2283
2284 #if CONFIG_MULTITHREAD
2285   vp8cx_remove_encoder_threads(cpi);
2286 #endif
2287
2288 #if CONFIG_TEMPORAL_DENOISING
2289   vp8_denoiser_free(&cpi->denoiser);
2290 #endif
2291   dealloc_compressor_data(cpi);
2292   vpx_free(cpi->mb.ss);
2293   vpx_free(cpi->tok);
2294   vpx_free(cpi->cyclic_refresh_map);
2295   vpx_free(cpi->consec_zero_last);
2296   vpx_free(cpi->consec_zero_last_mvbias);
2297
2298   vp8_remove_common(&cpi->common);
2299   vpx_free(cpi);
2300   *ptr = 0;
2301
2302 #ifdef OUTPUT_YUV_SRC
2303   fclose(yuv_file);
2304 #endif
2305 #ifdef OUTPUT_YUV_DENOISED
2306   fclose(yuv_denoised_file);
2307 #endif
2308 #ifdef OUTPUT_YUV_SKINMAP
2309   fclose(yuv_skinmap_file);
2310 #endif
2311
2312 #if 0
2313
2314     if (keyfile)
2315         fclose(keyfile);
2316
2317     if (framepsnr)
2318         fclose(framepsnr);
2319
2320     if (kf_list)
2321         fclose(kf_list);
2322
2323 #endif
2324 }
2325
2326 static uint64_t calc_plane_error(unsigned char *orig, int orig_stride,
2327                                  unsigned char *recon, int recon_stride,
2328                                  unsigned int cols, unsigned int rows) {
2329   unsigned int row, col;
2330   uint64_t total_sse = 0;
2331   int diff;
2332
2333   for (row = 0; row + 16 <= rows; row += 16) {
2334     for (col = 0; col + 16 <= cols; col += 16) {
2335       unsigned int sse;
2336
2337       vpx_mse16x16(orig + col, orig_stride, recon + col, recon_stride, &sse);
2338       total_sse += sse;
2339     }
2340
2341     /* Handle odd-sized width */
2342     if (col < cols) {
2343       unsigned int border_row, border_col;
2344       unsigned char *border_orig = orig;
2345       unsigned char *border_recon = recon;
2346
2347       for (border_row = 0; border_row < 16; ++border_row) {
2348         for (border_col = col; border_col < cols; ++border_col) {
2349           diff = border_orig[border_col] - border_recon[border_col];
2350           total_sse += diff * diff;
2351         }
2352
2353         border_orig += orig_stride;
2354         border_recon += recon_stride;
2355       }
2356     }
2357
2358     orig += orig_stride * 16;
2359     recon += recon_stride * 16;
2360   }
2361
2362   /* Handle odd-sized height */
2363   for (; row < rows; ++row) {
2364     for (col = 0; col < cols; ++col) {
2365       diff = orig[col] - recon[col];
2366       total_sse += diff * diff;
2367     }
2368
2369     orig += orig_stride;
2370     recon += recon_stride;
2371   }
2372
2373   vpx_clear_system_state();
2374   return total_sse;
2375 }
2376
2377 static void generate_psnr_packet(VP8_COMP *cpi) {
2378   YV12_BUFFER_CONFIG *orig = cpi->Source;
2379   YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
2380   struct vpx_codec_cx_pkt pkt;
2381   uint64_t sse;
2382   int i;
2383   unsigned int width = cpi->common.Width;
2384   unsigned int height = cpi->common.Height;
2385
2386   pkt.kind = VPX_CODEC_PSNR_PKT;
2387   sse = calc_plane_error(orig->y_buffer, orig->y_stride, recon->y_buffer,
2388                          recon->y_stride, width, height);
2389   pkt.data.psnr.sse[0] = sse;
2390   pkt.data.psnr.sse[1] = sse;
2391   pkt.data.psnr.samples[0] = width * height;
2392   pkt.data.psnr.samples[1] = width * height;
2393
2394   width = (width + 1) / 2;
2395   height = (height + 1) / 2;
2396
2397   sse = calc_plane_error(orig->u_buffer, orig->uv_stride, recon->u_buffer,
2398                          recon->uv_stride, width, height);
2399   pkt.data.psnr.sse[0] += sse;
2400   pkt.data.psnr.sse[2] = sse;
2401   pkt.data.psnr.samples[0] += width * height;
2402   pkt.data.psnr.samples[2] = width * height;
2403
2404   sse = calc_plane_error(orig->v_buffer, orig->uv_stride, recon->v_buffer,
2405                          recon->uv_stride, width, height);
2406   pkt.data.psnr.sse[0] += sse;
2407   pkt.data.psnr.sse[3] = sse;
2408   pkt.data.psnr.samples[0] += width * height;
2409   pkt.data.psnr.samples[3] = width * height;
2410
2411   for (i = 0; i < 4; ++i) {
2412     pkt.data.psnr.psnr[i] = vpx_sse_to_psnr(pkt.data.psnr.samples[i], 255.0,
2413                                             (double)(pkt.data.psnr.sse[i]));
2414   }
2415
2416   vpx_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
2417 }
2418
2419 int vp8_use_as_reference(VP8_COMP *cpi, int ref_frame_flags) {
2420   if (ref_frame_flags > 7) return -1;
2421
2422   cpi->ref_frame_flags = ref_frame_flags;
2423   return 0;
2424 }
2425 int vp8_update_reference(VP8_COMP *cpi, int ref_frame_flags) {
2426   if (ref_frame_flags > 7) return -1;
2427
2428   cpi->common.refresh_golden_frame = 0;
2429   cpi->common.refresh_alt_ref_frame = 0;
2430   cpi->common.refresh_last_frame = 0;
2431
2432   if (ref_frame_flags & VP8_LAST_FRAME) cpi->common.refresh_last_frame = 1;
2433
2434   if (ref_frame_flags & VP8_GOLD_FRAME) cpi->common.refresh_golden_frame = 1;
2435
2436   if (ref_frame_flags & VP8_ALTR_FRAME) cpi->common.refresh_alt_ref_frame = 1;
2437
2438   return 0;
2439 }
2440
2441 int vp8_get_reference(VP8_COMP *cpi, enum vpx_ref_frame_type ref_frame_flag,
2442                       YV12_BUFFER_CONFIG *sd) {
2443   VP8_COMMON *cm = &cpi->common;
2444   int ref_fb_idx;
2445
2446   if (ref_frame_flag == VP8_LAST_FRAME) {
2447     ref_fb_idx = cm->lst_fb_idx;
2448   } else if (ref_frame_flag == VP8_GOLD_FRAME) {
2449     ref_fb_idx = cm->gld_fb_idx;
2450   } else if (ref_frame_flag == VP8_ALTR_FRAME) {
2451     ref_fb_idx = cm->alt_fb_idx;
2452   } else {
2453     return -1;
2454   }
2455
2456   vp8_yv12_copy_frame(&cm->yv12_fb[ref_fb_idx], sd);
2457
2458   return 0;
2459 }
2460 int vp8_set_reference(VP8_COMP *cpi, enum vpx_ref_frame_type ref_frame_flag,
2461                       YV12_BUFFER_CONFIG *sd) {
2462   VP8_COMMON *cm = &cpi->common;
2463
2464   int ref_fb_idx;
2465
2466   if (ref_frame_flag == VP8_LAST_FRAME) {
2467     ref_fb_idx = cm->lst_fb_idx;
2468   } else if (ref_frame_flag == VP8_GOLD_FRAME) {
2469     ref_fb_idx = cm->gld_fb_idx;
2470   } else if (ref_frame_flag == VP8_ALTR_FRAME) {
2471     ref_fb_idx = cm->alt_fb_idx;
2472   } else {
2473     return -1;
2474   }
2475
2476   vp8_yv12_copy_frame(sd, &cm->yv12_fb[ref_fb_idx]);
2477
2478   return 0;
2479 }
2480 int vp8_update_entropy(VP8_COMP *cpi, int update) {
2481   VP8_COMMON *cm = &cpi->common;
2482   cm->refresh_entropy_probs = update;
2483
2484   return 0;
2485 }
2486
2487 #if defined(OUTPUT_YUV_SRC) || defined(OUTPUT_YUV_DENOISED) || \
2488     defined(OUTPUT_YUV_SKINMAP)
2489 void vp8_write_yuv_frame(FILE *yuv_file, YV12_BUFFER_CONFIG *s) {
2490   unsigned char *src = s->y_buffer;
2491   int h = s->y_crop_height;
2492
2493   do {
2494     fwrite(src, s->y_width, 1, yuv_file);
2495     src += s->y_stride;
2496   } while (--h);
2497
2498   src = s->u_buffer;
2499   h = s->uv_crop_height;
2500
2501   do {
2502     fwrite(src, s->uv_width, 1, yuv_file);
2503     src += s->uv_stride;
2504   } while (--h);
2505
2506   src = s->v_buffer;
2507   h = s->uv_crop_height;
2508
2509   do {
2510     fwrite(src, s->uv_width, 1, yuv_file);
2511     src += s->uv_stride;
2512   } while (--h);
2513 }
2514 #endif
2515
2516 static void scale_and_extend_source(YV12_BUFFER_CONFIG *sd, VP8_COMP *cpi) {
2517   VP8_COMMON *cm = &cpi->common;
2518
2519   /* are we resizing the image */
2520   if (cm->horiz_scale != 0 || cm->vert_scale != 0) {
2521 #if CONFIG_SPATIAL_RESAMPLING
2522     int hr, hs, vr, vs;
2523     int tmp_height;
2524
2525     if (cm->vert_scale == 3) {
2526       tmp_height = 9;
2527     } else {
2528       tmp_height = 11;
2529     }
2530
2531     Scale2Ratio(cm->horiz_scale, &hr, &hs);
2532     Scale2Ratio(cm->vert_scale, &vr, &vs);
2533
2534     vpx_scale_frame(sd, &cpi->scaled_source, cm->temp_scale_frame.y_buffer,
2535                     tmp_height, hs, hr, vs, vr, 0);
2536
2537     vp8_yv12_extend_frame_borders(&cpi->scaled_source);
2538     cpi->Source = &cpi->scaled_source;
2539 #endif
2540   } else {
2541     cpi->Source = sd;
2542   }
2543 }
2544
2545 static int resize_key_frame(VP8_COMP *cpi) {
2546 #if CONFIG_SPATIAL_RESAMPLING
2547   VP8_COMMON *cm = &cpi->common;
2548
2549   /* Do we need to apply resampling for one pass cbr.
2550    * In one pass this is more limited than in two pass cbr.
2551    * The test and any change is only made once per key frame sequence.
2552    */
2553   if (cpi->oxcf.allow_spatial_resampling &&
2554       (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)) {
2555     int hr, hs, vr, vs;
2556     int new_width, new_height;
2557
2558     /* If we are below the resample DOWN watermark then scale down a
2559      * notch.
2560      */
2561     if (cpi->buffer_level < (cpi->oxcf.resample_down_water_mark *
2562                              cpi->oxcf.optimal_buffer_level / 100)) {
2563       cm->horiz_scale =
2564           (cm->horiz_scale < ONETWO) ? cm->horiz_scale + 1 : ONETWO;
2565       cm->vert_scale = (cm->vert_scale < ONETWO) ? cm->vert_scale + 1 : ONETWO;
2566     }
2567     /* Should we now start scaling back up */
2568     else if (cpi->buffer_level > (cpi->oxcf.resample_up_water_mark *
2569                                   cpi->oxcf.optimal_buffer_level / 100)) {
2570       cm->horiz_scale =
2571           (cm->horiz_scale > NORMAL) ? cm->horiz_scale - 1 : NORMAL;
2572       cm->vert_scale = (cm->vert_scale > NORMAL) ? cm->vert_scale - 1 : NORMAL;
2573     }
2574
2575     /* Get the new height and width */
2576     Scale2Ratio(cm->horiz_scale, &hr, &hs);
2577     Scale2Ratio(cm->vert_scale, &vr, &vs);
2578     new_width = ((hs - 1) + (cpi->oxcf.Width * hr)) / hs;
2579     new_height = ((vs - 1) + (cpi->oxcf.Height * vr)) / vs;
2580
2581     /* If the image size has changed we need to reallocate the buffers
2582      * and resample the source image
2583      */
2584     if ((cm->Width != new_width) || (cm->Height != new_height)) {
2585       cm->Width = new_width;
2586       cm->Height = new_height;
2587       vp8_alloc_compressor_data(cpi);
2588       scale_and_extend_source(cpi->un_scaled_source, cpi);
2589       return 1;
2590     }
2591   }
2592
2593 #endif
2594   return 0;
2595 }
2596
2597 static void update_alt_ref_frame_stats(VP8_COMP *cpi) {
2598   VP8_COMMON *cm = &cpi->common;
2599
2600   /* Select an interval before next GF or altref */
2601   if (!cpi->auto_gold) cpi->frames_till_gf_update_due = DEFAULT_GF_INTERVAL;
2602
2603   if ((cpi->pass != 2) && cpi->frames_till_gf_update_due) {
2604     cpi->current_gf_interval = cpi->frames_till_gf_update_due;
2605
2606     /* Set the bits per frame that we should try and recover in
2607      * subsequent inter frames to account for the extra GF spend...
2608      * note that his does not apply for GF updates that occur
2609      * coincident with a key frame as the extra cost of key frames is
2610      * dealt with elsewhere.
2611      */
2612     cpi->gf_overspend_bits += cpi->projected_frame_size;
2613     cpi->non_gf_bitrate_adjustment =
2614         cpi->gf_overspend_bits / cpi->frames_till_gf_update_due;
2615   }
2616
2617   /* Update data structure that monitors level of reference to last GF */
2618   memset(cpi->gf_active_flags, 1, (cm->mb_rows * cm->mb_cols));
2619   cpi->gf_active_count = cm->mb_rows * cm->mb_cols;
2620
2621   /* this frame refreshes means next frames don't unless specified by user */
2622   cpi->frames_since_golden = 0;
2623
2624   /* Clear the alternate reference update pending flag. */
2625   cpi->source_alt_ref_pending = 0;
2626
2627   /* Set the alternate reference frame active flag */
2628   cpi->source_alt_ref_active = 1;
2629 }
2630 static void update_golden_frame_stats(VP8_COMP *cpi) {
2631   VP8_COMMON *cm = &cpi->common;
2632
2633   /* Update the Golden frame usage counts. */
2634   if (cm->refresh_golden_frame) {
2635     /* Select an interval before next GF */
2636     if (!cpi->auto_gold) cpi->frames_till_gf_update_due = DEFAULT_GF_INTERVAL;
2637
2638     if ((cpi->pass != 2) && (cpi->frames_till_gf_update_due > 0)) {
2639       cpi->current_gf_interval = cpi->frames_till_gf_update_due;
2640
2641       /* Set the bits per frame that we should try and recover in
2642        * subsequent inter frames to account for the extra GF spend...
2643        * note that his does not apply for GF updates that occur
2644        * coincident with a key frame as the extra cost of key frames
2645        * is dealt with elsewhere.
2646        */
2647       if ((cm->frame_type != KEY_FRAME) && !cpi->source_alt_ref_active) {
2648         /* Calcluate GF bits to be recovered
2649          * Projected size - av frame bits available for inter
2650          * frames for clip as a whole
2651          */
2652         cpi->gf_overspend_bits +=
2653             (cpi->projected_frame_size - cpi->inter_frame_target);
2654       }
2655
2656       cpi->non_gf_bitrate_adjustment =
2657           cpi->gf_overspend_bits / cpi->frames_till_gf_update_due;
2658     }
2659
2660     /* Update data structure that monitors level of reference to last GF */
2661     memset(cpi->gf_active_flags, 1, (cm->mb_rows * cm->mb_cols));
2662     cpi->gf_active_count = cm->mb_rows * cm->mb_cols;
2663
2664     /* this frame refreshes means next frames don't unless specified by
2665      * user
2666      */
2667     cm->refresh_golden_frame = 0;
2668     cpi->frames_since_golden = 0;
2669
2670     cpi->recent_ref_frame_usage[INTRA_FRAME] = 1;
2671     cpi->recent_ref_frame_usage[LAST_FRAME] = 1;
2672     cpi->recent_ref_frame_usage[GOLDEN_FRAME] = 1;
2673     cpi->recent_ref_frame_usage[ALTREF_FRAME] = 1;
2674
2675     /* ******** Fixed Q test code only ************ */
2676     /* If we are going to use the ALT reference for the next group of
2677      * frames set a flag to say so.
2678      */
2679     if (cpi->oxcf.fixed_q >= 0 && cpi->oxcf.play_alternate &&
2680         !cpi->common.refresh_alt_ref_frame) {
2681       cpi->source_alt_ref_pending = 1;
2682       cpi->frames_till_gf_update_due = cpi->baseline_gf_interval;
2683     }
2684
2685     if (!cpi->source_alt_ref_pending) cpi->source_alt_ref_active = 0;
2686
2687     /* Decrement count down till next gf */
2688     if (cpi->frames_till_gf_update_due > 0) cpi->frames_till_gf_update_due--;
2689
2690   } else if (!cpi->common.refresh_alt_ref_frame) {
2691     /* Decrement count down till next gf */
2692     if (cpi->frames_till_gf_update_due > 0) cpi->frames_till_gf_update_due--;
2693
2694     if (cpi->frames_till_alt_ref_frame) cpi->frames_till_alt_ref_frame--;
2695
2696     cpi->frames_since_golden++;
2697
2698     if (cpi->frames_since_golden > 1) {
2699       cpi->recent_ref_frame_usage[INTRA_FRAME] +=
2700           cpi->mb.count_mb_ref_frame_usage[INTRA_FRAME];
2701       cpi->recent_ref_frame_usage[LAST_FRAME] +=
2702           cpi->mb.count_mb_ref_frame_usage[LAST_FRAME];
2703       cpi->recent_ref_frame_usage[GOLDEN_FRAME] +=
2704           cpi->mb.count_mb_ref_frame_usage[GOLDEN_FRAME];
2705       cpi->recent_ref_frame_usage[ALTREF_FRAME] +=
2706           cpi->mb.count_mb_ref_frame_usage[ALTREF_FRAME];
2707     }
2708   }
2709 }
2710
2711 /* This function updates the reference frame probability estimates that
2712  * will be used during mode selection
2713  */
2714 static void update_rd_ref_frame_probs(VP8_COMP *cpi) {
2715   VP8_COMMON *cm = &cpi->common;
2716
2717   const int *const rfct = cpi->mb.count_mb_ref_frame_usage;
2718   const int rf_intra = rfct[INTRA_FRAME];
2719   const int rf_inter =
2720       rfct[LAST_FRAME] + rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME];
2721
2722   if (cm->frame_type == KEY_FRAME) {
2723     cpi->prob_intra_coded = 255;
2724     cpi->prob_last_coded = 128;
2725     cpi->prob_gf_coded = 128;
2726   } else if (!(rf_intra + rf_inter)) {
2727     cpi->prob_intra_coded = 63;
2728     cpi->prob_last_coded = 128;
2729     cpi->prob_gf_coded = 128;
2730   }
2731
2732   /* update reference frame costs since we can do better than what we got
2733    * last frame.
2734    */
2735   if (cpi->oxcf.number_of_layers == 1) {
2736     if (cpi->common.refresh_alt_ref_frame) {
2737       cpi->prob_intra_coded += 40;
2738       if (cpi->prob_intra_coded > 255) cpi->prob_intra_coded = 255;
2739       cpi->prob_last_coded = 200;
2740       cpi->prob_gf_coded = 1;
2741     } else if (cpi->frames_since_golden == 0) {
2742       cpi->prob_last_coded = 214;
2743     } else if (cpi->frames_since_golden == 1) {
2744       cpi->prob_last_coded = 192;
2745       cpi->prob_gf_coded = 220;
2746     } else if (cpi->source_alt_ref_active) {
2747       cpi->prob_gf_coded -= 20;
2748
2749       if (cpi->prob_gf_coded < 10) cpi->prob_gf_coded = 10;
2750     }
2751     if (!cpi->source_alt_ref_active) cpi->prob_gf_coded = 255;
2752   }
2753 }
2754
2755 #if !CONFIG_REALTIME_ONLY
2756 /* 1 = key, 0 = inter */
2757 static int decide_key_frame(VP8_COMP *cpi) {
2758   VP8_COMMON *cm = &cpi->common;
2759
2760   int code_key_frame = 0;
2761
2762   cpi->kf_boost = 0;
2763
2764   if (cpi->Speed > 11) return 0;
2765
2766   /* Clear down mmx registers */
2767   vpx_clear_system_state();
2768
2769   if ((cpi->compressor_speed == 2) && (cpi->Speed >= 5) && (cpi->sf.RD == 0)) {
2770     double change = 1.0 *
2771                     abs((int)(cpi->mb.intra_error - cpi->last_intra_error)) /
2772                     (1 + cpi->last_intra_error);
2773     double change2 =
2774         1.0 *
2775         abs((int)(cpi->mb.prediction_error - cpi->last_prediction_error)) /
2776         (1 + cpi->last_prediction_error);
2777     double minerror = cm->MBs * 256;
2778
2779     cpi->last_intra_error = cpi->mb.intra_error;
2780     cpi->last_prediction_error = cpi->mb.prediction_error;
2781
2782     if (10 * cpi->mb.intra_error / (1 + cpi->mb.prediction_error) < 15 &&
2783         cpi->mb.prediction_error > minerror &&
2784         (change > .25 || change2 > .25)) {
2785       /*(change > 1.4 || change < .75)&& cpi->this_frame_percent_intra >
2786        * cpi->last_frame_percent_intra + 3*/
2787       return 1;
2788     }
2789
2790     return 0;
2791   }
2792
2793   /* If the following are true we might as well code a key frame */
2794   if (((cpi->this_frame_percent_intra == 100) &&
2795        (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra + 2))) ||
2796       ((cpi->this_frame_percent_intra > 95) &&
2797        (cpi->this_frame_percent_intra >=
2798         (cpi->last_frame_percent_intra + 5)))) {
2799     code_key_frame = 1;
2800   }
2801   /* in addition if the following are true and this is not a golden frame
2802    * then code a key frame Note that on golden frames there often seems
2803    * to be a pop in intra useage anyway hence this restriction is
2804    * designed to prevent spurious key frames. The Intra pop needs to be
2805    * investigated.
2806    */
2807   else if (((cpi->this_frame_percent_intra > 60) &&
2808             (cpi->this_frame_percent_intra >
2809              (cpi->last_frame_percent_intra * 2))) ||
2810            ((cpi->this_frame_percent_intra > 75) &&
2811             (cpi->this_frame_percent_intra >
2812              (cpi->last_frame_percent_intra * 3 / 2))) ||
2813            ((cpi->this_frame_percent_intra > 90) &&
2814             (cpi->this_frame_percent_intra >
2815              (cpi->last_frame_percent_intra + 10)))) {
2816     if (!cm->refresh_golden_frame) code_key_frame = 1;
2817   }
2818
2819   return code_key_frame;
2820 }
2821
2822 static void Pass1Encode(VP8_COMP *cpi, size_t *size, unsigned char *dest,
2823                         unsigned int *frame_flags) {
2824   (void)size;
2825   (void)dest;
2826   (void)frame_flags;
2827   vp8_set_quantizer(cpi, 26);
2828
2829   vp8_first_pass(cpi);
2830 }
2831 #endif
2832
2833 #if 0
2834 void write_cx_frame_to_file(YV12_BUFFER_CONFIG *frame, int this_frame)
2835 {
2836
2837     /* write the frame */
2838     FILE *yframe;
2839     int i;
2840     char filename[255];
2841
2842     sprintf(filename, "cx\\y%04d.raw", this_frame);
2843     yframe = fopen(filename, "wb");
2844
2845     for (i = 0; i < frame->y_height; ++i)
2846         fwrite(frame->y_buffer + i * frame->y_stride, frame->y_width, 1, yframe);
2847
2848     fclose(yframe);
2849     sprintf(filename, "cx\\u%04d.raw", this_frame);
2850     yframe = fopen(filename, "wb");
2851
2852     for (i = 0; i < frame->uv_height; ++i)
2853         fwrite(frame->u_buffer + i * frame->uv_stride, frame->uv_width, 1, yframe);
2854
2855     fclose(yframe);
2856     sprintf(filename, "cx\\v%04d.raw", this_frame);
2857     yframe = fopen(filename, "wb");
2858
2859     for (i = 0; i < frame->uv_height; ++i)
2860         fwrite(frame->v_buffer + i * frame->uv_stride, frame->uv_width, 1, yframe);
2861
2862     fclose(yframe);
2863 }
2864 #endif
2865 /* return of 0 means drop frame */
2866
2867 #if !CONFIG_REALTIME_ONLY
2868 /* Function to test for conditions that indeicate we should loop
2869  * back and recode a frame.
2870  */
2871 static int recode_loop_test(VP8_COMP *cpi, int high_limit, int low_limit, int q,
2872                             int maxq, int minq) {
2873   int force_recode = 0;
2874   VP8_COMMON *cm = &cpi->common;
2875
2876   /* Is frame recode allowed at all
2877    * Yes if either recode mode 1 is selected or mode two is selcted
2878    * and the frame is a key frame. golden frame or alt_ref_frame
2879    */
2880   if ((cpi->sf.recode_loop == 1) ||
2881       ((cpi->sf.recode_loop == 2) &&
2882        ((cm->frame_type == KEY_FRAME) || cm->refresh_golden_frame ||
2883         cm->refresh_alt_ref_frame))) {
2884     /* General over and under shoot tests */
2885     if (((cpi->projected_frame_size > high_limit) && (q < maxq)) ||
2886         ((cpi->projected_frame_size < low_limit) && (q > minq))) {
2887       force_recode = 1;
2888     }
2889     /* Special Constrained quality tests */
2890     else if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) {
2891       /* Undershoot and below auto cq level */
2892       if ((q > cpi->cq_target_quality) &&
2893           (cpi->projected_frame_size < ((cpi->this_frame_target * 7) >> 3))) {
2894         force_recode = 1;
2895       }
2896       /* Severe undershoot and between auto and user cq level */
2897       else if ((q > cpi->oxcf.cq_level) &&
2898                (cpi->projected_frame_size < cpi->min_frame_bandwidth) &&
2899                (cpi->active_best_quality > cpi->oxcf.cq_level)) {
2900         force_recode = 1;
2901         cpi->active_best_quality = cpi->oxcf.cq_level;
2902       }
2903     }
2904   }
2905
2906   return force_recode;
2907 }
2908 #endif  // !CONFIG_REALTIME_ONLY
2909
2910 static void update_reference_frames(VP8_COMP *cpi) {
2911   VP8_COMMON *cm = &cpi->common;
2912   YV12_BUFFER_CONFIG *yv12_fb = cm->yv12_fb;
2913
2914   /* At this point the new frame has been encoded.
2915    * If any buffer copy / swapping is signaled it should be done here.
2916    */
2917
2918   if (cm->frame_type == KEY_FRAME) {
2919     yv12_fb[cm->new_fb_idx].flags |= VP8_GOLD_FRAME | VP8_ALTR_FRAME;
2920
2921     yv12_fb[cm->gld_fb_idx].flags &= ~VP8_GOLD_FRAME;
2922     yv12_fb[cm->alt_fb_idx].flags &= ~VP8_ALTR_FRAME;
2923
2924     cm->alt_fb_idx = cm->gld_fb_idx = cm->new_fb_idx;
2925
2926     cpi->current_ref_frames[GOLDEN_FRAME] = cm->current_video_frame;
2927     cpi->current_ref_frames[ALTREF_FRAME] = cm->current_video_frame;
2928   } else /* For non key frames */
2929   {
2930     if (cm->refresh_alt_ref_frame) {
2931       assert(!cm->copy_buffer_to_arf);
2932
2933       cm->yv12_fb[cm->new_fb_idx].flags |= VP8_ALTR_FRAME;
2934       cm->yv12_fb[cm->alt_fb_idx].flags &= ~VP8_ALTR_FRAME;
2935       cm->alt_fb_idx = cm->new_fb_idx;
2936
2937       cpi->current_ref_frames[ALTREF_FRAME] = cm->current_video_frame;
2938     } else if (cm->copy_buffer_to_arf) {
2939       assert(!(cm->copy_buffer_to_arf & ~0x3));
2940
2941       if (cm->copy_buffer_to_arf == 1) {
2942         if (cm->alt_fb_idx != cm->lst_fb_idx) {
2943           yv12_fb[cm->lst_fb_idx].flags |= VP8_ALTR_FRAME;
2944           yv12_fb[cm->alt_fb_idx].flags &= ~VP8_ALTR_FRAME;
2945           cm->alt_fb_idx = cm->lst_fb_idx;
2946
2947           cpi->current_ref_frames[ALTREF_FRAME] =
2948               cpi->current_ref_frames[LAST_FRAME];
2949         }
2950       } else /* if (cm->copy_buffer_to_arf == 2) */
2951       {
2952         if (cm->alt_fb_idx != cm->gld_fb_idx) {
2953           yv12_fb[cm->gld_fb_idx].flags |= VP8_ALTR_FRAME;
2954           yv12_fb[cm->alt_fb_idx].flags &= ~VP8_ALTR_FRAME;
2955           cm->alt_fb_idx = cm->gld_fb_idx;
2956
2957           cpi->current_ref_frames[ALTREF_FRAME] =
2958               cpi->current_ref_frames[GOLDEN_FRAME];
2959         }
2960       }
2961     }
2962
2963     if (cm->refresh_golden_frame) {
2964       assert(!cm->copy_buffer_to_gf);
2965
2966       cm->yv12_fb[cm->new_fb_idx].flags |= VP8_GOLD_FRAME;
2967       cm->yv12_fb[cm->gld_fb_idx].flags &= ~VP8_GOLD_FRAME;
2968       cm->gld_fb_idx = cm->new_fb_idx;
2969
2970       cpi->current_ref_frames[GOLDEN_FRAME] = cm->current_video_frame;
2971     } else if (cm->copy_buffer_to_gf) {
2972       assert(!(cm->copy_buffer_to_arf & ~0x3));
2973
2974       if (cm->copy_buffer_to_gf == 1) {
2975         if (cm->gld_fb_idx != cm->lst_fb_idx) {
2976           yv12_fb[cm->lst_fb_idx].flags |= VP8_GOLD_FRAME;
2977           yv12_fb[cm->gld_fb_idx].flags &= ~VP8_GOLD_FRAME;
2978           cm->gld_fb_idx = cm->lst_fb_idx;
2979
2980           cpi->current_ref_frames[GOLDEN_FRAME] =
2981               cpi->current_ref_frames[LAST_FRAME];
2982         }
2983       } else /* if (cm->copy_buffer_to_gf == 2) */
2984       {
2985         if (cm->alt_fb_idx != cm->gld_fb_idx) {
2986           yv12_fb[cm->alt_fb_idx].flags |= VP8_GOLD_FRAME;
2987           yv12_fb[cm->gld_fb_idx].flags &= ~VP8_GOLD_FRAME;
2988           cm->gld_fb_idx = cm->alt_fb_idx;
2989
2990           cpi->current_ref_frames[GOLDEN_FRAME] =
2991               cpi->current_ref_frames[ALTREF_FRAME];
2992         }
2993       }
2994     }
2995   }
2996
2997   if (cm->refresh_last_frame) {
2998     cm->yv12_fb[cm->new_fb_idx].flags |= VP8_LAST_FRAME;
2999     cm->yv12_fb[cm->lst_fb_idx].flags &= ~VP8_LAST_FRAME;
3000     cm->lst_fb_idx = cm->new_fb_idx;
3001
3002     cpi->current_ref_frames[LAST_FRAME] = cm->current_video_frame;
3003   }
3004
3005 #if CONFIG_TEMPORAL_DENOISING
3006   if (cpi->oxcf.noise_sensitivity) {
3007     /* we shouldn't have to keep multiple copies as we know in advance which
3008      * buffer we should start - for now to get something up and running
3009      * I've chosen to copy the buffers
3010      */
3011     if (cm->frame_type == KEY_FRAME) {
3012       int i;
3013       for (i = LAST_FRAME; i < MAX_REF_FRAMES; ++i)
3014         vp8_yv12_copy_frame(cpi->Source, &cpi->denoiser.yv12_running_avg[i]);
3015     } else /* For non key frames */
3016     {
3017       vp8_yv12_extend_frame_borders(
3018           &cpi->denoiser.yv12_running_avg[INTRA_FRAME]);
3019
3020       if (cm->refresh_alt_ref_frame || cm->copy_buffer_to_arf) {
3021         vp8_yv12_copy_frame(&cpi->denoiser.yv12_running_avg[INTRA_FRAME],
3022                             &cpi->denoiser.yv12_running_avg[ALTREF_FRAME]);
3023       }
3024       if (cm->refresh_golden_frame || cm->copy_buffer_to_gf) {
3025         vp8_yv12_copy_frame(&cpi->denoiser.yv12_running_avg[INTRA_FRAME],
3026                             &cpi->denoiser.yv12_running_avg[GOLDEN_FRAME]);
3027       }
3028       if (cm->refresh_last_frame) {
3029         vp8_yv12_copy_frame(&cpi->denoiser.yv12_running_avg[INTRA_FRAME],
3030                             &cpi->denoiser.yv12_running_avg[LAST_FRAME]);
3031       }
3032     }
3033     if (cpi->oxcf.noise_sensitivity == 4)
3034       vp8_yv12_copy_frame(cpi->Source, &cpi->denoiser.yv12_last_source);
3035   }
3036 #endif
3037 }
3038
3039 static int measure_square_diff_partial(YV12_BUFFER_CONFIG *source,
3040                                        YV12_BUFFER_CONFIG *dest,
3041                                        VP8_COMP *cpi) {
3042   int i, j;
3043   int Total = 0;
3044   int num_blocks = 0;
3045   int skip = 2;
3046   int min_consec_zero_last = 10;
3047   int tot_num_blocks = (source->y_height * source->y_width) >> 8;
3048   unsigned char *src = source->y_buffer;
3049   unsigned char *dst = dest->y_buffer;
3050
3051   /* Loop through the Y plane, every |skip| blocks along rows and colmumns,
3052    * summing the square differences, and only for blocks that have been
3053    * zero_last mode at least |x| frames in a row.
3054    */
3055   for (i = 0; i < source->y_height; i += 16 * skip) {
3056     int block_index_row = (i >> 4) * cpi->common.mb_cols;
3057     for (j = 0; j < source->y_width; j += 16 * skip) {
3058       int index = block_index_row + (j >> 4);
3059       if (cpi->consec_zero_last[index] >= min_consec_zero_last) {
3060         unsigned int sse;
3061         Total += vpx_mse16x16(src + j, source->y_stride, dst + j,
3062                               dest->y_stride, &sse);
3063         num_blocks++;
3064       }
3065     }
3066     src += 16 * skip * source->y_stride;
3067     dst += 16 * skip * dest->y_stride;
3068   }
3069   // Only return non-zero if we have at least ~1/16 samples for estimate.
3070   if (num_blocks > (tot_num_blocks >> 4)) {
3071     assert(num_blocks != 0);
3072     return (Total / num_blocks);
3073   } else {
3074     return 0;
3075   }
3076 }
3077
3078 #if CONFIG_TEMPORAL_DENOISING
3079 static void process_denoiser_mode_change(VP8_COMP *cpi) {
3080   const VP8_COMMON *const cm = &cpi->common;
3081   int i, j;
3082   int total = 0;
3083   int num_blocks = 0;
3084   // Number of blocks skipped along row/column in computing the
3085   // nmse (normalized mean square error) of source.
3086   int skip = 2;
3087   // Only select blocks for computing nmse that have been encoded
3088   // as ZERO LAST min_consec_zero_last frames in a row.
3089   // Scale with number of temporal layers.
3090   int min_consec_zero_last = 12 / cpi->oxcf.number_of_layers;
3091   // Decision is tested for changing the denoising mode every
3092   // num_mode_change times this function is called. Note that this
3093   // function called every 8 frames, so (8 * num_mode_change) is number
3094   // of frames where denoising mode change is tested for switch.
3095   int num_mode_change = 20;
3096   // Framerate factor, to compensate for larger mse at lower framerates.
3097   // Use ref_framerate, which is full source framerate for temporal layers.
3098   // TODO(marpan): Adjust this factor.
3099   int fac_framerate = cpi->ref_framerate < 25.0f ? 80 : 100;
3100   int tot_num_blocks = cm->mb_rows * cm->mb_cols;
3101   int ystride = cpi->Source->y_stride;
3102   unsigned char *src = cpi->Source->y_buffer;
3103   unsigned char *dst = cpi->denoiser.yv12_last_source.y_buffer;
3104   static const unsigned char const_source[16] = { 128, 128, 128, 128, 128, 128,
3105                                                   128, 128, 128, 128, 128, 128,
3106                                                   128, 128, 128, 128 };
3107   int bandwidth = (int)(cpi->target_bandwidth);
3108   // For temporal layers, use full bandwidth (top layer).
3109   if (cpi->oxcf.number_of_layers > 1) {
3110     LAYER_CONTEXT *lc = &cpi->layer_context[cpi->oxcf.number_of_layers - 1];
3111     bandwidth = (int)(lc->target_bandwidth);
3112   }
3113   // Loop through the Y plane, every skip blocks along rows and columns,
3114   // summing the normalized mean square error, only for blocks that have
3115   // been encoded as ZEROMV LAST at least min_consec_zero_last least frames in
3116   // a row and have small sum difference between current and previous frame.
3117   // Normalization here is by the contrast of the current frame block.
3118   for (i = 0; i < cm->Height; i += 16 * skip) {
3119     int block_index_row = (i >> 4) * cm->mb_cols;
3120     for (j = 0; j < cm->Width; j += 16 * skip) {
3121       int index = block_index_row + (j >> 4);
3122       if (cpi->consec_zero_last[index] >= min_consec_zero_last) {
3123         unsigned int sse;
3124         const unsigned int var =
3125             vpx_variance16x16(src + j, ystride, dst + j, ystride, &sse);
3126         // Only consider this block as valid for noise measurement
3127         // if the sum_diff average of the current and previous frame
3128         // is small (to avoid effects from lighting change).
3129         if ((sse - var) < 128) {
3130           unsigned int sse2;
3131           const unsigned int act =
3132               vpx_variance16x16(src + j, ystride, const_source, 0, &sse2);
3133           if (act > 0) total += sse / act;
3134           num_blocks++;
3135         }
3136       }
3137     }
3138     src += 16 * skip * ystride;
3139     dst += 16 * skip * ystride;
3140   }
3141   total = total * fac_framerate / 100;
3142
3143   // Only consider this frame as valid sample if we have computed nmse over
3144   // at least ~1/16 blocks, and Total > 0 (Total == 0 can happen if the
3145   // application inputs duplicate frames, or contrast is all zero).
3146   if (total > 0 && (num_blocks > (tot_num_blocks >> 4))) {
3147     // Update the recursive mean square source_diff.
3148     total = (total << 8) / num_blocks;
3149     if (cpi->denoiser.nmse_source_diff_count == 0) {
3150       // First sample in new interval.
3151       cpi->denoiser.nmse_source_diff = total;
3152       cpi->denoiser.qp_avg = cm->base_qindex;
3153     } else {
3154       // For subsequent samples, use average with weight ~1/4 for new sample.
3155       cpi->denoiser.nmse_source_diff =
3156           (int)((total + 3 * cpi->denoiser.nmse_source_diff) >> 2);
3157       cpi->denoiser.qp_avg =
3158           (int)((cm->base_qindex + 3 * cpi->denoiser.qp_avg) >> 2);
3159     }
3160     cpi->denoiser.nmse_source_diff_count++;
3161   }
3162   // Check for changing the denoiser mode, when we have obtained #samples =
3163   // num_mode_change. Condition the change also on the bitrate and QP.
3164   if (cpi->denoiser.nmse_source_diff_count == num_mode_change) {
3165     // Check for going up: from normal to aggressive mode.
3166     if ((cpi->denoiser.denoiser_mode == kDenoiserOnYUV) &&
3167         (cpi->denoiser.nmse_source_diff >
3168          cpi->denoiser.threshold_aggressive_mode) &&
3169         (cpi->denoiser.qp_avg < cpi->denoiser.qp_threshold_up &&
3170          bandwidth > cpi->denoiser.bitrate_threshold)) {
3171       vp8_denoiser_set_parameters(&cpi->denoiser, kDenoiserOnYUVAggressive);
3172     } else {
3173       // Check for going down: from aggressive to normal mode.
3174       if (((cpi->denoiser.denoiser_mode == kDenoiserOnYUVAggressive) &&
3175            (cpi->denoiser.nmse_source_diff <
3176             cpi->denoiser.threshold_aggressive_mode)) ||
3177           ((cpi->denoiser.denoiser_mode == kDenoiserOnYUVAggressive) &&
3178            (cpi->denoiser.qp_avg > cpi->denoiser.qp_threshold_down ||
3179             bandwidth < cpi->denoiser.bitrate_threshold))) {
3180         vp8_denoiser_set_parameters(&cpi->denoiser, kDenoiserOnYUV);
3181       }
3182     }
3183     // Reset metric and counter for next interval.
3184     cpi->denoiser.nmse_source_diff = 0;
3185     cpi->denoiser.qp_avg = 0;
3186     cpi->denoiser.nmse_source_diff_count = 0;
3187   }
3188 }
3189 #endif
3190
3191 void vp8_loopfilter_frame(VP8_COMP *cpi, VP8_COMMON *cm) {
3192   const FRAME_TYPE frame_type = cm->frame_type;
3193
3194   int update_any_ref_buffers = 1;
3195   if (cpi->common.refresh_last_frame == 0 &&
3196       cpi->common.refresh_golden_frame == 0 &&
3197       cpi->common.refresh_alt_ref_frame == 0) {
3198     update_any_ref_buffers = 0;
3199   }
3200
3201   if (cm->no_lpf) {
3202     cm->filter_level = 0;
3203   } else {
3204     struct vpx_usec_timer timer;
3205
3206     vpx_clear_system_state();
3207
3208     vpx_usec_timer_start(&timer);
3209     if (cpi->sf.auto_filter == 0) {
3210 #if CONFIG_TEMPORAL_DENOISING
3211       if (cpi->oxcf.noise_sensitivity && cm->frame_type != KEY_FRAME) {
3212         // Use the denoised buffer for selecting base loop filter level.
3213         // Denoised signal for current frame is stored in INTRA_FRAME.
3214         // No denoising on key frames.
3215         vp8cx_pick_filter_level_fast(
3216             &cpi->denoiser.yv12_running_avg[INTRA_FRAME], cpi);
3217       } else {
3218         vp8cx_pick_filter_level_fast(cpi->Source, cpi);
3219       }
3220 #else
3221       vp8cx_pick_filter_level_fast(cpi->Source, cpi);
3222 #endif
3223     } else {
3224 #if CONFIG_TEMPORAL_DENOISING
3225       if (cpi->oxcf.noise_sensitivity && cm->frame_type != KEY_FRAME) {
3226         // Use the denoised buffer for selecting base loop filter level.
3227         // Denoised signal for current frame is stored in INTRA_FRAME.
3228         // No denoising on key frames.
3229         vp8cx_pick_filter_level(&cpi->denoiser.yv12_running_avg[INTRA_FRAME],
3230                                 cpi);
3231       } else {
3232         vp8cx_pick_filter_level(cpi->Source, cpi);
3233       }
3234 #else
3235       vp8cx_pick_filter_level(cpi->Source, cpi);
3236 #endif
3237     }
3238
3239     if (cm->filter_level > 0) {
3240       vp8cx_set_alt_lf_level(cpi, cm->filter_level);
3241     }
3242
3243     vpx_usec_timer_mark(&timer);
3244     cpi->time_pick_lpf += vpx_usec_timer_elapsed(&timer);
3245   }
3246
3247 #if CONFIG_MULTITHREAD
3248   if (cpi->b_multi_threaded) {
3249     sem_post(&cpi->h_event_end_lpf); /* signal that we have set filter_level */
3250   }
3251 #endif
3252
3253   // No need to apply loop-filter if the encoded frame does not update
3254   // any reference buffers.
3255   if (cm->filter_level > 0 && update_any_ref_buffers) {
3256     vp8_loop_filter_frame(cm, &cpi->mb.e_mbd, frame_type);
3257   }
3258
3259   vp8_yv12_extend_frame_borders(cm->frame_to_show);
3260 }
3261
3262 static void encode_frame_to_data_rate(VP8_COMP *cpi, size_t *size,
3263                                       unsigned char *dest,
3264                                       unsigned char *dest_end,
3265                                       unsigned int *frame_flags) {
3266   int Q;
3267   int frame_over_shoot_limit;
3268   int frame_under_shoot_limit;
3269
3270   int Loop = 0;
3271   int loop_count;
3272
3273   VP8_COMMON *cm = &cpi->common;
3274   int active_worst_qchanged = 0;
3275
3276 #if !CONFIG_REALTIME_ONLY
3277   int q_low;
3278   int q_high;
3279   int zbin_oq_high;
3280   int zbin_oq_low = 0;
3281   int top_index;
3282   int bottom_index;
3283   int overshoot_seen = 0;
3284   int undershoot_seen = 0;
3285 #endif
3286
3287   int drop_mark = (int)(cpi->oxcf.drop_frames_water_mark *
3288                         cpi->oxcf.optimal_buffer_level / 100);
3289   int drop_mark75 = drop_mark * 2 / 3;
3290   int drop_mark50 = drop_mark / 4;
3291   int drop_mark25 = drop_mark / 8;
3292
3293   /* Clear down mmx registers to allow floating point in what follows */
3294   vpx_clear_system_state();
3295
3296   if (cpi->force_next_frame_intra) {
3297     cm->frame_type = KEY_FRAME; /* delayed intra frame */
3298     cpi->force_next_frame_intra = 0;
3299   }
3300
3301   /* For an alt ref frame in 2 pass we skip the call to the second pass
3302    * function that sets the target bandwidth
3303    */
3304   switch (cpi->pass) {
3305 #if !CONFIG_REALTIME_ONLY
3306     case 2:
3307       if (cpi->common.refresh_alt_ref_frame) {
3308         /* Per frame bit target for the alt ref frame */
3309         cpi->per_frame_bandwidth = cpi->twopass.gf_bits;
3310         /* per second target bitrate */
3311         cpi->target_bandwidth =
3312             (int)(cpi->twopass.gf_bits * cpi->output_framerate);
3313       }
3314       break;
3315 #endif  // !CONFIG_REALTIME_ONLY
3316     default:
3317       cpi->per_frame_bandwidth =
3318           (int)(cpi->target_bandwidth / cpi->output_framerate);
3319       break;
3320   }
3321
3322   /* Default turn off buffer to buffer copying */
3323   cm->copy_buffer_to_gf = 0;
3324   cm->copy_buffer_to_arf = 0;
3325
3326   /* Clear zbin over-quant value and mode boost values. */
3327   cpi->mb.zbin_over_quant = 0;
3328   cpi->mb.zbin_mode_boost = 0;
3329
3330   /* Enable or disable mode based tweaking of the zbin
3331    * For 2 Pass Only used where GF/ARF prediction quality
3332    * is above a threshold
3333    */
3334   cpi->mb.zbin_mode_boost_enabled = 1;
3335   if (cpi->pass == 2) {
3336     if (cpi->gfu_boost <= 400) {
3337       cpi->mb.zbin_mode_boost_enabled = 0;
3338     }
3339   }
3340
3341   /* Current default encoder behaviour for the altref sign bias */
3342   if (cpi->source_alt_ref_active) {
3343     cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = 1;
3344   } else {
3345     cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = 0;
3346   }
3347
3348   /* Check to see if a key frame is signaled
3349    * For two pass with auto key frame enabled cm->frame_type may already
3350    * be set, but not for one pass.
3351    */
3352   if ((cm->current_video_frame == 0) || (cm->frame_flags & FRAMEFLAGS_KEY) ||
3353       (cpi->oxcf.auto_key &&
3354        (cpi->frames_since_key % cpi->key_frame_frequency == 0))) {
3355     /* Key frame from VFW/auto-keyframe/first frame */
3356     cm->frame_type = KEY_FRAME;
3357 #if CONFIG_TEMPORAL_DENOISING
3358     if (cpi->oxcf.noise_sensitivity == 4) {
3359       // For adaptive mode, reset denoiser to normal mode on key frame.
3360       vp8_denoiser_set_parameters(&cpi->denoiser, kDenoiserOnYUV);
3361     }
3362 #endif
3363   }
3364
3365 #if CONFIG_MULTI_RES_ENCODING
3366   if (cpi->oxcf.mr_total_resolutions > 1) {
3367     LOWER_RES_FRAME_INFO *low_res_frame_info =
3368         (LOWER_RES_FRAME_INFO *)cpi->oxcf.mr_low_res_mode_info;
3369
3370     if (cpi->oxcf.mr_encoder_id) {
3371       // TODO(marpan): This constraint shouldn't be needed, as we would like
3372       // to allow for key frame setting (forced or periodic) defined per
3373       // spatial layer. For now, keep this in.
3374       cm->frame_type = low_res_frame_info->frame_type;
3375
3376       // Check if lower resolution is available for motion vector reuse.
3377       if (cm->frame_type != KEY_FRAME) {
3378         cpi->mr_low_res_mv_avail = 1;
3379         cpi->mr_low_res_mv_avail &= !(low_res_frame_info->is_frame_dropped);
3380
3381         if (cpi->ref_frame_flags & VP8_LAST_FRAME)
3382           cpi->mr_low_res_mv_avail &=
3383               (cpi->current_ref_frames[LAST_FRAME] ==
3384                low_res_frame_info->low_res_ref_frames[LAST_FRAME]);
3385
3386         if (cpi->ref_frame_flags & VP8_GOLD_FRAME)
3387           cpi->mr_low_res_mv_avail &=
3388               (cpi->current_ref_frames[GOLDEN_FRAME] ==
3389                low_res_frame_info->low_res_ref_frames[GOLDEN_FRAME]);
3390
3391         // Don't use altref to determine whether low res is available.
3392         // TODO (marpan): Should we make this type of condition on a
3393         // per-reference frame basis?
3394         /*
3395         if (cpi->ref_frame_flags & VP8_ALTR_FRAME)
3396             cpi->mr_low_res_mv_avail &= (cpi->current_ref_frames[ALTREF_FRAME]
3397                      == low_res_frame_info->low_res_ref_frames[ALTREF_FRAME]);
3398         */
3399       }
3400     }
3401
3402     // On a key frame: For the lowest resolution, keep track of the key frame
3403     // counter value. For the higher resolutions, reset the current video
3404     // frame counter to that of the lowest resolution.
3405     // This is done to the handle the case where we may stop/start encoding
3406     // higher layer(s). The restart-encoding of higher layer is only signaled
3407     // by a key frame for now.
3408     // TODO (marpan): Add flag to indicate restart-encoding of higher layer.
3409     if (cm->frame_type == KEY_FRAME) {
3410       if (cpi->oxcf.mr_encoder_id) {
3411         // If the initial starting value of the buffer level is zero (this can
3412         // happen because we may have not started encoding this higher stream),
3413         // then reset it to non-zero value based on |starting_buffer_level|.
3414         if (cpi->common.current_video_frame == 0 && cpi->buffer_level == 0) {
3415           unsigned int i;
3416           cpi->bits_off_target = cpi->oxcf.starting_buffer_level;
3417           cpi->buffer_level = cpi->oxcf.starting_buffer_level;
3418           for (i = 0; i < cpi->oxcf.number_of_layers; ++i) {
3419             LAYER_CONTEXT *lc = &cpi->layer_context[i];
3420             lc->bits_off_target = lc->starting_buffer_level;
3421             lc->buffer_level = lc->starting_buffer_level;
3422           }
3423         }
3424         cpi->common.current_video_frame =
3425             low_res_frame_info->key_frame_counter_value;
3426       } else {
3427         low_res_frame_info->key_frame_counter_value =
3428             cpi->common.current_video_frame;
3429       }
3430     }
3431   }
3432 #endif
3433
3434   // Find the reference frame closest to the current frame.
3435   cpi->closest_reference_frame = LAST_FRAME;
3436   if (cm->frame_type != KEY_FRAME) {
3437     int i;
3438     MV_REFERENCE_FRAME closest_ref = INTRA_FRAME;
3439     if (cpi->ref_frame_flags & VP8_LAST_FRAME) {
3440       closest_ref = LAST_FRAME;
3441     } else if (cpi->ref_frame_flags & VP8_GOLD_FRAME) {
3442       closest_ref = GOLDEN_FRAME;
3443     } else if (cpi->ref_frame_flags & VP8_ALTR_FRAME) {
3444       closest_ref = ALTREF_FRAME;
3445     }
3446     for (i = 1; i <= 3; ++i) {
3447       vpx_ref_frame_type_t ref_frame_type =
3448           (vpx_ref_frame_type_t)((i == 3) ? 4 : i);
3449       if (cpi->ref_frame_flags & ref_frame_type) {
3450         if ((cm->current_video_frame - cpi->current_ref_frames[i]) <
3451             (cm->current_video_frame - cpi->current_ref_frames[closest_ref])) {
3452           closest_ref = i;
3453         }
3454       }
3455     }
3456     cpi->closest_reference_frame = closest_ref;
3457   }
3458
3459   /* Set various flags etc to special state if it is a key frame */
3460   if (cm->frame_type == KEY_FRAME) {
3461     int i;
3462
3463     // Set the loop filter deltas and segmentation map update
3464     setup_features(cpi);
3465
3466     /* The alternate reference frame cannot be active for a key frame */
3467     cpi->source_alt_ref_active = 0;
3468
3469     /* Reset the RD threshold multipliers to default of * 1 (128) */
3470     for (i = 0; i < MAX_MODES; ++i) {
3471       cpi->mb.rd_thresh_mult[i] = 128;
3472     }
3473
3474     // Reset the zero_last counter to 0 on key frame.
3475     memset(cpi->consec_zero_last, 0, cm->mb_rows * cm->mb_cols);
3476     memset(cpi->consec_zero_last_mvbias, 0,
3477            (cpi->common.mb_rows * cpi->common.mb_cols));
3478   }
3479
3480 #if 0
3481     /* Experimental code for lagged compress and one pass
3482      * Initialise one_pass GF frames stats
3483      * Update stats used for GF selection
3484      */
3485     {
3486         cpi->one_pass_frame_index = cm->current_video_frame % MAX_LAG_BUFFERS;
3487
3488         cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frames_so_far = 0;
3489         cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_intra_error = 0.0;
3490         cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_coded_error = 0.0;
3491         cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_pcnt_inter = 0.0;
3492         cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_pcnt_motion = 0.0;
3493         cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvr = 0.0;
3494         cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvr_abs = 0.0;
3495         cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvc = 0.0;
3496         cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvc_abs = 0.0;
3497     }
3498 #endif
3499
3500   update_rd_ref_frame_probs(cpi);
3501
3502   if (cpi->drop_frames_allowed) {
3503     /* The reset to decimation 0 is only done here for one pass.
3504      * Once it is set two pass leaves decimation on till the next kf.
3505      */
3506     if ((cpi->buffer_level > drop_mark) && (cpi->decimation_factor > 0)) {
3507       cpi->decimation_factor--;
3508     }
3509
3510     if (cpi->buffer_level > drop_mark75 && cpi->decimation_factor > 0) {
3511       cpi->decimation_factor = 1;
3512
3513     } else if (cpi->buffer_level < drop_mark25 &&
3514                (cpi->decimation_factor == 2 || cpi->decimation_factor == 3)) {
3515       cpi->decimation_factor = 3;
3516     } else if (cpi->buffer_level < drop_mark50 &&
3517                (cpi->decimation_factor == 1 || cpi->decimation_factor == 2)) {
3518       cpi->decimation_factor = 2;
3519     } else if (cpi->buffer_level < drop_mark75 &&
3520                (cpi->decimation_factor == 0 || cpi->decimation_factor == 1)) {
3521       cpi->decimation_factor = 1;
3522     }
3523   }
3524
3525   /* The following decimates the frame rate according to a regular
3526    * pattern (i.e. to 1/2 or 2/3 frame rate) This can be used to help
3527    * prevent buffer under-run in CBR mode. Alternatively it might be
3528    * desirable in some situations to drop frame rate but throw more bits
3529    * at each frame.
3530    *
3531    * Note that dropping a key frame can be problematic if spatial
3532    * resampling is also active
3533    */
3534   if (cpi->decimation_factor > 0) {
3535     switch (cpi->decimation_factor) {
3536       case 1:
3537         cpi->per_frame_bandwidth = cpi->per_frame_bandwidth * 3 / 2;
3538         break;
3539       case 2:
3540         cpi->per_frame_bandwidth = cpi->per_frame_bandwidth * 5 / 4;
3541         break;
3542       case 3:
3543         cpi->per_frame_bandwidth = cpi->per_frame_bandwidth * 5 / 4;
3544         break;
3545     }
3546
3547     /* Note that we should not throw out a key frame (especially when
3548      * spatial resampling is enabled).
3549      */
3550     if (cm->frame_type == KEY_FRAME) {
3551       cpi->decimation_count = cpi->decimation_factor;
3552     } else if (cpi->decimation_count > 0) {
3553       cpi->decimation_count--;
3554
3555       cpi->bits_off_target += cpi->av_per_frame_bandwidth;
3556       if (cpi->bits_off_target > cpi->oxcf.maximum_buffer_size) {
3557         cpi->bits_off_target = cpi->oxcf.maximum_buffer_size;
3558       }
3559
3560 #if CONFIG_MULTI_RES_ENCODING
3561       vp8_store_drop_frame_info(cpi);
3562 #endif
3563
3564       cm->current_video_frame++;
3565       cpi->frames_since_key++;
3566       // We advance the temporal pattern for dropped frames.
3567       cpi->temporal_pattern_counter++;
3568
3569 #if CONFIG_INTERNAL_STATS
3570       cpi->count++;
3571 #endif
3572
3573       cpi->buffer_level = cpi->bits_off_target;
3574
3575       if (cpi->oxcf.number_of_layers > 1) {
3576         unsigned int i;
3577
3578         /* Propagate bits saved by dropping the frame to higher
3579          * layers
3580          */
3581         for (i = cpi->current_layer + 1; i < cpi->oxcf.number_of_layers; ++i) {
3582           LAYER_CONTEXT *lc = &cpi->layer_context[i];
3583           lc->bits_off_target += (int)(lc->target_bandwidth / lc->framerate);
3584           if (lc->bits_off_target > lc->maximum_buffer_size) {
3585             lc->bits_off_target = lc->maximum_buffer_size;
3586           }
3587           lc->buffer_level = lc->bits_off_target;
3588         }
3589       }
3590
3591       return;
3592     } else {
3593       cpi->decimation_count = cpi->decimation_factor;
3594     }
3595   } else {
3596     cpi->decimation_count = 0;
3597   }
3598
3599   /* Decide how big to make the frame */
3600   if (!vp8_pick_frame_size(cpi)) {
3601 /*TODO: 2 drop_frame and return code could be put together. */
3602 #if CONFIG_MULTI_RES_ENCODING
3603     vp8_store_drop_frame_info(cpi);
3604 #endif
3605     cm->current_video_frame++;
3606     cpi->frames_since_key++;
3607     // We advance the temporal pattern for dropped frames.
3608     cpi->temporal_pattern_counter++;
3609     return;
3610   }
3611
3612   /* Reduce active_worst_allowed_q for CBR if our buffer is getting too full.
3613    * This has a knock on effect on active best quality as well.
3614    * For CBR if the buffer reaches its maximum level then we can no longer
3615    * save up bits for later frames so we might as well use them up
3616    * on the current frame.
3617    */
3618   if ((cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) &&
3619       (cpi->buffer_level >= cpi->oxcf.optimal_buffer_level) &&
3620       cpi->buffered_mode) {
3621     /* Max adjustment is 1/4 */
3622     int Adjustment = cpi->active_worst_quality / 4;
3623
3624     if (Adjustment) {
3625       int buff_lvl_step;
3626
3627       if (cpi->buffer_level < cpi->oxcf.maximum_buffer_size) {
3628         buff_lvl_step = (int)((cpi->oxcf.maximum_buffer_size -
3629                                cpi->oxcf.optimal_buffer_level) /
3630                               Adjustment);
3631
3632         if (buff_lvl_step) {
3633           Adjustment =
3634               (int)((cpi->buffer_level - cpi->oxcf.optimal_buffer_level) /
3635                     buff_lvl_step);
3636         } else {
3637           Adjustment = 0;
3638         }
3639       }
3640
3641       cpi->active_worst_quality -= Adjustment;
3642
3643       if (cpi->active_worst_quality < cpi->active_best_quality) {
3644         cpi->active_worst_quality = cpi->active_best_quality;
3645       }
3646     }
3647   }
3648
3649   /* Set an active best quality and if necessary active worst quality
3650    * There is some odd behavior for one pass here that needs attention.
3651    */
3652   if ((cpi->pass == 2) || (cpi->ni_frames > 150)) {
3653     vpx_clear_system_state();
3654
3655     Q = cpi->active_worst_quality;
3656
3657     if (cm->frame_type == KEY_FRAME) {
3658       if (cpi->pass == 2) {
3659         if (cpi->gfu_boost > 600) {
3660           cpi->active_best_quality = kf_low_motion_minq[Q];
3661         } else {
3662           cpi->active_best_quality = kf_high_motion_minq[Q];
3663         }
3664
3665         /* Special case for key frames forced because we have reached
3666          * the maximum key frame interval. Here force the Q to a range
3667          * based on the ambient Q to reduce the risk of popping
3668          */
3669         if (cpi->this_key_frame_forced) {
3670           if (cpi->active_best_quality > cpi->avg_frame_qindex * 7 / 8) {
3671             cpi->active_best_quality = cpi->avg_frame_qindex * 7 / 8;
3672           } else if (cpi->active_best_quality<cpi->avg_frame_qindex>> 2) {
3673             cpi->active_best_quality = cpi->avg_frame_qindex >> 2;
3674           }
3675         }
3676       }
3677       /* One pass more conservative */
3678       else {
3679         cpi->active_best_quality = kf_high_motion_minq[Q];
3680       }
3681     }
3682
3683     else if (cpi->oxcf.number_of_layers == 1 &&
3684              (cm->refresh_golden_frame || cpi->common.refresh_alt_ref_frame)) {
3685       /* Use the lower of cpi->active_worst_quality and recent
3686        * average Q as basis for GF/ARF Q limit unless last frame was
3687        * a key frame.
3688        */
3689       if ((cpi->frames_since_key > 1) &&
3690           (cpi->avg_frame_qindex < cpi->active_worst_quality)) {
3691         Q = cpi->avg_frame_qindex;
3692       }
3693
3694       /* For constrained quality dont allow Q less than the cq level */
3695       if ((cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) &&
3696           (Q < cpi->cq_target_quality)) {
3697         Q = cpi->cq_target_quality;
3698       }
3699
3700       if (cpi->pass == 2) {
3701         if (cpi->gfu_boost > 1000) {
3702           cpi->active_best_quality = gf_low_motion_minq[Q];
3703         } else if (cpi->gfu_boost < 400) {
3704           cpi->active_best_quality = gf_high_motion_minq[Q];
3705         } else {
3706           cpi->active_best_quality = gf_mid_motion_minq[Q];
3707         }
3708
3709         /* Constrained quality use slightly lower active best. */
3710         if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) {
3711           cpi->active_best_quality = cpi->active_best_quality * 15 / 16;
3712         }
3713       }
3714       /* One pass more conservative */
3715       else {
3716         cpi->active_best_quality = gf_high_motion_minq[Q];
3717       }
3718     } else {
3719       cpi->active_best_quality = inter_minq[Q];
3720
3721       /* For the constant/constrained quality mode we dont want
3722        * q to fall below the cq level.
3723        */
3724       if ((cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) &&
3725           (cpi->active_best_quality < cpi->cq_target_quality)) {
3726         /* If we are strongly undershooting the target rate in the last
3727          * frames then use the user passed in cq value not the auto
3728          * cq value.
3729          */
3730         if (cpi->rolling_actual_bits < cpi->min_frame_bandwidth) {
3731           cpi->active_best_quality = cpi->oxcf.cq_level;
3732         } else {
3733           cpi->active_best_quality = cpi->cq_target_quality;
3734         }
3735       }
3736     }
3737
3738     /* If CBR and the buffer is as full then it is reasonable to allow
3739      * higher quality on the frames to prevent bits just going to waste.
3740      */
3741     if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
3742       /* Note that the use of >= here elliminates the risk of a devide
3743        * by 0 error in the else if clause
3744        */
3745       if (cpi->buffer_level >= cpi->oxcf.maximum_buffer_size) {
3746         cpi->active_best_quality = cpi->best_quality;
3747
3748       } else if (cpi->buffer_level > cpi->oxcf.optimal_buffer_level) {
3749         int Fraction =
3750             (int)(((cpi->buffer_level - cpi->oxcf.optimal_buffer_level) * 128) /
3751                   (cpi->oxcf.maximum_buffer_size -
3752                    cpi->oxcf.optimal_buffer_level));
3753         int min_qadjustment =
3754             ((cpi->active_best_quality - cpi->best_quality) * Fraction) / 128;
3755
3756         cpi->active_best_quality -= min_qadjustment;
3757       }
3758     }
3759   }
3760   /* Make sure constrained quality mode limits are adhered to for the first
3761    * few frames of one pass encodes
3762    */
3763   else if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) {
3764     if ((cm->frame_type == KEY_FRAME) || cm->refresh_golden_frame ||
3765         cpi->common.refresh_alt_ref_frame) {
3766       cpi->active_best_quality = cpi->best_quality;
3767     } else if (cpi->active_best_quality < cpi->cq_target_quality) {
3768       cpi->active_best_quality = cpi->cq_target_quality;
3769     }
3770   }
3771
3772   /* Clip the active best and worst quality values to limits */
3773   if (cpi->active_worst_quality > cpi->worst_quality) {
3774     cpi->active_worst_quality = cpi->worst_quality;
3775   }
3776
3777   if (cpi->active_best_quality < cpi->best_quality) {
3778     cpi->active_best_quality = cpi->best_quality;
3779   }
3780
3781   if (cpi->active_worst_quality < cpi->active_best_quality) {
3782     cpi->active_worst_quality = cpi->active_best_quality;
3783   }
3784
3785   /* Determine initial Q to try */
3786   Q = vp8_regulate_q(cpi, cpi->this_frame_target);
3787
3788 #if !CONFIG_REALTIME_ONLY
3789
3790   /* Set highest allowed value for Zbin over quant */
3791   if (cm->frame_type == KEY_FRAME) {
3792     zbin_oq_high = 0;
3793   } else if ((cpi->oxcf.number_of_layers == 1) &&
3794              ((cm->refresh_alt_ref_frame ||
3795                (cm->refresh_golden_frame && !cpi->source_alt_ref_active)))) {
3796     zbin_oq_high = 16;
3797   } else {
3798     zbin_oq_high = ZBIN_OQ_MAX;
3799   }
3800 #endif
3801
3802   /* Setup background Q adjustment for error resilient mode.
3803    * For multi-layer encodes only enable this for the base layer.
3804   */
3805   if (cpi->cyclic_refresh_mode_enabled) {
3806     // Special case for screen_content_mode with golden frame updates.
3807     int disable_cr_gf =
3808         (cpi->oxcf.screen_content_mode == 2 && cm->refresh_golden_frame);
3809     if (cpi->current_layer == 0 && cpi->force_maxqp == 0 && !disable_cr_gf) {
3810       cyclic_background_refresh(cpi, Q, 0);
3811     } else {
3812       disable_segmentation(cpi);
3813     }
3814   }
3815
3816   vp8_compute_frame_size_bounds(cpi, &frame_under_shoot_limit,
3817                                 &frame_over_shoot_limit);
3818
3819 #if !CONFIG_REALTIME_ONLY
3820   /* Limit Q range for the adaptive loop. */
3821   bottom_index = cpi->active_best_quality;
3822   top_index = cpi->active_worst_quality;
3823   q_low = cpi->active_best_quality;
3824   q_high = cpi->active_worst_quality;
3825 #endif
3826
3827   vp8_save_coding_context(cpi);
3828
3829   loop_count = 0;
3830
3831   scale_and_extend_source(cpi->un_scaled_source, cpi);
3832
3833 #if CONFIG_TEMPORAL_DENOISING && CONFIG_POSTPROC
3834   // Option to apply spatial blur under the aggressive or adaptive
3835   // (temporal denoising) mode.
3836   if (cpi->oxcf.noise_sensitivity >= 3) {
3837     if (cpi->denoiser.denoise_pars.spatial_blur != 0) {
3838       vp8_de_noise(cm, cpi->Source, cpi->Source,
3839                    cpi->denoiser.denoise_pars.spatial_blur, 1, 0, 0);
3840     }
3841   }
3842 #endif
3843
3844 #if !(CONFIG_REALTIME_ONLY) && CONFIG_POSTPROC && !(CONFIG_TEMPORAL_DENOISING)
3845
3846   if (cpi->oxcf.noise_sensitivity > 0) {
3847     unsigned char *src;
3848     int l = 0;
3849
3850     switch (cpi->oxcf.noise_sensitivity) {
3851       case 1: l = 20; break;
3852       case 2: l = 40; break;
3853       case 3: l = 60; break;
3854       case 4: l = 80; break;
3855       case 5: l = 100; break;
3856       case 6: l = 150; break;
3857     }
3858
3859     if (cm->frame_type == KEY_FRAME) {
3860       vp8_de_noise(cm, cpi->Source, cpi->Source, l, 1, 0, 1);
3861     } else {
3862       vp8_de_noise(cm, cpi->Source, cpi->Source, l, 1, 0, 1);
3863
3864       src = cpi->Source->y_buffer;
3865
3866       if (cpi->Source->y_stride < 0) {
3867         src += cpi->Source->y_stride * (cpi->Source->y_height - 1);
3868       }
3869     }
3870   }
3871
3872 #endif
3873
3874 #ifdef OUTPUT_YUV_SRC
3875   vp8_write_yuv_frame(yuv_file, cpi->Source);
3876 #endif
3877
3878   do {
3879     vpx_clear_system_state();
3880
3881     vp8_set_quantizer(cpi, Q);
3882
3883     /* setup skip prob for costing in mode/mv decision */
3884     if (cpi->common.mb_no_coeff_skip) {
3885       cpi->prob_skip_false = cpi->base_skip_false_prob[Q];
3886
3887       if (cm->frame_type != KEY_FRAME) {
3888         if (cpi->common.refresh_alt_ref_frame) {
3889           if (cpi->last_skip_false_probs[2] != 0) {
3890             cpi->prob_skip_false = cpi->last_skip_false_probs[2];
3891           }
3892
3893           /*
3894                               if(cpi->last_skip_false_probs[2]!=0 && abs(Q-
3895              cpi->last_skip_probs_q[2])<=16 )
3896              cpi->prob_skip_false = cpi->last_skip_false_probs[2];
3897                               else if (cpi->last_skip_false_probs[2]!=0)
3898              cpi->prob_skip_false = (cpi->last_skip_false_probs[2]  +
3899              cpi->prob_skip_false ) / 2;
3900              */
3901         } else if (cpi->common.refresh_golden_frame) {
3902           if (cpi->last_skip_false_probs[1] != 0) {
3903             cpi->prob_skip_false = cpi->last_skip_false_probs[1];
3904           }
3905
3906           /*
3907                               if(cpi->last_skip_false_probs[1]!=0 && abs(Q-
3908              cpi->last_skip_probs_q[1])<=16 )
3909              cpi->prob_skip_false = cpi->last_skip_false_probs[1];
3910                               else if (cpi->last_skip_false_probs[1]!=0)
3911              cpi->prob_skip_false = (cpi->last_skip_false_probs[1]  +
3912              cpi->prob_skip_false ) / 2;
3913              */
3914         } else {
3915           if (cpi->last_skip_false_probs[0] != 0) {
3916             cpi->prob_skip_false = cpi->last_skip_false_probs[0];
3917           }
3918
3919           /*
3920           if(cpi->last_skip_false_probs[0]!=0 && abs(Q-
3921           cpi->last_skip_probs_q[0])<=16 )
3922               cpi->prob_skip_false = cpi->last_skip_false_probs[0];
3923           else if(cpi->last_skip_false_probs[0]!=0)
3924               cpi->prob_skip_false = (cpi->last_skip_false_probs[0]  +
3925           cpi->prob_skip_false ) / 2;
3926               */
3927         }
3928
3929         /* as this is for cost estimate, let's make sure it does not
3930          * go extreme eitehr way
3931          */
3932         if (cpi->prob_skip_false < 5) cpi->prob_skip_false = 5;
3933
3934         if (cpi->prob_skip_false > 250) cpi->prob_skip_false = 250;
3935
3936         if (cpi->oxcf.number_of_layers == 1 && cpi->is_src_frame_alt_ref) {
3937           cpi->prob_skip_false = 1;
3938         }
3939       }
3940
3941 #if 0
3942
3943             if (cpi->pass != 1)
3944             {
3945                 FILE *f = fopen("skip.stt", "a");
3946                 fprintf(f, "%d, %d, %4d ", cpi->common.refresh_golden_frame, cpi->common.refresh_alt_ref_frame, cpi->prob_skip_false);
3947                 fclose(f);
3948             }
3949
3950 #endif
3951     }
3952
3953     if (cm->frame_type == KEY_FRAME) {
3954       if (resize_key_frame(cpi)) {
3955         /* If the frame size has changed, need to reset Q, quantizer,
3956          * and background refresh.
3957          */
3958         Q = vp8_regulate_q(cpi, cpi->this_frame_target);
3959         if (cpi->cyclic_refresh_mode_enabled) {
3960           if (cpi->current_layer == 0) {
3961             cyclic_background_refresh(cpi, Q, 0);
3962           } else {
3963             disable_segmentation(cpi);
3964           }
3965         }
3966         // Reset the zero_last counter to 0 on key frame.
3967         memset(cpi->consec_zero_last, 0, cm->mb_rows * cm->mb_cols);
3968         memset(cpi->consec_zero_last_mvbias, 0,
3969                (cpi->common.mb_rows * cpi->common.mb_cols));
3970         vp8_set_quantizer(cpi, Q);
3971       }
3972
3973       vp8_setup_key_frame(cpi);
3974     }
3975
3976 #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
3977     {
3978       if (cpi->oxcf.error_resilient_mode) cm->refresh_entropy_probs = 0;
3979
3980       if (cpi->oxcf.error_resilient_mode & VPX_ERROR_RESILIENT_PARTITIONS) {
3981         if (cm->frame_type == KEY_FRAME) cm->refresh_entropy_probs = 1;
3982       }
3983
3984       if (cm->refresh_entropy_probs == 0) {
3985         /* save a copy for later refresh */
3986         memcpy(&cm->lfc, &cm->fc, sizeof(cm->fc));
3987       }
3988
3989       vp8_update_coef_context(cpi);
3990
3991       vp8_update_coef_probs(cpi);
3992
3993       /* transform / motion compensation build reconstruction frame
3994        * +pack coef partitions
3995        */
3996       vp8_encode_frame(cpi);
3997
3998       /* cpi->projected_frame_size is not needed for RT mode */
3999     }
4000 #else
4001     /* transform / motion compensation build reconstruction frame */
4002     vp8_encode_frame(cpi);
4003     if (cpi->oxcf.screen_content_mode == 2) {
4004       if (vp8_drop_encodedframe_overshoot(cpi, Q)) return;
4005     }
4006
4007     cpi->projected_frame_size -= vp8_estimate_entropy_savings(cpi);
4008     cpi->projected_frame_size =
4009         (cpi->projected_frame_size > 0) ? cpi->projected_frame_size : 0;
4010 #endif
4011     vpx_clear_system_state();
4012
4013     /* Test to see if the stats generated for this frame indicate that
4014      * we should have coded a key frame (assuming that we didn't)!
4015      */
4016
4017     if (cpi->pass != 2 && cpi->oxcf.auto_key && cm->frame_type != KEY_FRAME &&
4018         cpi->compressor_speed != 2) {
4019 #if !CONFIG_REALTIME_ONLY
4020       if (decide_key_frame(cpi)) {
4021         /* Reset all our sizing numbers and recode */
4022         cm->frame_type = KEY_FRAME;
4023
4024         vp8_pick_frame_size(cpi);
4025
4026         /* Clear the Alt reference frame active flag when we have
4027          * a key frame
4028          */
4029         cpi->source_alt_ref_active = 0;
4030
4031         // Set the loop filter deltas and segmentation map update
4032         setup_features(cpi);
4033
4034         vp8_restore_coding_context(cpi);
4035
4036         Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4037
4038         vp8_compute_frame_size_bounds(cpi, &frame_under_shoot_limit,
4039                                       &frame_over_shoot_limit);
4040
4041         /* Limit Q range for the adaptive loop. */
4042         bottom_index = cpi->active_best_quality;
4043         top_index = cpi->active_worst_quality;
4044         q_low = cpi->active_best_quality;
4045         q_high = cpi->active_worst_quality;
4046
4047         loop_count++;
4048         Loop = 1;
4049
4050         continue;
4051       }
4052 #endif
4053     }
4054
4055     vpx_clear_system_state();
4056
4057     if (frame_over_shoot_limit == 0) frame_over_shoot_limit = 1;
4058
4059     /* Are we are overshooting and up against the limit of active max Q. */
4060     if (((cpi->pass != 2) ||
4061          (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)) &&
4062         (Q == cpi->active_worst_quality) &&
4063         (cpi->active_worst_quality < cpi->worst_quality) &&
4064         (cpi->projected_frame_size > frame_over_shoot_limit)) {
4065       int over_size_percent =
4066           ((cpi->projected_frame_size - frame_over_shoot_limit) * 100) /
4067           frame_over_shoot_limit;
4068
4069       /* If so is there any scope for relaxing it */
4070       while ((cpi->active_worst_quality < cpi->worst_quality) &&
4071              (over_size_percent > 0)) {
4072         cpi->active_worst_quality++;
4073         /* Assume 1 qstep = about 4% on frame size. */
4074         over_size_percent = (int)(over_size_percent * 0.96);
4075       }
4076 #if !CONFIG_REALTIME_ONLY
4077       top_index = cpi->active_worst_quality;
4078 #endif  // !CONFIG_REALTIME_ONLY
4079       /* If we have updated the active max Q do not call
4080        * vp8_update_rate_correction_factors() this loop.
4081        */
4082       active_worst_qchanged = 1;
4083     } else {
4084       active_worst_qchanged = 0;
4085     }
4086
4087 #if CONFIG_REALTIME_ONLY
4088     Loop = 0;
4089 #else
4090     /* Special case handling for forced key frames */
4091     if ((cm->frame_type == KEY_FRAME) && cpi->this_key_frame_forced) {
4092       int last_q = Q;
4093       int kf_err = vp8_calc_ss_err(cpi->Source, &cm->yv12_fb[cm->new_fb_idx]);
4094
4095       /* The key frame is not good enough */
4096       if (kf_err > ((cpi->ambient_err * 7) >> 3)) {
4097         /* Lower q_high */
4098         q_high = (Q > q_low) ? (Q - 1) : q_low;
4099
4100         /* Adjust Q */
4101         Q = (q_high + q_low) >> 1;
4102       }
4103       /* The key frame is much better than the previous frame */
4104       else if (kf_err < (cpi->ambient_err >> 1)) {
4105         /* Raise q_low */
4106         q_low = (Q < q_high) ? (Q + 1) : q_high;
4107
4108         /* Adjust Q */
4109         Q = (q_high + q_low + 1) >> 1;
4110       }
4111
4112       /* Clamp Q to upper and lower limits: */
4113       if (Q > q_high) {
4114         Q = q_high;
4115       } else if (Q < q_low) {
4116         Q = q_low;
4117       }
4118
4119       Loop = Q != last_q;
4120     }
4121
4122     /* Is the projected frame size out of range and are we allowed
4123      * to attempt to recode.
4124      */
4125     else if (recode_loop_test(cpi, frame_over_shoot_limit,
4126                               frame_under_shoot_limit, Q, top_index,
4127                               bottom_index)) {
4128       int last_q = Q;
4129       int Retries = 0;
4130
4131       /* Frame size out of permitted range. Update correction factor
4132        * & compute new Q to try...
4133        */
4134
4135       /* Frame is too large */
4136       if (cpi->projected_frame_size > cpi->this_frame_target) {
4137         /* Raise Qlow as to at least the current value */
4138         q_low = (Q < q_high) ? (Q + 1) : q_high;
4139
4140         /* If we are using over quant do the same for zbin_oq_low */
4141         if (cpi->mb.zbin_over_quant > 0) {
4142           zbin_oq_low = (cpi->mb.zbin_over_quant < zbin_oq_high)
4143                             ? (cpi->mb.zbin_over_quant + 1)
4144                             : zbin_oq_high;
4145         }
4146
4147         if (undershoot_seen) {
4148           /* Update rate_correction_factor unless
4149            * cpi->active_worst_quality has changed.
4150            */
4151           if (!active_worst_qchanged) {
4152             vp8_update_rate_correction_factors(cpi, 1);
4153           }
4154
4155           Q = (q_high + q_low + 1) / 2;
4156
4157           /* Adjust cpi->zbin_over_quant (only allowed when Q
4158            * is max)
4159            */
4160           if (Q < MAXQ) {
4161             cpi->mb.zbin_over_quant = 0;
4162           } else {
4163             zbin_oq_low = (cpi->mb.zbin_over_quant < zbin_oq_high)
4164                               ? (cpi->mb.zbin_over_quant + 1)
4165                               : zbin_oq_high;
4166             cpi->mb.zbin_over_quant = (zbin_oq_high + zbin_oq_low) / 2;
4167           }
4168         } else {
4169           /* Update rate_correction_factor unless
4170            * cpi->active_worst_quality has changed.
4171            */
4172           if (!active_worst_qchanged) {
4173             vp8_update_rate_correction_factors(cpi, 0);
4174           }
4175
4176           Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4177
4178           while (((Q < q_low) || (cpi->mb.zbin_over_quant < zbin_oq_low)) &&
4179                  (Retries < 10)) {
4180             vp8_update_rate_correction_factors(cpi, 0);
4181             Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4182             Retries++;
4183           }
4184         }
4185
4186         overshoot_seen = 1;
4187       }
4188       /* Frame is too small */
4189       else {
4190         if (cpi->mb.zbin_over_quant == 0) {
4191           /* Lower q_high if not using over quant */
4192           q_high = (Q > q_low) ? (Q - 1) : q_low;
4193         } else {
4194           /* else lower zbin_oq_high */
4195           zbin_oq_high = (cpi->mb.zbin_over_quant > zbin_oq_low)
4196                              ? (cpi->mb.zbin_over_quant - 1)
4197                              : zbin_oq_low;
4198         }
4199
4200         if (overshoot_seen) {
4201           /* Update rate_correction_factor unless
4202            * cpi->active_worst_quality has changed.
4203            */
4204           if (!active_worst_qchanged) {
4205             vp8_update_rate_correction_factors(cpi, 1);
4206           }
4207
4208           Q = (q_high + q_low) / 2;
4209
4210           /* Adjust cpi->zbin_over_quant (only allowed when Q
4211            * is max)
4212            */
4213           if (Q < MAXQ) {
4214             cpi->mb.zbin_over_quant = 0;
4215           } else {
4216             cpi->mb.zbin_over_quant = (zbin_oq_high + zbin_oq_low) / 2;
4217           }
4218         } else {
4219           /* Update rate_correction_factor unless
4220            * cpi->active_worst_quality has changed.
4221            */
4222           if (!active_worst_qchanged) {
4223             vp8_update_rate_correction_factors(cpi, 0);
4224           }
4225
4226           Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4227
4228           /* Special case reset for qlow for constrained quality.
4229            * This should only trigger where there is very substantial
4230            * undershoot on a frame and the auto cq level is above
4231            * the user passsed in value.
4232            */
4233           if ((cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) &&
4234               (Q < q_low)) {
4235             q_low = Q;
4236           }
4237
4238           while (((Q > q_high) || (cpi->mb.zbin_over_quant > zbin_oq_high)) &&
4239                  (Retries < 10)) {
4240             vp8_update_rate_correction_factors(cpi, 0);
4241             Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4242             Retries++;
4243           }
4244         }
4245
4246         undershoot_seen = 1;
4247       }
4248
4249       /* Clamp Q to upper and lower limits: */
4250       if (Q > q_high) {
4251         Q = q_high;
4252       } else if (Q < q_low) {
4253         Q = q_low;
4254       }
4255
4256       /* Clamp cpi->zbin_over_quant */
4257       cpi->mb.zbin_over_quant = (cpi->mb.zbin_over_quant < zbin_oq_low)
4258                                     ? zbin_oq_low
4259                                     : (cpi->mb.zbin_over_quant > zbin_oq_high)
4260                                           ? zbin_oq_high
4261                                           : cpi->mb.zbin_over_quant;
4262
4263       Loop = Q != last_q;
4264     } else {
4265       Loop = 0;
4266     }
4267 #endif  // CONFIG_REALTIME_ONLY
4268
4269     if (cpi->is_src_frame_alt_ref) Loop = 0;
4270
4271     if (Loop == 1) {
4272       vp8_restore_coding_context(cpi);
4273       loop_count++;
4274 #if CONFIG_INTERNAL_STATS
4275       cpi->tot_recode_hits++;
4276 #endif
4277     }
4278   } while (Loop == 1);
4279
4280 #if defined(DROP_UNCODED_FRAMES)
4281   /* if there are no coded macroblocks at all drop this frame */
4282   if (cpi->common.MBs == cpi->mb.skip_true_count &&
4283       (cpi->drop_frame_count & 7) != 7 && cm->frame_type != KEY_FRAME) {
4284     cpi->common.current_video_frame++;
4285     cpi->frames_since_key++;
4286     cpi->drop_frame_count++;
4287     // We advance the temporal pattern for dropped frames.
4288     cpi->temporal_pattern_counter++;
4289     return;
4290   }
4291   cpi->drop_frame_count = 0;
4292 #endif
4293
4294 #if 0
4295     /* Experimental code for lagged and one pass
4296      * Update stats used for one pass GF selection
4297      */
4298     {
4299         cpi->one_pass_frame_stats[cpi->one_pass_frame_index].frame_coded_error = (double)cpi->prediction_error;
4300         cpi->one_pass_frame_stats[cpi->one_pass_frame_index].frame_intra_error = (double)cpi->intra_error;
4301         cpi->one_pass_frame_stats[cpi->one_pass_frame_index].frame_pcnt_inter = (double)(100 - cpi->this_frame_percent_intra) / 100.0;
4302     }
4303 #endif
4304
4305   /* Special case code to reduce pulsing when key frames are forced at a
4306    * fixed interval. Note the reconstruction error if it is the frame before
4307    * the force key frame
4308    */
4309   if (cpi->next_key_frame_forced && (cpi->twopass.frames_to_key == 0)) {
4310     cpi->ambient_err =
4311         vp8_calc_ss_err(cpi->Source, &cm->yv12_fb[cm->new_fb_idx]);
4312   }
4313
4314 /* This frame's MVs are saved and will be used in next frame's MV predictor.
4315  * Last frame has one more line(add to bottom) and one more column(add to
4316  * right) than cm->mip. The edge elements are initialized to 0.
4317  */
4318 #if CONFIG_MULTI_RES_ENCODING
4319   if (!cpi->oxcf.mr_encoder_id && cm->show_frame)
4320 #else
4321   if (cm->show_frame) /* do not save for altref frame */
4322 #endif
4323   {
4324     int mb_row;
4325     int mb_col;
4326     /* Point to beginning of allocated MODE_INFO arrays. */
4327     MODE_INFO *tmp = cm->mip;
4328
4329     if (cm->frame_type != KEY_FRAME) {
4330       for (mb_row = 0; mb_row < cm->mb_rows + 1; ++mb_row) {
4331         for (mb_col = 0; mb_col < cm->mb_cols + 1; ++mb_col) {
4332           if (tmp->mbmi.ref_frame != INTRA_FRAME) {
4333             cpi->lfmv[mb_col + mb_row * (cm->mode_info_stride + 1)].as_int =
4334                 tmp->mbmi.mv.as_int;
4335           }
4336
4337           cpi->lf_ref_frame_sign_bias[mb_col +
4338                                       mb_row * (cm->mode_info_stride + 1)] =
4339               cm->ref_frame_sign_bias[tmp->mbmi.ref_frame];
4340           cpi->lf_ref_frame[mb_col + mb_row * (cm->mode_info_stride + 1)] =
4341               tmp->mbmi.ref_frame;
4342           tmp++;
4343         }
4344       }
4345     }
4346   }
4347
4348   /* Count last ref frame 0,0 usage on current encoded frame. */
4349   {
4350     int mb_row;
4351     int mb_col;
4352     /* Point to beginning of MODE_INFO arrays. */
4353     MODE_INFO *tmp = cm->mi;
4354
4355     cpi->zeromv_count = 0;
4356
4357     if (cm->frame_type != KEY_FRAME) {
4358       for (mb_row = 0; mb_row < cm->mb_rows; ++mb_row) {
4359         for (mb_col = 0; mb_col < cm->mb_cols; ++mb_col) {
4360           if (tmp->mbmi.mode == ZEROMV && tmp->mbmi.ref_frame == LAST_FRAME) {
4361             cpi->zeromv_count++;
4362           }
4363           tmp++;
4364         }
4365         tmp++;
4366       }
4367     }
4368   }
4369
4370 #if CONFIG_MULTI_RES_ENCODING
4371   vp8_cal_dissimilarity(cpi);
4372 #endif
4373
4374   /* Update the GF useage maps.
4375    * This is done after completing the compression of a frame when all
4376    * modes etc. are finalized but before loop filter
4377    */
4378   if (cpi->oxcf.number_of_layers == 1) {
4379     vp8_update_gf_useage_maps(cpi, cm, &cpi->mb);
4380   }
4381
4382   if (cm->frame_type == KEY_FRAME) cm->refresh_last_frame = 1;
4383
4384 #if 0
4385     {
4386         FILE *f = fopen("gfactive.stt", "a");
4387         fprintf(f, "%8d %8d %8d %8d %8d\n", cm->current_video_frame, (100 * cpi->gf_active_count) / (cpi->common.mb_rows * cpi->common.mb_cols), cpi->this_iiratio, cpi->next_iiratio, cm->refresh_golden_frame);
4388         fclose(f);
4389     }
4390 #endif
4391
4392   /* For inter frames the current default behavior is that when
4393    * cm->refresh_golden_frame is set we copy the old GF over to the ARF buffer
4394    * This is purely an encoder decision at present.
4395    */
4396   if (!cpi->oxcf.error_resilient_mode && cm->refresh_golden_frame) {
4397     cm->copy_buffer_to_arf = 2;
4398   } else {
4399     cm->copy_buffer_to_arf = 0;
4400   }
4401
4402   cm->frame_to_show = &cm->yv12_fb[cm->new_fb_idx];
4403
4404 #if CONFIG_TEMPORAL_DENOISING
4405   // Get some measure of the amount of noise, by measuring the (partial) mse
4406   // between source and denoised buffer, for y channel. Partial refers to
4407   // computing the sse for a sub-sample of the frame (i.e., skip x blocks along
4408   // row/column),
4409   // and only for blocks in that set that are consecutive ZEROMV_LAST mode.
4410   // Do this every ~8 frames, to further reduce complexity.
4411   // TODO(marpan): Keep this for now for the case cpi->oxcf.noise_sensitivity <
4412   // 4,
4413   // should be removed in favor of the process_denoiser_mode_change() function
4414   // below.
4415   if (cpi->oxcf.noise_sensitivity > 0 && cpi->oxcf.noise_sensitivity < 4 &&
4416       !cpi->oxcf.screen_content_mode && cpi->frames_since_key % 8 == 0 &&
4417       cm->frame_type != KEY_FRAME) {
4418     cpi->mse_source_denoised = measure_square_diff_partial(
4419         &cpi->denoiser.yv12_running_avg[INTRA_FRAME], cpi->Source, cpi);
4420   }
4421
4422   // For the adaptive denoising mode (noise_sensitivity == 4), sample the mse
4423   // of source diff (between current and previous frame), and determine if we
4424   // should switch the denoiser mode. Sampling refers to computing the mse for
4425   // a sub-sample of the frame (i.e., skip x blocks along row/column), and
4426   // only for blocks in that set that have used ZEROMV LAST, along with some
4427   // constraint on the sum diff between blocks. This process is called every
4428   // ~8 frames, to further reduce complexity.
4429   if (cpi->oxcf.noise_sensitivity == 4 && !cpi->oxcf.screen_content_mode &&
4430       cpi->frames_since_key % 8 == 0 && cm->frame_type != KEY_FRAME) {
4431     process_denoiser_mode_change(cpi);
4432   }
4433 #endif
4434
4435 #ifdef OUTPUT_YUV_SKINMAP
4436   if (cpi->common.current_video_frame > 1) {
4437     compute_skin_map(cpi, yuv_skinmap_file);
4438   }
4439 #endif
4440
4441 #if CONFIG_MULTITHREAD
4442   if (cpi->b_multi_threaded) {
4443     /* start loopfilter in separate thread */
4444     sem_post(&cpi->h_event_start_lpf);
4445     cpi->b_lpf_running = 1;
4446   } else
4447 #endif
4448   {
4449     vp8_loopfilter_frame(cpi, cm);
4450   }
4451
4452   update_reference_frames(cpi);
4453
4454 #ifdef OUTPUT_YUV_DENOISED
4455   vp8_write_yuv_frame(yuv_denoised_file,
4456                       &cpi->denoiser.yv12_running_avg[INTRA_FRAME]);
4457 #endif
4458
4459 #if !(CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
4460   if (cpi->oxcf.error_resilient_mode) {
4461     cm->refresh_entropy_probs = 0;
4462   }
4463 #endif
4464
4465 #if CONFIG_MULTITHREAD
4466   /* wait that filter_level is picked so that we can continue with stream
4467    * packing */
4468   if (cpi->b_multi_threaded) sem_wait(&cpi->h_event_end_lpf);
4469 #endif
4470
4471   /* build the bitstream */
4472   vp8_pack_bitstream(cpi, dest, dest_end, size);
4473
4474   /* Move storing frame_type out of the above loop since it is also
4475    * needed in motion search besides loopfilter */
4476   cm->last_frame_type = cm->frame_type;
4477
4478   /* Update rate control heuristics */
4479   cpi->total_byte_count += (*size);
4480   cpi->projected_frame_size = (int)(*size) << 3;
4481
4482   if (cpi->oxcf.number_of_layers > 1) {
4483     unsigned int i;
4484     for (i = cpi->current_layer + 1; i < cpi->oxcf.number_of_layers; ++i) {
4485       cpi->layer_context[i].total_byte_count += (*size);
4486     }
4487   }
4488
4489   if (!active_worst_qchanged) vp8_update_rate_correction_factors(cpi, 2);
4490
4491   cpi->last_q[cm->frame_type] = cm->base_qindex;
4492
4493   if (cm->frame_type == KEY_FRAME) {
4494     vp8_adjust_key_frame_context(cpi);
4495   }
4496
4497   /* Keep a record of ambient average Q. */
4498   if (cm->frame_type != KEY_FRAME) {
4499     cpi->avg_frame_qindex =
4500         (2 + 3 * cpi->avg_frame_qindex + cm->base_qindex) >> 2;
4501   }
4502
4503   /* Keep a record from which we can calculate the average Q excluding
4504    * GF updates and key frames
4505    */
4506   if ((cm->frame_type != KEY_FRAME) &&
4507       ((cpi->oxcf.number_of_layers > 1) ||
4508        (!cm->refresh_golden_frame && !cm->refresh_alt_ref_frame))) {
4509     cpi->ni_frames++;
4510
4511     /* Calculate the average Q for normal inter frames (not key or GFU
4512      * frames).
4513      */
4514     if (cpi->pass == 2) {
4515       cpi->ni_tot_qi += Q;
4516       cpi->ni_av_qi = (cpi->ni_tot_qi / cpi->ni_frames);
4517     } else {
4518       /* Damp value for first few frames */
4519       if (cpi->ni_frames > 150) {
4520         cpi->ni_tot_qi += Q;
4521         cpi->ni_av_qi = (cpi->ni_tot_qi / cpi->ni_frames);
4522       }
4523       /* For one pass, early in the clip ... average the current frame Q
4524        * value with the worstq entered by the user as a dampening measure
4525        */
4526       else {
4527         cpi->ni_tot_qi += Q;
4528         cpi->ni_av_qi =
4529             ((cpi->ni_tot_qi / cpi->ni_frames) + cpi->worst_quality + 1) / 2;
4530       }
4531
4532       /* If the average Q is higher than what was used in the last
4533        * frame (after going through the recode loop to keep the frame
4534        * size within range) then use the last frame value - 1. The -1
4535        * is designed to stop Q and hence the data rate, from
4536        * progressively falling away during difficult sections, but at
4537        * the same time reduce the number of itterations around the
4538        * recode loop.
4539        */
4540       if (Q > cpi->ni_av_qi) cpi->ni_av_qi = Q - 1;
4541     }
4542   }
4543
4544   /* Update the buffer level variable. */
4545   /* Non-viewable frames are a special case and are treated as pure overhead. */
4546   if (!cm->show_frame) {
4547     cpi->bits_off_target -= cpi->projected_frame_size;
4548   } else {
4549     cpi->bits_off_target +=
4550         cpi->av_per_frame_bandwidth - cpi->projected_frame_size;
4551   }
4552
4553   /* Clip the buffer level to the maximum specified buffer size */
4554   if (cpi->bits_off_target > cpi->oxcf.maximum_buffer_size) {
4555     cpi->bits_off_target = cpi->oxcf.maximum_buffer_size;
4556   }
4557
4558   // If the frame dropper is not enabled, don't let the buffer level go below
4559   // some threshold, given here by -|maximum_buffer_size|. For now we only do
4560   // this for screen content input.
4561   if (cpi->drop_frames_allowed == 0 && cpi->oxcf.screen_content_mode &&
4562       cpi->bits_off_target < -cpi->oxcf.maximum_buffer_size) {
4563     cpi->bits_off_target = -cpi->oxcf.maximum_buffer_size;
4564   }
4565
4566   /* Rolling monitors of whether we are over or underspending used to
4567    * help regulate min and Max Q in two pass.
4568    */
4569   cpi->rolling_target_bits =
4570       ((cpi->rolling_target_bits * 3) + cpi->this_frame_target + 2) / 4;
4571   cpi->rolling_actual_bits =
4572       ((cpi->rolling_actual_bits * 3) + cpi->projected_frame_size + 2) / 4;
4573   cpi->long_rolling_target_bits =
4574       ((cpi->long_rolling_target_bits * 31) + cpi->this_frame_target + 16) / 32;
4575   cpi->long_rolling_actual_bits =
4576       ((cpi->long_rolling_actual_bits * 31) + cpi->projected_frame_size + 16) /
4577       32;
4578
4579   /* Actual bits spent */
4580   cpi->total_actual_bits += cpi->projected_frame_size;
4581
4582   /* Debug stats */
4583   cpi->total_target_vs_actual +=
4584       (cpi->this_frame_target - cpi->projected_frame_size);
4585
4586   cpi->buffer_level = cpi->bits_off_target;
4587
4588   /* Propagate values to higher temporal layers */
4589   if (cpi->oxcf.number_of_layers > 1) {
4590     unsigned int i;
4591
4592     for (i = cpi->current_layer + 1; i < cpi->oxcf.number_of_layers; ++i) {
4593       LAYER_CONTEXT *lc = &cpi->layer_context[i];
4594       int bits_off_for_this_layer = (int)(lc->target_bandwidth / lc->framerate -
4595                                           cpi->projected_frame_size);
4596
4597       lc->bits_off_target += bits_off_for_this_layer;
4598
4599       /* Clip buffer level to maximum buffer size for the layer */
4600       if (lc->bits_off_target > lc->maximum_buffer_size) {
4601         lc->bits_off_target = lc->maximum_buffer_size;
4602       }
4603
4604       lc->total_actual_bits += cpi->projected_frame_size;
4605       lc->total_target_vs_actual += bits_off_for_this_layer;
4606       lc->buffer_level = lc->bits_off_target;
4607     }
4608   }
4609
4610   /* Update bits left to the kf and gf groups to account for overshoot
4611    * or undershoot on these frames
4612    */
4613   if (cm->frame_type == KEY_FRAME) {
4614     cpi->twopass.kf_group_bits +=
4615         cpi->this_frame_target - cpi->projected_frame_size;
4616
4617     if (cpi->twopass.kf_group_bits < 0) cpi->twopass.kf_group_bits = 0;
4618   } else if (cm->refresh_golden_frame || cm->refresh_alt_ref_frame) {
4619     cpi->twopass.gf_group_bits +=
4620         cpi->this_frame_target - cpi->projected_frame_size;
4621
4622     if (cpi->twopass.gf_group_bits < 0) cpi->twopass.gf_group_bits = 0;
4623   }
4624
4625   if (cm->frame_type != KEY_FRAME) {
4626     if (cpi->common.refresh_alt_ref_frame) {
4627       cpi->last_skip_false_probs[2] = cpi->prob_skip_false;
4628       cpi->last_skip_probs_q[2] = cm->base_qindex;
4629     } else if (cpi->common.refresh_golden_frame) {
4630       cpi->last_skip_false_probs[1] = cpi->prob_skip_false;
4631       cpi->last_skip_probs_q[1] = cm->base_qindex;
4632     } else {
4633       cpi->last_skip_false_probs[0] = cpi->prob_skip_false;
4634       cpi->last_skip_probs_q[0] = cm->base_qindex;
4635
4636       /* update the baseline */
4637       cpi->base_skip_false_prob[cm->base_qindex] = cpi->prob_skip_false;
4638     }
4639   }
4640
4641 #if 0 && CONFIG_INTERNAL_STATS
4642     {
4643         FILE *f = fopen("tmp.stt", "a");
4644
4645         vpx_clear_system_state();
4646
4647         if (cpi->twopass.total_left_stats.coded_error != 0.0)
4648             fprintf(f, "%10d %10d %10d %10d %10d %10"PRId64" %10"PRId64
4649                        "%10"PRId64" %10d %6d %6d %6d %6d %5d %5d %5d %8d "
4650                        "%8.2lf %"PRId64" %10.3lf %10"PRId64" %8d\n",
4651                        cpi->common.current_video_frame, cpi->this_frame_target,
4652                        cpi->projected_frame_size,
4653                        (cpi->projected_frame_size - cpi->this_frame_target),
4654                        cpi->total_target_vs_actual,
4655                        cpi->buffer_level,
4656                        (cpi->oxcf.starting_buffer_level-cpi->bits_off_target),
4657                        cpi->total_actual_bits, cm->base_qindex,
4658                        cpi->active_best_quality, cpi->active_worst_quality,
4659                        cpi->ni_av_qi, cpi->cq_target_quality,
4660                        cm->refresh_golden_frame, cm->refresh_alt_ref_frame,
4661                        cm->frame_type, cpi->gfu_boost,
4662                        cpi->twopass.est_max_qcorrection_factor,
4663                        cpi->twopass.bits_left,
4664                        cpi->twopass.total_left_stats.coded_error,
4665                        (double)cpi->twopass.bits_left /
4666                            cpi->twopass.total_left_stats.coded_error,
4667                        cpi->tot_recode_hits);
4668         else
4669             fprintf(f, "%10d %10d %10d %10d %10d %10"PRId64" %10"PRId64
4670                        "%10"PRId64" %10d %6d %6d %6d %6d %5d %5d %5d %8d "
4671                        "%8.2lf %"PRId64" %10.3lf %8d\n",
4672                        cpi->common.current_video_frame, cpi->this_frame_target,
4673                        cpi->projected_frame_size,
4674                        (cpi->projected_frame_size - cpi->this_frame_target),
4675                        cpi->total_target_vs_actual,
4676                        cpi->buffer_level,
4677                        (cpi->oxcf.starting_buffer_level-cpi->bits_off_target),
4678                        cpi->total_actual_bits, cm->base_qindex,
4679                        cpi->active_best_quality, cpi->active_worst_quality,
4680                        cpi->ni_av_qi, cpi->cq_target_quality,
4681                        cm->refresh_golden_frame, cm->refresh_alt_ref_frame,
4682                        cm->frame_type, cpi->gfu_boost,
4683                        cpi->twopass.est_max_qcorrection_factor,
4684                        cpi->twopass.bits_left,
4685                        cpi->twopass.total_left_stats.coded_error,
4686                        cpi->tot_recode_hits);
4687
4688         fclose(f);
4689
4690         {
4691             FILE *fmodes = fopen("Modes.stt", "a");
4692
4693             fprintf(fmodes, "%6d:%1d:%1d:%1d ",
4694                         cpi->common.current_video_frame,
4695                         cm->frame_type, cm->refresh_golden_frame,
4696                         cm->refresh_alt_ref_frame);
4697
4698             fprintf(fmodes, "\n");
4699
4700             fclose(fmodes);
4701         }
4702     }
4703
4704 #endif
4705
4706   if (cm->refresh_golden_frame == 1) {
4707     cm->frame_flags = cm->frame_flags | FRAMEFLAGS_GOLDEN;
4708   } else {
4709     cm->frame_flags = cm->frame_flags & ~FRAMEFLAGS_GOLDEN;
4710   }
4711
4712   if (cm->refresh_alt_ref_frame == 1) {
4713     cm->frame_flags = cm->frame_flags | FRAMEFLAGS_ALTREF;
4714   } else {
4715     cm->frame_flags = cm->frame_flags & ~FRAMEFLAGS_ALTREF;
4716   }
4717
4718   if (cm->refresh_last_frame & cm->refresh_golden_frame) { /* both refreshed */
4719     cpi->gold_is_last = 1;
4720   } else if (cm->refresh_last_frame ^ cm->refresh_golden_frame) {
4721     /* 1 refreshed but not the other */
4722     cpi->gold_is_last = 0;
4723   }
4724
4725   if (cm->refresh_last_frame & cm->refresh_alt_ref_frame) { /* both refreshed */
4726     cpi->alt_is_last = 1;
4727   } else if (cm->refresh_last_frame ^ cm->refresh_alt_ref_frame) {
4728     /* 1 refreshed but not the other */
4729     cpi->alt_is_last = 0;
4730   }
4731
4732   if (cm->refresh_alt_ref_frame &
4733       cm->refresh_golden_frame) { /* both refreshed */
4734     cpi->gold_is_alt = 1;
4735   } else if (cm->refresh_alt_ref_frame ^ cm->refresh_golden_frame) {
4736     /* 1 refreshed but not the other */
4737     cpi->gold_is_alt = 0;
4738   }
4739
4740   cpi->ref_frame_flags = VP8_ALTR_FRAME | VP8_GOLD_FRAME | VP8_LAST_FRAME;
4741
4742   if (cpi->gold_is_last) cpi->ref_frame_flags &= ~VP8_GOLD_FRAME;
4743
4744   if (cpi->alt_is_last) cpi->ref_frame_flags &= ~VP8_ALTR_FRAME;
4745
4746   if (cpi->gold_is_alt) cpi->ref_frame_flags &= ~VP8_ALTR_FRAME;
4747
4748   if (!cpi->oxcf.error_resilient_mode) {
4749     if (cpi->oxcf.play_alternate && cm->refresh_alt_ref_frame &&
4750         (cm->frame_type != KEY_FRAME)) {
4751       /* Update the alternate reference frame stats as appropriate. */
4752       update_alt_ref_frame_stats(cpi);
4753     } else {
4754       /* Update the Golden frame stats as appropriate. */
4755       update_golden_frame_stats(cpi);
4756     }
4757   }
4758
4759   if (cm->frame_type == KEY_FRAME) {
4760     /* Tell the caller that the frame was coded as a key frame */
4761     *frame_flags = cm->frame_flags | FRAMEFLAGS_KEY;
4762
4763     /* As this frame is a key frame  the next defaults to an inter frame. */
4764     cm->frame_type = INTER_FRAME;
4765
4766     cpi->last_frame_percent_intra = 100;
4767   } else {
4768     *frame_flags = cm->frame_flags & ~FRAMEFLAGS_KEY;
4769
4770     cpi->last_frame_percent_intra = cpi->this_frame_percent_intra;
4771   }
4772
4773   /* Clear the one shot update flags for segmentation map and mode/ref
4774    * loop filter deltas.
4775    */
4776   cpi->mb.e_mbd.update_mb_segmentation_map = 0;
4777   cpi->mb.e_mbd.update_mb_segmentation_data = 0;
4778   cpi->mb.e_mbd.mode_ref_lf_delta_update = 0;
4779
4780   /* Dont increment frame counters if this was an altref buffer update
4781    * not a real frame
4782    */
4783   if (cm->show_frame) {
4784     cm->current_video_frame++;
4785     cpi->frames_since_key++;
4786     cpi->temporal_pattern_counter++;
4787   }
4788
4789 /* reset to normal state now that we are done. */
4790
4791 #if 0
4792     {
4793         char filename[512];
4794         FILE *recon_file;
4795         sprintf(filename, "enc%04d.yuv", (int) cm->current_video_frame);
4796         recon_file = fopen(filename, "wb");
4797         fwrite(cm->yv12_fb[cm->lst_fb_idx].buffer_alloc,
4798                cm->yv12_fb[cm->lst_fb_idx].frame_size, 1, recon_file);
4799         fclose(recon_file);
4800     }
4801 #endif
4802
4803   /* DEBUG */
4804   /* vp8_write_yuv_frame("encoder_recon.yuv", cm->frame_to_show); */
4805 }
4806 #if !CONFIG_REALTIME_ONLY
4807 static void Pass2Encode(VP8_COMP *cpi, size_t *size, unsigned char *dest,
4808                         unsigned char *dest_end, unsigned int *frame_flags) {
4809   if (!cpi->common.refresh_alt_ref_frame) vp8_second_pass(cpi);
4810
4811   encode_frame_to_data_rate(cpi, size, dest, dest_end, frame_flags);
4812   cpi->twopass.bits_left -= 8 * (int)(*size);
4813
4814   if (!cpi->common.refresh_alt_ref_frame) {
4815     double two_pass_min_rate =
4816         (double)(cpi->oxcf.target_bandwidth *
4817                  cpi->oxcf.two_pass_vbrmin_section / 100);
4818     cpi->twopass.bits_left += (int64_t)(two_pass_min_rate / cpi->framerate);
4819   }
4820 }
4821 #endif
4822
4823 int vp8_receive_raw_frame(VP8_COMP *cpi, unsigned int frame_flags,
4824                           YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
4825                           int64_t end_time) {
4826   struct vpx_usec_timer timer;
4827   int res = 0;
4828
4829   vpx_usec_timer_start(&timer);
4830
4831   /* Reinit the lookahead buffer if the frame size changes */
4832   if (sd->y_width != cpi->oxcf.Width || sd->y_height != cpi->oxcf.Height) {
4833     assert(cpi->oxcf.lag_in_frames < 2);
4834     dealloc_raw_frame_buffers(cpi);
4835     alloc_raw_frame_buffers(cpi);
4836   }
4837
4838   if (vp8_lookahead_push(cpi->lookahead, sd, time_stamp, end_time, frame_flags,
4839                          cpi->active_map_enabled ? cpi->active_map : NULL)) {
4840     res = -1;
4841   }
4842   vpx_usec_timer_mark(&timer);
4843   cpi->time_receive_data += vpx_usec_timer_elapsed(&timer);
4844
4845   return res;
4846 }
4847
4848 static int frame_is_reference(const VP8_COMP *cpi) {
4849   const VP8_COMMON *cm = &cpi->common;
4850   const MACROBLOCKD *xd = &cpi->mb.e_mbd;
4851
4852   return cm->frame_type == KEY_FRAME || cm->refresh_last_frame ||
4853          cm->refresh_golden_frame || cm->refresh_alt_ref_frame ||
4854          cm->copy_buffer_to_gf || cm->copy_buffer_to_arf ||
4855          cm->refresh_entropy_probs || xd->mode_ref_lf_delta_update ||
4856          xd->update_mb_segmentation_map || xd->update_mb_segmentation_data;
4857 }
4858
4859 int vp8_get_compressed_data(VP8_COMP *cpi, unsigned int *frame_flags,
4860                             size_t *size, unsigned char *dest,
4861                             unsigned char *dest_end, int64_t *time_stamp,
4862                             int64_t *time_end, int flush) {
4863   VP8_COMMON *cm;
4864   struct vpx_usec_timer tsctimer;
4865   struct vpx_usec_timer ticktimer;
4866   struct vpx_usec_timer cmptimer;
4867   YV12_BUFFER_CONFIG *force_src_buffer = NULL;
4868
4869   if (!cpi) return -1;
4870
4871   cm = &cpi->common;
4872
4873   if (setjmp(cpi->common.error.jmp)) {
4874     cpi->common.error.setjmp = 0;
4875     vpx_clear_system_state();
4876     return VPX_CODEC_CORRUPT_FRAME;
4877   }
4878
4879   cpi->common.error.setjmp = 1;
4880
4881   vpx_usec_timer_start(&cmptimer);
4882
4883   cpi->source = NULL;
4884
4885 #if !CONFIG_REALTIME_ONLY
4886   /* Should we code an alternate reference frame */
4887   if (cpi->oxcf.error_resilient_mode == 0 && cpi->oxcf.play_alternate &&
4888       cpi->source_alt_ref_pending) {
4889     if ((cpi->source = vp8_lookahead_peek(
4890              cpi->lookahead, cpi->frames_till_gf_update_due, PEEK_FORWARD))) {
4891       cpi->alt_ref_source = cpi->source;
4892       if (cpi->oxcf.arnr_max_frames > 0) {
4893         vp8_temporal_filter_prepare_c(cpi, cpi->frames_till_gf_update_due);
4894         force_src_buffer = &cpi->alt_ref_buffer;
4895       }
4896       cpi->frames_till_alt_ref_frame = cpi->frames_till_gf_update_due;
4897       cm->refresh_alt_ref_frame = 1;
4898       cm->refresh_golden_frame = 0;
4899       cm->refresh_last_frame = 0;
4900       cm->show_frame = 0;
4901       /* Clear Pending alt Ref flag. */
4902       cpi->source_alt_ref_pending = 0;
4903       cpi->is_src_frame_alt_ref = 0;
4904     }
4905   }
4906 #endif
4907
4908   if (!cpi->source) {
4909     /* Read last frame source if we are encoding first pass. */
4910     if (cpi->pass == 1 && cm->current_video_frame > 0) {
4911       if ((cpi->last_source =
4912                vp8_lookahead_peek(cpi->lookahead, 1, PEEK_BACKWARD)) == NULL) {
4913         return -1;
4914       }
4915     }
4916
4917     if ((cpi->source = vp8_lookahead_pop(cpi->lookahead, flush))) {
4918       cm->show_frame = 1;
4919
4920       cpi->is_src_frame_alt_ref =
4921           cpi->alt_ref_source && (cpi->source == cpi->alt_ref_source);
4922
4923       if (cpi->is_src_frame_alt_ref) cpi->alt_ref_source = NULL;
4924     }
4925   }
4926
4927   if (cpi->source) {
4928     cpi->Source = force_src_buffer ? force_src_buffer : &cpi->source->img;
4929     cpi->un_scaled_source = cpi->Source;
4930     *time_stamp = cpi->source->ts_start;
4931     *time_end = cpi->source->ts_end;
4932     *frame_flags = cpi->source->flags;
4933
4934     if (cpi->pass == 1 && cm->current_video_frame > 0) {
4935       cpi->last_frame_unscaled_source = &cpi->last_source->img;
4936     }
4937   } else {
4938     *size = 0;
4939 #if !CONFIG_REALTIME_ONLY
4940
4941     if (flush && cpi->pass == 1 && !cpi->twopass.first_pass_done) {
4942       vp8_end_first_pass(cpi); /* get last stats packet */
4943       cpi->twopass.first_pass_done = 1;
4944     }
4945
4946 #endif
4947
4948     return -1;
4949   }
4950
4951   if (cpi->source->ts_start < cpi->first_time_stamp_ever) {
4952     cpi->first_time_stamp_ever = cpi->source->ts_start;
4953     cpi->last_end_time_stamp_seen = cpi->source->ts_start;
4954   }
4955
4956   /* adjust frame rates based on timestamps given */
4957   if (cm->show_frame) {
4958     int64_t this_duration;
4959     int step = 0;
4960
4961     if (cpi->source->ts_start == cpi->first_time_stamp_ever) {
4962       this_duration = cpi->source->ts_end - cpi->source->ts_start;
4963       step = 1;
4964     } else {
4965       int64_t last_duration;
4966
4967       this_duration = cpi->source->ts_end - cpi->last_end_time_stamp_seen;
4968       last_duration = cpi->last_end_time_stamp_seen - cpi->last_time_stamp_seen;
4969       /* do a step update if the duration changes by 10% */
4970       if (last_duration) {
4971         step = (int)(((this_duration - last_duration) * 10 / last_duration));
4972       }
4973     }
4974
4975     if (this_duration) {
4976       if (step) {
4977         cpi->ref_framerate = 10000000.0 / this_duration;
4978       } else {
4979         double avg_duration, interval;
4980
4981         /* Average this frame's rate into the last second's average
4982          * frame rate. If we haven't seen 1 second yet, then average
4983          * over the whole interval seen.
4984          */
4985         interval = (double)(cpi->source->ts_end - cpi->first_time_stamp_ever);
4986         if (interval > 10000000.0) interval = 10000000;
4987
4988         avg_duration = 10000000.0 / cpi->ref_framerate;
4989         avg_duration *= (interval - avg_duration + this_duration);
4990         avg_duration /= interval;
4991
4992         cpi->ref_framerate = 10000000.0 / avg_duration;
4993       }
4994 #if CONFIG_MULTI_RES_ENCODING
4995       if (cpi->oxcf.mr_total_resolutions > 1) {
4996         LOWER_RES_FRAME_INFO *low_res_frame_info =
4997             (LOWER_RES_FRAME_INFO *)cpi->oxcf.mr_low_res_mode_info;
4998         // Frame rate should be the same for all spatial layers in
4999         // multi-res-encoding (simulcast), so we constrain the frame for
5000         // higher layers to be that of lowest resolution. This is needed
5001         // as he application may decide to skip encoding a high layer and
5002         // then start again, in which case a big jump in time-stamps will
5003         // be received for that high layer, which will yield an incorrect
5004         // frame rate (from time-stamp adjustment in above calculation).
5005         if (cpi->oxcf.mr_encoder_id) {
5006           cpi->ref_framerate = low_res_frame_info->low_res_framerate;
5007         } else {
5008           // Keep track of frame rate for lowest resolution.
5009           low_res_frame_info->low_res_framerate = cpi->ref_framerate;
5010         }
5011       }
5012 #endif
5013       if (cpi->oxcf.number_of_layers > 1) {
5014         unsigned int i;
5015
5016         /* Update frame rates for each layer */
5017         assert(cpi->oxcf.number_of_layers <= VPX_TS_MAX_LAYERS);
5018         for (i = 0; i < cpi->oxcf.number_of_layers && i < VPX_TS_MAX_LAYERS;
5019              ++i) {
5020           LAYER_CONTEXT *lc = &cpi->layer_context[i];
5021           lc->framerate = cpi->ref_framerate / cpi->oxcf.rate_decimator[i];
5022         }
5023       } else {
5024         vp8_new_framerate(cpi, cpi->ref_framerate);
5025       }
5026     }
5027
5028     cpi->last_time_stamp_seen = cpi->source->ts_start;
5029     cpi->last_end_time_stamp_seen = cpi->source->ts_end;
5030   }
5031
5032   if (cpi->oxcf.number_of_layers > 1) {
5033     int layer;
5034
5035     update_layer_contexts(cpi);
5036
5037     /* Restore layer specific context & set frame rate */
5038     if (cpi->temporal_layer_id >= 0) {
5039       layer = cpi->temporal_layer_id;
5040     } else {
5041       layer =
5042           cpi->oxcf
5043               .layer_id[cpi->temporal_pattern_counter % cpi->oxcf.periodicity];
5044     }
5045     restore_layer_context(cpi, layer);
5046     vp8_new_framerate(cpi, cpi->layer_context[layer].framerate);
5047   }
5048
5049   if (cpi->compressor_speed == 2) {
5050     vpx_usec_timer_start(&tsctimer);
5051     vpx_usec_timer_start(&ticktimer);
5052   }
5053
5054   cpi->lf_zeromv_pct = (cpi->zeromv_count * 100) / cm->MBs;
5055
5056 #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
5057   {
5058     int i;
5059     const int num_part = (1 << cm->multi_token_partition);
5060     /* the available bytes in dest */
5061     const unsigned long dest_size = dest_end - dest;
5062     const int tok_part_buff_size = (dest_size * 9) / (10 * num_part);
5063
5064     unsigned char *dp = dest;
5065
5066     cpi->partition_d[0] = dp;
5067     dp += dest_size / 10; /* reserve 1/10 for control partition */
5068     cpi->partition_d_end[0] = dp;
5069
5070     for (i = 0; i < num_part; ++i) {
5071       cpi->partition_d[i + 1] = dp;
5072       dp += tok_part_buff_size;
5073       cpi->partition_d_end[i + 1] = dp;
5074     }
5075   }
5076 #endif
5077
5078   /* start with a 0 size frame */
5079   *size = 0;
5080
5081   /* Clear down mmx registers */
5082   vpx_clear_system_state();
5083
5084   cm->frame_type = INTER_FRAME;
5085   cm->frame_flags = *frame_flags;
5086
5087 #if 0
5088
5089     if (cm->refresh_alt_ref_frame)
5090     {
5091         cm->refresh_golden_frame = 0;
5092         cm->refresh_last_frame = 0;
5093     }
5094     else
5095     {
5096         cm->refresh_golden_frame = 0;
5097         cm->refresh_last_frame = 1;
5098     }
5099
5100 #endif
5101   /* find a free buffer for the new frame */
5102   {
5103     int i = 0;
5104     for (; i < NUM_YV12_BUFFERS; ++i) {
5105       if (!cm->yv12_fb[i].flags) {
5106         cm->new_fb_idx = i;
5107         break;
5108       }
5109     }
5110
5111     assert(i < NUM_YV12_BUFFERS);
5112   }
5113   switch (cpi->pass) {
5114 #if !CONFIG_REALTIME_ONLY
5115     case 1: Pass1Encode(cpi, size, dest, frame_flags); break;
5116     case 2: Pass2Encode(cpi, size, dest, dest_end, frame_flags); break;
5117 #endif  // !CONFIG_REALTIME_ONLY
5118     default:
5119       encode_frame_to_data_rate(cpi, size, dest, dest_end, frame_flags);
5120       break;
5121   }
5122
5123   if (cpi->compressor_speed == 2) {
5124     unsigned int duration, duration2;
5125     vpx_usec_timer_mark(&tsctimer);
5126     vpx_usec_timer_mark(&ticktimer);
5127
5128     duration = (int)(vpx_usec_timer_elapsed(&ticktimer));
5129     duration2 = (unsigned int)((double)duration / 2);
5130
5131     if (cm->frame_type != KEY_FRAME) {
5132       if (cpi->avg_encode_time == 0) {
5133         cpi->avg_encode_time = duration;
5134       } else {
5135         cpi->avg_encode_time = (7 * cpi->avg_encode_time + duration) >> 3;
5136       }
5137     }
5138
5139     if (duration2) {
5140       {
5141         if (cpi->avg_pick_mode_time == 0) {
5142           cpi->avg_pick_mode_time = duration2;
5143         } else {
5144           cpi->avg_pick_mode_time =
5145               (7 * cpi->avg_pick_mode_time + duration2) >> 3;
5146         }
5147       }
5148     }
5149   }
5150
5151   if (cm->refresh_entropy_probs == 0) {
5152     memcpy(&cm->fc, &cm->lfc, sizeof(cm->fc));
5153   }
5154
5155   /* Save the contexts separately for alt ref, gold and last. */
5156   /* (TODO jbb -> Optimize this with pointers to avoid extra copies. ) */
5157   if (cm->refresh_alt_ref_frame) memcpy(&cpi->lfc_a, &cm->fc, sizeof(cm->fc));
5158
5159   if (cm->refresh_golden_frame) memcpy(&cpi->lfc_g, &cm->fc, sizeof(cm->fc));
5160
5161   if (cm->refresh_last_frame) memcpy(&cpi->lfc_n, &cm->fc, sizeof(cm->fc));
5162
5163   /* if its a dropped frame honor the requests on subsequent frames */
5164   if (*size > 0) {
5165     cpi->droppable = !frame_is_reference(cpi);
5166
5167     /* return to normal state */
5168     cm->refresh_entropy_probs = 1;
5169     cm->refresh_alt_ref_frame = 0;
5170     cm->refresh_golden_frame = 0;
5171     cm->refresh_last_frame = 1;
5172     cm->frame_type = INTER_FRAME;
5173   }
5174
5175   /* Save layer specific state */
5176   if (cpi->oxcf.number_of_layers > 1) save_layer_context(cpi);
5177
5178   vpx_usec_timer_mark(&cmptimer);
5179   cpi->time_compress_data += vpx_usec_timer_elapsed(&cmptimer);
5180
5181   if (cpi->b_calculate_psnr && cpi->pass != 1 && cm->show_frame) {
5182     generate_psnr_packet(cpi);
5183   }
5184
5185 #if CONFIG_INTERNAL_STATS
5186
5187   if (cpi->pass != 1) {
5188     cpi->bytes += *size;
5189
5190     if (cm->show_frame) {
5191       cpi->common.show_frame_mi = cpi->common.mi;
5192       cpi->count++;
5193
5194       if (cpi->b_calculate_psnr) {
5195         uint64_t ye, ue, ve;
5196         double frame_psnr;
5197         YV12_BUFFER_CONFIG *orig = cpi->Source;
5198         YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
5199         unsigned int y_width = cpi->common.Width;
5200         unsigned int y_height = cpi->common.Height;
5201         unsigned int uv_width = (y_width + 1) / 2;
5202         unsigned int uv_height = (y_height + 1) / 2;
5203         int y_samples = y_height * y_width;
5204         int uv_samples = uv_height * uv_width;
5205         int t_samples = y_samples + 2 * uv_samples;
5206         double sq_error;
5207
5208         ye = calc_plane_error(orig->y_buffer, orig->y_stride, recon->y_buffer,
5209                               recon->y_stride, y_width, y_height);
5210
5211         ue = calc_plane_error(orig->u_buffer, orig->uv_stride, recon->u_buffer,
5212                               recon->uv_stride, uv_width, uv_height);
5213
5214         ve = calc_plane_error(orig->v_buffer, orig->uv_stride, recon->v_buffer,
5215                               recon->uv_stride, uv_width, uv_height);
5216
5217         sq_error = (double)(ye + ue + ve);
5218
5219         frame_psnr = vpx_sse_to_psnr(t_samples, 255.0, sq_error);
5220
5221         cpi->total_y += vpx_sse_to_psnr(y_samples, 255.0, (double)ye);
5222         cpi->total_u += vpx_sse_to_psnr(uv_samples, 255.0, (double)ue);
5223         cpi->total_v += vpx_sse_to_psnr(uv_samples, 255.0, (double)ve);
5224         cpi->total_sq_error += sq_error;
5225         cpi->total += frame_psnr;
5226 #if CONFIG_POSTPROC
5227         {
5228           YV12_BUFFER_CONFIG *pp = &cm->post_proc_buffer;
5229           double sq_error2;
5230           double frame_psnr2, frame_ssim2 = 0;
5231           double weight = 0;
5232
5233           vp8_deblock(cm, cm->frame_to_show, &cm->post_proc_buffer,
5234                       cm->filter_level * 10 / 6, 1, 0);
5235           vpx_clear_system_state();
5236
5237           ye = calc_plane_error(orig->y_buffer, orig->y_stride, pp->y_buffer,
5238                                 pp->y_stride, y_width, y_height);
5239
5240           ue = calc_plane_error(orig->u_buffer, orig->uv_stride, pp->u_buffer,
5241                                 pp->uv_stride, uv_width, uv_height);
5242
5243           ve = calc_plane_error(orig->v_buffer, orig->uv_stride, pp->v_buffer,
5244                                 pp->uv_stride, uv_width, uv_height);
5245
5246           sq_error2 = (double)(ye + ue + ve);
5247
5248           frame_psnr2 = vpx_sse_to_psnr(t_samples, 255.0, sq_error2);
5249
5250           cpi->totalp_y += vpx_sse_to_psnr(y_samples, 255.0, (double)ye);
5251           cpi->totalp_u += vpx_sse_to_psnr(uv_samples, 255.0, (double)ue);
5252           cpi->totalp_v += vpx_sse_to_psnr(uv_samples, 255.0, (double)ve);
5253           cpi->total_sq_error2 += sq_error2;
5254           cpi->totalp += frame_psnr2;
5255
5256           frame_ssim2 =
5257               vpx_calc_ssim(cpi->Source, &cm->post_proc_buffer, &weight);
5258
5259           cpi->summed_quality += frame_ssim2 * weight;
5260           cpi->summed_weights += weight;
5261
5262           if (cpi->oxcf.number_of_layers > 1) {
5263             unsigned int i;
5264
5265             for (i = cpi->current_layer; i < cpi->oxcf.number_of_layers; ++i) {
5266               cpi->frames_in_layer[i]++;
5267
5268               cpi->bytes_in_layer[i] += *size;
5269               cpi->sum_psnr[i] += frame_psnr;
5270               cpi->sum_psnr_p[i] += frame_psnr2;
5271               cpi->total_error2[i] += sq_error;
5272               cpi->total_error2_p[i] += sq_error2;
5273               cpi->sum_ssim[i] += frame_ssim2 * weight;
5274               cpi->sum_weights[i] += weight;
5275             }
5276           }
5277         }
5278 #endif
5279       }
5280     }
5281   }
5282
5283 #if 0
5284
5285     if (cpi->common.frame_type != 0 && cpi->common.base_qindex == cpi->oxcf.worst_allowed_q)
5286     {
5287         skiptruecount += cpi->skip_true_count;
5288         skipfalsecount += cpi->skip_false_count;
5289     }
5290
5291 #endif
5292 #if 0
5293
5294     if (cpi->pass != 1)
5295     {
5296         FILE *f = fopen("skip.stt", "a");
5297         fprintf(f, "frame:%4d flags:%4x Q:%4d P:%4d Size:%5d\n", cpi->common.current_video_frame, *frame_flags, cpi->common.base_qindex, cpi->prob_skip_false, *size);
5298
5299         if (cpi->is_src_frame_alt_ref == 1)
5300             fprintf(f, "skipcount: %4d framesize: %d\n", cpi->skip_true_count , *size);
5301
5302         fclose(f);
5303     }
5304
5305 #endif
5306 #endif
5307
5308   cpi->common.error.setjmp = 0;
5309
5310 #if CONFIG_MULTITHREAD
5311   /* wait for the lpf thread done */
5312   if (cpi->b_multi_threaded && cpi->b_lpf_running) {
5313     sem_wait(&cpi->h_event_end_lpf);
5314     cpi->b_lpf_running = 0;
5315   }
5316 #endif
5317
5318   return 0;
5319 }
5320
5321 int vp8_get_preview_raw_frame(VP8_COMP *cpi, YV12_BUFFER_CONFIG *dest,
5322                               vp8_ppflags_t *flags) {
5323   if (cpi->common.refresh_alt_ref_frame) {
5324     return -1;
5325   } else {
5326     int ret;
5327
5328 #if CONFIG_POSTPROC
5329     cpi->common.show_frame_mi = cpi->common.mi;
5330     ret = vp8_post_proc_frame(&cpi->common, dest, flags);
5331 #else
5332     (void)flags;
5333
5334     if (cpi->common.frame_to_show) {
5335       *dest = *cpi->common.frame_to_show;
5336       dest->y_width = cpi->common.Width;
5337       dest->y_height = cpi->common.Height;
5338       dest->uv_height = cpi->common.Height / 2;
5339       ret = 0;
5340     } else {
5341       ret = -1;
5342     }
5343
5344 #endif
5345     vpx_clear_system_state();
5346     return ret;
5347   }
5348 }
5349
5350 int vp8_set_roimap(VP8_COMP *cpi, unsigned char *map, unsigned int rows,
5351                    unsigned int cols, int delta_q[4], int delta_lf[4],
5352                    unsigned int threshold[4]) {
5353   signed char feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS];
5354   int internal_delta_q[MAX_MB_SEGMENTS];
5355   const int range = 63;
5356   int i;
5357
5358   // This method is currently incompatible with the cyclic refresh method
5359   if (cpi->cyclic_refresh_mode_enabled) return -1;
5360
5361   // Check number of rows and columns match
5362   if (cpi->common.mb_rows != (int)rows || cpi->common.mb_cols != (int)cols) {
5363     return -1;
5364   }
5365
5366   // Range check the delta Q values and convert the external Q range values
5367   // to internal ones.
5368   if ((abs(delta_q[0]) > range) || (abs(delta_q[1]) > range) ||
5369       (abs(delta_q[2]) > range) || (abs(delta_q[3]) > range)) {
5370     return -1;
5371   }
5372
5373   // Range check the delta lf values
5374   if ((abs(delta_lf[0]) > range) || (abs(delta_lf[1]) > range) ||
5375       (abs(delta_lf[2]) > range) || (abs(delta_lf[3]) > range)) {
5376     return -1;
5377   }
5378
5379   if (!map) {
5380     disable_segmentation(cpi);
5381     return 0;
5382   }
5383
5384   // Translate the external delta q values to internal values.
5385   for (i = 0; i < MAX_MB_SEGMENTS; ++i) {
5386     internal_delta_q[i] =
5387         (delta_q[i] >= 0) ? q_trans[delta_q[i]] : -q_trans[-delta_q[i]];
5388   }
5389
5390   /* Set the segmentation Map */
5391   set_segmentation_map(cpi, map);
5392
5393   /* Activate segmentation. */
5394   enable_segmentation(cpi);
5395
5396   /* Set up the quant segment data */
5397   feature_data[MB_LVL_ALT_Q][0] = internal_delta_q[0];
5398   feature_data[MB_LVL_ALT_Q][1] = internal_delta_q[1];
5399   feature_data[MB_LVL_ALT_Q][2] = internal_delta_q[2];
5400   feature_data[MB_LVL_ALT_Q][3] = internal_delta_q[3];
5401
5402   /* Set up the loop segment data s */
5403   feature_data[MB_LVL_ALT_LF][0] = delta_lf[0];
5404   feature_data[MB_LVL_ALT_LF][1] = delta_lf[1];
5405   feature_data[MB_LVL_ALT_LF][2] = delta_lf[2];
5406   feature_data[MB_LVL_ALT_LF][3] = delta_lf[3];
5407
5408   cpi->segment_encode_breakout[0] = threshold[0];
5409   cpi->segment_encode_breakout[1] = threshold[1];
5410   cpi->segment_encode_breakout[2] = threshold[2];
5411   cpi->segment_encode_breakout[3] = threshold[3];
5412
5413   /* Initialise the feature data structure */
5414   set_segment_data(cpi, &feature_data[0][0], SEGMENT_DELTADATA);
5415
5416   return 0;
5417 }
5418
5419 int vp8_set_active_map(VP8_COMP *cpi, unsigned char *map, unsigned int rows,
5420                        unsigned int cols) {
5421   if ((int)rows == cpi->common.mb_rows && (int)cols == cpi->common.mb_cols) {
5422     if (map) {
5423       memcpy(cpi->active_map, map, rows * cols);
5424       cpi->active_map_enabled = 1;
5425     } else {
5426       cpi->active_map_enabled = 0;
5427     }
5428
5429     return 0;
5430   } else {
5431     return -1;
5432   }
5433 }
5434
5435 int vp8_set_internal_size(VP8_COMP *cpi, VPX_SCALING horiz_mode,
5436                           VPX_SCALING vert_mode) {
5437   if (horiz_mode <= ONETWO) {
5438     cpi->common.horiz_scale = horiz_mode;
5439   } else {
5440     return -1;
5441   }
5442
5443   if (vert_mode <= ONETWO) {
5444     cpi->common.vert_scale = vert_mode;
5445   } else {
5446     return -1;
5447   }
5448
5449   return 0;
5450 }
5451
5452 int vp8_calc_ss_err(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *dest) {
5453   int i, j;
5454   int Total = 0;
5455
5456   unsigned char *src = source->y_buffer;
5457   unsigned char *dst = dest->y_buffer;
5458
5459   /* Loop through the Y plane raw and reconstruction data summing
5460    * (square differences)
5461    */
5462   for (i = 0; i < source->y_height; i += 16) {
5463     for (j = 0; j < source->y_width; j += 16) {
5464       unsigned int sse;
5465       Total += vpx_mse16x16(src + j, source->y_stride, dst + j, dest->y_stride,
5466                             &sse);
5467     }
5468
5469     src += 16 * source->y_stride;
5470     dst += 16 * dest->y_stride;
5471   }
5472
5473   return Total;
5474 }
5475
5476 int vp8_get_quantizer(VP8_COMP *cpi) { return cpi->common.base_qindex; }