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 int width = (cpi->oxcf.Width + 15) & ~15;
1020 int height = (cpi->oxcf.Height + 15) & ~15;
1022 cpi->lookahead = vp8_lookahead_init(cpi->oxcf.Width, cpi->oxcf.Height,
1023 cpi->oxcf.lag_in_frames);
1025 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1026 "Failed to allocate lag buffers");
1028 #if VP8_TEMPORAL_ALT_REF
1030 if (vp8_yv12_alloc_frame_buffer(&cpi->alt_ref_buffer,
1031 width, height, VP8BORDERINPIXELS))
1032 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1033 "Failed to allocate altref buffer");
1039 static void dealloc_raw_frame_buffers(VP8_COMP *cpi)
1041 #if VP8_TEMPORAL_ALT_REF
1042 vp8_yv12_de_alloc_frame_buffer(&cpi->alt_ref_buffer);
1044 vp8_lookahead_destroy(cpi->lookahead);
1048 static int vp8_alloc_partition_data(VP8_COMP *cpi)
1050 vpx_free(cpi->mb.pip);
1052 cpi->mb.pip = vpx_calloc((cpi->common.mb_cols + 1) *
1053 (cpi->common.mb_rows + 1),
1054 sizeof(PARTITION_INFO));
1058 cpi->mb.pi = cpi->mb.pip + cpi->common.mode_info_stride + 1;
1063 void vp8_alloc_compressor_data(VP8_COMP *cpi)
1065 VP8_COMMON *cm = & cpi->common;
1067 int width = cm->Width;
1068 int height = cm->Height;
1070 if (vp8_alloc_frame_buffers(cm, width, height))
1071 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1072 "Failed to allocate frame buffers");
1074 if (vp8_alloc_partition_data(cpi))
1075 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1076 "Failed to allocate partition data");
1079 if ((width & 0xf) != 0)
1080 width += 16 - (width & 0xf);
1082 if ((height & 0xf) != 0)
1083 height += 16 - (height & 0xf);
1086 if (vp8_yv12_alloc_frame_buffer(&cpi->pick_lf_lvl_frame,
1087 width, height, VP8BORDERINPIXELS))
1088 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1089 "Failed to allocate last frame buffer");
1091 if (vp8_yv12_alloc_frame_buffer(&cpi->scaled_source,
1092 width, height, VP8BORDERINPIXELS))
1093 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1094 "Failed to allocate scaled source buffer");
1099 #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
1100 unsigned int tokens = 8 * 24 * 16; /* one MB for each thread */
1102 unsigned int tokens = cm->mb_rows * cm->mb_cols * 24 * 16;
1104 CHECK_MEM_ERROR(cpi->tok, vpx_calloc(tokens, sizeof(*cpi->tok)));
1107 // Data used for real time vc mode to see if gf needs refreshing
1108 cpi->inter_zz_count = 0;
1109 cpi->gf_bad_count = 0;
1110 cpi->gf_update_recommended = 0;
1113 // Structures used to minitor GF usage
1114 vpx_free(cpi->gf_active_flags);
1115 CHECK_MEM_ERROR(cpi->gf_active_flags,
1116 vpx_calloc(1, cm->mb_rows * cm->mb_cols));
1117 cpi->gf_active_count = cm->mb_rows * cm->mb_cols;
1119 vpx_free(cpi->mb_activity_map);
1120 CHECK_MEM_ERROR(cpi->mb_activity_map,
1121 vpx_calloc(sizeof(unsigned int),
1122 cm->mb_rows * cm->mb_cols));
1124 vpx_free(cpi->mb_norm_activity_map);
1125 CHECK_MEM_ERROR(cpi->mb_norm_activity_map,
1126 vpx_calloc(sizeof(unsigned int),
1127 cm->mb_rows * cm->mb_cols));
1129 #if CONFIG_MULTITHREAD
1131 cpi->mt_sync_range = 1;
1132 else if (width <= 1280)
1133 cpi->mt_sync_range = 4;
1134 else if (width <= 2560)
1135 cpi->mt_sync_range = 8;
1137 cpi->mt_sync_range = 16;
1140 vpx_free(cpi->tplist);
1142 CHECK_MEM_ERROR(cpi->tplist, vpx_malloc(sizeof(TOKENLIST) * cpi->common.mb_rows));
1147 static const int q_trans[] =
1149 0, 1, 2, 3, 4, 5, 7, 8,
1150 9, 10, 12, 13, 15, 17, 18, 19,
1151 20, 21, 23, 24, 25, 26, 27, 28,
1152 29, 30, 31, 33, 35, 37, 39, 41,
1153 43, 45, 47, 49, 51, 53, 55, 57,
1154 59, 61, 64, 67, 70, 73, 76, 79,
1155 82, 85, 88, 91, 94, 97, 100, 103,
1156 106, 109, 112, 115, 118, 121, 124, 127,
1159 int vp8_reverse_trans(int x)
1163 for (i = 0; i < 64; i++)
1164 if (q_trans[i] >= x)
1169 void vp8_new_frame_rate(VP8_COMP *cpi, double framerate)
1174 cpi->frame_rate = framerate;
1175 cpi->output_frame_rate = framerate;
1176 cpi->per_frame_bandwidth = (int)(cpi->oxcf.target_bandwidth /
1177 cpi->output_frame_rate);
1178 cpi->av_per_frame_bandwidth = cpi->per_frame_bandwidth;
1179 cpi->min_frame_bandwidth = (int)(cpi->av_per_frame_bandwidth *
1180 cpi->oxcf.two_pass_vbrmin_section / 100);
1182 // Set Maximum gf/arf interval
1183 cpi->max_gf_interval = ((int)(cpi->output_frame_rate / 2.0) + 2);
1185 if(cpi->max_gf_interval < 12)
1186 cpi->max_gf_interval = 12;
1188 // Extended interval for genuinely static scenes
1189 cpi->twopass.static_scene_max_gf_interval = cpi->key_frame_frequency >> 1;
1191 // Special conditions when altr ref frame enabled in lagged compress mode
1192 if (cpi->oxcf.play_alternate && cpi->oxcf.lag_in_frames)
1194 if (cpi->max_gf_interval > cpi->oxcf.lag_in_frames - 1)
1195 cpi->max_gf_interval = cpi->oxcf.lag_in_frames - 1;
1197 if (cpi->twopass.static_scene_max_gf_interval > cpi->oxcf.lag_in_frames - 1)
1198 cpi->twopass.static_scene_max_gf_interval = cpi->oxcf.lag_in_frames - 1;
1201 if ( cpi->max_gf_interval > cpi->twopass.static_scene_max_gf_interval )
1202 cpi->max_gf_interval = cpi->twopass.static_scene_max_gf_interval;
1207 rescale(int val, int num, int denom)
1209 int64_t llnum = num;
1210 int64_t llden = denom;
1211 int64_t llval = val;
1213 return llval * llnum / llden;
1217 static void init_config(VP8_COMP *cpi, VP8_CONFIG *oxcf)
1219 VP8_COMMON *cm = &cpi->common;
1224 cpi->auto_adjust_gold_quantizer = 1;
1227 cm->version = oxcf->Version;
1228 vp8_setup_version(cm);
1230 /* frame rate is not available on the first frame, as it's derived from
1231 * the observed timestamps. The actual value used here doesn't matter
1232 * too much, as it will adapt quickly. If the reciprocal of the timebase
1233 * seems like a reasonable framerate, then use that as a guess, otherwise
1236 cpi->frame_rate = (double)(oxcf->timebase.den) /
1237 (double)(oxcf->timebase.num);
1239 if (cpi->frame_rate > 180)
1240 cpi->frame_rate = 30;
1242 cpi->ref_frame_rate = cpi->frame_rate;
1244 // change includes all joint functionality
1245 vp8_change_config(cpi, oxcf);
1247 // Initialize active best and worst q and average q values.
1248 cpi->active_worst_quality = cpi->oxcf.worst_allowed_q;
1249 cpi->active_best_quality = cpi->oxcf.best_allowed_q;
1250 cpi->avg_frame_qindex = cpi->oxcf.worst_allowed_q;
1252 // Initialise the starting buffer levels
1253 cpi->buffer_level = cpi->oxcf.starting_buffer_level;
1254 cpi->bits_off_target = cpi->oxcf.starting_buffer_level;
1256 cpi->rolling_target_bits = cpi->av_per_frame_bandwidth;
1257 cpi->rolling_actual_bits = cpi->av_per_frame_bandwidth;
1258 cpi->long_rolling_target_bits = cpi->av_per_frame_bandwidth;
1259 cpi->long_rolling_actual_bits = cpi->av_per_frame_bandwidth;
1261 cpi->total_actual_bits = 0;
1262 cpi->total_target_vs_actual = 0;
1264 // Temporal scalabilty
1265 if (cpi->oxcf.number_of_layers > 1)
1268 double prev_layer_frame_rate=0;
1270 for (i=0; i<cpi->oxcf.number_of_layers; i++)
1272 LAYER_CONTEXT *lc = &cpi->layer_context[i];
1274 // Layer configuration
1276 cpi->output_frame_rate / cpi->oxcf.rate_decimator[i];
1277 lc->target_bandwidth = cpi->oxcf.target_bitrate[i] * 1000;
1279 lc->starting_buffer_level_in_ms = oxcf->starting_buffer_level;
1280 lc->optimal_buffer_level_in_ms = oxcf->optimal_buffer_level;
1281 lc->maximum_buffer_size_in_ms = oxcf->maximum_buffer_size;
1283 lc->starting_buffer_level =
1284 rescale(oxcf->starting_buffer_level,
1285 lc->target_bandwidth, 1000);
1287 if (oxcf->optimal_buffer_level == 0)
1288 lc->optimal_buffer_level = lc->target_bandwidth / 8;
1290 lc->optimal_buffer_level =
1291 rescale(oxcf->optimal_buffer_level,
1292 lc->target_bandwidth, 1000);
1294 if (oxcf->maximum_buffer_size == 0)
1295 lc->maximum_buffer_size = lc->target_bandwidth / 8;
1297 lc->maximum_buffer_size =
1298 rescale(oxcf->maximum_buffer_size,
1299 lc->target_bandwidth, 1000);
1301 // Work out the average size of a frame within this layer
1303 lc->avg_frame_size_for_layer = (cpi->oxcf.target_bitrate[i] -
1304 cpi->oxcf.target_bitrate[i-1]) * 1000 /
1305 (lc->frame_rate - prev_layer_frame_rate);
1307 lc->active_worst_quality = cpi->oxcf.worst_allowed_q;
1308 lc->active_best_quality = cpi->oxcf.best_allowed_q;
1309 lc->avg_frame_qindex = cpi->oxcf.worst_allowed_q;
1311 lc->buffer_level = lc->starting_buffer_level;
1312 lc->bits_off_target = lc->starting_buffer_level;
1314 lc->total_actual_bits = 0;
1318 lc->rate_correction_factor = 1.0;
1319 lc->key_frame_rate_correction_factor = 1.0;
1320 lc->gf_rate_correction_factor = 1.0;
1321 lc->inter_frame_target = 0.0;
1323 prev_layer_frame_rate = lc->frame_rate;
1327 #if VP8_TEMPORAL_ALT_REF
1331 cpi->fixed_divide[0] = 0;
1333 for (i = 1; i < 512; i++)
1334 cpi->fixed_divide[i] = 0x80000 / i;
1339 void update_layer_contexts (VP8_COMP *cpi)
1341 VP8_CONFIG *oxcf = &cpi->oxcf;
1343 /* Update snapshots of the layer contexts to reflect new parameters */
1344 if (oxcf->number_of_layers > 1)
1347 double prev_layer_frame_rate=0;
1349 for (i=0; i<oxcf->number_of_layers; i++)
1351 LAYER_CONTEXT *lc = &cpi->layer_context[i];
1354 cpi->ref_frame_rate / oxcf->rate_decimator[i];
1355 lc->target_bandwidth = oxcf->target_bitrate[i] * 1000;
1357 lc->starting_buffer_level = rescale(
1358 oxcf->starting_buffer_level_in_ms,
1359 lc->target_bandwidth, 1000);
1361 if (oxcf->optimal_buffer_level == 0)
1362 lc->optimal_buffer_level = lc->target_bandwidth / 8;
1364 lc->optimal_buffer_level = rescale(
1365 oxcf->optimal_buffer_level_in_ms,
1366 lc->target_bandwidth, 1000);
1368 if (oxcf->maximum_buffer_size == 0)
1369 lc->maximum_buffer_size = lc->target_bandwidth / 8;
1371 lc->maximum_buffer_size = rescale(
1372 oxcf->maximum_buffer_size_in_ms,
1373 lc->target_bandwidth, 1000);
1375 // Work out the average size of a frame within this layer
1377 lc->avg_frame_size_for_layer = (oxcf->target_bitrate[i] -
1378 oxcf->target_bitrate[i-1]) * 1000 /
1379 (lc->frame_rate - prev_layer_frame_rate);
1381 lc->active_worst_quality = oxcf->worst_allowed_q;
1382 lc->active_best_quality = oxcf->best_allowed_q;
1383 lc->avg_frame_qindex = oxcf->worst_allowed_q;
1385 prev_layer_frame_rate = lc->frame_rate;
1390 void vp8_change_config(VP8_COMP *cpi, VP8_CONFIG *oxcf)
1392 VP8_COMMON *cm = &cpi->common;
1401 if (cm->version != oxcf->Version)
1403 cm->version = oxcf->Version;
1404 vp8_setup_version(cm);
1409 switch (cpi->oxcf.Mode)
1414 cpi->compressor_speed = 2;
1416 if (cpi->oxcf.cpu_used < -16)
1418 cpi->oxcf.cpu_used = -16;
1421 if (cpi->oxcf.cpu_used > 16)
1422 cpi->oxcf.cpu_used = 16;
1426 case MODE_GOODQUALITY:
1428 cpi->compressor_speed = 1;
1430 if (cpi->oxcf.cpu_used < -5)
1432 cpi->oxcf.cpu_used = -5;
1435 if (cpi->oxcf.cpu_used > 5)
1436 cpi->oxcf.cpu_used = 5;
1440 case MODE_BESTQUALITY:
1442 cpi->compressor_speed = 0;
1445 case MODE_FIRSTPASS:
1447 cpi->compressor_speed = 1;
1449 case MODE_SECONDPASS:
1451 cpi->compressor_speed = 1;
1453 if (cpi->oxcf.cpu_used < -5)
1455 cpi->oxcf.cpu_used = -5;
1458 if (cpi->oxcf.cpu_used > 5)
1459 cpi->oxcf.cpu_used = 5;
1462 case MODE_SECONDPASS_BEST:
1464 cpi->compressor_speed = 0;
1469 cpi->auto_worst_q = 1;
1471 cpi->oxcf.worst_allowed_q = q_trans[oxcf->worst_allowed_q];
1472 cpi->oxcf.best_allowed_q = q_trans[oxcf->best_allowed_q];
1473 cpi->oxcf.cq_level = q_trans[cpi->oxcf.cq_level];
1475 if (oxcf->fixed_q >= 0)
1477 if (oxcf->worst_allowed_q < 0)
1478 cpi->oxcf.fixed_q = q_trans[0];
1480 cpi->oxcf.fixed_q = q_trans[oxcf->worst_allowed_q];
1482 if (oxcf->alt_q < 0)
1483 cpi->oxcf.alt_q = q_trans[0];
1485 cpi->oxcf.alt_q = q_trans[oxcf->alt_q];
1487 if (oxcf->key_q < 0)
1488 cpi->oxcf.key_q = q_trans[0];
1490 cpi->oxcf.key_q = q_trans[oxcf->key_q];
1492 if (oxcf->gold_q < 0)
1493 cpi->oxcf.gold_q = q_trans[0];
1495 cpi->oxcf.gold_q = q_trans[oxcf->gold_q];
1499 cpi->baseline_gf_interval =
1500 cpi->oxcf.alt_freq ? cpi->oxcf.alt_freq : DEFAULT_GF_INTERVAL;
1502 cpi->ref_frame_flags = VP8_ALT_FLAG | VP8_GOLD_FLAG | VP8_LAST_FLAG;
1504 //cpi->use_golden_frame_only = 0;
1505 //cpi->use_last_frame_only = 0;
1506 cm->refresh_golden_frame = 0;
1507 cm->refresh_last_frame = 1;
1508 cm->refresh_entropy_probs = 1;
1510 #if (CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
1511 cpi->oxcf.token_partitions = 3;
1514 if (cpi->oxcf.token_partitions >= 0 && cpi->oxcf.token_partitions <= 3)
1515 cm->multi_token_partition =
1516 (TOKEN_PARTITION) cpi->oxcf.token_partitions;
1518 setup_features(cpi);
1523 for (i = 0; i < MAX_MB_SEGMENTS; i++)
1524 cpi->segment_encode_breakout[i] = cpi->oxcf.encode_breakout;
1527 // At the moment the first order values may not be > MAXQ
1528 if (cpi->oxcf.fixed_q > MAXQ)
1529 cpi->oxcf.fixed_q = MAXQ;
1531 // local file playback mode == really big buffer
1532 if (cpi->oxcf.end_usage == USAGE_LOCAL_FILE_PLAYBACK)
1534 cpi->oxcf.starting_buffer_level = 60000;
1535 cpi->oxcf.optimal_buffer_level = 60000;
1536 cpi->oxcf.maximum_buffer_size = 240000;
1537 cpi->oxcf.starting_buffer_level_in_ms = 60000;
1538 cpi->oxcf.optimal_buffer_level_in_ms = 60000;
1539 cpi->oxcf.maximum_buffer_size_in_ms = 240000;
1542 // Convert target bandwidth from Kbit/s to Bit/s
1543 cpi->oxcf.target_bandwidth *= 1000;
1545 cpi->oxcf.starting_buffer_level =
1546 rescale(cpi->oxcf.starting_buffer_level,
1547 cpi->oxcf.target_bandwidth, 1000);
1549 // Set or reset optimal and maximum buffer levels.
1550 if (cpi->oxcf.optimal_buffer_level == 0)
1551 cpi->oxcf.optimal_buffer_level = cpi->oxcf.target_bandwidth / 8;
1553 cpi->oxcf.optimal_buffer_level =
1554 rescale(cpi->oxcf.optimal_buffer_level,
1555 cpi->oxcf.target_bandwidth, 1000);
1557 if (cpi->oxcf.maximum_buffer_size == 0)
1558 cpi->oxcf.maximum_buffer_size = cpi->oxcf.target_bandwidth / 8;
1560 cpi->oxcf.maximum_buffer_size =
1561 rescale(cpi->oxcf.maximum_buffer_size,
1562 cpi->oxcf.target_bandwidth, 1000);
1564 // Set up frame rate and related parameters rate control values.
1565 vp8_new_frame_rate(cpi, cpi->frame_rate);
1567 // Set absolute upper and lower quality limits
1568 cpi->worst_quality = cpi->oxcf.worst_allowed_q;
1569 cpi->best_quality = cpi->oxcf.best_allowed_q;
1571 // active values should only be modified if out of new range
1572 if (cpi->active_worst_quality > cpi->oxcf.worst_allowed_q)
1574 cpi->active_worst_quality = cpi->oxcf.worst_allowed_q;
1577 else if (cpi->active_worst_quality < cpi->oxcf.best_allowed_q)
1579 cpi->active_worst_quality = cpi->oxcf.best_allowed_q;
1581 if (cpi->active_best_quality < cpi->oxcf.best_allowed_q)
1583 cpi->active_best_quality = cpi->oxcf.best_allowed_q;
1586 else if (cpi->active_best_quality > cpi->oxcf.worst_allowed_q)
1588 cpi->active_best_quality = cpi->oxcf.worst_allowed_q;
1591 cpi->buffered_mode = cpi->oxcf.optimal_buffer_level > 0;
1593 cpi->cq_target_quality = cpi->oxcf.cq_level;
1595 // Only allow dropped frames in buffered mode
1596 cpi->drop_frames_allowed = cpi->oxcf.allow_df && cpi->buffered_mode;
1598 cpi->target_bandwidth = cpi->oxcf.target_bandwidth;
1602 last_h = cm->Height;
1604 cm->Width = cpi->oxcf.Width;
1605 cm->Height = cpi->oxcf.Height;
1607 cm->horiz_scale = cpi->horiz_scale;
1608 cm->vert_scale = cpi->vert_scale;
1610 // VP8 sharpness level mapping 0-7 (vs 0-10 in general VPx dialogs)
1611 if (cpi->oxcf.Sharpness > 7)
1612 cpi->oxcf.Sharpness = 7;
1614 cm->sharpness_level = cpi->oxcf.Sharpness;
1616 if (cm->horiz_scale != NORMAL || cm->vert_scale != NORMAL)
1618 int UNINITIALIZED_IS_SAFE(hr), UNINITIALIZED_IS_SAFE(hs);
1619 int UNINITIALIZED_IS_SAFE(vr), UNINITIALIZED_IS_SAFE(vs);
1621 Scale2Ratio(cm->horiz_scale, &hr, &hs);
1622 Scale2Ratio(cm->vert_scale, &vr, &vs);
1624 // always go to the next whole number
1625 cm->Width = (hs - 1 + cpi->oxcf.Width * hr) / hs;
1626 cm->Height = (vs - 1 + cpi->oxcf.Height * vr) / vs;
1629 if (last_w != cm->Width || last_h != cm->Height)
1630 cpi->force_next_frame_intra = 1;
1632 if (((cm->Width + 15) & 0xfffffff0) !=
1633 cm->yv12_fb[cm->lst_fb_idx].y_width ||
1634 ((cm->Height + 15) & 0xfffffff0) !=
1635 cm->yv12_fb[cm->lst_fb_idx].y_height ||
1636 cm->yv12_fb[cm->lst_fb_idx].y_width == 0)
1638 alloc_raw_frame_buffers(cpi);
1639 vp8_alloc_compressor_data(cpi);
1642 if (cpi->oxcf.fixed_q >= 0)
1644 cpi->last_q[0] = cpi->oxcf.fixed_q;
1645 cpi->last_q[1] = cpi->oxcf.fixed_q;
1648 cpi->Speed = cpi->oxcf.cpu_used;
1650 // force to allowlag to 0 if lag_in_frames is 0;
1651 if (cpi->oxcf.lag_in_frames == 0)
1653 cpi->oxcf.allow_lag = 0;
1655 // Limit on lag buffers as these are not currently dynamically allocated
1656 else if (cpi->oxcf.lag_in_frames > MAX_LAG_BUFFERS)
1657 cpi->oxcf.lag_in_frames = MAX_LAG_BUFFERS;
1660 cpi->alt_ref_source = NULL;
1661 cpi->is_src_frame_alt_ref = 0;
1663 #if CONFIG_TEMPORAL_DENOISING
1664 if (cpi->oxcf.noise_sensitivity)
1666 if (!cpi->denoiser.yv12_mc_running_avg.buffer_alloc)
1668 int width = (cpi->oxcf.Width + 15) & ~15;
1669 int height = (cpi->oxcf.Height + 15) & ~15;
1670 vp8_denoiser_allocate(&cpi->denoiser, width, height);
1676 // Experimental RD Code
1677 cpi->frame_distortion = 0;
1678 cpi->last_frame_distortion = 0;
1683 #define M_LOG2_E 0.693147180559945309417
1684 #define log2f(x) (log (x) / (float) M_LOG2_E)
1685 static void cal_mvsadcosts(int *mvsadcost[2])
1689 mvsadcost [0] [0] = 300;
1690 mvsadcost [1] [0] = 300;
1694 double z = 256 * (2 * (log2f(8 * i) + .6));
1695 mvsadcost [0][i] = (int) z;
1696 mvsadcost [1][i] = (int) z;
1697 mvsadcost [0][-i] = (int) z;
1698 mvsadcost [1][-i] = (int) z;
1700 while (++i <= mvfp_max);
1703 struct VP8_COMP* vp8_create_compressor(VP8_CONFIG *oxcf)
1710 cpi = vpx_memalign(32, sizeof(VP8_COMP));
1711 // Check that the CPI instance is valid
1717 vpx_memset(cpi, 0, sizeof(VP8_COMP));
1719 if (setjmp(cm->error.jmp))
1721 cpi->common.error.setjmp = 0;
1722 vp8_remove_compressor(&cpi);
1726 cpi->common.error.setjmp = 1;
1728 CHECK_MEM_ERROR(cpi->mb.ss, vpx_calloc(sizeof(search_site), (MAX_MVSEARCH_STEPS * 8) + 1));
1730 vp8_create_common(&cpi->common);
1732 init_config(cpi, oxcf);
1734 memcpy(cpi->base_skip_false_prob, vp8cx_base_skip_false_prob, sizeof(vp8cx_base_skip_false_prob));
1735 cpi->common.current_video_frame = 0;
1736 cpi->kf_overspend_bits = 0;
1737 cpi->kf_bitrate_adjustment = 0;
1738 cpi->frames_till_gf_update_due = 0;
1739 cpi->gf_overspend_bits = 0;
1740 cpi->non_gf_bitrate_adjustment = 0;
1741 cpi->prob_last_coded = 128;
1742 cpi->prob_gf_coded = 128;
1743 cpi->prob_intra_coded = 63;
1745 // Prime the recent reference frame usage counters.
1746 // Hereafter they will be maintained as a sort of moving average
1747 cpi->recent_ref_frame_usage[INTRA_FRAME] = 1;
1748 cpi->recent_ref_frame_usage[LAST_FRAME] = 1;
1749 cpi->recent_ref_frame_usage[GOLDEN_FRAME] = 1;
1750 cpi->recent_ref_frame_usage[ALTREF_FRAME] = 1;
1752 // Set reference frame sign bias for ALTREF frame to 1 (for now)
1753 cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = 1;
1755 cpi->twopass.gf_decay_rate = 0;
1756 cpi->baseline_gf_interval = DEFAULT_GF_INTERVAL;
1758 cpi->gold_is_last = 0 ;
1759 cpi->alt_is_last = 0 ;
1760 cpi->gold_is_alt = 0 ;
1762 // allocate memory for storing last frame's MVs for MV prediction.
1763 CHECK_MEM_ERROR(cpi->lfmv, vpx_calloc((cpi->common.mb_rows+2) * (cpi->common.mb_cols+2), sizeof(int_mv)));
1764 CHECK_MEM_ERROR(cpi->lf_ref_frame_sign_bias, vpx_calloc((cpi->common.mb_rows+2) * (cpi->common.mb_cols+2), sizeof(int)));
1765 CHECK_MEM_ERROR(cpi->lf_ref_frame, vpx_calloc((cpi->common.mb_rows+2) * (cpi->common.mb_cols+2), sizeof(int)));
1767 // Create the encoder segmentation map and set all entries to 0
1768 CHECK_MEM_ERROR(cpi->segmentation_map, vpx_calloc(cpi->common.mb_rows * cpi->common.mb_cols, 1));
1769 CHECK_MEM_ERROR(cpi->active_map, vpx_calloc(cpi->common.mb_rows * cpi->common.mb_cols, 1));
1770 vpx_memset(cpi->active_map , 1, (cpi->common.mb_rows * cpi->common.mb_cols));
1771 cpi->active_map_enabled = 0;
1774 // Experimental code for lagged and one pass
1775 // Initialise one_pass GF frames stats
1776 // Update stats used for GF selection
1779 cpi->one_pass_frame_index = 0;
1781 for (i = 0; i < MAX_LAG_BUFFERS; i++)
1783 cpi->one_pass_frame_stats[i].frames_so_far = 0;
1784 cpi->one_pass_frame_stats[i].frame_intra_error = 0.0;
1785 cpi->one_pass_frame_stats[i].frame_coded_error = 0.0;
1786 cpi->one_pass_frame_stats[i].frame_pcnt_inter = 0.0;
1787 cpi->one_pass_frame_stats[i].frame_pcnt_motion = 0.0;
1788 cpi->one_pass_frame_stats[i].frame_mvr = 0.0;
1789 cpi->one_pass_frame_stats[i].frame_mvr_abs = 0.0;
1790 cpi->one_pass_frame_stats[i].frame_mvc = 0.0;
1791 cpi->one_pass_frame_stats[i].frame_mvc_abs = 0.0;
1796 // Should we use the cyclic refresh method.
1797 // Currently this is tied to error resilliant mode
1798 cpi->cyclic_refresh_mode_enabled = cpi->oxcf.error_resilient_mode;
1799 cpi->cyclic_refresh_mode_max_mbs_perframe = (cpi->common.mb_rows * cpi->common.mb_cols) / 40;
1800 cpi->cyclic_refresh_mode_index = 0;
1801 cpi->cyclic_refresh_q = 32;
1803 if (cpi->cyclic_refresh_mode_enabled)
1805 CHECK_MEM_ERROR(cpi->cyclic_refresh_map, vpx_calloc((cpi->common.mb_rows * cpi->common.mb_cols), 1));
1808 cpi->cyclic_refresh_map = (signed char *) NULL;
1810 // Test function for segmentation
1811 //segmentation_test_function( cpi);
1813 #ifdef ENTROPY_STATS
1814 init_context_counters();
1817 /*Initialize the feed-forward activity masking.*/
1818 cpi->activity_avg = 90<<12;
1820 cpi->frames_since_key = 8; // Give a sensible default for the first frame.
1821 cpi->key_frame_frequency = cpi->oxcf.key_freq;
1822 cpi->this_key_frame_forced = 0;
1823 cpi->next_key_frame_forced = 0;
1825 cpi->source_alt_ref_pending = 0;
1826 cpi->source_alt_ref_active = 0;
1827 cpi->common.refresh_alt_ref_frame = 0;
1829 cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
1830 #if CONFIG_INTERNAL_STATS
1831 cpi->b_calculate_ssimg = 0;
1836 if (cpi->b_calculate_psnr)
1838 cpi->total_sq_error = 0.0;
1839 cpi->total_sq_error2 = 0.0;
1844 cpi->totalp_y = 0.0;
1845 cpi->totalp_u = 0.0;
1846 cpi->totalp_v = 0.0;
1848 cpi->tot_recode_hits = 0;
1849 cpi->summed_quality = 0;
1850 cpi->summed_weights = 0;
1853 if (cpi->b_calculate_ssimg)
1855 cpi->total_ssimg_y = 0;
1856 cpi->total_ssimg_u = 0;
1857 cpi->total_ssimg_v = 0;
1858 cpi->total_ssimg_all = 0;
1864 #define LLONG_MAX 9223372036854775807LL
1866 cpi->first_time_stamp_ever = LLONG_MAX;
1868 cpi->frames_till_gf_update_due = 0;
1869 cpi->key_frame_count = 1;
1871 cpi->ni_av_qi = cpi->oxcf.worst_allowed_q;
1874 cpi->total_byte_count = 0;
1876 cpi->drop_frame = 0;
1877 cpi->drop_count = 0;
1878 cpi->max_drop_count = 0;
1879 cpi->max_consec_dropped_frames = 4;
1881 cpi->rate_correction_factor = 1.0;
1882 cpi->key_frame_rate_correction_factor = 1.0;
1883 cpi->gf_rate_correction_factor = 1.0;
1884 cpi->twopass.est_max_qcorrection_factor = 1.0;
1886 for (i = 0; i < KEY_FRAME_CONTEXT; i++)
1888 cpi->prior_key_frame_distance[i] = (int)cpi->output_frame_rate;
1891 #ifdef OUTPUT_YUV_SRC
1892 yuv_file = fopen("bd.yuv", "ab");
1896 framepsnr = fopen("framepsnr.stt", "a");
1897 kf_list = fopen("kf_list.stt", "w");
1900 cpi->output_pkt_list = oxcf->output_pkt_list;
1902 #if !(CONFIG_REALTIME_ONLY)
1906 vp8_init_first_pass(cpi);
1908 else if (cpi->pass == 2)
1910 size_t packet_sz = sizeof(FIRSTPASS_STATS);
1911 int packets = oxcf->two_pass_stats_in.sz / packet_sz;
1913 cpi->twopass.stats_in_start = oxcf->two_pass_stats_in.buf;
1914 cpi->twopass.stats_in = cpi->twopass.stats_in_start;
1915 cpi->twopass.stats_in_end = (void*)((char *)cpi->twopass.stats_in
1916 + (packets - 1) * packet_sz);
1917 vp8_init_second_pass(cpi);
1922 if (cpi->compressor_speed == 2)
1924 cpi->cpu_freq = 0; //vp8_get_processor_freq();
1925 cpi->avg_encode_time = 0;
1926 cpi->avg_pick_mode_time = 0;
1929 vp8_set_speed_features(cpi);
1931 // Set starting values of RD threshold multipliers (128 = *1)
1932 for (i = 0; i < MAX_MODES; i++)
1934 cpi->rd_thresh_mult[i] = 128;
1937 #ifdef ENTROPY_STATS
1938 init_mv_ref_counts();
1941 #if CONFIG_MULTITHREAD
1942 vp8cx_create_encoder_threads(cpi);
1945 cpi->fn_ptr[BLOCK_16X16].sdf = vp8_sad16x16;
1946 cpi->fn_ptr[BLOCK_16X16].vf = vp8_variance16x16;
1947 cpi->fn_ptr[BLOCK_16X16].svf = vp8_sub_pixel_variance16x16;
1948 cpi->fn_ptr[BLOCK_16X16].svf_halfpix_h = vp8_variance_halfpixvar16x16_h;
1949 cpi->fn_ptr[BLOCK_16X16].svf_halfpix_v = vp8_variance_halfpixvar16x16_v;
1950 cpi->fn_ptr[BLOCK_16X16].svf_halfpix_hv = vp8_variance_halfpixvar16x16_hv;
1951 cpi->fn_ptr[BLOCK_16X16].sdx3f = vp8_sad16x16x3;
1952 cpi->fn_ptr[BLOCK_16X16].sdx8f = vp8_sad16x16x8;
1953 cpi->fn_ptr[BLOCK_16X16].sdx4df = vp8_sad16x16x4d;
1955 cpi->fn_ptr[BLOCK_16X8].sdf = vp8_sad16x8;
1956 cpi->fn_ptr[BLOCK_16X8].vf = vp8_variance16x8;
1957 cpi->fn_ptr[BLOCK_16X8].svf = vp8_sub_pixel_variance16x8;
1958 cpi->fn_ptr[BLOCK_16X8].svf_halfpix_h = NULL;
1959 cpi->fn_ptr[BLOCK_16X8].svf_halfpix_v = NULL;
1960 cpi->fn_ptr[BLOCK_16X8].svf_halfpix_hv = NULL;
1961 cpi->fn_ptr[BLOCK_16X8].sdx3f = vp8_sad16x8x3;
1962 cpi->fn_ptr[BLOCK_16X8].sdx8f = vp8_sad16x8x8;
1963 cpi->fn_ptr[BLOCK_16X8].sdx4df = vp8_sad16x8x4d;
1965 cpi->fn_ptr[BLOCK_8X16].sdf = vp8_sad8x16;
1966 cpi->fn_ptr[BLOCK_8X16].vf = vp8_variance8x16;
1967 cpi->fn_ptr[BLOCK_8X16].svf = vp8_sub_pixel_variance8x16;
1968 cpi->fn_ptr[BLOCK_8X16].svf_halfpix_h = NULL;
1969 cpi->fn_ptr[BLOCK_8X16].svf_halfpix_v = NULL;
1970 cpi->fn_ptr[BLOCK_8X16].svf_halfpix_hv = NULL;
1971 cpi->fn_ptr[BLOCK_8X16].sdx3f = vp8_sad8x16x3;
1972 cpi->fn_ptr[BLOCK_8X16].sdx8f = vp8_sad8x16x8;
1973 cpi->fn_ptr[BLOCK_8X16].sdx4df = vp8_sad8x16x4d;
1975 cpi->fn_ptr[BLOCK_8X8].sdf = vp8_sad8x8;
1976 cpi->fn_ptr[BLOCK_8X8].vf = vp8_variance8x8;
1977 cpi->fn_ptr[BLOCK_8X8].svf = vp8_sub_pixel_variance8x8;
1978 cpi->fn_ptr[BLOCK_8X8].svf_halfpix_h = NULL;
1979 cpi->fn_ptr[BLOCK_8X8].svf_halfpix_v = NULL;
1980 cpi->fn_ptr[BLOCK_8X8].svf_halfpix_hv = NULL;
1981 cpi->fn_ptr[BLOCK_8X8].sdx3f = vp8_sad8x8x3;
1982 cpi->fn_ptr[BLOCK_8X8].sdx8f = vp8_sad8x8x8;
1983 cpi->fn_ptr[BLOCK_8X8].sdx4df = vp8_sad8x8x4d;
1985 cpi->fn_ptr[BLOCK_4X4].sdf = vp8_sad4x4;
1986 cpi->fn_ptr[BLOCK_4X4].vf = vp8_variance4x4;
1987 cpi->fn_ptr[BLOCK_4X4].svf = vp8_sub_pixel_variance4x4;
1988 cpi->fn_ptr[BLOCK_4X4].svf_halfpix_h = NULL;
1989 cpi->fn_ptr[BLOCK_4X4].svf_halfpix_v = NULL;
1990 cpi->fn_ptr[BLOCK_4X4].svf_halfpix_hv = NULL;
1991 cpi->fn_ptr[BLOCK_4X4].sdx3f = vp8_sad4x4x3;
1992 cpi->fn_ptr[BLOCK_4X4].sdx8f = vp8_sad4x4x8;
1993 cpi->fn_ptr[BLOCK_4X4].sdx4df = vp8_sad4x4x4d;
1995 #if ARCH_X86 || ARCH_X86_64
1996 cpi->fn_ptr[BLOCK_16X16].copymem = vp8_copy32xn;
1997 cpi->fn_ptr[BLOCK_16X8].copymem = vp8_copy32xn;
1998 cpi->fn_ptr[BLOCK_8X16].copymem = vp8_copy32xn;
1999 cpi->fn_ptr[BLOCK_8X8].copymem = vp8_copy32xn;
2000 cpi->fn_ptr[BLOCK_4X4].copymem = vp8_copy32xn;
2003 cpi->full_search_sad = vp8_full_search_sad;
2004 cpi->diamond_search_sad = vp8_diamond_search_sad;
2005 cpi->refining_search_sad = vp8_refining_search_sad;
2007 // make sure frame 1 is okay
2008 cpi->error_bins[0] = cpi->common.MBs;
2010 //vp8cx_init_quantizer() is first called here. Add check in vp8cx_frame_init_quantizer() so that vp8cx_init_quantizer is only called later
2011 //when needed. This will avoid unnecessary calls of vp8cx_init_quantizer() for every frame.
2012 vp8cx_init_quantizer(cpi);
2014 vp8_loop_filter_init(cm);
2016 cpi->common.error.setjmp = 0;
2018 #if CONFIG_MULTI_RES_ENCODING
2020 /* Calculate # of MBs in a row in lower-resolution level image. */
2021 if (cpi->oxcf.mr_encoder_id > 0)
2022 vp8_cal_low_res_mb_cols(cpi);
2026 /* setup RD costs to MACROBLOCK struct */
2028 cpi->mb.mvcost[0] = &cpi->rd_costs.mvcosts[0][mv_max+1];
2029 cpi->mb.mvcost[1] = &cpi->rd_costs.mvcosts[1][mv_max+1];
2030 cpi->mb.mvsadcost[0] = &cpi->rd_costs.mvsadcosts[0][mvfp_max+1];
2031 cpi->mb.mvsadcost[1] = &cpi->rd_costs.mvsadcosts[1][mvfp_max+1];
2033 cal_mvsadcosts(cpi->mb.mvsadcost);
2035 cpi->mb.mbmode_cost = cpi->rd_costs.mbmode_cost;
2036 cpi->mb.intra_uv_mode_cost = cpi->rd_costs.intra_uv_mode_cost;
2037 cpi->mb.bmode_costs = cpi->rd_costs.bmode_costs;
2038 cpi->mb.inter_bmode_costs = cpi->rd_costs.inter_bmode_costs;
2039 cpi->mb.token_costs = cpi->rd_costs.token_costs;
2045 void vp8_remove_compressor(VP8_COMP **ptr)
2047 VP8_COMP *cpi = *ptr;
2052 if (cpi && (cpi->common.current_video_frame > 0))
2054 #if !(CONFIG_REALTIME_ONLY)
2058 vp8_end_second_pass(cpi);
2063 #ifdef ENTROPY_STATS
2064 print_context_counters();
2065 print_tree_update_probs();
2066 print_mode_context();
2069 #if CONFIG_INTERNAL_STATS
2073 FILE *f = fopen("opsnr.stt", "a");
2074 double time_encoded = (cpi->last_end_time_stamp_seen
2075 - cpi->first_time_stamp_ever) / 10000000.000;
2076 double total_encode_time = (cpi->time_receive_data +
2077 cpi->time_compress_data) / 1000.000;
2078 double dr = (double)cpi->bytes * 8.0 / 1000.0 / time_encoded;
2080 if (cpi->b_calculate_psnr)
2082 YV12_BUFFER_CONFIG *lst_yv12 =
2083 &cpi->common.yv12_fb[cpi->common.lst_fb_idx];
2085 if (cpi->oxcf.number_of_layers > 1)
2089 fprintf(f, "Layer\tBitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\t"
2090 "GLPsnrP\tVPXSSIM\t\n");
2091 for (i=0; i<cpi->oxcf.number_of_layers; i++)
2093 double dr = (double)cpi->bytes_in_layer[i] *
2094 8.0 / 1000.0 / time_encoded;
2095 double samples = 3.0 / 2 * cpi->frames_in_layer[i] *
2096 lst_yv12->y_width * lst_yv12->y_height;
2097 double total_psnr = vp8_mse2psnr(samples, 255.0,
2098 cpi->total_error2[i]);
2099 double total_psnr2 = vp8_mse2psnr(samples, 255.0,
2100 cpi->total_error2_p[i]);
2101 double total_ssim = 100 * pow(cpi->sum_ssim[i] /
2102 cpi->sum_weights[i], 8.0);
2104 fprintf(f, "%5d\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
2107 cpi->sum_psnr[i] / cpi->frames_in_layer[i],
2109 cpi->sum_psnr_p[i] / cpi->frames_in_layer[i],
2110 total_psnr2, total_ssim);
2115 double samples = 3.0 / 2 * cpi->count *
2116 lst_yv12->y_width * lst_yv12->y_height;
2117 double total_psnr = vp8_mse2psnr(samples, 255.0,
2118 cpi->total_sq_error);
2119 double total_psnr2 = vp8_mse2psnr(samples, 255.0,
2120 cpi->total_sq_error2);
2121 double total_ssim = 100 * pow(cpi->summed_quality /
2122 cpi->summed_weights, 8.0);
2124 fprintf(f, "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\t"
2125 "GLPsnrP\tVPXSSIM\t Time(us)\n");
2126 fprintf(f, "%7.3f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
2128 dr, cpi->total / cpi->count, total_psnr,
2129 cpi->totalp / cpi->count, total_psnr2,
2130 total_ssim, total_encode_time);
2134 if (cpi->b_calculate_ssimg)
2136 if (cpi->oxcf.number_of_layers > 1)
2140 fprintf(f, "Layer\tBitRate\tSSIM_Y\tSSIM_U\tSSIM_V\tSSIM_A\t"
2142 for (i=0; i<cpi->oxcf.number_of_layers; i++)
2144 double dr = (double)cpi->bytes_in_layer[i] *
2145 8.0 / 1000.0 / time_encoded;
2146 fprintf(f, "%5d\t%7.3f\t%6.4f\t"
2147 "%6.4f\t%6.4f\t%6.4f\t%8.0f\n",
2149 cpi->total_ssimg_y_in_layer[i] /
2150 cpi->frames_in_layer[i],
2151 cpi->total_ssimg_u_in_layer[i] /
2152 cpi->frames_in_layer[i],
2153 cpi->total_ssimg_v_in_layer[i] /
2154 cpi->frames_in_layer[i],
2155 cpi->total_ssimg_all_in_layer[i] /
2156 cpi->frames_in_layer[i],
2162 fprintf(f, "BitRate\tSSIM_Y\tSSIM_U\tSSIM_V\tSSIM_A\t"
2164 fprintf(f, "%7.3f\t%6.4f\t%6.4f\t%6.4f\t%6.4f\t%8.0f\n", dr,
2165 cpi->total_ssimg_y / cpi->count,
2166 cpi->total_ssimg_u / cpi->count,
2167 cpi->total_ssimg_v / cpi->count,
2168 cpi->total_ssimg_all / cpi->count, total_encode_time);
2174 f = fopen("qskip.stt", "a");
2175 fprintf(f, "minq:%d -maxq:%d skiptrue:skipfalse = %d:%d\n", cpi->oxcf.best_allowed_q, cpi->oxcf.worst_allowed_q, skiptruecount, skipfalsecount);
2186 if (cpi->compressor_speed == 2)
2189 FILE *f = fopen("cxspeed.stt", "a");
2190 cnt_pm /= cpi->common.MBs;
2192 for (i = 0; i < 16; i++)
2193 fprintf(f, "%5d", frames_at_speed[i]);
2196 //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);
2205 extern int count_mb_seg[4];
2206 FILE *f = fopen("modes.stt", "a");
2207 double dr = (double)cpi->frame_rate * (double)bytes * (double)8 / (double)count / (double)1000 ;
2208 fprintf(f, "intra_mode in Intra Frames:\n");
2209 fprintf(f, "Y: %8d, %8d, %8d, %8d, %8d\n", y_modes[0], y_modes[1], y_modes[2], y_modes[3], y_modes[4]);
2210 fprintf(f, "UV:%8d, %8d, %8d, %8d\n", uv_modes[0], uv_modes[1], uv_modes[2], uv_modes[3]);
2215 for (i = 0; i < 10; i++)
2216 fprintf(f, "%8d, ", b_modes[i]);
2222 fprintf(f, "Modes in Inter Frames:\n");
2223 fprintf(f, "Y: %8d, %8d, %8d, %8d, %8d, %8d, %8d, %8d, %8d, %8d\n",
2224 inter_y_modes[0], inter_y_modes[1], inter_y_modes[2], inter_y_modes[3], inter_y_modes[4],
2225 inter_y_modes[5], inter_y_modes[6], inter_y_modes[7], inter_y_modes[8], inter_y_modes[9]);
2226 fprintf(f, "UV:%8d, %8d, %8d, %8d\n", inter_uv_modes[0], inter_uv_modes[1], inter_uv_modes[2], inter_uv_modes[3]);
2231 for (i = 0; i < 15; i++)
2232 fprintf(f, "%8d, ", inter_b_modes[i]);
2237 fprintf(f, "P:%8d, %8d, %8d, %8d\n", count_mb_seg[0], count_mb_seg[1], count_mb_seg[2], count_mb_seg[3]);
2238 fprintf(f, "PB:%8d, %8d, %8d, %8d\n", inter_b_modes[LEFT4X4], inter_b_modes[ABOVE4X4], inter_b_modes[ZERO4X4], inter_b_modes[NEW4X4]);
2246 #ifdef ENTROPY_STATS
2249 FILE *fmode = fopen("modecontext.c", "w");
2251 fprintf(fmode, "\n#include \"entropymode.h\"\n\n");
2252 fprintf(fmode, "const unsigned int vp8_kf_default_bmode_counts ");
2253 fprintf(fmode, "[VP8_BINTRAMODES] [VP8_BINTRAMODES] [VP8_BINTRAMODES] =\n{\n");
2255 for (i = 0; i < 10; i++)
2258 fprintf(fmode, " { //Above Mode : %d\n", i);
2260 for (j = 0; j < 10; j++)
2263 fprintf(fmode, " {");
2265 for (k = 0; k < 10; k++)
2267 if (!intra_mode_stats[i][j][k])
2268 fprintf(fmode, " %5d, ", 1);
2270 fprintf(fmode, " %5d, ", intra_mode_stats[i][j][k]);
2273 fprintf(fmode, "}, // left_mode %d\n", j);
2277 fprintf(fmode, " },\n");
2281 fprintf(fmode, "};\n");
2287 #if defined(SECTIONBITS_OUTPUT)
2292 FILE *f = fopen("tokenbits.stt", "a");
2294 for (i = 0; i < 28; i++)
2295 fprintf(f, "%8d", (int)(Sectionbits[i] / 256));
2305 printf("\n_pick_loop_filter_level:%d\n", cpi->time_pick_lpf / 1000);
2306 printf("\n_frames recive_data encod_mb_row compress_frame Total\n");
2307 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);
2313 #if CONFIG_MULTITHREAD
2314 vp8cx_remove_encoder_threads(cpi);
2317 #if CONFIG_TEMPORAL_DENOISING
2318 vp8_denoiser_free(&cpi->denoiser);
2320 dealloc_compressor_data(cpi);
2321 vpx_free(cpi->mb.ss);
2323 vpx_free(cpi->cyclic_refresh_map);
2325 vp8_remove_common(&cpi->common);
2329 #ifdef OUTPUT_YUV_SRC
2349 static uint64_t calc_plane_error(unsigned char *orig, int orig_stride,
2350 unsigned char *recon, int recon_stride,
2351 unsigned int cols, unsigned int rows)
2353 unsigned int row, col;
2354 uint64_t total_sse = 0;
2357 for (row = 0; row + 16 <= rows; row += 16)
2359 for (col = 0; col + 16 <= cols; col += 16)
2363 vp8_mse16x16(orig + col, orig_stride,
2364 recon + col, recon_stride,
2369 /* Handle odd-sized width */
2372 unsigned int border_row, border_col;
2373 unsigned char *border_orig = orig;
2374 unsigned char *border_recon = recon;
2376 for (border_row = 0; border_row < 16; border_row++)
2378 for (border_col = col; border_col < cols; border_col++)
2380 diff = border_orig[border_col] - border_recon[border_col];
2381 total_sse += diff * diff;
2384 border_orig += orig_stride;
2385 border_recon += recon_stride;
2389 orig += orig_stride * 16;
2390 recon += recon_stride * 16;
2393 /* Handle odd-sized height */
2394 for (; row < rows; row++)
2396 for (col = 0; col < cols; col++)
2398 diff = orig[col] - recon[col];
2399 total_sse += diff * diff;
2402 orig += orig_stride;
2403 recon += recon_stride;
2406 vp8_clear_system_state();
2411 static void generate_psnr_packet(VP8_COMP *cpi)
2413 YV12_BUFFER_CONFIG *orig = cpi->Source;
2414 YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
2415 struct vpx_codec_cx_pkt pkt;
2418 unsigned int width = cpi->common.Width;
2419 unsigned int height = cpi->common.Height;
2421 pkt.kind = VPX_CODEC_PSNR_PKT;
2422 sse = calc_plane_error(orig->y_buffer, orig->y_stride,
2423 recon->y_buffer, recon->y_stride,
2425 pkt.data.psnr.sse[0] = sse;
2426 pkt.data.psnr.sse[1] = sse;
2427 pkt.data.psnr.samples[0] = width * height;
2428 pkt.data.psnr.samples[1] = width * height;
2430 width = (width + 1) / 2;
2431 height = (height + 1) / 2;
2433 sse = calc_plane_error(orig->u_buffer, orig->uv_stride,
2434 recon->u_buffer, recon->uv_stride,
2436 pkt.data.psnr.sse[0] += sse;
2437 pkt.data.psnr.sse[2] = sse;
2438 pkt.data.psnr.samples[0] += width * height;
2439 pkt.data.psnr.samples[2] = width * height;
2441 sse = calc_plane_error(orig->v_buffer, orig->uv_stride,
2442 recon->v_buffer, recon->uv_stride,
2444 pkt.data.psnr.sse[0] += sse;
2445 pkt.data.psnr.sse[3] = sse;
2446 pkt.data.psnr.samples[0] += width * height;
2447 pkt.data.psnr.samples[3] = width * height;
2449 for (i = 0; i < 4; i++)
2450 pkt.data.psnr.psnr[i] = vp8_mse2psnr(pkt.data.psnr.samples[i], 255.0,
2451 pkt.data.psnr.sse[i]);
2453 vpx_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
2457 int vp8_use_as_reference(VP8_COMP *cpi, int ref_frame_flags)
2459 if (ref_frame_flags > 7)
2462 cpi->ref_frame_flags = ref_frame_flags;
2465 int vp8_update_reference(VP8_COMP *cpi, int ref_frame_flags)
2467 if (ref_frame_flags > 7)
2470 cpi->common.refresh_golden_frame = 0;
2471 cpi->common.refresh_alt_ref_frame = 0;
2472 cpi->common.refresh_last_frame = 0;
2474 if (ref_frame_flags & VP8_LAST_FLAG)
2475 cpi->common.refresh_last_frame = 1;
2477 if (ref_frame_flags & VP8_GOLD_FLAG)
2478 cpi->common.refresh_golden_frame = 1;
2480 if (ref_frame_flags & VP8_ALT_FLAG)
2481 cpi->common.refresh_alt_ref_frame = 1;
2486 int vp8_get_reference(VP8_COMP *cpi, VP8_REFFRAME ref_frame_flag, YV12_BUFFER_CONFIG *sd)
2488 VP8_COMMON *cm = &cpi->common;
2491 if (ref_frame_flag == VP8_LAST_FLAG)
2492 ref_fb_idx = cm->lst_fb_idx;
2493 else if (ref_frame_flag == VP8_GOLD_FLAG)
2494 ref_fb_idx = cm->gld_fb_idx;
2495 else if (ref_frame_flag == VP8_ALT_FLAG)
2496 ref_fb_idx = cm->alt_fb_idx;
2500 vp8_yv12_copy_frame(&cm->yv12_fb[ref_fb_idx], sd);
2504 int vp8_set_reference(VP8_COMP *cpi, VP8_REFFRAME ref_frame_flag, YV12_BUFFER_CONFIG *sd)
2506 VP8_COMMON *cm = &cpi->common;
2510 if (ref_frame_flag == VP8_LAST_FLAG)
2511 ref_fb_idx = cm->lst_fb_idx;
2512 else if (ref_frame_flag == VP8_GOLD_FLAG)
2513 ref_fb_idx = cm->gld_fb_idx;
2514 else if (ref_frame_flag == VP8_ALT_FLAG)
2515 ref_fb_idx = cm->alt_fb_idx;
2519 vp8_yv12_copy_frame(sd, &cm->yv12_fb[ref_fb_idx]);
2523 int vp8_update_entropy(VP8_COMP *cpi, int update)
2525 VP8_COMMON *cm = &cpi->common;
2526 cm->refresh_entropy_probs = update;
2533 void vp8_write_yuv_frame(const char *name, YV12_BUFFER_CONFIG *s)
2535 FILE *yuv_file = fopen(name, "ab");
2536 unsigned char *src = s->y_buffer;
2537 int h = s->y_height;
2541 fwrite(src, s->y_width, 1, yuv_file);
2551 fwrite(src, s->uv_width, 1, yuv_file);
2552 src += s->uv_stride;
2561 fwrite(src, s->uv_width, 1, yuv_file);
2562 src += s->uv_stride;
2571 static void scale_and_extend_source(YV12_BUFFER_CONFIG *sd, VP8_COMP *cpi)
2573 VP8_COMMON *cm = &cpi->common;
2575 // are we resizing the image
2576 if (cm->horiz_scale != 0 || cm->vert_scale != 0)
2578 #if CONFIG_SPATIAL_RESAMPLING
2579 int UNINITIALIZED_IS_SAFE(hr), UNINITIALIZED_IS_SAFE(hs);
2580 int UNINITIALIZED_IS_SAFE(vr), UNINITIALIZED_IS_SAFE(vs);
2583 if (cm->vert_scale == 3)
2588 Scale2Ratio(cm->horiz_scale, &hr, &hs);
2589 Scale2Ratio(cm->vert_scale, &vr, &vs);
2591 vp8_scale_frame(sd, &cpi->scaled_source, cm->temp_scale_frame.y_buffer,
2592 tmp_height, hs, hr, vs, vr, 0);
2594 vp8_yv12_extend_frame_borders(&cpi->scaled_source);
2595 cpi->Source = &cpi->scaled_source;
2603 static void resize_key_frame(VP8_COMP *cpi)
2605 #if CONFIG_SPATIAL_RESAMPLING
2606 VP8_COMMON *cm = &cpi->common;
2608 // Do we need to apply resampling for one pass cbr.
2609 // In one pass this is more limited than in two pass cbr
2610 // The test and any change is only made one per key frame sequence
2611 if (cpi->oxcf.allow_spatial_resampling && (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER))
2613 int UNINITIALIZED_IS_SAFE(hr), UNINITIALIZED_IS_SAFE(hs);
2614 int UNINITIALIZED_IS_SAFE(vr), UNINITIALIZED_IS_SAFE(vs);
2615 int new_width, new_height;
2617 // If we are below the resample DOWN watermark then scale down a notch.
2618 if (cpi->buffer_level < (cpi->oxcf.resample_down_water_mark * cpi->oxcf.optimal_buffer_level / 100))
2620 cm->horiz_scale = (cm->horiz_scale < ONETWO) ? cm->horiz_scale + 1 : ONETWO;
2621 cm->vert_scale = (cm->vert_scale < ONETWO) ? cm->vert_scale + 1 : ONETWO;
2623 // Should we now start scaling back up
2624 else if (cpi->buffer_level > (cpi->oxcf.resample_up_water_mark * cpi->oxcf.optimal_buffer_level / 100))
2626 cm->horiz_scale = (cm->horiz_scale > NORMAL) ? cm->horiz_scale - 1 : NORMAL;
2627 cm->vert_scale = (cm->vert_scale > NORMAL) ? cm->vert_scale - 1 : NORMAL;
2630 // Get the new hieght and width
2631 Scale2Ratio(cm->horiz_scale, &hr, &hs);
2632 Scale2Ratio(cm->vert_scale, &vr, &vs);
2633 new_width = ((hs - 1) + (cpi->oxcf.Width * hr)) / hs;
2634 new_height = ((vs - 1) + (cpi->oxcf.Height * vr)) / vs;
2636 // If the image size has changed we need to reallocate the buffers
2637 // and resample the source image
2638 if ((cm->Width != new_width) || (cm->Height != new_height))
2640 cm->Width = new_width;
2641 cm->Height = new_height;
2642 vp8_alloc_compressor_data(cpi);
2643 scale_and_extend_source(cpi->un_scaled_source, cpi);
2651 static void update_alt_ref_frame_stats(VP8_COMP *cpi)
2653 VP8_COMMON *cm = &cpi->common;
2655 // Select an interval before next GF or altref
2656 if (!cpi->auto_gold)
2657 cpi->frames_till_gf_update_due = cpi->goldfreq;
2659 if ((cpi->pass != 2) && cpi->frames_till_gf_update_due)
2661 cpi->current_gf_interval = cpi->frames_till_gf_update_due;
2663 // Set the bits per frame that we should try and recover in subsequent inter frames
2664 // to account for the extra GF spend... note that his does not apply for GF updates
2665 // that occur coincident with a key frame as the extra cost of key frames is dealt
2668 cpi->gf_overspend_bits += cpi->projected_frame_size;
2669 cpi->non_gf_bitrate_adjustment = cpi->gf_overspend_bits / cpi->frames_till_gf_update_due;
2672 // Update data structure that monitors level of reference to last GF
2673 vpx_memset(cpi->gf_active_flags, 1, (cm->mb_rows * cm->mb_cols));
2674 cpi->gf_active_count = cm->mb_rows * cm->mb_cols;
2676 // this frame refreshes means next frames don't unless specified by user
2677 cpi->common.frames_since_golden = 0;
2679 // Clear the alternate reference update pending flag.
2680 cpi->source_alt_ref_pending = 0;
2682 // Set the alternate refernce frame active flag
2683 cpi->source_alt_ref_active = 1;
2687 static void update_golden_frame_stats(VP8_COMP *cpi)
2689 VP8_COMMON *cm = &cpi->common;
2691 // Update the Golden frame usage counts.
2692 if (cm->refresh_golden_frame)
2694 // Select an interval before next GF
2695 if (!cpi->auto_gold)
2696 cpi->frames_till_gf_update_due = cpi->goldfreq;
2698 if ((cpi->pass != 2) && (cpi->frames_till_gf_update_due > 0))
2700 cpi->current_gf_interval = cpi->frames_till_gf_update_due;
2702 // Set the bits per frame that we should try and recover in subsequent inter frames
2703 // to account for the extra GF spend... note that his does not apply for GF updates
2704 // that occur coincident with a key frame as the extra cost of key frames is dealt
2706 if ((cm->frame_type != KEY_FRAME) && !cpi->source_alt_ref_active)
2708 // Calcluate GF bits to be recovered
2709 // Projected size - av frame bits available for inter frames for clip as a whole
2710 cpi->gf_overspend_bits += (cpi->projected_frame_size - cpi->inter_frame_target);
2713 cpi->non_gf_bitrate_adjustment = cpi->gf_overspend_bits / cpi->frames_till_gf_update_due;
2717 // Update data structure that monitors level of reference to last GF
2718 vpx_memset(cpi->gf_active_flags, 1, (cm->mb_rows * cm->mb_cols));
2719 cpi->gf_active_count = cm->mb_rows * cm->mb_cols;
2721 // this frame refreshes means next frames don't unless specified by user
2722 cm->refresh_golden_frame = 0;
2723 cpi->common.frames_since_golden = 0;
2725 //if ( cm->frame_type == KEY_FRAME )
2727 cpi->recent_ref_frame_usage[INTRA_FRAME] = 1;
2728 cpi->recent_ref_frame_usage[LAST_FRAME] = 1;
2729 cpi->recent_ref_frame_usage[GOLDEN_FRAME] = 1;
2730 cpi->recent_ref_frame_usage[ALTREF_FRAME] = 1;
2734 // // Carry a potrtion of count over to begining of next gf sequence
2735 // cpi->recent_ref_frame_usage[INTRA_FRAME] >>= 5;
2736 // cpi->recent_ref_frame_usage[LAST_FRAME] >>= 5;
2737 // cpi->recent_ref_frame_usage[GOLDEN_FRAME] >>= 5;
2738 // cpi->recent_ref_frame_usage[ALTREF_FRAME] >>= 5;
2741 // ******** Fixed Q test code only ************
2742 // If we are going to use the ALT reference for the next group of frames set a flag to say so.
2743 if (cpi->oxcf.fixed_q >= 0 &&
2744 cpi->oxcf.play_alternate && !cpi->common.refresh_alt_ref_frame)
2746 cpi->source_alt_ref_pending = 1;
2747 cpi->frames_till_gf_update_due = cpi->baseline_gf_interval;
2750 if (!cpi->source_alt_ref_pending)
2751 cpi->source_alt_ref_active = 0;
2753 // Decrement count down till next gf
2754 if (cpi->frames_till_gf_update_due > 0)
2755 cpi->frames_till_gf_update_due--;
2758 else if (!cpi->common.refresh_alt_ref_frame)
2760 // Decrement count down till next gf
2761 if (cpi->frames_till_gf_update_due > 0)
2762 cpi->frames_till_gf_update_due--;
2764 if (cpi->common.frames_till_alt_ref_frame)
2765 cpi->common.frames_till_alt_ref_frame --;
2767 cpi->common.frames_since_golden ++;
2769 if (cpi->common.frames_since_golden > 1)
2771 cpi->recent_ref_frame_usage[INTRA_FRAME] += cpi->count_mb_ref_frame_usage[INTRA_FRAME];
2772 cpi->recent_ref_frame_usage[LAST_FRAME] += cpi->count_mb_ref_frame_usage[LAST_FRAME];
2773 cpi->recent_ref_frame_usage[GOLDEN_FRAME] += cpi->count_mb_ref_frame_usage[GOLDEN_FRAME];
2774 cpi->recent_ref_frame_usage[ALTREF_FRAME] += cpi->count_mb_ref_frame_usage[ALTREF_FRAME];
2779 // This function updates the reference frame probability estimates that
2780 // will be used during mode selection
2781 static void update_rd_ref_frame_probs(VP8_COMP *cpi)
2783 VP8_COMMON *cm = &cpi->common;
2785 const int *const rfct = cpi->count_mb_ref_frame_usage;
2786 const int rf_intra = rfct[INTRA_FRAME];
2787 const int rf_inter = rfct[LAST_FRAME] + rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME];
2789 if (cm->frame_type == KEY_FRAME)
2791 cpi->prob_intra_coded = 255;
2792 cpi->prob_last_coded = 128;
2793 cpi->prob_gf_coded = 128;
2795 else if (!(rf_intra + rf_inter))
2797 cpi->prob_intra_coded = 63;
2798 cpi->prob_last_coded = 128;
2799 cpi->prob_gf_coded = 128;
2802 // update reference frame costs since we can do better than what we got last frame.
2803 if (cpi->oxcf.number_of_layers == 1)
2805 if (cpi->common.refresh_alt_ref_frame)
2807 cpi->prob_intra_coded += 40;
2808 cpi->prob_last_coded = 200;
2809 cpi->prob_gf_coded = 1;
2811 else if (cpi->common.frames_since_golden == 0)
2813 cpi->prob_last_coded = 214;
2815 else if (cpi->common.frames_since_golden == 1)
2817 cpi->prob_last_coded = 192;
2818 cpi->prob_gf_coded = 220;
2820 else if (cpi->source_alt_ref_active)
2822 cpi->prob_gf_coded -= 20;
2824 if (cpi->prob_gf_coded < 10)
2825 cpi->prob_gf_coded = 10;
2827 if (!cpi->source_alt_ref_active)
2828 cpi->prob_gf_coded = 255;
2833 // 1 = key, 0 = inter
2834 static int decide_key_frame(VP8_COMP *cpi)
2836 VP8_COMMON *cm = &cpi->common;
2838 int code_key_frame = 0;
2842 if (cpi->Speed > 11)
2845 // Clear down mmx registers
2846 vp8_clear_system_state(); //__asm emms;
2848 if ((cpi->compressor_speed == 2) && (cpi->Speed >= 5) && (cpi->sf.RD == 0))
2850 double change = 1.0 * abs((int)(cpi->intra_error - cpi->last_intra_error)) / (1 + cpi->last_intra_error);
2851 double change2 = 1.0 * abs((int)(cpi->prediction_error - cpi->last_prediction_error)) / (1 + cpi->last_prediction_error);
2852 double minerror = cm->MBs * 256;
2856 if (10 * cpi->intra_error / (1 + cpi->prediction_error) < 15
2857 && cpi->prediction_error > minerror
2858 && (change > .25 || change2 > .25))
2860 FILE *f = fopen("intra_inter.stt", "a");
2862 if (cpi->prediction_error <= 0)
2863 cpi->prediction_error = 1;
2865 fprintf(f, "%d %d %d %d %14.4f\n",
2866 cm->current_video_frame,
2867 (int) cpi->prediction_error,
2868 (int) cpi->intra_error,
2869 (int)((10 * cpi->intra_error) / cpi->prediction_error),
2877 cpi->last_intra_error = cpi->intra_error;
2878 cpi->last_prediction_error = cpi->prediction_error;
2880 if (10 * cpi->intra_error / (1 + cpi->prediction_error) < 15
2881 && cpi->prediction_error > minerror
2882 && (change > .25 || change2 > .25))
2884 /*(change > 1.4 || change < .75)&& cpi->this_frame_percent_intra > cpi->last_frame_percent_intra + 3*/
2892 // If the following are true we might as well code a key frame
2893 if (((cpi->this_frame_percent_intra == 100) &&
2894 (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra + 2))) ||
2895 ((cpi->this_frame_percent_intra > 95) &&
2896 (cpi->this_frame_percent_intra >= (cpi->last_frame_percent_intra + 5))))
2900 // in addition if the following are true and this is not a golden frame then code a key frame
2901 // Note that on golden frames there often seems to be a pop in intra useage anyway hence this
2902 // restriction is designed to prevent spurious key frames. The Intra pop needs to be investigated.
2903 else if (((cpi->this_frame_percent_intra > 60) &&
2904 (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra * 2))) ||
2905 ((cpi->this_frame_percent_intra > 75) &&
2906 (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra * 3 / 2))) ||
2907 ((cpi->this_frame_percent_intra > 90) &&
2908 (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra + 10))))
2910 if (!cm->refresh_golden_frame)
2914 return code_key_frame;
2918 #if !(CONFIG_REALTIME_ONLY)
2919 static void Pass1Encode(VP8_COMP *cpi, unsigned long *size, unsigned char *dest, unsigned int *frame_flags)
2924 vp8_set_quantizer(cpi, 26);
2926 vp8_first_pass(cpi);
2931 void write_cx_frame_to_file(YV12_BUFFER_CONFIG *frame, int this_frame)
2939 sprintf(filename, "cx\\y%04d.raw", this_frame);
2940 yframe = fopen(filename, "wb");
2942 for (i = 0; i < frame->y_height; i++)
2943 fwrite(frame->y_buffer + i * frame->y_stride, frame->y_width, 1, yframe);
2946 sprintf(filename, "cx\\u%04d.raw", this_frame);
2947 yframe = fopen(filename, "wb");
2949 for (i = 0; i < frame->uv_height; i++)
2950 fwrite(frame->u_buffer + i * frame->uv_stride, frame->uv_width, 1, yframe);
2953 sprintf(filename, "cx\\v%04d.raw", this_frame);
2954 yframe = fopen(filename, "wb");
2956 for (i = 0; i < frame->uv_height; i++)
2957 fwrite(frame->v_buffer + i * frame->uv_stride, frame->uv_width, 1, yframe);
2962 // return of 0 means drop frame
2964 // Function to test for conditions that indeicate we should loop
2965 // back and recode a frame.
2966 static int recode_loop_test( VP8_COMP *cpi,
2967 int high_limit, int low_limit,
2968 int q, int maxq, int minq )
2970 int force_recode = 0;
2971 VP8_COMMON *cm = &cpi->common;
2973 // Is frame recode allowed at all
2974 // Yes if either recode mode 1 is selected or mode two is selcted
2975 // and the frame is a key frame. golden frame or alt_ref_frame
2976 if ( (cpi->sf.recode_loop == 1) ||
2977 ( (cpi->sf.recode_loop == 2) &&
2978 ( (cm->frame_type == KEY_FRAME) ||
2979 cm->refresh_golden_frame ||
2980 cm->refresh_alt_ref_frame ) ) )
2982 // General over and under shoot tests
2983 if ( ((cpi->projected_frame_size > high_limit) && (q < maxq)) ||
2984 ((cpi->projected_frame_size < low_limit) && (q > minq)) )
2988 // Special Constrained quality tests
2989 else if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY)
2991 // Undershoot and below auto cq level
2992 if ( (q > cpi->cq_target_quality) &&
2993 (cpi->projected_frame_size <
2994 ((cpi->this_frame_target * 7) >> 3)))
2998 // Severe undershoot and between auto and user cq level
2999 else if ( (q > cpi->oxcf.cq_level) &&
3000 (cpi->projected_frame_size < cpi->min_frame_bandwidth) &&
3001 (cpi->active_best_quality > cpi->oxcf.cq_level))
3004 cpi->active_best_quality = cpi->oxcf.cq_level;
3009 return force_recode;
3012 void update_reference_frames(VP8_COMMON *cm)
3014 YV12_BUFFER_CONFIG *yv12_fb = cm->yv12_fb;
3016 // At this point the new frame has been encoded.
3017 // If any buffer copy / swapping is signaled it should be done here.
3019 if (cm->frame_type == KEY_FRAME)
3021 yv12_fb[cm->new_fb_idx].flags |= VP8_GOLD_FLAG | VP8_ALT_FLAG ;
3023 yv12_fb[cm->gld_fb_idx].flags &= ~VP8_GOLD_FLAG;
3024 yv12_fb[cm->alt_fb_idx].flags &= ~VP8_ALT_FLAG;
3026 cm->alt_fb_idx = cm->gld_fb_idx = cm->new_fb_idx;
3028 else /* For non key frames */
3030 if (cm->refresh_alt_ref_frame)
3032 assert(!cm->copy_buffer_to_arf);
3034 cm->yv12_fb[cm->new_fb_idx].flags |= VP8_ALT_FLAG;
3035 cm->yv12_fb[cm->alt_fb_idx].flags &= ~VP8_ALT_FLAG;
3036 cm->alt_fb_idx = cm->new_fb_idx;
3038 else if (cm->copy_buffer_to_arf)
3040 assert(!(cm->copy_buffer_to_arf & ~0x3));
3042 if (cm->copy_buffer_to_arf == 1)
3044 if(cm->alt_fb_idx != cm->lst_fb_idx)
3046 yv12_fb[cm->lst_fb_idx].flags |= VP8_ALT_FLAG;
3047 yv12_fb[cm->alt_fb_idx].flags &= ~VP8_ALT_FLAG;
3048 cm->alt_fb_idx = cm->lst_fb_idx;
3051 else /* if (cm->copy_buffer_to_arf == 2) */
3053 if(cm->alt_fb_idx != cm->gld_fb_idx)
3055 yv12_fb[cm->gld_fb_idx].flags |= VP8_ALT_FLAG;
3056 yv12_fb[cm->alt_fb_idx].flags &= ~VP8_ALT_FLAG;
3057 cm->alt_fb_idx = cm->gld_fb_idx;
3062 if (cm->refresh_golden_frame)
3064 assert(!cm->copy_buffer_to_gf);
3066 cm->yv12_fb[cm->new_fb_idx].flags |= VP8_GOLD_FLAG;
3067 cm->yv12_fb[cm->gld_fb_idx].flags &= ~VP8_GOLD_FLAG;
3068 cm->gld_fb_idx = cm->new_fb_idx;
3070 else if (cm->copy_buffer_to_gf)
3072 assert(!(cm->copy_buffer_to_arf & ~0x3));
3074 if (cm->copy_buffer_to_gf == 1)
3076 if(cm->gld_fb_idx != cm->lst_fb_idx)
3078 yv12_fb[cm->lst_fb_idx].flags |= VP8_GOLD_FLAG;
3079 yv12_fb[cm->gld_fb_idx].flags &= ~VP8_GOLD_FLAG;
3080 cm->gld_fb_idx = cm->lst_fb_idx;
3083 else /* if (cm->copy_buffer_to_gf == 2) */
3085 if(cm->alt_fb_idx != cm->gld_fb_idx)
3087 yv12_fb[cm->alt_fb_idx].flags |= VP8_GOLD_FLAG;
3088 yv12_fb[cm->gld_fb_idx].flags &= ~VP8_GOLD_FLAG;
3089 cm->gld_fb_idx = cm->alt_fb_idx;
3095 if (cm->refresh_last_frame)
3097 cm->yv12_fb[cm->new_fb_idx].flags |= VP8_LAST_FLAG;
3098 cm->yv12_fb[cm->lst_fb_idx].flags &= ~VP8_LAST_FLAG;
3099 cm->lst_fb_idx = cm->new_fb_idx;
3103 void vp8_loopfilter_frame(VP8_COMP *cpi, VP8_COMMON *cm)
3107 cm->filter_level = 0;
3111 struct vpx_usec_timer timer;
3113 vp8_clear_system_state();
3115 vpx_usec_timer_start(&timer);
3116 if (cpi->sf.auto_filter == 0)
3117 vp8cx_pick_filter_level_fast(cpi->Source, cpi);
3120 vp8cx_pick_filter_level(cpi->Source, cpi);
3122 vpx_usec_timer_mark(&timer);
3123 cpi->time_pick_lpf += vpx_usec_timer_elapsed(&timer);
3126 #if CONFIG_MULTITHREAD
3127 if (cpi->b_multi_threaded)
3128 sem_post(&cpi->h_event_end_lpf); /* signal that we have set filter_level */
3131 if (cm->filter_level > 0)
3133 vp8cx_set_alt_lf_level(cpi, cm->filter_level);
3134 vp8_loop_filter_frame(cm, &cpi->mb.e_mbd);
3137 vp8_yv12_extend_frame_borders(cm->frame_to_show);
3138 #if CONFIG_TEMPORAL_DENOISING
3139 if (cpi->oxcf.noise_sensitivity)
3141 vp8_yv12_extend_frame_borders(&cpi->denoiser.yv12_running_avg);
3146 static void encode_frame_to_data_rate
3149 unsigned long *size,
3150 unsigned char *dest,
3151 unsigned char* dest_end,
3152 unsigned int *frame_flags
3156 int frame_over_shoot_limit;
3157 int frame_under_shoot_limit;
3167 int zbin_oq_low = 0;
3170 VP8_COMMON *cm = &cpi->common;
3171 int active_worst_qchanged = 0;
3173 int overshoot_seen = 0;
3174 int undershoot_seen = 0;
3175 int drop_mark = cpi->oxcf.drop_frames_water_mark * cpi->oxcf.optimal_buffer_level / 100;
3176 int drop_mark75 = drop_mark * 2 / 3;
3177 int drop_mark50 = drop_mark / 4;
3178 int drop_mark25 = drop_mark / 8;
3181 // Clear down mmx registers to allow floating point in what follows
3182 vp8_clear_system_state();
3184 // Test code for segmentation of gf/arf (0,0)
3185 //segmentation_test_function( cpi);
3187 if(cpi->force_next_frame_intra)
3189 cm->frame_type = KEY_FRAME; /* delayed intra frame */
3190 cpi->force_next_frame_intra = 0;
3193 // For an alt ref frame in 2 pass we skip the call to the second pass function that sets the target bandwidth
3194 #if !(CONFIG_REALTIME_ONLY)
3198 if (cpi->common.refresh_alt_ref_frame)
3200 cpi->per_frame_bandwidth = cpi->twopass.gf_bits; // Per frame bit target for the alt ref frame
3201 cpi->target_bandwidth = cpi->twopass.gf_bits * cpi->output_frame_rate; // per second target bitrate
3206 cpi->per_frame_bandwidth = (int)(cpi->target_bandwidth / cpi->output_frame_rate);
3208 // Default turn off buffer to buffer copying
3209 cm->copy_buffer_to_gf = 0;
3210 cm->copy_buffer_to_arf = 0;
3212 // Clear zbin over-quant value and mode boost values.
3213 cpi->zbin_over_quant = 0;
3214 cpi->zbin_mode_boost = 0;
3216 // Enable or disable mode based tweaking of the zbin
3217 // For 2 Pass Only used where GF/ARF prediction quality
3218 // is above a threshold
3219 cpi->zbin_mode_boost_enabled = 1;
3222 if ( cpi->gfu_boost <= 400 )
3224 cpi->zbin_mode_boost_enabled = 0;
3228 // Current default encoder behaviour for the altref sign bias
3229 if (cpi->source_alt_ref_active)
3230 cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = 1;
3232 cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = 0;
3234 // Check to see if a key frame is signalled
3235 // For two pass with auto key frame enabled cm->frame_type may already be set, but not for one pass.
3236 if ((cm->current_video_frame == 0) ||
3237 (cm->frame_flags & FRAMEFLAGS_KEY) ||
3238 (cpi->oxcf.auto_key && (cpi->frames_since_key % cpi->key_frame_frequency == 0)))
3240 // Key frame from VFW/auto-keyframe/first frame
3241 cm->frame_type = KEY_FRAME;
3244 // Set default state for segment and mode based loop filter update flags
3245 cpi->mb.e_mbd.update_mb_segmentation_map = 0;
3246 cpi->mb.e_mbd.update_mb_segmentation_data = 0;
3247 cpi->mb.e_mbd.mode_ref_lf_delta_update = 0;
3249 // Set various flags etc to special state if it is a key frame
3250 if (cm->frame_type == KEY_FRAME)
3254 // Reset the loop filter deltas and segmentation map
3255 setup_features(cpi);
3257 // If segmentation is enabled force a map update for key frames
3258 if (cpi->mb.e_mbd.segmentation_enabled)
3260 cpi->mb.e_mbd.update_mb_segmentation_map = 1;
3261 cpi->mb.e_mbd.update_mb_segmentation_data = 1;
3264 // The alternate reference frame cannot be active for a key frame
3265 cpi->source_alt_ref_active = 0;
3267 // Reset the RD threshold multipliers to default of * 1 (128)
3268 for (i = 0; i < MAX_MODES; i++)
3270 cpi->rd_thresh_mult[i] = 128;
3274 // Test code for segmentation
3275 //if ( (cm->frame_type == KEY_FRAME) || ((cm->current_video_frame % 2) == 0))
3276 //if ( (cm->current_video_frame % 2) == 0 )
3277 // enable_segmentation(cpi);
3279 // disable_segmentation(cpi);
3282 // Experimental code for lagged compress and one pass
3283 // Initialise one_pass GF frames stats
3284 // Update stats used for GF selection
3285 //if ( cpi->pass == 0 )
3287 cpi->one_pass_frame_index = cm->current_video_frame % MAX_LAG_BUFFERS;
3289 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frames_so_far = 0;
3290 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_intra_error = 0.0;
3291 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_coded_error = 0.0;
3292 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_pcnt_inter = 0.0;
3293 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_pcnt_motion = 0.0;
3294 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvr = 0.0;
3295 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvr_abs = 0.0;
3296 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvc = 0.0;
3297 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvc_abs = 0.0;
3301 update_rd_ref_frame_probs(cpi);
3303 if (cpi->drop_frames_allowed)
3305 // The reset to decimation 0 is only done here for one pass.
3306 // Once it is set two pass leaves decimation on till the next kf.
3307 if ((cpi->buffer_level > drop_mark) && (cpi->decimation_factor > 0))
3308 cpi->decimation_factor --;
3310 if (cpi->buffer_level > drop_mark75 && cpi->decimation_factor > 0)
3311 cpi->decimation_factor = 1;
3313 else if (cpi->buffer_level < drop_mark25 && (cpi->decimation_factor == 2 || cpi->decimation_factor == 3))
3315 cpi->decimation_factor = 3;
3317 else if (cpi->buffer_level < drop_mark50 && (cpi->decimation_factor == 1 || cpi->decimation_factor == 2))
3319 cpi->decimation_factor = 2;
3321 else if (cpi->buffer_level < drop_mark75 && (cpi->decimation_factor == 0 || cpi->decimation_factor == 1))
3323 cpi->decimation_factor = 1;
3326 //vpx_log("Encoder: Decimation Factor: %d \n",cpi->decimation_factor);
3329 // The following decimates the frame rate according to a regular pattern (i.e. to 1/2 or 2/3 frame rate)
3330 // This can be used to help prevent buffer under-run in CBR mode. Alternatively it might be desirable in
3331 // some situations to drop frame rate but throw more bits at each frame.
3333 // Note that dropping a key frame can be problematic if spatial resampling is also active
3334 if (cpi->decimation_factor > 0)
3336 switch (cpi->decimation_factor)
3339 cpi->per_frame_bandwidth = cpi->per_frame_bandwidth * 3 / 2;
3342 cpi->per_frame_bandwidth = cpi->per_frame_bandwidth * 5 / 4;
3345 cpi->per_frame_bandwidth = cpi->per_frame_bandwidth * 5 / 4;
3349 // Note that we should not throw out a key frame (especially when spatial resampling is enabled).
3350 if ((cm->frame_type == KEY_FRAME)) // && cpi->oxcf.allow_spatial_resampling )
3352 cpi->decimation_count = cpi->decimation_factor;
3354 else if (cpi->decimation_count > 0)
3356 cpi->decimation_count --;
3358 cpi->bits_off_target += cpi->av_per_frame_bandwidth;
3359 if (cpi->bits_off_target > cpi->oxcf.maximum_buffer_size)
3360 cpi->bits_off_target = cpi->oxcf.maximum_buffer_size;
3362 cm->current_video_frame++;
3363 cpi->frames_since_key++;
3365 #if CONFIG_INTERNAL_STATS
3369 cpi->buffer_level = cpi->bits_off_target;
3371 if (cpi->oxcf.number_of_layers > 1)
3375 // Propagate bits saved by dropping the frame to higher layers
3376 for (i=cpi->current_layer+1; i<cpi->oxcf.number_of_layers; i++)
3378 LAYER_CONTEXT *lc = &cpi->layer_context[i];
3379 lc->bits_off_target += cpi->av_per_frame_bandwidth;
3380 if (lc->bits_off_target > lc->maximum_buffer_size)
3381 lc->bits_off_target = lc->maximum_buffer_size;
3382 lc->buffer_level = lc->bits_off_target;
3389 cpi->decimation_count = cpi->decimation_factor;
3392 // Decide how big to make the frame
3393 if (!vp8_pick_frame_size(cpi))
3395 cm->current_video_frame++;
3396 cpi->frames_since_key++;
3400 // Reduce active_worst_allowed_q for CBR if our buffer is getting too full.
3401 // This has a knock on effect on active best quality as well.
3402 // For CBR if the buffer reaches its maximum level then we can no longer
3403 // save up bits for later frames so we might as well use them up
3404 // on the current frame.
3405 if ((cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) &&
3406 (cpi->buffer_level >= cpi->oxcf.optimal_buffer_level) && cpi->buffered_mode)
3408 int Adjustment = cpi->active_worst_quality / 4; // Max adjustment is 1/4
3414 if (cpi->buffer_level < cpi->oxcf.maximum_buffer_size)
3416 buff_lvl_step = (cpi->oxcf.maximum_buffer_size - cpi->oxcf.optimal_buffer_level) / Adjustment;
3419 Adjustment = (cpi->buffer_level - cpi->oxcf.optimal_buffer_level) / buff_lvl_step;
3424 cpi->active_worst_quality -= Adjustment;
3426 if(cpi->active_worst_quality < cpi->active_best_quality)
3427 cpi->active_worst_quality = cpi->active_best_quality;
3431 // Set an active best quality and if necessary active worst quality
3432 // There is some odd behavior for one pass here that needs attention.
3433 if ( (cpi->pass == 2) || (cpi->ni_frames > 150))
3435 vp8_clear_system_state();
3437 Q = cpi->active_worst_quality;
3439 if ( cm->frame_type == KEY_FRAME )
3441 if ( cpi->pass == 2 )
3443 if (cpi->gfu_boost > 600)
3444 cpi->active_best_quality = kf_low_motion_minq[Q];
3446 cpi->active_best_quality = kf_high_motion_minq[Q];
3448 // Special case for key frames forced because we have reached
3449 // the maximum key frame interval. Here force the Q to a range
3450 // based on the ambient Q to reduce the risk of popping
3451 if ( cpi->this_key_frame_forced )
3453 if ( cpi->active_best_quality > cpi->avg_frame_qindex * 7/8)
3454 cpi->active_best_quality = cpi->avg_frame_qindex * 7/8;
3455 else if ( cpi->active_best_quality < cpi->avg_frame_qindex >> 2 )
3456 cpi->active_best_quality = cpi->avg_frame_qindex >> 2;
3459 // One pass more conservative
3461 cpi->active_best_quality = kf_high_motion_minq[Q];
3464 else if (cpi->oxcf.number_of_layers==1 &&
3465 (cm->refresh_golden_frame || cpi->common.refresh_alt_ref_frame))
3467 // Use the lower of cpi->active_worst_quality and recent
3468 // average Q as basis for GF/ARF Q limit unless last frame was
3470 if ( (cpi->frames_since_key > 1) &&
3471 (cpi->avg_frame_qindex < cpi->active_worst_quality) )
3473 Q = cpi->avg_frame_qindex;
3476 // For constrained quality dont allow Q less than the cq level
3477 if ( (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) &&
3478 (Q < cpi->cq_target_quality) )
3480 Q = cpi->cq_target_quality;
3483 if ( cpi->pass == 2 )
3485 if ( cpi->gfu_boost > 1000 )
3486 cpi->active_best_quality = gf_low_motion_minq[Q];
3487 else if ( cpi->gfu_boost < 400 )
3488 cpi->active_best_quality = gf_high_motion_minq[Q];
3490 cpi->active_best_quality = gf_mid_motion_minq[Q];
3492 // Constrained quality use slightly lower active best.
3493 if ( cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY )
3495 cpi->active_best_quality =
3496 cpi->active_best_quality * 15/16;
3499 // One pass more conservative
3501 cpi->active_best_quality = gf_high_motion_minq[Q];
3505 cpi->active_best_quality = inter_minq[Q];
3507 // For the constant/constrained quality mode we dont want
3508 // q to fall below the cq level.
3509 if ((cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) &&
3510 (cpi->active_best_quality < cpi->cq_target_quality) )
3512 // If we are strongly undershooting the target rate in the last
3513 // frames then use the user passed in cq value not the auto
3515 if ( cpi->rolling_actual_bits < cpi->min_frame_bandwidth )
3516 cpi->active_best_quality = cpi->oxcf.cq_level;
3518 cpi->active_best_quality = cpi->cq_target_quality;
3522 // If CBR and the buffer is as full then it is reasonable to allow
3523 // higher quality on the frames to prevent bits just going to waste.
3524 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
3526 // Note that the use of >= here elliminates the risk of a devide
3527 // by 0 error in the else if clause
3528 if (cpi->buffer_level >= cpi->oxcf.maximum_buffer_size)
3529 cpi->active_best_quality = cpi->best_quality;
3531 else if (cpi->buffer_level > cpi->oxcf.optimal_buffer_level)
3533 int Fraction = ((cpi->buffer_level - cpi->oxcf.optimal_buffer_level) * 128) / (cpi->oxcf.maximum_buffer_size - cpi->oxcf.optimal_buffer_level);
3534 int min_qadjustment = ((cpi->active_best_quality - cpi->best_quality) * Fraction) / 128;
3536 cpi->active_best_quality -= min_qadjustment;
3540 // Make sure constrained quality mode limits are adhered to for the first
3541 // few frames of one pass encodes
3542 else if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY)
3544 if ( (cm->frame_type == KEY_FRAME) ||
3545 cm->refresh_golden_frame || cpi->common.refresh_alt_ref_frame )
3547 cpi->active_best_quality = cpi->best_quality;
3549 else if (cpi->active_best_quality < cpi->cq_target_quality)
3551 cpi->active_best_quality = cpi->cq_target_quality;
3555 // Clip the active best and worst quality values to limits
3556 if (cpi->active_worst_quality > cpi->worst_quality)
3557 cpi->active_worst_quality = cpi->worst_quality;
3559 if (cpi->active_best_quality < cpi->best_quality)
3560 cpi->active_best_quality = cpi->best_quality;
3562 if ( cpi->active_worst_quality < cpi->active_best_quality )
3563 cpi->active_worst_quality = cpi->active_best_quality;
3565 // Determine initial Q to try
3566 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
3567 last_zbin_oq = cpi->zbin_over_quant;
3569 // Set highest allowed value for Zbin over quant
3570 if (cm->frame_type == KEY_FRAME)
3571 zbin_oq_high = 0; //ZBIN_OQ_MAX/16
3572 else if ((cpi->oxcf.number_of_layers == 1) && ((cm->refresh_alt_ref_frame ||
3573 (cm->refresh_golden_frame && !cpi->source_alt_ref_active))))
3578 zbin_oq_high = ZBIN_OQ_MAX;
3580 // Setup background Q adjustment for error resilient mode.
3581 // For multi-layer encodes only enable this for the base layer.
3582 if (cpi->cyclic_refresh_mode_enabled && (cpi->current_layer==0))
3583 cyclic_background_refresh(cpi, Q, 0);
3585 vp8_compute_frame_size_bounds(cpi, &frame_under_shoot_limit, &frame_over_shoot_limit);
3587 // Limit Q range for the adaptive loop.
3588 bottom_index = cpi->active_best_quality;
3589 top_index = cpi->active_worst_quality;
3590 q_low = cpi->active_best_quality;
3591 q_high = cpi->active_worst_quality;
3593 vp8_save_coding_context(cpi);
3598 scale_and_extend_source(cpi->un_scaled_source, cpi);
3599 #if !(CONFIG_REALTIME_ONLY) && CONFIG_POSTPROC && !(CONFIG_TEMPORAL_DENOISING)
3601 if (cpi->oxcf.noise_sensitivity > 0)
3606 switch (cpi->oxcf.noise_sensitivity)
3629 if (cm->frame_type == KEY_FRAME)
3631 vp8_de_noise(cpi->Source, cpi->Source, l , 1, 0);
3635 vp8_de_noise(cpi->Source, cpi->Source, l , 1, 0);
3637 src = cpi->Source->y_buffer;
3639 if (cpi->Source->y_stride < 0)
3641 src += cpi->Source->y_stride * (cpi->Source->y_height - 1);
3648 #ifdef OUTPUT_YUV_SRC
3649 vp8_write_yuv_frame(cpi->Source);
3654 vp8_clear_system_state(); //__asm emms;
3657 if(cpi->is_src_frame_alt_ref)
3661 vp8_set_quantizer(cpi, Q);
3664 // setup skip prob for costing in mode/mv decision
3665 if (cpi->common.mb_no_coeff_skip)
3667 cpi->prob_skip_false = cpi->base_skip_false_prob[Q];
3669 if (cm->frame_type != KEY_FRAME)
3671 if (cpi->common.refresh_alt_ref_frame)
3673 if (cpi->last_skip_false_probs[2] != 0)
3674 cpi->prob_skip_false = cpi->last_skip_false_probs[2];
3677 if(cpi->last_skip_false_probs[2]!=0 && abs(Q- cpi->last_skip_probs_q[2])<=16 )
3678 cpi->prob_skip_false = cpi->last_skip_false_probs[2];
3679 else if (cpi->last_skip_false_probs[2]!=0)
3680 cpi->prob_skip_false = (cpi->last_skip_false_probs[2] + cpi->prob_skip_false ) / 2;
3683 else if (cpi->common.refresh_golden_frame)
3685 if (cpi->last_skip_false_probs[1] != 0)
3686 cpi->prob_skip_false = cpi->last_skip_false_probs[1];
3689 if(cpi->last_skip_false_probs[1]!=0 && abs(Q- cpi->last_skip_probs_q[1])<=16 )
3690 cpi->prob_skip_false = cpi->last_skip_false_probs[1];
3691 else if (cpi->last_skip_false_probs[1]!=0)
3692 cpi->prob_skip_false = (cpi->last_skip_false_probs[1] + cpi->prob_skip_false ) / 2;
3697 if (cpi->last_skip_false_probs[0] != 0)
3698 cpi->prob_skip_false = cpi->last_skip_false_probs[0];
3701 if(cpi->last_skip_false_probs[0]!=0 && abs(Q- cpi->last_skip_probs_q[0])<=16 )
3702 cpi->prob_skip_false = cpi->last_skip_false_probs[0];
3703 else if(cpi->last_skip_false_probs[0]!=0)
3704 cpi->prob_skip_false = (cpi->last_skip_false_probs[0] + cpi->prob_skip_false ) / 2;
3708 //as this is for cost estimate, let's make sure it does not go extreme eitehr way
3709 if (cpi->prob_skip_false < 5)
3710 cpi->prob_skip_false = 5;
3712 if (cpi->prob_skip_false > 250)
3713 cpi->prob_skip_false = 250;
3715 if (cpi->oxcf.number_of_layers == 1 && cpi->is_src_frame_alt_ref)
3716 cpi->prob_skip_false = 1;
3723 FILE *f = fopen("skip.stt", "a");
3724 fprintf(f, "%d, %d, %4d ", cpi->common.refresh_golden_frame, cpi->common.refresh_alt_ref_frame, cpi->prob_skip_false);
3732 if (cm->frame_type == KEY_FRAME)
3734 resize_key_frame(cpi);
3735 vp8_setup_key_frame(cpi);
3738 #if CONFIG_MULTITHREAD
3739 /* wait for the last picture loopfilter thread done */
3740 if (cpi->b_lpf_running)
3742 sem_wait(&cpi->h_event_end_lpf);
3743 cpi->b_lpf_running = 0;
3747 #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
3749 if(cpi->oxcf.error_resilient_mode)
3750 cm->refresh_entropy_probs = 0;
3752 if (cpi->oxcf.error_resilient_mode & VPX_ERROR_RESILIENT_PARTITIONS)
3754 if (cm->frame_type == KEY_FRAME)
3755 cm->refresh_entropy_probs = 1;
3758 if (cm->refresh_entropy_probs == 0)
3760 // save a copy for later refresh
3761 vpx_memcpy(&cm->lfc, &cm->fc, sizeof(cm->fc));
3764 vp8_update_coef_context(cpi);
3766 vp8_update_coef_probs(cpi);
3768 // transform / motion compensation build reconstruction frame
3769 // +pack coef partitions
3770 vp8_encode_frame(cpi);
3772 /* cpi->projected_frame_size is not needed for RT mode */
3775 // transform / motion compensation build reconstruction frame
3776 vp8_encode_frame(cpi);
3778 cpi->projected_frame_size -= vp8_estimate_entropy_savings(cpi);
3779 cpi->projected_frame_size = (cpi->projected_frame_size > 0) ? cpi->projected_frame_size : 0;
3781 vp8_clear_system_state(); //__asm emms;
3783 // Test to see if the stats generated for this frame indicate that we should have coded a key frame
3784 // (assuming that we didn't)!
3785 if (cpi->pass != 2 && cpi->oxcf.auto_key && cm->frame_type != KEY_FRAME)
3787 int key_frame_decision = decide_key_frame(cpi);
3789 if (cpi->compressor_speed == 2)
3791 /* we don't do re-encoding in realtime mode
3792 * if key frame is decided than we force it on next frame */
3793 cpi->force_next_frame_intra = key_frame_decision;
3795 else if (key_frame_decision)
3797 // Reset all our sizing numbers and recode
3798 cm->frame_type = KEY_FRAME;
3800 vp8_pick_frame_size(cpi);
3802 // Clear the Alt reference frame active flag when we have a key frame
3803 cpi->source_alt_ref_active = 0;
3805 // Reset the loop filter deltas and segmentation map
3806 setup_features(cpi);
3808 // If segmentation is enabled force a map update for key frames
3809 if (cpi->mb.e_mbd.segmentation_enabled)
3811 cpi->mb.e_mbd.update_mb_segmentation_map = 1;
3812 cpi->mb.e_mbd.update_mb_segmentation_data = 1;
3815 vp8_restore_coding_context(cpi);
3817 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
3819 vp8_compute_frame_size_bounds(cpi, &frame_under_shoot_limit, &frame_over_shoot_limit);
3821 // Limit Q range for the adaptive loop.
3822 bottom_index = cpi->active_best_quality;
3823 top_index = cpi->active_worst_quality;
3824 q_low = cpi->active_best_quality;
3825 q_high = cpi->active_worst_quality;
3834 vp8_clear_system_state();
3836 if (frame_over_shoot_limit == 0)
3837 frame_over_shoot_limit = 1;
3839 // Are we are overshooting and up against the limit of active max Q.
3840 if (((cpi->pass != 2) || (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)) &&
3841 (Q == cpi->active_worst_quality) &&
3842 (cpi->active_worst_quality < cpi->worst_quality) &&
3843 (cpi->projected_frame_size > frame_over_shoot_limit))
3845 int over_size_percent = ((cpi->projected_frame_size - frame_over_shoot_limit) * 100) / frame_over_shoot_limit;
3847 // If so is there any scope for relaxing it
3848 while ((cpi->active_worst_quality < cpi->worst_quality) && (over_size_percent > 0))
3850 cpi->active_worst_quality++;
3851 top_index = cpi->active_worst_quality;
3852 over_size_percent = (int)(over_size_percent * 0.96); // Assume 1 qstep = about 4% on frame size.
3855 // If we have updated the active max Q do not call vp8_update_rate_correction_factors() this loop.
3856 active_worst_qchanged = 1;
3859 active_worst_qchanged = 0;
3861 #if !(CONFIG_REALTIME_ONLY)
3862 // Special case handling for forced key frames
3863 if ( (cm->frame_type == KEY_FRAME) && cpi->this_key_frame_forced )
3866 int kf_err = vp8_calc_ss_err(cpi->Source,
3867 &cm->yv12_fb[cm->new_fb_idx]);
3869 // The key frame is not good enough
3870 if ( kf_err > ((cpi->ambient_err * 7) >> 3) )
3873 q_high = (Q > q_low) ? (Q - 1) : q_low;
3876 Q = (q_high + q_low) >> 1;
3878 // The key frame is much better than the previous frame
3879 else if ( kf_err < (cpi->ambient_err >> 1) )
3882 q_low = (Q < q_high) ? (Q + 1) : q_high;
3885 Q = (q_high + q_low + 1) >> 1;
3888 // Clamp Q to upper and lower limits:
3897 // Is the projected frame size out of range and are we allowed to attempt to recode.
3898 else if ( recode_loop_test( cpi,
3899 frame_over_shoot_limit, frame_under_shoot_limit,
3900 Q, top_index, bottom_index ) )
3905 // Frame size out of permitted range:
3906 // Update correction factor & compute new Q to try...
3908 // Frame is too large
3909 if (cpi->projected_frame_size > cpi->this_frame_target)
3911 //if ( cpi->zbin_over_quant == 0 )
3912 q_low = (Q < q_high) ? (Q + 1) : q_high; // Raise Qlow as to at least the current value
3914 if (cpi->zbin_over_quant > 0) // If we are using over quant do the same for zbin_oq_low
3915 zbin_oq_low = (cpi->zbin_over_quant < zbin_oq_high) ? (cpi->zbin_over_quant + 1) : zbin_oq_high;
3917 //if ( undershoot_seen || (Q == MAXQ) )
3918 if (undershoot_seen)
3920 // Update rate_correction_factor unless cpi->active_worst_quality has changed.
3921 if (!active_worst_qchanged)
3922 vp8_update_rate_correction_factors(cpi, 1);
3924 Q = (q_high + q_low + 1) / 2;
3926 // Adjust cpi->zbin_over_quant (only allowed when Q is max)
3928 cpi->zbin_over_quant = 0;
3931 zbin_oq_low = (cpi->zbin_over_quant < zbin_oq_high) ? (cpi->zbin_over_quant + 1) : zbin_oq_high;
3932 cpi->zbin_over_quant = (zbin_oq_high + zbin_oq_low) / 2;
3937 // Update rate_correction_factor unless cpi->active_worst_quality has changed.
3938 if (!active_worst_qchanged)
3939 vp8_update_rate_correction_factors(cpi, 0);
3941 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
3943 while (((Q < q_low) || (cpi->zbin_over_quant < zbin_oq_low)) && (Retries < 10))
3945 vp8_update_rate_correction_factors(cpi, 0);
3946 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
3953 // Frame is too small
3956 if (cpi->zbin_over_quant == 0)
3957 q_high = (Q > q_low) ? (Q - 1) : q_low; // Lower q_high if not using over quant
3958 else // else lower zbin_oq_high
3959 zbin_oq_high = (cpi->zbin_over_quant > zbin_oq_low) ? (cpi->zbin_over_quant - 1) : zbin_oq_low;
3963 // Update rate_correction_factor unless cpi->active_worst_quality has changed.
3964 if (!active_worst_qchanged)
3965 vp8_update_rate_correction_factors(cpi, 1);
3967 Q = (q_high + q_low) / 2;
3969 // Adjust cpi->zbin_over_quant (only allowed when Q is max)
3971 cpi->zbin_over_quant = 0;
3973 cpi->zbin_over_quant = (zbin_oq_high + zbin_oq_low) / 2;
3977 // Update rate_correction_factor unless cpi->active_worst_quality has changed.
3978 if (!active_worst_qchanged)
3979 vp8_update_rate_correction_factors(cpi, 0);
3981 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
3983 // Special case reset for qlow for constrained quality.
3984 // This should only trigger where there is very substantial
3985 // undershoot on a frame and the auto cq level is above
3986 // the user passsed in value.
3987 if ( (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) &&
3993 while (((Q > q_high) || (cpi->zbin_over_quant > zbin_oq_high)) && (Retries < 10))
3995 vp8_update_rate_correction_factors(cpi, 0);
3996 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4001 undershoot_seen = 1;
4004 // Clamp Q to upper and lower limits:
4010 // Clamp cpi->zbin_over_quant
4011 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;
4013 //Loop = (Q != last_q) || (last_zbin_oq != cpi->zbin_over_quant);
4015 last_zbin_oq = cpi->zbin_over_quant;
4021 if (cpi->is_src_frame_alt_ref)
4026 vp8_restore_coding_context(cpi);
4028 #if CONFIG_INTERNAL_STATS
4029 cpi->tot_recode_hits++;
4036 // Experimental code for lagged and one pass
4037 // Update stats used for one pass GF selection
4041 double frame_intra_error;
4042 double frame_coded_error;
4043 double frame_pcnt_inter;
4044 double frame_pcnt_motion;
4046 double frame_mvr_abs;
4048 double frame_mvc_abs;
4051 cpi->one_pass_frame_stats[cpi->one_pass_frame_index].frame_coded_error = (double)cpi->prediction_error;
4052 cpi->one_pass_frame_stats[cpi->one_pass_frame_index].frame_intra_error = (double)cpi->intra_error;
4053 cpi->one_pass_frame_stats[cpi->one_pass_frame_index].frame_pcnt_inter = (double)(100 - cpi->this_frame_percent_intra) / 100.0;
4057 // Special case code to reduce pulsing when key frames are forced at a
4058 // fixed interval. Note the reconstruction error if it is the frame before
4059 // the force key frame
4060 if ( cpi->next_key_frame_forced && (cpi->twopass.frames_to_key == 0) )
4062 cpi->ambient_err = vp8_calc_ss_err(cpi->Source,
4063 &cm->yv12_fb[cm->new_fb_idx]);
4066 /* This frame's MVs are saved and will be used in next frame's MV predictor.
4067 * Last frame has one more line(add to bottom) and one more column(add to
4068 * right) than cm->mip. The edge elements are initialized to 0.
4070 #if CONFIG_MULTI_RES_ENCODING
4071 if(!cpi->oxcf.mr_encoder_id && cm->show_frame)
4073 if(cm->show_frame) /* do not save for altref frame */
4078 /* Point to beginning of allocated MODE_INFO arrays. */
4079 MODE_INFO *tmp = cm->mip;
4081 if(cm->frame_type != KEY_FRAME)
4083 for (mb_row = 0; mb_row < cm->mb_rows+1; mb_row ++)
4085 for (mb_col = 0; mb_col < cm->mb_cols+1; mb_col ++)
4087 if(tmp->mbmi.ref_frame != INTRA_FRAME)
4088 cpi->lfmv[mb_col + mb_row*(cm->mode_info_stride+1)].as_int = tmp->mbmi.mv.as_int;
4090 cpi->lf_ref_frame_sign_bias[mb_col + mb_row*(cm->mode_info_stride+1)] = cm->ref_frame_sign_bias[tmp->mbmi.ref_frame];
4091 cpi->lf_ref_frame[mb_col + mb_row*(cm->mode_info_stride+1)] = tmp->mbmi.ref_frame;
4098 #if CONFIG_MULTI_RES_ENCODING
4099 vp8_cal_dissimilarity(cpi);
4102 // Update the GF useage maps.
4103 // This is done after completing the compression of a frame when all
4104 // modes etc. are finalized but before loop filter
4105 if (cpi->oxcf.number_of_layers == 1)
4106 vp8_update_gf_useage_maps(cpi, cm, &cpi->mb);
4108 if (cm->frame_type == KEY_FRAME)
4109 cm->refresh_last_frame = 1;
4113 FILE *f = fopen("gfactive.stt", "a");
4114 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);
4119 // For inter frames the current default behavior is that when
4120 // cm->refresh_golden_frame is set we copy the old GF over to the ARF buffer
4121 // This is purely an encoder decision at present.
4122 if (!cpi->oxcf.error_resilient_mode && cm->refresh_golden_frame)
4123 cm->copy_buffer_to_arf = 2;
4125 cm->copy_buffer_to_arf = 0;
4127 cm->frame_to_show = &cm->yv12_fb[cm->new_fb_idx];
4129 #if CONFIG_MULTITHREAD
4130 if (cpi->b_multi_threaded)
4132 sem_post(&cpi->h_event_start_lpf); /* start loopfilter in separate thread */
4133 cpi->b_lpf_running = 1;
4138 vp8_loopfilter_frame(cpi, cm);
4141 update_reference_frames(cm);
4143 #if !(CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
4144 if (cpi->oxcf.error_resilient_mode)
4146 cm->refresh_entropy_probs = 0;
4150 #if CONFIG_MULTITHREAD
4151 /* wait that filter_level is picked so that we can continue with stream packing */
4152 if (cpi->b_multi_threaded)
4153 sem_wait(&cpi->h_event_end_lpf);
4156 // build the bitstream
4157 vp8_pack_bitstream(cpi, dest, dest_end, size);
4159 #if CONFIG_MULTITHREAD
4160 /* if PSNR packets are generated we have to wait for the lpf */
4161 if (cpi->b_lpf_running && cpi->b_calculate_psnr)
4163 sem_wait(&cpi->h_event_end_lpf);
4164 cpi->b_lpf_running = 0;
4168 /* Move storing frame_type out of the above loop since it is also
4169 * needed in motion search besides loopfilter */
4170 cm->last_frame_type = cm->frame_type;
4172 // Update rate control heuristics
4173 cpi->total_byte_count += (*size);
4174 cpi->projected_frame_size = (*size) << 3;
4176 if (cpi->oxcf.number_of_layers > 1)
4179 for (i=cpi->current_layer+1; i<cpi->oxcf.number_of_layers; i++)
4180 cpi->layer_context[i].total_byte_count += (*size);
4183 if (!active_worst_qchanged)
4184 vp8_update_rate_correction_factors(cpi, 2);
4186 cpi->last_q[cm->frame_type] = cm->base_qindex;
4188 if (cm->frame_type == KEY_FRAME)
4190 vp8_adjust_key_frame_context(cpi);
4193 // Keep a record of ambient average Q.
4194 if (cm->frame_type != KEY_FRAME)
4195 cpi->avg_frame_qindex = (2 + 3 * cpi->avg_frame_qindex + cm->base_qindex) >> 2;
4197 // Keep a record from which we can calculate the average Q excluding GF updates and key frames
4198 if ((cm->frame_type != KEY_FRAME) && ((cpi->oxcf.number_of_layers > 1) ||
4199 (!cm->refresh_golden_frame && !cm->refresh_alt_ref_frame)))
4203 // Calculate the average Q for normal inter frames (not key or GFU
4205 if ( cpi->pass == 2 )
4207 cpi->ni_tot_qi += Q;
4208 cpi->ni_av_qi = (cpi->ni_tot_qi / cpi->ni_frames);
4212 // Damp value for first few frames
4213 if (cpi->ni_frames > 150 )
4215 cpi->ni_tot_qi += Q;
4216 cpi->ni_av_qi = (cpi->ni_tot_qi / cpi->ni_frames);
4218 // For one pass, early in the clip ... average the current frame Q
4219 // value with the worstq entered by the user as a dampening measure
4222 cpi->ni_tot_qi += Q;
4223 cpi->ni_av_qi = ((cpi->ni_tot_qi / cpi->ni_frames) + cpi->worst_quality + 1) / 2;
4226 // If the average Q is higher than what was used in the last frame
4227 // (after going through the recode loop to keep the frame size within range)
4228 // then use the last frame value - 1.
4229 // The -1 is designed to stop Q and hence the data rate, from progressively
4230 // falling away during difficult sections, but at the same time reduce the number of
4231 // itterations around the recode loop.
4232 if (Q > cpi->ni_av_qi)
4233 cpi->ni_av_qi = Q - 1;
4239 // If the frame was massively oversize and we are below optimal buffer level drop next frame
4240 if ((cpi->drop_frames_allowed) &&
4241 (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) &&
4242 (cpi->buffer_level < cpi->oxcf.drop_frames_water_mark * cpi->oxcf.optimal_buffer_level / 100) &&
4243 (cpi->projected_frame_size > (4 * cpi->this_frame_target)))
4245 cpi->drop_frame = 1;
4250 // Set the count for maximum consecutive dropped frames based upon the ratio of
4251 // this frame size to the target average per frame bandwidth.
4252 // (cpi->av_per_frame_bandwidth > 0) is just a sanity check to prevent / 0.
4253 if (cpi->drop_frames_allowed && (cpi->av_per_frame_bandwidth > 0))
4255 cpi->max_drop_count = cpi->projected_frame_size / cpi->av_per_frame_bandwidth;
4257 if (cpi->max_drop_count > cpi->max_consec_dropped_frames)
4258 cpi->max_drop_count = cpi->max_consec_dropped_frames;
4261 // Update the buffer level variable.
4262 // Non-viewable frames are a special case and are treated as pure overhead.
4263 if ( !cm->show_frame )
4264 cpi->bits_off_target -= cpi->projected_frame_size;
4266 cpi->bits_off_target += cpi->av_per_frame_bandwidth - cpi->projected_frame_size;
4268 // Clip the buffer level to the maximum specified buffer size
4269 if (cpi->bits_off_target > cpi->oxcf.maximum_buffer_size)
4270 cpi->bits_off_target = cpi->oxcf.maximum_buffer_size;
4272 // Rolling monitors of whether we are over or underspending used to help regulate min and Max Q in two pass.
4273 cpi->rolling_target_bits = ((cpi->rolling_target_bits * 3) + cpi->this_frame_target + 2) / 4;
4274 cpi->rolling_actual_bits = ((cpi->rolling_actual_bits * 3) + cpi->projected_frame_size + 2) / 4;
4275 cpi->long_rolling_target_bits = ((cpi->long_rolling_target_bits * 31) + cpi->this_frame_target + 16) / 32;
4276 cpi->long_rolling_actual_bits = ((cpi->long_rolling_actual_bits * 31) + cpi->projected_frame_size + 16) / 32;
4278 // Actual bits spent
4279 cpi->total_actual_bits += cpi->projected_frame_size;
4282 cpi->total_target_vs_actual += (cpi->this_frame_target - cpi->projected_frame_size);
4284 cpi->buffer_level = cpi->bits_off_target;
4286 // Propagate values to higher temporal layers
4287 if (cpi->oxcf.number_of_layers > 1)
4291 for (i=cpi->current_layer+1; i<cpi->oxcf.number_of_layers; i++)
4293 LAYER_CONTEXT *lc = &cpi->layer_context[i];
4294 int bits_off_for_this_layer = lc->target_bandwidth / lc->frame_rate
4295 - cpi->projected_frame_size;
4297 lc->bits_off_target += bits_off_for_this_layer;
4299 // Clip buffer level to maximum buffer size for the layer
4300 if (lc->bits_off_target > lc->maximum_buffer_size)
4301 lc->bits_off_target = lc->maximum_buffer_size;
4303 lc->total_actual_bits += cpi->projected_frame_size;
4304 lc->total_target_vs_actual += bits_off_for_this_layer;
4305 lc->buffer_level = lc->bits_off_target;
4309 // Update bits left to the kf and gf groups to account for overshoot or undershoot on these frames
4310 if (cm->frame_type == KEY_FRAME)
4312 cpi->twopass.kf_group_bits += cpi->this_frame_target - cpi->projected_frame_size;
4314 if (cpi->twopass.kf_group_bits < 0)
4315 cpi->twopass.kf_group_bits = 0 ;
4317 else if (cm->refresh_golden_frame || cm->refresh_alt_ref_frame)
4319 cpi->twopass.gf_group_bits += cpi->this_frame_target - cpi->projected_frame_size;
4321 if (cpi->twopass.gf_group_bits < 0)
4322 cpi->twopass.gf_group_bits = 0 ;
4325 if (cm->frame_type != KEY_FRAME)
4327 if (cpi->common.refresh_alt_ref_frame)
4329 cpi->last_skip_false_probs[2] = cpi->prob_skip_false;
4330 cpi->last_skip_probs_q[2] = cm->base_qindex;
4332 else if (cpi->common.refresh_golden_frame)
4334 cpi->last_skip_false_probs[1] = cpi->prob_skip_false;
4335 cpi->last_skip_probs_q[1] = cm->base_qindex;
4339 cpi->last_skip_false_probs[0] = cpi->prob_skip_false;
4340 cpi->last_skip_probs_q[0] = cm->base_qindex;
4342 //update the baseline
4343 cpi->base_skip_false_prob[cm->base_qindex] = cpi->prob_skip_false;
4348 #if 0 && CONFIG_INTERNAL_STATS
4350 FILE *f = fopen("tmp.stt", "a");
4352 vp8_clear_system_state(); //__asm emms;
4354 if (cpi->twopass.total_left_stats.coded_error != 0.0)
4355 fprintf(f, "%10d %10d %10d %10d %10d %10d %10d %10d %10d %6d %6d"
4356 "%6d %6d %6d %5d %5d %5d %8d %8.2f %10d %10.3f"
4358 cpi->common.current_video_frame, cpi->this_frame_target,
4359 cpi->projected_frame_size,
4360 (cpi->projected_frame_size - cpi->this_frame_target),
4361 (int)cpi->total_target_vs_actual,
4363 (cpi->oxcf.starting_buffer_level-cpi->bits_off_target),
4364 (int)cpi->total_actual_bits, cm->base_qindex,
4365 cpi->active_best_quality, cpi->active_worst_quality,
4366 cpi->ni_av_qi, cpi->cq_target_quality,
4367 cpi->zbin_over_quant,
4368 //cpi->avg_frame_qindex, cpi->zbin_over_quant,
4369 cm->refresh_golden_frame, cm->refresh_alt_ref_frame,
4370 cm->frame_type, cpi->gfu_boost,
4371 cpi->twopass.est_max_qcorrection_factor,
4372 (int)cpi->twopass.bits_left,
4373 cpi->twopass.total_left_stats.coded_error,
4374 (double)cpi->twopass.bits_left /
4375 cpi->twopass.total_left_stats.coded_error,
4376 cpi->tot_recode_hits);
4378 fprintf(f, "%10d %10d %10d %10d %10d %10d %10d %10d %10d %6d %6d"
4379 "%6d %6d %6d %5d %5d %5d %8d %8.2f %10d %10.3f"
4381 cpi->common.current_video_frame,
4382 cpi->this_frame_target, cpi->projected_frame_size,
4383 (cpi->projected_frame_size - cpi->this_frame_target),
4384 (int)cpi->total_target_vs_actual,
4386 (cpi->oxcf.starting_buffer_level-cpi->bits_off_target),
4387 (int)cpi->total_actual_bits, cm->base_qindex,
4388 cpi->active_best_quality, cpi->active_worst_quality,
4389 cpi->ni_av_qi, cpi->cq_target_quality,
4390 cpi->zbin_over_quant,
4391 //cpi->avg_frame_qindex, cpi->zbin_over_quant,
4392 cm->refresh_golden_frame, cm->refresh_alt_ref_frame,
4393 cm->frame_type, cpi->gfu_boost,
4394 cpi->twopass.est_max_qcorrection_factor,
4395 (int)cpi->twopass.bits_left,
4396 cpi->twopass.total_left_stats.coded_error,
4397 cpi->tot_recode_hits);
4402 FILE *fmodes = fopen("Modes.stt", "a");
4405 fprintf(fmodes, "%6d:%1d:%1d:%1d ",
4406 cpi->common.current_video_frame,
4407 cm->frame_type, cm->refresh_golden_frame,
4408 cm->refresh_alt_ref_frame);
4410 for (i = 0; i < MAX_MODES; i++)
4411 fprintf(fmodes, "%5d ", cpi->mode_chosen_counts[i]);
4413 fprintf(fmodes, "\n");
4421 // If this was a kf or Gf note the Q
4422 if ((cm->frame_type == KEY_FRAME) || cm->refresh_golden_frame || cm->refresh_alt_ref_frame)
4423 cm->last_kf_gf_q = cm->base_qindex;
4425 if (cm->refresh_golden_frame == 1)
4426 cm->frame_flags = cm->frame_flags | FRAMEFLAGS_GOLDEN;
4428 cm->frame_flags = cm->frame_flags&~FRAMEFLAGS_GOLDEN;
4430 if (cm->refresh_alt_ref_frame == 1)
4431 cm->frame_flags = cm->frame_flags | FRAMEFLAGS_ALTREF;
4433 cm->frame_flags = cm->frame_flags&~FRAMEFLAGS_ALTREF;
4436 if (cm->refresh_last_frame & cm->refresh_golden_frame) // both refreshed
4437 cpi->gold_is_last = 1;
4438 else if (cm->refresh_last_frame ^ cm->refresh_golden_frame) // 1 refreshed but not the other
4439 cpi->gold_is_last = 0;
4441 if (cm->refresh_last_frame & cm->refresh_alt_ref_frame) // both refreshed
4442 cpi->alt_is_last = 1;
4443 else if (cm->refresh_last_frame ^ cm->refresh_alt_ref_frame) // 1 refreshed but not the other
4444 cpi->alt_is_last = 0;
4446 if (cm->refresh_alt_ref_frame & cm->refresh_golden_frame) // both refreshed
4447 cpi->gold_is_alt = 1;
4448 else if (cm->refresh_alt_ref_frame ^ cm->refresh_golden_frame) // 1 refreshed but not the other
4449 cpi->gold_is_alt = 0;
4451 cpi->ref_frame_flags = VP8_ALT_FLAG | VP8_GOLD_FLAG | VP8_LAST_FLAG;
4453 if (cpi->gold_is_last)
4454 cpi->ref_frame_flags &= ~VP8_GOLD_FLAG;
4456 if (cpi->alt_is_last)
4457 cpi->ref_frame_flags &= ~VP8_ALT_FLAG;
4459 if (cpi->gold_is_alt)
4460 cpi->ref_frame_flags &= ~VP8_ALT_FLAG;
4463 if (!cpi->oxcf.error_resilient_mode)
4465 if (cpi->oxcf.play_alternate && cm->refresh_alt_ref_frame && (cm->frame_type != KEY_FRAME))
4466 // Update the alternate reference frame stats as appropriate.
4467 update_alt_ref_frame_stats(cpi);
4469 // Update the Golden frame stats as appropriate.
4470 update_golden_frame_stats(cpi);
4473 if (cm->frame_type == KEY_FRAME)
4475 // Tell the caller that the frame was coded as a key frame
4476 *frame_flags = cm->frame_flags | FRAMEFLAGS_KEY;
4478 // As this frame is a key frame the next defaults to an inter frame.
4479 cm->frame_type = INTER_FRAME;
4481 cpi->last_frame_percent_intra = 100;
4485 *frame_flags = cm->frame_flags&~FRAMEFLAGS_KEY;
4487 cpi->last_frame_percent_intra = cpi->this_frame_percent_intra;
4490 // Clear the one shot update flags for segmentation map and mode/ref loop filter deltas.
4491 cpi->mb.e_mbd.update_mb_segmentation_map = 0;
4492 cpi->mb.e_mbd.update_mb_segmentation_data = 0;
4493 cpi->mb.e_mbd.mode_ref_lf_delta_update = 0;
4496 // Dont increment frame counters if this was an altref buffer update not a real frame
4499 cm->current_video_frame++;
4500 cpi->frames_since_key++;
4503 // reset to normal state now that we are done.
4511 sprintf(filename, "enc%04d.yuv", (int) cm->current_video_frame);
4512 recon_file = fopen(filename, "wb");
4513 fwrite(cm->yv12_fb[cm->lst_fb_idx].buffer_alloc,
4514 cm->yv12_fb[cm->lst_fb_idx].frame_size, 1, recon_file);
4520 //vp8_write_yuv_frame("encoder_recon.yuv", cm->frame_to_show);
4526 static void check_gf_quality(VP8_COMP *cpi)
4528 VP8_COMMON *cm = &cpi->common;
4529 int gf_active_pct = (100 * cpi->gf_active_count) / (cm->mb_rows * cm->mb_cols);
4530 int gf_ref_usage_pct = (cpi->count_mb_ref_frame_usage[GOLDEN_FRAME] * 100) / (cm->mb_rows * cm->mb_cols);
4531 int last_ref_zz_useage = (cpi->inter_zz_count * 100) / (cm->mb_rows * cm->mb_cols);
4533 // Gf refresh is not currently being signalled
4534 if (cpi->gf_update_recommended == 0)
4536 if (cpi->common.frames_since_golden > 7)
4539 if ((gf_active_pct < 10) || ((gf_active_pct + gf_ref_usage_pct) < 15))
4541 // ...but last frame zero zero usage is reasonbable so a new gf might be appropriate
4542 if (last_ref_zz_useage >= 25)
4544 cpi->gf_bad_count ++;
4546 if (cpi->gf_bad_count >= 8) // Check that the condition is stable
4548 cpi->gf_update_recommended = 1;
4549 cpi->gf_bad_count = 0;
4553 cpi->gf_bad_count = 0; // Restart count as the background is not stable enough
4556 cpi->gf_bad_count = 0; // Gf useage has picked up so reset count
4559 // If the signal is set but has not been read should we cancel it.
4560 else if (last_ref_zz_useage < 15)
4562 cpi->gf_update_recommended = 0;
4563 cpi->gf_bad_count = 0;
4568 FILE *f = fopen("gfneeded.stt", "a");
4569 fprintf(f, "%10d %10d %10d %10d %10ld \n",
4570 cm->current_video_frame,
4571 cpi->common.frames_since_golden,
4572 gf_active_pct, gf_ref_usage_pct,
4573 cpi->gf_update_recommended);
4580 #if !(CONFIG_REALTIME_ONLY)
4581 static void Pass2Encode(VP8_COMP *cpi, unsigned long *size, unsigned char *dest, unsigned char * dest_end, unsigned int *frame_flags)
4584 if (!cpi->common.refresh_alt_ref_frame)
4585 vp8_second_pass(cpi);
4587 encode_frame_to_data_rate(cpi, size, dest, dest_end, frame_flags);
4588 cpi->twopass.bits_left -= 8 * *size;
4590 if (!cpi->common.refresh_alt_ref_frame)
4592 double two_pass_min_rate = (double)(cpi->oxcf.target_bandwidth
4593 *cpi->oxcf.two_pass_vbrmin_section / 100);
4594 cpi->twopass.bits_left += (int64_t)(two_pass_min_rate / cpi->frame_rate);
4599 //For ARM NEON, d8-d15 are callee-saved registers, and need to be saved by us.
4601 extern void vp8_push_neon(int64_t *store);
4602 extern void vp8_pop_neon(int64_t *store);
4606 int vp8_receive_raw_frame(VP8_COMP *cpi, unsigned int frame_flags, YV12_BUFFER_CONFIG *sd, int64_t time_stamp, int64_t end_time)
4609 int64_t store_reg[8];
4611 VP8_COMMON *cm = &cpi->common;
4612 struct vpx_usec_timer timer;
4616 #if CONFIG_RUNTIME_CPU_DETECT
4617 if (cm->cpu_caps & HAS_NEON)
4620 vp8_push_neon(store_reg);
4624 vpx_usec_timer_start(&timer);
4626 /* Reinit the lookahead buffer if the frame size changes */
4627 if (sd->y_width != cpi->oxcf.Width || sd->y_height != cpi->oxcf.Height)
4629 assert(cpi->oxcf.lag_in_frames < 2);
4630 dealloc_raw_frame_buffers(cpi);
4631 alloc_raw_frame_buffers(cpi);
4634 if(vp8_lookahead_push(cpi->lookahead, sd, time_stamp, end_time,
4635 frame_flags, cpi->active_map_enabled ? cpi->active_map : NULL))
4637 cm->clr_type = sd->clrtype;
4638 vpx_usec_timer_mark(&timer);
4639 cpi->time_receive_data += vpx_usec_timer_elapsed(&timer);
4642 #if CONFIG_RUNTIME_CPU_DETECT
4643 if (cm->cpu_caps & HAS_NEON)
4646 vp8_pop_neon(store_reg);
4654 static int frame_is_reference(const VP8_COMP *cpi)
4656 const VP8_COMMON *cm = &cpi->common;
4657 const MACROBLOCKD *xd = &cpi->mb.e_mbd;
4659 return cm->frame_type == KEY_FRAME || cm->refresh_last_frame
4660 || cm->refresh_golden_frame || cm->refresh_alt_ref_frame
4661 || cm->copy_buffer_to_gf || cm->copy_buffer_to_arf
4662 || cm->refresh_entropy_probs
4663 || xd->mode_ref_lf_delta_update
4664 || xd->update_mb_segmentation_map || xd->update_mb_segmentation_data;
4668 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)
4671 int64_t store_reg[8];
4674 struct vpx_usec_timer tsctimer;
4675 struct vpx_usec_timer ticktimer;
4676 struct vpx_usec_timer cmptimer;
4677 YV12_BUFFER_CONFIG *force_src_buffer = NULL;
4684 if (setjmp(cpi->common.error.jmp))
4686 cpi->common.error.setjmp = 0;
4687 return VPX_CODEC_CORRUPT_FRAME;
4690 cpi->common.error.setjmp = 1;
4693 #if CONFIG_RUNTIME_CPU_DETECT
4694 if (cm->cpu_caps & HAS_NEON)
4697 vp8_push_neon(store_reg);
4701 vpx_usec_timer_start(&cmptimer);
4705 #if !(CONFIG_REALTIME_ONLY)
4706 // Should we code an alternate reference frame
4707 if (cpi->oxcf.error_resilient_mode == 0 &&
4708 cpi->oxcf.play_alternate &&
4709 cpi->source_alt_ref_pending)
4711 if ((cpi->source = vp8_lookahead_peek(cpi->lookahead,
4712 cpi->frames_till_gf_update_due,
4715 cpi->alt_ref_source = cpi->source;
4716 if (cpi->oxcf.arnr_max_frames > 0)
4718 vp8_temporal_filter_prepare_c(cpi,
4719 cpi->frames_till_gf_update_due);
4720 force_src_buffer = &cpi->alt_ref_buffer;
4722 cm->frames_till_alt_ref_frame = cpi->frames_till_gf_update_due;
4723 cm->refresh_alt_ref_frame = 1;
4724 cm->refresh_golden_frame = 0;
4725 cm->refresh_last_frame = 0;
4727 cpi->source_alt_ref_pending = 0; // Clear Pending alt Ref flag.
4728 cpi->is_src_frame_alt_ref = 0;
4735 /* Read last frame source if we are encoding first pass. */
4736 if (cpi->pass == 1 && cm->current_video_frame > 0)
4738 if((cpi->last_source = vp8_lookahead_peek(cpi->lookahead, 1,
4739 PEEK_BACKWARD)) == NULL)
4744 if ((cpi->source = vp8_lookahead_pop(cpi->lookahead, flush)))
4748 cpi->is_src_frame_alt_ref = cpi->alt_ref_source
4749 && (cpi->source == cpi->alt_ref_source);
4751 if(cpi->is_src_frame_alt_ref)
4752 cpi->alt_ref_source = NULL;
4758 cpi->Source = force_src_buffer ? force_src_buffer : &cpi->source->img;
4759 cpi->un_scaled_source = cpi->Source;
4760 *time_stamp = cpi->source->ts_start;
4761 *time_end = cpi->source->ts_end;
4762 *frame_flags = cpi->source->flags;
4764 if (cpi->pass == 1 && cm->current_video_frame > 0)
4766 cpi->last_frame_unscaled_source = &cpi->last_source->img;
4772 #if !(CONFIG_REALTIME_ONLY)
4774 if (flush && cpi->pass == 1 && !cpi->twopass.first_pass_done)
4776 vp8_end_first_pass(cpi); /* get last stats packet */
4777 cpi->twopass.first_pass_done = 1;
4783 #if CONFIG_RUNTIME_CPU_DETECT
4784 if (cm->cpu_caps & HAS_NEON)
4787 vp8_pop_neon(store_reg);
4793 if (cpi->source->ts_start < cpi->first_time_stamp_ever)
4795 cpi->first_time_stamp_ever = cpi->source->ts_start;
4796 cpi->last_end_time_stamp_seen = cpi->source->ts_start;
4799 // adjust frame rates based on timestamps given
4800 if (!cm->refresh_alt_ref_frame || (cpi->oxcf.number_of_layers > 1))
4802 int64_t this_duration;
4805 if (cpi->source->ts_start == cpi->first_time_stamp_ever)
4807 this_duration = cpi->source->ts_end - cpi->source->ts_start;
4812 int64_t last_duration;
4814 this_duration = cpi->source->ts_end - cpi->last_end_time_stamp_seen;
4815 last_duration = cpi->last_end_time_stamp_seen
4816 - cpi->last_time_stamp_seen;
4817 // do a step update if the duration changes by 10%
4819 step = ((this_duration - last_duration) * 10 / last_duration);
4825 cpi->ref_frame_rate = 10000000.0 / this_duration;
4828 double avg_duration, interval;
4830 /* Average this frame's rate into the last second's average
4831 * frame rate. If we haven't seen 1 second yet, then average
4832 * over the whole interval seen.
4834 interval = cpi->source->ts_end - cpi->first_time_stamp_ever;
4835 if(interval > 10000000.0)
4836 interval = 10000000;
4838 avg_duration = 10000000.0 / cpi->ref_frame_rate;
4839 avg_duration *= (interval - avg_duration + this_duration);
4840 avg_duration /= interval;
4842 cpi->ref_frame_rate = 10000000.0 / avg_duration;
4845 if (cpi->oxcf.number_of_layers > 1)
4849 // Update frame rates for each layer
4850 for (i=0; i<cpi->oxcf.number_of_layers; i++)
4852 LAYER_CONTEXT *lc = &cpi->layer_context[i];
4853 lc->frame_rate = cpi->ref_frame_rate /
4854 cpi->oxcf.rate_decimator[i];
4858 vp8_new_frame_rate(cpi, cpi->ref_frame_rate);
4861 cpi->last_time_stamp_seen = cpi->source->ts_start;
4862 cpi->last_end_time_stamp_seen = cpi->source->ts_end;
4865 if (cpi->oxcf.number_of_layers > 1)
4869 update_layer_contexts (cpi);
4871 // Restore layer specific context & set frame rate
4872 layer = cpi->oxcf.layer_id[
4873 cm->current_video_frame % cpi->oxcf.periodicity];
4874 restore_layer_context (cpi, layer);
4875 vp8_new_frame_rate (cpi, cpi->layer_context[layer].frame_rate);
4878 if (cpi->compressor_speed == 2)
4880 if (cpi->oxcf.number_of_layers == 1)
4881 check_gf_quality(cpi);
4882 vpx_usec_timer_start(&tsctimer);
4883 vpx_usec_timer_start(&ticktimer);
4886 #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
4889 const int num_part = (1 << cm->multi_token_partition);
4890 /* the available bytes in dest */
4891 const unsigned long dest_size = dest_end - dest;
4892 const int tok_part_buff_size = (dest_size * 9) / (10 * num_part);
4894 unsigned char *dp = dest;
4896 cpi->partition_d[0] = dp;
4897 dp += dest_size/10; /* reserve 1/10 for control partition */
4898 cpi->partition_d_end[0] = dp;
4900 for(i = 0; i < num_part; i++)
4902 cpi->partition_d[i + 1] = dp;
4903 dp += tok_part_buff_size;
4904 cpi->partition_d_end[i + 1] = dp;
4909 // start with a 0 size frame
4912 // Clear down mmx registers
4913 vp8_clear_system_state(); //__asm emms;
4915 cm->frame_type = INTER_FRAME;
4916 cm->frame_flags = *frame_flags;
4920 if (cm->refresh_alt_ref_frame)
4922 //cm->refresh_golden_frame = 1;
4923 cm->refresh_golden_frame = 0;
4924 cm->refresh_last_frame = 0;
4928 cm->refresh_golden_frame = 0;
4929 cm->refresh_last_frame = 1;
4933 /* find a free buffer for the new frame */
4936 for(; i < NUM_YV12_BUFFERS; i++)
4938 if(!cm->yv12_fb[i].flags)
4945 assert(i < NUM_YV12_BUFFERS );
4947 #if !(CONFIG_REALTIME_ONLY)
4951 Pass1Encode(cpi, size, dest, frame_flags);
4953 else if (cpi->pass == 2)
4955 Pass2Encode(cpi, size, dest, dest_end, frame_flags);
4959 encode_frame_to_data_rate(cpi, size, dest, dest_end, frame_flags);
4961 if (cpi->compressor_speed == 2)
4963 unsigned int duration, duration2;
4964 vpx_usec_timer_mark(&tsctimer);
4965 vpx_usec_timer_mark(&ticktimer);
4967 duration = vpx_usec_timer_elapsed(&ticktimer);
4968 duration2 = (unsigned int)((double)duration / 2);
4970 if (cm->frame_type != KEY_FRAME)
4972 if (cpi->avg_encode_time == 0)
4973 cpi->avg_encode_time = duration;
4975 cpi->avg_encode_time = (7 * cpi->avg_encode_time + duration) >> 3;
4980 //if(*frame_flags!=1)
4983 if (cpi->avg_pick_mode_time == 0)
4984 cpi->avg_pick_mode_time = duration2;
4986 cpi->avg_pick_mode_time = (7 * cpi->avg_pick_mode_time + duration2) >> 3;
4992 if (cm->refresh_entropy_probs == 0)
4994 vpx_memcpy(&cm->fc, &cm->lfc, sizeof(cm->fc));
4997 // Save the contexts separately for alt ref, gold and last.
4998 // (TODO jbb -> Optimize this with pointers to avoid extra copies. )
4999 if(cm->refresh_alt_ref_frame)
5000 vpx_memcpy(&cpi->lfc_a, &cm->fc, sizeof(cm->fc));
5002 if(cm->refresh_golden_frame)
5003 vpx_memcpy(&cpi->lfc_g, &cm->fc, sizeof(cm->fc));
5005 if(cm->refresh_last_frame)
5006 vpx_memcpy(&cpi->lfc_n, &cm->fc, sizeof(cm->fc));
5008 // if its a dropped frame honor the requests on subsequent frames
5011 cpi->droppable = !frame_is_reference(cpi);
5013 // return to normal state
5014 cm->refresh_entropy_probs = 1;
5015 cm->refresh_alt_ref_frame = 0;
5016 cm->refresh_golden_frame = 0;
5017 cm->refresh_last_frame = 1;
5018 cm->frame_type = INTER_FRAME;
5022 // Save layer specific state
5023 if (cpi->oxcf.number_of_layers > 1)
5024 save_layer_context (cpi);
5026 vpx_usec_timer_mark(&cmptimer);
5027 cpi->time_compress_data += vpx_usec_timer_elapsed(&cmptimer);
5029 if (cpi->b_calculate_psnr && cpi->pass != 1 && cm->show_frame)
5031 generate_psnr_packet(cpi);
5034 #if CONFIG_INTERNAL_STATS
5038 cpi->bytes += *size;
5045 if (cpi->b_calculate_psnr)
5049 YV12_BUFFER_CONFIG *orig = cpi->Source;
5050 YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
5051 int y_samples = orig->y_height * orig->y_width ;
5052 int uv_samples = orig->uv_height * orig->uv_width ;
5053 int t_samples = y_samples + 2 * uv_samples;
5054 int64_t sq_error, sq_error2;
5056 ye = calc_plane_error(orig->y_buffer, orig->y_stride,
5057 recon->y_buffer, recon->y_stride, orig->y_width, orig->y_height);
5059 ue = calc_plane_error(orig->u_buffer, orig->uv_stride,
5060 recon->u_buffer, recon->uv_stride, orig->uv_width, orig->uv_height);
5062 ve = calc_plane_error(orig->v_buffer, orig->uv_stride,
5063 recon->v_buffer, recon->uv_stride, orig->uv_width, orig->uv_height);
5065 sq_error = ye + ue + ve;
5067 frame_psnr = vp8_mse2psnr(t_samples, 255.0, sq_error);
5069 cpi->total_y += vp8_mse2psnr(y_samples, 255.0, ye);
5070 cpi->total_u += vp8_mse2psnr(uv_samples, 255.0, ue);
5071 cpi->total_v += vp8_mse2psnr(uv_samples, 255.0, ve);
5072 cpi->total_sq_error += sq_error;
5073 cpi->total += frame_psnr;
5076 YV12_BUFFER_CONFIG *pp = &cm->post_proc_buffer;
5077 double frame_psnr2, frame_ssim2 = 0;
5080 vp8_deblock(cm->frame_to_show, &cm->post_proc_buffer, cm->filter_level * 10 / 6, 1, 0);
5081 vp8_clear_system_state();
5083 ye = calc_plane_error(orig->y_buffer, orig->y_stride,
5084 pp->y_buffer, pp->y_stride, orig->y_width, orig->y_height);
5086 ue = calc_plane_error(orig->u_buffer, orig->uv_stride,
5087 pp->u_buffer, pp->uv_stride, orig->uv_width, orig->uv_height);
5089 ve = calc_plane_error(orig->v_buffer, orig->uv_stride,
5090 pp->v_buffer, pp->uv_stride, orig->uv_width, orig->uv_height);
5092 sq_error2 = ye + ue + ve;
5094 frame_psnr2 = vp8_mse2psnr(t_samples, 255.0, sq_error2);
5096 cpi->totalp_y += vp8_mse2psnr(y_samples, 255.0, ye);
5097 cpi->totalp_u += vp8_mse2psnr(uv_samples, 255.0, ue);
5098 cpi->totalp_v += vp8_mse2psnr(uv_samples, 255.0, ve);
5099 cpi->total_sq_error2 += sq_error2;
5100 cpi->totalp += frame_psnr2;
5102 frame_ssim2 = vp8_calc_ssim(cpi->Source,
5103 &cm->post_proc_buffer, 1, &weight);
5105 cpi->summed_quality += frame_ssim2 * weight;
5106 cpi->summed_weights += weight;
5108 if (cpi->oxcf.number_of_layers > 1)
5112 for (i=cpi->current_layer;
5113 i<cpi->oxcf.number_of_layers; i++)
5115 cpi->frames_in_layer[i]++;
5117 cpi->bytes_in_layer[i] += *size;
5118 cpi->sum_psnr[i] += frame_psnr;
5119 cpi->sum_psnr_p[i] += frame_psnr2;
5120 cpi->total_error2[i] += sq_error;
5121 cpi->total_error2_p[i] += sq_error2;
5122 cpi->sum_ssim[i] += frame_ssim2 * weight;
5123 cpi->sum_weights[i] += weight;
5130 if (cpi->b_calculate_ssimg)
5132 double y, u, v, frame_all;
5133 frame_all = vp8_calc_ssimg(cpi->Source, cm->frame_to_show,
5136 if (cpi->oxcf.number_of_layers > 1)
5140 for (i=cpi->current_layer;
5141 i<cpi->oxcf.number_of_layers; i++)
5143 if (!cpi->b_calculate_psnr)
5144 cpi->frames_in_layer[i]++;
5146 cpi->total_ssimg_y_in_layer[i] += y;
5147 cpi->total_ssimg_u_in_layer[i] += u;
5148 cpi->total_ssimg_v_in_layer[i] += v;
5149 cpi->total_ssimg_all_in_layer[i] += frame_all;
5154 cpi->total_ssimg_y += y;
5155 cpi->total_ssimg_u += u;
5156 cpi->total_ssimg_v += v;
5157 cpi->total_ssimg_all += frame_all;
5166 if (cpi->common.frame_type != 0 && cpi->common.base_qindex == cpi->oxcf.worst_allowed_q)
5168 skiptruecount += cpi->skip_true_count;
5169 skipfalsecount += cpi->skip_false_count;
5177 FILE *f = fopen("skip.stt", "a");
5178 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);
5180 if (cpi->is_src_frame_alt_ref == 1)
5181 fprintf(f, "skipcount: %4d framesize: %d\n", cpi->skip_true_count , *size);
5190 #if CONFIG_RUNTIME_CPU_DETECT
5191 if (cm->cpu_caps & HAS_NEON)
5194 vp8_pop_neon(store_reg);
5198 cpi->common.error.setjmp = 0;
5203 int vp8_get_preview_raw_frame(VP8_COMP *cpi, YV12_BUFFER_CONFIG *dest, vp8_ppflags_t *flags)
5205 if (cpi->common.refresh_alt_ref_frame)
5211 #if CONFIG_MULTITHREAD
5212 if(cpi->b_lpf_running)
5214 sem_wait(&cpi->h_event_end_lpf);
5215 cpi->b_lpf_running = 0;
5220 ret = vp8_post_proc_frame(&cpi->common, dest, flags);
5223 if (cpi->common.frame_to_show)
5225 *dest = *cpi->common.frame_to_show;
5226 dest->y_width = cpi->common.Width;
5227 dest->y_height = cpi->common.Height;
5228 dest->uv_height = cpi->common.Height / 2;
5236 #endif //!CONFIG_POSTPROC
5237 vp8_clear_system_state();
5242 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])
5244 signed char feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS];
5246 if (cpi->common.mb_rows != rows || cpi->common.mb_cols != cols)
5251 disable_segmentation(cpi);
5255 // Set the segmentation Map
5256 set_segmentation_map(cpi, map);
5258 // Activate segmentation.
5259 enable_segmentation(cpi);
5261 // Set up the quant segment data
5262 feature_data[MB_LVL_ALT_Q][0] = delta_q[0];
5263 feature_data[MB_LVL_ALT_Q][1] = delta_q[1];
5264 feature_data[MB_LVL_ALT_Q][2] = delta_q[2];
5265 feature_data[MB_LVL_ALT_Q][3] = delta_q[3];
5267 // Set up the loop segment data s
5268 feature_data[MB_LVL_ALT_LF][0] = delta_lf[0];
5269 feature_data[MB_LVL_ALT_LF][1] = delta_lf[1];
5270 feature_data[MB_LVL_ALT_LF][2] = delta_lf[2];
5271 feature_data[MB_LVL_ALT_LF][3] = delta_lf[3];
5273 cpi->segment_encode_breakout[0] = threshold[0];
5274 cpi->segment_encode_breakout[1] = threshold[1];
5275 cpi->segment_encode_breakout[2] = threshold[2];
5276 cpi->segment_encode_breakout[3] = threshold[3];
5278 // Initialise the feature data structure
5279 // SEGMENT_DELTADATA 0, SEGMENT_ABSDATA 1
5280 set_segment_data(cpi, &feature_data[0][0], SEGMENT_DELTADATA);
5285 int vp8_set_active_map(VP8_COMP *cpi, unsigned char *map, unsigned int rows, unsigned int cols)
5287 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols)
5291 vpx_memcpy(cpi->active_map, map, rows * cols);
5292 cpi->active_map_enabled = 1;
5295 cpi->active_map_enabled = 0;
5301 //cpi->active_map_enabled = 0;
5306 int vp8_set_internal_size(VP8_COMP *cpi, VPX_SCALING horiz_mode, VPX_SCALING vert_mode)
5308 if (horiz_mode <= ONETWO)
5309 cpi->common.horiz_scale = horiz_mode;
5313 if (vert_mode <= ONETWO)
5314 cpi->common.vert_scale = vert_mode;
5323 int vp8_calc_ss_err(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *dest)
5328 unsigned char *src = source->y_buffer;
5329 unsigned char *dst = dest->y_buffer;
5331 // Loop through the Y plane raw and reconstruction data summing (square differences)
5332 for (i = 0; i < source->y_height; i += 16)
5334 for (j = 0; j < source->y_width; j += 16)
5337 Total += vp8_mse16x16(src + j, source->y_stride, dst + j, dest->y_stride, &sse);
5340 src += 16 * source->y_stride;
5341 dst += 16 * dest->y_stride;
5348 int vp8_get_quantizer(VP8_COMP *cpi)
5350 return cpi->common.base_qindex;