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"
27 #include "vp8/common/postproc.h"
29 #include "vpx_mem/vpx_mem.h"
30 #include "vp8/common/swapyv12buffer.h"
31 #include "vp8/common/threading.h"
32 #include "vpx_ports/vpx_timer.h"
34 #include "vpx_ports/arm.h"
36 #if CONFIG_MULTI_RES_ENCODING
37 #include "mr_dissim.h"
44 #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
45 extern int vp8_update_coef_context(VP8_COMP *cpi);
46 extern void vp8_update_coef_probs(VP8_COMP *cpi);
49 extern void vp8cx_pick_filter_level_fast(YV12_BUFFER_CONFIG *sd, VP8_COMP *cpi);
50 extern void vp8cx_set_alt_lf_level(VP8_COMP *cpi, int filt_val);
51 extern void vp8cx_pick_filter_level(YV12_BUFFER_CONFIG *sd, VP8_COMP *cpi);
53 extern void vp8_deblock_frame(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *post, int filt_lvl, int low_var_thresh, int flag);
54 extern void print_parms(VP8_CONFIG *ocf, char *filenam);
55 extern unsigned int vp8_get_processor_freq();
56 extern void print_tree_update_probs();
57 extern void vp8cx_create_encoder_threads(VP8_COMP *cpi);
58 extern void vp8cx_remove_encoder_threads(VP8_COMP *cpi);
60 extern void vp8_yv12_copy_frame_func_neon(YV12_BUFFER_CONFIG *src_ybc, YV12_BUFFER_CONFIG *dst_ybc);
61 extern void vp8_yv12_copy_src_frame_func_neon(YV12_BUFFER_CONFIG *src_ybc, YV12_BUFFER_CONFIG *dst_ybc);
64 int vp8_estimate_entropy_savings(VP8_COMP *cpi);
66 int vp8_calc_ss_err(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *dest);
68 extern void vp8_temporal_filter_prepare_c(VP8_COMP *cpi, int distance);
70 static void set_default_lf_deltas(VP8_COMP *cpi);
72 extern const int vp8_gf_interval_table[101];
74 #if CONFIG_INTERNAL_STATS
77 extern double vp8_calc_ssim
79 YV12_BUFFER_CONFIG *source,
80 YV12_BUFFER_CONFIG *dest,
86 extern double vp8_calc_ssimg
88 YV12_BUFFER_CONFIG *source,
89 YV12_BUFFER_CONFIG *dest,
110 extern int skip_true_count;
111 extern int skip_false_count;
116 extern int intra_mode_stats[10][10][10];
120 unsigned int frames_at_speed[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
121 unsigned int tot_pm = 0;
122 unsigned int cnt_pm = 0;
123 unsigned int tot_ef = 0;
124 unsigned int cnt_ef = 0;
128 extern unsigned __int64 Sectionbits[50];
129 extern int y_modes[5] ;
130 extern int uv_modes[4] ;
131 extern int b_modes[10] ;
133 extern int inter_y_modes[10] ;
134 extern int inter_uv_modes[4] ;
135 extern unsigned int inter_b_modes[15];
138 extern const int vp8_bits_per_mb[2][QINDEX_RANGE];
140 extern const int qrounding_factors[129];
141 extern const int qzbin_factors[129];
142 extern void vp8cx_init_quantizer(VP8_COMP *cpi);
143 extern const int vp8cx_base_skip_false_prob[128];
145 // Tables relating active max Q to active min Q
146 static const unsigned char kf_low_motion_minq[QINDEX_RANGE] =
148 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
149 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
150 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
151 0,0,0,0,1,1,1,1,1,1,1,1,2,2,2,2,
152 3,3,3,3,3,3,4,4,4,5,5,5,5,5,6,6,
153 6,6,7,7,8,8,8,8,9,9,10,10,10,10,11,11,
154 11,11,12,12,13,13,13,13,14,14,15,15,15,15,16,16,
155 16,16,17,17,18,18,18,18,19,20,20,21,21,22,23,23
157 static const unsigned char kf_high_motion_minq[QINDEX_RANGE] =
159 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
160 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
161 1,1,1,1,1,1,1,1,2,2,2,2,3,3,3,3,
162 3,3,3,3,4,4,4,4,5,5,5,5,5,5,6,6,
163 6,6,7,7,8,8,8,8,9,9,10,10,10,10,11,11,
164 11,11,12,12,13,13,13,13,14,14,15,15,15,15,16,16,
165 16,16,17,17,18,18,18,18,19,19,20,20,20,20,21,21,
166 21,21,22,22,23,23,24,25,25,26,26,27,28,28,29,30
168 static const unsigned char gf_low_motion_minq[QINDEX_RANGE] =
170 0,0,0,0,1,1,1,1,1,1,1,1,2,2,2,2,
171 3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,
172 7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10,
173 11,11,12,12,13,13,14,14,15,15,16,16,17,17,18,18,
174 19,19,20,20,21,21,22,22,23,23,24,24,25,25,26,26,
175 27,27,28,28,29,29,30,30,31,31,32,32,33,33,34,34,
176 35,35,36,36,37,37,38,38,39,39,40,40,41,41,42,42,
177 43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58
179 static const unsigned char gf_mid_motion_minq[QINDEX_RANGE] =
181 0,0,0,0,1,1,1,1,1,1,2,2,3,3,3,4,
182 4,4,5,5,5,6,6,6,7,7,7,8,8,8,9,9,
183 9,10,10,10,10,11,11,11,12,12,12,12,13,13,13,14,
184 14,14,15,15,16,16,17,17,18,18,19,19,20,20,21,21,
185 22,22,23,23,24,24,25,25,26,26,27,27,28,28,29,29,
186 30,30,31,31,32,32,33,33,34,34,35,35,36,36,37,37,
187 38,39,39,40,40,41,41,42,42,43,43,44,45,46,47,48,
188 49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64
190 static const unsigned char gf_high_motion_minq[QINDEX_RANGE] =
192 0,0,0,0,1,1,1,1,1,2,2,2,3,3,3,4,
193 4,4,5,5,5,6,6,6,7,7,7,8,8,8,9,9,
194 9,10,10,10,11,11,12,12,13,13,14,14,15,15,16,16,
195 17,17,18,18,19,19,20,20,21,21,22,22,23,23,24,24,
196 25,25,26,26,27,27,28,28,29,29,30,30,31,31,32,32,
197 33,33,34,34,35,35,36,36,37,37,38,38,39,39,40,40,
198 41,41,42,42,43,44,45,46,47,48,49,50,51,52,53,54,
199 55,56,57,58,59,60,62,64,66,68,70,72,74,76,78,80
201 static const unsigned char inter_minq[QINDEX_RANGE] =
203 0,0,1,1,2,3,3,4,4,5,6,6,7,8,8,9,
204 9,10,11,11,12,13,13,14,15,15,16,17,17,18,19,20,
205 20,21,22,22,23,24,24,25,26,27,27,28,29,30,30,31,
206 32,33,33,34,35,36,36,37,38,39,39,40,41,42,42,43,
207 44,45,46,46,47,48,49,50,50,51,52,53,54,55,55,56,
208 57,58,59,60,60,61,62,63,64,65,66,67,67,68,69,70,
209 71,72,73,74,75,75,76,77,78,79,80,81,82,83,84,85,
210 86,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100
213 #ifdef PACKET_TESTING
214 extern FILE *vpxlogc;
217 static void save_layer_context(VP8_COMP *cpi)
219 LAYER_CONTEXT *lc = &cpi->layer_context[cpi->current_layer];
221 // Save layer dependent coding state
222 lc->target_bandwidth = cpi->target_bandwidth;
223 //lc->target_bandwidth = cpi->oxcf.target_bandwidth;
224 lc->starting_buffer_level = cpi->oxcf.starting_buffer_level;
225 lc->optimal_buffer_level = cpi->oxcf.optimal_buffer_level;
226 lc->maximum_buffer_size = cpi->oxcf.maximum_buffer_size;
227 lc->starting_buffer_level_in_ms = cpi->oxcf.starting_buffer_level_in_ms;
228 lc->optimal_buffer_level_in_ms = cpi->oxcf.optimal_buffer_level_in_ms;
229 lc->maximum_buffer_size_in_ms = cpi->oxcf.maximum_buffer_size_in_ms;
230 lc->buffer_level = cpi->buffer_level;
231 lc->bits_off_target = cpi->bits_off_target;
232 lc->total_actual_bits = cpi->total_actual_bits;
233 lc->worst_quality = cpi->worst_quality;
234 lc->active_worst_quality = cpi->active_worst_quality;
235 lc->best_quality = cpi->best_quality;
236 lc->active_best_quality = cpi->active_best_quality;
237 lc->ni_av_qi = cpi->ni_av_qi;
238 lc->ni_tot_qi = cpi->ni_tot_qi;
239 lc->ni_frames = cpi->ni_frames;
240 lc->avg_frame_qindex = cpi->avg_frame_qindex;
241 lc->rate_correction_factor = cpi->rate_correction_factor;
242 lc->key_frame_rate_correction_factor = cpi->key_frame_rate_correction_factor;
243 lc->gf_rate_correction_factor = cpi->gf_rate_correction_factor;
244 lc->zbin_over_quant = cpi->zbin_over_quant;
245 lc->inter_frame_target = cpi->inter_frame_target;
246 lc->total_byte_count = cpi->total_byte_count;
247 lc->filter_level = cpi->common.filter_level;
249 lc->last_frame_percent_intra = cpi->last_frame_percent_intra;
251 memcpy (lc->count_mb_ref_frame_usage,
252 cpi->count_mb_ref_frame_usage,
253 sizeof(cpi->count_mb_ref_frame_usage));
256 static void restore_layer_context(VP8_COMP *cpi, const int layer)
258 LAYER_CONTEXT *lc = &cpi->layer_context[layer];
260 // Restore layer dependent coding state
261 cpi->current_layer = layer;
262 cpi->target_bandwidth = lc->target_bandwidth;
263 cpi->oxcf.target_bandwidth = lc->target_bandwidth;
264 cpi->oxcf.starting_buffer_level = lc->starting_buffer_level;
265 cpi->oxcf.optimal_buffer_level = lc->optimal_buffer_level;
266 cpi->oxcf.maximum_buffer_size = lc->maximum_buffer_size;
267 cpi->oxcf.starting_buffer_level_in_ms = lc->starting_buffer_level_in_ms;
268 cpi->oxcf.optimal_buffer_level_in_ms = lc->optimal_buffer_level_in_ms;
269 cpi->oxcf.maximum_buffer_size_in_ms = lc->maximum_buffer_size_in_ms;
270 cpi->buffer_level = lc->buffer_level;
271 cpi->bits_off_target = lc->bits_off_target;
272 cpi->total_actual_bits = lc->total_actual_bits;
273 //cpi->worst_quality = lc->worst_quality;
274 cpi->active_worst_quality = lc->active_worst_quality;
275 //cpi->best_quality = lc->best_quality;
276 cpi->active_best_quality = lc->active_best_quality;
277 cpi->ni_av_qi = lc->ni_av_qi;
278 cpi->ni_tot_qi = lc->ni_tot_qi;
279 cpi->ni_frames = lc->ni_frames;
280 cpi->avg_frame_qindex = lc->avg_frame_qindex;
281 cpi->rate_correction_factor = lc->rate_correction_factor;
282 cpi->key_frame_rate_correction_factor = lc->key_frame_rate_correction_factor;
283 cpi->gf_rate_correction_factor = lc->gf_rate_correction_factor;
284 cpi->zbin_over_quant = lc->zbin_over_quant;
285 cpi->inter_frame_target = lc->inter_frame_target;
286 cpi->total_byte_count = lc->total_byte_count;
287 cpi->common.filter_level = lc->filter_level;
289 cpi->last_frame_percent_intra = lc->last_frame_percent_intra;
291 memcpy (cpi->count_mb_ref_frame_usage,
292 lc->count_mb_ref_frame_usage,
293 sizeof(cpi->count_mb_ref_frame_usage));
296 static void setup_features(VP8_COMP *cpi)
298 // Set up default state for MB feature flags
299 cpi->mb.e_mbd.segmentation_enabled = 0;
300 cpi->mb.e_mbd.update_mb_segmentation_map = 0;
301 cpi->mb.e_mbd.update_mb_segmentation_data = 0;
302 vpx_memset(cpi->mb.e_mbd.mb_segment_tree_probs, 255, sizeof(cpi->mb.e_mbd.mb_segment_tree_probs));
303 vpx_memset(cpi->mb.e_mbd.segment_feature_data, 0, sizeof(cpi->mb.e_mbd.segment_feature_data));
305 cpi->mb.e_mbd.mode_ref_lf_delta_enabled = 0;
306 cpi->mb.e_mbd.mode_ref_lf_delta_update = 0;
307 vpx_memset(cpi->mb.e_mbd.ref_lf_deltas, 0, sizeof(cpi->mb.e_mbd.ref_lf_deltas));
308 vpx_memset(cpi->mb.e_mbd.mode_lf_deltas, 0, sizeof(cpi->mb.e_mbd.mode_lf_deltas));
309 vpx_memset(cpi->mb.e_mbd.last_ref_lf_deltas, 0, sizeof(cpi->mb.e_mbd.ref_lf_deltas));
310 vpx_memset(cpi->mb.e_mbd.last_mode_lf_deltas, 0, sizeof(cpi->mb.e_mbd.mode_lf_deltas));
312 set_default_lf_deltas(cpi);
317 static void dealloc_raw_frame_buffers(VP8_COMP *cpi);
320 static void dealloc_compressor_data(VP8_COMP *cpi)
322 vpx_free(cpi->tplist);
325 // Delete last frame MV storage buffers
329 vpx_free(cpi->lf_ref_frame_sign_bias);
330 cpi->lf_ref_frame_sign_bias = 0;
332 vpx_free(cpi->lf_ref_frame);
333 cpi->lf_ref_frame = 0;
335 // Delete sementation map
336 vpx_free(cpi->segmentation_map);
337 cpi->segmentation_map = 0;
339 vpx_free(cpi->active_map);
342 vp8_de_alloc_frame_buffers(&cpi->common);
344 vp8_yv12_de_alloc_frame_buffer(&cpi->pick_lf_lvl_frame);
345 vp8_yv12_de_alloc_frame_buffer(&cpi->scaled_source);
346 dealloc_raw_frame_buffers(cpi);
351 // Structure used to monitor GF usage
352 vpx_free(cpi->gf_active_flags);
353 cpi->gf_active_flags = 0;
355 // Activity mask based per mb zbin adjustments
356 vpx_free(cpi->mb_activity_map);
357 cpi->mb_activity_map = 0;
358 vpx_free(cpi->mb_norm_activity_map);
359 cpi->mb_norm_activity_map = 0;
361 vpx_free(cpi->mb.pip);
365 static void enable_segmentation(VP8_COMP *cpi)
367 // Set the appropriate feature bit
368 cpi->mb.e_mbd.segmentation_enabled = 1;
369 cpi->mb.e_mbd.update_mb_segmentation_map = 1;
370 cpi->mb.e_mbd.update_mb_segmentation_data = 1;
372 static void disable_segmentation(VP8_COMP *cpi)
374 // Clear the appropriate feature bit
375 cpi->mb.e_mbd.segmentation_enabled = 0;
378 // Valid values for a segment are 0 to 3
379 // Segmentation map is arrange as [Rows][Columns]
380 static void set_segmentation_map(VP8_COMP *cpi, unsigned char *segmentation_map)
382 // Copy in the new segmentation map
383 vpx_memcpy(cpi->segmentation_map, segmentation_map, (cpi->common.mb_rows * cpi->common.mb_cols));
385 // Signal that the map should be updated.
386 cpi->mb.e_mbd.update_mb_segmentation_map = 1;
387 cpi->mb.e_mbd.update_mb_segmentation_data = 1;
390 // The values given for each segment can be either deltas (from the default value chosen for the frame) or absolute values.
392 // Valid range for abs values is (0-127 for MB_LVL_ALT_Q) , (0-63 for SEGMENT_ALT_LF)
393 // Valid range for delta values are (+/-127 for MB_LVL_ALT_Q) , (+/-63 for SEGMENT_ALT_LF)
395 // abs_delta = SEGMENT_DELTADATA (deltas) abs_delta = SEGMENT_ABSDATA (use the absolute values given).
398 static void set_segment_data(VP8_COMP *cpi, signed char *feature_data, unsigned char abs_delta)
400 cpi->mb.e_mbd.mb_segement_abs_delta = abs_delta;
401 vpx_memcpy(cpi->segment_feature_data, feature_data, sizeof(cpi->segment_feature_data));
405 static void segmentation_test_function(VP8_COMP *cpi)
407 unsigned char *seg_map;
408 signed char feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS];
410 // Create a temporary map for segmentation data.
411 CHECK_MEM_ERROR(seg_map, vpx_calloc(cpi->common.mb_rows * cpi->common.mb_cols, 1));
413 // MB loop to set local segmentation map
414 /*for ( i = 0; i < cpi->common.mb_rows; i++ )
416 for ( j = 0; j < cpi->common.mb_cols; j++ )
418 //seg_map[(i*cpi->common.mb_cols) + j] = (j % 2) + ((i%2)* 2);
419 //if ( j < cpi->common.mb_cols/2 )
421 // Segment 1 around the edge else 0
422 if ( (i == 0) || (j == 0) || (i == (cpi->common.mb_rows-1)) || (j == (cpi->common.mb_cols-1)) )
423 seg_map[(i*cpi->common.mb_cols) + j] = 1;
424 //else if ( (i < 2) || (j < 2) || (i > (cpi->common.mb_rows-3)) || (j > (cpi->common.mb_cols-3)) )
425 // seg_map[(i*cpi->common.mb_cols) + j] = 2;
426 //else if ( (i < 5) || (j < 5) || (i > (cpi->common.mb_rows-6)) || (j > (cpi->common.mb_cols-6)) )
427 // seg_map[(i*cpi->common.mb_cols) + j] = 3;
429 seg_map[(i*cpi->common.mb_cols) + j] = 0;
433 // Set the segmentation Map
434 set_segmentation_map(cpi, seg_map);
436 // Activate segmentation.
437 enable_segmentation(cpi);
439 // Set up the quant segment data
440 feature_data[MB_LVL_ALT_Q][0] = 0;
441 feature_data[MB_LVL_ALT_Q][1] = 4;
442 feature_data[MB_LVL_ALT_Q][2] = 0;
443 feature_data[MB_LVL_ALT_Q][3] = 0;
444 // Set up the loop segment data
445 feature_data[MB_LVL_ALT_LF][0] = 0;
446 feature_data[MB_LVL_ALT_LF][1] = 0;
447 feature_data[MB_LVL_ALT_LF][2] = 0;
448 feature_data[MB_LVL_ALT_LF][3] = 0;
450 // Initialise the feature data structure
451 // SEGMENT_DELTADATA 0, SEGMENT_ABSDATA 1
452 set_segment_data(cpi, &feature_data[0][0], SEGMENT_DELTADATA);
454 // Delete sementation map
461 // A simple function to cyclically refresh the background at a lower Q
462 static void cyclic_background_refresh(VP8_COMP *cpi, int Q, int lf_adjustment)
464 unsigned char *seg_map;
465 signed char feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS];
467 int block_count = cpi->cyclic_refresh_mode_max_mbs_perframe;
468 int mbs_in_frame = cpi->common.mb_rows * cpi->common.mb_cols;
470 // Create a temporary map for segmentation data.
471 CHECK_MEM_ERROR(seg_map, vpx_calloc(cpi->common.mb_rows * cpi->common.mb_cols, 1));
473 cpi->cyclic_refresh_q = Q;
475 for (i = Q; i > 0; i--)
477 if (vp8_bits_per_mb[cpi->common.frame_type][i] >= ((vp8_bits_per_mb[cpi->common.frame_type][Q]*(Q + 128)) / 64))
478 //if ( vp8_bits_per_mb[cpi->common.frame_type][i] >= ((vp8_bits_per_mb[cpi->common.frame_type][Q]*((2*Q)+96))/64) )
484 cpi->cyclic_refresh_q = i;
486 // Only update for inter frames
487 if (cpi->common.frame_type != KEY_FRAME)
489 // Cycle through the macro_block rows
490 // MB loop to set local segmentation map
491 for (i = cpi->cyclic_refresh_mode_index; i < mbs_in_frame; i++)
493 // If the MB is as a candidate for clean up then mark it for possible boost/refresh (segment 1)
494 // The segment id may get reset to 0 later if the MB gets coded anything other than last frame 0,0
495 // as only (last frame 0,0) MBs are eligable for refresh : that is to say Mbs likely to be background blocks.
496 if (cpi->cyclic_refresh_map[i] == 0)
504 // Skip blocks that have been refreshed recently anyway.
505 if (cpi->cyclic_refresh_map[i] < 0)
506 //cpi->cyclic_refresh_map[i] = cpi->cyclic_refresh_map[i] / 16;
507 cpi->cyclic_refresh_map[i]++;
518 // If we have gone through the frame reset to the start
519 cpi->cyclic_refresh_mode_index = i;
521 if (cpi->cyclic_refresh_mode_index >= mbs_in_frame)
522 cpi->cyclic_refresh_mode_index = 0;
525 // Set the segmentation Map
526 set_segmentation_map(cpi, seg_map);
528 // Activate segmentation.
529 enable_segmentation(cpi);
531 // Set up the quant segment data
532 feature_data[MB_LVL_ALT_Q][0] = 0;
533 feature_data[MB_LVL_ALT_Q][1] = (cpi->cyclic_refresh_q - Q);
534 feature_data[MB_LVL_ALT_Q][2] = 0;
535 feature_data[MB_LVL_ALT_Q][3] = 0;
537 // Set up the loop segment data
538 feature_data[MB_LVL_ALT_LF][0] = 0;
539 feature_data[MB_LVL_ALT_LF][1] = lf_adjustment;
540 feature_data[MB_LVL_ALT_LF][2] = 0;
541 feature_data[MB_LVL_ALT_LF][3] = 0;
543 // Initialise the feature data structure
544 // SEGMENT_DELTADATA 0, SEGMENT_ABSDATA 1
545 set_segment_data(cpi, &feature_data[0][0], SEGMENT_DELTADATA);
547 // Delete sementation map
554 static void set_default_lf_deltas(VP8_COMP *cpi)
556 cpi->mb.e_mbd.mode_ref_lf_delta_enabled = 1;
557 cpi->mb.e_mbd.mode_ref_lf_delta_update = 1;
559 vpx_memset(cpi->mb.e_mbd.ref_lf_deltas, 0, sizeof(cpi->mb.e_mbd.ref_lf_deltas));
560 vpx_memset(cpi->mb.e_mbd.mode_lf_deltas, 0, sizeof(cpi->mb.e_mbd.mode_lf_deltas));
562 // Test of ref frame deltas
563 cpi->mb.e_mbd.ref_lf_deltas[INTRA_FRAME] = 2;
564 cpi->mb.e_mbd.ref_lf_deltas[LAST_FRAME] = 0;
565 cpi->mb.e_mbd.ref_lf_deltas[GOLDEN_FRAME] = -2;
566 cpi->mb.e_mbd.ref_lf_deltas[ALTREF_FRAME] = -2;
568 cpi->mb.e_mbd.mode_lf_deltas[0] = 4; // BPRED
569 cpi->mb.e_mbd.mode_lf_deltas[1] = -2; // Zero
570 cpi->mb.e_mbd.mode_lf_deltas[2] = 2; // New mv
571 cpi->mb.e_mbd.mode_lf_deltas[3] = 4; // Split mv
574 /* Convenience macros for mapping speed and mode into a continuous
577 #define GOOD(x) (x+1)
580 static int speed_map(int speed, const int *map)
587 } while(speed >= *map++);
591 static const int thresh_mult_map_znn[] = {
592 /* map common to zero, nearest, and near */
593 0, GOOD(2), 1500, GOOD(3), 2000, RT(0), 1000, RT(2), 2000, INT_MAX
596 static const int thresh_mult_map_vhpred[] = {
597 1000, GOOD(2), 1500, GOOD(3), 2000, RT(0), 1000, RT(1), 2000,
598 RT(7), INT_MAX, INT_MAX
601 static const int thresh_mult_map_bpred[] = {
602 2000, GOOD(0), 2500, GOOD(2), 5000, GOOD(3), 7500, RT(0), 2500, RT(1), 5000,
603 RT(6), INT_MAX, INT_MAX
606 static const int thresh_mult_map_tm[] = {
607 1000, GOOD(2), 1500, GOOD(3), 2000, RT(0), 0, RT(1), 1000, RT(2), 2000,
608 RT(7), INT_MAX, INT_MAX
611 static const int thresh_mult_map_new1[] = {
612 1000, GOOD(2), 2000, RT(0), 2000, INT_MAX
615 static const int thresh_mult_map_new2[] = {
616 1000, GOOD(2), 2000, GOOD(3), 2500, GOOD(5), 4000, RT(0), 2000, RT(2), 2500,
620 static const int thresh_mult_map_split1[] = {
621 2500, GOOD(0), 1700, GOOD(2), 10000, GOOD(3), 25000, GOOD(4), INT_MAX,
622 RT(0), 5000, RT(1), 10000, RT(2), 25000, RT(3), INT_MAX, INT_MAX
625 static const int thresh_mult_map_split2[] = {
626 5000, GOOD(0), 4500, GOOD(2), 20000, GOOD(3), 50000, GOOD(4), INT_MAX,
627 RT(0), 10000, RT(1), 20000, RT(2), 50000, RT(3), INT_MAX, INT_MAX
630 static const int mode_check_freq_map_zn2[] = {
631 /* {zero,nearest}{2,3} */
632 0, RT(10), 1<<1, RT(11), 1<<2, RT(12), 1<<3, INT_MAX
635 static const int mode_check_freq_map_vhbpred[] = {
636 0, GOOD(5), 2, RT(0), 0, RT(3), 2, RT(5), 4, INT_MAX
639 static const int mode_check_freq_map_near2[] = {
640 0, GOOD(5), 2, RT(0), 0, RT(3), 2, RT(10), 1<<2, RT(11), 1<<3, RT(12), 1<<4,
644 static const int mode_check_freq_map_new1[] = {
645 0, RT(10), 1<<1, RT(11), 1<<2, RT(12), 1<<3, INT_MAX
648 static const int mode_check_freq_map_new2[] = {
649 0, GOOD(5), 4, RT(0), 0, RT(3), 4, RT(10), 1<<3, RT(11), 1<<4, RT(12), 1<<5,
653 static const int mode_check_freq_map_split1[] = {
654 0, GOOD(2), 2, GOOD(3), 7, RT(1), 2, RT(2), 7, INT_MAX
657 static const int mode_check_freq_map_split2[] = {
658 0, GOOD(1), 2, GOOD(2), 4, GOOD(3), 15, RT(1), 4, RT(2), 15, INT_MAX
661 void vp8_set_speed_features(VP8_COMP *cpi)
663 SPEED_FEATURES *sf = &cpi->sf;
664 int Mode = cpi->compressor_speed;
665 int Speed = cpi->Speed;
667 VP8_COMMON *cm = &cpi->common;
668 int last_improved_quant = sf->improved_quant;
671 // Initialise default mode frequency sampling variables
672 for (i = 0; i < MAX_MODES; i ++)
674 cpi->mode_check_freq[i] = 0;
675 cpi->mode_test_hit_counts[i] = 0;
676 cpi->mode_chosen_counts[i] = 0;
679 cpi->mbs_tested_so_far = 0;
681 // best quality defaults
683 sf->search_method = NSTEP;
684 sf->improved_quant = 1;
685 sf->improved_dct = 1;
688 sf->quarter_pixel_search = 1;
689 sf->half_pixel_search = 1;
690 sf->iterative_sub_pixel = 1;
691 sf->optimize_coefficients = 1;
692 sf->use_fastquant_for_pick = 0;
693 sf->no_skip_block4x4_search = 1;
696 sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
697 sf->improved_mv_pred = 1;
699 // default thresholds to 0
700 for (i = 0; i < MAX_MODES; i++)
701 sf->thresh_mult[i] = 0;
703 /* Count enabled references */
705 if (cpi->ref_frame_flags & VP8_LAST_FLAG)
707 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
709 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
712 /* Convert speed to continuous range, with clamping */
724 sf->thresh_mult[THR_ZERO1] =
725 sf->thresh_mult[THR_NEAREST1] =
726 sf->thresh_mult[THR_NEAR1] =
727 sf->thresh_mult[THR_DC] = 0; /* always */
729 sf->thresh_mult[THR_ZERO2] =
730 sf->thresh_mult[THR_ZERO3] =
731 sf->thresh_mult[THR_NEAREST2] =
732 sf->thresh_mult[THR_NEAREST3] =
733 sf->thresh_mult[THR_NEAR2] =
734 sf->thresh_mult[THR_NEAR3] = speed_map(Speed, thresh_mult_map_znn);
736 sf->thresh_mult[THR_V_PRED] =
737 sf->thresh_mult[THR_H_PRED] = speed_map(Speed, thresh_mult_map_vhpred);
738 sf->thresh_mult[THR_B_PRED] = speed_map(Speed, thresh_mult_map_bpred);
739 sf->thresh_mult[THR_TM] = speed_map(Speed, thresh_mult_map_tm);
740 sf->thresh_mult[THR_NEW1] = speed_map(Speed, thresh_mult_map_new1);
741 sf->thresh_mult[THR_NEW2] =
742 sf->thresh_mult[THR_NEW3] = speed_map(Speed, thresh_mult_map_new2);
743 sf->thresh_mult[THR_SPLIT1] = speed_map(Speed, thresh_mult_map_split1);
744 sf->thresh_mult[THR_SPLIT2] =
745 sf->thresh_mult[THR_SPLIT3] = speed_map(Speed, thresh_mult_map_split2);
747 cpi->mode_check_freq[THR_ZERO1] =
748 cpi->mode_check_freq[THR_NEAREST1] =
749 cpi->mode_check_freq[THR_NEAR1] =
750 cpi->mode_check_freq[THR_TM] =
751 cpi->mode_check_freq[THR_DC] = 0; /* always */
753 cpi->mode_check_freq[THR_ZERO2] =
754 cpi->mode_check_freq[THR_ZERO3] =
755 cpi->mode_check_freq[THR_NEAREST2] =
756 cpi->mode_check_freq[THR_NEAREST3] = speed_map(Speed,
757 mode_check_freq_map_zn2);
759 cpi->mode_check_freq[THR_NEAR2] =
760 cpi->mode_check_freq[THR_NEAR3] = speed_map(Speed,
761 mode_check_freq_map_near2);
763 cpi->mode_check_freq[THR_V_PRED] =
764 cpi->mode_check_freq[THR_H_PRED] =
765 cpi->mode_check_freq[THR_B_PRED] = speed_map(Speed,
766 mode_check_freq_map_vhbpred);
767 cpi->mode_check_freq[THR_NEW1] = speed_map(Speed,
768 mode_check_freq_map_new1);
769 cpi->mode_check_freq[THR_NEW2] =
770 cpi->mode_check_freq[THR_NEW3] = speed_map(Speed,
771 mode_check_freq_map_new2);
772 cpi->mode_check_freq[THR_SPLIT1] = speed_map(Speed,
773 mode_check_freq_map_split1);
774 cpi->mode_check_freq[THR_SPLIT2] =
775 cpi->mode_check_freq[THR_SPLIT3] = speed_map(Speed,
776 mode_check_freq_map_split2);
780 #if !(CONFIG_REALTIME_ONLY)
781 case 0: // best quality mode
783 sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
789 /* Disable coefficient optimization above speed 0 */
790 sf->optimize_coefficients = 0;
791 sf->use_fastquant_for_pick = 1;
792 sf->no_skip_block4x4_search = 0;
799 sf->improved_quant = 0;
800 sf->improved_dct = 0;
802 // Only do recode loop on key frames, golden frames and
811 sf->recode_loop = 0; // recode loop off
812 sf->RD = 0; // Turn rd off
818 sf->auto_filter = 0; // Faster selection of loop filter
824 sf->optimize_coefficients = 0;
827 sf->iterative_sub_pixel = 1;
828 sf->search_method = NSTEP;
832 sf->improved_quant = 0;
833 sf->improved_dct = 0;
835 sf->use_fastquant_for_pick = 1;
836 sf->no_skip_block4x4_search = 0;
841 sf->auto_filter = 0; // Faster selection of loop filter
851 sf->auto_filter = 0; // Faster selection of loop filter
852 sf->search_method = HEX;
853 sf->iterative_sub_pixel = 0;
858 unsigned int sum = 0;
859 unsigned int total_mbs = cm->MBs;
861 unsigned int total_skip;
865 if (cpi->oxcf.encode_breakout > 2000)
866 min = cpi->oxcf.encode_breakout;
870 for (i = 0; i < min; i++)
872 sum += cpi->error_bins[i];
878 // i starts from 2 to make sure thresh started from 2048
879 for (; i < 1024; i++)
881 sum += cpi->error_bins[i];
883 if (10 * sum >= (unsigned int)(cpi->Speed - 6)*(total_mbs - total_skip))
895 sf->thresh_mult[THR_NEW1 ] = thresh;
896 sf->thresh_mult[THR_NEAREST1 ] = thresh >> 1;
897 sf->thresh_mult[THR_NEAR1 ] = thresh >> 1;
902 sf->thresh_mult[THR_NEW2] = thresh << 1;
903 sf->thresh_mult[THR_NEAREST2 ] = thresh;
904 sf->thresh_mult[THR_NEAR2 ] = thresh;
909 sf->thresh_mult[THR_NEW3] = thresh << 1;
910 sf->thresh_mult[THR_NEAREST3 ] = thresh;
911 sf->thresh_mult[THR_NEAR3 ] = thresh;
914 sf->improved_mv_pred = 0;
918 sf->quarter_pixel_search = 0;
922 cm->filter_type = NORMAL_LOOPFILTER;
925 cm->filter_type = SIMPLE_LOOPFILTER;
929 cm->filter_type = SIMPLE_LOOPFILTER;
932 // This has a big hit on quality. Last resort
934 sf->half_pixel_search = 0;
936 vpx_memset(cpi->error_bins, 0, sizeof(cpi->error_bins));
940 // Slow quant, dct and trellis not worthwhile for first pass
941 // so make sure they are always turned off.
942 if ( cpi->pass == 1 )
944 sf->improved_quant = 0;
945 sf->optimize_coefficients = 0;
946 sf->improved_dct = 0;
949 if (cpi->sf.search_method == NSTEP)
951 vp8_init3smotion_compensation(&cpi->mb, cm->yv12_fb[cm->lst_fb_idx].y_stride);
953 else if (cpi->sf.search_method == DIAMOND)
955 vp8_init_dsmotion_compensation(&cpi->mb, cm->yv12_fb[cm->lst_fb_idx].y_stride);
958 if (cpi->sf.improved_dct)
960 cpi->mb.short_fdct8x4 = vp8_short_fdct8x4;
961 cpi->mb.short_fdct4x4 = vp8_short_fdct4x4;
965 /* No fast FDCT defined for any platform at this time. */
966 cpi->mb.short_fdct8x4 = vp8_short_fdct8x4;
967 cpi->mb.short_fdct4x4 = vp8_short_fdct4x4;
970 cpi->mb.short_walsh4x4 = vp8_short_walsh4x4;
972 if (cpi->sf.improved_quant)
974 cpi->mb.quantize_b = vp8_regular_quantize_b;
975 cpi->mb.quantize_b_pair = vp8_regular_quantize_b_pair;
979 cpi->mb.quantize_b = vp8_fast_quantize_b;
980 cpi->mb.quantize_b_pair = vp8_fast_quantize_b_pair;
982 if (cpi->sf.improved_quant != last_improved_quant)
983 vp8cx_init_quantizer(cpi);
985 if (cpi->sf.iterative_sub_pixel == 1)
987 cpi->find_fractional_mv_step = vp8_find_best_sub_pixel_step_iteratively;
989 else if (cpi->sf.quarter_pixel_search)
991 cpi->find_fractional_mv_step = vp8_find_best_sub_pixel_step;
993 else if (cpi->sf.half_pixel_search)
995 cpi->find_fractional_mv_step = vp8_find_best_half_pixel_step;
999 cpi->find_fractional_mv_step = vp8_skip_fractional_mv_step;
1002 if (cpi->sf.optimize_coefficients == 1 && cpi->pass!=1)
1003 cpi->mb.optimize = 1;
1005 cpi->mb.optimize = 0;
1007 if (cpi->common.full_pixel)
1008 cpi->find_fractional_mv_step = vp8_skip_fractional_mv_step;
1011 frames_at_speed[cpi->Speed]++;
1017 static void alloc_raw_frame_buffers(VP8_COMP *cpi)
1019 #if VP8_TEMPORAL_ALT_REF
1020 int width = (cpi->oxcf.Width + 15) & ~15;
1021 int height = (cpi->oxcf.Height + 15) & ~15;
1024 cpi->lookahead = vp8_lookahead_init(cpi->oxcf.Width, cpi->oxcf.Height,
1025 cpi->oxcf.lag_in_frames);
1027 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1028 "Failed to allocate lag buffers");
1030 #if VP8_TEMPORAL_ALT_REF
1032 if (vp8_yv12_alloc_frame_buffer(&cpi->alt_ref_buffer,
1033 width, height, VP8BORDERINPIXELS))
1034 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1035 "Failed to allocate altref buffer");
1041 static void dealloc_raw_frame_buffers(VP8_COMP *cpi)
1043 #if VP8_TEMPORAL_ALT_REF
1044 vp8_yv12_de_alloc_frame_buffer(&cpi->alt_ref_buffer);
1046 vp8_lookahead_destroy(cpi->lookahead);
1050 static int vp8_alloc_partition_data(VP8_COMP *cpi)
1052 vpx_free(cpi->mb.pip);
1054 cpi->mb.pip = vpx_calloc((cpi->common.mb_cols + 1) *
1055 (cpi->common.mb_rows + 1),
1056 sizeof(PARTITION_INFO));
1060 cpi->mb.pi = cpi->mb.pip + cpi->common.mode_info_stride + 1;
1065 void vp8_alloc_compressor_data(VP8_COMP *cpi)
1067 VP8_COMMON *cm = & cpi->common;
1069 int width = cm->Width;
1070 int height = cm->Height;
1072 if (vp8_alloc_frame_buffers(cm, width, height))
1073 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1074 "Failed to allocate frame buffers");
1076 if (vp8_alloc_partition_data(cpi))
1077 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1078 "Failed to allocate partition data");
1081 if ((width & 0xf) != 0)
1082 width += 16 - (width & 0xf);
1084 if ((height & 0xf) != 0)
1085 height += 16 - (height & 0xf);
1088 if (vp8_yv12_alloc_frame_buffer(&cpi->pick_lf_lvl_frame,
1089 width, height, VP8BORDERINPIXELS))
1090 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1091 "Failed to allocate last frame buffer");
1093 if (vp8_yv12_alloc_frame_buffer(&cpi->scaled_source,
1094 width, height, VP8BORDERINPIXELS))
1095 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1096 "Failed to allocate scaled source buffer");
1101 #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
1102 unsigned int tokens = 8 * 24 * 16; /* one MB for each thread */
1104 unsigned int tokens = cm->mb_rows * cm->mb_cols * 24 * 16;
1106 CHECK_MEM_ERROR(cpi->tok, vpx_calloc(tokens, sizeof(*cpi->tok)));
1109 // Data used for real time vc mode to see if gf needs refreshing
1110 cpi->inter_zz_count = 0;
1111 cpi->gf_bad_count = 0;
1112 cpi->gf_update_recommended = 0;
1115 // Structures used to minitor GF usage
1116 vpx_free(cpi->gf_active_flags);
1117 CHECK_MEM_ERROR(cpi->gf_active_flags,
1118 vpx_calloc(1, cm->mb_rows * cm->mb_cols));
1119 cpi->gf_active_count = cm->mb_rows * cm->mb_cols;
1121 vpx_free(cpi->mb_activity_map);
1122 CHECK_MEM_ERROR(cpi->mb_activity_map,
1123 vpx_calloc(sizeof(unsigned int),
1124 cm->mb_rows * cm->mb_cols));
1126 vpx_free(cpi->mb_norm_activity_map);
1127 CHECK_MEM_ERROR(cpi->mb_norm_activity_map,
1128 vpx_calloc(sizeof(unsigned int),
1129 cm->mb_rows * cm->mb_cols));
1131 #if CONFIG_MULTITHREAD
1133 cpi->mt_sync_range = 1;
1134 else if (width <= 1280)
1135 cpi->mt_sync_range = 4;
1136 else if (width <= 2560)
1137 cpi->mt_sync_range = 8;
1139 cpi->mt_sync_range = 16;
1142 vpx_free(cpi->tplist);
1144 CHECK_MEM_ERROR(cpi->tplist, vpx_malloc(sizeof(TOKENLIST) * cpi->common.mb_rows));
1149 static const int q_trans[] =
1151 0, 1, 2, 3, 4, 5, 7, 8,
1152 9, 10, 12, 13, 15, 17, 18, 19,
1153 20, 21, 23, 24, 25, 26, 27, 28,
1154 29, 30, 31, 33, 35, 37, 39, 41,
1155 43, 45, 47, 49, 51, 53, 55, 57,
1156 59, 61, 64, 67, 70, 73, 76, 79,
1157 82, 85, 88, 91, 94, 97, 100, 103,
1158 106, 109, 112, 115, 118, 121, 124, 127,
1161 int vp8_reverse_trans(int x)
1165 for (i = 0; i < 64; i++)
1166 if (q_trans[i] >= x)
1171 void vp8_new_frame_rate(VP8_COMP *cpi, double framerate)
1176 cpi->frame_rate = framerate;
1177 cpi->output_frame_rate = framerate;
1178 cpi->per_frame_bandwidth = (int)(cpi->oxcf.target_bandwidth /
1179 cpi->output_frame_rate);
1180 cpi->av_per_frame_bandwidth = cpi->per_frame_bandwidth;
1181 cpi->min_frame_bandwidth = (int)(cpi->av_per_frame_bandwidth *
1182 cpi->oxcf.two_pass_vbrmin_section / 100);
1184 // Set Maximum gf/arf interval
1185 cpi->max_gf_interval = ((int)(cpi->output_frame_rate / 2.0) + 2);
1187 if(cpi->max_gf_interval < 12)
1188 cpi->max_gf_interval = 12;
1190 // Extended interval for genuinely static scenes
1191 cpi->twopass.static_scene_max_gf_interval = cpi->key_frame_frequency >> 1;
1193 // Special conditions when altr ref frame enabled in lagged compress mode
1194 if (cpi->oxcf.play_alternate && cpi->oxcf.lag_in_frames)
1196 if (cpi->max_gf_interval > cpi->oxcf.lag_in_frames - 1)
1197 cpi->max_gf_interval = cpi->oxcf.lag_in_frames - 1;
1199 if (cpi->twopass.static_scene_max_gf_interval > cpi->oxcf.lag_in_frames - 1)
1200 cpi->twopass.static_scene_max_gf_interval = cpi->oxcf.lag_in_frames - 1;
1203 if ( cpi->max_gf_interval > cpi->twopass.static_scene_max_gf_interval )
1204 cpi->max_gf_interval = cpi->twopass.static_scene_max_gf_interval;
1209 rescale(int val, int num, int denom)
1211 int64_t llnum = num;
1212 int64_t llden = denom;
1213 int64_t llval = val;
1215 return llval * llnum / llden;
1219 static void init_config(VP8_COMP *cpi, VP8_CONFIG *oxcf)
1221 VP8_COMMON *cm = &cpi->common;
1226 cpi->auto_adjust_gold_quantizer = 1;
1229 cm->version = oxcf->Version;
1230 vp8_setup_version(cm);
1232 /* frame rate is not available on the first frame, as it's derived from
1233 * the observed timestamps. The actual value used here doesn't matter
1234 * too much, as it will adapt quickly. If the reciprocal of the timebase
1235 * seems like a reasonable framerate, then use that as a guess, otherwise
1238 cpi->frame_rate = (double)(oxcf->timebase.den) /
1239 (double)(oxcf->timebase.num);
1241 if (cpi->frame_rate > 180)
1242 cpi->frame_rate = 30;
1244 cpi->ref_frame_rate = cpi->frame_rate;
1246 // change includes all joint functionality
1247 vp8_change_config(cpi, oxcf);
1249 // Initialize active best and worst q and average q values.
1250 cpi->active_worst_quality = cpi->oxcf.worst_allowed_q;
1251 cpi->active_best_quality = cpi->oxcf.best_allowed_q;
1252 cpi->avg_frame_qindex = cpi->oxcf.worst_allowed_q;
1254 // Initialise the starting buffer levels
1255 cpi->buffer_level = cpi->oxcf.starting_buffer_level;
1256 cpi->bits_off_target = cpi->oxcf.starting_buffer_level;
1258 cpi->rolling_target_bits = cpi->av_per_frame_bandwidth;
1259 cpi->rolling_actual_bits = cpi->av_per_frame_bandwidth;
1260 cpi->long_rolling_target_bits = cpi->av_per_frame_bandwidth;
1261 cpi->long_rolling_actual_bits = cpi->av_per_frame_bandwidth;
1263 cpi->total_actual_bits = 0;
1264 cpi->total_target_vs_actual = 0;
1266 // Temporal scalabilty
1267 if (cpi->oxcf.number_of_layers > 1)
1270 double prev_layer_frame_rate=0;
1272 for (i=0; i<cpi->oxcf.number_of_layers; i++)
1274 LAYER_CONTEXT *lc = &cpi->layer_context[i];
1276 // Layer configuration
1278 cpi->output_frame_rate / cpi->oxcf.rate_decimator[i];
1279 lc->target_bandwidth = cpi->oxcf.target_bitrate[i] * 1000;
1281 lc->starting_buffer_level_in_ms = oxcf->starting_buffer_level;
1282 lc->optimal_buffer_level_in_ms = oxcf->optimal_buffer_level;
1283 lc->maximum_buffer_size_in_ms = oxcf->maximum_buffer_size;
1285 lc->starting_buffer_level =
1286 rescale(oxcf->starting_buffer_level,
1287 lc->target_bandwidth, 1000);
1289 if (oxcf->optimal_buffer_level == 0)
1290 lc->optimal_buffer_level = lc->target_bandwidth / 8;
1292 lc->optimal_buffer_level =
1293 rescale(oxcf->optimal_buffer_level,
1294 lc->target_bandwidth, 1000);
1296 if (oxcf->maximum_buffer_size == 0)
1297 lc->maximum_buffer_size = lc->target_bandwidth / 8;
1299 lc->maximum_buffer_size =
1300 rescale(oxcf->maximum_buffer_size,
1301 lc->target_bandwidth, 1000);
1303 // Work out the average size of a frame within this layer
1305 lc->avg_frame_size_for_layer = (cpi->oxcf.target_bitrate[i] -
1306 cpi->oxcf.target_bitrate[i-1]) * 1000 /
1307 (lc->frame_rate - prev_layer_frame_rate);
1309 lc->active_worst_quality = cpi->oxcf.worst_allowed_q;
1310 lc->active_best_quality = cpi->oxcf.best_allowed_q;
1311 lc->avg_frame_qindex = cpi->oxcf.worst_allowed_q;
1313 lc->buffer_level = lc->starting_buffer_level;
1314 lc->bits_off_target = lc->starting_buffer_level;
1316 lc->total_actual_bits = 0;
1320 lc->rate_correction_factor = 1.0;
1321 lc->key_frame_rate_correction_factor = 1.0;
1322 lc->gf_rate_correction_factor = 1.0;
1323 lc->inter_frame_target = 0.0;
1325 prev_layer_frame_rate = lc->frame_rate;
1329 #if VP8_TEMPORAL_ALT_REF
1333 cpi->fixed_divide[0] = 0;
1335 for (i = 1; i < 512; i++)
1336 cpi->fixed_divide[i] = 0x80000 / i;
1341 void update_layer_contexts (VP8_COMP *cpi)
1343 VP8_CONFIG *oxcf = &cpi->oxcf;
1345 /* Update snapshots of the layer contexts to reflect new parameters */
1346 if (oxcf->number_of_layers > 1)
1349 double prev_layer_frame_rate=0;
1351 for (i=0; i<oxcf->number_of_layers; i++)
1353 LAYER_CONTEXT *lc = &cpi->layer_context[i];
1356 cpi->ref_frame_rate / oxcf->rate_decimator[i];
1357 lc->target_bandwidth = oxcf->target_bitrate[i] * 1000;
1359 lc->starting_buffer_level = rescale(
1360 oxcf->starting_buffer_level_in_ms,
1361 lc->target_bandwidth, 1000);
1363 if (oxcf->optimal_buffer_level == 0)
1364 lc->optimal_buffer_level = lc->target_bandwidth / 8;
1366 lc->optimal_buffer_level = rescale(
1367 oxcf->optimal_buffer_level_in_ms,
1368 lc->target_bandwidth, 1000);
1370 if (oxcf->maximum_buffer_size == 0)
1371 lc->maximum_buffer_size = lc->target_bandwidth / 8;
1373 lc->maximum_buffer_size = rescale(
1374 oxcf->maximum_buffer_size_in_ms,
1375 lc->target_bandwidth, 1000);
1377 // Work out the average size of a frame within this layer
1379 lc->avg_frame_size_for_layer = (oxcf->target_bitrate[i] -
1380 oxcf->target_bitrate[i-1]) * 1000 /
1381 (lc->frame_rate - prev_layer_frame_rate);
1383 lc->active_worst_quality = oxcf->worst_allowed_q;
1384 lc->active_best_quality = oxcf->best_allowed_q;
1385 lc->avg_frame_qindex = oxcf->worst_allowed_q;
1387 prev_layer_frame_rate = lc->frame_rate;
1392 void vp8_change_config(VP8_COMP *cpi, VP8_CONFIG *oxcf)
1394 VP8_COMMON *cm = &cpi->common;
1403 #if CONFIG_MULTITHREAD
1404 /* wait for the last picture loopfilter thread done */
1405 if (cpi->b_lpf_running)
1407 sem_wait(&cpi->h_event_end_lpf);
1408 cpi->b_lpf_running = 0;
1412 if (cm->version != oxcf->Version)
1414 cm->version = oxcf->Version;
1415 vp8_setup_version(cm);
1418 last_w = cpi->oxcf.Width;
1419 last_h = cpi->oxcf.Height;
1423 switch (cpi->oxcf.Mode)
1428 cpi->compressor_speed = 2;
1430 if (cpi->oxcf.cpu_used < -16)
1432 cpi->oxcf.cpu_used = -16;
1435 if (cpi->oxcf.cpu_used > 16)
1436 cpi->oxcf.cpu_used = 16;
1440 case MODE_GOODQUALITY:
1442 cpi->compressor_speed = 1;
1444 if (cpi->oxcf.cpu_used < -5)
1446 cpi->oxcf.cpu_used = -5;
1449 if (cpi->oxcf.cpu_used > 5)
1450 cpi->oxcf.cpu_used = 5;
1454 case MODE_BESTQUALITY:
1456 cpi->compressor_speed = 0;
1459 case MODE_FIRSTPASS:
1461 cpi->compressor_speed = 1;
1463 case MODE_SECONDPASS:
1465 cpi->compressor_speed = 1;
1467 if (cpi->oxcf.cpu_used < -5)
1469 cpi->oxcf.cpu_used = -5;
1472 if (cpi->oxcf.cpu_used > 5)
1473 cpi->oxcf.cpu_used = 5;
1476 case MODE_SECONDPASS_BEST:
1478 cpi->compressor_speed = 0;
1483 cpi->auto_worst_q = 1;
1485 cpi->oxcf.worst_allowed_q = q_trans[oxcf->worst_allowed_q];
1486 cpi->oxcf.best_allowed_q = q_trans[oxcf->best_allowed_q];
1487 cpi->oxcf.cq_level = q_trans[cpi->oxcf.cq_level];
1489 if (oxcf->fixed_q >= 0)
1491 if (oxcf->worst_allowed_q < 0)
1492 cpi->oxcf.fixed_q = q_trans[0];
1494 cpi->oxcf.fixed_q = q_trans[oxcf->worst_allowed_q];
1496 if (oxcf->alt_q < 0)
1497 cpi->oxcf.alt_q = q_trans[0];
1499 cpi->oxcf.alt_q = q_trans[oxcf->alt_q];
1501 if (oxcf->key_q < 0)
1502 cpi->oxcf.key_q = q_trans[0];
1504 cpi->oxcf.key_q = q_trans[oxcf->key_q];
1506 if (oxcf->gold_q < 0)
1507 cpi->oxcf.gold_q = q_trans[0];
1509 cpi->oxcf.gold_q = q_trans[oxcf->gold_q];
1513 cpi->baseline_gf_interval =
1514 cpi->oxcf.alt_freq ? cpi->oxcf.alt_freq : DEFAULT_GF_INTERVAL;
1516 cpi->ref_frame_flags = VP8_ALT_FLAG | VP8_GOLD_FLAG | VP8_LAST_FLAG;
1518 //cpi->use_golden_frame_only = 0;
1519 //cpi->use_last_frame_only = 0;
1520 cm->refresh_golden_frame = 0;
1521 cm->refresh_last_frame = 1;
1522 cm->refresh_entropy_probs = 1;
1524 #if (CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
1525 cpi->oxcf.token_partitions = 3;
1528 if (cpi->oxcf.token_partitions >= 0 && cpi->oxcf.token_partitions <= 3)
1529 cm->multi_token_partition =
1530 (TOKEN_PARTITION) cpi->oxcf.token_partitions;
1532 setup_features(cpi);
1537 for (i = 0; i < MAX_MB_SEGMENTS; i++)
1538 cpi->segment_encode_breakout[i] = cpi->oxcf.encode_breakout;
1541 // At the moment the first order values may not be > MAXQ
1542 if (cpi->oxcf.fixed_q > MAXQ)
1543 cpi->oxcf.fixed_q = MAXQ;
1545 // local file playback mode == really big buffer
1546 if (cpi->oxcf.end_usage == USAGE_LOCAL_FILE_PLAYBACK)
1548 cpi->oxcf.starting_buffer_level = 60000;
1549 cpi->oxcf.optimal_buffer_level = 60000;
1550 cpi->oxcf.maximum_buffer_size = 240000;
1551 cpi->oxcf.starting_buffer_level_in_ms = 60000;
1552 cpi->oxcf.optimal_buffer_level_in_ms = 60000;
1553 cpi->oxcf.maximum_buffer_size_in_ms = 240000;
1556 // Convert target bandwidth from Kbit/s to Bit/s
1557 cpi->oxcf.target_bandwidth *= 1000;
1559 cpi->oxcf.starting_buffer_level =
1560 rescale(cpi->oxcf.starting_buffer_level,
1561 cpi->oxcf.target_bandwidth, 1000);
1563 // Set or reset optimal and maximum buffer levels.
1564 if (cpi->oxcf.optimal_buffer_level == 0)
1565 cpi->oxcf.optimal_buffer_level = cpi->oxcf.target_bandwidth / 8;
1567 cpi->oxcf.optimal_buffer_level =
1568 rescale(cpi->oxcf.optimal_buffer_level,
1569 cpi->oxcf.target_bandwidth, 1000);
1571 if (cpi->oxcf.maximum_buffer_size == 0)
1572 cpi->oxcf.maximum_buffer_size = cpi->oxcf.target_bandwidth / 8;
1574 cpi->oxcf.maximum_buffer_size =
1575 rescale(cpi->oxcf.maximum_buffer_size,
1576 cpi->oxcf.target_bandwidth, 1000);
1578 // Set up frame rate and related parameters rate control values.
1579 vp8_new_frame_rate(cpi, cpi->frame_rate);
1581 // Set absolute upper and lower quality limits
1582 cpi->worst_quality = cpi->oxcf.worst_allowed_q;
1583 cpi->best_quality = cpi->oxcf.best_allowed_q;
1585 // active values should only be modified if out of new range
1586 if (cpi->active_worst_quality > cpi->oxcf.worst_allowed_q)
1588 cpi->active_worst_quality = cpi->oxcf.worst_allowed_q;
1591 else if (cpi->active_worst_quality < cpi->oxcf.best_allowed_q)
1593 cpi->active_worst_quality = cpi->oxcf.best_allowed_q;
1595 if (cpi->active_best_quality < cpi->oxcf.best_allowed_q)
1597 cpi->active_best_quality = cpi->oxcf.best_allowed_q;
1600 else if (cpi->active_best_quality > cpi->oxcf.worst_allowed_q)
1602 cpi->active_best_quality = cpi->oxcf.worst_allowed_q;
1605 cpi->buffered_mode = cpi->oxcf.optimal_buffer_level > 0;
1607 cpi->cq_target_quality = cpi->oxcf.cq_level;
1609 // Only allow dropped frames in buffered mode
1610 cpi->drop_frames_allowed = cpi->oxcf.allow_df && cpi->buffered_mode;
1612 if (!cm->use_bilinear_mc_filter)
1613 cm->mcomp_filter_type = SIXTAP;
1615 cm->mcomp_filter_type = BILINEAR;
1617 cpi->target_bandwidth = cpi->oxcf.target_bandwidth;
1620 cm->Width = cpi->oxcf.Width;
1621 cm->Height = cpi->oxcf.Height;
1623 /* TODO(jkoleszar): if an internal spatial resampling is active,
1624 * and we downsize the input image, maybe we should clear the
1625 * internal scale immediately rather than waiting for it to
1629 // VP8 sharpness level mapping 0-7 (vs 0-10 in general VPx dialogs)
1630 if (cpi->oxcf.Sharpness > 7)
1631 cpi->oxcf.Sharpness = 7;
1633 cm->sharpness_level = cpi->oxcf.Sharpness;
1635 if (cm->horiz_scale != NORMAL || cm->vert_scale != NORMAL)
1637 int UNINITIALIZED_IS_SAFE(hr), UNINITIALIZED_IS_SAFE(hs);
1638 int UNINITIALIZED_IS_SAFE(vr), UNINITIALIZED_IS_SAFE(vs);
1640 Scale2Ratio(cm->horiz_scale, &hr, &hs);
1641 Scale2Ratio(cm->vert_scale, &vr, &vs);
1643 // always go to the next whole number
1644 cm->Width = (hs - 1 + cpi->oxcf.Width * hr) / hs;
1645 cm->Height = (vs - 1 + cpi->oxcf.Height * vr) / vs;
1648 if (last_w != cpi->oxcf.Width || last_h != cpi->oxcf.Height)
1649 cpi->force_next_frame_intra = 1;
1651 if (((cm->Width + 15) & 0xfffffff0) !=
1652 cm->yv12_fb[cm->lst_fb_idx].y_width ||
1653 ((cm->Height + 15) & 0xfffffff0) !=
1654 cm->yv12_fb[cm->lst_fb_idx].y_height ||
1655 cm->yv12_fb[cm->lst_fb_idx].y_width == 0)
1657 alloc_raw_frame_buffers(cpi);
1658 vp8_alloc_compressor_data(cpi);
1661 if (cpi->oxcf.fixed_q >= 0)
1663 cpi->last_q[0] = cpi->oxcf.fixed_q;
1664 cpi->last_q[1] = cpi->oxcf.fixed_q;
1667 cpi->Speed = cpi->oxcf.cpu_used;
1669 // force to allowlag to 0 if lag_in_frames is 0;
1670 if (cpi->oxcf.lag_in_frames == 0)
1672 cpi->oxcf.allow_lag = 0;
1674 // Limit on lag buffers as these are not currently dynamically allocated
1675 else if (cpi->oxcf.lag_in_frames > MAX_LAG_BUFFERS)
1676 cpi->oxcf.lag_in_frames = MAX_LAG_BUFFERS;
1679 cpi->alt_ref_source = NULL;
1680 cpi->is_src_frame_alt_ref = 0;
1682 #if CONFIG_TEMPORAL_DENOISING
1683 if (cpi->oxcf.noise_sensitivity)
1685 if (!cpi->denoiser.yv12_mc_running_avg.buffer_alloc)
1687 int width = (cpi->oxcf.Width + 15) & ~15;
1688 int height = (cpi->oxcf.Height + 15) & ~15;
1689 vp8_denoiser_allocate(&cpi->denoiser, width, height);
1695 // Experimental RD Code
1696 cpi->frame_distortion = 0;
1697 cpi->last_frame_distortion = 0;
1702 #define M_LOG2_E 0.693147180559945309417
1703 #define log2f(x) (log (x) / (float) M_LOG2_E)
1704 static void cal_mvsadcosts(int *mvsadcost[2])
1708 mvsadcost [0] [0] = 300;
1709 mvsadcost [1] [0] = 300;
1713 double z = 256 * (2 * (log2f(8 * i) + .6));
1714 mvsadcost [0][i] = (int) z;
1715 mvsadcost [1][i] = (int) z;
1716 mvsadcost [0][-i] = (int) z;
1717 mvsadcost [1][-i] = (int) z;
1719 while (++i <= mvfp_max);
1722 struct VP8_COMP* vp8_create_compressor(VP8_CONFIG *oxcf)
1729 cpi = vpx_memalign(32, sizeof(VP8_COMP));
1730 // Check that the CPI instance is valid
1736 vpx_memset(cpi, 0, sizeof(VP8_COMP));
1738 if (setjmp(cm->error.jmp))
1740 cpi->common.error.setjmp = 0;
1741 vp8_remove_compressor(&cpi);
1745 cpi->common.error.setjmp = 1;
1747 CHECK_MEM_ERROR(cpi->mb.ss, vpx_calloc(sizeof(search_site), (MAX_MVSEARCH_STEPS * 8) + 1));
1749 vp8_create_common(&cpi->common);
1751 init_config(cpi, oxcf);
1753 memcpy(cpi->base_skip_false_prob, vp8cx_base_skip_false_prob, sizeof(vp8cx_base_skip_false_prob));
1754 cpi->common.current_video_frame = 0;
1755 cpi->kf_overspend_bits = 0;
1756 cpi->kf_bitrate_adjustment = 0;
1757 cpi->frames_till_gf_update_due = 0;
1758 cpi->gf_overspend_bits = 0;
1759 cpi->non_gf_bitrate_adjustment = 0;
1760 cpi->prob_last_coded = 128;
1761 cpi->prob_gf_coded = 128;
1762 cpi->prob_intra_coded = 63;
1764 // Prime the recent reference frame usage counters.
1765 // Hereafter they will be maintained as a sort of moving average
1766 cpi->recent_ref_frame_usage[INTRA_FRAME] = 1;
1767 cpi->recent_ref_frame_usage[LAST_FRAME] = 1;
1768 cpi->recent_ref_frame_usage[GOLDEN_FRAME] = 1;
1769 cpi->recent_ref_frame_usage[ALTREF_FRAME] = 1;
1771 // Set reference frame sign bias for ALTREF frame to 1 (for now)
1772 cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = 1;
1774 cpi->twopass.gf_decay_rate = 0;
1775 cpi->baseline_gf_interval = DEFAULT_GF_INTERVAL;
1777 cpi->gold_is_last = 0 ;
1778 cpi->alt_is_last = 0 ;
1779 cpi->gold_is_alt = 0 ;
1781 // allocate memory for storing last frame's MVs for MV prediction.
1782 CHECK_MEM_ERROR(cpi->lfmv, vpx_calloc((cpi->common.mb_rows+2) * (cpi->common.mb_cols+2), sizeof(int_mv)));
1783 CHECK_MEM_ERROR(cpi->lf_ref_frame_sign_bias, vpx_calloc((cpi->common.mb_rows+2) * (cpi->common.mb_cols+2), sizeof(int)));
1784 CHECK_MEM_ERROR(cpi->lf_ref_frame, vpx_calloc((cpi->common.mb_rows+2) * (cpi->common.mb_cols+2), sizeof(int)));
1786 // Create the encoder segmentation map and set all entries to 0
1787 CHECK_MEM_ERROR(cpi->segmentation_map, vpx_calloc(cpi->common.mb_rows * cpi->common.mb_cols, 1));
1788 CHECK_MEM_ERROR(cpi->active_map, vpx_calloc(cpi->common.mb_rows * cpi->common.mb_cols, 1));
1789 vpx_memset(cpi->active_map , 1, (cpi->common.mb_rows * cpi->common.mb_cols));
1790 cpi->active_map_enabled = 0;
1793 // Experimental code for lagged and one pass
1794 // Initialise one_pass GF frames stats
1795 // Update stats used for GF selection
1798 cpi->one_pass_frame_index = 0;
1800 for (i = 0; i < MAX_LAG_BUFFERS; i++)
1802 cpi->one_pass_frame_stats[i].frames_so_far = 0;
1803 cpi->one_pass_frame_stats[i].frame_intra_error = 0.0;
1804 cpi->one_pass_frame_stats[i].frame_coded_error = 0.0;
1805 cpi->one_pass_frame_stats[i].frame_pcnt_inter = 0.0;
1806 cpi->one_pass_frame_stats[i].frame_pcnt_motion = 0.0;
1807 cpi->one_pass_frame_stats[i].frame_mvr = 0.0;
1808 cpi->one_pass_frame_stats[i].frame_mvr_abs = 0.0;
1809 cpi->one_pass_frame_stats[i].frame_mvc = 0.0;
1810 cpi->one_pass_frame_stats[i].frame_mvc_abs = 0.0;
1815 // Should we use the cyclic refresh method.
1816 // Currently this is tied to error resilliant mode
1817 cpi->cyclic_refresh_mode_enabled = cpi->oxcf.error_resilient_mode;
1818 cpi->cyclic_refresh_mode_max_mbs_perframe = (cpi->common.mb_rows * cpi->common.mb_cols) / 40;
1819 cpi->cyclic_refresh_mode_index = 0;
1820 cpi->cyclic_refresh_q = 32;
1822 if (cpi->cyclic_refresh_mode_enabled)
1824 CHECK_MEM_ERROR(cpi->cyclic_refresh_map, vpx_calloc((cpi->common.mb_rows * cpi->common.mb_cols), 1));
1827 cpi->cyclic_refresh_map = (signed char *) NULL;
1829 // Test function for segmentation
1830 //segmentation_test_function( cpi);
1832 #ifdef ENTROPY_STATS
1833 init_context_counters();
1836 /*Initialize the feed-forward activity masking.*/
1837 cpi->activity_avg = 90<<12;
1839 cpi->frames_since_key = 8; // Give a sensible default for the first frame.
1840 cpi->key_frame_frequency = cpi->oxcf.key_freq;
1841 cpi->this_key_frame_forced = 0;
1842 cpi->next_key_frame_forced = 0;
1844 cpi->source_alt_ref_pending = 0;
1845 cpi->source_alt_ref_active = 0;
1846 cpi->common.refresh_alt_ref_frame = 0;
1848 cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
1849 #if CONFIG_INTERNAL_STATS
1850 cpi->b_calculate_ssimg = 0;
1855 if (cpi->b_calculate_psnr)
1857 cpi->total_sq_error = 0.0;
1858 cpi->total_sq_error2 = 0.0;
1863 cpi->totalp_y = 0.0;
1864 cpi->totalp_u = 0.0;
1865 cpi->totalp_v = 0.0;
1867 cpi->tot_recode_hits = 0;
1868 cpi->summed_quality = 0;
1869 cpi->summed_weights = 0;
1872 if (cpi->b_calculate_ssimg)
1874 cpi->total_ssimg_y = 0;
1875 cpi->total_ssimg_u = 0;
1876 cpi->total_ssimg_v = 0;
1877 cpi->total_ssimg_all = 0;
1883 #define LLONG_MAX 9223372036854775807LL
1885 cpi->first_time_stamp_ever = LLONG_MAX;
1887 cpi->frames_till_gf_update_due = 0;
1888 cpi->key_frame_count = 1;
1890 cpi->ni_av_qi = cpi->oxcf.worst_allowed_q;
1893 cpi->total_byte_count = 0;
1895 cpi->drop_frame = 0;
1896 cpi->drop_count = 0;
1897 cpi->max_drop_count = 0;
1898 cpi->max_consec_dropped_frames = 4;
1900 cpi->rate_correction_factor = 1.0;
1901 cpi->key_frame_rate_correction_factor = 1.0;
1902 cpi->gf_rate_correction_factor = 1.0;
1903 cpi->twopass.est_max_qcorrection_factor = 1.0;
1905 cpi->mb.mvcost[0] = &cpi->mb.mvcosts[0][mv_max+1];
1906 cpi->mb.mvcost[1] = &cpi->mb.mvcosts[1][mv_max+1];
1907 cpi->mb.mvsadcost[0] = &cpi->mb.mvsadcosts[0][mvfp_max+1];
1908 cpi->mb.mvsadcost[1] = &cpi->mb.mvsadcosts[1][mvfp_max+1];
1910 cal_mvsadcosts(cpi->mb.mvsadcost);
1912 for (i = 0; i < KEY_FRAME_CONTEXT; i++)
1914 cpi->prior_key_frame_distance[i] = (int)cpi->output_frame_rate;
1917 #ifdef OUTPUT_YUV_SRC
1918 yuv_file = fopen("bd.yuv", "ab");
1922 framepsnr = fopen("framepsnr.stt", "a");
1923 kf_list = fopen("kf_list.stt", "w");
1926 cpi->output_pkt_list = oxcf->output_pkt_list;
1928 #if !(CONFIG_REALTIME_ONLY)
1932 vp8_init_first_pass(cpi);
1934 else if (cpi->pass == 2)
1936 size_t packet_sz = sizeof(FIRSTPASS_STATS);
1937 int packets = oxcf->two_pass_stats_in.sz / packet_sz;
1939 cpi->twopass.stats_in_start = oxcf->two_pass_stats_in.buf;
1940 cpi->twopass.stats_in = cpi->twopass.stats_in_start;
1941 cpi->twopass.stats_in_end = (void*)((char *)cpi->twopass.stats_in
1942 + (packets - 1) * packet_sz);
1943 vp8_init_second_pass(cpi);
1948 if (cpi->compressor_speed == 2)
1950 cpi->cpu_freq = 0; //vp8_get_processor_freq();
1951 cpi->avg_encode_time = 0;
1952 cpi->avg_pick_mode_time = 0;
1955 vp8_set_speed_features(cpi);
1957 // Set starting values of RD threshold multipliers (128 = *1)
1958 for (i = 0; i < MAX_MODES; i++)
1960 cpi->rd_thresh_mult[i] = 128;
1963 #ifdef ENTROPY_STATS
1964 init_mv_ref_counts();
1967 #if CONFIG_MULTITHREAD
1968 vp8cx_create_encoder_threads(cpi);
1971 cpi->fn_ptr[BLOCK_16X16].sdf = vp8_sad16x16;
1972 cpi->fn_ptr[BLOCK_16X16].vf = vp8_variance16x16;
1973 cpi->fn_ptr[BLOCK_16X16].svf = vp8_sub_pixel_variance16x16;
1974 cpi->fn_ptr[BLOCK_16X16].svf_halfpix_h = vp8_variance_halfpixvar16x16_h;
1975 cpi->fn_ptr[BLOCK_16X16].svf_halfpix_v = vp8_variance_halfpixvar16x16_v;
1976 cpi->fn_ptr[BLOCK_16X16].svf_halfpix_hv = vp8_variance_halfpixvar16x16_hv;
1977 cpi->fn_ptr[BLOCK_16X16].sdx3f = vp8_sad16x16x3;
1978 cpi->fn_ptr[BLOCK_16X16].sdx8f = vp8_sad16x16x8;
1979 cpi->fn_ptr[BLOCK_16X16].sdx4df = vp8_sad16x16x4d;
1981 cpi->fn_ptr[BLOCK_16X8].sdf = vp8_sad16x8;
1982 cpi->fn_ptr[BLOCK_16X8].vf = vp8_variance16x8;
1983 cpi->fn_ptr[BLOCK_16X8].svf = vp8_sub_pixel_variance16x8;
1984 cpi->fn_ptr[BLOCK_16X8].svf_halfpix_h = NULL;
1985 cpi->fn_ptr[BLOCK_16X8].svf_halfpix_v = NULL;
1986 cpi->fn_ptr[BLOCK_16X8].svf_halfpix_hv = NULL;
1987 cpi->fn_ptr[BLOCK_16X8].sdx3f = vp8_sad16x8x3;
1988 cpi->fn_ptr[BLOCK_16X8].sdx8f = vp8_sad16x8x8;
1989 cpi->fn_ptr[BLOCK_16X8].sdx4df = vp8_sad16x8x4d;
1991 cpi->fn_ptr[BLOCK_8X16].sdf = vp8_sad8x16;
1992 cpi->fn_ptr[BLOCK_8X16].vf = vp8_variance8x16;
1993 cpi->fn_ptr[BLOCK_8X16].svf = vp8_sub_pixel_variance8x16;
1994 cpi->fn_ptr[BLOCK_8X16].svf_halfpix_h = NULL;
1995 cpi->fn_ptr[BLOCK_8X16].svf_halfpix_v = NULL;
1996 cpi->fn_ptr[BLOCK_8X16].svf_halfpix_hv = NULL;
1997 cpi->fn_ptr[BLOCK_8X16].sdx3f = vp8_sad8x16x3;
1998 cpi->fn_ptr[BLOCK_8X16].sdx8f = vp8_sad8x16x8;
1999 cpi->fn_ptr[BLOCK_8X16].sdx4df = vp8_sad8x16x4d;
2001 cpi->fn_ptr[BLOCK_8X8].sdf = vp8_sad8x8;
2002 cpi->fn_ptr[BLOCK_8X8].vf = vp8_variance8x8;
2003 cpi->fn_ptr[BLOCK_8X8].svf = vp8_sub_pixel_variance8x8;
2004 cpi->fn_ptr[BLOCK_8X8].svf_halfpix_h = NULL;
2005 cpi->fn_ptr[BLOCK_8X8].svf_halfpix_v = NULL;
2006 cpi->fn_ptr[BLOCK_8X8].svf_halfpix_hv = NULL;
2007 cpi->fn_ptr[BLOCK_8X8].sdx3f = vp8_sad8x8x3;
2008 cpi->fn_ptr[BLOCK_8X8].sdx8f = vp8_sad8x8x8;
2009 cpi->fn_ptr[BLOCK_8X8].sdx4df = vp8_sad8x8x4d;
2011 cpi->fn_ptr[BLOCK_4X4].sdf = vp8_sad4x4;
2012 cpi->fn_ptr[BLOCK_4X4].vf = vp8_variance4x4;
2013 cpi->fn_ptr[BLOCK_4X4].svf = vp8_sub_pixel_variance4x4;
2014 cpi->fn_ptr[BLOCK_4X4].svf_halfpix_h = NULL;
2015 cpi->fn_ptr[BLOCK_4X4].svf_halfpix_v = NULL;
2016 cpi->fn_ptr[BLOCK_4X4].svf_halfpix_hv = NULL;
2017 cpi->fn_ptr[BLOCK_4X4].sdx3f = vp8_sad4x4x3;
2018 cpi->fn_ptr[BLOCK_4X4].sdx8f = vp8_sad4x4x8;
2019 cpi->fn_ptr[BLOCK_4X4].sdx4df = vp8_sad4x4x4d;
2021 #if ARCH_X86 || ARCH_X86_64
2022 cpi->fn_ptr[BLOCK_16X16].copymem = vp8_copy32xn;
2023 cpi->fn_ptr[BLOCK_16X8].copymem = vp8_copy32xn;
2024 cpi->fn_ptr[BLOCK_8X16].copymem = vp8_copy32xn;
2025 cpi->fn_ptr[BLOCK_8X8].copymem = vp8_copy32xn;
2026 cpi->fn_ptr[BLOCK_4X4].copymem = vp8_copy32xn;
2029 cpi->full_search_sad = vp8_full_search_sad;
2030 cpi->diamond_search_sad = vp8_diamond_search_sad;
2031 cpi->refining_search_sad = vp8_refining_search_sad;
2033 // make sure frame 1 is okay
2034 cpi->error_bins[0] = cpi->common.MBs;
2036 //vp8cx_init_quantizer() is first called here. Add check in vp8cx_frame_init_quantizer() so that vp8cx_init_quantizer is only called later
2037 //when needed. This will avoid unnecessary calls of vp8cx_init_quantizer() for every frame.
2038 vp8cx_init_quantizer(cpi);
2040 vp8_loop_filter_init(cm);
2042 cpi->common.error.setjmp = 0;
2044 #if CONFIG_MULTI_RES_ENCODING
2045 /* Calculate # of MBs in a row in lower-resolution level image. */
2046 if (cpi->oxcf.mr_encoder_id > 0)
2047 vp8_cal_low_res_mb_cols(cpi);
2055 void vp8_remove_compressor(VP8_COMP **ptr)
2057 VP8_COMP *cpi = *ptr;
2062 if (cpi && (cpi->common.current_video_frame > 0))
2064 #if !(CONFIG_REALTIME_ONLY)
2068 vp8_end_second_pass(cpi);
2073 #ifdef ENTROPY_STATS
2074 print_context_counters();
2075 print_tree_update_probs();
2076 print_mode_context();
2079 #if CONFIG_INTERNAL_STATS
2083 FILE *f = fopen("opsnr.stt", "a");
2084 double time_encoded = (cpi->last_end_time_stamp_seen
2085 - cpi->first_time_stamp_ever) / 10000000.000;
2086 double total_encode_time = (cpi->time_receive_data +
2087 cpi->time_compress_data) / 1000.000;
2088 double dr = (double)cpi->bytes * 8.0 / 1000.0 / time_encoded;
2090 if (cpi->b_calculate_psnr)
2092 YV12_BUFFER_CONFIG *lst_yv12 =
2093 &cpi->common.yv12_fb[cpi->common.lst_fb_idx];
2095 if (cpi->oxcf.number_of_layers > 1)
2099 fprintf(f, "Layer\tBitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\t"
2100 "GLPsnrP\tVPXSSIM\t\n");
2101 for (i=0; i<cpi->oxcf.number_of_layers; i++)
2103 double dr = (double)cpi->bytes_in_layer[i] *
2104 8.0 / 1000.0 / time_encoded;
2105 double samples = 3.0 / 2 * cpi->frames_in_layer[i] *
2106 lst_yv12->y_width * lst_yv12->y_height;
2107 double total_psnr = vp8_mse2psnr(samples, 255.0,
2108 cpi->total_error2[i]);
2109 double total_psnr2 = vp8_mse2psnr(samples, 255.0,
2110 cpi->total_error2_p[i]);
2111 double total_ssim = 100 * pow(cpi->sum_ssim[i] /
2112 cpi->sum_weights[i], 8.0);
2114 fprintf(f, "%5d\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
2117 cpi->sum_psnr[i] / cpi->frames_in_layer[i],
2119 cpi->sum_psnr_p[i] / cpi->frames_in_layer[i],
2120 total_psnr2, total_ssim);
2125 double samples = 3.0 / 2 * cpi->count *
2126 lst_yv12->y_width * lst_yv12->y_height;
2127 double total_psnr = vp8_mse2psnr(samples, 255.0,
2128 cpi->total_sq_error);
2129 double total_psnr2 = vp8_mse2psnr(samples, 255.0,
2130 cpi->total_sq_error2);
2131 double total_ssim = 100 * pow(cpi->summed_quality /
2132 cpi->summed_weights, 8.0);
2134 fprintf(f, "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\t"
2135 "GLPsnrP\tVPXSSIM\t Time(us)\n");
2136 fprintf(f, "%7.3f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
2138 dr, cpi->total / cpi->count, total_psnr,
2139 cpi->totalp / cpi->count, total_psnr2,
2140 total_ssim, total_encode_time);
2144 if (cpi->b_calculate_ssimg)
2146 if (cpi->oxcf.number_of_layers > 1)
2150 fprintf(f, "Layer\tBitRate\tSSIM_Y\tSSIM_U\tSSIM_V\tSSIM_A\t"
2152 for (i=0; i<cpi->oxcf.number_of_layers; i++)
2154 double dr = (double)cpi->bytes_in_layer[i] *
2155 8.0 / 1000.0 / time_encoded;
2156 fprintf(f, "%5d\t%7.3f\t%6.4f\t"
2157 "%6.4f\t%6.4f\t%6.4f\t%8.0f\n",
2159 cpi->total_ssimg_y_in_layer[i] /
2160 cpi->frames_in_layer[i],
2161 cpi->total_ssimg_u_in_layer[i] /
2162 cpi->frames_in_layer[i],
2163 cpi->total_ssimg_v_in_layer[i] /
2164 cpi->frames_in_layer[i],
2165 cpi->total_ssimg_all_in_layer[i] /
2166 cpi->frames_in_layer[i],
2172 fprintf(f, "BitRate\tSSIM_Y\tSSIM_U\tSSIM_V\tSSIM_A\t"
2174 fprintf(f, "%7.3f\t%6.4f\t%6.4f\t%6.4f\t%6.4f\t%8.0f\n", dr,
2175 cpi->total_ssimg_y / cpi->count,
2176 cpi->total_ssimg_u / cpi->count,
2177 cpi->total_ssimg_v / cpi->count,
2178 cpi->total_ssimg_all / cpi->count, total_encode_time);
2184 f = fopen("qskip.stt", "a");
2185 fprintf(f, "minq:%d -maxq:%d skiptrue:skipfalse = %d:%d\n", cpi->oxcf.best_allowed_q, cpi->oxcf.worst_allowed_q, skiptruecount, skipfalsecount);
2196 if (cpi->compressor_speed == 2)
2199 FILE *f = fopen("cxspeed.stt", "a");
2200 cnt_pm /= cpi->common.MBs;
2202 for (i = 0; i < 16; i++)
2203 fprintf(f, "%5d", frames_at_speed[i]);
2206 //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);
2215 extern int count_mb_seg[4];
2216 FILE *f = fopen("modes.stt", "a");
2217 double dr = (double)cpi->frame_rate * (double)bytes * (double)8 / (double)count / (double)1000 ;
2218 fprintf(f, "intra_mode in Intra Frames:\n");
2219 fprintf(f, "Y: %8d, %8d, %8d, %8d, %8d\n", y_modes[0], y_modes[1], y_modes[2], y_modes[3], y_modes[4]);
2220 fprintf(f, "UV:%8d, %8d, %8d, %8d\n", uv_modes[0], uv_modes[1], uv_modes[2], uv_modes[3]);
2225 for (i = 0; i < 10; i++)
2226 fprintf(f, "%8d, ", b_modes[i]);
2232 fprintf(f, "Modes in Inter Frames:\n");
2233 fprintf(f, "Y: %8d, %8d, %8d, %8d, %8d, %8d, %8d, %8d, %8d, %8d\n",
2234 inter_y_modes[0], inter_y_modes[1], inter_y_modes[2], inter_y_modes[3], inter_y_modes[4],
2235 inter_y_modes[5], inter_y_modes[6], inter_y_modes[7], inter_y_modes[8], inter_y_modes[9]);
2236 fprintf(f, "UV:%8d, %8d, %8d, %8d\n", inter_uv_modes[0], inter_uv_modes[1], inter_uv_modes[2], inter_uv_modes[3]);
2241 for (i = 0; i < 15; i++)
2242 fprintf(f, "%8d, ", inter_b_modes[i]);
2247 fprintf(f, "P:%8d, %8d, %8d, %8d\n", count_mb_seg[0], count_mb_seg[1], count_mb_seg[2], count_mb_seg[3]);
2248 fprintf(f, "PB:%8d, %8d, %8d, %8d\n", inter_b_modes[LEFT4X4], inter_b_modes[ABOVE4X4], inter_b_modes[ZERO4X4], inter_b_modes[NEW4X4]);
2256 #ifdef ENTROPY_STATS
2259 FILE *fmode = fopen("modecontext.c", "w");
2261 fprintf(fmode, "\n#include \"entropymode.h\"\n\n");
2262 fprintf(fmode, "const unsigned int vp8_kf_default_bmode_counts ");
2263 fprintf(fmode, "[VP8_BINTRAMODES] [VP8_BINTRAMODES] [VP8_BINTRAMODES] =\n{\n");
2265 for (i = 0; i < 10; i++)
2268 fprintf(fmode, " { //Above Mode : %d\n", i);
2270 for (j = 0; j < 10; j++)
2273 fprintf(fmode, " {");
2275 for (k = 0; k < 10; k++)
2277 if (!intra_mode_stats[i][j][k])
2278 fprintf(fmode, " %5d, ", 1);
2280 fprintf(fmode, " %5d, ", intra_mode_stats[i][j][k]);
2283 fprintf(fmode, "}, // left_mode %d\n", j);
2287 fprintf(fmode, " },\n");
2291 fprintf(fmode, "};\n");
2297 #if defined(SECTIONBITS_OUTPUT)
2302 FILE *f = fopen("tokenbits.stt", "a");
2304 for (i = 0; i < 28; i++)
2305 fprintf(f, "%8d", (int)(Sectionbits[i] / 256));
2315 printf("\n_pick_loop_filter_level:%d\n", cpi->time_pick_lpf / 1000);
2316 printf("\n_frames recive_data encod_mb_row compress_frame Total\n");
2317 printf("%6d %10ld %10ld %10ld %10ld\n", cpi->common.current_video_frame, cpi->time_receive_data / 1000, cpi->time_encode_mb_row / 1000, cpi->time_compress_data / 1000, (cpi->time_receive_data + cpi->time_compress_data) / 1000);
2323 #if CONFIG_MULTITHREAD
2324 vp8cx_remove_encoder_threads(cpi);
2327 #if CONFIG_TEMPORAL_DENOISING
2328 vp8_denoiser_free(&cpi->denoiser);
2330 dealloc_compressor_data(cpi);
2331 vpx_free(cpi->mb.ss);
2333 vpx_free(cpi->cyclic_refresh_map);
2335 vp8_remove_common(&cpi->common);
2339 #ifdef OUTPUT_YUV_SRC
2359 static uint64_t calc_plane_error(unsigned char *orig, int orig_stride,
2360 unsigned char *recon, int recon_stride,
2361 unsigned int cols, unsigned int rows)
2363 unsigned int row, col;
2364 uint64_t total_sse = 0;
2367 for (row = 0; row + 16 <= rows; row += 16)
2369 for (col = 0; col + 16 <= cols; col += 16)
2373 vp8_mse16x16(orig + col, orig_stride,
2374 recon + col, recon_stride,
2379 /* Handle odd-sized width */
2382 unsigned int border_row, border_col;
2383 unsigned char *border_orig = orig;
2384 unsigned char *border_recon = recon;
2386 for (border_row = 0; border_row < 16; border_row++)
2388 for (border_col = col; border_col < cols; border_col++)
2390 diff = border_orig[border_col] - border_recon[border_col];
2391 total_sse += diff * diff;
2394 border_orig += orig_stride;
2395 border_recon += recon_stride;
2399 orig += orig_stride * 16;
2400 recon += recon_stride * 16;
2403 /* Handle odd-sized height */
2404 for (; row < rows; row++)
2406 for (col = 0; col < cols; col++)
2408 diff = orig[col] - recon[col];
2409 total_sse += diff * diff;
2412 orig += orig_stride;
2413 recon += recon_stride;
2416 vp8_clear_system_state();
2421 static void generate_psnr_packet(VP8_COMP *cpi)
2423 YV12_BUFFER_CONFIG *orig = cpi->Source;
2424 YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
2425 struct vpx_codec_cx_pkt pkt;
2428 unsigned int width = cpi->common.Width;
2429 unsigned int height = cpi->common.Height;
2431 pkt.kind = VPX_CODEC_PSNR_PKT;
2432 sse = calc_plane_error(orig->y_buffer, orig->y_stride,
2433 recon->y_buffer, recon->y_stride,
2435 pkt.data.psnr.sse[0] = sse;
2436 pkt.data.psnr.sse[1] = sse;
2437 pkt.data.psnr.samples[0] = width * height;
2438 pkt.data.psnr.samples[1] = width * height;
2440 width = (width + 1) / 2;
2441 height = (height + 1) / 2;
2443 sse = calc_plane_error(orig->u_buffer, orig->uv_stride,
2444 recon->u_buffer, recon->uv_stride,
2446 pkt.data.psnr.sse[0] += sse;
2447 pkt.data.psnr.sse[2] = sse;
2448 pkt.data.psnr.samples[0] += width * height;
2449 pkt.data.psnr.samples[2] = width * height;
2451 sse = calc_plane_error(orig->v_buffer, orig->uv_stride,
2452 recon->v_buffer, recon->uv_stride,
2454 pkt.data.psnr.sse[0] += sse;
2455 pkt.data.psnr.sse[3] = sse;
2456 pkt.data.psnr.samples[0] += width * height;
2457 pkt.data.psnr.samples[3] = width * height;
2459 for (i = 0; i < 4; i++)
2460 pkt.data.psnr.psnr[i] = vp8_mse2psnr(pkt.data.psnr.samples[i], 255.0,
2461 pkt.data.psnr.sse[i]);
2463 vpx_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
2467 int vp8_use_as_reference(VP8_COMP *cpi, int ref_frame_flags)
2469 if (ref_frame_flags > 7)
2472 cpi->ref_frame_flags = ref_frame_flags;
2475 int vp8_update_reference(VP8_COMP *cpi, int ref_frame_flags)
2477 if (ref_frame_flags > 7)
2480 cpi->common.refresh_golden_frame = 0;
2481 cpi->common.refresh_alt_ref_frame = 0;
2482 cpi->common.refresh_last_frame = 0;
2484 if (ref_frame_flags & VP8_LAST_FLAG)
2485 cpi->common.refresh_last_frame = 1;
2487 if (ref_frame_flags & VP8_GOLD_FLAG)
2488 cpi->common.refresh_golden_frame = 1;
2490 if (ref_frame_flags & VP8_ALT_FLAG)
2491 cpi->common.refresh_alt_ref_frame = 1;
2496 int vp8_get_reference(VP8_COMP *cpi, VP8_REFFRAME ref_frame_flag, YV12_BUFFER_CONFIG *sd)
2498 VP8_COMMON *cm = &cpi->common;
2501 if (ref_frame_flag == VP8_LAST_FLAG)
2502 ref_fb_idx = cm->lst_fb_idx;
2503 else if (ref_frame_flag == VP8_GOLD_FLAG)
2504 ref_fb_idx = cm->gld_fb_idx;
2505 else if (ref_frame_flag == VP8_ALT_FLAG)
2506 ref_fb_idx = cm->alt_fb_idx;
2510 vp8_yv12_copy_frame(&cm->yv12_fb[ref_fb_idx], sd);
2514 int vp8_set_reference(VP8_COMP *cpi, VP8_REFFRAME ref_frame_flag, YV12_BUFFER_CONFIG *sd)
2516 VP8_COMMON *cm = &cpi->common;
2520 if (ref_frame_flag == VP8_LAST_FLAG)
2521 ref_fb_idx = cm->lst_fb_idx;
2522 else if (ref_frame_flag == VP8_GOLD_FLAG)
2523 ref_fb_idx = cm->gld_fb_idx;
2524 else if (ref_frame_flag == VP8_ALT_FLAG)
2525 ref_fb_idx = cm->alt_fb_idx;
2529 vp8_yv12_copy_frame(sd, &cm->yv12_fb[ref_fb_idx]);
2533 int vp8_update_entropy(VP8_COMP *cpi, int update)
2535 VP8_COMMON *cm = &cpi->common;
2536 cm->refresh_entropy_probs = update;
2543 void vp8_write_yuv_frame(const char *name, YV12_BUFFER_CONFIG *s)
2545 FILE *yuv_file = fopen(name, "ab");
2546 unsigned char *src = s->y_buffer;
2547 int h = s->y_height;
2551 fwrite(src, s->y_width, 1, yuv_file);
2561 fwrite(src, s->uv_width, 1, yuv_file);
2562 src += s->uv_stride;
2571 fwrite(src, s->uv_width, 1, yuv_file);
2572 src += s->uv_stride;
2581 static void scale_and_extend_source(YV12_BUFFER_CONFIG *sd, VP8_COMP *cpi)
2583 VP8_COMMON *cm = &cpi->common;
2585 // are we resizing the image
2586 if (cm->horiz_scale != 0 || cm->vert_scale != 0)
2588 #if CONFIG_SPATIAL_RESAMPLING
2589 int UNINITIALIZED_IS_SAFE(hr), UNINITIALIZED_IS_SAFE(hs);
2590 int UNINITIALIZED_IS_SAFE(vr), UNINITIALIZED_IS_SAFE(vs);
2593 if (cm->vert_scale == 3)
2598 Scale2Ratio(cm->horiz_scale, &hr, &hs);
2599 Scale2Ratio(cm->vert_scale, &vr, &vs);
2601 vp8_scale_frame(sd, &cpi->scaled_source, cm->temp_scale_frame.y_buffer,
2602 tmp_height, hs, hr, vs, vr, 0);
2604 vp8_yv12_extend_frame_borders(&cpi->scaled_source);
2605 cpi->Source = &cpi->scaled_source;
2613 static void resize_key_frame(VP8_COMP *cpi)
2615 #if CONFIG_SPATIAL_RESAMPLING
2616 VP8_COMMON *cm = &cpi->common;
2618 // Do we need to apply resampling for one pass cbr.
2619 // In one pass this is more limited than in two pass cbr
2620 // The test and any change is only made one per key frame sequence
2621 if (cpi->oxcf.allow_spatial_resampling && (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER))
2623 int UNINITIALIZED_IS_SAFE(hr), UNINITIALIZED_IS_SAFE(hs);
2624 int UNINITIALIZED_IS_SAFE(vr), UNINITIALIZED_IS_SAFE(vs);
2625 int new_width, new_height;
2627 // If we are below the resample DOWN watermark then scale down a notch.
2628 if (cpi->buffer_level < (cpi->oxcf.resample_down_water_mark * cpi->oxcf.optimal_buffer_level / 100))
2630 cm->horiz_scale = (cm->horiz_scale < ONETWO) ? cm->horiz_scale + 1 : ONETWO;
2631 cm->vert_scale = (cm->vert_scale < ONETWO) ? cm->vert_scale + 1 : ONETWO;
2633 // Should we now start scaling back up
2634 else if (cpi->buffer_level > (cpi->oxcf.resample_up_water_mark * cpi->oxcf.optimal_buffer_level / 100))
2636 cm->horiz_scale = (cm->horiz_scale > NORMAL) ? cm->horiz_scale - 1 : NORMAL;
2637 cm->vert_scale = (cm->vert_scale > NORMAL) ? cm->vert_scale - 1 : NORMAL;
2640 // Get the new hieght and width
2641 Scale2Ratio(cm->horiz_scale, &hr, &hs);
2642 Scale2Ratio(cm->vert_scale, &vr, &vs);
2643 new_width = ((hs - 1) + (cpi->oxcf.Width * hr)) / hs;
2644 new_height = ((vs - 1) + (cpi->oxcf.Height * vr)) / vs;
2646 // If the image size has changed we need to reallocate the buffers
2647 // and resample the source image
2648 if ((cm->Width != new_width) || (cm->Height != new_height))
2650 cm->Width = new_width;
2651 cm->Height = new_height;
2652 vp8_alloc_compressor_data(cpi);
2653 scale_and_extend_source(cpi->un_scaled_source, cpi);
2661 static void update_alt_ref_frame_stats(VP8_COMP *cpi)
2663 VP8_COMMON *cm = &cpi->common;
2665 // Select an interval before next GF or altref
2666 if (!cpi->auto_gold)
2667 cpi->frames_till_gf_update_due = cpi->goldfreq;
2669 if ((cpi->pass != 2) && cpi->frames_till_gf_update_due)
2671 cpi->current_gf_interval = cpi->frames_till_gf_update_due;
2673 // Set the bits per frame that we should try and recover in subsequent inter frames
2674 // to account for the extra GF spend... note that his does not apply for GF updates
2675 // that occur coincident with a key frame as the extra cost of key frames is dealt
2678 cpi->gf_overspend_bits += cpi->projected_frame_size;
2679 cpi->non_gf_bitrate_adjustment = cpi->gf_overspend_bits / cpi->frames_till_gf_update_due;
2682 // Update data structure that monitors level of reference to last GF
2683 vpx_memset(cpi->gf_active_flags, 1, (cm->mb_rows * cm->mb_cols));
2684 cpi->gf_active_count = cm->mb_rows * cm->mb_cols;
2686 // this frame refreshes means next frames don't unless specified by user
2687 cpi->common.frames_since_golden = 0;
2689 // Clear the alternate reference update pending flag.
2690 cpi->source_alt_ref_pending = 0;
2692 // Set the alternate refernce frame active flag
2693 cpi->source_alt_ref_active = 1;
2697 static void update_golden_frame_stats(VP8_COMP *cpi)
2699 VP8_COMMON *cm = &cpi->common;
2701 // Update the Golden frame usage counts.
2702 if (cm->refresh_golden_frame)
2704 // Select an interval before next GF
2705 if (!cpi->auto_gold)
2706 cpi->frames_till_gf_update_due = cpi->goldfreq;
2708 if ((cpi->pass != 2) && (cpi->frames_till_gf_update_due > 0))
2710 cpi->current_gf_interval = cpi->frames_till_gf_update_due;
2712 // Set the bits per frame that we should try and recover in subsequent inter frames
2713 // to account for the extra GF spend... note that his does not apply for GF updates
2714 // that occur coincident with a key frame as the extra cost of key frames is dealt
2716 if ((cm->frame_type != KEY_FRAME) && !cpi->source_alt_ref_active)
2718 // Calcluate GF bits to be recovered
2719 // Projected size - av frame bits available for inter frames for clip as a whole
2720 cpi->gf_overspend_bits += (cpi->projected_frame_size - cpi->inter_frame_target);
2723 cpi->non_gf_bitrate_adjustment = cpi->gf_overspend_bits / cpi->frames_till_gf_update_due;
2727 // Update data structure that monitors level of reference to last GF
2728 vpx_memset(cpi->gf_active_flags, 1, (cm->mb_rows * cm->mb_cols));
2729 cpi->gf_active_count = cm->mb_rows * cm->mb_cols;
2731 // this frame refreshes means next frames don't unless specified by user
2732 cm->refresh_golden_frame = 0;
2733 cpi->common.frames_since_golden = 0;
2735 //if ( cm->frame_type == KEY_FRAME )
2737 cpi->recent_ref_frame_usage[INTRA_FRAME] = 1;
2738 cpi->recent_ref_frame_usage[LAST_FRAME] = 1;
2739 cpi->recent_ref_frame_usage[GOLDEN_FRAME] = 1;
2740 cpi->recent_ref_frame_usage[ALTREF_FRAME] = 1;
2744 // // Carry a potrtion of count over to begining of next gf sequence
2745 // cpi->recent_ref_frame_usage[INTRA_FRAME] >>= 5;
2746 // cpi->recent_ref_frame_usage[LAST_FRAME] >>= 5;
2747 // cpi->recent_ref_frame_usage[GOLDEN_FRAME] >>= 5;
2748 // cpi->recent_ref_frame_usage[ALTREF_FRAME] >>= 5;
2751 // ******** Fixed Q test code only ************
2752 // If we are going to use the ALT reference for the next group of frames set a flag to say so.
2753 if (cpi->oxcf.fixed_q >= 0 &&
2754 cpi->oxcf.play_alternate && !cpi->common.refresh_alt_ref_frame)
2756 cpi->source_alt_ref_pending = 1;
2757 cpi->frames_till_gf_update_due = cpi->baseline_gf_interval;
2760 if (!cpi->source_alt_ref_pending)
2761 cpi->source_alt_ref_active = 0;
2763 // Decrement count down till next gf
2764 if (cpi->frames_till_gf_update_due > 0)
2765 cpi->frames_till_gf_update_due--;
2768 else if (!cpi->common.refresh_alt_ref_frame)
2770 // Decrement count down till next gf
2771 if (cpi->frames_till_gf_update_due > 0)
2772 cpi->frames_till_gf_update_due--;
2774 if (cpi->common.frames_till_alt_ref_frame)
2775 cpi->common.frames_till_alt_ref_frame --;
2777 cpi->common.frames_since_golden ++;
2779 if (cpi->common.frames_since_golden > 1)
2781 cpi->recent_ref_frame_usage[INTRA_FRAME] += cpi->count_mb_ref_frame_usage[INTRA_FRAME];
2782 cpi->recent_ref_frame_usage[LAST_FRAME] += cpi->count_mb_ref_frame_usage[LAST_FRAME];
2783 cpi->recent_ref_frame_usage[GOLDEN_FRAME] += cpi->count_mb_ref_frame_usage[GOLDEN_FRAME];
2784 cpi->recent_ref_frame_usage[ALTREF_FRAME] += cpi->count_mb_ref_frame_usage[ALTREF_FRAME];
2789 // This function updates the reference frame probability estimates that
2790 // will be used during mode selection
2791 static void update_rd_ref_frame_probs(VP8_COMP *cpi)
2793 VP8_COMMON *cm = &cpi->common;
2795 const int *const rfct = cpi->count_mb_ref_frame_usage;
2796 const int rf_intra = rfct[INTRA_FRAME];
2797 const int rf_inter = rfct[LAST_FRAME] + rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME];
2799 if (cm->frame_type == KEY_FRAME)
2801 cpi->prob_intra_coded = 255;
2802 cpi->prob_last_coded = 128;
2803 cpi->prob_gf_coded = 128;
2805 else if (!(rf_intra + rf_inter))
2807 cpi->prob_intra_coded = 63;
2808 cpi->prob_last_coded = 128;
2809 cpi->prob_gf_coded = 128;
2812 // update reference frame costs since we can do better than what we got last frame.
2813 if (cpi->oxcf.number_of_layers == 1)
2815 if (cpi->common.refresh_alt_ref_frame)
2817 cpi->prob_intra_coded += 40;
2818 cpi->prob_last_coded = 200;
2819 cpi->prob_gf_coded = 1;
2821 else if (cpi->common.frames_since_golden == 0)
2823 cpi->prob_last_coded = 214;
2825 else if (cpi->common.frames_since_golden == 1)
2827 cpi->prob_last_coded = 192;
2828 cpi->prob_gf_coded = 220;
2830 else if (cpi->source_alt_ref_active)
2832 cpi->prob_gf_coded -= 20;
2834 if (cpi->prob_gf_coded < 10)
2835 cpi->prob_gf_coded = 10;
2837 if (!cpi->source_alt_ref_active)
2838 cpi->prob_gf_coded = 255;
2843 // 1 = key, 0 = inter
2844 static int decide_key_frame(VP8_COMP *cpi)
2846 VP8_COMMON *cm = &cpi->common;
2848 int code_key_frame = 0;
2852 if (cpi->Speed > 11)
2855 // Clear down mmx registers
2856 vp8_clear_system_state(); //__asm emms;
2858 if ((cpi->compressor_speed == 2) && (cpi->Speed >= 5) && (cpi->sf.RD == 0))
2860 double change = 1.0 * abs((int)(cpi->intra_error - cpi->last_intra_error)) / (1 + cpi->last_intra_error);
2861 double change2 = 1.0 * abs((int)(cpi->prediction_error - cpi->last_prediction_error)) / (1 + cpi->last_prediction_error);
2862 double minerror = cm->MBs * 256;
2866 if (10 * cpi->intra_error / (1 + cpi->prediction_error) < 15
2867 && cpi->prediction_error > minerror
2868 && (change > .25 || change2 > .25))
2870 FILE *f = fopen("intra_inter.stt", "a");
2872 if (cpi->prediction_error <= 0)
2873 cpi->prediction_error = 1;
2875 fprintf(f, "%d %d %d %d %14.4f\n",
2876 cm->current_video_frame,
2877 (int) cpi->prediction_error,
2878 (int) cpi->intra_error,
2879 (int)((10 * cpi->intra_error) / cpi->prediction_error),
2887 cpi->last_intra_error = cpi->intra_error;
2888 cpi->last_prediction_error = cpi->prediction_error;
2890 if (10 * cpi->intra_error / (1 + cpi->prediction_error) < 15
2891 && cpi->prediction_error > minerror
2892 && (change > .25 || change2 > .25))
2894 /*(change > 1.4 || change < .75)&& cpi->this_frame_percent_intra > cpi->last_frame_percent_intra + 3*/
2902 // If the following are true we might as well code a key frame
2903 if (((cpi->this_frame_percent_intra == 100) &&
2904 (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra + 2))) ||
2905 ((cpi->this_frame_percent_intra > 95) &&
2906 (cpi->this_frame_percent_intra >= (cpi->last_frame_percent_intra + 5))))
2910 // in addition if the following are true and this is not a golden frame then code a key frame
2911 // Note that on golden frames there often seems to be a pop in intra useage anyway hence this
2912 // restriction is designed to prevent spurious key frames. The Intra pop needs to be investigated.
2913 else if (((cpi->this_frame_percent_intra > 60) &&
2914 (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra * 2))) ||
2915 ((cpi->this_frame_percent_intra > 75) &&
2916 (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra * 3 / 2))) ||
2917 ((cpi->this_frame_percent_intra > 90) &&
2918 (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra + 10))))
2920 if (!cm->refresh_golden_frame)
2924 return code_key_frame;
2928 #if !(CONFIG_REALTIME_ONLY)
2929 static void Pass1Encode(VP8_COMP *cpi, unsigned long *size, unsigned char *dest, unsigned int *frame_flags)
2934 vp8_set_quantizer(cpi, 26);
2936 vp8_first_pass(cpi);
2941 void write_cx_frame_to_file(YV12_BUFFER_CONFIG *frame, int this_frame)
2949 sprintf(filename, "cx\\y%04d.raw", this_frame);
2950 yframe = fopen(filename, "wb");
2952 for (i = 0; i < frame->y_height; i++)
2953 fwrite(frame->y_buffer + i * frame->y_stride, frame->y_width, 1, yframe);
2956 sprintf(filename, "cx\\u%04d.raw", this_frame);
2957 yframe = fopen(filename, "wb");
2959 for (i = 0; i < frame->uv_height; i++)
2960 fwrite(frame->u_buffer + i * frame->uv_stride, frame->uv_width, 1, yframe);
2963 sprintf(filename, "cx\\v%04d.raw", this_frame);
2964 yframe = fopen(filename, "wb");
2966 for (i = 0; i < frame->uv_height; i++)
2967 fwrite(frame->v_buffer + i * frame->uv_stride, frame->uv_width, 1, yframe);
2972 // return of 0 means drop frame
2974 // Function to test for conditions that indeicate we should loop
2975 // back and recode a frame.
2976 static int recode_loop_test( VP8_COMP *cpi,
2977 int high_limit, int low_limit,
2978 int q, int maxq, int minq )
2980 int force_recode = 0;
2981 VP8_COMMON *cm = &cpi->common;
2983 // Is frame recode allowed at all
2984 // Yes if either recode mode 1 is selected or mode two is selcted
2985 // and the frame is a key frame. golden frame or alt_ref_frame
2986 if ( (cpi->sf.recode_loop == 1) ||
2987 ( (cpi->sf.recode_loop == 2) &&
2988 ( (cm->frame_type == KEY_FRAME) ||
2989 cm->refresh_golden_frame ||
2990 cm->refresh_alt_ref_frame ) ) )
2992 // General over and under shoot tests
2993 if ( ((cpi->projected_frame_size > high_limit) && (q < maxq)) ||
2994 ((cpi->projected_frame_size < low_limit) && (q > minq)) )
2998 // Special Constrained quality tests
2999 else if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY)
3001 // Undershoot and below auto cq level
3002 if ( (q > cpi->cq_target_quality) &&
3003 (cpi->projected_frame_size <
3004 ((cpi->this_frame_target * 7) >> 3)))
3008 // Severe undershoot and between auto and user cq level
3009 else if ( (q > cpi->oxcf.cq_level) &&
3010 (cpi->projected_frame_size < cpi->min_frame_bandwidth) &&
3011 (cpi->active_best_quality > cpi->oxcf.cq_level))
3014 cpi->active_best_quality = cpi->oxcf.cq_level;
3019 return force_recode;
3022 void update_reference_frames(VP8_COMMON *cm)
3024 YV12_BUFFER_CONFIG *yv12_fb = cm->yv12_fb;
3026 // At this point the new frame has been encoded.
3027 // If any buffer copy / swapping is signaled it should be done here.
3029 if (cm->frame_type == KEY_FRAME)
3031 yv12_fb[cm->new_fb_idx].flags |= VP8_GOLD_FLAG | VP8_ALT_FLAG ;
3033 yv12_fb[cm->gld_fb_idx].flags &= ~VP8_GOLD_FLAG;
3034 yv12_fb[cm->alt_fb_idx].flags &= ~VP8_ALT_FLAG;
3036 cm->alt_fb_idx = cm->gld_fb_idx = cm->new_fb_idx;
3038 else /* For non key frames */
3040 if (cm->refresh_alt_ref_frame)
3042 assert(!cm->copy_buffer_to_arf);
3044 cm->yv12_fb[cm->new_fb_idx].flags |= VP8_ALT_FLAG;
3045 cm->yv12_fb[cm->alt_fb_idx].flags &= ~VP8_ALT_FLAG;
3046 cm->alt_fb_idx = cm->new_fb_idx;
3048 else if (cm->copy_buffer_to_arf)
3050 assert(!(cm->copy_buffer_to_arf & ~0x3));
3052 if (cm->copy_buffer_to_arf == 1)
3054 if(cm->alt_fb_idx != cm->lst_fb_idx)
3056 yv12_fb[cm->lst_fb_idx].flags |= VP8_ALT_FLAG;
3057 yv12_fb[cm->alt_fb_idx].flags &= ~VP8_ALT_FLAG;
3058 cm->alt_fb_idx = cm->lst_fb_idx;
3061 else /* if (cm->copy_buffer_to_arf == 2) */
3063 if(cm->alt_fb_idx != cm->gld_fb_idx)
3065 yv12_fb[cm->gld_fb_idx].flags |= VP8_ALT_FLAG;
3066 yv12_fb[cm->alt_fb_idx].flags &= ~VP8_ALT_FLAG;
3067 cm->alt_fb_idx = cm->gld_fb_idx;
3072 if (cm->refresh_golden_frame)
3074 assert(!cm->copy_buffer_to_gf);
3076 cm->yv12_fb[cm->new_fb_idx].flags |= VP8_GOLD_FLAG;
3077 cm->yv12_fb[cm->gld_fb_idx].flags &= ~VP8_GOLD_FLAG;
3078 cm->gld_fb_idx = cm->new_fb_idx;
3080 else if (cm->copy_buffer_to_gf)
3082 assert(!(cm->copy_buffer_to_arf & ~0x3));
3084 if (cm->copy_buffer_to_gf == 1)
3086 if(cm->gld_fb_idx != cm->lst_fb_idx)
3088 yv12_fb[cm->lst_fb_idx].flags |= VP8_GOLD_FLAG;
3089 yv12_fb[cm->gld_fb_idx].flags &= ~VP8_GOLD_FLAG;
3090 cm->gld_fb_idx = cm->lst_fb_idx;
3093 else /* if (cm->copy_buffer_to_gf == 2) */
3095 if(cm->alt_fb_idx != cm->gld_fb_idx)
3097 yv12_fb[cm->alt_fb_idx].flags |= VP8_GOLD_FLAG;
3098 yv12_fb[cm->gld_fb_idx].flags &= ~VP8_GOLD_FLAG;
3099 cm->gld_fb_idx = cm->alt_fb_idx;
3105 if (cm->refresh_last_frame)
3107 cm->yv12_fb[cm->new_fb_idx].flags |= VP8_LAST_FLAG;
3108 cm->yv12_fb[cm->lst_fb_idx].flags &= ~VP8_LAST_FLAG;
3109 cm->lst_fb_idx = cm->new_fb_idx;
3113 void vp8_loopfilter_frame(VP8_COMP *cpi, VP8_COMMON *cm)
3117 cm->filter_level = 0;
3121 struct vpx_usec_timer timer;
3123 vp8_clear_system_state();
3125 vpx_usec_timer_start(&timer);
3126 if (cpi->sf.auto_filter == 0)
3127 vp8cx_pick_filter_level_fast(cpi->Source, cpi);
3130 vp8cx_pick_filter_level(cpi->Source, cpi);
3132 vpx_usec_timer_mark(&timer);
3133 cpi->time_pick_lpf += vpx_usec_timer_elapsed(&timer);
3136 #if CONFIG_MULTITHREAD
3137 if (cpi->b_multi_threaded)
3138 sem_post(&cpi->h_event_end_lpf); /* signal that we have set filter_level */
3141 if (cm->filter_level > 0)
3143 vp8cx_set_alt_lf_level(cpi, cm->filter_level);
3144 vp8_loop_filter_frame(cm, &cpi->mb.e_mbd);
3147 vp8_yv12_extend_frame_borders(cm->frame_to_show);
3148 #if CONFIG_TEMPORAL_DENOISING
3149 if (cpi->oxcf.noise_sensitivity)
3151 vp8_yv12_extend_frame_borders(&cpi->denoiser.yv12_running_avg);
3156 static void encode_frame_to_data_rate
3159 unsigned long *size,
3160 unsigned char *dest,
3161 unsigned char* dest_end,
3162 unsigned int *frame_flags
3166 int frame_over_shoot_limit;
3167 int frame_under_shoot_limit;
3172 VP8_COMMON *cm = &cpi->common;
3173 int active_worst_qchanged = 0;
3175 #if !(CONFIG_REALTIME_ONLY)
3179 int zbin_oq_low = 0;
3182 int overshoot_seen = 0;
3183 int undershoot_seen = 0;
3186 int drop_mark = cpi->oxcf.drop_frames_water_mark * cpi->oxcf.optimal_buffer_level / 100;
3187 int drop_mark75 = drop_mark * 2 / 3;
3188 int drop_mark50 = drop_mark / 4;
3189 int drop_mark25 = drop_mark / 8;
3192 // Clear down mmx registers to allow floating point in what follows
3193 vp8_clear_system_state();
3195 #if CONFIG_MULTITHREAD
3196 /* wait for the last picture loopfilter thread done */
3197 if (cpi->b_lpf_running)
3199 sem_wait(&cpi->h_event_end_lpf);
3200 cpi->b_lpf_running = 0;
3204 // Test code for segmentation of gf/arf (0,0)
3205 //segmentation_test_function( cpi);
3207 if(cpi->force_next_frame_intra)
3209 cm->frame_type = KEY_FRAME; /* delayed intra frame */
3210 cpi->force_next_frame_intra = 0;
3213 // For an alt ref frame in 2 pass we skip the call to the second pass function that sets the target bandwidth
3214 #if !(CONFIG_REALTIME_ONLY)
3218 if (cpi->common.refresh_alt_ref_frame)
3220 cpi->per_frame_bandwidth = cpi->twopass.gf_bits; // Per frame bit target for the alt ref frame
3221 cpi->target_bandwidth = cpi->twopass.gf_bits * cpi->output_frame_rate; // per second target bitrate
3226 cpi->per_frame_bandwidth = (int)(cpi->target_bandwidth / cpi->output_frame_rate);
3228 // Default turn off buffer to buffer copying
3229 cm->copy_buffer_to_gf = 0;
3230 cm->copy_buffer_to_arf = 0;
3232 // Clear zbin over-quant value and mode boost values.
3233 cpi->zbin_over_quant = 0;
3234 cpi->zbin_mode_boost = 0;
3236 // Enable or disable mode based tweaking of the zbin
3237 // For 2 Pass Only used where GF/ARF prediction quality
3238 // is above a threshold
3239 cpi->zbin_mode_boost_enabled = 1;
3242 if ( cpi->gfu_boost <= 400 )
3244 cpi->zbin_mode_boost_enabled = 0;
3248 // Current default encoder behaviour for the altref sign bias
3249 if (cpi->source_alt_ref_active)
3250 cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = 1;
3252 cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = 0;
3254 // Check to see if a key frame is signalled
3255 // For two pass with auto key frame enabled cm->frame_type may already be set, but not for one pass.
3256 if ((cm->current_video_frame == 0) ||
3257 (cm->frame_flags & FRAMEFLAGS_KEY) ||
3258 (cpi->oxcf.auto_key && (cpi->frames_since_key % cpi->key_frame_frequency == 0)))
3260 // Key frame from VFW/auto-keyframe/first frame
3261 cm->frame_type = KEY_FRAME;
3264 // Set default state for segment and mode based loop filter update flags
3265 cpi->mb.e_mbd.update_mb_segmentation_map = 0;
3266 cpi->mb.e_mbd.update_mb_segmentation_data = 0;
3267 cpi->mb.e_mbd.mode_ref_lf_delta_update = 0;
3269 // Set various flags etc to special state if it is a key frame
3270 if (cm->frame_type == KEY_FRAME)
3274 // Reset the loop filter deltas and segmentation map
3275 setup_features(cpi);
3277 // If segmentation is enabled force a map update for key frames
3278 if (cpi->mb.e_mbd.segmentation_enabled)
3280 cpi->mb.e_mbd.update_mb_segmentation_map = 1;
3281 cpi->mb.e_mbd.update_mb_segmentation_data = 1;
3284 // The alternate reference frame cannot be active for a key frame
3285 cpi->source_alt_ref_active = 0;
3287 // Reset the RD threshold multipliers to default of * 1 (128)
3288 for (i = 0; i < MAX_MODES; i++)
3290 cpi->rd_thresh_mult[i] = 128;
3294 // Test code for segmentation
3295 //if ( (cm->frame_type == KEY_FRAME) || ((cm->current_video_frame % 2) == 0))
3296 //if ( (cm->current_video_frame % 2) == 0 )
3297 // enable_segmentation(cpi);
3299 // disable_segmentation(cpi);
3302 // Experimental code for lagged compress and one pass
3303 // Initialise one_pass GF frames stats
3304 // Update stats used for GF selection
3305 //if ( cpi->pass == 0 )
3307 cpi->one_pass_frame_index = cm->current_video_frame % MAX_LAG_BUFFERS;
3309 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frames_so_far = 0;
3310 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_intra_error = 0.0;
3311 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_coded_error = 0.0;
3312 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_pcnt_inter = 0.0;
3313 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_pcnt_motion = 0.0;
3314 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvr = 0.0;
3315 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvr_abs = 0.0;
3316 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvc = 0.0;
3317 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvc_abs = 0.0;
3321 update_rd_ref_frame_probs(cpi);
3323 if (cpi->drop_frames_allowed)
3325 // The reset to decimation 0 is only done here for one pass.
3326 // Once it is set two pass leaves decimation on till the next kf.
3327 if ((cpi->buffer_level > drop_mark) && (cpi->decimation_factor > 0))
3328 cpi->decimation_factor --;
3330 if (cpi->buffer_level > drop_mark75 && cpi->decimation_factor > 0)
3331 cpi->decimation_factor = 1;
3333 else if (cpi->buffer_level < drop_mark25 && (cpi->decimation_factor == 2 || cpi->decimation_factor == 3))
3335 cpi->decimation_factor = 3;
3337 else if (cpi->buffer_level < drop_mark50 && (cpi->decimation_factor == 1 || cpi->decimation_factor == 2))
3339 cpi->decimation_factor = 2;
3341 else if (cpi->buffer_level < drop_mark75 && (cpi->decimation_factor == 0 || cpi->decimation_factor == 1))
3343 cpi->decimation_factor = 1;
3345 //vpx_log("Encoder: Decimation Factor: %d \n",cpi->decimation_factor);
3348 // The following decimates the frame rate according to a regular pattern (i.e. to 1/2 or 2/3 frame rate)
3349 // This can be used to help prevent buffer under-run in CBR mode. Alternatively it might be desirable in
3350 // some situations to drop frame rate but throw more bits at each frame.
3352 // Note that dropping a key frame can be problematic if spatial resampling is also active
3353 if (cpi->decimation_factor > 0)
3355 switch (cpi->decimation_factor)
3358 cpi->per_frame_bandwidth = cpi->per_frame_bandwidth * 3 / 2;
3361 cpi->per_frame_bandwidth = cpi->per_frame_bandwidth * 5 / 4;
3364 cpi->per_frame_bandwidth = cpi->per_frame_bandwidth * 5 / 4;
3368 // Note that we should not throw out a key frame (especially when spatial resampling is enabled).
3369 if ((cm->frame_type == KEY_FRAME)) // && cpi->oxcf.allow_spatial_resampling )
3371 cpi->decimation_count = cpi->decimation_factor;
3373 else if (cpi->decimation_count > 0)
3375 cpi->decimation_count --;
3377 cpi->bits_off_target += cpi->av_per_frame_bandwidth;
3378 if (cpi->bits_off_target > cpi->oxcf.maximum_buffer_size)
3379 cpi->bits_off_target = cpi->oxcf.maximum_buffer_size;
3381 cm->current_video_frame++;
3382 cpi->frames_since_key++;
3384 #if CONFIG_INTERNAL_STATS
3388 cpi->buffer_level = cpi->bits_off_target;
3390 if (cpi->oxcf.number_of_layers > 1)
3394 // Propagate bits saved by dropping the frame to higher layers
3395 for (i=cpi->current_layer+1; i<cpi->oxcf.number_of_layers; i++)
3397 LAYER_CONTEXT *lc = &cpi->layer_context[i];
3398 lc->bits_off_target += cpi->av_per_frame_bandwidth;
3399 if (lc->bits_off_target > lc->maximum_buffer_size)
3400 lc->bits_off_target = lc->maximum_buffer_size;
3401 lc->buffer_level = lc->bits_off_target;
3408 cpi->decimation_count = cpi->decimation_factor;
3411 // Decide how big to make the frame
3412 if (!vp8_pick_frame_size(cpi))
3414 cm->current_video_frame++;
3415 cpi->frames_since_key++;
3419 // Reduce active_worst_allowed_q for CBR if our buffer is getting too full.
3420 // This has a knock on effect on active best quality as well.
3421 // For CBR if the buffer reaches its maximum level then we can no longer
3422 // save up bits for later frames so we might as well use them up
3423 // on the current frame.
3424 if ((cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) &&
3425 (cpi->buffer_level >= cpi->oxcf.optimal_buffer_level) && cpi->buffered_mode)
3427 int Adjustment = cpi->active_worst_quality / 4; // Max adjustment is 1/4
3433 if (cpi->buffer_level < cpi->oxcf.maximum_buffer_size)
3435 buff_lvl_step = (cpi->oxcf.maximum_buffer_size - cpi->oxcf.optimal_buffer_level) / Adjustment;
3438 Adjustment = (cpi->buffer_level - cpi->oxcf.optimal_buffer_level) / buff_lvl_step;
3443 cpi->active_worst_quality -= Adjustment;
3445 if(cpi->active_worst_quality < cpi->active_best_quality)
3446 cpi->active_worst_quality = cpi->active_best_quality;
3450 // Set an active best quality and if necessary active worst quality
3451 // There is some odd behavior for one pass here that needs attention.
3452 if ( (cpi->pass == 2) || (cpi->ni_frames > 150))
3454 vp8_clear_system_state();
3456 Q = cpi->active_worst_quality;
3458 if ( cm->frame_type == KEY_FRAME )
3460 if ( cpi->pass == 2 )
3462 if (cpi->gfu_boost > 600)
3463 cpi->active_best_quality = kf_low_motion_minq[Q];
3465 cpi->active_best_quality = kf_high_motion_minq[Q];
3467 // Special case for key frames forced because we have reached
3468 // the maximum key frame interval. Here force the Q to a range
3469 // based on the ambient Q to reduce the risk of popping
3470 if ( cpi->this_key_frame_forced )
3472 if ( cpi->active_best_quality > cpi->avg_frame_qindex * 7/8)
3473 cpi->active_best_quality = cpi->avg_frame_qindex * 7/8;
3474 else if ( cpi->active_best_quality < cpi->avg_frame_qindex >> 2 )
3475 cpi->active_best_quality = cpi->avg_frame_qindex >> 2;
3478 // One pass more conservative
3480 cpi->active_best_quality = kf_high_motion_minq[Q];
3483 else if (cpi->oxcf.number_of_layers==1 &&
3484 (cm->refresh_golden_frame || cpi->common.refresh_alt_ref_frame))
3486 // Use the lower of cpi->active_worst_quality and recent
3487 // average Q as basis for GF/ARF Q limit unless last frame was
3489 if ( (cpi->frames_since_key > 1) &&
3490 (cpi->avg_frame_qindex < cpi->active_worst_quality) )
3492 Q = cpi->avg_frame_qindex;
3495 // For constrained quality dont allow Q less than the cq level
3496 if ( (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) &&
3497 (Q < cpi->cq_target_quality) )
3499 Q = cpi->cq_target_quality;
3502 if ( cpi->pass == 2 )
3504 if ( cpi->gfu_boost > 1000 )
3505 cpi->active_best_quality = gf_low_motion_minq[Q];
3506 else if ( cpi->gfu_boost < 400 )
3507 cpi->active_best_quality = gf_high_motion_minq[Q];
3509 cpi->active_best_quality = gf_mid_motion_minq[Q];
3511 // Constrained quality use slightly lower active best.
3512 if ( cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY )
3514 cpi->active_best_quality =
3515 cpi->active_best_quality * 15/16;
3518 // One pass more conservative
3520 cpi->active_best_quality = gf_high_motion_minq[Q];
3524 cpi->active_best_quality = inter_minq[Q];
3526 // For the constant/constrained quality mode we dont want
3527 // q to fall below the cq level.
3528 if ((cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) &&
3529 (cpi->active_best_quality < cpi->cq_target_quality) )
3531 // If we are strongly undershooting the target rate in the last
3532 // frames then use the user passed in cq value not the auto
3534 if ( cpi->rolling_actual_bits < cpi->min_frame_bandwidth )
3535 cpi->active_best_quality = cpi->oxcf.cq_level;
3537 cpi->active_best_quality = cpi->cq_target_quality;
3541 // If CBR and the buffer is as full then it is reasonable to allow
3542 // higher quality on the frames to prevent bits just going to waste.
3543 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
3545 // Note that the use of >= here elliminates the risk of a devide
3546 // by 0 error in the else if clause
3547 if (cpi->buffer_level >= cpi->oxcf.maximum_buffer_size)
3548 cpi->active_best_quality = cpi->best_quality;
3550 else if (cpi->buffer_level > cpi->oxcf.optimal_buffer_level)
3552 int Fraction = ((cpi->buffer_level - cpi->oxcf.optimal_buffer_level) * 128) / (cpi->oxcf.maximum_buffer_size - cpi->oxcf.optimal_buffer_level);
3553 int min_qadjustment = ((cpi->active_best_quality - cpi->best_quality) * Fraction) / 128;
3555 cpi->active_best_quality -= min_qadjustment;
3559 // Make sure constrained quality mode limits are adhered to for the first
3560 // few frames of one pass encodes
3561 else if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY)
3563 if ( (cm->frame_type == KEY_FRAME) ||
3564 cm->refresh_golden_frame || cpi->common.refresh_alt_ref_frame )
3566 cpi->active_best_quality = cpi->best_quality;
3568 else if (cpi->active_best_quality < cpi->cq_target_quality)
3570 cpi->active_best_quality = cpi->cq_target_quality;
3574 // Clip the active best and worst quality values to limits
3575 if (cpi->active_worst_quality > cpi->worst_quality)
3576 cpi->active_worst_quality = cpi->worst_quality;
3578 if (cpi->active_best_quality < cpi->best_quality)
3579 cpi->active_best_quality = cpi->best_quality;
3581 if ( cpi->active_worst_quality < cpi->active_best_quality )
3582 cpi->active_worst_quality = cpi->active_best_quality;
3584 // Determine initial Q to try
3585 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
3587 #if !(CONFIG_REALTIME_ONLY)
3589 // Set highest allowed value for Zbin over quant
3590 if (cm->frame_type == KEY_FRAME)
3591 zbin_oq_high = 0; //ZBIN_OQ_MAX/16
3592 else if ((cpi->oxcf.number_of_layers == 1) && ((cm->refresh_alt_ref_frame ||
3593 (cm->refresh_golden_frame && !cpi->source_alt_ref_active))))
3598 zbin_oq_high = ZBIN_OQ_MAX;
3601 // Setup background Q adjustment for error resilient mode.
3602 // For multi-layer encodes only enable this for the base layer.
3603 if (cpi->cyclic_refresh_mode_enabled && (cpi->current_layer==0))
3604 cyclic_background_refresh(cpi, Q, 0);
3606 vp8_compute_frame_size_bounds(cpi, &frame_under_shoot_limit, &frame_over_shoot_limit);
3608 #if !(CONFIG_REALTIME_ONLY)
3609 // Limit Q range for the adaptive loop.
3610 bottom_index = cpi->active_best_quality;
3611 top_index = cpi->active_worst_quality;
3612 q_low = cpi->active_best_quality;
3613 q_high = cpi->active_worst_quality;
3616 vp8_save_coding_context(cpi);
3620 scale_and_extend_source(cpi->un_scaled_source, cpi);
3622 #if !(CONFIG_REALTIME_ONLY) && CONFIG_POSTPROC && !(CONFIG_TEMPORAL_DENOISING)
3624 if (cpi->oxcf.noise_sensitivity > 0)
3629 switch (cpi->oxcf.noise_sensitivity)
3652 if (cm->frame_type == KEY_FRAME)
3654 vp8_de_noise(cpi->Source, cpi->Source, l , 1, 0);
3658 vp8_de_noise(cpi->Source, cpi->Source, l , 1, 0);
3660 src = cpi->Source->y_buffer;
3662 if (cpi->Source->y_stride < 0)
3664 src += cpi->Source->y_stride * (cpi->Source->y_height - 1);
3671 #ifdef OUTPUT_YUV_SRC
3672 vp8_write_yuv_frame(cpi->Source);
3677 vp8_clear_system_state(); //__asm emms;
3680 if(cpi->is_src_frame_alt_ref)
3684 vp8_set_quantizer(cpi, Q);
3686 // setup skip prob for costing in mode/mv decision
3687 if (cpi->common.mb_no_coeff_skip)
3689 cpi->prob_skip_false = cpi->base_skip_false_prob[Q];
3691 if (cm->frame_type != KEY_FRAME)
3693 if (cpi->common.refresh_alt_ref_frame)
3695 if (cpi->last_skip_false_probs[2] != 0)
3696 cpi->prob_skip_false = cpi->last_skip_false_probs[2];
3699 if(cpi->last_skip_false_probs[2]!=0 && abs(Q- cpi->last_skip_probs_q[2])<=16 )
3700 cpi->prob_skip_false = cpi->last_skip_false_probs[2];
3701 else if (cpi->last_skip_false_probs[2]!=0)
3702 cpi->prob_skip_false = (cpi->last_skip_false_probs[2] + cpi->prob_skip_false ) / 2;
3705 else if (cpi->common.refresh_golden_frame)
3707 if (cpi->last_skip_false_probs[1] != 0)
3708 cpi->prob_skip_false = cpi->last_skip_false_probs[1];
3711 if(cpi->last_skip_false_probs[1]!=0 && abs(Q- cpi->last_skip_probs_q[1])<=16 )
3712 cpi->prob_skip_false = cpi->last_skip_false_probs[1];
3713 else if (cpi->last_skip_false_probs[1]!=0)
3714 cpi->prob_skip_false = (cpi->last_skip_false_probs[1] + cpi->prob_skip_false ) / 2;
3719 if (cpi->last_skip_false_probs[0] != 0)
3720 cpi->prob_skip_false = cpi->last_skip_false_probs[0];
3723 if(cpi->last_skip_false_probs[0]!=0 && abs(Q- cpi->last_skip_probs_q[0])<=16 )
3724 cpi->prob_skip_false = cpi->last_skip_false_probs[0];
3725 else if(cpi->last_skip_false_probs[0]!=0)
3726 cpi->prob_skip_false = (cpi->last_skip_false_probs[0] + cpi->prob_skip_false ) / 2;
3730 //as this is for cost estimate, let's make sure it does not go extreme eitehr way
3731 if (cpi->prob_skip_false < 5)
3732 cpi->prob_skip_false = 5;
3734 if (cpi->prob_skip_false > 250)
3735 cpi->prob_skip_false = 250;
3737 if (cpi->oxcf.number_of_layers == 1 && cpi->is_src_frame_alt_ref)
3738 cpi->prob_skip_false = 1;
3745 FILE *f = fopen("skip.stt", "a");
3746 fprintf(f, "%d, %d, %4d ", cpi->common.refresh_golden_frame, cpi->common.refresh_alt_ref_frame, cpi->prob_skip_false);
3754 if (cm->frame_type == KEY_FRAME)
3756 resize_key_frame(cpi);
3757 vp8_setup_key_frame(cpi);
3762 #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
3764 if(cpi->oxcf.error_resilient_mode)
3765 cm->refresh_entropy_probs = 0;
3767 if (cpi->oxcf.error_resilient_mode & VPX_ERROR_RESILIENT_PARTITIONS)
3769 if (cm->frame_type == KEY_FRAME)
3770 cm->refresh_entropy_probs = 1;
3773 if (cm->refresh_entropy_probs == 0)
3775 // save a copy for later refresh
3776 vpx_memcpy(&cm->lfc, &cm->fc, sizeof(cm->fc));
3779 vp8_update_coef_context(cpi);
3781 vp8_update_coef_probs(cpi);
3783 // transform / motion compensation build reconstruction frame
3784 // +pack coef partitions
3785 vp8_encode_frame(cpi);
3787 /* cpi->projected_frame_size is not needed for RT mode */
3790 // transform / motion compensation build reconstruction frame
3791 vp8_encode_frame(cpi);
3793 cpi->projected_frame_size -= vp8_estimate_entropy_savings(cpi);
3794 cpi->projected_frame_size = (cpi->projected_frame_size > 0) ? cpi->projected_frame_size : 0;
3796 vp8_clear_system_state(); //__asm emms;
3798 // Test to see if the stats generated for this frame indicate that we should have coded a key frame
3799 // (assuming that we didn't)!
3800 if (cpi->pass != 2 && cpi->oxcf.auto_key && cm->frame_type != KEY_FRAME)
3802 int key_frame_decision = decide_key_frame(cpi);
3804 if (cpi->compressor_speed == 2)
3806 /* we don't do re-encoding in realtime mode
3807 * if key frame is decided then we force it on next frame */
3808 cpi->force_next_frame_intra = key_frame_decision;
3810 #if !(CONFIG_REALTIME_ONLY)
3811 else if (key_frame_decision)
3813 // Reset all our sizing numbers and recode
3814 cm->frame_type = KEY_FRAME;
3816 vp8_pick_frame_size(cpi);
3818 // Clear the Alt reference frame active flag when we have a key frame
3819 cpi->source_alt_ref_active = 0;
3821 // Reset the loop filter deltas and segmentation map
3822 setup_features(cpi);
3824 // If segmentation is enabled force a map update for key frames
3825 if (cpi->mb.e_mbd.segmentation_enabled)
3827 cpi->mb.e_mbd.update_mb_segmentation_map = 1;
3828 cpi->mb.e_mbd.update_mb_segmentation_data = 1;
3831 vp8_restore_coding_context(cpi);
3833 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
3835 vp8_compute_frame_size_bounds(cpi, &frame_under_shoot_limit, &frame_over_shoot_limit);
3837 // Limit Q range for the adaptive loop.
3838 bottom_index = cpi->active_best_quality;
3839 top_index = cpi->active_worst_quality;
3840 q_low = cpi->active_best_quality;
3841 q_high = cpi->active_worst_quality;
3851 vp8_clear_system_state();
3853 if (frame_over_shoot_limit == 0)
3854 frame_over_shoot_limit = 1;
3856 // Are we are overshooting and up against the limit of active max Q.
3857 if (((cpi->pass != 2) || (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)) &&
3858 (Q == cpi->active_worst_quality) &&
3859 (cpi->active_worst_quality < cpi->worst_quality) &&
3860 (cpi->projected_frame_size > frame_over_shoot_limit))
3862 int over_size_percent = ((cpi->projected_frame_size - frame_over_shoot_limit) * 100) / frame_over_shoot_limit;
3864 // If so is there any scope for relaxing it
3865 while ((cpi->active_worst_quality < cpi->worst_quality) && (over_size_percent > 0))
3867 cpi->active_worst_quality++;
3869 over_size_percent = (int)(over_size_percent * 0.96); // Assume 1 qstep = about 4% on frame size.
3871 #if !(CONFIG_REALTIME_ONLY)
3872 top_index = cpi->active_worst_quality;
3874 // If we have updated the active max Q do not call vp8_update_rate_correction_factors() this loop.
3875 active_worst_qchanged = 1;
3878 active_worst_qchanged = 0;
3880 #if !(CONFIG_REALTIME_ONLY)
3881 // Special case handling for forced key frames
3882 if ( (cm->frame_type == KEY_FRAME) && cpi->this_key_frame_forced )
3885 int kf_err = vp8_calc_ss_err(cpi->Source,
3886 &cm->yv12_fb[cm->new_fb_idx]);
3888 // The key frame is not good enough
3889 if ( kf_err > ((cpi->ambient_err * 7) >> 3) )
3892 q_high = (Q > q_low) ? (Q - 1) : q_low;
3895 Q = (q_high + q_low) >> 1;
3897 // The key frame is much better than the previous frame
3898 else if ( kf_err < (cpi->ambient_err >> 1) )
3901 q_low = (Q < q_high) ? (Q + 1) : q_high;
3904 Q = (q_high + q_low + 1) >> 1;
3907 // Clamp Q to upper and lower limits:
3916 // Is the projected frame size out of range and are we allowed to attempt to recode.
3917 else if ( recode_loop_test( cpi,
3918 frame_over_shoot_limit, frame_under_shoot_limit,
3919 Q, top_index, bottom_index ) )
3924 // Frame size out of permitted range:
3925 // Update correction factor & compute new Q to try...
3927 // Frame is too large
3928 if (cpi->projected_frame_size > cpi->this_frame_target)
3930 //if ( cpi->zbin_over_quant == 0 )
3931 q_low = (Q < q_high) ? (Q + 1) : q_high; // Raise Qlow as to at least the current value
3933 if (cpi->zbin_over_quant > 0) // If we are using over quant do the same for zbin_oq_low
3934 zbin_oq_low = (cpi->zbin_over_quant < zbin_oq_high) ? (cpi->zbin_over_quant + 1) : zbin_oq_high;
3936 //if ( undershoot_seen || (Q == MAXQ) )
3937 if (undershoot_seen)
3939 // Update rate_correction_factor unless cpi->active_worst_quality has changed.
3940 if (!active_worst_qchanged)
3941 vp8_update_rate_correction_factors(cpi, 1);
3943 Q = (q_high + q_low + 1) / 2;
3945 // Adjust cpi->zbin_over_quant (only allowed when Q is max)
3947 cpi->zbin_over_quant = 0;
3950 zbin_oq_low = (cpi->zbin_over_quant < zbin_oq_high) ? (cpi->zbin_over_quant + 1) : zbin_oq_high;
3951 cpi->zbin_over_quant = (zbin_oq_high + zbin_oq_low) / 2;
3956 // Update rate_correction_factor unless cpi->active_worst_quality has changed.
3957 if (!active_worst_qchanged)
3958 vp8_update_rate_correction_factors(cpi, 0);
3960 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
3962 while (((Q < q_low) || (cpi->zbin_over_quant < zbin_oq_low)) && (Retries < 10))
3964 vp8_update_rate_correction_factors(cpi, 0);
3965 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
3972 // Frame is too small
3975 if (cpi->zbin_over_quant == 0)
3976 q_high = (Q > q_low) ? (Q - 1) : q_low; // Lower q_high if not using over quant
3977 else // else lower zbin_oq_high
3978 zbin_oq_high = (cpi->zbin_over_quant > zbin_oq_low) ? (cpi->zbin_over_quant - 1) : zbin_oq_low;
3982 // Update rate_correction_factor unless cpi->active_worst_quality has changed.
3983 if (!active_worst_qchanged)
3984 vp8_update_rate_correction_factors(cpi, 1);
3986 Q = (q_high + q_low) / 2;
3988 // Adjust cpi->zbin_over_quant (only allowed when Q is max)
3990 cpi->zbin_over_quant = 0;
3992 cpi->zbin_over_quant = (zbin_oq_high + zbin_oq_low) / 2;
3996 // Update rate_correction_factor unless cpi->active_worst_quality has changed.
3997 if (!active_worst_qchanged)
3998 vp8_update_rate_correction_factors(cpi, 0);
4000 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4002 // Special case reset for qlow for constrained quality.
4003 // This should only trigger where there is very substantial
4004 // undershoot on a frame and the auto cq level is above
4005 // the user passsed in value.
4006 if ( (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) &&
4012 while (((Q > q_high) || (cpi->zbin_over_quant > zbin_oq_high)) && (Retries < 10))
4014 vp8_update_rate_correction_factors(cpi, 0);
4015 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4020 undershoot_seen = 1;
4023 // Clamp Q to upper and lower limits:
4029 // Clamp cpi->zbin_over_quant
4030 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;
4038 if (cpi->is_src_frame_alt_ref)
4043 vp8_restore_coding_context(cpi);
4045 #if CONFIG_INTERNAL_STATS
4046 cpi->tot_recode_hits++;
4053 // Experimental code for lagged and one pass
4054 // Update stats used for one pass GF selection
4058 double frame_intra_error;
4059 double frame_coded_error;
4060 double frame_pcnt_inter;
4061 double frame_pcnt_motion;
4063 double frame_mvr_abs;
4065 double frame_mvc_abs;
4068 cpi->one_pass_frame_stats[cpi->one_pass_frame_index].frame_coded_error = (double)cpi->prediction_error;
4069 cpi->one_pass_frame_stats[cpi->one_pass_frame_index].frame_intra_error = (double)cpi->intra_error;
4070 cpi->one_pass_frame_stats[cpi->one_pass_frame_index].frame_pcnt_inter = (double)(100 - cpi->this_frame_percent_intra) / 100.0;
4074 // Special case code to reduce pulsing when key frames are forced at a
4075 // fixed interval. Note the reconstruction error if it is the frame before
4076 // the force key frame
4077 if ( cpi->next_key_frame_forced && (cpi->twopass.frames_to_key == 0) )
4079 cpi->ambient_err = vp8_calc_ss_err(cpi->Source,
4080 &cm->yv12_fb[cm->new_fb_idx]);
4083 /* This frame's MVs are saved and will be used in next frame's MV predictor.
4084 * Last frame has one more line(add to bottom) and one more column(add to
4085 * right) than cm->mip. The edge elements are initialized to 0.
4087 #if CONFIG_MULTI_RES_ENCODING
4088 if(!cpi->oxcf.mr_encoder_id && cm->show_frame)
4090 if(cm->show_frame) /* do not save for altref frame */
4095 /* Point to beginning of allocated MODE_INFO arrays. */
4096 MODE_INFO *tmp = cm->mip;
4098 if(cm->frame_type != KEY_FRAME)
4100 for (mb_row = 0; mb_row < cm->mb_rows+1; mb_row ++)
4102 for (mb_col = 0; mb_col < cm->mb_cols+1; mb_col ++)
4104 if(tmp->mbmi.ref_frame != INTRA_FRAME)
4105 cpi->lfmv[mb_col + mb_row*(cm->mode_info_stride+1)].as_int = tmp->mbmi.mv.as_int;
4107 cpi->lf_ref_frame_sign_bias[mb_col + mb_row*(cm->mode_info_stride+1)] = cm->ref_frame_sign_bias[tmp->mbmi.ref_frame];
4108 cpi->lf_ref_frame[mb_col + mb_row*(cm->mode_info_stride+1)] = tmp->mbmi.ref_frame;
4115 #if CONFIG_MULTI_RES_ENCODING
4116 vp8_cal_dissimilarity(cpi);
4119 // Update the GF useage maps.
4120 // This is done after completing the compression of a frame when all
4121 // modes etc. are finalized but before loop filter
4122 if (cpi->oxcf.number_of_layers == 1)
4123 vp8_update_gf_useage_maps(cpi, cm, &cpi->mb);
4125 if (cm->frame_type == KEY_FRAME)
4126 cm->refresh_last_frame = 1;
4130 FILE *f = fopen("gfactive.stt", "a");
4131 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);
4136 // For inter frames the current default behavior is that when
4137 // cm->refresh_golden_frame is set we copy the old GF over to the ARF buffer
4138 // This is purely an encoder decision at present.
4139 if (!cpi->oxcf.error_resilient_mode && cm->refresh_golden_frame)
4140 cm->copy_buffer_to_arf = 2;
4142 cm->copy_buffer_to_arf = 0;
4144 cm->frame_to_show = &cm->yv12_fb[cm->new_fb_idx];
4146 #if CONFIG_MULTITHREAD
4147 if (cpi->b_multi_threaded)
4149 sem_post(&cpi->h_event_start_lpf); /* start loopfilter in separate thread */
4150 cpi->b_lpf_running = 1;
4155 vp8_loopfilter_frame(cpi, cm);
4158 update_reference_frames(cm);
4160 #if !(CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
4161 if (cpi->oxcf.error_resilient_mode)
4163 cm->refresh_entropy_probs = 0;
4167 #if CONFIG_MULTITHREAD
4168 /* wait that filter_level is picked so that we can continue with stream packing */
4169 if (cpi->b_multi_threaded)
4170 sem_wait(&cpi->h_event_end_lpf);
4173 // build the bitstream
4174 vp8_pack_bitstream(cpi, dest, dest_end, size);
4176 #if CONFIG_MULTITHREAD
4177 /* if PSNR packets are generated we have to wait for the lpf */
4178 if (cpi->b_lpf_running && cpi->b_calculate_psnr)
4180 sem_wait(&cpi->h_event_end_lpf);
4181 cpi->b_lpf_running = 0;
4185 /* Move storing frame_type out of the above loop since it is also
4186 * needed in motion search besides loopfilter */
4187 cm->last_frame_type = cm->frame_type;
4189 // Update rate control heuristics
4190 cpi->total_byte_count += (*size);
4191 cpi->projected_frame_size = (*size) << 3;
4193 if (cpi->oxcf.number_of_layers > 1)
4196 for (i=cpi->current_layer+1; i<cpi->oxcf.number_of_layers; i++)
4197 cpi->layer_context[i].total_byte_count += (*size);
4200 if (!active_worst_qchanged)
4201 vp8_update_rate_correction_factors(cpi, 2);
4203 cpi->last_q[cm->frame_type] = cm->base_qindex;
4205 if (cm->frame_type == KEY_FRAME)
4207 vp8_adjust_key_frame_context(cpi);
4210 // Keep a record of ambient average Q.
4211 if (cm->frame_type != KEY_FRAME)
4212 cpi->avg_frame_qindex = (2 + 3 * cpi->avg_frame_qindex + cm->base_qindex) >> 2;
4214 // Keep a record from which we can calculate the average Q excluding GF updates and key frames
4215 if ((cm->frame_type != KEY_FRAME) && ((cpi->oxcf.number_of_layers > 1) ||
4216 (!cm->refresh_golden_frame && !cm->refresh_alt_ref_frame)))
4220 // Calculate the average Q for normal inter frames (not key or GFU
4222 if ( cpi->pass == 2 )
4224 cpi->ni_tot_qi += Q;
4225 cpi->ni_av_qi = (cpi->ni_tot_qi / cpi->ni_frames);
4229 // Damp value for first few frames
4230 if (cpi->ni_frames > 150 )
4232 cpi->ni_tot_qi += Q;
4233 cpi->ni_av_qi = (cpi->ni_tot_qi / cpi->ni_frames);
4235 // For one pass, early in the clip ... average the current frame Q
4236 // value with the worstq entered by the user as a dampening measure
4239 cpi->ni_tot_qi += Q;
4240 cpi->ni_av_qi = ((cpi->ni_tot_qi / cpi->ni_frames) + cpi->worst_quality + 1) / 2;
4243 // If the average Q is higher than what was used in the last frame
4244 // (after going through the recode loop to keep the frame size within range)
4245 // then use the last frame value - 1.
4246 // The -1 is designed to stop Q and hence the data rate, from progressively
4247 // falling away during difficult sections, but at the same time reduce the number of
4248 // itterations around the recode loop.
4249 if (Q > cpi->ni_av_qi)
4250 cpi->ni_av_qi = Q - 1;
4256 // If the frame was massively oversize and we are below optimal buffer level drop next frame
4257 if ((cpi->drop_frames_allowed) &&
4258 (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) &&
4259 (cpi->buffer_level < cpi->oxcf.drop_frames_water_mark * cpi->oxcf.optimal_buffer_level / 100) &&
4260 (cpi->projected_frame_size > (4 * cpi->this_frame_target)))
4262 cpi->drop_frame = 1;
4267 // Set the count for maximum consecutive dropped frames based upon the ratio of
4268 // this frame size to the target average per frame bandwidth.
4269 // (cpi->av_per_frame_bandwidth > 0) is just a sanity check to prevent / 0.
4270 if (cpi->drop_frames_allowed && (cpi->av_per_frame_bandwidth > 0))
4272 cpi->max_drop_count = cpi->projected_frame_size / cpi->av_per_frame_bandwidth;
4274 if (cpi->max_drop_count > cpi->max_consec_dropped_frames)
4275 cpi->max_drop_count = cpi->max_consec_dropped_frames;
4278 // Update the buffer level variable.
4279 // Non-viewable frames are a special case and are treated as pure overhead.
4280 if ( !cm->show_frame )
4281 cpi->bits_off_target -= cpi->projected_frame_size;
4283 cpi->bits_off_target += cpi->av_per_frame_bandwidth - cpi->projected_frame_size;
4285 // Clip the buffer level to the maximum specified buffer size
4286 if (cpi->bits_off_target > cpi->oxcf.maximum_buffer_size)
4287 cpi->bits_off_target = cpi->oxcf.maximum_buffer_size;
4289 // Rolling monitors of whether we are over or underspending used to help regulate min and Max Q in two pass.
4290 cpi->rolling_target_bits = ((cpi->rolling_target_bits * 3) + cpi->this_frame_target + 2) / 4;
4291 cpi->rolling_actual_bits = ((cpi->rolling_actual_bits * 3) + cpi->projected_frame_size + 2) / 4;
4292 cpi->long_rolling_target_bits = ((cpi->long_rolling_target_bits * 31) + cpi->this_frame_target + 16) / 32;
4293 cpi->long_rolling_actual_bits = ((cpi->long_rolling_actual_bits * 31) + cpi->projected_frame_size + 16) / 32;
4295 // Actual bits spent
4296 cpi->total_actual_bits += cpi->projected_frame_size;
4299 cpi->total_target_vs_actual += (cpi->this_frame_target - cpi->projected_frame_size);
4301 cpi->buffer_level = cpi->bits_off_target;
4303 // Propagate values to higher temporal layers
4304 if (cpi->oxcf.number_of_layers > 1)
4308 for (i=cpi->current_layer+1; i<cpi->oxcf.number_of_layers; i++)
4310 LAYER_CONTEXT *lc = &cpi->layer_context[i];
4311 int bits_off_for_this_layer = lc->target_bandwidth / lc->frame_rate
4312 - cpi->projected_frame_size;
4314 lc->bits_off_target += bits_off_for_this_layer;
4316 // Clip buffer level to maximum buffer size for the layer
4317 if (lc->bits_off_target > lc->maximum_buffer_size)
4318 lc->bits_off_target = lc->maximum_buffer_size;
4320 lc->total_actual_bits += cpi->projected_frame_size;
4321 lc->total_target_vs_actual += bits_off_for_this_layer;
4322 lc->buffer_level = lc->bits_off_target;
4326 // Update bits left to the kf and gf groups to account for overshoot or undershoot on these frames
4327 if (cm->frame_type == KEY_FRAME)
4329 cpi->twopass.kf_group_bits += cpi->this_frame_target - cpi->projected_frame_size;
4331 if (cpi->twopass.kf_group_bits < 0)
4332 cpi->twopass.kf_group_bits = 0 ;
4334 else if (cm->refresh_golden_frame || cm->refresh_alt_ref_frame)
4336 cpi->twopass.gf_group_bits += cpi->this_frame_target - cpi->projected_frame_size;
4338 if (cpi->twopass.gf_group_bits < 0)
4339 cpi->twopass.gf_group_bits = 0 ;
4342 if (cm->frame_type != KEY_FRAME)
4344 if (cpi->common.refresh_alt_ref_frame)
4346 cpi->last_skip_false_probs[2] = cpi->prob_skip_false;
4347 cpi->last_skip_probs_q[2] = cm->base_qindex;
4349 else if (cpi->common.refresh_golden_frame)
4351 cpi->last_skip_false_probs[1] = cpi->prob_skip_false;
4352 cpi->last_skip_probs_q[1] = cm->base_qindex;
4356 cpi->last_skip_false_probs[0] = cpi->prob_skip_false;
4357 cpi->last_skip_probs_q[0] = cm->base_qindex;
4359 //update the baseline
4360 cpi->base_skip_false_prob[cm->base_qindex] = cpi->prob_skip_false;
4365 #if 0 && CONFIG_INTERNAL_STATS
4367 FILE *f = fopen("tmp.stt", "a");
4369 vp8_clear_system_state(); //__asm emms;
4371 if (cpi->twopass.total_left_stats.coded_error != 0.0)
4372 fprintf(f, "%10d %10d %10d %10d %10d %10d %10d %10d %10d %6d %6d"
4373 "%6d %6d %6d %5d %5d %5d %8d %8.2f %10d %10.3f"
4375 cpi->common.current_video_frame, cpi->this_frame_target,
4376 cpi->projected_frame_size,
4377 (cpi->projected_frame_size - cpi->this_frame_target),
4378 (int)cpi->total_target_vs_actual,
4380 (cpi->oxcf.starting_buffer_level-cpi->bits_off_target),
4381 (int)cpi->total_actual_bits, cm->base_qindex,
4382 cpi->active_best_quality, cpi->active_worst_quality,
4383 cpi->ni_av_qi, cpi->cq_target_quality,
4384 cpi->zbin_over_quant,
4385 //cpi->avg_frame_qindex, cpi->zbin_over_quant,
4386 cm->refresh_golden_frame, cm->refresh_alt_ref_frame,
4387 cm->frame_type, cpi->gfu_boost,
4388 cpi->twopass.est_max_qcorrection_factor,
4389 (int)cpi->twopass.bits_left,
4390 cpi->twopass.total_left_stats.coded_error,
4391 (double)cpi->twopass.bits_left /
4392 cpi->twopass.total_left_stats.coded_error,
4393 cpi->tot_recode_hits);
4395 fprintf(f, "%10d %10d %10d %10d %10d %10d %10d %10d %10d %6d %6d"
4396 "%6d %6d %6d %5d %5d %5d %8d %8.2f %10d %10.3f"
4398 cpi->common.current_video_frame,
4399 cpi->this_frame_target, cpi->projected_frame_size,
4400 (cpi->projected_frame_size - cpi->this_frame_target),
4401 (int)cpi->total_target_vs_actual,
4403 (cpi->oxcf.starting_buffer_level-cpi->bits_off_target),
4404 (int)cpi->total_actual_bits, cm->base_qindex,
4405 cpi->active_best_quality, cpi->active_worst_quality,
4406 cpi->ni_av_qi, cpi->cq_target_quality,
4407 cpi->zbin_over_quant,
4408 //cpi->avg_frame_qindex, cpi->zbin_over_quant,
4409 cm->refresh_golden_frame, cm->refresh_alt_ref_frame,
4410 cm->frame_type, cpi->gfu_boost,
4411 cpi->twopass.est_max_qcorrection_factor,
4412 (int)cpi->twopass.bits_left,
4413 cpi->twopass.total_left_stats.coded_error,
4414 cpi->tot_recode_hits);
4419 FILE *fmodes = fopen("Modes.stt", "a");
4422 fprintf(fmodes, "%6d:%1d:%1d:%1d ",
4423 cpi->common.current_video_frame,
4424 cm->frame_type, cm->refresh_golden_frame,
4425 cm->refresh_alt_ref_frame);
4427 for (i = 0; i < MAX_MODES; i++)
4428 fprintf(fmodes, "%5d ", cpi->mode_chosen_counts[i]);
4430 fprintf(fmodes, "\n");
4438 // If this was a kf or Gf note the Q
4439 if ((cm->frame_type == KEY_FRAME) || cm->refresh_golden_frame || cm->refresh_alt_ref_frame)
4440 cm->last_kf_gf_q = cm->base_qindex;
4442 if (cm->refresh_golden_frame == 1)
4443 cm->frame_flags = cm->frame_flags | FRAMEFLAGS_GOLDEN;
4445 cm->frame_flags = cm->frame_flags&~FRAMEFLAGS_GOLDEN;
4447 if (cm->refresh_alt_ref_frame == 1)
4448 cm->frame_flags = cm->frame_flags | FRAMEFLAGS_ALTREF;
4450 cm->frame_flags = cm->frame_flags&~FRAMEFLAGS_ALTREF;
4453 if (cm->refresh_last_frame & cm->refresh_golden_frame) // both refreshed
4454 cpi->gold_is_last = 1;
4455 else if (cm->refresh_last_frame ^ cm->refresh_golden_frame) // 1 refreshed but not the other
4456 cpi->gold_is_last = 0;
4458 if (cm->refresh_last_frame & cm->refresh_alt_ref_frame) // both refreshed
4459 cpi->alt_is_last = 1;
4460 else if (cm->refresh_last_frame ^ cm->refresh_alt_ref_frame) // 1 refreshed but not the other
4461 cpi->alt_is_last = 0;
4463 if (cm->refresh_alt_ref_frame & cm->refresh_golden_frame) // both refreshed
4464 cpi->gold_is_alt = 1;
4465 else if (cm->refresh_alt_ref_frame ^ cm->refresh_golden_frame) // 1 refreshed but not the other
4466 cpi->gold_is_alt = 0;
4468 cpi->ref_frame_flags = VP8_ALT_FLAG | VP8_GOLD_FLAG | VP8_LAST_FLAG;
4470 if (cpi->gold_is_last)
4471 cpi->ref_frame_flags &= ~VP8_GOLD_FLAG;
4473 if (cpi->alt_is_last)
4474 cpi->ref_frame_flags &= ~VP8_ALT_FLAG;
4476 if (cpi->gold_is_alt)
4477 cpi->ref_frame_flags &= ~VP8_ALT_FLAG;
4480 if (!cpi->oxcf.error_resilient_mode)
4482 if (cpi->oxcf.play_alternate && cm->refresh_alt_ref_frame && (cm->frame_type != KEY_FRAME))
4483 // Update the alternate reference frame stats as appropriate.
4484 update_alt_ref_frame_stats(cpi);
4486 // Update the Golden frame stats as appropriate.
4487 update_golden_frame_stats(cpi);
4490 if (cm->frame_type == KEY_FRAME)
4492 // Tell the caller that the frame was coded as a key frame
4493 *frame_flags = cm->frame_flags | FRAMEFLAGS_KEY;
4495 // As this frame is a key frame the next defaults to an inter frame.
4496 cm->frame_type = INTER_FRAME;
4498 cpi->last_frame_percent_intra = 100;
4502 *frame_flags = cm->frame_flags&~FRAMEFLAGS_KEY;
4504 cpi->last_frame_percent_intra = cpi->this_frame_percent_intra;
4507 // Clear the one shot update flags for segmentation map and mode/ref loop filter deltas.
4508 cpi->mb.e_mbd.update_mb_segmentation_map = 0;
4509 cpi->mb.e_mbd.update_mb_segmentation_data = 0;
4510 cpi->mb.e_mbd.mode_ref_lf_delta_update = 0;
4513 // Dont increment frame counters if this was an altref buffer update not a real frame
4516 cm->current_video_frame++;
4517 cpi->frames_since_key++;
4520 // reset to normal state now that we are done.
4528 sprintf(filename, "enc%04d.yuv", (int) cm->current_video_frame);
4529 recon_file = fopen(filename, "wb");
4530 fwrite(cm->yv12_fb[cm->lst_fb_idx].buffer_alloc,
4531 cm->yv12_fb[cm->lst_fb_idx].frame_size, 1, recon_file);
4537 //vp8_write_yuv_frame("encoder_recon.yuv", cm->frame_to_show);
4543 static void check_gf_quality(VP8_COMP *cpi)
4545 VP8_COMMON *cm = &cpi->common;
4546 int gf_active_pct = (100 * cpi->gf_active_count) / (cm->mb_rows * cm->mb_cols);
4547 int gf_ref_usage_pct = (cpi->count_mb_ref_frame_usage[GOLDEN_FRAME] * 100) / (cm->mb_rows * cm->mb_cols);
4548 int last_ref_zz_useage = (cpi->inter_zz_count * 100) / (cm->mb_rows * cm->mb_cols);
4550 // Gf refresh is not currently being signalled
4551 if (cpi->gf_update_recommended == 0)
4553 if (cpi->common.frames_since_golden > 7)
4556 if ((gf_active_pct < 10) || ((gf_active_pct + gf_ref_usage_pct) < 15))
4558 // ...but last frame zero zero usage is reasonbable so a new gf might be appropriate
4559 if (last_ref_zz_useage >= 25)
4561 cpi->gf_bad_count ++;
4563 if (cpi->gf_bad_count >= 8) // Check that the condition is stable
4565 cpi->gf_update_recommended = 1;
4566 cpi->gf_bad_count = 0;
4570 cpi->gf_bad_count = 0; // Restart count as the background is not stable enough
4573 cpi->gf_bad_count = 0; // Gf useage has picked up so reset count
4576 // If the signal is set but has not been read should we cancel it.
4577 else if (last_ref_zz_useage < 15)
4579 cpi->gf_update_recommended = 0;
4580 cpi->gf_bad_count = 0;
4585 FILE *f = fopen("gfneeded.stt", "a");
4586 fprintf(f, "%10d %10d %10d %10d %10ld \n",
4587 cm->current_video_frame,
4588 cpi->common.frames_since_golden,
4589 gf_active_pct, gf_ref_usage_pct,
4590 cpi->gf_update_recommended);
4597 #if !(CONFIG_REALTIME_ONLY)
4598 static void Pass2Encode(VP8_COMP *cpi, unsigned long *size, unsigned char *dest, unsigned char * dest_end, unsigned int *frame_flags)
4601 if (!cpi->common.refresh_alt_ref_frame)
4602 vp8_second_pass(cpi);
4604 encode_frame_to_data_rate(cpi, size, dest, dest_end, frame_flags);
4605 cpi->twopass.bits_left -= 8 * *size;
4607 if (!cpi->common.refresh_alt_ref_frame)
4609 double two_pass_min_rate = (double)(cpi->oxcf.target_bandwidth
4610 *cpi->oxcf.two_pass_vbrmin_section / 100);
4611 cpi->twopass.bits_left += (int64_t)(two_pass_min_rate / cpi->frame_rate);
4616 //For ARM NEON, d8-d15 are callee-saved registers, and need to be saved by us.
4618 extern void vp8_push_neon(int64_t *store);
4619 extern void vp8_pop_neon(int64_t *store);
4623 int vp8_receive_raw_frame(VP8_COMP *cpi, unsigned int frame_flags, YV12_BUFFER_CONFIG *sd, int64_t time_stamp, int64_t end_time)
4626 int64_t store_reg[8];
4628 VP8_COMMON *cm = &cpi->common;
4629 struct vpx_usec_timer timer;
4633 #if CONFIG_RUNTIME_CPU_DETECT
4634 if (cm->cpu_caps & HAS_NEON)
4637 vp8_push_neon(store_reg);
4641 vpx_usec_timer_start(&timer);
4643 /* Reinit the lookahead buffer if the frame size changes */
4644 if (sd->y_width != cpi->oxcf.Width || sd->y_height != cpi->oxcf.Height)
4646 assert(cpi->oxcf.lag_in_frames < 2);
4647 dealloc_raw_frame_buffers(cpi);
4648 alloc_raw_frame_buffers(cpi);
4651 if(vp8_lookahead_push(cpi->lookahead, sd, time_stamp, end_time,
4652 frame_flags, cpi->active_map_enabled ? cpi->active_map : NULL))
4654 cm->clr_type = sd->clrtype;
4655 vpx_usec_timer_mark(&timer);
4656 cpi->time_receive_data += vpx_usec_timer_elapsed(&timer);
4659 #if CONFIG_RUNTIME_CPU_DETECT
4660 if (cm->cpu_caps & HAS_NEON)
4663 vp8_pop_neon(store_reg);
4671 static int frame_is_reference(const VP8_COMP *cpi)
4673 const VP8_COMMON *cm = &cpi->common;
4674 const MACROBLOCKD *xd = &cpi->mb.e_mbd;
4676 return cm->frame_type == KEY_FRAME || cm->refresh_last_frame
4677 || cm->refresh_golden_frame || cm->refresh_alt_ref_frame
4678 || cm->copy_buffer_to_gf || cm->copy_buffer_to_arf
4679 || cm->refresh_entropy_probs
4680 || xd->mode_ref_lf_delta_update
4681 || xd->update_mb_segmentation_map || xd->update_mb_segmentation_data;
4685 int vp8_get_compressed_data(VP8_COMP *cpi, unsigned int *frame_flags, unsigned long *size, unsigned char *dest, unsigned char *dest_end, int64_t *time_stamp, int64_t *time_end, int flush)
4688 int64_t store_reg[8];
4691 struct vpx_usec_timer tsctimer;
4692 struct vpx_usec_timer ticktimer;
4693 struct vpx_usec_timer cmptimer;
4694 YV12_BUFFER_CONFIG *force_src_buffer = NULL;
4701 if (setjmp(cpi->common.error.jmp))
4703 cpi->common.error.setjmp = 0;
4704 return VPX_CODEC_CORRUPT_FRAME;
4707 cpi->common.error.setjmp = 1;
4710 #if CONFIG_RUNTIME_CPU_DETECT
4711 if (cm->cpu_caps & HAS_NEON)
4714 vp8_push_neon(store_reg);
4718 vpx_usec_timer_start(&cmptimer);
4722 #if !(CONFIG_REALTIME_ONLY)
4723 // Should we code an alternate reference frame
4724 if (cpi->oxcf.error_resilient_mode == 0 &&
4725 cpi->oxcf.play_alternate &&
4726 cpi->source_alt_ref_pending)
4728 if ((cpi->source = vp8_lookahead_peek(cpi->lookahead,
4729 cpi->frames_till_gf_update_due,
4732 cpi->alt_ref_source = cpi->source;
4733 if (cpi->oxcf.arnr_max_frames > 0)
4735 vp8_temporal_filter_prepare_c(cpi,
4736 cpi->frames_till_gf_update_due);
4737 force_src_buffer = &cpi->alt_ref_buffer;
4739 cm->frames_till_alt_ref_frame = cpi->frames_till_gf_update_due;
4740 cm->refresh_alt_ref_frame = 1;
4741 cm->refresh_golden_frame = 0;
4742 cm->refresh_last_frame = 0;
4744 cpi->source_alt_ref_pending = 0; // Clear Pending alt Ref flag.
4745 cpi->is_src_frame_alt_ref = 0;
4752 /* Read last frame source if we are encoding first pass. */
4753 if (cpi->pass == 1 && cm->current_video_frame > 0)
4755 if((cpi->last_source = vp8_lookahead_peek(cpi->lookahead, 1,
4756 PEEK_BACKWARD)) == NULL)
4761 if ((cpi->source = vp8_lookahead_pop(cpi->lookahead, flush)))
4765 cpi->is_src_frame_alt_ref = cpi->alt_ref_source
4766 && (cpi->source == cpi->alt_ref_source);
4768 if(cpi->is_src_frame_alt_ref)
4769 cpi->alt_ref_source = NULL;
4775 cpi->Source = force_src_buffer ? force_src_buffer : &cpi->source->img;
4776 cpi->un_scaled_source = cpi->Source;
4777 *time_stamp = cpi->source->ts_start;
4778 *time_end = cpi->source->ts_end;
4779 *frame_flags = cpi->source->flags;
4781 if (cpi->pass == 1 && cm->current_video_frame > 0)
4783 cpi->last_frame_unscaled_source = &cpi->last_source->img;
4789 #if !(CONFIG_REALTIME_ONLY)
4791 if (flush && cpi->pass == 1 && !cpi->twopass.first_pass_done)
4793 vp8_end_first_pass(cpi); /* get last stats packet */
4794 cpi->twopass.first_pass_done = 1;
4800 #if CONFIG_RUNTIME_CPU_DETECT
4801 if (cm->cpu_caps & HAS_NEON)
4804 vp8_pop_neon(store_reg);
4810 if (cpi->source->ts_start < cpi->first_time_stamp_ever)
4812 cpi->first_time_stamp_ever = cpi->source->ts_start;
4813 cpi->last_end_time_stamp_seen = cpi->source->ts_start;
4816 // adjust frame rates based on timestamps given
4819 int64_t this_duration;
4822 if (cpi->source->ts_start == cpi->first_time_stamp_ever)
4824 this_duration = cpi->source->ts_end - cpi->source->ts_start;
4829 int64_t last_duration;
4831 this_duration = cpi->source->ts_end - cpi->last_end_time_stamp_seen;
4832 last_duration = cpi->last_end_time_stamp_seen
4833 - cpi->last_time_stamp_seen;
4834 // do a step update if the duration changes by 10%
4836 step = ((this_duration - last_duration) * 10 / last_duration);
4842 cpi->ref_frame_rate = 10000000.0 / this_duration;
4845 double avg_duration, interval;
4847 /* Average this frame's rate into the last second's average
4848 * frame rate. If we haven't seen 1 second yet, then average
4849 * over the whole interval seen.
4851 interval = cpi->source->ts_end - cpi->first_time_stamp_ever;
4852 if(interval > 10000000.0)
4853 interval = 10000000;
4855 avg_duration = 10000000.0 / cpi->ref_frame_rate;
4856 avg_duration *= (interval - avg_duration + this_duration);
4857 avg_duration /= interval;
4859 cpi->ref_frame_rate = 10000000.0 / avg_duration;
4862 if (cpi->oxcf.number_of_layers > 1)
4866 // Update frame rates for each layer
4867 for (i=0; i<cpi->oxcf.number_of_layers; i++)
4869 LAYER_CONTEXT *lc = &cpi->layer_context[i];
4870 lc->frame_rate = cpi->ref_frame_rate /
4871 cpi->oxcf.rate_decimator[i];
4875 vp8_new_frame_rate(cpi, cpi->ref_frame_rate);
4878 cpi->last_time_stamp_seen = cpi->source->ts_start;
4879 cpi->last_end_time_stamp_seen = cpi->source->ts_end;
4882 if (cpi->oxcf.number_of_layers > 1)
4886 update_layer_contexts (cpi);
4888 // Restore layer specific context & set frame rate
4889 layer = cpi->oxcf.layer_id[
4890 cm->current_video_frame % cpi->oxcf.periodicity];
4891 restore_layer_context (cpi, layer);
4892 vp8_new_frame_rate (cpi, cpi->layer_context[layer].frame_rate);
4895 if (cpi->compressor_speed == 2)
4897 if (cpi->oxcf.number_of_layers == 1)
4898 check_gf_quality(cpi);
4899 vpx_usec_timer_start(&tsctimer);
4900 vpx_usec_timer_start(&ticktimer);
4903 #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
4906 const int num_part = (1 << cm->multi_token_partition);
4907 /* the available bytes in dest */
4908 const unsigned long dest_size = dest_end - dest;
4909 const int tok_part_buff_size = (dest_size * 9) / (10 * num_part);
4911 unsigned char *dp = dest;
4913 cpi->partition_d[0] = dp;
4914 dp += dest_size/10; /* reserve 1/10 for control partition */
4915 cpi->partition_d_end[0] = dp;
4917 for(i = 0; i < num_part; i++)
4919 cpi->partition_d[i + 1] = dp;
4920 dp += tok_part_buff_size;
4921 cpi->partition_d_end[i + 1] = dp;
4926 // start with a 0 size frame
4929 // Clear down mmx registers
4930 vp8_clear_system_state(); //__asm emms;
4932 cm->frame_type = INTER_FRAME;
4933 cm->frame_flags = *frame_flags;
4937 if (cm->refresh_alt_ref_frame)
4939 //cm->refresh_golden_frame = 1;
4940 cm->refresh_golden_frame = 0;
4941 cm->refresh_last_frame = 0;
4945 cm->refresh_golden_frame = 0;
4946 cm->refresh_last_frame = 1;
4950 /* find a free buffer for the new frame */
4953 for(; i < NUM_YV12_BUFFERS; i++)
4955 if(!cm->yv12_fb[i].flags)
4962 assert(i < NUM_YV12_BUFFERS );
4964 #if !(CONFIG_REALTIME_ONLY)
4968 Pass1Encode(cpi, size, dest, frame_flags);
4970 else if (cpi->pass == 2)
4972 Pass2Encode(cpi, size, dest, dest_end, frame_flags);
4976 encode_frame_to_data_rate(cpi, size, dest, dest_end, frame_flags);
4978 if (cpi->compressor_speed == 2)
4980 unsigned int duration, duration2;
4981 vpx_usec_timer_mark(&tsctimer);
4982 vpx_usec_timer_mark(&ticktimer);
4984 duration = vpx_usec_timer_elapsed(&ticktimer);
4985 duration2 = (unsigned int)((double)duration / 2);
4987 if (cm->frame_type != KEY_FRAME)
4989 if (cpi->avg_encode_time == 0)
4990 cpi->avg_encode_time = duration;
4992 cpi->avg_encode_time = (7 * cpi->avg_encode_time + duration) >> 3;
4997 //if(*frame_flags!=1)
5000 if (cpi->avg_pick_mode_time == 0)
5001 cpi->avg_pick_mode_time = duration2;
5003 cpi->avg_pick_mode_time = (7 * cpi->avg_pick_mode_time + duration2) >> 3;
5009 if (cm->refresh_entropy_probs == 0)
5011 vpx_memcpy(&cm->fc, &cm->lfc, sizeof(cm->fc));
5014 // Save the contexts separately for alt ref, gold and last.
5015 // (TODO jbb -> Optimize this with pointers to avoid extra copies. )
5016 if(cm->refresh_alt_ref_frame)
5017 vpx_memcpy(&cpi->lfc_a, &cm->fc, sizeof(cm->fc));
5019 if(cm->refresh_golden_frame)
5020 vpx_memcpy(&cpi->lfc_g, &cm->fc, sizeof(cm->fc));
5022 if(cm->refresh_last_frame)
5023 vpx_memcpy(&cpi->lfc_n, &cm->fc, sizeof(cm->fc));
5025 // if its a dropped frame honor the requests on subsequent frames
5028 cpi->droppable = !frame_is_reference(cpi);
5030 // return to normal state
5031 cm->refresh_entropy_probs = 1;
5032 cm->refresh_alt_ref_frame = 0;
5033 cm->refresh_golden_frame = 0;
5034 cm->refresh_last_frame = 1;
5035 cm->frame_type = INTER_FRAME;
5039 // Save layer specific state
5040 if (cpi->oxcf.number_of_layers > 1)
5041 save_layer_context (cpi);
5043 vpx_usec_timer_mark(&cmptimer);
5044 cpi->time_compress_data += vpx_usec_timer_elapsed(&cmptimer);
5046 if (cpi->b_calculate_psnr && cpi->pass != 1 && cm->show_frame)
5048 generate_psnr_packet(cpi);
5051 #if CONFIG_INTERNAL_STATS
5055 cpi->bytes += *size;
5062 if (cpi->b_calculate_psnr)
5066 YV12_BUFFER_CONFIG *orig = cpi->Source;
5067 YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
5068 int y_samples = orig->y_height * orig->y_width ;
5069 int uv_samples = orig->uv_height * orig->uv_width ;
5070 int t_samples = y_samples + 2 * uv_samples;
5071 int64_t sq_error, sq_error2;
5073 ye = calc_plane_error(orig->y_buffer, orig->y_stride,
5074 recon->y_buffer, recon->y_stride, orig->y_width, orig->y_height);
5076 ue = calc_plane_error(orig->u_buffer, orig->uv_stride,
5077 recon->u_buffer, recon->uv_stride, orig->uv_width, orig->uv_height);
5079 ve = calc_plane_error(orig->v_buffer, orig->uv_stride,
5080 recon->v_buffer, recon->uv_stride, orig->uv_width, orig->uv_height);
5082 sq_error = ye + ue + ve;
5084 frame_psnr = vp8_mse2psnr(t_samples, 255.0, sq_error);
5086 cpi->total_y += vp8_mse2psnr(y_samples, 255.0, ye);
5087 cpi->total_u += vp8_mse2psnr(uv_samples, 255.0, ue);
5088 cpi->total_v += vp8_mse2psnr(uv_samples, 255.0, ve);
5089 cpi->total_sq_error += sq_error;
5090 cpi->total += frame_psnr;
5093 YV12_BUFFER_CONFIG *pp = &cm->post_proc_buffer;
5094 double frame_psnr2, frame_ssim2 = 0;
5097 vp8_deblock(cm->frame_to_show, &cm->post_proc_buffer, cm->filter_level * 10 / 6, 1, 0);
5098 vp8_clear_system_state();
5100 ye = calc_plane_error(orig->y_buffer, orig->y_stride,
5101 pp->y_buffer, pp->y_stride, orig->y_width, orig->y_height);
5103 ue = calc_plane_error(orig->u_buffer, orig->uv_stride,
5104 pp->u_buffer, pp->uv_stride, orig->uv_width, orig->uv_height);
5106 ve = calc_plane_error(orig->v_buffer, orig->uv_stride,
5107 pp->v_buffer, pp->uv_stride, orig->uv_width, orig->uv_height);
5109 sq_error2 = ye + ue + ve;
5111 frame_psnr2 = vp8_mse2psnr(t_samples, 255.0, sq_error2);
5113 cpi->totalp_y += vp8_mse2psnr(y_samples, 255.0, ye);
5114 cpi->totalp_u += vp8_mse2psnr(uv_samples, 255.0, ue);
5115 cpi->totalp_v += vp8_mse2psnr(uv_samples, 255.0, ve);
5116 cpi->total_sq_error2 += sq_error2;
5117 cpi->totalp += frame_psnr2;
5119 frame_ssim2 = vp8_calc_ssim(cpi->Source,
5120 &cm->post_proc_buffer, 1, &weight);
5122 cpi->summed_quality += frame_ssim2 * weight;
5123 cpi->summed_weights += weight;
5125 if (cpi->oxcf.number_of_layers > 1)
5129 for (i=cpi->current_layer;
5130 i<cpi->oxcf.number_of_layers; i++)
5132 cpi->frames_in_layer[i]++;
5134 cpi->bytes_in_layer[i] += *size;
5135 cpi->sum_psnr[i] += frame_psnr;
5136 cpi->sum_psnr_p[i] += frame_psnr2;
5137 cpi->total_error2[i] += sq_error;
5138 cpi->total_error2_p[i] += sq_error2;
5139 cpi->sum_ssim[i] += frame_ssim2 * weight;
5140 cpi->sum_weights[i] += weight;
5147 if (cpi->b_calculate_ssimg)
5149 double y, u, v, frame_all;
5150 frame_all = vp8_calc_ssimg(cpi->Source, cm->frame_to_show,
5153 if (cpi->oxcf.number_of_layers > 1)
5157 for (i=cpi->current_layer;
5158 i<cpi->oxcf.number_of_layers; i++)
5160 if (!cpi->b_calculate_psnr)
5161 cpi->frames_in_layer[i]++;
5163 cpi->total_ssimg_y_in_layer[i] += y;
5164 cpi->total_ssimg_u_in_layer[i] += u;
5165 cpi->total_ssimg_v_in_layer[i] += v;
5166 cpi->total_ssimg_all_in_layer[i] += frame_all;
5171 cpi->total_ssimg_y += y;
5172 cpi->total_ssimg_u += u;
5173 cpi->total_ssimg_v += v;
5174 cpi->total_ssimg_all += frame_all;
5183 if (cpi->common.frame_type != 0 && cpi->common.base_qindex == cpi->oxcf.worst_allowed_q)
5185 skiptruecount += cpi->skip_true_count;
5186 skipfalsecount += cpi->skip_false_count;
5194 FILE *f = fopen("skip.stt", "a");
5195 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);
5197 if (cpi->is_src_frame_alt_ref == 1)
5198 fprintf(f, "skipcount: %4d framesize: %d\n", cpi->skip_true_count , *size);
5207 #if CONFIG_RUNTIME_CPU_DETECT
5208 if (cm->cpu_caps & HAS_NEON)
5211 vp8_pop_neon(store_reg);
5215 cpi->common.error.setjmp = 0;
5220 int vp8_get_preview_raw_frame(VP8_COMP *cpi, YV12_BUFFER_CONFIG *dest, vp8_ppflags_t *flags)
5222 if (cpi->common.refresh_alt_ref_frame)
5228 #if CONFIG_MULTITHREAD
5229 if(cpi->b_lpf_running)
5231 sem_wait(&cpi->h_event_end_lpf);
5232 cpi->b_lpf_running = 0;
5237 ret = vp8_post_proc_frame(&cpi->common, dest, flags);
5240 if (cpi->common.frame_to_show)
5242 *dest = *cpi->common.frame_to_show;
5243 dest->y_width = cpi->common.Width;
5244 dest->y_height = cpi->common.Height;
5245 dest->uv_height = cpi->common.Height / 2;
5253 #endif //!CONFIG_POSTPROC
5254 vp8_clear_system_state();
5259 int vp8_set_roimap(VP8_COMP *cpi, unsigned char *map, unsigned int rows, unsigned int cols, int delta_q[4], int delta_lf[4], unsigned int threshold[4])
5261 signed char feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS];
5263 if (cpi->common.mb_rows != rows || cpi->common.mb_cols != cols)
5268 disable_segmentation(cpi);
5272 // Set the segmentation Map
5273 set_segmentation_map(cpi, map);
5275 // Activate segmentation.
5276 enable_segmentation(cpi);
5278 // Set up the quant segment data
5279 feature_data[MB_LVL_ALT_Q][0] = delta_q[0];
5280 feature_data[MB_LVL_ALT_Q][1] = delta_q[1];
5281 feature_data[MB_LVL_ALT_Q][2] = delta_q[2];
5282 feature_data[MB_LVL_ALT_Q][3] = delta_q[3];
5284 // Set up the loop segment data s
5285 feature_data[MB_LVL_ALT_LF][0] = delta_lf[0];
5286 feature_data[MB_LVL_ALT_LF][1] = delta_lf[1];
5287 feature_data[MB_LVL_ALT_LF][2] = delta_lf[2];
5288 feature_data[MB_LVL_ALT_LF][3] = delta_lf[3];
5290 cpi->segment_encode_breakout[0] = threshold[0];
5291 cpi->segment_encode_breakout[1] = threshold[1];
5292 cpi->segment_encode_breakout[2] = threshold[2];
5293 cpi->segment_encode_breakout[3] = threshold[3];
5295 // Initialise the feature data structure
5296 // SEGMENT_DELTADATA 0, SEGMENT_ABSDATA 1
5297 set_segment_data(cpi, &feature_data[0][0], SEGMENT_DELTADATA);
5302 int vp8_set_active_map(VP8_COMP *cpi, unsigned char *map, unsigned int rows, unsigned int cols)
5304 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols)
5308 vpx_memcpy(cpi->active_map, map, rows * cols);
5309 cpi->active_map_enabled = 1;
5312 cpi->active_map_enabled = 0;
5318 //cpi->active_map_enabled = 0;
5323 int vp8_set_internal_size(VP8_COMP *cpi, VPX_SCALING horiz_mode, VPX_SCALING vert_mode)
5325 if (horiz_mode <= ONETWO)
5326 cpi->common.horiz_scale = horiz_mode;
5330 if (vert_mode <= ONETWO)
5331 cpi->common.vert_scale = vert_mode;
5340 int vp8_calc_ss_err(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *dest)
5345 unsigned char *src = source->y_buffer;
5346 unsigned char *dst = dest->y_buffer;
5348 // Loop through the Y plane raw and reconstruction data summing (square differences)
5349 for (i = 0; i < source->y_height; i += 16)
5351 for (j = 0; j < source->y_width; j += 16)
5354 Total += vp8_mse16x16(src + j, source->y_stride, dst + j, dest->y_stride, &sse);
5357 src += 16 * source->y_stride;
5358 dst += 16 * dest->y_stride;
5365 int vp8_get_quantizer(VP8_COMP *cpi)
5367 return cpi->common.base_qindex;