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