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