Removed cpi->target_bits_per_mb
[libvpx.git] / vp8 / encoder / onyx_if.c
blob398321edee87e50066efd4c64ea8b03249e99908
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 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4,
162 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 10,10,
163 11,11,12,12,13,13,14,14,15,15,16,16,17,17,18,18,
164 19,19,20,20,21,21,22,22,23,23,24,24,25,25,26,26,
165 27,27,28,28,29,29,30,30,31,32,33,34,35,36,37,38,
167 static const int kf_high_motion_minq[QINDEX_RANGE] =
169 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
170 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
171 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5,
172 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10,10,
173 11,11,12,12,13,13,14,14,15,15,16,16,17,17,18,18,
174 19,19,20,20,21,21,22,22,23,23,24,24,25,25,26,26,
175 27,27,28,28,29,29,30,30,31,31,32,32,33,33,34,34,
176 35,35,36,36,37,38,39,40,41,42,43,44,45,46,47,48,
178 static const int gf_low_motion_minq[QINDEX_RANGE] =
180 0,0,0,0,1,1,1,1,1,1,1,1,2,2,2,2,
181 3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,
182 7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10,
183 11,11,12,12,13,13,14,14,15,15,16,16,17,17,18,18,
184 19,19,20,20,21,21,22,22,23,23,24,24,25,25,26,26,
185 27,27,28,28,29,29,30,30,31,31,32,32,33,33,34,34,
186 35,35,36,36,37,37,38,38,39,39,40,40,41,41,42,42,
187 43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58
189 static const int gf_mid_motion_minq[QINDEX_RANGE] =
191 0,0,0,0,1,1,1,1,1,1,2,2,3,3,3,4,
192 4,4,5,5,5,6,6,6,7,7,7,8,8,8,9,9,
193 9,10,10,10,10,11,11,11,12,12,12,12,13,13,13,14,
194 14,14,15,15,16,16,17,17,18,18,19,19,20,20,21,21,
195 22,22,23,23,24,24,25,25,26,26,27,27,28,28,29,29,
196 30,30,31,31,32,32,33,33,34,34,35,35,36,36,37,37,
197 38,39,39,40,40,41,41,42,42,43,43,44,45,46,47,48,
198 49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,
200 static const int gf_high_motion_minq[QINDEX_RANGE] =
202 0,0,0,0,1,1,1,1,1,2,2,2,3,3,3,4,
203 4,4,5,5,5,6,6,6,7,7,7,8,8,8,9,9,
204 9,10,10,10,11,11,12,12,13,13,14,14,15,15,16,16,
205 17,17,18,18,19,19,20,20,21,21,22,22,23,23,24,24,
206 25,25,26,26,27,27,28,28,29,29,30,30,31,31,32,32,
207 33,33,34,34,35,35,36,36,37,37,38,38,39,39,40,40,
208 41,41,42,42,43,44,45,46,47,48,49,50,51,52,53,54,
209 55,56,57,58,59,60,62,64,66,68,70,72,74,76,78,80,
211 static const int inter_minq[QINDEX_RANGE] =
213 0,0,1,1,2,3,3,4,4,5,6,6,7,8,8,9,
214 9,10,11,11,12,13,13,14,15,15,16,17,17,18,19,20,
215 20,21,22,22,23,24,24,25,26,27,27,28,29,30,30,31,
216 32,33,33,34,35,36,36,37,38,39,39,40,41,42,42,43,
217 44,45,46,46,47,48,49,50,50,51,52,53,54,55,55,56,
218 57,58,59,60,60,61,62,63,64,65,66,67,67,68,69,70,
219 71,72,73,74,75,75,76,77,78,79,80,81,82,83,84,85,
220 86,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100
223 void vp8_initialize()
225 static int init_done = 0;
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 // Delete last frame MV storage buffers
266 if (cpi->lfmv != 0)
267 vpx_free(cpi->lfmv);
269 cpi->lfmv = 0;
271 if (cpi->lf_ref_frame_sign_bias != 0)
272 vpx_free(cpi->lf_ref_frame_sign_bias);
274 cpi->lf_ref_frame_sign_bias = 0;
276 if (cpi->lf_ref_frame != 0)
277 vpx_free(cpi->lf_ref_frame);
279 cpi->lf_ref_frame = 0;
281 // Delete sementation map
282 if (cpi->segmentation_map != 0)
283 vpx_free(cpi->segmentation_map);
285 cpi->segmentation_map = 0;
287 if (cpi->active_map != 0)
288 vpx_free(cpi->active_map);
290 cpi->active_map = 0;
292 // Delete first pass motion map
293 if (cpi->fp_motion_map != 0)
294 vpx_free(cpi->fp_motion_map);
296 cpi->fp_motion_map = 0;
298 vp8_de_alloc_frame_buffers(&cpi->common);
300 vp8_yv12_de_alloc_frame_buffer(&cpi->last_frame_uf);
301 vp8_yv12_de_alloc_frame_buffer(&cpi->scaled_source);
302 #if VP8_TEMPORAL_ALT_REF
303 vp8_yv12_de_alloc_frame_buffer(&cpi->alt_ref_buffer.source_buffer);
304 #endif
306 int i;
308 for (i = 0; i < MAX_LAG_BUFFERS; i++)
309 vp8_yv12_de_alloc_frame_buffer(&cpi->src_buffer[i].source_buffer);
311 cpi->source_buffer_count = 0;
314 vpx_free(cpi->tok);
315 cpi->tok = 0;
317 // Structure used to minitor GF useage
318 if (cpi->gf_active_flags != 0)
319 vpx_free(cpi->gf_active_flags);
321 cpi->gf_active_flags = 0;
323 if(cpi->mb.pip)
324 vpx_free(cpi->mb.pip);
326 cpi->mb.pip = 0;
328 if(cpi->total_stats)
329 vpx_free(cpi->total_stats);
331 cpi->total_stats = 0;
333 if(cpi->this_frame_stats)
334 vpx_free(cpi->this_frame_stats);
336 cpi->this_frame_stats = 0;
339 static void enable_segmentation(VP8_PTR ptr)
341 VP8_COMP *cpi = (VP8_COMP *)(ptr);
343 // Set the appropriate feature bit
344 cpi->mb.e_mbd.segmentation_enabled = 1;
345 cpi->mb.e_mbd.update_mb_segmentation_map = 1;
346 cpi->mb.e_mbd.update_mb_segmentation_data = 1;
348 static void disable_segmentation(VP8_PTR ptr)
350 VP8_COMP *cpi = (VP8_COMP *)(ptr);
352 // Clear the appropriate feature bit
353 cpi->mb.e_mbd.segmentation_enabled = 0;
356 // Valid values for a segment are 0 to 3
357 // Segmentation map is arrange as [Rows][Columns]
358 static void set_segmentation_map(VP8_PTR ptr, unsigned char *segmentation_map)
360 VP8_COMP *cpi = (VP8_COMP *)(ptr);
362 // Copy in the new segmentation map
363 vpx_memcpy(cpi->segmentation_map, segmentation_map, (cpi->common.mb_rows * cpi->common.mb_cols));
365 // Signal that the map should be updated.
366 cpi->mb.e_mbd.update_mb_segmentation_map = 1;
367 cpi->mb.e_mbd.update_mb_segmentation_data = 1;
370 // The values given for each segment can be either deltas (from the default value chosen for the frame) or absolute values.
372 // Valid range for abs values is (0-127 for MB_LVL_ALT_Q) , (0-63 for SEGMENT_ALT_LF)
373 // Valid range for delta values are (+/-127 for MB_LVL_ALT_Q) , (+/-63 for SEGMENT_ALT_LF)
375 // abs_delta = SEGMENT_DELTADATA (deltas) abs_delta = SEGMENT_ABSDATA (use the absolute values given).
378 static void set_segment_data(VP8_PTR ptr, signed char *feature_data, unsigned char abs_delta)
380 VP8_COMP *cpi = (VP8_COMP *)(ptr);
382 cpi->mb.e_mbd.mb_segement_abs_delta = abs_delta;
383 vpx_memcpy(cpi->segment_feature_data, feature_data, sizeof(cpi->segment_feature_data));
387 static void segmentation_test_function(VP8_PTR ptr)
389 VP8_COMP *cpi = (VP8_COMP *)(ptr);
391 unsigned char *seg_map;
392 signed char feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS];
394 // Create a temporary map for segmentation data.
395 CHECK_MEM_ERROR(seg_map, vpx_calloc(cpi->common.mb_rows * cpi->common.mb_cols, 1));
397 // MB loop to set local segmentation map
398 /*for ( i = 0; i < cpi->common.mb_rows; i++ )
400 for ( j = 0; j < cpi->common.mb_cols; j++ )
402 //seg_map[(i*cpi->common.mb_cols) + j] = (j % 2) + ((i%2)* 2);
403 //if ( j < cpi->common.mb_cols/2 )
405 // Segment 1 around the edge else 0
406 if ( (i == 0) || (j == 0) || (i == (cpi->common.mb_rows-1)) || (j == (cpi->common.mb_cols-1)) )
407 seg_map[(i*cpi->common.mb_cols) + j] = 1;
408 //else if ( (i < 2) || (j < 2) || (i > (cpi->common.mb_rows-3)) || (j > (cpi->common.mb_cols-3)) )
409 // seg_map[(i*cpi->common.mb_cols) + j] = 2;
410 //else if ( (i < 5) || (j < 5) || (i > (cpi->common.mb_rows-6)) || (j > (cpi->common.mb_cols-6)) )
411 // seg_map[(i*cpi->common.mb_cols) + j] = 3;
412 else
413 seg_map[(i*cpi->common.mb_cols) + j] = 0;
417 // Set the segmentation Map
418 set_segmentation_map(ptr, seg_map);
420 // Activate segmentation.
421 enable_segmentation(ptr);
423 // Set up the quant segment data
424 feature_data[MB_LVL_ALT_Q][0] = 0;
425 feature_data[MB_LVL_ALT_Q][1] = 4;
426 feature_data[MB_LVL_ALT_Q][2] = 0;
427 feature_data[MB_LVL_ALT_Q][3] = 0;
428 // Set up the loop segment data
429 feature_data[MB_LVL_ALT_LF][0] = 0;
430 feature_data[MB_LVL_ALT_LF][1] = 0;
431 feature_data[MB_LVL_ALT_LF][2] = 0;
432 feature_data[MB_LVL_ALT_LF][3] = 0;
434 // Initialise the feature data structure
435 // SEGMENT_DELTADATA 0, SEGMENT_ABSDATA 1
436 set_segment_data(ptr, &feature_data[0][0], SEGMENT_DELTADATA);
438 // Delete sementation map
439 if (seg_map != 0)
440 vpx_free(seg_map);
442 seg_map = 0;
446 // A simple function to cyclically refresh the background at a lower Q
447 static void cyclic_background_refresh(VP8_COMP *cpi, int Q, int lf_adjustment)
449 unsigned char *seg_map;
450 signed char feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS];
451 int i;
452 int block_count = cpi->cyclic_refresh_mode_max_mbs_perframe;
453 int mbs_in_frame = cpi->common.mb_rows * cpi->common.mb_cols;
455 // Create a temporary map for segmentation data.
456 CHECK_MEM_ERROR(seg_map, vpx_calloc(cpi->common.mb_rows * cpi->common.mb_cols, 1));
458 cpi->cyclic_refresh_q = Q;
460 for (i = Q; i > 0; i--)
462 if (vp8_bits_per_mb[cpi->common.frame_type][i] >= ((vp8_bits_per_mb[cpi->common.frame_type][Q]*(Q + 128)) / 64))
463 //if ( vp8_bits_per_mb[cpi->common.frame_type][i] >= ((vp8_bits_per_mb[cpi->common.frame_type][Q]*((2*Q)+96))/64) )
465 break;
469 cpi->cyclic_refresh_q = i;
471 // Only update for inter frames
472 if (cpi->common.frame_type != KEY_FRAME)
474 // Cycle through the macro_block rows
475 // MB loop to set local segmentation map
476 for (i = cpi->cyclic_refresh_mode_index; i < mbs_in_frame; i++)
478 // If the MB is as a candidate for clean up then mark it for possible boost/refresh (segment 1)
479 // The segment id may get reset to 0 later if the MB gets coded anything other than last frame 0,0
480 // as only (last frame 0,0) MBs are eligable for refresh : that is to say Mbs likely to be background blocks.
481 if (cpi->cyclic_refresh_map[i] == 0)
483 seg_map[i] = 1;
485 else
487 seg_map[i] = 0;
489 // Skip blocks that have been refreshed recently anyway.
490 if (cpi->cyclic_refresh_map[i] < 0)
491 //cpi->cyclic_refresh_map[i] = cpi->cyclic_refresh_map[i] / 16;
492 cpi->cyclic_refresh_map[i]++;
496 if (block_count > 0)
497 block_count--;
498 else
499 break;
503 // If we have gone through the frame reset to the start
504 cpi->cyclic_refresh_mode_index = i;
506 if (cpi->cyclic_refresh_mode_index >= mbs_in_frame)
507 cpi->cyclic_refresh_mode_index = 0;
510 // Set the segmentation Map
511 set_segmentation_map((VP8_PTR)cpi, seg_map);
513 // Activate segmentation.
514 enable_segmentation((VP8_PTR)cpi);
516 // Set up the quant segment data
517 feature_data[MB_LVL_ALT_Q][0] = 0;
518 feature_data[MB_LVL_ALT_Q][1] = (cpi->cyclic_refresh_q - Q);
519 feature_data[MB_LVL_ALT_Q][2] = 0;
520 feature_data[MB_LVL_ALT_Q][3] = 0;
522 // Set up the loop segment data
523 feature_data[MB_LVL_ALT_LF][0] = 0;
524 feature_data[MB_LVL_ALT_LF][1] = lf_adjustment;
525 feature_data[MB_LVL_ALT_LF][2] = 0;
526 feature_data[MB_LVL_ALT_LF][3] = 0;
528 // Initialise the feature data structure
529 // SEGMENT_DELTADATA 0, SEGMENT_ABSDATA 1
530 set_segment_data((VP8_PTR)cpi, &feature_data[0][0], SEGMENT_DELTADATA);
532 // Delete sementation map
533 if (seg_map != 0)
534 vpx_free(seg_map);
536 seg_map = 0;
540 static void set_default_lf_deltas(VP8_COMP *cpi)
542 cpi->mb.e_mbd.mode_ref_lf_delta_enabled = 1;
543 cpi->mb.e_mbd.mode_ref_lf_delta_update = 1;
545 vpx_memset(cpi->mb.e_mbd.ref_lf_deltas, 0, sizeof(cpi->mb.e_mbd.ref_lf_deltas));
546 vpx_memset(cpi->mb.e_mbd.mode_lf_deltas, 0, sizeof(cpi->mb.e_mbd.mode_lf_deltas));
548 // Test of ref frame deltas
549 cpi->mb.e_mbd.ref_lf_deltas[INTRA_FRAME] = 2;
550 cpi->mb.e_mbd.ref_lf_deltas[LAST_FRAME] = 0;
551 cpi->mb.e_mbd.ref_lf_deltas[GOLDEN_FRAME] = -2;
552 cpi->mb.e_mbd.ref_lf_deltas[ALTREF_FRAME] = -2;
554 cpi->mb.e_mbd.mode_lf_deltas[0] = 4; // BPRED
555 cpi->mb.e_mbd.mode_lf_deltas[1] = -2; // Zero
556 cpi->mb.e_mbd.mode_lf_deltas[2] = 2; // New mv
557 cpi->mb.e_mbd.mode_lf_deltas[3] = 4; // Split mv
560 void vp8_set_speed_features(VP8_COMP *cpi)
562 SPEED_FEATURES *sf = &cpi->sf;
563 int Mode = cpi->compressor_speed;
564 int Speed = cpi->Speed;
565 int i;
566 VP8_COMMON *cm = &cpi->common;
567 int last_improved_quant = sf->improved_quant;
569 // Initialise default mode frequency sampling variables
570 for (i = 0; i < MAX_MODES; i ++)
572 cpi->mode_check_freq[i] = 0;
573 cpi->mode_test_hit_counts[i] = 0;
574 cpi->mode_chosen_counts[i] = 0;
577 cpi->mbs_tested_so_far = 0;
579 // best quality
580 sf->RD = 1;
581 sf->search_method = NSTEP;
582 sf->improved_quant = 1;
583 sf->improved_dct = 1;
584 sf->auto_filter = 1;
585 sf->recode_loop = 1;
586 sf->quarter_pixel_search = 1;
587 sf->half_pixel_search = 1;
588 sf->full_freq[0] = 7;
589 sf->full_freq[1] = 7;
590 sf->min_fs_radius = 8;
591 sf->max_fs_radius = 32;
592 sf->iterative_sub_pixel = 1;
593 sf->optimize_coefficients = 1;
594 sf->use_fastquant_for_pick = 0;
596 sf->first_step = 0;
597 sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
599 cpi->do_full[0] = 0;
600 cpi->do_full[1] = 0;
602 // default thresholds to 0
603 for (i = 0; i < MAX_MODES; i++)
604 sf->thresh_mult[i] = 0;
606 switch (Mode)
608 #if !(CONFIG_REALTIME_ONLY)
609 case 0: // best quality mode
610 sf->thresh_mult[THR_ZEROMV ] = 0;
611 sf->thresh_mult[THR_ZEROG ] = 0;
612 sf->thresh_mult[THR_ZEROA ] = 0;
613 sf->thresh_mult[THR_NEARESTMV] = 0;
614 sf->thresh_mult[THR_NEARESTG ] = 0;
615 sf->thresh_mult[THR_NEARESTA ] = 0;
616 sf->thresh_mult[THR_NEARMV ] = 0;
617 sf->thresh_mult[THR_NEARG ] = 0;
618 sf->thresh_mult[THR_NEARA ] = 0;
620 sf->thresh_mult[THR_DC ] = 0;
622 sf->thresh_mult[THR_V_PRED ] = 1000;
623 sf->thresh_mult[THR_H_PRED ] = 1000;
624 sf->thresh_mult[THR_B_PRED ] = 2000;
625 sf->thresh_mult[THR_TM ] = 1000;
627 sf->thresh_mult[THR_NEWMV ] = 1000;
628 sf->thresh_mult[THR_NEWG ] = 1000;
629 sf->thresh_mult[THR_NEWA ] = 1000;
631 sf->thresh_mult[THR_SPLITMV ] = 2500;
632 sf->thresh_mult[THR_SPLITG ] = 5000;
633 sf->thresh_mult[THR_SPLITA ] = 5000;
635 sf->full_freq[0] = 7;
636 sf->full_freq[1] = 15;
638 sf->first_step = 0;
639 sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
641 if (!(cpi->ref_frame_flags & VP8_LAST_FLAG))
643 sf->thresh_mult[THR_NEWMV ] = INT_MAX;
644 sf->thresh_mult[THR_NEARESTMV] = INT_MAX;
645 sf->thresh_mult[THR_ZEROMV ] = INT_MAX;
646 sf->thresh_mult[THR_NEARMV ] = INT_MAX;
647 sf->thresh_mult[THR_SPLITMV ] = INT_MAX;
650 if (!(cpi->ref_frame_flags & VP8_GOLD_FLAG))
652 sf->thresh_mult[THR_NEARESTG ] = INT_MAX;
653 sf->thresh_mult[THR_ZEROG ] = INT_MAX;
654 sf->thresh_mult[THR_NEARG ] = INT_MAX;
655 sf->thresh_mult[THR_NEWG ] = INT_MAX;
656 sf->thresh_mult[THR_SPLITG ] = INT_MAX;
659 if (!(cpi->ref_frame_flags & VP8_ALT_FLAG))
661 sf->thresh_mult[THR_NEARESTA ] = INT_MAX;
662 sf->thresh_mult[THR_ZEROA ] = INT_MAX;
663 sf->thresh_mult[THR_NEARA ] = INT_MAX;
664 sf->thresh_mult[THR_NEWA ] = INT_MAX;
665 sf->thresh_mult[THR_SPLITA ] = INT_MAX;
668 break;
669 case 1:
670 case 3:
671 sf->thresh_mult[THR_NEARESTMV] = 0;
672 sf->thresh_mult[THR_ZEROMV ] = 0;
673 sf->thresh_mult[THR_DC ] = 0;
674 sf->thresh_mult[THR_NEARMV ] = 0;
675 sf->thresh_mult[THR_V_PRED ] = 1000;
676 sf->thresh_mult[THR_H_PRED ] = 1000;
677 sf->thresh_mult[THR_B_PRED ] = 2500;
678 sf->thresh_mult[THR_TM ] = 1000;
680 sf->thresh_mult[THR_NEARESTG ] = 1000;
681 sf->thresh_mult[THR_NEARESTA ] = 1000;
683 sf->thresh_mult[THR_ZEROG ] = 1000;
684 sf->thresh_mult[THR_ZEROA ] = 1000;
685 sf->thresh_mult[THR_NEARG ] = 1000;
686 sf->thresh_mult[THR_NEARA ] = 1000;
688 #if 1
689 sf->thresh_mult[THR_ZEROMV ] = 0;
690 sf->thresh_mult[THR_ZEROG ] = 0;
691 sf->thresh_mult[THR_ZEROA ] = 0;
692 sf->thresh_mult[THR_NEARESTMV] = 0;
693 sf->thresh_mult[THR_NEARESTG ] = 0;
694 sf->thresh_mult[THR_NEARESTA ] = 0;
695 sf->thresh_mult[THR_NEARMV ] = 0;
696 sf->thresh_mult[THR_NEARG ] = 0;
697 sf->thresh_mult[THR_NEARA ] = 0;
699 // sf->thresh_mult[THR_DC ] = 0;
701 // sf->thresh_mult[THR_V_PRED ] = 1000;
702 // sf->thresh_mult[THR_H_PRED ] = 1000;
703 // sf->thresh_mult[THR_B_PRED ] = 2000;
704 // sf->thresh_mult[THR_TM ] = 1000;
706 sf->thresh_mult[THR_NEWMV ] = 1000;
707 sf->thresh_mult[THR_NEWG ] = 1000;
708 sf->thresh_mult[THR_NEWA ] = 1000;
710 sf->thresh_mult[THR_SPLITMV ] = 1700;
711 sf->thresh_mult[THR_SPLITG ] = 4500;
712 sf->thresh_mult[THR_SPLITA ] = 4500;
713 #else
714 sf->thresh_mult[THR_NEWMV ] = 1500;
715 sf->thresh_mult[THR_NEWG ] = 1500;
716 sf->thresh_mult[THR_NEWA ] = 1500;
718 sf->thresh_mult[THR_SPLITMV ] = 5000;
719 sf->thresh_mult[THR_SPLITG ] = 10000;
720 sf->thresh_mult[THR_SPLITA ] = 10000;
721 #endif
722 sf->full_freq[0] = 15;
723 sf->full_freq[1] = 31;
725 sf->first_step = 0;
726 sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
728 if (!(cpi->ref_frame_flags & VP8_LAST_FLAG))
730 sf->thresh_mult[THR_NEWMV ] = INT_MAX;
731 sf->thresh_mult[THR_NEARESTMV] = INT_MAX;
732 sf->thresh_mult[THR_ZEROMV ] = INT_MAX;
733 sf->thresh_mult[THR_NEARMV ] = INT_MAX;
734 sf->thresh_mult[THR_SPLITMV ] = INT_MAX;
737 if (!(cpi->ref_frame_flags & VP8_GOLD_FLAG))
739 sf->thresh_mult[THR_NEARESTG ] = INT_MAX;
740 sf->thresh_mult[THR_ZEROG ] = INT_MAX;
741 sf->thresh_mult[THR_NEARG ] = INT_MAX;
742 sf->thresh_mult[THR_NEWG ] = INT_MAX;
743 sf->thresh_mult[THR_SPLITG ] = INT_MAX;
746 if (!(cpi->ref_frame_flags & VP8_ALT_FLAG))
748 sf->thresh_mult[THR_NEARESTA ] = INT_MAX;
749 sf->thresh_mult[THR_ZEROA ] = INT_MAX;
750 sf->thresh_mult[THR_NEARA ] = INT_MAX;
751 sf->thresh_mult[THR_NEWA ] = INT_MAX;
752 sf->thresh_mult[THR_SPLITA ] = INT_MAX;
755 if (Speed > 0)
757 // Disable coefficient optimization above speed 0
758 sf->optimize_coefficients = 0;
760 cpi->mode_check_freq[THR_SPLITG] = 4;
761 cpi->mode_check_freq[THR_SPLITA] = 4;
762 cpi->mode_check_freq[THR_SPLITMV] = 2;
764 sf->thresh_mult[THR_TM ] = 1500;
765 sf->thresh_mult[THR_V_PRED ] = 1500;
766 sf->thresh_mult[THR_H_PRED ] = 1500;
767 sf->thresh_mult[THR_B_PRED ] = 5000;
769 if (cpi->ref_frame_flags & VP8_LAST_FLAG)
771 sf->thresh_mult[THR_NEWMV ] = 2000;
772 sf->thresh_mult[THR_SPLITMV ] = 10000;
775 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
777 sf->thresh_mult[THR_NEARESTG ] = 1500;
778 sf->thresh_mult[THR_ZEROG ] = 1500;
779 sf->thresh_mult[THR_NEARG ] = 1500;
780 sf->thresh_mult[THR_NEWG ] = 2000;
781 sf->thresh_mult[THR_SPLITG ] = 20000;
784 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
786 sf->thresh_mult[THR_NEARESTA ] = 1500;
787 sf->thresh_mult[THR_ZEROA ] = 1500;
788 sf->thresh_mult[THR_NEARA ] = 1500;
789 sf->thresh_mult[THR_NEWA ] = 2000;
790 sf->thresh_mult[THR_SPLITA ] = 20000;
793 sf->use_fastquant_for_pick = 1;
795 sf->first_step = 1;
796 sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
799 if (Speed > 1)
801 sf->use_fastquant_for_pick = 0;
803 cpi->mode_check_freq[THR_SPLITG] = 15;
804 cpi->mode_check_freq[THR_SPLITA] = 15;
805 cpi->mode_check_freq[THR_SPLITMV] = 7;
807 sf->thresh_mult[THR_TM ] = 2000;
808 sf->thresh_mult[THR_V_PRED ] = 2000;
809 sf->thresh_mult[THR_H_PRED ] = 2000;
810 sf->thresh_mult[THR_B_PRED ] = 7500;
812 if (cpi->ref_frame_flags & VP8_LAST_FLAG)
814 sf->thresh_mult[THR_NEWMV ] = 2000;
815 sf->thresh_mult[THR_SPLITMV ] = 25000;
818 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
820 sf->thresh_mult[THR_NEARESTG ] = 2000;
821 sf->thresh_mult[THR_ZEROG ] = 2000;
822 sf->thresh_mult[THR_NEARG ] = 2000;
823 sf->thresh_mult[THR_NEWG ] = 2500;
824 sf->thresh_mult[THR_SPLITG ] = 50000;
827 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
829 sf->thresh_mult[THR_NEARESTA ] = 2000;
830 sf->thresh_mult[THR_ZEROA ] = 2000;
831 sf->thresh_mult[THR_NEARA ] = 2000;
832 sf->thresh_mult[THR_NEWA ] = 2500;
833 sf->thresh_mult[THR_SPLITA ] = 50000;
836 sf->first_step = 1;
838 sf->improved_quant = 0;
839 sf->improved_dct = 0;
841 // Only do recode loop on key frames, golden frames and
842 // alt ref frames
843 sf->recode_loop = 2;
845 sf->full_freq[0] = 31;
846 sf->full_freq[1] = 63;
850 if (Speed > 2)
852 sf->auto_filter = 0; // Faster selection of loop filter
853 cpi->mode_check_freq[THR_V_PRED] = 2;
854 cpi->mode_check_freq[THR_H_PRED] = 2;
855 cpi->mode_check_freq[THR_B_PRED] = 2;
857 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
859 cpi->mode_check_freq[THR_NEARG] = 2;
860 cpi->mode_check_freq[THR_NEWG] = 4;
863 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
865 cpi->mode_check_freq[THR_NEARA] = 2;
866 cpi->mode_check_freq[THR_NEWA] = 4;
869 sf->thresh_mult[THR_SPLITA ] = INT_MAX;
870 sf->thresh_mult[THR_SPLITG ] = INT_MAX;
871 sf->thresh_mult[THR_SPLITMV ] = INT_MAX;
873 sf->full_freq[0] = 63;
874 sf->full_freq[1] = 127;
877 if (Speed > 3)
879 cpi->mode_check_freq[THR_V_PRED] = 0;
880 cpi->mode_check_freq[THR_H_PRED] = 0;
881 cpi->mode_check_freq[THR_B_PRED] = 0;
882 cpi->mode_check_freq[THR_NEARG] = 0;
883 cpi->mode_check_freq[THR_NEWG] = 0;
884 cpi->mode_check_freq[THR_NEARA] = 0;
885 cpi->mode_check_freq[THR_NEWA] = 0;
887 sf->auto_filter = 1;
888 sf->recode_loop = 0; // recode loop off
889 sf->RD = 0; // Turn rd off
890 sf->full_freq[0] = INT_MAX;
891 sf->full_freq[1] = INT_MAX;
894 if (Speed > 4)
896 sf->auto_filter = 0; // Faster selection of loop filter
898 cpi->mode_check_freq[THR_V_PRED] = 2;
899 cpi->mode_check_freq[THR_H_PRED] = 2;
900 cpi->mode_check_freq[THR_B_PRED] = 2;
902 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
904 cpi->mode_check_freq[THR_NEARG] = 2;
905 cpi->mode_check_freq[THR_NEWG] = 4;
908 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
910 cpi->mode_check_freq[THR_NEARA] = 2;
911 cpi->mode_check_freq[THR_NEWA] = 4;
914 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
916 sf->thresh_mult[THR_NEARESTG ] = 2000;
917 sf->thresh_mult[THR_ZEROG ] = 2000;
918 sf->thresh_mult[THR_NEARG ] = 2000;
919 sf->thresh_mult[THR_NEWG ] = 4000;
922 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
924 sf->thresh_mult[THR_NEARESTA ] = 2000;
925 sf->thresh_mult[THR_ZEROA ] = 2000;
926 sf->thresh_mult[THR_NEARA ] = 2000;
927 sf->thresh_mult[THR_NEWA ] = 4000;
931 break;
932 #endif
933 case 2:
934 sf->optimize_coefficients = 0;
935 sf->recode_loop = 0;
936 sf->auto_filter = 1;
937 sf->iterative_sub_pixel = 1;
938 sf->thresh_mult[THR_NEARESTMV] = 0;
939 sf->thresh_mult[THR_ZEROMV ] = 0;
940 sf->thresh_mult[THR_DC ] = 0;
941 sf->thresh_mult[THR_TM ] = 0;
942 sf->thresh_mult[THR_NEARMV ] = 0;
943 sf->thresh_mult[THR_V_PRED ] = 1000;
944 sf->thresh_mult[THR_H_PRED ] = 1000;
945 sf->thresh_mult[THR_B_PRED ] = 2500;
946 sf->thresh_mult[THR_NEARESTG ] = 1000;
947 sf->thresh_mult[THR_ZEROG ] = 1000;
948 sf->thresh_mult[THR_NEARG ] = 1000;
949 sf->thresh_mult[THR_NEARESTA ] = 1000;
950 sf->thresh_mult[THR_ZEROA ] = 1000;
951 sf->thresh_mult[THR_NEARA ] = 1000;
952 sf->thresh_mult[THR_NEWMV ] = 2000;
953 sf->thresh_mult[THR_NEWG ] = 2000;
954 sf->thresh_mult[THR_NEWA ] = 2000;
955 sf->thresh_mult[THR_SPLITMV ] = 5000;
956 sf->thresh_mult[THR_SPLITG ] = 10000;
957 sf->thresh_mult[THR_SPLITA ] = 10000;
958 sf->full_freq[0] = 15;
959 sf->full_freq[1] = 31;
960 sf->search_method = NSTEP;
962 if (!(cpi->ref_frame_flags & VP8_LAST_FLAG))
964 sf->thresh_mult[THR_NEWMV ] = INT_MAX;
965 sf->thresh_mult[THR_NEARESTMV] = INT_MAX;
966 sf->thresh_mult[THR_ZEROMV ] = INT_MAX;
967 sf->thresh_mult[THR_NEARMV ] = INT_MAX;
968 sf->thresh_mult[THR_SPLITMV ] = INT_MAX;
971 if (!(cpi->ref_frame_flags & VP8_GOLD_FLAG))
973 sf->thresh_mult[THR_NEARESTG ] = INT_MAX;
974 sf->thresh_mult[THR_ZEROG ] = INT_MAX;
975 sf->thresh_mult[THR_NEARG ] = INT_MAX;
976 sf->thresh_mult[THR_NEWG ] = INT_MAX;
977 sf->thresh_mult[THR_SPLITG ] = INT_MAX;
980 if (!(cpi->ref_frame_flags & VP8_ALT_FLAG))
982 sf->thresh_mult[THR_NEARESTA ] = INT_MAX;
983 sf->thresh_mult[THR_ZEROA ] = INT_MAX;
984 sf->thresh_mult[THR_NEARA ] = INT_MAX;
985 sf->thresh_mult[THR_NEWA ] = INT_MAX;
986 sf->thresh_mult[THR_SPLITA ] = INT_MAX;
989 if (Speed > 0)
991 cpi->mode_check_freq[THR_SPLITG] = 4;
992 cpi->mode_check_freq[THR_SPLITA] = 4;
993 cpi->mode_check_freq[THR_SPLITMV] = 2;
995 sf->thresh_mult[THR_DC ] = 0;
996 sf->thresh_mult[THR_TM ] = 1000;
997 sf->thresh_mult[THR_V_PRED ] = 2000;
998 sf->thresh_mult[THR_H_PRED ] = 2000;
999 sf->thresh_mult[THR_B_PRED ] = 5000;
1001 if (cpi->ref_frame_flags & VP8_LAST_FLAG)
1003 sf->thresh_mult[THR_NEARESTMV] = 0;
1004 sf->thresh_mult[THR_ZEROMV ] = 0;
1005 sf->thresh_mult[THR_NEARMV ] = 0;
1006 sf->thresh_mult[THR_NEWMV ] = 2000;
1007 sf->thresh_mult[THR_SPLITMV ] = 10000;
1010 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
1012 sf->thresh_mult[THR_NEARESTG ] = 1000;
1013 sf->thresh_mult[THR_ZEROG ] = 1000;
1014 sf->thresh_mult[THR_NEARG ] = 1000;
1015 sf->thresh_mult[THR_NEWG ] = 2000;
1016 sf->thresh_mult[THR_SPLITG ] = 20000;
1019 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
1021 sf->thresh_mult[THR_NEARESTA ] = 1000;
1022 sf->thresh_mult[THR_ZEROA ] = 1000;
1023 sf->thresh_mult[THR_NEARA ] = 1000;
1024 sf->thresh_mult[THR_NEWA ] = 2000;
1025 sf->thresh_mult[THR_SPLITA ] = 20000;
1028 sf->improved_quant = 0;
1029 sf->improved_dct = 0;
1032 if (Speed > 1)
1034 cpi->mode_check_freq[THR_SPLITMV] = 7;
1035 cpi->mode_check_freq[THR_SPLITG] = 15;
1036 cpi->mode_check_freq[THR_SPLITA] = 15;
1038 sf->thresh_mult[THR_TM ] = 2000;
1039 sf->thresh_mult[THR_V_PRED ] = 2000;
1040 sf->thresh_mult[THR_H_PRED ] = 2000;
1041 sf->thresh_mult[THR_B_PRED ] = 5000;
1043 if (cpi->ref_frame_flags & VP8_LAST_FLAG)
1045 sf->thresh_mult[THR_NEWMV ] = 2000;
1046 sf->thresh_mult[THR_SPLITMV ] = 25000;
1049 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
1051 sf->thresh_mult[THR_NEARESTG ] = 2000;
1052 sf->thresh_mult[THR_ZEROG ] = 2000;
1053 sf->thresh_mult[THR_NEARG ] = 2000;
1054 sf->thresh_mult[THR_NEWG ] = 2500;
1055 sf->thresh_mult[THR_SPLITG ] = 50000;
1058 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
1060 sf->thresh_mult[THR_NEARESTA ] = 2000;
1061 sf->thresh_mult[THR_ZEROA ] = 2000;
1062 sf->thresh_mult[THR_NEARA ] = 2000;
1063 sf->thresh_mult[THR_NEWA ] = 2500;
1064 sf->thresh_mult[THR_SPLITA ] = 50000;
1067 sf->full_freq[0] = 31;
1068 sf->full_freq[1] = 63;
1071 if (Speed > 2)
1073 sf->auto_filter = 0; // Faster selection of loop filter
1075 cpi->mode_check_freq[THR_V_PRED] = 2;
1076 cpi->mode_check_freq[THR_H_PRED] = 2;
1077 cpi->mode_check_freq[THR_B_PRED] = 2;
1079 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
1081 cpi->mode_check_freq[THR_NEARG] = 2;
1082 cpi->mode_check_freq[THR_NEWG] = 4;
1085 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
1087 cpi->mode_check_freq[THR_NEARA] = 2;
1088 cpi->mode_check_freq[THR_NEWA] = 4;
1091 sf->thresh_mult[THR_SPLITMV ] = INT_MAX;
1092 sf->thresh_mult[THR_SPLITG ] = INT_MAX;
1093 sf->thresh_mult[THR_SPLITA ] = INT_MAX;
1095 sf->full_freq[0] = 63;
1096 sf->full_freq[1] = 127;
1099 if (Speed > 3)
1101 sf->RD = 0;
1102 sf->full_freq[0] = INT_MAX;
1103 sf->full_freq[1] = INT_MAX;
1105 sf->auto_filter = 1;
1108 if (Speed > 4)
1110 sf->auto_filter = 0; // Faster selection of loop filter
1112 #if CONFIG_REALTIME_ONLY
1113 sf->search_method = HEX;
1114 #else
1115 sf->search_method = DIAMOND;
1116 #endif
1118 cpi->mode_check_freq[THR_V_PRED] = 4;
1119 cpi->mode_check_freq[THR_H_PRED] = 4;
1120 cpi->mode_check_freq[THR_B_PRED] = 4;
1122 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
1124 cpi->mode_check_freq[THR_NEARG] = 2;
1125 cpi->mode_check_freq[THR_NEWG] = 4;
1128 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
1130 cpi->mode_check_freq[THR_NEARA] = 2;
1131 cpi->mode_check_freq[THR_NEWA] = 4;
1134 sf->thresh_mult[THR_TM ] = 2000;
1135 sf->thresh_mult[THR_B_PRED ] = 5000;
1137 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
1139 sf->thresh_mult[THR_NEARESTG ] = 2000;
1140 sf->thresh_mult[THR_ZEROG ] = 2000;
1141 sf->thresh_mult[THR_NEARG ] = 2000;
1142 sf->thresh_mult[THR_NEWG ] = 4000;
1145 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
1147 sf->thresh_mult[THR_NEARESTA ] = 2000;
1148 sf->thresh_mult[THR_ZEROA ] = 2000;
1149 sf->thresh_mult[THR_NEARA ] = 2000;
1150 sf->thresh_mult[THR_NEWA ] = 4000;
1154 if (Speed > 5)
1156 // Disable split MB intra prediction mode
1157 sf->thresh_mult[THR_B_PRED] = INT_MAX;
1160 if (Speed > 6)
1162 unsigned int i, sum = 0;
1163 unsigned int total_mbs = cm->MBs;
1164 int thresh;
1165 int total_skip;
1167 int min = 2000;
1168 sf->iterative_sub_pixel = 0;
1170 if (cpi->oxcf.encode_breakout > 2000)
1171 min = cpi->oxcf.encode_breakout;
1173 min >>= 7;
1175 for (i = 0; i < min; i++)
1177 sum += cpi->error_bins[i];
1180 total_skip = sum;
1181 sum = 0;
1183 // i starts from 2 to make sure thresh started from 2048
1184 for (; i < 1024; i++)
1186 sum += cpi->error_bins[i];
1188 if (10 * sum >= (unsigned int)(cpi->Speed - 6)*(total_mbs - total_skip))
1189 break;
1192 i--;
1193 thresh = (i << 7);
1195 if (thresh < 2000)
1196 thresh = 2000;
1198 if (cpi->ref_frame_flags & VP8_LAST_FLAG)
1200 sf->thresh_mult[THR_NEWMV] = thresh;
1201 sf->thresh_mult[THR_NEARESTMV ] = thresh >> 1;
1202 sf->thresh_mult[THR_NEARMV ] = thresh >> 1;
1205 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
1207 sf->thresh_mult[THR_NEWG] = thresh << 1;
1208 sf->thresh_mult[THR_NEARESTG ] = thresh;
1209 sf->thresh_mult[THR_NEARG ] = thresh;
1212 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
1214 sf->thresh_mult[THR_NEWA] = thresh << 1;
1215 sf->thresh_mult[THR_NEARESTA ] = thresh;
1216 sf->thresh_mult[THR_NEARA ] = thresh;
1219 // Disable other intra prediction modes
1220 sf->thresh_mult[THR_TM] = INT_MAX;
1221 sf->thresh_mult[THR_V_PRED] = INT_MAX;
1222 sf->thresh_mult[THR_H_PRED] = INT_MAX;
1226 if (Speed > 8)
1228 sf->quarter_pixel_search = 0;
1231 if (Speed > 9)
1233 int Tmp = cpi->Speed - 8;
1235 if (Tmp > 4)
1236 Tmp = 4;
1238 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
1240 cpi->mode_check_freq[THR_ZEROG] = 1 << (Tmp - 1);
1241 cpi->mode_check_freq[THR_NEARESTG] = 1 << (Tmp - 1);
1242 cpi->mode_check_freq[THR_NEARG] = 1 << Tmp;
1243 cpi->mode_check_freq[THR_NEWG] = 1 << (Tmp + 1);
1246 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
1248 cpi->mode_check_freq[THR_ZEROA] = 1 << (Tmp - 1);
1249 cpi->mode_check_freq[THR_NEARESTA] = 1 << (Tmp - 1);
1250 cpi->mode_check_freq[THR_NEARA] = 1 << Tmp;
1251 cpi->mode_check_freq[THR_NEWA] = 1 << (Tmp + 1);
1254 cpi->mode_check_freq[THR_NEWMV] = 1 << (Tmp - 1);
1257 cm->filter_type = NORMAL_LOOPFILTER;
1259 if (Speed >= 14)
1260 cm->filter_type = SIMPLE_LOOPFILTER;
1262 if (Speed >= 15)
1264 sf->half_pixel_search = 0; // This has a big hit on quality. Last resort
1267 vpx_memset(cpi->error_bins, 0, sizeof(cpi->error_bins));
1271 if (cpi->sf.search_method == NSTEP)
1273 vp8_init3smotion_compensation(&cpi->mb, cm->yv12_fb[cm->lst_fb_idx].y_stride);
1275 else if (cpi->sf.search_method == DIAMOND)
1277 vp8_init_dsmotion_compensation(&cpi->mb, cm->yv12_fb[cm->lst_fb_idx].y_stride);
1280 if (cpi->sf.improved_dct)
1282 cpi->mb.vp8_short_fdct8x4 = FDCT_INVOKE(&cpi->rtcd.fdct, short8x4);
1283 cpi->mb.vp8_short_fdct4x4 = FDCT_INVOKE(&cpi->rtcd.fdct, short4x4);
1285 else
1287 cpi->mb.vp8_short_fdct8x4 = FDCT_INVOKE(&cpi->rtcd.fdct, fast8x4);
1288 cpi->mb.vp8_short_fdct4x4 = FDCT_INVOKE(&cpi->rtcd.fdct, fast4x4);
1291 cpi->mb.short_walsh4x4 = FDCT_INVOKE(&cpi->rtcd.fdct, walsh_short4x4);
1293 if (cpi->sf.improved_quant)
1295 cpi->mb.quantize_b = QUANTIZE_INVOKE(&cpi->rtcd.quantize, quantb);
1297 else
1299 cpi->mb.quantize_b = QUANTIZE_INVOKE(&cpi->rtcd.quantize, fastquantb);
1301 if (cpi->sf.improved_quant != last_improved_quant)
1302 vp8cx_init_quantizer(cpi);
1304 #if CONFIG_RUNTIME_CPU_DETECT
1305 cpi->mb.e_mbd.rtcd = &cpi->common.rtcd;
1306 #endif
1308 if (cpi->sf.iterative_sub_pixel == 1)
1310 cpi->find_fractional_mv_step = vp8_find_best_sub_pixel_step_iteratively;
1312 else if (cpi->sf.quarter_pixel_search)
1314 cpi->find_fractional_mv_step = vp8_find_best_sub_pixel_step;
1316 else if (cpi->sf.half_pixel_search)
1318 cpi->find_fractional_mv_step = vp8_find_best_half_pixel_step;
1320 else
1322 cpi->find_fractional_mv_step = vp8_skip_fractional_mv_step;
1325 if (cpi->sf.optimize_coefficients == 1)
1326 cpi->mb.optimize = 1 + cpi->is_next_src_alt_ref;
1327 else
1328 cpi->mb.optimize = 0;
1330 if (cpi->common.full_pixel)
1331 cpi->find_fractional_mv_step = vp8_skip_fractional_mv_step;
1333 #ifdef SPEEDSTATS
1334 frames_at_speed[cpi->Speed]++;
1335 #endif
1337 static void alloc_raw_frame_buffers(VP8_COMP *cpi)
1339 int i, buffers;
1341 buffers = cpi->oxcf.lag_in_frames;
1343 if (buffers > MAX_LAG_BUFFERS)
1344 buffers = MAX_LAG_BUFFERS;
1346 if (buffers < 1)
1347 buffers = 1;
1349 for (i = 0; i < buffers; i++)
1350 if (vp8_yv12_alloc_frame_buffer(&cpi->src_buffer[i].source_buffer,
1351 cpi->oxcf.Width, cpi->oxcf.Height,
1352 16))
1353 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1354 "Failed to allocate lag buffer");
1356 #if VP8_TEMPORAL_ALT_REF
1358 if (vp8_yv12_alloc_frame_buffer(&cpi->alt_ref_buffer.source_buffer,
1359 cpi->oxcf.Width, cpi->oxcf.Height, 16))
1360 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1361 "Failed to allocate altref buffer");
1363 #endif
1365 cpi->source_buffer_count = 0;
1368 static int vp8_alloc_partition_data(VP8_COMP *cpi)
1370 if(cpi->mb.pip)
1371 vpx_free(cpi->mb.pip);
1373 cpi->mb.pip = vpx_calloc((cpi->common.mb_cols + 1) *
1374 (cpi->common.mb_rows + 1),
1375 sizeof(PARTITION_INFO));
1376 if(!cpi->mb.pip)
1377 return ALLOC_FAILURE;
1379 cpi->mb.pi = cpi->mb.pip + cpi->common.mode_info_stride + 1;
1381 return 0;
1384 void vp8_alloc_compressor_data(VP8_COMP *cpi)
1386 VP8_COMMON *cm = & cpi->common;
1388 int width = cm->Width;
1389 int height = cm->Height;
1391 if (vp8_alloc_frame_buffers(cm, width, height))
1392 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1393 "Failed to allocate frame buffers");
1395 if (vp8_alloc_partition_data(cpi))
1396 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1397 "Failed to allocate partition data");
1400 if ((width & 0xf) != 0)
1401 width += 16 - (width & 0xf);
1403 if ((height & 0xf) != 0)
1404 height += 16 - (height & 0xf);
1407 if (vp8_yv12_alloc_frame_buffer(&cpi->last_frame_uf,
1408 width, height, VP8BORDERINPIXELS))
1409 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1410 "Failed to allocate last frame buffer");
1412 if (vp8_yv12_alloc_frame_buffer(&cpi->scaled_source, width, height, 16))
1413 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1414 "Failed to allocate scaled source buffer");
1417 if (cpi->tok != 0)
1418 vpx_free(cpi->tok);
1421 unsigned int tokens = cm->mb_rows * cm->mb_cols * 24 * 16;
1423 CHECK_MEM_ERROR(cpi->tok, vpx_calloc(tokens, sizeof(*cpi->tok)));
1426 // Data used for real time vc mode to see if gf needs refreshing
1427 cpi->inter_zz_count = 0;
1428 cpi->gf_bad_count = 0;
1429 cpi->gf_update_recommended = 0;
1432 // Structures used to minitor GF usage
1433 if (cpi->gf_active_flags != 0)
1434 vpx_free(cpi->gf_active_flags);
1436 CHECK_MEM_ERROR(cpi->gf_active_flags, vpx_calloc(1, cm->mb_rows * cm->mb_cols));
1438 cpi->gf_active_count = cm->mb_rows * cm->mb_cols;
1440 if(cpi->total_stats)
1441 vpx_free(cpi->total_stats);
1443 cpi->total_stats = vpx_calloc(1, vp8_firstpass_stats_sz(cpi->common.MBs));
1445 if(cpi->this_frame_stats)
1446 vpx_free(cpi->this_frame_stats);
1448 cpi->this_frame_stats = vpx_calloc(1, vp8_firstpass_stats_sz(cpi->common.MBs));
1450 if(!cpi->total_stats || !cpi->this_frame_stats)
1451 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1452 "Failed to allocate firstpass stats");
1456 // Quant MOD
1457 static const int q_trans[] =
1459 0, 1, 2, 3, 4, 5, 7, 8,
1460 9, 10, 12, 13, 15, 17, 18, 19,
1461 20, 21, 23, 24, 25, 26, 27, 28,
1462 29, 30, 31, 33, 35, 37, 39, 41,
1463 43, 45, 47, 49, 51, 53, 55, 57,
1464 59, 61, 64, 67, 70, 73, 76, 79,
1465 82, 85, 88, 91, 94, 97, 100, 103,
1466 106, 109, 112, 115, 118, 121, 124, 127,
1469 int vp8_reverse_trans(int x)
1471 int i;
1473 for (i = 0; i < 64; i++)
1474 if (q_trans[i] >= x)
1475 return i;
1477 return 63;
1479 void vp8_new_frame_rate(VP8_COMP *cpi, double framerate)
1481 if(framerate < .1)
1482 framerate = 30;
1484 cpi->oxcf.frame_rate = framerate;
1485 cpi->output_frame_rate = cpi->oxcf.frame_rate;
1486 cpi->per_frame_bandwidth = (int)(cpi->oxcf.target_bandwidth / cpi->output_frame_rate);
1487 cpi->av_per_frame_bandwidth = (int)(cpi->oxcf.target_bandwidth / cpi->output_frame_rate);
1488 cpi->min_frame_bandwidth = (int)(cpi->av_per_frame_bandwidth * cpi->oxcf.two_pass_vbrmin_section / 100);
1489 cpi->max_gf_interval = (int)(cpi->output_frame_rate / 2) + 2;
1491 //cpi->max_gf_interval = (int)(cpi->output_frame_rate * 2 / 3) + 1;
1492 //cpi->max_gf_interval = 24;
1494 if (cpi->max_gf_interval < 12)
1495 cpi->max_gf_interval = 12;
1498 // Special conditions when altr ref frame enabled in lagged compress mode
1499 if (cpi->oxcf.play_alternate && cpi->oxcf.lag_in_frames)
1501 if (cpi->max_gf_interval > cpi->oxcf.lag_in_frames - 1)
1502 cpi->max_gf_interval = cpi->oxcf.lag_in_frames - 1;
1507 static int
1508 rescale(int val, int num, int denom)
1510 int64_t llnum = num;
1511 int64_t llden = denom;
1512 int64_t llval = val;
1514 return llval * llnum / llden;
1518 void vp8_init_config(VP8_PTR ptr, VP8_CONFIG *oxcf)
1520 VP8_COMP *cpi = (VP8_COMP *)(ptr);
1521 VP8_COMMON *cm = &cpi->common;
1523 if (!cpi)
1524 return;
1526 cpi->auto_gold = 1;
1527 cpi->auto_adjust_gold_quantizer = 1;
1528 cpi->goldquantizer = 1;
1529 cpi->goldfreq = 7;
1530 cpi->auto_adjust_key_quantizer = 1;
1531 cpi->keyquantizer = 1;
1533 cm->version = oxcf->Version;
1534 vp8_setup_version(cm);
1536 if (oxcf == 0)
1538 cpi->pass = 0;
1540 cpi->auto_worst_q = 0;
1541 cpi->oxcf.best_allowed_q = MINQ;
1542 cpi->oxcf.worst_allowed_q = MAXQ;
1544 cpi->oxcf.end_usage = USAGE_STREAM_FROM_SERVER;
1545 cpi->oxcf.starting_buffer_level = 4000;
1546 cpi->oxcf.optimal_buffer_level = 5000;
1547 cpi->oxcf.maximum_buffer_size = 6000;
1548 cpi->oxcf.under_shoot_pct = 90;
1549 cpi->oxcf.allow_df = 0;
1550 cpi->oxcf.drop_frames_water_mark = 20;
1552 cpi->oxcf.allow_spatial_resampling = 0;
1553 cpi->oxcf.resample_down_water_mark = 40;
1554 cpi->oxcf.resample_up_water_mark = 60;
1556 cpi->oxcf.fixed_q = cpi->interquantizer;
1558 cpi->filter_type = NORMAL_LOOPFILTER;
1560 if (cm->simpler_lpf)
1561 cpi->filter_type = SIMPLE_LOOPFILTER;
1563 cpi->compressor_speed = 1;
1564 cpi->horiz_scale = 0;
1565 cpi->vert_scale = 0;
1566 cpi->oxcf.two_pass_vbrbias = 50;
1567 cpi->oxcf.two_pass_vbrmax_section = 400;
1568 cpi->oxcf.two_pass_vbrmin_section = 0;
1570 cpi->oxcf.Sharpness = 0;
1571 cpi->oxcf.noise_sensitivity = 0;
1573 else
1574 cpi->oxcf = *oxcf;
1577 switch (cpi->oxcf.Mode)
1580 case MODE_REALTIME:
1581 cpi->pass = 0;
1582 cpi->compressor_speed = 2;
1584 if (cpi->oxcf.cpu_used < -16)
1586 cpi->oxcf.cpu_used = -16;
1589 if (cpi->oxcf.cpu_used > 16)
1590 cpi->oxcf.cpu_used = 16;
1592 break;
1594 #if !(CONFIG_REALTIME_ONLY)
1595 case MODE_GOODQUALITY:
1596 cpi->pass = 0;
1597 cpi->compressor_speed = 1;
1599 if (cpi->oxcf.cpu_used < -5)
1601 cpi->oxcf.cpu_used = -5;
1604 if (cpi->oxcf.cpu_used > 5)
1605 cpi->oxcf.cpu_used = 5;
1607 break;
1609 case MODE_BESTQUALITY:
1610 cpi->pass = 0;
1611 cpi->compressor_speed = 0;
1612 break;
1614 case MODE_FIRSTPASS:
1615 cpi->pass = 1;
1616 cpi->compressor_speed = 1;
1617 break;
1618 case MODE_SECONDPASS:
1619 cpi->pass = 2;
1620 cpi->compressor_speed = 1;
1622 if (cpi->oxcf.cpu_used < -5)
1624 cpi->oxcf.cpu_used = -5;
1627 if (cpi->oxcf.cpu_used > 5)
1628 cpi->oxcf.cpu_used = 5;
1630 break;
1631 case MODE_SECONDPASS_BEST:
1632 cpi->pass = 2;
1633 cpi->compressor_speed = 0;
1634 break;
1635 #endif
1638 if (cpi->pass == 0)
1639 cpi->auto_worst_q = 1;
1641 cpi->oxcf.worst_allowed_q = q_trans[oxcf->worst_allowed_q];
1642 cpi->oxcf.best_allowed_q = q_trans[oxcf->best_allowed_q];
1644 if (oxcf->fixed_q >= 0)
1646 if (oxcf->worst_allowed_q < 0)
1647 cpi->oxcf.fixed_q = q_trans[0];
1648 else
1649 cpi->oxcf.fixed_q = q_trans[oxcf->worst_allowed_q];
1651 if (oxcf->alt_q < 0)
1652 cpi->oxcf.alt_q = q_trans[0];
1653 else
1654 cpi->oxcf.alt_q = q_trans[oxcf->alt_q];
1656 if (oxcf->key_q < 0)
1657 cpi->oxcf.key_q = q_trans[0];
1658 else
1659 cpi->oxcf.key_q = q_trans[oxcf->key_q];
1661 if (oxcf->gold_q < 0)
1662 cpi->oxcf.gold_q = q_trans[0];
1663 else
1664 cpi->oxcf.gold_q = q_trans[oxcf->gold_q];
1668 cpi->baseline_gf_interval = cpi->oxcf.alt_freq ? cpi->oxcf.alt_freq : DEFAULT_GF_INTERVAL;
1669 cpi->ref_frame_flags = VP8_ALT_FLAG | VP8_GOLD_FLAG | VP8_LAST_FLAG;
1671 //cpi->use_golden_frame_only = 0;
1672 //cpi->use_last_frame_only = 0;
1673 cm->refresh_golden_frame = 0;
1674 cm->refresh_last_frame = 1;
1675 cm->refresh_entropy_probs = 1;
1677 if (cpi->oxcf.token_partitions >= 0 && cpi->oxcf.token_partitions <= 3)
1678 cm->multi_token_partition = (TOKEN_PARTITION) cpi->oxcf.token_partitions;
1680 setup_features(cpi);
1683 int i;
1685 for (i = 0; i < MAX_MB_SEGMENTS; i++)
1686 cpi->segment_encode_breakout[i] = cpi->oxcf.encode_breakout;
1689 // At the moment the first order values may not be > MAXQ
1690 if (cpi->oxcf.fixed_q > MAXQ)
1691 cpi->oxcf.fixed_q = MAXQ;
1693 // local file playback mode == really big buffer
1694 if (cpi->oxcf.end_usage == USAGE_LOCAL_FILE_PLAYBACK)
1696 cpi->oxcf.starting_buffer_level = 60000;
1697 cpi->oxcf.optimal_buffer_level = 60000;
1698 cpi->oxcf.maximum_buffer_size = 240000;
1703 // Convert target bandwidth from Kbit/s to Bit/s
1704 cpi->oxcf.target_bandwidth *= 1000;
1705 cpi->oxcf.starting_buffer_level =
1706 rescale(cpi->oxcf.starting_buffer_level,
1707 cpi->oxcf.target_bandwidth, 1000);
1709 if (cpi->oxcf.optimal_buffer_level == 0)
1710 cpi->oxcf.optimal_buffer_level = cpi->oxcf.target_bandwidth / 8;
1711 else
1712 cpi->oxcf.optimal_buffer_level =
1713 rescale(cpi->oxcf.optimal_buffer_level,
1714 cpi->oxcf.target_bandwidth, 1000);
1716 if (cpi->oxcf.maximum_buffer_size == 0)
1717 cpi->oxcf.maximum_buffer_size = cpi->oxcf.target_bandwidth / 8;
1718 else
1719 cpi->oxcf.maximum_buffer_size =
1720 rescale(cpi->oxcf.maximum_buffer_size,
1721 cpi->oxcf.target_bandwidth, 1000);
1723 cpi->buffer_level = cpi->oxcf.starting_buffer_level;
1724 cpi->bits_off_target = cpi->oxcf.starting_buffer_level;
1726 vp8_new_frame_rate(cpi, cpi->oxcf.frame_rate);
1727 cpi->worst_quality = cpi->oxcf.worst_allowed_q;
1728 cpi->active_worst_quality = cpi->oxcf.worst_allowed_q;
1729 cpi->avg_frame_qindex = cpi->oxcf.worst_allowed_q;
1730 cpi->best_quality = cpi->oxcf.best_allowed_q;
1731 cpi->active_best_quality = cpi->oxcf.best_allowed_q;
1732 cpi->buffered_mode = (cpi->oxcf.optimal_buffer_level > 0) ? TRUE : FALSE;
1734 cpi->rolling_target_bits = cpi->av_per_frame_bandwidth;
1735 cpi->rolling_actual_bits = cpi->av_per_frame_bandwidth;
1736 cpi->long_rolling_target_bits = cpi->av_per_frame_bandwidth;
1737 cpi->long_rolling_actual_bits = cpi->av_per_frame_bandwidth;
1739 cpi->total_actual_bits = 0;
1740 cpi->total_target_vs_actual = 0;
1742 // Only allow dropped frames in buffered mode
1743 cpi->drop_frames_allowed = cpi->oxcf.allow_df && cpi->buffered_mode;
1745 cm->filter_type = (LOOPFILTERTYPE) cpi->filter_type;
1747 if (!cm->use_bilinear_mc_filter)
1748 cm->mcomp_filter_type = SIXTAP;
1749 else
1750 cm->mcomp_filter_type = BILINEAR;
1752 cpi->target_bandwidth = cpi->oxcf.target_bandwidth;
1754 cm->Width = cpi->oxcf.Width ;
1755 cm->Height = cpi->oxcf.Height ;
1757 cpi->intra_frame_target = (4 * (cm->Width + cm->Height) / 15) * 1000; // As per VP8
1759 cm->horiz_scale = cpi->horiz_scale;
1760 cm->vert_scale = cpi->vert_scale ;
1762 // VP8 sharpness level mapping 0-7 (vs 0-10 in general VPx dialogs)
1763 if (cpi->oxcf.Sharpness > 7)
1764 cpi->oxcf.Sharpness = 7;
1766 cm->sharpness_level = cpi->oxcf.Sharpness;
1768 if (cm->horiz_scale != NORMAL || cm->vert_scale != NORMAL)
1770 int UNINITIALIZED_IS_SAFE(hr), UNINITIALIZED_IS_SAFE(hs);
1771 int UNINITIALIZED_IS_SAFE(vr), UNINITIALIZED_IS_SAFE(vs);
1773 Scale2Ratio(cm->horiz_scale, &hr, &hs);
1774 Scale2Ratio(cm->vert_scale, &vr, &vs);
1776 // always go to the next whole number
1777 cm->Width = (hs - 1 + cpi->oxcf.Width * hr) / hs;
1778 cm->Height = (vs - 1 + cpi->oxcf.Height * vr) / vs;
1781 if (((cm->Width + 15) & 0xfffffff0) != cm->yv12_fb[cm->lst_fb_idx].y_width ||
1782 ((cm->Height + 15) & 0xfffffff0) != cm->yv12_fb[cm->lst_fb_idx].y_height ||
1783 cm->yv12_fb[cm->lst_fb_idx].y_width == 0)
1785 alloc_raw_frame_buffers(cpi);
1786 vp8_alloc_compressor_data(cpi);
1789 // Clamp KF frame size to quarter of data rate
1790 if (cpi->intra_frame_target > cpi->target_bandwidth >> 2)
1791 cpi->intra_frame_target = cpi->target_bandwidth >> 2;
1793 if (cpi->oxcf.fixed_q >= 0)
1795 cpi->last_q[0] = cpi->oxcf.fixed_q;
1796 cpi->last_q[1] = cpi->oxcf.fixed_q;
1799 cpi->Speed = cpi->oxcf.cpu_used;
1801 // force to allowlag to 0 if lag_in_frames is 0;
1802 if (cpi->oxcf.lag_in_frames == 0)
1804 cpi->oxcf.allow_lag = 0;
1806 // Limit on lag buffers as these are not currently dynamically allocated
1807 else if (cpi->oxcf.lag_in_frames > MAX_LAG_BUFFERS)
1808 cpi->oxcf.lag_in_frames = MAX_LAG_BUFFERS;
1810 // YX Temp
1811 cpi->last_alt_ref_sei = -1;
1812 cpi->is_src_frame_alt_ref = 0;
1813 cpi->is_next_src_alt_ref = 0;
1815 #if 0
1816 // Experimental RD Code
1817 cpi->frame_distortion = 0;
1818 cpi->last_frame_distortion = 0;
1819 #endif
1821 #if VP8_TEMPORAL_ALT_REF
1823 cpi->use_weighted_temporal_filter = 0;
1826 int i;
1828 cpi->fixed_divide[0] = 0;
1830 for (i = 1; i < 512; i++)
1831 cpi->fixed_divide[i] = 0x80000 / i;
1833 #endif
1837 * This function needs more clean up, i.e. be more tuned torwards
1838 * change_config rather than init_config !!!!!!!!!!!!!!!!
1839 * YX - 5/28/2009
1843 void vp8_change_config(VP8_PTR ptr, VP8_CONFIG *oxcf)
1845 VP8_COMP *cpi = (VP8_COMP *)(ptr);
1846 VP8_COMMON *cm = &cpi->common;
1848 if (!cpi)
1849 return;
1851 if (!oxcf)
1852 return;
1854 if (cm->version != oxcf->Version)
1856 cm->version = oxcf->Version;
1857 vp8_setup_version(cm);
1860 cpi->oxcf = *oxcf;
1862 switch (cpi->oxcf.Mode)
1865 case MODE_REALTIME:
1866 cpi->pass = 0;
1867 cpi->compressor_speed = 2;
1869 if (cpi->oxcf.cpu_used < -16)
1871 cpi->oxcf.cpu_used = -16;
1874 if (cpi->oxcf.cpu_used > 16)
1875 cpi->oxcf.cpu_used = 16;
1877 break;
1879 #if !(CONFIG_REALTIME_ONLY)
1880 case MODE_GOODQUALITY:
1881 cpi->pass = 0;
1882 cpi->compressor_speed = 1;
1884 if (cpi->oxcf.cpu_used < -5)
1886 cpi->oxcf.cpu_used = -5;
1889 if (cpi->oxcf.cpu_used > 5)
1890 cpi->oxcf.cpu_used = 5;
1892 break;
1894 case MODE_BESTQUALITY:
1895 cpi->pass = 0;
1896 cpi->compressor_speed = 0;
1897 break;
1899 case MODE_FIRSTPASS:
1900 cpi->pass = 1;
1901 cpi->compressor_speed = 1;
1902 break;
1903 case MODE_SECONDPASS:
1904 cpi->pass = 2;
1905 cpi->compressor_speed = 1;
1907 if (cpi->oxcf.cpu_used < -5)
1909 cpi->oxcf.cpu_used = -5;
1912 if (cpi->oxcf.cpu_used > 5)
1913 cpi->oxcf.cpu_used = 5;
1915 break;
1916 case MODE_SECONDPASS_BEST:
1917 cpi->pass = 2;
1918 cpi->compressor_speed = 0;
1919 break;
1920 #endif
1923 if (cpi->pass == 0)
1924 cpi->auto_worst_q = 1;
1926 cpi->oxcf.worst_allowed_q = q_trans[oxcf->worst_allowed_q];
1927 cpi->oxcf.best_allowed_q = q_trans[oxcf->best_allowed_q];
1929 if (oxcf->fixed_q >= 0)
1931 if (oxcf->worst_allowed_q < 0)
1932 cpi->oxcf.fixed_q = q_trans[0];
1933 else
1934 cpi->oxcf.fixed_q = q_trans[oxcf->worst_allowed_q];
1936 if (oxcf->alt_q < 0)
1937 cpi->oxcf.alt_q = q_trans[0];
1938 else
1939 cpi->oxcf.alt_q = q_trans[oxcf->alt_q];
1941 if (oxcf->key_q < 0)
1942 cpi->oxcf.key_q = q_trans[0];
1943 else
1944 cpi->oxcf.key_q = q_trans[oxcf->key_q];
1946 if (oxcf->gold_q < 0)
1947 cpi->oxcf.gold_q = q_trans[0];
1948 else
1949 cpi->oxcf.gold_q = q_trans[oxcf->gold_q];
1953 cpi->baseline_gf_interval = cpi->oxcf.alt_freq ? cpi->oxcf.alt_freq : DEFAULT_GF_INTERVAL;
1955 cpi->ref_frame_flags = VP8_ALT_FLAG | VP8_GOLD_FLAG | VP8_LAST_FLAG;
1957 //cpi->use_golden_frame_only = 0;
1958 //cpi->use_last_frame_only = 0;
1959 cm->refresh_golden_frame = 0;
1960 cm->refresh_last_frame = 1;
1961 cm->refresh_entropy_probs = 1;
1963 if (cpi->oxcf.token_partitions >= 0 && cpi->oxcf.token_partitions <= 3)
1964 cm->multi_token_partition = (TOKEN_PARTITION) cpi->oxcf.token_partitions;
1966 setup_features(cpi);
1969 int i;
1971 for (i = 0; i < MAX_MB_SEGMENTS; i++)
1972 cpi->segment_encode_breakout[i] = cpi->oxcf.encode_breakout;
1975 // At the moment the first order values may not be > MAXQ
1976 if (cpi->oxcf.fixed_q > MAXQ)
1977 cpi->oxcf.fixed_q = MAXQ;
1979 // local file playback mode == really big buffer
1980 if (cpi->oxcf.end_usage == USAGE_LOCAL_FILE_PLAYBACK)
1982 cpi->oxcf.starting_buffer_level = 60000;
1983 cpi->oxcf.optimal_buffer_level = 60000;
1984 cpi->oxcf.maximum_buffer_size = 240000;
1988 // Convert target bandwidth from Kbit/s to Bit/s
1989 cpi->oxcf.target_bandwidth *= 1000;
1991 cpi->oxcf.starting_buffer_level =
1992 rescale(cpi->oxcf.starting_buffer_level,
1993 cpi->oxcf.target_bandwidth, 1000);
1995 if (cpi->oxcf.optimal_buffer_level == 0)
1996 cpi->oxcf.optimal_buffer_level = cpi->oxcf.target_bandwidth / 8;
1997 else
1998 cpi->oxcf.optimal_buffer_level =
1999 rescale(cpi->oxcf.optimal_buffer_level,
2000 cpi->oxcf.target_bandwidth, 1000);
2002 if (cpi->oxcf.maximum_buffer_size == 0)
2003 cpi->oxcf.maximum_buffer_size = cpi->oxcf.target_bandwidth / 8;
2004 else
2005 cpi->oxcf.maximum_buffer_size =
2006 rescale(cpi->oxcf.maximum_buffer_size,
2007 cpi->oxcf.target_bandwidth, 1000);
2009 cpi->buffer_level = cpi->oxcf.starting_buffer_level;
2010 cpi->bits_off_target = cpi->oxcf.starting_buffer_level;
2012 vp8_new_frame_rate(cpi, cpi->oxcf.frame_rate);
2013 cpi->worst_quality = cpi->oxcf.worst_allowed_q;
2014 cpi->active_worst_quality = cpi->oxcf.worst_allowed_q;
2015 cpi->avg_frame_qindex = cpi->oxcf.worst_allowed_q;
2016 cpi->best_quality = cpi->oxcf.best_allowed_q;
2017 cpi->active_best_quality = cpi->oxcf.best_allowed_q;
2018 cpi->buffered_mode = (cpi->oxcf.optimal_buffer_level > 0) ? TRUE : FALSE;
2020 cpi->rolling_target_bits = cpi->av_per_frame_bandwidth;
2021 cpi->rolling_actual_bits = cpi->av_per_frame_bandwidth;
2022 cpi->long_rolling_target_bits = cpi->av_per_frame_bandwidth;
2023 cpi->long_rolling_actual_bits = cpi->av_per_frame_bandwidth;
2025 cpi->total_actual_bits = 0;
2026 cpi->total_target_vs_actual = 0;
2028 // Only allow dropped frames in buffered mode
2029 cpi->drop_frames_allowed = cpi->oxcf.allow_df && cpi->buffered_mode;
2031 cm->filter_type = (LOOPFILTERTYPE) cpi->filter_type;
2033 if (!cm->use_bilinear_mc_filter)
2034 cm->mcomp_filter_type = SIXTAP;
2035 else
2036 cm->mcomp_filter_type = BILINEAR;
2038 cpi->target_bandwidth = cpi->oxcf.target_bandwidth;
2040 cm->Width = cpi->oxcf.Width ;
2041 cm->Height = cpi->oxcf.Height ;
2043 cm->horiz_scale = cpi->horiz_scale;
2044 cm->vert_scale = cpi->vert_scale ;
2046 cpi->intra_frame_target = (4 * (cm->Width + cm->Height) / 15) * 1000; // As per VP8
2048 // VP8 sharpness level mapping 0-7 (vs 0-10 in general VPx dialogs)
2049 if (cpi->oxcf.Sharpness > 7)
2050 cpi->oxcf.Sharpness = 7;
2052 cm->sharpness_level = cpi->oxcf.Sharpness;
2054 if (cm->horiz_scale != NORMAL || cm->vert_scale != NORMAL)
2056 int UNINITIALIZED_IS_SAFE(hr), UNINITIALIZED_IS_SAFE(hs);
2057 int UNINITIALIZED_IS_SAFE(vr), UNINITIALIZED_IS_SAFE(vs);
2059 Scale2Ratio(cm->horiz_scale, &hr, &hs);
2060 Scale2Ratio(cm->vert_scale, &vr, &vs);
2062 // always go to the next whole number
2063 cm->Width = (hs - 1 + cpi->oxcf.Width * hr) / hs;
2064 cm->Height = (vs - 1 + cpi->oxcf.Height * vr) / vs;
2067 if (((cm->Width + 15) & 0xfffffff0) != cm->yv12_fb[cm->lst_fb_idx].y_width ||
2068 ((cm->Height + 15) & 0xfffffff0) != cm->yv12_fb[cm->lst_fb_idx].y_height ||
2069 cm->yv12_fb[cm->lst_fb_idx].y_width == 0)
2071 alloc_raw_frame_buffers(cpi);
2072 vp8_alloc_compressor_data(cpi);
2075 // Clamp KF frame size to quarter of data rate
2076 if (cpi->intra_frame_target > cpi->target_bandwidth >> 2)
2077 cpi->intra_frame_target = cpi->target_bandwidth >> 2;
2079 if (cpi->oxcf.fixed_q >= 0)
2081 cpi->last_q[0] = cpi->oxcf.fixed_q;
2082 cpi->last_q[1] = cpi->oxcf.fixed_q;
2085 cpi->Speed = cpi->oxcf.cpu_used;
2087 // force to allowlag to 0 if lag_in_frames is 0;
2088 if (cpi->oxcf.lag_in_frames == 0)
2090 cpi->oxcf.allow_lag = 0;
2092 // Limit on lag buffers as these are not currently dynamically allocated
2093 else if (cpi->oxcf.lag_in_frames > MAX_LAG_BUFFERS)
2094 cpi->oxcf.lag_in_frames = MAX_LAG_BUFFERS;
2096 // YX Temp
2097 cpi->last_alt_ref_sei = -1;
2098 cpi->is_src_frame_alt_ref = 0;
2099 cpi->is_next_src_alt_ref = 0;
2101 #if 0
2102 // Experimental RD Code
2103 cpi->frame_distortion = 0;
2104 cpi->last_frame_distortion = 0;
2105 #endif
2109 #define M_LOG2_E 0.693147180559945309417
2110 #define log2f(x) (log (x) / (float) M_LOG2_E)
2111 static void cal_mvsadcosts(int *mvsadcost[2])
2113 int i = 1;
2115 mvsadcost [0] [0] = 300;
2116 mvsadcost [1] [0] = 300;
2120 double z = 256 * (2 * (log2f(2 * i) + .6));
2121 mvsadcost [0][i] = (int) z;
2122 mvsadcost [1][i] = (int) z;
2123 mvsadcost [0][-i] = (int) z;
2124 mvsadcost [1][-i] = (int) z;
2126 while (++i <= mv_max);
2129 VP8_PTR vp8_create_compressor(VP8_CONFIG *oxcf)
2131 int i;
2132 volatile union
2134 VP8_COMP *cpi;
2135 VP8_PTR ptr;
2136 } ctx;
2138 VP8_COMP *cpi;
2139 VP8_COMMON *cm;
2141 cpi = ctx.cpi = vpx_memalign(32, sizeof(VP8_COMP));
2142 // Check that the CPI instance is valid
2143 if (!cpi)
2144 return 0;
2146 cm = &cpi->common;
2148 vpx_memset(cpi, 0, sizeof(VP8_COMP));
2150 if (setjmp(cm->error.jmp))
2152 VP8_PTR ptr = ctx.ptr;
2154 ctx.cpi->common.error.setjmp = 0;
2155 vp8_remove_compressor(&ptr);
2156 return 0;
2159 cpi->common.error.setjmp = 1;
2161 CHECK_MEM_ERROR(cpi->rdtok, vpx_calloc(256 * 3 / 2, sizeof(TOKENEXTRA)));
2162 CHECK_MEM_ERROR(cpi->mb.ss, vpx_calloc(sizeof(search_site), (MAX_MVSEARCH_STEPS * 8) + 1));
2164 vp8_create_common(&cpi->common);
2165 vp8_cmachine_specific_config(cpi);
2167 vp8_init_config((VP8_PTR)cpi, oxcf);
2169 memcpy(cpi->base_skip_false_prob, vp8cx_base_skip_false_prob, sizeof(vp8cx_base_skip_false_prob));
2170 cpi->common.current_video_frame = 0;
2171 cpi->kf_overspend_bits = 0;
2172 cpi->kf_bitrate_adjustment = 0;
2173 cpi->frames_till_gf_update_due = 0;
2174 cpi->gf_overspend_bits = 0;
2175 cpi->non_gf_bitrate_adjustment = 0;
2176 cpi->prob_last_coded = 128;
2177 cpi->prob_gf_coded = 128;
2178 cpi->prob_intra_coded = 63;
2180 // Prime the recent reference frame useage counters.
2181 // Hereafter they will be maintained as a sort of moving average
2182 cpi->recent_ref_frame_usage[INTRA_FRAME] = 1;
2183 cpi->recent_ref_frame_usage[LAST_FRAME] = 1;
2184 cpi->recent_ref_frame_usage[GOLDEN_FRAME] = 1;
2185 cpi->recent_ref_frame_usage[ALTREF_FRAME] = 1;
2187 // Set reference frame sign bias for ALTREF frame to 1 (for now)
2188 cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = 1;
2190 cpi->gf_decay_rate = 0;
2191 cpi->baseline_gf_interval = DEFAULT_GF_INTERVAL;
2193 cpi->gold_is_last = 0 ;
2194 cpi->alt_is_last = 0 ;
2195 cpi->gold_is_alt = 0 ;
2197 // allocate memory for storing last frame's MVs for MV prediction.
2198 CHECK_MEM_ERROR(cpi->lfmv, vpx_calloc((cpi->common.mb_rows+1) * (cpi->common.mb_cols+1), sizeof(int_mv)));
2199 CHECK_MEM_ERROR(cpi->lf_ref_frame_sign_bias, vpx_calloc((cpi->common.mb_rows+1) * (cpi->common.mb_cols+1), sizeof(int)));
2200 CHECK_MEM_ERROR(cpi->lf_ref_frame, vpx_calloc((cpi->common.mb_rows+1) * (cpi->common.mb_cols+1), sizeof(int)));
2202 // Create the encoder segmentation map and set all entries to 0
2203 CHECK_MEM_ERROR(cpi->segmentation_map, vpx_calloc(cpi->common.mb_rows * cpi->common.mb_cols, 1));
2204 CHECK_MEM_ERROR(cpi->active_map, vpx_calloc(cpi->common.mb_rows * cpi->common.mb_cols, 1));
2205 vpx_memset(cpi->active_map , 1, (cpi->common.mb_rows * cpi->common.mb_cols));
2206 cpi->active_map_enabled = 0;
2208 // Create the first pass motion map structure and set to 0
2209 // Allocate space for maximum of 15 buffers
2210 CHECK_MEM_ERROR(cpi->fp_motion_map, vpx_calloc(15*cpi->common.MBs, 1));
2212 #if 0
2213 // Experimental code for lagged and one pass
2214 // Initialise one_pass GF frames stats
2215 // Update stats used for GF selection
2216 if (cpi->pass == 0)
2218 cpi->one_pass_frame_index = 0;
2220 for (i = 0; i < MAX_LAG_BUFFERS; i++)
2222 cpi->one_pass_frame_stats[i].frames_so_far = 0;
2223 cpi->one_pass_frame_stats[i].frame_intra_error = 0.0;
2224 cpi->one_pass_frame_stats[i].frame_coded_error = 0.0;
2225 cpi->one_pass_frame_stats[i].frame_pcnt_inter = 0.0;
2226 cpi->one_pass_frame_stats[i].frame_pcnt_motion = 0.0;
2227 cpi->one_pass_frame_stats[i].frame_mvr = 0.0;
2228 cpi->one_pass_frame_stats[i].frame_mvr_abs = 0.0;
2229 cpi->one_pass_frame_stats[i].frame_mvc = 0.0;
2230 cpi->one_pass_frame_stats[i].frame_mvc_abs = 0.0;
2233 #endif
2235 // Should we use the cyclic refresh method.
2236 // Currently this is tied to error resilliant mode
2237 cpi->cyclic_refresh_mode_enabled = cpi->oxcf.error_resilient_mode;
2238 cpi->cyclic_refresh_mode_max_mbs_perframe = (cpi->common.mb_rows * cpi->common.mb_cols) / 40;
2239 cpi->cyclic_refresh_mode_index = 0;
2240 cpi->cyclic_refresh_q = 32;
2242 if (cpi->cyclic_refresh_mode_enabled)
2244 CHECK_MEM_ERROR(cpi->cyclic_refresh_map, vpx_calloc((cpi->common.mb_rows * cpi->common.mb_cols), 1));
2246 else
2247 cpi->cyclic_refresh_map = (signed char *) NULL;
2249 // Test function for segmentation
2250 //segmentation_test_function((VP8_PTR) cpi);
2252 #ifdef ENTROPY_STATS
2253 init_context_counters();
2254 #endif
2256 /*Initialize the feed-forward activity masking.*/
2257 cpi->activity_avg = 90<<12;
2259 cpi->frames_since_key = 8; // Give a sensible default for the first frame.
2260 cpi->key_frame_frequency = cpi->oxcf.key_freq;
2262 cpi->source_alt_ref_pending = FALSE;
2263 cpi->source_alt_ref_active = FALSE;
2264 cpi->common.refresh_alt_ref_frame = 0;
2266 cpi->b_calculate_psnr = CONFIG_PSNR;
2267 #if CONFIG_PSNR
2268 cpi->b_calculate_ssimg = 0;
2270 cpi->count = 0;
2271 cpi->bytes = 0;
2273 if (cpi->b_calculate_psnr)
2275 cpi->total_sq_error = 0.0;
2276 cpi->total_sq_error2 = 0.0;
2277 cpi->total_y = 0.0;
2278 cpi->total_u = 0.0;
2279 cpi->total_v = 0.0;
2280 cpi->total = 0.0;
2281 cpi->totalp_y = 0.0;
2282 cpi->totalp_u = 0.0;
2283 cpi->totalp_v = 0.0;
2284 cpi->totalp = 0.0;
2285 cpi->tot_recode_hits = 0;
2286 cpi->summed_quality = 0;
2287 cpi->summed_weights = 0;
2290 if (cpi->b_calculate_ssimg)
2292 cpi->total_ssimg_y = 0;
2293 cpi->total_ssimg_u = 0;
2294 cpi->total_ssimg_v = 0;
2295 cpi->total_ssimg_all = 0;
2298 #ifndef LLONG_MAX
2299 #define LLONG_MAX 9223372036854775807LL
2300 #endif
2301 cpi->first_time_stamp_ever = LLONG_MAX;
2303 #endif
2305 cpi->frames_till_gf_update_due = 0;
2306 cpi->key_frame_count = 1;
2307 cpi->tot_key_frame_bits = 0;
2309 cpi->ni_av_qi = cpi->oxcf.worst_allowed_q;
2310 cpi->ni_tot_qi = 0;
2311 cpi->ni_frames = 0;
2312 cpi->total_byte_count = 0;
2314 cpi->drop_frame = 0;
2315 cpi->drop_count = 0;
2316 cpi->max_drop_count = 0;
2317 cpi->max_consec_dropped_frames = 4;
2319 cpi->rate_correction_factor = 1.0;
2320 cpi->key_frame_rate_correction_factor = 1.0;
2321 cpi->gf_rate_correction_factor = 1.0;
2322 cpi->est_max_qcorrection_factor = 1.0;
2324 cpi->mb.mvcost[0] = &cpi->mb.mvcosts[0][mv_max+1];
2325 cpi->mb.mvcost[1] = &cpi->mb.mvcosts[1][mv_max+1];
2326 cpi->mb.mvsadcost[0] = &cpi->mb.mvsadcosts[0][mv_max+1];
2327 cpi->mb.mvsadcost[1] = &cpi->mb.mvsadcosts[1][mv_max+1];
2329 cal_mvsadcosts(cpi->mb.mvsadcost);
2331 for (i = 0; i < KEY_FRAME_CONTEXT; i++)
2333 cpi->prior_key_frame_size[i] = cpi->intra_frame_target;
2334 cpi->prior_key_frame_distance[i] = (int)cpi->output_frame_rate;
2337 cpi->check_freq[0] = 15;
2338 cpi->check_freq[1] = 15;
2340 #ifdef OUTPUT_YUV_SRC
2341 yuv_file = fopen("bd.yuv", "ab");
2342 #endif
2344 #if 0
2345 framepsnr = fopen("framepsnr.stt", "a");
2346 kf_list = fopen("kf_list.stt", "w");
2347 #endif
2349 cpi->output_pkt_list = oxcf->output_pkt_list;
2351 #if !(CONFIG_REALTIME_ONLY)
2353 if (cpi->pass == 1)
2355 vp8_init_first_pass(cpi);
2357 else if (cpi->pass == 2)
2359 size_t packet_sz = vp8_firstpass_stats_sz(cpi->common.MBs);
2360 int packets = oxcf->two_pass_stats_in.sz / packet_sz;
2362 cpi->stats_in = oxcf->two_pass_stats_in.buf;
2363 cpi->stats_in_end = (void*)((char *)cpi->stats_in
2364 + (packets - 1) * packet_sz);
2365 vp8_init_second_pass(cpi);
2368 #endif
2370 if (cpi->compressor_speed == 2)
2372 cpi->cpu_freq = 0; //vp8_get_processor_freq();
2373 cpi->avg_encode_time = 0;
2374 cpi->avg_pick_mode_time = 0;
2377 vp8_set_speed_features(cpi);
2379 // Set starting values of RD threshold multipliers (128 = *1)
2380 for (i = 0; i < MAX_MODES; i++)
2382 cpi->rd_thresh_mult[i] = 128;
2385 #ifdef ENTROPY_STATS
2386 init_mv_ref_counts();
2387 #endif
2389 vp8cx_create_encoder_threads(cpi);
2391 cpi->fn_ptr[BLOCK_16X16].sdf = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x16);
2392 cpi->fn_ptr[BLOCK_16X16].vf = VARIANCE_INVOKE(&cpi->rtcd.variance, var16x16);
2393 cpi->fn_ptr[BLOCK_16X16].svf = VARIANCE_INVOKE(&cpi->rtcd.variance, subpixvar16x16);
2394 cpi->fn_ptr[BLOCK_16X16].svf_halfpix_h = VARIANCE_INVOKE(&cpi->rtcd.variance, halfpixvar16x16_h);
2395 cpi->fn_ptr[BLOCK_16X16].svf_halfpix_v = VARIANCE_INVOKE(&cpi->rtcd.variance, halfpixvar16x16_v);
2396 cpi->fn_ptr[BLOCK_16X16].svf_halfpix_hv = VARIANCE_INVOKE(&cpi->rtcd.variance, halfpixvar16x16_hv);
2397 cpi->fn_ptr[BLOCK_16X16].sdx3f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x16x3);
2398 cpi->fn_ptr[BLOCK_16X16].sdx8f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x16x8);
2399 cpi->fn_ptr[BLOCK_16X16].sdx4df = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x16x4d);
2401 cpi->fn_ptr[BLOCK_16X8].sdf = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x8);
2402 cpi->fn_ptr[BLOCK_16X8].vf = VARIANCE_INVOKE(&cpi->rtcd.variance, var16x8);
2403 cpi->fn_ptr[BLOCK_16X8].svf = VARIANCE_INVOKE(&cpi->rtcd.variance, subpixvar16x8);
2404 cpi->fn_ptr[BLOCK_16X8].svf_halfpix_h = NULL;
2405 cpi->fn_ptr[BLOCK_16X8].svf_halfpix_v = NULL;
2406 cpi->fn_ptr[BLOCK_16X8].svf_halfpix_hv = NULL;
2407 cpi->fn_ptr[BLOCK_16X8].sdx3f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x8x3);
2408 cpi->fn_ptr[BLOCK_16X8].sdx8f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x8x8);
2409 cpi->fn_ptr[BLOCK_16X8].sdx4df = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x8x4d);
2411 cpi->fn_ptr[BLOCK_8X16].sdf = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x16);
2412 cpi->fn_ptr[BLOCK_8X16].vf = VARIANCE_INVOKE(&cpi->rtcd.variance, var8x16);
2413 cpi->fn_ptr[BLOCK_8X16].svf = VARIANCE_INVOKE(&cpi->rtcd.variance, subpixvar8x16);
2414 cpi->fn_ptr[BLOCK_8X16].svf_halfpix_h = NULL;
2415 cpi->fn_ptr[BLOCK_8X16].svf_halfpix_v = NULL;
2416 cpi->fn_ptr[BLOCK_8X16].svf_halfpix_hv = NULL;
2417 cpi->fn_ptr[BLOCK_8X16].sdx3f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x16x3);
2418 cpi->fn_ptr[BLOCK_8X16].sdx8f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x16x8);
2419 cpi->fn_ptr[BLOCK_8X16].sdx4df = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x16x4d);
2421 cpi->fn_ptr[BLOCK_8X8].sdf = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x8);
2422 cpi->fn_ptr[BLOCK_8X8].vf = VARIANCE_INVOKE(&cpi->rtcd.variance, var8x8);
2423 cpi->fn_ptr[BLOCK_8X8].svf = VARIANCE_INVOKE(&cpi->rtcd.variance, subpixvar8x8);
2424 cpi->fn_ptr[BLOCK_8X8].svf_halfpix_h = NULL;
2425 cpi->fn_ptr[BLOCK_8X8].svf_halfpix_v = NULL;
2426 cpi->fn_ptr[BLOCK_8X8].svf_halfpix_hv = NULL;
2427 cpi->fn_ptr[BLOCK_8X8].sdx3f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x8x3);
2428 cpi->fn_ptr[BLOCK_8X8].sdx8f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x8x8);
2429 cpi->fn_ptr[BLOCK_8X8].sdx4df = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x8x4d);
2431 cpi->fn_ptr[BLOCK_4X4].sdf = VARIANCE_INVOKE(&cpi->rtcd.variance, sad4x4);
2432 cpi->fn_ptr[BLOCK_4X4].vf = VARIANCE_INVOKE(&cpi->rtcd.variance, var4x4);
2433 cpi->fn_ptr[BLOCK_4X4].svf = VARIANCE_INVOKE(&cpi->rtcd.variance, subpixvar4x4);
2434 cpi->fn_ptr[BLOCK_4X4].svf_halfpix_h = NULL;
2435 cpi->fn_ptr[BLOCK_4X4].svf_halfpix_v = NULL;
2436 cpi->fn_ptr[BLOCK_4X4].svf_halfpix_hv = NULL;
2437 cpi->fn_ptr[BLOCK_4X4].sdx3f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad4x4x3);
2438 cpi->fn_ptr[BLOCK_4X4].sdx8f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad4x4x8);
2439 cpi->fn_ptr[BLOCK_4X4].sdx4df = VARIANCE_INVOKE(&cpi->rtcd.variance, sad4x4x4d);
2441 #if !(CONFIG_REALTIME_ONLY)
2442 cpi->full_search_sad = SEARCH_INVOKE(&cpi->rtcd.search, full_search);
2443 #endif
2444 cpi->diamond_search_sad = SEARCH_INVOKE(&cpi->rtcd.search, diamond_search);
2446 cpi->ready_for_new_frame = 1;
2448 cpi->source_encode_index = 0;
2450 // make sure frame 1 is okay
2451 cpi->error_bins[0] = cpi->common.MBs;
2453 //vp8cx_init_quantizer() is first called here. Add check in vp8cx_frame_init_quantizer() so that vp8cx_init_quantizer is only called later
2454 //when needed. This will avoid unnecessary calls of vp8cx_init_quantizer() for every frame.
2455 vp8cx_init_quantizer(cpi);
2457 vp8_init_loop_filter(cm);
2458 cm->last_frame_type = KEY_FRAME;
2459 cm->last_filter_type = cm->filter_type;
2460 cm->last_sharpness_level = cm->sharpness_level;
2462 cpi->common.error.setjmp = 0;
2463 return (VP8_PTR) cpi;
2468 void vp8_remove_compressor(VP8_PTR *ptr)
2470 VP8_COMP *cpi = (VP8_COMP *)(*ptr);
2472 if (!cpi)
2473 return;
2475 if (cpi && (cpi->common.current_video_frame > 0))
2477 #if !(CONFIG_REALTIME_ONLY)
2479 if (cpi->pass == 2)
2481 vp8_end_second_pass(cpi);
2484 #endif
2486 #ifdef ENTROPY_STATS
2487 print_context_counters();
2488 print_tree_update_probs();
2489 print_mode_context();
2490 #endif
2492 #if CONFIG_PSNR
2494 if (cpi->pass != 1)
2496 FILE *f = fopen("opsnr.stt", "a");
2497 double time_encoded = (cpi->source_end_time_stamp - cpi->first_time_stamp_ever) / 10000000.000;
2498 double total_encode_time = (cpi->time_receive_data + cpi->time_compress_data) / 1000.000;
2499 double dr = (double)cpi->bytes * (double) 8 / (double)1000 / time_encoded;
2501 if (cpi->b_calculate_psnr)
2503 YV12_BUFFER_CONFIG *lst_yv12 = &cpi->common.yv12_fb[cpi->common.lst_fb_idx];
2504 double samples = 3.0 / 2 * cpi->count * lst_yv12->y_width * lst_yv12->y_height;
2505 double total_psnr = vp8_mse2psnr(samples, 255.0, cpi->total_sq_error);
2506 double total_psnr2 = vp8_mse2psnr(samples, 255.0, cpi->total_sq_error2);
2507 double total_ssim = 100 * pow(cpi->summed_quality / cpi->summed_weights, 8.0);
2509 fprintf(f, "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\tVPXSSIM\t Time(us)\n");
2510 fprintf(f, "%7.3f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t%7.3f %8.0f\n",
2511 dr, cpi->total / cpi->count, total_psnr, cpi->totalp / cpi->count, total_psnr2, total_ssim,
2512 total_encode_time);
2515 if (cpi->b_calculate_ssimg)
2517 fprintf(f, "BitRate\tSSIM_Y\tSSIM_U\tSSIM_V\tSSIM_A\t Time(us)\n");
2518 fprintf(f, "%7.3f\t%6.4f\t%6.4f\t%6.4f\t%6.4f\t%8.0f\n", dr,
2519 cpi->total_ssimg_y / cpi->count, cpi->total_ssimg_u / cpi->count,
2520 cpi->total_ssimg_v / cpi->count, cpi->total_ssimg_all / cpi->count, total_encode_time);
2523 fclose(f);
2524 #if 0
2525 f = fopen("qskip.stt", "a");
2526 fprintf(f, "minq:%d -maxq:%d skipture:skipfalse = %d:%d\n", cpi->oxcf.best_allowed_q, cpi->oxcf.worst_allowed_q, skiptruecount, skipfalsecount);
2527 fclose(f);
2528 #endif
2532 #endif
2535 #ifdef SPEEDSTATS
2537 if (cpi->compressor_speed == 2)
2539 int i;
2540 FILE *f = fopen("cxspeed.stt", "a");
2541 cnt_pm /= cpi->common.MBs;
2543 for (i = 0; i < 16; i++)
2544 fprintf(f, "%5d", frames_at_speed[i]);
2546 fprintf(f, "\n");
2547 //fprintf(f, "%10d PM %10d %10d %10d EF %10d %10d %10d\n", cpi->Speed, cpi->avg_pick_mode_time, (tot_pm/cnt_pm), cnt_pm, cpi->avg_encode_time, 0, 0);
2548 fclose(f);
2551 #endif
2554 #ifdef MODE_STATS
2556 extern int count_mb_seg[4];
2557 FILE *f = fopen("modes.stt", "a");
2558 double dr = (double)cpi->oxcf.frame_rate * (double)bytes * (double)8 / (double)count / (double)1000 ;
2559 fprintf(f, "intra_mode in Intra Frames:\n");
2560 fprintf(f, "Y: %8d, %8d, %8d, %8d, %8d\n", y_modes[0], y_modes[1], y_modes[2], y_modes[3], y_modes[4]);
2561 fprintf(f, "UV:%8d, %8d, %8d, %8d\n", uv_modes[0], uv_modes[1], uv_modes[2], uv_modes[3]);
2562 fprintf(f, "B: ");
2564 int i;
2566 for (i = 0; i < 10; i++)
2567 fprintf(f, "%8d, ", b_modes[i]);
2569 fprintf(f, "\n");
2573 fprintf(f, "Modes in Inter Frames:\n");
2574 fprintf(f, "Y: %8d, %8d, %8d, %8d, %8d, %8d, %8d, %8d, %8d, %8d\n",
2575 inter_y_modes[0], inter_y_modes[1], inter_y_modes[2], inter_y_modes[3], inter_y_modes[4],
2576 inter_y_modes[5], inter_y_modes[6], inter_y_modes[7], inter_y_modes[8], inter_y_modes[9]);
2577 fprintf(f, "UV:%8d, %8d, %8d, %8d\n", inter_uv_modes[0], inter_uv_modes[1], inter_uv_modes[2], inter_uv_modes[3]);
2578 fprintf(f, "B: ");
2580 int i;
2582 for (i = 0; i < 15; i++)
2583 fprintf(f, "%8d, ", inter_b_modes[i]);
2585 fprintf(f, "\n");
2588 fprintf(f, "P:%8d, %8d, %8d, %8d\n", count_mb_seg[0], count_mb_seg[1], count_mb_seg[2], count_mb_seg[3]);
2589 fprintf(f, "PB:%8d, %8d, %8d, %8d\n", inter_b_modes[LEFT4X4], inter_b_modes[ABOVE4X4], inter_b_modes[ZERO4X4], inter_b_modes[NEW4X4]);
2593 fclose(f);
2595 #endif
2597 #ifdef ENTROPY_STATS
2599 int i, j, k;
2600 FILE *fmode = fopen("modecontext.c", "w");
2602 fprintf(fmode, "\n#include \"entropymode.h\"\n\n");
2603 fprintf(fmode, "const unsigned int vp8_kf_default_bmode_counts ");
2604 fprintf(fmode, "[VP8_BINTRAMODES] [VP8_BINTRAMODES] [VP8_BINTRAMODES] =\n{\n");
2606 for (i = 0; i < 10; i++)
2609 fprintf(fmode, " { //Above Mode : %d\n", i);
2611 for (j = 0; j < 10; j++)
2614 fprintf(fmode, " {");
2616 for (k = 0; k < 10; k++)
2618 if (!intra_mode_stats[i][j][k])
2619 fprintf(fmode, " %5d, ", 1);
2620 else
2621 fprintf(fmode, " %5d, ", intra_mode_stats[i][j][k]);
2624 fprintf(fmode, "}, // left_mode %d\n", j);
2628 fprintf(fmode, " },\n");
2632 fprintf(fmode, "};\n");
2633 fclose(fmode);
2635 #endif
2638 #if defined(SECTIONBITS_OUTPUT)
2640 if (0)
2642 int i;
2643 FILE *f = fopen("tokenbits.stt", "a");
2645 for (i = 0; i < 28; i++)
2646 fprintf(f, "%8d", (int)(Sectionbits[i] / 256));
2648 fprintf(f, "\n");
2649 fclose(f);
2652 #endif
2654 #if 0
2656 printf("\n_pick_loop_filter_level:%d\n", cpi->time_pick_lpf / 1000);
2657 printf("\n_frames recive_data encod_mb_row compress_frame Total\n");
2658 printf("%6d %10ld %10ld %10ld %10ld\n", cpi->common.current_video_frame, cpi->time_receive_data / 1000, cpi->time_encode_mb_row / 1000, cpi->time_compress_data / 1000, (cpi->time_receive_data + cpi->time_compress_data) / 1000);
2660 #endif
2664 vp8cx_remove_encoder_threads(cpi);
2666 vp8_dealloc_compressor_data(cpi);
2667 vpx_free(cpi->mb.ss);
2668 vpx_free(cpi->tok);
2669 vpx_free(cpi->rdtok);
2670 vpx_free(cpi->cyclic_refresh_map);
2672 vp8_remove_common(&cpi->common);
2673 vpx_free(cpi);
2674 *ptr = 0;
2676 #ifdef OUTPUT_YUV_SRC
2677 fclose(yuv_file);
2678 #endif
2680 #if 0
2682 if (keyfile)
2683 fclose(keyfile);
2685 if (framepsnr)
2686 fclose(framepsnr);
2688 if (kf_list)
2689 fclose(kf_list);
2691 #endif
2696 static uint64_t calc_plane_error(unsigned char *orig, int orig_stride,
2697 unsigned char *recon, int recon_stride,
2698 unsigned int cols, unsigned int rows,
2699 vp8_variance_rtcd_vtable_t *rtcd)
2701 unsigned int row, col;
2702 uint64_t total_sse = 0;
2703 int diff;
2705 for (row = 0; row + 16 <= rows; row += 16)
2707 for (col = 0; col + 16 <= cols; col += 16)
2709 unsigned int sse;
2711 VARIANCE_INVOKE(rtcd, mse16x16)(orig + col, orig_stride,
2712 recon + col, recon_stride,
2713 &sse);
2714 total_sse += sse;
2717 /* Handle odd-sized width */
2718 if (col < cols)
2720 unsigned int border_row, border_col;
2721 unsigned char *border_orig = orig;
2722 unsigned char *border_recon = recon;
2724 for (border_row = 0; border_row < 16; border_row++)
2726 for (border_col = col; border_col < cols; border_col++)
2728 diff = border_orig[border_col] - border_recon[border_col];
2729 total_sse += diff * diff;
2732 border_orig += orig_stride;
2733 border_recon += recon_stride;
2737 orig += orig_stride * 16;
2738 recon += recon_stride * 16;
2741 /* Handle odd-sized height */
2742 for (; row < rows; row++)
2744 for (col = 0; col < cols; col++)
2746 diff = orig[col] - recon[col];
2747 total_sse += diff * diff;
2750 orig += orig_stride;
2751 recon += recon_stride;
2754 return total_sse;
2758 static void generate_psnr_packet(VP8_COMP *cpi)
2760 YV12_BUFFER_CONFIG *orig = cpi->Source;
2761 YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
2762 struct vpx_codec_cx_pkt pkt;
2763 uint64_t sse;
2764 int i;
2765 unsigned int width = cpi->common.Width;
2766 unsigned int height = cpi->common.Height;
2768 pkt.kind = VPX_CODEC_PSNR_PKT;
2769 sse = calc_plane_error(orig->y_buffer, orig->y_stride,
2770 recon->y_buffer, recon->y_stride,
2771 width, height,
2772 IF_RTCD(&cpi->rtcd.variance));
2773 pkt.data.psnr.sse[0] = sse;
2774 pkt.data.psnr.sse[1] = sse;
2775 pkt.data.psnr.samples[0] = width * height;
2776 pkt.data.psnr.samples[1] = width * height;
2778 width = (width + 1) / 2;
2779 height = (height + 1) / 2;
2781 sse = calc_plane_error(orig->u_buffer, orig->uv_stride,
2782 recon->u_buffer, recon->uv_stride,
2783 width, height,
2784 IF_RTCD(&cpi->rtcd.variance));
2785 pkt.data.psnr.sse[0] += sse;
2786 pkt.data.psnr.sse[2] = sse;
2787 pkt.data.psnr.samples[0] += width * height;
2788 pkt.data.psnr.samples[2] = width * height;
2790 sse = calc_plane_error(orig->v_buffer, orig->uv_stride,
2791 recon->v_buffer, recon->uv_stride,
2792 width, height,
2793 IF_RTCD(&cpi->rtcd.variance));
2794 pkt.data.psnr.sse[0] += sse;
2795 pkt.data.psnr.sse[3] = sse;
2796 pkt.data.psnr.samples[0] += width * height;
2797 pkt.data.psnr.samples[3] = width * height;
2799 for (i = 0; i < 4; i++)
2800 pkt.data.psnr.psnr[i] = vp8_mse2psnr(pkt.data.psnr.samples[i], 255.0,
2801 pkt.data.psnr.sse[i]);
2803 vpx_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
2807 int vp8_use_as_reference(VP8_PTR ptr, int ref_frame_flags)
2809 VP8_COMP *cpi = (VP8_COMP *)(ptr);
2811 if (ref_frame_flags > 7)
2812 return -1 ;
2814 cpi->ref_frame_flags = ref_frame_flags;
2815 return 0;
2817 int vp8_update_reference(VP8_PTR ptr, int ref_frame_flags)
2819 VP8_COMP *cpi = (VP8_COMP *)(ptr);
2821 if (ref_frame_flags > 7)
2822 return -1 ;
2824 cpi->common.refresh_golden_frame = 0;
2825 cpi->common.refresh_alt_ref_frame = 0;
2826 cpi->common.refresh_last_frame = 0;
2828 if (ref_frame_flags & VP8_LAST_FLAG)
2829 cpi->common.refresh_last_frame = 1;
2831 if (ref_frame_flags & VP8_GOLD_FLAG)
2832 cpi->common.refresh_golden_frame = 1;
2834 if (ref_frame_flags & VP8_ALT_FLAG)
2835 cpi->common.refresh_alt_ref_frame = 1;
2837 return 0;
2840 int vp8_get_reference(VP8_PTR ptr, VP8_REFFRAME ref_frame_flag, YV12_BUFFER_CONFIG *sd)
2842 VP8_COMP *cpi = (VP8_COMP *)(ptr);
2843 VP8_COMMON *cm = &cpi->common;
2844 int ref_fb_idx;
2846 if (ref_frame_flag == VP8_LAST_FLAG)
2847 ref_fb_idx = cm->lst_fb_idx;
2848 else if (ref_frame_flag == VP8_GOLD_FLAG)
2849 ref_fb_idx = cm->gld_fb_idx;
2850 else if (ref_frame_flag == VP8_ALT_FLAG)
2851 ref_fb_idx = cm->alt_fb_idx;
2852 else
2853 return -1;
2855 vp8_yv12_copy_frame_ptr(&cm->yv12_fb[ref_fb_idx], sd);
2857 return 0;
2859 int vp8_set_reference(VP8_PTR ptr, VP8_REFFRAME ref_frame_flag, YV12_BUFFER_CONFIG *sd)
2861 VP8_COMP *cpi = (VP8_COMP *)(ptr);
2862 VP8_COMMON *cm = &cpi->common;
2864 int ref_fb_idx;
2866 if (ref_frame_flag == VP8_LAST_FLAG)
2867 ref_fb_idx = cm->lst_fb_idx;
2868 else if (ref_frame_flag == VP8_GOLD_FLAG)
2869 ref_fb_idx = cm->gld_fb_idx;
2870 else if (ref_frame_flag == VP8_ALT_FLAG)
2871 ref_fb_idx = cm->alt_fb_idx;
2872 else
2873 return -1;
2875 vp8_yv12_copy_frame_ptr(sd, &cm->yv12_fb[ref_fb_idx]);
2877 return 0;
2879 int vp8_update_entropy(VP8_PTR comp, int update)
2881 VP8_COMP *cpi = (VP8_COMP *) comp;
2882 VP8_COMMON *cm = &cpi->common;
2883 cm->refresh_entropy_probs = update;
2885 return 0;
2889 #if OUTPUT_YUV_SRC
2890 void vp8_write_yuv_frame(const char *name, YV12_BUFFER_CONFIG *s)
2892 FILE *yuv_file = fopen(name, "ab");
2893 unsigned char *src = s->y_buffer;
2894 int h = s->y_height;
2898 fwrite(src, s->y_width, 1, yuv_file);
2899 src += s->y_stride;
2901 while (--h);
2903 src = s->u_buffer;
2904 h = s->uv_height;
2908 fwrite(src, s->uv_width, 1, yuv_file);
2909 src += s->uv_stride;
2911 while (--h);
2913 src = s->v_buffer;
2914 h = s->uv_height;
2918 fwrite(src, s->uv_width, 1, yuv_file);
2919 src += s->uv_stride;
2921 while (--h);
2923 fclose(yuv_file);
2925 #endif
2928 static void scale_and_extend_source(YV12_BUFFER_CONFIG *sd, VP8_COMP *cpi)
2930 VP8_COMMON *cm = &cpi->common;
2932 // are we resizing the image
2933 if (cm->horiz_scale != 0 || cm->vert_scale != 0)
2935 #if CONFIG_SPATIAL_RESAMPLING
2936 int UNINITIALIZED_IS_SAFE(hr), UNINITIALIZED_IS_SAFE(hs);
2937 int UNINITIALIZED_IS_SAFE(vr), UNINITIALIZED_IS_SAFE(vs);
2938 int tmp_height;
2940 if (cm->vert_scale == 3)
2941 tmp_height = 9;
2942 else
2943 tmp_height = 11;
2945 Scale2Ratio(cm->horiz_scale, &hr, &hs);
2946 Scale2Ratio(cm->vert_scale, &vr, &vs);
2948 vp8_scale_frame(sd, &cpi->scaled_source, cm->temp_scale_frame.y_buffer,
2949 tmp_height, hs, hr, vs, vr, 0);
2951 cpi->Source = &cpi->scaled_source;
2952 #endif
2954 // we may need to copy to a buffer so we can extend the image...
2955 else if (cm->Width != cm->yv12_fb[cm->lst_fb_idx].y_width ||
2956 cm->Height != cm->yv12_fb[cm->lst_fb_idx].y_height)
2958 //vp8_yv12_copy_frame_ptr(sd, &cpi->scaled_source);
2959 #if HAVE_ARMV7
2960 #if CONFIG_RUNTIME_CPU_DETECT
2961 if (cm->rtcd.flags & HAS_NEON)
2962 #endif
2964 vp8_yv12_copy_src_frame_func_neon(sd, &cpi->scaled_source);
2966 #if CONFIG_RUNTIME_CPU_DETECT
2967 else
2968 #endif
2969 #endif
2970 #if !HAVE_ARMV7 || CONFIG_RUNTIME_CPU_DETECT
2972 vp8_yv12_copy_frame_ptr(sd, &cpi->scaled_source);
2974 #endif
2976 cpi->Source = &cpi->scaled_source;
2979 vp8_extend_to_multiple_of16(cpi->Source, cm->Width, cm->Height);
2982 static void resize_key_frame(VP8_COMP *cpi)
2984 #if CONFIG_SPATIAL_RESAMPLING
2985 VP8_COMMON *cm = &cpi->common;
2987 // Do we need to apply resampling for one pass cbr.
2988 // In one pass this is more limited than in two pass cbr
2989 // The test and any change is only made one per key frame sequence
2990 if (cpi->oxcf.allow_spatial_resampling && (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER))
2992 int UNINITIALIZED_IS_SAFE(hr), UNINITIALIZED_IS_SAFE(hs);
2993 int UNINITIALIZED_IS_SAFE(vr), UNINITIALIZED_IS_SAFE(vs);
2994 int new_width, new_height;
2996 // If we are below the resample DOWN watermark then scale down a notch.
2997 if (cpi->buffer_level < (cpi->oxcf.resample_down_water_mark * cpi->oxcf.optimal_buffer_level / 100))
2999 cm->horiz_scale = (cm->horiz_scale < ONETWO) ? cm->horiz_scale + 1 : ONETWO;
3000 cm->vert_scale = (cm->vert_scale < ONETWO) ? cm->vert_scale + 1 : ONETWO;
3002 // Should we now start scaling back up
3003 else if (cpi->buffer_level > (cpi->oxcf.resample_up_water_mark * cpi->oxcf.optimal_buffer_level / 100))
3005 cm->horiz_scale = (cm->horiz_scale > NORMAL) ? cm->horiz_scale - 1 : NORMAL;
3006 cm->vert_scale = (cm->vert_scale > NORMAL) ? cm->vert_scale - 1 : NORMAL;
3009 // Get the new hieght and width
3010 Scale2Ratio(cm->horiz_scale, &hr, &hs);
3011 Scale2Ratio(cm->vert_scale, &vr, &vs);
3012 new_width = ((hs - 1) + (cpi->oxcf.Width * hr)) / hs;
3013 new_height = ((vs - 1) + (cpi->oxcf.Height * vr)) / vs;
3015 // If the image size has changed we need to reallocate the buffers
3016 // and resample the source image
3017 if ((cm->Width != new_width) || (cm->Height != new_height))
3019 cm->Width = new_width;
3020 cm->Height = new_height;
3021 vp8_alloc_compressor_data(cpi);
3022 scale_and_extend_source(cpi->un_scaled_source, cpi);
3026 #endif
3028 // return of 0 means drop frame
3029 static int pick_frame_size(VP8_COMP *cpi)
3031 VP8_COMMON *cm = &cpi->common;
3033 // First Frame is a special case
3034 if (cm->current_video_frame == 0)
3036 #if !(CONFIG_REALTIME_ONLY)
3038 if (cpi->pass == 2)
3039 vp8_calc_auto_iframe_target_size(cpi);
3041 // 1 Pass there is no information on which to base size so use bandwidth per second * fixed fraction
3042 else
3043 #endif
3044 cpi->this_frame_target = cpi->oxcf.target_bandwidth / 2;
3046 // in error resilient mode the first frame is bigger since it likely contains
3047 // all the static background
3048 if (cpi->oxcf.error_resilient_mode == 1 || (cpi->compressor_speed == 2))
3050 cpi->this_frame_target *= 3; // 5;
3053 // Key frame from VFW/auto-keyframe/first frame
3054 cm->frame_type = KEY_FRAME;
3057 // Special case for forced key frames
3058 // The frame sizing here is still far from ideal for 2 pass.
3059 else if (cm->frame_flags & FRAMEFLAGS_KEY)
3061 cm->frame_type = KEY_FRAME;
3062 resize_key_frame(cpi);
3063 vp8_calc_iframe_target_size(cpi);
3065 else if (cm->frame_type == KEY_FRAME)
3067 vp8_calc_auto_iframe_target_size(cpi);
3069 else
3071 // INTER frame: compute target frame size
3072 cm->frame_type = INTER_FRAME;
3073 vp8_calc_pframe_target_size(cpi);
3075 // Check if we're dropping the frame:
3076 if (cpi->drop_frame)
3078 cpi->drop_frame = FALSE;
3079 cpi->drop_count++;
3080 return 0;
3084 return 1;
3086 static void set_quantizer(VP8_COMP *cpi, int Q)
3088 VP8_COMMON *cm = &cpi->common;
3089 MACROBLOCKD *mbd = &cpi->mb.e_mbd;
3091 cm->base_qindex = Q;
3093 cm->y1dc_delta_q = 0;
3094 cm->y2dc_delta_q = 0;
3095 cm->y2ac_delta_q = 0;
3096 cm->uvdc_delta_q = 0;
3097 cm->uvac_delta_q = 0;
3099 // Set Segment specific quatizers
3100 mbd->segment_feature_data[MB_LVL_ALT_Q][0] = cpi->segment_feature_data[MB_LVL_ALT_Q][0];
3101 mbd->segment_feature_data[MB_LVL_ALT_Q][1] = cpi->segment_feature_data[MB_LVL_ALT_Q][1];
3102 mbd->segment_feature_data[MB_LVL_ALT_Q][2] = cpi->segment_feature_data[MB_LVL_ALT_Q][2];
3103 mbd->segment_feature_data[MB_LVL_ALT_Q][3] = cpi->segment_feature_data[MB_LVL_ALT_Q][3];
3106 static void update_alt_ref_frame_and_stats(VP8_COMP *cpi)
3108 VP8_COMMON *cm = &cpi->common;
3110 // Update the golden frame buffer
3111 vp8_yv12_copy_frame_ptr(cm->frame_to_show, &cm->yv12_fb[cm->alt_fb_idx]);
3113 // Select an interval before next GF or altref
3114 if (!cpi->auto_gold)
3115 cpi->frames_till_gf_update_due = cpi->goldfreq;
3117 if ((cpi->pass != 2) && cpi->frames_till_gf_update_due)
3119 cpi->current_gf_interval = cpi->frames_till_gf_update_due;
3121 // Set the bits per frame that we should try and recover in subsequent inter frames
3122 // to account for the extra GF spend... note that his does not apply for GF updates
3123 // that occur coincident with a key frame as the extra cost of key frames is dealt
3124 // with elsewhere.
3126 cpi->gf_overspend_bits += cpi->projected_frame_size;
3127 cpi->non_gf_bitrate_adjustment = cpi->gf_overspend_bits / cpi->frames_till_gf_update_due;
3130 // Update data structure that monitors level of reference to last GF
3131 vpx_memset(cpi->gf_active_flags, 1, (cm->mb_rows * cm->mb_cols));
3132 cpi->gf_active_count = cm->mb_rows * cm->mb_cols;
3133 // this frame refreshes means next frames don't unless specified by user
3135 cpi->common.frames_since_golden = 0;
3137 // Clear the alternate reference update pending flag.
3138 cpi->source_alt_ref_pending = FALSE;
3140 // Set the alternate refernce frame active flag
3141 cpi->source_alt_ref_active = TRUE;
3145 static void update_golden_frame_and_stats(VP8_COMP *cpi)
3147 VP8_COMMON *cm = &cpi->common;
3149 // Update the Golden frame reconstruction buffer if signalled and the GF usage counts.
3150 if (cm->refresh_golden_frame)
3152 // Update the golden frame buffer
3153 vp8_yv12_copy_frame_ptr(cm->frame_to_show, &cm->yv12_fb[cm->gld_fb_idx]);
3155 // Select an interval before next GF
3156 if (!cpi->auto_gold)
3157 cpi->frames_till_gf_update_due = cpi->goldfreq;
3159 if ((cpi->pass != 2) && (cpi->frames_till_gf_update_due > 0))
3161 cpi->current_gf_interval = cpi->frames_till_gf_update_due;
3163 // Set the bits per frame that we should try and recover in subsequent inter frames
3164 // to account for the extra GF spend... note that his does not apply for GF updates
3165 // that occur coincident with a key frame as the extra cost of key frames is dealt
3166 // with elsewhere.
3167 if ((cm->frame_type != KEY_FRAME) && !cpi->source_alt_ref_active)
3169 // Calcluate GF bits to be recovered
3170 // Projected size - av frame bits available for inter frames for clip as a whole
3171 cpi->gf_overspend_bits += (cpi->projected_frame_size - cpi->inter_frame_target);
3174 cpi->non_gf_bitrate_adjustment = cpi->gf_overspend_bits / cpi->frames_till_gf_update_due;
3178 // Update data structure that monitors level of reference to last GF
3179 vpx_memset(cpi->gf_active_flags, 1, (cm->mb_rows * cm->mb_cols));
3180 cpi->gf_active_count = cm->mb_rows * cm->mb_cols;
3182 // this frame refreshes means next frames don't unless specified by user
3183 cm->refresh_golden_frame = 0;
3184 cpi->common.frames_since_golden = 0;
3186 //if ( cm->frame_type == KEY_FRAME )
3188 cpi->recent_ref_frame_usage[INTRA_FRAME] = 1;
3189 cpi->recent_ref_frame_usage[LAST_FRAME] = 1;
3190 cpi->recent_ref_frame_usage[GOLDEN_FRAME] = 1;
3191 cpi->recent_ref_frame_usage[ALTREF_FRAME] = 1;
3193 //else
3195 // // Carry a potrtion of count over to begining of next gf sequence
3196 // cpi->recent_ref_frame_usage[INTRA_FRAME] >>= 5;
3197 // cpi->recent_ref_frame_usage[LAST_FRAME] >>= 5;
3198 // cpi->recent_ref_frame_usage[GOLDEN_FRAME] >>= 5;
3199 // cpi->recent_ref_frame_usage[ALTREF_FRAME] >>= 5;
3202 // ******** Fixed Q test code only ************
3203 // If we are going to use the ALT reference for the next group of frames set a flag to say so.
3204 if (cpi->oxcf.fixed_q >= 0 &&
3205 cpi->oxcf.play_alternate && !cpi->common.refresh_alt_ref_frame)
3207 cpi->source_alt_ref_pending = TRUE;
3208 cpi->frames_till_gf_update_due = cpi->baseline_gf_interval;
3211 if (!cpi->source_alt_ref_pending)
3212 cpi->source_alt_ref_active = FALSE;
3214 // Decrement count down till next gf
3215 if (cpi->frames_till_gf_update_due > 0)
3216 cpi->frames_till_gf_update_due--;
3219 else if (!cpi->common.refresh_alt_ref_frame)
3221 // Decrement count down till next gf
3222 if (cpi->frames_till_gf_update_due > 0)
3223 cpi->frames_till_gf_update_due--;
3225 if (cpi->common.frames_till_alt_ref_frame)
3226 cpi->common.frames_till_alt_ref_frame --;
3228 cpi->common.frames_since_golden ++;
3230 if (cpi->common.frames_since_golden > 1)
3232 cpi->recent_ref_frame_usage[INTRA_FRAME] += cpi->count_mb_ref_frame_usage[INTRA_FRAME];
3233 cpi->recent_ref_frame_usage[LAST_FRAME] += cpi->count_mb_ref_frame_usage[LAST_FRAME];
3234 cpi->recent_ref_frame_usage[GOLDEN_FRAME] += cpi->count_mb_ref_frame_usage[GOLDEN_FRAME];
3235 cpi->recent_ref_frame_usage[ALTREF_FRAME] += cpi->count_mb_ref_frame_usage[ALTREF_FRAME];
3240 // This function updates the reference frame probability estimates that
3241 // will be used during mode selection
3242 static void update_rd_ref_frame_probs(VP8_COMP *cpi)
3244 VP8_COMMON *cm = &cpi->common;
3246 #if 0
3247 const int *const rfct = cpi->recent_ref_frame_usage;
3248 const int rf_intra = rfct[INTRA_FRAME];
3249 const int rf_inter = rfct[LAST_FRAME] + rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME];
3251 if (cm->frame_type == KEY_FRAME)
3253 cpi->prob_intra_coded = 255;
3254 cpi->prob_last_coded = 128;
3255 cpi->prob_gf_coded = 128;
3257 else if (!(rf_intra + rf_inter))
3259 // This is a trap in case this function is called with cpi->recent_ref_frame_usage[] blank.
3260 cpi->prob_intra_coded = 63;
3261 cpi->prob_last_coded = 128;
3262 cpi->prob_gf_coded = 128;
3264 else
3266 cpi->prob_intra_coded = (rf_intra * 255) / (rf_intra + rf_inter);
3268 if (cpi->prob_intra_coded < 1)
3269 cpi->prob_intra_coded = 1;
3271 if ((cm->frames_since_golden > 0) || cpi->source_alt_ref_active)
3273 cpi->prob_last_coded = rf_inter ? (rfct[LAST_FRAME] * 255) / rf_inter : 128;
3275 if (cpi->prob_last_coded < 1)
3276 cpi->prob_last_coded = 1;
3278 cpi->prob_gf_coded = (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME])
3279 ? (rfct[GOLDEN_FRAME] * 255) / (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME]) : 128;
3281 if (cpi->prob_gf_coded < 1)
3282 cpi->prob_gf_coded = 1;
3286 #else
3287 const int *const rfct = cpi->count_mb_ref_frame_usage;
3288 const int rf_intra = rfct[INTRA_FRAME];
3289 const int rf_inter = rfct[LAST_FRAME] + rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME];
3291 if (cm->frame_type == KEY_FRAME)
3293 cpi->prob_intra_coded = 255;
3294 cpi->prob_last_coded = 128;
3295 cpi->prob_gf_coded = 128;
3297 else if (!(rf_intra + rf_inter))
3299 // This is a trap in case this function is called with cpi->recent_ref_frame_usage[] blank.
3300 cpi->prob_intra_coded = 63;
3301 cpi->prob_last_coded = 128;
3302 cpi->prob_gf_coded = 128;
3304 else
3306 cpi->prob_intra_coded = (rf_intra * 255) / (rf_intra + rf_inter);
3308 if (cpi->prob_intra_coded < 1)
3309 cpi->prob_intra_coded = 1;
3311 cpi->prob_last_coded = rf_inter ? (rfct[LAST_FRAME] * 255) / rf_inter : 128;
3313 if (cpi->prob_last_coded < 1)
3314 cpi->prob_last_coded = 1;
3316 cpi->prob_gf_coded = (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME])
3317 ? (rfct[GOLDEN_FRAME] * 255) / (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME]) : 128;
3319 if (cpi->prob_gf_coded < 1)
3320 cpi->prob_gf_coded = 1;
3323 // update reference frame costs since we can do better than what we got last frame.
3325 if (cpi->common.refresh_alt_ref_frame)
3327 cpi->prob_intra_coded += 40;
3328 cpi->prob_last_coded = 200;
3329 cpi->prob_gf_coded = 1;
3331 else if (cpi->common.frames_since_golden == 0)
3333 cpi->prob_last_coded = 214;
3334 cpi->prob_gf_coded = 1;
3336 else if (cpi->common.frames_since_golden == 1)
3338 cpi->prob_last_coded = 192;
3339 cpi->prob_gf_coded = 220;
3341 else if (cpi->source_alt_ref_active)
3343 //int dist = cpi->common.frames_till_alt_ref_frame + cpi->common.frames_since_golden;
3344 cpi->prob_gf_coded -= 20;
3346 if (cpi->prob_gf_coded < 10)
3347 cpi->prob_gf_coded = 10;
3350 #endif
3354 // 1 = key, 0 = inter
3355 static int decide_key_frame(VP8_COMP *cpi)
3357 VP8_COMMON *cm = &cpi->common;
3359 int code_key_frame = FALSE;
3361 cpi->kf_boost = 0;
3363 if (cpi->Speed > 11)
3364 return FALSE;
3366 // Clear down mmx registers
3367 vp8_clear_system_state(); //__asm emms;
3369 if ((cpi->compressor_speed == 2) && (cpi->Speed >= 5) && (cpi->sf.RD == 0))
3371 double change = 1.0 * abs((int)(cpi->intra_error - cpi->last_intra_error)) / (1 + cpi->last_intra_error);
3372 double change2 = 1.0 * abs((int)(cpi->prediction_error - cpi->last_prediction_error)) / (1 + cpi->last_prediction_error);
3373 double minerror = cm->MBs * 256;
3375 #if 0
3377 if (10 * cpi->intra_error / (1 + cpi->prediction_error) < 15
3378 && cpi->prediction_error > minerror
3379 && (change > .25 || change2 > .25))
3381 FILE *f = fopen("intra_inter.stt", "a");
3383 if (cpi->prediction_error <= 0)
3384 cpi->prediction_error = 1;
3386 fprintf(f, "%d %d %d %d %14.4f\n",
3387 cm->current_video_frame,
3388 (int) cpi->prediction_error,
3389 (int) cpi->intra_error,
3390 (int)((10 * cpi->intra_error) / cpi->prediction_error),
3391 change);
3393 fclose(f);
3396 #endif
3398 cpi->last_intra_error = cpi->intra_error;
3399 cpi->last_prediction_error = cpi->prediction_error;
3401 if (10 * cpi->intra_error / (1 + cpi->prediction_error) < 15
3402 && cpi->prediction_error > minerror
3403 && (change > .25 || change2 > .25))
3405 /*(change > 1.4 || change < .75)&& cpi->this_frame_percent_intra > cpi->last_frame_percent_intra + 3*/
3406 return TRUE;
3409 return FALSE;
3413 // If the following are true we might as well code a key frame
3414 if (((cpi->this_frame_percent_intra == 100) &&
3415 (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra + 2))) ||
3416 ((cpi->this_frame_percent_intra > 95) &&
3417 (cpi->this_frame_percent_intra >= (cpi->last_frame_percent_intra + 5))))
3419 code_key_frame = TRUE;
3421 // in addition if the following are true and this is not a golden frame then code a key frame
3422 // Note that on golden frames there often seems to be a pop in intra useage anyway hence this
3423 // restriction is designed to prevent spurious key frames. The Intra pop needs to be investigated.
3424 else if (((cpi->this_frame_percent_intra > 60) &&
3425 (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra * 2))) ||
3426 ((cpi->this_frame_percent_intra > 75) &&
3427 (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra * 3 / 2))) ||
3428 ((cpi->this_frame_percent_intra > 90) &&
3429 (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra + 10))))
3431 if (!cm->refresh_golden_frame)
3432 code_key_frame = TRUE;
3435 return code_key_frame;
3439 #if !(CONFIG_REALTIME_ONLY)
3440 static void Pass1Encode(VP8_COMP *cpi, unsigned long *size, unsigned char *dest, unsigned int *frame_flags)
3442 (void) size;
3443 (void) dest;
3444 (void) frame_flags;
3445 set_quantizer(cpi, 26);
3447 scale_and_extend_source(cpi->un_scaled_source, cpi);
3448 vp8_first_pass(cpi);
3450 #endif
3452 #if 0
3453 void write_cx_frame_to_file(YV12_BUFFER_CONFIG *frame, int this_frame)
3456 // write the frame
3457 FILE *yframe;
3458 int i;
3459 char filename[255];
3461 sprintf(filename, "cx\\y%04d.raw", this_frame);
3462 yframe = fopen(filename, "wb");
3464 for (i = 0; i < frame->y_height; i++)
3465 fwrite(frame->y_buffer + i * frame->y_stride, frame->y_width, 1, yframe);
3467 fclose(yframe);
3468 sprintf(filename, "cx\\u%04d.raw", this_frame);
3469 yframe = fopen(filename, "wb");
3471 for (i = 0; i < frame->uv_height; i++)
3472 fwrite(frame->u_buffer + i * frame->uv_stride, frame->uv_width, 1, yframe);
3474 fclose(yframe);
3475 sprintf(filename, "cx\\v%04d.raw", this_frame);
3476 yframe = fopen(filename, "wb");
3478 for (i = 0; i < frame->uv_height; i++)
3479 fwrite(frame->v_buffer + i * frame->uv_stride, frame->uv_width, 1, yframe);
3481 fclose(yframe);
3483 #endif
3484 // return of 0 means drop frame
3486 // Function to test for conditions that indeicate we should loop
3487 // back and recode a frame.
3488 static BOOL recode_loop_test( VP8_COMP *cpi,
3489 int high_limit, int low_limit,
3490 int q, int maxq, int minq )
3492 BOOL force_recode = FALSE;
3493 VP8_COMMON *cm = &cpi->common;
3495 // Is frame recode allowed at all
3496 // Yes if either recode mode 1 is selected or mode two is selcted
3497 // and the frame is a key frame. golden frame or alt_ref_frame
3498 if ( (cpi->sf.recode_loop == 1) ||
3499 ( (cpi->sf.recode_loop == 2) &&
3500 ( (cm->frame_type == KEY_FRAME) ||
3501 cm->refresh_golden_frame ||
3502 cm->refresh_alt_ref_frame ) ) )
3504 // General over and under shoot tests
3505 if ( ((cpi->projected_frame_size > high_limit) && (q < maxq)) ||
3506 ((cpi->projected_frame_size < low_limit) && (q > minq)) )
3508 force_recode = TRUE;
3510 // Specific rate control mode related tests
3511 // TBD
3514 return force_recode;
3517 static void encode_frame_to_data_rate
3519 VP8_COMP *cpi,
3520 unsigned long *size,
3521 unsigned char *dest,
3522 unsigned int *frame_flags
3525 int Q;
3526 int frame_over_shoot_limit;
3527 int frame_under_shoot_limit;
3529 int Loop = FALSE;
3530 int loop_count;
3531 int this_q;
3532 int last_zbin_oq;
3534 int q_low;
3535 int q_high;
3536 int zbin_oq_high;
3537 int zbin_oq_low = 0;
3538 int top_index;
3539 int bottom_index;
3540 VP8_COMMON *cm = &cpi->common;
3541 int active_worst_qchanged = FALSE;
3543 int overshoot_seen = FALSE;
3544 int undershoot_seen = FALSE;
3545 int drop_mark = cpi->oxcf.drop_frames_water_mark * cpi->oxcf.optimal_buffer_level / 100;
3546 int drop_mark75 = drop_mark * 2 / 3;
3547 int drop_mark50 = drop_mark / 4;
3548 int drop_mark25 = drop_mark / 8;
3550 // Clear down mmx registers to allow floating point in what follows
3551 vp8_clear_system_state();
3553 // Test code for segmentation of gf/arf (0,0)
3554 //segmentation_test_function((VP8_PTR) cpi);
3556 // For an alt ref frame in 2 pass we skip the call to the second pass function that sets the target bandwidth
3557 #if !(CONFIG_REALTIME_ONLY)
3559 if (cpi->pass == 2)
3561 if (cpi->common.refresh_alt_ref_frame)
3563 cpi->per_frame_bandwidth = cpi->gf_bits; // Per frame bit target for the alt ref frame
3564 cpi->target_bandwidth = cpi->gf_bits * cpi->output_frame_rate; // per second target bitrate
3567 else
3568 #endif
3569 cpi->per_frame_bandwidth = (int)(cpi->target_bandwidth / cpi->output_frame_rate);
3571 // Default turn off buffer to buffer copying
3572 cm->copy_buffer_to_gf = 0;
3573 cm->copy_buffer_to_arf = 0;
3575 // Clear zbin over-quant value and mode boost values.
3576 cpi->zbin_over_quant = 0;
3577 cpi->zbin_mode_boost = 0;
3579 // Enable or disable mode based tweaking of the zbin
3580 // For 2 Pass Only used where GF/ARF prediction quality
3581 // is above a threshold
3582 cpi->zbin_mode_boost = 0;
3583 cpi->zbin_mode_boost_enabled = TRUE;
3584 if (cpi->pass == 2)
3586 if ( cpi->gfu_boost <= 400 )
3588 cpi->zbin_mode_boost_enabled = FALSE;
3592 // Current default encoder behaviour for the altref sign bias
3593 if (cpi->source_alt_ref_active)
3594 cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = 1;
3595 else
3596 cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = 0;
3598 // Check to see if a key frame is signalled
3599 // For two pass with auto key frame enabled cm->frame_type may already be set, but not for one pass.
3600 if ((cm->current_video_frame == 0) ||
3601 (cm->frame_flags & FRAMEFLAGS_KEY) ||
3602 (cpi->oxcf.auto_key && (cpi->frames_since_key % cpi->key_frame_frequency == 0)))
3604 // Key frame from VFW/auto-keyframe/first frame
3605 cm->frame_type = KEY_FRAME;
3608 // Set default state for segment and mode based loop filter update flags
3609 cpi->mb.e_mbd.update_mb_segmentation_map = 0;
3610 cpi->mb.e_mbd.update_mb_segmentation_data = 0;
3611 cpi->mb.e_mbd.mode_ref_lf_delta_update = 0;
3613 // Set various flags etc to special state if it is a key frame
3614 if (cm->frame_type == KEY_FRAME)
3616 int i;
3618 // Reset the loop filter deltas and segmentation map
3619 setup_features(cpi);
3621 // If segmentation is enabled force a map update for key frames
3622 if (cpi->mb.e_mbd.segmentation_enabled)
3624 cpi->mb.e_mbd.update_mb_segmentation_map = 1;
3625 cpi->mb.e_mbd.update_mb_segmentation_data = 1;
3628 // The alternate reference frame cannot be active for a key frame
3629 cpi->source_alt_ref_active = FALSE;
3631 // Reset the RD threshold multipliers to default of * 1 (128)
3632 for (i = 0; i < MAX_MODES; i++)
3634 cpi->rd_thresh_mult[i] = 128;
3638 // Test code for segmentation
3639 //if ( (cm->frame_type == KEY_FRAME) || ((cm->current_video_frame % 2) == 0))
3640 //if ( (cm->current_video_frame % 2) == 0 )
3641 // enable_segmentation((VP8_PTR)cpi);
3642 //else
3643 // disable_segmentation((VP8_PTR)cpi);
3645 #if 0
3646 // Experimental code for lagged compress and one pass
3647 // Initialise one_pass GF frames stats
3648 // Update stats used for GF selection
3649 //if ( cpi->pass == 0 )
3651 cpi->one_pass_frame_index = cm->current_video_frame % MAX_LAG_BUFFERS;
3653 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frames_so_far = 0;
3654 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_intra_error = 0.0;
3655 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_coded_error = 0.0;
3656 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_pcnt_inter = 0.0;
3657 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_pcnt_motion = 0.0;
3658 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvr = 0.0;
3659 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvr_abs = 0.0;
3660 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvc = 0.0;
3661 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvc_abs = 0.0;
3663 #endif
3665 update_rd_ref_frame_probs(cpi);
3667 if (cpi->drop_frames_allowed)
3669 // The reset to decimation 0 is only done here for one pass.
3670 // Once it is set two pass leaves decimation on till the next kf.
3671 if ((cpi->buffer_level > drop_mark) && (cpi->decimation_factor > 0))
3672 cpi->decimation_factor --;
3674 if (cpi->buffer_level > drop_mark75 && cpi->decimation_factor > 0)
3675 cpi->decimation_factor = 1;
3677 else if (cpi->buffer_level < drop_mark25 && (cpi->decimation_factor == 2 || cpi->decimation_factor == 3))
3679 cpi->decimation_factor = 3;
3681 else if (cpi->buffer_level < drop_mark50 && (cpi->decimation_factor == 1 || cpi->decimation_factor == 2))
3683 cpi->decimation_factor = 2;
3685 else if (cpi->buffer_level < drop_mark75 && (cpi->decimation_factor == 0 || cpi->decimation_factor == 1))
3687 cpi->decimation_factor = 1;
3690 //vpx_log("Encoder: Decimation Factor: %d \n",cpi->decimation_factor);
3693 // The following decimates the frame rate according to a regular pattern (i.e. to 1/2 or 2/3 frame rate)
3694 // This can be used to help prevent buffer under-run in CBR mode. Alternatively it might be desirable in
3695 // some situations to drop frame rate but throw more bits at each frame.
3697 // Note that dropping a key frame can be problematic if spatial resampling is also active
3698 if (cpi->decimation_factor > 0)
3700 switch (cpi->decimation_factor)
3702 case 1:
3703 cpi->per_frame_bandwidth = cpi->per_frame_bandwidth * 3 / 2;
3704 break;
3705 case 2:
3706 cpi->per_frame_bandwidth = cpi->per_frame_bandwidth * 5 / 4;
3707 break;
3708 case 3:
3709 cpi->per_frame_bandwidth = cpi->per_frame_bandwidth * 5 / 4;
3710 break;
3713 // Note that we should not throw out a key frame (especially when spatial resampling is enabled).
3714 if ((cm->frame_type == KEY_FRAME)) // && cpi->oxcf.allow_spatial_resampling )
3716 cpi->decimation_count = cpi->decimation_factor;
3718 else if (cpi->decimation_count > 0)
3720 cpi->decimation_count --;
3721 cpi->bits_off_target += cpi->av_per_frame_bandwidth;
3722 cm->current_video_frame++;
3723 cpi->frames_since_key++;
3725 #if CONFIG_PSNR
3726 cpi->count ++;
3727 #endif
3729 cpi->buffer_level = cpi->bits_off_target;
3731 return;
3733 else
3734 cpi->decimation_count = cpi->decimation_factor;
3737 // Decide how big to make the frame
3738 if (!pick_frame_size(cpi))
3740 cm->current_video_frame++;
3741 cpi->frames_since_key++;
3742 return;
3745 // Reduce active_worst_allowed_q for CBR if our buffer is getting too full.
3746 // This has a knock on effect on active best quality as well.
3747 // For CBR if the buffer reaches its maximum level then we can no longer
3748 // save up bits for later frames so we might as well use them up
3749 // on the current frame.
3750 if ((cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) &&
3751 (cpi->buffer_level >= cpi->oxcf.optimal_buffer_level) && cpi->buffered_mode)
3753 int Adjustment = cpi->active_worst_quality / 4; // Max adjustment is 1/4
3755 if (Adjustment)
3757 int buff_lvl_step;
3758 int tmp_lvl = cpi->buffer_level;
3760 if (cpi->buffer_level < cpi->oxcf.maximum_buffer_size)
3762 buff_lvl_step = (cpi->oxcf.maximum_buffer_size - cpi->oxcf.optimal_buffer_level) / Adjustment;
3764 if (buff_lvl_step)
3766 Adjustment = (cpi->buffer_level - cpi->oxcf.optimal_buffer_level) / buff_lvl_step;
3767 cpi->active_worst_quality -= Adjustment;
3770 else
3772 cpi->active_worst_quality -= Adjustment;
3777 // Set an active best quality and if necessary active worst quality
3778 if (cpi->pass == 2 || (cm->current_video_frame > 150))
3780 int Q;
3781 int i;
3782 int bpm_target;
3783 //int tmp;
3785 vp8_clear_system_state();
3787 Q = cpi->active_worst_quality;
3789 if ((cm->frame_type == KEY_FRAME) || cm->refresh_golden_frame || cpi->common.refresh_alt_ref_frame)
3791 if (cm->frame_type != KEY_FRAME)
3793 if (cpi->avg_frame_qindex < cpi->active_worst_quality)
3794 Q = cpi->avg_frame_qindex;
3796 if ( cpi->gfu_boost > 1000 )
3797 cpi->active_best_quality = gf_low_motion_minq[Q];
3798 else if ( cpi->gfu_boost < 400 )
3799 cpi->active_best_quality = gf_high_motion_minq[Q];
3800 else
3801 cpi->active_best_quality = gf_mid_motion_minq[Q];
3803 /*cpi->active_best_quality = gf_arf_minq[Q];
3804 tmp = (cpi->gfu_boost > 1000) ? 600 : cpi->gfu_boost - 400;
3805 //tmp = (cpi->gfu_boost > 1000) ? 600 :
3806 //(cpi->gfu_boost < 400) ? 0 : cpi->gfu_boost - 400;
3807 tmp = 128 - (tmp >> 4);
3808 cpi->active_best_quality = (cpi->active_best_quality * tmp)>>7;*/
3811 // KEY FRAMES
3812 else
3814 if (cpi->gfu_boost > 600)
3815 cpi->active_best_quality = kf_low_motion_minq[Q];
3816 else
3817 cpi->active_best_quality = kf_high_motion_minq[Q];
3820 else
3822 cpi->active_best_quality = inter_minq[Q];
3825 // If CBR and the buffer is as full then it is reasonable to allow higher quality on the frames
3826 // to prevent bits just going to waste.
3827 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
3829 // Note that the use of >= here elliminates the risk of a devide by 0 error in the else if clause
3830 if (cpi->buffer_level >= cpi->oxcf.maximum_buffer_size)
3831 cpi->active_best_quality = cpi->best_quality;
3833 else if (cpi->buffer_level > cpi->oxcf.optimal_buffer_level)
3835 int Fraction = ((cpi->buffer_level - cpi->oxcf.optimal_buffer_level) * 128) / (cpi->oxcf.maximum_buffer_size - cpi->oxcf.optimal_buffer_level);
3836 int min_qadjustment = ((cpi->active_best_quality - cpi->best_quality) * Fraction) / 128;
3838 cpi->active_best_quality -= min_qadjustment;
3844 // Clip the active best and worst quality values to limits
3845 if (cpi->active_worst_quality > cpi->worst_quality)
3846 cpi->active_worst_quality = cpi->worst_quality;
3848 if (cpi->active_best_quality < cpi->best_quality)
3849 cpi->active_best_quality = cpi->best_quality;
3850 else if (cpi->active_best_quality > cpi->active_worst_quality)
3851 cpi->active_best_quality = cpi->active_worst_quality;
3853 // Determine initial Q to try
3854 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
3855 last_zbin_oq = cpi->zbin_over_quant;
3857 // Set highest allowed value for Zbin over quant
3858 if (cm->frame_type == KEY_FRAME)
3859 zbin_oq_high = 0; //ZBIN_OQ_MAX/16
3860 else if (cm->refresh_alt_ref_frame || (cm->refresh_golden_frame && !cpi->source_alt_ref_active))
3861 zbin_oq_high = 16;
3862 else
3863 zbin_oq_high = ZBIN_OQ_MAX;
3865 // Setup background Q adjustment for error resilliant mode
3866 if (cpi->cyclic_refresh_mode_enabled)
3867 cyclic_background_refresh(cpi, Q, 0);
3869 vp8_compute_frame_size_bounds(cpi, &frame_under_shoot_limit, &frame_over_shoot_limit);
3871 // Limit Q range for the adaptive loop.
3872 bottom_index = cpi->active_best_quality;
3873 top_index = cpi->active_worst_quality;
3874 q_low = cpi->active_best_quality;
3875 q_high = cpi->active_worst_quality;
3877 vp8_save_coding_context(cpi);
3879 loop_count = 0;
3882 scale_and_extend_source(cpi->un_scaled_source, cpi);
3883 #if !(CONFIG_REALTIME_ONLY) && CONFIG_POSTPROC
3885 if (cpi->oxcf.noise_sensitivity > 0)
3887 unsigned char *src;
3888 int l = 0;
3890 switch (cpi->oxcf.noise_sensitivity)
3892 case 1:
3893 l = 20;
3894 break;
3895 case 2:
3896 l = 40;
3897 break;
3898 case 3:
3899 l = 60;
3900 break;
3901 case 4:
3902 l = 80;
3903 break;
3904 case 5:
3905 l = 100;
3906 break;
3907 case 6:
3908 l = 150;
3909 break;
3913 if (cm->frame_type == KEY_FRAME)
3915 vp8_de_noise(cpi->Source, cpi->Source, l , 1, 0, RTCD(postproc));
3917 else
3919 vp8_de_noise(cpi->Source, cpi->Source, l , 1, 0, RTCD(postproc));
3921 src = cpi->Source->y_buffer;
3923 if (cpi->Source->y_stride < 0)
3925 src += cpi->Source->y_stride * (cpi->Source->y_height - 1);
3930 #endif
3932 #ifdef OUTPUT_YUV_SRC
3933 vp8_write_yuv_frame(cpi->Source);
3934 #endif
3938 vp8_clear_system_state(); //__asm emms;
3941 if(cpi->is_src_frame_alt_ref)
3942 Q = 127;
3945 set_quantizer(cpi, Q);
3946 this_q = Q;
3948 // setup skip prob for costing in mode/mv decision
3949 if (cpi->common.mb_no_coeff_skip)
3951 cpi->prob_skip_false = cpi->base_skip_false_prob[Q];
3953 if (cm->frame_type != KEY_FRAME)
3955 if (cpi->common.refresh_alt_ref_frame)
3957 if (cpi->last_skip_false_probs[2] != 0)
3958 cpi->prob_skip_false = cpi->last_skip_false_probs[2];
3961 if(cpi->last_skip_false_probs[2]!=0 && abs(Q- cpi->last_skip_probs_q[2])<=16 )
3962 cpi->prob_skip_false = cpi->last_skip_false_probs[2];
3963 else if (cpi->last_skip_false_probs[2]!=0)
3964 cpi->prob_skip_false = (cpi->last_skip_false_probs[2] + cpi->prob_skip_false ) / 2;
3967 else if (cpi->common.refresh_golden_frame)
3969 if (cpi->last_skip_false_probs[1] != 0)
3970 cpi->prob_skip_false = cpi->last_skip_false_probs[1];
3973 if(cpi->last_skip_false_probs[1]!=0 && abs(Q- cpi->last_skip_probs_q[1])<=16 )
3974 cpi->prob_skip_false = cpi->last_skip_false_probs[1];
3975 else if (cpi->last_skip_false_probs[1]!=0)
3976 cpi->prob_skip_false = (cpi->last_skip_false_probs[1] + cpi->prob_skip_false ) / 2;
3979 else
3981 if (cpi->last_skip_false_probs[0] != 0)
3982 cpi->prob_skip_false = cpi->last_skip_false_probs[0];
3985 if(cpi->last_skip_false_probs[0]!=0 && abs(Q- cpi->last_skip_probs_q[0])<=16 )
3986 cpi->prob_skip_false = cpi->last_skip_false_probs[0];
3987 else if(cpi->last_skip_false_probs[0]!=0)
3988 cpi->prob_skip_false = (cpi->last_skip_false_probs[0] + cpi->prob_skip_false ) / 2;
3992 //as this is for cost estimate, let's make sure it does not go extreme eitehr way
3993 if (cpi->prob_skip_false < 5)
3994 cpi->prob_skip_false = 5;
3996 if (cpi->prob_skip_false > 250)
3997 cpi->prob_skip_false = 250;
3999 if (cpi->is_src_frame_alt_ref)
4000 cpi->prob_skip_false = 1;
4005 #if 0
4007 if (cpi->pass != 1)
4009 FILE *f = fopen("skip.stt", "a");
4010 fprintf(f, "%d, %d, %4d ", cpi->common.refresh_golden_frame, cpi->common.refresh_alt_ref_frame, cpi->prob_skip_false);
4011 fclose(f);
4014 #endif
4018 if (cm->frame_type == KEY_FRAME)
4019 vp8_setup_key_frame(cpi);
4021 // transform / motion compensation build reconstruction frame
4023 vp8_encode_frame(cpi);
4024 cpi->projected_frame_size -= vp8_estimate_entropy_savings(cpi);
4025 cpi->projected_frame_size = (cpi->projected_frame_size > 0) ? cpi->projected_frame_size : 0;
4027 vp8_clear_system_state(); //__asm emms;
4029 // Test to see if the stats generated for this frame indicate that we should have coded a key frame
4030 // (assuming that we didn't)!
4031 if (cpi->pass != 2 && cpi->oxcf.auto_key && cm->frame_type != KEY_FRAME)
4033 if (decide_key_frame(cpi))
4035 vp8_calc_auto_iframe_target_size(cpi);
4037 // Reset all our sizing numbers and recode
4038 cm->frame_type = KEY_FRAME;
4040 // Clear the Alt reference frame active flag when we have a key frame
4041 cpi->source_alt_ref_active = FALSE;
4043 // Reset the loop filter deltas and segmentation map
4044 setup_features(cpi);
4046 // If segmentation is enabled force a map update for key frames
4047 if (cpi->mb.e_mbd.segmentation_enabled)
4049 cpi->mb.e_mbd.update_mb_segmentation_map = 1;
4050 cpi->mb.e_mbd.update_mb_segmentation_data = 1;
4053 vp8_restore_coding_context(cpi);
4055 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4057 vp8_compute_frame_size_bounds(cpi, &frame_under_shoot_limit, &frame_over_shoot_limit);
4059 // Limit Q range for the adaptive loop.
4060 bottom_index = cpi->active_best_quality;
4061 top_index = cpi->active_worst_quality;
4062 q_low = cpi->active_best_quality;
4063 q_high = cpi->active_worst_quality;
4065 loop_count++;
4066 Loop = TRUE;
4068 resize_key_frame(cpi);
4069 continue;
4073 vp8_clear_system_state();
4075 if (frame_over_shoot_limit == 0)
4076 frame_over_shoot_limit = 1;
4078 // Are we are overshooting and up against the limit of active max Q.
4079 if (((cpi->pass != 2) || (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)) &&
4080 (Q == cpi->active_worst_quality) &&
4081 (cpi->active_worst_quality < cpi->worst_quality) &&
4082 (cpi->projected_frame_size > frame_over_shoot_limit))
4084 int over_size_percent = ((cpi->projected_frame_size - frame_over_shoot_limit) * 100) / frame_over_shoot_limit;
4086 // If so is there any scope for relaxing it
4087 while ((cpi->active_worst_quality < cpi->worst_quality) && (over_size_percent > 0))
4089 cpi->active_worst_quality++;
4090 top_index = cpi->active_worst_quality;
4091 over_size_percent = (int)(over_size_percent * 0.96); // Assume 1 qstep = about 4% on frame size.
4094 // If we have updated the active max Q do not call vp8_update_rate_correction_factors() this loop.
4095 active_worst_qchanged = TRUE;
4097 else
4098 active_worst_qchanged = FALSE;
4100 #if !(CONFIG_REALTIME_ONLY)
4102 // Is the projected frame size out of range and are we allowed to attempt to recode.
4103 if ( recode_loop_test( cpi,
4104 frame_over_shoot_limit, frame_under_shoot_limit,
4105 Q, top_index, bottom_index ) )
4107 int last_q = Q;
4108 int Retries = 0;
4110 // Frame size out of permitted range:
4111 // Update correction factor & compute new Q to try...
4113 // Frame is too large
4114 if (cpi->projected_frame_size > cpi->this_frame_target)
4116 //if ( cpi->zbin_over_quant == 0 )
4117 q_low = (Q < q_high) ? (Q + 1) : q_high; // Raise Qlow as to at least the current value
4119 if (cpi->zbin_over_quant > 0) // If we are using over quant do the same for zbin_oq_low
4120 zbin_oq_low = (cpi->zbin_over_quant < zbin_oq_high) ? (cpi->zbin_over_quant + 1) : zbin_oq_high;
4122 //if ( undershoot_seen || (Q == MAXQ) )
4123 if (undershoot_seen)
4125 // Update rate_correction_factor unless cpi->active_worst_quality has changed.
4126 if (!active_worst_qchanged)
4127 vp8_update_rate_correction_factors(cpi, 1);
4129 Q = (q_high + q_low + 1) / 2;
4131 // Adjust cpi->zbin_over_quant (only allowed when Q is max)
4132 if (Q < MAXQ)
4133 cpi->zbin_over_quant = 0;
4134 else
4136 zbin_oq_low = (cpi->zbin_over_quant < zbin_oq_high) ? (cpi->zbin_over_quant + 1) : zbin_oq_high;
4137 cpi->zbin_over_quant = (zbin_oq_high + zbin_oq_low) / 2;
4140 else
4142 // Update rate_correction_factor unless cpi->active_worst_quality has changed.
4143 if (!active_worst_qchanged)
4144 vp8_update_rate_correction_factors(cpi, 0);
4146 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4148 while (((Q < q_low) || (cpi->zbin_over_quant < zbin_oq_low)) && (Retries < 10))
4150 vp8_update_rate_correction_factors(cpi, 0);
4151 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4152 Retries ++;
4156 overshoot_seen = TRUE;
4158 // Frame is too small
4159 else
4161 if (cpi->zbin_over_quant == 0)
4162 q_high = (Q > q_low) ? (Q - 1) : q_low; // Lower q_high if not using over quant
4163 else // else lower zbin_oq_high
4164 zbin_oq_high = (cpi->zbin_over_quant > zbin_oq_low) ? (cpi->zbin_over_quant - 1) : zbin_oq_low;
4166 if (overshoot_seen)
4168 // Update rate_correction_factor unless cpi->active_worst_quality has changed.
4169 if (!active_worst_qchanged)
4170 vp8_update_rate_correction_factors(cpi, 1);
4172 Q = (q_high + q_low) / 2;
4174 // Adjust cpi->zbin_over_quant (only allowed when Q is max)
4175 if (Q < MAXQ)
4176 cpi->zbin_over_quant = 0;
4177 else
4178 cpi->zbin_over_quant = (zbin_oq_high + zbin_oq_low) / 2;
4180 else
4182 // Update rate_correction_factor unless cpi->active_worst_quality has changed.
4183 if (!active_worst_qchanged)
4184 vp8_update_rate_correction_factors(cpi, 0);
4186 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4188 while (((Q > q_high) || (cpi->zbin_over_quant > zbin_oq_high)) && (Retries < 10))
4190 vp8_update_rate_correction_factors(cpi, 0);
4191 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4192 Retries ++;
4196 undershoot_seen = TRUE;
4199 // Clamp Q to upper and lower limits:
4200 if (Q > q_high)
4201 Q = q_high;
4202 else if (Q < q_low)
4203 Q = q_low;
4205 // Clamp cpi->zbin_over_quant
4206 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;
4208 //Loop = ((Q != last_q) || (last_zbin_oq != cpi->zbin_over_quant)) ? TRUE : FALSE;
4209 Loop = ((Q != last_q)) ? TRUE : FALSE;
4210 last_zbin_oq = cpi->zbin_over_quant;
4212 else
4213 #endif
4214 Loop = FALSE;
4216 if (cpi->is_src_frame_alt_ref)
4217 Loop = FALSE;
4219 if (Loop == TRUE)
4221 vp8_restore_coding_context(cpi);
4222 loop_count++;
4223 #if CONFIG_PSNR
4224 cpi->tot_recode_hits++;
4225 #endif
4228 while (Loop == TRUE);
4230 #if 0
4231 // Experimental code for lagged and one pass
4232 // Update stats used for one pass GF selection
4235 int frames_so_far;
4236 double frame_intra_error;
4237 double frame_coded_error;
4238 double frame_pcnt_inter;
4239 double frame_pcnt_motion;
4240 double frame_mvr;
4241 double frame_mvr_abs;
4242 double frame_mvc;
4243 double frame_mvc_abs;
4246 cpi->one_pass_frame_stats[cpi->one_pass_frame_index].frame_coded_error = (double)cpi->prediction_error;
4247 cpi->one_pass_frame_stats[cpi->one_pass_frame_index].frame_intra_error = (double)cpi->intra_error;
4248 cpi->one_pass_frame_stats[cpi->one_pass_frame_index].frame_pcnt_inter = (double)(100 - cpi->this_frame_percent_intra) / 100.0;
4250 #endif
4252 // Update the GF useage maps.
4253 // This is done after completing the compression of a frame when all modes etc. are finalized but before loop filter
4254 vp8_update_gf_useage_maps(cpi, cm, &cpi->mb);
4256 // This frame's MVs are saved and will be used in next frame's MV prediction.
4257 if(cm->show_frame) //do not save for altref frame
4259 int mb_row;
4260 int mb_col;
4261 MODE_INFO *tmp = cm->mip; //point to beginning of allocated MODE_INFO arrays.
4262 //static int last_video_frame = 0;
4264 if(cm->frame_type != KEY_FRAME)
4266 for (mb_row = 0; mb_row < cm->mb_rows+1; mb_row ++)
4268 for (mb_col = 0; mb_col < cm->mb_cols+1; mb_col ++)
4270 if(tmp->mbmi.ref_frame != INTRA_FRAME)
4271 cpi->lfmv[mb_col + mb_row*(cm->mode_info_stride)].as_int = tmp->mbmi.mv.as_int;
4273 cpi->lf_ref_frame_sign_bias[mb_col + mb_row*(cm->mode_info_stride)] = cm->ref_frame_sign_bias[tmp->mbmi.ref_frame];
4274 cpi->lf_ref_frame[mb_col + mb_row*(cm->mode_info_stride)] = tmp->mbmi.ref_frame;
4275 tmp++;
4282 // Update the GF useage maps.
4283 // This is done after completing the compression of a frame when all modes etc. are finalized but before loop filter
4284 vp8_update_gf_useage_maps(cpi, cm, &cpi->mb);
4286 if (cm->frame_type == KEY_FRAME)
4287 cm->refresh_last_frame = 1;
4289 #if 0
4291 FILE *f = fopen("gfactive.stt", "a");
4292 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);
4293 fclose(f);
4295 #endif
4297 // 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
4298 // This is purely an encoder descision at present.
4299 if (!cpi->oxcf.error_resilient_mode && cm->refresh_golden_frame)
4300 cm->copy_buffer_to_arf = 2;
4301 else
4302 cm->copy_buffer_to_arf = 0;
4304 if (cm->refresh_last_frame)
4306 vp8_swap_yv12_buffer(&cm->yv12_fb[cm->lst_fb_idx], &cm->yv12_fb[cm->new_fb_idx]);
4307 cm->frame_to_show = &cm->yv12_fb[cm->lst_fb_idx];
4309 else
4310 cm->frame_to_show = &cm->yv12_fb[cm->new_fb_idx];
4314 //#pragma omp parallel sections
4317 //#pragma omp section
4320 struct vpx_usec_timer timer;
4322 vpx_usec_timer_start(&timer);
4324 if (cpi->sf.auto_filter == 0)
4325 vp8cx_pick_filter_level_fast(cpi->Source, cpi);
4326 else
4327 vp8cx_pick_filter_level(cpi->Source, cpi);
4329 vpx_usec_timer_mark(&timer);
4331 cpi->time_pick_lpf += vpx_usec_timer_elapsed(&timer);
4333 if (cm->no_lpf)
4334 cm->filter_level = 0;
4336 if (cm->filter_level > 0)
4338 vp8cx_set_alt_lf_level(cpi, cm->filter_level);
4339 vp8_loop_filter_frame(cm, &cpi->mb.e_mbd, cm->filter_level);
4340 cm->last_frame_type = cm->frame_type;
4341 cm->last_filter_type = cm->filter_type;
4342 cm->last_sharpness_level = cm->sharpness_level;
4345 vp8_yv12_extend_frame_borders_ptr(cm->frame_to_show);
4347 if (cpi->oxcf.error_resilient_mode == 1)
4349 cm->refresh_entropy_probs = 0;
4353 //#pragma omp section
4355 // build the bitstream
4356 vp8_pack_bitstream(cpi, dest, size);
4361 YV12_BUFFER_CONFIG *lst_yv12 = &cm->yv12_fb[cm->lst_fb_idx];
4362 YV12_BUFFER_CONFIG *new_yv12 = &cm->yv12_fb[cm->new_fb_idx];
4363 YV12_BUFFER_CONFIG *gld_yv12 = &cm->yv12_fb[cm->gld_fb_idx];
4364 YV12_BUFFER_CONFIG *alt_yv12 = &cm->yv12_fb[cm->alt_fb_idx];
4365 // At this point the new frame has been encoded coded.
4366 // If any buffer copy / swaping is signalled it should be done here.
4367 if (cm->frame_type == KEY_FRAME)
4369 vp8_yv12_copy_frame_ptr(cm->frame_to_show, gld_yv12);
4370 vp8_yv12_copy_frame_ptr(cm->frame_to_show, alt_yv12);
4372 else // For non key frames
4374 // Code to copy between reference buffers
4375 if (cm->copy_buffer_to_arf)
4377 if (cm->copy_buffer_to_arf == 1)
4379 if (cm->refresh_last_frame)
4380 // We copy new_frame here because last and new buffers will already have been swapped if cm->refresh_last_frame is set.
4381 vp8_yv12_copy_frame_ptr(new_yv12, alt_yv12);
4382 else
4383 vp8_yv12_copy_frame_ptr(lst_yv12, alt_yv12);
4385 else if (cm->copy_buffer_to_arf == 2)
4386 vp8_yv12_copy_frame_ptr(gld_yv12, alt_yv12);
4389 if (cm->copy_buffer_to_gf)
4391 if (cm->copy_buffer_to_gf == 1)
4393 if (cm->refresh_last_frame)
4394 // We copy new_frame here because last and new buffers will already have been swapped if cm->refresh_last_frame is set.
4395 vp8_yv12_copy_frame_ptr(new_yv12, gld_yv12);
4396 else
4397 vp8_yv12_copy_frame_ptr(lst_yv12, gld_yv12);
4399 else if (cm->copy_buffer_to_gf == 2)
4400 vp8_yv12_copy_frame_ptr(alt_yv12, gld_yv12);
4405 // Update rate control heuristics
4406 cpi->total_byte_count += (*size);
4407 cpi->projected_frame_size = (*size) << 3;
4409 if (!active_worst_qchanged)
4410 vp8_update_rate_correction_factors(cpi, 2);
4412 cpi->last_q[cm->frame_type] = cm->base_qindex;
4414 if (cm->frame_type == KEY_FRAME)
4416 vp8_adjust_key_frame_context(cpi);
4419 // Keep a record of ambient average Q.
4420 if (cm->frame_type == KEY_FRAME)
4421 cpi->avg_frame_qindex = cm->base_qindex;
4422 else
4423 cpi->avg_frame_qindex = (2 + 3 * cpi->avg_frame_qindex + cm->base_qindex) >> 2;
4425 // Keep a record from which we can calculate the average Q excluding GF updates and key frames
4426 if ((cm->frame_type != KEY_FRAME) && !cm->refresh_golden_frame && !cm->refresh_alt_ref_frame)
4428 cpi->ni_frames++;
4430 // Calculate the average Q for normal inter frames (not key or GFU frames)
4431 // This is used as a basis for setting active worst quality.
4432 if (cpi->ni_frames > 150)
4434 cpi->ni_tot_qi += Q;
4435 cpi->ni_av_qi = (cpi->ni_tot_qi / cpi->ni_frames);
4437 // Early in the clip ... average the current frame Q value with the default
4438 // entered by the user as a dampening measure
4439 else
4441 cpi->ni_tot_qi += Q;
4442 cpi->ni_av_qi = ((cpi->ni_tot_qi / cpi->ni_frames) + cpi->worst_quality + 1) / 2;
4445 // If the average Q is higher than what was used in the last frame
4446 // (after going through the recode loop to keep the frame size within range)
4447 // then use the last frame value - 1.
4448 // The -1 is designed to stop Q and hence the data rate, from progressively
4449 // falling away during difficult sections, but at the same time reduce the number of
4450 // itterations around the recode loop.
4451 if (Q > cpi->ni_av_qi)
4452 cpi->ni_av_qi = Q - 1;
4456 #if 0
4458 // If the frame was massively oversize and we are below optimal buffer level drop next frame
4459 if ((cpi->drop_frames_allowed) &&
4460 (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) &&
4461 (cpi->buffer_level < cpi->oxcf.drop_frames_water_mark * cpi->oxcf.optimal_buffer_level / 100) &&
4462 (cpi->projected_frame_size > (4 * cpi->this_frame_target)))
4464 cpi->drop_frame = TRUE;
4467 #endif
4469 // Set the count for maximum consequative dropped frames based upon the ratio of
4470 // this frame size to the target average per frame bandwidth.
4471 // (cpi->av_per_frame_bandwidth > 0) is just a sanity check to prevent / 0.
4472 if (cpi->drop_frames_allowed && (cpi->av_per_frame_bandwidth > 0))
4474 cpi->max_drop_count = cpi->projected_frame_size / cpi->av_per_frame_bandwidth;
4476 if (cpi->max_drop_count > cpi->max_consec_dropped_frames)
4477 cpi->max_drop_count = cpi->max_consec_dropped_frames;
4480 // Update the buffer level variable.
4481 if (cpi->common.refresh_alt_ref_frame)
4482 cpi->bits_off_target -= cpi->projected_frame_size;
4483 else
4484 cpi->bits_off_target += cpi->av_per_frame_bandwidth - cpi->projected_frame_size;
4486 // Rolling monitors of whether we are over or underspending used to help regulate min and Max Q in two pass.
4487 cpi->rolling_target_bits = ((cpi->rolling_target_bits * 3) + cpi->this_frame_target + 2) / 4;
4488 cpi->rolling_actual_bits = ((cpi->rolling_actual_bits * 3) + cpi->projected_frame_size + 2) / 4;
4489 cpi->long_rolling_target_bits = ((cpi->long_rolling_target_bits * 31) + cpi->this_frame_target + 16) / 32;
4490 cpi->long_rolling_actual_bits = ((cpi->long_rolling_actual_bits * 31) + cpi->projected_frame_size + 16) / 32;
4492 // Actual bits spent
4493 cpi->total_actual_bits += cpi->projected_frame_size;
4495 // Debug stats
4496 cpi->total_target_vs_actual += (cpi->this_frame_target - cpi->projected_frame_size);
4498 cpi->buffer_level = cpi->bits_off_target;
4500 // Update bits left to the kf and gf groups to account for overshoot or undershoot on these frames
4501 if (cm->frame_type == KEY_FRAME)
4503 cpi->kf_group_bits += cpi->this_frame_target - cpi->projected_frame_size;
4505 if (cpi->kf_group_bits < 0)
4506 cpi->kf_group_bits = 0 ;
4508 else if (cm->refresh_golden_frame || cm->refresh_alt_ref_frame)
4510 cpi->gf_group_bits += cpi->this_frame_target - cpi->projected_frame_size;
4512 if (cpi->gf_group_bits < 0)
4513 cpi->gf_group_bits = 0 ;
4516 if (cm->frame_type != KEY_FRAME)
4518 if (cpi->common.refresh_alt_ref_frame)
4520 cpi->last_skip_false_probs[2] = cpi->prob_skip_false;
4521 cpi->last_skip_probs_q[2] = cm->base_qindex;
4523 else if (cpi->common.refresh_golden_frame)
4525 cpi->last_skip_false_probs[1] = cpi->prob_skip_false;
4526 cpi->last_skip_probs_q[1] = cm->base_qindex;
4528 else
4530 cpi->last_skip_false_probs[0] = cpi->prob_skip_false;
4531 cpi->last_skip_probs_q[0] = cm->base_qindex;
4533 //update the baseline
4534 cpi->base_skip_false_prob[cm->base_qindex] = cpi->prob_skip_false;
4539 #if 0 && CONFIG_PSNR
4541 FILE *f = fopen("tmp.stt", "a");
4543 vp8_clear_system_state(); //__asm emms;
4545 if (cpi->total_coded_error_left != 0.0)
4546 fprintf(f, "%10d %10d %10d %10d %10d %10d %10d %10d %6ld %6ld"
4547 "%6ld %6ld %5ld %5ld %5ld %8ld %8.2f %10d %10.3f"
4548 "%10.3f %8ld\n",
4549 cpi->common.current_video_frame, cpi->this_frame_target,
4550 cpi->projected_frame_size,
4551 (cpi->projected_frame_size - cpi->this_frame_target),
4552 (int)cpi->total_target_vs_actual,
4553 (cpi->oxcf.starting_buffer_level-cpi->bits_off_target),
4554 (int)cpi->total_actual_bits, cm->base_qindex,
4555 cpi->active_best_quality, cpi->active_worst_quality,
4556 cpi->avg_frame_qindex, cpi->zbin_over_quant,
4557 cm->refresh_golden_frame, cm->refresh_alt_ref_frame,
4558 cm->frame_type, cpi->gfu_boost,
4559 cpi->est_max_qcorrection_factor, (int)cpi->bits_left,
4560 cpi->total_coded_error_left,
4561 (double)cpi->bits_left / cpi->total_coded_error_left,
4562 cpi->tot_recode_hits);
4563 else
4564 fprintf(f, "%10d %10d %10d %10d %10d %10d %10d %10d %6ld %6ld"
4565 "%6ld %6ld %5ld %5ld %5ld %8ld %8.2f %10d %10.3f"
4566 "%8ld\n",
4567 cpi->common.current_video_frame,
4568 cpi->this_frame_target, cpi->projected_frame_size,
4569 (cpi->projected_frame_size - cpi->this_frame_target),
4570 (int)cpi->total_target_vs_actual,
4571 (cpi->oxcf.starting_buffer_level-cpi->bits_off_target),
4572 (int)cpi->total_actual_bits, cm->base_qindex,
4573 cpi->active_best_quality, cpi->active_worst_quality,
4574 cpi->avg_frame_qindex, cpi->zbin_over_quant,
4575 cm->refresh_golden_frame, cm->refresh_alt_ref_frame,
4576 cm->frame_type, cpi->gfu_boost,
4577 cpi->est_max_qcorrection_factor, (int)cpi->bits_left,
4578 cpi->total_coded_error_left, cpi->tot_recode_hits);
4580 fclose(f);
4583 FILE *fmodes = fopen("Modes.stt", "a");
4584 int i;
4586 fprintf(fmodes, "%6d:%1d:%1d:%1d ",
4587 cpi->common.current_video_frame,
4588 cm->frame_type, cm->refresh_golden_frame,
4589 cm->refresh_alt_ref_frame);
4591 for (i = 0; i < MAX_MODES; i++)
4592 fprintf(fmodes, "%5d ", cpi->mode_chosen_counts[i]);
4594 fprintf(fmodes, "\n");
4596 fclose(fmodes);
4600 #endif
4602 // If this was a kf or Gf note the Q
4603 if ((cm->frame_type == KEY_FRAME) || cm->refresh_golden_frame || cm->refresh_alt_ref_frame)
4604 cm->last_kf_gf_q = cm->base_qindex;
4606 if (cm->refresh_golden_frame == 1)
4607 cm->frame_flags = cm->frame_flags | FRAMEFLAGS_GOLDEN;
4608 else
4609 cm->frame_flags = cm->frame_flags&~FRAMEFLAGS_GOLDEN;
4611 if (cm->refresh_alt_ref_frame == 1)
4612 cm->frame_flags = cm->frame_flags | FRAMEFLAGS_ALTREF;
4613 else
4614 cm->frame_flags = cm->frame_flags&~FRAMEFLAGS_ALTREF;
4617 if (cm->refresh_last_frame & cm->refresh_golden_frame) // both refreshed
4618 cpi->gold_is_last = 1;
4619 else if (cm->refresh_last_frame ^ cm->refresh_golden_frame) // 1 refreshed but not the other
4620 cpi->gold_is_last = 0;
4622 if (cm->refresh_last_frame & cm->refresh_alt_ref_frame) // both refreshed
4623 cpi->alt_is_last = 1;
4624 else if (cm->refresh_last_frame ^ cm->refresh_alt_ref_frame) // 1 refreshed but not the other
4625 cpi->alt_is_last = 0;
4627 if (cm->refresh_alt_ref_frame & cm->refresh_golden_frame) // both refreshed
4628 cpi->gold_is_alt = 1;
4629 else if (cm->refresh_alt_ref_frame ^ cm->refresh_golden_frame) // 1 refreshed but not the other
4630 cpi->gold_is_alt = 0;
4632 cpi->ref_frame_flags = VP8_ALT_FLAG | VP8_GOLD_FLAG | VP8_LAST_FLAG;
4634 if (cpi->gold_is_last)
4635 cpi->ref_frame_flags &= ~VP8_GOLD_FLAG;
4637 if (cpi->alt_is_last)
4638 cpi->ref_frame_flags &= ~VP8_ALT_FLAG;
4640 if (cpi->gold_is_alt)
4641 cpi->ref_frame_flags &= ~VP8_ALT_FLAG;
4644 if (cpi->oxcf.error_resilient_mode)
4646 // Is this an alternate reference update
4647 if (cpi->common.refresh_alt_ref_frame)
4648 vp8_yv12_copy_frame_ptr(cm->frame_to_show, &cm->yv12_fb[cm->alt_fb_idx]);
4650 if (cpi->common.refresh_golden_frame)
4651 vp8_yv12_copy_frame_ptr(cm->frame_to_show, &cm->yv12_fb[cm->gld_fb_idx]);
4653 else
4655 if (cpi->oxcf.play_alternate && cpi->common.refresh_alt_ref_frame && (cpi->common.frame_type != KEY_FRAME))
4656 // Update the alternate reference frame and stats as appropriate.
4657 update_alt_ref_frame_and_stats(cpi);
4658 else
4659 // Update the Golden frame and golden frame and stats as appropriate.
4660 update_golden_frame_and_stats(cpi);
4663 if (cm->frame_type == KEY_FRAME)
4665 // Tell the caller that the frame was coded as a key frame
4666 *frame_flags = cm->frame_flags | FRAMEFLAGS_KEY;
4668 // As this frame is a key frame the next defaults to an inter frame.
4669 cm->frame_type = INTER_FRAME;
4671 cpi->last_frame_percent_intra = 100;
4673 else
4675 *frame_flags = cm->frame_flags&~FRAMEFLAGS_KEY;
4677 cpi->last_frame_percent_intra = cpi->this_frame_percent_intra;
4680 // Clear the one shot update flags for segmentation map and mode/ref loop filter deltas.
4681 cpi->mb.e_mbd.update_mb_segmentation_map = 0;
4682 cpi->mb.e_mbd.update_mb_segmentation_data = 0;
4683 cpi->mb.e_mbd.mode_ref_lf_delta_update = 0;
4686 // Dont increment frame counters if this was an altref buffer update not a real frame
4687 if (cm->show_frame)
4689 cm->current_video_frame++;
4690 cpi->frames_since_key++;
4693 // reset to normal state now that we are done.
4697 #if 0
4699 char filename[512];
4700 FILE *recon_file;
4701 sprintf(filename, "enc%04d.yuv", (int) cm->current_video_frame);
4702 recon_file = fopen(filename, "wb");
4703 fwrite(cm->yv12_fb[cm->lst_fb_idx].buffer_alloc,
4704 cm->yv12_fb[cm->lst_fb_idx].frame_size, 1, recon_file);
4705 fclose(recon_file);
4707 #endif
4709 // DEBUG
4710 //vp8_write_yuv_frame("encoder_recon.yuv", cm->frame_to_show);
4715 int vp8_is_gf_update_needed(VP8_PTR ptr)
4717 VP8_COMP *cpi = (VP8_COMP *) ptr;
4718 int ret_val;
4720 ret_val = cpi->gf_update_recommended;
4721 cpi->gf_update_recommended = 0;
4723 return ret_val;
4726 void vp8_check_gf_quality(VP8_COMP *cpi)
4728 VP8_COMMON *cm = &cpi->common;
4729 int gf_active_pct = (100 * cpi->gf_active_count) / (cm->mb_rows * cm->mb_cols);
4730 int gf_ref_usage_pct = (cpi->count_mb_ref_frame_usage[GOLDEN_FRAME] * 100) / (cm->mb_rows * cm->mb_cols);
4731 int last_ref_zz_useage = (cpi->inter_zz_count * 100) / (cm->mb_rows * cm->mb_cols);
4733 // Gf refresh is not currently being signalled
4734 if (cpi->gf_update_recommended == 0)
4736 if (cpi->common.frames_since_golden > 7)
4738 // Low use of gf
4739 if ((gf_active_pct < 10) || ((gf_active_pct + gf_ref_usage_pct) < 15))
4741 // ...but last frame zero zero usage is reasonbable so a new gf might be appropriate
4742 if (last_ref_zz_useage >= 25)
4744 cpi->gf_bad_count ++;
4746 if (cpi->gf_bad_count >= 8) // Check that the condition is stable
4748 cpi->gf_update_recommended = 1;
4749 cpi->gf_bad_count = 0;
4752 else
4753 cpi->gf_bad_count = 0; // Restart count as the background is not stable enough
4755 else
4756 cpi->gf_bad_count = 0; // Gf useage has picked up so reset count
4759 // If the signal is set but has not been read should we cancel it.
4760 else if (last_ref_zz_useage < 15)
4762 cpi->gf_update_recommended = 0;
4763 cpi->gf_bad_count = 0;
4766 #if 0
4768 FILE *f = fopen("gfneeded.stt", "a");
4769 fprintf(f, "%10d %10d %10d %10d %10ld \n",
4770 cm->current_video_frame,
4771 cpi->common.frames_since_golden,
4772 gf_active_pct, gf_ref_usage_pct,
4773 cpi->gf_update_recommended);
4774 fclose(f);
4777 #endif
4780 #if !(CONFIG_REALTIME_ONLY)
4781 static void Pass2Encode(VP8_COMP *cpi, unsigned long *size, unsigned char *dest, unsigned int *frame_flags)
4784 if (!cpi->common.refresh_alt_ref_frame)
4785 vp8_second_pass(cpi);
4787 encode_frame_to_data_rate(cpi, size, dest, frame_flags);
4788 cpi->bits_left -= 8 * *size;
4790 if (!cpi->common.refresh_alt_ref_frame)
4792 double two_pass_min_rate = (double)(cpi->oxcf.target_bandwidth
4793 *cpi->oxcf.two_pass_vbrmin_section / 100);
4794 cpi->bits_left += (long long)(two_pass_min_rate / cpi->oxcf.frame_rate);
4797 #endif
4799 //For ARM NEON, d8-d15 are callee-saved registers, and need to be saved by us.
4800 #if HAVE_ARMV7
4801 extern void vp8_push_neon(INT64 *store);
4802 extern void vp8_pop_neon(INT64 *store);
4803 #endif
4804 int vp8_receive_raw_frame(VP8_PTR ptr, unsigned int frame_flags, YV12_BUFFER_CONFIG *sd, INT64 time_stamp, INT64 end_time)
4806 INT64 store_reg[8];
4807 VP8_COMP *cpi = (VP8_COMP *) ptr;
4808 VP8_COMMON *cm = &cpi->common;
4809 struct vpx_usec_timer timer;
4811 if (!cpi)
4812 return -1;
4814 #if HAVE_ARMV7
4815 #if CONFIG_RUNTIME_CPU_DETECT
4816 if (cm->rtcd.flags & HAS_NEON)
4817 #endif
4819 vp8_push_neon(store_reg);
4821 #endif
4823 vpx_usec_timer_start(&timer);
4825 // no more room for frames;
4826 if (cpi->source_buffer_count != 0 && cpi->source_buffer_count >= cpi->oxcf.lag_in_frames)
4828 #if HAVE_ARMV7
4829 #if CONFIG_RUNTIME_CPU_DETECT
4830 if (cm->rtcd.flags & HAS_NEON)
4831 #endif
4833 vp8_pop_neon(store_reg);
4835 #endif
4836 return -1;
4839 //printf("in-cpi->source_buffer_count: %d\n", cpi->source_buffer_count);
4841 cm->clr_type = sd->clrtype;
4843 // make a copy of the frame for use later...
4844 #if !(CONFIG_REALTIME_ONLY)
4846 if (cpi->oxcf.allow_lag)
4848 int which_buffer = cpi->source_encode_index - 1;
4849 SOURCE_SAMPLE *s;
4851 if (which_buffer == -1)
4852 which_buffer = cpi->oxcf.lag_in_frames - 1;
4854 if (cpi->source_buffer_count < cpi->oxcf.lag_in_frames - 1)
4855 which_buffer = cpi->source_buffer_count;
4857 s = &cpi->src_buffer[which_buffer];
4859 s->source_time_stamp = time_stamp;
4860 s->source_end_time_stamp = end_time;
4861 s->source_frame_flags = frame_flags;
4862 vp8_yv12_copy_frame_ptr(sd, &s->source_buffer);
4864 cpi->source_buffer_count ++;
4866 else
4867 #endif
4869 SOURCE_SAMPLE *s;
4870 s = &cpi->src_buffer[0];
4871 s->source_end_time_stamp = end_time;
4872 s->source_time_stamp = time_stamp;
4873 s->source_frame_flags = frame_flags;
4874 #if HAVE_ARMV7
4875 #if CONFIG_RUNTIME_CPU_DETECT
4876 if (cm->rtcd.flags & HAS_NEON)
4877 #endif
4879 vp8_yv12_copy_src_frame_func_neon(sd, &s->source_buffer);
4881 #if CONFIG_RUNTIME_CPU_DETECT
4882 else
4883 #endif
4884 #endif
4885 #if !HAVE_ARMV7 || CONFIG_RUNTIME_CPU_DETECT
4887 vp8_yv12_copy_frame_ptr(sd, &s->source_buffer);
4889 #endif
4890 cpi->source_buffer_count = 1;
4893 vpx_usec_timer_mark(&timer);
4894 cpi->time_receive_data += vpx_usec_timer_elapsed(&timer);
4896 #if HAVE_ARMV7
4897 #if CONFIG_RUNTIME_CPU_DETECT
4898 if (cm->rtcd.flags & HAS_NEON)
4899 #endif
4901 vp8_pop_neon(store_reg);
4903 #endif
4905 return 0;
4907 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)
4909 INT64 store_reg[8];
4910 VP8_COMP *cpi = (VP8_COMP *) ptr;
4911 VP8_COMMON *cm = &cpi->common;
4912 struct vpx_usec_timer tsctimer;
4913 struct vpx_usec_timer ticktimer;
4914 struct vpx_usec_timer cmptimer;
4916 if (!cpi)
4917 return -1;
4919 #if HAVE_ARMV7
4920 #if CONFIG_RUNTIME_CPU_DETECT
4921 if (cm->rtcd.flags & HAS_NEON)
4922 #endif
4924 vp8_push_neon(store_reg);
4926 #endif
4928 vpx_usec_timer_start(&cmptimer);
4931 // flush variable tells us that even though we have less than 10 frames
4932 // in our buffer we need to start producing compressed frames.
4933 // Probably because we are at the end of a file....
4934 if ((cpi->source_buffer_count == cpi->oxcf.lag_in_frames && cpi->oxcf.lag_in_frames > 0)
4935 || (!cpi->oxcf.allow_lag && cpi->source_buffer_count > 0)
4936 || (flush && cpi->source_buffer_count > 0))
4939 SOURCE_SAMPLE *s;
4941 s = &cpi->src_buffer[cpi->source_encode_index];
4942 cpi->source_time_stamp = s->source_time_stamp;
4943 cpi->source_end_time_stamp = s->source_end_time_stamp;
4945 #if !(CONFIG_REALTIME_ONLY)
4947 // Should we code an alternate reference frame
4948 if (cpi->oxcf.error_resilient_mode == 0 &&
4949 cpi->oxcf.play_alternate &&
4950 cpi->source_alt_ref_pending &&
4951 (cpi->frames_till_gf_update_due < cpi->source_buffer_count) &&
4952 cpi->oxcf.lag_in_frames != 0)
4954 cpi->last_alt_ref_sei = (cpi->source_encode_index + cpi->frames_till_gf_update_due) % cpi->oxcf.lag_in_frames;
4956 #if VP8_TEMPORAL_ALT_REF
4958 if (cpi->oxcf.arnr_max_frames > 0)
4960 #if 0
4961 // my attempt at a loop that tests the results of strength filter.
4962 int start_frame = cpi->last_alt_ref_sei - 3;
4964 int i, besti = -1, pastin = cpi->oxcf.arnr_strength;
4966 int besterr;
4968 if (start_frame < 0)
4969 start_frame += cpi->oxcf.lag_in_frames;
4971 besterr = vp8_calc_low_ss_err(&cpi->src_buffer[cpi->last_alt_ref_sei].source_buffer,
4972 &cpi->src_buffer[start_frame].source_buffer, IF_RTCD(&cpi->rtcd.variance));
4974 for (i = 0; i < 7; i++)
4976 int thiserr;
4977 cpi->oxcf.arnr_strength = i;
4978 vp8_temporal_filter_prepare_c(cpi);
4980 thiserr = vp8_calc_low_ss_err(&cpi->alt_ref_buffer.source_buffer,
4981 &cpi->src_buffer[start_frame].source_buffer, IF_RTCD(&cpi->rtcd.variance));
4983 if (10 * thiserr < besterr * 8)
4985 besterr = thiserr;
4986 besti = i;
4990 if (besti != -1)
4992 cpi->oxcf.arnr_strength = besti;
4993 vp8_temporal_filter_prepare_c(cpi);
4994 s = &cpi->alt_ref_buffer;
4996 // FWG not sure if I need to copy this data for the Alt Ref frame
4997 s->source_time_stamp = cpi->src_buffer[cpi->last_alt_ref_sei].source_time_stamp;
4998 s->source_end_time_stamp = cpi->src_buffer[cpi->last_alt_ref_sei].source_end_time_stamp;
4999 s->source_frame_flags = cpi->src_buffer[cpi->last_alt_ref_sei].source_frame_flags;
5001 else
5002 s = &cpi->src_buffer[cpi->last_alt_ref_sei];
5004 #else
5005 vp8_temporal_filter_prepare_c(cpi);
5006 s = &cpi->alt_ref_buffer;
5008 // FWG not sure if I need to copy this data for the Alt Ref frame
5009 s->source_time_stamp = cpi->src_buffer[cpi->last_alt_ref_sei].source_time_stamp;
5010 s->source_end_time_stamp = cpi->src_buffer[cpi->last_alt_ref_sei].source_end_time_stamp;
5011 s->source_frame_flags = cpi->src_buffer[cpi->last_alt_ref_sei].source_frame_flags;
5013 #endif
5015 else
5016 #endif
5017 s = &cpi->src_buffer[cpi->last_alt_ref_sei];
5019 cm->frames_till_alt_ref_frame = cpi->frames_till_gf_update_due;
5020 cm->refresh_alt_ref_frame = 1;
5021 cm->refresh_golden_frame = 0;
5022 cm->refresh_last_frame = 0;
5023 cm->show_frame = 0;
5024 cpi->source_alt_ref_pending = FALSE; // Clear Pending altf Ref flag.
5025 cpi->is_src_frame_alt_ref = 0;
5026 cpi->is_next_src_alt_ref = 0;
5028 else
5029 #endif
5031 cm->show_frame = 1;
5032 #if !(CONFIG_REALTIME_ONLY)
5034 if (cpi->oxcf.allow_lag)
5036 if (cpi->source_encode_index == cpi->last_alt_ref_sei)
5038 cpi->is_src_frame_alt_ref = 1;
5039 cpi->last_alt_ref_sei = -1;
5041 else
5042 cpi->is_src_frame_alt_ref = 0;
5044 cpi->source_encode_index = (cpi->source_encode_index + 1) % cpi->oxcf.lag_in_frames;
5046 if(cpi->source_encode_index == cpi->last_alt_ref_sei)
5047 cpi->is_next_src_alt_ref = 1;
5048 else
5049 cpi->is_next_src_alt_ref = 0;
5052 #endif
5053 cpi->source_buffer_count--;
5056 cpi->un_scaled_source = &s->source_buffer;
5057 cpi->Source = &s->source_buffer;
5058 cpi->source_frame_flags = s->source_frame_flags;
5060 *time_stamp = cpi->source_time_stamp;
5061 *time_end = cpi->source_end_time_stamp;
5063 else
5065 *size = 0;
5066 #if !(CONFIG_REALTIME_ONLY)
5068 if (flush && cpi->pass == 1 && !cpi->first_pass_done)
5070 vp8_end_first_pass(cpi); /* get last stats packet */
5071 cpi->first_pass_done = 1;
5074 #endif
5076 #if HAVE_ARMV7
5077 #if CONFIG_RUNTIME_CPU_DETECT
5078 if (cm->rtcd.flags & HAS_NEON)
5079 #endif
5081 vp8_pop_neon(store_reg);
5083 #endif
5084 return -1;
5087 *frame_flags = cpi->source_frame_flags;
5089 if (cpi->source_time_stamp < cpi->first_time_stamp_ever)
5091 cpi->first_time_stamp_ever = cpi->source_time_stamp;
5092 cpi->last_end_time_stamp_seen = cpi->source_time_stamp;
5095 // adjust frame rates based on timestamps given
5096 if (!cm->refresh_alt_ref_frame)
5098 if (cpi->source_time_stamp == cpi->first_time_stamp_ever)
5100 double this_fps = 10000000.000 / (cpi->source_end_time_stamp - cpi->source_time_stamp);
5102 vp8_new_frame_rate(cpi, this_fps);
5104 else
5106 long long nanosecs = cpi->source_end_time_stamp
5107 - cpi->last_end_time_stamp_seen;
5108 double this_fps = 10000000.000 / nanosecs;
5110 vp8_new_frame_rate(cpi, (7 * cpi->oxcf.frame_rate + this_fps) / 8);
5114 cpi->last_time_stamp_seen = cpi->source_time_stamp;
5115 cpi->last_end_time_stamp_seen = cpi->source_end_time_stamp;
5118 if (cpi->compressor_speed == 2)
5120 vp8_check_gf_quality(cpi);
5123 if (!cpi)
5125 #if HAVE_ARMV7
5126 #if CONFIG_RUNTIME_CPU_DETECT
5127 if (cm->rtcd.flags & HAS_NEON)
5128 #endif
5130 vp8_pop_neon(store_reg);
5132 #endif
5133 return 0;
5136 if (cpi->compressor_speed == 2)
5138 vpx_usec_timer_start(&tsctimer);
5139 vpx_usec_timer_start(&ticktimer);
5142 // start with a 0 size frame
5143 *size = 0;
5145 // Clear down mmx registers
5146 vp8_clear_system_state(); //__asm emms;
5148 cm->frame_type = INTER_FRAME;
5149 cm->frame_flags = *frame_flags;
5151 #if 0
5153 if (cm->refresh_alt_ref_frame)
5155 //cm->refresh_golden_frame = 1;
5156 cm->refresh_golden_frame = 0;
5157 cm->refresh_last_frame = 0;
5159 else
5161 cm->refresh_golden_frame = 0;
5162 cm->refresh_last_frame = 1;
5165 #endif
5167 #if !(CONFIG_REALTIME_ONLY)
5169 if (cpi->pass == 1)
5171 Pass1Encode(cpi, size, dest, frame_flags);
5173 else if (cpi->pass == 2)
5175 Pass2Encode(cpi, size, dest, frame_flags);
5177 else
5178 #endif
5179 encode_frame_to_data_rate(cpi, size, dest, frame_flags);
5181 if (cpi->compressor_speed == 2)
5183 unsigned int duration, duration2;
5184 vpx_usec_timer_mark(&tsctimer);
5185 vpx_usec_timer_mark(&ticktimer);
5187 duration = vpx_usec_timer_elapsed(&ticktimer);
5188 duration2 = (unsigned int)((double)duration / 2);
5190 if (cm->frame_type != KEY_FRAME)
5192 if (cpi->avg_encode_time == 0)
5193 cpi->avg_encode_time = duration;
5194 else
5195 cpi->avg_encode_time = (7 * cpi->avg_encode_time + duration) >> 3;
5198 if (duration2)
5200 //if(*frame_flags!=1)
5203 if (cpi->avg_pick_mode_time == 0)
5204 cpi->avg_pick_mode_time = duration2;
5205 else
5206 cpi->avg_pick_mode_time = (7 * cpi->avg_pick_mode_time + duration2) >> 3;
5212 if (cm->refresh_entropy_probs == 0)
5214 vpx_memcpy(&cm->fc, &cm->lfc, sizeof(cm->fc));
5217 // if its a dropped frame honor the requests on subsequent frames
5218 if (*size > 0)
5221 // return to normal state
5222 cm->refresh_entropy_probs = 1;
5223 cm->refresh_alt_ref_frame = 0;
5224 cm->refresh_golden_frame = 0;
5225 cm->refresh_last_frame = 1;
5226 cm->frame_type = INTER_FRAME;
5230 cpi->ready_for_new_frame = 1;
5232 vpx_usec_timer_mark(&cmptimer);
5233 cpi->time_compress_data += vpx_usec_timer_elapsed(&cmptimer);
5235 if (cpi->b_calculate_psnr && cpi->pass != 1 && cm->show_frame)
5236 generate_psnr_packet(cpi);
5238 #if CONFIG_PSNR
5240 if (cpi->pass != 1)
5242 cpi->bytes += *size;
5244 if (cm->show_frame)
5247 cpi->count ++;
5249 if (cpi->b_calculate_psnr)
5251 double y, u, v;
5252 double sq_error;
5253 double frame_psnr = vp8_calc_psnr(cpi->Source, cm->frame_to_show, &y, &u, &v, &sq_error);
5255 cpi->total_y += y;
5256 cpi->total_u += u;
5257 cpi->total_v += v;
5258 cpi->total_sq_error += sq_error;
5259 cpi->total += frame_psnr;
5261 double y2, u2, v2, frame_psnr2, frame_ssim2 = 0;
5262 double weight = 0;
5264 vp8_deblock(cm->frame_to_show, &cm->post_proc_buffer, cm->filter_level * 10 / 6, 1, 0, IF_RTCD(&cm->rtcd.postproc));
5265 vp8_clear_system_state();
5266 frame_psnr2 = vp8_calc_psnr(cpi->Source, &cm->post_proc_buffer, &y2, &u2, &v2, &sq_error);
5267 frame_ssim2 = vp8_calc_ssim(cpi->Source, &cm->post_proc_buffer, 1, &weight);
5269 cpi->summed_quality += frame_ssim2 * weight;
5270 cpi->summed_weights += weight;
5272 cpi->totalp_y += y2;
5273 cpi->totalp_u += u2;
5274 cpi->totalp_v += v2;
5275 cpi->totalp += frame_psnr2;
5276 cpi->total_sq_error2 += sq_error;
5281 if (cpi->b_calculate_ssimg)
5283 double y, u, v, frame_all;
5284 frame_all = vp8_calc_ssimg(cpi->Source, cm->frame_to_show, &y, &u, &v);
5285 cpi->total_ssimg_y += y;
5286 cpi->total_ssimg_u += u;
5287 cpi->total_ssimg_v += v;
5288 cpi->total_ssimg_all += frame_all;
5294 #if 0
5296 if (cpi->common.frame_type != 0 && cpi->common.base_qindex == cpi->oxcf.worst_allowed_q)
5298 skiptruecount += cpi->skip_true_count;
5299 skipfalsecount += cpi->skip_false_count;
5302 #endif
5303 #if 0
5305 if (cpi->pass != 1)
5307 FILE *f = fopen("skip.stt", "a");
5308 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);
5310 if (cpi->is_src_frame_alt_ref == 1)
5311 fprintf(f, "skipcount: %4d framesize: %d\n", cpi->skip_true_count , *size);
5313 fclose(f);
5316 #endif
5317 #endif
5319 #if HAVE_ARMV7
5320 #if CONFIG_RUNTIME_CPU_DETECT
5321 if (cm->rtcd.flags & HAS_NEON)
5322 #endif
5324 vp8_pop_neon(store_reg);
5326 #endif
5328 return 0;
5331 int vp8_get_preview_raw_frame(VP8_PTR comp, YV12_BUFFER_CONFIG *dest, vp8_ppflags_t *flags)
5333 VP8_COMP *cpi = (VP8_COMP *) comp;
5335 if (cpi->common.refresh_alt_ref_frame)
5336 return -1;
5337 else
5339 int ret;
5340 #if CONFIG_POSTPROC
5341 ret = vp8_post_proc_frame(&cpi->common, dest, flags);
5342 #else
5344 if (cpi->common.frame_to_show)
5346 *dest = *cpi->common.frame_to_show;
5347 dest->y_width = cpi->common.Width;
5348 dest->y_height = cpi->common.Height;
5349 dest->uv_height = cpi->common.Height / 2;
5350 ret = 0;
5352 else
5354 ret = -1;
5357 #endif //!CONFIG_POSTPROC
5358 vp8_clear_system_state();
5359 return ret;
5363 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])
5365 VP8_COMP *cpi = (VP8_COMP *) comp;
5366 signed char feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS];
5368 if (cpi->common.mb_rows != rows || cpi->common.mb_cols != cols)
5369 return -1;
5371 if (!map)
5373 disable_segmentation((VP8_PTR)cpi);
5374 return 0;
5377 // Set the segmentation Map
5378 set_segmentation_map((VP8_PTR)cpi, map);
5380 // Activate segmentation.
5381 enable_segmentation((VP8_PTR)cpi);
5383 // Set up the quant segment data
5384 feature_data[MB_LVL_ALT_Q][0] = delta_q[0];
5385 feature_data[MB_LVL_ALT_Q][1] = delta_q[1];
5386 feature_data[MB_LVL_ALT_Q][2] = delta_q[2];
5387 feature_data[MB_LVL_ALT_Q][3] = delta_q[3];
5389 // Set up the loop segment data s
5390 feature_data[MB_LVL_ALT_LF][0] = delta_lf[0];
5391 feature_data[MB_LVL_ALT_LF][1] = delta_lf[1];
5392 feature_data[MB_LVL_ALT_LF][2] = delta_lf[2];
5393 feature_data[MB_LVL_ALT_LF][3] = delta_lf[3];
5395 cpi->segment_encode_breakout[0] = threshold[0];
5396 cpi->segment_encode_breakout[1] = threshold[1];
5397 cpi->segment_encode_breakout[2] = threshold[2];
5398 cpi->segment_encode_breakout[3] = threshold[3];
5400 // Initialise the feature data structure
5401 // SEGMENT_DELTADATA 0, SEGMENT_ABSDATA 1
5402 set_segment_data((VP8_PTR)cpi, &feature_data[0][0], SEGMENT_DELTADATA);
5404 return 0;
5407 int vp8_set_active_map(VP8_PTR comp, unsigned char *map, unsigned int rows, unsigned int cols)
5409 VP8_COMP *cpi = (VP8_COMP *) comp;
5411 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols)
5413 if (map)
5415 vpx_memcpy(cpi->active_map, map, rows * cols);
5416 cpi->active_map_enabled = 1;
5418 else
5419 cpi->active_map_enabled = 0;
5421 return 0;
5423 else
5425 //cpi->active_map_enabled = 0;
5426 return -1 ;
5430 int vp8_set_internal_size(VP8_PTR comp, VPX_SCALING horiz_mode, VPX_SCALING vert_mode)
5432 VP8_COMP *cpi = (VP8_COMP *) comp;
5434 if (horiz_mode <= ONETWO)
5435 cpi->common.horiz_scale = horiz_mode;
5436 else
5437 return -1;
5439 if (vert_mode <= ONETWO)
5440 cpi->common.vert_scale = vert_mode;
5441 else
5442 return -1;
5444 return 0;
5449 int vp8_calc_ss_err(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *dest, const vp8_variance_rtcd_vtable_t *rtcd)
5451 int i, j;
5452 int Total = 0;
5454 unsigned char *src = source->y_buffer;
5455 unsigned char *dst = dest->y_buffer;
5456 (void)rtcd;
5458 // Loop through the Y plane raw and reconstruction data summing (square differences)
5459 for (i = 0; i < source->y_height; i += 16)
5461 for (j = 0; j < source->y_width; j += 16)
5463 unsigned int sse;
5464 Total += VARIANCE_INVOKE(rtcd, mse16x16)(src + j, source->y_stride, dst + j, dest->y_stride, &sse);
5467 src += 16 * source->y_stride;
5468 dst += 16 * dest->y_stride;
5471 return Total;
5473 int vp8_calc_low_ss_err(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *dest, const vp8_variance_rtcd_vtable_t *rtcd)
5475 int i, j;
5476 int Total = 0;
5478 unsigned char *src = source->y_buffer;
5479 unsigned char *dst = dest->y_buffer;
5480 (void)rtcd;
5482 // Loop through the Y plane raw and reconstruction data summing (square differences)
5483 for (i = 0; i < source->y_height; i += 16)
5485 for (j = 0; j < source->y_width; j += 16)
5487 unsigned int sse;
5488 VARIANCE_INVOKE(rtcd, mse16x16)(src + j, source->y_stride, dst + j, dest->y_stride, &sse);
5490 if (sse < 8096)
5491 Total += sse;
5494 src += 16 * source->y_stride;
5495 dst += 16 * dest->y_stride;
5498 return Total;
5501 int vp8_get_speed(VP8_PTR c)
5503 VP8_COMP *cpi = (VP8_COMP *) c;
5504 return cpi->Speed;
5506 int vp8_get_quantizer(VP8_PTR c)
5508 VP8_COMP *cpi = (VP8_COMP *) c;
5509 return cpi->common.base_qindex;