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