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