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 "vp8/common/onyxc_int.h"
14 #include "vp8/common/systemdependent.h"
16 #include "vp8/common/alloccommon.h"
18 #include "firstpass.h"
20 #include "vpx_scale/vpxscale.h"
21 #include "vp8/common/extend.h"
23 #include "vp8/common/quant_common.h"
24 #include "segmentation.h"
25 #include "vp8/common/g_common.h"
26 #include "vpx_scale/yv12extend.h"
27 #include "vp8/common/postproc.h"
28 #include "vpx_mem/vpx_mem.h"
29 #include "vp8/common/swapyv12buffer.h"
30 #include "vp8/common/threading.h"
31 #include "vpx_ports/vpx_timer.h"
32 #include "temporal_filter.h"
34 #include "vpx_ports/arm.h"
41 #if CONFIG_RUNTIME_CPU_DETECT
42 #define IF_RTCD(x) (x)
43 #define RTCD(x) &cpi->common.rtcd.x
45 #define IF_RTCD(x) NULL
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_dmachine_specific_config(VP8_COMP *cpi);
54 extern void vp8_cmachine_specific_config(VP8_COMP *cpi);
55 extern void vp8_deblock_frame(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *post, int filt_lvl, int low_var_thresh, int flag);
56 extern void print_parms(VP8_CONFIG *ocf, char *filenam);
57 extern unsigned int vp8_get_processor_freq();
58 extern void print_tree_update_probs();
59 extern void vp8cx_create_encoder_threads(VP8_COMP *cpi);
60 extern void vp8cx_remove_encoder_threads(VP8_COMP *cpi);
62 extern void vp8_yv12_copy_frame_func_neon(YV12_BUFFER_CONFIG *src_ybc, YV12_BUFFER_CONFIG *dst_ybc);
63 extern void vp8_yv12_copy_src_frame_func_neon(YV12_BUFFER_CONFIG *src_ybc, YV12_BUFFER_CONFIG *dst_ybc);
66 int vp8_estimate_entropy_savings(VP8_COMP *cpi);
67 int vp8_calc_ss_err(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *dest, const vp8_variance_rtcd_vtable_t *rtcd);
69 extern void vp8_temporal_filter_prepare_c(VP8_COMP *cpi, int distance);
71 static void set_default_lf_deltas(VP8_COMP *cpi);
73 extern const int vp8_gf_interval_table[101];
75 #if CONFIG_INTERNAL_STATS
78 extern double vp8_calc_ssim
80 YV12_BUFFER_CONFIG *source,
81 YV12_BUFFER_CONFIG *dest,
84 const vp8_variance_rtcd_vtable_t *rtcd
88 extern double vp8_calc_ssimg
90 YV12_BUFFER_CONFIG *source,
91 YV12_BUFFER_CONFIG *dest,
95 const vp8_variance_rtcd_vtable_t *rtcd
102 #ifdef OUTPUT_YUV_SRC
113 extern int skip_true_count;
114 extern int skip_false_count;
119 extern int intra_mode_stats[10][10][10];
123 unsigned int frames_at_speed[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
124 unsigned int tot_pm = 0;
125 unsigned int cnt_pm = 0;
126 unsigned int tot_ef = 0;
127 unsigned int cnt_ef = 0;
131 extern unsigned __int64 Sectionbits[50];
132 extern int y_modes[5] ;
133 extern int uv_modes[4] ;
134 extern int b_modes[10] ;
136 extern int inter_y_modes[10] ;
137 extern int inter_uv_modes[4] ;
138 extern unsigned int inter_b_modes[15];
141 extern void (*vp8_short_fdct4x4)(short *input, short *output, int pitch);
142 extern void (*vp8_short_fdct8x4)(short *input, short *output, int pitch);
144 extern const int vp8_bits_per_mb[2][QINDEX_RANGE];
146 extern const int qrounding_factors[129];
147 extern const int qzbin_factors[129];
148 extern void vp8cx_init_quantizer(VP8_COMP *cpi);
149 extern const int vp8cx_base_skip_false_prob[128];
151 // Tables relating active max Q to active min Q
152 static const int kf_low_motion_minq[QINDEX_RANGE] =
154 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
155 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
156 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
157 0,0,0,0,1,1,1,1,1,1,1,1,2,2,2,2,
158 3,3,3,3,3,3,4,4,4,5,5,5,5,5,6,6,
159 6,6,7,7,8,8,8,8,9,9,10,10,10,10,11,11,
160 11,11,12,12,13,13,13,13,14,14,15,15,15,15,16,16,
161 16,16,17,17,18,18,18,18,19,20,20,21,21,22,23,23
163 static const int kf_high_motion_minq[QINDEX_RANGE] =
165 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
166 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
167 1,1,1,1,1,1,1,1,2,2,2,2,3,3,3,3,
168 3,3,3,3,4,4,4,4,5,5,5,5,5,5,6,6,
169 6,6,7,7,8,8,8,8,9,9,10,10,10,10,11,11,
170 11,11,12,12,13,13,13,13,14,14,15,15,15,15,16,16,
171 16,16,17,17,18,18,18,18,19,19,20,20,20,20,21,21,
172 21,21,22,22,23,23,24,25,25,26,26,27,28,28,29,30
174 static const int gf_low_motion_minq[QINDEX_RANGE] =
176 0,0,0,0,1,1,1,1,1,1,1,1,2,2,2,2,
177 3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,
178 7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10,
179 11,11,12,12,13,13,14,14,15,15,16,16,17,17,18,18,
180 19,19,20,20,21,21,22,22,23,23,24,24,25,25,26,26,
181 27,27,28,28,29,29,30,30,31,31,32,32,33,33,34,34,
182 35,35,36,36,37,37,38,38,39,39,40,40,41,41,42,42,
183 43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58
185 static const int gf_mid_motion_minq[QINDEX_RANGE] =
187 0,0,0,0,1,1,1,1,1,1,2,2,3,3,3,4,
188 4,4,5,5,5,6,6,6,7,7,7,8,8,8,9,9,
189 9,10,10,10,10,11,11,11,12,12,12,12,13,13,13,14,
190 14,14,15,15,16,16,17,17,18,18,19,19,20,20,21,21,
191 22,22,23,23,24,24,25,25,26,26,27,27,28,28,29,29,
192 30,30,31,31,32,32,33,33,34,34,35,35,36,36,37,37,
193 38,39,39,40,40,41,41,42,42,43,43,44,45,46,47,48,
194 49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64
196 static const int gf_high_motion_minq[QINDEX_RANGE] =
198 0,0,0,0,1,1,1,1,1,2,2,2,3,3,3,4,
199 4,4,5,5,5,6,6,6,7,7,7,8,8,8,9,9,
200 9,10,10,10,11,11,12,12,13,13,14,14,15,15,16,16,
201 17,17,18,18,19,19,20,20,21,21,22,22,23,23,24,24,
202 25,25,26,26,27,27,28,28,29,29,30,30,31,31,32,32,
203 33,33,34,34,35,35,36,36,37,37,38,38,39,39,40,40,
204 41,41,42,42,43,44,45,46,47,48,49,50,51,52,53,54,
205 55,56,57,58,59,60,62,64,66,68,70,72,74,76,78,80
207 static const int inter_minq[QINDEX_RANGE] =
209 0,0,1,1,2,3,3,4,4,5,6,6,7,8,8,9,
210 9,10,11,11,12,13,13,14,15,15,16,17,17,18,19,20,
211 20,21,22,22,23,24,24,25,26,27,27,28,29,30,30,31,
212 32,33,33,34,35,36,36,37,38,39,39,40,41,42,42,43,
213 44,45,46,46,47,48,49,50,50,51,52,53,54,55,55,56,
214 57,58,59,60,60,61,62,63,64,65,66,67,67,68,69,70,
215 71,72,73,74,75,75,76,77,78,79,80,81,82,83,84,85,
216 86,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100
219 void vp8_initialize()
221 static int init_done = 0;
225 vp8_scale_machine_specific_config();
226 vp8_initialize_common();
227 //vp8_dmachine_specific_config();
228 vp8_tokenize_initialize();
233 #ifdef PACKET_TESTING
234 extern FILE *vpxlogc;
237 static void setup_features(VP8_COMP *cpi)
239 // Set up default state for MB feature flags
240 cpi->mb.e_mbd.segmentation_enabled = 0;
241 cpi->mb.e_mbd.update_mb_segmentation_map = 0;
242 cpi->mb.e_mbd.update_mb_segmentation_data = 0;
243 vpx_memset(cpi->mb.e_mbd.mb_segment_tree_probs, 255, sizeof(cpi->mb.e_mbd.mb_segment_tree_probs));
244 vpx_memset(cpi->mb.e_mbd.segment_feature_data, 0, sizeof(cpi->mb.e_mbd.segment_feature_data));
246 cpi->mb.e_mbd.mode_ref_lf_delta_enabled = 0;
247 cpi->mb.e_mbd.mode_ref_lf_delta_update = 0;
248 vpx_memset(cpi->mb.e_mbd.ref_lf_deltas, 0, sizeof(cpi->mb.e_mbd.ref_lf_deltas));
249 vpx_memset(cpi->mb.e_mbd.mode_lf_deltas, 0, sizeof(cpi->mb.e_mbd.mode_lf_deltas));
250 vpx_memset(cpi->mb.e_mbd.last_ref_lf_deltas, 0, sizeof(cpi->mb.e_mbd.ref_lf_deltas));
251 vpx_memset(cpi->mb.e_mbd.last_mode_lf_deltas, 0, sizeof(cpi->mb.e_mbd.mode_lf_deltas));
253 set_default_lf_deltas(cpi);
258 static void dealloc_compressor_data(VP8_COMP *cpi)
260 vpx_free(cpi->tplist);
263 // Delete last frame MV storage buffers
267 vpx_free(cpi->lf_ref_frame_sign_bias);
268 cpi->lf_ref_frame_sign_bias = 0;
270 vpx_free(cpi->lf_ref_frame);
271 cpi->lf_ref_frame = 0;
273 // Delete sementation map
274 vpx_free(cpi->segmentation_map);
275 cpi->segmentation_map = 0;
277 vpx_free(cpi->active_map);
280 vp8_de_alloc_frame_buffers(&cpi->common);
282 vp8_yv12_de_alloc_frame_buffer(&cpi->last_frame_uf);
283 vp8_yv12_de_alloc_frame_buffer(&cpi->scaled_source);
284 #if VP8_TEMPORAL_ALT_REF
285 vp8_yv12_de_alloc_frame_buffer(&cpi->alt_ref_buffer);
287 vp8_lookahead_destroy(cpi->lookahead);
292 // Structure used to monitor GF usage
293 vpx_free(cpi->gf_active_flags);
294 cpi->gf_active_flags = 0;
296 // Activity mask based per mb zbin adjustments
297 vpx_free(cpi->mb_activity_map);
298 cpi->mb_activity_map = 0;
299 vpx_free(cpi->mb_norm_activity_map);
300 cpi->mb_norm_activity_map = 0;
302 vpx_free(cpi->mb.pip);
305 #if !(CONFIG_REALTIME_ONLY)
306 vpx_free(cpi->twopass.total_stats);
307 cpi->twopass.total_stats = 0;
309 vpx_free(cpi->twopass.this_frame_stats);
310 cpi->twopass.this_frame_stats = 0;
314 static void enable_segmentation(VP8_PTR ptr)
316 VP8_COMP *cpi = (VP8_COMP *)(ptr);
318 // Set the appropriate feature bit
319 cpi->mb.e_mbd.segmentation_enabled = 1;
320 cpi->mb.e_mbd.update_mb_segmentation_map = 1;
321 cpi->mb.e_mbd.update_mb_segmentation_data = 1;
323 static void disable_segmentation(VP8_PTR ptr)
325 VP8_COMP *cpi = (VP8_COMP *)(ptr);
327 // Clear the appropriate feature bit
328 cpi->mb.e_mbd.segmentation_enabled = 0;
331 // Valid values for a segment are 0 to 3
332 // Segmentation map is arrange as [Rows][Columns]
333 static void set_segmentation_map(VP8_PTR ptr, unsigned char *segmentation_map)
335 VP8_COMP *cpi = (VP8_COMP *)(ptr);
337 // Copy in the new segmentation map
338 vpx_memcpy(cpi->segmentation_map, segmentation_map, (cpi->common.mb_rows * cpi->common.mb_cols));
340 // Signal that the map should be updated.
341 cpi->mb.e_mbd.update_mb_segmentation_map = 1;
342 cpi->mb.e_mbd.update_mb_segmentation_data = 1;
345 // The values given for each segment can be either deltas (from the default value chosen for the frame) or absolute values.
347 // Valid range for abs values is (0-127 for MB_LVL_ALT_Q) , (0-63 for SEGMENT_ALT_LF)
348 // Valid range for delta values are (+/-127 for MB_LVL_ALT_Q) , (+/-63 for SEGMENT_ALT_LF)
350 // abs_delta = SEGMENT_DELTADATA (deltas) abs_delta = SEGMENT_ABSDATA (use the absolute values given).
353 static void set_segment_data(VP8_PTR ptr, signed char *feature_data, unsigned char abs_delta)
355 VP8_COMP *cpi = (VP8_COMP *)(ptr);
357 cpi->mb.e_mbd.mb_segement_abs_delta = abs_delta;
358 vpx_memcpy(cpi->segment_feature_data, feature_data, sizeof(cpi->segment_feature_data));
362 static void segmentation_test_function(VP8_PTR ptr)
364 VP8_COMP *cpi = (VP8_COMP *)(ptr);
366 unsigned char *seg_map;
367 signed char feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS];
369 // Create a temporary map for segmentation data.
370 CHECK_MEM_ERROR(seg_map, vpx_calloc(cpi->common.mb_rows * cpi->common.mb_cols, 1));
372 // MB loop to set local segmentation map
373 /*for ( i = 0; i < cpi->common.mb_rows; i++ )
375 for ( j = 0; j < cpi->common.mb_cols; j++ )
377 //seg_map[(i*cpi->common.mb_cols) + j] = (j % 2) + ((i%2)* 2);
378 //if ( j < cpi->common.mb_cols/2 )
380 // Segment 1 around the edge else 0
381 if ( (i == 0) || (j == 0) || (i == (cpi->common.mb_rows-1)) || (j == (cpi->common.mb_cols-1)) )
382 seg_map[(i*cpi->common.mb_cols) + j] = 1;
383 //else if ( (i < 2) || (j < 2) || (i > (cpi->common.mb_rows-3)) || (j > (cpi->common.mb_cols-3)) )
384 // seg_map[(i*cpi->common.mb_cols) + j] = 2;
385 //else if ( (i < 5) || (j < 5) || (i > (cpi->common.mb_rows-6)) || (j > (cpi->common.mb_cols-6)) )
386 // seg_map[(i*cpi->common.mb_cols) + j] = 3;
388 seg_map[(i*cpi->common.mb_cols) + j] = 0;
392 // Set the segmentation Map
393 set_segmentation_map(ptr, seg_map);
395 // Activate segmentation.
396 enable_segmentation(ptr);
398 // Set up the quant segment data
399 feature_data[MB_LVL_ALT_Q][0] = 0;
400 feature_data[MB_LVL_ALT_Q][1] = 4;
401 feature_data[MB_LVL_ALT_Q][2] = 0;
402 feature_data[MB_LVL_ALT_Q][3] = 0;
403 // Set up the loop segment data
404 feature_data[MB_LVL_ALT_LF][0] = 0;
405 feature_data[MB_LVL_ALT_LF][1] = 0;
406 feature_data[MB_LVL_ALT_LF][2] = 0;
407 feature_data[MB_LVL_ALT_LF][3] = 0;
409 // Initialise the feature data structure
410 // SEGMENT_DELTADATA 0, SEGMENT_ABSDATA 1
411 set_segment_data(ptr, &feature_data[0][0], SEGMENT_DELTADATA);
413 // Delete sementation map
420 // A simple function to cyclically refresh the background at a lower Q
421 static void cyclic_background_refresh(VP8_COMP *cpi, int Q, int lf_adjustment)
423 unsigned char *seg_map;
424 signed char feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS];
426 int block_count = cpi->cyclic_refresh_mode_max_mbs_perframe;
427 int mbs_in_frame = cpi->common.mb_rows * cpi->common.mb_cols;
429 // Create a temporary map for segmentation data.
430 CHECK_MEM_ERROR(seg_map, vpx_calloc(cpi->common.mb_rows * cpi->common.mb_cols, 1));
432 cpi->cyclic_refresh_q = Q;
434 for (i = Q; i > 0; i--)
436 if (vp8_bits_per_mb[cpi->common.frame_type][i] >= ((vp8_bits_per_mb[cpi->common.frame_type][Q]*(Q + 128)) / 64))
437 //if ( vp8_bits_per_mb[cpi->common.frame_type][i] >= ((vp8_bits_per_mb[cpi->common.frame_type][Q]*((2*Q)+96))/64) )
443 cpi->cyclic_refresh_q = i;
445 // Only update for inter frames
446 if (cpi->common.frame_type != KEY_FRAME)
448 // Cycle through the macro_block rows
449 // MB loop to set local segmentation map
450 for (i = cpi->cyclic_refresh_mode_index; i < mbs_in_frame; i++)
452 // If the MB is as a candidate for clean up then mark it for possible boost/refresh (segment 1)
453 // The segment id may get reset to 0 later if the MB gets coded anything other than last frame 0,0
454 // as only (last frame 0,0) MBs are eligable for refresh : that is to say Mbs likely to be background blocks.
455 if (cpi->cyclic_refresh_map[i] == 0)
463 // Skip blocks that have been refreshed recently anyway.
464 if (cpi->cyclic_refresh_map[i] < 0)
465 //cpi->cyclic_refresh_map[i] = cpi->cyclic_refresh_map[i] / 16;
466 cpi->cyclic_refresh_map[i]++;
477 // If we have gone through the frame reset to the start
478 cpi->cyclic_refresh_mode_index = i;
480 if (cpi->cyclic_refresh_mode_index >= mbs_in_frame)
481 cpi->cyclic_refresh_mode_index = 0;
484 // Set the segmentation Map
485 set_segmentation_map((VP8_PTR)cpi, seg_map);
487 // Activate segmentation.
488 enable_segmentation((VP8_PTR)cpi);
490 // Set up the quant segment data
491 feature_data[MB_LVL_ALT_Q][0] = 0;
492 feature_data[MB_LVL_ALT_Q][1] = (cpi->cyclic_refresh_q - Q);
493 feature_data[MB_LVL_ALT_Q][2] = 0;
494 feature_data[MB_LVL_ALT_Q][3] = 0;
496 // Set up the loop segment data
497 feature_data[MB_LVL_ALT_LF][0] = 0;
498 feature_data[MB_LVL_ALT_LF][1] = lf_adjustment;
499 feature_data[MB_LVL_ALT_LF][2] = 0;
500 feature_data[MB_LVL_ALT_LF][3] = 0;
502 // Initialise the feature data structure
503 // SEGMENT_DELTADATA 0, SEGMENT_ABSDATA 1
504 set_segment_data((VP8_PTR)cpi, &feature_data[0][0], SEGMENT_DELTADATA);
506 // Delete sementation map
513 static void set_default_lf_deltas(VP8_COMP *cpi)
515 cpi->mb.e_mbd.mode_ref_lf_delta_enabled = 1;
516 cpi->mb.e_mbd.mode_ref_lf_delta_update = 1;
518 vpx_memset(cpi->mb.e_mbd.ref_lf_deltas, 0, sizeof(cpi->mb.e_mbd.ref_lf_deltas));
519 vpx_memset(cpi->mb.e_mbd.mode_lf_deltas, 0, sizeof(cpi->mb.e_mbd.mode_lf_deltas));
521 // Test of ref frame deltas
522 cpi->mb.e_mbd.ref_lf_deltas[INTRA_FRAME] = 2;
523 cpi->mb.e_mbd.ref_lf_deltas[LAST_FRAME] = 0;
524 cpi->mb.e_mbd.ref_lf_deltas[GOLDEN_FRAME] = -2;
525 cpi->mb.e_mbd.ref_lf_deltas[ALTREF_FRAME] = -2;
527 cpi->mb.e_mbd.mode_lf_deltas[0] = 4; // BPRED
528 cpi->mb.e_mbd.mode_lf_deltas[1] = -2; // Zero
529 cpi->mb.e_mbd.mode_lf_deltas[2] = 2; // New mv
530 cpi->mb.e_mbd.mode_lf_deltas[3] = 4; // Split mv
533 void vp8_set_speed_features(VP8_COMP *cpi)
535 SPEED_FEATURES *sf = &cpi->sf;
536 int Mode = cpi->compressor_speed;
537 int Speed = cpi->Speed;
539 VP8_COMMON *cm = &cpi->common;
540 int last_improved_quant = sf->improved_quant;
542 // Initialise default mode frequency sampling variables
543 for (i = 0; i < MAX_MODES; i ++)
545 cpi->mode_check_freq[i] = 0;
546 cpi->mode_test_hit_counts[i] = 0;
547 cpi->mode_chosen_counts[i] = 0;
550 cpi->mbs_tested_so_far = 0;
552 // best quality defaults
554 sf->search_method = NSTEP;
555 sf->improved_quant = 1;
556 sf->improved_dct = 1;
559 sf->quarter_pixel_search = 1;
560 sf->half_pixel_search = 1;
561 sf->iterative_sub_pixel = 1;
562 sf->optimize_coefficients = 1;
563 sf->use_fastquant_for_pick = 0;
564 sf->no_skip_block4x4_search = 1;
567 sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
568 sf->improved_mv_pred = 1;
570 // default thresholds to 0
571 for (i = 0; i < MAX_MODES; i++)
572 sf->thresh_mult[i] = 0;
576 #if !(CONFIG_REALTIME_ONLY)
577 case 0: // best quality mode
578 sf->thresh_mult[THR_ZEROMV ] = 0;
579 sf->thresh_mult[THR_ZEROG ] = 0;
580 sf->thresh_mult[THR_ZEROA ] = 0;
581 sf->thresh_mult[THR_NEARESTMV] = 0;
582 sf->thresh_mult[THR_NEARESTG ] = 0;
583 sf->thresh_mult[THR_NEARESTA ] = 0;
584 sf->thresh_mult[THR_NEARMV ] = 0;
585 sf->thresh_mult[THR_NEARG ] = 0;
586 sf->thresh_mult[THR_NEARA ] = 0;
588 sf->thresh_mult[THR_DC ] = 0;
590 sf->thresh_mult[THR_V_PRED ] = 1000;
591 sf->thresh_mult[THR_H_PRED ] = 1000;
592 sf->thresh_mult[THR_B_PRED ] = 2000;
593 sf->thresh_mult[THR_TM ] = 1000;
595 sf->thresh_mult[THR_NEWMV ] = 1000;
596 sf->thresh_mult[THR_NEWG ] = 1000;
597 sf->thresh_mult[THR_NEWA ] = 1000;
599 sf->thresh_mult[THR_SPLITMV ] = 2500;
600 sf->thresh_mult[THR_SPLITG ] = 5000;
601 sf->thresh_mult[THR_SPLITA ] = 5000;
605 sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
609 sf->thresh_mult[THR_NEARESTMV] = 0;
610 sf->thresh_mult[THR_ZEROMV ] = 0;
611 sf->thresh_mult[THR_DC ] = 0;
612 sf->thresh_mult[THR_NEARMV ] = 0;
613 sf->thresh_mult[THR_V_PRED ] = 1000;
614 sf->thresh_mult[THR_H_PRED ] = 1000;
615 sf->thresh_mult[THR_B_PRED ] = 2500;
616 sf->thresh_mult[THR_TM ] = 1000;
618 sf->thresh_mult[THR_NEARESTG ] = 1000;
619 sf->thresh_mult[THR_NEARESTA ] = 1000;
621 sf->thresh_mult[THR_ZEROG ] = 1000;
622 sf->thresh_mult[THR_ZEROA ] = 1000;
623 sf->thresh_mult[THR_NEARG ] = 1000;
624 sf->thresh_mult[THR_NEARA ] = 1000;
627 sf->thresh_mult[THR_ZEROMV ] = 0;
628 sf->thresh_mult[THR_ZEROG ] = 0;
629 sf->thresh_mult[THR_ZEROA ] = 0;
630 sf->thresh_mult[THR_NEARESTMV] = 0;
631 sf->thresh_mult[THR_NEARESTG ] = 0;
632 sf->thresh_mult[THR_NEARESTA ] = 0;
633 sf->thresh_mult[THR_NEARMV ] = 0;
634 sf->thresh_mult[THR_NEARG ] = 0;
635 sf->thresh_mult[THR_NEARA ] = 0;
637 // sf->thresh_mult[THR_DC ] = 0;
639 // sf->thresh_mult[THR_V_PRED ] = 1000;
640 // sf->thresh_mult[THR_H_PRED ] = 1000;
641 // sf->thresh_mult[THR_B_PRED ] = 2000;
642 // sf->thresh_mult[THR_TM ] = 1000;
644 sf->thresh_mult[THR_NEWMV ] = 1000;
645 sf->thresh_mult[THR_NEWG ] = 1000;
646 sf->thresh_mult[THR_NEWA ] = 1000;
648 sf->thresh_mult[THR_SPLITMV ] = 1700;
649 sf->thresh_mult[THR_SPLITG ] = 4500;
650 sf->thresh_mult[THR_SPLITA ] = 4500;
652 sf->thresh_mult[THR_NEWMV ] = 1500;
653 sf->thresh_mult[THR_NEWG ] = 1500;
654 sf->thresh_mult[THR_NEWA ] = 1500;
656 sf->thresh_mult[THR_SPLITMV ] = 5000;
657 sf->thresh_mult[THR_SPLITG ] = 10000;
658 sf->thresh_mult[THR_SPLITA ] = 10000;
663 /* Disable coefficient optimization above speed 0 */
664 sf->optimize_coefficients = 0;
665 sf->use_fastquant_for_pick = 1;
666 sf->no_skip_block4x4_search = 0;
670 cpi->mode_check_freq[THR_SPLITG] = 2;
671 cpi->mode_check_freq[THR_SPLITA] = 2;
672 cpi->mode_check_freq[THR_SPLITMV] = 0;
677 cpi->mode_check_freq[THR_SPLITG] = 4;
678 cpi->mode_check_freq[THR_SPLITA] = 4;
679 cpi->mode_check_freq[THR_SPLITMV] = 2;
681 sf->thresh_mult[THR_TM ] = 1500;
682 sf->thresh_mult[THR_V_PRED ] = 1500;
683 sf->thresh_mult[THR_H_PRED ] = 1500;
684 sf->thresh_mult[THR_B_PRED ] = 5000;
686 if (cpi->ref_frame_flags & VP8_LAST_FLAG)
688 sf->thresh_mult[THR_NEWMV ] = 2000;
689 sf->thresh_mult[THR_SPLITMV ] = 10000;
692 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
694 sf->thresh_mult[THR_NEARESTG ] = 1500;
695 sf->thresh_mult[THR_ZEROG ] = 1500;
696 sf->thresh_mult[THR_NEARG ] = 1500;
697 sf->thresh_mult[THR_NEWG ] = 2000;
698 sf->thresh_mult[THR_SPLITG ] = 20000;
701 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
703 sf->thresh_mult[THR_NEARESTA ] = 1500;
704 sf->thresh_mult[THR_ZEROA ] = 1500;
705 sf->thresh_mult[THR_NEARA ] = 1500;
706 sf->thresh_mult[THR_NEWA ] = 2000;
707 sf->thresh_mult[THR_SPLITA ] = 20000;
713 cpi->mode_check_freq[THR_SPLITG] = 15;
714 cpi->mode_check_freq[THR_SPLITA] = 15;
715 cpi->mode_check_freq[THR_SPLITMV] = 7;
717 sf->thresh_mult[THR_TM ] = 2000;
718 sf->thresh_mult[THR_V_PRED ] = 2000;
719 sf->thresh_mult[THR_H_PRED ] = 2000;
720 sf->thresh_mult[THR_B_PRED ] = 7500;
722 if (cpi->ref_frame_flags & VP8_LAST_FLAG)
724 sf->thresh_mult[THR_NEWMV ] = 2000;
725 sf->thresh_mult[THR_SPLITMV ] = 25000;
728 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
730 sf->thresh_mult[THR_NEARESTG ] = 2000;
731 sf->thresh_mult[THR_ZEROG ] = 2000;
732 sf->thresh_mult[THR_NEARG ] = 2000;
733 sf->thresh_mult[THR_NEWG ] = 2500;
734 sf->thresh_mult[THR_SPLITG ] = 50000;
737 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
739 sf->thresh_mult[THR_NEARESTA ] = 2000;
740 sf->thresh_mult[THR_ZEROA ] = 2000;
741 sf->thresh_mult[THR_NEARA ] = 2000;
742 sf->thresh_mult[THR_NEWA ] = 2500;
743 sf->thresh_mult[THR_SPLITA ] = 50000;
746 sf->improved_quant = 0;
747 sf->improved_dct = 0;
749 // Only do recode loop on key frames, golden frames and
757 sf->thresh_mult[THR_SPLITA ] = INT_MAX;
758 sf->thresh_mult[THR_SPLITG ] = INT_MAX;
759 sf->thresh_mult[THR_SPLITMV ] = INT_MAX;
761 cpi->mode_check_freq[THR_V_PRED] = 0;
762 cpi->mode_check_freq[THR_H_PRED] = 0;
763 cpi->mode_check_freq[THR_B_PRED] = 0;
764 cpi->mode_check_freq[THR_NEARG] = 0;
765 cpi->mode_check_freq[THR_NEWG] = 0;
766 cpi->mode_check_freq[THR_NEARA] = 0;
767 cpi->mode_check_freq[THR_NEWA] = 0;
770 sf->recode_loop = 0; // recode loop off
771 sf->RD = 0; // Turn rd off
777 sf->auto_filter = 0; // Faster selection of loop filter
779 cpi->mode_check_freq[THR_V_PRED] = 2;
780 cpi->mode_check_freq[THR_H_PRED] = 2;
781 cpi->mode_check_freq[THR_B_PRED] = 2;
783 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
785 cpi->mode_check_freq[THR_NEARG] = 2;
786 cpi->mode_check_freq[THR_NEWG] = 4;
789 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
791 cpi->mode_check_freq[THR_NEARA] = 2;
792 cpi->mode_check_freq[THR_NEWA] = 4;
795 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
797 sf->thresh_mult[THR_NEARESTG ] = 2000;
798 sf->thresh_mult[THR_ZEROG ] = 2000;
799 sf->thresh_mult[THR_NEARG ] = 2000;
800 sf->thresh_mult[THR_NEWG ] = 4000;
803 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
805 sf->thresh_mult[THR_NEARESTA ] = 2000;
806 sf->thresh_mult[THR_ZEROA ] = 2000;
807 sf->thresh_mult[THR_NEARA ] = 2000;
808 sf->thresh_mult[THR_NEWA ] = 4000;
815 sf->optimize_coefficients = 0;
818 sf->iterative_sub_pixel = 1;
819 sf->thresh_mult[THR_NEARESTMV] = 0;
820 sf->thresh_mult[THR_ZEROMV ] = 0;
821 sf->thresh_mult[THR_DC ] = 0;
822 sf->thresh_mult[THR_TM ] = 0;
823 sf->thresh_mult[THR_NEARMV ] = 0;
824 sf->thresh_mult[THR_V_PRED ] = 1000;
825 sf->thresh_mult[THR_H_PRED ] = 1000;
826 sf->thresh_mult[THR_B_PRED ] = 2500;
827 sf->thresh_mult[THR_NEARESTG ] = 1000;
828 sf->thresh_mult[THR_ZEROG ] = 1000;
829 sf->thresh_mult[THR_NEARG ] = 1000;
830 sf->thresh_mult[THR_NEARESTA ] = 1000;
831 sf->thresh_mult[THR_ZEROA ] = 1000;
832 sf->thresh_mult[THR_NEARA ] = 1000;
833 sf->thresh_mult[THR_NEWMV ] = 2000;
834 sf->thresh_mult[THR_NEWG ] = 2000;
835 sf->thresh_mult[THR_NEWA ] = 2000;
836 sf->thresh_mult[THR_SPLITMV ] = 5000;
837 sf->thresh_mult[THR_SPLITG ] = 10000;
838 sf->thresh_mult[THR_SPLITA ] = 10000;
839 sf->search_method = NSTEP;
843 cpi->mode_check_freq[THR_SPLITG] = 4;
844 cpi->mode_check_freq[THR_SPLITA] = 4;
845 cpi->mode_check_freq[THR_SPLITMV] = 2;
847 sf->thresh_mult[THR_DC ] = 0;
848 sf->thresh_mult[THR_TM ] = 1000;
849 sf->thresh_mult[THR_V_PRED ] = 2000;
850 sf->thresh_mult[THR_H_PRED ] = 2000;
851 sf->thresh_mult[THR_B_PRED ] = 5000;
853 if (cpi->ref_frame_flags & VP8_LAST_FLAG)
855 sf->thresh_mult[THR_NEARESTMV] = 0;
856 sf->thresh_mult[THR_ZEROMV ] = 0;
857 sf->thresh_mult[THR_NEARMV ] = 0;
858 sf->thresh_mult[THR_NEWMV ] = 2000;
859 sf->thresh_mult[THR_SPLITMV ] = 10000;
862 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
864 sf->thresh_mult[THR_NEARESTG ] = 1000;
865 sf->thresh_mult[THR_ZEROG ] = 1000;
866 sf->thresh_mult[THR_NEARG ] = 1000;
867 sf->thresh_mult[THR_NEWG ] = 2000;
868 sf->thresh_mult[THR_SPLITG ] = 20000;
871 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
873 sf->thresh_mult[THR_NEARESTA ] = 1000;
874 sf->thresh_mult[THR_ZEROA ] = 1000;
875 sf->thresh_mult[THR_NEARA ] = 1000;
876 sf->thresh_mult[THR_NEWA ] = 2000;
877 sf->thresh_mult[THR_SPLITA ] = 20000;
880 sf->improved_quant = 0;
881 sf->improved_dct = 0;
886 cpi->mode_check_freq[THR_SPLITMV] = 7;
887 cpi->mode_check_freq[THR_SPLITG] = 15;
888 cpi->mode_check_freq[THR_SPLITA] = 15;
890 sf->thresh_mult[THR_TM ] = 2000;
891 sf->thresh_mult[THR_V_PRED ] = 2000;
892 sf->thresh_mult[THR_H_PRED ] = 2000;
893 sf->thresh_mult[THR_B_PRED ] = 5000;
895 if (cpi->ref_frame_flags & VP8_LAST_FLAG)
897 sf->thresh_mult[THR_NEWMV ] = 2000;
898 sf->thresh_mult[THR_SPLITMV ] = 25000;
901 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
903 sf->thresh_mult[THR_NEARESTG ] = 2000;
904 sf->thresh_mult[THR_ZEROG ] = 2000;
905 sf->thresh_mult[THR_NEARG ] = 2000;
906 sf->thresh_mult[THR_NEWG ] = 2500;
907 sf->thresh_mult[THR_SPLITG ] = 50000;
910 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
912 sf->thresh_mult[THR_NEARESTA ] = 2000;
913 sf->thresh_mult[THR_ZEROA ] = 2000;
914 sf->thresh_mult[THR_NEARA ] = 2000;
915 sf->thresh_mult[THR_NEWA ] = 2500;
916 sf->thresh_mult[THR_SPLITA ] = 50000;
923 sf->auto_filter = 0; // Faster selection of loop filter
925 cpi->mode_check_freq[THR_V_PRED] = 2;
926 cpi->mode_check_freq[THR_H_PRED] = 2;
927 cpi->mode_check_freq[THR_B_PRED] = 2;
929 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
931 cpi->mode_check_freq[THR_NEARG] = 2;
932 cpi->mode_check_freq[THR_NEWG] = 4;
935 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
937 cpi->mode_check_freq[THR_NEARA] = 2;
938 cpi->mode_check_freq[THR_NEWA] = 4;
941 sf->thresh_mult[THR_SPLITMV ] = INT_MAX;
942 sf->thresh_mult[THR_SPLITG ] = INT_MAX;
943 sf->thresh_mult[THR_SPLITA ] = INT_MAX;
956 sf->auto_filter = 0; // Faster selection of loop filter
958 sf->search_method = HEX;
959 //sf->search_method = DIAMOND;
961 sf->iterative_sub_pixel = 0;
963 cpi->mode_check_freq[THR_V_PRED] = 4;
964 cpi->mode_check_freq[THR_H_PRED] = 4;
965 cpi->mode_check_freq[THR_B_PRED] = 4;
967 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
969 cpi->mode_check_freq[THR_NEARG] = 2;
970 cpi->mode_check_freq[THR_NEWG] = 4;
973 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
975 cpi->mode_check_freq[THR_NEARA] = 2;
976 cpi->mode_check_freq[THR_NEWA] = 4;
979 sf->thresh_mult[THR_TM ] = 2000;
980 sf->thresh_mult[THR_B_PRED ] = 5000;
982 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
984 sf->thresh_mult[THR_NEARESTG ] = 2000;
985 sf->thresh_mult[THR_ZEROG ] = 2000;
986 sf->thresh_mult[THR_NEARG ] = 2000;
987 sf->thresh_mult[THR_NEWG ] = 4000;
990 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
992 sf->thresh_mult[THR_NEARESTA ] = 2000;
993 sf->thresh_mult[THR_ZEROA ] = 2000;
994 sf->thresh_mult[THR_NEARA ] = 2000;
995 sf->thresh_mult[THR_NEWA ] = 4000;
1001 // Disable split MB intra prediction mode
1002 sf->thresh_mult[THR_B_PRED] = INT_MAX;
1007 unsigned int i, sum = 0;
1008 unsigned int total_mbs = cm->MBs;
1014 if (cpi->oxcf.encode_breakout > 2000)
1015 min = cpi->oxcf.encode_breakout;
1019 for (i = 0; i < min; i++)
1021 sum += cpi->error_bins[i];
1027 // i starts from 2 to make sure thresh started from 2048
1028 for (; i < 1024; i++)
1030 sum += cpi->error_bins[i];
1032 if (10 * sum >= (unsigned int)(cpi->Speed - 6)*(total_mbs - total_skip))
1042 if (cpi->ref_frame_flags & VP8_LAST_FLAG)
1044 sf->thresh_mult[THR_NEWMV] = thresh;
1045 sf->thresh_mult[THR_NEARESTMV ] = thresh >> 1;
1046 sf->thresh_mult[THR_NEARMV ] = thresh >> 1;
1049 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
1051 sf->thresh_mult[THR_NEWG] = thresh << 1;
1052 sf->thresh_mult[THR_NEARESTG ] = thresh;
1053 sf->thresh_mult[THR_NEARG ] = thresh;
1056 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
1058 sf->thresh_mult[THR_NEWA] = thresh << 1;
1059 sf->thresh_mult[THR_NEARESTA ] = thresh;
1060 sf->thresh_mult[THR_NEARA ] = thresh;
1063 // Disable other intra prediction modes
1064 sf->thresh_mult[THR_TM] = INT_MAX;
1065 sf->thresh_mult[THR_V_PRED] = INT_MAX;
1066 sf->thresh_mult[THR_H_PRED] = INT_MAX;
1068 sf->improved_mv_pred = 0;
1073 sf->quarter_pixel_search = 0;
1078 int Tmp = cpi->Speed - 8;
1083 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
1085 cpi->mode_check_freq[THR_ZEROG] = 1 << (Tmp - 1);
1086 cpi->mode_check_freq[THR_NEARESTG] = 1 << (Tmp - 1);
1087 cpi->mode_check_freq[THR_NEARG] = 1 << Tmp;
1088 cpi->mode_check_freq[THR_NEWG] = 1 << (Tmp + 1);
1091 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
1093 cpi->mode_check_freq[THR_ZEROA] = 1 << (Tmp - 1);
1094 cpi->mode_check_freq[THR_NEARESTA] = 1 << (Tmp - 1);
1095 cpi->mode_check_freq[THR_NEARA] = 1 << Tmp;
1096 cpi->mode_check_freq[THR_NEWA] = 1 << (Tmp + 1);
1099 cpi->mode_check_freq[THR_NEWMV] = 1 << (Tmp - 1);
1102 cm->filter_type = NORMAL_LOOPFILTER;
1105 cm->filter_type = SIMPLE_LOOPFILTER;
1109 sf->half_pixel_search = 0; // This has a big hit on quality. Last resort
1112 vpx_memset(cpi->error_bins, 0, sizeof(cpi->error_bins));
1116 /* disable frame modes if flags not set */
1117 if (!(cpi->ref_frame_flags & VP8_LAST_FLAG))
1119 sf->thresh_mult[THR_NEWMV ] = INT_MAX;
1120 sf->thresh_mult[THR_NEARESTMV] = INT_MAX;
1121 sf->thresh_mult[THR_ZEROMV ] = INT_MAX;
1122 sf->thresh_mult[THR_NEARMV ] = INT_MAX;
1123 sf->thresh_mult[THR_SPLITMV ] = INT_MAX;
1126 if (!(cpi->ref_frame_flags & VP8_GOLD_FLAG))
1128 sf->thresh_mult[THR_NEARESTG ] = INT_MAX;
1129 sf->thresh_mult[THR_ZEROG ] = INT_MAX;
1130 sf->thresh_mult[THR_NEARG ] = INT_MAX;
1131 sf->thresh_mult[THR_NEWG ] = INT_MAX;
1132 sf->thresh_mult[THR_SPLITG ] = INT_MAX;
1135 if (!(cpi->ref_frame_flags & VP8_ALT_FLAG))
1137 sf->thresh_mult[THR_NEARESTA ] = INT_MAX;
1138 sf->thresh_mult[THR_ZEROA ] = INT_MAX;
1139 sf->thresh_mult[THR_NEARA ] = INT_MAX;
1140 sf->thresh_mult[THR_NEWA ] = INT_MAX;
1141 sf->thresh_mult[THR_SPLITA ] = INT_MAX;
1145 // Slow quant, dct and trellis not worthwhile for first pass
1146 // so make sure they are always turned off.
1147 if ( cpi->pass == 1 )
1149 sf->improved_quant = 0;
1150 sf->optimize_coefficients = 0;
1151 sf->improved_dct = 0;
1154 if (cpi->sf.search_method == NSTEP)
1156 vp8_init3smotion_compensation(&cpi->mb, cm->yv12_fb[cm->lst_fb_idx].y_stride);
1158 else if (cpi->sf.search_method == DIAMOND)
1160 vp8_init_dsmotion_compensation(&cpi->mb, cm->yv12_fb[cm->lst_fb_idx].y_stride);
1163 if (cpi->sf.improved_dct)
1165 cpi->mb.vp8_short_fdct8x4 = FDCT_INVOKE(&cpi->rtcd.fdct, short8x4);
1166 cpi->mb.vp8_short_fdct4x4 = FDCT_INVOKE(&cpi->rtcd.fdct, short4x4);
1170 cpi->mb.vp8_short_fdct8x4 = FDCT_INVOKE(&cpi->rtcd.fdct, fast8x4);
1171 cpi->mb.vp8_short_fdct4x4 = FDCT_INVOKE(&cpi->rtcd.fdct, fast4x4);
1174 cpi->mb.short_walsh4x4 = FDCT_INVOKE(&cpi->rtcd.fdct, walsh_short4x4);
1176 if (cpi->sf.improved_quant)
1178 cpi->mb.quantize_b = QUANTIZE_INVOKE(&cpi->rtcd.quantize,
1180 cpi->mb.quantize_b_pair = QUANTIZE_INVOKE(&cpi->rtcd.quantize,
1185 cpi->mb.quantize_b = QUANTIZE_INVOKE(&cpi->rtcd.quantize,
1187 cpi->mb.quantize_b_pair = QUANTIZE_INVOKE(&cpi->rtcd.quantize,
1190 if (cpi->sf.improved_quant != last_improved_quant)
1191 vp8cx_init_quantizer(cpi);
1193 #if CONFIG_RUNTIME_CPU_DETECT
1194 cpi->mb.e_mbd.rtcd = &cpi->common.rtcd;
1197 if (cpi->sf.iterative_sub_pixel == 1)
1199 cpi->find_fractional_mv_step = vp8_find_best_sub_pixel_step_iteratively;
1201 else if (cpi->sf.quarter_pixel_search)
1203 cpi->find_fractional_mv_step = vp8_find_best_sub_pixel_step;
1205 else if (cpi->sf.half_pixel_search)
1207 cpi->find_fractional_mv_step = vp8_find_best_half_pixel_step;
1211 cpi->find_fractional_mv_step = vp8_skip_fractional_mv_step;
1214 if (cpi->sf.optimize_coefficients == 1 && cpi->pass!=1)
1215 cpi->mb.optimize = 1;
1217 cpi->mb.optimize = 0;
1219 if (cpi->common.full_pixel)
1220 cpi->find_fractional_mv_step = vp8_skip_fractional_mv_step;
1223 frames_at_speed[cpi->Speed]++;
1226 static void alloc_raw_frame_buffers(VP8_COMP *cpi)
1228 int width = (cpi->oxcf.Width + 15) & ~15;
1229 int height = (cpi->oxcf.Height + 15) & ~15;
1231 cpi->lookahead = vp8_lookahead_init(cpi->oxcf.Width, cpi->oxcf.Height,
1232 cpi->oxcf.lag_in_frames);
1234 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1235 "Failed to allocate lag buffers");
1237 #if VP8_TEMPORAL_ALT_REF
1239 if (vp8_yv12_alloc_frame_buffer(&cpi->alt_ref_buffer,
1241 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1242 "Failed to allocate altref buffer");
1247 static int vp8_alloc_partition_data(VP8_COMP *cpi)
1249 vpx_free(cpi->mb.pip);
1251 cpi->mb.pip = vpx_calloc((cpi->common.mb_cols + 1) *
1252 (cpi->common.mb_rows + 1),
1253 sizeof(PARTITION_INFO));
1257 cpi->mb.pi = cpi->mb.pip + cpi->common.mode_info_stride + 1;
1262 void vp8_alloc_compressor_data(VP8_COMP *cpi)
1264 VP8_COMMON *cm = & cpi->common;
1266 int width = cm->Width;
1267 int height = cm->Height;
1269 if (vp8_alloc_frame_buffers(cm, width, height))
1270 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1271 "Failed to allocate frame buffers");
1273 if (vp8_alloc_partition_data(cpi))
1274 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1275 "Failed to allocate partition data");
1278 if ((width & 0xf) != 0)
1279 width += 16 - (width & 0xf);
1281 if ((height & 0xf) != 0)
1282 height += 16 - (height & 0xf);
1285 if (vp8_yv12_alloc_frame_buffer(&cpi->last_frame_uf,
1286 width, height, VP8BORDERINPIXELS))
1287 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1288 "Failed to allocate last frame buffer");
1290 if (vp8_yv12_alloc_frame_buffer(&cpi->scaled_source, width, height, 16))
1291 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1292 "Failed to allocate scaled source buffer");
1298 unsigned int tokens = cm->mb_rows * cm->mb_cols * 24 * 16;
1300 CHECK_MEM_ERROR(cpi->tok, vpx_calloc(tokens, sizeof(*cpi->tok)));
1303 // Data used for real time vc mode to see if gf needs refreshing
1304 cpi->inter_zz_count = 0;
1305 cpi->gf_bad_count = 0;
1306 cpi->gf_update_recommended = 0;
1309 // Structures used to minitor GF usage
1310 vpx_free(cpi->gf_active_flags);
1311 CHECK_MEM_ERROR(cpi->gf_active_flags,
1312 vpx_calloc(1, cm->mb_rows * cm->mb_cols));
1313 cpi->gf_active_count = cm->mb_rows * cm->mb_cols;
1315 vpx_free(cpi->mb_activity_map);
1316 CHECK_MEM_ERROR(cpi->mb_activity_map,
1317 vpx_calloc(sizeof(unsigned int),
1318 cm->mb_rows * cm->mb_cols));
1320 vpx_free(cpi->mb_norm_activity_map);
1321 CHECK_MEM_ERROR(cpi->mb_norm_activity_map,
1322 vpx_calloc(sizeof(unsigned int),
1323 cm->mb_rows * cm->mb_cols));
1325 #if !(CONFIG_REALTIME_ONLY)
1326 vpx_free(cpi->twopass.total_stats);
1328 cpi->twopass.total_stats = vpx_calloc(1, sizeof(FIRSTPASS_STATS));
1330 vpx_free(cpi->twopass.this_frame_stats);
1332 cpi->twopass.this_frame_stats = vpx_calloc(1, sizeof(FIRSTPASS_STATS));
1334 if(!cpi->twopass.total_stats || !cpi->twopass.this_frame_stats)
1335 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1336 "Failed to allocate firstpass stats");
1339 #if CONFIG_MULTITHREAD
1341 cpi->mt_sync_range = 1;
1342 else if (width <= 1280)
1343 cpi->mt_sync_range = 4;
1344 else if (width <= 2560)
1345 cpi->mt_sync_range = 8;
1347 cpi->mt_sync_range = 16;
1350 vpx_free(cpi->tplist);
1352 CHECK_MEM_ERROR(cpi->tplist, vpx_malloc(sizeof(TOKENLIST) * cpi->common.mb_rows));
1357 static const int q_trans[] =
1359 0, 1, 2, 3, 4, 5, 7, 8,
1360 9, 10, 12, 13, 15, 17, 18, 19,
1361 20, 21, 23, 24, 25, 26, 27, 28,
1362 29, 30, 31, 33, 35, 37, 39, 41,
1363 43, 45, 47, 49, 51, 53, 55, 57,
1364 59, 61, 64, 67, 70, 73, 76, 79,
1365 82, 85, 88, 91, 94, 97, 100, 103,
1366 106, 109, 112, 115, 118, 121, 124, 127,
1369 int vp8_reverse_trans(int x)
1373 for (i = 0; i < 64; i++)
1374 if (q_trans[i] >= x)
1379 void vp8_new_frame_rate(VP8_COMP *cpi, double framerate)
1384 cpi->oxcf.frame_rate = framerate;
1385 cpi->output_frame_rate = cpi->oxcf.frame_rate;
1386 cpi->per_frame_bandwidth = (int)(cpi->oxcf.target_bandwidth / cpi->output_frame_rate);
1387 cpi->av_per_frame_bandwidth = (int)(cpi->oxcf.target_bandwidth / cpi->output_frame_rate);
1388 cpi->min_frame_bandwidth = (int)(cpi->av_per_frame_bandwidth * cpi->oxcf.two_pass_vbrmin_section / 100);
1390 // Set Maximum gf/arf interval
1391 cpi->max_gf_interval = ((int)(cpi->output_frame_rate / 2.0) + 2);
1393 if(cpi->max_gf_interval < 12)
1394 cpi->max_gf_interval = 12;
1396 // Extended interval for genuinely static scenes
1397 cpi->twopass.static_scene_max_gf_interval = cpi->key_frame_frequency >> 1;
1399 // Special conditions when altr ref frame enabled in lagged compress mode
1400 if (cpi->oxcf.play_alternate && cpi->oxcf.lag_in_frames)
1402 if (cpi->max_gf_interval > cpi->oxcf.lag_in_frames - 1)
1403 cpi->max_gf_interval = cpi->oxcf.lag_in_frames - 1;
1405 if (cpi->twopass.static_scene_max_gf_interval > cpi->oxcf.lag_in_frames - 1)
1406 cpi->twopass.static_scene_max_gf_interval = cpi->oxcf.lag_in_frames - 1;
1409 if ( cpi->max_gf_interval > cpi->twopass.static_scene_max_gf_interval )
1410 cpi->max_gf_interval = cpi->twopass.static_scene_max_gf_interval;
1415 rescale(int val, int num, int denom)
1417 int64_t llnum = num;
1418 int64_t llden = denom;
1419 int64_t llval = val;
1421 return llval * llnum / llden;
1425 static void init_config(VP8_PTR ptr, VP8_CONFIG *oxcf)
1427 VP8_COMP *cpi = (VP8_COMP *)(ptr);
1428 VP8_COMMON *cm = &cpi->common;
1433 cpi->auto_adjust_gold_quantizer = 1;
1436 cm->version = oxcf->Version;
1437 vp8_setup_version(cm);
1439 // change includes all joint functionality
1440 vp8_change_config(ptr, oxcf);
1442 // Initialize active best and worst q and average q values.
1443 cpi->active_worst_quality = cpi->oxcf.worst_allowed_q;
1444 cpi->active_best_quality = cpi->oxcf.best_allowed_q;
1445 cpi->avg_frame_qindex = cpi->oxcf.worst_allowed_q;
1447 // Initialise the starting buffer levels
1448 cpi->buffer_level = cpi->oxcf.starting_buffer_level;
1449 cpi->bits_off_target = cpi->oxcf.starting_buffer_level;
1451 cpi->rolling_target_bits = cpi->av_per_frame_bandwidth;
1452 cpi->rolling_actual_bits = cpi->av_per_frame_bandwidth;
1453 cpi->long_rolling_target_bits = cpi->av_per_frame_bandwidth;
1454 cpi->long_rolling_actual_bits = cpi->av_per_frame_bandwidth;
1455 cpi->buffered_av_per_frame_bandwidth = cpi->av_per_frame_bandwidth;
1457 cpi->total_actual_bits = 0;
1458 cpi->total_target_vs_actual = 0;
1460 #if VP8_TEMPORAL_ALT_REF
1464 cpi->fixed_divide[0] = 0;
1466 for (i = 1; i < 512; i++)
1467 cpi->fixed_divide[i] = 0x80000 / i;
1473 void vp8_change_config(VP8_PTR ptr, VP8_CONFIG *oxcf)
1475 VP8_COMP *cpi = (VP8_COMP *)(ptr);
1476 VP8_COMMON *cm = &cpi->common;
1484 if (cm->version != oxcf->Version)
1486 cm->version = oxcf->Version;
1487 vp8_setup_version(cm);
1492 switch (cpi->oxcf.Mode)
1497 cpi->compressor_speed = 2;
1499 if (cpi->oxcf.cpu_used < -16)
1501 cpi->oxcf.cpu_used = -16;
1504 if (cpi->oxcf.cpu_used > 16)
1505 cpi->oxcf.cpu_used = 16;
1509 case MODE_GOODQUALITY:
1511 cpi->compressor_speed = 1;
1513 if (cpi->oxcf.cpu_used < -5)
1515 cpi->oxcf.cpu_used = -5;
1518 if (cpi->oxcf.cpu_used > 5)
1519 cpi->oxcf.cpu_used = 5;
1523 case MODE_BESTQUALITY:
1525 cpi->compressor_speed = 0;
1528 case MODE_FIRSTPASS:
1530 cpi->compressor_speed = 1;
1532 case MODE_SECONDPASS:
1534 cpi->compressor_speed = 1;
1536 if (cpi->oxcf.cpu_used < -5)
1538 cpi->oxcf.cpu_used = -5;
1541 if (cpi->oxcf.cpu_used > 5)
1542 cpi->oxcf.cpu_used = 5;
1545 case MODE_SECONDPASS_BEST:
1547 cpi->compressor_speed = 0;
1551 if (cpi->pass == 0 && cpi->oxcf.end_usage != USAGE_STREAM_FROM_SERVER)
1552 cpi->auto_worst_q = 1;
1554 cpi->oxcf.worst_allowed_q = q_trans[oxcf->worst_allowed_q];
1555 cpi->oxcf.best_allowed_q = q_trans[oxcf->best_allowed_q];
1556 cpi->oxcf.cq_level = q_trans[cpi->oxcf.cq_level];
1558 if (oxcf->fixed_q >= 0)
1560 if (oxcf->worst_allowed_q < 0)
1561 cpi->oxcf.fixed_q = q_trans[0];
1563 cpi->oxcf.fixed_q = q_trans[oxcf->worst_allowed_q];
1565 if (oxcf->alt_q < 0)
1566 cpi->oxcf.alt_q = q_trans[0];
1568 cpi->oxcf.alt_q = q_trans[oxcf->alt_q];
1570 if (oxcf->key_q < 0)
1571 cpi->oxcf.key_q = q_trans[0];
1573 cpi->oxcf.key_q = q_trans[oxcf->key_q];
1575 if (oxcf->gold_q < 0)
1576 cpi->oxcf.gold_q = q_trans[0];
1578 cpi->oxcf.gold_q = q_trans[oxcf->gold_q];
1582 cpi->baseline_gf_interval =
1583 cpi->oxcf.alt_freq ? cpi->oxcf.alt_freq : DEFAULT_GF_INTERVAL;
1585 cpi->ref_frame_flags = VP8_ALT_FLAG | VP8_GOLD_FLAG | VP8_LAST_FLAG;
1587 //cpi->use_golden_frame_only = 0;
1588 //cpi->use_last_frame_only = 0;
1589 cm->refresh_golden_frame = 0;
1590 cm->refresh_last_frame = 1;
1591 cm->refresh_entropy_probs = 1;
1593 if (cpi->oxcf.token_partitions >= 0 && cpi->oxcf.token_partitions <= 3)
1594 cm->multi_token_partition =
1595 (TOKEN_PARTITION) cpi->oxcf.token_partitions;
1597 setup_features(cpi);
1602 for (i = 0; i < MAX_MB_SEGMENTS; i++)
1603 cpi->segment_encode_breakout[i] = cpi->oxcf.encode_breakout;
1606 // At the moment the first order values may not be > MAXQ
1607 if (cpi->oxcf.fixed_q > MAXQ)
1608 cpi->oxcf.fixed_q = MAXQ;
1610 // local file playback mode == really big buffer
1611 if (cpi->oxcf.end_usage == USAGE_LOCAL_FILE_PLAYBACK)
1613 cpi->oxcf.starting_buffer_level = 60000;
1614 cpi->oxcf.optimal_buffer_level = 60000;
1615 cpi->oxcf.maximum_buffer_size = 240000;
1618 // Convert target bandwidth from Kbit/s to Bit/s
1619 cpi->oxcf.target_bandwidth *= 1000;
1621 cpi->oxcf.starting_buffer_level =
1622 rescale(cpi->oxcf.starting_buffer_level,
1623 cpi->oxcf.target_bandwidth, 1000);
1625 // Set or reset optimal and maximum buffer levels.
1626 if (cpi->oxcf.optimal_buffer_level == 0)
1627 cpi->oxcf.optimal_buffer_level = cpi->oxcf.target_bandwidth / 8;
1629 cpi->oxcf.optimal_buffer_level =
1630 rescale(cpi->oxcf.optimal_buffer_level,
1631 cpi->oxcf.target_bandwidth, 1000);
1633 if (cpi->oxcf.maximum_buffer_size == 0)
1634 cpi->oxcf.maximum_buffer_size = cpi->oxcf.target_bandwidth / 8;
1636 cpi->oxcf.maximum_buffer_size =
1637 rescale(cpi->oxcf.maximum_buffer_size,
1638 cpi->oxcf.target_bandwidth, 1000);
1640 // Set up frame rate and related parameters rate control values.
1641 vp8_new_frame_rate(cpi, cpi->oxcf.frame_rate);
1643 // Set absolute upper and lower quality limits
1644 cpi->worst_quality = cpi->oxcf.worst_allowed_q;
1645 cpi->best_quality = cpi->oxcf.best_allowed_q;
1647 // active values should only be modified if out of new range
1648 if (cpi->active_worst_quality > cpi->oxcf.worst_allowed_q)
1650 cpi->active_worst_quality = cpi->oxcf.worst_allowed_q;
1653 else if (cpi->active_worst_quality < cpi->oxcf.best_allowed_q)
1655 cpi->active_worst_quality = cpi->oxcf.best_allowed_q;
1657 if (cpi->active_best_quality < cpi->oxcf.best_allowed_q)
1659 cpi->active_best_quality = cpi->oxcf.best_allowed_q;
1662 else if (cpi->active_best_quality > cpi->oxcf.worst_allowed_q)
1664 cpi->active_best_quality = cpi->oxcf.worst_allowed_q;
1667 cpi->buffered_mode = (cpi->oxcf.optimal_buffer_level > 0) ? TRUE : FALSE;
1669 cpi->cq_target_quality = cpi->oxcf.cq_level;
1671 // Only allow dropped frames in buffered mode
1672 cpi->drop_frames_allowed = cpi->oxcf.allow_df && cpi->buffered_mode;
1674 if (!cm->use_bilinear_mc_filter)
1675 cm->mcomp_filter_type = SIXTAP;
1677 cm->mcomp_filter_type = BILINEAR;
1679 cpi->target_bandwidth = cpi->oxcf.target_bandwidth;
1681 cm->Width = cpi->oxcf.Width ;
1682 cm->Height = cpi->oxcf.Height ;
1684 cm->horiz_scale = cpi->horiz_scale;
1685 cm->vert_scale = cpi->vert_scale ;
1687 // VP8 sharpness level mapping 0-7 (vs 0-10 in general VPx dialogs)
1688 if (cpi->oxcf.Sharpness > 7)
1689 cpi->oxcf.Sharpness = 7;
1691 cm->sharpness_level = cpi->oxcf.Sharpness;
1693 if (cm->horiz_scale != NORMAL || cm->vert_scale != NORMAL)
1695 int UNINITIALIZED_IS_SAFE(hr), UNINITIALIZED_IS_SAFE(hs);
1696 int UNINITIALIZED_IS_SAFE(vr), UNINITIALIZED_IS_SAFE(vs);
1698 Scale2Ratio(cm->horiz_scale, &hr, &hs);
1699 Scale2Ratio(cm->vert_scale, &vr, &vs);
1701 // always go to the next whole number
1702 cm->Width = (hs - 1 + cpi->oxcf.Width * hr) / hs;
1703 cm->Height = (vs - 1 + cpi->oxcf.Height * vr) / vs;
1706 if (((cm->Width + 15) & 0xfffffff0) !=
1707 cm->yv12_fb[cm->lst_fb_idx].y_width ||
1708 ((cm->Height + 15) & 0xfffffff0) !=
1709 cm->yv12_fb[cm->lst_fb_idx].y_height ||
1710 cm->yv12_fb[cm->lst_fb_idx].y_width == 0)
1712 alloc_raw_frame_buffers(cpi);
1713 vp8_alloc_compressor_data(cpi);
1716 if (cpi->oxcf.fixed_q >= 0)
1718 cpi->last_q[0] = cpi->oxcf.fixed_q;
1719 cpi->last_q[1] = cpi->oxcf.fixed_q;
1722 cpi->Speed = cpi->oxcf.cpu_used;
1724 // force to allowlag to 0 if lag_in_frames is 0;
1725 if (cpi->oxcf.lag_in_frames == 0)
1727 cpi->oxcf.allow_lag = 0;
1729 // Limit on lag buffers as these are not currently dynamically allocated
1730 else if (cpi->oxcf.lag_in_frames > MAX_LAG_BUFFERS)
1731 cpi->oxcf.lag_in_frames = MAX_LAG_BUFFERS;
1734 cpi->alt_ref_source = NULL;
1735 cpi->is_src_frame_alt_ref = 0;
1739 // Experimental RD Code
1740 cpi->frame_distortion = 0;
1741 cpi->last_frame_distortion = 0;
1746 #define M_LOG2_E 0.693147180559945309417
1747 #define log2f(x) (log (x) / (float) M_LOG2_E)
1748 static void cal_mvsadcosts(int *mvsadcost[2])
1752 mvsadcost [0] [0] = 300;
1753 mvsadcost [1] [0] = 300;
1757 double z = 256 * (2 * (log2f(8 * i) + .6));
1758 mvsadcost [0][i] = (int) z;
1759 mvsadcost [1][i] = (int) z;
1760 mvsadcost [0][-i] = (int) z;
1761 mvsadcost [1][-i] = (int) z;
1763 while (++i <= mvfp_max);
1766 VP8_PTR vp8_create_compressor(VP8_CONFIG *oxcf)
1778 cpi = ctx.cpi = vpx_memalign(32, sizeof(VP8_COMP));
1779 // Check that the CPI instance is valid
1785 vpx_memset(cpi, 0, sizeof(VP8_COMP));
1787 if (setjmp(cm->error.jmp))
1789 VP8_PTR ptr = ctx.ptr;
1791 ctx.cpi->common.error.setjmp = 0;
1792 vp8_remove_compressor(&ptr);
1796 cpi->common.error.setjmp = 1;
1798 CHECK_MEM_ERROR(cpi->mb.ss, vpx_calloc(sizeof(search_site), (MAX_MVSEARCH_STEPS * 8) + 1));
1800 vp8_create_common(&cpi->common);
1801 vp8_cmachine_specific_config(cpi);
1803 init_config((VP8_PTR)cpi, oxcf);
1805 memcpy(cpi->base_skip_false_prob, vp8cx_base_skip_false_prob, sizeof(vp8cx_base_skip_false_prob));
1806 cpi->common.current_video_frame = 0;
1807 cpi->kf_overspend_bits = 0;
1808 cpi->kf_bitrate_adjustment = 0;
1809 cpi->frames_till_gf_update_due = 0;
1810 cpi->gf_overspend_bits = 0;
1811 cpi->non_gf_bitrate_adjustment = 0;
1812 cpi->prob_last_coded = 128;
1813 cpi->prob_gf_coded = 128;
1814 cpi->prob_intra_coded = 63;
1816 // Prime the recent reference frame useage counters.
1817 // Hereafter they will be maintained as a sort of moving average
1818 cpi->recent_ref_frame_usage[INTRA_FRAME] = 1;
1819 cpi->recent_ref_frame_usage[LAST_FRAME] = 1;
1820 cpi->recent_ref_frame_usage[GOLDEN_FRAME] = 1;
1821 cpi->recent_ref_frame_usage[ALTREF_FRAME] = 1;
1823 // Set reference frame sign bias for ALTREF frame to 1 (for now)
1824 cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = 1;
1826 cpi->twopass.gf_decay_rate = 0;
1827 cpi->baseline_gf_interval = DEFAULT_GF_INTERVAL;
1829 cpi->gold_is_last = 0 ;
1830 cpi->alt_is_last = 0 ;
1831 cpi->gold_is_alt = 0 ;
1833 // allocate memory for storing last frame's MVs for MV prediction.
1834 CHECK_MEM_ERROR(cpi->lfmv, vpx_calloc((cpi->common.mb_rows+2) * (cpi->common.mb_cols+2), sizeof(int_mv)));
1835 CHECK_MEM_ERROR(cpi->lf_ref_frame_sign_bias, vpx_calloc((cpi->common.mb_rows+2) * (cpi->common.mb_cols+2), sizeof(int)));
1836 CHECK_MEM_ERROR(cpi->lf_ref_frame, vpx_calloc((cpi->common.mb_rows+2) * (cpi->common.mb_cols+2), sizeof(int)));
1838 // Create the encoder segmentation map and set all entries to 0
1839 CHECK_MEM_ERROR(cpi->segmentation_map, vpx_calloc(cpi->common.mb_rows * cpi->common.mb_cols, 1));
1840 CHECK_MEM_ERROR(cpi->active_map, vpx_calloc(cpi->common.mb_rows * cpi->common.mb_cols, 1));
1841 vpx_memset(cpi->active_map , 1, (cpi->common.mb_rows * cpi->common.mb_cols));
1842 cpi->active_map_enabled = 0;
1845 // Experimental code for lagged and one pass
1846 // Initialise one_pass GF frames stats
1847 // Update stats used for GF selection
1850 cpi->one_pass_frame_index = 0;
1852 for (i = 0; i < MAX_LAG_BUFFERS; i++)
1854 cpi->one_pass_frame_stats[i].frames_so_far = 0;
1855 cpi->one_pass_frame_stats[i].frame_intra_error = 0.0;
1856 cpi->one_pass_frame_stats[i].frame_coded_error = 0.0;
1857 cpi->one_pass_frame_stats[i].frame_pcnt_inter = 0.0;
1858 cpi->one_pass_frame_stats[i].frame_pcnt_motion = 0.0;
1859 cpi->one_pass_frame_stats[i].frame_mvr = 0.0;
1860 cpi->one_pass_frame_stats[i].frame_mvr_abs = 0.0;
1861 cpi->one_pass_frame_stats[i].frame_mvc = 0.0;
1862 cpi->one_pass_frame_stats[i].frame_mvc_abs = 0.0;
1867 // Should we use the cyclic refresh method.
1868 // Currently this is tied to error resilliant mode
1869 cpi->cyclic_refresh_mode_enabled = cpi->oxcf.error_resilient_mode;
1870 cpi->cyclic_refresh_mode_max_mbs_perframe = (cpi->common.mb_rows * cpi->common.mb_cols) / 40;
1871 cpi->cyclic_refresh_mode_index = 0;
1872 cpi->cyclic_refresh_q = 32;
1874 if (cpi->cyclic_refresh_mode_enabled)
1876 CHECK_MEM_ERROR(cpi->cyclic_refresh_map, vpx_calloc((cpi->common.mb_rows * cpi->common.mb_cols), 1));
1879 cpi->cyclic_refresh_map = (signed char *) NULL;
1881 // Test function for segmentation
1882 //segmentation_test_function((VP8_PTR) cpi);
1884 #ifdef ENTROPY_STATS
1885 init_context_counters();
1888 /*Initialize the feed-forward activity masking.*/
1889 cpi->activity_avg = 90<<12;
1891 cpi->frames_since_key = 8; // Give a sensible default for the first frame.
1892 cpi->key_frame_frequency = cpi->oxcf.key_freq;
1893 cpi->this_key_frame_forced = FALSE;
1894 cpi->next_key_frame_forced = FALSE;
1896 cpi->source_alt_ref_pending = FALSE;
1897 cpi->source_alt_ref_active = FALSE;
1898 cpi->common.refresh_alt_ref_frame = 0;
1900 cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
1901 #if CONFIG_INTERNAL_STATS
1902 cpi->b_calculate_ssimg = 0;
1907 if (cpi->b_calculate_psnr)
1909 cpi->total_sq_error = 0.0;
1910 cpi->total_sq_error2 = 0.0;
1915 cpi->totalp_y = 0.0;
1916 cpi->totalp_u = 0.0;
1917 cpi->totalp_v = 0.0;
1919 cpi->tot_recode_hits = 0;
1920 cpi->summed_quality = 0;
1921 cpi->summed_weights = 0;
1924 if (cpi->b_calculate_ssimg)
1926 cpi->total_ssimg_y = 0;
1927 cpi->total_ssimg_u = 0;
1928 cpi->total_ssimg_v = 0;
1929 cpi->total_ssimg_all = 0;
1935 #define LLONG_MAX 9223372036854775807LL
1937 cpi->first_time_stamp_ever = LLONG_MAX;
1939 cpi->frames_till_gf_update_due = 0;
1940 cpi->key_frame_count = 1;
1942 cpi->ni_av_qi = cpi->oxcf.worst_allowed_q;
1945 cpi->total_byte_count = 0;
1947 cpi->drop_frame = 0;
1948 cpi->drop_count = 0;
1949 cpi->max_drop_count = 0;
1950 cpi->max_consec_dropped_frames = 4;
1952 cpi->rate_correction_factor = 1.0;
1953 cpi->key_frame_rate_correction_factor = 1.0;
1954 cpi->gf_rate_correction_factor = 1.0;
1955 cpi->twopass.est_max_qcorrection_factor = 1.0;
1957 cpi->mb.mvcost[0] = &cpi->mb.mvcosts[0][mv_max+1];
1958 cpi->mb.mvcost[1] = &cpi->mb.mvcosts[1][mv_max+1];
1959 cpi->mb.mvsadcost[0] = &cpi->mb.mvsadcosts[0][mvfp_max+1];
1960 cpi->mb.mvsadcost[1] = &cpi->mb.mvsadcosts[1][mvfp_max+1];
1962 cal_mvsadcosts(cpi->mb.mvsadcost);
1964 for (i = 0; i < KEY_FRAME_CONTEXT; i++)
1966 cpi->prior_key_frame_distance[i] = (int)cpi->output_frame_rate;
1969 #ifdef OUTPUT_YUV_SRC
1970 yuv_file = fopen("bd.yuv", "ab");
1974 framepsnr = fopen("framepsnr.stt", "a");
1975 kf_list = fopen("kf_list.stt", "w");
1978 cpi->output_pkt_list = oxcf->output_pkt_list;
1980 #if !(CONFIG_REALTIME_ONLY)
1984 vp8_init_first_pass(cpi);
1986 else if (cpi->pass == 2)
1988 size_t packet_sz = sizeof(FIRSTPASS_STATS);
1989 int packets = oxcf->two_pass_stats_in.sz / packet_sz;
1991 cpi->twopass.stats_in_start = oxcf->two_pass_stats_in.buf;
1992 cpi->twopass.stats_in = cpi->twopass.stats_in_start;
1993 cpi->twopass.stats_in_end = (void*)((char *)cpi->twopass.stats_in
1994 + (packets - 1) * packet_sz);
1995 vp8_init_second_pass(cpi);
2000 if (cpi->compressor_speed == 2)
2002 cpi->cpu_freq = 0; //vp8_get_processor_freq();
2003 cpi->avg_encode_time = 0;
2004 cpi->avg_pick_mode_time = 0;
2007 vp8_set_speed_features(cpi);
2009 // Set starting values of RD threshold multipliers (128 = *1)
2010 for (i = 0; i < MAX_MODES; i++)
2012 cpi->rd_thresh_mult[i] = 128;
2015 #ifdef ENTROPY_STATS
2016 init_mv_ref_counts();
2019 #if CONFIG_MULTITHREAD
2020 vp8cx_create_encoder_threads(cpi);
2023 cpi->fn_ptr[BLOCK_16X16].sdf = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x16);
2024 cpi->fn_ptr[BLOCK_16X16].vf = VARIANCE_INVOKE(&cpi->rtcd.variance, var16x16);
2025 cpi->fn_ptr[BLOCK_16X16].svf = VARIANCE_INVOKE(&cpi->rtcd.variance, subpixvar16x16);
2026 cpi->fn_ptr[BLOCK_16X16].svf_halfpix_h = VARIANCE_INVOKE(&cpi->rtcd.variance, halfpixvar16x16_h);
2027 cpi->fn_ptr[BLOCK_16X16].svf_halfpix_v = VARIANCE_INVOKE(&cpi->rtcd.variance, halfpixvar16x16_v);
2028 cpi->fn_ptr[BLOCK_16X16].svf_halfpix_hv = VARIANCE_INVOKE(&cpi->rtcd.variance, halfpixvar16x16_hv);
2029 cpi->fn_ptr[BLOCK_16X16].sdx3f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x16x3);
2030 cpi->fn_ptr[BLOCK_16X16].sdx8f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x16x8);
2031 cpi->fn_ptr[BLOCK_16X16].sdx4df = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x16x4d);
2033 cpi->fn_ptr[BLOCK_16X8].sdf = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x8);
2034 cpi->fn_ptr[BLOCK_16X8].vf = VARIANCE_INVOKE(&cpi->rtcd.variance, var16x8);
2035 cpi->fn_ptr[BLOCK_16X8].svf = VARIANCE_INVOKE(&cpi->rtcd.variance, subpixvar16x8);
2036 cpi->fn_ptr[BLOCK_16X8].svf_halfpix_h = NULL;
2037 cpi->fn_ptr[BLOCK_16X8].svf_halfpix_v = NULL;
2038 cpi->fn_ptr[BLOCK_16X8].svf_halfpix_hv = NULL;
2039 cpi->fn_ptr[BLOCK_16X8].sdx3f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x8x3);
2040 cpi->fn_ptr[BLOCK_16X8].sdx8f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x8x8);
2041 cpi->fn_ptr[BLOCK_16X8].sdx4df = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x8x4d);
2043 cpi->fn_ptr[BLOCK_8X16].sdf = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x16);
2044 cpi->fn_ptr[BLOCK_8X16].vf = VARIANCE_INVOKE(&cpi->rtcd.variance, var8x16);
2045 cpi->fn_ptr[BLOCK_8X16].svf = VARIANCE_INVOKE(&cpi->rtcd.variance, subpixvar8x16);
2046 cpi->fn_ptr[BLOCK_8X16].svf_halfpix_h = NULL;
2047 cpi->fn_ptr[BLOCK_8X16].svf_halfpix_v = NULL;
2048 cpi->fn_ptr[BLOCK_8X16].svf_halfpix_hv = NULL;
2049 cpi->fn_ptr[BLOCK_8X16].sdx3f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x16x3);
2050 cpi->fn_ptr[BLOCK_8X16].sdx8f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x16x8);
2051 cpi->fn_ptr[BLOCK_8X16].sdx4df = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x16x4d);
2053 cpi->fn_ptr[BLOCK_8X8].sdf = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x8);
2054 cpi->fn_ptr[BLOCK_8X8].vf = VARIANCE_INVOKE(&cpi->rtcd.variance, var8x8);
2055 cpi->fn_ptr[BLOCK_8X8].svf = VARIANCE_INVOKE(&cpi->rtcd.variance, subpixvar8x8);
2056 cpi->fn_ptr[BLOCK_8X8].svf_halfpix_h = NULL;
2057 cpi->fn_ptr[BLOCK_8X8].svf_halfpix_v = NULL;
2058 cpi->fn_ptr[BLOCK_8X8].svf_halfpix_hv = NULL;
2059 cpi->fn_ptr[BLOCK_8X8].sdx3f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x8x3);
2060 cpi->fn_ptr[BLOCK_8X8].sdx8f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x8x8);
2061 cpi->fn_ptr[BLOCK_8X8].sdx4df = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x8x4d);
2063 cpi->fn_ptr[BLOCK_4X4].sdf = VARIANCE_INVOKE(&cpi->rtcd.variance, sad4x4);
2064 cpi->fn_ptr[BLOCK_4X4].vf = VARIANCE_INVOKE(&cpi->rtcd.variance, var4x4);
2065 cpi->fn_ptr[BLOCK_4X4].svf = VARIANCE_INVOKE(&cpi->rtcd.variance, subpixvar4x4);
2066 cpi->fn_ptr[BLOCK_4X4].svf_halfpix_h = NULL;
2067 cpi->fn_ptr[BLOCK_4X4].svf_halfpix_v = NULL;
2068 cpi->fn_ptr[BLOCK_4X4].svf_halfpix_hv = NULL;
2069 cpi->fn_ptr[BLOCK_4X4].sdx3f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad4x4x3);
2070 cpi->fn_ptr[BLOCK_4X4].sdx8f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad4x4x8);
2071 cpi->fn_ptr[BLOCK_4X4].sdx4df = VARIANCE_INVOKE(&cpi->rtcd.variance, sad4x4x4d);
2073 cpi->full_search_sad = SEARCH_INVOKE(&cpi->rtcd.search, full_search);
2074 cpi->diamond_search_sad = SEARCH_INVOKE(&cpi->rtcd.search, diamond_search);
2075 cpi->refining_search_sad = SEARCH_INVOKE(&cpi->rtcd.search, refining_search);
2077 // make sure frame 1 is okay
2078 cpi->error_bins[0] = cpi->common.MBs;
2080 //vp8cx_init_quantizer() is first called here. Add check in vp8cx_frame_init_quantizer() so that vp8cx_init_quantizer is only called later
2081 //when needed. This will avoid unnecessary calls of vp8cx_init_quantizer() for every frame.
2082 vp8cx_init_quantizer(cpi);
2084 vp8_loop_filter_init(cm);
2086 cpi->common.error.setjmp = 0;
2087 return (VP8_PTR) cpi;
2092 void vp8_remove_compressor(VP8_PTR *ptr)
2094 VP8_COMP *cpi = (VP8_COMP *)(*ptr);
2099 if (cpi && (cpi->common.current_video_frame > 0))
2101 #if !(CONFIG_REALTIME_ONLY)
2105 vp8_end_second_pass(cpi);
2110 #ifdef ENTROPY_STATS
2111 print_context_counters();
2112 print_tree_update_probs();
2113 print_mode_context();
2116 #if CONFIG_INTERNAL_STATS
2120 FILE *f = fopen("opsnr.stt", "a");
2121 double time_encoded = (cpi->last_end_time_stamp_seen
2122 - cpi->first_time_stamp_ever) / 10000000.000;
2123 double total_encode_time = (cpi->time_receive_data + cpi->time_compress_data) / 1000.000;
2124 double dr = (double)cpi->bytes * (double) 8 / (double)1000 / time_encoded;
2126 if (cpi->b_calculate_psnr)
2128 YV12_BUFFER_CONFIG *lst_yv12 = &cpi->common.yv12_fb[cpi->common.lst_fb_idx];
2129 double samples = 3.0 / 2 * cpi->count * lst_yv12->y_width * lst_yv12->y_height;
2130 double total_psnr = vp8_mse2psnr(samples, 255.0, cpi->total_sq_error);
2131 double total_psnr2 = vp8_mse2psnr(samples, 255.0, cpi->total_sq_error2);
2132 double total_ssim = 100 * pow(cpi->summed_quality / cpi->summed_weights, 8.0);
2134 fprintf(f, "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\tVPXSSIM\t Time(us)\n");
2135 fprintf(f, "%7.3f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t%8.0f\n",
2136 dr, cpi->total / cpi->count, total_psnr, cpi->totalp / cpi->count, total_psnr2, total_ssim,
2140 if (cpi->b_calculate_ssimg)
2142 fprintf(f, "BitRate\tSSIM_Y\tSSIM_U\tSSIM_V\tSSIM_A\t Time(us)\n");
2143 fprintf(f, "%7.3f\t%6.4f\t%6.4f\t%6.4f\t%6.4f\t%8.0f\n", dr,
2144 cpi->total_ssimg_y / cpi->count, cpi->total_ssimg_u / cpi->count,
2145 cpi->total_ssimg_v / cpi->count, cpi->total_ssimg_all / cpi->count, total_encode_time);
2150 f = fopen("qskip.stt", "a");
2151 fprintf(f, "minq:%d -maxq:%d skipture:skipfalse = %d:%d\n", cpi->oxcf.best_allowed_q, cpi->oxcf.worst_allowed_q, skiptruecount, skipfalsecount);
2162 if (cpi->compressor_speed == 2)
2165 FILE *f = fopen("cxspeed.stt", "a");
2166 cnt_pm /= cpi->common.MBs;
2168 for (i = 0; i < 16; i++)
2169 fprintf(f, "%5d", frames_at_speed[i]);
2172 //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);
2181 extern int count_mb_seg[4];
2182 FILE *f = fopen("modes.stt", "a");
2183 double dr = (double)cpi->oxcf.frame_rate * (double)bytes * (double)8 / (double)count / (double)1000 ;
2184 fprintf(f, "intra_mode in Intra Frames:\n");
2185 fprintf(f, "Y: %8d, %8d, %8d, %8d, %8d\n", y_modes[0], y_modes[1], y_modes[2], y_modes[3], y_modes[4]);
2186 fprintf(f, "UV:%8d, %8d, %8d, %8d\n", uv_modes[0], uv_modes[1], uv_modes[2], uv_modes[3]);
2191 for (i = 0; i < 10; i++)
2192 fprintf(f, "%8d, ", b_modes[i]);
2198 fprintf(f, "Modes in Inter Frames:\n");
2199 fprintf(f, "Y: %8d, %8d, %8d, %8d, %8d, %8d, %8d, %8d, %8d, %8d\n",
2200 inter_y_modes[0], inter_y_modes[1], inter_y_modes[2], inter_y_modes[3], inter_y_modes[4],
2201 inter_y_modes[5], inter_y_modes[6], inter_y_modes[7], inter_y_modes[8], inter_y_modes[9]);
2202 fprintf(f, "UV:%8d, %8d, %8d, %8d\n", inter_uv_modes[0], inter_uv_modes[1], inter_uv_modes[2], inter_uv_modes[3]);
2207 for (i = 0; i < 15; i++)
2208 fprintf(f, "%8d, ", inter_b_modes[i]);
2213 fprintf(f, "P:%8d, %8d, %8d, %8d\n", count_mb_seg[0], count_mb_seg[1], count_mb_seg[2], count_mb_seg[3]);
2214 fprintf(f, "PB:%8d, %8d, %8d, %8d\n", inter_b_modes[LEFT4X4], inter_b_modes[ABOVE4X4], inter_b_modes[ZERO4X4], inter_b_modes[NEW4X4]);
2222 #ifdef ENTROPY_STATS
2225 FILE *fmode = fopen("modecontext.c", "w");
2227 fprintf(fmode, "\n#include \"entropymode.h\"\n\n");
2228 fprintf(fmode, "const unsigned int vp8_kf_default_bmode_counts ");
2229 fprintf(fmode, "[VP8_BINTRAMODES] [VP8_BINTRAMODES] [VP8_BINTRAMODES] =\n{\n");
2231 for (i = 0; i < 10; i++)
2234 fprintf(fmode, " { //Above Mode : %d\n", i);
2236 for (j = 0; j < 10; j++)
2239 fprintf(fmode, " {");
2241 for (k = 0; k < 10; k++)
2243 if (!intra_mode_stats[i][j][k])
2244 fprintf(fmode, " %5d, ", 1);
2246 fprintf(fmode, " %5d, ", intra_mode_stats[i][j][k]);
2249 fprintf(fmode, "}, // left_mode %d\n", j);
2253 fprintf(fmode, " },\n");
2257 fprintf(fmode, "};\n");
2263 #if defined(SECTIONBITS_OUTPUT)
2268 FILE *f = fopen("tokenbits.stt", "a");
2270 for (i = 0; i < 28; i++)
2271 fprintf(f, "%8d", (int)(Sectionbits[i] / 256));
2281 printf("\n_pick_loop_filter_level:%d\n", cpi->time_pick_lpf / 1000);
2282 printf("\n_frames recive_data encod_mb_row compress_frame Total\n");
2283 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);
2289 #if CONFIG_MULTITHREAD
2290 vp8cx_remove_encoder_threads(cpi);
2293 dealloc_compressor_data(cpi);
2294 vpx_free(cpi->mb.ss);
2296 vpx_free(cpi->cyclic_refresh_map);
2298 vp8_remove_common(&cpi->common);
2302 #ifdef OUTPUT_YUV_SRC
2322 static uint64_t calc_plane_error(unsigned char *orig, int orig_stride,
2323 unsigned char *recon, int recon_stride,
2324 unsigned int cols, unsigned int rows,
2325 vp8_variance_rtcd_vtable_t *rtcd)
2327 unsigned int row, col;
2328 uint64_t total_sse = 0;
2331 for (row = 0; row + 16 <= rows; row += 16)
2333 for (col = 0; col + 16 <= cols; col += 16)
2337 VARIANCE_INVOKE(rtcd, mse16x16)(orig + col, orig_stride,
2338 recon + col, recon_stride,
2343 /* Handle odd-sized width */
2346 unsigned int border_row, border_col;
2347 unsigned char *border_orig = orig;
2348 unsigned char *border_recon = recon;
2350 for (border_row = 0; border_row < 16; border_row++)
2352 for (border_col = col; border_col < cols; border_col++)
2354 diff = border_orig[border_col] - border_recon[border_col];
2355 total_sse += diff * diff;
2358 border_orig += orig_stride;
2359 border_recon += recon_stride;
2363 orig += orig_stride * 16;
2364 recon += recon_stride * 16;
2367 /* Handle odd-sized height */
2368 for (; row < rows; row++)
2370 for (col = 0; col < cols; col++)
2372 diff = orig[col] - recon[col];
2373 total_sse += diff * diff;
2376 orig += orig_stride;
2377 recon += recon_stride;
2384 static void generate_psnr_packet(VP8_COMP *cpi)
2386 YV12_BUFFER_CONFIG *orig = cpi->Source;
2387 YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
2388 struct vpx_codec_cx_pkt pkt;
2391 unsigned int width = cpi->common.Width;
2392 unsigned int height = cpi->common.Height;
2394 pkt.kind = VPX_CODEC_PSNR_PKT;
2395 sse = calc_plane_error(orig->y_buffer, orig->y_stride,
2396 recon->y_buffer, recon->y_stride,
2398 IF_RTCD(&cpi->rtcd.variance));
2399 pkt.data.psnr.sse[0] = sse;
2400 pkt.data.psnr.sse[1] = sse;
2401 pkt.data.psnr.samples[0] = width * height;
2402 pkt.data.psnr.samples[1] = width * height;
2404 width = (width + 1) / 2;
2405 height = (height + 1) / 2;
2407 sse = calc_plane_error(orig->u_buffer, orig->uv_stride,
2408 recon->u_buffer, recon->uv_stride,
2410 IF_RTCD(&cpi->rtcd.variance));
2411 pkt.data.psnr.sse[0] += sse;
2412 pkt.data.psnr.sse[2] = sse;
2413 pkt.data.psnr.samples[0] += width * height;
2414 pkt.data.psnr.samples[2] = width * height;
2416 sse = calc_plane_error(orig->v_buffer, orig->uv_stride,
2417 recon->v_buffer, recon->uv_stride,
2419 IF_RTCD(&cpi->rtcd.variance));
2420 pkt.data.psnr.sse[0] += sse;
2421 pkt.data.psnr.sse[3] = sse;
2422 pkt.data.psnr.samples[0] += width * height;
2423 pkt.data.psnr.samples[3] = width * height;
2425 for (i = 0; i < 4; i++)
2426 pkt.data.psnr.psnr[i] = vp8_mse2psnr(pkt.data.psnr.samples[i], 255.0,
2427 pkt.data.psnr.sse[i]);
2429 vpx_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
2433 int vp8_use_as_reference(VP8_PTR ptr, int ref_frame_flags)
2435 VP8_COMP *cpi = (VP8_COMP *)(ptr);
2437 if (ref_frame_flags > 7)
2440 cpi->ref_frame_flags = ref_frame_flags;
2443 int vp8_update_reference(VP8_PTR ptr, int ref_frame_flags)
2445 VP8_COMP *cpi = (VP8_COMP *)(ptr);
2447 if (ref_frame_flags > 7)
2450 cpi->common.refresh_golden_frame = 0;
2451 cpi->common.refresh_alt_ref_frame = 0;
2452 cpi->common.refresh_last_frame = 0;
2454 if (ref_frame_flags & VP8_LAST_FLAG)
2455 cpi->common.refresh_last_frame = 1;
2457 if (ref_frame_flags & VP8_GOLD_FLAG)
2458 cpi->common.refresh_golden_frame = 1;
2460 if (ref_frame_flags & VP8_ALT_FLAG)
2461 cpi->common.refresh_alt_ref_frame = 1;
2466 int vp8_get_reference(VP8_PTR ptr, VP8_REFFRAME ref_frame_flag, YV12_BUFFER_CONFIG *sd)
2468 VP8_COMP *cpi = (VP8_COMP *)(ptr);
2469 VP8_COMMON *cm = &cpi->common;
2472 if (ref_frame_flag == VP8_LAST_FLAG)
2473 ref_fb_idx = cm->lst_fb_idx;
2474 else if (ref_frame_flag == VP8_GOLD_FLAG)
2475 ref_fb_idx = cm->gld_fb_idx;
2476 else if (ref_frame_flag == VP8_ALT_FLAG)
2477 ref_fb_idx = cm->alt_fb_idx;
2481 vp8_yv12_copy_frame_ptr(&cm->yv12_fb[ref_fb_idx], sd);
2485 int vp8_set_reference(VP8_PTR ptr, VP8_REFFRAME ref_frame_flag, YV12_BUFFER_CONFIG *sd)
2487 VP8_COMP *cpi = (VP8_COMP *)(ptr);
2488 VP8_COMMON *cm = &cpi->common;
2492 if (ref_frame_flag == VP8_LAST_FLAG)
2493 ref_fb_idx = cm->lst_fb_idx;
2494 else if (ref_frame_flag == VP8_GOLD_FLAG)
2495 ref_fb_idx = cm->gld_fb_idx;
2496 else if (ref_frame_flag == VP8_ALT_FLAG)
2497 ref_fb_idx = cm->alt_fb_idx;
2501 vp8_yv12_copy_frame_ptr(sd, &cm->yv12_fb[ref_fb_idx]);
2505 int vp8_update_entropy(VP8_PTR comp, int update)
2507 VP8_COMP *cpi = (VP8_COMP *) comp;
2508 VP8_COMMON *cm = &cpi->common;
2509 cm->refresh_entropy_probs = update;
2516 void vp8_write_yuv_frame(const char *name, YV12_BUFFER_CONFIG *s)
2518 FILE *yuv_file = fopen(name, "ab");
2519 unsigned char *src = s->y_buffer;
2520 int h = s->y_height;
2524 fwrite(src, s->y_width, 1, yuv_file);
2534 fwrite(src, s->uv_width, 1, yuv_file);
2535 src += s->uv_stride;
2544 fwrite(src, s->uv_width, 1, yuv_file);
2545 src += s->uv_stride;
2554 static void scale_and_extend_source(YV12_BUFFER_CONFIG *sd, VP8_COMP *cpi)
2556 VP8_COMMON *cm = &cpi->common;
2558 // are we resizing the image
2559 if (cm->horiz_scale != 0 || cm->vert_scale != 0)
2561 #if CONFIG_SPATIAL_RESAMPLING
2562 int UNINITIALIZED_IS_SAFE(hr), UNINITIALIZED_IS_SAFE(hs);
2563 int UNINITIALIZED_IS_SAFE(vr), UNINITIALIZED_IS_SAFE(vs);
2566 if (cm->vert_scale == 3)
2571 Scale2Ratio(cm->horiz_scale, &hr, &hs);
2572 Scale2Ratio(cm->vert_scale, &vr, &vs);
2574 vp8_scale_frame(sd, &cpi->scaled_source, cm->temp_scale_frame.y_buffer,
2575 tmp_height, hs, hr, vs, vr, 0);
2577 vp8_yv12_extend_frame_borders(&cpi->scaled_source);
2578 cpi->Source = &cpi->scaled_source;
2584 static void resize_key_frame(VP8_COMP *cpi)
2586 #if CONFIG_SPATIAL_RESAMPLING
2587 VP8_COMMON *cm = &cpi->common;
2589 // Do we need to apply resampling for one pass cbr.
2590 // In one pass this is more limited than in two pass cbr
2591 // The test and any change is only made one per key frame sequence
2592 if (cpi->oxcf.allow_spatial_resampling && (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER))
2594 int UNINITIALIZED_IS_SAFE(hr), UNINITIALIZED_IS_SAFE(hs);
2595 int UNINITIALIZED_IS_SAFE(vr), UNINITIALIZED_IS_SAFE(vs);
2596 int new_width, new_height;
2598 // If we are below the resample DOWN watermark then scale down a notch.
2599 if (cpi->buffer_level < (cpi->oxcf.resample_down_water_mark * cpi->oxcf.optimal_buffer_level / 100))
2601 cm->horiz_scale = (cm->horiz_scale < ONETWO) ? cm->horiz_scale + 1 : ONETWO;
2602 cm->vert_scale = (cm->vert_scale < ONETWO) ? cm->vert_scale + 1 : ONETWO;
2604 // Should we now start scaling back up
2605 else if (cpi->buffer_level > (cpi->oxcf.resample_up_water_mark * cpi->oxcf.optimal_buffer_level / 100))
2607 cm->horiz_scale = (cm->horiz_scale > NORMAL) ? cm->horiz_scale - 1 : NORMAL;
2608 cm->vert_scale = (cm->vert_scale > NORMAL) ? cm->vert_scale - 1 : NORMAL;
2611 // Get the new hieght and width
2612 Scale2Ratio(cm->horiz_scale, &hr, &hs);
2613 Scale2Ratio(cm->vert_scale, &vr, &vs);
2614 new_width = ((hs - 1) + (cpi->oxcf.Width * hr)) / hs;
2615 new_height = ((vs - 1) + (cpi->oxcf.Height * vr)) / vs;
2617 // If the image size has changed we need to reallocate the buffers
2618 // and resample the source image
2619 if ((cm->Width != new_width) || (cm->Height != new_height))
2621 cm->Width = new_width;
2622 cm->Height = new_height;
2623 vp8_alloc_compressor_data(cpi);
2624 scale_and_extend_source(cpi->un_scaled_source, cpi);
2632 static void update_alt_ref_frame_stats(VP8_COMP *cpi)
2634 VP8_COMMON *cm = &cpi->common;
2636 // Select an interval before next GF or altref
2637 if (!cpi->auto_gold)
2638 cpi->frames_till_gf_update_due = cpi->goldfreq;
2640 if ((cpi->pass != 2) && cpi->frames_till_gf_update_due)
2642 cpi->current_gf_interval = cpi->frames_till_gf_update_due;
2644 // Set the bits per frame that we should try and recover in subsequent inter frames
2645 // to account for the extra GF spend... note that his does not apply for GF updates
2646 // that occur coincident with a key frame as the extra cost of key frames is dealt
2649 cpi->gf_overspend_bits += cpi->projected_frame_size;
2650 cpi->non_gf_bitrate_adjustment = cpi->gf_overspend_bits / cpi->frames_till_gf_update_due;
2653 // Update data structure that monitors level of reference to last GF
2654 vpx_memset(cpi->gf_active_flags, 1, (cm->mb_rows * cm->mb_cols));
2655 cpi->gf_active_count = cm->mb_rows * cm->mb_cols;
2657 // this frame refreshes means next frames don't unless specified by user
2658 cpi->common.frames_since_golden = 0;
2660 // Clear the alternate reference update pending flag.
2661 cpi->source_alt_ref_pending = FALSE;
2663 // Set the alternate refernce frame active flag
2664 cpi->source_alt_ref_active = TRUE;
2668 static void update_golden_frame_stats(VP8_COMP *cpi)
2670 VP8_COMMON *cm = &cpi->common;
2672 // Update the Golden frame usage counts.
2673 if (cm->refresh_golden_frame)
2675 // Select an interval before next GF
2676 if (!cpi->auto_gold)
2677 cpi->frames_till_gf_update_due = cpi->goldfreq;
2679 if ((cpi->pass != 2) && (cpi->frames_till_gf_update_due > 0))
2681 cpi->current_gf_interval = cpi->frames_till_gf_update_due;
2683 // Set the bits per frame that we should try and recover in subsequent inter frames
2684 // to account for the extra GF spend... note that his does not apply for GF updates
2685 // that occur coincident with a key frame as the extra cost of key frames is dealt
2687 if ((cm->frame_type != KEY_FRAME) && !cpi->source_alt_ref_active)
2689 // Calcluate GF bits to be recovered
2690 // Projected size - av frame bits available for inter frames for clip as a whole
2691 cpi->gf_overspend_bits += (cpi->projected_frame_size - cpi->inter_frame_target);
2694 cpi->non_gf_bitrate_adjustment = cpi->gf_overspend_bits / cpi->frames_till_gf_update_due;
2698 // Update data structure that monitors level of reference to last GF
2699 vpx_memset(cpi->gf_active_flags, 1, (cm->mb_rows * cm->mb_cols));
2700 cpi->gf_active_count = cm->mb_rows * cm->mb_cols;
2702 // this frame refreshes means next frames don't unless specified by user
2703 cm->refresh_golden_frame = 0;
2704 cpi->common.frames_since_golden = 0;
2706 //if ( cm->frame_type == KEY_FRAME )
2708 cpi->recent_ref_frame_usage[INTRA_FRAME] = 1;
2709 cpi->recent_ref_frame_usage[LAST_FRAME] = 1;
2710 cpi->recent_ref_frame_usage[GOLDEN_FRAME] = 1;
2711 cpi->recent_ref_frame_usage[ALTREF_FRAME] = 1;
2715 // // Carry a potrtion of count over to begining of next gf sequence
2716 // cpi->recent_ref_frame_usage[INTRA_FRAME] >>= 5;
2717 // cpi->recent_ref_frame_usage[LAST_FRAME] >>= 5;
2718 // cpi->recent_ref_frame_usage[GOLDEN_FRAME] >>= 5;
2719 // cpi->recent_ref_frame_usage[ALTREF_FRAME] >>= 5;
2722 // ******** Fixed Q test code only ************
2723 // If we are going to use the ALT reference for the next group of frames set a flag to say so.
2724 if (cpi->oxcf.fixed_q >= 0 &&
2725 cpi->oxcf.play_alternate && !cpi->common.refresh_alt_ref_frame)
2727 cpi->source_alt_ref_pending = TRUE;
2728 cpi->frames_till_gf_update_due = cpi->baseline_gf_interval;
2731 if (!cpi->source_alt_ref_pending)
2732 cpi->source_alt_ref_active = FALSE;
2734 // Decrement count down till next gf
2735 if (cpi->frames_till_gf_update_due > 0)
2736 cpi->frames_till_gf_update_due--;
2739 else if (!cpi->common.refresh_alt_ref_frame)
2741 // Decrement count down till next gf
2742 if (cpi->frames_till_gf_update_due > 0)
2743 cpi->frames_till_gf_update_due--;
2745 if (cpi->common.frames_till_alt_ref_frame)
2746 cpi->common.frames_till_alt_ref_frame --;
2748 cpi->common.frames_since_golden ++;
2750 if (cpi->common.frames_since_golden > 1)
2752 cpi->recent_ref_frame_usage[INTRA_FRAME] += cpi->count_mb_ref_frame_usage[INTRA_FRAME];
2753 cpi->recent_ref_frame_usage[LAST_FRAME] += cpi->count_mb_ref_frame_usage[LAST_FRAME];
2754 cpi->recent_ref_frame_usage[GOLDEN_FRAME] += cpi->count_mb_ref_frame_usage[GOLDEN_FRAME];
2755 cpi->recent_ref_frame_usage[ALTREF_FRAME] += cpi->count_mb_ref_frame_usage[ALTREF_FRAME];
2760 // This function updates the reference frame probability estimates that
2761 // will be used during mode selection
2762 static void update_rd_ref_frame_probs(VP8_COMP *cpi)
2764 VP8_COMMON *cm = &cpi->common;
2767 const int *const rfct = cpi->recent_ref_frame_usage;
2768 const int rf_intra = rfct[INTRA_FRAME];
2769 const int rf_inter = rfct[LAST_FRAME] + rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME];
2771 if (cm->frame_type == KEY_FRAME)
2773 cpi->prob_intra_coded = 255;
2774 cpi->prob_last_coded = 128;
2775 cpi->prob_gf_coded = 128;
2777 else if (!(rf_intra + rf_inter))
2779 // This is a trap in case this function is called with cpi->recent_ref_frame_usage[] blank.
2780 cpi->prob_intra_coded = 63;
2781 cpi->prob_last_coded = 128;
2782 cpi->prob_gf_coded = 128;
2786 cpi->prob_intra_coded = (rf_intra * 255) / (rf_intra + rf_inter);
2788 if (cpi->prob_intra_coded < 1)
2789 cpi->prob_intra_coded = 1;
2791 if ((cm->frames_since_golden > 0) || cpi->source_alt_ref_active)
2793 cpi->prob_last_coded = rf_inter ? (rfct[LAST_FRAME] * 255) / rf_inter : 128;
2795 if (cpi->prob_last_coded < 1)
2796 cpi->prob_last_coded = 1;
2798 cpi->prob_gf_coded = (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME])
2799 ? (rfct[GOLDEN_FRAME] * 255) / (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME]) : 128;
2801 if (cpi->prob_gf_coded < 1)
2802 cpi->prob_gf_coded = 1;
2807 const int *const rfct = cpi->count_mb_ref_frame_usage;
2808 const int rf_intra = rfct[INTRA_FRAME];
2809 const int rf_inter = rfct[LAST_FRAME] + rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME];
2811 if (cm->frame_type == KEY_FRAME)
2813 cpi->prob_intra_coded = 255;
2814 cpi->prob_last_coded = 128;
2815 cpi->prob_gf_coded = 128;
2817 else if (!(rf_intra + rf_inter))
2819 // This is a trap in case this function is called with cpi->recent_ref_frame_usage[] blank.
2820 cpi->prob_intra_coded = 63;
2821 cpi->prob_last_coded = 128;
2822 cpi->prob_gf_coded = 128;
2826 cpi->prob_intra_coded = (rf_intra * 255) / (rf_intra + rf_inter);
2828 if (cpi->prob_intra_coded < 1)
2829 cpi->prob_intra_coded = 1;
2831 cpi->prob_last_coded = rf_inter ? (rfct[LAST_FRAME] * 255) / rf_inter : 128;
2833 if (cpi->prob_last_coded < 1)
2834 cpi->prob_last_coded = 1;
2836 cpi->prob_gf_coded = (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME])
2837 ? (rfct[GOLDEN_FRAME] * 255) / (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME]) : 128;
2839 if (cpi->prob_gf_coded < 1)
2840 cpi->prob_gf_coded = 1;
2843 // update reference frame costs since we can do better than what we got last frame.
2845 if (cpi->common.refresh_alt_ref_frame)
2847 cpi->prob_intra_coded += 40;
2848 cpi->prob_last_coded = 200;
2849 cpi->prob_gf_coded = 1;
2851 else if (cpi->common.frames_since_golden == 0)
2853 cpi->prob_last_coded = 214;
2854 cpi->prob_gf_coded = 1;
2856 else if (cpi->common.frames_since_golden == 1)
2858 cpi->prob_last_coded = 192;
2859 cpi->prob_gf_coded = 220;
2861 else if (cpi->source_alt_ref_active)
2863 //int dist = cpi->common.frames_till_alt_ref_frame + cpi->common.frames_since_golden;
2864 cpi->prob_gf_coded -= 20;
2866 if (cpi->prob_gf_coded < 10)
2867 cpi->prob_gf_coded = 10;
2874 // 1 = key, 0 = inter
2875 static int decide_key_frame(VP8_COMP *cpi)
2877 VP8_COMMON *cm = &cpi->common;
2879 int code_key_frame = FALSE;
2883 if (cpi->Speed > 11)
2886 // Clear down mmx registers
2887 vp8_clear_system_state(); //__asm emms;
2889 if ((cpi->compressor_speed == 2) && (cpi->Speed >= 5) && (cpi->sf.RD == 0))
2891 double change = 1.0 * abs((int)(cpi->intra_error - cpi->last_intra_error)) / (1 + cpi->last_intra_error);
2892 double change2 = 1.0 * abs((int)(cpi->prediction_error - cpi->last_prediction_error)) / (1 + cpi->last_prediction_error);
2893 double minerror = cm->MBs * 256;
2897 if (10 * cpi->intra_error / (1 + cpi->prediction_error) < 15
2898 && cpi->prediction_error > minerror
2899 && (change > .25 || change2 > .25))
2901 FILE *f = fopen("intra_inter.stt", "a");
2903 if (cpi->prediction_error <= 0)
2904 cpi->prediction_error = 1;
2906 fprintf(f, "%d %d %d %d %14.4f\n",
2907 cm->current_video_frame,
2908 (int) cpi->prediction_error,
2909 (int) cpi->intra_error,
2910 (int)((10 * cpi->intra_error) / cpi->prediction_error),
2918 cpi->last_intra_error = cpi->intra_error;
2919 cpi->last_prediction_error = cpi->prediction_error;
2921 if (10 * cpi->intra_error / (1 + cpi->prediction_error) < 15
2922 && cpi->prediction_error > minerror
2923 && (change > .25 || change2 > .25))
2925 /*(change > 1.4 || change < .75)&& cpi->this_frame_percent_intra > cpi->last_frame_percent_intra + 3*/
2933 // If the following are true we might as well code a key frame
2934 if (((cpi->this_frame_percent_intra == 100) &&
2935 (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra + 2))) ||
2936 ((cpi->this_frame_percent_intra > 95) &&
2937 (cpi->this_frame_percent_intra >= (cpi->last_frame_percent_intra + 5))))
2939 code_key_frame = TRUE;
2941 // in addition if the following are true and this is not a golden frame then code a key frame
2942 // Note that on golden frames there often seems to be a pop in intra useage anyway hence this
2943 // restriction is designed to prevent spurious key frames. The Intra pop needs to be investigated.
2944 else if (((cpi->this_frame_percent_intra > 60) &&
2945 (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra * 2))) ||
2946 ((cpi->this_frame_percent_intra > 75) &&
2947 (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra * 3 / 2))) ||
2948 ((cpi->this_frame_percent_intra > 90) &&
2949 (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra + 10))))
2951 if (!cm->refresh_golden_frame)
2952 code_key_frame = TRUE;
2955 return code_key_frame;
2959 #if !(CONFIG_REALTIME_ONLY)
2960 static void Pass1Encode(VP8_COMP *cpi, unsigned long *size, unsigned char *dest, unsigned int *frame_flags)
2965 vp8_set_quantizer(cpi, 26);
2967 scale_and_extend_source(cpi->un_scaled_source, cpi);
2968 vp8_first_pass(cpi);
2973 void write_cx_frame_to_file(YV12_BUFFER_CONFIG *frame, int this_frame)
2981 sprintf(filename, "cx\\y%04d.raw", this_frame);
2982 yframe = fopen(filename, "wb");
2984 for (i = 0; i < frame->y_height; i++)
2985 fwrite(frame->y_buffer + i * frame->y_stride, frame->y_width, 1, yframe);
2988 sprintf(filename, "cx\\u%04d.raw", this_frame);
2989 yframe = fopen(filename, "wb");
2991 for (i = 0; i < frame->uv_height; i++)
2992 fwrite(frame->u_buffer + i * frame->uv_stride, frame->uv_width, 1, yframe);
2995 sprintf(filename, "cx\\v%04d.raw", this_frame);
2996 yframe = fopen(filename, "wb");
2998 for (i = 0; i < frame->uv_height; i++)
2999 fwrite(frame->v_buffer + i * frame->uv_stride, frame->uv_width, 1, yframe);
3004 // return of 0 means drop frame
3006 // Function to test for conditions that indeicate we should loop
3007 // back and recode a frame.
3008 static BOOL recode_loop_test( VP8_COMP *cpi,
3009 int high_limit, int low_limit,
3010 int q, int maxq, int minq )
3012 BOOL force_recode = FALSE;
3013 VP8_COMMON *cm = &cpi->common;
3015 // Is frame recode allowed at all
3016 // Yes if either recode mode 1 is selected or mode two is selcted
3017 // and the frame is a key frame. golden frame or alt_ref_frame
3018 if ( (cpi->sf.recode_loop == 1) ||
3019 ( (cpi->sf.recode_loop == 2) &&
3020 ( (cm->frame_type == KEY_FRAME) ||
3021 cm->refresh_golden_frame ||
3022 cm->refresh_alt_ref_frame ) ) )
3024 // General over and under shoot tests
3025 if ( ((cpi->projected_frame_size > high_limit) && (q < maxq)) ||
3026 ((cpi->projected_frame_size < low_limit) && (q > minq)) )
3028 force_recode = TRUE;
3030 // Special Constrained quality tests
3031 else if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY)
3033 // Undershoot and below auto cq level
3034 if ( (q > cpi->cq_target_quality) &&
3035 (cpi->projected_frame_size <
3036 ((cpi->this_frame_target * 7) >> 3)))
3038 force_recode = TRUE;
3040 // Severe undershoot and between auto and user cq level
3041 else if ( (q > cpi->oxcf.cq_level) &&
3042 (cpi->projected_frame_size < cpi->min_frame_bandwidth) &&
3043 (cpi->active_best_quality > cpi->oxcf.cq_level))
3045 force_recode = TRUE;
3046 cpi->active_best_quality = cpi->oxcf.cq_level;
3051 return force_recode;
3054 void update_reference_frames(VP8_COMMON *cm)
3056 YV12_BUFFER_CONFIG *yv12_fb = cm->yv12_fb;
3058 // At this point the new frame has been encoded.
3059 // If any buffer copy / swapping is signaled it should be done here.
3061 if (cm->frame_type == KEY_FRAME)
3063 yv12_fb[cm->new_fb_idx].flags |= VP8_GOLD_FLAG | VP8_ALT_FLAG ;
3065 yv12_fb[cm->gld_fb_idx].flags &= ~VP8_GOLD_FLAG;
3066 yv12_fb[cm->alt_fb_idx].flags &= ~VP8_ALT_FLAG;
3068 cm->alt_fb_idx = cm->gld_fb_idx = cm->new_fb_idx;
3070 else /* For non key frames */
3072 if (cm->refresh_alt_ref_frame)
3074 assert(!cm->copy_buffer_to_arf);
3076 cm->yv12_fb[cm->new_fb_idx].flags |= VP8_ALT_FLAG;
3077 cm->yv12_fb[cm->alt_fb_idx].flags &= ~VP8_ALT_FLAG;
3078 cm->alt_fb_idx = cm->new_fb_idx;
3080 else if (cm->copy_buffer_to_arf)
3082 assert(!(cm->copy_buffer_to_arf & ~0x3));
3084 if (cm->copy_buffer_to_arf == 1)
3086 if(cm->alt_fb_idx != cm->lst_fb_idx)
3088 yv12_fb[cm->lst_fb_idx].flags |= VP8_ALT_FLAG;
3089 yv12_fb[cm->alt_fb_idx].flags &= ~VP8_ALT_FLAG;
3090 cm->alt_fb_idx = cm->lst_fb_idx;
3093 else /* if (cm->copy_buffer_to_arf == 2) */
3095 if(cm->alt_fb_idx != cm->gld_fb_idx)
3097 yv12_fb[cm->gld_fb_idx].flags |= VP8_ALT_FLAG;
3098 yv12_fb[cm->alt_fb_idx].flags &= ~VP8_ALT_FLAG;
3099 cm->alt_fb_idx = cm->gld_fb_idx;
3104 if (cm->refresh_golden_frame)
3106 assert(!cm->copy_buffer_to_gf);
3108 cm->yv12_fb[cm->new_fb_idx].flags |= VP8_GOLD_FLAG;
3109 cm->yv12_fb[cm->gld_fb_idx].flags &= ~VP8_GOLD_FLAG;
3110 cm->gld_fb_idx = cm->new_fb_idx;
3112 else if (cm->copy_buffer_to_gf)
3114 assert(!(cm->copy_buffer_to_arf & ~0x3));
3116 if (cm->copy_buffer_to_gf == 1)
3118 if(cm->gld_fb_idx != cm->lst_fb_idx)
3120 yv12_fb[cm->lst_fb_idx].flags |= VP8_GOLD_FLAG;
3121 yv12_fb[cm->gld_fb_idx].flags &= ~VP8_GOLD_FLAG;
3122 cm->gld_fb_idx = cm->lst_fb_idx;
3125 else /* if (cm->copy_buffer_to_gf == 2) */
3127 if(cm->alt_fb_idx != cm->gld_fb_idx)
3129 yv12_fb[cm->alt_fb_idx].flags |= VP8_GOLD_FLAG;
3130 yv12_fb[cm->gld_fb_idx].flags &= ~VP8_GOLD_FLAG;
3131 cm->gld_fb_idx = cm->alt_fb_idx;
3137 if (cm->refresh_last_frame)
3139 cm->yv12_fb[cm->new_fb_idx].flags |= VP8_LAST_FLAG;
3140 cm->yv12_fb[cm->lst_fb_idx].flags &= ~VP8_LAST_FLAG;
3141 cm->lst_fb_idx = cm->new_fb_idx;
3145 void loopfilter_frame(VP8_COMP *cpi, VP8_COMMON *cm)
3149 cm->filter_level = 0;
3153 struct vpx_usec_timer timer;
3155 vp8_clear_system_state();
3157 vpx_usec_timer_start(&timer);
3158 if (cpi->sf.auto_filter == 0)
3159 vp8cx_pick_filter_level_fast(cpi->Source, cpi);
3162 vp8cx_pick_filter_level(cpi->Source, cpi);
3164 vpx_usec_timer_mark(&timer);
3165 cpi->time_pick_lpf += vpx_usec_timer_elapsed(&timer);
3168 #if CONFIG_MULTITHREAD
3169 if (cpi->b_multi_threaded)
3170 sem_post(&cpi->h_event_end_lpf); /* signal that we have set filter_level */
3173 if (cm->filter_level > 0)
3175 vp8cx_set_alt_lf_level(cpi, cm->filter_level);
3176 vp8_loop_filter_frame(cm, &cpi->mb.e_mbd);
3179 vp8_yv12_extend_frame_borders_ptr(cm->frame_to_show);
3184 static void update_buffer_level(VP8_COMP *cpi)
3188 /* Update the buffered average bitrate.
3190 * The buffered average bitrate tracks the bitrate over the buffer
3191 * window. Here we simulate taking a frame of average size out
3192 * of the buffer, and putting in the new frame just encoded.
3193 * It is calculated accordingly:
3195 * A = Average Bits Per Frame In The Buffer
3196 * P = New Frame Size
3197 * N = Number of bits in the buffer
3199 * We recalculate the average as so:
3200 * (N-A)*A + A*P A * (N - A + P)
3201 * A' = ------------- = ---------------
3204 * This is modeled after a the standard algorithm for a moving
3205 * average with fixed weighting (eg A' = ((N-1)*A + 1*P) / N). This makes
3206 * the step response nonlinear but consistent with expected behavior --
3207 * when A is large, the model adapts more quickly, since there are
3208 * fewer frames in the buffer and conversely when A is small there
3209 * will be more frames in the buffer so the average will adapt
3212 * TODO(jkoleszar): This may give poor step response in some situations,
3213 * for example motion following a long static section. It might be
3214 * worth experimenting more with weighting by av_per_frame_bandwidth
3215 * rather than buffered_av_per_frame_bandwidth or using a more accurate
3216 * algorithm to get faster response. Current testing showed worse results
3217 * with that setting though.
3221 /* Guard against buffered_av_per_frame_bandwidth falling to 0. Should
3222 * never happen, but without this check, it would be irrecoverable.
3224 if(cpi->buffered_av_per_frame_bandwidth == 0)
3225 cpi->buffered_av_per_frame_bandwidth = 1;
3227 tmp = cpi->oxcf.maximum_buffer_size
3228 - cpi->buffered_av_per_frame_bandwidth
3229 + cpi->projected_frame_size;
3230 tmp *= cpi->buffered_av_per_frame_bandwidth;
3231 cpi->buffered_av_per_frame_bandwidth = tmp
3232 / cpi->oxcf.maximum_buffer_size;
3234 if(cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
3236 /* In CBR mode, buffer level is synthesized from the buffered
3237 * average per-frame bandwidth to get the response characteristics
3238 * of that model, rather than using the unbounded (wrt buffer size)
3239 * bits_off_target. ie, the long term average bitrate doesn't
3240 * matter in CBR mode. If the clip is consistently undershooting
3241 * because it is very static, for example, you don't want to blow
3242 * your short term bitrate budget trying to the the long term spend
3243 * up to the target when you hit a motion section.
3245 * Instead, the ratio of buffered_av_per_frame_bandwidth to the
3246 * target av_per_frame_bandwidth is taken, scaled by
3247 * maximum_buffer_size and centered around optimal_buffer_level,
3248 * which presents the expected behavior of buffer_level for the other
3249 * parts of the rate control code which handle the targeting.
3251 * Note that this only happens after the starting_buffer_level
3252 * has passed, to give the model a chance to stabilize.
3254 if(cpi->total_actual_bits > cpi->oxcf.starting_buffer_level)
3256 tmp = (long long)cpi->buffered_av_per_frame_bandwidth
3257 * cpi->oxcf.maximum_buffer_size
3258 / cpi->av_per_frame_bandwidth;
3259 cpi->buffer_level = cpi->oxcf.maximum_buffer_size
3261 + cpi->oxcf.optimal_buffer_level;
3264 cpi->buffer_level = cpi->oxcf.optimal_buffer_level;
3266 /* Accumulate recent overshoot error.
3268 * If this frame is larger than the target, then accumulate
3269 * that error to apply as a damping factor later. Only care about
3270 * recent overshoot, so this value decays by (N-P)/N
3272 if(cpi->total_actual_bits > cpi->oxcf.starting_buffer_level)
3274 long long decayed_overshoot;
3276 decayed_overshoot = cpi->accumulated_overshoot;
3277 decayed_overshoot *= (cpi->oxcf.maximum_buffer_size
3278 - cpi->projected_frame_size);
3279 decayed_overshoot /= cpi->oxcf.maximum_buffer_size;
3280 cpi->accumulated_overshoot = decayed_overshoot;
3282 cpi->accumulated_overshoot +=
3283 (cpi->projected_frame_size > cpi->av_per_frame_bandwidth)
3284 ? cpi->projected_frame_size - cpi->av_per_frame_bandwidth
3289 cpi->buffer_level = cpi->bits_off_target;
3293 static void encode_frame_to_data_rate
3296 unsigned long *size,
3297 unsigned char *dest,
3298 unsigned int *frame_flags
3302 int frame_over_shoot_limit;
3303 int frame_under_shoot_limit;
3313 int zbin_oq_low = 0;
3316 VP8_COMMON *cm = &cpi->common;
3317 int active_worst_qchanged = FALSE;
3319 int overshoot_seen = FALSE;
3320 int undershoot_seen = FALSE;
3321 int drop_mark = cpi->oxcf.drop_frames_water_mark * cpi->oxcf.optimal_buffer_level / 100;
3322 int drop_mark75 = drop_mark * 2 / 3;
3323 int drop_mark50 = drop_mark / 4;
3324 int drop_mark25 = drop_mark / 8;
3327 // Clear down mmx registers to allow floating point in what follows
3328 vp8_clear_system_state();
3330 // Test code for segmentation of gf/arf (0,0)
3331 //segmentation_test_function((VP8_PTR) cpi);
3333 #if CONFIG_REALTIME_ONLY
3334 if(cpi->oxcf.auto_key && cm->frame_type != KEY_FRAME)
3336 if(cpi->force_next_frame_intra)
3338 cm->frame_type = KEY_FRAME; /* delayed intra frame */
3341 cpi->force_next_frame_intra = 0;
3344 // For an alt ref frame in 2 pass we skip the call to the second pass function that sets the target bandwidth
3345 #if !(CONFIG_REALTIME_ONLY)
3349 if (cpi->common.refresh_alt_ref_frame)
3351 cpi->per_frame_bandwidth = cpi->twopass.gf_bits; // Per frame bit target for the alt ref frame
3352 cpi->target_bandwidth = cpi->twopass.gf_bits * cpi->output_frame_rate; // per second target bitrate
3357 cpi->per_frame_bandwidth = (int)(cpi->target_bandwidth / cpi->output_frame_rate);
3359 // Default turn off buffer to buffer copying
3360 cm->copy_buffer_to_gf = 0;
3361 cm->copy_buffer_to_arf = 0;
3363 // Clear zbin over-quant value and mode boost values.
3364 cpi->zbin_over_quant = 0;
3365 cpi->zbin_mode_boost = 0;
3367 // Enable or disable mode based tweaking of the zbin
3368 // For 2 Pass Only used where GF/ARF prediction quality
3369 // is above a threshold
3370 cpi->zbin_mode_boost = 0;
3371 cpi->zbin_mode_boost_enabled = TRUE;
3374 if ( cpi->gfu_boost <= 400 )
3376 cpi->zbin_mode_boost_enabled = FALSE;
3380 // Current default encoder behaviour for the altref sign bias
3381 if (cpi->source_alt_ref_active)
3382 cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = 1;
3384 cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = 0;
3386 // Check to see if a key frame is signalled
3387 // For two pass with auto key frame enabled cm->frame_type may already be set, but not for one pass.
3388 if ((cm->current_video_frame == 0) ||
3389 (cm->frame_flags & FRAMEFLAGS_KEY) ||
3390 (cpi->oxcf.auto_key && (cpi->frames_since_key % cpi->key_frame_frequency == 0)))
3392 // Key frame from VFW/auto-keyframe/first frame
3393 cm->frame_type = KEY_FRAME;
3396 // Set default state for segment and mode based loop filter update flags
3397 cpi->mb.e_mbd.update_mb_segmentation_map = 0;
3398 cpi->mb.e_mbd.update_mb_segmentation_data = 0;
3399 cpi->mb.e_mbd.mode_ref_lf_delta_update = 0;
3401 // Set various flags etc to special state if it is a key frame
3402 if (cm->frame_type == KEY_FRAME)
3406 // Reset the loop filter deltas and segmentation map
3407 setup_features(cpi);
3409 // If segmentation is enabled force a map update for key frames
3410 if (cpi->mb.e_mbd.segmentation_enabled)
3412 cpi->mb.e_mbd.update_mb_segmentation_map = 1;
3413 cpi->mb.e_mbd.update_mb_segmentation_data = 1;
3416 // The alternate reference frame cannot be active for a key frame
3417 cpi->source_alt_ref_active = FALSE;
3419 // Reset the RD threshold multipliers to default of * 1 (128)
3420 for (i = 0; i < MAX_MODES; i++)
3422 cpi->rd_thresh_mult[i] = 128;
3426 // Test code for segmentation
3427 //if ( (cm->frame_type == KEY_FRAME) || ((cm->current_video_frame % 2) == 0))
3428 //if ( (cm->current_video_frame % 2) == 0 )
3429 // enable_segmentation((VP8_PTR)cpi);
3431 // disable_segmentation((VP8_PTR)cpi);
3434 // Experimental code for lagged compress and one pass
3435 // Initialise one_pass GF frames stats
3436 // Update stats used for GF selection
3437 //if ( cpi->pass == 0 )
3439 cpi->one_pass_frame_index = cm->current_video_frame % MAX_LAG_BUFFERS;
3441 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frames_so_far = 0;
3442 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_intra_error = 0.0;
3443 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_coded_error = 0.0;
3444 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_pcnt_inter = 0.0;
3445 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_pcnt_motion = 0.0;
3446 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvr = 0.0;
3447 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvr_abs = 0.0;
3448 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvc = 0.0;
3449 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvc_abs = 0.0;
3453 update_rd_ref_frame_probs(cpi);
3455 if (cpi->drop_frames_allowed)
3457 // The reset to decimation 0 is only done here for one pass.
3458 // Once it is set two pass leaves decimation on till the next kf.
3459 if ((cpi->buffer_level > drop_mark) && (cpi->decimation_factor > 0))
3460 cpi->decimation_factor --;
3462 if (cpi->buffer_level > drop_mark75 && cpi->decimation_factor > 0)
3463 cpi->decimation_factor = 1;
3465 else if (cpi->buffer_level < drop_mark25 && (cpi->decimation_factor == 2 || cpi->decimation_factor == 3))
3467 cpi->decimation_factor = 3;
3469 else if (cpi->buffer_level < drop_mark50 && (cpi->decimation_factor == 1 || cpi->decimation_factor == 2))
3471 cpi->decimation_factor = 2;
3473 else if (cpi->buffer_level < drop_mark75 && (cpi->decimation_factor == 0 || cpi->decimation_factor == 1))
3475 cpi->decimation_factor = 1;
3478 //vpx_log("Encoder: Decimation Factor: %d \n",cpi->decimation_factor);
3481 // The following decimates the frame rate according to a regular pattern (i.e. to 1/2 or 2/3 frame rate)
3482 // This can be used to help prevent buffer under-run in CBR mode. Alternatively it might be desirable in
3483 // some situations to drop frame rate but throw more bits at each frame.
3485 // Note that dropping a key frame can be problematic if spatial resampling is also active
3486 if (cpi->decimation_factor > 0)
3488 switch (cpi->decimation_factor)
3491 cpi->per_frame_bandwidth = cpi->per_frame_bandwidth * 3 / 2;
3494 cpi->per_frame_bandwidth = cpi->per_frame_bandwidth * 5 / 4;
3497 cpi->per_frame_bandwidth = cpi->per_frame_bandwidth * 5 / 4;
3501 // Note that we should not throw out a key frame (especially when spatial resampling is enabled).
3502 if ((cm->frame_type == KEY_FRAME)) // && cpi->oxcf.allow_spatial_resampling )
3504 cpi->decimation_count = cpi->decimation_factor;
3506 else if (cpi->decimation_count > 0)
3508 cpi->decimation_count --;
3509 cpi->bits_off_target += cpi->av_per_frame_bandwidth;
3510 cm->current_video_frame++;
3511 cpi->frames_since_key++;
3513 #if CONFIG_INTERNAL_STATS
3517 cpi->buffer_level = cpi->bits_off_target;
3522 cpi->decimation_count = cpi->decimation_factor;
3525 // Decide how big to make the frame
3526 if (!vp8_pick_frame_size(cpi))
3528 cm->current_video_frame++;
3529 cpi->frames_since_key++;
3533 // Reduce active_worst_allowed_q for CBR if our buffer is getting too full.
3534 // This has a knock on effect on active best quality as well.
3535 // For CBR if the buffer reaches its maximum level then we can no longer
3536 // save up bits for later frames so we might as well use them up
3537 // on the current frame.
3539 && (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) &&
3540 (cpi->buffer_level >= cpi->oxcf.optimal_buffer_level) && cpi->buffered_mode)
3542 int Adjustment = cpi->active_worst_quality / 4; // Max adjustment is 1/4
3548 if (cpi->buffer_level < cpi->oxcf.maximum_buffer_size)
3550 buff_lvl_step = (cpi->oxcf.maximum_buffer_size - cpi->oxcf.optimal_buffer_level) / Adjustment;
3553 Adjustment = (cpi->buffer_level - cpi->oxcf.optimal_buffer_level) / buff_lvl_step;
3558 cpi->active_worst_quality -= Adjustment;
3560 if(cpi->active_worst_quality < cpi->active_best_quality)
3561 cpi->active_worst_quality = cpi->active_best_quality;
3565 // Set an active best quality and if necessary active worst quality
3566 // There is some odd behaviour for one pass here that needs attention.
3567 if ( (cpi->pass == 2) || (cpi->ni_frames > 150))
3569 vp8_clear_system_state();
3571 Q = cpi->active_worst_quality;
3573 if ( cm->frame_type == KEY_FRAME )
3575 if ( cpi->pass == 2 )
3577 if (cpi->gfu_boost > 600)
3578 cpi->active_best_quality = kf_low_motion_minq[Q];
3580 cpi->active_best_quality = kf_high_motion_minq[Q];
3582 // Special case for key frames forced because we have reached
3583 // the maximum key frame interval. Here force the Q to a range
3584 // based on the ambient Q to reduce the risk of popping
3585 if ( cpi->this_key_frame_forced )
3587 if ( cpi->active_best_quality > cpi->avg_frame_qindex * 7/8)
3588 cpi->active_best_quality = cpi->avg_frame_qindex * 7/8;
3589 else if ( cpi->active_best_quality < cpi->avg_frame_qindex >> 2 )
3590 cpi->active_best_quality = cpi->avg_frame_qindex >> 2;
3593 // One pass more conservative
3595 cpi->active_best_quality = kf_high_motion_minq[Q];
3598 else if (cm->refresh_golden_frame || cpi->common.refresh_alt_ref_frame)
3600 // Use the lower of cpi->active_worst_quality and recent
3601 // average Q as basis for GF/ARF Q limit unless last frame was
3603 if ( (cpi->frames_since_key > 1) &&
3604 (cpi->avg_frame_qindex < cpi->active_worst_quality) )
3606 Q = cpi->avg_frame_qindex;
3608 if ( (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) &&
3609 (Q < cpi->oxcf.cq_level) )
3611 Q = cpi->oxcf.cq_level;
3615 if ( cpi->pass == 2 )
3617 if ( cpi->gfu_boost > 1000 )
3618 cpi->active_best_quality = gf_low_motion_minq[Q];
3619 else if ( cpi->gfu_boost < 400 )
3620 cpi->active_best_quality = gf_high_motion_minq[Q];
3622 cpi->active_best_quality = gf_mid_motion_minq[Q];
3624 // One pass more conservative
3626 cpi->active_best_quality = gf_high_motion_minq[Q];
3631 Q = cpi->auto_worst_q?
3632 cpi->active_worst_quality:cpi->avg_frame_qindex;
3634 cpi->active_best_quality = inter_minq[Q];
3636 // For the constant/constrained quality mode we dont want
3637 // the quality to rise above the cq level.
3638 if ((cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) &&
3639 (cpi->active_best_quality < cpi->cq_target_quality) )
3641 // If we are strongly undershooting the target rate in the last
3642 // frames then use the user passed in cq value not the auto
3644 if ( cpi->rolling_actual_bits < cpi->min_frame_bandwidth )
3645 cpi->active_best_quality = cpi->oxcf.cq_level;
3647 cpi->active_best_quality = cpi->cq_target_quality;
3651 // If CBR and the buffer is as full then it is reasonable to allow
3652 // higher quality on the frames to prevent bits just going to waste.
3653 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
3655 // Note that the use of >= here elliminates the risk of a devide
3656 // by 0 error in the else if clause
3657 if (cpi->buffer_level >= cpi->oxcf.maximum_buffer_size)
3658 cpi->active_best_quality = cpi->best_quality;
3660 else if (cpi->buffer_level > cpi->oxcf.optimal_buffer_level)
3662 int Fraction = ((cpi->buffer_level - cpi->oxcf.optimal_buffer_level) * 128) / (cpi->oxcf.maximum_buffer_size - cpi->oxcf.optimal_buffer_level);
3663 int min_qadjustment = ((cpi->active_best_quality - cpi->best_quality) * Fraction) / 128;
3665 cpi->active_best_quality -= min_qadjustment;
3669 // Make sure constrained quality mode limits are adhered to for the first
3670 // few frames of one pass encodes
3671 else if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY)
3673 if ( (cm->frame_type == KEY_FRAME) ||
3674 cm->refresh_golden_frame || cpi->common.refresh_alt_ref_frame )
3676 cpi->active_best_quality = cpi->best_quality;
3678 else if (cpi->active_best_quality < cpi->cq_target_quality)
3680 cpi->active_best_quality = cpi->cq_target_quality;
3684 // Clip the active best and worst quality values to limits
3685 if (cpi->active_worst_quality > cpi->worst_quality)
3686 cpi->active_worst_quality = cpi->worst_quality;
3688 if (cpi->active_best_quality < cpi->best_quality)
3689 cpi->active_best_quality = cpi->best_quality;
3690 else if (cpi->active_best_quality > cpi->active_worst_quality)
3691 cpi->active_best_quality = cpi->active_worst_quality;
3693 // Determine initial Q to try
3694 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
3695 last_zbin_oq = cpi->zbin_over_quant;
3697 // Set highest allowed value for Zbin over quant
3698 if (cm->frame_type == KEY_FRAME)
3699 zbin_oq_high = 0; //ZBIN_OQ_MAX/16
3700 else if (cm->refresh_alt_ref_frame || (cm->refresh_golden_frame && !cpi->source_alt_ref_active))
3703 zbin_oq_high = ZBIN_OQ_MAX;
3705 // Setup background Q adjustment for error resilliant mode
3706 if (cpi->cyclic_refresh_mode_enabled)
3707 cyclic_background_refresh(cpi, Q, 0);
3709 vp8_compute_frame_size_bounds(cpi, &frame_under_shoot_limit, &frame_over_shoot_limit);
3711 // Limit Q range for the adaptive loop.
3712 bottom_index = cpi->active_best_quality;
3713 top_index = cpi->active_worst_quality;
3714 q_low = cpi->active_best_quality;
3715 q_high = cpi->active_worst_quality;
3717 vp8_save_coding_context(cpi);
3722 scale_and_extend_source(cpi->un_scaled_source, cpi);
3723 #if !(CONFIG_REALTIME_ONLY) && CONFIG_POSTPROC
3725 if (cpi->oxcf.noise_sensitivity > 0)
3730 switch (cpi->oxcf.noise_sensitivity)
3753 if (cm->frame_type == KEY_FRAME)
3755 vp8_de_noise(cpi->Source, cpi->Source, l , 1, 0, RTCD(postproc));
3759 vp8_de_noise(cpi->Source, cpi->Source, l , 1, 0, RTCD(postproc));
3761 src = cpi->Source->y_buffer;
3763 if (cpi->Source->y_stride < 0)
3765 src += cpi->Source->y_stride * (cpi->Source->y_height - 1);
3772 #ifdef OUTPUT_YUV_SRC
3773 vp8_write_yuv_frame(cpi->Source);
3778 vp8_clear_system_state(); //__asm emms;
3781 if(cpi->is_src_frame_alt_ref)
3785 vp8_set_quantizer(cpi, Q);
3788 // setup skip prob for costing in mode/mv decision
3789 if (cpi->common.mb_no_coeff_skip)
3791 cpi->prob_skip_false = cpi->base_skip_false_prob[Q];
3793 if (cm->frame_type != KEY_FRAME)
3795 if (cpi->common.refresh_alt_ref_frame)
3797 if (cpi->last_skip_false_probs[2] != 0)
3798 cpi->prob_skip_false = cpi->last_skip_false_probs[2];
3801 if(cpi->last_skip_false_probs[2]!=0 && abs(Q- cpi->last_skip_probs_q[2])<=16 )
3802 cpi->prob_skip_false = cpi->last_skip_false_probs[2];
3803 else if (cpi->last_skip_false_probs[2]!=0)
3804 cpi->prob_skip_false = (cpi->last_skip_false_probs[2] + cpi->prob_skip_false ) / 2;
3807 else if (cpi->common.refresh_golden_frame)
3809 if (cpi->last_skip_false_probs[1] != 0)
3810 cpi->prob_skip_false = cpi->last_skip_false_probs[1];
3813 if(cpi->last_skip_false_probs[1]!=0 && abs(Q- cpi->last_skip_probs_q[1])<=16 )
3814 cpi->prob_skip_false = cpi->last_skip_false_probs[1];
3815 else if (cpi->last_skip_false_probs[1]!=0)
3816 cpi->prob_skip_false = (cpi->last_skip_false_probs[1] + cpi->prob_skip_false ) / 2;
3821 if (cpi->last_skip_false_probs[0] != 0)
3822 cpi->prob_skip_false = cpi->last_skip_false_probs[0];
3825 if(cpi->last_skip_false_probs[0]!=0 && abs(Q- cpi->last_skip_probs_q[0])<=16 )
3826 cpi->prob_skip_false = cpi->last_skip_false_probs[0];
3827 else if(cpi->last_skip_false_probs[0]!=0)
3828 cpi->prob_skip_false = (cpi->last_skip_false_probs[0] + cpi->prob_skip_false ) / 2;
3832 //as this is for cost estimate, let's make sure it does not go extreme eitehr way
3833 if (cpi->prob_skip_false < 5)
3834 cpi->prob_skip_false = 5;
3836 if (cpi->prob_skip_false > 250)
3837 cpi->prob_skip_false = 250;
3839 if (cpi->is_src_frame_alt_ref)
3840 cpi->prob_skip_false = 1;
3849 FILE *f = fopen("skip.stt", "a");
3850 fprintf(f, "%d, %d, %4d ", cpi->common.refresh_golden_frame, cpi->common.refresh_alt_ref_frame, cpi->prob_skip_false);
3858 if (cm->frame_type == KEY_FRAME)
3860 resize_key_frame(cpi);
3861 vp8_setup_key_frame(cpi);
3864 // transform / motion compensation build reconstruction frame
3865 vp8_encode_frame(cpi);
3867 cpi->projected_frame_size -= vp8_estimate_entropy_savings(cpi);
3868 cpi->projected_frame_size = (cpi->projected_frame_size > 0) ? cpi->projected_frame_size : 0;
3870 vp8_clear_system_state(); //__asm emms;
3872 // Test to see if the stats generated for this frame indicate that we should have coded a key frame
3873 // (assuming that we didn't)!
3874 if (cpi->pass != 2 && cpi->oxcf.auto_key && cm->frame_type != KEY_FRAME)
3877 #if CONFIG_REALTIME_ONLY
3879 /* we don't do re-encoding in realtime mode
3880 * if key frame is decided than we force it on next frame */
3881 cpi->force_next_frame_intra = decide_key_frame(cpi);
3884 if (decide_key_frame(cpi))
3886 // Reset all our sizing numbers and recode
3887 cm->frame_type = KEY_FRAME;
3889 vp8_pick_frame_size(cpi);
3891 // Clear the Alt reference frame active flag when we have a key frame
3892 cpi->source_alt_ref_active = FALSE;
3894 // Reset the loop filter deltas and segmentation map
3895 setup_features(cpi);
3897 // If segmentation is enabled force a map update for key frames
3898 if (cpi->mb.e_mbd.segmentation_enabled)
3900 cpi->mb.e_mbd.update_mb_segmentation_map = 1;
3901 cpi->mb.e_mbd.update_mb_segmentation_data = 1;
3904 vp8_restore_coding_context(cpi);
3906 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
3908 vp8_compute_frame_size_bounds(cpi, &frame_under_shoot_limit, &frame_over_shoot_limit);
3910 // Limit Q range for the adaptive loop.
3911 bottom_index = cpi->active_best_quality;
3912 top_index = cpi->active_worst_quality;
3913 q_low = cpi->active_best_quality;
3914 q_high = cpi->active_worst_quality;
3924 vp8_clear_system_state();
3926 if (frame_over_shoot_limit == 0)
3927 frame_over_shoot_limit = 1;
3929 // Are we are overshooting and up against the limit of active max Q.
3930 if (((cpi->pass != 2) || (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)) &&
3931 (Q == cpi->active_worst_quality) &&
3932 (cpi->active_worst_quality < cpi->worst_quality) &&
3933 (cpi->projected_frame_size > frame_over_shoot_limit))
3935 /* step down active_worst_quality such that the corresponding
3936 * active_best_quality will be equal to the current
3937 * active_worst_quality + 1. Once the limit on active_best_quality
3938 * is reached, active_worst_quality will equal worst_quality.
3942 for(i=cpi->active_worst_quality; i<cpi->worst_quality; i++)
3943 if(inter_minq[i] >= cpi->active_worst_quality + 1)
3945 cpi->active_worst_quality = i;
3947 // If we have updated the active max Q do not call vp8_update_rate_correction_factors() this loop.
3948 active_worst_qchanged = TRUE;
3951 active_worst_qchanged = FALSE;
3953 #if !(CONFIG_REALTIME_ONLY)
3954 // Special case handling for forced key frames
3955 if ( (cm->frame_type == KEY_FRAME) && cpi->this_key_frame_forced )
3958 int kf_err = vp8_calc_ss_err(cpi->Source,
3959 &cm->yv12_fb[cm->new_fb_idx],
3960 IF_RTCD(&cpi->rtcd.variance));
3962 // The key frame is not good enough
3963 if ( kf_err > ((cpi->ambient_err * 7) >> 3) )
3966 q_high = (Q > q_low) ? (Q - 1) : q_low;
3969 Q = (q_high + q_low) >> 1;
3971 // The key frame is much better than the previous frame
3972 else if ( kf_err < (cpi->ambient_err >> 1) )
3975 q_low = (Q < q_high) ? (Q + 1) : q_high;
3978 Q = (q_high + q_low + 1) >> 1;
3981 // Clamp Q to upper and lower limits:
3987 Loop = ((Q != last_q)) ? TRUE : FALSE;
3990 // Is the projected frame size out of range and are we allowed to attempt to recode.
3991 else if ( recode_loop_test( cpi,
3992 frame_over_shoot_limit, frame_under_shoot_limit,
3993 Q, top_index, bottom_index ) )
3998 // Frame size out of permitted range:
3999 // Update correction factor & compute new Q to try...
4001 // Frame is too large
4002 if (cpi->projected_frame_size > cpi->this_frame_target)
4004 //if ( cpi->zbin_over_quant == 0 )
4005 q_low = (Q < q_high) ? (Q + 1) : q_high; // Raise Qlow as to at least the current value
4007 if (cpi->zbin_over_quant > 0) // If we are using over quant do the same for zbin_oq_low
4008 zbin_oq_low = (cpi->zbin_over_quant < zbin_oq_high) ? (cpi->zbin_over_quant + 1) : zbin_oq_high;
4010 //if ( undershoot_seen || (Q == MAXQ) )
4011 if (undershoot_seen)
4013 // Update rate_correction_factor unless cpi->active_worst_quality has changed.
4014 if (!active_worst_qchanged)
4015 vp8_update_rate_correction_factors(cpi, 1);
4017 Q = (q_high + q_low + 1) / 2;
4019 // Adjust cpi->zbin_over_quant (only allowed when Q is max)
4021 cpi->zbin_over_quant = 0;
4024 zbin_oq_low = (cpi->zbin_over_quant < zbin_oq_high) ? (cpi->zbin_over_quant + 1) : zbin_oq_high;
4025 cpi->zbin_over_quant = (zbin_oq_high + zbin_oq_low) / 2;
4030 // Update rate_correction_factor unless cpi->active_worst_quality has changed.
4031 if (!active_worst_qchanged)
4032 vp8_update_rate_correction_factors(cpi, 0);
4034 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4036 while (((Q < q_low) || (cpi->zbin_over_quant < zbin_oq_low)) && (Retries < 10))
4038 vp8_update_rate_correction_factors(cpi, 0);
4039 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4044 overshoot_seen = TRUE;
4046 // Frame is too small
4049 if (cpi->zbin_over_quant == 0)
4050 q_high = (Q > q_low) ? (Q - 1) : q_low; // Lower q_high if not using over quant
4051 else // else lower zbin_oq_high
4052 zbin_oq_high = (cpi->zbin_over_quant > zbin_oq_low) ? (cpi->zbin_over_quant - 1) : zbin_oq_low;
4056 // Update rate_correction_factor unless cpi->active_worst_quality has changed.
4057 if (!active_worst_qchanged)
4058 vp8_update_rate_correction_factors(cpi, 1);
4060 Q = (q_high + q_low) / 2;
4062 // Adjust cpi->zbin_over_quant (only allowed when Q is max)
4064 cpi->zbin_over_quant = 0;
4066 cpi->zbin_over_quant = (zbin_oq_high + zbin_oq_low) / 2;
4070 // Update rate_correction_factor unless cpi->active_worst_quality has changed.
4071 if (!active_worst_qchanged)
4072 vp8_update_rate_correction_factors(cpi, 0);
4074 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4076 // Special case reset for qlow for constrained quality.
4077 // This should only trigger where there is very substantial
4078 // undershoot on a frame and the auto cq level is above
4079 // the user passsed in value.
4080 if ( (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) &&
4086 while (((Q > q_high) || (cpi->zbin_over_quant > zbin_oq_high)) && (Retries < 10))
4088 vp8_update_rate_correction_factors(cpi, 0);
4089 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4094 undershoot_seen = TRUE;
4097 // Clamp Q to upper and lower limits:
4103 // Clamp cpi->zbin_over_quant
4104 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;
4106 //Loop = ((Q != last_q) || (last_zbin_oq != cpi->zbin_over_quant)) ? TRUE : FALSE;
4107 Loop = ((Q != last_q)) ? TRUE : FALSE;
4108 last_zbin_oq = cpi->zbin_over_quant;
4114 if (cpi->is_src_frame_alt_ref)
4119 vp8_restore_coding_context(cpi);
4121 #if CONFIG_INTERNAL_STATS
4122 cpi->tot_recode_hits++;
4126 while (Loop == TRUE);
4129 // Experimental code for lagged and one pass
4130 // Update stats used for one pass GF selection
4134 double frame_intra_error;
4135 double frame_coded_error;
4136 double frame_pcnt_inter;
4137 double frame_pcnt_motion;
4139 double frame_mvr_abs;
4141 double frame_mvc_abs;
4144 cpi->one_pass_frame_stats[cpi->one_pass_frame_index].frame_coded_error = (double)cpi->prediction_error;
4145 cpi->one_pass_frame_stats[cpi->one_pass_frame_index].frame_intra_error = (double)cpi->intra_error;
4146 cpi->one_pass_frame_stats[cpi->one_pass_frame_index].frame_pcnt_inter = (double)(100 - cpi->this_frame_percent_intra) / 100.0;
4150 // Special case code to reduce pulsing when key frames are forced at a
4151 // fixed interval. Note the reconstruction error if it is the frame before
4152 // the force key frame
4153 if ( cpi->next_key_frame_forced && (cpi->twopass.frames_to_key == 0) )
4155 cpi->ambient_err = vp8_calc_ss_err(cpi->Source,
4156 &cm->yv12_fb[cm->new_fb_idx],
4157 IF_RTCD(&cpi->rtcd.variance));
4160 // This frame's MVs are saved and will be used in next frame's MV prediction.
4161 // Last frame has one more line(add to bottom) and one more column(add to right) than cm->mip. The edge elements are initialized to 0.
4162 if(cm->show_frame) //do not save for altref frame
4166 MODE_INFO *tmp = cm->mip; //point to beginning of allocated MODE_INFO arrays.
4168 if(cm->frame_type != KEY_FRAME)
4170 for (mb_row = 0; mb_row < cm->mb_rows+1; mb_row ++)
4172 for (mb_col = 0; mb_col < cm->mb_cols+1; mb_col ++)
4174 if(tmp->mbmi.ref_frame != INTRA_FRAME)
4175 cpi->lfmv[mb_col + mb_row*(cm->mode_info_stride+1)].as_int = tmp->mbmi.mv.as_int;
4177 cpi->lf_ref_frame_sign_bias[mb_col + mb_row*(cm->mode_info_stride+1)] = cm->ref_frame_sign_bias[tmp->mbmi.ref_frame];
4178 cpi->lf_ref_frame[mb_col + mb_row*(cm->mode_info_stride+1)] = tmp->mbmi.ref_frame;
4185 // Update the GF useage maps.
4186 // This is done after completing the compression of a frame when all modes etc. are finalized but before loop filter
4187 // This is done after completing the compression of a frame when all modes etc. are finalized but before loop filter
4188 vp8_update_gf_useage_maps(cpi, cm, &cpi->mb);
4190 if (cm->frame_type == KEY_FRAME)
4191 cm->refresh_last_frame = 1;
4195 FILE *f = fopen("gfactive.stt", "a");
4196 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);
4201 // For inter frames the current default behavior is that when
4202 // cm->refresh_golden_frame is set we copy the old GF over to the ARF buffer
4203 // This is purely an encoder decision at present.
4204 if (!cpi->oxcf.error_resilient_mode && cm->refresh_golden_frame)
4205 cm->copy_buffer_to_arf = 2;
4207 cm->copy_buffer_to_arf = 0;
4209 cm->frame_to_show = &cm->yv12_fb[cm->new_fb_idx];
4211 #if CONFIG_MULTITHREAD
4212 if (cpi->b_multi_threaded)
4214 sem_post(&cpi->h_event_start_lpf); /* start loopfilter in separate thread */
4219 loopfilter_frame(cpi, cm);
4222 update_reference_frames(cm);
4224 if (cpi->oxcf.error_resilient_mode)
4226 cm->refresh_entropy_probs = 0;
4229 #if CONFIG_MULTITHREAD
4230 /* wait that filter_level is picked so that we can continue with stream packing */
4231 if (cpi->b_multi_threaded)
4232 sem_wait(&cpi->h_event_end_lpf);
4235 // build the bitstream
4236 vp8_pack_bitstream(cpi, dest, size);
4238 #if CONFIG_MULTITHREAD
4239 /* wait for loopfilter thread done */
4240 if (cpi->b_multi_threaded)
4242 sem_wait(&cpi->h_event_end_lpf);
4246 /* Move storing frame_type out of the above loop since it is also
4247 * needed in motion search besides loopfilter */
4248 cm->last_frame_type = cm->frame_type;
4250 // Update rate control heuristics
4251 cpi->total_byte_count += (*size);
4252 cpi->projected_frame_size = (*size) << 3;
4254 if (!active_worst_qchanged)
4255 vp8_update_rate_correction_factors(cpi, 2);
4257 cpi->last_q[cm->frame_type] = cm->base_qindex;
4259 if (cm->frame_type == KEY_FRAME)
4261 vp8_adjust_key_frame_context(cpi);
4264 // Keep a record of ambient average Q.
4265 if (cm->frame_type != KEY_FRAME)
4266 cpi->avg_frame_qindex = (2 + 3 * cpi->avg_frame_qindex + cm->base_qindex) >> 2;
4268 // Keep a record from which we can calculate the average Q excluding GF updates and key frames
4269 if ((cm->frame_type != KEY_FRAME) && !cm->refresh_golden_frame && !cm->refresh_alt_ref_frame)
4273 // Calculate the average Q for normal inter frames (not key or GFU
4275 if ( cpi->pass == 2 )
4277 cpi->ni_tot_qi += Q;
4278 cpi->ni_av_qi = (cpi->ni_tot_qi / cpi->ni_frames);
4282 // Damp value for first few frames
4283 if (cpi->ni_frames > 150 )
4285 cpi->ni_tot_qi += Q;
4286 cpi->ni_av_qi = (cpi->ni_tot_qi / cpi->ni_frames);
4288 // For one pass, early in the clip ... average the current frame Q
4289 // value with the worstq entered by the user as a dampening measure
4292 cpi->ni_tot_qi += Q;
4293 cpi->ni_av_qi = ((cpi->ni_tot_qi / cpi->ni_frames) + cpi->worst_quality + 1) / 2;
4296 // If the average Q is higher than what was used in the last frame
4297 // (after going through the recode loop to keep the frame size within range)
4298 // then use the last frame value - 1.
4299 // The -1 is designed to stop Q and hence the data rate, from progressively
4300 // falling away during difficult sections, but at the same time reduce the number of
4301 // itterations around the recode loop.
4302 if (Q > cpi->ni_av_qi)
4303 cpi->ni_av_qi = Q - 1;
4309 // If the frame was massively oversize and we are below optimal buffer level drop next frame
4310 if ((cpi->drop_frames_allowed) &&
4311 (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) &&
4312 (cpi->buffer_level < cpi->oxcf.drop_frames_water_mark * cpi->oxcf.optimal_buffer_level / 100) &&
4313 (cpi->projected_frame_size > (4 * cpi->this_frame_target)))
4315 cpi->drop_frame = TRUE;
4320 // Set the count for maximum consequative dropped frames based upon the ratio of
4321 // this frame size to the target average per frame bandwidth.
4322 // (cpi->av_per_frame_bandwidth > 0) is just a sanity check to prevent / 0.
4323 if (cpi->drop_frames_allowed && (cpi->av_per_frame_bandwidth > 0))
4325 cpi->max_drop_count = cpi->projected_frame_size / cpi->av_per_frame_bandwidth;
4327 if (cpi->max_drop_count > cpi->max_consec_dropped_frames)
4328 cpi->max_drop_count = cpi->max_consec_dropped_frames;
4331 // Update the buffer level variable.
4332 // Non-viewable frames are a special case and are treated as pure overhead.
4333 if ( cm->show_frame )
4334 cpi->bits_off_target += cpi->av_per_frame_bandwidth;
4335 cpi->bits_off_target -= cpi->projected_frame_size;
4337 // Rolling monitors of whether we are over or underspending used to help regulate min and Max Q in two pass.
4338 cpi->rolling_target_bits = ((cpi->rolling_target_bits * 3) + cpi->this_frame_target + 2) / 4;
4339 cpi->rolling_actual_bits = ((cpi->rolling_actual_bits * 3) + cpi->projected_frame_size + 2) / 4;
4340 cpi->long_rolling_target_bits = ((cpi->long_rolling_target_bits * 31) + cpi->this_frame_target + 16) / 32;
4341 cpi->long_rolling_actual_bits = ((cpi->long_rolling_actual_bits * 31) + cpi->projected_frame_size + 16) / 32;
4343 // Actual bits spent
4344 cpi->total_actual_bits += cpi->projected_frame_size;
4347 cpi->total_target_vs_actual += (cpi->this_frame_target - cpi->projected_frame_size);
4349 update_buffer_level(cpi);
4351 // Update bits left to the kf and gf groups to account for overshoot or undershoot on these frames
4352 if (cm->frame_type == KEY_FRAME)
4354 cpi->twopass.kf_group_bits += cpi->this_frame_target - cpi->projected_frame_size;
4356 if (cpi->twopass.kf_group_bits < 0)
4357 cpi->twopass.kf_group_bits = 0 ;
4359 else if (cm->refresh_golden_frame || cm->refresh_alt_ref_frame)
4361 cpi->twopass.gf_group_bits += cpi->this_frame_target - cpi->projected_frame_size;
4363 if (cpi->twopass.gf_group_bits < 0)
4364 cpi->twopass.gf_group_bits = 0 ;
4367 if (cm->frame_type != KEY_FRAME)
4369 if (cpi->common.refresh_alt_ref_frame)
4371 cpi->last_skip_false_probs[2] = cpi->prob_skip_false;
4372 cpi->last_skip_probs_q[2] = cm->base_qindex;
4374 else if (cpi->common.refresh_golden_frame)
4376 cpi->last_skip_false_probs[1] = cpi->prob_skip_false;
4377 cpi->last_skip_probs_q[1] = cm->base_qindex;
4381 cpi->last_skip_false_probs[0] = cpi->prob_skip_false;
4382 cpi->last_skip_probs_q[0] = cm->base_qindex;
4384 //update the baseline
4385 cpi->base_skip_false_prob[cm->base_qindex] = cpi->prob_skip_false;
4390 #if 0 && CONFIG_INTERNAL_STATS
4392 FILE *f = fopen("tmp.stt", "a");
4394 vp8_clear_system_state(); //__asm emms;
4396 if (cpi->twopass.total_coded_error_left != 0.0)
4397 fprintf(f, "%10d %10d %10d %10d %10d %10d %10d %10d %6d %6d"
4398 "%6d %6d %6d %5d %5d %5d %8d %8.2f %10d %10.3f"
4400 cpi->common.current_video_frame, cpi->this_frame_target,
4401 cpi->projected_frame_size,
4402 (cpi->projected_frame_size - cpi->this_frame_target),
4403 (int)cpi->total_target_vs_actual,
4404 (cpi->oxcf.starting_buffer_level-cpi->bits_off_target),
4405 (int)cpi->total_actual_bits, cm->base_qindex,
4406 cpi->active_best_quality, cpi->active_worst_quality,
4407 cpi->ni_av_qi, cpi->cq_target_quality, cpi->zbin_over_quant,
4408 //cpi->avg_frame_qindex, cpi->zbin_over_quant,
4409 cm->refresh_golden_frame, cm->refresh_alt_ref_frame,
4410 cm->frame_type, cpi->gfu_boost,
4411 cpi->twopass.est_max_qcorrection_factor, (int)cpi->twopass.bits_left,
4412 cpi->twopass.total_coded_error_left,
4413 (double)cpi->twopass.bits_left / cpi->twopass.total_coded_error_left,
4414 cpi->tot_recode_hits);
4416 fprintf(f, "%10d %10d %10d %10d %10d %10d %10d %10d %6d %6d"
4417 "%6d %6d %6d %5d %5d %5d %8d %8.2f %10d %10.3f"
4419 cpi->common.current_video_frame,
4420 cpi->this_frame_target, cpi->projected_frame_size,
4421 (cpi->projected_frame_size - cpi->this_frame_target),
4422 (int)cpi->total_target_vs_actual,
4423 (cpi->oxcf.starting_buffer_level-cpi->bits_off_target),
4424 (int)cpi->total_actual_bits, cm->base_qindex,
4425 cpi->active_best_quality, cpi->active_worst_quality,
4426 cpi->ni_av_qi, cpi->cq_target_quality, cpi->zbin_over_quant,
4427 //cpi->avg_frame_qindex, cpi->zbin_over_quant,
4428 cm->refresh_golden_frame, cm->refresh_alt_ref_frame,
4429 cm->frame_type, cpi->gfu_boost,
4430 cpi->twopass.est_max_qcorrection_factor, (int)cpi->twopass.bits_left,
4431 cpi->twopass.total_coded_error_left, cpi->tot_recode_hits);
4436 FILE *fmodes = fopen("Modes.stt", "a");
4439 fprintf(fmodes, "%6d:%1d:%1d:%1d ",
4440 cpi->common.current_video_frame,
4441 cm->frame_type, cm->refresh_golden_frame,
4442 cm->refresh_alt_ref_frame);
4444 for (i = 0; i < MAX_MODES; i++)
4445 fprintf(fmodes, "%5d ", cpi->mode_chosen_counts[i]);
4447 fprintf(fmodes, "\n");
4455 // If this was a kf or Gf note the Q
4456 if ((cm->frame_type == KEY_FRAME) || cm->refresh_golden_frame || cm->refresh_alt_ref_frame)
4457 cm->last_kf_gf_q = cm->base_qindex;
4459 if (cm->refresh_golden_frame == 1)
4460 cm->frame_flags = cm->frame_flags | FRAMEFLAGS_GOLDEN;
4462 cm->frame_flags = cm->frame_flags&~FRAMEFLAGS_GOLDEN;
4464 if (cm->refresh_alt_ref_frame == 1)
4465 cm->frame_flags = cm->frame_flags | FRAMEFLAGS_ALTREF;
4467 cm->frame_flags = cm->frame_flags&~FRAMEFLAGS_ALTREF;
4470 if (cm->refresh_last_frame & cm->refresh_golden_frame) // both refreshed
4471 cpi->gold_is_last = 1;
4472 else if (cm->refresh_last_frame ^ cm->refresh_golden_frame) // 1 refreshed but not the other
4473 cpi->gold_is_last = 0;
4475 if (cm->refresh_last_frame & cm->refresh_alt_ref_frame) // both refreshed
4476 cpi->alt_is_last = 1;
4477 else if (cm->refresh_last_frame ^ cm->refresh_alt_ref_frame) // 1 refreshed but not the other
4478 cpi->alt_is_last = 0;
4480 if (cm->refresh_alt_ref_frame & cm->refresh_golden_frame) // both refreshed
4481 cpi->gold_is_alt = 1;
4482 else if (cm->refresh_alt_ref_frame ^ cm->refresh_golden_frame) // 1 refreshed but not the other
4483 cpi->gold_is_alt = 0;
4485 cpi->ref_frame_flags = VP8_ALT_FLAG | VP8_GOLD_FLAG | VP8_LAST_FLAG;
4487 if (cpi->gold_is_last)
4488 cpi->ref_frame_flags &= ~VP8_GOLD_FLAG;
4490 if (cpi->alt_is_last)
4491 cpi->ref_frame_flags &= ~VP8_ALT_FLAG;
4493 if (cpi->gold_is_alt)
4494 cpi->ref_frame_flags &= ~VP8_ALT_FLAG;
4497 if (!cpi->oxcf.error_resilient_mode)
4499 if (cpi->oxcf.play_alternate && cm->refresh_alt_ref_frame && (cm->frame_type != KEY_FRAME))
4500 // Update the alternate reference frame stats as appropriate.
4501 update_alt_ref_frame_stats(cpi);
4503 // Update the Golden frame stats as appropriate.
4504 update_golden_frame_stats(cpi);
4507 if (cm->frame_type == KEY_FRAME)
4509 // Tell the caller that the frame was coded as a key frame
4510 *frame_flags = cm->frame_flags | FRAMEFLAGS_KEY;
4512 // As this frame is a key frame the next defaults to an inter frame.
4513 cm->frame_type = INTER_FRAME;
4515 cpi->last_frame_percent_intra = 100;
4519 *frame_flags = cm->frame_flags&~FRAMEFLAGS_KEY;
4521 cpi->last_frame_percent_intra = cpi->this_frame_percent_intra;
4524 // Clear the one shot update flags for segmentation map and mode/ref loop filter deltas.
4525 cpi->mb.e_mbd.update_mb_segmentation_map = 0;
4526 cpi->mb.e_mbd.update_mb_segmentation_data = 0;
4527 cpi->mb.e_mbd.mode_ref_lf_delta_update = 0;
4530 // Dont increment frame counters if this was an altref buffer update not a real frame
4533 cm->current_video_frame++;
4534 cpi->frames_since_key++;
4537 // reset to normal state now that we are done.
4545 sprintf(filename, "enc%04d.yuv", (int) cm->current_video_frame);
4546 recon_file = fopen(filename, "wb");
4547 fwrite(cm->yv12_fb[cm->lst_fb_idx].buffer_alloc,
4548 cm->yv12_fb[cm->lst_fb_idx].frame_size, 1, recon_file);
4554 //vp8_write_yuv_frame("encoder_recon.yuv", cm->frame_to_show);
4560 static void check_gf_quality(VP8_COMP *cpi)
4562 VP8_COMMON *cm = &cpi->common;
4563 int gf_active_pct = (100 * cpi->gf_active_count) / (cm->mb_rows * cm->mb_cols);
4564 int gf_ref_usage_pct = (cpi->count_mb_ref_frame_usage[GOLDEN_FRAME] * 100) / (cm->mb_rows * cm->mb_cols);
4565 int last_ref_zz_useage = (cpi->inter_zz_count * 100) / (cm->mb_rows * cm->mb_cols);
4567 // Gf refresh is not currently being signalled
4568 if (cpi->gf_update_recommended == 0)
4570 if (cpi->common.frames_since_golden > 7)
4573 if ((gf_active_pct < 10) || ((gf_active_pct + gf_ref_usage_pct) < 15))
4575 // ...but last frame zero zero usage is reasonbable so a new gf might be appropriate
4576 if (last_ref_zz_useage >= 25)
4578 cpi->gf_bad_count ++;
4580 if (cpi->gf_bad_count >= 8) // Check that the condition is stable
4582 cpi->gf_update_recommended = 1;
4583 cpi->gf_bad_count = 0;
4587 cpi->gf_bad_count = 0; // Restart count as the background is not stable enough
4590 cpi->gf_bad_count = 0; // Gf useage has picked up so reset count
4593 // If the signal is set but has not been read should we cancel it.
4594 else if (last_ref_zz_useage < 15)
4596 cpi->gf_update_recommended = 0;
4597 cpi->gf_bad_count = 0;
4602 FILE *f = fopen("gfneeded.stt", "a");
4603 fprintf(f, "%10d %10d %10d %10d %10ld \n",
4604 cm->current_video_frame,
4605 cpi->common.frames_since_golden,
4606 gf_active_pct, gf_ref_usage_pct,
4607 cpi->gf_update_recommended);
4614 #if !(CONFIG_REALTIME_ONLY)
4615 static void Pass2Encode(VP8_COMP *cpi, unsigned long *size, unsigned char *dest, unsigned int *frame_flags)
4618 if (!cpi->common.refresh_alt_ref_frame)
4619 vp8_second_pass(cpi);
4621 encode_frame_to_data_rate(cpi, size, dest, frame_flags);
4622 cpi->twopass.bits_left -= 8 * *size;
4624 if (!cpi->common.refresh_alt_ref_frame)
4626 double two_pass_min_rate = (double)(cpi->oxcf.target_bandwidth
4627 *cpi->oxcf.two_pass_vbrmin_section / 100);
4628 cpi->twopass.bits_left += (long long)(two_pass_min_rate / cpi->oxcf.frame_rate);
4633 //For ARM NEON, d8-d15 are callee-saved registers, and need to be saved by us.
4635 extern void vp8_push_neon(INT64 *store);
4636 extern void vp8_pop_neon(INT64 *store);
4640 int vp8_receive_raw_frame(VP8_PTR ptr, unsigned int frame_flags, YV12_BUFFER_CONFIG *sd, INT64 time_stamp, INT64 end_time)
4645 VP8_COMP *cpi = (VP8_COMP *) ptr;
4646 VP8_COMMON *cm = &cpi->common;
4647 struct vpx_usec_timer timer;
4651 #if CONFIG_RUNTIME_CPU_DETECT
4652 if (cm->rtcd.flags & HAS_NEON)
4655 vp8_push_neon(store_reg);
4659 vpx_usec_timer_start(&timer);
4660 if(vp8_lookahead_push(cpi->lookahead, sd, time_stamp, end_time,
4663 cm->clr_type = sd->clrtype;
4664 vpx_usec_timer_mark(&timer);
4665 cpi->time_receive_data += vpx_usec_timer_elapsed(&timer);
4668 #if CONFIG_RUNTIME_CPU_DETECT
4669 if (cm->rtcd.flags & HAS_NEON)
4672 vp8_pop_neon(store_reg);
4680 static int frame_is_reference(const VP8_COMP *cpi)
4682 const VP8_COMMON *cm = &cpi->common;
4683 const MACROBLOCKD *xd = &cpi->mb.e_mbd;
4685 return cm->frame_type == KEY_FRAME || cm->refresh_last_frame
4686 || cm->refresh_golden_frame || cm->refresh_alt_ref_frame
4687 || cm->copy_buffer_to_gf || cm->copy_buffer_to_arf
4688 || cm->refresh_entropy_probs
4689 || xd->mode_ref_lf_delta_update
4690 || xd->update_mb_segmentation_map || xd->update_mb_segmentation_data;
4694 int vp8_get_compressed_data(VP8_PTR ptr, unsigned int *frame_flags, unsigned long *size, unsigned char *dest, INT64 *time_stamp, INT64 *time_end, int flush)
4699 VP8_COMP *cpi = (VP8_COMP *) ptr;
4700 VP8_COMMON *cm = &cpi->common;
4701 struct vpx_usec_timer tsctimer;
4702 struct vpx_usec_timer ticktimer;
4703 struct vpx_usec_timer cmptimer;
4704 YV12_BUFFER_CONFIG *force_src_buffer = NULL;
4710 #if CONFIG_RUNTIME_CPU_DETECT
4711 if (cm->rtcd.flags & HAS_NEON)
4714 vp8_push_neon(store_reg);
4718 vpx_usec_timer_start(&cmptimer);
4722 #if !(CONFIG_REALTIME_ONLY)
4723 // Should we code an alternate reference frame
4724 if (cpi->oxcf.error_resilient_mode == 0 &&
4725 cpi->oxcf.play_alternate &&
4726 cpi->source_alt_ref_pending)
4728 if ((cpi->source = vp8_lookahead_peek(cpi->lookahead,
4729 cpi->frames_till_gf_update_due)))
4731 cpi->alt_ref_source = cpi->source;
4732 if (cpi->oxcf.arnr_max_frames > 0)
4734 vp8_temporal_filter_prepare_c(cpi,
4735 cpi->frames_till_gf_update_due);
4736 force_src_buffer = &cpi->alt_ref_buffer;
4738 cm->frames_till_alt_ref_frame = cpi->frames_till_gf_update_due;
4739 cm->refresh_alt_ref_frame = 1;
4740 cm->refresh_golden_frame = 0;
4741 cm->refresh_last_frame = 0;
4743 cpi->source_alt_ref_pending = FALSE; // Clear Pending altf Ref flag.
4744 cpi->is_src_frame_alt_ref = 0;
4751 if ((cpi->source = vp8_lookahead_pop(cpi->lookahead, flush)))
4755 cpi->is_src_frame_alt_ref = cpi->alt_ref_source
4756 && (cpi->source == cpi->alt_ref_source);
4758 if(cpi->is_src_frame_alt_ref)
4759 cpi->alt_ref_source = NULL;
4765 cpi->un_scaled_source =
4766 cpi->Source = force_src_buffer ? force_src_buffer : &cpi->source->img;
4767 *time_stamp = cpi->source->ts_start;
4768 *time_end = cpi->source->ts_end;
4769 *frame_flags = cpi->source->flags;
4774 #if !(CONFIG_REALTIME_ONLY)
4776 if (flush && cpi->pass == 1 && !cpi->twopass.first_pass_done)
4778 vp8_end_first_pass(cpi); /* get last stats packet */
4779 cpi->twopass.first_pass_done = 1;
4785 #if CONFIG_RUNTIME_CPU_DETECT
4786 if (cm->rtcd.flags & HAS_NEON)
4789 vp8_pop_neon(store_reg);
4795 if (cpi->source->ts_start < cpi->first_time_stamp_ever)
4797 cpi->first_time_stamp_ever = cpi->source->ts_start;
4798 cpi->last_end_time_stamp_seen = cpi->source->ts_start;
4801 // adjust frame rates based on timestamps given
4802 if (!cm->refresh_alt_ref_frame)
4804 long long this_duration;
4807 if (cpi->source->ts_start == cpi->first_time_stamp_ever)
4809 this_duration = cpi->source->ts_end - cpi->source->ts_start;
4814 long long last_duration;
4816 this_duration = cpi->source->ts_end - cpi->last_end_time_stamp_seen;
4817 last_duration = cpi->last_end_time_stamp_seen
4818 - cpi->last_time_stamp_seen;
4819 // do a step update if the duration changes by 10%
4821 step = ((this_duration - last_duration) * 10 / last_duration);
4827 vp8_new_frame_rate(cpi, 10000000.0 / this_duration);
4830 double avg_duration, interval;
4832 /* Average this frame's rate into the last second's average
4833 * frame rate. If we haven't seen 1 second yet, then average
4834 * over the whole interval seen.
4836 interval = cpi->source->ts_end - cpi->first_time_stamp_ever;
4837 if(interval > 10000000.0)
4838 interval = 10000000;
4840 avg_duration = 10000000.0 / cpi->oxcf.frame_rate;
4841 avg_duration *= (interval - avg_duration + this_duration);
4842 avg_duration /= interval;
4844 vp8_new_frame_rate(cpi, 10000000.0 / avg_duration);
4848 cpi->last_time_stamp_seen = cpi->source->ts_start;
4849 cpi->last_end_time_stamp_seen = cpi->source->ts_end;
4852 if (cpi->compressor_speed == 2)
4854 check_gf_quality(cpi);
4855 vpx_usec_timer_start(&tsctimer);
4856 vpx_usec_timer_start(&ticktimer);
4859 // start with a 0 size frame
4862 // Clear down mmx registers
4863 vp8_clear_system_state(); //__asm emms;
4865 cm->frame_type = INTER_FRAME;
4866 cm->frame_flags = *frame_flags;
4870 if (cm->refresh_alt_ref_frame)
4872 //cm->refresh_golden_frame = 1;
4873 cm->refresh_golden_frame = 0;
4874 cm->refresh_last_frame = 0;
4878 cm->refresh_golden_frame = 0;
4879 cm->refresh_last_frame = 1;
4883 /* find a free buffer for the new frame */
4886 for(; i < NUM_YV12_BUFFERS; i++)
4888 if(!cm->yv12_fb[i].flags)
4895 assert(i < NUM_YV12_BUFFERS );
4897 #if !(CONFIG_REALTIME_ONLY)
4901 Pass1Encode(cpi, size, dest, frame_flags);
4903 else if (cpi->pass == 2)
4905 Pass2Encode(cpi, size, dest, frame_flags);
4909 encode_frame_to_data_rate(cpi, size, dest, frame_flags);
4911 if (cpi->compressor_speed == 2)
4913 unsigned int duration, duration2;
4914 vpx_usec_timer_mark(&tsctimer);
4915 vpx_usec_timer_mark(&ticktimer);
4917 duration = vpx_usec_timer_elapsed(&ticktimer);
4918 duration2 = (unsigned int)((double)duration / 2);
4920 if (cm->frame_type != KEY_FRAME)
4922 if (cpi->avg_encode_time == 0)
4923 cpi->avg_encode_time = duration;
4925 cpi->avg_encode_time = (7 * cpi->avg_encode_time + duration) >> 3;
4930 //if(*frame_flags!=1)
4933 if (cpi->avg_pick_mode_time == 0)
4934 cpi->avg_pick_mode_time = duration2;
4936 cpi->avg_pick_mode_time = (7 * cpi->avg_pick_mode_time + duration2) >> 3;
4942 if (cm->refresh_entropy_probs == 0)
4944 vpx_memcpy(&cm->fc, &cm->lfc, sizeof(cm->fc));
4947 // if its a dropped frame honor the requests on subsequent frames
4950 cpi->droppable = !frame_is_reference(cpi);
4952 // return to normal state
4953 cm->refresh_entropy_probs = 1;
4954 cm->refresh_alt_ref_frame = 0;
4955 cm->refresh_golden_frame = 0;
4956 cm->refresh_last_frame = 1;
4957 cm->frame_type = INTER_FRAME;
4961 vpx_usec_timer_mark(&cmptimer);
4962 cpi->time_compress_data += vpx_usec_timer_elapsed(&cmptimer);
4964 if (cpi->b_calculate_psnr && cpi->pass != 1 && cm->show_frame)
4966 generate_psnr_packet(cpi);
4969 #if CONFIG_INTERNAL_STATS
4973 cpi->bytes += *size;
4980 if (cpi->b_calculate_psnr)
4984 YV12_BUFFER_CONFIG *orig = cpi->Source;
4985 YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
4986 YV12_BUFFER_CONFIG *pp = &cm->post_proc_buffer;
4987 int y_samples = orig->y_height * orig->y_width ;
4988 int uv_samples = orig->uv_height * orig->uv_width ;
4989 int t_samples = y_samples + 2 * uv_samples;
4992 ye = calc_plane_error(orig->y_buffer, orig->y_stride,
4993 recon->y_buffer, recon->y_stride, orig->y_width, orig->y_height,
4994 IF_RTCD(&cpi->rtcd.variance));
4996 ue = calc_plane_error(orig->u_buffer, orig->uv_stride,
4997 recon->u_buffer, recon->uv_stride, orig->uv_width, orig->uv_height,
4998 IF_RTCD(&cpi->rtcd.variance));
5000 ve = calc_plane_error(orig->v_buffer, orig->uv_stride,
5001 recon->v_buffer, recon->uv_stride, orig->uv_width, orig->uv_height,
5002 IF_RTCD(&cpi->rtcd.variance));
5004 sq_error = ye + ue + ve;
5006 frame_psnr = vp8_mse2psnr(t_samples, 255.0, sq_error);
5008 cpi->total_y += vp8_mse2psnr(y_samples, 255.0, ye);
5009 cpi->total_u += vp8_mse2psnr(uv_samples, 255.0, ue);
5010 cpi->total_v += vp8_mse2psnr(uv_samples, 255.0, ve);
5011 cpi->total_sq_error += sq_error;
5012 cpi->total += frame_psnr;
5014 double frame_psnr2, frame_ssim2 = 0;
5017 vp8_deblock(cm->frame_to_show, &cm->post_proc_buffer, cm->filter_level * 10 / 6, 1, 0, IF_RTCD(&cm->rtcd.postproc));
5018 vp8_clear_system_state();
5020 ye = calc_plane_error(orig->y_buffer, orig->y_stride,
5021 pp->y_buffer, pp->y_stride, orig->y_width, orig->y_height,
5022 IF_RTCD(&cpi->rtcd.variance));
5024 ue = calc_plane_error(orig->u_buffer, orig->uv_stride,
5025 pp->u_buffer, pp->uv_stride, orig->uv_width, orig->uv_height,
5026 IF_RTCD(&cpi->rtcd.variance));
5028 ve = calc_plane_error(orig->v_buffer, orig->uv_stride,
5029 pp->v_buffer, pp->uv_stride, orig->uv_width, orig->uv_height,
5030 IF_RTCD(&cpi->rtcd.variance));
5032 sq_error = ye + ue + ve;
5034 frame_psnr2 = vp8_mse2psnr(t_samples, 255.0, sq_error);
5036 cpi->totalp_y += vp8_mse2psnr(y_samples, 255.0, ye);
5037 cpi->totalp_u += vp8_mse2psnr(uv_samples, 255.0, ue);
5038 cpi->totalp_v += vp8_mse2psnr(uv_samples, 255.0, ve);
5039 cpi->total_sq_error2 += sq_error;
5040 cpi->totalp += frame_psnr2;
5042 frame_ssim2 = vp8_calc_ssim(cpi->Source,
5043 &cm->post_proc_buffer, 1, &weight,
5044 IF_RTCD(&cpi->rtcd.variance));
5046 cpi->summed_quality += frame_ssim2 * weight;
5047 cpi->summed_weights += weight;
5052 if (cpi->b_calculate_ssimg)
5054 double y, u, v, frame_all;
5055 frame_all = vp8_calc_ssimg(cpi->Source, cm->frame_to_show,
5056 &y, &u, &v, IF_RTCD(&cpi->rtcd.variance));
5057 cpi->total_ssimg_y += y;
5058 cpi->total_ssimg_u += u;
5059 cpi->total_ssimg_v += v;
5060 cpi->total_ssimg_all += frame_all;
5068 if (cpi->common.frame_type != 0 && cpi->common.base_qindex == cpi->oxcf.worst_allowed_q)
5070 skiptruecount += cpi->skip_true_count;
5071 skipfalsecount += cpi->skip_false_count;
5079 FILE *f = fopen("skip.stt", "a");
5080 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);
5082 if (cpi->is_src_frame_alt_ref == 1)
5083 fprintf(f, "skipcount: %4d framesize: %d\n", cpi->skip_true_count , *size);
5092 #if CONFIG_RUNTIME_CPU_DETECT
5093 if (cm->rtcd.flags & HAS_NEON)
5096 vp8_pop_neon(store_reg);
5103 int vp8_get_preview_raw_frame(VP8_PTR comp, YV12_BUFFER_CONFIG *dest, vp8_ppflags_t *flags)
5105 VP8_COMP *cpi = (VP8_COMP *) comp;
5107 if (cpi->common.refresh_alt_ref_frame)
5113 ret = vp8_post_proc_frame(&cpi->common, dest, flags);
5116 if (cpi->common.frame_to_show)
5118 *dest = *cpi->common.frame_to_show;
5119 dest->y_width = cpi->common.Width;
5120 dest->y_height = cpi->common.Height;
5121 dest->uv_height = cpi->common.Height / 2;
5129 #endif //!CONFIG_POSTPROC
5130 vp8_clear_system_state();
5135 int vp8_set_roimap(VP8_PTR comp, unsigned char *map, unsigned int rows, unsigned int cols, int delta_q[4], int delta_lf[4], unsigned int threshold[4])
5137 VP8_COMP *cpi = (VP8_COMP *) comp;
5138 signed char feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS];
5140 if (cpi->common.mb_rows != rows || cpi->common.mb_cols != cols)
5145 disable_segmentation((VP8_PTR)cpi);
5149 // Set the segmentation Map
5150 set_segmentation_map((VP8_PTR)cpi, map);
5152 // Activate segmentation.
5153 enable_segmentation((VP8_PTR)cpi);
5155 // Set up the quant segment data
5156 feature_data[MB_LVL_ALT_Q][0] = delta_q[0];
5157 feature_data[MB_LVL_ALT_Q][1] = delta_q[1];
5158 feature_data[MB_LVL_ALT_Q][2] = delta_q[2];
5159 feature_data[MB_LVL_ALT_Q][3] = delta_q[3];
5161 // Set up the loop segment data s
5162 feature_data[MB_LVL_ALT_LF][0] = delta_lf[0];
5163 feature_data[MB_LVL_ALT_LF][1] = delta_lf[1];
5164 feature_data[MB_LVL_ALT_LF][2] = delta_lf[2];
5165 feature_data[MB_LVL_ALT_LF][3] = delta_lf[3];
5167 cpi->segment_encode_breakout[0] = threshold[0];
5168 cpi->segment_encode_breakout[1] = threshold[1];
5169 cpi->segment_encode_breakout[2] = threshold[2];
5170 cpi->segment_encode_breakout[3] = threshold[3];
5172 // Initialise the feature data structure
5173 // SEGMENT_DELTADATA 0, SEGMENT_ABSDATA 1
5174 set_segment_data((VP8_PTR)cpi, &feature_data[0][0], SEGMENT_DELTADATA);
5179 int vp8_set_active_map(VP8_PTR comp, unsigned char *map, unsigned int rows, unsigned int cols)
5181 VP8_COMP *cpi = (VP8_COMP *) comp;
5183 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols)
5187 vpx_memcpy(cpi->active_map, map, rows * cols);
5188 cpi->active_map_enabled = 1;
5191 cpi->active_map_enabled = 0;
5197 //cpi->active_map_enabled = 0;
5202 int vp8_set_internal_size(VP8_PTR comp, VPX_SCALING horiz_mode, VPX_SCALING vert_mode)
5204 VP8_COMP *cpi = (VP8_COMP *) comp;
5206 if (horiz_mode <= ONETWO)
5207 cpi->common.horiz_scale = horiz_mode;
5211 if (vert_mode <= ONETWO)
5212 cpi->common.vert_scale = vert_mode;
5221 int vp8_calc_ss_err(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *dest, const vp8_variance_rtcd_vtable_t *rtcd)
5226 unsigned char *src = source->y_buffer;
5227 unsigned char *dst = dest->y_buffer;
5230 // Loop through the Y plane raw and reconstruction data summing (square differences)
5231 for (i = 0; i < source->y_height; i += 16)
5233 for (j = 0; j < source->y_width; j += 16)
5236 Total += VARIANCE_INVOKE(rtcd, mse16x16)(src + j, source->y_stride, dst + j, dest->y_stride, &sse);
5239 src += 16 * source->y_stride;
5240 dst += 16 * dest->y_stride;
5247 int vp8_get_quantizer(VP8_PTR c)
5249 VP8_COMP *cpi = (VP8_COMP *) c;
5250 return cpi->common.base_qindex;