2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
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.
12 #include "vpx_config.h"
13 #include "vp8/common/onyxc_int.h"
15 #include "vp8/common/systemdependent.h"
17 #include "vp8/common/alloccommon.h"
19 #include "firstpass.h"
21 #include "vpx_scale/vpxscale.h"
22 #include "vp8/common/extend.h"
24 #include "vp8/common/quant_common.h"
25 #include "segmentation.h"
26 #include "vp8/common/g_common.h"
27 #include "vpx_scale/yv12extend.h"
29 #include "vp8/common/postproc.h"
31 #include "vpx_mem/vpx_mem.h"
32 #include "vp8/common/swapyv12buffer.h"
33 #include "vp8/common/threading.h"
34 #include "vpx_ports/vpx_timer.h"
35 #include "temporal_filter.h"
37 #include "vpx_ports/arm.h"
39 #if CONFIG_MULTI_RES_ENCODING
40 #include "mr_dissim.h"
47 #if CONFIG_RUNTIME_CPU_DETECT
48 #define IF_RTCD(x) (x)
49 #define RTCD(x) &cpi->common.rtcd.x
51 #define IF_RTCD(x) NULL
55 extern void vp8cx_pick_filter_level_fast(YV12_BUFFER_CONFIG *sd, VP8_COMP *cpi);
56 extern void vp8cx_set_alt_lf_level(VP8_COMP *cpi, int filt_val);
57 extern void vp8cx_pick_filter_level(YV12_BUFFER_CONFIG *sd, VP8_COMP *cpi);
59 extern void vp8_dmachine_specific_config(VP8_COMP *cpi);
60 extern void vp8_cmachine_specific_config(VP8_COMP *cpi);
61 extern void vp8_deblock_frame(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *post, int filt_lvl, int low_var_thresh, int flag);
62 extern void print_parms(VP8_CONFIG *ocf, char *filenam);
63 extern unsigned int vp8_get_processor_freq();
64 extern void print_tree_update_probs();
65 extern void vp8cx_create_encoder_threads(VP8_COMP *cpi);
66 extern void vp8cx_remove_encoder_threads(VP8_COMP *cpi);
68 extern void vp8_yv12_copy_frame_func_neon(YV12_BUFFER_CONFIG *src_ybc, YV12_BUFFER_CONFIG *dst_ybc);
69 extern void vp8_yv12_copy_src_frame_func_neon(YV12_BUFFER_CONFIG *src_ybc, YV12_BUFFER_CONFIG *dst_ybc);
72 int vp8_estimate_entropy_savings(VP8_COMP *cpi);
74 int vp8_calc_ss_err(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *dest, const vp8_variance_rtcd_vtable_t *rtcd);
76 extern void vp8_temporal_filter_prepare_c(VP8_COMP *cpi, int distance);
78 static void set_default_lf_deltas(VP8_COMP *cpi);
80 extern const int vp8_gf_interval_table[101];
82 #if CONFIG_INTERNAL_STATS
85 extern double vp8_calc_ssim
87 YV12_BUFFER_CONFIG *source,
88 YV12_BUFFER_CONFIG *dest,
91 const vp8_variance_rtcd_vtable_t *rtcd
95 extern double vp8_calc_ssimg
97 YV12_BUFFER_CONFIG *source,
98 YV12_BUFFER_CONFIG *dest,
102 const vp8_variance_rtcd_vtable_t *rtcd
109 #ifdef OUTPUT_YUV_SRC
120 extern int skip_true_count;
121 extern int skip_false_count;
126 extern int intra_mode_stats[10][10][10];
130 unsigned int frames_at_speed[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
131 unsigned int tot_pm = 0;
132 unsigned int cnt_pm = 0;
133 unsigned int tot_ef = 0;
134 unsigned int cnt_ef = 0;
138 extern unsigned __int64 Sectionbits[50];
139 extern int y_modes[5] ;
140 extern int uv_modes[4] ;
141 extern int b_modes[10] ;
143 extern int inter_y_modes[10] ;
144 extern int inter_uv_modes[4] ;
145 extern unsigned int inter_b_modes[15];
148 extern void (*vp8_short_fdct4x4)(short *input, short *output, int pitch);
149 extern void (*vp8_short_fdct8x4)(short *input, short *output, int pitch);
151 extern const int vp8_bits_per_mb[2][QINDEX_RANGE];
153 extern const int qrounding_factors[129];
154 extern const int qzbin_factors[129];
155 extern void vp8cx_init_quantizer(VP8_COMP *cpi);
156 extern const int vp8cx_base_skip_false_prob[128];
158 // Tables relating active max Q to active min Q
159 static const int kf_low_motion_minq[QINDEX_RANGE] =
161 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
162 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
163 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
164 0,0,0,0,1,1,1,1,1,1,1,1,2,2,2,2,
165 3,3,3,3,3,3,4,4,4,5,5,5,5,5,6,6,
166 6,6,7,7,8,8,8,8,9,9,10,10,10,10,11,11,
167 11,11,12,12,13,13,13,13,14,14,15,15,15,15,16,16,
168 16,16,17,17,18,18,18,18,19,20,20,21,21,22,23,23
170 static const int kf_high_motion_minq[QINDEX_RANGE] =
172 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
173 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
174 1,1,1,1,1,1,1,1,2,2,2,2,3,3,3,3,
175 3,3,3,3,4,4,4,4,5,5,5,5,5,5,6,6,
176 6,6,7,7,8,8,8,8,9,9,10,10,10,10,11,11,
177 11,11,12,12,13,13,13,13,14,14,15,15,15,15,16,16,
178 16,16,17,17,18,18,18,18,19,19,20,20,20,20,21,21,
179 21,21,22,22,23,23,24,25,25,26,26,27,28,28,29,30
181 static const int gf_low_motion_minq[QINDEX_RANGE] =
183 0,0,0,0,1,1,1,1,1,1,1,1,2,2,2,2,
184 3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,
185 7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10,
186 11,11,12,12,13,13,14,14,15,15,16,16,17,17,18,18,
187 19,19,20,20,21,21,22,22,23,23,24,24,25,25,26,26,
188 27,27,28,28,29,29,30,30,31,31,32,32,33,33,34,34,
189 35,35,36,36,37,37,38,38,39,39,40,40,41,41,42,42,
190 43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58
192 static const int gf_mid_motion_minq[QINDEX_RANGE] =
194 0,0,0,0,1,1,1,1,1,1,2,2,3,3,3,4,
195 4,4,5,5,5,6,6,6,7,7,7,8,8,8,9,9,
196 9,10,10,10,10,11,11,11,12,12,12,12,13,13,13,14,
197 14,14,15,15,16,16,17,17,18,18,19,19,20,20,21,21,
198 22,22,23,23,24,24,25,25,26,26,27,27,28,28,29,29,
199 30,30,31,31,32,32,33,33,34,34,35,35,36,36,37,37,
200 38,39,39,40,40,41,41,42,42,43,43,44,45,46,47,48,
201 49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64
203 static const int gf_high_motion_minq[QINDEX_RANGE] =
205 0,0,0,0,1,1,1,1,1,2,2,2,3,3,3,4,
206 4,4,5,5,5,6,6,6,7,7,7,8,8,8,9,9,
207 9,10,10,10,11,11,12,12,13,13,14,14,15,15,16,16,
208 17,17,18,18,19,19,20,20,21,21,22,22,23,23,24,24,
209 25,25,26,26,27,27,28,28,29,29,30,30,31,31,32,32,
210 33,33,34,34,35,35,36,36,37,37,38,38,39,39,40,40,
211 41,41,42,42,43,44,45,46,47,48,49,50,51,52,53,54,
212 55,56,57,58,59,60,62,64,66,68,70,72,74,76,78,80
214 static const int inter_minq[QINDEX_RANGE] =
216 0,0,1,1,2,3,3,4,4,5,6,6,7,8,8,9,
217 9,10,11,11,12,13,13,14,15,15,16,17,17,18,19,20,
218 20,21,22,22,23,24,24,25,26,27,27,28,29,30,30,31,
219 32,33,33,34,35,36,36,37,38,39,39,40,41,42,42,43,
220 44,45,46,46,47,48,49,50,50,51,52,53,54,55,55,56,
221 57,58,59,60,60,61,62,63,64,65,66,67,67,68,69,70,
222 71,72,73,74,75,75,76,77,78,79,80,81,82,83,84,85,
223 86,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100
226 void vp8_initialize()
228 static int init_done = 0;
232 vp8_scale_machine_specific_config();
233 vp8_initialize_common();
234 //vp8_dmachine_specific_config();
235 vp8_tokenize_initialize();
240 #ifdef PACKET_TESTING
241 extern FILE *vpxlogc;
244 static void save_layer_context(VP8_COMP *cpi)
246 LAYER_CONTEXT *lc = &cpi->layer_context[cpi->current_layer];
248 // Save layer dependent coding state
249 lc->target_bandwidth = cpi->target_bandwidth;
250 //lc->target_bandwidth = cpi->oxcf.target_bandwidth;
251 lc->starting_buffer_level = cpi->oxcf.starting_buffer_level;
252 lc->optimal_buffer_level = cpi->oxcf.optimal_buffer_level;
253 lc->maximum_buffer_size = cpi->oxcf.maximum_buffer_size;
254 lc->buffer_level = cpi->buffer_level;
255 lc->bits_off_target = cpi->bits_off_target;
256 lc->total_actual_bits = cpi->total_actual_bits;
257 lc->worst_quality = cpi->worst_quality;
258 lc->active_worst_quality = cpi->active_worst_quality;
259 lc->best_quality = cpi->best_quality;
260 lc->active_best_quality = cpi->active_best_quality;
261 lc->ni_av_qi = cpi->ni_av_qi;
262 lc->ni_tot_qi = cpi->ni_tot_qi;
263 lc->ni_frames = cpi->ni_frames;
264 lc->avg_frame_qindex = cpi->avg_frame_qindex;
265 lc->rate_correction_factor = cpi->rate_correction_factor;
266 lc->key_frame_rate_correction_factor = cpi->key_frame_rate_correction_factor;
267 lc->gf_rate_correction_factor = cpi->gf_rate_correction_factor;
268 lc->zbin_over_quant = cpi->zbin_over_quant;
269 lc->inter_frame_target = cpi->inter_frame_target;
270 lc->total_byte_count = cpi->total_byte_count;
271 lc->filter_level = cpi->common.filter_level;
273 lc->last_frame_percent_intra = cpi->last_frame_percent_intra;
275 memcpy (lc->count_mb_ref_frame_usage,
276 cpi->count_mb_ref_frame_usage,
277 sizeof(cpi->count_mb_ref_frame_usage));
280 static void restore_layer_context(VP8_COMP *cpi, const int layer)
282 LAYER_CONTEXT *lc = &cpi->layer_context[layer];
284 // Restore layer dependent coding state
285 cpi->current_layer = layer;
286 cpi->target_bandwidth = lc->target_bandwidth;
287 cpi->oxcf.target_bandwidth = lc->target_bandwidth;
288 cpi->oxcf.starting_buffer_level = lc->starting_buffer_level;
289 cpi->oxcf.optimal_buffer_level = lc->optimal_buffer_level;
290 cpi->oxcf.maximum_buffer_size = lc->maximum_buffer_size;
291 cpi->buffer_level = lc->buffer_level;
292 cpi->bits_off_target = lc->bits_off_target;
293 cpi->total_actual_bits = lc->total_actual_bits;
294 //cpi->worst_quality = lc->worst_quality;
295 cpi->active_worst_quality = lc->active_worst_quality;
296 //cpi->best_quality = lc->best_quality;
297 cpi->active_best_quality = lc->active_best_quality;
298 cpi->ni_av_qi = lc->ni_av_qi;
299 cpi->ni_tot_qi = lc->ni_tot_qi;
300 cpi->ni_frames = lc->ni_frames;
301 cpi->avg_frame_qindex = lc->avg_frame_qindex;
302 cpi->rate_correction_factor = lc->rate_correction_factor;
303 cpi->key_frame_rate_correction_factor = lc->key_frame_rate_correction_factor;
304 cpi->gf_rate_correction_factor = lc->gf_rate_correction_factor;
305 cpi->zbin_over_quant = lc->zbin_over_quant;
306 cpi->inter_frame_target = lc->inter_frame_target;
307 cpi->total_byte_count = lc->total_byte_count;
308 cpi->common.filter_level = lc->filter_level;
310 cpi->last_frame_percent_intra = lc->last_frame_percent_intra;
312 memcpy (cpi->count_mb_ref_frame_usage,
313 lc->count_mb_ref_frame_usage,
314 sizeof(cpi->count_mb_ref_frame_usage));
317 static void setup_features(VP8_COMP *cpi)
319 // Set up default state for MB feature flags
320 cpi->mb.e_mbd.segmentation_enabled = 0;
321 cpi->mb.e_mbd.update_mb_segmentation_map = 0;
322 cpi->mb.e_mbd.update_mb_segmentation_data = 0;
323 vpx_memset(cpi->mb.e_mbd.mb_segment_tree_probs, 255, sizeof(cpi->mb.e_mbd.mb_segment_tree_probs));
324 vpx_memset(cpi->mb.e_mbd.segment_feature_data, 0, sizeof(cpi->mb.e_mbd.segment_feature_data));
326 cpi->mb.e_mbd.mode_ref_lf_delta_enabled = 0;
327 cpi->mb.e_mbd.mode_ref_lf_delta_update = 0;
328 vpx_memset(cpi->mb.e_mbd.ref_lf_deltas, 0, sizeof(cpi->mb.e_mbd.ref_lf_deltas));
329 vpx_memset(cpi->mb.e_mbd.mode_lf_deltas, 0, sizeof(cpi->mb.e_mbd.mode_lf_deltas));
330 vpx_memset(cpi->mb.e_mbd.last_ref_lf_deltas, 0, sizeof(cpi->mb.e_mbd.ref_lf_deltas));
331 vpx_memset(cpi->mb.e_mbd.last_mode_lf_deltas, 0, sizeof(cpi->mb.e_mbd.mode_lf_deltas));
333 set_default_lf_deltas(cpi);
338 static void dealloc_compressor_data(VP8_COMP *cpi)
340 vpx_free(cpi->tplist);
343 // Delete last frame MV storage buffers
347 vpx_free(cpi->lf_ref_frame_sign_bias);
348 cpi->lf_ref_frame_sign_bias = 0;
350 vpx_free(cpi->lf_ref_frame);
351 cpi->lf_ref_frame = 0;
353 // Delete sementation map
354 vpx_free(cpi->segmentation_map);
355 cpi->segmentation_map = 0;
357 vpx_free(cpi->active_map);
360 vp8_de_alloc_frame_buffers(&cpi->common);
362 vp8_yv12_de_alloc_frame_buffer(&cpi->pick_lf_lvl_frame);
363 vp8_yv12_de_alloc_frame_buffer(&cpi->scaled_source);
364 #if VP8_TEMPORAL_ALT_REF
365 vp8_yv12_de_alloc_frame_buffer(&cpi->alt_ref_buffer);
367 vp8_lookahead_destroy(cpi->lookahead);
372 // Structure used to monitor GF usage
373 vpx_free(cpi->gf_active_flags);
374 cpi->gf_active_flags = 0;
376 // Activity mask based per mb zbin adjustments
377 vpx_free(cpi->mb_activity_map);
378 cpi->mb_activity_map = 0;
379 vpx_free(cpi->mb_norm_activity_map);
380 cpi->mb_norm_activity_map = 0;
382 vpx_free(cpi->mb.pip);
385 #if !(CONFIG_REALTIME_ONLY)
386 vpx_free(cpi->twopass.total_stats);
387 cpi->twopass.total_stats = 0;
389 vpx_free(cpi->twopass.total_left_stats);
390 cpi->twopass.total_left_stats = 0;
392 vpx_free(cpi->twopass.this_frame_stats);
393 cpi->twopass.this_frame_stats = 0;
397 static void enable_segmentation(VP8_PTR ptr)
399 VP8_COMP *cpi = (VP8_COMP *)(ptr);
401 // Set the appropriate feature bit
402 cpi->mb.e_mbd.segmentation_enabled = 1;
403 cpi->mb.e_mbd.update_mb_segmentation_map = 1;
404 cpi->mb.e_mbd.update_mb_segmentation_data = 1;
406 static void disable_segmentation(VP8_PTR ptr)
408 VP8_COMP *cpi = (VP8_COMP *)(ptr);
410 // Clear the appropriate feature bit
411 cpi->mb.e_mbd.segmentation_enabled = 0;
414 // Valid values for a segment are 0 to 3
415 // Segmentation map is arrange as [Rows][Columns]
416 static void set_segmentation_map(VP8_PTR ptr, unsigned char *segmentation_map)
418 VP8_COMP *cpi = (VP8_COMP *)(ptr);
420 // Copy in the new segmentation map
421 vpx_memcpy(cpi->segmentation_map, segmentation_map, (cpi->common.mb_rows * cpi->common.mb_cols));
423 // Signal that the map should be updated.
424 cpi->mb.e_mbd.update_mb_segmentation_map = 1;
425 cpi->mb.e_mbd.update_mb_segmentation_data = 1;
428 // The values given for each segment can be either deltas (from the default value chosen for the frame) or absolute values.
430 // Valid range for abs values is (0-127 for MB_LVL_ALT_Q) , (0-63 for SEGMENT_ALT_LF)
431 // Valid range for delta values are (+/-127 for MB_LVL_ALT_Q) , (+/-63 for SEGMENT_ALT_LF)
433 // abs_delta = SEGMENT_DELTADATA (deltas) abs_delta = SEGMENT_ABSDATA (use the absolute values given).
436 static void set_segment_data(VP8_PTR ptr, signed char *feature_data, unsigned char abs_delta)
438 VP8_COMP *cpi = (VP8_COMP *)(ptr);
440 cpi->mb.e_mbd.mb_segement_abs_delta = abs_delta;
441 vpx_memcpy(cpi->segment_feature_data, feature_data, sizeof(cpi->segment_feature_data));
445 static void segmentation_test_function(VP8_PTR ptr)
447 VP8_COMP *cpi = (VP8_COMP *)(ptr);
449 unsigned char *seg_map;
450 signed char feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS];
452 // Create a temporary map for segmentation data.
453 CHECK_MEM_ERROR(seg_map, vpx_calloc(cpi->common.mb_rows * cpi->common.mb_cols, 1));
455 // MB loop to set local segmentation map
456 /*for ( i = 0; i < cpi->common.mb_rows; i++ )
458 for ( j = 0; j < cpi->common.mb_cols; j++ )
460 //seg_map[(i*cpi->common.mb_cols) + j] = (j % 2) + ((i%2)* 2);
461 //if ( j < cpi->common.mb_cols/2 )
463 // Segment 1 around the edge else 0
464 if ( (i == 0) || (j == 0) || (i == (cpi->common.mb_rows-1)) || (j == (cpi->common.mb_cols-1)) )
465 seg_map[(i*cpi->common.mb_cols) + j] = 1;
466 //else if ( (i < 2) || (j < 2) || (i > (cpi->common.mb_rows-3)) || (j > (cpi->common.mb_cols-3)) )
467 // seg_map[(i*cpi->common.mb_cols) + j] = 2;
468 //else if ( (i < 5) || (j < 5) || (i > (cpi->common.mb_rows-6)) || (j > (cpi->common.mb_cols-6)) )
469 // seg_map[(i*cpi->common.mb_cols) + j] = 3;
471 seg_map[(i*cpi->common.mb_cols) + j] = 0;
475 // Set the segmentation Map
476 set_segmentation_map(ptr, seg_map);
478 // Activate segmentation.
479 enable_segmentation(ptr);
481 // Set up the quant segment data
482 feature_data[MB_LVL_ALT_Q][0] = 0;
483 feature_data[MB_LVL_ALT_Q][1] = 4;
484 feature_data[MB_LVL_ALT_Q][2] = 0;
485 feature_data[MB_LVL_ALT_Q][3] = 0;
486 // Set up the loop segment data
487 feature_data[MB_LVL_ALT_LF][0] = 0;
488 feature_data[MB_LVL_ALT_LF][1] = 0;
489 feature_data[MB_LVL_ALT_LF][2] = 0;
490 feature_data[MB_LVL_ALT_LF][3] = 0;
492 // Initialise the feature data structure
493 // SEGMENT_DELTADATA 0, SEGMENT_ABSDATA 1
494 set_segment_data(ptr, &feature_data[0][0], SEGMENT_DELTADATA);
496 // Delete sementation map
503 // A simple function to cyclically refresh the background at a lower Q
504 static void cyclic_background_refresh(VP8_COMP *cpi, int Q, int lf_adjustment)
506 unsigned char *seg_map;
507 signed char feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS];
509 int block_count = cpi->cyclic_refresh_mode_max_mbs_perframe;
510 int mbs_in_frame = cpi->common.mb_rows * cpi->common.mb_cols;
512 // Create a temporary map for segmentation data.
513 CHECK_MEM_ERROR(seg_map, vpx_calloc(cpi->common.mb_rows * cpi->common.mb_cols, 1));
515 cpi->cyclic_refresh_q = Q;
517 for (i = Q; i > 0; i--)
519 if (vp8_bits_per_mb[cpi->common.frame_type][i] >= ((vp8_bits_per_mb[cpi->common.frame_type][Q]*(Q + 128)) / 64))
520 //if ( vp8_bits_per_mb[cpi->common.frame_type][i] >= ((vp8_bits_per_mb[cpi->common.frame_type][Q]*((2*Q)+96))/64) )
526 cpi->cyclic_refresh_q = i;
528 // Only update for inter frames
529 if (cpi->common.frame_type != KEY_FRAME)
531 // Cycle through the macro_block rows
532 // MB loop to set local segmentation map
533 for (i = cpi->cyclic_refresh_mode_index; i < mbs_in_frame; i++)
535 // If the MB is as a candidate for clean up then mark it for possible boost/refresh (segment 1)
536 // The segment id may get reset to 0 later if the MB gets coded anything other than last frame 0,0
537 // as only (last frame 0,0) MBs are eligable for refresh : that is to say Mbs likely to be background blocks.
538 if (cpi->cyclic_refresh_map[i] == 0)
546 // Skip blocks that have been refreshed recently anyway.
547 if (cpi->cyclic_refresh_map[i] < 0)
548 //cpi->cyclic_refresh_map[i] = cpi->cyclic_refresh_map[i] / 16;
549 cpi->cyclic_refresh_map[i]++;
560 // If we have gone through the frame reset to the start
561 cpi->cyclic_refresh_mode_index = i;
563 if (cpi->cyclic_refresh_mode_index >= mbs_in_frame)
564 cpi->cyclic_refresh_mode_index = 0;
567 // Set the segmentation Map
568 set_segmentation_map((VP8_PTR)cpi, seg_map);
570 // Activate segmentation.
571 enable_segmentation((VP8_PTR)cpi);
573 // Set up the quant segment data
574 feature_data[MB_LVL_ALT_Q][0] = 0;
575 feature_data[MB_LVL_ALT_Q][1] = (cpi->cyclic_refresh_q - Q);
576 feature_data[MB_LVL_ALT_Q][2] = 0;
577 feature_data[MB_LVL_ALT_Q][3] = 0;
579 // Set up the loop segment data
580 feature_data[MB_LVL_ALT_LF][0] = 0;
581 feature_data[MB_LVL_ALT_LF][1] = lf_adjustment;
582 feature_data[MB_LVL_ALT_LF][2] = 0;
583 feature_data[MB_LVL_ALT_LF][3] = 0;
585 // Initialise the feature data structure
586 // SEGMENT_DELTADATA 0, SEGMENT_ABSDATA 1
587 set_segment_data((VP8_PTR)cpi, &feature_data[0][0], SEGMENT_DELTADATA);
589 // Delete sementation map
596 static void set_default_lf_deltas(VP8_COMP *cpi)
598 cpi->mb.e_mbd.mode_ref_lf_delta_enabled = 1;
599 cpi->mb.e_mbd.mode_ref_lf_delta_update = 1;
601 vpx_memset(cpi->mb.e_mbd.ref_lf_deltas, 0, sizeof(cpi->mb.e_mbd.ref_lf_deltas));
602 vpx_memset(cpi->mb.e_mbd.mode_lf_deltas, 0, sizeof(cpi->mb.e_mbd.mode_lf_deltas));
604 // Test of ref frame deltas
605 cpi->mb.e_mbd.ref_lf_deltas[INTRA_FRAME] = 2;
606 cpi->mb.e_mbd.ref_lf_deltas[LAST_FRAME] = 0;
607 cpi->mb.e_mbd.ref_lf_deltas[GOLDEN_FRAME] = -2;
608 cpi->mb.e_mbd.ref_lf_deltas[ALTREF_FRAME] = -2;
610 cpi->mb.e_mbd.mode_lf_deltas[0] = 4; // BPRED
611 cpi->mb.e_mbd.mode_lf_deltas[1] = -2; // Zero
612 cpi->mb.e_mbd.mode_lf_deltas[2] = 2; // New mv
613 cpi->mb.e_mbd.mode_lf_deltas[3] = 4; // Split mv
616 void vp8_set_speed_features(VP8_COMP *cpi)
618 SPEED_FEATURES *sf = &cpi->sf;
619 int Mode = cpi->compressor_speed;
620 int Speed = cpi->Speed;
622 VP8_COMMON *cm = &cpi->common;
623 int last_improved_quant = sf->improved_quant;
626 // Initialise default mode frequency sampling variables
627 for (i = 0; i < MAX_MODES; i ++)
629 cpi->mode_check_freq[i] = 0;
630 cpi->mode_test_hit_counts[i] = 0;
631 cpi->mode_chosen_counts[i] = 0;
634 cpi->mbs_tested_so_far = 0;
636 // best quality defaults
638 sf->search_method = NSTEP;
639 sf->improved_quant = 1;
640 sf->improved_dct = 1;
643 sf->quarter_pixel_search = 1;
644 sf->half_pixel_search = 1;
645 sf->iterative_sub_pixel = 1;
646 sf->optimize_coefficients = 1;
647 sf->use_fastquant_for_pick = 0;
648 sf->no_skip_block4x4_search = 1;
651 sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
652 sf->improved_mv_pred = 1;
654 // default thresholds to 0
655 for (i = 0; i < MAX_MODES; i++)
656 sf->thresh_mult[i] = 0;
658 /* Count enabled references */
660 if (cpi->ref_frame_flags & VP8_LAST_FLAG)
662 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
664 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
669 #if !(CONFIG_REALTIME_ONLY)
670 case 0: // best quality mode
671 sf->thresh_mult[THR_ZERO1 ] = 0;
672 sf->thresh_mult[THR_ZERO2 ] = 0;
673 sf->thresh_mult[THR_ZERO3 ] = 0;
674 sf->thresh_mult[THR_NEAREST1 ] = 0;
675 sf->thresh_mult[THR_NEAREST2 ] = 0;
676 sf->thresh_mult[THR_NEAREST3 ] = 0;
677 sf->thresh_mult[THR_NEAR1 ] = 0;
678 sf->thresh_mult[THR_NEAR2 ] = 0;
679 sf->thresh_mult[THR_NEAR3 ] = 0;
681 sf->thresh_mult[THR_DC ] = 0;
683 sf->thresh_mult[THR_V_PRED ] = 1000;
684 sf->thresh_mult[THR_H_PRED ] = 1000;
685 sf->thresh_mult[THR_B_PRED ] = 2000;
686 sf->thresh_mult[THR_TM ] = 1000;
688 sf->thresh_mult[THR_NEW1 ] = 1000;
689 sf->thresh_mult[THR_NEW2 ] = 1000;
690 sf->thresh_mult[THR_NEW3 ] = 1000;
692 sf->thresh_mult[THR_SPLIT1 ] = 2500;
693 sf->thresh_mult[THR_SPLIT2 ] = 5000;
694 sf->thresh_mult[THR_SPLIT3 ] = 5000;
698 sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
702 sf->thresh_mult[THR_NEAREST1 ] = 0;
703 sf->thresh_mult[THR_ZERO1 ] = 0;
704 sf->thresh_mult[THR_DC ] = 0;
705 sf->thresh_mult[THR_NEAR1 ] = 0;
706 sf->thresh_mult[THR_V_PRED ] = 1000;
707 sf->thresh_mult[THR_H_PRED ] = 1000;
708 sf->thresh_mult[THR_B_PRED ] = 2500;
709 sf->thresh_mult[THR_TM ] = 1000;
711 sf->thresh_mult[THR_NEAREST2 ] = 1000;
712 sf->thresh_mult[THR_NEAREST3 ] = 1000;
714 sf->thresh_mult[THR_ZERO2 ] = 1000;
715 sf->thresh_mult[THR_ZERO3 ] = 1000;
716 sf->thresh_mult[THR_NEAR2 ] = 1000;
717 sf->thresh_mult[THR_NEAR3 ] = 1000;
720 sf->thresh_mult[THR_ZERO1 ] = 0;
721 sf->thresh_mult[THR_ZERO2 ] = 0;
722 sf->thresh_mult[THR_ZERO3 ] = 0;
723 sf->thresh_mult[THR_NEAREST1 ] = 0;
724 sf->thresh_mult[THR_NEAREST2 ] = 0;
725 sf->thresh_mult[THR_NEAREST3 ] = 0;
726 sf->thresh_mult[THR_NEAR1 ] = 0;
727 sf->thresh_mult[THR_NEAR2 ] = 0;
728 sf->thresh_mult[THR_NEAR3 ] = 0;
730 // sf->thresh_mult[THR_DC ] = 0;
732 // sf->thresh_mult[THR_V_PRED ] = 1000;
733 // sf->thresh_mult[THR_H_PRED ] = 1000;
734 // sf->thresh_mult[THR_B_PRED ] = 2000;
735 // sf->thresh_mult[THR_TM ] = 1000;
737 sf->thresh_mult[THR_NEW1 ] = 1000;
738 sf->thresh_mult[THR_NEW2 ] = 1000;
739 sf->thresh_mult[THR_NEW3 ] = 1000;
741 sf->thresh_mult[THR_SPLIT1 ] = 1700;
742 sf->thresh_mult[THR_SPLIT2 ] = 4500;
743 sf->thresh_mult[THR_SPLIT3 ] = 4500;
745 sf->thresh_mult[THR_NEW1 ] = 1500;
746 sf->thresh_mult[THR_NEW2 ] = 1500;
747 sf->thresh_mult[THR_NEW3 ] = 1500;
749 sf->thresh_mult[THR_SPLIT1 ] = 5000;
750 sf->thresh_mult[THR_SPLIT2 ] = 10000;
751 sf->thresh_mult[THR_SPLIT3 ] = 10000;
756 /* Disable coefficient optimization above speed 0 */
757 sf->optimize_coefficients = 0;
758 sf->use_fastquant_for_pick = 1;
759 sf->no_skip_block4x4_search = 0;
763 cpi->mode_check_freq[THR_SPLIT2] = 2;
764 cpi->mode_check_freq[THR_SPLIT3] = 2;
765 cpi->mode_check_freq[THR_SPLIT1 ] = 0;
770 cpi->mode_check_freq[THR_SPLIT2] = 4;
771 cpi->mode_check_freq[THR_SPLIT3] = 4;
772 cpi->mode_check_freq[THR_SPLIT1 ] = 2;
774 sf->thresh_mult[THR_TM ] = 1500;
775 sf->thresh_mult[THR_V_PRED ] = 1500;
776 sf->thresh_mult[THR_H_PRED ] = 1500;
777 sf->thresh_mult[THR_B_PRED ] = 5000;
781 sf->thresh_mult[THR_NEW1 ] = 2000;
782 sf->thresh_mult[THR_SPLIT1 ] = 10000;
787 sf->thresh_mult[THR_NEAREST2 ] = 1500;
788 sf->thresh_mult[THR_ZERO2 ] = 1500;
789 sf->thresh_mult[THR_NEAR2 ] = 1500;
790 sf->thresh_mult[THR_NEW2 ] = 2000;
791 sf->thresh_mult[THR_SPLIT2 ] = 20000;
796 sf->thresh_mult[THR_NEAREST3 ] = 1500;
797 sf->thresh_mult[THR_ZERO3 ] = 1500;
798 sf->thresh_mult[THR_NEAR3 ] = 1500;
799 sf->thresh_mult[THR_NEW3 ] = 2000;
800 sf->thresh_mult[THR_SPLIT3 ] = 20000;
806 cpi->mode_check_freq[THR_SPLIT2] = 15;
807 cpi->mode_check_freq[THR_SPLIT3] = 15;
808 cpi->mode_check_freq[THR_SPLIT1 ] = 7;
810 sf->thresh_mult[THR_TM ] = 2000;
811 sf->thresh_mult[THR_V_PRED ] = 2000;
812 sf->thresh_mult[THR_H_PRED ] = 2000;
813 sf->thresh_mult[THR_B_PRED ] = 7500;
817 sf->thresh_mult[THR_NEW1 ] = 2000;
818 sf->thresh_mult[THR_SPLIT1 ] = 25000;
823 sf->thresh_mult[THR_NEAREST2 ] = 2000;
824 sf->thresh_mult[THR_ZERO2 ] = 2000;
825 sf->thresh_mult[THR_NEAR2 ] = 2000;
826 sf->thresh_mult[THR_NEW2 ] = 2500;
827 sf->thresh_mult[THR_SPLIT2 ] = 50000;
832 sf->thresh_mult[THR_NEAREST3 ] = 2000;
833 sf->thresh_mult[THR_ZERO3 ] = 2000;
834 sf->thresh_mult[THR_NEAR3 ] = 2000;
835 sf->thresh_mult[THR_NEW3 ] = 2500;
836 sf->thresh_mult[THR_SPLIT3 ] = 50000;
839 sf->improved_quant = 0;
840 sf->improved_dct = 0;
842 // Only do recode loop on key frames, golden frames and
850 sf->thresh_mult[THR_SPLIT3 ] = INT_MAX;
851 sf->thresh_mult[THR_SPLIT2 ] = INT_MAX;
852 sf->thresh_mult[THR_SPLIT1 ] = INT_MAX;
854 cpi->mode_check_freq[THR_V_PRED] = 0;
855 cpi->mode_check_freq[THR_H_PRED] = 0;
856 cpi->mode_check_freq[THR_B_PRED] = 0;
857 cpi->mode_check_freq[THR_NEAR2] = 0;
858 cpi->mode_check_freq[THR_NEW2] = 0;
859 cpi->mode_check_freq[THR_NEAR3] = 0;
860 cpi->mode_check_freq[THR_NEW3] = 0;
863 sf->recode_loop = 0; // recode loop off
864 sf->RD = 0; // Turn rd off
870 sf->auto_filter = 0; // Faster selection of loop filter
872 cpi->mode_check_freq[THR_V_PRED] = 2;
873 cpi->mode_check_freq[THR_H_PRED] = 2;
874 cpi->mode_check_freq[THR_B_PRED] = 2;
878 cpi->mode_check_freq[THR_NEAR2] = 2;
879 cpi->mode_check_freq[THR_NEW2] = 4;
884 cpi->mode_check_freq[THR_NEAR3] = 2;
885 cpi->mode_check_freq[THR_NEW3] = 4;
890 sf->thresh_mult[THR_NEAREST2 ] = 2000;
891 sf->thresh_mult[THR_ZERO2 ] = 2000;
892 sf->thresh_mult[THR_NEAR2 ] = 2000;
893 sf->thresh_mult[THR_NEW2 ] = 4000;
898 sf->thresh_mult[THR_NEAREST3 ] = 2000;
899 sf->thresh_mult[THR_ZERO3 ] = 2000;
900 sf->thresh_mult[THR_NEAR3 ] = 2000;
901 sf->thresh_mult[THR_NEW3 ] = 4000;
908 sf->optimize_coefficients = 0;
911 sf->iterative_sub_pixel = 1;
912 sf->thresh_mult[THR_NEAREST1 ] = 0;
913 sf->thresh_mult[THR_ZERO1 ] = 0;
914 sf->thresh_mult[THR_DC ] = 0;
915 sf->thresh_mult[THR_TM ] = 0;
916 sf->thresh_mult[THR_NEAR1 ] = 0;
917 sf->thresh_mult[THR_V_PRED ] = 1000;
918 sf->thresh_mult[THR_H_PRED ] = 1000;
919 sf->thresh_mult[THR_B_PRED ] = 2500;
920 sf->thresh_mult[THR_NEAREST2 ] = 1000;
921 sf->thresh_mult[THR_ZERO2 ] = 1000;
922 sf->thresh_mult[THR_NEAR2 ] = 1000;
923 sf->thresh_mult[THR_NEAREST3 ] = 1000;
924 sf->thresh_mult[THR_ZERO3 ] = 1000;
925 sf->thresh_mult[THR_NEAR3 ] = 1000;
926 sf->thresh_mult[THR_NEW1 ] = 2000;
927 sf->thresh_mult[THR_NEW2 ] = 2000;
928 sf->thresh_mult[THR_NEW3 ] = 2000;
929 sf->thresh_mult[THR_SPLIT1 ] = 5000;
930 sf->thresh_mult[THR_SPLIT2 ] = 10000;
931 sf->thresh_mult[THR_SPLIT3 ] = 10000;
932 sf->search_method = NSTEP;
936 cpi->mode_check_freq[THR_SPLIT2] = 4;
937 cpi->mode_check_freq[THR_SPLIT3] = 4;
938 cpi->mode_check_freq[THR_SPLIT1 ] = 2;
940 sf->thresh_mult[THR_DC ] = 0;
941 sf->thresh_mult[THR_TM ] = 1000;
942 sf->thresh_mult[THR_V_PRED ] = 2000;
943 sf->thresh_mult[THR_H_PRED ] = 2000;
944 sf->thresh_mult[THR_B_PRED ] = 5000;
948 sf->thresh_mult[THR_NEAREST1 ] = 0;
949 sf->thresh_mult[THR_ZERO1 ] = 0;
950 sf->thresh_mult[THR_NEAR1 ] = 0;
951 sf->thresh_mult[THR_NEW1 ] = 2000;
952 sf->thresh_mult[THR_SPLIT1 ] = 10000;
957 sf->thresh_mult[THR_NEAREST2 ] = 1000;
958 sf->thresh_mult[THR_ZERO2 ] = 1000;
959 sf->thresh_mult[THR_NEAR2 ] = 1000;
960 sf->thresh_mult[THR_NEW2 ] = 2000;
961 sf->thresh_mult[THR_SPLIT2 ] = 20000;
966 sf->thresh_mult[THR_NEAREST3 ] = 1000;
967 sf->thresh_mult[THR_ZERO3 ] = 1000;
968 sf->thresh_mult[THR_NEAR3 ] = 1000;
969 sf->thresh_mult[THR_NEW3 ] = 2000;
970 sf->thresh_mult[THR_SPLIT3 ] = 20000;
973 sf->improved_quant = 0;
974 sf->improved_dct = 0;
976 sf->use_fastquant_for_pick = 1;
977 sf->no_skip_block4x4_search = 0;
983 cpi->mode_check_freq[THR_SPLIT1 ] = 7;
984 cpi->mode_check_freq[THR_SPLIT2] = 15;
985 cpi->mode_check_freq[THR_SPLIT3] = 15;
987 sf->thresh_mult[THR_TM ] = 2000;
988 sf->thresh_mult[THR_V_PRED ] = 2000;
989 sf->thresh_mult[THR_H_PRED ] = 2000;
990 sf->thresh_mult[THR_B_PRED ] = 5000;
994 sf->thresh_mult[THR_NEW1 ] = 2000;
995 sf->thresh_mult[THR_SPLIT1 ] = 25000;
1000 sf->thresh_mult[THR_NEAREST2 ] = 2000;
1001 sf->thresh_mult[THR_ZERO2 ] = 2000;
1002 sf->thresh_mult[THR_NEAR2 ] = 2000;
1003 sf->thresh_mult[THR_NEW2 ] = 2500;
1004 sf->thresh_mult[THR_SPLIT2 ] = 50000;
1009 sf->thresh_mult[THR_NEAREST3 ] = 2000;
1010 sf->thresh_mult[THR_ZERO3 ] = 2000;
1011 sf->thresh_mult[THR_NEAR3 ] = 2000;
1012 sf->thresh_mult[THR_NEW3 ] = 2500;
1013 sf->thresh_mult[THR_SPLIT3 ] = 50000;
1020 sf->auto_filter = 0; // Faster selection of loop filter
1022 cpi->mode_check_freq[THR_V_PRED] = 2;
1023 cpi->mode_check_freq[THR_H_PRED] = 2;
1024 cpi->mode_check_freq[THR_B_PRED] = 2;
1028 cpi->mode_check_freq[THR_NEAR2] = 2;
1029 cpi->mode_check_freq[THR_NEW2] = 4;
1034 cpi->mode_check_freq[THR_NEAR3] = 2;
1035 cpi->mode_check_freq[THR_NEW3] = 4;
1038 sf->thresh_mult[THR_SPLIT1 ] = INT_MAX;
1039 sf->thresh_mult[THR_SPLIT2 ] = INT_MAX;
1040 sf->thresh_mult[THR_SPLIT3 ] = INT_MAX;
1048 sf->auto_filter = 1;
1053 sf->auto_filter = 0; // Faster selection of loop filter
1055 sf->search_method = HEX;
1056 //sf->search_method = DIAMOND;
1058 sf->iterative_sub_pixel = 0;
1060 cpi->mode_check_freq[THR_V_PRED] = 4;
1061 cpi->mode_check_freq[THR_H_PRED] = 4;
1062 cpi->mode_check_freq[THR_B_PRED] = 4;
1064 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
1066 cpi->mode_check_freq[THR_NEAR2] = 2;
1067 cpi->mode_check_freq[THR_NEW2] = 4;
1070 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
1072 cpi->mode_check_freq[THR_NEAR3] = 2;
1073 cpi->mode_check_freq[THR_NEW3] = 4;
1076 sf->thresh_mult[THR_TM ] = 2000;
1077 sf->thresh_mult[THR_B_PRED ] = 5000;
1079 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
1081 sf->thresh_mult[THR_NEAREST2 ] = 2000;
1082 sf->thresh_mult[THR_ZERO2 ] = 2000;
1083 sf->thresh_mult[THR_NEAR2 ] = 2000;
1084 sf->thresh_mult[THR_NEW2 ] = 4000;
1087 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
1089 sf->thresh_mult[THR_NEAREST3 ] = 2000;
1090 sf->thresh_mult[THR_ZERO3 ] = 2000;
1091 sf->thresh_mult[THR_NEAR3 ] = 2000;
1092 sf->thresh_mult[THR_NEW3 ] = 4000;
1098 // Disable split MB intra prediction mode
1099 sf->thresh_mult[THR_B_PRED] = INT_MAX;
1104 unsigned int i, sum = 0;
1105 unsigned int total_mbs = cm->MBs;
1111 if (cpi->oxcf.encode_breakout > 2000)
1112 min = cpi->oxcf.encode_breakout;
1116 for (i = 0; i < min; i++)
1118 sum += cpi->error_bins[i];
1124 // i starts from 2 to make sure thresh started from 2048
1125 for (; i < 1024; i++)
1127 sum += cpi->error_bins[i];
1129 if (10 * sum >= (unsigned int)(cpi->Speed - 6)*(total_mbs - total_skip))
1141 sf->thresh_mult[THR_NEW1 ] = thresh;
1142 sf->thresh_mult[THR_NEAREST1 ] = thresh >> 1;
1143 sf->thresh_mult[THR_NEAR1 ] = thresh >> 1;
1148 sf->thresh_mult[THR_NEW2] = thresh << 1;
1149 sf->thresh_mult[THR_NEAREST2 ] = thresh;
1150 sf->thresh_mult[THR_NEAR2 ] = thresh;
1155 sf->thresh_mult[THR_NEW3] = thresh << 1;
1156 sf->thresh_mult[THR_NEAREST3 ] = thresh;
1157 sf->thresh_mult[THR_NEAR3 ] = thresh;
1160 // Disable other intra prediction modes
1161 sf->thresh_mult[THR_TM] = INT_MAX;
1162 sf->thresh_mult[THR_V_PRED] = INT_MAX;
1163 sf->thresh_mult[THR_H_PRED] = INT_MAX;
1165 sf->improved_mv_pred = 0;
1170 sf->quarter_pixel_search = 0;
1175 int Tmp = cpi->Speed - 8;
1180 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
1182 cpi->mode_check_freq[THR_ZERO2] = 1 << (Tmp - 1);
1183 cpi->mode_check_freq[THR_NEAREST2] = 1 << (Tmp - 1);
1184 cpi->mode_check_freq[THR_NEAR2] = 1 << Tmp;
1185 cpi->mode_check_freq[THR_NEW2] = 1 << (Tmp + 1);
1188 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
1190 cpi->mode_check_freq[THR_ZERO3] = 1 << (Tmp - 1);
1191 cpi->mode_check_freq[THR_NEAREST3] = 1 << (Tmp - 1);
1192 cpi->mode_check_freq[THR_NEAR3] = 1 << Tmp;
1193 cpi->mode_check_freq[THR_NEW3] = 1 << (Tmp + 1);
1196 cpi->mode_check_freq[THR_NEW1 ] = 1 << (Tmp - 1);
1199 cm->filter_type = NORMAL_LOOPFILTER;
1202 cm->filter_type = SIMPLE_LOOPFILTER;
1206 sf->half_pixel_search = 0; // This has a big hit on quality. Last resort
1209 vpx_memset(cpi->error_bins, 0, sizeof(cpi->error_bins));
1213 // Slow quant, dct and trellis not worthwhile for first pass
1214 // so make sure they are always turned off.
1215 if ( cpi->pass == 1 )
1217 sf->improved_quant = 0;
1218 sf->optimize_coefficients = 0;
1219 sf->improved_dct = 0;
1222 if (cpi->sf.search_method == NSTEP)
1224 vp8_init3smotion_compensation(&cpi->mb, cm->yv12_fb[cm->lst_fb_idx].y_stride);
1226 else if (cpi->sf.search_method == DIAMOND)
1228 vp8_init_dsmotion_compensation(&cpi->mb, cm->yv12_fb[cm->lst_fb_idx].y_stride);
1231 if (cpi->sf.improved_dct)
1233 cpi->mb.vp8_short_fdct8x4 = FDCT_INVOKE(&cpi->rtcd.fdct, short8x4);
1234 cpi->mb.vp8_short_fdct4x4 = FDCT_INVOKE(&cpi->rtcd.fdct, short4x4);
1238 cpi->mb.vp8_short_fdct8x4 = FDCT_INVOKE(&cpi->rtcd.fdct, fast8x4);
1239 cpi->mb.vp8_short_fdct4x4 = FDCT_INVOKE(&cpi->rtcd.fdct, fast4x4);
1242 cpi->mb.short_walsh4x4 = FDCT_INVOKE(&cpi->rtcd.fdct, walsh_short4x4);
1244 if (cpi->sf.improved_quant)
1246 cpi->mb.quantize_b = QUANTIZE_INVOKE(&cpi->rtcd.quantize,
1248 cpi->mb.quantize_b_pair = QUANTIZE_INVOKE(&cpi->rtcd.quantize,
1253 cpi->mb.quantize_b = QUANTIZE_INVOKE(&cpi->rtcd.quantize,
1255 cpi->mb.quantize_b_pair = QUANTIZE_INVOKE(&cpi->rtcd.quantize,
1258 if (cpi->sf.improved_quant != last_improved_quant)
1259 vp8cx_init_quantizer(cpi);
1261 #if CONFIG_RUNTIME_CPU_DETECT
1262 cpi->mb.e_mbd.rtcd = &cpi->common.rtcd;
1265 if (cpi->sf.iterative_sub_pixel == 1)
1267 cpi->find_fractional_mv_step = vp8_find_best_sub_pixel_step_iteratively;
1269 else if (cpi->sf.quarter_pixel_search)
1271 cpi->find_fractional_mv_step = vp8_find_best_sub_pixel_step;
1273 else if (cpi->sf.half_pixel_search)
1275 cpi->find_fractional_mv_step = vp8_find_best_half_pixel_step;
1279 cpi->find_fractional_mv_step = vp8_skip_fractional_mv_step;
1282 if (cpi->sf.optimize_coefficients == 1 && cpi->pass!=1)
1283 cpi->mb.optimize = 1;
1285 cpi->mb.optimize = 0;
1287 if (cpi->common.full_pixel)
1288 cpi->find_fractional_mv_step = vp8_skip_fractional_mv_step;
1291 frames_at_speed[cpi->Speed]++;
1294 static void alloc_raw_frame_buffers(VP8_COMP *cpi)
1296 int width = (cpi->oxcf.Width + 15) & ~15;
1297 int height = (cpi->oxcf.Height + 15) & ~15;
1299 cpi->lookahead = vp8_lookahead_init(cpi->oxcf.Width, cpi->oxcf.Height,
1300 cpi->oxcf.lag_in_frames);
1302 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1303 "Failed to allocate lag buffers");
1305 #if VP8_TEMPORAL_ALT_REF
1307 if (vp8_yv12_alloc_frame_buffer(&cpi->alt_ref_buffer,
1308 width, height, VP8BORDERINPIXELS))
1309 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1310 "Failed to allocate altref buffer");
1315 static int vp8_alloc_partition_data(VP8_COMP *cpi)
1317 vpx_free(cpi->mb.pip);
1319 cpi->mb.pip = vpx_calloc((cpi->common.mb_cols + 1) *
1320 (cpi->common.mb_rows + 1),
1321 sizeof(PARTITION_INFO));
1325 cpi->mb.pi = cpi->mb.pip + cpi->common.mode_info_stride + 1;
1330 void vp8_alloc_compressor_data(VP8_COMP *cpi)
1332 VP8_COMMON *cm = & cpi->common;
1334 int width = cm->Width;
1335 int height = cm->Height;
1337 if (vp8_alloc_frame_buffers(cm, width, height))
1338 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1339 "Failed to allocate frame buffers");
1341 if (vp8_alloc_partition_data(cpi))
1342 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1343 "Failed to allocate partition data");
1346 if ((width & 0xf) != 0)
1347 width += 16 - (width & 0xf);
1349 if ((height & 0xf) != 0)
1350 height += 16 - (height & 0xf);
1353 if (vp8_yv12_alloc_frame_buffer(&cpi->pick_lf_lvl_frame,
1354 width, height, VP8BORDERINPIXELS))
1355 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1356 "Failed to allocate last frame buffer");
1358 if (vp8_yv12_alloc_frame_buffer(&cpi->scaled_source,
1359 width, height, VP8BORDERINPIXELS))
1360 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1361 "Failed to allocate scaled source buffer");
1367 unsigned int tokens = cm->mb_rows * cm->mb_cols * 24 * 16;
1369 CHECK_MEM_ERROR(cpi->tok, vpx_calloc(tokens, sizeof(*cpi->tok)));
1372 // Data used for real time vc mode to see if gf needs refreshing
1373 cpi->inter_zz_count = 0;
1374 cpi->gf_bad_count = 0;
1375 cpi->gf_update_recommended = 0;
1378 // Structures used to minitor GF usage
1379 vpx_free(cpi->gf_active_flags);
1380 CHECK_MEM_ERROR(cpi->gf_active_flags,
1381 vpx_calloc(1, cm->mb_rows * cm->mb_cols));
1382 cpi->gf_active_count = cm->mb_rows * cm->mb_cols;
1384 vpx_free(cpi->mb_activity_map);
1385 CHECK_MEM_ERROR(cpi->mb_activity_map,
1386 vpx_calloc(sizeof(unsigned int),
1387 cm->mb_rows * cm->mb_cols));
1389 vpx_free(cpi->mb_norm_activity_map);
1390 CHECK_MEM_ERROR(cpi->mb_norm_activity_map,
1391 vpx_calloc(sizeof(unsigned int),
1392 cm->mb_rows * cm->mb_cols));
1394 #if !(CONFIG_REALTIME_ONLY)
1395 vpx_free(cpi->twopass.total_stats);
1397 cpi->twopass.total_stats = vpx_calloc(1, sizeof(FIRSTPASS_STATS));
1399 vpx_free(cpi->twopass.total_left_stats);
1400 cpi->twopass.total_left_stats = vpx_calloc(1, sizeof(FIRSTPASS_STATS));
1402 vpx_free(cpi->twopass.this_frame_stats);
1404 cpi->twopass.this_frame_stats = vpx_calloc(1, sizeof(FIRSTPASS_STATS));
1406 if( !cpi->twopass.total_stats ||
1407 !cpi->twopass.total_left_stats ||
1408 !cpi->twopass.this_frame_stats)
1409 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1410 "Failed to allocate firstpass stats");
1413 #if CONFIG_MULTITHREAD
1415 cpi->mt_sync_range = 1;
1416 else if (width <= 1280)
1417 cpi->mt_sync_range = 4;
1418 else if (width <= 2560)
1419 cpi->mt_sync_range = 8;
1421 cpi->mt_sync_range = 16;
1424 vpx_free(cpi->tplist);
1426 CHECK_MEM_ERROR(cpi->tplist, vpx_malloc(sizeof(TOKENLIST) * cpi->common.mb_rows));
1431 static const int q_trans[] =
1433 0, 1, 2, 3, 4, 5, 7, 8,
1434 9, 10, 12, 13, 15, 17, 18, 19,
1435 20, 21, 23, 24, 25, 26, 27, 28,
1436 29, 30, 31, 33, 35, 37, 39, 41,
1437 43, 45, 47, 49, 51, 53, 55, 57,
1438 59, 61, 64, 67, 70, 73, 76, 79,
1439 82, 85, 88, 91, 94, 97, 100, 103,
1440 106, 109, 112, 115, 118, 121, 124, 127,
1443 int vp8_reverse_trans(int x)
1447 for (i = 0; i < 64; i++)
1448 if (q_trans[i] >= x)
1453 void vp8_new_frame_rate(VP8_COMP *cpi, double framerate)
1458 cpi->frame_rate = framerate;
1459 cpi->output_frame_rate = framerate;
1460 cpi->per_frame_bandwidth = (int)(cpi->oxcf.target_bandwidth /
1461 cpi->output_frame_rate);
1462 cpi->av_per_frame_bandwidth = cpi->per_frame_bandwidth;
1463 cpi->min_frame_bandwidth = (int)(cpi->av_per_frame_bandwidth *
1464 cpi->oxcf.two_pass_vbrmin_section / 100);
1466 // Set Maximum gf/arf interval
1467 cpi->max_gf_interval = ((int)(cpi->output_frame_rate / 2.0) + 2);
1469 if(cpi->max_gf_interval < 12)
1470 cpi->max_gf_interval = 12;
1472 // Extended interval for genuinely static scenes
1473 cpi->twopass.static_scene_max_gf_interval = cpi->key_frame_frequency >> 1;
1475 // Special conditions when altr ref frame enabled in lagged compress mode
1476 if (cpi->oxcf.play_alternate && cpi->oxcf.lag_in_frames)
1478 if (cpi->max_gf_interval > cpi->oxcf.lag_in_frames - 1)
1479 cpi->max_gf_interval = cpi->oxcf.lag_in_frames - 1;
1481 if (cpi->twopass.static_scene_max_gf_interval > cpi->oxcf.lag_in_frames - 1)
1482 cpi->twopass.static_scene_max_gf_interval = cpi->oxcf.lag_in_frames - 1;
1485 if ( cpi->max_gf_interval > cpi->twopass.static_scene_max_gf_interval )
1486 cpi->max_gf_interval = cpi->twopass.static_scene_max_gf_interval;
1491 rescale(int val, int num, int denom)
1493 int64_t llnum = num;
1494 int64_t llden = denom;
1495 int64_t llval = val;
1497 return llval * llnum / llden;
1501 static void init_config(VP8_PTR ptr, VP8_CONFIG *oxcf)
1503 VP8_COMP *cpi = (VP8_COMP *)(ptr);
1504 VP8_COMMON *cm = &cpi->common;
1509 cpi->auto_adjust_gold_quantizer = 1;
1512 cm->version = oxcf->Version;
1513 vp8_setup_version(cm);
1515 /* frame rate is not available on the first frame, as it's derived from
1516 * the observed timestamps. The actual value used here doesn't matter
1517 * too much, as it will adapt quickly. If the reciprocal of the timebase
1518 * seems like a reasonable framerate, then use that as a guess, otherwise
1521 cpi->frame_rate = (double)(oxcf->timebase.den) /
1522 (double)(oxcf->timebase.num);
1524 if (cpi->frame_rate > 180)
1525 cpi->frame_rate = 30;
1527 // change includes all joint functionality
1528 vp8_change_config(ptr, oxcf);
1530 // Initialize active best and worst q and average q values.
1531 cpi->active_worst_quality = cpi->oxcf.worst_allowed_q;
1532 cpi->active_best_quality = cpi->oxcf.best_allowed_q;
1533 cpi->avg_frame_qindex = cpi->oxcf.worst_allowed_q;
1535 // Initialise the starting buffer levels
1536 cpi->buffer_level = cpi->oxcf.starting_buffer_level;
1537 cpi->bits_off_target = cpi->oxcf.starting_buffer_level;
1539 cpi->rolling_target_bits = cpi->av_per_frame_bandwidth;
1540 cpi->rolling_actual_bits = cpi->av_per_frame_bandwidth;
1541 cpi->long_rolling_target_bits = cpi->av_per_frame_bandwidth;
1542 cpi->long_rolling_actual_bits = cpi->av_per_frame_bandwidth;
1544 cpi->total_actual_bits = 0;
1545 cpi->total_target_vs_actual = 0;
1547 // Temporal scalabilty
1548 if (cpi->oxcf.number_of_layers > 1)
1551 int prev_layer_frame_rate=0;
1553 for (i=0; i<cpi->oxcf.number_of_layers; i++)
1555 LAYER_CONTEXT *lc = &cpi->layer_context[i];
1557 // Layer configuration
1559 cpi->output_frame_rate / cpi->oxcf.rate_decimator[i];
1560 lc->target_bandwidth = cpi->oxcf.target_bitrate[i] * 1000;
1562 lc->starting_buffer_level =
1563 rescale(oxcf->starting_buffer_level,
1564 lc->target_bandwidth, 1000);
1566 if (oxcf->optimal_buffer_level == 0)
1567 lc->optimal_buffer_level = lc->target_bandwidth / 8;
1569 lc->optimal_buffer_level =
1570 rescale(oxcf->optimal_buffer_level,
1571 lc->target_bandwidth, 1000);
1573 if (oxcf->maximum_buffer_size == 0)
1574 lc->maximum_buffer_size = lc->target_bandwidth / 8;
1576 lc->maximum_buffer_size =
1577 rescale(oxcf->maximum_buffer_size,
1578 lc->target_bandwidth, 1000);
1580 // Work out the average size of a frame within this layer
1582 lc->avg_frame_size_for_layer = (cpi->oxcf.target_bitrate[i] -
1583 cpi->oxcf.target_bitrate[i-1]) * 1000 /
1584 (lc->frame_rate - prev_layer_frame_rate);
1586 lc->active_worst_quality = cpi->oxcf.worst_allowed_q;
1587 lc->active_best_quality = cpi->oxcf.best_allowed_q;
1588 lc->avg_frame_qindex = cpi->oxcf.worst_allowed_q;
1590 lc->buffer_level = lc->starting_buffer_level;
1591 lc->bits_off_target = lc->starting_buffer_level;
1593 lc->total_actual_bits = 0;
1597 lc->rate_correction_factor = 1.0;
1598 lc->key_frame_rate_correction_factor = 1.0;
1599 lc->gf_rate_correction_factor = 1.0;
1600 lc->inter_frame_target = 0.0;
1602 prev_layer_frame_rate = lc->frame_rate;
1606 #if VP8_TEMPORAL_ALT_REF
1610 cpi->fixed_divide[0] = 0;
1612 for (i = 1; i < 512; i++)
1613 cpi->fixed_divide[i] = 0x80000 / i;
1619 void vp8_change_config(VP8_PTR ptr, VP8_CONFIG *oxcf)
1621 VP8_COMP *cpi = (VP8_COMP *)(ptr);
1622 VP8_COMMON *cm = &cpi->common;
1630 if (cm->version != oxcf->Version)
1632 cm->version = oxcf->Version;
1633 vp8_setup_version(cm);
1638 switch (cpi->oxcf.Mode)
1643 cpi->compressor_speed = 2;
1645 if (cpi->oxcf.cpu_used < -16)
1647 cpi->oxcf.cpu_used = -16;
1650 if (cpi->oxcf.cpu_used > 16)
1651 cpi->oxcf.cpu_used = 16;
1655 case MODE_GOODQUALITY:
1657 cpi->compressor_speed = 1;
1659 if (cpi->oxcf.cpu_used < -5)
1661 cpi->oxcf.cpu_used = -5;
1664 if (cpi->oxcf.cpu_used > 5)
1665 cpi->oxcf.cpu_used = 5;
1669 case MODE_BESTQUALITY:
1671 cpi->compressor_speed = 0;
1674 case MODE_FIRSTPASS:
1676 cpi->compressor_speed = 1;
1678 case MODE_SECONDPASS:
1680 cpi->compressor_speed = 1;
1682 if (cpi->oxcf.cpu_used < -5)
1684 cpi->oxcf.cpu_used = -5;
1687 if (cpi->oxcf.cpu_used > 5)
1688 cpi->oxcf.cpu_used = 5;
1691 case MODE_SECONDPASS_BEST:
1693 cpi->compressor_speed = 0;
1698 cpi->auto_worst_q = 1;
1700 cpi->oxcf.worst_allowed_q = q_trans[oxcf->worst_allowed_q];
1701 cpi->oxcf.best_allowed_q = q_trans[oxcf->best_allowed_q];
1702 cpi->oxcf.cq_level = q_trans[cpi->oxcf.cq_level];
1704 if (oxcf->fixed_q >= 0)
1706 if (oxcf->worst_allowed_q < 0)
1707 cpi->oxcf.fixed_q = q_trans[0];
1709 cpi->oxcf.fixed_q = q_trans[oxcf->worst_allowed_q];
1711 if (oxcf->alt_q < 0)
1712 cpi->oxcf.alt_q = q_trans[0];
1714 cpi->oxcf.alt_q = q_trans[oxcf->alt_q];
1716 if (oxcf->key_q < 0)
1717 cpi->oxcf.key_q = q_trans[0];
1719 cpi->oxcf.key_q = q_trans[oxcf->key_q];
1721 if (oxcf->gold_q < 0)
1722 cpi->oxcf.gold_q = q_trans[0];
1724 cpi->oxcf.gold_q = q_trans[oxcf->gold_q];
1728 cpi->baseline_gf_interval =
1729 cpi->oxcf.alt_freq ? cpi->oxcf.alt_freq : DEFAULT_GF_INTERVAL;
1731 cpi->ref_frame_flags = VP8_ALT_FLAG | VP8_GOLD_FLAG | VP8_LAST_FLAG;
1733 //cpi->use_golden_frame_only = 0;
1734 //cpi->use_last_frame_only = 0;
1735 cm->refresh_golden_frame = 0;
1736 cm->refresh_last_frame = 1;
1737 cm->refresh_entropy_probs = 1;
1739 if (cpi->oxcf.token_partitions >= 0 && cpi->oxcf.token_partitions <= 3)
1740 cm->multi_token_partition =
1741 (TOKEN_PARTITION) cpi->oxcf.token_partitions;
1743 setup_features(cpi);
1748 for (i = 0; i < MAX_MB_SEGMENTS; i++)
1749 cpi->segment_encode_breakout[i] = cpi->oxcf.encode_breakout;
1752 // At the moment the first order values may not be > MAXQ
1753 if (cpi->oxcf.fixed_q > MAXQ)
1754 cpi->oxcf.fixed_q = MAXQ;
1756 // local file playback mode == really big buffer
1757 if (cpi->oxcf.end_usage == USAGE_LOCAL_FILE_PLAYBACK)
1759 cpi->oxcf.starting_buffer_level = 60000;
1760 cpi->oxcf.optimal_buffer_level = 60000;
1761 cpi->oxcf.maximum_buffer_size = 240000;
1764 // Convert target bandwidth from Kbit/s to Bit/s
1765 cpi->oxcf.target_bandwidth *= 1000;
1767 cpi->oxcf.starting_buffer_level =
1768 rescale(cpi->oxcf.starting_buffer_level,
1769 cpi->oxcf.target_bandwidth, 1000);
1771 // Set or reset optimal and maximum buffer levels.
1772 if (cpi->oxcf.optimal_buffer_level == 0)
1773 cpi->oxcf.optimal_buffer_level = cpi->oxcf.target_bandwidth / 8;
1775 cpi->oxcf.optimal_buffer_level =
1776 rescale(cpi->oxcf.optimal_buffer_level,
1777 cpi->oxcf.target_bandwidth, 1000);
1779 if (cpi->oxcf.maximum_buffer_size == 0)
1780 cpi->oxcf.maximum_buffer_size = cpi->oxcf.target_bandwidth / 8;
1782 cpi->oxcf.maximum_buffer_size =
1783 rescale(cpi->oxcf.maximum_buffer_size,
1784 cpi->oxcf.target_bandwidth, 1000);
1786 // Set up frame rate and related parameters rate control values.
1787 vp8_new_frame_rate(cpi, cpi->frame_rate);
1789 // Set absolute upper and lower quality limits
1790 cpi->worst_quality = cpi->oxcf.worst_allowed_q;
1791 cpi->best_quality = cpi->oxcf.best_allowed_q;
1793 // active values should only be modified if out of new range
1794 if (cpi->active_worst_quality > cpi->oxcf.worst_allowed_q)
1796 cpi->active_worst_quality = cpi->oxcf.worst_allowed_q;
1799 else if (cpi->active_worst_quality < cpi->oxcf.best_allowed_q)
1801 cpi->active_worst_quality = cpi->oxcf.best_allowed_q;
1803 if (cpi->active_best_quality < cpi->oxcf.best_allowed_q)
1805 cpi->active_best_quality = cpi->oxcf.best_allowed_q;
1808 else if (cpi->active_best_quality > cpi->oxcf.worst_allowed_q)
1810 cpi->active_best_quality = cpi->oxcf.worst_allowed_q;
1813 cpi->buffered_mode = (cpi->oxcf.optimal_buffer_level > 0) ? TRUE : FALSE;
1815 cpi->cq_target_quality = cpi->oxcf.cq_level;
1817 // Only allow dropped frames in buffered mode
1818 cpi->drop_frames_allowed = cpi->oxcf.allow_df && cpi->buffered_mode;
1820 if (!cm->use_bilinear_mc_filter)
1821 cm->mcomp_filter_type = SIXTAP;
1823 cm->mcomp_filter_type = BILINEAR;
1825 cpi->target_bandwidth = cpi->oxcf.target_bandwidth;
1827 cm->Width = cpi->oxcf.Width;
1828 cm->Height = cpi->oxcf.Height;
1830 cm->horiz_scale = cpi->horiz_scale;
1831 cm->vert_scale = cpi->vert_scale;
1833 // VP8 sharpness level mapping 0-7 (vs 0-10 in general VPx dialogs)
1834 if (cpi->oxcf.Sharpness > 7)
1835 cpi->oxcf.Sharpness = 7;
1837 cm->sharpness_level = cpi->oxcf.Sharpness;
1839 if (cm->horiz_scale != NORMAL || cm->vert_scale != NORMAL)
1841 int UNINITIALIZED_IS_SAFE(hr), UNINITIALIZED_IS_SAFE(hs);
1842 int UNINITIALIZED_IS_SAFE(vr), UNINITIALIZED_IS_SAFE(vs);
1844 Scale2Ratio(cm->horiz_scale, &hr, &hs);
1845 Scale2Ratio(cm->vert_scale, &vr, &vs);
1847 // always go to the next whole number
1848 cm->Width = (hs - 1 + cpi->oxcf.Width * hr) / hs;
1849 cm->Height = (vs - 1 + cpi->oxcf.Height * vr) / vs;
1852 if (((cm->Width + 15) & 0xfffffff0) !=
1853 cm->yv12_fb[cm->lst_fb_idx].y_width ||
1854 ((cm->Height + 15) & 0xfffffff0) !=
1855 cm->yv12_fb[cm->lst_fb_idx].y_height ||
1856 cm->yv12_fb[cm->lst_fb_idx].y_width == 0)
1858 alloc_raw_frame_buffers(cpi);
1859 vp8_alloc_compressor_data(cpi);
1862 if (cpi->oxcf.fixed_q >= 0)
1864 cpi->last_q[0] = cpi->oxcf.fixed_q;
1865 cpi->last_q[1] = cpi->oxcf.fixed_q;
1868 cpi->Speed = cpi->oxcf.cpu_used;
1870 // force to allowlag to 0 if lag_in_frames is 0;
1871 if (cpi->oxcf.lag_in_frames == 0)
1873 cpi->oxcf.allow_lag = 0;
1875 // Limit on lag buffers as these are not currently dynamically allocated
1876 else if (cpi->oxcf.lag_in_frames > MAX_LAG_BUFFERS)
1877 cpi->oxcf.lag_in_frames = MAX_LAG_BUFFERS;
1880 cpi->alt_ref_source = NULL;
1881 cpi->is_src_frame_alt_ref = 0;
1885 // Experimental RD Code
1886 cpi->frame_distortion = 0;
1887 cpi->last_frame_distortion = 0;
1892 #define M_LOG2_E 0.693147180559945309417
1893 #define log2f(x) (log (x) / (float) M_LOG2_E)
1894 static void cal_mvsadcosts(int *mvsadcost[2])
1898 mvsadcost [0] [0] = 300;
1899 mvsadcost [1] [0] = 300;
1903 double z = 256 * (2 * (log2f(8 * i) + .6));
1904 mvsadcost [0][i] = (int) z;
1905 mvsadcost [1][i] = (int) z;
1906 mvsadcost [0][-i] = (int) z;
1907 mvsadcost [1][-i] = (int) z;
1909 while (++i <= mvfp_max);
1912 VP8_PTR vp8_create_compressor(VP8_CONFIG *oxcf)
1924 cpi = ctx.cpi = vpx_memalign(32, sizeof(VP8_COMP));
1925 // Check that the CPI instance is valid
1931 vpx_memset(cpi, 0, sizeof(VP8_COMP));
1933 if (setjmp(cm->error.jmp))
1935 VP8_PTR ptr = ctx.ptr;
1937 ctx.cpi->common.error.setjmp = 0;
1938 vp8_remove_compressor(&ptr);
1942 cpi->common.error.setjmp = 1;
1944 CHECK_MEM_ERROR(cpi->mb.ss, vpx_calloc(sizeof(search_site), (MAX_MVSEARCH_STEPS * 8) + 1));
1946 vp8_create_common(&cpi->common);
1947 vp8_cmachine_specific_config(cpi);
1949 init_config((VP8_PTR)cpi, oxcf);
1951 memcpy(cpi->base_skip_false_prob, vp8cx_base_skip_false_prob, sizeof(vp8cx_base_skip_false_prob));
1952 cpi->common.current_video_frame = 0;
1953 cpi->kf_overspend_bits = 0;
1954 cpi->kf_bitrate_adjustment = 0;
1955 cpi->frames_till_gf_update_due = 0;
1956 cpi->gf_overspend_bits = 0;
1957 cpi->non_gf_bitrate_adjustment = 0;
1958 cpi->prob_last_coded = 128;
1959 cpi->prob_gf_coded = 128;
1960 cpi->prob_intra_coded = 63;
1962 // Prime the recent reference frame usage counters.
1963 // Hereafter they will be maintained as a sort of moving average
1964 cpi->recent_ref_frame_usage[INTRA_FRAME] = 1;
1965 cpi->recent_ref_frame_usage[LAST_FRAME] = 1;
1966 cpi->recent_ref_frame_usage[GOLDEN_FRAME] = 1;
1967 cpi->recent_ref_frame_usage[ALTREF_FRAME] = 1;
1969 // Set reference frame sign bias for ALTREF frame to 1 (for now)
1970 cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = 1;
1972 cpi->twopass.gf_decay_rate = 0;
1973 cpi->baseline_gf_interval = DEFAULT_GF_INTERVAL;
1975 cpi->gold_is_last = 0 ;
1976 cpi->alt_is_last = 0 ;
1977 cpi->gold_is_alt = 0 ;
1979 // allocate memory for storing last frame's MVs for MV prediction.
1980 CHECK_MEM_ERROR(cpi->lfmv, vpx_calloc((cpi->common.mb_rows+2) * (cpi->common.mb_cols+2), sizeof(int_mv)));
1981 CHECK_MEM_ERROR(cpi->lf_ref_frame_sign_bias, vpx_calloc((cpi->common.mb_rows+2) * (cpi->common.mb_cols+2), sizeof(int)));
1982 CHECK_MEM_ERROR(cpi->lf_ref_frame, vpx_calloc((cpi->common.mb_rows+2) * (cpi->common.mb_cols+2), sizeof(int)));
1984 // Create the encoder segmentation map and set all entries to 0
1985 CHECK_MEM_ERROR(cpi->segmentation_map, vpx_calloc(cpi->common.mb_rows * cpi->common.mb_cols, 1));
1986 CHECK_MEM_ERROR(cpi->active_map, vpx_calloc(cpi->common.mb_rows * cpi->common.mb_cols, 1));
1987 vpx_memset(cpi->active_map , 1, (cpi->common.mb_rows * cpi->common.mb_cols));
1988 cpi->active_map_enabled = 0;
1991 // Experimental code for lagged and one pass
1992 // Initialise one_pass GF frames stats
1993 // Update stats used for GF selection
1996 cpi->one_pass_frame_index = 0;
1998 for (i = 0; i < MAX_LAG_BUFFERS; i++)
2000 cpi->one_pass_frame_stats[i].frames_so_far = 0;
2001 cpi->one_pass_frame_stats[i].frame_intra_error = 0.0;
2002 cpi->one_pass_frame_stats[i].frame_coded_error = 0.0;
2003 cpi->one_pass_frame_stats[i].frame_pcnt_inter = 0.0;
2004 cpi->one_pass_frame_stats[i].frame_pcnt_motion = 0.0;
2005 cpi->one_pass_frame_stats[i].frame_mvr = 0.0;
2006 cpi->one_pass_frame_stats[i].frame_mvr_abs = 0.0;
2007 cpi->one_pass_frame_stats[i].frame_mvc = 0.0;
2008 cpi->one_pass_frame_stats[i].frame_mvc_abs = 0.0;
2013 // Should we use the cyclic refresh method.
2014 // Currently this is tied to error resilliant mode
2015 cpi->cyclic_refresh_mode_enabled = cpi->oxcf.error_resilient_mode;
2016 cpi->cyclic_refresh_mode_max_mbs_perframe = (cpi->common.mb_rows * cpi->common.mb_cols) / 40;
2017 cpi->cyclic_refresh_mode_index = 0;
2018 cpi->cyclic_refresh_q = 32;
2020 if (cpi->cyclic_refresh_mode_enabled)
2022 CHECK_MEM_ERROR(cpi->cyclic_refresh_map, vpx_calloc((cpi->common.mb_rows * cpi->common.mb_cols), 1));
2025 cpi->cyclic_refresh_map = (signed char *) NULL;
2027 // Test function for segmentation
2028 //segmentation_test_function((VP8_PTR) cpi);
2030 #ifdef ENTROPY_STATS
2031 init_context_counters();
2034 /*Initialize the feed-forward activity masking.*/
2035 cpi->activity_avg = 90<<12;
2037 cpi->frames_since_key = 8; // Give a sensible default for the first frame.
2038 cpi->key_frame_frequency = cpi->oxcf.key_freq;
2039 cpi->this_key_frame_forced = FALSE;
2040 cpi->next_key_frame_forced = FALSE;
2042 cpi->source_alt_ref_pending = FALSE;
2043 cpi->source_alt_ref_active = FALSE;
2044 cpi->common.refresh_alt_ref_frame = 0;
2046 cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
2047 #if CONFIG_INTERNAL_STATS
2048 cpi->b_calculate_ssimg = 0;
2053 if (cpi->b_calculate_psnr)
2055 cpi->total_sq_error = 0.0;
2056 cpi->total_sq_error2 = 0.0;
2061 cpi->totalp_y = 0.0;
2062 cpi->totalp_u = 0.0;
2063 cpi->totalp_v = 0.0;
2065 cpi->tot_recode_hits = 0;
2066 cpi->summed_quality = 0;
2067 cpi->summed_weights = 0;
2070 if (cpi->b_calculate_ssimg)
2072 cpi->total_ssimg_y = 0;
2073 cpi->total_ssimg_u = 0;
2074 cpi->total_ssimg_v = 0;
2075 cpi->total_ssimg_all = 0;
2081 #define LLONG_MAX 9223372036854775807LL
2083 cpi->first_time_stamp_ever = LLONG_MAX;
2085 cpi->frames_till_gf_update_due = 0;
2086 cpi->key_frame_count = 1;
2088 cpi->ni_av_qi = cpi->oxcf.worst_allowed_q;
2091 cpi->total_byte_count = 0;
2093 cpi->drop_frame = 0;
2094 cpi->drop_count = 0;
2095 cpi->max_drop_count = 0;
2096 cpi->max_consec_dropped_frames = 4;
2098 cpi->rate_correction_factor = 1.0;
2099 cpi->key_frame_rate_correction_factor = 1.0;
2100 cpi->gf_rate_correction_factor = 1.0;
2101 cpi->twopass.est_max_qcorrection_factor = 1.0;
2103 cpi->mb.mvcost[0] = &cpi->mb.mvcosts[0][mv_max+1];
2104 cpi->mb.mvcost[1] = &cpi->mb.mvcosts[1][mv_max+1];
2105 cpi->mb.mvsadcost[0] = &cpi->mb.mvsadcosts[0][mvfp_max+1];
2106 cpi->mb.mvsadcost[1] = &cpi->mb.mvsadcosts[1][mvfp_max+1];
2108 cal_mvsadcosts(cpi->mb.mvsadcost);
2110 for (i = 0; i < KEY_FRAME_CONTEXT; i++)
2112 cpi->prior_key_frame_distance[i] = (int)cpi->output_frame_rate;
2115 #ifdef OUTPUT_YUV_SRC
2116 yuv_file = fopen("bd.yuv", "ab");
2120 framepsnr = fopen("framepsnr.stt", "a");
2121 kf_list = fopen("kf_list.stt", "w");
2124 cpi->output_pkt_list = oxcf->output_pkt_list;
2126 #if !(CONFIG_REALTIME_ONLY)
2130 vp8_init_first_pass(cpi);
2132 else if (cpi->pass == 2)
2134 size_t packet_sz = sizeof(FIRSTPASS_STATS);
2135 int packets = oxcf->two_pass_stats_in.sz / packet_sz;
2137 cpi->twopass.stats_in_start = oxcf->two_pass_stats_in.buf;
2138 cpi->twopass.stats_in = cpi->twopass.stats_in_start;
2139 cpi->twopass.stats_in_end = (void*)((char *)cpi->twopass.stats_in
2140 + (packets - 1) * packet_sz);
2141 vp8_init_second_pass(cpi);
2146 if (cpi->compressor_speed == 2)
2148 cpi->cpu_freq = 0; //vp8_get_processor_freq();
2149 cpi->avg_encode_time = 0;
2150 cpi->avg_pick_mode_time = 0;
2153 vp8_set_speed_features(cpi);
2155 // Set starting values of RD threshold multipliers (128 = *1)
2156 for (i = 0; i < MAX_MODES; i++)
2158 cpi->rd_thresh_mult[i] = 128;
2161 #ifdef ENTROPY_STATS
2162 init_mv_ref_counts();
2165 #if CONFIG_MULTITHREAD
2166 vp8cx_create_encoder_threads(cpi);
2169 cpi->fn_ptr[BLOCK_16X16].sdf = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x16);
2170 cpi->fn_ptr[BLOCK_16X16].vf = VARIANCE_INVOKE(&cpi->rtcd.variance, var16x16);
2171 cpi->fn_ptr[BLOCK_16X16].svf = VARIANCE_INVOKE(&cpi->rtcd.variance, subpixvar16x16);
2172 cpi->fn_ptr[BLOCK_16X16].svf_halfpix_h = VARIANCE_INVOKE(&cpi->rtcd.variance, halfpixvar16x16_h);
2173 cpi->fn_ptr[BLOCK_16X16].svf_halfpix_v = VARIANCE_INVOKE(&cpi->rtcd.variance, halfpixvar16x16_v);
2174 cpi->fn_ptr[BLOCK_16X16].svf_halfpix_hv = VARIANCE_INVOKE(&cpi->rtcd.variance, halfpixvar16x16_hv);
2175 cpi->fn_ptr[BLOCK_16X16].sdx3f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x16x3);
2176 cpi->fn_ptr[BLOCK_16X16].sdx8f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x16x8);
2177 cpi->fn_ptr[BLOCK_16X16].sdx4df = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x16x4d);
2179 cpi->fn_ptr[BLOCK_16X8].sdf = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x8);
2180 cpi->fn_ptr[BLOCK_16X8].vf = VARIANCE_INVOKE(&cpi->rtcd.variance, var16x8);
2181 cpi->fn_ptr[BLOCK_16X8].svf = VARIANCE_INVOKE(&cpi->rtcd.variance, subpixvar16x8);
2182 cpi->fn_ptr[BLOCK_16X8].svf_halfpix_h = NULL;
2183 cpi->fn_ptr[BLOCK_16X8].svf_halfpix_v = NULL;
2184 cpi->fn_ptr[BLOCK_16X8].svf_halfpix_hv = NULL;
2185 cpi->fn_ptr[BLOCK_16X8].sdx3f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x8x3);
2186 cpi->fn_ptr[BLOCK_16X8].sdx8f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x8x8);
2187 cpi->fn_ptr[BLOCK_16X8].sdx4df = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x8x4d);
2189 cpi->fn_ptr[BLOCK_8X16].sdf = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x16);
2190 cpi->fn_ptr[BLOCK_8X16].vf = VARIANCE_INVOKE(&cpi->rtcd.variance, var8x16);
2191 cpi->fn_ptr[BLOCK_8X16].svf = VARIANCE_INVOKE(&cpi->rtcd.variance, subpixvar8x16);
2192 cpi->fn_ptr[BLOCK_8X16].svf_halfpix_h = NULL;
2193 cpi->fn_ptr[BLOCK_8X16].svf_halfpix_v = NULL;
2194 cpi->fn_ptr[BLOCK_8X16].svf_halfpix_hv = NULL;
2195 cpi->fn_ptr[BLOCK_8X16].sdx3f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x16x3);
2196 cpi->fn_ptr[BLOCK_8X16].sdx8f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x16x8);
2197 cpi->fn_ptr[BLOCK_8X16].sdx4df = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x16x4d);
2199 cpi->fn_ptr[BLOCK_8X8].sdf = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x8);
2200 cpi->fn_ptr[BLOCK_8X8].vf = VARIANCE_INVOKE(&cpi->rtcd.variance, var8x8);
2201 cpi->fn_ptr[BLOCK_8X8].svf = VARIANCE_INVOKE(&cpi->rtcd.variance, subpixvar8x8);
2202 cpi->fn_ptr[BLOCK_8X8].svf_halfpix_h = NULL;
2203 cpi->fn_ptr[BLOCK_8X8].svf_halfpix_v = NULL;
2204 cpi->fn_ptr[BLOCK_8X8].svf_halfpix_hv = NULL;
2205 cpi->fn_ptr[BLOCK_8X8].sdx3f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x8x3);
2206 cpi->fn_ptr[BLOCK_8X8].sdx8f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x8x8);
2207 cpi->fn_ptr[BLOCK_8X8].sdx4df = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x8x4d);
2209 cpi->fn_ptr[BLOCK_4X4].sdf = VARIANCE_INVOKE(&cpi->rtcd.variance, sad4x4);
2210 cpi->fn_ptr[BLOCK_4X4].vf = VARIANCE_INVOKE(&cpi->rtcd.variance, var4x4);
2211 cpi->fn_ptr[BLOCK_4X4].svf = VARIANCE_INVOKE(&cpi->rtcd.variance, subpixvar4x4);
2212 cpi->fn_ptr[BLOCK_4X4].svf_halfpix_h = NULL;
2213 cpi->fn_ptr[BLOCK_4X4].svf_halfpix_v = NULL;
2214 cpi->fn_ptr[BLOCK_4X4].svf_halfpix_hv = NULL;
2215 cpi->fn_ptr[BLOCK_4X4].sdx3f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad4x4x3);
2216 cpi->fn_ptr[BLOCK_4X4].sdx8f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad4x4x8);
2217 cpi->fn_ptr[BLOCK_4X4].sdx4df = VARIANCE_INVOKE(&cpi->rtcd.variance, sad4x4x4d);
2219 #if ARCH_X86 || ARCH_X86_64
2220 cpi->fn_ptr[BLOCK_16X16].copymem = VARIANCE_INVOKE(&cpi->rtcd.variance, copy32xn);
2221 cpi->fn_ptr[BLOCK_16X8].copymem = VARIANCE_INVOKE(&cpi->rtcd.variance, copy32xn);
2222 cpi->fn_ptr[BLOCK_8X16].copymem = VARIANCE_INVOKE(&cpi->rtcd.variance, copy32xn);
2223 cpi->fn_ptr[BLOCK_8X8].copymem = VARIANCE_INVOKE(&cpi->rtcd.variance, copy32xn);
2224 cpi->fn_ptr[BLOCK_4X4].copymem = VARIANCE_INVOKE(&cpi->rtcd.variance, copy32xn);
2227 cpi->full_search_sad = SEARCH_INVOKE(&cpi->rtcd.search, full_search);
2228 cpi->diamond_search_sad = SEARCH_INVOKE(&cpi->rtcd.search, diamond_search);
2229 cpi->refining_search_sad = SEARCH_INVOKE(&cpi->rtcd.search, refining_search);
2231 // make sure frame 1 is okay
2232 cpi->error_bins[0] = cpi->common.MBs;
2234 //vp8cx_init_quantizer() is first called here. Add check in vp8cx_frame_init_quantizer() so that vp8cx_init_quantizer is only called later
2235 //when needed. This will avoid unnecessary calls of vp8cx_init_quantizer() for every frame.
2236 vp8cx_init_quantizer(cpi);
2238 vp8_loop_filter_init(cm);
2240 cpi->common.error.setjmp = 0;
2242 #if CONFIG_MULTI_RES_ENCODING
2243 /* Calculate # of MBs in a row in lower-resolution level image. */
2244 if (cpi->oxcf.mr_encoder_id > 0)
2245 vp8_cal_low_res_mb_cols(cpi);
2248 return (VP8_PTR) cpi;
2253 void vp8_remove_compressor(VP8_PTR *ptr)
2255 VP8_COMP *cpi = (VP8_COMP *)(*ptr);
2260 if (cpi && (cpi->common.current_video_frame > 0))
2262 #if !(CONFIG_REALTIME_ONLY)
2266 vp8_end_second_pass(cpi);
2271 #ifdef ENTROPY_STATS
2272 print_context_counters();
2273 print_tree_update_probs();
2274 print_mode_context();
2277 #if CONFIG_INTERNAL_STATS
2281 FILE *f = fopen("opsnr.stt", "a");
2282 double time_encoded = (cpi->last_end_time_stamp_seen
2283 - cpi->first_time_stamp_ever) / 10000000.000;
2284 double total_encode_time = (cpi->time_receive_data +
2285 cpi->time_compress_data) / 1000.000;
2286 double dr = (double)cpi->bytes * 8.0 / 1000.0 / time_encoded;
2288 if (cpi->b_calculate_psnr)
2290 YV12_BUFFER_CONFIG *lst_yv12 =
2291 &cpi->common.yv12_fb[cpi->common.lst_fb_idx];
2293 if (cpi->oxcf.number_of_layers > 1)
2297 fprintf(f, "Layer\tBitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\t"
2298 "GLPsnrP\tVPXSSIM\t\n");
2299 for (i=0; i<cpi->oxcf.number_of_layers; i++)
2301 double dr = (double)cpi->bytes_in_layer[i] *
2302 8.0 / 1000.0 / time_encoded;
2303 double samples = 3.0 / 2 * cpi->frames_in_layer[i] *
2304 lst_yv12->y_width * lst_yv12->y_height;
2305 double total_psnr = vp8_mse2psnr(samples, 255.0,
2306 cpi->total_error2[i]);
2307 double total_psnr2 = vp8_mse2psnr(samples, 255.0,
2308 cpi->total_error2_p[i]);
2309 double total_ssim = 100 * pow(cpi->sum_ssim[i] /
2310 cpi->sum_weights[i], 8.0);
2312 fprintf(f, "%5d\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
2315 cpi->sum_psnr[i] / cpi->frames_in_layer[i],
2317 cpi->sum_psnr_p[i] / cpi->frames_in_layer[i],
2318 total_psnr2, total_ssim);
2323 double samples = 3.0 / 2 * cpi->count *
2324 lst_yv12->y_width * lst_yv12->y_height;
2325 double total_psnr = vp8_mse2psnr(samples, 255.0,
2326 cpi->total_sq_error);
2327 double total_psnr2 = vp8_mse2psnr(samples, 255.0,
2328 cpi->total_sq_error2);
2329 double total_ssim = 100 * pow(cpi->summed_quality /
2330 cpi->summed_weights, 8.0);
2332 fprintf(f, "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\t"
2333 "GLPsnrP\tVPXSSIM\t Time(us)\n");
2334 fprintf(f, "%7.3f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
2336 dr, cpi->total / cpi->count, total_psnr,
2337 cpi->totalp / cpi->count, total_psnr2,
2338 total_ssim, total_encode_time);
2342 if (cpi->b_calculate_ssimg)
2344 if (cpi->oxcf.number_of_layers > 1)
2348 fprintf(f, "Layer\tBitRate\tSSIM_Y\tSSIM_U\tSSIM_V\tSSIM_A\t"
2350 for (i=0; i<cpi->oxcf.number_of_layers; i++)
2352 double dr = (double)cpi->bytes_in_layer[i] *
2353 8.0 / 1000.0 / time_encoded;
2354 fprintf(f, "%5d\t%7.3f\t%6.4f\t"
2355 "%6.4f\t%6.4f\t%6.4f\t%8.0f\n",
2357 cpi->total_ssimg_y_in_layer[i] /
2358 cpi->frames_in_layer[i],
2359 cpi->total_ssimg_u_in_layer[i] /
2360 cpi->frames_in_layer[i],
2361 cpi->total_ssimg_v_in_layer[i] /
2362 cpi->frames_in_layer[i],
2363 cpi->total_ssimg_all_in_layer[i] /
2364 cpi->frames_in_layer[i],
2370 fprintf(f, "BitRate\tSSIM_Y\tSSIM_U\tSSIM_V\tSSIM_A\t"
2372 fprintf(f, "%7.3f\t%6.4f\t%6.4f\t%6.4f\t%6.4f\t%8.0f\n", dr,
2373 cpi->total_ssimg_y / cpi->count,
2374 cpi->total_ssimg_u / cpi->count,
2375 cpi->total_ssimg_v / cpi->count,
2376 cpi->total_ssimg_all / cpi->count, total_encode_time);
2382 f = fopen("qskip.stt", "a");
2383 fprintf(f, "minq:%d -maxq:%d skiptrue:skipfalse = %d:%d\n", cpi->oxcf.best_allowed_q, cpi->oxcf.worst_allowed_q, skiptruecount, skipfalsecount);
2394 if (cpi->compressor_speed == 2)
2397 FILE *f = fopen("cxspeed.stt", "a");
2398 cnt_pm /= cpi->common.MBs;
2400 for (i = 0; i < 16; i++)
2401 fprintf(f, "%5d", frames_at_speed[i]);
2404 //fprintf(f, "%10d PM %10d %10d %10d EF %10d %10d %10d\n", cpi->Speed, cpi->avg_pick_mode_time, (tot_pm/cnt_pm), cnt_pm, cpi->avg_encode_time, 0, 0);
2413 extern int count_mb_seg[4];
2414 FILE *f = fopen("modes.stt", "a");
2415 double dr = (double)cpi->frame_rate * (double)bytes * (double)8 / (double)count / (double)1000 ;
2416 fprintf(f, "intra_mode in Intra Frames:\n");
2417 fprintf(f, "Y: %8d, %8d, %8d, %8d, %8d\n", y_modes[0], y_modes[1], y_modes[2], y_modes[3], y_modes[4]);
2418 fprintf(f, "UV:%8d, %8d, %8d, %8d\n", uv_modes[0], uv_modes[1], uv_modes[2], uv_modes[3]);
2423 for (i = 0; i < 10; i++)
2424 fprintf(f, "%8d, ", b_modes[i]);
2430 fprintf(f, "Modes in Inter Frames:\n");
2431 fprintf(f, "Y: %8d, %8d, %8d, %8d, %8d, %8d, %8d, %8d, %8d, %8d\n",
2432 inter_y_modes[0], inter_y_modes[1], inter_y_modes[2], inter_y_modes[3], inter_y_modes[4],
2433 inter_y_modes[5], inter_y_modes[6], inter_y_modes[7], inter_y_modes[8], inter_y_modes[9]);
2434 fprintf(f, "UV:%8d, %8d, %8d, %8d\n", inter_uv_modes[0], inter_uv_modes[1], inter_uv_modes[2], inter_uv_modes[3]);
2439 for (i = 0; i < 15; i++)
2440 fprintf(f, "%8d, ", inter_b_modes[i]);
2445 fprintf(f, "P:%8d, %8d, %8d, %8d\n", count_mb_seg[0], count_mb_seg[1], count_mb_seg[2], count_mb_seg[3]);
2446 fprintf(f, "PB:%8d, %8d, %8d, %8d\n", inter_b_modes[LEFT4X4], inter_b_modes[ABOVE4X4], inter_b_modes[ZERO4X4], inter_b_modes[NEW4X4]);
2454 #ifdef ENTROPY_STATS
2457 FILE *fmode = fopen("modecontext.c", "w");
2459 fprintf(fmode, "\n#include \"entropymode.h\"\n\n");
2460 fprintf(fmode, "const unsigned int vp8_kf_default_bmode_counts ");
2461 fprintf(fmode, "[VP8_BINTRAMODES] [VP8_BINTRAMODES] [VP8_BINTRAMODES] =\n{\n");
2463 for (i = 0; i < 10; i++)
2466 fprintf(fmode, " { //Above Mode : %d\n", i);
2468 for (j = 0; j < 10; j++)
2471 fprintf(fmode, " {");
2473 for (k = 0; k < 10; k++)
2475 if (!intra_mode_stats[i][j][k])
2476 fprintf(fmode, " %5d, ", 1);
2478 fprintf(fmode, " %5d, ", intra_mode_stats[i][j][k]);
2481 fprintf(fmode, "}, // left_mode %d\n", j);
2485 fprintf(fmode, " },\n");
2489 fprintf(fmode, "};\n");
2495 #if defined(SECTIONBITS_OUTPUT)
2500 FILE *f = fopen("tokenbits.stt", "a");
2502 for (i = 0; i < 28; i++)
2503 fprintf(f, "%8d", (int)(Sectionbits[i] / 256));
2513 printf("\n_pick_loop_filter_level:%d\n", cpi->time_pick_lpf / 1000);
2514 printf("\n_frames recive_data encod_mb_row compress_frame Total\n");
2515 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);
2521 #if CONFIG_MULTITHREAD
2522 vp8cx_remove_encoder_threads(cpi);
2525 dealloc_compressor_data(cpi);
2526 vpx_free(cpi->mb.ss);
2528 vpx_free(cpi->cyclic_refresh_map);
2530 vp8_remove_common(&cpi->common);
2534 #ifdef OUTPUT_YUV_SRC
2554 static uint64_t calc_plane_error(unsigned char *orig, int orig_stride,
2555 unsigned char *recon, int recon_stride,
2556 unsigned int cols, unsigned int rows,
2557 vp8_variance_rtcd_vtable_t *rtcd)
2559 unsigned int row, col;
2560 uint64_t total_sse = 0;
2563 for (row = 0; row + 16 <= rows; row += 16)
2565 for (col = 0; col + 16 <= cols; col += 16)
2569 VARIANCE_INVOKE(rtcd, mse16x16)(orig + col, orig_stride,
2570 recon + col, recon_stride,
2575 /* Handle odd-sized width */
2578 unsigned int border_row, border_col;
2579 unsigned char *border_orig = orig;
2580 unsigned char *border_recon = recon;
2582 for (border_row = 0; border_row < 16; border_row++)
2584 for (border_col = col; border_col < cols; border_col++)
2586 diff = border_orig[border_col] - border_recon[border_col];
2587 total_sse += diff * diff;
2590 border_orig += orig_stride;
2591 border_recon += recon_stride;
2595 orig += orig_stride * 16;
2596 recon += recon_stride * 16;
2599 /* Handle odd-sized height */
2600 for (; row < rows; row++)
2602 for (col = 0; col < cols; col++)
2604 diff = orig[col] - recon[col];
2605 total_sse += diff * diff;
2608 orig += orig_stride;
2609 recon += recon_stride;
2612 vp8_clear_system_state();
2617 static void generate_psnr_packet(VP8_COMP *cpi)
2619 YV12_BUFFER_CONFIG *orig = cpi->Source;
2620 YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
2621 struct vpx_codec_cx_pkt pkt;
2624 unsigned int width = cpi->common.Width;
2625 unsigned int height = cpi->common.Height;
2627 pkt.kind = VPX_CODEC_PSNR_PKT;
2628 sse = calc_plane_error(orig->y_buffer, orig->y_stride,
2629 recon->y_buffer, recon->y_stride,
2631 IF_RTCD(&cpi->rtcd.variance));
2632 pkt.data.psnr.sse[0] = sse;
2633 pkt.data.psnr.sse[1] = sse;
2634 pkt.data.psnr.samples[0] = width * height;
2635 pkt.data.psnr.samples[1] = width * height;
2637 width = (width + 1) / 2;
2638 height = (height + 1) / 2;
2640 sse = calc_plane_error(orig->u_buffer, orig->uv_stride,
2641 recon->u_buffer, recon->uv_stride,
2643 IF_RTCD(&cpi->rtcd.variance));
2644 pkt.data.psnr.sse[0] += sse;
2645 pkt.data.psnr.sse[2] = sse;
2646 pkt.data.psnr.samples[0] += width * height;
2647 pkt.data.psnr.samples[2] = width * height;
2649 sse = calc_plane_error(orig->v_buffer, orig->uv_stride,
2650 recon->v_buffer, recon->uv_stride,
2652 IF_RTCD(&cpi->rtcd.variance));
2653 pkt.data.psnr.sse[0] += sse;
2654 pkt.data.psnr.sse[3] = sse;
2655 pkt.data.psnr.samples[0] += width * height;
2656 pkt.data.psnr.samples[3] = width * height;
2658 for (i = 0; i < 4; i++)
2659 pkt.data.psnr.psnr[i] = vp8_mse2psnr(pkt.data.psnr.samples[i], 255.0,
2660 pkt.data.psnr.sse[i]);
2662 vpx_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
2666 int vp8_use_as_reference(VP8_PTR ptr, int ref_frame_flags)
2668 VP8_COMP *cpi = (VP8_COMP *)(ptr);
2670 if (ref_frame_flags > 7)
2673 cpi->ref_frame_flags = ref_frame_flags;
2676 int vp8_update_reference(VP8_PTR ptr, int ref_frame_flags)
2678 VP8_COMP *cpi = (VP8_COMP *)(ptr);
2680 if (ref_frame_flags > 7)
2683 cpi->common.refresh_golden_frame = 0;
2684 cpi->common.refresh_alt_ref_frame = 0;
2685 cpi->common.refresh_last_frame = 0;
2687 if (ref_frame_flags & VP8_LAST_FLAG)
2688 cpi->common.refresh_last_frame = 1;
2690 if (ref_frame_flags & VP8_GOLD_FLAG)
2691 cpi->common.refresh_golden_frame = 1;
2693 if (ref_frame_flags & VP8_ALT_FLAG)
2694 cpi->common.refresh_alt_ref_frame = 1;
2699 int vp8_get_reference(VP8_PTR ptr, VP8_REFFRAME ref_frame_flag, YV12_BUFFER_CONFIG *sd)
2701 VP8_COMP *cpi = (VP8_COMP *)(ptr);
2702 VP8_COMMON *cm = &cpi->common;
2705 if (ref_frame_flag == VP8_LAST_FLAG)
2706 ref_fb_idx = cm->lst_fb_idx;
2707 else if (ref_frame_flag == VP8_GOLD_FLAG)
2708 ref_fb_idx = cm->gld_fb_idx;
2709 else if (ref_frame_flag == VP8_ALT_FLAG)
2710 ref_fb_idx = cm->alt_fb_idx;
2714 vp8_yv12_copy_frame_ptr(&cm->yv12_fb[ref_fb_idx], sd);
2718 int vp8_set_reference(VP8_PTR ptr, VP8_REFFRAME ref_frame_flag, YV12_BUFFER_CONFIG *sd)
2720 VP8_COMP *cpi = (VP8_COMP *)(ptr);
2721 VP8_COMMON *cm = &cpi->common;
2725 if (ref_frame_flag == VP8_LAST_FLAG)
2726 ref_fb_idx = cm->lst_fb_idx;
2727 else if (ref_frame_flag == VP8_GOLD_FLAG)
2728 ref_fb_idx = cm->gld_fb_idx;
2729 else if (ref_frame_flag == VP8_ALT_FLAG)
2730 ref_fb_idx = cm->alt_fb_idx;
2734 vp8_yv12_copy_frame_ptr(sd, &cm->yv12_fb[ref_fb_idx]);
2738 int vp8_update_entropy(VP8_PTR comp, int update)
2740 VP8_COMP *cpi = (VP8_COMP *) comp;
2741 VP8_COMMON *cm = &cpi->common;
2742 cm->refresh_entropy_probs = update;
2749 void vp8_write_yuv_frame(const char *name, YV12_BUFFER_CONFIG *s)
2751 FILE *yuv_file = fopen(name, "ab");
2752 unsigned char *src = s->y_buffer;
2753 int h = s->y_height;
2757 fwrite(src, s->y_width, 1, yuv_file);
2767 fwrite(src, s->uv_width, 1, yuv_file);
2768 src += s->uv_stride;
2777 fwrite(src, s->uv_width, 1, yuv_file);
2778 src += s->uv_stride;
2787 static void scale_and_extend_source(YV12_BUFFER_CONFIG *sd, VP8_COMP *cpi)
2789 VP8_COMMON *cm = &cpi->common;
2791 // are we resizing the image
2792 if (cm->horiz_scale != 0 || cm->vert_scale != 0)
2794 #if CONFIG_SPATIAL_RESAMPLING
2795 int UNINITIALIZED_IS_SAFE(hr), UNINITIALIZED_IS_SAFE(hs);
2796 int UNINITIALIZED_IS_SAFE(vr), UNINITIALIZED_IS_SAFE(vs);
2799 if (cm->vert_scale == 3)
2804 Scale2Ratio(cm->horiz_scale, &hr, &hs);
2805 Scale2Ratio(cm->vert_scale, &vr, &vs);
2807 vp8_scale_frame(sd, &cpi->scaled_source, cm->temp_scale_frame.y_buffer,
2808 tmp_height, hs, hr, vs, vr, 0);
2810 vp8_yv12_extend_frame_borders(&cpi->scaled_source);
2811 cpi->Source = &cpi->scaled_source;
2819 static void resize_key_frame(VP8_COMP *cpi)
2821 #if CONFIG_SPATIAL_RESAMPLING
2822 VP8_COMMON *cm = &cpi->common;
2824 // Do we need to apply resampling for one pass cbr.
2825 // In one pass this is more limited than in two pass cbr
2826 // The test and any change is only made one per key frame sequence
2827 if (cpi->oxcf.allow_spatial_resampling && (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER))
2829 int UNINITIALIZED_IS_SAFE(hr), UNINITIALIZED_IS_SAFE(hs);
2830 int UNINITIALIZED_IS_SAFE(vr), UNINITIALIZED_IS_SAFE(vs);
2831 int new_width, new_height;
2833 // If we are below the resample DOWN watermark then scale down a notch.
2834 if (cpi->buffer_level < (cpi->oxcf.resample_down_water_mark * cpi->oxcf.optimal_buffer_level / 100))
2836 cm->horiz_scale = (cm->horiz_scale < ONETWO) ? cm->horiz_scale + 1 : ONETWO;
2837 cm->vert_scale = (cm->vert_scale < ONETWO) ? cm->vert_scale + 1 : ONETWO;
2839 // Should we now start scaling back up
2840 else if (cpi->buffer_level > (cpi->oxcf.resample_up_water_mark * cpi->oxcf.optimal_buffer_level / 100))
2842 cm->horiz_scale = (cm->horiz_scale > NORMAL) ? cm->horiz_scale - 1 : NORMAL;
2843 cm->vert_scale = (cm->vert_scale > NORMAL) ? cm->vert_scale - 1 : NORMAL;
2846 // Get the new hieght and width
2847 Scale2Ratio(cm->horiz_scale, &hr, &hs);
2848 Scale2Ratio(cm->vert_scale, &vr, &vs);
2849 new_width = ((hs - 1) + (cpi->oxcf.Width * hr)) / hs;
2850 new_height = ((vs - 1) + (cpi->oxcf.Height * vr)) / vs;
2852 // If the image size has changed we need to reallocate the buffers
2853 // and resample the source image
2854 if ((cm->Width != new_width) || (cm->Height != new_height))
2856 cm->Width = new_width;
2857 cm->Height = new_height;
2858 vp8_alloc_compressor_data(cpi);
2859 scale_and_extend_source(cpi->un_scaled_source, cpi);
2867 static void update_alt_ref_frame_stats(VP8_COMP *cpi)
2869 VP8_COMMON *cm = &cpi->common;
2871 // Select an interval before next GF or altref
2872 if (!cpi->auto_gold)
2873 cpi->frames_till_gf_update_due = cpi->goldfreq;
2875 if ((cpi->pass != 2) && cpi->frames_till_gf_update_due)
2877 cpi->current_gf_interval = cpi->frames_till_gf_update_due;
2879 // Set the bits per frame that we should try and recover in subsequent inter frames
2880 // to account for the extra GF spend... note that his does not apply for GF updates
2881 // that occur coincident with a key frame as the extra cost of key frames is dealt
2884 cpi->gf_overspend_bits += cpi->projected_frame_size;
2885 cpi->non_gf_bitrate_adjustment = cpi->gf_overspend_bits / cpi->frames_till_gf_update_due;
2888 // Update data structure that monitors level of reference to last GF
2889 vpx_memset(cpi->gf_active_flags, 1, (cm->mb_rows * cm->mb_cols));
2890 cpi->gf_active_count = cm->mb_rows * cm->mb_cols;
2892 // this frame refreshes means next frames don't unless specified by user
2893 cpi->common.frames_since_golden = 0;
2895 // Clear the alternate reference update pending flag.
2896 cpi->source_alt_ref_pending = FALSE;
2898 // Set the alternate refernce frame active flag
2899 cpi->source_alt_ref_active = TRUE;
2903 static void update_golden_frame_stats(VP8_COMP *cpi)
2905 VP8_COMMON *cm = &cpi->common;
2907 // Update the Golden frame usage counts.
2908 if (cm->refresh_golden_frame)
2910 // Select an interval before next GF
2911 if (!cpi->auto_gold)
2912 cpi->frames_till_gf_update_due = cpi->goldfreq;
2914 if ((cpi->pass != 2) && (cpi->frames_till_gf_update_due > 0))
2916 cpi->current_gf_interval = cpi->frames_till_gf_update_due;
2918 // Set the bits per frame that we should try and recover in subsequent inter frames
2919 // to account for the extra GF spend... note that his does not apply for GF updates
2920 // that occur coincident with a key frame as the extra cost of key frames is dealt
2922 if ((cm->frame_type != KEY_FRAME) && !cpi->source_alt_ref_active)
2924 // Calcluate GF bits to be recovered
2925 // Projected size - av frame bits available for inter frames for clip as a whole
2926 cpi->gf_overspend_bits += (cpi->projected_frame_size - cpi->inter_frame_target);
2929 cpi->non_gf_bitrate_adjustment = cpi->gf_overspend_bits / cpi->frames_till_gf_update_due;
2933 // Update data structure that monitors level of reference to last GF
2934 vpx_memset(cpi->gf_active_flags, 1, (cm->mb_rows * cm->mb_cols));
2935 cpi->gf_active_count = cm->mb_rows * cm->mb_cols;
2937 // this frame refreshes means next frames don't unless specified by user
2938 cm->refresh_golden_frame = 0;
2939 cpi->common.frames_since_golden = 0;
2941 //if ( cm->frame_type == KEY_FRAME )
2943 cpi->recent_ref_frame_usage[INTRA_FRAME] = 1;
2944 cpi->recent_ref_frame_usage[LAST_FRAME] = 1;
2945 cpi->recent_ref_frame_usage[GOLDEN_FRAME] = 1;
2946 cpi->recent_ref_frame_usage[ALTREF_FRAME] = 1;
2950 // // Carry a potrtion of count over to begining of next gf sequence
2951 // cpi->recent_ref_frame_usage[INTRA_FRAME] >>= 5;
2952 // cpi->recent_ref_frame_usage[LAST_FRAME] >>= 5;
2953 // cpi->recent_ref_frame_usage[GOLDEN_FRAME] >>= 5;
2954 // cpi->recent_ref_frame_usage[ALTREF_FRAME] >>= 5;
2957 // ******** Fixed Q test code only ************
2958 // If we are going to use the ALT reference for the next group of frames set a flag to say so.
2959 if (cpi->oxcf.fixed_q >= 0 &&
2960 cpi->oxcf.play_alternate && !cpi->common.refresh_alt_ref_frame)
2962 cpi->source_alt_ref_pending = TRUE;
2963 cpi->frames_till_gf_update_due = cpi->baseline_gf_interval;
2966 if (!cpi->source_alt_ref_pending)
2967 cpi->source_alt_ref_active = FALSE;
2969 // Decrement count down till next gf
2970 if (cpi->frames_till_gf_update_due > 0)
2971 cpi->frames_till_gf_update_due--;
2974 else if (!cpi->common.refresh_alt_ref_frame)
2976 // Decrement count down till next gf
2977 if (cpi->frames_till_gf_update_due > 0)
2978 cpi->frames_till_gf_update_due--;
2980 if (cpi->common.frames_till_alt_ref_frame)
2981 cpi->common.frames_till_alt_ref_frame --;
2983 cpi->common.frames_since_golden ++;
2985 if (cpi->common.frames_since_golden > 1)
2987 cpi->recent_ref_frame_usage[INTRA_FRAME] += cpi->count_mb_ref_frame_usage[INTRA_FRAME];
2988 cpi->recent_ref_frame_usage[LAST_FRAME] += cpi->count_mb_ref_frame_usage[LAST_FRAME];
2989 cpi->recent_ref_frame_usage[GOLDEN_FRAME] += cpi->count_mb_ref_frame_usage[GOLDEN_FRAME];
2990 cpi->recent_ref_frame_usage[ALTREF_FRAME] += cpi->count_mb_ref_frame_usage[ALTREF_FRAME];
2995 // This function updates the reference frame probability estimates that
2996 // will be used during mode selection
2997 static void update_rd_ref_frame_probs(VP8_COMP *cpi)
2999 VP8_COMMON *cm = &cpi->common;
3001 const int *const rfct = cpi->count_mb_ref_frame_usage;
3002 const int rf_intra = rfct[INTRA_FRAME];
3003 const int rf_inter = rfct[LAST_FRAME] + rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME];
3005 if (cm->frame_type == KEY_FRAME)
3007 cpi->prob_intra_coded = 255;
3008 cpi->prob_last_coded = 128;
3009 cpi->prob_gf_coded = 128;
3011 else if (!(rf_intra + rf_inter))
3013 cpi->prob_intra_coded = 63;
3014 cpi->prob_last_coded = 128;
3015 cpi->prob_gf_coded = 128;
3018 // update reference frame costs since we can do better than what we got last frame.
3019 if (cpi->oxcf.number_of_layers == 1)
3021 if (cpi->common.refresh_alt_ref_frame)
3023 cpi->prob_intra_coded += 40;
3024 cpi->prob_last_coded = 200;
3025 cpi->prob_gf_coded = 1;
3027 else if (cpi->common.frames_since_golden == 0)
3029 cpi->prob_last_coded = 214;
3031 else if (cpi->common.frames_since_golden == 1)
3033 cpi->prob_last_coded = 192;
3034 cpi->prob_gf_coded = 220;
3036 else if (cpi->source_alt_ref_active)
3038 cpi->prob_gf_coded -= 20;
3040 if (cpi->prob_gf_coded < 10)
3041 cpi->prob_gf_coded = 10;
3043 if (!cpi->source_alt_ref_active)
3044 cpi->prob_gf_coded = 255;
3049 // 1 = key, 0 = inter
3050 static int decide_key_frame(VP8_COMP *cpi)
3052 VP8_COMMON *cm = &cpi->common;
3054 int code_key_frame = FALSE;
3058 if (cpi->Speed > 11)
3061 // Clear down mmx registers
3062 vp8_clear_system_state(); //__asm emms;
3064 if ((cpi->compressor_speed == 2) && (cpi->Speed >= 5) && (cpi->sf.RD == 0))
3066 double change = 1.0 * abs((int)(cpi->intra_error - cpi->last_intra_error)) / (1 + cpi->last_intra_error);
3067 double change2 = 1.0 * abs((int)(cpi->prediction_error - cpi->last_prediction_error)) / (1 + cpi->last_prediction_error);
3068 double minerror = cm->MBs * 256;
3072 if (10 * cpi->intra_error / (1 + cpi->prediction_error) < 15
3073 && cpi->prediction_error > minerror
3074 && (change > .25 || change2 > .25))
3076 FILE *f = fopen("intra_inter.stt", "a");
3078 if (cpi->prediction_error <= 0)
3079 cpi->prediction_error = 1;
3081 fprintf(f, "%d %d %d %d %14.4f\n",
3082 cm->current_video_frame,
3083 (int) cpi->prediction_error,
3084 (int) cpi->intra_error,
3085 (int)((10 * cpi->intra_error) / cpi->prediction_error),
3093 cpi->last_intra_error = cpi->intra_error;
3094 cpi->last_prediction_error = cpi->prediction_error;
3096 if (10 * cpi->intra_error / (1 + cpi->prediction_error) < 15
3097 && cpi->prediction_error > minerror
3098 && (change > .25 || change2 > .25))
3100 /*(change > 1.4 || change < .75)&& cpi->this_frame_percent_intra > cpi->last_frame_percent_intra + 3*/
3108 // If the following are true we might as well code a key frame
3109 if (((cpi->this_frame_percent_intra == 100) &&
3110 (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra + 2))) ||
3111 ((cpi->this_frame_percent_intra > 95) &&
3112 (cpi->this_frame_percent_intra >= (cpi->last_frame_percent_intra + 5))))
3114 code_key_frame = TRUE;
3116 // in addition if the following are true and this is not a golden frame then code a key frame
3117 // Note that on golden frames there often seems to be a pop in intra useage anyway hence this
3118 // restriction is designed to prevent spurious key frames. The Intra pop needs to be investigated.
3119 else if (((cpi->this_frame_percent_intra > 60) &&
3120 (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra * 2))) ||
3121 ((cpi->this_frame_percent_intra > 75) &&
3122 (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra * 3 / 2))) ||
3123 ((cpi->this_frame_percent_intra > 90) &&
3124 (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra + 10))))
3126 if (!cm->refresh_golden_frame)
3127 code_key_frame = TRUE;
3130 return code_key_frame;
3134 #if !(CONFIG_REALTIME_ONLY)
3135 static void Pass1Encode(VP8_COMP *cpi, unsigned long *size, unsigned char *dest, unsigned int *frame_flags)
3140 vp8_set_quantizer(cpi, 26);
3142 scale_and_extend_source(cpi->un_scaled_source, cpi);
3143 vp8_first_pass(cpi);
3148 void write_cx_frame_to_file(YV12_BUFFER_CONFIG *frame, int this_frame)
3156 sprintf(filename, "cx\\y%04d.raw", this_frame);
3157 yframe = fopen(filename, "wb");
3159 for (i = 0; i < frame->y_height; i++)
3160 fwrite(frame->y_buffer + i * frame->y_stride, frame->y_width, 1, yframe);
3163 sprintf(filename, "cx\\u%04d.raw", this_frame);
3164 yframe = fopen(filename, "wb");
3166 for (i = 0; i < frame->uv_height; i++)
3167 fwrite(frame->u_buffer + i * frame->uv_stride, frame->uv_width, 1, yframe);
3170 sprintf(filename, "cx\\v%04d.raw", this_frame);
3171 yframe = fopen(filename, "wb");
3173 for (i = 0; i < frame->uv_height; i++)
3174 fwrite(frame->v_buffer + i * frame->uv_stride, frame->uv_width, 1, yframe);
3179 // return of 0 means drop frame
3181 // Function to test for conditions that indeicate we should loop
3182 // back and recode a frame.
3183 static BOOL recode_loop_test( VP8_COMP *cpi,
3184 int high_limit, int low_limit,
3185 int q, int maxq, int minq )
3187 BOOL force_recode = FALSE;
3188 VP8_COMMON *cm = &cpi->common;
3190 // Is frame recode allowed at all
3191 // Yes if either recode mode 1 is selected or mode two is selcted
3192 // and the frame is a key frame. golden frame or alt_ref_frame
3193 if ( (cpi->sf.recode_loop == 1) ||
3194 ( (cpi->sf.recode_loop == 2) &&
3195 ( (cm->frame_type == KEY_FRAME) ||
3196 cm->refresh_golden_frame ||
3197 cm->refresh_alt_ref_frame ) ) )
3199 // General over and under shoot tests
3200 if ( ((cpi->projected_frame_size > high_limit) && (q < maxq)) ||
3201 ((cpi->projected_frame_size < low_limit) && (q > minq)) )
3203 force_recode = TRUE;
3205 // Special Constrained quality tests
3206 else if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY)
3208 // Undershoot and below auto cq level
3209 if ( (q > cpi->cq_target_quality) &&
3210 (cpi->projected_frame_size <
3211 ((cpi->this_frame_target * 7) >> 3)))
3213 force_recode = TRUE;
3215 // Severe undershoot and between auto and user cq level
3216 else if ( (q > cpi->oxcf.cq_level) &&
3217 (cpi->projected_frame_size < cpi->min_frame_bandwidth) &&
3218 (cpi->active_best_quality > cpi->oxcf.cq_level))
3220 force_recode = TRUE;
3221 cpi->active_best_quality = cpi->oxcf.cq_level;
3226 return force_recode;
3229 void update_reference_frames(VP8_COMMON *cm)
3231 YV12_BUFFER_CONFIG *yv12_fb = cm->yv12_fb;
3233 // At this point the new frame has been encoded.
3234 // If any buffer copy / swapping is signaled it should be done here.
3236 if (cm->frame_type == KEY_FRAME)
3238 yv12_fb[cm->new_fb_idx].flags |= VP8_GOLD_FLAG | VP8_ALT_FLAG ;
3240 yv12_fb[cm->gld_fb_idx].flags &= ~VP8_GOLD_FLAG;
3241 yv12_fb[cm->alt_fb_idx].flags &= ~VP8_ALT_FLAG;
3243 cm->alt_fb_idx = cm->gld_fb_idx = cm->new_fb_idx;
3245 else /* For non key frames */
3247 if (cm->refresh_alt_ref_frame)
3249 assert(!cm->copy_buffer_to_arf);
3251 cm->yv12_fb[cm->new_fb_idx].flags |= VP8_ALT_FLAG;
3252 cm->yv12_fb[cm->alt_fb_idx].flags &= ~VP8_ALT_FLAG;
3253 cm->alt_fb_idx = cm->new_fb_idx;
3255 else if (cm->copy_buffer_to_arf)
3257 assert(!(cm->copy_buffer_to_arf & ~0x3));
3259 if (cm->copy_buffer_to_arf == 1)
3261 if(cm->alt_fb_idx != cm->lst_fb_idx)
3263 yv12_fb[cm->lst_fb_idx].flags |= VP8_ALT_FLAG;
3264 yv12_fb[cm->alt_fb_idx].flags &= ~VP8_ALT_FLAG;
3265 cm->alt_fb_idx = cm->lst_fb_idx;
3268 else /* if (cm->copy_buffer_to_arf == 2) */
3270 if(cm->alt_fb_idx != cm->gld_fb_idx)
3272 yv12_fb[cm->gld_fb_idx].flags |= VP8_ALT_FLAG;
3273 yv12_fb[cm->alt_fb_idx].flags &= ~VP8_ALT_FLAG;
3274 cm->alt_fb_idx = cm->gld_fb_idx;
3279 if (cm->refresh_golden_frame)
3281 assert(!cm->copy_buffer_to_gf);
3283 cm->yv12_fb[cm->new_fb_idx].flags |= VP8_GOLD_FLAG;
3284 cm->yv12_fb[cm->gld_fb_idx].flags &= ~VP8_GOLD_FLAG;
3285 cm->gld_fb_idx = cm->new_fb_idx;
3287 else if (cm->copy_buffer_to_gf)
3289 assert(!(cm->copy_buffer_to_arf & ~0x3));
3291 if (cm->copy_buffer_to_gf == 1)
3293 if(cm->gld_fb_idx != cm->lst_fb_idx)
3295 yv12_fb[cm->lst_fb_idx].flags |= VP8_GOLD_FLAG;
3296 yv12_fb[cm->gld_fb_idx].flags &= ~VP8_GOLD_FLAG;
3297 cm->gld_fb_idx = cm->lst_fb_idx;
3300 else /* if (cm->copy_buffer_to_gf == 2) */
3302 if(cm->alt_fb_idx != cm->gld_fb_idx)
3304 yv12_fb[cm->alt_fb_idx].flags |= VP8_GOLD_FLAG;
3305 yv12_fb[cm->gld_fb_idx].flags &= ~VP8_GOLD_FLAG;
3306 cm->gld_fb_idx = cm->alt_fb_idx;
3312 if (cm->refresh_last_frame)
3314 cm->yv12_fb[cm->new_fb_idx].flags |= VP8_LAST_FLAG;
3315 cm->yv12_fb[cm->lst_fb_idx].flags &= ~VP8_LAST_FLAG;
3316 cm->lst_fb_idx = cm->new_fb_idx;
3320 void loopfilter_frame(VP8_COMP *cpi, VP8_COMMON *cm)
3324 cm->filter_level = 0;
3328 struct vpx_usec_timer timer;
3330 vp8_clear_system_state();
3332 vpx_usec_timer_start(&timer);
3333 if (cpi->sf.auto_filter == 0)
3334 vp8cx_pick_filter_level_fast(cpi->Source, cpi);
3337 vp8cx_pick_filter_level(cpi->Source, cpi);
3339 vpx_usec_timer_mark(&timer);
3340 cpi->time_pick_lpf += vpx_usec_timer_elapsed(&timer);
3343 #if CONFIG_MULTITHREAD
3344 if (cpi->b_multi_threaded)
3345 sem_post(&cpi->h_event_end_lpf); /* signal that we have set filter_level */
3348 if (cm->filter_level > 0)
3350 vp8cx_set_alt_lf_level(cpi, cm->filter_level);
3351 vp8_loop_filter_frame(cm, &cpi->mb.e_mbd);
3354 vp8_yv12_extend_frame_borders_ptr(cm->frame_to_show);
3358 static void encode_frame_to_data_rate
3361 unsigned long *size,
3362 unsigned char *dest,
3363 unsigned char* dest_end,
3364 unsigned int *frame_flags
3368 int frame_over_shoot_limit;
3369 int frame_under_shoot_limit;
3379 int zbin_oq_low = 0;
3382 VP8_COMMON *cm = &cpi->common;
3383 int active_worst_qchanged = FALSE;
3385 int overshoot_seen = FALSE;
3386 int undershoot_seen = FALSE;
3387 int drop_mark = cpi->oxcf.drop_frames_water_mark * cpi->oxcf.optimal_buffer_level / 100;
3388 int drop_mark75 = drop_mark * 2 / 3;
3389 int drop_mark50 = drop_mark / 4;
3390 int drop_mark25 = drop_mark / 8;
3393 // Clear down mmx registers to allow floating point in what follows
3394 vp8_clear_system_state();
3396 // Test code for segmentation of gf/arf (0,0)
3397 //segmentation_test_function((VP8_PTR) cpi);
3399 if (cpi->compressor_speed == 2)
3401 if(cpi->oxcf.auto_key && cm->frame_type != KEY_FRAME)
3403 if(cpi->force_next_frame_intra)
3405 cm->frame_type = KEY_FRAME; /* delayed intra frame */
3408 cpi->force_next_frame_intra = 0;
3411 // For an alt ref frame in 2 pass we skip the call to the second pass function that sets the target bandwidth
3412 #if !(CONFIG_REALTIME_ONLY)
3416 if (cpi->common.refresh_alt_ref_frame)
3418 cpi->per_frame_bandwidth = cpi->twopass.gf_bits; // Per frame bit target for the alt ref frame
3419 cpi->target_bandwidth = cpi->twopass.gf_bits * cpi->output_frame_rate; // per second target bitrate
3424 cpi->per_frame_bandwidth = (int)(cpi->target_bandwidth / cpi->output_frame_rate);
3426 // Default turn off buffer to buffer copying
3427 cm->copy_buffer_to_gf = 0;
3428 cm->copy_buffer_to_arf = 0;
3430 // Clear zbin over-quant value and mode boost values.
3431 cpi->zbin_over_quant = 0;
3432 cpi->zbin_mode_boost = 0;
3434 // Enable or disable mode based tweaking of the zbin
3435 // For 2 Pass Only used where GF/ARF prediction quality
3436 // is above a threshold
3437 cpi->zbin_mode_boost_enabled = TRUE;
3440 if ( cpi->gfu_boost <= 400 )
3442 cpi->zbin_mode_boost_enabled = FALSE;
3446 // Current default encoder behaviour for the altref sign bias
3447 if (cpi->source_alt_ref_active)
3448 cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = 1;
3450 cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = 0;
3452 // Check to see if a key frame is signalled
3453 // For two pass with auto key frame enabled cm->frame_type may already be set, but not for one pass.
3454 if ((cm->current_video_frame == 0) ||
3455 (cm->frame_flags & FRAMEFLAGS_KEY) ||
3456 (cpi->oxcf.auto_key && (cpi->frames_since_key % cpi->key_frame_frequency == 0)))
3458 // Key frame from VFW/auto-keyframe/first frame
3459 cm->frame_type = KEY_FRAME;
3462 // Set default state for segment and mode based loop filter update flags
3463 cpi->mb.e_mbd.update_mb_segmentation_map = 0;
3464 cpi->mb.e_mbd.update_mb_segmentation_data = 0;
3465 cpi->mb.e_mbd.mode_ref_lf_delta_update = 0;
3467 // Set various flags etc to special state if it is a key frame
3468 if (cm->frame_type == KEY_FRAME)
3472 // Reset the loop filter deltas and segmentation map
3473 setup_features(cpi);
3475 // If segmentation is enabled force a map update for key frames
3476 if (cpi->mb.e_mbd.segmentation_enabled)
3478 cpi->mb.e_mbd.update_mb_segmentation_map = 1;
3479 cpi->mb.e_mbd.update_mb_segmentation_data = 1;
3482 // The alternate reference frame cannot be active for a key frame
3483 cpi->source_alt_ref_active = FALSE;
3485 // Reset the RD threshold multipliers to default of * 1 (128)
3486 for (i = 0; i < MAX_MODES; i++)
3488 cpi->rd_thresh_mult[i] = 128;
3492 // Test code for segmentation
3493 //if ( (cm->frame_type == KEY_FRAME) || ((cm->current_video_frame % 2) == 0))
3494 //if ( (cm->current_video_frame % 2) == 0 )
3495 // enable_segmentation((VP8_PTR)cpi);
3497 // disable_segmentation((VP8_PTR)cpi);
3500 // Experimental code for lagged compress and one pass
3501 // Initialise one_pass GF frames stats
3502 // Update stats used for GF selection
3503 //if ( cpi->pass == 0 )
3505 cpi->one_pass_frame_index = cm->current_video_frame % MAX_LAG_BUFFERS;
3507 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frames_so_far = 0;
3508 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_intra_error = 0.0;
3509 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_coded_error = 0.0;
3510 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_pcnt_inter = 0.0;
3511 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_pcnt_motion = 0.0;
3512 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvr = 0.0;
3513 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvr_abs = 0.0;
3514 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvc = 0.0;
3515 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvc_abs = 0.0;
3519 update_rd_ref_frame_probs(cpi);
3521 if (cpi->drop_frames_allowed)
3523 // The reset to decimation 0 is only done here for one pass.
3524 // Once it is set two pass leaves decimation on till the next kf.
3525 if ((cpi->buffer_level > drop_mark) && (cpi->decimation_factor > 0))
3526 cpi->decimation_factor --;
3528 if (cpi->buffer_level > drop_mark75 && cpi->decimation_factor > 0)
3529 cpi->decimation_factor = 1;
3531 else if (cpi->buffer_level < drop_mark25 && (cpi->decimation_factor == 2 || cpi->decimation_factor == 3))
3533 cpi->decimation_factor = 3;
3535 else if (cpi->buffer_level < drop_mark50 && (cpi->decimation_factor == 1 || cpi->decimation_factor == 2))
3537 cpi->decimation_factor = 2;
3539 else if (cpi->buffer_level < drop_mark75 && (cpi->decimation_factor == 0 || cpi->decimation_factor == 1))
3541 cpi->decimation_factor = 1;
3544 //vpx_log("Encoder: Decimation Factor: %d \n",cpi->decimation_factor);
3547 // The following decimates the frame rate according to a regular pattern (i.e. to 1/2 or 2/3 frame rate)
3548 // This can be used to help prevent buffer under-run in CBR mode. Alternatively it might be desirable in
3549 // some situations to drop frame rate but throw more bits at each frame.
3551 // Note that dropping a key frame can be problematic if spatial resampling is also active
3552 if (cpi->decimation_factor > 0)
3554 switch (cpi->decimation_factor)
3557 cpi->per_frame_bandwidth = cpi->per_frame_bandwidth * 3 / 2;
3560 cpi->per_frame_bandwidth = cpi->per_frame_bandwidth * 5 / 4;
3563 cpi->per_frame_bandwidth = cpi->per_frame_bandwidth * 5 / 4;
3567 // Note that we should not throw out a key frame (especially when spatial resampling is enabled).
3568 if ((cm->frame_type == KEY_FRAME)) // && cpi->oxcf.allow_spatial_resampling )
3570 cpi->decimation_count = cpi->decimation_factor;
3572 else if (cpi->decimation_count > 0)
3574 cpi->decimation_count --;
3576 cpi->bits_off_target += cpi->av_per_frame_bandwidth;
3577 if (cpi->bits_off_target > cpi->oxcf.maximum_buffer_size)
3578 cpi->bits_off_target = cpi->oxcf.maximum_buffer_size;
3580 cm->current_video_frame++;
3581 cpi->frames_since_key++;
3583 #if CONFIG_INTERNAL_STATS
3587 cpi->buffer_level = cpi->bits_off_target;
3589 if (cpi->oxcf.number_of_layers > 1)
3593 // Propagate bits saved by dropping the frame to higher layers
3594 for (i=cpi->current_layer+1; i<cpi->oxcf.number_of_layers; i++)
3596 LAYER_CONTEXT *lc = &cpi->layer_context[i];
3597 lc->bits_off_target += cpi->av_per_frame_bandwidth;
3598 if (lc->bits_off_target > lc->maximum_buffer_size)
3599 lc->bits_off_target = lc->maximum_buffer_size;
3600 lc->buffer_level = lc->bits_off_target;
3607 cpi->decimation_count = cpi->decimation_factor;
3610 // Decide how big to make the frame
3611 if (!vp8_pick_frame_size(cpi))
3613 cm->current_video_frame++;
3614 cpi->frames_since_key++;
3618 // Reduce active_worst_allowed_q for CBR if our buffer is getting too full.
3619 // This has a knock on effect on active best quality as well.
3620 // For CBR if the buffer reaches its maximum level then we can no longer
3621 // save up bits for later frames so we might as well use them up
3622 // on the current frame.
3623 if ((cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) &&
3624 (cpi->buffer_level >= cpi->oxcf.optimal_buffer_level) && cpi->buffered_mode)
3626 int Adjustment = cpi->active_worst_quality / 4; // Max adjustment is 1/4
3632 if (cpi->buffer_level < cpi->oxcf.maximum_buffer_size)
3634 buff_lvl_step = (cpi->oxcf.maximum_buffer_size - cpi->oxcf.optimal_buffer_level) / Adjustment;
3637 Adjustment = (cpi->buffer_level - cpi->oxcf.optimal_buffer_level) / buff_lvl_step;
3642 cpi->active_worst_quality -= Adjustment;
3644 if(cpi->active_worst_quality < cpi->active_best_quality)
3645 cpi->active_worst_quality = cpi->active_best_quality;
3649 // Set an active best quality and if necessary active worst quality
3650 // There is some odd behavior for one pass here that needs attention.
3651 if ( (cpi->pass == 2) || (cpi->ni_frames > 150))
3653 vp8_clear_system_state();
3655 Q = cpi->active_worst_quality;
3657 if ( cm->frame_type == KEY_FRAME )
3659 if ( cpi->pass == 2 )
3661 if (cpi->gfu_boost > 600)
3662 cpi->active_best_quality = kf_low_motion_minq[Q];
3664 cpi->active_best_quality = kf_high_motion_minq[Q];
3666 // Special case for key frames forced because we have reached
3667 // the maximum key frame interval. Here force the Q to a range
3668 // based on the ambient Q to reduce the risk of popping
3669 if ( cpi->this_key_frame_forced )
3671 if ( cpi->active_best_quality > cpi->avg_frame_qindex * 7/8)
3672 cpi->active_best_quality = cpi->avg_frame_qindex * 7/8;
3673 else if ( cpi->active_best_quality < cpi->avg_frame_qindex >> 2 )
3674 cpi->active_best_quality = cpi->avg_frame_qindex >> 2;
3677 // One pass more conservative
3679 cpi->active_best_quality = kf_high_motion_minq[Q];
3682 else if (cpi->oxcf.number_of_layers==1 &&
3683 (cm->refresh_golden_frame || cpi->common.refresh_alt_ref_frame))
3685 // Use the lower of cpi->active_worst_quality and recent
3686 // average Q as basis for GF/ARF Q limit unless last frame was
3688 if ( (cpi->frames_since_key > 1) &&
3689 (cpi->avg_frame_qindex < cpi->active_worst_quality) )
3691 Q = cpi->avg_frame_qindex;
3694 // For constrained quality dont allow Q less than the cq level
3695 if ( (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) &&
3696 (Q < cpi->cq_target_quality) )
3698 Q = cpi->cq_target_quality;
3701 if ( cpi->pass == 2 )
3703 if ( cpi->gfu_boost > 1000 )
3704 cpi->active_best_quality = gf_low_motion_minq[Q];
3705 else if ( cpi->gfu_boost < 400 )
3706 cpi->active_best_quality = gf_high_motion_minq[Q];
3708 cpi->active_best_quality = gf_mid_motion_minq[Q];
3710 // Constrained quality use slightly lower active best.
3711 if ( cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY )
3713 cpi->active_best_quality =
3714 cpi->active_best_quality * 15/16;
3717 // One pass more conservative
3719 cpi->active_best_quality = gf_high_motion_minq[Q];
3723 cpi->active_best_quality = inter_minq[Q];
3725 // For the constant/constrained quality mode we dont want
3726 // q to fall below the cq level.
3727 if ((cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) &&
3728 (cpi->active_best_quality < cpi->cq_target_quality) )
3730 // If we are strongly undershooting the target rate in the last
3731 // frames then use the user passed in cq value not the auto
3733 if ( cpi->rolling_actual_bits < cpi->min_frame_bandwidth )
3734 cpi->active_best_quality = cpi->oxcf.cq_level;
3736 cpi->active_best_quality = cpi->cq_target_quality;
3740 // If CBR and the buffer is as full then it is reasonable to allow
3741 // higher quality on the frames to prevent bits just going to waste.
3742 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
3744 // Note that the use of >= here elliminates the risk of a devide
3745 // by 0 error in the else if clause
3746 if (cpi->buffer_level >= cpi->oxcf.maximum_buffer_size)
3747 cpi->active_best_quality = cpi->best_quality;
3749 else if (cpi->buffer_level > cpi->oxcf.optimal_buffer_level)
3751 int Fraction = ((cpi->buffer_level - cpi->oxcf.optimal_buffer_level) * 128) / (cpi->oxcf.maximum_buffer_size - cpi->oxcf.optimal_buffer_level);
3752 int min_qadjustment = ((cpi->active_best_quality - cpi->best_quality) * Fraction) / 128;
3754 cpi->active_best_quality -= min_qadjustment;
3758 // Make sure constrained quality mode limits are adhered to for the first
3759 // few frames of one pass encodes
3760 else if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY)
3762 if ( (cm->frame_type == KEY_FRAME) ||
3763 cm->refresh_golden_frame || cpi->common.refresh_alt_ref_frame )
3765 cpi->active_best_quality = cpi->best_quality;
3767 else if (cpi->active_best_quality < cpi->cq_target_quality)
3769 cpi->active_best_quality = cpi->cq_target_quality;
3773 // Clip the active best and worst quality values to limits
3774 if (cpi->active_worst_quality > cpi->worst_quality)
3775 cpi->active_worst_quality = cpi->worst_quality;
3777 if (cpi->active_best_quality < cpi->best_quality)
3778 cpi->active_best_quality = cpi->best_quality;
3780 if ( cpi->active_worst_quality < cpi->active_best_quality )
3781 cpi->active_worst_quality = cpi->active_best_quality;
3783 // Determine initial Q to try
3784 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
3785 last_zbin_oq = cpi->zbin_over_quant;
3787 // Set highest allowed value for Zbin over quant
3788 if (cm->frame_type == KEY_FRAME)
3789 zbin_oq_high = 0; //ZBIN_OQ_MAX/16
3790 else if ((cpi->oxcf.number_of_layers == 1) && ((cm->refresh_alt_ref_frame ||
3791 (cm->refresh_golden_frame && !cpi->source_alt_ref_active))))
3796 zbin_oq_high = ZBIN_OQ_MAX;
3798 // Setup background Q adjustment for error resilient mode.
3799 // For multi-layer encodes only enable this for the base layer.
3800 if (cpi->cyclic_refresh_mode_enabled && (cpi->current_layer==0))
3801 cyclic_background_refresh(cpi, Q, 0);
3803 vp8_compute_frame_size_bounds(cpi, &frame_under_shoot_limit, &frame_over_shoot_limit);
3805 // Limit Q range for the adaptive loop.
3806 bottom_index = cpi->active_best_quality;
3807 top_index = cpi->active_worst_quality;
3808 q_low = cpi->active_best_quality;
3809 q_high = cpi->active_worst_quality;
3811 vp8_save_coding_context(cpi);
3816 scale_and_extend_source(cpi->un_scaled_source, cpi);
3817 #if !(CONFIG_REALTIME_ONLY) && CONFIG_POSTPROC
3819 if (cpi->oxcf.noise_sensitivity > 0)
3824 switch (cpi->oxcf.noise_sensitivity)
3847 if (cm->frame_type == KEY_FRAME)
3849 vp8_de_noise(cpi->Source, cpi->Source, l , 1, 0, RTCD(postproc));
3853 vp8_de_noise(cpi->Source, cpi->Source, l , 1, 0, RTCD(postproc));
3855 src = cpi->Source->y_buffer;
3857 if (cpi->Source->y_stride < 0)
3859 src += cpi->Source->y_stride * (cpi->Source->y_height - 1);
3866 #ifdef OUTPUT_YUV_SRC
3867 vp8_write_yuv_frame(cpi->Source);
3872 vp8_clear_system_state(); //__asm emms;
3875 if(cpi->is_src_frame_alt_ref)
3879 vp8_set_quantizer(cpi, Q);
3882 // setup skip prob for costing in mode/mv decision
3883 if (cpi->common.mb_no_coeff_skip)
3885 cpi->prob_skip_false = cpi->base_skip_false_prob[Q];
3887 if (cm->frame_type != KEY_FRAME)
3889 if (cpi->common.refresh_alt_ref_frame)
3891 if (cpi->last_skip_false_probs[2] != 0)
3892 cpi->prob_skip_false = cpi->last_skip_false_probs[2];
3895 if(cpi->last_skip_false_probs[2]!=0 && abs(Q- cpi->last_skip_probs_q[2])<=16 )
3896 cpi->prob_skip_false = cpi->last_skip_false_probs[2];
3897 else if (cpi->last_skip_false_probs[2]!=0)
3898 cpi->prob_skip_false = (cpi->last_skip_false_probs[2] + cpi->prob_skip_false ) / 2;
3901 else if (cpi->common.refresh_golden_frame)
3903 if (cpi->last_skip_false_probs[1] != 0)
3904 cpi->prob_skip_false = cpi->last_skip_false_probs[1];
3907 if(cpi->last_skip_false_probs[1]!=0 && abs(Q- cpi->last_skip_probs_q[1])<=16 )
3908 cpi->prob_skip_false = cpi->last_skip_false_probs[1];
3909 else if (cpi->last_skip_false_probs[1]!=0)
3910 cpi->prob_skip_false = (cpi->last_skip_false_probs[1] + cpi->prob_skip_false ) / 2;
3915 if (cpi->last_skip_false_probs[0] != 0)
3916 cpi->prob_skip_false = cpi->last_skip_false_probs[0];
3919 if(cpi->last_skip_false_probs[0]!=0 && abs(Q- cpi->last_skip_probs_q[0])<=16 )
3920 cpi->prob_skip_false = cpi->last_skip_false_probs[0];
3921 else if(cpi->last_skip_false_probs[0]!=0)
3922 cpi->prob_skip_false = (cpi->last_skip_false_probs[0] + cpi->prob_skip_false ) / 2;
3926 //as this is for cost estimate, let's make sure it does not go extreme eitehr way
3927 if (cpi->prob_skip_false < 5)
3928 cpi->prob_skip_false = 5;
3930 if (cpi->prob_skip_false > 250)
3931 cpi->prob_skip_false = 250;
3933 if (cpi->oxcf.number_of_layers == 1 && cpi->is_src_frame_alt_ref)
3934 cpi->prob_skip_false = 1;
3941 FILE *f = fopen("skip.stt", "a");
3942 fprintf(f, "%d, %d, %4d ", cpi->common.refresh_golden_frame, cpi->common.refresh_alt_ref_frame, cpi->prob_skip_false);
3950 if (cm->frame_type == KEY_FRAME)
3952 resize_key_frame(cpi);
3953 vp8_setup_key_frame(cpi);
3956 #if CONFIG_MULTITHREAD
3957 /* wait for loopfilter thread done (for last picture)
3958 * don't do it for first frame, re-code and when PSNR is calculated
3960 if (cpi->b_multi_threaded && cm->current_video_frame && (!Loop) &&
3961 (!cpi->b_calculate_psnr))
3963 sem_wait(&cpi->h_event_end_lpf);
3967 // transform / motion compensation build reconstruction frame
3968 vp8_encode_frame(cpi);
3970 cpi->projected_frame_size -= vp8_estimate_entropy_savings(cpi);
3971 cpi->projected_frame_size = (cpi->projected_frame_size > 0) ? cpi->projected_frame_size : 0;
3973 vp8_clear_system_state(); //__asm emms;
3975 // Test to see if the stats generated for this frame indicate that we should have coded a key frame
3976 // (assuming that we didn't)!
3977 if (cpi->pass != 2 && cpi->oxcf.auto_key && cm->frame_type != KEY_FRAME)
3979 int key_frame_decision = decide_key_frame(cpi);
3981 if (cpi->compressor_speed == 2)
3983 /* we don't do re-encoding in realtime mode
3984 * if key frame is decided than we force it on next frame */
3985 cpi->force_next_frame_intra = key_frame_decision;
3987 else if (key_frame_decision)
3989 // Reset all our sizing numbers and recode
3990 cm->frame_type = KEY_FRAME;
3992 vp8_pick_frame_size(cpi);
3994 // Clear the Alt reference frame active flag when we have a key frame
3995 cpi->source_alt_ref_active = FALSE;
3997 // Reset the loop filter deltas and segmentation map
3998 setup_features(cpi);
4000 // If segmentation is enabled force a map update for key frames
4001 if (cpi->mb.e_mbd.segmentation_enabled)
4003 cpi->mb.e_mbd.update_mb_segmentation_map = 1;
4004 cpi->mb.e_mbd.update_mb_segmentation_data = 1;
4007 vp8_restore_coding_context(cpi);
4009 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4011 vp8_compute_frame_size_bounds(cpi, &frame_under_shoot_limit, &frame_over_shoot_limit);
4013 // Limit Q range for the adaptive loop.
4014 bottom_index = cpi->active_best_quality;
4015 top_index = cpi->active_worst_quality;
4016 q_low = cpi->active_best_quality;
4017 q_high = cpi->active_worst_quality;
4026 vp8_clear_system_state();
4028 if (frame_over_shoot_limit == 0)
4029 frame_over_shoot_limit = 1;
4031 // Are we are overshooting and up against the limit of active max Q.
4032 if (((cpi->pass != 2) || (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)) &&
4033 (Q == cpi->active_worst_quality) &&
4034 (cpi->active_worst_quality < cpi->worst_quality) &&
4035 (cpi->projected_frame_size > frame_over_shoot_limit))
4037 int over_size_percent = ((cpi->projected_frame_size - frame_over_shoot_limit) * 100) / frame_over_shoot_limit;
4039 // If so is there any scope for relaxing it
4040 while ((cpi->active_worst_quality < cpi->worst_quality) && (over_size_percent > 0))
4042 cpi->active_worst_quality++;
4043 top_index = cpi->active_worst_quality;
4044 over_size_percent = (int)(over_size_percent * 0.96); // Assume 1 qstep = about 4% on frame size.
4047 // If we have updated the active max Q do not call vp8_update_rate_correction_factors() this loop.
4048 active_worst_qchanged = TRUE;
4051 active_worst_qchanged = FALSE;
4053 #if !(CONFIG_REALTIME_ONLY)
4054 // Special case handling for forced key frames
4055 if ( (cm->frame_type == KEY_FRAME) && cpi->this_key_frame_forced )
4058 int kf_err = vp8_calc_ss_err(cpi->Source,
4059 &cm->yv12_fb[cm->new_fb_idx],
4060 IF_RTCD(&cpi->rtcd.variance));
4062 // The key frame is not good enough
4063 if ( kf_err > ((cpi->ambient_err * 7) >> 3) )
4066 q_high = (Q > q_low) ? (Q - 1) : q_low;
4069 Q = (q_high + q_low) >> 1;
4071 // The key frame is much better than the previous frame
4072 else if ( kf_err < (cpi->ambient_err >> 1) )
4075 q_low = (Q < q_high) ? (Q + 1) : q_high;
4078 Q = (q_high + q_low + 1) >> 1;
4081 // Clamp Q to upper and lower limits:
4087 Loop = ((Q != last_q)) ? TRUE : FALSE;
4090 // Is the projected frame size out of range and are we allowed to attempt to recode.
4091 else if ( recode_loop_test( cpi,
4092 frame_over_shoot_limit, frame_under_shoot_limit,
4093 Q, top_index, bottom_index ) )
4098 // Frame size out of permitted range:
4099 // Update correction factor & compute new Q to try...
4101 // Frame is too large
4102 if (cpi->projected_frame_size > cpi->this_frame_target)
4104 //if ( cpi->zbin_over_quant == 0 )
4105 q_low = (Q < q_high) ? (Q + 1) : q_high; // Raise Qlow as to at least the current value
4107 if (cpi->zbin_over_quant > 0) // If we are using over quant do the same for zbin_oq_low
4108 zbin_oq_low = (cpi->zbin_over_quant < zbin_oq_high) ? (cpi->zbin_over_quant + 1) : zbin_oq_high;
4110 //if ( undershoot_seen || (Q == MAXQ) )
4111 if (undershoot_seen)
4113 // Update rate_correction_factor unless cpi->active_worst_quality has changed.
4114 if (!active_worst_qchanged)
4115 vp8_update_rate_correction_factors(cpi, 1);
4117 Q = (q_high + q_low + 1) / 2;
4119 // Adjust cpi->zbin_over_quant (only allowed when Q is max)
4121 cpi->zbin_over_quant = 0;
4124 zbin_oq_low = (cpi->zbin_over_quant < zbin_oq_high) ? (cpi->zbin_over_quant + 1) : zbin_oq_high;
4125 cpi->zbin_over_quant = (zbin_oq_high + zbin_oq_low) / 2;
4130 // Update rate_correction_factor unless cpi->active_worst_quality has changed.
4131 if (!active_worst_qchanged)
4132 vp8_update_rate_correction_factors(cpi, 0);
4134 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4136 while (((Q < q_low) || (cpi->zbin_over_quant < zbin_oq_low)) && (Retries < 10))
4138 vp8_update_rate_correction_factors(cpi, 0);
4139 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4144 overshoot_seen = TRUE;
4146 // Frame is too small
4149 if (cpi->zbin_over_quant == 0)
4150 q_high = (Q > q_low) ? (Q - 1) : q_low; // Lower q_high if not using over quant
4151 else // else lower zbin_oq_high
4152 zbin_oq_high = (cpi->zbin_over_quant > zbin_oq_low) ? (cpi->zbin_over_quant - 1) : zbin_oq_low;
4156 // Update rate_correction_factor unless cpi->active_worst_quality has changed.
4157 if (!active_worst_qchanged)
4158 vp8_update_rate_correction_factors(cpi, 1);
4160 Q = (q_high + q_low) / 2;
4162 // Adjust cpi->zbin_over_quant (only allowed when Q is max)
4164 cpi->zbin_over_quant = 0;
4166 cpi->zbin_over_quant = (zbin_oq_high + zbin_oq_low) / 2;
4170 // Update rate_correction_factor unless cpi->active_worst_quality has changed.
4171 if (!active_worst_qchanged)
4172 vp8_update_rate_correction_factors(cpi, 0);
4174 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4176 // Special case reset for qlow for constrained quality.
4177 // This should only trigger where there is very substantial
4178 // undershoot on a frame and the auto cq level is above
4179 // the user passsed in value.
4180 if ( (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) &&
4186 while (((Q > q_high) || (cpi->zbin_over_quant > zbin_oq_high)) && (Retries < 10))
4188 vp8_update_rate_correction_factors(cpi, 0);
4189 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4194 undershoot_seen = TRUE;
4197 // Clamp Q to upper and lower limits:
4203 // Clamp cpi->zbin_over_quant
4204 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;
4206 //Loop = ((Q != last_q) || (last_zbin_oq != cpi->zbin_over_quant)) ? TRUE : FALSE;
4207 Loop = ((Q != last_q)) ? TRUE : FALSE;
4208 last_zbin_oq = cpi->zbin_over_quant;
4214 if (cpi->is_src_frame_alt_ref)
4219 vp8_restore_coding_context(cpi);
4221 #if CONFIG_INTERNAL_STATS
4222 cpi->tot_recode_hits++;
4226 while (Loop == TRUE);
4229 // Experimental code for lagged and one pass
4230 // Update stats used for one pass GF selection
4234 double frame_intra_error;
4235 double frame_coded_error;
4236 double frame_pcnt_inter;
4237 double frame_pcnt_motion;
4239 double frame_mvr_abs;
4241 double frame_mvc_abs;
4244 cpi->one_pass_frame_stats[cpi->one_pass_frame_index].frame_coded_error = (double)cpi->prediction_error;
4245 cpi->one_pass_frame_stats[cpi->one_pass_frame_index].frame_intra_error = (double)cpi->intra_error;
4246 cpi->one_pass_frame_stats[cpi->one_pass_frame_index].frame_pcnt_inter = (double)(100 - cpi->this_frame_percent_intra) / 100.0;
4250 // Special case code to reduce pulsing when key frames are forced at a
4251 // fixed interval. Note the reconstruction error if it is the frame before
4252 // the force key frame
4253 if ( cpi->next_key_frame_forced && (cpi->twopass.frames_to_key == 0) )
4255 cpi->ambient_err = vp8_calc_ss_err(cpi->Source,
4256 &cm->yv12_fb[cm->new_fb_idx],
4257 IF_RTCD(&cpi->rtcd.variance));
4260 /* This frame's MVs are saved and will be used in next frame's MV predictor.
4261 * Last frame has one more line(add to bottom) and one more column(add to
4262 * right) than cm->mip. The edge elements are initialized to 0.
4264 #if CONFIG_MULTI_RES_ENCODING
4265 if(!cpi->oxcf.mr_encoder_id && cm->show_frame)
4267 if(cm->show_frame) /* do not save for altref frame */
4272 /* Point to beginning of allocated MODE_INFO arrays. */
4273 MODE_INFO *tmp = cm->mip;
4275 if(cm->frame_type != KEY_FRAME)
4277 for (mb_row = 0; mb_row < cm->mb_rows+1; mb_row ++)
4279 for (mb_col = 0; mb_col < cm->mb_cols+1; mb_col ++)
4281 if(tmp->mbmi.ref_frame != INTRA_FRAME)
4282 cpi->lfmv[mb_col + mb_row*(cm->mode_info_stride+1)].as_int = tmp->mbmi.mv.as_int;
4284 cpi->lf_ref_frame_sign_bias[mb_col + mb_row*(cm->mode_info_stride+1)] = cm->ref_frame_sign_bias[tmp->mbmi.ref_frame];
4285 cpi->lf_ref_frame[mb_col + mb_row*(cm->mode_info_stride+1)] = tmp->mbmi.ref_frame;
4292 #if CONFIG_MULTI_RES_ENCODING
4293 vp8_cal_dissimilarity(cpi);
4296 // Update the GF useage maps.
4297 // This is done after completing the compression of a frame when all
4298 // modes etc. are finalized but before loop filter
4299 if (cpi->oxcf.number_of_layers == 1)
4300 vp8_update_gf_useage_maps(cpi, cm, &cpi->mb);
4302 if (cm->frame_type == KEY_FRAME)
4303 cm->refresh_last_frame = 1;
4307 FILE *f = fopen("gfactive.stt", "a");
4308 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);
4313 // For inter frames the current default behavior is that when
4314 // cm->refresh_golden_frame is set we copy the old GF over to the ARF buffer
4315 // This is purely an encoder decision at present.
4316 if (!cpi->oxcf.error_resilient_mode && cm->refresh_golden_frame)
4317 cm->copy_buffer_to_arf = 2;
4319 cm->copy_buffer_to_arf = 0;
4321 cm->frame_to_show = &cm->yv12_fb[cm->new_fb_idx];
4323 #if CONFIG_MULTITHREAD
4324 if (cpi->b_multi_threaded)
4326 sem_post(&cpi->h_event_start_lpf); /* start loopfilter in separate thread */
4331 loopfilter_frame(cpi, cm);
4334 update_reference_frames(cm);
4336 if (cpi->oxcf.error_resilient_mode)
4338 cm->refresh_entropy_probs = 0;
4341 #if CONFIG_MULTITHREAD
4342 /* wait that filter_level is picked so that we can continue with stream packing */
4343 if (cpi->b_multi_threaded)
4344 sem_wait(&cpi->h_event_end_lpf);
4347 // build the bitstream
4348 vp8_pack_bitstream(cpi, dest, dest_end, size);
4350 #if CONFIG_MULTITHREAD
4351 /* if PSNR packets are generated we have to wait for the lpf */
4352 if (cpi->b_multi_threaded && cpi->b_calculate_psnr)
4354 sem_wait(&cpi->h_event_end_lpf);
4358 /* Move storing frame_type out of the above loop since it is also
4359 * needed in motion search besides loopfilter */
4360 cm->last_frame_type = cm->frame_type;
4362 // Update rate control heuristics
4363 cpi->total_byte_count += (*size);
4364 cpi->projected_frame_size = (*size) << 3;
4366 if (cpi->oxcf.number_of_layers > 1)
4369 for (i=cpi->current_layer+1; i<cpi->oxcf.number_of_layers; i++)
4370 cpi->layer_context[i].total_byte_count += (*size);
4373 if (!active_worst_qchanged)
4374 vp8_update_rate_correction_factors(cpi, 2);
4376 cpi->last_q[cm->frame_type] = cm->base_qindex;
4378 if (cm->frame_type == KEY_FRAME)
4380 vp8_adjust_key_frame_context(cpi);
4383 // Keep a record of ambient average Q.
4384 if (cm->frame_type != KEY_FRAME)
4385 cpi->avg_frame_qindex = (2 + 3 * cpi->avg_frame_qindex + cm->base_qindex) >> 2;
4387 // Keep a record from which we can calculate the average Q excluding GF updates and key frames
4388 if ((cm->frame_type != KEY_FRAME) && ((cpi->oxcf.number_of_layers > 1) ||
4389 (!cm->refresh_golden_frame && !cm->refresh_alt_ref_frame)))
4393 // Calculate the average Q for normal inter frames (not key or GFU
4395 if ( cpi->pass == 2 )
4397 cpi->ni_tot_qi += Q;
4398 cpi->ni_av_qi = (cpi->ni_tot_qi / cpi->ni_frames);
4402 // Damp value for first few frames
4403 if (cpi->ni_frames > 150 )
4405 cpi->ni_tot_qi += Q;
4406 cpi->ni_av_qi = (cpi->ni_tot_qi / cpi->ni_frames);
4408 // For one pass, early in the clip ... average the current frame Q
4409 // value with the worstq entered by the user as a dampening measure
4412 cpi->ni_tot_qi += Q;
4413 cpi->ni_av_qi = ((cpi->ni_tot_qi / cpi->ni_frames) + cpi->worst_quality + 1) / 2;
4416 // If the average Q is higher than what was used in the last frame
4417 // (after going through the recode loop to keep the frame size within range)
4418 // then use the last frame value - 1.
4419 // The -1 is designed to stop Q and hence the data rate, from progressively
4420 // falling away during difficult sections, but at the same time reduce the number of
4421 // itterations around the recode loop.
4422 if (Q > cpi->ni_av_qi)
4423 cpi->ni_av_qi = Q - 1;
4429 // If the frame was massively oversize and we are below optimal buffer level drop next frame
4430 if ((cpi->drop_frames_allowed) &&
4431 (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) &&
4432 (cpi->buffer_level < cpi->oxcf.drop_frames_water_mark * cpi->oxcf.optimal_buffer_level / 100) &&
4433 (cpi->projected_frame_size > (4 * cpi->this_frame_target)))
4435 cpi->drop_frame = TRUE;
4440 // Set the count for maximum consecutive dropped frames based upon the ratio of
4441 // this frame size to the target average per frame bandwidth.
4442 // (cpi->av_per_frame_bandwidth > 0) is just a sanity check to prevent / 0.
4443 if (cpi->drop_frames_allowed && (cpi->av_per_frame_bandwidth > 0))
4445 cpi->max_drop_count = cpi->projected_frame_size / cpi->av_per_frame_bandwidth;
4447 if (cpi->max_drop_count > cpi->max_consec_dropped_frames)
4448 cpi->max_drop_count = cpi->max_consec_dropped_frames;
4451 // Update the buffer level variable.
4452 // Non-viewable frames are a special case and are treated as pure overhead.
4453 if ( !cm->show_frame )
4454 cpi->bits_off_target -= cpi->projected_frame_size;
4456 cpi->bits_off_target += cpi->av_per_frame_bandwidth - cpi->projected_frame_size;
4458 // Clip the buffer level to the maximum specified buffer size
4459 if (cpi->bits_off_target > cpi->oxcf.maximum_buffer_size)
4460 cpi->bits_off_target = cpi->oxcf.maximum_buffer_size;
4462 // Rolling monitors of whether we are over or underspending used to help regulate min and Max Q in two pass.
4463 cpi->rolling_target_bits = ((cpi->rolling_target_bits * 3) + cpi->this_frame_target + 2) / 4;
4464 cpi->rolling_actual_bits = ((cpi->rolling_actual_bits * 3) + cpi->projected_frame_size + 2) / 4;
4465 cpi->long_rolling_target_bits = ((cpi->long_rolling_target_bits * 31) + cpi->this_frame_target + 16) / 32;
4466 cpi->long_rolling_actual_bits = ((cpi->long_rolling_actual_bits * 31) + cpi->projected_frame_size + 16) / 32;
4468 // Actual bits spent
4469 cpi->total_actual_bits += cpi->projected_frame_size;
4472 cpi->total_target_vs_actual += (cpi->this_frame_target - cpi->projected_frame_size);
4474 cpi->buffer_level = cpi->bits_off_target;
4476 // Propagate values to higher temporal layers
4477 if (cpi->oxcf.number_of_layers > 1)
4481 for (i=cpi->current_layer+1; i<cpi->oxcf.number_of_layers; i++)
4483 LAYER_CONTEXT *lc = &cpi->layer_context[i];
4484 int bits_off_for_this_layer = lc->target_bandwidth / lc->frame_rate
4485 - cpi->projected_frame_size;
4487 lc->bits_off_target += bits_off_for_this_layer;
4489 // Clip buffer level to maximum buffer size for the layer
4490 if (lc->bits_off_target > lc->maximum_buffer_size)
4491 lc->bits_off_target = lc->maximum_buffer_size;
4493 lc->total_actual_bits += cpi->projected_frame_size;
4494 lc->total_target_vs_actual += bits_off_for_this_layer;
4495 lc->buffer_level = lc->bits_off_target;
4499 // Update bits left to the kf and gf groups to account for overshoot or undershoot on these frames
4500 if (cm->frame_type == KEY_FRAME)
4502 cpi->twopass.kf_group_bits += cpi->this_frame_target - cpi->projected_frame_size;
4504 if (cpi->twopass.kf_group_bits < 0)
4505 cpi->twopass.kf_group_bits = 0 ;
4507 else if (cm->refresh_golden_frame || cm->refresh_alt_ref_frame)
4509 cpi->twopass.gf_group_bits += cpi->this_frame_target - cpi->projected_frame_size;
4511 if (cpi->twopass.gf_group_bits < 0)
4512 cpi->twopass.gf_group_bits = 0 ;
4515 if (cm->frame_type != KEY_FRAME)
4517 if (cpi->common.refresh_alt_ref_frame)
4519 cpi->last_skip_false_probs[2] = cpi->prob_skip_false;
4520 cpi->last_skip_probs_q[2] = cm->base_qindex;
4522 else if (cpi->common.refresh_golden_frame)
4524 cpi->last_skip_false_probs[1] = cpi->prob_skip_false;
4525 cpi->last_skip_probs_q[1] = cm->base_qindex;
4529 cpi->last_skip_false_probs[0] = cpi->prob_skip_false;
4530 cpi->last_skip_probs_q[0] = cm->base_qindex;
4532 //update the baseline
4533 cpi->base_skip_false_prob[cm->base_qindex] = cpi->prob_skip_false;
4538 #if 0 && CONFIG_INTERNAL_STATS
4540 FILE *f = fopen("tmp.stt", "a");
4542 vp8_clear_system_state(); //__asm emms;
4544 if (cpi->twopass.total_left_stats->coded_error != 0.0)
4545 fprintf(f, "%10d %10d %10d %10d %10d %10d %10d %10d %6d %6d"
4546 "%6d %6d %6d %5d %5d %5d %8d %8.2f %10d %10.3f"
4548 cpi->common.current_video_frame, cpi->this_frame_target,
4549 cpi->projected_frame_size,
4550 (cpi->projected_frame_size - cpi->this_frame_target),
4551 (int)cpi->total_target_vs_actual,
4552 (cpi->oxcf.starting_buffer_level-cpi->bits_off_target),
4553 (int)cpi->total_actual_bits, cm->base_qindex,
4554 cpi->active_best_quality, cpi->active_worst_quality,
4555 cpi->ni_av_qi, cpi->cq_target_quality,
4556 cpi->zbin_over_quant,
4557 //cpi->avg_frame_qindex, cpi->zbin_over_quant,
4558 cm->refresh_golden_frame, cm->refresh_alt_ref_frame,
4559 cm->frame_type, cpi->gfu_boost,
4560 cpi->twopass.est_max_qcorrection_factor,
4561 (int)cpi->twopass.bits_left,
4562 cpi->twopass.total_left_stats->coded_error,
4563 (double)cpi->twopass.bits_left /
4564 cpi->twopass.total_left_stats->coded_error,
4565 cpi->tot_recode_hits);
4567 fprintf(f, "%10d %10d %10d %10d %10d %10d %10d %10d %6d %6d"
4568 "%6d %6d %6d %5d %5d %5d %8d %8.2f %10d %10.3f"
4570 cpi->common.current_video_frame,
4571 cpi->this_frame_target, cpi->projected_frame_size,
4572 (cpi->projected_frame_size - cpi->this_frame_target),
4573 (int)cpi->total_target_vs_actual,
4574 (cpi->oxcf.starting_buffer_level-cpi->bits_off_target),
4575 (int)cpi->total_actual_bits, cm->base_qindex,
4576 cpi->active_best_quality, cpi->active_worst_quality,
4577 cpi->ni_av_qi, cpi->cq_target_quality,
4578 cpi->zbin_over_quant,
4579 //cpi->avg_frame_qindex, cpi->zbin_over_quant,
4580 cm->refresh_golden_frame, cm->refresh_alt_ref_frame,
4581 cm->frame_type, cpi->gfu_boost,
4582 cpi->twopass.est_max_qcorrection_factor,
4583 (int)cpi->twopass.bits_left,
4584 cpi->twopass.total_left_stats->coded_error,
4585 cpi->tot_recode_hits);
4590 FILE *fmodes = fopen("Modes.stt", "a");
4593 fprintf(fmodes, "%6d:%1d:%1d:%1d ",
4594 cpi->common.current_video_frame,
4595 cm->frame_type, cm->refresh_golden_frame,
4596 cm->refresh_alt_ref_frame);
4598 for (i = 0; i < MAX_MODES; i++)
4599 fprintf(fmodes, "%5d ", cpi->mode_chosen_counts[i]);
4601 fprintf(fmodes, "\n");
4609 // If this was a kf or Gf note the Q
4610 if ((cm->frame_type == KEY_FRAME) || cm->refresh_golden_frame || cm->refresh_alt_ref_frame)
4611 cm->last_kf_gf_q = cm->base_qindex;
4613 if (cm->refresh_golden_frame == 1)
4614 cm->frame_flags = cm->frame_flags | FRAMEFLAGS_GOLDEN;
4616 cm->frame_flags = cm->frame_flags&~FRAMEFLAGS_GOLDEN;
4618 if (cm->refresh_alt_ref_frame == 1)
4619 cm->frame_flags = cm->frame_flags | FRAMEFLAGS_ALTREF;
4621 cm->frame_flags = cm->frame_flags&~FRAMEFLAGS_ALTREF;
4624 if (cm->refresh_last_frame & cm->refresh_golden_frame) // both refreshed
4625 cpi->gold_is_last = 1;
4626 else if (cm->refresh_last_frame ^ cm->refresh_golden_frame) // 1 refreshed but not the other
4627 cpi->gold_is_last = 0;
4629 if (cm->refresh_last_frame & cm->refresh_alt_ref_frame) // both refreshed
4630 cpi->alt_is_last = 1;
4631 else if (cm->refresh_last_frame ^ cm->refresh_alt_ref_frame) // 1 refreshed but not the other
4632 cpi->alt_is_last = 0;
4634 if (cm->refresh_alt_ref_frame & cm->refresh_golden_frame) // both refreshed
4635 cpi->gold_is_alt = 1;
4636 else if (cm->refresh_alt_ref_frame ^ cm->refresh_golden_frame) // 1 refreshed but not the other
4637 cpi->gold_is_alt = 0;
4639 cpi->ref_frame_flags = VP8_ALT_FLAG | VP8_GOLD_FLAG | VP8_LAST_FLAG;
4641 if (cpi->gold_is_last)
4642 cpi->ref_frame_flags &= ~VP8_GOLD_FLAG;
4644 if (cpi->alt_is_last)
4645 cpi->ref_frame_flags &= ~VP8_ALT_FLAG;
4647 if (cpi->gold_is_alt)
4648 cpi->ref_frame_flags &= ~VP8_ALT_FLAG;
4651 if (!cpi->oxcf.error_resilient_mode)
4653 if (cpi->oxcf.play_alternate && cm->refresh_alt_ref_frame && (cm->frame_type != KEY_FRAME))
4654 // Update the alternate reference frame stats as appropriate.
4655 update_alt_ref_frame_stats(cpi);
4657 // Update the Golden frame stats as appropriate.
4658 update_golden_frame_stats(cpi);
4661 if (cm->frame_type == KEY_FRAME)
4663 // Tell the caller that the frame was coded as a key frame
4664 *frame_flags = cm->frame_flags | FRAMEFLAGS_KEY;
4666 // As this frame is a key frame the next defaults to an inter frame.
4667 cm->frame_type = INTER_FRAME;
4669 cpi->last_frame_percent_intra = 100;
4673 *frame_flags = cm->frame_flags&~FRAMEFLAGS_KEY;
4675 cpi->last_frame_percent_intra = cpi->this_frame_percent_intra;
4678 // Clear the one shot update flags for segmentation map and mode/ref loop filter deltas.
4679 cpi->mb.e_mbd.update_mb_segmentation_map = 0;
4680 cpi->mb.e_mbd.update_mb_segmentation_data = 0;
4681 cpi->mb.e_mbd.mode_ref_lf_delta_update = 0;
4684 // Dont increment frame counters if this was an altref buffer update not a real frame
4687 cm->current_video_frame++;
4688 cpi->frames_since_key++;
4691 // reset to normal state now that we are done.
4699 sprintf(filename, "enc%04d.yuv", (int) cm->current_video_frame);
4700 recon_file = fopen(filename, "wb");
4701 fwrite(cm->yv12_fb[cm->lst_fb_idx].buffer_alloc,
4702 cm->yv12_fb[cm->lst_fb_idx].frame_size, 1, recon_file);
4708 //vp8_write_yuv_frame("encoder_recon.yuv", cm->frame_to_show);
4714 static void check_gf_quality(VP8_COMP *cpi)
4716 VP8_COMMON *cm = &cpi->common;
4717 int gf_active_pct = (100 * cpi->gf_active_count) / (cm->mb_rows * cm->mb_cols);
4718 int gf_ref_usage_pct = (cpi->count_mb_ref_frame_usage[GOLDEN_FRAME] * 100) / (cm->mb_rows * cm->mb_cols);
4719 int last_ref_zz_useage = (cpi->inter_zz_count * 100) / (cm->mb_rows * cm->mb_cols);
4721 // Gf refresh is not currently being signalled
4722 if (cpi->gf_update_recommended == 0)
4724 if (cpi->common.frames_since_golden > 7)
4727 if ((gf_active_pct < 10) || ((gf_active_pct + gf_ref_usage_pct) < 15))
4729 // ...but last frame zero zero usage is reasonbable so a new gf might be appropriate
4730 if (last_ref_zz_useage >= 25)
4732 cpi->gf_bad_count ++;
4734 if (cpi->gf_bad_count >= 8) // Check that the condition is stable
4736 cpi->gf_update_recommended = 1;
4737 cpi->gf_bad_count = 0;
4741 cpi->gf_bad_count = 0; // Restart count as the background is not stable enough
4744 cpi->gf_bad_count = 0; // Gf useage has picked up so reset count
4747 // If the signal is set but has not been read should we cancel it.
4748 else if (last_ref_zz_useage < 15)
4750 cpi->gf_update_recommended = 0;
4751 cpi->gf_bad_count = 0;
4756 FILE *f = fopen("gfneeded.stt", "a");
4757 fprintf(f, "%10d %10d %10d %10d %10ld \n",
4758 cm->current_video_frame,
4759 cpi->common.frames_since_golden,
4760 gf_active_pct, gf_ref_usage_pct,
4761 cpi->gf_update_recommended);
4768 #if !(CONFIG_REALTIME_ONLY)
4769 static void Pass2Encode(VP8_COMP *cpi, unsigned long *size, unsigned char *dest, unsigned char * dest_end, unsigned int *frame_flags)
4772 if (!cpi->common.refresh_alt_ref_frame)
4773 vp8_second_pass(cpi);
4775 encode_frame_to_data_rate(cpi, size, dest, dest_end, frame_flags);
4776 cpi->twopass.bits_left -= 8 * *size;
4778 if (!cpi->common.refresh_alt_ref_frame)
4780 double two_pass_min_rate = (double)(cpi->oxcf.target_bandwidth
4781 *cpi->oxcf.two_pass_vbrmin_section / 100);
4782 cpi->twopass.bits_left += (int64_t)(two_pass_min_rate / cpi->frame_rate);
4787 //For ARM NEON, d8-d15 are callee-saved registers, and need to be saved by us.
4789 extern void vp8_push_neon(int64_t *store);
4790 extern void vp8_pop_neon(int64_t *store);
4794 int vp8_receive_raw_frame(VP8_PTR ptr, unsigned int frame_flags, YV12_BUFFER_CONFIG *sd, int64_t time_stamp, int64_t end_time)
4797 int64_t store_reg[8];
4799 VP8_COMP *cpi = (VP8_COMP *) ptr;
4800 VP8_COMMON *cm = &cpi->common;
4801 struct vpx_usec_timer timer;
4805 #if CONFIG_RUNTIME_CPU_DETECT
4806 if (cm->rtcd.flags & HAS_NEON)
4809 vp8_push_neon(store_reg);
4813 vpx_usec_timer_start(&timer);
4814 if(vp8_lookahead_push(cpi->lookahead, sd, time_stamp, end_time,
4815 frame_flags, cpi->active_map_enabled ? cpi->active_map : NULL))
4817 cm->clr_type = sd->clrtype;
4818 vpx_usec_timer_mark(&timer);
4819 cpi->time_receive_data += vpx_usec_timer_elapsed(&timer);
4822 #if CONFIG_RUNTIME_CPU_DETECT
4823 if (cm->rtcd.flags & HAS_NEON)
4826 vp8_pop_neon(store_reg);
4834 static int frame_is_reference(const VP8_COMP *cpi)
4836 const VP8_COMMON *cm = &cpi->common;
4837 const MACROBLOCKD *xd = &cpi->mb.e_mbd;
4839 return cm->frame_type == KEY_FRAME || cm->refresh_last_frame
4840 || cm->refresh_golden_frame || cm->refresh_alt_ref_frame
4841 || cm->copy_buffer_to_gf || cm->copy_buffer_to_arf
4842 || cm->refresh_entropy_probs
4843 || xd->mode_ref_lf_delta_update
4844 || xd->update_mb_segmentation_map || xd->update_mb_segmentation_data;
4848 int vp8_get_compressed_data(VP8_PTR ptr, unsigned int *frame_flags, unsigned long *size, unsigned char *dest, unsigned char *dest_end, int64_t *time_stamp, int64_t *time_end, int flush)
4851 int64_t store_reg[8];
4853 VP8_COMP *cpi = (VP8_COMP *) ptr;
4855 struct vpx_usec_timer tsctimer;
4856 struct vpx_usec_timer ticktimer;
4857 struct vpx_usec_timer cmptimer;
4858 YV12_BUFFER_CONFIG *force_src_buffer = NULL;
4865 if (setjmp(cpi->common.error.jmp))
4867 cpi->common.error.setjmp = 0;
4868 return VPX_CODEC_CORRUPT_FRAME;
4871 cpi->common.error.setjmp = 1;
4874 #if CONFIG_RUNTIME_CPU_DETECT
4875 if (cm->rtcd.flags & HAS_NEON)
4878 vp8_push_neon(store_reg);
4882 vpx_usec_timer_start(&cmptimer);
4886 #if !(CONFIG_REALTIME_ONLY)
4887 // Should we code an alternate reference frame
4888 if (cpi->oxcf.error_resilient_mode == 0 &&
4889 cpi->oxcf.play_alternate &&
4890 cpi->source_alt_ref_pending)
4892 if ((cpi->source = vp8_lookahead_peek(cpi->lookahead,
4893 cpi->frames_till_gf_update_due)))
4895 cpi->alt_ref_source = cpi->source;
4896 if (cpi->oxcf.arnr_max_frames > 0)
4898 vp8_temporal_filter_prepare_c(cpi,
4899 cpi->frames_till_gf_update_due);
4900 force_src_buffer = &cpi->alt_ref_buffer;
4902 cm->frames_till_alt_ref_frame = cpi->frames_till_gf_update_due;
4903 cm->refresh_alt_ref_frame = 1;
4904 cm->refresh_golden_frame = 0;
4905 cm->refresh_last_frame = 0;
4907 cpi->source_alt_ref_pending = FALSE; // Clear Pending alt Ref flag.
4908 cpi->is_src_frame_alt_ref = 0;
4915 if ((cpi->source = vp8_lookahead_pop(cpi->lookahead, flush)))
4919 cpi->is_src_frame_alt_ref = cpi->alt_ref_source
4920 && (cpi->source == cpi->alt_ref_source);
4922 if(cpi->is_src_frame_alt_ref)
4923 cpi->alt_ref_source = NULL;
4929 cpi->un_scaled_source =
4930 cpi->Source = force_src_buffer ? force_src_buffer : &cpi->source->img;
4931 *time_stamp = cpi->source->ts_start;
4932 *time_end = cpi->source->ts_end;
4933 *frame_flags = cpi->source->flags;
4938 #if !(CONFIG_REALTIME_ONLY)
4940 if (flush && cpi->pass == 1 && !cpi->twopass.first_pass_done)
4942 vp8_end_first_pass(cpi); /* get last stats packet */
4943 cpi->twopass.first_pass_done = 1;
4949 #if CONFIG_RUNTIME_CPU_DETECT
4950 if (cm->rtcd.flags & HAS_NEON)
4953 vp8_pop_neon(store_reg);
4959 // Restore layer specific context if necessary
4960 if (cpi->oxcf.number_of_layers > 1)
4962 restore_layer_context (cpi,
4963 cpi->oxcf.layer_id[cm->current_video_frame % cpi->oxcf.periodicity]);
4966 if (cpi->source->ts_start < cpi->first_time_stamp_ever)
4968 cpi->first_time_stamp_ever = cpi->source->ts_start;
4969 cpi->last_end_time_stamp_seen = cpi->source->ts_start;
4972 // adjust frame rates based on timestamps given
4973 if (cpi->oxcf.number_of_layers > 1 )
4975 vp8_new_frame_rate (
4976 cpi, cpi->layer_context[cpi->current_layer].frame_rate);
4978 cpi->last_time_stamp_seen = cpi->source->ts_start;
4979 cpi->last_end_time_stamp_seen = cpi->source->ts_end;
4982 else if (!cm->refresh_alt_ref_frame)
4984 int64_t this_duration;
4987 if (cpi->source->ts_start == cpi->first_time_stamp_ever)
4989 this_duration = cpi->source->ts_end - cpi->source->ts_start;
4994 int64_t last_duration;
4996 this_duration = cpi->source->ts_end - cpi->last_end_time_stamp_seen;
4997 last_duration = cpi->last_end_time_stamp_seen
4998 - cpi->last_time_stamp_seen;
4999 // do a step update if the duration changes by 10%
5001 step = ((this_duration - last_duration) * 10 / last_duration);
5007 vp8_new_frame_rate(cpi, 10000000.0 / this_duration);
5010 double avg_duration, interval;
5012 /* Average this frame's rate into the last second's average
5013 * frame rate. If we haven't seen 1 second yet, then average
5014 * over the whole interval seen.
5016 interval = cpi->source->ts_end - cpi->first_time_stamp_ever;
5017 if(interval > 10000000.0)
5018 interval = 10000000;
5020 avg_duration = 10000000.0 / cpi->frame_rate;
5021 avg_duration *= (interval - avg_duration + this_duration);
5022 avg_duration /= interval;
5024 vp8_new_frame_rate(cpi, 10000000.0 / avg_duration);
5028 cpi->last_time_stamp_seen = cpi->source->ts_start;
5029 cpi->last_end_time_stamp_seen = cpi->source->ts_end;
5032 if (cpi->compressor_speed == 2)
5034 if (cpi->oxcf.number_of_layers == 1)
5035 check_gf_quality(cpi);
5036 vpx_usec_timer_start(&tsctimer);
5037 vpx_usec_timer_start(&ticktimer);
5040 // start with a 0 size frame
5043 // Clear down mmx registers
5044 vp8_clear_system_state(); //__asm emms;
5046 cm->frame_type = INTER_FRAME;
5047 cm->frame_flags = *frame_flags;
5051 if (cm->refresh_alt_ref_frame)
5053 //cm->refresh_golden_frame = 1;
5054 cm->refresh_golden_frame = 0;
5055 cm->refresh_last_frame = 0;
5059 cm->refresh_golden_frame = 0;
5060 cm->refresh_last_frame = 1;
5064 /* find a free buffer for the new frame */
5067 for(; i < NUM_YV12_BUFFERS; i++)
5069 if(!cm->yv12_fb[i].flags)
5076 assert(i < NUM_YV12_BUFFERS );
5078 #if !(CONFIG_REALTIME_ONLY)
5082 Pass1Encode(cpi, size, dest, frame_flags);
5084 else if (cpi->pass == 2)
5086 Pass2Encode(cpi, size, dest, dest_end, frame_flags);
5090 encode_frame_to_data_rate(cpi, size, dest, dest_end, frame_flags);
5092 if (cpi->compressor_speed == 2)
5094 unsigned int duration, duration2;
5095 vpx_usec_timer_mark(&tsctimer);
5096 vpx_usec_timer_mark(&ticktimer);
5098 duration = vpx_usec_timer_elapsed(&ticktimer);
5099 duration2 = (unsigned int)((double)duration / 2);
5101 if (cm->frame_type != KEY_FRAME)
5103 if (cpi->avg_encode_time == 0)
5104 cpi->avg_encode_time = duration;
5106 cpi->avg_encode_time = (7 * cpi->avg_encode_time + duration) >> 3;
5111 //if(*frame_flags!=1)
5114 if (cpi->avg_pick_mode_time == 0)
5115 cpi->avg_pick_mode_time = duration2;
5117 cpi->avg_pick_mode_time = (7 * cpi->avg_pick_mode_time + duration2) >> 3;
5123 if (cm->refresh_entropy_probs == 0)
5125 vpx_memcpy(&cm->fc, &cm->lfc, sizeof(cm->fc));
5128 // Save the contexts separately for alt ref, gold and last.
5129 // (TODO jbb -> Optimize this with pointers to avoid extra copies. )
5130 if(cm->refresh_alt_ref_frame)
5131 vpx_memcpy(&cpi->lfc_a, &cm->fc, sizeof(cm->fc));
5133 if(cm->refresh_golden_frame)
5134 vpx_memcpy(&cpi->lfc_g, &cm->fc, sizeof(cm->fc));
5136 if(cm->refresh_last_frame)
5137 vpx_memcpy(&cpi->lfc_n, &cm->fc, sizeof(cm->fc));
5139 // if its a dropped frame honor the requests on subsequent frames
5142 cpi->droppable = !frame_is_reference(cpi);
5144 // return to normal state
5145 cm->refresh_entropy_probs = 1;
5146 cm->refresh_alt_ref_frame = 0;
5147 cm->refresh_golden_frame = 0;
5148 cm->refresh_last_frame = 1;
5149 cm->frame_type = INTER_FRAME;
5153 // Save layer specific state
5154 if (cpi->oxcf.number_of_layers > 1)
5155 save_layer_context (cpi);
5157 vpx_usec_timer_mark(&cmptimer);
5158 cpi->time_compress_data += vpx_usec_timer_elapsed(&cmptimer);
5160 if (cpi->b_calculate_psnr && cpi->pass != 1 && cm->show_frame)
5162 generate_psnr_packet(cpi);
5165 #if CONFIG_INTERNAL_STATS
5169 cpi->bytes += *size;
5176 if (cpi->b_calculate_psnr)
5180 YV12_BUFFER_CONFIG *orig = cpi->Source;
5181 YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
5182 YV12_BUFFER_CONFIG *pp = &cm->post_proc_buffer;
5183 int y_samples = orig->y_height * orig->y_width ;
5184 int uv_samples = orig->uv_height * orig->uv_width ;
5185 int t_samples = y_samples + 2 * uv_samples;
5186 int64_t sq_error, sq_error2;
5188 ye = calc_plane_error(orig->y_buffer, orig->y_stride,
5189 recon->y_buffer, recon->y_stride, orig->y_width, orig->y_height,
5190 IF_RTCD(&cpi->rtcd.variance));
5192 ue = calc_plane_error(orig->u_buffer, orig->uv_stride,
5193 recon->u_buffer, recon->uv_stride, orig->uv_width, orig->uv_height,
5194 IF_RTCD(&cpi->rtcd.variance));
5196 ve = calc_plane_error(orig->v_buffer, orig->uv_stride,
5197 recon->v_buffer, recon->uv_stride, orig->uv_width, orig->uv_height,
5198 IF_RTCD(&cpi->rtcd.variance));
5200 sq_error = ye + ue + ve;
5202 frame_psnr = vp8_mse2psnr(t_samples, 255.0, sq_error);
5204 cpi->total_y += vp8_mse2psnr(y_samples, 255.0, ye);
5205 cpi->total_u += vp8_mse2psnr(uv_samples, 255.0, ue);
5206 cpi->total_v += vp8_mse2psnr(uv_samples, 255.0, ve);
5207 cpi->total_sq_error += sq_error;
5208 cpi->total += frame_psnr;
5210 double frame_psnr2, frame_ssim2 = 0;
5213 vp8_deblock(cm->frame_to_show, &cm->post_proc_buffer, cm->filter_level * 10 / 6, 1, 0, IF_RTCD(&cm->rtcd.postproc));
5214 vp8_clear_system_state();
5216 ye = calc_plane_error(orig->y_buffer, orig->y_stride,
5217 pp->y_buffer, pp->y_stride, orig->y_width, orig->y_height,
5218 IF_RTCD(&cpi->rtcd.variance));
5220 ue = calc_plane_error(orig->u_buffer, orig->uv_stride,
5221 pp->u_buffer, pp->uv_stride, orig->uv_width, orig->uv_height,
5222 IF_RTCD(&cpi->rtcd.variance));
5224 ve = calc_plane_error(orig->v_buffer, orig->uv_stride,
5225 pp->v_buffer, pp->uv_stride, orig->uv_width, orig->uv_height,
5226 IF_RTCD(&cpi->rtcd.variance));
5228 sq_error2 = ye + ue + ve;
5230 frame_psnr2 = vp8_mse2psnr(t_samples, 255.0, sq_error2);
5232 cpi->totalp_y += vp8_mse2psnr(y_samples, 255.0, ye);
5233 cpi->totalp_u += vp8_mse2psnr(uv_samples, 255.0, ue);
5234 cpi->totalp_v += vp8_mse2psnr(uv_samples, 255.0, ve);
5235 cpi->total_sq_error2 += sq_error2;
5236 cpi->totalp += frame_psnr2;
5238 frame_ssim2 = vp8_calc_ssim(cpi->Source,
5239 &cm->post_proc_buffer, 1, &weight,
5240 IF_RTCD(&cpi->rtcd.variance));
5242 cpi->summed_quality += frame_ssim2 * weight;
5243 cpi->summed_weights += weight;
5245 if (cpi->oxcf.number_of_layers > 1)
5249 for (i=cpi->current_layer;
5250 i<cpi->oxcf.number_of_layers; i++)
5252 cpi->frames_in_layer[i]++;
5254 cpi->bytes_in_layer[i] += *size;
5255 cpi->sum_psnr[i] += frame_psnr;
5256 cpi->sum_psnr_p[i] += frame_psnr2;
5257 cpi->total_error2[i] += sq_error;
5258 cpi->total_error2_p[i] += sq_error2;
5259 cpi->sum_ssim[i] += frame_ssim2 * weight;
5260 cpi->sum_weights[i] += weight;
5266 if (cpi->b_calculate_ssimg)
5268 double y, u, v, frame_all;
5269 frame_all = vp8_calc_ssimg(cpi->Source, cm->frame_to_show,
5270 &y, &u, &v, IF_RTCD(&cpi->rtcd.variance));
5272 if (cpi->oxcf.number_of_layers > 1)
5276 for (i=cpi->current_layer;
5277 i<cpi->oxcf.number_of_layers; i++)
5279 if (!cpi->b_calculate_psnr)
5280 cpi->frames_in_layer[i]++;
5282 cpi->total_ssimg_y_in_layer[i] += y;
5283 cpi->total_ssimg_u_in_layer[i] += u;
5284 cpi->total_ssimg_v_in_layer[i] += v;
5285 cpi->total_ssimg_all_in_layer[i] += frame_all;
5290 cpi->total_ssimg_y += y;
5291 cpi->total_ssimg_u += u;
5292 cpi->total_ssimg_v += v;
5293 cpi->total_ssimg_all += frame_all;
5302 if (cpi->common.frame_type != 0 && cpi->common.base_qindex == cpi->oxcf.worst_allowed_q)
5304 skiptruecount += cpi->skip_true_count;
5305 skipfalsecount += cpi->skip_false_count;
5313 FILE *f = fopen("skip.stt", "a");
5314 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);
5316 if (cpi->is_src_frame_alt_ref == 1)
5317 fprintf(f, "skipcount: %4d framesize: %d\n", cpi->skip_true_count , *size);
5326 #if CONFIG_RUNTIME_CPU_DETECT
5327 if (cm->rtcd.flags & HAS_NEON)
5330 vp8_pop_neon(store_reg);
5334 cpi->common.error.setjmp = 0;
5339 int vp8_get_preview_raw_frame(VP8_PTR comp, YV12_BUFFER_CONFIG *dest, vp8_ppflags_t *flags)
5341 VP8_COMP *cpi = (VP8_COMP *) comp;
5343 if (cpi->common.refresh_alt_ref_frame)
5349 ret = vp8_post_proc_frame(&cpi->common, dest, flags);
5352 if (cpi->common.frame_to_show)
5354 *dest = *cpi->common.frame_to_show;
5355 dest->y_width = cpi->common.Width;
5356 dest->y_height = cpi->common.Height;
5357 dest->uv_height = cpi->common.Height / 2;
5365 #endif //!CONFIG_POSTPROC
5366 vp8_clear_system_state();
5371 int vp8_set_roimap(VP8_PTR comp, unsigned char *map, unsigned int rows, unsigned int cols, int delta_q[4], int delta_lf[4], unsigned int threshold[4])
5373 VP8_COMP *cpi = (VP8_COMP *) comp;
5374 signed char feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS];
5376 if (cpi->common.mb_rows != rows || cpi->common.mb_cols != cols)
5381 disable_segmentation((VP8_PTR)cpi);
5385 // Set the segmentation Map
5386 set_segmentation_map((VP8_PTR)cpi, map);
5388 // Activate segmentation.
5389 enable_segmentation((VP8_PTR)cpi);
5391 // Set up the quant segment data
5392 feature_data[MB_LVL_ALT_Q][0] = delta_q[0];
5393 feature_data[MB_LVL_ALT_Q][1] = delta_q[1];
5394 feature_data[MB_LVL_ALT_Q][2] = delta_q[2];
5395 feature_data[MB_LVL_ALT_Q][3] = delta_q[3];
5397 // Set up the loop segment data s
5398 feature_data[MB_LVL_ALT_LF][0] = delta_lf[0];
5399 feature_data[MB_LVL_ALT_LF][1] = delta_lf[1];
5400 feature_data[MB_LVL_ALT_LF][2] = delta_lf[2];
5401 feature_data[MB_LVL_ALT_LF][3] = delta_lf[3];
5403 cpi->segment_encode_breakout[0] = threshold[0];
5404 cpi->segment_encode_breakout[1] = threshold[1];
5405 cpi->segment_encode_breakout[2] = threshold[2];
5406 cpi->segment_encode_breakout[3] = threshold[3];
5408 // Initialise the feature data structure
5409 // SEGMENT_DELTADATA 0, SEGMENT_ABSDATA 1
5410 set_segment_data((VP8_PTR)cpi, &feature_data[0][0], SEGMENT_DELTADATA);
5415 int vp8_set_active_map(VP8_PTR comp, unsigned char *map, unsigned int rows, unsigned int cols)
5417 VP8_COMP *cpi = (VP8_COMP *) comp;
5419 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols)
5423 vpx_memcpy(cpi->active_map, map, rows * cols);
5424 cpi->active_map_enabled = 1;
5427 cpi->active_map_enabled = 0;
5433 //cpi->active_map_enabled = 0;
5438 int vp8_set_internal_size(VP8_PTR comp, VPX_SCALING horiz_mode, VPX_SCALING vert_mode)
5440 VP8_COMP *cpi = (VP8_COMP *) comp;
5442 if (horiz_mode <= ONETWO)
5443 cpi->common.horiz_scale = horiz_mode;
5447 if (vert_mode <= ONETWO)
5448 cpi->common.vert_scale = vert_mode;
5457 int vp8_calc_ss_err(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *dest, const vp8_variance_rtcd_vtable_t *rtcd)
5462 unsigned char *src = source->y_buffer;
5463 unsigned char *dst = dest->y_buffer;
5466 // Loop through the Y plane raw and reconstruction data summing (square differences)
5467 for (i = 0; i < source->y_height; i += 16)
5469 for (j = 0; j < source->y_width; j += 16)
5472 Total += VARIANCE_INVOKE(rtcd, mse16x16)(src + j, source->y_stride, dst + j, dest->y_stride, &sse);
5475 src += 16 * source->y_stride;
5476 dst += 16 * dest->y_stride;
5483 int vp8_get_quantizer(VP8_PTR c)
5485 VP8_COMP *cpi = (VP8_COMP *) c;
5486 return cpi->common.base_qindex;