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