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"
15 #include "vp8/common/systemdependent.h"
17 #include "vp8/common/alloccommon.h"
19 #include "firstpass.h"
21 #include "vpx_scale/vpxscale.h"
22 #include "vp8/common/extend.h"
24 #include "vp8/common/quant_common.h"
25 #include "segmentation.h"
27 #include "vp8/common/postproc.h"
29 #include "vpx_mem/vpx_mem.h"
30 #include "vp8/common/swapyv12buffer.h"
31 #include "vp8/common/threading.h"
32 #include "vpx_ports/vpx_timer.h"
34 #include "vpx_ports/arm.h"
36 #if CONFIG_MULTI_RES_ENCODING
37 #include "mr_dissim.h"
39 #include "encodeframe.h"
45 #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
46 extern int vp8_update_coef_context(VP8_COMP *cpi);
47 extern void vp8_update_coef_probs(VP8_COMP *cpi);
50 extern void vp8cx_pick_filter_level_fast(YV12_BUFFER_CONFIG *sd, VP8_COMP *cpi);
51 extern void vp8cx_set_alt_lf_level(VP8_COMP *cpi, int filt_val);
52 extern void vp8cx_pick_filter_level(YV12_BUFFER_CONFIG *sd, VP8_COMP *cpi);
54 extern void vp8_deblock_frame(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *post, int filt_lvl, int low_var_thresh, int flag);
55 extern void print_parms(VP8_CONFIG *ocf, char *filenam);
56 extern unsigned int vp8_get_processor_freq();
57 extern void print_tree_update_probs();
58 extern void vp8cx_create_encoder_threads(VP8_COMP *cpi);
59 extern void vp8cx_remove_encoder_threads(VP8_COMP *cpi);
61 int vp8_estimate_entropy_savings(VP8_COMP *cpi);
63 int vp8_calc_ss_err(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *dest);
65 extern void vp8_temporal_filter_prepare_c(VP8_COMP *cpi, int distance);
67 static void set_default_lf_deltas(VP8_COMP *cpi);
69 extern const int vp8_gf_interval_table[101];
71 #if CONFIG_INTERNAL_STATS
74 extern double vp8_calc_ssim
76 YV12_BUFFER_CONFIG *source,
77 YV12_BUFFER_CONFIG *dest,
83 extern double vp8_calc_ssimg
85 YV12_BUFFER_CONFIG *source,
86 YV12_BUFFER_CONFIG *dest,
107 extern int skip_true_count;
108 extern int skip_false_count;
113 extern int intra_mode_stats[10][10][10];
117 unsigned int frames_at_speed[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
118 unsigned int tot_pm = 0;
119 unsigned int cnt_pm = 0;
120 unsigned int tot_ef = 0;
121 unsigned int cnt_ef = 0;
125 extern unsigned __int64 Sectionbits[50];
126 extern int y_modes[5] ;
127 extern int uv_modes[4] ;
128 extern int b_modes[10] ;
130 extern int inter_y_modes[10] ;
131 extern int inter_uv_modes[4] ;
132 extern unsigned int inter_b_modes[15];
135 extern const int vp8_bits_per_mb[2][QINDEX_RANGE];
137 extern const int qrounding_factors[129];
138 extern const int qzbin_factors[129];
139 extern void vp8cx_init_quantizer(VP8_COMP *cpi);
140 extern const int vp8cx_base_skip_false_prob[128];
142 /* Tables relating active max Q to active min Q */
143 static const unsigned char kf_low_motion_minq[QINDEX_RANGE] =
145 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
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,1,1,1,1,1,1,1,1,2,2,2,2,
149 3,3,3,3,3,3,4,4,4,5,5,5,5,5,6,6,
150 6,6,7,7,8,8,8,8,9,9,10,10,10,10,11,11,
151 11,11,12,12,13,13,13,13,14,14,15,15,15,15,16,16,
152 16,16,17,17,18,18,18,18,19,20,20,21,21,22,23,23
154 static const unsigned char kf_high_motion_minq[QINDEX_RANGE] =
156 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
157 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
158 1,1,1,1,1,1,1,1,2,2,2,2,3,3,3,3,
159 3,3,3,3,4,4,4,4,5,5,5,5,5,5,6,6,
160 6,6,7,7,8,8,8,8,9,9,10,10,10,10,11,11,
161 11,11,12,12,13,13,13,13,14,14,15,15,15,15,16,16,
162 16,16,17,17,18,18,18,18,19,19,20,20,20,20,21,21,
163 21,21,22,22,23,23,24,25,25,26,26,27,28,28,29,30
165 static const unsigned char gf_low_motion_minq[QINDEX_RANGE] =
167 0,0,0,0,1,1,1,1,1,1,1,1,2,2,2,2,
168 3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,
169 7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10,
170 11,11,12,12,13,13,14,14,15,15,16,16,17,17,18,18,
171 19,19,20,20,21,21,22,22,23,23,24,24,25,25,26,26,
172 27,27,28,28,29,29,30,30,31,31,32,32,33,33,34,34,
173 35,35,36,36,37,37,38,38,39,39,40,40,41,41,42,42,
174 43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58
176 static const unsigned char gf_mid_motion_minq[QINDEX_RANGE] =
178 0,0,0,0,1,1,1,1,1,1,2,2,3,3,3,4,
179 4,4,5,5,5,6,6,6,7,7,7,8,8,8,9,9,
180 9,10,10,10,10,11,11,11,12,12,12,12,13,13,13,14,
181 14,14,15,15,16,16,17,17,18,18,19,19,20,20,21,21,
182 22,22,23,23,24,24,25,25,26,26,27,27,28,28,29,29,
183 30,30,31,31,32,32,33,33,34,34,35,35,36,36,37,37,
184 38,39,39,40,40,41,41,42,42,43,43,44,45,46,47,48,
185 49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64
187 static const unsigned char gf_high_motion_minq[QINDEX_RANGE] =
189 0,0,0,0,1,1,1,1,1,2,2,2,3,3,3,4,
190 4,4,5,5,5,6,6,6,7,7,7,8,8,8,9,9,
191 9,10,10,10,11,11,12,12,13,13,14,14,15,15,16,16,
192 17,17,18,18,19,19,20,20,21,21,22,22,23,23,24,24,
193 25,25,26,26,27,27,28,28,29,29,30,30,31,31,32,32,
194 33,33,34,34,35,35,36,36,37,37,38,38,39,39,40,40,
195 41,41,42,42,43,44,45,46,47,48,49,50,51,52,53,54,
196 55,56,57,58,59,60,62,64,66,68,70,72,74,76,78,80
198 static const unsigned char inter_minq[QINDEX_RANGE] =
200 0,0,1,1,2,3,3,4,4,5,6,6,7,8,8,9,
201 9,10,11,11,12,13,13,14,15,15,16,17,17,18,19,20,
202 20,21,22,22,23,24,24,25,26,27,27,28,29,30,30,31,
203 32,33,33,34,35,36,36,37,38,39,39,40,41,42,42,43,
204 44,45,46,46,47,48,49,50,50,51,52,53,54,55,55,56,
205 57,58,59,60,60,61,62,63,64,65,66,67,67,68,69,70,
206 71,72,73,74,75,75,76,77,78,79,80,81,82,83,84,85,
207 86,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100
210 #ifdef PACKET_TESTING
211 extern FILE *vpxlogc;
214 static void save_layer_context(VP8_COMP *cpi)
216 LAYER_CONTEXT *lc = &cpi->layer_context[cpi->current_layer];
218 /* Save layer dependent coding state */
219 lc->target_bandwidth = cpi->target_bandwidth;
220 lc->starting_buffer_level = cpi->oxcf.starting_buffer_level;
221 lc->optimal_buffer_level = cpi->oxcf.optimal_buffer_level;
222 lc->maximum_buffer_size = cpi->oxcf.maximum_buffer_size;
223 lc->starting_buffer_level_in_ms = cpi->oxcf.starting_buffer_level_in_ms;
224 lc->optimal_buffer_level_in_ms = cpi->oxcf.optimal_buffer_level_in_ms;
225 lc->maximum_buffer_size_in_ms = cpi->oxcf.maximum_buffer_size_in_ms;
226 lc->buffer_level = cpi->buffer_level;
227 lc->bits_off_target = cpi->bits_off_target;
228 lc->total_actual_bits = cpi->total_actual_bits;
229 lc->worst_quality = cpi->worst_quality;
230 lc->active_worst_quality = cpi->active_worst_quality;
231 lc->best_quality = cpi->best_quality;
232 lc->active_best_quality = cpi->active_best_quality;
233 lc->ni_av_qi = cpi->ni_av_qi;
234 lc->ni_tot_qi = cpi->ni_tot_qi;
235 lc->ni_frames = cpi->ni_frames;
236 lc->avg_frame_qindex = cpi->avg_frame_qindex;
237 lc->rate_correction_factor = cpi->rate_correction_factor;
238 lc->key_frame_rate_correction_factor = cpi->key_frame_rate_correction_factor;
239 lc->gf_rate_correction_factor = cpi->gf_rate_correction_factor;
240 lc->zbin_over_quant = cpi->zbin_over_quant;
241 lc->inter_frame_target = cpi->inter_frame_target;
242 lc->total_byte_count = cpi->total_byte_count;
243 lc->filter_level = cpi->common.filter_level;
245 lc->last_frame_percent_intra = cpi->last_frame_percent_intra;
247 memcpy (lc->count_mb_ref_frame_usage,
248 cpi->count_mb_ref_frame_usage,
249 sizeof(cpi->count_mb_ref_frame_usage));
252 static void restore_layer_context(VP8_COMP *cpi, const int layer)
254 LAYER_CONTEXT *lc = &cpi->layer_context[layer];
256 /* Restore layer dependent coding state */
257 cpi->current_layer = layer;
258 cpi->target_bandwidth = lc->target_bandwidth;
259 cpi->oxcf.target_bandwidth = lc->target_bandwidth;
260 cpi->oxcf.starting_buffer_level = lc->starting_buffer_level;
261 cpi->oxcf.optimal_buffer_level = lc->optimal_buffer_level;
262 cpi->oxcf.maximum_buffer_size = lc->maximum_buffer_size;
263 cpi->oxcf.starting_buffer_level_in_ms = lc->starting_buffer_level_in_ms;
264 cpi->oxcf.optimal_buffer_level_in_ms = lc->optimal_buffer_level_in_ms;
265 cpi->oxcf.maximum_buffer_size_in_ms = lc->maximum_buffer_size_in_ms;
266 cpi->buffer_level = lc->buffer_level;
267 cpi->bits_off_target = lc->bits_off_target;
268 cpi->total_actual_bits = lc->total_actual_bits;
269 cpi->active_worst_quality = lc->active_worst_quality;
270 cpi->active_best_quality = lc->active_best_quality;
271 cpi->ni_av_qi = lc->ni_av_qi;
272 cpi->ni_tot_qi = lc->ni_tot_qi;
273 cpi->ni_frames = lc->ni_frames;
274 cpi->avg_frame_qindex = lc->avg_frame_qindex;
275 cpi->rate_correction_factor = lc->rate_correction_factor;
276 cpi->key_frame_rate_correction_factor = lc->key_frame_rate_correction_factor;
277 cpi->gf_rate_correction_factor = lc->gf_rate_correction_factor;
278 cpi->zbin_over_quant = lc->zbin_over_quant;
279 cpi->inter_frame_target = lc->inter_frame_target;
280 cpi->total_byte_count = lc->total_byte_count;
281 cpi->common.filter_level = lc->filter_level;
283 cpi->last_frame_percent_intra = lc->last_frame_percent_intra;
285 memcpy (cpi->count_mb_ref_frame_usage,
286 lc->count_mb_ref_frame_usage,
287 sizeof(cpi->count_mb_ref_frame_usage));
290 static void setup_features(VP8_COMP *cpi)
292 // If segmentation enabled set the update flags
293 if ( cpi->mb.e_mbd.segmentation_enabled )
295 cpi->mb.e_mbd.update_mb_segmentation_map = 1;
296 cpi->mb.e_mbd.update_mb_segmentation_data = 1;
300 cpi->mb.e_mbd.update_mb_segmentation_map = 0;
301 cpi->mb.e_mbd.update_mb_segmentation_data = 0;
304 cpi->mb.e_mbd.mode_ref_lf_delta_enabled = 0;
305 cpi->mb.e_mbd.mode_ref_lf_delta_update = 0;
306 vpx_memset(cpi->mb.e_mbd.ref_lf_deltas, 0, sizeof(cpi->mb.e_mbd.ref_lf_deltas));
307 vpx_memset(cpi->mb.e_mbd.mode_lf_deltas, 0, sizeof(cpi->mb.e_mbd.mode_lf_deltas));
308 vpx_memset(cpi->mb.e_mbd.last_ref_lf_deltas, 0, sizeof(cpi->mb.e_mbd.ref_lf_deltas));
309 vpx_memset(cpi->mb.e_mbd.last_mode_lf_deltas, 0, sizeof(cpi->mb.e_mbd.mode_lf_deltas));
311 set_default_lf_deltas(cpi);
316 static void dealloc_raw_frame_buffers(VP8_COMP *cpi);
319 static void dealloc_compressor_data(VP8_COMP *cpi)
321 vpx_free(cpi->tplist);
324 /* Delete last frame MV storage buffers */
328 vpx_free(cpi->lf_ref_frame_sign_bias);
329 cpi->lf_ref_frame_sign_bias = 0;
331 vpx_free(cpi->lf_ref_frame);
332 cpi->lf_ref_frame = 0;
334 /* Delete sementation map */
335 vpx_free(cpi->segmentation_map);
336 cpi->segmentation_map = 0;
338 vpx_free(cpi->active_map);
341 vp8_de_alloc_frame_buffers(&cpi->common);
343 vp8_yv12_de_alloc_frame_buffer(&cpi->pick_lf_lvl_frame);
344 vp8_yv12_de_alloc_frame_buffer(&cpi->scaled_source);
345 dealloc_raw_frame_buffers(cpi);
350 /* Structure used to monitor GF usage */
351 vpx_free(cpi->gf_active_flags);
352 cpi->gf_active_flags = 0;
354 /* Activity mask based per mb zbin adjustments */
355 vpx_free(cpi->mb_activity_map);
356 cpi->mb_activity_map = 0;
357 vpx_free(cpi->mb_norm_activity_map);
358 cpi->mb_norm_activity_map = 0;
360 vpx_free(cpi->mb.pip);
364 static void enable_segmentation(VP8_COMP *cpi)
366 /* Set the appropriate feature bit */
367 cpi->mb.e_mbd.segmentation_enabled = 1;
368 cpi->mb.e_mbd.update_mb_segmentation_map = 1;
369 cpi->mb.e_mbd.update_mb_segmentation_data = 1;
371 static void disable_segmentation(VP8_COMP *cpi)
373 /* Clear the appropriate feature bit */
374 cpi->mb.e_mbd.segmentation_enabled = 0;
377 /* Valid values for a segment are 0 to 3
378 * Segmentation map is arrange as [Rows][Columns]
380 static void set_segmentation_map(VP8_COMP *cpi, unsigned char *segmentation_map)
382 /* Copy in the new segmentation map */
383 vpx_memcpy(cpi->segmentation_map, segmentation_map, (cpi->common.mb_rows * cpi->common.mb_cols));
385 /* Signal that the map should be updated. */
386 cpi->mb.e_mbd.update_mb_segmentation_map = 1;
387 cpi->mb.e_mbd.update_mb_segmentation_data = 1;
390 /* The values given for each segment can be either deltas (from the default
391 * value chosen for the frame) or absolute values.
393 * Valid range for abs values is:
394 * (0-127 for MB_LVL_ALT_Q), (0-63 for SEGMENT_ALT_LF)
395 * Valid range for delta values are:
396 * (+/-127 for MB_LVL_ALT_Q), (+/-63 for SEGMENT_ALT_LF)
398 * abs_delta = SEGMENT_DELTADATA (deltas)
399 * abs_delta = SEGMENT_ABSDATA (use the absolute values given).
402 static void set_segment_data(VP8_COMP *cpi, signed char *feature_data, unsigned char abs_delta)
404 cpi->mb.e_mbd.mb_segement_abs_delta = abs_delta;
405 vpx_memcpy(cpi->segment_feature_data, feature_data, sizeof(cpi->segment_feature_data));
409 static void segmentation_test_function(VP8_COMP *cpi)
411 unsigned char *seg_map;
412 signed char feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS];
414 // Create a temporary map for segmentation data.
415 CHECK_MEM_ERROR(seg_map, vpx_calloc(cpi->common.mb_rows * cpi->common.mb_cols, 1));
417 // Set the segmentation Map
418 set_segmentation_map(cpi, seg_map);
420 // Activate segmentation.
421 enable_segmentation(cpi);
423 // Set up the quant segment data
424 feature_data[MB_LVL_ALT_Q][0] = 0;
425 feature_data[MB_LVL_ALT_Q][1] = 4;
426 feature_data[MB_LVL_ALT_Q][2] = 0;
427 feature_data[MB_LVL_ALT_Q][3] = 0;
428 // Set up the loop segment data
429 feature_data[MB_LVL_ALT_LF][0] = 0;
430 feature_data[MB_LVL_ALT_LF][1] = 0;
431 feature_data[MB_LVL_ALT_LF][2] = 0;
432 feature_data[MB_LVL_ALT_LF][3] = 0;
434 // Initialise the feature data structure
435 // SEGMENT_DELTADATA 0, SEGMENT_ABSDATA 1
436 set_segment_data(cpi, &feature_data[0][0], SEGMENT_DELTADATA);
438 // Delete sementation map
444 /* A simple function to cyclically refresh the background at a lower Q */
445 static void cyclic_background_refresh(VP8_COMP *cpi, int Q, int lf_adjustment)
447 unsigned char *seg_map;
448 signed char feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS];
450 int block_count = cpi->cyclic_refresh_mode_max_mbs_perframe;
451 int mbs_in_frame = cpi->common.mb_rows * cpi->common.mb_cols;
453 /* Create a temporary map for segmentation data. */
454 CHECK_MEM_ERROR(seg_map, vpx_calloc(cpi->common.mb_rows * cpi->common.mb_cols, 1));
456 cpi->cyclic_refresh_q = Q;
458 for (i = Q; i > 0; i--)
460 if (vp8_bits_per_mb[cpi->common.frame_type][i] >= ((vp8_bits_per_mb[cpi->common.frame_type][Q]*(Q + 128)) / 64))
466 cpi->cyclic_refresh_q = i;
468 /* Only update for inter frames */
469 if (cpi->common.frame_type != KEY_FRAME)
471 /* Cycle through the macro_block rows */
472 /* MB loop to set local segmentation map */
473 for (i = cpi->cyclic_refresh_mode_index; i < mbs_in_frame; i++)
475 /* If the MB is as a candidate for clean up then mark it for
476 * possible boost/refresh (segment 1) The segment id may get
477 * reset to 0 later if the MB gets coded anything other than
478 * last frame 0,0 as only (last frame 0,0) MBs are eligable for
479 * refresh : that is to say Mbs likely to be background blocks.
481 if (cpi->cyclic_refresh_map[i] == 0)
489 /* Skip blocks that have been refreshed recently anyway. */
490 if (cpi->cyclic_refresh_map[i] < 0)
491 cpi->cyclic_refresh_map[i]++;
502 /* If we have gone through the frame reset to the start */
503 cpi->cyclic_refresh_mode_index = i;
505 if (cpi->cyclic_refresh_mode_index >= mbs_in_frame)
506 cpi->cyclic_refresh_mode_index = 0;
509 /* Set the segmentation Map */
510 set_segmentation_map(cpi, seg_map);
512 /* Activate segmentation. */
513 enable_segmentation(cpi);
515 /* Set up the quant segment data */
516 feature_data[MB_LVL_ALT_Q][0] = 0;
517 feature_data[MB_LVL_ALT_Q][1] = (cpi->cyclic_refresh_q - Q);
518 feature_data[MB_LVL_ALT_Q][2] = 0;
519 feature_data[MB_LVL_ALT_Q][3] = 0;
521 /* Set up the loop segment data */
522 feature_data[MB_LVL_ALT_LF][0] = 0;
523 feature_data[MB_LVL_ALT_LF][1] = lf_adjustment;
524 feature_data[MB_LVL_ALT_LF][2] = 0;
525 feature_data[MB_LVL_ALT_LF][3] = 0;
527 /* Initialise the feature data structure */
528 set_segment_data(cpi, &feature_data[0][0], SEGMENT_DELTADATA);
530 /* Delete sementation map */
537 static void set_default_lf_deltas(VP8_COMP *cpi)
539 cpi->mb.e_mbd.mode_ref_lf_delta_enabled = 1;
540 cpi->mb.e_mbd.mode_ref_lf_delta_update = 1;
542 vpx_memset(cpi->mb.e_mbd.ref_lf_deltas, 0, sizeof(cpi->mb.e_mbd.ref_lf_deltas));
543 vpx_memset(cpi->mb.e_mbd.mode_lf_deltas, 0, sizeof(cpi->mb.e_mbd.mode_lf_deltas));
545 /* Test of ref frame deltas */
546 cpi->mb.e_mbd.ref_lf_deltas[INTRA_FRAME] = 2;
547 cpi->mb.e_mbd.ref_lf_deltas[LAST_FRAME] = 0;
548 cpi->mb.e_mbd.ref_lf_deltas[GOLDEN_FRAME] = -2;
549 cpi->mb.e_mbd.ref_lf_deltas[ALTREF_FRAME] = -2;
551 cpi->mb.e_mbd.mode_lf_deltas[0] = 4; /* BPRED */
552 cpi->mb.e_mbd.mode_lf_deltas[1] = -2; /* Zero */
553 cpi->mb.e_mbd.mode_lf_deltas[2] = 2; /* New mv */
554 cpi->mb.e_mbd.mode_lf_deltas[3] = 4; /* Split mv */
557 /* Convenience macros for mapping speed and mode into a continuous
560 #define GOOD(x) (x+1)
563 static int speed_map(int speed, const int *map)
570 } while(speed >= *map++);
574 static const int thresh_mult_map_znn[] = {
575 /* map common to zero, nearest, and near */
576 0, GOOD(2), 1500, GOOD(3), 2000, RT(0), 1000, RT(2), 2000, INT_MAX
579 static const int thresh_mult_map_vhpred[] = {
580 1000, GOOD(2), 1500, GOOD(3), 2000, RT(0), 1000, RT(1), 2000,
581 RT(7), INT_MAX, INT_MAX
584 static const int thresh_mult_map_bpred[] = {
585 2000, GOOD(0), 2500, GOOD(2), 5000, GOOD(3), 7500, RT(0), 2500, RT(1), 5000,
586 RT(6), INT_MAX, INT_MAX
589 static const int thresh_mult_map_tm[] = {
590 1000, GOOD(2), 1500, GOOD(3), 2000, RT(0), 0, RT(1), 1000, RT(2), 2000,
591 RT(7), INT_MAX, INT_MAX
594 static const int thresh_mult_map_new1[] = {
595 1000, GOOD(2), 2000, RT(0), 2000, INT_MAX
598 static const int thresh_mult_map_new2[] = {
599 1000, GOOD(2), 2000, GOOD(3), 2500, GOOD(5), 4000, RT(0), 2000, RT(2), 2500,
603 static const int thresh_mult_map_split1[] = {
604 2500, GOOD(0), 1700, GOOD(2), 10000, GOOD(3), 25000, GOOD(4), INT_MAX,
605 RT(0), 5000, RT(1), 10000, RT(2), 25000, RT(3), INT_MAX, INT_MAX
608 static const int thresh_mult_map_split2[] = {
609 5000, GOOD(0), 4500, GOOD(2), 20000, GOOD(3), 50000, GOOD(4), INT_MAX,
610 RT(0), 10000, RT(1), 20000, RT(2), 50000, RT(3), INT_MAX, INT_MAX
613 static const int mode_check_freq_map_zn2[] = {
614 /* {zero,nearest}{2,3} */
615 0, RT(10), 1<<1, RT(11), 1<<2, RT(12), 1<<3, INT_MAX
618 static const int mode_check_freq_map_vhbpred[] = {
619 0, GOOD(5), 2, RT(0), 0, RT(3), 2, RT(5), 4, INT_MAX
622 static const int mode_check_freq_map_near2[] = {
623 0, GOOD(5), 2, RT(0), 0, RT(3), 2, RT(10), 1<<2, RT(11), 1<<3, RT(12), 1<<4,
627 static const int mode_check_freq_map_new1[] = {
628 0, RT(10), 1<<1, RT(11), 1<<2, RT(12), 1<<3, INT_MAX
631 static const int mode_check_freq_map_new2[] = {
632 0, GOOD(5), 4, RT(0), 0, RT(3), 4, RT(10), 1<<3, RT(11), 1<<4, RT(12), 1<<5,
636 static const int mode_check_freq_map_split1[] = {
637 0, GOOD(2), 2, GOOD(3), 7, RT(1), 2, RT(2), 7, INT_MAX
640 static const int mode_check_freq_map_split2[] = {
641 0, GOOD(1), 2, GOOD(2), 4, GOOD(3), 15, RT(1), 4, RT(2), 15, INT_MAX
644 void vp8_set_speed_features(VP8_COMP *cpi)
646 SPEED_FEATURES *sf = &cpi->sf;
647 int Mode = cpi->compressor_speed;
648 int Speed = cpi->Speed;
650 VP8_COMMON *cm = &cpi->common;
651 int last_improved_quant = sf->improved_quant;
654 /* Initialise default mode frequency sampling variables */
655 for (i = 0; i < MAX_MODES; i ++)
657 cpi->mode_check_freq[i] = 0;
658 cpi->mode_test_hit_counts[i] = 0;
659 cpi->mode_chosen_counts[i] = 0;
662 cpi->mbs_tested_so_far = 0;
664 /* best quality defaults */
666 sf->search_method = NSTEP;
667 sf->improved_quant = 1;
668 sf->improved_dct = 1;
671 sf->quarter_pixel_search = 1;
672 sf->half_pixel_search = 1;
673 sf->iterative_sub_pixel = 1;
674 sf->optimize_coefficients = 1;
675 sf->use_fastquant_for_pick = 0;
676 sf->no_skip_block4x4_search = 1;
679 sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
680 sf->improved_mv_pred = 1;
682 /* default thresholds to 0 */
683 for (i = 0; i < MAX_MODES; i++)
684 sf->thresh_mult[i] = 0;
686 /* Count enabled references */
688 if (cpi->ref_frame_flags & VP8_LAST_FRAME)
690 if (cpi->ref_frame_flags & VP8_GOLD_FRAME)
692 if (cpi->ref_frame_flags & VP8_ALTR_FRAME)
695 /* Convert speed to continuous range, with clamping */
707 sf->thresh_mult[THR_ZERO1] =
708 sf->thresh_mult[THR_NEAREST1] =
709 sf->thresh_mult[THR_NEAR1] =
710 sf->thresh_mult[THR_DC] = 0; /* always */
712 sf->thresh_mult[THR_ZERO2] =
713 sf->thresh_mult[THR_ZERO3] =
714 sf->thresh_mult[THR_NEAREST2] =
715 sf->thresh_mult[THR_NEAREST3] =
716 sf->thresh_mult[THR_NEAR2] =
717 sf->thresh_mult[THR_NEAR3] = speed_map(Speed, thresh_mult_map_znn);
719 sf->thresh_mult[THR_V_PRED] =
720 sf->thresh_mult[THR_H_PRED] = speed_map(Speed, thresh_mult_map_vhpred);
721 sf->thresh_mult[THR_B_PRED] = speed_map(Speed, thresh_mult_map_bpred);
722 sf->thresh_mult[THR_TM] = speed_map(Speed, thresh_mult_map_tm);
723 sf->thresh_mult[THR_NEW1] = speed_map(Speed, thresh_mult_map_new1);
724 sf->thresh_mult[THR_NEW2] =
725 sf->thresh_mult[THR_NEW3] = speed_map(Speed, thresh_mult_map_new2);
726 sf->thresh_mult[THR_SPLIT1] = speed_map(Speed, thresh_mult_map_split1);
727 sf->thresh_mult[THR_SPLIT2] =
728 sf->thresh_mult[THR_SPLIT3] = speed_map(Speed, thresh_mult_map_split2);
730 cpi->mode_check_freq[THR_ZERO1] =
731 cpi->mode_check_freq[THR_NEAREST1] =
732 cpi->mode_check_freq[THR_NEAR1] =
733 cpi->mode_check_freq[THR_TM] =
734 cpi->mode_check_freq[THR_DC] = 0; /* always */
736 cpi->mode_check_freq[THR_ZERO2] =
737 cpi->mode_check_freq[THR_ZERO3] =
738 cpi->mode_check_freq[THR_NEAREST2] =
739 cpi->mode_check_freq[THR_NEAREST3] = speed_map(Speed,
740 mode_check_freq_map_zn2);
742 cpi->mode_check_freq[THR_NEAR2] =
743 cpi->mode_check_freq[THR_NEAR3] = speed_map(Speed,
744 mode_check_freq_map_near2);
746 cpi->mode_check_freq[THR_V_PRED] =
747 cpi->mode_check_freq[THR_H_PRED] =
748 cpi->mode_check_freq[THR_B_PRED] = speed_map(Speed,
749 mode_check_freq_map_vhbpred);
750 cpi->mode_check_freq[THR_NEW1] = speed_map(Speed,
751 mode_check_freq_map_new1);
752 cpi->mode_check_freq[THR_NEW2] =
753 cpi->mode_check_freq[THR_NEW3] = speed_map(Speed,
754 mode_check_freq_map_new2);
755 cpi->mode_check_freq[THR_SPLIT1] = speed_map(Speed,
756 mode_check_freq_map_split1);
757 cpi->mode_check_freq[THR_SPLIT2] =
758 cpi->mode_check_freq[THR_SPLIT3] = speed_map(Speed,
759 mode_check_freq_map_split2);
763 #if !(CONFIG_REALTIME_ONLY)
764 case 0: /* best quality mode */
766 sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
772 /* Disable coefficient optimization above speed 0 */
773 sf->optimize_coefficients = 0;
774 sf->use_fastquant_for_pick = 1;
775 sf->no_skip_block4x4_search = 0;
782 sf->improved_quant = 0;
783 sf->improved_dct = 0;
785 /* Only do recode loop on key frames, golden frames and
795 sf->recode_loop = 0; /* recode loop off */
796 sf->RD = 0; /* Turn rd off */
802 sf->auto_filter = 0; /* Faster selection of loop filter */
808 sf->optimize_coefficients = 0;
811 sf->iterative_sub_pixel = 1;
812 sf->search_method = NSTEP;
816 sf->improved_quant = 0;
817 sf->improved_dct = 0;
819 sf->use_fastquant_for_pick = 1;
820 sf->no_skip_block4x4_search = 0;
825 sf->auto_filter = 0; /* Faster selection of loop filter */
835 sf->auto_filter = 0; /* Faster selection of loop filter */
836 sf->search_method = HEX;
837 sf->iterative_sub_pixel = 0;
842 unsigned int sum = 0;
843 unsigned int total_mbs = cm->MBs;
845 unsigned int total_skip;
849 if (cpi->oxcf.encode_breakout > 2000)
850 min = cpi->oxcf.encode_breakout;
854 for (i = 0; i < min; i++)
856 sum += cpi->error_bins[i];
862 /* i starts from 2 to make sure thresh started from 2048 */
863 for (; i < 1024; i++)
865 sum += cpi->error_bins[i];
867 if (10 * sum >= (unsigned int)(cpi->Speed - 6)*(total_mbs - total_skip))
879 sf->thresh_mult[THR_NEW1 ] = thresh;
880 sf->thresh_mult[THR_NEAREST1 ] = thresh >> 1;
881 sf->thresh_mult[THR_NEAR1 ] = thresh >> 1;
886 sf->thresh_mult[THR_NEW2] = thresh << 1;
887 sf->thresh_mult[THR_NEAREST2 ] = thresh;
888 sf->thresh_mult[THR_NEAR2 ] = thresh;
893 sf->thresh_mult[THR_NEW3] = thresh << 1;
894 sf->thresh_mult[THR_NEAREST3 ] = thresh;
895 sf->thresh_mult[THR_NEAR3 ] = thresh;
898 sf->improved_mv_pred = 0;
902 sf->quarter_pixel_search = 0;
906 cm->filter_type = NORMAL_LOOPFILTER;
909 cm->filter_type = SIMPLE_LOOPFILTER;
913 cm->filter_type = SIMPLE_LOOPFILTER;
916 /* This has a big hit on quality. Last resort */
918 sf->half_pixel_search = 0;
920 vpx_memset(cpi->error_bins, 0, sizeof(cpi->error_bins));
924 /* Slow quant, dct and trellis not worthwhile for first pass
925 * so make sure they are always turned off.
927 if ( cpi->pass == 1 )
929 sf->improved_quant = 0;
930 sf->optimize_coefficients = 0;
931 sf->improved_dct = 0;
934 if (cpi->sf.search_method == NSTEP)
936 vp8_init3smotion_compensation(&cpi->mb, cm->yv12_fb[cm->lst_fb_idx].y_stride);
938 else if (cpi->sf.search_method == DIAMOND)
940 vp8_init_dsmotion_compensation(&cpi->mb, cm->yv12_fb[cm->lst_fb_idx].y_stride);
943 if (cpi->sf.improved_dct)
945 cpi->mb.short_fdct8x4 = vp8_short_fdct8x4;
946 cpi->mb.short_fdct4x4 = vp8_short_fdct4x4;
950 /* No fast FDCT defined for any platform at this time. */
951 cpi->mb.short_fdct8x4 = vp8_short_fdct8x4;
952 cpi->mb.short_fdct4x4 = vp8_short_fdct4x4;
955 cpi->mb.short_walsh4x4 = vp8_short_walsh4x4;
957 if (cpi->sf.improved_quant)
959 cpi->mb.quantize_b = vp8_regular_quantize_b;
960 cpi->mb.quantize_b_pair = vp8_regular_quantize_b_pair;
964 cpi->mb.quantize_b = vp8_fast_quantize_b;
965 cpi->mb.quantize_b_pair = vp8_fast_quantize_b_pair;
967 if (cpi->sf.improved_quant != last_improved_quant)
968 vp8cx_init_quantizer(cpi);
970 if (cpi->sf.iterative_sub_pixel == 1)
972 cpi->find_fractional_mv_step = vp8_find_best_sub_pixel_step_iteratively;
974 else if (cpi->sf.quarter_pixel_search)
976 cpi->find_fractional_mv_step = vp8_find_best_sub_pixel_step;
978 else if (cpi->sf.half_pixel_search)
980 cpi->find_fractional_mv_step = vp8_find_best_half_pixel_step;
984 cpi->find_fractional_mv_step = vp8_skip_fractional_mv_step;
987 if (cpi->sf.optimize_coefficients == 1 && cpi->pass!=1)
988 cpi->mb.optimize = 1;
990 cpi->mb.optimize = 0;
992 if (cpi->common.full_pixel)
993 cpi->find_fractional_mv_step = vp8_skip_fractional_mv_step;
996 frames_at_speed[cpi->Speed]++;
1002 static void alloc_raw_frame_buffers(VP8_COMP *cpi)
1004 #if VP8_TEMPORAL_ALT_REF
1005 int width = (cpi->oxcf.Width + 15) & ~15;
1006 int height = (cpi->oxcf.Height + 15) & ~15;
1009 cpi->lookahead = vp8_lookahead_init(cpi->oxcf.Width, cpi->oxcf.Height,
1010 cpi->oxcf.lag_in_frames);
1012 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1013 "Failed to allocate lag buffers");
1015 #if VP8_TEMPORAL_ALT_REF
1017 if (vp8_yv12_alloc_frame_buffer(&cpi->alt_ref_buffer,
1018 width, height, VP8BORDERINPIXELS))
1019 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1020 "Failed to allocate altref buffer");
1026 static void dealloc_raw_frame_buffers(VP8_COMP *cpi)
1028 #if VP8_TEMPORAL_ALT_REF
1029 vp8_yv12_de_alloc_frame_buffer(&cpi->alt_ref_buffer);
1031 vp8_lookahead_destroy(cpi->lookahead);
1035 static int vp8_alloc_partition_data(VP8_COMP *cpi)
1037 vpx_free(cpi->mb.pip);
1039 cpi->mb.pip = vpx_calloc((cpi->common.mb_cols + 1) *
1040 (cpi->common.mb_rows + 1),
1041 sizeof(PARTITION_INFO));
1045 cpi->mb.pi = cpi->mb.pip + cpi->common.mode_info_stride + 1;
1050 void vp8_alloc_compressor_data(VP8_COMP *cpi)
1052 VP8_COMMON *cm = & cpi->common;
1054 int width = cm->Width;
1055 int height = cm->Height;
1057 if (vp8_alloc_frame_buffers(cm, width, height))
1058 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1059 "Failed to allocate frame buffers");
1061 if (vp8_alloc_partition_data(cpi))
1062 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1063 "Failed to allocate partition data");
1066 if ((width & 0xf) != 0)
1067 width += 16 - (width & 0xf);
1069 if ((height & 0xf) != 0)
1070 height += 16 - (height & 0xf);
1073 if (vp8_yv12_alloc_frame_buffer(&cpi->pick_lf_lvl_frame,
1074 width, height, VP8BORDERINPIXELS))
1075 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1076 "Failed to allocate last frame buffer");
1078 if (vp8_yv12_alloc_frame_buffer(&cpi->scaled_source,
1079 width, height, VP8BORDERINPIXELS))
1080 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1081 "Failed to allocate scaled source buffer");
1086 #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
1087 unsigned int tokens = 8 * 24 * 16; /* one MB for each thread */
1089 unsigned int tokens = cm->mb_rows * cm->mb_cols * 24 * 16;
1091 CHECK_MEM_ERROR(cpi->tok, vpx_calloc(tokens, sizeof(*cpi->tok)));
1094 /* Data used for real time vc mode to see if gf needs refreshing */
1095 cpi->inter_zz_count = 0;
1096 cpi->gf_bad_count = 0;
1097 cpi->gf_update_recommended = 0;
1100 /* Structures used to minitor GF usage */
1101 vpx_free(cpi->gf_active_flags);
1102 CHECK_MEM_ERROR(cpi->gf_active_flags,
1103 vpx_calloc(1, cm->mb_rows * cm->mb_cols));
1104 cpi->gf_active_count = cm->mb_rows * cm->mb_cols;
1106 vpx_free(cpi->mb_activity_map);
1107 CHECK_MEM_ERROR(cpi->mb_activity_map,
1108 vpx_calloc(sizeof(unsigned int),
1109 cm->mb_rows * cm->mb_cols));
1111 vpx_free(cpi->mb_norm_activity_map);
1112 CHECK_MEM_ERROR(cpi->mb_norm_activity_map,
1113 vpx_calloc(sizeof(unsigned int),
1114 cm->mb_rows * cm->mb_cols));
1116 #if CONFIG_MULTITHREAD
1118 cpi->mt_sync_range = 1;
1119 else if (width <= 1280)
1120 cpi->mt_sync_range = 4;
1121 else if (width <= 2560)
1122 cpi->mt_sync_range = 8;
1124 cpi->mt_sync_range = 16;
1127 vpx_free(cpi->tplist);
1129 CHECK_MEM_ERROR(cpi->tplist, vpx_malloc(sizeof(TOKENLIST) * cpi->common.mb_rows));
1134 static const int q_trans[] =
1136 0, 1, 2, 3, 4, 5, 7, 8,
1137 9, 10, 12, 13, 15, 17, 18, 19,
1138 20, 21, 23, 24, 25, 26, 27, 28,
1139 29, 30, 31, 33, 35, 37, 39, 41,
1140 43, 45, 47, 49, 51, 53, 55, 57,
1141 59, 61, 64, 67, 70, 73, 76, 79,
1142 82, 85, 88, 91, 94, 97, 100, 103,
1143 106, 109, 112, 115, 118, 121, 124, 127,
1146 int vp8_reverse_trans(int x)
1150 for (i = 0; i < 64; i++)
1151 if (q_trans[i] >= x)
1156 void vp8_new_frame_rate(VP8_COMP *cpi, double framerate)
1161 cpi->frame_rate = framerate;
1162 cpi->output_frame_rate = framerate;
1163 cpi->per_frame_bandwidth = (int)(cpi->oxcf.target_bandwidth /
1164 cpi->output_frame_rate);
1165 cpi->av_per_frame_bandwidth = cpi->per_frame_bandwidth;
1166 cpi->min_frame_bandwidth = (int)(cpi->av_per_frame_bandwidth *
1167 cpi->oxcf.two_pass_vbrmin_section / 100);
1169 /* Set Maximum gf/arf interval */
1170 cpi->max_gf_interval = ((int)(cpi->output_frame_rate / 2.0) + 2);
1172 if(cpi->max_gf_interval < 12)
1173 cpi->max_gf_interval = 12;
1175 /* Extended interval for genuinely static scenes */
1176 cpi->twopass.static_scene_max_gf_interval = cpi->key_frame_frequency >> 1;
1178 /* Special conditions when altr ref frame enabled in lagged compress mode */
1179 if (cpi->oxcf.play_alternate && cpi->oxcf.lag_in_frames)
1181 if (cpi->max_gf_interval > cpi->oxcf.lag_in_frames - 1)
1182 cpi->max_gf_interval = cpi->oxcf.lag_in_frames - 1;
1184 if (cpi->twopass.static_scene_max_gf_interval > cpi->oxcf.lag_in_frames - 1)
1185 cpi->twopass.static_scene_max_gf_interval = cpi->oxcf.lag_in_frames - 1;
1188 if ( cpi->max_gf_interval > cpi->twopass.static_scene_max_gf_interval )
1189 cpi->max_gf_interval = cpi->twopass.static_scene_max_gf_interval;
1194 rescale(int val, int num, int denom)
1196 int64_t llnum = num;
1197 int64_t llden = denom;
1198 int64_t llval = val;
1200 return llval * llnum / llden;
1204 static void init_config(VP8_COMP *cpi, VP8_CONFIG *oxcf)
1206 VP8_COMMON *cm = &cpi->common;
1211 cpi->auto_adjust_gold_quantizer = 1;
1213 cm->version = oxcf->Version;
1214 vp8_setup_version(cm);
1216 /* frame rate is not available on the first frame, as it's derived from
1217 * the observed timestamps. The actual value used here doesn't matter
1218 * too much, as it will adapt quickly. If the reciprocal of the timebase
1219 * seems like a reasonable framerate, then use that as a guess, otherwise
1222 cpi->frame_rate = (double)(oxcf->timebase.den) /
1223 (double)(oxcf->timebase.num);
1225 if (cpi->frame_rate > 180)
1226 cpi->frame_rate = 30;
1228 cpi->ref_frame_rate = cpi->frame_rate;
1230 /* change includes all joint functionality */
1231 vp8_change_config(cpi, oxcf);
1233 /* Initialize active best and worst q and average q values. */
1234 cpi->active_worst_quality = cpi->oxcf.worst_allowed_q;
1235 cpi->active_best_quality = cpi->oxcf.best_allowed_q;
1236 cpi->avg_frame_qindex = cpi->oxcf.worst_allowed_q;
1238 /* Initialise the starting buffer levels */
1239 cpi->buffer_level = cpi->oxcf.starting_buffer_level;
1240 cpi->bits_off_target = cpi->oxcf.starting_buffer_level;
1242 cpi->rolling_target_bits = cpi->av_per_frame_bandwidth;
1243 cpi->rolling_actual_bits = cpi->av_per_frame_bandwidth;
1244 cpi->long_rolling_target_bits = cpi->av_per_frame_bandwidth;
1245 cpi->long_rolling_actual_bits = cpi->av_per_frame_bandwidth;
1247 cpi->total_actual_bits = 0;
1248 cpi->total_target_vs_actual = 0;
1250 /* Temporal scalabilty */
1251 if (cpi->oxcf.number_of_layers > 1)
1254 double prev_layer_frame_rate=0;
1256 for (i=0; i<cpi->oxcf.number_of_layers; i++)
1258 LAYER_CONTEXT *lc = &cpi->layer_context[i];
1260 /* Layer configuration */
1262 cpi->output_frame_rate / cpi->oxcf.rate_decimator[i];
1263 lc->target_bandwidth = cpi->oxcf.target_bitrate[i] * 1000;
1265 lc->starting_buffer_level_in_ms = oxcf->starting_buffer_level;
1266 lc->optimal_buffer_level_in_ms = oxcf->optimal_buffer_level;
1267 lc->maximum_buffer_size_in_ms = oxcf->maximum_buffer_size;
1269 lc->starting_buffer_level =
1270 rescale(oxcf->starting_buffer_level,
1271 lc->target_bandwidth, 1000);
1273 if (oxcf->optimal_buffer_level == 0)
1274 lc->optimal_buffer_level = lc->target_bandwidth / 8;
1276 lc->optimal_buffer_level =
1277 rescale(oxcf->optimal_buffer_level,
1278 lc->target_bandwidth, 1000);
1280 if (oxcf->maximum_buffer_size == 0)
1281 lc->maximum_buffer_size = lc->target_bandwidth / 8;
1283 lc->maximum_buffer_size =
1284 rescale(oxcf->maximum_buffer_size,
1285 lc->target_bandwidth, 1000);
1287 /* Work out the average size of a frame within this layer */
1289 lc->avg_frame_size_for_layer = (cpi->oxcf.target_bitrate[i] -
1290 cpi->oxcf.target_bitrate[i-1]) * 1000 /
1291 (lc->frame_rate - prev_layer_frame_rate);
1293 lc->active_worst_quality = cpi->oxcf.worst_allowed_q;
1294 lc->active_best_quality = cpi->oxcf.best_allowed_q;
1295 lc->avg_frame_qindex = cpi->oxcf.worst_allowed_q;
1297 lc->buffer_level = lc->starting_buffer_level;
1298 lc->bits_off_target = lc->starting_buffer_level;
1300 lc->total_actual_bits = 0;
1304 lc->rate_correction_factor = 1.0;
1305 lc->key_frame_rate_correction_factor = 1.0;
1306 lc->gf_rate_correction_factor = 1.0;
1307 lc->inter_frame_target = 0.0;
1309 prev_layer_frame_rate = lc->frame_rate;
1313 #if VP8_TEMPORAL_ALT_REF
1317 cpi->fixed_divide[0] = 0;
1319 for (i = 1; i < 512; i++)
1320 cpi->fixed_divide[i] = 0x80000 / i;
1325 static void update_layer_contexts (VP8_COMP *cpi)
1327 VP8_CONFIG *oxcf = &cpi->oxcf;
1329 /* Update snapshots of the layer contexts to reflect new parameters */
1330 if (oxcf->number_of_layers > 1)
1333 double prev_layer_frame_rate=0;
1335 for (i=0; i<oxcf->number_of_layers; i++)
1337 LAYER_CONTEXT *lc = &cpi->layer_context[i];
1340 cpi->ref_frame_rate / oxcf->rate_decimator[i];
1341 lc->target_bandwidth = oxcf->target_bitrate[i] * 1000;
1343 lc->starting_buffer_level = rescale(
1344 oxcf->starting_buffer_level_in_ms,
1345 lc->target_bandwidth, 1000);
1347 if (oxcf->optimal_buffer_level == 0)
1348 lc->optimal_buffer_level = lc->target_bandwidth / 8;
1350 lc->optimal_buffer_level = rescale(
1351 oxcf->optimal_buffer_level_in_ms,
1352 lc->target_bandwidth, 1000);
1354 if (oxcf->maximum_buffer_size == 0)
1355 lc->maximum_buffer_size = lc->target_bandwidth / 8;
1357 lc->maximum_buffer_size = rescale(
1358 oxcf->maximum_buffer_size_in_ms,
1359 lc->target_bandwidth, 1000);
1361 /* Work out the average size of a frame within this layer */
1363 lc->avg_frame_size_for_layer = (oxcf->target_bitrate[i] -
1364 oxcf->target_bitrate[i-1]) * 1000 /
1365 (lc->frame_rate - prev_layer_frame_rate);
1367 lc->active_worst_quality = oxcf->worst_allowed_q;
1368 lc->active_best_quality = oxcf->best_allowed_q;
1369 lc->avg_frame_qindex = oxcf->worst_allowed_q;
1371 prev_layer_frame_rate = lc->frame_rate;
1376 void vp8_change_config(VP8_COMP *cpi, VP8_CONFIG *oxcf)
1378 VP8_COMMON *cm = &cpi->common;
1387 #if CONFIG_MULTITHREAD
1388 /* wait for the last picture loopfilter thread done */
1389 if (cpi->b_lpf_running)
1391 sem_wait(&cpi->h_event_end_lpf);
1392 cpi->b_lpf_running = 0;
1396 if (cm->version != oxcf->Version)
1398 cm->version = oxcf->Version;
1399 vp8_setup_version(cm);
1402 last_w = cpi->oxcf.Width;
1403 last_h = cpi->oxcf.Height;
1407 switch (cpi->oxcf.Mode)
1412 cpi->compressor_speed = 2;
1414 if (cpi->oxcf.cpu_used < -16)
1416 cpi->oxcf.cpu_used = -16;
1419 if (cpi->oxcf.cpu_used > 16)
1420 cpi->oxcf.cpu_used = 16;
1424 case MODE_GOODQUALITY:
1426 cpi->compressor_speed = 1;
1428 if (cpi->oxcf.cpu_used < -5)
1430 cpi->oxcf.cpu_used = -5;
1433 if (cpi->oxcf.cpu_used > 5)
1434 cpi->oxcf.cpu_used = 5;
1438 case MODE_BESTQUALITY:
1440 cpi->compressor_speed = 0;
1443 case MODE_FIRSTPASS:
1445 cpi->compressor_speed = 1;
1447 case MODE_SECONDPASS:
1449 cpi->compressor_speed = 1;
1451 if (cpi->oxcf.cpu_used < -5)
1453 cpi->oxcf.cpu_used = -5;
1456 if (cpi->oxcf.cpu_used > 5)
1457 cpi->oxcf.cpu_used = 5;
1460 case MODE_SECONDPASS_BEST:
1462 cpi->compressor_speed = 0;
1467 cpi->auto_worst_q = 1;
1469 cpi->oxcf.worst_allowed_q = q_trans[oxcf->worst_allowed_q];
1470 cpi->oxcf.best_allowed_q = q_trans[oxcf->best_allowed_q];
1471 cpi->oxcf.cq_level = q_trans[cpi->oxcf.cq_level];
1473 if (oxcf->fixed_q >= 0)
1475 if (oxcf->worst_allowed_q < 0)
1476 cpi->oxcf.fixed_q = q_trans[0];
1478 cpi->oxcf.fixed_q = q_trans[oxcf->worst_allowed_q];
1480 if (oxcf->alt_q < 0)
1481 cpi->oxcf.alt_q = q_trans[0];
1483 cpi->oxcf.alt_q = q_trans[oxcf->alt_q];
1485 if (oxcf->key_q < 0)
1486 cpi->oxcf.key_q = q_trans[0];
1488 cpi->oxcf.key_q = q_trans[oxcf->key_q];
1490 if (oxcf->gold_q < 0)
1491 cpi->oxcf.gold_q = q_trans[0];
1493 cpi->oxcf.gold_q = q_trans[oxcf->gold_q];
1497 cpi->baseline_gf_interval =
1498 cpi->oxcf.alt_freq ? cpi->oxcf.alt_freq : DEFAULT_GF_INTERVAL;
1500 cpi->ref_frame_flags = VP8_ALTR_FRAME | VP8_GOLD_FRAME | VP8_LAST_FRAME;
1502 cm->refresh_golden_frame = 0;
1503 cm->refresh_last_frame = 1;
1504 cm->refresh_entropy_probs = 1;
1506 #if (CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
1507 cpi->oxcf.token_partitions = 3;
1510 if (cpi->oxcf.token_partitions >= 0 && cpi->oxcf.token_partitions <= 3)
1511 cm->multi_token_partition =
1512 (TOKEN_PARTITION) cpi->oxcf.token_partitions;
1514 setup_features(cpi);
1519 for (i = 0; i < MAX_MB_SEGMENTS; i++)
1520 cpi->segment_encode_breakout[i] = cpi->oxcf.encode_breakout;
1523 /* At the moment the first order values may not be > MAXQ */
1524 if (cpi->oxcf.fixed_q > MAXQ)
1525 cpi->oxcf.fixed_q = MAXQ;
1527 /* local file playback mode == really big buffer */
1528 if (cpi->oxcf.end_usage == USAGE_LOCAL_FILE_PLAYBACK)
1530 cpi->oxcf.starting_buffer_level = 60000;
1531 cpi->oxcf.optimal_buffer_level = 60000;
1532 cpi->oxcf.maximum_buffer_size = 240000;
1533 cpi->oxcf.starting_buffer_level_in_ms = 60000;
1534 cpi->oxcf.optimal_buffer_level_in_ms = 60000;
1535 cpi->oxcf.maximum_buffer_size_in_ms = 240000;
1538 /* Convert target bandwidth from Kbit/s to Bit/s */
1539 cpi->oxcf.target_bandwidth *= 1000;
1541 cpi->oxcf.starting_buffer_level =
1542 rescale(cpi->oxcf.starting_buffer_level,
1543 cpi->oxcf.target_bandwidth, 1000);
1545 /* Set or reset optimal and maximum buffer levels. */
1546 if (cpi->oxcf.optimal_buffer_level == 0)
1547 cpi->oxcf.optimal_buffer_level = cpi->oxcf.target_bandwidth / 8;
1549 cpi->oxcf.optimal_buffer_level =
1550 rescale(cpi->oxcf.optimal_buffer_level,
1551 cpi->oxcf.target_bandwidth, 1000);
1553 if (cpi->oxcf.maximum_buffer_size == 0)
1554 cpi->oxcf.maximum_buffer_size = cpi->oxcf.target_bandwidth / 8;
1556 cpi->oxcf.maximum_buffer_size =
1557 rescale(cpi->oxcf.maximum_buffer_size,
1558 cpi->oxcf.target_bandwidth, 1000);
1560 /* Set up frame rate and related parameters rate control values. */
1561 vp8_new_frame_rate(cpi, cpi->frame_rate);
1563 /* Set absolute upper and lower quality limits */
1564 cpi->worst_quality = cpi->oxcf.worst_allowed_q;
1565 cpi->best_quality = cpi->oxcf.best_allowed_q;
1567 /* active values should only be modified if out of new range */
1568 if (cpi->active_worst_quality > cpi->oxcf.worst_allowed_q)
1570 cpi->active_worst_quality = cpi->oxcf.worst_allowed_q;
1573 else if (cpi->active_worst_quality < cpi->oxcf.best_allowed_q)
1575 cpi->active_worst_quality = cpi->oxcf.best_allowed_q;
1577 if (cpi->active_best_quality < cpi->oxcf.best_allowed_q)
1579 cpi->active_best_quality = cpi->oxcf.best_allowed_q;
1582 else if (cpi->active_best_quality > cpi->oxcf.worst_allowed_q)
1584 cpi->active_best_quality = cpi->oxcf.worst_allowed_q;
1587 cpi->buffered_mode = cpi->oxcf.optimal_buffer_level > 0;
1589 cpi->cq_target_quality = cpi->oxcf.cq_level;
1591 /* Only allow dropped frames in buffered mode */
1592 cpi->drop_frames_allowed = cpi->oxcf.allow_df && cpi->buffered_mode;
1594 cpi->target_bandwidth = cpi->oxcf.target_bandwidth;
1597 cm->Width = cpi->oxcf.Width;
1598 cm->Height = cpi->oxcf.Height;
1600 /* TODO(jkoleszar): if an internal spatial resampling is active,
1601 * and we downsize the input image, maybe we should clear the
1602 * internal scale immediately rather than waiting for it to
1606 /* VP8 sharpness level mapping 0-7 (vs 0-10 in general VPx dialogs) */
1607 if (cpi->oxcf.Sharpness > 7)
1608 cpi->oxcf.Sharpness = 7;
1610 cm->sharpness_level = cpi->oxcf.Sharpness;
1612 if (cm->horiz_scale != NORMAL || cm->vert_scale != NORMAL)
1614 int UNINITIALIZED_IS_SAFE(hr), UNINITIALIZED_IS_SAFE(hs);
1615 int UNINITIALIZED_IS_SAFE(vr), UNINITIALIZED_IS_SAFE(vs);
1617 Scale2Ratio(cm->horiz_scale, &hr, &hs);
1618 Scale2Ratio(cm->vert_scale, &vr, &vs);
1620 /* always go to the next whole number */
1621 cm->Width = (hs - 1 + cpi->oxcf.Width * hr) / hs;
1622 cm->Height = (vs - 1 + cpi->oxcf.Height * vr) / vs;
1625 if (last_w != cpi->oxcf.Width || last_h != cpi->oxcf.Height)
1626 cpi->force_next_frame_intra = 1;
1628 if (((cm->Width + 15) & 0xfffffff0) !=
1629 cm->yv12_fb[cm->lst_fb_idx].y_width ||
1630 ((cm->Height + 15) & 0xfffffff0) !=
1631 cm->yv12_fb[cm->lst_fb_idx].y_height ||
1632 cm->yv12_fb[cm->lst_fb_idx].y_width == 0)
1634 dealloc_raw_frame_buffers(cpi);
1635 alloc_raw_frame_buffers(cpi);
1636 vp8_alloc_compressor_data(cpi);
1639 if (cpi->oxcf.fixed_q >= 0)
1641 cpi->last_q[0] = cpi->oxcf.fixed_q;
1642 cpi->last_q[1] = cpi->oxcf.fixed_q;
1645 cpi->Speed = cpi->oxcf.cpu_used;
1647 /* force to allowlag to 0 if lag_in_frames is 0; */
1648 if (cpi->oxcf.lag_in_frames == 0)
1650 cpi->oxcf.allow_lag = 0;
1652 /* Limit on lag buffers as these are not currently dynamically allocated */
1653 else if (cpi->oxcf.lag_in_frames > MAX_LAG_BUFFERS)
1654 cpi->oxcf.lag_in_frames = MAX_LAG_BUFFERS;
1657 cpi->alt_ref_source = NULL;
1658 cpi->is_src_frame_alt_ref = 0;
1660 #if CONFIG_TEMPORAL_DENOISING
1661 if (cpi->oxcf.noise_sensitivity)
1663 if (!cpi->denoiser.yv12_mc_running_avg.buffer_alloc)
1665 int width = (cpi->oxcf.Width + 15) & ~15;
1666 int height = (cpi->oxcf.Height + 15) & ~15;
1667 vp8_denoiser_allocate(&cpi->denoiser, width, height);
1673 /* Experimental RD Code */
1674 cpi->frame_distortion = 0;
1675 cpi->last_frame_distortion = 0;
1680 #define M_LOG2_E 0.693147180559945309417
1681 #define log2f(x) (log (x) / (float) M_LOG2_E)
1682 static void cal_mvsadcosts(int *mvsadcost[2])
1686 mvsadcost [0] [0] = 300;
1687 mvsadcost [1] [0] = 300;
1691 double z = 256 * (2 * (log2f(8 * i) + .6));
1692 mvsadcost [0][i] = (int) z;
1693 mvsadcost [1][i] = (int) z;
1694 mvsadcost [0][-i] = (int) z;
1695 mvsadcost [1][-i] = (int) z;
1697 while (++i <= mvfp_max);
1700 struct VP8_COMP* vp8_create_compressor(VP8_CONFIG *oxcf)
1707 cpi = vpx_memalign(32, sizeof(VP8_COMP));
1708 /* Check that the CPI instance is valid */
1714 vpx_memset(cpi, 0, sizeof(VP8_COMP));
1716 if (setjmp(cm->error.jmp))
1718 cpi->common.error.setjmp = 0;
1719 vp8_remove_compressor(&cpi);
1723 cpi->common.error.setjmp = 1;
1725 CHECK_MEM_ERROR(cpi->mb.ss, vpx_calloc(sizeof(search_site), (MAX_MVSEARCH_STEPS * 8) + 1));
1727 vp8_create_common(&cpi->common);
1729 init_config(cpi, oxcf);
1731 memcpy(cpi->base_skip_false_prob, vp8cx_base_skip_false_prob, sizeof(vp8cx_base_skip_false_prob));
1732 cpi->common.current_video_frame = 0;
1733 cpi->kf_overspend_bits = 0;
1734 cpi->kf_bitrate_adjustment = 0;
1735 cpi->frames_till_gf_update_due = 0;
1736 cpi->gf_overspend_bits = 0;
1737 cpi->non_gf_bitrate_adjustment = 0;
1738 cpi->prob_last_coded = 128;
1739 cpi->prob_gf_coded = 128;
1740 cpi->prob_intra_coded = 63;
1742 /* Prime the recent reference frame usage counters.
1743 * Hereafter they will be maintained as a sort of moving average
1745 cpi->recent_ref_frame_usage[INTRA_FRAME] = 1;
1746 cpi->recent_ref_frame_usage[LAST_FRAME] = 1;
1747 cpi->recent_ref_frame_usage[GOLDEN_FRAME] = 1;
1748 cpi->recent_ref_frame_usage[ALTREF_FRAME] = 1;
1750 /* Set reference frame sign bias for ALTREF frame to 1 (for now) */
1751 cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = 1;
1753 cpi->twopass.gf_decay_rate = 0;
1754 cpi->baseline_gf_interval = DEFAULT_GF_INTERVAL;
1756 cpi->gold_is_last = 0 ;
1757 cpi->alt_is_last = 0 ;
1758 cpi->gold_is_alt = 0 ;
1760 /* allocate memory for storing last frame's MVs for MV prediction. */
1761 CHECK_MEM_ERROR(cpi->lfmv, vpx_calloc((cpi->common.mb_rows+2) * (cpi->common.mb_cols+2), sizeof(int_mv)));
1762 CHECK_MEM_ERROR(cpi->lf_ref_frame_sign_bias, vpx_calloc((cpi->common.mb_rows+2) * (cpi->common.mb_cols+2), sizeof(int)));
1763 CHECK_MEM_ERROR(cpi->lf_ref_frame, vpx_calloc((cpi->common.mb_rows+2) * (cpi->common.mb_cols+2), sizeof(int)));
1765 /* Create the encoder segmentation map and set all entries to 0 */
1766 CHECK_MEM_ERROR(cpi->segmentation_map, vpx_calloc(cpi->common.mb_rows * cpi->common.mb_cols, 1));
1768 CHECK_MEM_ERROR(cpi->active_map, vpx_calloc(cpi->common.mb_rows * cpi->common.mb_cols, 1));
1769 vpx_memset(cpi->active_map , 1, (cpi->common.mb_rows * cpi->common.mb_cols));
1770 cpi->active_map_enabled = 0;
1773 /* Experimental code for lagged and one pass */
1774 /* Initialise one_pass GF frames stats */
1775 /* Update stats used for GF selection */
1778 cpi->one_pass_frame_index = 0;
1780 for (i = 0; i < MAX_LAG_BUFFERS; i++)
1782 cpi->one_pass_frame_stats[i].frames_so_far = 0;
1783 cpi->one_pass_frame_stats[i].frame_intra_error = 0.0;
1784 cpi->one_pass_frame_stats[i].frame_coded_error = 0.0;
1785 cpi->one_pass_frame_stats[i].frame_pcnt_inter = 0.0;
1786 cpi->one_pass_frame_stats[i].frame_pcnt_motion = 0.0;
1787 cpi->one_pass_frame_stats[i].frame_mvr = 0.0;
1788 cpi->one_pass_frame_stats[i].frame_mvr_abs = 0.0;
1789 cpi->one_pass_frame_stats[i].frame_mvc = 0.0;
1790 cpi->one_pass_frame_stats[i].frame_mvc_abs = 0.0;
1795 /* Should we use the cyclic refresh method.
1796 * Currently this is tied to error resilliant mode
1798 cpi->cyclic_refresh_mode_enabled = cpi->oxcf.error_resilient_mode;
1799 cpi->cyclic_refresh_mode_max_mbs_perframe = (cpi->common.mb_rows * cpi->common.mb_cols) / 40;
1800 cpi->cyclic_refresh_mode_index = 0;
1801 cpi->cyclic_refresh_q = 32;
1803 if (cpi->cyclic_refresh_mode_enabled)
1805 CHECK_MEM_ERROR(cpi->cyclic_refresh_map, vpx_calloc((cpi->common.mb_rows * cpi->common.mb_cols), 1));
1808 cpi->cyclic_refresh_map = (signed char *) NULL;
1810 #ifdef ENTROPY_STATS
1811 init_context_counters();
1814 /*Initialize the feed-forward activity masking.*/
1815 cpi->activity_avg = 90<<12;
1817 /* Give a sensible default for the first frame. */
1818 cpi->frames_since_key = 8;
1819 cpi->key_frame_frequency = cpi->oxcf.key_freq;
1820 cpi->this_key_frame_forced = 0;
1821 cpi->next_key_frame_forced = 0;
1823 cpi->source_alt_ref_pending = 0;
1824 cpi->source_alt_ref_active = 0;
1825 cpi->common.refresh_alt_ref_frame = 0;
1827 cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
1828 #if CONFIG_INTERNAL_STATS
1829 cpi->b_calculate_ssimg = 0;
1834 if (cpi->b_calculate_psnr)
1836 cpi->total_sq_error = 0.0;
1837 cpi->total_sq_error2 = 0.0;
1842 cpi->totalp_y = 0.0;
1843 cpi->totalp_u = 0.0;
1844 cpi->totalp_v = 0.0;
1846 cpi->tot_recode_hits = 0;
1847 cpi->summed_quality = 0;
1848 cpi->summed_weights = 0;
1851 if (cpi->b_calculate_ssimg)
1853 cpi->total_ssimg_y = 0;
1854 cpi->total_ssimg_u = 0;
1855 cpi->total_ssimg_v = 0;
1856 cpi->total_ssimg_all = 0;
1861 cpi->first_time_stamp_ever = 0x7FFFFFFF;
1863 cpi->frames_till_gf_update_due = 0;
1864 cpi->key_frame_count = 1;
1866 cpi->ni_av_qi = cpi->oxcf.worst_allowed_q;
1869 cpi->total_byte_count = 0;
1871 cpi->drop_frame = 0;
1873 cpi->rate_correction_factor = 1.0;
1874 cpi->key_frame_rate_correction_factor = 1.0;
1875 cpi->gf_rate_correction_factor = 1.0;
1876 cpi->twopass.est_max_qcorrection_factor = 1.0;
1878 for (i = 0; i < KEY_FRAME_CONTEXT; i++)
1880 cpi->prior_key_frame_distance[i] = (int)cpi->output_frame_rate;
1883 #ifdef OUTPUT_YUV_SRC
1884 yuv_file = fopen("bd.yuv", "ab");
1888 framepsnr = fopen("framepsnr.stt", "a");
1889 kf_list = fopen("kf_list.stt", "w");
1892 cpi->output_pkt_list = oxcf->output_pkt_list;
1894 #if !(CONFIG_REALTIME_ONLY)
1898 vp8_init_first_pass(cpi);
1900 else if (cpi->pass == 2)
1902 size_t packet_sz = sizeof(FIRSTPASS_STATS);
1903 int packets = oxcf->two_pass_stats_in.sz / packet_sz;
1905 cpi->twopass.stats_in_start = oxcf->two_pass_stats_in.buf;
1906 cpi->twopass.stats_in = cpi->twopass.stats_in_start;
1907 cpi->twopass.stats_in_end = (void*)((char *)cpi->twopass.stats_in
1908 + (packets - 1) * packet_sz);
1909 vp8_init_second_pass(cpi);
1914 if (cpi->compressor_speed == 2)
1916 cpi->avg_encode_time = 0;
1917 cpi->avg_pick_mode_time = 0;
1920 vp8_set_speed_features(cpi);
1922 /* Set starting values of RD threshold multipliers (128 = *1) */
1923 for (i = 0; i < MAX_MODES; i++)
1925 cpi->rd_thresh_mult[i] = 128;
1928 #ifdef ENTROPY_STATS
1929 init_mv_ref_counts();
1932 #if CONFIG_MULTITHREAD
1933 vp8cx_create_encoder_threads(cpi);
1936 cpi->fn_ptr[BLOCK_16X16].sdf = vp8_sad16x16;
1937 cpi->fn_ptr[BLOCK_16X16].vf = vp8_variance16x16;
1938 cpi->fn_ptr[BLOCK_16X16].svf = vp8_sub_pixel_variance16x16;
1939 cpi->fn_ptr[BLOCK_16X16].svf_halfpix_h = vp8_variance_halfpixvar16x16_h;
1940 cpi->fn_ptr[BLOCK_16X16].svf_halfpix_v = vp8_variance_halfpixvar16x16_v;
1941 cpi->fn_ptr[BLOCK_16X16].svf_halfpix_hv = vp8_variance_halfpixvar16x16_hv;
1942 cpi->fn_ptr[BLOCK_16X16].sdx3f = vp8_sad16x16x3;
1943 cpi->fn_ptr[BLOCK_16X16].sdx8f = vp8_sad16x16x8;
1944 cpi->fn_ptr[BLOCK_16X16].sdx4df = vp8_sad16x16x4d;
1946 cpi->fn_ptr[BLOCK_16X8].sdf = vp8_sad16x8;
1947 cpi->fn_ptr[BLOCK_16X8].vf = vp8_variance16x8;
1948 cpi->fn_ptr[BLOCK_16X8].svf = vp8_sub_pixel_variance16x8;
1949 cpi->fn_ptr[BLOCK_16X8].svf_halfpix_h = NULL;
1950 cpi->fn_ptr[BLOCK_16X8].svf_halfpix_v = NULL;
1951 cpi->fn_ptr[BLOCK_16X8].svf_halfpix_hv = NULL;
1952 cpi->fn_ptr[BLOCK_16X8].sdx3f = vp8_sad16x8x3;
1953 cpi->fn_ptr[BLOCK_16X8].sdx8f = vp8_sad16x8x8;
1954 cpi->fn_ptr[BLOCK_16X8].sdx4df = vp8_sad16x8x4d;
1956 cpi->fn_ptr[BLOCK_8X16].sdf = vp8_sad8x16;
1957 cpi->fn_ptr[BLOCK_8X16].vf = vp8_variance8x16;
1958 cpi->fn_ptr[BLOCK_8X16].svf = vp8_sub_pixel_variance8x16;
1959 cpi->fn_ptr[BLOCK_8X16].svf_halfpix_h = NULL;
1960 cpi->fn_ptr[BLOCK_8X16].svf_halfpix_v = NULL;
1961 cpi->fn_ptr[BLOCK_8X16].svf_halfpix_hv = NULL;
1962 cpi->fn_ptr[BLOCK_8X16].sdx3f = vp8_sad8x16x3;
1963 cpi->fn_ptr[BLOCK_8X16].sdx8f = vp8_sad8x16x8;
1964 cpi->fn_ptr[BLOCK_8X16].sdx4df = vp8_sad8x16x4d;
1966 cpi->fn_ptr[BLOCK_8X8].sdf = vp8_sad8x8;
1967 cpi->fn_ptr[BLOCK_8X8].vf = vp8_variance8x8;
1968 cpi->fn_ptr[BLOCK_8X8].svf = vp8_sub_pixel_variance8x8;
1969 cpi->fn_ptr[BLOCK_8X8].svf_halfpix_h = NULL;
1970 cpi->fn_ptr[BLOCK_8X8].svf_halfpix_v = NULL;
1971 cpi->fn_ptr[BLOCK_8X8].svf_halfpix_hv = NULL;
1972 cpi->fn_ptr[BLOCK_8X8].sdx3f = vp8_sad8x8x3;
1973 cpi->fn_ptr[BLOCK_8X8].sdx8f = vp8_sad8x8x8;
1974 cpi->fn_ptr[BLOCK_8X8].sdx4df = vp8_sad8x8x4d;
1976 cpi->fn_ptr[BLOCK_4X4].sdf = vp8_sad4x4;
1977 cpi->fn_ptr[BLOCK_4X4].vf = vp8_variance4x4;
1978 cpi->fn_ptr[BLOCK_4X4].svf = vp8_sub_pixel_variance4x4;
1979 cpi->fn_ptr[BLOCK_4X4].svf_halfpix_h = NULL;
1980 cpi->fn_ptr[BLOCK_4X4].svf_halfpix_v = NULL;
1981 cpi->fn_ptr[BLOCK_4X4].svf_halfpix_hv = NULL;
1982 cpi->fn_ptr[BLOCK_4X4].sdx3f = vp8_sad4x4x3;
1983 cpi->fn_ptr[BLOCK_4X4].sdx8f = vp8_sad4x4x8;
1984 cpi->fn_ptr[BLOCK_4X4].sdx4df = vp8_sad4x4x4d;
1986 #if ARCH_X86 || ARCH_X86_64
1987 cpi->fn_ptr[BLOCK_16X16].copymem = vp8_copy32xn;
1988 cpi->fn_ptr[BLOCK_16X8].copymem = vp8_copy32xn;
1989 cpi->fn_ptr[BLOCK_8X16].copymem = vp8_copy32xn;
1990 cpi->fn_ptr[BLOCK_8X8].copymem = vp8_copy32xn;
1991 cpi->fn_ptr[BLOCK_4X4].copymem = vp8_copy32xn;
1994 cpi->full_search_sad = vp8_full_search_sad;
1995 cpi->diamond_search_sad = vp8_diamond_search_sad;
1996 cpi->refining_search_sad = vp8_refining_search_sad;
1998 /* make sure frame 1 is okay */
1999 cpi->error_bins[0] = cpi->common.MBs;
2001 /* vp8cx_init_quantizer() is first called here. Add check in
2002 * vp8cx_frame_init_quantizer() so that vp8cx_init_quantizer is only
2003 * called later when needed. This will avoid unnecessary calls of
2004 * vp8cx_init_quantizer() for every frame.
2006 vp8cx_init_quantizer(cpi);
2008 vp8_loop_filter_init(cm);
2010 cpi->common.error.setjmp = 0;
2012 #if CONFIG_MULTI_RES_ENCODING
2014 /* Calculate # of MBs in a row in lower-resolution level image. */
2015 if (cpi->oxcf.mr_encoder_id > 0)
2016 vp8_cal_low_res_mb_cols(cpi);
2020 /* setup RD costs to MACROBLOCK struct */
2022 cpi->mb.mvcost[0] = &cpi->rd_costs.mvcosts[0][mv_max+1];
2023 cpi->mb.mvcost[1] = &cpi->rd_costs.mvcosts[1][mv_max+1];
2024 cpi->mb.mvsadcost[0] = &cpi->rd_costs.mvsadcosts[0][mvfp_max+1];
2025 cpi->mb.mvsadcost[1] = &cpi->rd_costs.mvsadcosts[1][mvfp_max+1];
2027 cal_mvsadcosts(cpi->mb.mvsadcost);
2029 cpi->mb.mbmode_cost = cpi->rd_costs.mbmode_cost;
2030 cpi->mb.intra_uv_mode_cost = cpi->rd_costs.intra_uv_mode_cost;
2031 cpi->mb.bmode_costs = cpi->rd_costs.bmode_costs;
2032 cpi->mb.inter_bmode_costs = cpi->rd_costs.inter_bmode_costs;
2033 cpi->mb.token_costs = cpi->rd_costs.token_costs;
2039 void vp8_remove_compressor(VP8_COMP **ptr)
2041 VP8_COMP *cpi = *ptr;
2046 if (cpi && (cpi->common.current_video_frame > 0))
2048 #if !(CONFIG_REALTIME_ONLY)
2052 vp8_end_second_pass(cpi);
2057 #ifdef ENTROPY_STATS
2058 print_context_counters();
2059 print_tree_update_probs();
2060 print_mode_context();
2063 #if CONFIG_INTERNAL_STATS
2067 FILE *f = fopen("opsnr.stt", "a");
2068 double time_encoded = (cpi->last_end_time_stamp_seen
2069 - cpi->first_time_stamp_ever) / 10000000.000;
2070 double total_encode_time = (cpi->time_receive_data +
2071 cpi->time_compress_data) / 1000.000;
2072 double dr = (double)cpi->bytes * 8.0 / 1000.0 / time_encoded;
2074 if (cpi->b_calculate_psnr)
2076 YV12_BUFFER_CONFIG *lst_yv12 =
2077 &cpi->common.yv12_fb[cpi->common.lst_fb_idx];
2079 if (cpi->oxcf.number_of_layers > 1)
2083 fprintf(f, "Layer\tBitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\t"
2084 "GLPsnrP\tVPXSSIM\t\n");
2085 for (i=0; i<cpi->oxcf.number_of_layers; i++)
2087 double dr = (double)cpi->bytes_in_layer[i] *
2088 8.0 / 1000.0 / time_encoded;
2089 double samples = 3.0 / 2 * cpi->frames_in_layer[i] *
2090 lst_yv12->y_width * lst_yv12->y_height;
2091 double total_psnr = vp8_mse2psnr(samples, 255.0,
2092 cpi->total_error2[i]);
2093 double total_psnr2 = vp8_mse2psnr(samples, 255.0,
2094 cpi->total_error2_p[i]);
2095 double total_ssim = 100 * pow(cpi->sum_ssim[i] /
2096 cpi->sum_weights[i], 8.0);
2098 fprintf(f, "%5d\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
2101 cpi->sum_psnr[i] / cpi->frames_in_layer[i],
2103 cpi->sum_psnr_p[i] / cpi->frames_in_layer[i],
2104 total_psnr2, total_ssim);
2109 double samples = 3.0 / 2 * cpi->count *
2110 lst_yv12->y_width * lst_yv12->y_height;
2111 double total_psnr = vp8_mse2psnr(samples, 255.0,
2112 cpi->total_sq_error);
2113 double total_psnr2 = vp8_mse2psnr(samples, 255.0,
2114 cpi->total_sq_error2);
2115 double total_ssim = 100 * pow(cpi->summed_quality /
2116 cpi->summed_weights, 8.0);
2118 fprintf(f, "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\t"
2119 "GLPsnrP\tVPXSSIM\t Time(us)\n");
2120 fprintf(f, "%7.3f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
2122 dr, cpi->total / cpi->count, total_psnr,
2123 cpi->totalp / cpi->count, total_psnr2,
2124 total_ssim, total_encode_time);
2128 if (cpi->b_calculate_ssimg)
2130 if (cpi->oxcf.number_of_layers > 1)
2134 fprintf(f, "Layer\tBitRate\tSSIM_Y\tSSIM_U\tSSIM_V\tSSIM_A\t"
2136 for (i=0; i<cpi->oxcf.number_of_layers; i++)
2138 double dr = (double)cpi->bytes_in_layer[i] *
2139 8.0 / 1000.0 / time_encoded;
2140 fprintf(f, "%5d\t%7.3f\t%6.4f\t"
2141 "%6.4f\t%6.4f\t%6.4f\t%8.0f\n",
2143 cpi->total_ssimg_y_in_layer[i] /
2144 cpi->frames_in_layer[i],
2145 cpi->total_ssimg_u_in_layer[i] /
2146 cpi->frames_in_layer[i],
2147 cpi->total_ssimg_v_in_layer[i] /
2148 cpi->frames_in_layer[i],
2149 cpi->total_ssimg_all_in_layer[i] /
2150 cpi->frames_in_layer[i],
2156 fprintf(f, "BitRate\tSSIM_Y\tSSIM_U\tSSIM_V\tSSIM_A\t"
2158 fprintf(f, "%7.3f\t%6.4f\t%6.4f\t%6.4f\t%6.4f\t%8.0f\n", dr,
2159 cpi->total_ssimg_y / cpi->count,
2160 cpi->total_ssimg_u / cpi->count,
2161 cpi->total_ssimg_v / cpi->count,
2162 cpi->total_ssimg_all / cpi->count, total_encode_time);
2168 f = fopen("qskip.stt", "a");
2169 fprintf(f, "minq:%d -maxq:%d skiptrue:skipfalse = %d:%d\n", cpi->oxcf.best_allowed_q, cpi->oxcf.worst_allowed_q, skiptruecount, skipfalsecount);
2180 if (cpi->compressor_speed == 2)
2183 FILE *f = fopen("cxspeed.stt", "a");
2184 cnt_pm /= cpi->common.MBs;
2186 for (i = 0; i < 16; i++)
2187 fprintf(f, "%5d", frames_at_speed[i]);
2198 extern int count_mb_seg[4];
2199 FILE *f = fopen("modes.stt", "a");
2200 double dr = (double)cpi->frame_rate * (double)bytes * (double)8 / (double)count / (double)1000 ;
2201 fprintf(f, "intra_mode in Intra Frames:\n");
2202 fprintf(f, "Y: %8d, %8d, %8d, %8d, %8d\n", y_modes[0], y_modes[1], y_modes[2], y_modes[3], y_modes[4]);
2203 fprintf(f, "UV:%8d, %8d, %8d, %8d\n", uv_modes[0], uv_modes[1], uv_modes[2], uv_modes[3]);
2208 for (i = 0; i < 10; i++)
2209 fprintf(f, "%8d, ", b_modes[i]);
2215 fprintf(f, "Modes in Inter Frames:\n");
2216 fprintf(f, "Y: %8d, %8d, %8d, %8d, %8d, %8d, %8d, %8d, %8d, %8d\n",
2217 inter_y_modes[0], inter_y_modes[1], inter_y_modes[2], inter_y_modes[3], inter_y_modes[4],
2218 inter_y_modes[5], inter_y_modes[6], inter_y_modes[7], inter_y_modes[8], inter_y_modes[9]);
2219 fprintf(f, "UV:%8d, %8d, %8d, %8d\n", inter_uv_modes[0], inter_uv_modes[1], inter_uv_modes[2], inter_uv_modes[3]);
2224 for (i = 0; i < 15; i++)
2225 fprintf(f, "%8d, ", inter_b_modes[i]);
2230 fprintf(f, "P:%8d, %8d, %8d, %8d\n", count_mb_seg[0], count_mb_seg[1], count_mb_seg[2], count_mb_seg[3]);
2231 fprintf(f, "PB:%8d, %8d, %8d, %8d\n", inter_b_modes[LEFT4X4], inter_b_modes[ABOVE4X4], inter_b_modes[ZERO4X4], inter_b_modes[NEW4X4]);
2239 #ifdef ENTROPY_STATS
2242 FILE *fmode = fopen("modecontext.c", "w");
2244 fprintf(fmode, "\n#include \"entropymode.h\"\n\n");
2245 fprintf(fmode, "const unsigned int vp8_kf_default_bmode_counts ");
2246 fprintf(fmode, "[VP8_BINTRAMODES] [VP8_BINTRAMODES] [VP8_BINTRAMODES] =\n{\n");
2248 for (i = 0; i < 10; i++)
2251 fprintf(fmode, " { /* Above Mode : %d */\n", i);
2253 for (j = 0; j < 10; j++)
2256 fprintf(fmode, " {");
2258 for (k = 0; k < 10; k++)
2260 if (!intra_mode_stats[i][j][k])
2261 fprintf(fmode, " %5d, ", 1);
2263 fprintf(fmode, " %5d, ", intra_mode_stats[i][j][k]);
2266 fprintf(fmode, "}, /* left_mode %d */\n", j);
2270 fprintf(fmode, " },\n");
2274 fprintf(fmode, "};\n");
2280 #if defined(SECTIONBITS_OUTPUT)
2285 FILE *f = fopen("tokenbits.stt", "a");
2287 for (i = 0; i < 28; i++)
2288 fprintf(f, "%8d", (int)(Sectionbits[i] / 256));
2298 printf("\n_pick_loop_filter_level:%d\n", cpi->time_pick_lpf / 1000);
2299 printf("\n_frames recive_data encod_mb_row compress_frame Total\n");
2300 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);
2306 #if CONFIG_MULTITHREAD
2307 vp8cx_remove_encoder_threads(cpi);
2310 #if CONFIG_TEMPORAL_DENOISING
2311 vp8_denoiser_free(&cpi->denoiser);
2313 dealloc_compressor_data(cpi);
2314 vpx_free(cpi->mb.ss);
2316 vpx_free(cpi->cyclic_refresh_map);
2318 vp8_remove_common(&cpi->common);
2322 #ifdef OUTPUT_YUV_SRC
2342 static uint64_t calc_plane_error(unsigned char *orig, int orig_stride,
2343 unsigned char *recon, int recon_stride,
2344 unsigned int cols, unsigned int rows)
2346 unsigned int row, col;
2347 uint64_t total_sse = 0;
2350 for (row = 0; row + 16 <= rows; row += 16)
2352 for (col = 0; col + 16 <= cols; col += 16)
2356 vp8_mse16x16(orig + col, orig_stride,
2357 recon + col, recon_stride,
2362 /* Handle odd-sized width */
2365 unsigned int border_row, border_col;
2366 unsigned char *border_orig = orig;
2367 unsigned char *border_recon = recon;
2369 for (border_row = 0; border_row < 16; border_row++)
2371 for (border_col = col; border_col < cols; border_col++)
2373 diff = border_orig[border_col] - border_recon[border_col];
2374 total_sse += diff * diff;
2377 border_orig += orig_stride;
2378 border_recon += recon_stride;
2382 orig += orig_stride * 16;
2383 recon += recon_stride * 16;
2386 /* Handle odd-sized height */
2387 for (; row < rows; row++)
2389 for (col = 0; col < cols; col++)
2391 diff = orig[col] - recon[col];
2392 total_sse += diff * diff;
2395 orig += orig_stride;
2396 recon += recon_stride;
2399 vp8_clear_system_state();
2404 static void generate_psnr_packet(VP8_COMP *cpi)
2406 YV12_BUFFER_CONFIG *orig = cpi->Source;
2407 YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
2408 struct vpx_codec_cx_pkt pkt;
2411 unsigned int width = cpi->common.Width;
2412 unsigned int height = cpi->common.Height;
2414 pkt.kind = VPX_CODEC_PSNR_PKT;
2415 sse = calc_plane_error(orig->y_buffer, orig->y_stride,
2416 recon->y_buffer, recon->y_stride,
2418 pkt.data.psnr.sse[0] = sse;
2419 pkt.data.psnr.sse[1] = sse;
2420 pkt.data.psnr.samples[0] = width * height;
2421 pkt.data.psnr.samples[1] = width * height;
2423 width = (width + 1) / 2;
2424 height = (height + 1) / 2;
2426 sse = calc_plane_error(orig->u_buffer, orig->uv_stride,
2427 recon->u_buffer, recon->uv_stride,
2429 pkt.data.psnr.sse[0] += sse;
2430 pkt.data.psnr.sse[2] = sse;
2431 pkt.data.psnr.samples[0] += width * height;
2432 pkt.data.psnr.samples[2] = width * height;
2434 sse = calc_plane_error(orig->v_buffer, orig->uv_stride,
2435 recon->v_buffer, recon->uv_stride,
2437 pkt.data.psnr.sse[0] += sse;
2438 pkt.data.psnr.sse[3] = sse;
2439 pkt.data.psnr.samples[0] += width * height;
2440 pkt.data.psnr.samples[3] = width * height;
2442 for (i = 0; i < 4; i++)
2443 pkt.data.psnr.psnr[i] = vp8_mse2psnr(pkt.data.psnr.samples[i], 255.0,
2444 pkt.data.psnr.sse[i]);
2446 vpx_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
2450 int vp8_use_as_reference(VP8_COMP *cpi, int ref_frame_flags)
2452 if (ref_frame_flags > 7)
2455 cpi->ref_frame_flags = ref_frame_flags;
2458 int vp8_update_reference(VP8_COMP *cpi, int ref_frame_flags)
2460 if (ref_frame_flags > 7)
2463 cpi->common.refresh_golden_frame = 0;
2464 cpi->common.refresh_alt_ref_frame = 0;
2465 cpi->common.refresh_last_frame = 0;
2467 if (ref_frame_flags & VP8_LAST_FRAME)
2468 cpi->common.refresh_last_frame = 1;
2470 if (ref_frame_flags & VP8_GOLD_FRAME)
2471 cpi->common.refresh_golden_frame = 1;
2473 if (ref_frame_flags & VP8_ALTR_FRAME)
2474 cpi->common.refresh_alt_ref_frame = 1;
2479 int vp8_get_reference(VP8_COMP *cpi, enum vpx_ref_frame_type ref_frame_flag, YV12_BUFFER_CONFIG *sd)
2481 VP8_COMMON *cm = &cpi->common;
2484 if (ref_frame_flag == VP8_LAST_FRAME)
2485 ref_fb_idx = cm->lst_fb_idx;
2486 else if (ref_frame_flag == VP8_GOLD_FRAME)
2487 ref_fb_idx = cm->gld_fb_idx;
2488 else if (ref_frame_flag == VP8_ALTR_FRAME)
2489 ref_fb_idx = cm->alt_fb_idx;
2493 vp8_yv12_copy_frame(&cm->yv12_fb[ref_fb_idx], sd);
2497 int vp8_set_reference(VP8_COMP *cpi, enum vpx_ref_frame_type ref_frame_flag, YV12_BUFFER_CONFIG *sd)
2499 VP8_COMMON *cm = &cpi->common;
2503 if (ref_frame_flag == VP8_LAST_FRAME)
2504 ref_fb_idx = cm->lst_fb_idx;
2505 else if (ref_frame_flag == VP8_GOLD_FRAME)
2506 ref_fb_idx = cm->gld_fb_idx;
2507 else if (ref_frame_flag == VP8_ALTR_FRAME)
2508 ref_fb_idx = cm->alt_fb_idx;
2512 vp8_yv12_copy_frame(sd, &cm->yv12_fb[ref_fb_idx]);
2516 int vp8_update_entropy(VP8_COMP *cpi, int update)
2518 VP8_COMMON *cm = &cpi->common;
2519 cm->refresh_entropy_probs = update;
2526 void vp8_write_yuv_frame(const char *name, YV12_BUFFER_CONFIG *s)
2528 FILE *yuv_file = fopen(name, "ab");
2529 unsigned char *src = s->y_buffer;
2530 int h = s->y_height;
2534 fwrite(src, s->y_width, 1, yuv_file);
2544 fwrite(src, s->uv_width, 1, yuv_file);
2545 src += s->uv_stride;
2554 fwrite(src, s->uv_width, 1, yuv_file);
2555 src += s->uv_stride;
2564 static void scale_and_extend_source(YV12_BUFFER_CONFIG *sd, VP8_COMP *cpi)
2566 VP8_COMMON *cm = &cpi->common;
2568 /* are we resizing the image */
2569 if (cm->horiz_scale != 0 || cm->vert_scale != 0)
2571 #if CONFIG_SPATIAL_RESAMPLING
2572 int UNINITIALIZED_IS_SAFE(hr), UNINITIALIZED_IS_SAFE(hs);
2573 int UNINITIALIZED_IS_SAFE(vr), UNINITIALIZED_IS_SAFE(vs);
2576 if (cm->vert_scale == 3)
2581 Scale2Ratio(cm->horiz_scale, &hr, &hs);
2582 Scale2Ratio(cm->vert_scale, &vr, &vs);
2584 vp8_scale_frame(sd, &cpi->scaled_source, cm->temp_scale_frame.y_buffer,
2585 tmp_height, hs, hr, vs, vr, 0);
2587 vp8_yv12_extend_frame_borders(&cpi->scaled_source);
2588 cpi->Source = &cpi->scaled_source;
2596 static int resize_key_frame(VP8_COMP *cpi)
2598 #if CONFIG_SPATIAL_RESAMPLING
2599 VP8_COMMON *cm = &cpi->common;
2601 /* Do we need to apply resampling for one pass cbr.
2602 * In one pass this is more limited than in two pass cbr
2603 * The test and any change is only made one per key frame sequence
2605 if (cpi->oxcf.allow_spatial_resampling && (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER))
2607 int UNINITIALIZED_IS_SAFE(hr), UNINITIALIZED_IS_SAFE(hs);
2608 int UNINITIALIZED_IS_SAFE(vr), UNINITIALIZED_IS_SAFE(vs);
2609 int new_width, new_height;
2611 /* If we are below the resample DOWN watermark then scale down a
2614 if (cpi->buffer_level < (cpi->oxcf.resample_down_water_mark * cpi->oxcf.optimal_buffer_level / 100))
2616 cm->horiz_scale = (cm->horiz_scale < ONETWO) ? cm->horiz_scale + 1 : ONETWO;
2617 cm->vert_scale = (cm->vert_scale < ONETWO) ? cm->vert_scale + 1 : ONETWO;
2619 /* Should we now start scaling back up */
2620 else if (cpi->buffer_level > (cpi->oxcf.resample_up_water_mark * cpi->oxcf.optimal_buffer_level / 100))
2622 cm->horiz_scale = (cm->horiz_scale > NORMAL) ? cm->horiz_scale - 1 : NORMAL;
2623 cm->vert_scale = (cm->vert_scale > NORMAL) ? cm->vert_scale - 1 : NORMAL;
2626 /* Get the new hieght and width */
2627 Scale2Ratio(cm->horiz_scale, &hr, &hs);
2628 Scale2Ratio(cm->vert_scale, &vr, &vs);
2629 new_width = ((hs - 1) + (cpi->oxcf.Width * hr)) / hs;
2630 new_height = ((vs - 1) + (cpi->oxcf.Height * vr)) / vs;
2632 /* If the image size has changed we need to reallocate the buffers
2633 * and resample the source image
2635 if ((cm->Width != new_width) || (cm->Height != new_height))
2637 cm->Width = new_width;
2638 cm->Height = new_height;
2639 vp8_alloc_compressor_data(cpi);
2640 scale_and_extend_source(cpi->un_scaled_source, cpi);
2650 static void update_alt_ref_frame_stats(VP8_COMP *cpi)
2652 VP8_COMMON *cm = &cpi->common;
2654 /* Select an interval before next GF or altref */
2655 if (!cpi->auto_gold)
2656 cpi->frames_till_gf_update_due = DEFAULT_GF_INTERVAL;
2658 if ((cpi->pass != 2) && cpi->frames_till_gf_update_due)
2660 cpi->current_gf_interval = cpi->frames_till_gf_update_due;
2662 /* Set the bits per frame that we should try and recover in
2663 * subsequent inter frames to account for the extra GF spend...
2664 * note that his does not apply for GF updates that occur
2665 * coincident with a key frame as the extra cost of key frames is
2666 * dealt with elsewhere.
2668 cpi->gf_overspend_bits += cpi->projected_frame_size;
2669 cpi->non_gf_bitrate_adjustment = cpi->gf_overspend_bits / cpi->frames_till_gf_update_due;
2672 /* Update data structure that monitors level of reference to last GF */
2673 vpx_memset(cpi->gf_active_flags, 1, (cm->mb_rows * cm->mb_cols));
2674 cpi->gf_active_count = cm->mb_rows * cm->mb_cols;
2676 /* this frame refreshes means next frames don't unless specified by user */
2677 cpi->common.frames_since_golden = 0;
2679 /* Clear the alternate reference update pending flag. */
2680 cpi->source_alt_ref_pending = 0;
2682 /* Set the alternate refernce frame active flag */
2683 cpi->source_alt_ref_active = 1;
2687 static void update_golden_frame_stats(VP8_COMP *cpi)
2689 VP8_COMMON *cm = &cpi->common;
2691 /* Update the Golden frame usage counts. */
2692 if (cm->refresh_golden_frame)
2694 /* Select an interval before next GF */
2695 if (!cpi->auto_gold)
2696 cpi->frames_till_gf_update_due = DEFAULT_GF_INTERVAL;
2698 if ((cpi->pass != 2) && (cpi->frames_till_gf_update_due > 0))
2700 cpi->current_gf_interval = cpi->frames_till_gf_update_due;
2702 /* Set the bits per frame that we should try and recover in
2703 * subsequent inter frames to account for the extra GF spend...
2704 * note that his does not apply for GF updates that occur
2705 * coincident with a key frame as the extra cost of key frames
2706 * is dealt with elsewhere.
2708 if ((cm->frame_type != KEY_FRAME) && !cpi->source_alt_ref_active)
2710 /* Calcluate GF bits to be recovered
2711 * Projected size - av frame bits available for inter
2712 * frames for clip as a whole
2714 cpi->gf_overspend_bits += (cpi->projected_frame_size - cpi->inter_frame_target);
2717 cpi->non_gf_bitrate_adjustment = cpi->gf_overspend_bits / cpi->frames_till_gf_update_due;
2721 /* Update data structure that monitors level of reference to last GF */
2722 vpx_memset(cpi->gf_active_flags, 1, (cm->mb_rows * cm->mb_cols));
2723 cpi->gf_active_count = cm->mb_rows * cm->mb_cols;
2725 /* this frame refreshes means next frames don't unless specified by
2728 cm->refresh_golden_frame = 0;
2729 cpi->common.frames_since_golden = 0;
2731 cpi->recent_ref_frame_usage[INTRA_FRAME] = 1;
2732 cpi->recent_ref_frame_usage[LAST_FRAME] = 1;
2733 cpi->recent_ref_frame_usage[GOLDEN_FRAME] = 1;
2734 cpi->recent_ref_frame_usage[ALTREF_FRAME] = 1;
2736 /* ******** Fixed Q test code only ************ */
2737 /* If we are going to use the ALT reference for the next group of
2738 * frames set a flag to say so.
2740 if (cpi->oxcf.fixed_q >= 0 &&
2741 cpi->oxcf.play_alternate && !cpi->common.refresh_alt_ref_frame)
2743 cpi->source_alt_ref_pending = 1;
2744 cpi->frames_till_gf_update_due = cpi->baseline_gf_interval;
2747 if (!cpi->source_alt_ref_pending)
2748 cpi->source_alt_ref_active = 0;
2750 /* Decrement count down till next gf */
2751 if (cpi->frames_till_gf_update_due > 0)
2752 cpi->frames_till_gf_update_due--;
2755 else if (!cpi->common.refresh_alt_ref_frame)
2757 /* Decrement count down till next gf */
2758 if (cpi->frames_till_gf_update_due > 0)
2759 cpi->frames_till_gf_update_due--;
2761 if (cpi->common.frames_till_alt_ref_frame)
2762 cpi->common.frames_till_alt_ref_frame --;
2764 cpi->common.frames_since_golden ++;
2766 if (cpi->common.frames_since_golden > 1)
2768 cpi->recent_ref_frame_usage[INTRA_FRAME] += cpi->count_mb_ref_frame_usage[INTRA_FRAME];
2769 cpi->recent_ref_frame_usage[LAST_FRAME] += cpi->count_mb_ref_frame_usage[LAST_FRAME];
2770 cpi->recent_ref_frame_usage[GOLDEN_FRAME] += cpi->count_mb_ref_frame_usage[GOLDEN_FRAME];
2771 cpi->recent_ref_frame_usage[ALTREF_FRAME] += cpi->count_mb_ref_frame_usage[ALTREF_FRAME];
2776 /* This function updates the reference frame probability estimates that
2777 * will be used during mode selection
2779 static void update_rd_ref_frame_probs(VP8_COMP *cpi)
2781 VP8_COMMON *cm = &cpi->common;
2783 const int *const rfct = cpi->count_mb_ref_frame_usage;
2784 const int rf_intra = rfct[INTRA_FRAME];
2785 const int rf_inter = rfct[LAST_FRAME] + rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME];
2787 if (cm->frame_type == KEY_FRAME)
2789 cpi->prob_intra_coded = 255;
2790 cpi->prob_last_coded = 128;
2791 cpi->prob_gf_coded = 128;
2793 else if (!(rf_intra + rf_inter))
2795 cpi->prob_intra_coded = 63;
2796 cpi->prob_last_coded = 128;
2797 cpi->prob_gf_coded = 128;
2800 /* update reference frame costs since we can do better than what we got
2803 if (cpi->oxcf.number_of_layers == 1)
2805 if (cpi->common.refresh_alt_ref_frame)
2807 cpi->prob_intra_coded += 40;
2808 cpi->prob_last_coded = 200;
2809 cpi->prob_gf_coded = 1;
2811 else if (cpi->common.frames_since_golden == 0)
2813 cpi->prob_last_coded = 214;
2815 else if (cpi->common.frames_since_golden == 1)
2817 cpi->prob_last_coded = 192;
2818 cpi->prob_gf_coded = 220;
2820 else if (cpi->source_alt_ref_active)
2822 cpi->prob_gf_coded -= 20;
2824 if (cpi->prob_gf_coded < 10)
2825 cpi->prob_gf_coded = 10;
2827 if (!cpi->source_alt_ref_active)
2828 cpi->prob_gf_coded = 255;
2833 /* 1 = key, 0 = inter */
2834 static int decide_key_frame(VP8_COMP *cpi)
2836 VP8_COMMON *cm = &cpi->common;
2838 int code_key_frame = 0;
2842 if (cpi->Speed > 11)
2845 /* Clear down mmx registers */
2846 vp8_clear_system_state();
2848 if ((cpi->compressor_speed == 2) && (cpi->Speed >= 5) && (cpi->sf.RD == 0))
2850 double change = 1.0 * abs((int)(cpi->intra_error - cpi->last_intra_error)) / (1 + cpi->last_intra_error);
2851 double change2 = 1.0 * abs((int)(cpi->prediction_error - cpi->last_prediction_error)) / (1 + cpi->last_prediction_error);
2852 double minerror = cm->MBs * 256;
2856 if (10 * cpi->intra_error / (1 + cpi->prediction_error) < 15
2857 && cpi->prediction_error > minerror
2858 && (change > .25 || change2 > .25))
2860 FILE *f = fopen("intra_inter.stt", "a");
2862 if (cpi->prediction_error <= 0)
2863 cpi->prediction_error = 1;
2865 fprintf(f, "%d %d %d %d %14.4f\n",
2866 cm->current_video_frame,
2867 (int) cpi->prediction_error,
2868 (int) cpi->intra_error,
2869 (int)((10 * cpi->intra_error) / cpi->prediction_error),
2877 cpi->last_intra_error = cpi->intra_error;
2878 cpi->last_prediction_error = cpi->prediction_error;
2880 if (10 * cpi->intra_error / (1 + cpi->prediction_error) < 15
2881 && cpi->prediction_error > minerror
2882 && (change > .25 || change2 > .25))
2884 /*(change > 1.4 || change < .75)&& cpi->this_frame_percent_intra > cpi->last_frame_percent_intra + 3*/
2892 /* If the following are true we might as well code a key frame */
2893 if (((cpi->this_frame_percent_intra == 100) &&
2894 (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra + 2))) ||
2895 ((cpi->this_frame_percent_intra > 95) &&
2896 (cpi->this_frame_percent_intra >= (cpi->last_frame_percent_intra + 5))))
2900 /* in addition if the following are true and this is not a golden frame
2901 * then code a key frame Note that on golden frames there often seems
2902 * to be a pop in intra useage anyway hence this restriction is
2903 * designed to prevent spurious key frames. The Intra pop needs to be
2906 else if (((cpi->this_frame_percent_intra > 60) &&
2907 (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra * 2))) ||
2908 ((cpi->this_frame_percent_intra > 75) &&
2909 (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra * 3 / 2))) ||
2910 ((cpi->this_frame_percent_intra > 90) &&
2911 (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra + 10))))
2913 if (!cm->refresh_golden_frame)
2917 return code_key_frame;
2921 #if !(CONFIG_REALTIME_ONLY)
2922 static void Pass1Encode(VP8_COMP *cpi, unsigned long *size, unsigned char *dest, unsigned int *frame_flags)
2927 vp8_set_quantizer(cpi, 26);
2929 vp8_first_pass(cpi);
2934 void write_cx_frame_to_file(YV12_BUFFER_CONFIG *frame, int this_frame)
2937 /* write the frame */
2942 sprintf(filename, "cx\\y%04d.raw", this_frame);
2943 yframe = fopen(filename, "wb");
2945 for (i = 0; i < frame->y_height; i++)
2946 fwrite(frame->y_buffer + i * frame->y_stride, frame->y_width, 1, yframe);
2949 sprintf(filename, "cx\\u%04d.raw", this_frame);
2950 yframe = fopen(filename, "wb");
2952 for (i = 0; i < frame->uv_height; i++)
2953 fwrite(frame->u_buffer + i * frame->uv_stride, frame->uv_width, 1, yframe);
2956 sprintf(filename, "cx\\v%04d.raw", this_frame);
2957 yframe = fopen(filename, "wb");
2959 for (i = 0; i < frame->uv_height; i++)
2960 fwrite(frame->v_buffer + i * frame->uv_stride, frame->uv_width, 1, yframe);
2965 /* return of 0 means drop frame */
2967 /* Function to test for conditions that indeicate we should loop
2968 * back and recode a frame.
2970 static int recode_loop_test( VP8_COMP *cpi,
2971 int high_limit, int low_limit,
2972 int q, int maxq, int minq )
2974 int force_recode = 0;
2975 VP8_COMMON *cm = &cpi->common;
2977 /* Is frame recode allowed at all
2978 * Yes if either recode mode 1 is selected or mode two is selcted
2979 * and the frame is a key frame. golden frame or alt_ref_frame
2981 if ( (cpi->sf.recode_loop == 1) ||
2982 ( (cpi->sf.recode_loop == 2) &&
2983 ( (cm->frame_type == KEY_FRAME) ||
2984 cm->refresh_golden_frame ||
2985 cm->refresh_alt_ref_frame ) ) )
2987 /* General over and under shoot tests */
2988 if ( ((cpi->projected_frame_size > high_limit) && (q < maxq)) ||
2989 ((cpi->projected_frame_size < low_limit) && (q > minq)) )
2993 /* Special Constrained quality tests */
2994 else if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY)
2996 /* Undershoot and below auto cq level */
2997 if ( (q > cpi->cq_target_quality) &&
2998 (cpi->projected_frame_size <
2999 ((cpi->this_frame_target * 7) >> 3)))
3003 /* Severe undershoot and between auto and user cq level */
3004 else if ( (q > cpi->oxcf.cq_level) &&
3005 (cpi->projected_frame_size < cpi->min_frame_bandwidth) &&
3006 (cpi->active_best_quality > cpi->oxcf.cq_level))
3009 cpi->active_best_quality = cpi->oxcf.cq_level;
3014 return force_recode;
3017 static void update_reference_frames(VP8_COMMON *cm)
3019 YV12_BUFFER_CONFIG *yv12_fb = cm->yv12_fb;
3021 /* At this point the new frame has been encoded.
3022 * If any buffer copy / swapping is signaled it should be done here.
3025 if (cm->frame_type == KEY_FRAME)
3027 yv12_fb[cm->new_fb_idx].flags |= VP8_GOLD_FRAME | VP8_ALTR_FRAME ;
3029 yv12_fb[cm->gld_fb_idx].flags &= ~VP8_GOLD_FRAME;
3030 yv12_fb[cm->alt_fb_idx].flags &= ~VP8_ALTR_FRAME;
3032 cm->alt_fb_idx = cm->gld_fb_idx = cm->new_fb_idx;
3034 else /* For non key frames */
3036 if (cm->refresh_alt_ref_frame)
3038 assert(!cm->copy_buffer_to_arf);
3040 cm->yv12_fb[cm->new_fb_idx].flags |= VP8_ALTR_FRAME;
3041 cm->yv12_fb[cm->alt_fb_idx].flags &= ~VP8_ALTR_FRAME;
3042 cm->alt_fb_idx = cm->new_fb_idx;
3044 else if (cm->copy_buffer_to_arf)
3046 assert(!(cm->copy_buffer_to_arf & ~0x3));
3048 if (cm->copy_buffer_to_arf == 1)
3050 if(cm->alt_fb_idx != cm->lst_fb_idx)
3052 yv12_fb[cm->lst_fb_idx].flags |= VP8_ALTR_FRAME;
3053 yv12_fb[cm->alt_fb_idx].flags &= ~VP8_ALTR_FRAME;
3054 cm->alt_fb_idx = cm->lst_fb_idx;
3057 else /* if (cm->copy_buffer_to_arf == 2) */
3059 if(cm->alt_fb_idx != cm->gld_fb_idx)
3061 yv12_fb[cm->gld_fb_idx].flags |= VP8_ALTR_FRAME;
3062 yv12_fb[cm->alt_fb_idx].flags &= ~VP8_ALTR_FRAME;
3063 cm->alt_fb_idx = cm->gld_fb_idx;
3068 if (cm->refresh_golden_frame)
3070 assert(!cm->copy_buffer_to_gf);
3072 cm->yv12_fb[cm->new_fb_idx].flags |= VP8_GOLD_FRAME;
3073 cm->yv12_fb[cm->gld_fb_idx].flags &= ~VP8_GOLD_FRAME;
3074 cm->gld_fb_idx = cm->new_fb_idx;
3076 else if (cm->copy_buffer_to_gf)
3078 assert(!(cm->copy_buffer_to_arf & ~0x3));
3080 if (cm->copy_buffer_to_gf == 1)
3082 if(cm->gld_fb_idx != cm->lst_fb_idx)
3084 yv12_fb[cm->lst_fb_idx].flags |= VP8_GOLD_FRAME;
3085 yv12_fb[cm->gld_fb_idx].flags &= ~VP8_GOLD_FRAME;
3086 cm->gld_fb_idx = cm->lst_fb_idx;
3089 else /* if (cm->copy_buffer_to_gf == 2) */
3091 if(cm->alt_fb_idx != cm->gld_fb_idx)
3093 yv12_fb[cm->alt_fb_idx].flags |= VP8_GOLD_FRAME;
3094 yv12_fb[cm->gld_fb_idx].flags &= ~VP8_GOLD_FRAME;
3095 cm->gld_fb_idx = cm->alt_fb_idx;
3101 if (cm->refresh_last_frame)
3103 cm->yv12_fb[cm->new_fb_idx].flags |= VP8_LAST_FRAME;
3104 cm->yv12_fb[cm->lst_fb_idx].flags &= ~VP8_LAST_FRAME;
3105 cm->lst_fb_idx = cm->new_fb_idx;
3109 void vp8_loopfilter_frame(VP8_COMP *cpi, VP8_COMMON *cm)
3111 const FRAME_TYPE frame_type = cm->frame_type;
3115 cm->filter_level = 0;
3119 struct vpx_usec_timer timer;
3121 vp8_clear_system_state();
3123 vpx_usec_timer_start(&timer);
3124 if (cpi->sf.auto_filter == 0)
3125 vp8cx_pick_filter_level_fast(cpi->Source, cpi);
3128 vp8cx_pick_filter_level(cpi->Source, cpi);
3130 if (cm->filter_level > 0)
3132 vp8cx_set_alt_lf_level(cpi, cm->filter_level);
3135 vpx_usec_timer_mark(&timer);
3136 cpi->time_pick_lpf += vpx_usec_timer_elapsed(&timer);
3139 #if CONFIG_MULTITHREAD
3140 if (cpi->b_multi_threaded)
3141 sem_post(&cpi->h_event_end_lpf); /* signal that we have set filter_level */
3144 if (cm->filter_level > 0)
3146 vp8_loop_filter_frame(cm, &cpi->mb.e_mbd, frame_type);
3149 vp8_yv12_extend_frame_borders(cm->frame_to_show);
3150 #if CONFIG_TEMPORAL_DENOISING
3151 if (cpi->oxcf.noise_sensitivity)
3155 /* we shouldn't have to keep multiple copies as we know in advance which
3156 * buffer we should start - for now to get something up and running
3157 * I've chosen to copy the buffers
3159 if (cm->frame_type == KEY_FRAME)
3162 vp8_yv12_copy_frame(
3164 &cpi->denoiser.yv12_running_avg[LAST_FRAME]);
3166 vp8_yv12_extend_frame_borders(
3167 &cpi->denoiser.yv12_running_avg[LAST_FRAME]);
3169 for (i = 2; i < MAX_REF_FRAMES - 1; i++)
3170 vp8_yv12_copy_frame(
3172 &cpi->denoiser.yv12_running_avg[i]);
3174 else /* For non key frames */
3176 vp8_yv12_extend_frame_borders(
3177 &cpi->denoiser.yv12_running_avg[LAST_FRAME]);
3179 if (cm->refresh_alt_ref_frame || cm->copy_buffer_to_arf)
3181 vp8_yv12_copy_frame(
3182 &cpi->denoiser.yv12_running_avg[LAST_FRAME],
3183 &cpi->denoiser.yv12_running_avg[ALTREF_FRAME]);
3185 if (cm->refresh_golden_frame || cm->copy_buffer_to_gf)
3187 vp8_yv12_copy_frame(
3188 &cpi->denoiser.yv12_running_avg[LAST_FRAME],
3189 &cpi->denoiser.yv12_running_avg[GOLDEN_FRAME]);
3198 static void encode_frame_to_data_rate
3201 unsigned long *size,
3202 unsigned char *dest,
3203 unsigned char* dest_end,
3204 unsigned int *frame_flags
3208 int frame_over_shoot_limit;
3209 int frame_under_shoot_limit;
3214 VP8_COMMON *cm = &cpi->common;
3215 int active_worst_qchanged = 0;
3217 #if !(CONFIG_REALTIME_ONLY)
3221 int zbin_oq_low = 0;
3224 int overshoot_seen = 0;
3225 int undershoot_seen = 0;
3228 int drop_mark = cpi->oxcf.drop_frames_water_mark * cpi->oxcf.optimal_buffer_level / 100;
3229 int drop_mark75 = drop_mark * 2 / 3;
3230 int drop_mark50 = drop_mark / 4;
3231 int drop_mark25 = drop_mark / 8;
3234 /* Clear down mmx registers to allow floating point in what follows */
3235 vp8_clear_system_state();
3237 #if CONFIG_MULTITHREAD
3238 /* wait for the last picture loopfilter thread done */
3239 if (cpi->b_lpf_running)
3241 sem_wait(&cpi->h_event_end_lpf);
3242 cpi->b_lpf_running = 0;
3246 if(cpi->force_next_frame_intra)
3248 cm->frame_type = KEY_FRAME; /* delayed intra frame */
3249 cpi->force_next_frame_intra = 0;
3252 /* For an alt ref frame in 2 pass we skip the call to the second pass
3253 * function that sets the target bandwidth
3255 #if !(CONFIG_REALTIME_ONLY)
3259 if (cpi->common.refresh_alt_ref_frame)
3261 /* Per frame bit target for the alt ref frame */
3262 cpi->per_frame_bandwidth = cpi->twopass.gf_bits;
3263 /* per second target bitrate */
3264 cpi->target_bandwidth = cpi->twopass.gf_bits * cpi->output_frame_rate;
3269 cpi->per_frame_bandwidth = (int)(cpi->target_bandwidth / cpi->output_frame_rate);
3271 /* Default turn off buffer to buffer copying */
3272 cm->copy_buffer_to_gf = 0;
3273 cm->copy_buffer_to_arf = 0;
3275 /* Clear zbin over-quant value and mode boost values. */
3276 cpi->zbin_over_quant = 0;
3277 cpi->zbin_mode_boost = 0;
3279 /* Enable or disable mode based tweaking of the zbin
3280 * For 2 Pass Only used where GF/ARF prediction quality
3281 * is above a threshold
3283 cpi->zbin_mode_boost_enabled = 1;
3286 if ( cpi->gfu_boost <= 400 )
3288 cpi->zbin_mode_boost_enabled = 0;
3292 /* Current default encoder behaviour for the altref sign bias */
3293 if (cpi->source_alt_ref_active)
3294 cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = 1;
3296 cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = 0;
3298 /* Check to see if a key frame is signalled
3299 * For two pass with auto key frame enabled cm->frame_type may already
3300 * be set, but not for one pass.
3302 if ((cm->current_video_frame == 0) ||
3303 (cm->frame_flags & FRAMEFLAGS_KEY) ||
3304 (cpi->oxcf.auto_key && (cpi->frames_since_key % cpi->key_frame_frequency == 0)))
3306 /* Key frame from VFW/auto-keyframe/first frame */
3307 cm->frame_type = KEY_FRAME;
3310 #if CONFIG_MULTI_RES_ENCODING
3311 /* In multi-resolution encoding, frame_type is decided by lowest-resolution
3312 * encoder. Same frame_type is adopted while encoding at other resolution.
3314 if (cpi->oxcf.mr_encoder_id)
3317 ((LOWER_RES_FRAME_INFO*)cpi->oxcf.mr_low_res_mode_info)->frame_type;
3321 /* Set various flags etc to special state if it is a key frame */
3322 if (cm->frame_type == KEY_FRAME)
3326 // Set the loop filter deltas and segmentation map update
3327 setup_features(cpi);
3329 /* The alternate reference frame cannot be active for a key frame */
3330 cpi->source_alt_ref_active = 0;
3332 /* Reset the RD threshold multipliers to default of * 1 (128) */
3333 for (i = 0; i < MAX_MODES; i++)
3335 cpi->rd_thresh_mult[i] = 128;
3340 /* Experimental code for lagged compress and one pass
3341 * Initialise one_pass GF frames stats
3342 * Update stats used for GF selection
3345 cpi->one_pass_frame_index = cm->current_video_frame % MAX_LAG_BUFFERS;
3347 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frames_so_far = 0;
3348 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_intra_error = 0.0;
3349 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_coded_error = 0.0;
3350 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_pcnt_inter = 0.0;
3351 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_pcnt_motion = 0.0;
3352 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvr = 0.0;
3353 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvr_abs = 0.0;
3354 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvc = 0.0;
3355 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvc_abs = 0.0;
3359 update_rd_ref_frame_probs(cpi);
3361 if (cpi->drop_frames_allowed)
3363 /* The reset to decimation 0 is only done here for one pass.
3364 * Once it is set two pass leaves decimation on till the next kf.
3366 if ((cpi->buffer_level > drop_mark) && (cpi->decimation_factor > 0))
3367 cpi->decimation_factor --;
3369 if (cpi->buffer_level > drop_mark75 && cpi->decimation_factor > 0)
3370 cpi->decimation_factor = 1;
3372 else if (cpi->buffer_level < drop_mark25 && (cpi->decimation_factor == 2 || cpi->decimation_factor == 3))
3374 cpi->decimation_factor = 3;
3376 else if (cpi->buffer_level < drop_mark50 && (cpi->decimation_factor == 1 || cpi->decimation_factor == 2))
3378 cpi->decimation_factor = 2;
3380 else if (cpi->buffer_level < drop_mark75 && (cpi->decimation_factor == 0 || cpi->decimation_factor == 1))
3382 cpi->decimation_factor = 1;
3386 /* The following decimates the frame rate according to a regular
3387 * pattern (i.e. to 1/2 or 2/3 frame rate) This can be used to help
3388 * prevent buffer under-run in CBR mode. Alternatively it might be
3389 * desirable in some situations to drop frame rate but throw more bits
3392 * Note that dropping a key frame can be problematic if spatial
3393 * resampling is also active
3395 if (cpi->decimation_factor > 0)
3397 switch (cpi->decimation_factor)
3400 cpi->per_frame_bandwidth = cpi->per_frame_bandwidth * 3 / 2;
3403 cpi->per_frame_bandwidth = cpi->per_frame_bandwidth * 5 / 4;
3406 cpi->per_frame_bandwidth = cpi->per_frame_bandwidth * 5 / 4;
3410 /* Note that we should not throw out a key frame (especially when
3411 * spatial resampling is enabled).
3413 if ((cm->frame_type == KEY_FRAME))
3415 cpi->decimation_count = cpi->decimation_factor;
3417 else if (cpi->decimation_count > 0)
3419 cpi->decimation_count --;
3421 cpi->bits_off_target += cpi->av_per_frame_bandwidth;
3422 if (cpi->bits_off_target > cpi->oxcf.maximum_buffer_size)
3423 cpi->bits_off_target = cpi->oxcf.maximum_buffer_size;
3425 cm->current_video_frame++;
3426 cpi->frames_since_key++;
3428 #if CONFIG_INTERNAL_STATS
3432 cpi->buffer_level = cpi->bits_off_target;
3434 if (cpi->oxcf.number_of_layers > 1)
3438 /* Propagate bits saved by dropping the frame to higher
3441 for (i=cpi->current_layer+1; i<cpi->oxcf.number_of_layers; i++)
3443 LAYER_CONTEXT *lc = &cpi->layer_context[i];
3444 lc->bits_off_target += cpi->av_per_frame_bandwidth;
3445 if (lc->bits_off_target > lc->maximum_buffer_size)
3446 lc->bits_off_target = lc->maximum_buffer_size;
3447 lc->buffer_level = lc->bits_off_target;
3454 cpi->decimation_count = cpi->decimation_factor;
3457 cpi->decimation_count = 0;
3459 /* Decide how big to make the frame */
3460 if (!vp8_pick_frame_size(cpi))
3462 cm->current_video_frame++;
3463 cpi->frames_since_key++;
3467 /* Reduce active_worst_allowed_q for CBR if our buffer is getting too full.
3468 * This has a knock on effect on active best quality as well.
3469 * For CBR if the buffer reaches its maximum level then we can no longer
3470 * save up bits for later frames so we might as well use them up
3471 * on the current frame.
3473 if ((cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) &&
3474 (cpi->buffer_level >= cpi->oxcf.optimal_buffer_level) && cpi->buffered_mode)
3476 /* Max adjustment is 1/4 */
3477 int Adjustment = cpi->active_worst_quality / 4;
3483 if (cpi->buffer_level < cpi->oxcf.maximum_buffer_size)
3485 buff_lvl_step = (cpi->oxcf.maximum_buffer_size - cpi->oxcf.optimal_buffer_level) / Adjustment;
3488 Adjustment = (cpi->buffer_level - cpi->oxcf.optimal_buffer_level) / buff_lvl_step;
3493 cpi->active_worst_quality -= Adjustment;
3495 if(cpi->active_worst_quality < cpi->active_best_quality)
3496 cpi->active_worst_quality = cpi->active_best_quality;
3500 /* Set an active best quality and if necessary active worst quality
3501 * There is some odd behavior for one pass here that needs attention.
3503 if ( (cpi->pass == 2) || (cpi->ni_frames > 150))
3505 vp8_clear_system_state();
3507 Q = cpi->active_worst_quality;
3509 if ( cm->frame_type == KEY_FRAME )
3511 if ( cpi->pass == 2 )
3513 if (cpi->gfu_boost > 600)
3514 cpi->active_best_quality = kf_low_motion_minq[Q];
3516 cpi->active_best_quality = kf_high_motion_minq[Q];
3518 /* Special case for key frames forced because we have reached
3519 * the maximum key frame interval. Here force the Q to a range
3520 * based on the ambient Q to reduce the risk of popping
3522 if ( cpi->this_key_frame_forced )
3524 if ( cpi->active_best_quality > cpi->avg_frame_qindex * 7/8)
3525 cpi->active_best_quality = cpi->avg_frame_qindex * 7/8;
3526 else if ( cpi->active_best_quality < cpi->avg_frame_qindex >> 2 )
3527 cpi->active_best_quality = cpi->avg_frame_qindex >> 2;
3530 /* One pass more conservative */
3532 cpi->active_best_quality = kf_high_motion_minq[Q];
3535 else if (cpi->oxcf.number_of_layers==1 &&
3536 (cm->refresh_golden_frame || cpi->common.refresh_alt_ref_frame))
3538 /* Use the lower of cpi->active_worst_quality and recent
3539 * average Q as basis for GF/ARF Q limit unless last frame was
3542 if ( (cpi->frames_since_key > 1) &&
3543 (cpi->avg_frame_qindex < cpi->active_worst_quality) )
3545 Q = cpi->avg_frame_qindex;
3548 /* For constrained quality dont allow Q less than the cq level */
3549 if ( (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) &&
3550 (Q < cpi->cq_target_quality) )
3552 Q = cpi->cq_target_quality;
3555 if ( cpi->pass == 2 )
3557 if ( cpi->gfu_boost > 1000 )
3558 cpi->active_best_quality = gf_low_motion_minq[Q];
3559 else if ( cpi->gfu_boost < 400 )
3560 cpi->active_best_quality = gf_high_motion_minq[Q];
3562 cpi->active_best_quality = gf_mid_motion_minq[Q];
3564 /* Constrained quality use slightly lower active best. */
3565 if ( cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY )
3567 cpi->active_best_quality =
3568 cpi->active_best_quality * 15/16;
3571 /* One pass more conservative */
3573 cpi->active_best_quality = gf_high_motion_minq[Q];
3577 cpi->active_best_quality = inter_minq[Q];
3579 /* For the constant/constrained quality mode we dont want
3580 * q to fall below the cq level.
3582 if ((cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) &&
3583 (cpi->active_best_quality < cpi->cq_target_quality) )
3585 /* If we are strongly undershooting the target rate in the last
3586 * frames then use the user passed in cq value not the auto
3589 if ( cpi->rolling_actual_bits < cpi->min_frame_bandwidth )
3590 cpi->active_best_quality = cpi->oxcf.cq_level;
3592 cpi->active_best_quality = cpi->cq_target_quality;
3596 /* If CBR and the buffer is as full then it is reasonable to allow
3597 * higher quality on the frames to prevent bits just going to waste.
3599 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
3601 /* Note that the use of >= here elliminates the risk of a devide
3602 * by 0 error in the else if clause
3604 if (cpi->buffer_level >= cpi->oxcf.maximum_buffer_size)
3605 cpi->active_best_quality = cpi->best_quality;
3607 else if (cpi->buffer_level > cpi->oxcf.optimal_buffer_level)
3609 int Fraction = ((cpi->buffer_level - cpi->oxcf.optimal_buffer_level) * 128) / (cpi->oxcf.maximum_buffer_size - cpi->oxcf.optimal_buffer_level);
3610 int min_qadjustment = ((cpi->active_best_quality - cpi->best_quality) * Fraction) / 128;
3612 cpi->active_best_quality -= min_qadjustment;
3616 /* Make sure constrained quality mode limits are adhered to for the first
3617 * few frames of one pass encodes
3619 else if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY)
3621 if ( (cm->frame_type == KEY_FRAME) ||
3622 cm->refresh_golden_frame || cpi->common.refresh_alt_ref_frame )
3624 cpi->active_best_quality = cpi->best_quality;
3626 else if (cpi->active_best_quality < cpi->cq_target_quality)
3628 cpi->active_best_quality = cpi->cq_target_quality;
3632 /* Clip the active best and worst quality values to limits */
3633 if (cpi->active_worst_quality > cpi->worst_quality)
3634 cpi->active_worst_quality = cpi->worst_quality;
3636 if (cpi->active_best_quality < cpi->best_quality)
3637 cpi->active_best_quality = cpi->best_quality;
3639 if ( cpi->active_worst_quality < cpi->active_best_quality )
3640 cpi->active_worst_quality = cpi->active_best_quality;
3642 /* Determine initial Q to try */
3643 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
3645 #if !(CONFIG_REALTIME_ONLY)
3647 /* Set highest allowed value for Zbin over quant */
3648 if (cm->frame_type == KEY_FRAME)
3650 else if ((cpi->oxcf.number_of_layers == 1) && ((cm->refresh_alt_ref_frame ||
3651 (cm->refresh_golden_frame && !cpi->source_alt_ref_active))))
3656 zbin_oq_high = ZBIN_OQ_MAX;
3659 /* Setup background Q adjustment for error resilient mode.
3660 * For multi-layer encodes only enable this for the base layer.
3662 if (cpi->cyclic_refresh_mode_enabled && (cpi->current_layer==0))
3663 cyclic_background_refresh(cpi, Q, 0);
3665 vp8_compute_frame_size_bounds(cpi, &frame_under_shoot_limit, &frame_over_shoot_limit);
3667 #if !(CONFIG_REALTIME_ONLY)
3668 /* Limit Q range for the adaptive loop. */
3669 bottom_index = cpi->active_best_quality;
3670 top_index = cpi->active_worst_quality;
3671 q_low = cpi->active_best_quality;
3672 q_high = cpi->active_worst_quality;
3675 vp8_save_coding_context(cpi);
3679 scale_and_extend_source(cpi->un_scaled_source, cpi);
3681 #if !(CONFIG_REALTIME_ONLY) && CONFIG_POSTPROC && !(CONFIG_TEMPORAL_DENOISING)
3683 if (cpi->oxcf.noise_sensitivity > 0)
3688 switch (cpi->oxcf.noise_sensitivity)
3711 if (cm->frame_type == KEY_FRAME)
3713 vp8_de_noise(cpi->Source, cpi->Source, l , 1, 0);
3717 vp8_de_noise(cpi->Source, cpi->Source, l , 1, 0);
3719 src = cpi->Source->y_buffer;
3721 if (cpi->Source->y_stride < 0)
3723 src += cpi->Source->y_stride * (cpi->Source->y_height - 1);
3730 #ifdef OUTPUT_YUV_SRC
3731 vp8_write_yuv_frame(cpi->Source);
3736 vp8_clear_system_state();
3738 vp8_set_quantizer(cpi, Q);
3740 /* setup skip prob for costing in mode/mv decision */
3741 if (cpi->common.mb_no_coeff_skip)
3743 cpi->prob_skip_false = cpi->base_skip_false_prob[Q];
3745 if (cm->frame_type != KEY_FRAME)
3747 if (cpi->common.refresh_alt_ref_frame)
3749 if (cpi->last_skip_false_probs[2] != 0)
3750 cpi->prob_skip_false = cpi->last_skip_false_probs[2];
3753 if(cpi->last_skip_false_probs[2]!=0 && abs(Q- cpi->last_skip_probs_q[2])<=16 )
3754 cpi->prob_skip_false = cpi->last_skip_false_probs[2];
3755 else if (cpi->last_skip_false_probs[2]!=0)
3756 cpi->prob_skip_false = (cpi->last_skip_false_probs[2] + cpi->prob_skip_false ) / 2;
3759 else if (cpi->common.refresh_golden_frame)
3761 if (cpi->last_skip_false_probs[1] != 0)
3762 cpi->prob_skip_false = cpi->last_skip_false_probs[1];
3765 if(cpi->last_skip_false_probs[1]!=0 && abs(Q- cpi->last_skip_probs_q[1])<=16 )
3766 cpi->prob_skip_false = cpi->last_skip_false_probs[1];
3767 else if (cpi->last_skip_false_probs[1]!=0)
3768 cpi->prob_skip_false = (cpi->last_skip_false_probs[1] + cpi->prob_skip_false ) / 2;
3773 if (cpi->last_skip_false_probs[0] != 0)
3774 cpi->prob_skip_false = cpi->last_skip_false_probs[0];
3777 if(cpi->last_skip_false_probs[0]!=0 && abs(Q- cpi->last_skip_probs_q[0])<=16 )
3778 cpi->prob_skip_false = cpi->last_skip_false_probs[0];
3779 else if(cpi->last_skip_false_probs[0]!=0)
3780 cpi->prob_skip_false = (cpi->last_skip_false_probs[0] + cpi->prob_skip_false ) / 2;
3784 /* as this is for cost estimate, let's make sure it does not
3785 * go extreme eitehr way
3787 if (cpi->prob_skip_false < 5)
3788 cpi->prob_skip_false = 5;
3790 if (cpi->prob_skip_false > 250)
3791 cpi->prob_skip_false = 250;
3793 if (cpi->oxcf.number_of_layers == 1 && cpi->is_src_frame_alt_ref)
3794 cpi->prob_skip_false = 1;
3801 FILE *f = fopen("skip.stt", "a");
3802 fprintf(f, "%d, %d, %4d ", cpi->common.refresh_golden_frame, cpi->common.refresh_alt_ref_frame, cpi->prob_skip_false);
3810 if (cm->frame_type == KEY_FRAME)
3812 if(resize_key_frame(cpi))
3814 /* If the frame size has changed, need to reset Q, quantizer,
3815 * and background refresh.
3817 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
3818 if (cpi->cyclic_refresh_mode_enabled && (cpi->current_layer==0))
3819 cyclic_background_refresh(cpi, Q, 0);
3820 vp8_set_quantizer(cpi, Q);
3823 vp8_setup_key_frame(cpi);
3828 #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
3830 if(cpi->oxcf.error_resilient_mode)
3831 cm->refresh_entropy_probs = 0;
3833 if (cpi->oxcf.error_resilient_mode & VPX_ERROR_RESILIENT_PARTITIONS)
3835 if (cm->frame_type == KEY_FRAME)
3836 cm->refresh_entropy_probs = 1;
3839 if (cm->refresh_entropy_probs == 0)
3841 /* save a copy for later refresh */
3842 vpx_memcpy(&cm->lfc, &cm->fc, sizeof(cm->fc));
3845 vp8_update_coef_context(cpi);
3847 vp8_update_coef_probs(cpi);
3849 /* transform / motion compensation build reconstruction frame
3850 * +pack coef partitions
3852 vp8_encode_frame(cpi);
3854 /* cpi->projected_frame_size is not needed for RT mode */
3857 /* transform / motion compensation build reconstruction frame */
3858 vp8_encode_frame(cpi);
3860 cpi->projected_frame_size -= vp8_estimate_entropy_savings(cpi);
3861 cpi->projected_frame_size = (cpi->projected_frame_size > 0) ? cpi->projected_frame_size : 0;
3863 vp8_clear_system_state();
3865 /* Test to see if the stats generated for this frame indicate that
3866 * we should have coded a key frame (assuming that we didn't)!
3868 if (cpi->pass != 2 && cpi->oxcf.auto_key && cm->frame_type != KEY_FRAME)
3870 int key_frame_decision = decide_key_frame(cpi);
3872 if (cpi->compressor_speed == 2)
3874 /* we don't do re-encoding in realtime mode
3875 * if key frame is decided then we force it on next frame */
3876 cpi->force_next_frame_intra = key_frame_decision;
3878 #if !(CONFIG_REALTIME_ONLY)
3879 else if (key_frame_decision)
3881 /* Reset all our sizing numbers and recode */
3882 cm->frame_type = KEY_FRAME;
3884 vp8_pick_frame_size(cpi);
3886 /* Clear the Alt reference frame active flag when we have
3889 cpi->source_alt_ref_active = 0;
3891 // Set the loop filter deltas and segmentation map update
3892 setup_features(cpi);
3894 vp8_restore_coding_context(cpi);
3896 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
3898 vp8_compute_frame_size_bounds(cpi, &frame_under_shoot_limit, &frame_over_shoot_limit);
3900 /* Limit Q range for the adaptive loop. */
3901 bottom_index = cpi->active_best_quality;
3902 top_index = cpi->active_worst_quality;
3903 q_low = cpi->active_best_quality;
3904 q_high = cpi->active_worst_quality;
3914 vp8_clear_system_state();
3916 if (frame_over_shoot_limit == 0)
3917 frame_over_shoot_limit = 1;
3919 /* Are we are overshooting and up against the limit of active max Q. */
3920 if (((cpi->pass != 2) || (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)) &&
3921 (Q == cpi->active_worst_quality) &&
3922 (cpi->active_worst_quality < cpi->worst_quality) &&
3923 (cpi->projected_frame_size > frame_over_shoot_limit))
3925 int over_size_percent = ((cpi->projected_frame_size - frame_over_shoot_limit) * 100) / frame_over_shoot_limit;
3927 /* If so is there any scope for relaxing it */
3928 while ((cpi->active_worst_quality < cpi->worst_quality) && (over_size_percent > 0))
3930 cpi->active_worst_quality++;
3931 /* Assume 1 qstep = about 4% on frame size. */
3932 over_size_percent = (int)(over_size_percent * 0.96);
3934 #if !(CONFIG_REALTIME_ONLY)
3935 top_index = cpi->active_worst_quality;
3937 /* If we have updated the active max Q do not call
3938 * vp8_update_rate_correction_factors() this loop.
3940 active_worst_qchanged = 1;
3943 active_worst_qchanged = 0;
3945 #if !(CONFIG_REALTIME_ONLY)
3946 /* Special case handling for forced key frames */
3947 if ( (cm->frame_type == KEY_FRAME) && cpi->this_key_frame_forced )
3950 int kf_err = vp8_calc_ss_err(cpi->Source,
3951 &cm->yv12_fb[cm->new_fb_idx]);
3953 /* The key frame is not good enough */
3954 if ( kf_err > ((cpi->ambient_err * 7) >> 3) )
3957 q_high = (Q > q_low) ? (Q - 1) : q_low;
3960 Q = (q_high + q_low) >> 1;
3962 /* The key frame is much better than the previous frame */
3963 else if ( kf_err < (cpi->ambient_err >> 1) )
3966 q_low = (Q < q_high) ? (Q + 1) : q_high;
3969 Q = (q_high + q_low + 1) >> 1;
3972 /* Clamp Q to upper and lower limits: */
3981 /* Is the projected frame size out of range and are we allowed
3982 * to attempt to recode.
3984 else if ( recode_loop_test( cpi,
3985 frame_over_shoot_limit, frame_under_shoot_limit,
3986 Q, top_index, bottom_index ) )
3991 /* Frame size out of permitted range. Update correction factor
3992 * & compute new Q to try...
3995 /* Frame is too large */
3996 if (cpi->projected_frame_size > cpi->this_frame_target)
3998 /* Raise Qlow as to at least the current value */
3999 q_low = (Q < q_high) ? (Q + 1) : q_high;
4001 /* If we are using over quant do the same for zbin_oq_low */
4002 if (cpi->zbin_over_quant > 0)
4003 zbin_oq_low = (cpi->zbin_over_quant < zbin_oq_high) ? (cpi->zbin_over_quant + 1) : zbin_oq_high;
4005 if (undershoot_seen)
4007 /* Update rate_correction_factor unless
4008 * cpi->active_worst_quality has changed.
4010 if (!active_worst_qchanged)
4011 vp8_update_rate_correction_factors(cpi, 1);
4013 Q = (q_high + q_low + 1) / 2;
4015 /* Adjust cpi->zbin_over_quant (only allowed when Q
4019 cpi->zbin_over_quant = 0;
4022 zbin_oq_low = (cpi->zbin_over_quant < zbin_oq_high) ? (cpi->zbin_over_quant + 1) : zbin_oq_high;
4023 cpi->zbin_over_quant = (zbin_oq_high + zbin_oq_low) / 2;
4028 /* Update rate_correction_factor unless
4029 * cpi->active_worst_quality has changed.
4031 if (!active_worst_qchanged)
4032 vp8_update_rate_correction_factors(cpi, 0);
4034 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4036 while (((Q < q_low) || (cpi->zbin_over_quant < zbin_oq_low)) && (Retries < 10))
4038 vp8_update_rate_correction_factors(cpi, 0);
4039 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4046 /* Frame is too small */
4049 if (cpi->zbin_over_quant == 0)
4050 /* Lower q_high if not using over quant */
4051 q_high = (Q > q_low) ? (Q - 1) : q_low;
4053 /* else lower zbin_oq_high */
4054 zbin_oq_high = (cpi->zbin_over_quant > zbin_oq_low) ? (cpi->zbin_over_quant - 1) : zbin_oq_low;
4058 /* Update rate_correction_factor unless
4059 * cpi->active_worst_quality has changed.
4061 if (!active_worst_qchanged)
4062 vp8_update_rate_correction_factors(cpi, 1);
4064 Q = (q_high + q_low) / 2;
4066 /* Adjust cpi->zbin_over_quant (only allowed when Q
4070 cpi->zbin_over_quant = 0;
4072 cpi->zbin_over_quant = (zbin_oq_high + zbin_oq_low) / 2;
4076 /* Update rate_correction_factor unless
4077 * cpi->active_worst_quality has changed.
4079 if (!active_worst_qchanged)
4080 vp8_update_rate_correction_factors(cpi, 0);
4082 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4084 /* Special case reset for qlow for constrained quality.
4085 * This should only trigger where there is very substantial
4086 * undershoot on a frame and the auto cq level is above
4087 * the user passsed in value.
4089 if ( (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) &&
4095 while (((Q > q_high) || (cpi->zbin_over_quant > zbin_oq_high)) && (Retries < 10))
4097 vp8_update_rate_correction_factors(cpi, 0);
4098 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4103 undershoot_seen = 1;
4106 /* Clamp Q to upper and lower limits: */
4112 /* Clamp cpi->zbin_over_quant */
4113 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;
4121 if (cpi->is_src_frame_alt_ref)
4126 vp8_restore_coding_context(cpi);
4128 #if CONFIG_INTERNAL_STATS
4129 cpi->tot_recode_hits++;
4136 /* Experimental code for lagged and one pass
4137 * Update stats used for one pass GF selection
4140 cpi->one_pass_frame_stats[cpi->one_pass_frame_index].frame_coded_error = (double)cpi->prediction_error;
4141 cpi->one_pass_frame_stats[cpi->one_pass_frame_index].frame_intra_error = (double)cpi->intra_error;
4142 cpi->one_pass_frame_stats[cpi->one_pass_frame_index].frame_pcnt_inter = (double)(100 - cpi->this_frame_percent_intra) / 100.0;
4146 /* Special case code to reduce pulsing when key frames are forced at a
4147 * fixed interval. Note the reconstruction error if it is the frame before
4148 * the force key frame
4150 if ( cpi->next_key_frame_forced && (cpi->twopass.frames_to_key == 0) )
4152 cpi->ambient_err = vp8_calc_ss_err(cpi->Source,
4153 &cm->yv12_fb[cm->new_fb_idx]);
4156 /* This frame's MVs are saved and will be used in next frame's MV predictor.
4157 * Last frame has one more line(add to bottom) and one more column(add to
4158 * right) than cm->mip. The edge elements are initialized to 0.
4160 #if CONFIG_MULTI_RES_ENCODING
4161 if(!cpi->oxcf.mr_encoder_id && cm->show_frame)
4163 if(cm->show_frame) /* do not save for altref frame */
4168 /* Point to beginning of allocated MODE_INFO arrays. */
4169 MODE_INFO *tmp = cm->mip;
4171 if(cm->frame_type != KEY_FRAME)
4173 for (mb_row = 0; mb_row < cm->mb_rows+1; mb_row ++)
4175 for (mb_col = 0; mb_col < cm->mb_cols+1; mb_col ++)
4177 if(tmp->mbmi.ref_frame != INTRA_FRAME)
4178 cpi->lfmv[mb_col + mb_row*(cm->mode_info_stride+1)].as_int = tmp->mbmi.mv.as_int;
4180 cpi->lf_ref_frame_sign_bias[mb_col + mb_row*(cm->mode_info_stride+1)] = cm->ref_frame_sign_bias[tmp->mbmi.ref_frame];
4181 cpi->lf_ref_frame[mb_col + mb_row*(cm->mode_info_stride+1)] = tmp->mbmi.ref_frame;
4188 #if CONFIG_MULTI_RES_ENCODING
4189 vp8_cal_dissimilarity(cpi);
4192 /* Update the GF useage maps.
4193 * This is done after completing the compression of a frame when all
4194 * modes etc. are finalized but before loop filter
4196 if (cpi->oxcf.number_of_layers == 1)
4197 vp8_update_gf_useage_maps(cpi, cm, &cpi->mb);
4199 if (cm->frame_type == KEY_FRAME)
4200 cm->refresh_last_frame = 1;
4204 FILE *f = fopen("gfactive.stt", "a");
4205 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);
4210 /* For inter frames the current default behavior is that when
4211 * cm->refresh_golden_frame is set we copy the old GF over to the ARF buffer
4212 * This is purely an encoder decision at present.
4214 if (!cpi->oxcf.error_resilient_mode && cm->refresh_golden_frame)
4215 cm->copy_buffer_to_arf = 2;
4217 cm->copy_buffer_to_arf = 0;
4219 cm->frame_to_show = &cm->yv12_fb[cm->new_fb_idx];
4221 #if CONFIG_MULTITHREAD
4222 if (cpi->b_multi_threaded)
4224 /* start loopfilter in separate thread */
4225 sem_post(&cpi->h_event_start_lpf);
4226 cpi->b_lpf_running = 1;
4231 vp8_loopfilter_frame(cpi, cm);
4234 update_reference_frames(cm);
4236 #if !(CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
4237 if (cpi->oxcf.error_resilient_mode)
4239 cm->refresh_entropy_probs = 0;
4243 #if CONFIG_MULTITHREAD
4244 /* wait that filter_level is picked so that we can continue with stream packing */
4245 if (cpi->b_multi_threaded)
4246 sem_wait(&cpi->h_event_end_lpf);
4249 /* build the bitstream */
4250 vp8_pack_bitstream(cpi, dest, dest_end, size);
4252 #if CONFIG_MULTITHREAD
4253 /* if PSNR packets are generated we have to wait for the lpf */
4254 if (cpi->b_lpf_running && cpi->b_calculate_psnr)
4256 sem_wait(&cpi->h_event_end_lpf);
4257 cpi->b_lpf_running = 0;
4261 /* Move storing frame_type out of the above loop since it is also
4262 * needed in motion search besides loopfilter */
4263 cm->last_frame_type = cm->frame_type;
4265 /* Update rate control heuristics */
4266 cpi->total_byte_count += (*size);
4267 cpi->projected_frame_size = (*size) << 3;
4269 if (cpi->oxcf.number_of_layers > 1)
4272 for (i=cpi->current_layer+1; i<cpi->oxcf.number_of_layers; i++)
4273 cpi->layer_context[i].total_byte_count += (*size);
4276 if (!active_worst_qchanged)
4277 vp8_update_rate_correction_factors(cpi, 2);
4279 cpi->last_q[cm->frame_type] = cm->base_qindex;
4281 if (cm->frame_type == KEY_FRAME)
4283 vp8_adjust_key_frame_context(cpi);
4286 /* Keep a record of ambient average Q. */
4287 if (cm->frame_type != KEY_FRAME)
4288 cpi->avg_frame_qindex = (2 + 3 * cpi->avg_frame_qindex + cm->base_qindex) >> 2;
4290 /* Keep a record from which we can calculate the average Q excluding
4291 * GF updates and key frames
4293 if ((cm->frame_type != KEY_FRAME) && ((cpi->oxcf.number_of_layers > 1) ||
4294 (!cm->refresh_golden_frame && !cm->refresh_alt_ref_frame)))
4298 /* Calculate the average Q for normal inter frames (not key or GFU
4301 if ( cpi->pass == 2 )
4303 cpi->ni_tot_qi += Q;
4304 cpi->ni_av_qi = (cpi->ni_tot_qi / cpi->ni_frames);
4308 /* Damp value for first few frames */
4309 if (cpi->ni_frames > 150 )
4311 cpi->ni_tot_qi += Q;
4312 cpi->ni_av_qi = (cpi->ni_tot_qi / cpi->ni_frames);
4314 /* For one pass, early in the clip ... average the current frame Q
4315 * value with the worstq entered by the user as a dampening measure
4319 cpi->ni_tot_qi += Q;
4320 cpi->ni_av_qi = ((cpi->ni_tot_qi / cpi->ni_frames) + cpi->worst_quality + 1) / 2;
4323 /* If the average Q is higher than what was used in the last
4324 * frame (after going through the recode loop to keep the frame
4325 * size within range) then use the last frame value - 1. The -1
4326 * is designed to stop Q and hence the data rate, from
4327 * progressively falling away during difficult sections, but at
4328 * the same time reduce the number of itterations around the
4331 if (Q > cpi->ni_av_qi)
4332 cpi->ni_av_qi = Q - 1;
4336 /* Update the buffer level variable. */
4337 /* Non-viewable frames are a special case and are treated as pure overhead. */
4338 if ( !cm->show_frame )
4339 cpi->bits_off_target -= cpi->projected_frame_size;
4341 cpi->bits_off_target += cpi->av_per_frame_bandwidth - cpi->projected_frame_size;
4343 /* Clip the buffer level to the maximum specified buffer size */
4344 if (cpi->bits_off_target > cpi->oxcf.maximum_buffer_size)
4345 cpi->bits_off_target = cpi->oxcf.maximum_buffer_size;
4347 /* Rolling monitors of whether we are over or underspending used to
4348 * help regulate min and Max Q in two pass.
4350 cpi->rolling_target_bits = ((cpi->rolling_target_bits * 3) + cpi->this_frame_target + 2) / 4;
4351 cpi->rolling_actual_bits = ((cpi->rolling_actual_bits * 3) + cpi->projected_frame_size + 2) / 4;
4352 cpi->long_rolling_target_bits = ((cpi->long_rolling_target_bits * 31) + cpi->this_frame_target + 16) / 32;
4353 cpi->long_rolling_actual_bits = ((cpi->long_rolling_actual_bits * 31) + cpi->projected_frame_size + 16) / 32;
4355 /* Actual bits spent */
4356 cpi->total_actual_bits += cpi->projected_frame_size;
4359 cpi->total_target_vs_actual += (cpi->this_frame_target - cpi->projected_frame_size);
4361 cpi->buffer_level = cpi->bits_off_target;
4363 /* Propagate values to higher temporal layers */
4364 if (cpi->oxcf.number_of_layers > 1)
4368 for (i=cpi->current_layer+1; i<cpi->oxcf.number_of_layers; i++)
4370 LAYER_CONTEXT *lc = &cpi->layer_context[i];
4371 int bits_off_for_this_layer = lc->target_bandwidth / lc->frame_rate
4372 - cpi->projected_frame_size;
4374 lc->bits_off_target += bits_off_for_this_layer;
4376 /* Clip buffer level to maximum buffer size for the layer */
4377 if (lc->bits_off_target > lc->maximum_buffer_size)
4378 lc->bits_off_target = lc->maximum_buffer_size;
4380 lc->total_actual_bits += cpi->projected_frame_size;
4381 lc->total_target_vs_actual += bits_off_for_this_layer;
4382 lc->buffer_level = lc->bits_off_target;
4386 /* Update bits left to the kf and gf groups to account for overshoot
4387 * or undershoot on these frames
4389 if (cm->frame_type == KEY_FRAME)
4391 cpi->twopass.kf_group_bits += cpi->this_frame_target - cpi->projected_frame_size;
4393 if (cpi->twopass.kf_group_bits < 0)
4394 cpi->twopass.kf_group_bits = 0 ;
4396 else if (cm->refresh_golden_frame || cm->refresh_alt_ref_frame)
4398 cpi->twopass.gf_group_bits += cpi->this_frame_target - cpi->projected_frame_size;
4400 if (cpi->twopass.gf_group_bits < 0)
4401 cpi->twopass.gf_group_bits = 0 ;
4404 if (cm->frame_type != KEY_FRAME)
4406 if (cpi->common.refresh_alt_ref_frame)
4408 cpi->last_skip_false_probs[2] = cpi->prob_skip_false;
4409 cpi->last_skip_probs_q[2] = cm->base_qindex;
4411 else if (cpi->common.refresh_golden_frame)
4413 cpi->last_skip_false_probs[1] = cpi->prob_skip_false;
4414 cpi->last_skip_probs_q[1] = cm->base_qindex;
4418 cpi->last_skip_false_probs[0] = cpi->prob_skip_false;
4419 cpi->last_skip_probs_q[0] = cm->base_qindex;
4421 /* update the baseline */
4422 cpi->base_skip_false_prob[cm->base_qindex] = cpi->prob_skip_false;
4427 #if 0 && CONFIG_INTERNAL_STATS
4429 FILE *f = fopen("tmp.stt", "a");
4431 vp8_clear_system_state();
4433 if (cpi->twopass.total_left_stats.coded_error != 0.0)
4434 fprintf(f, "%10d %10d %10d %10d %10d %10d %10d %10d %10d %6d %6d"
4435 "%6d %6d %6d %5d %5d %5d %8d %8.2f %10d %10.3f"
4437 cpi->common.current_video_frame, cpi->this_frame_target,
4438 cpi->projected_frame_size,
4439 (cpi->projected_frame_size - cpi->this_frame_target),
4440 (int)cpi->total_target_vs_actual,
4442 (cpi->oxcf.starting_buffer_level-cpi->bits_off_target),
4443 (int)cpi->total_actual_bits, cm->base_qindex,
4444 cpi->active_best_quality, cpi->active_worst_quality,
4445 cpi->ni_av_qi, cpi->cq_target_quality,
4446 cpi->zbin_over_quant,
4447 cm->refresh_golden_frame, cm->refresh_alt_ref_frame,
4448 cm->frame_type, cpi->gfu_boost,
4449 cpi->twopass.est_max_qcorrection_factor,
4450 (int)cpi->twopass.bits_left,
4451 cpi->twopass.total_left_stats.coded_error,
4452 (double)cpi->twopass.bits_left /
4453 cpi->twopass.total_left_stats.coded_error,
4454 cpi->tot_recode_hits);
4456 fprintf(f, "%10d %10d %10d %10d %10d %10d %10d %10d %10d %6d %6d"
4457 "%6d %6d %6d %5d %5d %5d %8d %8.2f %10d %10.3f"
4459 cpi->common.current_video_frame,
4460 cpi->this_frame_target, cpi->projected_frame_size,
4461 (cpi->projected_frame_size - cpi->this_frame_target),
4462 (int)cpi->total_target_vs_actual,
4464 (cpi->oxcf.starting_buffer_level-cpi->bits_off_target),
4465 (int)cpi->total_actual_bits, cm->base_qindex,
4466 cpi->active_best_quality, cpi->active_worst_quality,
4467 cpi->ni_av_qi, cpi->cq_target_quality,
4468 cpi->zbin_over_quant,
4469 cm->refresh_golden_frame, cm->refresh_alt_ref_frame,
4470 cm->frame_type, cpi->gfu_boost,
4471 cpi->twopass.est_max_qcorrection_factor,
4472 (int)cpi->twopass.bits_left,
4473 cpi->twopass.total_left_stats.coded_error,
4474 cpi->tot_recode_hits);
4479 FILE *fmodes = fopen("Modes.stt", "a");
4482 fprintf(fmodes, "%6d:%1d:%1d:%1d ",
4483 cpi->common.current_video_frame,
4484 cm->frame_type, cm->refresh_golden_frame,
4485 cm->refresh_alt_ref_frame);
4487 for (i = 0; i < MAX_MODES; i++)
4488 fprintf(fmodes, "%5d ", cpi->mode_chosen_counts[i]);
4490 fprintf(fmodes, "\n");
4498 /* If this was a kf or Gf note the Q */
4499 if ((cm->frame_type == KEY_FRAME) || cm->refresh_golden_frame || cm->refresh_alt_ref_frame)
4500 cm->last_kf_gf_q = cm->base_qindex;
4502 if (cm->refresh_golden_frame == 1)
4503 cm->frame_flags = cm->frame_flags | FRAMEFLAGS_GOLDEN;
4505 cm->frame_flags = cm->frame_flags&~FRAMEFLAGS_GOLDEN;
4507 if (cm->refresh_alt_ref_frame == 1)
4508 cm->frame_flags = cm->frame_flags | FRAMEFLAGS_ALTREF;
4510 cm->frame_flags = cm->frame_flags&~FRAMEFLAGS_ALTREF;
4513 if (cm->refresh_last_frame & cm->refresh_golden_frame)
4514 /* both refreshed */
4515 cpi->gold_is_last = 1;
4516 else if (cm->refresh_last_frame ^ cm->refresh_golden_frame)
4517 /* 1 refreshed but not the other */
4518 cpi->gold_is_last = 0;
4520 if (cm->refresh_last_frame & cm->refresh_alt_ref_frame)
4521 /* both refreshed */
4522 cpi->alt_is_last = 1;
4523 else if (cm->refresh_last_frame ^ cm->refresh_alt_ref_frame)
4524 /* 1 refreshed but not the other */
4525 cpi->alt_is_last = 0;
4527 if (cm->refresh_alt_ref_frame & cm->refresh_golden_frame)
4528 /* both refreshed */
4529 cpi->gold_is_alt = 1;
4530 else if (cm->refresh_alt_ref_frame ^ cm->refresh_golden_frame)
4531 /* 1 refreshed but not the other */
4532 cpi->gold_is_alt = 0;
4534 cpi->ref_frame_flags = VP8_ALTR_FRAME | VP8_GOLD_FRAME | VP8_LAST_FRAME;
4536 if (cpi->gold_is_last)
4537 cpi->ref_frame_flags &= ~VP8_GOLD_FRAME;
4539 if (cpi->alt_is_last)
4540 cpi->ref_frame_flags &= ~VP8_ALTR_FRAME;
4542 if (cpi->gold_is_alt)
4543 cpi->ref_frame_flags &= ~VP8_ALTR_FRAME;
4546 if (!cpi->oxcf.error_resilient_mode)
4548 if (cpi->oxcf.play_alternate && cm->refresh_alt_ref_frame && (cm->frame_type != KEY_FRAME))
4549 /* Update the alternate reference frame stats as appropriate. */
4550 update_alt_ref_frame_stats(cpi);
4552 /* Update the Golden frame stats as appropriate. */
4553 update_golden_frame_stats(cpi);
4556 if (cm->frame_type == KEY_FRAME)
4558 /* Tell the caller that the frame was coded as a key frame */
4559 *frame_flags = cm->frame_flags | FRAMEFLAGS_KEY;
4561 /* As this frame is a key frame the next defaults to an inter frame. */
4562 cm->frame_type = INTER_FRAME;
4564 cpi->last_frame_percent_intra = 100;
4568 *frame_flags = cm->frame_flags&~FRAMEFLAGS_KEY;
4570 cpi->last_frame_percent_intra = cpi->this_frame_percent_intra;
4573 /* Clear the one shot update flags for segmentation map and mode/ref
4574 * loop filter deltas.
4576 cpi->mb.e_mbd.update_mb_segmentation_map = 0;
4577 cpi->mb.e_mbd.update_mb_segmentation_data = 0;
4578 cpi->mb.e_mbd.mode_ref_lf_delta_update = 0;
4581 /* Dont increment frame counters if this was an altref buffer update
4586 cm->current_video_frame++;
4587 cpi->frames_since_key++;
4590 /* reset to normal state now that we are done. */
4598 sprintf(filename, "enc%04d.yuv", (int) cm->current_video_frame);
4599 recon_file = fopen(filename, "wb");
4600 fwrite(cm->yv12_fb[cm->lst_fb_idx].buffer_alloc,
4601 cm->yv12_fb[cm->lst_fb_idx].frame_size, 1, recon_file);
4607 /* vp8_write_yuv_frame("encoder_recon.yuv", cm->frame_to_show); */
4613 static void check_gf_quality(VP8_COMP *cpi)
4615 VP8_COMMON *cm = &cpi->common;
4616 int gf_active_pct = (100 * cpi->gf_active_count) / (cm->mb_rows * cm->mb_cols);
4617 int gf_ref_usage_pct = (cpi->count_mb_ref_frame_usage[GOLDEN_FRAME] * 100) / (cm->mb_rows * cm->mb_cols);
4618 int last_ref_zz_useage = (cpi->inter_zz_count * 100) / (cm->mb_rows * cm->mb_cols);
4620 /* Gf refresh is not currently being signalled */
4621 if (cpi->gf_update_recommended == 0)
4623 if (cpi->common.frames_since_golden > 7)
4626 if ((gf_active_pct < 10) || ((gf_active_pct + gf_ref_usage_pct) < 15))
4628 /* ...but last frame zero zero usage is reasonbable so a
4629 * new gf might be appropriate
4631 if (last_ref_zz_useage >= 25)
4633 cpi->gf_bad_count ++;
4635 /* Check that the condition is stable */
4636 if (cpi->gf_bad_count >= 8)
4638 cpi->gf_update_recommended = 1;
4639 cpi->gf_bad_count = 0;
4643 /* Restart count as the background is not stable enough */
4644 cpi->gf_bad_count = 0;
4647 /* Gf useage has picked up so reset count */
4648 cpi->gf_bad_count = 0;
4651 /* If the signal is set but has not been read should we cancel it. */
4652 else if (last_ref_zz_useage < 15)
4654 cpi->gf_update_recommended = 0;
4655 cpi->gf_bad_count = 0;
4660 FILE *f = fopen("gfneeded.stt", "a");
4661 fprintf(f, "%10d %10d %10d %10d %10ld \n",
4662 cm->current_video_frame,
4663 cpi->common.frames_since_golden,
4664 gf_active_pct, gf_ref_usage_pct,
4665 cpi->gf_update_recommended);
4672 #if !(CONFIG_REALTIME_ONLY)
4673 static void Pass2Encode(VP8_COMP *cpi, unsigned long *size, unsigned char *dest, unsigned char * dest_end, unsigned int *frame_flags)
4676 if (!cpi->common.refresh_alt_ref_frame)
4677 vp8_second_pass(cpi);
4679 encode_frame_to_data_rate(cpi, size, dest, dest_end, frame_flags);
4680 cpi->twopass.bits_left -= 8 * *size;
4682 if (!cpi->common.refresh_alt_ref_frame)
4684 double two_pass_min_rate = (double)(cpi->oxcf.target_bandwidth
4685 *cpi->oxcf.two_pass_vbrmin_section / 100);
4686 cpi->twopass.bits_left += (int64_t)(two_pass_min_rate / cpi->frame_rate);
4691 /* For ARM NEON, d8-d15 are callee-saved registers, and need to be saved. */
4693 extern void vp8_push_neon(int64_t *store);
4694 extern void vp8_pop_neon(int64_t *store);
4698 int vp8_receive_raw_frame(VP8_COMP *cpi, unsigned int frame_flags, YV12_BUFFER_CONFIG *sd, int64_t time_stamp, int64_t end_time)
4701 int64_t store_reg[8];
4703 VP8_COMMON *cm = &cpi->common;
4704 struct vpx_usec_timer timer;
4708 #if CONFIG_RUNTIME_CPU_DETECT
4709 if (cm->cpu_caps & HAS_NEON)
4712 vp8_push_neon(store_reg);
4716 vpx_usec_timer_start(&timer);
4718 /* Reinit the lookahead buffer if the frame size changes */
4719 if (sd->y_width != cpi->oxcf.Width || sd->y_height != cpi->oxcf.Height)
4721 assert(cpi->oxcf.lag_in_frames < 2);
4722 dealloc_raw_frame_buffers(cpi);
4723 alloc_raw_frame_buffers(cpi);
4726 if(vp8_lookahead_push(cpi->lookahead, sd, time_stamp, end_time,
4727 frame_flags, cpi->active_map_enabled ? cpi->active_map : NULL))
4729 cm->clr_type = sd->clrtype;
4730 vpx_usec_timer_mark(&timer);
4731 cpi->time_receive_data += vpx_usec_timer_elapsed(&timer);
4734 #if CONFIG_RUNTIME_CPU_DETECT
4735 if (cm->cpu_caps & HAS_NEON)
4738 vp8_pop_neon(store_reg);
4746 static int frame_is_reference(const VP8_COMP *cpi)
4748 const VP8_COMMON *cm = &cpi->common;
4749 const MACROBLOCKD *xd = &cpi->mb.e_mbd;
4751 return cm->frame_type == KEY_FRAME || cm->refresh_last_frame
4752 || cm->refresh_golden_frame || cm->refresh_alt_ref_frame
4753 || cm->copy_buffer_to_gf || cm->copy_buffer_to_arf
4754 || cm->refresh_entropy_probs
4755 || xd->mode_ref_lf_delta_update
4756 || xd->update_mb_segmentation_map || xd->update_mb_segmentation_data;
4760 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)
4763 int64_t store_reg[8];
4766 struct vpx_usec_timer tsctimer;
4767 struct vpx_usec_timer ticktimer;
4768 struct vpx_usec_timer cmptimer;
4769 YV12_BUFFER_CONFIG *force_src_buffer = NULL;
4776 if (setjmp(cpi->common.error.jmp))
4778 cpi->common.error.setjmp = 0;
4779 return VPX_CODEC_CORRUPT_FRAME;
4782 cpi->common.error.setjmp = 1;
4785 #if CONFIG_RUNTIME_CPU_DETECT
4786 if (cm->cpu_caps & HAS_NEON)
4789 vp8_push_neon(store_reg);
4793 vpx_usec_timer_start(&cmptimer);
4797 #if !(CONFIG_REALTIME_ONLY)
4798 /* Should we code an alternate reference frame */
4799 if (cpi->oxcf.error_resilient_mode == 0 &&
4800 cpi->oxcf.play_alternate &&
4801 cpi->source_alt_ref_pending)
4803 if ((cpi->source = vp8_lookahead_peek(cpi->lookahead,
4804 cpi->frames_till_gf_update_due,
4807 cpi->alt_ref_source = cpi->source;
4808 if (cpi->oxcf.arnr_max_frames > 0)
4810 vp8_temporal_filter_prepare_c(cpi,
4811 cpi->frames_till_gf_update_due);
4812 force_src_buffer = &cpi->alt_ref_buffer;
4814 cm->frames_till_alt_ref_frame = cpi->frames_till_gf_update_due;
4815 cm->refresh_alt_ref_frame = 1;
4816 cm->refresh_golden_frame = 0;
4817 cm->refresh_last_frame = 0;
4819 /* Clear Pending alt Ref flag. */
4820 cpi->source_alt_ref_pending = 0;
4821 cpi->is_src_frame_alt_ref = 0;
4828 /* Read last frame source if we are encoding first pass. */
4829 if (cpi->pass == 1 && cm->current_video_frame > 0)
4831 if((cpi->last_source = vp8_lookahead_peek(cpi->lookahead, 1,
4832 PEEK_BACKWARD)) == NULL)
4837 if ((cpi->source = vp8_lookahead_pop(cpi->lookahead, flush)))
4841 cpi->is_src_frame_alt_ref = cpi->alt_ref_source
4842 && (cpi->source == cpi->alt_ref_source);
4844 if(cpi->is_src_frame_alt_ref)
4845 cpi->alt_ref_source = NULL;
4851 cpi->Source = force_src_buffer ? force_src_buffer : &cpi->source->img;
4852 cpi->un_scaled_source = cpi->Source;
4853 *time_stamp = cpi->source->ts_start;
4854 *time_end = cpi->source->ts_end;
4855 *frame_flags = cpi->source->flags;
4857 if (cpi->pass == 1 && cm->current_video_frame > 0)
4859 cpi->last_frame_unscaled_source = &cpi->last_source->img;
4865 #if !(CONFIG_REALTIME_ONLY)
4867 if (flush && cpi->pass == 1 && !cpi->twopass.first_pass_done)
4869 vp8_end_first_pass(cpi); /* get last stats packet */
4870 cpi->twopass.first_pass_done = 1;
4876 #if CONFIG_RUNTIME_CPU_DETECT
4877 if (cm->cpu_caps & HAS_NEON)
4880 vp8_pop_neon(store_reg);
4886 if (cpi->source->ts_start < cpi->first_time_stamp_ever)
4888 cpi->first_time_stamp_ever = cpi->source->ts_start;
4889 cpi->last_end_time_stamp_seen = cpi->source->ts_start;
4892 /* adjust frame rates based on timestamps given */
4895 int64_t this_duration;
4898 if (cpi->source->ts_start == cpi->first_time_stamp_ever)
4900 this_duration = cpi->source->ts_end - cpi->source->ts_start;
4905 int64_t last_duration;
4907 this_duration = cpi->source->ts_end - cpi->last_end_time_stamp_seen;
4908 last_duration = cpi->last_end_time_stamp_seen
4909 - cpi->last_time_stamp_seen;
4910 /* do a step update if the duration changes by 10% */
4912 step = ((this_duration - last_duration) * 10 / last_duration);
4918 cpi->ref_frame_rate = 10000000.0 / this_duration;
4921 double avg_duration, interval;
4923 /* Average this frame's rate into the last second's average
4924 * frame rate. If we haven't seen 1 second yet, then average
4925 * over the whole interval seen.
4927 interval = cpi->source->ts_end - cpi->first_time_stamp_ever;
4928 if(interval > 10000000.0)
4929 interval = 10000000;
4931 avg_duration = 10000000.0 / cpi->ref_frame_rate;
4932 avg_duration *= (interval - avg_duration + this_duration);
4933 avg_duration /= interval;
4935 cpi->ref_frame_rate = 10000000.0 / avg_duration;
4938 if (cpi->oxcf.number_of_layers > 1)
4942 /* Update frame rates for each layer */
4943 for (i=0; i<cpi->oxcf.number_of_layers; i++)
4945 LAYER_CONTEXT *lc = &cpi->layer_context[i];
4946 lc->frame_rate = cpi->ref_frame_rate /
4947 cpi->oxcf.rate_decimator[i];
4951 vp8_new_frame_rate(cpi, cpi->ref_frame_rate);
4954 cpi->last_time_stamp_seen = cpi->source->ts_start;
4955 cpi->last_end_time_stamp_seen = cpi->source->ts_end;
4958 if (cpi->oxcf.number_of_layers > 1)
4962 update_layer_contexts (cpi);
4964 /* Restore layer specific context & set frame rate */
4965 layer = cpi->oxcf.layer_id[
4966 cm->current_video_frame % cpi->oxcf.periodicity];
4967 restore_layer_context (cpi, layer);
4968 vp8_new_frame_rate (cpi, cpi->layer_context[layer].frame_rate);
4971 if (cpi->compressor_speed == 2)
4973 if (cpi->oxcf.number_of_layers == 1)
4974 check_gf_quality(cpi);
4975 vpx_usec_timer_start(&tsctimer);
4976 vpx_usec_timer_start(&ticktimer);
4979 #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
4982 const int num_part = (1 << cm->multi_token_partition);
4983 /* the available bytes in dest */
4984 const unsigned long dest_size = dest_end - dest;
4985 const int tok_part_buff_size = (dest_size * 9) / (10 * num_part);
4987 unsigned char *dp = dest;
4989 cpi->partition_d[0] = dp;
4990 dp += dest_size/10; /* reserve 1/10 for control partition */
4991 cpi->partition_d_end[0] = dp;
4993 for(i = 0; i < num_part; i++)
4995 cpi->partition_d[i + 1] = dp;
4996 dp += tok_part_buff_size;
4997 cpi->partition_d_end[i + 1] = dp;
5002 /* start with a 0 size frame */
5005 /* Clear down mmx registers */
5006 vp8_clear_system_state();
5008 cm->frame_type = INTER_FRAME;
5009 cm->frame_flags = *frame_flags;
5013 if (cm->refresh_alt_ref_frame)
5015 cm->refresh_golden_frame = 0;
5016 cm->refresh_last_frame = 0;
5020 cm->refresh_golden_frame = 0;
5021 cm->refresh_last_frame = 1;
5025 /* find a free buffer for the new frame */
5028 for(; i < NUM_YV12_BUFFERS; i++)
5030 if(!cm->yv12_fb[i].flags)
5037 assert(i < NUM_YV12_BUFFERS );
5039 #if !(CONFIG_REALTIME_ONLY)
5043 Pass1Encode(cpi, size, dest, frame_flags);
5045 else if (cpi->pass == 2)
5047 Pass2Encode(cpi, size, dest, dest_end, frame_flags);
5051 encode_frame_to_data_rate(cpi, size, dest, dest_end, frame_flags);
5053 if (cpi->compressor_speed == 2)
5055 unsigned int duration, duration2;
5056 vpx_usec_timer_mark(&tsctimer);
5057 vpx_usec_timer_mark(&ticktimer);
5059 duration = vpx_usec_timer_elapsed(&ticktimer);
5060 duration2 = (unsigned int)((double)duration / 2);
5062 if (cm->frame_type != KEY_FRAME)
5064 if (cpi->avg_encode_time == 0)
5065 cpi->avg_encode_time = duration;
5067 cpi->avg_encode_time = (7 * cpi->avg_encode_time + duration) >> 3;
5074 if (cpi->avg_pick_mode_time == 0)
5075 cpi->avg_pick_mode_time = duration2;
5077 cpi->avg_pick_mode_time = (7 * cpi->avg_pick_mode_time + duration2) >> 3;
5083 if (cm->refresh_entropy_probs == 0)
5085 vpx_memcpy(&cm->fc, &cm->lfc, sizeof(cm->fc));
5088 /* Save the contexts separately for alt ref, gold and last. */
5089 /* (TODO jbb -> Optimize this with pointers to avoid extra copies. ) */
5090 if(cm->refresh_alt_ref_frame)
5091 vpx_memcpy(&cpi->lfc_a, &cm->fc, sizeof(cm->fc));
5093 if(cm->refresh_golden_frame)
5094 vpx_memcpy(&cpi->lfc_g, &cm->fc, sizeof(cm->fc));
5096 if(cm->refresh_last_frame)
5097 vpx_memcpy(&cpi->lfc_n, &cm->fc, sizeof(cm->fc));
5099 /* if its a dropped frame honor the requests on subsequent frames */
5102 cpi->droppable = !frame_is_reference(cpi);
5104 /* return to normal state */
5105 cm->refresh_entropy_probs = 1;
5106 cm->refresh_alt_ref_frame = 0;
5107 cm->refresh_golden_frame = 0;
5108 cm->refresh_last_frame = 1;
5109 cm->frame_type = INTER_FRAME;
5113 /* Save layer specific state */
5114 if (cpi->oxcf.number_of_layers > 1)
5115 save_layer_context (cpi);
5117 vpx_usec_timer_mark(&cmptimer);
5118 cpi->time_compress_data += vpx_usec_timer_elapsed(&cmptimer);
5120 if (cpi->b_calculate_psnr && cpi->pass != 1 && cm->show_frame)
5122 generate_psnr_packet(cpi);
5125 #if CONFIG_INTERNAL_STATS
5129 cpi->bytes += *size;
5136 if (cpi->b_calculate_psnr)
5140 YV12_BUFFER_CONFIG *orig = cpi->Source;
5141 YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
5142 int y_samples = orig->y_height * orig->y_width ;
5143 int uv_samples = orig->uv_height * orig->uv_width ;
5144 int t_samples = y_samples + 2 * uv_samples;
5145 int64_t sq_error, sq_error2;
5147 ye = calc_plane_error(orig->y_buffer, orig->y_stride,
5148 recon->y_buffer, recon->y_stride, orig->y_width, orig->y_height);
5150 ue = calc_plane_error(orig->u_buffer, orig->uv_stride,
5151 recon->u_buffer, recon->uv_stride, orig->uv_width, orig->uv_height);
5153 ve = calc_plane_error(orig->v_buffer, orig->uv_stride,
5154 recon->v_buffer, recon->uv_stride, orig->uv_width, orig->uv_height);
5156 sq_error = ye + ue + ve;
5158 frame_psnr = vp8_mse2psnr(t_samples, 255.0, sq_error);
5160 cpi->total_y += vp8_mse2psnr(y_samples, 255.0, ye);
5161 cpi->total_u += vp8_mse2psnr(uv_samples, 255.0, ue);
5162 cpi->total_v += vp8_mse2psnr(uv_samples, 255.0, ve);
5163 cpi->total_sq_error += sq_error;
5164 cpi->total += frame_psnr;
5167 YV12_BUFFER_CONFIG *pp = &cm->post_proc_buffer;
5168 double frame_psnr2, frame_ssim2 = 0;
5171 vp8_deblock(cm->frame_to_show, &cm->post_proc_buffer, cm->filter_level * 10 / 6, 1, 0);
5172 vp8_clear_system_state();
5174 ye = calc_plane_error(orig->y_buffer, orig->y_stride,
5175 pp->y_buffer, pp->y_stride, orig->y_width, orig->y_height);
5177 ue = calc_plane_error(orig->u_buffer, orig->uv_stride,
5178 pp->u_buffer, pp->uv_stride, orig->uv_width, orig->uv_height);
5180 ve = calc_plane_error(orig->v_buffer, orig->uv_stride,
5181 pp->v_buffer, pp->uv_stride, orig->uv_width, orig->uv_height);
5183 sq_error2 = ye + ue + ve;
5185 frame_psnr2 = vp8_mse2psnr(t_samples, 255.0, sq_error2);
5187 cpi->totalp_y += vp8_mse2psnr(y_samples, 255.0, ye);
5188 cpi->totalp_u += vp8_mse2psnr(uv_samples, 255.0, ue);
5189 cpi->totalp_v += vp8_mse2psnr(uv_samples, 255.0, ve);
5190 cpi->total_sq_error2 += sq_error2;
5191 cpi->totalp += frame_psnr2;
5193 frame_ssim2 = vp8_calc_ssim(cpi->Source,
5194 &cm->post_proc_buffer, 1, &weight);
5196 cpi->summed_quality += frame_ssim2 * weight;
5197 cpi->summed_weights += weight;
5199 if (cpi->oxcf.number_of_layers > 1)
5203 for (i=cpi->current_layer;
5204 i<cpi->oxcf.number_of_layers; i++)
5206 cpi->frames_in_layer[i]++;
5208 cpi->bytes_in_layer[i] += *size;
5209 cpi->sum_psnr[i] += frame_psnr;
5210 cpi->sum_psnr_p[i] += frame_psnr2;
5211 cpi->total_error2[i] += sq_error;
5212 cpi->total_error2_p[i] += sq_error2;
5213 cpi->sum_ssim[i] += frame_ssim2 * weight;
5214 cpi->sum_weights[i] += weight;
5221 if (cpi->b_calculate_ssimg)
5223 double y, u, v, frame_all;
5224 frame_all = vp8_calc_ssimg(cpi->Source, cm->frame_to_show,
5227 if (cpi->oxcf.number_of_layers > 1)
5231 for (i=cpi->current_layer;
5232 i<cpi->oxcf.number_of_layers; i++)
5234 if (!cpi->b_calculate_psnr)
5235 cpi->frames_in_layer[i]++;
5237 cpi->total_ssimg_y_in_layer[i] += y;
5238 cpi->total_ssimg_u_in_layer[i] += u;
5239 cpi->total_ssimg_v_in_layer[i] += v;
5240 cpi->total_ssimg_all_in_layer[i] += frame_all;
5245 cpi->total_ssimg_y += y;
5246 cpi->total_ssimg_u += u;
5247 cpi->total_ssimg_v += v;
5248 cpi->total_ssimg_all += frame_all;
5257 if (cpi->common.frame_type != 0 && cpi->common.base_qindex == cpi->oxcf.worst_allowed_q)
5259 skiptruecount += cpi->skip_true_count;
5260 skipfalsecount += cpi->skip_false_count;
5268 FILE *f = fopen("skip.stt", "a");
5269 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);
5271 if (cpi->is_src_frame_alt_ref == 1)
5272 fprintf(f, "skipcount: %4d framesize: %d\n", cpi->skip_true_count , *size);
5281 #if CONFIG_RUNTIME_CPU_DETECT
5282 if (cm->cpu_caps & HAS_NEON)
5285 vp8_pop_neon(store_reg);
5289 cpi->common.error.setjmp = 0;
5294 int vp8_get_preview_raw_frame(VP8_COMP *cpi, YV12_BUFFER_CONFIG *dest, vp8_ppflags_t *flags)
5296 if (cpi->common.refresh_alt_ref_frame)
5302 #if CONFIG_MULTITHREAD
5303 if(cpi->b_lpf_running)
5305 sem_wait(&cpi->h_event_end_lpf);
5306 cpi->b_lpf_running = 0;
5311 ret = vp8_post_proc_frame(&cpi->common, dest, flags);
5314 if (cpi->common.frame_to_show)
5316 *dest = *cpi->common.frame_to_show;
5317 dest->y_width = cpi->common.Width;
5318 dest->y_height = cpi->common.Height;
5319 dest->uv_height = cpi->common.Height / 2;
5328 vp8_clear_system_state();
5333 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])
5335 signed char feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS];
5336 int internal_delta_q[MAX_MB_SEGMENTS];
5337 const unsigned int range = 63;
5340 // This method is currently incompatible with the cyclic refresh method
5341 if ( cpi->cyclic_refresh_mode_enabled )
5344 // Check number of rows and columns match
5345 if (cpi->common.mb_rows != rows || cpi->common.mb_cols != cols)
5348 // Range check the delta Q values and convert the external Q range values
5349 // to internal ones.
5350 if ( (abs(delta_q[0]) > range) || (abs(delta_q[1]) > range) ||
5351 (abs(delta_q[2]) > range) || (abs(delta_q[3]) > range) )
5354 // Range check the delta lf values
5355 if ( (abs(delta_lf[0]) > range) || (abs(delta_lf[1]) > range) ||
5356 (abs(delta_lf[2]) > range) || (abs(delta_lf[3]) > range) )
5361 disable_segmentation(cpi);
5365 // Translate the external delta q values to internal values.
5366 for ( i = 0; i < MAX_MB_SEGMENTS; i++ )
5367 internal_delta_q[i] =
5368 ( delta_q[i] >= 0 ) ? q_trans[delta_q[i]] : -q_trans[-delta_q[i]];
5370 /* Set the segmentation Map */
5371 set_segmentation_map(cpi, map);
5373 /* Activate segmentation. */
5374 enable_segmentation(cpi);
5376 /* Set up the quant segment data */
5377 feature_data[MB_LVL_ALT_Q][0] = internal_delta_q[0];
5378 feature_data[MB_LVL_ALT_Q][1] = internal_delta_q[1];
5379 feature_data[MB_LVL_ALT_Q][2] = internal_delta_q[2];
5380 feature_data[MB_LVL_ALT_Q][3] = internal_delta_q[3];
5382 /* Set up the loop segment data s */
5383 feature_data[MB_LVL_ALT_LF][0] = delta_lf[0];
5384 feature_data[MB_LVL_ALT_LF][1] = delta_lf[1];
5385 feature_data[MB_LVL_ALT_LF][2] = delta_lf[2];
5386 feature_data[MB_LVL_ALT_LF][3] = delta_lf[3];
5388 cpi->segment_encode_breakout[0] = threshold[0];
5389 cpi->segment_encode_breakout[1] = threshold[1];
5390 cpi->segment_encode_breakout[2] = threshold[2];
5391 cpi->segment_encode_breakout[3] = threshold[3];
5393 /* Initialise the feature data structure */
5394 set_segment_data(cpi, &feature_data[0][0], SEGMENT_DELTADATA);
5399 int vp8_set_active_map(VP8_COMP *cpi, unsigned char *map, unsigned int rows, unsigned int cols)
5401 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols)
5405 vpx_memcpy(cpi->active_map, map, rows * cols);
5406 cpi->active_map_enabled = 1;
5409 cpi->active_map_enabled = 0;
5419 int vp8_set_internal_size(VP8_COMP *cpi, VPX_SCALING horiz_mode, VPX_SCALING vert_mode)
5421 if (horiz_mode <= ONETWO)
5422 cpi->common.horiz_scale = horiz_mode;
5426 if (vert_mode <= ONETWO)
5427 cpi->common.vert_scale = vert_mode;
5436 int vp8_calc_ss_err(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *dest)
5441 unsigned char *src = source->y_buffer;
5442 unsigned char *dst = dest->y_buffer;
5444 /* Loop through the Y plane raw and reconstruction data summing
5445 * (square differences)
5447 for (i = 0; i < source->y_height; i += 16)
5449 for (j = 0; j < source->y_width; j += 16)
5452 Total += vp8_mse16x16(src + j, source->y_stride, dst + j, dest->y_stride, &sse);
5455 src += 16 * source->y_stride;
5456 dst += 16 * dest->y_stride;
5463 int vp8_get_quantizer(VP8_COMP *cpi)
5465 return cpi->common.base_qindex;