Lines Matching defs:cpi
43 #define RTCD(x) &cpi->common.rtcd.x
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);
57 extern void vp8_dmachine_specific_config(VP8_COMP *cpi);
58 extern void vp8_cmachine_specific_config(VP8_COMP *cpi);
59 extern void vp8_calc_auto_iframe_target_size(VP8_COMP *cpi);
64 extern void vp8cx_create_encoder_threads(VP8_COMP *cpi);
65 extern void vp8cx_remove_encoder_threads(VP8_COMP *cpi);
71 int vp8_estimate_entropy_savings(VP8_COMP *cpi);
74 extern void vp8_temporal_filter_prepare_c(VP8_COMP *cpi);
76 static void set_default_lf_deltas(VP8_COMP *cpi);
152 extern void vp8cx_init_quantizer(VP8_COMP *cpi);
242 static void setup_features(VP8_COMP *cpi)
245 cpi->mb.e_mbd.segmentation_enabled = 0;
246 cpi->mb.e_mbd.update_mb_segmentation_map = 0;
247 cpi->mb.e_mbd.update_mb_segmentation_data = 0;
248 vpx_memset(cpi->mb.e_mbd.mb_segment_tree_probs, 255, sizeof(cpi->mb.e_mbd.mb_segment_tree_probs));
249 vpx_memset(cpi->mb.e_mbd.segment_feature_data, 0, sizeof(cpi->mb.e_mbd.segment_feature_data));
251 cpi->mb.e_mbd.mode_ref_lf_delta_enabled = 0;
252 cpi->mb.e_mbd.mode_ref_lf_delta_update = 0;
253 vpx_memset(cpi->mb.e_mbd.ref_lf_deltas, 0, sizeof(cpi->mb.e_mbd.ref_lf_deltas));
254 vpx_memset(cpi->mb.e_mbd.mode_lf_deltas, 0, sizeof(cpi->mb.e_mbd.mode_lf_deltas));
255 vpx_memset(cpi->mb.e_mbd.last_ref_lf_deltas, 0, sizeof(cpi->mb.e_mbd.ref_lf_deltas));
256 vpx_memset(cpi->mb.e_mbd.last_mode_lf_deltas, 0, sizeof(cpi->mb.e_mbd.mode_lf_deltas));
258 set_default_lf_deltas(cpi);
263 static void dealloc_compressor_data(VP8_COMP *cpi)
265 vpx_free(cpi->tplist);
266 cpi->tplist = NULL;
269 vpx_free(cpi->lfmv);
270 cpi->lfmv = 0;
272 vpx_free(cpi->lf_ref_frame_sign_bias);
273 cpi->lf_ref_frame_sign_bias = 0;
275 vpx_free(cpi->lf_ref_frame);
276 cpi->lf_ref_frame = 0;
279 vpx_free(cpi->segmentation_map);
280 cpi->segmentation_map = 0;
282 vpx_free(cpi->active_map);
283 cpi->active_map = 0;
285 vp8_de_alloc_frame_buffers(&cpi->common);
287 vp8_yv12_de_alloc_frame_buffer(&cpi->last_frame_uf);
288 vp8_yv12_de_alloc_frame_buffer(&cpi->scaled_source);
290 vp8_yv12_de_alloc_frame_buffer(&cpi->alt_ref_buffer.source_buffer);
296 vp8_yv12_de_alloc_frame_buffer(&cpi->src_buffer[i].source_buffer);
298 cpi->source_buffer_count = 0;
301 vpx_free(cpi->tok);
302 cpi->tok = 0;
305 vpx_free(cpi->gf_active_flags);
306 cpi->gf_active_flags = 0;
308 vpx_free(cpi->mb.pip);
309 cpi->mb.pip = 0;
312 vpx_free(cpi->total_stats);
313 cpi->total_stats = 0;
315 vpx_free(cpi->this_frame_stats);
316 cpi->this_frame_stats = 0;
322 VP8_COMP *cpi = (VP8_COMP *)(ptr);
325 cpi->mb.e_mbd.segmentation_enabled = 1;
326 cpi->mb.e_mbd.update_mb_segmentation_map = 1;
327 cpi->mb.e_mbd.update_mb_segmentation_data = 1;
331 VP8_COMP *cpi = (VP8_COMP *)(ptr);
334 cpi->mb.e_mbd.segmentation_enabled = 0;
341 VP8_COMP *cpi = (VP8_COMP *)(ptr);
344 vpx_memcpy(cpi->segmentation_map, segmentation_map, (cpi->common.mb_rows * cpi->common.mb_cols));
347 cpi->mb.e_mbd.update_mb_segmentation_map = 1;
348 cpi->mb.e_mbd.update_mb_segmentation_data = 1;
361 VP8_COMP *cpi = (VP8_COMP *)(ptr);
363 cpi->mb.e_mbd.mb_segement_abs_delta = abs_delta;
364 vpx_memcpy(cpi->segment_feature_data, feature_data, sizeof(cpi->segment_feature_data));
370 VP8_COMP *cpi = (VP8_COMP *)(ptr);
376 CHECK_MEM_ERROR(seg_map, vpx_calloc(cpi->common.mb_rows * cpi->common.mb_cols, 1));
379 /*for ( i = 0; i < cpi->common.mb_rows; i++ )
381 for ( j = 0; j < cpi->common.mb_cols; j++ )
383 //seg_map[(i*cpi->common.mb_cols) + j] = (j % 2) + ((i%2)* 2);
384 //if ( j < cpi->common.mb_cols/2 )
387 if ( (i == 0) || (j == 0) || (i == (cpi->common.mb_rows-1)) || (j == (cpi->common.mb_cols-1)) )
388 seg_map[(i*cpi->common.mb_cols) + j] = 1;
389 //else if ( (i < 2) || (j < 2) || (i > (cpi->common.mb_rows-3)) || (j > (cpi->common.mb_cols-3)) )
390 // seg_map[(i*cpi->common.mb_cols) + j] = 2;
391 //else if ( (i < 5) || (j < 5) || (i > (cpi->common.mb_rows-6)) || (j > (cpi->common.mb_cols-6)) )
392 // seg_map[(i*cpi->common.mb_cols) + j] = 3;
394 seg_map[(i*cpi->common.mb_cols) + j] = 0;
427 static void cyclic_background_refresh(VP8_COMP *cpi, int Q, int lf_adjustment)
432 int block_count = cpi->cyclic_refresh_mode_max_mbs_perframe;
433 int mbs_in_frame = cpi->common.mb_rows * cpi->common.mb_cols;
436 CHECK_MEM_ERROR(seg_map, vpx_calloc(cpi->common.mb_rows * cpi->common.mb_cols, 1));
438 cpi->cyclic_refresh_q = Q;
442 if (vp8_bits_per_mb[cpi->common.frame_type][i] >= ((vp8_bits_per_mb[cpi->common.frame_type][Q]*(Q + 128)) / 64))
443 //if ( vp8_bits_per_mb[cpi->common.frame_type][i] >= ((vp8_bits_per_mb[cpi->common.frame_type][Q]*((2*Q)+96))/64) )
449 cpi->cyclic_refresh_q = i;
452 if (cpi->common.frame_type != KEY_FRAME)
456 for (i = cpi->cyclic_refresh_mode_index; i < mbs_in_frame; i++)
461 if (cpi->cyclic_refresh_map[i] == 0)
470 if (cpi->cyclic_refresh_map[i] < 0)
471 //cpi->cyclic_refresh_map[i] = cpi->cyclic_refresh_map[i] / 16;
472 cpi->cyclic_refresh_map[i]++;
484 cpi->cyclic_refresh_mode_index = i;
486 if (cpi->cyclic_refresh_mode_index >= mbs_in_frame)
487 cpi->cyclic_refresh_mode_index = 0;
491 set_segmentation_map((VP8_PTR)cpi, seg_map);
494 enable_segmentation((VP8_PTR)cpi);
498 feature_data[MB_LVL_ALT_Q][1] = (cpi->cyclic_refresh_q - Q);
510 set_segment_data((VP8_PTR)cpi, &feature_data[0][0], SEGMENT_DELTADATA);
519 static void set_default_lf_deltas(VP8_COMP *cpi)
521 cpi->mb.e_mbd.mode_ref_lf_delta_enabled = 1;
522 cpi->mb.e_mbd.mode_ref_lf_delta_update = 1;
524 vpx_memset(cpi->mb.e_mbd.ref_lf_deltas, 0, sizeof(cpi->mb.e_mbd.ref_lf_deltas));
525 vpx_memset(cpi->mb.e_mbd.mode_lf_deltas, 0, sizeof(cpi->mb.e_mbd.mode_lf_deltas));
528 cpi->mb.e_mbd.ref_lf_deltas[INTRA_FRAME] = 2;
529 cpi->mb.e_mbd.ref_lf_deltas[LAST_FRAME] = 0;
530 cpi->mb.e_mbd.ref_lf_deltas[GOLDEN_FRAME] = -2;
531 cpi->mb.e_mbd.ref_lf_deltas[ALTREF_FRAME] = -2;
533 cpi->mb.e_mbd.mode_lf_deltas[0] = 4; // BPRED
534 cpi->mb.e_mbd.mode_lf_deltas[1] = -2; // Zero
535 cpi->mb.e_mbd.mode_lf_deltas[2] = 2; // New mv
536 cpi->mb.e_mbd.mode_lf_deltas[3] = 4; // Split mv
539 void vp8_set_speed_features(VP8_COMP *cpi)
541 SPEED_FEATURES *sf = &cpi->sf;
542 int Mode = cpi->compressor_speed;
543 int Speed = cpi->Speed;
545 VP8_COMMON *cm = &cpi->common;
551 cpi->mode_check_freq[i] = 0;
552 cpi->mode_test_hit_counts[i] = 0;
553 cpi->mode_chosen_counts[i] = 0;
556 cpi->mbs_tested_so_far = 0;
580 cpi->do_full[0] = 0;
581 cpi->do_full[1] = 0;
687 cpi->mode_check_freq[THR_SPLITG] = 2;
688 cpi->mode_check_freq[THR_SPLITA] = 2;
689 cpi->mode_check_freq[THR_SPLITMV] = 0;
694 cpi->mode_check_freq[THR_SPLITG] = 4;
695 cpi->mode_check_freq[THR_SPLITA] = 4;
696 cpi->mode_check_freq[THR_SPLITMV] = 2;
703 if (cpi->ref_frame_flags & VP8_LAST_FLAG)
709 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
718 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
730 cpi->mode_check_freq[THR_SPLITG] = 15;
731 cpi->mode_check_freq[THR_SPLITA] = 15;
732 cpi->mode_check_freq[THR_SPLITMV] = 7;
739 if (cpi->ref_frame_flags & VP8_LAST_FLAG)
745 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
754 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
780 cpi->mode_check_freq[THR_V_PRED] = 0;
781 cpi->mode_check_freq[THR_H_PRED] = 0;
782 cpi->mode_check_freq[THR_B_PRED] = 0;
783 cpi->mode_check_freq[THR_NEARG] = 0;
784 cpi->mode_check_freq[THR_NEWG] = 0;
785 cpi->mode_check_freq[THR_NEARA] = 0;
786 cpi->mode_check_freq[THR_NEWA] = 0;
802 cpi->mode_check_freq[THR_V_PRED] = 2;
803 cpi->mode_check_freq[THR_H_PRED] = 2;
804 cpi->mode_check_freq[THR_B_PRED] = 2;
806 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
808 cpi->mode_check_freq[THR_NEARG] = 2;
809 cpi->mode_check_freq[THR_NEWG] = 4;
812 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
814 cpi->mode_check_freq[THR_NEARA] = 2;
815 cpi->mode_check_freq[THR_NEWA] = 4;
818 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
826 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
868 cpi->mode_check_freq[THR_SPLITG] = 4;
869 cpi->mode_check_freq[THR_SPLITA] = 4;
870 cpi->mode_check_freq[THR_SPLITMV] = 2;
878 if (cpi->ref_frame_flags & VP8_LAST_FLAG)
887 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
896 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
911 cpi->mode_check_freq[THR_SPLITMV] = 7;
912 cpi->mode_check_freq[THR_SPLITG] = 15;
913 cpi->mode_check_freq[THR_SPLITA] = 15;
920 if (cpi->ref_frame_flags & VP8_LAST_FLAG)
926 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
935 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
952 cpi->mode_check_freq[THR_V_PRED] = 2;
953 cpi->mode_check_freq[THR_H_PRED] = 2;
954 cpi->mode_check_freq[THR_B_PRED] = 2;
956 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
958 cpi->mode_check_freq[THR_NEARG] = 2;
959 cpi->mode_check_freq[THR_NEWG] = 4;
962 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
964 cpi->mode_check_freq[THR_NEARA] = 2;
965 cpi->mode_check_freq[THR_NEWA] = 4;
996 cpi->mode_check_freq[THR_V_PRED] = 4;
997 cpi->mode_check_freq[THR_H_PRED] = 4;
998 cpi->mode_check_freq[THR_B_PRED] = 4;
1000 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
1002 cpi->mode_check_freq[THR_NEARG] = 2;
1003 cpi->mode_check_freq[THR_NEWG] = 4;
1006 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
1008 cpi->mode_check_freq[THR_NEARA] = 2;
1009 cpi->mode_check_freq[THR_NEWA] = 4;
1015 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
1023 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
1047 if (cpi->oxcf.encode_breakout > 2000)
1048 min = cpi->oxcf.encode_breakout;
1054 sum += cpi->error_bins[i];
1063 sum += cpi->error_bins[i];
1065 if (10 * sum >= (unsigned int)(cpi->Speed - 6)*(total_mbs - total_skip))
1075 if (cpi->ref_frame_flags & VP8_LAST_FLAG)
1082 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
1089 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
1111 int Tmp = cpi->Speed - 8;
1116 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
1118 cpi->mode_check_freq[THR_ZEROG] = 1 << (Tmp - 1);
1119 cpi->mode_check_freq[THR_NEARESTG] = 1 << (Tmp - 1);
1120 cpi->mode_check_freq[THR_NEARG] = 1 << Tmp;
1121 cpi->mode_check_freq[THR_NEWG] = 1 << (Tmp + 1);
1124 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
1126 cpi->mode_check_freq[THR_ZEROA] = 1 << (Tmp - 1);
1127 cpi->mode_check_freq[THR_NEARESTA] = 1 << (Tmp - 1);
1128 cpi->mode_check_freq[THR_NEARA] = 1 << Tmp;
1129 cpi->mode_check_freq[THR_NEWA] = 1 << (Tmp + 1);
1132 cpi->mode_check_freq[THR_NEWMV] = 1 << (Tmp - 1);
1145 vpx_memset(cpi->error_bins, 0, sizeof(cpi->error_bins));
1150 if (!(cpi->ref_frame_flags & VP8_LAST_FLAG))
1159 if (!(cpi->ref_frame_flags & VP8_GOLD_FLAG))
1168 if (!(cpi->ref_frame_flags & VP8_ALT_FLAG))
1180 if ( cpi->pass == 1 )
1187 if (cpi->sf.search_method == NSTEP)
1189 vp8_init3smotion_compensation(&cpi->mb, cm->yv12_fb[cm->lst_fb_idx].y_stride);
1191 else if (cpi->sf.search_method == DIAMOND)
1193 vp8_init_dsmotion_compensation(&cpi->mb, cm->yv12_fb[cm->lst_fb_idx].y_stride);
1196 if (cpi->sf.improved_dct)
1198 cpi->mb.vp8_short_fdct8x4 = FDCT_INVOKE(&cpi->rtcd.fdct, short8x4);
1199 cpi->mb.vp8_short_fdct4x4 = FDCT_INVOKE(&cpi->rtcd.fdct, short4x4);
1203 cpi->mb.vp8_short_fdct8x4 = FDCT_INVOKE(&cpi->rtcd.fdct, fast8x4);
1204 cpi->mb.vp8_short_fdct4x4 = FDCT_INVOKE(&cpi->rtcd.fdct, fast4x4);
1207 cpi->mb.short_walsh4x4 = FDCT_INVOKE(&cpi->rtcd.fdct, walsh_short4x4);
1209 if (cpi->sf.improved_quant)
1211 cpi->mb.quantize_b = QUANTIZE_INVOKE(&cpi->rtcd.quantize, quantb);
1215 cpi->mb.quantize_b = QUANTIZE_INVOKE(&cpi->rtcd.quantize, fastquantb);
1217 if (cpi->sf.improved_quant != last_improved_quant)
1218 vp8cx_init_quantizer(cpi);
1221 cpi->mb.e_mbd.rtcd = &cpi->common.rtcd;
1224 if (cpi->sf.iterative_sub_pixel == 1)
1226 cpi->find_fractional_mv_step = vp8_find_best_sub_pixel_step_iteratively;
1228 else if (cpi->sf.quarter_pixel_search)
1230 cpi->find_fractional_mv_step = vp8_find_best_sub_pixel_step;
1232 else if (cpi->sf.half_pixel_search)
1234 cpi->find_fractional_mv_step = vp8_find_best_half_pixel_step;
1238 cpi->find_fractional_mv_step = vp8_skip_fractional_mv_step;
1241 if (cpi->sf.optimize_coefficients == 1)
1242 cpi->mb.optimize = 1 + cpi->is_next_src_alt_ref;
1244 cpi->mb.optimize = 0;
1246 if (cpi->common.full_pixel)
1247 cpi->find_fractional_mv_step = vp8_skip_fractional_mv_step;
1250 frames_at_speed[cpi->Speed]++;
1253 static void alloc_raw_frame_buffers(VP8_COMP *cpi)
1257 int width = (cpi->oxcf.Width + 15) & ~15;
1259 buffers = cpi->oxcf.lag_in_frames;
1268 if (vp8_yv12_alloc_frame_buffer(&cpi->src_buffer[i].source_buffer,
1269 width, cpi->oxcf.Height,
1271 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1276 if (vp8_yv12_alloc_frame_buffer(&cpi->alt_ref_buffer.source_buffer,
1277 width, cpi->oxcf.Height, 16))
1278 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1283 cpi->source_buffer_count = 0;
1286 static int vp8_alloc_partition_data(VP8_COMP *cpi)
1288 vpx_free(cpi->mb.pip);
1290 cpi->mb.pip = vpx_calloc((cpi->common.mb_cols + 1) *
1291 (cpi->common.mb_rows + 1),
1293 if(!cpi->mb.pip)
1296 cpi->mb.pi = cpi->mb.pip + cpi->common.mode_info_stride + 1;
1301 void vp8_alloc_compressor_data(VP8_COMP *cpi)
1303 VP8_COMMON *cm = & cpi->common;
1309 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1312 if (vp8_alloc_partition_data(cpi))
1313 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1324 if (vp8_yv12_alloc_frame_buffer(&cpi->last_frame_uf,
1326 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1329 if (vp8_yv12_alloc_frame_buffer(&cpi->scaled_source, width, height, 16))
1330 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1334 vpx_free(cpi->tok);
1339 CHECK_MEM_ERROR(cpi->tok, vpx_calloc(tokens, sizeof(*cpi->tok)));
1343 cpi->inter_zz_count = 0;
1344 cpi->gf_bad_count = 0;
1345 cpi->gf_update_recommended = 0;
1349 vpx_free(cpi->gf_active_flags);
1351 CHECK_MEM_ERROR(cpi->gf_active_flags, vpx_calloc(1, cm->mb_rows * cm->mb_cols));
1353 cpi->gf_active_count = cm->mb_rows * cm->mb_cols;
1356 vpx_free(cpi->total_stats);
1358 cpi->total_stats = vpx_calloc(1, sizeof(FIRSTPASS_STATS));
1360 vpx_free(cpi->this_frame_stats);
1362 cpi->this_frame_stats = vpx_calloc(1, sizeof(FIRSTPASS_STATS));
1364 if(!cpi->total_stats || !cpi->this_frame_stats)
1365 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1371 cpi->mt_sync_range = 1;
1373 cpi->mt_sync_range = 4;
1375 cpi->mt_sync_range = 8;
1377 cpi->mt_sync_range = 16;
1380 vpx_free(cpi->tplist);
1382 CHECK_MEM_ERROR(cpi->tplist, vpx_malloc(sizeof(TOKENLIST) * cpi->common.mb_rows));
1409 void vp8_new_frame_rate(VP8_COMP *cpi, double framerate)
1414 cpi->oxcf.frame_rate = framerate;
1415 cpi->output_frame_rate = cpi->oxcf.frame_rate;
1416 cpi->per_frame_bandwidth = (int)(cpi->oxcf.target_bandwidth / cpi->output_frame_rate);
1417 cpi->av_per_frame_bandwidth = (int)(cpi->oxcf.target_bandwidth / cpi->output_frame_rate);
1418 cpi->min_frame_bandwidth = (int)(cpi->av_per_frame_bandwidth * cpi->oxcf.two_pass_vbrmin_section / 100);
1421 cpi->max_gf_interval = ((int)(cpi->output_frame_rate / 2.0) + 2);
1423 if(cpi->max_gf_interval < 12)
1424 cpi->max_gf_interval = 12;
1427 cpi->static_scene_max_gf_interval = cpi->key_frame_frequency >> 1;
1430 if (cpi->oxcf.play_alternate && cpi->oxcf.lag_in_frames)
1432 if (cpi->max_gf_interval > cpi->oxcf.lag_in_frames - 1)
1433 cpi->max_gf_interval = cpi->oxcf.lag_in_frames - 1;
1435 if (cpi->static_scene_max_gf_interval > cpi->oxcf.lag_in_frames - 1)
1436 cpi->static_scene_max_gf_interval = cpi->oxcf.lag_in_frames - 1;
1439 if ( cpi->max_gf_interval > cpi->static_scene_max_gf_interval )
1440 cpi->max_gf_interval = cpi->static_scene_max_gf_interval;
1457 VP8_COMP *cpi = (VP8_COMP *)(ptr);
1458 VP8_COMMON *cm = &cpi->common;
1460 cpi->oxcf = *oxcf;
1462 cpi->auto_gold = 1;
1463 cpi->auto_adjust_gold_quantizer = 1;
1464 cpi->goldquantizer = 1;
1465 cpi->goldfreq = 7;
1466 cpi->auto_adjust_key_quantizer = 1;
1467 cpi->keyquantizer = 1;
1476 cpi->active_worst_quality = cpi->oxcf.worst_allowed_q;
1477 cpi->active_best_quality = cpi->oxcf.best_allowed_q;
1478 cpi->avg_frame_qindex = cpi->oxcf.worst_allowed_q;
1481 cpi->oxcf.starting_buffer_level =
1482 rescale(cpi->oxcf.starting_buffer_level,
1483 cpi->oxcf.target_bandwidth, 1000);
1485 cpi->buffer_level = cpi->oxcf.starting_buffer_level;
1486 cpi->bits_off_target = cpi->oxcf.starting_buffer_level;
1488 cpi->rolling_target_bits = cpi->av_per_frame_bandwidth;
1489 cpi->rolling_actual_bits = cpi->av_per_frame_bandwidth;
1490 cpi->long_rolling_target_bits = cpi->av_per_frame_bandwidth;
1491 cpi->long_rolling_actual_bits = cpi->av_per_frame_bandwidth;
1493 cpi->total_actual_bits = 0;
1494 cpi->total_target_vs_actual = 0;
1500 cpi->fixed_divide[0] = 0;
1503 cpi->fixed_divide[i] = 0x80000 / i;
1511 VP8_COMP *cpi = (VP8_COMP *)(ptr);
1512 VP8_COMMON *cm = &cpi->common;
1514 if (!cpi)
1526 cpi->oxcf = *oxcf;
1528 switch (cpi->oxcf.Mode)
1532 cpi->pass = 0;
1533 cpi->compressor_speed = 2;
1535 if (cpi->oxcf.cpu_used < -16)
1537 cpi->oxcf.cpu_used = -16;
1540 if (cpi->oxcf.cpu_used > 16)
1541 cpi->oxcf.cpu_used = 16;
1547 cpi->pass = 0;
1548 cpi->compressor_speed = 1;
1550 if (cpi->oxcf.cpu_used < -5)
1552 cpi->oxcf.cpu_used = -5;
1555 if (cpi->oxcf.cpu_used > 5)
1556 cpi->oxcf.cpu_used = 5;
1561 cpi->pass = 0;
1562 cpi->compressor_speed = 0;
1566 cpi->pass = 1;
1567 cpi->compressor_speed = 1;
1570 cpi->pass = 2;
1571 cpi->compressor_speed = 1;
1573 if (cpi->oxcf.cpu_used < -5)
1575 cpi->oxcf.cpu_used = -5;
1578 if (cpi->oxcf.cpu_used > 5)
1579 cpi->oxcf.cpu_used = 5;
1583 cpi->pass = 2;
1584 cpi->compressor_speed = 0;
1589 if (cpi->pass == 0)
1590 cpi->auto_worst_q = 1;
1592 cpi->oxcf.worst_allowed_q = q_trans[oxcf->worst_allowed_q];
1593 cpi->oxcf.best_allowed_q = q_trans[oxcf->best_allowed_q];
1594 cpi->oxcf.cq_level = q_trans[cpi->oxcf.cq_level];
1599 cpi->oxcf.fixed_q = q_trans[0];
1601 cpi->oxcf.fixed_q = q_trans[oxcf->worst_allowed_q];
1604 cpi->oxcf.alt_q = q_trans[0];
1606 cpi->oxcf.alt_q = q_trans[oxcf->alt_q];
1609 cpi->oxcf.key_q = q_trans[0];
1611 cpi->oxcf.key_q = q_trans[oxcf->key_q];
1614 cpi->oxcf.gold_q = q_trans[0];
1616 cpi->oxcf.gold_q = q_trans[oxcf->gold_q];
1620 cpi->baseline_gf_interval =
1621 cpi->oxcf.alt_freq ? cpi->oxcf.alt_freq : DEFAULT_GF_INTERVAL;
1623 cpi->ref_frame_flags = VP8_ALT_FLAG | VP8_GOLD_FLAG | VP8_LAST_FLAG;
1625 //cpi->use_golden_frame_only = 0;
1626 //cpi->use_last_frame_only = 0;
1631 if (cpi->oxcf.token_partitions >= 0 && cpi->oxcf.token_partitions <= 3)
1633 (TOKEN_PARTITION) cpi->oxcf.token_partitions;
1635 setup_features(cpi);
1641 cpi->segment_encode_breakout[i] = cpi->oxcf.encode_breakout;
1645 if (cpi->oxcf.fixed_q > MAXQ)
1646 cpi->oxcf.fixed_q = MAXQ;
1649 if (cpi->oxcf.end_usage == USAGE_LOCAL_FILE_PLAYBACK)
1651 cpi->oxcf.starting_buffer_level = 60000;
1652 cpi->oxcf.optimal_buffer_level = 60000;
1653 cpi->oxcf.maximum_buffer_size = 240000;
1657 cpi->oxcf.target_bandwidth *= 1000;
1660 if (cpi->oxcf.optimal_buffer_level == 0)
1661 cpi->oxcf.optimal_buffer_level = cpi->oxcf.target_bandwidth / 8;
1663 cpi->oxcf.optimal_buffer_level =
1664 rescale(cpi->oxcf.optimal_buffer_level,
1665 cpi->oxcf.target_bandwidth, 1000);
1667 if (cpi->oxcf.maximum_buffer_size == 0)
1668 cpi->oxcf.maximum_buffer_size = cpi->oxcf.target_bandwidth / 8;
1670 cpi->oxcf.maximum_buffer_size =
1671 rescale(cpi->oxcf.maximum_buffer_size,
1672 cpi->oxcf.target_bandwidth, 1000);
1675 vp8_new_frame_rate(cpi, cpi->oxcf.frame_rate);
1678 cpi->worst_quality = cpi->oxcf.worst_allowed_q;
1679 cpi->best_quality = cpi->oxcf.best_allowed_q;
1682 if (cpi->active_worst_quality > cpi->oxcf.worst_allowed_q)
1684 cpi->active_worst_quality = cpi->oxcf.worst_allowed_q;
1687 else if (cpi->active_worst_quality < cpi->oxcf.best_allowed_q)
1689 cpi->active_worst_quality = cpi->oxcf.best_allowed_q;
1691 if (cpi->active_best_quality < cpi->oxcf.best_allowed_q)
1693 cpi->active_best_quality = cpi->oxcf.best_allowed_q;
1696 else if (cpi->active_best_quality > cpi->oxcf.worst_allowed_q)
1698 cpi->active_best_quality = cpi->oxcf.worst_allowed_q;
1701 cpi->buffered_mode = (cpi->oxcf.optimal_buffer_level > 0) ? TRUE : FALSE;
1703 cpi->cq_target_quality = cpi->oxcf.cq_level;
1706 cpi->drop_frames_allowed = cpi->oxcf.allow_df && cpi->buffered_mode;
1708 cm->filter_type = (LOOPFILTERTYPE) cpi->filter_type;
1715 cpi->target_bandwidth = cpi->oxcf.target_bandwidth;
1717 cm->Width = cpi->oxcf.Width ;
1718 cm->Height = cpi->oxcf.Height ;
1720 cm->horiz_scale = cpi->horiz_scale;
1721 cm->vert_scale = cpi->vert_scale ;
1724 cpi->intra_frame_target = (4 * (cm->Width + cm->Height) / 15) * 1000;
1727 if (cpi->oxcf.Sharpness > 7)
1728 cpi->oxcf.Sharpness = 7;
1730 cm->sharpness_level = cpi->oxcf.Sharpness;
1741 cm->Width = (hs - 1 + cpi->oxcf.Width * hr) / hs;
1742 cm->Height = (vs - 1 + cpi->oxcf.Height * vr) / vs;
1751 alloc_raw_frame_buffers(cpi);
1752 vp8_alloc_compressor_data(cpi);
1756 if (cpi->intra_frame_target > cpi->target_bandwidth >> 2)
1757 cpi->intra_frame_target = cpi->target_bandwidth >> 2;
1759 if (cpi->oxcf.fixed_q >= 0)
1761 cpi->last_q[0] = cpi->oxcf.fixed_q;
1762 cpi->last_q[1] = cpi->oxcf.fixed_q;
1765 cpi->Speed = cpi->oxcf.cpu_used;
1768 if (cpi->oxcf.lag_in_frames == 0)
1770 cpi->oxcf.allow_lag = 0;
1773 else if (cpi->oxcf.lag_in_frames > MAX_LAG_BUFFERS)
1774 cpi->oxcf.lag_in_frames = MAX_LAG_BUFFERS;
1777 cpi->last_alt_ref_sei = -1;
1778 cpi->is_src_frame_alt_ref = 0;
1779 cpi->is_next_src_alt_ref = 0;
1783 cpi->frame_distortion = 0;
1784 cpi->last_frame_distortion = 0;
1814 VP8_COMP *cpi;
1818 VP8_COMP *cpi;
1821 cpi = ctx.cpi = vpx_memalign(32, sizeof(VP8_COMP));
1823 if (!cpi)
1826 cm = &cpi->common;
1828 vpx_memset(cpi, 0, sizeof(VP8_COMP));
1834 ctx.cpi->common.error.setjmp = 0;
1839 cpi->common.error.setjmp = 1;
1841 CHECK_MEM_ERROR(cpi->mb.ss, vpx_calloc(sizeof(search_site), (MAX_MVSEARCH_STEPS * 8) + 1));
1843 vp8_create_common(&cpi->common);
1844 vp8_cmachine_specific_config(cpi);
1846 init_config((VP8_PTR)cpi, oxcf);
1848 memcpy(cpi->base_skip_false_prob, vp8cx_base_skip_false_prob, sizeof(vp8cx_base_skip_false_prob));
1849 cpi->common.current_video_frame = 0;
1850 cpi->kf_overspend_bits = 0;
1851 cpi->kf_bitrate_adjustment = 0;
1852 cpi->frames_till_gf_update_due = 0;
1853 cpi->gf_overspend_bits = 0;
1854 cpi->non_gf_bitrate_adjustment = 0;
1855 cpi->prob_last_coded = 128;
1856 cpi->prob_gf_coded = 128;
1857 cpi->prob_intra_coded = 63;
1861 cpi->recent_ref_frame_usage[INTRA_FRAME] = 1;
1862 cpi->recent_ref_frame_usage[LAST_FRAME] = 1;
1863 cpi->recent_ref_frame_usage[GOLDEN_FRAME] = 1;
1864 cpi->recent_ref_frame_usage[ALTREF_FRAME] = 1;
1867 cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = 1;
1869 cpi->gf_decay_rate = 0;
1870 cpi->baseline_gf_interval = DEFAULT_GF_INTERVAL;
1872 cpi->gold_is_last = 0 ;
1873 cpi->alt_is_last = 0 ;
1874 cpi->gold_is_alt = 0 ;
1877 CHECK_MEM_ERROR(cpi->lfmv, vpx_calloc((cpi->common.mb_rows+2) * (cpi->common.mb_cols+2), sizeof(int_mv)));
1878 CHECK_MEM_ERROR(cpi->lf_ref_frame_sign_bias, vpx_calloc((cpi->common.mb_rows+2) * (cpi->common.mb_cols+2), sizeof(int)));
1879 CHECK_MEM_ERROR(cpi->lf_ref_frame, vpx_calloc((cpi->common.mb_rows+2) * (cpi->common.mb_cols+2), sizeof(int)));
1882 CHECK_MEM_ERROR(cpi->segmentation_map, vpx_calloc(cpi->common.mb_rows * cpi->common.mb_cols, 1));
1883 CHECK_MEM_ERROR(cpi->active_map, vpx_calloc(cpi->common.mb_rows * cpi->common.mb_cols, 1));
1884 vpx_memset(cpi->active_map , 1, (cpi->common.mb_rows * cpi->common.mb_cols));
1885 cpi->active_map_enabled = 0;
1891 if (cpi->pass == 0)
1893 cpi->one_pass_frame_index = 0;
1897 cpi->one_pass_frame_stats[i].frames_so_far = 0;
1898 cpi->one_pass_frame_stats[i].frame_intra_error = 0.0;
1899 cpi->one_pass_frame_stats[i].frame_coded_error = 0.0;
1900 cpi->one_pass_frame_stats[i].frame_pcnt_inter = 0.0;
1901 cpi->one_pass_frame_stats[i].frame_pcnt_motion = 0.0;
1902 cpi->one_pass_frame_stats[i].frame_mvr = 0.0;
1903 cpi->one_pass_frame_stats[i].frame_mvr_abs = 0.0;
1904 cpi->one_pass_frame_stats[i].frame_mvc = 0.0;
1905 cpi->one_pass_frame_stats[i].frame_mvc_abs = 0.0;
1912 cpi->cyclic_refresh_mode_enabled = cpi->oxcf.error_resilient_mode;
1913 cpi->cyclic_refresh_mode_max_mbs_perframe = (cpi->common.mb_rows * cpi->common.mb_cols) / 40;
1914 cpi->cyclic_refresh_mode_index = 0;
1915 cpi->cyclic_refresh_q = 32;
1917 if (cpi->cyclic_refresh_mode_enabled)
1919 CHECK_MEM_ERROR(cpi->cyclic_refresh_map, vpx_calloc((cpi->common.mb_rows * cpi->common.mb_cols), 1));
1922 cpi->cyclic_refresh_map = (signed char *) NULL;
1925 //segmentation_test_function((VP8_PTR) cpi);
1932 cpi->activity_avg = 90<<12;
1934 cpi->frames_since_key = 8; // Give a sensible default for the first frame.
1935 cpi->key_frame_frequency = cpi->oxcf.key_freq;
1936 cpi->this_key_frame_forced = FALSE;
1937 cpi->next_key_frame_forced = FALSE;
1939 cpi->source_alt_ref_pending = FALSE;
1940 cpi->source_alt_ref_active = FALSE;
1941 cpi->common.refresh_alt_ref_frame = 0;
1943 cpi->b_calculate_psnr = CONFIG_PSNR;
1945 cpi->b_calculate_ssimg = 0;
1947 cpi->count = 0;
1948 cpi->bytes = 0;
1950 if (cpi->b_calculate_psnr)
1952 cpi->total_sq_error = 0.0;
1953 cpi->total_sq_error2 = 0.0;
1954 cpi->total_y = 0.0;
1955 cpi->total_u = 0.0;
1956 cpi->total_v = 0.0;
1957 cpi->total = 0.0;
1958 cpi->totalp_y = 0.0;
1959 cpi->totalp_u = 0.0;
1960 cpi->totalp_v = 0.0;
1961 cpi->totalp = 0.0;
1962 cpi->tot_recode_hits = 0;
1963 cpi->summed_quality = 0;
1964 cpi->summed_weights = 0;
1967 if (cpi->b_calculate_ssimg)
1969 cpi->total_ssimg_y = 0;
1970 cpi->total_ssimg_u = 0;
1971 cpi->total_ssimg_v = 0;
1972 cpi->total_ssimg_all = 0;
1978 cpi->first_time_stamp_ever = LLONG_MAX;
1982 cpi->frames_till_gf_update_due = 0;
1983 cpi->key_frame_count = 1;
1984 cpi->tot_key_frame_bits = 0;
1986 cpi->ni_av_qi = cpi->oxcf.worst_allowed_q;
1987 cpi->ni_tot_qi = 0;
1988 cpi->ni_frames = 0;
1989 cpi->total_byte_count = 0;
1991 cpi->drop_frame = 0;
1992 cpi->drop_count = 0;
1993 cpi->max_drop_count = 0;
1994 cpi->max_consec_dropped_frames = 4;
1996 cpi->rate_correction_factor = 1.0;
1997 cpi->key_frame_rate_correction_factor = 1.0;
1998 cpi->gf_rate_correction_factor = 1.0;
1999 cpi->est_max_qcorrection_factor = 1.0;
2001 cpi->mb.mvcost[0] = &cpi->mb.mvcosts[0][mv_max+1];
2002 cpi->mb.mvcost[1] = &cpi->mb.mvcosts[1][mv_max+1];
2003 cpi->mb.mvsadcost[0] = &cpi->mb.mvsadcosts[0][mv_max+1];
2004 cpi->mb.mvsadcost[1] = &cpi->mb.mvsadcosts[1][mv_max+1];
2006 cal_mvsadcosts(cpi->mb.mvsadcost);
2010 cpi->prior_key_frame_size[i] = cpi->intra_frame_target;
2011 cpi->prior_key_frame_distance[i] = (int)cpi->output_frame_rate;
2014 cpi->check_freq[0] = 15;
2015 cpi->check_freq[1] = 15;
2026 cpi->output_pkt_list = oxcf->output_pkt_list;
2030 if (cpi->pass == 1)
2032 vp8_init_first_pass(cpi);
2034 else if (cpi->pass == 2)
2039 cpi->stats_in = oxcf->two_pass_stats_in.buf;
2040 cpi->stats_in_end = (void*)((char *)cpi->stats_in
2042 vp8_init_second_pass(cpi);
2047 if (cpi->compressor_speed == 2)
2049 cpi->cpu_freq = 0; //vp8_get_processor_freq();
2050 cpi->avg_encode_time = 0;
2051 cpi->avg_pick_mode_time = 0;
2054 vp8_set_speed_features(cpi);
2059 cpi->rd_thresh_mult[i] = 128;
2067 vp8cx_create_encoder_threads(cpi);
2070 cpi->fn_ptr[BLOCK_16X16].sdf = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x16);
2071 cpi->fn_ptr[BLOCK_16X16].vf = VARIANCE_INVOKE(&cpi->rtcd.variance, var16x16);
2072 cpi->fn_ptr[BLOCK_16X16].svf = VARIANCE_INVOKE(&cpi->rtcd.variance, subpixvar16x16);
2073 cpi->fn_ptr[BLOCK_16X16].svf_halfpix_h = VARIANCE_INVOKE(&cpi->rtcd.variance, halfpixvar16x16_h);
2074 cpi->fn_ptr[BLOCK_16X16].svf_halfpix_v = VARIANCE_INVOKE(&cpi->rtcd.variance, halfpixvar16x16_v);
2075 cpi->fn_ptr[BLOCK_16X16].svf_halfpix_hv = VARIANCE_INVOKE(&cpi->rtcd.variance, halfpixvar16x16_hv);
2076 cpi->fn_ptr[BLOCK_16X16].sdx3f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x16x3);
2077 cpi->fn_ptr[BLOCK_16X16].sdx8f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x16x8);
2078 cpi->fn_ptr[BLOCK_16X16].sdx4df = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x16x4d);
2080 cpi->fn_ptr[BLOCK_16X8].sdf = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x8);
2081 cpi->fn_ptr[BLOCK_16X8].vf = VARIANCE_INVOKE(&cpi->rtcd.variance, var16x8);
2082 cpi->fn_ptr[BLOCK_16X8].svf = VARIANCE_INVOKE(&cpi->rtcd.variance, subpixvar16x8);
2083 cpi->fn_ptr[BLOCK_16X8].svf_halfpix_h = NULL;
2084 cpi->fn_ptr[BLOCK_16X8].svf_halfpix_v = NULL;
2085 cpi->fn_ptr[BLOCK_16X8].svf_halfpix_hv = NULL;
2086 cpi->fn_ptr[BLOCK_16X8].sdx3f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x8x3);
2087 cpi->fn_ptr[BLOCK_16X8].sdx8f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x8x8);
2088 cpi->fn_ptr[BLOCK_16X8].sdx4df = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x8x4d);
2090 cpi->fn_ptr[BLOCK_8X16].sdf = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x16);
2091 cpi->fn_ptr[BLOCK_8X16].vf = VARIANCE_INVOKE(&cpi->rtcd.variance, var8x16);
2092 cpi->fn_ptr[BLOCK_8X16].svf = VARIANCE_INVOKE(&cpi->rtcd.variance, subpixvar8x16);
2093 cpi->fn_ptr[BLOCK_8X16].svf_halfpix_h = NULL;
2094 cpi->fn_ptr[BLOCK_8X16].svf_halfpix_v = NULL;
2095 cpi->fn_ptr[BLOCK_8X16].svf_halfpix_hv = NULL;
2096 cpi->fn_ptr[BLOCK_8X16].sdx3f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x16x3);
2097 cpi->fn_ptr[BLOCK_8X16].sdx8f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x16x8);
2098 cpi->fn_ptr[BLOCK_8X16].sdx4df = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x16x4d);
2100 cpi->fn_ptr[BLOCK_8X8].sdf = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x8);
2101 cpi->fn_ptr[BLOCK_8X8].vf = VARIANCE_INVOKE(&cpi->rtcd.variance, var8x8);
2102 cpi->fn_ptr[BLOCK_8X8].svf = VARIANCE_INVOKE(&cpi->rtcd.variance, subpixvar8x8);
2103 cpi->fn_ptr[BLOCK_8X8].svf_halfpix_h = NULL;
2104 cpi->fn_ptr[BLOCK_8X8].svf_halfpix_v = NULL;
2105 cpi->fn_ptr[BLOCK_8X8].svf_halfpix_hv = NULL;
2106 cpi->fn_ptr[BLOCK_8X8].sdx3f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x8x3);
2107 cpi->fn_ptr[BLOCK_8X8].sdx8f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x8x8);
2108 cpi->fn_ptr[BLOCK_8X8].sdx4df = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x8x4d);
2110 cpi->fn_ptr[BLOCK_4X4].sdf = VARIANCE_INVOKE(&cpi->rtcd.variance, sad4x4);
2111 cpi->fn_ptr[BLOCK_4X4].vf = VARIANCE_INVOKE(&cpi->rtcd.variance, var4x4);
2112 cpi->fn_ptr[BLOCK_4X4].svf = VARIANCE_INVOKE(&cpi->rtcd.variance, subpixvar4x4);
2113 cpi->fn_ptr[BLOCK_4X4].svf_halfpix_h = NULL;
2114 cpi->fn_ptr[BLOCK_4X4].svf_halfpix_v = NULL;
2115 cpi->fn_ptr[BLOCK_4X4].svf_halfpix_hv = NULL;
2116 cpi->fn_ptr[BLOCK_4X4].sdx3f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad4x4x3);
2117 cpi->fn_ptr[BLOCK_4X4].sdx8f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad4x4x8);
2118 cpi->fn_ptr[BLOCK_4X4].sdx4df = VARIANCE_INVOKE(&cpi->rtcd.variance, sad4x4x4d);
2121 cpi->full_search_sad = SEARCH_INVOKE(&cpi->rtcd.search, full_search);
2123 cpi->diamond_search_sad = SEARCH_INVOKE(&cpi->rtcd.search, diamond_search);
2125 cpi->ready_for_new_frame = 1;
2127 cpi->source_encode_index = 0;
2130 cpi->error_bins[0] = cpi->common.MBs;
2134 vp8cx_init_quantizer(cpi);
2141 cpi->common.error.setjmp = 0;
2142 return (VP8_PTR) cpi;
2149 VP8_COMP *cpi = (VP8_COMP *)(*ptr);
2151 if (!cpi)
2154 if (cpi && (cpi->common.current_video_frame > 0))
2158 if (cpi->pass == 2)
2160 vp8_end_second_pass(cpi);
2173 if (cpi->pass != 1)
2176 double time_encoded = (cpi->source_end_time_stamp - cpi->first_time_stamp_ever) / 10000000.000;
2177 double total_encode_time = (cpi->time_receive_data + cpi->time_compress_data) / 1000.000;
2178 double dr = (double)cpi->bytes * (double) 8 / (double)1000 / time_encoded;
2180 if (cpi->b_calculate_psnr)
2182 YV12_BUFFER_CONFIG *lst_yv12 = &cpi->common.yv12_fb[cpi->common.lst_fb_idx];
2183 double samples = 3.0 / 2 * cpi->count * lst_yv12->y_width * lst_yv12->y_height;
2184 double total_psnr = vp8_mse2psnr(samples, 255.0, cpi->total_sq_error);
2185 double total_psnr2 = vp8_mse2psnr(samples, 255.0, cpi->total_sq_error2);
2186 double total_ssim = 100 * pow(cpi->summed_quality / cpi->summed_weights, 8.0);
2190 dr, cpi->total / cpi->count, total_psnr, cpi->totalp / cpi->count, total_psnr2, total_ssim,
2194 if (cpi->b_calculate_ssimg)
2198 cpi->total_ssimg_y / cpi->count, cpi->total_ssimg_u / cpi->count,
2199 cpi->total_ssimg_v / cpi->count, cpi->total_ssimg_all / cpi->count, total_encode_time);
2205 fprintf(f, "minq:%d -maxq:%d skipture:skipfalse = %d:%d\n", cpi->oxcf.best_allowed_q, cpi->oxcf.worst_allowed_q, skiptruecount, skipfalsecount);
2216 if (cpi->compressor_speed == 2)
2220 cnt_pm /= cpi->common.MBs;
2226 //fprintf(f, "%10d PM %10d %10d %10d EF %10d %10d %10d\n", cpi->Speed, cpi->avg_pick_mode_time, (tot_pm/cnt_pm), cnt_pm, cpi->avg_encode_time, 0, 0);
2237 double dr = (double)cpi->oxcf.frame_rate * (double)bytes * (double)8 / (double)count / (double)1000 ;
2335 printf("\n_pick_loop_filter_level:%d\n", cpi->time_pick_lpf / 1000);
2337 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);
2344 vp8cx_remove_encoder_threads(cpi);
2347 dealloc_compressor_data(cpi);
2348 vpx_free(cpi->mb.ss);
2349 vpx_free(cpi->tok);
2350 vpx_free(cpi->cyclic_refresh_map);
2352 vp8_remove_common(&cpi->common);
2353 vpx_free(cpi);
2438 static void generate_psnr_packet(VP8_COMP *cpi)
2440 YV12_BUFFER_CONFIG *orig = cpi->Source;
2441 YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
2445 unsigned int width = cpi->common.Width;
2446 unsigned int height = cpi->common.Height;
2452 IF_RTCD(&cpi->rtcd.variance));
2464 IF_RTCD(&cpi->rtcd.variance));
2473 IF_RTCD(&cpi->rtcd.variance));
2483 vpx_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
2489 VP8_COMP *cpi = (VP8_COMP *)(ptr);
2494 cpi->ref_frame_flags = ref_frame_flags;
2499 VP8_COMP *cpi = (VP8_COMP *)(ptr);
2504 cpi->common.refresh_golden_frame = 0;
2505 cpi->common.refresh_alt_ref_frame = 0;
2506 cpi->common.refresh_last_frame = 0;
2509 cpi->common.refresh_last_frame = 1;
2512 cpi->common.refresh_golden_frame = 1;
2515 cpi->common.refresh_alt_ref_frame = 1;
2522 VP8_COMP *cpi = (VP8_COMP *)(ptr);
2523 VP8_COMMON *cm = &cpi->common;
2541 VP8_COMP *cpi = (VP8_COMP *)(ptr);
2542 VP8_COMMON *cm = &cpi->common;
2561 VP8_COMP *cpi = (VP8_COMP *) comp;
2562 VP8_COMMON *cm = &cpi->common;
2608 static void scale_and_extend_source(YV12_BUFFER_CONFIG *sd, VP8_COMP *cpi)
2610 VP8_COMMON *cm = &cpi->common;
2628 vp8_scale_frame(sd, &cpi->scaled_source, cm->temp_scale_frame.y_buffer,
2631 cpi->Source = &cpi->scaled_source;
2638 //vp8_yv12_copy_frame_ptr(sd, &cpi->scaled_source);
2644 vp8_yv12_copy_src_frame_func_neon(sd, &cpi->scaled_source);
2652 vp8_yv12_copy_frame_ptr(sd, &cpi->scaled_source);
2656 cpi->Source = &cpi->scaled_source;
2659 vp8_extend_to_multiple_of16(cpi->Source, cm->Width, cm->Height);
2662 static void resize_key_frame(VP8_COMP *cpi)
2665 VP8_COMMON *cm = &cpi->common;
2670 if (cpi->oxcf.allow_spatial_resampling && (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER))
2677 if (cpi->buffer_level < (cpi->oxcf.resample_down_water_mark * cpi->oxcf.optimal_buffer_level / 100))
2683 else if (cpi->buffer_level > (cpi->oxcf.resample_up_water_mark * cpi->oxcf.optimal_buffer_level / 100))
2692 new_width = ((hs - 1) + (cpi->oxcf.Width * hr)) / hs;
2693 new_height = ((vs - 1) + (cpi->oxcf.Height * vr)) / vs;
2701 vp8_alloc_compressor_data(cpi);
2702 scale_and_extend_source(cpi->un_scaled_source, cpi);
2709 static int pick_frame_size(VP8_COMP *cpi)
2711 VP8_COMMON *cm = &cpi->common;
2718 if (cpi->pass == 2)
2719 vp8_calc_auto_iframe_target_size(cpi);
2724 cpi->this_frame_target = cpi->oxcf.target_bandwidth / 2;
2728 if (cpi->oxcf.error_resilient_mode == 1 || (cpi->compressor_speed == 2))
2730 cpi->this_frame_target *= 3; // 5;
2742 resize_key_frame(cpi);
2743 vp8_calc_iframe_target_size(cpi);
2747 vp8_calc_auto_iframe_target_size(cpi);
2753 vp8_calc_pframe_target_size(cpi);
2756 if (cpi->drop_frame)
2758 cpi->drop_frame = FALSE;
2759 cpi->drop_count++;
2767 static void set_quantizer(VP8_COMP *cpi, int Q)
2769 VP8_COMMON *cm = &cpi->common;
2770 MACROBLOCKD *mbd = &cpi->mb.e_mbd;
2795 mbd->segment_feature_data[MB_LVL_ALT_Q][0] = cpi->segment_feature_data[MB_LVL_ALT_Q][0];
2796 mbd->segment_feature_data[MB_LVL_ALT_Q][1] = cpi->segment_feature_data[MB_LVL_ALT_Q][1];
2797 mbd->segment_feature_data[MB_LVL_ALT_Q][2] = cpi->segment_feature_data[MB_LVL_ALT_Q][2];
2798 mbd->segment_feature_data[MB_LVL_ALT_Q][3] = cpi->segment_feature_data[MB_LVL_ALT_Q][3];
2802 vp8cx_init_quantizer(cpi);
2806 static void update_alt_ref_frame_and_stats(VP8_COMP *cpi)
2808 VP8_COMMON *cm = &cpi->common;
2814 if (!cpi->auto_gold)
2815 cpi->frames_till_gf_update_due = cpi->goldfreq;
2817 if ((cpi->pass != 2) && cpi->frames_till_gf_update_due)
2819 cpi->current_gf_interval = cpi->frames_till_gf_update_due;
2826 cpi->gf_overspend_bits += cpi->projected_frame_size;
2827 cpi->non_gf_bitrate_adjustment = cpi->gf_overspend_bits / cpi->frames_till_gf_update_due;
2831 vpx_memset(cpi->gf_active_flags, 1, (cm->mb_rows * cm->mb_cols));
2832 cpi->gf_active_count = cm->mb_rows * cm->mb_cols;
2835 cpi->common.frames_since_golden = 0;
2838 cpi->source_alt_ref_pending = FALSE;
2841 cpi->source_alt_ref_active = TRUE;
2845 static void update_golden_frame_and_stats(VP8_COMP *cpi)
2847 VP8_COMMON *cm = &cpi->common;
2859 if (!cpi->auto_gold)
2860 cpi->frames_till_gf_update_due = cpi->goldfreq;
2862 if ((cpi->pass != 2) && (cpi->frames_till_gf_update_due > 0))
2864 cpi->current_gf_interval = cpi->frames_till_gf_update_due;
2870 if ((cm->frame_type != KEY_FRAME) && !cpi->source_alt_ref_active)
2874 cpi->gf_overspend_bits += (cpi->projected_frame_size - cpi->inter_frame_target);
2877 cpi->non_gf_bitrate_adjustment = cpi->gf_overspend_bits / cpi->frames_till_gf_update_due;
2882 vpx_memset(cpi->gf_active_flags, 1, (cm->mb_rows * cm->mb_cols));
2883 cpi->gf_active_count = cm->mb_rows * cm->mb_cols;
2887 cpi->common.frames_since_golden = 0;
2891 cpi->recent_ref_frame_usage[INTRA_FRAME] = 1;
2892 cpi->recent_ref_frame_usage[LAST_FRAME] = 1;
2893 cpi->recent_ref_frame_usage[GOLDEN_FRAME] = 1;
2894 cpi->recent_ref_frame_usage[ALTREF_FRAME] = 1;
2899 // cpi->recent_ref_frame_usage[INTRA_FRAME] >>= 5;
2900 // cpi->recent_ref_frame_usage[LAST_FRAME] >>= 5;
2901 // cpi->recent_ref_frame_usage[GOLDEN_FRAME] >>= 5;
2902 // cpi->recent_ref_frame_usage[ALTREF_FRAME] >>= 5;
2907 if (cpi->oxcf.fixed_q >= 0 &&
2908 cpi->oxcf.play_alternate && !cpi->common.refresh_alt_ref_frame)
2910 cpi->source_alt_ref_pending = TRUE;
2911 cpi->frames_till_gf_update_due = cpi->baseline_gf_interval;
2914 if (!cpi->source_alt_ref_pending)
2915 cpi->source_alt_ref_active = FALSE;
2918 if (cpi->frames_till_gf_update_due > 0)
2919 cpi->frames_till_gf_update_due--;
2922 else if (!cpi->common.refresh_alt_ref_frame)
2925 if (cpi->frames_till_gf_update_due > 0)
2926 cpi->frames_till_gf_update_due--;
2928 if (cpi->common.frames_till_alt_ref_frame)
2929 cpi->common.frames_till_alt_ref_frame --;
2931 cpi->common.frames_since_golden ++;
2933 if (cpi->common.frames_since_golden > 1)
2935 cpi->recent_ref_frame_usage[INTRA_FRAME] += cpi->count_mb_ref_frame_usage[INTRA_FRAME];
2936 cpi->recent_ref_frame_usage[LAST_FRAME] += cpi->count_mb_ref_frame_usage[LAST_FRAME];
2937 cpi->recent_ref_frame_usage[GOLDEN_FRAME] += cpi->count_mb_ref_frame_usage[GOLDEN_FRAME];
2938 cpi->recent_ref_frame_usage[ALTREF_FRAME] += cpi->count_mb_ref_frame_usage[ALTREF_FRAME];
2945 static void update_rd_ref_frame_probs(VP8_COMP *cpi)
2947 VP8_COMMON *cm = &cpi->common;
2950 const int *const rfct = cpi->recent_ref_frame_usage;
2956 cpi->prob_intra_coded = 255;
2957 cpi->prob_last_coded = 128;
2958 cpi->prob_gf_coded = 128;
2962 // This is a trap in case this function is called with cpi->recent_ref_frame_usage[] blank.
2963 cpi->prob_intra_coded = 63;
2964 cpi->prob_last_coded = 128;
2965 cpi->prob_gf_coded = 128;
2969 cpi->prob_intra_coded = (rf_intra * 255) / (rf_intra + rf_inter);
2971 if (cpi->prob_intra_coded < 1)
2972 cpi->prob_intra_coded = 1;
2974 if ((cm->frames_since_golden > 0) || cpi->source_alt_ref_active)
2976 cpi->prob_last_coded = rf_inter ? (rfct[LAST_FRAME] * 255) / rf_inter : 128;
2978 if (cpi->prob_last_coded < 1)
2979 cpi->prob_last_coded = 1;
2981 cpi->prob_gf_coded = (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME])
2984 if (cpi->prob_gf_coded < 1)
2985 cpi->prob_gf_coded = 1;
2990 const int *const rfct = cpi->count_mb_ref_frame_usage;
2996 cpi->prob_intra_coded = 255;
2997 cpi->prob_last_coded = 128;
2998 cpi->prob_gf_coded = 128;
3002 // This is a trap in case this function is called with cpi->recent_ref_frame_usage[] blank.
3003 cpi->prob_intra_coded = 63;
3004 cpi->prob_last_coded = 128;
3005 cpi->prob_gf_coded = 128;
3009 cpi->prob_intra_coded = (rf_intra * 255) / (rf_intra + rf_inter);
3011 if (cpi->prob_intra_coded < 1)
3012 cpi->prob_intra_coded = 1;
3014 cpi->prob_last_coded = rf_inter ? (rfct[LAST_FRAME] * 255) / rf_inter : 128;
3016 if (cpi->prob_last_coded < 1)
3017 cpi->prob_last_coded = 1;
3019 cpi->prob_gf_coded = (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME])
3022 if (cpi->prob_gf_coded < 1)
3023 cpi->prob_gf_coded = 1;
3028 if (cpi->common.refresh_alt_ref_frame)
3030 cpi->prob_intra_coded += 40;
3031 cpi->prob_last_coded = 200;
3032 cpi->prob_gf_coded = 1;
3034 else if (cpi->common.frames_since_golden == 0)
3036 cpi->prob_last_coded = 214;
3037 cpi->prob_gf_coded = 1;
3039 else if (cpi->common.frames_since_golden == 1)
3041 cpi->prob_last_coded = 192;
3042 cpi->prob_gf_coded = 220;
3044 else if (cpi->source_alt_ref_active)
3046 //int dist = cpi->common.frames_till_alt_ref_frame + cpi->common.frames_since_golden;
3047 cpi->prob_gf_coded -= 20;
3049 if (cpi->prob_gf_coded < 10)
3050 cpi->prob_gf_coded = 10;
3058 static int decide_key_frame(VP8_COMP *cpi)
3060 VP8_COMMON *cm = &cpi->common;
3064 cpi->kf_boost = 0;
3066 if (cpi->Speed > 11)
3072 if ((cpi->compressor_speed == 2) && (cpi->Speed >= 5) && (cpi->sf.RD == 0))
3074 double change = 1.0 * abs((int)(cpi->intra_error - cpi->last_intra_error)) / (1 + cpi->last_intra_error);
3075 double change2 = 1.0 * abs((int)(cpi->prediction_error - cpi->last_prediction_error)) / (1 + cpi->last_prediction_error);
3080 if (10 * cpi->intra_error / (1 + cpi->prediction_error) < 15
3081 && cpi->prediction_error > minerror
3086 if (cpi->prediction_error <= 0)
3087 cpi->prediction_error = 1;
3091 (int) cpi->prediction_error,
3092 (int) cpi->intra_error,
3093 (int)((10 * cpi->intra_error) / cpi->prediction_error),
3101 cpi->last_intra_error = cpi->intra_error;
3102 cpi->last_prediction_error = cpi->prediction_error;
3104 if (10 * cpi->intra_error / (1 + cpi->prediction_error) < 15
3105 && cpi->prediction_error > minerror
3108 /*(change > 1.4 || change < .75)&& cpi->this_frame_percent_intra > cpi->last_frame_percent_intra + 3*/
3117 if (((cpi->this_frame_percent_intra == 100) &&
3118 (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra + 2))) ||
3119 ((cpi->this_frame_percent_intra > 95) &&
3120 (cpi->this_frame_percent_intra >= (cpi->last_frame_percent_intra + 5))))
3127 else if (((cpi->this_frame_percent_intra > 60) &&
3128 (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra * 2))) ||
3129 ((cpi->this_frame_percent_intra > 75) &&
3130 (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra * 3 / 2))) ||
3131 ((cpi->this_frame_percent_intra > 90) &&
3132 (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra + 10))))
3143 static void Pass1Encode(VP8_COMP *cpi, unsigned long *size, unsigned char *dest, unsigned int *frame_flags)
3148 set_quantizer(cpi, 26);
3150 scale_and_extend_source(cpi->un_scaled_source, cpi);
3151 vp8_first_pass(cpi);
3191 static BOOL recode_loop_test( VP8_COMP *cpi,
3196 VP8_COMMON *cm = &cpi->common;
3201 if ( (cpi->sf.recode_loop == 1) ||
3202 ( (cpi->sf.recode_loop == 2) &&
3208 if ( ((cpi->projected_frame_size > high_limit) && (q < maxq)) ||
3209 ((cpi->projected_frame_size < low_limit) && (q > minq)) )
3214 else if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY)
3217 if ( (q > cpi->cq_target_quality) &&
3218 (cpi->projected_frame_size <
3219 ((cpi->this_frame_target * 7) >> 3)))
3224 else if ( (q > cpi->oxcf.cq_level) &&
3225 (cpi->projected_frame_size < cpi->min_frame_bandwidth) &&
3226 (cpi->active_best_quality > cpi->oxcf.cq_level))
3229 cpi->active_best_quality = cpi->oxcf.cq_level;
3237 void loopfilter_frame(VP8_COMP *cpi, VP8_COMMON *cm)
3250 if (cpi->sf.auto_filter == 0)
3251 vp8cx_pick_filter_level_fast(cpi->Source, cpi);
3254 vp8cx_pick_filter_level(cpi->Source, cpi);
3257 cpi->time_pick_lpf += vpx_usec_timer_elapsed(&timer);
3261 if (cpi->b_multi_threaded)
3262 sem_post(&cpi->h_event_end_lpf); /* signal that we have set filter_level */
3267 vp8cx_set_alt_lf_level(cpi, cm->filter_level);
3268 vp8_loop_filter_frame(cm, &cpi->mb.e_mbd, cm->filter_level);
3323 VP8_COMP *cpi,
3344 VP8_COMMON *cm = &cpi->common;
3349 int drop_mark = cpi->oxcf.drop_frames_water_mark * cpi->oxcf.optimal_buffer_level / 100;
3359 //segmentation_test_function((VP8_PTR) cpi);
3362 if(cpi->oxcf.auto_key && cm->frame_type != KEY_FRAME)
3364 if(cpi->force_next_frame_intra)
3369 cpi->force_next_frame_intra = 0;
3375 if (cpi->pass == 2)
3377 if (cpi->common.refresh_alt_ref_frame)
3379 cpi->per_frame_bandwidth = cpi->gf_bits; // Per frame bit target for the alt ref frame
3380 cpi->target_bandwidth = cpi->gf_bits * cpi->output_frame_rate; // per second target bitrate
3385 cpi->per_frame_bandwidth = (int)(cpi->target_bandwidth / cpi->output_frame_rate);
3392 cpi->zbin_over_quant = 0;
3393 cpi->zbin_mode_boost = 0;
3398 cpi->zbin_mode_boost = 0;
3399 cpi->zbin_mode_boost_enabled = TRUE;
3400 if (cpi->pass == 2)
3402 if ( cpi->gfu_boost <= 400 )
3404 cpi->zbin_mode_boost_enabled = FALSE;
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;
3418 (cpi->oxcf.auto_key && (cpi->frames_since_key % cpi->key_frame_frequency == 0)))
3425 cpi->mb.e_mbd.update_mb_segmentation_map = 0;
3426 cpi->mb.e_mbd.update_mb_segmentation_data = 0;
3427 cpi->mb.e_mbd.mode_ref_lf_delta_update = 0;
3435 setup_features(cpi);
3438 if (cpi->mb.e_mbd.segmentation_enabled)
3440 cpi->mb.e_mbd.update_mb_segmentation_map = 1;
3441 cpi->mb.e_mbd.update_mb_segmentation_data = 1;
3445 cpi->source_alt_ref_active = FALSE;
3450 cpi->rd_thresh_mult[i] = 128;
3457 // enable_segmentation((VP8_PTR)cpi);
3459 // disable_segmentation((VP8_PTR)cpi);
3465 //if ( cpi->pass == 0 )
3467 cpi->one_pass_frame_index = cm->current_video_frame % MAX_LAG_BUFFERS;
3469 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frames_so_far = 0;
3470 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_intra_error = 0.0;
3471 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_coded_error = 0.0;
3472 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_pcnt_inter = 0.0;
3473 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_pcnt_motion = 0.0;
3474 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvr = 0.0;
3475 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvr_abs = 0.0;
3476 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvc = 0.0;
3477 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvc_abs = 0.0;
3481 update_rd_ref_frame_probs(cpi);
3483 if (cpi->drop_frames_allowed)
3487 if ((cpi->buffer_level > drop_mark) && (cpi->decimation_factor > 0))
3488 cpi->decimation_factor --;
3490 if (cpi->buffer_level > drop_mark75 && cpi->decimation_factor > 0)
3491 cpi->decimation_factor = 1;
3493 else if (cpi->buffer_level < drop_mark25 && (cpi->decimation_factor == 2 || cpi->decimation_factor == 3))
3495 cpi->decimation_factor = 3;
3497 else if (cpi->buffer_level < drop_mark50 && (cpi->decimation_factor == 1 || cpi->decimation_factor == 2))
3499 cpi->decimation_factor = 2;
3501 else if (cpi->buffer_level < drop_mark75 && (cpi->decimation_factor == 0 || cpi->decimation_factor == 1))
3503 cpi->decimation_factor = 1;
3506 //vpx_log("Encoder: Decimation Factor: %d \n",cpi->decimation_factor);
3514 if (cpi->decimation_factor > 0)
3516 switch (cpi->decimation_factor)
3519 cpi->per_frame_bandwidth = cpi->per_frame_bandwidth * 3 / 2;
3522 cpi->per_frame_bandwidth = cpi->per_frame_bandwidth * 5 / 4;
3525 cpi->per_frame_bandwidth = cpi->per_frame_bandwidth * 5 / 4;
3530 if ((cm->frame_type == KEY_FRAME)) // && cpi->oxcf.allow_spatial_resampling )
3532 cpi->decimation_count = cpi->decimation_factor;
3534 else if (cpi->decimation_count > 0)
3536 cpi->decimation_count --;
3537 cpi->bits_off_target += cpi->av_per_frame_bandwidth;
3539 cpi->frames_since_key++;
3542 cpi->count ++;
3545 cpi->buffer_level = cpi->bits_off_target;
3550 cpi->decimation_count = cpi->decimation_factor;
3554 if (!pick_frame_size(cpi))
3557 cpi->frames_since_key++;
3566 if ((cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) &&
3567 (cpi->buffer_level >= cpi->oxcf.optimal_buffer_level) && cpi->buffered_mode)
3569 int Adjustment = cpi->active_worst_quality / 4; // Max adjustment is 1/4
3574 int tmp_lvl = cpi->buffer_level;
3576 if (cpi->buffer_level < cpi->oxcf.maximum_buffer_size)
3578 buff_lvl_step = (cpi->oxcf.maximum_buffer_size - cpi->oxcf.optimal_buffer_level) / Adjustment;
3582 Adjustment = (cpi->buffer_level - cpi->oxcf.optimal_buffer_level) / buff_lvl_step;
3583 cpi->active_worst_quality -= Adjustment;
3588 cpi->active_worst_quality -= Adjustment;
3595 if ( (cpi->pass == 2) || (cpi->ni_frames > 150))
3599 Q = cpi->active_worst_quality;
3603 if ( cpi->pass == 2 )
3605 if (cpi->gfu_boost > 600)
3606 cpi->active_best_quality = kf_low_motion_minq[Q];
3608 cpi->active_best_quality = kf_high_motion_minq[Q];
3613 if ( cpi->this_key_frame_forced )
3615 if ( cpi->active_best_quality > cpi->avg_frame_qindex * 7/8)
3616 cpi->active_best_quality = cpi->avg_frame_qindex * 7/8;
3617 else if ( cpi->active_best_quality < cpi->avg_frame_qindex >> 2 )
3618 cpi->active_best_quality = cpi->avg_frame_qindex >> 2;
3623 cpi->active_best_quality = kf_high_motion_minq[Q];
3626 else if (cm->refresh_golden_frame || cpi->common.refresh_alt_ref_frame)
3628 // Use the lower of cpi->active_worst_quality and recent
3631 if ( (cpi->frames_since_key > 1) &&
3632 (cpi->avg_frame_qindex < cpi->active_worst_quality) )
3634 Q = cpi->avg_frame_qindex;
3636 if ( (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) &&
3637 (Q < cpi->oxcf.cq_level) )
3639 Q = cpi->oxcf.cq_level;
3643 if ( cpi->pass == 2 )
3645 if ( cpi->gfu_boost > 1000 )
3646 cpi->active_best_quality = gf_low_motion_minq[Q];
3647 else if ( cpi->gfu_boost < 400 )
3648 cpi->active_best_quality = gf_high_motion_minq[Q];
3650 cpi->active_best_quality = gf_mid_motion_minq[Q];
3654 cpi->active_best_quality = gf_high_motion_minq[Q];
3658 cpi->active_best_quality = inter_minq[Q];
3662 if ((cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) &&
3663 (cpi->active_best_quality < cpi->cq_target_quality) )
3668 if ( cpi->rolling_actual_bits < cpi->min_frame_bandwidth )
3669 cpi->active_best_quality = cpi->oxcf.cq_level;
3671 cpi->active_best_quality = cpi->cq_target_quality;
3677 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
3681 if (cpi->buffer_level >= cpi->oxcf.maximum_buffer_size)
3682 cpi->active_best_quality = cpi->best_quality;
3684 else if (cpi->buffer_level > cpi->oxcf.optimal_buffer_level)
3686 int Fraction = ((cpi->buffer_level - cpi->oxcf.optimal_buffer_level) * 128) / (cpi->oxcf.maximum_buffer_size - cpi->oxcf.optimal_buffer_level);
3687 int min_qadjustment = ((cpi->active_best_quality - cpi->best_quality) * Fraction) / 128;
3689 cpi->active_best_quality -= min_qadjustment;
3695 else if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY)
3698 cm->refresh_golden_frame || cpi->common.refresh_alt_ref_frame )
3700 cpi->active_best_quality = cpi->best_quality;
3702 else if (cpi->active_best_quality < cpi->cq_target_quality)
3704 cpi->active_best_quality = cpi->cq_target_quality;
3709 if (cpi->active_worst_quality > cpi->worst_quality)
3710 cpi->active_worst_quality = cpi->worst_quality;
3712 if (cpi->active_best_quality < cpi->best_quality)
3713 cpi->active_best_quality = cpi->best_quality;
3714 else if (cpi->active_best_quality > cpi->active_worst_quality)
3715 cpi->active_best_quality = cpi->active_worst_quality;
3718 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
3719 last_zbin_oq = cpi->zbin_over_quant;
3724 else if (cm->refresh_alt_ref_frame || (cm->refresh_golden_frame && !cpi->source_alt_ref_active))
3730 if (cpi->cyclic_refresh_mode_enabled)
3731 cyclic_background_refresh(cpi, Q, 0);
3733 vp8_compute_frame_size_bounds(cpi, &frame_under_shoot_limit, &frame_over_shoot_limit);
3736 bottom_index = cpi->active_best_quality;
3737 top_index = cpi->active_worst_quality;
3738 q_low = cpi->active_best_quality;
3739 q_high = cpi->active_worst_quality;
3741 vp8_save_coding_context(cpi);
3746 scale_and_extend_source(cpi->un_scaled_source, cpi);
3749 if (cpi->oxcf.noise_sensitivity > 0)
3754 switch (cpi->oxcf.noise_sensitivity)
3779 vp8_de_noise(cpi->Source, cpi->Source, l , 1, 0, RTCD(postproc));
3783 vp8_de_noise(cpi->Source, cpi->Source, l , 1, 0, RTCD(postproc));
3785 src = cpi->Source->y_buffer;
3787 if (cpi->Source->y_stride < 0)
3789 src += cpi->Source->y_stride * (cpi->Source->y_height - 1);
3797 vp8_write_yuv_frame(cpi->Source);
3805 if(cpi->is_src_frame_alt_ref)
3809 set_quantizer(cpi, Q);
3813 if (cpi->common.mb_no_coeff_skip)
3815 cpi->prob_skip_false = cpi->base_skip_false_prob[Q];
3819 if (cpi->common.refresh_alt_ref_frame)
3821 if (cpi->last_skip_false_probs[2] != 0)
3822 cpi->prob_skip_false = cpi->last_skip_false_probs[2];
3825 if(cpi->last_skip_false_probs[2]!=0 && abs(Q- cpi->last_skip_probs_q[2])<=16 )
3826 cpi->prob_skip_false = cpi->last_skip_false_probs[2];
3827 else if (cpi->last_skip_false_probs[2]!=0)
3828 cpi->prob_skip_false = (cpi->last_skip_false_probs[2] + cpi->prob_skip_false ) / 2;
3831 else if (cpi->common.refresh_golden_frame)
3833 if (cpi->last_skip_false_probs[1] != 0)
3834 cpi->prob_skip_false = cpi->last_skip_false_probs[1];
3837 if(cpi->last_skip_false_probs[1]!=0 && abs(Q- cpi->last_skip_probs_q[1])<=16 )
3838 cpi->prob_skip_false = cpi->last_skip_false_probs[1];
3839 else if (cpi->last_skip_false_probs[1]!=0)
3840 cpi->prob_skip_false = (cpi->last_skip_false_probs[1] + cpi->prob_skip_false ) / 2;
3845 if (cpi->last_skip_false_probs[0] != 0)
3846 cpi->prob_skip_false = cpi->last_skip_false_probs[0];
3849 if(cpi->last_skip_false_probs[0]!=0 && abs(Q- cpi->last_skip_probs_q[0])<=16 )
3850 cpi->prob_skip_false = cpi->last_skip_false_probs[0];
3851 else if(cpi->last_skip_false_probs[0]!=0)
3852 cpi->prob_skip_false = (cpi->last_skip_false_probs[0] + cpi->prob_skip_false ) / 2;
3857 if (cpi->prob_skip_false < 5)
3858 cpi->prob_skip_false = 5;
3860 if (cpi->prob_skip_false > 250)
3861 cpi->prob_skip_false = 250;
3863 if (cpi->is_src_frame_alt_ref)
3864 cpi->prob_skip_false = 1;
3871 if (cpi->pass != 1)
3874 fprintf(f, "%d, %d, %4d ", cpi->common.refresh_golden_frame, cpi->common.refresh_alt_ref_frame, cpi->prob_skip_false);
3883 vp8_setup_key_frame(cpi);
3886 vp8_encode_frame(cpi);
3888 cpi->projected_frame_size -= vp8_estimate_entropy_savings(cpi);
3889 cpi->projected_frame_size = (cpi->projected_frame_size > 0) ? cpi->projected_frame_size : 0;
3895 if (cpi->pass != 2 && cpi->oxcf.auto_key && cm->frame_type != KEY_FRAME)
3902 cpi->force_next_frame_intra = decide_key_frame(cpi);
3905 if (decide_key_frame(cpi))
3907 vp8_calc_auto_iframe_target_size(cpi);
3913 cpi->source_alt_ref_active = FALSE;
3916 setup_features(cpi);
3919 if (cpi->mb.e_mbd.segmentation_enabled)
3921 cpi->mb.e_mbd.update_mb_segmentation_map = 1;
3922 cpi->mb.e_mbd.update_mb_segmentation_data = 1;
3925 vp8_restore_coding_context(cpi);
3927 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
3929 vp8_compute_frame_size_bounds(cpi, &frame_under_shoot_limit, &frame_over_shoot_limit);
3932 bottom_index = cpi->active_best_quality;
3933 top_index = cpi->active_worst_quality;
3934 q_low = cpi->active_best_quality;
3935 q_high = cpi->active_worst_quality;
3940 resize_key_frame(cpi);
3952 if (((cpi->pass != 2) || (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)) &&
3953 (Q == cpi->active_worst_quality) &&
3954 (cpi->active_worst_quality < cpi->worst_quality) &&
3955 (cpi->projected_frame_size > frame_over_shoot_limit))
3957 int over_size_percent = ((cpi->projected_frame_size - frame_over_shoot_limit) * 100) / frame_over_shoot_limit;
3960 while ((cpi->active_worst_quality < cpi->worst_quality) && (over_size_percent > 0))
3962 cpi->active_worst_quality++;
3963 top_index = cpi->active_worst_quality;
3975 if ( (cm->frame_type == KEY_FRAME) && cpi->this_key_frame_forced )
3978 int kf_err = vp8_calc_ss_err(cpi->Source,
3980 IF_RTCD(&cpi->rtcd.variance));
3983 if ( kf_err > ((cpi->ambient_err * 7) >> 3) )
3992 else if ( kf_err < (cpi->ambient_err >> 1) )
4011 else if ( recode_loop_test( cpi,
4022 if (cpi->projected_frame_size > cpi->this_frame_target)
4024 //if ( cpi->zbin_over_quant == 0 )
4027 if (cpi->zbin_over_quant > 0) // If we are using over quant do the same for zbin_oq_low
4028 zbin_oq_low = (cpi->zbin_over_quant < zbin_oq_high) ? (cpi->zbin_over_quant + 1) : zbin_oq_high;
4033 // Update rate_correction_factor unless cpi->active_worst_quality has changed.
4035 vp8_update_rate_correction_factors(cpi, 1);
4039 // Adjust cpi->zbin_over_quant (only allowed when Q is max)
4041 cpi->zbin_over_quant = 0;
4044 zbin_oq_low = (cpi->zbin_over_quant < zbin_oq_high) ? (cpi->zbin_over_quant + 1) : zbin_oq_high;
4045 cpi->zbin_over_quant = (zbin_oq_high + zbin_oq_low) / 2;
4050 // Update rate_correction_factor unless cpi->active_worst_quality has changed.
4052 vp8_update_rate_correction_factors(cpi, 0);
4054 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4056 while (((Q < q_low) || (cpi->zbin_over_quant < zbin_oq_low)) && (Retries < 10))
4058 vp8_update_rate_correction_factors(cpi, 0);
4059 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4069 if (cpi->zbin_over_quant == 0)
4072 zbin_oq_high = (cpi->zbin_over_quant > zbin_oq_low) ? (cpi->zbin_over_quant - 1) : zbin_oq_low;
4076 // Update rate_correction_factor unless cpi->active_worst_quality has changed.
4078 vp8_update_rate_correction_factors(cpi, 1);
4082 // Adjust cpi->zbin_over_quant (only allowed when Q is max)
4084 cpi->zbin_over_quant = 0;
4086 cpi->zbin_over_quant = (zbin_oq_high + zbin_oq_low) / 2;
4090 // Update rate_correction_factor unless cpi->active_worst_quality has changed.
4092 vp8_update_rate_correction_factors(cpi, 0);
4094 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4100 if ( (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) &&
4106 while (((Q > q_high) || (cpi->zbin_over_quant > zbin_oq_high)) && (Retries < 10))
4108 vp8_update_rate_correction_factors(cpi, 0);
4109 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4123 // Clamp cpi->zbin_over_quant
4124 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;
4126 //Loop = ((Q != last_q) || (last_zbin_oq != cpi->zbin_over_quant)) ? TRUE : FALSE;
4128 last_zbin_oq = cpi->zbin_over_quant;
4134 if (cpi->is_src_frame_alt_ref)
4139 vp8_restore_coding_context(cpi);
4142 cpi->tot_recode_hits++;
4164 cpi->one_pass_frame_stats[cpi->one_pass_frame_index].frame_coded_error = (double)cpi->prediction_error;
4165 cpi->one_pass_frame_stats[cpi->one_pass_frame_index].frame_intra_error = (double)cpi->intra_error;
4166 cpi->one_pass_frame_stats[cpi->one_pass_frame_index].frame_pcnt_inter = (double)(100 - cpi->this_frame_percent_intra) / 100.0;
4173 if ( cpi->next_key_frame_forced && (cpi->frames_to_key == 0) )
4175 cpi->ambient_err = vp8_calc_ss_err(cpi->Source,
4177 IF_RTCD(&cpi->rtcd.variance));
4195 cpi->lfmv[mb_col + mb_row*(cm->mode_info_stride+1)].as_int = tmp->mbmi.mv.as_int;
4197 cpi->lf_ref_frame_sign_bias[mb_col + mb_row*(cm->mode_info_stride+1)] = cm->ref_frame_sign_bias[tmp->mbmi.ref_frame];
4198 cpi->lf_ref_frame[mb_col + mb_row*(cm->mode_info_stride+1)] = tmp->mbmi.ref_frame;
4208 vp8_update_gf_useage_maps(cpi, cm, &cpi->mb);
4216 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);
4223 if (!cpi->oxcf.error_resilient_mode && cm->refresh_golden_frame)
4238 if (cpi->b_multi_threaded)
4240 sem_post(&cpi->h_event_start_lpf); /* start loopfilter in separate thread */
4245 loopfilter_frame(cpi, cm);
4248 if (cpi->oxcf.error_resilient_mode == 1)
4255 if (cpi->b_multi_threaded)
4256 sem_wait(&cpi->h_event_end_lpf);
4260 vp8_pack_bitstream(cpi, dest, size);
4264 if (cpi->b_multi_threaded)
4266 sem_wait(&cpi->h_event_end_lpf);
4275 cpi->total_byte_count += (*size);
4276 cpi->projected_frame_size = (*size) << 3;
4279 vp8_update_rate_correction_factors(cpi, 2);
4281 cpi->last_q[cm->frame_type] = cm->base_qindex;
4285 vp8_adjust_key_frame_context(cpi);
4290 cpi->avg_frame_qindex = (2 + 3 * cpi->avg_frame_qindex + cm->base_qindex) >> 2;
4295 cpi->ni_frames++;
4299 if ( cpi->pass == 2 )
4301 cpi->ni_tot_qi += Q;
4302 cpi->ni_av_qi = (cpi->ni_tot_qi / cpi->ni_frames);
4307 if (cpi->ni_frames > 150 )
4309 cpi->ni_tot_qi += Q;
4310 cpi->ni_av_qi = (cpi->ni_tot_qi / cpi->ni_frames);
4316 cpi->ni_tot_qi += Q;
4317 cpi->ni_av_qi = ((cpi->ni_tot_qi / cpi->ni_frames) + cpi->worst_quality + 1) / 2;
4326 if (Q > cpi->ni_av_qi)
4327 cpi->ni_av_qi = Q - 1;
4334 if ((cpi->drop_frames_allowed) &&
4335 (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) &&
4336 (cpi->buffer_level < cpi->oxcf.drop_frames_water_mark * cpi->oxcf.optimal_buffer_level / 100) &&
4337 (cpi->projected_frame_size > (4 * cpi->this_frame_target)))
4339 cpi->drop_frame = TRUE;
4346 // (cpi->av_per_frame_bandwidth > 0) is just a sanity check to prevent / 0.
4347 if (cpi->drop_frames_allowed && (cpi->av_per_frame_bandwidth > 0))
4349 cpi->max_drop_count = cpi->projected_frame_size / cpi->av_per_frame_bandwidth;
4351 if (cpi->max_drop_count > cpi->max_consec_dropped_frames)
4352 cpi->max_drop_count = cpi->max_consec_dropped_frames;
4358 cpi->bits_off_target -= cpi->projected_frame_size;
4360 cpi->bits_off_target += cpi->av_per_frame_bandwidth - cpi->projected_frame_size;
4363 cpi->rolling_target_bits = ((cpi->rolling_target_bits * 3) + cpi->this_frame_target + 2) / 4;
4364 cpi->rolling_actual_bits = ((cpi->rolling_actual_bits * 3) + cpi->projected_frame_size + 2) / 4;
4365 cpi->long_rolling_target_bits = ((cpi->long_rolling_target_bits * 31) + cpi->this_frame_target + 16) / 32;
4366 cpi->long_rolling_actual_bits = ((cpi->long_rolling_actual_bits * 31) + cpi->projected_frame_size + 16) / 32;
4369 cpi->total_actual_bits += cpi->projected_frame_size;
4372 cpi->total_target_vs_actual += (cpi->this_frame_target - cpi->projected_frame_size);
4374 cpi->buffer_level = cpi->bits_off_target;
4379 cpi->kf_group_bits += cpi->this_frame_target - cpi->projected_frame_size;
4381 if (cpi->kf_group_bits < 0)
4382 cpi->kf_group_bits = 0 ;
4386 cpi->gf_group_bits += cpi->this_frame_target - cpi->projected_frame_size;
4388 if (cpi->gf_group_bits < 0)
4389 cpi->gf_group_bits = 0 ;
4394 if (cpi->common.refresh_alt_ref_frame)
4396 cpi->last_skip_false_probs[2] = cpi->prob_skip_false;
4397 cpi->last_skip_probs_q[2] = cm->base_qindex;
4399 else if (cpi->common.refresh_golden_frame)
4401 cpi->last_skip_false_probs[1] = cpi->prob_skip_false;
4402 cpi->last_skip_probs_q[1] = cm->base_qindex;
4406 cpi->last_skip_false_probs[0] = cpi->prob_skip_false;
4407 cpi->last_skip_probs_q[0] = cm->base_qindex;
4410 cpi->base_skip_false_prob[cm->base_qindex] = cpi->prob_skip_false;
4421 if (cpi->total_coded_error_left != 0.0)
4425 cpi->common.current_video_frame, cpi->this_frame_target,
4426 cpi->projected_frame_size,
4427 (cpi->projected_frame_size - cpi->this_frame_target),
4428 (int)cpi->total_target_vs_actual,
4429 (cpi->oxcf.starting_buffer_level-cpi->bits_off_target),
4430 (int)cpi->total_actual_bits, cm->base_qindex,
4431 cpi->active_best_quality, cpi->active_worst_quality,
4432 cpi->ni_av_qi, cpi->cq_target_quality, cpi->zbin_over_quant,
4433 //cpi->avg_frame_qindex, cpi->zbin_over_quant,
4435 cm->frame_type, cpi->gfu_boost,
4436 cpi->est_max_qcorrection_factor, (int)cpi->bits_left,
4437 cpi->total_coded_error_left,
4438 (double)cpi->bits_left / cpi->total_coded_error_left,
4439 cpi->tot_recode_hits);
4444 cpi->common.current_video_frame,
4445 cpi->this_frame_target, cpi->projected_frame_size,
4446 (cpi->projected_frame_size - cpi->this_frame_target),
4447 (int)cpi->total_target_vs_actual,
4448 (cpi->oxcf.starting_buffer_level-cpi->bits_off_target),
4449 (int)cpi->total_actual_bits, cm->base_qindex,
4450 cpi->active_best_quality, cpi->active_worst_quality,
4451 cpi->ni_av_qi, cpi->cq_target_quality, cpi->zbin_over_quant,
4452 //cpi->avg_frame_qindex, cpi->zbin_over_quant,
4454 cm->frame_type, cpi->gfu_boost,
4455 cpi->est_max_qcorrection_factor, (int)cpi->bits_left,
4456 cpi->total_coded_error_left, cpi->tot_recode_hits);
4465 cpi->common.current_video_frame,
4470 fprintf(fmodes, "%5d ", cpi->mode_chosen_counts[i]);
4496 cpi->gold_is_last = 1;
4498 cpi->gold_is_last = 0;
4501 cpi->alt_is_last = 1;
4503 cpi->alt_is_last = 0;
4506 cpi->gold_is_alt = 1;
4508 cpi->gold_is_alt = 0;
4510 cpi->ref_frame_flags = VP8_ALT_FLAG | VP8_GOLD_FLAG | VP8_LAST_FLAG;
4512 if (cpi->gold_is_last)
4513 cpi->ref_frame_flags &= ~VP8_GOLD_FLAG;
4515 if (cpi->alt_is_last)
4516 cpi->ref_frame_flags &= ~VP8_ALT_FLAG;
4518 if (cpi->gold_is_alt)
4519 cpi->ref_frame_flags &= ~VP8_ALT_FLAG;
4522 if (cpi->oxcf.error_resilient_mode)
4536 if (cpi->oxcf.play_alternate && cm->refresh_alt_ref_frame && (cm->frame_type != KEY_FRAME))
4538 update_alt_ref_frame_and_stats(cpi);
4541 update_golden_frame_and_stats(cpi);
4552 cpi->last_frame_percent_intra = 100;
4558 cpi->last_frame_percent_intra = cpi->this_frame_percent_intra;
4562 cpi->mb.e_mbd.update_mb_segmentation_map = 0;
4563 cpi->mb.e_mbd.update_mb_segmentation_data = 0;
4564 cpi->mb.e_mbd.mode_ref_lf_delta_update = 0;
4571 cpi->frames_since_key++;
4597 static void check_gf_quality(VP8_COMP *cpi)
4599 VP8_COMMON *cm = &cpi->common;
4600 int gf_active_pct = (100 * cpi->gf_active_count) / (cm->mb_rows * cm->mb_cols);
4601 int gf_ref_usage_pct = (cpi->count_mb_ref_frame_usage[GOLDEN_FRAME] * 100) / (cm->mb_rows * cm->mb_cols);
4602 int last_ref_zz_useage = (cpi->inter_zz_count * 100) / (cm->mb_rows * cm->mb_cols);
4605 if (cpi->gf_update_recommended == 0)
4607 if (cpi->common.frames_since_golden > 7)
4615 cpi->gf_bad_count ++;
4617 if (cpi->gf_bad_count >= 8) // Check that the condition is stable
4619 cpi->gf_update_recommended = 1;
4620 cpi->gf_bad_count = 0;
4624 cpi->gf_bad_count = 0; // Restart count as the background is not stable enough
4627 cpi->gf_bad_count = 0; // Gf useage has picked up so reset count
4633 cpi->gf_update_recommended = 0;
4634 cpi->gf_bad_count = 0;
4642 cpi->common.frames_since_golden,
4644 cpi->gf_update_recommended);
4652 static void Pass2Encode(VP8_COMP *cpi, unsigned long *size, unsigned char *dest, unsigned int *frame_flags)
4655 if (!cpi->common.refresh_alt_ref_frame)
4656 vp8_second_pass(cpi);
4658 encode_frame_to_data_rate(cpi, size, dest, frame_flags);
4659 cpi->bits_left -= 8 * *size;
4661 if (!cpi->common.refresh_alt_ref_frame)
4663 double two_pass_min_rate = (double)(cpi->oxcf.target_bandwidth
4664 *cpi->oxcf.two_pass_vbrmin_section / 100);
4665 cpi->bits_left += (long long)(two_pass_min_rate / cpi->oxcf.frame_rate);
4680 VP8_COMP *cpi = (VP8_COMP *) ptr;
4681 VP8_COMMON *cm = &cpi->common;
4684 if (!cpi)
4699 if (cpi->source_buffer_count != 0 && cpi->source_buffer_count >= cpi->oxcf.lag_in_frames)
4712 //printf("in-cpi->source_buffer_count: %d\n", cpi->source_buffer_count);
4719 if (cpi->oxcf.allow_lag)
4721 int which_buffer = cpi->source_encode_index - 1;
4725 which_buffer = cpi->oxcf.lag_in_frames - 1;
4727 if (cpi->source_buffer_count < cpi->oxcf.lag_in_frames - 1)
4728 which_buffer = cpi->source_buffer_count;
4730 s = &cpi->src_buffer[which_buffer];
4737 cpi->source_buffer_count ++;
4743 s = &cpi->src_buffer[0];
4763 cpi->source_buffer_count = 1;
4767 cpi->time_receive_data += vpx_usec_timer_elapsed(&timer);
4785 VP8_COMP *cpi = (VP8_COMP *) ptr;
4786 VP8_COMMON *cm = &cpi->common;
4791 if (!cpi)
4809 if ((cpi->source_buffer_count == cpi->oxcf.lag_in_frames && cpi->oxcf.lag_in_frames > 0)
4810 || (!cpi->oxcf.allow_lag && cpi->source_buffer_count > 0)
4811 || (flush && cpi->source_buffer_count > 0))
4816 s = &cpi->src_buffer[cpi->source_encode_index];
4817 cpi->source_time_stamp = s->source_time_stamp;
4818 cpi->source_end_time_stamp = s->source_end_time_stamp;
4823 if (cpi->oxcf.error_resilient_mode == 0 &&
4824 cpi->oxcf.play_alternate &&
4825 cpi->source_alt_ref_pending &&
4826 (cpi->frames_till_gf_update_due < cpi->source_buffer_count) &&
4827 cpi->oxcf.lag_in_frames != 0)
4829 cpi->last_alt_ref_sei = (cpi->source_encode_index + cpi->frames_till_gf_update_due) % cpi->oxcf.lag_in_frames;
4833 if (cpi->oxcf.arnr_max_frames > 0)
4837 int start_frame = cpi->last_alt_ref_sei - 3;
4839 int i, besti = -1, pastin = cpi->oxcf.arnr_strength;
4844 start_frame += cpi->oxcf.lag_in_frames;
4846 besterr = calc_low_ss_err(&cpi->src_buffer[cpi->last_alt_ref_sei].source_buffer,
4847 &cpi->src_buffer[start_frame].source_buffer, IF_RTCD(&cpi->rtcd.variance));
4852 cpi->oxcf.arnr_strength = i;
4853 vp8_temporal_filter_prepare_c(cpi);
4855 thiserr = calc_low_ss_err(&cpi->alt_ref_buffer.source_buffer,
4856 &cpi->src_buffer[start_frame].source_buffer, IF_RTCD(&cpi->rtcd.variance));
4867 cpi->oxcf.arnr_strength = besti;
4868 vp8_temporal_filter_prepare_c(cpi);
4869 s = &cpi->alt_ref_buffer;
4872 s->source_time_stamp = cpi->src_buffer[cpi->last_alt_ref_sei].source_time_stamp;
4873 s->source_end_time_stamp = cpi->src_buffer[cpi->last_alt_ref_sei].source_end_time_stamp;
4874 s->source_frame_flags = cpi->src_buffer[cpi->last_alt_ref_sei].source_frame_flags;
4877 s = &cpi->src_buffer[cpi->last_alt_ref_sei];
4880 vp8_temporal_filter_prepare_c(cpi);
4881 s = &cpi->alt_ref_buffer;
4884 s->source_time_stamp = cpi->src_buffer[cpi->last_alt_ref_sei].source_time_stamp;
4885 s->source_end_time_stamp = cpi->src_buffer[cpi->last_alt_ref_sei].source_end_time_stamp;
4886 s->source_frame_flags = cpi->src_buffer[cpi->last_alt_ref_sei].source_frame_flags;
4892 s = &cpi->src_buffer[cpi->last_alt_ref_sei];
4894 cm->frames_till_alt_ref_frame = cpi->frames_till_gf_update_due;
4899 cpi->source_alt_ref_pending = FALSE; // Clear Pending altf Ref flag.
4900 cpi->is_src_frame_alt_ref = 0;
4901 cpi->is_next_src_alt_ref = 0;
4909 if (cpi->oxcf.allow_lag)
4911 if (cpi->source_encode_index == cpi->last_alt_ref_sei)
4913 cpi->is_src_frame_alt_ref = 1;
4914 cpi->last_alt_ref_sei = -1;
4917 cpi->is_src_frame_alt_ref = 0;
4919 cpi->source_encode_index = (cpi->source_encode_index + 1) % cpi->oxcf.lag_in_frames;
4921 if(cpi->source_encode_index == cpi->last_alt_ref_sei)
4922 cpi->is_next_src_alt_ref = 1;
4924 cpi->is_next_src_alt_ref = 0;
4928 cpi->source_buffer_count--;
4931 cpi->un_scaled_source = &s->source_buffer;
4932 cpi->Source = &s->source_buffer;
4933 cpi->source_frame_flags = s->source_frame_flags;
4935 *time_stamp = cpi->source_time_stamp;
4936 *time_end = cpi->source_end_time_stamp;
4943 if (flush && cpi->pass == 1 && !cpi->first_pass_done)
4945 vp8_end_first_pass(cpi); /* get last stats packet */
4946 cpi->first_pass_done = 1;
4962 *frame_flags = cpi->source_frame_flags;
4964 if (cpi->source_time_stamp < cpi->first_time_stamp_ever)
4966 cpi->first_time_stamp_ever = cpi->source_time_stamp;
4967 cpi->last_end_time_stamp_seen = cpi->source_time_stamp;
4973 if (cpi->source_time_stamp == cpi->first_time_stamp_ever)
4975 double this_fps = 10000000.000 / (cpi->source_end_time_stamp - cpi->source_time_stamp);
4977 vp8_new_frame_rate(cpi, this_fps);
4981 long long nanosecs = cpi->source_end_time_stamp
4982 - cpi->last_end_time_stamp_seen;
4987 vp8_new_frame_rate(cpi, (7 * cpi->oxcf.frame_rate + this_fps) / 8);
4992 cpi->last_time_stamp_seen = cpi->source_time_stamp;
4993 cpi->last_end_time_stamp_seen = cpi->source_end_time_stamp;
4996 if (cpi->compressor_speed == 2)
4998 check_gf_quality(cpi);
5030 if (cpi->pass == 1)
5032 Pass1Encode(cpi, size, dest, frame_flags);
5034 else if (cpi->pass == 2)
5036 Pass2Encode(cpi, size, dest, frame_flags);
5040 encode_frame_to_data_rate(cpi, size, dest, frame_flags);
5042 if (cpi->compressor_speed == 2)
5053 if (cpi->avg_encode_time == 0)
5054 cpi->avg_encode_time = duration;
5056 cpi->avg_encode_time = (7 * cpi->avg_encode_time + duration) >> 3;
5064 if (cpi->avg_pick_mode_time == 0)
5065 cpi->avg_pick_mode_time = duration2;
5067 cpi->avg_pick_mode_time = (7 * cpi->avg_pick_mode_time + duration2) >> 3;
5091 cpi->ready_for_new_frame = 1;
5094 cpi->time_compress_data += vpx_usec_timer_elapsed(&cmptimer);
5096 if (cpi->b_calculate_psnr && cpi->pass != 1 && cm->show_frame)
5098 generate_psnr_packet(cpi);
5103 if (cpi->pass != 1)
5105 cpi->bytes += *size;
5110 cpi->count ++;
5112 if (cpi->b_calculate_psnr)
5117 YV12_BUFFER_CONFIG *orig = cpi->Source;
5118 YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
5127 IF_RTCD(&cpi->rtcd.variance));
5131 IF_RTCD(&cpi->rtcd.variance));
5135 IF_RTCD(&cpi->rtcd.variance));
5141 cpi->total_y += vp8_mse2psnr(y_samples, 255.0, ye);
5142 cpi->total_u += vp8_mse2psnr(uv_samples, 255.0, ue);
5143 cpi->total_v += vp8_mse2psnr(uv_samples, 255.0, ve);
5144 cpi->total_sq_error += sq_error;
5145 cpi->total += frame_psnr;
5155 IF_RTCD(&cpi->rtcd.variance));
5159 IF_RTCD(&cpi->rtcd.variance));
5163 IF_RTCD(&cpi->rtcd.variance));
5169 cpi->totalp_y += vp8_mse2psnr(y_samples, 255.0, ye);
5170 cpi->totalp_u += vp8_mse2psnr(uv_samples, 255.0, ue);
5171 cpi->totalp_v += vp8_mse2psnr(uv_samples, 255.0, ve);
5172 cpi->total_sq_error2 += sq_error;
5173 cpi->totalp += frame_psnr2;
5175 frame_ssim2 = vp8_calc_ssim(cpi->Source,
5177 IF_RTCD(&cpi->rtcd.variance));
5179 cpi->summed_quality += frame_ssim2 * weight;
5180 cpi->summed_weights += weight;
5185 if (cpi->b_calculate_ssimg)
5188 frame_all = vp8_calc_ssimg(cpi->Source, cm->frame_to_show, &y, &u, &v);
5189 cpi->total_ssimg_y += y;
5190 cpi->total_ssimg_u += u;
5191 cpi->total_ssimg_v += v;
5192 cpi->total_ssimg_all += frame_all;
5200 if (cpi->common.frame_type != 0 && cpi->common.base_qindex == cpi->oxcf.worst_allowed_q)
5202 skiptruecount += cpi->skip_true_count;
5203 skipfalsecount += cpi->skip_false_count;
5209 if (cpi->pass != 1)
5212 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);
5214 if (cpi->is_src_frame_alt_ref == 1)
5215 fprintf(f, "skipcount: %4d framesize: %d\n", cpi->skip_true_count , *size);
5237 VP8_COMP *cpi = (VP8_COMP *) comp;
5239 if (cpi->common.refresh_alt_ref_frame)
5245 ret = vp8_post_proc_frame(&cpi->common, dest, flags);
5248 if (cpi->common.frame_to_show)
5250 *dest = *cpi->common.frame_to_show;
5251 dest->y_width = cpi->common.Width;
5252 dest->y_height = cpi->common.Height;
5253 dest->uv_height = cpi->common.Height / 2;
5269 VP8_COMP *cpi = (VP8_COMP *) comp;
5272 if (cpi->common.mb_rows != rows || cpi->common.mb_cols != cols)
5277 disable_segmentation((VP8_PTR)cpi);
5282 set_segmentation_map((VP8_PTR)cpi, map);
5285 enable_segmentation((VP8_PTR)cpi);
5299 cpi->segment_encode_breakout[0] = threshold[0];
5300 cpi->segment_encode_breakout[1] = threshold[1];
5301 cpi->segment_encode_breakout[2] = threshold[2];
5302 cpi->segment_encode_breakout[3] = threshold[3];
5306 set_segment_data((VP8_PTR)cpi, &feature_data[0][0], SEGMENT_DELTADATA);
5313 VP8_COMP *cpi = (VP8_COMP *) comp;
5315 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols)
5319 vpx_memcpy(cpi->active_map, map, rows * cols);
5320 cpi->active_map_enabled = 1;
5323 cpi->active_map_enabled = 0;
5329 //cpi->active_map_enabled = 0;
5336 VP8_COMP *cpi = (VP8_COMP *) comp;
5339 cpi->common.horiz_scale = horiz_mode;
5344 cpi->common.vert_scale = vert_mode;
5410 VP8_COMP *cpi = (VP8_COMP *) c;
5411 return cpi->common.base_qindex;