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);
77 static void set_default_lf_deltas(VP8_COMP *cpi);
79 extern const int vp8_gf_interval_table[101];
84 extern double vp8_calc_ssim
86 YV12_BUFFER_CONFIG *source,
87 YV12_BUFFER_CONFIG *dest,
92 extern double vp8_calc_ssimg
94 YV12_BUFFER_CONFIG *source,
95 YV12_BUFFER_CONFIG *dest,
105 #ifdef OUTPUT_YUV_SRC
116 extern int skip_true_count;
117 extern int skip_false_count;
122 extern int intra_mode_stats[10][10][10];
126 unsigned int frames_at_speed[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
127 unsigned int tot_pm = 0;
128 unsigned int cnt_pm = 0;
129 unsigned int tot_ef = 0;
130 unsigned int cnt_ef = 0;
134 extern unsigned __int64 Sectionbits[50];
135 extern int y_modes[5] ;
136 extern int uv_modes[4] ;
137 extern int b_modes[10] ;
139 extern int inter_y_modes[10] ;
140 extern int inter_uv_modes[4] ;
141 extern unsigned int inter_b_modes[15];
144 extern void (*vp8_short_fdct4x4)(short *input, short *output, int pitch);
145 extern void (*vp8_short_fdct8x4)(short *input, short *output, int pitch);
147 extern const int vp8_bits_per_mb[2][QINDEX_RANGE];
149 extern const int qrounding_factors[129];
150 extern const int qzbin_factors[129];
151 extern void vp8cx_init_quantizer(VP8_COMP *cpi);
152 extern const int vp8cx_base_skip_false_prob[128];
154 // Tables relating active max Q to active min Q
155 static const int kf_low_motion_minq[QINDEX_RANGE] =
157 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
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 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4,
161 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 10,10,
162 11,11,12,12,13,13,14,14,15,15,16,16,17,17,18,18,
163 19,19,20,20,21,21,22,22,23,23,24,24,25,25,26,26,
164 27,27,28,28,29,29,30,30,31,32,33,34,35,36,37,38,
166 static const int kf_high_motion_minq[QINDEX_RANGE] =
168 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
169 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
170 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5,
171 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10,10,
172 11,11,12,12,13,13,14,14,15,15,16,16,17,17,18,18,
173 19,19,20,20,21,21,22,22,23,23,24,24,25,25,26,26,
174 27,27,28,28,29,29,30,30,31,31,32,32,33,33,34,34,
175 35,35,36,36,37,38,39,40,41,42,43,44,45,46,47,48,
177 static const int gf_low_motion_minq[QINDEX_RANGE] =
179 0,0,0,0,1,1,1,1,1,1,1,1,2,2,2,2,
180 3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,
181 7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10,
182 11,11,12,12,13,13,14,14,15,15,16,16,17,17,18,18,
183 19,19,20,20,21,21,22,22,23,23,24,24,25,25,26,26,
184 27,27,28,28,29,29,30,30,31,31,32,32,33,33,34,34,
185 35,35,36,36,37,37,38,38,39,39,40,40,41,41,42,42,
186 43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58
188 static const int gf_mid_motion_minq[QINDEX_RANGE] =
190 0,0,0,0,1,1,1,1,1,1,2,2,3,3,3,4,
191 4,4,5,5,5,6,6,6,7,7,7,8,8,8,9,9,
192 9,10,10,10,10,11,11,11,12,12,12,12,13,13,13,14,
193 14,14,15,15,16,16,17,17,18,18,19,19,20,20,21,21,
194 22,22,23,23,24,24,25,25,26,26,27,27,28,28,29,29,
195 30,30,31,31,32,32,33,33,34,34,35,35,36,36,37,37,
196 38,39,39,40,40,41,41,42,42,43,43,44,45,46,47,48,
197 49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,
199 static const int gf_high_motion_minq[QINDEX_RANGE] =
201 0,0,0,0,1,1,1,1,1,2,2,2,3,3,3,4,
202 4,4,5,5,5,6,6,6,7,7,7,8,8,8,9,9,
203 9,10,10,10,11,11,12,12,13,13,14,14,15,15,16,16,
204 17,17,18,18,19,19,20,20,21,21,22,22,23,23,24,24,
205 25,25,26,26,27,27,28,28,29,29,30,30,31,31,32,32,
206 33,33,34,34,35,35,36,36,37,37,38,38,39,39,40,40,
207 41,41,42,42,43,44,45,46,47,48,49,50,51,52,53,54,
208 55,56,57,58,59,60,62,64,66,68,70,72,74,76,78,80,
210 static const int inter_minq[QINDEX_RANGE] =
212 0,0,1,1,2,3,3,4,4,5,6,6,7,8,8,9,
213 9,10,11,11,12,13,13,14,15,15,16,17,17,18,19,20,
214 20,21,22,22,23,24,24,25,26,27,27,28,29,30,30,31,
215 32,33,33,34,35,36,36,37,38,39,39,40,41,42,42,43,
216 44,45,46,46,47,48,49,50,50,51,52,53,54,55,55,56,
217 57,58,59,60,60,61,62,63,64,65,66,67,67,68,69,70,
218 71,72,73,74,75,75,76,77,78,79,80,81,82,83,84,85,
219 86,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100
222 void vp8_initialize()
224 static int init_done = 0;
228 vp8_scale_machine_specific_config();
229 vp8_initialize_common();
230 //vp8_dmachine_specific_config();
231 vp8_tokenize_initialize();
233 vp8cx_init_mv_bits_sadcost();
237 #ifdef PACKET_TESTING
238 extern FILE *vpxlogc;
241 static void setup_features(VP8_COMP *cpi)
243 // Set up default state for MB feature flags
244 cpi->mb.e_mbd.segmentation_enabled = 0;
245 cpi->mb.e_mbd.update_mb_segmentation_map = 0;
246 cpi->mb.e_mbd.update_mb_segmentation_data = 0;
247 vpx_memset(cpi->mb.e_mbd.mb_segment_tree_probs, 255, sizeof(cpi->mb.e_mbd.mb_segment_tree_probs));
248 vpx_memset(cpi->mb.e_mbd.segment_feature_data, 0, sizeof(cpi->mb.e_mbd.segment_feature_data));
250 cpi->mb.e_mbd.mode_ref_lf_delta_enabled = 0;
251 cpi->mb.e_mbd.mode_ref_lf_delta_update = 0;
252 vpx_memset(cpi->mb.e_mbd.ref_lf_deltas, 0, sizeof(cpi->mb.e_mbd.ref_lf_deltas));
253 vpx_memset(cpi->mb.e_mbd.mode_lf_deltas, 0, sizeof(cpi->mb.e_mbd.mode_lf_deltas));
254 vpx_memset(cpi->mb.e_mbd.last_ref_lf_deltas, 0, sizeof(cpi->mb.e_mbd.ref_lf_deltas));
255 vpx_memset(cpi->mb.e_mbd.last_mode_lf_deltas, 0, sizeof(cpi->mb.e_mbd.mode_lf_deltas));
257 set_default_lf_deltas(cpi);
262 void vp8_dealloc_compressor_data(VP8_COMP *cpi)
264 // Delete last frame MV storage buffers
270 if (cpi->lf_ref_frame_sign_bias != 0)
271 vpx_free(cpi->lf_ref_frame_sign_bias);
273 cpi->lf_ref_frame_sign_bias = 0;
275 if (cpi->lf_ref_frame != 0)
276 vpx_free(cpi->lf_ref_frame);
278 cpi->lf_ref_frame = 0;
280 // Delete sementation map
281 if (cpi->segmentation_map != 0)
282 vpx_free(cpi->segmentation_map);
284 cpi->segmentation_map = 0;
286 if (cpi->active_map != 0)
287 vpx_free(cpi->active_map);
291 // Delete first pass motion map
292 if (cpi->fp_motion_map != 0)
293 vpx_free(cpi->fp_motion_map);
295 cpi->fp_motion_map = 0;
297 vp8_de_alloc_frame_buffers(&cpi->common);
299 vp8_yv12_de_alloc_frame_buffer(&cpi->last_frame_uf);
300 vp8_yv12_de_alloc_frame_buffer(&cpi->scaled_source);
301 #if VP8_TEMPORAL_ALT_REF
302 vp8_yv12_de_alloc_frame_buffer(&cpi->alt_ref_buffer.source_buffer);
307 for (i = 0; i < MAX_LAG_BUFFERS; i++)
308 vp8_yv12_de_alloc_frame_buffer(&cpi->src_buffer[i].source_buffer);
310 cpi->source_buffer_count = 0;
316 // Structure used to minitor GF useage
317 if (cpi->gf_active_flags != 0)
318 vpx_free(cpi->gf_active_flags);
320 cpi->gf_active_flags = 0;
323 vpx_free(cpi->mb.pip);
328 vpx_free(cpi->total_stats);
330 cpi->total_stats = 0;
332 if(cpi->this_frame_stats)
333 vpx_free(cpi->this_frame_stats);
335 cpi->this_frame_stats = 0;
338 static void enable_segmentation(VP8_PTR ptr)
340 VP8_COMP *cpi = (VP8_COMP *)(ptr);
342 // Set the appropriate feature bit
343 cpi->mb.e_mbd.segmentation_enabled = 1;
344 cpi->mb.e_mbd.update_mb_segmentation_map = 1;
345 cpi->mb.e_mbd.update_mb_segmentation_data = 1;
347 static void disable_segmentation(VP8_PTR ptr)
349 VP8_COMP *cpi = (VP8_COMP *)(ptr);
351 // Clear the appropriate feature bit
352 cpi->mb.e_mbd.segmentation_enabled = 0;
355 // Valid values for a segment are 0 to 3
356 // Segmentation map is arrange as [Rows][Columns]
357 static void set_segmentation_map(VP8_PTR ptr, unsigned char *segmentation_map)
359 VP8_COMP *cpi = (VP8_COMP *)(ptr);
361 // Copy in the new segmentation map
362 vpx_memcpy(cpi->segmentation_map, segmentation_map, (cpi->common.mb_rows * cpi->common.mb_cols));
364 // Signal that the map should be updated.
365 cpi->mb.e_mbd.update_mb_segmentation_map = 1;
366 cpi->mb.e_mbd.update_mb_segmentation_data = 1;
369 // The values given for each segment can be either deltas (from the default value chosen for the frame) or absolute values.
371 // Valid range for abs values is (0-127 for MB_LVL_ALT_Q) , (0-63 for SEGMENT_ALT_LF)
372 // Valid range for delta values are (+/-127 for MB_LVL_ALT_Q) , (+/-63 for SEGMENT_ALT_LF)
374 // abs_delta = SEGMENT_DELTADATA (deltas) abs_delta = SEGMENT_ABSDATA (use the absolute values given).
377 static void set_segment_data(VP8_PTR ptr, signed char *feature_data, unsigned char abs_delta)
379 VP8_COMP *cpi = (VP8_COMP *)(ptr);
381 cpi->mb.e_mbd.mb_segement_abs_delta = abs_delta;
382 vpx_memcpy(cpi->segment_feature_data, feature_data, sizeof(cpi->segment_feature_data));
386 static void segmentation_test_function(VP8_PTR ptr)
388 VP8_COMP *cpi = (VP8_COMP *)(ptr);
390 unsigned char *seg_map;
391 signed char feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS];
393 // Create a temporary map for segmentation data.
394 CHECK_MEM_ERROR(seg_map, vpx_calloc(cpi->common.mb_rows * cpi->common.mb_cols, 1));
396 // MB loop to set local segmentation map
397 /*for ( i = 0; i < cpi->common.mb_rows; i++ )
399 for ( j = 0; j < cpi->common.mb_cols; j++ )
401 //seg_map[(i*cpi->common.mb_cols) + j] = (j % 2) + ((i%2)* 2);
402 //if ( j < cpi->common.mb_cols/2 )
404 // Segment 1 around the edge else 0
405 if ( (i == 0) || (j == 0) || (i == (cpi->common.mb_rows-1)) || (j == (cpi->common.mb_cols-1)) )
406 seg_map[(i*cpi->common.mb_cols) + j] = 1;
407 //else if ( (i < 2) || (j < 2) || (i > (cpi->common.mb_rows-3)) || (j > (cpi->common.mb_cols-3)) )
408 // seg_map[(i*cpi->common.mb_cols) + j] = 2;
409 //else if ( (i < 5) || (j < 5) || (i > (cpi->common.mb_rows-6)) || (j > (cpi->common.mb_cols-6)) )
410 // seg_map[(i*cpi->common.mb_cols) + j] = 3;
412 seg_map[(i*cpi->common.mb_cols) + j] = 0;
416 // Set the segmentation Map
417 set_segmentation_map(ptr, seg_map);
419 // Activate segmentation.
420 enable_segmentation(ptr);
422 // Set up the quant segment data
423 feature_data[MB_LVL_ALT_Q][0] = 0;
424 feature_data[MB_LVL_ALT_Q][1] = 4;
425 feature_data[MB_LVL_ALT_Q][2] = 0;
426 feature_data[MB_LVL_ALT_Q][3] = 0;
427 // Set up the loop segment data
428 feature_data[MB_LVL_ALT_LF][0] = 0;
429 feature_data[MB_LVL_ALT_LF][1] = 0;
430 feature_data[MB_LVL_ALT_LF][2] = 0;
431 feature_data[MB_LVL_ALT_LF][3] = 0;
433 // Initialise the feature data structure
434 // SEGMENT_DELTADATA 0, SEGMENT_ABSDATA 1
435 set_segment_data(ptr, &feature_data[0][0], SEGMENT_DELTADATA);
437 // Delete sementation map
445 // A simple function to cyclically refresh the background at a lower Q
446 static void cyclic_background_refresh(VP8_COMP *cpi, int Q, int lf_adjustment)
448 unsigned char *seg_map;
449 signed char feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS];
451 int block_count = cpi->cyclic_refresh_mode_max_mbs_perframe;
452 int mbs_in_frame = cpi->common.mb_rows * cpi->common.mb_cols;
454 // Create a temporary map for segmentation data.
455 CHECK_MEM_ERROR(seg_map, vpx_calloc(cpi->common.mb_rows * cpi->common.mb_cols, 1));
457 cpi->cyclic_refresh_q = Q;
459 for (i = Q; i > 0; i--)
461 if (vp8_bits_per_mb[cpi->common.frame_type][i] >= ((vp8_bits_per_mb[cpi->common.frame_type][Q]*(Q + 128)) / 64))
462 //if ( vp8_bits_per_mb[cpi->common.frame_type][i] >= ((vp8_bits_per_mb[cpi->common.frame_type][Q]*((2*Q)+96))/64) )
468 cpi->cyclic_refresh_q = i;
470 // Only update for inter frames
471 if (cpi->common.frame_type != KEY_FRAME)
473 // Cycle through the macro_block rows
474 // MB loop to set local segmentation map
475 for (i = cpi->cyclic_refresh_mode_index; i < mbs_in_frame; i++)
477 // If the MB is as a candidate for clean up then mark it for possible boost/refresh (segment 1)
478 // The segment id may get reset to 0 later if the MB gets coded anything other than last frame 0,0
479 // as only (last frame 0,0) MBs are eligable for refresh : that is to say Mbs likely to be background blocks.
480 if (cpi->cyclic_refresh_map[i] == 0)
488 // Skip blocks that have been refreshed recently anyway.
489 if (cpi->cyclic_refresh_map[i] < 0)
490 //cpi->cyclic_refresh_map[i] = cpi->cyclic_refresh_map[i] / 16;
491 cpi->cyclic_refresh_map[i]++;
502 // If we have gone through the frame reset to the start
503 cpi->cyclic_refresh_mode_index = i;
505 if (cpi->cyclic_refresh_mode_index >= mbs_in_frame)
506 cpi->cyclic_refresh_mode_index = 0;
509 // Set the segmentation Map
510 set_segmentation_map((VP8_PTR)cpi, seg_map);
512 // Activate segmentation.
513 enable_segmentation((VP8_PTR)cpi);
515 // Set up the quant segment data
516 feature_data[MB_LVL_ALT_Q][0] = 0;
517 feature_data[MB_LVL_ALT_Q][1] = (cpi->cyclic_refresh_q - Q);
518 feature_data[MB_LVL_ALT_Q][2] = 0;
519 feature_data[MB_LVL_ALT_Q][3] = 0;
521 // Set up the loop segment data
522 feature_data[MB_LVL_ALT_LF][0] = 0;
523 feature_data[MB_LVL_ALT_LF][1] = lf_adjustment;
524 feature_data[MB_LVL_ALT_LF][2] = 0;
525 feature_data[MB_LVL_ALT_LF][3] = 0;
527 // Initialise the feature data structure
528 // SEGMENT_DELTADATA 0, SEGMENT_ABSDATA 1
529 set_segment_data((VP8_PTR)cpi, &feature_data[0][0], SEGMENT_DELTADATA);
531 // Delete sementation map
539 static void set_default_lf_deltas(VP8_COMP *cpi)
541 cpi->mb.e_mbd.mode_ref_lf_delta_enabled = 1;
542 cpi->mb.e_mbd.mode_ref_lf_delta_update = 1;
544 vpx_memset(cpi->mb.e_mbd.ref_lf_deltas, 0, sizeof(cpi->mb.e_mbd.ref_lf_deltas));
545 vpx_memset(cpi->mb.e_mbd.mode_lf_deltas, 0, sizeof(cpi->mb.e_mbd.mode_lf_deltas));
547 // Test of ref frame deltas
548 cpi->mb.e_mbd.ref_lf_deltas[INTRA_FRAME] = 2;
549 cpi->mb.e_mbd.ref_lf_deltas[LAST_FRAME] = 0;
550 cpi->mb.e_mbd.ref_lf_deltas[GOLDEN_FRAME] = -2;
551 cpi->mb.e_mbd.ref_lf_deltas[ALTREF_FRAME] = -2;
553 cpi->mb.e_mbd.mode_lf_deltas[0] = 4; // BPRED
554 cpi->mb.e_mbd.mode_lf_deltas[1] = -2; // Zero
555 cpi->mb.e_mbd.mode_lf_deltas[2] = 2; // New mv
556 cpi->mb.e_mbd.mode_lf_deltas[3] = 4; // Split mv
559 void vp8_set_speed_features(VP8_COMP *cpi)
561 SPEED_FEATURES *sf = &cpi->sf;
562 int Mode = cpi->compressor_speed;
563 int Speed = cpi->Speed;
565 VP8_COMMON *cm = &cpi->common;
566 int last_improved_quant = sf->improved_quant;
568 // Initialise default mode frequency sampling variables
569 for (i = 0; i < MAX_MODES; i ++)
571 cpi->mode_check_freq[i] = 0;
572 cpi->mode_test_hit_counts[i] = 0;
573 cpi->mode_chosen_counts[i] = 0;
576 cpi->mbs_tested_so_far = 0;
580 sf->search_method = NSTEP;
581 sf->improved_quant = 1;
582 sf->improved_dct = 1;
585 sf->quarter_pixel_search = 1;
586 sf->half_pixel_search = 1;
587 sf->full_freq[0] = 7;
588 sf->full_freq[1] = 7;
589 sf->min_fs_radius = 8;
590 sf->max_fs_radius = 32;
591 sf->iterative_sub_pixel = 1;
592 sf->optimize_coefficients = 1;
595 sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
600 // default thresholds to 0
601 for (i = 0; i < MAX_MODES; i++)
602 sf->thresh_mult[i] = 0;
606 #if !(CONFIG_REALTIME_ONLY)
607 case 0: // best quality mode
608 sf->thresh_mult[THR_ZEROMV ] = 0;
609 sf->thresh_mult[THR_ZEROG ] = 0;
610 sf->thresh_mult[THR_ZEROA ] = 0;
611 sf->thresh_mult[THR_NEARESTMV] = 0;
612 sf->thresh_mult[THR_NEARESTG ] = 0;
613 sf->thresh_mult[THR_NEARESTA ] = 0;
614 sf->thresh_mult[THR_NEARMV ] = 0;
615 sf->thresh_mult[THR_NEARG ] = 0;
616 sf->thresh_mult[THR_NEARA ] = 0;
618 sf->thresh_mult[THR_DC ] = 0;
620 sf->thresh_mult[THR_V_PRED ] = 1000;
621 sf->thresh_mult[THR_H_PRED ] = 1000;
622 sf->thresh_mult[THR_B_PRED ] = 2000;
623 sf->thresh_mult[THR_TM ] = 1000;
625 sf->thresh_mult[THR_NEWMV ] = 1000;
626 sf->thresh_mult[THR_NEWG ] = 1000;
627 sf->thresh_mult[THR_NEWA ] = 1000;
629 sf->thresh_mult[THR_SPLITMV ] = 2500;
630 sf->thresh_mult[THR_SPLITG ] = 5000;
631 sf->thresh_mult[THR_SPLITA ] = 5000;
633 sf->full_freq[0] = 7;
634 sf->full_freq[1] = 15;
637 sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
639 if (!(cpi->ref_frame_flags & VP8_LAST_FLAG))
641 sf->thresh_mult[THR_NEWMV ] = INT_MAX;
642 sf->thresh_mult[THR_NEARESTMV] = INT_MAX;
643 sf->thresh_mult[THR_ZEROMV ] = INT_MAX;
644 sf->thresh_mult[THR_NEARMV ] = INT_MAX;
645 sf->thresh_mult[THR_SPLITMV ] = INT_MAX;
648 if (!(cpi->ref_frame_flags & VP8_GOLD_FLAG))
650 sf->thresh_mult[THR_NEARESTG ] = INT_MAX;
651 sf->thresh_mult[THR_ZEROG ] = INT_MAX;
652 sf->thresh_mult[THR_NEARG ] = INT_MAX;
653 sf->thresh_mult[THR_NEWG ] = INT_MAX;
654 sf->thresh_mult[THR_SPLITG ] = INT_MAX;
657 if (!(cpi->ref_frame_flags & VP8_ALT_FLAG))
659 sf->thresh_mult[THR_NEARESTA ] = INT_MAX;
660 sf->thresh_mult[THR_ZEROA ] = INT_MAX;
661 sf->thresh_mult[THR_NEARA ] = INT_MAX;
662 sf->thresh_mult[THR_NEWA ] = INT_MAX;
663 sf->thresh_mult[THR_SPLITA ] = INT_MAX;
669 sf->thresh_mult[THR_NEARESTMV] = 0;
670 sf->thresh_mult[THR_ZEROMV ] = 0;
671 sf->thresh_mult[THR_DC ] = 0;
672 sf->thresh_mult[THR_NEARMV ] = 0;
673 sf->thresh_mult[THR_V_PRED ] = 1000;
674 sf->thresh_mult[THR_H_PRED ] = 1000;
675 sf->thresh_mult[THR_B_PRED ] = 2500;
676 sf->thresh_mult[THR_TM ] = 1000;
678 sf->thresh_mult[THR_NEARESTG ] = 1000;
679 sf->thresh_mult[THR_NEARESTA ] = 1000;
681 sf->thresh_mult[THR_ZEROG ] = 1000;
682 sf->thresh_mult[THR_ZEROA ] = 1000;
683 sf->thresh_mult[THR_NEARG ] = 1000;
684 sf->thresh_mult[THR_NEARA ] = 1000;
686 sf->thresh_mult[THR_NEWMV ] = 1500;
687 sf->thresh_mult[THR_NEWG ] = 1500;
688 sf->thresh_mult[THR_NEWA ] = 1500;
690 sf->thresh_mult[THR_SPLITMV ] = 5000;
691 sf->thresh_mult[THR_SPLITG ] = 10000;
692 sf->thresh_mult[THR_SPLITA ] = 10000;
694 sf->full_freq[0] = 15;
695 sf->full_freq[1] = 31;
698 sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
700 if (!(cpi->ref_frame_flags & VP8_LAST_FLAG))
702 sf->thresh_mult[THR_NEWMV ] = INT_MAX;
703 sf->thresh_mult[THR_NEARESTMV] = INT_MAX;
704 sf->thresh_mult[THR_ZEROMV ] = INT_MAX;
705 sf->thresh_mult[THR_NEARMV ] = INT_MAX;
706 sf->thresh_mult[THR_SPLITMV ] = INT_MAX;
709 if (!(cpi->ref_frame_flags & VP8_GOLD_FLAG))
711 sf->thresh_mult[THR_NEARESTG ] = INT_MAX;
712 sf->thresh_mult[THR_ZEROG ] = INT_MAX;
713 sf->thresh_mult[THR_NEARG ] = INT_MAX;
714 sf->thresh_mult[THR_NEWG ] = INT_MAX;
715 sf->thresh_mult[THR_SPLITG ] = INT_MAX;
718 if (!(cpi->ref_frame_flags & VP8_ALT_FLAG))
720 sf->thresh_mult[THR_NEARESTA ] = INT_MAX;
721 sf->thresh_mult[THR_ZEROA ] = INT_MAX;
722 sf->thresh_mult[THR_NEARA ] = INT_MAX;
723 sf->thresh_mult[THR_NEWA ] = INT_MAX;
724 sf->thresh_mult[THR_SPLITA ] = INT_MAX;
729 // Disable coefficient optimization above speed 0
730 sf->optimize_coefficients = 0;
732 cpi->mode_check_freq[THR_SPLITG] = 4;
733 cpi->mode_check_freq[THR_SPLITA] = 4;
734 cpi->mode_check_freq[THR_SPLITMV] = 2;
736 sf->thresh_mult[THR_TM ] = 1500;
737 sf->thresh_mult[THR_V_PRED ] = 1500;
738 sf->thresh_mult[THR_H_PRED ] = 1500;
739 sf->thresh_mult[THR_B_PRED ] = 5000;
741 if (cpi->ref_frame_flags & VP8_LAST_FLAG)
743 sf->thresh_mult[THR_NEWMV ] = 2000;
744 sf->thresh_mult[THR_SPLITMV ] = 10000;
747 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
749 sf->thresh_mult[THR_NEARESTG ] = 1500;
750 sf->thresh_mult[THR_ZEROG ] = 1500;
751 sf->thresh_mult[THR_NEARG ] = 1500;
752 sf->thresh_mult[THR_NEWG ] = 2000;
753 sf->thresh_mult[THR_SPLITG ] = 20000;
756 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
758 sf->thresh_mult[THR_NEARESTA ] = 1500;
759 sf->thresh_mult[THR_ZEROA ] = 1500;
760 sf->thresh_mult[THR_NEARA ] = 1500;
761 sf->thresh_mult[THR_NEWA ] = 2000;
762 sf->thresh_mult[THR_SPLITA ] = 20000;
765 sf->improved_quant = 0;
766 sf->improved_dct = 0;
769 sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
774 cpi->mode_check_freq[THR_SPLITG] = 15;
775 cpi->mode_check_freq[THR_SPLITA] = 15;
776 cpi->mode_check_freq[THR_SPLITMV] = 7;
778 sf->thresh_mult[THR_TM ] = 2000;
779 sf->thresh_mult[THR_V_PRED ] = 2000;
780 sf->thresh_mult[THR_H_PRED ] = 2000;
781 sf->thresh_mult[THR_B_PRED ] = 7500;
783 if (cpi->ref_frame_flags & VP8_LAST_FLAG)
785 sf->thresh_mult[THR_NEWMV ] = 2000;
786 sf->thresh_mult[THR_SPLITMV ] = 25000;
789 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
791 sf->thresh_mult[THR_NEARESTG ] = 2000;
792 sf->thresh_mult[THR_ZEROG ] = 2000;
793 sf->thresh_mult[THR_NEARG ] = 2000;
794 sf->thresh_mult[THR_NEWG ] = 2500;
795 sf->thresh_mult[THR_SPLITG ] = 50000;
798 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
800 sf->thresh_mult[THR_NEARESTA ] = 2000;
801 sf->thresh_mult[THR_ZEROA ] = 2000;
802 sf->thresh_mult[THR_NEARA ] = 2000;
803 sf->thresh_mult[THR_NEWA ] = 2500;
804 sf->thresh_mult[THR_SPLITA ] = 50000;
807 // Only do recode loop on key frames, golden frames and
811 sf->full_freq[0] = 31;
812 sf->full_freq[1] = 63;
818 sf->auto_filter = 0; // Faster selection of loop filter
819 cpi->mode_check_freq[THR_V_PRED] = 2;
820 cpi->mode_check_freq[THR_H_PRED] = 2;
821 cpi->mode_check_freq[THR_B_PRED] = 2;
823 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
825 cpi->mode_check_freq[THR_NEARG] = 2;
826 cpi->mode_check_freq[THR_NEWG] = 4;
829 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
831 cpi->mode_check_freq[THR_NEARA] = 2;
832 cpi->mode_check_freq[THR_NEWA] = 4;
835 sf->thresh_mult[THR_SPLITA ] = INT_MAX;
836 sf->thresh_mult[THR_SPLITG ] = INT_MAX;
837 sf->thresh_mult[THR_SPLITMV ] = INT_MAX;
839 sf->full_freq[0] = 63;
840 sf->full_freq[1] = 127;
845 cpi->mode_check_freq[THR_V_PRED] = 0;
846 cpi->mode_check_freq[THR_H_PRED] = 0;
847 cpi->mode_check_freq[THR_B_PRED] = 0;
848 cpi->mode_check_freq[THR_NEARG] = 0;
849 cpi->mode_check_freq[THR_NEWG] = 0;
850 cpi->mode_check_freq[THR_NEARA] = 0;
851 cpi->mode_check_freq[THR_NEWA] = 0;
854 sf->recode_loop = 0; // recode loop off
855 sf->RD = 0; // Turn rd off
856 sf->full_freq[0] = INT_MAX;
857 sf->full_freq[1] = INT_MAX;
862 sf->auto_filter = 0; // Faster selection of loop filter
864 cpi->mode_check_freq[THR_V_PRED] = 2;
865 cpi->mode_check_freq[THR_H_PRED] = 2;
866 cpi->mode_check_freq[THR_B_PRED] = 2;
868 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
870 cpi->mode_check_freq[THR_NEARG] = 2;
871 cpi->mode_check_freq[THR_NEWG] = 4;
874 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
876 cpi->mode_check_freq[THR_NEARA] = 2;
877 cpi->mode_check_freq[THR_NEWA] = 4;
880 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
882 sf->thresh_mult[THR_NEARESTG ] = 2000;
883 sf->thresh_mult[THR_ZEROG ] = 2000;
884 sf->thresh_mult[THR_NEARG ] = 2000;
885 sf->thresh_mult[THR_NEWG ] = 4000;
888 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
890 sf->thresh_mult[THR_NEARESTA ] = 2000;
891 sf->thresh_mult[THR_ZEROA ] = 2000;
892 sf->thresh_mult[THR_NEARA ] = 2000;
893 sf->thresh_mult[THR_NEWA ] = 4000;
900 sf->optimize_coefficients = 0;
903 sf->iterative_sub_pixel = 1;
904 sf->thresh_mult[THR_NEARESTMV] = 0;
905 sf->thresh_mult[THR_ZEROMV ] = 0;
906 sf->thresh_mult[THR_DC ] = 0;
907 sf->thresh_mult[THR_TM ] = 0;
908 sf->thresh_mult[THR_NEARMV ] = 0;
909 sf->thresh_mult[THR_V_PRED ] = 1000;
910 sf->thresh_mult[THR_H_PRED ] = 1000;
911 sf->thresh_mult[THR_B_PRED ] = 2500;
912 sf->thresh_mult[THR_NEARESTG ] = 1000;
913 sf->thresh_mult[THR_ZEROG ] = 1000;
914 sf->thresh_mult[THR_NEARG ] = 1000;
915 sf->thresh_mult[THR_NEARESTA ] = 1000;
916 sf->thresh_mult[THR_ZEROA ] = 1000;
917 sf->thresh_mult[THR_NEARA ] = 1000;
918 sf->thresh_mult[THR_NEWMV ] = 2000;
919 sf->thresh_mult[THR_NEWG ] = 2000;
920 sf->thresh_mult[THR_NEWA ] = 2000;
921 sf->thresh_mult[THR_SPLITMV ] = 5000;
922 sf->thresh_mult[THR_SPLITG ] = 10000;
923 sf->thresh_mult[THR_SPLITA ] = 10000;
924 sf->full_freq[0] = 15;
925 sf->full_freq[1] = 31;
926 sf->search_method = NSTEP;
928 if (!(cpi->ref_frame_flags & VP8_LAST_FLAG))
930 sf->thresh_mult[THR_NEWMV ] = INT_MAX;
931 sf->thresh_mult[THR_NEARESTMV] = INT_MAX;
932 sf->thresh_mult[THR_ZEROMV ] = INT_MAX;
933 sf->thresh_mult[THR_NEARMV ] = INT_MAX;
934 sf->thresh_mult[THR_SPLITMV ] = INT_MAX;
937 if (!(cpi->ref_frame_flags & VP8_GOLD_FLAG))
939 sf->thresh_mult[THR_NEARESTG ] = INT_MAX;
940 sf->thresh_mult[THR_ZEROG ] = INT_MAX;
941 sf->thresh_mult[THR_NEARG ] = INT_MAX;
942 sf->thresh_mult[THR_NEWG ] = INT_MAX;
943 sf->thresh_mult[THR_SPLITG ] = INT_MAX;
946 if (!(cpi->ref_frame_flags & VP8_ALT_FLAG))
948 sf->thresh_mult[THR_NEARESTA ] = INT_MAX;
949 sf->thresh_mult[THR_ZEROA ] = INT_MAX;
950 sf->thresh_mult[THR_NEARA ] = INT_MAX;
951 sf->thresh_mult[THR_NEWA ] = INT_MAX;
952 sf->thresh_mult[THR_SPLITA ] = INT_MAX;
957 cpi->mode_check_freq[THR_SPLITG] = 4;
958 cpi->mode_check_freq[THR_SPLITA] = 4;
959 cpi->mode_check_freq[THR_SPLITMV] = 2;
961 sf->thresh_mult[THR_DC ] = 0;
962 sf->thresh_mult[THR_TM ] = 1000;
963 sf->thresh_mult[THR_V_PRED ] = 2000;
964 sf->thresh_mult[THR_H_PRED ] = 2000;
965 sf->thresh_mult[THR_B_PRED ] = 5000;
967 if (cpi->ref_frame_flags & VP8_LAST_FLAG)
969 sf->thresh_mult[THR_NEARESTMV] = 0;
970 sf->thresh_mult[THR_ZEROMV ] = 0;
971 sf->thresh_mult[THR_NEARMV ] = 0;
972 sf->thresh_mult[THR_NEWMV ] = 2000;
973 sf->thresh_mult[THR_SPLITMV ] = 10000;
976 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
978 sf->thresh_mult[THR_NEARESTG ] = 1000;
979 sf->thresh_mult[THR_ZEROG ] = 1000;
980 sf->thresh_mult[THR_NEARG ] = 1000;
981 sf->thresh_mult[THR_NEWG ] = 2000;
982 sf->thresh_mult[THR_SPLITG ] = 20000;
985 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
987 sf->thresh_mult[THR_NEARESTA ] = 1000;
988 sf->thresh_mult[THR_ZEROA ] = 1000;
989 sf->thresh_mult[THR_NEARA ] = 1000;
990 sf->thresh_mult[THR_NEWA ] = 2000;
991 sf->thresh_mult[THR_SPLITA ] = 20000;
994 sf->improved_quant = 0;
995 sf->improved_dct = 0;
1000 cpi->mode_check_freq[THR_SPLITMV] = 7;
1001 cpi->mode_check_freq[THR_SPLITG] = 15;
1002 cpi->mode_check_freq[THR_SPLITA] = 15;
1004 sf->thresh_mult[THR_TM ] = 2000;
1005 sf->thresh_mult[THR_V_PRED ] = 2000;
1006 sf->thresh_mult[THR_H_PRED ] = 2000;
1007 sf->thresh_mult[THR_B_PRED ] = 5000;
1009 if (cpi->ref_frame_flags & VP8_LAST_FLAG)
1011 sf->thresh_mult[THR_NEWMV ] = 2000;
1012 sf->thresh_mult[THR_SPLITMV ] = 25000;
1015 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
1017 sf->thresh_mult[THR_NEARESTG ] = 2000;
1018 sf->thresh_mult[THR_ZEROG ] = 2000;
1019 sf->thresh_mult[THR_NEARG ] = 2000;
1020 sf->thresh_mult[THR_NEWG ] = 2500;
1021 sf->thresh_mult[THR_SPLITG ] = 50000;
1024 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
1026 sf->thresh_mult[THR_NEARESTA ] = 2000;
1027 sf->thresh_mult[THR_ZEROA ] = 2000;
1028 sf->thresh_mult[THR_NEARA ] = 2000;
1029 sf->thresh_mult[THR_NEWA ] = 2500;
1030 sf->thresh_mult[THR_SPLITA ] = 50000;
1033 sf->full_freq[0] = 31;
1034 sf->full_freq[1] = 63;
1039 sf->auto_filter = 0; // Faster selection of loop filter
1041 cpi->mode_check_freq[THR_V_PRED] = 2;
1042 cpi->mode_check_freq[THR_H_PRED] = 2;
1043 cpi->mode_check_freq[THR_B_PRED] = 2;
1045 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
1047 cpi->mode_check_freq[THR_NEARG] = 2;
1048 cpi->mode_check_freq[THR_NEWG] = 4;
1051 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
1053 cpi->mode_check_freq[THR_NEARA] = 2;
1054 cpi->mode_check_freq[THR_NEWA] = 4;
1057 sf->thresh_mult[THR_SPLITMV ] = INT_MAX;
1058 sf->thresh_mult[THR_SPLITG ] = INT_MAX;
1059 sf->thresh_mult[THR_SPLITA ] = INT_MAX;
1061 sf->full_freq[0] = 63;
1062 sf->full_freq[1] = 127;
1068 sf->full_freq[0] = INT_MAX;
1069 sf->full_freq[1] = INT_MAX;
1071 sf->auto_filter = 1;
1076 sf->auto_filter = 0; // Faster selection of loop filter
1078 #if CONFIG_REALTIME_ONLY
1079 sf->search_method = HEX;
1081 sf->search_method = DIAMOND;
1084 cpi->mode_check_freq[THR_V_PRED] = 4;
1085 cpi->mode_check_freq[THR_H_PRED] = 4;
1086 cpi->mode_check_freq[THR_B_PRED] = 4;
1088 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
1090 cpi->mode_check_freq[THR_NEARG] = 2;
1091 cpi->mode_check_freq[THR_NEWG] = 4;
1094 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
1096 cpi->mode_check_freq[THR_NEARA] = 2;
1097 cpi->mode_check_freq[THR_NEWA] = 4;
1100 sf->thresh_mult[THR_TM ] = 2000;
1101 sf->thresh_mult[THR_B_PRED ] = 5000;
1103 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
1105 sf->thresh_mult[THR_NEARESTG ] = 2000;
1106 sf->thresh_mult[THR_ZEROG ] = 2000;
1107 sf->thresh_mult[THR_NEARG ] = 2000;
1108 sf->thresh_mult[THR_NEWG ] = 4000;
1111 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
1113 sf->thresh_mult[THR_NEARESTA ] = 2000;
1114 sf->thresh_mult[THR_ZEROA ] = 2000;
1115 sf->thresh_mult[THR_NEARA ] = 2000;
1116 sf->thresh_mult[THR_NEWA ] = 4000;
1122 // Disable split MB intra prediction mode
1123 sf->thresh_mult[THR_B_PRED] = INT_MAX;
1128 unsigned int i, sum = 0;
1129 unsigned int total_mbs = cm->MBs;
1134 sf->iterative_sub_pixel = 0;
1136 if (cpi->oxcf.encode_breakout > 2000)
1137 min = cpi->oxcf.encode_breakout;
1141 for (i = 0; i < min; i++)
1143 sum += cpi->error_bins[i];
1149 // i starts from 2 to make sure thresh started from 2048
1150 for (; i < 1024; i++)
1152 sum += cpi->error_bins[i];
1154 if (10 * sum >= (unsigned int)(cpi->Speed - 6)*(total_mbs - total_skip))
1164 if (cpi->ref_frame_flags & VP8_LAST_FLAG)
1166 sf->thresh_mult[THR_NEWMV] = thresh;
1167 sf->thresh_mult[THR_NEARESTMV ] = thresh >> 1;
1168 sf->thresh_mult[THR_NEARMV ] = thresh >> 1;
1171 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
1173 sf->thresh_mult[THR_NEWG] = thresh << 1;
1174 sf->thresh_mult[THR_NEARESTG ] = thresh;
1175 sf->thresh_mult[THR_NEARG ] = thresh;
1178 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
1180 sf->thresh_mult[THR_NEWA] = thresh << 1;
1181 sf->thresh_mult[THR_NEARESTA ] = thresh;
1182 sf->thresh_mult[THR_NEARA ] = thresh;
1185 // Disable other intra prediction modes
1186 sf->thresh_mult[THR_TM] = INT_MAX;
1187 sf->thresh_mult[THR_V_PRED] = INT_MAX;
1188 sf->thresh_mult[THR_H_PRED] = INT_MAX;
1194 sf->quarter_pixel_search = 0;
1199 int Tmp = cpi->Speed - 8;
1204 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
1206 cpi->mode_check_freq[THR_ZEROG] = 1 << (Tmp - 1);
1207 cpi->mode_check_freq[THR_NEARESTG] = 1 << (Tmp - 1);
1208 cpi->mode_check_freq[THR_NEARG] = 1 << Tmp;
1209 cpi->mode_check_freq[THR_NEWG] = 1 << (Tmp + 1);
1212 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
1214 cpi->mode_check_freq[THR_ZEROA] = 1 << (Tmp - 1);
1215 cpi->mode_check_freq[THR_NEARESTA] = 1 << (Tmp - 1);
1216 cpi->mode_check_freq[THR_NEARA] = 1 << Tmp;
1217 cpi->mode_check_freq[THR_NEWA] = 1 << (Tmp + 1);
1220 cpi->mode_check_freq[THR_NEWMV] = 1 << (Tmp - 1);
1223 cm->filter_type = NORMAL_LOOPFILTER;
1226 cm->filter_type = SIMPLE_LOOPFILTER;
1230 sf->half_pixel_search = 0; // This has a big hit on quality. Last resort
1233 vpx_memset(cpi->error_bins, 0, sizeof(cpi->error_bins));
1237 if (cpi->sf.search_method == NSTEP)
1239 vp8_init3smotion_compensation(&cpi->mb, cm->yv12_fb[cm->lst_fb_idx].y_stride);
1241 else if (cpi->sf.search_method == DIAMOND)
1243 vp8_init_dsmotion_compensation(&cpi->mb, cm->yv12_fb[cm->lst_fb_idx].y_stride);
1246 if (cpi->sf.improved_dct)
1248 cpi->mb.vp8_short_fdct8x4 = FDCT_INVOKE(&cpi->rtcd.fdct, short8x4);
1249 cpi->mb.vp8_short_fdct4x4 = FDCT_INVOKE(&cpi->rtcd.fdct, short4x4);
1253 cpi->mb.vp8_short_fdct8x4 = FDCT_INVOKE(&cpi->rtcd.fdct, fast8x4);
1254 cpi->mb.vp8_short_fdct4x4 = FDCT_INVOKE(&cpi->rtcd.fdct, fast4x4);
1257 cpi->mb.short_walsh4x4 = FDCT_INVOKE(&cpi->rtcd.fdct, walsh_short4x4);
1259 if (cpi->sf.improved_quant)
1261 cpi->mb.quantize_b = QUANTIZE_INVOKE(&cpi->rtcd.quantize, quantb);
1265 cpi->mb.quantize_b = QUANTIZE_INVOKE(&cpi->rtcd.quantize, fastquantb);
1267 if (cpi->sf.improved_quant != last_improved_quant)
1268 vp8cx_init_quantizer(cpi);
1270 #if CONFIG_RUNTIME_CPU_DETECT
1271 cpi->mb.e_mbd.rtcd = &cpi->common.rtcd;
1274 if (cpi->sf.iterative_sub_pixel == 1)
1276 cpi->find_fractional_mv_step = vp8_find_best_sub_pixel_step_iteratively;
1278 else if (cpi->sf.quarter_pixel_search)
1280 cpi->find_fractional_mv_step = vp8_find_best_sub_pixel_step;
1282 else if (cpi->sf.half_pixel_search)
1284 cpi->find_fractional_mv_step = vp8_find_best_half_pixel_step;
1288 cpi->find_fractional_mv_step = vp8_skip_fractional_mv_step;
1291 if (cpi->sf.optimize_coefficients == 1)
1292 cpi->mb.optimize = 1 + cpi->is_next_src_alt_ref;
1294 cpi->mb.optimize = 0;
1296 if (cpi->common.full_pixel)
1297 cpi->find_fractional_mv_step = vp8_skip_fractional_mv_step;
1300 frames_at_speed[cpi->Speed]++;
1303 static void alloc_raw_frame_buffers(VP8_COMP *cpi)
1307 buffers = cpi->oxcf.lag_in_frames;
1309 if (buffers > MAX_LAG_BUFFERS)
1310 buffers = MAX_LAG_BUFFERS;
1315 for (i = 0; i < buffers; i++)
1316 if (vp8_yv12_alloc_frame_buffer(&cpi->src_buffer[i].source_buffer,
1317 cpi->oxcf.Width, cpi->oxcf.Height,
1319 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1320 "Failed to allocate lag buffer");
1322 #if VP8_TEMPORAL_ALT_REF
1324 if (vp8_yv12_alloc_frame_buffer(&cpi->alt_ref_buffer.source_buffer,
1325 cpi->oxcf.Width, cpi->oxcf.Height, 16))
1326 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1327 "Failed to allocate altref buffer");
1331 cpi->source_buffer_count = 0;
1334 static int vp8_alloc_partition_data(VP8_COMP *cpi)
1337 vpx_free(cpi->mb.pip);
1339 cpi->mb.pip = vpx_calloc((cpi->common.mb_cols + 1) *
1340 (cpi->common.mb_rows + 1),
1341 sizeof(PARTITION_INFO));
1343 return ALLOC_FAILURE;
1345 cpi->mb.pi = cpi->mb.pip + cpi->common.mode_info_stride + 1;
1350 void vp8_alloc_compressor_data(VP8_COMP *cpi)
1352 VP8_COMMON *cm = & cpi->common;
1354 int width = cm->Width;
1355 int height = cm->Height;
1357 if (vp8_alloc_frame_buffers(cm, width, height))
1358 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1359 "Failed to allocate frame buffers");
1361 if (vp8_alloc_partition_data(cpi))
1362 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1363 "Failed to allocate partition data");
1366 if ((width & 0xf) != 0)
1367 width += 16 - (width & 0xf);
1369 if ((height & 0xf) != 0)
1370 height += 16 - (height & 0xf);
1373 if (vp8_yv12_alloc_frame_buffer(&cpi->last_frame_uf,
1374 width, height, VP8BORDERINPIXELS))
1375 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1376 "Failed to allocate last frame buffer");
1378 if (vp8_yv12_alloc_frame_buffer(&cpi->scaled_source, width, height, 16))
1379 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1380 "Failed to allocate scaled source buffer");
1387 unsigned int tokens = cm->mb_rows * cm->mb_cols * 24 * 16;
1389 CHECK_MEM_ERROR(cpi->tok, vpx_calloc(tokens, sizeof(*cpi->tok)));
1392 // Data used for real time vc mode to see if gf needs refreshing
1393 cpi->inter_zz_count = 0;
1394 cpi->gf_bad_count = 0;
1395 cpi->gf_update_recommended = 0;
1398 // Structures used to minitor GF usage
1399 if (cpi->gf_active_flags != 0)
1400 vpx_free(cpi->gf_active_flags);
1402 CHECK_MEM_ERROR(cpi->gf_active_flags, vpx_calloc(1, cm->mb_rows * cm->mb_cols));
1404 cpi->gf_active_count = cm->mb_rows * cm->mb_cols;
1406 if(cpi->total_stats)
1407 vpx_free(cpi->total_stats);
1409 cpi->total_stats = vpx_calloc(1, vp8_firstpass_stats_sz(cpi->common.MBs));
1411 if(cpi->this_frame_stats)
1412 vpx_free(cpi->this_frame_stats);
1414 cpi->this_frame_stats = vpx_calloc(1, vp8_firstpass_stats_sz(cpi->common.MBs));
1416 if(!cpi->total_stats || !cpi->this_frame_stats)
1417 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1418 "Failed to allocate firstpass stats");
1423 static const int q_trans[] =
1425 0, 1, 2, 3, 4, 5, 7, 8,
1426 9, 10, 12, 13, 15, 17, 18, 19,
1427 20, 21, 23, 24, 25, 26, 27, 28,
1428 29, 30, 31, 33, 35, 37, 39, 41,
1429 43, 45, 47, 49, 51, 53, 55, 57,
1430 59, 61, 64, 67, 70, 73, 76, 79,
1431 82, 85, 88, 91, 94, 97, 100, 103,
1432 106, 109, 112, 115, 118, 121, 124, 127,
1435 int vp8_reverse_trans(int x)
1439 for (i = 0; i < 64; i++)
1440 if (q_trans[i] >= x)
1445 void vp8_new_frame_rate(VP8_COMP *cpi, double framerate)
1450 cpi->oxcf.frame_rate = framerate;
1451 cpi->output_frame_rate = cpi->oxcf.frame_rate;
1452 cpi->per_frame_bandwidth = (int)(cpi->oxcf.target_bandwidth / cpi->output_frame_rate);
1453 cpi->av_per_frame_bandwidth = (int)(cpi->oxcf.target_bandwidth / cpi->output_frame_rate);
1454 cpi->min_frame_bandwidth = (int)(cpi->av_per_frame_bandwidth * cpi->oxcf.two_pass_vbrmin_section / 100);
1455 cpi->max_gf_interval = (int)(cpi->output_frame_rate / 2) + 2;
1457 //cpi->max_gf_interval = (int)(cpi->output_frame_rate * 2 / 3) + 1;
1458 //cpi->max_gf_interval = 24;
1460 if (cpi->max_gf_interval < 12)
1461 cpi->max_gf_interval = 12;
1464 // Special conditions when altr ref frame enabled in lagged compress mode
1465 if (cpi->oxcf.play_alternate && cpi->oxcf.lag_in_frames)
1467 if (cpi->max_gf_interval > cpi->oxcf.lag_in_frames - 1)
1468 cpi->max_gf_interval = cpi->oxcf.lag_in_frames - 1;
1474 rescale(int val, int num, int denom)
1476 int64_t llnum = num;
1477 int64_t llden = denom;
1478 int64_t llval = val;
1480 return llval * llnum / llden;
1484 void vp8_init_config(VP8_PTR ptr, VP8_CONFIG *oxcf)
1486 VP8_COMP *cpi = (VP8_COMP *)(ptr);
1487 VP8_COMMON *cm = &cpi->common;
1493 cpi->auto_adjust_gold_quantizer = 1;
1494 cpi->goldquantizer = 1;
1496 cpi->auto_adjust_key_quantizer = 1;
1497 cpi->keyquantizer = 1;
1499 cm->version = oxcf->Version;
1500 vp8_setup_version(cm);
1506 cpi->auto_worst_q = 0;
1507 cpi->oxcf.best_allowed_q = MINQ;
1508 cpi->oxcf.worst_allowed_q = MAXQ;
1510 cpi->oxcf.end_usage = USAGE_STREAM_FROM_SERVER;
1511 cpi->oxcf.starting_buffer_level = 4000;
1512 cpi->oxcf.optimal_buffer_level = 5000;
1513 cpi->oxcf.maximum_buffer_size = 6000;
1514 cpi->oxcf.under_shoot_pct = 90;
1515 cpi->oxcf.allow_df = 0;
1516 cpi->oxcf.drop_frames_water_mark = 20;
1518 cpi->oxcf.allow_spatial_resampling = 0;
1519 cpi->oxcf.resample_down_water_mark = 40;
1520 cpi->oxcf.resample_up_water_mark = 60;
1522 cpi->oxcf.fixed_q = cpi->interquantizer;
1524 cpi->filter_type = NORMAL_LOOPFILTER;
1526 if (cm->simpler_lpf)
1527 cpi->filter_type = SIMPLE_LOOPFILTER;
1529 cpi->compressor_speed = 1;
1530 cpi->horiz_scale = 0;
1531 cpi->vert_scale = 0;
1532 cpi->oxcf.two_pass_vbrbias = 50;
1533 cpi->oxcf.two_pass_vbrmax_section = 400;
1534 cpi->oxcf.two_pass_vbrmin_section = 0;
1536 cpi->oxcf.Sharpness = 0;
1537 cpi->oxcf.noise_sensitivity = 0;
1543 switch (cpi->oxcf.Mode)
1548 cpi->compressor_speed = 2;
1550 if (cpi->oxcf.cpu_used < -16)
1552 cpi->oxcf.cpu_used = -16;
1555 if (cpi->oxcf.cpu_used > 16)
1556 cpi->oxcf.cpu_used = 16;
1560 #if !(CONFIG_REALTIME_ONLY)
1561 case MODE_GOODQUALITY:
1563 cpi->compressor_speed = 1;
1565 if (cpi->oxcf.cpu_used < -5)
1567 cpi->oxcf.cpu_used = -5;
1570 if (cpi->oxcf.cpu_used > 5)
1571 cpi->oxcf.cpu_used = 5;
1575 case MODE_BESTQUALITY:
1577 cpi->compressor_speed = 0;
1580 case MODE_FIRSTPASS:
1582 cpi->compressor_speed = 1;
1584 case MODE_SECONDPASS:
1586 cpi->compressor_speed = 1;
1588 if (cpi->oxcf.cpu_used < -5)
1590 cpi->oxcf.cpu_used = -5;
1593 if (cpi->oxcf.cpu_used > 5)
1594 cpi->oxcf.cpu_used = 5;
1597 case MODE_SECONDPASS_BEST:
1599 cpi->compressor_speed = 0;
1605 cpi->auto_worst_q = 1;
1607 cpi->oxcf.worst_allowed_q = q_trans[oxcf->worst_allowed_q];
1608 cpi->oxcf.best_allowed_q = q_trans[oxcf->best_allowed_q];
1610 if (oxcf->fixed_q >= 0)
1612 if (oxcf->worst_allowed_q < 0)
1613 cpi->oxcf.fixed_q = q_trans[0];
1615 cpi->oxcf.fixed_q = q_trans[oxcf->worst_allowed_q];
1617 if (oxcf->alt_q < 0)
1618 cpi->oxcf.alt_q = q_trans[0];
1620 cpi->oxcf.alt_q = q_trans[oxcf->alt_q];
1622 if (oxcf->key_q < 0)
1623 cpi->oxcf.key_q = q_trans[0];
1625 cpi->oxcf.key_q = q_trans[oxcf->key_q];
1627 if (oxcf->gold_q < 0)
1628 cpi->oxcf.gold_q = q_trans[0];
1630 cpi->oxcf.gold_q = q_trans[oxcf->gold_q];
1634 cpi->baseline_gf_interval = cpi->oxcf.alt_freq ? cpi->oxcf.alt_freq : DEFAULT_GF_INTERVAL;
1635 cpi->ref_frame_flags = VP8_ALT_FLAG | VP8_GOLD_FLAG | VP8_LAST_FLAG;
1637 //cpi->use_golden_frame_only = 0;
1638 //cpi->use_last_frame_only = 0;
1639 cm->refresh_golden_frame = 0;
1640 cm->refresh_last_frame = 1;
1641 cm->refresh_entropy_probs = 1;
1643 if (cpi->oxcf.token_partitions >= 0 && cpi->oxcf.token_partitions <= 3)
1644 cm->multi_token_partition = (TOKEN_PARTITION) cpi->oxcf.token_partitions;
1646 setup_features(cpi);
1651 for (i = 0; i < MAX_MB_SEGMENTS; i++)
1652 cpi->segment_encode_breakout[i] = cpi->oxcf.encode_breakout;
1655 // At the moment the first order values may not be > MAXQ
1656 if (cpi->oxcf.fixed_q > MAXQ)
1657 cpi->oxcf.fixed_q = MAXQ;
1659 // local file playback mode == really big buffer
1660 if (cpi->oxcf.end_usage == USAGE_LOCAL_FILE_PLAYBACK)
1662 cpi->oxcf.starting_buffer_level = 60000;
1663 cpi->oxcf.optimal_buffer_level = 60000;
1664 cpi->oxcf.maximum_buffer_size = 240000;
1669 // Convert target bandwidth from Kbit/s to Bit/s
1670 cpi->oxcf.target_bandwidth *= 1000;
1671 cpi->oxcf.starting_buffer_level =
1672 rescale(cpi->oxcf.starting_buffer_level,
1673 cpi->oxcf.target_bandwidth, 1000);
1675 if (cpi->oxcf.optimal_buffer_level == 0)
1676 cpi->oxcf.optimal_buffer_level = cpi->oxcf.target_bandwidth / 8;
1678 cpi->oxcf.optimal_buffer_level =
1679 rescale(cpi->oxcf.optimal_buffer_level,
1680 cpi->oxcf.target_bandwidth, 1000);
1682 if (cpi->oxcf.maximum_buffer_size == 0)
1683 cpi->oxcf.maximum_buffer_size = cpi->oxcf.target_bandwidth / 8;
1685 cpi->oxcf.maximum_buffer_size =
1686 rescale(cpi->oxcf.maximum_buffer_size,
1687 cpi->oxcf.target_bandwidth, 1000);
1689 cpi->buffer_level = cpi->oxcf.starting_buffer_level;
1690 cpi->bits_off_target = cpi->oxcf.starting_buffer_level;
1692 vp8_new_frame_rate(cpi, cpi->oxcf.frame_rate);
1693 cpi->worst_quality = cpi->oxcf.worst_allowed_q;
1694 cpi->active_worst_quality = cpi->oxcf.worst_allowed_q;
1695 cpi->avg_frame_qindex = cpi->oxcf.worst_allowed_q;
1696 cpi->best_quality = cpi->oxcf.best_allowed_q;
1697 cpi->active_best_quality = cpi->oxcf.best_allowed_q;
1698 cpi->buffered_mode = (cpi->oxcf.optimal_buffer_level > 0) ? TRUE : FALSE;
1700 cpi->rolling_target_bits = cpi->av_per_frame_bandwidth;
1701 cpi->rolling_actual_bits = cpi->av_per_frame_bandwidth;
1702 cpi->long_rolling_target_bits = cpi->av_per_frame_bandwidth;
1703 cpi->long_rolling_actual_bits = cpi->av_per_frame_bandwidth;
1705 cpi->total_actual_bits = 0;
1706 cpi->total_target_vs_actual = 0;
1708 // Only allow dropped frames in buffered mode
1709 cpi->drop_frames_allowed = cpi->oxcf.allow_df && cpi->buffered_mode;
1711 cm->filter_type = (LOOPFILTERTYPE) cpi->filter_type;
1713 if (!cm->use_bilinear_mc_filter)
1714 cm->mcomp_filter_type = SIXTAP;
1716 cm->mcomp_filter_type = BILINEAR;
1718 cpi->target_bandwidth = cpi->oxcf.target_bandwidth;
1720 cm->Width = cpi->oxcf.Width ;
1721 cm->Height = cpi->oxcf.Height ;
1723 cpi->intra_frame_target = (4 * (cm->Width + cm->Height) / 15) * 1000; // As per VP8
1725 cm->horiz_scale = cpi->horiz_scale;
1726 cm->vert_scale = cpi->vert_scale ;
1728 // VP8 sharpness level mapping 0-7 (vs 0-10 in general VPx dialogs)
1729 if (cpi->oxcf.Sharpness > 7)
1730 cpi->oxcf.Sharpness = 7;
1732 cm->sharpness_level = cpi->oxcf.Sharpness;
1734 if (cm->horiz_scale != NORMAL || cm->vert_scale != NORMAL)
1736 int UNINITIALIZED_IS_SAFE(hr), UNINITIALIZED_IS_SAFE(hs);
1737 int UNINITIALIZED_IS_SAFE(vr), UNINITIALIZED_IS_SAFE(vs);
1739 Scale2Ratio(cm->horiz_scale, &hr, &hs);
1740 Scale2Ratio(cm->vert_scale, &vr, &vs);
1742 // always go to the next whole number
1743 cm->Width = (hs - 1 + cpi->oxcf.Width * hr) / hs;
1744 cm->Height = (vs - 1 + cpi->oxcf.Height * vr) / vs;
1747 if (((cm->Width + 15) & 0xfffffff0) != cm->yv12_fb[cm->lst_fb_idx].y_width ||
1748 ((cm->Height + 15) & 0xfffffff0) != cm->yv12_fb[cm->lst_fb_idx].y_height ||
1749 cm->yv12_fb[cm->lst_fb_idx].y_width == 0)
1751 alloc_raw_frame_buffers(cpi);
1752 vp8_alloc_compressor_data(cpi);
1755 // Clamp KF frame size to quarter of data rate
1756 if (cpi->intra_frame_target > cpi->target_bandwidth >> 2)
1757 cpi->intra_frame_target = cpi->target_bandwidth >> 2;
1759 if (cpi->oxcf.fixed_q >= 0)
1761 cpi->last_q[0] = cpi->oxcf.fixed_q;
1762 cpi->last_q[1] = cpi->oxcf.fixed_q;
1765 cpi->Speed = cpi->oxcf.cpu_used;
1767 // force to allowlag to 0 if lag_in_frames is 0;
1768 if (cpi->oxcf.lag_in_frames == 0)
1770 cpi->oxcf.allow_lag = 0;
1772 // Limit on lag buffers as these are not currently dynamically allocated
1773 else if (cpi->oxcf.lag_in_frames > MAX_LAG_BUFFERS)
1774 cpi->oxcf.lag_in_frames = MAX_LAG_BUFFERS;
1777 cpi->last_alt_ref_sei = -1;
1778 cpi->is_src_frame_alt_ref = 0;
1779 cpi->is_next_src_alt_ref = 0;
1782 // Experimental RD Code
1783 cpi->frame_distortion = 0;
1784 cpi->last_frame_distortion = 0;
1787 #if VP8_TEMPORAL_ALT_REF
1789 cpi->use_weighted_temporal_filter = 0;
1794 cpi->fixed_divide[0] = 0;
1796 for (i = 1; i < 512; i++)
1797 cpi->fixed_divide[i] = 0x80000 / i;
1803 * This function needs more clean up, i.e. be more tuned torwards
1804 * change_config rather than init_config !!!!!!!!!!!!!!!!
1809 void vp8_change_config(VP8_PTR ptr, VP8_CONFIG *oxcf)
1811 VP8_COMP *cpi = (VP8_COMP *)(ptr);
1812 VP8_COMMON *cm = &cpi->common;
1820 if (cm->version != oxcf->Version)
1822 cm->version = oxcf->Version;
1823 vp8_setup_version(cm);
1828 switch (cpi->oxcf.Mode)
1833 cpi->compressor_speed = 2;
1835 if (cpi->oxcf.cpu_used < -16)
1837 cpi->oxcf.cpu_used = -16;
1840 if (cpi->oxcf.cpu_used > 16)
1841 cpi->oxcf.cpu_used = 16;
1845 #if !(CONFIG_REALTIME_ONLY)
1846 case MODE_GOODQUALITY:
1848 cpi->compressor_speed = 1;
1850 if (cpi->oxcf.cpu_used < -5)
1852 cpi->oxcf.cpu_used = -5;
1855 if (cpi->oxcf.cpu_used > 5)
1856 cpi->oxcf.cpu_used = 5;
1860 case MODE_BESTQUALITY:
1862 cpi->compressor_speed = 0;
1865 case MODE_FIRSTPASS:
1867 cpi->compressor_speed = 1;
1869 case MODE_SECONDPASS:
1871 cpi->compressor_speed = 1;
1873 if (cpi->oxcf.cpu_used < -5)
1875 cpi->oxcf.cpu_used = -5;
1878 if (cpi->oxcf.cpu_used > 5)
1879 cpi->oxcf.cpu_used = 5;
1882 case MODE_SECONDPASS_BEST:
1884 cpi->compressor_speed = 0;
1890 cpi->auto_worst_q = 1;
1892 cpi->oxcf.worst_allowed_q = q_trans[oxcf->worst_allowed_q];
1893 cpi->oxcf.best_allowed_q = q_trans[oxcf->best_allowed_q];
1895 if (oxcf->fixed_q >= 0)
1897 if (oxcf->worst_allowed_q < 0)
1898 cpi->oxcf.fixed_q = q_trans[0];
1900 cpi->oxcf.fixed_q = q_trans[oxcf->worst_allowed_q];
1902 if (oxcf->alt_q < 0)
1903 cpi->oxcf.alt_q = q_trans[0];
1905 cpi->oxcf.alt_q = q_trans[oxcf->alt_q];
1907 if (oxcf->key_q < 0)
1908 cpi->oxcf.key_q = q_trans[0];
1910 cpi->oxcf.key_q = q_trans[oxcf->key_q];
1912 if (oxcf->gold_q < 0)
1913 cpi->oxcf.gold_q = q_trans[0];
1915 cpi->oxcf.gold_q = q_trans[oxcf->gold_q];
1919 cpi->baseline_gf_interval = cpi->oxcf.alt_freq ? cpi->oxcf.alt_freq : DEFAULT_GF_INTERVAL;
1921 cpi->ref_frame_flags = VP8_ALT_FLAG | VP8_GOLD_FLAG | VP8_LAST_FLAG;
1923 //cpi->use_golden_frame_only = 0;
1924 //cpi->use_last_frame_only = 0;
1925 cm->refresh_golden_frame = 0;
1926 cm->refresh_last_frame = 1;
1927 cm->refresh_entropy_probs = 1;
1929 if (cpi->oxcf.token_partitions >= 0 && cpi->oxcf.token_partitions <= 3)
1930 cm->multi_token_partition = (TOKEN_PARTITION) cpi->oxcf.token_partitions;
1932 setup_features(cpi);
1937 for (i = 0; i < MAX_MB_SEGMENTS; i++)
1938 cpi->segment_encode_breakout[i] = cpi->oxcf.encode_breakout;
1941 // At the moment the first order values may not be > MAXQ
1942 if (cpi->oxcf.fixed_q > MAXQ)
1943 cpi->oxcf.fixed_q = MAXQ;
1945 // local file playback mode == really big buffer
1946 if (cpi->oxcf.end_usage == USAGE_LOCAL_FILE_PLAYBACK)
1948 cpi->oxcf.starting_buffer_level = 60000;
1949 cpi->oxcf.optimal_buffer_level = 60000;
1950 cpi->oxcf.maximum_buffer_size = 240000;
1954 // Convert target bandwidth from Kbit/s to Bit/s
1955 cpi->oxcf.target_bandwidth *= 1000;
1957 cpi->oxcf.starting_buffer_level =
1958 rescale(cpi->oxcf.starting_buffer_level,
1959 cpi->oxcf.target_bandwidth, 1000);
1961 if (cpi->oxcf.optimal_buffer_level == 0)
1962 cpi->oxcf.optimal_buffer_level = cpi->oxcf.target_bandwidth / 8;
1964 cpi->oxcf.optimal_buffer_level =
1965 rescale(cpi->oxcf.optimal_buffer_level,
1966 cpi->oxcf.target_bandwidth, 1000);
1968 if (cpi->oxcf.maximum_buffer_size == 0)
1969 cpi->oxcf.maximum_buffer_size = cpi->oxcf.target_bandwidth / 8;
1971 cpi->oxcf.maximum_buffer_size =
1972 rescale(cpi->oxcf.maximum_buffer_size,
1973 cpi->oxcf.target_bandwidth, 1000);
1975 cpi->buffer_level = cpi->oxcf.starting_buffer_level;
1976 cpi->bits_off_target = cpi->oxcf.starting_buffer_level;
1978 vp8_new_frame_rate(cpi, cpi->oxcf.frame_rate);
1979 cpi->worst_quality = cpi->oxcf.worst_allowed_q;
1980 cpi->active_worst_quality = cpi->oxcf.worst_allowed_q;
1981 cpi->avg_frame_qindex = cpi->oxcf.worst_allowed_q;
1982 cpi->best_quality = cpi->oxcf.best_allowed_q;
1983 cpi->active_best_quality = cpi->oxcf.best_allowed_q;
1984 cpi->buffered_mode = (cpi->oxcf.optimal_buffer_level > 0) ? TRUE : FALSE;
1986 cpi->rolling_target_bits = cpi->av_per_frame_bandwidth;
1987 cpi->rolling_actual_bits = cpi->av_per_frame_bandwidth;
1988 cpi->long_rolling_target_bits = cpi->av_per_frame_bandwidth;
1989 cpi->long_rolling_actual_bits = cpi->av_per_frame_bandwidth;
1991 cpi->total_actual_bits = 0;
1992 cpi->total_target_vs_actual = 0;
1994 // Only allow dropped frames in buffered mode
1995 cpi->drop_frames_allowed = cpi->oxcf.allow_df && cpi->buffered_mode;
1997 cm->filter_type = (LOOPFILTERTYPE) cpi->filter_type;
1999 if (!cm->use_bilinear_mc_filter)
2000 cm->mcomp_filter_type = SIXTAP;
2002 cm->mcomp_filter_type = BILINEAR;
2004 cpi->target_bandwidth = cpi->oxcf.target_bandwidth;
2006 cm->Width = cpi->oxcf.Width ;
2007 cm->Height = cpi->oxcf.Height ;
2009 cm->horiz_scale = cpi->horiz_scale;
2010 cm->vert_scale = cpi->vert_scale ;
2012 cpi->intra_frame_target = (4 * (cm->Width + cm->Height) / 15) * 1000; // As per VP8
2014 // VP8 sharpness level mapping 0-7 (vs 0-10 in general VPx dialogs)
2015 if (cpi->oxcf.Sharpness > 7)
2016 cpi->oxcf.Sharpness = 7;
2018 cm->sharpness_level = cpi->oxcf.Sharpness;
2020 if (cm->horiz_scale != NORMAL || cm->vert_scale != NORMAL)
2022 int UNINITIALIZED_IS_SAFE(hr), UNINITIALIZED_IS_SAFE(hs);
2023 int UNINITIALIZED_IS_SAFE(vr), UNINITIALIZED_IS_SAFE(vs);
2025 Scale2Ratio(cm->horiz_scale, &hr, &hs);
2026 Scale2Ratio(cm->vert_scale, &vr, &vs);
2028 // always go to the next whole number
2029 cm->Width = (hs - 1 + cpi->oxcf.Width * hr) / hs;
2030 cm->Height = (vs - 1 + cpi->oxcf.Height * vr) / vs;
2033 if (((cm->Width + 15) & 0xfffffff0) != cm->yv12_fb[cm->lst_fb_idx].y_width ||
2034 ((cm->Height + 15) & 0xfffffff0) != cm->yv12_fb[cm->lst_fb_idx].y_height ||
2035 cm->yv12_fb[cm->lst_fb_idx].y_width == 0)
2037 alloc_raw_frame_buffers(cpi);
2038 vp8_alloc_compressor_data(cpi);
2041 // Clamp KF frame size to quarter of data rate
2042 if (cpi->intra_frame_target > cpi->target_bandwidth >> 2)
2043 cpi->intra_frame_target = cpi->target_bandwidth >> 2;
2045 if (cpi->oxcf.fixed_q >= 0)
2047 cpi->last_q[0] = cpi->oxcf.fixed_q;
2048 cpi->last_q[1] = cpi->oxcf.fixed_q;
2051 cpi->Speed = cpi->oxcf.cpu_used;
2053 // force to allowlag to 0 if lag_in_frames is 0;
2054 if (cpi->oxcf.lag_in_frames == 0)
2056 cpi->oxcf.allow_lag = 0;
2058 // Limit on lag buffers as these are not currently dynamically allocated
2059 else if (cpi->oxcf.lag_in_frames > MAX_LAG_BUFFERS)
2060 cpi->oxcf.lag_in_frames = MAX_LAG_BUFFERS;
2063 cpi->last_alt_ref_sei = -1;
2064 cpi->is_src_frame_alt_ref = 0;
2065 cpi->is_next_src_alt_ref = 0;
2068 // Experimental RD Code
2069 cpi->frame_distortion = 0;
2070 cpi->last_frame_distortion = 0;
2075 #define M_LOG2_E 0.693147180559945309417
2076 #define log2f(x) (log (x) / (float) M_LOG2_E)
2077 static void cal_mvsadcosts(int *mvsadcost[2])
2081 mvsadcost [0] [0] = 300;
2082 mvsadcost [1] [0] = 300;
2086 double z = 256 * (2 * (log2f(2 * i) + .6));
2087 mvsadcost [0][i] = (int) z;
2088 mvsadcost [1][i] = (int) z;
2089 mvsadcost [0][-i] = (int) z;
2090 mvsadcost [1][-i] = (int) z;
2092 while (++i <= mv_max);
2095 VP8_PTR vp8_create_compressor(VP8_CONFIG *oxcf)
2107 cpi = ctx.cpi = vpx_memalign(32, sizeof(VP8_COMP));
2108 // Check that the CPI instance is valid
2114 vpx_memset(cpi, 0, sizeof(VP8_COMP));
2116 if (setjmp(cm->error.jmp))
2118 VP8_PTR ptr = ctx.ptr;
2120 ctx.cpi->common.error.setjmp = 0;
2121 vp8_remove_compressor(&ptr);
2125 cpi->common.error.setjmp = 1;
2127 CHECK_MEM_ERROR(cpi->rdtok, vpx_calloc(256 * 3 / 2, sizeof(TOKENEXTRA)));
2128 CHECK_MEM_ERROR(cpi->mb.ss, vpx_calloc(sizeof(search_site), (MAX_MVSEARCH_STEPS * 8) + 1));
2130 vp8_create_common(&cpi->common);
2131 vp8_cmachine_specific_config(cpi);
2133 vp8_init_config((VP8_PTR)cpi, oxcf);
2135 memcpy(cpi->base_skip_false_prob, vp8cx_base_skip_false_prob, sizeof(vp8cx_base_skip_false_prob));
2136 cpi->common.current_video_frame = 0;
2137 cpi->kf_overspend_bits = 0;
2138 cpi->kf_bitrate_adjustment = 0;
2139 cpi->frames_till_gf_update_due = 0;
2140 cpi->gf_overspend_bits = 0;
2141 cpi->non_gf_bitrate_adjustment = 0;
2142 cpi->prob_last_coded = 128;
2143 cpi->prob_gf_coded = 128;
2144 cpi->prob_intra_coded = 63;
2146 // Prime the recent reference frame useage counters.
2147 // Hereafter they will be maintained as a sort of moving average
2148 cpi->recent_ref_frame_usage[INTRA_FRAME] = 1;
2149 cpi->recent_ref_frame_usage[LAST_FRAME] = 1;
2150 cpi->recent_ref_frame_usage[GOLDEN_FRAME] = 1;
2151 cpi->recent_ref_frame_usage[ALTREF_FRAME] = 1;
2153 // Set reference frame sign bias for ALTREF frame to 1 (for now)
2154 cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = 1;
2156 cpi->gf_decay_rate = 0;
2157 cpi->baseline_gf_interval = DEFAULT_GF_INTERVAL;
2159 cpi->gold_is_last = 0 ;
2160 cpi->alt_is_last = 0 ;
2161 cpi->gold_is_alt = 0 ;
2163 // allocate memory for storing last frame's MVs for MV prediction.
2164 CHECK_MEM_ERROR(cpi->lfmv, vpx_calloc((cpi->common.mb_rows+1) * (cpi->common.mb_cols+1), sizeof(int_mv)));
2165 CHECK_MEM_ERROR(cpi->lf_ref_frame_sign_bias, vpx_calloc((cpi->common.mb_rows+1) * (cpi->common.mb_cols+1), sizeof(int)));
2166 CHECK_MEM_ERROR(cpi->lf_ref_frame, vpx_calloc((cpi->common.mb_rows+1) * (cpi->common.mb_cols+1), sizeof(int)));
2168 // Create the encoder segmentation map and set all entries to 0
2169 CHECK_MEM_ERROR(cpi->segmentation_map, vpx_calloc(cpi->common.mb_rows * cpi->common.mb_cols, 1));
2170 CHECK_MEM_ERROR(cpi->active_map, vpx_calloc(cpi->common.mb_rows * cpi->common.mb_cols, 1));
2171 vpx_memset(cpi->active_map , 1, (cpi->common.mb_rows * cpi->common.mb_cols));
2172 cpi->active_map_enabled = 0;
2174 // Create the first pass motion map structure and set to 0
2175 // Allocate space for maximum of 15 buffers
2176 CHECK_MEM_ERROR(cpi->fp_motion_map, vpx_calloc(15*cpi->common.MBs, 1));
2179 // Experimental code for lagged and one pass
2180 // Initialise one_pass GF frames stats
2181 // Update stats used for GF selection
2184 cpi->one_pass_frame_index = 0;
2186 for (i = 0; i < MAX_LAG_BUFFERS; i++)
2188 cpi->one_pass_frame_stats[i].frames_so_far = 0;
2189 cpi->one_pass_frame_stats[i].frame_intra_error = 0.0;
2190 cpi->one_pass_frame_stats[i].frame_coded_error = 0.0;
2191 cpi->one_pass_frame_stats[i].frame_pcnt_inter = 0.0;
2192 cpi->one_pass_frame_stats[i].frame_pcnt_motion = 0.0;
2193 cpi->one_pass_frame_stats[i].frame_mvr = 0.0;
2194 cpi->one_pass_frame_stats[i].frame_mvr_abs = 0.0;
2195 cpi->one_pass_frame_stats[i].frame_mvc = 0.0;
2196 cpi->one_pass_frame_stats[i].frame_mvc_abs = 0.0;
2201 // Should we use the cyclic refresh method.
2202 // Currently this is tied to error resilliant mode
2203 cpi->cyclic_refresh_mode_enabled = cpi->oxcf.error_resilient_mode;
2204 cpi->cyclic_refresh_mode_max_mbs_perframe = (cpi->common.mb_rows * cpi->common.mb_cols) / 40;
2205 cpi->cyclic_refresh_mode_index = 0;
2206 cpi->cyclic_refresh_q = 32;
2208 if (cpi->cyclic_refresh_mode_enabled)
2210 CHECK_MEM_ERROR(cpi->cyclic_refresh_map, vpx_calloc((cpi->common.mb_rows * cpi->common.mb_cols), 1));
2213 cpi->cyclic_refresh_map = (signed char *) NULL;
2215 // Test function for segmentation
2216 //segmentation_test_function((VP8_PTR) cpi);
2218 #ifdef ENTROPY_STATS
2219 init_context_counters();
2222 /*Initialize the feed-forward activity masking.*/
2223 cpi->activity_avg = 90<<12;
2225 cpi->frames_since_key = 8; // Give a sensible default for the first frame.
2226 cpi->key_frame_frequency = cpi->oxcf.key_freq;
2228 cpi->source_alt_ref_pending = FALSE;
2229 cpi->source_alt_ref_active = FALSE;
2230 cpi->common.refresh_alt_ref_frame = 0;
2232 cpi->b_calculate_psnr = CONFIG_PSNR;
2234 cpi->b_calculate_ssimg = 0;
2239 if (cpi->b_calculate_psnr)
2241 cpi->total_sq_error = 0.0;
2242 cpi->total_sq_error2 = 0.0;
2247 cpi->totalp_y = 0.0;
2248 cpi->totalp_u = 0.0;
2249 cpi->totalp_v = 0.0;
2251 cpi->tot_recode_hits = 0;
2252 cpi->summed_quality = 0;
2253 cpi->summed_weights = 0;
2256 if (cpi->b_calculate_ssimg)
2258 cpi->total_ssimg_y = 0;
2259 cpi->total_ssimg_u = 0;
2260 cpi->total_ssimg_v = 0;
2261 cpi->total_ssimg_all = 0;
2265 #define LLONG_MAX 9223372036854775807LL
2267 cpi->first_time_stamp_ever = LLONG_MAX;
2271 cpi->frames_till_gf_update_due = 0;
2272 cpi->key_frame_count = 1;
2273 cpi->tot_key_frame_bits = 0;
2275 cpi->ni_av_qi = cpi->oxcf.worst_allowed_q;
2278 cpi->total_byte_count = 0;
2280 cpi->drop_frame = 0;
2281 cpi->drop_count = 0;
2282 cpi->max_drop_count = 0;
2283 cpi->max_consec_dropped_frames = 4;
2285 cpi->rate_correction_factor = 1.0;
2286 cpi->key_frame_rate_correction_factor = 1.0;
2287 cpi->gf_rate_correction_factor = 1.0;
2288 cpi->est_max_qcorrection_factor = 1.0;
2290 cpi->mb.mvcost[0] = &cpi->mb.mvcosts[0][mv_max+1];
2291 cpi->mb.mvcost[1] = &cpi->mb.mvcosts[1][mv_max+1];
2292 cpi->mb.mvsadcost[0] = &cpi->mb.mvsadcosts[0][mv_max+1];
2293 cpi->mb.mvsadcost[1] = &cpi->mb.mvsadcosts[1][mv_max+1];
2295 cal_mvsadcosts(cpi->mb.mvsadcost);
2297 for (i = 0; i < KEY_FRAME_CONTEXT; i++)
2299 cpi->prior_key_frame_size[i] = cpi->intra_frame_target;
2300 cpi->prior_key_frame_distance[i] = (int)cpi->output_frame_rate;
2303 cpi->check_freq[0] = 15;
2304 cpi->check_freq[1] = 15;
2306 #ifdef OUTPUT_YUV_SRC
2307 yuv_file = fopen("bd.yuv", "ab");
2311 framepsnr = fopen("framepsnr.stt", "a");
2312 kf_list = fopen("kf_list.stt", "w");
2315 cpi->output_pkt_list = oxcf->output_pkt_list;
2317 #if !(CONFIG_REALTIME_ONLY)
2321 vp8_init_first_pass(cpi);
2323 else if (cpi->pass == 2)
2325 size_t packet_sz = vp8_firstpass_stats_sz(cpi->common.MBs);
2326 int packets = oxcf->two_pass_stats_in.sz / packet_sz;
2328 cpi->stats_in = oxcf->two_pass_stats_in.buf;
2329 cpi->stats_in_end = (void*)((char *)cpi->stats_in
2330 + (packets - 1) * packet_sz);
2331 vp8_init_second_pass(cpi);
2336 if (cpi->compressor_speed == 2)
2338 cpi->cpu_freq = 0; //vp8_get_processor_freq();
2339 cpi->avg_encode_time = 0;
2340 cpi->avg_pick_mode_time = 0;
2343 vp8_set_speed_features(cpi);
2345 // Set starting values of RD threshold multipliers (128 = *1)
2346 for (i = 0; i < MAX_MODES; i++)
2348 cpi->rd_thresh_mult[i] = 128;
2351 #ifdef ENTROPY_STATS
2352 init_mv_ref_counts();
2355 vp8cx_create_encoder_threads(cpi);
2357 cpi->fn_ptr[BLOCK_16X16].sdf = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x16);
2358 cpi->fn_ptr[BLOCK_16X16].vf = VARIANCE_INVOKE(&cpi->rtcd.variance, var16x16);
2359 cpi->fn_ptr[BLOCK_16X16].svf = VARIANCE_INVOKE(&cpi->rtcd.variance, subpixvar16x16);
2360 cpi->fn_ptr[BLOCK_16X16].svf_halfpix_h = VARIANCE_INVOKE(&cpi->rtcd.variance, halfpixvar16x16_h);
2361 cpi->fn_ptr[BLOCK_16X16].svf_halfpix_v = VARIANCE_INVOKE(&cpi->rtcd.variance, halfpixvar16x16_v);
2362 cpi->fn_ptr[BLOCK_16X16].svf_halfpix_hv = VARIANCE_INVOKE(&cpi->rtcd.variance, halfpixvar16x16_hv);
2363 cpi->fn_ptr[BLOCK_16X16].sdx3f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x16x3);
2364 cpi->fn_ptr[BLOCK_16X16].sdx8f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x16x8);
2365 cpi->fn_ptr[BLOCK_16X16].sdx4df = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x16x4d);
2367 cpi->fn_ptr[BLOCK_16X8].sdf = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x8);
2368 cpi->fn_ptr[BLOCK_16X8].vf = VARIANCE_INVOKE(&cpi->rtcd.variance, var16x8);
2369 cpi->fn_ptr[BLOCK_16X8].svf = VARIANCE_INVOKE(&cpi->rtcd.variance, subpixvar16x8);
2370 cpi->fn_ptr[BLOCK_16X8].svf_halfpix_h = NULL;
2371 cpi->fn_ptr[BLOCK_16X8].svf_halfpix_v = NULL;
2372 cpi->fn_ptr[BLOCK_16X8].svf_halfpix_hv = NULL;
2373 cpi->fn_ptr[BLOCK_16X8].sdx3f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x8x3);
2374 cpi->fn_ptr[BLOCK_16X8].sdx8f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x8x8);
2375 cpi->fn_ptr[BLOCK_16X8].sdx4df = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x8x4d);
2377 cpi->fn_ptr[BLOCK_8X16].sdf = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x16);
2378 cpi->fn_ptr[BLOCK_8X16].vf = VARIANCE_INVOKE(&cpi->rtcd.variance, var8x16);
2379 cpi->fn_ptr[BLOCK_8X16].svf = VARIANCE_INVOKE(&cpi->rtcd.variance, subpixvar8x16);
2380 cpi->fn_ptr[BLOCK_8X16].svf_halfpix_h = NULL;
2381 cpi->fn_ptr[BLOCK_8X16].svf_halfpix_v = NULL;
2382 cpi->fn_ptr[BLOCK_8X16].svf_halfpix_hv = NULL;
2383 cpi->fn_ptr[BLOCK_8X16].sdx3f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x16x3);
2384 cpi->fn_ptr[BLOCK_8X16].sdx8f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x16x8);
2385 cpi->fn_ptr[BLOCK_8X16].sdx4df = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x16x4d);
2387 cpi->fn_ptr[BLOCK_8X8].sdf = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x8);
2388 cpi->fn_ptr[BLOCK_8X8].vf = VARIANCE_INVOKE(&cpi->rtcd.variance, var8x8);
2389 cpi->fn_ptr[BLOCK_8X8].svf = VARIANCE_INVOKE(&cpi->rtcd.variance, subpixvar8x8);
2390 cpi->fn_ptr[BLOCK_8X8].svf_halfpix_h = NULL;
2391 cpi->fn_ptr[BLOCK_8X8].svf_halfpix_v = NULL;
2392 cpi->fn_ptr[BLOCK_8X8].svf_halfpix_hv = NULL;
2393 cpi->fn_ptr[BLOCK_8X8].sdx3f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x8x3);
2394 cpi->fn_ptr[BLOCK_8X8].sdx8f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x8x8);
2395 cpi->fn_ptr[BLOCK_8X8].sdx4df = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x8x4d);
2397 cpi->fn_ptr[BLOCK_4X4].sdf = VARIANCE_INVOKE(&cpi->rtcd.variance, sad4x4);
2398 cpi->fn_ptr[BLOCK_4X4].vf = VARIANCE_INVOKE(&cpi->rtcd.variance, var4x4);
2399 cpi->fn_ptr[BLOCK_4X4].svf = VARIANCE_INVOKE(&cpi->rtcd.variance, subpixvar4x4);
2400 cpi->fn_ptr[BLOCK_4X4].svf_halfpix_h = NULL;
2401 cpi->fn_ptr[BLOCK_4X4].svf_halfpix_v = NULL;
2402 cpi->fn_ptr[BLOCK_4X4].svf_halfpix_hv = NULL;
2403 cpi->fn_ptr[BLOCK_4X4].sdx3f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad4x4x3);
2404 cpi->fn_ptr[BLOCK_4X4].sdx8f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad4x4x8);
2405 cpi->fn_ptr[BLOCK_4X4].sdx4df = VARIANCE_INVOKE(&cpi->rtcd.variance, sad4x4x4d);
2407 #if !(CONFIG_REALTIME_ONLY)
2408 cpi->full_search_sad = SEARCH_INVOKE(&cpi->rtcd.search, full_search);
2410 cpi->diamond_search_sad = SEARCH_INVOKE(&cpi->rtcd.search, diamond_search);
2412 cpi->ready_for_new_frame = 1;
2414 cpi->source_encode_index = 0;
2416 // make sure frame 1 is okay
2417 cpi->error_bins[0] = cpi->common.MBs;
2419 //vp8cx_init_quantizer() is first called here. Add check in vp8cx_frame_init_quantizer() so that vp8cx_init_quantizer is only called later
2420 //when needed. This will avoid unnecessary calls of vp8cx_init_quantizer() for every frame.
2421 vp8cx_init_quantizer(cpi);
2423 vp8_init_loop_filter(cm);
2424 cm->last_frame_type = KEY_FRAME;
2425 cm->last_filter_type = cm->filter_type;
2426 cm->last_sharpness_level = cm->sharpness_level;
2428 cpi->common.error.setjmp = 0;
2429 return (VP8_PTR) cpi;
2434 void vp8_remove_compressor(VP8_PTR *ptr)
2436 VP8_COMP *cpi = (VP8_COMP *)(*ptr);
2441 if (cpi && (cpi->common.current_video_frame > 0))
2443 #if !(CONFIG_REALTIME_ONLY)
2447 vp8_end_second_pass(cpi);
2452 #ifdef ENTROPY_STATS
2453 print_context_counters();
2454 print_tree_update_probs();
2455 print_mode_context();
2462 FILE *f = fopen("opsnr.stt", "a");
2463 double time_encoded = (cpi->source_end_time_stamp - cpi->first_time_stamp_ever) / 10000000.000;
2464 double total_encode_time = (cpi->time_receive_data + cpi->time_compress_data) / 1000.000;
2465 double dr = (double)cpi->bytes * (double) 8 / (double)1000 / time_encoded;
2467 if (cpi->b_calculate_psnr)
2469 YV12_BUFFER_CONFIG *lst_yv12 = &cpi->common.yv12_fb[cpi->common.lst_fb_idx];
2470 double samples = 3.0 / 2 * cpi->count * lst_yv12->y_width * lst_yv12->y_height;
2471 double total_psnr = vp8_mse2psnr(samples, 255.0, cpi->total_sq_error);
2472 double total_psnr2 = vp8_mse2psnr(samples, 255.0, cpi->total_sq_error2);
2473 double total_ssim = 100 * pow(cpi->summed_quality / cpi->summed_weights, 8.0);
2475 fprintf(f, "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\tVPXSSIM\t Time(us)\n");
2476 fprintf(f, "%7.3f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t%7.3f %8.0f\n",
2477 dr, cpi->total / cpi->count, total_psnr, cpi->totalp / cpi->count, total_psnr2, total_ssim,
2481 if (cpi->b_calculate_ssimg)
2483 fprintf(f, "BitRate\tSSIM_Y\tSSIM_U\tSSIM_V\tSSIM_A\t Time(us)\n");
2484 fprintf(f, "%7.3f\t%6.4f\t%6.4f\t%6.4f\t%6.4f\t%8.0f\n", dr,
2485 cpi->total_ssimg_y / cpi->count, cpi->total_ssimg_u / cpi->count,
2486 cpi->total_ssimg_v / cpi->count, cpi->total_ssimg_all / cpi->count, total_encode_time);
2491 f = fopen("qskip.stt", "a");
2492 fprintf(f, "minq:%d -maxq:%d skipture:skipfalse = %d:%d\n", cpi->oxcf.best_allowed_q, cpi->oxcf.worst_allowed_q, skiptruecount, skipfalsecount);
2503 if (cpi->compressor_speed == 2)
2506 FILE *f = fopen("cxspeed.stt", "a");
2507 cnt_pm /= cpi->common.MBs;
2509 for (i = 0; i < 16; i++)
2510 fprintf(f, "%5d", frames_at_speed[i]);
2513 //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);
2522 extern int count_mb_seg[4];
2523 FILE *f = fopen("modes.stt", "a");
2524 double dr = (double)cpi->oxcf.frame_rate * (double)bytes * (double)8 / (double)count / (double)1000 ;
2525 fprintf(f, "intra_mode in Intra Frames:\n");
2526 fprintf(f, "Y: %8d, %8d, %8d, %8d, %8d\n", y_modes[0], y_modes[1], y_modes[2], y_modes[3], y_modes[4]);
2527 fprintf(f, "UV:%8d, %8d, %8d, %8d\n", uv_modes[0], uv_modes[1], uv_modes[2], uv_modes[3]);
2532 for (i = 0; i < 10; i++)
2533 fprintf(f, "%8d, ", b_modes[i]);
2539 fprintf(f, "Modes in Inter Frames:\n");
2540 fprintf(f, "Y: %8d, %8d, %8d, %8d, %8d, %8d, %8d, %8d, %8d, %8d\n",
2541 inter_y_modes[0], inter_y_modes[1], inter_y_modes[2], inter_y_modes[3], inter_y_modes[4],
2542 inter_y_modes[5], inter_y_modes[6], inter_y_modes[7], inter_y_modes[8], inter_y_modes[9]);
2543 fprintf(f, "UV:%8d, %8d, %8d, %8d\n", inter_uv_modes[0], inter_uv_modes[1], inter_uv_modes[2], inter_uv_modes[3]);
2548 for (i = 0; i < 15; i++)
2549 fprintf(f, "%8d, ", inter_b_modes[i]);
2554 fprintf(f, "P:%8d, %8d, %8d, %8d\n", count_mb_seg[0], count_mb_seg[1], count_mb_seg[2], count_mb_seg[3]);
2555 fprintf(f, "PB:%8d, %8d, %8d, %8d\n", inter_b_modes[LEFT4X4], inter_b_modes[ABOVE4X4], inter_b_modes[ZERO4X4], inter_b_modes[NEW4X4]);
2563 #ifdef ENTROPY_STATS
2566 FILE *fmode = fopen("modecontext.c", "w");
2568 fprintf(fmode, "\n#include \"entropymode.h\"\n\n");
2569 fprintf(fmode, "const unsigned int vp8_kf_default_bmode_counts ");
2570 fprintf(fmode, "[VP8_BINTRAMODES] [VP8_BINTRAMODES] [VP8_BINTRAMODES] =\n{\n");
2572 for (i = 0; i < 10; i++)
2575 fprintf(fmode, " { //Above Mode : %d\n", i);
2577 for (j = 0; j < 10; j++)
2580 fprintf(fmode, " {");
2582 for (k = 0; k < 10; k++)
2584 if (!intra_mode_stats[i][j][k])
2585 fprintf(fmode, " %5d, ", 1);
2587 fprintf(fmode, " %5d, ", intra_mode_stats[i][j][k]);
2590 fprintf(fmode, "}, // left_mode %d\n", j);
2594 fprintf(fmode, " },\n");
2598 fprintf(fmode, "};\n");
2604 #if defined(SECTIONBITS_OUTPUT)
2609 FILE *f = fopen("tokenbits.stt", "a");
2611 for (i = 0; i < 28; i++)
2612 fprintf(f, "%8d", (int)(Sectionbits[i] / 256));
2622 printf("\n_pick_loop_filter_level:%d\n", cpi->time_pick_lpf / 1000);
2623 printf("\n_frames recive_data encod_mb_row compress_frame Total\n");
2624 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);
2630 vp8cx_remove_encoder_threads(cpi);
2632 vp8_dealloc_compressor_data(cpi);
2633 vpx_free(cpi->mb.ss);
2635 vpx_free(cpi->rdtok);
2636 vpx_free(cpi->cyclic_refresh_map);
2638 vp8_remove_common(&cpi->common);
2642 #ifdef OUTPUT_YUV_SRC
2662 static uint64_t calc_plane_error(unsigned char *orig, int orig_stride,
2663 unsigned char *recon, int recon_stride,
2664 unsigned int cols, unsigned int rows,
2665 vp8_variance_rtcd_vtable_t *rtcd)
2667 unsigned int row, col;
2668 uint64_t total_sse = 0;
2671 for (row = 0; row + 16 <= rows; row += 16)
2673 for (col = 0; col + 16 <= cols; col += 16)
2677 VARIANCE_INVOKE(rtcd, mse16x16)(orig + col, orig_stride,
2678 recon + col, recon_stride,
2683 /* Handle odd-sized width */
2686 unsigned int border_row, border_col;
2687 unsigned char *border_orig = orig;
2688 unsigned char *border_recon = recon;
2690 for (border_row = 0; border_row < 16; border_row++)
2692 for (border_col = col; border_col < cols; border_col++)
2694 diff = border_orig[border_col] - border_recon[border_col];
2695 total_sse += diff * diff;
2698 border_orig += orig_stride;
2699 border_recon += recon_stride;
2703 orig += orig_stride * 16;
2704 recon += recon_stride * 16;
2707 /* Handle odd-sized height */
2708 for (; row < rows; row++)
2710 for (col = 0; col < cols; col++)
2712 diff = orig[col] - recon[col];
2713 total_sse += diff * diff;
2716 orig += orig_stride;
2717 recon += recon_stride;
2724 static void generate_psnr_packet(VP8_COMP *cpi)
2726 YV12_BUFFER_CONFIG *orig = cpi->Source;
2727 YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
2728 struct vpx_codec_cx_pkt pkt;
2731 unsigned int width = cpi->common.Width;
2732 unsigned int height = cpi->common.Height;
2734 pkt.kind = VPX_CODEC_PSNR_PKT;
2735 sse = calc_plane_error(orig->y_buffer, orig->y_stride,
2736 recon->y_buffer, recon->y_stride,
2738 IF_RTCD(&cpi->rtcd.variance));
2739 pkt.data.psnr.sse[0] = sse;
2740 pkt.data.psnr.sse[1] = sse;
2741 pkt.data.psnr.samples[0] = width * height;
2742 pkt.data.psnr.samples[1] = width * height;
2744 width = (width + 1) / 2;
2745 height = (height + 1) / 2;
2747 sse = calc_plane_error(orig->u_buffer, orig->uv_stride,
2748 recon->u_buffer, recon->uv_stride,
2750 IF_RTCD(&cpi->rtcd.variance));
2751 pkt.data.psnr.sse[0] += sse;
2752 pkt.data.psnr.sse[2] = sse;
2753 pkt.data.psnr.samples[0] += width * height;
2754 pkt.data.psnr.samples[2] = width * height;
2756 sse = calc_plane_error(orig->v_buffer, orig->uv_stride,
2757 recon->v_buffer, recon->uv_stride,
2759 IF_RTCD(&cpi->rtcd.variance));
2760 pkt.data.psnr.sse[0] += sse;
2761 pkt.data.psnr.sse[3] = sse;
2762 pkt.data.psnr.samples[0] += width * height;
2763 pkt.data.psnr.samples[3] = width * height;
2765 for (i = 0; i < 4; i++)
2766 pkt.data.psnr.psnr[i] = vp8_mse2psnr(pkt.data.psnr.samples[i], 255.0,
2767 pkt.data.psnr.sse[i]);
2769 vpx_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
2773 int vp8_use_as_reference(VP8_PTR ptr, int ref_frame_flags)
2775 VP8_COMP *cpi = (VP8_COMP *)(ptr);
2777 if (ref_frame_flags > 7)
2780 cpi->ref_frame_flags = ref_frame_flags;
2783 int vp8_update_reference(VP8_PTR ptr, int ref_frame_flags)
2785 VP8_COMP *cpi = (VP8_COMP *)(ptr);
2787 if (ref_frame_flags > 7)
2790 cpi->common.refresh_golden_frame = 0;
2791 cpi->common.refresh_alt_ref_frame = 0;
2792 cpi->common.refresh_last_frame = 0;
2794 if (ref_frame_flags & VP8_LAST_FLAG)
2795 cpi->common.refresh_last_frame = 1;
2797 if (ref_frame_flags & VP8_GOLD_FLAG)
2798 cpi->common.refresh_golden_frame = 1;
2800 if (ref_frame_flags & VP8_ALT_FLAG)
2801 cpi->common.refresh_alt_ref_frame = 1;
2806 int vp8_get_reference(VP8_PTR ptr, VP8_REFFRAME ref_frame_flag, YV12_BUFFER_CONFIG *sd)
2808 VP8_COMP *cpi = (VP8_COMP *)(ptr);
2809 VP8_COMMON *cm = &cpi->common;
2812 if (ref_frame_flag == VP8_LAST_FLAG)
2813 ref_fb_idx = cm->lst_fb_idx;
2814 else if (ref_frame_flag == VP8_GOLD_FLAG)
2815 ref_fb_idx = cm->gld_fb_idx;
2816 else if (ref_frame_flag == VP8_ALT_FLAG)
2817 ref_fb_idx = cm->alt_fb_idx;
2821 vp8_yv12_copy_frame_ptr(&cm->yv12_fb[ref_fb_idx], sd);
2825 int vp8_set_reference(VP8_PTR ptr, VP8_REFFRAME ref_frame_flag, YV12_BUFFER_CONFIG *sd)
2827 VP8_COMP *cpi = (VP8_COMP *)(ptr);
2828 VP8_COMMON *cm = &cpi->common;
2832 if (ref_frame_flag == VP8_LAST_FLAG)
2833 ref_fb_idx = cm->lst_fb_idx;
2834 else if (ref_frame_flag == VP8_GOLD_FLAG)
2835 ref_fb_idx = cm->gld_fb_idx;
2836 else if (ref_frame_flag == VP8_ALT_FLAG)
2837 ref_fb_idx = cm->alt_fb_idx;
2841 vp8_yv12_copy_frame_ptr(sd, &cm->yv12_fb[ref_fb_idx]);
2845 int vp8_update_entropy(VP8_PTR comp, int update)
2847 VP8_COMP *cpi = (VP8_COMP *) comp;
2848 VP8_COMMON *cm = &cpi->common;
2849 cm->refresh_entropy_probs = update;
2856 void vp8_write_yuv_frame(const char *name, YV12_BUFFER_CONFIG *s)
2858 FILE *yuv_file = fopen(name, "ab");
2859 unsigned char *src = s->y_buffer;
2860 int h = s->y_height;
2864 fwrite(src, s->y_width, 1, yuv_file);
2874 fwrite(src, s->uv_width, 1, yuv_file);
2875 src += s->uv_stride;
2884 fwrite(src, s->uv_width, 1, yuv_file);
2885 src += s->uv_stride;
2894 static void scale_and_extend_source(YV12_BUFFER_CONFIG *sd, VP8_COMP *cpi)
2896 VP8_COMMON *cm = &cpi->common;
2898 // are we resizing the image
2899 if (cm->horiz_scale != 0 || cm->vert_scale != 0)
2901 #if CONFIG_SPATIAL_RESAMPLING
2902 int UNINITIALIZED_IS_SAFE(hr), UNINITIALIZED_IS_SAFE(hs);
2903 int UNINITIALIZED_IS_SAFE(vr), UNINITIALIZED_IS_SAFE(vs);
2906 if (cm->vert_scale == 3)
2911 Scale2Ratio(cm->horiz_scale, &hr, &hs);
2912 Scale2Ratio(cm->vert_scale, &vr, &vs);
2914 vp8_scale_frame(sd, &cpi->scaled_source, cm->temp_scale_frame.y_buffer,
2915 tmp_height, hs, hr, vs, vr, 0);
2917 cpi->Source = &cpi->scaled_source;
2920 // we may need to copy to a buffer so we can extend the image...
2921 else if (cm->Width != cm->yv12_fb[cm->lst_fb_idx].y_width ||
2922 cm->Height != cm->yv12_fb[cm->lst_fb_idx].y_height)
2924 //vp8_yv12_copy_frame_ptr(sd, &cpi->scaled_source);
2926 #if CONFIG_RUNTIME_CPU_DETECT
2927 if (cm->rtcd.flags & HAS_NEON)
2930 vp8_yv12_copy_src_frame_func_neon(sd, &cpi->scaled_source);
2932 #if CONFIG_RUNTIME_CPU_DETECT
2936 #if !HAVE_ARMV7 || CONFIG_RUNTIME_CPU_DETECT
2938 vp8_yv12_copy_frame_ptr(sd, &cpi->scaled_source);
2942 cpi->Source = &cpi->scaled_source;
2945 vp8_extend_to_multiple_of16(cpi->Source, cm->Width, cm->Height);
2948 static void resize_key_frame(VP8_COMP *cpi)
2950 #if CONFIG_SPATIAL_RESAMPLING
2951 VP8_COMMON *cm = &cpi->common;
2953 // Do we need to apply resampling for one pass cbr.
2954 // In one pass this is more limited than in two pass cbr
2955 // The test and any change is only made one per key frame sequence
2956 if (cpi->oxcf.allow_spatial_resampling && (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER))
2958 int UNINITIALIZED_IS_SAFE(hr), UNINITIALIZED_IS_SAFE(hs);
2959 int UNINITIALIZED_IS_SAFE(vr), UNINITIALIZED_IS_SAFE(vs);
2960 int new_width, new_height;
2962 // If we are below the resample DOWN watermark then scale down a notch.
2963 if (cpi->buffer_level < (cpi->oxcf.resample_down_water_mark * cpi->oxcf.optimal_buffer_level / 100))
2965 cm->horiz_scale = (cm->horiz_scale < ONETWO) ? cm->horiz_scale + 1 : ONETWO;
2966 cm->vert_scale = (cm->vert_scale < ONETWO) ? cm->vert_scale + 1 : ONETWO;
2968 // Should we now start scaling back up
2969 else if (cpi->buffer_level > (cpi->oxcf.resample_up_water_mark * cpi->oxcf.optimal_buffer_level / 100))
2971 cm->horiz_scale = (cm->horiz_scale > NORMAL) ? cm->horiz_scale - 1 : NORMAL;
2972 cm->vert_scale = (cm->vert_scale > NORMAL) ? cm->vert_scale - 1 : NORMAL;
2975 // Get the new hieght and width
2976 Scale2Ratio(cm->horiz_scale, &hr, &hs);
2977 Scale2Ratio(cm->vert_scale, &vr, &vs);
2978 new_width = ((hs - 1) + (cpi->oxcf.Width * hr)) / hs;
2979 new_height = ((vs - 1) + (cpi->oxcf.Height * vr)) / vs;
2981 // If the image size has changed we need to reallocate the buffers
2982 // and resample the source image
2983 if ((cm->Width != new_width) || (cm->Height != new_height))
2985 cm->Width = new_width;
2986 cm->Height = new_height;
2987 vp8_alloc_compressor_data(cpi);
2988 scale_and_extend_source(cpi->un_scaled_source, cpi);
2994 // return of 0 means drop frame
2995 static int pick_frame_size(VP8_COMP *cpi)
2997 VP8_COMMON *cm = &cpi->common;
2999 // First Frame is a special case
3000 if (cm->current_video_frame == 0)
3002 #if !(CONFIG_REALTIME_ONLY)
3005 vp8_calc_auto_iframe_target_size(cpi);
3007 // 1 Pass there is no information on which to base size so use bandwidth per second * fixed fraction
3010 cpi->this_frame_target = cpi->oxcf.target_bandwidth / 2;
3012 // in error resilient mode the first frame is bigger since it likely contains
3013 // all the static background
3014 if (cpi->oxcf.error_resilient_mode == 1 || (cpi->compressor_speed == 2))
3016 cpi->this_frame_target *= 3; // 5;
3019 // Key frame from VFW/auto-keyframe/first frame
3020 cm->frame_type = KEY_FRAME;
3023 // Special case for forced key frames
3024 // The frame sizing here is still far from ideal for 2 pass.
3025 else if (cm->frame_flags & FRAMEFLAGS_KEY)
3027 cm->frame_type = KEY_FRAME;
3028 resize_key_frame(cpi);
3029 vp8_calc_iframe_target_size(cpi);
3031 else if (cm->frame_type == KEY_FRAME)
3033 vp8_calc_auto_iframe_target_size(cpi);
3037 // INTER frame: compute target frame size
3038 cm->frame_type = INTER_FRAME;
3039 vp8_calc_pframe_target_size(cpi);
3041 // Check if we're dropping the frame:
3042 if (cpi->drop_frame)
3044 cpi->drop_frame = FALSE;
3050 // Note target_size in bits * 256 per MB
3051 cpi->target_bits_per_mb = (cpi->this_frame_target * 256) / cpi->common.MBs;
3055 static void set_quantizer(VP8_COMP *cpi, int Q)
3057 VP8_COMMON *cm = &cpi->common;
3058 MACROBLOCKD *mbd = &cpi->mb.e_mbd;
3060 cm->base_qindex = Q;
3062 cm->y1dc_delta_q = 0;
3063 cm->y2dc_delta_q = 0;
3064 cm->y2ac_delta_q = 0;
3065 cm->uvdc_delta_q = 0;
3066 cm->uvac_delta_q = 0;
3068 // Set Segment specific quatizers
3069 mbd->segment_feature_data[MB_LVL_ALT_Q][0] = cpi->segment_feature_data[MB_LVL_ALT_Q][0];
3070 mbd->segment_feature_data[MB_LVL_ALT_Q][1] = cpi->segment_feature_data[MB_LVL_ALT_Q][1];
3071 mbd->segment_feature_data[MB_LVL_ALT_Q][2] = cpi->segment_feature_data[MB_LVL_ALT_Q][2];
3072 mbd->segment_feature_data[MB_LVL_ALT_Q][3] = cpi->segment_feature_data[MB_LVL_ALT_Q][3];
3075 static void update_alt_ref_frame_and_stats(VP8_COMP *cpi)
3077 VP8_COMMON *cm = &cpi->common;
3079 // Update the golden frame buffer
3080 vp8_yv12_copy_frame_ptr(cm->frame_to_show, &cm->yv12_fb[cm->alt_fb_idx]);
3082 // Select an interval before next GF or altref
3083 if (!cpi->auto_gold)
3084 cpi->frames_till_gf_update_due = cpi->goldfreq;
3086 if ((cpi->pass != 2) && cpi->frames_till_gf_update_due)
3088 cpi->current_gf_interval = cpi->frames_till_gf_update_due;
3090 // Set the bits per frame that we should try and recover in subsequent inter frames
3091 // to account for the extra GF spend... note that his does not apply for GF updates
3092 // that occur coincident with a key frame as the extra cost of key frames is dealt
3095 cpi->gf_overspend_bits += cpi->projected_frame_size;
3096 cpi->non_gf_bitrate_adjustment = cpi->gf_overspend_bits / cpi->frames_till_gf_update_due;
3099 // Update data structure that monitors level of reference to last GF
3100 vpx_memset(cpi->gf_active_flags, 1, (cm->mb_rows * cm->mb_cols));
3101 cpi->gf_active_count = cm->mb_rows * cm->mb_cols;
3102 // this frame refreshes means next frames don't unless specified by user
3104 cpi->common.frames_since_golden = 0;
3106 // Clear the alternate reference update pending flag.
3107 cpi->source_alt_ref_pending = FALSE;
3109 // Set the alternate refernce frame active flag
3110 cpi->source_alt_ref_active = TRUE;
3114 static void update_golden_frame_and_stats(VP8_COMP *cpi)
3116 VP8_COMMON *cm = &cpi->common;
3118 // Update the Golden frame reconstruction buffer if signalled and the GF usage counts.
3119 if (cm->refresh_golden_frame)
3121 // Update the golden frame buffer
3122 vp8_yv12_copy_frame_ptr(cm->frame_to_show, &cm->yv12_fb[cm->gld_fb_idx]);
3124 // Select an interval before next GF
3125 if (!cpi->auto_gold)
3126 cpi->frames_till_gf_update_due = cpi->goldfreq;
3128 if ((cpi->pass != 2) && (cpi->frames_till_gf_update_due > 0))
3130 cpi->current_gf_interval = cpi->frames_till_gf_update_due;
3132 // Set the bits per frame that we should try and recover in subsequent inter frames
3133 // to account for the extra GF spend... note that his does not apply for GF updates
3134 // that occur coincident with a key frame as the extra cost of key frames is dealt
3136 if ((cm->frame_type != KEY_FRAME) && !cpi->source_alt_ref_active)
3138 // Calcluate GF bits to be recovered
3139 // Projected size - av frame bits available for inter frames for clip as a whole
3140 cpi->gf_overspend_bits += (cpi->projected_frame_size - cpi->inter_frame_target);
3143 cpi->non_gf_bitrate_adjustment = cpi->gf_overspend_bits / cpi->frames_till_gf_update_due;
3147 // Update data structure that monitors level of reference to last GF
3148 vpx_memset(cpi->gf_active_flags, 1, (cm->mb_rows * cm->mb_cols));
3149 cpi->gf_active_count = cm->mb_rows * cm->mb_cols;
3151 // this frame refreshes means next frames don't unless specified by user
3152 cm->refresh_golden_frame = 0;
3153 cpi->common.frames_since_golden = 0;
3155 //if ( cm->frame_type == KEY_FRAME )
3157 cpi->recent_ref_frame_usage[INTRA_FRAME] = 1;
3158 cpi->recent_ref_frame_usage[LAST_FRAME] = 1;
3159 cpi->recent_ref_frame_usage[GOLDEN_FRAME] = 1;
3160 cpi->recent_ref_frame_usage[ALTREF_FRAME] = 1;
3164 // // Carry a potrtion of count over to begining of next gf sequence
3165 // cpi->recent_ref_frame_usage[INTRA_FRAME] >>= 5;
3166 // cpi->recent_ref_frame_usage[LAST_FRAME] >>= 5;
3167 // cpi->recent_ref_frame_usage[GOLDEN_FRAME] >>= 5;
3168 // cpi->recent_ref_frame_usage[ALTREF_FRAME] >>= 5;
3171 // ******** Fixed Q test code only ************
3172 // If we are going to use the ALT reference for the next group of frames set a flag to say so.
3173 if (cpi->oxcf.fixed_q >= 0 &&
3174 cpi->oxcf.play_alternate && !cpi->common.refresh_alt_ref_frame)
3176 cpi->source_alt_ref_pending = TRUE;
3177 cpi->frames_till_gf_update_due = cpi->baseline_gf_interval;
3180 if (!cpi->source_alt_ref_pending)
3181 cpi->source_alt_ref_active = FALSE;
3183 // Decrement count down till next gf
3184 if (cpi->frames_till_gf_update_due > 0)
3185 cpi->frames_till_gf_update_due--;
3188 else if (!cpi->common.refresh_alt_ref_frame)
3190 // Decrement count down till next gf
3191 if (cpi->frames_till_gf_update_due > 0)
3192 cpi->frames_till_gf_update_due--;
3194 if (cpi->common.frames_till_alt_ref_frame)
3195 cpi->common.frames_till_alt_ref_frame --;
3197 cpi->common.frames_since_golden ++;
3199 if (cpi->common.frames_since_golden > 1)
3201 cpi->recent_ref_frame_usage[INTRA_FRAME] += cpi->count_mb_ref_frame_usage[INTRA_FRAME];
3202 cpi->recent_ref_frame_usage[LAST_FRAME] += cpi->count_mb_ref_frame_usage[LAST_FRAME];
3203 cpi->recent_ref_frame_usage[GOLDEN_FRAME] += cpi->count_mb_ref_frame_usage[GOLDEN_FRAME];
3204 cpi->recent_ref_frame_usage[ALTREF_FRAME] += cpi->count_mb_ref_frame_usage[ALTREF_FRAME];
3209 // This function updates the reference frame probability estimates that
3210 // will be used during mode selection
3211 static void update_rd_ref_frame_probs(VP8_COMP *cpi)
3213 VP8_COMMON *cm = &cpi->common;
3216 const int *const rfct = cpi->recent_ref_frame_usage;
3217 const int rf_intra = rfct[INTRA_FRAME];
3218 const int rf_inter = rfct[LAST_FRAME] + rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME];
3220 if (cm->frame_type == KEY_FRAME)
3222 cpi->prob_intra_coded = 255;
3223 cpi->prob_last_coded = 128;
3224 cpi->prob_gf_coded = 128;
3226 else if (!(rf_intra + rf_inter))
3228 // This is a trap in case this function is called with cpi->recent_ref_frame_usage[] blank.
3229 cpi->prob_intra_coded = 63;
3230 cpi->prob_last_coded = 128;
3231 cpi->prob_gf_coded = 128;
3235 cpi->prob_intra_coded = (rf_intra * 255) / (rf_intra + rf_inter);
3237 if (cpi->prob_intra_coded < 1)
3238 cpi->prob_intra_coded = 1;
3240 if ((cm->frames_since_golden > 0) || cpi->source_alt_ref_active)
3242 cpi->prob_last_coded = rf_inter ? (rfct[LAST_FRAME] * 255) / rf_inter : 128;
3244 if (cpi->prob_last_coded < 1)
3245 cpi->prob_last_coded = 1;
3247 cpi->prob_gf_coded = (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME])
3248 ? (rfct[GOLDEN_FRAME] * 255) / (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME]) : 128;
3250 if (cpi->prob_gf_coded < 1)
3251 cpi->prob_gf_coded = 1;
3256 const int *const rfct = cpi->count_mb_ref_frame_usage;
3257 const int rf_intra = rfct[INTRA_FRAME];
3258 const int rf_inter = rfct[LAST_FRAME] + rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME];
3260 if (cm->frame_type == KEY_FRAME)
3262 cpi->prob_intra_coded = 255;
3263 cpi->prob_last_coded = 128;
3264 cpi->prob_gf_coded = 128;
3266 else if (!(rf_intra + rf_inter))
3268 // This is a trap in case this function is called with cpi->recent_ref_frame_usage[] blank.
3269 cpi->prob_intra_coded = 63;
3270 cpi->prob_last_coded = 128;
3271 cpi->prob_gf_coded = 128;
3275 cpi->prob_intra_coded = (rf_intra * 255) / (rf_intra + rf_inter);
3277 if (cpi->prob_intra_coded < 1)
3278 cpi->prob_intra_coded = 1;
3280 cpi->prob_last_coded = rf_inter ? (rfct[LAST_FRAME] * 255) / rf_inter : 128;
3282 if (cpi->prob_last_coded < 1)
3283 cpi->prob_last_coded = 1;
3285 cpi->prob_gf_coded = (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME])
3286 ? (rfct[GOLDEN_FRAME] * 255) / (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME]) : 128;
3288 if (cpi->prob_gf_coded < 1)
3289 cpi->prob_gf_coded = 1;
3292 // update reference frame costs since we can do better than what we got last frame.
3294 if (cpi->common.refresh_alt_ref_frame)
3296 cpi->prob_intra_coded += 40;
3297 cpi->prob_last_coded = 200;
3298 cpi->prob_gf_coded = 1;
3300 else if (cpi->common.frames_since_golden == 0)
3302 cpi->prob_last_coded = 214;
3303 cpi->prob_gf_coded = 1;
3305 else if (cpi->common.frames_since_golden == 1)
3307 cpi->prob_last_coded = 192;
3308 cpi->prob_gf_coded = 220;
3310 else if (cpi->source_alt_ref_active)
3312 //int dist = cpi->common.frames_till_alt_ref_frame + cpi->common.frames_since_golden;
3313 cpi->prob_gf_coded -= 20;
3315 if (cpi->prob_gf_coded < 10)
3316 cpi->prob_gf_coded = 10;
3323 // 1 = key, 0 = inter
3324 static int decide_key_frame(VP8_COMP *cpi)
3326 VP8_COMMON *cm = &cpi->common;
3328 int code_key_frame = FALSE;
3332 if (cpi->Speed > 11)
3335 // Clear down mmx registers
3336 vp8_clear_system_state(); //__asm emms;
3338 if ((cpi->compressor_speed == 2) && (cpi->Speed >= 5) && (cpi->sf.RD == 0))
3340 double change = 1.0 * abs((int)(cpi->intra_error - cpi->last_intra_error)) / (1 + cpi->last_intra_error);
3341 double change2 = 1.0 * abs((int)(cpi->prediction_error - cpi->last_prediction_error)) / (1 + cpi->last_prediction_error);
3342 double minerror = cm->MBs * 256;
3346 if (10 * cpi->intra_error / (1 + cpi->prediction_error) < 15
3347 && cpi->prediction_error > minerror
3348 && (change > .25 || change2 > .25))
3350 FILE *f = fopen("intra_inter.stt", "a");
3352 if (cpi->prediction_error <= 0)
3353 cpi->prediction_error = 1;
3355 fprintf(f, "%d %d %d %d %14.4f\n",
3356 cm->current_video_frame,
3357 (int) cpi->prediction_error,
3358 (int) cpi->intra_error,
3359 (int)((10 * cpi->intra_error) / cpi->prediction_error),
3367 cpi->last_intra_error = cpi->intra_error;
3368 cpi->last_prediction_error = cpi->prediction_error;
3370 if (10 * cpi->intra_error / (1 + cpi->prediction_error) < 15
3371 && cpi->prediction_error > minerror
3372 && (change > .25 || change2 > .25))
3374 /*(change > 1.4 || change < .75)&& cpi->this_frame_percent_intra > cpi->last_frame_percent_intra + 3*/
3382 // If the following are true we might as well code a key frame
3383 if (((cpi->this_frame_percent_intra == 100) &&
3384 (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra + 2))) ||
3385 ((cpi->this_frame_percent_intra > 95) &&
3386 (cpi->this_frame_percent_intra >= (cpi->last_frame_percent_intra + 5))))
3388 code_key_frame = TRUE;
3390 // in addition if the following are true and this is not a golden frame then code a key frame
3391 // Note that on golden frames there often seems to be a pop in intra useage anyway hence this
3392 // restriction is designed to prevent spurious key frames. The Intra pop needs to be investigated.
3393 else if (((cpi->this_frame_percent_intra > 60) &&
3394 (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra * 2))) ||
3395 ((cpi->this_frame_percent_intra > 75) &&
3396 (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra * 3 / 2))) ||
3397 ((cpi->this_frame_percent_intra > 90) &&
3398 (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra + 10))))
3400 if (!cm->refresh_golden_frame)
3401 code_key_frame = TRUE;
3404 return code_key_frame;
3408 #if !(CONFIG_REALTIME_ONLY)
3409 static void Pass1Encode(VP8_COMP *cpi, unsigned long *size, unsigned char *dest, unsigned int *frame_flags)
3414 set_quantizer(cpi, 26);
3416 scale_and_extend_source(cpi->un_scaled_source, cpi);
3417 vp8_first_pass(cpi);
3422 void write_cx_frame_to_file(YV12_BUFFER_CONFIG *frame, int this_frame)
3430 sprintf(filename, "cx\\y%04d.raw", this_frame);
3431 yframe = fopen(filename, "wb");
3433 for (i = 0; i < frame->y_height; i++)
3434 fwrite(frame->y_buffer + i * frame->y_stride, frame->y_width, 1, yframe);
3437 sprintf(filename, "cx\\u%04d.raw", this_frame);
3438 yframe = fopen(filename, "wb");
3440 for (i = 0; i < frame->uv_height; i++)
3441 fwrite(frame->u_buffer + i * frame->uv_stride, frame->uv_width, 1, yframe);
3444 sprintf(filename, "cx\\v%04d.raw", this_frame);
3445 yframe = fopen(filename, "wb");
3447 for (i = 0; i < frame->uv_height; i++)
3448 fwrite(frame->v_buffer + i * frame->uv_stride, frame->uv_width, 1, yframe);
3453 // return of 0 means drop frame
3455 // Function to test for conditions that indeicate we should loop
3456 // back and recode a frame.
3457 static BOOL recode_loop_test( VP8_COMP *cpi,
3458 int high_limit, int low_limit,
3459 int q, int maxq, int minq )
3461 BOOL force_recode = FALSE;
3462 VP8_COMMON *cm = &cpi->common;
3464 // Is frame recode allowed at all
3465 // Yes if either recode mode 1 is selected or mode two is selcted
3466 // and the frame is a key frame. golden frame or alt_ref_frame
3467 if ( (cpi->sf.recode_loop == 1) ||
3468 ( (cpi->sf.recode_loop == 2) &&
3469 ( (cm->frame_type == KEY_FRAME) ||
3470 cm->refresh_golden_frame ||
3471 cm->refresh_alt_ref_frame ) ) )
3473 // General over and under shoot tests
3474 if ( ((cpi->projected_frame_size > high_limit) && (q < maxq)) ||
3475 ((cpi->projected_frame_size < low_limit) && (q > minq)) )
3477 force_recode = TRUE;
3479 // Specific rate control mode related tests
3483 return force_recode;
3486 static void encode_frame_to_data_rate
3489 unsigned long *size,
3490 unsigned char *dest,
3491 unsigned int *frame_flags
3495 int frame_over_shoot_limit;
3496 int frame_under_shoot_limit;
3506 int zbin_oq_low = 0;
3509 VP8_COMMON *cm = &cpi->common;
3510 int active_worst_qchanged = FALSE;
3512 int overshoot_seen = FALSE;
3513 int undershoot_seen = FALSE;
3514 int drop_mark = cpi->oxcf.drop_frames_water_mark * cpi->oxcf.optimal_buffer_level / 100;
3515 int drop_mark75 = drop_mark * 2 / 3;
3516 int drop_mark50 = drop_mark / 4;
3517 int drop_mark25 = drop_mark / 8;
3519 // Clear down mmx registers to allow floating point in what follows
3520 vp8_clear_system_state();
3522 // Test code for segmentation of gf/arf (0,0)
3523 //segmentation_test_function((VP8_PTR) cpi);
3525 // For an alt ref frame in 2 pass we skip the call to the second pass function that sets the target bandwidth
3526 #if !(CONFIG_REALTIME_ONLY)
3530 if (cpi->common.refresh_alt_ref_frame)
3532 cpi->per_frame_bandwidth = cpi->gf_bits; // Per frame bit target for the alt ref frame
3533 cpi->target_bandwidth = cpi->gf_bits * cpi->output_frame_rate; // per second target bitrate
3538 cpi->per_frame_bandwidth = (int)(cpi->target_bandwidth / cpi->output_frame_rate);
3540 // Default turn off buffer to buffer copying
3541 cm->copy_buffer_to_gf = 0;
3542 cm->copy_buffer_to_arf = 0;
3544 // Clear zbin over-quant value and mode boost values.
3545 cpi->zbin_over_quant = 0;
3546 cpi->zbin_mode_boost = 0;
3548 // Enable or disable mode based tweaking of the zbin
3549 // For 2 Pass Only used where GF/ARF prediction quality
3550 // is above a threshold
3551 cpi->zbin_mode_boost = 0;
3552 cpi->zbin_mode_boost_enabled = TRUE;
3555 if ( cpi->gfu_boost <= 400 )
3557 cpi->zbin_mode_boost_enabled = FALSE;
3561 // Current default encoder behaviour for the altref sign bias
3562 if (cpi->source_alt_ref_active)
3563 cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = 1;
3565 cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = 0;
3567 // Check to see if a key frame is signalled
3568 // For two pass with auto key frame enabled cm->frame_type may already be set, but not for one pass.
3569 if ((cm->current_video_frame == 0) ||
3570 (cm->frame_flags & FRAMEFLAGS_KEY) ||
3571 (cpi->oxcf.auto_key && (cpi->frames_since_key % cpi->key_frame_frequency == 0)))
3573 // Key frame from VFW/auto-keyframe/first frame
3574 cm->frame_type = KEY_FRAME;
3577 // Set default state for segment and mode based loop filter update flags
3578 cpi->mb.e_mbd.update_mb_segmentation_map = 0;
3579 cpi->mb.e_mbd.update_mb_segmentation_data = 0;
3580 cpi->mb.e_mbd.mode_ref_lf_delta_update = 0;
3582 // Set various flags etc to special state if it is a key frame
3583 if (cm->frame_type == KEY_FRAME)
3587 // Reset the loop filter deltas and segmentation map
3588 setup_features(cpi);
3590 // If segmentation is enabled force a map update for key frames
3591 if (cpi->mb.e_mbd.segmentation_enabled)
3593 cpi->mb.e_mbd.update_mb_segmentation_map = 1;
3594 cpi->mb.e_mbd.update_mb_segmentation_data = 1;
3597 // The alternate reference frame cannot be active for a key frame
3598 cpi->source_alt_ref_active = FALSE;
3600 // Reset the RD threshold multipliers to default of * 1 (128)
3601 for (i = 0; i < MAX_MODES; i++)
3603 cpi->rd_thresh_mult[i] = 128;
3607 // Test code for segmentation
3608 //if ( (cm->frame_type == KEY_FRAME) || ((cm->current_video_frame % 2) == 0))
3609 //if ( (cm->current_video_frame % 2) == 0 )
3610 // enable_segmentation((VP8_PTR)cpi);
3612 // disable_segmentation((VP8_PTR)cpi);
3615 // Experimental code for lagged compress and one pass
3616 // Initialise one_pass GF frames stats
3617 // Update stats used for GF selection
3618 //if ( cpi->pass == 0 )
3620 cpi->one_pass_frame_index = cm->current_video_frame % MAX_LAG_BUFFERS;
3622 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frames_so_far = 0;
3623 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_intra_error = 0.0;
3624 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_coded_error = 0.0;
3625 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_pcnt_inter = 0.0;
3626 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_pcnt_motion = 0.0;
3627 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvr = 0.0;
3628 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvr_abs = 0.0;
3629 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvc = 0.0;
3630 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvc_abs = 0.0;
3634 update_rd_ref_frame_probs(cpi);
3636 if (cpi->drop_frames_allowed)
3638 // The reset to decimation 0 is only done here for one pass.
3639 // Once it is set two pass leaves decimation on till the next kf.
3640 if ((cpi->buffer_level > drop_mark) && (cpi->decimation_factor > 0))
3641 cpi->decimation_factor --;
3643 if (cpi->buffer_level > drop_mark75 && cpi->decimation_factor > 0)
3644 cpi->decimation_factor = 1;
3646 else if (cpi->buffer_level < drop_mark25 && (cpi->decimation_factor == 2 || cpi->decimation_factor == 3))
3648 cpi->decimation_factor = 3;
3650 else if (cpi->buffer_level < drop_mark50 && (cpi->decimation_factor == 1 || cpi->decimation_factor == 2))
3652 cpi->decimation_factor = 2;
3654 else if (cpi->buffer_level < drop_mark75 && (cpi->decimation_factor == 0 || cpi->decimation_factor == 1))
3656 cpi->decimation_factor = 1;
3659 //vpx_log("Encoder: Decimation Factor: %d \n",cpi->decimation_factor);
3662 // The following decimates the frame rate according to a regular pattern (i.e. to 1/2 or 2/3 frame rate)
3663 // This can be used to help prevent buffer under-run in CBR mode. Alternatively it might be desirable in
3664 // some situations to drop frame rate but throw more bits at each frame.
3666 // Note that dropping a key frame can be problematic if spatial resampling is also active
3667 if (cpi->decimation_factor > 0)
3669 switch (cpi->decimation_factor)
3672 cpi->per_frame_bandwidth = cpi->per_frame_bandwidth * 3 / 2;
3675 cpi->per_frame_bandwidth = cpi->per_frame_bandwidth * 5 / 4;
3678 cpi->per_frame_bandwidth = cpi->per_frame_bandwidth * 5 / 4;
3682 // Note that we should not throw out a key frame (especially when spatial resampling is enabled).
3683 if ((cm->frame_type == KEY_FRAME)) // && cpi->oxcf.allow_spatial_resampling )
3685 cpi->decimation_count = cpi->decimation_factor;
3687 else if (cpi->decimation_count > 0)
3689 cpi->decimation_count --;
3690 cpi->bits_off_target += cpi->av_per_frame_bandwidth;
3691 cm->current_video_frame++;
3692 cpi->frames_since_key++;
3698 cpi->buffer_level = cpi->bits_off_target;
3703 cpi->decimation_count = cpi->decimation_factor;
3706 // Decide how big to make the frame
3707 if (!pick_frame_size(cpi))
3709 cm->current_video_frame++;
3710 cpi->frames_since_key++;
3714 // Reduce active_worst_allowed_q for CBR if our buffer is getting too full.
3715 // This has a knock on effect on active best quality as well.
3716 // For CBR if the buffer reaches its maximum level then we can no longer
3717 // save up bits for later frames so we might as well use them up
3718 // on the current frame.
3719 if ((cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) &&
3720 (cpi->buffer_level >= cpi->oxcf.optimal_buffer_level) && cpi->buffered_mode)
3722 int Adjustment = cpi->active_worst_quality / 4; // Max adjustment is 1/4
3727 int tmp_lvl = cpi->buffer_level;
3729 if (cpi->buffer_level < cpi->oxcf.maximum_buffer_size)
3731 buff_lvl_step = (cpi->oxcf.maximum_buffer_size - cpi->oxcf.optimal_buffer_level) / Adjustment;
3735 Adjustment = (cpi->buffer_level - cpi->oxcf.optimal_buffer_level) / buff_lvl_step;
3736 cpi->active_worst_quality -= Adjustment;
3741 cpi->active_worst_quality -= Adjustment;
3746 // Set an active best quality and if necessary active worst quality
3747 if (cpi->pass == 2 || (cm->current_video_frame > 150))
3754 vp8_clear_system_state();
3756 Q = cpi->active_worst_quality;
3758 if ((cm->frame_type == KEY_FRAME) || cm->refresh_golden_frame || cpi->common.refresh_alt_ref_frame)
3760 if (cm->frame_type != KEY_FRAME)
3762 if (cpi->avg_frame_qindex < cpi->active_worst_quality)
3763 Q = cpi->avg_frame_qindex;
3765 if ( cpi->gfu_boost > 1000 )
3766 cpi->active_best_quality = gf_low_motion_minq[Q];
3767 else if ( cpi->gfu_boost < 400 )
3768 cpi->active_best_quality = gf_high_motion_minq[Q];
3770 cpi->active_best_quality = gf_mid_motion_minq[Q];
3772 /*cpi->active_best_quality = gf_arf_minq[Q];
3773 tmp = (cpi->gfu_boost > 1000) ? 600 : cpi->gfu_boost - 400;
3774 //tmp = (cpi->gfu_boost > 1000) ? 600 :
3775 //(cpi->gfu_boost < 400) ? 0 : cpi->gfu_boost - 400;
3776 tmp = 128 - (tmp >> 4);
3777 cpi->active_best_quality = (cpi->active_best_quality * tmp)>>7;*/
3783 if (cpi->gfu_boost > 600)
3784 cpi->active_best_quality = kf_low_motion_minq[Q];
3786 cpi->active_best_quality = kf_high_motion_minq[Q];
3791 cpi->active_best_quality = inter_minq[Q];
3794 // If CBR and the buffer is as full then it is reasonable to allow higher quality on the frames
3795 // to prevent bits just going to waste.
3796 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
3798 // Note that the use of >= here elliminates the risk of a devide by 0 error in the else if clause
3799 if (cpi->buffer_level >= cpi->oxcf.maximum_buffer_size)
3800 cpi->active_best_quality = cpi->best_quality;
3802 else if (cpi->buffer_level > cpi->oxcf.optimal_buffer_level)
3804 int Fraction = ((cpi->buffer_level - cpi->oxcf.optimal_buffer_level) * 128) / (cpi->oxcf.maximum_buffer_size - cpi->oxcf.optimal_buffer_level);
3805 int min_qadjustment = ((cpi->active_best_quality - cpi->best_quality) * Fraction) / 128;
3807 cpi->active_best_quality -= min_qadjustment;
3813 // Clip the active best and worst quality values to limits
3814 if (cpi->active_worst_quality > cpi->worst_quality)
3815 cpi->active_worst_quality = cpi->worst_quality;
3817 if (cpi->active_best_quality < cpi->best_quality)
3818 cpi->active_best_quality = cpi->best_quality;
3819 else if (cpi->active_best_quality > cpi->active_worst_quality)
3820 cpi->active_best_quality = cpi->active_worst_quality;
3822 // Determine initial Q to try
3823 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
3824 last_zbin_oq = cpi->zbin_over_quant;
3826 // Set highest allowed value for Zbin over quant
3827 if (cm->frame_type == KEY_FRAME)
3828 zbin_oq_high = 0; //ZBIN_OQ_MAX/16
3829 else if (cm->refresh_alt_ref_frame || (cm->refresh_golden_frame && !cpi->source_alt_ref_active))
3832 zbin_oq_high = ZBIN_OQ_MAX;
3834 // Setup background Q adjustment for error resilliant mode
3835 if (cpi->cyclic_refresh_mode_enabled)
3836 cyclic_background_refresh(cpi, Q, 0);
3838 vp8_compute_frame_size_bounds(cpi, &frame_under_shoot_limit, &frame_over_shoot_limit);
3840 // Limit Q range for the adaptive loop.
3841 bottom_index = cpi->active_best_quality;
3842 top_index = cpi->active_worst_quality;
3843 q_low = cpi->active_best_quality;
3844 q_high = cpi->active_worst_quality;
3846 vp8_save_coding_context(cpi);
3851 scale_and_extend_source(cpi->un_scaled_source, cpi);
3852 #if !(CONFIG_REALTIME_ONLY) && CONFIG_POSTPROC
3854 if (cpi->oxcf.noise_sensitivity > 0)
3859 switch (cpi->oxcf.noise_sensitivity)
3882 if (cm->frame_type == KEY_FRAME)
3884 vp8_de_noise(cpi->Source, cpi->Source, l , 1, 0, RTCD(postproc));
3889 vp8_de_noise(cpi->Source, cpi->Source, l , 1, 0, RTCD(postproc));
3891 src = cpi->Source->y_buffer;
3893 if (cpi->Source->y_stride < 0)
3895 src += cpi->Source->y_stride * (cpi->Source->y_height - 1);
3898 //temp_filter(&cpi->ppi,src,src,
3899 // cm->last_frame.y_width * cm->last_frame.y_height,
3900 // cpi->oxcf.noise_sensitivity);
3906 #ifdef OUTPUT_YUV_SRC
3907 vp8_write_yuv_frame(cpi->Source);
3912 vp8_clear_system_state(); //__asm emms;
3915 if(cpi->is_src_frame_alt_ref)
3919 set_quantizer(cpi, Q);
3922 // setup skip prob for costing in mode/mv decision
3923 if (cpi->common.mb_no_coeff_skip)
3925 cpi->prob_skip_false = cpi->base_skip_false_prob[Q];
3927 if (cm->frame_type != KEY_FRAME)
3929 if (cpi->common.refresh_alt_ref_frame)
3931 if (cpi->last_skip_false_probs[2] != 0)
3932 cpi->prob_skip_false = cpi->last_skip_false_probs[2];
3935 if(cpi->last_skip_false_probs[2]!=0 && abs(Q- cpi->last_skip_probs_q[2])<=16 )
3936 cpi->prob_skip_false = cpi->last_skip_false_probs[2];
3937 else if (cpi->last_skip_false_probs[2]!=0)
3938 cpi->prob_skip_false = (cpi->last_skip_false_probs[2] + cpi->prob_skip_false ) / 2;
3941 else if (cpi->common.refresh_golden_frame)
3943 if (cpi->last_skip_false_probs[1] != 0)
3944 cpi->prob_skip_false = cpi->last_skip_false_probs[1];
3947 if(cpi->last_skip_false_probs[1]!=0 && abs(Q- cpi->last_skip_probs_q[1])<=16 )
3948 cpi->prob_skip_false = cpi->last_skip_false_probs[1];
3949 else if (cpi->last_skip_false_probs[1]!=0)
3950 cpi->prob_skip_false = (cpi->last_skip_false_probs[1] + cpi->prob_skip_false ) / 2;
3955 if (cpi->last_skip_false_probs[0] != 0)
3956 cpi->prob_skip_false = cpi->last_skip_false_probs[0];
3959 if(cpi->last_skip_false_probs[0]!=0 && abs(Q- cpi->last_skip_probs_q[0])<=16 )
3960 cpi->prob_skip_false = cpi->last_skip_false_probs[0];
3961 else if(cpi->last_skip_false_probs[0]!=0)
3962 cpi->prob_skip_false = (cpi->last_skip_false_probs[0] + cpi->prob_skip_false ) / 2;
3966 //as this is for cost estimate, let's make sure it does not go extreme eitehr way
3967 if (cpi->prob_skip_false < 5)
3968 cpi->prob_skip_false = 5;
3970 if (cpi->prob_skip_false > 250)
3971 cpi->prob_skip_false = 250;
3973 if (cpi->is_src_frame_alt_ref)
3974 cpi->prob_skip_false = 1;
3983 FILE *f = fopen("skip.stt", "a");
3984 fprintf(f, "%d, %d, %4d ", cpi->common.refresh_golden_frame, cpi->common.refresh_alt_ref_frame, cpi->prob_skip_false);
3992 if (cm->frame_type == KEY_FRAME)
3993 vp8_setup_key_frame(cpi);
3995 // transform / motion compensation build reconstruction frame
3997 vp8_encode_frame(cpi);
3998 cpi->projected_frame_size -= vp8_estimate_entropy_savings(cpi);
3999 cpi->projected_frame_size = (cpi->projected_frame_size > 0) ? cpi->projected_frame_size : 0;
4001 vp8_clear_system_state(); //__asm emms;
4003 // Test to see if the stats generated for this frame indicate that we should have coded a key frame
4004 // (assuming that we didn't)!
4005 if (cpi->pass != 2 && cpi->oxcf.auto_key && cm->frame_type != KEY_FRAME)
4007 if (decide_key_frame(cpi))
4009 vp8_calc_auto_iframe_target_size(cpi);
4011 // Reset all our sizing numbers and recode
4012 cm->frame_type = KEY_FRAME;
4014 // Clear the Alt reference frame active flag when we have a key frame
4015 cpi->source_alt_ref_active = FALSE;
4017 // Reset the loop filter deltas and segmentation map
4018 setup_features(cpi);
4020 // If segmentation is enabled force a map update for key frames
4021 if (cpi->mb.e_mbd.segmentation_enabled)
4023 cpi->mb.e_mbd.update_mb_segmentation_map = 1;
4024 cpi->mb.e_mbd.update_mb_segmentation_data = 1;
4027 vp8_restore_coding_context(cpi);
4029 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4031 vp8_compute_frame_size_bounds(cpi, &frame_under_shoot_limit, &frame_over_shoot_limit);
4033 // Limit Q range for the adaptive loop.
4034 bottom_index = cpi->active_best_quality;
4035 top_index = cpi->active_worst_quality;
4036 q_low = cpi->active_best_quality;
4037 q_high = cpi->active_worst_quality;
4042 resize_key_frame(cpi);
4047 vp8_clear_system_state();
4049 if (frame_over_shoot_limit == 0)
4050 frame_over_shoot_limit = 1;
4052 // Are we are overshooting and up against the limit of active max Q.
4053 if (((cpi->pass != 2) || (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)) &&
4054 (Q == cpi->active_worst_quality) &&
4055 (cpi->active_worst_quality < cpi->worst_quality) &&
4056 (cpi->projected_frame_size > frame_over_shoot_limit))
4058 int over_size_percent = ((cpi->projected_frame_size - frame_over_shoot_limit) * 100) / frame_over_shoot_limit;
4060 // If so is there any scope for relaxing it
4061 while ((cpi->active_worst_quality < cpi->worst_quality) && (over_size_percent > 0))
4063 cpi->active_worst_quality++;
4064 top_index = cpi->active_worst_quality;
4065 over_size_percent = (int)(over_size_percent * 0.96); // Assume 1 qstep = about 4% on frame size.
4068 // If we have updated the active max Q do not call vp8_update_rate_correction_factors() this loop.
4069 active_worst_qchanged = TRUE;
4072 active_worst_qchanged = FALSE;
4074 #if !(CONFIG_REALTIME_ONLY)
4076 // Is the projected frame size out of range and are we allowed to attempt to recode.
4077 if ( recode_loop_test( cpi,
4078 frame_over_shoot_limit, frame_under_shoot_limit,
4079 Q, top_index, bottom_index ) )
4084 // Frame size out of permitted range:
4085 // Update correction factor & compute new Q to try...
4087 // Frame is too large
4088 if (cpi->projected_frame_size > cpi->this_frame_target)
4090 //if ( cpi->zbin_over_quant == 0 )
4091 q_low = (Q < q_high) ? (Q + 1) : q_high; // Raise Qlow as to at least the current value
4093 if (cpi->zbin_over_quant > 0) // If we are using over quant do the same for zbin_oq_low
4094 zbin_oq_low = (cpi->zbin_over_quant < zbin_oq_high) ? (cpi->zbin_over_quant + 1) : zbin_oq_high;
4096 //if ( undershoot_seen || (Q == MAXQ) )
4097 if (undershoot_seen)
4099 // Update rate_correction_factor unless cpi->active_worst_quality has changed.
4100 if (!active_worst_qchanged)
4101 vp8_update_rate_correction_factors(cpi, 1);
4103 Q = (q_high + q_low + 1) / 2;
4105 // Adjust cpi->zbin_over_quant (only allowed when Q is max)
4107 cpi->zbin_over_quant = 0;
4110 zbin_oq_low = (cpi->zbin_over_quant < zbin_oq_high) ? (cpi->zbin_over_quant + 1) : zbin_oq_high;
4111 cpi->zbin_over_quant = (zbin_oq_high + zbin_oq_low) / 2;
4116 // Update rate_correction_factor unless cpi->active_worst_quality has changed.
4117 if (!active_worst_qchanged)
4118 vp8_update_rate_correction_factors(cpi, 0);
4120 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4122 while (((Q < q_low) || (cpi->zbin_over_quant < zbin_oq_low)) && (Retries < 10))
4124 vp8_update_rate_correction_factors(cpi, 0);
4125 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4130 overshoot_seen = TRUE;
4132 // Frame is too small
4135 if (cpi->zbin_over_quant == 0)
4136 q_high = (Q > q_low) ? (Q - 1) : q_low; // Lower q_high if not using over quant
4137 else // else lower zbin_oq_high
4138 zbin_oq_high = (cpi->zbin_over_quant > zbin_oq_low) ? (cpi->zbin_over_quant - 1) : zbin_oq_low;
4142 // Update rate_correction_factor unless cpi->active_worst_quality has changed.
4143 if (!active_worst_qchanged)
4144 vp8_update_rate_correction_factors(cpi, 1);
4146 Q = (q_high + q_low) / 2;
4148 // Adjust cpi->zbin_over_quant (only allowed when Q is max)
4150 cpi->zbin_over_quant = 0;
4152 cpi->zbin_over_quant = (zbin_oq_high + zbin_oq_low) / 2;
4156 // Update rate_correction_factor unless cpi->active_worst_quality has changed.
4157 if (!active_worst_qchanged)
4158 vp8_update_rate_correction_factors(cpi, 0);
4160 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4162 while (((Q > q_high) || (cpi->zbin_over_quant > zbin_oq_high)) && (Retries < 10))
4164 vp8_update_rate_correction_factors(cpi, 0);
4165 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4170 undershoot_seen = TRUE;
4173 // Clamp Q to upper and lower limits:
4179 // Clamp cpi->zbin_over_quant
4180 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;
4182 //Loop = ((Q != last_q) || (last_zbin_oq != cpi->zbin_over_quant)) ? TRUE : FALSE;
4183 Loop = ((Q != last_q)) ? TRUE : FALSE;
4184 last_zbin_oq = cpi->zbin_over_quant;
4190 if (cpi->is_src_frame_alt_ref)
4195 vp8_restore_coding_context(cpi);
4198 cpi->tot_recode_hits++;
4202 while (Loop == TRUE);
4205 // Experimental code for lagged and one pass
4206 // Update stats used for one pass GF selection
4210 double frame_intra_error;
4211 double frame_coded_error;
4212 double frame_pcnt_inter;
4213 double frame_pcnt_motion;
4215 double frame_mvr_abs;
4217 double frame_mvc_abs;
4220 cpi->one_pass_frame_stats[cpi->one_pass_frame_index].frame_coded_error = (double)cpi->prediction_error;
4221 cpi->one_pass_frame_stats[cpi->one_pass_frame_index].frame_intra_error = (double)cpi->intra_error;
4222 cpi->one_pass_frame_stats[cpi->one_pass_frame_index].frame_pcnt_inter = (double)(100 - cpi->this_frame_percent_intra) / 100.0;
4226 // Update the GF useage maps.
4227 // This is done after completing the compression of a frame when all modes etc. are finalized but before loop filter
4228 vp8_update_gf_useage_maps(cpi, cm, &cpi->mb);
4229 ////////////////////////////////
4230 ////////////////////////////////
4231 // This frame's MVs are saved and will be used in next frame's MV prediction.
4232 if(cm->show_frame) //do not save for altref frame
4236 MODE_INFO *tmp = cm->mip; //point to beginning of allocated MODE_INFO arrays.
4237 //static int last_video_frame = 0;
4240 if (cm->current_video_frame == 0) //first frame: set to 0
4242 for (mb_row = 0; mb_row < cm->mb_rows+1; mb_row ++)
4244 for (mb_col = 0; mb_col < cm->mb_cols+1; mb_col ++)
4246 cpi->lfmv[mb_col + mb_row*(cm->mode_info_stride)].as_int = 0;
4247 cpi->lf_ref_frame_sign_bias[mb_col + mb_row*(cm->mode_info_stride)] = 0;
4248 cpi->lf_ref_frame[mb_col + mb_row*(cm->mode_info_stride)] = 0;
4254 if(cm->frame_type != KEY_FRAME)
4256 for (mb_row = 0; mb_row < cm->mb_rows+1; mb_row ++)
4258 for (mb_col = 0; mb_col < cm->mb_cols+1; mb_col ++)
4260 if(tmp->mbmi.ref_frame != INTRA_FRAME)
4261 cpi->lfmv[mb_col + mb_row*(cm->mode_info_stride)].as_int = tmp->mbmi.mv.as_int;
4263 cpi->lf_ref_frame_sign_bias[mb_col + mb_row*(cm->mode_info_stride)] = cm->ref_frame_sign_bias[tmp->mbmi.ref_frame];
4264 cpi->lf_ref_frame[mb_col + mb_row*(cm->mode_info_stride)] = tmp->mbmi.ref_frame;
4265 //printf("[%d, %d] ", cpi->lfmv[mb_col + mb_row*(cm->mode_info_stride-1)].as_mv.row, cpi->lfmv[mb_col + mb_row*(cm->mode_info_stride-1)].as_mv.col);
4270 //last_video_frame = cm->current_video_frame;
4274 //printf("after: %d %d \n", cm->current_video_frame, cm->show_frame );
4280 // Update the GF useage maps.
4281 // This is done after completing the compression of a frame when all modes etc. are finalized but before loop filter
4282 vp8_update_gf_useage_maps(cpi, cm, &cpi->mb);
4284 if (cm->frame_type == KEY_FRAME)
4285 cm->refresh_last_frame = 1;
4289 FILE *f = fopen("gfactive.stt", "a");
4290 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);
4295 // 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
4296 // This is purely an encoder descision at present.
4297 if (!cpi->oxcf.error_resilient_mode && cm->refresh_golden_frame)
4298 cm->copy_buffer_to_arf = 2;
4300 cm->copy_buffer_to_arf = 0;
4302 if (cm->refresh_last_frame)
4304 vp8_swap_yv12_buffer(&cm->yv12_fb[cm->lst_fb_idx], &cm->yv12_fb[cm->new_fb_idx]);
4305 cm->frame_to_show = &cm->yv12_fb[cm->lst_fb_idx];
4308 cm->frame_to_show = &cm->yv12_fb[cm->new_fb_idx];
4312 //#pragma omp parallel sections
4315 //#pragma omp section
4318 struct vpx_usec_timer timer;
4320 vpx_usec_timer_start(&timer);
4322 if (cpi->sf.auto_filter == 0)
4323 vp8cx_pick_filter_level_fast(cpi->Source, cpi);
4325 vp8cx_pick_filter_level(cpi->Source, cpi);
4327 vpx_usec_timer_mark(&timer);
4329 cpi->time_pick_lpf += vpx_usec_timer_elapsed(&timer);
4332 cm->filter_level = 0;
4334 if (cm->filter_level > 0)
4336 vp8cx_set_alt_lf_level(cpi, cm->filter_level);
4337 vp8_loop_filter_frame(cm, &cpi->mb.e_mbd, cm->filter_level);
4338 cm->last_frame_type = cm->frame_type;
4339 cm->last_filter_type = cm->filter_type;
4340 cm->last_sharpness_level = cm->sharpness_level;
4343 vp8_yv12_extend_frame_borders_ptr(cm->frame_to_show);
4345 if (cpi->oxcf.error_resilient_mode == 1)
4347 cm->refresh_entropy_probs = 0;
4351 //#pragma omp section
4353 // build the bitstream
4354 vp8_pack_bitstream(cpi, dest, size);
4359 YV12_BUFFER_CONFIG *lst_yv12 = &cm->yv12_fb[cm->lst_fb_idx];
4360 YV12_BUFFER_CONFIG *new_yv12 = &cm->yv12_fb[cm->new_fb_idx];
4361 YV12_BUFFER_CONFIG *gld_yv12 = &cm->yv12_fb[cm->gld_fb_idx];
4362 YV12_BUFFER_CONFIG *alt_yv12 = &cm->yv12_fb[cm->alt_fb_idx];
4363 // At this point the new frame has been encoded coded.
4364 // If any buffer copy / swaping is signalled it should be done here.
4365 if (cm->frame_type == KEY_FRAME)
4367 vp8_yv12_copy_frame_ptr(cm->frame_to_show, gld_yv12);
4368 vp8_yv12_copy_frame_ptr(cm->frame_to_show, alt_yv12);
4370 else // For non key frames
4372 // Code to copy between reference buffers
4373 if (cm->copy_buffer_to_arf)
4375 if (cm->copy_buffer_to_arf == 1)
4377 if (cm->refresh_last_frame)
4378 // We copy new_frame here because last and new buffers will already have been swapped if cm->refresh_last_frame is set.
4379 vp8_yv12_copy_frame_ptr(new_yv12, alt_yv12);
4381 vp8_yv12_copy_frame_ptr(lst_yv12, alt_yv12);
4383 else if (cm->copy_buffer_to_arf == 2)
4384 vp8_yv12_copy_frame_ptr(gld_yv12, alt_yv12);
4387 if (cm->copy_buffer_to_gf)
4389 if (cm->copy_buffer_to_gf == 1)
4391 if (cm->refresh_last_frame)
4392 // We copy new_frame here because last and new buffers will already have been swapped if cm->refresh_last_frame is set.
4393 vp8_yv12_copy_frame_ptr(new_yv12, gld_yv12);
4395 vp8_yv12_copy_frame_ptr(lst_yv12, gld_yv12);
4397 else if (cm->copy_buffer_to_gf == 2)
4398 vp8_yv12_copy_frame_ptr(alt_yv12, gld_yv12);
4403 // Update rate control heuristics
4404 cpi->total_byte_count += (*size);
4405 cpi->projected_frame_size = (*size) << 3;
4407 if (!active_worst_qchanged)
4408 vp8_update_rate_correction_factors(cpi, 2);
4410 cpi->last_q[cm->frame_type] = cm->base_qindex;
4412 if (cm->frame_type == KEY_FRAME)
4414 vp8_adjust_key_frame_context(cpi);
4417 // Keep a record of ambient average Q.
4418 if (cm->frame_type == KEY_FRAME)
4419 cpi->avg_frame_qindex = cm->base_qindex;
4421 cpi->avg_frame_qindex = (2 + 3 * cpi->avg_frame_qindex + cm->base_qindex) >> 2;
4423 // Keep a record from which we can calculate the average Q excluding GF updates and key frames
4424 if ((cm->frame_type != KEY_FRAME) && !cm->refresh_golden_frame && !cm->refresh_alt_ref_frame)
4428 // Calculate the average Q for normal inter frames (not key or GFU frames)
4429 // This is used as a basis for setting active worst quality.
4430 if (cpi->ni_frames > 150)
4432 cpi->ni_tot_qi += Q;
4433 cpi->ni_av_qi = (cpi->ni_tot_qi / cpi->ni_frames);
4435 // Early in the clip ... average the current frame Q value with the default
4436 // entered by the user as a dampening measure
4439 cpi->ni_tot_qi += Q;
4440 cpi->ni_av_qi = ((cpi->ni_tot_qi / cpi->ni_frames) + cpi->worst_quality + 1) / 2;
4443 // If the average Q is higher than what was used in the last frame
4444 // (after going through the recode loop to keep the frame size within range)
4445 // then use the last frame value - 1.
4446 // The -1 is designed to stop Q and hence the data rate, from progressively
4447 // falling away during difficult sections, but at the same time reduce the number of
4448 // itterations around the recode loop.
4449 if (Q > cpi->ni_av_qi)
4450 cpi->ni_av_qi = Q - 1;
4456 // If the frame was massively oversize and we are below optimal buffer level drop next frame
4457 if ((cpi->drop_frames_allowed) &&
4458 (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) &&
4459 (cpi->buffer_level < cpi->oxcf.drop_frames_water_mark * cpi->oxcf.optimal_buffer_level / 100) &&
4460 (cpi->projected_frame_size > (4 * cpi->this_frame_target)))
4462 cpi->drop_frame = TRUE;
4467 // Set the count for maximum consequative dropped frames based upon the ratio of
4468 // this frame size to the target average per frame bandwidth.
4469 // (cpi->av_per_frame_bandwidth > 0) is just a sanity check to prevent / 0.
4470 if (cpi->drop_frames_allowed && (cpi->av_per_frame_bandwidth > 0))
4472 cpi->max_drop_count = cpi->projected_frame_size / cpi->av_per_frame_bandwidth;
4474 if (cpi->max_drop_count > cpi->max_consec_dropped_frames)
4475 cpi->max_drop_count = cpi->max_consec_dropped_frames;
4478 // Update the buffer level variable.
4479 if (cpi->common.refresh_alt_ref_frame)
4480 cpi->bits_off_target -= cpi->projected_frame_size;
4482 cpi->bits_off_target += cpi->av_per_frame_bandwidth - cpi->projected_frame_size;
4484 // Rolling monitors of whether we are over or underspending used to help regulate min and Max Q in two pass.
4485 cpi->rolling_target_bits = ((cpi->rolling_target_bits * 3) + cpi->this_frame_target + 2) / 4;
4486 cpi->rolling_actual_bits = ((cpi->rolling_actual_bits * 3) + cpi->projected_frame_size + 2) / 4;
4487 cpi->long_rolling_target_bits = ((cpi->long_rolling_target_bits * 31) + cpi->this_frame_target + 16) / 32;
4488 cpi->long_rolling_actual_bits = ((cpi->long_rolling_actual_bits * 31) + cpi->projected_frame_size + 16) / 32;
4490 // Actual bits spent
4491 cpi->total_actual_bits += cpi->projected_frame_size;
4494 cpi->total_target_vs_actual += (cpi->this_frame_target - cpi->projected_frame_size);
4496 cpi->buffer_level = cpi->bits_off_target;
4498 // Update bits left to the kf and gf groups to account for overshoot or undershoot on these frames
4499 if (cm->frame_type == KEY_FRAME)
4501 cpi->kf_group_bits += cpi->this_frame_target - cpi->projected_frame_size;
4503 if (cpi->kf_group_bits < 0)
4504 cpi->kf_group_bits = 0 ;
4506 else if (cm->refresh_golden_frame || cm->refresh_alt_ref_frame)
4508 cpi->gf_group_bits += cpi->this_frame_target - cpi->projected_frame_size;
4510 if (cpi->gf_group_bits < 0)
4511 cpi->gf_group_bits = 0 ;
4514 if (cm->frame_type != KEY_FRAME)
4516 if (cpi->common.refresh_alt_ref_frame)
4518 cpi->last_skip_false_probs[2] = cpi->prob_skip_false;
4519 cpi->last_skip_probs_q[2] = cm->base_qindex;
4521 else if (cpi->common.refresh_golden_frame)
4523 cpi->last_skip_false_probs[1] = cpi->prob_skip_false;
4524 cpi->last_skip_probs_q[1] = cm->base_qindex;
4528 cpi->last_skip_false_probs[0] = cpi->prob_skip_false;
4529 cpi->last_skip_probs_q[0] = cm->base_qindex;
4531 //update the baseline
4532 cpi->base_skip_false_prob[cm->base_qindex] = cpi->prob_skip_false;
4537 #if 0 && CONFIG_PSNR
4539 FILE *f = fopen("tmp.stt", "a");
4541 vp8_clear_system_state(); //__asm emms;
4543 if (cpi->total_coded_error_left != 0.0)
4544 fprintf(f, "%10d %10d %10d %10d %10d %10d %10d %10d %6ld %6ld"
4545 "%6ld %6ld %5ld %5ld %5ld %8ld %8.2f %10d %10.3f"
4547 cpi->common.current_video_frame, cpi->this_frame_target,
4548 cpi->projected_frame_size,
4549 (cpi->projected_frame_size - cpi->this_frame_target),
4550 (int)cpi->total_target_vs_actual,
4551 (cpi->oxcf.starting_buffer_level-cpi->bits_off_target),
4552 (int)cpi->total_actual_bits, cm->base_qindex,
4553 cpi->active_best_quality, cpi->active_worst_quality,
4554 cpi->avg_frame_qindex, cpi->zbin_over_quant,
4555 cm->refresh_golden_frame, cm->refresh_alt_ref_frame,
4556 cm->frame_type, cpi->gfu_boost,
4557 cpi->est_max_qcorrection_factor, (int)cpi->bits_left,
4558 cpi->total_coded_error_left,
4559 (double)cpi->bits_left / cpi->total_coded_error_left,
4560 cpi->tot_recode_hits);
4562 fprintf(f, "%10d %10d %10d %10d %10d %10d %10d %10d %6ld %6ld"
4563 "%6ld %6ld %5ld %5ld %5ld %8ld %8.2f %10d %10.3f"
4565 cpi->common.current_video_frame,
4566 cpi->this_frame_target, cpi->projected_frame_size,
4567 (cpi->projected_frame_size - cpi->this_frame_target),
4568 (int)cpi->total_target_vs_actual,
4569 (cpi->oxcf.starting_buffer_level-cpi->bits_off_target),
4570 (int)cpi->total_actual_bits, cm->base_qindex,
4571 cpi->active_best_quality, cpi->active_worst_quality,
4572 cpi->avg_frame_qindex, cpi->zbin_over_quant,
4573 cm->refresh_golden_frame, cm->refresh_alt_ref_frame,
4574 cm->frame_type, cpi->gfu_boost,
4575 cpi->est_max_qcorrection_factor, (int)cpi->bits_left,
4576 cpi->total_coded_error_left, cpi->tot_recode_hits);
4581 FILE *fmodes = fopen("Modes.stt", "a");
4584 fprintf(fmodes, "%6d:%1d:%1d:%1d ",
4585 cpi->common.current_video_frame,
4586 cm->frame_type, cm->refresh_golden_frame,
4587 cm->refresh_alt_ref_frame);
4589 for (i = 0; i < MAX_MODES; i++)
4590 fprintf(fmodes, "%5d ", cpi->mode_chosen_counts[i]);
4592 fprintf(fmodes, "\n");
4600 // If this was a kf or Gf note the Q
4601 if ((cm->frame_type == KEY_FRAME) || cm->refresh_golden_frame || cm->refresh_alt_ref_frame)
4602 cm->last_kf_gf_q = cm->base_qindex;
4604 if (cm->refresh_golden_frame == 1)
4605 cm->frame_flags = cm->frame_flags | FRAMEFLAGS_GOLDEN;
4607 cm->frame_flags = cm->frame_flags&~FRAMEFLAGS_GOLDEN;
4609 if (cm->refresh_alt_ref_frame == 1)
4610 cm->frame_flags = cm->frame_flags | FRAMEFLAGS_ALTREF;
4612 cm->frame_flags = cm->frame_flags&~FRAMEFLAGS_ALTREF;
4615 if (cm->refresh_last_frame & cm->refresh_golden_frame) // both refreshed
4616 cpi->gold_is_last = 1;
4617 else if (cm->refresh_last_frame ^ cm->refresh_golden_frame) // 1 refreshed but not the other
4618 cpi->gold_is_last = 0;
4620 if (cm->refresh_last_frame & cm->refresh_alt_ref_frame) // both refreshed
4621 cpi->alt_is_last = 1;
4622 else if (cm->refresh_last_frame ^ cm->refresh_alt_ref_frame) // 1 refreshed but not the other
4623 cpi->alt_is_last = 0;
4625 if (cm->refresh_alt_ref_frame & cm->refresh_golden_frame) // both refreshed
4626 cpi->gold_is_alt = 1;
4627 else if (cm->refresh_alt_ref_frame ^ cm->refresh_golden_frame) // 1 refreshed but not the other
4628 cpi->gold_is_alt = 0;
4630 cpi->ref_frame_flags = VP8_ALT_FLAG | VP8_GOLD_FLAG | VP8_LAST_FLAG;
4632 if (cpi->gold_is_last)
4633 cpi->ref_frame_flags &= ~VP8_GOLD_FLAG;
4635 if (cpi->alt_is_last)
4636 cpi->ref_frame_flags &= ~VP8_ALT_FLAG;
4638 if (cpi->gold_is_alt)
4639 cpi->ref_frame_flags &= ~VP8_ALT_FLAG;
4642 if (cpi->oxcf.error_resilient_mode)
4644 // Is this an alternate reference update
4645 if (cpi->common.refresh_alt_ref_frame)
4646 vp8_yv12_copy_frame_ptr(cm->frame_to_show, &cm->yv12_fb[cm->alt_fb_idx]);
4648 if (cpi->common.refresh_golden_frame)
4649 vp8_yv12_copy_frame_ptr(cm->frame_to_show, &cm->yv12_fb[cm->gld_fb_idx]);
4653 if (cpi->oxcf.play_alternate && cpi->common.refresh_alt_ref_frame && (cpi->common.frame_type != KEY_FRAME))
4654 // Update the alternate reference frame and stats as appropriate.
4655 update_alt_ref_frame_and_stats(cpi);
4657 // Update the Golden frame and golden frame and stats as appropriate.
4658 update_golden_frame_and_stats(cpi);
4661 if (cm->frame_type == KEY_FRAME)
4663 // Tell the caller that the frame was coded as a key frame
4664 *frame_flags = cm->frame_flags | FRAMEFLAGS_KEY;
4666 // As this frame is a key frame the next defaults to an inter frame.
4667 cm->frame_type = INTER_FRAME;
4669 cpi->last_frame_percent_intra = 100;
4673 *frame_flags = cm->frame_flags&~FRAMEFLAGS_KEY;
4675 cpi->last_frame_percent_intra = cpi->this_frame_percent_intra;
4678 // Clear the one shot update flags for segmentation map and mode/ref loop filter deltas.
4679 cpi->mb.e_mbd.update_mb_segmentation_map = 0;
4680 cpi->mb.e_mbd.update_mb_segmentation_data = 0;
4681 cpi->mb.e_mbd.mode_ref_lf_delta_update = 0;
4684 // Dont increment frame counters if this was an altref buffer update not a real frame
4687 cm->current_video_frame++;
4688 cpi->frames_since_key++;
4691 // reset to normal state now that we are done.
4699 sprintf(filename, "enc%04d.yuv", (int) cm->current_video_frame);
4700 recon_file = fopen(filename, "wb");
4701 fwrite(cm->yv12_fb[cm->lst_fb_idx].buffer_alloc,
4702 cm->yv12_fb[cm->lst_fb_idx].frame_size, 1, recon_file);
4708 //vp8_write_yuv_frame("encoder_recon.yuv", cm->frame_to_show);
4713 int vp8_is_gf_update_needed(VP8_PTR ptr)
4715 VP8_COMP *cpi = (VP8_COMP *) ptr;
4718 ret_val = cpi->gf_update_recommended;
4719 cpi->gf_update_recommended = 0;
4724 void vp8_check_gf_quality(VP8_COMP *cpi)
4726 VP8_COMMON *cm = &cpi->common;
4727 int gf_active_pct = (100 * cpi->gf_active_count) / (cm->mb_rows * cm->mb_cols);
4728 int gf_ref_usage_pct = (cpi->count_mb_ref_frame_usage[GOLDEN_FRAME] * 100) / (cm->mb_rows * cm->mb_cols);
4729 int last_ref_zz_useage = (cpi->inter_zz_count * 100) / (cm->mb_rows * cm->mb_cols);
4731 // Gf refresh is not currently being signalled
4732 if (cpi->gf_update_recommended == 0)
4734 if (cpi->common.frames_since_golden > 7)
4737 if ((gf_active_pct < 10) || ((gf_active_pct + gf_ref_usage_pct) < 15))
4739 // ...but last frame zero zero usage is reasonbable so a new gf might be appropriate
4740 if (last_ref_zz_useage >= 25)
4742 cpi->gf_bad_count ++;
4744 if (cpi->gf_bad_count >= 8) // Check that the condition is stable
4746 cpi->gf_update_recommended = 1;
4747 cpi->gf_bad_count = 0;
4751 cpi->gf_bad_count = 0; // Restart count as the background is not stable enough
4754 cpi->gf_bad_count = 0; // Gf useage has picked up so reset count
4757 // If the signal is set but has not been read should we cancel it.
4758 else if (last_ref_zz_useage < 15)
4760 cpi->gf_update_recommended = 0;
4761 cpi->gf_bad_count = 0;
4766 FILE *f = fopen("gfneeded.stt", "a");
4767 fprintf(f, "%10d %10d %10d %10d %10ld \n",
4768 cm->current_video_frame,
4769 cpi->common.frames_since_golden,
4770 gf_active_pct, gf_ref_usage_pct,
4771 cpi->gf_update_recommended);
4778 #if !(CONFIG_REALTIME_ONLY)
4779 static void Pass2Encode(VP8_COMP *cpi, unsigned long *size, unsigned char *dest, unsigned int *frame_flags)
4782 if (!cpi->common.refresh_alt_ref_frame)
4783 vp8_second_pass(cpi);
4785 encode_frame_to_data_rate(cpi, size, dest, frame_flags);
4786 cpi->bits_left -= 8 * *size;
4788 if (!cpi->common.refresh_alt_ref_frame)
4790 double two_pass_min_rate = (double)(cpi->oxcf.target_bandwidth
4791 *cpi->oxcf.two_pass_vbrmin_section / 100);
4792 cpi->bits_left += (long long)(two_pass_min_rate / cpi->oxcf.frame_rate);
4797 //For ARM NEON, d8-d15 are callee-saved registers, and need to be saved by us.
4799 extern void vp8_push_neon(INT64 *store);
4800 extern void vp8_pop_neon(INT64 *store);
4802 int vp8_receive_raw_frame(VP8_PTR ptr, unsigned int frame_flags, YV12_BUFFER_CONFIG *sd, INT64 time_stamp, INT64 end_time)
4805 VP8_COMP *cpi = (VP8_COMP *) ptr;
4806 VP8_COMMON *cm = &cpi->common;
4807 struct vpx_usec_timer timer;
4813 #if CONFIG_RUNTIME_CPU_DETECT
4814 if (cm->rtcd.flags & HAS_NEON)
4817 vp8_push_neon(store_reg);
4821 vpx_usec_timer_start(&timer);
4823 // no more room for frames;
4824 if (cpi->source_buffer_count != 0 && cpi->source_buffer_count >= cpi->oxcf.lag_in_frames)
4827 #if CONFIG_RUNTIME_CPU_DETECT
4828 if (cm->rtcd.flags & HAS_NEON)
4831 vp8_pop_neon(store_reg);
4837 //printf("in-cpi->source_buffer_count: %d\n", cpi->source_buffer_count);
4839 cm->clr_type = sd->clrtype;
4841 // make a copy of the frame for use later...
4842 #if !(CONFIG_REALTIME_ONLY)
4844 if (cpi->oxcf.allow_lag)
4846 int which_buffer = cpi->source_encode_index - 1;
4849 if (which_buffer == -1)
4850 which_buffer = cpi->oxcf.lag_in_frames - 1;
4852 if (cpi->source_buffer_count < cpi->oxcf.lag_in_frames - 1)
4853 which_buffer = cpi->source_buffer_count;
4855 s = &cpi->src_buffer[which_buffer];
4857 s->source_time_stamp = time_stamp;
4858 s->source_end_time_stamp = end_time;
4859 s->source_frame_flags = frame_flags;
4860 vp8_yv12_copy_frame_ptr(sd, &s->source_buffer);
4862 cpi->source_buffer_count ++;
4868 s = &cpi->src_buffer[0];
4869 s->source_end_time_stamp = end_time;
4870 s->source_time_stamp = time_stamp;
4871 s->source_frame_flags = frame_flags;
4873 #if CONFIG_RUNTIME_CPU_DETECT
4874 if (cm->rtcd.flags & HAS_NEON)
4877 vp8_yv12_copy_src_frame_func_neon(sd, &s->source_buffer);
4879 #if CONFIG_RUNTIME_CPU_DETECT
4883 #if !HAVE_ARMV7 || CONFIG_RUNTIME_CPU_DETECT
4885 vp8_yv12_copy_frame_ptr(sd, &s->source_buffer);
4888 cpi->source_buffer_count = 1;
4891 vpx_usec_timer_mark(&timer);
4892 cpi->time_receive_data += vpx_usec_timer_elapsed(&timer);
4895 #if CONFIG_RUNTIME_CPU_DETECT
4896 if (cm->rtcd.flags & HAS_NEON)
4899 vp8_pop_neon(store_reg);
4905 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)
4908 VP8_COMP *cpi = (VP8_COMP *) ptr;
4909 VP8_COMMON *cm = &cpi->common;
4910 struct vpx_usec_timer tsctimer;
4911 struct vpx_usec_timer ticktimer;
4912 struct vpx_usec_timer cmptimer;
4918 #if CONFIG_RUNTIME_CPU_DETECT
4919 if (cm->rtcd.flags & HAS_NEON)
4922 vp8_push_neon(store_reg);
4926 vpx_usec_timer_start(&cmptimer);
4929 // flush variable tells us that even though we have less than 10 frames
4930 // in our buffer we need to start producing compressed frames.
4931 // Probably because we are at the end of a file....
4932 if ((cpi->source_buffer_count == cpi->oxcf.lag_in_frames && cpi->oxcf.lag_in_frames > 0)
4933 || (!cpi->oxcf.allow_lag && cpi->source_buffer_count > 0)
4934 || (flush && cpi->source_buffer_count > 0))
4939 s = &cpi->src_buffer[cpi->source_encode_index];
4940 cpi->source_time_stamp = s->source_time_stamp;
4941 cpi->source_end_time_stamp = s->source_end_time_stamp;
4943 #if !(CONFIG_REALTIME_ONLY)
4945 // Should we code an alternate reference frame
4946 if (cpi->oxcf.error_resilient_mode == 0 &&
4947 cpi->oxcf.play_alternate &&
4948 cpi->source_alt_ref_pending &&
4949 (cpi->frames_till_gf_update_due < cpi->source_buffer_count) &&
4950 cpi->oxcf.lag_in_frames != 0)
4952 cpi->last_alt_ref_sei = (cpi->source_encode_index + cpi->frames_till_gf_update_due) % cpi->oxcf.lag_in_frames;
4954 #if VP8_TEMPORAL_ALT_REF
4956 if (cpi->oxcf.arnr_max_frames > 0)
4959 // my attempt at a loop that tests the results of strength filter.
4960 int start_frame = cpi->last_alt_ref_sei - 3;
4962 int i, besti = -1, pastin = cpi->oxcf.arnr_strength;
4966 if (start_frame < 0)
4967 start_frame += cpi->oxcf.lag_in_frames;
4969 besterr = vp8_calc_low_ss_err(&cpi->src_buffer[cpi->last_alt_ref_sei].source_buffer,
4970 &cpi->src_buffer[start_frame].source_buffer, IF_RTCD(&cpi->rtcd.variance));
4972 for (i = 0; i < 7; i++)
4975 cpi->oxcf.arnr_strength = i;
4976 vp8cx_temp_filter_c(cpi);
4978 thiserr = vp8_calc_low_ss_err(&cpi->alt_ref_buffer.source_buffer,
4979 &cpi->src_buffer[start_frame].source_buffer, IF_RTCD(&cpi->rtcd.variance));
4981 if (10 * thiserr < besterr * 8)
4990 cpi->oxcf.arnr_strength = besti;
4991 vp8cx_temp_filter_c(cpi);
4992 s = &cpi->alt_ref_buffer;
4994 // FWG not sure if I need to copy this data for the Alt Ref frame
4995 s->source_time_stamp = cpi->src_buffer[cpi->last_alt_ref_sei].source_time_stamp;
4996 s->source_end_time_stamp = cpi->src_buffer[cpi->last_alt_ref_sei].source_end_time_stamp;
4997 s->source_frame_flags = cpi->src_buffer[cpi->last_alt_ref_sei].source_frame_flags;
5000 s = &cpi->src_buffer[cpi->last_alt_ref_sei];
5003 vp8cx_temp_filter_c(cpi);
5004 s = &cpi->alt_ref_buffer;
5006 // FWG not sure if I need to copy this data for the Alt Ref frame
5007 s->source_time_stamp = cpi->src_buffer[cpi->last_alt_ref_sei].source_time_stamp;
5008 s->source_end_time_stamp = cpi->src_buffer[cpi->last_alt_ref_sei].source_end_time_stamp;
5009 s->source_frame_flags = cpi->src_buffer[cpi->last_alt_ref_sei].source_frame_flags;
5015 s = &cpi->src_buffer[cpi->last_alt_ref_sei];
5017 cm->frames_till_alt_ref_frame = cpi->frames_till_gf_update_due;
5018 cm->refresh_alt_ref_frame = 1;
5019 cm->refresh_golden_frame = 0;
5020 cm->refresh_last_frame = 0;
5022 cpi->source_alt_ref_pending = FALSE; // Clear Pending altf Ref flag.
5023 cpi->is_src_frame_alt_ref = 0;
5024 cpi->is_next_src_alt_ref = 0;
5030 #if !(CONFIG_REALTIME_ONLY)
5032 if (cpi->oxcf.allow_lag)
5034 if (cpi->source_encode_index == cpi->last_alt_ref_sei)
5036 cpi->is_src_frame_alt_ref = 1;
5037 cpi->last_alt_ref_sei = -1;
5040 cpi->is_src_frame_alt_ref = 0;
5042 cpi->source_encode_index = (cpi->source_encode_index + 1) % cpi->oxcf.lag_in_frames;
5044 if(cpi->source_encode_index == cpi->last_alt_ref_sei)
5045 cpi->is_next_src_alt_ref = 1;
5047 cpi->is_next_src_alt_ref = 0;
5051 cpi->source_buffer_count--;
5054 cpi->un_scaled_source = &s->source_buffer;
5055 cpi->Source = &s->source_buffer;
5056 cpi->source_frame_flags = s->source_frame_flags;
5058 *time_stamp = cpi->source_time_stamp;
5059 *time_end = cpi->source_end_time_stamp;
5064 #if !(CONFIG_REALTIME_ONLY)
5066 if (flush && cpi->pass == 1 && !cpi->first_pass_done)
5068 vp8_end_first_pass(cpi); /* get last stats packet */
5069 cpi->first_pass_done = 1;
5075 #if CONFIG_RUNTIME_CPU_DETECT
5076 if (cm->rtcd.flags & HAS_NEON)
5079 vp8_pop_neon(store_reg);
5085 *frame_flags = cpi->source_frame_flags;
5087 if (cpi->source_time_stamp < cpi->first_time_stamp_ever)
5089 cpi->first_time_stamp_ever = cpi->source_time_stamp;
5090 cpi->last_end_time_stamp_seen = cpi->source_time_stamp;
5093 // adjust frame rates based on timestamps given
5094 if (!cm->refresh_alt_ref_frame)
5096 if (cpi->source_time_stamp == cpi->first_time_stamp_ever)
5098 double this_fps = 10000000.000 / (cpi->source_end_time_stamp - cpi->source_time_stamp);
5100 vp8_new_frame_rate(cpi, this_fps);
5104 long long nanosecs = cpi->source_end_time_stamp
5105 - cpi->last_end_time_stamp_seen;
5106 double this_fps = 10000000.000 / nanosecs;
5108 vp8_new_frame_rate(cpi, (7 * cpi->oxcf.frame_rate + this_fps) / 8);
5112 cpi->last_time_stamp_seen = cpi->source_time_stamp;
5113 cpi->last_end_time_stamp_seen = cpi->source_end_time_stamp;
5116 if (cpi->compressor_speed == 2)
5118 vp8_check_gf_quality(cpi);
5124 #if CONFIG_RUNTIME_CPU_DETECT
5125 if (cm->rtcd.flags & HAS_NEON)
5128 vp8_pop_neon(store_reg);
5134 if (cpi->compressor_speed == 2)
5136 vpx_usec_timer_start(&tsctimer);
5137 vpx_usec_timer_start(&ticktimer);
5140 // start with a 0 size frame
5143 // Clear down mmx registers
5144 vp8_clear_system_state(); //__asm emms;
5146 cm->frame_type = INTER_FRAME;
5147 cm->frame_flags = *frame_flags;
5151 if (cm->refresh_alt_ref_frame)
5153 //cm->refresh_golden_frame = 1;
5154 cm->refresh_golden_frame = 0;
5155 cm->refresh_last_frame = 0;
5159 cm->refresh_golden_frame = 0;
5160 cm->refresh_last_frame = 1;
5165 #if !(CONFIG_REALTIME_ONLY)
5169 Pass1Encode(cpi, size, dest, frame_flags);
5171 else if (cpi->pass == 2)
5173 Pass2Encode(cpi, size, dest, frame_flags);
5177 encode_frame_to_data_rate(cpi, size, dest, frame_flags);
5179 if (cpi->compressor_speed == 2)
5181 unsigned int duration, duration2;
5182 vpx_usec_timer_mark(&tsctimer);
5183 vpx_usec_timer_mark(&ticktimer);
5185 duration = vpx_usec_timer_elapsed(&ticktimer);
5186 duration2 = (unsigned int)((double)duration / 2);
5188 if (cm->frame_type != KEY_FRAME)
5190 if (cpi->avg_encode_time == 0)
5191 cpi->avg_encode_time = duration;
5193 cpi->avg_encode_time = (7 * cpi->avg_encode_time + duration) >> 3;
5198 //if(*frame_flags!=1)
5201 if (cpi->avg_pick_mode_time == 0)
5202 cpi->avg_pick_mode_time = duration2;
5204 cpi->avg_pick_mode_time = (7 * cpi->avg_pick_mode_time + duration2) >> 3;
5210 if (cm->refresh_entropy_probs == 0)
5212 vpx_memcpy(&cm->fc, &cm->lfc, sizeof(cm->fc));
5215 // if its a dropped frame honor the requests on subsequent frames
5219 // return to normal state
5220 cm->refresh_entropy_probs = 1;
5221 cm->refresh_alt_ref_frame = 0;
5222 cm->refresh_golden_frame = 0;
5223 cm->refresh_last_frame = 1;
5224 cm->frame_type = INTER_FRAME;
5228 cpi->ready_for_new_frame = 1;
5230 vpx_usec_timer_mark(&cmptimer);
5231 cpi->time_compress_data += vpx_usec_timer_elapsed(&cmptimer);
5233 if (cpi->b_calculate_psnr && cpi->pass != 1 && cm->show_frame)
5234 generate_psnr_packet(cpi);
5240 cpi->bytes += *size;
5247 if (cpi->b_calculate_psnr)
5251 double frame_psnr = vp8_calc_psnr(cpi->Source, cm->frame_to_show, &y, &u, &v, &sq_error);
5256 cpi->total_sq_error += sq_error;
5257 cpi->total += frame_psnr;
5259 double y2, u2, v2, frame_psnr2, frame_ssim2 = 0;
5262 vp8_deblock(cm->frame_to_show, &cm->post_proc_buffer, cm->filter_level * 10 / 6, 1, 0, IF_RTCD(&cm->rtcd.postproc));
5263 vp8_clear_system_state();
5264 frame_psnr2 = vp8_calc_psnr(cpi->Source, &cm->post_proc_buffer, &y2, &u2, &v2, &sq_error);
5265 frame_ssim2 = vp8_calc_ssim(cpi->Source, &cm->post_proc_buffer, 1, &weight);
5267 cpi->summed_quality += frame_ssim2 * weight;
5268 cpi->summed_weights += weight;
5270 cpi->totalp_y += y2;
5271 cpi->totalp_u += u2;
5272 cpi->totalp_v += v2;
5273 cpi->totalp += frame_psnr2;
5274 cpi->total_sq_error2 += sq_error;
5279 if (cpi->b_calculate_ssimg)
5281 double y, u, v, frame_all;
5282 frame_all = vp8_calc_ssimg(cpi->Source, cm->frame_to_show, &y, &u, &v);
5283 cpi->total_ssimg_y += y;
5284 cpi->total_ssimg_u += u;
5285 cpi->total_ssimg_v += v;
5286 cpi->total_ssimg_all += frame_all;
5294 if (cpi->common.frame_type != 0 && cpi->common.base_qindex == cpi->oxcf.worst_allowed_q)
5296 skiptruecount += cpi->skip_true_count;
5297 skipfalsecount += cpi->skip_false_count;
5305 FILE *f = fopen("skip.stt", "a");
5306 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);
5308 if (cpi->is_src_frame_alt_ref == 1)
5309 fprintf(f, "skipcount: %4d framesize: %d\n", cpi->skip_true_count , *size);
5318 #if CONFIG_RUNTIME_CPU_DETECT
5319 if (cm->rtcd.flags & HAS_NEON)
5322 vp8_pop_neon(store_reg);
5329 int vp8_get_preview_raw_frame(VP8_PTR comp, YV12_BUFFER_CONFIG *dest, vp8_ppflags_t *flags)
5331 VP8_COMP *cpi = (VP8_COMP *) comp;
5333 if (cpi->common.refresh_alt_ref_frame)
5339 ret = vp8_post_proc_frame(&cpi->common, dest, flags);
5342 if (cpi->common.frame_to_show)
5344 *dest = *cpi->common.frame_to_show;
5345 dest->y_width = cpi->common.Width;
5346 dest->y_height = cpi->common.Height;
5347 dest->uv_height = cpi->common.Height / 2;
5355 #endif //!CONFIG_POSTPROC
5356 vp8_clear_system_state();
5361 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])
5363 VP8_COMP *cpi = (VP8_COMP *) comp;
5364 signed char feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS];
5366 if (cpi->common.mb_rows != rows || cpi->common.mb_cols != cols)
5371 disable_segmentation((VP8_PTR)cpi);
5375 // Set the segmentation Map
5376 set_segmentation_map((VP8_PTR)cpi, map);
5378 // Activate segmentation.
5379 enable_segmentation((VP8_PTR)cpi);
5381 // Set up the quant segment data
5382 feature_data[MB_LVL_ALT_Q][0] = delta_q[0];
5383 feature_data[MB_LVL_ALT_Q][1] = delta_q[1];
5384 feature_data[MB_LVL_ALT_Q][2] = delta_q[2];
5385 feature_data[MB_LVL_ALT_Q][3] = delta_q[3];
5387 // Set up the loop segment data s
5388 feature_data[MB_LVL_ALT_LF][0] = delta_lf[0];
5389 feature_data[MB_LVL_ALT_LF][1] = delta_lf[1];
5390 feature_data[MB_LVL_ALT_LF][2] = delta_lf[2];
5391 feature_data[MB_LVL_ALT_LF][3] = delta_lf[3];
5393 cpi->segment_encode_breakout[0] = threshold[0];
5394 cpi->segment_encode_breakout[1] = threshold[1];
5395 cpi->segment_encode_breakout[2] = threshold[2];
5396 cpi->segment_encode_breakout[3] = threshold[3];
5398 // Initialise the feature data structure
5399 // SEGMENT_DELTADATA 0, SEGMENT_ABSDATA 1
5400 set_segment_data((VP8_PTR)cpi, &feature_data[0][0], SEGMENT_DELTADATA);
5405 int vp8_set_active_map(VP8_PTR comp, unsigned char *map, unsigned int rows, unsigned int cols)
5407 VP8_COMP *cpi = (VP8_COMP *) comp;
5409 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols)
5413 vpx_memcpy(cpi->active_map, map, rows * cols);
5414 cpi->active_map_enabled = 1;
5417 cpi->active_map_enabled = 0;
5423 //cpi->active_map_enabled = 0;
5428 int vp8_set_internal_size(VP8_PTR comp, VPX_SCALING horiz_mode, VPX_SCALING vert_mode)
5430 VP8_COMP *cpi = (VP8_COMP *) comp;
5432 if (horiz_mode <= ONETWO)
5433 cpi->common.horiz_scale = horiz_mode;
5437 if (vert_mode <= ONETWO)
5438 cpi->common.vert_scale = vert_mode;
5447 int vp8_calc_ss_err(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *dest, const vp8_variance_rtcd_vtable_t *rtcd)
5452 unsigned char *src = source->y_buffer;
5453 unsigned char *dst = dest->y_buffer;
5456 // Loop through the Y plane raw and reconstruction data summing (square differences)
5457 for (i = 0; i < source->y_height; i += 16)
5459 for (j = 0; j < source->y_width; j += 16)
5462 Total += VARIANCE_INVOKE(rtcd, mse16x16)(src + j, source->y_stride, dst + j, dest->y_stride, &sse);
5465 src += 16 * source->y_stride;
5466 dst += 16 * dest->y_stride;
5471 int vp8_calc_low_ss_err(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *dest, const vp8_variance_rtcd_vtable_t *rtcd)
5476 unsigned char *src = source->y_buffer;
5477 unsigned char *dst = dest->y_buffer;
5480 // Loop through the Y plane raw and reconstruction data summing (square differences)
5481 for (i = 0; i < source->y_height; i += 16)
5483 for (j = 0; j < source->y_width; j += 16)
5486 VARIANCE_INVOKE(rtcd, mse16x16)(src + j, source->y_stride, dst + j, dest->y_stride, &sse);
5492 src += 16 * source->y_stride;
5493 dst += 16 * dest->y_stride;
5499 int vp8_get_speed(VP8_PTR c)
5501 VP8_COMP *cpi = (VP8_COMP *) c;
5504 int vp8_get_quantizer(VP8_PTR c)
5506 VP8_COMP *cpi = (VP8_COMP *) c;
5507 return cpi->common.base_qindex;