2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
12 #include "vpx_config.h"
13 #include "vp8/common/onyxc_int.h"
14 #include "vp8/common/blockd.h"
16 #include "vp8/common/systemdependent.h"
18 #include "vp8/common/alloccommon.h"
20 #include "firstpass.h"
22 #include "vpx_scale/vpxscale.h"
23 #include "vp8/common/extend.h"
25 #include "vp8/common/quant_common.h"
26 #include "segmentation.h"
28 #include "vp8/common/postproc.h"
30 #include "vpx_mem/vpx_mem.h"
31 #include "vp8/common/swapyv12buffer.h"
32 #include "vp8/common/threading.h"
33 #include "vpx_ports/vpx_timer.h"
35 #include "vpx_ports/arm.h"
37 #if CONFIG_MULTI_RES_ENCODING
38 #include "mr_dissim.h"
40 #include "encodeframe.h"
46 #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
47 extern int vp8_update_coef_context(VP8_COMP *cpi);
48 extern void vp8_update_coef_probs(VP8_COMP *cpi);
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_deblock_frame(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *post, int filt_lvl, int low_var_thresh, int flag);
56 extern void print_parms(VP8_CONFIG *ocf, char *filenam);
57 extern unsigned int vp8_get_processor_freq();
58 extern void print_tree_update_probs();
59 extern int vp8cx_create_encoder_threads(VP8_COMP *cpi);
60 extern void vp8cx_remove_encoder_threads(VP8_COMP *cpi);
62 int vp8_estimate_entropy_savings(VP8_COMP *cpi);
64 int vp8_calc_ss_err(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *dest);
66 extern void vp8_temporal_filter_prepare_c(VP8_COMP *cpi, int distance);
68 static void set_default_lf_deltas(VP8_COMP *cpi);
70 extern const int vp8_gf_interval_table[101];
72 #if CONFIG_INTERNAL_STATS
75 extern double vp8_calc_ssim
77 YV12_BUFFER_CONFIG *source,
78 YV12_BUFFER_CONFIG *dest,
84 extern double vp8_calc_ssimg
86 YV12_BUFFER_CONFIG *source,
87 YV12_BUFFER_CONFIG *dest,
108 extern int skip_true_count;
109 extern int skip_false_count;
114 extern int intra_mode_stats[10][10][10];
118 unsigned int frames_at_speed[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
119 unsigned int tot_pm = 0;
120 unsigned int cnt_pm = 0;
121 unsigned int tot_ef = 0;
122 unsigned int cnt_ef = 0;
126 extern unsigned __int64 Sectionbits[50];
127 extern int y_modes[5] ;
128 extern int uv_modes[4] ;
129 extern int b_modes[10] ;
131 extern int inter_y_modes[10] ;
132 extern int inter_uv_modes[4] ;
133 extern unsigned int inter_b_modes[15];
136 extern const int vp8_bits_per_mb[2][QINDEX_RANGE];
138 extern const int qrounding_factors[129];
139 extern const int qzbin_factors[129];
140 extern void vp8cx_init_quantizer(VP8_COMP *cpi);
141 extern const int vp8cx_base_skip_false_prob[128];
143 /* Tables relating active max Q to active min Q */
144 static const unsigned char kf_low_motion_minq[QINDEX_RANGE] =
146 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
147 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
148 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
149 0,0,0,0,1,1,1,1,1,1,1,1,2,2,2,2,
150 3,3,3,3,3,3,4,4,4,5,5,5,5,5,6,6,
151 6,6,7,7,8,8,8,8,9,9,10,10,10,10,11,11,
152 11,11,12,12,13,13,13,13,14,14,15,15,15,15,16,16,
153 16,16,17,17,18,18,18,18,19,20,20,21,21,22,23,23
155 static const unsigned char kf_high_motion_minq[QINDEX_RANGE] =
157 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
158 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
159 1,1,1,1,1,1,1,1,2,2,2,2,3,3,3,3,
160 3,3,3,3,4,4,4,4,5,5,5,5,5,5,6,6,
161 6,6,7,7,8,8,8,8,9,9,10,10,10,10,11,11,
162 11,11,12,12,13,13,13,13,14,14,15,15,15,15,16,16,
163 16,16,17,17,18,18,18,18,19,19,20,20,20,20,21,21,
164 21,21,22,22,23,23,24,25,25,26,26,27,28,28,29,30
166 static const unsigned char gf_low_motion_minq[QINDEX_RANGE] =
168 0,0,0,0,1,1,1,1,1,1,1,1,2,2,2,2,
169 3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,
170 7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10,
171 11,11,12,12,13,13,14,14,15,15,16,16,17,17,18,18,
172 19,19,20,20,21,21,22,22,23,23,24,24,25,25,26,26,
173 27,27,28,28,29,29,30,30,31,31,32,32,33,33,34,34,
174 35,35,36,36,37,37,38,38,39,39,40,40,41,41,42,42,
175 43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58
177 static const unsigned char gf_mid_motion_minq[QINDEX_RANGE] =
179 0,0,0,0,1,1,1,1,1,1,2,2,3,3,3,4,
180 4,4,5,5,5,6,6,6,7,7,7,8,8,8,9,9,
181 9,10,10,10,10,11,11,11,12,12,12,12,13,13,13,14,
182 14,14,15,15,16,16,17,17,18,18,19,19,20,20,21,21,
183 22,22,23,23,24,24,25,25,26,26,27,27,28,28,29,29,
184 30,30,31,31,32,32,33,33,34,34,35,35,36,36,37,37,
185 38,39,39,40,40,41,41,42,42,43,43,44,45,46,47,48,
186 49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64
188 static const unsigned char gf_high_motion_minq[QINDEX_RANGE] =
190 0,0,0,0,1,1,1,1,1,2,2,2,3,3,3,4,
191 4,4,5,5,5,6,6,6,7,7,7,8,8,8,9,9,
192 9,10,10,10,11,11,12,12,13,13,14,14,15,15,16,16,
193 17,17,18,18,19,19,20,20,21,21,22,22,23,23,24,24,
194 25,25,26,26,27,27,28,28,29,29,30,30,31,31,32,32,
195 33,33,34,34,35,35,36,36,37,37,38,38,39,39,40,40,
196 41,41,42,42,43,44,45,46,47,48,49,50,51,52,53,54,
197 55,56,57,58,59,60,62,64,66,68,70,72,74,76,78,80
199 static const unsigned char inter_minq[QINDEX_RANGE] =
201 0,0,1,1,2,3,3,4,4,5,6,6,7,8,8,9,
202 9,10,11,11,12,13,13,14,15,15,16,17,17,18,19,20,
203 20,21,22,22,23,24,24,25,26,27,27,28,29,30,30,31,
204 32,33,33,34,35,36,36,37,38,39,39,40,41,42,42,43,
205 44,45,46,46,47,48,49,50,50,51,52,53,54,55,55,56,
206 57,58,59,60,60,61,62,63,64,65,66,67,67,68,69,70,
207 71,72,73,74,75,75,76,77,78,79,80,81,82,83,84,85,
208 86,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100
211 #ifdef PACKET_TESTING
212 extern FILE *vpxlogc;
215 static void save_layer_context(VP8_COMP *cpi)
217 LAYER_CONTEXT *lc = &cpi->layer_context[cpi->current_layer];
219 /* Save layer dependent coding state */
220 lc->target_bandwidth = cpi->target_bandwidth;
221 lc->starting_buffer_level = cpi->oxcf.starting_buffer_level;
222 lc->optimal_buffer_level = cpi->oxcf.optimal_buffer_level;
223 lc->maximum_buffer_size = cpi->oxcf.maximum_buffer_size;
224 lc->starting_buffer_level_in_ms = cpi->oxcf.starting_buffer_level_in_ms;
225 lc->optimal_buffer_level_in_ms = cpi->oxcf.optimal_buffer_level_in_ms;
226 lc->maximum_buffer_size_in_ms = cpi->oxcf.maximum_buffer_size_in_ms;
227 lc->buffer_level = cpi->buffer_level;
228 lc->bits_off_target = cpi->bits_off_target;
229 lc->total_actual_bits = cpi->total_actual_bits;
230 lc->worst_quality = cpi->worst_quality;
231 lc->active_worst_quality = cpi->active_worst_quality;
232 lc->best_quality = cpi->best_quality;
233 lc->active_best_quality = cpi->active_best_quality;
234 lc->ni_av_qi = cpi->ni_av_qi;
235 lc->ni_tot_qi = cpi->ni_tot_qi;
236 lc->ni_frames = cpi->ni_frames;
237 lc->avg_frame_qindex = cpi->avg_frame_qindex;
238 lc->rate_correction_factor = cpi->rate_correction_factor;
239 lc->key_frame_rate_correction_factor = cpi->key_frame_rate_correction_factor;
240 lc->gf_rate_correction_factor = cpi->gf_rate_correction_factor;
241 lc->zbin_over_quant = cpi->zbin_over_quant;
242 lc->inter_frame_target = cpi->inter_frame_target;
243 lc->total_byte_count = cpi->total_byte_count;
244 lc->filter_level = cpi->common.filter_level;
246 lc->last_frame_percent_intra = cpi->last_frame_percent_intra;
248 memcpy (lc->count_mb_ref_frame_usage,
249 cpi->count_mb_ref_frame_usage,
250 sizeof(cpi->count_mb_ref_frame_usage));
253 static void restore_layer_context(VP8_COMP *cpi, const int layer)
255 LAYER_CONTEXT *lc = &cpi->layer_context[layer];
257 /* Restore layer dependent coding state */
258 cpi->current_layer = layer;
259 cpi->target_bandwidth = lc->target_bandwidth;
260 cpi->oxcf.target_bandwidth = lc->target_bandwidth;
261 cpi->oxcf.starting_buffer_level = lc->starting_buffer_level;
262 cpi->oxcf.optimal_buffer_level = lc->optimal_buffer_level;
263 cpi->oxcf.maximum_buffer_size = lc->maximum_buffer_size;
264 cpi->oxcf.starting_buffer_level_in_ms = lc->starting_buffer_level_in_ms;
265 cpi->oxcf.optimal_buffer_level_in_ms = lc->optimal_buffer_level_in_ms;
266 cpi->oxcf.maximum_buffer_size_in_ms = lc->maximum_buffer_size_in_ms;
267 cpi->buffer_level = lc->buffer_level;
268 cpi->bits_off_target = lc->bits_off_target;
269 cpi->total_actual_bits = lc->total_actual_bits;
270 cpi->active_worst_quality = lc->active_worst_quality;
271 cpi->active_best_quality = lc->active_best_quality;
272 cpi->ni_av_qi = lc->ni_av_qi;
273 cpi->ni_tot_qi = lc->ni_tot_qi;
274 cpi->ni_frames = lc->ni_frames;
275 cpi->avg_frame_qindex = lc->avg_frame_qindex;
276 cpi->rate_correction_factor = lc->rate_correction_factor;
277 cpi->key_frame_rate_correction_factor = lc->key_frame_rate_correction_factor;
278 cpi->gf_rate_correction_factor = lc->gf_rate_correction_factor;
279 cpi->zbin_over_quant = lc->zbin_over_quant;
280 cpi->inter_frame_target = lc->inter_frame_target;
281 cpi->total_byte_count = lc->total_byte_count;
282 cpi->common.filter_level = lc->filter_level;
284 cpi->last_frame_percent_intra = lc->last_frame_percent_intra;
286 memcpy (cpi->count_mb_ref_frame_usage,
287 lc->count_mb_ref_frame_usage,
288 sizeof(cpi->count_mb_ref_frame_usage));
291 static void setup_features(VP8_COMP *cpi)
293 // If segmentation enabled set the update flags
294 if ( cpi->mb.e_mbd.segmentation_enabled )
296 cpi->mb.e_mbd.update_mb_segmentation_map = 1;
297 cpi->mb.e_mbd.update_mb_segmentation_data = 1;
301 cpi->mb.e_mbd.update_mb_segmentation_map = 0;
302 cpi->mb.e_mbd.update_mb_segmentation_data = 0;
305 cpi->mb.e_mbd.mode_ref_lf_delta_enabled = 0;
306 cpi->mb.e_mbd.mode_ref_lf_delta_update = 0;
307 vpx_memset(cpi->mb.e_mbd.ref_lf_deltas, 0, sizeof(cpi->mb.e_mbd.ref_lf_deltas));
308 vpx_memset(cpi->mb.e_mbd.mode_lf_deltas, 0, sizeof(cpi->mb.e_mbd.mode_lf_deltas));
309 vpx_memset(cpi->mb.e_mbd.last_ref_lf_deltas, 0, sizeof(cpi->mb.e_mbd.ref_lf_deltas));
310 vpx_memset(cpi->mb.e_mbd.last_mode_lf_deltas, 0, sizeof(cpi->mb.e_mbd.mode_lf_deltas));
312 set_default_lf_deltas(cpi);
317 static void dealloc_raw_frame_buffers(VP8_COMP *cpi);
320 static void dealloc_compressor_data(VP8_COMP *cpi)
322 vpx_free(cpi->tplist);
325 /* Delete last frame MV storage buffers */
329 vpx_free(cpi->lf_ref_frame_sign_bias);
330 cpi->lf_ref_frame_sign_bias = 0;
332 vpx_free(cpi->lf_ref_frame);
333 cpi->lf_ref_frame = 0;
335 /* Delete sementation map */
336 vpx_free(cpi->segmentation_map);
337 cpi->segmentation_map = 0;
339 vpx_free(cpi->active_map);
342 vp8_de_alloc_frame_buffers(&cpi->common);
344 vp8_yv12_de_alloc_frame_buffer(&cpi->pick_lf_lvl_frame);
345 vp8_yv12_de_alloc_frame_buffer(&cpi->scaled_source);
346 dealloc_raw_frame_buffers(cpi);
351 /* Structure used to monitor GF usage */
352 vpx_free(cpi->gf_active_flags);
353 cpi->gf_active_flags = 0;
355 /* Activity mask based per mb zbin adjustments */
356 vpx_free(cpi->mb_activity_map);
357 cpi->mb_activity_map = 0;
358 vpx_free(cpi->mb_norm_activity_map);
359 cpi->mb_norm_activity_map = 0;
361 vpx_free(cpi->mb.pip);
364 #if CONFIG_MULTITHREAD
365 vpx_free(cpi->mt_current_mb_col);
366 cpi->mt_current_mb_col = NULL;
370 static void enable_segmentation(VP8_COMP *cpi)
372 /* Set the appropriate feature bit */
373 cpi->mb.e_mbd.segmentation_enabled = 1;
374 cpi->mb.e_mbd.update_mb_segmentation_map = 1;
375 cpi->mb.e_mbd.update_mb_segmentation_data = 1;
377 static void disable_segmentation(VP8_COMP *cpi)
379 /* Clear the appropriate feature bit */
380 cpi->mb.e_mbd.segmentation_enabled = 0;
383 /* Valid values for a segment are 0 to 3
384 * Segmentation map is arrange as [Rows][Columns]
386 static void set_segmentation_map(VP8_COMP *cpi, unsigned char *segmentation_map)
388 /* Copy in the new segmentation map */
389 vpx_memcpy(cpi->segmentation_map, segmentation_map, (cpi->common.mb_rows * cpi->common.mb_cols));
391 /* Signal that the map should be updated. */
392 cpi->mb.e_mbd.update_mb_segmentation_map = 1;
393 cpi->mb.e_mbd.update_mb_segmentation_data = 1;
396 /* The values given for each segment can be either deltas (from the default
397 * value chosen for the frame) or absolute values.
399 * Valid range for abs values is:
400 * (0-127 for MB_LVL_ALT_Q), (0-63 for SEGMENT_ALT_LF)
401 * Valid range for delta values are:
402 * (+/-127 for MB_LVL_ALT_Q), (+/-63 for SEGMENT_ALT_LF)
404 * abs_delta = SEGMENT_DELTADATA (deltas)
405 * abs_delta = SEGMENT_ABSDATA (use the absolute values given).
408 static void set_segment_data(VP8_COMP *cpi, signed char *feature_data, unsigned char abs_delta)
410 cpi->mb.e_mbd.mb_segement_abs_delta = abs_delta;
411 vpx_memcpy(cpi->segment_feature_data, feature_data, sizeof(cpi->segment_feature_data));
415 static void segmentation_test_function(VP8_COMP *cpi)
417 unsigned char *seg_map;
418 signed char feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS];
420 // Create a temporary map for segmentation data.
421 CHECK_MEM_ERROR(seg_map, vpx_calloc(cpi->common.mb_rows * cpi->common.mb_cols, 1));
423 // Set the segmentation Map
424 set_segmentation_map(cpi, seg_map);
426 // Activate segmentation.
427 enable_segmentation(cpi);
429 // Set up the quant segment data
430 feature_data[MB_LVL_ALT_Q][0] = 0;
431 feature_data[MB_LVL_ALT_Q][1] = 4;
432 feature_data[MB_LVL_ALT_Q][2] = 0;
433 feature_data[MB_LVL_ALT_Q][3] = 0;
434 // Set up the loop segment data
435 feature_data[MB_LVL_ALT_LF][0] = 0;
436 feature_data[MB_LVL_ALT_LF][1] = 0;
437 feature_data[MB_LVL_ALT_LF][2] = 0;
438 feature_data[MB_LVL_ALT_LF][3] = 0;
440 // Initialise the feature data structure
441 // SEGMENT_DELTADATA 0, SEGMENT_ABSDATA 1
442 set_segment_data(cpi, &feature_data[0][0], SEGMENT_DELTADATA);
444 // Delete sementation map
450 /* A simple function to cyclically refresh the background at a lower Q */
451 static void cyclic_background_refresh(VP8_COMP *cpi, int Q, int lf_adjustment)
453 unsigned char *seg_map = cpi->segmentation_map;
454 signed char feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS];
456 int block_count = cpi->cyclic_refresh_mode_max_mbs_perframe;
457 int mbs_in_frame = cpi->common.mb_rows * cpi->common.mb_cols;
459 cpi->cyclic_refresh_q = Q / 2;
461 // Set every macroblock to be eligible for update.
462 // For key frame this will reset seg map to 0.
463 vpx_memset(cpi->segmentation_map, 0, mbs_in_frame);
465 if (cpi->common.frame_type != KEY_FRAME)
467 /* Cycle through the macro_block rows */
468 /* MB loop to set local segmentation map */
469 i = cpi->cyclic_refresh_mode_index;
470 assert(i < mbs_in_frame);
473 /* If the MB is as a candidate for clean up then mark it for
474 * possible boost/refresh (segment 1) The segment id may get
475 * reset to 0 later if the MB gets coded anything other than
476 * last frame 0,0 as only (last frame 0,0) MBs are eligable for
477 * refresh : that is to say Mbs likely to be background blocks.
479 if (cpi->cyclic_refresh_map[i] == 0)
484 else if (cpi->cyclic_refresh_map[i] < 0)
485 cpi->cyclic_refresh_map[i]++;
488 if (i == mbs_in_frame)
492 while(block_count && i != cpi->cyclic_refresh_mode_index);
494 cpi->cyclic_refresh_mode_index = i;
497 /* Activate segmentation. */
498 cpi->mb.e_mbd.update_mb_segmentation_map = 1;
499 cpi->mb.e_mbd.update_mb_segmentation_data = 1;
500 enable_segmentation(cpi);
502 /* Set up the quant segment data */
503 feature_data[MB_LVL_ALT_Q][0] = 0;
504 feature_data[MB_LVL_ALT_Q][1] = (cpi->cyclic_refresh_q - Q);
505 feature_data[MB_LVL_ALT_Q][2] = 0;
506 feature_data[MB_LVL_ALT_Q][3] = 0;
508 /* Set up the loop segment data */
509 feature_data[MB_LVL_ALT_LF][0] = 0;
510 feature_data[MB_LVL_ALT_LF][1] = lf_adjustment;
511 feature_data[MB_LVL_ALT_LF][2] = 0;
512 feature_data[MB_LVL_ALT_LF][3] = 0;
514 /* Initialise the feature data structure */
515 set_segment_data(cpi, &feature_data[0][0], SEGMENT_DELTADATA);
519 static void set_default_lf_deltas(VP8_COMP *cpi)
521 cpi->mb.e_mbd.mode_ref_lf_delta_enabled = 1;
522 cpi->mb.e_mbd.mode_ref_lf_delta_update = 1;
524 vpx_memset(cpi->mb.e_mbd.ref_lf_deltas, 0, sizeof(cpi->mb.e_mbd.ref_lf_deltas));
525 vpx_memset(cpi->mb.e_mbd.mode_lf_deltas, 0, sizeof(cpi->mb.e_mbd.mode_lf_deltas));
527 /* Test of ref frame deltas */
528 cpi->mb.e_mbd.ref_lf_deltas[INTRA_FRAME] = 2;
529 cpi->mb.e_mbd.ref_lf_deltas[LAST_FRAME] = 0;
530 cpi->mb.e_mbd.ref_lf_deltas[GOLDEN_FRAME] = -2;
531 cpi->mb.e_mbd.ref_lf_deltas[ALTREF_FRAME] = -2;
533 cpi->mb.e_mbd.mode_lf_deltas[0] = 4; /* BPRED */
535 if(cpi->oxcf.Mode == MODE_REALTIME)
536 cpi->mb.e_mbd.mode_lf_deltas[1] = -12; /* Zero */
538 cpi->mb.e_mbd.mode_lf_deltas[1] = -2; /* Zero */
540 cpi->mb.e_mbd.mode_lf_deltas[2] = 2; /* New mv */
541 cpi->mb.e_mbd.mode_lf_deltas[3] = 4; /* Split mv */
544 /* Convenience macros for mapping speed and mode into a continuous
547 #define GOOD(x) (x+1)
550 static int speed_map(int speed, const int *map)
557 } while(speed >= *map++);
561 static const int thresh_mult_map_znn[] = {
562 /* map common to zero, nearest, and near */
563 0, GOOD(2), 1500, GOOD(3), 2000, RT(0), 1000, RT(2), 2000, INT_MAX
566 static const int thresh_mult_map_vhpred[] = {
567 1000, GOOD(2), 1500, GOOD(3), 2000, RT(0), 1000, RT(1), 2000,
568 RT(7), INT_MAX, INT_MAX
571 static const int thresh_mult_map_bpred[] = {
572 2000, GOOD(0), 2500, GOOD(2), 5000, GOOD(3), 7500, RT(0), 2500, RT(1), 5000,
573 RT(6), INT_MAX, INT_MAX
576 static const int thresh_mult_map_tm[] = {
577 1000, GOOD(2), 1500, GOOD(3), 2000, RT(0), 0, RT(1), 1000, RT(2), 2000,
578 RT(7), INT_MAX, INT_MAX
581 static const int thresh_mult_map_new1[] = {
582 1000, GOOD(2), 2000, RT(0), 2000, INT_MAX
585 static const int thresh_mult_map_new2[] = {
586 1000, GOOD(2), 2000, GOOD(3), 2500, GOOD(5), 4000, RT(0), 2000, RT(2), 2500,
590 static const int thresh_mult_map_split1[] = {
591 2500, GOOD(0), 1700, GOOD(2), 10000, GOOD(3), 25000, GOOD(4), INT_MAX,
592 RT(0), 5000, RT(1), 10000, RT(2), 25000, RT(3), INT_MAX, INT_MAX
595 static const int thresh_mult_map_split2[] = {
596 5000, GOOD(0), 4500, GOOD(2), 20000, GOOD(3), 50000, GOOD(4), INT_MAX,
597 RT(0), 10000, RT(1), 20000, RT(2), 50000, RT(3), INT_MAX, INT_MAX
600 static const int mode_check_freq_map_zn2[] = {
601 /* {zero,nearest}{2,3} */
602 0, RT(10), 1<<1, RT(11), 1<<2, RT(12), 1<<3, INT_MAX
605 static const int mode_check_freq_map_vhbpred[] = {
606 0, GOOD(5), 2, RT(0), 0, RT(3), 2, RT(5), 4, INT_MAX
609 static const int mode_check_freq_map_near2[] = {
610 0, GOOD(5), 2, RT(0), 0, RT(3), 2, RT(10), 1<<2, RT(11), 1<<3, RT(12), 1<<4,
614 static const int mode_check_freq_map_new1[] = {
615 0, RT(10), 1<<1, RT(11), 1<<2, RT(12), 1<<3, INT_MAX
618 static const int mode_check_freq_map_new2[] = {
619 0, GOOD(5), 4, RT(0), 0, RT(3), 4, RT(10), 1<<3, RT(11), 1<<4, RT(12), 1<<5,
623 static const int mode_check_freq_map_split1[] = {
624 0, GOOD(2), 2, GOOD(3), 7, RT(1), 2, RT(2), 7, INT_MAX
627 static const int mode_check_freq_map_split2[] = {
628 0, GOOD(1), 2, GOOD(2), 4, GOOD(3), 15, RT(1), 4, RT(2), 15, INT_MAX
631 void vp8_set_speed_features(VP8_COMP *cpi)
633 SPEED_FEATURES *sf = &cpi->sf;
634 int Mode = cpi->compressor_speed;
635 int Speed = cpi->Speed;
637 VP8_COMMON *cm = &cpi->common;
638 int last_improved_quant = sf->improved_quant;
641 /* Initialise default mode frequency sampling variables */
642 for (i = 0; i < MAX_MODES; i ++)
644 cpi->mode_check_freq[i] = 0;
645 cpi->mode_test_hit_counts[i] = 0;
646 cpi->mode_chosen_counts[i] = 0;
649 cpi->mbs_tested_so_far = 0;
651 /* best quality defaults */
653 sf->search_method = NSTEP;
654 sf->improved_quant = 1;
655 sf->improved_dct = 1;
658 sf->quarter_pixel_search = 1;
659 sf->half_pixel_search = 1;
660 sf->iterative_sub_pixel = 1;
661 sf->optimize_coefficients = 1;
662 sf->use_fastquant_for_pick = 0;
663 sf->no_skip_block4x4_search = 1;
666 sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
667 sf->improved_mv_pred = 1;
669 /* default thresholds to 0 */
670 for (i = 0; i < MAX_MODES; i++)
671 sf->thresh_mult[i] = 0;
673 /* Count enabled references */
675 if (cpi->ref_frame_flags & VP8_LAST_FRAME)
677 if (cpi->ref_frame_flags & VP8_GOLD_FRAME)
679 if (cpi->ref_frame_flags & VP8_ALTR_FRAME)
682 /* Convert speed to continuous range, with clamping */
694 sf->thresh_mult[THR_ZERO1] =
695 sf->thresh_mult[THR_NEAREST1] =
696 sf->thresh_mult[THR_NEAR1] =
697 sf->thresh_mult[THR_DC] = 0; /* always */
699 sf->thresh_mult[THR_ZERO2] =
700 sf->thresh_mult[THR_ZERO3] =
701 sf->thresh_mult[THR_NEAREST2] =
702 sf->thresh_mult[THR_NEAREST3] =
703 sf->thresh_mult[THR_NEAR2] =
704 sf->thresh_mult[THR_NEAR3] = speed_map(Speed, thresh_mult_map_znn);
706 sf->thresh_mult[THR_V_PRED] =
707 sf->thresh_mult[THR_H_PRED] = speed_map(Speed, thresh_mult_map_vhpred);
708 sf->thresh_mult[THR_B_PRED] = speed_map(Speed, thresh_mult_map_bpred);
709 sf->thresh_mult[THR_TM] = speed_map(Speed, thresh_mult_map_tm);
710 sf->thresh_mult[THR_NEW1] = speed_map(Speed, thresh_mult_map_new1);
711 sf->thresh_mult[THR_NEW2] =
712 sf->thresh_mult[THR_NEW3] = speed_map(Speed, thresh_mult_map_new2);
713 sf->thresh_mult[THR_SPLIT1] = speed_map(Speed, thresh_mult_map_split1);
714 sf->thresh_mult[THR_SPLIT2] =
715 sf->thresh_mult[THR_SPLIT3] = speed_map(Speed, thresh_mult_map_split2);
717 cpi->mode_check_freq[THR_ZERO1] =
718 cpi->mode_check_freq[THR_NEAREST1] =
719 cpi->mode_check_freq[THR_NEAR1] =
720 cpi->mode_check_freq[THR_TM] =
721 cpi->mode_check_freq[THR_DC] = 0; /* always */
723 cpi->mode_check_freq[THR_ZERO2] =
724 cpi->mode_check_freq[THR_ZERO3] =
725 cpi->mode_check_freq[THR_NEAREST2] =
726 cpi->mode_check_freq[THR_NEAREST3] = speed_map(Speed,
727 mode_check_freq_map_zn2);
729 cpi->mode_check_freq[THR_NEAR2] =
730 cpi->mode_check_freq[THR_NEAR3] = speed_map(Speed,
731 mode_check_freq_map_near2);
733 cpi->mode_check_freq[THR_V_PRED] =
734 cpi->mode_check_freq[THR_H_PRED] =
735 cpi->mode_check_freq[THR_B_PRED] = speed_map(Speed,
736 mode_check_freq_map_vhbpred);
737 cpi->mode_check_freq[THR_NEW1] = speed_map(Speed,
738 mode_check_freq_map_new1);
739 cpi->mode_check_freq[THR_NEW2] =
740 cpi->mode_check_freq[THR_NEW3] = speed_map(Speed,
741 mode_check_freq_map_new2);
742 cpi->mode_check_freq[THR_SPLIT1] = speed_map(Speed,
743 mode_check_freq_map_split1);
744 cpi->mode_check_freq[THR_SPLIT2] =
745 cpi->mode_check_freq[THR_SPLIT3] = speed_map(Speed,
746 mode_check_freq_map_split2);
750 #if !(CONFIG_REALTIME_ONLY)
751 case 0: /* best quality mode */
753 sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
759 /* Disable coefficient optimization above speed 0 */
760 sf->optimize_coefficients = 0;
761 sf->use_fastquant_for_pick = 1;
762 sf->no_skip_block4x4_search = 0;
769 sf->improved_quant = 0;
770 sf->improved_dct = 0;
772 /* Only do recode loop on key frames, golden frames and
782 sf->recode_loop = 0; /* recode loop off */
783 sf->RD = 0; /* Turn rd off */
789 sf->auto_filter = 0; /* Faster selection of loop filter */
795 sf->optimize_coefficients = 0;
798 sf->iterative_sub_pixel = 1;
799 sf->search_method = NSTEP;
803 sf->improved_quant = 0;
804 sf->improved_dct = 0;
806 sf->use_fastquant_for_pick = 1;
807 sf->no_skip_block4x4_search = 0;
812 sf->auto_filter = 0; /* Faster selection of loop filter */
822 sf->auto_filter = 0; /* Faster selection of loop filter */
823 sf->search_method = HEX;
824 sf->iterative_sub_pixel = 0;
829 unsigned int sum = 0;
830 unsigned int total_mbs = cm->MBs;
832 unsigned int total_skip;
836 if (cpi->oxcf.encode_breakout > 2000)
837 min = cpi->oxcf.encode_breakout;
841 for (i = 0; i < min; i++)
843 sum += cpi->error_bins[i];
849 /* i starts from 2 to make sure thresh started from 2048 */
850 for (; i < 1024; i++)
852 sum += cpi->error_bins[i];
854 if (10 * sum >= (unsigned int)(cpi->Speed - 6)*(total_mbs - total_skip))
866 sf->thresh_mult[THR_NEW1 ] = thresh;
867 sf->thresh_mult[THR_NEAREST1 ] = thresh >> 1;
868 sf->thresh_mult[THR_NEAR1 ] = thresh >> 1;
873 sf->thresh_mult[THR_NEW2] = thresh << 1;
874 sf->thresh_mult[THR_NEAREST2 ] = thresh;
875 sf->thresh_mult[THR_NEAR2 ] = thresh;
880 sf->thresh_mult[THR_NEW3] = thresh << 1;
881 sf->thresh_mult[THR_NEAREST3 ] = thresh;
882 sf->thresh_mult[THR_NEAR3 ] = thresh;
885 sf->improved_mv_pred = 0;
889 sf->quarter_pixel_search = 0;
893 cm->filter_type = NORMAL_LOOPFILTER;
896 cm->filter_type = SIMPLE_LOOPFILTER;
900 cm->filter_type = SIMPLE_LOOPFILTER;
903 /* This has a big hit on quality. Last resort */
905 sf->half_pixel_search = 0;
907 vpx_memset(cpi->error_bins, 0, sizeof(cpi->error_bins));
911 /* Slow quant, dct and trellis not worthwhile for first pass
912 * so make sure they are always turned off.
914 if ( cpi->pass == 1 )
916 sf->improved_quant = 0;
917 sf->optimize_coefficients = 0;
918 sf->improved_dct = 0;
921 if (cpi->sf.search_method == NSTEP)
923 vp8_init3smotion_compensation(&cpi->mb, cm->yv12_fb[cm->lst_fb_idx].y_stride);
925 else if (cpi->sf.search_method == DIAMOND)
927 vp8_init_dsmotion_compensation(&cpi->mb, cm->yv12_fb[cm->lst_fb_idx].y_stride);
930 if (cpi->sf.improved_dct)
932 cpi->mb.short_fdct8x4 = vp8_short_fdct8x4;
933 cpi->mb.short_fdct4x4 = vp8_short_fdct4x4;
937 /* No fast FDCT defined for any platform at this time. */
938 cpi->mb.short_fdct8x4 = vp8_short_fdct8x4;
939 cpi->mb.short_fdct4x4 = vp8_short_fdct4x4;
942 cpi->mb.short_walsh4x4 = vp8_short_walsh4x4;
944 if (cpi->sf.improved_quant)
946 cpi->mb.quantize_b = vp8_regular_quantize_b;
947 cpi->mb.quantize_b_pair = vp8_regular_quantize_b_pair;
951 cpi->mb.quantize_b = vp8_fast_quantize_b;
952 cpi->mb.quantize_b_pair = vp8_fast_quantize_b_pair;
954 if (cpi->sf.improved_quant != last_improved_quant)
955 vp8cx_init_quantizer(cpi);
957 if (cpi->sf.iterative_sub_pixel == 1)
959 cpi->find_fractional_mv_step = vp8_find_best_sub_pixel_step_iteratively;
961 else if (cpi->sf.quarter_pixel_search)
963 cpi->find_fractional_mv_step = vp8_find_best_sub_pixel_step;
965 else if (cpi->sf.half_pixel_search)
967 cpi->find_fractional_mv_step = vp8_find_best_half_pixel_step;
971 cpi->find_fractional_mv_step = vp8_skip_fractional_mv_step;
974 if (cpi->sf.optimize_coefficients == 1 && cpi->pass!=1)
975 cpi->mb.optimize = 1;
977 cpi->mb.optimize = 0;
979 if (cpi->common.full_pixel)
980 cpi->find_fractional_mv_step = vp8_skip_fractional_mv_step;
983 frames_at_speed[cpi->Speed]++;
989 static void alloc_raw_frame_buffers(VP8_COMP *cpi)
991 #if VP8_TEMPORAL_ALT_REF
992 int width = (cpi->oxcf.Width + 15) & ~15;
993 int height = (cpi->oxcf.Height + 15) & ~15;
996 cpi->lookahead = vp8_lookahead_init(cpi->oxcf.Width, cpi->oxcf.Height,
997 cpi->oxcf.lag_in_frames);
999 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1000 "Failed to allocate lag buffers");
1002 #if VP8_TEMPORAL_ALT_REF
1004 if (vp8_yv12_alloc_frame_buffer(&cpi->alt_ref_buffer,
1005 width, height, VP8BORDERINPIXELS))
1006 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1007 "Failed to allocate altref buffer");
1013 static void dealloc_raw_frame_buffers(VP8_COMP *cpi)
1015 #if VP8_TEMPORAL_ALT_REF
1016 vp8_yv12_de_alloc_frame_buffer(&cpi->alt_ref_buffer);
1018 vp8_lookahead_destroy(cpi->lookahead);
1022 static int vp8_alloc_partition_data(VP8_COMP *cpi)
1024 vpx_free(cpi->mb.pip);
1026 cpi->mb.pip = vpx_calloc((cpi->common.mb_cols + 1) *
1027 (cpi->common.mb_rows + 1),
1028 sizeof(PARTITION_INFO));
1032 cpi->mb.pi = cpi->mb.pip + cpi->common.mode_info_stride + 1;
1037 void vp8_alloc_compressor_data(VP8_COMP *cpi)
1039 VP8_COMMON *cm = & cpi->common;
1041 int width = cm->Width;
1042 int height = cm->Height;
1044 if (vp8_alloc_frame_buffers(cm, width, height))
1045 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1046 "Failed to allocate frame buffers");
1048 if (vp8_alloc_partition_data(cpi))
1049 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1050 "Failed to allocate partition data");
1053 if ((width & 0xf) != 0)
1054 width += 16 - (width & 0xf);
1056 if ((height & 0xf) != 0)
1057 height += 16 - (height & 0xf);
1060 if (vp8_yv12_alloc_frame_buffer(&cpi->pick_lf_lvl_frame,
1061 width, height, VP8BORDERINPIXELS))
1062 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1063 "Failed to allocate last frame buffer");
1065 if (vp8_yv12_alloc_frame_buffer(&cpi->scaled_source,
1066 width, height, VP8BORDERINPIXELS))
1067 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1068 "Failed to allocate scaled source buffer");
1073 #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
1074 unsigned int tokens = 8 * 24 * 16; /* one MB for each thread */
1076 unsigned int tokens = cm->mb_rows * cm->mb_cols * 24 * 16;
1078 CHECK_MEM_ERROR(cpi->tok, vpx_calloc(tokens, sizeof(*cpi->tok)));
1081 /* Data used for real time vc mode to see if gf needs refreshing */
1082 cpi->inter_zz_count = 0;
1083 cpi->zeromv_count = 0;
1084 cpi->gf_bad_count = 0;
1085 cpi->gf_update_recommended = 0;
1088 /* Structures used to monitor GF usage */
1089 vpx_free(cpi->gf_active_flags);
1090 CHECK_MEM_ERROR(cpi->gf_active_flags,
1091 vpx_calloc(sizeof(*cpi->gf_active_flags),
1092 cm->mb_rows * cm->mb_cols));
1093 cpi->gf_active_count = cm->mb_rows * cm->mb_cols;
1095 vpx_free(cpi->mb_activity_map);
1096 CHECK_MEM_ERROR(cpi->mb_activity_map,
1097 vpx_calloc(sizeof(*cpi->mb_activity_map),
1098 cm->mb_rows * cm->mb_cols));
1100 vpx_free(cpi->mb_norm_activity_map);
1101 CHECK_MEM_ERROR(cpi->mb_norm_activity_map,
1102 vpx_calloc(sizeof(*cpi->mb_norm_activity_map),
1103 cm->mb_rows * cm->mb_cols));
1105 /* allocate memory for storing last frame's MVs for MV prediction. */
1106 vpx_free(cpi->lfmv);
1107 CHECK_MEM_ERROR(cpi->lfmv, vpx_calloc((cm->mb_rows+2) * (cm->mb_cols+2),
1108 sizeof(*cpi->lfmv)));
1109 vpx_free(cpi->lf_ref_frame_sign_bias);
1110 CHECK_MEM_ERROR(cpi->lf_ref_frame_sign_bias,
1111 vpx_calloc((cm->mb_rows+2) * (cm->mb_cols+2),
1112 sizeof(*cpi->lf_ref_frame_sign_bias)));
1113 vpx_free(cpi->lf_ref_frame);
1114 CHECK_MEM_ERROR(cpi->lf_ref_frame,
1115 vpx_calloc((cm->mb_rows+2) * (cm->mb_cols+2),
1116 sizeof(*cpi->lf_ref_frame)));
1118 /* Create the encoder segmentation map and set all entries to 0 */
1119 vpx_free(cpi->segmentation_map);
1120 CHECK_MEM_ERROR(cpi->segmentation_map,
1121 vpx_calloc(cm->mb_rows * cm->mb_cols,
1122 sizeof(*cpi->segmentation_map)));
1123 cpi->cyclic_refresh_mode_index = 0;
1124 vpx_free(cpi->active_map);
1125 CHECK_MEM_ERROR(cpi->active_map,
1126 vpx_calloc(cm->mb_rows * cm->mb_cols,
1127 sizeof(*cpi->active_map)));
1128 vpx_memset(cpi->active_map , 1, (cm->mb_rows * cm->mb_cols));
1130 #if CONFIG_MULTITHREAD
1132 cpi->mt_sync_range = 1;
1133 else if (width <= 1280)
1134 cpi->mt_sync_range = 4;
1135 else if (width <= 2560)
1136 cpi->mt_sync_range = 8;
1138 cpi->mt_sync_range = 16;
1140 if (cpi->oxcf.multi_threaded > 1)
1142 vpx_free(cpi->mt_current_mb_col);
1143 CHECK_MEM_ERROR(cpi->mt_current_mb_col,
1144 vpx_malloc(sizeof(*cpi->mt_current_mb_col) * cm->mb_rows));
1149 vpx_free(cpi->tplist);
1150 CHECK_MEM_ERROR(cpi->tplist, vpx_malloc(sizeof(TOKENLIST) * cm->mb_rows));
1155 static const int q_trans[] =
1157 0, 1, 2, 3, 4, 5, 7, 8,
1158 9, 10, 12, 13, 15, 17, 18, 19,
1159 20, 21, 23, 24, 25, 26, 27, 28,
1160 29, 30, 31, 33, 35, 37, 39, 41,
1161 43, 45, 47, 49, 51, 53, 55, 57,
1162 59, 61, 64, 67, 70, 73, 76, 79,
1163 82, 85, 88, 91, 94, 97, 100, 103,
1164 106, 109, 112, 115, 118, 121, 124, 127,
1167 int vp8_reverse_trans(int x)
1171 for (i = 0; i < 64; i++)
1172 if (q_trans[i] >= x)
1177 void vp8_new_frame_rate(VP8_COMP *cpi, double framerate)
1182 cpi->frame_rate = framerate;
1183 cpi->output_frame_rate = framerate;
1184 cpi->per_frame_bandwidth = (int)(cpi->oxcf.target_bandwidth /
1185 cpi->output_frame_rate);
1186 cpi->av_per_frame_bandwidth = cpi->per_frame_bandwidth;
1187 cpi->min_frame_bandwidth = (int)(cpi->av_per_frame_bandwidth *
1188 cpi->oxcf.two_pass_vbrmin_section / 100);
1190 /* Set Maximum gf/arf interval */
1191 cpi->max_gf_interval = ((int)(cpi->output_frame_rate / 2.0) + 2);
1193 if(cpi->max_gf_interval < 12)
1194 cpi->max_gf_interval = 12;
1196 /* Extended interval for genuinely static scenes */
1197 cpi->twopass.static_scene_max_gf_interval = cpi->key_frame_frequency >> 1;
1199 /* Special conditions when altr ref frame enabled in lagged compress mode */
1200 if (cpi->oxcf.play_alternate && cpi->oxcf.lag_in_frames)
1202 if (cpi->max_gf_interval > cpi->oxcf.lag_in_frames - 1)
1203 cpi->max_gf_interval = cpi->oxcf.lag_in_frames - 1;
1205 if (cpi->twopass.static_scene_max_gf_interval > cpi->oxcf.lag_in_frames - 1)
1206 cpi->twopass.static_scene_max_gf_interval = cpi->oxcf.lag_in_frames - 1;
1209 if ( cpi->max_gf_interval > cpi->twopass.static_scene_max_gf_interval )
1210 cpi->max_gf_interval = cpi->twopass.static_scene_max_gf_interval;
1215 rescale(int val, int num, int denom)
1217 int64_t llnum = num;
1218 int64_t llden = denom;
1219 int64_t llval = val;
1221 return (int)(llval * llnum / llden);
1225 static void init_config(VP8_COMP *cpi, VP8_CONFIG *oxcf)
1227 VP8_COMMON *cm = &cpi->common;
1232 cpi->auto_adjust_gold_quantizer = 1;
1234 cm->version = oxcf->Version;
1235 vp8_setup_version(cm);
1237 /* frame rate is not available on the first frame, as it's derived from
1238 * the observed timestamps. The actual value used here doesn't matter
1239 * too much, as it will adapt quickly. If the reciprocal of the timebase
1240 * seems like a reasonable framerate, then use that as a guess, otherwise
1243 cpi->frame_rate = (double)(oxcf->timebase.den) /
1244 (double)(oxcf->timebase.num);
1246 if (cpi->frame_rate > 180)
1247 cpi->frame_rate = 30;
1249 cpi->ref_frame_rate = cpi->frame_rate;
1251 /* change includes all joint functionality */
1252 vp8_change_config(cpi, oxcf);
1254 /* Initialize active best and worst q and average q values. */
1255 cpi->active_worst_quality = cpi->oxcf.worst_allowed_q;
1256 cpi->active_best_quality = cpi->oxcf.best_allowed_q;
1257 cpi->avg_frame_qindex = cpi->oxcf.worst_allowed_q;
1259 /* Initialise the starting buffer levels */
1260 cpi->buffer_level = cpi->oxcf.starting_buffer_level;
1261 cpi->bits_off_target = cpi->oxcf.starting_buffer_level;
1263 cpi->rolling_target_bits = cpi->av_per_frame_bandwidth;
1264 cpi->rolling_actual_bits = cpi->av_per_frame_bandwidth;
1265 cpi->long_rolling_target_bits = cpi->av_per_frame_bandwidth;
1266 cpi->long_rolling_actual_bits = cpi->av_per_frame_bandwidth;
1268 cpi->total_actual_bits = 0;
1269 cpi->total_target_vs_actual = 0;
1271 /* Temporal scalabilty */
1272 if (cpi->oxcf.number_of_layers > 1)
1275 double prev_layer_frame_rate=0;
1277 for (i=0; i<cpi->oxcf.number_of_layers; i++)
1279 LAYER_CONTEXT *lc = &cpi->layer_context[i];
1281 /* Layer configuration */
1283 cpi->output_frame_rate / cpi->oxcf.rate_decimator[i];
1284 lc->target_bandwidth = cpi->oxcf.target_bitrate[i] * 1000;
1286 lc->starting_buffer_level_in_ms = oxcf->starting_buffer_level;
1287 lc->optimal_buffer_level_in_ms = oxcf->optimal_buffer_level;
1288 lc->maximum_buffer_size_in_ms = oxcf->maximum_buffer_size;
1290 lc->starting_buffer_level =
1291 rescale((int)(oxcf->starting_buffer_level),
1292 lc->target_bandwidth, 1000);
1294 if (oxcf->optimal_buffer_level == 0)
1295 lc->optimal_buffer_level = lc->target_bandwidth / 8;
1297 lc->optimal_buffer_level =
1298 rescale((int)(oxcf->optimal_buffer_level),
1299 lc->target_bandwidth, 1000);
1301 if (oxcf->maximum_buffer_size == 0)
1302 lc->maximum_buffer_size = lc->target_bandwidth / 8;
1304 lc->maximum_buffer_size =
1305 rescale((int)oxcf->maximum_buffer_size,
1306 lc->target_bandwidth, 1000);
1308 /* Work out the average size of a frame within this layer */
1310 lc->avg_frame_size_for_layer =
1311 (int)((cpi->oxcf.target_bitrate[i] -
1312 cpi->oxcf.target_bitrate[i-1]) * 1000 /
1313 (lc->frame_rate - prev_layer_frame_rate));
1315 lc->active_worst_quality = cpi->oxcf.worst_allowed_q;
1316 lc->active_best_quality = cpi->oxcf.best_allowed_q;
1317 lc->avg_frame_qindex = cpi->oxcf.worst_allowed_q;
1319 lc->buffer_level = lc->starting_buffer_level;
1320 lc->bits_off_target = lc->starting_buffer_level;
1322 lc->total_actual_bits = 0;
1326 lc->rate_correction_factor = 1.0;
1327 lc->key_frame_rate_correction_factor = 1.0;
1328 lc->gf_rate_correction_factor = 1.0;
1329 lc->inter_frame_target = 0;
1331 prev_layer_frame_rate = lc->frame_rate;
1335 #if VP8_TEMPORAL_ALT_REF
1339 cpi->fixed_divide[0] = 0;
1341 for (i = 1; i < 512; i++)
1342 cpi->fixed_divide[i] = 0x80000 / i;
1347 static void update_layer_contexts (VP8_COMP *cpi)
1349 VP8_CONFIG *oxcf = &cpi->oxcf;
1351 /* Update snapshots of the layer contexts to reflect new parameters */
1352 if (oxcf->number_of_layers > 1)
1355 double prev_layer_frame_rate=0;
1357 for (i=0; i<oxcf->number_of_layers; i++)
1359 LAYER_CONTEXT *lc = &cpi->layer_context[i];
1362 cpi->ref_frame_rate / oxcf->rate_decimator[i];
1363 lc->target_bandwidth = oxcf->target_bitrate[i] * 1000;
1365 lc->starting_buffer_level = rescale(
1366 (int)oxcf->starting_buffer_level_in_ms,
1367 lc->target_bandwidth, 1000);
1369 if (oxcf->optimal_buffer_level == 0)
1370 lc->optimal_buffer_level = lc->target_bandwidth / 8;
1372 lc->optimal_buffer_level = rescale(
1373 (int)oxcf->optimal_buffer_level_in_ms,
1374 lc->target_bandwidth, 1000);
1376 if (oxcf->maximum_buffer_size == 0)
1377 lc->maximum_buffer_size = lc->target_bandwidth / 8;
1379 lc->maximum_buffer_size = rescale(
1380 (int)oxcf->maximum_buffer_size_in_ms,
1381 lc->target_bandwidth, 1000);
1383 /* Work out the average size of a frame within this layer */
1385 lc->avg_frame_size_for_layer =
1386 (int)((oxcf->target_bitrate[i] -
1387 oxcf->target_bitrate[i-1]) * 1000 /
1388 (lc->frame_rate - prev_layer_frame_rate));
1390 prev_layer_frame_rate = lc->frame_rate;
1395 void vp8_change_config(VP8_COMP *cpi, VP8_CONFIG *oxcf)
1397 VP8_COMMON *cm = &cpi->common;
1406 #if CONFIG_MULTITHREAD
1407 /* wait for the last picture loopfilter thread done */
1408 if (cpi->b_lpf_running)
1410 sem_wait(&cpi->h_event_end_lpf);
1411 cpi->b_lpf_running = 0;
1415 if (cm->version != oxcf->Version)
1417 cm->version = oxcf->Version;
1418 vp8_setup_version(cm);
1421 last_w = cpi->oxcf.Width;
1422 last_h = cpi->oxcf.Height;
1426 switch (cpi->oxcf.Mode)
1431 cpi->compressor_speed = 2;
1433 if (cpi->oxcf.cpu_used < -16)
1435 cpi->oxcf.cpu_used = -16;
1438 if (cpi->oxcf.cpu_used > 16)
1439 cpi->oxcf.cpu_used = 16;
1443 case MODE_GOODQUALITY:
1445 cpi->compressor_speed = 1;
1447 if (cpi->oxcf.cpu_used < -5)
1449 cpi->oxcf.cpu_used = -5;
1452 if (cpi->oxcf.cpu_used > 5)
1453 cpi->oxcf.cpu_used = 5;
1457 case MODE_BESTQUALITY:
1459 cpi->compressor_speed = 0;
1462 case MODE_FIRSTPASS:
1464 cpi->compressor_speed = 1;
1466 case MODE_SECONDPASS:
1468 cpi->compressor_speed = 1;
1470 if (cpi->oxcf.cpu_used < -5)
1472 cpi->oxcf.cpu_used = -5;
1475 if (cpi->oxcf.cpu_used > 5)
1476 cpi->oxcf.cpu_used = 5;
1479 case MODE_SECONDPASS_BEST:
1481 cpi->compressor_speed = 0;
1486 cpi->auto_worst_q = 1;
1488 cpi->oxcf.worst_allowed_q = q_trans[oxcf->worst_allowed_q];
1489 cpi->oxcf.best_allowed_q = q_trans[oxcf->best_allowed_q];
1490 cpi->oxcf.cq_level = q_trans[cpi->oxcf.cq_level];
1492 if (oxcf->fixed_q >= 0)
1494 if (oxcf->worst_allowed_q < 0)
1495 cpi->oxcf.fixed_q = q_trans[0];
1497 cpi->oxcf.fixed_q = q_trans[oxcf->worst_allowed_q];
1499 if (oxcf->alt_q < 0)
1500 cpi->oxcf.alt_q = q_trans[0];
1502 cpi->oxcf.alt_q = q_trans[oxcf->alt_q];
1504 if (oxcf->key_q < 0)
1505 cpi->oxcf.key_q = q_trans[0];
1507 cpi->oxcf.key_q = q_trans[oxcf->key_q];
1509 if (oxcf->gold_q < 0)
1510 cpi->oxcf.gold_q = q_trans[0];
1512 cpi->oxcf.gold_q = q_trans[oxcf->gold_q];
1516 cpi->baseline_gf_interval =
1517 cpi->oxcf.alt_freq ? cpi->oxcf.alt_freq : DEFAULT_GF_INTERVAL;
1519 cpi->ref_frame_flags = VP8_ALTR_FRAME | VP8_GOLD_FRAME | VP8_LAST_FRAME;
1521 cm->refresh_golden_frame = 0;
1522 cm->refresh_last_frame = 1;
1523 cm->refresh_entropy_probs = 1;
1525 #if (CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
1526 cpi->oxcf.token_partitions = 3;
1529 if (cpi->oxcf.token_partitions >= 0 && cpi->oxcf.token_partitions <= 3)
1530 cm->multi_token_partition =
1531 (TOKEN_PARTITION) cpi->oxcf.token_partitions;
1533 setup_features(cpi);
1538 for (i = 0; i < MAX_MB_SEGMENTS; i++)
1539 cpi->segment_encode_breakout[i] = cpi->oxcf.encode_breakout;
1542 /* At the moment the first order values may not be > MAXQ */
1543 if (cpi->oxcf.fixed_q > MAXQ)
1544 cpi->oxcf.fixed_q = MAXQ;
1546 /* local file playback mode == really big buffer */
1547 if (cpi->oxcf.end_usage == USAGE_LOCAL_FILE_PLAYBACK)
1549 cpi->oxcf.starting_buffer_level = 60000;
1550 cpi->oxcf.optimal_buffer_level = 60000;
1551 cpi->oxcf.maximum_buffer_size = 240000;
1552 cpi->oxcf.starting_buffer_level_in_ms = 60000;
1553 cpi->oxcf.optimal_buffer_level_in_ms = 60000;
1554 cpi->oxcf.maximum_buffer_size_in_ms = 240000;
1557 /* Convert target bandwidth from Kbit/s to Bit/s */
1558 cpi->oxcf.target_bandwidth *= 1000;
1560 cpi->oxcf.starting_buffer_level =
1561 rescale((int)cpi->oxcf.starting_buffer_level,
1562 cpi->oxcf.target_bandwidth, 1000);
1564 /* Set or reset optimal and maximum buffer levels. */
1565 if (cpi->oxcf.optimal_buffer_level == 0)
1566 cpi->oxcf.optimal_buffer_level = cpi->oxcf.target_bandwidth / 8;
1568 cpi->oxcf.optimal_buffer_level =
1569 rescale((int)cpi->oxcf.optimal_buffer_level,
1570 cpi->oxcf.target_bandwidth, 1000);
1572 if (cpi->oxcf.maximum_buffer_size == 0)
1573 cpi->oxcf.maximum_buffer_size = cpi->oxcf.target_bandwidth / 8;
1575 cpi->oxcf.maximum_buffer_size =
1576 rescale((int)cpi->oxcf.maximum_buffer_size,
1577 cpi->oxcf.target_bandwidth, 1000);
1579 /* Set up frame rate and related parameters rate control values. */
1580 vp8_new_frame_rate(cpi, cpi->frame_rate);
1582 /* Set absolute upper and lower quality limits */
1583 cpi->worst_quality = cpi->oxcf.worst_allowed_q;
1584 cpi->best_quality = cpi->oxcf.best_allowed_q;
1586 /* active values should only be modified if out of new range */
1587 if (cpi->active_worst_quality > cpi->oxcf.worst_allowed_q)
1589 cpi->active_worst_quality = cpi->oxcf.worst_allowed_q;
1592 else if (cpi->active_worst_quality < cpi->oxcf.best_allowed_q)
1594 cpi->active_worst_quality = cpi->oxcf.best_allowed_q;
1596 if (cpi->active_best_quality < cpi->oxcf.best_allowed_q)
1598 cpi->active_best_quality = cpi->oxcf.best_allowed_q;
1601 else if (cpi->active_best_quality > cpi->oxcf.worst_allowed_q)
1603 cpi->active_best_quality = cpi->oxcf.worst_allowed_q;
1606 cpi->buffered_mode = cpi->oxcf.optimal_buffer_level > 0;
1608 cpi->cq_target_quality = cpi->oxcf.cq_level;
1610 /* Only allow dropped frames in buffered mode */
1611 cpi->drop_frames_allowed = cpi->oxcf.allow_df && cpi->buffered_mode;
1613 cpi->target_bandwidth = cpi->oxcf.target_bandwidth;
1616 cm->Width = cpi->oxcf.Width;
1617 cm->Height = cpi->oxcf.Height;
1619 /* TODO(jkoleszar): if an internal spatial resampling is active,
1620 * and we downsize the input image, maybe we should clear the
1621 * internal scale immediately rather than waiting for it to
1625 /* VP8 sharpness level mapping 0-7 (vs 0-10 in general VPx dialogs) */
1626 if (cpi->oxcf.Sharpness > 7)
1627 cpi->oxcf.Sharpness = 7;
1629 cm->sharpness_level = cpi->oxcf.Sharpness;
1631 if (cm->horiz_scale != NORMAL || cm->vert_scale != NORMAL)
1633 int UNINITIALIZED_IS_SAFE(hr), UNINITIALIZED_IS_SAFE(hs);
1634 int UNINITIALIZED_IS_SAFE(vr), UNINITIALIZED_IS_SAFE(vs);
1636 Scale2Ratio(cm->horiz_scale, &hr, &hs);
1637 Scale2Ratio(cm->vert_scale, &vr, &vs);
1639 /* always go to the next whole number */
1640 cm->Width = (hs - 1 + cpi->oxcf.Width * hr) / hs;
1641 cm->Height = (vs - 1 + cpi->oxcf.Height * vr) / vs;
1644 if (last_w != cpi->oxcf.Width || last_h != cpi->oxcf.Height)
1645 cpi->force_next_frame_intra = 1;
1647 if (((cm->Width + 15) & 0xfffffff0) !=
1648 cm->yv12_fb[cm->lst_fb_idx].y_width ||
1649 ((cm->Height + 15) & 0xfffffff0) !=
1650 cm->yv12_fb[cm->lst_fb_idx].y_height ||
1651 cm->yv12_fb[cm->lst_fb_idx].y_width == 0)
1653 dealloc_raw_frame_buffers(cpi);
1654 alloc_raw_frame_buffers(cpi);
1655 vp8_alloc_compressor_data(cpi);
1658 if (cpi->oxcf.fixed_q >= 0)
1660 cpi->last_q[0] = cpi->oxcf.fixed_q;
1661 cpi->last_q[1] = cpi->oxcf.fixed_q;
1664 cpi->Speed = cpi->oxcf.cpu_used;
1666 /* force to allowlag to 0 if lag_in_frames is 0; */
1667 if (cpi->oxcf.lag_in_frames == 0)
1669 cpi->oxcf.allow_lag = 0;
1671 /* Limit on lag buffers as these are not currently dynamically allocated */
1672 else if (cpi->oxcf.lag_in_frames > MAX_LAG_BUFFERS)
1673 cpi->oxcf.lag_in_frames = MAX_LAG_BUFFERS;
1676 cpi->alt_ref_source = NULL;
1677 cpi->is_src_frame_alt_ref = 0;
1679 #if CONFIG_TEMPORAL_DENOISING
1680 if (cpi->oxcf.noise_sensitivity)
1682 if (!cpi->denoiser.yv12_mc_running_avg.buffer_alloc)
1684 int width = (cpi->oxcf.Width + 15) & ~15;
1685 int height = (cpi->oxcf.Height + 15) & ~15;
1686 vp8_denoiser_allocate(&cpi->denoiser, width, height);
1692 /* Experimental RD Code */
1693 cpi->frame_distortion = 0;
1694 cpi->last_frame_distortion = 0;
1699 #define M_LOG2_E 0.693147180559945309417
1700 #define log2f(x) (log (x) / (float) M_LOG2_E)
1701 static void cal_mvsadcosts(int *mvsadcost[2])
1705 mvsadcost [0] [0] = 300;
1706 mvsadcost [1] [0] = 300;
1710 double z = 256 * (2 * (log2f(8 * i) + .6));
1711 mvsadcost [0][i] = (int) z;
1712 mvsadcost [1][i] = (int) z;
1713 mvsadcost [0][-i] = (int) z;
1714 mvsadcost [1][-i] = (int) z;
1716 while (++i <= mvfp_max);
1719 struct VP8_COMP* vp8_create_compressor(VP8_CONFIG *oxcf)
1726 cpi = vpx_memalign(32, sizeof(VP8_COMP));
1727 /* Check that the CPI instance is valid */
1733 vpx_memset(cpi, 0, sizeof(VP8_COMP));
1735 if (setjmp(cm->error.jmp))
1737 cpi->common.error.setjmp = 0;
1738 vp8_remove_compressor(&cpi);
1742 cpi->common.error.setjmp = 1;
1744 CHECK_MEM_ERROR(cpi->mb.ss, vpx_calloc(sizeof(search_site), (MAX_MVSEARCH_STEPS * 8) + 1));
1746 vp8_create_common(&cpi->common);
1748 init_config(cpi, oxcf);
1750 memcpy(cpi->base_skip_false_prob, vp8cx_base_skip_false_prob, sizeof(vp8cx_base_skip_false_prob));
1751 cpi->common.current_video_frame = 0;
1752 cpi->kf_overspend_bits = 0;
1753 cpi->kf_bitrate_adjustment = 0;
1754 cpi->frames_till_gf_update_due = 0;
1755 cpi->gf_overspend_bits = 0;
1756 cpi->non_gf_bitrate_adjustment = 0;
1757 cpi->prob_last_coded = 128;
1758 cpi->prob_gf_coded = 128;
1759 cpi->prob_intra_coded = 63;
1761 /* Prime the recent reference frame usage counters.
1762 * Hereafter they will be maintained as a sort of moving average
1764 cpi->recent_ref_frame_usage[INTRA_FRAME] = 1;
1765 cpi->recent_ref_frame_usage[LAST_FRAME] = 1;
1766 cpi->recent_ref_frame_usage[GOLDEN_FRAME] = 1;
1767 cpi->recent_ref_frame_usage[ALTREF_FRAME] = 1;
1769 /* Set reference frame sign bias for ALTREF frame to 1 (for now) */
1770 cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = 1;
1772 cpi->twopass.gf_decay_rate = 0;
1773 cpi->baseline_gf_interval = DEFAULT_GF_INTERVAL;
1775 cpi->gold_is_last = 0 ;
1776 cpi->alt_is_last = 0 ;
1777 cpi->gold_is_alt = 0 ;
1779 cpi->active_map_enabled = 0;
1782 /* Experimental code for lagged and one pass */
1783 /* Initialise one_pass GF frames stats */
1784 /* Update stats used for GF selection */
1787 cpi->one_pass_frame_index = 0;
1789 for (i = 0; i < MAX_LAG_BUFFERS; i++)
1791 cpi->one_pass_frame_stats[i].frames_so_far = 0;
1792 cpi->one_pass_frame_stats[i].frame_intra_error = 0.0;
1793 cpi->one_pass_frame_stats[i].frame_coded_error = 0.0;
1794 cpi->one_pass_frame_stats[i].frame_pcnt_inter = 0.0;
1795 cpi->one_pass_frame_stats[i].frame_pcnt_motion = 0.0;
1796 cpi->one_pass_frame_stats[i].frame_mvr = 0.0;
1797 cpi->one_pass_frame_stats[i].frame_mvr_abs = 0.0;
1798 cpi->one_pass_frame_stats[i].frame_mvc = 0.0;
1799 cpi->one_pass_frame_stats[i].frame_mvc_abs = 0.0;
1804 /* Should we use the cyclic refresh method.
1805 * Currently this is tied to error resilliant mode
1807 cpi->cyclic_refresh_mode_enabled = cpi->oxcf.error_resilient_mode;
1808 cpi->cyclic_refresh_mode_max_mbs_perframe = (cpi->common.mb_rows * cpi->common.mb_cols) / 5;
1809 cpi->cyclic_refresh_mode_index = 0;
1810 cpi->cyclic_refresh_q = 32;
1812 if (cpi->cyclic_refresh_mode_enabled)
1814 CHECK_MEM_ERROR(cpi->cyclic_refresh_map, vpx_calloc((cpi->common.mb_rows * cpi->common.mb_cols), 1));
1817 cpi->cyclic_refresh_map = (signed char *) NULL;
1819 #ifdef ENTROPY_STATS
1820 init_context_counters();
1823 /*Initialize the feed-forward activity masking.*/
1824 cpi->activity_avg = 90<<12;
1826 /* Give a sensible default for the first frame. */
1827 cpi->frames_since_key = 8;
1828 cpi->key_frame_frequency = cpi->oxcf.key_freq;
1829 cpi->this_key_frame_forced = 0;
1830 cpi->next_key_frame_forced = 0;
1832 cpi->source_alt_ref_pending = 0;
1833 cpi->source_alt_ref_active = 0;
1834 cpi->common.refresh_alt_ref_frame = 0;
1836 cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
1837 #if CONFIG_INTERNAL_STATS
1838 cpi->b_calculate_ssimg = 0;
1843 if (cpi->b_calculate_psnr)
1845 cpi->total_sq_error = 0.0;
1846 cpi->total_sq_error2 = 0.0;
1851 cpi->totalp_y = 0.0;
1852 cpi->totalp_u = 0.0;
1853 cpi->totalp_v = 0.0;
1855 cpi->tot_recode_hits = 0;
1856 cpi->summed_quality = 0;
1857 cpi->summed_weights = 0;
1860 if (cpi->b_calculate_ssimg)
1862 cpi->total_ssimg_y = 0;
1863 cpi->total_ssimg_u = 0;
1864 cpi->total_ssimg_v = 0;
1865 cpi->total_ssimg_all = 0;
1870 cpi->first_time_stamp_ever = 0x7FFFFFFF;
1872 cpi->frames_till_gf_update_due = 0;
1873 cpi->key_frame_count = 1;
1875 cpi->ni_av_qi = cpi->oxcf.worst_allowed_q;
1878 cpi->total_byte_count = 0;
1880 cpi->drop_frame = 0;
1882 cpi->rate_correction_factor = 1.0;
1883 cpi->key_frame_rate_correction_factor = 1.0;
1884 cpi->gf_rate_correction_factor = 1.0;
1885 cpi->twopass.est_max_qcorrection_factor = 1.0;
1887 for (i = 0; i < KEY_FRAME_CONTEXT; i++)
1889 cpi->prior_key_frame_distance[i] = (int)cpi->output_frame_rate;
1892 #ifdef OUTPUT_YUV_SRC
1893 yuv_file = fopen("bd.yuv", "ab");
1897 framepsnr = fopen("framepsnr.stt", "a");
1898 kf_list = fopen("kf_list.stt", "w");
1901 cpi->output_pkt_list = oxcf->output_pkt_list;
1903 #if !(CONFIG_REALTIME_ONLY)
1907 vp8_init_first_pass(cpi);
1909 else if (cpi->pass == 2)
1911 size_t packet_sz = sizeof(FIRSTPASS_STATS);
1912 int packets = (int)(oxcf->two_pass_stats_in.sz / packet_sz);
1914 cpi->twopass.stats_in_start = oxcf->two_pass_stats_in.buf;
1915 cpi->twopass.stats_in = cpi->twopass.stats_in_start;
1916 cpi->twopass.stats_in_end = (void*)((char *)cpi->twopass.stats_in
1917 + (packets - 1) * packet_sz);
1918 vp8_init_second_pass(cpi);
1923 if (cpi->compressor_speed == 2)
1925 cpi->avg_encode_time = 0;
1926 cpi->avg_pick_mode_time = 0;
1929 vp8_set_speed_features(cpi);
1931 /* Set starting values of RD threshold multipliers (128 = *1) */
1932 for (i = 0; i < MAX_MODES; i++)
1934 cpi->rd_thresh_mult[i] = 128;
1937 #ifdef ENTROPY_STATS
1938 init_mv_ref_counts();
1941 #if CONFIG_MULTITHREAD
1942 if(vp8cx_create_encoder_threads(cpi))
1944 vp8_remove_compressor(&cpi);
1949 cpi->fn_ptr[BLOCK_16X16].sdf = vp8_sad16x16;
1950 cpi->fn_ptr[BLOCK_16X16].vf = vp8_variance16x16;
1951 cpi->fn_ptr[BLOCK_16X16].svf = vp8_sub_pixel_variance16x16;
1952 cpi->fn_ptr[BLOCK_16X16].svf_halfpix_h = vp8_variance_halfpixvar16x16_h;
1953 cpi->fn_ptr[BLOCK_16X16].svf_halfpix_v = vp8_variance_halfpixvar16x16_v;
1954 cpi->fn_ptr[BLOCK_16X16].svf_halfpix_hv = vp8_variance_halfpixvar16x16_hv;
1955 cpi->fn_ptr[BLOCK_16X16].sdx3f = vp8_sad16x16x3;
1956 cpi->fn_ptr[BLOCK_16X16].sdx8f = vp8_sad16x16x8;
1957 cpi->fn_ptr[BLOCK_16X16].sdx4df = vp8_sad16x16x4d;
1959 cpi->fn_ptr[BLOCK_16X8].sdf = vp8_sad16x8;
1960 cpi->fn_ptr[BLOCK_16X8].vf = vp8_variance16x8;
1961 cpi->fn_ptr[BLOCK_16X8].svf = vp8_sub_pixel_variance16x8;
1962 cpi->fn_ptr[BLOCK_16X8].svf_halfpix_h = NULL;
1963 cpi->fn_ptr[BLOCK_16X8].svf_halfpix_v = NULL;
1964 cpi->fn_ptr[BLOCK_16X8].svf_halfpix_hv = NULL;
1965 cpi->fn_ptr[BLOCK_16X8].sdx3f = vp8_sad16x8x3;
1966 cpi->fn_ptr[BLOCK_16X8].sdx8f = vp8_sad16x8x8;
1967 cpi->fn_ptr[BLOCK_16X8].sdx4df = vp8_sad16x8x4d;
1969 cpi->fn_ptr[BLOCK_8X16].sdf = vp8_sad8x16;
1970 cpi->fn_ptr[BLOCK_8X16].vf = vp8_variance8x16;
1971 cpi->fn_ptr[BLOCK_8X16].svf = vp8_sub_pixel_variance8x16;
1972 cpi->fn_ptr[BLOCK_8X16].svf_halfpix_h = NULL;
1973 cpi->fn_ptr[BLOCK_8X16].svf_halfpix_v = NULL;
1974 cpi->fn_ptr[BLOCK_8X16].svf_halfpix_hv = NULL;
1975 cpi->fn_ptr[BLOCK_8X16].sdx3f = vp8_sad8x16x3;
1976 cpi->fn_ptr[BLOCK_8X16].sdx8f = vp8_sad8x16x8;
1977 cpi->fn_ptr[BLOCK_8X16].sdx4df = vp8_sad8x16x4d;
1979 cpi->fn_ptr[BLOCK_8X8].sdf = vp8_sad8x8;
1980 cpi->fn_ptr[BLOCK_8X8].vf = vp8_variance8x8;
1981 cpi->fn_ptr[BLOCK_8X8].svf = vp8_sub_pixel_variance8x8;
1982 cpi->fn_ptr[BLOCK_8X8].svf_halfpix_h = NULL;
1983 cpi->fn_ptr[BLOCK_8X8].svf_halfpix_v = NULL;
1984 cpi->fn_ptr[BLOCK_8X8].svf_halfpix_hv = NULL;
1985 cpi->fn_ptr[BLOCK_8X8].sdx3f = vp8_sad8x8x3;
1986 cpi->fn_ptr[BLOCK_8X8].sdx8f = vp8_sad8x8x8;
1987 cpi->fn_ptr[BLOCK_8X8].sdx4df = vp8_sad8x8x4d;
1989 cpi->fn_ptr[BLOCK_4X4].sdf = vp8_sad4x4;
1990 cpi->fn_ptr[BLOCK_4X4].vf = vp8_variance4x4;
1991 cpi->fn_ptr[BLOCK_4X4].svf = vp8_sub_pixel_variance4x4;
1992 cpi->fn_ptr[BLOCK_4X4].svf_halfpix_h = NULL;
1993 cpi->fn_ptr[BLOCK_4X4].svf_halfpix_v = NULL;
1994 cpi->fn_ptr[BLOCK_4X4].svf_halfpix_hv = NULL;
1995 cpi->fn_ptr[BLOCK_4X4].sdx3f = vp8_sad4x4x3;
1996 cpi->fn_ptr[BLOCK_4X4].sdx8f = vp8_sad4x4x8;
1997 cpi->fn_ptr[BLOCK_4X4].sdx4df = vp8_sad4x4x4d;
1999 #if ARCH_X86 || ARCH_X86_64
2000 cpi->fn_ptr[BLOCK_16X16].copymem = vp8_copy32xn;
2001 cpi->fn_ptr[BLOCK_16X8].copymem = vp8_copy32xn;
2002 cpi->fn_ptr[BLOCK_8X16].copymem = vp8_copy32xn;
2003 cpi->fn_ptr[BLOCK_8X8].copymem = vp8_copy32xn;
2004 cpi->fn_ptr[BLOCK_4X4].copymem = vp8_copy32xn;
2007 cpi->full_search_sad = vp8_full_search_sad;
2008 cpi->diamond_search_sad = vp8_diamond_search_sad;
2009 cpi->refining_search_sad = vp8_refining_search_sad;
2011 /* make sure frame 1 is okay */
2012 cpi->error_bins[0] = cpi->common.MBs;
2014 /* vp8cx_init_quantizer() is first called here. Add check in
2015 * vp8cx_frame_init_quantizer() so that vp8cx_init_quantizer is only
2016 * called later when needed. This will avoid unnecessary calls of
2017 * vp8cx_init_quantizer() for every frame.
2019 vp8cx_init_quantizer(cpi);
2021 vp8_loop_filter_init(cm);
2023 cpi->common.error.setjmp = 0;
2025 #if CONFIG_MULTI_RES_ENCODING
2027 /* Calculate # of MBs in a row in lower-resolution level image. */
2028 if (cpi->oxcf.mr_encoder_id > 0)
2029 vp8_cal_low_res_mb_cols(cpi);
2033 /* setup RD costs to MACROBLOCK struct */
2035 cpi->mb.mvcost[0] = &cpi->rd_costs.mvcosts[0][mv_max+1];
2036 cpi->mb.mvcost[1] = &cpi->rd_costs.mvcosts[1][mv_max+1];
2037 cpi->mb.mvsadcost[0] = &cpi->rd_costs.mvsadcosts[0][mvfp_max+1];
2038 cpi->mb.mvsadcost[1] = &cpi->rd_costs.mvsadcosts[1][mvfp_max+1];
2040 cal_mvsadcosts(cpi->mb.mvsadcost);
2042 cpi->mb.mbmode_cost = cpi->rd_costs.mbmode_cost;
2043 cpi->mb.intra_uv_mode_cost = cpi->rd_costs.intra_uv_mode_cost;
2044 cpi->mb.bmode_costs = cpi->rd_costs.bmode_costs;
2045 cpi->mb.inter_bmode_costs = cpi->rd_costs.inter_bmode_costs;
2046 cpi->mb.token_costs = cpi->rd_costs.token_costs;
2048 /* setup block ptrs & offsets */
2049 vp8_setup_block_ptrs(&cpi->mb);
2050 vp8_setup_block_dptrs(&cpi->mb.e_mbd);
2056 void vp8_remove_compressor(VP8_COMP **ptr)
2058 VP8_COMP *cpi = *ptr;
2063 if (cpi && (cpi->common.current_video_frame > 0))
2065 #if !(CONFIG_REALTIME_ONLY)
2069 vp8_end_second_pass(cpi);
2074 #ifdef ENTROPY_STATS
2075 print_context_counters();
2076 print_tree_update_probs();
2077 print_mode_context();
2080 #if CONFIG_INTERNAL_STATS
2084 FILE *f = fopen("opsnr.stt", "a");
2085 double time_encoded = (cpi->last_end_time_stamp_seen
2086 - cpi->first_time_stamp_ever) / 10000000.000;
2087 double total_encode_time = (cpi->time_receive_data +
2088 cpi->time_compress_data) / 1000.000;
2089 double dr = (double)cpi->bytes * 8.0 / 1000.0 / time_encoded;
2091 if (cpi->b_calculate_psnr)
2093 YV12_BUFFER_CONFIG *lst_yv12 =
2094 &cpi->common.yv12_fb[cpi->common.lst_fb_idx];
2096 if (cpi->oxcf.number_of_layers > 1)
2100 fprintf(f, "Layer\tBitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\t"
2101 "GLPsnrP\tVPXSSIM\t\n");
2102 for (i=0; i<(int)cpi->oxcf.number_of_layers; i++)
2104 double dr = (double)cpi->bytes_in_layer[i] *
2105 8.0 / 1000.0 / time_encoded;
2106 double samples = 3.0 / 2 * cpi->frames_in_layer[i] *
2107 lst_yv12->y_width * lst_yv12->y_height;
2108 double total_psnr = vp8_mse2psnr(samples, 255.0,
2109 cpi->total_error2[i]);
2110 double total_psnr2 = vp8_mse2psnr(samples, 255.0,
2111 cpi->total_error2_p[i]);
2112 double total_ssim = 100 * pow(cpi->sum_ssim[i] /
2113 cpi->sum_weights[i], 8.0);
2115 fprintf(f, "%5d\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
2118 cpi->sum_psnr[i] / cpi->frames_in_layer[i],
2120 cpi->sum_psnr_p[i] / cpi->frames_in_layer[i],
2121 total_psnr2, total_ssim);
2126 double samples = 3.0 / 2 * cpi->count *
2127 lst_yv12->y_width * lst_yv12->y_height;
2128 double total_psnr = vp8_mse2psnr(samples, 255.0,
2129 cpi->total_sq_error);
2130 double total_psnr2 = vp8_mse2psnr(samples, 255.0,
2131 cpi->total_sq_error2);
2132 double total_ssim = 100 * pow(cpi->summed_quality /
2133 cpi->summed_weights, 8.0);
2135 fprintf(f, "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\t"
2136 "GLPsnrP\tVPXSSIM\t Time(us)\n");
2137 fprintf(f, "%7.3f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
2139 dr, cpi->total / cpi->count, total_psnr,
2140 cpi->totalp / cpi->count, total_psnr2,
2141 total_ssim, total_encode_time);
2145 if (cpi->b_calculate_ssimg)
2147 if (cpi->oxcf.number_of_layers > 1)
2151 fprintf(f, "Layer\tBitRate\tSSIM_Y\tSSIM_U\tSSIM_V\tSSIM_A\t"
2153 for (i=0; i<(int)cpi->oxcf.number_of_layers; i++)
2155 double dr = (double)cpi->bytes_in_layer[i] *
2156 8.0 / 1000.0 / time_encoded;
2157 fprintf(f, "%5d\t%7.3f\t%6.4f\t"
2158 "%6.4f\t%6.4f\t%6.4f\t%8.0f\n",
2160 cpi->total_ssimg_y_in_layer[i] /
2161 cpi->frames_in_layer[i],
2162 cpi->total_ssimg_u_in_layer[i] /
2163 cpi->frames_in_layer[i],
2164 cpi->total_ssimg_v_in_layer[i] /
2165 cpi->frames_in_layer[i],
2166 cpi->total_ssimg_all_in_layer[i] /
2167 cpi->frames_in_layer[i],
2173 fprintf(f, "BitRate\tSSIM_Y\tSSIM_U\tSSIM_V\tSSIM_A\t"
2175 fprintf(f, "%7.3f\t%6.4f\t%6.4f\t%6.4f\t%6.4f\t%8.0f\n", dr,
2176 cpi->total_ssimg_y / cpi->count,
2177 cpi->total_ssimg_u / cpi->count,
2178 cpi->total_ssimg_v / cpi->count,
2179 cpi->total_ssimg_all / cpi->count, total_encode_time);
2185 f = fopen("qskip.stt", "a");
2186 fprintf(f, "minq:%d -maxq:%d skiptrue:skipfalse = %d:%d\n", cpi->oxcf.best_allowed_q, cpi->oxcf.worst_allowed_q, skiptruecount, skipfalsecount);
2197 if (cpi->compressor_speed == 2)
2200 FILE *f = fopen("cxspeed.stt", "a");
2201 cnt_pm /= cpi->common.MBs;
2203 for (i = 0; i < 16; i++)
2204 fprintf(f, "%5d", frames_at_speed[i]);
2215 extern int count_mb_seg[4];
2216 FILE *f = fopen("modes.stt", "a");
2217 double dr = (double)cpi->frame_rate * (double)bytes * (double)8 / (double)count / (double)1000 ;
2218 fprintf(f, "intra_mode in Intra Frames:\n");
2219 fprintf(f, "Y: %8d, %8d, %8d, %8d, %8d\n", y_modes[0], y_modes[1], y_modes[2], y_modes[3], y_modes[4]);
2220 fprintf(f, "UV:%8d, %8d, %8d, %8d\n", uv_modes[0], uv_modes[1], uv_modes[2], uv_modes[3]);
2225 for (i = 0; i < 10; i++)
2226 fprintf(f, "%8d, ", b_modes[i]);
2232 fprintf(f, "Modes in Inter Frames:\n");
2233 fprintf(f, "Y: %8d, %8d, %8d, %8d, %8d, %8d, %8d, %8d, %8d, %8d\n",
2234 inter_y_modes[0], inter_y_modes[1], inter_y_modes[2], inter_y_modes[3], inter_y_modes[4],
2235 inter_y_modes[5], inter_y_modes[6], inter_y_modes[7], inter_y_modes[8], inter_y_modes[9]);
2236 fprintf(f, "UV:%8d, %8d, %8d, %8d\n", inter_uv_modes[0], inter_uv_modes[1], inter_uv_modes[2], inter_uv_modes[3]);
2241 for (i = 0; i < 15; i++)
2242 fprintf(f, "%8d, ", inter_b_modes[i]);
2247 fprintf(f, "P:%8d, %8d, %8d, %8d\n", count_mb_seg[0], count_mb_seg[1], count_mb_seg[2], count_mb_seg[3]);
2248 fprintf(f, "PB:%8d, %8d, %8d, %8d\n", inter_b_modes[LEFT4X4], inter_b_modes[ABOVE4X4], inter_b_modes[ZERO4X4], inter_b_modes[NEW4X4]);
2256 #ifdef ENTROPY_STATS
2259 FILE *fmode = fopen("modecontext.c", "w");
2261 fprintf(fmode, "\n#include \"entropymode.h\"\n\n");
2262 fprintf(fmode, "const unsigned int vp8_kf_default_bmode_counts ");
2263 fprintf(fmode, "[VP8_BINTRAMODES] [VP8_BINTRAMODES] [VP8_BINTRAMODES] =\n{\n");
2265 for (i = 0; i < 10; i++)
2268 fprintf(fmode, " { /* Above Mode : %d */\n", i);
2270 for (j = 0; j < 10; j++)
2273 fprintf(fmode, " {");
2275 for (k = 0; k < 10; k++)
2277 if (!intra_mode_stats[i][j][k])
2278 fprintf(fmode, " %5d, ", 1);
2280 fprintf(fmode, " %5d, ", intra_mode_stats[i][j][k]);
2283 fprintf(fmode, "}, /* left_mode %d */\n", j);
2287 fprintf(fmode, " },\n");
2291 fprintf(fmode, "};\n");
2297 #if defined(SECTIONBITS_OUTPUT)
2302 FILE *f = fopen("tokenbits.stt", "a");
2304 for (i = 0; i < 28; i++)
2305 fprintf(f, "%8d", (int)(Sectionbits[i] / 256));
2315 printf("\n_pick_loop_filter_level:%d\n", cpi->time_pick_lpf / 1000);
2316 printf("\n_frames recive_data encod_mb_row compress_frame Total\n");
2317 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);
2323 #if CONFIG_MULTITHREAD
2324 vp8cx_remove_encoder_threads(cpi);
2327 #if CONFIG_TEMPORAL_DENOISING
2328 vp8_denoiser_free(&cpi->denoiser);
2330 dealloc_compressor_data(cpi);
2331 vpx_free(cpi->mb.ss);
2333 vpx_free(cpi->cyclic_refresh_map);
2335 vp8_remove_common(&cpi->common);
2339 #ifdef OUTPUT_YUV_SRC
2359 static uint64_t calc_plane_error(unsigned char *orig, int orig_stride,
2360 unsigned char *recon, int recon_stride,
2361 unsigned int cols, unsigned int rows)
2363 unsigned int row, col;
2364 uint64_t total_sse = 0;
2367 for (row = 0; row + 16 <= rows; row += 16)
2369 for (col = 0; col + 16 <= cols; col += 16)
2373 vp8_mse16x16(orig + col, orig_stride,
2374 recon + col, recon_stride,
2379 /* Handle odd-sized width */
2382 unsigned int border_row, border_col;
2383 unsigned char *border_orig = orig;
2384 unsigned char *border_recon = recon;
2386 for (border_row = 0; border_row < 16; border_row++)
2388 for (border_col = col; border_col < cols; border_col++)
2390 diff = border_orig[border_col] - border_recon[border_col];
2391 total_sse += diff * diff;
2394 border_orig += orig_stride;
2395 border_recon += recon_stride;
2399 orig += orig_stride * 16;
2400 recon += recon_stride * 16;
2403 /* Handle odd-sized height */
2404 for (; row < rows; row++)
2406 for (col = 0; col < cols; col++)
2408 diff = orig[col] - recon[col];
2409 total_sse += diff * diff;
2412 orig += orig_stride;
2413 recon += recon_stride;
2416 vp8_clear_system_state();
2421 static void generate_psnr_packet(VP8_COMP *cpi)
2423 YV12_BUFFER_CONFIG *orig = cpi->Source;
2424 YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
2425 struct vpx_codec_cx_pkt pkt;
2428 unsigned int width = cpi->common.Width;
2429 unsigned int height = cpi->common.Height;
2431 pkt.kind = VPX_CODEC_PSNR_PKT;
2432 sse = calc_plane_error(orig->y_buffer, orig->y_stride,
2433 recon->y_buffer, recon->y_stride,
2435 pkt.data.psnr.sse[0] = sse;
2436 pkt.data.psnr.sse[1] = sse;
2437 pkt.data.psnr.samples[0] = width * height;
2438 pkt.data.psnr.samples[1] = width * height;
2440 width = (width + 1) / 2;
2441 height = (height + 1) / 2;
2443 sse = calc_plane_error(orig->u_buffer, orig->uv_stride,
2444 recon->u_buffer, recon->uv_stride,
2446 pkt.data.psnr.sse[0] += sse;
2447 pkt.data.psnr.sse[2] = sse;
2448 pkt.data.psnr.samples[0] += width * height;
2449 pkt.data.psnr.samples[2] = width * height;
2451 sse = calc_plane_error(orig->v_buffer, orig->uv_stride,
2452 recon->v_buffer, recon->uv_stride,
2454 pkt.data.psnr.sse[0] += sse;
2455 pkt.data.psnr.sse[3] = sse;
2456 pkt.data.psnr.samples[0] += width * height;
2457 pkt.data.psnr.samples[3] = width * height;
2459 for (i = 0; i < 4; i++)
2460 pkt.data.psnr.psnr[i] = vp8_mse2psnr(pkt.data.psnr.samples[i], 255.0,
2461 (double)(pkt.data.psnr.sse[i]));
2463 vpx_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
2467 int vp8_use_as_reference(VP8_COMP *cpi, int ref_frame_flags)
2469 if (ref_frame_flags > 7)
2472 cpi->ref_frame_flags = ref_frame_flags;
2475 int vp8_update_reference(VP8_COMP *cpi, int ref_frame_flags)
2477 if (ref_frame_flags > 7)
2480 cpi->common.refresh_golden_frame = 0;
2481 cpi->common.refresh_alt_ref_frame = 0;
2482 cpi->common.refresh_last_frame = 0;
2484 if (ref_frame_flags & VP8_LAST_FRAME)
2485 cpi->common.refresh_last_frame = 1;
2487 if (ref_frame_flags & VP8_GOLD_FRAME)
2488 cpi->common.refresh_golden_frame = 1;
2490 if (ref_frame_flags & VP8_ALTR_FRAME)
2491 cpi->common.refresh_alt_ref_frame = 1;
2496 int vp8_get_reference(VP8_COMP *cpi, enum vpx_ref_frame_type ref_frame_flag, YV12_BUFFER_CONFIG *sd)
2498 VP8_COMMON *cm = &cpi->common;
2501 if (ref_frame_flag == VP8_LAST_FRAME)
2502 ref_fb_idx = cm->lst_fb_idx;
2503 else if (ref_frame_flag == VP8_GOLD_FRAME)
2504 ref_fb_idx = cm->gld_fb_idx;
2505 else if (ref_frame_flag == VP8_ALTR_FRAME)
2506 ref_fb_idx = cm->alt_fb_idx;
2510 vp8_yv12_copy_frame(&cm->yv12_fb[ref_fb_idx], sd);
2514 int vp8_set_reference(VP8_COMP *cpi, enum vpx_ref_frame_type ref_frame_flag, YV12_BUFFER_CONFIG *sd)
2516 VP8_COMMON *cm = &cpi->common;
2520 if (ref_frame_flag == VP8_LAST_FRAME)
2521 ref_fb_idx = cm->lst_fb_idx;
2522 else if (ref_frame_flag == VP8_GOLD_FRAME)
2523 ref_fb_idx = cm->gld_fb_idx;
2524 else if (ref_frame_flag == VP8_ALTR_FRAME)
2525 ref_fb_idx = cm->alt_fb_idx;
2529 vp8_yv12_copy_frame(sd, &cm->yv12_fb[ref_fb_idx]);
2533 int vp8_update_entropy(VP8_COMP *cpi, int update)
2535 VP8_COMMON *cm = &cpi->common;
2536 cm->refresh_entropy_probs = update;
2543 void vp8_write_yuv_frame(const char *name, YV12_BUFFER_CONFIG *s)
2545 FILE *yuv_file = fopen(name, "ab");
2546 unsigned char *src = s->y_buffer;
2547 int h = s->y_height;
2551 fwrite(src, s->y_width, 1, yuv_file);
2561 fwrite(src, s->uv_width, 1, yuv_file);
2562 src += s->uv_stride;
2571 fwrite(src, s->uv_width, 1, yuv_file);
2572 src += s->uv_stride;
2581 static void scale_and_extend_source(YV12_BUFFER_CONFIG *sd, VP8_COMP *cpi)
2583 VP8_COMMON *cm = &cpi->common;
2585 /* are we resizing the image */
2586 if (cm->horiz_scale != 0 || cm->vert_scale != 0)
2588 #if CONFIG_SPATIAL_RESAMPLING
2589 int UNINITIALIZED_IS_SAFE(hr), UNINITIALIZED_IS_SAFE(hs);
2590 int UNINITIALIZED_IS_SAFE(vr), UNINITIALIZED_IS_SAFE(vs);
2593 if (cm->vert_scale == 3)
2598 Scale2Ratio(cm->horiz_scale, &hr, &hs);
2599 Scale2Ratio(cm->vert_scale, &vr, &vs);
2601 vp8_scale_frame(sd, &cpi->scaled_source, cm->temp_scale_frame.y_buffer,
2602 tmp_height, hs, hr, vs, vr, 0);
2604 vp8_yv12_extend_frame_borders(&cpi->scaled_source);
2605 cpi->Source = &cpi->scaled_source;
2613 static int resize_key_frame(VP8_COMP *cpi)
2615 #if CONFIG_SPATIAL_RESAMPLING
2616 VP8_COMMON *cm = &cpi->common;
2618 /* Do we need to apply resampling for one pass cbr.
2619 * In one pass this is more limited than in two pass cbr
2620 * The test and any change is only made one per key frame sequence
2622 if (cpi->oxcf.allow_spatial_resampling && (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER))
2624 int UNINITIALIZED_IS_SAFE(hr), UNINITIALIZED_IS_SAFE(hs);
2625 int UNINITIALIZED_IS_SAFE(vr), UNINITIALIZED_IS_SAFE(vs);
2626 int new_width, new_height;
2628 /* If we are below the resample DOWN watermark then scale down a
2631 if (cpi->buffer_level < (cpi->oxcf.resample_down_water_mark * cpi->oxcf.optimal_buffer_level / 100))
2633 cm->horiz_scale = (cm->horiz_scale < ONETWO) ? cm->horiz_scale + 1 : ONETWO;
2634 cm->vert_scale = (cm->vert_scale < ONETWO) ? cm->vert_scale + 1 : ONETWO;
2636 /* Should we now start scaling back up */
2637 else if (cpi->buffer_level > (cpi->oxcf.resample_up_water_mark * cpi->oxcf.optimal_buffer_level / 100))
2639 cm->horiz_scale = (cm->horiz_scale > NORMAL) ? cm->horiz_scale - 1 : NORMAL;
2640 cm->vert_scale = (cm->vert_scale > NORMAL) ? cm->vert_scale - 1 : NORMAL;
2643 /* Get the new hieght and width */
2644 Scale2Ratio(cm->horiz_scale, &hr, &hs);
2645 Scale2Ratio(cm->vert_scale, &vr, &vs);
2646 new_width = ((hs - 1) + (cpi->oxcf.Width * hr)) / hs;
2647 new_height = ((vs - 1) + (cpi->oxcf.Height * vr)) / vs;
2649 /* If the image size has changed we need to reallocate the buffers
2650 * and resample the source image
2652 if ((cm->Width != new_width) || (cm->Height != new_height))
2654 cm->Width = new_width;
2655 cm->Height = new_height;
2656 vp8_alloc_compressor_data(cpi);
2657 scale_and_extend_source(cpi->un_scaled_source, cpi);
2667 static void update_alt_ref_frame_stats(VP8_COMP *cpi)
2669 VP8_COMMON *cm = &cpi->common;
2671 /* Select an interval before next GF or altref */
2672 if (!cpi->auto_gold)
2673 cpi->frames_till_gf_update_due = DEFAULT_GF_INTERVAL;
2675 if ((cpi->pass != 2) && cpi->frames_till_gf_update_due)
2677 cpi->current_gf_interval = cpi->frames_till_gf_update_due;
2679 /* Set the bits per frame that we should try and recover in
2680 * subsequent inter frames to account for the extra GF spend...
2681 * note that his does not apply for GF updates that occur
2682 * coincident with a key frame as the extra cost of key frames is
2683 * dealt with elsewhere.
2685 cpi->gf_overspend_bits += cpi->projected_frame_size;
2686 cpi->non_gf_bitrate_adjustment = cpi->gf_overspend_bits / cpi->frames_till_gf_update_due;
2689 /* Update data structure that monitors level of reference to last GF */
2690 vpx_memset(cpi->gf_active_flags, 1, (cm->mb_rows * cm->mb_cols));
2691 cpi->gf_active_count = cm->mb_rows * cm->mb_cols;
2693 /* this frame refreshes means next frames don't unless specified by user */
2694 cpi->common.frames_since_golden = 0;
2696 /* Clear the alternate reference update pending flag. */
2697 cpi->source_alt_ref_pending = 0;
2699 /* Set the alternate refernce frame active flag */
2700 cpi->source_alt_ref_active = 1;
2704 static void update_golden_frame_stats(VP8_COMP *cpi)
2706 VP8_COMMON *cm = &cpi->common;
2708 /* Update the Golden frame usage counts. */
2709 if (cm->refresh_golden_frame)
2711 /* Select an interval before next GF */
2712 if (!cpi->auto_gold)
2713 cpi->frames_till_gf_update_due = DEFAULT_GF_INTERVAL;
2715 if ((cpi->pass != 2) && (cpi->frames_till_gf_update_due > 0))
2717 cpi->current_gf_interval = cpi->frames_till_gf_update_due;
2719 /* Set the bits per frame that we should try and recover in
2720 * subsequent inter frames to account for the extra GF spend...
2721 * note that his does not apply for GF updates that occur
2722 * coincident with a key frame as the extra cost of key frames
2723 * is dealt with elsewhere.
2725 if ((cm->frame_type != KEY_FRAME) && !cpi->source_alt_ref_active)
2727 /* Calcluate GF bits to be recovered
2728 * Projected size - av frame bits available for inter
2729 * frames for clip as a whole
2731 cpi->gf_overspend_bits += (cpi->projected_frame_size - cpi->inter_frame_target);
2734 cpi->non_gf_bitrate_adjustment = cpi->gf_overspend_bits / cpi->frames_till_gf_update_due;
2738 /* Update data structure that monitors level of reference to last GF */
2739 vpx_memset(cpi->gf_active_flags, 1, (cm->mb_rows * cm->mb_cols));
2740 cpi->gf_active_count = cm->mb_rows * cm->mb_cols;
2742 /* this frame refreshes means next frames don't unless specified by
2745 cm->refresh_golden_frame = 0;
2746 cpi->common.frames_since_golden = 0;
2748 cpi->recent_ref_frame_usage[INTRA_FRAME] = 1;
2749 cpi->recent_ref_frame_usage[LAST_FRAME] = 1;
2750 cpi->recent_ref_frame_usage[GOLDEN_FRAME] = 1;
2751 cpi->recent_ref_frame_usage[ALTREF_FRAME] = 1;
2753 /* ******** Fixed Q test code only ************ */
2754 /* If we are going to use the ALT reference for the next group of
2755 * frames set a flag to say so.
2757 if (cpi->oxcf.fixed_q >= 0 &&
2758 cpi->oxcf.play_alternate && !cpi->common.refresh_alt_ref_frame)
2760 cpi->source_alt_ref_pending = 1;
2761 cpi->frames_till_gf_update_due = cpi->baseline_gf_interval;
2764 if (!cpi->source_alt_ref_pending)
2765 cpi->source_alt_ref_active = 0;
2767 /* Decrement count down till next gf */
2768 if (cpi->frames_till_gf_update_due > 0)
2769 cpi->frames_till_gf_update_due--;
2772 else if (!cpi->common.refresh_alt_ref_frame)
2774 /* Decrement count down till next gf */
2775 if (cpi->frames_till_gf_update_due > 0)
2776 cpi->frames_till_gf_update_due--;
2778 if (cpi->common.frames_till_alt_ref_frame)
2779 cpi->common.frames_till_alt_ref_frame --;
2781 cpi->common.frames_since_golden ++;
2783 if (cpi->common.frames_since_golden > 1)
2785 cpi->recent_ref_frame_usage[INTRA_FRAME] += cpi->count_mb_ref_frame_usage[INTRA_FRAME];
2786 cpi->recent_ref_frame_usage[LAST_FRAME] += cpi->count_mb_ref_frame_usage[LAST_FRAME];
2787 cpi->recent_ref_frame_usage[GOLDEN_FRAME] += cpi->count_mb_ref_frame_usage[GOLDEN_FRAME];
2788 cpi->recent_ref_frame_usage[ALTREF_FRAME] += cpi->count_mb_ref_frame_usage[ALTREF_FRAME];
2793 /* This function updates the reference frame probability estimates that
2794 * will be used during mode selection
2796 static void update_rd_ref_frame_probs(VP8_COMP *cpi)
2798 VP8_COMMON *cm = &cpi->common;
2800 const int *const rfct = cpi->count_mb_ref_frame_usage;
2801 const int rf_intra = rfct[INTRA_FRAME];
2802 const int rf_inter = rfct[LAST_FRAME] + rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME];
2804 if (cm->frame_type == KEY_FRAME)
2806 cpi->prob_intra_coded = 255;
2807 cpi->prob_last_coded = 128;
2808 cpi->prob_gf_coded = 128;
2810 else if (!(rf_intra + rf_inter))
2812 cpi->prob_intra_coded = 63;
2813 cpi->prob_last_coded = 128;
2814 cpi->prob_gf_coded = 128;
2817 /* update reference frame costs since we can do better than what we got
2820 if (cpi->oxcf.number_of_layers == 1)
2822 if (cpi->common.refresh_alt_ref_frame)
2824 cpi->prob_intra_coded += 40;
2825 cpi->prob_last_coded = 200;
2826 cpi->prob_gf_coded = 1;
2828 else if (cpi->common.frames_since_golden == 0)
2830 cpi->prob_last_coded = 214;
2832 else if (cpi->common.frames_since_golden == 1)
2834 cpi->prob_last_coded = 192;
2835 cpi->prob_gf_coded = 220;
2837 else if (cpi->source_alt_ref_active)
2839 cpi->prob_gf_coded -= 20;
2841 if (cpi->prob_gf_coded < 10)
2842 cpi->prob_gf_coded = 10;
2844 if (!cpi->source_alt_ref_active)
2845 cpi->prob_gf_coded = 255;
2850 /* 1 = key, 0 = inter */
2851 static int decide_key_frame(VP8_COMP *cpi)
2853 VP8_COMMON *cm = &cpi->common;
2855 int code_key_frame = 0;
2859 if (cpi->Speed > 11)
2862 /* Clear down mmx registers */
2863 vp8_clear_system_state();
2865 if ((cpi->compressor_speed == 2) && (cpi->Speed >= 5) && (cpi->sf.RD == 0))
2867 double change = 1.0 * abs((int)(cpi->intra_error - cpi->last_intra_error)) / (1 + cpi->last_intra_error);
2868 double change2 = 1.0 * abs((int)(cpi->prediction_error - cpi->last_prediction_error)) / (1 + cpi->last_prediction_error);
2869 double minerror = cm->MBs * 256;
2873 if (10 * cpi->intra_error / (1 + cpi->prediction_error) < 15
2874 && cpi->prediction_error > minerror
2875 && (change > .25 || change2 > .25))
2877 FILE *f = fopen("intra_inter.stt", "a");
2879 if (cpi->prediction_error <= 0)
2880 cpi->prediction_error = 1;
2882 fprintf(f, "%d %d %d %d %14.4f\n",
2883 cm->current_video_frame,
2884 (int) cpi->prediction_error,
2885 (int) cpi->intra_error,
2886 (int)((10 * cpi->intra_error) / cpi->prediction_error),
2894 cpi->last_intra_error = cpi->intra_error;
2895 cpi->last_prediction_error = cpi->prediction_error;
2897 if (10 * cpi->intra_error / (1 + cpi->prediction_error) < 15
2898 && cpi->prediction_error > minerror
2899 && (change > .25 || change2 > .25))
2901 /*(change > 1.4 || change < .75)&& cpi->this_frame_percent_intra > cpi->last_frame_percent_intra + 3*/
2909 /* If the following are true we might as well code a key frame */
2910 if (((cpi->this_frame_percent_intra == 100) &&
2911 (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra + 2))) ||
2912 ((cpi->this_frame_percent_intra > 95) &&
2913 (cpi->this_frame_percent_intra >= (cpi->last_frame_percent_intra + 5))))
2917 /* in addition if the following are true and this is not a golden frame
2918 * then code a key frame Note that on golden frames there often seems
2919 * to be a pop in intra useage anyway hence this restriction is
2920 * designed to prevent spurious key frames. The Intra pop needs to be
2923 else if (((cpi->this_frame_percent_intra > 60) &&
2924 (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra * 2))) ||
2925 ((cpi->this_frame_percent_intra > 75) &&
2926 (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra * 3 / 2))) ||
2927 ((cpi->this_frame_percent_intra > 90) &&
2928 (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra + 10))))
2930 if (!cm->refresh_golden_frame)
2934 return code_key_frame;
2938 #if !(CONFIG_REALTIME_ONLY)
2939 static void Pass1Encode(VP8_COMP *cpi, unsigned long *size, unsigned char *dest, unsigned int *frame_flags)
2944 vp8_set_quantizer(cpi, 26);
2946 vp8_first_pass(cpi);
2951 void write_cx_frame_to_file(YV12_BUFFER_CONFIG *frame, int this_frame)
2954 /* write the frame */
2959 sprintf(filename, "cx\\y%04d.raw", this_frame);
2960 yframe = fopen(filename, "wb");
2962 for (i = 0; i < frame->y_height; i++)
2963 fwrite(frame->y_buffer + i * frame->y_stride, frame->y_width, 1, yframe);
2966 sprintf(filename, "cx\\u%04d.raw", this_frame);
2967 yframe = fopen(filename, "wb");
2969 for (i = 0; i < frame->uv_height; i++)
2970 fwrite(frame->u_buffer + i * frame->uv_stride, frame->uv_width, 1, yframe);
2973 sprintf(filename, "cx\\v%04d.raw", this_frame);
2974 yframe = fopen(filename, "wb");
2976 for (i = 0; i < frame->uv_height; i++)
2977 fwrite(frame->v_buffer + i * frame->uv_stride, frame->uv_width, 1, yframe);
2982 /* return of 0 means drop frame */
2984 /* Function to test for conditions that indeicate we should loop
2985 * back and recode a frame.
2987 static int recode_loop_test( VP8_COMP *cpi,
2988 int high_limit, int low_limit,
2989 int q, int maxq, int minq )
2991 int force_recode = 0;
2992 VP8_COMMON *cm = &cpi->common;
2994 /* Is frame recode allowed at all
2995 * Yes if either recode mode 1 is selected or mode two is selcted
2996 * and the frame is a key frame. golden frame or alt_ref_frame
2998 if ( (cpi->sf.recode_loop == 1) ||
2999 ( (cpi->sf.recode_loop == 2) &&
3000 ( (cm->frame_type == KEY_FRAME) ||
3001 cm->refresh_golden_frame ||
3002 cm->refresh_alt_ref_frame ) ) )
3004 /* General over and under shoot tests */
3005 if ( ((cpi->projected_frame_size > high_limit) && (q < maxq)) ||
3006 ((cpi->projected_frame_size < low_limit) && (q > minq)) )
3010 /* Special Constrained quality tests */
3011 else if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY)
3013 /* Undershoot and below auto cq level */
3014 if ( (q > cpi->cq_target_quality) &&
3015 (cpi->projected_frame_size <
3016 ((cpi->this_frame_target * 7) >> 3)))
3020 /* Severe undershoot and between auto and user cq level */
3021 else if ( (q > cpi->oxcf.cq_level) &&
3022 (cpi->projected_frame_size < cpi->min_frame_bandwidth) &&
3023 (cpi->active_best_quality > cpi->oxcf.cq_level))
3026 cpi->active_best_quality = cpi->oxcf.cq_level;
3031 return force_recode;
3034 static void update_reference_frames(VP8_COMP *cpi)
3036 VP8_COMMON *cm = &cpi->common;
3037 YV12_BUFFER_CONFIG *yv12_fb = cm->yv12_fb;
3039 /* At this point the new frame has been encoded.
3040 * If any buffer copy / swapping is signaled it should be done here.
3043 if (cm->frame_type == KEY_FRAME)
3045 yv12_fb[cm->new_fb_idx].flags |= VP8_GOLD_FRAME | VP8_ALTR_FRAME ;
3047 yv12_fb[cm->gld_fb_idx].flags &= ~VP8_GOLD_FRAME;
3048 yv12_fb[cm->alt_fb_idx].flags &= ~VP8_ALTR_FRAME;
3050 cm->alt_fb_idx = cm->gld_fb_idx = cm->new_fb_idx;
3052 #if CONFIG_MULTI_RES_ENCODING
3053 cpi->current_ref_frames[GOLDEN_FRAME] = cm->current_video_frame;
3054 cpi->current_ref_frames[ALTREF_FRAME] = cm->current_video_frame;
3057 else /* For non key frames */
3059 if (cm->refresh_alt_ref_frame)
3061 assert(!cm->copy_buffer_to_arf);
3063 cm->yv12_fb[cm->new_fb_idx].flags |= VP8_ALTR_FRAME;
3064 cm->yv12_fb[cm->alt_fb_idx].flags &= ~VP8_ALTR_FRAME;
3065 cm->alt_fb_idx = cm->new_fb_idx;
3067 #if CONFIG_MULTI_RES_ENCODING
3068 cpi->current_ref_frames[ALTREF_FRAME] = cm->current_video_frame;
3071 else if (cm->copy_buffer_to_arf)
3073 assert(!(cm->copy_buffer_to_arf & ~0x3));
3075 if (cm->copy_buffer_to_arf == 1)
3077 if(cm->alt_fb_idx != cm->lst_fb_idx)
3079 yv12_fb[cm->lst_fb_idx].flags |= VP8_ALTR_FRAME;
3080 yv12_fb[cm->alt_fb_idx].flags &= ~VP8_ALTR_FRAME;
3081 cm->alt_fb_idx = cm->lst_fb_idx;
3083 #if CONFIG_MULTI_RES_ENCODING
3084 cpi->current_ref_frames[ALTREF_FRAME] =
3085 cpi->current_ref_frames[LAST_FRAME];
3089 else /* if (cm->copy_buffer_to_arf == 2) */
3091 if(cm->alt_fb_idx != cm->gld_fb_idx)
3093 yv12_fb[cm->gld_fb_idx].flags |= VP8_ALTR_FRAME;
3094 yv12_fb[cm->alt_fb_idx].flags &= ~VP8_ALTR_FRAME;
3095 cm->alt_fb_idx = cm->gld_fb_idx;
3097 #if CONFIG_MULTI_RES_ENCODING
3098 cpi->current_ref_frames[ALTREF_FRAME] =
3099 cpi->current_ref_frames[GOLDEN_FRAME];
3105 if (cm->refresh_golden_frame)
3107 assert(!cm->copy_buffer_to_gf);
3109 cm->yv12_fb[cm->new_fb_idx].flags |= VP8_GOLD_FRAME;
3110 cm->yv12_fb[cm->gld_fb_idx].flags &= ~VP8_GOLD_FRAME;
3111 cm->gld_fb_idx = cm->new_fb_idx;
3113 #if CONFIG_MULTI_RES_ENCODING
3114 cpi->current_ref_frames[GOLDEN_FRAME] = cm->current_video_frame;
3117 else if (cm->copy_buffer_to_gf)
3119 assert(!(cm->copy_buffer_to_arf & ~0x3));
3121 if (cm->copy_buffer_to_gf == 1)
3123 if(cm->gld_fb_idx != cm->lst_fb_idx)
3125 yv12_fb[cm->lst_fb_idx].flags |= VP8_GOLD_FRAME;
3126 yv12_fb[cm->gld_fb_idx].flags &= ~VP8_GOLD_FRAME;
3127 cm->gld_fb_idx = cm->lst_fb_idx;
3129 #if CONFIG_MULTI_RES_ENCODING
3130 cpi->current_ref_frames[GOLDEN_FRAME] =
3131 cpi->current_ref_frames[LAST_FRAME];
3135 else /* if (cm->copy_buffer_to_gf == 2) */
3137 if(cm->alt_fb_idx != cm->gld_fb_idx)
3139 yv12_fb[cm->alt_fb_idx].flags |= VP8_GOLD_FRAME;
3140 yv12_fb[cm->gld_fb_idx].flags &= ~VP8_GOLD_FRAME;
3141 cm->gld_fb_idx = cm->alt_fb_idx;
3143 #if CONFIG_MULTI_RES_ENCODING
3144 cpi->current_ref_frames[GOLDEN_FRAME] =
3145 cpi->current_ref_frames[ALTREF_FRAME];
3152 if (cm->refresh_last_frame)
3154 cm->yv12_fb[cm->new_fb_idx].flags |= VP8_LAST_FRAME;
3155 cm->yv12_fb[cm->lst_fb_idx].flags &= ~VP8_LAST_FRAME;
3156 cm->lst_fb_idx = cm->new_fb_idx;
3158 #if CONFIG_MULTI_RES_ENCODING
3159 cpi->current_ref_frames[LAST_FRAME] = cm->current_video_frame;
3164 void vp8_loopfilter_frame(VP8_COMP *cpi, VP8_COMMON *cm)
3166 const FRAME_TYPE frame_type = cm->frame_type;
3170 cm->filter_level = 0;
3174 struct vpx_usec_timer timer;
3176 vp8_clear_system_state();
3178 vpx_usec_timer_start(&timer);
3179 if (cpi->sf.auto_filter == 0)
3180 vp8cx_pick_filter_level_fast(cpi->Source, cpi);
3183 vp8cx_pick_filter_level(cpi->Source, cpi);
3185 if (cm->filter_level > 0)
3187 vp8cx_set_alt_lf_level(cpi, cm->filter_level);
3190 vpx_usec_timer_mark(&timer);
3191 cpi->time_pick_lpf += vpx_usec_timer_elapsed(&timer);
3194 #if CONFIG_MULTITHREAD
3195 if (cpi->b_multi_threaded)
3196 sem_post(&cpi->h_event_end_lpf); /* signal that we have set filter_level */
3199 if (cm->filter_level > 0)
3201 vp8_loop_filter_frame(cm, &cpi->mb.e_mbd, frame_type);
3204 vp8_yv12_extend_frame_borders(cm->frame_to_show);
3205 #if CONFIG_TEMPORAL_DENOISING
3206 if (cpi->oxcf.noise_sensitivity)
3210 /* we shouldn't have to keep multiple copies as we know in advance which
3211 * buffer we should start - for now to get something up and running
3212 * I've chosen to copy the buffers
3214 if (cm->frame_type == KEY_FRAME)
3217 vp8_yv12_copy_frame(
3219 &cpi->denoiser.yv12_running_avg[LAST_FRAME]);
3221 vp8_yv12_extend_frame_borders(
3222 &cpi->denoiser.yv12_running_avg[LAST_FRAME]);
3224 for (i = 2; i < MAX_REF_FRAMES - 1; i++)
3225 vp8_yv12_copy_frame(
3227 &cpi->denoiser.yv12_running_avg[i]);
3229 else /* For non key frames */
3231 vp8_yv12_extend_frame_borders(
3232 &cpi->denoiser.yv12_running_avg[LAST_FRAME]);
3234 if (cm->refresh_alt_ref_frame || cm->copy_buffer_to_arf)
3236 vp8_yv12_copy_frame(
3237 &cpi->denoiser.yv12_running_avg[LAST_FRAME],
3238 &cpi->denoiser.yv12_running_avg[ALTREF_FRAME]);
3240 if (cm->refresh_golden_frame || cm->copy_buffer_to_gf)
3242 vp8_yv12_copy_frame(
3243 &cpi->denoiser.yv12_running_avg[LAST_FRAME],
3244 &cpi->denoiser.yv12_running_avg[GOLDEN_FRAME]);
3253 static void encode_frame_to_data_rate
3256 unsigned long *size,
3257 unsigned char *dest,
3258 unsigned char* dest_end,
3259 unsigned int *frame_flags
3263 int frame_over_shoot_limit;
3264 int frame_under_shoot_limit;
3269 VP8_COMMON *cm = &cpi->common;
3270 int active_worst_qchanged = 0;
3272 #if !(CONFIG_REALTIME_ONLY)
3276 int zbin_oq_low = 0;
3279 int overshoot_seen = 0;
3280 int undershoot_seen = 0;
3283 int drop_mark = (int)(cpi->oxcf.drop_frames_water_mark *
3284 cpi->oxcf.optimal_buffer_level / 100);
3285 int drop_mark75 = drop_mark * 2 / 3;
3286 int drop_mark50 = drop_mark / 4;
3287 int drop_mark25 = drop_mark / 8;
3290 /* Clear down mmx registers to allow floating point in what follows */
3291 vp8_clear_system_state();
3293 #if CONFIG_MULTITHREAD
3294 /* wait for the last picture loopfilter thread done */
3295 if (cpi->b_lpf_running)
3297 sem_wait(&cpi->h_event_end_lpf);
3298 cpi->b_lpf_running = 0;
3302 if(cpi->force_next_frame_intra)
3304 cm->frame_type = KEY_FRAME; /* delayed intra frame */
3305 cpi->force_next_frame_intra = 0;
3308 /* For an alt ref frame in 2 pass we skip the call to the second pass
3309 * function that sets the target bandwidth
3311 #if !(CONFIG_REALTIME_ONLY)
3315 if (cpi->common.refresh_alt_ref_frame)
3317 /* Per frame bit target for the alt ref frame */
3318 cpi->per_frame_bandwidth = cpi->twopass.gf_bits;
3319 /* per second target bitrate */
3320 cpi->target_bandwidth = (int)(cpi->twopass.gf_bits *
3321 cpi->output_frame_rate);
3326 cpi->per_frame_bandwidth = (int)(cpi->target_bandwidth / cpi->output_frame_rate);
3328 /* Default turn off buffer to buffer copying */
3329 cm->copy_buffer_to_gf = 0;
3330 cm->copy_buffer_to_arf = 0;
3332 /* Clear zbin over-quant value and mode boost values. */
3333 cpi->zbin_over_quant = 0;
3334 cpi->zbin_mode_boost = 0;
3336 /* Enable or disable mode based tweaking of the zbin
3337 * For 2 Pass Only used where GF/ARF prediction quality
3338 * is above a threshold
3340 cpi->zbin_mode_boost_enabled = 1;
3343 if ( cpi->gfu_boost <= 400 )
3345 cpi->zbin_mode_boost_enabled = 0;
3349 /* Current default encoder behaviour for the altref sign bias */
3350 if (cpi->source_alt_ref_active)
3351 cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = 1;
3353 cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = 0;
3355 /* Check to see if a key frame is signalled
3356 * For two pass with auto key frame enabled cm->frame_type may already
3357 * be set, but not for one pass.
3359 if ((cm->current_video_frame == 0) ||
3360 (cm->frame_flags & FRAMEFLAGS_KEY) ||
3361 (cpi->oxcf.auto_key && (cpi->frames_since_key % cpi->key_frame_frequency == 0)))
3363 /* Key frame from VFW/auto-keyframe/first frame */
3364 cm->frame_type = KEY_FRAME;
3367 #if CONFIG_MULTI_RES_ENCODING
3368 /* In multi-resolution encoding, frame_type is decided by lowest-resolution
3369 * encoder. Same frame_type is adopted while encoding at other resolution.
3371 if (cpi->oxcf.mr_encoder_id)
3373 LOWER_RES_FRAME_INFO* low_res_frame_info
3374 = (LOWER_RES_FRAME_INFO*)cpi->oxcf.mr_low_res_mode_info;
3376 cm->frame_type = low_res_frame_info->frame_type;
3378 if(cm->frame_type != KEY_FRAME)
3380 cpi->mr_low_res_mv_avail = 1;
3381 cpi->mr_low_res_mv_avail &= !(low_res_frame_info->is_frame_dropped);
3383 if (cpi->ref_frame_flags & VP8_LAST_FRAME)
3384 cpi->mr_low_res_mv_avail &= (cpi->current_ref_frames[LAST_FRAME]
3385 == low_res_frame_info->low_res_ref_frames[LAST_FRAME]);
3387 if (cpi->ref_frame_flags & VP8_GOLD_FRAME)
3388 cpi->mr_low_res_mv_avail &= (cpi->current_ref_frames[GOLDEN_FRAME]
3389 == low_res_frame_info->low_res_ref_frames[GOLDEN_FRAME]);
3391 if (cpi->ref_frame_flags & VP8_ALTR_FRAME)
3392 cpi->mr_low_res_mv_avail &= (cpi->current_ref_frames[ALTREF_FRAME]
3393 == low_res_frame_info->low_res_ref_frames[ALTREF_FRAME]);
3398 /* Set various flags etc to special state if it is a key frame */
3399 if (cm->frame_type == KEY_FRAME)
3403 // Set the loop filter deltas and segmentation map update
3404 setup_features(cpi);
3406 /* The alternate reference frame cannot be active for a key frame */
3407 cpi->source_alt_ref_active = 0;
3409 /* Reset the RD threshold multipliers to default of * 1 (128) */
3410 for (i = 0; i < MAX_MODES; i++)
3412 cpi->rd_thresh_mult[i] = 128;
3417 /* Experimental code for lagged compress and one pass
3418 * Initialise one_pass GF frames stats
3419 * Update stats used for GF selection
3422 cpi->one_pass_frame_index = cm->current_video_frame % MAX_LAG_BUFFERS;
3424 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frames_so_far = 0;
3425 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_intra_error = 0.0;
3426 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_coded_error = 0.0;
3427 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_pcnt_inter = 0.0;
3428 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_pcnt_motion = 0.0;
3429 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvr = 0.0;
3430 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvr_abs = 0.0;
3431 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvc = 0.0;
3432 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvc_abs = 0.0;
3436 update_rd_ref_frame_probs(cpi);
3438 if (cpi->drop_frames_allowed)
3440 /* The reset to decimation 0 is only done here for one pass.
3441 * Once it is set two pass leaves decimation on till the next kf.
3443 if ((cpi->buffer_level > drop_mark) && (cpi->decimation_factor > 0))
3444 cpi->decimation_factor --;
3446 if (cpi->buffer_level > drop_mark75 && cpi->decimation_factor > 0)
3447 cpi->decimation_factor = 1;
3449 else if (cpi->buffer_level < drop_mark25 && (cpi->decimation_factor == 2 || cpi->decimation_factor == 3))
3451 cpi->decimation_factor = 3;
3453 else if (cpi->buffer_level < drop_mark50 && (cpi->decimation_factor == 1 || cpi->decimation_factor == 2))
3455 cpi->decimation_factor = 2;
3457 else if (cpi->buffer_level < drop_mark75 && (cpi->decimation_factor == 0 || cpi->decimation_factor == 1))
3459 cpi->decimation_factor = 1;
3463 /* The following decimates the frame rate according to a regular
3464 * pattern (i.e. to 1/2 or 2/3 frame rate) This can be used to help
3465 * prevent buffer under-run in CBR mode. Alternatively it might be
3466 * desirable in some situations to drop frame rate but throw more bits
3469 * Note that dropping a key frame can be problematic if spatial
3470 * resampling is also active
3472 if (cpi->decimation_factor > 0)
3474 switch (cpi->decimation_factor)
3477 cpi->per_frame_bandwidth = cpi->per_frame_bandwidth * 3 / 2;
3480 cpi->per_frame_bandwidth = cpi->per_frame_bandwidth * 5 / 4;
3483 cpi->per_frame_bandwidth = cpi->per_frame_bandwidth * 5 / 4;
3487 /* Note that we should not throw out a key frame (especially when
3488 * spatial resampling is enabled).
3490 if ((cm->frame_type == KEY_FRAME))
3492 cpi->decimation_count = cpi->decimation_factor;
3494 else if (cpi->decimation_count > 0)
3496 cpi->decimation_count --;
3498 cpi->bits_off_target += cpi->av_per_frame_bandwidth;
3499 if (cpi->bits_off_target > cpi->oxcf.maximum_buffer_size)
3500 cpi->bits_off_target = cpi->oxcf.maximum_buffer_size;
3502 #if CONFIG_MULTI_RES_ENCODING
3503 vp8_store_drop_frame_info(cpi);
3506 cm->current_video_frame++;
3507 cpi->frames_since_key++;
3509 #if CONFIG_INTERNAL_STATS
3513 cpi->buffer_level = cpi->bits_off_target;
3515 if (cpi->oxcf.number_of_layers > 1)
3519 /* Propagate bits saved by dropping the frame to higher
3522 for (i=cpi->current_layer+1; i<cpi->oxcf.number_of_layers; i++)
3524 LAYER_CONTEXT *lc = &cpi->layer_context[i];
3525 lc->bits_off_target += cpi->av_per_frame_bandwidth;
3526 if (lc->bits_off_target > lc->maximum_buffer_size)
3527 lc->bits_off_target = lc->maximum_buffer_size;
3528 lc->buffer_level = lc->bits_off_target;
3535 cpi->decimation_count = cpi->decimation_factor;
3538 cpi->decimation_count = 0;
3540 /* Decide how big to make the frame */
3541 if (!vp8_pick_frame_size(cpi))
3543 /*TODO: 2 drop_frame and return code could be put together. */
3544 #if CONFIG_MULTI_RES_ENCODING
3545 vp8_store_drop_frame_info(cpi);
3547 cm->current_video_frame++;
3548 cpi->frames_since_key++;
3552 /* Reduce active_worst_allowed_q for CBR if our buffer is getting too full.
3553 * This has a knock on effect on active best quality as well.
3554 * For CBR if the buffer reaches its maximum level then we can no longer
3555 * save up bits for later frames so we might as well use them up
3556 * on the current frame.
3558 if ((cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) &&
3559 (cpi->buffer_level >= cpi->oxcf.optimal_buffer_level) && cpi->buffered_mode)
3561 /* Max adjustment is 1/4 */
3562 int Adjustment = cpi->active_worst_quality / 4;
3568 if (cpi->buffer_level < cpi->oxcf.maximum_buffer_size)
3570 buff_lvl_step = (int)
3571 ((cpi->oxcf.maximum_buffer_size -
3572 cpi->oxcf.optimal_buffer_level) /
3577 ((cpi->buffer_level -
3578 cpi->oxcf.optimal_buffer_level) /
3584 cpi->active_worst_quality -= Adjustment;
3586 if(cpi->active_worst_quality < cpi->active_best_quality)
3587 cpi->active_worst_quality = cpi->active_best_quality;
3591 /* Set an active best quality and if necessary active worst quality
3592 * There is some odd behavior for one pass here that needs attention.
3594 if ( (cpi->pass == 2) || (cpi->ni_frames > 150))
3596 vp8_clear_system_state();
3598 Q = cpi->active_worst_quality;
3600 if ( cm->frame_type == KEY_FRAME )
3602 if ( cpi->pass == 2 )
3604 if (cpi->gfu_boost > 600)
3605 cpi->active_best_quality = kf_low_motion_minq[Q];
3607 cpi->active_best_quality = kf_high_motion_minq[Q];
3609 /* Special case for key frames forced because we have reached
3610 * the maximum key frame interval. Here force the Q to a range
3611 * based on the ambient Q to reduce the risk of popping
3613 if ( cpi->this_key_frame_forced )
3615 if ( cpi->active_best_quality > cpi->avg_frame_qindex * 7/8)
3616 cpi->active_best_quality = cpi->avg_frame_qindex * 7/8;
3617 else if ( cpi->active_best_quality < cpi->avg_frame_qindex >> 2 )
3618 cpi->active_best_quality = cpi->avg_frame_qindex >> 2;
3621 /* One pass more conservative */
3623 cpi->active_best_quality = kf_high_motion_minq[Q];
3626 else if (cpi->oxcf.number_of_layers==1 &&
3627 (cm->refresh_golden_frame || cpi->common.refresh_alt_ref_frame))
3629 /* Use the lower of cpi->active_worst_quality and recent
3630 * average Q as basis for GF/ARF Q limit unless last frame was
3633 if ( (cpi->frames_since_key > 1) &&
3634 (cpi->avg_frame_qindex < cpi->active_worst_quality) )
3636 Q = cpi->avg_frame_qindex;
3639 /* For constrained quality dont allow Q less than the cq level */
3640 if ( (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) &&
3641 (Q < cpi->cq_target_quality) )
3643 Q = cpi->cq_target_quality;
3646 if ( cpi->pass == 2 )
3648 if ( cpi->gfu_boost > 1000 )
3649 cpi->active_best_quality = gf_low_motion_minq[Q];
3650 else if ( cpi->gfu_boost < 400 )
3651 cpi->active_best_quality = gf_high_motion_minq[Q];
3653 cpi->active_best_quality = gf_mid_motion_minq[Q];
3655 /* Constrained quality use slightly lower active best. */
3656 if ( cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY )
3658 cpi->active_best_quality =
3659 cpi->active_best_quality * 15/16;
3662 /* One pass more conservative */
3664 cpi->active_best_quality = gf_high_motion_minq[Q];
3668 cpi->active_best_quality = inter_minq[Q];
3670 /* For the constant/constrained quality mode we dont want
3671 * q to fall below the cq level.
3673 if ((cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) &&
3674 (cpi->active_best_quality < cpi->cq_target_quality) )
3676 /* If we are strongly undershooting the target rate in the last
3677 * frames then use the user passed in cq value not the auto
3680 if ( cpi->rolling_actual_bits < cpi->min_frame_bandwidth )
3681 cpi->active_best_quality = cpi->oxcf.cq_level;
3683 cpi->active_best_quality = cpi->cq_target_quality;
3687 /* If CBR and the buffer is as full then it is reasonable to allow
3688 * higher quality on the frames to prevent bits just going to waste.
3690 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
3692 /* Note that the use of >= here elliminates the risk of a devide
3693 * by 0 error in the else if clause
3695 if (cpi->buffer_level >= cpi->oxcf.maximum_buffer_size)
3696 cpi->active_best_quality = cpi->best_quality;
3698 else if (cpi->buffer_level > cpi->oxcf.optimal_buffer_level)
3700 int Fraction = (int)
3701 (((cpi->buffer_level - cpi->oxcf.optimal_buffer_level) * 128)
3702 / (cpi->oxcf.maximum_buffer_size -
3703 cpi->oxcf.optimal_buffer_level));
3704 int min_qadjustment = ((cpi->active_best_quality -
3705 cpi->best_quality) * Fraction) / 128;
3707 cpi->active_best_quality -= min_qadjustment;
3711 /* Make sure constrained quality mode limits are adhered to for the first
3712 * few frames of one pass encodes
3714 else if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY)
3716 if ( (cm->frame_type == KEY_FRAME) ||
3717 cm->refresh_golden_frame || cpi->common.refresh_alt_ref_frame )
3719 cpi->active_best_quality = cpi->best_quality;
3721 else if (cpi->active_best_quality < cpi->cq_target_quality)
3723 cpi->active_best_quality = cpi->cq_target_quality;
3727 /* Clip the active best and worst quality values to limits */
3728 if (cpi->active_worst_quality > cpi->worst_quality)
3729 cpi->active_worst_quality = cpi->worst_quality;
3731 if (cpi->active_best_quality < cpi->best_quality)
3732 cpi->active_best_quality = cpi->best_quality;
3734 if ( cpi->active_worst_quality < cpi->active_best_quality )
3735 cpi->active_worst_quality = cpi->active_best_quality;
3737 /* Determine initial Q to try */
3738 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
3740 #if !(CONFIG_REALTIME_ONLY)
3742 /* Set highest allowed value for Zbin over quant */
3743 if (cm->frame_type == KEY_FRAME)
3745 else if ((cpi->oxcf.number_of_layers == 1) && ((cm->refresh_alt_ref_frame ||
3746 (cm->refresh_golden_frame && !cpi->source_alt_ref_active))))
3751 zbin_oq_high = ZBIN_OQ_MAX;
3754 /* Setup background Q adjustment for error resilient mode.
3755 * For multi-layer encodes only enable this for the base layer.
3757 if (cpi->cyclic_refresh_mode_enabled)
3759 if (cpi->current_layer==0)
3760 cyclic_background_refresh(cpi, Q, 0);
3762 disable_segmentation(cpi);
3765 vp8_compute_frame_size_bounds(cpi, &frame_under_shoot_limit, &frame_over_shoot_limit);
3767 #if !(CONFIG_REALTIME_ONLY)
3768 /* Limit Q range for the adaptive loop. */
3769 bottom_index = cpi->active_best_quality;
3770 top_index = cpi->active_worst_quality;
3771 q_low = cpi->active_best_quality;
3772 q_high = cpi->active_worst_quality;
3775 vp8_save_coding_context(cpi);
3779 scale_and_extend_source(cpi->un_scaled_source, cpi);
3781 #if !(CONFIG_REALTIME_ONLY) && CONFIG_POSTPROC && !(CONFIG_TEMPORAL_DENOISING)
3783 if (cpi->oxcf.noise_sensitivity > 0)
3788 switch (cpi->oxcf.noise_sensitivity)
3811 if (cm->frame_type == KEY_FRAME)
3813 vp8_de_noise(cpi->Source, cpi->Source, l , 1, 0);
3817 vp8_de_noise(cpi->Source, cpi->Source, l , 1, 0);
3819 src = cpi->Source->y_buffer;
3821 if (cpi->Source->y_stride < 0)
3823 src += cpi->Source->y_stride * (cpi->Source->y_height - 1);
3830 #ifdef OUTPUT_YUV_SRC
3831 vp8_write_yuv_frame(cpi->Source);
3836 vp8_clear_system_state();
3838 vp8_set_quantizer(cpi, Q);
3840 /* setup skip prob for costing in mode/mv decision */
3841 if (cpi->common.mb_no_coeff_skip)
3843 cpi->prob_skip_false = cpi->base_skip_false_prob[Q];
3845 if (cm->frame_type != KEY_FRAME)
3847 if (cpi->common.refresh_alt_ref_frame)
3849 if (cpi->last_skip_false_probs[2] != 0)
3850 cpi->prob_skip_false = cpi->last_skip_false_probs[2];
3853 if(cpi->last_skip_false_probs[2]!=0 && abs(Q- cpi->last_skip_probs_q[2])<=16 )
3854 cpi->prob_skip_false = cpi->last_skip_false_probs[2];
3855 else if (cpi->last_skip_false_probs[2]!=0)
3856 cpi->prob_skip_false = (cpi->last_skip_false_probs[2] + cpi->prob_skip_false ) / 2;
3859 else if (cpi->common.refresh_golden_frame)
3861 if (cpi->last_skip_false_probs[1] != 0)
3862 cpi->prob_skip_false = cpi->last_skip_false_probs[1];
3865 if(cpi->last_skip_false_probs[1]!=0 && abs(Q- cpi->last_skip_probs_q[1])<=16 )
3866 cpi->prob_skip_false = cpi->last_skip_false_probs[1];
3867 else if (cpi->last_skip_false_probs[1]!=0)
3868 cpi->prob_skip_false = (cpi->last_skip_false_probs[1] + cpi->prob_skip_false ) / 2;
3873 if (cpi->last_skip_false_probs[0] != 0)
3874 cpi->prob_skip_false = cpi->last_skip_false_probs[0];
3877 if(cpi->last_skip_false_probs[0]!=0 && abs(Q- cpi->last_skip_probs_q[0])<=16 )
3878 cpi->prob_skip_false = cpi->last_skip_false_probs[0];
3879 else if(cpi->last_skip_false_probs[0]!=0)
3880 cpi->prob_skip_false = (cpi->last_skip_false_probs[0] + cpi->prob_skip_false ) / 2;
3884 /* as this is for cost estimate, let's make sure it does not
3885 * go extreme eitehr way
3887 if (cpi->prob_skip_false < 5)
3888 cpi->prob_skip_false = 5;
3890 if (cpi->prob_skip_false > 250)
3891 cpi->prob_skip_false = 250;
3893 if (cpi->oxcf.number_of_layers == 1 && cpi->is_src_frame_alt_ref)
3894 cpi->prob_skip_false = 1;
3901 FILE *f = fopen("skip.stt", "a");
3902 fprintf(f, "%d, %d, %4d ", cpi->common.refresh_golden_frame, cpi->common.refresh_alt_ref_frame, cpi->prob_skip_false);
3910 if (cm->frame_type == KEY_FRAME)
3912 if(resize_key_frame(cpi))
3914 /* If the frame size has changed, need to reset Q, quantizer,
3915 * and background refresh.
3917 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
3918 if (cpi->cyclic_refresh_mode_enabled)
3920 if (cpi->current_layer==0)
3921 cyclic_background_refresh(cpi, Q, 0);
3923 disable_segmentation(cpi);
3925 vp8_set_quantizer(cpi, Q);
3928 vp8_setup_key_frame(cpi);
3933 #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
3935 if(cpi->oxcf.error_resilient_mode)
3936 cm->refresh_entropy_probs = 0;
3938 if (cpi->oxcf.error_resilient_mode & VPX_ERROR_RESILIENT_PARTITIONS)
3940 if (cm->frame_type == KEY_FRAME)
3941 cm->refresh_entropy_probs = 1;
3944 if (cm->refresh_entropy_probs == 0)
3946 /* save a copy for later refresh */
3947 vpx_memcpy(&cm->lfc, &cm->fc, sizeof(cm->fc));
3950 vp8_update_coef_context(cpi);
3952 vp8_update_coef_probs(cpi);
3954 /* transform / motion compensation build reconstruction frame
3955 * +pack coef partitions
3957 vp8_encode_frame(cpi);
3959 /* cpi->projected_frame_size is not needed for RT mode */
3962 /* transform / motion compensation build reconstruction frame */
3963 vp8_encode_frame(cpi);
3965 cpi->projected_frame_size -= vp8_estimate_entropy_savings(cpi);
3966 cpi->projected_frame_size = (cpi->projected_frame_size > 0) ? cpi->projected_frame_size : 0;
3968 vp8_clear_system_state();
3970 /* Test to see if the stats generated for this frame indicate that
3971 * we should have coded a key frame (assuming that we didn't)!
3973 if (cpi->pass != 2 && cpi->oxcf.auto_key && cm->frame_type != KEY_FRAME)
3975 int key_frame_decision = decide_key_frame(cpi);
3977 if (cpi->compressor_speed == 2)
3979 /* we don't do re-encoding in realtime mode
3980 * if key frame is decided then we force it on next frame */
3981 cpi->force_next_frame_intra = key_frame_decision;
3983 #if !(CONFIG_REALTIME_ONLY)
3984 else if (key_frame_decision)
3986 /* Reset all our sizing numbers and recode */
3987 cm->frame_type = KEY_FRAME;
3989 vp8_pick_frame_size(cpi);
3991 /* Clear the Alt reference frame active flag when we have
3994 cpi->source_alt_ref_active = 0;
3996 // Set the loop filter deltas and segmentation map update
3997 setup_features(cpi);
3999 vp8_restore_coding_context(cpi);
4001 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4003 vp8_compute_frame_size_bounds(cpi, &frame_under_shoot_limit, &frame_over_shoot_limit);
4005 /* Limit Q range for the adaptive loop. */
4006 bottom_index = cpi->active_best_quality;
4007 top_index = cpi->active_worst_quality;
4008 q_low = cpi->active_best_quality;
4009 q_high = cpi->active_worst_quality;
4019 vp8_clear_system_state();
4021 if (frame_over_shoot_limit == 0)
4022 frame_over_shoot_limit = 1;
4024 /* Are we are overshooting and up against the limit of active max Q. */
4025 if (((cpi->pass != 2) || (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)) &&
4026 (Q == cpi->active_worst_quality) &&
4027 (cpi->active_worst_quality < cpi->worst_quality) &&
4028 (cpi->projected_frame_size > frame_over_shoot_limit))
4030 int over_size_percent = ((cpi->projected_frame_size - frame_over_shoot_limit) * 100) / frame_over_shoot_limit;
4032 /* If so is there any scope for relaxing it */
4033 while ((cpi->active_worst_quality < cpi->worst_quality) && (over_size_percent > 0))
4035 cpi->active_worst_quality++;
4036 /* Assume 1 qstep = about 4% on frame size. */
4037 over_size_percent = (int)(over_size_percent * 0.96);
4039 #if !(CONFIG_REALTIME_ONLY)
4040 top_index = cpi->active_worst_quality;
4042 /* If we have updated the active max Q do not call
4043 * vp8_update_rate_correction_factors() this loop.
4045 active_worst_qchanged = 1;
4048 active_worst_qchanged = 0;
4050 #if !(CONFIG_REALTIME_ONLY)
4051 /* Special case handling for forced key frames */
4052 if ( (cm->frame_type == KEY_FRAME) && cpi->this_key_frame_forced )
4055 int kf_err = vp8_calc_ss_err(cpi->Source,
4056 &cm->yv12_fb[cm->new_fb_idx]);
4058 /* The key frame is not good enough */
4059 if ( kf_err > ((cpi->ambient_err * 7) >> 3) )
4062 q_high = (Q > q_low) ? (Q - 1) : q_low;
4065 Q = (q_high + q_low) >> 1;
4067 /* The key frame is much better than the previous frame */
4068 else if ( kf_err < (cpi->ambient_err >> 1) )
4071 q_low = (Q < q_high) ? (Q + 1) : q_high;
4074 Q = (q_high + q_low + 1) >> 1;
4077 /* Clamp Q to upper and lower limits: */
4086 /* Is the projected frame size out of range and are we allowed
4087 * to attempt to recode.
4089 else if ( recode_loop_test( cpi,
4090 frame_over_shoot_limit, frame_under_shoot_limit,
4091 Q, top_index, bottom_index ) )
4096 /* Frame size out of permitted range. Update correction factor
4097 * & compute new Q to try...
4100 /* Frame is too large */
4101 if (cpi->projected_frame_size > cpi->this_frame_target)
4103 /* Raise Qlow as to at least the current value */
4104 q_low = (Q < q_high) ? (Q + 1) : q_high;
4106 /* If we are using over quant do the same for zbin_oq_low */
4107 if (cpi->zbin_over_quant > 0)
4108 zbin_oq_low = (cpi->zbin_over_quant < zbin_oq_high) ? (cpi->zbin_over_quant + 1) : zbin_oq_high;
4110 if (undershoot_seen)
4112 /* Update rate_correction_factor unless
4113 * cpi->active_worst_quality has changed.
4115 if (!active_worst_qchanged)
4116 vp8_update_rate_correction_factors(cpi, 1);
4118 Q = (q_high + q_low + 1) / 2;
4120 /* Adjust cpi->zbin_over_quant (only allowed when Q
4124 cpi->zbin_over_quant = 0;
4127 zbin_oq_low = (cpi->zbin_over_quant < zbin_oq_high) ? (cpi->zbin_over_quant + 1) : zbin_oq_high;
4128 cpi->zbin_over_quant = (zbin_oq_high + zbin_oq_low) / 2;
4133 /* Update rate_correction_factor unless
4134 * cpi->active_worst_quality has changed.
4136 if (!active_worst_qchanged)
4137 vp8_update_rate_correction_factors(cpi, 0);
4139 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4141 while (((Q < q_low) || (cpi->zbin_over_quant < zbin_oq_low)) && (Retries < 10))
4143 vp8_update_rate_correction_factors(cpi, 0);
4144 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4151 /* Frame is too small */
4154 if (cpi->zbin_over_quant == 0)
4155 /* Lower q_high if not using over quant */
4156 q_high = (Q > q_low) ? (Q - 1) : q_low;
4158 /* else lower zbin_oq_high */
4159 zbin_oq_high = (cpi->zbin_over_quant > zbin_oq_low) ? (cpi->zbin_over_quant - 1) : zbin_oq_low;
4163 /* Update rate_correction_factor unless
4164 * cpi->active_worst_quality has changed.
4166 if (!active_worst_qchanged)
4167 vp8_update_rate_correction_factors(cpi, 1);
4169 Q = (q_high + q_low) / 2;
4171 /* Adjust cpi->zbin_over_quant (only allowed when Q
4175 cpi->zbin_over_quant = 0;
4177 cpi->zbin_over_quant = (zbin_oq_high + zbin_oq_low) / 2;
4181 /* Update rate_correction_factor unless
4182 * cpi->active_worst_quality has changed.
4184 if (!active_worst_qchanged)
4185 vp8_update_rate_correction_factors(cpi, 0);
4187 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4189 /* Special case reset for qlow for constrained quality.
4190 * This should only trigger where there is very substantial
4191 * undershoot on a frame and the auto cq level is above
4192 * the user passsed in value.
4194 if ( (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) &&
4200 while (((Q > q_high) || (cpi->zbin_over_quant > zbin_oq_high)) && (Retries < 10))
4202 vp8_update_rate_correction_factors(cpi, 0);
4203 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4208 undershoot_seen = 1;
4211 /* Clamp Q to upper and lower limits: */
4217 /* Clamp cpi->zbin_over_quant */
4218 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;
4226 if (cpi->is_src_frame_alt_ref)
4231 vp8_restore_coding_context(cpi);
4233 #if CONFIG_INTERNAL_STATS
4234 cpi->tot_recode_hits++;
4241 /* Experimental code for lagged and one pass
4242 * Update stats used for one pass GF selection
4245 cpi->one_pass_frame_stats[cpi->one_pass_frame_index].frame_coded_error = (double)cpi->prediction_error;
4246 cpi->one_pass_frame_stats[cpi->one_pass_frame_index].frame_intra_error = (double)cpi->intra_error;
4247 cpi->one_pass_frame_stats[cpi->one_pass_frame_index].frame_pcnt_inter = (double)(100 - cpi->this_frame_percent_intra) / 100.0;
4251 /* Special case code to reduce pulsing when key frames are forced at a
4252 * fixed interval. Note the reconstruction error if it is the frame before
4253 * the force key frame
4255 if ( cpi->next_key_frame_forced && (cpi->twopass.frames_to_key == 0) )
4257 cpi->ambient_err = vp8_calc_ss_err(cpi->Source,
4258 &cm->yv12_fb[cm->new_fb_idx]);
4261 /* This frame's MVs are saved and will be used in next frame's MV predictor.
4262 * Last frame has one more line(add to bottom) and one more column(add to
4263 * right) than cm->mip. The edge elements are initialized to 0.
4265 #if CONFIG_MULTI_RES_ENCODING
4266 if(!cpi->oxcf.mr_encoder_id && cm->show_frame)
4268 if(cm->show_frame) /* do not save for altref frame */
4273 /* Point to beginning of allocated MODE_INFO arrays. */
4274 MODE_INFO *tmp = cm->mip;
4276 if(cm->frame_type != KEY_FRAME)
4278 for (mb_row = 0; mb_row < cm->mb_rows+1; mb_row ++)
4280 for (mb_col = 0; mb_col < cm->mb_cols+1; mb_col ++)
4282 if(tmp->mbmi.ref_frame != INTRA_FRAME)
4283 cpi->lfmv[mb_col + mb_row*(cm->mode_info_stride+1)].as_int = tmp->mbmi.mv.as_int;
4285 cpi->lf_ref_frame_sign_bias[mb_col + mb_row*(cm->mode_info_stride+1)] = cm->ref_frame_sign_bias[tmp->mbmi.ref_frame];
4286 cpi->lf_ref_frame[mb_col + mb_row*(cm->mode_info_stride+1)] = tmp->mbmi.ref_frame;
4293 /* Count last ref frame 0,0 usage on current encoded frame. */
4297 /* Point to beginning of MODE_INFO arrays. */
4298 MODE_INFO *tmp = cm->mi;
4300 cpi->inter_zz_count = 0;
4301 cpi->zeromv_count = 0;
4303 if(cm->frame_type != KEY_FRAME)
4305 for (mb_row = 0; mb_row < cm->mb_rows; mb_row ++)
4307 for (mb_col = 0; mb_col < cm->mb_cols; mb_col ++)
4309 if(tmp->mbmi.mode == ZEROMV && tmp->mbmi.ref_frame == LAST_FRAME)
4310 cpi->inter_zz_count++;
4311 if(tmp->mbmi.mode == ZEROMV)
4312 cpi->zeromv_count++;
4320 #if CONFIG_MULTI_RES_ENCODING
4321 vp8_cal_dissimilarity(cpi);
4324 /* Update the GF useage maps.
4325 * This is done after completing the compression of a frame when all
4326 * modes etc. are finalized but before loop filter
4328 if (cpi->oxcf.number_of_layers == 1)
4329 vp8_update_gf_useage_maps(cpi, cm, &cpi->mb);
4331 if (cm->frame_type == KEY_FRAME)
4332 cm->refresh_last_frame = 1;
4336 FILE *f = fopen("gfactive.stt", "a");
4337 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);
4342 /* For inter frames the current default behavior is that when
4343 * cm->refresh_golden_frame is set we copy the old GF over to the ARF buffer
4344 * This is purely an encoder decision at present.
4346 if (!cpi->oxcf.error_resilient_mode && cm->refresh_golden_frame)
4347 cm->copy_buffer_to_arf = 2;
4349 cm->copy_buffer_to_arf = 0;
4351 cm->frame_to_show = &cm->yv12_fb[cm->new_fb_idx];
4353 #if CONFIG_MULTITHREAD
4354 if (cpi->b_multi_threaded)
4356 /* start loopfilter in separate thread */
4357 sem_post(&cpi->h_event_start_lpf);
4358 cpi->b_lpf_running = 1;
4363 vp8_loopfilter_frame(cpi, cm);
4366 update_reference_frames(cpi);
4368 #if !(CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
4369 if (cpi->oxcf.error_resilient_mode)
4371 cm->refresh_entropy_probs = 0;
4375 #if CONFIG_MULTITHREAD
4376 /* wait that filter_level is picked so that we can continue with stream packing */
4377 if (cpi->b_multi_threaded)
4378 sem_wait(&cpi->h_event_end_lpf);
4381 /* build the bitstream */
4382 vp8_pack_bitstream(cpi, dest, dest_end, size);
4384 #if CONFIG_MULTITHREAD
4385 /* if PSNR packets are generated we have to wait for the lpf */
4386 if (cpi->b_lpf_running && cpi->b_calculate_psnr)
4388 sem_wait(&cpi->h_event_end_lpf);
4389 cpi->b_lpf_running = 0;
4393 /* Move storing frame_type out of the above loop since it is also
4394 * needed in motion search besides loopfilter */
4395 cm->last_frame_type = cm->frame_type;
4397 /* Update rate control heuristics */
4398 cpi->total_byte_count += (*size);
4399 cpi->projected_frame_size = (*size) << 3;
4401 if (cpi->oxcf.number_of_layers > 1)
4404 for (i=cpi->current_layer+1; i<cpi->oxcf.number_of_layers; i++)
4405 cpi->layer_context[i].total_byte_count += (*size);
4408 if (!active_worst_qchanged)
4409 vp8_update_rate_correction_factors(cpi, 2);
4411 cpi->last_q[cm->frame_type] = cm->base_qindex;
4413 if (cm->frame_type == KEY_FRAME)
4415 vp8_adjust_key_frame_context(cpi);
4418 /* Keep a record of ambient average Q. */
4419 if (cm->frame_type != KEY_FRAME)
4420 cpi->avg_frame_qindex = (2 + 3 * cpi->avg_frame_qindex + cm->base_qindex) >> 2;
4422 /* Keep a record from which we can calculate the average Q excluding
4423 * GF updates and key frames
4425 if ((cm->frame_type != KEY_FRAME) && ((cpi->oxcf.number_of_layers > 1) ||
4426 (!cm->refresh_golden_frame && !cm->refresh_alt_ref_frame)))
4430 /* Calculate the average Q for normal inter frames (not key or GFU
4433 if ( cpi->pass == 2 )
4435 cpi->ni_tot_qi += Q;
4436 cpi->ni_av_qi = (cpi->ni_tot_qi / cpi->ni_frames);
4440 /* Damp value for first few frames */
4441 if (cpi->ni_frames > 150 )
4443 cpi->ni_tot_qi += Q;
4444 cpi->ni_av_qi = (cpi->ni_tot_qi / cpi->ni_frames);
4446 /* For one pass, early in the clip ... average the current frame Q
4447 * value with the worstq entered by the user as a dampening measure
4451 cpi->ni_tot_qi += Q;
4452 cpi->ni_av_qi = ((cpi->ni_tot_qi / cpi->ni_frames) + cpi->worst_quality + 1) / 2;
4455 /* If the average Q is higher than what was used in the last
4456 * frame (after going through the recode loop to keep the frame
4457 * size within range) then use the last frame value - 1. The -1
4458 * is designed to stop Q and hence the data rate, from
4459 * progressively falling away during difficult sections, but at
4460 * the same time reduce the number of itterations around the
4463 if (Q > cpi->ni_av_qi)
4464 cpi->ni_av_qi = Q - 1;
4468 /* Update the buffer level variable. */
4469 /* Non-viewable frames are a special case and are treated as pure overhead. */
4470 if ( !cm->show_frame )
4471 cpi->bits_off_target -= cpi->projected_frame_size;
4473 cpi->bits_off_target += cpi->av_per_frame_bandwidth - cpi->projected_frame_size;
4475 /* Clip the buffer level to the maximum specified buffer size */
4476 if (cpi->bits_off_target > cpi->oxcf.maximum_buffer_size)
4477 cpi->bits_off_target = cpi->oxcf.maximum_buffer_size;
4479 /* Rolling monitors of whether we are over or underspending used to
4480 * help regulate min and Max Q in two pass.
4482 cpi->rolling_target_bits = ((cpi->rolling_target_bits * 3) + cpi->this_frame_target + 2) / 4;
4483 cpi->rolling_actual_bits = ((cpi->rolling_actual_bits * 3) + cpi->projected_frame_size + 2) / 4;
4484 cpi->long_rolling_target_bits = ((cpi->long_rolling_target_bits * 31) + cpi->this_frame_target + 16) / 32;
4485 cpi->long_rolling_actual_bits = ((cpi->long_rolling_actual_bits * 31) + cpi->projected_frame_size + 16) / 32;
4487 /* Actual bits spent */
4488 cpi->total_actual_bits += cpi->projected_frame_size;
4491 cpi->total_target_vs_actual += (cpi->this_frame_target - cpi->projected_frame_size);
4493 cpi->buffer_level = cpi->bits_off_target;
4495 /* Propagate values to higher temporal layers */
4496 if (cpi->oxcf.number_of_layers > 1)
4500 for (i=cpi->current_layer+1; i<cpi->oxcf.number_of_layers; i++)
4502 LAYER_CONTEXT *lc = &cpi->layer_context[i];
4503 int bits_off_for_this_layer =
4504 (int)(lc->target_bandwidth / lc->frame_rate -
4505 cpi->projected_frame_size);
4507 lc->bits_off_target += bits_off_for_this_layer;
4509 /* Clip buffer level to maximum buffer size for the layer */
4510 if (lc->bits_off_target > lc->maximum_buffer_size)
4511 lc->bits_off_target = lc->maximum_buffer_size;
4513 lc->total_actual_bits += cpi->projected_frame_size;
4514 lc->total_target_vs_actual += bits_off_for_this_layer;
4515 lc->buffer_level = lc->bits_off_target;
4519 /* Update bits left to the kf and gf groups to account for overshoot
4520 * or undershoot on these frames
4522 if (cm->frame_type == KEY_FRAME)
4524 cpi->twopass.kf_group_bits += cpi->this_frame_target - cpi->projected_frame_size;
4526 if (cpi->twopass.kf_group_bits < 0)
4527 cpi->twopass.kf_group_bits = 0 ;
4529 else if (cm->refresh_golden_frame || cm->refresh_alt_ref_frame)
4531 cpi->twopass.gf_group_bits += cpi->this_frame_target - cpi->projected_frame_size;
4533 if (cpi->twopass.gf_group_bits < 0)
4534 cpi->twopass.gf_group_bits = 0 ;
4537 if (cm->frame_type != KEY_FRAME)
4539 if (cpi->common.refresh_alt_ref_frame)
4541 cpi->last_skip_false_probs[2] = cpi->prob_skip_false;
4542 cpi->last_skip_probs_q[2] = cm->base_qindex;
4544 else if (cpi->common.refresh_golden_frame)
4546 cpi->last_skip_false_probs[1] = cpi->prob_skip_false;
4547 cpi->last_skip_probs_q[1] = cm->base_qindex;
4551 cpi->last_skip_false_probs[0] = cpi->prob_skip_false;
4552 cpi->last_skip_probs_q[0] = cm->base_qindex;
4554 /* update the baseline */
4555 cpi->base_skip_false_prob[cm->base_qindex] = cpi->prob_skip_false;
4560 #if 0 && CONFIG_INTERNAL_STATS
4562 FILE *f = fopen("tmp.stt", "a");
4564 vp8_clear_system_state();
4566 if (cpi->twopass.total_left_stats.coded_error != 0.0)
4567 fprintf(f, "%10d %10d %10d %10d %10d %10d %10d %10d %10d %6d %6d"
4568 "%6d %6d %6d %5d %5d %5d %8d %8.2f %10d %10.3f"
4570 cpi->common.current_video_frame, cpi->this_frame_target,
4571 cpi->projected_frame_size,
4572 (cpi->projected_frame_size - cpi->this_frame_target),
4573 (int)cpi->total_target_vs_actual,
4575 (cpi->oxcf.starting_buffer_level-cpi->bits_off_target),
4576 (int)cpi->total_actual_bits, cm->base_qindex,
4577 cpi->active_best_quality, cpi->active_worst_quality,
4578 cpi->ni_av_qi, cpi->cq_target_quality,
4579 cpi->zbin_over_quant,
4580 cm->refresh_golden_frame, cm->refresh_alt_ref_frame,
4581 cm->frame_type, cpi->gfu_boost,
4582 cpi->twopass.est_max_qcorrection_factor,
4583 (int)cpi->twopass.bits_left,
4584 cpi->twopass.total_left_stats.coded_error,
4585 (double)cpi->twopass.bits_left /
4586 cpi->twopass.total_left_stats.coded_error,
4587 cpi->tot_recode_hits);
4589 fprintf(f, "%10d %10d %10d %10d %10d %10d %10d %10d %10d %6d %6d"
4590 "%6d %6d %6d %5d %5d %5d %8d %8.2f %10d %10.3f"
4592 cpi->common.current_video_frame,
4593 cpi->this_frame_target, cpi->projected_frame_size,
4594 (cpi->projected_frame_size - cpi->this_frame_target),
4595 (int)cpi->total_target_vs_actual,
4597 (cpi->oxcf.starting_buffer_level-cpi->bits_off_target),
4598 (int)cpi->total_actual_bits, cm->base_qindex,
4599 cpi->active_best_quality, cpi->active_worst_quality,
4600 cpi->ni_av_qi, cpi->cq_target_quality,
4601 cpi->zbin_over_quant,
4602 cm->refresh_golden_frame, cm->refresh_alt_ref_frame,
4603 cm->frame_type, cpi->gfu_boost,
4604 cpi->twopass.est_max_qcorrection_factor,
4605 (int)cpi->twopass.bits_left,
4606 cpi->twopass.total_left_stats.coded_error,
4607 cpi->tot_recode_hits);
4612 FILE *fmodes = fopen("Modes.stt", "a");
4615 fprintf(fmodes, "%6d:%1d:%1d:%1d ",
4616 cpi->common.current_video_frame,
4617 cm->frame_type, cm->refresh_golden_frame,
4618 cm->refresh_alt_ref_frame);
4620 for (i = 0; i < MAX_MODES; i++)
4621 fprintf(fmodes, "%5d ", cpi->mode_chosen_counts[i]);
4623 fprintf(fmodes, "\n");
4631 if (cm->refresh_golden_frame == 1)
4632 cm->frame_flags = cm->frame_flags | FRAMEFLAGS_GOLDEN;
4634 cm->frame_flags = cm->frame_flags&~FRAMEFLAGS_GOLDEN;
4636 if (cm->refresh_alt_ref_frame == 1)
4637 cm->frame_flags = cm->frame_flags | FRAMEFLAGS_ALTREF;
4639 cm->frame_flags = cm->frame_flags&~FRAMEFLAGS_ALTREF;
4642 if (cm->refresh_last_frame & cm->refresh_golden_frame)
4643 /* both refreshed */
4644 cpi->gold_is_last = 1;
4645 else if (cm->refresh_last_frame ^ cm->refresh_golden_frame)
4646 /* 1 refreshed but not the other */
4647 cpi->gold_is_last = 0;
4649 if (cm->refresh_last_frame & cm->refresh_alt_ref_frame)
4650 /* both refreshed */
4651 cpi->alt_is_last = 1;
4652 else if (cm->refresh_last_frame ^ cm->refresh_alt_ref_frame)
4653 /* 1 refreshed but not the other */
4654 cpi->alt_is_last = 0;
4656 if (cm->refresh_alt_ref_frame & cm->refresh_golden_frame)
4657 /* both refreshed */
4658 cpi->gold_is_alt = 1;
4659 else if (cm->refresh_alt_ref_frame ^ cm->refresh_golden_frame)
4660 /* 1 refreshed but not the other */
4661 cpi->gold_is_alt = 0;
4663 cpi->ref_frame_flags = VP8_ALTR_FRAME | VP8_GOLD_FRAME | VP8_LAST_FRAME;
4665 if (cpi->gold_is_last)
4666 cpi->ref_frame_flags &= ~VP8_GOLD_FRAME;
4668 if (cpi->alt_is_last)
4669 cpi->ref_frame_flags &= ~VP8_ALTR_FRAME;
4671 if (cpi->gold_is_alt)
4672 cpi->ref_frame_flags &= ~VP8_ALTR_FRAME;
4675 if (!cpi->oxcf.error_resilient_mode)
4677 if (cpi->oxcf.play_alternate && cm->refresh_alt_ref_frame && (cm->frame_type != KEY_FRAME))
4678 /* Update the alternate reference frame stats as appropriate. */
4679 update_alt_ref_frame_stats(cpi);
4681 /* Update the Golden frame stats as appropriate. */
4682 update_golden_frame_stats(cpi);
4685 if (cm->frame_type == KEY_FRAME)
4687 /* Tell the caller that the frame was coded as a key frame */
4688 *frame_flags = cm->frame_flags | FRAMEFLAGS_KEY;
4690 /* As this frame is a key frame the next defaults to an inter frame. */
4691 cm->frame_type = INTER_FRAME;
4693 cpi->last_frame_percent_intra = 100;
4697 *frame_flags = cm->frame_flags&~FRAMEFLAGS_KEY;
4699 cpi->last_frame_percent_intra = cpi->this_frame_percent_intra;
4702 /* Clear the one shot update flags for segmentation map and mode/ref
4703 * loop filter deltas.
4705 cpi->mb.e_mbd.update_mb_segmentation_map = 0;
4706 cpi->mb.e_mbd.update_mb_segmentation_data = 0;
4707 cpi->mb.e_mbd.mode_ref_lf_delta_update = 0;
4710 /* Dont increment frame counters if this was an altref buffer update
4715 cm->current_video_frame++;
4716 cpi->frames_since_key++;
4719 /* reset to normal state now that we are done. */
4727 sprintf(filename, "enc%04d.yuv", (int) cm->current_video_frame);
4728 recon_file = fopen(filename, "wb");
4729 fwrite(cm->yv12_fb[cm->lst_fb_idx].buffer_alloc,
4730 cm->yv12_fb[cm->lst_fb_idx].frame_size, 1, recon_file);
4736 /* vp8_write_yuv_frame("encoder_recon.yuv", cm->frame_to_show); */
4742 static void check_gf_quality(VP8_COMP *cpi)
4744 VP8_COMMON *cm = &cpi->common;
4745 int gf_active_pct = (100 * cpi->gf_active_count) / (cm->mb_rows * cm->mb_cols);
4746 int gf_ref_usage_pct = (cpi->count_mb_ref_frame_usage[GOLDEN_FRAME] * 100) / (cm->mb_rows * cm->mb_cols);
4747 int last_ref_zz_useage = (cpi->inter_zz_count * 100) / (cm->mb_rows * cm->mb_cols);
4749 /* Gf refresh is not currently being signalled */
4750 if (cpi->gf_update_recommended == 0)
4752 if (cpi->common.frames_since_golden > 7)
4755 if ((gf_active_pct < 10) || ((gf_active_pct + gf_ref_usage_pct) < 15))
4757 /* ...but last frame zero zero usage is reasonbable so a
4758 * new gf might be appropriate
4760 if (last_ref_zz_useage >= 25)
4762 cpi->gf_bad_count ++;
4764 /* Check that the condition is stable */
4765 if (cpi->gf_bad_count >= 8)
4767 cpi->gf_update_recommended = 1;
4768 cpi->gf_bad_count = 0;
4772 /* Restart count as the background is not stable enough */
4773 cpi->gf_bad_count = 0;
4776 /* Gf useage has picked up so reset count */
4777 cpi->gf_bad_count = 0;
4780 /* If the signal is set but has not been read should we cancel it. */
4781 else if (last_ref_zz_useage < 15)
4783 cpi->gf_update_recommended = 0;
4784 cpi->gf_bad_count = 0;
4789 FILE *f = fopen("gfneeded.stt", "a");
4790 fprintf(f, "%10d %10d %10d %10d %10ld \n",
4791 cm->current_video_frame,
4792 cpi->common.frames_since_golden,
4793 gf_active_pct, gf_ref_usage_pct,
4794 cpi->gf_update_recommended);
4801 #if !(CONFIG_REALTIME_ONLY)
4802 static void Pass2Encode(VP8_COMP *cpi, unsigned long *size, unsigned char *dest, unsigned char * dest_end, unsigned int *frame_flags)
4805 if (!cpi->common.refresh_alt_ref_frame)
4806 vp8_second_pass(cpi);
4808 encode_frame_to_data_rate(cpi, size, dest, dest_end, frame_flags);
4809 cpi->twopass.bits_left -= 8 * *size;
4811 if (!cpi->common.refresh_alt_ref_frame)
4813 double two_pass_min_rate = (double)(cpi->oxcf.target_bandwidth
4814 *cpi->oxcf.two_pass_vbrmin_section / 100);
4815 cpi->twopass.bits_left += (int64_t)(two_pass_min_rate / cpi->frame_rate);
4820 /* For ARM NEON, d8-d15 are callee-saved registers, and need to be saved. */
4822 extern void vp8_push_neon(int64_t *store);
4823 extern void vp8_pop_neon(int64_t *store);
4827 int vp8_receive_raw_frame(VP8_COMP *cpi, unsigned int frame_flags, YV12_BUFFER_CONFIG *sd, int64_t time_stamp, int64_t end_time)
4830 int64_t store_reg[8];
4832 VP8_COMMON *cm = &cpi->common;
4833 struct vpx_usec_timer timer;
4837 #if CONFIG_RUNTIME_CPU_DETECT
4838 if (cm->cpu_caps & HAS_NEON)
4841 vp8_push_neon(store_reg);
4845 vpx_usec_timer_start(&timer);
4847 /* Reinit the lookahead buffer if the frame size changes */
4848 if (sd->y_width != cpi->oxcf.Width || sd->y_height != cpi->oxcf.Height)
4850 assert(cpi->oxcf.lag_in_frames < 2);
4851 dealloc_raw_frame_buffers(cpi);
4852 alloc_raw_frame_buffers(cpi);
4855 if(vp8_lookahead_push(cpi->lookahead, sd, time_stamp, end_time,
4856 frame_flags, cpi->active_map_enabled ? cpi->active_map : NULL))
4858 cm->clr_type = sd->clrtype;
4859 vpx_usec_timer_mark(&timer);
4860 cpi->time_receive_data += vpx_usec_timer_elapsed(&timer);
4863 #if CONFIG_RUNTIME_CPU_DETECT
4864 if (cm->cpu_caps & HAS_NEON)
4867 vp8_pop_neon(store_reg);
4875 static int frame_is_reference(const VP8_COMP *cpi)
4877 const VP8_COMMON *cm = &cpi->common;
4878 const MACROBLOCKD *xd = &cpi->mb.e_mbd;
4880 return cm->frame_type == KEY_FRAME || cm->refresh_last_frame
4881 || cm->refresh_golden_frame || cm->refresh_alt_ref_frame
4882 || cm->copy_buffer_to_gf || cm->copy_buffer_to_arf
4883 || cm->refresh_entropy_probs
4884 || xd->mode_ref_lf_delta_update
4885 || xd->update_mb_segmentation_map || xd->update_mb_segmentation_data;
4889 int vp8_get_compressed_data(VP8_COMP *cpi, unsigned int *frame_flags, unsigned long *size, unsigned char *dest, unsigned char *dest_end, int64_t *time_stamp, int64_t *time_end, int flush)
4892 int64_t store_reg[8];
4895 struct vpx_usec_timer tsctimer;
4896 struct vpx_usec_timer ticktimer;
4897 struct vpx_usec_timer cmptimer;
4898 YV12_BUFFER_CONFIG *force_src_buffer = NULL;
4905 if (setjmp(cpi->common.error.jmp))
4907 cpi->common.error.setjmp = 0;
4908 return VPX_CODEC_CORRUPT_FRAME;
4911 cpi->common.error.setjmp = 1;
4914 #if CONFIG_RUNTIME_CPU_DETECT
4915 if (cm->cpu_caps & HAS_NEON)
4918 vp8_push_neon(store_reg);
4922 vpx_usec_timer_start(&cmptimer);
4926 #if !(CONFIG_REALTIME_ONLY)
4927 /* Should we code an alternate reference frame */
4928 if (cpi->oxcf.error_resilient_mode == 0 &&
4929 cpi->oxcf.play_alternate &&
4930 cpi->source_alt_ref_pending)
4932 if ((cpi->source = vp8_lookahead_peek(cpi->lookahead,
4933 cpi->frames_till_gf_update_due,
4936 cpi->alt_ref_source = cpi->source;
4937 if (cpi->oxcf.arnr_max_frames > 0)
4939 vp8_temporal_filter_prepare_c(cpi,
4940 cpi->frames_till_gf_update_due);
4941 force_src_buffer = &cpi->alt_ref_buffer;
4943 cm->frames_till_alt_ref_frame = cpi->frames_till_gf_update_due;
4944 cm->refresh_alt_ref_frame = 1;
4945 cm->refresh_golden_frame = 0;
4946 cm->refresh_last_frame = 0;
4948 /* Clear Pending alt Ref flag. */
4949 cpi->source_alt_ref_pending = 0;
4950 cpi->is_src_frame_alt_ref = 0;
4957 /* Read last frame source if we are encoding first pass. */
4958 if (cpi->pass == 1 && cm->current_video_frame > 0)
4960 if((cpi->last_source = vp8_lookahead_peek(cpi->lookahead, 1,
4961 PEEK_BACKWARD)) == NULL)
4966 if ((cpi->source = vp8_lookahead_pop(cpi->lookahead, flush)))
4970 cpi->is_src_frame_alt_ref = cpi->alt_ref_source
4971 && (cpi->source == cpi->alt_ref_source);
4973 if(cpi->is_src_frame_alt_ref)
4974 cpi->alt_ref_source = NULL;
4980 cpi->Source = force_src_buffer ? force_src_buffer : &cpi->source->img;
4981 cpi->un_scaled_source = cpi->Source;
4982 *time_stamp = cpi->source->ts_start;
4983 *time_end = cpi->source->ts_end;
4984 *frame_flags = cpi->source->flags;
4986 if (cpi->pass == 1 && cm->current_video_frame > 0)
4988 cpi->last_frame_unscaled_source = &cpi->last_source->img;
4994 #if !(CONFIG_REALTIME_ONLY)
4996 if (flush && cpi->pass == 1 && !cpi->twopass.first_pass_done)
4998 vp8_end_first_pass(cpi); /* get last stats packet */
4999 cpi->twopass.first_pass_done = 1;
5005 #if CONFIG_RUNTIME_CPU_DETECT
5006 if (cm->cpu_caps & HAS_NEON)
5009 vp8_pop_neon(store_reg);
5015 if (cpi->source->ts_start < cpi->first_time_stamp_ever)
5017 cpi->first_time_stamp_ever = cpi->source->ts_start;
5018 cpi->last_end_time_stamp_seen = cpi->source->ts_start;
5021 /* adjust frame rates based on timestamps given */
5024 int64_t this_duration;
5027 if (cpi->source->ts_start == cpi->first_time_stamp_ever)
5029 this_duration = cpi->source->ts_end - cpi->source->ts_start;
5034 int64_t last_duration;
5036 this_duration = cpi->source->ts_end - cpi->last_end_time_stamp_seen;
5037 last_duration = cpi->last_end_time_stamp_seen
5038 - cpi->last_time_stamp_seen;
5039 /* do a step update if the duration changes by 10% */
5041 step = (int)(((this_duration - last_duration) *
5042 10 / last_duration));
5048 cpi->ref_frame_rate = 10000000.0 / this_duration;
5051 double avg_duration, interval;
5053 /* Average this frame's rate into the last second's average
5054 * frame rate. If we haven't seen 1 second yet, then average
5055 * over the whole interval seen.
5057 interval = (double)(cpi->source->ts_end -
5058 cpi->first_time_stamp_ever);
5059 if(interval > 10000000.0)
5060 interval = 10000000;
5062 avg_duration = 10000000.0 / cpi->ref_frame_rate;
5063 avg_duration *= (interval - avg_duration + this_duration);
5064 avg_duration /= interval;
5066 cpi->ref_frame_rate = 10000000.0 / avg_duration;
5069 if (cpi->oxcf.number_of_layers > 1)
5073 /* Update frame rates for each layer */
5074 for (i=0; i<cpi->oxcf.number_of_layers; i++)
5076 LAYER_CONTEXT *lc = &cpi->layer_context[i];
5077 lc->frame_rate = cpi->ref_frame_rate /
5078 cpi->oxcf.rate_decimator[i];
5082 vp8_new_frame_rate(cpi, cpi->ref_frame_rate);
5085 cpi->last_time_stamp_seen = cpi->source->ts_start;
5086 cpi->last_end_time_stamp_seen = cpi->source->ts_end;
5089 if (cpi->oxcf.number_of_layers > 1)
5093 update_layer_contexts (cpi);
5095 /* Restore layer specific context & set frame rate */
5096 layer = cpi->oxcf.layer_id[
5097 cm->current_video_frame % cpi->oxcf.periodicity];
5098 restore_layer_context (cpi, layer);
5099 vp8_new_frame_rate (cpi, cpi->layer_context[layer].frame_rate);
5102 if (cpi->compressor_speed == 2)
5104 if (cpi->oxcf.number_of_layers == 1)
5105 check_gf_quality(cpi);
5106 vpx_usec_timer_start(&tsctimer);
5107 vpx_usec_timer_start(&ticktimer);
5110 cpi->lf_zeromv_pct = (cpi->zeromv_count * 100)/cm->MBs;
5112 #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
5115 const int num_part = (1 << cm->multi_token_partition);
5116 /* the available bytes in dest */
5117 const unsigned long dest_size = dest_end - dest;
5118 const int tok_part_buff_size = (dest_size * 9) / (10 * num_part);
5120 unsigned char *dp = dest;
5122 cpi->partition_d[0] = dp;
5123 dp += dest_size/10; /* reserve 1/10 for control partition */
5124 cpi->partition_d_end[0] = dp;
5126 for(i = 0; i < num_part; i++)
5128 cpi->partition_d[i + 1] = dp;
5129 dp += tok_part_buff_size;
5130 cpi->partition_d_end[i + 1] = dp;
5135 /* start with a 0 size frame */
5138 /* Clear down mmx registers */
5139 vp8_clear_system_state();
5141 cm->frame_type = INTER_FRAME;
5142 cm->frame_flags = *frame_flags;
5146 if (cm->refresh_alt_ref_frame)
5148 cm->refresh_golden_frame = 0;
5149 cm->refresh_last_frame = 0;
5153 cm->refresh_golden_frame = 0;
5154 cm->refresh_last_frame = 1;
5158 /* find a free buffer for the new frame */
5161 for(; i < NUM_YV12_BUFFERS; i++)
5163 if(!cm->yv12_fb[i].flags)
5170 assert(i < NUM_YV12_BUFFERS );
5172 #if !(CONFIG_REALTIME_ONLY)
5176 Pass1Encode(cpi, size, dest, frame_flags);
5178 else if (cpi->pass == 2)
5180 Pass2Encode(cpi, size, dest, dest_end, frame_flags);
5184 encode_frame_to_data_rate(cpi, size, dest, dest_end, frame_flags);
5186 if (cpi->compressor_speed == 2)
5188 unsigned int duration, duration2;
5189 vpx_usec_timer_mark(&tsctimer);
5190 vpx_usec_timer_mark(&ticktimer);
5192 duration = (int)(vpx_usec_timer_elapsed(&ticktimer));
5193 duration2 = (unsigned int)((double)duration / 2);
5195 if (cm->frame_type != KEY_FRAME)
5197 if (cpi->avg_encode_time == 0)
5198 cpi->avg_encode_time = duration;
5200 cpi->avg_encode_time = (7 * cpi->avg_encode_time + duration) >> 3;
5207 if (cpi->avg_pick_mode_time == 0)
5208 cpi->avg_pick_mode_time = duration2;
5210 cpi->avg_pick_mode_time = (7 * cpi->avg_pick_mode_time + duration2) >> 3;
5216 if (cm->refresh_entropy_probs == 0)
5218 vpx_memcpy(&cm->fc, &cm->lfc, sizeof(cm->fc));
5221 /* Save the contexts separately for alt ref, gold and last. */
5222 /* (TODO jbb -> Optimize this with pointers to avoid extra copies. ) */
5223 if(cm->refresh_alt_ref_frame)
5224 vpx_memcpy(&cpi->lfc_a, &cm->fc, sizeof(cm->fc));
5226 if(cm->refresh_golden_frame)
5227 vpx_memcpy(&cpi->lfc_g, &cm->fc, sizeof(cm->fc));
5229 if(cm->refresh_last_frame)
5230 vpx_memcpy(&cpi->lfc_n, &cm->fc, sizeof(cm->fc));
5232 /* if its a dropped frame honor the requests on subsequent frames */
5235 cpi->droppable = !frame_is_reference(cpi);
5237 /* return to normal state */
5238 cm->refresh_entropy_probs = 1;
5239 cm->refresh_alt_ref_frame = 0;
5240 cm->refresh_golden_frame = 0;
5241 cm->refresh_last_frame = 1;
5242 cm->frame_type = INTER_FRAME;
5246 /* Save layer specific state */
5247 if (cpi->oxcf.number_of_layers > 1)
5248 save_layer_context (cpi);
5250 vpx_usec_timer_mark(&cmptimer);
5251 cpi->time_compress_data += vpx_usec_timer_elapsed(&cmptimer);
5253 if (cpi->b_calculate_psnr && cpi->pass != 1 && cm->show_frame)
5255 generate_psnr_packet(cpi);
5258 #if CONFIG_INTERNAL_STATS
5262 cpi->bytes += *size;
5269 if (cpi->b_calculate_psnr)
5273 YV12_BUFFER_CONFIG *orig = cpi->Source;
5274 YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
5275 int y_samples = orig->y_height * orig->y_width ;
5276 int uv_samples = orig->uv_height * orig->uv_width ;
5277 int t_samples = y_samples + 2 * uv_samples;
5278 double sq_error, sq_error2;
5280 ye = calc_plane_error(orig->y_buffer, orig->y_stride,
5281 recon->y_buffer, recon->y_stride, orig->y_width, orig->y_height);
5283 ue = calc_plane_error(orig->u_buffer, orig->uv_stride,
5284 recon->u_buffer, recon->uv_stride, orig->uv_width, orig->uv_height);
5286 ve = calc_plane_error(orig->v_buffer, orig->uv_stride,
5287 recon->v_buffer, recon->uv_stride, orig->uv_width, orig->uv_height);
5289 sq_error = (double)(ye + ue + ve);
5291 frame_psnr = vp8_mse2psnr(t_samples, 255.0, sq_error);
5293 cpi->total_y += vp8_mse2psnr(y_samples, 255.0, (double)ye);
5294 cpi->total_u += vp8_mse2psnr(uv_samples, 255.0, (double)ue);
5295 cpi->total_v += vp8_mse2psnr(uv_samples, 255.0, (double)ve);
5296 cpi->total_sq_error += sq_error;
5297 cpi->total += frame_psnr;
5300 YV12_BUFFER_CONFIG *pp = &cm->post_proc_buffer;
5301 double frame_psnr2, frame_ssim2 = 0;
5304 vp8_deblock(cm->frame_to_show, &cm->post_proc_buffer, cm->filter_level * 10 / 6, 1, 0);
5305 vp8_clear_system_state();
5307 ye = calc_plane_error(orig->y_buffer, orig->y_stride,
5308 pp->y_buffer, pp->y_stride, orig->y_width, orig->y_height);
5310 ue = calc_plane_error(orig->u_buffer, orig->uv_stride,
5311 pp->u_buffer, pp->uv_stride, orig->uv_width, orig->uv_height);
5313 ve = calc_plane_error(orig->v_buffer, orig->uv_stride,
5314 pp->v_buffer, pp->uv_stride, orig->uv_width, orig->uv_height);
5316 sq_error2 = (double)(ye + ue + ve);
5318 frame_psnr2 = vp8_mse2psnr(t_samples, 255.0, sq_error2);
5320 cpi->totalp_y += vp8_mse2psnr(y_samples,
5322 cpi->totalp_u += vp8_mse2psnr(uv_samples,
5324 cpi->totalp_v += vp8_mse2psnr(uv_samples,
5326 cpi->total_sq_error2 += sq_error2;
5327 cpi->totalp += frame_psnr2;
5329 frame_ssim2 = vp8_calc_ssim(cpi->Source,
5330 &cm->post_proc_buffer, 1, &weight);
5332 cpi->summed_quality += frame_ssim2 * weight;
5333 cpi->summed_weights += weight;
5335 if (cpi->oxcf.number_of_layers > 1)
5339 for (i=cpi->current_layer;
5340 i<cpi->oxcf.number_of_layers; i++)
5342 cpi->frames_in_layer[i]++;
5344 cpi->bytes_in_layer[i] += *size;
5345 cpi->sum_psnr[i] += frame_psnr;
5346 cpi->sum_psnr_p[i] += frame_psnr2;
5347 cpi->total_error2[i] += sq_error;
5348 cpi->total_error2_p[i] += sq_error2;
5349 cpi->sum_ssim[i] += frame_ssim2 * weight;
5350 cpi->sum_weights[i] += weight;
5357 if (cpi->b_calculate_ssimg)
5359 double y, u, v, frame_all;
5360 frame_all = vp8_calc_ssimg(cpi->Source, cm->frame_to_show,
5363 if (cpi->oxcf.number_of_layers > 1)
5367 for (i=cpi->current_layer;
5368 i<cpi->oxcf.number_of_layers; i++)
5370 if (!cpi->b_calculate_psnr)
5371 cpi->frames_in_layer[i]++;
5373 cpi->total_ssimg_y_in_layer[i] += y;
5374 cpi->total_ssimg_u_in_layer[i] += u;
5375 cpi->total_ssimg_v_in_layer[i] += v;
5376 cpi->total_ssimg_all_in_layer[i] += frame_all;
5381 cpi->total_ssimg_y += y;
5382 cpi->total_ssimg_u += u;
5383 cpi->total_ssimg_v += v;
5384 cpi->total_ssimg_all += frame_all;
5393 if (cpi->common.frame_type != 0 && cpi->common.base_qindex == cpi->oxcf.worst_allowed_q)
5395 skiptruecount += cpi->skip_true_count;
5396 skipfalsecount += cpi->skip_false_count;
5404 FILE *f = fopen("skip.stt", "a");
5405 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);
5407 if (cpi->is_src_frame_alt_ref == 1)
5408 fprintf(f, "skipcount: %4d framesize: %d\n", cpi->skip_true_count , *size);
5417 #if CONFIG_RUNTIME_CPU_DETECT
5418 if (cm->cpu_caps & HAS_NEON)
5421 vp8_pop_neon(store_reg);
5425 cpi->common.error.setjmp = 0;
5430 int vp8_get_preview_raw_frame(VP8_COMP *cpi, YV12_BUFFER_CONFIG *dest, vp8_ppflags_t *flags)
5432 if (cpi->common.refresh_alt_ref_frame)
5438 #if CONFIG_MULTITHREAD
5439 if(cpi->b_lpf_running)
5441 sem_wait(&cpi->h_event_end_lpf);
5442 cpi->b_lpf_running = 0;
5447 ret = vp8_post_proc_frame(&cpi->common, dest, flags);
5450 if (cpi->common.frame_to_show)
5452 *dest = *cpi->common.frame_to_show;
5453 dest->y_width = cpi->common.Width;
5454 dest->y_height = cpi->common.Height;
5455 dest->uv_height = cpi->common.Height / 2;
5464 vp8_clear_system_state();
5469 int vp8_set_roimap(VP8_COMP *cpi, unsigned char *map, unsigned int rows, unsigned int cols, int delta_q[4], int delta_lf[4], unsigned int threshold[4])
5471 signed char feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS];
5472 int internal_delta_q[MAX_MB_SEGMENTS];
5473 const int range = 63;
5476 // This method is currently incompatible with the cyclic refresh method
5477 if ( cpi->cyclic_refresh_mode_enabled )
5480 // Check number of rows and columns match
5481 if (cpi->common.mb_rows != rows || cpi->common.mb_cols != cols)
5484 // Range check the delta Q values and convert the external Q range values
5485 // to internal ones.
5486 if ( (abs(delta_q[0]) > range) || (abs(delta_q[1]) > range) ||
5487 (abs(delta_q[2]) > range) || (abs(delta_q[3]) > range) )
5490 // Range check the delta lf values
5491 if ( (abs(delta_lf[0]) > range) || (abs(delta_lf[1]) > range) ||
5492 (abs(delta_lf[2]) > range) || (abs(delta_lf[3]) > range) )
5497 disable_segmentation(cpi);
5501 // Translate the external delta q values to internal values.
5502 for ( i = 0; i < MAX_MB_SEGMENTS; i++ )
5503 internal_delta_q[i] =
5504 ( delta_q[i] >= 0 ) ? q_trans[delta_q[i]] : -q_trans[-delta_q[i]];
5506 /* Set the segmentation Map */
5507 set_segmentation_map(cpi, map);
5509 /* Activate segmentation. */
5510 enable_segmentation(cpi);
5512 /* Set up the quant segment data */
5513 feature_data[MB_LVL_ALT_Q][0] = internal_delta_q[0];
5514 feature_data[MB_LVL_ALT_Q][1] = internal_delta_q[1];
5515 feature_data[MB_LVL_ALT_Q][2] = internal_delta_q[2];
5516 feature_data[MB_LVL_ALT_Q][3] = internal_delta_q[3];
5518 /* Set up the loop segment data s */
5519 feature_data[MB_LVL_ALT_LF][0] = delta_lf[0];
5520 feature_data[MB_LVL_ALT_LF][1] = delta_lf[1];
5521 feature_data[MB_LVL_ALT_LF][2] = delta_lf[2];
5522 feature_data[MB_LVL_ALT_LF][3] = delta_lf[3];
5524 cpi->segment_encode_breakout[0] = threshold[0];
5525 cpi->segment_encode_breakout[1] = threshold[1];
5526 cpi->segment_encode_breakout[2] = threshold[2];
5527 cpi->segment_encode_breakout[3] = threshold[3];
5529 /* Initialise the feature data structure */
5530 set_segment_data(cpi, &feature_data[0][0], SEGMENT_DELTADATA);
5535 int vp8_set_active_map(VP8_COMP *cpi, unsigned char *map, unsigned int rows, unsigned int cols)
5537 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols)
5541 vpx_memcpy(cpi->active_map, map, rows * cols);
5542 cpi->active_map_enabled = 1;
5545 cpi->active_map_enabled = 0;
5555 int vp8_set_internal_size(VP8_COMP *cpi, VPX_SCALING horiz_mode, VPX_SCALING vert_mode)
5557 if (horiz_mode <= ONETWO)
5558 cpi->common.horiz_scale = horiz_mode;
5562 if (vert_mode <= ONETWO)
5563 cpi->common.vert_scale = vert_mode;
5572 int vp8_calc_ss_err(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *dest)
5577 unsigned char *src = source->y_buffer;
5578 unsigned char *dst = dest->y_buffer;
5580 /* Loop through the Y plane raw and reconstruction data summing
5581 * (square differences)
5583 for (i = 0; i < source->y_height; i += 16)
5585 for (j = 0; j < source->y_width; j += 16)
5588 Total += vp8_mse16x16(src + j, source->y_stride, dst + j, dest->y_stride, &sse);
5591 src += 16 * source->y_stride;
5592 dst += 16 * dest->y_stride;
5599 int vp8_get_quantizer(VP8_COMP *cpi)
5601 return cpi->common.base_qindex;