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