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_init_mv_bits_sadcost();
50 extern void vp8cx_pick_filter_level_fast(YV12_BUFFER_CONFIG *sd, VP8_COMP *cpi);
51 extern void vp8cx_set_alt_lf_level(VP8_COMP *cpi, int filt_val);
52 extern void vp8cx_pick_filter_level(YV12_BUFFER_CONFIG *sd, VP8_COMP *cpi);
54 extern void vp8_init_loop_filter(VP8_COMMON *cm);
55 extern void vp8_loop_filter_frame(VP8_COMMON *cm, MACROBLOCKD *mbd, int filt_val);
56 extern void vp8_loop_filter_frame_yonly(VP8_COMMON *cm, MACROBLOCKD *mbd, int filt_val, int sharpness_lvl);
57 extern void vp8_dmachine_specific_config(VP8_COMP *cpi);
58 extern void vp8_cmachine_specific_config(VP8_COMP *cpi);
59 extern void vp8_calc_auto_iframe_target_size(VP8_COMP *cpi);
60 extern void vp8_deblock_frame(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *post, int filt_lvl, int low_var_thresh, int flag);
61 extern void print_parms(VP8_CONFIG *ocf, char *filenam);
62 extern unsigned int vp8_get_processor_freq();
63 extern void print_tree_update_probs();
64 extern void vp8cx_create_encoder_threads(VP8_COMP *cpi);
65 extern void vp8cx_remove_encoder_threads(VP8_COMP *cpi);
67 extern void vp8_yv12_copy_frame_func_neon(YV12_BUFFER_CONFIG *src_ybc, YV12_BUFFER_CONFIG *dst_ybc);
68 extern void vp8_yv12_copy_src_frame_func_neon(YV12_BUFFER_CONFIG *src_ybc, YV12_BUFFER_CONFIG *dst_ybc);
71 int vp8_estimate_entropy_savings(VP8_COMP *cpi);
72 int vp8_calc_ss_err(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *dest, const vp8_variance_rtcd_vtable_t *rtcd);
73 int vp8_calc_low_ss_err(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *dest, const vp8_variance_rtcd_vtable_t *rtcd);
75 extern void vp8_temporal_filter_prepare_c(VP8_COMP *cpi);
77 static void set_default_lf_deltas(VP8_COMP *cpi);
79 extern const int vp8_gf_interval_table[101];
84 extern double vp8_calc_ssim
86 YV12_BUFFER_CONFIG *source,
87 YV12_BUFFER_CONFIG *dest,
90 const vp8_variance_rtcd_vtable_t *rtcd
94 extern double vp8_calc_ssimg
96 YV12_BUFFER_CONFIG *source,
97 YV12_BUFFER_CONFIG *dest,
107 #ifdef OUTPUT_YUV_SRC
118 extern int skip_true_count;
119 extern int skip_false_count;
124 extern int intra_mode_stats[10][10][10];
128 unsigned int frames_at_speed[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
129 unsigned int tot_pm = 0;
130 unsigned int cnt_pm = 0;
131 unsigned int tot_ef = 0;
132 unsigned int cnt_ef = 0;
136 extern unsigned __int64 Sectionbits[50];
137 extern int y_modes[5] ;
138 extern int uv_modes[4] ;
139 extern int b_modes[10] ;
141 extern int inter_y_modes[10] ;
142 extern int inter_uv_modes[4] ;
143 extern unsigned int inter_b_modes[15];
146 extern void (*vp8_short_fdct4x4)(short *input, short *output, int pitch);
147 extern void (*vp8_short_fdct8x4)(short *input, short *output, int pitch);
149 extern const int vp8_bits_per_mb[2][QINDEX_RANGE];
151 extern const int qrounding_factors[129];
152 extern const int qzbin_factors[129];
153 extern void vp8cx_init_quantizer(VP8_COMP *cpi);
154 extern const int vp8cx_base_skip_false_prob[128];
156 // Tables relating active max Q to active min Q
157 static const int kf_low_motion_minq[QINDEX_RANGE] =
159 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
160 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
161 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
162 0,0,0,0,1,1,1,1,1,1,1,1,2,2,2,2,
163 3,3,3,3,3,3,4,4,4,5,5,5,5,5,6,6,
164 6,6,7,7,8,8,8,8,9,9,10,10,10,10,11,11,
165 11,11,12,12,13,13,13,13,14,14,15,15,15,15,16,16,
166 16,16,17,17,18,18,18,18,19,20,20,21,21,22,23,23
168 static const int kf_high_motion_minq[QINDEX_RANGE] =
170 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
171 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
172 1,1,1,1,1,1,1,1,2,2,2,2,3,3,3,3,
173 3,3,3,3,4,4,4,4,5,5,5,5,5,5,6,6,
174 6,6,7,7,8,8,8,8,9,9,10,10,10,10,11,11,
175 11,11,12,12,13,13,13,13,14,14,15,15,15,15,16,16,
176 16,16,17,17,18,18,18,18,19,19,20,20,20,20,21,21,
177 21,21,22,22,23,23,24,25,25,26,26,27,28,28,29,30
179 static const int gf_low_motion_minq[QINDEX_RANGE] =
181 0,0,0,0,1,1,1,1,1,1,1,1,2,2,2,2,
182 3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,
183 7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10,
184 11,11,12,12,13,13,14,14,15,15,16,16,17,17,18,18,
185 19,19,20,20,21,21,22,22,23,23,24,24,25,25,26,26,
186 27,27,28,28,29,29,30,30,31,31,32,32,33,33,34,34,
187 35,35,36,36,37,37,38,38,39,39,40,40,41,41,42,42,
188 43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58
190 static const int gf_mid_motion_minq[QINDEX_RANGE] =
192 0,0,0,0,1,1,1,1,1,1,2,2,3,3,3,4,
193 4,4,5,5,5,6,6,6,7,7,7,8,8,8,9,9,
194 9,10,10,10,10,11,11,11,12,12,12,12,13,13,13,14,
195 14,14,15,15,16,16,17,17,18,18,19,19,20,20,21,21,
196 22,22,23,23,24,24,25,25,26,26,27,27,28,28,29,29,
197 30,30,31,31,32,32,33,33,34,34,35,35,36,36,37,37,
198 38,39,39,40,40,41,41,42,42,43,43,44,45,46,47,48,
199 49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64
201 static const int gf_high_motion_minq[QINDEX_RANGE] =
203 0,0,0,0,1,1,1,1,1,2,2,2,3,3,3,4,
204 4,4,5,5,5,6,6,6,7,7,7,8,8,8,9,9,
205 9,10,10,10,11,11,12,12,13,13,14,14,15,15,16,16,
206 17,17,18,18,19,19,20,20,21,21,22,22,23,23,24,24,
207 25,25,26,26,27,27,28,28,29,29,30,30,31,31,32,32,
208 33,33,34,34,35,35,36,36,37,37,38,38,39,39,40,40,
209 41,41,42,42,43,44,45,46,47,48,49,50,51,52,53,54,
210 55,56,57,58,59,60,62,64,66,68,70,72,74,76,78,80
212 static const int inter_minq[QINDEX_RANGE] =
214 0,0,1,1,2,3,3,4,4,5,6,6,7,8,8,9,
215 9,10,11,11,12,13,13,14,15,15,16,17,17,18,19,20,
216 20,21,22,22,23,24,24,25,26,27,27,28,29,30,30,31,
217 32,33,33,34,35,36,36,37,38,39,39,40,41,42,42,43,
218 44,45,46,46,47,48,49,50,50,51,52,53,54,55,55,56,
219 57,58,59,60,60,61,62,63,64,65,66,67,67,68,69,70,
220 71,72,73,74,75,75,76,77,78,79,80,81,82,83,84,85,
221 86,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100
224 void vp8_initialize()
226 static int init_done = 0;
230 vp8_scale_machine_specific_config();
231 vp8_initialize_common();
232 //vp8_dmachine_specific_config();
233 vp8_tokenize_initialize();
235 vp8cx_init_mv_bits_sadcost();
239 #ifdef PACKET_TESTING
240 extern FILE *vpxlogc;
243 static void setup_features(VP8_COMP *cpi)
245 // Set up default state for MB feature flags
246 cpi->mb.e_mbd.segmentation_enabled = 0;
247 cpi->mb.e_mbd.update_mb_segmentation_map = 0;
248 cpi->mb.e_mbd.update_mb_segmentation_data = 0;
249 vpx_memset(cpi->mb.e_mbd.mb_segment_tree_probs, 255, sizeof(cpi->mb.e_mbd.mb_segment_tree_probs));
250 vpx_memset(cpi->mb.e_mbd.segment_feature_data, 0, sizeof(cpi->mb.e_mbd.segment_feature_data));
252 cpi->mb.e_mbd.mode_ref_lf_delta_enabled = 0;
253 cpi->mb.e_mbd.mode_ref_lf_delta_update = 0;
254 vpx_memset(cpi->mb.e_mbd.ref_lf_deltas, 0, sizeof(cpi->mb.e_mbd.ref_lf_deltas));
255 vpx_memset(cpi->mb.e_mbd.mode_lf_deltas, 0, sizeof(cpi->mb.e_mbd.mode_lf_deltas));
256 vpx_memset(cpi->mb.e_mbd.last_ref_lf_deltas, 0, sizeof(cpi->mb.e_mbd.ref_lf_deltas));
257 vpx_memset(cpi->mb.e_mbd.last_mode_lf_deltas, 0, sizeof(cpi->mb.e_mbd.mode_lf_deltas));
259 set_default_lf_deltas(cpi);
264 void vp8_dealloc_compressor_data(VP8_COMP *cpi)
266 vpx_free(cpi->tplist);
269 // Delete last frame MV storage buffers
273 vpx_free(cpi->lf_ref_frame_sign_bias);
274 cpi->lf_ref_frame_sign_bias = 0;
276 vpx_free(cpi->lf_ref_frame);
277 cpi->lf_ref_frame = 0;
279 // Delete sementation map
280 vpx_free(cpi->segmentation_map);
281 cpi->segmentation_map = 0;
283 vpx_free(cpi->active_map);
286 vp8_de_alloc_frame_buffers(&cpi->common);
288 vp8_yv12_de_alloc_frame_buffer(&cpi->last_frame_uf);
289 vp8_yv12_de_alloc_frame_buffer(&cpi->scaled_source);
290 #if VP8_TEMPORAL_ALT_REF
291 vp8_yv12_de_alloc_frame_buffer(&cpi->alt_ref_buffer.source_buffer);
296 for (i = 0; i < MAX_LAG_BUFFERS; i++)
297 vp8_yv12_de_alloc_frame_buffer(&cpi->src_buffer[i].source_buffer);
299 cpi->source_buffer_count = 0;
305 // Structure used to monitor GF usage
306 vpx_free(cpi->gf_active_flags);
307 cpi->gf_active_flags = 0;
309 vpx_free(cpi->mb.pip);
312 #if !(CONFIG_REALTIME_ONLY)
313 vpx_free(cpi->total_stats);
314 cpi->total_stats = 0;
316 vpx_free(cpi->this_frame_stats);
317 cpi->this_frame_stats = 0;
321 static void enable_segmentation(VP8_PTR ptr)
323 VP8_COMP *cpi = (VP8_COMP *)(ptr);
325 // Set the appropriate feature bit
326 cpi->mb.e_mbd.segmentation_enabled = 1;
327 cpi->mb.e_mbd.update_mb_segmentation_map = 1;
328 cpi->mb.e_mbd.update_mb_segmentation_data = 1;
330 static void disable_segmentation(VP8_PTR ptr)
332 VP8_COMP *cpi = (VP8_COMP *)(ptr);
334 // Clear the appropriate feature bit
335 cpi->mb.e_mbd.segmentation_enabled = 0;
338 // Valid values for a segment are 0 to 3
339 // Segmentation map is arrange as [Rows][Columns]
340 static void set_segmentation_map(VP8_PTR ptr, unsigned char *segmentation_map)
342 VP8_COMP *cpi = (VP8_COMP *)(ptr);
344 // Copy in the new segmentation map
345 vpx_memcpy(cpi->segmentation_map, segmentation_map, (cpi->common.mb_rows * cpi->common.mb_cols));
347 // Signal that the map should be updated.
348 cpi->mb.e_mbd.update_mb_segmentation_map = 1;
349 cpi->mb.e_mbd.update_mb_segmentation_data = 1;
352 // The values given for each segment can be either deltas (from the default value chosen for the frame) or absolute values.
354 // Valid range for abs values is (0-127 for MB_LVL_ALT_Q) , (0-63 for SEGMENT_ALT_LF)
355 // Valid range for delta values are (+/-127 for MB_LVL_ALT_Q) , (+/-63 for SEGMENT_ALT_LF)
357 // abs_delta = SEGMENT_DELTADATA (deltas) abs_delta = SEGMENT_ABSDATA (use the absolute values given).
360 static void set_segment_data(VP8_PTR ptr, signed char *feature_data, unsigned char abs_delta)
362 VP8_COMP *cpi = (VP8_COMP *)(ptr);
364 cpi->mb.e_mbd.mb_segement_abs_delta = abs_delta;
365 vpx_memcpy(cpi->segment_feature_data, feature_data, sizeof(cpi->segment_feature_data));
369 static void segmentation_test_function(VP8_PTR ptr)
371 VP8_COMP *cpi = (VP8_COMP *)(ptr);
373 unsigned char *seg_map;
374 signed char feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS];
376 // Create a temporary map for segmentation data.
377 CHECK_MEM_ERROR(seg_map, vpx_calloc(cpi->common.mb_rows * cpi->common.mb_cols, 1));
379 // MB loop to set local segmentation map
380 /*for ( i = 0; i < cpi->common.mb_rows; i++ )
382 for ( j = 0; j < cpi->common.mb_cols; j++ )
384 //seg_map[(i*cpi->common.mb_cols) + j] = (j % 2) + ((i%2)* 2);
385 //if ( j < cpi->common.mb_cols/2 )
387 // Segment 1 around the edge else 0
388 if ( (i == 0) || (j == 0) || (i == (cpi->common.mb_rows-1)) || (j == (cpi->common.mb_cols-1)) )
389 seg_map[(i*cpi->common.mb_cols) + j] = 1;
390 //else if ( (i < 2) || (j < 2) || (i > (cpi->common.mb_rows-3)) || (j > (cpi->common.mb_cols-3)) )
391 // seg_map[(i*cpi->common.mb_cols) + j] = 2;
392 //else if ( (i < 5) || (j < 5) || (i > (cpi->common.mb_rows-6)) || (j > (cpi->common.mb_cols-6)) )
393 // seg_map[(i*cpi->common.mb_cols) + j] = 3;
395 seg_map[(i*cpi->common.mb_cols) + j] = 0;
399 // Set the segmentation Map
400 set_segmentation_map(ptr, seg_map);
402 // Activate segmentation.
403 enable_segmentation(ptr);
405 // Set up the quant segment data
406 feature_data[MB_LVL_ALT_Q][0] = 0;
407 feature_data[MB_LVL_ALT_Q][1] = 4;
408 feature_data[MB_LVL_ALT_Q][2] = 0;
409 feature_data[MB_LVL_ALT_Q][3] = 0;
410 // Set up the loop segment data
411 feature_data[MB_LVL_ALT_LF][0] = 0;
412 feature_data[MB_LVL_ALT_LF][1] = 0;
413 feature_data[MB_LVL_ALT_LF][2] = 0;
414 feature_data[MB_LVL_ALT_LF][3] = 0;
416 // Initialise the feature data structure
417 // SEGMENT_DELTADATA 0, SEGMENT_ABSDATA 1
418 set_segment_data(ptr, &feature_data[0][0], SEGMENT_DELTADATA);
420 // Delete sementation map
427 // A simple function to cyclically refresh the background at a lower Q
428 static void cyclic_background_refresh(VP8_COMP *cpi, int Q, int lf_adjustment)
430 unsigned char *seg_map;
431 signed char feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS];
433 int block_count = cpi->cyclic_refresh_mode_max_mbs_perframe;
434 int mbs_in_frame = cpi->common.mb_rows * cpi->common.mb_cols;
436 // Create a temporary map for segmentation data.
437 CHECK_MEM_ERROR(seg_map, vpx_calloc(cpi->common.mb_rows * cpi->common.mb_cols, 1));
439 cpi->cyclic_refresh_q = Q;
441 for (i = Q; i > 0; i--)
443 if (vp8_bits_per_mb[cpi->common.frame_type][i] >= ((vp8_bits_per_mb[cpi->common.frame_type][Q]*(Q + 128)) / 64))
444 //if ( vp8_bits_per_mb[cpi->common.frame_type][i] >= ((vp8_bits_per_mb[cpi->common.frame_type][Q]*((2*Q)+96))/64) )
450 cpi->cyclic_refresh_q = i;
452 // Only update for inter frames
453 if (cpi->common.frame_type != KEY_FRAME)
455 // Cycle through the macro_block rows
456 // MB loop to set local segmentation map
457 for (i = cpi->cyclic_refresh_mode_index; i < mbs_in_frame; i++)
459 // If the MB is as a candidate for clean up then mark it for possible boost/refresh (segment 1)
460 // The segment id may get reset to 0 later if the MB gets coded anything other than last frame 0,0
461 // as only (last frame 0,0) MBs are eligable for refresh : that is to say Mbs likely to be background blocks.
462 if (cpi->cyclic_refresh_map[i] == 0)
470 // Skip blocks that have been refreshed recently anyway.
471 if (cpi->cyclic_refresh_map[i] < 0)
472 //cpi->cyclic_refresh_map[i] = cpi->cyclic_refresh_map[i] / 16;
473 cpi->cyclic_refresh_map[i]++;
484 // If we have gone through the frame reset to the start
485 cpi->cyclic_refresh_mode_index = i;
487 if (cpi->cyclic_refresh_mode_index >= mbs_in_frame)
488 cpi->cyclic_refresh_mode_index = 0;
491 // Set the segmentation Map
492 set_segmentation_map((VP8_PTR)cpi, seg_map);
494 // Activate segmentation.
495 enable_segmentation((VP8_PTR)cpi);
497 // Set up the quant segment data
498 feature_data[MB_LVL_ALT_Q][0] = 0;
499 feature_data[MB_LVL_ALT_Q][1] = (cpi->cyclic_refresh_q - Q);
500 feature_data[MB_LVL_ALT_Q][2] = 0;
501 feature_data[MB_LVL_ALT_Q][3] = 0;
503 // Set up the loop segment data
504 feature_data[MB_LVL_ALT_LF][0] = 0;
505 feature_data[MB_LVL_ALT_LF][1] = lf_adjustment;
506 feature_data[MB_LVL_ALT_LF][2] = 0;
507 feature_data[MB_LVL_ALT_LF][3] = 0;
509 // Initialise the feature data structure
510 // SEGMENT_DELTADATA 0, SEGMENT_ABSDATA 1
511 set_segment_data((VP8_PTR)cpi, &feature_data[0][0], SEGMENT_DELTADATA);
513 // Delete sementation map
520 static void set_default_lf_deltas(VP8_COMP *cpi)
522 cpi->mb.e_mbd.mode_ref_lf_delta_enabled = 1;
523 cpi->mb.e_mbd.mode_ref_lf_delta_update = 1;
525 vpx_memset(cpi->mb.e_mbd.ref_lf_deltas, 0, sizeof(cpi->mb.e_mbd.ref_lf_deltas));
526 vpx_memset(cpi->mb.e_mbd.mode_lf_deltas, 0, sizeof(cpi->mb.e_mbd.mode_lf_deltas));
528 // Test of ref frame deltas
529 cpi->mb.e_mbd.ref_lf_deltas[INTRA_FRAME] = 2;
530 cpi->mb.e_mbd.ref_lf_deltas[LAST_FRAME] = 0;
531 cpi->mb.e_mbd.ref_lf_deltas[GOLDEN_FRAME] = -2;
532 cpi->mb.e_mbd.ref_lf_deltas[ALTREF_FRAME] = -2;
534 cpi->mb.e_mbd.mode_lf_deltas[0] = 4; // BPRED
535 cpi->mb.e_mbd.mode_lf_deltas[1] = -2; // Zero
536 cpi->mb.e_mbd.mode_lf_deltas[2] = 2; // New mv
537 cpi->mb.e_mbd.mode_lf_deltas[3] = 4; // Split mv
540 void vp8_set_speed_features(VP8_COMP *cpi)
542 SPEED_FEATURES *sf = &cpi->sf;
543 int Mode = cpi->compressor_speed;
544 int Speed = cpi->Speed;
546 VP8_COMMON *cm = &cpi->common;
547 int last_improved_quant = sf->improved_quant;
549 // Initialise default mode frequency sampling variables
550 for (i = 0; i < MAX_MODES; i ++)
552 cpi->mode_check_freq[i] = 0;
553 cpi->mode_test_hit_counts[i] = 0;
554 cpi->mode_chosen_counts[i] = 0;
557 cpi->mbs_tested_so_far = 0;
559 // best quality defaults
561 sf->search_method = NSTEP;
562 sf->improved_quant = 1;
563 sf->improved_dct = 1;
566 sf->quarter_pixel_search = 1;
567 sf->half_pixel_search = 1;
568 sf->full_freq[0] = 7;
569 sf->full_freq[1] = 7;
570 sf->min_fs_radius = 8;
571 sf->max_fs_radius = 32;
572 sf->iterative_sub_pixel = 1;
573 sf->optimize_coefficients = 1;
574 sf->use_fastquant_for_pick = 0;
575 sf->no_skip_block4x4_search = 1;
578 sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
579 sf->improved_mv_pred = 1;
584 // default thresholds to 0
585 for (i = 0; i < MAX_MODES; i++)
586 sf->thresh_mult[i] = 0;
590 #if !(CONFIG_REALTIME_ONLY)
591 case 0: // best quality mode
592 sf->thresh_mult[THR_ZEROMV ] = 0;
593 sf->thresh_mult[THR_ZEROG ] = 0;
594 sf->thresh_mult[THR_ZEROA ] = 0;
595 sf->thresh_mult[THR_NEARESTMV] = 0;
596 sf->thresh_mult[THR_NEARESTG ] = 0;
597 sf->thresh_mult[THR_NEARESTA ] = 0;
598 sf->thresh_mult[THR_NEARMV ] = 0;
599 sf->thresh_mult[THR_NEARG ] = 0;
600 sf->thresh_mult[THR_NEARA ] = 0;
602 sf->thresh_mult[THR_DC ] = 0;
604 sf->thresh_mult[THR_V_PRED ] = 1000;
605 sf->thresh_mult[THR_H_PRED ] = 1000;
606 sf->thresh_mult[THR_B_PRED ] = 2000;
607 sf->thresh_mult[THR_TM ] = 1000;
609 sf->thresh_mult[THR_NEWMV ] = 1000;
610 sf->thresh_mult[THR_NEWG ] = 1000;
611 sf->thresh_mult[THR_NEWA ] = 1000;
613 sf->thresh_mult[THR_SPLITMV ] = 2500;
614 sf->thresh_mult[THR_SPLITG ] = 5000;
615 sf->thresh_mult[THR_SPLITA ] = 5000;
617 sf->full_freq[0] = 7;
618 sf->full_freq[1] = 15;
621 sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
625 sf->thresh_mult[THR_NEARESTMV] = 0;
626 sf->thresh_mult[THR_ZEROMV ] = 0;
627 sf->thresh_mult[THR_DC ] = 0;
628 sf->thresh_mult[THR_NEARMV ] = 0;
629 sf->thresh_mult[THR_V_PRED ] = 1000;
630 sf->thresh_mult[THR_H_PRED ] = 1000;
631 sf->thresh_mult[THR_B_PRED ] = 2500;
632 sf->thresh_mult[THR_TM ] = 1000;
634 sf->thresh_mult[THR_NEARESTG ] = 1000;
635 sf->thresh_mult[THR_NEARESTA ] = 1000;
637 sf->thresh_mult[THR_ZEROG ] = 1000;
638 sf->thresh_mult[THR_ZEROA ] = 1000;
639 sf->thresh_mult[THR_NEARG ] = 1000;
640 sf->thresh_mult[THR_NEARA ] = 1000;
643 sf->thresh_mult[THR_ZEROMV ] = 0;
644 sf->thresh_mult[THR_ZEROG ] = 0;
645 sf->thresh_mult[THR_ZEROA ] = 0;
646 sf->thresh_mult[THR_NEARESTMV] = 0;
647 sf->thresh_mult[THR_NEARESTG ] = 0;
648 sf->thresh_mult[THR_NEARESTA ] = 0;
649 sf->thresh_mult[THR_NEARMV ] = 0;
650 sf->thresh_mult[THR_NEARG ] = 0;
651 sf->thresh_mult[THR_NEARA ] = 0;
653 // sf->thresh_mult[THR_DC ] = 0;
655 // sf->thresh_mult[THR_V_PRED ] = 1000;
656 // sf->thresh_mult[THR_H_PRED ] = 1000;
657 // sf->thresh_mult[THR_B_PRED ] = 2000;
658 // sf->thresh_mult[THR_TM ] = 1000;
660 sf->thresh_mult[THR_NEWMV ] = 1000;
661 sf->thresh_mult[THR_NEWG ] = 1000;
662 sf->thresh_mult[THR_NEWA ] = 1000;
664 sf->thresh_mult[THR_SPLITMV ] = 1700;
665 sf->thresh_mult[THR_SPLITG ] = 4500;
666 sf->thresh_mult[THR_SPLITA ] = 4500;
668 sf->thresh_mult[THR_NEWMV ] = 1500;
669 sf->thresh_mult[THR_NEWG ] = 1500;
670 sf->thresh_mult[THR_NEWA ] = 1500;
672 sf->thresh_mult[THR_SPLITMV ] = 5000;
673 sf->thresh_mult[THR_SPLITG ] = 10000;
674 sf->thresh_mult[THR_SPLITA ] = 10000;
676 sf->full_freq[0] = 15;
677 sf->full_freq[1] = 31;
681 /* Disable coefficient optimization above speed 0 */
682 sf->optimize_coefficients = 0;
683 sf->use_fastquant_for_pick = 1;
684 sf->no_skip_block4x4_search = 0;
688 cpi->mode_check_freq[THR_SPLITG] = 2;
689 cpi->mode_check_freq[THR_SPLITA] = 2;
690 cpi->mode_check_freq[THR_SPLITMV] = 0;
695 cpi->mode_check_freq[THR_SPLITG] = 4;
696 cpi->mode_check_freq[THR_SPLITA] = 4;
697 cpi->mode_check_freq[THR_SPLITMV] = 2;
699 sf->thresh_mult[THR_TM ] = 1500;
700 sf->thresh_mult[THR_V_PRED ] = 1500;
701 sf->thresh_mult[THR_H_PRED ] = 1500;
702 sf->thresh_mult[THR_B_PRED ] = 5000;
704 if (cpi->ref_frame_flags & VP8_LAST_FLAG)
706 sf->thresh_mult[THR_NEWMV ] = 2000;
707 sf->thresh_mult[THR_SPLITMV ] = 10000;
710 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
712 sf->thresh_mult[THR_NEARESTG ] = 1500;
713 sf->thresh_mult[THR_ZEROG ] = 1500;
714 sf->thresh_mult[THR_NEARG ] = 1500;
715 sf->thresh_mult[THR_NEWG ] = 2000;
716 sf->thresh_mult[THR_SPLITG ] = 20000;
719 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
721 sf->thresh_mult[THR_NEARESTA ] = 1500;
722 sf->thresh_mult[THR_ZEROA ] = 1500;
723 sf->thresh_mult[THR_NEARA ] = 1500;
724 sf->thresh_mult[THR_NEWA ] = 2000;
725 sf->thresh_mult[THR_SPLITA ] = 20000;
731 cpi->mode_check_freq[THR_SPLITG] = 15;
732 cpi->mode_check_freq[THR_SPLITA] = 15;
733 cpi->mode_check_freq[THR_SPLITMV] = 7;
735 sf->thresh_mult[THR_TM ] = 2000;
736 sf->thresh_mult[THR_V_PRED ] = 2000;
737 sf->thresh_mult[THR_H_PRED ] = 2000;
738 sf->thresh_mult[THR_B_PRED ] = 7500;
740 if (cpi->ref_frame_flags & VP8_LAST_FLAG)
742 sf->thresh_mult[THR_NEWMV ] = 2000;
743 sf->thresh_mult[THR_SPLITMV ] = 25000;
746 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
748 sf->thresh_mult[THR_NEARESTG ] = 2000;
749 sf->thresh_mult[THR_ZEROG ] = 2000;
750 sf->thresh_mult[THR_NEARG ] = 2000;
751 sf->thresh_mult[THR_NEWG ] = 2500;
752 sf->thresh_mult[THR_SPLITG ] = 50000;
755 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
757 sf->thresh_mult[THR_NEARESTA ] = 2000;
758 sf->thresh_mult[THR_ZEROA ] = 2000;
759 sf->thresh_mult[THR_NEARA ] = 2000;
760 sf->thresh_mult[THR_NEWA ] = 2500;
761 sf->thresh_mult[THR_SPLITA ] = 50000;
764 sf->improved_quant = 0;
765 sf->improved_dct = 0;
767 // Only do recode loop on key frames, golden frames and
771 sf->full_freq[0] = 31;
772 sf->full_freq[1] = 63;
777 sf->thresh_mult[THR_SPLITA ] = INT_MAX;
778 sf->thresh_mult[THR_SPLITG ] = INT_MAX;
779 sf->thresh_mult[THR_SPLITMV ] = INT_MAX;
781 cpi->mode_check_freq[THR_V_PRED] = 0;
782 cpi->mode_check_freq[THR_H_PRED] = 0;
783 cpi->mode_check_freq[THR_B_PRED] = 0;
784 cpi->mode_check_freq[THR_NEARG] = 0;
785 cpi->mode_check_freq[THR_NEWG] = 0;
786 cpi->mode_check_freq[THR_NEARA] = 0;
787 cpi->mode_check_freq[THR_NEWA] = 0;
790 sf->recode_loop = 0; // recode loop off
791 sf->RD = 0; // Turn rd off
793 sf->full_freq[0] = 63;
794 sf->full_freq[1] = 127;
799 sf->auto_filter = 0; // Faster selection of loop filter
800 sf->full_freq[0] = INT_MAX;
801 sf->full_freq[1] = INT_MAX;
803 cpi->mode_check_freq[THR_V_PRED] = 2;
804 cpi->mode_check_freq[THR_H_PRED] = 2;
805 cpi->mode_check_freq[THR_B_PRED] = 2;
807 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
809 cpi->mode_check_freq[THR_NEARG] = 2;
810 cpi->mode_check_freq[THR_NEWG] = 4;
813 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
815 cpi->mode_check_freq[THR_NEARA] = 2;
816 cpi->mode_check_freq[THR_NEWA] = 4;
819 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
821 sf->thresh_mult[THR_NEARESTG ] = 2000;
822 sf->thresh_mult[THR_ZEROG ] = 2000;
823 sf->thresh_mult[THR_NEARG ] = 2000;
824 sf->thresh_mult[THR_NEWG ] = 4000;
827 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
829 sf->thresh_mult[THR_NEARESTA ] = 2000;
830 sf->thresh_mult[THR_ZEROA ] = 2000;
831 sf->thresh_mult[THR_NEARA ] = 2000;
832 sf->thresh_mult[THR_NEWA ] = 4000;
839 sf->optimize_coefficients = 0;
842 sf->iterative_sub_pixel = 1;
843 sf->thresh_mult[THR_NEARESTMV] = 0;
844 sf->thresh_mult[THR_ZEROMV ] = 0;
845 sf->thresh_mult[THR_DC ] = 0;
846 sf->thresh_mult[THR_TM ] = 0;
847 sf->thresh_mult[THR_NEARMV ] = 0;
848 sf->thresh_mult[THR_V_PRED ] = 1000;
849 sf->thresh_mult[THR_H_PRED ] = 1000;
850 sf->thresh_mult[THR_B_PRED ] = 2500;
851 sf->thresh_mult[THR_NEARESTG ] = 1000;
852 sf->thresh_mult[THR_ZEROG ] = 1000;
853 sf->thresh_mult[THR_NEARG ] = 1000;
854 sf->thresh_mult[THR_NEARESTA ] = 1000;
855 sf->thresh_mult[THR_ZEROA ] = 1000;
856 sf->thresh_mult[THR_NEARA ] = 1000;
857 sf->thresh_mult[THR_NEWMV ] = 2000;
858 sf->thresh_mult[THR_NEWG ] = 2000;
859 sf->thresh_mult[THR_NEWA ] = 2000;
860 sf->thresh_mult[THR_SPLITMV ] = 5000;
861 sf->thresh_mult[THR_SPLITG ] = 10000;
862 sf->thresh_mult[THR_SPLITA ] = 10000;
863 sf->full_freq[0] = 15;
864 sf->full_freq[1] = 31;
865 sf->search_method = NSTEP;
869 cpi->mode_check_freq[THR_SPLITG] = 4;
870 cpi->mode_check_freq[THR_SPLITA] = 4;
871 cpi->mode_check_freq[THR_SPLITMV] = 2;
873 sf->thresh_mult[THR_DC ] = 0;
874 sf->thresh_mult[THR_TM ] = 1000;
875 sf->thresh_mult[THR_V_PRED ] = 2000;
876 sf->thresh_mult[THR_H_PRED ] = 2000;
877 sf->thresh_mult[THR_B_PRED ] = 5000;
879 if (cpi->ref_frame_flags & VP8_LAST_FLAG)
881 sf->thresh_mult[THR_NEARESTMV] = 0;
882 sf->thresh_mult[THR_ZEROMV ] = 0;
883 sf->thresh_mult[THR_NEARMV ] = 0;
884 sf->thresh_mult[THR_NEWMV ] = 2000;
885 sf->thresh_mult[THR_SPLITMV ] = 10000;
888 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
890 sf->thresh_mult[THR_NEARESTG ] = 1000;
891 sf->thresh_mult[THR_ZEROG ] = 1000;
892 sf->thresh_mult[THR_NEARG ] = 1000;
893 sf->thresh_mult[THR_NEWG ] = 2000;
894 sf->thresh_mult[THR_SPLITG ] = 20000;
897 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
899 sf->thresh_mult[THR_NEARESTA ] = 1000;
900 sf->thresh_mult[THR_ZEROA ] = 1000;
901 sf->thresh_mult[THR_NEARA ] = 1000;
902 sf->thresh_mult[THR_NEWA ] = 2000;
903 sf->thresh_mult[THR_SPLITA ] = 20000;
906 sf->improved_quant = 0;
907 sf->improved_dct = 0;
912 cpi->mode_check_freq[THR_SPLITMV] = 7;
913 cpi->mode_check_freq[THR_SPLITG] = 15;
914 cpi->mode_check_freq[THR_SPLITA] = 15;
916 sf->thresh_mult[THR_TM ] = 2000;
917 sf->thresh_mult[THR_V_PRED ] = 2000;
918 sf->thresh_mult[THR_H_PRED ] = 2000;
919 sf->thresh_mult[THR_B_PRED ] = 5000;
921 if (cpi->ref_frame_flags & VP8_LAST_FLAG)
923 sf->thresh_mult[THR_NEWMV ] = 2000;
924 sf->thresh_mult[THR_SPLITMV ] = 25000;
927 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
929 sf->thresh_mult[THR_NEARESTG ] = 2000;
930 sf->thresh_mult[THR_ZEROG ] = 2000;
931 sf->thresh_mult[THR_NEARG ] = 2000;
932 sf->thresh_mult[THR_NEWG ] = 2500;
933 sf->thresh_mult[THR_SPLITG ] = 50000;
936 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
938 sf->thresh_mult[THR_NEARESTA ] = 2000;
939 sf->thresh_mult[THR_ZEROA ] = 2000;
940 sf->thresh_mult[THR_NEARA ] = 2000;
941 sf->thresh_mult[THR_NEWA ] = 2500;
942 sf->thresh_mult[THR_SPLITA ] = 50000;
945 sf->full_freq[0] = 31;
946 sf->full_freq[1] = 63;
951 sf->auto_filter = 0; // Faster selection of loop filter
953 cpi->mode_check_freq[THR_V_PRED] = 2;
954 cpi->mode_check_freq[THR_H_PRED] = 2;
955 cpi->mode_check_freq[THR_B_PRED] = 2;
957 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
959 cpi->mode_check_freq[THR_NEARG] = 2;
960 cpi->mode_check_freq[THR_NEWG] = 4;
963 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
965 cpi->mode_check_freq[THR_NEARA] = 2;
966 cpi->mode_check_freq[THR_NEWA] = 4;
969 sf->thresh_mult[THR_SPLITMV ] = INT_MAX;
970 sf->thresh_mult[THR_SPLITG ] = INT_MAX;
971 sf->thresh_mult[THR_SPLITA ] = INT_MAX;
973 sf->full_freq[0] = 63;
974 sf->full_freq[1] = 127;
980 sf->full_freq[0] = INT_MAX;
981 sf->full_freq[1] = INT_MAX;
988 sf->auto_filter = 0; // Faster selection of loop filter
990 #if CONFIG_REALTIME_ONLY
991 sf->search_method = HEX;
993 sf->search_method = DIAMOND;
995 sf->iterative_sub_pixel = 0;
997 cpi->mode_check_freq[THR_V_PRED] = 4;
998 cpi->mode_check_freq[THR_H_PRED] = 4;
999 cpi->mode_check_freq[THR_B_PRED] = 4;
1001 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
1003 cpi->mode_check_freq[THR_NEARG] = 2;
1004 cpi->mode_check_freq[THR_NEWG] = 4;
1007 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
1009 cpi->mode_check_freq[THR_NEARA] = 2;
1010 cpi->mode_check_freq[THR_NEWA] = 4;
1013 sf->thresh_mult[THR_TM ] = 2000;
1014 sf->thresh_mult[THR_B_PRED ] = 5000;
1016 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
1018 sf->thresh_mult[THR_NEARESTG ] = 2000;
1019 sf->thresh_mult[THR_ZEROG ] = 2000;
1020 sf->thresh_mult[THR_NEARG ] = 2000;
1021 sf->thresh_mult[THR_NEWG ] = 4000;
1024 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
1026 sf->thresh_mult[THR_NEARESTA ] = 2000;
1027 sf->thresh_mult[THR_ZEROA ] = 2000;
1028 sf->thresh_mult[THR_NEARA ] = 2000;
1029 sf->thresh_mult[THR_NEWA ] = 4000;
1035 // Disable split MB intra prediction mode
1036 sf->thresh_mult[THR_B_PRED] = INT_MAX;
1041 unsigned int i, sum = 0;
1042 unsigned int total_mbs = cm->MBs;
1048 if (cpi->oxcf.encode_breakout > 2000)
1049 min = cpi->oxcf.encode_breakout;
1053 for (i = 0; i < min; i++)
1055 sum += cpi->error_bins[i];
1061 // i starts from 2 to make sure thresh started from 2048
1062 for (; i < 1024; i++)
1064 sum += cpi->error_bins[i];
1066 if (10 * sum >= (unsigned int)(cpi->Speed - 6)*(total_mbs - total_skip))
1076 if (cpi->ref_frame_flags & VP8_LAST_FLAG)
1078 sf->thresh_mult[THR_NEWMV] = thresh;
1079 sf->thresh_mult[THR_NEARESTMV ] = thresh >> 1;
1080 sf->thresh_mult[THR_NEARMV ] = thresh >> 1;
1083 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
1085 sf->thresh_mult[THR_NEWG] = thresh << 1;
1086 sf->thresh_mult[THR_NEARESTG ] = thresh;
1087 sf->thresh_mult[THR_NEARG ] = thresh;
1090 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
1092 sf->thresh_mult[THR_NEWA] = thresh << 1;
1093 sf->thresh_mult[THR_NEARESTA ] = thresh;
1094 sf->thresh_mult[THR_NEARA ] = thresh;
1097 // Disable other intra prediction modes
1098 sf->thresh_mult[THR_TM] = INT_MAX;
1099 sf->thresh_mult[THR_V_PRED] = INT_MAX;
1100 sf->thresh_mult[THR_H_PRED] = INT_MAX;
1102 sf->improved_mv_pred = 0;
1107 sf->quarter_pixel_search = 0;
1112 int Tmp = cpi->Speed - 8;
1117 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
1119 cpi->mode_check_freq[THR_ZEROG] = 1 << (Tmp - 1);
1120 cpi->mode_check_freq[THR_NEARESTG] = 1 << (Tmp - 1);
1121 cpi->mode_check_freq[THR_NEARG] = 1 << Tmp;
1122 cpi->mode_check_freq[THR_NEWG] = 1 << (Tmp + 1);
1125 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
1127 cpi->mode_check_freq[THR_ZEROA] = 1 << (Tmp - 1);
1128 cpi->mode_check_freq[THR_NEARESTA] = 1 << (Tmp - 1);
1129 cpi->mode_check_freq[THR_NEARA] = 1 << Tmp;
1130 cpi->mode_check_freq[THR_NEWA] = 1 << (Tmp + 1);
1133 cpi->mode_check_freq[THR_NEWMV] = 1 << (Tmp - 1);
1136 cm->filter_type = NORMAL_LOOPFILTER;
1139 cm->filter_type = SIMPLE_LOOPFILTER;
1143 sf->half_pixel_search = 0; // This has a big hit on quality. Last resort
1146 vpx_memset(cpi->error_bins, 0, sizeof(cpi->error_bins));
1150 /* disable frame modes if flags not set */
1151 if (!(cpi->ref_frame_flags & VP8_LAST_FLAG))
1153 sf->thresh_mult[THR_NEWMV ] = INT_MAX;
1154 sf->thresh_mult[THR_NEARESTMV] = INT_MAX;
1155 sf->thresh_mult[THR_ZEROMV ] = INT_MAX;
1156 sf->thresh_mult[THR_NEARMV ] = INT_MAX;
1157 sf->thresh_mult[THR_SPLITMV ] = INT_MAX;
1160 if (!(cpi->ref_frame_flags & VP8_GOLD_FLAG))
1162 sf->thresh_mult[THR_NEARESTG ] = INT_MAX;
1163 sf->thresh_mult[THR_ZEROG ] = INT_MAX;
1164 sf->thresh_mult[THR_NEARG ] = INT_MAX;
1165 sf->thresh_mult[THR_NEWG ] = INT_MAX;
1166 sf->thresh_mult[THR_SPLITG ] = INT_MAX;
1169 if (!(cpi->ref_frame_flags & VP8_ALT_FLAG))
1171 sf->thresh_mult[THR_NEARESTA ] = INT_MAX;
1172 sf->thresh_mult[THR_ZEROA ] = INT_MAX;
1173 sf->thresh_mult[THR_NEARA ] = INT_MAX;
1174 sf->thresh_mult[THR_NEWA ] = INT_MAX;
1175 sf->thresh_mult[THR_SPLITA ] = INT_MAX;
1179 // Slow quant, dct and trellis not worthwhile for first pass
1180 // so make sure they are always turned off.
1181 if ( cpi->pass == 1 )
1183 sf->improved_quant = 0;
1184 sf->optimize_coefficients = 0;
1185 sf->improved_dct = 0;
1188 if (cpi->sf.search_method == NSTEP)
1190 vp8_init3smotion_compensation(&cpi->mb, cm->yv12_fb[cm->lst_fb_idx].y_stride);
1192 else if (cpi->sf.search_method == DIAMOND)
1194 vp8_init_dsmotion_compensation(&cpi->mb, cm->yv12_fb[cm->lst_fb_idx].y_stride);
1197 if (cpi->sf.improved_dct)
1199 cpi->mb.vp8_short_fdct8x4 = FDCT_INVOKE(&cpi->rtcd.fdct, short8x4);
1200 cpi->mb.vp8_short_fdct4x4 = FDCT_INVOKE(&cpi->rtcd.fdct, short4x4);
1204 cpi->mb.vp8_short_fdct8x4 = FDCT_INVOKE(&cpi->rtcd.fdct, fast8x4);
1205 cpi->mb.vp8_short_fdct4x4 = FDCT_INVOKE(&cpi->rtcd.fdct, fast4x4);
1208 cpi->mb.short_walsh4x4 = FDCT_INVOKE(&cpi->rtcd.fdct, walsh_short4x4);
1210 if (cpi->sf.improved_quant)
1212 cpi->mb.quantize_b = QUANTIZE_INVOKE(&cpi->rtcd.quantize, quantb);
1216 cpi->mb.quantize_b = QUANTIZE_INVOKE(&cpi->rtcd.quantize, fastquantb);
1218 if (cpi->sf.improved_quant != last_improved_quant)
1219 vp8cx_init_quantizer(cpi);
1221 #if CONFIG_RUNTIME_CPU_DETECT
1222 cpi->mb.e_mbd.rtcd = &cpi->common.rtcd;
1225 if (cpi->sf.iterative_sub_pixel == 1)
1227 cpi->find_fractional_mv_step = vp8_find_best_sub_pixel_step_iteratively;
1229 else if (cpi->sf.quarter_pixel_search)
1231 cpi->find_fractional_mv_step = vp8_find_best_sub_pixel_step;
1233 else if (cpi->sf.half_pixel_search)
1235 cpi->find_fractional_mv_step = vp8_find_best_half_pixel_step;
1239 cpi->find_fractional_mv_step = vp8_skip_fractional_mv_step;
1242 if (cpi->sf.optimize_coefficients == 1)
1243 cpi->mb.optimize = 1 + cpi->is_next_src_alt_ref;
1245 cpi->mb.optimize = 0;
1247 if (cpi->common.full_pixel)
1248 cpi->find_fractional_mv_step = vp8_skip_fractional_mv_step;
1251 frames_at_speed[cpi->Speed]++;
1254 static void alloc_raw_frame_buffers(VP8_COMP *cpi)
1257 /* allocate source_buffer to be multiples of 16 */
1258 int width = (cpi->oxcf.Width + 15) & ~15;
1260 buffers = cpi->oxcf.lag_in_frames;
1262 if (buffers > MAX_LAG_BUFFERS)
1263 buffers = MAX_LAG_BUFFERS;
1268 for (i = 0; i < buffers; i++)
1269 if (vp8_yv12_alloc_frame_buffer(&cpi->src_buffer[i].source_buffer,
1270 width, cpi->oxcf.Height,
1272 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1273 "Failed to allocate lag buffer");
1275 #if VP8_TEMPORAL_ALT_REF
1277 if (vp8_yv12_alloc_frame_buffer(&cpi->alt_ref_buffer.source_buffer,
1278 width, cpi->oxcf.Height, 16))
1279 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1280 "Failed to allocate altref buffer");
1284 cpi->source_buffer_count = 0;
1287 static int vp8_alloc_partition_data(VP8_COMP *cpi)
1289 vpx_free(cpi->mb.pip);
1291 cpi->mb.pip = vpx_calloc((cpi->common.mb_cols + 1) *
1292 (cpi->common.mb_rows + 1),
1293 sizeof(PARTITION_INFO));
1297 cpi->mb.pi = cpi->mb.pip + cpi->common.mode_info_stride + 1;
1302 void vp8_alloc_compressor_data(VP8_COMP *cpi)
1304 VP8_COMMON *cm = & cpi->common;
1306 int width = cm->Width;
1307 int height = cm->Height;
1309 if (vp8_alloc_frame_buffers(cm, width, height))
1310 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1311 "Failed to allocate frame buffers");
1313 if (vp8_alloc_partition_data(cpi))
1314 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1315 "Failed to allocate partition data");
1318 if ((width & 0xf) != 0)
1319 width += 16 - (width & 0xf);
1321 if ((height & 0xf) != 0)
1322 height += 16 - (height & 0xf);
1325 if (vp8_yv12_alloc_frame_buffer(&cpi->last_frame_uf,
1326 width, height, VP8BORDERINPIXELS))
1327 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1328 "Failed to allocate last frame buffer");
1330 if (vp8_yv12_alloc_frame_buffer(&cpi->scaled_source, width, height, 16))
1331 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1332 "Failed to allocate scaled source buffer");
1338 unsigned int tokens = cm->mb_rows * cm->mb_cols * 24 * 16;
1340 CHECK_MEM_ERROR(cpi->tok, vpx_calloc(tokens, sizeof(*cpi->tok)));
1343 // Data used for real time vc mode to see if gf needs refreshing
1344 cpi->inter_zz_count = 0;
1345 cpi->gf_bad_count = 0;
1346 cpi->gf_update_recommended = 0;
1349 // Structures used to minitor GF usage
1350 vpx_free(cpi->gf_active_flags);
1352 CHECK_MEM_ERROR(cpi->gf_active_flags, vpx_calloc(1, cm->mb_rows * cm->mb_cols));
1354 cpi->gf_active_count = cm->mb_rows * cm->mb_cols;
1356 #if !(CONFIG_REALTIME_ONLY)
1357 vpx_free(cpi->total_stats);
1359 cpi->total_stats = vpx_calloc(1, sizeof(FIRSTPASS_STATS));
1361 vpx_free(cpi->this_frame_stats);
1363 cpi->this_frame_stats = vpx_calloc(1, sizeof(FIRSTPASS_STATS));
1365 if(!cpi->total_stats || !cpi->this_frame_stats)
1366 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1367 "Failed to allocate firstpass stats");
1370 #if CONFIG_MULTITHREAD
1372 cpi->mt_sync_range = 1;
1373 else if (width <= 1280)
1374 cpi->mt_sync_range = 4;
1375 else if (width <= 2560)
1376 cpi->mt_sync_range = 8;
1378 cpi->mt_sync_range = 16;
1381 vpx_free(cpi->tplist);
1383 CHECK_MEM_ERROR(cpi->tplist, vpx_malloc(sizeof(TOKENLIST) * cpi->common.mb_rows));
1388 static const int q_trans[] =
1390 0, 1, 2, 3, 4, 5, 7, 8,
1391 9, 10, 12, 13, 15, 17, 18, 19,
1392 20, 21, 23, 24, 25, 26, 27, 28,
1393 29, 30, 31, 33, 35, 37, 39, 41,
1394 43, 45, 47, 49, 51, 53, 55, 57,
1395 59, 61, 64, 67, 70, 73, 76, 79,
1396 82, 85, 88, 91, 94, 97, 100, 103,
1397 106, 109, 112, 115, 118, 121, 124, 127,
1400 int vp8_reverse_trans(int x)
1404 for (i = 0; i < 64; i++)
1405 if (q_trans[i] >= x)
1410 void vp8_new_frame_rate(VP8_COMP *cpi, double framerate)
1415 cpi->oxcf.frame_rate = framerate;
1416 cpi->output_frame_rate = cpi->oxcf.frame_rate;
1417 cpi->per_frame_bandwidth = (int)(cpi->oxcf.target_bandwidth / cpi->output_frame_rate);
1418 cpi->av_per_frame_bandwidth = (int)(cpi->oxcf.target_bandwidth / cpi->output_frame_rate);
1419 cpi->min_frame_bandwidth = (int)(cpi->av_per_frame_bandwidth * cpi->oxcf.two_pass_vbrmin_section / 100);
1421 // Set Maximum gf/arf interval
1422 cpi->max_gf_interval = ((int)(cpi->output_frame_rate / 2.0) + 2);
1424 if(cpi->max_gf_interval < 12)
1425 cpi->max_gf_interval = 12;
1427 // Extended interval for genuinely static scenes
1428 cpi->static_scene_max_gf_interval = cpi->key_frame_frequency >> 1;
1430 // Special conditions when altr ref frame enabled in lagged compress mode
1431 if (cpi->oxcf.play_alternate && cpi->oxcf.lag_in_frames)
1433 if (cpi->max_gf_interval > cpi->oxcf.lag_in_frames - 1)
1434 cpi->max_gf_interval = cpi->oxcf.lag_in_frames - 1;
1436 if (cpi->static_scene_max_gf_interval > cpi->oxcf.lag_in_frames - 1)
1437 cpi->static_scene_max_gf_interval = cpi->oxcf.lag_in_frames - 1;
1440 if ( cpi->max_gf_interval > cpi->static_scene_max_gf_interval )
1441 cpi->max_gf_interval = cpi->static_scene_max_gf_interval;
1446 rescale(int val, int num, int denom)
1448 int64_t llnum = num;
1449 int64_t llden = denom;
1450 int64_t llval = val;
1452 return llval * llnum / llden;
1456 void vp8_init_config(VP8_PTR ptr, VP8_CONFIG *oxcf)
1458 VP8_COMP *cpi = (VP8_COMP *)(ptr);
1459 VP8_COMMON *cm = &cpi->common;
1464 cpi->auto_adjust_gold_quantizer = 1;
1465 cpi->goldquantizer = 1;
1467 cpi->auto_adjust_key_quantizer = 1;
1468 cpi->keyquantizer = 1;
1470 cm->version = oxcf->Version;
1471 vp8_setup_version(cm);
1473 // change includes all joint functionality
1474 vp8_change_config(ptr, oxcf);
1476 // Initialize active best and worst q and average q values.
1477 cpi->active_worst_quality = cpi->oxcf.worst_allowed_q;
1478 cpi->active_best_quality = cpi->oxcf.best_allowed_q;
1479 cpi->avg_frame_qindex = cpi->oxcf.worst_allowed_q;
1481 // Initialise the starting buffer levels
1482 cpi->oxcf.starting_buffer_level =
1483 rescale(cpi->oxcf.starting_buffer_level,
1484 cpi->oxcf.target_bandwidth, 1000);
1486 cpi->buffer_level = cpi->oxcf.starting_buffer_level;
1487 cpi->bits_off_target = cpi->oxcf.starting_buffer_level;
1489 cpi->rolling_target_bits = cpi->av_per_frame_bandwidth;
1490 cpi->rolling_actual_bits = cpi->av_per_frame_bandwidth;
1491 cpi->long_rolling_target_bits = cpi->av_per_frame_bandwidth;
1492 cpi->long_rolling_actual_bits = cpi->av_per_frame_bandwidth;
1494 cpi->total_actual_bits = 0;
1495 cpi->total_target_vs_actual = 0;
1497 #if VP8_TEMPORAL_ALT_REF
1501 cpi->fixed_divide[0] = 0;
1503 for (i = 1; i < 512; i++)
1504 cpi->fixed_divide[i] = 0x80000 / i;
1510 void vp8_change_config(VP8_PTR ptr, VP8_CONFIG *oxcf)
1512 VP8_COMP *cpi = (VP8_COMP *)(ptr);
1513 VP8_COMMON *cm = &cpi->common;
1521 if (cm->version != oxcf->Version)
1523 cm->version = oxcf->Version;
1524 vp8_setup_version(cm);
1529 switch (cpi->oxcf.Mode)
1534 cpi->compressor_speed = 2;
1536 if (cpi->oxcf.cpu_used < -16)
1538 cpi->oxcf.cpu_used = -16;
1541 if (cpi->oxcf.cpu_used > 16)
1542 cpi->oxcf.cpu_used = 16;
1546 #if !(CONFIG_REALTIME_ONLY)
1547 case MODE_GOODQUALITY:
1549 cpi->compressor_speed = 1;
1551 if (cpi->oxcf.cpu_used < -5)
1553 cpi->oxcf.cpu_used = -5;
1556 if (cpi->oxcf.cpu_used > 5)
1557 cpi->oxcf.cpu_used = 5;
1561 case MODE_BESTQUALITY:
1563 cpi->compressor_speed = 0;
1566 case MODE_FIRSTPASS:
1568 cpi->compressor_speed = 1;
1570 case MODE_SECONDPASS:
1572 cpi->compressor_speed = 1;
1574 if (cpi->oxcf.cpu_used < -5)
1576 cpi->oxcf.cpu_used = -5;
1579 if (cpi->oxcf.cpu_used > 5)
1580 cpi->oxcf.cpu_used = 5;
1583 case MODE_SECONDPASS_BEST:
1585 cpi->compressor_speed = 0;
1591 cpi->auto_worst_q = 1;
1593 cpi->oxcf.worst_allowed_q = q_trans[oxcf->worst_allowed_q];
1594 cpi->oxcf.best_allowed_q = q_trans[oxcf->best_allowed_q];
1595 cpi->oxcf.cq_level = q_trans[cpi->oxcf.cq_level];
1597 if (oxcf->fixed_q >= 0)
1599 if (oxcf->worst_allowed_q < 0)
1600 cpi->oxcf.fixed_q = q_trans[0];
1602 cpi->oxcf.fixed_q = q_trans[oxcf->worst_allowed_q];
1604 if (oxcf->alt_q < 0)
1605 cpi->oxcf.alt_q = q_trans[0];
1607 cpi->oxcf.alt_q = q_trans[oxcf->alt_q];
1609 if (oxcf->key_q < 0)
1610 cpi->oxcf.key_q = q_trans[0];
1612 cpi->oxcf.key_q = q_trans[oxcf->key_q];
1614 if (oxcf->gold_q < 0)
1615 cpi->oxcf.gold_q = q_trans[0];
1617 cpi->oxcf.gold_q = q_trans[oxcf->gold_q];
1621 cpi->baseline_gf_interval =
1622 cpi->oxcf.alt_freq ? cpi->oxcf.alt_freq : DEFAULT_GF_INTERVAL;
1624 cpi->ref_frame_flags = VP8_ALT_FLAG | VP8_GOLD_FLAG | VP8_LAST_FLAG;
1626 //cpi->use_golden_frame_only = 0;
1627 //cpi->use_last_frame_only = 0;
1628 cm->refresh_golden_frame = 0;
1629 cm->refresh_last_frame = 1;
1630 cm->refresh_entropy_probs = 1;
1632 if (cpi->oxcf.token_partitions >= 0 && cpi->oxcf.token_partitions <= 3)
1633 cm->multi_token_partition =
1634 (TOKEN_PARTITION) cpi->oxcf.token_partitions;
1636 setup_features(cpi);
1641 for (i = 0; i < MAX_MB_SEGMENTS; i++)
1642 cpi->segment_encode_breakout[i] = cpi->oxcf.encode_breakout;
1645 // At the moment the first order values may not be > MAXQ
1646 if (cpi->oxcf.fixed_q > MAXQ)
1647 cpi->oxcf.fixed_q = MAXQ;
1649 // local file playback mode == really big buffer
1650 if (cpi->oxcf.end_usage == USAGE_LOCAL_FILE_PLAYBACK)
1652 cpi->oxcf.starting_buffer_level = 60000;
1653 cpi->oxcf.optimal_buffer_level = 60000;
1654 cpi->oxcf.maximum_buffer_size = 240000;
1657 // Convert target bandwidth from Kbit/s to Bit/s
1658 cpi->oxcf.target_bandwidth *= 1000;
1660 // Set or reset optimal and maximum buffer levels.
1661 if (cpi->oxcf.optimal_buffer_level == 0)
1662 cpi->oxcf.optimal_buffer_level = cpi->oxcf.target_bandwidth / 8;
1664 cpi->oxcf.optimal_buffer_level =
1665 rescale(cpi->oxcf.optimal_buffer_level,
1666 cpi->oxcf.target_bandwidth, 1000);
1668 if (cpi->oxcf.maximum_buffer_size == 0)
1669 cpi->oxcf.maximum_buffer_size = cpi->oxcf.target_bandwidth / 8;
1671 cpi->oxcf.maximum_buffer_size =
1672 rescale(cpi->oxcf.maximum_buffer_size,
1673 cpi->oxcf.target_bandwidth, 1000);
1675 // Set up frame rate and related parameters rate control values.
1676 vp8_new_frame_rate(cpi, cpi->oxcf.frame_rate);
1678 // Set absolute upper and lower quality limits
1679 cpi->worst_quality = cpi->oxcf.worst_allowed_q;
1680 cpi->best_quality = cpi->oxcf.best_allowed_q;
1682 // active values should only be modified if out of new range
1683 if (cpi->active_worst_quality > cpi->oxcf.worst_allowed_q)
1685 cpi->active_worst_quality = cpi->oxcf.worst_allowed_q;
1688 else if (cpi->active_worst_quality < cpi->oxcf.best_allowed_q)
1690 cpi->active_worst_quality = cpi->oxcf.best_allowed_q;
1692 if (cpi->active_best_quality < cpi->oxcf.best_allowed_q)
1694 cpi->active_best_quality = cpi->oxcf.best_allowed_q;
1697 else if (cpi->active_best_quality > cpi->oxcf.worst_allowed_q)
1699 cpi->active_best_quality = cpi->oxcf.worst_allowed_q;
1702 cpi->buffered_mode = (cpi->oxcf.optimal_buffer_level > 0) ? TRUE : FALSE;
1704 cpi->cq_target_quality = cpi->oxcf.cq_level;
1706 // Only allow dropped frames in buffered mode
1707 cpi->drop_frames_allowed = cpi->oxcf.allow_df && cpi->buffered_mode;
1709 cm->filter_type = (LOOPFILTERTYPE) cpi->filter_type;
1711 if (!cm->use_bilinear_mc_filter)
1712 cm->mcomp_filter_type = SIXTAP;
1714 cm->mcomp_filter_type = BILINEAR;
1716 cpi->target_bandwidth = cpi->oxcf.target_bandwidth;
1718 cm->Width = cpi->oxcf.Width ;
1719 cm->Height = cpi->oxcf.Height ;
1721 cm->horiz_scale = cpi->horiz_scale;
1722 cm->vert_scale = cpi->vert_scale ;
1725 cpi->intra_frame_target = (4 * (cm->Width + cm->Height) / 15) * 1000;
1727 // VP8 sharpness level mapping 0-7 (vs 0-10 in general VPx dialogs)
1728 if (cpi->oxcf.Sharpness > 7)
1729 cpi->oxcf.Sharpness = 7;
1731 cm->sharpness_level = cpi->oxcf.Sharpness;
1733 if (cm->horiz_scale != NORMAL || cm->vert_scale != NORMAL)
1735 int UNINITIALIZED_IS_SAFE(hr), UNINITIALIZED_IS_SAFE(hs);
1736 int UNINITIALIZED_IS_SAFE(vr), UNINITIALIZED_IS_SAFE(vs);
1738 Scale2Ratio(cm->horiz_scale, &hr, &hs);
1739 Scale2Ratio(cm->vert_scale, &vr, &vs);
1741 // always go to the next whole number
1742 cm->Width = (hs - 1 + cpi->oxcf.Width * hr) / hs;
1743 cm->Height = (vs - 1 + cpi->oxcf.Height * vr) / vs;
1746 if (((cm->Width + 15) & 0xfffffff0) !=
1747 cm->yv12_fb[cm->lst_fb_idx].y_width ||
1748 ((cm->Height + 15) & 0xfffffff0) !=
1749 cm->yv12_fb[cm->lst_fb_idx].y_height ||
1750 cm->yv12_fb[cm->lst_fb_idx].y_width == 0)
1752 alloc_raw_frame_buffers(cpi);
1753 vp8_alloc_compressor_data(cpi);
1756 // Clamp KF frame size to quarter of data rate
1757 if (cpi->intra_frame_target > cpi->target_bandwidth >> 2)
1758 cpi->intra_frame_target = cpi->target_bandwidth >> 2;
1760 if (cpi->oxcf.fixed_q >= 0)
1762 cpi->last_q[0] = cpi->oxcf.fixed_q;
1763 cpi->last_q[1] = cpi->oxcf.fixed_q;
1766 cpi->Speed = cpi->oxcf.cpu_used;
1768 // force to allowlag to 0 if lag_in_frames is 0;
1769 if (cpi->oxcf.lag_in_frames == 0)
1771 cpi->oxcf.allow_lag = 0;
1773 // Limit on lag buffers as these are not currently dynamically allocated
1774 else if (cpi->oxcf.lag_in_frames > MAX_LAG_BUFFERS)
1775 cpi->oxcf.lag_in_frames = MAX_LAG_BUFFERS;
1778 cpi->last_alt_ref_sei = -1;
1779 cpi->is_src_frame_alt_ref = 0;
1780 cpi->is_next_src_alt_ref = 0;
1783 // Experimental RD Code
1784 cpi->frame_distortion = 0;
1785 cpi->last_frame_distortion = 0;
1790 #define M_LOG2_E 0.693147180559945309417
1791 #define log2f(x) (log (x) / (float) M_LOG2_E)
1792 static void cal_mvsadcosts(int *mvsadcost[2])
1796 mvsadcost [0] [0] = 300;
1797 mvsadcost [1] [0] = 300;
1801 double z = 256 * (2 * (log2f(2 * i) + .6));
1802 mvsadcost [0][i] = (int) z;
1803 mvsadcost [1][i] = (int) z;
1804 mvsadcost [0][-i] = (int) z;
1805 mvsadcost [1][-i] = (int) z;
1807 while (++i <= mv_max);
1810 VP8_PTR vp8_create_compressor(VP8_CONFIG *oxcf)
1822 cpi = ctx.cpi = vpx_memalign(32, sizeof(VP8_COMP));
1823 // Check that the CPI instance is valid
1829 vpx_memset(cpi, 0, sizeof(VP8_COMP));
1831 if (setjmp(cm->error.jmp))
1833 VP8_PTR ptr = ctx.ptr;
1835 ctx.cpi->common.error.setjmp = 0;
1836 vp8_remove_compressor(&ptr);
1840 cpi->common.error.setjmp = 1;
1842 CHECK_MEM_ERROR(cpi->mb.ss, vpx_calloc(sizeof(search_site), (MAX_MVSEARCH_STEPS * 8) + 1));
1844 vp8_create_common(&cpi->common);
1845 vp8_cmachine_specific_config(cpi);
1847 vp8_init_config((VP8_PTR)cpi, oxcf);
1849 memcpy(cpi->base_skip_false_prob, vp8cx_base_skip_false_prob, sizeof(vp8cx_base_skip_false_prob));
1850 cpi->common.current_video_frame = 0;
1851 cpi->kf_overspend_bits = 0;
1852 cpi->kf_bitrate_adjustment = 0;
1853 cpi->frames_till_gf_update_due = 0;
1854 cpi->gf_overspend_bits = 0;
1855 cpi->non_gf_bitrate_adjustment = 0;
1856 cpi->prob_last_coded = 128;
1857 cpi->prob_gf_coded = 128;
1858 cpi->prob_intra_coded = 63;
1860 // Prime the recent reference frame useage counters.
1861 // Hereafter they will be maintained as a sort of moving average
1862 cpi->recent_ref_frame_usage[INTRA_FRAME] = 1;
1863 cpi->recent_ref_frame_usage[LAST_FRAME] = 1;
1864 cpi->recent_ref_frame_usage[GOLDEN_FRAME] = 1;
1865 cpi->recent_ref_frame_usage[ALTREF_FRAME] = 1;
1867 // Set reference frame sign bias for ALTREF frame to 1 (for now)
1868 cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = 1;
1870 cpi->gf_decay_rate = 0;
1871 cpi->baseline_gf_interval = DEFAULT_GF_INTERVAL;
1873 cpi->gold_is_last = 0 ;
1874 cpi->alt_is_last = 0 ;
1875 cpi->gold_is_alt = 0 ;
1877 // allocate memory for storing last frame's MVs for MV prediction.
1878 CHECK_MEM_ERROR(cpi->lfmv, vpx_calloc((cpi->common.mb_rows+2) * (cpi->common.mb_cols+2), sizeof(int_mv)));
1879 CHECK_MEM_ERROR(cpi->lf_ref_frame_sign_bias, vpx_calloc((cpi->common.mb_rows+2) * (cpi->common.mb_cols+2), sizeof(int)));
1880 CHECK_MEM_ERROR(cpi->lf_ref_frame, vpx_calloc((cpi->common.mb_rows+2) * (cpi->common.mb_cols+2), sizeof(int)));
1882 // Create the encoder segmentation map and set all entries to 0
1883 CHECK_MEM_ERROR(cpi->segmentation_map, vpx_calloc(cpi->common.mb_rows * cpi->common.mb_cols, 1));
1884 CHECK_MEM_ERROR(cpi->active_map, vpx_calloc(cpi->common.mb_rows * cpi->common.mb_cols, 1));
1885 vpx_memset(cpi->active_map , 1, (cpi->common.mb_rows * cpi->common.mb_cols));
1886 cpi->active_map_enabled = 0;
1889 // Experimental code for lagged and one pass
1890 // Initialise one_pass GF frames stats
1891 // Update stats used for GF selection
1894 cpi->one_pass_frame_index = 0;
1896 for (i = 0; i < MAX_LAG_BUFFERS; i++)
1898 cpi->one_pass_frame_stats[i].frames_so_far = 0;
1899 cpi->one_pass_frame_stats[i].frame_intra_error = 0.0;
1900 cpi->one_pass_frame_stats[i].frame_coded_error = 0.0;
1901 cpi->one_pass_frame_stats[i].frame_pcnt_inter = 0.0;
1902 cpi->one_pass_frame_stats[i].frame_pcnt_motion = 0.0;
1903 cpi->one_pass_frame_stats[i].frame_mvr = 0.0;
1904 cpi->one_pass_frame_stats[i].frame_mvr_abs = 0.0;
1905 cpi->one_pass_frame_stats[i].frame_mvc = 0.0;
1906 cpi->one_pass_frame_stats[i].frame_mvc_abs = 0.0;
1911 // Should we use the cyclic refresh method.
1912 // Currently this is tied to error resilliant mode
1913 cpi->cyclic_refresh_mode_enabled = cpi->oxcf.error_resilient_mode;
1914 cpi->cyclic_refresh_mode_max_mbs_perframe = (cpi->common.mb_rows * cpi->common.mb_cols) / 40;
1915 cpi->cyclic_refresh_mode_index = 0;
1916 cpi->cyclic_refresh_q = 32;
1918 if (cpi->cyclic_refresh_mode_enabled)
1920 CHECK_MEM_ERROR(cpi->cyclic_refresh_map, vpx_calloc((cpi->common.mb_rows * cpi->common.mb_cols), 1));
1923 cpi->cyclic_refresh_map = (signed char *) NULL;
1925 // Test function for segmentation
1926 //segmentation_test_function((VP8_PTR) cpi);
1928 #ifdef ENTROPY_STATS
1929 init_context_counters();
1932 /*Initialize the feed-forward activity masking.*/
1933 cpi->activity_avg = 90<<12;
1935 cpi->frames_since_key = 8; // Give a sensible default for the first frame.
1936 cpi->key_frame_frequency = cpi->oxcf.key_freq;
1937 cpi->this_key_frame_forced = FALSE;
1938 cpi->next_key_frame_forced = FALSE;
1940 cpi->source_alt_ref_pending = FALSE;
1941 cpi->source_alt_ref_active = FALSE;
1942 cpi->common.refresh_alt_ref_frame = 0;
1944 cpi->b_calculate_psnr = CONFIG_PSNR;
1946 cpi->b_calculate_ssimg = 0;
1951 if (cpi->b_calculate_psnr)
1953 cpi->total_sq_error = 0.0;
1954 cpi->total_sq_error2 = 0.0;
1959 cpi->totalp_y = 0.0;
1960 cpi->totalp_u = 0.0;
1961 cpi->totalp_v = 0.0;
1963 cpi->tot_recode_hits = 0;
1964 cpi->summed_quality = 0;
1965 cpi->summed_weights = 0;
1968 if (cpi->b_calculate_ssimg)
1970 cpi->total_ssimg_y = 0;
1971 cpi->total_ssimg_u = 0;
1972 cpi->total_ssimg_v = 0;
1973 cpi->total_ssimg_all = 0;
1977 #define LLONG_MAX 9223372036854775807LL
1979 cpi->first_time_stamp_ever = LLONG_MAX;
1983 cpi->frames_till_gf_update_due = 0;
1984 cpi->key_frame_count = 1;
1985 cpi->tot_key_frame_bits = 0;
1987 cpi->ni_av_qi = cpi->oxcf.worst_allowed_q;
1990 cpi->total_byte_count = 0;
1992 cpi->drop_frame = 0;
1993 cpi->drop_count = 0;
1994 cpi->max_drop_count = 0;
1995 cpi->max_consec_dropped_frames = 4;
1997 cpi->rate_correction_factor = 1.0;
1998 cpi->key_frame_rate_correction_factor = 1.0;
1999 cpi->gf_rate_correction_factor = 1.0;
2000 cpi->est_max_qcorrection_factor = 1.0;
2002 cpi->mb.mvcost[0] = &cpi->mb.mvcosts[0][mv_max+1];
2003 cpi->mb.mvcost[1] = &cpi->mb.mvcosts[1][mv_max+1];
2004 cpi->mb.mvsadcost[0] = &cpi->mb.mvsadcosts[0][mv_max+1];
2005 cpi->mb.mvsadcost[1] = &cpi->mb.mvsadcosts[1][mv_max+1];
2007 cal_mvsadcosts(cpi->mb.mvsadcost);
2009 for (i = 0; i < KEY_FRAME_CONTEXT; i++)
2011 cpi->prior_key_frame_size[i] = cpi->intra_frame_target;
2012 cpi->prior_key_frame_distance[i] = (int)cpi->output_frame_rate;
2015 cpi->check_freq[0] = 15;
2016 cpi->check_freq[1] = 15;
2018 #ifdef OUTPUT_YUV_SRC
2019 yuv_file = fopen("bd.yuv", "ab");
2023 framepsnr = fopen("framepsnr.stt", "a");
2024 kf_list = fopen("kf_list.stt", "w");
2027 cpi->output_pkt_list = oxcf->output_pkt_list;
2029 #if !(CONFIG_REALTIME_ONLY)
2033 vp8_init_first_pass(cpi);
2035 else if (cpi->pass == 2)
2037 size_t packet_sz = sizeof(FIRSTPASS_STATS);
2038 int packets = oxcf->two_pass_stats_in.sz / packet_sz;
2040 cpi->stats_in = oxcf->two_pass_stats_in.buf;
2041 cpi->stats_in_end = (void*)((char *)cpi->stats_in
2042 + (packets - 1) * packet_sz);
2043 vp8_init_second_pass(cpi);
2048 if (cpi->compressor_speed == 2)
2050 cpi->cpu_freq = 0; //vp8_get_processor_freq();
2051 cpi->avg_encode_time = 0;
2052 cpi->avg_pick_mode_time = 0;
2055 vp8_set_speed_features(cpi);
2057 // Set starting values of RD threshold multipliers (128 = *1)
2058 for (i = 0; i < MAX_MODES; i++)
2060 cpi->rd_thresh_mult[i] = 128;
2063 #ifdef ENTROPY_STATS
2064 init_mv_ref_counts();
2067 #if CONFIG_MULTITHREAD
2068 vp8cx_create_encoder_threads(cpi);
2071 cpi->fn_ptr[BLOCK_16X16].sdf = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x16);
2072 cpi->fn_ptr[BLOCK_16X16].vf = VARIANCE_INVOKE(&cpi->rtcd.variance, var16x16);
2073 cpi->fn_ptr[BLOCK_16X16].svf = VARIANCE_INVOKE(&cpi->rtcd.variance, subpixvar16x16);
2074 cpi->fn_ptr[BLOCK_16X16].svf_halfpix_h = VARIANCE_INVOKE(&cpi->rtcd.variance, halfpixvar16x16_h);
2075 cpi->fn_ptr[BLOCK_16X16].svf_halfpix_v = VARIANCE_INVOKE(&cpi->rtcd.variance, halfpixvar16x16_v);
2076 cpi->fn_ptr[BLOCK_16X16].svf_halfpix_hv = VARIANCE_INVOKE(&cpi->rtcd.variance, halfpixvar16x16_hv);
2077 cpi->fn_ptr[BLOCK_16X16].sdx3f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x16x3);
2078 cpi->fn_ptr[BLOCK_16X16].sdx8f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x16x8);
2079 cpi->fn_ptr[BLOCK_16X16].sdx4df = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x16x4d);
2081 cpi->fn_ptr[BLOCK_16X8].sdf = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x8);
2082 cpi->fn_ptr[BLOCK_16X8].vf = VARIANCE_INVOKE(&cpi->rtcd.variance, var16x8);
2083 cpi->fn_ptr[BLOCK_16X8].svf = VARIANCE_INVOKE(&cpi->rtcd.variance, subpixvar16x8);
2084 cpi->fn_ptr[BLOCK_16X8].svf_halfpix_h = NULL;
2085 cpi->fn_ptr[BLOCK_16X8].svf_halfpix_v = NULL;
2086 cpi->fn_ptr[BLOCK_16X8].svf_halfpix_hv = NULL;
2087 cpi->fn_ptr[BLOCK_16X8].sdx3f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x8x3);
2088 cpi->fn_ptr[BLOCK_16X8].sdx8f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x8x8);
2089 cpi->fn_ptr[BLOCK_16X8].sdx4df = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x8x4d);
2091 cpi->fn_ptr[BLOCK_8X16].sdf = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x16);
2092 cpi->fn_ptr[BLOCK_8X16].vf = VARIANCE_INVOKE(&cpi->rtcd.variance, var8x16);
2093 cpi->fn_ptr[BLOCK_8X16].svf = VARIANCE_INVOKE(&cpi->rtcd.variance, subpixvar8x16);
2094 cpi->fn_ptr[BLOCK_8X16].svf_halfpix_h = NULL;
2095 cpi->fn_ptr[BLOCK_8X16].svf_halfpix_v = NULL;
2096 cpi->fn_ptr[BLOCK_8X16].svf_halfpix_hv = NULL;
2097 cpi->fn_ptr[BLOCK_8X16].sdx3f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x16x3);
2098 cpi->fn_ptr[BLOCK_8X16].sdx8f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x16x8);
2099 cpi->fn_ptr[BLOCK_8X16].sdx4df = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x16x4d);
2101 cpi->fn_ptr[BLOCK_8X8].sdf = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x8);
2102 cpi->fn_ptr[BLOCK_8X8].vf = VARIANCE_INVOKE(&cpi->rtcd.variance, var8x8);
2103 cpi->fn_ptr[BLOCK_8X8].svf = VARIANCE_INVOKE(&cpi->rtcd.variance, subpixvar8x8);
2104 cpi->fn_ptr[BLOCK_8X8].svf_halfpix_h = NULL;
2105 cpi->fn_ptr[BLOCK_8X8].svf_halfpix_v = NULL;
2106 cpi->fn_ptr[BLOCK_8X8].svf_halfpix_hv = NULL;
2107 cpi->fn_ptr[BLOCK_8X8].sdx3f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x8x3);
2108 cpi->fn_ptr[BLOCK_8X8].sdx8f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x8x8);
2109 cpi->fn_ptr[BLOCK_8X8].sdx4df = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x8x4d);
2111 cpi->fn_ptr[BLOCK_4X4].sdf = VARIANCE_INVOKE(&cpi->rtcd.variance, sad4x4);
2112 cpi->fn_ptr[BLOCK_4X4].vf = VARIANCE_INVOKE(&cpi->rtcd.variance, var4x4);
2113 cpi->fn_ptr[BLOCK_4X4].svf = VARIANCE_INVOKE(&cpi->rtcd.variance, subpixvar4x4);
2114 cpi->fn_ptr[BLOCK_4X4].svf_halfpix_h = NULL;
2115 cpi->fn_ptr[BLOCK_4X4].svf_halfpix_v = NULL;
2116 cpi->fn_ptr[BLOCK_4X4].svf_halfpix_hv = NULL;
2117 cpi->fn_ptr[BLOCK_4X4].sdx3f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad4x4x3);
2118 cpi->fn_ptr[BLOCK_4X4].sdx8f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad4x4x8);
2119 cpi->fn_ptr[BLOCK_4X4].sdx4df = VARIANCE_INVOKE(&cpi->rtcd.variance, sad4x4x4d);
2121 #if !(CONFIG_REALTIME_ONLY)
2122 cpi->full_search_sad = SEARCH_INVOKE(&cpi->rtcd.search, full_search);
2124 cpi->diamond_search_sad = SEARCH_INVOKE(&cpi->rtcd.search, diamond_search);
2126 cpi->ready_for_new_frame = 1;
2128 cpi->source_encode_index = 0;
2130 // make sure frame 1 is okay
2131 cpi->error_bins[0] = cpi->common.MBs;
2133 //vp8cx_init_quantizer() is first called here. Add check in vp8cx_frame_init_quantizer() so that vp8cx_init_quantizer is only called later
2134 //when needed. This will avoid unnecessary calls of vp8cx_init_quantizer() for every frame.
2135 vp8cx_init_quantizer(cpi);
2137 vp8_init_loop_filter(cm);
2138 cm->last_frame_type = KEY_FRAME;
2139 cm->last_filter_type = cm->filter_type;
2140 cm->last_sharpness_level = cm->sharpness_level;
2142 cpi->common.error.setjmp = 0;
2143 return (VP8_PTR) cpi;
2148 void vp8_remove_compressor(VP8_PTR *ptr)
2150 VP8_COMP *cpi = (VP8_COMP *)(*ptr);
2155 if (cpi && (cpi->common.current_video_frame > 0))
2157 #if !(CONFIG_REALTIME_ONLY)
2161 vp8_end_second_pass(cpi);
2166 #ifdef ENTROPY_STATS
2167 print_context_counters();
2168 print_tree_update_probs();
2169 print_mode_context();
2176 FILE *f = fopen("opsnr.stt", "a");
2177 double time_encoded = (cpi->source_end_time_stamp - cpi->first_time_stamp_ever) / 10000000.000;
2178 double total_encode_time = (cpi->time_receive_data + cpi->time_compress_data) / 1000.000;
2179 double dr = (double)cpi->bytes * (double) 8 / (double)1000 / time_encoded;
2181 if (cpi->b_calculate_psnr)
2183 YV12_BUFFER_CONFIG *lst_yv12 = &cpi->common.yv12_fb[cpi->common.lst_fb_idx];
2184 double samples = 3.0 / 2 * cpi->count * lst_yv12->y_width * lst_yv12->y_height;
2185 double total_psnr = vp8_mse2psnr(samples, 255.0, cpi->total_sq_error);
2186 double total_psnr2 = vp8_mse2psnr(samples, 255.0, cpi->total_sq_error2);
2187 double total_ssim = 100 * pow(cpi->summed_quality / cpi->summed_weights, 8.0);
2189 fprintf(f, "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\tVPXSSIM\t Time(us)\n");
2190 fprintf(f, "%7.3f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t%7.3f %8.0f\n",
2191 dr, cpi->total / cpi->count, total_psnr, cpi->totalp / cpi->count, total_psnr2, total_ssim,
2195 if (cpi->b_calculate_ssimg)
2197 fprintf(f, "BitRate\tSSIM_Y\tSSIM_U\tSSIM_V\tSSIM_A\t Time(us)\n");
2198 fprintf(f, "%7.3f\t%6.4f\t%6.4f\t%6.4f\t%6.4f\t%8.0f\n", dr,
2199 cpi->total_ssimg_y / cpi->count, cpi->total_ssimg_u / cpi->count,
2200 cpi->total_ssimg_v / cpi->count, cpi->total_ssimg_all / cpi->count, total_encode_time);
2205 f = fopen("qskip.stt", "a");
2206 fprintf(f, "minq:%d -maxq:%d skipture:skipfalse = %d:%d\n", cpi->oxcf.best_allowed_q, cpi->oxcf.worst_allowed_q, skiptruecount, skipfalsecount);
2217 if (cpi->compressor_speed == 2)
2220 FILE *f = fopen("cxspeed.stt", "a");
2221 cnt_pm /= cpi->common.MBs;
2223 for (i = 0; i < 16; i++)
2224 fprintf(f, "%5d", frames_at_speed[i]);
2227 //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);
2236 extern int count_mb_seg[4];
2237 FILE *f = fopen("modes.stt", "a");
2238 double dr = (double)cpi->oxcf.frame_rate * (double)bytes * (double)8 / (double)count / (double)1000 ;
2239 fprintf(f, "intra_mode in Intra Frames:\n");
2240 fprintf(f, "Y: %8d, %8d, %8d, %8d, %8d\n", y_modes[0], y_modes[1], y_modes[2], y_modes[3], y_modes[4]);
2241 fprintf(f, "UV:%8d, %8d, %8d, %8d\n", uv_modes[0], uv_modes[1], uv_modes[2], uv_modes[3]);
2246 for (i = 0; i < 10; i++)
2247 fprintf(f, "%8d, ", b_modes[i]);
2253 fprintf(f, "Modes in Inter Frames:\n");
2254 fprintf(f, "Y: %8d, %8d, %8d, %8d, %8d, %8d, %8d, %8d, %8d, %8d\n",
2255 inter_y_modes[0], inter_y_modes[1], inter_y_modes[2], inter_y_modes[3], inter_y_modes[4],
2256 inter_y_modes[5], inter_y_modes[6], inter_y_modes[7], inter_y_modes[8], inter_y_modes[9]);
2257 fprintf(f, "UV:%8d, %8d, %8d, %8d\n", inter_uv_modes[0], inter_uv_modes[1], inter_uv_modes[2], inter_uv_modes[3]);
2262 for (i = 0; i < 15; i++)
2263 fprintf(f, "%8d, ", inter_b_modes[i]);
2268 fprintf(f, "P:%8d, %8d, %8d, %8d\n", count_mb_seg[0], count_mb_seg[1], count_mb_seg[2], count_mb_seg[3]);
2269 fprintf(f, "PB:%8d, %8d, %8d, %8d\n", inter_b_modes[LEFT4X4], inter_b_modes[ABOVE4X4], inter_b_modes[ZERO4X4], inter_b_modes[NEW4X4]);
2277 #ifdef ENTROPY_STATS
2280 FILE *fmode = fopen("modecontext.c", "w");
2282 fprintf(fmode, "\n#include \"entropymode.h\"\n\n");
2283 fprintf(fmode, "const unsigned int vp8_kf_default_bmode_counts ");
2284 fprintf(fmode, "[VP8_BINTRAMODES] [VP8_BINTRAMODES] [VP8_BINTRAMODES] =\n{\n");
2286 for (i = 0; i < 10; i++)
2289 fprintf(fmode, " { //Above Mode : %d\n", i);
2291 for (j = 0; j < 10; j++)
2294 fprintf(fmode, " {");
2296 for (k = 0; k < 10; k++)
2298 if (!intra_mode_stats[i][j][k])
2299 fprintf(fmode, " %5d, ", 1);
2301 fprintf(fmode, " %5d, ", intra_mode_stats[i][j][k]);
2304 fprintf(fmode, "}, // left_mode %d\n", j);
2308 fprintf(fmode, " },\n");
2312 fprintf(fmode, "};\n");
2318 #if defined(SECTIONBITS_OUTPUT)
2323 FILE *f = fopen("tokenbits.stt", "a");
2325 for (i = 0; i < 28; i++)
2326 fprintf(f, "%8d", (int)(Sectionbits[i] / 256));
2336 printf("\n_pick_loop_filter_level:%d\n", cpi->time_pick_lpf / 1000);
2337 printf("\n_frames recive_data encod_mb_row compress_frame Total\n");
2338 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);
2344 #if CONFIG_MULTITHREAD
2345 vp8cx_remove_encoder_threads(cpi);
2348 vp8_dealloc_compressor_data(cpi);
2349 vpx_free(cpi->mb.ss);
2351 vpx_free(cpi->cyclic_refresh_map);
2353 vp8_remove_common(&cpi->common);
2357 #ifdef OUTPUT_YUV_SRC
2377 static uint64_t calc_plane_error(unsigned char *orig, int orig_stride,
2378 unsigned char *recon, int recon_stride,
2379 unsigned int cols, unsigned int rows,
2380 vp8_variance_rtcd_vtable_t *rtcd)
2382 unsigned int row, col;
2383 uint64_t total_sse = 0;
2386 for (row = 0; row + 16 <= rows; row += 16)
2388 for (col = 0; col + 16 <= cols; col += 16)
2392 VARIANCE_INVOKE(rtcd, mse16x16)(orig + col, orig_stride,
2393 recon + col, recon_stride,
2398 /* Handle odd-sized width */
2401 unsigned int border_row, border_col;
2402 unsigned char *border_orig = orig;
2403 unsigned char *border_recon = recon;
2405 for (border_row = 0; border_row < 16; border_row++)
2407 for (border_col = col; border_col < cols; border_col++)
2409 diff = border_orig[border_col] - border_recon[border_col];
2410 total_sse += diff * diff;
2413 border_orig += orig_stride;
2414 border_recon += recon_stride;
2418 orig += orig_stride * 16;
2419 recon += recon_stride * 16;
2422 /* Handle odd-sized height */
2423 for (; row < rows; row++)
2425 for (col = 0; col < cols; col++)
2427 diff = orig[col] - recon[col];
2428 total_sse += diff * diff;
2431 orig += orig_stride;
2432 recon += recon_stride;
2439 static void generate_psnr_packet(VP8_COMP *cpi)
2441 YV12_BUFFER_CONFIG *orig = cpi->Source;
2442 YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
2443 struct vpx_codec_cx_pkt pkt;
2446 unsigned int width = cpi->common.Width;
2447 unsigned int height = cpi->common.Height;
2449 pkt.kind = VPX_CODEC_PSNR_PKT;
2450 sse = calc_plane_error(orig->y_buffer, orig->y_stride,
2451 recon->y_buffer, recon->y_stride,
2453 IF_RTCD(&cpi->rtcd.variance));
2454 pkt.data.psnr.sse[0] = sse;
2455 pkt.data.psnr.sse[1] = sse;
2456 pkt.data.psnr.samples[0] = width * height;
2457 pkt.data.psnr.samples[1] = width * height;
2459 width = (width + 1) / 2;
2460 height = (height + 1) / 2;
2462 sse = calc_plane_error(orig->u_buffer, orig->uv_stride,
2463 recon->u_buffer, recon->uv_stride,
2465 IF_RTCD(&cpi->rtcd.variance));
2466 pkt.data.psnr.sse[0] += sse;
2467 pkt.data.psnr.sse[2] = sse;
2468 pkt.data.psnr.samples[0] += width * height;
2469 pkt.data.psnr.samples[2] = width * height;
2471 sse = calc_plane_error(orig->v_buffer, orig->uv_stride,
2472 recon->v_buffer, recon->uv_stride,
2474 IF_RTCD(&cpi->rtcd.variance));
2475 pkt.data.psnr.sse[0] += sse;
2476 pkt.data.psnr.sse[3] = sse;
2477 pkt.data.psnr.samples[0] += width * height;
2478 pkt.data.psnr.samples[3] = width * height;
2480 for (i = 0; i < 4; i++)
2481 pkt.data.psnr.psnr[i] = vp8_mse2psnr(pkt.data.psnr.samples[i], 255.0,
2482 pkt.data.psnr.sse[i]);
2484 vpx_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
2488 int vp8_use_as_reference(VP8_PTR ptr, int ref_frame_flags)
2490 VP8_COMP *cpi = (VP8_COMP *)(ptr);
2492 if (ref_frame_flags > 7)
2495 cpi->ref_frame_flags = ref_frame_flags;
2498 int vp8_update_reference(VP8_PTR ptr, int ref_frame_flags)
2500 VP8_COMP *cpi = (VP8_COMP *)(ptr);
2502 if (ref_frame_flags > 7)
2505 cpi->common.refresh_golden_frame = 0;
2506 cpi->common.refresh_alt_ref_frame = 0;
2507 cpi->common.refresh_last_frame = 0;
2509 if (ref_frame_flags & VP8_LAST_FLAG)
2510 cpi->common.refresh_last_frame = 1;
2512 if (ref_frame_flags & VP8_GOLD_FLAG)
2513 cpi->common.refresh_golden_frame = 1;
2515 if (ref_frame_flags & VP8_ALT_FLAG)
2516 cpi->common.refresh_alt_ref_frame = 1;
2521 int vp8_get_reference(VP8_PTR ptr, VP8_REFFRAME ref_frame_flag, YV12_BUFFER_CONFIG *sd)
2523 VP8_COMP *cpi = (VP8_COMP *)(ptr);
2524 VP8_COMMON *cm = &cpi->common;
2527 if (ref_frame_flag == VP8_LAST_FLAG)
2528 ref_fb_idx = cm->lst_fb_idx;
2529 else if (ref_frame_flag == VP8_GOLD_FLAG)
2530 ref_fb_idx = cm->gld_fb_idx;
2531 else if (ref_frame_flag == VP8_ALT_FLAG)
2532 ref_fb_idx = cm->alt_fb_idx;
2536 vp8_yv12_copy_frame_ptr(&cm->yv12_fb[ref_fb_idx], sd);
2540 int vp8_set_reference(VP8_PTR ptr, VP8_REFFRAME ref_frame_flag, YV12_BUFFER_CONFIG *sd)
2542 VP8_COMP *cpi = (VP8_COMP *)(ptr);
2543 VP8_COMMON *cm = &cpi->common;
2547 if (ref_frame_flag == VP8_LAST_FLAG)
2548 ref_fb_idx = cm->lst_fb_idx;
2549 else if (ref_frame_flag == VP8_GOLD_FLAG)
2550 ref_fb_idx = cm->gld_fb_idx;
2551 else if (ref_frame_flag == VP8_ALT_FLAG)
2552 ref_fb_idx = cm->alt_fb_idx;
2556 vp8_yv12_copy_frame_ptr(sd, &cm->yv12_fb[ref_fb_idx]);
2560 int vp8_update_entropy(VP8_PTR comp, int update)
2562 VP8_COMP *cpi = (VP8_COMP *) comp;
2563 VP8_COMMON *cm = &cpi->common;
2564 cm->refresh_entropy_probs = update;
2571 void vp8_write_yuv_frame(const char *name, YV12_BUFFER_CONFIG *s)
2573 FILE *yuv_file = fopen(name, "ab");
2574 unsigned char *src = s->y_buffer;
2575 int h = s->y_height;
2579 fwrite(src, s->y_width, 1, yuv_file);
2589 fwrite(src, s->uv_width, 1, yuv_file);
2590 src += s->uv_stride;
2599 fwrite(src, s->uv_width, 1, yuv_file);
2600 src += s->uv_stride;
2609 static void scale_and_extend_source(YV12_BUFFER_CONFIG *sd, VP8_COMP *cpi)
2611 VP8_COMMON *cm = &cpi->common;
2613 // are we resizing the image
2614 if (cm->horiz_scale != 0 || cm->vert_scale != 0)
2616 #if CONFIG_SPATIAL_RESAMPLING
2617 int UNINITIALIZED_IS_SAFE(hr), UNINITIALIZED_IS_SAFE(hs);
2618 int UNINITIALIZED_IS_SAFE(vr), UNINITIALIZED_IS_SAFE(vs);
2621 if (cm->vert_scale == 3)
2626 Scale2Ratio(cm->horiz_scale, &hr, &hs);
2627 Scale2Ratio(cm->vert_scale, &vr, &vs);
2629 vp8_scale_frame(sd, &cpi->scaled_source, cm->temp_scale_frame.y_buffer,
2630 tmp_height, hs, hr, vs, vr, 0);
2632 cpi->Source = &cpi->scaled_source;
2635 // we may need to copy to a buffer so we can extend the image...
2636 else if (cm->Width != cm->yv12_fb[cm->lst_fb_idx].y_width ||
2637 cm->Height != cm->yv12_fb[cm->lst_fb_idx].y_height)
2639 //vp8_yv12_copy_frame_ptr(sd, &cpi->scaled_source);
2641 #if CONFIG_RUNTIME_CPU_DETECT
2642 if (cm->rtcd.flags & HAS_NEON)
2645 vp8_yv12_copy_src_frame_func_neon(sd, &cpi->scaled_source);
2647 #if CONFIG_RUNTIME_CPU_DETECT
2651 #if !HAVE_ARMV7 || CONFIG_RUNTIME_CPU_DETECT
2653 vp8_yv12_copy_frame_ptr(sd, &cpi->scaled_source);
2657 cpi->Source = &cpi->scaled_source;
2660 vp8_extend_to_multiple_of16(cpi->Source, cm->Width, cm->Height);
2663 static void resize_key_frame(VP8_COMP *cpi)
2665 #if CONFIG_SPATIAL_RESAMPLING
2666 VP8_COMMON *cm = &cpi->common;
2668 // Do we need to apply resampling for one pass cbr.
2669 // In one pass this is more limited than in two pass cbr
2670 // The test and any change is only made one per key frame sequence
2671 if (cpi->oxcf.allow_spatial_resampling && (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER))
2673 int UNINITIALIZED_IS_SAFE(hr), UNINITIALIZED_IS_SAFE(hs);
2674 int UNINITIALIZED_IS_SAFE(vr), UNINITIALIZED_IS_SAFE(vs);
2675 int new_width, new_height;
2677 // If we are below the resample DOWN watermark then scale down a notch.
2678 if (cpi->buffer_level < (cpi->oxcf.resample_down_water_mark * cpi->oxcf.optimal_buffer_level / 100))
2680 cm->horiz_scale = (cm->horiz_scale < ONETWO) ? cm->horiz_scale + 1 : ONETWO;
2681 cm->vert_scale = (cm->vert_scale < ONETWO) ? cm->vert_scale + 1 : ONETWO;
2683 // Should we now start scaling back up
2684 else if (cpi->buffer_level > (cpi->oxcf.resample_up_water_mark * cpi->oxcf.optimal_buffer_level / 100))
2686 cm->horiz_scale = (cm->horiz_scale > NORMAL) ? cm->horiz_scale - 1 : NORMAL;
2687 cm->vert_scale = (cm->vert_scale > NORMAL) ? cm->vert_scale - 1 : NORMAL;
2690 // Get the new hieght and width
2691 Scale2Ratio(cm->horiz_scale, &hr, &hs);
2692 Scale2Ratio(cm->vert_scale, &vr, &vs);
2693 new_width = ((hs - 1) + (cpi->oxcf.Width * hr)) / hs;
2694 new_height = ((vs - 1) + (cpi->oxcf.Height * vr)) / vs;
2696 // If the image size has changed we need to reallocate the buffers
2697 // and resample the source image
2698 if ((cm->Width != new_width) || (cm->Height != new_height))
2700 cm->Width = new_width;
2701 cm->Height = new_height;
2702 vp8_alloc_compressor_data(cpi);
2703 scale_and_extend_source(cpi->un_scaled_source, cpi);
2709 // return of 0 means drop frame
2710 static int pick_frame_size(VP8_COMP *cpi)
2712 VP8_COMMON *cm = &cpi->common;
2714 // First Frame is a special case
2715 if (cm->current_video_frame == 0)
2717 #if !(CONFIG_REALTIME_ONLY)
2720 vp8_calc_auto_iframe_target_size(cpi);
2722 // 1 Pass there is no information on which to base size so use bandwidth per second * fixed fraction
2725 cpi->this_frame_target = cpi->oxcf.target_bandwidth / 2;
2727 // in error resilient mode the first frame is bigger since it likely contains
2728 // all the static background
2729 if (cpi->oxcf.error_resilient_mode == 1 || (cpi->compressor_speed == 2))
2731 cpi->this_frame_target *= 3; // 5;
2734 // Key frame from VFW/auto-keyframe/first frame
2735 cm->frame_type = KEY_FRAME;
2738 // Special case for forced key frames
2739 // The frame sizing here is still far from ideal for 2 pass.
2740 else if (cm->frame_flags & FRAMEFLAGS_KEY)
2742 cm->frame_type = KEY_FRAME;
2743 resize_key_frame(cpi);
2744 vp8_calc_iframe_target_size(cpi);
2746 else if (cm->frame_type == KEY_FRAME)
2748 vp8_calc_auto_iframe_target_size(cpi);
2752 // INTER frame: compute target frame size
2753 cm->frame_type = INTER_FRAME;
2754 vp8_calc_pframe_target_size(cpi);
2756 // Check if we're dropping the frame:
2757 if (cpi->drop_frame)
2759 cpi->drop_frame = FALSE;
2768 static void set_quantizer(VP8_COMP *cpi, int Q)
2770 VP8_COMMON *cm = &cpi->common;
2771 MACROBLOCKD *mbd = &cpi->mb.e_mbd;
2774 cm->base_qindex = Q;
2776 /* if any of the delta_q values are changing update flag has to be set */
2777 /* currently only y2dc_delta_q may change */
2779 cm->y1dc_delta_q = 0;
2780 cm->y2ac_delta_q = 0;
2781 cm->uvdc_delta_q = 0;
2782 cm->uvac_delta_q = 0;
2791 update |= cm->y2dc_delta_q != new_delta_q;
2792 cm->y2dc_delta_q = new_delta_q;
2795 // Set Segment specific quatizers
2796 mbd->segment_feature_data[MB_LVL_ALT_Q][0] = cpi->segment_feature_data[MB_LVL_ALT_Q][0];
2797 mbd->segment_feature_data[MB_LVL_ALT_Q][1] = cpi->segment_feature_data[MB_LVL_ALT_Q][1];
2798 mbd->segment_feature_data[MB_LVL_ALT_Q][2] = cpi->segment_feature_data[MB_LVL_ALT_Q][2];
2799 mbd->segment_feature_data[MB_LVL_ALT_Q][3] = cpi->segment_feature_data[MB_LVL_ALT_Q][3];
2801 /* quantizer has to be reinitialized for any delta_q changes */
2803 vp8cx_init_quantizer(cpi);
2807 static void update_alt_ref_frame_and_stats(VP8_COMP *cpi)
2809 VP8_COMMON *cm = &cpi->common;
2811 // Update the golden frame buffer
2812 vp8_yv12_copy_frame_ptr(cm->frame_to_show, &cm->yv12_fb[cm->alt_fb_idx]);
2814 // Select an interval before next GF or altref
2815 if (!cpi->auto_gold)
2816 cpi->frames_till_gf_update_due = cpi->goldfreq;
2818 if ((cpi->pass != 2) && cpi->frames_till_gf_update_due)
2820 cpi->current_gf_interval = cpi->frames_till_gf_update_due;
2822 // Set the bits per frame that we should try and recover in subsequent inter frames
2823 // to account for the extra GF spend... note that his does not apply for GF updates
2824 // that occur coincident with a key frame as the extra cost of key frames is dealt
2827 cpi->gf_overspend_bits += cpi->projected_frame_size;
2828 cpi->non_gf_bitrate_adjustment = cpi->gf_overspend_bits / cpi->frames_till_gf_update_due;
2831 // Update data structure that monitors level of reference to last GF
2832 vpx_memset(cpi->gf_active_flags, 1, (cm->mb_rows * cm->mb_cols));
2833 cpi->gf_active_count = cm->mb_rows * cm->mb_cols;
2835 // this frame refreshes means next frames don't unless specified by user
2836 cpi->common.frames_since_golden = 0;
2838 // Clear the alternate reference update pending flag.
2839 cpi->source_alt_ref_pending = FALSE;
2841 // Set the alternate refernce frame active flag
2842 cpi->source_alt_ref_active = TRUE;
2846 static void update_golden_frame_and_stats(VP8_COMP *cpi)
2848 VP8_COMMON *cm = &cpi->common;
2850 // Update the Golden frame reconstruction buffer if signalled and the GF usage counts.
2851 if (cm->refresh_golden_frame)
2853 if (cm->frame_type != KEY_FRAME)
2855 // Update the golden frame buffer
2856 vp8_yv12_copy_frame_ptr(cm->frame_to_show, &cm->yv12_fb[cm->gld_fb_idx]);
2859 // Select an interval before next GF
2860 if (!cpi->auto_gold)
2861 cpi->frames_till_gf_update_due = cpi->goldfreq;
2863 if ((cpi->pass != 2) && (cpi->frames_till_gf_update_due > 0))
2865 cpi->current_gf_interval = cpi->frames_till_gf_update_due;
2867 // Set the bits per frame that we should try and recover in subsequent inter frames
2868 // to account for the extra GF spend... note that his does not apply for GF updates
2869 // that occur coincident with a key frame as the extra cost of key frames is dealt
2871 if ((cm->frame_type != KEY_FRAME) && !cpi->source_alt_ref_active)
2873 // Calcluate GF bits to be recovered
2874 // Projected size - av frame bits available for inter frames for clip as a whole
2875 cpi->gf_overspend_bits += (cpi->projected_frame_size - cpi->inter_frame_target);
2878 cpi->non_gf_bitrate_adjustment = cpi->gf_overspend_bits / cpi->frames_till_gf_update_due;
2882 // Update data structure that monitors level of reference to last GF
2883 vpx_memset(cpi->gf_active_flags, 1, (cm->mb_rows * cm->mb_cols));
2884 cpi->gf_active_count = cm->mb_rows * cm->mb_cols;
2886 // this frame refreshes means next frames don't unless specified by user
2887 cm->refresh_golden_frame = 0;
2888 cpi->common.frames_since_golden = 0;
2890 //if ( cm->frame_type == KEY_FRAME )
2892 cpi->recent_ref_frame_usage[INTRA_FRAME] = 1;
2893 cpi->recent_ref_frame_usage[LAST_FRAME] = 1;
2894 cpi->recent_ref_frame_usage[GOLDEN_FRAME] = 1;
2895 cpi->recent_ref_frame_usage[ALTREF_FRAME] = 1;
2899 // // Carry a potrtion of count over to begining of next gf sequence
2900 // cpi->recent_ref_frame_usage[INTRA_FRAME] >>= 5;
2901 // cpi->recent_ref_frame_usage[LAST_FRAME] >>= 5;
2902 // cpi->recent_ref_frame_usage[GOLDEN_FRAME] >>= 5;
2903 // cpi->recent_ref_frame_usage[ALTREF_FRAME] >>= 5;
2906 // ******** Fixed Q test code only ************
2907 // If we are going to use the ALT reference for the next group of frames set a flag to say so.
2908 if (cpi->oxcf.fixed_q >= 0 &&
2909 cpi->oxcf.play_alternate && !cpi->common.refresh_alt_ref_frame)
2911 cpi->source_alt_ref_pending = TRUE;
2912 cpi->frames_till_gf_update_due = cpi->baseline_gf_interval;
2915 if (!cpi->source_alt_ref_pending)
2916 cpi->source_alt_ref_active = FALSE;
2918 // Decrement count down till next gf
2919 if (cpi->frames_till_gf_update_due > 0)
2920 cpi->frames_till_gf_update_due--;
2923 else if (!cpi->common.refresh_alt_ref_frame)
2925 // Decrement count down till next gf
2926 if (cpi->frames_till_gf_update_due > 0)
2927 cpi->frames_till_gf_update_due--;
2929 if (cpi->common.frames_till_alt_ref_frame)
2930 cpi->common.frames_till_alt_ref_frame --;
2932 cpi->common.frames_since_golden ++;
2934 if (cpi->common.frames_since_golden > 1)
2936 cpi->recent_ref_frame_usage[INTRA_FRAME] += cpi->count_mb_ref_frame_usage[INTRA_FRAME];
2937 cpi->recent_ref_frame_usage[LAST_FRAME] += cpi->count_mb_ref_frame_usage[LAST_FRAME];
2938 cpi->recent_ref_frame_usage[GOLDEN_FRAME] += cpi->count_mb_ref_frame_usage[GOLDEN_FRAME];
2939 cpi->recent_ref_frame_usage[ALTREF_FRAME] += cpi->count_mb_ref_frame_usage[ALTREF_FRAME];
2944 // This function updates the reference frame probability estimates that
2945 // will be used during mode selection
2946 static void update_rd_ref_frame_probs(VP8_COMP *cpi)
2948 VP8_COMMON *cm = &cpi->common;
2951 const int *const rfct = cpi->recent_ref_frame_usage;
2952 const int rf_intra = rfct[INTRA_FRAME];
2953 const int rf_inter = rfct[LAST_FRAME] + rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME];
2955 if (cm->frame_type == KEY_FRAME)
2957 cpi->prob_intra_coded = 255;
2958 cpi->prob_last_coded = 128;
2959 cpi->prob_gf_coded = 128;
2961 else if (!(rf_intra + rf_inter))
2963 // This is a trap in case this function is called with cpi->recent_ref_frame_usage[] blank.
2964 cpi->prob_intra_coded = 63;
2965 cpi->prob_last_coded = 128;
2966 cpi->prob_gf_coded = 128;
2970 cpi->prob_intra_coded = (rf_intra * 255) / (rf_intra + rf_inter);
2972 if (cpi->prob_intra_coded < 1)
2973 cpi->prob_intra_coded = 1;
2975 if ((cm->frames_since_golden > 0) || cpi->source_alt_ref_active)
2977 cpi->prob_last_coded = rf_inter ? (rfct[LAST_FRAME] * 255) / rf_inter : 128;
2979 if (cpi->prob_last_coded < 1)
2980 cpi->prob_last_coded = 1;
2982 cpi->prob_gf_coded = (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME])
2983 ? (rfct[GOLDEN_FRAME] * 255) / (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME]) : 128;
2985 if (cpi->prob_gf_coded < 1)
2986 cpi->prob_gf_coded = 1;
2991 const int *const rfct = cpi->count_mb_ref_frame_usage;
2992 const int rf_intra = rfct[INTRA_FRAME];
2993 const int rf_inter = rfct[LAST_FRAME] + rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME];
2995 if (cm->frame_type == KEY_FRAME)
2997 cpi->prob_intra_coded = 255;
2998 cpi->prob_last_coded = 128;
2999 cpi->prob_gf_coded = 128;
3001 else if (!(rf_intra + rf_inter))
3003 // This is a trap in case this function is called with cpi->recent_ref_frame_usage[] blank.
3004 cpi->prob_intra_coded = 63;
3005 cpi->prob_last_coded = 128;
3006 cpi->prob_gf_coded = 128;
3010 cpi->prob_intra_coded = (rf_intra * 255) / (rf_intra + rf_inter);
3012 if (cpi->prob_intra_coded < 1)
3013 cpi->prob_intra_coded = 1;
3015 cpi->prob_last_coded = rf_inter ? (rfct[LAST_FRAME] * 255) / rf_inter : 128;
3017 if (cpi->prob_last_coded < 1)
3018 cpi->prob_last_coded = 1;
3020 cpi->prob_gf_coded = (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME])
3021 ? (rfct[GOLDEN_FRAME] * 255) / (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME]) : 128;
3023 if (cpi->prob_gf_coded < 1)
3024 cpi->prob_gf_coded = 1;
3027 // update reference frame costs since we can do better than what we got last frame.
3029 if (cpi->common.refresh_alt_ref_frame)
3031 cpi->prob_intra_coded += 40;
3032 cpi->prob_last_coded = 200;
3033 cpi->prob_gf_coded = 1;
3035 else if (cpi->common.frames_since_golden == 0)
3037 cpi->prob_last_coded = 214;
3038 cpi->prob_gf_coded = 1;
3040 else if (cpi->common.frames_since_golden == 1)
3042 cpi->prob_last_coded = 192;
3043 cpi->prob_gf_coded = 220;
3045 else if (cpi->source_alt_ref_active)
3047 //int dist = cpi->common.frames_till_alt_ref_frame + cpi->common.frames_since_golden;
3048 cpi->prob_gf_coded -= 20;
3050 if (cpi->prob_gf_coded < 10)
3051 cpi->prob_gf_coded = 10;
3058 // 1 = key, 0 = inter
3059 static int decide_key_frame(VP8_COMP *cpi)
3061 VP8_COMMON *cm = &cpi->common;
3063 int code_key_frame = FALSE;
3067 if (cpi->Speed > 11)
3070 // Clear down mmx registers
3071 vp8_clear_system_state(); //__asm emms;
3073 if ((cpi->compressor_speed == 2) && (cpi->Speed >= 5) && (cpi->sf.RD == 0))
3075 double change = 1.0 * abs((int)(cpi->intra_error - cpi->last_intra_error)) / (1 + cpi->last_intra_error);
3076 double change2 = 1.0 * abs((int)(cpi->prediction_error - cpi->last_prediction_error)) / (1 + cpi->last_prediction_error);
3077 double minerror = cm->MBs * 256;
3081 if (10 * cpi->intra_error / (1 + cpi->prediction_error) < 15
3082 && cpi->prediction_error > minerror
3083 && (change > .25 || change2 > .25))
3085 FILE *f = fopen("intra_inter.stt", "a");
3087 if (cpi->prediction_error <= 0)
3088 cpi->prediction_error = 1;
3090 fprintf(f, "%d %d %d %d %14.4f\n",
3091 cm->current_video_frame,
3092 (int) cpi->prediction_error,
3093 (int) cpi->intra_error,
3094 (int)((10 * cpi->intra_error) / cpi->prediction_error),
3102 cpi->last_intra_error = cpi->intra_error;
3103 cpi->last_prediction_error = cpi->prediction_error;
3105 if (10 * cpi->intra_error / (1 + cpi->prediction_error) < 15
3106 && cpi->prediction_error > minerror
3107 && (change > .25 || change2 > .25))
3109 /*(change > 1.4 || change < .75)&& cpi->this_frame_percent_intra > cpi->last_frame_percent_intra + 3*/
3117 // If the following are true we might as well code a key frame
3118 if (((cpi->this_frame_percent_intra == 100) &&
3119 (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra + 2))) ||
3120 ((cpi->this_frame_percent_intra > 95) &&
3121 (cpi->this_frame_percent_intra >= (cpi->last_frame_percent_intra + 5))))
3123 code_key_frame = TRUE;
3125 // in addition if the following are true and this is not a golden frame then code a key frame
3126 // Note that on golden frames there often seems to be a pop in intra useage anyway hence this
3127 // restriction is designed to prevent spurious key frames. The Intra pop needs to be investigated.
3128 else if (((cpi->this_frame_percent_intra > 60) &&
3129 (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra * 2))) ||
3130 ((cpi->this_frame_percent_intra > 75) &&
3131 (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra * 3 / 2))) ||
3132 ((cpi->this_frame_percent_intra > 90) &&
3133 (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra + 10))))
3135 if (!cm->refresh_golden_frame)
3136 code_key_frame = TRUE;
3139 return code_key_frame;
3143 #if !(CONFIG_REALTIME_ONLY)
3144 static void Pass1Encode(VP8_COMP *cpi, unsigned long *size, unsigned char *dest, unsigned int *frame_flags)
3149 set_quantizer(cpi, 26);
3151 scale_and_extend_source(cpi->un_scaled_source, cpi);
3152 vp8_first_pass(cpi);
3157 void write_cx_frame_to_file(YV12_BUFFER_CONFIG *frame, int this_frame)
3165 sprintf(filename, "cx\\y%04d.raw", this_frame);
3166 yframe = fopen(filename, "wb");
3168 for (i = 0; i < frame->y_height; i++)
3169 fwrite(frame->y_buffer + i * frame->y_stride, frame->y_width, 1, yframe);
3172 sprintf(filename, "cx\\u%04d.raw", this_frame);
3173 yframe = fopen(filename, "wb");
3175 for (i = 0; i < frame->uv_height; i++)
3176 fwrite(frame->u_buffer + i * frame->uv_stride, frame->uv_width, 1, yframe);
3179 sprintf(filename, "cx\\v%04d.raw", this_frame);
3180 yframe = fopen(filename, "wb");
3182 for (i = 0; i < frame->uv_height; i++)
3183 fwrite(frame->v_buffer + i * frame->uv_stride, frame->uv_width, 1, yframe);
3188 // return of 0 means drop frame
3190 // Function to test for conditions that indeicate we should loop
3191 // back and recode a frame.
3192 static BOOL recode_loop_test( VP8_COMP *cpi,
3193 int high_limit, int low_limit,
3194 int q, int maxq, int minq )
3196 BOOL force_recode = FALSE;
3197 VP8_COMMON *cm = &cpi->common;
3199 // Is frame recode allowed at all
3200 // Yes if either recode mode 1 is selected or mode two is selcted
3201 // and the frame is a key frame. golden frame or alt_ref_frame
3202 if ( (cpi->sf.recode_loop == 1) ||
3203 ( (cpi->sf.recode_loop == 2) &&
3204 ( (cm->frame_type == KEY_FRAME) ||
3205 cm->refresh_golden_frame ||
3206 cm->refresh_alt_ref_frame ) ) )
3208 // General over and under shoot tests
3209 if ( ((cpi->projected_frame_size > high_limit) && (q < maxq)) ||
3210 ((cpi->projected_frame_size < low_limit) && (q > minq)) )
3212 force_recode = TRUE;
3214 // Special Constrained quality tests
3215 else if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY)
3217 // Undershoot and below auto cq level
3218 if ( (q > cpi->cq_target_quality) &&
3219 (cpi->projected_frame_size <
3220 ((cpi->this_frame_target * 7) >> 3)))
3222 force_recode = TRUE;
3224 // Severe undershoot and between auto and user cq level
3225 else if ( (q > cpi->oxcf.cq_level) &&
3226 (cpi->projected_frame_size < cpi->min_frame_bandwidth) &&
3227 (cpi->active_best_quality > cpi->oxcf.cq_level))
3229 force_recode = TRUE;
3230 cpi->active_best_quality = cpi->oxcf.cq_level;
3235 return force_recode;
3238 void loopfilter_frame(VP8_COMP *cpi, VP8_COMMON *cm)
3242 cm->filter_level = 0;
3246 struct vpx_usec_timer timer;
3248 vp8_clear_system_state();
3250 vpx_usec_timer_start(&timer);
3251 if (cpi->sf.auto_filter == 0)
3252 vp8cx_pick_filter_level_fast(cpi->Source, cpi);
3255 vp8cx_pick_filter_level(cpi->Source, cpi);
3257 vpx_usec_timer_mark(&timer);
3258 cpi->time_pick_lpf += vpx_usec_timer_elapsed(&timer);
3261 #if CONFIG_MULTITHREAD
3262 sem_post(&cpi->h_event_end_lpf); /* signal that we have set filter_level */
3265 if (cm->filter_level > 0)
3267 vp8cx_set_alt_lf_level(cpi, cm->filter_level);
3268 vp8_loop_filter_frame(cm, &cpi->mb.e_mbd, cm->filter_level);
3269 cm->last_filter_type = cm->filter_type;
3270 cm->last_sharpness_level = cm->sharpness_level;
3273 vp8_yv12_extend_frame_borders_ptr(cm->frame_to_show);
3276 YV12_BUFFER_CONFIG *lst_yv12 = &cm->yv12_fb[cm->lst_fb_idx];
3277 YV12_BUFFER_CONFIG *new_yv12 = &cm->yv12_fb[cm->new_fb_idx];
3278 YV12_BUFFER_CONFIG *gld_yv12 = &cm->yv12_fb[cm->gld_fb_idx];
3279 YV12_BUFFER_CONFIG *alt_yv12 = &cm->yv12_fb[cm->alt_fb_idx];
3280 // At this point the new frame has been encoded.
3281 // If any buffer copy / swapping is signaled it should be done here.
3282 if (cm->frame_type == KEY_FRAME)
3284 vp8_yv12_copy_frame_ptr(cm->frame_to_show, gld_yv12);
3285 vp8_yv12_copy_frame_ptr(cm->frame_to_show, alt_yv12);
3287 else // For non key frames
3289 // Code to copy between reference buffers
3290 if (cm->copy_buffer_to_arf)
3292 if (cm->copy_buffer_to_arf == 1)
3294 if (cm->refresh_last_frame)
3295 // We copy new_frame here because last and new buffers will already have been swapped if cm->refresh_last_frame is set.
3296 vp8_yv12_copy_frame_ptr(new_yv12, alt_yv12);
3298 vp8_yv12_copy_frame_ptr(lst_yv12, alt_yv12);
3300 else if (cm->copy_buffer_to_arf == 2)
3301 vp8_yv12_copy_frame_ptr(gld_yv12, alt_yv12);
3304 if (cm->copy_buffer_to_gf)
3306 if (cm->copy_buffer_to_gf == 1)
3308 if (cm->refresh_last_frame)
3309 // We copy new_frame here because last and new buffers will already have been swapped if cm->refresh_last_frame is set.
3310 vp8_yv12_copy_frame_ptr(new_yv12, gld_yv12);
3312 vp8_yv12_copy_frame_ptr(lst_yv12, gld_yv12);
3314 else if (cm->copy_buffer_to_gf == 2)
3315 vp8_yv12_copy_frame_ptr(alt_yv12, gld_yv12);
3321 static void encode_frame_to_data_rate
3324 unsigned long *size,
3325 unsigned char *dest,
3326 unsigned int *frame_flags
3330 int frame_over_shoot_limit;
3331 int frame_under_shoot_limit;
3341 int zbin_oq_low = 0;
3344 VP8_COMMON *cm = &cpi->common;
3345 int active_worst_qchanged = FALSE;
3347 int overshoot_seen = FALSE;
3348 int undershoot_seen = FALSE;
3349 int drop_mark = cpi->oxcf.drop_frames_water_mark * cpi->oxcf.optimal_buffer_level / 100;
3350 int drop_mark75 = drop_mark * 2 / 3;
3351 int drop_mark50 = drop_mark / 4;
3352 int drop_mark25 = drop_mark / 8;
3355 // Clear down mmx registers to allow floating point in what follows
3356 vp8_clear_system_state();
3358 // Test code for segmentation of gf/arf (0,0)
3359 //segmentation_test_function((VP8_PTR) cpi);
3361 #if CONFIG_REALTIME_ONLY
3362 if(cpi->oxcf.auto_key && cm->frame_type != KEY_FRAME)
3364 if(cpi->force_next_frame_intra)
3366 cm->frame_type = KEY_FRAME; /* delayed intra frame */
3369 cpi->force_next_frame_intra = 0;
3372 // For an alt ref frame in 2 pass we skip the call to the second pass function that sets the target bandwidth
3373 #if !(CONFIG_REALTIME_ONLY)
3377 if (cpi->common.refresh_alt_ref_frame)
3379 cpi->per_frame_bandwidth = cpi->gf_bits; // Per frame bit target for the alt ref frame
3380 cpi->target_bandwidth = cpi->gf_bits * cpi->output_frame_rate; // per second target bitrate
3385 cpi->per_frame_bandwidth = (int)(cpi->target_bandwidth / cpi->output_frame_rate);
3387 // Default turn off buffer to buffer copying
3388 cm->copy_buffer_to_gf = 0;
3389 cm->copy_buffer_to_arf = 0;
3391 // Clear zbin over-quant value and mode boost values.
3392 cpi->zbin_over_quant = 0;
3393 cpi->zbin_mode_boost = 0;
3395 // Enable or disable mode based tweaking of the zbin
3396 // For 2 Pass Only used where GF/ARF prediction quality
3397 // is above a threshold
3398 cpi->zbin_mode_boost = 0;
3399 cpi->zbin_mode_boost_enabled = TRUE;
3402 if ( cpi->gfu_boost <= 400 )
3404 cpi->zbin_mode_boost_enabled = FALSE;
3408 // Current default encoder behaviour for the altref sign bias
3409 if (cpi->source_alt_ref_active)
3410 cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = 1;
3412 cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = 0;
3414 // Check to see if a key frame is signalled
3415 // For two pass with auto key frame enabled cm->frame_type may already be set, but not for one pass.
3416 if ((cm->current_video_frame == 0) ||
3417 (cm->frame_flags & FRAMEFLAGS_KEY) ||
3418 (cpi->oxcf.auto_key && (cpi->frames_since_key % cpi->key_frame_frequency == 0)))
3420 // Key frame from VFW/auto-keyframe/first frame
3421 cm->frame_type = KEY_FRAME;
3424 // Set default state for segment and mode based loop filter update flags
3425 cpi->mb.e_mbd.update_mb_segmentation_map = 0;
3426 cpi->mb.e_mbd.update_mb_segmentation_data = 0;
3427 cpi->mb.e_mbd.mode_ref_lf_delta_update = 0;
3429 // Set various flags etc to special state if it is a key frame
3430 if (cm->frame_type == KEY_FRAME)
3434 // Reset the loop filter deltas and segmentation map
3435 setup_features(cpi);
3437 // If segmentation is enabled force a map update for key frames
3438 if (cpi->mb.e_mbd.segmentation_enabled)
3440 cpi->mb.e_mbd.update_mb_segmentation_map = 1;
3441 cpi->mb.e_mbd.update_mb_segmentation_data = 1;
3444 // The alternate reference frame cannot be active for a key frame
3445 cpi->source_alt_ref_active = FALSE;
3447 // Reset the RD threshold multipliers to default of * 1 (128)
3448 for (i = 0; i < MAX_MODES; i++)
3450 cpi->rd_thresh_mult[i] = 128;
3454 // Test code for segmentation
3455 //if ( (cm->frame_type == KEY_FRAME) || ((cm->current_video_frame % 2) == 0))
3456 //if ( (cm->current_video_frame % 2) == 0 )
3457 // enable_segmentation((VP8_PTR)cpi);
3459 // disable_segmentation((VP8_PTR)cpi);
3462 // Experimental code for lagged compress and one pass
3463 // Initialise one_pass GF frames stats
3464 // Update stats used for GF selection
3465 //if ( cpi->pass == 0 )
3467 cpi->one_pass_frame_index = cm->current_video_frame % MAX_LAG_BUFFERS;
3469 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frames_so_far = 0;
3470 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_intra_error = 0.0;
3471 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_coded_error = 0.0;
3472 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_pcnt_inter = 0.0;
3473 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_pcnt_motion = 0.0;
3474 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvr = 0.0;
3475 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvr_abs = 0.0;
3476 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvc = 0.0;
3477 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvc_abs = 0.0;
3481 update_rd_ref_frame_probs(cpi);
3483 if (cpi->drop_frames_allowed)
3485 // The reset to decimation 0 is only done here for one pass.
3486 // Once it is set two pass leaves decimation on till the next kf.
3487 if ((cpi->buffer_level > drop_mark) && (cpi->decimation_factor > 0))
3488 cpi->decimation_factor --;
3490 if (cpi->buffer_level > drop_mark75 && cpi->decimation_factor > 0)
3491 cpi->decimation_factor = 1;
3493 else if (cpi->buffer_level < drop_mark25 && (cpi->decimation_factor == 2 || cpi->decimation_factor == 3))
3495 cpi->decimation_factor = 3;
3497 else if (cpi->buffer_level < drop_mark50 && (cpi->decimation_factor == 1 || cpi->decimation_factor == 2))
3499 cpi->decimation_factor = 2;
3501 else if (cpi->buffer_level < drop_mark75 && (cpi->decimation_factor == 0 || cpi->decimation_factor == 1))
3503 cpi->decimation_factor = 1;
3506 //vpx_log("Encoder: Decimation Factor: %d \n",cpi->decimation_factor);
3509 // The following decimates the frame rate according to a regular pattern (i.e. to 1/2 or 2/3 frame rate)
3510 // This can be used to help prevent buffer under-run in CBR mode. Alternatively it might be desirable in
3511 // some situations to drop frame rate but throw more bits at each frame.
3513 // Note that dropping a key frame can be problematic if spatial resampling is also active
3514 if (cpi->decimation_factor > 0)
3516 switch (cpi->decimation_factor)
3519 cpi->per_frame_bandwidth = cpi->per_frame_bandwidth * 3 / 2;
3522 cpi->per_frame_bandwidth = cpi->per_frame_bandwidth * 5 / 4;
3525 cpi->per_frame_bandwidth = cpi->per_frame_bandwidth * 5 / 4;
3529 // Note that we should not throw out a key frame (especially when spatial resampling is enabled).
3530 if ((cm->frame_type == KEY_FRAME)) // && cpi->oxcf.allow_spatial_resampling )
3532 cpi->decimation_count = cpi->decimation_factor;
3534 else if (cpi->decimation_count > 0)
3536 cpi->decimation_count --;
3537 cpi->bits_off_target += cpi->av_per_frame_bandwidth;
3538 cm->current_video_frame++;
3539 cpi->frames_since_key++;
3545 cpi->buffer_level = cpi->bits_off_target;
3550 cpi->decimation_count = cpi->decimation_factor;
3553 // Decide how big to make the frame
3554 if (!pick_frame_size(cpi))
3556 cm->current_video_frame++;
3557 cpi->frames_since_key++;
3561 // Reduce active_worst_allowed_q for CBR if our buffer is getting too full.
3562 // This has a knock on effect on active best quality as well.
3563 // For CBR if the buffer reaches its maximum level then we can no longer
3564 // save up bits for later frames so we might as well use them up
3565 // on the current frame.
3566 if ((cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) &&
3567 (cpi->buffer_level >= cpi->oxcf.optimal_buffer_level) && cpi->buffered_mode)
3569 int Adjustment = cpi->active_worst_quality / 4; // Max adjustment is 1/4
3574 int tmp_lvl = cpi->buffer_level;
3576 if (cpi->buffer_level < cpi->oxcf.maximum_buffer_size)
3578 buff_lvl_step = (cpi->oxcf.maximum_buffer_size - cpi->oxcf.optimal_buffer_level) / Adjustment;
3582 Adjustment = (cpi->buffer_level - cpi->oxcf.optimal_buffer_level) / buff_lvl_step;
3583 cpi->active_worst_quality -= Adjustment;
3588 cpi->active_worst_quality -= Adjustment;
3593 // Set an active best quality and if necessary active worst quality
3594 // There is some odd behaviour for one pass here that needs attention.
3595 if ( (cpi->pass == 2) || (cpi->ni_frames > 150))
3597 vp8_clear_system_state();
3599 Q = cpi->active_worst_quality;
3601 if ( cm->frame_type == KEY_FRAME )
3603 if ( cpi->pass == 2 )
3605 if (cpi->gfu_boost > 600)
3606 cpi->active_best_quality = kf_low_motion_minq[Q];
3608 cpi->active_best_quality = kf_high_motion_minq[Q];
3610 // Special case for key frames forced because we have reached
3611 // the maximum key frame interval. Here force the Q to a range
3612 // based on the ambient Q to reduce the risk of popping
3613 if ( cpi->this_key_frame_forced )
3615 if ( cpi->active_best_quality > cpi->avg_frame_qindex * 7/8)
3616 cpi->active_best_quality = cpi->avg_frame_qindex * 7/8;
3617 else if ( cpi->active_best_quality < cpi->avg_frame_qindex >> 2 )
3618 cpi->active_best_quality = cpi->avg_frame_qindex >> 2;
3621 // One pass more conservative
3623 cpi->active_best_quality = kf_high_motion_minq[Q];
3626 else if (cm->refresh_golden_frame || cpi->common.refresh_alt_ref_frame)
3628 // Use the lower of cpi->active_worst_quality and recent
3629 // average Q as basis for GF/ARF Q limit unless last frame was
3631 if ( (cpi->frames_since_key > 1) &&
3632 (cpi->avg_frame_qindex < cpi->active_worst_quality) )
3634 Q = cpi->avg_frame_qindex;
3636 if ( (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) &&
3637 (Q < cpi->oxcf.cq_level) )
3639 Q = cpi->oxcf.cq_level;
3643 if ( cpi->pass == 2 )
3645 if ( cpi->gfu_boost > 1000 )
3646 cpi->active_best_quality = gf_low_motion_minq[Q];
3647 else if ( cpi->gfu_boost < 400 )
3648 cpi->active_best_quality = gf_high_motion_minq[Q];
3650 cpi->active_best_quality = gf_mid_motion_minq[Q];
3652 // One pass more conservative
3654 cpi->active_best_quality = gf_high_motion_minq[Q];
3658 cpi->active_best_quality = inter_minq[Q];
3660 // For the constant/constrained quality mode we dont want
3661 // the quality to rise above the cq level.
3662 if ((cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) &&
3663 (cpi->active_best_quality < cpi->cq_target_quality) )
3665 // If we are strongly undershooting the target rate in the last
3666 // frames then use the user passed in cq value not the auto
3668 if ( cpi->rolling_actual_bits < cpi->min_frame_bandwidth )
3669 cpi->active_best_quality = cpi->oxcf.cq_level;
3671 cpi->active_best_quality = cpi->cq_target_quality;
3675 // If CBR and the buffer is as full then it is reasonable to allow
3676 // higher quality on the frames to prevent bits just going to waste.
3677 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
3679 // Note that the use of >= here elliminates the risk of a devide
3680 // by 0 error in the else if clause
3681 if (cpi->buffer_level >= cpi->oxcf.maximum_buffer_size)
3682 cpi->active_best_quality = cpi->best_quality;
3684 else if (cpi->buffer_level > cpi->oxcf.optimal_buffer_level)
3686 int Fraction = ((cpi->buffer_level - cpi->oxcf.optimal_buffer_level) * 128) / (cpi->oxcf.maximum_buffer_size - cpi->oxcf.optimal_buffer_level);
3687 int min_qadjustment = ((cpi->active_best_quality - cpi->best_quality) * Fraction) / 128;
3689 cpi->active_best_quality -= min_qadjustment;
3693 // Make sure constrained quality mode limits are adhered to for the first
3694 // few frames of one pass encodes
3695 else if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY)
3697 if ( (cm->frame_type == KEY_FRAME) ||
3698 cm->refresh_golden_frame || cpi->common.refresh_alt_ref_frame )
3700 cpi->active_best_quality = cpi->best_quality;
3702 else if (cpi->active_best_quality < cpi->cq_target_quality)
3704 cpi->active_best_quality = cpi->cq_target_quality;
3708 // Clip the active best and worst quality values to limits
3709 if (cpi->active_worst_quality > cpi->worst_quality)
3710 cpi->active_worst_quality = cpi->worst_quality;
3712 if (cpi->active_best_quality < cpi->best_quality)
3713 cpi->active_best_quality = cpi->best_quality;
3714 else if (cpi->active_best_quality > cpi->active_worst_quality)
3715 cpi->active_best_quality = cpi->active_worst_quality;
3717 // Determine initial Q to try
3718 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
3719 last_zbin_oq = cpi->zbin_over_quant;
3721 // Set highest allowed value for Zbin over quant
3722 if (cm->frame_type == KEY_FRAME)
3723 zbin_oq_high = 0; //ZBIN_OQ_MAX/16
3724 else if (cm->refresh_alt_ref_frame || (cm->refresh_golden_frame && !cpi->source_alt_ref_active))
3727 zbin_oq_high = ZBIN_OQ_MAX;
3729 // Setup background Q adjustment for error resilliant mode
3730 if (cpi->cyclic_refresh_mode_enabled)
3731 cyclic_background_refresh(cpi, Q, 0);
3733 vp8_compute_frame_size_bounds(cpi, &frame_under_shoot_limit, &frame_over_shoot_limit);
3735 // Limit Q range for the adaptive loop.
3736 bottom_index = cpi->active_best_quality;
3737 top_index = cpi->active_worst_quality;
3738 q_low = cpi->active_best_quality;
3739 q_high = cpi->active_worst_quality;
3741 vp8_save_coding_context(cpi);
3746 scale_and_extend_source(cpi->un_scaled_source, cpi);
3747 #if !(CONFIG_REALTIME_ONLY) && CONFIG_POSTPROC
3749 if (cpi->oxcf.noise_sensitivity > 0)
3754 switch (cpi->oxcf.noise_sensitivity)
3777 if (cm->frame_type == KEY_FRAME)
3779 vp8_de_noise(cpi->Source, cpi->Source, l , 1, 0, RTCD(postproc));
3783 vp8_de_noise(cpi->Source, cpi->Source, l , 1, 0, RTCD(postproc));
3785 src = cpi->Source->y_buffer;
3787 if (cpi->Source->y_stride < 0)
3789 src += cpi->Source->y_stride * (cpi->Source->y_height - 1);
3796 #ifdef OUTPUT_YUV_SRC
3797 vp8_write_yuv_frame(cpi->Source);
3802 vp8_clear_system_state(); //__asm emms;
3805 if(cpi->is_src_frame_alt_ref)
3809 set_quantizer(cpi, Q);
3812 // setup skip prob for costing in mode/mv decision
3813 if (cpi->common.mb_no_coeff_skip)
3815 cpi->prob_skip_false = cpi->base_skip_false_prob[Q];
3817 if (cm->frame_type != KEY_FRAME)
3819 if (cpi->common.refresh_alt_ref_frame)
3821 if (cpi->last_skip_false_probs[2] != 0)
3822 cpi->prob_skip_false = cpi->last_skip_false_probs[2];
3825 if(cpi->last_skip_false_probs[2]!=0 && abs(Q- cpi->last_skip_probs_q[2])<=16 )
3826 cpi->prob_skip_false = cpi->last_skip_false_probs[2];
3827 else if (cpi->last_skip_false_probs[2]!=0)
3828 cpi->prob_skip_false = (cpi->last_skip_false_probs[2] + cpi->prob_skip_false ) / 2;
3831 else if (cpi->common.refresh_golden_frame)
3833 if (cpi->last_skip_false_probs[1] != 0)
3834 cpi->prob_skip_false = cpi->last_skip_false_probs[1];
3837 if(cpi->last_skip_false_probs[1]!=0 && abs(Q- cpi->last_skip_probs_q[1])<=16 )
3838 cpi->prob_skip_false = cpi->last_skip_false_probs[1];
3839 else if (cpi->last_skip_false_probs[1]!=0)
3840 cpi->prob_skip_false = (cpi->last_skip_false_probs[1] + cpi->prob_skip_false ) / 2;
3845 if (cpi->last_skip_false_probs[0] != 0)
3846 cpi->prob_skip_false = cpi->last_skip_false_probs[0];
3849 if(cpi->last_skip_false_probs[0]!=0 && abs(Q- cpi->last_skip_probs_q[0])<=16 )
3850 cpi->prob_skip_false = cpi->last_skip_false_probs[0];
3851 else if(cpi->last_skip_false_probs[0]!=0)
3852 cpi->prob_skip_false = (cpi->last_skip_false_probs[0] + cpi->prob_skip_false ) / 2;
3856 //as this is for cost estimate, let's make sure it does not go extreme eitehr way
3857 if (cpi->prob_skip_false < 5)
3858 cpi->prob_skip_false = 5;
3860 if (cpi->prob_skip_false > 250)
3861 cpi->prob_skip_false = 250;
3863 if (cpi->is_src_frame_alt_ref)
3864 cpi->prob_skip_false = 1;
3873 FILE *f = fopen("skip.stt", "a");
3874 fprintf(f, "%d, %d, %4d ", cpi->common.refresh_golden_frame, cpi->common.refresh_alt_ref_frame, cpi->prob_skip_false);
3882 if (cm->frame_type == KEY_FRAME)
3883 vp8_setup_key_frame(cpi);
3885 // transform / motion compensation build reconstruction frame
3886 vp8_encode_frame(cpi);
3888 cpi->projected_frame_size -= vp8_estimate_entropy_savings(cpi);
3889 cpi->projected_frame_size = (cpi->projected_frame_size > 0) ? cpi->projected_frame_size : 0;
3891 vp8_clear_system_state(); //__asm emms;
3893 // Test to see if the stats generated for this frame indicate that we should have coded a key frame
3894 // (assuming that we didn't)!
3895 if (cpi->pass != 2 && cpi->oxcf.auto_key && cm->frame_type != KEY_FRAME)
3898 #if CONFIG_REALTIME_ONLY
3900 /* we don't do re-encoding in realtime mode
3901 * if key frame is decided than we force it on next frame */
3902 cpi->force_next_frame_intra = decide_key_frame(cpi);
3905 if (decide_key_frame(cpi))
3907 vp8_calc_auto_iframe_target_size(cpi);
3909 // Reset all our sizing numbers and recode
3910 cm->frame_type = KEY_FRAME;
3912 // Clear the Alt reference frame active flag when we have a key frame
3913 cpi->source_alt_ref_active = FALSE;
3915 // Reset the loop filter deltas and segmentation map
3916 setup_features(cpi);
3918 // If segmentation is enabled force a map update for key frames
3919 if (cpi->mb.e_mbd.segmentation_enabled)
3921 cpi->mb.e_mbd.update_mb_segmentation_map = 1;
3922 cpi->mb.e_mbd.update_mb_segmentation_data = 1;
3925 vp8_restore_coding_context(cpi);
3927 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
3929 vp8_compute_frame_size_bounds(cpi, &frame_under_shoot_limit, &frame_over_shoot_limit);
3931 // Limit Q range for the adaptive loop.
3932 bottom_index = cpi->active_best_quality;
3933 top_index = cpi->active_worst_quality;
3934 q_low = cpi->active_best_quality;
3935 q_high = cpi->active_worst_quality;
3940 resize_key_frame(cpi);
3946 vp8_clear_system_state();
3948 if (frame_over_shoot_limit == 0)
3949 frame_over_shoot_limit = 1;
3951 // Are we are overshooting and up against the limit of active max Q.
3952 if (((cpi->pass != 2) || (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)) &&
3953 (Q == cpi->active_worst_quality) &&
3954 (cpi->active_worst_quality < cpi->worst_quality) &&
3955 (cpi->projected_frame_size > frame_over_shoot_limit))
3957 int over_size_percent = ((cpi->projected_frame_size - frame_over_shoot_limit) * 100) / frame_over_shoot_limit;
3959 // If so is there any scope for relaxing it
3960 while ((cpi->active_worst_quality < cpi->worst_quality) && (over_size_percent > 0))
3962 cpi->active_worst_quality++;
3963 top_index = cpi->active_worst_quality;
3964 over_size_percent = (int)(over_size_percent * 0.96); // Assume 1 qstep = about 4% on frame size.
3967 // If we have updated the active max Q do not call vp8_update_rate_correction_factors() this loop.
3968 active_worst_qchanged = TRUE;
3971 active_worst_qchanged = FALSE;
3973 #if !(CONFIG_REALTIME_ONLY)
3974 // Special case handling for forced key frames
3975 if ( (cm->frame_type == KEY_FRAME) && cpi->this_key_frame_forced )
3978 int kf_err = vp8_calc_ss_err(cpi->Source,
3979 &cm->yv12_fb[cm->new_fb_idx],
3980 IF_RTCD(&cpi->rtcd.variance));
3982 // The key frame is not good enough
3983 if ( kf_err > ((cpi->ambient_err * 7) >> 3) )
3986 q_high = (Q > q_low) ? (Q - 1) : q_low;
3989 Q = (q_high + q_low) >> 1;
3991 // The key frame is much better than the previous frame
3992 else if ( kf_err < (cpi->ambient_err >> 1) )
3995 q_low = (Q < q_high) ? (Q + 1) : q_high;
3998 Q = (q_high + q_low + 1) >> 1;
4001 // Clamp Q to upper and lower limits:
4007 Loop = ((Q != last_q)) ? TRUE : FALSE;
4010 // Is the projected frame size out of range and are we allowed to attempt to recode.
4011 else if ( recode_loop_test( cpi,
4012 frame_over_shoot_limit, frame_under_shoot_limit,
4013 Q, top_index, bottom_index ) )
4018 // Frame size out of permitted range:
4019 // Update correction factor & compute new Q to try...
4021 // Frame is too large
4022 if (cpi->projected_frame_size > cpi->this_frame_target)
4024 //if ( cpi->zbin_over_quant == 0 )
4025 q_low = (Q < q_high) ? (Q + 1) : q_high; // Raise Qlow as to at least the current value
4027 if (cpi->zbin_over_quant > 0) // If we are using over quant do the same for zbin_oq_low
4028 zbin_oq_low = (cpi->zbin_over_quant < zbin_oq_high) ? (cpi->zbin_over_quant + 1) : zbin_oq_high;
4030 //if ( undershoot_seen || (Q == MAXQ) )
4031 if (undershoot_seen)
4033 // Update rate_correction_factor unless cpi->active_worst_quality has changed.
4034 if (!active_worst_qchanged)
4035 vp8_update_rate_correction_factors(cpi, 1);
4037 Q = (q_high + q_low + 1) / 2;
4039 // Adjust cpi->zbin_over_quant (only allowed when Q is max)
4041 cpi->zbin_over_quant = 0;
4044 zbin_oq_low = (cpi->zbin_over_quant < zbin_oq_high) ? (cpi->zbin_over_quant + 1) : zbin_oq_high;
4045 cpi->zbin_over_quant = (zbin_oq_high + zbin_oq_low) / 2;
4050 // Update rate_correction_factor unless cpi->active_worst_quality has changed.
4051 if (!active_worst_qchanged)
4052 vp8_update_rate_correction_factors(cpi, 0);
4054 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4056 while (((Q < q_low) || (cpi->zbin_over_quant < zbin_oq_low)) && (Retries < 10))
4058 vp8_update_rate_correction_factors(cpi, 0);
4059 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4064 overshoot_seen = TRUE;
4066 // Frame is too small
4069 if (cpi->zbin_over_quant == 0)
4070 q_high = (Q > q_low) ? (Q - 1) : q_low; // Lower q_high if not using over quant
4071 else // else lower zbin_oq_high
4072 zbin_oq_high = (cpi->zbin_over_quant > zbin_oq_low) ? (cpi->zbin_over_quant - 1) : zbin_oq_low;
4076 // Update rate_correction_factor unless cpi->active_worst_quality has changed.
4077 if (!active_worst_qchanged)
4078 vp8_update_rate_correction_factors(cpi, 1);
4080 Q = (q_high + q_low) / 2;
4082 // Adjust cpi->zbin_over_quant (only allowed when Q is max)
4084 cpi->zbin_over_quant = 0;
4086 cpi->zbin_over_quant = (zbin_oq_high + zbin_oq_low) / 2;
4090 // Update rate_correction_factor unless cpi->active_worst_quality has changed.
4091 if (!active_worst_qchanged)
4092 vp8_update_rate_correction_factors(cpi, 0);
4094 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4096 // Special case reset for qlow for constrained quality.
4097 // This should only trigger where there is very substantial
4098 // undershoot on a frame and the auto cq level is above
4099 // the user passsed in value.
4100 if ( (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) &&
4106 while (((Q > q_high) || (cpi->zbin_over_quant > zbin_oq_high)) && (Retries < 10))
4108 vp8_update_rate_correction_factors(cpi, 0);
4109 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4114 undershoot_seen = TRUE;
4117 // Clamp Q to upper and lower limits:
4123 // Clamp cpi->zbin_over_quant
4124 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;
4126 //Loop = ((Q != last_q) || (last_zbin_oq != cpi->zbin_over_quant)) ? TRUE : FALSE;
4127 Loop = ((Q != last_q)) ? TRUE : FALSE;
4128 last_zbin_oq = cpi->zbin_over_quant;
4134 if (cpi->is_src_frame_alt_ref)
4139 vp8_restore_coding_context(cpi);
4142 cpi->tot_recode_hits++;
4146 while (Loop == TRUE);
4149 // Experimental code for lagged and one pass
4150 // Update stats used for one pass GF selection
4154 double frame_intra_error;
4155 double frame_coded_error;
4156 double frame_pcnt_inter;
4157 double frame_pcnt_motion;
4159 double frame_mvr_abs;
4161 double frame_mvc_abs;
4164 cpi->one_pass_frame_stats[cpi->one_pass_frame_index].frame_coded_error = (double)cpi->prediction_error;
4165 cpi->one_pass_frame_stats[cpi->one_pass_frame_index].frame_intra_error = (double)cpi->intra_error;
4166 cpi->one_pass_frame_stats[cpi->one_pass_frame_index].frame_pcnt_inter = (double)(100 - cpi->this_frame_percent_intra) / 100.0;
4170 // Special case code to reduce pulsing when key frames are forced at a
4171 // fixed interval. Note the reconstruction error if it is the frame before
4172 // the force key frame
4173 if ( cpi->next_key_frame_forced && (cpi->frames_to_key == 0) )
4175 cpi->ambient_err = vp8_calc_ss_err(cpi->Source,
4176 &cm->yv12_fb[cm->new_fb_idx],
4177 IF_RTCD(&cpi->rtcd.variance));
4180 // This frame's MVs are saved and will be used in next frame's MV prediction.
4181 // 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.
4182 if(cm->show_frame) //do not save for altref frame
4186 MODE_INFO *tmp = cm->mip; //point to beginning of allocated MODE_INFO arrays.
4188 if(cm->frame_type != KEY_FRAME)
4190 for (mb_row = 0; mb_row < cm->mb_rows+1; mb_row ++)
4192 for (mb_col = 0; mb_col < cm->mb_cols+1; mb_col ++)
4194 if(tmp->mbmi.ref_frame != INTRA_FRAME)
4195 cpi->lfmv[mb_col + mb_row*(cm->mode_info_stride+1)].as_int = tmp->mbmi.mv.as_int;
4197 cpi->lf_ref_frame_sign_bias[mb_col + mb_row*(cm->mode_info_stride+1)] = cm->ref_frame_sign_bias[tmp->mbmi.ref_frame];
4198 cpi->lf_ref_frame[mb_col + mb_row*(cm->mode_info_stride+1)] = tmp->mbmi.ref_frame;
4205 // Update the GF useage maps.
4206 // This is done after completing the compression of a frame when all modes etc. are finalized but before loop filter
4207 // This is done after completing the compression of a frame when all modes etc. are finalized but before loop filter
4208 vp8_update_gf_useage_maps(cpi, cm, &cpi->mb);
4210 if (cm->frame_type == KEY_FRAME)
4211 cm->refresh_last_frame = 1;
4215 FILE *f = fopen("gfactive.stt", "a");
4216 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);
4221 // For inter frames the current default behaviour is that when cm->refresh_golden_frame is set we copy the old GF over to the ARF buffer
4222 // This is purely an encoder descision at present.
4223 if (!cpi->oxcf.error_resilient_mode && cm->refresh_golden_frame)
4224 cm->copy_buffer_to_arf = 2;
4226 cm->copy_buffer_to_arf = 0;
4228 if (cm->refresh_last_frame)
4230 vp8_swap_yv12_buffer(&cm->yv12_fb[cm->lst_fb_idx], &cm->yv12_fb[cm->new_fb_idx]);
4231 cm->frame_to_show = &cm->yv12_fb[cm->lst_fb_idx];
4234 cm->frame_to_show = &cm->yv12_fb[cm->new_fb_idx];
4237 #if CONFIG_MULTITHREAD
4238 if (cpi->b_multi_threaded)
4240 sem_post(&cpi->h_event_start_lpf); /* start loopfilter in separate thread */
4245 loopfilter_frame(cpi, cm);
4248 if (cpi->oxcf.error_resilient_mode == 1)
4250 cm->refresh_entropy_probs = 0;
4253 #if CONFIG_MULTITHREAD
4254 /* wait that filter_level is picked so that we can continue with stream packing */
4255 if (cpi->b_multi_threaded)
4256 sem_wait(&cpi->h_event_end_lpf);
4259 // build the bitstream
4260 vp8_pack_bitstream(cpi, dest, size);
4262 #if CONFIG_MULTITHREAD
4263 /* wait for loopfilter thread done */
4264 if (cpi->b_multi_threaded)
4266 sem_wait(&cpi->h_event_end_lpf);
4270 /* Move storing frame_type out of the above loop since it is also
4271 * needed in motion search besides loopfilter */
4272 cm->last_frame_type = cm->frame_type;
4274 // Update rate control heuristics
4275 cpi->total_byte_count += (*size);
4276 cpi->projected_frame_size = (*size) << 3;
4278 if (!active_worst_qchanged)
4279 vp8_update_rate_correction_factors(cpi, 2);
4281 cpi->last_q[cm->frame_type] = cm->base_qindex;
4283 if (cm->frame_type == KEY_FRAME)
4285 vp8_adjust_key_frame_context(cpi);
4288 // Keep a record of ambient average Q.
4289 if (cm->frame_type != KEY_FRAME)
4290 cpi->avg_frame_qindex = (2 + 3 * cpi->avg_frame_qindex + cm->base_qindex) >> 2;
4292 // Keep a record from which we can calculate the average Q excluding GF updates and key frames
4293 if ((cm->frame_type != KEY_FRAME) && !cm->refresh_golden_frame && !cm->refresh_alt_ref_frame)
4297 // Calculate the average Q for normal inter frames (not key or GFU
4299 if ( cpi->pass == 2 )
4301 cpi->ni_tot_qi += Q;
4302 cpi->ni_av_qi = (cpi->ni_tot_qi / cpi->ni_frames);
4306 // Damp value for first few frames
4307 if (cpi->ni_frames > 150 )
4309 cpi->ni_tot_qi += Q;
4310 cpi->ni_av_qi = (cpi->ni_tot_qi / cpi->ni_frames);
4312 // For one pass, early in the clip ... average the current frame Q
4313 // value with the worstq entered by the user as a dampening measure
4316 cpi->ni_tot_qi += Q;
4317 cpi->ni_av_qi = ((cpi->ni_tot_qi / cpi->ni_frames) + cpi->worst_quality + 1) / 2;
4320 // If the average Q is higher than what was used in the last frame
4321 // (after going through the recode loop to keep the frame size within range)
4322 // then use the last frame value - 1.
4323 // The -1 is designed to stop Q and hence the data rate, from progressively
4324 // falling away during difficult sections, but at the same time reduce the number of
4325 // itterations around the recode loop.
4326 if (Q > cpi->ni_av_qi)
4327 cpi->ni_av_qi = Q - 1;
4333 // If the frame was massively oversize and we are below optimal buffer level drop next frame
4334 if ((cpi->drop_frames_allowed) &&
4335 (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) &&
4336 (cpi->buffer_level < cpi->oxcf.drop_frames_water_mark * cpi->oxcf.optimal_buffer_level / 100) &&
4337 (cpi->projected_frame_size > (4 * cpi->this_frame_target)))
4339 cpi->drop_frame = TRUE;
4344 // Set the count for maximum consequative dropped frames based upon the ratio of
4345 // this frame size to the target average per frame bandwidth.
4346 // (cpi->av_per_frame_bandwidth > 0) is just a sanity check to prevent / 0.
4347 if (cpi->drop_frames_allowed && (cpi->av_per_frame_bandwidth > 0))
4349 cpi->max_drop_count = cpi->projected_frame_size / cpi->av_per_frame_bandwidth;
4351 if (cpi->max_drop_count > cpi->max_consec_dropped_frames)
4352 cpi->max_drop_count = cpi->max_consec_dropped_frames;
4355 // Update the buffer level variable.
4356 // Non-viewable frames are a special case and are treated as pure overhead.
4357 if ( !cm->show_frame )
4358 cpi->bits_off_target -= cpi->projected_frame_size;
4360 cpi->bits_off_target += cpi->av_per_frame_bandwidth - cpi->projected_frame_size;
4362 // Rolling monitors of whether we are over or underspending used to help regulate min and Max Q in two pass.
4363 cpi->rolling_target_bits = ((cpi->rolling_target_bits * 3) + cpi->this_frame_target + 2) / 4;
4364 cpi->rolling_actual_bits = ((cpi->rolling_actual_bits * 3) + cpi->projected_frame_size + 2) / 4;
4365 cpi->long_rolling_target_bits = ((cpi->long_rolling_target_bits * 31) + cpi->this_frame_target + 16) / 32;
4366 cpi->long_rolling_actual_bits = ((cpi->long_rolling_actual_bits * 31) + cpi->projected_frame_size + 16) / 32;
4368 // Actual bits spent
4369 cpi->total_actual_bits += cpi->projected_frame_size;
4372 cpi->total_target_vs_actual += (cpi->this_frame_target - cpi->projected_frame_size);
4374 cpi->buffer_level = cpi->bits_off_target;
4376 // Update bits left to the kf and gf groups to account for overshoot or undershoot on these frames
4377 if (cm->frame_type == KEY_FRAME)
4379 cpi->kf_group_bits += cpi->this_frame_target - cpi->projected_frame_size;
4381 if (cpi->kf_group_bits < 0)
4382 cpi->kf_group_bits = 0 ;
4384 else if (cm->refresh_golden_frame || cm->refresh_alt_ref_frame)
4386 cpi->gf_group_bits += cpi->this_frame_target - cpi->projected_frame_size;
4388 if (cpi->gf_group_bits < 0)
4389 cpi->gf_group_bits = 0 ;
4392 if (cm->frame_type != KEY_FRAME)
4394 if (cpi->common.refresh_alt_ref_frame)
4396 cpi->last_skip_false_probs[2] = cpi->prob_skip_false;
4397 cpi->last_skip_probs_q[2] = cm->base_qindex;
4399 else if (cpi->common.refresh_golden_frame)
4401 cpi->last_skip_false_probs[1] = cpi->prob_skip_false;
4402 cpi->last_skip_probs_q[1] = cm->base_qindex;
4406 cpi->last_skip_false_probs[0] = cpi->prob_skip_false;
4407 cpi->last_skip_probs_q[0] = cm->base_qindex;
4409 //update the baseline
4410 cpi->base_skip_false_prob[cm->base_qindex] = cpi->prob_skip_false;
4415 #if 0 && CONFIG_PSNR
4417 FILE *f = fopen("tmp.stt", "a");
4419 vp8_clear_system_state(); //__asm emms;
4421 if (cpi->total_coded_error_left != 0.0)
4422 fprintf(f, "%10d %10d %10d %10d %10d %10d %10d %10d %6ld %6ld"
4423 "%6ld %6ld %6ld %5ld %5ld %5ld %8ld %8.2f %10d %10.3f"
4425 cpi->common.current_video_frame, cpi->this_frame_target,
4426 cpi->projected_frame_size,
4427 (cpi->projected_frame_size - cpi->this_frame_target),
4428 (int)cpi->total_target_vs_actual,
4429 (cpi->oxcf.starting_buffer_level-cpi->bits_off_target),
4430 (int)cpi->total_actual_bits, cm->base_qindex,
4431 cpi->active_best_quality, cpi->active_worst_quality,
4432 cpi->ni_av_qi, cpi->cq_target_quality, cpi->zbin_over_quant,
4433 //cpi->avg_frame_qindex, cpi->zbin_over_quant,
4434 cm->refresh_golden_frame, cm->refresh_alt_ref_frame,
4435 cm->frame_type, cpi->gfu_boost,
4436 cpi->est_max_qcorrection_factor, (int)cpi->bits_left,
4437 cpi->total_coded_error_left,
4438 (double)cpi->bits_left / cpi->total_coded_error_left,
4439 cpi->tot_recode_hits);
4441 fprintf(f, "%10d %10d %10d %10d %10d %10d %10d %10d %6ld %6ld"
4442 "%6ld %6ld %6ld %5ld %5ld %5ld %8ld %8.2f %10d %10.3f"
4444 cpi->common.current_video_frame,
4445 cpi->this_frame_target, cpi->projected_frame_size,
4446 (cpi->projected_frame_size - cpi->this_frame_target),
4447 (int)cpi->total_target_vs_actual,
4448 (cpi->oxcf.starting_buffer_level-cpi->bits_off_target),
4449 (int)cpi->total_actual_bits, cm->base_qindex,
4450 cpi->active_best_quality, cpi->active_worst_quality,
4451 cpi->ni_av_qi, cpi->cq_target_quality, cpi->zbin_over_quant,
4452 //cpi->avg_frame_qindex, cpi->zbin_over_quant,
4453 cm->refresh_golden_frame, cm->refresh_alt_ref_frame,
4454 cm->frame_type, cpi->gfu_boost,
4455 cpi->est_max_qcorrection_factor, (int)cpi->bits_left,
4456 cpi->total_coded_error_left, cpi->tot_recode_hits);
4461 FILE *fmodes = fopen("Modes.stt", "a");
4464 fprintf(fmodes, "%6d:%1d:%1d:%1d ",
4465 cpi->common.current_video_frame,
4466 cm->frame_type, cm->refresh_golden_frame,
4467 cm->refresh_alt_ref_frame);
4469 for (i = 0; i < MAX_MODES; i++)
4470 fprintf(fmodes, "%5d ", cpi->mode_chosen_counts[i]);
4472 fprintf(fmodes, "\n");
4480 // If this was a kf or Gf note the Q
4481 if ((cm->frame_type == KEY_FRAME) || cm->refresh_golden_frame || cm->refresh_alt_ref_frame)
4482 cm->last_kf_gf_q = cm->base_qindex;
4484 if (cm->refresh_golden_frame == 1)
4485 cm->frame_flags = cm->frame_flags | FRAMEFLAGS_GOLDEN;
4487 cm->frame_flags = cm->frame_flags&~FRAMEFLAGS_GOLDEN;
4489 if (cm->refresh_alt_ref_frame == 1)
4490 cm->frame_flags = cm->frame_flags | FRAMEFLAGS_ALTREF;
4492 cm->frame_flags = cm->frame_flags&~FRAMEFLAGS_ALTREF;
4495 if (cm->refresh_last_frame & cm->refresh_golden_frame) // both refreshed
4496 cpi->gold_is_last = 1;
4497 else if (cm->refresh_last_frame ^ cm->refresh_golden_frame) // 1 refreshed but not the other
4498 cpi->gold_is_last = 0;
4500 if (cm->refresh_last_frame & cm->refresh_alt_ref_frame) // both refreshed
4501 cpi->alt_is_last = 1;
4502 else if (cm->refresh_last_frame ^ cm->refresh_alt_ref_frame) // 1 refreshed but not the other
4503 cpi->alt_is_last = 0;
4505 if (cm->refresh_alt_ref_frame & cm->refresh_golden_frame) // both refreshed
4506 cpi->gold_is_alt = 1;
4507 else if (cm->refresh_alt_ref_frame ^ cm->refresh_golden_frame) // 1 refreshed but not the other
4508 cpi->gold_is_alt = 0;
4510 cpi->ref_frame_flags = VP8_ALT_FLAG | VP8_GOLD_FLAG | VP8_LAST_FLAG;
4512 if (cpi->gold_is_last)
4513 cpi->ref_frame_flags &= ~VP8_GOLD_FLAG;
4515 if (cpi->alt_is_last)
4516 cpi->ref_frame_flags &= ~VP8_ALT_FLAG;
4518 if (cpi->gold_is_alt)
4519 cpi->ref_frame_flags &= ~VP8_ALT_FLAG;
4522 if (cpi->oxcf.error_resilient_mode)
4524 if (cm->frame_type != KEY_FRAME)
4526 // Is this an alternate reference update
4527 if (cm->refresh_alt_ref_frame)
4528 vp8_yv12_copy_frame_ptr(cm->frame_to_show, &cm->yv12_fb[cm->alt_fb_idx]);
4530 if (cm->refresh_golden_frame)
4531 vp8_yv12_copy_frame_ptr(cm->frame_to_show, &cm->yv12_fb[cm->gld_fb_idx]);
4536 if (cpi->oxcf.play_alternate && cm->refresh_alt_ref_frame && (cm->frame_type != KEY_FRAME))
4537 // Update the alternate reference frame and stats as appropriate.
4538 update_alt_ref_frame_and_stats(cpi);
4540 // Update the Golden frame and golden frame and stats as appropriate.
4541 update_golden_frame_and_stats(cpi);
4544 if (cm->frame_type == KEY_FRAME)
4546 // Tell the caller that the frame was coded as a key frame
4547 *frame_flags = cm->frame_flags | FRAMEFLAGS_KEY;
4549 // As this frame is a key frame the next defaults to an inter frame.
4550 cm->frame_type = INTER_FRAME;
4552 cpi->last_frame_percent_intra = 100;
4556 *frame_flags = cm->frame_flags&~FRAMEFLAGS_KEY;
4558 cpi->last_frame_percent_intra = cpi->this_frame_percent_intra;
4561 // Clear the one shot update flags for segmentation map and mode/ref loop filter deltas.
4562 cpi->mb.e_mbd.update_mb_segmentation_map = 0;
4563 cpi->mb.e_mbd.update_mb_segmentation_data = 0;
4564 cpi->mb.e_mbd.mode_ref_lf_delta_update = 0;
4567 // Dont increment frame counters if this was an altref buffer update not a real frame
4570 cm->current_video_frame++;
4571 cpi->frames_since_key++;
4574 // reset to normal state now that we are done.
4582 sprintf(filename, "enc%04d.yuv", (int) cm->current_video_frame);
4583 recon_file = fopen(filename, "wb");
4584 fwrite(cm->yv12_fb[cm->lst_fb_idx].buffer_alloc,
4585 cm->yv12_fb[cm->lst_fb_idx].frame_size, 1, recon_file);
4591 //vp8_write_yuv_frame("encoder_recon.yuv", cm->frame_to_show);
4596 int vp8_is_gf_update_needed(VP8_PTR ptr)
4598 VP8_COMP *cpi = (VP8_COMP *) ptr;
4601 ret_val = cpi->gf_update_recommended;
4602 cpi->gf_update_recommended = 0;
4607 void vp8_check_gf_quality(VP8_COMP *cpi)
4609 VP8_COMMON *cm = &cpi->common;
4610 int gf_active_pct = (100 * cpi->gf_active_count) / (cm->mb_rows * cm->mb_cols);
4611 int gf_ref_usage_pct = (cpi->count_mb_ref_frame_usage[GOLDEN_FRAME] * 100) / (cm->mb_rows * cm->mb_cols);
4612 int last_ref_zz_useage = (cpi->inter_zz_count * 100) / (cm->mb_rows * cm->mb_cols);
4614 // Gf refresh is not currently being signalled
4615 if (cpi->gf_update_recommended == 0)
4617 if (cpi->common.frames_since_golden > 7)
4620 if ((gf_active_pct < 10) || ((gf_active_pct + gf_ref_usage_pct) < 15))
4622 // ...but last frame zero zero usage is reasonbable so a new gf might be appropriate
4623 if (last_ref_zz_useage >= 25)
4625 cpi->gf_bad_count ++;
4627 if (cpi->gf_bad_count >= 8) // Check that the condition is stable
4629 cpi->gf_update_recommended = 1;
4630 cpi->gf_bad_count = 0;
4634 cpi->gf_bad_count = 0; // Restart count as the background is not stable enough
4637 cpi->gf_bad_count = 0; // Gf useage has picked up so reset count
4640 // If the signal is set but has not been read should we cancel it.
4641 else if (last_ref_zz_useage < 15)
4643 cpi->gf_update_recommended = 0;
4644 cpi->gf_bad_count = 0;
4649 FILE *f = fopen("gfneeded.stt", "a");
4650 fprintf(f, "%10d %10d %10d %10d %10ld \n",
4651 cm->current_video_frame,
4652 cpi->common.frames_since_golden,
4653 gf_active_pct, gf_ref_usage_pct,
4654 cpi->gf_update_recommended);
4661 #if !(CONFIG_REALTIME_ONLY)
4662 static void Pass2Encode(VP8_COMP *cpi, unsigned long *size, unsigned char *dest, unsigned int *frame_flags)
4665 if (!cpi->common.refresh_alt_ref_frame)
4666 vp8_second_pass(cpi);
4668 encode_frame_to_data_rate(cpi, size, dest, frame_flags);
4669 cpi->bits_left -= 8 * *size;
4671 if (!cpi->common.refresh_alt_ref_frame)
4673 double two_pass_min_rate = (double)(cpi->oxcf.target_bandwidth
4674 *cpi->oxcf.two_pass_vbrmin_section / 100);
4675 cpi->bits_left += (long long)(two_pass_min_rate / cpi->oxcf.frame_rate);
4680 //For ARM NEON, d8-d15 are callee-saved registers, and need to be saved by us.
4682 extern void vp8_push_neon(INT64 *store);
4683 extern void vp8_pop_neon(INT64 *store);
4685 int vp8_receive_raw_frame(VP8_PTR ptr, unsigned int frame_flags, YV12_BUFFER_CONFIG *sd, INT64 time_stamp, INT64 end_time)
4690 VP8_COMP *cpi = (VP8_COMP *) ptr;
4691 VP8_COMMON *cm = &cpi->common;
4692 struct vpx_usec_timer timer;
4698 #if CONFIG_RUNTIME_CPU_DETECT
4699 if (cm->rtcd.flags & HAS_NEON)
4702 vp8_push_neon(store_reg);
4706 vpx_usec_timer_start(&timer);
4708 // no more room for frames;
4709 if (cpi->source_buffer_count != 0 && cpi->source_buffer_count >= cpi->oxcf.lag_in_frames)
4712 #if CONFIG_RUNTIME_CPU_DETECT
4713 if (cm->rtcd.flags & HAS_NEON)
4716 vp8_pop_neon(store_reg);
4722 //printf("in-cpi->source_buffer_count: %d\n", cpi->source_buffer_count);
4724 cm->clr_type = sd->clrtype;
4726 // make a copy of the frame for use later...
4727 #if !(CONFIG_REALTIME_ONLY)
4729 if (cpi->oxcf.allow_lag)
4731 int which_buffer = cpi->source_encode_index - 1;
4734 if (which_buffer == -1)
4735 which_buffer = cpi->oxcf.lag_in_frames - 1;
4737 if (cpi->source_buffer_count < cpi->oxcf.lag_in_frames - 1)
4738 which_buffer = cpi->source_buffer_count;
4740 s = &cpi->src_buffer[which_buffer];
4742 s->source_time_stamp = time_stamp;
4743 s->source_end_time_stamp = end_time;
4744 s->source_frame_flags = frame_flags;
4745 vp8_yv12_copy_frame_ptr(sd, &s->source_buffer);
4747 cpi->source_buffer_count ++;
4753 s = &cpi->src_buffer[0];
4754 s->source_end_time_stamp = end_time;
4755 s->source_time_stamp = time_stamp;
4756 s->source_frame_flags = frame_flags;
4758 #if CONFIG_RUNTIME_CPU_DETECT
4759 if (cm->rtcd.flags & HAS_NEON)
4762 vp8_yv12_copy_src_frame_func_neon(sd, &s->source_buffer);
4764 #if CONFIG_RUNTIME_CPU_DETECT
4768 #if !HAVE_ARMV7 || CONFIG_RUNTIME_CPU_DETECT
4770 vp8_yv12_copy_frame_ptr(sd, &s->source_buffer);
4773 cpi->source_buffer_count = 1;
4776 vpx_usec_timer_mark(&timer);
4777 cpi->time_receive_data += vpx_usec_timer_elapsed(&timer);
4780 #if CONFIG_RUNTIME_CPU_DETECT
4781 if (cm->rtcd.flags & HAS_NEON)
4784 vp8_pop_neon(store_reg);
4790 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)
4795 VP8_COMP *cpi = (VP8_COMP *) ptr;
4796 VP8_COMMON *cm = &cpi->common;
4797 struct vpx_usec_timer tsctimer;
4798 struct vpx_usec_timer ticktimer;
4799 struct vpx_usec_timer cmptimer;
4805 #if CONFIG_RUNTIME_CPU_DETECT
4806 if (cm->rtcd.flags & HAS_NEON)
4809 vp8_push_neon(store_reg);
4813 vpx_usec_timer_start(&cmptimer);
4816 // flush variable tells us that even though we have less than 10 frames
4817 // in our buffer we need to start producing compressed frames.
4818 // Probably because we are at the end of a file....
4819 if ((cpi->source_buffer_count == cpi->oxcf.lag_in_frames && cpi->oxcf.lag_in_frames > 0)
4820 || (!cpi->oxcf.allow_lag && cpi->source_buffer_count > 0)
4821 || (flush && cpi->source_buffer_count > 0))
4826 s = &cpi->src_buffer[cpi->source_encode_index];
4827 cpi->source_time_stamp = s->source_time_stamp;
4828 cpi->source_end_time_stamp = s->source_end_time_stamp;
4830 #if !(CONFIG_REALTIME_ONLY)
4832 // Should we code an alternate reference frame
4833 if (cpi->oxcf.error_resilient_mode == 0 &&
4834 cpi->oxcf.play_alternate &&
4835 cpi->source_alt_ref_pending &&
4836 (cpi->frames_till_gf_update_due < cpi->source_buffer_count) &&
4837 cpi->oxcf.lag_in_frames != 0)
4839 cpi->last_alt_ref_sei = (cpi->source_encode_index + cpi->frames_till_gf_update_due) % cpi->oxcf.lag_in_frames;
4841 #if VP8_TEMPORAL_ALT_REF
4843 if (cpi->oxcf.arnr_max_frames > 0)
4846 // my attempt at a loop that tests the results of strength filter.
4847 int start_frame = cpi->last_alt_ref_sei - 3;
4849 int i, besti = -1, pastin = cpi->oxcf.arnr_strength;
4853 if (start_frame < 0)
4854 start_frame += cpi->oxcf.lag_in_frames;
4856 besterr = vp8_calc_low_ss_err(&cpi->src_buffer[cpi->last_alt_ref_sei].source_buffer,
4857 &cpi->src_buffer[start_frame].source_buffer, IF_RTCD(&cpi->rtcd.variance));
4859 for (i = 0; i < 7; i++)
4862 cpi->oxcf.arnr_strength = i;
4863 vp8_temporal_filter_prepare_c(cpi);
4865 thiserr = vp8_calc_low_ss_err(&cpi->alt_ref_buffer.source_buffer,
4866 &cpi->src_buffer[start_frame].source_buffer, IF_RTCD(&cpi->rtcd.variance));
4868 if (10 * thiserr < besterr * 8)
4877 cpi->oxcf.arnr_strength = besti;
4878 vp8_temporal_filter_prepare_c(cpi);
4879 s = &cpi->alt_ref_buffer;
4881 // FWG not sure if I need to copy this data for the Alt Ref frame
4882 s->source_time_stamp = cpi->src_buffer[cpi->last_alt_ref_sei].source_time_stamp;
4883 s->source_end_time_stamp = cpi->src_buffer[cpi->last_alt_ref_sei].source_end_time_stamp;
4884 s->source_frame_flags = cpi->src_buffer[cpi->last_alt_ref_sei].source_frame_flags;
4887 s = &cpi->src_buffer[cpi->last_alt_ref_sei];
4890 vp8_temporal_filter_prepare_c(cpi);
4891 s = &cpi->alt_ref_buffer;
4893 // FWG not sure if I need to copy this data for the Alt Ref frame
4894 s->source_time_stamp = cpi->src_buffer[cpi->last_alt_ref_sei].source_time_stamp;
4895 s->source_end_time_stamp = cpi->src_buffer[cpi->last_alt_ref_sei].source_end_time_stamp;
4896 s->source_frame_flags = cpi->src_buffer[cpi->last_alt_ref_sei].source_frame_flags;
4902 s = &cpi->src_buffer[cpi->last_alt_ref_sei];
4904 cm->frames_till_alt_ref_frame = cpi->frames_till_gf_update_due;
4905 cm->refresh_alt_ref_frame = 1;
4906 cm->refresh_golden_frame = 0;
4907 cm->refresh_last_frame = 0;
4909 cpi->source_alt_ref_pending = FALSE; // Clear Pending altf Ref flag.
4910 cpi->is_src_frame_alt_ref = 0;
4911 cpi->is_next_src_alt_ref = 0;
4917 #if !(CONFIG_REALTIME_ONLY)
4919 if (cpi->oxcf.allow_lag)
4921 if (cpi->source_encode_index == cpi->last_alt_ref_sei)
4923 cpi->is_src_frame_alt_ref = 1;
4924 cpi->last_alt_ref_sei = -1;
4927 cpi->is_src_frame_alt_ref = 0;
4929 cpi->source_encode_index = (cpi->source_encode_index + 1) % cpi->oxcf.lag_in_frames;
4931 if(cpi->source_encode_index == cpi->last_alt_ref_sei)
4932 cpi->is_next_src_alt_ref = 1;
4934 cpi->is_next_src_alt_ref = 0;
4938 cpi->source_buffer_count--;
4941 cpi->un_scaled_source = &s->source_buffer;
4942 cpi->Source = &s->source_buffer;
4943 cpi->source_frame_flags = s->source_frame_flags;
4945 *time_stamp = cpi->source_time_stamp;
4946 *time_end = cpi->source_end_time_stamp;
4951 #if !(CONFIG_REALTIME_ONLY)
4953 if (flush && cpi->pass == 1 && !cpi->first_pass_done)
4955 vp8_end_first_pass(cpi); /* get last stats packet */
4956 cpi->first_pass_done = 1;
4962 #if CONFIG_RUNTIME_CPU_DETECT
4963 if (cm->rtcd.flags & HAS_NEON)
4966 vp8_pop_neon(store_reg);
4972 *frame_flags = cpi->source_frame_flags;
4974 if (cpi->source_time_stamp < cpi->first_time_stamp_ever)
4976 cpi->first_time_stamp_ever = cpi->source_time_stamp;
4977 cpi->last_end_time_stamp_seen = cpi->source_time_stamp;
4980 // adjust frame rates based on timestamps given
4981 if (!cm->refresh_alt_ref_frame)
4983 if (cpi->source_time_stamp == cpi->first_time_stamp_ever)
4985 double this_fps = 10000000.000 / (cpi->source_end_time_stamp - cpi->source_time_stamp);
4987 vp8_new_frame_rate(cpi, this_fps);
4991 long long nanosecs = cpi->source_end_time_stamp
4992 - cpi->last_end_time_stamp_seen;
4996 double this_fps = 10000000.000 / nanosecs;
4997 vp8_new_frame_rate(cpi, (7 * cpi->oxcf.frame_rate + this_fps) / 8);
5002 cpi->last_time_stamp_seen = cpi->source_time_stamp;
5003 cpi->last_end_time_stamp_seen = cpi->source_end_time_stamp;
5006 if (cpi->compressor_speed == 2)
5008 vp8_check_gf_quality(cpi);
5009 vpx_usec_timer_start(&tsctimer);
5010 vpx_usec_timer_start(&ticktimer);
5013 // start with a 0 size frame
5016 // Clear down mmx registers
5017 vp8_clear_system_state(); //__asm emms;
5019 cm->frame_type = INTER_FRAME;
5020 cm->frame_flags = *frame_flags;
5024 if (cm->refresh_alt_ref_frame)
5026 //cm->refresh_golden_frame = 1;
5027 cm->refresh_golden_frame = 0;
5028 cm->refresh_last_frame = 0;
5032 cm->refresh_golden_frame = 0;
5033 cm->refresh_last_frame = 1;
5038 #if !(CONFIG_REALTIME_ONLY)
5042 Pass1Encode(cpi, size, dest, frame_flags);
5044 else if (cpi->pass == 2)
5046 Pass2Encode(cpi, size, dest, frame_flags);
5050 encode_frame_to_data_rate(cpi, size, dest, frame_flags);
5052 if (cpi->compressor_speed == 2)
5054 unsigned int duration, duration2;
5055 vpx_usec_timer_mark(&tsctimer);
5056 vpx_usec_timer_mark(&ticktimer);
5058 duration = vpx_usec_timer_elapsed(&ticktimer);
5059 duration2 = (unsigned int)((double)duration / 2);
5061 if (cm->frame_type != KEY_FRAME)
5063 if (cpi->avg_encode_time == 0)
5064 cpi->avg_encode_time = duration;
5066 cpi->avg_encode_time = (7 * cpi->avg_encode_time + duration) >> 3;
5071 //if(*frame_flags!=1)
5074 if (cpi->avg_pick_mode_time == 0)
5075 cpi->avg_pick_mode_time = duration2;
5077 cpi->avg_pick_mode_time = (7 * cpi->avg_pick_mode_time + duration2) >> 3;
5083 if (cm->refresh_entropy_probs == 0)
5085 vpx_memcpy(&cm->fc, &cm->lfc, sizeof(cm->fc));
5088 // if its a dropped frame honor the requests on subsequent frames
5092 // return to normal state
5093 cm->refresh_entropy_probs = 1;
5094 cm->refresh_alt_ref_frame = 0;
5095 cm->refresh_golden_frame = 0;
5096 cm->refresh_last_frame = 1;
5097 cm->frame_type = INTER_FRAME;
5101 cpi->ready_for_new_frame = 1;
5103 vpx_usec_timer_mark(&cmptimer);
5104 cpi->time_compress_data += vpx_usec_timer_elapsed(&cmptimer);
5106 if (cpi->b_calculate_psnr && cpi->pass != 1 && cm->show_frame)
5108 generate_psnr_packet(cpi);
5115 cpi->bytes += *size;
5122 if (cpi->b_calculate_psnr)
5127 YV12_BUFFER_CONFIG *orig = cpi->Source;
5128 YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
5129 YV12_BUFFER_CONFIG *pp = &cm->post_proc_buffer;
5130 int y_samples = orig->y_height * orig->y_width ;
5131 int uv_samples = orig->uv_height * orig->uv_width ;
5132 int t_samples = y_samples + 2 * uv_samples;
5135 ye = calc_plane_error(orig->y_buffer, orig->y_stride,
5136 recon->y_buffer, recon->y_stride, orig->y_width, orig->y_height,
5137 IF_RTCD(&cpi->rtcd.variance));
5139 ue = calc_plane_error(orig->u_buffer, orig->uv_stride,
5140 recon->u_buffer, recon->uv_stride, orig->uv_width, orig->uv_height,
5141 IF_RTCD(&cpi->rtcd.variance));
5143 ve = calc_plane_error(orig->v_buffer, orig->uv_stride,
5144 recon->v_buffer, recon->uv_stride, orig->uv_width, orig->uv_height,
5145 IF_RTCD(&cpi->rtcd.variance));
5147 sq_error = ye + ue + ve;
5149 frame_psnr = vp8_mse2psnr(t_samples, 255.0, sq_error);
5151 cpi->total_y += vp8_mse2psnr(y_samples, 255.0, ye);
5152 cpi->total_u += vp8_mse2psnr(uv_samples, 255.0, ue);
5153 cpi->total_v += vp8_mse2psnr(uv_samples, 255.0, ve);
5154 cpi->total_sq_error += sq_error;
5155 cpi->total += frame_psnr;
5157 double y2, u2, v2, frame_psnr2, frame_ssim2 = 0;
5160 vp8_deblock(cm->frame_to_show, &cm->post_proc_buffer, cm->filter_level * 10 / 6, 1, 0, IF_RTCD(&cm->rtcd.postproc));
5161 vp8_clear_system_state();
5163 ye = calc_plane_error(orig->y_buffer, orig->y_stride,
5164 pp->y_buffer, pp->y_stride, orig->y_width, orig->y_height,
5165 IF_RTCD(&cpi->rtcd.variance));
5167 ue = calc_plane_error(orig->u_buffer, orig->uv_stride,
5168 pp->u_buffer, pp->uv_stride, orig->uv_width, orig->uv_height,
5169 IF_RTCD(&cpi->rtcd.variance));
5171 ve = calc_plane_error(orig->v_buffer, orig->uv_stride,
5172 pp->v_buffer, pp->uv_stride, orig->uv_width, orig->uv_height,
5173 IF_RTCD(&cpi->rtcd.variance));
5175 sq_error = ye + ue + ve;
5177 frame_psnr2 = vp8_mse2psnr(t_samples, 255.0, sq_error);
5179 cpi->totalp_y += vp8_mse2psnr(y_samples, 255.0, ye);
5180 cpi->totalp_u += vp8_mse2psnr(uv_samples, 255.0, ue);
5181 cpi->totalp_v += vp8_mse2psnr(uv_samples, 255.0, ve);
5182 cpi->total_sq_error2 += sq_error;
5183 cpi->totalp += frame_psnr2;
5185 frame_ssim2 = vp8_calc_ssim(cpi->Source,
5186 &cm->post_proc_buffer, 1, &weight,
5187 IF_RTCD(&cpi->rtcd.variance));
5189 cpi->summed_quality += frame_ssim2 * weight;
5190 cpi->summed_weights += weight;
5195 if (cpi->b_calculate_ssimg)
5197 double y, u, v, frame_all;
5198 frame_all = vp8_calc_ssimg(cpi->Source, cm->frame_to_show, &y, &u, &v);
5199 cpi->total_ssimg_y += y;
5200 cpi->total_ssimg_u += u;
5201 cpi->total_ssimg_v += v;
5202 cpi->total_ssimg_all += frame_all;
5210 if (cpi->common.frame_type != 0 && cpi->common.base_qindex == cpi->oxcf.worst_allowed_q)
5212 skiptruecount += cpi->skip_true_count;
5213 skipfalsecount += cpi->skip_false_count;
5221 FILE *f = fopen("skip.stt", "a");
5222 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);
5224 if (cpi->is_src_frame_alt_ref == 1)
5225 fprintf(f, "skipcount: %4d framesize: %d\n", cpi->skip_true_count , *size);
5234 #if CONFIG_RUNTIME_CPU_DETECT
5235 if (cm->rtcd.flags & HAS_NEON)
5238 vp8_pop_neon(store_reg);
5245 int vp8_get_preview_raw_frame(VP8_PTR comp, YV12_BUFFER_CONFIG *dest, vp8_ppflags_t *flags)
5247 VP8_COMP *cpi = (VP8_COMP *) comp;
5249 if (cpi->common.refresh_alt_ref_frame)
5255 ret = vp8_post_proc_frame(&cpi->common, dest, flags);
5258 if (cpi->common.frame_to_show)
5260 *dest = *cpi->common.frame_to_show;
5261 dest->y_width = cpi->common.Width;
5262 dest->y_height = cpi->common.Height;
5263 dest->uv_height = cpi->common.Height / 2;
5271 #endif //!CONFIG_POSTPROC
5272 vp8_clear_system_state();
5277 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])
5279 VP8_COMP *cpi = (VP8_COMP *) comp;
5280 signed char feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS];
5282 if (cpi->common.mb_rows != rows || cpi->common.mb_cols != cols)
5287 disable_segmentation((VP8_PTR)cpi);
5291 // Set the segmentation Map
5292 set_segmentation_map((VP8_PTR)cpi, map);
5294 // Activate segmentation.
5295 enable_segmentation((VP8_PTR)cpi);
5297 // Set up the quant segment data
5298 feature_data[MB_LVL_ALT_Q][0] = delta_q[0];
5299 feature_data[MB_LVL_ALT_Q][1] = delta_q[1];
5300 feature_data[MB_LVL_ALT_Q][2] = delta_q[2];
5301 feature_data[MB_LVL_ALT_Q][3] = delta_q[3];
5303 // Set up the loop segment data s
5304 feature_data[MB_LVL_ALT_LF][0] = delta_lf[0];
5305 feature_data[MB_LVL_ALT_LF][1] = delta_lf[1];
5306 feature_data[MB_LVL_ALT_LF][2] = delta_lf[2];
5307 feature_data[MB_LVL_ALT_LF][3] = delta_lf[3];
5309 cpi->segment_encode_breakout[0] = threshold[0];
5310 cpi->segment_encode_breakout[1] = threshold[1];
5311 cpi->segment_encode_breakout[2] = threshold[2];
5312 cpi->segment_encode_breakout[3] = threshold[3];
5314 // Initialise the feature data structure
5315 // SEGMENT_DELTADATA 0, SEGMENT_ABSDATA 1
5316 set_segment_data((VP8_PTR)cpi, &feature_data[0][0], SEGMENT_DELTADATA);
5321 int vp8_set_active_map(VP8_PTR comp, unsigned char *map, unsigned int rows, unsigned int cols)
5323 VP8_COMP *cpi = (VP8_COMP *) comp;
5325 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols)
5329 vpx_memcpy(cpi->active_map, map, rows * cols);
5330 cpi->active_map_enabled = 1;
5333 cpi->active_map_enabled = 0;
5339 //cpi->active_map_enabled = 0;
5344 int vp8_set_internal_size(VP8_PTR comp, VPX_SCALING horiz_mode, VPX_SCALING vert_mode)
5346 VP8_COMP *cpi = (VP8_COMP *) comp;
5348 if (horiz_mode <= ONETWO)
5349 cpi->common.horiz_scale = horiz_mode;
5353 if (vert_mode <= ONETWO)
5354 cpi->common.vert_scale = vert_mode;
5363 int vp8_calc_ss_err(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *dest, const vp8_variance_rtcd_vtable_t *rtcd)
5368 unsigned char *src = source->y_buffer;
5369 unsigned char *dst = dest->y_buffer;
5372 // Loop through the Y plane raw and reconstruction data summing (square differences)
5373 for (i = 0; i < source->y_height; i += 16)
5375 for (j = 0; j < source->y_width; j += 16)
5378 Total += VARIANCE_INVOKE(rtcd, mse16x16)(src + j, source->y_stride, dst + j, dest->y_stride, &sse);
5381 src += 16 * source->y_stride;
5382 dst += 16 * dest->y_stride;
5387 int vp8_calc_low_ss_err(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *dest, const vp8_variance_rtcd_vtable_t *rtcd)
5392 unsigned char *src = source->y_buffer;
5393 unsigned char *dst = dest->y_buffer;
5396 // Loop through the Y plane raw and reconstruction data summing (square differences)
5397 for (i = 0; i < source->y_height; i += 16)
5399 for (j = 0; j < source->y_width; j += 16)
5402 VARIANCE_INVOKE(rtcd, mse16x16)(src + j, source->y_stride, dst + j, dest->y_stride, &sse);
5408 src += 16 * source->y_stride;
5409 dst += 16 * dest->y_stride;
5415 int vp8_get_speed(VP8_PTR c)
5417 VP8_COMP *cpi = (VP8_COMP *) c;
5420 int vp8_get_quantizer(VP8_PTR c)
5422 VP8_COMP *cpi = (VP8_COMP *) c;
5423 return cpi->common.base_qindex;