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