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