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