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