Removed unused vp8_recon_intra4x4mb function
[libvpx.git] / vp8 / encoder / onyx_if.c
blob3f787d6da37887ec978730fa26e83aa083fea8ed
1 /*
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.
9 */
12 #include "onyxc_int.h"
13 #include "onyx_int.h"
14 #include "systemdependent.h"
15 #include "quantize.h"
16 #include "alloccommon.h"
17 #include "mcomp.h"
18 #include "firstpass.h"
19 #include "psnr.h"
20 #include "vpx_scale/vpxscale.h"
21 #include "extend.h"
22 #include "ratectrl.h"
23 #include "quant_common.h"
24 #include "segmentation.h"
25 #include "g_common.h"
26 #include "vpx_scale/yv12extend.h"
27 #include "postproc.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"
34 #if ARCH_ARM
35 #include "vpx_ports/arm.h"
36 #endif
38 #include <math.h>
39 #include <stdio.h>
40 #include <limits.h>
42 #if CONFIG_RUNTIME_CPU_DETECT
43 #define IF_RTCD(x) (x)
44 #define RTCD(x) &cpi->common.rtcd.x
45 #else
46 #define IF_RTCD(x) NULL
47 #define RTCD(x) NULL
48 #endif
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);
67 #if HAVE_ARMV7
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);
70 #endif
72 int vp8_estimate_entropy_savings(VP8_COMP *cpi);
73 int vp8_calc_ss_err(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *dest, const vp8_variance_rtcd_vtable_t *rtcd);
74 int vp8_calc_low_ss_err(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *dest, const vp8_variance_rtcd_vtable_t *rtcd);
76 extern void vp8_temporal_filter_prepare_c(VP8_COMP *cpi);
78 static void set_default_lf_deltas(VP8_COMP *cpi);
80 extern const int vp8_gf_interval_table[101];
82 #if CONFIG_PSNR
83 #include "math.h"
85 extern double vp8_calc_ssim
87 YV12_BUFFER_CONFIG *source,
88 YV12_BUFFER_CONFIG *dest,
89 int lumamask,
90 double *weight
93 extern double vp8_calc_ssimg
95 YV12_BUFFER_CONFIG *source,
96 YV12_BUFFER_CONFIG *dest,
97 double *ssim_y,
98 double *ssim_u,
99 double *ssim_v
103 #endif
106 #ifdef OUTPUT_YUV_SRC
107 FILE *yuv_file;
108 #endif
110 #if 0
111 FILE *framepsnr;
112 FILE *kf_list;
113 FILE *keyfile;
114 #endif
116 #if 0
117 extern int skip_true_count;
118 extern int skip_false_count;
119 #endif
122 #ifdef ENTROPY_STATS
123 extern int intra_mode_stats[10][10][10];
124 #endif
126 #ifdef SPEEDSTATS
127 unsigned int frames_at_speed[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
128 unsigned int tot_pm = 0;
129 unsigned int cnt_pm = 0;
130 unsigned int tot_ef = 0;
131 unsigned int cnt_ef = 0;
132 #endif
134 #ifdef MODE_STATS
135 extern unsigned __int64 Sectionbits[50];
136 extern int y_modes[5] ;
137 extern int uv_modes[4] ;
138 extern int b_modes[10] ;
140 extern int inter_y_modes[10] ;
141 extern int inter_uv_modes[4] ;
142 extern unsigned int inter_b_modes[15];
143 #endif
145 extern void (*vp8_short_fdct4x4)(short *input, short *output, int pitch);
146 extern void (*vp8_short_fdct8x4)(short *input, short *output, int pitch);
148 extern const int vp8_bits_per_mb[2][QINDEX_RANGE];
150 extern const int qrounding_factors[129];
151 extern const int qzbin_factors[129];
152 extern void vp8cx_init_quantizer(VP8_COMP *cpi);
153 extern const int vp8cx_base_skip_false_prob[128];
155 // Tables relating active max Q to active min Q
156 static const int kf_low_motion_minq[QINDEX_RANGE] =
158 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
159 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
160 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
161 0,0,0,0,1,1,1,1,1,1,1,1,2,2,2,2,
162 3,3,3,3,3,3,4,4,4,5,5,5,5,5,6,6,
163 6,6,7,7,8,8,8,8,9,9,10,10,10,10,11,11,
164 11,11,12,12,13,13,13,13,14,14,15,15,15,15,16,16,
165 16,16,17,17,18,18,18,18,19,20,20,21,21,22,23,23
167 static const int kf_high_motion_minq[QINDEX_RANGE] =
169 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
170 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
171 1,1,1,1,1,1,1,1,2,2,2,2,3,3,3,3,
172 3,3,3,3,4,4,4,4,5,5,5,5,5,5,6,6,
173 6,6,7,7,8,8,8,8,9,9,10,10,10,10,11,11,
174 11,11,12,12,13,13,13,13,14,14,15,15,15,15,16,16,
175 16,16,17,17,18,18,18,18,19,19,20,20,20,20,21,21,
176 21,21,22,22,23,23,24,25,25,26,26,27,28,28,29,30
178 static const int gf_low_motion_minq[QINDEX_RANGE] =
180 0,0,0,0,1,1,1,1,1,1,1,1,2,2,2,2,
181 3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,
182 7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10,
183 11,11,12,12,13,13,14,14,15,15,16,16,17,17,18,18,
184 19,19,20,20,21,21,22,22,23,23,24,24,25,25,26,26,
185 27,27,28,28,29,29,30,30,31,31,32,32,33,33,34,34,
186 35,35,36,36,37,37,38,38,39,39,40,40,41,41,42,42,
187 43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58
189 static const int gf_mid_motion_minq[QINDEX_RANGE] =
191 0,0,0,0,1,1,1,1,1,1,2,2,3,3,3,4,
192 4,4,5,5,5,6,6,6,7,7,7,8,8,8,9,9,
193 9,10,10,10,10,11,11,11,12,12,12,12,13,13,13,14,
194 14,14,15,15,16,16,17,17,18,18,19,19,20,20,21,21,
195 22,22,23,23,24,24,25,25,26,26,27,27,28,28,29,29,
196 30,30,31,31,32,32,33,33,34,34,35,35,36,36,37,37,
197 38,39,39,40,40,41,41,42,42,43,43,44,45,46,47,48,
198 49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64
200 static const int gf_high_motion_minq[QINDEX_RANGE] =
202 0,0,0,0,1,1,1,1,1,2,2,2,3,3,3,4,
203 4,4,5,5,5,6,6,6,7,7,7,8,8,8,9,9,
204 9,10,10,10,11,11,12,12,13,13,14,14,15,15,16,16,
205 17,17,18,18,19,19,20,20,21,21,22,22,23,23,24,24,
206 25,25,26,26,27,27,28,28,29,29,30,30,31,31,32,32,
207 33,33,34,34,35,35,36,36,37,37,38,38,39,39,40,40,
208 41,41,42,42,43,44,45,46,47,48,49,50,51,52,53,54,
209 55,56,57,58,59,60,62,64,66,68,70,72,74,76,78,80
211 static const int inter_minq[QINDEX_RANGE] =
213 0,0,1,1,2,3,3,4,4,5,6,6,7,8,8,9,
214 9,10,11,11,12,13,13,14,15,15,16,17,17,18,19,20,
215 20,21,22,22,23,24,24,25,26,27,27,28,29,30,30,31,
216 32,33,33,34,35,36,36,37,38,39,39,40,41,42,42,43,
217 44,45,46,46,47,48,49,50,50,51,52,53,54,55,55,56,
218 57,58,59,60,60,61,62,63,64,65,66,67,67,68,69,70,
219 71,72,73,74,75,75,76,77,78,79,80,81,82,83,84,85,
220 86,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100
223 void vp8_initialize()
225 static int init_done = 0;
227 if (!init_done)
229 vp8_scale_machine_specific_config();
230 vp8_initialize_common();
231 //vp8_dmachine_specific_config();
232 vp8_tokenize_initialize();
234 vp8cx_init_mv_bits_sadcost();
235 init_done = 1;
238 #ifdef PACKET_TESTING
239 extern FILE *vpxlogc;
240 #endif
242 static void setup_features(VP8_COMP *cpi)
244 // Set up default state for MB feature flags
245 cpi->mb.e_mbd.segmentation_enabled = 0;
246 cpi->mb.e_mbd.update_mb_segmentation_map = 0;
247 cpi->mb.e_mbd.update_mb_segmentation_data = 0;
248 vpx_memset(cpi->mb.e_mbd.mb_segment_tree_probs, 255, sizeof(cpi->mb.e_mbd.mb_segment_tree_probs));
249 vpx_memset(cpi->mb.e_mbd.segment_feature_data, 0, sizeof(cpi->mb.e_mbd.segment_feature_data));
251 cpi->mb.e_mbd.mode_ref_lf_delta_enabled = 0;
252 cpi->mb.e_mbd.mode_ref_lf_delta_update = 0;
253 vpx_memset(cpi->mb.e_mbd.ref_lf_deltas, 0, sizeof(cpi->mb.e_mbd.ref_lf_deltas));
254 vpx_memset(cpi->mb.e_mbd.mode_lf_deltas, 0, sizeof(cpi->mb.e_mbd.mode_lf_deltas));
255 vpx_memset(cpi->mb.e_mbd.last_ref_lf_deltas, 0, sizeof(cpi->mb.e_mbd.ref_lf_deltas));
256 vpx_memset(cpi->mb.e_mbd.last_mode_lf_deltas, 0, sizeof(cpi->mb.e_mbd.mode_lf_deltas));
258 set_default_lf_deltas(cpi);
263 void vp8_dealloc_compressor_data(VP8_COMP *cpi)
265 if(cpi->tplist!=0)
266 vpx_free(cpi->tplist);
267 cpi->tplist = NULL;
269 // Delete last frame MV storage buffers
270 if (cpi->lfmv != 0)
271 vpx_free(cpi->lfmv);
273 cpi->lfmv = 0;
275 if (cpi->lf_ref_frame_sign_bias != 0)
276 vpx_free(cpi->lf_ref_frame_sign_bias);
278 cpi->lf_ref_frame_sign_bias = 0;
280 if (cpi->lf_ref_frame != 0)
281 vpx_free(cpi->lf_ref_frame);
283 cpi->lf_ref_frame = 0;
285 // Delete sementation map
286 if (cpi->segmentation_map != 0)
287 vpx_free(cpi->segmentation_map);
289 cpi->segmentation_map = 0;
291 if (cpi->active_map != 0)
292 vpx_free(cpi->active_map);
294 cpi->active_map = 0;
296 // Delete first pass motion map
297 if (cpi->fp_motion_map != 0)
298 vpx_free(cpi->fp_motion_map);
300 cpi->fp_motion_map = 0;
302 vp8_de_alloc_frame_buffers(&cpi->common);
304 vp8_yv12_de_alloc_frame_buffer(&cpi->last_frame_uf);
305 vp8_yv12_de_alloc_frame_buffer(&cpi->scaled_source);
306 #if VP8_TEMPORAL_ALT_REF
307 vp8_yv12_de_alloc_frame_buffer(&cpi->alt_ref_buffer.source_buffer);
308 #endif
310 int i;
312 for (i = 0; i < MAX_LAG_BUFFERS; i++)
313 vp8_yv12_de_alloc_frame_buffer(&cpi->src_buffer[i].source_buffer);
315 cpi->source_buffer_count = 0;
318 vpx_free(cpi->tok);
319 cpi->tok = 0;
321 // Structure used to monitor GF usage
322 if (cpi->gf_active_flags != 0)
323 vpx_free(cpi->gf_active_flags);
325 cpi->gf_active_flags = 0;
327 if(cpi->mb.pip)
328 vpx_free(cpi->mb.pip);
330 cpi->mb.pip = 0;
332 #if !(CONFIG_REALTIME_ONLY)
333 if(cpi->total_stats)
334 vpx_free(cpi->total_stats);
336 cpi->total_stats = 0;
338 if(cpi->this_frame_stats)
339 vpx_free(cpi->this_frame_stats);
341 cpi->this_frame_stats = 0;
342 #endif
345 static void enable_segmentation(VP8_PTR ptr)
347 VP8_COMP *cpi = (VP8_COMP *)(ptr);
349 // Set the appropriate feature bit
350 cpi->mb.e_mbd.segmentation_enabled = 1;
351 cpi->mb.e_mbd.update_mb_segmentation_map = 1;
352 cpi->mb.e_mbd.update_mb_segmentation_data = 1;
354 static void disable_segmentation(VP8_PTR ptr)
356 VP8_COMP *cpi = (VP8_COMP *)(ptr);
358 // Clear the appropriate feature bit
359 cpi->mb.e_mbd.segmentation_enabled = 0;
362 // Valid values for a segment are 0 to 3
363 // Segmentation map is arrange as [Rows][Columns]
364 static void set_segmentation_map(VP8_PTR ptr, unsigned char *segmentation_map)
366 VP8_COMP *cpi = (VP8_COMP *)(ptr);
368 // Copy in the new segmentation map
369 vpx_memcpy(cpi->segmentation_map, segmentation_map, (cpi->common.mb_rows * cpi->common.mb_cols));
371 // Signal that the map should be updated.
372 cpi->mb.e_mbd.update_mb_segmentation_map = 1;
373 cpi->mb.e_mbd.update_mb_segmentation_data = 1;
376 // The values given for each segment can be either deltas (from the default value chosen for the frame) or absolute values.
378 // Valid range for abs values is (0-127 for MB_LVL_ALT_Q) , (0-63 for SEGMENT_ALT_LF)
379 // Valid range for delta values are (+/-127 for MB_LVL_ALT_Q) , (+/-63 for SEGMENT_ALT_LF)
381 // abs_delta = SEGMENT_DELTADATA (deltas) abs_delta = SEGMENT_ABSDATA (use the absolute values given).
384 static void set_segment_data(VP8_PTR ptr, signed char *feature_data, unsigned char abs_delta)
386 VP8_COMP *cpi = (VP8_COMP *)(ptr);
388 cpi->mb.e_mbd.mb_segement_abs_delta = abs_delta;
389 vpx_memcpy(cpi->segment_feature_data, feature_data, sizeof(cpi->segment_feature_data));
393 static void segmentation_test_function(VP8_PTR ptr)
395 VP8_COMP *cpi = (VP8_COMP *)(ptr);
397 unsigned char *seg_map;
398 signed char feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS];
400 // Create a temporary map for segmentation data.
401 CHECK_MEM_ERROR(seg_map, vpx_calloc(cpi->common.mb_rows * cpi->common.mb_cols, 1));
403 // MB loop to set local segmentation map
404 /*for ( i = 0; i < cpi->common.mb_rows; i++ )
406 for ( j = 0; j < cpi->common.mb_cols; j++ )
408 //seg_map[(i*cpi->common.mb_cols) + j] = (j % 2) + ((i%2)* 2);
409 //if ( j < cpi->common.mb_cols/2 )
411 // Segment 1 around the edge else 0
412 if ( (i == 0) || (j == 0) || (i == (cpi->common.mb_rows-1)) || (j == (cpi->common.mb_cols-1)) )
413 seg_map[(i*cpi->common.mb_cols) + j] = 1;
414 //else if ( (i < 2) || (j < 2) || (i > (cpi->common.mb_rows-3)) || (j > (cpi->common.mb_cols-3)) )
415 // seg_map[(i*cpi->common.mb_cols) + j] = 2;
416 //else if ( (i < 5) || (j < 5) || (i > (cpi->common.mb_rows-6)) || (j > (cpi->common.mb_cols-6)) )
417 // seg_map[(i*cpi->common.mb_cols) + j] = 3;
418 else
419 seg_map[(i*cpi->common.mb_cols) + j] = 0;
423 // Set the segmentation Map
424 set_segmentation_map(ptr, seg_map);
426 // Activate segmentation.
427 enable_segmentation(ptr);
429 // Set up the quant segment data
430 feature_data[MB_LVL_ALT_Q][0] = 0;
431 feature_data[MB_LVL_ALT_Q][1] = 4;
432 feature_data[MB_LVL_ALT_Q][2] = 0;
433 feature_data[MB_LVL_ALT_Q][3] = 0;
434 // Set up the loop segment data
435 feature_data[MB_LVL_ALT_LF][0] = 0;
436 feature_data[MB_LVL_ALT_LF][1] = 0;
437 feature_data[MB_LVL_ALT_LF][2] = 0;
438 feature_data[MB_LVL_ALT_LF][3] = 0;
440 // Initialise the feature data structure
441 // SEGMENT_DELTADATA 0, SEGMENT_ABSDATA 1
442 set_segment_data(ptr, &feature_data[0][0], SEGMENT_DELTADATA);
444 // Delete sementation map
445 if (seg_map != 0)
446 vpx_free(seg_map);
448 seg_map = 0;
452 // A simple function to cyclically refresh the background at a lower Q
453 static void cyclic_background_refresh(VP8_COMP *cpi, int Q, int lf_adjustment)
455 unsigned char *seg_map;
456 signed char feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS];
457 int i;
458 int block_count = cpi->cyclic_refresh_mode_max_mbs_perframe;
459 int mbs_in_frame = cpi->common.mb_rows * cpi->common.mb_cols;
461 // Create a temporary map for segmentation data.
462 CHECK_MEM_ERROR(seg_map, vpx_calloc(cpi->common.mb_rows * cpi->common.mb_cols, 1));
464 cpi->cyclic_refresh_q = Q;
466 for (i = Q; i > 0; i--)
468 if (vp8_bits_per_mb[cpi->common.frame_type][i] >= ((vp8_bits_per_mb[cpi->common.frame_type][Q]*(Q + 128)) / 64))
469 //if ( vp8_bits_per_mb[cpi->common.frame_type][i] >= ((vp8_bits_per_mb[cpi->common.frame_type][Q]*((2*Q)+96))/64) )
471 break;
475 cpi->cyclic_refresh_q = i;
477 // Only update for inter frames
478 if (cpi->common.frame_type != KEY_FRAME)
480 // Cycle through the macro_block rows
481 // MB loop to set local segmentation map
482 for (i = cpi->cyclic_refresh_mode_index; i < mbs_in_frame; i++)
484 // If the MB is as a candidate for clean up then mark it for possible boost/refresh (segment 1)
485 // The segment id may get reset to 0 later if the MB gets coded anything other than last frame 0,0
486 // as only (last frame 0,0) MBs are eligable for refresh : that is to say Mbs likely to be background blocks.
487 if (cpi->cyclic_refresh_map[i] == 0)
489 seg_map[i] = 1;
491 else
493 seg_map[i] = 0;
495 // Skip blocks that have been refreshed recently anyway.
496 if (cpi->cyclic_refresh_map[i] < 0)
497 //cpi->cyclic_refresh_map[i] = cpi->cyclic_refresh_map[i] / 16;
498 cpi->cyclic_refresh_map[i]++;
502 if (block_count > 0)
503 block_count--;
504 else
505 break;
509 // If we have gone through the frame reset to the start
510 cpi->cyclic_refresh_mode_index = i;
512 if (cpi->cyclic_refresh_mode_index >= mbs_in_frame)
513 cpi->cyclic_refresh_mode_index = 0;
516 // Set the segmentation Map
517 set_segmentation_map((VP8_PTR)cpi, seg_map);
519 // Activate segmentation.
520 enable_segmentation((VP8_PTR)cpi);
522 // Set up the quant segment data
523 feature_data[MB_LVL_ALT_Q][0] = 0;
524 feature_data[MB_LVL_ALT_Q][1] = (cpi->cyclic_refresh_q - Q);
525 feature_data[MB_LVL_ALT_Q][2] = 0;
526 feature_data[MB_LVL_ALT_Q][3] = 0;
528 // Set up the loop segment data
529 feature_data[MB_LVL_ALT_LF][0] = 0;
530 feature_data[MB_LVL_ALT_LF][1] = lf_adjustment;
531 feature_data[MB_LVL_ALT_LF][2] = 0;
532 feature_data[MB_LVL_ALT_LF][3] = 0;
534 // Initialise the feature data structure
535 // SEGMENT_DELTADATA 0, SEGMENT_ABSDATA 1
536 set_segment_data((VP8_PTR)cpi, &feature_data[0][0], SEGMENT_DELTADATA);
538 // Delete sementation map
539 if (seg_map != 0)
540 vpx_free(seg_map);
542 seg_map = 0;
546 static void set_default_lf_deltas(VP8_COMP *cpi)
548 cpi->mb.e_mbd.mode_ref_lf_delta_enabled = 1;
549 cpi->mb.e_mbd.mode_ref_lf_delta_update = 1;
551 vpx_memset(cpi->mb.e_mbd.ref_lf_deltas, 0, sizeof(cpi->mb.e_mbd.ref_lf_deltas));
552 vpx_memset(cpi->mb.e_mbd.mode_lf_deltas, 0, sizeof(cpi->mb.e_mbd.mode_lf_deltas));
554 // Test of ref frame deltas
555 cpi->mb.e_mbd.ref_lf_deltas[INTRA_FRAME] = 2;
556 cpi->mb.e_mbd.ref_lf_deltas[LAST_FRAME] = 0;
557 cpi->mb.e_mbd.ref_lf_deltas[GOLDEN_FRAME] = -2;
558 cpi->mb.e_mbd.ref_lf_deltas[ALTREF_FRAME] = -2;
560 cpi->mb.e_mbd.mode_lf_deltas[0] = 4; // BPRED
561 cpi->mb.e_mbd.mode_lf_deltas[1] = -2; // Zero
562 cpi->mb.e_mbd.mode_lf_deltas[2] = 2; // New mv
563 cpi->mb.e_mbd.mode_lf_deltas[3] = 4; // Split mv
566 void vp8_set_speed_features(VP8_COMP *cpi)
568 SPEED_FEATURES *sf = &cpi->sf;
569 int Mode = cpi->compressor_speed;
570 int Speed = cpi->Speed;
571 int i;
572 VP8_COMMON *cm = &cpi->common;
573 int last_improved_quant = sf->improved_quant;
575 // Initialise default mode frequency sampling variables
576 for (i = 0; i < MAX_MODES; i ++)
578 cpi->mode_check_freq[i] = 0;
579 cpi->mode_test_hit_counts[i] = 0;
580 cpi->mode_chosen_counts[i] = 0;
583 cpi->mbs_tested_so_far = 0;
585 // best quality defaults
586 sf->RD = 1;
587 sf->search_method = NSTEP;
588 sf->improved_quant = 1;
589 sf->improved_dct = 1;
590 sf->auto_filter = 1;
591 sf->recode_loop = 1;
592 sf->quarter_pixel_search = 1;
593 sf->half_pixel_search = 1;
594 sf->full_freq[0] = 7;
595 sf->full_freq[1] = 7;
596 sf->min_fs_radius = 8;
597 sf->max_fs_radius = 32;
598 sf->iterative_sub_pixel = 1;
599 sf->optimize_coefficients = 1;
600 sf->use_fastquant_for_pick = 0;
601 sf->no_skip_block4x4_search = 1;
603 sf->first_step = 0;
604 sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
605 sf->improved_mv_pred = 1;
607 cpi->do_full[0] = 0;
608 cpi->do_full[1] = 0;
610 // default thresholds to 0
611 for (i = 0; i < MAX_MODES; i++)
612 sf->thresh_mult[i] = 0;
614 switch (Mode)
616 #if !(CONFIG_REALTIME_ONLY)
617 case 0: // best quality mode
618 sf->thresh_mult[THR_ZEROMV ] = 0;
619 sf->thresh_mult[THR_ZEROG ] = 0;
620 sf->thresh_mult[THR_ZEROA ] = 0;
621 sf->thresh_mult[THR_NEARESTMV] = 0;
622 sf->thresh_mult[THR_NEARESTG ] = 0;
623 sf->thresh_mult[THR_NEARESTA ] = 0;
624 sf->thresh_mult[THR_NEARMV ] = 0;
625 sf->thresh_mult[THR_NEARG ] = 0;
626 sf->thresh_mult[THR_NEARA ] = 0;
628 sf->thresh_mult[THR_DC ] = 0;
630 sf->thresh_mult[THR_V_PRED ] = 1000;
631 sf->thresh_mult[THR_H_PRED ] = 1000;
632 sf->thresh_mult[THR_B_PRED ] = 2000;
633 sf->thresh_mult[THR_TM ] = 1000;
635 sf->thresh_mult[THR_NEWMV ] = 1000;
636 sf->thresh_mult[THR_NEWG ] = 1000;
637 sf->thresh_mult[THR_NEWA ] = 1000;
639 sf->thresh_mult[THR_SPLITMV ] = 2500;
640 sf->thresh_mult[THR_SPLITG ] = 5000;
641 sf->thresh_mult[THR_SPLITA ] = 5000;
643 sf->full_freq[0] = 7;
644 sf->full_freq[1] = 15;
646 sf->first_step = 0;
647 sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
648 break;
649 case 1:
650 case 3:
651 sf->thresh_mult[THR_NEARESTMV] = 0;
652 sf->thresh_mult[THR_ZEROMV ] = 0;
653 sf->thresh_mult[THR_DC ] = 0;
654 sf->thresh_mult[THR_NEARMV ] = 0;
655 sf->thresh_mult[THR_V_PRED ] = 1000;
656 sf->thresh_mult[THR_H_PRED ] = 1000;
657 sf->thresh_mult[THR_B_PRED ] = 2500;
658 sf->thresh_mult[THR_TM ] = 1000;
660 sf->thresh_mult[THR_NEARESTG ] = 1000;
661 sf->thresh_mult[THR_NEARESTA ] = 1000;
663 sf->thresh_mult[THR_ZEROG ] = 1000;
664 sf->thresh_mult[THR_ZEROA ] = 1000;
665 sf->thresh_mult[THR_NEARG ] = 1000;
666 sf->thresh_mult[THR_NEARA ] = 1000;
668 #if 1
669 sf->thresh_mult[THR_ZEROMV ] = 0;
670 sf->thresh_mult[THR_ZEROG ] = 0;
671 sf->thresh_mult[THR_ZEROA ] = 0;
672 sf->thresh_mult[THR_NEARESTMV] = 0;
673 sf->thresh_mult[THR_NEARESTG ] = 0;
674 sf->thresh_mult[THR_NEARESTA ] = 0;
675 sf->thresh_mult[THR_NEARMV ] = 0;
676 sf->thresh_mult[THR_NEARG ] = 0;
677 sf->thresh_mult[THR_NEARA ] = 0;
679 // sf->thresh_mult[THR_DC ] = 0;
681 // sf->thresh_mult[THR_V_PRED ] = 1000;
682 // sf->thresh_mult[THR_H_PRED ] = 1000;
683 // sf->thresh_mult[THR_B_PRED ] = 2000;
684 // sf->thresh_mult[THR_TM ] = 1000;
686 sf->thresh_mult[THR_NEWMV ] = 1000;
687 sf->thresh_mult[THR_NEWG ] = 1000;
688 sf->thresh_mult[THR_NEWA ] = 1000;
690 sf->thresh_mult[THR_SPLITMV ] = 1700;
691 sf->thresh_mult[THR_SPLITG ] = 4500;
692 sf->thresh_mult[THR_SPLITA ] = 4500;
693 #else
694 sf->thresh_mult[THR_NEWMV ] = 1500;
695 sf->thresh_mult[THR_NEWG ] = 1500;
696 sf->thresh_mult[THR_NEWA ] = 1500;
698 sf->thresh_mult[THR_SPLITMV ] = 5000;
699 sf->thresh_mult[THR_SPLITG ] = 10000;
700 sf->thresh_mult[THR_SPLITA ] = 10000;
701 #endif
702 sf->full_freq[0] = 15;
703 sf->full_freq[1] = 31;
705 if (Speed > 0)
707 /* Disable coefficient optimization above speed 0 */
708 sf->optimize_coefficients = 0;
709 sf->use_fastquant_for_pick = 1;
710 sf->no_skip_block4x4_search = 0;
712 sf->first_step = 1;
714 cpi->mode_check_freq[THR_SPLITG] = 2;
715 cpi->mode_check_freq[THR_SPLITA] = 2;
716 cpi->mode_check_freq[THR_SPLITMV] = 0;
719 if (Speed > 1)
721 cpi->mode_check_freq[THR_SPLITG] = 4;
722 cpi->mode_check_freq[THR_SPLITA] = 4;
723 cpi->mode_check_freq[THR_SPLITMV] = 2;
725 sf->thresh_mult[THR_TM ] = 1500;
726 sf->thresh_mult[THR_V_PRED ] = 1500;
727 sf->thresh_mult[THR_H_PRED ] = 1500;
728 sf->thresh_mult[THR_B_PRED ] = 5000;
730 if (cpi->ref_frame_flags & VP8_LAST_FLAG)
732 sf->thresh_mult[THR_NEWMV ] = 2000;
733 sf->thresh_mult[THR_SPLITMV ] = 10000;
736 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
738 sf->thresh_mult[THR_NEARESTG ] = 1500;
739 sf->thresh_mult[THR_ZEROG ] = 1500;
740 sf->thresh_mult[THR_NEARG ] = 1500;
741 sf->thresh_mult[THR_NEWG ] = 2000;
742 sf->thresh_mult[THR_SPLITG ] = 20000;
745 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
747 sf->thresh_mult[THR_NEARESTA ] = 1500;
748 sf->thresh_mult[THR_ZEROA ] = 1500;
749 sf->thresh_mult[THR_NEARA ] = 1500;
750 sf->thresh_mult[THR_NEWA ] = 2000;
751 sf->thresh_mult[THR_SPLITA ] = 20000;
755 if (Speed > 2)
757 cpi->mode_check_freq[THR_SPLITG] = 15;
758 cpi->mode_check_freq[THR_SPLITA] = 15;
759 cpi->mode_check_freq[THR_SPLITMV] = 7;
761 sf->thresh_mult[THR_TM ] = 2000;
762 sf->thresh_mult[THR_V_PRED ] = 2000;
763 sf->thresh_mult[THR_H_PRED ] = 2000;
764 sf->thresh_mult[THR_B_PRED ] = 7500;
766 if (cpi->ref_frame_flags & VP8_LAST_FLAG)
768 sf->thresh_mult[THR_NEWMV ] = 2000;
769 sf->thresh_mult[THR_SPLITMV ] = 25000;
772 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
774 sf->thresh_mult[THR_NEARESTG ] = 2000;
775 sf->thresh_mult[THR_ZEROG ] = 2000;
776 sf->thresh_mult[THR_NEARG ] = 2000;
777 sf->thresh_mult[THR_NEWG ] = 2500;
778 sf->thresh_mult[THR_SPLITG ] = 50000;
781 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
783 sf->thresh_mult[THR_NEARESTA ] = 2000;
784 sf->thresh_mult[THR_ZEROA ] = 2000;
785 sf->thresh_mult[THR_NEARA ] = 2000;
786 sf->thresh_mult[THR_NEWA ] = 2500;
787 sf->thresh_mult[THR_SPLITA ] = 50000;
790 sf->improved_quant = 0;
791 sf->improved_dct = 0;
793 // Only do recode loop on key frames, golden frames and
794 // alt ref frames
795 sf->recode_loop = 2;
797 sf->full_freq[0] = 31;
798 sf->full_freq[1] = 63;
801 if (Speed > 3)
803 sf->thresh_mult[THR_SPLITA ] = INT_MAX;
804 sf->thresh_mult[THR_SPLITG ] = INT_MAX;
805 sf->thresh_mult[THR_SPLITMV ] = INT_MAX;
807 cpi->mode_check_freq[THR_V_PRED] = 0;
808 cpi->mode_check_freq[THR_H_PRED] = 0;
809 cpi->mode_check_freq[THR_B_PRED] = 0;
810 cpi->mode_check_freq[THR_NEARG] = 0;
811 cpi->mode_check_freq[THR_NEWG] = 0;
812 cpi->mode_check_freq[THR_NEARA] = 0;
813 cpi->mode_check_freq[THR_NEWA] = 0;
815 sf->auto_filter = 1;
816 sf->recode_loop = 0; // recode loop off
817 sf->RD = 0; // Turn rd off
819 sf->full_freq[0] = 63;
820 sf->full_freq[1] = 127;
823 if (Speed > 4)
825 sf->auto_filter = 0; // Faster selection of loop filter
826 sf->full_freq[0] = INT_MAX;
827 sf->full_freq[1] = INT_MAX;
829 cpi->mode_check_freq[THR_V_PRED] = 2;
830 cpi->mode_check_freq[THR_H_PRED] = 2;
831 cpi->mode_check_freq[THR_B_PRED] = 2;
833 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
835 cpi->mode_check_freq[THR_NEARG] = 2;
836 cpi->mode_check_freq[THR_NEWG] = 4;
839 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
841 cpi->mode_check_freq[THR_NEARA] = 2;
842 cpi->mode_check_freq[THR_NEWA] = 4;
845 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
847 sf->thresh_mult[THR_NEARESTG ] = 2000;
848 sf->thresh_mult[THR_ZEROG ] = 2000;
849 sf->thresh_mult[THR_NEARG ] = 2000;
850 sf->thresh_mult[THR_NEWG ] = 4000;
853 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
855 sf->thresh_mult[THR_NEARESTA ] = 2000;
856 sf->thresh_mult[THR_ZEROA ] = 2000;
857 sf->thresh_mult[THR_NEARA ] = 2000;
858 sf->thresh_mult[THR_NEWA ] = 4000;
862 break;
863 #endif
864 case 2:
865 sf->optimize_coefficients = 0;
866 sf->recode_loop = 0;
867 sf->auto_filter = 1;
868 sf->iterative_sub_pixel = 1;
869 sf->thresh_mult[THR_NEARESTMV] = 0;
870 sf->thresh_mult[THR_ZEROMV ] = 0;
871 sf->thresh_mult[THR_DC ] = 0;
872 sf->thresh_mult[THR_TM ] = 0;
873 sf->thresh_mult[THR_NEARMV ] = 0;
874 sf->thresh_mult[THR_V_PRED ] = 1000;
875 sf->thresh_mult[THR_H_PRED ] = 1000;
876 sf->thresh_mult[THR_B_PRED ] = 2500;
877 sf->thresh_mult[THR_NEARESTG ] = 1000;
878 sf->thresh_mult[THR_ZEROG ] = 1000;
879 sf->thresh_mult[THR_NEARG ] = 1000;
880 sf->thresh_mult[THR_NEARESTA ] = 1000;
881 sf->thresh_mult[THR_ZEROA ] = 1000;
882 sf->thresh_mult[THR_NEARA ] = 1000;
883 sf->thresh_mult[THR_NEWMV ] = 2000;
884 sf->thresh_mult[THR_NEWG ] = 2000;
885 sf->thresh_mult[THR_NEWA ] = 2000;
886 sf->thresh_mult[THR_SPLITMV ] = 5000;
887 sf->thresh_mult[THR_SPLITG ] = 10000;
888 sf->thresh_mult[THR_SPLITA ] = 10000;
889 sf->full_freq[0] = 15;
890 sf->full_freq[1] = 31;
891 sf->search_method = NSTEP;
893 if (Speed > 0)
895 cpi->mode_check_freq[THR_SPLITG] = 4;
896 cpi->mode_check_freq[THR_SPLITA] = 4;
897 cpi->mode_check_freq[THR_SPLITMV] = 2;
899 sf->thresh_mult[THR_DC ] = 0;
900 sf->thresh_mult[THR_TM ] = 1000;
901 sf->thresh_mult[THR_V_PRED ] = 2000;
902 sf->thresh_mult[THR_H_PRED ] = 2000;
903 sf->thresh_mult[THR_B_PRED ] = 5000;
905 if (cpi->ref_frame_flags & VP8_LAST_FLAG)
907 sf->thresh_mult[THR_NEARESTMV] = 0;
908 sf->thresh_mult[THR_ZEROMV ] = 0;
909 sf->thresh_mult[THR_NEARMV ] = 0;
910 sf->thresh_mult[THR_NEWMV ] = 2000;
911 sf->thresh_mult[THR_SPLITMV ] = 10000;
914 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
916 sf->thresh_mult[THR_NEARESTG ] = 1000;
917 sf->thresh_mult[THR_ZEROG ] = 1000;
918 sf->thresh_mult[THR_NEARG ] = 1000;
919 sf->thresh_mult[THR_NEWG ] = 2000;
920 sf->thresh_mult[THR_SPLITG ] = 20000;
923 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
925 sf->thresh_mult[THR_NEARESTA ] = 1000;
926 sf->thresh_mult[THR_ZEROA ] = 1000;
927 sf->thresh_mult[THR_NEARA ] = 1000;
928 sf->thresh_mult[THR_NEWA ] = 2000;
929 sf->thresh_mult[THR_SPLITA ] = 20000;
932 sf->improved_quant = 0;
933 sf->improved_dct = 0;
936 if (Speed > 1)
938 cpi->mode_check_freq[THR_SPLITMV] = 7;
939 cpi->mode_check_freq[THR_SPLITG] = 15;
940 cpi->mode_check_freq[THR_SPLITA] = 15;
942 sf->thresh_mult[THR_TM ] = 2000;
943 sf->thresh_mult[THR_V_PRED ] = 2000;
944 sf->thresh_mult[THR_H_PRED ] = 2000;
945 sf->thresh_mult[THR_B_PRED ] = 5000;
947 if (cpi->ref_frame_flags & VP8_LAST_FLAG)
949 sf->thresh_mult[THR_NEWMV ] = 2000;
950 sf->thresh_mult[THR_SPLITMV ] = 25000;
953 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
955 sf->thresh_mult[THR_NEARESTG ] = 2000;
956 sf->thresh_mult[THR_ZEROG ] = 2000;
957 sf->thresh_mult[THR_NEARG ] = 2000;
958 sf->thresh_mult[THR_NEWG ] = 2500;
959 sf->thresh_mult[THR_SPLITG ] = 50000;
962 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
964 sf->thresh_mult[THR_NEARESTA ] = 2000;
965 sf->thresh_mult[THR_ZEROA ] = 2000;
966 sf->thresh_mult[THR_NEARA ] = 2000;
967 sf->thresh_mult[THR_NEWA ] = 2500;
968 sf->thresh_mult[THR_SPLITA ] = 50000;
971 sf->full_freq[0] = 31;
972 sf->full_freq[1] = 63;
975 if (Speed > 2)
977 sf->auto_filter = 0; // Faster selection of loop filter
979 cpi->mode_check_freq[THR_V_PRED] = 2;
980 cpi->mode_check_freq[THR_H_PRED] = 2;
981 cpi->mode_check_freq[THR_B_PRED] = 2;
983 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
985 cpi->mode_check_freq[THR_NEARG] = 2;
986 cpi->mode_check_freq[THR_NEWG] = 4;
989 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
991 cpi->mode_check_freq[THR_NEARA] = 2;
992 cpi->mode_check_freq[THR_NEWA] = 4;
995 sf->thresh_mult[THR_SPLITMV ] = INT_MAX;
996 sf->thresh_mult[THR_SPLITG ] = INT_MAX;
997 sf->thresh_mult[THR_SPLITA ] = INT_MAX;
999 sf->full_freq[0] = 63;
1000 sf->full_freq[1] = 127;
1003 if (Speed > 3)
1005 sf->RD = 0;
1006 sf->full_freq[0] = INT_MAX;
1007 sf->full_freq[1] = INT_MAX;
1009 sf->auto_filter = 1;
1012 if (Speed > 4)
1014 sf->auto_filter = 0; // Faster selection of loop filter
1016 #if CONFIG_REALTIME_ONLY
1017 sf->search_method = HEX;
1018 #else
1019 sf->search_method = DIAMOND;
1020 #endif
1021 sf->iterative_sub_pixel = 0;
1023 cpi->mode_check_freq[THR_V_PRED] = 4;
1024 cpi->mode_check_freq[THR_H_PRED] = 4;
1025 cpi->mode_check_freq[THR_B_PRED] = 4;
1027 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
1029 cpi->mode_check_freq[THR_NEARG] = 2;
1030 cpi->mode_check_freq[THR_NEWG] = 4;
1033 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
1035 cpi->mode_check_freq[THR_NEARA] = 2;
1036 cpi->mode_check_freq[THR_NEWA] = 4;
1039 sf->thresh_mult[THR_TM ] = 2000;
1040 sf->thresh_mult[THR_B_PRED ] = 5000;
1042 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
1044 sf->thresh_mult[THR_NEARESTG ] = 2000;
1045 sf->thresh_mult[THR_ZEROG ] = 2000;
1046 sf->thresh_mult[THR_NEARG ] = 2000;
1047 sf->thresh_mult[THR_NEWG ] = 4000;
1050 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
1052 sf->thresh_mult[THR_NEARESTA ] = 2000;
1053 sf->thresh_mult[THR_ZEROA ] = 2000;
1054 sf->thresh_mult[THR_NEARA ] = 2000;
1055 sf->thresh_mult[THR_NEWA ] = 4000;
1059 if (Speed > 5)
1061 // Disable split MB intra prediction mode
1062 sf->thresh_mult[THR_B_PRED] = INT_MAX;
1065 if (Speed > 6)
1067 unsigned int i, sum = 0;
1068 unsigned int total_mbs = cm->MBs;
1069 int thresh;
1070 int total_skip;
1072 int min = 2000;
1074 if (cpi->oxcf.encode_breakout > 2000)
1075 min = cpi->oxcf.encode_breakout;
1077 min >>= 7;
1079 for (i = 0; i < min; i++)
1081 sum += cpi->error_bins[i];
1084 total_skip = sum;
1085 sum = 0;
1087 // i starts from 2 to make sure thresh started from 2048
1088 for (; i < 1024; i++)
1090 sum += cpi->error_bins[i];
1092 if (10 * sum >= (unsigned int)(cpi->Speed - 6)*(total_mbs - total_skip))
1093 break;
1096 i--;
1097 thresh = (i << 7);
1099 if (thresh < 2000)
1100 thresh = 2000;
1102 if (cpi->ref_frame_flags & VP8_LAST_FLAG)
1104 sf->thresh_mult[THR_NEWMV] = thresh;
1105 sf->thresh_mult[THR_NEARESTMV ] = thresh >> 1;
1106 sf->thresh_mult[THR_NEARMV ] = thresh >> 1;
1109 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
1111 sf->thresh_mult[THR_NEWG] = thresh << 1;
1112 sf->thresh_mult[THR_NEARESTG ] = thresh;
1113 sf->thresh_mult[THR_NEARG ] = thresh;
1116 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
1118 sf->thresh_mult[THR_NEWA] = thresh << 1;
1119 sf->thresh_mult[THR_NEARESTA ] = thresh;
1120 sf->thresh_mult[THR_NEARA ] = thresh;
1123 // Disable other intra prediction modes
1124 sf->thresh_mult[THR_TM] = INT_MAX;
1125 sf->thresh_mult[THR_V_PRED] = INT_MAX;
1126 sf->thresh_mult[THR_H_PRED] = INT_MAX;
1128 sf->improved_mv_pred = 0;
1131 if (Speed > 8)
1133 sf->quarter_pixel_search = 0;
1136 if (Speed > 9)
1138 int Tmp = cpi->Speed - 8;
1140 if (Tmp > 4)
1141 Tmp = 4;
1143 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
1145 cpi->mode_check_freq[THR_ZEROG] = 1 << (Tmp - 1);
1146 cpi->mode_check_freq[THR_NEARESTG] = 1 << (Tmp - 1);
1147 cpi->mode_check_freq[THR_NEARG] = 1 << Tmp;
1148 cpi->mode_check_freq[THR_NEWG] = 1 << (Tmp + 1);
1151 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
1153 cpi->mode_check_freq[THR_ZEROA] = 1 << (Tmp - 1);
1154 cpi->mode_check_freq[THR_NEARESTA] = 1 << (Tmp - 1);
1155 cpi->mode_check_freq[THR_NEARA] = 1 << Tmp;
1156 cpi->mode_check_freq[THR_NEWA] = 1 << (Tmp + 1);
1159 cpi->mode_check_freq[THR_NEWMV] = 1 << (Tmp - 1);
1162 cm->filter_type = NORMAL_LOOPFILTER;
1164 if (Speed >= 14)
1165 cm->filter_type = SIMPLE_LOOPFILTER;
1167 if (Speed >= 15)
1169 sf->half_pixel_search = 0; // This has a big hit on quality. Last resort
1172 vpx_memset(cpi->error_bins, 0, sizeof(cpi->error_bins));
1174 }; /* switch */
1176 /* disable frame modes if flags not set */
1177 if (!(cpi->ref_frame_flags & VP8_LAST_FLAG))
1179 sf->thresh_mult[THR_NEWMV ] = INT_MAX;
1180 sf->thresh_mult[THR_NEARESTMV] = INT_MAX;
1181 sf->thresh_mult[THR_ZEROMV ] = INT_MAX;
1182 sf->thresh_mult[THR_NEARMV ] = INT_MAX;
1183 sf->thresh_mult[THR_SPLITMV ] = INT_MAX;
1186 if (!(cpi->ref_frame_flags & VP8_GOLD_FLAG))
1188 sf->thresh_mult[THR_NEARESTG ] = INT_MAX;
1189 sf->thresh_mult[THR_ZEROG ] = INT_MAX;
1190 sf->thresh_mult[THR_NEARG ] = INT_MAX;
1191 sf->thresh_mult[THR_NEWG ] = INT_MAX;
1192 sf->thresh_mult[THR_SPLITG ] = INT_MAX;
1195 if (!(cpi->ref_frame_flags & VP8_ALT_FLAG))
1197 sf->thresh_mult[THR_NEARESTA ] = INT_MAX;
1198 sf->thresh_mult[THR_ZEROA ] = INT_MAX;
1199 sf->thresh_mult[THR_NEARA ] = INT_MAX;
1200 sf->thresh_mult[THR_NEWA ] = INT_MAX;
1201 sf->thresh_mult[THR_SPLITA ] = INT_MAX;
1205 // Slow quant, dct and trellis not worthwhile for first pass
1206 // so make sure they are always turned off.
1207 if ( cpi->pass == 1 )
1209 sf->improved_quant = 0;
1210 sf->optimize_coefficients = 0;
1211 sf->improved_dct = 0;
1214 if (cpi->sf.search_method == NSTEP)
1216 vp8_init3smotion_compensation(&cpi->mb, cm->yv12_fb[cm->lst_fb_idx].y_stride);
1218 else if (cpi->sf.search_method == DIAMOND)
1220 vp8_init_dsmotion_compensation(&cpi->mb, cm->yv12_fb[cm->lst_fb_idx].y_stride);
1223 if (cpi->sf.improved_dct)
1225 cpi->mb.vp8_short_fdct8x4 = FDCT_INVOKE(&cpi->rtcd.fdct, short8x4);
1226 cpi->mb.vp8_short_fdct4x4 = FDCT_INVOKE(&cpi->rtcd.fdct, short4x4);
1228 else
1230 cpi->mb.vp8_short_fdct8x4 = FDCT_INVOKE(&cpi->rtcd.fdct, fast8x4);
1231 cpi->mb.vp8_short_fdct4x4 = FDCT_INVOKE(&cpi->rtcd.fdct, fast4x4);
1234 cpi->mb.short_walsh4x4 = FDCT_INVOKE(&cpi->rtcd.fdct, walsh_short4x4);
1236 if (cpi->sf.improved_quant)
1238 cpi->mb.quantize_b = QUANTIZE_INVOKE(&cpi->rtcd.quantize, quantb);
1240 else
1242 cpi->mb.quantize_b = QUANTIZE_INVOKE(&cpi->rtcd.quantize, fastquantb);
1244 if (cpi->sf.improved_quant != last_improved_quant)
1245 vp8cx_init_quantizer(cpi);
1247 #if CONFIG_RUNTIME_CPU_DETECT
1248 cpi->mb.e_mbd.rtcd = &cpi->common.rtcd;
1249 #endif
1251 if (cpi->sf.iterative_sub_pixel == 1)
1253 cpi->find_fractional_mv_step = vp8_find_best_sub_pixel_step_iteratively;
1255 else if (cpi->sf.quarter_pixel_search)
1257 cpi->find_fractional_mv_step = vp8_find_best_sub_pixel_step;
1259 else if (cpi->sf.half_pixel_search)
1261 cpi->find_fractional_mv_step = vp8_find_best_half_pixel_step;
1263 else
1265 cpi->find_fractional_mv_step = vp8_skip_fractional_mv_step;
1268 if (cpi->sf.optimize_coefficients == 1)
1269 cpi->mb.optimize = 1 + cpi->is_next_src_alt_ref;
1270 else
1271 cpi->mb.optimize = 0;
1273 if (cpi->common.full_pixel)
1274 cpi->find_fractional_mv_step = vp8_skip_fractional_mv_step;
1276 #ifdef SPEEDSTATS
1277 frames_at_speed[cpi->Speed]++;
1278 #endif
1280 static void alloc_raw_frame_buffers(VP8_COMP *cpi)
1282 int i, buffers;
1284 buffers = cpi->oxcf.lag_in_frames;
1286 if (buffers > MAX_LAG_BUFFERS)
1287 buffers = MAX_LAG_BUFFERS;
1289 if (buffers < 1)
1290 buffers = 1;
1292 for (i = 0; i < buffers; i++)
1293 if (vp8_yv12_alloc_frame_buffer(&cpi->src_buffer[i].source_buffer,
1294 cpi->oxcf.Width, cpi->oxcf.Height,
1295 16))
1296 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1297 "Failed to allocate lag buffer");
1299 #if VP8_TEMPORAL_ALT_REF
1301 if (vp8_yv12_alloc_frame_buffer(&cpi->alt_ref_buffer.source_buffer,
1302 cpi->oxcf.Width, cpi->oxcf.Height, 16))
1303 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1304 "Failed to allocate altref buffer");
1306 #endif
1308 cpi->source_buffer_count = 0;
1311 static int vp8_alloc_partition_data(VP8_COMP *cpi)
1313 if(cpi->mb.pip)
1314 vpx_free(cpi->mb.pip);
1316 cpi->mb.pip = vpx_calloc((cpi->common.mb_cols + 1) *
1317 (cpi->common.mb_rows + 1),
1318 sizeof(PARTITION_INFO));
1319 if(!cpi->mb.pip)
1320 return ALLOC_FAILURE;
1322 cpi->mb.pi = cpi->mb.pip + cpi->common.mode_info_stride + 1;
1324 return 0;
1327 void vp8_alloc_compressor_data(VP8_COMP *cpi)
1329 VP8_COMMON *cm = & cpi->common;
1331 int width = cm->Width;
1332 int height = cm->Height;
1334 if (vp8_alloc_frame_buffers(cm, width, height))
1335 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1336 "Failed to allocate frame buffers");
1338 if (vp8_alloc_partition_data(cpi))
1339 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1340 "Failed to allocate partition data");
1343 if ((width & 0xf) != 0)
1344 width += 16 - (width & 0xf);
1346 if ((height & 0xf) != 0)
1347 height += 16 - (height & 0xf);
1350 if (vp8_yv12_alloc_frame_buffer(&cpi->last_frame_uf,
1351 width, height, VP8BORDERINPIXELS))
1352 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1353 "Failed to allocate last frame buffer");
1355 if (vp8_yv12_alloc_frame_buffer(&cpi->scaled_source, width, height, 16))
1356 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1357 "Failed to allocate scaled source buffer");
1360 if (cpi->tok != 0)
1361 vpx_free(cpi->tok);
1364 unsigned int tokens = cm->mb_rows * cm->mb_cols * 24 * 16;
1366 CHECK_MEM_ERROR(cpi->tok, vpx_calloc(tokens, sizeof(*cpi->tok)));
1369 // Data used for real time vc mode to see if gf needs refreshing
1370 cpi->inter_zz_count = 0;
1371 cpi->gf_bad_count = 0;
1372 cpi->gf_update_recommended = 0;
1375 // Structures used to minitor GF usage
1376 if (cpi->gf_active_flags != 0)
1377 vpx_free(cpi->gf_active_flags);
1379 CHECK_MEM_ERROR(cpi->gf_active_flags, vpx_calloc(1, cm->mb_rows * cm->mb_cols));
1381 cpi->gf_active_count = cm->mb_rows * cm->mb_cols;
1383 #if !(CONFIG_REALTIME_ONLY)
1384 if(cpi->total_stats)
1385 vpx_free(cpi->total_stats);
1387 cpi->total_stats = vpx_calloc(1, vp8_firstpass_stats_sz(cpi->common.MBs));
1389 if(cpi->this_frame_stats)
1390 vpx_free(cpi->this_frame_stats);
1392 cpi->this_frame_stats = vpx_calloc(1, vp8_firstpass_stats_sz(cpi->common.MBs));
1394 if(!cpi->total_stats || !cpi->this_frame_stats)
1395 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1396 "Failed to allocate firstpass stats");
1397 #endif
1399 #if CONFIG_MULTITHREAD
1400 if (width < 640)
1401 cpi->mt_sync_range = 1;
1402 else if (width <= 1280)
1403 cpi->mt_sync_range = 4;
1404 else if (width <= 2560)
1405 cpi->mt_sync_range = 8;
1406 else
1407 cpi->mt_sync_range = 16;
1408 #endif
1410 if(cpi->tplist);
1411 vpx_free(cpi->tplist);
1413 CHECK_MEM_ERROR(cpi->tplist, vpx_malloc(sizeof(TOKENLIST) * cpi->common.mb_rows));
1417 // Quant MOD
1418 static const int q_trans[] =
1420 0, 1, 2, 3, 4, 5, 7, 8,
1421 9, 10, 12, 13, 15, 17, 18, 19,
1422 20, 21, 23, 24, 25, 26, 27, 28,
1423 29, 30, 31, 33, 35, 37, 39, 41,
1424 43, 45, 47, 49, 51, 53, 55, 57,
1425 59, 61, 64, 67, 70, 73, 76, 79,
1426 82, 85, 88, 91, 94, 97, 100, 103,
1427 106, 109, 112, 115, 118, 121, 124, 127,
1430 int vp8_reverse_trans(int x)
1432 int i;
1434 for (i = 0; i < 64; i++)
1435 if (q_trans[i] >= x)
1436 return i;
1438 return 63;
1440 void vp8_new_frame_rate(VP8_COMP *cpi, double framerate)
1442 if(framerate < .1)
1443 framerate = 30;
1445 cpi->oxcf.frame_rate = framerate;
1446 cpi->output_frame_rate = cpi->oxcf.frame_rate;
1447 cpi->per_frame_bandwidth = (int)(cpi->oxcf.target_bandwidth / cpi->output_frame_rate);
1448 cpi->av_per_frame_bandwidth = (int)(cpi->oxcf.target_bandwidth / cpi->output_frame_rate);
1449 cpi->min_frame_bandwidth = (int)(cpi->av_per_frame_bandwidth * cpi->oxcf.two_pass_vbrmin_section / 100);
1451 // Set Maximum gf/arf interval
1452 cpi->max_gf_interval = ((int)(cpi->output_frame_rate / 2.0) + 2);
1454 if(cpi->max_gf_interval < 12)
1455 cpi->max_gf_interval = 12;
1457 // Extended interval for genuinely static scenes
1458 cpi->static_scene_max_gf_interval = cpi->key_frame_frequency >> 1;
1460 // Special conditions when altr ref frame enabled in lagged compress mode
1461 if (cpi->oxcf.play_alternate && cpi->oxcf.lag_in_frames)
1463 if (cpi->max_gf_interval > cpi->oxcf.lag_in_frames - 1)
1464 cpi->max_gf_interval = cpi->oxcf.lag_in_frames - 1;
1466 if (cpi->static_scene_max_gf_interval > cpi->oxcf.lag_in_frames - 1)
1467 cpi->static_scene_max_gf_interval = cpi->oxcf.lag_in_frames - 1;
1470 if ( cpi->max_gf_interval > cpi->static_scene_max_gf_interval )
1471 cpi->max_gf_interval = cpi->static_scene_max_gf_interval;
1475 static int
1476 rescale(int val, int num, int denom)
1478 int64_t llnum = num;
1479 int64_t llden = denom;
1480 int64_t llval = val;
1482 return llval * llnum / llden;
1486 void vp8_init_config(VP8_PTR ptr, VP8_CONFIG *oxcf)
1488 VP8_COMP *cpi = (VP8_COMP *)(ptr);
1489 VP8_COMMON *cm = &cpi->common;
1491 if (!cpi)
1492 return;
1494 cpi->auto_gold = 1;
1495 cpi->auto_adjust_gold_quantizer = 1;
1496 cpi->goldquantizer = 1;
1497 cpi->goldfreq = 7;
1498 cpi->auto_adjust_key_quantizer = 1;
1499 cpi->keyquantizer = 1;
1501 cm->version = oxcf->Version;
1502 vp8_setup_version(cm);
1504 if (oxcf == 0)
1506 cpi->pass = 0;
1508 cpi->auto_worst_q = 0;
1509 cpi->oxcf.best_allowed_q = MINQ;
1510 cpi->oxcf.worst_allowed_q = MAXQ;
1511 cpi->oxcf.cq_level = MINQ;
1513 cpi->oxcf.end_usage = USAGE_STREAM_FROM_SERVER;
1514 cpi->oxcf.starting_buffer_level = 4000;
1515 cpi->oxcf.optimal_buffer_level = 5000;
1516 cpi->oxcf.maximum_buffer_size = 6000;
1517 cpi->oxcf.under_shoot_pct = 90;
1518 cpi->oxcf.allow_df = 0;
1519 cpi->oxcf.drop_frames_water_mark = 20;
1521 cpi->oxcf.allow_spatial_resampling = 0;
1522 cpi->oxcf.resample_down_water_mark = 40;
1523 cpi->oxcf.resample_up_water_mark = 60;
1525 cpi->oxcf.fixed_q = cpi->interquantizer;
1527 cpi->filter_type = NORMAL_LOOPFILTER;
1529 if (cm->simpler_lpf)
1530 cpi->filter_type = SIMPLE_LOOPFILTER;
1532 cpi->compressor_speed = 1;
1533 cpi->horiz_scale = 0;
1534 cpi->vert_scale = 0;
1535 cpi->oxcf.two_pass_vbrbias = 50;
1536 cpi->oxcf.two_pass_vbrmax_section = 400;
1537 cpi->oxcf.two_pass_vbrmin_section = 0;
1539 cpi->oxcf.Sharpness = 0;
1540 cpi->oxcf.noise_sensitivity = 0;
1542 else
1543 cpi->oxcf = *oxcf;
1546 switch (cpi->oxcf.Mode)
1549 case MODE_REALTIME:
1550 cpi->pass = 0;
1551 cpi->compressor_speed = 2;
1553 if (cpi->oxcf.cpu_used < -16)
1555 cpi->oxcf.cpu_used = -16;
1558 if (cpi->oxcf.cpu_used > 16)
1559 cpi->oxcf.cpu_used = 16;
1561 break;
1563 #if !(CONFIG_REALTIME_ONLY)
1564 case MODE_GOODQUALITY:
1565 cpi->pass = 0;
1566 cpi->compressor_speed = 1;
1568 if (cpi->oxcf.cpu_used < -5)
1570 cpi->oxcf.cpu_used = -5;
1573 if (cpi->oxcf.cpu_used > 5)
1574 cpi->oxcf.cpu_used = 5;
1576 break;
1578 case MODE_BESTQUALITY:
1579 cpi->pass = 0;
1580 cpi->compressor_speed = 0;
1581 break;
1583 case MODE_FIRSTPASS:
1584 cpi->pass = 1;
1585 cpi->compressor_speed = 1;
1586 break;
1587 case MODE_SECONDPASS:
1588 cpi->pass = 2;
1589 cpi->compressor_speed = 1;
1591 if (cpi->oxcf.cpu_used < -5)
1593 cpi->oxcf.cpu_used = -5;
1596 if (cpi->oxcf.cpu_used > 5)
1597 cpi->oxcf.cpu_used = 5;
1599 break;
1600 case MODE_SECONDPASS_BEST:
1601 cpi->pass = 2;
1602 cpi->compressor_speed = 0;
1603 break;
1604 #endif
1607 if (cpi->pass == 0)
1608 cpi->auto_worst_q = 1;
1610 cpi->oxcf.worst_allowed_q = q_trans[oxcf->worst_allowed_q];
1611 cpi->oxcf.best_allowed_q = q_trans[oxcf->best_allowed_q];
1612 cpi->oxcf.cq_level = q_trans[cpi->oxcf.cq_level];
1614 if (oxcf->fixed_q >= 0)
1616 if (oxcf->worst_allowed_q < 0)
1617 cpi->oxcf.fixed_q = q_trans[0];
1618 else
1619 cpi->oxcf.fixed_q = q_trans[oxcf->worst_allowed_q];
1621 if (oxcf->alt_q < 0)
1622 cpi->oxcf.alt_q = q_trans[0];
1623 else
1624 cpi->oxcf.alt_q = q_trans[oxcf->alt_q];
1626 if (oxcf->key_q < 0)
1627 cpi->oxcf.key_q = q_trans[0];
1628 else
1629 cpi->oxcf.key_q = q_trans[oxcf->key_q];
1631 if (oxcf->gold_q < 0)
1632 cpi->oxcf.gold_q = q_trans[0];
1633 else
1634 cpi->oxcf.gold_q = q_trans[oxcf->gold_q];
1638 cpi->baseline_gf_interval = cpi->oxcf.alt_freq ? cpi->oxcf.alt_freq : DEFAULT_GF_INTERVAL;
1639 cpi->ref_frame_flags = VP8_ALT_FLAG | VP8_GOLD_FLAG | VP8_LAST_FLAG;
1641 //cpi->use_golden_frame_only = 0;
1642 //cpi->use_last_frame_only = 0;
1643 cm->refresh_golden_frame = 0;
1644 cm->refresh_last_frame = 1;
1645 cm->refresh_entropy_probs = 1;
1647 if (cpi->oxcf.token_partitions >= 0 && cpi->oxcf.token_partitions <= 3)
1648 cm->multi_token_partition = (TOKEN_PARTITION) cpi->oxcf.token_partitions;
1650 setup_features(cpi);
1653 int i;
1655 for (i = 0; i < MAX_MB_SEGMENTS; i++)
1656 cpi->segment_encode_breakout[i] = cpi->oxcf.encode_breakout;
1659 // At the moment the first order values may not be > MAXQ
1660 if (cpi->oxcf.fixed_q > MAXQ)
1661 cpi->oxcf.fixed_q = MAXQ;
1663 // local file playback mode == really big buffer
1664 if (cpi->oxcf.end_usage == USAGE_LOCAL_FILE_PLAYBACK)
1666 cpi->oxcf.starting_buffer_level = 60000;
1667 cpi->oxcf.optimal_buffer_level = 60000;
1668 cpi->oxcf.maximum_buffer_size = 240000;
1673 // Convert target bandwidth from Kbit/s to Bit/s
1674 cpi->oxcf.target_bandwidth *= 1000;
1675 cpi->oxcf.starting_buffer_level =
1676 rescale(cpi->oxcf.starting_buffer_level,
1677 cpi->oxcf.target_bandwidth, 1000);
1679 if (cpi->oxcf.optimal_buffer_level == 0)
1680 cpi->oxcf.optimal_buffer_level = cpi->oxcf.target_bandwidth / 8;
1681 else
1682 cpi->oxcf.optimal_buffer_level =
1683 rescale(cpi->oxcf.optimal_buffer_level,
1684 cpi->oxcf.target_bandwidth, 1000);
1686 if (cpi->oxcf.maximum_buffer_size == 0)
1687 cpi->oxcf.maximum_buffer_size = cpi->oxcf.target_bandwidth / 8;
1688 else
1689 cpi->oxcf.maximum_buffer_size =
1690 rescale(cpi->oxcf.maximum_buffer_size,
1691 cpi->oxcf.target_bandwidth, 1000);
1693 cpi->buffer_level = cpi->oxcf.starting_buffer_level;
1694 cpi->bits_off_target = cpi->oxcf.starting_buffer_level;
1696 vp8_new_frame_rate(cpi, cpi->oxcf.frame_rate);
1697 cpi->worst_quality = cpi->oxcf.worst_allowed_q;
1698 cpi->active_worst_quality = cpi->oxcf.worst_allowed_q;
1699 cpi->avg_frame_qindex = cpi->oxcf.worst_allowed_q;
1700 cpi->best_quality = cpi->oxcf.best_allowed_q;
1701 cpi->active_best_quality = cpi->oxcf.best_allowed_q;
1702 cpi->cq_target_quality = cpi->oxcf.cq_level;
1704 cpi->buffered_mode = (cpi->oxcf.optimal_buffer_level > 0) ? TRUE : FALSE;
1706 cpi->rolling_target_bits = cpi->av_per_frame_bandwidth;
1707 cpi->rolling_actual_bits = cpi->av_per_frame_bandwidth;
1708 cpi->long_rolling_target_bits = cpi->av_per_frame_bandwidth;
1709 cpi->long_rolling_actual_bits = cpi->av_per_frame_bandwidth;
1711 cpi->total_actual_bits = 0;
1712 cpi->total_target_vs_actual = 0;
1714 // Only allow dropped frames in buffered mode
1715 cpi->drop_frames_allowed = cpi->oxcf.allow_df && cpi->buffered_mode;
1717 cm->filter_type = (LOOPFILTERTYPE) cpi->filter_type;
1719 if (!cm->use_bilinear_mc_filter)
1720 cm->mcomp_filter_type = SIXTAP;
1721 else
1722 cm->mcomp_filter_type = BILINEAR;
1724 cpi->target_bandwidth = cpi->oxcf.target_bandwidth;
1726 cm->Width = cpi->oxcf.Width ;
1727 cm->Height = cpi->oxcf.Height ;
1729 cpi->intra_frame_target = (4 * (cm->Width + cm->Height) / 15) * 1000; // As per VP8
1731 cm->horiz_scale = cpi->horiz_scale;
1732 cm->vert_scale = cpi->vert_scale ;
1734 // VP8 sharpness level mapping 0-7 (vs 0-10 in general VPx dialogs)
1735 if (cpi->oxcf.Sharpness > 7)
1736 cpi->oxcf.Sharpness = 7;
1738 cm->sharpness_level = cpi->oxcf.Sharpness;
1740 if (cm->horiz_scale != NORMAL || cm->vert_scale != NORMAL)
1742 int UNINITIALIZED_IS_SAFE(hr), UNINITIALIZED_IS_SAFE(hs);
1743 int UNINITIALIZED_IS_SAFE(vr), UNINITIALIZED_IS_SAFE(vs);
1745 Scale2Ratio(cm->horiz_scale, &hr, &hs);
1746 Scale2Ratio(cm->vert_scale, &vr, &vs);
1748 // always go to the next whole number
1749 cm->Width = (hs - 1 + cpi->oxcf.Width * hr) / hs;
1750 cm->Height = (vs - 1 + cpi->oxcf.Height * vr) / vs;
1753 if (((cm->Width + 15) & 0xfffffff0) != cm->yv12_fb[cm->lst_fb_idx].y_width ||
1754 ((cm->Height + 15) & 0xfffffff0) != cm->yv12_fb[cm->lst_fb_idx].y_height ||
1755 cm->yv12_fb[cm->lst_fb_idx].y_width == 0)
1757 alloc_raw_frame_buffers(cpi);
1758 vp8_alloc_compressor_data(cpi);
1761 // Clamp KF frame size to quarter of data rate
1762 if (cpi->intra_frame_target > cpi->target_bandwidth >> 2)
1763 cpi->intra_frame_target = cpi->target_bandwidth >> 2;
1765 if (cpi->oxcf.fixed_q >= 0)
1767 cpi->last_q[0] = cpi->oxcf.fixed_q;
1768 cpi->last_q[1] = cpi->oxcf.fixed_q;
1771 cpi->Speed = cpi->oxcf.cpu_used;
1773 // force to allowlag to 0 if lag_in_frames is 0;
1774 if (cpi->oxcf.lag_in_frames == 0)
1776 cpi->oxcf.allow_lag = 0;
1778 // Limit on lag buffers as these are not currently dynamically allocated
1779 else if (cpi->oxcf.lag_in_frames > MAX_LAG_BUFFERS)
1780 cpi->oxcf.lag_in_frames = MAX_LAG_BUFFERS;
1782 // YX Temp
1783 cpi->last_alt_ref_sei = -1;
1784 cpi->is_src_frame_alt_ref = 0;
1785 cpi->is_next_src_alt_ref = 0;
1787 #if 0
1788 // Experimental RD Code
1789 cpi->frame_distortion = 0;
1790 cpi->last_frame_distortion = 0;
1791 #endif
1793 #if VP8_TEMPORAL_ALT_REF
1795 cpi->use_weighted_temporal_filter = 0;
1798 int i;
1800 cpi->fixed_divide[0] = 0;
1802 for (i = 1; i < 512; i++)
1803 cpi->fixed_divide[i] = 0x80000 / i;
1805 #endif
1809 * This function needs more clean up, i.e. be more tuned torwards
1810 * change_config rather than init_config !!!!!!!!!!!!!!!!
1811 * YX - 5/28/2009
1815 void vp8_change_config(VP8_PTR ptr, VP8_CONFIG *oxcf)
1817 VP8_COMP *cpi = (VP8_COMP *)(ptr);
1818 VP8_COMMON *cm = &cpi->common;
1820 if (!cpi)
1821 return;
1823 if (!oxcf)
1824 return;
1826 if (cm->version != oxcf->Version)
1828 cm->version = oxcf->Version;
1829 vp8_setup_version(cm);
1832 cpi->oxcf = *oxcf;
1834 switch (cpi->oxcf.Mode)
1837 case MODE_REALTIME:
1838 cpi->pass = 0;
1839 cpi->compressor_speed = 2;
1841 if (cpi->oxcf.cpu_used < -16)
1843 cpi->oxcf.cpu_used = -16;
1846 if (cpi->oxcf.cpu_used > 16)
1847 cpi->oxcf.cpu_used = 16;
1849 break;
1851 #if !(CONFIG_REALTIME_ONLY)
1852 case MODE_GOODQUALITY:
1853 cpi->pass = 0;
1854 cpi->compressor_speed = 1;
1856 if (cpi->oxcf.cpu_used < -5)
1858 cpi->oxcf.cpu_used = -5;
1861 if (cpi->oxcf.cpu_used > 5)
1862 cpi->oxcf.cpu_used = 5;
1864 break;
1866 case MODE_BESTQUALITY:
1867 cpi->pass = 0;
1868 cpi->compressor_speed = 0;
1869 break;
1871 case MODE_FIRSTPASS:
1872 cpi->pass = 1;
1873 cpi->compressor_speed = 1;
1874 break;
1875 case MODE_SECONDPASS:
1876 cpi->pass = 2;
1877 cpi->compressor_speed = 1;
1879 if (cpi->oxcf.cpu_used < -5)
1881 cpi->oxcf.cpu_used = -5;
1884 if (cpi->oxcf.cpu_used > 5)
1885 cpi->oxcf.cpu_used = 5;
1887 break;
1888 case MODE_SECONDPASS_BEST:
1889 cpi->pass = 2;
1890 cpi->compressor_speed = 0;
1891 break;
1892 #endif
1895 if (cpi->pass == 0)
1896 cpi->auto_worst_q = 1;
1898 cpi->oxcf.worst_allowed_q = q_trans[oxcf->worst_allowed_q];
1899 cpi->oxcf.best_allowed_q = q_trans[oxcf->best_allowed_q];
1900 cpi->oxcf.cq_level = q_trans[cpi->oxcf.cq_level];
1902 if (oxcf->fixed_q >= 0)
1904 if (oxcf->worst_allowed_q < 0)
1905 cpi->oxcf.fixed_q = q_trans[0];
1906 else
1907 cpi->oxcf.fixed_q = q_trans[oxcf->worst_allowed_q];
1909 if (oxcf->alt_q < 0)
1910 cpi->oxcf.alt_q = q_trans[0];
1911 else
1912 cpi->oxcf.alt_q = q_trans[oxcf->alt_q];
1914 if (oxcf->key_q < 0)
1915 cpi->oxcf.key_q = q_trans[0];
1916 else
1917 cpi->oxcf.key_q = q_trans[oxcf->key_q];
1919 if (oxcf->gold_q < 0)
1920 cpi->oxcf.gold_q = q_trans[0];
1921 else
1922 cpi->oxcf.gold_q = q_trans[oxcf->gold_q];
1926 cpi->baseline_gf_interval = cpi->oxcf.alt_freq ? cpi->oxcf.alt_freq : DEFAULT_GF_INTERVAL;
1928 cpi->ref_frame_flags = VP8_ALT_FLAG | VP8_GOLD_FLAG | VP8_LAST_FLAG;
1930 //cpi->use_golden_frame_only = 0;
1931 //cpi->use_last_frame_only = 0;
1932 cm->refresh_golden_frame = 0;
1933 cm->refresh_last_frame = 1;
1934 cm->refresh_entropy_probs = 1;
1936 if (cpi->oxcf.token_partitions >= 0 && cpi->oxcf.token_partitions <= 3)
1937 cm->multi_token_partition = (TOKEN_PARTITION) cpi->oxcf.token_partitions;
1939 setup_features(cpi);
1942 int i;
1944 for (i = 0; i < MAX_MB_SEGMENTS; i++)
1945 cpi->segment_encode_breakout[i] = cpi->oxcf.encode_breakout;
1948 // At the moment the first order values may not be > MAXQ
1949 if (cpi->oxcf.fixed_q > MAXQ)
1950 cpi->oxcf.fixed_q = MAXQ;
1952 // local file playback mode == really big buffer
1953 if (cpi->oxcf.end_usage == USAGE_LOCAL_FILE_PLAYBACK)
1955 cpi->oxcf.starting_buffer_level = 60000;
1956 cpi->oxcf.optimal_buffer_level = 60000;
1957 cpi->oxcf.maximum_buffer_size = 240000;
1961 // Convert target bandwidth from Kbit/s to Bit/s
1962 cpi->oxcf.target_bandwidth *= 1000;
1964 cpi->oxcf.starting_buffer_level =
1965 rescale(cpi->oxcf.starting_buffer_level,
1966 cpi->oxcf.target_bandwidth, 1000);
1968 if (cpi->oxcf.optimal_buffer_level == 0)
1969 cpi->oxcf.optimal_buffer_level = cpi->oxcf.target_bandwidth / 8;
1970 else
1971 cpi->oxcf.optimal_buffer_level =
1972 rescale(cpi->oxcf.optimal_buffer_level,
1973 cpi->oxcf.target_bandwidth, 1000);
1975 if (cpi->oxcf.maximum_buffer_size == 0)
1976 cpi->oxcf.maximum_buffer_size = cpi->oxcf.target_bandwidth / 8;
1977 else
1978 cpi->oxcf.maximum_buffer_size =
1979 rescale(cpi->oxcf.maximum_buffer_size,
1980 cpi->oxcf.target_bandwidth, 1000);
1982 cpi->buffer_level = cpi->oxcf.starting_buffer_level;
1983 cpi->bits_off_target = cpi->oxcf.starting_buffer_level;
1985 vp8_new_frame_rate(cpi, cpi->oxcf.frame_rate);
1986 cpi->worst_quality = cpi->oxcf.worst_allowed_q;
1987 cpi->active_worst_quality = cpi->oxcf.worst_allowed_q;
1988 cpi->avg_frame_qindex = cpi->oxcf.worst_allowed_q;
1989 cpi->best_quality = cpi->oxcf.best_allowed_q;
1990 cpi->active_best_quality = cpi->oxcf.best_allowed_q;
1991 cpi->buffered_mode = (cpi->oxcf.optimal_buffer_level > 0) ? TRUE : FALSE;
1993 cpi->cq_target_quality = cpi->oxcf.cq_level;
1995 cpi->rolling_target_bits = cpi->av_per_frame_bandwidth;
1996 cpi->rolling_actual_bits = cpi->av_per_frame_bandwidth;
1997 cpi->long_rolling_target_bits = cpi->av_per_frame_bandwidth;
1998 cpi->long_rolling_actual_bits = cpi->av_per_frame_bandwidth;
2000 cpi->total_actual_bits = 0;
2001 cpi->total_target_vs_actual = 0;
2003 // Only allow dropped frames in buffered mode
2004 cpi->drop_frames_allowed = cpi->oxcf.allow_df && cpi->buffered_mode;
2006 cm->filter_type = (LOOPFILTERTYPE) cpi->filter_type;
2008 if (!cm->use_bilinear_mc_filter)
2009 cm->mcomp_filter_type = SIXTAP;
2010 else
2011 cm->mcomp_filter_type = BILINEAR;
2013 cpi->target_bandwidth = cpi->oxcf.target_bandwidth;
2015 cm->Width = cpi->oxcf.Width ;
2016 cm->Height = cpi->oxcf.Height ;
2018 cm->horiz_scale = cpi->horiz_scale;
2019 cm->vert_scale = cpi->vert_scale ;
2021 cpi->intra_frame_target = (4 * (cm->Width + cm->Height) / 15) * 1000; // As per VP8
2023 // VP8 sharpness level mapping 0-7 (vs 0-10 in general VPx dialogs)
2024 if (cpi->oxcf.Sharpness > 7)
2025 cpi->oxcf.Sharpness = 7;
2027 cm->sharpness_level = cpi->oxcf.Sharpness;
2029 if (cm->horiz_scale != NORMAL || cm->vert_scale != NORMAL)
2031 int UNINITIALIZED_IS_SAFE(hr), UNINITIALIZED_IS_SAFE(hs);
2032 int UNINITIALIZED_IS_SAFE(vr), UNINITIALIZED_IS_SAFE(vs);
2034 Scale2Ratio(cm->horiz_scale, &hr, &hs);
2035 Scale2Ratio(cm->vert_scale, &vr, &vs);
2037 // always go to the next whole number
2038 cm->Width = (hs - 1 + cpi->oxcf.Width * hr) / hs;
2039 cm->Height = (vs - 1 + cpi->oxcf.Height * vr) / vs;
2042 if (((cm->Width + 15) & 0xfffffff0) != cm->yv12_fb[cm->lst_fb_idx].y_width ||
2043 ((cm->Height + 15) & 0xfffffff0) != cm->yv12_fb[cm->lst_fb_idx].y_height ||
2044 cm->yv12_fb[cm->lst_fb_idx].y_width == 0)
2046 alloc_raw_frame_buffers(cpi);
2047 vp8_alloc_compressor_data(cpi);
2050 // Clamp KF frame size to quarter of data rate
2051 if (cpi->intra_frame_target > cpi->target_bandwidth >> 2)
2052 cpi->intra_frame_target = cpi->target_bandwidth >> 2;
2054 if (cpi->oxcf.fixed_q >= 0)
2056 cpi->last_q[0] = cpi->oxcf.fixed_q;
2057 cpi->last_q[1] = cpi->oxcf.fixed_q;
2060 cpi->Speed = cpi->oxcf.cpu_used;
2062 // force to allowlag to 0 if lag_in_frames is 0;
2063 if (cpi->oxcf.lag_in_frames == 0)
2065 cpi->oxcf.allow_lag = 0;
2067 // Limit on lag buffers as these are not currently dynamically allocated
2068 else if (cpi->oxcf.lag_in_frames > MAX_LAG_BUFFERS)
2069 cpi->oxcf.lag_in_frames = MAX_LAG_BUFFERS;
2071 // YX Temp
2072 cpi->last_alt_ref_sei = -1;
2073 cpi->is_src_frame_alt_ref = 0;
2074 cpi->is_next_src_alt_ref = 0;
2076 #if 0
2077 // Experimental RD Code
2078 cpi->frame_distortion = 0;
2079 cpi->last_frame_distortion = 0;
2080 #endif
2084 #define M_LOG2_E 0.693147180559945309417
2085 #define log2f(x) (log (x) / (float) M_LOG2_E)
2086 static void cal_mvsadcosts(int *mvsadcost[2])
2088 int i = 1;
2090 mvsadcost [0] [0] = 300;
2091 mvsadcost [1] [0] = 300;
2095 double z = 256 * (2 * (log2f(2 * i) + .6));
2096 mvsadcost [0][i] = (int) z;
2097 mvsadcost [1][i] = (int) z;
2098 mvsadcost [0][-i] = (int) z;
2099 mvsadcost [1][-i] = (int) z;
2101 while (++i <= mv_max);
2104 VP8_PTR vp8_create_compressor(VP8_CONFIG *oxcf)
2106 int i;
2107 volatile union
2109 VP8_COMP *cpi;
2110 VP8_PTR ptr;
2111 } ctx;
2113 VP8_COMP *cpi;
2114 VP8_COMMON *cm;
2116 cpi = ctx.cpi = vpx_memalign(32, sizeof(VP8_COMP));
2117 // Check that the CPI instance is valid
2118 if (!cpi)
2119 return 0;
2121 cm = &cpi->common;
2123 vpx_memset(cpi, 0, sizeof(VP8_COMP));
2125 if (setjmp(cm->error.jmp))
2127 VP8_PTR ptr = ctx.ptr;
2129 ctx.cpi->common.error.setjmp = 0;
2130 vp8_remove_compressor(&ptr);
2131 return 0;
2134 cpi->common.error.setjmp = 1;
2136 CHECK_MEM_ERROR(cpi->mb.ss, vpx_calloc(sizeof(search_site), (MAX_MVSEARCH_STEPS * 8) + 1));
2138 vp8_create_common(&cpi->common);
2139 vp8_cmachine_specific_config(cpi);
2141 vp8_init_config((VP8_PTR)cpi, oxcf);
2143 memcpy(cpi->base_skip_false_prob, vp8cx_base_skip_false_prob, sizeof(vp8cx_base_skip_false_prob));
2144 cpi->common.current_video_frame = 0;
2145 cpi->kf_overspend_bits = 0;
2146 cpi->kf_bitrate_adjustment = 0;
2147 cpi->frames_till_gf_update_due = 0;
2148 cpi->gf_overspend_bits = 0;
2149 cpi->non_gf_bitrate_adjustment = 0;
2150 cpi->prob_last_coded = 128;
2151 cpi->prob_gf_coded = 128;
2152 cpi->prob_intra_coded = 63;
2154 // Prime the recent reference frame useage counters.
2155 // Hereafter they will be maintained as a sort of moving average
2156 cpi->recent_ref_frame_usage[INTRA_FRAME] = 1;
2157 cpi->recent_ref_frame_usage[LAST_FRAME] = 1;
2158 cpi->recent_ref_frame_usage[GOLDEN_FRAME] = 1;
2159 cpi->recent_ref_frame_usage[ALTREF_FRAME] = 1;
2161 // Set reference frame sign bias for ALTREF frame to 1 (for now)
2162 cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = 1;
2164 cpi->gf_decay_rate = 0;
2165 cpi->baseline_gf_interval = DEFAULT_GF_INTERVAL;
2167 cpi->gold_is_last = 0 ;
2168 cpi->alt_is_last = 0 ;
2169 cpi->gold_is_alt = 0 ;
2171 // allocate memory for storing last frame's MVs for MV prediction.
2172 CHECK_MEM_ERROR(cpi->lfmv, vpx_calloc((cpi->common.mb_rows+2) * (cpi->common.mb_cols+2), sizeof(int_mv)));
2173 CHECK_MEM_ERROR(cpi->lf_ref_frame_sign_bias, vpx_calloc((cpi->common.mb_rows+2) * (cpi->common.mb_cols+2), sizeof(int)));
2174 CHECK_MEM_ERROR(cpi->lf_ref_frame, vpx_calloc((cpi->common.mb_rows+2) * (cpi->common.mb_cols+2), sizeof(int)));
2176 // Create the encoder segmentation map and set all entries to 0
2177 CHECK_MEM_ERROR(cpi->segmentation_map, vpx_calloc(cpi->common.mb_rows * cpi->common.mb_cols, 1));
2178 CHECK_MEM_ERROR(cpi->active_map, vpx_calloc(cpi->common.mb_rows * cpi->common.mb_cols, 1));
2179 vpx_memset(cpi->active_map , 1, (cpi->common.mb_rows * cpi->common.mb_cols));
2180 cpi->active_map_enabled = 0;
2182 // Create the first pass motion map structure and set to 0
2183 // Allocate space for maximum of 15 buffers
2184 CHECK_MEM_ERROR(cpi->fp_motion_map, vpx_calloc(15*cpi->common.MBs, 1));
2186 #if 0
2187 // Experimental code for lagged and one pass
2188 // Initialise one_pass GF frames stats
2189 // Update stats used for GF selection
2190 if (cpi->pass == 0)
2192 cpi->one_pass_frame_index = 0;
2194 for (i = 0; i < MAX_LAG_BUFFERS; i++)
2196 cpi->one_pass_frame_stats[i].frames_so_far = 0;
2197 cpi->one_pass_frame_stats[i].frame_intra_error = 0.0;
2198 cpi->one_pass_frame_stats[i].frame_coded_error = 0.0;
2199 cpi->one_pass_frame_stats[i].frame_pcnt_inter = 0.0;
2200 cpi->one_pass_frame_stats[i].frame_pcnt_motion = 0.0;
2201 cpi->one_pass_frame_stats[i].frame_mvr = 0.0;
2202 cpi->one_pass_frame_stats[i].frame_mvr_abs = 0.0;
2203 cpi->one_pass_frame_stats[i].frame_mvc = 0.0;
2204 cpi->one_pass_frame_stats[i].frame_mvc_abs = 0.0;
2207 #endif
2209 // Should we use the cyclic refresh method.
2210 // Currently this is tied to error resilliant mode
2211 cpi->cyclic_refresh_mode_enabled = cpi->oxcf.error_resilient_mode;
2212 cpi->cyclic_refresh_mode_max_mbs_perframe = (cpi->common.mb_rows * cpi->common.mb_cols) / 40;
2213 cpi->cyclic_refresh_mode_index = 0;
2214 cpi->cyclic_refresh_q = 32;
2216 if (cpi->cyclic_refresh_mode_enabled)
2218 CHECK_MEM_ERROR(cpi->cyclic_refresh_map, vpx_calloc((cpi->common.mb_rows * cpi->common.mb_cols), 1));
2220 else
2221 cpi->cyclic_refresh_map = (signed char *) NULL;
2223 // Test function for segmentation
2224 //segmentation_test_function((VP8_PTR) cpi);
2226 #ifdef ENTROPY_STATS
2227 init_context_counters();
2228 #endif
2230 /*Initialize the feed-forward activity masking.*/
2231 cpi->activity_avg = 90<<12;
2233 cpi->frames_since_key = 8; // Give a sensible default for the first frame.
2234 cpi->key_frame_frequency = cpi->oxcf.key_freq;
2235 cpi->this_key_frame_forced = FALSE;
2236 cpi->next_key_frame_forced = FALSE;
2238 cpi->source_alt_ref_pending = FALSE;
2239 cpi->source_alt_ref_active = FALSE;
2240 cpi->common.refresh_alt_ref_frame = 0;
2242 cpi->b_calculate_psnr = CONFIG_PSNR;
2243 #if CONFIG_PSNR
2244 cpi->b_calculate_ssimg = 0;
2246 cpi->count = 0;
2247 cpi->bytes = 0;
2249 if (cpi->b_calculate_psnr)
2251 cpi->total_sq_error = 0.0;
2252 cpi->total_sq_error2 = 0.0;
2253 cpi->total_y = 0.0;
2254 cpi->total_u = 0.0;
2255 cpi->total_v = 0.0;
2256 cpi->total = 0.0;
2257 cpi->totalp_y = 0.0;
2258 cpi->totalp_u = 0.0;
2259 cpi->totalp_v = 0.0;
2260 cpi->totalp = 0.0;
2261 cpi->tot_recode_hits = 0;
2262 cpi->summed_quality = 0;
2263 cpi->summed_weights = 0;
2266 if (cpi->b_calculate_ssimg)
2268 cpi->total_ssimg_y = 0;
2269 cpi->total_ssimg_u = 0;
2270 cpi->total_ssimg_v = 0;
2271 cpi->total_ssimg_all = 0;
2274 #ifndef LLONG_MAX
2275 #define LLONG_MAX 9223372036854775807LL
2276 #endif
2277 cpi->first_time_stamp_ever = LLONG_MAX;
2279 #endif
2281 cpi->frames_till_gf_update_due = 0;
2282 cpi->key_frame_count = 1;
2283 cpi->tot_key_frame_bits = 0;
2285 cpi->ni_av_qi = cpi->oxcf.worst_allowed_q;
2286 cpi->ni_tot_qi = 0;
2287 cpi->ni_frames = 0;
2288 cpi->total_byte_count = 0;
2290 cpi->drop_frame = 0;
2291 cpi->drop_count = 0;
2292 cpi->max_drop_count = 0;
2293 cpi->max_consec_dropped_frames = 4;
2295 cpi->rate_correction_factor = 1.0;
2296 cpi->key_frame_rate_correction_factor = 1.0;
2297 cpi->gf_rate_correction_factor = 1.0;
2298 cpi->est_max_qcorrection_factor = 1.0;
2300 cpi->mb.mvcost[0] = &cpi->mb.mvcosts[0][mv_max+1];
2301 cpi->mb.mvcost[1] = &cpi->mb.mvcosts[1][mv_max+1];
2302 cpi->mb.mvsadcost[0] = &cpi->mb.mvsadcosts[0][mv_max+1];
2303 cpi->mb.mvsadcost[1] = &cpi->mb.mvsadcosts[1][mv_max+1];
2305 cal_mvsadcosts(cpi->mb.mvsadcost);
2307 for (i = 0; i < KEY_FRAME_CONTEXT; i++)
2309 cpi->prior_key_frame_size[i] = cpi->intra_frame_target;
2310 cpi->prior_key_frame_distance[i] = (int)cpi->output_frame_rate;
2313 cpi->check_freq[0] = 15;
2314 cpi->check_freq[1] = 15;
2316 #ifdef OUTPUT_YUV_SRC
2317 yuv_file = fopen("bd.yuv", "ab");
2318 #endif
2320 #if 0
2321 framepsnr = fopen("framepsnr.stt", "a");
2322 kf_list = fopen("kf_list.stt", "w");
2323 #endif
2325 cpi->output_pkt_list = oxcf->output_pkt_list;
2327 #if !(CONFIG_REALTIME_ONLY)
2329 if (cpi->pass == 1)
2331 vp8_init_first_pass(cpi);
2333 else if (cpi->pass == 2)
2335 size_t packet_sz = vp8_firstpass_stats_sz(cpi->common.MBs);
2336 int packets = oxcf->two_pass_stats_in.sz / packet_sz;
2338 cpi->stats_in = oxcf->two_pass_stats_in.buf;
2339 cpi->stats_in_end = (void*)((char *)cpi->stats_in
2340 + (packets - 1) * packet_sz);
2341 vp8_init_second_pass(cpi);
2344 #endif
2346 if (cpi->compressor_speed == 2)
2348 cpi->cpu_freq = 0; //vp8_get_processor_freq();
2349 cpi->avg_encode_time = 0;
2350 cpi->avg_pick_mode_time = 0;
2353 vp8_set_speed_features(cpi);
2355 // Set starting values of RD threshold multipliers (128 = *1)
2356 for (i = 0; i < MAX_MODES; i++)
2358 cpi->rd_thresh_mult[i] = 128;
2361 #ifdef ENTROPY_STATS
2362 init_mv_ref_counts();
2363 #endif
2365 #if CONFIG_MULTITHREAD
2366 vp8cx_create_encoder_threads(cpi);
2367 #endif
2369 cpi->fn_ptr[BLOCK_16X16].sdf = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x16);
2370 cpi->fn_ptr[BLOCK_16X16].vf = VARIANCE_INVOKE(&cpi->rtcd.variance, var16x16);
2371 cpi->fn_ptr[BLOCK_16X16].svf = VARIANCE_INVOKE(&cpi->rtcd.variance, subpixvar16x16);
2372 cpi->fn_ptr[BLOCK_16X16].svf_halfpix_h = VARIANCE_INVOKE(&cpi->rtcd.variance, halfpixvar16x16_h);
2373 cpi->fn_ptr[BLOCK_16X16].svf_halfpix_v = VARIANCE_INVOKE(&cpi->rtcd.variance, halfpixvar16x16_v);
2374 cpi->fn_ptr[BLOCK_16X16].svf_halfpix_hv = VARIANCE_INVOKE(&cpi->rtcd.variance, halfpixvar16x16_hv);
2375 cpi->fn_ptr[BLOCK_16X16].sdx3f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x16x3);
2376 cpi->fn_ptr[BLOCK_16X16].sdx8f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x16x8);
2377 cpi->fn_ptr[BLOCK_16X16].sdx4df = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x16x4d);
2379 cpi->fn_ptr[BLOCK_16X8].sdf = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x8);
2380 cpi->fn_ptr[BLOCK_16X8].vf = VARIANCE_INVOKE(&cpi->rtcd.variance, var16x8);
2381 cpi->fn_ptr[BLOCK_16X8].svf = VARIANCE_INVOKE(&cpi->rtcd.variance, subpixvar16x8);
2382 cpi->fn_ptr[BLOCK_16X8].svf_halfpix_h = NULL;
2383 cpi->fn_ptr[BLOCK_16X8].svf_halfpix_v = NULL;
2384 cpi->fn_ptr[BLOCK_16X8].svf_halfpix_hv = NULL;
2385 cpi->fn_ptr[BLOCK_16X8].sdx3f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x8x3);
2386 cpi->fn_ptr[BLOCK_16X8].sdx8f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x8x8);
2387 cpi->fn_ptr[BLOCK_16X8].sdx4df = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x8x4d);
2389 cpi->fn_ptr[BLOCK_8X16].sdf = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x16);
2390 cpi->fn_ptr[BLOCK_8X16].vf = VARIANCE_INVOKE(&cpi->rtcd.variance, var8x16);
2391 cpi->fn_ptr[BLOCK_8X16].svf = VARIANCE_INVOKE(&cpi->rtcd.variance, subpixvar8x16);
2392 cpi->fn_ptr[BLOCK_8X16].svf_halfpix_h = NULL;
2393 cpi->fn_ptr[BLOCK_8X16].svf_halfpix_v = NULL;
2394 cpi->fn_ptr[BLOCK_8X16].svf_halfpix_hv = NULL;
2395 cpi->fn_ptr[BLOCK_8X16].sdx3f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x16x3);
2396 cpi->fn_ptr[BLOCK_8X16].sdx8f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x16x8);
2397 cpi->fn_ptr[BLOCK_8X16].sdx4df = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x16x4d);
2399 cpi->fn_ptr[BLOCK_8X8].sdf = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x8);
2400 cpi->fn_ptr[BLOCK_8X8].vf = VARIANCE_INVOKE(&cpi->rtcd.variance, var8x8);
2401 cpi->fn_ptr[BLOCK_8X8].svf = VARIANCE_INVOKE(&cpi->rtcd.variance, subpixvar8x8);
2402 cpi->fn_ptr[BLOCK_8X8].svf_halfpix_h = NULL;
2403 cpi->fn_ptr[BLOCK_8X8].svf_halfpix_v = NULL;
2404 cpi->fn_ptr[BLOCK_8X8].svf_halfpix_hv = NULL;
2405 cpi->fn_ptr[BLOCK_8X8].sdx3f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x8x3);
2406 cpi->fn_ptr[BLOCK_8X8].sdx8f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x8x8);
2407 cpi->fn_ptr[BLOCK_8X8].sdx4df = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x8x4d);
2409 cpi->fn_ptr[BLOCK_4X4].sdf = VARIANCE_INVOKE(&cpi->rtcd.variance, sad4x4);
2410 cpi->fn_ptr[BLOCK_4X4].vf = VARIANCE_INVOKE(&cpi->rtcd.variance, var4x4);
2411 cpi->fn_ptr[BLOCK_4X4].svf = VARIANCE_INVOKE(&cpi->rtcd.variance, subpixvar4x4);
2412 cpi->fn_ptr[BLOCK_4X4].svf_halfpix_h = NULL;
2413 cpi->fn_ptr[BLOCK_4X4].svf_halfpix_v = NULL;
2414 cpi->fn_ptr[BLOCK_4X4].svf_halfpix_hv = NULL;
2415 cpi->fn_ptr[BLOCK_4X4].sdx3f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad4x4x3);
2416 cpi->fn_ptr[BLOCK_4X4].sdx8f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad4x4x8);
2417 cpi->fn_ptr[BLOCK_4X4].sdx4df = VARIANCE_INVOKE(&cpi->rtcd.variance, sad4x4x4d);
2419 #if !(CONFIG_REALTIME_ONLY)
2420 cpi->full_search_sad = SEARCH_INVOKE(&cpi->rtcd.search, full_search);
2421 #endif
2422 cpi->diamond_search_sad = SEARCH_INVOKE(&cpi->rtcd.search, diamond_search);
2424 cpi->ready_for_new_frame = 1;
2426 cpi->source_encode_index = 0;
2428 // make sure frame 1 is okay
2429 cpi->error_bins[0] = cpi->common.MBs;
2431 //vp8cx_init_quantizer() is first called here. Add check in vp8cx_frame_init_quantizer() so that vp8cx_init_quantizer is only called later
2432 //when needed. This will avoid unnecessary calls of vp8cx_init_quantizer() for every frame.
2433 vp8cx_init_quantizer(cpi);
2435 vp8_init_loop_filter(cm);
2436 cm->last_frame_type = KEY_FRAME;
2437 cm->last_filter_type = cm->filter_type;
2438 cm->last_sharpness_level = cm->sharpness_level;
2440 cpi->common.error.setjmp = 0;
2441 return (VP8_PTR) cpi;
2446 void vp8_remove_compressor(VP8_PTR *ptr)
2448 VP8_COMP *cpi = (VP8_COMP *)(*ptr);
2450 if (!cpi)
2451 return;
2453 if (cpi && (cpi->common.current_video_frame > 0))
2455 #if !(CONFIG_REALTIME_ONLY)
2457 if (cpi->pass == 2)
2459 vp8_end_second_pass(cpi);
2462 #endif
2464 #ifdef ENTROPY_STATS
2465 print_context_counters();
2466 print_tree_update_probs();
2467 print_mode_context();
2468 #endif
2470 #if CONFIG_PSNR
2472 if (cpi->pass != 1)
2474 FILE *f = fopen("opsnr.stt", "a");
2475 double time_encoded = (cpi->source_end_time_stamp - cpi->first_time_stamp_ever) / 10000000.000;
2476 double total_encode_time = (cpi->time_receive_data + cpi->time_compress_data) / 1000.000;
2477 double dr = (double)cpi->bytes * (double) 8 / (double)1000 / time_encoded;
2479 if (cpi->b_calculate_psnr)
2481 YV12_BUFFER_CONFIG *lst_yv12 = &cpi->common.yv12_fb[cpi->common.lst_fb_idx];
2482 double samples = 3.0 / 2 * cpi->count * lst_yv12->y_width * lst_yv12->y_height;
2483 double total_psnr = vp8_mse2psnr(samples, 255.0, cpi->total_sq_error);
2484 double total_psnr2 = vp8_mse2psnr(samples, 255.0, cpi->total_sq_error2);
2485 double total_ssim = 100 * pow(cpi->summed_quality / cpi->summed_weights, 8.0);
2487 fprintf(f, "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\tVPXSSIM\t Time(us)\n");
2488 fprintf(f, "%7.3f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t%7.3f %8.0f\n",
2489 dr, cpi->total / cpi->count, total_psnr, cpi->totalp / cpi->count, total_psnr2, total_ssim,
2490 total_encode_time);
2493 if (cpi->b_calculate_ssimg)
2495 fprintf(f, "BitRate\tSSIM_Y\tSSIM_U\tSSIM_V\tSSIM_A\t Time(us)\n");
2496 fprintf(f, "%7.3f\t%6.4f\t%6.4f\t%6.4f\t%6.4f\t%8.0f\n", dr,
2497 cpi->total_ssimg_y / cpi->count, cpi->total_ssimg_u / cpi->count,
2498 cpi->total_ssimg_v / cpi->count, cpi->total_ssimg_all / cpi->count, total_encode_time);
2501 fclose(f);
2502 #if 0
2503 f = fopen("qskip.stt", "a");
2504 fprintf(f, "minq:%d -maxq:%d skipture:skipfalse = %d:%d\n", cpi->oxcf.best_allowed_q, cpi->oxcf.worst_allowed_q, skiptruecount, skipfalsecount);
2505 fclose(f);
2506 #endif
2510 #endif
2513 #ifdef SPEEDSTATS
2515 if (cpi->compressor_speed == 2)
2517 int i;
2518 FILE *f = fopen("cxspeed.stt", "a");
2519 cnt_pm /= cpi->common.MBs;
2521 for (i = 0; i < 16; i++)
2522 fprintf(f, "%5d", frames_at_speed[i]);
2524 fprintf(f, "\n");
2525 //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);
2526 fclose(f);
2529 #endif
2532 #ifdef MODE_STATS
2534 extern int count_mb_seg[4];
2535 FILE *f = fopen("modes.stt", "a");
2536 double dr = (double)cpi->oxcf.frame_rate * (double)bytes * (double)8 / (double)count / (double)1000 ;
2537 fprintf(f, "intra_mode in Intra Frames:\n");
2538 fprintf(f, "Y: %8d, %8d, %8d, %8d, %8d\n", y_modes[0], y_modes[1], y_modes[2], y_modes[3], y_modes[4]);
2539 fprintf(f, "UV:%8d, %8d, %8d, %8d\n", uv_modes[0], uv_modes[1], uv_modes[2], uv_modes[3]);
2540 fprintf(f, "B: ");
2542 int i;
2544 for (i = 0; i < 10; i++)
2545 fprintf(f, "%8d, ", b_modes[i]);
2547 fprintf(f, "\n");
2551 fprintf(f, "Modes in Inter Frames:\n");
2552 fprintf(f, "Y: %8d, %8d, %8d, %8d, %8d, %8d, %8d, %8d, %8d, %8d\n",
2553 inter_y_modes[0], inter_y_modes[1], inter_y_modes[2], inter_y_modes[3], inter_y_modes[4],
2554 inter_y_modes[5], inter_y_modes[6], inter_y_modes[7], inter_y_modes[8], inter_y_modes[9]);
2555 fprintf(f, "UV:%8d, %8d, %8d, %8d\n", inter_uv_modes[0], inter_uv_modes[1], inter_uv_modes[2], inter_uv_modes[3]);
2556 fprintf(f, "B: ");
2558 int i;
2560 for (i = 0; i < 15; i++)
2561 fprintf(f, "%8d, ", inter_b_modes[i]);
2563 fprintf(f, "\n");
2566 fprintf(f, "P:%8d, %8d, %8d, %8d\n", count_mb_seg[0], count_mb_seg[1], count_mb_seg[2], count_mb_seg[3]);
2567 fprintf(f, "PB:%8d, %8d, %8d, %8d\n", inter_b_modes[LEFT4X4], inter_b_modes[ABOVE4X4], inter_b_modes[ZERO4X4], inter_b_modes[NEW4X4]);
2571 fclose(f);
2573 #endif
2575 #ifdef ENTROPY_STATS
2577 int i, j, k;
2578 FILE *fmode = fopen("modecontext.c", "w");
2580 fprintf(fmode, "\n#include \"entropymode.h\"\n\n");
2581 fprintf(fmode, "const unsigned int vp8_kf_default_bmode_counts ");
2582 fprintf(fmode, "[VP8_BINTRAMODES] [VP8_BINTRAMODES] [VP8_BINTRAMODES] =\n{\n");
2584 for (i = 0; i < 10; i++)
2587 fprintf(fmode, " { //Above Mode : %d\n", i);
2589 for (j = 0; j < 10; j++)
2592 fprintf(fmode, " {");
2594 for (k = 0; k < 10; k++)
2596 if (!intra_mode_stats[i][j][k])
2597 fprintf(fmode, " %5d, ", 1);
2598 else
2599 fprintf(fmode, " %5d, ", intra_mode_stats[i][j][k]);
2602 fprintf(fmode, "}, // left_mode %d\n", j);
2606 fprintf(fmode, " },\n");
2610 fprintf(fmode, "};\n");
2611 fclose(fmode);
2613 #endif
2616 #if defined(SECTIONBITS_OUTPUT)
2618 if (0)
2620 int i;
2621 FILE *f = fopen("tokenbits.stt", "a");
2623 for (i = 0; i < 28; i++)
2624 fprintf(f, "%8d", (int)(Sectionbits[i] / 256));
2626 fprintf(f, "\n");
2627 fclose(f);
2630 #endif
2632 #if 0
2634 printf("\n_pick_loop_filter_level:%d\n", cpi->time_pick_lpf / 1000);
2635 printf("\n_frames recive_data encod_mb_row compress_frame Total\n");
2636 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);
2638 #endif
2642 #if CONFIG_MULTITHREAD
2643 vp8cx_remove_encoder_threads(cpi);
2644 #endif
2646 vp8_dealloc_compressor_data(cpi);
2647 vpx_free(cpi->mb.ss);
2648 vpx_free(cpi->tok);
2649 vpx_free(cpi->cyclic_refresh_map);
2651 vp8_remove_common(&cpi->common);
2652 vpx_free(cpi);
2653 *ptr = 0;
2655 #ifdef OUTPUT_YUV_SRC
2656 fclose(yuv_file);
2657 #endif
2659 #if 0
2661 if (keyfile)
2662 fclose(keyfile);
2664 if (framepsnr)
2665 fclose(framepsnr);
2667 if (kf_list)
2668 fclose(kf_list);
2670 #endif
2675 static uint64_t calc_plane_error(unsigned char *orig, int orig_stride,
2676 unsigned char *recon, int recon_stride,
2677 unsigned int cols, unsigned int rows,
2678 vp8_variance_rtcd_vtable_t *rtcd)
2680 unsigned int row, col;
2681 uint64_t total_sse = 0;
2682 int diff;
2684 for (row = 0; row + 16 <= rows; row += 16)
2686 for (col = 0; col + 16 <= cols; col += 16)
2688 unsigned int sse;
2690 VARIANCE_INVOKE(rtcd, mse16x16)(orig + col, orig_stride,
2691 recon + col, recon_stride,
2692 &sse);
2693 total_sse += sse;
2696 /* Handle odd-sized width */
2697 if (col < cols)
2699 unsigned int border_row, border_col;
2700 unsigned char *border_orig = orig;
2701 unsigned char *border_recon = recon;
2703 for (border_row = 0; border_row < 16; border_row++)
2705 for (border_col = col; border_col < cols; border_col++)
2707 diff = border_orig[border_col] - border_recon[border_col];
2708 total_sse += diff * diff;
2711 border_orig += orig_stride;
2712 border_recon += recon_stride;
2716 orig += orig_stride * 16;
2717 recon += recon_stride * 16;
2720 /* Handle odd-sized height */
2721 for (; row < rows; row++)
2723 for (col = 0; col < cols; col++)
2725 diff = orig[col] - recon[col];
2726 total_sse += diff * diff;
2729 orig += orig_stride;
2730 recon += recon_stride;
2733 return total_sse;
2737 static void generate_psnr_packet(VP8_COMP *cpi)
2739 YV12_BUFFER_CONFIG *orig = cpi->Source;
2740 YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
2741 struct vpx_codec_cx_pkt pkt;
2742 uint64_t sse;
2743 int i;
2744 unsigned int width = cpi->common.Width;
2745 unsigned int height = cpi->common.Height;
2747 pkt.kind = VPX_CODEC_PSNR_PKT;
2748 sse = calc_plane_error(orig->y_buffer, orig->y_stride,
2749 recon->y_buffer, recon->y_stride,
2750 width, height,
2751 IF_RTCD(&cpi->rtcd.variance));
2752 pkt.data.psnr.sse[0] = sse;
2753 pkt.data.psnr.sse[1] = sse;
2754 pkt.data.psnr.samples[0] = width * height;
2755 pkt.data.psnr.samples[1] = width * height;
2757 width = (width + 1) / 2;
2758 height = (height + 1) / 2;
2760 sse = calc_plane_error(orig->u_buffer, orig->uv_stride,
2761 recon->u_buffer, recon->uv_stride,
2762 width, height,
2763 IF_RTCD(&cpi->rtcd.variance));
2764 pkt.data.psnr.sse[0] += sse;
2765 pkt.data.psnr.sse[2] = sse;
2766 pkt.data.psnr.samples[0] += width * height;
2767 pkt.data.psnr.samples[2] = width * height;
2769 sse = calc_plane_error(orig->v_buffer, orig->uv_stride,
2770 recon->v_buffer, recon->uv_stride,
2771 width, height,
2772 IF_RTCD(&cpi->rtcd.variance));
2773 pkt.data.psnr.sse[0] += sse;
2774 pkt.data.psnr.sse[3] = sse;
2775 pkt.data.psnr.samples[0] += width * height;
2776 pkt.data.psnr.samples[3] = width * height;
2778 for (i = 0; i < 4; i++)
2779 pkt.data.psnr.psnr[i] = vp8_mse2psnr(pkt.data.psnr.samples[i], 255.0,
2780 pkt.data.psnr.sse[i]);
2782 vpx_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
2786 int vp8_use_as_reference(VP8_PTR ptr, int ref_frame_flags)
2788 VP8_COMP *cpi = (VP8_COMP *)(ptr);
2790 if (ref_frame_flags > 7)
2791 return -1 ;
2793 cpi->ref_frame_flags = ref_frame_flags;
2794 return 0;
2796 int vp8_update_reference(VP8_PTR ptr, int ref_frame_flags)
2798 VP8_COMP *cpi = (VP8_COMP *)(ptr);
2800 if (ref_frame_flags > 7)
2801 return -1 ;
2803 cpi->common.refresh_golden_frame = 0;
2804 cpi->common.refresh_alt_ref_frame = 0;
2805 cpi->common.refresh_last_frame = 0;
2807 if (ref_frame_flags & VP8_LAST_FLAG)
2808 cpi->common.refresh_last_frame = 1;
2810 if (ref_frame_flags & VP8_GOLD_FLAG)
2811 cpi->common.refresh_golden_frame = 1;
2813 if (ref_frame_flags & VP8_ALT_FLAG)
2814 cpi->common.refresh_alt_ref_frame = 1;
2816 return 0;
2819 int vp8_get_reference(VP8_PTR ptr, VP8_REFFRAME ref_frame_flag, YV12_BUFFER_CONFIG *sd)
2821 VP8_COMP *cpi = (VP8_COMP *)(ptr);
2822 VP8_COMMON *cm = &cpi->common;
2823 int ref_fb_idx;
2825 if (ref_frame_flag == VP8_LAST_FLAG)
2826 ref_fb_idx = cm->lst_fb_idx;
2827 else if (ref_frame_flag == VP8_GOLD_FLAG)
2828 ref_fb_idx = cm->gld_fb_idx;
2829 else if (ref_frame_flag == VP8_ALT_FLAG)
2830 ref_fb_idx = cm->alt_fb_idx;
2831 else
2832 return -1;
2834 vp8_yv12_copy_frame_ptr(&cm->yv12_fb[ref_fb_idx], sd);
2836 return 0;
2838 int vp8_set_reference(VP8_PTR ptr, VP8_REFFRAME ref_frame_flag, YV12_BUFFER_CONFIG *sd)
2840 VP8_COMP *cpi = (VP8_COMP *)(ptr);
2841 VP8_COMMON *cm = &cpi->common;
2843 int ref_fb_idx;
2845 if (ref_frame_flag == VP8_LAST_FLAG)
2846 ref_fb_idx = cm->lst_fb_idx;
2847 else if (ref_frame_flag == VP8_GOLD_FLAG)
2848 ref_fb_idx = cm->gld_fb_idx;
2849 else if (ref_frame_flag == VP8_ALT_FLAG)
2850 ref_fb_idx = cm->alt_fb_idx;
2851 else
2852 return -1;
2854 vp8_yv12_copy_frame_ptr(sd, &cm->yv12_fb[ref_fb_idx]);
2856 return 0;
2858 int vp8_update_entropy(VP8_PTR comp, int update)
2860 VP8_COMP *cpi = (VP8_COMP *) comp;
2861 VP8_COMMON *cm = &cpi->common;
2862 cm->refresh_entropy_probs = update;
2864 return 0;
2868 #if OUTPUT_YUV_SRC
2869 void vp8_write_yuv_frame(const char *name, YV12_BUFFER_CONFIG *s)
2871 FILE *yuv_file = fopen(name, "ab");
2872 unsigned char *src = s->y_buffer;
2873 int h = s->y_height;
2877 fwrite(src, s->y_width, 1, yuv_file);
2878 src += s->y_stride;
2880 while (--h);
2882 src = s->u_buffer;
2883 h = s->uv_height;
2887 fwrite(src, s->uv_width, 1, yuv_file);
2888 src += s->uv_stride;
2890 while (--h);
2892 src = s->v_buffer;
2893 h = s->uv_height;
2897 fwrite(src, s->uv_width, 1, yuv_file);
2898 src += s->uv_stride;
2900 while (--h);
2902 fclose(yuv_file);
2904 #endif
2907 static void scale_and_extend_source(YV12_BUFFER_CONFIG *sd, VP8_COMP *cpi)
2909 VP8_COMMON *cm = &cpi->common;
2911 // are we resizing the image
2912 if (cm->horiz_scale != 0 || cm->vert_scale != 0)
2914 #if CONFIG_SPATIAL_RESAMPLING
2915 int UNINITIALIZED_IS_SAFE(hr), UNINITIALIZED_IS_SAFE(hs);
2916 int UNINITIALIZED_IS_SAFE(vr), UNINITIALIZED_IS_SAFE(vs);
2917 int tmp_height;
2919 if (cm->vert_scale == 3)
2920 tmp_height = 9;
2921 else
2922 tmp_height = 11;
2924 Scale2Ratio(cm->horiz_scale, &hr, &hs);
2925 Scale2Ratio(cm->vert_scale, &vr, &vs);
2927 vp8_scale_frame(sd, &cpi->scaled_source, cm->temp_scale_frame.y_buffer,
2928 tmp_height, hs, hr, vs, vr, 0);
2930 cpi->Source = &cpi->scaled_source;
2931 #endif
2933 // we may need to copy to a buffer so we can extend the image...
2934 else if (cm->Width != cm->yv12_fb[cm->lst_fb_idx].y_width ||
2935 cm->Height != cm->yv12_fb[cm->lst_fb_idx].y_height)
2937 //vp8_yv12_copy_frame_ptr(sd, &cpi->scaled_source);
2938 #if HAVE_ARMV7
2939 #if CONFIG_RUNTIME_CPU_DETECT
2940 if (cm->rtcd.flags & HAS_NEON)
2941 #endif
2943 vp8_yv12_copy_src_frame_func_neon(sd, &cpi->scaled_source);
2945 #if CONFIG_RUNTIME_CPU_DETECT
2946 else
2947 #endif
2948 #endif
2949 #if !HAVE_ARMV7 || CONFIG_RUNTIME_CPU_DETECT
2951 vp8_yv12_copy_frame_ptr(sd, &cpi->scaled_source);
2953 #endif
2955 cpi->Source = &cpi->scaled_source;
2958 vp8_extend_to_multiple_of16(cpi->Source, cm->Width, cm->Height);
2961 static void resize_key_frame(VP8_COMP *cpi)
2963 #if CONFIG_SPATIAL_RESAMPLING
2964 VP8_COMMON *cm = &cpi->common;
2966 // Do we need to apply resampling for one pass cbr.
2967 // In one pass this is more limited than in two pass cbr
2968 // The test and any change is only made one per key frame sequence
2969 if (cpi->oxcf.allow_spatial_resampling && (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER))
2971 int UNINITIALIZED_IS_SAFE(hr), UNINITIALIZED_IS_SAFE(hs);
2972 int UNINITIALIZED_IS_SAFE(vr), UNINITIALIZED_IS_SAFE(vs);
2973 int new_width, new_height;
2975 // If we are below the resample DOWN watermark then scale down a notch.
2976 if (cpi->buffer_level < (cpi->oxcf.resample_down_water_mark * cpi->oxcf.optimal_buffer_level / 100))
2978 cm->horiz_scale = (cm->horiz_scale < ONETWO) ? cm->horiz_scale + 1 : ONETWO;
2979 cm->vert_scale = (cm->vert_scale < ONETWO) ? cm->vert_scale + 1 : ONETWO;
2981 // Should we now start scaling back up
2982 else if (cpi->buffer_level > (cpi->oxcf.resample_up_water_mark * cpi->oxcf.optimal_buffer_level / 100))
2984 cm->horiz_scale = (cm->horiz_scale > NORMAL) ? cm->horiz_scale - 1 : NORMAL;
2985 cm->vert_scale = (cm->vert_scale > NORMAL) ? cm->vert_scale - 1 : NORMAL;
2988 // Get the new hieght and width
2989 Scale2Ratio(cm->horiz_scale, &hr, &hs);
2990 Scale2Ratio(cm->vert_scale, &vr, &vs);
2991 new_width = ((hs - 1) + (cpi->oxcf.Width * hr)) / hs;
2992 new_height = ((vs - 1) + (cpi->oxcf.Height * vr)) / vs;
2994 // If the image size has changed we need to reallocate the buffers
2995 // and resample the source image
2996 if ((cm->Width != new_width) || (cm->Height != new_height))
2998 cm->Width = new_width;
2999 cm->Height = new_height;
3000 vp8_alloc_compressor_data(cpi);
3001 scale_and_extend_source(cpi->un_scaled_source, cpi);
3005 #endif
3007 // return of 0 means drop frame
3008 static int pick_frame_size(VP8_COMP *cpi)
3010 VP8_COMMON *cm = &cpi->common;
3012 // First Frame is a special case
3013 if (cm->current_video_frame == 0)
3015 #if !(CONFIG_REALTIME_ONLY)
3017 if (cpi->pass == 2)
3018 vp8_calc_auto_iframe_target_size(cpi);
3020 // 1 Pass there is no information on which to base size so use bandwidth per second * fixed fraction
3021 else
3022 #endif
3023 cpi->this_frame_target = cpi->oxcf.target_bandwidth / 2;
3025 // in error resilient mode the first frame is bigger since it likely contains
3026 // all the static background
3027 if (cpi->oxcf.error_resilient_mode == 1 || (cpi->compressor_speed == 2))
3029 cpi->this_frame_target *= 3; // 5;
3032 // Key frame from VFW/auto-keyframe/first frame
3033 cm->frame_type = KEY_FRAME;
3036 // Special case for forced key frames
3037 // The frame sizing here is still far from ideal for 2 pass.
3038 else if (cm->frame_flags & FRAMEFLAGS_KEY)
3040 cm->frame_type = KEY_FRAME;
3041 resize_key_frame(cpi);
3042 vp8_calc_iframe_target_size(cpi);
3044 else if (cm->frame_type == KEY_FRAME)
3046 vp8_calc_auto_iframe_target_size(cpi);
3048 else
3050 // INTER frame: compute target frame size
3051 cm->frame_type = INTER_FRAME;
3052 vp8_calc_pframe_target_size(cpi);
3054 // Check if we're dropping the frame:
3055 if (cpi->drop_frame)
3057 cpi->drop_frame = FALSE;
3058 cpi->drop_count++;
3059 return 0;
3063 return 1;
3066 static void set_quantizer(VP8_COMP *cpi, int Q)
3068 VP8_COMMON *cm = &cpi->common;
3069 MACROBLOCKD *mbd = &cpi->mb.e_mbd;
3070 int update = 0;
3072 update |= cm->base_qindex != Q;
3073 cm->base_qindex = Q;
3075 cm->y1dc_delta_q = 0;
3076 cm->y2dc_delta_q = 0;
3077 cm->y2ac_delta_q = 0;
3078 cm->uvdc_delta_q = 0;
3079 cm->uvac_delta_q = 0;
3081 if(Q<4)
3083 update |= cm->y2dc_delta_q != 4-Q;
3084 cm->y2dc_delta_q = 4-Q;
3087 // Set Segment specific quatizers
3088 mbd->segment_feature_data[MB_LVL_ALT_Q][0] = cpi->segment_feature_data[MB_LVL_ALT_Q][0];
3089 mbd->segment_feature_data[MB_LVL_ALT_Q][1] = cpi->segment_feature_data[MB_LVL_ALT_Q][1];
3090 mbd->segment_feature_data[MB_LVL_ALT_Q][2] = cpi->segment_feature_data[MB_LVL_ALT_Q][2];
3091 mbd->segment_feature_data[MB_LVL_ALT_Q][3] = cpi->segment_feature_data[MB_LVL_ALT_Q][3];
3093 if(update)
3094 vp8cx_init_quantizer(cpi);
3098 static void update_alt_ref_frame_and_stats(VP8_COMP *cpi)
3100 VP8_COMMON *cm = &cpi->common;
3102 // Update the golden frame buffer
3103 vp8_yv12_copy_frame_ptr(cm->frame_to_show, &cm->yv12_fb[cm->alt_fb_idx]);
3105 // Select an interval before next GF or altref
3106 if (!cpi->auto_gold)
3107 cpi->frames_till_gf_update_due = cpi->goldfreq;
3109 if ((cpi->pass != 2) && cpi->frames_till_gf_update_due)
3111 cpi->current_gf_interval = cpi->frames_till_gf_update_due;
3113 // Set the bits per frame that we should try and recover in subsequent inter frames
3114 // to account for the extra GF spend... note that his does not apply for GF updates
3115 // that occur coincident with a key frame as the extra cost of key frames is dealt
3116 // with elsewhere.
3118 cpi->gf_overspend_bits += cpi->projected_frame_size;
3119 cpi->non_gf_bitrate_adjustment = cpi->gf_overspend_bits / cpi->frames_till_gf_update_due;
3122 // Update data structure that monitors level of reference to last GF
3123 vpx_memset(cpi->gf_active_flags, 1, (cm->mb_rows * cm->mb_cols));
3124 cpi->gf_active_count = cm->mb_rows * cm->mb_cols;
3126 // this frame refreshes means next frames don't unless specified by user
3127 cpi->common.frames_since_golden = 0;
3129 // Clear the alternate reference update pending flag.
3130 cpi->source_alt_ref_pending = FALSE;
3132 // Set the alternate refernce frame active flag
3133 cpi->source_alt_ref_active = TRUE;
3137 static void update_golden_frame_and_stats(VP8_COMP *cpi)
3139 VP8_COMMON *cm = &cpi->common;
3141 // Update the Golden frame reconstruction buffer if signalled and the GF usage counts.
3142 if (cm->refresh_golden_frame)
3144 // Update the golden frame buffer
3145 vp8_yv12_copy_frame_ptr(cm->frame_to_show, &cm->yv12_fb[cm->gld_fb_idx]);
3147 // Select an interval before next GF
3148 if (!cpi->auto_gold)
3149 cpi->frames_till_gf_update_due = cpi->goldfreq;
3151 if ((cpi->pass != 2) && (cpi->frames_till_gf_update_due > 0))
3153 cpi->current_gf_interval = cpi->frames_till_gf_update_due;
3155 // Set the bits per frame that we should try and recover in subsequent inter frames
3156 // to account for the extra GF spend... note that his does not apply for GF updates
3157 // that occur coincident with a key frame as the extra cost of key frames is dealt
3158 // with elsewhere.
3159 if ((cm->frame_type != KEY_FRAME) && !cpi->source_alt_ref_active)
3161 // Calcluate GF bits to be recovered
3162 // Projected size - av frame bits available for inter frames for clip as a whole
3163 cpi->gf_overspend_bits += (cpi->projected_frame_size - cpi->inter_frame_target);
3166 cpi->non_gf_bitrate_adjustment = cpi->gf_overspend_bits / cpi->frames_till_gf_update_due;
3170 // Update data structure that monitors level of reference to last GF
3171 vpx_memset(cpi->gf_active_flags, 1, (cm->mb_rows * cm->mb_cols));
3172 cpi->gf_active_count = cm->mb_rows * cm->mb_cols;
3174 // this frame refreshes means next frames don't unless specified by user
3175 cm->refresh_golden_frame = 0;
3176 cpi->common.frames_since_golden = 0;
3178 //if ( cm->frame_type == KEY_FRAME )
3180 cpi->recent_ref_frame_usage[INTRA_FRAME] = 1;
3181 cpi->recent_ref_frame_usage[LAST_FRAME] = 1;
3182 cpi->recent_ref_frame_usage[GOLDEN_FRAME] = 1;
3183 cpi->recent_ref_frame_usage[ALTREF_FRAME] = 1;
3185 //else
3187 // // Carry a potrtion of count over to begining of next gf sequence
3188 // cpi->recent_ref_frame_usage[INTRA_FRAME] >>= 5;
3189 // cpi->recent_ref_frame_usage[LAST_FRAME] >>= 5;
3190 // cpi->recent_ref_frame_usage[GOLDEN_FRAME] >>= 5;
3191 // cpi->recent_ref_frame_usage[ALTREF_FRAME] >>= 5;
3194 // ******** Fixed Q test code only ************
3195 // If we are going to use the ALT reference for the next group of frames set a flag to say so.
3196 if (cpi->oxcf.fixed_q >= 0 &&
3197 cpi->oxcf.play_alternate && !cpi->common.refresh_alt_ref_frame)
3199 cpi->source_alt_ref_pending = TRUE;
3200 cpi->frames_till_gf_update_due = cpi->baseline_gf_interval;
3203 if (!cpi->source_alt_ref_pending)
3204 cpi->source_alt_ref_active = FALSE;
3206 // Decrement count down till next gf
3207 if (cpi->frames_till_gf_update_due > 0)
3208 cpi->frames_till_gf_update_due--;
3211 else if (!cpi->common.refresh_alt_ref_frame)
3213 // Decrement count down till next gf
3214 if (cpi->frames_till_gf_update_due > 0)
3215 cpi->frames_till_gf_update_due--;
3217 if (cpi->common.frames_till_alt_ref_frame)
3218 cpi->common.frames_till_alt_ref_frame --;
3220 cpi->common.frames_since_golden ++;
3222 if (cpi->common.frames_since_golden > 1)
3224 cpi->recent_ref_frame_usage[INTRA_FRAME] += cpi->count_mb_ref_frame_usage[INTRA_FRAME];
3225 cpi->recent_ref_frame_usage[LAST_FRAME] += cpi->count_mb_ref_frame_usage[LAST_FRAME];
3226 cpi->recent_ref_frame_usage[GOLDEN_FRAME] += cpi->count_mb_ref_frame_usage[GOLDEN_FRAME];
3227 cpi->recent_ref_frame_usage[ALTREF_FRAME] += cpi->count_mb_ref_frame_usage[ALTREF_FRAME];
3232 // This function updates the reference frame probability estimates that
3233 // will be used during mode selection
3234 static void update_rd_ref_frame_probs(VP8_COMP *cpi)
3236 VP8_COMMON *cm = &cpi->common;
3238 #if 0
3239 const int *const rfct = cpi->recent_ref_frame_usage;
3240 const int rf_intra = rfct[INTRA_FRAME];
3241 const int rf_inter = rfct[LAST_FRAME] + rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME];
3243 if (cm->frame_type == KEY_FRAME)
3245 cpi->prob_intra_coded = 255;
3246 cpi->prob_last_coded = 128;
3247 cpi->prob_gf_coded = 128;
3249 else if (!(rf_intra + rf_inter))
3251 // This is a trap in case this function is called with cpi->recent_ref_frame_usage[] blank.
3252 cpi->prob_intra_coded = 63;
3253 cpi->prob_last_coded = 128;
3254 cpi->prob_gf_coded = 128;
3256 else
3258 cpi->prob_intra_coded = (rf_intra * 255) / (rf_intra + rf_inter);
3260 if (cpi->prob_intra_coded < 1)
3261 cpi->prob_intra_coded = 1;
3263 if ((cm->frames_since_golden > 0) || cpi->source_alt_ref_active)
3265 cpi->prob_last_coded = rf_inter ? (rfct[LAST_FRAME] * 255) / rf_inter : 128;
3267 if (cpi->prob_last_coded < 1)
3268 cpi->prob_last_coded = 1;
3270 cpi->prob_gf_coded = (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME])
3271 ? (rfct[GOLDEN_FRAME] * 255) / (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME]) : 128;
3273 if (cpi->prob_gf_coded < 1)
3274 cpi->prob_gf_coded = 1;
3278 #else
3279 const int *const rfct = cpi->count_mb_ref_frame_usage;
3280 const int rf_intra = rfct[INTRA_FRAME];
3281 const int rf_inter = rfct[LAST_FRAME] + rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME];
3283 if (cm->frame_type == KEY_FRAME)
3285 cpi->prob_intra_coded = 255;
3286 cpi->prob_last_coded = 128;
3287 cpi->prob_gf_coded = 128;
3289 else if (!(rf_intra + rf_inter))
3291 // This is a trap in case this function is called with cpi->recent_ref_frame_usage[] blank.
3292 cpi->prob_intra_coded = 63;
3293 cpi->prob_last_coded = 128;
3294 cpi->prob_gf_coded = 128;
3296 else
3298 cpi->prob_intra_coded = (rf_intra * 255) / (rf_intra + rf_inter);
3300 if (cpi->prob_intra_coded < 1)
3301 cpi->prob_intra_coded = 1;
3303 cpi->prob_last_coded = rf_inter ? (rfct[LAST_FRAME] * 255) / rf_inter : 128;
3305 if (cpi->prob_last_coded < 1)
3306 cpi->prob_last_coded = 1;
3308 cpi->prob_gf_coded = (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME])
3309 ? (rfct[GOLDEN_FRAME] * 255) / (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME]) : 128;
3311 if (cpi->prob_gf_coded < 1)
3312 cpi->prob_gf_coded = 1;
3315 // update reference frame costs since we can do better than what we got last frame.
3317 if (cpi->common.refresh_alt_ref_frame)
3319 cpi->prob_intra_coded += 40;
3320 cpi->prob_last_coded = 200;
3321 cpi->prob_gf_coded = 1;
3323 else if (cpi->common.frames_since_golden == 0)
3325 cpi->prob_last_coded = 214;
3326 cpi->prob_gf_coded = 1;
3328 else if (cpi->common.frames_since_golden == 1)
3330 cpi->prob_last_coded = 192;
3331 cpi->prob_gf_coded = 220;
3333 else if (cpi->source_alt_ref_active)
3335 //int dist = cpi->common.frames_till_alt_ref_frame + cpi->common.frames_since_golden;
3336 cpi->prob_gf_coded -= 20;
3338 if (cpi->prob_gf_coded < 10)
3339 cpi->prob_gf_coded = 10;
3342 #endif
3346 // 1 = key, 0 = inter
3347 static int decide_key_frame(VP8_COMP *cpi)
3349 VP8_COMMON *cm = &cpi->common;
3351 int code_key_frame = FALSE;
3353 cpi->kf_boost = 0;
3355 if (cpi->Speed > 11)
3356 return FALSE;
3358 // Clear down mmx registers
3359 vp8_clear_system_state(); //__asm emms;
3361 if ((cpi->compressor_speed == 2) && (cpi->Speed >= 5) && (cpi->sf.RD == 0))
3363 double change = 1.0 * abs((int)(cpi->intra_error - cpi->last_intra_error)) / (1 + cpi->last_intra_error);
3364 double change2 = 1.0 * abs((int)(cpi->prediction_error - cpi->last_prediction_error)) / (1 + cpi->last_prediction_error);
3365 double minerror = cm->MBs * 256;
3367 #if 0
3369 if (10 * cpi->intra_error / (1 + cpi->prediction_error) < 15
3370 && cpi->prediction_error > minerror
3371 && (change > .25 || change2 > .25))
3373 FILE *f = fopen("intra_inter.stt", "a");
3375 if (cpi->prediction_error <= 0)
3376 cpi->prediction_error = 1;
3378 fprintf(f, "%d %d %d %d %14.4f\n",
3379 cm->current_video_frame,
3380 (int) cpi->prediction_error,
3381 (int) cpi->intra_error,
3382 (int)((10 * cpi->intra_error) / cpi->prediction_error),
3383 change);
3385 fclose(f);
3388 #endif
3390 cpi->last_intra_error = cpi->intra_error;
3391 cpi->last_prediction_error = cpi->prediction_error;
3393 if (10 * cpi->intra_error / (1 + cpi->prediction_error) < 15
3394 && cpi->prediction_error > minerror
3395 && (change > .25 || change2 > .25))
3397 /*(change > 1.4 || change < .75)&& cpi->this_frame_percent_intra > cpi->last_frame_percent_intra + 3*/
3398 return TRUE;
3401 return FALSE;
3405 // If the following are true we might as well code a key frame
3406 if (((cpi->this_frame_percent_intra == 100) &&
3407 (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra + 2))) ||
3408 ((cpi->this_frame_percent_intra > 95) &&
3409 (cpi->this_frame_percent_intra >= (cpi->last_frame_percent_intra + 5))))
3411 code_key_frame = TRUE;
3413 // in addition if the following are true and this is not a golden frame then code a key frame
3414 // Note that on golden frames there often seems to be a pop in intra useage anyway hence this
3415 // restriction is designed to prevent spurious key frames. The Intra pop needs to be investigated.
3416 else if (((cpi->this_frame_percent_intra > 60) &&
3417 (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra * 2))) ||
3418 ((cpi->this_frame_percent_intra > 75) &&
3419 (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra * 3 / 2))) ||
3420 ((cpi->this_frame_percent_intra > 90) &&
3421 (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra + 10))))
3423 if (!cm->refresh_golden_frame)
3424 code_key_frame = TRUE;
3427 return code_key_frame;
3431 #if !(CONFIG_REALTIME_ONLY)
3432 static void Pass1Encode(VP8_COMP *cpi, unsigned long *size, unsigned char *dest, unsigned int *frame_flags)
3434 (void) size;
3435 (void) dest;
3436 (void) frame_flags;
3437 set_quantizer(cpi, 26);
3439 scale_and_extend_source(cpi->un_scaled_source, cpi);
3440 vp8_first_pass(cpi);
3442 #endif
3444 #if 0
3445 void write_cx_frame_to_file(YV12_BUFFER_CONFIG *frame, int this_frame)
3448 // write the frame
3449 FILE *yframe;
3450 int i;
3451 char filename[255];
3453 sprintf(filename, "cx\\y%04d.raw", this_frame);
3454 yframe = fopen(filename, "wb");
3456 for (i = 0; i < frame->y_height; i++)
3457 fwrite(frame->y_buffer + i * frame->y_stride, frame->y_width, 1, yframe);
3459 fclose(yframe);
3460 sprintf(filename, "cx\\u%04d.raw", this_frame);
3461 yframe = fopen(filename, "wb");
3463 for (i = 0; i < frame->uv_height; i++)
3464 fwrite(frame->u_buffer + i * frame->uv_stride, frame->uv_width, 1, yframe);
3466 fclose(yframe);
3467 sprintf(filename, "cx\\v%04d.raw", this_frame);
3468 yframe = fopen(filename, "wb");
3470 for (i = 0; i < frame->uv_height; i++)
3471 fwrite(frame->v_buffer + i * frame->uv_stride, frame->uv_width, 1, yframe);
3473 fclose(yframe);
3475 #endif
3476 // return of 0 means drop frame
3478 // Function to test for conditions that indeicate we should loop
3479 // back and recode a frame.
3480 static BOOL recode_loop_test( VP8_COMP *cpi,
3481 int high_limit, int low_limit,
3482 int q, int maxq, int minq )
3484 BOOL force_recode = FALSE;
3485 VP8_COMMON *cm = &cpi->common;
3487 // Is frame recode allowed at all
3488 // Yes if either recode mode 1 is selected or mode two is selcted
3489 // and the frame is a key frame. golden frame or alt_ref_frame
3490 if ( (cpi->sf.recode_loop == 1) ||
3491 ( (cpi->sf.recode_loop == 2) &&
3492 ( (cm->frame_type == KEY_FRAME) ||
3493 cm->refresh_golden_frame ||
3494 cm->refresh_alt_ref_frame ) ) )
3496 // General over and under shoot tests
3497 if ( ((cpi->projected_frame_size > high_limit) && (q < maxq)) ||
3498 ((cpi->projected_frame_size < low_limit) && (q > minq)) )
3500 force_recode = TRUE;
3502 // Special Constrained quality tests
3503 else if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY)
3505 // Undershoot and below auto cq level
3506 if ( (q > cpi->cq_target_quality) &&
3507 (cpi->projected_frame_size <
3508 ((cpi->this_frame_target * 7) >> 3)))
3510 force_recode = TRUE;
3512 // Severe undershoot and between auto and user cq level
3513 else if ( (q > cpi->oxcf.cq_level) &&
3514 (cpi->projected_frame_size < cpi->min_frame_bandwidth) &&
3515 (cpi->active_best_quality > cpi->oxcf.cq_level))
3517 force_recode = TRUE;
3518 cpi->active_best_quality = cpi->oxcf.cq_level;
3523 return force_recode;
3526 static void encode_frame_to_data_rate
3528 VP8_COMP *cpi,
3529 unsigned long *size,
3530 unsigned char *dest,
3531 unsigned int *frame_flags
3534 int Q;
3535 int frame_over_shoot_limit;
3536 int frame_under_shoot_limit;
3538 int Loop = FALSE;
3539 int loop_count;
3540 int this_q;
3541 int last_zbin_oq;
3543 int q_low;
3544 int q_high;
3545 int zbin_oq_high;
3546 int zbin_oq_low = 0;
3547 int top_index;
3548 int bottom_index;
3549 VP8_COMMON *cm = &cpi->common;
3550 int active_worst_qchanged = FALSE;
3552 int overshoot_seen = FALSE;
3553 int undershoot_seen = FALSE;
3554 int drop_mark = cpi->oxcf.drop_frames_water_mark * cpi->oxcf.optimal_buffer_level / 100;
3555 int drop_mark75 = drop_mark * 2 / 3;
3556 int drop_mark50 = drop_mark / 4;
3557 int drop_mark25 = drop_mark / 8;
3559 // Clear down mmx registers to allow floating point in what follows
3560 vp8_clear_system_state();
3562 // Test code for segmentation of gf/arf (0,0)
3563 //segmentation_test_function((VP8_PTR) cpi);
3565 #if CONFIG_REALTIME_ONLY
3566 if(cpi->oxcf.auto_key && cm->frame_type != KEY_FRAME)
3568 if(cpi->force_next_frame_intra)
3570 cm->frame_type = KEY_FRAME; /* delayed intra frame */
3573 cpi->force_next_frame_intra = 0;
3574 #endif
3576 // For an alt ref frame in 2 pass we skip the call to the second pass function that sets the target bandwidth
3577 #if !(CONFIG_REALTIME_ONLY)
3579 if (cpi->pass == 2)
3581 if (cpi->common.refresh_alt_ref_frame)
3583 cpi->per_frame_bandwidth = cpi->gf_bits; // Per frame bit target for the alt ref frame
3584 cpi->target_bandwidth = cpi->gf_bits * cpi->output_frame_rate; // per second target bitrate
3587 else
3588 #endif
3589 cpi->per_frame_bandwidth = (int)(cpi->target_bandwidth / cpi->output_frame_rate);
3591 // Default turn off buffer to buffer copying
3592 cm->copy_buffer_to_gf = 0;
3593 cm->copy_buffer_to_arf = 0;
3595 // Clear zbin over-quant value and mode boost values.
3596 cpi->zbin_over_quant = 0;
3597 cpi->zbin_mode_boost = 0;
3599 // Enable or disable mode based tweaking of the zbin
3600 // For 2 Pass Only used where GF/ARF prediction quality
3601 // is above a threshold
3602 cpi->zbin_mode_boost = 0;
3603 cpi->zbin_mode_boost_enabled = TRUE;
3604 if (cpi->pass == 2)
3606 if ( cpi->gfu_boost <= 400 )
3608 cpi->zbin_mode_boost_enabled = FALSE;
3612 // Current default encoder behaviour for the altref sign bias
3613 if (cpi->source_alt_ref_active)
3614 cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = 1;
3615 else
3616 cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = 0;
3618 // Check to see if a key frame is signalled
3619 // For two pass with auto key frame enabled cm->frame_type may already be set, but not for one pass.
3620 if ((cm->current_video_frame == 0) ||
3621 (cm->frame_flags & FRAMEFLAGS_KEY) ||
3622 (cpi->oxcf.auto_key && (cpi->frames_since_key % cpi->key_frame_frequency == 0)))
3624 // Key frame from VFW/auto-keyframe/first frame
3625 cm->frame_type = KEY_FRAME;
3628 // Set default state for segment and mode based loop filter update flags
3629 cpi->mb.e_mbd.update_mb_segmentation_map = 0;
3630 cpi->mb.e_mbd.update_mb_segmentation_data = 0;
3631 cpi->mb.e_mbd.mode_ref_lf_delta_update = 0;
3633 // Set various flags etc to special state if it is a key frame
3634 if (cm->frame_type == KEY_FRAME)
3636 int i;
3638 // Reset the loop filter deltas and segmentation map
3639 setup_features(cpi);
3641 // If segmentation is enabled force a map update for key frames
3642 if (cpi->mb.e_mbd.segmentation_enabled)
3644 cpi->mb.e_mbd.update_mb_segmentation_map = 1;
3645 cpi->mb.e_mbd.update_mb_segmentation_data = 1;
3648 // The alternate reference frame cannot be active for a key frame
3649 cpi->source_alt_ref_active = FALSE;
3651 // Reset the RD threshold multipliers to default of * 1 (128)
3652 for (i = 0; i < MAX_MODES; i++)
3654 cpi->rd_thresh_mult[i] = 128;
3658 // Test code for segmentation
3659 //if ( (cm->frame_type == KEY_FRAME) || ((cm->current_video_frame % 2) == 0))
3660 //if ( (cm->current_video_frame % 2) == 0 )
3661 // enable_segmentation((VP8_PTR)cpi);
3662 //else
3663 // disable_segmentation((VP8_PTR)cpi);
3665 #if 0
3666 // Experimental code for lagged compress and one pass
3667 // Initialise one_pass GF frames stats
3668 // Update stats used for GF selection
3669 //if ( cpi->pass == 0 )
3671 cpi->one_pass_frame_index = cm->current_video_frame % MAX_LAG_BUFFERS;
3673 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frames_so_far = 0;
3674 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_intra_error = 0.0;
3675 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_coded_error = 0.0;
3676 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_pcnt_inter = 0.0;
3677 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_pcnt_motion = 0.0;
3678 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvr = 0.0;
3679 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvr_abs = 0.0;
3680 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvc = 0.0;
3681 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvc_abs = 0.0;
3683 #endif
3685 update_rd_ref_frame_probs(cpi);
3687 if (cpi->drop_frames_allowed)
3689 // The reset to decimation 0 is only done here for one pass.
3690 // Once it is set two pass leaves decimation on till the next kf.
3691 if ((cpi->buffer_level > drop_mark) && (cpi->decimation_factor > 0))
3692 cpi->decimation_factor --;
3694 if (cpi->buffer_level > drop_mark75 && cpi->decimation_factor > 0)
3695 cpi->decimation_factor = 1;
3697 else if (cpi->buffer_level < drop_mark25 && (cpi->decimation_factor == 2 || cpi->decimation_factor == 3))
3699 cpi->decimation_factor = 3;
3701 else if (cpi->buffer_level < drop_mark50 && (cpi->decimation_factor == 1 || cpi->decimation_factor == 2))
3703 cpi->decimation_factor = 2;
3705 else if (cpi->buffer_level < drop_mark75 && (cpi->decimation_factor == 0 || cpi->decimation_factor == 1))
3707 cpi->decimation_factor = 1;
3710 //vpx_log("Encoder: Decimation Factor: %d \n",cpi->decimation_factor);
3713 // The following decimates the frame rate according to a regular pattern (i.e. to 1/2 or 2/3 frame rate)
3714 // This can be used to help prevent buffer under-run in CBR mode. Alternatively it might be desirable in
3715 // some situations to drop frame rate but throw more bits at each frame.
3717 // Note that dropping a key frame can be problematic if spatial resampling is also active
3718 if (cpi->decimation_factor > 0)
3720 switch (cpi->decimation_factor)
3722 case 1:
3723 cpi->per_frame_bandwidth = cpi->per_frame_bandwidth * 3 / 2;
3724 break;
3725 case 2:
3726 cpi->per_frame_bandwidth = cpi->per_frame_bandwidth * 5 / 4;
3727 break;
3728 case 3:
3729 cpi->per_frame_bandwidth = cpi->per_frame_bandwidth * 5 / 4;
3730 break;
3733 // Note that we should not throw out a key frame (especially when spatial resampling is enabled).
3734 if ((cm->frame_type == KEY_FRAME)) // && cpi->oxcf.allow_spatial_resampling )
3736 cpi->decimation_count = cpi->decimation_factor;
3738 else if (cpi->decimation_count > 0)
3740 cpi->decimation_count --;
3741 cpi->bits_off_target += cpi->av_per_frame_bandwidth;
3742 cm->current_video_frame++;
3743 cpi->frames_since_key++;
3745 #if CONFIG_PSNR
3746 cpi->count ++;
3747 #endif
3749 cpi->buffer_level = cpi->bits_off_target;
3751 return;
3753 else
3754 cpi->decimation_count = cpi->decimation_factor;
3757 // Decide how big to make the frame
3758 if (!pick_frame_size(cpi))
3760 cm->current_video_frame++;
3761 cpi->frames_since_key++;
3762 return;
3765 // Reduce active_worst_allowed_q for CBR if our buffer is getting too full.
3766 // This has a knock on effect on active best quality as well.
3767 // For CBR if the buffer reaches its maximum level then we can no longer
3768 // save up bits for later frames so we might as well use them up
3769 // on the current frame.
3770 if ((cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) &&
3771 (cpi->buffer_level >= cpi->oxcf.optimal_buffer_level) && cpi->buffered_mode)
3773 int Adjustment = cpi->active_worst_quality / 4; // Max adjustment is 1/4
3775 if (Adjustment)
3777 int buff_lvl_step;
3778 int tmp_lvl = cpi->buffer_level;
3780 if (cpi->buffer_level < cpi->oxcf.maximum_buffer_size)
3782 buff_lvl_step = (cpi->oxcf.maximum_buffer_size - cpi->oxcf.optimal_buffer_level) / Adjustment;
3784 if (buff_lvl_step)
3786 Adjustment = (cpi->buffer_level - cpi->oxcf.optimal_buffer_level) / buff_lvl_step;
3787 cpi->active_worst_quality -= Adjustment;
3790 else
3792 cpi->active_worst_quality -= Adjustment;
3797 // Set an active best quality and if necessary active worst quality
3798 // There is some odd behaviour for one pass here that needs attention.
3799 if ( (cpi->pass == 2) || (cpi->ni_frames > 150))
3801 vp8_clear_system_state();
3803 Q = cpi->active_worst_quality;
3805 if ( cm->frame_type == KEY_FRAME )
3807 if ( cpi->pass == 2 )
3809 if (cpi->gfu_boost > 600)
3810 cpi->active_best_quality = kf_low_motion_minq[Q];
3811 else
3812 cpi->active_best_quality = kf_high_motion_minq[Q];
3814 // Special case for key frames forced because we have reached
3815 // the maximum key frame interval. Here force the Q to a range
3816 // based on the ambient Q to reduce the risk of popping
3817 if ( cpi->this_key_frame_forced )
3819 if ( cpi->active_best_quality > cpi->avg_frame_qindex * 7/8)
3820 cpi->active_best_quality = cpi->avg_frame_qindex * 7/8;
3821 else if ( cpi->active_best_quality < cpi->avg_frame_qindex >> 2 )
3822 cpi->active_best_quality = cpi->avg_frame_qindex >> 2;
3825 // One pass more conservative
3826 else
3827 cpi->active_best_quality = kf_high_motion_minq[Q];
3830 else if (cm->refresh_golden_frame || cpi->common.refresh_alt_ref_frame)
3832 // Use the lower of cpi->active_worst_quality and recent
3833 // average Q as basis for GF/ARF Q limit unless last frame was
3834 // a key frame.
3835 if ( (cpi->frames_since_key > 1) &&
3836 (cpi->avg_frame_qindex < cpi->active_worst_quality) )
3838 Q = cpi->avg_frame_qindex;
3840 if ( (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) &&
3841 (Q < cpi->oxcf.cq_level) )
3843 Q = cpi->oxcf.cq_level;
3847 if ( cpi->pass == 2 )
3849 if ( cpi->gfu_boost > 1000 )
3850 cpi->active_best_quality = gf_low_motion_minq[Q];
3851 else if ( cpi->gfu_boost < 400 )
3852 cpi->active_best_quality = gf_high_motion_minq[Q];
3853 else
3854 cpi->active_best_quality = gf_mid_motion_minq[Q];
3856 // One pass more conservative
3857 else
3858 cpi->active_best_quality = gf_high_motion_minq[Q];
3860 else
3862 cpi->active_best_quality = inter_minq[Q];
3864 // For the constant/constrained quality mode we dont want
3865 // the quality to rise above the cq level.
3866 if ((cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) &&
3867 (cpi->active_best_quality < cpi->cq_target_quality) )
3869 // If we are strongly undershooting the target rate in the last
3870 // frames then use the user passed in cq value not the auto
3871 // cq value.
3872 if ( cpi->rolling_actual_bits < cpi->min_frame_bandwidth )
3873 cpi->active_best_quality = cpi->oxcf.cq_level;
3874 else
3875 cpi->active_best_quality = cpi->cq_target_quality;
3879 // If CBR and the buffer is as full then it is reasonable to allow higher quality on the frames
3880 // to prevent bits just going to waste.
3881 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
3883 // Note that the use of >= here elliminates the risk of a devide by 0 error in the else if clause
3884 if (cpi->buffer_level >= cpi->oxcf.maximum_buffer_size)
3885 cpi->active_best_quality = cpi->best_quality;
3887 else if (cpi->buffer_level > cpi->oxcf.optimal_buffer_level)
3889 int Fraction = ((cpi->buffer_level - cpi->oxcf.optimal_buffer_level) * 128) / (cpi->oxcf.maximum_buffer_size - cpi->oxcf.optimal_buffer_level);
3890 int min_qadjustment = ((cpi->active_best_quality - cpi->best_quality) * Fraction) / 128;
3892 cpi->active_best_quality -= min_qadjustment;
3897 // Clip the active best and worst quality values to limits
3898 if (cpi->active_worst_quality > cpi->worst_quality)
3899 cpi->active_worst_quality = cpi->worst_quality;
3901 if (cpi->active_best_quality < cpi->best_quality)
3902 cpi->active_best_quality = cpi->best_quality;
3903 else if (cpi->active_best_quality > cpi->active_worst_quality)
3904 cpi->active_best_quality = cpi->active_worst_quality;
3906 // Determine initial Q to try
3907 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
3908 last_zbin_oq = cpi->zbin_over_quant;
3910 // Set highest allowed value for Zbin over quant
3911 if (cm->frame_type == KEY_FRAME)
3912 zbin_oq_high = 0; //ZBIN_OQ_MAX/16
3913 else if (cm->refresh_alt_ref_frame || (cm->refresh_golden_frame && !cpi->source_alt_ref_active))
3914 zbin_oq_high = 16;
3915 else
3916 zbin_oq_high = ZBIN_OQ_MAX;
3918 // Setup background Q adjustment for error resilliant mode
3919 if (cpi->cyclic_refresh_mode_enabled)
3920 cyclic_background_refresh(cpi, Q, 0);
3922 vp8_compute_frame_size_bounds(cpi, &frame_under_shoot_limit, &frame_over_shoot_limit);
3924 // Limit Q range for the adaptive loop.
3925 bottom_index = cpi->active_best_quality;
3926 top_index = cpi->active_worst_quality;
3927 q_low = cpi->active_best_quality;
3928 q_high = cpi->active_worst_quality;
3930 vp8_save_coding_context(cpi);
3932 loop_count = 0;
3935 scale_and_extend_source(cpi->un_scaled_source, cpi);
3936 #if !(CONFIG_REALTIME_ONLY) && CONFIG_POSTPROC
3938 if (cpi->oxcf.noise_sensitivity > 0)
3940 unsigned char *src;
3941 int l = 0;
3943 switch (cpi->oxcf.noise_sensitivity)
3945 case 1:
3946 l = 20;
3947 break;
3948 case 2:
3949 l = 40;
3950 break;
3951 case 3:
3952 l = 60;
3953 break;
3954 case 4:
3955 l = 80;
3956 break;
3957 case 5:
3958 l = 100;
3959 break;
3960 case 6:
3961 l = 150;
3962 break;
3966 if (cm->frame_type == KEY_FRAME)
3968 vp8_de_noise(cpi->Source, cpi->Source, l , 1, 0, RTCD(postproc));
3970 else
3972 vp8_de_noise(cpi->Source, cpi->Source, l , 1, 0, RTCD(postproc));
3974 src = cpi->Source->y_buffer;
3976 if (cpi->Source->y_stride < 0)
3978 src += cpi->Source->y_stride * (cpi->Source->y_height - 1);
3983 #endif
3985 #ifdef OUTPUT_YUV_SRC
3986 vp8_write_yuv_frame(cpi->Source);
3987 #endif
3991 vp8_clear_system_state(); //__asm emms;
3994 if(cpi->is_src_frame_alt_ref)
3995 Q = 127;
3998 set_quantizer(cpi, Q);
3999 this_q = Q;
4001 // setup skip prob for costing in mode/mv decision
4002 if (cpi->common.mb_no_coeff_skip)
4004 cpi->prob_skip_false = cpi->base_skip_false_prob[Q];
4006 if (cm->frame_type != KEY_FRAME)
4008 if (cpi->common.refresh_alt_ref_frame)
4010 if (cpi->last_skip_false_probs[2] != 0)
4011 cpi->prob_skip_false = cpi->last_skip_false_probs[2];
4014 if(cpi->last_skip_false_probs[2]!=0 && abs(Q- cpi->last_skip_probs_q[2])<=16 )
4015 cpi->prob_skip_false = cpi->last_skip_false_probs[2];
4016 else if (cpi->last_skip_false_probs[2]!=0)
4017 cpi->prob_skip_false = (cpi->last_skip_false_probs[2] + cpi->prob_skip_false ) / 2;
4020 else if (cpi->common.refresh_golden_frame)
4022 if (cpi->last_skip_false_probs[1] != 0)
4023 cpi->prob_skip_false = cpi->last_skip_false_probs[1];
4026 if(cpi->last_skip_false_probs[1]!=0 && abs(Q- cpi->last_skip_probs_q[1])<=16 )
4027 cpi->prob_skip_false = cpi->last_skip_false_probs[1];
4028 else if (cpi->last_skip_false_probs[1]!=0)
4029 cpi->prob_skip_false = (cpi->last_skip_false_probs[1] + cpi->prob_skip_false ) / 2;
4032 else
4034 if (cpi->last_skip_false_probs[0] != 0)
4035 cpi->prob_skip_false = cpi->last_skip_false_probs[0];
4038 if(cpi->last_skip_false_probs[0]!=0 && abs(Q- cpi->last_skip_probs_q[0])<=16 )
4039 cpi->prob_skip_false = cpi->last_skip_false_probs[0];
4040 else if(cpi->last_skip_false_probs[0]!=0)
4041 cpi->prob_skip_false = (cpi->last_skip_false_probs[0] + cpi->prob_skip_false ) / 2;
4045 //as this is for cost estimate, let's make sure it does not go extreme eitehr way
4046 if (cpi->prob_skip_false < 5)
4047 cpi->prob_skip_false = 5;
4049 if (cpi->prob_skip_false > 250)
4050 cpi->prob_skip_false = 250;
4052 if (cpi->is_src_frame_alt_ref)
4053 cpi->prob_skip_false = 1;
4058 #if 0
4060 if (cpi->pass != 1)
4062 FILE *f = fopen("skip.stt", "a");
4063 fprintf(f, "%d, %d, %4d ", cpi->common.refresh_golden_frame, cpi->common.refresh_alt_ref_frame, cpi->prob_skip_false);
4064 fclose(f);
4067 #endif
4071 if (cm->frame_type == KEY_FRAME)
4072 vp8_setup_key_frame(cpi);
4074 // transform / motion compensation build reconstruction frame
4076 vp8_encode_frame(cpi);
4077 cpi->projected_frame_size -= vp8_estimate_entropy_savings(cpi);
4078 cpi->projected_frame_size = (cpi->projected_frame_size > 0) ? cpi->projected_frame_size : 0;
4080 vp8_clear_system_state(); //__asm emms;
4082 // Test to see if the stats generated for this frame indicate that we should have coded a key frame
4083 // (assuming that we didn't)!
4084 if (cpi->pass != 2 && cpi->oxcf.auto_key && cm->frame_type != KEY_FRAME)
4087 #if CONFIG_REALTIME_ONLY
4089 /* we don't do re-encoding in realtime mode
4090 * if key frame is decided than we force it on next frame */
4091 cpi->force_next_frame_intra = decide_key_frame(cpi);
4093 #else
4094 if (decide_key_frame(cpi))
4096 vp8_calc_auto_iframe_target_size(cpi);
4098 // Reset all our sizing numbers and recode
4099 cm->frame_type = KEY_FRAME;
4101 // Clear the Alt reference frame active flag when we have a key frame
4102 cpi->source_alt_ref_active = FALSE;
4104 // Reset the loop filter deltas and segmentation map
4105 setup_features(cpi);
4107 // If segmentation is enabled force a map update for key frames
4108 if (cpi->mb.e_mbd.segmentation_enabled)
4110 cpi->mb.e_mbd.update_mb_segmentation_map = 1;
4111 cpi->mb.e_mbd.update_mb_segmentation_data = 1;
4114 vp8_restore_coding_context(cpi);
4116 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4118 vp8_compute_frame_size_bounds(cpi, &frame_under_shoot_limit, &frame_over_shoot_limit);
4120 // Limit Q range for the adaptive loop.
4121 bottom_index = cpi->active_best_quality;
4122 top_index = cpi->active_worst_quality;
4123 q_low = cpi->active_best_quality;
4124 q_high = cpi->active_worst_quality;
4126 loop_count++;
4127 Loop = TRUE;
4129 resize_key_frame(cpi);
4130 continue;
4132 #endif
4135 vp8_clear_system_state();
4137 if (frame_over_shoot_limit == 0)
4138 frame_over_shoot_limit = 1;
4140 // Are we are overshooting and up against the limit of active max Q.
4141 if (((cpi->pass != 2) || (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)) &&
4142 (Q == cpi->active_worst_quality) &&
4143 (cpi->active_worst_quality < cpi->worst_quality) &&
4144 (cpi->projected_frame_size > frame_over_shoot_limit))
4146 int over_size_percent = ((cpi->projected_frame_size - frame_over_shoot_limit) * 100) / frame_over_shoot_limit;
4148 // If so is there any scope for relaxing it
4149 while ((cpi->active_worst_quality < cpi->worst_quality) && (over_size_percent > 0))
4151 cpi->active_worst_quality++;
4152 top_index = cpi->active_worst_quality;
4153 over_size_percent = (int)(over_size_percent * 0.96); // Assume 1 qstep = about 4% on frame size.
4156 // If we have updated the active max Q do not call vp8_update_rate_correction_factors() this loop.
4157 active_worst_qchanged = TRUE;
4159 else
4160 active_worst_qchanged = FALSE;
4162 #if !(CONFIG_REALTIME_ONLY)
4163 // Special case handling for forced key frames
4164 if ( (cm->frame_type == KEY_FRAME) && cpi->this_key_frame_forced )
4166 int last_q = Q;
4167 int kf_err = vp8_calc_ss_err(cpi->Source,
4168 &cm->yv12_fb[cm->new_fb_idx],
4169 IF_RTCD(&cpi->rtcd.variance));
4171 // The key frame is not good enough
4172 if ( kf_err > ((cpi->ambient_err * 7) >> 3) )
4174 // Lower q_high
4175 q_high = (Q > q_low) ? (Q - 1) : q_low;
4177 // Adjust Q
4178 Q = (q_high + q_low) >> 1;
4180 // The key frame is much better than the previous frame
4181 else if ( kf_err < (cpi->ambient_err >> 1) )
4183 // Raise q_low
4184 q_low = (Q < q_high) ? (Q + 1) : q_high;
4186 // Adjust Q
4187 Q = (q_high + q_low + 1) >> 1;
4190 // Clamp Q to upper and lower limits:
4191 if (Q > q_high)
4192 Q = q_high;
4193 else if (Q < q_low)
4194 Q = q_low;
4196 Loop = ((Q != last_q)) ? TRUE : FALSE;
4199 // Is the projected frame size out of range and are we allowed to attempt to recode.
4200 else if ( recode_loop_test( cpi,
4201 frame_over_shoot_limit, frame_under_shoot_limit,
4202 Q, top_index, bottom_index ) )
4204 int last_q = Q;
4205 int Retries = 0;
4207 // Frame size out of permitted range:
4208 // Update correction factor & compute new Q to try...
4210 // Frame is too large
4211 if (cpi->projected_frame_size > cpi->this_frame_target)
4213 //if ( cpi->zbin_over_quant == 0 )
4214 q_low = (Q < q_high) ? (Q + 1) : q_high; // Raise Qlow as to at least the current value
4216 if (cpi->zbin_over_quant > 0) // If we are using over quant do the same for zbin_oq_low
4217 zbin_oq_low = (cpi->zbin_over_quant < zbin_oq_high) ? (cpi->zbin_over_quant + 1) : zbin_oq_high;
4219 //if ( undershoot_seen || (Q == MAXQ) )
4220 if (undershoot_seen)
4222 // Update rate_correction_factor unless cpi->active_worst_quality has changed.
4223 if (!active_worst_qchanged)
4224 vp8_update_rate_correction_factors(cpi, 1);
4226 Q = (q_high + q_low + 1) / 2;
4228 // Adjust cpi->zbin_over_quant (only allowed when Q is max)
4229 if (Q < MAXQ)
4230 cpi->zbin_over_quant = 0;
4231 else
4233 zbin_oq_low = (cpi->zbin_over_quant < zbin_oq_high) ? (cpi->zbin_over_quant + 1) : zbin_oq_high;
4234 cpi->zbin_over_quant = (zbin_oq_high + zbin_oq_low) / 2;
4237 else
4239 // Update rate_correction_factor unless cpi->active_worst_quality has changed.
4240 if (!active_worst_qchanged)
4241 vp8_update_rate_correction_factors(cpi, 0);
4243 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4245 while (((Q < q_low) || (cpi->zbin_over_quant < zbin_oq_low)) && (Retries < 10))
4247 vp8_update_rate_correction_factors(cpi, 0);
4248 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4249 Retries ++;
4253 overshoot_seen = TRUE;
4255 // Frame is too small
4256 else
4258 if (cpi->zbin_over_quant == 0)
4259 q_high = (Q > q_low) ? (Q - 1) : q_low; // Lower q_high if not using over quant
4260 else // else lower zbin_oq_high
4261 zbin_oq_high = (cpi->zbin_over_quant > zbin_oq_low) ? (cpi->zbin_over_quant - 1) : zbin_oq_low;
4263 if (overshoot_seen)
4265 // Update rate_correction_factor unless cpi->active_worst_quality has changed.
4266 if (!active_worst_qchanged)
4267 vp8_update_rate_correction_factors(cpi, 1);
4269 Q = (q_high + q_low) / 2;
4271 // Adjust cpi->zbin_over_quant (only allowed when Q is max)
4272 if (Q < MAXQ)
4273 cpi->zbin_over_quant = 0;
4274 else
4275 cpi->zbin_over_quant = (zbin_oq_high + zbin_oq_low) / 2;
4277 else
4279 // Update rate_correction_factor unless cpi->active_worst_quality has changed.
4280 if (!active_worst_qchanged)
4281 vp8_update_rate_correction_factors(cpi, 0);
4283 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4285 // Special case reset for qlow for constrained quality.
4286 // This should only trigger where there is very substantial
4287 // undershoot on a frame and the auto cq level is above
4288 // the user passsed in value.
4289 if ( (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) &&
4290 (Q < q_low) )
4292 q_low = Q;
4295 while (((Q > q_high) || (cpi->zbin_over_quant > zbin_oq_high)) && (Retries < 10))
4297 vp8_update_rate_correction_factors(cpi, 0);
4298 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4299 Retries ++;
4303 undershoot_seen = TRUE;
4306 // Clamp Q to upper and lower limits:
4307 if (Q > q_high)
4308 Q = q_high;
4309 else if (Q < q_low)
4310 Q = q_low;
4312 // Clamp cpi->zbin_over_quant
4313 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;
4315 //Loop = ((Q != last_q) || (last_zbin_oq != cpi->zbin_over_quant)) ? TRUE : FALSE;
4316 Loop = ((Q != last_q)) ? TRUE : FALSE;
4317 last_zbin_oq = cpi->zbin_over_quant;
4319 else
4320 #endif
4321 Loop = FALSE;
4323 if (cpi->is_src_frame_alt_ref)
4324 Loop = FALSE;
4326 if (Loop == TRUE)
4328 vp8_restore_coding_context(cpi);
4329 loop_count++;
4330 #if CONFIG_PSNR
4331 cpi->tot_recode_hits++;
4332 #endif
4335 while (Loop == TRUE);
4337 #if 0
4338 // Experimental code for lagged and one pass
4339 // Update stats used for one pass GF selection
4342 int frames_so_far;
4343 double frame_intra_error;
4344 double frame_coded_error;
4345 double frame_pcnt_inter;
4346 double frame_pcnt_motion;
4347 double frame_mvr;
4348 double frame_mvr_abs;
4349 double frame_mvc;
4350 double frame_mvc_abs;
4353 cpi->one_pass_frame_stats[cpi->one_pass_frame_index].frame_coded_error = (double)cpi->prediction_error;
4354 cpi->one_pass_frame_stats[cpi->one_pass_frame_index].frame_intra_error = (double)cpi->intra_error;
4355 cpi->one_pass_frame_stats[cpi->one_pass_frame_index].frame_pcnt_inter = (double)(100 - cpi->this_frame_percent_intra) / 100.0;
4357 #endif
4359 // Special case code to reduce pulsing when key frames are forced at a
4360 // fixed interval. Note the reconstruction error if it is the frame before
4361 // the force key frame
4362 if ( cpi->next_key_frame_forced && (cpi->frames_to_key == 0) )
4364 cpi->ambient_err = vp8_calc_ss_err(cpi->Source,
4365 &cm->yv12_fb[cm->new_fb_idx],
4366 IF_RTCD(&cpi->rtcd.variance));
4369 // This frame's MVs are saved and will be used in next frame's MV prediction.
4370 // Last frame has one more line(add to bottom) and one more column(add to right) than cm->mip. The edge elements are initialized to 0.
4371 if(cm->show_frame) //do not save for altref frame
4373 int mb_row;
4374 int mb_col;
4375 MODE_INFO *tmp = cm->mip; //point to beginning of allocated MODE_INFO arrays.
4377 if(cm->frame_type != KEY_FRAME)
4379 for (mb_row = 0; mb_row < cm->mb_rows+1; mb_row ++)
4381 for (mb_col = 0; mb_col < cm->mb_cols+1; mb_col ++)
4383 if(tmp->mbmi.ref_frame != INTRA_FRAME)
4384 cpi->lfmv[mb_col + mb_row*(cm->mode_info_stride+1)].as_int = tmp->mbmi.mv.as_int;
4386 cpi->lf_ref_frame_sign_bias[mb_col + mb_row*(cm->mode_info_stride+1)] = cm->ref_frame_sign_bias[tmp->mbmi.ref_frame];
4387 cpi->lf_ref_frame[mb_col + mb_row*(cm->mode_info_stride+1)] = tmp->mbmi.ref_frame;
4388 tmp++;
4394 // Update the GF useage maps.
4395 // This is done after completing the compression of a frame when all modes etc. are finalized but before loop filter
4396 // This is done after completing the compression of a frame when all modes etc. are finalized but before loop filter
4397 vp8_update_gf_useage_maps(cpi, cm, &cpi->mb);
4399 if (cm->frame_type == KEY_FRAME)
4400 cm->refresh_last_frame = 1;
4402 #if 0
4404 FILE *f = fopen("gfactive.stt", "a");
4405 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);
4406 fclose(f);
4408 #endif
4410 // 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
4411 // This is purely an encoder descision at present.
4412 if (!cpi->oxcf.error_resilient_mode && cm->refresh_golden_frame)
4413 cm->copy_buffer_to_arf = 2;
4414 else
4415 cm->copy_buffer_to_arf = 0;
4417 if (cm->refresh_last_frame)
4419 vp8_swap_yv12_buffer(&cm->yv12_fb[cm->lst_fb_idx], &cm->yv12_fb[cm->new_fb_idx]);
4420 cm->frame_to_show = &cm->yv12_fb[cm->lst_fb_idx];
4422 else
4423 cm->frame_to_show = &cm->yv12_fb[cm->new_fb_idx];
4425 //#pragma omp parallel sections
4428 //#pragma omp section
4431 struct vpx_usec_timer timer;
4433 vpx_usec_timer_start(&timer);
4435 if (cpi->sf.auto_filter == 0)
4436 vp8cx_pick_filter_level_fast(cpi->Source, cpi);
4437 else
4438 vp8cx_pick_filter_level(cpi->Source, cpi);
4440 vpx_usec_timer_mark(&timer);
4442 cpi->time_pick_lpf += vpx_usec_timer_elapsed(&timer);
4444 if (cm->no_lpf)
4445 cm->filter_level = 0;
4447 if (cm->filter_level > 0)
4449 vp8cx_set_alt_lf_level(cpi, cm->filter_level);
4450 vp8_loop_filter_frame(cm, &cpi->mb.e_mbd, cm->filter_level);
4451 cm->last_filter_type = cm->filter_type;
4452 cm->last_sharpness_level = cm->sharpness_level;
4454 /* Move storing frame_type out of the above loop since it is also needed in motion search besides loopfilter */
4455 cm->last_frame_type = cm->frame_type;
4457 vp8_yv12_extend_frame_borders_ptr(cm->frame_to_show);
4459 if (cpi->oxcf.error_resilient_mode == 1)
4461 cm->refresh_entropy_probs = 0;
4465 //#pragma omp section
4467 // build the bitstream
4468 vp8_pack_bitstream(cpi, dest, size);
4473 YV12_BUFFER_CONFIG *lst_yv12 = &cm->yv12_fb[cm->lst_fb_idx];
4474 YV12_BUFFER_CONFIG *new_yv12 = &cm->yv12_fb[cm->new_fb_idx];
4475 YV12_BUFFER_CONFIG *gld_yv12 = &cm->yv12_fb[cm->gld_fb_idx];
4476 YV12_BUFFER_CONFIG *alt_yv12 = &cm->yv12_fb[cm->alt_fb_idx];
4477 // At this point the new frame has been encoded coded.
4478 // If any buffer copy / swaping is signalled it should be done here.
4479 if (cm->frame_type == KEY_FRAME)
4481 vp8_yv12_copy_frame_ptr(cm->frame_to_show, gld_yv12);
4482 vp8_yv12_copy_frame_ptr(cm->frame_to_show, alt_yv12);
4484 else // For non key frames
4486 // Code to copy between reference buffers
4487 if (cm->copy_buffer_to_arf)
4489 if (cm->copy_buffer_to_arf == 1)
4491 if (cm->refresh_last_frame)
4492 // We copy new_frame here because last and new buffers will already have been swapped if cm->refresh_last_frame is set.
4493 vp8_yv12_copy_frame_ptr(new_yv12, alt_yv12);
4494 else
4495 vp8_yv12_copy_frame_ptr(lst_yv12, alt_yv12);
4497 else if (cm->copy_buffer_to_arf == 2)
4498 vp8_yv12_copy_frame_ptr(gld_yv12, alt_yv12);
4501 if (cm->copy_buffer_to_gf)
4503 if (cm->copy_buffer_to_gf == 1)
4505 if (cm->refresh_last_frame)
4506 // We copy new_frame here because last and new buffers will already have been swapped if cm->refresh_last_frame is set.
4507 vp8_yv12_copy_frame_ptr(new_yv12, gld_yv12);
4508 else
4509 vp8_yv12_copy_frame_ptr(lst_yv12, gld_yv12);
4511 else if (cm->copy_buffer_to_gf == 2)
4512 vp8_yv12_copy_frame_ptr(alt_yv12, gld_yv12);
4517 // Update rate control heuristics
4518 cpi->total_byte_count += (*size);
4519 cpi->projected_frame_size = (*size) << 3;
4521 if (!active_worst_qchanged)
4522 vp8_update_rate_correction_factors(cpi, 2);
4524 cpi->last_q[cm->frame_type] = cm->base_qindex;
4526 if (cm->frame_type == KEY_FRAME)
4528 vp8_adjust_key_frame_context(cpi);
4531 // Keep a record of ambient average Q.
4532 if (cm->frame_type != KEY_FRAME)
4533 cpi->avg_frame_qindex = (2 + 3 * cpi->avg_frame_qindex + cm->base_qindex) >> 2;
4535 // Keep a record from which we can calculate the average Q excluding GF updates and key frames
4536 if ((cm->frame_type != KEY_FRAME) && !cm->refresh_golden_frame && !cm->refresh_alt_ref_frame)
4538 cpi->ni_frames++;
4540 // Calculate the average Q for normal inter frames (not key or GFU
4541 // frames).
4542 if ( cpi->pass == 2 )
4544 cpi->ni_tot_qi += Q;
4545 cpi->ni_av_qi = (cpi->ni_tot_qi / cpi->ni_frames);
4547 else
4549 // Damp value for first few frames
4550 if (cpi->ni_frames > 150 )
4552 cpi->ni_tot_qi += Q;
4553 cpi->ni_av_qi = (cpi->ni_tot_qi / cpi->ni_frames);
4555 // For one pass, early in the clip ... average the current frame Q
4556 // value with the worstq entered by the user as a dampening measure
4557 else
4559 cpi->ni_tot_qi += Q;
4560 cpi->ni_av_qi = ((cpi->ni_tot_qi / cpi->ni_frames) + cpi->worst_quality + 1) / 2;
4563 // If the average Q is higher than what was used in the last frame
4564 // (after going through the recode loop to keep the frame size within range)
4565 // then use the last frame value - 1.
4566 // The -1 is designed to stop Q and hence the data rate, from progressively
4567 // falling away during difficult sections, but at the same time reduce the number of
4568 // itterations around the recode loop.
4569 if (Q > cpi->ni_av_qi)
4570 cpi->ni_av_qi = Q - 1;
4574 #if 0
4576 // If the frame was massively oversize and we are below optimal buffer level drop next frame
4577 if ((cpi->drop_frames_allowed) &&
4578 (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) &&
4579 (cpi->buffer_level < cpi->oxcf.drop_frames_water_mark * cpi->oxcf.optimal_buffer_level / 100) &&
4580 (cpi->projected_frame_size > (4 * cpi->this_frame_target)))
4582 cpi->drop_frame = TRUE;
4585 #endif
4587 // Set the count for maximum consequative dropped frames based upon the ratio of
4588 // this frame size to the target average per frame bandwidth.
4589 // (cpi->av_per_frame_bandwidth > 0) is just a sanity check to prevent / 0.
4590 if (cpi->drop_frames_allowed && (cpi->av_per_frame_bandwidth > 0))
4592 cpi->max_drop_count = cpi->projected_frame_size / cpi->av_per_frame_bandwidth;
4594 if (cpi->max_drop_count > cpi->max_consec_dropped_frames)
4595 cpi->max_drop_count = cpi->max_consec_dropped_frames;
4598 // Update the buffer level variable.
4599 // Non-viewable frames are a special case and are treated as pure overhead.
4600 if ( !cm->show_frame )
4601 cpi->bits_off_target -= cpi->projected_frame_size;
4602 else
4603 cpi->bits_off_target += cpi->av_per_frame_bandwidth - cpi->projected_frame_size;
4605 // Rolling monitors of whether we are over or underspending used to help regulate min and Max Q in two pass.
4606 cpi->rolling_target_bits = ((cpi->rolling_target_bits * 3) + cpi->this_frame_target + 2) / 4;
4607 cpi->rolling_actual_bits = ((cpi->rolling_actual_bits * 3) + cpi->projected_frame_size + 2) / 4;
4608 cpi->long_rolling_target_bits = ((cpi->long_rolling_target_bits * 31) + cpi->this_frame_target + 16) / 32;
4609 cpi->long_rolling_actual_bits = ((cpi->long_rolling_actual_bits * 31) + cpi->projected_frame_size + 16) / 32;
4611 // Actual bits spent
4612 cpi->total_actual_bits += cpi->projected_frame_size;
4614 // Debug stats
4615 cpi->total_target_vs_actual += (cpi->this_frame_target - cpi->projected_frame_size);
4617 cpi->buffer_level = cpi->bits_off_target;
4619 // Update bits left to the kf and gf groups to account for overshoot or undershoot on these frames
4620 if (cm->frame_type == KEY_FRAME)
4622 cpi->kf_group_bits += cpi->this_frame_target - cpi->projected_frame_size;
4624 if (cpi->kf_group_bits < 0)
4625 cpi->kf_group_bits = 0 ;
4627 else if (cm->refresh_golden_frame || cm->refresh_alt_ref_frame)
4629 cpi->gf_group_bits += cpi->this_frame_target - cpi->projected_frame_size;
4631 if (cpi->gf_group_bits < 0)
4632 cpi->gf_group_bits = 0 ;
4635 if (cm->frame_type != KEY_FRAME)
4637 if (cpi->common.refresh_alt_ref_frame)
4639 cpi->last_skip_false_probs[2] = cpi->prob_skip_false;
4640 cpi->last_skip_probs_q[2] = cm->base_qindex;
4642 else if (cpi->common.refresh_golden_frame)
4644 cpi->last_skip_false_probs[1] = cpi->prob_skip_false;
4645 cpi->last_skip_probs_q[1] = cm->base_qindex;
4647 else
4649 cpi->last_skip_false_probs[0] = cpi->prob_skip_false;
4650 cpi->last_skip_probs_q[0] = cm->base_qindex;
4652 //update the baseline
4653 cpi->base_skip_false_prob[cm->base_qindex] = cpi->prob_skip_false;
4658 #if 0 && CONFIG_PSNR
4660 FILE *f = fopen("tmp.stt", "a");
4662 vp8_clear_system_state(); //__asm emms;
4664 if (cpi->total_coded_error_left != 0.0)
4665 fprintf(f, "%10d %10d %10d %10d %10d %10d %10d %10d %6ld %6ld"
4666 "%6ld %6ld %6ld %5ld %5ld %5ld %8ld %8.2f %10d %10.3f"
4667 "%10.3f %8ld\n",
4668 cpi->common.current_video_frame, cpi->this_frame_target,
4669 cpi->projected_frame_size,
4670 (cpi->projected_frame_size - cpi->this_frame_target),
4671 (int)cpi->total_target_vs_actual,
4672 (cpi->oxcf.starting_buffer_level-cpi->bits_off_target),
4673 (int)cpi->total_actual_bits, cm->base_qindex,
4674 cpi->active_best_quality, cpi->active_worst_quality,
4675 cpi->ni_av_qi, cpi->cq_target_quality, cpi->zbin_over_quant,
4676 //cpi->avg_frame_qindex, cpi->zbin_over_quant,
4677 cm->refresh_golden_frame, cm->refresh_alt_ref_frame,
4678 cm->frame_type, cpi->gfu_boost,
4679 cpi->est_max_qcorrection_factor, (int)cpi->bits_left,
4680 cpi->total_coded_error_left,
4681 (double)cpi->bits_left / cpi->total_coded_error_left,
4682 cpi->tot_recode_hits);
4683 else
4684 fprintf(f, "%10d %10d %10d %10d %10d %10d %10d %10d %6ld %6ld"
4685 "%6ld %6ld %6ld %5ld %5ld %5ld %8ld %8.2f %10d %10.3f"
4686 "%8ld\n",
4687 cpi->common.current_video_frame,
4688 cpi->this_frame_target, cpi->projected_frame_size,
4689 (cpi->projected_frame_size - cpi->this_frame_target),
4690 (int)cpi->total_target_vs_actual,
4691 (cpi->oxcf.starting_buffer_level-cpi->bits_off_target),
4692 (int)cpi->total_actual_bits, cm->base_qindex,
4693 cpi->active_best_quality, cpi->active_worst_quality,
4694 cpi->ni_av_qi, cpi->cq_target_quality, cpi->zbin_over_quant,
4695 //cpi->avg_frame_qindex, cpi->zbin_over_quant,
4696 cm->refresh_golden_frame, cm->refresh_alt_ref_frame,
4697 cm->frame_type, cpi->gfu_boost,
4698 cpi->est_max_qcorrection_factor, (int)cpi->bits_left,
4699 cpi->total_coded_error_left, cpi->tot_recode_hits);
4701 fclose(f);
4704 FILE *fmodes = fopen("Modes.stt", "a");
4705 int i;
4707 fprintf(fmodes, "%6d:%1d:%1d:%1d ",
4708 cpi->common.current_video_frame,
4709 cm->frame_type, cm->refresh_golden_frame,
4710 cm->refresh_alt_ref_frame);
4712 for (i = 0; i < MAX_MODES; i++)
4713 fprintf(fmodes, "%5d ", cpi->mode_chosen_counts[i]);
4715 fprintf(fmodes, "\n");
4717 fclose(fmodes);
4721 #endif
4723 // If this was a kf or Gf note the Q
4724 if ((cm->frame_type == KEY_FRAME) || cm->refresh_golden_frame || cm->refresh_alt_ref_frame)
4725 cm->last_kf_gf_q = cm->base_qindex;
4727 if (cm->refresh_golden_frame == 1)
4728 cm->frame_flags = cm->frame_flags | FRAMEFLAGS_GOLDEN;
4729 else
4730 cm->frame_flags = cm->frame_flags&~FRAMEFLAGS_GOLDEN;
4732 if (cm->refresh_alt_ref_frame == 1)
4733 cm->frame_flags = cm->frame_flags | FRAMEFLAGS_ALTREF;
4734 else
4735 cm->frame_flags = cm->frame_flags&~FRAMEFLAGS_ALTREF;
4738 if (cm->refresh_last_frame & cm->refresh_golden_frame) // both refreshed
4739 cpi->gold_is_last = 1;
4740 else if (cm->refresh_last_frame ^ cm->refresh_golden_frame) // 1 refreshed but not the other
4741 cpi->gold_is_last = 0;
4743 if (cm->refresh_last_frame & cm->refresh_alt_ref_frame) // both refreshed
4744 cpi->alt_is_last = 1;
4745 else if (cm->refresh_last_frame ^ cm->refresh_alt_ref_frame) // 1 refreshed but not the other
4746 cpi->alt_is_last = 0;
4748 if (cm->refresh_alt_ref_frame & cm->refresh_golden_frame) // both refreshed
4749 cpi->gold_is_alt = 1;
4750 else if (cm->refresh_alt_ref_frame ^ cm->refresh_golden_frame) // 1 refreshed but not the other
4751 cpi->gold_is_alt = 0;
4753 cpi->ref_frame_flags = VP8_ALT_FLAG | VP8_GOLD_FLAG | VP8_LAST_FLAG;
4755 if (cpi->gold_is_last)
4756 cpi->ref_frame_flags &= ~VP8_GOLD_FLAG;
4758 if (cpi->alt_is_last)
4759 cpi->ref_frame_flags &= ~VP8_ALT_FLAG;
4761 if (cpi->gold_is_alt)
4762 cpi->ref_frame_flags &= ~VP8_ALT_FLAG;
4765 if (cpi->oxcf.error_resilient_mode)
4767 // Is this an alternate reference update
4768 if (cpi->common.refresh_alt_ref_frame)
4769 vp8_yv12_copy_frame_ptr(cm->frame_to_show, &cm->yv12_fb[cm->alt_fb_idx]);
4771 if (cpi->common.refresh_golden_frame)
4772 vp8_yv12_copy_frame_ptr(cm->frame_to_show, &cm->yv12_fb[cm->gld_fb_idx]);
4774 else
4776 if (cpi->oxcf.play_alternate && cpi->common.refresh_alt_ref_frame && (cpi->common.frame_type != KEY_FRAME))
4777 // Update the alternate reference frame and stats as appropriate.
4778 update_alt_ref_frame_and_stats(cpi);
4779 else
4780 // Update the Golden frame and golden frame and stats as appropriate.
4781 update_golden_frame_and_stats(cpi);
4784 if (cm->frame_type == KEY_FRAME)
4786 // Tell the caller that the frame was coded as a key frame
4787 *frame_flags = cm->frame_flags | FRAMEFLAGS_KEY;
4789 // As this frame is a key frame the next defaults to an inter frame.
4790 cm->frame_type = INTER_FRAME;
4792 cpi->last_frame_percent_intra = 100;
4794 else
4796 *frame_flags = cm->frame_flags&~FRAMEFLAGS_KEY;
4798 cpi->last_frame_percent_intra = cpi->this_frame_percent_intra;
4801 // Clear the one shot update flags for segmentation map and mode/ref loop filter deltas.
4802 cpi->mb.e_mbd.update_mb_segmentation_map = 0;
4803 cpi->mb.e_mbd.update_mb_segmentation_data = 0;
4804 cpi->mb.e_mbd.mode_ref_lf_delta_update = 0;
4807 // Dont increment frame counters if this was an altref buffer update not a real frame
4808 if (cm->show_frame)
4810 cm->current_video_frame++;
4811 cpi->frames_since_key++;
4814 // reset to normal state now that we are done.
4818 #if 0
4820 char filename[512];
4821 FILE *recon_file;
4822 sprintf(filename, "enc%04d.yuv", (int) cm->current_video_frame);
4823 recon_file = fopen(filename, "wb");
4824 fwrite(cm->yv12_fb[cm->lst_fb_idx].buffer_alloc,
4825 cm->yv12_fb[cm->lst_fb_idx].frame_size, 1, recon_file);
4826 fclose(recon_file);
4828 #endif
4830 // DEBUG
4831 //vp8_write_yuv_frame("encoder_recon.yuv", cm->frame_to_show);
4836 int vp8_is_gf_update_needed(VP8_PTR ptr)
4838 VP8_COMP *cpi = (VP8_COMP *) ptr;
4839 int ret_val;
4841 ret_val = cpi->gf_update_recommended;
4842 cpi->gf_update_recommended = 0;
4844 return ret_val;
4847 void vp8_check_gf_quality(VP8_COMP *cpi)
4849 VP8_COMMON *cm = &cpi->common;
4850 int gf_active_pct = (100 * cpi->gf_active_count) / (cm->mb_rows * cm->mb_cols);
4851 int gf_ref_usage_pct = (cpi->count_mb_ref_frame_usage[GOLDEN_FRAME] * 100) / (cm->mb_rows * cm->mb_cols);
4852 int last_ref_zz_useage = (cpi->inter_zz_count * 100) / (cm->mb_rows * cm->mb_cols);
4854 // Gf refresh is not currently being signalled
4855 if (cpi->gf_update_recommended == 0)
4857 if (cpi->common.frames_since_golden > 7)
4859 // Low use of gf
4860 if ((gf_active_pct < 10) || ((gf_active_pct + gf_ref_usage_pct) < 15))
4862 // ...but last frame zero zero usage is reasonbable so a new gf might be appropriate
4863 if (last_ref_zz_useage >= 25)
4865 cpi->gf_bad_count ++;
4867 if (cpi->gf_bad_count >= 8) // Check that the condition is stable
4869 cpi->gf_update_recommended = 1;
4870 cpi->gf_bad_count = 0;
4873 else
4874 cpi->gf_bad_count = 0; // Restart count as the background is not stable enough
4876 else
4877 cpi->gf_bad_count = 0; // Gf useage has picked up so reset count
4880 // If the signal is set but has not been read should we cancel it.
4881 else if (last_ref_zz_useage < 15)
4883 cpi->gf_update_recommended = 0;
4884 cpi->gf_bad_count = 0;
4887 #if 0
4889 FILE *f = fopen("gfneeded.stt", "a");
4890 fprintf(f, "%10d %10d %10d %10d %10ld \n",
4891 cm->current_video_frame,
4892 cpi->common.frames_since_golden,
4893 gf_active_pct, gf_ref_usage_pct,
4894 cpi->gf_update_recommended);
4895 fclose(f);
4898 #endif
4901 #if !(CONFIG_REALTIME_ONLY)
4902 static void Pass2Encode(VP8_COMP *cpi, unsigned long *size, unsigned char *dest, unsigned int *frame_flags)
4905 if (!cpi->common.refresh_alt_ref_frame)
4906 vp8_second_pass(cpi);
4908 encode_frame_to_data_rate(cpi, size, dest, frame_flags);
4909 cpi->bits_left -= 8 * *size;
4911 if (!cpi->common.refresh_alt_ref_frame)
4913 double two_pass_min_rate = (double)(cpi->oxcf.target_bandwidth
4914 *cpi->oxcf.two_pass_vbrmin_section / 100);
4915 cpi->bits_left += (long long)(two_pass_min_rate / cpi->oxcf.frame_rate);
4918 #endif
4920 //For ARM NEON, d8-d15 are callee-saved registers, and need to be saved by us.
4921 #if HAVE_ARMV7
4922 extern void vp8_push_neon(INT64 *store);
4923 extern void vp8_pop_neon(INT64 *store);
4924 #endif
4925 int vp8_receive_raw_frame(VP8_PTR ptr, unsigned int frame_flags, YV12_BUFFER_CONFIG *sd, INT64 time_stamp, INT64 end_time)
4927 #if HAVE_ARMV7
4928 INT64 store_reg[8];
4929 #endif
4930 VP8_COMP *cpi = (VP8_COMP *) ptr;
4931 VP8_COMMON *cm = &cpi->common;
4932 struct vpx_usec_timer timer;
4934 if (!cpi)
4935 return -1;
4937 #if HAVE_ARMV7
4938 #if CONFIG_RUNTIME_CPU_DETECT
4939 if (cm->rtcd.flags & HAS_NEON)
4940 #endif
4942 vp8_push_neon(store_reg);
4944 #endif
4946 vpx_usec_timer_start(&timer);
4948 // no more room for frames;
4949 if (cpi->source_buffer_count != 0 && cpi->source_buffer_count >= cpi->oxcf.lag_in_frames)
4951 #if HAVE_ARMV7
4952 #if CONFIG_RUNTIME_CPU_DETECT
4953 if (cm->rtcd.flags & HAS_NEON)
4954 #endif
4956 vp8_pop_neon(store_reg);
4958 #endif
4959 return -1;
4962 //printf("in-cpi->source_buffer_count: %d\n", cpi->source_buffer_count);
4964 cm->clr_type = sd->clrtype;
4966 // make a copy of the frame for use later...
4967 #if !(CONFIG_REALTIME_ONLY)
4969 if (cpi->oxcf.allow_lag)
4971 int which_buffer = cpi->source_encode_index - 1;
4972 SOURCE_SAMPLE *s;
4974 if (which_buffer == -1)
4975 which_buffer = cpi->oxcf.lag_in_frames - 1;
4977 if (cpi->source_buffer_count < cpi->oxcf.lag_in_frames - 1)
4978 which_buffer = cpi->source_buffer_count;
4980 s = &cpi->src_buffer[which_buffer];
4982 s->source_time_stamp = time_stamp;
4983 s->source_end_time_stamp = end_time;
4984 s->source_frame_flags = frame_flags;
4985 vp8_yv12_copy_frame_ptr(sd, &s->source_buffer);
4987 cpi->source_buffer_count ++;
4989 else
4990 #endif
4992 SOURCE_SAMPLE *s;
4993 s = &cpi->src_buffer[0];
4994 s->source_end_time_stamp = end_time;
4995 s->source_time_stamp = time_stamp;
4996 s->source_frame_flags = frame_flags;
4997 #if HAVE_ARMV7
4998 #if CONFIG_RUNTIME_CPU_DETECT
4999 if (cm->rtcd.flags & HAS_NEON)
5000 #endif
5002 vp8_yv12_copy_src_frame_func_neon(sd, &s->source_buffer);
5004 #if CONFIG_RUNTIME_CPU_DETECT
5005 else
5006 #endif
5007 #endif
5008 #if !HAVE_ARMV7 || CONFIG_RUNTIME_CPU_DETECT
5010 vp8_yv12_copy_frame_ptr(sd, &s->source_buffer);
5012 #endif
5013 cpi->source_buffer_count = 1;
5016 vpx_usec_timer_mark(&timer);
5017 cpi->time_receive_data += vpx_usec_timer_elapsed(&timer);
5019 #if HAVE_ARMV7
5020 #if CONFIG_RUNTIME_CPU_DETECT
5021 if (cm->rtcd.flags & HAS_NEON)
5022 #endif
5024 vp8_pop_neon(store_reg);
5026 #endif
5028 return 0;
5030 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)
5032 #if HAVE_ARMV7
5033 INT64 store_reg[8];
5034 #endif
5035 VP8_COMP *cpi = (VP8_COMP *) ptr;
5036 VP8_COMMON *cm = &cpi->common;
5037 struct vpx_usec_timer tsctimer;
5038 struct vpx_usec_timer ticktimer;
5039 struct vpx_usec_timer cmptimer;
5041 if (!cpi)
5042 return -1;
5044 #if HAVE_ARMV7
5045 #if CONFIG_RUNTIME_CPU_DETECT
5046 if (cm->rtcd.flags & HAS_NEON)
5047 #endif
5049 vp8_push_neon(store_reg);
5051 #endif
5053 vpx_usec_timer_start(&cmptimer);
5056 // flush variable tells us that even though we have less than 10 frames
5057 // in our buffer we need to start producing compressed frames.
5058 // Probably because we are at the end of a file....
5059 if ((cpi->source_buffer_count == cpi->oxcf.lag_in_frames && cpi->oxcf.lag_in_frames > 0)
5060 || (!cpi->oxcf.allow_lag && cpi->source_buffer_count > 0)
5061 || (flush && cpi->source_buffer_count > 0))
5064 SOURCE_SAMPLE *s;
5066 s = &cpi->src_buffer[cpi->source_encode_index];
5067 cpi->source_time_stamp = s->source_time_stamp;
5068 cpi->source_end_time_stamp = s->source_end_time_stamp;
5070 #if !(CONFIG_REALTIME_ONLY)
5072 // Should we code an alternate reference frame
5073 if (cpi->oxcf.error_resilient_mode == 0 &&
5074 cpi->oxcf.play_alternate &&
5075 cpi->source_alt_ref_pending &&
5076 (cpi->frames_till_gf_update_due < cpi->source_buffer_count) &&
5077 cpi->oxcf.lag_in_frames != 0)
5079 cpi->last_alt_ref_sei = (cpi->source_encode_index + cpi->frames_till_gf_update_due) % cpi->oxcf.lag_in_frames;
5081 #if VP8_TEMPORAL_ALT_REF
5083 if (cpi->oxcf.arnr_max_frames > 0)
5085 #if 0
5086 // my attempt at a loop that tests the results of strength filter.
5087 int start_frame = cpi->last_alt_ref_sei - 3;
5089 int i, besti = -1, pastin = cpi->oxcf.arnr_strength;
5091 int besterr;
5093 if (start_frame < 0)
5094 start_frame += cpi->oxcf.lag_in_frames;
5096 besterr = vp8_calc_low_ss_err(&cpi->src_buffer[cpi->last_alt_ref_sei].source_buffer,
5097 &cpi->src_buffer[start_frame].source_buffer, IF_RTCD(&cpi->rtcd.variance));
5099 for (i = 0; i < 7; i++)
5101 int thiserr;
5102 cpi->oxcf.arnr_strength = i;
5103 vp8_temporal_filter_prepare_c(cpi);
5105 thiserr = vp8_calc_low_ss_err(&cpi->alt_ref_buffer.source_buffer,
5106 &cpi->src_buffer[start_frame].source_buffer, IF_RTCD(&cpi->rtcd.variance));
5108 if (10 * thiserr < besterr * 8)
5110 besterr = thiserr;
5111 besti = i;
5115 if (besti != -1)
5117 cpi->oxcf.arnr_strength = besti;
5118 vp8_temporal_filter_prepare_c(cpi);
5119 s = &cpi->alt_ref_buffer;
5121 // FWG not sure if I need to copy this data for the Alt Ref frame
5122 s->source_time_stamp = cpi->src_buffer[cpi->last_alt_ref_sei].source_time_stamp;
5123 s->source_end_time_stamp = cpi->src_buffer[cpi->last_alt_ref_sei].source_end_time_stamp;
5124 s->source_frame_flags = cpi->src_buffer[cpi->last_alt_ref_sei].source_frame_flags;
5126 else
5127 s = &cpi->src_buffer[cpi->last_alt_ref_sei];
5129 #else
5130 vp8_temporal_filter_prepare_c(cpi);
5131 s = &cpi->alt_ref_buffer;
5133 // FWG not sure if I need to copy this data for the Alt Ref frame
5134 s->source_time_stamp = cpi->src_buffer[cpi->last_alt_ref_sei].source_time_stamp;
5135 s->source_end_time_stamp = cpi->src_buffer[cpi->last_alt_ref_sei].source_end_time_stamp;
5136 s->source_frame_flags = cpi->src_buffer[cpi->last_alt_ref_sei].source_frame_flags;
5138 #endif
5140 else
5141 #endif
5142 s = &cpi->src_buffer[cpi->last_alt_ref_sei];
5144 cm->frames_till_alt_ref_frame = cpi->frames_till_gf_update_due;
5145 cm->refresh_alt_ref_frame = 1;
5146 cm->refresh_golden_frame = 0;
5147 cm->refresh_last_frame = 0;
5148 cm->show_frame = 0;
5149 cpi->source_alt_ref_pending = FALSE; // Clear Pending altf Ref flag.
5150 cpi->is_src_frame_alt_ref = 0;
5151 cpi->is_next_src_alt_ref = 0;
5153 else
5154 #endif
5156 cm->show_frame = 1;
5157 #if !(CONFIG_REALTIME_ONLY)
5159 if (cpi->oxcf.allow_lag)
5161 if (cpi->source_encode_index == cpi->last_alt_ref_sei)
5163 cpi->is_src_frame_alt_ref = 1;
5164 cpi->last_alt_ref_sei = -1;
5166 else
5167 cpi->is_src_frame_alt_ref = 0;
5169 cpi->source_encode_index = (cpi->source_encode_index + 1) % cpi->oxcf.lag_in_frames;
5171 if(cpi->source_encode_index == cpi->last_alt_ref_sei)
5172 cpi->is_next_src_alt_ref = 1;
5173 else
5174 cpi->is_next_src_alt_ref = 0;
5177 #endif
5178 cpi->source_buffer_count--;
5181 cpi->un_scaled_source = &s->source_buffer;
5182 cpi->Source = &s->source_buffer;
5183 cpi->source_frame_flags = s->source_frame_flags;
5185 *time_stamp = cpi->source_time_stamp;
5186 *time_end = cpi->source_end_time_stamp;
5188 else
5190 *size = 0;
5191 #if !(CONFIG_REALTIME_ONLY)
5193 if (flush && cpi->pass == 1 && !cpi->first_pass_done)
5195 vp8_end_first_pass(cpi); /* get last stats packet */
5196 cpi->first_pass_done = 1;
5199 #endif
5201 #if HAVE_ARMV7
5202 #if CONFIG_RUNTIME_CPU_DETECT
5203 if (cm->rtcd.flags & HAS_NEON)
5204 #endif
5206 vp8_pop_neon(store_reg);
5208 #endif
5209 return -1;
5212 *frame_flags = cpi->source_frame_flags;
5214 if (cpi->source_time_stamp < cpi->first_time_stamp_ever)
5216 cpi->first_time_stamp_ever = cpi->source_time_stamp;
5217 cpi->last_end_time_stamp_seen = cpi->source_time_stamp;
5220 // adjust frame rates based on timestamps given
5221 if (!cm->refresh_alt_ref_frame)
5223 if (cpi->source_time_stamp == cpi->first_time_stamp_ever)
5225 double this_fps = 10000000.000 / (cpi->source_end_time_stamp - cpi->source_time_stamp);
5227 vp8_new_frame_rate(cpi, this_fps);
5229 else
5231 long long nanosecs = cpi->source_end_time_stamp
5232 - cpi->last_end_time_stamp_seen;
5233 double this_fps = 10000000.000 / nanosecs;
5235 vp8_new_frame_rate(cpi, (7 * cpi->oxcf.frame_rate + this_fps) / 8);
5239 cpi->last_time_stamp_seen = cpi->source_time_stamp;
5240 cpi->last_end_time_stamp_seen = cpi->source_end_time_stamp;
5243 if (cpi->compressor_speed == 2)
5245 vp8_check_gf_quality(cpi);
5248 if (!cpi)
5250 #if HAVE_ARMV7
5251 #if CONFIG_RUNTIME_CPU_DETECT
5252 if (cm->rtcd.flags & HAS_NEON)
5253 #endif
5255 vp8_pop_neon(store_reg);
5257 #endif
5258 return 0;
5261 if (cpi->compressor_speed == 2)
5263 vpx_usec_timer_start(&tsctimer);
5264 vpx_usec_timer_start(&ticktimer);
5267 // start with a 0 size frame
5268 *size = 0;
5270 // Clear down mmx registers
5271 vp8_clear_system_state(); //__asm emms;
5273 cm->frame_type = INTER_FRAME;
5274 cm->frame_flags = *frame_flags;
5276 #if 0
5278 if (cm->refresh_alt_ref_frame)
5280 //cm->refresh_golden_frame = 1;
5281 cm->refresh_golden_frame = 0;
5282 cm->refresh_last_frame = 0;
5284 else
5286 cm->refresh_golden_frame = 0;
5287 cm->refresh_last_frame = 1;
5290 #endif
5292 #if !(CONFIG_REALTIME_ONLY)
5294 if (cpi->pass == 1)
5296 Pass1Encode(cpi, size, dest, frame_flags);
5298 else if (cpi->pass == 2)
5300 Pass2Encode(cpi, size, dest, frame_flags);
5302 else
5303 #endif
5304 encode_frame_to_data_rate(cpi, size, dest, frame_flags);
5306 if (cpi->compressor_speed == 2)
5308 unsigned int duration, duration2;
5309 vpx_usec_timer_mark(&tsctimer);
5310 vpx_usec_timer_mark(&ticktimer);
5312 duration = vpx_usec_timer_elapsed(&ticktimer);
5313 duration2 = (unsigned int)((double)duration / 2);
5315 if (cm->frame_type != KEY_FRAME)
5317 if (cpi->avg_encode_time == 0)
5318 cpi->avg_encode_time = duration;
5319 else
5320 cpi->avg_encode_time = (7 * cpi->avg_encode_time + duration) >> 3;
5323 if (duration2)
5325 //if(*frame_flags!=1)
5328 if (cpi->avg_pick_mode_time == 0)
5329 cpi->avg_pick_mode_time = duration2;
5330 else
5331 cpi->avg_pick_mode_time = (7 * cpi->avg_pick_mode_time + duration2) >> 3;
5337 if (cm->refresh_entropy_probs == 0)
5339 vpx_memcpy(&cm->fc, &cm->lfc, sizeof(cm->fc));
5342 // if its a dropped frame honor the requests on subsequent frames
5343 if (*size > 0)
5346 // return to normal state
5347 cm->refresh_entropy_probs = 1;
5348 cm->refresh_alt_ref_frame = 0;
5349 cm->refresh_golden_frame = 0;
5350 cm->refresh_last_frame = 1;
5351 cm->frame_type = INTER_FRAME;
5355 cpi->ready_for_new_frame = 1;
5357 vpx_usec_timer_mark(&cmptimer);
5358 cpi->time_compress_data += vpx_usec_timer_elapsed(&cmptimer);
5360 if (cpi->b_calculate_psnr && cpi->pass != 1 && cm->show_frame)
5361 generate_psnr_packet(cpi);
5363 #if CONFIG_PSNR
5365 if (cpi->pass != 1)
5367 cpi->bytes += *size;
5369 if (cm->show_frame)
5372 cpi->count ++;
5374 if (cpi->b_calculate_psnr)
5376 double y, u, v;
5377 double sq_error;
5378 double frame_psnr = vp8_calc_psnr(cpi->Source, cm->frame_to_show, &y, &u, &v, &sq_error);
5380 cpi->total_y += y;
5381 cpi->total_u += u;
5382 cpi->total_v += v;
5383 cpi->total_sq_error += sq_error;
5384 cpi->total += frame_psnr;
5386 double y2, u2, v2, frame_psnr2, frame_ssim2 = 0;
5387 double weight = 0;
5389 vp8_deblock(cm->frame_to_show, &cm->post_proc_buffer, cm->filter_level * 10 / 6, 1, 0, IF_RTCD(&cm->rtcd.postproc));
5390 vp8_clear_system_state();
5391 frame_psnr2 = vp8_calc_psnr(cpi->Source, &cm->post_proc_buffer, &y2, &u2, &v2, &sq_error);
5392 frame_ssim2 = vp8_calc_ssim(cpi->Source, &cm->post_proc_buffer, 1, &weight);
5394 cpi->summed_quality += frame_ssim2 * weight;
5395 cpi->summed_weights += weight;
5397 cpi->totalp_y += y2;
5398 cpi->totalp_u += u2;
5399 cpi->totalp_v += v2;
5400 cpi->totalp += frame_psnr2;
5401 cpi->total_sq_error2 += sq_error;
5406 if (cpi->b_calculate_ssimg)
5408 double y, u, v, frame_all;
5409 frame_all = vp8_calc_ssimg(cpi->Source, cm->frame_to_show, &y, &u, &v);
5410 cpi->total_ssimg_y += y;
5411 cpi->total_ssimg_u += u;
5412 cpi->total_ssimg_v += v;
5413 cpi->total_ssimg_all += frame_all;
5419 #if 0
5421 if (cpi->common.frame_type != 0 && cpi->common.base_qindex == cpi->oxcf.worst_allowed_q)
5423 skiptruecount += cpi->skip_true_count;
5424 skipfalsecount += cpi->skip_false_count;
5427 #endif
5428 #if 0
5430 if (cpi->pass != 1)
5432 FILE *f = fopen("skip.stt", "a");
5433 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);
5435 if (cpi->is_src_frame_alt_ref == 1)
5436 fprintf(f, "skipcount: %4d framesize: %d\n", cpi->skip_true_count , *size);
5438 fclose(f);
5441 #endif
5442 #endif
5444 #if HAVE_ARMV7
5445 #if CONFIG_RUNTIME_CPU_DETECT
5446 if (cm->rtcd.flags & HAS_NEON)
5447 #endif
5449 vp8_pop_neon(store_reg);
5451 #endif
5453 return 0;
5456 int vp8_get_preview_raw_frame(VP8_PTR comp, YV12_BUFFER_CONFIG *dest, vp8_ppflags_t *flags)
5458 VP8_COMP *cpi = (VP8_COMP *) comp;
5460 if (cpi->common.refresh_alt_ref_frame)
5461 return -1;
5462 else
5464 int ret;
5465 #if CONFIG_POSTPROC
5466 ret = vp8_post_proc_frame(&cpi->common, dest, flags);
5467 #else
5469 if (cpi->common.frame_to_show)
5471 *dest = *cpi->common.frame_to_show;
5472 dest->y_width = cpi->common.Width;
5473 dest->y_height = cpi->common.Height;
5474 dest->uv_height = cpi->common.Height / 2;
5475 ret = 0;
5477 else
5479 ret = -1;
5482 #endif //!CONFIG_POSTPROC
5483 vp8_clear_system_state();
5484 return ret;
5488 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])
5490 VP8_COMP *cpi = (VP8_COMP *) comp;
5491 signed char feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS];
5493 if (cpi->common.mb_rows != rows || cpi->common.mb_cols != cols)
5494 return -1;
5496 if (!map)
5498 disable_segmentation((VP8_PTR)cpi);
5499 return 0;
5502 // Set the segmentation Map
5503 set_segmentation_map((VP8_PTR)cpi, map);
5505 // Activate segmentation.
5506 enable_segmentation((VP8_PTR)cpi);
5508 // Set up the quant segment data
5509 feature_data[MB_LVL_ALT_Q][0] = delta_q[0];
5510 feature_data[MB_LVL_ALT_Q][1] = delta_q[1];
5511 feature_data[MB_LVL_ALT_Q][2] = delta_q[2];
5512 feature_data[MB_LVL_ALT_Q][3] = delta_q[3];
5514 // Set up the loop segment data s
5515 feature_data[MB_LVL_ALT_LF][0] = delta_lf[0];
5516 feature_data[MB_LVL_ALT_LF][1] = delta_lf[1];
5517 feature_data[MB_LVL_ALT_LF][2] = delta_lf[2];
5518 feature_data[MB_LVL_ALT_LF][3] = delta_lf[3];
5520 cpi->segment_encode_breakout[0] = threshold[0];
5521 cpi->segment_encode_breakout[1] = threshold[1];
5522 cpi->segment_encode_breakout[2] = threshold[2];
5523 cpi->segment_encode_breakout[3] = threshold[3];
5525 // Initialise the feature data structure
5526 // SEGMENT_DELTADATA 0, SEGMENT_ABSDATA 1
5527 set_segment_data((VP8_PTR)cpi, &feature_data[0][0], SEGMENT_DELTADATA);
5529 return 0;
5532 int vp8_set_active_map(VP8_PTR comp, unsigned char *map, unsigned int rows, unsigned int cols)
5534 VP8_COMP *cpi = (VP8_COMP *) comp;
5536 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols)
5538 if (map)
5540 vpx_memcpy(cpi->active_map, map, rows * cols);
5541 cpi->active_map_enabled = 1;
5543 else
5544 cpi->active_map_enabled = 0;
5546 return 0;
5548 else
5550 //cpi->active_map_enabled = 0;
5551 return -1 ;
5555 int vp8_set_internal_size(VP8_PTR comp, VPX_SCALING horiz_mode, VPX_SCALING vert_mode)
5557 VP8_COMP *cpi = (VP8_COMP *) comp;
5559 if (horiz_mode <= ONETWO)
5560 cpi->common.horiz_scale = horiz_mode;
5561 else
5562 return -1;
5564 if (vert_mode <= ONETWO)
5565 cpi->common.vert_scale = vert_mode;
5566 else
5567 return -1;
5569 return 0;
5574 int vp8_calc_ss_err(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *dest, const vp8_variance_rtcd_vtable_t *rtcd)
5576 int i, j;
5577 int Total = 0;
5579 unsigned char *src = source->y_buffer;
5580 unsigned char *dst = dest->y_buffer;
5581 (void)rtcd;
5583 // Loop through the Y plane raw and reconstruction data summing (square differences)
5584 for (i = 0; i < source->y_height; i += 16)
5586 for (j = 0; j < source->y_width; j += 16)
5588 unsigned int sse;
5589 Total += VARIANCE_INVOKE(rtcd, mse16x16)(src + j, source->y_stride, dst + j, dest->y_stride, &sse);
5592 src += 16 * source->y_stride;
5593 dst += 16 * dest->y_stride;
5596 return Total;
5598 int vp8_calc_low_ss_err(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *dest, const vp8_variance_rtcd_vtable_t *rtcd)
5600 int i, j;
5601 int Total = 0;
5603 unsigned char *src = source->y_buffer;
5604 unsigned char *dst = dest->y_buffer;
5605 (void)rtcd;
5607 // Loop through the Y plane raw and reconstruction data summing (square differences)
5608 for (i = 0; i < source->y_height; i += 16)
5610 for (j = 0; j < source->y_width; j += 16)
5612 unsigned int sse;
5613 VARIANCE_INVOKE(rtcd, mse16x16)(src + j, source->y_stride, dst + j, dest->y_stride, &sse);
5615 if (sse < 8096)
5616 Total += sse;
5619 src += 16 * source->y_stride;
5620 dst += 16 * dest->y_stride;
5623 return Total;
5626 int vp8_get_speed(VP8_PTR c)
5628 VP8_COMP *cpi = (VP8_COMP *) c;
5629 return cpi->Speed;
5631 int vp8_get_quantizer(VP8_PTR c)
5633 VP8_COMP *cpi = (VP8_COMP *) c;
5634 return cpi->common.base_qindex;