Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright (c) 2010 The WebM project authors. All Rights Reserved. |
michael@0 | 3 | * |
michael@0 | 4 | * Use of this source code is governed by a BSD-style license |
michael@0 | 5 | * that can be found in the LICENSE file in the root of the source |
michael@0 | 6 | * tree. An additional intellectual property rights grant can be found |
michael@0 | 7 | * in the file PATENTS. All contributing project authors may |
michael@0 | 8 | * be found in the AUTHORS file in the root of the source tree. |
michael@0 | 9 | */ |
michael@0 | 10 | |
michael@0 | 11 | #include <math.h> |
michael@0 | 12 | #include <limits.h> |
michael@0 | 13 | #include <stdio.h> |
michael@0 | 14 | |
michael@0 | 15 | #include "./vpx_scale_rtcd.h" |
michael@0 | 16 | #include "block.h" |
michael@0 | 17 | #include "onyx_int.h" |
michael@0 | 18 | #include "vp8/common/variance.h" |
michael@0 | 19 | #include "encodeintra.h" |
michael@0 | 20 | #include "vp8/common/setupintrarecon.h" |
michael@0 | 21 | #include "vp8/common/systemdependent.h" |
michael@0 | 22 | #include "mcomp.h" |
michael@0 | 23 | #include "firstpass.h" |
michael@0 | 24 | #include "vpx_scale/vpx_scale.h" |
michael@0 | 25 | #include "encodemb.h" |
michael@0 | 26 | #include "vp8/common/extend.h" |
michael@0 | 27 | #include "vpx_mem/vpx_mem.h" |
michael@0 | 28 | #include "vp8/common/swapyv12buffer.h" |
michael@0 | 29 | #include "rdopt.h" |
michael@0 | 30 | #include "vp8/common/quant_common.h" |
michael@0 | 31 | #include "encodemv.h" |
michael@0 | 32 | #include "encodeframe.h" |
michael@0 | 33 | |
michael@0 | 34 | /* #define OUTPUT_FPF 1 */ |
michael@0 | 35 | |
michael@0 | 36 | extern void vp8cx_frame_init_quantizer(VP8_COMP *cpi); |
michael@0 | 37 | extern void vp8_set_mbmode_and_mvs(MACROBLOCK *x, MB_PREDICTION_MODE mb, int_mv *mv); |
michael@0 | 38 | extern void vp8_alloc_compressor_data(VP8_COMP *cpi); |
michael@0 | 39 | |
michael@0 | 40 | #define GFQ_ADJUSTMENT vp8_gf_boost_qadjustment[Q] |
michael@0 | 41 | extern int vp8_kf_boost_qadjustment[QINDEX_RANGE]; |
michael@0 | 42 | |
michael@0 | 43 | extern const int vp8_gf_boost_qadjustment[QINDEX_RANGE]; |
michael@0 | 44 | |
michael@0 | 45 | #define IIFACTOR 1.5 |
michael@0 | 46 | #define IIKFACTOR1 1.40 |
michael@0 | 47 | #define IIKFACTOR2 1.5 |
michael@0 | 48 | #define RMAX 14.0 |
michael@0 | 49 | #define GF_RMAX 48.0 |
michael@0 | 50 | |
michael@0 | 51 | #define KF_MB_INTRA_MIN 300 |
michael@0 | 52 | #define GF_MB_INTRA_MIN 200 |
michael@0 | 53 | |
michael@0 | 54 | #define DOUBLE_DIVIDE_CHECK(X) ((X)<0?(X)-.000001:(X)+.000001) |
michael@0 | 55 | |
michael@0 | 56 | #define POW1 (double)cpi->oxcf.two_pass_vbrbias/100.0 |
michael@0 | 57 | #define POW2 (double)cpi->oxcf.two_pass_vbrbias/100.0 |
michael@0 | 58 | |
michael@0 | 59 | #define NEW_BOOST 1 |
michael@0 | 60 | |
michael@0 | 61 | static int vscale_lookup[7] = {0, 1, 1, 2, 2, 3, 3}; |
michael@0 | 62 | static int hscale_lookup[7] = {0, 0, 1, 1, 2, 2, 3}; |
michael@0 | 63 | |
michael@0 | 64 | |
michael@0 | 65 | static const int cq_level[QINDEX_RANGE] = |
michael@0 | 66 | { |
michael@0 | 67 | 0,0,1,1,2,3,3,4,4,5,6,6,7,8,8,9, |
michael@0 | 68 | 9,10,11,11,12,13,13,14,15,15,16,17,17,18,19,20, |
michael@0 | 69 | 20,21,22,22,23,24,24,25,26,27,27,28,29,30,30,31, |
michael@0 | 70 | 32,33,33,34,35,36,36,37,38,39,39,40,41,42,42,43, |
michael@0 | 71 | 44,45,46,46,47,48,49,50,50,51,52,53,54,55,55,56, |
michael@0 | 72 | 57,58,59,60,60,61,62,63,64,65,66,67,67,68,69,70, |
michael@0 | 73 | 71,72,73,74,75,75,76,77,78,79,80,81,82,83,84,85, |
michael@0 | 74 | 86,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100 |
michael@0 | 75 | }; |
michael@0 | 76 | |
michael@0 | 77 | static void find_next_key_frame(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame); |
michael@0 | 78 | |
michael@0 | 79 | /* Resets the first pass file to the given position using a relative seek |
michael@0 | 80 | * from the current position |
michael@0 | 81 | */ |
michael@0 | 82 | static void reset_fpf_position(VP8_COMP *cpi, FIRSTPASS_STATS *Position) |
michael@0 | 83 | { |
michael@0 | 84 | cpi->twopass.stats_in = Position; |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | static int lookup_next_frame_stats(VP8_COMP *cpi, FIRSTPASS_STATS *next_frame) |
michael@0 | 88 | { |
michael@0 | 89 | if (cpi->twopass.stats_in >= cpi->twopass.stats_in_end) |
michael@0 | 90 | return EOF; |
michael@0 | 91 | |
michael@0 | 92 | *next_frame = *cpi->twopass.stats_in; |
michael@0 | 93 | return 1; |
michael@0 | 94 | } |
michael@0 | 95 | |
michael@0 | 96 | /* Read frame stats at an offset from the current position */ |
michael@0 | 97 | static int read_frame_stats( VP8_COMP *cpi, |
michael@0 | 98 | FIRSTPASS_STATS *frame_stats, |
michael@0 | 99 | int offset ) |
michael@0 | 100 | { |
michael@0 | 101 | FIRSTPASS_STATS * fps_ptr = cpi->twopass.stats_in; |
michael@0 | 102 | |
michael@0 | 103 | /* Check legality of offset */ |
michael@0 | 104 | if ( offset >= 0 ) |
michael@0 | 105 | { |
michael@0 | 106 | if ( &fps_ptr[offset] >= cpi->twopass.stats_in_end ) |
michael@0 | 107 | return EOF; |
michael@0 | 108 | } |
michael@0 | 109 | else if ( offset < 0 ) |
michael@0 | 110 | { |
michael@0 | 111 | if ( &fps_ptr[offset] < cpi->twopass.stats_in_start ) |
michael@0 | 112 | return EOF; |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | *frame_stats = fps_ptr[offset]; |
michael@0 | 116 | return 1; |
michael@0 | 117 | } |
michael@0 | 118 | |
michael@0 | 119 | static int input_stats(VP8_COMP *cpi, FIRSTPASS_STATS *fps) |
michael@0 | 120 | { |
michael@0 | 121 | if (cpi->twopass.stats_in >= cpi->twopass.stats_in_end) |
michael@0 | 122 | return EOF; |
michael@0 | 123 | |
michael@0 | 124 | *fps = *cpi->twopass.stats_in; |
michael@0 | 125 | cpi->twopass.stats_in = |
michael@0 | 126 | (void*)((char *)cpi->twopass.stats_in + sizeof(FIRSTPASS_STATS)); |
michael@0 | 127 | return 1; |
michael@0 | 128 | } |
michael@0 | 129 | |
michael@0 | 130 | static void output_stats(const VP8_COMP *cpi, |
michael@0 | 131 | struct vpx_codec_pkt_list *pktlist, |
michael@0 | 132 | FIRSTPASS_STATS *stats) |
michael@0 | 133 | { |
michael@0 | 134 | struct vpx_codec_cx_pkt pkt; |
michael@0 | 135 | pkt.kind = VPX_CODEC_STATS_PKT; |
michael@0 | 136 | pkt.data.twopass_stats.buf = stats; |
michael@0 | 137 | pkt.data.twopass_stats.sz = sizeof(FIRSTPASS_STATS); |
michael@0 | 138 | vpx_codec_pkt_list_add(pktlist, &pkt); |
michael@0 | 139 | |
michael@0 | 140 | /* TEMP debug code */ |
michael@0 | 141 | #if OUTPUT_FPF |
michael@0 | 142 | |
michael@0 | 143 | { |
michael@0 | 144 | FILE *fpfile; |
michael@0 | 145 | fpfile = fopen("firstpass.stt", "a"); |
michael@0 | 146 | |
michael@0 | 147 | fprintf(fpfile, "%12.0f %12.0f %12.0f %12.4f %12.4f %12.4f %12.4f" |
michael@0 | 148 | " %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f" |
michael@0 | 149 | " %12.0f %12.0f %12.4f\n", |
michael@0 | 150 | stats->frame, |
michael@0 | 151 | stats->intra_error, |
michael@0 | 152 | stats->coded_error, |
michael@0 | 153 | stats->ssim_weighted_pred_err, |
michael@0 | 154 | stats->pcnt_inter, |
michael@0 | 155 | stats->pcnt_motion, |
michael@0 | 156 | stats->pcnt_second_ref, |
michael@0 | 157 | stats->pcnt_neutral, |
michael@0 | 158 | stats->MVr, |
michael@0 | 159 | stats->mvr_abs, |
michael@0 | 160 | stats->MVc, |
michael@0 | 161 | stats->mvc_abs, |
michael@0 | 162 | stats->MVrv, |
michael@0 | 163 | stats->MVcv, |
michael@0 | 164 | stats->mv_in_out_count, |
michael@0 | 165 | stats->new_mv_count, |
michael@0 | 166 | stats->count, |
michael@0 | 167 | stats->duration); |
michael@0 | 168 | fclose(fpfile); |
michael@0 | 169 | } |
michael@0 | 170 | #endif |
michael@0 | 171 | } |
michael@0 | 172 | |
michael@0 | 173 | static void zero_stats(FIRSTPASS_STATS *section) |
michael@0 | 174 | { |
michael@0 | 175 | section->frame = 0.0; |
michael@0 | 176 | section->intra_error = 0.0; |
michael@0 | 177 | section->coded_error = 0.0; |
michael@0 | 178 | section->ssim_weighted_pred_err = 0.0; |
michael@0 | 179 | section->pcnt_inter = 0.0; |
michael@0 | 180 | section->pcnt_motion = 0.0; |
michael@0 | 181 | section->pcnt_second_ref = 0.0; |
michael@0 | 182 | section->pcnt_neutral = 0.0; |
michael@0 | 183 | section->MVr = 0.0; |
michael@0 | 184 | section->mvr_abs = 0.0; |
michael@0 | 185 | section->MVc = 0.0; |
michael@0 | 186 | section->mvc_abs = 0.0; |
michael@0 | 187 | section->MVrv = 0.0; |
michael@0 | 188 | section->MVcv = 0.0; |
michael@0 | 189 | section->mv_in_out_count = 0.0; |
michael@0 | 190 | section->new_mv_count = 0.0; |
michael@0 | 191 | section->count = 0.0; |
michael@0 | 192 | section->duration = 1.0; |
michael@0 | 193 | } |
michael@0 | 194 | |
michael@0 | 195 | static void accumulate_stats(FIRSTPASS_STATS *section, FIRSTPASS_STATS *frame) |
michael@0 | 196 | { |
michael@0 | 197 | section->frame += frame->frame; |
michael@0 | 198 | section->intra_error += frame->intra_error; |
michael@0 | 199 | section->coded_error += frame->coded_error; |
michael@0 | 200 | section->ssim_weighted_pred_err += frame->ssim_weighted_pred_err; |
michael@0 | 201 | section->pcnt_inter += frame->pcnt_inter; |
michael@0 | 202 | section->pcnt_motion += frame->pcnt_motion; |
michael@0 | 203 | section->pcnt_second_ref += frame->pcnt_second_ref; |
michael@0 | 204 | section->pcnt_neutral += frame->pcnt_neutral; |
michael@0 | 205 | section->MVr += frame->MVr; |
michael@0 | 206 | section->mvr_abs += frame->mvr_abs; |
michael@0 | 207 | section->MVc += frame->MVc; |
michael@0 | 208 | section->mvc_abs += frame->mvc_abs; |
michael@0 | 209 | section->MVrv += frame->MVrv; |
michael@0 | 210 | section->MVcv += frame->MVcv; |
michael@0 | 211 | section->mv_in_out_count += frame->mv_in_out_count; |
michael@0 | 212 | section->new_mv_count += frame->new_mv_count; |
michael@0 | 213 | section->count += frame->count; |
michael@0 | 214 | section->duration += frame->duration; |
michael@0 | 215 | } |
michael@0 | 216 | |
michael@0 | 217 | static void subtract_stats(FIRSTPASS_STATS *section, FIRSTPASS_STATS *frame) |
michael@0 | 218 | { |
michael@0 | 219 | section->frame -= frame->frame; |
michael@0 | 220 | section->intra_error -= frame->intra_error; |
michael@0 | 221 | section->coded_error -= frame->coded_error; |
michael@0 | 222 | section->ssim_weighted_pred_err -= frame->ssim_weighted_pred_err; |
michael@0 | 223 | section->pcnt_inter -= frame->pcnt_inter; |
michael@0 | 224 | section->pcnt_motion -= frame->pcnt_motion; |
michael@0 | 225 | section->pcnt_second_ref -= frame->pcnt_second_ref; |
michael@0 | 226 | section->pcnt_neutral -= frame->pcnt_neutral; |
michael@0 | 227 | section->MVr -= frame->MVr; |
michael@0 | 228 | section->mvr_abs -= frame->mvr_abs; |
michael@0 | 229 | section->MVc -= frame->MVc; |
michael@0 | 230 | section->mvc_abs -= frame->mvc_abs; |
michael@0 | 231 | section->MVrv -= frame->MVrv; |
michael@0 | 232 | section->MVcv -= frame->MVcv; |
michael@0 | 233 | section->mv_in_out_count -= frame->mv_in_out_count; |
michael@0 | 234 | section->new_mv_count -= frame->new_mv_count; |
michael@0 | 235 | section->count -= frame->count; |
michael@0 | 236 | section->duration -= frame->duration; |
michael@0 | 237 | } |
michael@0 | 238 | |
michael@0 | 239 | static void avg_stats(FIRSTPASS_STATS *section) |
michael@0 | 240 | { |
michael@0 | 241 | if (section->count < 1.0) |
michael@0 | 242 | return; |
michael@0 | 243 | |
michael@0 | 244 | section->intra_error /= section->count; |
michael@0 | 245 | section->coded_error /= section->count; |
michael@0 | 246 | section->ssim_weighted_pred_err /= section->count; |
michael@0 | 247 | section->pcnt_inter /= section->count; |
michael@0 | 248 | section->pcnt_second_ref /= section->count; |
michael@0 | 249 | section->pcnt_neutral /= section->count; |
michael@0 | 250 | section->pcnt_motion /= section->count; |
michael@0 | 251 | section->MVr /= section->count; |
michael@0 | 252 | section->mvr_abs /= section->count; |
michael@0 | 253 | section->MVc /= section->count; |
michael@0 | 254 | section->mvc_abs /= section->count; |
michael@0 | 255 | section->MVrv /= section->count; |
michael@0 | 256 | section->MVcv /= section->count; |
michael@0 | 257 | section->mv_in_out_count /= section->count; |
michael@0 | 258 | section->duration /= section->count; |
michael@0 | 259 | } |
michael@0 | 260 | |
michael@0 | 261 | /* Calculate a modified Error used in distributing bits between easier |
michael@0 | 262 | * and harder frames |
michael@0 | 263 | */ |
michael@0 | 264 | static double calculate_modified_err(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame) |
michael@0 | 265 | { |
michael@0 | 266 | double av_err = ( cpi->twopass.total_stats.ssim_weighted_pred_err / |
michael@0 | 267 | cpi->twopass.total_stats.count ); |
michael@0 | 268 | double this_err = this_frame->ssim_weighted_pred_err; |
michael@0 | 269 | double modified_err; |
michael@0 | 270 | |
michael@0 | 271 | if (this_err > av_err) |
michael@0 | 272 | modified_err = av_err * pow((this_err / DOUBLE_DIVIDE_CHECK(av_err)), POW1); |
michael@0 | 273 | else |
michael@0 | 274 | modified_err = av_err * pow((this_err / DOUBLE_DIVIDE_CHECK(av_err)), POW2); |
michael@0 | 275 | |
michael@0 | 276 | return modified_err; |
michael@0 | 277 | } |
michael@0 | 278 | |
michael@0 | 279 | static const double weight_table[256] = { |
michael@0 | 280 | 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, |
michael@0 | 281 | 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, |
michael@0 | 282 | 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, |
michael@0 | 283 | 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, |
michael@0 | 284 | 0.020000, 0.031250, 0.062500, 0.093750, 0.125000, 0.156250, 0.187500, 0.218750, |
michael@0 | 285 | 0.250000, 0.281250, 0.312500, 0.343750, 0.375000, 0.406250, 0.437500, 0.468750, |
michael@0 | 286 | 0.500000, 0.531250, 0.562500, 0.593750, 0.625000, 0.656250, 0.687500, 0.718750, |
michael@0 | 287 | 0.750000, 0.781250, 0.812500, 0.843750, 0.875000, 0.906250, 0.937500, 0.968750, |
michael@0 | 288 | 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, |
michael@0 | 289 | 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, |
michael@0 | 290 | 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, |
michael@0 | 291 | 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, |
michael@0 | 292 | 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, |
michael@0 | 293 | 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, |
michael@0 | 294 | 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, |
michael@0 | 295 | 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, |
michael@0 | 296 | 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, |
michael@0 | 297 | 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, |
michael@0 | 298 | 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, |
michael@0 | 299 | 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, |
michael@0 | 300 | 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, |
michael@0 | 301 | 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, |
michael@0 | 302 | 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, |
michael@0 | 303 | 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, |
michael@0 | 304 | 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, |
michael@0 | 305 | 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, |
michael@0 | 306 | 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, |
michael@0 | 307 | 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, |
michael@0 | 308 | 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, |
michael@0 | 309 | 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, |
michael@0 | 310 | 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, |
michael@0 | 311 | 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000 |
michael@0 | 312 | }; |
michael@0 | 313 | |
michael@0 | 314 | static double simple_weight(YV12_BUFFER_CONFIG *source) |
michael@0 | 315 | { |
michael@0 | 316 | int i, j; |
michael@0 | 317 | |
michael@0 | 318 | unsigned char *src = source->y_buffer; |
michael@0 | 319 | double sum_weights = 0.0; |
michael@0 | 320 | |
michael@0 | 321 | /* Loop throught the Y plane raw examining levels and creating a weight |
michael@0 | 322 | * for the image |
michael@0 | 323 | */ |
michael@0 | 324 | i = source->y_height; |
michael@0 | 325 | do |
michael@0 | 326 | { |
michael@0 | 327 | j = source->y_width; |
michael@0 | 328 | do |
michael@0 | 329 | { |
michael@0 | 330 | sum_weights += weight_table[ *src]; |
michael@0 | 331 | src++; |
michael@0 | 332 | }while(--j); |
michael@0 | 333 | src -= source->y_width; |
michael@0 | 334 | src += source->y_stride; |
michael@0 | 335 | }while(--i); |
michael@0 | 336 | |
michael@0 | 337 | sum_weights /= (source->y_height * source->y_width); |
michael@0 | 338 | |
michael@0 | 339 | return sum_weights; |
michael@0 | 340 | } |
michael@0 | 341 | |
michael@0 | 342 | |
michael@0 | 343 | /* This function returns the current per frame maximum bitrate target */ |
michael@0 | 344 | static int frame_max_bits(VP8_COMP *cpi) |
michael@0 | 345 | { |
michael@0 | 346 | /* Max allocation for a single frame based on the max section guidelines |
michael@0 | 347 | * passed in and how many bits are left |
michael@0 | 348 | */ |
michael@0 | 349 | int max_bits; |
michael@0 | 350 | |
michael@0 | 351 | /* For CBR we need to also consider buffer fullness. |
michael@0 | 352 | * If we are running below the optimal level then we need to gradually |
michael@0 | 353 | * tighten up on max_bits. |
michael@0 | 354 | */ |
michael@0 | 355 | if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) |
michael@0 | 356 | { |
michael@0 | 357 | double buffer_fullness_ratio = (double)cpi->buffer_level / DOUBLE_DIVIDE_CHECK((double)cpi->oxcf.optimal_buffer_level); |
michael@0 | 358 | |
michael@0 | 359 | /* For CBR base this on the target average bits per frame plus the |
michael@0 | 360 | * maximum sedction rate passed in by the user |
michael@0 | 361 | */ |
michael@0 | 362 | max_bits = (int)(cpi->av_per_frame_bandwidth * ((double)cpi->oxcf.two_pass_vbrmax_section / 100.0)); |
michael@0 | 363 | |
michael@0 | 364 | /* If our buffer is below the optimum level */ |
michael@0 | 365 | if (buffer_fullness_ratio < 1.0) |
michael@0 | 366 | { |
michael@0 | 367 | /* The lower of max_bits / 4 or cpi->av_per_frame_bandwidth / 4. */ |
michael@0 | 368 | int min_max_bits = ((cpi->av_per_frame_bandwidth >> 2) < (max_bits >> 2)) ? cpi->av_per_frame_bandwidth >> 2 : max_bits >> 2; |
michael@0 | 369 | |
michael@0 | 370 | max_bits = (int)(max_bits * buffer_fullness_ratio); |
michael@0 | 371 | |
michael@0 | 372 | /* Lowest value we will set ... which should allow the buffer to |
michael@0 | 373 | * refill. |
michael@0 | 374 | */ |
michael@0 | 375 | if (max_bits < min_max_bits) |
michael@0 | 376 | max_bits = min_max_bits; |
michael@0 | 377 | } |
michael@0 | 378 | } |
michael@0 | 379 | /* VBR */ |
michael@0 | 380 | else |
michael@0 | 381 | { |
michael@0 | 382 | /* For VBR base this on the bits and frames left plus the |
michael@0 | 383 | * two_pass_vbrmax_section rate passed in by the user |
michael@0 | 384 | */ |
michael@0 | 385 | max_bits = (int)(((double)cpi->twopass.bits_left / (cpi->twopass.total_stats.count - (double)cpi->common.current_video_frame)) * ((double)cpi->oxcf.two_pass_vbrmax_section / 100.0)); |
michael@0 | 386 | } |
michael@0 | 387 | |
michael@0 | 388 | /* Trap case where we are out of bits */ |
michael@0 | 389 | if (max_bits < 0) |
michael@0 | 390 | max_bits = 0; |
michael@0 | 391 | |
michael@0 | 392 | return max_bits; |
michael@0 | 393 | } |
michael@0 | 394 | |
michael@0 | 395 | void vp8_init_first_pass(VP8_COMP *cpi) |
michael@0 | 396 | { |
michael@0 | 397 | zero_stats(&cpi->twopass.total_stats); |
michael@0 | 398 | } |
michael@0 | 399 | |
michael@0 | 400 | void vp8_end_first_pass(VP8_COMP *cpi) |
michael@0 | 401 | { |
michael@0 | 402 | output_stats(cpi, cpi->output_pkt_list, &cpi->twopass.total_stats); |
michael@0 | 403 | } |
michael@0 | 404 | |
michael@0 | 405 | static void zz_motion_search( VP8_COMP *cpi, MACROBLOCK * x, |
michael@0 | 406 | YV12_BUFFER_CONFIG * raw_buffer, |
michael@0 | 407 | int * raw_motion_err, |
michael@0 | 408 | YV12_BUFFER_CONFIG * recon_buffer, |
michael@0 | 409 | int * best_motion_err, int recon_yoffset) |
michael@0 | 410 | { |
michael@0 | 411 | MACROBLOCKD * const xd = & x->e_mbd; |
michael@0 | 412 | BLOCK *b = &x->block[0]; |
michael@0 | 413 | BLOCKD *d = &x->e_mbd.block[0]; |
michael@0 | 414 | |
michael@0 | 415 | unsigned char *src_ptr = (*(b->base_src) + b->src); |
michael@0 | 416 | int src_stride = b->src_stride; |
michael@0 | 417 | unsigned char *raw_ptr; |
michael@0 | 418 | int raw_stride = raw_buffer->y_stride; |
michael@0 | 419 | unsigned char *ref_ptr; |
michael@0 | 420 | int ref_stride = x->e_mbd.pre.y_stride; |
michael@0 | 421 | |
michael@0 | 422 | /* Set up pointers for this macro block raw buffer */ |
michael@0 | 423 | raw_ptr = (unsigned char *)(raw_buffer->y_buffer + recon_yoffset |
michael@0 | 424 | + d->offset); |
michael@0 | 425 | vp8_mse16x16 ( src_ptr, src_stride, raw_ptr, raw_stride, |
michael@0 | 426 | (unsigned int *)(raw_motion_err)); |
michael@0 | 427 | |
michael@0 | 428 | /* Set up pointers for this macro block recon buffer */ |
michael@0 | 429 | xd->pre.y_buffer = recon_buffer->y_buffer + recon_yoffset; |
michael@0 | 430 | ref_ptr = (unsigned char *)(xd->pre.y_buffer + d->offset ); |
michael@0 | 431 | vp8_mse16x16 ( src_ptr, src_stride, ref_ptr, ref_stride, |
michael@0 | 432 | (unsigned int *)(best_motion_err)); |
michael@0 | 433 | } |
michael@0 | 434 | |
michael@0 | 435 | static void first_pass_motion_search(VP8_COMP *cpi, MACROBLOCK *x, |
michael@0 | 436 | int_mv *ref_mv, MV *best_mv, |
michael@0 | 437 | YV12_BUFFER_CONFIG *recon_buffer, |
michael@0 | 438 | int *best_motion_err, int recon_yoffset ) |
michael@0 | 439 | { |
michael@0 | 440 | MACROBLOCKD *const xd = & x->e_mbd; |
michael@0 | 441 | BLOCK *b = &x->block[0]; |
michael@0 | 442 | BLOCKD *d = &x->e_mbd.block[0]; |
michael@0 | 443 | int num00; |
michael@0 | 444 | |
michael@0 | 445 | int_mv tmp_mv; |
michael@0 | 446 | int_mv ref_mv_full; |
michael@0 | 447 | |
michael@0 | 448 | int tmp_err; |
michael@0 | 449 | int step_param = 3; /* Dont search over full range for first pass */ |
michael@0 | 450 | int further_steps = (MAX_MVSEARCH_STEPS - 1) - step_param; |
michael@0 | 451 | int n; |
michael@0 | 452 | vp8_variance_fn_ptr_t v_fn_ptr = cpi->fn_ptr[BLOCK_16X16]; |
michael@0 | 453 | int new_mv_mode_penalty = 256; |
michael@0 | 454 | |
michael@0 | 455 | /* override the default variance function to use MSE */ |
michael@0 | 456 | v_fn_ptr.vf = vp8_mse16x16; |
michael@0 | 457 | |
michael@0 | 458 | /* Set up pointers for this macro block recon buffer */ |
michael@0 | 459 | xd->pre.y_buffer = recon_buffer->y_buffer + recon_yoffset; |
michael@0 | 460 | |
michael@0 | 461 | /* Initial step/diamond search centred on best mv */ |
michael@0 | 462 | tmp_mv.as_int = 0; |
michael@0 | 463 | ref_mv_full.as_mv.col = ref_mv->as_mv.col>>3; |
michael@0 | 464 | ref_mv_full.as_mv.row = ref_mv->as_mv.row>>3; |
michael@0 | 465 | tmp_err = cpi->diamond_search_sad(x, b, d, &ref_mv_full, &tmp_mv, step_param, |
michael@0 | 466 | x->sadperbit16, &num00, &v_fn_ptr, |
michael@0 | 467 | x->mvcost, ref_mv); |
michael@0 | 468 | if ( tmp_err < INT_MAX-new_mv_mode_penalty ) |
michael@0 | 469 | tmp_err += new_mv_mode_penalty; |
michael@0 | 470 | |
michael@0 | 471 | if (tmp_err < *best_motion_err) |
michael@0 | 472 | { |
michael@0 | 473 | *best_motion_err = tmp_err; |
michael@0 | 474 | best_mv->row = tmp_mv.as_mv.row; |
michael@0 | 475 | best_mv->col = tmp_mv.as_mv.col; |
michael@0 | 476 | } |
michael@0 | 477 | |
michael@0 | 478 | /* Further step/diamond searches as necessary */ |
michael@0 | 479 | n = num00; |
michael@0 | 480 | num00 = 0; |
michael@0 | 481 | |
michael@0 | 482 | while (n < further_steps) |
michael@0 | 483 | { |
michael@0 | 484 | n++; |
michael@0 | 485 | |
michael@0 | 486 | if (num00) |
michael@0 | 487 | num00--; |
michael@0 | 488 | else |
michael@0 | 489 | { |
michael@0 | 490 | tmp_err = cpi->diamond_search_sad(x, b, d, &ref_mv_full, &tmp_mv, |
michael@0 | 491 | step_param + n, x->sadperbit16, |
michael@0 | 492 | &num00, &v_fn_ptr, x->mvcost, |
michael@0 | 493 | ref_mv); |
michael@0 | 494 | if ( tmp_err < INT_MAX-new_mv_mode_penalty ) |
michael@0 | 495 | tmp_err += new_mv_mode_penalty; |
michael@0 | 496 | |
michael@0 | 497 | if (tmp_err < *best_motion_err) |
michael@0 | 498 | { |
michael@0 | 499 | *best_motion_err = tmp_err; |
michael@0 | 500 | best_mv->row = tmp_mv.as_mv.row; |
michael@0 | 501 | best_mv->col = tmp_mv.as_mv.col; |
michael@0 | 502 | } |
michael@0 | 503 | } |
michael@0 | 504 | } |
michael@0 | 505 | } |
michael@0 | 506 | |
michael@0 | 507 | void vp8_first_pass(VP8_COMP *cpi) |
michael@0 | 508 | { |
michael@0 | 509 | int mb_row, mb_col; |
michael@0 | 510 | MACROBLOCK *const x = & cpi->mb; |
michael@0 | 511 | VP8_COMMON *const cm = & cpi->common; |
michael@0 | 512 | MACROBLOCKD *const xd = & x->e_mbd; |
michael@0 | 513 | |
michael@0 | 514 | int recon_yoffset, recon_uvoffset; |
michael@0 | 515 | YV12_BUFFER_CONFIG *lst_yv12 = &cm->yv12_fb[cm->lst_fb_idx]; |
michael@0 | 516 | YV12_BUFFER_CONFIG *new_yv12 = &cm->yv12_fb[cm->new_fb_idx]; |
michael@0 | 517 | YV12_BUFFER_CONFIG *gld_yv12 = &cm->yv12_fb[cm->gld_fb_idx]; |
michael@0 | 518 | int recon_y_stride = lst_yv12->y_stride; |
michael@0 | 519 | int recon_uv_stride = lst_yv12->uv_stride; |
michael@0 | 520 | int64_t intra_error = 0; |
michael@0 | 521 | int64_t coded_error = 0; |
michael@0 | 522 | |
michael@0 | 523 | int sum_mvr = 0, sum_mvc = 0; |
michael@0 | 524 | int sum_mvr_abs = 0, sum_mvc_abs = 0; |
michael@0 | 525 | int sum_mvrs = 0, sum_mvcs = 0; |
michael@0 | 526 | int mvcount = 0; |
michael@0 | 527 | int intercount = 0; |
michael@0 | 528 | int second_ref_count = 0; |
michael@0 | 529 | int intrapenalty = 256; |
michael@0 | 530 | int neutral_count = 0; |
michael@0 | 531 | int new_mv_count = 0; |
michael@0 | 532 | int sum_in_vectors = 0; |
michael@0 | 533 | uint32_t lastmv_as_int = 0; |
michael@0 | 534 | |
michael@0 | 535 | int_mv zero_ref_mv; |
michael@0 | 536 | |
michael@0 | 537 | zero_ref_mv.as_int = 0; |
michael@0 | 538 | |
michael@0 | 539 | vp8_clear_system_state(); |
michael@0 | 540 | |
michael@0 | 541 | x->src = * cpi->Source; |
michael@0 | 542 | xd->pre = *lst_yv12; |
michael@0 | 543 | xd->dst = *new_yv12; |
michael@0 | 544 | |
michael@0 | 545 | x->partition_info = x->pi; |
michael@0 | 546 | |
michael@0 | 547 | xd->mode_info_context = cm->mi; |
michael@0 | 548 | |
michael@0 | 549 | if(!cm->use_bilinear_mc_filter) |
michael@0 | 550 | { |
michael@0 | 551 | xd->subpixel_predict = vp8_sixtap_predict4x4; |
michael@0 | 552 | xd->subpixel_predict8x4 = vp8_sixtap_predict8x4; |
michael@0 | 553 | xd->subpixel_predict8x8 = vp8_sixtap_predict8x8; |
michael@0 | 554 | xd->subpixel_predict16x16 = vp8_sixtap_predict16x16; |
michael@0 | 555 | } |
michael@0 | 556 | else |
michael@0 | 557 | { |
michael@0 | 558 | xd->subpixel_predict = vp8_bilinear_predict4x4; |
michael@0 | 559 | xd->subpixel_predict8x4 = vp8_bilinear_predict8x4; |
michael@0 | 560 | xd->subpixel_predict8x8 = vp8_bilinear_predict8x8; |
michael@0 | 561 | xd->subpixel_predict16x16 = vp8_bilinear_predict16x16; |
michael@0 | 562 | } |
michael@0 | 563 | |
michael@0 | 564 | vp8_build_block_offsets(x); |
michael@0 | 565 | |
michael@0 | 566 | /* set up frame new frame for intra coded blocks */ |
michael@0 | 567 | vp8_setup_intra_recon(new_yv12); |
michael@0 | 568 | vp8cx_frame_init_quantizer(cpi); |
michael@0 | 569 | |
michael@0 | 570 | /* Initialise the MV cost table to the defaults */ |
michael@0 | 571 | { |
michael@0 | 572 | int flag[2] = {1, 1}; |
michael@0 | 573 | vp8_initialize_rd_consts(cpi, x, vp8_dc_quant(cm->base_qindex, cm->y1dc_delta_q)); |
michael@0 | 574 | vpx_memcpy(cm->fc.mvc, vp8_default_mv_context, sizeof(vp8_default_mv_context)); |
michael@0 | 575 | vp8_build_component_cost_table(cpi->mb.mvcost, (const MV_CONTEXT *) cm->fc.mvc, flag); |
michael@0 | 576 | } |
michael@0 | 577 | |
michael@0 | 578 | /* for each macroblock row in image */ |
michael@0 | 579 | for (mb_row = 0; mb_row < cm->mb_rows; mb_row++) |
michael@0 | 580 | { |
michael@0 | 581 | int_mv best_ref_mv; |
michael@0 | 582 | |
michael@0 | 583 | best_ref_mv.as_int = 0; |
michael@0 | 584 | |
michael@0 | 585 | /* reset above block coeffs */ |
michael@0 | 586 | xd->up_available = (mb_row != 0); |
michael@0 | 587 | recon_yoffset = (mb_row * recon_y_stride * 16); |
michael@0 | 588 | recon_uvoffset = (mb_row * recon_uv_stride * 8); |
michael@0 | 589 | |
michael@0 | 590 | /* Set up limit values for motion vectors to prevent them extending |
michael@0 | 591 | * outside the UMV borders |
michael@0 | 592 | */ |
michael@0 | 593 | x->mv_row_min = -((mb_row * 16) + (VP8BORDERINPIXELS - 16)); |
michael@0 | 594 | x->mv_row_max = ((cm->mb_rows - 1 - mb_row) * 16) + (VP8BORDERINPIXELS - 16); |
michael@0 | 595 | |
michael@0 | 596 | |
michael@0 | 597 | /* for each macroblock col in image */ |
michael@0 | 598 | for (mb_col = 0; mb_col < cm->mb_cols; mb_col++) |
michael@0 | 599 | { |
michael@0 | 600 | int this_error; |
michael@0 | 601 | int gf_motion_error = INT_MAX; |
michael@0 | 602 | int use_dc_pred = (mb_col || mb_row) && (!mb_col || !mb_row); |
michael@0 | 603 | |
michael@0 | 604 | xd->dst.y_buffer = new_yv12->y_buffer + recon_yoffset; |
michael@0 | 605 | xd->dst.u_buffer = new_yv12->u_buffer + recon_uvoffset; |
michael@0 | 606 | xd->dst.v_buffer = new_yv12->v_buffer + recon_uvoffset; |
michael@0 | 607 | xd->left_available = (mb_col != 0); |
michael@0 | 608 | |
michael@0 | 609 | /* Copy current mb to a buffer */ |
michael@0 | 610 | vp8_copy_mem16x16(x->src.y_buffer, x->src.y_stride, x->thismb, 16); |
michael@0 | 611 | |
michael@0 | 612 | /* do intra 16x16 prediction */ |
michael@0 | 613 | this_error = vp8_encode_intra(cpi, x, use_dc_pred); |
michael@0 | 614 | |
michael@0 | 615 | /* "intrapenalty" below deals with situations where the intra |
michael@0 | 616 | * and inter error scores are very low (eg a plain black frame) |
michael@0 | 617 | * We do not have special cases in first pass for 0,0 and |
michael@0 | 618 | * nearest etc so all inter modes carry an overhead cost |
michael@0 | 619 | * estimate fot the mv. When the error score is very low this |
michael@0 | 620 | * causes us to pick all or lots of INTRA modes and throw lots |
michael@0 | 621 | * of key frames. This penalty adds a cost matching that of a |
michael@0 | 622 | * 0,0 mv to the intra case. |
michael@0 | 623 | */ |
michael@0 | 624 | this_error += intrapenalty; |
michael@0 | 625 | |
michael@0 | 626 | /* Cumulative intra error total */ |
michael@0 | 627 | intra_error += (int64_t)this_error; |
michael@0 | 628 | |
michael@0 | 629 | /* Set up limit values for motion vectors to prevent them |
michael@0 | 630 | * extending outside the UMV borders |
michael@0 | 631 | */ |
michael@0 | 632 | x->mv_col_min = -((mb_col * 16) + (VP8BORDERINPIXELS - 16)); |
michael@0 | 633 | x->mv_col_max = ((cm->mb_cols - 1 - mb_col) * 16) + (VP8BORDERINPIXELS - 16); |
michael@0 | 634 | |
michael@0 | 635 | /* Other than for the first frame do a motion search */ |
michael@0 | 636 | if (cm->current_video_frame > 0) |
michael@0 | 637 | { |
michael@0 | 638 | BLOCKD *d = &x->e_mbd.block[0]; |
michael@0 | 639 | MV tmp_mv = {0, 0}; |
michael@0 | 640 | int tmp_err; |
michael@0 | 641 | int motion_error = INT_MAX; |
michael@0 | 642 | int raw_motion_error = INT_MAX; |
michael@0 | 643 | |
michael@0 | 644 | /* Simple 0,0 motion with no mv overhead */ |
michael@0 | 645 | zz_motion_search( cpi, x, cpi->last_frame_unscaled_source, |
michael@0 | 646 | &raw_motion_error, lst_yv12, &motion_error, |
michael@0 | 647 | recon_yoffset ); |
michael@0 | 648 | d->bmi.mv.as_mv.row = 0; |
michael@0 | 649 | d->bmi.mv.as_mv.col = 0; |
michael@0 | 650 | |
michael@0 | 651 | if (raw_motion_error < cpi->oxcf.encode_breakout) |
michael@0 | 652 | goto skip_motion_search; |
michael@0 | 653 | |
michael@0 | 654 | /* Test last reference frame using the previous best mv as the |
michael@0 | 655 | * starting point (best reference) for the search |
michael@0 | 656 | */ |
michael@0 | 657 | first_pass_motion_search(cpi, x, &best_ref_mv, |
michael@0 | 658 | &d->bmi.mv.as_mv, lst_yv12, |
michael@0 | 659 | &motion_error, recon_yoffset); |
michael@0 | 660 | |
michael@0 | 661 | /* If the current best reference mv is not centred on 0,0 |
michael@0 | 662 | * then do a 0,0 based search as well |
michael@0 | 663 | */ |
michael@0 | 664 | if (best_ref_mv.as_int) |
michael@0 | 665 | { |
michael@0 | 666 | tmp_err = INT_MAX; |
michael@0 | 667 | first_pass_motion_search(cpi, x, &zero_ref_mv, &tmp_mv, |
michael@0 | 668 | lst_yv12, &tmp_err, recon_yoffset); |
michael@0 | 669 | |
michael@0 | 670 | if ( tmp_err < motion_error ) |
michael@0 | 671 | { |
michael@0 | 672 | motion_error = tmp_err; |
michael@0 | 673 | d->bmi.mv.as_mv.row = tmp_mv.row; |
michael@0 | 674 | d->bmi.mv.as_mv.col = tmp_mv.col; |
michael@0 | 675 | } |
michael@0 | 676 | } |
michael@0 | 677 | |
michael@0 | 678 | /* Experimental search in a second reference frame ((0,0) |
michael@0 | 679 | * based only) |
michael@0 | 680 | */ |
michael@0 | 681 | if (cm->current_video_frame > 1) |
michael@0 | 682 | { |
michael@0 | 683 | first_pass_motion_search(cpi, x, &zero_ref_mv, &tmp_mv, gld_yv12, &gf_motion_error, recon_yoffset); |
michael@0 | 684 | |
michael@0 | 685 | if ((gf_motion_error < motion_error) && (gf_motion_error < this_error)) |
michael@0 | 686 | { |
michael@0 | 687 | second_ref_count++; |
michael@0 | 688 | } |
michael@0 | 689 | |
michael@0 | 690 | /* Reset to last frame as reference buffer */ |
michael@0 | 691 | xd->pre.y_buffer = lst_yv12->y_buffer + recon_yoffset; |
michael@0 | 692 | xd->pre.u_buffer = lst_yv12->u_buffer + recon_uvoffset; |
michael@0 | 693 | xd->pre.v_buffer = lst_yv12->v_buffer + recon_uvoffset; |
michael@0 | 694 | } |
michael@0 | 695 | |
michael@0 | 696 | skip_motion_search: |
michael@0 | 697 | /* Intra assumed best */ |
michael@0 | 698 | best_ref_mv.as_int = 0; |
michael@0 | 699 | |
michael@0 | 700 | if (motion_error <= this_error) |
michael@0 | 701 | { |
michael@0 | 702 | /* Keep a count of cases where the inter and intra were |
michael@0 | 703 | * very close and very low. This helps with scene cut |
michael@0 | 704 | * detection for example in cropped clips with black bars |
michael@0 | 705 | * at the sides or top and bottom. |
michael@0 | 706 | */ |
michael@0 | 707 | if( (((this_error-intrapenalty) * 9) <= |
michael@0 | 708 | (motion_error*10)) && |
michael@0 | 709 | (this_error < (2*intrapenalty)) ) |
michael@0 | 710 | { |
michael@0 | 711 | neutral_count++; |
michael@0 | 712 | } |
michael@0 | 713 | |
michael@0 | 714 | d->bmi.mv.as_mv.row *= 8; |
michael@0 | 715 | d->bmi.mv.as_mv.col *= 8; |
michael@0 | 716 | this_error = motion_error; |
michael@0 | 717 | vp8_set_mbmode_and_mvs(x, NEWMV, &d->bmi.mv); |
michael@0 | 718 | vp8_encode_inter16x16y(x); |
michael@0 | 719 | sum_mvr += d->bmi.mv.as_mv.row; |
michael@0 | 720 | sum_mvr_abs += abs(d->bmi.mv.as_mv.row); |
michael@0 | 721 | sum_mvc += d->bmi.mv.as_mv.col; |
michael@0 | 722 | sum_mvc_abs += abs(d->bmi.mv.as_mv.col); |
michael@0 | 723 | sum_mvrs += d->bmi.mv.as_mv.row * d->bmi.mv.as_mv.row; |
michael@0 | 724 | sum_mvcs += d->bmi.mv.as_mv.col * d->bmi.mv.as_mv.col; |
michael@0 | 725 | intercount++; |
michael@0 | 726 | |
michael@0 | 727 | best_ref_mv.as_int = d->bmi.mv.as_int; |
michael@0 | 728 | |
michael@0 | 729 | /* Was the vector non-zero */ |
michael@0 | 730 | if (d->bmi.mv.as_int) |
michael@0 | 731 | { |
michael@0 | 732 | mvcount++; |
michael@0 | 733 | |
michael@0 | 734 | /* Was it different from the last non zero vector */ |
michael@0 | 735 | if ( d->bmi.mv.as_int != lastmv_as_int ) |
michael@0 | 736 | new_mv_count++; |
michael@0 | 737 | lastmv_as_int = d->bmi.mv.as_int; |
michael@0 | 738 | |
michael@0 | 739 | /* Does the Row vector point inwards or outwards */ |
michael@0 | 740 | if (mb_row < cm->mb_rows / 2) |
michael@0 | 741 | { |
michael@0 | 742 | if (d->bmi.mv.as_mv.row > 0) |
michael@0 | 743 | sum_in_vectors--; |
michael@0 | 744 | else if (d->bmi.mv.as_mv.row < 0) |
michael@0 | 745 | sum_in_vectors++; |
michael@0 | 746 | } |
michael@0 | 747 | else if (mb_row > cm->mb_rows / 2) |
michael@0 | 748 | { |
michael@0 | 749 | if (d->bmi.mv.as_mv.row > 0) |
michael@0 | 750 | sum_in_vectors++; |
michael@0 | 751 | else if (d->bmi.mv.as_mv.row < 0) |
michael@0 | 752 | sum_in_vectors--; |
michael@0 | 753 | } |
michael@0 | 754 | |
michael@0 | 755 | /* Does the Row vector point inwards or outwards */ |
michael@0 | 756 | if (mb_col < cm->mb_cols / 2) |
michael@0 | 757 | { |
michael@0 | 758 | if (d->bmi.mv.as_mv.col > 0) |
michael@0 | 759 | sum_in_vectors--; |
michael@0 | 760 | else if (d->bmi.mv.as_mv.col < 0) |
michael@0 | 761 | sum_in_vectors++; |
michael@0 | 762 | } |
michael@0 | 763 | else if (mb_col > cm->mb_cols / 2) |
michael@0 | 764 | { |
michael@0 | 765 | if (d->bmi.mv.as_mv.col > 0) |
michael@0 | 766 | sum_in_vectors++; |
michael@0 | 767 | else if (d->bmi.mv.as_mv.col < 0) |
michael@0 | 768 | sum_in_vectors--; |
michael@0 | 769 | } |
michael@0 | 770 | } |
michael@0 | 771 | } |
michael@0 | 772 | } |
michael@0 | 773 | |
michael@0 | 774 | coded_error += (int64_t)this_error; |
michael@0 | 775 | |
michael@0 | 776 | /* adjust to the next column of macroblocks */ |
michael@0 | 777 | x->src.y_buffer += 16; |
michael@0 | 778 | x->src.u_buffer += 8; |
michael@0 | 779 | x->src.v_buffer += 8; |
michael@0 | 780 | |
michael@0 | 781 | recon_yoffset += 16; |
michael@0 | 782 | recon_uvoffset += 8; |
michael@0 | 783 | } |
michael@0 | 784 | |
michael@0 | 785 | /* adjust to the next row of mbs */ |
michael@0 | 786 | x->src.y_buffer += 16 * x->src.y_stride - 16 * cm->mb_cols; |
michael@0 | 787 | x->src.u_buffer += 8 * x->src.uv_stride - 8 * cm->mb_cols; |
michael@0 | 788 | x->src.v_buffer += 8 * x->src.uv_stride - 8 * cm->mb_cols; |
michael@0 | 789 | |
michael@0 | 790 | /* extend the recon for intra prediction */ |
michael@0 | 791 | vp8_extend_mb_row(new_yv12, xd->dst.y_buffer + 16, xd->dst.u_buffer + 8, xd->dst.v_buffer + 8); |
michael@0 | 792 | vp8_clear_system_state(); |
michael@0 | 793 | } |
michael@0 | 794 | |
michael@0 | 795 | vp8_clear_system_state(); |
michael@0 | 796 | { |
michael@0 | 797 | double weight = 0.0; |
michael@0 | 798 | |
michael@0 | 799 | FIRSTPASS_STATS fps; |
michael@0 | 800 | |
michael@0 | 801 | fps.frame = cm->current_video_frame ; |
michael@0 | 802 | fps.intra_error = (double)(intra_error >> 8); |
michael@0 | 803 | fps.coded_error = (double)(coded_error >> 8); |
michael@0 | 804 | weight = simple_weight(cpi->Source); |
michael@0 | 805 | |
michael@0 | 806 | |
michael@0 | 807 | if (weight < 0.1) |
michael@0 | 808 | weight = 0.1; |
michael@0 | 809 | |
michael@0 | 810 | fps.ssim_weighted_pred_err = fps.coded_error * weight; |
michael@0 | 811 | |
michael@0 | 812 | fps.pcnt_inter = 0.0; |
michael@0 | 813 | fps.pcnt_motion = 0.0; |
michael@0 | 814 | fps.MVr = 0.0; |
michael@0 | 815 | fps.mvr_abs = 0.0; |
michael@0 | 816 | fps.MVc = 0.0; |
michael@0 | 817 | fps.mvc_abs = 0.0; |
michael@0 | 818 | fps.MVrv = 0.0; |
michael@0 | 819 | fps.MVcv = 0.0; |
michael@0 | 820 | fps.mv_in_out_count = 0.0; |
michael@0 | 821 | fps.new_mv_count = 0.0; |
michael@0 | 822 | fps.count = 1.0; |
michael@0 | 823 | |
michael@0 | 824 | fps.pcnt_inter = 1.0 * (double)intercount / cm->MBs; |
michael@0 | 825 | fps.pcnt_second_ref = 1.0 * (double)second_ref_count / cm->MBs; |
michael@0 | 826 | fps.pcnt_neutral = 1.0 * (double)neutral_count / cm->MBs; |
michael@0 | 827 | |
michael@0 | 828 | if (mvcount > 0) |
michael@0 | 829 | { |
michael@0 | 830 | fps.MVr = (double)sum_mvr / (double)mvcount; |
michael@0 | 831 | fps.mvr_abs = (double)sum_mvr_abs / (double)mvcount; |
michael@0 | 832 | fps.MVc = (double)sum_mvc / (double)mvcount; |
michael@0 | 833 | fps.mvc_abs = (double)sum_mvc_abs / (double)mvcount; |
michael@0 | 834 | fps.MVrv = ((double)sum_mvrs - (fps.MVr * fps.MVr / (double)mvcount)) / (double)mvcount; |
michael@0 | 835 | fps.MVcv = ((double)sum_mvcs - (fps.MVc * fps.MVc / (double)mvcount)) / (double)mvcount; |
michael@0 | 836 | fps.mv_in_out_count = (double)sum_in_vectors / (double)(mvcount * 2); |
michael@0 | 837 | fps.new_mv_count = new_mv_count; |
michael@0 | 838 | |
michael@0 | 839 | fps.pcnt_motion = 1.0 * (double)mvcount / cpi->common.MBs; |
michael@0 | 840 | } |
michael@0 | 841 | |
michael@0 | 842 | /* TODO: handle the case when duration is set to 0, or something less |
michael@0 | 843 | * than the full time between subsequent cpi->source_time_stamps |
michael@0 | 844 | */ |
michael@0 | 845 | fps.duration = (double)(cpi->source->ts_end |
michael@0 | 846 | - cpi->source->ts_start); |
michael@0 | 847 | |
michael@0 | 848 | /* don't want to do output stats with a stack variable! */ |
michael@0 | 849 | memcpy(&cpi->twopass.this_frame_stats, |
michael@0 | 850 | &fps, |
michael@0 | 851 | sizeof(FIRSTPASS_STATS)); |
michael@0 | 852 | output_stats(cpi, cpi->output_pkt_list, &cpi->twopass.this_frame_stats); |
michael@0 | 853 | accumulate_stats(&cpi->twopass.total_stats, &fps); |
michael@0 | 854 | } |
michael@0 | 855 | |
michael@0 | 856 | /* Copy the previous Last Frame into the GF buffer if specific |
michael@0 | 857 | * conditions for doing so are met |
michael@0 | 858 | */ |
michael@0 | 859 | if ((cm->current_video_frame > 0) && |
michael@0 | 860 | (cpi->twopass.this_frame_stats.pcnt_inter > 0.20) && |
michael@0 | 861 | ((cpi->twopass.this_frame_stats.intra_error / |
michael@0 | 862 | DOUBLE_DIVIDE_CHECK(cpi->twopass.this_frame_stats.coded_error)) > |
michael@0 | 863 | 2.0)) |
michael@0 | 864 | { |
michael@0 | 865 | vp8_yv12_copy_frame(lst_yv12, gld_yv12); |
michael@0 | 866 | } |
michael@0 | 867 | |
michael@0 | 868 | /* swap frame pointers so last frame refers to the frame we just |
michael@0 | 869 | * compressed |
michael@0 | 870 | */ |
michael@0 | 871 | vp8_swap_yv12_buffer(lst_yv12, new_yv12); |
michael@0 | 872 | vp8_yv12_extend_frame_borders(lst_yv12); |
michael@0 | 873 | |
michael@0 | 874 | /* Special case for the first frame. Copy into the GF buffer as a |
michael@0 | 875 | * second reference. |
michael@0 | 876 | */ |
michael@0 | 877 | if (cm->current_video_frame == 0) |
michael@0 | 878 | { |
michael@0 | 879 | vp8_yv12_copy_frame(lst_yv12, gld_yv12); |
michael@0 | 880 | } |
michael@0 | 881 | |
michael@0 | 882 | |
michael@0 | 883 | /* use this to see what the first pass reconstruction looks like */ |
michael@0 | 884 | if (0) |
michael@0 | 885 | { |
michael@0 | 886 | char filename[512]; |
michael@0 | 887 | FILE *recon_file; |
michael@0 | 888 | sprintf(filename, "enc%04d.yuv", (int) cm->current_video_frame); |
michael@0 | 889 | |
michael@0 | 890 | if (cm->current_video_frame == 0) |
michael@0 | 891 | recon_file = fopen(filename, "wb"); |
michael@0 | 892 | else |
michael@0 | 893 | recon_file = fopen(filename, "ab"); |
michael@0 | 894 | |
michael@0 | 895 | (void) fwrite(lst_yv12->buffer_alloc, lst_yv12->frame_size, 1, |
michael@0 | 896 | recon_file); |
michael@0 | 897 | fclose(recon_file); |
michael@0 | 898 | } |
michael@0 | 899 | |
michael@0 | 900 | cm->current_video_frame++; |
michael@0 | 901 | |
michael@0 | 902 | } |
michael@0 | 903 | extern const int vp8_bits_per_mb[2][QINDEX_RANGE]; |
michael@0 | 904 | |
michael@0 | 905 | /* Estimate a cost per mb attributable to overheads such as the coding of |
michael@0 | 906 | * modes and motion vectors. |
michael@0 | 907 | * Currently simplistic in its assumptions for testing. |
michael@0 | 908 | */ |
michael@0 | 909 | |
michael@0 | 910 | static double bitcost( double prob ) |
michael@0 | 911 | { |
michael@0 | 912 | if (prob > 0.000122) |
michael@0 | 913 | return -log(prob) / log(2.0); |
michael@0 | 914 | else |
michael@0 | 915 | return 13.0; |
michael@0 | 916 | } |
michael@0 | 917 | static int64_t estimate_modemvcost(VP8_COMP *cpi, |
michael@0 | 918 | FIRSTPASS_STATS * fpstats) |
michael@0 | 919 | { |
michael@0 | 920 | int mv_cost; |
michael@0 | 921 | int64_t mode_cost; |
michael@0 | 922 | |
michael@0 | 923 | double av_pct_inter = fpstats->pcnt_inter / fpstats->count; |
michael@0 | 924 | double av_pct_motion = fpstats->pcnt_motion / fpstats->count; |
michael@0 | 925 | double av_intra = (1.0 - av_pct_inter); |
michael@0 | 926 | |
michael@0 | 927 | double zz_cost; |
michael@0 | 928 | double motion_cost; |
michael@0 | 929 | double intra_cost; |
michael@0 | 930 | |
michael@0 | 931 | zz_cost = bitcost(av_pct_inter - av_pct_motion); |
michael@0 | 932 | motion_cost = bitcost(av_pct_motion); |
michael@0 | 933 | intra_cost = bitcost(av_intra); |
michael@0 | 934 | |
michael@0 | 935 | /* Estimate of extra bits per mv overhead for mbs |
michael@0 | 936 | * << 9 is the normalization to the (bits * 512) used in vp8_bits_per_mb |
michael@0 | 937 | */ |
michael@0 | 938 | mv_cost = ((int)(fpstats->new_mv_count / fpstats->count) * 8) << 9; |
michael@0 | 939 | |
michael@0 | 940 | /* Crude estimate of overhead cost from modes |
michael@0 | 941 | * << 9 is the normalization to (bits * 512) used in vp8_bits_per_mb |
michael@0 | 942 | */ |
michael@0 | 943 | mode_cost =((((av_pct_inter - av_pct_motion) * zz_cost) + |
michael@0 | 944 | (av_pct_motion * motion_cost) + |
michael@0 | 945 | (av_intra * intra_cost)) * cpi->common.MBs) * 512; |
michael@0 | 946 | |
michael@0 | 947 | return mv_cost + mode_cost; |
michael@0 | 948 | } |
michael@0 | 949 | |
michael@0 | 950 | static double calc_correction_factor( double err_per_mb, |
michael@0 | 951 | double err_devisor, |
michael@0 | 952 | double pt_low, |
michael@0 | 953 | double pt_high, |
michael@0 | 954 | int Q ) |
michael@0 | 955 | { |
michael@0 | 956 | double power_term; |
michael@0 | 957 | double error_term = err_per_mb / err_devisor; |
michael@0 | 958 | double correction_factor; |
michael@0 | 959 | |
michael@0 | 960 | /* Adjustment based on Q to power term. */ |
michael@0 | 961 | power_term = pt_low + (Q * 0.01); |
michael@0 | 962 | power_term = (power_term > pt_high) ? pt_high : power_term; |
michael@0 | 963 | |
michael@0 | 964 | /* Adjustments to error term */ |
michael@0 | 965 | /* TBD */ |
michael@0 | 966 | |
michael@0 | 967 | /* Calculate correction factor */ |
michael@0 | 968 | correction_factor = pow(error_term, power_term); |
michael@0 | 969 | |
michael@0 | 970 | /* Clip range */ |
michael@0 | 971 | correction_factor = |
michael@0 | 972 | (correction_factor < 0.05) |
michael@0 | 973 | ? 0.05 : (correction_factor > 5.0) ? 5.0 : correction_factor; |
michael@0 | 974 | |
michael@0 | 975 | return correction_factor; |
michael@0 | 976 | } |
michael@0 | 977 | |
michael@0 | 978 | static int estimate_max_q(VP8_COMP *cpi, |
michael@0 | 979 | FIRSTPASS_STATS * fpstats, |
michael@0 | 980 | int section_target_bandwitdh, |
michael@0 | 981 | int overhead_bits ) |
michael@0 | 982 | { |
michael@0 | 983 | int Q; |
michael@0 | 984 | int num_mbs = cpi->common.MBs; |
michael@0 | 985 | int target_norm_bits_per_mb; |
michael@0 | 986 | |
michael@0 | 987 | double section_err = (fpstats->coded_error / fpstats->count); |
michael@0 | 988 | double err_per_mb = section_err / num_mbs; |
michael@0 | 989 | double err_correction_factor; |
michael@0 | 990 | double speed_correction = 1.0; |
michael@0 | 991 | int overhead_bits_per_mb; |
michael@0 | 992 | |
michael@0 | 993 | if (section_target_bandwitdh <= 0) |
michael@0 | 994 | return cpi->twopass.maxq_max_limit; /* Highest value allowed */ |
michael@0 | 995 | |
michael@0 | 996 | target_norm_bits_per_mb = |
michael@0 | 997 | (section_target_bandwitdh < (1 << 20)) |
michael@0 | 998 | ? (512 * section_target_bandwitdh) / num_mbs |
michael@0 | 999 | : 512 * (section_target_bandwitdh / num_mbs); |
michael@0 | 1000 | |
michael@0 | 1001 | /* Calculate a corrective factor based on a rolling ratio of bits spent |
michael@0 | 1002 | * vs target bits |
michael@0 | 1003 | */ |
michael@0 | 1004 | if ((cpi->rolling_target_bits > 0) && |
michael@0 | 1005 | (cpi->active_worst_quality < cpi->worst_quality)) |
michael@0 | 1006 | { |
michael@0 | 1007 | double rolling_ratio; |
michael@0 | 1008 | |
michael@0 | 1009 | rolling_ratio = (double)cpi->rolling_actual_bits / |
michael@0 | 1010 | (double)cpi->rolling_target_bits; |
michael@0 | 1011 | |
michael@0 | 1012 | if (rolling_ratio < 0.95) |
michael@0 | 1013 | cpi->twopass.est_max_qcorrection_factor -= 0.005; |
michael@0 | 1014 | else if (rolling_ratio > 1.05) |
michael@0 | 1015 | cpi->twopass.est_max_qcorrection_factor += 0.005; |
michael@0 | 1016 | |
michael@0 | 1017 | cpi->twopass.est_max_qcorrection_factor = |
michael@0 | 1018 | (cpi->twopass.est_max_qcorrection_factor < 0.1) |
michael@0 | 1019 | ? 0.1 |
michael@0 | 1020 | : (cpi->twopass.est_max_qcorrection_factor > 10.0) |
michael@0 | 1021 | ? 10.0 : cpi->twopass.est_max_qcorrection_factor; |
michael@0 | 1022 | } |
michael@0 | 1023 | |
michael@0 | 1024 | /* Corrections for higher compression speed settings |
michael@0 | 1025 | * (reduced compression expected) |
michael@0 | 1026 | */ |
michael@0 | 1027 | if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1)) |
michael@0 | 1028 | { |
michael@0 | 1029 | if (cpi->oxcf.cpu_used <= 5) |
michael@0 | 1030 | speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04); |
michael@0 | 1031 | else |
michael@0 | 1032 | speed_correction = 1.25; |
michael@0 | 1033 | } |
michael@0 | 1034 | |
michael@0 | 1035 | /* Estimate of overhead bits per mb */ |
michael@0 | 1036 | /* Correction to overhead bits for min allowed Q. */ |
michael@0 | 1037 | overhead_bits_per_mb = overhead_bits / num_mbs; |
michael@0 | 1038 | overhead_bits_per_mb = (int)(overhead_bits_per_mb * |
michael@0 | 1039 | pow( 0.98, (double)cpi->twopass.maxq_min_limit )); |
michael@0 | 1040 | |
michael@0 | 1041 | /* Try and pick a max Q that will be high enough to encode the |
michael@0 | 1042 | * content at the given rate. |
michael@0 | 1043 | */ |
michael@0 | 1044 | for (Q = cpi->twopass.maxq_min_limit; Q < cpi->twopass.maxq_max_limit; Q++) |
michael@0 | 1045 | { |
michael@0 | 1046 | int bits_per_mb_at_this_q; |
michael@0 | 1047 | |
michael@0 | 1048 | /* Error per MB based correction factor */ |
michael@0 | 1049 | err_correction_factor = |
michael@0 | 1050 | calc_correction_factor(err_per_mb, 150.0, 0.40, 0.90, Q); |
michael@0 | 1051 | |
michael@0 | 1052 | bits_per_mb_at_this_q = |
michael@0 | 1053 | vp8_bits_per_mb[INTER_FRAME][Q] + overhead_bits_per_mb; |
michael@0 | 1054 | |
michael@0 | 1055 | bits_per_mb_at_this_q = (int)(.5 + err_correction_factor |
michael@0 | 1056 | * speed_correction * cpi->twopass.est_max_qcorrection_factor |
michael@0 | 1057 | * cpi->twopass.section_max_qfactor |
michael@0 | 1058 | * (double)bits_per_mb_at_this_q); |
michael@0 | 1059 | |
michael@0 | 1060 | /* Mode and motion overhead */ |
michael@0 | 1061 | /* As Q rises in real encode loop rd code will force overhead down |
michael@0 | 1062 | * We make a crude adjustment for this here as *.98 per Q step. |
michael@0 | 1063 | */ |
michael@0 | 1064 | overhead_bits_per_mb = (int)((double)overhead_bits_per_mb * 0.98); |
michael@0 | 1065 | |
michael@0 | 1066 | if (bits_per_mb_at_this_q <= target_norm_bits_per_mb) |
michael@0 | 1067 | break; |
michael@0 | 1068 | } |
michael@0 | 1069 | |
michael@0 | 1070 | /* Restriction on active max q for constrained quality mode. */ |
michael@0 | 1071 | if ( (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) && |
michael@0 | 1072 | (Q < cpi->cq_target_quality) ) |
michael@0 | 1073 | { |
michael@0 | 1074 | Q = cpi->cq_target_quality; |
michael@0 | 1075 | } |
michael@0 | 1076 | |
michael@0 | 1077 | /* Adjust maxq_min_limit and maxq_max_limit limits based on |
michael@0 | 1078 | * average q observed in clip for non kf/gf.arf frames |
michael@0 | 1079 | * Give average a chance to settle though. |
michael@0 | 1080 | */ |
michael@0 | 1081 | if ( (cpi->ni_frames > |
michael@0 | 1082 | ((int)cpi->twopass.total_stats.count >> 8)) && |
michael@0 | 1083 | (cpi->ni_frames > 150) ) |
michael@0 | 1084 | { |
michael@0 | 1085 | cpi->twopass.maxq_max_limit = ((cpi->ni_av_qi + 32) < cpi->worst_quality) |
michael@0 | 1086 | ? (cpi->ni_av_qi + 32) : cpi->worst_quality; |
michael@0 | 1087 | cpi->twopass.maxq_min_limit = ((cpi->ni_av_qi - 32) > cpi->best_quality) |
michael@0 | 1088 | ? (cpi->ni_av_qi - 32) : cpi->best_quality; |
michael@0 | 1089 | } |
michael@0 | 1090 | |
michael@0 | 1091 | return Q; |
michael@0 | 1092 | } |
michael@0 | 1093 | |
michael@0 | 1094 | /* For cq mode estimate a cq level that matches the observed |
michael@0 | 1095 | * complexity and data rate. |
michael@0 | 1096 | */ |
michael@0 | 1097 | static int estimate_cq( VP8_COMP *cpi, |
michael@0 | 1098 | FIRSTPASS_STATS * fpstats, |
michael@0 | 1099 | int section_target_bandwitdh, |
michael@0 | 1100 | int overhead_bits ) |
michael@0 | 1101 | { |
michael@0 | 1102 | int Q; |
michael@0 | 1103 | int num_mbs = cpi->common.MBs; |
michael@0 | 1104 | int target_norm_bits_per_mb; |
michael@0 | 1105 | |
michael@0 | 1106 | double section_err = (fpstats->coded_error / fpstats->count); |
michael@0 | 1107 | double err_per_mb = section_err / num_mbs; |
michael@0 | 1108 | double err_correction_factor; |
michael@0 | 1109 | double speed_correction = 1.0; |
michael@0 | 1110 | double clip_iiratio; |
michael@0 | 1111 | double clip_iifactor; |
michael@0 | 1112 | int overhead_bits_per_mb; |
michael@0 | 1113 | |
michael@0 | 1114 | if (0) |
michael@0 | 1115 | { |
michael@0 | 1116 | FILE *f = fopen("epmp.stt", "a"); |
michael@0 | 1117 | fprintf(f, "%10.2f\n", err_per_mb ); |
michael@0 | 1118 | fclose(f); |
michael@0 | 1119 | } |
michael@0 | 1120 | |
michael@0 | 1121 | target_norm_bits_per_mb = (section_target_bandwitdh < (1 << 20)) |
michael@0 | 1122 | ? (512 * section_target_bandwitdh) / num_mbs |
michael@0 | 1123 | : 512 * (section_target_bandwitdh / num_mbs); |
michael@0 | 1124 | |
michael@0 | 1125 | /* Estimate of overhead bits per mb */ |
michael@0 | 1126 | overhead_bits_per_mb = overhead_bits / num_mbs; |
michael@0 | 1127 | |
michael@0 | 1128 | /* Corrections for higher compression speed settings |
michael@0 | 1129 | * (reduced compression expected) |
michael@0 | 1130 | */ |
michael@0 | 1131 | if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1)) |
michael@0 | 1132 | { |
michael@0 | 1133 | if (cpi->oxcf.cpu_used <= 5) |
michael@0 | 1134 | speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04); |
michael@0 | 1135 | else |
michael@0 | 1136 | speed_correction = 1.25; |
michael@0 | 1137 | } |
michael@0 | 1138 | |
michael@0 | 1139 | /* II ratio correction factor for clip as a whole */ |
michael@0 | 1140 | clip_iiratio = cpi->twopass.total_stats.intra_error / |
michael@0 | 1141 | DOUBLE_DIVIDE_CHECK(cpi->twopass.total_stats.coded_error); |
michael@0 | 1142 | clip_iifactor = 1.0 - ((clip_iiratio - 10.0) * 0.025); |
michael@0 | 1143 | if (clip_iifactor < 0.80) |
michael@0 | 1144 | clip_iifactor = 0.80; |
michael@0 | 1145 | |
michael@0 | 1146 | /* Try and pick a Q that can encode the content at the given rate. */ |
michael@0 | 1147 | for (Q = 0; Q < MAXQ; Q++) |
michael@0 | 1148 | { |
michael@0 | 1149 | int bits_per_mb_at_this_q; |
michael@0 | 1150 | |
michael@0 | 1151 | /* Error per MB based correction factor */ |
michael@0 | 1152 | err_correction_factor = |
michael@0 | 1153 | calc_correction_factor(err_per_mb, 100.0, 0.40, 0.90, Q); |
michael@0 | 1154 | |
michael@0 | 1155 | bits_per_mb_at_this_q = |
michael@0 | 1156 | vp8_bits_per_mb[INTER_FRAME][Q] + overhead_bits_per_mb; |
michael@0 | 1157 | |
michael@0 | 1158 | bits_per_mb_at_this_q = |
michael@0 | 1159 | (int)( .5 + err_correction_factor * |
michael@0 | 1160 | speed_correction * |
michael@0 | 1161 | clip_iifactor * |
michael@0 | 1162 | (double)bits_per_mb_at_this_q); |
michael@0 | 1163 | |
michael@0 | 1164 | /* Mode and motion overhead */ |
michael@0 | 1165 | /* As Q rises in real encode loop rd code will force overhead down |
michael@0 | 1166 | * We make a crude adjustment for this here as *.98 per Q step. |
michael@0 | 1167 | */ |
michael@0 | 1168 | overhead_bits_per_mb = (int)((double)overhead_bits_per_mb * 0.98); |
michael@0 | 1169 | |
michael@0 | 1170 | if (bits_per_mb_at_this_q <= target_norm_bits_per_mb) |
michael@0 | 1171 | break; |
michael@0 | 1172 | } |
michael@0 | 1173 | |
michael@0 | 1174 | /* Clip value to range "best allowed to (worst allowed - 1)" */ |
michael@0 | 1175 | Q = cq_level[Q]; |
michael@0 | 1176 | if ( Q >= cpi->worst_quality ) |
michael@0 | 1177 | Q = cpi->worst_quality - 1; |
michael@0 | 1178 | if ( Q < cpi->best_quality ) |
michael@0 | 1179 | Q = cpi->best_quality; |
michael@0 | 1180 | |
michael@0 | 1181 | return Q; |
michael@0 | 1182 | } |
michael@0 | 1183 | |
michael@0 | 1184 | static int estimate_q(VP8_COMP *cpi, double section_err, int section_target_bandwitdh) |
michael@0 | 1185 | { |
michael@0 | 1186 | int Q; |
michael@0 | 1187 | int num_mbs = cpi->common.MBs; |
michael@0 | 1188 | int target_norm_bits_per_mb; |
michael@0 | 1189 | |
michael@0 | 1190 | double err_per_mb = section_err / num_mbs; |
michael@0 | 1191 | double err_correction_factor; |
michael@0 | 1192 | double speed_correction = 1.0; |
michael@0 | 1193 | |
michael@0 | 1194 | target_norm_bits_per_mb = (section_target_bandwitdh < (1 << 20)) ? (512 * section_target_bandwitdh) / num_mbs : 512 * (section_target_bandwitdh / num_mbs); |
michael@0 | 1195 | |
michael@0 | 1196 | /* Corrections for higher compression speed settings |
michael@0 | 1197 | * (reduced compression expected) |
michael@0 | 1198 | */ |
michael@0 | 1199 | if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1)) |
michael@0 | 1200 | { |
michael@0 | 1201 | if (cpi->oxcf.cpu_used <= 5) |
michael@0 | 1202 | speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04); |
michael@0 | 1203 | else |
michael@0 | 1204 | speed_correction = 1.25; |
michael@0 | 1205 | } |
michael@0 | 1206 | |
michael@0 | 1207 | /* Try and pick a Q that can encode the content at the given rate. */ |
michael@0 | 1208 | for (Q = 0; Q < MAXQ; Q++) |
michael@0 | 1209 | { |
michael@0 | 1210 | int bits_per_mb_at_this_q; |
michael@0 | 1211 | |
michael@0 | 1212 | /* Error per MB based correction factor */ |
michael@0 | 1213 | err_correction_factor = |
michael@0 | 1214 | calc_correction_factor(err_per_mb, 150.0, 0.40, 0.90, Q); |
michael@0 | 1215 | |
michael@0 | 1216 | bits_per_mb_at_this_q = |
michael@0 | 1217 | (int)( .5 + ( err_correction_factor * |
michael@0 | 1218 | speed_correction * |
michael@0 | 1219 | cpi->twopass.est_max_qcorrection_factor * |
michael@0 | 1220 | (double)vp8_bits_per_mb[INTER_FRAME][Q] / 1.0 ) ); |
michael@0 | 1221 | |
michael@0 | 1222 | if (bits_per_mb_at_this_q <= target_norm_bits_per_mb) |
michael@0 | 1223 | break; |
michael@0 | 1224 | } |
michael@0 | 1225 | |
michael@0 | 1226 | return Q; |
michael@0 | 1227 | } |
michael@0 | 1228 | |
michael@0 | 1229 | /* Estimate a worst case Q for a KF group */ |
michael@0 | 1230 | static int estimate_kf_group_q(VP8_COMP *cpi, double section_err, int section_target_bandwitdh, double group_iiratio) |
michael@0 | 1231 | { |
michael@0 | 1232 | int Q; |
michael@0 | 1233 | int num_mbs = cpi->common.MBs; |
michael@0 | 1234 | int target_norm_bits_per_mb = (512 * section_target_bandwitdh) / num_mbs; |
michael@0 | 1235 | int bits_per_mb_at_this_q; |
michael@0 | 1236 | |
michael@0 | 1237 | double err_per_mb = section_err / num_mbs; |
michael@0 | 1238 | double err_correction_factor; |
michael@0 | 1239 | double speed_correction = 1.0; |
michael@0 | 1240 | double current_spend_ratio = 1.0; |
michael@0 | 1241 | |
michael@0 | 1242 | double pow_highq = (POW1 < 0.6) ? POW1 + 0.3 : 0.90; |
michael@0 | 1243 | double pow_lowq = (POW1 < 0.7) ? POW1 + 0.1 : 0.80; |
michael@0 | 1244 | |
michael@0 | 1245 | double iiratio_correction_factor = 1.0; |
michael@0 | 1246 | |
michael@0 | 1247 | double combined_correction_factor; |
michael@0 | 1248 | |
michael@0 | 1249 | /* Trap special case where the target is <= 0 */ |
michael@0 | 1250 | if (target_norm_bits_per_mb <= 0) |
michael@0 | 1251 | return MAXQ * 2; |
michael@0 | 1252 | |
michael@0 | 1253 | /* Calculate a corrective factor based on a rolling ratio of bits spent |
michael@0 | 1254 | * vs target bits |
michael@0 | 1255 | * This is clamped to the range 0.1 to 10.0 |
michael@0 | 1256 | */ |
michael@0 | 1257 | if (cpi->long_rolling_target_bits <= 0) |
michael@0 | 1258 | current_spend_ratio = 10.0; |
michael@0 | 1259 | else |
michael@0 | 1260 | { |
michael@0 | 1261 | current_spend_ratio = (double)cpi->long_rolling_actual_bits / (double)cpi->long_rolling_target_bits; |
michael@0 | 1262 | current_spend_ratio = (current_spend_ratio > 10.0) ? 10.0 : (current_spend_ratio < 0.1) ? 0.1 : current_spend_ratio; |
michael@0 | 1263 | } |
michael@0 | 1264 | |
michael@0 | 1265 | /* Calculate a correction factor based on the quality of prediction in |
michael@0 | 1266 | * the sequence as indicated by intra_inter error score ratio (IIRatio) |
michael@0 | 1267 | * The idea here is to favour subsampling in the hardest sections vs |
michael@0 | 1268 | * the easyest. |
michael@0 | 1269 | */ |
michael@0 | 1270 | iiratio_correction_factor = 1.0 - ((group_iiratio - 6.0) * 0.1); |
michael@0 | 1271 | |
michael@0 | 1272 | if (iiratio_correction_factor < 0.5) |
michael@0 | 1273 | iiratio_correction_factor = 0.5; |
michael@0 | 1274 | |
michael@0 | 1275 | /* Corrections for higher compression speed settings |
michael@0 | 1276 | * (reduced compression expected) |
michael@0 | 1277 | */ |
michael@0 | 1278 | if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1)) |
michael@0 | 1279 | { |
michael@0 | 1280 | if (cpi->oxcf.cpu_used <= 5) |
michael@0 | 1281 | speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04); |
michael@0 | 1282 | else |
michael@0 | 1283 | speed_correction = 1.25; |
michael@0 | 1284 | } |
michael@0 | 1285 | |
michael@0 | 1286 | /* Combine the various factors calculated above */ |
michael@0 | 1287 | combined_correction_factor = speed_correction * iiratio_correction_factor * current_spend_ratio; |
michael@0 | 1288 | |
michael@0 | 1289 | /* Try and pick a Q that should be high enough to encode the content at |
michael@0 | 1290 | * the given rate. |
michael@0 | 1291 | */ |
michael@0 | 1292 | for (Q = 0; Q < MAXQ; Q++) |
michael@0 | 1293 | { |
michael@0 | 1294 | /* Error per MB based correction factor */ |
michael@0 | 1295 | err_correction_factor = |
michael@0 | 1296 | calc_correction_factor(err_per_mb, 150.0, pow_lowq, pow_highq, Q); |
michael@0 | 1297 | |
michael@0 | 1298 | bits_per_mb_at_this_q = |
michael@0 | 1299 | (int)(.5 + ( err_correction_factor * |
michael@0 | 1300 | combined_correction_factor * |
michael@0 | 1301 | (double)vp8_bits_per_mb[INTER_FRAME][Q]) ); |
michael@0 | 1302 | |
michael@0 | 1303 | if (bits_per_mb_at_this_q <= target_norm_bits_per_mb) |
michael@0 | 1304 | break; |
michael@0 | 1305 | } |
michael@0 | 1306 | |
michael@0 | 1307 | /* If we could not hit the target even at Max Q then estimate what Q |
michael@0 | 1308 | * would have been required |
michael@0 | 1309 | */ |
michael@0 | 1310 | while ((bits_per_mb_at_this_q > target_norm_bits_per_mb) && (Q < (MAXQ * 2))) |
michael@0 | 1311 | { |
michael@0 | 1312 | |
michael@0 | 1313 | bits_per_mb_at_this_q = (int)(0.96 * bits_per_mb_at_this_q); |
michael@0 | 1314 | Q++; |
michael@0 | 1315 | } |
michael@0 | 1316 | |
michael@0 | 1317 | if (0) |
michael@0 | 1318 | { |
michael@0 | 1319 | FILE *f = fopen("estkf_q.stt", "a"); |
michael@0 | 1320 | fprintf(f, "%8d %8d %8d %8.2f %8.3f %8.2f %8.3f %8.3f %8.3f %8d\n", cpi->common.current_video_frame, bits_per_mb_at_this_q, |
michael@0 | 1321 | target_norm_bits_per_mb, err_per_mb, err_correction_factor, |
michael@0 | 1322 | current_spend_ratio, group_iiratio, iiratio_correction_factor, |
michael@0 | 1323 | (double)cpi->buffer_level / (double)cpi->oxcf.optimal_buffer_level, Q); |
michael@0 | 1324 | fclose(f); |
michael@0 | 1325 | } |
michael@0 | 1326 | |
michael@0 | 1327 | return Q; |
michael@0 | 1328 | } |
michael@0 | 1329 | |
michael@0 | 1330 | extern void vp8_new_framerate(VP8_COMP *cpi, double framerate); |
michael@0 | 1331 | |
michael@0 | 1332 | void vp8_init_second_pass(VP8_COMP *cpi) |
michael@0 | 1333 | { |
michael@0 | 1334 | FIRSTPASS_STATS this_frame; |
michael@0 | 1335 | FIRSTPASS_STATS *start_pos; |
michael@0 | 1336 | |
michael@0 | 1337 | double two_pass_min_rate = (double)(cpi->oxcf.target_bandwidth * cpi->oxcf.two_pass_vbrmin_section / 100); |
michael@0 | 1338 | |
michael@0 | 1339 | zero_stats(&cpi->twopass.total_stats); |
michael@0 | 1340 | zero_stats(&cpi->twopass.total_left_stats); |
michael@0 | 1341 | |
michael@0 | 1342 | if (!cpi->twopass.stats_in_end) |
michael@0 | 1343 | return; |
michael@0 | 1344 | |
michael@0 | 1345 | cpi->twopass.total_stats = *cpi->twopass.stats_in_end; |
michael@0 | 1346 | cpi->twopass.total_left_stats = cpi->twopass.total_stats; |
michael@0 | 1347 | |
michael@0 | 1348 | /* each frame can have a different duration, as the frame rate in the |
michael@0 | 1349 | * source isn't guaranteed to be constant. The frame rate prior to |
michael@0 | 1350 | * the first frame encoded in the second pass is a guess. However the |
michael@0 | 1351 | * sum duration is not. Its calculated based on the actual durations of |
michael@0 | 1352 | * all frames from the first pass. |
michael@0 | 1353 | */ |
michael@0 | 1354 | vp8_new_framerate(cpi, 10000000.0 * cpi->twopass.total_stats.count / cpi->twopass.total_stats.duration); |
michael@0 | 1355 | |
michael@0 | 1356 | cpi->output_framerate = cpi->framerate; |
michael@0 | 1357 | cpi->twopass.bits_left = (int64_t)(cpi->twopass.total_stats.duration * cpi->oxcf.target_bandwidth / 10000000.0) ; |
michael@0 | 1358 | cpi->twopass.bits_left -= (int64_t)(cpi->twopass.total_stats.duration * two_pass_min_rate / 10000000.0); |
michael@0 | 1359 | |
michael@0 | 1360 | /* Calculate a minimum intra value to be used in determining the IIratio |
michael@0 | 1361 | * scores used in the second pass. We have this minimum to make sure |
michael@0 | 1362 | * that clips that are static but "low complexity" in the intra domain |
michael@0 | 1363 | * are still boosted appropriately for KF/GF/ARF |
michael@0 | 1364 | */ |
michael@0 | 1365 | cpi->twopass.kf_intra_err_min = KF_MB_INTRA_MIN * cpi->common.MBs; |
michael@0 | 1366 | cpi->twopass.gf_intra_err_min = GF_MB_INTRA_MIN * cpi->common.MBs; |
michael@0 | 1367 | |
michael@0 | 1368 | /* Scan the first pass file and calculate an average Intra / Inter error |
michael@0 | 1369 | * score ratio for the sequence |
michael@0 | 1370 | */ |
michael@0 | 1371 | { |
michael@0 | 1372 | double sum_iiratio = 0.0; |
michael@0 | 1373 | double IIRatio; |
michael@0 | 1374 | |
michael@0 | 1375 | start_pos = cpi->twopass.stats_in; /* Note starting "file" position */ |
michael@0 | 1376 | |
michael@0 | 1377 | while (input_stats(cpi, &this_frame) != EOF) |
michael@0 | 1378 | { |
michael@0 | 1379 | IIRatio = this_frame.intra_error / DOUBLE_DIVIDE_CHECK(this_frame.coded_error); |
michael@0 | 1380 | IIRatio = (IIRatio < 1.0) ? 1.0 : (IIRatio > 20.0) ? 20.0 : IIRatio; |
michael@0 | 1381 | sum_iiratio += IIRatio; |
michael@0 | 1382 | } |
michael@0 | 1383 | |
michael@0 | 1384 | cpi->twopass.avg_iiratio = sum_iiratio / DOUBLE_DIVIDE_CHECK((double)cpi->twopass.total_stats.count); |
michael@0 | 1385 | |
michael@0 | 1386 | /* Reset file position */ |
michael@0 | 1387 | reset_fpf_position(cpi, start_pos); |
michael@0 | 1388 | } |
michael@0 | 1389 | |
michael@0 | 1390 | /* Scan the first pass file and calculate a modified total error based |
michael@0 | 1391 | * upon the bias/power function used to allocate bits |
michael@0 | 1392 | */ |
michael@0 | 1393 | { |
michael@0 | 1394 | start_pos = cpi->twopass.stats_in; /* Note starting "file" position */ |
michael@0 | 1395 | |
michael@0 | 1396 | cpi->twopass.modified_error_total = 0.0; |
michael@0 | 1397 | cpi->twopass.modified_error_used = 0.0; |
michael@0 | 1398 | |
michael@0 | 1399 | while (input_stats(cpi, &this_frame) != EOF) |
michael@0 | 1400 | { |
michael@0 | 1401 | cpi->twopass.modified_error_total += calculate_modified_err(cpi, &this_frame); |
michael@0 | 1402 | } |
michael@0 | 1403 | cpi->twopass.modified_error_left = cpi->twopass.modified_error_total; |
michael@0 | 1404 | |
michael@0 | 1405 | reset_fpf_position(cpi, start_pos); /* Reset file position */ |
michael@0 | 1406 | |
michael@0 | 1407 | } |
michael@0 | 1408 | } |
michael@0 | 1409 | |
michael@0 | 1410 | void vp8_end_second_pass(VP8_COMP *cpi) |
michael@0 | 1411 | { |
michael@0 | 1412 | } |
michael@0 | 1413 | |
michael@0 | 1414 | /* This function gives and estimate of how badly we believe the prediction |
michael@0 | 1415 | * quality is decaying from frame to frame. |
michael@0 | 1416 | */ |
michael@0 | 1417 | static double get_prediction_decay_rate(VP8_COMP *cpi, FIRSTPASS_STATS *next_frame) |
michael@0 | 1418 | { |
michael@0 | 1419 | double prediction_decay_rate; |
michael@0 | 1420 | double motion_decay; |
michael@0 | 1421 | double motion_pct = next_frame->pcnt_motion; |
michael@0 | 1422 | |
michael@0 | 1423 | /* Initial basis is the % mbs inter coded */ |
michael@0 | 1424 | prediction_decay_rate = next_frame->pcnt_inter; |
michael@0 | 1425 | |
michael@0 | 1426 | /* High % motion -> somewhat higher decay rate */ |
michael@0 | 1427 | motion_decay = (1.0 - (motion_pct / 20.0)); |
michael@0 | 1428 | if (motion_decay < prediction_decay_rate) |
michael@0 | 1429 | prediction_decay_rate = motion_decay; |
michael@0 | 1430 | |
michael@0 | 1431 | /* Adjustment to decay rate based on speed of motion */ |
michael@0 | 1432 | { |
michael@0 | 1433 | double this_mv_rabs; |
michael@0 | 1434 | double this_mv_cabs; |
michael@0 | 1435 | double distance_factor; |
michael@0 | 1436 | |
michael@0 | 1437 | this_mv_rabs = fabs(next_frame->mvr_abs * motion_pct); |
michael@0 | 1438 | this_mv_cabs = fabs(next_frame->mvc_abs * motion_pct); |
michael@0 | 1439 | |
michael@0 | 1440 | distance_factor = sqrt((this_mv_rabs * this_mv_rabs) + |
michael@0 | 1441 | (this_mv_cabs * this_mv_cabs)) / 250.0; |
michael@0 | 1442 | distance_factor = ((distance_factor > 1.0) |
michael@0 | 1443 | ? 0.0 : (1.0 - distance_factor)); |
michael@0 | 1444 | if (distance_factor < prediction_decay_rate) |
michael@0 | 1445 | prediction_decay_rate = distance_factor; |
michael@0 | 1446 | } |
michael@0 | 1447 | |
michael@0 | 1448 | return prediction_decay_rate; |
michael@0 | 1449 | } |
michael@0 | 1450 | |
michael@0 | 1451 | /* Function to test for a condition where a complex transition is followed |
michael@0 | 1452 | * by a static section. For example in slide shows where there is a fade |
michael@0 | 1453 | * between slides. This is to help with more optimal kf and gf positioning. |
michael@0 | 1454 | */ |
michael@0 | 1455 | static int detect_transition_to_still( |
michael@0 | 1456 | VP8_COMP *cpi, |
michael@0 | 1457 | int frame_interval, |
michael@0 | 1458 | int still_interval, |
michael@0 | 1459 | double loop_decay_rate, |
michael@0 | 1460 | double decay_accumulator ) |
michael@0 | 1461 | { |
michael@0 | 1462 | int trans_to_still = 0; |
michael@0 | 1463 | |
michael@0 | 1464 | /* Break clause to detect very still sections after motion |
michael@0 | 1465 | * For example a static image after a fade or other transition |
michael@0 | 1466 | * instead of a clean scene cut. |
michael@0 | 1467 | */ |
michael@0 | 1468 | if ( (frame_interval > MIN_GF_INTERVAL) && |
michael@0 | 1469 | (loop_decay_rate >= 0.999) && |
michael@0 | 1470 | (decay_accumulator < 0.9) ) |
michael@0 | 1471 | { |
michael@0 | 1472 | int j; |
michael@0 | 1473 | FIRSTPASS_STATS * position = cpi->twopass.stats_in; |
michael@0 | 1474 | FIRSTPASS_STATS tmp_next_frame; |
michael@0 | 1475 | double decay_rate; |
michael@0 | 1476 | |
michael@0 | 1477 | /* Look ahead a few frames to see if static condition persists... */ |
michael@0 | 1478 | for ( j = 0; j < still_interval; j++ ) |
michael@0 | 1479 | { |
michael@0 | 1480 | if (EOF == input_stats(cpi, &tmp_next_frame)) |
michael@0 | 1481 | break; |
michael@0 | 1482 | |
michael@0 | 1483 | decay_rate = get_prediction_decay_rate(cpi, &tmp_next_frame); |
michael@0 | 1484 | if ( decay_rate < 0.999 ) |
michael@0 | 1485 | break; |
michael@0 | 1486 | } |
michael@0 | 1487 | /* Reset file position */ |
michael@0 | 1488 | reset_fpf_position(cpi, position); |
michael@0 | 1489 | |
michael@0 | 1490 | /* Only if it does do we signal a transition to still */ |
michael@0 | 1491 | if ( j == still_interval ) |
michael@0 | 1492 | trans_to_still = 1; |
michael@0 | 1493 | } |
michael@0 | 1494 | |
michael@0 | 1495 | return trans_to_still; |
michael@0 | 1496 | } |
michael@0 | 1497 | |
michael@0 | 1498 | /* This function detects a flash through the high relative pcnt_second_ref |
michael@0 | 1499 | * score in the frame following a flash frame. The offset passed in should |
michael@0 | 1500 | * reflect this |
michael@0 | 1501 | */ |
michael@0 | 1502 | static int detect_flash( VP8_COMP *cpi, int offset ) |
michael@0 | 1503 | { |
michael@0 | 1504 | FIRSTPASS_STATS next_frame; |
michael@0 | 1505 | |
michael@0 | 1506 | int flash_detected = 0; |
michael@0 | 1507 | |
michael@0 | 1508 | /* Read the frame data. */ |
michael@0 | 1509 | /* The return is 0 (no flash detected) if not a valid frame */ |
michael@0 | 1510 | if ( read_frame_stats(cpi, &next_frame, offset) != EOF ) |
michael@0 | 1511 | { |
michael@0 | 1512 | /* What we are looking for here is a situation where there is a |
michael@0 | 1513 | * brief break in prediction (such as a flash) but subsequent frames |
michael@0 | 1514 | * are reasonably well predicted by an earlier (pre flash) frame. |
michael@0 | 1515 | * The recovery after a flash is indicated by a high pcnt_second_ref |
michael@0 | 1516 | * comapred to pcnt_inter. |
michael@0 | 1517 | */ |
michael@0 | 1518 | if ( (next_frame.pcnt_second_ref > next_frame.pcnt_inter) && |
michael@0 | 1519 | (next_frame.pcnt_second_ref >= 0.5 ) ) |
michael@0 | 1520 | { |
michael@0 | 1521 | flash_detected = 1; |
michael@0 | 1522 | |
michael@0 | 1523 | /*if (1) |
michael@0 | 1524 | { |
michael@0 | 1525 | FILE *f = fopen("flash.stt", "a"); |
michael@0 | 1526 | fprintf(f, "%8.0f %6.2f %6.2f\n", |
michael@0 | 1527 | next_frame.frame, |
michael@0 | 1528 | next_frame.pcnt_inter, |
michael@0 | 1529 | next_frame.pcnt_second_ref); |
michael@0 | 1530 | fclose(f); |
michael@0 | 1531 | }*/ |
michael@0 | 1532 | } |
michael@0 | 1533 | } |
michael@0 | 1534 | |
michael@0 | 1535 | return flash_detected; |
michael@0 | 1536 | } |
michael@0 | 1537 | |
michael@0 | 1538 | /* Update the motion related elements to the GF arf boost calculation */ |
michael@0 | 1539 | static void accumulate_frame_motion_stats( |
michael@0 | 1540 | VP8_COMP *cpi, |
michael@0 | 1541 | FIRSTPASS_STATS * this_frame, |
michael@0 | 1542 | double * this_frame_mv_in_out, |
michael@0 | 1543 | double * mv_in_out_accumulator, |
michael@0 | 1544 | double * abs_mv_in_out_accumulator, |
michael@0 | 1545 | double * mv_ratio_accumulator ) |
michael@0 | 1546 | { |
michael@0 | 1547 | double this_frame_mvr_ratio; |
michael@0 | 1548 | double this_frame_mvc_ratio; |
michael@0 | 1549 | double motion_pct; |
michael@0 | 1550 | |
michael@0 | 1551 | /* Accumulate motion stats. */ |
michael@0 | 1552 | motion_pct = this_frame->pcnt_motion; |
michael@0 | 1553 | |
michael@0 | 1554 | /* Accumulate Motion In/Out of frame stats */ |
michael@0 | 1555 | *this_frame_mv_in_out = this_frame->mv_in_out_count * motion_pct; |
michael@0 | 1556 | *mv_in_out_accumulator += this_frame->mv_in_out_count * motion_pct; |
michael@0 | 1557 | *abs_mv_in_out_accumulator += |
michael@0 | 1558 | fabs(this_frame->mv_in_out_count * motion_pct); |
michael@0 | 1559 | |
michael@0 | 1560 | /* Accumulate a measure of how uniform (or conversely how random) |
michael@0 | 1561 | * the motion field is. (A ratio of absmv / mv) |
michael@0 | 1562 | */ |
michael@0 | 1563 | if (motion_pct > 0.05) |
michael@0 | 1564 | { |
michael@0 | 1565 | this_frame_mvr_ratio = fabs(this_frame->mvr_abs) / |
michael@0 | 1566 | DOUBLE_DIVIDE_CHECK(fabs(this_frame->MVr)); |
michael@0 | 1567 | |
michael@0 | 1568 | this_frame_mvc_ratio = fabs(this_frame->mvc_abs) / |
michael@0 | 1569 | DOUBLE_DIVIDE_CHECK(fabs(this_frame->MVc)); |
michael@0 | 1570 | |
michael@0 | 1571 | *mv_ratio_accumulator += |
michael@0 | 1572 | (this_frame_mvr_ratio < this_frame->mvr_abs) |
michael@0 | 1573 | ? (this_frame_mvr_ratio * motion_pct) |
michael@0 | 1574 | : this_frame->mvr_abs * motion_pct; |
michael@0 | 1575 | |
michael@0 | 1576 | *mv_ratio_accumulator += |
michael@0 | 1577 | (this_frame_mvc_ratio < this_frame->mvc_abs) |
michael@0 | 1578 | ? (this_frame_mvc_ratio * motion_pct) |
michael@0 | 1579 | : this_frame->mvc_abs * motion_pct; |
michael@0 | 1580 | |
michael@0 | 1581 | } |
michael@0 | 1582 | } |
michael@0 | 1583 | |
michael@0 | 1584 | /* Calculate a baseline boost number for the current frame. */ |
michael@0 | 1585 | static double calc_frame_boost( |
michael@0 | 1586 | VP8_COMP *cpi, |
michael@0 | 1587 | FIRSTPASS_STATS * this_frame, |
michael@0 | 1588 | double this_frame_mv_in_out ) |
michael@0 | 1589 | { |
michael@0 | 1590 | double frame_boost; |
michael@0 | 1591 | |
michael@0 | 1592 | /* Underlying boost factor is based on inter intra error ratio */ |
michael@0 | 1593 | if (this_frame->intra_error > cpi->twopass.gf_intra_err_min) |
michael@0 | 1594 | frame_boost = (IIFACTOR * this_frame->intra_error / |
michael@0 | 1595 | DOUBLE_DIVIDE_CHECK(this_frame->coded_error)); |
michael@0 | 1596 | else |
michael@0 | 1597 | frame_boost = (IIFACTOR * cpi->twopass.gf_intra_err_min / |
michael@0 | 1598 | DOUBLE_DIVIDE_CHECK(this_frame->coded_error)); |
michael@0 | 1599 | |
michael@0 | 1600 | /* Increase boost for frames where new data coming into frame |
michael@0 | 1601 | * (eg zoom out). Slightly reduce boost if there is a net balance |
michael@0 | 1602 | * of motion out of the frame (zoom in). |
michael@0 | 1603 | * The range for this_frame_mv_in_out is -1.0 to +1.0 |
michael@0 | 1604 | */ |
michael@0 | 1605 | if (this_frame_mv_in_out > 0.0) |
michael@0 | 1606 | frame_boost += frame_boost * (this_frame_mv_in_out * 2.0); |
michael@0 | 1607 | /* In extreme case boost is halved */ |
michael@0 | 1608 | else |
michael@0 | 1609 | frame_boost += frame_boost * (this_frame_mv_in_out / 2.0); |
michael@0 | 1610 | |
michael@0 | 1611 | /* Clip to maximum */ |
michael@0 | 1612 | if (frame_boost > GF_RMAX) |
michael@0 | 1613 | frame_boost = GF_RMAX; |
michael@0 | 1614 | |
michael@0 | 1615 | return frame_boost; |
michael@0 | 1616 | } |
michael@0 | 1617 | |
michael@0 | 1618 | #if NEW_BOOST |
michael@0 | 1619 | static int calc_arf_boost( |
michael@0 | 1620 | VP8_COMP *cpi, |
michael@0 | 1621 | int offset, |
michael@0 | 1622 | int f_frames, |
michael@0 | 1623 | int b_frames, |
michael@0 | 1624 | int *f_boost, |
michael@0 | 1625 | int *b_boost ) |
michael@0 | 1626 | { |
michael@0 | 1627 | FIRSTPASS_STATS this_frame; |
michael@0 | 1628 | |
michael@0 | 1629 | int i; |
michael@0 | 1630 | double boost_score = 0.0; |
michael@0 | 1631 | double mv_ratio_accumulator = 0.0; |
michael@0 | 1632 | double decay_accumulator = 1.0; |
michael@0 | 1633 | double this_frame_mv_in_out = 0.0; |
michael@0 | 1634 | double mv_in_out_accumulator = 0.0; |
michael@0 | 1635 | double abs_mv_in_out_accumulator = 0.0; |
michael@0 | 1636 | double r; |
michael@0 | 1637 | int flash_detected = 0; |
michael@0 | 1638 | |
michael@0 | 1639 | /* Search forward from the proposed arf/next gf position */ |
michael@0 | 1640 | for ( i = 0; i < f_frames; i++ ) |
michael@0 | 1641 | { |
michael@0 | 1642 | if ( read_frame_stats(cpi, &this_frame, (i+offset)) == EOF ) |
michael@0 | 1643 | break; |
michael@0 | 1644 | |
michael@0 | 1645 | /* Update the motion related elements to the boost calculation */ |
michael@0 | 1646 | accumulate_frame_motion_stats( cpi, &this_frame, |
michael@0 | 1647 | &this_frame_mv_in_out, &mv_in_out_accumulator, |
michael@0 | 1648 | &abs_mv_in_out_accumulator, &mv_ratio_accumulator ); |
michael@0 | 1649 | |
michael@0 | 1650 | /* Calculate the baseline boost number for this frame */ |
michael@0 | 1651 | r = calc_frame_boost( cpi, &this_frame, this_frame_mv_in_out ); |
michael@0 | 1652 | |
michael@0 | 1653 | /* We want to discount the the flash frame itself and the recovery |
michael@0 | 1654 | * frame that follows as both will have poor scores. |
michael@0 | 1655 | */ |
michael@0 | 1656 | flash_detected = detect_flash(cpi, (i+offset)) || |
michael@0 | 1657 | detect_flash(cpi, (i+offset+1)); |
michael@0 | 1658 | |
michael@0 | 1659 | /* Cumulative effect of prediction quality decay */ |
michael@0 | 1660 | if ( !flash_detected ) |
michael@0 | 1661 | { |
michael@0 | 1662 | decay_accumulator = |
michael@0 | 1663 | decay_accumulator * |
michael@0 | 1664 | get_prediction_decay_rate(cpi, &this_frame); |
michael@0 | 1665 | decay_accumulator = |
michael@0 | 1666 | decay_accumulator < 0.1 ? 0.1 : decay_accumulator; |
michael@0 | 1667 | } |
michael@0 | 1668 | boost_score += (decay_accumulator * r); |
michael@0 | 1669 | |
michael@0 | 1670 | /* Break out conditions. */ |
michael@0 | 1671 | if ( (!flash_detected) && |
michael@0 | 1672 | ((mv_ratio_accumulator > 100.0) || |
michael@0 | 1673 | (abs_mv_in_out_accumulator > 3.0) || |
michael@0 | 1674 | (mv_in_out_accumulator < -2.0) ) ) |
michael@0 | 1675 | { |
michael@0 | 1676 | break; |
michael@0 | 1677 | } |
michael@0 | 1678 | } |
michael@0 | 1679 | |
michael@0 | 1680 | *f_boost = (int)(boost_score * 100.0) >> 4; |
michael@0 | 1681 | |
michael@0 | 1682 | /* Reset for backward looking loop */ |
michael@0 | 1683 | boost_score = 0.0; |
michael@0 | 1684 | mv_ratio_accumulator = 0.0; |
michael@0 | 1685 | decay_accumulator = 1.0; |
michael@0 | 1686 | this_frame_mv_in_out = 0.0; |
michael@0 | 1687 | mv_in_out_accumulator = 0.0; |
michael@0 | 1688 | abs_mv_in_out_accumulator = 0.0; |
michael@0 | 1689 | |
michael@0 | 1690 | /* Search forward from the proposed arf/next gf position */ |
michael@0 | 1691 | for ( i = -1; i >= -b_frames; i-- ) |
michael@0 | 1692 | { |
michael@0 | 1693 | if ( read_frame_stats(cpi, &this_frame, (i+offset)) == EOF ) |
michael@0 | 1694 | break; |
michael@0 | 1695 | |
michael@0 | 1696 | /* Update the motion related elements to the boost calculation */ |
michael@0 | 1697 | accumulate_frame_motion_stats( cpi, &this_frame, |
michael@0 | 1698 | &this_frame_mv_in_out, &mv_in_out_accumulator, |
michael@0 | 1699 | &abs_mv_in_out_accumulator, &mv_ratio_accumulator ); |
michael@0 | 1700 | |
michael@0 | 1701 | /* Calculate the baseline boost number for this frame */ |
michael@0 | 1702 | r = calc_frame_boost( cpi, &this_frame, this_frame_mv_in_out ); |
michael@0 | 1703 | |
michael@0 | 1704 | /* We want to discount the the flash frame itself and the recovery |
michael@0 | 1705 | * frame that follows as both will have poor scores. |
michael@0 | 1706 | */ |
michael@0 | 1707 | flash_detected = detect_flash(cpi, (i+offset)) || |
michael@0 | 1708 | detect_flash(cpi, (i+offset+1)); |
michael@0 | 1709 | |
michael@0 | 1710 | /* Cumulative effect of prediction quality decay */ |
michael@0 | 1711 | if ( !flash_detected ) |
michael@0 | 1712 | { |
michael@0 | 1713 | decay_accumulator = |
michael@0 | 1714 | decay_accumulator * |
michael@0 | 1715 | get_prediction_decay_rate(cpi, &this_frame); |
michael@0 | 1716 | decay_accumulator = |
michael@0 | 1717 | decay_accumulator < 0.1 ? 0.1 : decay_accumulator; |
michael@0 | 1718 | } |
michael@0 | 1719 | |
michael@0 | 1720 | boost_score += (decay_accumulator * r); |
michael@0 | 1721 | |
michael@0 | 1722 | /* Break out conditions. */ |
michael@0 | 1723 | if ( (!flash_detected) && |
michael@0 | 1724 | ((mv_ratio_accumulator > 100.0) || |
michael@0 | 1725 | (abs_mv_in_out_accumulator > 3.0) || |
michael@0 | 1726 | (mv_in_out_accumulator < -2.0) ) ) |
michael@0 | 1727 | { |
michael@0 | 1728 | break; |
michael@0 | 1729 | } |
michael@0 | 1730 | } |
michael@0 | 1731 | *b_boost = (int)(boost_score * 100.0) >> 4; |
michael@0 | 1732 | |
michael@0 | 1733 | return (*f_boost + *b_boost); |
michael@0 | 1734 | } |
michael@0 | 1735 | #endif |
michael@0 | 1736 | |
michael@0 | 1737 | /* Analyse and define a gf/arf group . */ |
michael@0 | 1738 | static void define_gf_group(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame) |
michael@0 | 1739 | { |
michael@0 | 1740 | FIRSTPASS_STATS next_frame; |
michael@0 | 1741 | FIRSTPASS_STATS *start_pos; |
michael@0 | 1742 | int i; |
michael@0 | 1743 | double r; |
michael@0 | 1744 | double boost_score = 0.0; |
michael@0 | 1745 | double old_boost_score = 0.0; |
michael@0 | 1746 | double gf_group_err = 0.0; |
michael@0 | 1747 | double gf_first_frame_err = 0.0; |
michael@0 | 1748 | double mod_frame_err = 0.0; |
michael@0 | 1749 | |
michael@0 | 1750 | double mv_ratio_accumulator = 0.0; |
michael@0 | 1751 | double decay_accumulator = 1.0; |
michael@0 | 1752 | |
michael@0 | 1753 | double loop_decay_rate = 1.00; /* Starting decay rate */ |
michael@0 | 1754 | |
michael@0 | 1755 | double this_frame_mv_in_out = 0.0; |
michael@0 | 1756 | double mv_in_out_accumulator = 0.0; |
michael@0 | 1757 | double abs_mv_in_out_accumulator = 0.0; |
michael@0 | 1758 | double mod_err_per_mb_accumulator = 0.0; |
michael@0 | 1759 | |
michael@0 | 1760 | int max_bits = frame_max_bits(cpi); /* Max for a single frame */ |
michael@0 | 1761 | |
michael@0 | 1762 | unsigned int allow_alt_ref = |
michael@0 | 1763 | cpi->oxcf.play_alternate && cpi->oxcf.lag_in_frames; |
michael@0 | 1764 | |
michael@0 | 1765 | int alt_boost = 0; |
michael@0 | 1766 | int f_boost = 0; |
michael@0 | 1767 | int b_boost = 0; |
michael@0 | 1768 | int flash_detected; |
michael@0 | 1769 | |
michael@0 | 1770 | cpi->twopass.gf_group_bits = 0; |
michael@0 | 1771 | cpi->twopass.gf_decay_rate = 0; |
michael@0 | 1772 | |
michael@0 | 1773 | vp8_clear_system_state(); |
michael@0 | 1774 | |
michael@0 | 1775 | start_pos = cpi->twopass.stats_in; |
michael@0 | 1776 | |
michael@0 | 1777 | vpx_memset(&next_frame, 0, sizeof(next_frame)); /* assure clean */ |
michael@0 | 1778 | |
michael@0 | 1779 | /* Load stats for the current frame. */ |
michael@0 | 1780 | mod_frame_err = calculate_modified_err(cpi, this_frame); |
michael@0 | 1781 | |
michael@0 | 1782 | /* Note the error of the frame at the start of the group (this will be |
michael@0 | 1783 | * the GF frame error if we code a normal gf |
michael@0 | 1784 | */ |
michael@0 | 1785 | gf_first_frame_err = mod_frame_err; |
michael@0 | 1786 | |
michael@0 | 1787 | /* Special treatment if the current frame is a key frame (which is also |
michael@0 | 1788 | * a gf). If it is then its error score (and hence bit allocation) need |
michael@0 | 1789 | * to be subtracted out from the calculation for the GF group |
michael@0 | 1790 | */ |
michael@0 | 1791 | if (cpi->common.frame_type == KEY_FRAME) |
michael@0 | 1792 | gf_group_err -= gf_first_frame_err; |
michael@0 | 1793 | |
michael@0 | 1794 | /* Scan forward to try and work out how many frames the next gf group |
michael@0 | 1795 | * should contain and what level of boost is appropriate for the GF |
michael@0 | 1796 | * or ARF that will be coded with the group |
michael@0 | 1797 | */ |
michael@0 | 1798 | i = 0; |
michael@0 | 1799 | |
michael@0 | 1800 | while (((i < cpi->twopass.static_scene_max_gf_interval) || |
michael@0 | 1801 | ((cpi->twopass.frames_to_key - i) < MIN_GF_INTERVAL)) && |
michael@0 | 1802 | (i < cpi->twopass.frames_to_key)) |
michael@0 | 1803 | { |
michael@0 | 1804 | i++; |
michael@0 | 1805 | |
michael@0 | 1806 | /* Accumulate error score of frames in this gf group */ |
michael@0 | 1807 | mod_frame_err = calculate_modified_err(cpi, this_frame); |
michael@0 | 1808 | |
michael@0 | 1809 | gf_group_err += mod_frame_err; |
michael@0 | 1810 | |
michael@0 | 1811 | mod_err_per_mb_accumulator += |
michael@0 | 1812 | mod_frame_err / DOUBLE_DIVIDE_CHECK((double)cpi->common.MBs); |
michael@0 | 1813 | |
michael@0 | 1814 | if (EOF == input_stats(cpi, &next_frame)) |
michael@0 | 1815 | break; |
michael@0 | 1816 | |
michael@0 | 1817 | /* Test for the case where there is a brief flash but the prediction |
michael@0 | 1818 | * quality back to an earlier frame is then restored. |
michael@0 | 1819 | */ |
michael@0 | 1820 | flash_detected = detect_flash(cpi, 0); |
michael@0 | 1821 | |
michael@0 | 1822 | /* Update the motion related elements to the boost calculation */ |
michael@0 | 1823 | accumulate_frame_motion_stats( cpi, &next_frame, |
michael@0 | 1824 | &this_frame_mv_in_out, &mv_in_out_accumulator, |
michael@0 | 1825 | &abs_mv_in_out_accumulator, &mv_ratio_accumulator ); |
michael@0 | 1826 | |
michael@0 | 1827 | /* Calculate a baseline boost number for this frame */ |
michael@0 | 1828 | r = calc_frame_boost( cpi, &next_frame, this_frame_mv_in_out ); |
michael@0 | 1829 | |
michael@0 | 1830 | /* Cumulative effect of prediction quality decay */ |
michael@0 | 1831 | if ( !flash_detected ) |
michael@0 | 1832 | { |
michael@0 | 1833 | loop_decay_rate = get_prediction_decay_rate(cpi, &next_frame); |
michael@0 | 1834 | decay_accumulator = decay_accumulator * loop_decay_rate; |
michael@0 | 1835 | decay_accumulator = |
michael@0 | 1836 | decay_accumulator < 0.1 ? 0.1 : decay_accumulator; |
michael@0 | 1837 | } |
michael@0 | 1838 | boost_score += (decay_accumulator * r); |
michael@0 | 1839 | |
michael@0 | 1840 | /* Break clause to detect very still sections after motion |
michael@0 | 1841 | * For example a staic image after a fade or other transition. |
michael@0 | 1842 | */ |
michael@0 | 1843 | if ( detect_transition_to_still( cpi, i, 5, |
michael@0 | 1844 | loop_decay_rate, |
michael@0 | 1845 | decay_accumulator ) ) |
michael@0 | 1846 | { |
michael@0 | 1847 | allow_alt_ref = 0; |
michael@0 | 1848 | boost_score = old_boost_score; |
michael@0 | 1849 | break; |
michael@0 | 1850 | } |
michael@0 | 1851 | |
michael@0 | 1852 | /* Break out conditions. */ |
michael@0 | 1853 | if ( |
michael@0 | 1854 | /* Break at cpi->max_gf_interval unless almost totally static */ |
michael@0 | 1855 | (i >= cpi->max_gf_interval && (decay_accumulator < 0.995)) || |
michael@0 | 1856 | ( |
michael@0 | 1857 | /* Dont break out with a very short interval */ |
michael@0 | 1858 | (i > MIN_GF_INTERVAL) && |
michael@0 | 1859 | /* Dont break out very close to a key frame */ |
michael@0 | 1860 | ((cpi->twopass.frames_to_key - i) >= MIN_GF_INTERVAL) && |
michael@0 | 1861 | ((boost_score > 20.0) || (next_frame.pcnt_inter < 0.75)) && |
michael@0 | 1862 | (!flash_detected) && |
michael@0 | 1863 | ((mv_ratio_accumulator > 100.0) || |
michael@0 | 1864 | (abs_mv_in_out_accumulator > 3.0) || |
michael@0 | 1865 | (mv_in_out_accumulator < -2.0) || |
michael@0 | 1866 | ((boost_score - old_boost_score) < 2.0)) |
michael@0 | 1867 | ) ) |
michael@0 | 1868 | { |
michael@0 | 1869 | boost_score = old_boost_score; |
michael@0 | 1870 | break; |
michael@0 | 1871 | } |
michael@0 | 1872 | |
michael@0 | 1873 | vpx_memcpy(this_frame, &next_frame, sizeof(*this_frame)); |
michael@0 | 1874 | |
michael@0 | 1875 | old_boost_score = boost_score; |
michael@0 | 1876 | } |
michael@0 | 1877 | |
michael@0 | 1878 | cpi->twopass.gf_decay_rate = |
michael@0 | 1879 | (i > 0) ? (int)(100.0 * (1.0 - decay_accumulator)) / i : 0; |
michael@0 | 1880 | |
michael@0 | 1881 | /* When using CBR apply additional buffer related upper limits */ |
michael@0 | 1882 | if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) |
michael@0 | 1883 | { |
michael@0 | 1884 | double max_boost; |
michael@0 | 1885 | |
michael@0 | 1886 | /* For cbr apply buffer related limits */ |
michael@0 | 1887 | if (cpi->drop_frames_allowed) |
michael@0 | 1888 | { |
michael@0 | 1889 | int64_t df_buffer_level = cpi->oxcf.drop_frames_water_mark * |
michael@0 | 1890 | (cpi->oxcf.optimal_buffer_level / 100); |
michael@0 | 1891 | |
michael@0 | 1892 | if (cpi->buffer_level > df_buffer_level) |
michael@0 | 1893 | max_boost = ((double)((cpi->buffer_level - df_buffer_level) * 2 / 3) * 16.0) / DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth); |
michael@0 | 1894 | else |
michael@0 | 1895 | max_boost = 0.0; |
michael@0 | 1896 | } |
michael@0 | 1897 | else if (cpi->buffer_level > 0) |
michael@0 | 1898 | { |
michael@0 | 1899 | max_boost = ((double)(cpi->buffer_level * 2 / 3) * 16.0) / DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth); |
michael@0 | 1900 | } |
michael@0 | 1901 | else |
michael@0 | 1902 | { |
michael@0 | 1903 | max_boost = 0.0; |
michael@0 | 1904 | } |
michael@0 | 1905 | |
michael@0 | 1906 | if (boost_score > max_boost) |
michael@0 | 1907 | boost_score = max_boost; |
michael@0 | 1908 | } |
michael@0 | 1909 | |
michael@0 | 1910 | /* Dont allow conventional gf too near the next kf */ |
michael@0 | 1911 | if ((cpi->twopass.frames_to_key - i) < MIN_GF_INTERVAL) |
michael@0 | 1912 | { |
michael@0 | 1913 | while (i < cpi->twopass.frames_to_key) |
michael@0 | 1914 | { |
michael@0 | 1915 | i++; |
michael@0 | 1916 | |
michael@0 | 1917 | if (EOF == input_stats(cpi, this_frame)) |
michael@0 | 1918 | break; |
michael@0 | 1919 | |
michael@0 | 1920 | if (i < cpi->twopass.frames_to_key) |
michael@0 | 1921 | { |
michael@0 | 1922 | mod_frame_err = calculate_modified_err(cpi, this_frame); |
michael@0 | 1923 | gf_group_err += mod_frame_err; |
michael@0 | 1924 | } |
michael@0 | 1925 | } |
michael@0 | 1926 | } |
michael@0 | 1927 | |
michael@0 | 1928 | cpi->gfu_boost = (int)(boost_score * 100.0) >> 4; |
michael@0 | 1929 | |
michael@0 | 1930 | #if NEW_BOOST |
michael@0 | 1931 | /* Alterrnative boost calculation for alt ref */ |
michael@0 | 1932 | alt_boost = calc_arf_boost( cpi, 0, (i-1), (i-1), &f_boost, &b_boost ); |
michael@0 | 1933 | #endif |
michael@0 | 1934 | |
michael@0 | 1935 | /* Should we use the alternate refernce frame */ |
michael@0 | 1936 | if (allow_alt_ref && |
michael@0 | 1937 | (i >= MIN_GF_INTERVAL) && |
michael@0 | 1938 | /* dont use ARF very near next kf */ |
michael@0 | 1939 | (i <= (cpi->twopass.frames_to_key - MIN_GF_INTERVAL)) && |
michael@0 | 1940 | #if NEW_BOOST |
michael@0 | 1941 | ((next_frame.pcnt_inter > 0.75) || |
michael@0 | 1942 | (next_frame.pcnt_second_ref > 0.5)) && |
michael@0 | 1943 | ((mv_in_out_accumulator / (double)i > -0.2) || |
michael@0 | 1944 | (mv_in_out_accumulator > -2.0)) && |
michael@0 | 1945 | (b_boost > 100) && |
michael@0 | 1946 | (f_boost > 100) ) |
michael@0 | 1947 | #else |
michael@0 | 1948 | (next_frame.pcnt_inter > 0.75) && |
michael@0 | 1949 | ((mv_in_out_accumulator / (double)i > -0.2) || |
michael@0 | 1950 | (mv_in_out_accumulator > -2.0)) && |
michael@0 | 1951 | (cpi->gfu_boost > 100) && |
michael@0 | 1952 | (cpi->twopass.gf_decay_rate <= |
michael@0 | 1953 | (ARF_DECAY_THRESH + (cpi->gfu_boost / 200))) ) |
michael@0 | 1954 | #endif |
michael@0 | 1955 | { |
michael@0 | 1956 | int Boost; |
michael@0 | 1957 | int allocation_chunks; |
michael@0 | 1958 | int Q = (cpi->oxcf.fixed_q < 0) |
michael@0 | 1959 | ? cpi->last_q[INTER_FRAME] : cpi->oxcf.fixed_q; |
michael@0 | 1960 | int tmp_q; |
michael@0 | 1961 | int arf_frame_bits = 0; |
michael@0 | 1962 | int group_bits; |
michael@0 | 1963 | |
michael@0 | 1964 | #if NEW_BOOST |
michael@0 | 1965 | cpi->gfu_boost = alt_boost; |
michael@0 | 1966 | #endif |
michael@0 | 1967 | |
michael@0 | 1968 | /* Estimate the bits to be allocated to the group as a whole */ |
michael@0 | 1969 | if ((cpi->twopass.kf_group_bits > 0) && |
michael@0 | 1970 | (cpi->twopass.kf_group_error_left > 0)) |
michael@0 | 1971 | { |
michael@0 | 1972 | group_bits = (int)((double)cpi->twopass.kf_group_bits * |
michael@0 | 1973 | (gf_group_err / (double)cpi->twopass.kf_group_error_left)); |
michael@0 | 1974 | } |
michael@0 | 1975 | else |
michael@0 | 1976 | group_bits = 0; |
michael@0 | 1977 | |
michael@0 | 1978 | /* Boost for arf frame */ |
michael@0 | 1979 | #if NEW_BOOST |
michael@0 | 1980 | Boost = (alt_boost * GFQ_ADJUSTMENT) / 100; |
michael@0 | 1981 | #else |
michael@0 | 1982 | Boost = (cpi->gfu_boost * 3 * GFQ_ADJUSTMENT) / (2 * 100); |
michael@0 | 1983 | #endif |
michael@0 | 1984 | Boost += (i * 50); |
michael@0 | 1985 | |
michael@0 | 1986 | /* Set max and minimum boost and hence minimum allocation */ |
michael@0 | 1987 | if (Boost > ((cpi->baseline_gf_interval + 1) * 200)) |
michael@0 | 1988 | Boost = ((cpi->baseline_gf_interval + 1) * 200); |
michael@0 | 1989 | else if (Boost < 125) |
michael@0 | 1990 | Boost = 125; |
michael@0 | 1991 | |
michael@0 | 1992 | allocation_chunks = (i * 100) + Boost; |
michael@0 | 1993 | |
michael@0 | 1994 | /* Normalize Altboost and allocations chunck down to prevent overflow */ |
michael@0 | 1995 | while (Boost > 1000) |
michael@0 | 1996 | { |
michael@0 | 1997 | Boost /= 2; |
michael@0 | 1998 | allocation_chunks /= 2; |
michael@0 | 1999 | } |
michael@0 | 2000 | |
michael@0 | 2001 | /* Calculate the number of bits to be spent on the arf based on the |
michael@0 | 2002 | * boost number |
michael@0 | 2003 | */ |
michael@0 | 2004 | arf_frame_bits = (int)((double)Boost * (group_bits / |
michael@0 | 2005 | (double)allocation_chunks)); |
michael@0 | 2006 | |
michael@0 | 2007 | /* Estimate if there are enough bits available to make worthwhile use |
michael@0 | 2008 | * of an arf. |
michael@0 | 2009 | */ |
michael@0 | 2010 | tmp_q = estimate_q(cpi, mod_frame_err, (int)arf_frame_bits); |
michael@0 | 2011 | |
michael@0 | 2012 | /* Only use an arf if it is likely we will be able to code |
michael@0 | 2013 | * it at a lower Q than the surrounding frames. |
michael@0 | 2014 | */ |
michael@0 | 2015 | if (tmp_q < cpi->worst_quality) |
michael@0 | 2016 | { |
michael@0 | 2017 | int half_gf_int; |
michael@0 | 2018 | int frames_after_arf; |
michael@0 | 2019 | int frames_bwd = cpi->oxcf.arnr_max_frames - 1; |
michael@0 | 2020 | int frames_fwd = cpi->oxcf.arnr_max_frames - 1; |
michael@0 | 2021 | |
michael@0 | 2022 | cpi->source_alt_ref_pending = 1; |
michael@0 | 2023 | |
michael@0 | 2024 | /* |
michael@0 | 2025 | * For alt ref frames the error score for the end frame of the |
michael@0 | 2026 | * group (the alt ref frame) should not contribute to the group |
michael@0 | 2027 | * total and hence the number of bit allocated to the group. |
michael@0 | 2028 | * Rather it forms part of the next group (it is the GF at the |
michael@0 | 2029 | * start of the next group) |
michael@0 | 2030 | * gf_group_err -= mod_frame_err; |
michael@0 | 2031 | * |
michael@0 | 2032 | * For alt ref frames alt ref frame is technically part of the |
michael@0 | 2033 | * GF frame for the next group but we always base the error |
michael@0 | 2034 | * calculation and bit allocation on the current group of frames. |
michael@0 | 2035 | * |
michael@0 | 2036 | * Set the interval till the next gf or arf. |
michael@0 | 2037 | * For ARFs this is the number of frames to be coded before the |
michael@0 | 2038 | * future frame that is coded as an ARF. |
michael@0 | 2039 | * The future frame itself is part of the next group |
michael@0 | 2040 | */ |
michael@0 | 2041 | cpi->baseline_gf_interval = i; |
michael@0 | 2042 | |
michael@0 | 2043 | /* |
michael@0 | 2044 | * Define the arnr filter width for this group of frames: |
michael@0 | 2045 | * We only filter frames that lie within a distance of half |
michael@0 | 2046 | * the GF interval from the ARF frame. We also have to trap |
michael@0 | 2047 | * cases where the filter extends beyond the end of clip. |
michael@0 | 2048 | * Note: this_frame->frame has been updated in the loop |
michael@0 | 2049 | * so it now points at the ARF frame. |
michael@0 | 2050 | */ |
michael@0 | 2051 | half_gf_int = cpi->baseline_gf_interval >> 1; |
michael@0 | 2052 | frames_after_arf = (int)(cpi->twopass.total_stats.count - |
michael@0 | 2053 | this_frame->frame - 1); |
michael@0 | 2054 | |
michael@0 | 2055 | switch (cpi->oxcf.arnr_type) |
michael@0 | 2056 | { |
michael@0 | 2057 | case 1: /* Backward filter */ |
michael@0 | 2058 | frames_fwd = 0; |
michael@0 | 2059 | if (frames_bwd > half_gf_int) |
michael@0 | 2060 | frames_bwd = half_gf_int; |
michael@0 | 2061 | break; |
michael@0 | 2062 | |
michael@0 | 2063 | case 2: /* Forward filter */ |
michael@0 | 2064 | if (frames_fwd > half_gf_int) |
michael@0 | 2065 | frames_fwd = half_gf_int; |
michael@0 | 2066 | if (frames_fwd > frames_after_arf) |
michael@0 | 2067 | frames_fwd = frames_after_arf; |
michael@0 | 2068 | frames_bwd = 0; |
michael@0 | 2069 | break; |
michael@0 | 2070 | |
michael@0 | 2071 | case 3: /* Centered filter */ |
michael@0 | 2072 | default: |
michael@0 | 2073 | frames_fwd >>= 1; |
michael@0 | 2074 | if (frames_fwd > frames_after_arf) |
michael@0 | 2075 | frames_fwd = frames_after_arf; |
michael@0 | 2076 | if (frames_fwd > half_gf_int) |
michael@0 | 2077 | frames_fwd = half_gf_int; |
michael@0 | 2078 | |
michael@0 | 2079 | frames_bwd = frames_fwd; |
michael@0 | 2080 | |
michael@0 | 2081 | /* For even length filter there is one more frame backward |
michael@0 | 2082 | * than forward: e.g. len=6 ==> bbbAff, len=7 ==> bbbAfff. |
michael@0 | 2083 | */ |
michael@0 | 2084 | if (frames_bwd < half_gf_int) |
michael@0 | 2085 | frames_bwd += (cpi->oxcf.arnr_max_frames+1) & 0x1; |
michael@0 | 2086 | break; |
michael@0 | 2087 | } |
michael@0 | 2088 | |
michael@0 | 2089 | cpi->active_arnr_frames = frames_bwd + 1 + frames_fwd; |
michael@0 | 2090 | } |
michael@0 | 2091 | else |
michael@0 | 2092 | { |
michael@0 | 2093 | cpi->source_alt_ref_pending = 0; |
michael@0 | 2094 | cpi->baseline_gf_interval = i; |
michael@0 | 2095 | } |
michael@0 | 2096 | } |
michael@0 | 2097 | else |
michael@0 | 2098 | { |
michael@0 | 2099 | cpi->source_alt_ref_pending = 0; |
michael@0 | 2100 | cpi->baseline_gf_interval = i; |
michael@0 | 2101 | } |
michael@0 | 2102 | |
michael@0 | 2103 | /* |
michael@0 | 2104 | * Now decide how many bits should be allocated to the GF group as a |
michael@0 | 2105 | * proportion of those remaining in the kf group. |
michael@0 | 2106 | * The final key frame group in the clip is treated as a special case |
michael@0 | 2107 | * where cpi->twopass.kf_group_bits is tied to cpi->twopass.bits_left. |
michael@0 | 2108 | * This is also important for short clips where there may only be one |
michael@0 | 2109 | * key frame. |
michael@0 | 2110 | */ |
michael@0 | 2111 | if (cpi->twopass.frames_to_key >= (int)(cpi->twopass.total_stats.count - |
michael@0 | 2112 | cpi->common.current_video_frame)) |
michael@0 | 2113 | { |
michael@0 | 2114 | cpi->twopass.kf_group_bits = |
michael@0 | 2115 | (cpi->twopass.bits_left > 0) ? cpi->twopass.bits_left : 0; |
michael@0 | 2116 | } |
michael@0 | 2117 | |
michael@0 | 2118 | /* Calculate the bits to be allocated to the group as a whole */ |
michael@0 | 2119 | if ((cpi->twopass.kf_group_bits > 0) && |
michael@0 | 2120 | (cpi->twopass.kf_group_error_left > 0)) |
michael@0 | 2121 | { |
michael@0 | 2122 | cpi->twopass.gf_group_bits = |
michael@0 | 2123 | (int64_t)(cpi->twopass.kf_group_bits * |
michael@0 | 2124 | (gf_group_err / cpi->twopass.kf_group_error_left)); |
michael@0 | 2125 | } |
michael@0 | 2126 | else |
michael@0 | 2127 | cpi->twopass.gf_group_bits = 0; |
michael@0 | 2128 | |
michael@0 | 2129 | cpi->twopass.gf_group_bits = |
michael@0 | 2130 | (cpi->twopass.gf_group_bits < 0) |
michael@0 | 2131 | ? 0 |
michael@0 | 2132 | : (cpi->twopass.gf_group_bits > cpi->twopass.kf_group_bits) |
michael@0 | 2133 | ? cpi->twopass.kf_group_bits : cpi->twopass.gf_group_bits; |
michael@0 | 2134 | |
michael@0 | 2135 | /* Clip cpi->twopass.gf_group_bits based on user supplied data rate |
michael@0 | 2136 | * variability limit (cpi->oxcf.two_pass_vbrmax_section) |
michael@0 | 2137 | */ |
michael@0 | 2138 | if (cpi->twopass.gf_group_bits > |
michael@0 | 2139 | (int64_t)max_bits * cpi->baseline_gf_interval) |
michael@0 | 2140 | cpi->twopass.gf_group_bits = |
michael@0 | 2141 | (int64_t)max_bits * cpi->baseline_gf_interval; |
michael@0 | 2142 | |
michael@0 | 2143 | /* Reset the file position */ |
michael@0 | 2144 | reset_fpf_position(cpi, start_pos); |
michael@0 | 2145 | |
michael@0 | 2146 | /* Update the record of error used so far (only done once per gf group) */ |
michael@0 | 2147 | cpi->twopass.modified_error_used += gf_group_err; |
michael@0 | 2148 | |
michael@0 | 2149 | /* Assign bits to the arf or gf. */ |
michael@0 | 2150 | for (i = 0; i <= (cpi->source_alt_ref_pending && cpi->common.frame_type != KEY_FRAME); i++) { |
michael@0 | 2151 | int Boost; |
michael@0 | 2152 | int allocation_chunks; |
michael@0 | 2153 | int Q = (cpi->oxcf.fixed_q < 0) ? cpi->last_q[INTER_FRAME] : cpi->oxcf.fixed_q; |
michael@0 | 2154 | int gf_bits; |
michael@0 | 2155 | |
michael@0 | 2156 | /* For ARF frames */ |
michael@0 | 2157 | if (cpi->source_alt_ref_pending && i == 0) |
michael@0 | 2158 | { |
michael@0 | 2159 | #if NEW_BOOST |
michael@0 | 2160 | Boost = (alt_boost * GFQ_ADJUSTMENT) / 100; |
michael@0 | 2161 | #else |
michael@0 | 2162 | Boost = (cpi->gfu_boost * 3 * GFQ_ADJUSTMENT) / (2 * 100); |
michael@0 | 2163 | #endif |
michael@0 | 2164 | Boost += (cpi->baseline_gf_interval * 50); |
michael@0 | 2165 | |
michael@0 | 2166 | /* Set max and minimum boost and hence minimum allocation */ |
michael@0 | 2167 | if (Boost > ((cpi->baseline_gf_interval + 1) * 200)) |
michael@0 | 2168 | Boost = ((cpi->baseline_gf_interval + 1) * 200); |
michael@0 | 2169 | else if (Boost < 125) |
michael@0 | 2170 | Boost = 125; |
michael@0 | 2171 | |
michael@0 | 2172 | allocation_chunks = |
michael@0 | 2173 | ((cpi->baseline_gf_interval + 1) * 100) + Boost; |
michael@0 | 2174 | } |
michael@0 | 2175 | /* Else for standard golden frames */ |
michael@0 | 2176 | else |
michael@0 | 2177 | { |
michael@0 | 2178 | /* boost based on inter / intra ratio of subsequent frames */ |
michael@0 | 2179 | Boost = (cpi->gfu_boost * GFQ_ADJUSTMENT) / 100; |
michael@0 | 2180 | |
michael@0 | 2181 | /* Set max and minimum boost and hence minimum allocation */ |
michael@0 | 2182 | if (Boost > (cpi->baseline_gf_interval * 150)) |
michael@0 | 2183 | Boost = (cpi->baseline_gf_interval * 150); |
michael@0 | 2184 | else if (Boost < 125) |
michael@0 | 2185 | Boost = 125; |
michael@0 | 2186 | |
michael@0 | 2187 | allocation_chunks = |
michael@0 | 2188 | (cpi->baseline_gf_interval * 100) + (Boost - 100); |
michael@0 | 2189 | } |
michael@0 | 2190 | |
michael@0 | 2191 | /* Normalize Altboost and allocations chunck down to prevent overflow */ |
michael@0 | 2192 | while (Boost > 1000) |
michael@0 | 2193 | { |
michael@0 | 2194 | Boost /= 2; |
michael@0 | 2195 | allocation_chunks /= 2; |
michael@0 | 2196 | } |
michael@0 | 2197 | |
michael@0 | 2198 | /* Calculate the number of bits to be spent on the gf or arf based on |
michael@0 | 2199 | * the boost number |
michael@0 | 2200 | */ |
michael@0 | 2201 | gf_bits = (int)((double)Boost * |
michael@0 | 2202 | (cpi->twopass.gf_group_bits / |
michael@0 | 2203 | (double)allocation_chunks)); |
michael@0 | 2204 | |
michael@0 | 2205 | /* If the frame that is to be boosted is simpler than the average for |
michael@0 | 2206 | * the gf/arf group then use an alternative calculation |
michael@0 | 2207 | * based on the error score of the frame itself |
michael@0 | 2208 | */ |
michael@0 | 2209 | if (mod_frame_err < gf_group_err / (double)cpi->baseline_gf_interval) |
michael@0 | 2210 | { |
michael@0 | 2211 | double alt_gf_grp_bits; |
michael@0 | 2212 | int alt_gf_bits; |
michael@0 | 2213 | |
michael@0 | 2214 | alt_gf_grp_bits = |
michael@0 | 2215 | (double)cpi->twopass.kf_group_bits * |
michael@0 | 2216 | (mod_frame_err * (double)cpi->baseline_gf_interval) / |
michael@0 | 2217 | DOUBLE_DIVIDE_CHECK((double)cpi->twopass.kf_group_error_left); |
michael@0 | 2218 | |
michael@0 | 2219 | alt_gf_bits = (int)((double)Boost * (alt_gf_grp_bits / |
michael@0 | 2220 | (double)allocation_chunks)); |
michael@0 | 2221 | |
michael@0 | 2222 | if (gf_bits > alt_gf_bits) |
michael@0 | 2223 | { |
michael@0 | 2224 | gf_bits = alt_gf_bits; |
michael@0 | 2225 | } |
michael@0 | 2226 | } |
michael@0 | 2227 | /* Else if it is harder than other frames in the group make sure it at |
michael@0 | 2228 | * least receives an allocation in keeping with its relative error |
michael@0 | 2229 | * score, otherwise it may be worse off than an "un-boosted" frame |
michael@0 | 2230 | */ |
michael@0 | 2231 | else |
michael@0 | 2232 | { |
michael@0 | 2233 | int alt_gf_bits = |
michael@0 | 2234 | (int)((double)cpi->twopass.kf_group_bits * |
michael@0 | 2235 | mod_frame_err / |
michael@0 | 2236 | DOUBLE_DIVIDE_CHECK((double)cpi->twopass.kf_group_error_left)); |
michael@0 | 2237 | |
michael@0 | 2238 | if (alt_gf_bits > gf_bits) |
michael@0 | 2239 | { |
michael@0 | 2240 | gf_bits = alt_gf_bits; |
michael@0 | 2241 | } |
michael@0 | 2242 | } |
michael@0 | 2243 | |
michael@0 | 2244 | /* Apply an additional limit for CBR */ |
michael@0 | 2245 | if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) |
michael@0 | 2246 | { |
michael@0 | 2247 | if (cpi->twopass.gf_bits > (int)(cpi->buffer_level >> 1)) |
michael@0 | 2248 | cpi->twopass.gf_bits = (int)(cpi->buffer_level >> 1); |
michael@0 | 2249 | } |
michael@0 | 2250 | |
michael@0 | 2251 | /* Dont allow a negative value for gf_bits */ |
michael@0 | 2252 | if (gf_bits < 0) |
michael@0 | 2253 | gf_bits = 0; |
michael@0 | 2254 | |
michael@0 | 2255 | /* Add in minimum for a frame */ |
michael@0 | 2256 | gf_bits += cpi->min_frame_bandwidth; |
michael@0 | 2257 | |
michael@0 | 2258 | if (i == 0) |
michael@0 | 2259 | { |
michael@0 | 2260 | cpi->twopass.gf_bits = gf_bits; |
michael@0 | 2261 | } |
michael@0 | 2262 | if (i == 1 || (!cpi->source_alt_ref_pending && (cpi->common.frame_type != KEY_FRAME))) |
michael@0 | 2263 | { |
michael@0 | 2264 | /* Per frame bit target for this frame */ |
michael@0 | 2265 | cpi->per_frame_bandwidth = gf_bits; |
michael@0 | 2266 | } |
michael@0 | 2267 | } |
michael@0 | 2268 | |
michael@0 | 2269 | { |
michael@0 | 2270 | /* Adjust KF group bits and error remainin */ |
michael@0 | 2271 | cpi->twopass.kf_group_error_left -= (int64_t)gf_group_err; |
michael@0 | 2272 | cpi->twopass.kf_group_bits -= cpi->twopass.gf_group_bits; |
michael@0 | 2273 | |
michael@0 | 2274 | if (cpi->twopass.kf_group_bits < 0) |
michael@0 | 2275 | cpi->twopass.kf_group_bits = 0; |
michael@0 | 2276 | |
michael@0 | 2277 | /* Note the error score left in the remaining frames of the group. |
michael@0 | 2278 | * For normal GFs we want to remove the error score for the first |
michael@0 | 2279 | * frame of the group (except in Key frame case where this has |
michael@0 | 2280 | * already happened) |
michael@0 | 2281 | */ |
michael@0 | 2282 | if (!cpi->source_alt_ref_pending && cpi->common.frame_type != KEY_FRAME) |
michael@0 | 2283 | cpi->twopass.gf_group_error_left = (int)(gf_group_err - |
michael@0 | 2284 | gf_first_frame_err); |
michael@0 | 2285 | else |
michael@0 | 2286 | cpi->twopass.gf_group_error_left = (int) gf_group_err; |
michael@0 | 2287 | |
michael@0 | 2288 | cpi->twopass.gf_group_bits -= cpi->twopass.gf_bits - cpi->min_frame_bandwidth; |
michael@0 | 2289 | |
michael@0 | 2290 | if (cpi->twopass.gf_group_bits < 0) |
michael@0 | 2291 | cpi->twopass.gf_group_bits = 0; |
michael@0 | 2292 | |
michael@0 | 2293 | /* This condition could fail if there are two kfs very close together |
michael@0 | 2294 | * despite (MIN_GF_INTERVAL) and would cause a devide by 0 in the |
michael@0 | 2295 | * calculation of cpi->twopass.alt_extra_bits. |
michael@0 | 2296 | */ |
michael@0 | 2297 | if ( cpi->baseline_gf_interval >= 3 ) |
michael@0 | 2298 | { |
michael@0 | 2299 | #if NEW_BOOST |
michael@0 | 2300 | int boost = (cpi->source_alt_ref_pending) |
michael@0 | 2301 | ? b_boost : cpi->gfu_boost; |
michael@0 | 2302 | #else |
michael@0 | 2303 | int boost = cpi->gfu_boost; |
michael@0 | 2304 | #endif |
michael@0 | 2305 | if ( boost >= 150 ) |
michael@0 | 2306 | { |
michael@0 | 2307 | int pct_extra; |
michael@0 | 2308 | |
michael@0 | 2309 | pct_extra = (boost - 100) / 50; |
michael@0 | 2310 | pct_extra = (pct_extra > 20) ? 20 : pct_extra; |
michael@0 | 2311 | |
michael@0 | 2312 | cpi->twopass.alt_extra_bits = |
michael@0 | 2313 | (cpi->twopass.gf_group_bits * pct_extra) / 100; |
michael@0 | 2314 | cpi->twopass.gf_group_bits -= cpi->twopass.alt_extra_bits; |
michael@0 | 2315 | cpi->twopass.alt_extra_bits /= |
michael@0 | 2316 | ((cpi->baseline_gf_interval-1)>>1); |
michael@0 | 2317 | } |
michael@0 | 2318 | else |
michael@0 | 2319 | cpi->twopass.alt_extra_bits = 0; |
michael@0 | 2320 | } |
michael@0 | 2321 | else |
michael@0 | 2322 | cpi->twopass.alt_extra_bits = 0; |
michael@0 | 2323 | } |
michael@0 | 2324 | |
michael@0 | 2325 | /* Adjustments based on a measure of complexity of the section */ |
michael@0 | 2326 | if (cpi->common.frame_type != KEY_FRAME) |
michael@0 | 2327 | { |
michael@0 | 2328 | FIRSTPASS_STATS sectionstats; |
michael@0 | 2329 | double Ratio; |
michael@0 | 2330 | |
michael@0 | 2331 | zero_stats(§ionstats); |
michael@0 | 2332 | reset_fpf_position(cpi, start_pos); |
michael@0 | 2333 | |
michael@0 | 2334 | for (i = 0 ; i < cpi->baseline_gf_interval ; i++) |
michael@0 | 2335 | { |
michael@0 | 2336 | input_stats(cpi, &next_frame); |
michael@0 | 2337 | accumulate_stats(§ionstats, &next_frame); |
michael@0 | 2338 | } |
michael@0 | 2339 | |
michael@0 | 2340 | avg_stats(§ionstats); |
michael@0 | 2341 | |
michael@0 | 2342 | cpi->twopass.section_intra_rating = (unsigned int) |
michael@0 | 2343 | (sectionstats.intra_error / |
michael@0 | 2344 | DOUBLE_DIVIDE_CHECK(sectionstats.coded_error)); |
michael@0 | 2345 | |
michael@0 | 2346 | Ratio = sectionstats.intra_error / DOUBLE_DIVIDE_CHECK(sectionstats.coded_error); |
michael@0 | 2347 | cpi->twopass.section_max_qfactor = 1.0 - ((Ratio - 10.0) * 0.025); |
michael@0 | 2348 | |
michael@0 | 2349 | if (cpi->twopass.section_max_qfactor < 0.80) |
michael@0 | 2350 | cpi->twopass.section_max_qfactor = 0.80; |
michael@0 | 2351 | |
michael@0 | 2352 | reset_fpf_position(cpi, start_pos); |
michael@0 | 2353 | } |
michael@0 | 2354 | } |
michael@0 | 2355 | |
michael@0 | 2356 | /* Allocate bits to a normal frame that is neither a gf an arf or a key frame. */ |
michael@0 | 2357 | static void assign_std_frame_bits(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame) |
michael@0 | 2358 | { |
michael@0 | 2359 | int target_frame_size; |
michael@0 | 2360 | |
michael@0 | 2361 | double modified_err; |
michael@0 | 2362 | double err_fraction; |
michael@0 | 2363 | |
michael@0 | 2364 | int max_bits = frame_max_bits(cpi); /* Max for a single frame */ |
michael@0 | 2365 | |
michael@0 | 2366 | /* Calculate modified prediction error used in bit allocation */ |
michael@0 | 2367 | modified_err = calculate_modified_err(cpi, this_frame); |
michael@0 | 2368 | |
michael@0 | 2369 | /* What portion of the remaining GF group error is used by this frame */ |
michael@0 | 2370 | if (cpi->twopass.gf_group_error_left > 0) |
michael@0 | 2371 | err_fraction = modified_err / cpi->twopass.gf_group_error_left; |
michael@0 | 2372 | else |
michael@0 | 2373 | err_fraction = 0.0; |
michael@0 | 2374 | |
michael@0 | 2375 | /* How many of those bits available for allocation should we give it? */ |
michael@0 | 2376 | target_frame_size = (int)((double)cpi->twopass.gf_group_bits * err_fraction); |
michael@0 | 2377 | |
michael@0 | 2378 | /* Clip to target size to 0 - max_bits (or cpi->twopass.gf_group_bits) |
michael@0 | 2379 | * at the top end. |
michael@0 | 2380 | */ |
michael@0 | 2381 | if (target_frame_size < 0) |
michael@0 | 2382 | target_frame_size = 0; |
michael@0 | 2383 | else |
michael@0 | 2384 | { |
michael@0 | 2385 | if (target_frame_size > max_bits) |
michael@0 | 2386 | target_frame_size = max_bits; |
michael@0 | 2387 | |
michael@0 | 2388 | if (target_frame_size > cpi->twopass.gf_group_bits) |
michael@0 | 2389 | target_frame_size = cpi->twopass.gf_group_bits; |
michael@0 | 2390 | } |
michael@0 | 2391 | |
michael@0 | 2392 | /* Adjust error and bits remaining */ |
michael@0 | 2393 | cpi->twopass.gf_group_error_left -= (int)modified_err; |
michael@0 | 2394 | cpi->twopass.gf_group_bits -= target_frame_size; |
michael@0 | 2395 | |
michael@0 | 2396 | if (cpi->twopass.gf_group_bits < 0) |
michael@0 | 2397 | cpi->twopass.gf_group_bits = 0; |
michael@0 | 2398 | |
michael@0 | 2399 | /* Add in the minimum number of bits that is set aside for every frame. */ |
michael@0 | 2400 | target_frame_size += cpi->min_frame_bandwidth; |
michael@0 | 2401 | |
michael@0 | 2402 | /* Every other frame gets a few extra bits */ |
michael@0 | 2403 | if ( (cpi->frames_since_golden & 0x01) && |
michael@0 | 2404 | (cpi->frames_till_gf_update_due > 0) ) |
michael@0 | 2405 | { |
michael@0 | 2406 | target_frame_size += cpi->twopass.alt_extra_bits; |
michael@0 | 2407 | } |
michael@0 | 2408 | |
michael@0 | 2409 | /* Per frame bit target for this frame */ |
michael@0 | 2410 | cpi->per_frame_bandwidth = target_frame_size; |
michael@0 | 2411 | } |
michael@0 | 2412 | |
michael@0 | 2413 | void vp8_second_pass(VP8_COMP *cpi) |
michael@0 | 2414 | { |
michael@0 | 2415 | int tmp_q; |
michael@0 | 2416 | int frames_left = (int)(cpi->twopass.total_stats.count - cpi->common.current_video_frame); |
michael@0 | 2417 | |
michael@0 | 2418 | FIRSTPASS_STATS this_frame = {0}; |
michael@0 | 2419 | FIRSTPASS_STATS this_frame_copy; |
michael@0 | 2420 | |
michael@0 | 2421 | double this_frame_intra_error; |
michael@0 | 2422 | double this_frame_coded_error; |
michael@0 | 2423 | |
michael@0 | 2424 | int overhead_bits; |
michael@0 | 2425 | |
michael@0 | 2426 | if (!cpi->twopass.stats_in) |
michael@0 | 2427 | { |
michael@0 | 2428 | return ; |
michael@0 | 2429 | } |
michael@0 | 2430 | |
michael@0 | 2431 | vp8_clear_system_state(); |
michael@0 | 2432 | |
michael@0 | 2433 | if (EOF == input_stats(cpi, &this_frame)) |
michael@0 | 2434 | return; |
michael@0 | 2435 | |
michael@0 | 2436 | this_frame_intra_error = this_frame.intra_error; |
michael@0 | 2437 | this_frame_coded_error = this_frame.coded_error; |
michael@0 | 2438 | |
michael@0 | 2439 | /* keyframe and section processing ! */ |
michael@0 | 2440 | if (cpi->twopass.frames_to_key == 0) |
michael@0 | 2441 | { |
michael@0 | 2442 | /* Define next KF group and assign bits to it */ |
michael@0 | 2443 | vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame)); |
michael@0 | 2444 | find_next_key_frame(cpi, &this_frame_copy); |
michael@0 | 2445 | |
michael@0 | 2446 | /* Special case: Error error_resilient_mode mode does not make much |
michael@0 | 2447 | * sense for two pass but with its current meaning but this code is |
michael@0 | 2448 | * designed to stop outlandish behaviour if someone does set it when |
michael@0 | 2449 | * using two pass. It effectively disables GF groups. This is |
michael@0 | 2450 | * temporary code till we decide what should really happen in this |
michael@0 | 2451 | * case. |
michael@0 | 2452 | */ |
michael@0 | 2453 | if (cpi->oxcf.error_resilient_mode) |
michael@0 | 2454 | { |
michael@0 | 2455 | cpi->twopass.gf_group_bits = cpi->twopass.kf_group_bits; |
michael@0 | 2456 | cpi->twopass.gf_group_error_left = |
michael@0 | 2457 | (int)cpi->twopass.kf_group_error_left; |
michael@0 | 2458 | cpi->baseline_gf_interval = cpi->twopass.frames_to_key; |
michael@0 | 2459 | cpi->frames_till_gf_update_due = cpi->baseline_gf_interval; |
michael@0 | 2460 | cpi->source_alt_ref_pending = 0; |
michael@0 | 2461 | } |
michael@0 | 2462 | |
michael@0 | 2463 | } |
michael@0 | 2464 | |
michael@0 | 2465 | /* Is this a GF / ARF (Note that a KF is always also a GF) */ |
michael@0 | 2466 | if (cpi->frames_till_gf_update_due == 0) |
michael@0 | 2467 | { |
michael@0 | 2468 | /* Define next gf group and assign bits to it */ |
michael@0 | 2469 | vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame)); |
michael@0 | 2470 | define_gf_group(cpi, &this_frame_copy); |
michael@0 | 2471 | |
michael@0 | 2472 | /* If we are going to code an altref frame at the end of the group |
michael@0 | 2473 | * and the current frame is not a key frame.... If the previous |
michael@0 | 2474 | * group used an arf this frame has already benefited from that arf |
michael@0 | 2475 | * boost and it should not be given extra bits If the previous |
michael@0 | 2476 | * group was NOT coded using arf we may want to apply some boost to |
michael@0 | 2477 | * this GF as well |
michael@0 | 2478 | */ |
michael@0 | 2479 | if (cpi->source_alt_ref_pending && (cpi->common.frame_type != KEY_FRAME)) |
michael@0 | 2480 | { |
michael@0 | 2481 | /* Assign a standard frames worth of bits from those allocated |
michael@0 | 2482 | * to the GF group |
michael@0 | 2483 | */ |
michael@0 | 2484 | int bak = cpi->per_frame_bandwidth; |
michael@0 | 2485 | vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame)); |
michael@0 | 2486 | assign_std_frame_bits(cpi, &this_frame_copy); |
michael@0 | 2487 | cpi->per_frame_bandwidth = bak; |
michael@0 | 2488 | } |
michael@0 | 2489 | } |
michael@0 | 2490 | |
michael@0 | 2491 | /* Otherwise this is an ordinary frame */ |
michael@0 | 2492 | else |
michael@0 | 2493 | { |
michael@0 | 2494 | /* Special case: Error error_resilient_mode mode does not make much |
michael@0 | 2495 | * sense for two pass but with its current meaning but this code is |
michael@0 | 2496 | * designed to stop outlandish behaviour if someone does set it |
michael@0 | 2497 | * when using two pass. It effectively disables GF groups. This is |
michael@0 | 2498 | * temporary code till we decide what should really happen in this |
michael@0 | 2499 | * case. |
michael@0 | 2500 | */ |
michael@0 | 2501 | if (cpi->oxcf.error_resilient_mode) |
michael@0 | 2502 | { |
michael@0 | 2503 | cpi->frames_till_gf_update_due = cpi->twopass.frames_to_key; |
michael@0 | 2504 | |
michael@0 | 2505 | if (cpi->common.frame_type != KEY_FRAME) |
michael@0 | 2506 | { |
michael@0 | 2507 | /* Assign bits from those allocated to the GF group */ |
michael@0 | 2508 | vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame)); |
michael@0 | 2509 | assign_std_frame_bits(cpi, &this_frame_copy); |
michael@0 | 2510 | } |
michael@0 | 2511 | } |
michael@0 | 2512 | else |
michael@0 | 2513 | { |
michael@0 | 2514 | /* Assign bits from those allocated to the GF group */ |
michael@0 | 2515 | vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame)); |
michael@0 | 2516 | assign_std_frame_bits(cpi, &this_frame_copy); |
michael@0 | 2517 | } |
michael@0 | 2518 | } |
michael@0 | 2519 | |
michael@0 | 2520 | /* Keep a globally available copy of this and the next frame's iiratio. */ |
michael@0 | 2521 | cpi->twopass.this_iiratio = (unsigned int)(this_frame_intra_error / |
michael@0 | 2522 | DOUBLE_DIVIDE_CHECK(this_frame_coded_error)); |
michael@0 | 2523 | { |
michael@0 | 2524 | FIRSTPASS_STATS next_frame; |
michael@0 | 2525 | if ( lookup_next_frame_stats(cpi, &next_frame) != EOF ) |
michael@0 | 2526 | { |
michael@0 | 2527 | cpi->twopass.next_iiratio = (unsigned int)(next_frame.intra_error / |
michael@0 | 2528 | DOUBLE_DIVIDE_CHECK(next_frame.coded_error)); |
michael@0 | 2529 | } |
michael@0 | 2530 | } |
michael@0 | 2531 | |
michael@0 | 2532 | /* Set nominal per second bandwidth for this frame */ |
michael@0 | 2533 | cpi->target_bandwidth = (int) |
michael@0 | 2534 | (cpi->per_frame_bandwidth * cpi->output_framerate); |
michael@0 | 2535 | if (cpi->target_bandwidth < 0) |
michael@0 | 2536 | cpi->target_bandwidth = 0; |
michael@0 | 2537 | |
michael@0 | 2538 | |
michael@0 | 2539 | /* Account for mv, mode and other overheads. */ |
michael@0 | 2540 | overhead_bits = (int)estimate_modemvcost( |
michael@0 | 2541 | cpi, &cpi->twopass.total_left_stats ); |
michael@0 | 2542 | |
michael@0 | 2543 | /* Special case code for first frame. */ |
michael@0 | 2544 | if (cpi->common.current_video_frame == 0) |
michael@0 | 2545 | { |
michael@0 | 2546 | cpi->twopass.est_max_qcorrection_factor = 1.0; |
michael@0 | 2547 | |
michael@0 | 2548 | /* Set a cq_level in constrained quality mode. */ |
michael@0 | 2549 | if ( cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY ) |
michael@0 | 2550 | { |
michael@0 | 2551 | int est_cq; |
michael@0 | 2552 | |
michael@0 | 2553 | est_cq = |
michael@0 | 2554 | estimate_cq( cpi, |
michael@0 | 2555 | &cpi->twopass.total_left_stats, |
michael@0 | 2556 | (int)(cpi->twopass.bits_left / frames_left), |
michael@0 | 2557 | overhead_bits ); |
michael@0 | 2558 | |
michael@0 | 2559 | cpi->cq_target_quality = cpi->oxcf.cq_level; |
michael@0 | 2560 | if ( est_cq > cpi->cq_target_quality ) |
michael@0 | 2561 | cpi->cq_target_quality = est_cq; |
michael@0 | 2562 | } |
michael@0 | 2563 | |
michael@0 | 2564 | /* guess at maxq needed in 2nd pass */ |
michael@0 | 2565 | cpi->twopass.maxq_max_limit = cpi->worst_quality; |
michael@0 | 2566 | cpi->twopass.maxq_min_limit = cpi->best_quality; |
michael@0 | 2567 | |
michael@0 | 2568 | tmp_q = estimate_max_q( |
michael@0 | 2569 | cpi, |
michael@0 | 2570 | &cpi->twopass.total_left_stats, |
michael@0 | 2571 | (int)(cpi->twopass.bits_left / frames_left), |
michael@0 | 2572 | overhead_bits ); |
michael@0 | 2573 | |
michael@0 | 2574 | /* Limit the maxq value returned subsequently. |
michael@0 | 2575 | * This increases the risk of overspend or underspend if the initial |
michael@0 | 2576 | * estimate for the clip is bad, but helps prevent excessive |
michael@0 | 2577 | * variation in Q, especially near the end of a clip |
michael@0 | 2578 | * where for example a small overspend may cause Q to crash |
michael@0 | 2579 | */ |
michael@0 | 2580 | cpi->twopass.maxq_max_limit = ((tmp_q + 32) < cpi->worst_quality) |
michael@0 | 2581 | ? (tmp_q + 32) : cpi->worst_quality; |
michael@0 | 2582 | cpi->twopass.maxq_min_limit = ((tmp_q - 32) > cpi->best_quality) |
michael@0 | 2583 | ? (tmp_q - 32) : cpi->best_quality; |
michael@0 | 2584 | |
michael@0 | 2585 | cpi->active_worst_quality = tmp_q; |
michael@0 | 2586 | cpi->ni_av_qi = tmp_q; |
michael@0 | 2587 | } |
michael@0 | 2588 | |
michael@0 | 2589 | /* The last few frames of a clip almost always have to few or too many |
michael@0 | 2590 | * bits and for the sake of over exact rate control we dont want to make |
michael@0 | 2591 | * radical adjustments to the allowed quantizer range just to use up a |
michael@0 | 2592 | * few surplus bits or get beneath the target rate. |
michael@0 | 2593 | */ |
michael@0 | 2594 | else if ( (cpi->common.current_video_frame < |
michael@0 | 2595 | (((unsigned int)cpi->twopass.total_stats.count * 255)>>8)) && |
michael@0 | 2596 | ((cpi->common.current_video_frame + cpi->baseline_gf_interval) < |
michael@0 | 2597 | (unsigned int)cpi->twopass.total_stats.count) ) |
michael@0 | 2598 | { |
michael@0 | 2599 | if (frames_left < 1) |
michael@0 | 2600 | frames_left = 1; |
michael@0 | 2601 | |
michael@0 | 2602 | tmp_q = estimate_max_q( |
michael@0 | 2603 | cpi, |
michael@0 | 2604 | &cpi->twopass.total_left_stats, |
michael@0 | 2605 | (int)(cpi->twopass.bits_left / frames_left), |
michael@0 | 2606 | overhead_bits ); |
michael@0 | 2607 | |
michael@0 | 2608 | /* Move active_worst_quality but in a damped way */ |
michael@0 | 2609 | if (tmp_q > cpi->active_worst_quality) |
michael@0 | 2610 | cpi->active_worst_quality ++; |
michael@0 | 2611 | else if (tmp_q < cpi->active_worst_quality) |
michael@0 | 2612 | cpi->active_worst_quality --; |
michael@0 | 2613 | |
michael@0 | 2614 | cpi->active_worst_quality = |
michael@0 | 2615 | ((cpi->active_worst_quality * 3) + tmp_q + 2) / 4; |
michael@0 | 2616 | } |
michael@0 | 2617 | |
michael@0 | 2618 | cpi->twopass.frames_to_key --; |
michael@0 | 2619 | |
michael@0 | 2620 | /* Update the total stats remaining sturcture */ |
michael@0 | 2621 | subtract_stats(&cpi->twopass.total_left_stats, &this_frame ); |
michael@0 | 2622 | } |
michael@0 | 2623 | |
michael@0 | 2624 | |
michael@0 | 2625 | static int test_candidate_kf(VP8_COMP *cpi, FIRSTPASS_STATS *last_frame, FIRSTPASS_STATS *this_frame, FIRSTPASS_STATS *next_frame) |
michael@0 | 2626 | { |
michael@0 | 2627 | int is_viable_kf = 0; |
michael@0 | 2628 | |
michael@0 | 2629 | /* Does the frame satisfy the primary criteria of a key frame |
michael@0 | 2630 | * If so, then examine how well it predicts subsequent frames |
michael@0 | 2631 | */ |
michael@0 | 2632 | if ((this_frame->pcnt_second_ref < 0.10) && |
michael@0 | 2633 | (next_frame->pcnt_second_ref < 0.10) && |
michael@0 | 2634 | ((this_frame->pcnt_inter < 0.05) || |
michael@0 | 2635 | ( |
michael@0 | 2636 | ((this_frame->pcnt_inter - this_frame->pcnt_neutral) < .25) && |
michael@0 | 2637 | ((this_frame->intra_error / DOUBLE_DIVIDE_CHECK(this_frame->coded_error)) < 2.5) && |
michael@0 | 2638 | ((fabs(last_frame->coded_error - this_frame->coded_error) / DOUBLE_DIVIDE_CHECK(this_frame->coded_error) > .40) || |
michael@0 | 2639 | (fabs(last_frame->intra_error - this_frame->intra_error) / DOUBLE_DIVIDE_CHECK(this_frame->intra_error) > .40) || |
michael@0 | 2640 | ((next_frame->intra_error / DOUBLE_DIVIDE_CHECK(next_frame->coded_error)) > 3.5) |
michael@0 | 2641 | ) |
michael@0 | 2642 | ) |
michael@0 | 2643 | ) |
michael@0 | 2644 | ) |
michael@0 | 2645 | { |
michael@0 | 2646 | int i; |
michael@0 | 2647 | FIRSTPASS_STATS *start_pos; |
michael@0 | 2648 | |
michael@0 | 2649 | FIRSTPASS_STATS local_next_frame; |
michael@0 | 2650 | |
michael@0 | 2651 | double boost_score = 0.0; |
michael@0 | 2652 | double old_boost_score = 0.0; |
michael@0 | 2653 | double decay_accumulator = 1.0; |
michael@0 | 2654 | double next_iiratio; |
michael@0 | 2655 | |
michael@0 | 2656 | vpx_memcpy(&local_next_frame, next_frame, sizeof(*next_frame)); |
michael@0 | 2657 | |
michael@0 | 2658 | /* Note the starting file position so we can reset to it */ |
michael@0 | 2659 | start_pos = cpi->twopass.stats_in; |
michael@0 | 2660 | |
michael@0 | 2661 | /* Examine how well the key frame predicts subsequent frames */ |
michael@0 | 2662 | for (i = 0 ; i < 16; i++) |
michael@0 | 2663 | { |
michael@0 | 2664 | next_iiratio = (IIKFACTOR1 * local_next_frame.intra_error / DOUBLE_DIVIDE_CHECK(local_next_frame.coded_error)) ; |
michael@0 | 2665 | |
michael@0 | 2666 | if (next_iiratio > RMAX) |
michael@0 | 2667 | next_iiratio = RMAX; |
michael@0 | 2668 | |
michael@0 | 2669 | /* Cumulative effect of decay in prediction quality */ |
michael@0 | 2670 | if (local_next_frame.pcnt_inter > 0.85) |
michael@0 | 2671 | decay_accumulator = decay_accumulator * local_next_frame.pcnt_inter; |
michael@0 | 2672 | else |
michael@0 | 2673 | decay_accumulator = decay_accumulator * ((0.85 + local_next_frame.pcnt_inter) / 2.0); |
michael@0 | 2674 | |
michael@0 | 2675 | /* Keep a running total */ |
michael@0 | 2676 | boost_score += (decay_accumulator * next_iiratio); |
michael@0 | 2677 | |
michael@0 | 2678 | /* Test various breakout clauses */ |
michael@0 | 2679 | if ((local_next_frame.pcnt_inter < 0.05) || |
michael@0 | 2680 | (next_iiratio < 1.5) || |
michael@0 | 2681 | (((local_next_frame.pcnt_inter - |
michael@0 | 2682 | local_next_frame.pcnt_neutral) < 0.20) && |
michael@0 | 2683 | (next_iiratio < 3.0)) || |
michael@0 | 2684 | ((boost_score - old_boost_score) < 0.5) || |
michael@0 | 2685 | (local_next_frame.intra_error < 200) |
michael@0 | 2686 | ) |
michael@0 | 2687 | { |
michael@0 | 2688 | break; |
michael@0 | 2689 | } |
michael@0 | 2690 | |
michael@0 | 2691 | old_boost_score = boost_score; |
michael@0 | 2692 | |
michael@0 | 2693 | /* Get the next frame details */ |
michael@0 | 2694 | if (EOF == input_stats(cpi, &local_next_frame)) |
michael@0 | 2695 | break; |
michael@0 | 2696 | } |
michael@0 | 2697 | |
michael@0 | 2698 | /* If there is tolerable prediction for at least the next 3 frames |
michael@0 | 2699 | * then break out else discard this pottential key frame and move on |
michael@0 | 2700 | */ |
michael@0 | 2701 | if (boost_score > 5.0 && (i > 3)) |
michael@0 | 2702 | is_viable_kf = 1; |
michael@0 | 2703 | else |
michael@0 | 2704 | { |
michael@0 | 2705 | /* Reset the file position */ |
michael@0 | 2706 | reset_fpf_position(cpi, start_pos); |
michael@0 | 2707 | |
michael@0 | 2708 | is_viable_kf = 0; |
michael@0 | 2709 | } |
michael@0 | 2710 | } |
michael@0 | 2711 | |
michael@0 | 2712 | return is_viable_kf; |
michael@0 | 2713 | } |
michael@0 | 2714 | static void find_next_key_frame(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame) |
michael@0 | 2715 | { |
michael@0 | 2716 | int i,j; |
michael@0 | 2717 | FIRSTPASS_STATS last_frame; |
michael@0 | 2718 | FIRSTPASS_STATS first_frame; |
michael@0 | 2719 | FIRSTPASS_STATS next_frame; |
michael@0 | 2720 | FIRSTPASS_STATS *start_position; |
michael@0 | 2721 | |
michael@0 | 2722 | double decay_accumulator = 1.0; |
michael@0 | 2723 | double boost_score = 0; |
michael@0 | 2724 | double old_boost_score = 0.0; |
michael@0 | 2725 | double loop_decay_rate; |
michael@0 | 2726 | |
michael@0 | 2727 | double kf_mod_err = 0.0; |
michael@0 | 2728 | double kf_group_err = 0.0; |
michael@0 | 2729 | double kf_group_intra_err = 0.0; |
michael@0 | 2730 | double kf_group_coded_err = 0.0; |
michael@0 | 2731 | double recent_loop_decay[8] = {1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0}; |
michael@0 | 2732 | |
michael@0 | 2733 | vpx_memset(&next_frame, 0, sizeof(next_frame)); |
michael@0 | 2734 | |
michael@0 | 2735 | vp8_clear_system_state(); |
michael@0 | 2736 | start_position = cpi->twopass.stats_in; |
michael@0 | 2737 | |
michael@0 | 2738 | cpi->common.frame_type = KEY_FRAME; |
michael@0 | 2739 | |
michael@0 | 2740 | /* is this a forced key frame by interval */ |
michael@0 | 2741 | cpi->this_key_frame_forced = cpi->next_key_frame_forced; |
michael@0 | 2742 | |
michael@0 | 2743 | /* Clear the alt ref active flag as this can never be active on a key |
michael@0 | 2744 | * frame |
michael@0 | 2745 | */ |
michael@0 | 2746 | cpi->source_alt_ref_active = 0; |
michael@0 | 2747 | |
michael@0 | 2748 | /* Kf is always a gf so clear frames till next gf counter */ |
michael@0 | 2749 | cpi->frames_till_gf_update_due = 0; |
michael@0 | 2750 | |
michael@0 | 2751 | cpi->twopass.frames_to_key = 1; |
michael@0 | 2752 | |
michael@0 | 2753 | /* Take a copy of the initial frame details */ |
michael@0 | 2754 | vpx_memcpy(&first_frame, this_frame, sizeof(*this_frame)); |
michael@0 | 2755 | |
michael@0 | 2756 | cpi->twopass.kf_group_bits = 0; |
michael@0 | 2757 | cpi->twopass.kf_group_error_left = 0; |
michael@0 | 2758 | |
michael@0 | 2759 | kf_mod_err = calculate_modified_err(cpi, this_frame); |
michael@0 | 2760 | |
michael@0 | 2761 | /* find the next keyframe */ |
michael@0 | 2762 | i = 0; |
michael@0 | 2763 | while (cpi->twopass.stats_in < cpi->twopass.stats_in_end) |
michael@0 | 2764 | { |
michael@0 | 2765 | /* Accumulate kf group error */ |
michael@0 | 2766 | kf_group_err += calculate_modified_err(cpi, this_frame); |
michael@0 | 2767 | |
michael@0 | 2768 | /* These figures keep intra and coded error counts for all frames |
michael@0 | 2769 | * including key frames in the group. The effect of the key frame |
michael@0 | 2770 | * itself can be subtracted out using the first_frame data |
michael@0 | 2771 | * collected above |
michael@0 | 2772 | */ |
michael@0 | 2773 | kf_group_intra_err += this_frame->intra_error; |
michael@0 | 2774 | kf_group_coded_err += this_frame->coded_error; |
michael@0 | 2775 | |
michael@0 | 2776 | /* load a the next frame's stats */ |
michael@0 | 2777 | vpx_memcpy(&last_frame, this_frame, sizeof(*this_frame)); |
michael@0 | 2778 | input_stats(cpi, this_frame); |
michael@0 | 2779 | |
michael@0 | 2780 | /* Provided that we are not at the end of the file... */ |
michael@0 | 2781 | if (cpi->oxcf.auto_key |
michael@0 | 2782 | && lookup_next_frame_stats(cpi, &next_frame) != EOF) |
michael@0 | 2783 | { |
michael@0 | 2784 | /* Normal scene cut check */ |
michael@0 | 2785 | if ( ( i >= MIN_GF_INTERVAL ) && |
michael@0 | 2786 | test_candidate_kf(cpi, &last_frame, this_frame, &next_frame) ) |
michael@0 | 2787 | { |
michael@0 | 2788 | break; |
michael@0 | 2789 | } |
michael@0 | 2790 | |
michael@0 | 2791 | /* How fast is prediction quality decaying */ |
michael@0 | 2792 | loop_decay_rate = get_prediction_decay_rate(cpi, &next_frame); |
michael@0 | 2793 | |
michael@0 | 2794 | /* We want to know something about the recent past... rather than |
michael@0 | 2795 | * as used elsewhere where we are concened with decay in prediction |
michael@0 | 2796 | * quality since the last GF or KF. |
michael@0 | 2797 | */ |
michael@0 | 2798 | recent_loop_decay[i%8] = loop_decay_rate; |
michael@0 | 2799 | decay_accumulator = 1.0; |
michael@0 | 2800 | for (j = 0; j < 8; j++) |
michael@0 | 2801 | { |
michael@0 | 2802 | decay_accumulator = decay_accumulator * recent_loop_decay[j]; |
michael@0 | 2803 | } |
michael@0 | 2804 | |
michael@0 | 2805 | /* Special check for transition or high motion followed by a |
michael@0 | 2806 | * static scene. |
michael@0 | 2807 | */ |
michael@0 | 2808 | if ( detect_transition_to_still( cpi, i, |
michael@0 | 2809 | (cpi->key_frame_frequency-i), |
michael@0 | 2810 | loop_decay_rate, |
michael@0 | 2811 | decay_accumulator ) ) |
michael@0 | 2812 | { |
michael@0 | 2813 | break; |
michael@0 | 2814 | } |
michael@0 | 2815 | |
michael@0 | 2816 | |
michael@0 | 2817 | /* Step on to the next frame */ |
michael@0 | 2818 | cpi->twopass.frames_to_key ++; |
michael@0 | 2819 | |
michael@0 | 2820 | /* If we don't have a real key frame within the next two |
michael@0 | 2821 | * forcekeyframeevery intervals then break out of the loop. |
michael@0 | 2822 | */ |
michael@0 | 2823 | if (cpi->twopass.frames_to_key >= 2 *(int)cpi->key_frame_frequency) |
michael@0 | 2824 | break; |
michael@0 | 2825 | } else |
michael@0 | 2826 | cpi->twopass.frames_to_key ++; |
michael@0 | 2827 | |
michael@0 | 2828 | i++; |
michael@0 | 2829 | } |
michael@0 | 2830 | |
michael@0 | 2831 | /* If there is a max kf interval set by the user we must obey it. |
michael@0 | 2832 | * We already breakout of the loop above at 2x max. |
michael@0 | 2833 | * This code centers the extra kf if the actual natural |
michael@0 | 2834 | * interval is between 1x and 2x |
michael@0 | 2835 | */ |
michael@0 | 2836 | if (cpi->oxcf.auto_key |
michael@0 | 2837 | && cpi->twopass.frames_to_key > (int)cpi->key_frame_frequency ) |
michael@0 | 2838 | { |
michael@0 | 2839 | FIRSTPASS_STATS *current_pos = cpi->twopass.stats_in; |
michael@0 | 2840 | FIRSTPASS_STATS tmp_frame; |
michael@0 | 2841 | |
michael@0 | 2842 | cpi->twopass.frames_to_key /= 2; |
michael@0 | 2843 | |
michael@0 | 2844 | /* Copy first frame details */ |
michael@0 | 2845 | vpx_memcpy(&tmp_frame, &first_frame, sizeof(first_frame)); |
michael@0 | 2846 | |
michael@0 | 2847 | /* Reset to the start of the group */ |
michael@0 | 2848 | reset_fpf_position(cpi, start_position); |
michael@0 | 2849 | |
michael@0 | 2850 | kf_group_err = 0; |
michael@0 | 2851 | kf_group_intra_err = 0; |
michael@0 | 2852 | kf_group_coded_err = 0; |
michael@0 | 2853 | |
michael@0 | 2854 | /* Rescan to get the correct error data for the forced kf group */ |
michael@0 | 2855 | for( i = 0; i < cpi->twopass.frames_to_key; i++ ) |
michael@0 | 2856 | { |
michael@0 | 2857 | /* Accumulate kf group errors */ |
michael@0 | 2858 | kf_group_err += calculate_modified_err(cpi, &tmp_frame); |
michael@0 | 2859 | kf_group_intra_err += tmp_frame.intra_error; |
michael@0 | 2860 | kf_group_coded_err += tmp_frame.coded_error; |
michael@0 | 2861 | |
michael@0 | 2862 | /* Load a the next frame's stats */ |
michael@0 | 2863 | input_stats(cpi, &tmp_frame); |
michael@0 | 2864 | } |
michael@0 | 2865 | |
michael@0 | 2866 | /* Reset to the start of the group */ |
michael@0 | 2867 | reset_fpf_position(cpi, current_pos); |
michael@0 | 2868 | |
michael@0 | 2869 | cpi->next_key_frame_forced = 1; |
michael@0 | 2870 | } |
michael@0 | 2871 | else |
michael@0 | 2872 | cpi->next_key_frame_forced = 0; |
michael@0 | 2873 | |
michael@0 | 2874 | /* Special case for the last frame of the file */ |
michael@0 | 2875 | if (cpi->twopass.stats_in >= cpi->twopass.stats_in_end) |
michael@0 | 2876 | { |
michael@0 | 2877 | /* Accumulate kf group error */ |
michael@0 | 2878 | kf_group_err += calculate_modified_err(cpi, this_frame); |
michael@0 | 2879 | |
michael@0 | 2880 | /* These figures keep intra and coded error counts for all frames |
michael@0 | 2881 | * including key frames in the group. The effect of the key frame |
michael@0 | 2882 | * itself can be subtracted out using the first_frame data |
michael@0 | 2883 | * collected above |
michael@0 | 2884 | */ |
michael@0 | 2885 | kf_group_intra_err += this_frame->intra_error; |
michael@0 | 2886 | kf_group_coded_err += this_frame->coded_error; |
michael@0 | 2887 | } |
michael@0 | 2888 | |
michael@0 | 2889 | /* Calculate the number of bits that should be assigned to the kf group. */ |
michael@0 | 2890 | if ((cpi->twopass.bits_left > 0) && (cpi->twopass.modified_error_left > 0.0)) |
michael@0 | 2891 | { |
michael@0 | 2892 | /* Max for a single normal frame (not key frame) */ |
michael@0 | 2893 | int max_bits = frame_max_bits(cpi); |
michael@0 | 2894 | |
michael@0 | 2895 | /* Maximum bits for the kf group */ |
michael@0 | 2896 | int64_t max_grp_bits; |
michael@0 | 2897 | |
michael@0 | 2898 | /* Default allocation based on bits left and relative |
michael@0 | 2899 | * complexity of the section |
michael@0 | 2900 | */ |
michael@0 | 2901 | cpi->twopass.kf_group_bits = (int64_t)( cpi->twopass.bits_left * |
michael@0 | 2902 | ( kf_group_err / |
michael@0 | 2903 | cpi->twopass.modified_error_left )); |
michael@0 | 2904 | |
michael@0 | 2905 | /* Clip based on maximum per frame rate defined by the user. */ |
michael@0 | 2906 | max_grp_bits = (int64_t)max_bits * (int64_t)cpi->twopass.frames_to_key; |
michael@0 | 2907 | if (cpi->twopass.kf_group_bits > max_grp_bits) |
michael@0 | 2908 | cpi->twopass.kf_group_bits = max_grp_bits; |
michael@0 | 2909 | |
michael@0 | 2910 | /* Additional special case for CBR if buffer is getting full. */ |
michael@0 | 2911 | if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) |
michael@0 | 2912 | { |
michael@0 | 2913 | int64_t opt_buffer_lvl = cpi->oxcf.optimal_buffer_level; |
michael@0 | 2914 | int64_t buffer_lvl = cpi->buffer_level; |
michael@0 | 2915 | |
michael@0 | 2916 | /* If the buffer is near or above the optimal and this kf group is |
michael@0 | 2917 | * not being allocated much then increase the allocation a bit. |
michael@0 | 2918 | */ |
michael@0 | 2919 | if (buffer_lvl >= opt_buffer_lvl) |
michael@0 | 2920 | { |
michael@0 | 2921 | int64_t high_water_mark = (opt_buffer_lvl + |
michael@0 | 2922 | cpi->oxcf.maximum_buffer_size) >> 1; |
michael@0 | 2923 | |
michael@0 | 2924 | int64_t av_group_bits; |
michael@0 | 2925 | |
michael@0 | 2926 | /* Av bits per frame * number of frames */ |
michael@0 | 2927 | av_group_bits = (int64_t)cpi->av_per_frame_bandwidth * |
michael@0 | 2928 | (int64_t)cpi->twopass.frames_to_key; |
michael@0 | 2929 | |
michael@0 | 2930 | /* We are at or above the maximum. */ |
michael@0 | 2931 | if (cpi->buffer_level >= high_water_mark) |
michael@0 | 2932 | { |
michael@0 | 2933 | int64_t min_group_bits; |
michael@0 | 2934 | |
michael@0 | 2935 | min_group_bits = av_group_bits + |
michael@0 | 2936 | (int64_t)(buffer_lvl - |
michael@0 | 2937 | high_water_mark); |
michael@0 | 2938 | |
michael@0 | 2939 | if (cpi->twopass.kf_group_bits < min_group_bits) |
michael@0 | 2940 | cpi->twopass.kf_group_bits = min_group_bits; |
michael@0 | 2941 | } |
michael@0 | 2942 | /* We are above optimal but below the maximum */ |
michael@0 | 2943 | else if (cpi->twopass.kf_group_bits < av_group_bits) |
michael@0 | 2944 | { |
michael@0 | 2945 | int64_t bits_below_av = av_group_bits - |
michael@0 | 2946 | cpi->twopass.kf_group_bits; |
michael@0 | 2947 | |
michael@0 | 2948 | cpi->twopass.kf_group_bits += |
michael@0 | 2949 | (int64_t)((double)bits_below_av * |
michael@0 | 2950 | (double)(buffer_lvl - opt_buffer_lvl) / |
michael@0 | 2951 | (double)(high_water_mark - opt_buffer_lvl)); |
michael@0 | 2952 | } |
michael@0 | 2953 | } |
michael@0 | 2954 | } |
michael@0 | 2955 | } |
michael@0 | 2956 | else |
michael@0 | 2957 | cpi->twopass.kf_group_bits = 0; |
michael@0 | 2958 | |
michael@0 | 2959 | /* Reset the first pass file position */ |
michael@0 | 2960 | reset_fpf_position(cpi, start_position); |
michael@0 | 2961 | |
michael@0 | 2962 | /* determine how big to make this keyframe based on how well the |
michael@0 | 2963 | * subsequent frames use inter blocks |
michael@0 | 2964 | */ |
michael@0 | 2965 | decay_accumulator = 1.0; |
michael@0 | 2966 | boost_score = 0.0; |
michael@0 | 2967 | loop_decay_rate = 1.00; /* Starting decay rate */ |
michael@0 | 2968 | |
michael@0 | 2969 | for (i = 0 ; i < cpi->twopass.frames_to_key ; i++) |
michael@0 | 2970 | { |
michael@0 | 2971 | double r; |
michael@0 | 2972 | |
michael@0 | 2973 | if (EOF == input_stats(cpi, &next_frame)) |
michael@0 | 2974 | break; |
michael@0 | 2975 | |
michael@0 | 2976 | if (next_frame.intra_error > cpi->twopass.kf_intra_err_min) |
michael@0 | 2977 | r = (IIKFACTOR2 * next_frame.intra_error / |
michael@0 | 2978 | DOUBLE_DIVIDE_CHECK(next_frame.coded_error)); |
michael@0 | 2979 | else |
michael@0 | 2980 | r = (IIKFACTOR2 * cpi->twopass.kf_intra_err_min / |
michael@0 | 2981 | DOUBLE_DIVIDE_CHECK(next_frame.coded_error)); |
michael@0 | 2982 | |
michael@0 | 2983 | if (r > RMAX) |
michael@0 | 2984 | r = RMAX; |
michael@0 | 2985 | |
michael@0 | 2986 | /* How fast is prediction quality decaying */ |
michael@0 | 2987 | loop_decay_rate = get_prediction_decay_rate(cpi, &next_frame); |
michael@0 | 2988 | |
michael@0 | 2989 | decay_accumulator = decay_accumulator * loop_decay_rate; |
michael@0 | 2990 | decay_accumulator = decay_accumulator < 0.1 ? 0.1 : decay_accumulator; |
michael@0 | 2991 | |
michael@0 | 2992 | boost_score += (decay_accumulator * r); |
michael@0 | 2993 | |
michael@0 | 2994 | if ((i > MIN_GF_INTERVAL) && |
michael@0 | 2995 | ((boost_score - old_boost_score) < 1.0)) |
michael@0 | 2996 | { |
michael@0 | 2997 | break; |
michael@0 | 2998 | } |
michael@0 | 2999 | |
michael@0 | 3000 | old_boost_score = boost_score; |
michael@0 | 3001 | } |
michael@0 | 3002 | |
michael@0 | 3003 | if (1) |
michael@0 | 3004 | { |
michael@0 | 3005 | FIRSTPASS_STATS sectionstats; |
michael@0 | 3006 | double Ratio; |
michael@0 | 3007 | |
michael@0 | 3008 | zero_stats(§ionstats); |
michael@0 | 3009 | reset_fpf_position(cpi, start_position); |
michael@0 | 3010 | |
michael@0 | 3011 | for (i = 0 ; i < cpi->twopass.frames_to_key ; i++) |
michael@0 | 3012 | { |
michael@0 | 3013 | input_stats(cpi, &next_frame); |
michael@0 | 3014 | accumulate_stats(§ionstats, &next_frame); |
michael@0 | 3015 | } |
michael@0 | 3016 | |
michael@0 | 3017 | avg_stats(§ionstats); |
michael@0 | 3018 | |
michael@0 | 3019 | cpi->twopass.section_intra_rating = (unsigned int) |
michael@0 | 3020 | (sectionstats.intra_error |
michael@0 | 3021 | / DOUBLE_DIVIDE_CHECK(sectionstats.coded_error)); |
michael@0 | 3022 | |
michael@0 | 3023 | Ratio = sectionstats.intra_error / DOUBLE_DIVIDE_CHECK(sectionstats.coded_error); |
michael@0 | 3024 | cpi->twopass.section_max_qfactor = 1.0 - ((Ratio - 10.0) * 0.025); |
michael@0 | 3025 | |
michael@0 | 3026 | if (cpi->twopass.section_max_qfactor < 0.80) |
michael@0 | 3027 | cpi->twopass.section_max_qfactor = 0.80; |
michael@0 | 3028 | } |
michael@0 | 3029 | |
michael@0 | 3030 | /* When using CBR apply additional buffer fullness related upper limits */ |
michael@0 | 3031 | if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) |
michael@0 | 3032 | { |
michael@0 | 3033 | double max_boost; |
michael@0 | 3034 | |
michael@0 | 3035 | if (cpi->drop_frames_allowed) |
michael@0 | 3036 | { |
michael@0 | 3037 | int df_buffer_level = (int)(cpi->oxcf.drop_frames_water_mark |
michael@0 | 3038 | * (cpi->oxcf.optimal_buffer_level / 100)); |
michael@0 | 3039 | |
michael@0 | 3040 | if (cpi->buffer_level > df_buffer_level) |
michael@0 | 3041 | max_boost = ((double)((cpi->buffer_level - df_buffer_level) * 2 / 3) * 16.0) / DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth); |
michael@0 | 3042 | else |
michael@0 | 3043 | max_boost = 0.0; |
michael@0 | 3044 | } |
michael@0 | 3045 | else if (cpi->buffer_level > 0) |
michael@0 | 3046 | { |
michael@0 | 3047 | max_boost = ((double)(cpi->buffer_level * 2 / 3) * 16.0) / DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth); |
michael@0 | 3048 | } |
michael@0 | 3049 | else |
michael@0 | 3050 | { |
michael@0 | 3051 | max_boost = 0.0; |
michael@0 | 3052 | } |
michael@0 | 3053 | |
michael@0 | 3054 | if (boost_score > max_boost) |
michael@0 | 3055 | boost_score = max_boost; |
michael@0 | 3056 | } |
michael@0 | 3057 | |
michael@0 | 3058 | /* Reset the first pass file position */ |
michael@0 | 3059 | reset_fpf_position(cpi, start_position); |
michael@0 | 3060 | |
michael@0 | 3061 | /* Work out how many bits to allocate for the key frame itself */ |
michael@0 | 3062 | if (1) |
michael@0 | 3063 | { |
michael@0 | 3064 | int kf_boost = (int)boost_score; |
michael@0 | 3065 | int allocation_chunks; |
michael@0 | 3066 | int Counter = cpi->twopass.frames_to_key; |
michael@0 | 3067 | int alt_kf_bits; |
michael@0 | 3068 | YV12_BUFFER_CONFIG *lst_yv12 = &cpi->common.yv12_fb[cpi->common.lst_fb_idx]; |
michael@0 | 3069 | /* Min boost based on kf interval */ |
michael@0 | 3070 | #if 0 |
michael@0 | 3071 | |
michael@0 | 3072 | while ((kf_boost < 48) && (Counter > 0)) |
michael@0 | 3073 | { |
michael@0 | 3074 | Counter -= 2; |
michael@0 | 3075 | kf_boost ++; |
michael@0 | 3076 | } |
michael@0 | 3077 | |
michael@0 | 3078 | #endif |
michael@0 | 3079 | |
michael@0 | 3080 | if (kf_boost < 48) |
michael@0 | 3081 | { |
michael@0 | 3082 | kf_boost += ((Counter + 1) >> 1); |
michael@0 | 3083 | |
michael@0 | 3084 | if (kf_boost > 48) kf_boost = 48; |
michael@0 | 3085 | } |
michael@0 | 3086 | |
michael@0 | 3087 | /* bigger frame sizes need larger kf boosts, smaller frames smaller |
michael@0 | 3088 | * boosts... |
michael@0 | 3089 | */ |
michael@0 | 3090 | if ((lst_yv12->y_width * lst_yv12->y_height) > (320 * 240)) |
michael@0 | 3091 | kf_boost += 2 * (lst_yv12->y_width * lst_yv12->y_height) / (320 * 240); |
michael@0 | 3092 | else if ((lst_yv12->y_width * lst_yv12->y_height) < (320 * 240)) |
michael@0 | 3093 | kf_boost -= 4 * (320 * 240) / (lst_yv12->y_width * lst_yv12->y_height); |
michael@0 | 3094 | |
michael@0 | 3095 | /* Min KF boost */ |
michael@0 | 3096 | kf_boost = (int)((double)kf_boost * 100.0) >> 4; /* Scale 16 to 100 */ |
michael@0 | 3097 | if (kf_boost < 250) |
michael@0 | 3098 | kf_boost = 250; |
michael@0 | 3099 | |
michael@0 | 3100 | /* |
michael@0 | 3101 | * We do three calculations for kf size. |
michael@0 | 3102 | * The first is based on the error score for the whole kf group. |
michael@0 | 3103 | * The second (optionaly) on the key frames own error if this is |
michael@0 | 3104 | * smaller than the average for the group. |
michael@0 | 3105 | * The final one insures that the frame receives at least the |
michael@0 | 3106 | * allocation it would have received based on its own error score vs |
michael@0 | 3107 | * the error score remaining |
michael@0 | 3108 | * Special case if the sequence appears almost totaly static |
michael@0 | 3109 | * as measured by the decay accumulator. In this case we want to |
michael@0 | 3110 | * spend almost all of the bits on the key frame. |
michael@0 | 3111 | * cpi->twopass.frames_to_key-1 because key frame itself is taken |
michael@0 | 3112 | * care of by kf_boost. |
michael@0 | 3113 | */ |
michael@0 | 3114 | if ( decay_accumulator >= 0.99 ) |
michael@0 | 3115 | { |
michael@0 | 3116 | allocation_chunks = |
michael@0 | 3117 | ((cpi->twopass.frames_to_key - 1) * 10) + kf_boost; |
michael@0 | 3118 | } |
michael@0 | 3119 | else |
michael@0 | 3120 | { |
michael@0 | 3121 | allocation_chunks = |
michael@0 | 3122 | ((cpi->twopass.frames_to_key - 1) * 100) + kf_boost; |
michael@0 | 3123 | } |
michael@0 | 3124 | |
michael@0 | 3125 | /* Normalize Altboost and allocations chunck down to prevent overflow */ |
michael@0 | 3126 | while (kf_boost > 1000) |
michael@0 | 3127 | { |
michael@0 | 3128 | kf_boost /= 2; |
michael@0 | 3129 | allocation_chunks /= 2; |
michael@0 | 3130 | } |
michael@0 | 3131 | |
michael@0 | 3132 | cpi->twopass.kf_group_bits = (cpi->twopass.kf_group_bits < 0) ? 0 : cpi->twopass.kf_group_bits; |
michael@0 | 3133 | |
michael@0 | 3134 | /* Calculate the number of bits to be spent on the key frame */ |
michael@0 | 3135 | cpi->twopass.kf_bits = (int)((double)kf_boost * ((double)cpi->twopass.kf_group_bits / (double)allocation_chunks)); |
michael@0 | 3136 | |
michael@0 | 3137 | /* Apply an additional limit for CBR */ |
michael@0 | 3138 | if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) |
michael@0 | 3139 | { |
michael@0 | 3140 | if (cpi->twopass.kf_bits > (int)((3 * cpi->buffer_level) >> 2)) |
michael@0 | 3141 | cpi->twopass.kf_bits = (int)((3 * cpi->buffer_level) >> 2); |
michael@0 | 3142 | } |
michael@0 | 3143 | |
michael@0 | 3144 | /* If the key frame is actually easier than the average for the |
michael@0 | 3145 | * kf group (which does sometimes happen... eg a blank intro frame) |
michael@0 | 3146 | * Then use an alternate calculation based on the kf error score |
michael@0 | 3147 | * which should give a smaller key frame. |
michael@0 | 3148 | */ |
michael@0 | 3149 | if (kf_mod_err < kf_group_err / cpi->twopass.frames_to_key) |
michael@0 | 3150 | { |
michael@0 | 3151 | double alt_kf_grp_bits = |
michael@0 | 3152 | ((double)cpi->twopass.bits_left * |
michael@0 | 3153 | (kf_mod_err * (double)cpi->twopass.frames_to_key) / |
michael@0 | 3154 | DOUBLE_DIVIDE_CHECK(cpi->twopass.modified_error_left)); |
michael@0 | 3155 | |
michael@0 | 3156 | alt_kf_bits = (int)((double)kf_boost * |
michael@0 | 3157 | (alt_kf_grp_bits / (double)allocation_chunks)); |
michael@0 | 3158 | |
michael@0 | 3159 | if (cpi->twopass.kf_bits > alt_kf_bits) |
michael@0 | 3160 | { |
michael@0 | 3161 | cpi->twopass.kf_bits = alt_kf_bits; |
michael@0 | 3162 | } |
michael@0 | 3163 | } |
michael@0 | 3164 | /* Else if it is much harder than other frames in the group make sure |
michael@0 | 3165 | * it at least receives an allocation in keeping with its relative |
michael@0 | 3166 | * error score |
michael@0 | 3167 | */ |
michael@0 | 3168 | else |
michael@0 | 3169 | { |
michael@0 | 3170 | alt_kf_bits = |
michael@0 | 3171 | (int)((double)cpi->twopass.bits_left * |
michael@0 | 3172 | (kf_mod_err / |
michael@0 | 3173 | DOUBLE_DIVIDE_CHECK(cpi->twopass.modified_error_left))); |
michael@0 | 3174 | |
michael@0 | 3175 | if (alt_kf_bits > cpi->twopass.kf_bits) |
michael@0 | 3176 | { |
michael@0 | 3177 | cpi->twopass.kf_bits = alt_kf_bits; |
michael@0 | 3178 | } |
michael@0 | 3179 | } |
michael@0 | 3180 | |
michael@0 | 3181 | cpi->twopass.kf_group_bits -= cpi->twopass.kf_bits; |
michael@0 | 3182 | /* Add in the minimum frame allowance */ |
michael@0 | 3183 | cpi->twopass.kf_bits += cpi->min_frame_bandwidth; |
michael@0 | 3184 | |
michael@0 | 3185 | /* Peer frame bit target for this frame */ |
michael@0 | 3186 | cpi->per_frame_bandwidth = cpi->twopass.kf_bits; |
michael@0 | 3187 | |
michael@0 | 3188 | /* Convert to a per second bitrate */ |
michael@0 | 3189 | cpi->target_bandwidth = (int)(cpi->twopass.kf_bits * |
michael@0 | 3190 | cpi->output_framerate); |
michael@0 | 3191 | } |
michael@0 | 3192 | |
michael@0 | 3193 | /* Note the total error score of the kf group minus the key frame itself */ |
michael@0 | 3194 | cpi->twopass.kf_group_error_left = (int)(kf_group_err - kf_mod_err); |
michael@0 | 3195 | |
michael@0 | 3196 | /* Adjust the count of total modified error left. The count of bits left |
michael@0 | 3197 | * is adjusted elsewhere based on real coded frame sizes |
michael@0 | 3198 | */ |
michael@0 | 3199 | cpi->twopass.modified_error_left -= kf_group_err; |
michael@0 | 3200 | |
michael@0 | 3201 | if (cpi->oxcf.allow_spatial_resampling) |
michael@0 | 3202 | { |
michael@0 | 3203 | int resample_trigger = 0; |
michael@0 | 3204 | int last_kf_resampled = 0; |
michael@0 | 3205 | int kf_q; |
michael@0 | 3206 | int scale_val = 0; |
michael@0 | 3207 | int hr, hs, vr, vs; |
michael@0 | 3208 | int new_width = cpi->oxcf.Width; |
michael@0 | 3209 | int new_height = cpi->oxcf.Height; |
michael@0 | 3210 | |
michael@0 | 3211 | int projected_buffer_level = (int)cpi->buffer_level; |
michael@0 | 3212 | int tmp_q; |
michael@0 | 3213 | |
michael@0 | 3214 | double projected_bits_perframe; |
michael@0 | 3215 | double group_iiratio = (kf_group_intra_err - first_frame.intra_error) / (kf_group_coded_err - first_frame.coded_error); |
michael@0 | 3216 | double err_per_frame = kf_group_err / cpi->twopass.frames_to_key; |
michael@0 | 3217 | double bits_per_frame; |
michael@0 | 3218 | double av_bits_per_frame; |
michael@0 | 3219 | double effective_size_ratio; |
michael@0 | 3220 | |
michael@0 | 3221 | if ((cpi->common.Width != cpi->oxcf.Width) || (cpi->common.Height != cpi->oxcf.Height)) |
michael@0 | 3222 | last_kf_resampled = 1; |
michael@0 | 3223 | |
michael@0 | 3224 | /* Set back to unscaled by defaults */ |
michael@0 | 3225 | cpi->common.horiz_scale = NORMAL; |
michael@0 | 3226 | cpi->common.vert_scale = NORMAL; |
michael@0 | 3227 | |
michael@0 | 3228 | /* Calculate Average bits per frame. */ |
michael@0 | 3229 | av_bits_per_frame = cpi->oxcf.target_bandwidth / DOUBLE_DIVIDE_CHECK((double)cpi->framerate); |
michael@0 | 3230 | |
michael@0 | 3231 | /* CBR... Use the clip average as the target for deciding resample */ |
michael@0 | 3232 | if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) |
michael@0 | 3233 | { |
michael@0 | 3234 | bits_per_frame = av_bits_per_frame; |
michael@0 | 3235 | } |
michael@0 | 3236 | |
michael@0 | 3237 | /* In VBR we want to avoid downsampling in easy section unless we |
michael@0 | 3238 | * are under extreme pressure So use the larger of target bitrate |
michael@0 | 3239 | * for this section or average bitrate for sequence |
michael@0 | 3240 | */ |
michael@0 | 3241 | else |
michael@0 | 3242 | { |
michael@0 | 3243 | /* This accounts for how hard the section is... */ |
michael@0 | 3244 | bits_per_frame = (double) |
michael@0 | 3245 | (cpi->twopass.kf_group_bits / cpi->twopass.frames_to_key); |
michael@0 | 3246 | |
michael@0 | 3247 | /* Dont turn to resampling in easy sections just because they |
michael@0 | 3248 | * have been assigned a small number of bits |
michael@0 | 3249 | */ |
michael@0 | 3250 | if (bits_per_frame < av_bits_per_frame) |
michael@0 | 3251 | bits_per_frame = av_bits_per_frame; |
michael@0 | 3252 | } |
michael@0 | 3253 | |
michael@0 | 3254 | /* bits_per_frame should comply with our minimum */ |
michael@0 | 3255 | if (bits_per_frame < (cpi->oxcf.target_bandwidth * cpi->oxcf.two_pass_vbrmin_section / 100)) |
michael@0 | 3256 | bits_per_frame = (cpi->oxcf.target_bandwidth * cpi->oxcf.two_pass_vbrmin_section / 100); |
michael@0 | 3257 | |
michael@0 | 3258 | /* Work out if spatial resampling is necessary */ |
michael@0 | 3259 | kf_q = estimate_kf_group_q(cpi, err_per_frame, |
michael@0 | 3260 | (int)bits_per_frame, group_iiratio); |
michael@0 | 3261 | |
michael@0 | 3262 | /* If we project a required Q higher than the maximum allowed Q then |
michael@0 | 3263 | * make a guess at the actual size of frames in this section |
michael@0 | 3264 | */ |
michael@0 | 3265 | projected_bits_perframe = bits_per_frame; |
michael@0 | 3266 | tmp_q = kf_q; |
michael@0 | 3267 | |
michael@0 | 3268 | while (tmp_q > cpi->worst_quality) |
michael@0 | 3269 | { |
michael@0 | 3270 | projected_bits_perframe *= 1.04; |
michael@0 | 3271 | tmp_q--; |
michael@0 | 3272 | } |
michael@0 | 3273 | |
michael@0 | 3274 | /* Guess at buffer level at the end of the section */ |
michael@0 | 3275 | projected_buffer_level = (int) |
michael@0 | 3276 | (cpi->buffer_level - (int) |
michael@0 | 3277 | ((projected_bits_perframe - av_bits_per_frame) * |
michael@0 | 3278 | cpi->twopass.frames_to_key)); |
michael@0 | 3279 | |
michael@0 | 3280 | if (0) |
michael@0 | 3281 | { |
michael@0 | 3282 | FILE *f = fopen("Subsamle.stt", "a"); |
michael@0 | 3283 | fprintf(f, " %8d %8d %8d %8d %12.0f %8d %8d %8d\n", cpi->common.current_video_frame, kf_q, cpi->common.horiz_scale, cpi->common.vert_scale, kf_group_err / cpi->twopass.frames_to_key, (int)(cpi->twopass.kf_group_bits / cpi->twopass.frames_to_key), new_height, new_width); |
michael@0 | 3284 | fclose(f); |
michael@0 | 3285 | } |
michael@0 | 3286 | |
michael@0 | 3287 | /* The trigger for spatial resampling depends on the various |
michael@0 | 3288 | * parameters such as whether we are streaming (CBR) or VBR. |
michael@0 | 3289 | */ |
michael@0 | 3290 | if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) |
michael@0 | 3291 | { |
michael@0 | 3292 | /* Trigger resample if we are projected to fall below down |
michael@0 | 3293 | * sample level or resampled last time and are projected to |
michael@0 | 3294 | * remain below the up sample level |
michael@0 | 3295 | */ |
michael@0 | 3296 | if ((projected_buffer_level < (cpi->oxcf.resample_down_water_mark * cpi->oxcf.optimal_buffer_level / 100)) || |
michael@0 | 3297 | (last_kf_resampled && (projected_buffer_level < (cpi->oxcf.resample_up_water_mark * cpi->oxcf.optimal_buffer_level / 100)))) |
michael@0 | 3298 | resample_trigger = 1; |
michael@0 | 3299 | else |
michael@0 | 3300 | resample_trigger = 0; |
michael@0 | 3301 | } |
michael@0 | 3302 | else |
michael@0 | 3303 | { |
michael@0 | 3304 | int64_t clip_bits = (int64_t)(cpi->twopass.total_stats.count * cpi->oxcf.target_bandwidth / DOUBLE_DIVIDE_CHECK((double)cpi->framerate)); |
michael@0 | 3305 | int64_t over_spend = cpi->oxcf.starting_buffer_level - cpi->buffer_level; |
michael@0 | 3306 | |
michael@0 | 3307 | /* If triggered last time the threshold for triggering again is |
michael@0 | 3308 | * reduced: |
michael@0 | 3309 | * |
michael@0 | 3310 | * Projected Q higher than allowed and Overspend > 5% of total |
michael@0 | 3311 | * bits |
michael@0 | 3312 | */ |
michael@0 | 3313 | if ((last_kf_resampled && (kf_q > cpi->worst_quality)) || |
michael@0 | 3314 | ((kf_q > cpi->worst_quality) && |
michael@0 | 3315 | (over_spend > clip_bits / 20))) |
michael@0 | 3316 | resample_trigger = 1; |
michael@0 | 3317 | else |
michael@0 | 3318 | resample_trigger = 0; |
michael@0 | 3319 | |
michael@0 | 3320 | } |
michael@0 | 3321 | |
michael@0 | 3322 | if (resample_trigger) |
michael@0 | 3323 | { |
michael@0 | 3324 | while ((kf_q >= cpi->worst_quality) && (scale_val < 6)) |
michael@0 | 3325 | { |
michael@0 | 3326 | scale_val ++; |
michael@0 | 3327 | |
michael@0 | 3328 | cpi->common.vert_scale = vscale_lookup[scale_val]; |
michael@0 | 3329 | cpi->common.horiz_scale = hscale_lookup[scale_val]; |
michael@0 | 3330 | |
michael@0 | 3331 | Scale2Ratio(cpi->common.horiz_scale, &hr, &hs); |
michael@0 | 3332 | Scale2Ratio(cpi->common.vert_scale, &vr, &vs); |
michael@0 | 3333 | |
michael@0 | 3334 | new_width = ((hs - 1) + (cpi->oxcf.Width * hr)) / hs; |
michael@0 | 3335 | new_height = ((vs - 1) + (cpi->oxcf.Height * vr)) / vs; |
michael@0 | 3336 | |
michael@0 | 3337 | /* Reducing the area to 1/4 does not reduce the complexity |
michael@0 | 3338 | * (err_per_frame) to 1/4... effective_sizeratio attempts |
michael@0 | 3339 | * to provide a crude correction for this |
michael@0 | 3340 | */ |
michael@0 | 3341 | effective_size_ratio = (double)(new_width * new_height) / (double)(cpi->oxcf.Width * cpi->oxcf.Height); |
michael@0 | 3342 | effective_size_ratio = (1.0 + (3.0 * effective_size_ratio)) / 4.0; |
michael@0 | 3343 | |
michael@0 | 3344 | /* Now try again and see what Q we get with the smaller |
michael@0 | 3345 | * image size |
michael@0 | 3346 | */ |
michael@0 | 3347 | kf_q = estimate_kf_group_q(cpi, |
michael@0 | 3348 | err_per_frame * effective_size_ratio, |
michael@0 | 3349 | (int)bits_per_frame, group_iiratio); |
michael@0 | 3350 | |
michael@0 | 3351 | if (0) |
michael@0 | 3352 | { |
michael@0 | 3353 | FILE *f = fopen("Subsamle.stt", "a"); |
michael@0 | 3354 | fprintf(f, "******** %8d %8d %8d %12.0f %8d %8d %8d\n", kf_q, cpi->common.horiz_scale, cpi->common.vert_scale, kf_group_err / cpi->twopass.frames_to_key, (int)(cpi->twopass.kf_group_bits / cpi->twopass.frames_to_key), new_height, new_width); |
michael@0 | 3355 | fclose(f); |
michael@0 | 3356 | } |
michael@0 | 3357 | } |
michael@0 | 3358 | } |
michael@0 | 3359 | |
michael@0 | 3360 | if ((cpi->common.Width != new_width) || (cpi->common.Height != new_height)) |
michael@0 | 3361 | { |
michael@0 | 3362 | cpi->common.Width = new_width; |
michael@0 | 3363 | cpi->common.Height = new_height; |
michael@0 | 3364 | vp8_alloc_compressor_data(cpi); |
michael@0 | 3365 | } |
michael@0 | 3366 | } |
michael@0 | 3367 | } |