Lines Matching defs:cpi

36 extern void vp8cx_frame_init_quantizer(VP8_COMP *cpi);
38 extern void vp8_alloc_compressor_data(VP8_COMP *cpi);
56 #define POW1 (double)cpi->oxcf.two_pass_vbrbias/100.0
57 #define POW2 (double)cpi->oxcf.two_pass_vbrbias/100.0
77 static void find_next_key_frame(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame);
82 static void reset_fpf_position(VP8_COMP *cpi, FIRSTPASS_STATS *Position)
84 cpi->twopass.stats_in = Position;
87 static int lookup_next_frame_stats(VP8_COMP *cpi, FIRSTPASS_STATS *next_frame)
89 if (cpi->twopass.stats_in >= cpi->twopass.stats_in_end)
92 *next_frame = *cpi->twopass.stats_in;
97 static int read_frame_stats( VP8_COMP *cpi,
101 FIRSTPASS_STATS * fps_ptr = cpi->twopass.stats_in;
106 if ( &fps_ptr[offset] >= cpi->twopass.stats_in_end )
111 if ( &fps_ptr[offset] < cpi->twopass.stats_in_start )
119 static int input_stats(VP8_COMP *cpi, FIRSTPASS_STATS *fps)
121 if (cpi->twopass.stats_in >= cpi->twopass.stats_in_end)
124 *fps = *cpi->twopass.stats_in;
125 cpi->twopass.stats_in =
126 (void*)((char *)cpi->twopass.stats_in + sizeof(FIRSTPASS_STATS));
130 static void output_stats(const VP8_COMP *cpi,
264 static double calculate_modified_err(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame)
266 double av_err = ( cpi->twopass.total_stats.ssim_weighted_pred_err /
267 cpi->twopass.total_stats.count );
344 static int frame_max_bits(VP8_COMP *cpi)
355 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
357 double buffer_fullness_ratio = (double)cpi->buffer_level / DOUBLE_DIVIDE_CHECK((double)cpi->oxcf.optimal_buffer_level);
362 max_bits = (int)(cpi->av_per_frame_bandwidth * ((double)cpi->oxcf.two_pass_vbrmax_section / 100.0));
367 /* The lower of max_bits / 4 or cpi->av_per_frame_bandwidth / 4. */
368 int min_max_bits = ((cpi->av_per_frame_bandwidth >> 2) < (max_bits >> 2)) ? cpi->av_per_frame_bandwidth >> 2 : max_bits >> 2;
385 max_bits = (int)(((double)cpi->twopass.bits_left / (cpi->twopass.total_stats.count - (double)cpi->common.current_video_frame)) * ((double)cpi->oxcf.two_pass_vbrmax_section / 100.0));
395 void vp8_init_first_pass(VP8_COMP *cpi)
397 zero_stats(&cpi->twopass.total_stats);
400 void vp8_end_first_pass(VP8_COMP *cpi)
402 output_stats(cpi, cpi->output_pkt_list, &cpi->twopass.total_stats);
405 static void zz_motion_search( VP8_COMP *cpi, MACROBLOCK * x,
435 static void first_pass_motion_search(VP8_COMP *cpi, MACROBLOCK *x,
452 vp8_variance_fn_ptr_t v_fn_ptr = cpi->fn_ptr[BLOCK_16X16];
465 tmp_err = cpi->diamond_search_sad(x, b, d, &ref_mv_full, &tmp_mv, step_param,
490 tmp_err = cpi->diamond_search_sad(x, b, d, &ref_mv_full, &tmp_mv,
507 void vp8_first_pass(VP8_COMP *cpi)
510 MACROBLOCK *const x = & cpi->mb;
511 VP8_COMMON *const cm = & cpi->common;
541 x->src = * cpi->Source;
568 vp8cx_frame_init_quantizer(cpi);
573 vp8_initialize_rd_consts(cpi, x, vp8_dc_quant(cm->base_qindex, cm->y1dc_delta_q));
575 vp8_build_component_cost_table(cpi->mb.mvcost, (const MV_CONTEXT *) cm->fc.mvc, flag);
613 this_error = vp8_encode_intra(cpi, x, use_dc_pred);
645 zz_motion_search( cpi, x, cpi->last_frame_unscaled_source,
651 if (raw_motion_error < cpi->oxcf.encode_breakout)
657 first_pass_motion_search(cpi, x, &best_ref_mv,
667 first_pass_motion_search(cpi, x, &zero_ref_mv, &tmp_mv,
683 first_pass_motion_search(cpi, x, &zero_ref_mv, &tmp_mv, gld_yv12, &gf_motion_error, recon_yoffset);
804 weight = simple_weight(cpi->Source);
839 fps.pcnt_motion = 1.0 * (double)mvcount / cpi->common.MBs;
843 * than the full time between subsequent cpi->source_time_stamps
845 fps.duration = (double)(cpi->source->ts_end
846 - cpi->source->ts_start);
849 memcpy(&cpi->twopass.this_frame_stats,
852 output_stats(cpi, cpi->output_pkt_list, &cpi->twopass.this_frame_stats);
853 accumulate_stats(&cpi->twopass.total_stats, &fps);
860 (cpi->twopass.this_frame_stats.pcnt_inter > 0.20) &&
861 ((cpi->twopass.this_frame_stats.intra_error /
862 DOUBLE_DIVIDE_CHECK(cpi->twopass.this_frame_stats.coded_error)) >
917 static int64_t estimate_modemvcost(VP8_COMP *cpi,
945 (av_intra * intra_cost)) * cpi->common.MBs) * 512;
978 static int estimate_max_q(VP8_COMP *cpi,
984 int num_mbs = cpi->common.MBs;
994 return cpi->twopass.maxq_max_limit; /* Highest value allowed */
1004 if ((cpi->rolling_target_bits > 0) &&
1005 (cpi->active_worst_quality < cpi->worst_quality))
1009 rolling_ratio = (double)cpi->rolling_actual_bits /
1010 (double)cpi->rolling_target_bits;
1013 cpi->twopass.est_max_qcorrection_factor -= 0.005;
1015 cpi->twopass.est_max_qcorrection_factor += 0.005;
1017 cpi->twopass.est_max_qcorrection_factor =
1018 (cpi->twopass.est_max_qcorrection_factor < 0.1)
1020 : (cpi->twopass.est_max_qcorrection_factor > 10.0)
1021 ? 10.0 : cpi->twopass.est_max_qcorrection_factor;
1027 if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1))
1029 if (cpi->oxcf.cpu_used <= 5)
1030 speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04);
1039 pow( 0.98, (double)cpi->twopass.maxq_min_limit ));
1044 for (Q = cpi->twopass.maxq_min_limit; Q < cpi->twopass.maxq_max_limit; Q++)
1056 * speed_correction * cpi->twopass.est_max_qcorrection_factor
1057 * cpi->twopass.section_max_qfactor
1071 if ( (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) &&
1072 (Q < cpi->cq_target_quality) )
1074 Q = cpi->cq_target_quality;
1081 if ( (cpi->ni_frames >
1082 ((int)cpi->twopass.total_stats.count >> 8)) &&
1083 (cpi->ni_frames > 150) )
1085 cpi->twopass.maxq_max_limit = ((cpi->ni_av_qi + 32) < cpi->worst_quality)
1086 ? (cpi->ni_av_qi + 32) : cpi->worst_quality;
1087 cpi->twopass.maxq_min_limit = ((cpi->ni_av_qi - 32) > cpi->best_quality)
1088 ? (cpi->ni_av_qi - 32) : cpi->best_quality;
1097 static int estimate_cq( VP8_COMP *cpi,
1103 int num_mbs = cpi->common.MBs;
1131 if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1))
1133 if (cpi->oxcf.cpu_used <= 5)
1134 speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04);
1140 clip_iiratio = cpi->twopass.total_stats.intra_error /
1141 DOUBLE_DIVIDE_CHECK(cpi->twopass.total_stats.coded_error);
1176 if ( Q >= cpi->worst_quality )
1177 Q = cpi->worst_quality - 1;
1178 if ( Q < cpi->best_quality )
1179 Q = cpi->best_quality;
1184 static int estimate_q(VP8_COMP *cpi, double section_err, int section_target_bandwitdh)
1187 int num_mbs = cpi->common.MBs;
1199 if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1))
1201 if (cpi->oxcf.cpu_used <= 5)
1202 speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04);
1219 cpi->twopass.est_max_qcorrection_factor *
1230 static int estimate_kf_group_q(VP8_COMP *cpi, double section_err, int section_target_bandwitdh, double group_iiratio)
1233 int num_mbs = cpi->common.MBs;
1257 if (cpi->long_rolling_target_bits <= 0)
1261 current_spend_ratio = (double)cpi->long_rolling_actual_bits / (double)cpi->long_rolling_target_bits;
1278 if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1))
1280 if (cpi->oxcf.cpu_used <= 5)
1281 speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04);
1320 fprintf(f, "%8d %8d %8d %8.2f %8.3f %8.2f %8.3f %8.3f %8.3f %8d\n", cpi->common.current_video_frame, bits_per_mb_at_this_q,
1323 (double)cpi->buffer_level / (double)cpi->oxcf.optimal_buffer_level, Q);
1330 extern void vp8_new_framerate(VP8_COMP *cpi, double framerate);
1332 void vp8_init_second_pass(VP8_COMP *cpi)
1337 double two_pass_min_rate = (double)(cpi->oxcf.target_bandwidth * cpi->oxcf.two_pass_vbrmin_section / 100);
1339 zero_stats(&cpi->twopass.total_stats);
1340 zero_stats(&cpi->twopass.total_left_stats);
1342 if (!cpi->twopass.stats_in_end)
1345 cpi->twopass.total_stats = *cpi->twopass.stats_in_end;
1346 cpi->twopass.total_left_stats = cpi->twopass.total_stats;
1354 vp8_new_framerate(cpi, 10000000.0 * cpi->twopass.total_stats.count / cpi->twopass.total_stats.duration);
1356 cpi->output_framerate = cpi->framerate;
1357 cpi->twopass.bits_left = (int64_t)(cpi->twopass.total_stats.duration * cpi->oxcf.target_bandwidth / 10000000.0) ;
1358 cpi->twopass.bits_left -= (int64_t)(cpi->twopass.total_stats.duration * two_pass_min_rate / 10000000.0);
1365 cpi->twopass.kf_intra_err_min = KF_MB_INTRA_MIN * cpi->common.MBs;
1366 cpi->twopass.gf_intra_err_min = GF_MB_INTRA_MIN * cpi->common.MBs;
1375 start_pos = cpi->twopass.stats_in; /* Note starting "file" position */
1377 while (input_stats(cpi, &this_frame) != EOF)
1384 cpi->twopass.avg_iiratio = sum_iiratio / DOUBLE_DIVIDE_CHECK((double)cpi->twopass.total_stats.count);
1387 reset_fpf_position(cpi, start_pos);
1394 start_pos = cpi->twopass.stats_in; /* Note starting "file" position */
1396 cpi->twopass.modified_error_total = 0.0;
1397 cpi->twopass.modified_error_used = 0.0;
1399 while (input_stats(cpi, &this_frame) != EOF)
1401 cpi->twopass.modified_error_total += calculate_modified_err(cpi, &this_frame);
1403 cpi->twopass.modified_error_left = cpi->twopass.modified_error_total;
1405 reset_fpf_position(cpi, start_pos); /* Reset file position */
1410 void vp8_end_second_pass(VP8_COMP *cpi)
1417 static double get_prediction_decay_rate(VP8_COMP *cpi, FIRSTPASS_STATS *next_frame)
1456 VP8_COMP *cpi,
1473 FIRSTPASS_STATS * position = cpi->twopass.stats_in;
1480 if (EOF == input_stats(cpi, &tmp_next_frame))
1483 decay_rate = get_prediction_decay_rate(cpi, &tmp_next_frame);
1488 reset_fpf_position(cpi, position);
1502 static int detect_flash( VP8_COMP *cpi, int offset )
1510 if ( read_frame_stats(cpi, &next_frame, offset) != EOF )
1540 VP8_COMP *cpi,
1586 VP8_COMP *cpi,
1593 if (this_frame->intra_error > cpi->twopass.gf_intra_err_min)
1597 frame_boost = (IIFACTOR * cpi->twopass.gf_intra_err_min /
1620 VP8_COMP *cpi,
1642 if ( read_frame_stats(cpi, &this_frame, (i+offset)) == EOF )
1646 accumulate_frame_motion_stats( cpi, &this_frame,
1651 r = calc_frame_boost( cpi, &this_frame, this_frame_mv_in_out );
1656 flash_detected = detect_flash(cpi, (i+offset)) ||
1657 detect_flash(cpi, (i+offset+1));
1664 get_prediction_decay_rate(cpi, &this_frame);
1693 if ( read_frame_stats(cpi, &this_frame, (i+offset)) == EOF )
1697 accumulate_frame_motion_stats( cpi, &this_frame,
1702 r = calc_frame_boost( cpi, &this_frame, this_frame_mv_in_out );
1707 flash_detected = detect_flash(cpi, (i+offset)) ||
1708 detect_flash(cpi, (i+offset+1));
1715 get_prediction_decay_rate(cpi, &this_frame);
1738 static void define_gf_group(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame)
1760 int max_bits = frame_max_bits(cpi); /* Max for a single frame */
1763 cpi->oxcf.play_alternate && cpi->oxcf.lag_in_frames;
1770 cpi->twopass.gf_group_bits = 0;
1771 cpi->twopass.gf_decay_rate = 0;
1775 start_pos = cpi->twopass.stats_in;
1780 mod_frame_err = calculate_modified_err(cpi, this_frame);
1791 if (cpi->common.frame_type == KEY_FRAME)
1800 while (((i < cpi->twopass.static_scene_max_gf_interval) ||
1801 ((cpi->twopass.frames_to_key - i) < MIN_GF_INTERVAL)) &&
1802 (i < cpi->twopass.frames_to_key))
1807 mod_frame_err = calculate_modified_err(cpi, this_frame);
1812 mod_frame_err / DOUBLE_DIVIDE_CHECK((double)cpi->common.MBs);
1814 if (EOF == input_stats(cpi, &next_frame))
1820 flash_detected = detect_flash(cpi, 0);
1823 accumulate_frame_motion_stats( cpi, &next_frame,
1828 r = calc_frame_boost( cpi, &next_frame, this_frame_mv_in_out );
1833 loop_decay_rate = get_prediction_decay_rate(cpi, &next_frame);
1843 if ( detect_transition_to_still( cpi, i, 5,
1854 /* Break at cpi->max_gf_interval unless almost totally static */
1855 (i >= cpi->max_gf_interval && (decay_accumulator < 0.995)) ||
1860 ((cpi->twopass.frames_to_key - i) >= MIN_GF_INTERVAL) &&
1878 cpi->twopass.gf_decay_rate =
1882 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
1887 if (cpi->drop_frames_allowed)
1889 int64_t df_buffer_level = cpi->oxcf.drop_frames_water_mark *
1890 (cpi->oxcf.optimal_buffer_level / 100);
1892 if (cpi->buffer_level > df_buffer_level)
1893 max_boost = ((double)((cpi->buffer_level - df_buffer_level) * 2 / 3) * 16.0) / DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth);
1897 else if (cpi->buffer_level > 0)
1899 max_boost = ((double)(cpi->buffer_level * 2 / 3) * 16.0) / DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth);
1911 if ((cpi->twopass.frames_to_key - i) < MIN_GF_INTERVAL)
1913 while (i < cpi->twopass.frames_to_key)
1917 if (EOF == input_stats(cpi, this_frame))
1920 if (i < cpi->twopass.frames_to_key)
1922 mod_frame_err = calculate_modified_err(cpi, this_frame);
1928 cpi->gfu_boost = (int)(boost_score * 100.0) >> 4;
1932 alt_boost = calc_arf_boost( cpi, 0, (i-1), (i-1), &f_boost, &b_boost );
1939 (i <= (cpi->twopass.frames_to_key - MIN_GF_INTERVAL)) &&
1951 (cpi->gfu_boost > 100) &&
1952 (cpi->twopass.gf_decay_rate <=
1953 (ARF_DECAY_THRESH + (cpi->gfu_boost / 200))) )
1958 int Q = (cpi->oxcf.fixed_q < 0)
1959 ? cpi->last_q[INTER_FRAME] : cpi->oxcf.fixed_q;
1965 cpi->gfu_boost = alt_boost;
1969 if ((cpi->twopass.kf_group_bits > 0) &&
1970 (cpi->twopass.kf_group_error_left > 0))
1972 group_bits = (int)((double)cpi->twopass.kf_group_bits *
1973 (gf_group_err / (double)cpi->twopass.kf_group_error_left));
1982 Boost = (cpi->gfu_boost * 3 * GFQ_ADJUSTMENT) / (2 * 100);
1987 if (Boost > ((cpi->baseline_gf_interval + 1) * 200))
1988 Boost = ((cpi->baseline_gf_interval + 1) * 200);
2010 tmp_q = estimate_q(cpi, mod_frame_err, (int)arf_frame_bits);
2015 if (tmp_q < cpi->worst_quality)
2019 int frames_bwd = cpi->oxcf.arnr_max_frames - 1;
2020 int frames_fwd = cpi->oxcf.arnr_max_frames - 1;
2022 cpi->source_alt_ref_pending = 1;
2041 cpi->baseline_gf_interval = i;
2051 half_gf_int = cpi->baseline_gf_interval >> 1;
2052 frames_after_arf = (int)(cpi->twopass.total_stats.count -
2055 switch (cpi->oxcf.arnr_type)
2085 frames_bwd += (cpi->oxcf.arnr_max_frames+1) & 0x1;
2089 cpi->active_arnr_frames = frames_bwd + 1 + frames_fwd;
2093 cpi->source_alt_ref_pending = 0;
2094 cpi->baseline_gf_interval = i;
2099 cpi->source_alt_ref_pending = 0;
2100 cpi->baseline_gf_interval = i;
2107 * where cpi->twopass.kf_group_bits is tied to cpi->twopass.bits_left.
2111 if (cpi->twopass.frames_to_key >= (int)(cpi->twopass.total_stats.count -
2112 cpi->common.current_video_frame))
2114 cpi->twopass.kf_group_bits =
2115 (cpi->twopass.bits_left > 0) ? cpi->twopass.bits_left : 0;
2119 if ((cpi->twopass.kf_group_bits > 0) &&
2120 (cpi->twopass.kf_group_error_left > 0))
2122 cpi->twopass.gf_group_bits =
2123 (int64_t)(cpi->twopass.kf_group_bits *
2124 (gf_group_err / cpi->twopass.kf_group_error_left));
2127 cpi->twopass.gf_group_bits = 0;
2129 cpi->twopass.gf_group_bits =
2130 (cpi->twopass.gf_group_bits < 0)
2132 : (cpi->twopass.gf_group_bits > cpi->twopass.kf_group_bits)
2133 ? cpi->twopass.kf_group_bits : cpi->twopass.gf_group_bits;
2135 /* Clip cpi->twopass.gf_group_bits based on user supplied data rate
2136 * variability limit (cpi->oxcf.two_pass_vbrmax_section)
2138 if (cpi->twopass.gf_group_bits >
2139 (int64_t)max_bits * cpi->baseline_gf_interval)
2140 cpi->twopass.gf_group_bits =
2141 (int64_t)max_bits * cpi->baseline_gf_interval;
2144 reset_fpf_position(cpi, start_pos);
2147 cpi->twopass.modified_error_used += gf_group_err;
2150 for (i = 0; i <= (cpi->source_alt_ref_pending && cpi->common.frame_type != KEY_FRAME); i++) {
2153 int Q = (cpi->oxcf.fixed_q < 0) ? cpi->last_q[INTER_FRAME] : cpi->oxcf.fixed_q;
2157 if (cpi->source_alt_ref_pending && i == 0)
2162 Boost = (cpi->gfu_boost * 3 * GFQ_ADJUSTMENT) / (2 * 100);
2164 Boost += (cpi->baseline_gf_interval * 50);
2167 if (Boost > ((cpi->baseline_gf_interval + 1) * 200))
2168 Boost = ((cpi->baseline_gf_interval + 1) * 200);
2173 ((cpi->baseline_gf_interval + 1) * 100) + Boost;
2179 Boost = (cpi->gfu_boost * GFQ_ADJUSTMENT) / 100;
2182 if (Boost > (cpi->baseline_gf_interval * 150))
2183 Boost = (cpi->baseline_gf_interval * 150);
2188 (cpi->baseline_gf_interval * 100) + (Boost - 100);
2202 (cpi->twopass.gf_group_bits /
2209 if (mod_frame_err < gf_group_err / (double)cpi->baseline_gf_interval)
2215 (double)cpi->twopass.kf_group_bits *
2216 (mod_frame_err * (double)cpi->baseline_gf_interval) /
2217 DOUBLE_DIVIDE_CHECK((double)cpi->twopass.kf_group_error_left);
2234 (int)((double)cpi->twopass.kf_group_bits *
2236 DOUBLE_DIVIDE_CHECK((double)cpi->twopass.kf_group_error_left));
2245 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
2247 if (cpi->twopass.gf_bits > (int)(cpi->buffer_level >> 1))
2248 cpi->twopass.gf_bits = (int)(cpi->buffer_level >> 1);
2256 gf_bits += cpi->min_frame_bandwidth;
2260 cpi->twopass.gf_bits = gf_bits;
2262 if (i == 1 || (!cpi->source_alt_ref_pending && (cpi->common.frame_type != KEY_FRAME)))
2265 cpi->per_frame_bandwidth = gf_bits;
2271 cpi->twopass.kf_group_error_left -= (int64_t)gf_group_err;
2272 cpi->twopass.kf_group_bits -= cpi->twopass.gf_group_bits;
2274 if (cpi->twopass.kf_group_bits < 0)
2275 cpi->twopass.kf_group_bits = 0;
2282 if (!cpi->source_alt_ref_pending && cpi->common.frame_type != KEY_FRAME)
2283 cpi->twopass.gf_group_error_left = (int)(gf_group_err -
2286 cpi->twopass.gf_group_error_left = (int) gf_group_err;
2288 cpi->twopass.gf_group_bits -= cpi->twopass.gf_bits - cpi->min_frame_bandwidth;
2290 if (cpi->twopass.gf_group_bits < 0)
2291 cpi->twopass.gf_group_bits = 0;
2295 * calculation of cpi->twopass.alt_extra_bits.
2297 if ( cpi->baseline_gf_interval >= 3 )
2300 int boost = (cpi->source_alt_ref_pending)
2301 ? b_boost : cpi->gfu_boost;
2303 int boost = cpi->gfu_boost;
2312 cpi->twopass.alt_extra_bits =
2313 (int)(cpi->twopass.gf_group_bits * pct_extra) / 100;
2314 cpi->twopass.gf_group_bits -= cpi->twopass.alt_extra_bits;
2315 cpi->twopass.alt_extra_bits /=
2316 ((cpi->baseline_gf_interval-1)>>1);
2319 cpi->twopass.alt_extra_bits = 0;
2322 cpi->twopass.alt_extra_bits = 0;
2326 if (cpi->common.frame_type != KEY_FRAME)
2332 reset_fpf_position(cpi, start_pos);
2334 for (i = 0 ; i < cpi->baseline_gf_interval ; i++)
2336 input_stats(cpi, &next_frame);
2342 cpi->twopass.section_intra_rating = (unsigned int)
2347 cpi->twopass.section_max_qfactor = 1.0 - ((Ratio - 10.0) * 0.025);
2349 if (cpi->twopass.section_max_qfactor < 0.80)
2350 cpi->twopass.section_max_qfactor = 0.80;
2352 reset_fpf_position(cpi, start_pos);
2357 static void assign_std_frame_bits(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame)
2364 int max_bits = frame_max_bits(cpi); /* Max for a single frame */
2367 modified_err = calculate_modified_err(cpi, this_frame);
2370 if (cpi->twopass.gf_group_error_left > 0)
2371 err_fraction = modified_err / cpi->twopass.gf_group_error_left;
2376 target_frame_size = (int)((double)cpi->twopass.gf_group_bits * err_fraction);
2378 /* Clip to target size to 0 - max_bits (or cpi->twopass.gf_group_bits)
2388 if (target_frame_size > cpi->twopass.gf_group_bits)
2389 target_frame_size = (int)cpi->twopass.gf_group_bits;
2393 cpi->twopass.gf_group_error_left -= (int)modified_err;
2394 cpi->twopass.gf_group_bits -= target_frame_size;
2396 if (cpi->twopass.gf_group_bits < 0)
2397 cpi->twopass.gf_group_bits = 0;
2400 target_frame_size += cpi->min_frame_bandwidth;
2403 if ( (cpi->frames_since_golden & 0x01) &&
2404 (cpi->frames_till_gf_update_due > 0) )
2406 target_frame_size += cpi->twopass.alt_extra_bits;
2410 cpi->per_frame_bandwidth = target_frame_size;
2413 void vp8_second_pass(VP8_COMP *cpi)
2416 int frames_left = (int)(cpi->twopass.total_stats.count - cpi->common.current_video_frame);
2426 if (!cpi->twopass.stats_in)
2433 if (EOF == input_stats(cpi, &this_frame))
2440 if (cpi->twopass.frames_to_key == 0)
2444 find_next_key_frame(cpi, &this_frame_copy);
2453 if (cpi->oxcf.error_resilient_mode)
2455 cpi->twopass.gf_group_bits = cpi->twopass.kf_group_bits;
2456 cpi->twopass.gf_group_error_left =
2457 (int)cpi->twopass.kf_group_error_left;
2458 cpi->baseline_gf_interval = cpi->twopass.frames_to_key;
2459 cpi->frames_till_gf_update_due = cpi->baseline_gf_interval;
2460 cpi->source_alt_ref_pending = 0;
2466 if (cpi->frames_till_gf_update_due == 0)
2470 define_gf_group(cpi, &this_frame_copy);
2479 if (cpi->source_alt_ref_pending && (cpi->common.frame_type != KEY_FRAME))
2484 int bak = cpi->per_frame_bandwidth;
2486 assign_std_frame_bits(cpi, &this_frame_copy);
2487 cpi->per_frame_bandwidth = bak;
2501 if (cpi->oxcf.error_resilient_mode)
2503 cpi->frames_till_gf_update_due = cpi->twopass.frames_to_key;
2505 if (cpi->common.frame_type != KEY_FRAME)
2509 assign_std_frame_bits(cpi, &this_frame_copy);
2516 assign_std_frame_bits(cpi, &this_frame_copy);
2521 cpi->twopass.this_iiratio = (unsigned int)(this_frame_intra_error /
2525 if ( lookup_next_frame_stats(cpi, &next_frame) != EOF )
2527 cpi->twopass.next_iiratio = (unsigned int)(next_frame.intra_error /
2533 cpi->target_bandwidth = (int)
2534 (cpi->per_frame_bandwidth * cpi->output_framerate);
2535 if (cpi->target_bandwidth < 0)
2536 cpi->target_bandwidth = 0;
2541 cpi, &cpi->twopass.total_left_stats );
2544 if (cpi->common.current_video_frame == 0)
2546 cpi->twopass.est_max_qcorrection_factor = 1.0;
2549 if ( cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY )
2554 estimate_cq( cpi,
2555 &cpi->twopass.total_left_stats,
2556 (int)(cpi->twopass.bits_left / frames_left),
2559 cpi->cq_target_quality = cpi->oxcf.cq_level;
2560 if ( est_cq > cpi->cq_target_quality )
2561 cpi->cq_target_quality = est_cq;
2565 cpi->twopass.maxq_max_limit = cpi->worst_quality;
2566 cpi->twopass.maxq_min_limit = cpi->best_quality;
2569 cpi,
2570 &cpi->twopass.total_left_stats,
2571 (int)(cpi->twopass.bits_left / frames_left),
2580 cpi->twopass.maxq_max_limit = ((tmp_q + 32) < cpi->worst_quality)
2581 ? (tmp_q + 32) : cpi->worst_quality;
2582 cpi->twopass.maxq_min_limit = ((tmp_q - 32) > cpi->best_quality)
2583 ? (tmp_q - 32) : cpi->best_quality;
2585 cpi->active_worst_quality = tmp_q;
2586 cpi->ni_av_qi = tmp_q;
2594 else if ( (cpi->common.current_video_frame <
2595 (((unsigned int)cpi->twopass.total_stats.count * 255)>>8)) &&
2596 ((cpi->common.current_video_frame + cpi->baseline_gf_interval) <
2597 (unsigned int)cpi->twopass.total_stats.count) )
2603 cpi,
2604 &cpi->twopass.total_left_stats,
2605 (int)(cpi->twopass.bits_left / frames_left),
2609 if (tmp_q > cpi->active_worst_quality)
2610 cpi->active_worst_quality ++;
2611 else if (tmp_q < cpi->active_worst_quality)
2612 cpi->active_worst_quality --;
2614 cpi->active_worst_quality =
2615 ((cpi->active_worst_quality * 3) + tmp_q + 2) / 4;
2618 cpi->twopass.frames_to_key --;
2621 subtract_stats(&cpi->twopass.total_left_stats, &this_frame );
2625 static int test_candidate_kf(VP8_COMP *cpi, FIRSTPASS_STATS *last_frame, FIRSTPASS_STATS *this_frame, FIRSTPASS_STATS *next_frame)
2659 start_pos = cpi->twopass.stats_in;
2694 if (EOF == input_stats(cpi, &local_next_frame))
2706 reset_fpf_position(cpi, start_pos);
2714 static void find_next_key_frame(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame)
2736 start_position = cpi->twopass.stats_in;
2738 cpi->common.frame_type = KEY_FRAME;
2741 cpi->this_key_frame_forced = cpi->next_key_frame_forced;
2746 cpi->source_alt_ref_active = 0;
2749 cpi->frames_till_gf_update_due = 0;
2751 cpi->twopass.frames_to_key = 1;
2756 cpi->twopass.kf_group_bits = 0;
2757 cpi->twopass.kf_group_error_left = 0;
2759 kf_mod_err = calculate_modified_err(cpi, this_frame);
2763 while (cpi->twopass.stats_in < cpi->twopass.stats_in_end)
2766 kf_group_err += calculate_modified_err(cpi, this_frame);
2778 input_stats(cpi, this_frame);
2781 if (cpi->oxcf.auto_key
2782 && lookup_next_frame_stats(cpi, &next_frame) != EOF)
2786 test_candidate_kf(cpi, &last_frame, this_frame, &next_frame) )
2792 loop_decay_rate = get_prediction_decay_rate(cpi, &next_frame);
2808 if ( detect_transition_to_still( cpi, i,
2809 (cpi->key_frame_frequency-i),
2818 cpi->twopass.frames_to_key ++;
2823 if (cpi->twopass.frames_to_key >= 2 *(int)cpi->key_frame_frequency)
2826 cpi->twopass.frames_to_key ++;
2836 if (cpi->oxcf.auto_key
2837 && cpi->twopass.frames_to_key > (int)cpi->key_frame_frequency )
2839 FIRSTPASS_STATS *current_pos = cpi->twopass.stats_in;
2842 cpi->twopass.frames_to_key /= 2;
2848 reset_fpf_position(cpi, start_position);
2855 for( i = 0; i < cpi->twopass.frames_to_key; i++ )
2858 kf_group_err += calculate_modified_err(cpi, &tmp_frame);
2863 input_stats(cpi, &tmp_frame);
2867 reset_fpf_position(cpi, current_pos);
2869 cpi->next_key_frame_forced = 1;
2872 cpi->next_key_frame_forced = 0;
2875 if (cpi->twopass.stats_in >= cpi->twopass.stats_in_end)
2878 kf_group_err += calculate_modified_err(cpi, this_frame);
2890 if ((cpi->twopass.bits_left > 0) && (cpi->twopass.modified_error_left > 0.0))
2893 int max_bits = frame_max_bits(cpi);
2901 cpi->twopass.kf_group_bits = (int64_t)( cpi->twopass.bits_left *
2903 cpi->twopass.modified_error_left ));
2906 max_grp_bits = (int64_t)max_bits * (int64_t)cpi->twopass.frames_to_key;
2907 if (cpi->twopass.kf_group_bits > max_grp_bits)
2908 cpi->twopass.kf_group_bits = max_grp_bits;
2911 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
2913 int64_t opt_buffer_lvl = cpi->oxcf.optimal_buffer_level;
2914 int64_t buffer_lvl = cpi->buffer_level;
2922 cpi->oxcf.maximum_buffer_size) >> 1;
2927 av_group_bits = (int64_t)cpi->av_per_frame_bandwidth *
2928 (int64_t)cpi->twopass.frames_to_key;
2931 if (cpi->buffer_level >= high_water_mark)
2939 if (cpi->twopass.kf_group_bits < min_group_bits)
2940 cpi->twopass.kf_group_bits = min_group_bits;
2943 else if (cpi->twopass.kf_group_bits < av_group_bits)
2946 cpi->twopass.kf_group_bits;
2948 cpi->twopass.kf_group_bits +=
2957 cpi->twopass.kf_group_bits = 0;
2960 reset_fpf_position(cpi, start_position);
2969 for (i = 0 ; i < cpi->twopass.frames_to_key ; i++)
2973 if (EOF == input_stats(cpi, &next_frame))
2976 if (next_frame.intra_error > cpi->twopass.kf_intra_err_min)
2980 r = (IIKFACTOR2 * cpi->twopass.kf_intra_err_min /
2987 loop_decay_rate = get_prediction_decay_rate(cpi, &next_frame);
3009 reset_fpf_position(cpi, start_position);
3011 for (i = 0 ; i < cpi->twopass.frames_to_key ; i++)
3013 input_stats(cpi, &next_frame);
3019 cpi->twopass.section_intra_rating = (unsigned int)
3024 cpi->twopass.section_max_qfactor = 1.0 - ((Ratio - 10.0) * 0.025);
3026 if (cpi->twopass.section_max_qfactor < 0.80)
3027 cpi->twopass.section_max_qfactor = 0.80;
3031 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
3035 if (cpi->drop_frames_allowed)
3037 int df_buffer_level = (int)(cpi->oxcf.drop_frames_water_mark
3038 * (cpi->oxcf.optimal_buffer_level / 100));
3040 if (cpi->buffer_level > df_buffer_level)
3041 max_boost = ((double)((cpi->buffer_level - df_buffer_level) * 2 / 3) * 16.0) / DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth);
3045 else if (cpi->buffer_level > 0)
3047 max_boost = ((double)(cpi->buffer_level * 2 / 3) * 16.0) / DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth);
3059 reset_fpf_position(cpi, start_position);
3066 int Counter = cpi->twopass.frames_to_key;
3068 YV12_BUFFER_CONFIG *lst_yv12 = &cpi->common.yv12_fb[cpi->common.lst_fb_idx];
3111 * cpi->twopass.frames_to_key-1 because key frame itself is taken
3117 ((cpi->twopass.frames_to_key - 1) * 10) + kf_boost;
3122 ((cpi->twopass.frames_to_key - 1) * 100) + kf_boost;
3132 cpi->twopass.kf_group_bits = (cpi->twopass.kf_group_bits < 0) ? 0 : cpi->twopass.kf_group_bits;
3135 cpi->twopass.kf_bits = (int)((double)kf_boost * ((double)cpi->twopass.kf_group_bits / (double)allocation_chunks));
3138 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
3140 if (cpi->twopass.kf_bits > (int)((3 * cpi->buffer_level) >> 2))
3141 cpi->twopass.kf_bits = (int)((3 * cpi->buffer_level) >> 2);
3149 if (kf_mod_err < kf_group_err / cpi->twopass.frames_to_key)
3152 ((double)cpi->twopass.bits_left *
3153 (kf_mod_err * (double)cpi->twopass.frames_to_key) /
3154 DOUBLE_DIVIDE_CHECK(cpi->twopass.modified_error_left));
3159 if (cpi->twopass.kf_bits > alt_kf_bits)
3161 cpi->twopass.kf_bits = alt_kf_bits;
3171 (int)((double)cpi->twopass.bits_left *
3173 DOUBLE_DIVIDE_CHECK(cpi->twopass.modified_error_left)));
3175 if (alt_kf_bits > cpi->twopass.kf_bits)
3177 cpi->twopass.kf_bits = alt_kf_bits;
3181 cpi->twopass.kf_group_bits -= cpi->twopass.kf_bits;
3183 cpi->twopass.kf_bits += cpi->min_frame_bandwidth;
3186 cpi->per_frame_bandwidth = cpi->twopass.kf_bits;
3189 cpi->target_bandwidth = (int)(cpi->twopass.kf_bits *
3190 cpi->output_framerate);
3194 cpi->twopass.kf_group_error_left = (int)(kf_group_err - kf_mod_err);
3199 cpi->twopass.modified_error_left -= kf_group_err;
3201 if (cpi->oxcf.allow_spatial_resampling)
3208 int new_width = cpi->oxcf.Width;
3209 int new_height = cpi->oxcf.Height;
3211 int projected_buffer_level = (int)cpi->buffer_level;
3216 double err_per_frame = kf_group_err / cpi->twopass.frames_to_key;
3221 if ((cpi->common.Width != cpi->oxcf.Width) || (cpi->common.Height != cpi->oxcf.Height))
3225 cpi->common.horiz_scale = NORMAL;
3226 cpi->common.vert_scale = NORMAL;
3229 av_bits_per_frame = cpi->oxcf.target_bandwidth / DOUBLE_DIVIDE_CHECK((double)cpi->framerate);
3232 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
3245 (cpi->twopass.kf_group_bits / cpi->twopass.frames_to_key);
3255 if (bits_per_frame < (cpi->oxcf.target_bandwidth * cpi->oxcf.two_pass_vbrmin_section / 100))
3256 bits_per_frame = (cpi->oxcf.target_bandwidth * cpi->oxcf.two_pass_vbrmin_section / 100);
3259 kf_q = estimate_kf_group_q(cpi, err_per_frame,
3268 while (tmp_q > cpi->worst_quality)
3276 (cpi->buffer_level - (int)
3278 cpi->twopass.frames_to_key));
3283 fprintf(f, " %8d %8d %8d %8d %12.0f %8d %8d %8d\n", cpi->common.current_video_frame, kf_q, cpi->common.horiz_scale, cpi->common.vert_scale, kf_group_err / cpi->twopass.frames_to_key, (int)(cpi->twopass.kf_group_bits / cpi->twopass.frames_to_key), new_height, new_width);
3290 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
3296 if ((projected_buffer_level < (cpi->oxcf.resample_down_water_mark * cpi->oxcf.optimal_buffer_level / 100)) ||
3297 (last_kf_resampled && (projected_buffer_level < (cpi->oxcf.resample_up_water_mark * cpi->oxcf.optimal_buffer_level / 100))))
3304 int64_t clip_bits = (int64_t)(cpi->twopass.total_stats.count * cpi->oxcf.target_bandwidth / DOUBLE_DIVIDE_CHECK((double)cpi->framerate));
3305 int64_t over_spend = cpi->oxcf.starting_buffer_level - cpi->buffer_level;
3313 if ((last_kf_resampled && (kf_q > cpi->worst_quality)) ||
3314 ((kf_q > cpi->worst_quality) &&
3324 while ((kf_q >= cpi->worst_quality) && (scale_val < 6))
3328 cpi->common.vert_scale = vscale_lookup[scale_val];
3329 cpi->common.horiz_scale = hscale_lookup[scale_val];
3331 Scale2Ratio(cpi->common.horiz_scale, &hr, &hs);
3332 Scale2Ratio(cpi->common.vert_scale, &vr, &vs);
3334 new_width = ((hs - 1) + (cpi->oxcf.Width * hr)) / hs;
3335 new_height = ((vs - 1) + (cpi->oxcf.Height * vr)) / vs;
3341 effective_size_ratio = (double)(new_width * new_height) / (double)(cpi->oxcf.Width * cpi->oxcf.Height);
3347 kf_q = estimate_kf_group_q(cpi,
3354 fprintf(f, "******** %8d %8d %8d %12.0f %8d %8d %8d\n", kf_q, cpi->common.horiz_scale, cpi->common.vert_scale, kf_group_err / cpi->twopass.frames_to_key, (int)(cpi->twopass.kf_group_bits / cpi->twopass.frames_to_key), new_height, new_width);
3360 if ((cpi->common.Width != new_width) || (cpi->common.Height != new_height))
3362 cpi->common.Width = new_width;
3363 cpi->common.Height = new_height;
3364 vp8_alloc_compressor_data(cpi);