Lines Matching refs:cpi

48 extern int vp8_update_coef_context(VP8_COMP *cpi);
49 extern void vp8_update_coef_probs(VP8_COMP *cpi);
52 extern void vp8cx_pick_filter_level_fast(YV12_BUFFER_CONFIG *sd, VP8_COMP *cpi);
53 extern void vp8cx_set_alt_lf_level(VP8_COMP *cpi, int filt_val);
54 extern void vp8cx_pick_filter_level(YV12_BUFFER_CONFIG *sd, VP8_COMP *cpi);
60 extern int vp8cx_create_encoder_threads(VP8_COMP *cpi);
61 extern void vp8cx_remove_encoder_threads(VP8_COMP *cpi);
63 int vp8_estimate_entropy_savings(VP8_COMP *cpi);
67 extern void vp8_temporal_filter_prepare_c(VP8_COMP *cpi, int distance);
69 static void set_default_lf_deltas(VP8_COMP *cpi);
141 extern void vp8cx_init_quantizer(VP8_COMP *cpi);
216 static void save_layer_context(VP8_COMP *cpi)
218 LAYER_CONTEXT *lc = &cpi->layer_context[cpi->current_layer];
221 lc->target_bandwidth = cpi->target_bandwidth;
222 lc->starting_buffer_level = cpi->oxcf.starting_buffer_level;
223 lc->optimal_buffer_level = cpi->oxcf.optimal_buffer_level;
224 lc->maximum_buffer_size = cpi->oxcf.maximum_buffer_size;
225 lc->starting_buffer_level_in_ms = cpi->oxcf.starting_buffer_level_in_ms;
226 lc->optimal_buffer_level_in_ms = cpi->oxcf.optimal_buffer_level_in_ms;
227 lc->maximum_buffer_size_in_ms = cpi->oxcf.maximum_buffer_size_in_ms;
228 lc->buffer_level = cpi->buffer_level;
229 lc->bits_off_target = cpi->bits_off_target;
230 lc->total_actual_bits = cpi->total_actual_bits;
231 lc->worst_quality = cpi->worst_quality;
232 lc->active_worst_quality = cpi->active_worst_quality;
233 lc->best_quality = cpi->best_quality;
234 lc->active_best_quality = cpi->active_best_quality;
235 lc->ni_av_qi = cpi->ni_av_qi;
236 lc->ni_tot_qi = cpi->ni_tot_qi;
237 lc->ni_frames = cpi->ni_frames;
238 lc->avg_frame_qindex = cpi->avg_frame_qindex;
239 lc->rate_correction_factor = cpi->rate_correction_factor;
240 lc->key_frame_rate_correction_factor = cpi->key_frame_rate_correction_factor;
241 lc->gf_rate_correction_factor = cpi->gf_rate_correction_factor;
242 lc->zbin_over_quant = cpi->mb.zbin_over_quant;
243 lc->inter_frame_target = cpi->inter_frame_target;
244 lc->total_byte_count = cpi->total_byte_count;
245 lc->filter_level = cpi->common.filter_level;
247 lc->last_frame_percent_intra = cpi->last_frame_percent_intra;
250 cpi->mb.count_mb_ref_frame_usage,
251 sizeof(cpi->mb.count_mb_ref_frame_usage));
254 static void restore_layer_context(VP8_COMP *cpi, const int layer)
256 LAYER_CONTEXT *lc = &cpi->layer_context[layer];
259 cpi->current_layer = layer;
260 cpi->target_bandwidth = lc->target_bandwidth;
261 cpi->oxcf.target_bandwidth = lc->target_bandwidth;
262 cpi->oxcf.starting_buffer_level = lc->starting_buffer_level;
263 cpi->oxcf.optimal_buffer_level = lc->optimal_buffer_level;
264 cpi->oxcf.maximum_buffer_size = lc->maximum_buffer_size;
265 cpi->oxcf.starting_buffer_level_in_ms = lc->starting_buffer_level_in_ms;
266 cpi->oxcf.optimal_buffer_level_in_ms = lc->optimal_buffer_level_in_ms;
267 cpi->oxcf.maximum_buffer_size_in_ms = lc->maximum_buffer_size_in_ms;
268 cpi->buffer_level = lc->buffer_level;
269 cpi->bits_off_target = lc->bits_off_target;
270 cpi->total_actual_bits = lc->total_actual_bits;
271 cpi->active_worst_quality = lc->active_worst_quality;
272 cpi->active_best_quality = lc->active_best_quality;
273 cpi->ni_av_qi = lc->ni_av_qi;
274 cpi->ni_tot_qi = lc->ni_tot_qi;
275 cpi->ni_frames = lc->ni_frames;
276 cpi->avg_frame_qindex = lc->avg_frame_qindex;
277 cpi->rate_correction_factor = lc->rate_correction_factor;
278 cpi->key_frame_rate_correction_factor = lc->key_frame_rate_correction_factor;
279 cpi->gf_rate_correction_factor = lc->gf_rate_correction_factor;
280 cpi->mb.zbin_over_quant = lc->zbin_over_quant;
281 cpi->inter_frame_target = lc->inter_frame_target;
282 cpi->total_byte_count = lc->total_byte_count;
283 cpi->common.filter_level = lc->filter_level;
285 cpi->last_frame_percent_intra = lc->last_frame_percent_intra;
287 memcpy (cpi->mb.count_mb_ref_frame_usage,
289 sizeof(cpi->mb.count_mb_ref_frame_usage));
301 static void init_temporal_layer_context(VP8_COMP *cpi,
306 LAYER_CONTEXT *lc = &cpi->layer_context[layer];
308 lc->framerate = cpi->output_framerate / cpi->oxcf.rate_decimator[layer];
309 lc->target_bandwidth = cpi->oxcf.target_bitrate[layer] * 1000;
336 (int)((cpi->oxcf.target_bitrate[layer] -
337 cpi->oxcf.target_bitrate[layer-1]) * 1000 /
340 lc->active_worst_quality = cpi->oxcf.worst_allowed_q;
341 lc->active_best_quality = cpi->oxcf.best_allowed_q;
342 lc->avg_frame_qindex = cpi->oxcf.worst_allowed_q;
361 static void reset_temporal_layer_change(VP8_COMP *cpi,
367 const int curr_num_layers = cpi->oxcf.number_of_layers;
368 // If the previous state was 1 layer, get current layer context from cpi.
372 cpi->current_layer = 0;
373 save_layer_context(cpi);
377 LAYER_CONTEXT *lc = &cpi->layer_context[i];
380 init_temporal_layer_context(cpi, oxcf, i, prev_layer_framerate);
387 lc->buffer_level = cpi->oxcf.starting_buffer_level_in_ms *
388 cpi->oxcf.target_bitrate[i];
397 // pass the layer context state to |cpi|.
400 lc->target_bandwidth = cpi->oxcf.target_bandwidth;
401 lc->buffer_level = cpi->oxcf.starting_buffer_level_in_ms *
404 restore_layer_context(cpi, 0);
406 prev_layer_framerate = cpi->output_framerate /
407 cpi->oxcf.rate_decimator[i];
411 static void setup_features(VP8_COMP *cpi)
414 if ( cpi->mb.e_mbd.segmentation_enabled )
416 cpi->mb.e_mbd.update_mb_segmentation_map = 1;
417 cpi->mb.e_mbd.update_mb_segmentation_data = 1;
421 cpi->mb.e_mbd.update_mb_segmentation_map = 0;
422 cpi->mb.e_mbd.update_mb_segmentation_data = 0;
425 cpi->mb.e_mbd.mode_ref_lf_delta_enabled = 0;
426 cpi->mb.e_mbd.mode_ref_lf_delta_update = 0;
427 vpx_memset(cpi->mb.e_mbd.ref_lf_deltas, 0, sizeof(cpi->mb.e_mbd.ref_lf_deltas));
428 vpx_memset(cpi->mb.e_mbd.mode_lf_deltas, 0, sizeof(cpi->mb.e_mbd.mode_lf_deltas));
429 vpx_memset(cpi->mb.e_mbd.last_ref_lf_deltas, 0, sizeof(cpi->mb.e_mbd.ref_lf_deltas));
430 vpx_memset(cpi->mb.e_mbd.last_mode_lf_deltas, 0, sizeof(cpi->mb.e_mbd.mode_lf_deltas));
432 set_default_lf_deltas(cpi);
437 static void dealloc_raw_frame_buffers(VP8_COMP *cpi);
440 static void dealloc_compressor_data(VP8_COMP *cpi)
442 vpx_free(cpi->tplist);
443 cpi->tplist = NULL;
446 vpx_free(cpi->lfmv);
447 cpi->lfmv = 0;
449 vpx_free(cpi->lf_ref_frame_sign_bias);
450 cpi->lf_ref_frame_sign_bias = 0;
452 vpx_free(cpi->lf_ref_frame);
453 cpi->lf_ref_frame = 0;
456 vpx_free(cpi->segmentation_map);
457 cpi->segmentation_map = 0;
459 vpx_free(cpi->active_map);
460 cpi->active_map = 0;
462 vp8_de_alloc_frame_buffers(&cpi->common);
464 vp8_yv12_de_alloc_frame_buffer(&cpi->pick_lf_lvl_frame);
465 vp8_yv12_de_alloc_frame_buffer(&cpi->scaled_source);
466 dealloc_raw_frame_buffers(cpi);
468 vpx_free(cpi->tok);
469 cpi->tok = 0;
472 vpx_free(cpi->gf_active_flags);
473 cpi->gf_active_flags = 0;
476 vpx_free(cpi->mb_activity_map);
477 cpi->mb_activity_map = 0;
479 vpx_free(cpi->mb.pip);
480 cpi->mb.pip = 0;
483 vpx_free(cpi->mt_current_mb_col);
484 cpi->mt_current_mb_col = NULL;
488 static void enable_segmentation(VP8_COMP *cpi)
491 cpi->mb.e_mbd.segmentation_enabled = 1;
492 cpi->mb.e_mbd.update_mb_segmentation_map = 1;
493 cpi->mb.e_mbd.update_mb_segmentation_data = 1;
495 static void disable_segmentation(VP8_COMP *cpi)
498 cpi->mb.e_mbd.segmentation_enabled = 0;
504 static void set_segmentation_map(VP8_COMP *cpi, unsigned char *segmentation_map)
507 vpx_memcpy(cpi->segmentation_map, segmentation_map, (cpi->common.mb_rows * cpi->common.mb_cols));
510 cpi->mb.e_mbd.update_mb_segmentation_map = 1;
511 cpi->mb.e_mbd.update_mb_segmentation_data = 1;
526 static void set_segment_data(VP8_COMP *cpi, signed char *feature_data, unsigned char abs_delta)
528 cpi->mb.e_mbd.mb_segement_abs_delta = abs_delta;
529 vpx_memcpy(cpi->segment_feature_data, feature_data, sizeof(cpi->segment_feature_data));
533 static void segmentation_test_function(VP8_COMP *cpi)
539 CHECK_MEM_ERROR(seg_map, vpx_calloc(cpi->common.mb_rows * cpi->common.mb_cols, 1));
542 set_segmentation_map(cpi, seg_map);
545 enable_segmentation(cpi);
560 set_segment_data(cpi, &feature_data[0][0], SEGMENT_DELTADATA);
569 static void cyclic_background_refresh(VP8_COMP *cpi, int Q, int lf_adjustment)
571 unsigned char *seg_map = cpi->segmentation_map;
574 int block_count = cpi->cyclic_refresh_mode_max_mbs_perframe;
575 int mbs_in_frame = cpi->common.mb_rows * cpi->common.mb_cols;
577 cpi->cyclic_refresh_q = Q / 2;
581 vpx_memset(cpi->segmentation_map, 0, mbs_in_frame);
583 if (cpi->common.frame_type != KEY_FRAME)
587 i = cpi->cyclic_refresh_mode_index;
597 if (cpi->cyclic_refresh_map[i] == 0)
602 else if (cpi->cyclic_refresh_map[i] < 0)
603 cpi->cyclic_refresh_map[i]++;
610 while(block_count && i != cpi->cyclic_refresh_mode_index);
612 cpi->cyclic_refresh_mode_index = i;
616 cpi->mb.e_mbd.update_mb_segmentation_map = 1;
617 cpi->mb.e_mbd.update_mb_segmentation_data = 1;
618 enable_segmentation(cpi);
622 feature_data[MB_LVL_ALT_Q][1] = (cpi->cyclic_refresh_q - Q);
633 set_segment_data(cpi, &feature_data[0][0], SEGMENT_DELTADATA);
637 static void set_default_lf_deltas(VP8_COMP *cpi)
639 cpi->mb.e_mbd.mode_ref_lf_delta_enabled = 1;
640 cpi->mb.e_mbd.mode_ref_lf_delta_update = 1;
642 vpx_memset(cpi->mb.e_mbd.ref_lf_deltas, 0, sizeof(cpi->mb.e_mbd.ref_lf_deltas));
643 vpx_memset(cpi->mb.e_mbd.mode_lf_deltas, 0, sizeof(cpi->mb.e_mbd.mode_lf_deltas));
646 cpi->mb.e_mbd.ref_lf_deltas[INTRA_FRAME] = 2;
647 cpi->mb.e_mbd.ref_lf_deltas[LAST_FRAME] = 0;
648 cpi->mb.e_mbd.ref_lf_deltas[GOLDEN_FRAME] = -2;
649 cpi->mb.e_mbd.ref_lf_deltas[ALTREF_FRAME] = -2;
651 cpi->mb.e_mbd.mode_lf_deltas[0] = 4; /* BPRED */
653 if(cpi->oxcf.Mode == MODE_REALTIME)
654 cpi->mb.e_mbd.mode_lf_deltas[1] = -12; /* Zero */
656 cpi->mb.e_mbd.mode_lf_deltas[1] = -2; /* Zero */
658 cpi->mb.e_mbd.mode_lf_deltas[2] = 2; /* New mv */
659 cpi->mb.e_mbd.mode_lf_deltas[3] = 4; /* Split mv */
749 void vp8_set_speed_features(VP8_COMP *cpi)
751 SPEED_FEATURES *sf = &cpi->sf;
752 int Mode = cpi->compressor_speed;
753 int Speed = cpi->Speed;
755 VP8_COMMON *cm = &cpi->common;
762 cpi->mode_check_freq[i] = 0;
765 cpi->mb.mbs_tested_so_far = 0;
791 if (cpi->ref_frame_flags & VP8_LAST_FRAME)
793 if (cpi->ref_frame_flags & VP8_GOLD_FRAME)
795 if (cpi->ref_frame_flags & VP8_ALTR_FRAME)
833 cpi->mode_check_freq[THR_ZERO1] =
834 cpi->mode_check_freq[THR_NEAREST1] =
835 cpi->mode_check_freq[THR_NEAR1] =
836 cpi->mode_check_freq[THR_TM] =
837 cpi->mode_check_freq[THR_DC] = 0; /* always */
839 cpi->mode_check_freq[THR_ZERO2] =
840 cpi->mode_check_freq[THR_ZERO3] =
841 cpi->mode_check_freq[THR_NEAREST2] =
842 cpi->mode_check_freq[THR_NEAREST3] = speed_map(Speed,
845 cpi->mode_check_freq[THR_NEAR2] =
846 cpi->mode_check_freq[THR_NEAR3] = speed_map(Speed,
849 cpi->mode_check_freq[THR_V_PRED] =
850 cpi->mode_check_freq[THR_H_PRED] =
851 cpi->mode_check_freq[THR_B_PRED] = speed_map(Speed,
853 cpi->mode_check_freq[THR_NEW1] = speed_map(Speed,
855 cpi->mode_check_freq[THR_NEW2] =
856 cpi->mode_check_freq[THR_NEW3] = speed_map(Speed,
858 cpi->mode_check_freq[THR_SPLIT1] = speed_map(Speed,
860 cpi->mode_check_freq[THR_SPLIT2] =
861 cpi->mode_check_freq[THR_SPLIT3] = speed_map(Speed,
863 Speed = cpi->Speed;
952 if (cpi->oxcf.encode_breakout > 2000)
953 min = cpi->oxcf.encode_breakout;
959 sum += cpi->mb.error_bins[i];
968 sum += cpi->mb.error_bins[i];
970 if (10 * sum >= (unsigned int)(cpi->Speed - 6)*(total_mbs - total_skip))
1023 vpx_memset(cpi->mb.error_bins, 0, sizeof(cpi->mb.error_bins));
1030 if ( cpi->pass == 1 )
1037 if (cpi->sf.search_method == NSTEP)
1039 vp8_init3smotion_compensation(&cpi->mb, cm->yv12_fb[cm->lst_fb_idx].y_stride);
1041 else if (cpi->sf.search_method == DIAMOND)
1043 vp8_init_dsmotion_compensation(&cpi->mb, cm->yv12_fb[cm->lst_fb_idx].y_stride);
1046 if (cpi->sf.improved_dct)
1048 cpi->mb.short_fdct8x4 = vp8_short_fdct8x4;
1049 cpi->mb.short_fdct4x4 = vp8_short_fdct4x4;
1054 cpi->mb.short_fdct8x4 = vp8_short_fdct8x4;
1055 cpi->mb.short_fdct4x4 = vp8_short_fdct4x4;
1058 cpi->mb.short_walsh4x4 = vp8_short_walsh4x4;
1060 if (cpi->sf.improved_quant)
1062 cpi->mb.quantize_b = vp8_regular_quantize_b;
1063 cpi->mb.quantize_b_pair = vp8_regular_quantize_b_pair;
1067 cpi->mb.quantize_b = vp8_fast_quantize_b;
1068 cpi->mb.quantize_b_pair = vp8_fast_quantize_b_pair;
1070 if (cpi->sf.improved_quant != last_improved_quant)
1071 vp8cx_init_quantizer(cpi);
1073 if (cpi->sf.iterative_sub_pixel == 1)
1075 cpi->find_fractional_mv_step = vp8_find_best_sub_pixel_step_iteratively;
1077 else if (cpi->sf.quarter_pixel_search)
1079 cpi->find_fractional_mv_step = vp8_find_best_sub_pixel_step;
1081 else if (cpi->sf.half_pixel_search)
1083 cpi->find_fractional_mv_step = vp8_find_best_half_pixel_step;
1087 cpi->find_fractional_mv_step = vp8_skip_fractional_mv_step;
1090 if (cpi->sf.optimize_coefficients == 1 && cpi->pass!=1)
1091 cpi->mb.optimize = 1;
1093 cpi->mb.optimize = 0;
1095 if (cpi->common.full_pixel)
1096 cpi->find_fractional_mv_step = vp8_skip_fractional_mv_step;
1099 frames_at_speed[cpi->Speed]++;
1105 static void alloc_raw_frame_buffers(VP8_COMP *cpi)
1108 int width = (cpi->oxcf.Width + 15) & ~15;
1109 int height = (cpi->oxcf.Height + 15) & ~15;
1112 cpi->lookahead = vp8_lookahead_init(cpi->oxcf.Width, cpi->oxcf.Height,
1113 cpi->oxcf.lag_in_frames);
1114 if(!cpi->lookahead)
1115 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1120 if (vp8_yv12_alloc_frame_buffer(&cpi->alt_ref_buffer,
1122 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1129 static void dealloc_raw_frame_buffers(VP8_COMP *cpi)
1132 vp8_yv12_de_alloc_frame_buffer(&cpi->alt_ref_buffer);
1134 vp8_lookahead_destroy(cpi->lookahead);
1138 static int vp8_alloc_partition_data(VP8_COMP *cpi)
1140 vpx_free(cpi->mb.pip);
1142 cpi->mb.pip = vpx_calloc((cpi->common.mb_cols + 1) *
1143 (cpi->common.mb_rows + 1),
1145 if(!cpi->mb.pip)
1148 cpi->mb.pi = cpi->mb.pip + cpi->common.mode_info_stride + 1;
1153 void vp8_alloc_compressor_data(VP8_COMP *cpi)
1155 VP8_COMMON *cm = & cpi->common;
1161 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1164 if (vp8_alloc_partition_data(cpi))
1165 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1176 if (vp8_yv12_alloc_frame_buffer(&cpi->pick_lf_lvl_frame,
1178 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1181 if (vp8_yv12_alloc_frame_buffer(&cpi->scaled_source,
1183 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1186 vpx_free(cpi->tok);
1194 CHECK_MEM_ERROR(cpi->tok, vpx_calloc(tokens, sizeof(*cpi->tok)));
1198 cpi->zeromv_count = 0;
1202 vpx_free(cpi->gf_active_flags);
1203 CHECK_MEM_ERROR(cpi->gf_active_flags,
1204 vpx_calloc(sizeof(*cpi->gf_active_flags),
1206 cpi->gf_active_count = cm->mb_rows * cm->mb_cols;
1208 vpx_free(cpi->mb_activity_map);
1209 CHECK_MEM_ERROR(cpi->mb_activity_map,
1210 vpx_calloc(sizeof(*cpi->mb_activity_map),
1214 vpx_free(cpi->lfmv);
1215 CHECK_MEM_ERROR(cpi->lfmv, vpx_calloc((cm->mb_rows+2) * (cm->mb_cols+2),
1216 sizeof(*cpi->lfmv)));
1217 vpx_free(cpi->lf_ref_frame_sign_bias);
1218 CHECK_MEM_ERROR(cpi->lf_ref_frame_sign_bias,
1220 sizeof(*cpi->lf_ref_frame_sign_bias)));
1221 vpx_free(cpi->lf_ref_frame);
1222 CHECK_MEM_ERROR(cpi->lf_ref_frame,
1224 sizeof(*cpi->lf_ref_frame)));
1227 vpx_free(cpi->segmentation_map);
1228 CHECK_MEM_ERROR(cpi->segmentation_map,
1230 sizeof(*cpi->segmentation_map)));
1231 cpi->cyclic_refresh_mode_index = 0;
1232 vpx_free(cpi->active_map);
1233 CHECK_MEM_ERROR(cpi->active_map,
1235 sizeof(*cpi->active_map)));
1236 vpx_memset(cpi->active_map , 1, (cm->mb_rows * cm->mb_cols));
1240 cpi->mt_sync_range = 1;
1242 cpi->mt_sync_range = 4;
1244 cpi->mt_sync_range = 8;
1246 cpi->mt_sync_range = 16;
1248 if (cpi->oxcf.multi_threaded > 1)
1250 vpx_free(cpi->mt_current_mb_col);
1251 CHECK_MEM_ERROR(cpi->mt_current_mb_col,
1252 vpx_malloc(sizeof(*cpi->mt_current_mb_col) * cm->mb_rows));
1257 vpx_free(cpi->tplist);
1258 CHECK_MEM_ERROR(cpi->tplist, vpx_malloc(sizeof(TOKENLIST) * cm->mb_rows));
1285 void vp8_new_framerate(VP8_COMP *cpi, double framerate)
1290 cpi->framerate = framerate;
1291 cpi->output_framerate = framerate;
1292 cpi->per_frame_bandwidth = (int)(cpi->oxcf.target_bandwidth /
1293 cpi->output_framerate);
1294 cpi->av_per_frame_bandwidth = cpi->per_frame_bandwidth;
1295 cpi->min_frame_bandwidth = (int)(cpi->av_per_frame_bandwidth *
1296 cpi->oxcf.two_pass_vbrmin_section / 100);
1299 cpi->max_gf_interval = ((int)(cpi->output_framerate / 2.0) + 2);
1301 if(cpi->max_gf_interval < 12)
1302 cpi->max_gf_interval = 12;
1305 cpi->twopass.static_scene_max_gf_interval = cpi->key_frame_frequency >> 1;
1308 if (cpi->oxcf.play_alternate && cpi->oxcf.lag_in_frames)
1310 if (cpi->max_gf_interval > cpi->oxcf.lag_in_frames - 1)
1311 cpi->max_gf_interval = cpi->oxcf.lag_in_frames - 1;
1313 if (cpi->twopass.static_scene_max_gf_interval > cpi->oxcf.lag_in_frames - 1)
1314 cpi->twopass.static_scene_max_gf_interval = cpi->oxcf.lag_in_frames - 1;
1317 if ( cpi->max_gf_interval > cpi->twopass.static_scene_max_gf_interval )
1318 cpi->max_gf_interval = cpi->twopass.static_scene_max_gf_interval;
1322 static void init_config(VP8_COMP *cpi, VP8_CONFIG *oxcf)
1324 VP8_COMMON *cm = &cpi->common;
1326 cpi->oxcf = *oxcf;
1328 cpi->auto_gold = 1;
1329 cpi->auto_adjust_gold_quantizer = 1;
1340 cpi->framerate = (double)(oxcf->timebase.den) /
1343 if (cpi->framerate > 180)
1344 cpi->framerate = 30;
1346 cpi->ref_framerate = cpi->framerate;
1349 vp8_change_config(cpi, oxcf);
1352 cpi->active_worst_quality = cpi->oxcf.worst_allowed_q;
1353 cpi->active_best_quality = cpi->oxcf.best_allowed_q;
1354 cpi->avg_frame_qindex = cpi->oxcf.worst_allowed_q;
1357 cpi->buffer_level = cpi->oxcf.starting_buffer_level;
1358 cpi->bits_off_target = cpi->oxcf.starting_buffer_level;
1360 cpi->rolling_target_bits = cpi->av_per_frame_bandwidth;
1361 cpi->rolling_actual_bits = cpi->av_per_frame_bandwidth;
1362 cpi->long_rolling_target_bits = cpi->av_per_frame_bandwidth;
1363 cpi->long_rolling_actual_bits = cpi->av_per_frame_bandwidth;
1365 cpi->total_actual_bits = 0;
1366 cpi->total_target_vs_actual = 0;
1369 if (cpi->oxcf.number_of_layers > 1)
1374 for (i=0; i<cpi->oxcf.number_of_layers; i++)
1376 init_temporal_layer_context(cpi, oxcf, i, prev_layer_framerate);
1377 prev_layer_framerate = cpi->output_framerate /
1378 cpi->oxcf.rate_decimator[i];
1386 cpi->fixed_divide[0] = 0;
1389 cpi->fixed_divide[i] = 0x80000 / i;
1394 static void update_layer_contexts (VP8_COMP *cpi)
1396 VP8_CONFIG *oxcf = &cpi->oxcf;
1407 LAYER_CONTEXT *lc = &cpi->layer_context[i];
1410 cpi->ref_framerate / oxcf->rate_decimator[i];
1443 void vp8_change_config(VP8_COMP *cpi, VP8_CONFIG *oxcf)
1445 VP8_COMMON *cm = &cpi->common;
1448 if (!cpi)
1456 if (cpi->b_lpf_running)
1458 sem_wait(&cpi->h_event_end_lpf);
1459 cpi->b_lpf_running = 0;
1469 last_w = cpi->oxcf.Width;
1470 last_h = cpi->oxcf.Height;
1471 prev_number_of_layers = cpi->oxcf.number_of_layers;
1473 cpi->oxcf = *oxcf;
1475 switch (cpi->oxcf.Mode)
1479 cpi->pass = 0;
1480 cpi->compressor_speed = 2;
1482 if (cpi->oxcf.cpu_used < -16)
1484 cpi->oxcf.cpu_used = -16;
1487 if (cpi->oxcf.cpu_used > 16)
1488 cpi->oxcf.cpu_used = 16;
1493 cpi->pass = 0;
1494 cpi->compressor_speed = 1;
1496 if (cpi->oxcf.cpu_used < -5)
1498 cpi->oxcf.cpu_used = -5;
1501 if (cpi->oxcf.cpu_used > 5)
1502 cpi->oxcf.cpu_used = 5;
1507 cpi->pass = 0;
1508 cpi->compressor_speed = 0;
1512 cpi->pass = 1;
1513 cpi->compressor_speed = 1;
1516 cpi->pass = 2;
1517 cpi->compressor_speed = 1;
1519 if (cpi->oxcf.cpu_used < -5)
1521 cpi->oxcf.cpu_used = -5;
1524 if (cpi->oxcf.cpu_used > 5)
1525 cpi->oxcf.cpu_used = 5;
1529 cpi->pass = 2;
1530 cpi->compressor_speed = 0;
1534 if (cpi->pass == 0)
1535 cpi->auto_worst_q = 1;
1537 cpi->oxcf.worst_allowed_q = q_trans[oxcf->worst_allowed_q];
1538 cpi->oxcf.best_allowed_q = q_trans[oxcf->best_allowed_q];
1539 cpi->oxcf.cq_level = q_trans[cpi->oxcf.cq_level];
1544 cpi->oxcf.fixed_q = q_trans[0];
1546 cpi->oxcf.fixed_q = q_trans[oxcf->worst_allowed_q];
1549 cpi->oxcf.alt_q = q_trans[0];
1551 cpi->oxcf.alt_q = q_trans[oxcf->alt_q];
1554 cpi->oxcf.key_q = q_trans[0];
1556 cpi->oxcf.key_q = q_trans[oxcf->key_q];
1559 cpi->oxcf.gold_q = q_trans[0];
1561 cpi->oxcf.gold_q = q_trans[oxcf->gold_q];
1565 cpi->baseline_gf_interval =
1566 cpi->oxcf.alt_freq ? cpi->oxcf.alt_freq : DEFAULT_GF_INTERVAL;
1568 cpi->ref_frame_flags = VP8_ALTR_FRAME | VP8_GOLD_FRAME | VP8_LAST_FRAME;
1575 cpi->oxcf.token_partitions = 3;
1578 if (cpi->oxcf.token_partitions >= 0 && cpi->oxcf.token_partitions <= 3)
1580 (TOKEN_PARTITION) cpi->oxcf.token_partitions;
1582 setup_features(cpi);
1588 cpi->segment_encode_breakout[i] = cpi->oxcf.encode_breakout;
1592 if (cpi->oxcf.fixed_q > MAXQ)
1593 cpi->oxcf.fixed_q = MAXQ;
1596 if (cpi->oxcf.end_usage == USAGE_LOCAL_FILE_PLAYBACK)
1598 cpi->oxcf.starting_buffer_level = 60000;
1599 cpi->oxcf.optimal_buffer_level = 60000;
1600 cpi->oxcf.maximum_buffer_size = 240000;
1601 cpi->oxcf.starting_buffer_level_in_ms = 60000;
1602 cpi->oxcf.optimal_buffer_level_in_ms = 60000;
1603 cpi->oxcf.maximum_buffer_size_in_ms = 240000;
1607 cpi->oxcf.target_bandwidth *= 1000;
1609 cpi->oxcf.starting_buffer_level =
1610 rescale((int)cpi->oxcf.starting_buffer_level,
1611 cpi->oxcf.target_bandwidth, 1000);
1614 if (cpi->oxcf.optimal_buffer_level == 0)
1615 cpi->oxcf.optimal_buffer_level = cpi->oxcf.target_bandwidth / 8;
1617 cpi->oxcf.optimal_buffer_level =
1618 rescale((int)cpi->oxcf.optimal_buffer_level,
1619 cpi->oxcf.target_bandwidth, 1000);
1621 if (cpi->oxcf.maximum_buffer_size == 0)
1622 cpi->oxcf.maximum_buffer_size = cpi->oxcf.target_bandwidth / 8;
1624 cpi->oxcf.maximum_buffer_size =
1625 rescale((int)cpi->oxcf.maximum_buffer_size,
1626 cpi->oxcf.target_bandwidth, 1000);
1629 if (cpi->bits_off_target > cpi->oxcf.maximum_buffer_size) {
1630 cpi->bits_off_target = cpi->oxcf.maximum_buffer_size;
1631 cpi->buffer_level = cpi->bits_off_target;
1635 vp8_new_framerate(cpi, cpi->framerate);
1638 cpi->worst_quality = cpi->oxcf.worst_allowed_q;
1639 cpi->best_quality = cpi->oxcf.best_allowed_q;
1642 if (cpi->active_worst_quality > cpi->oxcf.worst_allowed_q)
1644 cpi->active_worst_quality = cpi->oxcf.worst_allowed_q;
1647 else if (cpi->active_worst_quality < cpi->oxcf.best_allowed_q)
1649 cpi->active_worst_quality = cpi->oxcf.best_allowed_q;
1651 if (cpi->active_best_quality < cpi->oxcf.best_allowed_q)
1653 cpi->active_best_quality = cpi->oxcf.best_allowed_q;
1656 else if (cpi->active_best_quality > cpi->oxcf.worst_allowed_q)
1658 cpi->active_best_quality = cpi->oxcf.worst_allowed_q;
1661 cpi->buffered_mode = cpi->oxcf.optimal_buffer_level > 0;
1663 cpi->cq_target_quality = cpi->oxcf.cq_level;
1666 cpi->drop_frames_allowed = cpi->oxcf.allow_df && cpi->buffered_mode;
1668 cpi->target_bandwidth = cpi->oxcf.target_bandwidth;
1673 if (cpi->oxcf.number_of_layers != prev_number_of_layers)
1677 cpi->temporal_pattern_counter = 0;
1678 reset_temporal_layer_change(cpi, oxcf, prev_number_of_layers);
1681 cm->Width = cpi->oxcf.Width;
1682 cm->Height = cpi->oxcf.Height;
1691 if (cpi->oxcf.Sharpness > 7)
1692 cpi->oxcf.Sharpness = 7;
1694 cm->sharpness_level = cpi->oxcf.Sharpness;
1705 cm->Width = (hs - 1 + cpi->oxcf.Width * hr) / hs;
1706 cm->Height = (vs - 1 + cpi->oxcf.Height * vr) / vs;
1709 if (last_w != cpi->oxcf.Width || last_h != cpi->oxcf.Height)
1710 cpi->force_next_frame_intra = 1;
1718 dealloc_raw_frame_buffers(cpi);
1719 alloc_raw_frame_buffers(cpi);
1720 vp8_alloc_compressor_data(cpi);
1723 if (cpi->oxcf.fixed_q >= 0)
1725 cpi->last_q[0] = cpi->oxcf.fixed_q;
1726 cpi->last_q[1] = cpi->oxcf.fixed_q;
1729 cpi->Speed = cpi->oxcf.cpu_used;
1732 if (cpi->oxcf.lag_in_frames == 0)
1734 cpi->oxcf.allow_lag = 0;
1737 else if (cpi->oxcf.lag_in_frames > MAX_LAG_BUFFERS)
1738 cpi->oxcf.lag_in_frames = MAX_LAG_BUFFERS;
1741 cpi->alt_ref_source = NULL;
1742 cpi->is_src_frame_alt_ref = 0;
1745 if (cpi->oxcf.noise_sensitivity)
1747 if (!cpi->denoiser.yv12_mc_running_avg.buffer_alloc)
1749 int width = (cpi->oxcf.Width + 15) & ~15;
1750 int height = (cpi->oxcf.Height + 15) & ~15;
1751 vp8_denoiser_allocate(&cpi->denoiser, width, height);
1758 cpi->frame_distortion = 0;
1759 cpi->last_frame_distortion = 0;
1788 VP8_COMP *cpi;
1791 cpi = vpx_memalign(32, sizeof(VP8_COMP));
1793 if (!cpi)
1796 cm = &cpi->common;
1798 vpx_memset(cpi, 0, sizeof(VP8_COMP));
1802 cpi->common.error.setjmp = 0;
1803 vp8_remove_compressor(&cpi);
1807 cpi->common.error.setjmp = 1;
1809 CHECK_MEM_ERROR(cpi->mb.ss, vpx_calloc(sizeof(search_site), (MAX_MVSEARCH_STEPS * 8) + 1));
1811 vp8_create_common(&cpi->common);
1813 init_config(cpi, oxcf);
1815 memcpy(cpi->base_skip_false_prob, vp8cx_base_skip_false_prob, sizeof(vp8cx_base_skip_false_prob));
1816 cpi->common.current_video_frame = 0;
1817 cpi->temporal_pattern_counter = 0;
1818 cpi->kf_overspend_bits = 0;
1819 cpi->kf_bitrate_adjustment = 0;
1820 cpi->frames_till_gf_update_due = 0;
1821 cpi->gf_overspend_bits = 0;
1822 cpi->non_gf_bitrate_adjustment = 0;
1823 cpi->prob_last_coded = 128;
1824 cpi->prob_gf_coded = 128;
1825 cpi->prob_intra_coded = 63;
1830 cpi->recent_ref_frame_usage[INTRA_FRAME] = 1;
1831 cpi->recent_ref_frame_usage[LAST_FRAME] = 1;
1832 cpi->recent_ref_frame_usage[GOLDEN_FRAME] = 1;
1833 cpi->recent_ref_frame_usage[ALTREF_FRAME] = 1;
1836 cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = 1;
1838 cpi->twopass.gf_decay_rate = 0;
1839 cpi->baseline_gf_interval = DEFAULT_GF_INTERVAL;
1841 cpi->gold_is_last = 0 ;
1842 cpi->alt_is_last = 0 ;
1843 cpi->gold_is_alt = 0 ;
1845 cpi->active_map_enabled = 0;
1851 if (cpi->pass == 0)
1853 cpi->one_pass_frame_index = 0;
1857 cpi->one_pass_frame_stats[i].frames_so_far = 0;
1858 cpi->one_pass_frame_stats[i].frame_intra_error = 0.0;
1859 cpi->one_pass_frame_stats[i].frame_coded_error = 0.0;
1860 cpi->one_pass_frame_stats[i].frame_pcnt_inter = 0.0;
1861 cpi->one_pass_frame_stats[i].frame_pcnt_motion = 0.0;
1862 cpi->one_pass_frame_stats[i].frame_mvr = 0.0;
1863 cpi->one_pass_frame_stats[i].frame_mvr_abs = 0.0;
1864 cpi->one_pass_frame_stats[i].frame_mvc = 0.0;
1865 cpi->one_pass_frame_stats[i].frame_mvc_abs = 0.0;
1873 cpi->cyclic_refresh_mode_enabled = cpi->oxcf.error_resilient_mode;
1874 cpi->cyclic_refresh_mode_max_mbs_perframe = (cpi->common.mb_rows * cpi->common.mb_cols) / 5;
1875 cpi->cyclic_refresh_mode_index = 0;
1876 cpi->cyclic_refresh_q = 32;
1878 if (cpi->cyclic_refresh_mode_enabled)
1880 CHECK_MEM_ERROR(cpi->cyclic_refresh_map, vpx_calloc((cpi->common.mb_rows * cpi->common.mb_cols), 1));
1883 cpi->cyclic_refresh_map = (signed char *) NULL;
1890 cpi->activity_avg = 90<<12;
1893 cpi->frames_since_key = 8;
1894 cpi->key_frame_frequency = cpi->oxcf.key_freq;
1895 cpi->this_key_frame_forced = 0;
1896 cpi->next_key_frame_forced = 0;
1898 cpi->source_alt_ref_pending = 0;
1899 cpi->source_alt_ref_active = 0;
1900 cpi->common.refresh_alt_ref_frame = 0;
1902 cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
1904 cpi->b_calculate_ssimg = 0;
1906 cpi->count = 0;
1907 cpi->bytes = 0;
1909 if (cpi->b_calculate_psnr)
1911 cpi->total_sq_error = 0.0;
1912 cpi->total_sq_error2 = 0.0;
1913 cpi->total_y = 0.0;
1914 cpi->total_u = 0.0;
1915 cpi->total_v = 0.0;
1916 cpi->total = 0.0;
1917 cpi->totalp_y = 0.0;
1918 cpi->totalp_u = 0.0;
1919 cpi->totalp_v = 0.0;
1920 cpi->totalp = 0.0;
1921 cpi->tot_recode_hits = 0;
1922 cpi->summed_quality = 0;
1923 cpi->summed_weights = 0;
1926 if (cpi->b_calculate_ssimg)
1928 cpi->total_ssimg_y = 0;
1929 cpi->total_ssimg_u = 0;
1930 cpi->total_ssimg_v = 0;
1931 cpi->total_ssimg_all = 0;
1936 cpi->first_time_stamp_ever = 0x7FFFFFFF;
1938 cpi->frames_till_gf_update_due = 0;
1939 cpi->key_frame_count = 1;
1941 cpi->ni_av_qi = cpi->oxcf.worst_allowed_q;
1942 cpi->ni_tot_qi = 0;
1943 cpi->ni_frames = 0;
1944 cpi->total_byte_count = 0;
1946 cpi->drop_frame = 0;
1948 cpi->rate_correction_factor = 1.0;
1949 cpi->key_frame_rate_correction_factor = 1.0;
1950 cpi->gf_rate_correction_factor = 1.0;
1951 cpi->twopass.est_max_qcorrection_factor = 1.0;
1955 cpi->prior_key_frame_distance[i] = (int)cpi->output_framerate;
1967 cpi->output_pkt_list = oxcf->output_pkt_list;
1971 if (cpi->pass == 1)
1973 vp8_init_first_pass(cpi);
1975 else if (cpi->pass == 2)
1980 cpi->twopass.stats_in_start = oxcf->two_pass_stats_in.buf;
1981 cpi->twopass.stats_in = cpi->twopass.stats_in_start;
1982 cpi->twopass.stats_in_end = (void*)((char *)cpi->twopass.stats_in
1984 vp8_init_second_pass(cpi);
1989 if (cpi->compressor_speed == 2)
1991 cpi->avg_encode_time = 0;
1992 cpi->avg_pick_mode_time = 0;
1995 vp8_set_speed_features(cpi);
2000 cpi->mb.rd_thresh_mult[i] = 128;
2008 if(vp8cx_create_encoder_threads(cpi))
2010 vp8_remove_compressor(&cpi);
2015 cpi->fn_ptr[BLOCK_16X16].sdf = vp8_sad16x16;
2016 cpi->fn_ptr[BLOCK_16X16].vf = vp8_variance16x16;
2017 cpi->fn_ptr[BLOCK_16X16].svf = vp8_sub_pixel_variance16x16;
2018 cpi->fn_ptr[BLOCK_16X16].svf_halfpix_h = vp8_variance_halfpixvar16x16_h;
2019 cpi->fn_ptr[BLOCK_16X16].svf_halfpix_v = vp8_variance_halfpixvar16x16_v;
2020 cpi->fn_ptr[BLOCK_16X16].svf_halfpix_hv = vp8_variance_halfpixvar16x16_hv;
2021 cpi->fn_ptr[BLOCK_16X16].sdx3f = vp8_sad16x16x3;
2022 cpi->fn_ptr[BLOCK_16X16].sdx8f = vp8_sad16x16x8;
2023 cpi->fn_ptr[BLOCK_16X16].sdx4df = vp8_sad16x16x4d;
2025 cpi->fn_ptr[BLOCK_16X8].sdf = vp8_sad16x8;
2026 cpi->fn_ptr[BLOCK_16X8].vf = vp8_variance16x8;
2027 cpi->fn_ptr[BLOCK_16X8].svf = vp8_sub_pixel_variance16x8;
2028 cpi->fn_ptr[BLOCK_16X8].svf_halfpix_h = NULL;
2029 cpi->fn_ptr[BLOCK_16X8].svf_halfpix_v = NULL;
2030 cpi->fn_ptr[BLOCK_16X8].svf_halfpix_hv = NULL;
2031 cpi->fn_ptr[BLOCK_16X8].sdx3f = vp8_sad16x8x3;
2032 cpi->fn_ptr[BLOCK_16X8].sdx8f = vp8_sad16x8x8;
2033 cpi->fn_ptr[BLOCK_16X8].sdx4df = vp8_sad16x8x4d;
2035 cpi->fn_ptr[BLOCK_8X16].sdf = vp8_sad8x16;
2036 cpi->fn_ptr[BLOCK_8X16].vf = vp8_variance8x16;
2037 cpi->fn_ptr[BLOCK_8X16].svf = vp8_sub_pixel_variance8x16;
2038 cpi->fn_ptr[BLOCK_8X16].svf_halfpix_h = NULL;
2039 cpi->fn_ptr[BLOCK_8X16].svf_halfpix_v = NULL;
2040 cpi->fn_ptr[BLOCK_8X16].svf_halfpix_hv = NULL;
2041 cpi->fn_ptr[BLOCK_8X16].sdx3f = vp8_sad8x16x3;
2042 cpi->fn_ptr[BLOCK_8X16].sdx8f = vp8_sad8x16x8;
2043 cpi->fn_ptr[BLOCK_8X16].sdx4df = vp8_sad8x16x4d;
2045 cpi->fn_ptr[BLOCK_8X8].sdf = vp8_sad8x8;
2046 cpi->fn_ptr[BLOCK_8X8].vf = vp8_variance8x8;
2047 cpi->fn_ptr[BLOCK_8X8].svf = vp8_sub_pixel_variance8x8;
2048 cpi->fn_ptr[BLOCK_8X8].svf_halfpix_h = NULL;
2049 cpi->fn_ptr[BLOCK_8X8].svf_halfpix_v = NULL;
2050 cpi->fn_ptr[BLOCK_8X8].svf_halfpix_hv = NULL;
2051 cpi->fn_ptr[BLOCK_8X8].sdx3f = vp8_sad8x8x3;
2052 cpi->fn_ptr[BLOCK_8X8].sdx8f = vp8_sad8x8x8;
2053 cpi->fn_ptr[BLOCK_8X8].sdx4df = vp8_sad8x8x4d;
2055 cpi->fn_ptr[BLOCK_4X4].sdf = vp8_sad4x4;
2056 cpi->fn_ptr[BLOCK_4X4].vf = vp8_variance4x4;
2057 cpi->fn_ptr[BLOCK_4X4].svf = vp8_sub_pixel_variance4x4;
2058 cpi->fn_ptr[BLOCK_4X4].svf_halfpix_h = NULL;
2059 cpi->fn_ptr[BLOCK_4X4].svf_halfpix_v = NULL;
2060 cpi->fn_ptr[BLOCK_4X4].svf_halfpix_hv = NULL;
2061 cpi->fn_ptr[BLOCK_4X4].sdx3f = vp8_sad4x4x3;
2062 cpi->fn_ptr[BLOCK_4X4].sdx8f = vp8_sad4x4x8;
2063 cpi->fn_ptr[BLOCK_4X4].sdx4df = vp8_sad4x4x4d;
2066 cpi->fn_ptr[BLOCK_16X16].copymem = vp8_copy32xn;
2067 cpi->fn_ptr[BLOCK_16X8].copymem = vp8_copy32xn;
2068 cpi->fn_ptr[BLOCK_8X16].copymem = vp8_copy32xn;
2069 cpi->fn_ptr[BLOCK_8X8].copymem = vp8_copy32xn;
2070 cpi->fn_ptr[BLOCK_4X4].copymem = vp8_copy32xn;
2073 cpi->full_search_sad = vp8_full_search_sad;
2074 cpi->diamond_search_sad = vp8_diamond_search_sad;
2075 cpi->refining_search_sad = vp8_refining_search_sad;
2078 cpi->mb.error_bins[0] = cpi->common.MBs;
2085 vp8cx_init_quantizer(cpi);
2089 cpi->common.error.setjmp = 0;
2094 if (cpi->oxcf.mr_encoder_id > 0)
2095 vp8_cal_low_res_mb_cols(cpi);
2101 cpi->mb.mvcost[0] = &cpi->rd_costs.mvcosts[0][mv_max+1];
2102 cpi->mb.mvcost[1] = &cpi->rd_costs.mvcosts[1][mv_max+1];
2103 cpi->mb.mvsadcost[0] = &cpi->rd_costs.mvsadcosts[0][mvfp_max+1];
2104 cpi->mb.mvsadcost[1] = &cpi->rd_costs.mvsadcosts[1][mvfp_max+1];
2106 cal_mvsadcosts(cpi->mb.mvsadcost);
2108 cpi->mb.mbmode_cost = cpi->rd_costs.mbmode_cost;
2109 cpi->mb.intra_uv_mode_cost = cpi->rd_costs.intra_uv_mode_cost;
2110 cpi->mb.bmode_costs = cpi->rd_costs.bmode_costs;
2111 cpi->mb.inter_bmode_costs = cpi->rd_costs.inter_bmode_costs;
2112 cpi->mb.token_costs = cpi->rd_costs.token_costs;
2115 vp8_setup_block_ptrs(&cpi->mb);
2116 vp8_setup_block_dptrs(&cpi->mb.e_mbd);
2118 return cpi;
2124 VP8_COMP *cpi = *ptr;
2126 if (!cpi)
2129 if (cpi && (cpi->common.current_video_frame > 0))
2133 if (cpi->pass == 2)
2135 vp8_end_second_pass(cpi);
2148 if (cpi->pass != 1)
2151 double time_encoded = (cpi->last_end_time_stamp_seen
2152 - cpi->first_time_stamp_ever) / 10000000.000;
2153 double total_encode_time = (cpi->time_receive_data +
2154 cpi->time_compress_data) / 1000.000;
2155 double dr = (double)cpi->bytes * 8.0 / 1000.0 / time_encoded;
2157 if (cpi->b_calculate_psnr)
2160 &cpi->common.yv12_fb[cpi->common.lst_fb_idx];
2162 if (cpi->oxcf.number_of_layers > 1)
2168 for (i=0; i<(int)cpi->oxcf.number_of_layers; i++)
2170 double dr = (double)cpi->bytes_in_layer[i] *
2172 double samples = 3.0 / 2 * cpi->frames_in_layer[i] *
2176 cpi->total_error2[i]);
2179 cpi->total_error2_p[i]);
2180 double total_ssim = 100 * pow(cpi->sum_ssim[i] /
2181 cpi->sum_weights[i], 8.0);
2186 cpi->sum_psnr[i] / cpi->frames_in_layer[i],
2188 cpi->sum_psnr_p[i] / cpi->frames_in_layer[i],
2194 double samples = 3.0 / 2 * cpi->count *
2197 cpi->total_sq_error);
2199 cpi->total_sq_error2);
2200 double total_ssim = 100 * pow(cpi->summed_quality /
2201 cpi->summed_weights, 8.0);
2207 dr, cpi->total / cpi->count, total_psnr,
2208 cpi->totalp / cpi->count, total_psnr2,
2213 if (cpi->b_calculate_ssimg)
2215 if (cpi->oxcf.number_of_layers > 1)
2221 for (i=0; i<(int)cpi->oxcf.number_of_layers; i++)
2223 double dr = (double)cpi->bytes_in_layer[i] *
2228 cpi->total_ssimg_y_in_layer[i] /
2229 cpi->frames_in_layer[i],
2230 cpi->total_ssimg_u_in_layer[i] /
2231 cpi->frames_in_layer[i],
2232 cpi->total_ssimg_v_in_layer[i] /
2233 cpi->frames_in_layer[i],
2234 cpi->total_ssimg_all_in_layer[i] /
2235 cpi->frames_in_layer[i],
2244 cpi->total_ssimg_y / cpi->count,
2245 cpi->total_ssimg_u / cpi->count,
2246 cpi->total_ssimg_v / cpi->count,
2247 cpi->total_ssimg_all / cpi->count, total_encode_time);
2254 fprintf(f, "minq:%d -maxq:%d skiptrue:skipfalse = %d:%d\n", cpi->oxcf.best_allowed_q, cpi->oxcf.worst_allowed_q, skiptruecount, skipfalsecount);
2265 if (cpi->compressor_speed == 2)
2269 cnt_pm /= cpi->common.MBs;
2285 double dr = (double)cpi->framerate * (double)bytes * (double)8 / (double)count / (double)1000 ;
2383 printf("\n_pick_loop_filter_level:%d\n", cpi->time_pick_lpf / 1000);
2385 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);
2392 vp8cx_remove_encoder_threads(cpi);
2396 vp8_denoiser_free(&cpi->denoiser);
2398 dealloc_compressor_data(cpi);
2399 vpx_free(cpi->mb.ss);
2400 vpx_free(cpi->tok);
2401 vpx_free(cpi->cyclic_refresh_map);
2403 vp8_remove_common(&cpi->common);
2404 vpx_free(cpi);
2489 static void generate_psnr_packet(VP8_COMP *cpi)
2491 YV12_BUFFER_CONFIG *orig = cpi->Source;
2492 YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
2496 unsigned int width = cpi->common.Width;
2497 unsigned int height = cpi->common.Height;
2531 vpx_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
2535 int vp8_use_as_reference(VP8_COMP *cpi, int ref_frame_flags)
2540 cpi->ref_frame_flags = ref_frame_flags;
2543 int vp8_update_reference(VP8_COMP *cpi, int ref_frame_flags)
2548 cpi->common.refresh_golden_frame = 0;
2549 cpi->common.refresh_alt_ref_frame = 0;
2550 cpi->common.refresh_last_frame = 0;
2553 cpi->common.refresh_last_frame = 1;
2556 cpi->common.refresh_golden_frame = 1;
2559 cpi->common.refresh_alt_ref_frame = 1;
2564 int vp8_get_reference(VP8_COMP *cpi, enum vpx_ref_frame_type ref_frame_flag, YV12_BUFFER_CONFIG *sd)
2566 VP8_COMMON *cm = &cpi->common;
2582 int vp8_set_reference(VP8_COMP *cpi, enum vpx_ref_frame_type ref_frame_flag, YV12_BUFFER_CONFIG *sd)
2584 VP8_COMMON *cm = &cpi->common;
2601 int vp8_update_entropy(VP8_COMP *cpi, int update)
2603 VP8_COMMON *cm = &cpi->common;
2649 static void scale_and_extend_source(YV12_BUFFER_CONFIG *sd, VP8_COMP *cpi)
2651 VP8_COMMON *cm = &cpi->common;
2669 vpx_scale_frame(sd, &cpi->scaled_source, cm->temp_scale_frame.y_buffer,
2672 vp8_yv12_extend_frame_borders(&cpi->scaled_source);
2673 cpi->Source = &cpi->scaled_source;
2677 cpi->Source = sd;
2681 static int resize_key_frame(VP8_COMP *cpi)
2684 VP8_COMMON *cm = &cpi->common;
2690 if (cpi->oxcf.allow_spatial_resampling && (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER))
2699 if (cpi->buffer_level < (cpi->oxcf.resample_down_water_mark * cpi->oxcf.optimal_buffer_level / 100))
2705 else if (cpi->buffer_level > (cpi->oxcf.resample_up_water_mark * cpi->oxcf.optimal_buffer_level / 100))
2714 new_width = ((hs - 1) + (cpi->oxcf.Width * hr)) / hs;
2715 new_height = ((vs - 1) + (cpi->oxcf.Height * vr)) / vs;
2724 vp8_alloc_compressor_data(cpi);
2725 scale_and_extend_source(cpi->un_scaled_source, cpi);
2735 static void update_alt_ref_frame_stats(VP8_COMP *cpi)
2737 VP8_COMMON *cm = &cpi->common;
2740 if (!cpi->auto_gold)
2741 cpi->frames_till_gf_update_due = DEFAULT_GF_INTERVAL;
2743 if ((cpi->pass != 2) && cpi->frames_till_gf_update_due)
2745 cpi->current_gf_interval = cpi->frames_till_gf_update_due;
2753 cpi->gf_overspend_bits += cpi->projected_frame_size;
2754 cpi->non_gf_bitrate_adjustment = cpi->gf_overspend_bits / cpi->frames_till_gf_update_due;
2758 vpx_memset(cpi->gf_active_flags, 1, (cm->mb_rows * cm->mb_cols));
2759 cpi->gf_active_count = cm->mb_rows * cm->mb_cols;
2762 cpi->frames_since_golden = 0;
2765 cpi->source_alt_ref_pending = 0;
2768 cpi->source_alt_ref_active = 1;
2772 static void update_golden_frame_stats(VP8_COMP *cpi)
2774 VP8_COMMON *cm = &cpi->common;
2780 if (!cpi->auto_gold)
2781 cpi->frames_till_gf_update_due = DEFAULT_GF_INTERVAL;
2783 if ((cpi->pass != 2) && (cpi->frames_till_gf_update_due > 0))
2785 cpi->current_gf_interval = cpi->frames_till_gf_update_due;
2793 if ((cm->frame_type != KEY_FRAME) && !cpi->source_alt_ref_active)
2799 cpi->gf_overspend_bits += (cpi->projected_frame_size - cpi->inter_frame_target);
2802 cpi->non_gf_bitrate_adjustment = cpi->gf_overspend_bits / cpi->frames_till_gf_update_due;
2807 vpx_memset(cpi->gf_active_flags, 1, (cm->mb_rows * cm->mb_cols));
2808 cpi->gf_active_count = cm->mb_rows * cm->mb_cols;
2814 cpi->frames_since_golden = 0;
2816 cpi->recent_ref_frame_usage[INTRA_FRAME] = 1;
2817 cpi->recent_ref_frame_usage[LAST_FRAME] = 1;
2818 cpi->recent_ref_frame_usage[GOLDEN_FRAME] = 1;
2819 cpi->recent_ref_frame_usage[ALTREF_FRAME] = 1;
2825 if (cpi->oxcf.fixed_q >= 0 &&
2826 cpi->oxcf.play_alternate && !cpi->common.refresh_alt_ref_frame)
2828 cpi->source_alt_ref_pending = 1;
2829 cpi->frames_till_gf_update_due = cpi->baseline_gf_interval;
2832 if (!cpi->source_alt_ref_pending)
2833 cpi->source_alt_ref_active = 0;
2836 if (cpi->frames_till_gf_update_due > 0)
2837 cpi->frames_till_gf_update_due--;
2840 else if (!cpi->common.refresh_alt_ref_frame)
2843 if (cpi->frames_till_gf_update_due > 0)
2844 cpi->frames_till_gf_update_due--;
2846 if (cpi->frames_till_alt_ref_frame)
2847 cpi->frames_till_alt_ref_frame --;
2849 cpi->frames_since_golden ++;
2851 if (cpi->frames_since_golden > 1)
2853 cpi->recent_ref_frame_usage[INTRA_FRAME] +=
2854 cpi->mb.count_mb_ref_frame_usage[INTRA_FRAME];
2855 cpi->recent_ref_frame_usage[LAST_FRAME] +=
2856 cpi->mb.count_mb_ref_frame_usage[LAST_FRAME];
2857 cpi->recent_ref_frame_usage[GOLDEN_FRAME] +=
2858 cpi->mb.count_mb_ref_frame_usage[GOLDEN_FRAME];
2859 cpi->recent_ref_frame_usage[ALTREF_FRAME] +=
2860 cpi->mb.count_mb_ref_frame_usage[ALTREF_FRAME];
2868 static void update_rd_ref_frame_probs(VP8_COMP *cpi)
2870 VP8_COMMON *cm = &cpi->common;
2872 const int *const rfct = cpi->mb.count_mb_ref_frame_usage;
2878 cpi->prob_intra_coded = 255;
2879 cpi->prob_last_coded = 128;
2880 cpi->prob_gf_coded = 128;
2884 cpi->prob_intra_coded = 63;
2885 cpi->prob_last_coded = 128;
2886 cpi->prob_gf_coded = 128;
2892 if (cpi->oxcf.number_of_layers == 1)
2894 if (cpi->common.refresh_alt_ref_frame)
2896 cpi->prob_intra_coded += 40;
2897 if (cpi->prob_intra_coded > 255)
2898 cpi->prob_intra_coded = 255;
2899 cpi->prob_last_coded = 200;
2900 cpi->prob_gf_coded = 1;
2902 else if (cpi->frames_since_golden == 0)
2904 cpi->prob_last_coded = 214;
2906 else if (cpi->frames_since_golden == 1)
2908 cpi->prob_last_coded = 192;
2909 cpi->prob_gf_coded = 220;
2911 else if (cpi->source_alt_ref_active)
2913 cpi->prob_gf_coded -= 20;
2915 if (cpi->prob_gf_coded < 10)
2916 cpi->prob_gf_coded = 10;
2918 if (!cpi->source_alt_ref_active)
2919 cpi->prob_gf_coded = 255;
2925 static int decide_key_frame(VP8_COMP *cpi)
2927 VP8_COMMON *cm = &cpi->common;
2931 cpi->kf_boost = 0;
2933 if (cpi->Speed > 11)
2939 if ((cpi->compressor_speed == 2) && (cpi->Speed >= 5) && (cpi->sf.RD == 0))
2941 double change = 1.0 * abs((int)(cpi->mb.intra_error -
2942 cpi->last_intra_error)) / (1 + cpi->last_intra_error);
2943 double change2 = 1.0 * abs((int)(cpi->mb.prediction_error -
2944 cpi->last_prediction_error)) / (1 + cpi->last_prediction_error);
2947 cpi->last_intra_error = cpi->mb.intra_error;
2948 cpi->last_prediction_error = cpi->mb.prediction_error;
2950 if (10 * cpi->mb.intra_error / (1 + cpi->mb.prediction_error) < 15
2951 && cpi->mb.prediction_error > minerror
2954 /*(change > 1.4 || change < .75)&& cpi->this_frame_percent_intra > cpi->last_frame_percent_intra + 3*/
2963 if (((cpi->this_frame_percent_intra == 100) &&
2964 (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra + 2))) ||
2965 ((cpi->this_frame_percent_intra > 95) &&
2966 (cpi->this_frame_percent_intra >= (cpi->last_frame_percent_intra + 5))))
2976 else if (((cpi->this_frame_percent_intra > 60) &&
2977 (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra * 2))) ||
2978 ((cpi->this_frame_percent_intra > 75) &&
2979 (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra * 3 / 2))) ||
2980 ((cpi->this_frame_percent_intra > 90) &&
2981 (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra + 10))))
2992 static void Pass1Encode(VP8_COMP *cpi, unsigned long *size, unsigned char *dest, unsigned int *frame_flags)
2997 vp8_set_quantizer(cpi, 26);
2999 vp8_first_pass(cpi);
3040 static int recode_loop_test( VP8_COMP *cpi,
3045 VP8_COMMON *cm = &cpi->common;
3051 if ( (cpi->sf.recode_loop == 1) ||
3052 ( (cpi->sf.recode_loop == 2) &&
3058 if ( ((cpi->projected_frame_size > high_limit) && (q < maxq)) ||
3059 ((cpi->projected_frame_size < low_limit) && (q > minq)) )
3064 else if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY)
3067 if ( (q > cpi->cq_target_quality) &&
3068 (cpi->projected_frame_size <
3069 ((cpi->this_frame_target * 7) >> 3)))
3074 else if ( (q > cpi->oxcf.cq_level) &&
3075 (cpi->projected_frame_size < cpi->min_frame_bandwidth) &&
3076 (cpi->active_best_quality > cpi->oxcf.cq_level))
3079 cpi->active_best_quality = cpi->oxcf.cq_level;
3087 static void update_reference_frames(VP8_COMP *cpi)
3089 VP8_COMMON *cm = &cpi->common;
3106 cpi->current_ref_frames[GOLDEN_FRAME] = cm->current_video_frame;
3107 cpi->current_ref_frames[ALTREF_FRAME] = cm->current_video_frame;
3121 cpi->current_ref_frames[ALTREF_FRAME] = cm->current_video_frame;
3137 cpi->current_ref_frames[ALTREF_FRAME] =
3138 cpi->current_ref_frames[LAST_FRAME];
3151 cpi->current_ref_frames[ALTREF_FRAME] =
3152 cpi->current_ref_frames[GOLDEN_FRAME];
3167 cpi->current_ref_frames[GOLDEN_FRAME] = cm->current_video_frame;
3183 cpi->current_ref_frames[GOLDEN_FRAME] =
3184 cpi->current_ref_frames[LAST_FRAME];
3197 cpi->current_ref_frames[GOLDEN_FRAME] =
3198 cpi->current_ref_frames[ALTREF_FRAME];
3212 cpi->current_ref_frames[LAST_FRAME] = cm->current_video_frame;
3217 if (cpi->oxcf.noise_sensitivity)
3227 cpi->Source,
3228 &cpi->denoiser.yv12_running_avg[LAST_FRAME]);
3231 &cpi->denoiser.yv12_running_avg[LAST_FRAME]);
3235 &cpi->denoiser.yv12_running_avg[LAST_FRAME],
3236 &cpi->denoiser.yv12_running_avg[i]);
3241 &cpi->denoiser.yv12_running_avg[INTRA_FRAME]);
3246 &cpi->denoiser.yv12_running_avg[INTRA_FRAME],
3247 &cpi->denoiser.yv12_running_avg[ALTREF_FRAME]);
3252 &cpi->denoiser.yv12_running_avg[INTRA_FRAME],
3253 &cpi->denoiser.yv12_running_avg[GOLDEN_FRAME]);
3258 &cpi->denoiser.yv12_running_avg[INTRA_FRAME],
3259 &cpi->denoiser.yv12_running_avg[LAST_FRAME]);
3268 void vp8_loopfilter_frame(VP8_COMP *cpi, VP8_COMMON *cm)
3283 if (cpi->sf.auto_filter == 0)
3284 vp8cx_pick_filter_level_fast(cpi->Source, cpi);
3287 vp8cx_pick_filter_level(cpi->Source, cpi);
3291 vp8cx_set_alt_lf_level(cpi, cm->filter_level);
3295 cpi->time_pick_lpf += vpx_usec_timer_elapsed(&timer);
3299 if (cpi->b_multi_threaded)
3300 sem_post(&cpi->h_event_end_lpf); /* signal that we have set filter_level */
3305 vp8_loop_filter_frame(cm, &cpi->mb.e_mbd, frame_type);
3314 VP8_COMP *cpi,
3328 VP8_COMMON *cm = &cpi->common;
3342 int drop_mark = (int)(cpi->oxcf.drop_frames_water_mark *
3343 cpi->oxcf.optimal_buffer_level / 100);
3354 if (cpi->b_lpf_running)
3356 sem_wait(&cpi->h_event_end_lpf);
3357 cpi->b_lpf_running = 0;
3361 if(cpi->force_next_frame_intra)
3364 cpi->force_next_frame_intra = 0;
3372 if (cpi->pass == 2)
3374 if (cpi->common.refresh_alt_ref_frame)
3377 cpi->per_frame_bandwidth = cpi->twopass.gf_bits;
3379 cpi->target_bandwidth = (int)(cpi->twopass.gf_bits *
3380 cpi->output_framerate);
3385 cpi->per_frame_bandwidth = (int)(cpi->target_bandwidth / cpi->output_framerate);
3392 cpi->mb.zbin_over_quant = 0;
3393 cpi->mb.zbin_mode_boost = 0;
3399 cpi->mb.zbin_mode_boost_enabled = 1;
3400 if (cpi->pass == 2)
3402 if ( cpi->gfu_boost <= 400 )
3404 cpi->mb.zbin_mode_boost_enabled = 0;
3409 if (cpi->source_alt_ref_active)
3410 cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = 1;
3412 cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = 0;
3420 (cpi->oxcf.auto_key && (cpi->frames_since_key % cpi->key_frame_frequency == 0)))
3430 if (cpi->oxcf.mr_encoder_id)
3433 = (LOWER_RES_FRAME_INFO*)cpi->oxcf.mr_low_res_mode_info;
3439 cpi->mr_low_res_mv_avail = 1;
3440 cpi->mr_low_res_mv_avail &= !(low_res_frame_info->is_frame_dropped);
3442 if (cpi->ref_frame_flags & VP8_LAST_FRAME)
3443 cpi->mr_low_res_mv_avail &= (cpi->current_ref_frames[LAST_FRAME]
3446 if (cpi->ref_frame_flags & VP8_GOLD_FRAME)
3447 cpi->mr_low_res_mv_avail &= (cpi->current_ref_frames[GOLDEN_FRAME]
3450 if (cpi->ref_frame_flags & VP8_ALTR_FRAME)
3451 cpi->mr_low_res_mv_avail &= (cpi->current_ref_frames[ALTREF_FRAME]
3463 setup_features(cpi);
3466 cpi->source_alt_ref_active = 0;
3471 cpi->mb.rd_thresh_mult[i] = 128;
3481 cpi->one_pass_frame_index = cm->current_video_frame % MAX_LAG_BUFFERS;
3483 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frames_so_far = 0;
3484 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_intra_error = 0.0;
3485 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_coded_error = 0.0;
3486 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_pcnt_inter = 0.0;
3487 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_pcnt_motion = 0.0;
3488 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvr = 0.0;
3489 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvr_abs = 0.0;
3490 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvc = 0.0;
3491 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvc_abs = 0.0;
3495 update_rd_ref_frame_probs(cpi);
3497 if (cpi->drop_frames_allowed)
3502 if ((cpi->buffer_level > drop_mark) && (cpi->decimation_factor > 0))
3503 cpi->decimation_factor --;
3505 if (cpi->buffer_level > drop_mark75 && cpi->decimation_factor > 0)
3506 cpi->decimation_factor = 1;
3508 else if (cpi->buffer_level < drop_mark25 && (cpi->decimation_factor == 2 || cpi->decimation_factor == 3))
3510 cpi->decimation_factor = 3;
3512 else if (cpi->buffer_level < drop_mark50 && (cpi->decimation_factor == 1 || cpi->decimation_factor == 2))
3514 cpi->decimation_factor = 2;
3516 else if (cpi->buffer_level < drop_mark75 && (cpi->decimation_factor == 0 || cpi->decimation_factor == 1))
3518 cpi->decimation_factor = 1;
3531 if (cpi->decimation_factor > 0)
3533 switch (cpi->decimation_factor)
3536 cpi->per_frame_bandwidth = cpi->per_frame_bandwidth * 3 / 2;
3539 cpi->per_frame_bandwidth = cpi->per_frame_bandwidth * 5 / 4;
3542 cpi->per_frame_bandwidth = cpi->per_frame_bandwidth * 5 / 4;
3551 cpi->decimation_count = cpi->decimation_factor;
3553 else if (cpi->decimation_count > 0)
3555 cpi->decimation_count --;
3557 cpi->bits_off_target += cpi->av_per_frame_bandwidth;
3558 if (cpi->bits_off_target > cpi->oxcf.maximum_buffer_size)
3559 cpi->bits_off_target = cpi->oxcf.maximum_buffer_size;
3562 vp8_store_drop_frame_info(cpi);
3566 cpi->frames_since_key++;
3568 cpi->temporal_pattern_counter++;
3571 cpi->count ++;
3574 cpi->buffer_level = cpi->bits_off_target;
3576 if (cpi->oxcf.number_of_layers > 1)
3583 for (i=cpi->current_layer+1; i<cpi->oxcf.number_of_layers; i++)
3585 LAYER_CONTEXT *lc = &cpi->layer_context[i];
3597 cpi->decimation_count = cpi->decimation_factor;
3600 cpi->decimation_count = 0;
3603 if (!vp8_pick_frame_size(cpi))
3607 vp8_store_drop_frame_info(cpi);
3610 cpi->frames_since_key++;
3612 cpi->temporal_pattern_counter++;
3622 if ((cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) &&
3623 (cpi->buffer_level >= cpi->oxcf.optimal_buffer_level) && cpi->buffered_mode)
3626 int Adjustment = cpi->active_worst_quality / 4;
3632 if (cpi->buffer_level < cpi->oxcf.maximum_buffer_size)
3635 ((cpi->oxcf.maximum_buffer_size -
3636 cpi->oxcf.optimal_buffer_level) /
3641 ((cpi->buffer_level -
3642 cpi->oxcf.optimal_buffer_level) /
3648 cpi->active_worst_quality -= Adjustment;
3650 if(cpi->active_worst_quality < cpi->active_best_quality)
3651 cpi->active_worst_quality = cpi->active_best_quality;
3658 if ( (cpi->pass == 2) || (cpi->ni_frames > 150))
3662 Q = cpi->active_worst_quality;
3666 if ( cpi->pass == 2 )
3668 if (cpi->gfu_boost > 600)
3669 cpi->active_best_quality = kf_low_motion_minq[Q];
3671 cpi->active_best_quality = kf_high_motion_minq[Q];
3677 if ( cpi->this_key_frame_forced )
3679 if ( cpi->active_best_quality > cpi->avg_frame_qindex * 7/8)
3680 cpi->active_best_quality = cpi->avg_frame_qindex * 7/8;
3681 else if ( cpi->active_best_quality < cpi->avg_frame_qindex >> 2 )
3682 cpi->active_best_quality = cpi->avg_frame_qindex >> 2;
3687 cpi->active_best_quality = kf_high_motion_minq[Q];
3690 else if (cpi->oxcf.number_of_layers==1 &&
3691 (cm->refresh_golden_frame || cpi->common.refresh_alt_ref_frame))
3693 /* Use the lower of cpi->active_worst_quality and recent
3697 if ( (cpi->frames_since_key > 1) &&
3698 (cpi->avg_frame_qindex < cpi->active_worst_quality) )
3700 Q = cpi->avg_frame_qindex;
3704 if ( (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) &&
3705 (Q < cpi->cq_target_quality) )
3707 Q = cpi->cq_target_quality;
3710 if ( cpi->pass == 2 )
3712 if ( cpi->gfu_boost > 1000 )
3713 cpi->active_best_quality = gf_low_motion_minq[Q];
3714 else if ( cpi->gfu_boost < 400 )
3715 cpi->active_best_quality = gf_high_motion_minq[Q];
3717 cpi->active_best_quality = gf_mid_motion_minq[Q];
3720 if ( cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY )
3722 cpi->active_best_quality =
3723 cpi->active_best_quality * 15/16;
3728 cpi->active_best_quality = gf_high_motion_minq[Q];
3732 cpi->active_best_quality = inter_minq[Q];
3737 if ((cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) &&
3738 (cpi->active_best_quality < cpi->cq_target_quality) )
3744 if ( cpi->rolling_actual_bits < cpi->min_frame_bandwidth )
3745 cpi->active_best_quality = cpi->oxcf.cq_level;
3747 cpi->active_best_quality = cpi->cq_target_quality;
3754 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
3759 if (cpi->buffer_level >= cpi->oxcf.maximum_buffer_size)
3760 cpi->active_best_quality = cpi->best_quality;
3762 else if (cpi->buffer_level > cpi->oxcf.optimal_buffer_level)
3765 (((cpi->buffer_level - cpi->oxcf.optimal_buffer_level) * 128)
3766 / (cpi->oxcf.maximum_buffer_size -
3767 cpi->oxcf.optimal_buffer_level));
3768 int min_qadjustment = ((cpi->active_best_quality -
3769 cpi->best_quality) * Fraction) / 128;
3771 cpi->active_best_quality -= min_qadjustment;
3778 else if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY)
3781 cm->refresh_golden_frame || cpi->common.refresh_alt_ref_frame )
3783 cpi->active_best_quality = cpi->best_quality;
3785 else if (cpi->active_best_quality < cpi->cq_target_quality)
3787 cpi->active_best_quality = cpi->cq_target_quality;
3792 if (cpi->active_worst_quality > cpi->worst_quality)
3793 cpi->active_worst_quality = cpi->worst_quality;
3795 if (cpi->active_best_quality < cpi->best_quality)
3796 cpi->active_best_quality = cpi->best_quality;
3798 if ( cpi->active_worst_quality < cpi->active_best_quality )
3799 cpi->active_worst_quality = cpi->active_best_quality;
3802 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
3809 else if ((cpi->oxcf.number_of_layers == 1) && ((cm->refresh_alt_ref_frame ||
3810 (cm->refresh_golden_frame && !cpi->source_alt_ref_active))))
3821 if (cpi->cyclic_refresh_mode_enabled)
3823 if (cpi->current_layer==0)
3824 cyclic_background_refresh(cpi, Q, 0);
3826 disable_segmentation(cpi);
3829 vp8_compute_frame_size_bounds(cpi, &frame_under_shoot_limit, &frame_over_shoot_limit);
3833 bottom_index = cpi->active_best_quality;
3834 top_index = cpi->active_worst_quality;
3835 q_low = cpi->active_best_quality;
3836 q_high = cpi->active_worst_quality;
3839 vp8_save_coding_context(cpi);
3843 scale_and_extend_source(cpi->un_scaled_source, cpi);
3847 if (cpi->oxcf.noise_sensitivity > 0)
3852 switch (cpi->oxcf.noise_sensitivity)
3877 vp8_de_noise(cm, cpi->Source, cpi->Source, l , 1, 0);
3881 vp8_de_noise(cm, cpi->Source, cpi->Source, l , 1, 0);
3883 src = cpi->Source->y_buffer;
3885 if (cpi->Source->y_stride < 0)
3887 src += cpi->Source->y_stride * (cpi->Source->y_height - 1);
3895 vp8_write_yuv_frame(cpi->Source);
3902 vp8_set_quantizer(cpi, Q);
3905 if (cpi->common.mb_no_coeff_skip)
3907 cpi->prob_skip_false = cpi->base_skip_false_prob[Q];
3911 if (cpi->common.refresh_alt_ref_frame)
3913 if (cpi->last_skip_false_probs[2] != 0)
3914 cpi->prob_skip_false = cpi->last_skip_false_probs[2];
3917 if(cpi->last_skip_false_probs[2]!=0 && abs(Q- cpi->last_skip_probs_q[2])<=16 )
3918 cpi->prob_skip_false = cpi->last_skip_false_probs[2];
3919 else if (cpi->last_skip_false_probs[2]!=0)
3920 cpi->prob_skip_false = (cpi->last_skip_false_probs[2] + cpi->prob_skip_false ) / 2;
3923 else if (cpi->common.refresh_golden_frame)
3925 if (cpi->last_skip_false_probs[1] != 0)
3926 cpi->prob_skip_false = cpi->last_skip_false_probs[1];
3929 if(cpi->last_skip_false_probs[1]!=0 && abs(Q- cpi->last_skip_probs_q[1])<=16 )
3930 cpi->prob_skip_false = cpi->last_skip_false_probs[1];
3931 else if (cpi->last_skip_false_probs[1]!=0)
3932 cpi->prob_skip_false = (cpi->last_skip_false_probs[1] + cpi->prob_skip_false ) / 2;
3937 if (cpi->last_skip_false_probs[0] != 0)
3938 cpi->prob_skip_false = cpi->last_skip_false_probs[0];
3941 if(cpi->last_skip_false_probs[0]!=0 && abs(Q- cpi->last_skip_probs_q[0])<=16 )
3942 cpi->prob_skip_false = cpi->last_skip_false_probs[0];
3943 else if(cpi->last_skip_false_probs[0]!=0)
3944 cpi->prob_skip_false = (cpi->last_skip_false_probs[0] + cpi->prob_skip_false ) / 2;
3951 if (cpi->prob_skip_false < 5)
3952 cpi->prob_skip_false = 5;
3954 if (cpi->prob_skip_false > 250)
3955 cpi->prob_skip_false = 250;
3957 if (cpi->oxcf.number_of_layers == 1 && cpi->is_src_frame_alt_ref)
3958 cpi->prob_skip_false = 1;
3963 if (cpi->pass != 1)
3966 fprintf(f, "%d, %d, %4d ", cpi->common.refresh_golden_frame, cpi->common.refresh_alt_ref_frame, cpi->prob_skip_false);
3976 if(resize_key_frame(cpi))
3981 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
3982 if (cpi->cyclic_refresh_mode_enabled)
3984 if (cpi->current_layer==0)
3985 cyclic_background_refresh(cpi, Q, 0);
3987 disable_segmentation(cpi);
3989 vp8_set_quantizer(cpi, Q);
3992 vp8_setup_key_frame(cpi);
3999 if(cpi->oxcf.error_resilient_mode)
4002 if (cpi->oxcf.error_resilient_mode & VPX_ERROR_RESILIENT_PARTITIONS)
4014 vp8_update_coef_context(cpi);
4016 vp8_update_coef_probs(cpi);
4021 vp8_encode_frame(cpi);
4023 /* cpi->projected_frame_size is not needed for RT mode */
4027 vp8_encode_frame(cpi);
4029 cpi->projected_frame_size -= vp8_estimate_entropy_savings(cpi);
4030 cpi->projected_frame_size = (cpi->projected_frame_size > 0) ? cpi->projected_frame_size : 0;
4038 if (cpi->pass != 2 && cpi->oxcf.auto_key && cm->frame_type != KEY_FRAME
4039 && cpi->compressor_speed != 2)
4042 if (decide_key_frame(cpi))
4047 vp8_pick_frame_size(cpi);
4052 cpi->source_alt_ref_active = 0;
4055 setup_features(cpi);
4057 vp8_restore_coding_context(cpi);
4059 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4061 vp8_compute_frame_size_bounds(cpi, &frame_under_shoot_limit, &frame_over_shoot_limit);
4064 bottom_index = cpi->active_best_quality;
4065 top_index = cpi->active_worst_quality;
4066 q_low = cpi->active_best_quality;
4067 q_high = cpi->active_worst_quality;
4083 if (((cpi->pass != 2) || (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)) &&
4084 (Q == cpi->active_worst_quality) &&
4085 (cpi->active_worst_quality < cpi->worst_quality) &&
4086 (cpi->projected_frame_size > frame_over_shoot_limit))
4088 int over_size_percent = ((cpi->projected_frame_size - frame_over_shoot_limit) * 100) / frame_over_shoot_limit;
4091 while ((cpi->active_worst_quality < cpi->worst_quality) && (over_size_percent > 0))
4093 cpi->active_worst_quality++;
4098 top_index = cpi->active_worst_quality;
4110 if ( (cm->frame_type == KEY_FRAME) && cpi->this_key_frame_forced )
4113 int kf_err = vp8_calc_ss_err(cpi->Source,
4117 if ( kf_err > ((cpi->ambient_err * 7) >> 3) )
4126 else if ( kf_err < (cpi->ambient_err >> 1) )
4147 else if ( recode_loop_test( cpi,
4159 if (cpi->projected_frame_size > cpi->this_frame_target)
4165 if (cpi->mb.zbin_over_quant > 0)
4166 zbin_oq_low = (cpi->mb.zbin_over_quant < zbin_oq_high) ?
4167 (cpi->mb.zbin_over_quant + 1) : zbin_oq_high;
4172 * cpi->active_worst_quality has changed.
4175 vp8_update_rate_correction_factors(cpi, 1);
4179 /* Adjust cpi->zbin_over_quant (only allowed when Q
4183 cpi->mb.zbin_over_quant = 0;
4186 zbin_oq_low = (cpi->mb.zbin_over_quant < zbin_oq_high) ?
4187 (cpi->mb.zbin_over_quant + 1) : zbin_oq_high;
4188 cpi->mb.zbin_over_quant =
4195 * cpi->active_worst_quality has changed.
4198 vp8_update_rate_correction_factors(cpi, 0);
4200 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4203 (cpi->mb.zbin_over_quant < zbin_oq_low)) &&
4206 vp8_update_rate_correction_factors(cpi, 0);
4207 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4217 if (cpi->mb.zbin_over_quant == 0)
4222 zbin_oq_high = (cpi->mb.zbin_over_quant > zbin_oq_low) ?
4223 (cpi->mb.zbin_over_quant - 1) : zbin_oq_low;
4228 * cpi->active_worst_quality has changed.
4231 vp8_update_rate_correction_factors(cpi, 1);
4235 /* Adjust cpi->zbin_over_quant (only allowed when Q
4239 cpi->mb.zbin_over_quant = 0;
4241 cpi->mb.zbin_over_quant =
4247 * cpi->active_worst_quality has changed.
4250 vp8_update_rate_correction_factors(cpi, 0);
4252 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4259 if ( (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) &&
4266 (cpi->mb.zbin_over_quant > zbin_oq_high)) &&
4269 vp8_update_rate_correction_factors(cpi, 0);
4270 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4284 /* Clamp cpi->zbin_over_quant */
4285 cpi->mb.zbin_over_quant = (cpi->mb.zbin_over_quant < zbin_oq_low) ?
4286 zbin_oq_low : (cpi->mb.zbin_over_quant > zbin_oq_high) ?
4287 zbin_oq_high : cpi->mb.zbin_over_quant;
4295 if (cpi->is_src_frame_alt_ref)
4300 vp8_restore_coding_context(cpi);
4303 cpi->tot_recode_hits++;
4314 cpi->one_pass_frame_stats[cpi->one_pass_frame_index].frame_coded_error = (double)cpi->prediction_error;
4315 cpi->one_pass_frame_stats[cpi->one_pass_frame_index].frame_intra_error = (double)cpi->intra_error;
4316 cpi->one_pass_frame_stats[cpi->one_pass_frame_index].frame_pcnt_inter = (double)(100 - cpi->this_frame_percent_intra) / 100.0;
4324 if ( cpi->next_key_frame_forced && (cpi->twopass.frames_to_key == 0) )
4326 cpi->ambient_err = vp8_calc_ss_err(cpi->Source,
4335 if(!cpi->oxcf.mr_encoder_id && cm->show_frame)
4352 cpi->lfmv[mb_col + mb_row*(cm->mode_info_stride+1)].as_int = tmp->mbmi.mv.as_int;
4354 cpi->lf_ref_frame_sign_bias[mb_col + mb_row*(cm->mode_info_stride+1)] = cm->ref_frame_sign_bias[tmp->mbmi.ref_frame];
4355 cpi->lf_ref_frame[mb_col + mb_row*(cm->mode_info_stride+1)] = tmp->mbmi.ref_frame;
4369 cpi->zeromv_count = 0;
4378 cpi->zeromv_count++;
4387 vp8_cal_dissimilarity(cpi);
4394 if (cpi->oxcf.number_of_layers == 1)
4395 vp8_update_gf_useage_maps(cpi, cm, &cpi->mb);
4403 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);
4412 if (!cpi->oxcf.error_resilient_mode && cm->refresh_golden_frame)
4420 if (cpi->b_multi_threaded)
4423 sem_post(&cpi->h_event_start_lpf);
4424 cpi->b_lpf_running = 1;
4429 vp8_loopfilter_frame(cpi, cm);
4432 update_reference_frames(cpi);
4435 if (cpi->oxcf.error_resilient_mode)
4443 if (cpi->b_multi_threaded)
4444 sem_wait(&cpi->h_event_end_lpf);
4448 vp8_pack_bitstream(cpi, dest, dest_end, size);
4452 if (cpi->b_lpf_running && cpi->b_calculate_psnr)
4454 sem_wait(&cpi->h_event_end_lpf);
4455 cpi->b_lpf_running = 0;
4464 cpi->total_byte_count += (*size);
4465 cpi->projected_frame_size = (*size) << 3;
4467 if (cpi->oxcf.number_of_layers > 1)
4470 for (i=cpi->current_layer+1; i<cpi->oxcf.number_of_layers; i++)
4471 cpi->layer_context[i].total_byte_count += (*size);
4475 vp8_update_rate_correction_factors(cpi, 2);
4477 cpi->last_q[cm->frame_type] = cm->base_qindex;
4481 vp8_adjust_key_frame_context(cpi);
4486 cpi->avg_frame_qindex = (2 + 3 * cpi->avg_frame_qindex + cm->base_qindex) >> 2;
4491 if ((cm->frame_type != KEY_FRAME) && ((cpi->oxcf.number_of_layers > 1) ||
4494 cpi->ni_frames++;
4499 if ( cpi->pass == 2 )
4501 cpi->ni_tot_qi += Q;
4502 cpi->ni_av_qi = (cpi->ni_tot_qi / cpi->ni_frames);
4507 if (cpi->ni_frames > 150 )
4509 cpi->ni_tot_qi += Q;
4510 cpi->ni_av_qi = (cpi->ni_tot_qi / cpi->ni_frames);
4517 cpi->ni_tot_qi += Q;
4518 cpi->ni_av_qi = ((cpi->ni_tot_qi / cpi->ni_frames) + cpi->worst_quality + 1) / 2;
4529 if (Q > cpi->ni_av_qi)
4530 cpi->ni_av_qi = Q - 1;
4537 cpi->bits_off_target -= cpi->projected_frame_size;
4539 cpi->bits_off_target += cpi->av_per_frame_bandwidth - cpi->projected_frame_size;
4542 if (cpi->bits_off_target > cpi->oxcf.maximum_buffer_size)
4543 cpi->bits_off_target = cpi->oxcf.maximum_buffer_size;
4548 cpi->rolling_target_bits = ((cpi->rolling_target_bits * 3) + cpi->this_frame_target + 2) / 4;
4549 cpi->rolling_actual_bits = ((cpi->rolling_actual_bits * 3) + cpi->projected_frame_size + 2) / 4;
4550 cpi->long_rolling_target_bits = ((cpi->long_rolling_target_bits * 31) + cpi->this_frame_target + 16) / 32;
4551 cpi->long_rolling_actual_bits = ((cpi->long_rolling_actual_bits * 31) + cpi->projected_frame_size + 16) / 32;
4554 cpi->total_actual_bits += cpi->projected_frame_size;
4557 cpi->total_target_vs_actual += (cpi->this_frame_target - cpi->projected_frame_size);
4559 cpi->buffer_level = cpi->bits_off_target;
4562 if (cpi->oxcf.number_of_layers > 1)
4566 for (i=cpi->current_layer+1; i<cpi->oxcf.number_of_layers; i++)
4568 LAYER_CONTEXT *lc = &cpi->layer_context[i];
4571 cpi->projected_frame_size);
4579 lc->total_actual_bits += cpi->projected_frame_size;
4590 cpi->twopass.kf_group_bits += cpi->this_frame_target - cpi->projected_frame_size;
4592 if (cpi->twopass.kf_group_bits < 0)
4593 cpi->twopass.kf_group_bits = 0 ;
4597 cpi->twopass.gf_group_bits += cpi->this_frame_target - cpi->projected_frame_size;
4599 if (cpi->twopass.gf_group_bits < 0)
4600 cpi->twopass.gf_group_bits = 0 ;
4605 if (cpi->common.refresh_alt_ref_frame)
4607 cpi->last_skip_false_probs[2] = cpi->prob_skip_false;
4608 cpi->last_skip_probs_q[2] = cm->base_qindex;
4610 else if (cpi->common.refresh_golden_frame)
4612 cpi->last_skip_false_probs[1] = cpi->prob_skip_false;
4613 cpi->last_skip_probs_q[1] = cm->base_qindex;
4617 cpi->last_skip_false_probs[0] = cpi->prob_skip_false;
4618 cpi->last_skip_probs_q[0] = cm->base_qindex;
4621 cpi->base_skip_false_prob[cm->base_qindex] = cpi->prob_skip_false;
4632 if (cpi->twopass.total_left_stats.coded_error != 0.0)
4636 cpi->common.current_video_frame, cpi->this_frame_target,
4637 cpi->projected_frame_size,
4638 (cpi->projected_frame_size - cpi->this_frame_target),
4639 cpi->total_target_vs_actual,
4640 cpi->buffer_level,
4641 (cpi->oxcf.starting_buffer_level-cpi->bits_off_target),
4642 cpi->total_actual_bits, cm->base_qindex,
4643 cpi->active_best_quality, cpi->active_worst_quality,
4644 cpi->ni_av_qi, cpi->cq_target_quality,
4646 cm->frame_type, cpi->gfu_boost,
4647 cpi->twopass.est_max_qcorrection_factor,
4648 cpi->twopass.bits_left,
4649 cpi->twopass.total_left_stats.coded_error,
4650 (double)cpi->twopass.bits_left /
4651 cpi->twopass.total_left_stats.coded_error,
4652 cpi->tot_recode_hits);
4657 cpi->common.current_video_frame, cpi->this_frame_target,
4658 cpi->projected_frame_size,
4659 (cpi->projected_frame_size - cpi->this_frame_target),
4660 cpi->total_target_vs_actual,
4661 cpi->buffer_level,
4662 (cpi->oxcf.starting_buffer_level-cpi->bits_off_target),
4663 cpi->total_actual_bits, cm->base_qindex,
4664 cpi->active_best_quality, cpi->active_worst_quality,
4665 cpi->ni_av_qi, cpi->cq_target_quality,
4667 cm->frame_type, cpi->gfu_boost,
4668 cpi->twopass.est_max_qcorrection_factor,
4669 cpi->twopass.bits_left,
4670 cpi->twopass.total_left_stats.coded_error,
4671 cpi->tot_recode_hits);
4679 cpi->common.current_video_frame,
4704 cpi->gold_is_last = 1;
4707 cpi->gold_is_last = 0;
4711 cpi->alt_is_last = 1;
4714 cpi->alt_is_last = 0;
4718 cpi->gold_is_alt = 1;
4721 cpi->gold_is_alt = 0;
4723 cpi->ref_frame_flags = VP8_ALTR_FRAME | VP8_GOLD_FRAME | VP8_LAST_FRAME;
4725 if (cpi->gold_is_last)
4726 cpi->ref_frame_flags &= ~VP8_GOLD_FRAME;
4728 if (cpi->alt_is_last)
4729 cpi->ref_frame_flags &= ~VP8_ALTR_FRAME;
4731 if (cpi->gold_is_alt)
4732 cpi->ref_frame_flags &= ~VP8_ALTR_FRAME;
4735 if (!cpi->oxcf.error_resilient_mode)
4737 if (cpi->oxcf.play_alternate && cm->refresh_alt_ref_frame && (cm->frame_type != KEY_FRAME))
4739 update_alt_ref_frame_stats(cpi);
4742 update_golden_frame_stats(cpi);
4753 cpi->last_frame_percent_intra = 100;
4759 cpi->last_frame_percent_intra = cpi->this_frame_percent_intra;
4765 cpi->mb.e_mbd.update_mb_segmentation_map = 0;
4766 cpi->mb.e_mbd.update_mb_segmentation_data = 0;
4767 cpi->mb.e_mbd.mode_ref_lf_delta_update = 0;
4776 cpi->frames_since_key++;
4777 cpi->temporal_pattern_counter++;
4802 static void Pass2Encode(VP8_COMP *cpi, unsigned long *size, unsigned char *dest, unsigned char * dest_end, unsigned int *frame_flags)
4805 if (!cpi->common.refresh_alt_ref_frame)
4806 vp8_second_pass(cpi);
4808 encode_frame_to_data_rate(cpi, size, dest, dest_end, frame_flags);
4809 cpi->twopass.bits_left -= 8 * *size;
4811 if (!cpi->common.refresh_alt_ref_frame)
4813 double two_pass_min_rate = (double)(cpi->oxcf.target_bandwidth
4814 *cpi->oxcf.two_pass_vbrmin_section / 100);
4815 cpi->twopass.bits_left += (int64_t)(two_pass_min_rate / cpi->framerate);
4827 int vp8_receive_raw_frame(VP8_COMP *cpi, unsigned int frame_flags, YV12_BUFFER_CONFIG *sd, int64_t time_stamp, int64_t end_time)
4832 VP8_COMMON *cm = &cpi->common;
4850 if (sd->y_width != cpi->oxcf.Width || sd->y_height != cpi->oxcf.Height)
4852 assert(cpi->oxcf.lag_in_frames < 2);
4853 dealloc_raw_frame_buffers(cpi);
4854 alloc_raw_frame_buffers(cpi);
4857 if(vp8_lookahead_push(cpi->lookahead, sd, time_stamp, end_time,
4858 frame_flags, cpi->active_map_enabled ? cpi->active_map : NULL))
4861 cpi->time_receive_data += vpx_usec_timer_elapsed(&timer);
4876 static int frame_is_reference(const VP8_COMP *cpi)
4878 const VP8_COMMON *cm = &cpi->common;
4879 const MACROBLOCKD *xd = &cpi->mb.e_mbd;
4890 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)
4901 if (!cpi)
4904 cm = &cpi->common;
4906 if (setjmp(cpi->common.error.jmp))
4908 cpi->common.error.setjmp = 0;
4912 cpi->common.error.setjmp = 1;
4925 cpi->source = NULL;
4929 if (cpi->oxcf.error_resilient_mode == 0 &&
4930 cpi->oxcf.play_alternate &&
4931 cpi->source_alt_ref_pending)
4933 if ((cpi->source = vp8_lookahead_peek(cpi->lookahead,
4934 cpi->frames_till_gf_update_due,
4937 cpi->alt_ref_source = cpi->source;
4938 if (cpi->oxcf.arnr_max_frames > 0)
4940 vp8_temporal_filter_prepare_c(cpi,
4941 cpi->frames_till_gf_update_due);
4942 force_src_buffer = &cpi->alt_ref_buffer;
4944 cpi->frames_till_alt_ref_frame = cpi->frames_till_gf_update_due;
4950 cpi->source_alt_ref_pending = 0;
4951 cpi->is_src_frame_alt_ref = 0;
4956 if (!cpi->source)
4959 if (cpi->pass == 1 && cm->current_video_frame > 0)
4961 if((cpi->last_source = vp8_lookahead_peek(cpi->lookahead, 1,
4967 if ((cpi->source = vp8_lookahead_pop(cpi->lookahead, flush)))
4971 cpi->is_src_frame_alt_ref = cpi->alt_ref_source
4972 && (cpi->source == cpi->alt_ref_source);
4974 if(cpi->is_src_frame_alt_ref)
4975 cpi->alt_ref_source = NULL;
4979 if (cpi->source)
4981 cpi->Source = force_src_buffer ? force_src_buffer : &cpi->source->img;
4982 cpi->un_scaled_source = cpi->Source;
4983 *time_stamp = cpi->source->ts_start;
4984 *time_end = cpi->source->ts_end;
4985 *frame_flags = cpi->source->flags;
4987 if (cpi->pass == 1 && cm->current_video_frame > 0)
4989 cpi->last_frame_unscaled_source = &cpi->last_source->img;
4997 if (flush && cpi->pass == 1 && !cpi->twopass.first_pass_done)
4999 vp8_end_first_pass(cpi); /* get last stats packet */
5000 cpi->twopass.first_pass_done = 1;
5016 if (cpi->source->ts_start < cpi->first_time_stamp_ever)
5018 cpi->first_time_stamp_ever = cpi->source->ts_start;
5019 cpi->last_end_time_stamp_seen = cpi->source->ts_start;
5028 if (cpi->source->ts_start == cpi->first_time_stamp_ever)
5030 this_duration = cpi->source->ts_end - cpi->source->ts_start;
5037 this_duration = cpi->source->ts_end - cpi->last_end_time_stamp_seen;
5038 last_duration = cpi->last_end_time_stamp_seen
5039 - cpi->last_time_stamp_seen;
5049 cpi->ref_framerate = 10000000.0 / this_duration;
5058 interval = (double)(cpi->source->ts_end -
5059 cpi->first_time_stamp_ever);
5063 avg_duration = 10000000.0 / cpi->ref_framerate;
5067 cpi->ref_framerate = 10000000.0 / avg_duration;
5070 if (cpi->oxcf.number_of_layers > 1)
5075 assert(cpi->oxcf.number_of_layers <= VPX_TS_MAX_LAYERS);
5076 for (i=0; i<cpi->oxcf.number_of_layers; i++)
5078 LAYER_CONTEXT *lc = &cpi->layer_context[i];
5079 lc->framerate = cpi->ref_framerate /
5080 cpi->oxcf.rate_decimator[i];
5084 vp8_new_framerate(cpi, cpi->ref_framerate);
5087 cpi->last_time_stamp_seen = cpi->source->ts_start;
5088 cpi->last_end_time_stamp_seen = cpi->source->ts_end;
5091 if (cpi->oxcf.number_of_layers > 1)
5095 update_layer_contexts (cpi);
5098 layer = cpi->oxcf.layer_id[
5099 cpi->temporal_pattern_counter % cpi->oxcf.periodicity];
5100 restore_layer_context (cpi, layer);
5101 vp8_new_framerate(cpi, cpi->layer_context[layer].framerate);
5104 if (cpi->compressor_speed == 2)
5110 cpi->lf_zeromv_pct = (cpi->zeromv_count * 100)/cm->MBs;
5122 cpi->partition_d[0] = dp;
5124 cpi->partition_d_end[0] = dp;
5128 cpi->partition_d[i + 1] = dp;
5130 cpi->partition_d_end[i + 1] = dp;
5174 if (cpi->pass == 1)
5176 Pass1Encode(cpi, size, dest, frame_flags);
5178 else if (cpi->pass == 2)
5180 Pass2Encode(cpi, size, dest, dest_end, frame_flags);
5184 encode_frame_to_data_rate(cpi, size, dest, dest_end, frame_flags);
5186 if (cpi->compressor_speed == 2)
5197 if (cpi->avg_encode_time == 0)
5198 cpi->avg_encode_time = duration;
5200 cpi->avg_encode_time = (7 * cpi->avg_encode_time + duration) >> 3;
5207 if (cpi->avg_pick_mode_time == 0)
5208 cpi->avg_pick_mode_time = duration2;
5210 cpi->avg_pick_mode_time = (7 * cpi->avg_pick_mode_time + duration2) >> 3;
5224 vpx_memcpy(&cpi->lfc_a, &cm->fc, sizeof(cm->fc));
5227 vpx_memcpy(&cpi->lfc_g, &cm->fc, sizeof(cm->fc));
5230 vpx_memcpy(&cpi->lfc_n, &cm->fc, sizeof(cm->fc));
5235 cpi->droppable = !frame_is_reference(cpi);
5247 if (cpi->oxcf.number_of_layers > 1)
5248 save_layer_context (cpi);
5251 cpi->time_compress_data += vpx_usec_timer_elapsed(&cmptimer);
5253 if (cpi->b_calculate_psnr && cpi->pass != 1 && cm->show_frame)
5255 generate_psnr_packet(cpi);
5260 if (cpi->pass != 1)
5262 cpi->bytes += *size;
5266 cpi->common.show_frame_mi = cpi->common.mi;
5267 cpi->count ++;
5269 if (cpi->b_calculate_psnr)
5273 YV12_BUFFER_CONFIG *orig = cpi->Source;
5274 YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
5293 cpi->total_y += vpx_sse_to_psnr(y_samples, 255.0, (double)ye);
5294 cpi->total_u += vpx_sse_to_psnr(uv_samples, 255.0, (double)ue);
5295 cpi->total_v += vpx_sse_to_psnr(uv_samples, 255.0, (double)ve);
5296 cpi->total_sq_error += sq_error;
5297 cpi->total += frame_psnr;
5320 cpi->totalp_y += vpx_sse_to_psnr(y_samples,
5322 cpi->totalp_u += vpx_sse_to_psnr(uv_samples,
5324 cpi->totalp_v += vpx_sse_to_psnr(uv_samples,
5326 cpi->total_sq_error2 += sq_error2;
5327 cpi->totalp += frame_psnr2;
5329 frame_ssim2 = vp8_calc_ssim(cpi->Source,
5332 cpi->summed_quality += frame_ssim2 * weight;
5333 cpi->summed_weights += weight;
5335 if (cpi->oxcf.number_of_layers > 1)
5339 for (i=cpi->current_layer;
5340 i<cpi->oxcf.number_of_layers; i++)
5342 cpi->frames_in_layer[i]++;
5344 cpi->bytes_in_layer[i] += *size;
5345 cpi->sum_psnr[i] += frame_psnr;
5346 cpi->sum_psnr_p[i] += frame_psnr2;
5347 cpi->total_error2[i] += sq_error;
5348 cpi->total_error2_p[i] += sq_error2;
5349 cpi->sum_ssim[i] += frame_ssim2 * weight;
5350 cpi->sum_weights[i] += weight;
5357 if (cpi->b_calculate_ssimg)
5360 frame_all = vp8_calc_ssimg(cpi->Source, cm->frame_to_show,
5363 if (cpi->oxcf.number_of_layers > 1)
5367 for (i=cpi->current_layer;
5368 i<cpi->oxcf.number_of_layers; i++)
5370 if (!cpi->b_calculate_psnr)
5371 cpi->frames_in_layer[i]++;
5373 cpi->total_ssimg_y_in_layer[i] += y;
5374 cpi->total_ssimg_u_in_layer[i] += u;
5375 cpi->total_ssimg_v_in_layer[i] += v;
5376 cpi->total_ssimg_all_in_layer[i] += frame_all;
5381 cpi->total_ssimg_y += y;
5382 cpi->total_ssimg_u += u;
5383 cpi->total_ssimg_v += v;
5384 cpi->total_ssimg_all += frame_all;
5393 if (cpi->common.frame_type != 0 && cpi->common.base_qindex == cpi->oxcf.worst_allowed_q)
5395 skiptruecount += cpi->skip_true_count;
5396 skipfalsecount += cpi->skip_false_count;
5402 if (cpi->pass != 1)
5405 fprintf(f, "frame:%4d flags:%4x Q:%4d P:%4d Size:%5d\n", cpi->common.current_video_frame, *frame_flags, cpi->common.base_qindex, cpi->prob_skip_false, *size);
5407 if (cpi->is_src_frame_alt_ref == 1)
5408 fprintf(f, "skipcount: %4d framesize: %d\n", cpi->skip_true_count , *size);
5425 cpi->common.error.setjmp = 0;
5430 int vp8_get_preview_raw_frame(VP8_COMP *cpi, YV12_BUFFER_CONFIG *dest, vp8_ppflags_t *flags)
5432 if (cpi->common.refresh_alt_ref_frame)
5439 if(cpi->b_lpf_running)
5441 sem_wait(&cpi->h_event_end_lpf);
5442 cpi->b_lpf_running = 0;
5447 cpi->common.show_frame_mi = cpi->common.mi;
5448 ret = vp8_post_proc_frame(&cpi->common, dest, flags);
5451 if (cpi->common.frame_to_show)
5453 *dest = *cpi->common.frame_to_show;
5454 dest->y_width = cpi->common.Width;
5455 dest->y_height = cpi->common.Height;
5456 dest->uv_height = cpi->common.Height / 2;
5470 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])
5478 if ( cpi->cyclic_refresh_mode_enabled )
5482 if (cpi->common.mb_rows != rows || cpi->common.mb_cols != cols)
5498 disable_segmentation(cpi);
5508 set_segmentation_map(cpi, map);
5511 enable_segmentation(cpi);
5525 cpi->segment_encode_breakout[0] = threshold[0];
5526 cpi->segment_encode_breakout[1] = threshold[1];
5527 cpi->segment_encode_breakout[2] = threshold[2];
5528 cpi->segment_encode_breakout[3] = threshold[3];
5531 set_segment_data(cpi, &feature_data[0][0], SEGMENT_DELTADATA);
5536 int vp8_set_active_map(VP8_COMP *cpi, unsigned char *map, unsigned int rows, unsigned int cols)
5538 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols)
5542 vpx_memcpy(cpi->active_map, map, rows * cols);
5543 cpi->active_map_enabled = 1;
5546 cpi->active_map_enabled = 0;
5556 int vp8_set_internal_size(VP8_COMP *cpi, VPX_SCALING horiz_mode, VPX_SCALING vert_mode)
5559 cpi->common.horiz_scale = horiz_mode;
5564 cpi->common.vert_scale = vert_mode;
5600 int vp8_get_quantizer(VP8_COMP *cpi)
5602 return cpi->common.base_qindex;