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