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 "onyxc_int.h"
14 #include "systemdependent.h"
16 #include "alloccommon.h"
18 #include "firstpass.h"
20 #include "vpx_scale/vpxscale.h"
23 #include "quant_common.h"
24 #include "segmentation.h"
26 #include "vpx_scale/yv12extend.h"
28 #include "vpx_mem/vpx_mem.h"
29 #include "swapyv12buffer.h"
30 #include "threading.h"
31 #include "vpx_ports/vpx_timer.h"
32 #include "vpxerrors.h"
33 #include "temporal_filter.h"
35 #include "vpx_ports/arm.h"
42 #if CONFIG_RUNTIME_CPU_DETECT
43 #define IF_RTCD(x) (x)
44 #define RTCD(x) &cpi->common.rtcd.x
46 #define IF_RTCD(x) NULL
50 extern void vp8cx_init_mv_bits_sadcost();
51 extern void vp8cx_pick_filter_level_fast(YV12_BUFFER_CONFIG *sd, VP8_COMP *cpi);
52 extern void vp8cx_set_alt_lf_level(VP8_COMP *cpi, int filt_val);
53 extern void vp8cx_pick_filter_level(YV12_BUFFER_CONFIG *sd, VP8_COMP *cpi);
55 extern void vp8_init_loop_filter(VP8_COMMON *cm);
56 extern void vp8_loop_filter_frame(VP8_COMMON *cm, MACROBLOCKD *mbd, int filt_val);
57 extern void vp8_loop_filter_frame_yonly(VP8_COMMON *cm, MACROBLOCKD *mbd, int filt_val, int sharpness_lvl);
58 extern void vp8_dmachine_specific_config(VP8_COMP *cpi);
59 extern void vp8_cmachine_specific_config(VP8_COMP *cpi);
60 extern void vp8_calc_auto_iframe_target_size(VP8_COMP *cpi);
61 extern void vp8_deblock_frame(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *post, int filt_lvl, int low_var_thresh, int flag);
62 extern void print_parms(VP8_CONFIG *ocf, char *filenam);
63 extern unsigned int vp8_get_processor_freq();
64 extern void print_tree_update_probs();
65 extern void vp8cx_create_encoder_threads(VP8_COMP *cpi);
66 extern void vp8cx_remove_encoder_threads(VP8_COMP *cpi);
68 extern void vp8_yv12_copy_frame_func_neon(YV12_BUFFER_CONFIG *src_ybc, YV12_BUFFER_CONFIG *dst_ybc);
69 extern void vp8_yv12_copy_src_frame_func_neon(YV12_BUFFER_CONFIG *src_ybc, YV12_BUFFER_CONFIG *dst_ybc);
72 int vp8_estimate_entropy_savings(VP8_COMP *cpi);
73 int vp8_calc_ss_err(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *dest, const vp8_variance_rtcd_vtable_t *rtcd);
74 int vp8_calc_low_ss_err(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *dest, const vp8_variance_rtcd_vtable_t *rtcd);
76 extern void vp8_temporal_filter_prepare_c(VP8_COMP *cpi);
78 static void set_default_lf_deltas(VP8_COMP *cpi);
80 extern const int vp8_gf_interval_table[101];
85 extern double vp8_calc_ssim
87 YV12_BUFFER_CONFIG *source,
88 YV12_BUFFER_CONFIG *dest,
93 extern double vp8_calc_ssimg
95 YV12_BUFFER_CONFIG *source,
96 YV12_BUFFER_CONFIG *dest,
106 #ifdef OUTPUT_YUV_SRC
117 extern int skip_true_count;
118 extern int skip_false_count;
123 extern int intra_mode_stats[10][10][10];
127 unsigned int frames_at_speed[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
128 unsigned int tot_pm = 0;
129 unsigned int cnt_pm = 0;
130 unsigned int tot_ef = 0;
131 unsigned int cnt_ef = 0;
135 extern unsigned __int64 Sectionbits[50];
136 extern int y_modes[5] ;
137 extern int uv_modes[4] ;
138 extern int b_modes[10] ;
140 extern int inter_y_modes[10] ;
141 extern int inter_uv_modes[4] ;
142 extern unsigned int inter_b_modes[15];
145 extern void (*vp8_short_fdct4x4)(short *input, short *output, int pitch);
146 extern void (*vp8_short_fdct8x4)(short *input, short *output, int pitch);
148 extern const int vp8_bits_per_mb[2][QINDEX_RANGE];
150 extern const int qrounding_factors[129];
151 extern const int qzbin_factors[129];
152 extern void vp8cx_init_quantizer(VP8_COMP *cpi);
153 extern const int vp8cx_base_skip_false_prob[128];
155 // Tables relating active max Q to active min Q
156 static const int kf_low_motion_minq[QINDEX_RANGE] =
158 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
159 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
160 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
161 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4,
162 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 10,10,
163 11,11,12,12,13,13,14,14,15,15,16,16,17,17,18,18,
164 19,19,20,20,21,21,22,22,23,23,24,24,25,25,26,26,
165 27,27,28,28,29,29,30,30,31,32,33,34,35,36,37,38,
167 static const int kf_high_motion_minq[QINDEX_RANGE] =
169 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
170 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
171 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5,
172 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10,10,
173 11,11,12,12,13,13,14,14,15,15,16,16,17,17,18,18,
174 19,19,20,20,21,21,22,22,23,23,24,24,25,25,26,26,
175 27,27,28,28,29,29,30,30,31,31,32,32,33,33,34,34,
176 35,35,36,36,37,38,39,40,41,42,43,44,45,46,47,48,
178 static const int gf_low_motion_minq[QINDEX_RANGE] =
180 0,0,0,0,1,1,1,1,1,1,1,1,2,2,2,2,
181 3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,
182 7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10,
183 11,11,12,12,13,13,14,14,15,15,16,16,17,17,18,18,
184 19,19,20,20,21,21,22,22,23,23,24,24,25,25,26,26,
185 27,27,28,28,29,29,30,30,31,31,32,32,33,33,34,34,
186 35,35,36,36,37,37,38,38,39,39,40,40,41,41,42,42,
187 43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58
189 static const int gf_mid_motion_minq[QINDEX_RANGE] =
191 0,0,0,0,1,1,1,1,1,1,2,2,3,3,3,4,
192 4,4,5,5,5,6,6,6,7,7,7,8,8,8,9,9,
193 9,10,10,10,10,11,11,11,12,12,12,12,13,13,13,14,
194 14,14,15,15,16,16,17,17,18,18,19,19,20,20,21,21,
195 22,22,23,23,24,24,25,25,26,26,27,27,28,28,29,29,
196 30,30,31,31,32,32,33,33,34,34,35,35,36,36,37,37,
197 38,39,39,40,40,41,41,42,42,43,43,44,45,46,47,48,
198 49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,
200 static const int gf_high_motion_minq[QINDEX_RANGE] =
202 0,0,0,0,1,1,1,1,1,2,2,2,3,3,3,4,
203 4,4,5,5,5,6,6,6,7,7,7,8,8,8,9,9,
204 9,10,10,10,11,11,12,12,13,13,14,14,15,15,16,16,
205 17,17,18,18,19,19,20,20,21,21,22,22,23,23,24,24,
206 25,25,26,26,27,27,28,28,29,29,30,30,31,31,32,32,
207 33,33,34,34,35,35,36,36,37,37,38,38,39,39,40,40,
208 41,41,42,42,43,44,45,46,47,48,49,50,51,52,53,54,
209 55,56,57,58,59,60,62,64,66,68,70,72,74,76,78,80,
211 static const int inter_minq[QINDEX_RANGE] =
213 0,0,1,1,2,3,3,4,4,5,6,6,7,8,8,9,
214 9,10,11,11,12,13,13,14,15,15,16,17,17,18,19,20,
215 20,21,22,22,23,24,24,25,26,27,27,28,29,30,30,31,
216 32,33,33,34,35,36,36,37,38,39,39,40,41,42,42,43,
217 44,45,46,46,47,48,49,50,50,51,52,53,54,55,55,56,
218 57,58,59,60,60,61,62,63,64,65,66,67,67,68,69,70,
219 71,72,73,74,75,75,76,77,78,79,80,81,82,83,84,85,
220 86,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100
223 void vp8_initialize()
225 static int init_done = 0;
229 vp8_scale_machine_specific_config();
230 vp8_initialize_common();
231 //vp8_dmachine_specific_config();
232 vp8_tokenize_initialize();
234 vp8cx_init_mv_bits_sadcost();
238 #ifdef PACKET_TESTING
239 extern FILE *vpxlogc;
242 static void setup_features(VP8_COMP *cpi)
244 // Set up default state for MB feature flags
245 cpi->mb.e_mbd.segmentation_enabled = 0;
246 cpi->mb.e_mbd.update_mb_segmentation_map = 0;
247 cpi->mb.e_mbd.update_mb_segmentation_data = 0;
248 vpx_memset(cpi->mb.e_mbd.mb_segment_tree_probs, 255, sizeof(cpi->mb.e_mbd.mb_segment_tree_probs));
249 vpx_memset(cpi->mb.e_mbd.segment_feature_data, 0, sizeof(cpi->mb.e_mbd.segment_feature_data));
251 cpi->mb.e_mbd.mode_ref_lf_delta_enabled = 0;
252 cpi->mb.e_mbd.mode_ref_lf_delta_update = 0;
253 vpx_memset(cpi->mb.e_mbd.ref_lf_deltas, 0, sizeof(cpi->mb.e_mbd.ref_lf_deltas));
254 vpx_memset(cpi->mb.e_mbd.mode_lf_deltas, 0, sizeof(cpi->mb.e_mbd.mode_lf_deltas));
255 vpx_memset(cpi->mb.e_mbd.last_ref_lf_deltas, 0, sizeof(cpi->mb.e_mbd.ref_lf_deltas));
256 vpx_memset(cpi->mb.e_mbd.last_mode_lf_deltas, 0, sizeof(cpi->mb.e_mbd.mode_lf_deltas));
258 set_default_lf_deltas(cpi);
263 void vp8_dealloc_compressor_data(VP8_COMP *cpi)
265 // Delete last frame MV storage buffers
271 if (cpi->lf_ref_frame_sign_bias != 0)
272 vpx_free(cpi->lf_ref_frame_sign_bias);
274 cpi->lf_ref_frame_sign_bias = 0;
276 if (cpi->lf_ref_frame != 0)
277 vpx_free(cpi->lf_ref_frame);
279 cpi->lf_ref_frame = 0;
281 // Delete sementation map
282 if (cpi->segmentation_map != 0)
283 vpx_free(cpi->segmentation_map);
285 cpi->segmentation_map = 0;
287 if (cpi->active_map != 0)
288 vpx_free(cpi->active_map);
292 // Delete first pass motion map
293 if (cpi->fp_motion_map != 0)
294 vpx_free(cpi->fp_motion_map);
296 cpi->fp_motion_map = 0;
298 vp8_de_alloc_frame_buffers(&cpi->common);
300 vp8_yv12_de_alloc_frame_buffer(&cpi->last_frame_uf);
301 vp8_yv12_de_alloc_frame_buffer(&cpi->scaled_source);
302 #if VP8_TEMPORAL_ALT_REF
303 vp8_yv12_de_alloc_frame_buffer(&cpi->alt_ref_buffer.source_buffer);
308 for (i = 0; i < MAX_LAG_BUFFERS; i++)
309 vp8_yv12_de_alloc_frame_buffer(&cpi->src_buffer[i].source_buffer);
311 cpi->source_buffer_count = 0;
317 // Structure used to minitor GF useage
318 if (cpi->gf_active_flags != 0)
319 vpx_free(cpi->gf_active_flags);
321 cpi->gf_active_flags = 0;
324 vpx_free(cpi->mb.pip);
329 vpx_free(cpi->total_stats);
331 cpi->total_stats = 0;
333 if(cpi->this_frame_stats)
334 vpx_free(cpi->this_frame_stats);
336 cpi->this_frame_stats = 0;
339 static void enable_segmentation(VP8_PTR ptr)
341 VP8_COMP *cpi = (VP8_COMP *)(ptr);
343 // Set the appropriate feature bit
344 cpi->mb.e_mbd.segmentation_enabled = 1;
345 cpi->mb.e_mbd.update_mb_segmentation_map = 1;
346 cpi->mb.e_mbd.update_mb_segmentation_data = 1;
348 static void disable_segmentation(VP8_PTR ptr)
350 VP8_COMP *cpi = (VP8_COMP *)(ptr);
352 // Clear the appropriate feature bit
353 cpi->mb.e_mbd.segmentation_enabled = 0;
356 // Valid values for a segment are 0 to 3
357 // Segmentation map is arrange as [Rows][Columns]
358 static void set_segmentation_map(VP8_PTR ptr, unsigned char *segmentation_map)
360 VP8_COMP *cpi = (VP8_COMP *)(ptr);
362 // Copy in the new segmentation map
363 vpx_memcpy(cpi->segmentation_map, segmentation_map, (cpi->common.mb_rows * cpi->common.mb_cols));
365 // Signal that the map should be updated.
366 cpi->mb.e_mbd.update_mb_segmentation_map = 1;
367 cpi->mb.e_mbd.update_mb_segmentation_data = 1;
370 // The values given for each segment can be either deltas (from the default value chosen for the frame) or absolute values.
372 // Valid range for abs values is (0-127 for MB_LVL_ALT_Q) , (0-63 for SEGMENT_ALT_LF)
373 // Valid range for delta values are (+/-127 for MB_LVL_ALT_Q) , (+/-63 for SEGMENT_ALT_LF)
375 // abs_delta = SEGMENT_DELTADATA (deltas) abs_delta = SEGMENT_ABSDATA (use the absolute values given).
378 static void set_segment_data(VP8_PTR ptr, signed char *feature_data, unsigned char abs_delta)
380 VP8_COMP *cpi = (VP8_COMP *)(ptr);
382 cpi->mb.e_mbd.mb_segement_abs_delta = abs_delta;
383 vpx_memcpy(cpi->segment_feature_data, feature_data, sizeof(cpi->segment_feature_data));
387 static void segmentation_test_function(VP8_PTR ptr)
389 VP8_COMP *cpi = (VP8_COMP *)(ptr);
391 unsigned char *seg_map;
392 signed char feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS];
394 // Create a temporary map for segmentation data.
395 CHECK_MEM_ERROR(seg_map, vpx_calloc(cpi->common.mb_rows * cpi->common.mb_cols, 1));
397 // MB loop to set local segmentation map
398 /*for ( i = 0; i < cpi->common.mb_rows; i++ )
400 for ( j = 0; j < cpi->common.mb_cols; j++ )
402 //seg_map[(i*cpi->common.mb_cols) + j] = (j % 2) + ((i%2)* 2);
403 //if ( j < cpi->common.mb_cols/2 )
405 // Segment 1 around the edge else 0
406 if ( (i == 0) || (j == 0) || (i == (cpi->common.mb_rows-1)) || (j == (cpi->common.mb_cols-1)) )
407 seg_map[(i*cpi->common.mb_cols) + j] = 1;
408 //else if ( (i < 2) || (j < 2) || (i > (cpi->common.mb_rows-3)) || (j > (cpi->common.mb_cols-3)) )
409 // seg_map[(i*cpi->common.mb_cols) + j] = 2;
410 //else if ( (i < 5) || (j < 5) || (i > (cpi->common.mb_rows-6)) || (j > (cpi->common.mb_cols-6)) )
411 // seg_map[(i*cpi->common.mb_cols) + j] = 3;
413 seg_map[(i*cpi->common.mb_cols) + j] = 0;
417 // Set the segmentation Map
418 set_segmentation_map(ptr, seg_map);
420 // Activate segmentation.
421 enable_segmentation(ptr);
423 // Set up the quant segment data
424 feature_data[MB_LVL_ALT_Q][0] = 0;
425 feature_data[MB_LVL_ALT_Q][1] = 4;
426 feature_data[MB_LVL_ALT_Q][2] = 0;
427 feature_data[MB_LVL_ALT_Q][3] = 0;
428 // Set up the loop segment data
429 feature_data[MB_LVL_ALT_LF][0] = 0;
430 feature_data[MB_LVL_ALT_LF][1] = 0;
431 feature_data[MB_LVL_ALT_LF][2] = 0;
432 feature_data[MB_LVL_ALT_LF][3] = 0;
434 // Initialise the feature data structure
435 // SEGMENT_DELTADATA 0, SEGMENT_ABSDATA 1
436 set_segment_data(ptr, &feature_data[0][0], SEGMENT_DELTADATA);
438 // Delete sementation map
446 // A simple function to cyclically refresh the background at a lower Q
447 static void cyclic_background_refresh(VP8_COMP *cpi, int Q, int lf_adjustment)
449 unsigned char *seg_map;
450 signed char feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS];
452 int block_count = cpi->cyclic_refresh_mode_max_mbs_perframe;
453 int mbs_in_frame = cpi->common.mb_rows * cpi->common.mb_cols;
455 // Create a temporary map for segmentation data.
456 CHECK_MEM_ERROR(seg_map, vpx_calloc(cpi->common.mb_rows * cpi->common.mb_cols, 1));
458 cpi->cyclic_refresh_q = Q;
460 for (i = Q; i > 0; i--)
462 if (vp8_bits_per_mb[cpi->common.frame_type][i] >= ((vp8_bits_per_mb[cpi->common.frame_type][Q]*(Q + 128)) / 64))
463 //if ( vp8_bits_per_mb[cpi->common.frame_type][i] >= ((vp8_bits_per_mb[cpi->common.frame_type][Q]*((2*Q)+96))/64) )
469 cpi->cyclic_refresh_q = i;
471 // Only update for inter frames
472 if (cpi->common.frame_type != KEY_FRAME)
474 // Cycle through the macro_block rows
475 // MB loop to set local segmentation map
476 for (i = cpi->cyclic_refresh_mode_index; i < mbs_in_frame; i++)
478 // If the MB is as a candidate for clean up then mark it for possible boost/refresh (segment 1)
479 // The segment id may get reset to 0 later if the MB gets coded anything other than last frame 0,0
480 // as only (last frame 0,0) MBs are eligable for refresh : that is to say Mbs likely to be background blocks.
481 if (cpi->cyclic_refresh_map[i] == 0)
489 // Skip blocks that have been refreshed recently anyway.
490 if (cpi->cyclic_refresh_map[i] < 0)
491 //cpi->cyclic_refresh_map[i] = cpi->cyclic_refresh_map[i] / 16;
492 cpi->cyclic_refresh_map[i]++;
503 // If we have gone through the frame reset to the start
504 cpi->cyclic_refresh_mode_index = i;
506 if (cpi->cyclic_refresh_mode_index >= mbs_in_frame)
507 cpi->cyclic_refresh_mode_index = 0;
510 // Set the segmentation Map
511 set_segmentation_map((VP8_PTR)cpi, seg_map);
513 // Activate segmentation.
514 enable_segmentation((VP8_PTR)cpi);
516 // Set up the quant segment data
517 feature_data[MB_LVL_ALT_Q][0] = 0;
518 feature_data[MB_LVL_ALT_Q][1] = (cpi->cyclic_refresh_q - Q);
519 feature_data[MB_LVL_ALT_Q][2] = 0;
520 feature_data[MB_LVL_ALT_Q][3] = 0;
522 // Set up the loop segment data
523 feature_data[MB_LVL_ALT_LF][0] = 0;
524 feature_data[MB_LVL_ALT_LF][1] = lf_adjustment;
525 feature_data[MB_LVL_ALT_LF][2] = 0;
526 feature_data[MB_LVL_ALT_LF][3] = 0;
528 // Initialise the feature data structure
529 // SEGMENT_DELTADATA 0, SEGMENT_ABSDATA 1
530 set_segment_data((VP8_PTR)cpi, &feature_data[0][0], SEGMENT_DELTADATA);
532 // Delete sementation map
540 static void set_default_lf_deltas(VP8_COMP *cpi)
542 cpi->mb.e_mbd.mode_ref_lf_delta_enabled = 1;
543 cpi->mb.e_mbd.mode_ref_lf_delta_update = 1;
545 vpx_memset(cpi->mb.e_mbd.ref_lf_deltas, 0, sizeof(cpi->mb.e_mbd.ref_lf_deltas));
546 vpx_memset(cpi->mb.e_mbd.mode_lf_deltas, 0, sizeof(cpi->mb.e_mbd.mode_lf_deltas));
548 // Test of ref frame deltas
549 cpi->mb.e_mbd.ref_lf_deltas[INTRA_FRAME] = 2;
550 cpi->mb.e_mbd.ref_lf_deltas[LAST_FRAME] = 0;
551 cpi->mb.e_mbd.ref_lf_deltas[GOLDEN_FRAME] = -2;
552 cpi->mb.e_mbd.ref_lf_deltas[ALTREF_FRAME] = -2;
554 cpi->mb.e_mbd.mode_lf_deltas[0] = 4; // BPRED
555 cpi->mb.e_mbd.mode_lf_deltas[1] = -2; // Zero
556 cpi->mb.e_mbd.mode_lf_deltas[2] = 2; // New mv
557 cpi->mb.e_mbd.mode_lf_deltas[3] = 4; // Split mv
560 void vp8_set_speed_features(VP8_COMP *cpi)
562 SPEED_FEATURES *sf = &cpi->sf;
563 int Mode = cpi->compressor_speed;
564 int Speed = cpi->Speed;
566 VP8_COMMON *cm = &cpi->common;
567 int last_improved_quant = sf->improved_quant;
569 // Initialise default mode frequency sampling variables
570 for (i = 0; i < MAX_MODES; i ++)
572 cpi->mode_check_freq[i] = 0;
573 cpi->mode_test_hit_counts[i] = 0;
574 cpi->mode_chosen_counts[i] = 0;
577 cpi->mbs_tested_so_far = 0;
581 sf->search_method = NSTEP;
582 sf->improved_quant = 1;
583 sf->improved_dct = 1;
586 sf->quarter_pixel_search = 1;
587 sf->half_pixel_search = 1;
588 sf->full_freq[0] = 7;
589 sf->full_freq[1] = 7;
590 sf->min_fs_radius = 8;
591 sf->max_fs_radius = 32;
592 sf->iterative_sub_pixel = 1;
593 sf->optimize_coefficients = 1;
594 sf->use_fastquant_for_pick = 0;
597 sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
602 // default thresholds to 0
603 for (i = 0; i < MAX_MODES; i++)
604 sf->thresh_mult[i] = 0;
608 #if !(CONFIG_REALTIME_ONLY)
609 case 0: // best quality mode
610 sf->thresh_mult[THR_ZEROMV ] = 0;
611 sf->thresh_mult[THR_ZEROG ] = 0;
612 sf->thresh_mult[THR_ZEROA ] = 0;
613 sf->thresh_mult[THR_NEARESTMV] = 0;
614 sf->thresh_mult[THR_NEARESTG ] = 0;
615 sf->thresh_mult[THR_NEARESTA ] = 0;
616 sf->thresh_mult[THR_NEARMV ] = 0;
617 sf->thresh_mult[THR_NEARG ] = 0;
618 sf->thresh_mult[THR_NEARA ] = 0;
620 sf->thresh_mult[THR_DC ] = 0;
622 sf->thresh_mult[THR_V_PRED ] = 1000;
623 sf->thresh_mult[THR_H_PRED ] = 1000;
624 sf->thresh_mult[THR_B_PRED ] = 2000;
625 sf->thresh_mult[THR_TM ] = 1000;
627 sf->thresh_mult[THR_NEWMV ] = 1000;
628 sf->thresh_mult[THR_NEWG ] = 1000;
629 sf->thresh_mult[THR_NEWA ] = 1000;
631 sf->thresh_mult[THR_SPLITMV ] = 2500;
632 sf->thresh_mult[THR_SPLITG ] = 5000;
633 sf->thresh_mult[THR_SPLITA ] = 5000;
635 sf->full_freq[0] = 7;
636 sf->full_freq[1] = 15;
639 sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
641 if (!(cpi->ref_frame_flags & VP8_LAST_FLAG))
643 sf->thresh_mult[THR_NEWMV ] = INT_MAX;
644 sf->thresh_mult[THR_NEARESTMV] = INT_MAX;
645 sf->thresh_mult[THR_ZEROMV ] = INT_MAX;
646 sf->thresh_mult[THR_NEARMV ] = INT_MAX;
647 sf->thresh_mult[THR_SPLITMV ] = INT_MAX;
650 if (!(cpi->ref_frame_flags & VP8_GOLD_FLAG))
652 sf->thresh_mult[THR_NEARESTG ] = INT_MAX;
653 sf->thresh_mult[THR_ZEROG ] = INT_MAX;
654 sf->thresh_mult[THR_NEARG ] = INT_MAX;
655 sf->thresh_mult[THR_NEWG ] = INT_MAX;
656 sf->thresh_mult[THR_SPLITG ] = INT_MAX;
659 if (!(cpi->ref_frame_flags & VP8_ALT_FLAG))
661 sf->thresh_mult[THR_NEARESTA ] = INT_MAX;
662 sf->thresh_mult[THR_ZEROA ] = INT_MAX;
663 sf->thresh_mult[THR_NEARA ] = INT_MAX;
664 sf->thresh_mult[THR_NEWA ] = INT_MAX;
665 sf->thresh_mult[THR_SPLITA ] = INT_MAX;
671 sf->thresh_mult[THR_NEARESTMV] = 0;
672 sf->thresh_mult[THR_ZEROMV ] = 0;
673 sf->thresh_mult[THR_DC ] = 0;
674 sf->thresh_mult[THR_NEARMV ] = 0;
675 sf->thresh_mult[THR_V_PRED ] = 1000;
676 sf->thresh_mult[THR_H_PRED ] = 1000;
677 sf->thresh_mult[THR_B_PRED ] = 2500;
678 sf->thresh_mult[THR_TM ] = 1000;
680 sf->thresh_mult[THR_NEARESTG ] = 1000;
681 sf->thresh_mult[THR_NEARESTA ] = 1000;
683 sf->thresh_mult[THR_ZEROG ] = 1000;
684 sf->thresh_mult[THR_ZEROA ] = 1000;
685 sf->thresh_mult[THR_NEARG ] = 1000;
686 sf->thresh_mult[THR_NEARA ] = 1000;
689 sf->thresh_mult[THR_ZEROMV ] = 0;
690 sf->thresh_mult[THR_ZEROG ] = 0;
691 sf->thresh_mult[THR_ZEROA ] = 0;
692 sf->thresh_mult[THR_NEARESTMV] = 0;
693 sf->thresh_mult[THR_NEARESTG ] = 0;
694 sf->thresh_mult[THR_NEARESTA ] = 0;
695 sf->thresh_mult[THR_NEARMV ] = 0;
696 sf->thresh_mult[THR_NEARG ] = 0;
697 sf->thresh_mult[THR_NEARA ] = 0;
699 // sf->thresh_mult[THR_DC ] = 0;
701 // sf->thresh_mult[THR_V_PRED ] = 1000;
702 // sf->thresh_mult[THR_H_PRED ] = 1000;
703 // sf->thresh_mult[THR_B_PRED ] = 2000;
704 // sf->thresh_mult[THR_TM ] = 1000;
706 sf->thresh_mult[THR_NEWMV ] = 1000;
707 sf->thresh_mult[THR_NEWG ] = 1000;
708 sf->thresh_mult[THR_NEWA ] = 1000;
710 sf->thresh_mult[THR_SPLITMV ] = 1700;
711 sf->thresh_mult[THR_SPLITG ] = 4500;
712 sf->thresh_mult[THR_SPLITA ] = 4500;
714 sf->thresh_mult[THR_NEWMV ] = 1500;
715 sf->thresh_mult[THR_NEWG ] = 1500;
716 sf->thresh_mult[THR_NEWA ] = 1500;
718 sf->thresh_mult[THR_SPLITMV ] = 5000;
719 sf->thresh_mult[THR_SPLITG ] = 10000;
720 sf->thresh_mult[THR_SPLITA ] = 10000;
722 sf->full_freq[0] = 15;
723 sf->full_freq[1] = 31;
726 sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
728 if (!(cpi->ref_frame_flags & VP8_LAST_FLAG))
730 sf->thresh_mult[THR_NEWMV ] = INT_MAX;
731 sf->thresh_mult[THR_NEARESTMV] = INT_MAX;
732 sf->thresh_mult[THR_ZEROMV ] = INT_MAX;
733 sf->thresh_mult[THR_NEARMV ] = INT_MAX;
734 sf->thresh_mult[THR_SPLITMV ] = INT_MAX;
737 if (!(cpi->ref_frame_flags & VP8_GOLD_FLAG))
739 sf->thresh_mult[THR_NEARESTG ] = INT_MAX;
740 sf->thresh_mult[THR_ZEROG ] = INT_MAX;
741 sf->thresh_mult[THR_NEARG ] = INT_MAX;
742 sf->thresh_mult[THR_NEWG ] = INT_MAX;
743 sf->thresh_mult[THR_SPLITG ] = INT_MAX;
746 if (!(cpi->ref_frame_flags & VP8_ALT_FLAG))
748 sf->thresh_mult[THR_NEARESTA ] = INT_MAX;
749 sf->thresh_mult[THR_ZEROA ] = INT_MAX;
750 sf->thresh_mult[THR_NEARA ] = INT_MAX;
751 sf->thresh_mult[THR_NEWA ] = INT_MAX;
752 sf->thresh_mult[THR_SPLITA ] = INT_MAX;
757 // Disable coefficient optimization above speed 0
758 sf->optimize_coefficients = 0;
760 cpi->mode_check_freq[THR_SPLITG] = 4;
761 cpi->mode_check_freq[THR_SPLITA] = 4;
762 cpi->mode_check_freq[THR_SPLITMV] = 2;
764 sf->thresh_mult[THR_TM ] = 1500;
765 sf->thresh_mult[THR_V_PRED ] = 1500;
766 sf->thresh_mult[THR_H_PRED ] = 1500;
767 sf->thresh_mult[THR_B_PRED ] = 5000;
769 if (cpi->ref_frame_flags & VP8_LAST_FLAG)
771 sf->thresh_mult[THR_NEWMV ] = 2000;
772 sf->thresh_mult[THR_SPLITMV ] = 10000;
775 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
777 sf->thresh_mult[THR_NEARESTG ] = 1500;
778 sf->thresh_mult[THR_ZEROG ] = 1500;
779 sf->thresh_mult[THR_NEARG ] = 1500;
780 sf->thresh_mult[THR_NEWG ] = 2000;
781 sf->thresh_mult[THR_SPLITG ] = 20000;
784 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
786 sf->thresh_mult[THR_NEARESTA ] = 1500;
787 sf->thresh_mult[THR_ZEROA ] = 1500;
788 sf->thresh_mult[THR_NEARA ] = 1500;
789 sf->thresh_mult[THR_NEWA ] = 2000;
790 sf->thresh_mult[THR_SPLITA ] = 20000;
793 sf->use_fastquant_for_pick = 1;
796 sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
801 sf->use_fastquant_for_pick = 0;
803 cpi->mode_check_freq[THR_SPLITG] = 15;
804 cpi->mode_check_freq[THR_SPLITA] = 15;
805 cpi->mode_check_freq[THR_SPLITMV] = 7;
807 sf->thresh_mult[THR_TM ] = 2000;
808 sf->thresh_mult[THR_V_PRED ] = 2000;
809 sf->thresh_mult[THR_H_PRED ] = 2000;
810 sf->thresh_mult[THR_B_PRED ] = 7500;
812 if (cpi->ref_frame_flags & VP8_LAST_FLAG)
814 sf->thresh_mult[THR_NEWMV ] = 2000;
815 sf->thresh_mult[THR_SPLITMV ] = 25000;
818 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
820 sf->thresh_mult[THR_NEARESTG ] = 2000;
821 sf->thresh_mult[THR_ZEROG ] = 2000;
822 sf->thresh_mult[THR_NEARG ] = 2000;
823 sf->thresh_mult[THR_NEWG ] = 2500;
824 sf->thresh_mult[THR_SPLITG ] = 50000;
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 ] = 2500;
833 sf->thresh_mult[THR_SPLITA ] = 50000;
838 sf->improved_quant = 0;
839 sf->improved_dct = 0;
841 // Only do recode loop on key frames, golden frames and
845 sf->full_freq[0] = 31;
846 sf->full_freq[1] = 63;
852 sf->auto_filter = 0; // Faster selection of loop filter
853 cpi->mode_check_freq[THR_V_PRED] = 2;
854 cpi->mode_check_freq[THR_H_PRED] = 2;
855 cpi->mode_check_freq[THR_B_PRED] = 2;
857 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
859 cpi->mode_check_freq[THR_NEARG] = 2;
860 cpi->mode_check_freq[THR_NEWG] = 4;
863 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
865 cpi->mode_check_freq[THR_NEARA] = 2;
866 cpi->mode_check_freq[THR_NEWA] = 4;
869 sf->thresh_mult[THR_SPLITA ] = INT_MAX;
870 sf->thresh_mult[THR_SPLITG ] = INT_MAX;
871 sf->thresh_mult[THR_SPLITMV ] = INT_MAX;
873 sf->full_freq[0] = 63;
874 sf->full_freq[1] = 127;
879 cpi->mode_check_freq[THR_V_PRED] = 0;
880 cpi->mode_check_freq[THR_H_PRED] = 0;
881 cpi->mode_check_freq[THR_B_PRED] = 0;
882 cpi->mode_check_freq[THR_NEARG] = 0;
883 cpi->mode_check_freq[THR_NEWG] = 0;
884 cpi->mode_check_freq[THR_NEARA] = 0;
885 cpi->mode_check_freq[THR_NEWA] = 0;
888 sf->recode_loop = 0; // recode loop off
889 sf->RD = 0; // Turn rd off
890 sf->full_freq[0] = INT_MAX;
891 sf->full_freq[1] = INT_MAX;
896 sf->auto_filter = 0; // Faster selection of loop filter
898 cpi->mode_check_freq[THR_V_PRED] = 2;
899 cpi->mode_check_freq[THR_H_PRED] = 2;
900 cpi->mode_check_freq[THR_B_PRED] = 2;
902 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
904 cpi->mode_check_freq[THR_NEARG] = 2;
905 cpi->mode_check_freq[THR_NEWG] = 4;
908 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
910 cpi->mode_check_freq[THR_NEARA] = 2;
911 cpi->mode_check_freq[THR_NEWA] = 4;
914 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
916 sf->thresh_mult[THR_NEARESTG ] = 2000;
917 sf->thresh_mult[THR_ZEROG ] = 2000;
918 sf->thresh_mult[THR_NEARG ] = 2000;
919 sf->thresh_mult[THR_NEWG ] = 4000;
922 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
924 sf->thresh_mult[THR_NEARESTA ] = 2000;
925 sf->thresh_mult[THR_ZEROA ] = 2000;
926 sf->thresh_mult[THR_NEARA ] = 2000;
927 sf->thresh_mult[THR_NEWA ] = 4000;
934 sf->optimize_coefficients = 0;
937 sf->iterative_sub_pixel = 1;
938 sf->thresh_mult[THR_NEARESTMV] = 0;
939 sf->thresh_mult[THR_ZEROMV ] = 0;
940 sf->thresh_mult[THR_DC ] = 0;
941 sf->thresh_mult[THR_TM ] = 0;
942 sf->thresh_mult[THR_NEARMV ] = 0;
943 sf->thresh_mult[THR_V_PRED ] = 1000;
944 sf->thresh_mult[THR_H_PRED ] = 1000;
945 sf->thresh_mult[THR_B_PRED ] = 2500;
946 sf->thresh_mult[THR_NEARESTG ] = 1000;
947 sf->thresh_mult[THR_ZEROG ] = 1000;
948 sf->thresh_mult[THR_NEARG ] = 1000;
949 sf->thresh_mult[THR_NEARESTA ] = 1000;
950 sf->thresh_mult[THR_ZEROA ] = 1000;
951 sf->thresh_mult[THR_NEARA ] = 1000;
952 sf->thresh_mult[THR_NEWMV ] = 2000;
953 sf->thresh_mult[THR_NEWG ] = 2000;
954 sf->thresh_mult[THR_NEWA ] = 2000;
955 sf->thresh_mult[THR_SPLITMV ] = 5000;
956 sf->thresh_mult[THR_SPLITG ] = 10000;
957 sf->thresh_mult[THR_SPLITA ] = 10000;
958 sf->full_freq[0] = 15;
959 sf->full_freq[1] = 31;
960 sf->search_method = NSTEP;
962 if (!(cpi->ref_frame_flags & VP8_LAST_FLAG))
964 sf->thresh_mult[THR_NEWMV ] = INT_MAX;
965 sf->thresh_mult[THR_NEARESTMV] = INT_MAX;
966 sf->thresh_mult[THR_ZEROMV ] = INT_MAX;
967 sf->thresh_mult[THR_NEARMV ] = INT_MAX;
968 sf->thresh_mult[THR_SPLITMV ] = INT_MAX;
971 if (!(cpi->ref_frame_flags & VP8_GOLD_FLAG))
973 sf->thresh_mult[THR_NEARESTG ] = INT_MAX;
974 sf->thresh_mult[THR_ZEROG ] = INT_MAX;
975 sf->thresh_mult[THR_NEARG ] = INT_MAX;
976 sf->thresh_mult[THR_NEWG ] = INT_MAX;
977 sf->thresh_mult[THR_SPLITG ] = INT_MAX;
980 if (!(cpi->ref_frame_flags & VP8_ALT_FLAG))
982 sf->thresh_mult[THR_NEARESTA ] = INT_MAX;
983 sf->thresh_mult[THR_ZEROA ] = INT_MAX;
984 sf->thresh_mult[THR_NEARA ] = INT_MAX;
985 sf->thresh_mult[THR_NEWA ] = INT_MAX;
986 sf->thresh_mult[THR_SPLITA ] = INT_MAX;
991 cpi->mode_check_freq[THR_SPLITG] = 4;
992 cpi->mode_check_freq[THR_SPLITA] = 4;
993 cpi->mode_check_freq[THR_SPLITMV] = 2;
995 sf->thresh_mult[THR_DC ] = 0;
996 sf->thresh_mult[THR_TM ] = 1000;
997 sf->thresh_mult[THR_V_PRED ] = 2000;
998 sf->thresh_mult[THR_H_PRED ] = 2000;
999 sf->thresh_mult[THR_B_PRED ] = 5000;
1001 if (cpi->ref_frame_flags & VP8_LAST_FLAG)
1003 sf->thresh_mult[THR_NEARESTMV] = 0;
1004 sf->thresh_mult[THR_ZEROMV ] = 0;
1005 sf->thresh_mult[THR_NEARMV ] = 0;
1006 sf->thresh_mult[THR_NEWMV ] = 2000;
1007 sf->thresh_mult[THR_SPLITMV ] = 10000;
1010 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
1012 sf->thresh_mult[THR_NEARESTG ] = 1000;
1013 sf->thresh_mult[THR_ZEROG ] = 1000;
1014 sf->thresh_mult[THR_NEARG ] = 1000;
1015 sf->thresh_mult[THR_NEWG ] = 2000;
1016 sf->thresh_mult[THR_SPLITG ] = 20000;
1019 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
1021 sf->thresh_mult[THR_NEARESTA ] = 1000;
1022 sf->thresh_mult[THR_ZEROA ] = 1000;
1023 sf->thresh_mult[THR_NEARA ] = 1000;
1024 sf->thresh_mult[THR_NEWA ] = 2000;
1025 sf->thresh_mult[THR_SPLITA ] = 20000;
1028 sf->improved_quant = 0;
1029 sf->improved_dct = 0;
1034 cpi->mode_check_freq[THR_SPLITMV] = 7;
1035 cpi->mode_check_freq[THR_SPLITG] = 15;
1036 cpi->mode_check_freq[THR_SPLITA] = 15;
1038 sf->thresh_mult[THR_TM ] = 2000;
1039 sf->thresh_mult[THR_V_PRED ] = 2000;
1040 sf->thresh_mult[THR_H_PRED ] = 2000;
1041 sf->thresh_mult[THR_B_PRED ] = 5000;
1043 if (cpi->ref_frame_flags & VP8_LAST_FLAG)
1045 sf->thresh_mult[THR_NEWMV ] = 2000;
1046 sf->thresh_mult[THR_SPLITMV ] = 25000;
1049 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
1051 sf->thresh_mult[THR_NEARESTG ] = 2000;
1052 sf->thresh_mult[THR_ZEROG ] = 2000;
1053 sf->thresh_mult[THR_NEARG ] = 2000;
1054 sf->thresh_mult[THR_NEWG ] = 2500;
1055 sf->thresh_mult[THR_SPLITG ] = 50000;
1058 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
1060 sf->thresh_mult[THR_NEARESTA ] = 2000;
1061 sf->thresh_mult[THR_ZEROA ] = 2000;
1062 sf->thresh_mult[THR_NEARA ] = 2000;
1063 sf->thresh_mult[THR_NEWA ] = 2500;
1064 sf->thresh_mult[THR_SPLITA ] = 50000;
1067 sf->full_freq[0] = 31;
1068 sf->full_freq[1] = 63;
1073 sf->auto_filter = 0; // Faster selection of loop filter
1075 cpi->mode_check_freq[THR_V_PRED] = 2;
1076 cpi->mode_check_freq[THR_H_PRED] = 2;
1077 cpi->mode_check_freq[THR_B_PRED] = 2;
1079 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
1081 cpi->mode_check_freq[THR_NEARG] = 2;
1082 cpi->mode_check_freq[THR_NEWG] = 4;
1085 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
1087 cpi->mode_check_freq[THR_NEARA] = 2;
1088 cpi->mode_check_freq[THR_NEWA] = 4;
1091 sf->thresh_mult[THR_SPLITMV ] = INT_MAX;
1092 sf->thresh_mult[THR_SPLITG ] = INT_MAX;
1093 sf->thresh_mult[THR_SPLITA ] = INT_MAX;
1095 sf->full_freq[0] = 63;
1096 sf->full_freq[1] = 127;
1102 sf->full_freq[0] = INT_MAX;
1103 sf->full_freq[1] = INT_MAX;
1105 sf->auto_filter = 1;
1110 sf->auto_filter = 0; // Faster selection of loop filter
1112 #if CONFIG_REALTIME_ONLY
1113 sf->search_method = HEX;
1115 sf->search_method = DIAMOND;
1118 cpi->mode_check_freq[THR_V_PRED] = 4;
1119 cpi->mode_check_freq[THR_H_PRED] = 4;
1120 cpi->mode_check_freq[THR_B_PRED] = 4;
1122 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
1124 cpi->mode_check_freq[THR_NEARG] = 2;
1125 cpi->mode_check_freq[THR_NEWG] = 4;
1128 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
1130 cpi->mode_check_freq[THR_NEARA] = 2;
1131 cpi->mode_check_freq[THR_NEWA] = 4;
1134 sf->thresh_mult[THR_TM ] = 2000;
1135 sf->thresh_mult[THR_B_PRED ] = 5000;
1137 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
1139 sf->thresh_mult[THR_NEARESTG ] = 2000;
1140 sf->thresh_mult[THR_ZEROG ] = 2000;
1141 sf->thresh_mult[THR_NEARG ] = 2000;
1142 sf->thresh_mult[THR_NEWG ] = 4000;
1145 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
1147 sf->thresh_mult[THR_NEARESTA ] = 2000;
1148 sf->thresh_mult[THR_ZEROA ] = 2000;
1149 sf->thresh_mult[THR_NEARA ] = 2000;
1150 sf->thresh_mult[THR_NEWA ] = 4000;
1156 // Disable split MB intra prediction mode
1157 sf->thresh_mult[THR_B_PRED] = INT_MAX;
1162 unsigned int i, sum = 0;
1163 unsigned int total_mbs = cm->MBs;
1168 sf->iterative_sub_pixel = 0;
1170 if (cpi->oxcf.encode_breakout > 2000)
1171 min = cpi->oxcf.encode_breakout;
1175 for (i = 0; i < min; i++)
1177 sum += cpi->error_bins[i];
1183 // i starts from 2 to make sure thresh started from 2048
1184 for (; i < 1024; i++)
1186 sum += cpi->error_bins[i];
1188 if (10 * sum >= (unsigned int)(cpi->Speed - 6)*(total_mbs - total_skip))
1198 if (cpi->ref_frame_flags & VP8_LAST_FLAG)
1200 sf->thresh_mult[THR_NEWMV] = thresh;
1201 sf->thresh_mult[THR_NEARESTMV ] = thresh >> 1;
1202 sf->thresh_mult[THR_NEARMV ] = thresh >> 1;
1205 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
1207 sf->thresh_mult[THR_NEWG] = thresh << 1;
1208 sf->thresh_mult[THR_NEARESTG ] = thresh;
1209 sf->thresh_mult[THR_NEARG ] = thresh;
1212 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
1214 sf->thresh_mult[THR_NEWA] = thresh << 1;
1215 sf->thresh_mult[THR_NEARESTA ] = thresh;
1216 sf->thresh_mult[THR_NEARA ] = thresh;
1219 // Disable other intra prediction modes
1220 sf->thresh_mult[THR_TM] = INT_MAX;
1221 sf->thresh_mult[THR_V_PRED] = INT_MAX;
1222 sf->thresh_mult[THR_H_PRED] = INT_MAX;
1228 sf->quarter_pixel_search = 0;
1233 int Tmp = cpi->Speed - 8;
1238 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
1240 cpi->mode_check_freq[THR_ZEROG] = 1 << (Tmp - 1);
1241 cpi->mode_check_freq[THR_NEARESTG] = 1 << (Tmp - 1);
1242 cpi->mode_check_freq[THR_NEARG] = 1 << Tmp;
1243 cpi->mode_check_freq[THR_NEWG] = 1 << (Tmp + 1);
1246 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
1248 cpi->mode_check_freq[THR_ZEROA] = 1 << (Tmp - 1);
1249 cpi->mode_check_freq[THR_NEARESTA] = 1 << (Tmp - 1);
1250 cpi->mode_check_freq[THR_NEARA] = 1 << Tmp;
1251 cpi->mode_check_freq[THR_NEWA] = 1 << (Tmp + 1);
1254 cpi->mode_check_freq[THR_NEWMV] = 1 << (Tmp - 1);
1257 cm->filter_type = NORMAL_LOOPFILTER;
1260 cm->filter_type = SIMPLE_LOOPFILTER;
1264 sf->half_pixel_search = 0; // This has a big hit on quality. Last resort
1267 vpx_memset(cpi->error_bins, 0, sizeof(cpi->error_bins));
1271 if (cpi->sf.search_method == NSTEP)
1273 vp8_init3smotion_compensation(&cpi->mb, cm->yv12_fb[cm->lst_fb_idx].y_stride);
1275 else if (cpi->sf.search_method == DIAMOND)
1277 vp8_init_dsmotion_compensation(&cpi->mb, cm->yv12_fb[cm->lst_fb_idx].y_stride);
1280 if (cpi->sf.improved_dct)
1282 cpi->mb.vp8_short_fdct8x4 = FDCT_INVOKE(&cpi->rtcd.fdct, short8x4);
1283 cpi->mb.vp8_short_fdct4x4 = FDCT_INVOKE(&cpi->rtcd.fdct, short4x4);
1287 cpi->mb.vp8_short_fdct8x4 = FDCT_INVOKE(&cpi->rtcd.fdct, fast8x4);
1288 cpi->mb.vp8_short_fdct4x4 = FDCT_INVOKE(&cpi->rtcd.fdct, fast4x4);
1291 cpi->mb.short_walsh4x4 = FDCT_INVOKE(&cpi->rtcd.fdct, walsh_short4x4);
1293 if (cpi->sf.improved_quant)
1295 cpi->mb.quantize_b = QUANTIZE_INVOKE(&cpi->rtcd.quantize, quantb);
1299 cpi->mb.quantize_b = QUANTIZE_INVOKE(&cpi->rtcd.quantize, fastquantb);
1301 if (cpi->sf.improved_quant != last_improved_quant)
1302 vp8cx_init_quantizer(cpi);
1304 #if CONFIG_RUNTIME_CPU_DETECT
1305 cpi->mb.e_mbd.rtcd = &cpi->common.rtcd;
1308 if (cpi->sf.iterative_sub_pixel == 1)
1310 cpi->find_fractional_mv_step = vp8_find_best_sub_pixel_step_iteratively;
1312 else if (cpi->sf.quarter_pixel_search)
1314 cpi->find_fractional_mv_step = vp8_find_best_sub_pixel_step;
1316 else if (cpi->sf.half_pixel_search)
1318 cpi->find_fractional_mv_step = vp8_find_best_half_pixel_step;
1322 cpi->find_fractional_mv_step = vp8_skip_fractional_mv_step;
1325 if (cpi->sf.optimize_coefficients == 1)
1326 cpi->mb.optimize = 1 + cpi->is_next_src_alt_ref;
1328 cpi->mb.optimize = 0;
1330 if (cpi->common.full_pixel)
1331 cpi->find_fractional_mv_step = vp8_skip_fractional_mv_step;
1334 frames_at_speed[cpi->Speed]++;
1337 static void alloc_raw_frame_buffers(VP8_COMP *cpi)
1341 buffers = cpi->oxcf.lag_in_frames;
1343 if (buffers > MAX_LAG_BUFFERS)
1344 buffers = MAX_LAG_BUFFERS;
1349 for (i = 0; i < buffers; i++)
1350 if (vp8_yv12_alloc_frame_buffer(&cpi->src_buffer[i].source_buffer,
1351 cpi->oxcf.Width, cpi->oxcf.Height,
1353 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1354 "Failed to allocate lag buffer");
1356 #if VP8_TEMPORAL_ALT_REF
1358 if (vp8_yv12_alloc_frame_buffer(&cpi->alt_ref_buffer.source_buffer,
1359 cpi->oxcf.Width, cpi->oxcf.Height, 16))
1360 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1361 "Failed to allocate altref buffer");
1365 cpi->source_buffer_count = 0;
1368 static int vp8_alloc_partition_data(VP8_COMP *cpi)
1371 vpx_free(cpi->mb.pip);
1373 cpi->mb.pip = vpx_calloc((cpi->common.mb_cols + 1) *
1374 (cpi->common.mb_rows + 1),
1375 sizeof(PARTITION_INFO));
1377 return ALLOC_FAILURE;
1379 cpi->mb.pi = cpi->mb.pip + cpi->common.mode_info_stride + 1;
1384 void vp8_alloc_compressor_data(VP8_COMP *cpi)
1386 VP8_COMMON *cm = & cpi->common;
1388 int width = cm->Width;
1389 int height = cm->Height;
1391 if (vp8_alloc_frame_buffers(cm, width, height))
1392 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1393 "Failed to allocate frame buffers");
1395 if (vp8_alloc_partition_data(cpi))
1396 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1397 "Failed to allocate partition data");
1400 if ((width & 0xf) != 0)
1401 width += 16 - (width & 0xf);
1403 if ((height & 0xf) != 0)
1404 height += 16 - (height & 0xf);
1407 if (vp8_yv12_alloc_frame_buffer(&cpi->last_frame_uf,
1408 width, height, VP8BORDERINPIXELS))
1409 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1410 "Failed to allocate last frame buffer");
1412 if (vp8_yv12_alloc_frame_buffer(&cpi->scaled_source, width, height, 16))
1413 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1414 "Failed to allocate scaled source buffer");
1421 unsigned int tokens = cm->mb_rows * cm->mb_cols * 24 * 16;
1423 CHECK_MEM_ERROR(cpi->tok, vpx_calloc(tokens, sizeof(*cpi->tok)));
1426 // Data used for real time vc mode to see if gf needs refreshing
1427 cpi->inter_zz_count = 0;
1428 cpi->gf_bad_count = 0;
1429 cpi->gf_update_recommended = 0;
1432 // Structures used to minitor GF usage
1433 if (cpi->gf_active_flags != 0)
1434 vpx_free(cpi->gf_active_flags);
1436 CHECK_MEM_ERROR(cpi->gf_active_flags, vpx_calloc(1, cm->mb_rows * cm->mb_cols));
1438 cpi->gf_active_count = cm->mb_rows * cm->mb_cols;
1440 if(cpi->total_stats)
1441 vpx_free(cpi->total_stats);
1443 cpi->total_stats = vpx_calloc(1, vp8_firstpass_stats_sz(cpi->common.MBs));
1445 if(cpi->this_frame_stats)
1446 vpx_free(cpi->this_frame_stats);
1448 cpi->this_frame_stats = vpx_calloc(1, vp8_firstpass_stats_sz(cpi->common.MBs));
1450 if(!cpi->total_stats || !cpi->this_frame_stats)
1451 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1452 "Failed to allocate firstpass stats");
1457 static const int q_trans[] =
1459 0, 1, 2, 3, 4, 5, 7, 8,
1460 9, 10, 12, 13, 15, 17, 18, 19,
1461 20, 21, 23, 24, 25, 26, 27, 28,
1462 29, 30, 31, 33, 35, 37, 39, 41,
1463 43, 45, 47, 49, 51, 53, 55, 57,
1464 59, 61, 64, 67, 70, 73, 76, 79,
1465 82, 85, 88, 91, 94, 97, 100, 103,
1466 106, 109, 112, 115, 118, 121, 124, 127,
1469 int vp8_reverse_trans(int x)
1473 for (i = 0; i < 64; i++)
1474 if (q_trans[i] >= x)
1479 void vp8_new_frame_rate(VP8_COMP *cpi, double framerate)
1484 cpi->oxcf.frame_rate = framerate;
1485 cpi->output_frame_rate = cpi->oxcf.frame_rate;
1486 cpi->per_frame_bandwidth = (int)(cpi->oxcf.target_bandwidth / cpi->output_frame_rate);
1487 cpi->av_per_frame_bandwidth = (int)(cpi->oxcf.target_bandwidth / cpi->output_frame_rate);
1488 cpi->min_frame_bandwidth = (int)(cpi->av_per_frame_bandwidth * cpi->oxcf.two_pass_vbrmin_section / 100);
1489 cpi->max_gf_interval = (int)(cpi->output_frame_rate / 2) + 2;
1491 //cpi->max_gf_interval = (int)(cpi->output_frame_rate * 2 / 3) + 1;
1492 //cpi->max_gf_interval = 24;
1494 if (cpi->max_gf_interval < 12)
1495 cpi->max_gf_interval = 12;
1498 // Special conditions when altr ref frame enabled in lagged compress mode
1499 if (cpi->oxcf.play_alternate && cpi->oxcf.lag_in_frames)
1501 if (cpi->max_gf_interval > cpi->oxcf.lag_in_frames - 1)
1502 cpi->max_gf_interval = cpi->oxcf.lag_in_frames - 1;
1508 rescale(int val, int num, int denom)
1510 int64_t llnum = num;
1511 int64_t llden = denom;
1512 int64_t llval = val;
1514 return llval * llnum / llden;
1518 void vp8_init_config(VP8_PTR ptr, VP8_CONFIG *oxcf)
1520 VP8_COMP *cpi = (VP8_COMP *)(ptr);
1521 VP8_COMMON *cm = &cpi->common;
1527 cpi->auto_adjust_gold_quantizer = 1;
1528 cpi->goldquantizer = 1;
1530 cpi->auto_adjust_key_quantizer = 1;
1531 cpi->keyquantizer = 1;
1533 cm->version = oxcf->Version;
1534 vp8_setup_version(cm);
1540 cpi->auto_worst_q = 0;
1541 cpi->oxcf.best_allowed_q = MINQ;
1542 cpi->oxcf.worst_allowed_q = MAXQ;
1544 cpi->oxcf.end_usage = USAGE_STREAM_FROM_SERVER;
1545 cpi->oxcf.starting_buffer_level = 4000;
1546 cpi->oxcf.optimal_buffer_level = 5000;
1547 cpi->oxcf.maximum_buffer_size = 6000;
1548 cpi->oxcf.under_shoot_pct = 90;
1549 cpi->oxcf.allow_df = 0;
1550 cpi->oxcf.drop_frames_water_mark = 20;
1552 cpi->oxcf.allow_spatial_resampling = 0;
1553 cpi->oxcf.resample_down_water_mark = 40;
1554 cpi->oxcf.resample_up_water_mark = 60;
1556 cpi->oxcf.fixed_q = cpi->interquantizer;
1558 cpi->filter_type = NORMAL_LOOPFILTER;
1560 if (cm->simpler_lpf)
1561 cpi->filter_type = SIMPLE_LOOPFILTER;
1563 cpi->compressor_speed = 1;
1564 cpi->horiz_scale = 0;
1565 cpi->vert_scale = 0;
1566 cpi->oxcf.two_pass_vbrbias = 50;
1567 cpi->oxcf.two_pass_vbrmax_section = 400;
1568 cpi->oxcf.two_pass_vbrmin_section = 0;
1570 cpi->oxcf.Sharpness = 0;
1571 cpi->oxcf.noise_sensitivity = 0;
1577 switch (cpi->oxcf.Mode)
1582 cpi->compressor_speed = 2;
1584 if (cpi->oxcf.cpu_used < -16)
1586 cpi->oxcf.cpu_used = -16;
1589 if (cpi->oxcf.cpu_used > 16)
1590 cpi->oxcf.cpu_used = 16;
1594 #if !(CONFIG_REALTIME_ONLY)
1595 case MODE_GOODQUALITY:
1597 cpi->compressor_speed = 1;
1599 if (cpi->oxcf.cpu_used < -5)
1601 cpi->oxcf.cpu_used = -5;
1604 if (cpi->oxcf.cpu_used > 5)
1605 cpi->oxcf.cpu_used = 5;
1609 case MODE_BESTQUALITY:
1611 cpi->compressor_speed = 0;
1614 case MODE_FIRSTPASS:
1616 cpi->compressor_speed = 1;
1618 case MODE_SECONDPASS:
1620 cpi->compressor_speed = 1;
1622 if (cpi->oxcf.cpu_used < -5)
1624 cpi->oxcf.cpu_used = -5;
1627 if (cpi->oxcf.cpu_used > 5)
1628 cpi->oxcf.cpu_used = 5;
1631 case MODE_SECONDPASS_BEST:
1633 cpi->compressor_speed = 0;
1639 cpi->auto_worst_q = 1;
1641 cpi->oxcf.worst_allowed_q = q_trans[oxcf->worst_allowed_q];
1642 cpi->oxcf.best_allowed_q = q_trans[oxcf->best_allowed_q];
1644 if (oxcf->fixed_q >= 0)
1646 if (oxcf->worst_allowed_q < 0)
1647 cpi->oxcf.fixed_q = q_trans[0];
1649 cpi->oxcf.fixed_q = q_trans[oxcf->worst_allowed_q];
1651 if (oxcf->alt_q < 0)
1652 cpi->oxcf.alt_q = q_trans[0];
1654 cpi->oxcf.alt_q = q_trans[oxcf->alt_q];
1656 if (oxcf->key_q < 0)
1657 cpi->oxcf.key_q = q_trans[0];
1659 cpi->oxcf.key_q = q_trans[oxcf->key_q];
1661 if (oxcf->gold_q < 0)
1662 cpi->oxcf.gold_q = q_trans[0];
1664 cpi->oxcf.gold_q = q_trans[oxcf->gold_q];
1668 cpi->baseline_gf_interval = cpi->oxcf.alt_freq ? cpi->oxcf.alt_freq : DEFAULT_GF_INTERVAL;
1669 cpi->ref_frame_flags = VP8_ALT_FLAG | VP8_GOLD_FLAG | VP8_LAST_FLAG;
1671 //cpi->use_golden_frame_only = 0;
1672 //cpi->use_last_frame_only = 0;
1673 cm->refresh_golden_frame = 0;
1674 cm->refresh_last_frame = 1;
1675 cm->refresh_entropy_probs = 1;
1677 if (cpi->oxcf.token_partitions >= 0 && cpi->oxcf.token_partitions <= 3)
1678 cm->multi_token_partition = (TOKEN_PARTITION) cpi->oxcf.token_partitions;
1680 setup_features(cpi);
1685 for (i = 0; i < MAX_MB_SEGMENTS; i++)
1686 cpi->segment_encode_breakout[i] = cpi->oxcf.encode_breakout;
1689 // At the moment the first order values may not be > MAXQ
1690 if (cpi->oxcf.fixed_q > MAXQ)
1691 cpi->oxcf.fixed_q = MAXQ;
1693 // local file playback mode == really big buffer
1694 if (cpi->oxcf.end_usage == USAGE_LOCAL_FILE_PLAYBACK)
1696 cpi->oxcf.starting_buffer_level = 60000;
1697 cpi->oxcf.optimal_buffer_level = 60000;
1698 cpi->oxcf.maximum_buffer_size = 240000;
1703 // Convert target bandwidth from Kbit/s to Bit/s
1704 cpi->oxcf.target_bandwidth *= 1000;
1705 cpi->oxcf.starting_buffer_level =
1706 rescale(cpi->oxcf.starting_buffer_level,
1707 cpi->oxcf.target_bandwidth, 1000);
1709 if (cpi->oxcf.optimal_buffer_level == 0)
1710 cpi->oxcf.optimal_buffer_level = cpi->oxcf.target_bandwidth / 8;
1712 cpi->oxcf.optimal_buffer_level =
1713 rescale(cpi->oxcf.optimal_buffer_level,
1714 cpi->oxcf.target_bandwidth, 1000);
1716 if (cpi->oxcf.maximum_buffer_size == 0)
1717 cpi->oxcf.maximum_buffer_size = cpi->oxcf.target_bandwidth / 8;
1719 cpi->oxcf.maximum_buffer_size =
1720 rescale(cpi->oxcf.maximum_buffer_size,
1721 cpi->oxcf.target_bandwidth, 1000);
1723 cpi->buffer_level = cpi->oxcf.starting_buffer_level;
1724 cpi->bits_off_target = cpi->oxcf.starting_buffer_level;
1726 vp8_new_frame_rate(cpi, cpi->oxcf.frame_rate);
1727 cpi->worst_quality = cpi->oxcf.worst_allowed_q;
1728 cpi->active_worst_quality = cpi->oxcf.worst_allowed_q;
1729 cpi->avg_frame_qindex = cpi->oxcf.worst_allowed_q;
1730 cpi->best_quality = cpi->oxcf.best_allowed_q;
1731 cpi->active_best_quality = cpi->oxcf.best_allowed_q;
1732 cpi->buffered_mode = (cpi->oxcf.optimal_buffer_level > 0) ? TRUE : FALSE;
1734 cpi->rolling_target_bits = cpi->av_per_frame_bandwidth;
1735 cpi->rolling_actual_bits = cpi->av_per_frame_bandwidth;
1736 cpi->long_rolling_target_bits = cpi->av_per_frame_bandwidth;
1737 cpi->long_rolling_actual_bits = cpi->av_per_frame_bandwidth;
1739 cpi->total_actual_bits = 0;
1740 cpi->total_target_vs_actual = 0;
1742 // Only allow dropped frames in buffered mode
1743 cpi->drop_frames_allowed = cpi->oxcf.allow_df && cpi->buffered_mode;
1745 cm->filter_type = (LOOPFILTERTYPE) cpi->filter_type;
1747 if (!cm->use_bilinear_mc_filter)
1748 cm->mcomp_filter_type = SIXTAP;
1750 cm->mcomp_filter_type = BILINEAR;
1752 cpi->target_bandwidth = cpi->oxcf.target_bandwidth;
1754 cm->Width = cpi->oxcf.Width ;
1755 cm->Height = cpi->oxcf.Height ;
1757 cpi->intra_frame_target = (4 * (cm->Width + cm->Height) / 15) * 1000; // As per VP8
1759 cm->horiz_scale = cpi->horiz_scale;
1760 cm->vert_scale = cpi->vert_scale ;
1762 // VP8 sharpness level mapping 0-7 (vs 0-10 in general VPx dialogs)
1763 if (cpi->oxcf.Sharpness > 7)
1764 cpi->oxcf.Sharpness = 7;
1766 cm->sharpness_level = cpi->oxcf.Sharpness;
1768 if (cm->horiz_scale != NORMAL || cm->vert_scale != NORMAL)
1770 int UNINITIALIZED_IS_SAFE(hr), UNINITIALIZED_IS_SAFE(hs);
1771 int UNINITIALIZED_IS_SAFE(vr), UNINITIALIZED_IS_SAFE(vs);
1773 Scale2Ratio(cm->horiz_scale, &hr, &hs);
1774 Scale2Ratio(cm->vert_scale, &vr, &vs);
1776 // always go to the next whole number
1777 cm->Width = (hs - 1 + cpi->oxcf.Width * hr) / hs;
1778 cm->Height = (vs - 1 + cpi->oxcf.Height * vr) / vs;
1781 if (((cm->Width + 15) & 0xfffffff0) != cm->yv12_fb[cm->lst_fb_idx].y_width ||
1782 ((cm->Height + 15) & 0xfffffff0) != cm->yv12_fb[cm->lst_fb_idx].y_height ||
1783 cm->yv12_fb[cm->lst_fb_idx].y_width == 0)
1785 alloc_raw_frame_buffers(cpi);
1786 vp8_alloc_compressor_data(cpi);
1789 // Clamp KF frame size to quarter of data rate
1790 if (cpi->intra_frame_target > cpi->target_bandwidth >> 2)
1791 cpi->intra_frame_target = cpi->target_bandwidth >> 2;
1793 if (cpi->oxcf.fixed_q >= 0)
1795 cpi->last_q[0] = cpi->oxcf.fixed_q;
1796 cpi->last_q[1] = cpi->oxcf.fixed_q;
1799 cpi->Speed = cpi->oxcf.cpu_used;
1801 // force to allowlag to 0 if lag_in_frames is 0;
1802 if (cpi->oxcf.lag_in_frames == 0)
1804 cpi->oxcf.allow_lag = 0;
1806 // Limit on lag buffers as these are not currently dynamically allocated
1807 else if (cpi->oxcf.lag_in_frames > MAX_LAG_BUFFERS)
1808 cpi->oxcf.lag_in_frames = MAX_LAG_BUFFERS;
1811 cpi->last_alt_ref_sei = -1;
1812 cpi->is_src_frame_alt_ref = 0;
1813 cpi->is_next_src_alt_ref = 0;
1816 // Experimental RD Code
1817 cpi->frame_distortion = 0;
1818 cpi->last_frame_distortion = 0;
1821 #if VP8_TEMPORAL_ALT_REF
1823 cpi->use_weighted_temporal_filter = 0;
1828 cpi->fixed_divide[0] = 0;
1830 for (i = 1; i < 512; i++)
1831 cpi->fixed_divide[i] = 0x80000 / i;
1837 * This function needs more clean up, i.e. be more tuned torwards
1838 * change_config rather than init_config !!!!!!!!!!!!!!!!
1843 void vp8_change_config(VP8_PTR ptr, VP8_CONFIG *oxcf)
1845 VP8_COMP *cpi = (VP8_COMP *)(ptr);
1846 VP8_COMMON *cm = &cpi->common;
1854 if (cm->version != oxcf->Version)
1856 cm->version = oxcf->Version;
1857 vp8_setup_version(cm);
1862 switch (cpi->oxcf.Mode)
1867 cpi->compressor_speed = 2;
1869 if (cpi->oxcf.cpu_used < -16)
1871 cpi->oxcf.cpu_used = -16;
1874 if (cpi->oxcf.cpu_used > 16)
1875 cpi->oxcf.cpu_used = 16;
1879 #if !(CONFIG_REALTIME_ONLY)
1880 case MODE_GOODQUALITY:
1882 cpi->compressor_speed = 1;
1884 if (cpi->oxcf.cpu_used < -5)
1886 cpi->oxcf.cpu_used = -5;
1889 if (cpi->oxcf.cpu_used > 5)
1890 cpi->oxcf.cpu_used = 5;
1894 case MODE_BESTQUALITY:
1896 cpi->compressor_speed = 0;
1899 case MODE_FIRSTPASS:
1901 cpi->compressor_speed = 1;
1903 case MODE_SECONDPASS:
1905 cpi->compressor_speed = 1;
1907 if (cpi->oxcf.cpu_used < -5)
1909 cpi->oxcf.cpu_used = -5;
1912 if (cpi->oxcf.cpu_used > 5)
1913 cpi->oxcf.cpu_used = 5;
1916 case MODE_SECONDPASS_BEST:
1918 cpi->compressor_speed = 0;
1924 cpi->auto_worst_q = 1;
1926 cpi->oxcf.worst_allowed_q = q_trans[oxcf->worst_allowed_q];
1927 cpi->oxcf.best_allowed_q = q_trans[oxcf->best_allowed_q];
1929 if (oxcf->fixed_q >= 0)
1931 if (oxcf->worst_allowed_q < 0)
1932 cpi->oxcf.fixed_q = q_trans[0];
1934 cpi->oxcf.fixed_q = q_trans[oxcf->worst_allowed_q];
1936 if (oxcf->alt_q < 0)
1937 cpi->oxcf.alt_q = q_trans[0];
1939 cpi->oxcf.alt_q = q_trans[oxcf->alt_q];
1941 if (oxcf->key_q < 0)
1942 cpi->oxcf.key_q = q_trans[0];
1944 cpi->oxcf.key_q = q_trans[oxcf->key_q];
1946 if (oxcf->gold_q < 0)
1947 cpi->oxcf.gold_q = q_trans[0];
1949 cpi->oxcf.gold_q = q_trans[oxcf->gold_q];
1953 cpi->baseline_gf_interval = cpi->oxcf.alt_freq ? cpi->oxcf.alt_freq : DEFAULT_GF_INTERVAL;
1955 cpi->ref_frame_flags = VP8_ALT_FLAG | VP8_GOLD_FLAG | VP8_LAST_FLAG;
1957 //cpi->use_golden_frame_only = 0;
1958 //cpi->use_last_frame_only = 0;
1959 cm->refresh_golden_frame = 0;
1960 cm->refresh_last_frame = 1;
1961 cm->refresh_entropy_probs = 1;
1963 if (cpi->oxcf.token_partitions >= 0 && cpi->oxcf.token_partitions <= 3)
1964 cm->multi_token_partition = (TOKEN_PARTITION) cpi->oxcf.token_partitions;
1966 setup_features(cpi);
1971 for (i = 0; i < MAX_MB_SEGMENTS; i++)
1972 cpi->segment_encode_breakout[i] = cpi->oxcf.encode_breakout;
1975 // At the moment the first order values may not be > MAXQ
1976 if (cpi->oxcf.fixed_q > MAXQ)
1977 cpi->oxcf.fixed_q = MAXQ;
1979 // local file playback mode == really big buffer
1980 if (cpi->oxcf.end_usage == USAGE_LOCAL_FILE_PLAYBACK)
1982 cpi->oxcf.starting_buffer_level = 60000;
1983 cpi->oxcf.optimal_buffer_level = 60000;
1984 cpi->oxcf.maximum_buffer_size = 240000;
1988 // Convert target bandwidth from Kbit/s to Bit/s
1989 cpi->oxcf.target_bandwidth *= 1000;
1991 cpi->oxcf.starting_buffer_level =
1992 rescale(cpi->oxcf.starting_buffer_level,
1993 cpi->oxcf.target_bandwidth, 1000);
1995 if (cpi->oxcf.optimal_buffer_level == 0)
1996 cpi->oxcf.optimal_buffer_level = cpi->oxcf.target_bandwidth / 8;
1998 cpi->oxcf.optimal_buffer_level =
1999 rescale(cpi->oxcf.optimal_buffer_level,
2000 cpi->oxcf.target_bandwidth, 1000);
2002 if (cpi->oxcf.maximum_buffer_size == 0)
2003 cpi->oxcf.maximum_buffer_size = cpi->oxcf.target_bandwidth / 8;
2005 cpi->oxcf.maximum_buffer_size =
2006 rescale(cpi->oxcf.maximum_buffer_size,
2007 cpi->oxcf.target_bandwidth, 1000);
2009 cpi->buffer_level = cpi->oxcf.starting_buffer_level;
2010 cpi->bits_off_target = cpi->oxcf.starting_buffer_level;
2012 vp8_new_frame_rate(cpi, cpi->oxcf.frame_rate);
2013 cpi->worst_quality = cpi->oxcf.worst_allowed_q;
2014 cpi->active_worst_quality = cpi->oxcf.worst_allowed_q;
2015 cpi->avg_frame_qindex = cpi->oxcf.worst_allowed_q;
2016 cpi->best_quality = cpi->oxcf.best_allowed_q;
2017 cpi->active_best_quality = cpi->oxcf.best_allowed_q;
2018 cpi->buffered_mode = (cpi->oxcf.optimal_buffer_level > 0) ? TRUE : FALSE;
2020 cpi->rolling_target_bits = cpi->av_per_frame_bandwidth;
2021 cpi->rolling_actual_bits = cpi->av_per_frame_bandwidth;
2022 cpi->long_rolling_target_bits = cpi->av_per_frame_bandwidth;
2023 cpi->long_rolling_actual_bits = cpi->av_per_frame_bandwidth;
2025 cpi->total_actual_bits = 0;
2026 cpi->total_target_vs_actual = 0;
2028 // Only allow dropped frames in buffered mode
2029 cpi->drop_frames_allowed = cpi->oxcf.allow_df && cpi->buffered_mode;
2031 cm->filter_type = (LOOPFILTERTYPE) cpi->filter_type;
2033 if (!cm->use_bilinear_mc_filter)
2034 cm->mcomp_filter_type = SIXTAP;
2036 cm->mcomp_filter_type = BILINEAR;
2038 cpi->target_bandwidth = cpi->oxcf.target_bandwidth;
2040 cm->Width = cpi->oxcf.Width ;
2041 cm->Height = cpi->oxcf.Height ;
2043 cm->horiz_scale = cpi->horiz_scale;
2044 cm->vert_scale = cpi->vert_scale ;
2046 cpi->intra_frame_target = (4 * (cm->Width + cm->Height) / 15) * 1000; // As per VP8
2048 // VP8 sharpness level mapping 0-7 (vs 0-10 in general VPx dialogs)
2049 if (cpi->oxcf.Sharpness > 7)
2050 cpi->oxcf.Sharpness = 7;
2052 cm->sharpness_level = cpi->oxcf.Sharpness;
2054 if (cm->horiz_scale != NORMAL || cm->vert_scale != NORMAL)
2056 int UNINITIALIZED_IS_SAFE(hr), UNINITIALIZED_IS_SAFE(hs);
2057 int UNINITIALIZED_IS_SAFE(vr), UNINITIALIZED_IS_SAFE(vs);
2059 Scale2Ratio(cm->horiz_scale, &hr, &hs);
2060 Scale2Ratio(cm->vert_scale, &vr, &vs);
2062 // always go to the next whole number
2063 cm->Width = (hs - 1 + cpi->oxcf.Width * hr) / hs;
2064 cm->Height = (vs - 1 + cpi->oxcf.Height * vr) / vs;
2067 if (((cm->Width + 15) & 0xfffffff0) != cm->yv12_fb[cm->lst_fb_idx].y_width ||
2068 ((cm->Height + 15) & 0xfffffff0) != cm->yv12_fb[cm->lst_fb_idx].y_height ||
2069 cm->yv12_fb[cm->lst_fb_idx].y_width == 0)
2071 alloc_raw_frame_buffers(cpi);
2072 vp8_alloc_compressor_data(cpi);
2075 // Clamp KF frame size to quarter of data rate
2076 if (cpi->intra_frame_target > cpi->target_bandwidth >> 2)
2077 cpi->intra_frame_target = cpi->target_bandwidth >> 2;
2079 if (cpi->oxcf.fixed_q >= 0)
2081 cpi->last_q[0] = cpi->oxcf.fixed_q;
2082 cpi->last_q[1] = cpi->oxcf.fixed_q;
2085 cpi->Speed = cpi->oxcf.cpu_used;
2087 // force to allowlag to 0 if lag_in_frames is 0;
2088 if (cpi->oxcf.lag_in_frames == 0)
2090 cpi->oxcf.allow_lag = 0;
2092 // Limit on lag buffers as these are not currently dynamically allocated
2093 else if (cpi->oxcf.lag_in_frames > MAX_LAG_BUFFERS)
2094 cpi->oxcf.lag_in_frames = MAX_LAG_BUFFERS;
2097 cpi->last_alt_ref_sei = -1;
2098 cpi->is_src_frame_alt_ref = 0;
2099 cpi->is_next_src_alt_ref = 0;
2102 // Experimental RD Code
2103 cpi->frame_distortion = 0;
2104 cpi->last_frame_distortion = 0;
2109 #define M_LOG2_E 0.693147180559945309417
2110 #define log2f(x) (log (x) / (float) M_LOG2_E)
2111 static void cal_mvsadcosts(int *mvsadcost[2])
2115 mvsadcost [0] [0] = 300;
2116 mvsadcost [1] [0] = 300;
2120 double z = 256 * (2 * (log2f(2 * i) + .6));
2121 mvsadcost [0][i] = (int) z;
2122 mvsadcost [1][i] = (int) z;
2123 mvsadcost [0][-i] = (int) z;
2124 mvsadcost [1][-i] = (int) z;
2126 while (++i <= mv_max);
2129 VP8_PTR vp8_create_compressor(VP8_CONFIG *oxcf)
2141 cpi = ctx.cpi = vpx_memalign(32, sizeof(VP8_COMP));
2142 // Check that the CPI instance is valid
2148 vpx_memset(cpi, 0, sizeof(VP8_COMP));
2150 if (setjmp(cm->error.jmp))
2152 VP8_PTR ptr = ctx.ptr;
2154 ctx.cpi->common.error.setjmp = 0;
2155 vp8_remove_compressor(&ptr);
2159 cpi->common.error.setjmp = 1;
2161 CHECK_MEM_ERROR(cpi->rdtok, vpx_calloc(256 * 3 / 2, sizeof(TOKENEXTRA)));
2162 CHECK_MEM_ERROR(cpi->mb.ss, vpx_calloc(sizeof(search_site), (MAX_MVSEARCH_STEPS * 8) + 1));
2164 vp8_create_common(&cpi->common);
2165 vp8_cmachine_specific_config(cpi);
2167 vp8_init_config((VP8_PTR)cpi, oxcf);
2169 memcpy(cpi->base_skip_false_prob, vp8cx_base_skip_false_prob, sizeof(vp8cx_base_skip_false_prob));
2170 cpi->common.current_video_frame = 0;
2171 cpi->kf_overspend_bits = 0;
2172 cpi->kf_bitrate_adjustment = 0;
2173 cpi->frames_till_gf_update_due = 0;
2174 cpi->gf_overspend_bits = 0;
2175 cpi->non_gf_bitrate_adjustment = 0;
2176 cpi->prob_last_coded = 128;
2177 cpi->prob_gf_coded = 128;
2178 cpi->prob_intra_coded = 63;
2180 // Prime the recent reference frame useage counters.
2181 // Hereafter they will be maintained as a sort of moving average
2182 cpi->recent_ref_frame_usage[INTRA_FRAME] = 1;
2183 cpi->recent_ref_frame_usage[LAST_FRAME] = 1;
2184 cpi->recent_ref_frame_usage[GOLDEN_FRAME] = 1;
2185 cpi->recent_ref_frame_usage[ALTREF_FRAME] = 1;
2187 // Set reference frame sign bias for ALTREF frame to 1 (for now)
2188 cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = 1;
2190 cpi->gf_decay_rate = 0;
2191 cpi->baseline_gf_interval = DEFAULT_GF_INTERVAL;
2193 cpi->gold_is_last = 0 ;
2194 cpi->alt_is_last = 0 ;
2195 cpi->gold_is_alt = 0 ;
2197 // allocate memory for storing last frame's MVs for MV prediction.
2198 CHECK_MEM_ERROR(cpi->lfmv, vpx_calloc((cpi->common.mb_rows+1) * (cpi->common.mb_cols+1), sizeof(int_mv)));
2199 CHECK_MEM_ERROR(cpi->lf_ref_frame_sign_bias, vpx_calloc((cpi->common.mb_rows+1) * (cpi->common.mb_cols+1), sizeof(int)));
2200 CHECK_MEM_ERROR(cpi->lf_ref_frame, vpx_calloc((cpi->common.mb_rows+1) * (cpi->common.mb_cols+1), sizeof(int)));
2202 // Create the encoder segmentation map and set all entries to 0
2203 CHECK_MEM_ERROR(cpi->segmentation_map, vpx_calloc(cpi->common.mb_rows * cpi->common.mb_cols, 1));
2204 CHECK_MEM_ERROR(cpi->active_map, vpx_calloc(cpi->common.mb_rows * cpi->common.mb_cols, 1));
2205 vpx_memset(cpi->active_map , 1, (cpi->common.mb_rows * cpi->common.mb_cols));
2206 cpi->active_map_enabled = 0;
2208 // Create the first pass motion map structure and set to 0
2209 // Allocate space for maximum of 15 buffers
2210 CHECK_MEM_ERROR(cpi->fp_motion_map, vpx_calloc(15*cpi->common.MBs, 1));
2213 // Experimental code for lagged and one pass
2214 // Initialise one_pass GF frames stats
2215 // Update stats used for GF selection
2218 cpi->one_pass_frame_index = 0;
2220 for (i = 0; i < MAX_LAG_BUFFERS; i++)
2222 cpi->one_pass_frame_stats[i].frames_so_far = 0;
2223 cpi->one_pass_frame_stats[i].frame_intra_error = 0.0;
2224 cpi->one_pass_frame_stats[i].frame_coded_error = 0.0;
2225 cpi->one_pass_frame_stats[i].frame_pcnt_inter = 0.0;
2226 cpi->one_pass_frame_stats[i].frame_pcnt_motion = 0.0;
2227 cpi->one_pass_frame_stats[i].frame_mvr = 0.0;
2228 cpi->one_pass_frame_stats[i].frame_mvr_abs = 0.0;
2229 cpi->one_pass_frame_stats[i].frame_mvc = 0.0;
2230 cpi->one_pass_frame_stats[i].frame_mvc_abs = 0.0;
2235 // Should we use the cyclic refresh method.
2236 // Currently this is tied to error resilliant mode
2237 cpi->cyclic_refresh_mode_enabled = cpi->oxcf.error_resilient_mode;
2238 cpi->cyclic_refresh_mode_max_mbs_perframe = (cpi->common.mb_rows * cpi->common.mb_cols) / 40;
2239 cpi->cyclic_refresh_mode_index = 0;
2240 cpi->cyclic_refresh_q = 32;
2242 if (cpi->cyclic_refresh_mode_enabled)
2244 CHECK_MEM_ERROR(cpi->cyclic_refresh_map, vpx_calloc((cpi->common.mb_rows * cpi->common.mb_cols), 1));
2247 cpi->cyclic_refresh_map = (signed char *) NULL;
2249 // Test function for segmentation
2250 //segmentation_test_function((VP8_PTR) cpi);
2252 #ifdef ENTROPY_STATS
2253 init_context_counters();
2256 /*Initialize the feed-forward activity masking.*/
2257 cpi->activity_avg = 90<<12;
2259 cpi->frames_since_key = 8; // Give a sensible default for the first frame.
2260 cpi->key_frame_frequency = cpi->oxcf.key_freq;
2262 cpi->source_alt_ref_pending = FALSE;
2263 cpi->source_alt_ref_active = FALSE;
2264 cpi->common.refresh_alt_ref_frame = 0;
2266 cpi->b_calculate_psnr = CONFIG_PSNR;
2268 cpi->b_calculate_ssimg = 0;
2273 if (cpi->b_calculate_psnr)
2275 cpi->total_sq_error = 0.0;
2276 cpi->total_sq_error2 = 0.0;
2281 cpi->totalp_y = 0.0;
2282 cpi->totalp_u = 0.0;
2283 cpi->totalp_v = 0.0;
2285 cpi->tot_recode_hits = 0;
2286 cpi->summed_quality = 0;
2287 cpi->summed_weights = 0;
2290 if (cpi->b_calculate_ssimg)
2292 cpi->total_ssimg_y = 0;
2293 cpi->total_ssimg_u = 0;
2294 cpi->total_ssimg_v = 0;
2295 cpi->total_ssimg_all = 0;
2299 #define LLONG_MAX 9223372036854775807LL
2301 cpi->first_time_stamp_ever = LLONG_MAX;
2305 cpi->frames_till_gf_update_due = 0;
2306 cpi->key_frame_count = 1;
2307 cpi->tot_key_frame_bits = 0;
2309 cpi->ni_av_qi = cpi->oxcf.worst_allowed_q;
2312 cpi->total_byte_count = 0;
2314 cpi->drop_frame = 0;
2315 cpi->drop_count = 0;
2316 cpi->max_drop_count = 0;
2317 cpi->max_consec_dropped_frames = 4;
2319 cpi->rate_correction_factor = 1.0;
2320 cpi->key_frame_rate_correction_factor = 1.0;
2321 cpi->gf_rate_correction_factor = 1.0;
2322 cpi->est_max_qcorrection_factor = 1.0;
2324 cpi->mb.mvcost[0] = &cpi->mb.mvcosts[0][mv_max+1];
2325 cpi->mb.mvcost[1] = &cpi->mb.mvcosts[1][mv_max+1];
2326 cpi->mb.mvsadcost[0] = &cpi->mb.mvsadcosts[0][mv_max+1];
2327 cpi->mb.mvsadcost[1] = &cpi->mb.mvsadcosts[1][mv_max+1];
2329 cal_mvsadcosts(cpi->mb.mvsadcost);
2331 for (i = 0; i < KEY_FRAME_CONTEXT; i++)
2333 cpi->prior_key_frame_size[i] = cpi->intra_frame_target;
2334 cpi->prior_key_frame_distance[i] = (int)cpi->output_frame_rate;
2337 cpi->check_freq[0] = 15;
2338 cpi->check_freq[1] = 15;
2340 #ifdef OUTPUT_YUV_SRC
2341 yuv_file = fopen("bd.yuv", "ab");
2345 framepsnr = fopen("framepsnr.stt", "a");
2346 kf_list = fopen("kf_list.stt", "w");
2349 cpi->output_pkt_list = oxcf->output_pkt_list;
2351 #if !(CONFIG_REALTIME_ONLY)
2355 vp8_init_first_pass(cpi);
2357 else if (cpi->pass == 2)
2359 size_t packet_sz = vp8_firstpass_stats_sz(cpi->common.MBs);
2360 int packets = oxcf->two_pass_stats_in.sz / packet_sz;
2362 cpi->stats_in = oxcf->two_pass_stats_in.buf;
2363 cpi->stats_in_end = (void*)((char *)cpi->stats_in
2364 + (packets - 1) * packet_sz);
2365 vp8_init_second_pass(cpi);
2370 if (cpi->compressor_speed == 2)
2372 cpi->cpu_freq = 0; //vp8_get_processor_freq();
2373 cpi->avg_encode_time = 0;
2374 cpi->avg_pick_mode_time = 0;
2377 vp8_set_speed_features(cpi);
2379 // Set starting values of RD threshold multipliers (128 = *1)
2380 for (i = 0; i < MAX_MODES; i++)
2382 cpi->rd_thresh_mult[i] = 128;
2385 #ifdef ENTROPY_STATS
2386 init_mv_ref_counts();
2389 vp8cx_create_encoder_threads(cpi);
2391 cpi->fn_ptr[BLOCK_16X16].sdf = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x16);
2392 cpi->fn_ptr[BLOCK_16X16].vf = VARIANCE_INVOKE(&cpi->rtcd.variance, var16x16);
2393 cpi->fn_ptr[BLOCK_16X16].svf = VARIANCE_INVOKE(&cpi->rtcd.variance, subpixvar16x16);
2394 cpi->fn_ptr[BLOCK_16X16].svf_halfpix_h = VARIANCE_INVOKE(&cpi->rtcd.variance, halfpixvar16x16_h);
2395 cpi->fn_ptr[BLOCK_16X16].svf_halfpix_v = VARIANCE_INVOKE(&cpi->rtcd.variance, halfpixvar16x16_v);
2396 cpi->fn_ptr[BLOCK_16X16].svf_halfpix_hv = VARIANCE_INVOKE(&cpi->rtcd.variance, halfpixvar16x16_hv);
2397 cpi->fn_ptr[BLOCK_16X16].sdx3f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x16x3);
2398 cpi->fn_ptr[BLOCK_16X16].sdx8f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x16x8);
2399 cpi->fn_ptr[BLOCK_16X16].sdx4df = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x16x4d);
2401 cpi->fn_ptr[BLOCK_16X8].sdf = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x8);
2402 cpi->fn_ptr[BLOCK_16X8].vf = VARIANCE_INVOKE(&cpi->rtcd.variance, var16x8);
2403 cpi->fn_ptr[BLOCK_16X8].svf = VARIANCE_INVOKE(&cpi->rtcd.variance, subpixvar16x8);
2404 cpi->fn_ptr[BLOCK_16X8].svf_halfpix_h = NULL;
2405 cpi->fn_ptr[BLOCK_16X8].svf_halfpix_v = NULL;
2406 cpi->fn_ptr[BLOCK_16X8].svf_halfpix_hv = NULL;
2407 cpi->fn_ptr[BLOCK_16X8].sdx3f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x8x3);
2408 cpi->fn_ptr[BLOCK_16X8].sdx8f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x8x8);
2409 cpi->fn_ptr[BLOCK_16X8].sdx4df = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x8x4d);
2411 cpi->fn_ptr[BLOCK_8X16].sdf = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x16);
2412 cpi->fn_ptr[BLOCK_8X16].vf = VARIANCE_INVOKE(&cpi->rtcd.variance, var8x16);
2413 cpi->fn_ptr[BLOCK_8X16].svf = VARIANCE_INVOKE(&cpi->rtcd.variance, subpixvar8x16);
2414 cpi->fn_ptr[BLOCK_8X16].svf_halfpix_h = NULL;
2415 cpi->fn_ptr[BLOCK_8X16].svf_halfpix_v = NULL;
2416 cpi->fn_ptr[BLOCK_8X16].svf_halfpix_hv = NULL;
2417 cpi->fn_ptr[BLOCK_8X16].sdx3f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x16x3);
2418 cpi->fn_ptr[BLOCK_8X16].sdx8f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x16x8);
2419 cpi->fn_ptr[BLOCK_8X16].sdx4df = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x16x4d);
2421 cpi->fn_ptr[BLOCK_8X8].sdf = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x8);
2422 cpi->fn_ptr[BLOCK_8X8].vf = VARIANCE_INVOKE(&cpi->rtcd.variance, var8x8);
2423 cpi->fn_ptr[BLOCK_8X8].svf = VARIANCE_INVOKE(&cpi->rtcd.variance, subpixvar8x8);
2424 cpi->fn_ptr[BLOCK_8X8].svf_halfpix_h = NULL;
2425 cpi->fn_ptr[BLOCK_8X8].svf_halfpix_v = NULL;
2426 cpi->fn_ptr[BLOCK_8X8].svf_halfpix_hv = NULL;
2427 cpi->fn_ptr[BLOCK_8X8].sdx3f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x8x3);
2428 cpi->fn_ptr[BLOCK_8X8].sdx8f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x8x8);
2429 cpi->fn_ptr[BLOCK_8X8].sdx4df = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x8x4d);
2431 cpi->fn_ptr[BLOCK_4X4].sdf = VARIANCE_INVOKE(&cpi->rtcd.variance, sad4x4);
2432 cpi->fn_ptr[BLOCK_4X4].vf = VARIANCE_INVOKE(&cpi->rtcd.variance, var4x4);
2433 cpi->fn_ptr[BLOCK_4X4].svf = VARIANCE_INVOKE(&cpi->rtcd.variance, subpixvar4x4);
2434 cpi->fn_ptr[BLOCK_4X4].svf_halfpix_h = NULL;
2435 cpi->fn_ptr[BLOCK_4X4].svf_halfpix_v = NULL;
2436 cpi->fn_ptr[BLOCK_4X4].svf_halfpix_hv = NULL;
2437 cpi->fn_ptr[BLOCK_4X4].sdx3f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad4x4x3);
2438 cpi->fn_ptr[BLOCK_4X4].sdx8f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad4x4x8);
2439 cpi->fn_ptr[BLOCK_4X4].sdx4df = VARIANCE_INVOKE(&cpi->rtcd.variance, sad4x4x4d);
2441 #if !(CONFIG_REALTIME_ONLY)
2442 cpi->full_search_sad = SEARCH_INVOKE(&cpi->rtcd.search, full_search);
2444 cpi->diamond_search_sad = SEARCH_INVOKE(&cpi->rtcd.search, diamond_search);
2446 cpi->ready_for_new_frame = 1;
2448 cpi->source_encode_index = 0;
2450 // make sure frame 1 is okay
2451 cpi->error_bins[0] = cpi->common.MBs;
2453 //vp8cx_init_quantizer() is first called here. Add check in vp8cx_frame_init_quantizer() so that vp8cx_init_quantizer is only called later
2454 //when needed. This will avoid unnecessary calls of vp8cx_init_quantizer() for every frame.
2455 vp8cx_init_quantizer(cpi);
2457 vp8_init_loop_filter(cm);
2458 cm->last_frame_type = KEY_FRAME;
2459 cm->last_filter_type = cm->filter_type;
2460 cm->last_sharpness_level = cm->sharpness_level;
2462 cpi->common.error.setjmp = 0;
2463 return (VP8_PTR) cpi;
2468 void vp8_remove_compressor(VP8_PTR *ptr)
2470 VP8_COMP *cpi = (VP8_COMP *)(*ptr);
2475 if (cpi && (cpi->common.current_video_frame > 0))
2477 #if !(CONFIG_REALTIME_ONLY)
2481 vp8_end_second_pass(cpi);
2486 #ifdef ENTROPY_STATS
2487 print_context_counters();
2488 print_tree_update_probs();
2489 print_mode_context();
2496 FILE *f = fopen("opsnr.stt", "a");
2497 double time_encoded = (cpi->source_end_time_stamp - cpi->first_time_stamp_ever) / 10000000.000;
2498 double total_encode_time = (cpi->time_receive_data + cpi->time_compress_data) / 1000.000;
2499 double dr = (double)cpi->bytes * (double) 8 / (double)1000 / time_encoded;
2501 if (cpi->b_calculate_psnr)
2503 YV12_BUFFER_CONFIG *lst_yv12 = &cpi->common.yv12_fb[cpi->common.lst_fb_idx];
2504 double samples = 3.0 / 2 * cpi->count * lst_yv12->y_width * lst_yv12->y_height;
2505 double total_psnr = vp8_mse2psnr(samples, 255.0, cpi->total_sq_error);
2506 double total_psnr2 = vp8_mse2psnr(samples, 255.0, cpi->total_sq_error2);
2507 double total_ssim = 100 * pow(cpi->summed_quality / cpi->summed_weights, 8.0);
2509 fprintf(f, "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\tVPXSSIM\t Time(us)\n");
2510 fprintf(f, "%7.3f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t%7.3f %8.0f\n",
2511 dr, cpi->total / cpi->count, total_psnr, cpi->totalp / cpi->count, total_psnr2, total_ssim,
2515 if (cpi->b_calculate_ssimg)
2517 fprintf(f, "BitRate\tSSIM_Y\tSSIM_U\tSSIM_V\tSSIM_A\t Time(us)\n");
2518 fprintf(f, "%7.3f\t%6.4f\t%6.4f\t%6.4f\t%6.4f\t%8.0f\n", dr,
2519 cpi->total_ssimg_y / cpi->count, cpi->total_ssimg_u / cpi->count,
2520 cpi->total_ssimg_v / cpi->count, cpi->total_ssimg_all / cpi->count, total_encode_time);
2525 f = fopen("qskip.stt", "a");
2526 fprintf(f, "minq:%d -maxq:%d skipture:skipfalse = %d:%d\n", cpi->oxcf.best_allowed_q, cpi->oxcf.worst_allowed_q, skiptruecount, skipfalsecount);
2537 if (cpi->compressor_speed == 2)
2540 FILE *f = fopen("cxspeed.stt", "a");
2541 cnt_pm /= cpi->common.MBs;
2543 for (i = 0; i < 16; i++)
2544 fprintf(f, "%5d", frames_at_speed[i]);
2547 //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);
2556 extern int count_mb_seg[4];
2557 FILE *f = fopen("modes.stt", "a");
2558 double dr = (double)cpi->oxcf.frame_rate * (double)bytes * (double)8 / (double)count / (double)1000 ;
2559 fprintf(f, "intra_mode in Intra Frames:\n");
2560 fprintf(f, "Y: %8d, %8d, %8d, %8d, %8d\n", y_modes[0], y_modes[1], y_modes[2], y_modes[3], y_modes[4]);
2561 fprintf(f, "UV:%8d, %8d, %8d, %8d\n", uv_modes[0], uv_modes[1], uv_modes[2], uv_modes[3]);
2566 for (i = 0; i < 10; i++)
2567 fprintf(f, "%8d, ", b_modes[i]);
2573 fprintf(f, "Modes in Inter Frames:\n");
2574 fprintf(f, "Y: %8d, %8d, %8d, %8d, %8d, %8d, %8d, %8d, %8d, %8d\n",
2575 inter_y_modes[0], inter_y_modes[1], inter_y_modes[2], inter_y_modes[3], inter_y_modes[4],
2576 inter_y_modes[5], inter_y_modes[6], inter_y_modes[7], inter_y_modes[8], inter_y_modes[9]);
2577 fprintf(f, "UV:%8d, %8d, %8d, %8d\n", inter_uv_modes[0], inter_uv_modes[1], inter_uv_modes[2], inter_uv_modes[3]);
2582 for (i = 0; i < 15; i++)
2583 fprintf(f, "%8d, ", inter_b_modes[i]);
2588 fprintf(f, "P:%8d, %8d, %8d, %8d\n", count_mb_seg[0], count_mb_seg[1], count_mb_seg[2], count_mb_seg[3]);
2589 fprintf(f, "PB:%8d, %8d, %8d, %8d\n", inter_b_modes[LEFT4X4], inter_b_modes[ABOVE4X4], inter_b_modes[ZERO4X4], inter_b_modes[NEW4X4]);
2597 #ifdef ENTROPY_STATS
2600 FILE *fmode = fopen("modecontext.c", "w");
2602 fprintf(fmode, "\n#include \"entropymode.h\"\n\n");
2603 fprintf(fmode, "const unsigned int vp8_kf_default_bmode_counts ");
2604 fprintf(fmode, "[VP8_BINTRAMODES] [VP8_BINTRAMODES] [VP8_BINTRAMODES] =\n{\n");
2606 for (i = 0; i < 10; i++)
2609 fprintf(fmode, " { //Above Mode : %d\n", i);
2611 for (j = 0; j < 10; j++)
2614 fprintf(fmode, " {");
2616 for (k = 0; k < 10; k++)
2618 if (!intra_mode_stats[i][j][k])
2619 fprintf(fmode, " %5d, ", 1);
2621 fprintf(fmode, " %5d, ", intra_mode_stats[i][j][k]);
2624 fprintf(fmode, "}, // left_mode %d\n", j);
2628 fprintf(fmode, " },\n");
2632 fprintf(fmode, "};\n");
2638 #if defined(SECTIONBITS_OUTPUT)
2643 FILE *f = fopen("tokenbits.stt", "a");
2645 for (i = 0; i < 28; i++)
2646 fprintf(f, "%8d", (int)(Sectionbits[i] / 256));
2656 printf("\n_pick_loop_filter_level:%d\n", cpi->time_pick_lpf / 1000);
2657 printf("\n_frames recive_data encod_mb_row compress_frame Total\n");
2658 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);
2664 vp8cx_remove_encoder_threads(cpi);
2666 vp8_dealloc_compressor_data(cpi);
2667 vpx_free(cpi->mb.ss);
2669 vpx_free(cpi->rdtok);
2670 vpx_free(cpi->cyclic_refresh_map);
2672 vp8_remove_common(&cpi->common);
2676 #ifdef OUTPUT_YUV_SRC
2696 static uint64_t calc_plane_error(unsigned char *orig, int orig_stride,
2697 unsigned char *recon, int recon_stride,
2698 unsigned int cols, unsigned int rows,
2699 vp8_variance_rtcd_vtable_t *rtcd)
2701 unsigned int row, col;
2702 uint64_t total_sse = 0;
2705 for (row = 0; row + 16 <= rows; row += 16)
2707 for (col = 0; col + 16 <= cols; col += 16)
2711 VARIANCE_INVOKE(rtcd, mse16x16)(orig + col, orig_stride,
2712 recon + col, recon_stride,
2717 /* Handle odd-sized width */
2720 unsigned int border_row, border_col;
2721 unsigned char *border_orig = orig;
2722 unsigned char *border_recon = recon;
2724 for (border_row = 0; border_row < 16; border_row++)
2726 for (border_col = col; border_col < cols; border_col++)
2728 diff = border_orig[border_col] - border_recon[border_col];
2729 total_sse += diff * diff;
2732 border_orig += orig_stride;
2733 border_recon += recon_stride;
2737 orig += orig_stride * 16;
2738 recon += recon_stride * 16;
2741 /* Handle odd-sized height */
2742 for (; row < rows; row++)
2744 for (col = 0; col < cols; col++)
2746 diff = orig[col] - recon[col];
2747 total_sse += diff * diff;
2750 orig += orig_stride;
2751 recon += recon_stride;
2758 static void generate_psnr_packet(VP8_COMP *cpi)
2760 YV12_BUFFER_CONFIG *orig = cpi->Source;
2761 YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
2762 struct vpx_codec_cx_pkt pkt;
2765 unsigned int width = cpi->common.Width;
2766 unsigned int height = cpi->common.Height;
2768 pkt.kind = VPX_CODEC_PSNR_PKT;
2769 sse = calc_plane_error(orig->y_buffer, orig->y_stride,
2770 recon->y_buffer, recon->y_stride,
2772 IF_RTCD(&cpi->rtcd.variance));
2773 pkt.data.psnr.sse[0] = sse;
2774 pkt.data.psnr.sse[1] = sse;
2775 pkt.data.psnr.samples[0] = width * height;
2776 pkt.data.psnr.samples[1] = width * height;
2778 width = (width + 1) / 2;
2779 height = (height + 1) / 2;
2781 sse = calc_plane_error(orig->u_buffer, orig->uv_stride,
2782 recon->u_buffer, recon->uv_stride,
2784 IF_RTCD(&cpi->rtcd.variance));
2785 pkt.data.psnr.sse[0] += sse;
2786 pkt.data.psnr.sse[2] = sse;
2787 pkt.data.psnr.samples[0] += width * height;
2788 pkt.data.psnr.samples[2] = width * height;
2790 sse = calc_plane_error(orig->v_buffer, orig->uv_stride,
2791 recon->v_buffer, recon->uv_stride,
2793 IF_RTCD(&cpi->rtcd.variance));
2794 pkt.data.psnr.sse[0] += sse;
2795 pkt.data.psnr.sse[3] = sse;
2796 pkt.data.psnr.samples[0] += width * height;
2797 pkt.data.psnr.samples[3] = width * height;
2799 for (i = 0; i < 4; i++)
2800 pkt.data.psnr.psnr[i] = vp8_mse2psnr(pkt.data.psnr.samples[i], 255.0,
2801 pkt.data.psnr.sse[i]);
2803 vpx_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
2807 int vp8_use_as_reference(VP8_PTR ptr, int ref_frame_flags)
2809 VP8_COMP *cpi = (VP8_COMP *)(ptr);
2811 if (ref_frame_flags > 7)
2814 cpi->ref_frame_flags = ref_frame_flags;
2817 int vp8_update_reference(VP8_PTR ptr, int ref_frame_flags)
2819 VP8_COMP *cpi = (VP8_COMP *)(ptr);
2821 if (ref_frame_flags > 7)
2824 cpi->common.refresh_golden_frame = 0;
2825 cpi->common.refresh_alt_ref_frame = 0;
2826 cpi->common.refresh_last_frame = 0;
2828 if (ref_frame_flags & VP8_LAST_FLAG)
2829 cpi->common.refresh_last_frame = 1;
2831 if (ref_frame_flags & VP8_GOLD_FLAG)
2832 cpi->common.refresh_golden_frame = 1;
2834 if (ref_frame_flags & VP8_ALT_FLAG)
2835 cpi->common.refresh_alt_ref_frame = 1;
2840 int vp8_get_reference(VP8_PTR ptr, VP8_REFFRAME ref_frame_flag, YV12_BUFFER_CONFIG *sd)
2842 VP8_COMP *cpi = (VP8_COMP *)(ptr);
2843 VP8_COMMON *cm = &cpi->common;
2846 if (ref_frame_flag == VP8_LAST_FLAG)
2847 ref_fb_idx = cm->lst_fb_idx;
2848 else if (ref_frame_flag == VP8_GOLD_FLAG)
2849 ref_fb_idx = cm->gld_fb_idx;
2850 else if (ref_frame_flag == VP8_ALT_FLAG)
2851 ref_fb_idx = cm->alt_fb_idx;
2855 vp8_yv12_copy_frame_ptr(&cm->yv12_fb[ref_fb_idx], sd);
2859 int vp8_set_reference(VP8_PTR ptr, VP8_REFFRAME ref_frame_flag, YV12_BUFFER_CONFIG *sd)
2861 VP8_COMP *cpi = (VP8_COMP *)(ptr);
2862 VP8_COMMON *cm = &cpi->common;
2866 if (ref_frame_flag == VP8_LAST_FLAG)
2867 ref_fb_idx = cm->lst_fb_idx;
2868 else if (ref_frame_flag == VP8_GOLD_FLAG)
2869 ref_fb_idx = cm->gld_fb_idx;
2870 else if (ref_frame_flag == VP8_ALT_FLAG)
2871 ref_fb_idx = cm->alt_fb_idx;
2875 vp8_yv12_copy_frame_ptr(sd, &cm->yv12_fb[ref_fb_idx]);
2879 int vp8_update_entropy(VP8_PTR comp, int update)
2881 VP8_COMP *cpi = (VP8_COMP *) comp;
2882 VP8_COMMON *cm = &cpi->common;
2883 cm->refresh_entropy_probs = update;
2890 void vp8_write_yuv_frame(const char *name, YV12_BUFFER_CONFIG *s)
2892 FILE *yuv_file = fopen(name, "ab");
2893 unsigned char *src = s->y_buffer;
2894 int h = s->y_height;
2898 fwrite(src, s->y_width, 1, yuv_file);
2908 fwrite(src, s->uv_width, 1, yuv_file);
2909 src += s->uv_stride;
2918 fwrite(src, s->uv_width, 1, yuv_file);
2919 src += s->uv_stride;
2928 static void scale_and_extend_source(YV12_BUFFER_CONFIG *sd, VP8_COMP *cpi)
2930 VP8_COMMON *cm = &cpi->common;
2932 // are we resizing the image
2933 if (cm->horiz_scale != 0 || cm->vert_scale != 0)
2935 #if CONFIG_SPATIAL_RESAMPLING
2936 int UNINITIALIZED_IS_SAFE(hr), UNINITIALIZED_IS_SAFE(hs);
2937 int UNINITIALIZED_IS_SAFE(vr), UNINITIALIZED_IS_SAFE(vs);
2940 if (cm->vert_scale == 3)
2945 Scale2Ratio(cm->horiz_scale, &hr, &hs);
2946 Scale2Ratio(cm->vert_scale, &vr, &vs);
2948 vp8_scale_frame(sd, &cpi->scaled_source, cm->temp_scale_frame.y_buffer,
2949 tmp_height, hs, hr, vs, vr, 0);
2951 cpi->Source = &cpi->scaled_source;
2954 // we may need to copy to a buffer so we can extend the image...
2955 else if (cm->Width != cm->yv12_fb[cm->lst_fb_idx].y_width ||
2956 cm->Height != cm->yv12_fb[cm->lst_fb_idx].y_height)
2958 //vp8_yv12_copy_frame_ptr(sd, &cpi->scaled_source);
2960 #if CONFIG_RUNTIME_CPU_DETECT
2961 if (cm->rtcd.flags & HAS_NEON)
2964 vp8_yv12_copy_src_frame_func_neon(sd, &cpi->scaled_source);
2966 #if CONFIG_RUNTIME_CPU_DETECT
2970 #if !HAVE_ARMV7 || CONFIG_RUNTIME_CPU_DETECT
2972 vp8_yv12_copy_frame_ptr(sd, &cpi->scaled_source);
2976 cpi->Source = &cpi->scaled_source;
2979 vp8_extend_to_multiple_of16(cpi->Source, cm->Width, cm->Height);
2982 static void resize_key_frame(VP8_COMP *cpi)
2984 #if CONFIG_SPATIAL_RESAMPLING
2985 VP8_COMMON *cm = &cpi->common;
2987 // Do we need to apply resampling for one pass cbr.
2988 // In one pass this is more limited than in two pass cbr
2989 // The test and any change is only made one per key frame sequence
2990 if (cpi->oxcf.allow_spatial_resampling && (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER))
2992 int UNINITIALIZED_IS_SAFE(hr), UNINITIALIZED_IS_SAFE(hs);
2993 int UNINITIALIZED_IS_SAFE(vr), UNINITIALIZED_IS_SAFE(vs);
2994 int new_width, new_height;
2996 // If we are below the resample DOWN watermark then scale down a notch.
2997 if (cpi->buffer_level < (cpi->oxcf.resample_down_water_mark * cpi->oxcf.optimal_buffer_level / 100))
2999 cm->horiz_scale = (cm->horiz_scale < ONETWO) ? cm->horiz_scale + 1 : ONETWO;
3000 cm->vert_scale = (cm->vert_scale < ONETWO) ? cm->vert_scale + 1 : ONETWO;
3002 // Should we now start scaling back up
3003 else if (cpi->buffer_level > (cpi->oxcf.resample_up_water_mark * cpi->oxcf.optimal_buffer_level / 100))
3005 cm->horiz_scale = (cm->horiz_scale > NORMAL) ? cm->horiz_scale - 1 : NORMAL;
3006 cm->vert_scale = (cm->vert_scale > NORMAL) ? cm->vert_scale - 1 : NORMAL;
3009 // Get the new hieght and width
3010 Scale2Ratio(cm->horiz_scale, &hr, &hs);
3011 Scale2Ratio(cm->vert_scale, &vr, &vs);
3012 new_width = ((hs - 1) + (cpi->oxcf.Width * hr)) / hs;
3013 new_height = ((vs - 1) + (cpi->oxcf.Height * vr)) / vs;
3015 // If the image size has changed we need to reallocate the buffers
3016 // and resample the source image
3017 if ((cm->Width != new_width) || (cm->Height != new_height))
3019 cm->Width = new_width;
3020 cm->Height = new_height;
3021 vp8_alloc_compressor_data(cpi);
3022 scale_and_extend_source(cpi->un_scaled_source, cpi);
3028 // return of 0 means drop frame
3029 static int pick_frame_size(VP8_COMP *cpi)
3031 VP8_COMMON *cm = &cpi->common;
3033 // First Frame is a special case
3034 if (cm->current_video_frame == 0)
3036 #if !(CONFIG_REALTIME_ONLY)
3039 vp8_calc_auto_iframe_target_size(cpi);
3041 // 1 Pass there is no information on which to base size so use bandwidth per second * fixed fraction
3044 cpi->this_frame_target = cpi->oxcf.target_bandwidth / 2;
3046 // in error resilient mode the first frame is bigger since it likely contains
3047 // all the static background
3048 if (cpi->oxcf.error_resilient_mode == 1 || (cpi->compressor_speed == 2))
3050 cpi->this_frame_target *= 3; // 5;
3053 // Key frame from VFW/auto-keyframe/first frame
3054 cm->frame_type = KEY_FRAME;
3057 // Special case for forced key frames
3058 // The frame sizing here is still far from ideal for 2 pass.
3059 else if (cm->frame_flags & FRAMEFLAGS_KEY)
3061 cm->frame_type = KEY_FRAME;
3062 resize_key_frame(cpi);
3063 vp8_calc_iframe_target_size(cpi);
3065 else if (cm->frame_type == KEY_FRAME)
3067 vp8_calc_auto_iframe_target_size(cpi);
3071 // INTER frame: compute target frame size
3072 cm->frame_type = INTER_FRAME;
3073 vp8_calc_pframe_target_size(cpi);
3075 // Check if we're dropping the frame:
3076 if (cpi->drop_frame)
3078 cpi->drop_frame = FALSE;
3084 // Note target_size in bits * 256 per MB
3085 cpi->target_bits_per_mb = (cpi->this_frame_target * 256) / cpi->common.MBs;
3089 static void set_quantizer(VP8_COMP *cpi, int Q)
3091 VP8_COMMON *cm = &cpi->common;
3092 MACROBLOCKD *mbd = &cpi->mb.e_mbd;
3094 cm->base_qindex = Q;
3096 cm->y1dc_delta_q = 0;
3097 cm->y2dc_delta_q = 0;
3098 cm->y2ac_delta_q = 0;
3099 cm->uvdc_delta_q = 0;
3100 cm->uvac_delta_q = 0;
3102 // Set Segment specific quatizers
3103 mbd->segment_feature_data[MB_LVL_ALT_Q][0] = cpi->segment_feature_data[MB_LVL_ALT_Q][0];
3104 mbd->segment_feature_data[MB_LVL_ALT_Q][1] = cpi->segment_feature_data[MB_LVL_ALT_Q][1];
3105 mbd->segment_feature_data[MB_LVL_ALT_Q][2] = cpi->segment_feature_data[MB_LVL_ALT_Q][2];
3106 mbd->segment_feature_data[MB_LVL_ALT_Q][3] = cpi->segment_feature_data[MB_LVL_ALT_Q][3];
3109 static void update_alt_ref_frame_and_stats(VP8_COMP *cpi)
3111 VP8_COMMON *cm = &cpi->common;
3113 // Update the golden frame buffer
3114 vp8_yv12_copy_frame_ptr(cm->frame_to_show, &cm->yv12_fb[cm->alt_fb_idx]);
3116 // Select an interval before next GF or altref
3117 if (!cpi->auto_gold)
3118 cpi->frames_till_gf_update_due = cpi->goldfreq;
3120 if ((cpi->pass != 2) && cpi->frames_till_gf_update_due)
3122 cpi->current_gf_interval = cpi->frames_till_gf_update_due;
3124 // Set the bits per frame that we should try and recover in subsequent inter frames
3125 // to account for the extra GF spend... note that his does not apply for GF updates
3126 // that occur coincident with a key frame as the extra cost of key frames is dealt
3129 cpi->gf_overspend_bits += cpi->projected_frame_size;
3130 cpi->non_gf_bitrate_adjustment = cpi->gf_overspend_bits / cpi->frames_till_gf_update_due;
3133 // Update data structure that monitors level of reference to last GF
3134 vpx_memset(cpi->gf_active_flags, 1, (cm->mb_rows * cm->mb_cols));
3135 cpi->gf_active_count = cm->mb_rows * cm->mb_cols;
3136 // this frame refreshes means next frames don't unless specified by user
3138 cpi->common.frames_since_golden = 0;
3140 // Clear the alternate reference update pending flag.
3141 cpi->source_alt_ref_pending = FALSE;
3143 // Set the alternate refernce frame active flag
3144 cpi->source_alt_ref_active = TRUE;
3148 static void update_golden_frame_and_stats(VP8_COMP *cpi)
3150 VP8_COMMON *cm = &cpi->common;
3152 // Update the Golden frame reconstruction buffer if signalled and the GF usage counts.
3153 if (cm->refresh_golden_frame)
3155 // Update the golden frame buffer
3156 vp8_yv12_copy_frame_ptr(cm->frame_to_show, &cm->yv12_fb[cm->gld_fb_idx]);
3158 // Select an interval before next GF
3159 if (!cpi->auto_gold)
3160 cpi->frames_till_gf_update_due = cpi->goldfreq;
3162 if ((cpi->pass != 2) && (cpi->frames_till_gf_update_due > 0))
3164 cpi->current_gf_interval = cpi->frames_till_gf_update_due;
3166 // Set the bits per frame that we should try and recover in subsequent inter frames
3167 // to account for the extra GF spend... note that his does not apply for GF updates
3168 // that occur coincident with a key frame as the extra cost of key frames is dealt
3170 if ((cm->frame_type != KEY_FRAME) && !cpi->source_alt_ref_active)
3172 // Calcluate GF bits to be recovered
3173 // Projected size - av frame bits available for inter frames for clip as a whole
3174 cpi->gf_overspend_bits += (cpi->projected_frame_size - cpi->inter_frame_target);
3177 cpi->non_gf_bitrate_adjustment = cpi->gf_overspend_bits / cpi->frames_till_gf_update_due;
3181 // Update data structure that monitors level of reference to last GF
3182 vpx_memset(cpi->gf_active_flags, 1, (cm->mb_rows * cm->mb_cols));
3183 cpi->gf_active_count = cm->mb_rows * cm->mb_cols;
3185 // this frame refreshes means next frames don't unless specified by user
3186 cm->refresh_golden_frame = 0;
3187 cpi->common.frames_since_golden = 0;
3189 //if ( cm->frame_type == KEY_FRAME )
3191 cpi->recent_ref_frame_usage[INTRA_FRAME] = 1;
3192 cpi->recent_ref_frame_usage[LAST_FRAME] = 1;
3193 cpi->recent_ref_frame_usage[GOLDEN_FRAME] = 1;
3194 cpi->recent_ref_frame_usage[ALTREF_FRAME] = 1;
3198 // // Carry a potrtion of count over to begining of next gf sequence
3199 // cpi->recent_ref_frame_usage[INTRA_FRAME] >>= 5;
3200 // cpi->recent_ref_frame_usage[LAST_FRAME] >>= 5;
3201 // cpi->recent_ref_frame_usage[GOLDEN_FRAME] >>= 5;
3202 // cpi->recent_ref_frame_usage[ALTREF_FRAME] >>= 5;
3205 // ******** Fixed Q test code only ************
3206 // If we are going to use the ALT reference for the next group of frames set a flag to say so.
3207 if (cpi->oxcf.fixed_q >= 0 &&
3208 cpi->oxcf.play_alternate && !cpi->common.refresh_alt_ref_frame)
3210 cpi->source_alt_ref_pending = TRUE;
3211 cpi->frames_till_gf_update_due = cpi->baseline_gf_interval;
3214 if (!cpi->source_alt_ref_pending)
3215 cpi->source_alt_ref_active = FALSE;
3217 // Decrement count down till next gf
3218 if (cpi->frames_till_gf_update_due > 0)
3219 cpi->frames_till_gf_update_due--;
3222 else if (!cpi->common.refresh_alt_ref_frame)
3224 // Decrement count down till next gf
3225 if (cpi->frames_till_gf_update_due > 0)
3226 cpi->frames_till_gf_update_due--;
3228 if (cpi->common.frames_till_alt_ref_frame)
3229 cpi->common.frames_till_alt_ref_frame --;
3231 cpi->common.frames_since_golden ++;
3233 if (cpi->common.frames_since_golden > 1)
3235 cpi->recent_ref_frame_usage[INTRA_FRAME] += cpi->count_mb_ref_frame_usage[INTRA_FRAME];
3236 cpi->recent_ref_frame_usage[LAST_FRAME] += cpi->count_mb_ref_frame_usage[LAST_FRAME];
3237 cpi->recent_ref_frame_usage[GOLDEN_FRAME] += cpi->count_mb_ref_frame_usage[GOLDEN_FRAME];
3238 cpi->recent_ref_frame_usage[ALTREF_FRAME] += cpi->count_mb_ref_frame_usage[ALTREF_FRAME];
3243 // This function updates the reference frame probability estimates that
3244 // will be used during mode selection
3245 static void update_rd_ref_frame_probs(VP8_COMP *cpi)
3247 VP8_COMMON *cm = &cpi->common;
3250 const int *const rfct = cpi->recent_ref_frame_usage;
3251 const int rf_intra = rfct[INTRA_FRAME];
3252 const int rf_inter = rfct[LAST_FRAME] + rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME];
3254 if (cm->frame_type == KEY_FRAME)
3256 cpi->prob_intra_coded = 255;
3257 cpi->prob_last_coded = 128;
3258 cpi->prob_gf_coded = 128;
3260 else if (!(rf_intra + rf_inter))
3262 // This is a trap in case this function is called with cpi->recent_ref_frame_usage[] blank.
3263 cpi->prob_intra_coded = 63;
3264 cpi->prob_last_coded = 128;
3265 cpi->prob_gf_coded = 128;
3269 cpi->prob_intra_coded = (rf_intra * 255) / (rf_intra + rf_inter);
3271 if (cpi->prob_intra_coded < 1)
3272 cpi->prob_intra_coded = 1;
3274 if ((cm->frames_since_golden > 0) || cpi->source_alt_ref_active)
3276 cpi->prob_last_coded = rf_inter ? (rfct[LAST_FRAME] * 255) / rf_inter : 128;
3278 if (cpi->prob_last_coded < 1)
3279 cpi->prob_last_coded = 1;
3281 cpi->prob_gf_coded = (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME])
3282 ? (rfct[GOLDEN_FRAME] * 255) / (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME]) : 128;
3284 if (cpi->prob_gf_coded < 1)
3285 cpi->prob_gf_coded = 1;
3290 const int *const rfct = cpi->count_mb_ref_frame_usage;
3291 const int rf_intra = rfct[INTRA_FRAME];
3292 const int rf_inter = rfct[LAST_FRAME] + rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME];
3294 if (cm->frame_type == KEY_FRAME)
3296 cpi->prob_intra_coded = 255;
3297 cpi->prob_last_coded = 128;
3298 cpi->prob_gf_coded = 128;
3300 else if (!(rf_intra + rf_inter))
3302 // This is a trap in case this function is called with cpi->recent_ref_frame_usage[] blank.
3303 cpi->prob_intra_coded = 63;
3304 cpi->prob_last_coded = 128;
3305 cpi->prob_gf_coded = 128;
3309 cpi->prob_intra_coded = (rf_intra * 255) / (rf_intra + rf_inter);
3311 if (cpi->prob_intra_coded < 1)
3312 cpi->prob_intra_coded = 1;
3314 cpi->prob_last_coded = rf_inter ? (rfct[LAST_FRAME] * 255) / rf_inter : 128;
3316 if (cpi->prob_last_coded < 1)
3317 cpi->prob_last_coded = 1;
3319 cpi->prob_gf_coded = (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME])
3320 ? (rfct[GOLDEN_FRAME] * 255) / (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME]) : 128;
3322 if (cpi->prob_gf_coded < 1)
3323 cpi->prob_gf_coded = 1;
3326 // update reference frame costs since we can do better than what we got last frame.
3328 if (cpi->common.refresh_alt_ref_frame)
3330 cpi->prob_intra_coded += 40;
3331 cpi->prob_last_coded = 200;
3332 cpi->prob_gf_coded = 1;
3334 else if (cpi->common.frames_since_golden == 0)
3336 cpi->prob_last_coded = 214;
3337 cpi->prob_gf_coded = 1;
3339 else if (cpi->common.frames_since_golden == 1)
3341 cpi->prob_last_coded = 192;
3342 cpi->prob_gf_coded = 220;
3344 else if (cpi->source_alt_ref_active)
3346 //int dist = cpi->common.frames_till_alt_ref_frame + cpi->common.frames_since_golden;
3347 cpi->prob_gf_coded -= 20;
3349 if (cpi->prob_gf_coded < 10)
3350 cpi->prob_gf_coded = 10;
3357 // 1 = key, 0 = inter
3358 static int decide_key_frame(VP8_COMP *cpi)
3360 VP8_COMMON *cm = &cpi->common;
3362 int code_key_frame = FALSE;
3366 if (cpi->Speed > 11)
3369 // Clear down mmx registers
3370 vp8_clear_system_state(); //__asm emms;
3372 if ((cpi->compressor_speed == 2) && (cpi->Speed >= 5) && (cpi->sf.RD == 0))
3374 double change = 1.0 * abs((int)(cpi->intra_error - cpi->last_intra_error)) / (1 + cpi->last_intra_error);
3375 double change2 = 1.0 * abs((int)(cpi->prediction_error - cpi->last_prediction_error)) / (1 + cpi->last_prediction_error);
3376 double minerror = cm->MBs * 256;
3380 if (10 * cpi->intra_error / (1 + cpi->prediction_error) < 15
3381 && cpi->prediction_error > minerror
3382 && (change > .25 || change2 > .25))
3384 FILE *f = fopen("intra_inter.stt", "a");
3386 if (cpi->prediction_error <= 0)
3387 cpi->prediction_error = 1;
3389 fprintf(f, "%d %d %d %d %14.4f\n",
3390 cm->current_video_frame,
3391 (int) cpi->prediction_error,
3392 (int) cpi->intra_error,
3393 (int)((10 * cpi->intra_error) / cpi->prediction_error),
3401 cpi->last_intra_error = cpi->intra_error;
3402 cpi->last_prediction_error = cpi->prediction_error;
3404 if (10 * cpi->intra_error / (1 + cpi->prediction_error) < 15
3405 && cpi->prediction_error > minerror
3406 && (change > .25 || change2 > .25))
3408 /*(change > 1.4 || change < .75)&& cpi->this_frame_percent_intra > cpi->last_frame_percent_intra + 3*/
3416 // If the following are true we might as well code a key frame
3417 if (((cpi->this_frame_percent_intra == 100) &&
3418 (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra + 2))) ||
3419 ((cpi->this_frame_percent_intra > 95) &&
3420 (cpi->this_frame_percent_intra >= (cpi->last_frame_percent_intra + 5))))
3422 code_key_frame = TRUE;
3424 // in addition if the following are true and this is not a golden frame then code a key frame
3425 // Note that on golden frames there often seems to be a pop in intra useage anyway hence this
3426 // restriction is designed to prevent spurious key frames. The Intra pop needs to be investigated.
3427 else if (((cpi->this_frame_percent_intra > 60) &&
3428 (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra * 2))) ||
3429 ((cpi->this_frame_percent_intra > 75) &&
3430 (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra * 3 / 2))) ||
3431 ((cpi->this_frame_percent_intra > 90) &&
3432 (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra + 10))))
3434 if (!cm->refresh_golden_frame)
3435 code_key_frame = TRUE;
3438 return code_key_frame;
3442 #if !(CONFIG_REALTIME_ONLY)
3443 static void Pass1Encode(VP8_COMP *cpi, unsigned long *size, unsigned char *dest, unsigned int *frame_flags)
3448 set_quantizer(cpi, 26);
3450 scale_and_extend_source(cpi->un_scaled_source, cpi);
3451 vp8_first_pass(cpi);
3456 void write_cx_frame_to_file(YV12_BUFFER_CONFIG *frame, int this_frame)
3464 sprintf(filename, "cx\\y%04d.raw", this_frame);
3465 yframe = fopen(filename, "wb");
3467 for (i = 0; i < frame->y_height; i++)
3468 fwrite(frame->y_buffer + i * frame->y_stride, frame->y_width, 1, yframe);
3471 sprintf(filename, "cx\\u%04d.raw", this_frame);
3472 yframe = fopen(filename, "wb");
3474 for (i = 0; i < frame->uv_height; i++)
3475 fwrite(frame->u_buffer + i * frame->uv_stride, frame->uv_width, 1, yframe);
3478 sprintf(filename, "cx\\v%04d.raw", this_frame);
3479 yframe = fopen(filename, "wb");
3481 for (i = 0; i < frame->uv_height; i++)
3482 fwrite(frame->v_buffer + i * frame->uv_stride, frame->uv_width, 1, yframe);
3487 // return of 0 means drop frame
3489 // Function to test for conditions that indeicate we should loop
3490 // back and recode a frame.
3491 static BOOL recode_loop_test( VP8_COMP *cpi,
3492 int high_limit, int low_limit,
3493 int q, int maxq, int minq )
3495 BOOL force_recode = FALSE;
3496 VP8_COMMON *cm = &cpi->common;
3498 // Is frame recode allowed at all
3499 // Yes if either recode mode 1 is selected or mode two is selcted
3500 // and the frame is a key frame. golden frame or alt_ref_frame
3501 if ( (cpi->sf.recode_loop == 1) ||
3502 ( (cpi->sf.recode_loop == 2) &&
3503 ( (cm->frame_type == KEY_FRAME) ||
3504 cm->refresh_golden_frame ||
3505 cm->refresh_alt_ref_frame ) ) )
3507 // General over and under shoot tests
3508 if ( ((cpi->projected_frame_size > high_limit) && (q < maxq)) ||
3509 ((cpi->projected_frame_size < low_limit) && (q > minq)) )
3511 force_recode = TRUE;
3513 // Specific rate control mode related tests
3517 return force_recode;
3520 static void encode_frame_to_data_rate
3523 unsigned long *size,
3524 unsigned char *dest,
3525 unsigned int *frame_flags
3529 int frame_over_shoot_limit;
3530 int frame_under_shoot_limit;
3540 int zbin_oq_low = 0;
3543 VP8_COMMON *cm = &cpi->common;
3544 int active_worst_qchanged = FALSE;
3546 int overshoot_seen = FALSE;
3547 int undershoot_seen = FALSE;
3548 int drop_mark = cpi->oxcf.drop_frames_water_mark * cpi->oxcf.optimal_buffer_level / 100;
3549 int drop_mark75 = drop_mark * 2 / 3;
3550 int drop_mark50 = drop_mark / 4;
3551 int drop_mark25 = drop_mark / 8;
3553 // Clear down mmx registers to allow floating point in what follows
3554 vp8_clear_system_state();
3556 // Test code for segmentation of gf/arf (0,0)
3557 //segmentation_test_function((VP8_PTR) cpi);
3559 // For an alt ref frame in 2 pass we skip the call to the second pass function that sets the target bandwidth
3560 #if !(CONFIG_REALTIME_ONLY)
3564 if (cpi->common.refresh_alt_ref_frame)
3566 cpi->per_frame_bandwidth = cpi->gf_bits; // Per frame bit target for the alt ref frame
3567 cpi->target_bandwidth = cpi->gf_bits * cpi->output_frame_rate; // per second target bitrate
3572 cpi->per_frame_bandwidth = (int)(cpi->target_bandwidth / cpi->output_frame_rate);
3574 // Default turn off buffer to buffer copying
3575 cm->copy_buffer_to_gf = 0;
3576 cm->copy_buffer_to_arf = 0;
3578 // Clear zbin over-quant value and mode boost values.
3579 cpi->zbin_over_quant = 0;
3580 cpi->zbin_mode_boost = 0;
3582 // Enable or disable mode based tweaking of the zbin
3583 // For 2 Pass Only used where GF/ARF prediction quality
3584 // is above a threshold
3585 cpi->zbin_mode_boost = 0;
3586 cpi->zbin_mode_boost_enabled = TRUE;
3589 if ( cpi->gfu_boost <= 400 )
3591 cpi->zbin_mode_boost_enabled = FALSE;
3595 // Current default encoder behaviour for the altref sign bias
3596 if (cpi->source_alt_ref_active)
3597 cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = 1;
3599 cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = 0;
3601 // Check to see if a key frame is signalled
3602 // For two pass with auto key frame enabled cm->frame_type may already be set, but not for one pass.
3603 if ((cm->current_video_frame == 0) ||
3604 (cm->frame_flags & FRAMEFLAGS_KEY) ||
3605 (cpi->oxcf.auto_key && (cpi->frames_since_key % cpi->key_frame_frequency == 0)))
3607 // Key frame from VFW/auto-keyframe/first frame
3608 cm->frame_type = KEY_FRAME;
3611 // Set default state for segment and mode based loop filter update flags
3612 cpi->mb.e_mbd.update_mb_segmentation_map = 0;
3613 cpi->mb.e_mbd.update_mb_segmentation_data = 0;
3614 cpi->mb.e_mbd.mode_ref_lf_delta_update = 0;
3616 // Set various flags etc to special state if it is a key frame
3617 if (cm->frame_type == KEY_FRAME)
3621 // Reset the loop filter deltas and segmentation map
3622 setup_features(cpi);
3624 // If segmentation is enabled force a map update for key frames
3625 if (cpi->mb.e_mbd.segmentation_enabled)
3627 cpi->mb.e_mbd.update_mb_segmentation_map = 1;
3628 cpi->mb.e_mbd.update_mb_segmentation_data = 1;
3631 // The alternate reference frame cannot be active for a key frame
3632 cpi->source_alt_ref_active = FALSE;
3634 // Reset the RD threshold multipliers to default of * 1 (128)
3635 for (i = 0; i < MAX_MODES; i++)
3637 cpi->rd_thresh_mult[i] = 128;
3641 // Test code for segmentation
3642 //if ( (cm->frame_type == KEY_FRAME) || ((cm->current_video_frame % 2) == 0))
3643 //if ( (cm->current_video_frame % 2) == 0 )
3644 // enable_segmentation((VP8_PTR)cpi);
3646 // disable_segmentation((VP8_PTR)cpi);
3649 // Experimental code for lagged compress and one pass
3650 // Initialise one_pass GF frames stats
3651 // Update stats used for GF selection
3652 //if ( cpi->pass == 0 )
3654 cpi->one_pass_frame_index = cm->current_video_frame % MAX_LAG_BUFFERS;
3656 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frames_so_far = 0;
3657 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_intra_error = 0.0;
3658 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_coded_error = 0.0;
3659 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_pcnt_inter = 0.0;
3660 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_pcnt_motion = 0.0;
3661 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvr = 0.0;
3662 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvr_abs = 0.0;
3663 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvc = 0.0;
3664 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvc_abs = 0.0;
3668 update_rd_ref_frame_probs(cpi);
3670 if (cpi->drop_frames_allowed)
3672 // The reset to decimation 0 is only done here for one pass.
3673 // Once it is set two pass leaves decimation on till the next kf.
3674 if ((cpi->buffer_level > drop_mark) && (cpi->decimation_factor > 0))
3675 cpi->decimation_factor --;
3677 if (cpi->buffer_level > drop_mark75 && cpi->decimation_factor > 0)
3678 cpi->decimation_factor = 1;
3680 else if (cpi->buffer_level < drop_mark25 && (cpi->decimation_factor == 2 || cpi->decimation_factor == 3))
3682 cpi->decimation_factor = 3;
3684 else if (cpi->buffer_level < drop_mark50 && (cpi->decimation_factor == 1 || cpi->decimation_factor == 2))
3686 cpi->decimation_factor = 2;
3688 else if (cpi->buffer_level < drop_mark75 && (cpi->decimation_factor == 0 || cpi->decimation_factor == 1))
3690 cpi->decimation_factor = 1;
3693 //vpx_log("Encoder: Decimation Factor: %d \n",cpi->decimation_factor);
3696 // The following decimates the frame rate according to a regular pattern (i.e. to 1/2 or 2/3 frame rate)
3697 // This can be used to help prevent buffer under-run in CBR mode. Alternatively it might be desirable in
3698 // some situations to drop frame rate but throw more bits at each frame.
3700 // Note that dropping a key frame can be problematic if spatial resampling is also active
3701 if (cpi->decimation_factor > 0)
3703 switch (cpi->decimation_factor)
3706 cpi->per_frame_bandwidth = cpi->per_frame_bandwidth * 3 / 2;
3709 cpi->per_frame_bandwidth = cpi->per_frame_bandwidth * 5 / 4;
3712 cpi->per_frame_bandwidth = cpi->per_frame_bandwidth * 5 / 4;
3716 // Note that we should not throw out a key frame (especially when spatial resampling is enabled).
3717 if ((cm->frame_type == KEY_FRAME)) // && cpi->oxcf.allow_spatial_resampling )
3719 cpi->decimation_count = cpi->decimation_factor;
3721 else if (cpi->decimation_count > 0)
3723 cpi->decimation_count --;
3724 cpi->bits_off_target += cpi->av_per_frame_bandwidth;
3725 cm->current_video_frame++;
3726 cpi->frames_since_key++;
3732 cpi->buffer_level = cpi->bits_off_target;
3737 cpi->decimation_count = cpi->decimation_factor;
3740 // Decide how big to make the frame
3741 if (!pick_frame_size(cpi))
3743 cm->current_video_frame++;
3744 cpi->frames_since_key++;
3748 // Reduce active_worst_allowed_q for CBR if our buffer is getting too full.
3749 // This has a knock on effect on active best quality as well.
3750 // For CBR if the buffer reaches its maximum level then we can no longer
3751 // save up bits for later frames so we might as well use them up
3752 // on the current frame.
3753 if ((cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) &&
3754 (cpi->buffer_level >= cpi->oxcf.optimal_buffer_level) && cpi->buffered_mode)
3756 int Adjustment = cpi->active_worst_quality / 4; // Max adjustment is 1/4
3761 int tmp_lvl = cpi->buffer_level;
3763 if (cpi->buffer_level < cpi->oxcf.maximum_buffer_size)
3765 buff_lvl_step = (cpi->oxcf.maximum_buffer_size - cpi->oxcf.optimal_buffer_level) / Adjustment;
3769 Adjustment = (cpi->buffer_level - cpi->oxcf.optimal_buffer_level) / buff_lvl_step;
3770 cpi->active_worst_quality -= Adjustment;
3775 cpi->active_worst_quality -= Adjustment;
3780 // Set an active best quality and if necessary active worst quality
3781 if (cpi->pass == 2 || (cm->current_video_frame > 150))
3788 vp8_clear_system_state();
3790 Q = cpi->active_worst_quality;
3792 if ((cm->frame_type == KEY_FRAME) || cm->refresh_golden_frame || cpi->common.refresh_alt_ref_frame)
3794 if (cm->frame_type != KEY_FRAME)
3796 if (cpi->avg_frame_qindex < cpi->active_worst_quality)
3797 Q = cpi->avg_frame_qindex;
3799 if ( cpi->gfu_boost > 1000 )
3800 cpi->active_best_quality = gf_low_motion_minq[Q];
3801 else if ( cpi->gfu_boost < 400 )
3802 cpi->active_best_quality = gf_high_motion_minq[Q];
3804 cpi->active_best_quality = gf_mid_motion_minq[Q];
3806 /*cpi->active_best_quality = gf_arf_minq[Q];
3807 tmp = (cpi->gfu_boost > 1000) ? 600 : cpi->gfu_boost - 400;
3808 //tmp = (cpi->gfu_boost > 1000) ? 600 :
3809 //(cpi->gfu_boost < 400) ? 0 : cpi->gfu_boost - 400;
3810 tmp = 128 - (tmp >> 4);
3811 cpi->active_best_quality = (cpi->active_best_quality * tmp)>>7;*/
3817 if (cpi->gfu_boost > 600)
3818 cpi->active_best_quality = kf_low_motion_minq[Q];
3820 cpi->active_best_quality = kf_high_motion_minq[Q];
3825 cpi->active_best_quality = inter_minq[Q];
3828 // If CBR and the buffer is as full then it is reasonable to allow higher quality on the frames
3829 // to prevent bits just going to waste.
3830 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
3832 // Note that the use of >= here elliminates the risk of a devide by 0 error in the else if clause
3833 if (cpi->buffer_level >= cpi->oxcf.maximum_buffer_size)
3834 cpi->active_best_quality = cpi->best_quality;
3836 else if (cpi->buffer_level > cpi->oxcf.optimal_buffer_level)
3838 int Fraction = ((cpi->buffer_level - cpi->oxcf.optimal_buffer_level) * 128) / (cpi->oxcf.maximum_buffer_size - cpi->oxcf.optimal_buffer_level);
3839 int min_qadjustment = ((cpi->active_best_quality - cpi->best_quality) * Fraction) / 128;
3841 cpi->active_best_quality -= min_qadjustment;
3847 // Clip the active best and worst quality values to limits
3848 if (cpi->active_worst_quality > cpi->worst_quality)
3849 cpi->active_worst_quality = cpi->worst_quality;
3851 if (cpi->active_best_quality < cpi->best_quality)
3852 cpi->active_best_quality = cpi->best_quality;
3853 else if (cpi->active_best_quality > cpi->active_worst_quality)
3854 cpi->active_best_quality = cpi->active_worst_quality;
3856 // Determine initial Q to try
3857 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
3858 last_zbin_oq = cpi->zbin_over_quant;
3860 // Set highest allowed value for Zbin over quant
3861 if (cm->frame_type == KEY_FRAME)
3862 zbin_oq_high = 0; //ZBIN_OQ_MAX/16
3863 else if (cm->refresh_alt_ref_frame || (cm->refresh_golden_frame && !cpi->source_alt_ref_active))
3866 zbin_oq_high = ZBIN_OQ_MAX;
3868 // Setup background Q adjustment for error resilliant mode
3869 if (cpi->cyclic_refresh_mode_enabled)
3870 cyclic_background_refresh(cpi, Q, 0);
3872 vp8_compute_frame_size_bounds(cpi, &frame_under_shoot_limit, &frame_over_shoot_limit);
3874 // Limit Q range for the adaptive loop.
3875 bottom_index = cpi->active_best_quality;
3876 top_index = cpi->active_worst_quality;
3877 q_low = cpi->active_best_quality;
3878 q_high = cpi->active_worst_quality;
3880 vp8_save_coding_context(cpi);
3885 scale_and_extend_source(cpi->un_scaled_source, cpi);
3886 #if !(CONFIG_REALTIME_ONLY) && CONFIG_POSTPROC
3888 if (cpi->oxcf.noise_sensitivity > 0)
3893 switch (cpi->oxcf.noise_sensitivity)
3916 if (cm->frame_type == KEY_FRAME)
3918 vp8_de_noise(cpi->Source, cpi->Source, l , 1, 0, RTCD(postproc));
3922 vp8_de_noise(cpi->Source, cpi->Source, l , 1, 0, RTCD(postproc));
3924 src = cpi->Source->y_buffer;
3926 if (cpi->Source->y_stride < 0)
3928 src += cpi->Source->y_stride * (cpi->Source->y_height - 1);
3935 #ifdef OUTPUT_YUV_SRC
3936 vp8_write_yuv_frame(cpi->Source);
3941 vp8_clear_system_state(); //__asm emms;
3944 if(cpi->is_src_frame_alt_ref)
3948 set_quantizer(cpi, Q);
3951 // setup skip prob for costing in mode/mv decision
3952 if (cpi->common.mb_no_coeff_skip)
3954 cpi->prob_skip_false = cpi->base_skip_false_prob[Q];
3956 if (cm->frame_type != KEY_FRAME)
3958 if (cpi->common.refresh_alt_ref_frame)
3960 if (cpi->last_skip_false_probs[2] != 0)
3961 cpi->prob_skip_false = cpi->last_skip_false_probs[2];
3964 if(cpi->last_skip_false_probs[2]!=0 && abs(Q- cpi->last_skip_probs_q[2])<=16 )
3965 cpi->prob_skip_false = cpi->last_skip_false_probs[2];
3966 else if (cpi->last_skip_false_probs[2]!=0)
3967 cpi->prob_skip_false = (cpi->last_skip_false_probs[2] + cpi->prob_skip_false ) / 2;
3970 else if (cpi->common.refresh_golden_frame)
3972 if (cpi->last_skip_false_probs[1] != 0)
3973 cpi->prob_skip_false = cpi->last_skip_false_probs[1];
3976 if(cpi->last_skip_false_probs[1]!=0 && abs(Q- cpi->last_skip_probs_q[1])<=16 )
3977 cpi->prob_skip_false = cpi->last_skip_false_probs[1];
3978 else if (cpi->last_skip_false_probs[1]!=0)
3979 cpi->prob_skip_false = (cpi->last_skip_false_probs[1] + cpi->prob_skip_false ) / 2;
3984 if (cpi->last_skip_false_probs[0] != 0)
3985 cpi->prob_skip_false = cpi->last_skip_false_probs[0];
3988 if(cpi->last_skip_false_probs[0]!=0 && abs(Q- cpi->last_skip_probs_q[0])<=16 )
3989 cpi->prob_skip_false = cpi->last_skip_false_probs[0];
3990 else if(cpi->last_skip_false_probs[0]!=0)
3991 cpi->prob_skip_false = (cpi->last_skip_false_probs[0] + cpi->prob_skip_false ) / 2;
3995 //as this is for cost estimate, let's make sure it does not go extreme eitehr way
3996 if (cpi->prob_skip_false < 5)
3997 cpi->prob_skip_false = 5;
3999 if (cpi->prob_skip_false > 250)
4000 cpi->prob_skip_false = 250;
4002 if (cpi->is_src_frame_alt_ref)
4003 cpi->prob_skip_false = 1;
4012 FILE *f = fopen("skip.stt", "a");
4013 fprintf(f, "%d, %d, %4d ", cpi->common.refresh_golden_frame, cpi->common.refresh_alt_ref_frame, cpi->prob_skip_false);
4021 if (cm->frame_type == KEY_FRAME)
4022 vp8_setup_key_frame(cpi);
4024 // transform / motion compensation build reconstruction frame
4026 vp8_encode_frame(cpi);
4027 cpi->projected_frame_size -= vp8_estimate_entropy_savings(cpi);
4028 cpi->projected_frame_size = (cpi->projected_frame_size > 0) ? cpi->projected_frame_size : 0;
4030 vp8_clear_system_state(); //__asm emms;
4032 // Test to see if the stats generated for this frame indicate that we should have coded a key frame
4033 // (assuming that we didn't)!
4034 if (cpi->pass != 2 && cpi->oxcf.auto_key && cm->frame_type != KEY_FRAME)
4036 if (decide_key_frame(cpi))
4038 vp8_calc_auto_iframe_target_size(cpi);
4040 // Reset all our sizing numbers and recode
4041 cm->frame_type = KEY_FRAME;
4043 // Clear the Alt reference frame active flag when we have a key frame
4044 cpi->source_alt_ref_active = FALSE;
4046 // Reset the loop filter deltas and segmentation map
4047 setup_features(cpi);
4049 // If segmentation is enabled force a map update for key frames
4050 if (cpi->mb.e_mbd.segmentation_enabled)
4052 cpi->mb.e_mbd.update_mb_segmentation_map = 1;
4053 cpi->mb.e_mbd.update_mb_segmentation_data = 1;
4056 vp8_restore_coding_context(cpi);
4058 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4060 vp8_compute_frame_size_bounds(cpi, &frame_under_shoot_limit, &frame_over_shoot_limit);
4062 // Limit Q range for the adaptive loop.
4063 bottom_index = cpi->active_best_quality;
4064 top_index = cpi->active_worst_quality;
4065 q_low = cpi->active_best_quality;
4066 q_high = cpi->active_worst_quality;
4071 resize_key_frame(cpi);
4076 vp8_clear_system_state();
4078 if (frame_over_shoot_limit == 0)
4079 frame_over_shoot_limit = 1;
4081 // Are we are overshooting and up against the limit of active max Q.
4082 if (((cpi->pass != 2) || (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)) &&
4083 (Q == cpi->active_worst_quality) &&
4084 (cpi->active_worst_quality < cpi->worst_quality) &&
4085 (cpi->projected_frame_size > frame_over_shoot_limit))
4087 int over_size_percent = ((cpi->projected_frame_size - frame_over_shoot_limit) * 100) / frame_over_shoot_limit;
4089 // If so is there any scope for relaxing it
4090 while ((cpi->active_worst_quality < cpi->worst_quality) && (over_size_percent > 0))
4092 cpi->active_worst_quality++;
4093 top_index = cpi->active_worst_quality;
4094 over_size_percent = (int)(over_size_percent * 0.96); // Assume 1 qstep = about 4% on frame size.
4097 // If we have updated the active max Q do not call vp8_update_rate_correction_factors() this loop.
4098 active_worst_qchanged = TRUE;
4101 active_worst_qchanged = FALSE;
4103 #if !(CONFIG_REALTIME_ONLY)
4105 // Is the projected frame size out of range and are we allowed to attempt to recode.
4106 if ( recode_loop_test( cpi,
4107 frame_over_shoot_limit, frame_under_shoot_limit,
4108 Q, top_index, bottom_index ) )
4113 // Frame size out of permitted range:
4114 // Update correction factor & compute new Q to try...
4116 // Frame is too large
4117 if (cpi->projected_frame_size > cpi->this_frame_target)
4119 //if ( cpi->zbin_over_quant == 0 )
4120 q_low = (Q < q_high) ? (Q + 1) : q_high; // Raise Qlow as to at least the current value
4122 if (cpi->zbin_over_quant > 0) // If we are using over quant do the same for zbin_oq_low
4123 zbin_oq_low = (cpi->zbin_over_quant < zbin_oq_high) ? (cpi->zbin_over_quant + 1) : zbin_oq_high;
4125 //if ( undershoot_seen || (Q == MAXQ) )
4126 if (undershoot_seen)
4128 // Update rate_correction_factor unless cpi->active_worst_quality has changed.
4129 if (!active_worst_qchanged)
4130 vp8_update_rate_correction_factors(cpi, 1);
4132 Q = (q_high + q_low + 1) / 2;
4134 // Adjust cpi->zbin_over_quant (only allowed when Q is max)
4136 cpi->zbin_over_quant = 0;
4139 zbin_oq_low = (cpi->zbin_over_quant < zbin_oq_high) ? (cpi->zbin_over_quant + 1) : zbin_oq_high;
4140 cpi->zbin_over_quant = (zbin_oq_high + zbin_oq_low) / 2;
4145 // Update rate_correction_factor unless cpi->active_worst_quality has changed.
4146 if (!active_worst_qchanged)
4147 vp8_update_rate_correction_factors(cpi, 0);
4149 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4151 while (((Q < q_low) || (cpi->zbin_over_quant < zbin_oq_low)) && (Retries < 10))
4153 vp8_update_rate_correction_factors(cpi, 0);
4154 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4159 overshoot_seen = TRUE;
4161 // Frame is too small
4164 if (cpi->zbin_over_quant == 0)
4165 q_high = (Q > q_low) ? (Q - 1) : q_low; // Lower q_high if not using over quant
4166 else // else lower zbin_oq_high
4167 zbin_oq_high = (cpi->zbin_over_quant > zbin_oq_low) ? (cpi->zbin_over_quant - 1) : zbin_oq_low;
4171 // Update rate_correction_factor unless cpi->active_worst_quality has changed.
4172 if (!active_worst_qchanged)
4173 vp8_update_rate_correction_factors(cpi, 1);
4175 Q = (q_high + q_low) / 2;
4177 // Adjust cpi->zbin_over_quant (only allowed when Q is max)
4179 cpi->zbin_over_quant = 0;
4181 cpi->zbin_over_quant = (zbin_oq_high + zbin_oq_low) / 2;
4185 // Update rate_correction_factor unless cpi->active_worst_quality has changed.
4186 if (!active_worst_qchanged)
4187 vp8_update_rate_correction_factors(cpi, 0);
4189 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4191 while (((Q > q_high) || (cpi->zbin_over_quant > zbin_oq_high)) && (Retries < 10))
4193 vp8_update_rate_correction_factors(cpi, 0);
4194 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4199 undershoot_seen = TRUE;
4202 // Clamp Q to upper and lower limits:
4208 // Clamp cpi->zbin_over_quant
4209 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;
4211 //Loop = ((Q != last_q) || (last_zbin_oq != cpi->zbin_over_quant)) ? TRUE : FALSE;
4212 Loop = ((Q != last_q)) ? TRUE : FALSE;
4213 last_zbin_oq = cpi->zbin_over_quant;
4219 if (cpi->is_src_frame_alt_ref)
4224 vp8_restore_coding_context(cpi);
4227 cpi->tot_recode_hits++;
4231 while (Loop == TRUE);
4234 // Experimental code for lagged and one pass
4235 // Update stats used for one pass GF selection
4239 double frame_intra_error;
4240 double frame_coded_error;
4241 double frame_pcnt_inter;
4242 double frame_pcnt_motion;
4244 double frame_mvr_abs;
4246 double frame_mvc_abs;
4249 cpi->one_pass_frame_stats[cpi->one_pass_frame_index].frame_coded_error = (double)cpi->prediction_error;
4250 cpi->one_pass_frame_stats[cpi->one_pass_frame_index].frame_intra_error = (double)cpi->intra_error;
4251 cpi->one_pass_frame_stats[cpi->one_pass_frame_index].frame_pcnt_inter = (double)(100 - cpi->this_frame_percent_intra) / 100.0;
4255 // Update the GF useage maps.
4256 // This is done after completing the compression of a frame when all modes etc. are finalized but before loop filter
4257 vp8_update_gf_useage_maps(cpi, cm, &cpi->mb);
4259 // This frame's MVs are saved and will be used in next frame's MV prediction.
4260 if(cm->show_frame) //do not save for altref frame
4264 MODE_INFO *tmp = cm->mip; //point to beginning of allocated MODE_INFO arrays.
4265 //static int last_video_frame = 0;
4267 if(cm->frame_type != KEY_FRAME)
4269 for (mb_row = 0; mb_row < cm->mb_rows+1; mb_row ++)
4271 for (mb_col = 0; mb_col < cm->mb_cols+1; mb_col ++)
4273 if(tmp->mbmi.ref_frame != INTRA_FRAME)
4274 cpi->lfmv[mb_col + mb_row*(cm->mode_info_stride)].as_int = tmp->mbmi.mv.as_int;
4276 cpi->lf_ref_frame_sign_bias[mb_col + mb_row*(cm->mode_info_stride)] = cm->ref_frame_sign_bias[tmp->mbmi.ref_frame];
4277 cpi->lf_ref_frame[mb_col + mb_row*(cm->mode_info_stride)] = tmp->mbmi.ref_frame;
4285 // Update the GF useage maps.
4286 // This is done after completing the compression of a frame when all modes etc. are finalized but before loop filter
4287 vp8_update_gf_useage_maps(cpi, cm, &cpi->mb);
4289 if (cm->frame_type == KEY_FRAME)
4290 cm->refresh_last_frame = 1;
4294 FILE *f = fopen("gfactive.stt", "a");
4295 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);
4300 // 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
4301 // This is purely an encoder descision at present.
4302 if (!cpi->oxcf.error_resilient_mode && cm->refresh_golden_frame)
4303 cm->copy_buffer_to_arf = 2;
4305 cm->copy_buffer_to_arf = 0;
4307 if (cm->refresh_last_frame)
4309 vp8_swap_yv12_buffer(&cm->yv12_fb[cm->lst_fb_idx], &cm->yv12_fb[cm->new_fb_idx]);
4310 cm->frame_to_show = &cm->yv12_fb[cm->lst_fb_idx];
4313 cm->frame_to_show = &cm->yv12_fb[cm->new_fb_idx];
4317 //#pragma omp parallel sections
4320 //#pragma omp section
4323 struct vpx_usec_timer timer;
4325 vpx_usec_timer_start(&timer);
4327 if (cpi->sf.auto_filter == 0)
4328 vp8cx_pick_filter_level_fast(cpi->Source, cpi);
4330 vp8cx_pick_filter_level(cpi->Source, cpi);
4332 vpx_usec_timer_mark(&timer);
4334 cpi->time_pick_lpf += vpx_usec_timer_elapsed(&timer);
4337 cm->filter_level = 0;
4339 if (cm->filter_level > 0)
4341 vp8cx_set_alt_lf_level(cpi, cm->filter_level);
4342 vp8_loop_filter_frame(cm, &cpi->mb.e_mbd, cm->filter_level);
4343 cm->last_frame_type = cm->frame_type;
4344 cm->last_filter_type = cm->filter_type;
4345 cm->last_sharpness_level = cm->sharpness_level;
4348 vp8_yv12_extend_frame_borders_ptr(cm->frame_to_show);
4350 if (cpi->oxcf.error_resilient_mode == 1)
4352 cm->refresh_entropy_probs = 0;
4356 //#pragma omp section
4358 // build the bitstream
4359 vp8_pack_bitstream(cpi, dest, size);
4364 YV12_BUFFER_CONFIG *lst_yv12 = &cm->yv12_fb[cm->lst_fb_idx];
4365 YV12_BUFFER_CONFIG *new_yv12 = &cm->yv12_fb[cm->new_fb_idx];
4366 YV12_BUFFER_CONFIG *gld_yv12 = &cm->yv12_fb[cm->gld_fb_idx];
4367 YV12_BUFFER_CONFIG *alt_yv12 = &cm->yv12_fb[cm->alt_fb_idx];
4368 // At this point the new frame has been encoded coded.
4369 // If any buffer copy / swaping is signalled it should be done here.
4370 if (cm->frame_type == KEY_FRAME)
4372 vp8_yv12_copy_frame_ptr(cm->frame_to_show, gld_yv12);
4373 vp8_yv12_copy_frame_ptr(cm->frame_to_show, alt_yv12);
4375 else // For non key frames
4377 // Code to copy between reference buffers
4378 if (cm->copy_buffer_to_arf)
4380 if (cm->copy_buffer_to_arf == 1)
4382 if (cm->refresh_last_frame)
4383 // We copy new_frame here because last and new buffers will already have been swapped if cm->refresh_last_frame is set.
4384 vp8_yv12_copy_frame_ptr(new_yv12, alt_yv12);
4386 vp8_yv12_copy_frame_ptr(lst_yv12, alt_yv12);
4388 else if (cm->copy_buffer_to_arf == 2)
4389 vp8_yv12_copy_frame_ptr(gld_yv12, alt_yv12);
4392 if (cm->copy_buffer_to_gf)
4394 if (cm->copy_buffer_to_gf == 1)
4396 if (cm->refresh_last_frame)
4397 // We copy new_frame here because last and new buffers will already have been swapped if cm->refresh_last_frame is set.
4398 vp8_yv12_copy_frame_ptr(new_yv12, gld_yv12);
4400 vp8_yv12_copy_frame_ptr(lst_yv12, gld_yv12);
4402 else if (cm->copy_buffer_to_gf == 2)
4403 vp8_yv12_copy_frame_ptr(alt_yv12, gld_yv12);
4408 // Update rate control heuristics
4409 cpi->total_byte_count += (*size);
4410 cpi->projected_frame_size = (*size) << 3;
4412 if (!active_worst_qchanged)
4413 vp8_update_rate_correction_factors(cpi, 2);
4415 cpi->last_q[cm->frame_type] = cm->base_qindex;
4417 if (cm->frame_type == KEY_FRAME)
4419 vp8_adjust_key_frame_context(cpi);
4422 // Keep a record of ambient average Q.
4423 if (cm->frame_type == KEY_FRAME)
4424 cpi->avg_frame_qindex = cm->base_qindex;
4426 cpi->avg_frame_qindex = (2 + 3 * cpi->avg_frame_qindex + cm->base_qindex) >> 2;
4428 // Keep a record from which we can calculate the average Q excluding GF updates and key frames
4429 if ((cm->frame_type != KEY_FRAME) && !cm->refresh_golden_frame && !cm->refresh_alt_ref_frame)
4433 // Calculate the average Q for normal inter frames (not key or GFU frames)
4434 // This is used as a basis for setting active worst quality.
4435 if (cpi->ni_frames > 150)
4437 cpi->ni_tot_qi += Q;
4438 cpi->ni_av_qi = (cpi->ni_tot_qi / cpi->ni_frames);
4440 // Early in the clip ... average the current frame Q value with the default
4441 // entered by the user as a dampening measure
4444 cpi->ni_tot_qi += Q;
4445 cpi->ni_av_qi = ((cpi->ni_tot_qi / cpi->ni_frames) + cpi->worst_quality + 1) / 2;
4448 // If the average Q is higher than what was used in the last frame
4449 // (after going through the recode loop to keep the frame size within range)
4450 // then use the last frame value - 1.
4451 // The -1 is designed to stop Q and hence the data rate, from progressively
4452 // falling away during difficult sections, but at the same time reduce the number of
4453 // itterations around the recode loop.
4454 if (Q > cpi->ni_av_qi)
4455 cpi->ni_av_qi = Q - 1;
4461 // If the frame was massively oversize and we are below optimal buffer level drop next frame
4462 if ((cpi->drop_frames_allowed) &&
4463 (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) &&
4464 (cpi->buffer_level < cpi->oxcf.drop_frames_water_mark * cpi->oxcf.optimal_buffer_level / 100) &&
4465 (cpi->projected_frame_size > (4 * cpi->this_frame_target)))
4467 cpi->drop_frame = TRUE;
4472 // Set the count for maximum consequative dropped frames based upon the ratio of
4473 // this frame size to the target average per frame bandwidth.
4474 // (cpi->av_per_frame_bandwidth > 0) is just a sanity check to prevent / 0.
4475 if (cpi->drop_frames_allowed && (cpi->av_per_frame_bandwidth > 0))
4477 cpi->max_drop_count = cpi->projected_frame_size / cpi->av_per_frame_bandwidth;
4479 if (cpi->max_drop_count > cpi->max_consec_dropped_frames)
4480 cpi->max_drop_count = cpi->max_consec_dropped_frames;
4483 // Update the buffer level variable.
4484 if (cpi->common.refresh_alt_ref_frame)
4485 cpi->bits_off_target -= cpi->projected_frame_size;
4487 cpi->bits_off_target += cpi->av_per_frame_bandwidth - cpi->projected_frame_size;
4489 // Rolling monitors of whether we are over or underspending used to help regulate min and Max Q in two pass.
4490 cpi->rolling_target_bits = ((cpi->rolling_target_bits * 3) + cpi->this_frame_target + 2) / 4;
4491 cpi->rolling_actual_bits = ((cpi->rolling_actual_bits * 3) + cpi->projected_frame_size + 2) / 4;
4492 cpi->long_rolling_target_bits = ((cpi->long_rolling_target_bits * 31) + cpi->this_frame_target + 16) / 32;
4493 cpi->long_rolling_actual_bits = ((cpi->long_rolling_actual_bits * 31) + cpi->projected_frame_size + 16) / 32;
4495 // Actual bits spent
4496 cpi->total_actual_bits += cpi->projected_frame_size;
4499 cpi->total_target_vs_actual += (cpi->this_frame_target - cpi->projected_frame_size);
4501 cpi->buffer_level = cpi->bits_off_target;
4503 // Update bits left to the kf and gf groups to account for overshoot or undershoot on these frames
4504 if (cm->frame_type == KEY_FRAME)
4506 cpi->kf_group_bits += cpi->this_frame_target - cpi->projected_frame_size;
4508 if (cpi->kf_group_bits < 0)
4509 cpi->kf_group_bits = 0 ;
4511 else if (cm->refresh_golden_frame || cm->refresh_alt_ref_frame)
4513 cpi->gf_group_bits += cpi->this_frame_target - cpi->projected_frame_size;
4515 if (cpi->gf_group_bits < 0)
4516 cpi->gf_group_bits = 0 ;
4519 if (cm->frame_type != KEY_FRAME)
4521 if (cpi->common.refresh_alt_ref_frame)
4523 cpi->last_skip_false_probs[2] = cpi->prob_skip_false;
4524 cpi->last_skip_probs_q[2] = cm->base_qindex;
4526 else if (cpi->common.refresh_golden_frame)
4528 cpi->last_skip_false_probs[1] = cpi->prob_skip_false;
4529 cpi->last_skip_probs_q[1] = cm->base_qindex;
4533 cpi->last_skip_false_probs[0] = cpi->prob_skip_false;
4534 cpi->last_skip_probs_q[0] = cm->base_qindex;
4536 //update the baseline
4537 cpi->base_skip_false_prob[cm->base_qindex] = cpi->prob_skip_false;
4542 #if 0 && CONFIG_PSNR
4544 FILE *f = fopen("tmp.stt", "a");
4546 vp8_clear_system_state(); //__asm emms;
4548 if (cpi->total_coded_error_left != 0.0)
4549 fprintf(f, "%10d %10d %10d %10d %10d %10d %10d %10d %6ld %6ld"
4550 "%6ld %6ld %5ld %5ld %5ld %8ld %8.2f %10d %10.3f"
4552 cpi->common.current_video_frame, cpi->this_frame_target,
4553 cpi->projected_frame_size,
4554 (cpi->projected_frame_size - cpi->this_frame_target),
4555 (int)cpi->total_target_vs_actual,
4556 (cpi->oxcf.starting_buffer_level-cpi->bits_off_target),
4557 (int)cpi->total_actual_bits, cm->base_qindex,
4558 cpi->active_best_quality, cpi->active_worst_quality,
4559 cpi->avg_frame_qindex, cpi->zbin_over_quant,
4560 cm->refresh_golden_frame, cm->refresh_alt_ref_frame,
4561 cm->frame_type, cpi->gfu_boost,
4562 cpi->est_max_qcorrection_factor, (int)cpi->bits_left,
4563 cpi->total_coded_error_left,
4564 (double)cpi->bits_left / cpi->total_coded_error_left,
4565 cpi->tot_recode_hits);
4567 fprintf(f, "%10d %10d %10d %10d %10d %10d %10d %10d %6ld %6ld"
4568 "%6ld %6ld %5ld %5ld %5ld %8ld %8.2f %10d %10.3f"
4570 cpi->common.current_video_frame,
4571 cpi->this_frame_target, cpi->projected_frame_size,
4572 (cpi->projected_frame_size - cpi->this_frame_target),
4573 (int)cpi->total_target_vs_actual,
4574 (cpi->oxcf.starting_buffer_level-cpi->bits_off_target),
4575 (int)cpi->total_actual_bits, cm->base_qindex,
4576 cpi->active_best_quality, cpi->active_worst_quality,
4577 cpi->avg_frame_qindex, cpi->zbin_over_quant,
4578 cm->refresh_golden_frame, cm->refresh_alt_ref_frame,
4579 cm->frame_type, cpi->gfu_boost,
4580 cpi->est_max_qcorrection_factor, (int)cpi->bits_left,
4581 cpi->total_coded_error_left, cpi->tot_recode_hits);
4586 FILE *fmodes = fopen("Modes.stt", "a");
4589 fprintf(fmodes, "%6d:%1d:%1d:%1d ",
4590 cpi->common.current_video_frame,
4591 cm->frame_type, cm->refresh_golden_frame,
4592 cm->refresh_alt_ref_frame);
4594 for (i = 0; i < MAX_MODES; i++)
4595 fprintf(fmodes, "%5d ", cpi->mode_chosen_counts[i]);
4597 fprintf(fmodes, "\n");
4605 // If this was a kf or Gf note the Q
4606 if ((cm->frame_type == KEY_FRAME) || cm->refresh_golden_frame || cm->refresh_alt_ref_frame)
4607 cm->last_kf_gf_q = cm->base_qindex;
4609 if (cm->refresh_golden_frame == 1)
4610 cm->frame_flags = cm->frame_flags | FRAMEFLAGS_GOLDEN;
4612 cm->frame_flags = cm->frame_flags&~FRAMEFLAGS_GOLDEN;
4614 if (cm->refresh_alt_ref_frame == 1)
4615 cm->frame_flags = cm->frame_flags | FRAMEFLAGS_ALTREF;
4617 cm->frame_flags = cm->frame_flags&~FRAMEFLAGS_ALTREF;
4620 if (cm->refresh_last_frame & cm->refresh_golden_frame) // both refreshed
4621 cpi->gold_is_last = 1;
4622 else if (cm->refresh_last_frame ^ cm->refresh_golden_frame) // 1 refreshed but not the other
4623 cpi->gold_is_last = 0;
4625 if (cm->refresh_last_frame & cm->refresh_alt_ref_frame) // both refreshed
4626 cpi->alt_is_last = 1;
4627 else if (cm->refresh_last_frame ^ cm->refresh_alt_ref_frame) // 1 refreshed but not the other
4628 cpi->alt_is_last = 0;
4630 if (cm->refresh_alt_ref_frame & cm->refresh_golden_frame) // both refreshed
4631 cpi->gold_is_alt = 1;
4632 else if (cm->refresh_alt_ref_frame ^ cm->refresh_golden_frame) // 1 refreshed but not the other
4633 cpi->gold_is_alt = 0;
4635 cpi->ref_frame_flags = VP8_ALT_FLAG | VP8_GOLD_FLAG | VP8_LAST_FLAG;
4637 if (cpi->gold_is_last)
4638 cpi->ref_frame_flags &= ~VP8_GOLD_FLAG;
4640 if (cpi->alt_is_last)
4641 cpi->ref_frame_flags &= ~VP8_ALT_FLAG;
4643 if (cpi->gold_is_alt)
4644 cpi->ref_frame_flags &= ~VP8_ALT_FLAG;
4647 if (cpi->oxcf.error_resilient_mode)
4649 // Is this an alternate reference update
4650 if (cpi->common.refresh_alt_ref_frame)
4651 vp8_yv12_copy_frame_ptr(cm->frame_to_show, &cm->yv12_fb[cm->alt_fb_idx]);
4653 if (cpi->common.refresh_golden_frame)
4654 vp8_yv12_copy_frame_ptr(cm->frame_to_show, &cm->yv12_fb[cm->gld_fb_idx]);
4658 if (cpi->oxcf.play_alternate && cpi->common.refresh_alt_ref_frame && (cpi->common.frame_type != KEY_FRAME))
4659 // Update the alternate reference frame and stats as appropriate.
4660 update_alt_ref_frame_and_stats(cpi);
4662 // Update the Golden frame and golden frame and stats as appropriate.
4663 update_golden_frame_and_stats(cpi);
4666 if (cm->frame_type == KEY_FRAME)
4668 // Tell the caller that the frame was coded as a key frame
4669 *frame_flags = cm->frame_flags | FRAMEFLAGS_KEY;
4671 // As this frame is a key frame the next defaults to an inter frame.
4672 cm->frame_type = INTER_FRAME;
4674 cpi->last_frame_percent_intra = 100;
4678 *frame_flags = cm->frame_flags&~FRAMEFLAGS_KEY;
4680 cpi->last_frame_percent_intra = cpi->this_frame_percent_intra;
4683 // Clear the one shot update flags for segmentation map and mode/ref loop filter deltas.
4684 cpi->mb.e_mbd.update_mb_segmentation_map = 0;
4685 cpi->mb.e_mbd.update_mb_segmentation_data = 0;
4686 cpi->mb.e_mbd.mode_ref_lf_delta_update = 0;
4689 // Dont increment frame counters if this was an altref buffer update not a real frame
4692 cm->current_video_frame++;
4693 cpi->frames_since_key++;
4696 // reset to normal state now that we are done.
4704 sprintf(filename, "enc%04d.yuv", (int) cm->current_video_frame);
4705 recon_file = fopen(filename, "wb");
4706 fwrite(cm->yv12_fb[cm->lst_fb_idx].buffer_alloc,
4707 cm->yv12_fb[cm->lst_fb_idx].frame_size, 1, recon_file);
4713 //vp8_write_yuv_frame("encoder_recon.yuv", cm->frame_to_show);
4718 int vp8_is_gf_update_needed(VP8_PTR ptr)
4720 VP8_COMP *cpi = (VP8_COMP *) ptr;
4723 ret_val = cpi->gf_update_recommended;
4724 cpi->gf_update_recommended = 0;
4729 void vp8_check_gf_quality(VP8_COMP *cpi)
4731 VP8_COMMON *cm = &cpi->common;
4732 int gf_active_pct = (100 * cpi->gf_active_count) / (cm->mb_rows * cm->mb_cols);
4733 int gf_ref_usage_pct = (cpi->count_mb_ref_frame_usage[GOLDEN_FRAME] * 100) / (cm->mb_rows * cm->mb_cols);
4734 int last_ref_zz_useage = (cpi->inter_zz_count * 100) / (cm->mb_rows * cm->mb_cols);
4736 // Gf refresh is not currently being signalled
4737 if (cpi->gf_update_recommended == 0)
4739 if (cpi->common.frames_since_golden > 7)
4742 if ((gf_active_pct < 10) || ((gf_active_pct + gf_ref_usage_pct) < 15))
4744 // ...but last frame zero zero usage is reasonbable so a new gf might be appropriate
4745 if (last_ref_zz_useage >= 25)
4747 cpi->gf_bad_count ++;
4749 if (cpi->gf_bad_count >= 8) // Check that the condition is stable
4751 cpi->gf_update_recommended = 1;
4752 cpi->gf_bad_count = 0;
4756 cpi->gf_bad_count = 0; // Restart count as the background is not stable enough
4759 cpi->gf_bad_count = 0; // Gf useage has picked up so reset count
4762 // If the signal is set but has not been read should we cancel it.
4763 else if (last_ref_zz_useage < 15)
4765 cpi->gf_update_recommended = 0;
4766 cpi->gf_bad_count = 0;
4771 FILE *f = fopen("gfneeded.stt", "a");
4772 fprintf(f, "%10d %10d %10d %10d %10ld \n",
4773 cm->current_video_frame,
4774 cpi->common.frames_since_golden,
4775 gf_active_pct, gf_ref_usage_pct,
4776 cpi->gf_update_recommended);
4783 #if !(CONFIG_REALTIME_ONLY)
4784 static void Pass2Encode(VP8_COMP *cpi, unsigned long *size, unsigned char *dest, unsigned int *frame_flags)
4787 if (!cpi->common.refresh_alt_ref_frame)
4788 vp8_second_pass(cpi);
4790 encode_frame_to_data_rate(cpi, size, dest, frame_flags);
4791 cpi->bits_left -= 8 * *size;
4793 if (!cpi->common.refresh_alt_ref_frame)
4795 double two_pass_min_rate = (double)(cpi->oxcf.target_bandwidth
4796 *cpi->oxcf.two_pass_vbrmin_section / 100);
4797 cpi->bits_left += (long long)(two_pass_min_rate / cpi->oxcf.frame_rate);
4802 //For ARM NEON, d8-d15 are callee-saved registers, and need to be saved by us.
4804 extern void vp8_push_neon(INT64 *store);
4805 extern void vp8_pop_neon(INT64 *store);
4807 int vp8_receive_raw_frame(VP8_PTR ptr, unsigned int frame_flags, YV12_BUFFER_CONFIG *sd, INT64 time_stamp, INT64 end_time)
4810 VP8_COMP *cpi = (VP8_COMP *) ptr;
4811 VP8_COMMON *cm = &cpi->common;
4812 struct vpx_usec_timer timer;
4818 #if CONFIG_RUNTIME_CPU_DETECT
4819 if (cm->rtcd.flags & HAS_NEON)
4822 vp8_push_neon(store_reg);
4826 vpx_usec_timer_start(&timer);
4828 // no more room for frames;
4829 if (cpi->source_buffer_count != 0 && cpi->source_buffer_count >= cpi->oxcf.lag_in_frames)
4832 #if CONFIG_RUNTIME_CPU_DETECT
4833 if (cm->rtcd.flags & HAS_NEON)
4836 vp8_pop_neon(store_reg);
4842 //printf("in-cpi->source_buffer_count: %d\n", cpi->source_buffer_count);
4844 cm->clr_type = sd->clrtype;
4846 // make a copy of the frame for use later...
4847 #if !(CONFIG_REALTIME_ONLY)
4849 if (cpi->oxcf.allow_lag)
4851 int which_buffer = cpi->source_encode_index - 1;
4854 if (which_buffer == -1)
4855 which_buffer = cpi->oxcf.lag_in_frames - 1;
4857 if (cpi->source_buffer_count < cpi->oxcf.lag_in_frames - 1)
4858 which_buffer = cpi->source_buffer_count;
4860 s = &cpi->src_buffer[which_buffer];
4862 s->source_time_stamp = time_stamp;
4863 s->source_end_time_stamp = end_time;
4864 s->source_frame_flags = frame_flags;
4865 vp8_yv12_copy_frame_ptr(sd, &s->source_buffer);
4867 cpi->source_buffer_count ++;
4873 s = &cpi->src_buffer[0];
4874 s->source_end_time_stamp = end_time;
4875 s->source_time_stamp = time_stamp;
4876 s->source_frame_flags = frame_flags;
4878 #if CONFIG_RUNTIME_CPU_DETECT
4879 if (cm->rtcd.flags & HAS_NEON)
4882 vp8_yv12_copy_src_frame_func_neon(sd, &s->source_buffer);
4884 #if CONFIG_RUNTIME_CPU_DETECT
4888 #if !HAVE_ARMV7 || CONFIG_RUNTIME_CPU_DETECT
4890 vp8_yv12_copy_frame_ptr(sd, &s->source_buffer);
4893 cpi->source_buffer_count = 1;
4896 vpx_usec_timer_mark(&timer);
4897 cpi->time_receive_data += vpx_usec_timer_elapsed(&timer);
4900 #if CONFIG_RUNTIME_CPU_DETECT
4901 if (cm->rtcd.flags & HAS_NEON)
4904 vp8_pop_neon(store_reg);
4910 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)
4913 VP8_COMP *cpi = (VP8_COMP *) ptr;
4914 VP8_COMMON *cm = &cpi->common;
4915 struct vpx_usec_timer tsctimer;
4916 struct vpx_usec_timer ticktimer;
4917 struct vpx_usec_timer cmptimer;
4923 #if CONFIG_RUNTIME_CPU_DETECT
4924 if (cm->rtcd.flags & HAS_NEON)
4927 vp8_push_neon(store_reg);
4931 vpx_usec_timer_start(&cmptimer);
4934 // flush variable tells us that even though we have less than 10 frames
4935 // in our buffer we need to start producing compressed frames.
4936 // Probably because we are at the end of a file....
4937 if ((cpi->source_buffer_count == cpi->oxcf.lag_in_frames && cpi->oxcf.lag_in_frames > 0)
4938 || (!cpi->oxcf.allow_lag && cpi->source_buffer_count > 0)
4939 || (flush && cpi->source_buffer_count > 0))
4944 s = &cpi->src_buffer[cpi->source_encode_index];
4945 cpi->source_time_stamp = s->source_time_stamp;
4946 cpi->source_end_time_stamp = s->source_end_time_stamp;
4948 #if !(CONFIG_REALTIME_ONLY)
4950 // Should we code an alternate reference frame
4951 if (cpi->oxcf.error_resilient_mode == 0 &&
4952 cpi->oxcf.play_alternate &&
4953 cpi->source_alt_ref_pending &&
4954 (cpi->frames_till_gf_update_due < cpi->source_buffer_count) &&
4955 cpi->oxcf.lag_in_frames != 0)
4957 cpi->last_alt_ref_sei = (cpi->source_encode_index + cpi->frames_till_gf_update_due) % cpi->oxcf.lag_in_frames;
4959 #if VP8_TEMPORAL_ALT_REF
4961 if (cpi->oxcf.arnr_max_frames > 0)
4964 // my attempt at a loop that tests the results of strength filter.
4965 int start_frame = cpi->last_alt_ref_sei - 3;
4967 int i, besti = -1, pastin = cpi->oxcf.arnr_strength;
4971 if (start_frame < 0)
4972 start_frame += cpi->oxcf.lag_in_frames;
4974 besterr = vp8_calc_low_ss_err(&cpi->src_buffer[cpi->last_alt_ref_sei].source_buffer,
4975 &cpi->src_buffer[start_frame].source_buffer, IF_RTCD(&cpi->rtcd.variance));
4977 for (i = 0; i < 7; i++)
4980 cpi->oxcf.arnr_strength = i;
4981 vp8_temporal_filter_prepare_c(cpi);
4983 thiserr = vp8_calc_low_ss_err(&cpi->alt_ref_buffer.source_buffer,
4984 &cpi->src_buffer[start_frame].source_buffer, IF_RTCD(&cpi->rtcd.variance));
4986 if (10 * thiserr < besterr * 8)
4995 cpi->oxcf.arnr_strength = besti;
4996 vp8_temporal_filter_prepare_c(cpi);
4997 s = &cpi->alt_ref_buffer;
4999 // FWG not sure if I need to copy this data for the Alt Ref frame
5000 s->source_time_stamp = cpi->src_buffer[cpi->last_alt_ref_sei].source_time_stamp;
5001 s->source_end_time_stamp = cpi->src_buffer[cpi->last_alt_ref_sei].source_end_time_stamp;
5002 s->source_frame_flags = cpi->src_buffer[cpi->last_alt_ref_sei].source_frame_flags;
5005 s = &cpi->src_buffer[cpi->last_alt_ref_sei];
5008 vp8_temporal_filter_prepare_c(cpi);
5009 s = &cpi->alt_ref_buffer;
5011 // FWG not sure if I need to copy this data for the Alt Ref frame
5012 s->source_time_stamp = cpi->src_buffer[cpi->last_alt_ref_sei].source_time_stamp;
5013 s->source_end_time_stamp = cpi->src_buffer[cpi->last_alt_ref_sei].source_end_time_stamp;
5014 s->source_frame_flags = cpi->src_buffer[cpi->last_alt_ref_sei].source_frame_flags;
5020 s = &cpi->src_buffer[cpi->last_alt_ref_sei];
5022 cm->frames_till_alt_ref_frame = cpi->frames_till_gf_update_due;
5023 cm->refresh_alt_ref_frame = 1;
5024 cm->refresh_golden_frame = 0;
5025 cm->refresh_last_frame = 0;
5027 cpi->source_alt_ref_pending = FALSE; // Clear Pending altf Ref flag.
5028 cpi->is_src_frame_alt_ref = 0;
5029 cpi->is_next_src_alt_ref = 0;
5035 #if !(CONFIG_REALTIME_ONLY)
5037 if (cpi->oxcf.allow_lag)
5039 if (cpi->source_encode_index == cpi->last_alt_ref_sei)
5041 cpi->is_src_frame_alt_ref = 1;
5042 cpi->last_alt_ref_sei = -1;
5045 cpi->is_src_frame_alt_ref = 0;
5047 cpi->source_encode_index = (cpi->source_encode_index + 1) % cpi->oxcf.lag_in_frames;
5049 if(cpi->source_encode_index == cpi->last_alt_ref_sei)
5050 cpi->is_next_src_alt_ref = 1;
5052 cpi->is_next_src_alt_ref = 0;
5056 cpi->source_buffer_count--;
5059 cpi->un_scaled_source = &s->source_buffer;
5060 cpi->Source = &s->source_buffer;
5061 cpi->source_frame_flags = s->source_frame_flags;
5063 *time_stamp = cpi->source_time_stamp;
5064 *time_end = cpi->source_end_time_stamp;
5069 #if !(CONFIG_REALTIME_ONLY)
5071 if (flush && cpi->pass == 1 && !cpi->first_pass_done)
5073 vp8_end_first_pass(cpi); /* get last stats packet */
5074 cpi->first_pass_done = 1;
5080 #if CONFIG_RUNTIME_CPU_DETECT
5081 if (cm->rtcd.flags & HAS_NEON)
5084 vp8_pop_neon(store_reg);
5090 *frame_flags = cpi->source_frame_flags;
5092 if (cpi->source_time_stamp < cpi->first_time_stamp_ever)
5094 cpi->first_time_stamp_ever = cpi->source_time_stamp;
5095 cpi->last_end_time_stamp_seen = cpi->source_time_stamp;
5098 // adjust frame rates based on timestamps given
5099 if (!cm->refresh_alt_ref_frame)
5101 if (cpi->source_time_stamp == cpi->first_time_stamp_ever)
5103 double this_fps = 10000000.000 / (cpi->source_end_time_stamp - cpi->source_time_stamp);
5105 vp8_new_frame_rate(cpi, this_fps);
5109 long long nanosecs = cpi->source_end_time_stamp
5110 - cpi->last_end_time_stamp_seen;
5111 double this_fps = 10000000.000 / nanosecs;
5113 vp8_new_frame_rate(cpi, (7 * cpi->oxcf.frame_rate + this_fps) / 8);
5117 cpi->last_time_stamp_seen = cpi->source_time_stamp;
5118 cpi->last_end_time_stamp_seen = cpi->source_end_time_stamp;
5121 if (cpi->compressor_speed == 2)
5123 vp8_check_gf_quality(cpi);
5129 #if CONFIG_RUNTIME_CPU_DETECT
5130 if (cm->rtcd.flags & HAS_NEON)
5133 vp8_pop_neon(store_reg);
5139 if (cpi->compressor_speed == 2)
5141 vpx_usec_timer_start(&tsctimer);
5142 vpx_usec_timer_start(&ticktimer);
5145 // start with a 0 size frame
5148 // Clear down mmx registers
5149 vp8_clear_system_state(); //__asm emms;
5151 cm->frame_type = INTER_FRAME;
5152 cm->frame_flags = *frame_flags;
5156 if (cm->refresh_alt_ref_frame)
5158 //cm->refresh_golden_frame = 1;
5159 cm->refresh_golden_frame = 0;
5160 cm->refresh_last_frame = 0;
5164 cm->refresh_golden_frame = 0;
5165 cm->refresh_last_frame = 1;
5170 #if !(CONFIG_REALTIME_ONLY)
5174 Pass1Encode(cpi, size, dest, frame_flags);
5176 else if (cpi->pass == 2)
5178 Pass2Encode(cpi, size, dest, frame_flags);
5182 encode_frame_to_data_rate(cpi, size, dest, frame_flags);
5184 if (cpi->compressor_speed == 2)
5186 unsigned int duration, duration2;
5187 vpx_usec_timer_mark(&tsctimer);
5188 vpx_usec_timer_mark(&ticktimer);
5190 duration = vpx_usec_timer_elapsed(&ticktimer);
5191 duration2 = (unsigned int)((double)duration / 2);
5193 if (cm->frame_type != KEY_FRAME)
5195 if (cpi->avg_encode_time == 0)
5196 cpi->avg_encode_time = duration;
5198 cpi->avg_encode_time = (7 * cpi->avg_encode_time + duration) >> 3;
5203 //if(*frame_flags!=1)
5206 if (cpi->avg_pick_mode_time == 0)
5207 cpi->avg_pick_mode_time = duration2;
5209 cpi->avg_pick_mode_time = (7 * cpi->avg_pick_mode_time + duration2) >> 3;
5215 if (cm->refresh_entropy_probs == 0)
5217 vpx_memcpy(&cm->fc, &cm->lfc, sizeof(cm->fc));
5220 // if its a dropped frame honor the requests on subsequent frames
5224 // return to normal state
5225 cm->refresh_entropy_probs = 1;
5226 cm->refresh_alt_ref_frame = 0;
5227 cm->refresh_golden_frame = 0;
5228 cm->refresh_last_frame = 1;
5229 cm->frame_type = INTER_FRAME;
5233 cpi->ready_for_new_frame = 1;
5235 vpx_usec_timer_mark(&cmptimer);
5236 cpi->time_compress_data += vpx_usec_timer_elapsed(&cmptimer);
5238 if (cpi->b_calculate_psnr && cpi->pass != 1 && cm->show_frame)
5239 generate_psnr_packet(cpi);
5245 cpi->bytes += *size;
5252 if (cpi->b_calculate_psnr)
5256 double frame_psnr = vp8_calc_psnr(cpi->Source, cm->frame_to_show, &y, &u, &v, &sq_error);
5261 cpi->total_sq_error += sq_error;
5262 cpi->total += frame_psnr;
5264 double y2, u2, v2, frame_psnr2, frame_ssim2 = 0;
5267 vp8_deblock(cm->frame_to_show, &cm->post_proc_buffer, cm->filter_level * 10 / 6, 1, 0, IF_RTCD(&cm->rtcd.postproc));
5268 vp8_clear_system_state();
5269 frame_psnr2 = vp8_calc_psnr(cpi->Source, &cm->post_proc_buffer, &y2, &u2, &v2, &sq_error);
5270 frame_ssim2 = vp8_calc_ssim(cpi->Source, &cm->post_proc_buffer, 1, &weight);
5272 cpi->summed_quality += frame_ssim2 * weight;
5273 cpi->summed_weights += weight;
5275 cpi->totalp_y += y2;
5276 cpi->totalp_u += u2;
5277 cpi->totalp_v += v2;
5278 cpi->totalp += frame_psnr2;
5279 cpi->total_sq_error2 += sq_error;
5284 if (cpi->b_calculate_ssimg)
5286 double y, u, v, frame_all;
5287 frame_all = vp8_calc_ssimg(cpi->Source, cm->frame_to_show, &y, &u, &v);
5288 cpi->total_ssimg_y += y;
5289 cpi->total_ssimg_u += u;
5290 cpi->total_ssimg_v += v;
5291 cpi->total_ssimg_all += frame_all;
5299 if (cpi->common.frame_type != 0 && cpi->common.base_qindex == cpi->oxcf.worst_allowed_q)
5301 skiptruecount += cpi->skip_true_count;
5302 skipfalsecount += cpi->skip_false_count;
5310 FILE *f = fopen("skip.stt", "a");
5311 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);
5313 if (cpi->is_src_frame_alt_ref == 1)
5314 fprintf(f, "skipcount: %4d framesize: %d\n", cpi->skip_true_count , *size);
5323 #if CONFIG_RUNTIME_CPU_DETECT
5324 if (cm->rtcd.flags & HAS_NEON)
5327 vp8_pop_neon(store_reg);
5334 int vp8_get_preview_raw_frame(VP8_PTR comp, YV12_BUFFER_CONFIG *dest, vp8_ppflags_t *flags)
5336 VP8_COMP *cpi = (VP8_COMP *) comp;
5338 if (cpi->common.refresh_alt_ref_frame)
5344 ret = vp8_post_proc_frame(&cpi->common, dest, flags);
5347 if (cpi->common.frame_to_show)
5349 *dest = *cpi->common.frame_to_show;
5350 dest->y_width = cpi->common.Width;
5351 dest->y_height = cpi->common.Height;
5352 dest->uv_height = cpi->common.Height / 2;
5360 #endif //!CONFIG_POSTPROC
5361 vp8_clear_system_state();
5366 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])
5368 VP8_COMP *cpi = (VP8_COMP *) comp;
5369 signed char feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS];
5371 if (cpi->common.mb_rows != rows || cpi->common.mb_cols != cols)
5376 disable_segmentation((VP8_PTR)cpi);
5380 // Set the segmentation Map
5381 set_segmentation_map((VP8_PTR)cpi, map);
5383 // Activate segmentation.
5384 enable_segmentation((VP8_PTR)cpi);
5386 // Set up the quant segment data
5387 feature_data[MB_LVL_ALT_Q][0] = delta_q[0];
5388 feature_data[MB_LVL_ALT_Q][1] = delta_q[1];
5389 feature_data[MB_LVL_ALT_Q][2] = delta_q[2];
5390 feature_data[MB_LVL_ALT_Q][3] = delta_q[3];
5392 // Set up the loop segment data s
5393 feature_data[MB_LVL_ALT_LF][0] = delta_lf[0];
5394 feature_data[MB_LVL_ALT_LF][1] = delta_lf[1];
5395 feature_data[MB_LVL_ALT_LF][2] = delta_lf[2];
5396 feature_data[MB_LVL_ALT_LF][3] = delta_lf[3];
5398 cpi->segment_encode_breakout[0] = threshold[0];
5399 cpi->segment_encode_breakout[1] = threshold[1];
5400 cpi->segment_encode_breakout[2] = threshold[2];
5401 cpi->segment_encode_breakout[3] = threshold[3];
5403 // Initialise the feature data structure
5404 // SEGMENT_DELTADATA 0, SEGMENT_ABSDATA 1
5405 set_segment_data((VP8_PTR)cpi, &feature_data[0][0], SEGMENT_DELTADATA);
5410 int vp8_set_active_map(VP8_PTR comp, unsigned char *map, unsigned int rows, unsigned int cols)
5412 VP8_COMP *cpi = (VP8_COMP *) comp;
5414 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols)
5418 vpx_memcpy(cpi->active_map, map, rows * cols);
5419 cpi->active_map_enabled = 1;
5422 cpi->active_map_enabled = 0;
5428 //cpi->active_map_enabled = 0;
5433 int vp8_set_internal_size(VP8_PTR comp, VPX_SCALING horiz_mode, VPX_SCALING vert_mode)
5435 VP8_COMP *cpi = (VP8_COMP *) comp;
5437 if (horiz_mode <= ONETWO)
5438 cpi->common.horiz_scale = horiz_mode;
5442 if (vert_mode <= ONETWO)
5443 cpi->common.vert_scale = vert_mode;
5452 int vp8_calc_ss_err(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *dest, const vp8_variance_rtcd_vtable_t *rtcd)
5457 unsigned char *src = source->y_buffer;
5458 unsigned char *dst = dest->y_buffer;
5461 // Loop through the Y plane raw and reconstruction data summing (square differences)
5462 for (i = 0; i < source->y_height; i += 16)
5464 for (j = 0; j < source->y_width; j += 16)
5467 Total += VARIANCE_INVOKE(rtcd, mse16x16)(src + j, source->y_stride, dst + j, dest->y_stride, &sse);
5470 src += 16 * source->y_stride;
5471 dst += 16 * dest->y_stride;
5476 int vp8_calc_low_ss_err(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *dest, const vp8_variance_rtcd_vtable_t *rtcd)
5481 unsigned char *src = source->y_buffer;
5482 unsigned char *dst = dest->y_buffer;
5485 // Loop through the Y plane raw and reconstruction data summing (square differences)
5486 for (i = 0; i < source->y_height; i += 16)
5488 for (j = 0; j < source->y_width; j += 16)
5491 VARIANCE_INVOKE(rtcd, mse16x16)(src + j, source->y_stride, dst + j, dest->y_stride, &sse);
5497 src += 16 * source->y_stride;
5498 dst += 16 * dest->y_stride;
5504 int vp8_get_speed(VP8_PTR c)
5506 VP8_COMP *cpi = (VP8_COMP *) c;
5509 int vp8_get_quantizer(VP8_PTR c)
5511 VP8_COMP *cpi = (VP8_COMP *) c;
5512 return cpi->common.base_qindex;