John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2010 The VP8 project authors. All Rights Reserved. |
| 3 | * |
John Koleszar | 94c52e4 | 2010-06-18 12:39:21 -0400 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license |
John Koleszar | 09202d8 | 2010-06-04 16:19:40 -0400 | [diff] [blame] | 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
John Koleszar | 94c52e4 | 2010-06-18 12:39:21 -0400 | [diff] [blame] | 7 | * in the file PATENTS. All contributing project authors may |
John Koleszar | 09202d8 | 2010-06-04 16:19:40 -0400 | [diff] [blame] | 8 | * be found in the AUTHORS file in the root of the source tree. |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | |
| 12 | #include "math.h" |
| 13 | #include "limits.h" |
| 14 | #include "block.h" |
| 15 | #include "onyx_int.h" |
| 16 | #include "variance.h" |
| 17 | #include "encodeintra.h" |
| 18 | #include "setupintrarecon.h" |
| 19 | #include "mcomp.h" |
| 20 | #include "vpx_scale/vpxscale.h" |
| 21 | #include "encodemb.h" |
| 22 | #include "extend.h" |
| 23 | #include "systemdependent.h" |
| 24 | #include "vpx_scale/yv12extend.h" |
| 25 | #include "vpx_mem/vpx_mem.h" |
| 26 | #include "swapyv12buffer.h" |
| 27 | #include <stdio.h> |
| 28 | #include "rdopt.h" |
| 29 | #include "quant_common.h" |
| 30 | #include "encodemv.h" |
| 31 | |
| 32 | //#define OUTPUT_FPF 1 |
| 33 | //#define FIRSTPASS_MM 1 |
| 34 | |
| 35 | #if CONFIG_RUNTIME_CPU_DETECT |
| 36 | #define IF_RTCD(x) (x) |
| 37 | #else |
| 38 | #define IF_RTCD(x) NULL |
| 39 | #endif |
| 40 | |
| 41 | extern void vp8_build_block_offsets(MACROBLOCK *x); |
| 42 | extern void vp8_setup_block_ptrs(MACROBLOCK *x); |
| 43 | extern void vp8cx_frame_init_quantizer(VP8_COMP *cpi); |
| 44 | extern void vp8_set_mbmode_and_mvs(MACROBLOCK *x, MB_PREDICTION_MODE mb, MV *mv); |
| 45 | extern void vp8_alloc_compressor_data(VP8_COMP *cpi); |
| 46 | |
| 47 | //#define GFQ_ADJUSTMENT (40 + ((15*Q)/10)) |
| 48 | //#define GFQ_ADJUSTMENT (80 + ((15*Q)/10)) |
| 49 | #define GFQ_ADJUSTMENT vp8_gf_boost_qadjustment[Q] |
| 50 | extern int vp8_kf_boost_qadjustment[QINDEX_RANGE]; |
| 51 | |
| 52 | extern const int vp8_gf_boost_qadjustment[QINDEX_RANGE]; |
| 53 | |
| 54 | #define IIFACTOR 1.4 |
| 55 | #define IIKFACTOR1 1.40 |
| 56 | #define IIKFACTOR2 1.5 |
| 57 | #define RMAX 14.0 |
| 58 | #define GF_RMAX 48.0 // 128.0 |
| 59 | |
| 60 | #define DOUBLE_DIVIDE_CHECK(X) ((X)<0?(X)-.000001:(X)+.000001) |
| 61 | |
| 62 | #define POW1 (double)cpi->oxcf.two_pass_vbrbias/100.0 |
| 63 | #define POW2 (double)cpi->oxcf.two_pass_vbrbias/100.0 |
| 64 | |
| 65 | static int vscale_lookup[7] = {0, 1, 1, 2, 2, 3, 3}; |
| 66 | static int hscale_lookup[7] = {0, 0, 1, 1, 2, 2, 3}; |
| 67 | |
| 68 | |
| 69 | void vp8_find_next_key_frame(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame); |
| 70 | int vp8_input_stats(VP8_COMP *cpi, FIRSTPASS_STATS *fps); |
| 71 | |
| 72 | int vp8_encode_intra(VP8_COMP *cpi, MACROBLOCK *x, int use_dc_pred) |
| 73 | { |
| 74 | |
| 75 | int i; |
| 76 | int intra_pred_var = 0; |
| 77 | (void) cpi; |
| 78 | |
| 79 | if (use_dc_pred) |
| 80 | { |
Scott LaVarnway | 9c7a009 | 2010-08-12 16:25:43 -0400 | [diff] [blame^] | 81 | x->e_mbd.mode_info_context->mbmi.mode = DC_PRED; |
| 82 | x->e_mbd.mode_info_context->mbmi.uv_mode = DC_PRED; |
| 83 | x->e_mbd.mode_info_context->mbmi.ref_frame = INTRA_FRAME; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 84 | |
| 85 | vp8_encode_intra16x16mby(IF_RTCD(&cpi->rtcd), x); |
| 86 | } |
| 87 | else |
| 88 | { |
| 89 | for (i = 0; i < 16; i++) |
| 90 | { |
| 91 | BLOCKD *b = &x->e_mbd.block[i]; |
| 92 | BLOCK *be = &x->block[i]; |
| 93 | |
| 94 | vp8_encode_intra4x4block(IF_RTCD(&cpi->rtcd), x, be, b, B_DC_PRED); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | intra_pred_var = VARIANCE_INVOKE(&cpi->rtcd.variance, getmbss)(x->src_diff); |
| 99 | |
| 100 | return intra_pred_var; |
| 101 | } |
| 102 | |
| 103 | // Resets the first pass file to the given position using a relative seek from the current position |
| 104 | static void reset_fpf_position(VP8_COMP *cpi, FIRSTPASS_STATS *Position) |
| 105 | { |
| 106 | cpi->stats_in = Position; |
| 107 | } |
| 108 | |
| 109 | static int lookup_next_frame_stats(VP8_COMP *cpi, FIRSTPASS_STATS *next_frame) |
| 110 | { |
| 111 | /*FIRSTPASS_STATS * start_pos; |
| 112 | int ret_val; |
| 113 | |
| 114 | start_pos = cpi->stats_in; |
| 115 | ret_val = vp8_input_stats(cpi, next_frame); |
| 116 | reset_fpf_position(cpi, start_pos); |
| 117 | |
| 118 | return ret_val;*/ |
| 119 | |
| 120 | if (cpi->stats_in >= cpi->stats_in_end) |
| 121 | return EOF; |
| 122 | |
| 123 | *next_frame = *cpi->stats_in; |
| 124 | return 1; |
| 125 | } |
| 126 | |
| 127 | // Calculate a modified Error used in distributing bits between easier and harder frames |
| 128 | static double calculate_modified_err(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame) |
| 129 | { |
| 130 | double av_err = cpi->total_stats.ssim_weighted_pred_err; |
| 131 | double this_err = this_frame->ssim_weighted_pred_err; |
| 132 | double modified_err; |
| 133 | |
| 134 | //double relative_next_iiratio; |
| 135 | //double next_iiratio; |
| 136 | //double sum_iiratio; |
| 137 | //int i; |
| 138 | |
| 139 | //FIRSTPASS_STATS next_frame; |
| 140 | //FIRSTPASS_STATS *start_pos; |
| 141 | |
| 142 | /*start_pos = cpi->stats_in; |
| 143 | sum_iiratio = 0.0; |
| 144 | i = 0; |
| 145 | while ( (i < 1) && vp8_input_stats(cpi,&next_frame) != EOF ) |
| 146 | { |
| 147 | |
| 148 | next_iiratio = next_frame.intra_error / DOUBLE_DIVIDE_CHECK(next_frame.coded_error); |
| 149 | next_iiratio = ( next_iiratio < 1.0 ) ? 1.0 : (next_iiratio > 20.0) ? 20.0 : next_iiratio; |
| 150 | sum_iiratio += next_iiratio; |
| 151 | i++; |
| 152 | } |
| 153 | if ( i > 0 ) |
| 154 | { |
| 155 | relative_next_iiratio = sum_iiratio / DOUBLE_DIVIDE_CHECK(cpi->avg_iiratio * (double)i); |
| 156 | } |
| 157 | else |
| 158 | { |
| 159 | relative_next_iiratio = 1.0; |
| 160 | } |
| 161 | reset_fpf_position(cpi, start_pos);*/ |
| 162 | |
| 163 | if (this_err > av_err) |
| 164 | modified_err = av_err * pow((this_err / DOUBLE_DIVIDE_CHECK(av_err)), POW1); |
| 165 | else |
| 166 | modified_err = av_err * pow((this_err / DOUBLE_DIVIDE_CHECK(av_err)), POW2); |
| 167 | |
| 168 | /* |
| 169 | relative_next_iiratio = pow(relative_next_iiratio,0.25); |
| 170 | modified_err = modified_err * relative_next_iiratio; |
| 171 | */ |
| 172 | |
| 173 | return modified_err; |
| 174 | } |
| 175 | |
| 176 | double vp8_simple_weight(YV12_BUFFER_CONFIG *source) |
| 177 | { |
| 178 | int i, j; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 179 | |
| 180 | unsigned char *src = source->y_buffer; |
| 181 | unsigned char value; |
| 182 | double sum_weights = 0.0; |
| 183 | double Weight; |
| 184 | |
| 185 | // Loop throught the Y plane raw examining levels and creating a weight for the image |
| 186 | for (i = 0; i < source->y_height; i++) |
| 187 | { |
| 188 | for (j = 0; j < source->y_width; j++) |
| 189 | { |
| 190 | value = src[j]; |
| 191 | |
| 192 | if (value >= 64) |
| 193 | Weight = 1.0; |
| 194 | else if (value > 32) |
| 195 | Weight = (value - 32.0f) / 32.0f; |
| 196 | else |
| 197 | Weight = 0.02; |
| 198 | |
| 199 | sum_weights += Weight; |
| 200 | } |
| 201 | |
| 202 | src += source->y_stride; |
| 203 | } |
| 204 | |
| 205 | sum_weights /= (source->y_height * source->y_width); |
| 206 | |
| 207 | return sum_weights; |
| 208 | } |
| 209 | |
| 210 | // This function returns the current per frame maximum bitrate target |
| 211 | int frame_max_bits(VP8_COMP *cpi) |
| 212 | { |
| 213 | // Max allocation for a single frame based on the max section guidelines passed in and how many bits are left |
| 214 | int max_bits; |
| 215 | |
| 216 | // For CBR we need to also consider buffer fullness. |
| 217 | // If we are running below the optimal level then we need to gradually tighten up on max_bits. |
| 218 | if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) |
| 219 | { |
agrange | d4b99b8 | 2010-06-18 15:18:09 +0100 | [diff] [blame] | 220 | double buffer_fullness_ratio = (double)cpi->buffer_level / DOUBLE_DIVIDE_CHECK((double)cpi->oxcf.optimal_buffer_level); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 221 | |
| 222 | // For CBR base this on the target average bits per frame plus the maximum sedction rate passed in by the user |
| 223 | max_bits = (int)(cpi->av_per_frame_bandwidth * ((double)cpi->oxcf.two_pass_vbrmax_section / 100.0)); |
| 224 | |
| 225 | // If our buffer is below the optimum level |
| 226 | if (buffer_fullness_ratio < 1.0) |
| 227 | { |
| 228 | // The lower of max_bits / 4 or cpi->av_per_frame_bandwidth / 4. |
| 229 | int min_max_bits = ((cpi->av_per_frame_bandwidth >> 2) < (max_bits >> 2)) ? cpi->av_per_frame_bandwidth >> 2 : max_bits >> 2; |
| 230 | |
| 231 | max_bits = (int)(max_bits * buffer_fullness_ratio); |
| 232 | |
| 233 | if (max_bits < min_max_bits) |
| 234 | max_bits = min_max_bits; // Lowest value we will set ... which should allow the buffer to refil. |
| 235 | } |
| 236 | } |
| 237 | // VBR |
| 238 | else |
| 239 | { |
| 240 | // For VBR base this on the bits and frames left plus the two_pass_vbrmax_section rate passed in by the user |
| 241 | max_bits = (int)(((double)cpi->bits_left / (cpi->total_stats.count - (double)cpi->common.current_video_frame)) * ((double)cpi->oxcf.two_pass_vbrmax_section / 100.0)); |
| 242 | } |
| 243 | |
| 244 | // Trap case where we are out of bits |
| 245 | if (max_bits < 0) |
| 246 | max_bits = 0; |
| 247 | |
| 248 | return max_bits; |
| 249 | } |
| 250 | |
| 251 | void vp8_output_stats(struct vpx_codec_pkt_list *pktlist, |
| 252 | FIRSTPASS_STATS *stats) |
| 253 | { |
| 254 | struct vpx_codec_cx_pkt pkt; |
| 255 | pkt.kind = VPX_CODEC_STATS_PKT; |
| 256 | pkt.data.twopass_stats.buf = stats; |
| 257 | pkt.data.twopass_stats.sz = sizeof(*stats); |
| 258 | vpx_codec_pkt_list_add(pktlist, &pkt); |
| 259 | |
| 260 | // TEMP debug code |
| 261 | #ifdef OUTPUT_FPF |
| 262 | { |
| 263 | FILE *fpfile; |
| 264 | fpfile = fopen("firstpass.stt", "a"); |
| 265 | |
| 266 | fprintf(fpfile, "%12.0f %12.0f %12.0f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.0f\n", |
| 267 | stats->frame, |
| 268 | stats->intra_error, |
| 269 | stats->coded_error, |
| 270 | stats->ssim_weighted_pred_err, |
| 271 | stats->pcnt_inter, |
| 272 | stats->pcnt_motion, |
| 273 | stats->pcnt_second_ref, |
| 274 | stats->MVr, |
| 275 | stats->mvr_abs, |
| 276 | stats->MVc, |
| 277 | stats->mvc_abs, |
| 278 | stats->MVrv, |
| 279 | stats->MVcv, |
| 280 | stats->mv_in_out_count, |
| 281 | stats->count); |
| 282 | fclose(fpfile); |
| 283 | } |
| 284 | #endif |
| 285 | } |
| 286 | |
| 287 | int vp8_input_stats(VP8_COMP *cpi, FIRSTPASS_STATS *fps) |
| 288 | { |
| 289 | if (cpi->stats_in >= cpi->stats_in_end) |
| 290 | return EOF; |
| 291 | |
| 292 | *fps = *cpi->stats_in++; |
| 293 | return 1; |
| 294 | } |
| 295 | |
| 296 | void vp8_zero_stats(FIRSTPASS_STATS *section) |
| 297 | { |
| 298 | section->frame = 0.0; |
| 299 | section->intra_error = 0.0; |
| 300 | section->coded_error = 0.0; |
| 301 | section->ssim_weighted_pred_err = 0.0; |
| 302 | section->pcnt_inter = 0.0; |
| 303 | section->pcnt_motion = 0.0; |
| 304 | section->pcnt_second_ref = 0.0; |
| 305 | section->MVr = 0.0; |
| 306 | section->mvr_abs = 0.0; |
| 307 | section->MVc = 0.0; |
| 308 | section->mvc_abs = 0.0; |
| 309 | section->MVrv = 0.0; |
| 310 | section->MVcv = 0.0; |
| 311 | section->mv_in_out_count = 0.0; |
| 312 | section->count = 0.0; |
| 313 | section->duration = 1.0; |
| 314 | } |
| 315 | void vp8_accumulate_stats(FIRSTPASS_STATS *section, FIRSTPASS_STATS *frame) |
| 316 | { |
| 317 | section->frame += frame->frame; |
| 318 | section->intra_error += frame->intra_error; |
| 319 | section->coded_error += frame->coded_error; |
| 320 | section->ssim_weighted_pred_err += frame->ssim_weighted_pred_err; |
| 321 | section->pcnt_inter += frame->pcnt_inter; |
| 322 | section->pcnt_motion += frame->pcnt_motion; |
| 323 | section->pcnt_second_ref += frame->pcnt_second_ref; |
| 324 | section->MVr += frame->MVr; |
| 325 | section->mvr_abs += frame->mvr_abs; |
| 326 | section->MVc += frame->MVc; |
| 327 | section->mvc_abs += frame->mvc_abs; |
| 328 | section->MVrv += frame->MVrv; |
| 329 | section->MVcv += frame->MVcv; |
| 330 | section->mv_in_out_count += frame->mv_in_out_count; |
| 331 | section->count += frame->count; |
| 332 | section->duration += frame->duration; |
| 333 | } |
| 334 | void vp8_avg_stats(FIRSTPASS_STATS *section) |
| 335 | { |
| 336 | if (section->count < 1.0) |
| 337 | return; |
| 338 | |
| 339 | section->intra_error /= section->count; |
| 340 | section->coded_error /= section->count; |
| 341 | section->ssim_weighted_pred_err /= section->count; |
| 342 | section->pcnt_inter /= section->count; |
| 343 | section->pcnt_second_ref /= section->count; |
| 344 | section->pcnt_motion /= section->count; |
| 345 | section->MVr /= section->count; |
| 346 | section->mvr_abs /= section->count; |
| 347 | section->MVc /= section->count; |
| 348 | section->mvc_abs /= section->count; |
| 349 | section->MVrv /= section->count; |
| 350 | section->MVcv /= section->count; |
| 351 | section->mv_in_out_count /= section->count; |
| 352 | section->duration /= section->count; |
| 353 | } |
| 354 | |
| 355 | int vp8_fpmm_get_pos(VP8_COMP *cpi) |
| 356 | { |
| 357 | return ftell(cpi->fp_motion_mapfile); |
| 358 | } |
| 359 | void vp8_fpmm_reset_pos(VP8_COMP *cpi, int target_pos) |
| 360 | { |
| 361 | int Offset; |
| 362 | |
| 363 | if (cpi->fp_motion_mapfile) |
| 364 | { |
| 365 | Offset = ftell(cpi->fp_motion_mapfile) - target_pos; |
| 366 | fseek(cpi->fp_motion_mapfile, (int) - Offset, SEEK_CUR); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | void vp8_advance_fpmm(VP8_COMP *cpi, int count) |
| 371 | { |
| 372 | #ifdef FIRSTPASS_MM |
| 373 | fseek(cpi->fp_motion_mapfile, (int)(count * cpi->common.MBs), SEEK_CUR); |
| 374 | #endif |
| 375 | } |
| 376 | |
| 377 | void vp8_input_fpmm(VP8_COMP *cpi, int count) |
| 378 | { |
| 379 | #ifdef FIRSTPASS_MM |
| 380 | |
| 381 | unsigned char *tmp_motion_map; |
| 382 | int i, j; |
| 383 | |
| 384 | if (!cpi->fp_motion_mapfile) |
| 385 | return; // Error |
| 386 | |
| 387 | // Create the first pass motion map structure and set to 0 |
| 388 | CHECK_MEM_ERROR(tmp_motion_map, vpx_calloc(cpi->common.MBs, 1)); |
| 389 | |
| 390 | // Reset the state of the global map |
| 391 | vpx_memset(cpi->fp_motion_map, 0, cpi->common.MBs); |
| 392 | |
| 393 | // Read the specified number of frame maps and set the global map to the highest value seen for each mb. |
| 394 | for (i = 0; i < count; i++) |
| 395 | { |
| 396 | if (fread(tmp_motion_map, 1, cpi->common.MBs, cpi->fp_motion_mapfile) == cpi->common.MBs) |
| 397 | { |
| 398 | for (j = 0; j < cpi->common.MBs; j++) |
| 399 | { |
| 400 | if (tmp_motion_map[j] > 1) |
| 401 | cpi->fp_motion_map[j] += 5; // Intra is flagged |
| 402 | else |
| 403 | cpi->fp_motion_map[j] += tmp_motion_map[j]; |
| 404 | } |
| 405 | } |
| 406 | else |
| 407 | break; // Read error |
| 408 | |
| 409 | } |
| 410 | |
| 411 | if (tmp_motion_map != 0) |
| 412 | vpx_free(tmp_motion_map); |
| 413 | |
| 414 | #endif |
| 415 | |
| 416 | } |
| 417 | |
| 418 | void vp8_init_first_pass(VP8_COMP *cpi) |
| 419 | { |
| 420 | vp8_zero_stats(&cpi->total_stats); |
| 421 | |
| 422 | #ifdef FIRSTPASS_MM |
| 423 | cpi->fp_motion_mapfile = fopen("fpmotionmap.stt", "wb"); |
| 424 | #endif |
| 425 | |
| 426 | // TEMP debug code |
| 427 | #ifdef OUTPUT_FPF |
| 428 | { |
| 429 | FILE *fpfile; |
| 430 | fpfile = fopen("firstpass.stt", "w"); |
| 431 | fclose(fpfile); |
| 432 | } |
| 433 | #endif |
| 434 | |
| 435 | } |
| 436 | |
| 437 | void vp8_end_first_pass(VP8_COMP *cpi) |
| 438 | { |
| 439 | vp8_output_stats(cpi->output_pkt_list, &cpi->total_stats); |
| 440 | |
| 441 | #ifdef FIRSTPASS_MM |
| 442 | |
| 443 | if (cpi->fp_motion_mapfile) |
| 444 | fclose(cpi->fp_motion_mapfile); |
| 445 | |
| 446 | #endif |
| 447 | |
| 448 | } |
| 449 | void vp8_zz_motion_search( VP8_COMP *cpi, MACROBLOCK * x, YV12_BUFFER_CONFIG * recon_buffer, int * best_motion_err, int recon_yoffset ) |
| 450 | { |
| 451 | MACROBLOCKD * const xd = & x->e_mbd; |
| 452 | BLOCK *b = &x->block[0]; |
| 453 | BLOCKD *d = &x->e_mbd.block[0]; |
| 454 | |
| 455 | unsigned char *src_ptr = (*(b->base_src) + b->src); |
| 456 | int src_stride = b->src_stride; |
| 457 | unsigned char *ref_ptr; |
| 458 | int ref_stride=d->pre_stride; |
| 459 | |
| 460 | // Set up pointers for this macro block recon buffer |
| 461 | xd->pre.y_buffer = recon_buffer->y_buffer + recon_yoffset; |
| 462 | |
| 463 | ref_ptr = (unsigned char *)(*(d->base_pre) + d->pre ); |
| 464 | |
| 465 | VARIANCE_INVOKE(IF_RTCD(&cpi->rtcd.variance), mse16x16) ( src_ptr, src_stride, ref_ptr, ref_stride, (unsigned int *)(best_motion_err)); |
| 466 | } |
| 467 | |
| 468 | |
| 469 | void vp8_first_pass_motion_search(VP8_COMP *cpi, MACROBLOCK *x, MV *ref_mv, MV *best_mv, YV12_BUFFER_CONFIG *recon_buffer, int *best_motion_err, int recon_yoffset ) |
| 470 | { |
| 471 | MACROBLOCKD *const xd = & x->e_mbd; |
| 472 | BLOCK *b = &x->block[0]; |
| 473 | BLOCKD *d = &x->e_mbd.block[0]; |
| 474 | int num00; |
| 475 | |
| 476 | MV tmp_mv = {0, 0}; |
| 477 | |
| 478 | int tmp_err; |
| 479 | int step_param = 3; //3; // Dont search over full range for first pass |
| 480 | int further_steps = (MAX_MVSEARCH_STEPS - 1) - step_param; //3; |
| 481 | int n; |
| 482 | vp8_variance_fn_ptr_t v_fn_ptr; |
| 483 | int new_mv_mode_penalty = 256; |
| 484 | |
| 485 | v_fn_ptr.vf = VARIANCE_INVOKE(IF_RTCD(&cpi->rtcd.variance), mse16x16); |
| 486 | v_fn_ptr.sdf = cpi->fn_ptr.sdf; |
| 487 | v_fn_ptr.sdx4df = cpi->fn_ptr.sdx4df; |
| 488 | |
| 489 | // Set up pointers for this macro block recon buffer |
| 490 | xd->pre.y_buffer = recon_buffer->y_buffer + recon_yoffset; |
| 491 | |
| 492 | // Initial step/diamond search centred on best mv |
| 493 | tmp_err = cpi->diamond_search_sad(x, b, d, ref_mv, &tmp_mv, step_param, x->errorperbit, &num00, &v_fn_ptr, x->mvsadcost, x->mvcost); |
| 494 | if ( tmp_err < INT_MAX-new_mv_mode_penalty ) |
| 495 | tmp_err += new_mv_mode_penalty; |
| 496 | |
| 497 | if (tmp_err < *best_motion_err) |
| 498 | { |
| 499 | *best_motion_err = tmp_err; |
| 500 | best_mv->row = tmp_mv.row; |
| 501 | best_mv->col = tmp_mv.col; |
| 502 | } |
| 503 | |
| 504 | // Further step/diamond searches as necessary |
| 505 | n = num00; |
| 506 | num00 = 0; |
| 507 | |
| 508 | while (n < further_steps) |
| 509 | { |
| 510 | n++; |
| 511 | |
| 512 | if (num00) |
| 513 | num00--; |
| 514 | else |
| 515 | { |
| 516 | tmp_err = cpi->diamond_search_sad(x, b, d, ref_mv, &tmp_mv, step_param + n, x->errorperbit, &num00, &v_fn_ptr, x->mvsadcost, x->mvcost); |
| 517 | if ( tmp_err < INT_MAX-new_mv_mode_penalty ) |
| 518 | tmp_err += new_mv_mode_penalty; |
| 519 | |
| 520 | if (tmp_err < *best_motion_err) |
| 521 | { |
| 522 | *best_motion_err = tmp_err; |
| 523 | best_mv->row = tmp_mv.row; |
| 524 | best_mv->col = tmp_mv.col; |
| 525 | } |
| 526 | } |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | void vp8_first_pass(VP8_COMP *cpi) |
| 531 | { |
| 532 | int mb_row, mb_col; |
| 533 | MACROBLOCK *const x = & cpi->mb; |
| 534 | VP8_COMMON *const cm = & cpi->common; |
| 535 | MACROBLOCKD *const xd = & x->e_mbd; |
| 536 | |
| 537 | int col_blocks = 4 * cm->mb_cols; |
| 538 | int recon_yoffset, recon_uvoffset; |
Fritz Koenig | 0ce3901 | 2010-07-22 08:07:32 -0400 | [diff] [blame] | 539 | YV12_BUFFER_CONFIG *lst_yv12 = &cm->yv12_fb[cm->lst_fb_idx]; |
| 540 | YV12_BUFFER_CONFIG *new_yv12 = &cm->yv12_fb[cm->new_fb_idx]; |
| 541 | YV12_BUFFER_CONFIG *gld_yv12 = &cm->yv12_fb[cm->gld_fb_idx]; |
| 542 | int recon_y_stride = lst_yv12->y_stride; |
| 543 | int recon_uv_stride = lst_yv12->uv_stride; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 544 | int intra_error = 0; |
| 545 | int coded_error = 0; |
| 546 | |
| 547 | int sum_mvr = 0, sum_mvc = 0; |
| 548 | int sum_mvr_abs = 0, sum_mvc_abs = 0; |
| 549 | int sum_mvrs = 0, sum_mvcs = 0; |
| 550 | int mvcount = 0; |
| 551 | int intercount = 0; |
| 552 | int second_ref_count = 0; |
| 553 | int intrapenalty = 256; |
| 554 | |
| 555 | int sum_in_vectors = 0; |
| 556 | |
| 557 | MV best_ref_mv = {0, 0}; |
| 558 | MV zero_ref_mv = {0, 0}; |
| 559 | |
| 560 | unsigned char *fp_motion_map_ptr = cpi->fp_motion_map; |
| 561 | |
| 562 | vp8_clear_system_state(); //__asm emms; |
| 563 | |
| 564 | x->src = * cpi->Source; |
Fritz Koenig | 0ce3901 | 2010-07-22 08:07:32 -0400 | [diff] [blame] | 565 | xd->pre = *lst_yv12; |
| 566 | xd->dst = *new_yv12; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 567 | |
Scott LaVarnway | 9c7a009 | 2010-08-12 16:25:43 -0400 | [diff] [blame^] | 568 | xd->mode_info_context = cm->mi; |
| 569 | |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 570 | vp8_build_block_offsets(x); |
| 571 | |
| 572 | vp8_setup_block_dptrs(&x->e_mbd); |
| 573 | |
| 574 | vp8_setup_block_ptrs(x); |
| 575 | |
| 576 | // set up frame new frame for intra coded blocks |
Fritz Koenig | 0ce3901 | 2010-07-22 08:07:32 -0400 | [diff] [blame] | 577 | vp8_setup_intra_recon(new_yv12); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 578 | vp8cx_frame_init_quantizer(cpi); |
| 579 | |
| 580 | // Initialise the MV cost table to the defaults |
| 581 | //if( cm->current_video_frame == 0) |
| 582 | //if ( 0 ) |
| 583 | { |
| 584 | int flag[2] = {1, 1}; |
| 585 | vp8_initialize_rd_consts(cpi, vp8_dc_quant(cm->base_qindex, cm->y1dc_delta_q)); |
| 586 | vpx_memcpy(cm->fc.mvc, vp8_default_mv_context, sizeof(vp8_default_mv_context)); |
| 587 | vp8_build_component_cost_table(cpi->mb.mvcost, cpi->mb.mvsadcost, (const MV_CONTEXT *) cm->fc.mvc, flag); |
| 588 | } |
| 589 | |
| 590 | // for each macroblock row in image |
| 591 | for (mb_row = 0; mb_row < cm->mb_rows; mb_row++) |
| 592 | { |
| 593 | MV best_ref_mv = {0, 0}; |
| 594 | |
| 595 | // reset above block coeffs |
| 596 | xd->up_available = (mb_row != 0); |
| 597 | recon_yoffset = (mb_row * recon_y_stride * 16); |
| 598 | recon_uvoffset = (mb_row * recon_uv_stride * 8); |
| 599 | |
| 600 | // for each macroblock col in image |
| 601 | for (mb_col = 0; mb_col < cm->mb_cols; mb_col++) |
| 602 | { |
| 603 | int this_error; |
| 604 | int gf_motion_error = INT_MAX; |
| 605 | int use_dc_pred = (mb_col || mb_row) && (!mb_col || !mb_row); |
| 606 | |
Fritz Koenig | 0ce3901 | 2010-07-22 08:07:32 -0400 | [diff] [blame] | 607 | xd->dst.y_buffer = new_yv12->y_buffer + recon_yoffset; |
| 608 | xd->dst.u_buffer = new_yv12->u_buffer + recon_uvoffset; |
| 609 | xd->dst.v_buffer = new_yv12->v_buffer + recon_uvoffset; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 610 | xd->left_available = (mb_col != 0); |
| 611 | |
| 612 | // do intra 16x16 prediction |
| 613 | this_error = vp8_encode_intra(cpi, x, use_dc_pred); |
| 614 | |
| 615 | // "intrapenalty" below deals with situations where the intra and inter error scores are very low (eg a plain black frame) |
| 616 | // We do not have special cases in first pass for 0,0 and nearest etc so all inter modes carry an overhead cost estimate fot the mv. |
| 617 | // When the error score is very low this causes us to pick all or lots of INTRA modes and throw lots of key frames. |
| 618 | // This penalty adds a cost matching that of a 0,0 mv to the intra case. |
| 619 | this_error += intrapenalty; |
| 620 | |
| 621 | // Cumulative intra error total |
| 622 | intra_error += this_error; |
| 623 | |
| 624 | // Indicate default assumption of intra in the motion map |
| 625 | *fp_motion_map_ptr = 2; |
| 626 | |
| 627 | // Set up limit values for motion vectors to prevent them extending outside the UMV borders |
| 628 | x->mv_col_min = -((mb_col * 16) + (VP8BORDERINPIXELS - 16)); |
| 629 | x->mv_col_max = ((cm->mb_cols - 1 - mb_col) * 16) + (VP8BORDERINPIXELS - 16); |
| 630 | x->mv_row_min = -((mb_row * 16) + (VP8BORDERINPIXELS - 16)); |
| 631 | x->mv_row_max = ((cm->mb_rows - 1 - mb_row) * 16) + (VP8BORDERINPIXELS - 16); |
| 632 | |
| 633 | // Other than for the first frame do a motion search |
| 634 | if (cm->current_video_frame > 0) |
| 635 | { |
| 636 | BLOCK *b = &x->block[0]; |
| 637 | BLOCKD *d = &x->e_mbd.block[0]; |
| 638 | MV tmp_mv = {0, 0}; |
| 639 | int tmp_err; |
| 640 | int motion_error = INT_MAX; |
| 641 | |
| 642 | // Simple 0,0 motion with no mv overhead |
Fritz Koenig | 0ce3901 | 2010-07-22 08:07:32 -0400 | [diff] [blame] | 643 | vp8_zz_motion_search( cpi, x, lst_yv12, &motion_error, recon_yoffset ); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 644 | d->bmi.mv.as_mv.row = 0; |
| 645 | d->bmi.mv.as_mv.col = 0; |
| 646 | |
Adrian Grange | 0618ff1 | 2010-07-01 14:17:04 +0100 | [diff] [blame] | 647 | // Test last reference frame using the previous best mv as the |
| 648 | // starting point (best reference) for the search |
| 649 | vp8_first_pass_motion_search(cpi, x, &best_ref_mv, |
Fritz Koenig | 0ce3901 | 2010-07-22 08:07:32 -0400 | [diff] [blame] | 650 | &d->bmi.mv.as_mv, lst_yv12, |
Adrian Grange | 0618ff1 | 2010-07-01 14:17:04 +0100 | [diff] [blame] | 651 | &motion_error, recon_yoffset); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 652 | |
| 653 | // If the current best reference mv is not centred on 0,0 then do a 0,0 based search as well |
| 654 | if ((best_ref_mv.col != 0) || (best_ref_mv.row != 0)) |
| 655 | { |
| 656 | tmp_err = INT_MAX; |
Adrian Grange | 0618ff1 | 2010-07-01 14:17:04 +0100 | [diff] [blame] | 657 | vp8_first_pass_motion_search(cpi, x, &zero_ref_mv, &tmp_mv, |
Fritz Koenig | 0ce3901 | 2010-07-22 08:07:32 -0400 | [diff] [blame] | 658 | lst_yv12, &tmp_err, recon_yoffset); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 659 | |
| 660 | if ( tmp_err < motion_error ) |
| 661 | { |
| 662 | motion_error = tmp_err; |
| 663 | d->bmi.mv.as_mv.row = tmp_mv.row; |
| 664 | d->bmi.mv.as_mv.col = tmp_mv.col; |
| 665 | } |
| 666 | |
| 667 | } |
| 668 | |
| 669 | // Experimental search in a second reference frame ((0,0) based only) |
| 670 | if (cm->current_video_frame > 1) |
| 671 | { |
Fritz Koenig | 0ce3901 | 2010-07-22 08:07:32 -0400 | [diff] [blame] | 672 | vp8_first_pass_motion_search(cpi, x, &zero_ref_mv, &tmp_mv, gld_yv12, &gf_motion_error, recon_yoffset); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 673 | |
| 674 | if ((gf_motion_error < motion_error) && (gf_motion_error < this_error)) |
| 675 | { |
| 676 | second_ref_count++; |
| 677 | //motion_error = gf_motion_error; |
| 678 | //d->bmi.mv.as_mv.row = tmp_mv.row; |
| 679 | //d->bmi.mv.as_mv.col = tmp_mv.col; |
| 680 | } |
| 681 | /*else |
| 682 | { |
| 683 | xd->pre.y_buffer = cm->last_frame.y_buffer + recon_yoffset; |
| 684 | xd->pre.u_buffer = cm->last_frame.u_buffer + recon_uvoffset; |
| 685 | xd->pre.v_buffer = cm->last_frame.v_buffer + recon_uvoffset; |
| 686 | }*/ |
| 687 | |
| 688 | |
| 689 | // Reset to last frame as reference buffer |
Fritz Koenig | 0ce3901 | 2010-07-22 08:07:32 -0400 | [diff] [blame] | 690 | xd->pre.y_buffer = lst_yv12->y_buffer + recon_yoffset; |
| 691 | xd->pre.u_buffer = lst_yv12->u_buffer + recon_uvoffset; |
| 692 | xd->pre.v_buffer = lst_yv12->v_buffer + recon_uvoffset; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | if (motion_error <= this_error) |
| 696 | { |
| 697 | d->bmi.mv.as_mv.row <<= 3; |
| 698 | d->bmi.mv.as_mv.col <<= 3; |
| 699 | this_error = motion_error; |
| 700 | vp8_set_mbmode_and_mvs(x, NEWMV, &d->bmi.mv.as_mv); |
| 701 | vp8_encode_inter16x16y(IF_RTCD(&cpi->rtcd), x); |
| 702 | sum_mvr += d->bmi.mv.as_mv.row; |
| 703 | sum_mvr_abs += abs(d->bmi.mv.as_mv.row); |
| 704 | sum_mvc += d->bmi.mv.as_mv.col; |
| 705 | sum_mvc_abs += abs(d->bmi.mv.as_mv.col); |
| 706 | sum_mvrs += d->bmi.mv.as_mv.row * d->bmi.mv.as_mv.row; |
| 707 | sum_mvcs += d->bmi.mv.as_mv.col * d->bmi.mv.as_mv.col; |
| 708 | intercount++; |
| 709 | |
| 710 | best_ref_mv.row = d->bmi.mv.as_mv.row; |
| 711 | best_ref_mv.col = d->bmi.mv.as_mv.col; |
| 712 | //best_ref_mv.row = 0; |
| 713 | //best_ref_mv.col = 0; |
| 714 | |
| 715 | // Was the vector non-zero |
| 716 | if (d->bmi.mv.as_mv.row || d->bmi.mv.as_mv.col) |
| 717 | { |
| 718 | mvcount++; |
| 719 | |
| 720 | *fp_motion_map_ptr = 1; |
| 721 | |
| 722 | // Does the Row vector point inwards or outwards |
| 723 | if (mb_row < cm->mb_rows / 2) |
| 724 | { |
| 725 | if (d->bmi.mv.as_mv.row > 0) |
| 726 | sum_in_vectors--; |
| 727 | else if (d->bmi.mv.as_mv.row < 0) |
| 728 | sum_in_vectors++; |
| 729 | } |
| 730 | else if (mb_row > cm->mb_rows / 2) |
| 731 | { |
| 732 | if (d->bmi.mv.as_mv.row > 0) |
| 733 | sum_in_vectors++; |
| 734 | else if (d->bmi.mv.as_mv.row < 0) |
| 735 | sum_in_vectors--; |
| 736 | } |
| 737 | |
| 738 | // Does the Row vector point inwards or outwards |
| 739 | if (mb_col < cm->mb_cols / 2) |
| 740 | { |
| 741 | if (d->bmi.mv.as_mv.col > 0) |
| 742 | sum_in_vectors--; |
| 743 | else if (d->bmi.mv.as_mv.col < 0) |
| 744 | sum_in_vectors++; |
| 745 | } |
| 746 | else if (mb_col > cm->mb_cols / 2) |
| 747 | { |
| 748 | if (d->bmi.mv.as_mv.col > 0) |
| 749 | sum_in_vectors++; |
| 750 | else if (d->bmi.mv.as_mv.col < 0) |
| 751 | sum_in_vectors--; |
| 752 | } |
| 753 | } |
| 754 | else |
| 755 | *fp_motion_map_ptr = 0; // 0,0 mv was best |
| 756 | } |
| 757 | else |
| 758 | { |
| 759 | best_ref_mv.row = 0; |
| 760 | best_ref_mv.col = 0; |
| 761 | } |
| 762 | } |
| 763 | |
| 764 | coded_error += this_error; |
| 765 | |
| 766 | // adjust to the next column of macroblocks |
| 767 | x->src.y_buffer += 16; |
| 768 | x->src.u_buffer += 8; |
| 769 | x->src.v_buffer += 8; |
| 770 | |
| 771 | recon_yoffset += 16; |
| 772 | recon_uvoffset += 8; |
| 773 | |
| 774 | // Update the motion map |
| 775 | fp_motion_map_ptr++; |
| 776 | } |
| 777 | |
| 778 | // adjust to the next row of mbs |
| 779 | x->src.y_buffer += 16 * x->src.y_stride - 16 * cm->mb_cols; |
| 780 | x->src.u_buffer += 8 * x->src.uv_stride - 8 * cm->mb_cols; |
| 781 | x->src.v_buffer += 8 * x->src.uv_stride - 8 * cm->mb_cols; |
| 782 | |
| 783 | //extend the recon for intra prediction |
Fritz Koenig | 0ce3901 | 2010-07-22 08:07:32 -0400 | [diff] [blame] | 784 | vp8_extend_mb_row(new_yv12, xd->dst.y_buffer + 16, xd->dst.u_buffer + 8, xd->dst.v_buffer + 8); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 785 | vp8_clear_system_state(); //__asm emms; |
| 786 | } |
| 787 | |
| 788 | vp8_clear_system_state(); //__asm emms; |
| 789 | { |
| 790 | double weight = 0.0; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 791 | |
| 792 | FIRSTPASS_STATS fps; |
| 793 | |
| 794 | fps.frame = cm->current_video_frame ; |
| 795 | fps.intra_error = intra_error >> 8; |
| 796 | fps.coded_error = coded_error >> 8; |
| 797 | weight = vp8_simple_weight(cpi->Source); |
| 798 | |
| 799 | if (weight < 0.1) |
| 800 | weight = 0.1; |
| 801 | |
| 802 | fps.ssim_weighted_pred_err = fps.coded_error * weight; |
| 803 | |
| 804 | fps.pcnt_inter = 0.0; |
| 805 | fps.pcnt_motion = 0.0; |
| 806 | fps.MVr = 0.0; |
| 807 | fps.mvr_abs = 0.0; |
| 808 | fps.MVc = 0.0; |
| 809 | fps.mvc_abs = 0.0; |
| 810 | fps.MVrv = 0.0; |
| 811 | fps.MVcv = 0.0; |
| 812 | fps.mv_in_out_count = 0.0; |
| 813 | fps.count = 1.0; |
| 814 | |
| 815 | fps.pcnt_inter = 1.0 * (double)intercount / cm->MBs; |
| 816 | fps.pcnt_second_ref = 1.0 * (double)second_ref_count / cm->MBs; |
| 817 | |
| 818 | if (mvcount > 0) |
| 819 | { |
| 820 | fps.MVr = (double)sum_mvr / (double)mvcount; |
| 821 | fps.mvr_abs = (double)sum_mvr_abs / (double)mvcount; |
| 822 | fps.MVc = (double)sum_mvc / (double)mvcount; |
| 823 | fps.mvc_abs = (double)sum_mvc_abs / (double)mvcount; |
| 824 | fps.MVrv = ((double)sum_mvrs - (fps.MVr * fps.MVr / (double)mvcount)) / (double)mvcount; |
| 825 | fps.MVcv = ((double)sum_mvcs - (fps.MVc * fps.MVc / (double)mvcount)) / (double)mvcount; |
| 826 | fps.mv_in_out_count = (double)sum_in_vectors / (double)(mvcount * 2); |
| 827 | |
| 828 | fps.pcnt_motion = 1.0 * (double)mvcount / cpi->common.MBs; |
| 829 | } |
| 830 | |
| 831 | // TODO: handle the case when duration is set to 0, or something less |
| 832 | // than the full time between subsequent cpi->source_time_stamp s . |
| 833 | fps.duration = cpi->source_end_time_stamp - cpi->source_time_stamp; |
| 834 | |
| 835 | // don't want to do outputstats with a stack variable! |
| 836 | cpi->this_frame_stats = fps; |
| 837 | vp8_output_stats(cpi->output_pkt_list, &cpi->this_frame_stats); |
| 838 | vp8_accumulate_stats(&cpi->total_stats, &fps); |
| 839 | |
| 840 | #ifdef FIRSTPASS_MM |
| 841 | fwrite(cpi->fp_motion_map, 1, cpi->common.MBs, cpi->fp_motion_mapfile); |
| 842 | #endif |
| 843 | } |
| 844 | |
| 845 | // Copy the previous Last Frame into the GF buffer if specific conditions for doing so are met |
| 846 | if ((cm->current_video_frame > 0) && |
| 847 | (cpi->this_frame_stats.pcnt_inter > 0.20) && |
| 848 | ((cpi->this_frame_stats.intra_error / cpi->this_frame_stats.coded_error) > 2.0)) |
| 849 | { |
Fritz Koenig | 0ce3901 | 2010-07-22 08:07:32 -0400 | [diff] [blame] | 850 | vp8_yv12_copy_frame_ptr(lst_yv12, gld_yv12); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | // swap frame pointers so last frame refers to the frame we just compressed |
Fritz Koenig | 0ce3901 | 2010-07-22 08:07:32 -0400 | [diff] [blame] | 854 | vp8_swap_yv12_buffer(lst_yv12, new_yv12); |
| 855 | vp8_yv12_extend_frame_borders(lst_yv12); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 856 | |
| 857 | // Special case for the first frame. Copy into the GF buffer as a second reference. |
| 858 | if (cm->current_video_frame == 0) |
| 859 | { |
Fritz Koenig | 0ce3901 | 2010-07-22 08:07:32 -0400 | [diff] [blame] | 860 | vp8_yv12_copy_frame_ptr(lst_yv12, gld_yv12); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | |
| 864 | // use this to see what the first pass reconstruction looks like |
| 865 | if (0) |
| 866 | { |
| 867 | char filename[512]; |
| 868 | FILE *recon_file; |
| 869 | sprintf(filename, "enc%04d.yuv", (int) cm->current_video_frame); |
| 870 | |
| 871 | if (cm->current_video_frame == 0) |
| 872 | recon_file = fopen(filename, "wb"); |
| 873 | else |
| 874 | recon_file = fopen(filename, "ab"); |
| 875 | |
Fritz Koenig | 0ce3901 | 2010-07-22 08:07:32 -0400 | [diff] [blame] | 876 | fwrite(lst_yv12->buffer_alloc, lst_yv12->frame_size, 1, recon_file); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 877 | fclose(recon_file); |
| 878 | } |
| 879 | |
| 880 | cm->current_video_frame++; |
| 881 | |
| 882 | } |
| 883 | extern const int vp8_bits_per_mb[2][QINDEX_RANGE]; |
| 884 | |
| 885 | #define BASE_ERRPERMB 150 |
| 886 | static int estimate_max_q(VP8_COMP *cpi, double section_err, int section_target_bandwitdh, int Height, int Width) |
| 887 | { |
| 888 | int Q; |
| 889 | int num_mbs = ((Height * Width) / (16 * 16)); |
| 890 | int target_norm_bits_per_mb; |
| 891 | |
| 892 | double err_per_mb = section_err / num_mbs; |
| 893 | double correction_factor; |
| 894 | double corr_high; |
| 895 | double speed_correction = 1.0; |
| 896 | double rolling_ratio; |
| 897 | |
| 898 | double pow_highq = 0.90; |
| 899 | double pow_lowq = 0.40; |
| 900 | |
| 901 | if (section_target_bandwitdh <= 0) |
| 902 | return MAXQ; |
| 903 | |
| 904 | target_norm_bits_per_mb = (section_target_bandwitdh < (1 << 20)) ? (512 * section_target_bandwitdh) / num_mbs : 512 * (section_target_bandwitdh / num_mbs); |
| 905 | |
| 906 | // Calculate a corrective factor based on a rolling ratio of bits spent vs target bits |
| 907 | if ((cpi->rolling_target_bits > 0.0) && (cpi->active_worst_quality < cpi->worst_quality)) |
| 908 | { |
| 909 | //double adjustment_rate = 0.985 + (0.00005 * cpi->active_worst_quality); |
| 910 | double adjustment_rate = 0.99; |
| 911 | |
| 912 | rolling_ratio = (double)cpi->rolling_actual_bits / (double)cpi->rolling_target_bits; |
| 913 | |
| 914 | //if ( cpi->est_max_qcorrection_factor > rolling_ratio ) |
| 915 | if (rolling_ratio < 0.95) |
| 916 | //cpi->est_max_qcorrection_factor *= adjustment_rate; |
| 917 | cpi->est_max_qcorrection_factor -= 0.005; |
| 918 | //else if ( cpi->est_max_qcorrection_factor < rolling_ratio ) |
| 919 | else if (rolling_ratio > 1.05) |
| 920 | cpi->est_max_qcorrection_factor += 0.005; |
| 921 | |
| 922 | //cpi->est_max_qcorrection_factor /= adjustment_rate; |
| 923 | |
| 924 | cpi->est_max_qcorrection_factor = (cpi->est_max_qcorrection_factor < 0.1) ? 0.1 : (cpi->est_max_qcorrection_factor > 10.0) ? 10.0 : cpi->est_max_qcorrection_factor; |
| 925 | } |
| 926 | |
| 927 | // Corrections for higher compression speed settings (reduced compression expected) |
| 928 | if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1)) |
| 929 | { |
| 930 | if (cpi->oxcf.cpu_used <= 5) |
| 931 | speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04); |
| 932 | else |
| 933 | speed_correction = 1.25; |
| 934 | } |
| 935 | |
| 936 | // Correction factor used for Q values >= 20 |
| 937 | corr_high = pow(err_per_mb / BASE_ERRPERMB, pow_highq); |
| 938 | corr_high = (corr_high < 0.05) ? 0.05 : (corr_high > 5.0) ? 5.0 : corr_high; |
| 939 | |
| 940 | // Try and pick a Q that should be high enough to encode the content at the given rate. |
| 941 | for (Q = 0; Q < MAXQ; Q++) |
| 942 | { |
| 943 | int bits_per_mb_at_this_q; |
| 944 | |
| 945 | if (Q < 50) |
| 946 | { |
| 947 | correction_factor = pow(err_per_mb / BASE_ERRPERMB, (pow_lowq + Q * 0.01)); |
| 948 | correction_factor = (correction_factor < 0.05) ? 0.05 : (correction_factor > 5.0) ? 5.0 : correction_factor; |
| 949 | } |
| 950 | else |
| 951 | correction_factor = corr_high; |
| 952 | |
| 953 | bits_per_mb_at_this_q = (int)(.5 + correction_factor * speed_correction * cpi->est_max_qcorrection_factor * cpi->section_max_qfactor * (double)vp8_bits_per_mb[INTER_FRAME][Q] / 1.0); |
| 954 | //bits_per_mb_at_this_q = (int)(.5 + correction_factor * speed_correction * cpi->est_max_qcorrection_factor * (double)vp8_bits_per_mb[INTER_FRAME][Q] / 1.0); |
| 955 | |
| 956 | if (bits_per_mb_at_this_q <= target_norm_bits_per_mb) |
| 957 | break; |
| 958 | } |
| 959 | |
| 960 | return Q; |
| 961 | } |
| 962 | static int estimate_q(VP8_COMP *cpi, double section_err, int section_target_bandwitdh, int Height, int Width) |
| 963 | { |
| 964 | int Q; |
| 965 | int num_mbs = ((Height * Width) / (16 * 16)); |
| 966 | int target_norm_bits_per_mb; |
| 967 | |
| 968 | double err_per_mb = section_err / num_mbs; |
| 969 | double correction_factor; |
| 970 | double corr_high; |
| 971 | double speed_correction = 1.0; |
| 972 | double pow_highq = 0.90; |
| 973 | double pow_lowq = 0.40; |
| 974 | |
| 975 | target_norm_bits_per_mb = (section_target_bandwitdh < (1 << 20)) ? (512 * section_target_bandwitdh) / num_mbs : 512 * (section_target_bandwitdh / num_mbs); |
| 976 | |
| 977 | // Corrections for higher compression speed settings (reduced compression expected) |
| 978 | if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1)) |
| 979 | { |
| 980 | if (cpi->oxcf.cpu_used <= 5) |
| 981 | speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04); |
| 982 | else |
| 983 | speed_correction = 1.25; |
| 984 | } |
| 985 | |
| 986 | // Correction factor used for Q values >= 20 |
| 987 | corr_high = pow(err_per_mb / BASE_ERRPERMB, pow_highq); |
| 988 | corr_high = (corr_high < 0.05) ? 0.05 : (corr_high > 5.0) ? 5.0 : corr_high; |
| 989 | |
| 990 | // Try and pick a Q that can encode the content at the given rate. |
| 991 | for (Q = 0; Q < MAXQ; Q++) |
| 992 | { |
| 993 | int bits_per_mb_at_this_q; |
| 994 | |
| 995 | if (Q < 50) |
| 996 | { |
| 997 | correction_factor = pow(err_per_mb / BASE_ERRPERMB, (pow_lowq + Q * 0.01)); |
| 998 | correction_factor = (correction_factor < 0.05) ? 0.05 : (correction_factor > 5.0) ? 5.0 : correction_factor; |
| 999 | } |
| 1000 | else |
| 1001 | correction_factor = corr_high; |
| 1002 | |
| 1003 | bits_per_mb_at_this_q = (int)(.5 + correction_factor * speed_correction * cpi->est_max_qcorrection_factor * (double)vp8_bits_per_mb[INTER_FRAME][Q] / 1.0); |
| 1004 | |
| 1005 | if (bits_per_mb_at_this_q <= target_norm_bits_per_mb) |
| 1006 | break; |
| 1007 | } |
| 1008 | |
| 1009 | return Q; |
| 1010 | } |
| 1011 | |
| 1012 | // Estimate a worst case Q for a KF group |
| 1013 | static int estimate_kf_group_q(VP8_COMP *cpi, double section_err, int section_target_bandwitdh, int Height, int Width, double group_iiratio) |
| 1014 | { |
| 1015 | int Q; |
| 1016 | int num_mbs = ((Height * Width) / (16 * 16)); |
| 1017 | int target_norm_bits_per_mb = (512 * section_target_bandwitdh) / num_mbs; |
| 1018 | int bits_per_mb_at_this_q; |
| 1019 | |
| 1020 | double err_per_mb = section_err / num_mbs; |
| 1021 | double err_correction_factor; |
| 1022 | double corr_high; |
| 1023 | double speed_correction = 1.0; |
| 1024 | double current_spend_ratio = 1.0; |
| 1025 | |
| 1026 | double pow_highq = (POW1 < 0.6) ? POW1 + 0.3 : 0.90; |
| 1027 | double pow_lowq = (POW1 < 0.7) ? POW1 + 0.1 : 0.80; |
| 1028 | |
| 1029 | double iiratio_correction_factor = 1.0; |
| 1030 | |
| 1031 | double combined_correction_factor; |
| 1032 | |
| 1033 | // Trap special case where the target is <= 0 |
| 1034 | if (target_norm_bits_per_mb <= 0) |
| 1035 | return MAXQ * 2; |
| 1036 | |
| 1037 | // Calculate a corrective factor based on a rolling ratio of bits spent vs target bits |
| 1038 | // This is clamped to the range 0.1 to 10.0 |
| 1039 | if (cpi->long_rolling_target_bits <= 0) |
| 1040 | current_spend_ratio = 10.0; |
| 1041 | else |
| 1042 | { |
| 1043 | current_spend_ratio = (double)cpi->long_rolling_actual_bits / (double)cpi->long_rolling_target_bits; |
| 1044 | current_spend_ratio = (current_spend_ratio > 10.0) ? 10.0 : (current_spend_ratio < 0.1) ? 0.1 : current_spend_ratio; |
| 1045 | } |
| 1046 | |
| 1047 | // Calculate a correction factor based on the quality of prediction in the sequence as indicated by intra_inter error score ratio (IIRatio) |
| 1048 | // The idea here is to favour subsampling in the hardest sections vs the easyest. |
| 1049 | iiratio_correction_factor = 1.0 - ((group_iiratio - 6.0) * 0.1); |
| 1050 | |
| 1051 | if (iiratio_correction_factor < 0.5) |
| 1052 | iiratio_correction_factor = 0.5; |
| 1053 | |
| 1054 | // Corrections for higher compression speed settings (reduced compression expected) |
| 1055 | if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1)) |
| 1056 | { |
| 1057 | if (cpi->oxcf.cpu_used <= 5) |
| 1058 | speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04); |
| 1059 | else |
| 1060 | speed_correction = 1.25; |
| 1061 | } |
| 1062 | |
| 1063 | // Combine the various factors calculated above |
| 1064 | combined_correction_factor = speed_correction * iiratio_correction_factor * current_spend_ratio; |
| 1065 | |
| 1066 | // Correction factor used for Q values >= 20 |
| 1067 | corr_high = pow(err_per_mb / BASE_ERRPERMB, pow_highq); |
| 1068 | corr_high = (corr_high < 0.05) ? 0.05 : (corr_high > 5.0) ? 5.0 : corr_high; |
| 1069 | |
| 1070 | // Try and pick a Q that should be high enough to encode the content at the given rate. |
| 1071 | for (Q = 0; Q < MAXQ; Q++) |
| 1072 | { |
| 1073 | // Q values < 20 treated as a special case |
| 1074 | if (Q < 20) |
| 1075 | { |
| 1076 | err_correction_factor = pow(err_per_mb / BASE_ERRPERMB, (pow_lowq + Q * 0.01)); |
| 1077 | err_correction_factor = (err_correction_factor < 0.05) ? 0.05 : (err_correction_factor > 5.0) ? 5.0 : err_correction_factor; |
| 1078 | } |
| 1079 | else |
| 1080 | err_correction_factor = corr_high; |
| 1081 | |
| 1082 | bits_per_mb_at_this_q = (int)(.5 + err_correction_factor * combined_correction_factor * (double)vp8_bits_per_mb[INTER_FRAME][Q]); |
| 1083 | |
| 1084 | if (bits_per_mb_at_this_q <= target_norm_bits_per_mb) |
| 1085 | break; |
| 1086 | } |
| 1087 | |
| 1088 | // If we could not hit the target even at Max Q then estimate what Q would have bee required |
| 1089 | while ((bits_per_mb_at_this_q > target_norm_bits_per_mb) && (Q < (MAXQ * 2))) |
| 1090 | { |
| 1091 | |
| 1092 | bits_per_mb_at_this_q = (int)(0.96 * bits_per_mb_at_this_q); |
| 1093 | Q++; |
| 1094 | } |
| 1095 | |
| 1096 | if (0) |
| 1097 | { |
| 1098 | FILE *f = fopen("estkf_q.stt", "a"); |
| 1099 | 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, |
| 1100 | target_norm_bits_per_mb, err_per_mb, err_correction_factor, |
| 1101 | current_spend_ratio, group_iiratio, iiratio_correction_factor, |
| 1102 | (double)cpi->buffer_level / (double)cpi->oxcf.optimal_buffer_level, Q); |
| 1103 | fclose(f); |
| 1104 | } |
| 1105 | |
| 1106 | return Q; |
| 1107 | } |
| 1108 | extern void vp8_new_frame_rate(VP8_COMP *cpi, double framerate); |
| 1109 | |
| 1110 | void vp8_init_second_pass(VP8_COMP *cpi) |
| 1111 | { |
| 1112 | FIRSTPASS_STATS this_frame; |
| 1113 | FIRSTPASS_STATS *start_pos; |
| 1114 | |
| 1115 | double two_pass_min_rate = (double)(cpi->oxcf.target_bandwidth * cpi->oxcf.two_pass_vbrmin_section / 100); |
| 1116 | |
| 1117 | vp8_zero_stats(&cpi->total_stats); |
| 1118 | |
| 1119 | if (!cpi->stats_in_end) |
| 1120 | return; |
| 1121 | |
| 1122 | cpi->total_stats = *cpi->stats_in_end; |
| 1123 | |
| 1124 | cpi->total_error_left = cpi->total_stats.ssim_weighted_pred_err; |
| 1125 | cpi->total_intra_error_left = cpi->total_stats.intra_error; |
| 1126 | cpi->total_coded_error_left = cpi->total_stats.coded_error; |
| 1127 | cpi->start_tot_err_left = cpi->total_error_left; |
| 1128 | |
| 1129 | //cpi->bits_left = (long long)(cpi->total_stats.count * cpi->oxcf.target_bandwidth / DOUBLE_DIVIDE_CHECK((double)cpi->oxcf.frame_rate)); |
| 1130 | //cpi->bits_left -= (long long)(cpi->total_stats.count * two_pass_min_rate / DOUBLE_DIVIDE_CHECK((double)cpi->oxcf.frame_rate)); |
| 1131 | |
| 1132 | // each frame can have a different duration, as the frame rate in the source |
| 1133 | // isn't guaranteed to be constant. The frame rate prior to the first frame |
| 1134 | // encoded in the second pass is a guess. However the sum duration is not. |
| 1135 | // Its calculated based on the actual durations of all frames from the first |
| 1136 | // pass. |
| 1137 | vp8_new_frame_rate(cpi, 10000000.0 * cpi->total_stats.count / cpi->total_stats.duration); |
| 1138 | |
| 1139 | cpi->output_frame_rate = cpi->oxcf.frame_rate; |
| 1140 | cpi->bits_left = (long long)(cpi->total_stats.duration * cpi->oxcf.target_bandwidth / 10000000.0) ; |
| 1141 | cpi->bits_left -= (long long)(cpi->total_stats.duration * two_pass_min_rate / 10000000.0); |
| 1142 | |
| 1143 | vp8_avg_stats(&cpi->total_stats); |
| 1144 | |
| 1145 | // Scan the first pass file and calculate an average Intra / Inter error score ratio for the sequence |
| 1146 | { |
| 1147 | double sum_iiratio = 0.0; |
| 1148 | double IIRatio; |
| 1149 | |
| 1150 | start_pos = cpi->stats_in; // Note starting "file" position |
| 1151 | |
| 1152 | while (vp8_input_stats(cpi, &this_frame) != EOF) |
| 1153 | { |
| 1154 | IIRatio = this_frame.intra_error / DOUBLE_DIVIDE_CHECK(this_frame.coded_error); |
| 1155 | IIRatio = (IIRatio < 1.0) ? 1.0 : (IIRatio > 20.0) ? 20.0 : IIRatio; |
| 1156 | sum_iiratio += IIRatio; |
| 1157 | } |
| 1158 | |
| 1159 | cpi->avg_iiratio = sum_iiratio / DOUBLE_DIVIDE_CHECK((double)cpi->total_stats.count); |
| 1160 | |
| 1161 | // Reset file position |
| 1162 | reset_fpf_position(cpi, start_pos); |
| 1163 | } |
| 1164 | |
| 1165 | // Scan the first pass file and calculate a modified total error based upon the bias/power function |
| 1166 | // used to allocate bits |
| 1167 | { |
| 1168 | start_pos = cpi->stats_in; // Note starting "file" position |
| 1169 | |
| 1170 | cpi->modified_total_error_left = 0.0; |
| 1171 | |
| 1172 | while (vp8_input_stats(cpi, &this_frame) != EOF) |
| 1173 | { |
| 1174 | cpi->modified_total_error_left += calculate_modified_err(cpi, &this_frame); |
| 1175 | } |
| 1176 | |
| 1177 | reset_fpf_position(cpi, start_pos); // Reset file position |
| 1178 | |
| 1179 | } |
| 1180 | |
| 1181 | #ifdef FIRSTPASS_MM |
| 1182 | cpi->fp_motion_mapfile = 0; |
| 1183 | cpi->fp_motion_mapfile = fopen("fpmotionmap.stt", "rb"); |
| 1184 | #endif |
| 1185 | |
| 1186 | } |
| 1187 | |
| 1188 | void vp8_end_second_pass(VP8_COMP *cpi) |
| 1189 | { |
| 1190 | #ifdef FIRSTPASS_MM |
| 1191 | |
| 1192 | if (cpi->fp_motion_mapfile) |
| 1193 | fclose(cpi->fp_motion_mapfile); |
| 1194 | |
| 1195 | #endif |
| 1196 | } |
| 1197 | |
| 1198 | // Analyse and define a gf/arf group . |
| 1199 | static void define_gf_group(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame) |
| 1200 | { |
| 1201 | FIRSTPASS_STATS next_frame; |
| 1202 | FIRSTPASS_STATS *start_pos; |
| 1203 | int i; |
Fritz Koenig | 0ce3901 | 2010-07-22 08:07:32 -0400 | [diff] [blame] | 1204 | int y_width = cpi->common.yv12_fb[cpi->common.lst_fb_idx].y_width; |
| 1205 | int y_height = cpi->common.yv12_fb[cpi->common.lst_fb_idx].y_height; |
| 1206 | int image_size = y_width * y_height; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1207 | double boost_score = 0.0; |
| 1208 | double old_boost_score = 0.0; |
| 1209 | double gf_group_err = 0.0; |
| 1210 | double gf_first_frame_err = 0.0; |
| 1211 | double mod_frame_err = 0.0; |
| 1212 | |
| 1213 | double mv_accumulator_rabs = 0.0; |
| 1214 | double mv_accumulator_cabs = 0.0; |
| 1215 | double this_mv_rabs; |
| 1216 | double this_mv_cabs; |
| 1217 | double mv_ratio_accumulator = 0.0; |
| 1218 | double distance_factor = 0.0; |
| 1219 | double decay_accumulator = 1.0; |
| 1220 | |
| 1221 | double boost_factor = IIFACTOR; |
| 1222 | double loop_decay_rate = 1.00; // Starting decay rate |
| 1223 | |
| 1224 | double this_frame_mv_in_out = 0.0; |
| 1225 | double mv_in_out_accumulator = 0.0; |
| 1226 | double abs_mv_in_out_accumulator = 0.0; |
| 1227 | double mod_err_per_mb_accumulator = 0.0; |
| 1228 | |
| 1229 | int max_bits = frame_max_bits(cpi); // Max for a single frame |
| 1230 | |
| 1231 | #ifdef FIRSTPASS_MM |
| 1232 | int fpmm_pos; |
| 1233 | #endif |
| 1234 | |
| 1235 | cpi->gf_group_bits = 0; |
| 1236 | cpi->gf_decay_rate = 0; |
| 1237 | |
| 1238 | vp8_clear_system_state(); //__asm emms; |
| 1239 | |
| 1240 | #ifdef FIRSTPASS_MM |
| 1241 | fpmm_pos = vp8_fpmm_get_pos(cpi); |
| 1242 | #endif |
| 1243 | |
| 1244 | start_pos = cpi->stats_in; |
| 1245 | |
Guillermo Ballester Valor | 5a72620 | 2010-06-11 14:33:49 -0400 | [diff] [blame] | 1246 | vpx_memset(&next_frame, 0, sizeof(next_frame)); // assure clean |
| 1247 | |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1248 | // Preload the stats for the next frame. |
| 1249 | mod_frame_err = calculate_modified_err(cpi, this_frame); |
| 1250 | |
| 1251 | // Note the error of the frame at the start of the group (this will be the GF frame error if we code a normal gf |
| 1252 | gf_first_frame_err = mod_frame_err; |
| 1253 | |
| 1254 | // Special treatment if the current frame is a key frame (which is also a gf). |
| 1255 | // If it is then its error score (and hence bit allocation) need to be subtracted out |
| 1256 | // from the calculation for the GF group |
| 1257 | if (cpi->common.frame_type == KEY_FRAME) |
| 1258 | gf_group_err -= gf_first_frame_err; |
| 1259 | |
| 1260 | // Scan forward to try and work out how many frames the next gf group should contain and |
| 1261 | // what level of boost is appropriate for the GF or ARF that will be coded with the group |
| 1262 | i = 0; |
| 1263 | |
| 1264 | while (((i < cpi->max_gf_interval) || ((cpi->frames_to_key - i) < MIN_GF_INTERVAL)) && (i < cpi->frames_to_key)) |
| 1265 | { |
| 1266 | double r; |
| 1267 | double motion_factor; |
| 1268 | double this_frame_mvr_ratio; |
| 1269 | double this_frame_mvc_ratio; |
| 1270 | |
| 1271 | i++; // Increment the loop counter |
| 1272 | |
| 1273 | // Accumulate error score of frames in this gf group |
| 1274 | mod_frame_err = calculate_modified_err(cpi, this_frame); |
| 1275 | |
| 1276 | gf_group_err += mod_frame_err; |
| 1277 | |
| 1278 | mod_err_per_mb_accumulator += mod_frame_err / DOUBLE_DIVIDE_CHECK((double)cpi->common.MBs); |
| 1279 | |
| 1280 | if (EOF == vp8_input_stats(cpi, &next_frame)) |
| 1281 | break; |
| 1282 | |
| 1283 | // Accumulate motion stats. |
| 1284 | motion_factor = next_frame.pcnt_motion; |
| 1285 | this_mv_rabs = fabs(next_frame.mvr_abs * motion_factor); |
| 1286 | this_mv_cabs = fabs(next_frame.mvc_abs * motion_factor); |
| 1287 | |
| 1288 | mv_accumulator_rabs += fabs(next_frame.mvr_abs * motion_factor); |
| 1289 | mv_accumulator_cabs += fabs(next_frame.mvc_abs * motion_factor); |
| 1290 | |
| 1291 | //Accumulate Motion In/Out of frame stats |
| 1292 | this_frame_mv_in_out = next_frame.mv_in_out_count * next_frame.pcnt_motion; |
| 1293 | mv_in_out_accumulator += next_frame.mv_in_out_count * next_frame.pcnt_motion; |
| 1294 | abs_mv_in_out_accumulator += fabs(next_frame.mv_in_out_count * next_frame.pcnt_motion); |
| 1295 | |
| 1296 | // If there is a significant amount of motion |
| 1297 | if (motion_factor > 0.05) |
| 1298 | { |
| 1299 | this_frame_mvr_ratio = fabs(next_frame.mvr_abs) / DOUBLE_DIVIDE_CHECK(fabs(next_frame.MVr)); |
| 1300 | this_frame_mvc_ratio = fabs(next_frame.mvc_abs) / DOUBLE_DIVIDE_CHECK(fabs(next_frame.MVc)); |
| 1301 | |
| 1302 | mv_ratio_accumulator += (this_frame_mvr_ratio < next_frame.mvr_abs) ? (this_frame_mvr_ratio * motion_factor) : next_frame.mvr_abs * motion_factor; |
| 1303 | mv_ratio_accumulator += (this_frame_mvc_ratio < next_frame.mvc_abs) ? (this_frame_mvc_ratio * motion_factor) : next_frame.mvc_abs * motion_factor; |
| 1304 | } |
| 1305 | else |
| 1306 | { |
| 1307 | mv_ratio_accumulator += 0.0; |
| 1308 | this_frame_mvr_ratio = 1.0; |
| 1309 | this_frame_mvc_ratio = 1.0; |
| 1310 | } |
| 1311 | |
| 1312 | // Underlying boost factor is based on inter intra error ratio |
| 1313 | r = (boost_factor * (next_frame.intra_error / DOUBLE_DIVIDE_CHECK(next_frame.coded_error))); |
| 1314 | |
| 1315 | // Increase boost for frames where new data coming into frame (eg zoom out) |
| 1316 | // Slightly reduce boost if there is a net balance of motion out of the frame (zoom in) |
| 1317 | // The range for this_frame_mv_in_out is -1.0 to +1.0 |
| 1318 | if (this_frame_mv_in_out > 0.0) |
| 1319 | r += r * (this_frame_mv_in_out * 2.0); |
| 1320 | else |
| 1321 | r += r * (this_frame_mv_in_out / 2.0); // In extreme case boost is halved |
| 1322 | |
| 1323 | if (r > GF_RMAX) |
| 1324 | r = GF_RMAX; |
| 1325 | |
| 1326 | // Adjust loop decay rate |
| 1327 | //if ( next_frame.pcnt_inter < loop_decay_rate ) |
| 1328 | loop_decay_rate = next_frame.pcnt_inter; |
| 1329 | |
| 1330 | // High % motion -> somewhat higher decay rate |
| 1331 | if ((1.0 - (next_frame.pcnt_motion / 10.0)) < loop_decay_rate) |
| 1332 | loop_decay_rate = (1.0 - (next_frame.pcnt_motion / 10.0)); |
| 1333 | |
| 1334 | distance_factor = sqrt((this_mv_rabs * this_mv_rabs) + (this_mv_cabs * this_mv_cabs)) / 300.0; |
| 1335 | distance_factor = ((distance_factor > 1.0) ? 0.0 : (1.0 - distance_factor)); |
| 1336 | |
| 1337 | if (distance_factor < loop_decay_rate) |
| 1338 | loop_decay_rate = distance_factor; |
| 1339 | |
| 1340 | // Cumulative effect of decay |
| 1341 | decay_accumulator = decay_accumulator * loop_decay_rate; |
| 1342 | decay_accumulator = decay_accumulator < 0.1 ? 0.1 : decay_accumulator; |
| 1343 | //decay_accumulator = ( loop_decay_rate < decay_accumulator ) ? loop_decay_rate : decay_accumulator; |
| 1344 | |
| 1345 | boost_score += (decay_accumulator * r); |
| 1346 | |
| 1347 | // Break out conditions. |
| 1348 | if ( /* i>4 || */ |
| 1349 | ( |
| 1350 | (i > MIN_GF_INTERVAL) && // Dont break out with a very short interval |
| 1351 | ((cpi->frames_to_key - i) >= MIN_GF_INTERVAL) && // Dont break out very close to a key frame |
| 1352 | ((boost_score > 20.0) || (next_frame.pcnt_inter < 0.75)) && |
| 1353 | ((mv_ratio_accumulator > 100.0) || |
| 1354 | (abs_mv_in_out_accumulator > 3.0) || |
| 1355 | (mv_in_out_accumulator < -2.0) || |
| 1356 | ((boost_score - old_boost_score) < 2.0) |
| 1357 | ) |
| 1358 | ) |
| 1359 | ) |
| 1360 | { |
| 1361 | boost_score = old_boost_score; |
| 1362 | break; |
| 1363 | } |
| 1364 | |
| 1365 | vpx_memcpy(this_frame, &next_frame, sizeof(*this_frame)); |
| 1366 | |
| 1367 | old_boost_score = boost_score; |
| 1368 | } |
| 1369 | |
| 1370 | cpi->gf_decay_rate = (i > 0) ? (int)(100.0 * (1.0 - decay_accumulator)) / i : 0; |
| 1371 | |
| 1372 | // When using CBR apply additional buffer related upper limits |
| 1373 | if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) |
| 1374 | { |
| 1375 | double max_boost; |
| 1376 | |
| 1377 | // For cbr apply buffer related limits |
| 1378 | if (cpi->drop_frames_allowed) |
| 1379 | { |
| 1380 | int df_buffer_level = cpi->oxcf.drop_frames_water_mark * (cpi->oxcf.optimal_buffer_level / 100); |
| 1381 | |
| 1382 | if (cpi->buffer_level > df_buffer_level) |
| 1383 | max_boost = ((double)((cpi->buffer_level - df_buffer_level) * 2 / 3) * 16.0) / DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth); |
| 1384 | else |
| 1385 | max_boost = 0.0; |
| 1386 | } |
| 1387 | else if (cpi->buffer_level > 0) |
| 1388 | { |
| 1389 | max_boost = ((double)(cpi->buffer_level * 2 / 3) * 16.0) / DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth); |
| 1390 | } |
| 1391 | else |
| 1392 | { |
| 1393 | max_boost = 0.0; |
| 1394 | } |
| 1395 | |
| 1396 | if (boost_score > max_boost) |
| 1397 | boost_score = max_boost; |
| 1398 | } |
| 1399 | |
| 1400 | cpi->gfu_boost = (int)(boost_score * 100.0) >> 4; |
| 1401 | |
| 1402 | // Should we use the alternate refernce frame |
| 1403 | if (cpi->oxcf.play_alternate && |
| 1404 | (i >= MIN_GF_INTERVAL) && |
| 1405 | (i <= (cpi->frames_to_key - MIN_GF_INTERVAL)) && // dont use ARF very near next kf |
| 1406 | (((next_frame.pcnt_inter > 0.75) && |
| 1407 | ((mv_in_out_accumulator / (double)i > -0.2) || (mv_in_out_accumulator > -2.0)) && |
| 1408 | //(cpi->gfu_boost>150) && |
| 1409 | (cpi->gfu_boost > 100) && |
| 1410 | //(cpi->gfu_boost>AF_THRESH2) && |
| 1411 | //((cpi->gfu_boost/i)>AF_THRESH) && |
| 1412 | //(decay_accumulator > 0.5) && |
| 1413 | (cpi->gf_decay_rate <= (ARF_DECAY_THRESH + (cpi->gfu_boost / 200))) |
| 1414 | ) |
| 1415 | ) |
| 1416 | ) |
| 1417 | { |
| 1418 | int Boost; |
| 1419 | int allocation_chunks; |
| 1420 | int Q = (cpi->oxcf.fixed_q < 0) ? cpi->last_q[INTER_FRAME] : cpi->oxcf.fixed_q; |
| 1421 | int tmp_q; |
| 1422 | int arf_frame_bits = 0; |
| 1423 | int group_bits; |
| 1424 | |
| 1425 | // Estimate the bits to be allocated to the group as a whole |
| 1426 | if ((cpi->kf_group_bits > 0) && (cpi->kf_group_error_left > 0)) |
| 1427 | group_bits = (int)((double)cpi->kf_group_bits * (gf_group_err / (double)cpi->kf_group_error_left)); |
| 1428 | else |
| 1429 | group_bits = 0; |
| 1430 | |
| 1431 | // Boost for arf frame |
| 1432 | Boost = (cpi->gfu_boost * 3 * GFQ_ADJUSTMENT) / (2 * 100); |
| 1433 | Boost += (cpi->baseline_gf_interval * 50); |
| 1434 | allocation_chunks = (i * 100) + Boost; |
| 1435 | |
| 1436 | // Normalize Altboost and allocations chunck down to prevent overflow |
| 1437 | while (Boost > 1000) |
| 1438 | { |
| 1439 | Boost /= 2; |
| 1440 | allocation_chunks /= 2; |
| 1441 | } |
| 1442 | |
| 1443 | // Calculate the number of bits to be spent on the arf based on the boost number |
| 1444 | arf_frame_bits = (int)((double)Boost * (group_bits / (double)allocation_chunks)); |
| 1445 | |
| 1446 | // Estimate if there are enough bits available to make worthwhile use of an arf. |
| 1447 | tmp_q = estimate_q(cpi, mod_frame_err, (int)arf_frame_bits, cpi->common.Height, cpi->common.Width); |
| 1448 | |
| 1449 | // Only use an arf if it is likely we will be able to code it at a lower Q than the surrounding frames. |
| 1450 | if (tmp_q < cpi->worst_quality) |
| 1451 | { |
| 1452 | cpi->source_alt_ref_pending = TRUE; |
| 1453 | |
| 1454 | // For alt ref frames the error score for the end frame of the group (the alt ref frame) should not contribute to the group total and hence |
| 1455 | // the number of bit allocated to the group. Rather it forms part of the next group (it is the GF at the start of the next group) |
| 1456 | gf_group_err -= mod_frame_err; |
| 1457 | |
| 1458 | // Set the interval till the next gf or arf. For ARFs this is the number of frames to be coded before the future frame that is coded as an ARF. |
| 1459 | // The future frame itself is part of the next group |
| 1460 | cpi->baseline_gf_interval = i - 1; |
| 1461 | |
| 1462 | #ifdef FIRSTPASS_MM |
| 1463 | // Read through the motion map to load up the entry for the ARF |
| 1464 | { |
| 1465 | int j; |
| 1466 | |
| 1467 | // Advance to the region of interest |
| 1468 | // Current default 2 frames before to 2 frames after the ARF frame itsef |
| 1469 | vp8_fpmm_reset_pos(cpi, cpi->fpmm_pos); |
| 1470 | |
| 1471 | for (j = 0; j < cpi->baseline_gf_interval - 2; j++) |
| 1472 | vp8_advance_fpmm(cpi, 1); |
| 1473 | |
| 1474 | // Read / create a motion map for the region of interest |
| 1475 | vp8_input_fpmm(cpi, 5); |
| 1476 | } |
| 1477 | #endif |
| 1478 | } |
| 1479 | else |
| 1480 | { |
| 1481 | cpi->source_alt_ref_pending = FALSE; |
| 1482 | cpi->baseline_gf_interval = i; |
| 1483 | } |
| 1484 | } |
| 1485 | else |
| 1486 | { |
| 1487 | cpi->source_alt_ref_pending = FALSE; |
| 1488 | cpi->baseline_gf_interval = i; |
| 1489 | } |
| 1490 | |
| 1491 | // Conventional GF |
| 1492 | if (!cpi->source_alt_ref_pending) |
| 1493 | { |
| 1494 | // Dont allow conventional gf too near the next kf |
| 1495 | if ((cpi->frames_to_key - cpi->baseline_gf_interval) < MIN_GF_INTERVAL) |
| 1496 | { |
| 1497 | while (cpi->baseline_gf_interval < cpi->frames_to_key) |
| 1498 | { |
| 1499 | if (EOF == vp8_input_stats(cpi, this_frame)) |
| 1500 | break; |
| 1501 | |
| 1502 | cpi->baseline_gf_interval++; |
| 1503 | |
| 1504 | if (cpi->baseline_gf_interval < cpi->frames_to_key) |
| 1505 | gf_group_err += calculate_modified_err(cpi, this_frame); |
| 1506 | } |
| 1507 | } |
| 1508 | } |
| 1509 | |
| 1510 | // Now decide how many bits should be allocated to the GF group as a proportion of those remaining in the kf group. |
| 1511 | // The final key frame group in the clip is treated as a special case where cpi->kf_group_bits is tied to cpi->bits_left. |
| 1512 | // This is also important for short clips where there may only be one key frame. |
| 1513 | if (cpi->frames_to_key >= (int)(cpi->total_stats.count - cpi->common.current_video_frame)) |
| 1514 | { |
| 1515 | cpi->kf_group_bits = (cpi->bits_left > 0) ? cpi->bits_left : 0; |
| 1516 | } |
| 1517 | |
| 1518 | // Calculate the bits to be allocated to the group as a whole |
| 1519 | if ((cpi->kf_group_bits > 0) && (cpi->kf_group_error_left > 0)) |
| 1520 | cpi->gf_group_bits = (int)((double)cpi->kf_group_bits * (gf_group_err / (double)cpi->kf_group_error_left)); |
| 1521 | else |
| 1522 | cpi->gf_group_bits = 0; |
| 1523 | |
| 1524 | cpi->gf_group_bits = (cpi->gf_group_bits < 0) ? 0 : (cpi->gf_group_bits > cpi->kf_group_bits) ? cpi->kf_group_bits : cpi->gf_group_bits; |
| 1525 | |
| 1526 | // Clip cpi->gf_group_bits based on user supplied data rate variability limit (cpi->oxcf.two_pass_vbrmax_section) |
| 1527 | if (cpi->gf_group_bits > max_bits * cpi->baseline_gf_interval) |
| 1528 | cpi->gf_group_bits = max_bits * cpi->baseline_gf_interval; |
| 1529 | |
| 1530 | // Reset the file position |
| 1531 | reset_fpf_position(cpi, start_pos); |
| 1532 | |
| 1533 | // Assign bits to the arf or gf. |
| 1534 | { |
| 1535 | int Boost; |
| 1536 | int frames_in_section; |
| 1537 | int allocation_chunks; |
| 1538 | int Q = (cpi->oxcf.fixed_q < 0) ? cpi->last_q[INTER_FRAME] : cpi->oxcf.fixed_q; |
| 1539 | |
| 1540 | // For ARF frames |
| 1541 | if (cpi->source_alt_ref_pending) |
| 1542 | { |
| 1543 | Boost = (cpi->gfu_boost * 3 * GFQ_ADJUSTMENT) / (2 * 100); |
| 1544 | //Boost += (cpi->baseline_gf_interval * 25); |
| 1545 | Boost += (cpi->baseline_gf_interval * 50); |
| 1546 | |
| 1547 | // Set max and minimum boost and hence minimum allocation |
| 1548 | if (Boost > ((cpi->baseline_gf_interval + 1) * 200)) |
| 1549 | Boost = ((cpi->baseline_gf_interval + 1) * 200); |
| 1550 | else if (Boost < 125) |
| 1551 | Boost = 125; |
| 1552 | |
| 1553 | frames_in_section = cpi->baseline_gf_interval + 1; |
| 1554 | allocation_chunks = (frames_in_section * 100) + Boost; |
| 1555 | } |
| 1556 | // Else for standard golden frames |
| 1557 | else |
| 1558 | { |
| 1559 | // boost based on inter / intra ratio of subsequent frames |
| 1560 | Boost = (cpi->gfu_boost * GFQ_ADJUSTMENT) / 100; |
| 1561 | |
| 1562 | // Set max and minimum boost and hence minimum allocation |
| 1563 | if (Boost > (cpi->baseline_gf_interval * 150)) |
| 1564 | Boost = (cpi->baseline_gf_interval * 150); |
| 1565 | else if (Boost < 125) |
| 1566 | Boost = 125; |
| 1567 | |
| 1568 | frames_in_section = cpi->baseline_gf_interval; |
| 1569 | allocation_chunks = (frames_in_section * 100) + (Boost - 100); |
| 1570 | } |
| 1571 | |
| 1572 | // Normalize Altboost and allocations chunck down to prevent overflow |
| 1573 | while (Boost > 1000) |
| 1574 | { |
| 1575 | Boost /= 2; |
| 1576 | allocation_chunks /= 2; |
| 1577 | } |
| 1578 | |
| 1579 | // Calculate the number of bits to be spent on the gf or arf based on the boost number |
| 1580 | cpi->gf_bits = (int)((double)Boost * (cpi->gf_group_bits / (double)allocation_chunks)); |
| 1581 | |
Paul Wilkins | 9404c7d | 2010-07-23 17:01:12 +0100 | [diff] [blame] | 1582 | // If the frame that is to be boosted is simpler than the average for |
| 1583 | // the gf/arf group then use an alternative calculation |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1584 | // based on the error score of the frame itself |
| 1585 | if (mod_frame_err < gf_group_err / (double)cpi->baseline_gf_interval) |
| 1586 | { |
| 1587 | double alt_gf_grp_bits; |
| 1588 | int alt_gf_bits; |
| 1589 | |
Paul Wilkins | 9404c7d | 2010-07-23 17:01:12 +0100 | [diff] [blame] | 1590 | alt_gf_grp_bits = |
| 1591 | (double)cpi->kf_group_bits * |
| 1592 | (mod_frame_err * (double)cpi->baseline_gf_interval) / |
| 1593 | DOUBLE_DIVIDE_CHECK((double)cpi->kf_group_error_left); |
| 1594 | |
| 1595 | alt_gf_bits = (int)((double)Boost * (alt_gf_grp_bits / |
| 1596 | (double)allocation_chunks)); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1597 | |
| 1598 | if (cpi->gf_bits > alt_gf_bits) |
| 1599 | { |
| 1600 | cpi->gf_bits = alt_gf_bits; |
| 1601 | } |
| 1602 | } |
Paul Wilkins | 9404c7d | 2010-07-23 17:01:12 +0100 | [diff] [blame] | 1603 | // Else if it is harder than other frames in the group make sure it at |
| 1604 | // least receives an allocation in keeping with its relative error |
| 1605 | // score, otherwise it may be worse off than an "un-boosted" frame |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1606 | else |
| 1607 | { |
Paul Wilkins | 9404c7d | 2010-07-23 17:01:12 +0100 | [diff] [blame] | 1608 | int alt_gf_bits = |
| 1609 | (int)((double)cpi->kf_group_bits * |
| 1610 | mod_frame_err / |
| 1611 | DOUBLE_DIVIDE_CHECK((double)cpi->kf_group_error_left)); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1612 | |
| 1613 | if (alt_gf_bits > cpi->gf_bits) |
| 1614 | { |
| 1615 | cpi->gf_bits = alt_gf_bits; |
| 1616 | } |
| 1617 | } |
| 1618 | |
| 1619 | // Apply an additional limit for CBR |
| 1620 | if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) |
| 1621 | { |
| 1622 | if (cpi->gf_bits > (cpi->buffer_level >> 1)) |
| 1623 | cpi->gf_bits = cpi->buffer_level >> 1; |
| 1624 | } |
| 1625 | |
| 1626 | // Dont allow a negative value for gf_bits |
| 1627 | if (cpi->gf_bits < 0) |
| 1628 | cpi->gf_bits = 0; |
| 1629 | |
| 1630 | // Adjust KF group bits and error remainin |
| 1631 | cpi->kf_group_error_left -= gf_group_err; |
| 1632 | cpi->kf_group_bits -= cpi->gf_group_bits; |
| 1633 | |
| 1634 | if (cpi->kf_group_bits < 0) |
| 1635 | cpi->kf_group_bits = 0; |
| 1636 | |
| 1637 | // Note the error score left in the remaining frames of the group. |
| 1638 | // For normal GFs we want to remove the error score for the first frame of the group (except in Key frame case where this has already happened) |
| 1639 | if (!cpi->source_alt_ref_pending && cpi->common.frame_type != KEY_FRAME) |
| 1640 | cpi->gf_group_error_left = gf_group_err - gf_first_frame_err; |
| 1641 | else |
| 1642 | cpi->gf_group_error_left = gf_group_err; |
| 1643 | |
| 1644 | cpi->gf_group_bits -= cpi->gf_bits; |
| 1645 | |
| 1646 | if (cpi->gf_group_bits < 0) |
| 1647 | cpi->gf_group_bits = 0; |
| 1648 | |
| 1649 | // Set aside some bits for a mid gf sequence boost |
| 1650 | if ((cpi->gfu_boost > 150) && (cpi->baseline_gf_interval > 5)) |
| 1651 | { |
| 1652 | int pct_extra = (cpi->gfu_boost - 100) / 50; |
| 1653 | pct_extra = (pct_extra > 10) ? 10 : pct_extra; |
| 1654 | |
| 1655 | cpi->mid_gf_extra_bits = (cpi->gf_group_bits * pct_extra) / 100; |
| 1656 | cpi->gf_group_bits -= cpi->mid_gf_extra_bits; |
| 1657 | } |
| 1658 | else |
| 1659 | cpi->mid_gf_extra_bits = 0; |
| 1660 | |
| 1661 | cpi->gf_bits += cpi->min_frame_bandwidth; // Add in minimum for a frame |
| 1662 | } |
| 1663 | |
| 1664 | if (!cpi->source_alt_ref_pending && (cpi->common.frame_type != KEY_FRAME)) // Normal GF and not a KF |
| 1665 | { |
| 1666 | cpi->per_frame_bandwidth = cpi->gf_bits; // Per frame bit target for this frame |
| 1667 | } |
| 1668 | |
| 1669 | // Adjustment to estimate_max_q based on a measure of complexity of the section |
| 1670 | if (cpi->common.frame_type != KEY_FRAME) |
| 1671 | { |
| 1672 | FIRSTPASS_STATS sectionstats; |
| 1673 | double Ratio; |
| 1674 | |
| 1675 | vp8_zero_stats(§ionstats); |
| 1676 | reset_fpf_position(cpi, start_pos); |
| 1677 | |
| 1678 | for (i = 0 ; i < cpi->baseline_gf_interval ; i++) |
| 1679 | { |
| 1680 | vp8_input_stats(cpi, &next_frame); |
| 1681 | vp8_accumulate_stats(§ionstats, &next_frame); |
| 1682 | } |
| 1683 | |
| 1684 | vp8_avg_stats(§ionstats); |
| 1685 | |
| 1686 | if (sectionstats.pcnt_motion < .17) |
| 1687 | cpi->section_is_low_motion = 1; |
| 1688 | else |
| 1689 | cpi->section_is_low_motion = 0; |
| 1690 | |
| 1691 | if (sectionstats.mvc_abs + sectionstats.mvr_abs > 45) |
| 1692 | cpi->section_is_fast_motion = 1; |
| 1693 | else |
| 1694 | cpi->section_is_fast_motion = 0; |
| 1695 | |
| 1696 | cpi->section_intra_rating = sectionstats.intra_error / DOUBLE_DIVIDE_CHECK(sectionstats.coded_error); |
| 1697 | |
| 1698 | Ratio = sectionstats.intra_error / DOUBLE_DIVIDE_CHECK(sectionstats.coded_error); |
| 1699 | //if( (Ratio > 11) ) //&& (sectionstats.pcnt_second_ref < .20) ) |
| 1700 | //{ |
| 1701 | cpi->section_max_qfactor = 1.0 - ((Ratio - 10.0) * 0.025); |
| 1702 | |
| 1703 | if (cpi->section_max_qfactor < 0.80) |
| 1704 | cpi->section_max_qfactor = 0.80; |
| 1705 | |
| 1706 | //} |
| 1707 | //else |
| 1708 | // cpi->section_max_qfactor = 1.0; |
| 1709 | |
| 1710 | reset_fpf_position(cpi, start_pos); |
| 1711 | } |
| 1712 | |
| 1713 | #ifdef FIRSTPASS_MM |
| 1714 | // Reset the First pass motion map file position |
| 1715 | vp8_fpmm_reset_pos(cpi, fpmm_pos); |
| 1716 | #endif |
| 1717 | } |
| 1718 | |
| 1719 | // Allocate bits to a normal frame that is neither a gf an arf or a key frame. |
| 1720 | static void assign_std_frame_bits(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame) |
| 1721 | { |
| 1722 | int target_frame_size; // gf_group_error_left |
| 1723 | |
| 1724 | double modified_err; |
| 1725 | double err_fraction; // What portion of the remaining GF group error is used by this frame |
| 1726 | |
| 1727 | int max_bits = frame_max_bits(cpi); // Max for a single frame |
| 1728 | |
| 1729 | // The final few frames have special treatment |
| 1730 | if (cpi->frames_till_gf_update_due >= (int)(cpi->total_stats.count - cpi->common.current_video_frame)) |
| 1731 | { |
| 1732 | cpi->gf_group_bits = (cpi->bits_left > 0) ? cpi->bits_left : 0;; |
| 1733 | } |
| 1734 | |
| 1735 | // Calculate modified prediction error used in bit allocation |
| 1736 | modified_err = calculate_modified_err(cpi, this_frame); |
| 1737 | |
| 1738 | if (cpi->gf_group_error_left > 0) |
| 1739 | err_fraction = modified_err / cpi->gf_group_error_left; // What portion of the remaining GF group error is used by this frame |
| 1740 | else |
| 1741 | err_fraction = 0.0; |
| 1742 | |
| 1743 | target_frame_size = (int)((double)cpi->gf_group_bits * err_fraction); // How many of those bits available for allocation should we give it? |
| 1744 | |
| 1745 | // Clip to target size to 0 - max_bits (or cpi->gf_group_bits) at the top end. |
| 1746 | if (target_frame_size < 0) |
| 1747 | target_frame_size = 0; |
| 1748 | else |
| 1749 | { |
| 1750 | if (target_frame_size > max_bits) |
| 1751 | target_frame_size = max_bits; |
| 1752 | |
| 1753 | if (target_frame_size > cpi->gf_group_bits) |
| 1754 | target_frame_size = cpi->gf_group_bits; |
| 1755 | } |
| 1756 | |
| 1757 | cpi->gf_group_error_left -= modified_err; // Adjust error remaining |
| 1758 | cpi->gf_group_bits -= target_frame_size; // Adjust bits remaining |
| 1759 | |
| 1760 | if (cpi->gf_group_bits < 0) |
| 1761 | cpi->gf_group_bits = 0; |
| 1762 | |
| 1763 | target_frame_size += cpi->min_frame_bandwidth; // Add in the minimum number of bits that is set aside for every frame. |
| 1764 | |
| 1765 | // Special case for the frame that lies half way between two gfs |
| 1766 | if (cpi->common.frames_since_golden == cpi->baseline_gf_interval / 2) |
| 1767 | target_frame_size += cpi->mid_gf_extra_bits; |
| 1768 | |
| 1769 | cpi->per_frame_bandwidth = target_frame_size; // Per frame bit target for this frame |
| 1770 | } |
| 1771 | |
| 1772 | void vp8_second_pass(VP8_COMP *cpi) |
| 1773 | { |
| 1774 | int tmp_q; |
| 1775 | int frames_left = (int)(cpi->total_stats.count - cpi->common.current_video_frame); |
| 1776 | |
| 1777 | FIRSTPASS_STATS this_frame; |
| 1778 | FIRSTPASS_STATS this_frame_copy; |
| 1779 | |
| 1780 | VP8_COMMON *cm = &cpi->common; |
| 1781 | |
| 1782 | double this_frame_error; |
| 1783 | double this_frame_intra_error; |
| 1784 | double this_frame_coded_error; |
| 1785 | |
| 1786 | FIRSTPASS_STATS *start_pos; |
| 1787 | |
| 1788 | if (!cpi->stats_in) |
| 1789 | { |
| 1790 | return ; |
| 1791 | } |
| 1792 | |
| 1793 | vp8_clear_system_state(); |
| 1794 | |
| 1795 | if (EOF == vp8_input_stats(cpi, &this_frame)) |
| 1796 | return; |
| 1797 | |
| 1798 | #ifdef FIRSTPASS_MM |
| 1799 | vpx_memset(cpi->fp_motion_map, 0, cpi->common.MBs); |
| 1800 | cpi->fpmm_pos = vp8_fpmm_get_pos(cpi); |
| 1801 | vp8_advance_fpmm(cpi, 1); // Read this frame's first pass motion map |
| 1802 | #endif |
| 1803 | |
| 1804 | this_frame_error = this_frame.ssim_weighted_pred_err; |
| 1805 | this_frame_intra_error = this_frame.intra_error; |
| 1806 | this_frame_coded_error = this_frame.coded_error; |
| 1807 | |
| 1808 | // Store information regarding level of motion etc for use mode decisions. |
| 1809 | cpi->motion_speed = (int)(fabs(this_frame.MVr) + fabs(this_frame.MVc)); |
| 1810 | cpi->motion_var = (int)(fabs(this_frame.MVrv) + fabs(this_frame.MVcv)); |
| 1811 | cpi->inter_lvl = (int)(this_frame.pcnt_inter * 100); |
| 1812 | cpi->intra_lvl = (int)((1.0 - this_frame.pcnt_inter) * 100); |
| 1813 | cpi->motion_lvl = (int)(this_frame.pcnt_motion * 100); |
| 1814 | |
| 1815 | start_pos = cpi->stats_in; |
| 1816 | |
| 1817 | // keyframe and section processing ! |
| 1818 | if (cpi->frames_to_key == 0) |
| 1819 | { |
| 1820 | // Define next KF group and assign bits to it |
| 1821 | vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame)); |
| 1822 | vp8_find_next_key_frame(cpi, &this_frame_copy); |
| 1823 | |
| 1824 | // Special case: Error error_resilient_mode mode does not make much sense for two pass but with its current meaning but this code is designed to stop |
| 1825 | // outlandish behaviour if someone does set it when using two pass. It effectively disables GF groups. |
| 1826 | // This is temporary code till we decide what should really happen in this case. |
| 1827 | if (cpi->oxcf.error_resilient_mode) |
| 1828 | { |
| 1829 | cpi->gf_group_bits = cpi->kf_group_bits; |
| 1830 | cpi->gf_group_error_left = cpi->kf_group_error_left; |
| 1831 | cpi->baseline_gf_interval = cpi->frames_to_key; |
| 1832 | cpi->frames_till_gf_update_due = cpi->baseline_gf_interval; |
| 1833 | cpi->source_alt_ref_pending = FALSE; |
| 1834 | } |
| 1835 | |
| 1836 | } |
| 1837 | |
| 1838 | // Is this a GF / ARF (Note that a KF is always also a GF) |
| 1839 | if (cpi->frames_till_gf_update_due == 0) |
| 1840 | { |
| 1841 | // Define next gf group and assign bits to it |
| 1842 | vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame)); |
| 1843 | define_gf_group(cpi, &this_frame_copy); |
| 1844 | |
| 1845 | // If we are going to code an altref frame at the end of the group and the current frame is not a key frame.... |
| 1846 | // If the previous group used an arf this frame has already benefited from that arf boost and it should not be given extra bits |
| 1847 | // If the previous group was NOT coded using arf we may want to apply some boost to this GF as well |
| 1848 | if (cpi->source_alt_ref_pending && (cpi->common.frame_type != KEY_FRAME)) |
| 1849 | { |
| 1850 | // Assign a standard frames worth of bits from those allocated to the GF group |
| 1851 | vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame)); |
| 1852 | assign_std_frame_bits(cpi, &this_frame_copy); |
| 1853 | |
| 1854 | // If appropriate (we are switching into ARF active but it was not previously active) apply a boost for the gf at the start of the group. |
| 1855 | //if ( !cpi->source_alt_ref_active && (cpi->gfu_boost > 150) ) |
| 1856 | if (FALSE) |
| 1857 | { |
| 1858 | int extra_bits; |
| 1859 | int pct_extra = (cpi->gfu_boost - 100) / 50; |
| 1860 | |
| 1861 | pct_extra = (pct_extra > 20) ? 20 : pct_extra; |
| 1862 | |
| 1863 | extra_bits = (cpi->gf_group_bits * pct_extra) / 100; |
| 1864 | cpi->gf_group_bits -= extra_bits; |
| 1865 | cpi->per_frame_bandwidth += extra_bits; |
| 1866 | } |
| 1867 | } |
| 1868 | } |
| 1869 | |
| 1870 | // Otherwise this is an ordinary frame |
| 1871 | else |
| 1872 | { |
| 1873 | // Special case: Error error_resilient_mode mode does not make much sense for two pass but with its current meaning but this code is designed to stop |
| 1874 | // outlandish behaviour if someone does set it when using two pass. It effectively disables GF groups. |
| 1875 | // This is temporary code till we decide what should really happen in this case. |
| 1876 | if (cpi->oxcf.error_resilient_mode) |
| 1877 | { |
| 1878 | cpi->frames_till_gf_update_due = cpi->frames_to_key; |
| 1879 | |
| 1880 | if (cpi->common.frame_type != KEY_FRAME) |
| 1881 | { |
| 1882 | // Assign bits from those allocated to the GF group |
| 1883 | vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame)); |
| 1884 | assign_std_frame_bits(cpi, &this_frame_copy); |
| 1885 | } |
| 1886 | } |
| 1887 | else |
| 1888 | { |
| 1889 | // Assign bits from those allocated to the GF group |
| 1890 | vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame)); |
| 1891 | assign_std_frame_bits(cpi, &this_frame_copy); |
| 1892 | } |
| 1893 | } |
| 1894 | |
Paul Wilkins | a04ed23 | 2010-06-09 15:03:48 +0100 | [diff] [blame] | 1895 | // Keep a globally available copy of this and the next frame's iiratio. |
| 1896 | cpi->this_iiratio = this_frame_intra_error / |
| 1897 | DOUBLE_DIVIDE_CHECK(this_frame_coded_error); |
Paul Wilkins | 28de670 | 2010-06-07 17:34:46 +0100 | [diff] [blame] | 1898 | { |
| 1899 | FIRSTPASS_STATS next_frame; |
| 1900 | if ( lookup_next_frame_stats(cpi, &next_frame) != EOF ) |
Paul Wilkins | a04ed23 | 2010-06-09 15:03:48 +0100 | [diff] [blame] | 1901 | { |
| 1902 | cpi->next_iiratio = next_frame.intra_error / |
| 1903 | DOUBLE_DIVIDE_CHECK(next_frame.coded_error); |
| 1904 | } |
Paul Wilkins | 28de670 | 2010-06-07 17:34:46 +0100 | [diff] [blame] | 1905 | } |
| 1906 | |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1907 | // Set nominal per second bandwidth for this frame |
| 1908 | cpi->target_bandwidth = cpi->per_frame_bandwidth * cpi->output_frame_rate; |
| 1909 | if (cpi->target_bandwidth < 0) |
| 1910 | cpi->target_bandwidth = 0; |
| 1911 | |
| 1912 | if (cpi->common.current_video_frame == 0) |
| 1913 | { |
| 1914 | // guess at 2nd pass q |
| 1915 | cpi->est_max_qcorrection_factor = 1.0; |
| 1916 | tmp_q = estimate_max_q(cpi, (cpi->total_coded_error_left / frames_left), (int)(cpi->bits_left / frames_left), cpi->common.Height, cpi->common.Width); |
| 1917 | |
| 1918 | if (tmp_q < cpi->worst_quality) |
| 1919 | { |
| 1920 | cpi->active_worst_quality = tmp_q; |
| 1921 | cpi->ni_av_qi = tmp_q; |
| 1922 | } |
| 1923 | else |
| 1924 | { |
| 1925 | cpi->active_worst_quality = cpi->worst_quality; |
| 1926 | cpi->ni_av_qi = cpi->worst_quality; |
| 1927 | } |
| 1928 | } |
| 1929 | else |
| 1930 | { |
| 1931 | if (frames_left < 1) |
| 1932 | frames_left = 1; |
| 1933 | |
| 1934 | tmp_q = estimate_max_q(cpi, (cpi->total_coded_error_left / frames_left), (int)(cpi->bits_left / frames_left), cpi->common.Height, cpi->common.Width); |
| 1935 | |
| 1936 | // Move active_worst_quality but in a damped way |
| 1937 | if (tmp_q > cpi->active_worst_quality) |
| 1938 | cpi->active_worst_quality ++; |
| 1939 | else if (tmp_q < cpi->active_worst_quality) |
| 1940 | cpi->active_worst_quality --; |
| 1941 | |
| 1942 | cpi->active_worst_quality = ((cpi->active_worst_quality * 3) + tmp_q + 2) / 4; |
| 1943 | |
| 1944 | // Clamp to user set limits |
| 1945 | if (cpi->active_worst_quality > cpi->worst_quality) |
| 1946 | cpi->active_worst_quality = cpi->worst_quality; |
| 1947 | else if (cpi->active_worst_quality < cpi->best_quality) |
| 1948 | cpi->active_worst_quality = cpi->best_quality; |
| 1949 | |
| 1950 | } |
| 1951 | |
| 1952 | cpi->frames_to_key --; |
| 1953 | cpi->total_error_left -= this_frame_error; |
| 1954 | cpi->total_intra_error_left -= this_frame_intra_error; |
| 1955 | cpi->total_coded_error_left -= this_frame_coded_error; |
| 1956 | } |
| 1957 | |
| 1958 | |
| 1959 | static BOOL test_candidate_kf(VP8_COMP *cpi, FIRSTPASS_STATS *last_frame, FIRSTPASS_STATS *this_frame, FIRSTPASS_STATS *next_frame) |
| 1960 | { |
| 1961 | BOOL is_viable_kf = FALSE; |
| 1962 | |
| 1963 | // Does the frame satisfy the primary criteria of a key frame |
| 1964 | // If so, then examine how well it predicts subsequent frames |
| 1965 | if ((this_frame->pcnt_second_ref < 0.10) && |
| 1966 | (next_frame->pcnt_second_ref < 0.10) && |
| 1967 | ((this_frame->pcnt_inter < 0.05) || |
| 1968 | ( |
| 1969 | (this_frame->pcnt_inter < .25) && |
| 1970 | ((this_frame->intra_error / DOUBLE_DIVIDE_CHECK(this_frame->coded_error)) < 2.5) && |
| 1971 | ((fabs(last_frame->coded_error - this_frame->coded_error) / DOUBLE_DIVIDE_CHECK(this_frame->coded_error) > .40) || |
| 1972 | (fabs(last_frame->intra_error - this_frame->intra_error) / DOUBLE_DIVIDE_CHECK(this_frame->intra_error) > .40) || |
| 1973 | ((next_frame->intra_error / DOUBLE_DIVIDE_CHECK(next_frame->coded_error)) > 3.5) |
| 1974 | ) |
| 1975 | ) |
| 1976 | ) |
| 1977 | ) |
| 1978 | { |
| 1979 | int i; |
| 1980 | FIRSTPASS_STATS *start_pos; |
| 1981 | |
| 1982 | FIRSTPASS_STATS local_next_frame; |
| 1983 | |
| 1984 | double boost_score = 0.0; |
| 1985 | double old_boost_score = 0.0; |
| 1986 | double decay_accumulator = 1.0; |
| 1987 | double next_iiratio; |
| 1988 | |
| 1989 | vpx_memcpy(&local_next_frame, next_frame, sizeof(*next_frame)); |
| 1990 | |
| 1991 | // Note the starting file position so we can reset to it |
| 1992 | start_pos = cpi->stats_in; |
| 1993 | |
| 1994 | // Examine how well the key frame predicts subsequent frames |
| 1995 | for (i = 0 ; i < 16; i++) |
| 1996 | { |
| 1997 | next_iiratio = (IIKFACTOR1 * local_next_frame.intra_error / DOUBLE_DIVIDE_CHECK(local_next_frame.coded_error)) ; |
| 1998 | |
| 1999 | if (next_iiratio > RMAX) |
| 2000 | next_iiratio = RMAX; |
| 2001 | |
| 2002 | // Cumulative effect of decay in prediction quality |
| 2003 | if (local_next_frame.pcnt_inter > 0.85) |
| 2004 | decay_accumulator = decay_accumulator * local_next_frame.pcnt_inter; |
| 2005 | else |
| 2006 | decay_accumulator = decay_accumulator * ((0.85 + local_next_frame.pcnt_inter) / 2.0); |
| 2007 | |
| 2008 | //decay_accumulator = decay_accumulator * local_next_frame.pcnt_inter; |
| 2009 | |
| 2010 | // Keep a running total |
| 2011 | boost_score += (decay_accumulator * next_iiratio); |
| 2012 | |
| 2013 | // Test various breakout clauses |
| 2014 | if ((local_next_frame.pcnt_inter < 0.05) || |
| 2015 | (next_iiratio < 1.5) || |
| 2016 | ((local_next_frame.pcnt_inter < 0.20) && (next_iiratio < 3.0)) || |
| 2017 | ((boost_score - old_boost_score) < 0.5) || |
| 2018 | (local_next_frame.intra_error < 200) |
| 2019 | ) |
| 2020 | { |
| 2021 | break; |
| 2022 | } |
| 2023 | |
| 2024 | old_boost_score = boost_score; |
| 2025 | |
| 2026 | // Get the next frame details |
| 2027 | if (EOF == vp8_input_stats(cpi, &local_next_frame)) |
| 2028 | break; |
| 2029 | } |
| 2030 | |
| 2031 | // If there is tolerable prediction for at least the next 3 frames then break out else discard this pottential key frame and move on |
| 2032 | if (boost_score > 5.0 && (i > 3)) |
| 2033 | is_viable_kf = TRUE; |
| 2034 | else |
| 2035 | { |
| 2036 | // Reset the file position |
| 2037 | reset_fpf_position(cpi, start_pos); |
| 2038 | |
| 2039 | is_viable_kf = FALSE; |
| 2040 | } |
| 2041 | } |
| 2042 | |
| 2043 | return is_viable_kf; |
| 2044 | } |
| 2045 | void vp8_find_next_key_frame(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame) |
| 2046 | { |
| 2047 | int i; |
| 2048 | FIRSTPASS_STATS last_frame; |
| 2049 | FIRSTPASS_STATS first_frame; |
| 2050 | FIRSTPASS_STATS next_frame; |
| 2051 | FIRSTPASS_STATS *start_position; |
| 2052 | |
| 2053 | double decay_accumulator = 0; |
| 2054 | double boost_score = 0; |
| 2055 | double old_boost_score = 0.0; |
| 2056 | double loop_decay_rate; |
| 2057 | |
| 2058 | double kf_mod_err = 0.0; |
| 2059 | double kf_group_err = 0.0; |
| 2060 | double kf_group_intra_err = 0.0; |
| 2061 | double kf_group_coded_err = 0.0; |
| 2062 | double two_pass_min_rate = (double)(cpi->oxcf.target_bandwidth * cpi->oxcf.two_pass_vbrmin_section / 100); |
| 2063 | |
Guillermo Ballester Valor | 5a72620 | 2010-06-11 14:33:49 -0400 | [diff] [blame] | 2064 | vpx_memset(&next_frame, 0, sizeof(next_frame)); // assure clean |
| 2065 | |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 2066 | vp8_clear_system_state(); //__asm emms; |
| 2067 | start_position = cpi->stats_in; |
| 2068 | |
| 2069 | cpi->common.frame_type = KEY_FRAME; |
| 2070 | |
| 2071 | // Clear the alt ref active flag as this can never be active on a key frame |
| 2072 | cpi->source_alt_ref_active = FALSE; |
| 2073 | |
| 2074 | // Kf is always a gf so clear frames till next gf counter |
| 2075 | cpi->frames_till_gf_update_due = 0; |
| 2076 | |
| 2077 | cpi->frames_to_key = 1; |
| 2078 | |
| 2079 | // Take a copy of the initial frame details |
| 2080 | vpx_memcpy(&first_frame, this_frame, sizeof(*this_frame)); |
| 2081 | |
Paul Wilkins | 9404c7d | 2010-07-23 17:01:12 +0100 | [diff] [blame] | 2082 | cpi->kf_group_bits = 0; // Total bits avaialable to kf group |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 2083 | cpi->kf_group_error_left = 0; // Group modified error score. |
| 2084 | |
| 2085 | kf_mod_err = calculate_modified_err(cpi, this_frame); |
| 2086 | |
| 2087 | // find the next keyframe |
| 2088 | while (cpi->stats_in < cpi->stats_in_end) |
| 2089 | { |
| 2090 | // Accumulate kf group error |
| 2091 | kf_group_err += calculate_modified_err(cpi, this_frame); |
| 2092 | |
| 2093 | // These figures keep intra and coded error counts for all frames including key frames in the group. |
| 2094 | // The effect of the key frame itself can be subtracted out using the first_frame data collected above |
| 2095 | kf_group_intra_err += this_frame->intra_error; |
| 2096 | kf_group_coded_err += this_frame->coded_error; |
| 2097 | |
| 2098 | vpx_memcpy(&last_frame, this_frame, sizeof(*this_frame)); |
| 2099 | |
| 2100 | // Provided that we are not at the end of the file... |
| 2101 | if (EOF != vp8_input_stats(cpi, this_frame)) |
| 2102 | { |
| 2103 | if (lookup_next_frame_stats(cpi, &next_frame) != EOF) |
| 2104 | { |
| 2105 | if (test_candidate_kf(cpi, &last_frame, this_frame, &next_frame)) |
| 2106 | break; |
| 2107 | } |
| 2108 | } |
| 2109 | |
| 2110 | // Step on to the next frame |
| 2111 | cpi->frames_to_key ++; |
| 2112 | |
| 2113 | // If we don't have a real key frame within the next two |
| 2114 | // forcekeyframeevery intervals then break out of the loop. |
| 2115 | if (cpi->frames_to_key >= 2 *(int)cpi->key_frame_frequency) |
| 2116 | break; |
| 2117 | |
| 2118 | } |
| 2119 | |
| 2120 | // If there is a max kf interval set by the user we must obey it. |
| 2121 | // We already breakout of the loop above at 2x max. |
| 2122 | // This code centers the extra kf if the actual natural |
| 2123 | // interval is between 1x and 2x |
| 2124 | if ( cpi->frames_to_key > (int)cpi->key_frame_frequency ) |
| 2125 | { |
| 2126 | cpi->frames_to_key /= 2; |
| 2127 | |
| 2128 | // Estimate corrected kf group error |
| 2129 | kf_group_err /= 2.0; |
| 2130 | kf_group_intra_err /= 2.0; |
| 2131 | kf_group_coded_err /= 2.0; |
| 2132 | } |
| 2133 | |
| 2134 | // Special case for the last frame of the file |
| 2135 | if (cpi->stats_in >= cpi->stats_in_end) |
| 2136 | { |
| 2137 | // Accumulate kf group error |
| 2138 | kf_group_err += calculate_modified_err(cpi, this_frame); |
| 2139 | |
| 2140 | // These figures keep intra and coded error counts for all frames including key frames in the group. |
| 2141 | // The effect of the key frame itself can be subtracted out using the first_frame data collected above |
| 2142 | kf_group_intra_err += this_frame->intra_error; |
| 2143 | kf_group_coded_err += this_frame->coded_error; |
| 2144 | } |
| 2145 | |
| 2146 | // Calculate the number of bits that should be assigned to the kf group. |
| 2147 | if ((cpi->bits_left > 0) && ((int)cpi->modified_total_error_left > 0)) |
| 2148 | { |
Paul Wilkins | 9404c7d | 2010-07-23 17:01:12 +0100 | [diff] [blame] | 2149 | // Max for a single normal frame (not key frame) |
| 2150 | int max_bits = frame_max_bits(cpi); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 2151 | |
Paul Wilkins | 9404c7d | 2010-07-23 17:01:12 +0100 | [diff] [blame] | 2152 | // Maximum bits for the kf group |
| 2153 | long long max_grp_bits; |
| 2154 | |
| 2155 | // Default allocation based on bits left and relative |
| 2156 | // complexity of the section |
| 2157 | cpi->kf_group_bits = (long long)( cpi->bits_left * |
| 2158 | ( kf_group_err / |
| 2159 | cpi->modified_total_error_left )); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 2160 | |
| 2161 | // Clip based on maximum per frame rate defined by the user. |
Paul Wilkins | 9404c7d | 2010-07-23 17:01:12 +0100 | [diff] [blame] | 2162 | max_grp_bits = (long long)max_bits * (long long)cpi->frames_to_key; |
| 2163 | if (cpi->kf_group_bits > max_grp_bits) |
| 2164 | cpi->kf_group_bits = max_grp_bits; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 2165 | |
| 2166 | // Additional special case for CBR if buffer is getting full. |
| 2167 | if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) |
| 2168 | { |
Paul Wilkins | 9404c7d | 2010-07-23 17:01:12 +0100 | [diff] [blame] | 2169 | int opt_buffer_lvl = cpi->oxcf.optimal_buffer_level; |
| 2170 | int buffer_lvl = cpi->buffer_level; |
| 2171 | |
| 2172 | // If the buffer is near or above the optimal and this kf group is |
| 2173 | // not being allocated much then increase the allocation a bit. |
| 2174 | if (buffer_lvl >= opt_buffer_lvl) |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 2175 | { |
Paul Wilkins | 9404c7d | 2010-07-23 17:01:12 +0100 | [diff] [blame] | 2176 | int high_water_mark = (opt_buffer_lvl + |
| 2177 | cpi->oxcf.maximum_buffer_size) >> 1; |
| 2178 | |
| 2179 | long long av_group_bits; |
| 2180 | |
| 2181 | // Av bits per frame * number of frames |
| 2182 | av_group_bits = (long long)cpi->av_per_frame_bandwidth * |
| 2183 | (long long)cpi->frames_to_key; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 2184 | |
| 2185 | // We are at or above the maximum. |
| 2186 | if (cpi->buffer_level >= high_water_mark) |
| 2187 | { |
Paul Wilkins | 9404c7d | 2010-07-23 17:01:12 +0100 | [diff] [blame] | 2188 | long long min_group_bits; |
| 2189 | |
| 2190 | min_group_bits = av_group_bits + |
| 2191 | (long long)(buffer_lvl - |
| 2192 | high_water_mark); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 2193 | |
| 2194 | if (cpi->kf_group_bits < min_group_bits) |
| 2195 | cpi->kf_group_bits = min_group_bits; |
| 2196 | } |
| 2197 | // We are above optimal but below the maximum |
Paul Wilkins | 9404c7d | 2010-07-23 17:01:12 +0100 | [diff] [blame] | 2198 | else if (cpi->kf_group_bits < av_group_bits) |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 2199 | { |
Paul Wilkins | 9404c7d | 2010-07-23 17:01:12 +0100 | [diff] [blame] | 2200 | long long bits_below_av = av_group_bits - |
| 2201 | cpi->kf_group_bits; |
| 2202 | |
| 2203 | cpi->kf_group_bits += |
| 2204 | (long long)((double)bits_below_av * |
| 2205 | (double)(buffer_lvl - opt_buffer_lvl) / |
| 2206 | (double)(high_water_mark - opt_buffer_lvl)); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 2207 | } |
| 2208 | } |
| 2209 | } |
| 2210 | } |
| 2211 | else |
| 2212 | cpi->kf_group_bits = 0; |
| 2213 | |
| 2214 | // Reset the first pass file position |
| 2215 | reset_fpf_position(cpi, start_position); |
| 2216 | |
| 2217 | // determine how big to make this keyframe based on how well the subsequent frames use inter blocks |
| 2218 | decay_accumulator = 1.0; |
| 2219 | boost_score = 0.0; |
| 2220 | loop_decay_rate = 1.00; // Starting decay rate |
| 2221 | |
| 2222 | for (i = 0 ; i < cpi->frames_to_key ; i++) |
| 2223 | { |
| 2224 | double r; |
| 2225 | |
| 2226 | if (EOF == vp8_input_stats(cpi, &next_frame)) |
| 2227 | break; |
| 2228 | |
| 2229 | r = (IIKFACTOR2 * next_frame.intra_error / DOUBLE_DIVIDE_CHECK(next_frame.coded_error)) ; |
| 2230 | |
| 2231 | if (r > RMAX) |
| 2232 | r = RMAX; |
| 2233 | |
| 2234 | // Adjust loop decay rate |
| 2235 | //if ( next_frame.pcnt_inter < loop_decay_rate ) |
| 2236 | loop_decay_rate = next_frame.pcnt_inter; |
| 2237 | |
| 2238 | if ((1.0 - (next_frame.pcnt_motion / 10.0)) < loop_decay_rate) |
| 2239 | loop_decay_rate = (1.0 - (next_frame.pcnt_motion / 10.0)); |
| 2240 | |
| 2241 | decay_accumulator = decay_accumulator * loop_decay_rate; |
| 2242 | |
| 2243 | boost_score += (decay_accumulator * r); |
| 2244 | |
| 2245 | if ((i > MIN_GF_INTERVAL) && |
| 2246 | ((boost_score - old_boost_score) < 1.0)) |
| 2247 | { |
| 2248 | break; |
| 2249 | } |
| 2250 | |
| 2251 | old_boost_score = boost_score; |
| 2252 | } |
| 2253 | |
| 2254 | if (1) |
| 2255 | { |
| 2256 | FIRSTPASS_STATS sectionstats; |
| 2257 | double Ratio; |
| 2258 | |
| 2259 | vp8_zero_stats(§ionstats); |
| 2260 | reset_fpf_position(cpi, start_position); |
| 2261 | |
| 2262 | for (i = 0 ; i < cpi->frames_to_key ; i++) |
| 2263 | { |
| 2264 | vp8_input_stats(cpi, &next_frame); |
| 2265 | vp8_accumulate_stats(§ionstats, &next_frame); |
| 2266 | } |
| 2267 | |
| 2268 | vp8_avg_stats(§ionstats); |
| 2269 | |
| 2270 | if (sectionstats.pcnt_motion < .17) |
| 2271 | cpi->section_is_low_motion = 1; |
| 2272 | else |
| 2273 | cpi->section_is_low_motion = 0; |
| 2274 | |
| 2275 | if (sectionstats.mvc_abs + sectionstats.mvr_abs > 45) |
| 2276 | cpi->section_is_fast_motion = 1; |
| 2277 | else |
| 2278 | cpi->section_is_fast_motion = 0; |
| 2279 | |
| 2280 | cpi->section_intra_rating = sectionstats.intra_error / DOUBLE_DIVIDE_CHECK(sectionstats.coded_error); |
| 2281 | |
| 2282 | Ratio = sectionstats.intra_error / DOUBLE_DIVIDE_CHECK(sectionstats.coded_error); |
| 2283 | // if( (Ratio > 11) ) //&& (sectionstats.pcnt_second_ref < .20) ) |
| 2284 | //{ |
| 2285 | cpi->section_max_qfactor = 1.0 - ((Ratio - 10.0) * 0.025); |
| 2286 | |
| 2287 | if (cpi->section_max_qfactor < 0.80) |
| 2288 | cpi->section_max_qfactor = 0.80; |
| 2289 | |
| 2290 | //} |
| 2291 | //else |
| 2292 | // cpi->section_max_qfactor = 1.0; |
| 2293 | } |
| 2294 | |
| 2295 | // When using CBR apply additional buffer fullness related upper limits |
| 2296 | if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) |
| 2297 | { |
| 2298 | double max_boost; |
| 2299 | |
| 2300 | if (cpi->drop_frames_allowed) |
| 2301 | { |
| 2302 | int df_buffer_level = cpi->oxcf.drop_frames_water_mark * (cpi->oxcf.optimal_buffer_level / 100); |
| 2303 | |
| 2304 | if (cpi->buffer_level > df_buffer_level) |
| 2305 | max_boost = ((double)((cpi->buffer_level - df_buffer_level) * 2 / 3) * 16.0) / DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth); |
| 2306 | else |
| 2307 | max_boost = 0.0; |
| 2308 | } |
| 2309 | else if (cpi->buffer_level > 0) |
| 2310 | { |
| 2311 | max_boost = ((double)(cpi->buffer_level * 2 / 3) * 16.0) / DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth); |
| 2312 | } |
| 2313 | else |
| 2314 | { |
| 2315 | max_boost = 0.0; |
| 2316 | } |
| 2317 | |
| 2318 | if (boost_score > max_boost) |
| 2319 | boost_score = max_boost; |
| 2320 | } |
| 2321 | |
| 2322 | // Reset the first pass file position |
| 2323 | reset_fpf_position(cpi, start_position); |
| 2324 | |
| 2325 | // Work out how many bits to allocate for the key frame itself |
| 2326 | if (1) |
| 2327 | { |
| 2328 | int kf_boost = boost_score; |
| 2329 | int allocation_chunks; |
| 2330 | int Counter = cpi->frames_to_key; |
| 2331 | int alt_kf_bits; |
Fritz Koenig | 0ce3901 | 2010-07-22 08:07:32 -0400 | [diff] [blame] | 2332 | YV12_BUFFER_CONFIG *lst_yv12 = &cpi->common.yv12_fb[cpi->common.lst_fb_idx]; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 2333 | // Min boost based on kf interval |
| 2334 | #if 0 |
| 2335 | |
| 2336 | while ((kf_boost < 48) && (Counter > 0)) |
| 2337 | { |
| 2338 | Counter -= 2; |
| 2339 | kf_boost ++; |
| 2340 | } |
| 2341 | |
| 2342 | #endif |
| 2343 | |
| 2344 | if (kf_boost < 48) |
| 2345 | { |
| 2346 | kf_boost += ((Counter + 1) >> 1); |
| 2347 | |
| 2348 | if (kf_boost > 48) kf_boost = 48; |
| 2349 | } |
| 2350 | |
| 2351 | // bigger frame sizes need larger kf boosts, smaller frames smaller boosts... |
Fritz Koenig | 0ce3901 | 2010-07-22 08:07:32 -0400 | [diff] [blame] | 2352 | if ((lst_yv12->y_width * lst_yv12->y_height) > (320 * 240)) |
| 2353 | kf_boost += 2 * (lst_yv12->y_width * lst_yv12->y_height) / (320 * 240); |
| 2354 | else if ((lst_yv12->y_width * lst_yv12->y_height) < (320 * 240)) |
| 2355 | kf_boost -= 4 * (320 * 240) / (lst_yv12->y_width * lst_yv12->y_height); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 2356 | |
| 2357 | kf_boost = (int)((double)kf_boost * 100.0) >> 4; // Scale 16 to 100 |
| 2358 | |
| 2359 | // Adjustment to boost based on recent average q |
| 2360 | kf_boost = kf_boost * vp8_kf_boost_qadjustment[cpi->ni_av_qi] / 100; |
| 2361 | |
| 2362 | if (kf_boost < 250) // Min KF boost |
| 2363 | kf_boost = 250; |
| 2364 | |
| 2365 | // We do three calculations for kf size. |
| 2366 | // The first is based on the error score for the whole kf group. |
| 2367 | // The second (optionaly) on the key frames own error if this is smaller than the average for the group. |
| 2368 | // The final one insures that the frame receives at least the allocation it would have received based on its own error score vs the error score remaining |
| 2369 | |
| 2370 | allocation_chunks = ((cpi->frames_to_key - 1) * 100) + kf_boost; // cpi->frames_to_key-1 because key frame itself is taken care of by kf_boost |
| 2371 | |
| 2372 | // Normalize Altboost and allocations chunck down to prevent overflow |
| 2373 | while (kf_boost > 1000) |
| 2374 | { |
| 2375 | kf_boost /= 2; |
| 2376 | allocation_chunks /= 2; |
| 2377 | } |
| 2378 | |
| 2379 | cpi->kf_group_bits = (cpi->kf_group_bits < 0) ? 0 : cpi->kf_group_bits; |
| 2380 | |
| 2381 | // Calculate the number of bits to be spent on the key frame |
| 2382 | cpi->kf_bits = (int)((double)kf_boost * ((double)cpi->kf_group_bits / (double)allocation_chunks)); |
| 2383 | |
| 2384 | // Apply an additional limit for CBR |
| 2385 | if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) |
| 2386 | { |
| 2387 | if (cpi->kf_bits > ((3 * cpi->buffer_level) >> 2)) |
| 2388 | cpi->kf_bits = (3 * cpi->buffer_level) >> 2; |
| 2389 | } |
| 2390 | |
| 2391 | // If the key frame is actually easier than the average for the kf group (which does sometimes happen... eg a blank intro frame) |
| 2392 | // Then use an alternate calculation based on the kf error score which should give a smaller key frame. |
| 2393 | if (kf_mod_err < kf_group_err / cpi->frames_to_key) |
| 2394 | { |
| 2395 | double alt_kf_grp_bits = ((double)cpi->bits_left * (kf_mod_err * (double)cpi->frames_to_key) / cpi->modified_total_error_left) ; |
| 2396 | |
| 2397 | alt_kf_bits = (int)((double)kf_boost * (alt_kf_grp_bits / (double)allocation_chunks)); |
| 2398 | |
| 2399 | if (cpi->kf_bits > alt_kf_bits) |
| 2400 | { |
| 2401 | cpi->kf_bits = alt_kf_bits; |
| 2402 | } |
| 2403 | } |
| 2404 | // Else if it is much harder than other frames in the group make sure it at least receives an allocation in keeping with its relative error score |
| 2405 | else |
| 2406 | { |
| 2407 | alt_kf_bits = (int)((double)cpi->bits_left * (kf_mod_err / cpi->modified_total_error_left)); |
| 2408 | |
| 2409 | if (alt_kf_bits > cpi->kf_bits) |
| 2410 | { |
| 2411 | cpi->kf_bits = alt_kf_bits; |
| 2412 | } |
| 2413 | } |
| 2414 | |
| 2415 | cpi->kf_group_bits -= cpi->kf_bits; |
| 2416 | cpi->kf_bits += cpi->min_frame_bandwidth; // Add in the minimum frame allowance |
| 2417 | |
| 2418 | cpi->per_frame_bandwidth = cpi->kf_bits; // Peer frame bit target for this frame |
| 2419 | cpi->target_bandwidth = cpi->kf_bits * cpi->output_frame_rate; // Convert to a per second bitrate |
| 2420 | } |
| 2421 | |
| 2422 | // Note the total error score of the kf group minus the key frame itself |
| 2423 | cpi->kf_group_error_left = (int)(kf_group_err - kf_mod_err); |
| 2424 | |
| 2425 | // Adjust the count of total modified error left. |
| 2426 | // The count of bits left is adjusted elsewhere based on real coded frame sizes |
| 2427 | cpi->modified_total_error_left -= kf_group_err; |
| 2428 | |
| 2429 | if (cpi->oxcf.allow_spatial_resampling) |
| 2430 | { |
| 2431 | int resample_trigger = FALSE; |
| 2432 | int last_kf_resampled = FALSE; |
| 2433 | int kf_q; |
| 2434 | int scale_val = 0; |
| 2435 | int hr, hs, vr, vs; |
| 2436 | int new_width = cpi->oxcf.Width; |
| 2437 | int new_height = cpi->oxcf.Height; |
| 2438 | |
| 2439 | int projected_buffer_level = cpi->buffer_level; |
| 2440 | int tmp_q; |
| 2441 | |
| 2442 | double projected_bits_perframe; |
| 2443 | double group_iiratio = (kf_group_intra_err - first_frame.intra_error) / (kf_group_coded_err - first_frame.coded_error); |
| 2444 | double err_per_frame = kf_group_err / cpi->frames_to_key; |
| 2445 | double bits_per_frame; |
| 2446 | double av_bits_per_frame; |
| 2447 | double effective_size_ratio; |
| 2448 | |
| 2449 | if ((cpi->common.Width != cpi->oxcf.Width) || (cpi->common.Height != cpi->oxcf.Height)) |
| 2450 | last_kf_resampled = TRUE; |
| 2451 | |
| 2452 | // Set back to unscaled by defaults |
| 2453 | cpi->common.horiz_scale = NORMAL; |
| 2454 | cpi->common.vert_scale = NORMAL; |
| 2455 | |
| 2456 | // Calculate Average bits per frame. |
| 2457 | //av_bits_per_frame = cpi->bits_left/(double)(cpi->total_stats.count - cpi->common.current_video_frame); |
| 2458 | av_bits_per_frame = cpi->oxcf.target_bandwidth / DOUBLE_DIVIDE_CHECK((double)cpi->oxcf.frame_rate); |
| 2459 | //if ( av_bits_per_frame < 0.0 ) |
| 2460 | // av_bits_per_frame = 0.0 |
| 2461 | |
| 2462 | // CBR... Use the clip average as the target for deciding resample |
| 2463 | if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) |
| 2464 | { |
| 2465 | bits_per_frame = av_bits_per_frame; |
| 2466 | } |
| 2467 | |
| 2468 | // In VBR we want to avoid downsampling in easy section unless we are under extreme pressure |
| 2469 | // So use the larger of target bitrate for this sectoion or average bitrate for sequence |
| 2470 | else |
| 2471 | { |
| 2472 | bits_per_frame = cpi->kf_group_bits / cpi->frames_to_key; // This accounts for how hard the section is... |
| 2473 | |
| 2474 | if (bits_per_frame < av_bits_per_frame) // Dont turn to resampling in easy sections just because they have been assigned a small number of bits |
| 2475 | bits_per_frame = av_bits_per_frame; |
| 2476 | } |
| 2477 | |
| 2478 | // bits_per_frame should comply with our minimum |
| 2479 | if (bits_per_frame < (cpi->oxcf.target_bandwidth * cpi->oxcf.two_pass_vbrmin_section / 100)) |
| 2480 | bits_per_frame = (cpi->oxcf.target_bandwidth * cpi->oxcf.two_pass_vbrmin_section / 100); |
| 2481 | |
| 2482 | // Work out if spatial resampling is necessary |
| 2483 | kf_q = estimate_kf_group_q(cpi, err_per_frame, bits_per_frame, new_height, new_width, group_iiratio); |
| 2484 | |
| 2485 | // If we project a required Q higher than the maximum allowed Q then make a guess at the actual size of frames in this section |
| 2486 | projected_bits_perframe = bits_per_frame; |
| 2487 | tmp_q = kf_q; |
| 2488 | |
| 2489 | while (tmp_q > cpi->worst_quality) |
| 2490 | { |
| 2491 | projected_bits_perframe *= 1.04; |
| 2492 | tmp_q--; |
| 2493 | } |
| 2494 | |
| 2495 | // Guess at buffer level at the end of the section |
| 2496 | projected_buffer_level = cpi->buffer_level - (int)((projected_bits_perframe - av_bits_per_frame) * cpi->frames_to_key); |
| 2497 | |
| 2498 | if (0) |
| 2499 | { |
| 2500 | FILE *f = fopen("Subsamle.stt", "a"); |
| 2501 | 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->frames_to_key, cpi->kf_group_bits / cpi->frames_to_key, new_height, new_width); |
| 2502 | fclose(f); |
| 2503 | } |
| 2504 | |
| 2505 | // The trigger for spatial resampling depends on the various parameters such as whether we are streaming (CBR) or VBR. |
| 2506 | if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) |
| 2507 | { |
| 2508 | // Trigger resample if we are projected to fall below down sample level or |
| 2509 | // resampled last time and are projected to remain below the up sample level |
| 2510 | if ((projected_buffer_level < (cpi->oxcf.resample_down_water_mark * cpi->oxcf.optimal_buffer_level / 100)) || |
| 2511 | (last_kf_resampled && (projected_buffer_level < (cpi->oxcf.resample_up_water_mark * cpi->oxcf.optimal_buffer_level / 100)))) |
| 2512 | //( ((cpi->buffer_level < (cpi->oxcf.resample_down_water_mark * cpi->oxcf.optimal_buffer_level / 100))) && |
| 2513 | // ((projected_buffer_level < (cpi->oxcf.resample_up_water_mark * cpi->oxcf.optimal_buffer_level / 100))) )) |
| 2514 | resample_trigger = TRUE; |
| 2515 | else |
| 2516 | resample_trigger = FALSE; |
| 2517 | } |
| 2518 | else |
| 2519 | { |
| 2520 | long long clip_bits = (long long)(cpi->total_stats.count * cpi->oxcf.target_bandwidth / DOUBLE_DIVIDE_CHECK((double)cpi->oxcf.frame_rate)); |
| 2521 | long long over_spend = cpi->oxcf.starting_buffer_level - cpi->buffer_level; |
| 2522 | long long over_spend2 = cpi->oxcf.starting_buffer_level - projected_buffer_level; |
| 2523 | |
| 2524 | if ((last_kf_resampled && (kf_q > cpi->worst_quality)) || // If triggered last time the threshold for triggering again is reduced |
| 2525 | ((kf_q > cpi->worst_quality) && // Projected Q higher than allowed and ... |
| 2526 | (over_spend > clip_bits / 20))) // ... Overspend > 5% of total bits |
| 2527 | resample_trigger = TRUE; |
| 2528 | else |
| 2529 | resample_trigger = FALSE; |
| 2530 | |
| 2531 | } |
| 2532 | |
| 2533 | if (resample_trigger) |
| 2534 | { |
| 2535 | while ((kf_q >= cpi->worst_quality) && (scale_val < 6)) |
| 2536 | { |
| 2537 | scale_val ++; |
| 2538 | |
| 2539 | cpi->common.vert_scale = vscale_lookup[scale_val]; |
| 2540 | cpi->common.horiz_scale = hscale_lookup[scale_val]; |
| 2541 | |
| 2542 | Scale2Ratio(cpi->common.horiz_scale, &hr, &hs); |
| 2543 | Scale2Ratio(cpi->common.vert_scale, &vr, &vs); |
| 2544 | |
| 2545 | new_width = ((hs - 1) + (cpi->oxcf.Width * hr)) / hs; |
| 2546 | new_height = ((vs - 1) + (cpi->oxcf.Height * vr)) / vs; |
| 2547 | |
| 2548 | // Reducing the area to 1/4 does not reduce the complexity (err_per_frame) to 1/4... |
| 2549 | // effective_sizeratio attempts to provide a crude correction for this |
| 2550 | effective_size_ratio = (double)(new_width * new_height) / (double)(cpi->oxcf.Width * cpi->oxcf.Height); |
| 2551 | effective_size_ratio = (1.0 + (3.0 * effective_size_ratio)) / 4.0; |
| 2552 | |
| 2553 | // Now try again and see what Q we get with the smaller image size |
| 2554 | kf_q = estimate_kf_group_q(cpi, err_per_frame * effective_size_ratio, bits_per_frame, new_height, new_width, group_iiratio); |
| 2555 | |
| 2556 | if (0) |
| 2557 | { |
| 2558 | FILE *f = fopen("Subsamle.stt", "a"); |
| 2559 | 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->frames_to_key, cpi->kf_group_bits / cpi->frames_to_key, new_height, new_width); |
| 2560 | fclose(f); |
| 2561 | } |
| 2562 | } |
| 2563 | } |
| 2564 | |
| 2565 | if ((cpi->common.Width != new_width) || (cpi->common.Height != new_height)) |
| 2566 | { |
| 2567 | cpi->common.Width = new_width; |
| 2568 | cpi->common.Height = new_height; |
| 2569 | vp8_alloc_compressor_data(cpi); |
| 2570 | } |
| 2571 | } |
| 2572 | } |