Lines Matching defs:cpi
224 void vp8_save_coding_context(VP8_COMP *cpi)
226 CODING_CONTEXT *const cc = & cpi->coding_context;
233 cc->frames_since_key = cpi->frames_since_key;
234 cc->filter_level = cpi->common.filter_level;
235 cc->frames_till_gf_update_due = cpi->frames_till_gf_update_due;
236 cc->frames_since_golden = cpi->common.frames_since_golden;
238 vp8_copy(cc->mvc, cpi->common.fc.mvc);
239 vp8_copy(cc->mvcosts, cpi->mb.mvcosts);
241 vp8_copy(cc->kf_ymode_prob, cpi->common.kf_ymode_prob);
242 vp8_copy(cc->ymode_prob, cpi->common.fc.ymode_prob);
243 vp8_copy(cc->kf_uv_mode_prob, cpi->common.kf_uv_mode_prob);
244 vp8_copy(cc->uv_mode_prob, cpi->common.fc.uv_mode_prob);
246 vp8_copy(cc->ymode_count, cpi->ymode_count);
247 vp8_copy(cc->uv_mode_count, cpi->uv_mode_count);
260 cc->this_frame_percent_intra = cpi->this_frame_percent_intra;
264 void vp8_restore_coding_context(VP8_COMP *cpi)
266 CODING_CONTEXT *const cc = & cpi->coding_context;
271 cpi->frames_since_key = cc->frames_since_key;
272 cpi->common.filter_level = cc->filter_level;
273 cpi->frames_till_gf_update_due = cc->frames_till_gf_update_due;
274 cpi->common.frames_since_golden = cc->frames_since_golden;
276 vp8_copy(cpi->common.fc.mvc, cc->mvc);
278 vp8_copy(cpi->mb.mvcosts, cc->mvcosts);
280 vp8_copy(cpi->common.kf_ymode_prob, cc->kf_ymode_prob);
281 vp8_copy(cpi->common.fc.ymode_prob, cc->ymode_prob);
282 vp8_copy(cpi->common.kf_uv_mode_prob, cc->kf_uv_mode_prob);
283 vp8_copy(cpi->common.fc.uv_mode_prob, cc->uv_mode_prob);
285 vp8_copy(cpi->ymode_count, cc->ymode_count);
286 vp8_copy(cpi->uv_mode_count, cc->uv_mode_count);
299 cpi->this_frame_percent_intra = cc->this_frame_percent_intra;
303 void vp8_setup_key_frame(VP8_COMP *cpi)
307 vp8_default_coef_probs(& cpi->common);
308 vp8_kf_default_bmode_probs(cpi->common.kf_bmode_prob);
310 vpx_memcpy(cpi->common.fc.mvc, vp8_default_mv_context, sizeof(vp8_default_mv_context));
313 vp8_build_component_cost_table(cpi->mb.mvcost, cpi->mb.mvsadcost, (const MV_CONTEXT *) cpi->common.fc.mvc, flag);
316 vpx_memset(cpi->common.fc.pre_mvc, 0, sizeof(cpi->common.fc.pre_mvc)); //initialize pre_mvc to all zero.
318 //cpi->common.filter_level = 0; // Reset every key frame.
319 cpi->common.filter_level = cpi->common.base_qindex * 3 / 8 ;
322 if (cpi->auto_gold)
323 //cpi->frames_till_gf_update_due = DEFAULT_GF_INTERVAL;
324 cpi->frames_till_gf_update_due = cpi->baseline_gf_interval;
326 cpi->frames_till_gf_update_due = cpi->goldfreq;
328 cpi->common.refresh_golden_frame = TRUE;
329 cpi->common.refresh_alt_ref_frame = TRUE;
332 void vp8_calc_auto_iframe_target_size(VP8_COMP *cpi)
340 if (cpi->oxcf.fixed_q >= 0)
342 vp8_calc_iframe_target_size(cpi);
346 if (cpi->pass == 2)
348 cpi->this_frame_target = cpi->per_frame_bandwidth; // New Two pass RC
353 kf_boost = (int)(2 * cpi->output_frame_rate - 16);
356 kf_boost = kf_boost * kf_boost_qadjustment[cpi->ni_av_qi] / 100;
359 if (cpi->frames_since_key < cpi->output_frame_rate / 2)
360 kf_boost = (int)(kf_boost * cpi->frames_since_key / (cpi->output_frame_rate / 2));
366 cpi->active_worst_quality = cpi->worst_quality;
368 cpi->this_frame_target = ((16 + kf_boost) * cpi->per_frame_bandwidth) >> 4;
373 if (cpi->pass != 2)
376 cpi->source_alt_ref_pending = FALSE;
378 /*if ( cpi->oxcf.fixed_q == -1)
380 if ( cpi->oxcf.play_alternate && ( (cpi->last_boost/2) > (100+(AF_THRESH*cpi->frames_till_gf_update_due)) ) )
381 cpi->source_alt_ref_pending = TRUE;
383 cpi->source_alt_ref_pending = FALSE;
393 // cpi->common.current_video_frame, cpi->target_bandwidth, cpi->frames_to_key, kf_boost_qadjustment[cpi->ni_av_qi], cpi->kf_boost, (cpi->this_frame_target *100 / cpi->per_frame_bandwidth), cpi->this_frame_target );
396 cpi->common.current_video_frame, cpi->gfu_boost, cpi->baseline_gf_interval, cpi->source_alt_ref_pending);
403 static void calc_gf_params(VP8_COMP *cpi)
405 int Q = (cpi->oxcf.fixed_q < 0) ? cpi->last_q[INTER_FRAME] : cpi->oxcf.fixed_q;
409 int tot_mbs = cpi->recent_ref_frame_usage[INTRA_FRAME] +
410 cpi->recent_ref_frame_usage[LAST_FRAME] +
411 cpi->recent_ref_frame_usage[GOLDEN_FRAME] +
412 cpi->recent_ref_frame_usage[ALTREF_FRAME];
414 int pct_gf_active = (100 * cpi->gf_active_count) / (cpi->common.mb_rows * cpi->common.mb_cols);
417 //cpi->last_boost = 100;
420 gf_frame_useage = (cpi->recent_ref_frame_usage[GOLDEN_FRAME] + cpi->recent_ref_frame_usage[ALTREF_FRAME]) * 100 / tot_mbs;
426 if (cpi->pass != 2)
438 int index = cpi->one_pass_frame_index;
439 int frames_to_scan = (cpi->max_gf_interval <= MAX_LAG_BUFFERS) ? cpi->max_gf_interval : MAX_LAG_BUFFERS;
448 cpi->one_pass_frame_index = cpi->common.current_video_frame%MAX_LAG_BUFFERS;
456 if ( cpi->one_pass_frame_stats[index].frame_coded_error > 0.0 )
458 IIRatio = cpi->one_pass_frame_stats[index].frame_intra_error / cpi->one_pass_frame_stats[index].frame_coded_error;
468 decay_val = decay_val * cpi->one_pass_frame_stats[index].frame_pcnt_inter;
479 cpi->baseline_gf_interval = i;
491 Boost = Boost * gf_intra_usage_adjustment[(cpi->this_frame_percent_intra < 15) ? cpi->this_frame_percent_intra : 14] / 100;
499 if (!cpi->sf.recode_loop)
501 if (cpi->compressor_speed == 2)
506 if (Boost > kf_gf_boost_qlimits[Q] && (cpi->pass == 0))
514 cpi->last_boost = Boost;
520 if (cpi->oxcf.fixed_q == -1)
522 if (cpi->pass == 2) // 2 Pass
524 cpi->frames_till_gf_update_due = cpi->baseline_gf_interval;
528 cpi->frames_till_gf_update_due = cpi->baseline_gf_interval;
530 if (cpi->last_boost > 750)
531 cpi->frames_till_gf_update_due++;
533 if (cpi->last_boost > 1000)
534 cpi->frames_till_gf_update_due++;
536 if (cpi->last_boost > 1250)
537 cpi->frames_till_gf_update_due++;
539 if (cpi->last_boost >= 1500)
540 cpi->frames_till_gf_update_due ++;
542 if (gf_interval_table[gf_frame_useage] > cpi->frames_till_gf_update_due)
543 cpi->frames_till_gf_update_due = gf_interval_table[gf_frame_useage];
545 if (cpi->frames_till_gf_update_due > cpi->max_gf_interval)
546 cpi->frames_till_gf_update_due = cpi->max_gf_interval;
550 cpi->frames_till_gf_update_due = cpi->baseline_gf_interval;
553 if (cpi->pass != 2)
556 cpi->source_alt_ref_pending = FALSE;
558 /*if ( cpi->oxcf.fixed_q == -1)
560 if ( cpi->oxcf.play_alternate && (cpi->last_boost > (100 + (AF_THRESH*cpi->frames_till_gf_update_due)) ) )
561 cpi->source_alt_ref_pending = TRUE;
563 cpi->source_alt_ref_pending = FALSE;
582 void vp8_calc_iframe_target_size(VP8_COMP *cpi)
587 Q = (cpi->oxcf.fixed_q >= 0) ? cpi->oxcf.fixed_q : cpi->avg_frame_qindex;
589 if (cpi->auto_adjust_key_quantizer == 1)
600 if (cpi->frames_since_key < 16)
601 Boost = Boost * kf_boost_seperation_adjustment[cpi->frames_since_key] / 100;
613 cpi->last_boost = Boost;
616 if (cpi->pass != 2)
619 cpi->source_alt_ref_pending = FALSE;
621 /*if ( cpi->oxcf.fixed_q == -1)
623 if ( cpi->oxcf.play_alternate && ( (cpi->last_boost/2) > (100+(AF_THRESH*cpi->frames_till_gf_update_due)) ) )
624 cpi->source_alt_ref_pending = TRUE;
626 cpi->source_alt_ref_pending = FALSE;
630 if (cpi->oxcf.fixed_q >= 0)
632 cpi->this_frame_target = (baseline_bits_at_q(0, Q, cpi->common.MBs) * Boost) / 100;
639 if (cpi->oxcf.error_resilient_mode == 1)
641 cpi->this_frame_target = 2 * cpi->av_per_frame_bandwidth;
646 // Be careful of 32-bit OVERFLOW if restructuring the caluclation of cpi->this_frame_target
648 cpi->key_frame_rate_correction_factor * vp8_bits_per_mb[0][Q]);
650 cpi->this_frame_target = (((bits_per_mb_at_this_q * cpi->common.MBs) >> BPER_MB_NORMBITS) * Boost) / 100;
653 if (cpi->pass < 2)
654 cpi->active_worst_quality = cpi->worst_quality;
660 void vp8_calc_pframe_target_size(VP8_COMP *cpi)
666 //min_frame_target = estimate_min_frame_size( cpi );
669 if (cpi->pass == 2)
671 min_frame_target = cpi->min_frame_bandwidth;
673 if (min_frame_target < (cpi->av_per_frame_bandwidth >> 5))
674 min_frame_target = cpi->av_per_frame_bandwidth >> 5;
676 else if (min_frame_target < cpi->per_frame_bandwidth / 4)
677 min_frame_target = cpi->per_frame_bandwidth / 4;
681 if (cpi->common.refresh_alt_ref_frame)
683 if (cpi->pass == 2)
685 cpi->per_frame_bandwidth = cpi->gf_bits; // Per frame bit target for the alt ref frame
686 cpi->this_frame_target = cpi->per_frame_bandwidth;
694 int Q = (cpi->oxcf.fixed_q < 0) ? cpi->last_q[INTER_FRAME] : cpi->oxcf.fixed_q;
698 alt_boost = (cpi->gfu_boost * 3 * GFQ_ADJUSTMENT) / (2 * 100);
699 alt_boost += (cpi->frames_till_gf_update_due * 50);
703 if ( !cpi->source_alt_ref_active )
706 frames_in_section = cpi->frames_till_gf_update_due+1; // Standard frames + GF
720 if ( cpi->kf_overspend_bits > 0 )
722 Adjustment = (cpi->kf_bitrate_adjustment <= cpi->kf_overspend_bits) ? cpi->kf_bitrate_adjustment : cpi->kf_overspend_bits;
724 if ( Adjustment > (cpi->per_frame_bandwidth - min_frame_target) )
725 Adjustment = (cpi->per_frame_bandwidth - min_frame_target);
727 cpi->kf_overspend_bits -= Adjustment;
731 cpi->inter_frame_target = cpi->per_frame_bandwidth - Adjustment;
732 if ( cpi->inter_frame_target < min_frame_target )
733 cpi->inter_frame_target = min_frame_target;
736 cpi->inter_frame_target = cpi->per_frame_bandwidth;
738 bits_in_section = cpi->inter_frame_target * frames_in_section;
742 cpi->this_frame_target = alt_boost * (bits_in_section / allocation_chunks);
744 cpi->this_frame_target = (alt_boost * bits_in_section) / allocation_chunks;
754 if (cpi->pass == 2)
756 cpi->this_frame_target = cpi->per_frame_bandwidth;
763 if (cpi->kf_overspend_bits > 0)
765 Adjustment = (cpi->kf_bitrate_adjustment <= cpi->kf_overspend_bits) ? cpi->kf_bitrate_adjustment : cpi->kf_overspend_bits;
767 if (Adjustment > (cpi->per_frame_bandwidth - min_frame_target))
768 Adjustment = (cpi->per_frame_bandwidth - min_frame_target);
770 cpi->kf_overspend_bits -= Adjustment;
774 cpi->this_frame_target = cpi->per_frame_bandwidth - Adjustment;
776 if (cpi->this_frame_target < min_frame_target)
777 cpi->this_frame_target = min_frame_target;
780 cpi->this_frame_target = cpi->per_frame_bandwidth;
783 if ((cpi->gf_overspend_bits > 0) && (cpi->this_frame_target > min_frame_target))
785 int Adjustment = (cpi->non_gf_bitrate_adjustment <= cpi->gf_overspend_bits) ? cpi->non_gf_bitrate_adjustment : cpi->gf_overspend_bits;
787 if (Adjustment > (cpi->this_frame_target - min_frame_target))
788 Adjustment = (cpi->this_frame_target - min_frame_target);
790 cpi->gf_overspend_bits -= Adjustment;
791 cpi->this_frame_target -= Adjustment;
795 if ((cpi->last_boost > 150) && (cpi->frames_till_gf_update_due > 0) &&
796 (cpi->current_gf_interval >= (MIN_GF_INTERVAL << 1)))
799 Adjustment = (cpi->last_boost - 100) >> 5;
807 Adjustment = (cpi->this_frame_target * Adjustment) / 100;
809 if (Adjustment > (cpi->this_frame_target - min_frame_target))
810 Adjustment = (cpi->this_frame_target - min_frame_target);
812 if (cpi->common.frames_since_golden == (cpi->current_gf_interval >> 1))
813 cpi->this_frame_target += ((cpi->current_gf_interval - 1) * Adjustment);
815 cpi->this_frame_target -= Adjustment;
822 if ((cpi->oxcf.under_shoot_pct > 0) && (cpi->oxcf.under_shoot_pct <= 100))
823 cpi->this_frame_target = (cpi->this_frame_target * cpi->oxcf.under_shoot_pct) / 100;
830 if (cpi->this_frame_target < min_frame_target)
831 cpi->this_frame_target = min_frame_target;
833 if (!cpi->common.refresh_alt_ref_frame)
835 cpi->inter_frame_target = cpi->this_frame_target;
838 if (cpi->pass == 0)
841 if (cpi->buffered_mode)
843 int one_percent_bits = 1 + cpi->oxcf.optimal_buffer_level / 100;
845 if ((cpi->buffer_level < cpi->oxcf.optimal_buffer_level) ||
846 (cpi->bits_off_target < cpi->oxcf.optimal_buffer_level))
855 if ((cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) &&
856 (cpi->buffer_level < cpi->oxcf.optimal_buffer_level))
859 (cpi->oxcf.optimal_buffer_level - cpi->buffer_level) /
868 else if (cpi->bits_off_target < 0)
871 percent_low = (int)(100 * -cpi->bits_off_target /
872 (cpi->total_byte_count * 8));
881 cpi->this_frame_target =
882 (cpi->this_frame_target * (100 - (percent_low / 2))) / 100;
886 if (cpi->auto_worst_q)
891 // cpi->buffer_level as this takes into account the
895 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
897 // Take the smaller of cpi->buffer_level and
898 // cpi->bits_off_target
900 (cpi->buffer_level < cpi->bits_off_target)
901 ? cpi->buffer_level : cpi->bits_off_target;
909 critical_buffer_level = cpi->bits_off_target;
914 if (critical_buffer_level < cpi->oxcf.optimal_buffer_level)
917 (cpi->oxcf.optimal_buffer_level >> 2) )
920 cpi->worst_quality - cpi->ni_av_qi;
923 (cpi->oxcf.optimal_buffer_level >> 2));
926 // cpi->ni_av_qi when (critical_buffer_level ==
927 // cpi->optimal_buffer_level) to
928 // cpi->worst_quality when
930 // cpi->optimal_buffer_level >> 2)
931 cpi->active_worst_quality =
932 cpi->worst_quality -
934 (cpi->oxcf.optimal_buffer_level*3>>2));
938 cpi->active_worst_quality = cpi->worst_quality;
943 cpi->active_worst_quality = cpi->ni_av_qi;
948 cpi->active_worst_quality = cpi->worst_quality;
955 if (cpi->bits_off_target > cpi->oxcf.optimal_buffer_level)
957 percent_high = (int)(100 * (cpi->bits_off_target - cpi->oxcf.optimal_buffer_level) / (cpi->total_byte_count * 8));
964 cpi->this_frame_target = (cpi->this_frame_target * (100 + (percent_high / 2))) / 100;
969 if (cpi->auto_worst_q)
972 cpi->active_worst_quality = cpi->ni_av_qi;
976 cpi->active_worst_quality = cpi->worst_quality;
981 cpi->active_best_quality = cpi->best_quality;
984 if (cpi->active_worst_quality <= cpi->active_best_quality)
985 cpi->active_worst_quality = cpi->active_best_quality + 1;
992 cpi->active_worst_quality = cpi->worst_quality;
998 if ( cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY &&
999 cpi->active_worst_quality < cpi->cq_target_quality)
1001 cpi->active_worst_quality = cpi->cq_target_quality;
1010 if (cpi->drop_frames_allowed && cpi->buffered_mode &&
1011 (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) &&
1012 ((cpi->common.frame_type != KEY_FRAME))) //|| !cpi->oxcf.allow_spatial_resampling) )
1015 if ((cpi->buffer_level < 0))
1020 (int) cpi->common.current_video_frame,
1021 cpi->decimation_factor, cpi->common.horiz_scale,
1022 (cpi->buffer_level * 100) / cpi->oxcf.optimal_buffer_level);
1025 //vpx_log("Decoder: Drop frame due to bandwidth: %d \n",cpi->buffer_level, cpi->av_per_frame_bandwidth);
1027 cpi->drop_frame = TRUE;
1032 else if ((cpi->buffer_level < cpi->oxcf.drop_frames_water_mark * cpi->oxcf.optimal_buffer_level / 100) &&
1033 (cpi->drop_count < cpi->max_drop_count) && (cpi->pass == 0))
1035 cpi->drop_frame = TRUE;
1040 if (cpi->drop_frame)
1043 cpi->bits_off_target += cpi->av_per_frame_bandwidth;
1044 cpi->buffer_level = cpi->bits_off_target;
1047 cpi->drop_count = 0;
1051 if (cpi->oxcf.error_resilient_mode == 0 &&
1052 (cpi->frames_till_gf_update_due == 0) && !cpi->drop_frame)
1055 int Q = (cpi->oxcf.fixed_q < 0) ? cpi->last_q[INTER_FRAME] : cpi->oxcf.fixed_q;
1058 int tot_mbs = cpi->recent_ref_frame_usage[INTRA_FRAME] +
1059 cpi->recent_ref_frame_usage[LAST_FRAME] +
1060 cpi->recent_ref_frame_usage[GOLDEN_FRAME] +
1061 cpi->recent_ref_frame_usage[ALTREF_FRAME];
1063 int pct_gf_active = (100 * cpi->gf_active_count) / (cpi->common.mb_rows * cpi->common.mb_cols);
1066 //cpi->last_boost = 100;
1069 gf_frame_useage = (cpi->recent_ref_frame_usage[GOLDEN_FRAME] + cpi->recent_ref_frame_usage[ALTREF_FRAME]) * 100 / tot_mbs;
1075 if (cpi->auto_gold)
1078 if ((cpi->pass == 0) && (cpi->this_frame_percent_intra < 15 || gf_frame_useage >= 5))
1079 cpi->common.refresh_golden_frame = TRUE;
1082 else if (cpi->pass == 2)
1083 cpi->common.refresh_golden_frame = TRUE;
1095 cpi->common.current_video_frame, cpi->gfu_boost, GFQ_ADJUSTMENT, cpi->gfu_boost, gf_frame_useage);
1101 if (cpi->common.refresh_golden_frame == TRUE)
1110 fprintf(f, "%8ld GF coded\n", cpi->common.current_video_frame);
1115 cpi->initial_gf_use = 0;
1117 if (cpi->auto_adjust_gold_quantizer)
1119 calc_gf_params(cpi);
1125 if (!cpi->source_alt_ref_active)
1127 if (cpi->oxcf.fixed_q < 0)
1129 if (cpi->pass == 2)
1131 cpi->this_frame_target = cpi->per_frame_bandwidth; // The spend on the GF is defined in the two pass code for two pass encodes
1135 int Boost = cpi->last_boost;
1136 int frames_in_section = cpi->frames_till_gf_update_due + 1;
1138 int bits_in_section = cpi->inter_frame_target * frames_in_section;
1149 cpi->this_frame_target = Boost * (bits_in_section / allocation_chunks);
1151 cpi->this_frame_target = (Boost * bits_in_section) / allocation_chunks;
1155 cpi->this_frame_target = (baseline_bits_at_q(1, Q, cpi->common.MBs) * cpi->last_boost) / 100;
1164 cpi->this_frame_target = 0;
1167 cpi->current_gf_interval = cpi->frames_till_gf_update_due;
1174 void vp8_update_rate_correction_factors(VP8_COMP *cpi, int damp_var)
1176 int Q = cpi->common.base_qindex;
1186 if (cpi->common.frame_type == KEY_FRAME)
1188 rate_correction_factor = cpi->key_frame_rate_correction_factor;
1192 if (cpi->common.refresh_alt_ref_frame || cpi->common.refresh_golden_frame)
1193 rate_correction_factor = cpi->gf_rate_correction_factor;
1195 rate_correction_factor = cpi->rate_correction_factor;
1200 //projected_size_based_on_q = ((int)(.5 + rate_correction_factor * vp8_bits_per_mb[cpi->common.frame_type][Q]) * cpi->common.MBs) >> BPER_MB_NORMBITS;
1201 projected_size_based_on_q = (int)(((.5 + rate_correction_factor * vp8_bits_per_mb[cpi->common.frame_type][Q]) * cpi->common.MBs) / (1 << BPER_MB_NORMBITS));
1203 // Make some allowance for cpi->zbin_over_quant
1204 if (cpi->zbin_over_quant > 0)
1206 int Z = cpi->zbin_over_quant;
1223 //if ( cpi->this_frame_target > 0 )
1224 // correction_factor = (100 * cpi->projected_frame_size) / cpi->this_frame_target;
1226 correction_factor = (100 * cpi->projected_frame_size) / projected_size_based_on_q;
1243 //if ( (correction_factor > 102) && (Q < cpi->active_worst_quality) )
1254 //else if ( (correction_factor < 99) && (Q > cpi->active_best_quality) )
1266 if (cpi->common.frame_type == KEY_FRAME)
1267 cpi->key_frame_rate_correction_factor = rate_correction_factor;
1270 if (cpi->common.refresh_alt_ref_frame || cpi->common.refresh_golden_frame)
1271 cpi->gf_rate_correction_factor = rate_correction_factor;
1273 cpi->rate_correction_factor = rate_correction_factor;
1277 static int estimate_bits_at_q(VP8_COMP *cpi, int Q)
1279 int Bpm = (int)(.5 + cpi->rate_correction_factor * vp8_bits_per_mb[INTER_FRAME][Q]);
1285 if (cpi->common.MBs > (1 << 11))
1286 return (Bpm >> BPER_MB_NORMBITS) * cpi->common.MBs;
1288 return (Bpm * cpi->common.MBs) >> BPER_MB_NORMBITS;
1293 int vp8_regulate_q(VP8_COMP *cpi, int target_bits_per_frame)
1295 int Q = cpi->active_worst_quality;
1298 cpi->zbin_over_quant = 0;
1300 if (cpi->oxcf.fixed_q >= 0)
1302 Q = cpi->oxcf.fixed_q;
1304 if (cpi->common.frame_type == KEY_FRAME)
1306 Q = cpi->oxcf.key_q;
1308 else if (cpi->common.refresh_alt_ref_frame)
1310 Q = cpi->oxcf.alt_q;
1312 else if (cpi->common.refresh_golden_frame)
1314 Q = cpi->oxcf.gold_q;
1327 if (cpi->common.frame_type == KEY_FRAME)
1328 correction_factor = cpi->key_frame_rate_correction_factor;
1331 if (cpi->common.refresh_alt_ref_frame || cpi->common.refresh_golden_frame)
1332 correction_factor = cpi->gf_rate_correction_factor;
1334 correction_factor = cpi->rate_correction_factor;
1339 target_bits_per_mb = (target_bits_per_frame / cpi->common.MBs) << BPER_MB_NORMBITS; // Case where we would overflow int
1341 target_bits_per_mb = (target_bits_per_frame << BPER_MB_NORMBITS) / cpi->common.MBs;
1343 i = cpi->active_best_quality;
1347 bits_per_mb_at_this_q = (int)(.5 + correction_factor * vp8_bits_per_mb[cpi->common.frame_type][i]);
1361 while (++i <= cpi->active_worst_quality);
1373 if (cpi->common.frame_type == KEY_FRAME)
1375 else if (cpi->common.refresh_alt_ref_frame || (cpi->common.refresh_golden_frame && !cpi->source_alt_ref_active))
1391 cpi->zbin_over_quant = (int)Oq;
1398 while (cpi->zbin_over_quant < zbin_oqmax)
1400 cpi->zbin_over_quant ++;
1402 if (cpi->zbin_over_quant > zbin_oqmax)
1403 cpi->zbin_over_quant = zbin_oqmax;
1422 static int estimate_min_frame_size(VP8_COMP *cpi)
1428 if (cpi->common.current_video_frame < 10)
1430 if (cpi->pass == 2)
1431 return (cpi->min_frame_bandwidth);
1433 return cpi->per_frame_bandwidth / 3;
1437 if ( cpi->common.frame_type == KEY_FRAME )
1438 correction_factor = cpi->key_frame_rate_correction_factor;
1441 if ( cpi->common.refresh_alt_ref_frame || cpi->common.refresh_golden_frame )
1442 correction_factor = cpi->gf_rate_correction_factor;
1444 correction_factor = cpi->rate_correction_factor;
1448 correction_factor = cpi->rate_correction_factor / 2.0;
1450 bits_per_mb_at_max_q = (int)(.5 + correction_factor * vp8_bits_per_mb[cpi->common.frame_type][MAXQ]);
1452 return (bits_per_mb_at_max_q * cpi->common.MBs) >> BPER_MB_NORMBITS;
1455 void vp8_adjust_key_frame_context(VP8_COMP *cpi)
1465 unsigned int output_frame_rate = (unsigned int)(100 * cpi->output_frame_rate);
1466 unsigned int target_bandwidth = (unsigned int)(100 * cpi->target_bandwidth);
1472 cpi->tot_key_frame_bits += cpi->projected_frame_size;
1475 if (cpi->key_frame_count == 1)
1477 av_key_frame_frequency = (int)cpi->output_frame_rate * 2; // Assume a default of 1 kf every 2 seconds
1478 av_key_frame_bits = cpi->projected_frame_size;
1479 av_key_frames_per_second = output_frame_rate / av_key_frame_frequency; // Note output_frame_rate not cpi->output_frame_rate
1484 (cpi->frames_since_key > 0) ? cpi->frames_since_key : 1;
1491 cpi->prior_key_frame_size[i] = cpi->prior_key_frame_size[i+1];
1492 cpi->prior_key_frame_distance[i] = cpi->prior_key_frame_distance[i+1];
1496 cpi->prior_key_frame_size[i] = cpi->projected_frame_size;
1497 cpi->prior_key_frame_distance[i] = last_kf_interval;
1500 av_key_frame_bits += prior_key_frame_weight[i] * cpi->prior_key_frame_size[i];
1501 av_key_frame_frequency += prior_key_frame_weight[i] * cpi->prior_key_frame_distance[i];
1512 if ((cpi->pass != 2) && (cpi->projected_frame_size > cpi->per_frame_bandwidth))
1518 cpi->kf_overspend_bits += (cpi->projected_frame_size - cpi->per_frame_bandwidth) * 7 / 8;
1519 cpi->gf_overspend_bits += (cpi->projected_frame_size - cpi->per_frame_bandwidth) * 1 / 8;
1526 if (cpi->pass == 2)
1528 if (cpi->frames_to_key > 16)
1529 cpi->kf_bitrate_adjustment = cpi->kf_overspend_bits / (int)cpi->frames_to_key;
1531 cpi->kf_bitrate_adjustment = cpi->kf_overspend_bits / 16;
1534 cpi->kf_bitrate_adjustment = cpi->kf_overspend_bits / (int)av_key_frame_frequency;
1537 cpi->frames_since_key = 0;
1538 cpi->last_key_frame_size = cpi->projected_frame_size;
1539 cpi->key_frame_count++;
1542 void vp8_compute_frame_size_bounds(VP8_COMP *cpi, int *frame_under_shoot_limit, int *frame_over_shoot_limit)
1545 if (cpi->oxcf.fixed_q >= 0)
1553 if (cpi->common.frame_type == KEY_FRAME)
1555 *frame_over_shoot_limit = cpi->this_frame_target * 9 / 8;
1556 *frame_under_shoot_limit = cpi->this_frame_target * 7 / 8;
1560 if (cpi->common.refresh_alt_ref_frame || cpi->common.refresh_golden_frame)
1562 *frame_over_shoot_limit = cpi->this_frame_target * 9 / 8;
1563 *frame_under_shoot_limit = cpi->this_frame_target * 7 / 8;
1568 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
1570 if (cpi->buffer_level >= ((cpi->oxcf.optimal_buffer_level + cpi->oxcf.maximum_buffer_size) >> 1))
1573 *frame_over_shoot_limit = cpi->this_frame_target * 12 / 8;
1574 *frame_under_shoot_limit = cpi->this_frame_target * 6 / 8;
1576 else if (cpi->buffer_level <= (cpi->oxcf.optimal_buffer_level >> 1))
1579 *frame_over_shoot_limit = cpi->this_frame_target * 10 / 8;
1580 *frame_under_shoot_limit = cpi->this_frame_target * 4 / 8;
1584 *frame_over_shoot_limit = cpi->this_frame_target * 11 / 8;
1585 *frame_under_shoot_limit = cpi->this_frame_target * 5 / 8;
1593 if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY)
1595 *frame_over_shoot_limit = cpi->this_frame_target * 11 / 8;
1596 *frame_under_shoot_limit = cpi->this_frame_target * 2 / 8;
1600 *frame_over_shoot_limit = cpi->this_frame_target * 11 / 8;
1601 *frame_under_shoot_limit = cpi->this_frame_target * 5 / 8;