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