| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved. | 2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 | 39 |
| 40 #define OUTPUT_FPF 0 | 40 #define OUTPUT_FPF 0 |
| 41 | 41 |
| 42 #define IIFACTOR 12.5 | 42 #define IIFACTOR 12.5 |
| 43 #define IIKFACTOR1 12.5 | 43 #define IIKFACTOR1 12.5 |
| 44 #define IIKFACTOR2 15.0 | 44 #define IIKFACTOR2 15.0 |
| 45 #define RMAX 512.0 | 45 #define RMAX 512.0 |
| 46 #define GF_RMAX 96.0 | 46 #define GF_RMAX 96.0 |
| 47 #define ERR_DIVISOR 150.0 | 47 #define ERR_DIVISOR 150.0 |
| 48 #define MIN_DECAY_FACTOR 0.1 | 48 #define MIN_DECAY_FACTOR 0.1 |
| 49 #define SVC_FACTOR_PT_LOW 0.45 |
| 50 #define FACTOR_PT_LOW 0.5 |
| 51 #define FACTOR_PT_HIGH 0.9 |
| 49 | 52 |
| 50 #define KF_MB_INTRA_MIN 150 | 53 #define KF_MB_INTRA_MIN 150 |
| 51 #define GF_MB_INTRA_MIN 100 | 54 #define GF_MB_INTRA_MIN 100 |
| 52 | 55 |
| 53 #define DOUBLE_DIVIDE_CHECK(x) ((x) < 0 ? (x) - 0.000001 : (x) + 0.000001) | 56 #define DOUBLE_DIVIDE_CHECK(x) ((x) < 0 ? (x) - 0.000001 : (x) + 0.000001) |
| 54 | 57 |
| 55 #define MIN_KF_BOOST 300 | 58 #define MIN_KF_BOOST 300 |
| 56 | 59 |
| 57 #if CONFIG_MULTIPLE_ARF | 60 #if CONFIG_MULTIPLE_ARF |
| 58 // Set MIN_GF_INTERVAL to 1 for the full decomposition. | 61 // Set MIN_GF_INTERVAL to 1 for the full decomposition. |
| 59 #define MIN_GF_INTERVAL 2 | 62 #define MIN_GF_INTERVAL 2 |
| 60 #else | 63 #else |
| 61 #define MIN_GF_INTERVAL 4 | 64 #define MIN_GF_INTERVAL 4 |
| 62 #endif | 65 #endif |
| 63 | 66 |
| 64 | 67 #define LONG_TERM_VBR_CORRECTION |
| 65 // #define LONG_TERM_VBR_CORRECTION | |
| 66 | 68 |
| 67 static void swap_yv12(YV12_BUFFER_CONFIG *a, YV12_BUFFER_CONFIG *b) { | 69 static void swap_yv12(YV12_BUFFER_CONFIG *a, YV12_BUFFER_CONFIG *b) { |
| 68 YV12_BUFFER_CONFIG temp = *a; | 70 YV12_BUFFER_CONFIG temp = *a; |
| 69 *a = *b; | 71 *a = *b; |
| 70 *b = temp; | 72 *b = temp; |
| 71 } | 73 } |
| 72 | 74 |
| 73 static int gfboost_qadjust(int qindex) { | 75 static int gfboost_qadjust(int qindex) { |
| 74 const double q = vp9_convert_qindex_to_q(qindex); | 76 const double q = vp9_convert_qindex_to_q(qindex); |
| 75 return (int)((0.00000828 * q * q * q) + | 77 return (int)((0.00000828 * q * q * q) + |
| 76 (-0.0055 * q * q) + | 78 (-0.0055 * q * q) + |
| 77 (1.32 * q) + 79.3); | 79 (1.32 * q) + 79.3); |
| 78 } | 80 } |
| 79 | 81 |
| 80 // Resets the first pass file to the given position using a relative seek from | 82 // Resets the first pass file to the given position using a relative seek from |
| 81 // the current position. | 83 // the current position. |
| 82 static void reset_fpf_position(struct twopass_rc *p, | 84 static void reset_fpf_position(TWO_PASS *p, |
| 83 const FIRSTPASS_STATS *position) { | 85 const FIRSTPASS_STATS *position) { |
| 84 p->stats_in = position; | 86 p->stats_in = position; |
| 85 } | 87 } |
| 86 | 88 |
| 87 static int lookup_next_frame_stats(const struct twopass_rc *p, | 89 static int lookup_next_frame_stats(const TWO_PASS *p, |
| 88 FIRSTPASS_STATS *next_frame) { | 90 FIRSTPASS_STATS *next_frame) { |
| 89 if (p->stats_in >= p->stats_in_end) | 91 if (p->stats_in >= p->stats_in_end) |
| 90 return EOF; | 92 return EOF; |
| 91 | 93 |
| 92 *next_frame = *p->stats_in; | 94 *next_frame = *p->stats_in; |
| 93 return 1; | 95 return 1; |
| 94 } | 96 } |
| 95 | 97 |
| 96 | 98 |
| 97 // Read frame stats at an offset from the current position. | 99 // Read frame stats at an offset from the current position. |
| 98 static int read_frame_stats(const struct twopass_rc *p, | 100 static int read_frame_stats(const TWO_PASS *p, |
| 99 FIRSTPASS_STATS *frame_stats, int offset) { | 101 FIRSTPASS_STATS *frame_stats, int offset) { |
| 100 const FIRSTPASS_STATS *fps_ptr = p->stats_in; | 102 const FIRSTPASS_STATS *fps_ptr = p->stats_in; |
| 101 | 103 |
| 102 // Check legality of offset. | 104 // Check legality of offset. |
| 103 if (offset >= 0) { | 105 if (offset >= 0) { |
| 104 if (&fps_ptr[offset] >= p->stats_in_end) | 106 if (&fps_ptr[offset] >= p->stats_in_end) |
| 105 return EOF; | 107 return EOF; |
| 106 } else if (offset < 0) { | 108 } else if (offset < 0) { |
| 107 if (&fps_ptr[offset] < p->stats_in_start) | 109 if (&fps_ptr[offset] < p->stats_in_start) |
| 108 return EOF; | 110 return EOF; |
| 109 } | 111 } |
| 110 | 112 |
| 111 *frame_stats = fps_ptr[offset]; | 113 *frame_stats = fps_ptr[offset]; |
| 112 return 1; | 114 return 1; |
| 113 } | 115 } |
| 114 | 116 |
| 115 static int input_stats(struct twopass_rc *p, FIRSTPASS_STATS *fps) { | 117 static int input_stats(TWO_PASS *p, FIRSTPASS_STATS *fps) { |
| 116 if (p->stats_in >= p->stats_in_end) | 118 if (p->stats_in >= p->stats_in_end) |
| 117 return EOF; | 119 return EOF; |
| 118 | 120 |
| 119 *fps = *p->stats_in; | 121 *fps = *p->stats_in; |
| 120 ++p->stats_in; | 122 ++p->stats_in; |
| 121 return 1; | 123 return 1; |
| 122 } | 124 } |
| 123 | 125 |
| 124 static void output_stats(FIRSTPASS_STATS *stats, | 126 static void output_stats(FIRSTPASS_STATS *stats, |
| 125 struct vpx_codec_pkt_list *pktlist) { | 127 struct vpx_codec_pkt_list *pktlist) { |
| 126 struct vpx_codec_cx_pkt pkt; | 128 struct vpx_codec_cx_pkt pkt; |
| 127 pkt.kind = VPX_CODEC_STATS_PKT; | 129 pkt.kind = VPX_CODEC_STATS_PKT; |
| 128 pkt.data.twopass_stats.buf = stats; | 130 pkt.data.twopass_stats.buf = stats; |
| 129 pkt.data.twopass_stats.sz = sizeof(FIRSTPASS_STATS); | 131 pkt.data.twopass_stats.sz = sizeof(FIRSTPASS_STATS); |
| 130 vpx_codec_pkt_list_add(pktlist, &pkt); | 132 vpx_codec_pkt_list_add(pktlist, &pkt); |
| 131 | 133 |
| 132 // TEMP debug code | 134 // TEMP debug code |
| 133 #if OUTPUT_FPF | 135 #if OUTPUT_FPF |
| 134 { | 136 { |
| 135 FILE *fpfile; | 137 FILE *fpfile; |
| 136 fpfile = fopen("firstpass.stt", "a"); | 138 fpfile = fopen("firstpass.stt", "a"); |
| 137 | 139 |
| 138 fprintf(fpfile, "%12.0f %12.0f %12.0f %12.0f %12.0f %12.4f %12.4f" | 140 fprintf(fpfile, "%12.0f %12.0f %12.0f %12.0f %12.4f %12.4f" |
| 139 "%12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f" | 141 "%12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f" |
| 140 "%12.0f %12.0f %12.4f %12.0f %12.0f %12.4f\n", | 142 "%12.0f %12.0f %12.4f %12.0f %12.0f %12.4f\n", |
| 141 stats->frame, | 143 stats->frame, |
| 142 stats->intra_error, | 144 stats->intra_error, |
| 143 stats->coded_error, | 145 stats->coded_error, |
| 144 stats->sr_coded_error, | 146 stats->sr_coded_error, |
| 145 stats->ssim_weighted_pred_err, | |
| 146 stats->pcnt_inter, | 147 stats->pcnt_inter, |
| 147 stats->pcnt_motion, | 148 stats->pcnt_motion, |
| 148 stats->pcnt_second_ref, | 149 stats->pcnt_second_ref, |
| 149 stats->pcnt_neutral, | 150 stats->pcnt_neutral, |
| 150 stats->MVr, | 151 stats->MVr, |
| 151 stats->mvr_abs, | 152 stats->mvr_abs, |
| 152 stats->MVc, | 153 stats->MVc, |
| 153 stats->mvc_abs, | 154 stats->mvc_abs, |
| 154 stats->MVrv, | 155 stats->MVrv, |
| 155 stats->MVcv, | 156 stats->MVcv, |
| 156 stats->mv_in_out_count, | 157 stats->mv_in_out_count, |
| 157 stats->new_mv_count, | 158 stats->new_mv_count, |
| 158 stats->count, | 159 stats->count, |
| 159 stats->duration); | 160 stats->duration); |
| 160 fclose(fpfile); | 161 fclose(fpfile); |
| 161 } | 162 } |
| 162 #endif | 163 #endif |
| 163 } | 164 } |
| 164 | 165 |
| 165 static void zero_stats(FIRSTPASS_STATS *section) { | 166 static void zero_stats(FIRSTPASS_STATS *section) { |
| 166 section->frame = 0.0; | 167 section->frame = 0.0; |
| 167 section->intra_error = 0.0; | 168 section->intra_error = 0.0; |
| 168 section->coded_error = 0.0; | 169 section->coded_error = 0.0; |
| 169 section->sr_coded_error = 0.0; | 170 section->sr_coded_error = 0.0; |
| 170 section->ssim_weighted_pred_err = 0.0; | |
| 171 section->pcnt_inter = 0.0; | 171 section->pcnt_inter = 0.0; |
| 172 section->pcnt_motion = 0.0; | 172 section->pcnt_motion = 0.0; |
| 173 section->pcnt_second_ref = 0.0; | 173 section->pcnt_second_ref = 0.0; |
| 174 section->pcnt_neutral = 0.0; | 174 section->pcnt_neutral = 0.0; |
| 175 section->MVr = 0.0; | 175 section->MVr = 0.0; |
| 176 section->mvr_abs = 0.0; | 176 section->mvr_abs = 0.0; |
| 177 section->MVc = 0.0; | 177 section->MVc = 0.0; |
| 178 section->mvc_abs = 0.0; | 178 section->mvc_abs = 0.0; |
| 179 section->MVrv = 0.0; | 179 section->MVrv = 0.0; |
| 180 section->MVcv = 0.0; | 180 section->MVcv = 0.0; |
| 181 section->mv_in_out_count = 0.0; | 181 section->mv_in_out_count = 0.0; |
| 182 section->new_mv_count = 0.0; | 182 section->new_mv_count = 0.0; |
| 183 section->count = 0.0; | 183 section->count = 0.0; |
| 184 section->duration = 1.0; | 184 section->duration = 1.0; |
| 185 section->spatial_layer_id = 0; | 185 section->spatial_layer_id = 0; |
| 186 } | 186 } |
| 187 | 187 |
| 188 static void accumulate_stats(FIRSTPASS_STATS *section, | 188 static void accumulate_stats(FIRSTPASS_STATS *section, |
| 189 const FIRSTPASS_STATS *frame) { | 189 const FIRSTPASS_STATS *frame) { |
| 190 section->frame += frame->frame; | 190 section->frame += frame->frame; |
| 191 section->spatial_layer_id = frame->spatial_layer_id; | 191 section->spatial_layer_id = frame->spatial_layer_id; |
| 192 section->intra_error += frame->intra_error; | 192 section->intra_error += frame->intra_error; |
| 193 section->coded_error += frame->coded_error; | 193 section->coded_error += frame->coded_error; |
| 194 section->sr_coded_error += frame->sr_coded_error; | 194 section->sr_coded_error += frame->sr_coded_error; |
| 195 section->ssim_weighted_pred_err += frame->ssim_weighted_pred_err; | |
| 196 section->pcnt_inter += frame->pcnt_inter; | 195 section->pcnt_inter += frame->pcnt_inter; |
| 197 section->pcnt_motion += frame->pcnt_motion; | 196 section->pcnt_motion += frame->pcnt_motion; |
| 198 section->pcnt_second_ref += frame->pcnt_second_ref; | 197 section->pcnt_second_ref += frame->pcnt_second_ref; |
| 199 section->pcnt_neutral += frame->pcnt_neutral; | 198 section->pcnt_neutral += frame->pcnt_neutral; |
| 200 section->MVr += frame->MVr; | 199 section->MVr += frame->MVr; |
| 201 section->mvr_abs += frame->mvr_abs; | 200 section->mvr_abs += frame->mvr_abs; |
| 202 section->MVc += frame->MVc; | 201 section->MVc += frame->MVc; |
| 203 section->mvc_abs += frame->mvc_abs; | 202 section->mvc_abs += frame->mvc_abs; |
| 204 section->MVrv += frame->MVrv; | 203 section->MVrv += frame->MVrv; |
| 205 section->MVcv += frame->MVcv; | 204 section->MVcv += frame->MVcv; |
| 206 section->mv_in_out_count += frame->mv_in_out_count; | 205 section->mv_in_out_count += frame->mv_in_out_count; |
| 207 section->new_mv_count += frame->new_mv_count; | 206 section->new_mv_count += frame->new_mv_count; |
| 208 section->count += frame->count; | 207 section->count += frame->count; |
| 209 section->duration += frame->duration; | 208 section->duration += frame->duration; |
| 210 } | 209 } |
| 211 | 210 |
| 212 static void subtract_stats(FIRSTPASS_STATS *section, | 211 static void subtract_stats(FIRSTPASS_STATS *section, |
| 213 const FIRSTPASS_STATS *frame) { | 212 const FIRSTPASS_STATS *frame) { |
| 214 section->frame -= frame->frame; | 213 section->frame -= frame->frame; |
| 215 section->intra_error -= frame->intra_error; | 214 section->intra_error -= frame->intra_error; |
| 216 section->coded_error -= frame->coded_error; | 215 section->coded_error -= frame->coded_error; |
| 217 section->sr_coded_error -= frame->sr_coded_error; | 216 section->sr_coded_error -= frame->sr_coded_error; |
| 218 section->ssim_weighted_pred_err -= frame->ssim_weighted_pred_err; | |
| 219 section->pcnt_inter -= frame->pcnt_inter; | 217 section->pcnt_inter -= frame->pcnt_inter; |
| 220 section->pcnt_motion -= frame->pcnt_motion; | 218 section->pcnt_motion -= frame->pcnt_motion; |
| 221 section->pcnt_second_ref -= frame->pcnt_second_ref; | 219 section->pcnt_second_ref -= frame->pcnt_second_ref; |
| 222 section->pcnt_neutral -= frame->pcnt_neutral; | 220 section->pcnt_neutral -= frame->pcnt_neutral; |
| 223 section->MVr -= frame->MVr; | 221 section->MVr -= frame->MVr; |
| 224 section->mvr_abs -= frame->mvr_abs; | 222 section->mvr_abs -= frame->mvr_abs; |
| 225 section->MVc -= frame->MVc; | 223 section->MVc -= frame->MVc; |
| 226 section->mvc_abs -= frame->mvc_abs; | 224 section->mvc_abs -= frame->mvc_abs; |
| 227 section->MVrv -= frame->MVrv; | 225 section->MVrv -= frame->MVrv; |
| 228 section->MVcv -= frame->MVcv; | 226 section->MVcv -= frame->MVcv; |
| 229 section->mv_in_out_count -= frame->mv_in_out_count; | 227 section->mv_in_out_count -= frame->mv_in_out_count; |
| 230 section->new_mv_count -= frame->new_mv_count; | 228 section->new_mv_count -= frame->new_mv_count; |
| 231 section->count -= frame->count; | 229 section->count -= frame->count; |
| 232 section->duration -= frame->duration; | 230 section->duration -= frame->duration; |
| 233 } | 231 } |
| 234 | 232 |
| 235 static void avg_stats(FIRSTPASS_STATS *section) { | 233 static void avg_stats(FIRSTPASS_STATS *section) { |
| 236 if (section->count < 1.0) | 234 if (section->count < 1.0) |
| 237 return; | 235 return; |
| 238 | 236 |
| 239 section->intra_error /= section->count; | 237 section->intra_error /= section->count; |
| 240 section->coded_error /= section->count; | 238 section->coded_error /= section->count; |
| 241 section->sr_coded_error /= section->count; | 239 section->sr_coded_error /= section->count; |
| 242 section->ssim_weighted_pred_err /= section->count; | |
| 243 section->pcnt_inter /= section->count; | 240 section->pcnt_inter /= section->count; |
| 244 section->pcnt_second_ref /= section->count; | 241 section->pcnt_second_ref /= section->count; |
| 245 section->pcnt_neutral /= section->count; | 242 section->pcnt_neutral /= section->count; |
| 246 section->pcnt_motion /= section->count; | 243 section->pcnt_motion /= section->count; |
| 247 section->MVr /= section->count; | 244 section->MVr /= section->count; |
| 248 section->mvr_abs /= section->count; | 245 section->mvr_abs /= section->count; |
| 249 section->MVc /= section->count; | 246 section->MVc /= section->count; |
| 250 section->mvc_abs /= section->count; | 247 section->mvc_abs /= section->count; |
| 251 section->MVrv /= section->count; | 248 section->MVrv /= section->count; |
| 252 section->MVcv /= section->count; | 249 section->MVcv /= section->count; |
| 253 section->mv_in_out_count /= section->count; | 250 section->mv_in_out_count /= section->count; |
| 254 section->duration /= section->count; | 251 section->duration /= section->count; |
| 255 } | 252 } |
| 256 | 253 |
| 257 // Calculate a modified Error used in distributing bits between easier and | 254 // Calculate a modified Error used in distributing bits between easier and |
| 258 // harder frames. | 255 // harder frames. |
| 259 static double calculate_modified_err(const VP9_COMP *cpi, | 256 static double calculate_modified_err(const TWO_PASS *twopass, |
| 257 const VP9EncoderConfig *oxcf, |
| 260 const FIRSTPASS_STATS *this_frame) { | 258 const FIRSTPASS_STATS *this_frame) { |
| 261 const struct twopass_rc *twopass = &cpi->twopass; | 259 const FIRSTPASS_STATS *const stats = &twopass->total_stats; |
| 262 const SVC *const svc = &cpi->svc; | 260 const double av_err = stats->coded_error / stats->count; |
| 263 const FIRSTPASS_STATS *stats; | 261 const double modified_error = av_err * |
| 264 double av_err; | 262 pow(this_frame->coded_error / DOUBLE_DIVIDE_CHECK(av_err), |
| 265 double modified_error; | 263 oxcf->two_pass_vbrbias / 100.0); |
| 266 | |
| 267 if (svc->number_spatial_layers > 1 && | |
| 268 svc->number_temporal_layers == 1) { | |
| 269 twopass = &svc->layer_context[svc->spatial_layer_id].twopass; | |
| 270 } | |
| 271 | |
| 272 stats = &twopass->total_stats; | |
| 273 av_err = stats->ssim_weighted_pred_err / stats->count; | |
| 274 modified_error = av_err * pow(this_frame->ssim_weighted_pred_err / | |
| 275 DOUBLE_DIVIDE_CHECK(av_err), | |
| 276 cpi->oxcf.two_pass_vbrbias / 100.0); | |
| 277 | |
| 278 return fclamp(modified_error, | 264 return fclamp(modified_error, |
| 279 twopass->modified_error_min, twopass->modified_error_max); | 265 twopass->modified_error_min, twopass->modified_error_max); |
| 280 } | 266 } |
| 281 | 267 |
| 282 static const double weight_table[256] = { | |
| 283 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, | |
| 284 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, | |
| 285 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, | |
| 286 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, | |
| 287 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.031250, 0.062500, | |
| 288 0.093750, 0.125000, 0.156250, 0.187500, 0.218750, 0.250000, 0.281250, | |
| 289 0.312500, 0.343750, 0.375000, 0.406250, 0.437500, 0.468750, 0.500000, | |
| 290 0.531250, 0.562500, 0.593750, 0.625000, 0.656250, 0.687500, 0.718750, | |
| 291 0.750000, 0.781250, 0.812500, 0.843750, 0.875000, 0.906250, 0.937500, | |
| 292 0.968750, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, | |
| 293 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, | |
| 294 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, | |
| 295 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, | |
| 296 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, | |
| 297 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, | |
| 298 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, | |
| 299 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, | |
| 300 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, | |
| 301 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, | |
| 302 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, | |
| 303 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, | |
| 304 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, | |
| 305 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, | |
| 306 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, | |
| 307 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, | |
| 308 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, | |
| 309 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, | |
| 310 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, | |
| 311 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, | |
| 312 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, | |
| 313 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, | |
| 314 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, | |
| 315 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, | |
| 316 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, | |
| 317 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, | |
| 318 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, | |
| 319 1.000000, 1.000000, 1.000000, 1.000000 | |
| 320 }; | |
| 321 | |
| 322 static double simple_weight(const YV12_BUFFER_CONFIG *buf) { | |
| 323 int i, j; | |
| 324 double sum = 0.0; | |
| 325 const int w = buf->y_crop_width; | |
| 326 const int h = buf->y_crop_height; | |
| 327 const uint8_t *row = buf->y_buffer; | |
| 328 | |
| 329 for (i = 0; i < h; ++i) { | |
| 330 const uint8_t *pixel = row; | |
| 331 for (j = 0; j < w; ++j) | |
| 332 sum += weight_table[*pixel++]; | |
| 333 row += buf->y_stride; | |
| 334 } | |
| 335 | |
| 336 return MAX(0.1, sum / (w * h)); | |
| 337 } | |
| 338 | |
| 339 // This function returns the maximum target rate per frame. | 268 // This function returns the maximum target rate per frame. |
| 340 static int frame_max_bits(const RATE_CONTROL *rc, | 269 static int frame_max_bits(const RATE_CONTROL *rc, |
| 341 const VP9EncoderConfig *oxcf) { | 270 const VP9EncoderConfig *oxcf) { |
| 342 int64_t max_bits = ((int64_t)rc->avg_frame_bandwidth * | 271 int64_t max_bits = ((int64_t)rc->avg_frame_bandwidth * |
| 343 (int64_t)oxcf->two_pass_vbrmax_section) / 100; | 272 (int64_t)oxcf->two_pass_vbrmax_section) / 100; |
| 344 if (max_bits < 0) | 273 if (max_bits < 0) |
| 345 max_bits = 0; | 274 max_bits = 0; |
| 346 else if (max_bits > rc->max_frame_bandwidth) | 275 else if (max_bits > rc->max_frame_bandwidth) |
| 347 max_bits = rc->max_frame_bandwidth; | 276 max_bits = rc->max_frame_bandwidth; |
| 348 | 277 |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 461 static BLOCK_SIZE get_bsize(const VP9_COMMON *cm, int mb_row, int mb_col) { | 390 static BLOCK_SIZE get_bsize(const VP9_COMMON *cm, int mb_row, int mb_col) { |
| 462 if (2 * mb_col + 1 < cm->mi_cols) { | 391 if (2 * mb_col + 1 < cm->mi_cols) { |
| 463 return 2 * mb_row + 1 < cm->mi_rows ? BLOCK_16X16 | 392 return 2 * mb_row + 1 < cm->mi_rows ? BLOCK_16X16 |
| 464 : BLOCK_16X8; | 393 : BLOCK_16X8; |
| 465 } else { | 394 } else { |
| 466 return 2 * mb_row + 1 < cm->mi_rows ? BLOCK_8X16 | 395 return 2 * mb_row + 1 < cm->mi_rows ? BLOCK_8X16 |
| 467 : BLOCK_8X8; | 396 : BLOCK_8X8; |
| 468 } | 397 } |
| 469 } | 398 } |
| 470 | 399 |
| 400 static int find_fp_qindex() { |
| 401 int i; |
| 402 |
| 403 for (i = 0; i < QINDEX_RANGE; ++i) |
| 404 if (vp9_convert_qindex_to_q(i) >= 30.0) |
| 405 break; |
| 406 |
| 407 if (i == QINDEX_RANGE) |
| 408 i--; |
| 409 |
| 410 return i; |
| 411 } |
| 412 |
| 413 static void set_first_pass_params(VP9_COMP *cpi) { |
| 414 VP9_COMMON *const cm = &cpi->common; |
| 415 if (!cpi->refresh_alt_ref_frame && |
| 416 (cm->current_video_frame == 0 || |
| 417 (cpi->frame_flags & FRAMEFLAGS_KEY))) { |
| 418 cm->frame_type = KEY_FRAME; |
| 419 } else { |
| 420 cm->frame_type = INTER_FRAME; |
| 421 } |
| 422 // Do not use periodic key frames. |
| 423 cpi->rc.frames_to_key = INT_MAX; |
| 424 } |
| 425 |
| 471 void vp9_first_pass(VP9_COMP *cpi) { | 426 void vp9_first_pass(VP9_COMP *cpi) { |
| 472 int mb_row, mb_col; | 427 int mb_row, mb_col; |
| 473 MACROBLOCK *const x = &cpi->mb; | 428 MACROBLOCK *const x = &cpi->mb; |
| 474 VP9_COMMON *const cm = &cpi->common; | 429 VP9_COMMON *const cm = &cpi->common; |
| 475 MACROBLOCKD *const xd = &x->e_mbd; | 430 MACROBLOCKD *const xd = &x->e_mbd; |
| 476 TileInfo tile; | 431 TileInfo tile; |
| 477 struct macroblock_plane *const p = x->plane; | 432 struct macroblock_plane *const p = x->plane; |
| 478 struct macroblockd_plane *const pd = xd->plane; | 433 struct macroblockd_plane *const pd = xd->plane; |
| 479 const PICK_MODE_CONTEXT *ctx = &x->pc_root->none; | 434 const PICK_MODE_CONTEXT *ctx = &cpi->pc_root->none; |
| 480 int i; | 435 int i; |
| 481 | 436 |
| 482 int recon_yoffset, recon_uvoffset; | 437 int recon_yoffset, recon_uvoffset; |
| 483 YV12_BUFFER_CONFIG *const lst_yv12 = get_ref_frame_buffer(cpi, LAST_FRAME); | 438 YV12_BUFFER_CONFIG *const lst_yv12 = get_ref_frame_buffer(cpi, LAST_FRAME); |
| 484 YV12_BUFFER_CONFIG *gld_yv12 = get_ref_frame_buffer(cpi, GOLDEN_FRAME); | 439 YV12_BUFFER_CONFIG *gld_yv12 = get_ref_frame_buffer(cpi, GOLDEN_FRAME); |
| 485 YV12_BUFFER_CONFIG *const new_yv12 = get_frame_new_buffer(cm); | 440 YV12_BUFFER_CONFIG *const new_yv12 = get_frame_new_buffer(cm); |
| 486 int recon_y_stride = lst_yv12->y_stride; | 441 int recon_y_stride = lst_yv12->y_stride; |
| 487 int recon_uv_stride = lst_yv12->uv_stride; | 442 int recon_uv_stride = lst_yv12->uv_stride; |
| 488 int uv_mb_height = 16 >> (lst_yv12->y_height > lst_yv12->uv_height); | 443 int uv_mb_height = 16 >> (lst_yv12->y_height > lst_yv12->uv_height); |
| 489 int64_t intra_error = 0; | 444 int64_t intra_error = 0; |
| 490 int64_t coded_error = 0; | 445 int64_t coded_error = 0; |
| 491 int64_t sr_coded_error = 0; | 446 int64_t sr_coded_error = 0; |
| 492 | 447 |
| 493 int sum_mvr = 0, sum_mvc = 0; | 448 int sum_mvr = 0, sum_mvc = 0; |
| 494 int sum_mvr_abs = 0, sum_mvc_abs = 0; | 449 int sum_mvr_abs = 0, sum_mvc_abs = 0; |
| 495 int64_t sum_mvrs = 0, sum_mvcs = 0; | 450 int64_t sum_mvrs = 0, sum_mvcs = 0; |
| 496 int mvcount = 0; | 451 int mvcount = 0; |
| 497 int intercount = 0; | 452 int intercount = 0; |
| 498 int second_ref_count = 0; | 453 int second_ref_count = 0; |
| 499 int intrapenalty = 256; | 454 int intrapenalty = 256; |
| 500 int neutral_count = 0; | 455 int neutral_count = 0; |
| 501 int new_mv_count = 0; | 456 int new_mv_count = 0; |
| 502 int sum_in_vectors = 0; | 457 int sum_in_vectors = 0; |
| 503 uint32_t lastmv_as_int = 0; | 458 uint32_t lastmv_as_int = 0; |
| 504 struct twopass_rc *twopass = &cpi->twopass; | 459 TWO_PASS *twopass = &cpi->twopass; |
| 505 const MV zero_mv = {0, 0}; | 460 const MV zero_mv = {0, 0}; |
| 506 const YV12_BUFFER_CONFIG *first_ref_buf = lst_yv12; | 461 const YV12_BUFFER_CONFIG *first_ref_buf = lst_yv12; |
| 507 | 462 |
| 508 vp9_clear_system_state(); | 463 vp9_clear_system_state(); |
| 509 | 464 |
| 465 set_first_pass_params(cpi); |
| 466 vp9_set_quantizer(cm, find_fp_qindex()); |
| 467 |
| 510 if (cpi->use_svc && cpi->svc.number_temporal_layers == 1) { | 468 if (cpi->use_svc && cpi->svc.number_temporal_layers == 1) { |
| 511 MV_REFERENCE_FRAME ref_frame = LAST_FRAME; | 469 MV_REFERENCE_FRAME ref_frame = LAST_FRAME; |
| 512 const YV12_BUFFER_CONFIG *scaled_ref_buf = NULL; | 470 const YV12_BUFFER_CONFIG *scaled_ref_buf = NULL; |
| 513 twopass = &cpi->svc.layer_context[cpi->svc.spatial_layer_id].twopass; | 471 twopass = &cpi->svc.layer_context[cpi->svc.spatial_layer_id].twopass; |
| 514 | 472 |
| 515 vp9_scale_references(cpi); | 473 vp9_scale_references(cpi); |
| 516 | 474 |
| 517 // Use either last frame or alt frame for motion search. | 475 // Use either last frame or alt frame for motion search. |
| 518 if (cpi->ref_frame_flags & VP9_LAST_FLAG) { | 476 if (cpi->ref_frame_flags & VP9_LAST_FLAG) { |
| 519 scaled_ref_buf = vp9_get_scaled_ref_frame(cpi, LAST_FRAME); | 477 scaled_ref_buf = vp9_get_scaled_ref_frame(cpi, LAST_FRAME); |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 629 // Accumulate the intra error. | 587 // Accumulate the intra error. |
| 630 intra_error += (int64_t)this_error; | 588 intra_error += (int64_t)this_error; |
| 631 | 589 |
| 632 // Set up limit values for motion vectors to prevent them extending | 590 // Set up limit values for motion vectors to prevent them extending |
| 633 // outside the UMV borders. | 591 // outside the UMV borders. |
| 634 x->mv_col_min = -((mb_col * 16) + BORDER_MV_PIXELS_B16); | 592 x->mv_col_min = -((mb_col * 16) + BORDER_MV_PIXELS_B16); |
| 635 x->mv_col_max = ((cm->mb_cols - 1 - mb_col) * 16) + BORDER_MV_PIXELS_B16; | 593 x->mv_col_max = ((cm->mb_cols - 1 - mb_col) * 16) + BORDER_MV_PIXELS_B16; |
| 636 | 594 |
| 637 // Other than for the first frame do a motion search. | 595 // Other than for the first frame do a motion search. |
| 638 if (cm->current_video_frame > 0) { | 596 if (cm->current_video_frame > 0) { |
| 639 int tmp_err, motion_error; | 597 int tmp_err, motion_error, raw_motion_error; |
| 640 int_mv mv, tmp_mv; | 598 int_mv mv, tmp_mv; |
| 599 struct buf_2d unscaled_last_source_buf_2d; |
| 641 | 600 |
| 642 xd->plane[0].pre[0].buf = first_ref_buf->y_buffer + recon_yoffset; | 601 xd->plane[0].pre[0].buf = first_ref_buf->y_buffer + recon_yoffset; |
| 643 motion_error = get_prediction_error(bsize, &x->plane[0].src, | 602 motion_error = get_prediction_error(bsize, &x->plane[0].src, |
| 644 &xd->plane[0].pre[0]); | 603 &xd->plane[0].pre[0]); |
| 645 // Assume 0,0 motion with no mv overhead. | 604 // Assume 0,0 motion with no mv overhead. |
| 646 mv.as_int = tmp_mv.as_int = 0; | 605 mv.as_int = tmp_mv.as_int = 0; |
| 647 | 606 |
| 648 // Test last reference frame using the previous best mv as the | 607 // Compute the motion error of the 0,0 motion using the last source |
| 649 // starting point (best reference) for the search. | 608 // frame as the reference. Skip the further motion search on |
| 650 first_pass_motion_search(cpi, x, &best_ref_mv.as_mv, &mv.as_mv, | 609 // reconstructed frame if this error is small. |
| 651 &motion_error); | 610 unscaled_last_source_buf_2d.buf = |
| 652 if (cpi->oxcf.aq_mode == VARIANCE_AQ) { | 611 cpi->unscaled_last_source->y_buffer + recon_yoffset; |
| 653 vp9_clear_system_state(); | 612 unscaled_last_source_buf_2d.stride = |
| 654 motion_error = (int)(motion_error * error_weight); | 613 cpi->unscaled_last_source->y_stride; |
| 655 } | 614 raw_motion_error = get_prediction_error(bsize, &x->plane[0].src, |
| 615 &unscaled_last_source_buf_2d); |
| 656 | 616 |
| 657 // If the current best reference mv is not centered on 0,0 then do a 0,0 | 617 // TODO(pengchong): Replace the hard-coded threshold |
| 658 // based search as well. | 618 if (raw_motion_error > 25) { |
| 659 if (best_ref_mv.as_int) { | 619 // Test last reference frame using the previous best mv as the |
| 660 tmp_err = INT_MAX; | 620 // starting point (best reference) for the search. |
| 661 first_pass_motion_search(cpi, x, &zero_mv, &tmp_mv.as_mv, | 621 first_pass_motion_search(cpi, x, &best_ref_mv.as_mv, &mv.as_mv, |
| 662 &tmp_err); | 622 &motion_error); |
| 663 if (cpi->oxcf.aq_mode == VARIANCE_AQ) { | 623 if (cpi->oxcf.aq_mode == VARIANCE_AQ) { |
| 664 vp9_clear_system_state(); | 624 vp9_clear_system_state(); |
| 665 tmp_err = (int)(tmp_err * error_weight); | 625 motion_error = (int)(motion_error * error_weight); |
| 666 } | 626 } |
| 667 | 627 |
| 668 if (tmp_err < motion_error) { | 628 // If the current best reference mv is not centered on 0,0 then do a |
| 669 motion_error = tmp_err; | 629 // 0,0 based search as well. |
| 670 mv.as_int = tmp_mv.as_int; | 630 if (best_ref_mv.as_int) { |
| 671 } | 631 tmp_err = INT_MAX; |
| 672 } | 632 first_pass_motion_search(cpi, x, &zero_mv, &tmp_mv.as_mv, &tmp_err); |
| 633 if (cpi->oxcf.aq_mode == VARIANCE_AQ) { |
| 634 vp9_clear_system_state(); |
| 635 tmp_err = (int)(tmp_err * error_weight); |
| 636 } |
| 673 | 637 |
| 674 // Search in an older reference frame. | 638 if (tmp_err < motion_error) { |
| 675 if (cm->current_video_frame > 1 && gld_yv12 != NULL) { | 639 motion_error = tmp_err; |
| 676 // Assume 0,0 motion with no mv overhead. | 640 mv.as_int = tmp_mv.as_int; |
| 677 int gf_motion_error; | 641 } |
| 678 | |
| 679 xd->plane[0].pre[0].buf = gld_yv12->y_buffer + recon_yoffset; | |
| 680 gf_motion_error = get_prediction_error(bsize, &x->plane[0].src, | |
| 681 &xd->plane[0].pre[0]); | |
| 682 | |
| 683 first_pass_motion_search(cpi, x, &zero_mv, &tmp_mv.as_mv, | |
| 684 &gf_motion_error); | |
| 685 if (cpi->oxcf.aq_mode == VARIANCE_AQ) { | |
| 686 vp9_clear_system_state(); | |
| 687 gf_motion_error = (int)(gf_motion_error * error_weight); | |
| 688 } | 642 } |
| 689 | 643 |
| 690 if (gf_motion_error < motion_error && gf_motion_error < this_error) | 644 // Search in an older reference frame. |
| 691 ++second_ref_count; | 645 if (cm->current_video_frame > 1 && gld_yv12 != NULL) { |
| 646 // Assume 0,0 motion with no mv overhead. |
| 647 int gf_motion_error; |
| 692 | 648 |
| 693 // Reset to last frame as reference buffer. | 649 xd->plane[0].pre[0].buf = gld_yv12->y_buffer + recon_yoffset; |
| 694 xd->plane[0].pre[0].buf = first_ref_buf->y_buffer + recon_yoffset; | 650 gf_motion_error = get_prediction_error(bsize, &x->plane[0].src, |
| 695 xd->plane[1].pre[0].buf = first_ref_buf->u_buffer + recon_uvoffset; | 651 &xd->plane[0].pre[0]); |
| 696 xd->plane[2].pre[0].buf = first_ref_buf->v_buffer + recon_uvoffset; | |
| 697 | 652 |
| 698 // In accumulating a score for the older reference frame take the | 653 first_pass_motion_search(cpi, x, &zero_mv, &tmp_mv.as_mv, |
| 699 // best of the motion predicted score and the intra coded error | 654 &gf_motion_error); |
| 700 // (just as will be done for) accumulation of "coded_error" for | 655 if (cpi->oxcf.aq_mode == VARIANCE_AQ) { |
| 701 // the last frame. | 656 vp9_clear_system_state(); |
| 702 if (gf_motion_error < this_error) | 657 gf_motion_error = (int)(gf_motion_error * error_weight); |
| 703 sr_coded_error += gf_motion_error; | 658 } |
| 704 else | 659 |
| 705 sr_coded_error += this_error; | 660 if (gf_motion_error < motion_error && gf_motion_error < this_error) |
| 661 ++second_ref_count; |
| 662 |
| 663 // Reset to last frame as reference buffer. |
| 664 xd->plane[0].pre[0].buf = first_ref_buf->y_buffer + recon_yoffset; |
| 665 xd->plane[1].pre[0].buf = first_ref_buf->u_buffer + recon_uvoffset; |
| 666 xd->plane[2].pre[0].buf = first_ref_buf->v_buffer + recon_uvoffset; |
| 667 |
| 668 // In accumulating a score for the older reference frame take the |
| 669 // best of the motion predicted score and the intra coded error |
| 670 // (just as will be done for) accumulation of "coded_error" for |
| 671 // the last frame. |
| 672 if (gf_motion_error < this_error) |
| 673 sr_coded_error += gf_motion_error; |
| 674 else |
| 675 sr_coded_error += this_error; |
| 676 } else { |
| 677 sr_coded_error += motion_error; |
| 678 } |
| 706 } else { | 679 } else { |
| 707 sr_coded_error += motion_error; | 680 sr_coded_error += motion_error; |
| 708 } | 681 } |
| 682 |
| 709 // Start by assuming that intra mode is best. | 683 // Start by assuming that intra mode is best. |
| 710 best_ref_mv.as_int = 0; | 684 best_ref_mv.as_int = 0; |
| 711 | 685 |
| 712 if (motion_error <= this_error) { | 686 if (motion_error <= this_error) { |
| 713 // Keep a count of cases where the inter and intra were very close | 687 // Keep a count of cases where the inter and intra were very close |
| 714 // and very low. This helps with scene cut detection for example in | 688 // and very low. This helps with scene cut detection for example in |
| 715 // cropped clips with black bars at the sides or top and bottom. | 689 // cropped clips with black bars at the sides or top and bottom. |
| 716 if (((this_error - intrapenalty) * 9 <= motion_error * 10) && | 690 if (((this_error - intrapenalty) * 9 <= motion_error * 10) && |
| 717 this_error < 2 * intrapenalty) | 691 this_error < 2 * intrapenalty) |
| 718 ++neutral_count; | 692 ++neutral_count; |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 798 | 772 |
| 799 vp9_clear_system_state(); | 773 vp9_clear_system_state(); |
| 800 { | 774 { |
| 801 FIRSTPASS_STATS fps; | 775 FIRSTPASS_STATS fps; |
| 802 | 776 |
| 803 fps.frame = cm->current_video_frame; | 777 fps.frame = cm->current_video_frame; |
| 804 fps.spatial_layer_id = cpi->svc.spatial_layer_id; | 778 fps.spatial_layer_id = cpi->svc.spatial_layer_id; |
| 805 fps.intra_error = (double)(intra_error >> 8); | 779 fps.intra_error = (double)(intra_error >> 8); |
| 806 fps.coded_error = (double)(coded_error >> 8); | 780 fps.coded_error = (double)(coded_error >> 8); |
| 807 fps.sr_coded_error = (double)(sr_coded_error >> 8); | 781 fps.sr_coded_error = (double)(sr_coded_error >> 8); |
| 808 fps.ssim_weighted_pred_err = fps.coded_error * simple_weight(cpi->Source); | |
| 809 fps.count = 1.0; | 782 fps.count = 1.0; |
| 810 fps.pcnt_inter = (double)intercount / cm->MBs; | 783 fps.pcnt_inter = (double)intercount / cm->MBs; |
| 811 fps.pcnt_second_ref = (double)second_ref_count / cm->MBs; | 784 fps.pcnt_second_ref = (double)second_ref_count / cm->MBs; |
| 812 fps.pcnt_neutral = (double)neutral_count / cm->MBs; | 785 fps.pcnt_neutral = (double)neutral_count / cm->MBs; |
| 813 | 786 |
| 814 if (mvcount > 0) { | 787 if (mvcount > 0) { |
| 815 fps.MVr = (double)sum_mvr / mvcount; | 788 fps.MVr = (double)sum_mvr / mvcount; |
| 816 fps.mvr_abs = (double)sum_mvr_abs / mvcount; | 789 fps.mvr_abs = (double)sum_mvr_abs / mvcount; |
| 817 fps.MVc = (double)sum_mvc / mvcount; | 790 fps.MVc = (double)sum_mvc / mvcount; |
| 818 fps.mvc_abs = (double)sum_mvc_abs / mvcount; | 791 fps.mvc_abs = (double)sum_mvc_abs / mvcount; |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 931 if (cpi->use_svc && cpi->svc.number_temporal_layers == 1 && | 904 if (cpi->use_svc && cpi->svc.number_temporal_layers == 1 && |
| 932 cpi->svc.spatial_layer_id > 0) { | 905 cpi->svc.spatial_layer_id > 0) { |
| 933 is_svc_upper_layer = 1; | 906 is_svc_upper_layer = 1; |
| 934 } | 907 } |
| 935 | 908 |
| 936 // Try and pick a max Q that will be high enough to encode the | 909 // Try and pick a max Q that will be high enough to encode the |
| 937 // content at the given rate. | 910 // content at the given rate. |
| 938 for (q = rc->best_quality; q < rc->worst_quality; ++q) { | 911 for (q = rc->best_quality; q < rc->worst_quality; ++q) { |
| 939 const double factor = | 912 const double factor = |
| 940 calc_correction_factor(err_per_mb, ERR_DIVISOR, | 913 calc_correction_factor(err_per_mb, ERR_DIVISOR, |
| 941 is_svc_upper_layer ? 0.8 : 0.5, | 914 is_svc_upper_layer ? SVC_FACTOR_PT_LOW : |
| 942 is_svc_upper_layer ? 1.0 : 0.90, q); | 915 FACTOR_PT_LOW, FACTOR_PT_HIGH, q); |
| 943 const int bits_per_mb = vp9_rc_bits_per_mb(INTER_FRAME, q, | 916 const int bits_per_mb = vp9_rc_bits_per_mb(INTER_FRAME, q, |
| 944 factor * speed_term); | 917 factor * speed_term); |
| 945 if (bits_per_mb <= target_norm_bits_per_mb) | 918 if (bits_per_mb <= target_norm_bits_per_mb) |
| 946 break; | 919 break; |
| 947 } | 920 } |
| 948 | 921 |
| 949 // Restriction on active max q for constrained quality mode. | 922 // Restriction on active max q for constrained quality mode. |
| 950 if (cpi->oxcf.rc_mode == RC_MODE_CONSTRAINED_QUALITY) | 923 if (cpi->oxcf.rc_mode == VPX_CQ) |
| 951 q = MAX(q, oxcf->cq_level); | 924 q = MAX(q, oxcf->cq_level); |
| 952 return q; | 925 return q; |
| 953 } | 926 } |
| 954 } | 927 } |
| 955 | 928 |
| 956 extern void vp9_new_framerate(VP9_COMP *cpi, double framerate); | 929 extern void vp9_new_framerate(VP9_COMP *cpi, double framerate); |
| 957 | 930 |
| 958 void vp9_init_second_pass(VP9_COMP *cpi) { | 931 void vp9_init_second_pass(VP9_COMP *cpi) { |
| 959 SVC *const svc = &cpi->svc; | 932 SVC *const svc = &cpi->svc; |
| 960 const VP9EncoderConfig *const oxcf = &cpi->oxcf; | 933 const VP9EncoderConfig *const oxcf = &cpi->oxcf; |
| 961 const int is_spatial_svc = (svc->number_spatial_layers > 1) && | 934 const int is_spatial_svc = (svc->number_spatial_layers > 1) && |
| 962 (svc->number_temporal_layers == 1); | 935 (svc->number_temporal_layers == 1); |
| 963 struct twopass_rc *const twopass = is_spatial_svc ? | 936 TWO_PASS *const twopass = is_spatial_svc ? |
| 964 &svc->layer_context[svc->spatial_layer_id].twopass : &cpi->twopass; | 937 &svc->layer_context[svc->spatial_layer_id].twopass : &cpi->twopass; |
| 965 double frame_rate; | 938 double frame_rate; |
| 966 FIRSTPASS_STATS *stats; | 939 FIRSTPASS_STATS *stats; |
| 967 | 940 |
| 968 zero_stats(&twopass->total_stats); | 941 zero_stats(&twopass->total_stats); |
| 969 zero_stats(&twopass->total_left_stats); | 942 zero_stats(&twopass->total_left_stats); |
| 970 | 943 |
| 971 if (!twopass->stats_in_end) | 944 if (!twopass->stats_in_end) |
| 972 return; | 945 return; |
| 973 | 946 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 1001 if (!is_spatial_svc) { | 974 if (!is_spatial_svc) { |
| 1002 // We don't know the number of MBs for each layer at this point. | 975 // We don't know the number of MBs for each layer at this point. |
| 1003 // So we will do it later. | 976 // So we will do it later. |
| 1004 twopass->kf_intra_err_min = KF_MB_INTRA_MIN * cpi->common.MBs; | 977 twopass->kf_intra_err_min = KF_MB_INTRA_MIN * cpi->common.MBs; |
| 1005 twopass->gf_intra_err_min = GF_MB_INTRA_MIN * cpi->common.MBs; | 978 twopass->gf_intra_err_min = GF_MB_INTRA_MIN * cpi->common.MBs; |
| 1006 } | 979 } |
| 1007 | 980 |
| 1008 // This variable monitors how far behind the second ref update is lagging. | 981 // This variable monitors how far behind the second ref update is lagging. |
| 1009 twopass->sr_update_lag = 1; | 982 twopass->sr_update_lag = 1; |
| 1010 | 983 |
| 1011 // Scan the first pass file and calculate an average Intra / Inter error | |
| 1012 // score ratio for the sequence. | |
| 1013 { | |
| 1014 const FIRSTPASS_STATS *const start_pos = twopass->stats_in; | |
| 1015 FIRSTPASS_STATS this_frame; | |
| 1016 double sum_iiratio = 0.0; | |
| 1017 | |
| 1018 while (input_stats(twopass, &this_frame) != EOF) { | |
| 1019 const double iiratio = this_frame.intra_error / | |
| 1020 DOUBLE_DIVIDE_CHECK(this_frame.coded_error); | |
| 1021 sum_iiratio += fclamp(iiratio, 1.0, 20.0); | |
| 1022 } | |
| 1023 | |
| 1024 twopass->avg_iiratio = sum_iiratio / | |
| 1025 DOUBLE_DIVIDE_CHECK((double)stats->count); | |
| 1026 | |
| 1027 reset_fpf_position(twopass, start_pos); | |
| 1028 } | |
| 1029 | |
| 1030 // Scan the first pass file and calculate a modified total error based upon | 984 // Scan the first pass file and calculate a modified total error based upon |
| 1031 // the bias/power function used to allocate bits. | 985 // the bias/power function used to allocate bits. |
| 1032 { | 986 { |
| 1033 const FIRSTPASS_STATS *const start_pos = twopass->stats_in; | 987 const double avg_error = stats->coded_error / |
| 1034 FIRSTPASS_STATS this_frame; | 988 DOUBLE_DIVIDE_CHECK(stats->count); |
| 1035 const double av_error = stats->ssim_weighted_pred_err / | 989 const FIRSTPASS_STATS *s = twopass->stats_in; |
| 1036 DOUBLE_DIVIDE_CHECK(stats->count); | 990 double modified_error_total = 0.0; |
| 1037 | 991 twopass->modified_error_min = (avg_error * |
| 1038 | 992 oxcf->two_pass_vbrmin_section) / 100; |
| 1039 twopass->modified_error_total = 0.0; | 993 twopass->modified_error_max = (avg_error * |
| 1040 twopass->modified_error_min = | 994 oxcf->two_pass_vbrmax_section) / 100; |
| 1041 (av_error * oxcf->two_pass_vbrmin_section) / 100; | 995 while (s < twopass->stats_in_end) { |
| 1042 twopass->modified_error_max = | 996 modified_error_total += calculate_modified_err(twopass, oxcf, s); |
| 1043 (av_error * oxcf->two_pass_vbrmax_section) / 100; | 997 ++s; |
| 1044 | |
| 1045 while (input_stats(twopass, &this_frame) != EOF) { | |
| 1046 twopass->modified_error_total += | |
| 1047 calculate_modified_err(cpi, &this_frame); | |
| 1048 } | 998 } |
| 1049 twopass->modified_error_left = twopass->modified_error_total; | 999 twopass->modified_error_left = modified_error_total; |
| 1050 | |
| 1051 reset_fpf_position(twopass, start_pos); | |
| 1052 } | 1000 } |
| 1053 | 1001 |
| 1054 // Reset the vbr bits off target counter | 1002 // Reset the vbr bits off target counter |
| 1055 cpi->rc.vbr_bits_off_target = 0; | 1003 cpi->rc.vbr_bits_off_target = 0; |
| 1056 } | 1004 } |
| 1057 | 1005 |
| 1058 // This function gives an estimate of how badly we believe the prediction | 1006 // This function gives an estimate of how badly we believe the prediction |
| 1059 // quality is decaying from frame to frame. | 1007 // quality is decaying from frame to frame. |
| 1060 static double get_prediction_decay_rate(const VP9_COMMON *cm, | 1008 static double get_prediction_decay_rate(const VP9_COMMON *cm, |
| 1061 const FIRSTPASS_STATS *next_frame) { | 1009 const FIRSTPASS_STATS *next_frame) { |
| 1062 // Look at the observed drop in prediction quality between the last frame | 1010 // Look at the observed drop in prediction quality between the last frame |
| 1063 // and the GF buffer (which contains an older frame). | 1011 // and the GF buffer (which contains an older frame). |
| 1064 const double mb_sr_err_diff = (next_frame->sr_coded_error - | 1012 const double mb_sr_err_diff = (next_frame->sr_coded_error - |
| 1065 next_frame->coded_error) / cm->MBs; | 1013 next_frame->coded_error) / cm->MBs; |
| 1066 const double second_ref_decay = mb_sr_err_diff <= 512.0 | 1014 const double second_ref_decay = mb_sr_err_diff <= 512.0 |
| 1067 ? fclamp(pow(1.0 - (mb_sr_err_diff / 512.0), 0.5), 0.85, 1.0) | 1015 ? fclamp(pow(1.0 - (mb_sr_err_diff / 512.0), 0.5), 0.85, 1.0) |
| 1068 : 0.85; | 1016 : 0.85; |
| 1069 | 1017 |
| 1070 return MIN(second_ref_decay, next_frame->pcnt_inter); | 1018 return MIN(second_ref_decay, next_frame->pcnt_inter); |
| 1071 } | 1019 } |
| 1072 | 1020 |
| 1073 // Function to test for a condition where a complex transition is followed | 1021 // Function to test for a condition where a complex transition is followed |
| 1074 // by a static section. For example in slide shows where there is a fade | 1022 // by a static section. For example in slide shows where there is a fade |
| 1075 // between slides. This is to help with more optimal kf and gf positioning. | 1023 // between slides. This is to help with more optimal kf and gf positioning. |
| 1076 static int detect_transition_to_still(struct twopass_rc *twopass, | 1024 static int detect_transition_to_still(TWO_PASS *twopass, |
| 1077 int frame_interval, int still_interval, | 1025 int frame_interval, int still_interval, |
| 1078 double loop_decay_rate, | 1026 double loop_decay_rate, |
| 1079 double last_decay_rate) { | 1027 double last_decay_rate) { |
| 1080 int trans_to_still = 0; | 1028 int trans_to_still = 0; |
| 1081 | 1029 |
| 1082 // Break clause to detect very still sections after motion | 1030 // Break clause to detect very still sections after motion |
| 1083 // For example a static image after a fade or other transition | 1031 // For example a static image after a fade or other transition |
| 1084 // instead of a clean scene cut. | 1032 // instead of a clean scene cut. |
| 1085 if (frame_interval > MIN_GF_INTERVAL && | 1033 if (frame_interval > MIN_GF_INTERVAL && |
| 1086 loop_decay_rate >= 0.999 && | 1034 loop_decay_rate >= 0.999 && |
| (...skipping 17 matching lines...) Expand all Loading... |
| 1104 if (j == still_interval) | 1052 if (j == still_interval) |
| 1105 trans_to_still = 1; | 1053 trans_to_still = 1; |
| 1106 } | 1054 } |
| 1107 | 1055 |
| 1108 return trans_to_still; | 1056 return trans_to_still; |
| 1109 } | 1057 } |
| 1110 | 1058 |
| 1111 // This function detects a flash through the high relative pcnt_second_ref | 1059 // This function detects a flash through the high relative pcnt_second_ref |
| 1112 // score in the frame following a flash frame. The offset passed in should | 1060 // score in the frame following a flash frame. The offset passed in should |
| 1113 // reflect this. | 1061 // reflect this. |
| 1114 static int detect_flash(const struct twopass_rc *twopass, int offset) { | 1062 static int detect_flash(const TWO_PASS *twopass, int offset) { |
| 1115 FIRSTPASS_STATS next_frame; | 1063 FIRSTPASS_STATS next_frame; |
| 1116 | 1064 |
| 1117 int flash_detected = 0; | 1065 int flash_detected = 0; |
| 1118 | 1066 |
| 1119 // Read the frame data. | 1067 // Read the frame data. |
| 1120 // The return is FALSE (no flash detected) if not a valid frame | 1068 // The return is FALSE (no flash detected) if not a valid frame |
| 1121 if (read_frame_stats(twopass, &next_frame, offset) != EOF) { | 1069 if (read_frame_stats(twopass, &next_frame, offset) != EOF) { |
| 1122 // What we are looking for here is a situation where there is a | 1070 // What we are looking for here is a situation where there is a |
| 1123 // brief break in prediction (such as a flash) but subsequent frames | 1071 // brief break in prediction (such as a flash) but subsequent frames |
| 1124 // are reasonably well predicted by an earlier (pre flash) frame. | 1072 // are reasonably well predicted by an earlier (pre flash) frame. |
| 1125 // The recovery after a flash is indicated by a high pcnt_second_ref | 1073 // The recovery after a flash is indicated by a high pcnt_second_ref |
| 1126 // compared to pcnt_inter. | 1074 // compared to pcnt_inter. |
| 1127 if (next_frame.pcnt_second_ref > next_frame.pcnt_inter && | 1075 if (next_frame.pcnt_second_ref > next_frame.pcnt_inter && |
| 1128 next_frame.pcnt_second_ref >= 0.5) | 1076 next_frame.pcnt_second_ref >= 0.5) |
| 1129 flash_detected = 1; | 1077 flash_detected = 1; |
| 1130 } | 1078 } |
| 1131 | 1079 |
| 1132 return flash_detected; | 1080 return flash_detected; |
| 1133 } | 1081 } |
| 1134 | 1082 |
| 1135 // Update the motion related elements to the GF arf boost calculation. | 1083 // Update the motion related elements to the GF arf boost calculation. |
| 1136 static void accumulate_frame_motion_stats( | 1084 static void accumulate_frame_motion_stats(const FIRSTPASS_STATS *stats, |
| 1137 FIRSTPASS_STATS *this_frame, | 1085 double *mv_in_out, |
| 1138 double *this_frame_mv_in_out, | 1086 double *mv_in_out_accumulator, |
| 1139 double *mv_in_out_accumulator, | 1087 double *abs_mv_in_out_accumulator, |
| 1140 double *abs_mv_in_out_accumulator, | 1088 double *mv_ratio_accumulator) { |
| 1141 double *mv_ratio_accumulator) { | 1089 const double pct = stats->pcnt_motion; |
| 1142 double motion_pct; | |
| 1143 | |
| 1144 // Accumulate motion stats. | |
| 1145 motion_pct = this_frame->pcnt_motion; | |
| 1146 | 1090 |
| 1147 // Accumulate Motion In/Out of frame stats. | 1091 // Accumulate Motion In/Out of frame stats. |
| 1148 *this_frame_mv_in_out = this_frame->mv_in_out_count * motion_pct; | 1092 *mv_in_out = stats->mv_in_out_count * pct; |
| 1149 *mv_in_out_accumulator += this_frame->mv_in_out_count * motion_pct; | 1093 *mv_in_out_accumulator += *mv_in_out; |
| 1150 *abs_mv_in_out_accumulator += fabs(this_frame->mv_in_out_count * motion_pct); | 1094 *abs_mv_in_out_accumulator += fabs(*mv_in_out); |
| 1151 | 1095 |
| 1152 // Accumulate a measure of how uniform (or conversely how random) | 1096 // Accumulate a measure of how uniform (or conversely how random) the motion |
| 1153 // the motion field is (a ratio of absmv / mv). | 1097 // field is (a ratio of abs(mv) / mv). |
| 1154 if (motion_pct > 0.05) { | 1098 if (pct > 0.05) { |
| 1155 const double this_frame_mvr_ratio = fabs(this_frame->mvr_abs) / | 1099 const double mvr_ratio = fabs(stats->mvr_abs) / |
| 1156 DOUBLE_DIVIDE_CHECK(fabs(this_frame->MVr)); | 1100 DOUBLE_DIVIDE_CHECK(fabs(stats->MVr)); |
| 1101 const double mvc_ratio = fabs(stats->mvc_abs) / |
| 1102 DOUBLE_DIVIDE_CHECK(fabs(stats->MVc)); |
| 1157 | 1103 |
| 1158 const double this_frame_mvc_ratio = fabs(this_frame->mvc_abs) / | 1104 *mv_ratio_accumulator += pct * (mvr_ratio < stats->mvr_abs ? |
| 1159 DOUBLE_DIVIDE_CHECK(fabs(this_frame->MVc)); | 1105 mvr_ratio : stats->mvr_abs); |
| 1160 | 1106 *mv_ratio_accumulator += pct * (mvc_ratio < stats->mvc_abs ? |
| 1161 *mv_ratio_accumulator += (this_frame_mvr_ratio < this_frame->mvr_abs) | 1107 mvc_ratio : stats->mvc_abs); |
| 1162 ? (this_frame_mvr_ratio * motion_pct) | |
| 1163 : this_frame->mvr_abs * motion_pct; | |
| 1164 | |
| 1165 *mv_ratio_accumulator += (this_frame_mvc_ratio < this_frame->mvc_abs) | |
| 1166 ? (this_frame_mvc_ratio * motion_pct) | |
| 1167 : this_frame->mvc_abs * motion_pct; | |
| 1168 } | 1108 } |
| 1169 } | 1109 } |
| 1170 | 1110 |
| 1171 // Calculate a baseline boost number for the current frame. | 1111 // Calculate a baseline boost number for the current frame. |
| 1172 static double calc_frame_boost(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame, | 1112 static double calc_frame_boost(const TWO_PASS *twopass, |
| 1113 const FIRSTPASS_STATS *this_frame, |
| 1173 double this_frame_mv_in_out) { | 1114 double this_frame_mv_in_out) { |
| 1174 double frame_boost; | 1115 double frame_boost; |
| 1175 | 1116 |
| 1176 // Underlying boost factor is based on inter intra error ratio. | 1117 // Underlying boost factor is based on inter intra error ratio. |
| 1177 if (this_frame->intra_error > cpi->twopass.gf_intra_err_min) | 1118 if (this_frame->intra_error > twopass->gf_intra_err_min) |
| 1178 frame_boost = (IIFACTOR * this_frame->intra_error / | 1119 frame_boost = (IIFACTOR * this_frame->intra_error / |
| 1179 DOUBLE_DIVIDE_CHECK(this_frame->coded_error)); | 1120 DOUBLE_DIVIDE_CHECK(this_frame->coded_error)); |
| 1180 else | 1121 else |
| 1181 frame_boost = (IIFACTOR * cpi->twopass.gf_intra_err_min / | 1122 frame_boost = (IIFACTOR * twopass->gf_intra_err_min / |
| 1182 DOUBLE_DIVIDE_CHECK(this_frame->coded_error)); | 1123 DOUBLE_DIVIDE_CHECK(this_frame->coded_error)); |
| 1183 | 1124 |
| 1184 // Increase boost for frames where new data coming into frame (e.g. zoom out). | 1125 // Increase boost for frames where new data coming into frame (e.g. zoom out). |
| 1185 // Slightly reduce boost if there is a net balance of motion out of the frame | 1126 // Slightly reduce boost if there is a net balance of motion out of the frame |
| 1186 // (zoom in). The range for this_frame_mv_in_out is -1.0 to +1.0. | 1127 // (zoom in). The range for this_frame_mv_in_out is -1.0 to +1.0. |
| 1187 if (this_frame_mv_in_out > 0.0) | 1128 if (this_frame_mv_in_out > 0.0) |
| 1188 frame_boost += frame_boost * (this_frame_mv_in_out * 2.0); | 1129 frame_boost += frame_boost * (this_frame_mv_in_out * 2.0); |
| 1189 // In the extreme case the boost is halved. | 1130 // In the extreme case the boost is halved. |
| 1190 else | 1131 else |
| 1191 frame_boost += frame_boost * (this_frame_mv_in_out / 2.0); | 1132 frame_boost += frame_boost * (this_frame_mv_in_out / 2.0); |
| 1192 | 1133 |
| 1193 return MIN(frame_boost, GF_RMAX); | 1134 return MIN(frame_boost, GF_RMAX); |
| 1194 } | 1135 } |
| 1195 | 1136 |
| 1196 static int calc_arf_boost(VP9_COMP *cpi, int offset, | 1137 static int calc_arf_boost(VP9_COMP *cpi, int offset, |
| 1197 int f_frames, int b_frames, | 1138 int f_frames, int b_frames, |
| 1198 int *f_boost, int *b_boost) { | 1139 int *f_boost, int *b_boost) { |
| 1199 FIRSTPASS_STATS this_frame; | 1140 FIRSTPASS_STATS this_frame; |
| 1200 struct twopass_rc *const twopass = &cpi->twopass; | 1141 TWO_PASS *const twopass = &cpi->twopass; |
| 1201 int i; | 1142 int i; |
| 1202 double boost_score = 0.0; | 1143 double boost_score = 0.0; |
| 1203 double mv_ratio_accumulator = 0.0; | 1144 double mv_ratio_accumulator = 0.0; |
| 1204 double decay_accumulator = 1.0; | 1145 double decay_accumulator = 1.0; |
| 1205 double this_frame_mv_in_out = 0.0; | 1146 double this_frame_mv_in_out = 0.0; |
| 1206 double mv_in_out_accumulator = 0.0; | 1147 double mv_in_out_accumulator = 0.0; |
| 1207 double abs_mv_in_out_accumulator = 0.0; | 1148 double abs_mv_in_out_accumulator = 0.0; |
| 1208 int arf_boost; | 1149 int arf_boost; |
| 1209 int flash_detected = 0; | 1150 int flash_detected = 0; |
| 1210 | 1151 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 1224 flash_detected = detect_flash(twopass, i + offset) || | 1165 flash_detected = detect_flash(twopass, i + offset) || |
| 1225 detect_flash(twopass, i + offset + 1); | 1166 detect_flash(twopass, i + offset + 1); |
| 1226 | 1167 |
| 1227 // Accumulate the effect of prediction quality decay. | 1168 // Accumulate the effect of prediction quality decay. |
| 1228 if (!flash_detected) { | 1169 if (!flash_detected) { |
| 1229 decay_accumulator *= get_prediction_decay_rate(&cpi->common, &this_frame); | 1170 decay_accumulator *= get_prediction_decay_rate(&cpi->common, &this_frame); |
| 1230 decay_accumulator = decay_accumulator < MIN_DECAY_FACTOR | 1171 decay_accumulator = decay_accumulator < MIN_DECAY_FACTOR |
| 1231 ? MIN_DECAY_FACTOR : decay_accumulator; | 1172 ? MIN_DECAY_FACTOR : decay_accumulator; |
| 1232 } | 1173 } |
| 1233 | 1174 |
| 1234 boost_score += (decay_accumulator * | 1175 boost_score += decay_accumulator * calc_frame_boost(twopass, &this_frame, |
| 1235 calc_frame_boost(cpi, &this_frame, this_frame_mv_in_out)); | 1176 this_frame_mv_in_out); |
| 1236 } | 1177 } |
| 1237 | 1178 |
| 1238 *f_boost = (int)boost_score; | 1179 *f_boost = (int)boost_score; |
| 1239 | 1180 |
| 1240 // Reset for backward looking loop. | 1181 // Reset for backward looking loop. |
| 1241 boost_score = 0.0; | 1182 boost_score = 0.0; |
| 1242 mv_ratio_accumulator = 0.0; | 1183 mv_ratio_accumulator = 0.0; |
| 1243 decay_accumulator = 1.0; | 1184 decay_accumulator = 1.0; |
| 1244 this_frame_mv_in_out = 0.0; | 1185 this_frame_mv_in_out = 0.0; |
| 1245 mv_in_out_accumulator = 0.0; | 1186 mv_in_out_accumulator = 0.0; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 1261 flash_detected = detect_flash(twopass, i + offset) || | 1202 flash_detected = detect_flash(twopass, i + offset) || |
| 1262 detect_flash(twopass, i + offset + 1); | 1203 detect_flash(twopass, i + offset + 1); |
| 1263 | 1204 |
| 1264 // Cumulative effect of prediction quality decay. | 1205 // Cumulative effect of prediction quality decay. |
| 1265 if (!flash_detected) { | 1206 if (!flash_detected) { |
| 1266 decay_accumulator *= get_prediction_decay_rate(&cpi->common, &this_frame); | 1207 decay_accumulator *= get_prediction_decay_rate(&cpi->common, &this_frame); |
| 1267 decay_accumulator = decay_accumulator < MIN_DECAY_FACTOR | 1208 decay_accumulator = decay_accumulator < MIN_DECAY_FACTOR |
| 1268 ? MIN_DECAY_FACTOR : decay_accumulator; | 1209 ? MIN_DECAY_FACTOR : decay_accumulator; |
| 1269 } | 1210 } |
| 1270 | 1211 |
| 1271 boost_score += (decay_accumulator * | 1212 boost_score += decay_accumulator * calc_frame_boost(twopass, &this_frame, |
| 1272 calc_frame_boost(cpi, &this_frame, this_frame_mv_in_out)); | 1213 this_frame_mv_in_out); |
| 1273 } | 1214 } |
| 1274 *b_boost = (int)boost_score; | 1215 *b_boost = (int)boost_score; |
| 1275 | 1216 |
| 1276 arf_boost = (*f_boost + *b_boost); | 1217 arf_boost = (*f_boost + *b_boost); |
| 1277 if (arf_boost < ((b_frames + f_frames) * 20)) | 1218 if (arf_boost < ((b_frames + f_frames) * 20)) |
| 1278 arf_boost = ((b_frames + f_frames) * 20); | 1219 arf_boost = ((b_frames + f_frames) * 20); |
| 1279 | 1220 |
| 1280 return arf_boost; | 1221 return arf_boost; |
| 1281 } | 1222 } |
| 1282 | 1223 |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1412 printf("Weight: "); | 1353 printf("Weight: "); |
| 1413 for (i = 0; i < cpi->new_frame_coding_order_period; ++i) { | 1354 for (i = 0; i < cpi->new_frame_coding_order_period; ++i) { |
| 1414 printf("%4d ", cpi->arf_weight[i]); | 1355 printf("%4d ", cpi->arf_weight[i]); |
| 1415 } | 1356 } |
| 1416 printf("\n"); | 1357 printf("\n"); |
| 1417 #endif | 1358 #endif |
| 1418 } | 1359 } |
| 1419 #endif | 1360 #endif |
| 1420 | 1361 |
| 1421 // Calculate a section intra ratio used in setting max loop filter. | 1362 // Calculate a section intra ratio used in setting max loop filter. |
| 1422 static void calculate_section_intra_ratio(struct twopass_rc *twopass, | 1363 static int calculate_section_intra_ratio(const FIRSTPASS_STATS *begin, |
| 1423 const FIRSTPASS_STATS *start_pos, | 1364 const FIRSTPASS_STATS *end, |
| 1424 int section_length) { | 1365 int section_length) { |
| 1425 FIRSTPASS_STATS next_frame; | 1366 const FIRSTPASS_STATS *s = begin; |
| 1426 FIRSTPASS_STATS sectionstats; | 1367 double intra_error = 0.0; |
| 1427 int i; | 1368 double coded_error = 0.0; |
| 1369 int i = 0; |
| 1428 | 1370 |
| 1429 vp9_zero(next_frame); | 1371 while (s < end && i < section_length) { |
| 1430 vp9_zero(sectionstats); | 1372 intra_error += s->intra_error; |
| 1431 | 1373 coded_error += s->coded_error; |
| 1432 reset_fpf_position(twopass, start_pos); | 1374 ++s; |
| 1433 | 1375 ++i; |
| 1434 for (i = 0; i < section_length; ++i) { | |
| 1435 input_stats(twopass, &next_frame); | |
| 1436 accumulate_stats(§ionstats, &next_frame); | |
| 1437 } | 1376 } |
| 1438 | 1377 |
| 1439 avg_stats(§ionstats); | 1378 return (int)(intra_error / DOUBLE_DIVIDE_CHECK(coded_error)); |
| 1440 | |
| 1441 twopass->section_intra_rating = | |
| 1442 (int)(sectionstats.intra_error / | |
| 1443 DOUBLE_DIVIDE_CHECK(sectionstats.coded_error)); | |
| 1444 | |
| 1445 reset_fpf_position(twopass, start_pos); | |
| 1446 } | 1379 } |
| 1447 | 1380 |
| 1448 // Calculate the total bits to allocate in this GF/ARF group. | 1381 // Calculate the total bits to allocate in this GF/ARF group. |
| 1449 static int64_t calculate_total_gf_group_bits(VP9_COMP *cpi, | 1382 static int64_t calculate_total_gf_group_bits(VP9_COMP *cpi, |
| 1450 double gf_group_err) { | 1383 double gf_group_err) { |
| 1451 const RATE_CONTROL *const rc = &cpi->rc; | 1384 const RATE_CONTROL *const rc = &cpi->rc; |
| 1452 const struct twopass_rc *const twopass = &cpi->twopass; | 1385 const TWO_PASS *const twopass = &cpi->twopass; |
| 1453 const int max_bits = frame_max_bits(rc, &cpi->oxcf); | 1386 const int max_bits = frame_max_bits(rc, &cpi->oxcf); |
| 1454 int64_t total_group_bits; | 1387 int64_t total_group_bits; |
| 1455 | 1388 |
| 1456 // Calculate the bits to be allocated to the group as a whole. | 1389 // Calculate the bits to be allocated to the group as a whole. |
| 1457 if ((twopass->kf_group_bits > 0) && (twopass->kf_group_error_left > 0)) { | 1390 if ((twopass->kf_group_bits > 0) && (twopass->kf_group_error_left > 0)) { |
| 1458 total_group_bits = (int64_t)(twopass->kf_group_bits * | 1391 total_group_bits = (int64_t)(twopass->kf_group_bits * |
| 1459 (gf_group_err / twopass->kf_group_error_left)); | 1392 (gf_group_err / twopass->kf_group_error_left)); |
| 1460 } else { | 1393 } else { |
| 1461 total_group_bits = 0; | 1394 total_group_bits = 0; |
| 1462 } | 1395 } |
| (...skipping 25 matching lines...) Expand all Loading... |
| 1488 if (boost > 1023) { | 1421 if (boost > 1023) { |
| 1489 int divisor = boost >> 10; | 1422 int divisor = boost >> 10; |
| 1490 boost /= divisor; | 1423 boost /= divisor; |
| 1491 allocation_chunks /= divisor; | 1424 allocation_chunks /= divisor; |
| 1492 } | 1425 } |
| 1493 | 1426 |
| 1494 // Calculate the number of extra bits for use in the boosted frame or frames. | 1427 // Calculate the number of extra bits for use in the boosted frame or frames. |
| 1495 return MAX((int)(((int64_t)boost * total_group_bits) / allocation_chunks), 0); | 1428 return MAX((int)(((int64_t)boost * total_group_bits) / allocation_chunks), 0); |
| 1496 } | 1429 } |
| 1497 | 1430 |
| 1431 static void allocate_gf_group_bits(VP9_COMP *cpi, int64_t gf_group_bits, |
| 1432 double group_error, int gf_arf_bits) { |
| 1433 RATE_CONTROL *const rc = &cpi->rc; |
| 1434 const VP9EncoderConfig *const oxcf = &cpi->oxcf; |
| 1435 TWO_PASS *twopass = &cpi->twopass; |
| 1436 FIRSTPASS_STATS frame_stats; |
| 1437 int i; |
| 1438 int group_frame_index = 1; |
| 1439 int target_frame_size; |
| 1440 int key_frame; |
| 1441 const int max_bits = frame_max_bits(&cpi->rc, &cpi->oxcf); |
| 1442 int64_t total_group_bits = gf_group_bits; |
| 1443 double modified_err = 0.0; |
| 1444 double err_fraction; |
| 1445 |
| 1446 key_frame = cpi->common.frame_type == KEY_FRAME || |
| 1447 vp9_is_upper_layer_key_frame(cpi); |
| 1448 |
| 1449 // For key frames the frame target rate is already set and it |
| 1450 // is also the golden frame. |
| 1451 // NOTE: We dont bother to check for the special case of ARF overlay |
| 1452 // frames here, as there is clamping code for this in the function |
| 1453 // vp9_rc_clamp_pframe_target_size(), which applies to one and two pass |
| 1454 // encodes. |
| 1455 if (!key_frame) { |
| 1456 twopass->gf_group_bit_allocation[0] = gf_arf_bits; |
| 1457 |
| 1458 // Step over the golden frame / overlay frame |
| 1459 if (EOF == input_stats(twopass, &frame_stats)) |
| 1460 return; |
| 1461 } |
| 1462 |
| 1463 // Store the bits to spend on the ARF if there is one. |
| 1464 if (rc->source_alt_ref_pending) { |
| 1465 twopass->gf_group_bit_allocation[group_frame_index++] = gf_arf_bits; |
| 1466 } |
| 1467 |
| 1468 // Deduct the boost bits for arf or gf if it is not a key frame. |
| 1469 if (rc->source_alt_ref_pending || !key_frame) |
| 1470 total_group_bits -= gf_arf_bits; |
| 1471 |
| 1472 // Allocate bits to the other frames in the group. |
| 1473 for (i = 0; i < rc->baseline_gf_interval - 1; ++i) { |
| 1474 if (EOF == input_stats(twopass, &frame_stats)) |
| 1475 break; |
| 1476 |
| 1477 modified_err = calculate_modified_err(twopass, oxcf, &frame_stats); |
| 1478 |
| 1479 if (group_error > 0) |
| 1480 err_fraction = modified_err / DOUBLE_DIVIDE_CHECK(group_error); |
| 1481 else |
| 1482 err_fraction = 0.0; |
| 1483 |
| 1484 target_frame_size = (int)((double)total_group_bits * err_fraction); |
| 1485 target_frame_size = clamp(target_frame_size, 0, |
| 1486 MIN(max_bits, (int)total_group_bits)); |
| 1487 |
| 1488 twopass->gf_group_bit_allocation[group_frame_index++] = target_frame_size; |
| 1489 } |
| 1490 } |
| 1498 | 1491 |
| 1499 // Analyse and define a gf/arf group. | 1492 // Analyse and define a gf/arf group. |
| 1500 static void define_gf_group(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame) { | 1493 static void define_gf_group(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame) { |
| 1501 RATE_CONTROL *const rc = &cpi->rc; | 1494 RATE_CONTROL *const rc = &cpi->rc; |
| 1502 const VP9EncoderConfig *const oxcf = &cpi->oxcf; | 1495 const VP9EncoderConfig *const oxcf = &cpi->oxcf; |
| 1503 struct twopass_rc *const twopass = &cpi->twopass; | 1496 TWO_PASS *const twopass = &cpi->twopass; |
| 1504 FIRSTPASS_STATS next_frame; | 1497 FIRSTPASS_STATS next_frame; |
| 1505 const FIRSTPASS_STATS *start_pos; | 1498 const FIRSTPASS_STATS *const start_pos = twopass->stats_in; |
| 1506 int i; | 1499 int i; |
| 1500 |
| 1507 double boost_score = 0.0; | 1501 double boost_score = 0.0; |
| 1508 double old_boost_score = 0.0; | 1502 double old_boost_score = 0.0; |
| 1509 double gf_group_err = 0.0; | 1503 double gf_group_err = 0.0; |
| 1510 double gf_first_frame_err = 0.0; | 1504 double gf_first_frame_err = 0.0; |
| 1511 double mod_frame_err = 0.0; | 1505 double mod_frame_err = 0.0; |
| 1512 | 1506 |
| 1513 double mv_ratio_accumulator = 0.0; | 1507 double mv_ratio_accumulator = 0.0; |
| 1514 double decay_accumulator = 1.0; | 1508 double decay_accumulator = 1.0; |
| 1515 double zero_motion_accumulator = 1.0; | 1509 double zero_motion_accumulator = 1.0; |
| 1516 | 1510 |
| 1517 double loop_decay_rate = 1.00; | 1511 double loop_decay_rate = 1.00; |
| 1518 double last_loop_decay_rate = 1.00; | 1512 double last_loop_decay_rate = 1.00; |
| 1519 | 1513 |
| 1520 double this_frame_mv_in_out = 0.0; | 1514 double this_frame_mv_in_out = 0.0; |
| 1521 double mv_in_out_accumulator = 0.0; | 1515 double mv_in_out_accumulator = 0.0; |
| 1522 double abs_mv_in_out_accumulator = 0.0; | 1516 double abs_mv_in_out_accumulator = 0.0; |
| 1523 double mv_ratio_accumulator_thresh; | 1517 double mv_ratio_accumulator_thresh; |
| 1524 unsigned int allow_alt_ref = oxcf->play_alternate && oxcf->lag_in_frames; | 1518 unsigned int allow_alt_ref = is_altref_enabled(oxcf); |
| 1525 | 1519 |
| 1526 int f_boost = 0; | 1520 int f_boost = 0; |
| 1527 int b_boost = 0; | 1521 int b_boost = 0; |
| 1528 int flash_detected; | 1522 int flash_detected; |
| 1529 int active_max_gf_interval; | 1523 int active_max_gf_interval; |
| 1524 int64_t gf_group_bits; |
| 1525 double gf_group_error_left; |
| 1526 int gf_arf_bits; |
| 1527 |
| 1528 // Reset the GF group data structures unless this is a key |
| 1529 // frame in which case it will already have been done. |
| 1530 if (cpi->common.frame_type != KEY_FRAME) { |
| 1531 twopass->gf_group_index = 0; |
| 1532 vp9_zero(twopass->gf_group_bit_allocation); |
| 1533 } |
| 1530 | 1534 |
| 1531 vp9_clear_system_state(); | 1535 vp9_clear_system_state(); |
| 1532 vp9_zero(next_frame); | 1536 vp9_zero(next_frame); |
| 1533 | 1537 |
| 1534 twopass->gf_group_bits = 0; | 1538 gf_group_bits = 0; |
| 1535 start_pos = twopass->stats_in; | |
| 1536 | 1539 |
| 1537 // Load stats for the current frame. | 1540 // Load stats for the current frame. |
| 1538 mod_frame_err = calculate_modified_err(cpi, this_frame); | 1541 mod_frame_err = calculate_modified_err(twopass, oxcf, this_frame); |
| 1539 | 1542 |
| 1540 // Note the error of the frame at the start of the group. This will be | 1543 // Note the error of the frame at the start of the group. This will be |
| 1541 // the GF frame error if we code a normal gf. | 1544 // the GF frame error if we code a normal gf. |
| 1542 gf_first_frame_err = mod_frame_err; | 1545 gf_first_frame_err = mod_frame_err; |
| 1543 | 1546 |
| 1544 // If this is a key frame or the overlay from a previous arf then | 1547 // If this is a key frame or the overlay from a previous arf then |
| 1545 // the error score / cost of this frame has already been accounted for. | 1548 // the error score / cost of this frame has already been accounted for. |
| 1546 if (cpi->common.frame_type == KEY_FRAME || rc->source_alt_ref_active) | 1549 if (cpi->common.frame_type == KEY_FRAME || rc->source_alt_ref_active) |
| 1547 gf_group_err -= gf_first_frame_err; | 1550 gf_group_err -= gf_first_frame_err; |
| 1548 | 1551 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 1560 12 + ((int)vp9_convert_qindex_to_q(rc->last_q[INTER_FRAME]) >> 5); | 1563 12 + ((int)vp9_convert_qindex_to_q(rc->last_q[INTER_FRAME]) >> 5); |
| 1561 | 1564 |
| 1562 if (active_max_gf_interval > rc->max_gf_interval) | 1565 if (active_max_gf_interval > rc->max_gf_interval) |
| 1563 active_max_gf_interval = rc->max_gf_interval; | 1566 active_max_gf_interval = rc->max_gf_interval; |
| 1564 | 1567 |
| 1565 i = 0; | 1568 i = 0; |
| 1566 while (i < rc->static_scene_max_gf_interval && i < rc->frames_to_key) { | 1569 while (i < rc->static_scene_max_gf_interval && i < rc->frames_to_key) { |
| 1567 ++i; | 1570 ++i; |
| 1568 | 1571 |
| 1569 // Accumulate error score of frames in this gf group. | 1572 // Accumulate error score of frames in this gf group. |
| 1570 mod_frame_err = calculate_modified_err(cpi, this_frame); | 1573 mod_frame_err = calculate_modified_err(twopass, oxcf, this_frame); |
| 1571 gf_group_err += mod_frame_err; | 1574 gf_group_err += mod_frame_err; |
| 1572 | 1575 |
| 1573 if (EOF == input_stats(twopass, &next_frame)) | 1576 if (EOF == input_stats(twopass, &next_frame)) |
| 1574 break; | 1577 break; |
| 1575 | 1578 |
| 1576 // Test for the case where there is a brief flash but the prediction | 1579 // Test for the case where there is a brief flash but the prediction |
| 1577 // quality back to an earlier frame is then restored. | 1580 // quality back to an earlier frame is then restored. |
| 1578 flash_detected = detect_flash(twopass, 0); | 1581 flash_detected = detect_flash(twopass, 0); |
| 1579 | 1582 |
| 1580 // Update the motion related elements to the boost calculation. | 1583 // Update the motion related elements to the boost calculation. |
| (...skipping 18 matching lines...) Expand all Loading... |
| 1599 // Break clause to detect very still sections after motion. For example, | 1602 // Break clause to detect very still sections after motion. For example, |
| 1600 // a static image after a fade or other transition. | 1603 // a static image after a fade or other transition. |
| 1601 if (detect_transition_to_still(twopass, i, 5, loop_decay_rate, | 1604 if (detect_transition_to_still(twopass, i, 5, loop_decay_rate, |
| 1602 last_loop_decay_rate)) { | 1605 last_loop_decay_rate)) { |
| 1603 allow_alt_ref = 0; | 1606 allow_alt_ref = 0; |
| 1604 break; | 1607 break; |
| 1605 } | 1608 } |
| 1606 } | 1609 } |
| 1607 | 1610 |
| 1608 // Calculate a boost number for this frame. | 1611 // Calculate a boost number for this frame. |
| 1609 boost_score += (decay_accumulator * | 1612 boost_score += decay_accumulator * calc_frame_boost(twopass, &next_frame, |
| 1610 calc_frame_boost(cpi, &next_frame, this_frame_mv_in_out)); | 1613 this_frame_mv_in_out); |
| 1611 | 1614 |
| 1612 // Break out conditions. | 1615 // Break out conditions. |
| 1613 if ( | 1616 if ( |
| 1614 // Break at cpi->max_gf_interval unless almost totally static. | 1617 // Break at active_max_gf_interval unless almost totally static. |
| 1615 (i >= active_max_gf_interval && (zero_motion_accumulator < 0.995)) || | 1618 (i >= active_max_gf_interval && (zero_motion_accumulator < 0.995)) || |
| 1616 ( | 1619 ( |
| 1617 // Don't break out with a very short interval. | 1620 // Don't break out with a very short interval. |
| 1618 (i > MIN_GF_INTERVAL) && | 1621 (i > MIN_GF_INTERVAL) && |
| 1619 ((boost_score > 125.0) || (next_frame.pcnt_inter < 0.75)) && | 1622 ((boost_score > 125.0) || (next_frame.pcnt_inter < 0.75)) && |
| 1620 (!flash_detected) && | 1623 (!flash_detected) && |
| 1621 ((mv_ratio_accumulator > mv_ratio_accumulator_thresh) || | 1624 ((mv_ratio_accumulator > mv_ratio_accumulator_thresh) || |
| 1622 (abs_mv_in_out_accumulator > 3.0) || | 1625 (abs_mv_in_out_accumulator > 3.0) || |
| 1623 (mv_in_out_accumulator < -2.0) || | 1626 (mv_in_out_accumulator < -2.0) || |
| 1624 ((boost_score - old_boost_score) < IIFACTOR)))) { | 1627 ((boost_score - old_boost_score) < IIFACTOR)))) { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 1635 | 1638 |
| 1636 // Don't allow a gf too near the next kf. | 1639 // Don't allow a gf too near the next kf. |
| 1637 if ((rc->frames_to_key - i) < MIN_GF_INTERVAL) { | 1640 if ((rc->frames_to_key - i) < MIN_GF_INTERVAL) { |
| 1638 while (i < (rc->frames_to_key + !rc->next_key_frame_forced)) { | 1641 while (i < (rc->frames_to_key + !rc->next_key_frame_forced)) { |
| 1639 ++i; | 1642 ++i; |
| 1640 | 1643 |
| 1641 if (EOF == input_stats(twopass, this_frame)) | 1644 if (EOF == input_stats(twopass, this_frame)) |
| 1642 break; | 1645 break; |
| 1643 | 1646 |
| 1644 if (i < rc->frames_to_key) { | 1647 if (i < rc->frames_to_key) { |
| 1645 mod_frame_err = calculate_modified_err(cpi, this_frame); | 1648 mod_frame_err = calculate_modified_err(twopass, oxcf, this_frame); |
| 1646 gf_group_err += mod_frame_err; | 1649 gf_group_err += mod_frame_err; |
| 1647 } | 1650 } |
| 1648 } | 1651 } |
| 1649 } | 1652 } |
| 1650 | 1653 |
| 1651 #if CONFIG_MULTIPLE_ARF | 1654 #if CONFIG_MULTIPLE_ARF |
| 1652 if (cpi->multi_arf_enabled) { | 1655 if (cpi->multi_arf_enabled) { |
| 1653 // Initialize frame coding order variables. | 1656 // Initialize frame coding order variables. |
| 1654 cpi->new_frame_coding_order_period = 0; | 1657 cpi->new_frame_coding_order_period = 0; |
| 1655 cpi->next_frame_in_order = 0; | 1658 cpi->next_frame_in_order = 0; |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1731 printf("%4d ", cpi->arf_weight[i]); | 1734 printf("%4d ", cpi->arf_weight[i]); |
| 1732 } | 1735 } |
| 1733 printf("\n"); | 1736 printf("\n"); |
| 1734 } | 1737 } |
| 1735 #endif | 1738 #endif |
| 1736 #endif | 1739 #endif |
| 1737 // Reset the file position. | 1740 // Reset the file position. |
| 1738 reset_fpf_position(twopass, start_pos); | 1741 reset_fpf_position(twopass, start_pos); |
| 1739 | 1742 |
| 1740 // Calculate the bits to be allocated to the gf/arf group as a whole | 1743 // Calculate the bits to be allocated to the gf/arf group as a whole |
| 1741 twopass->gf_group_bits = calculate_total_gf_group_bits(cpi, gf_group_err); | 1744 gf_group_bits = calculate_total_gf_group_bits(cpi, gf_group_err); |
| 1742 | 1745 |
| 1743 // Calculate the extra bits to be used for boosted frame(s) | 1746 // Calculate the extra bits to be used for boosted frame(s) |
| 1744 { | 1747 { |
| 1745 int q = rc->last_q[INTER_FRAME]; | 1748 int q = rc->last_q[INTER_FRAME]; |
| 1746 int boost = (rc->gfu_boost * gfboost_qadjust(q)) / 100; | 1749 int boost = (rc->gfu_boost * gfboost_qadjust(q)) / 100; |
| 1747 | 1750 |
| 1748 // Set max and minimum boost and hence minimum allocation. | 1751 // Set max and minimum boost and hence minimum allocation. |
| 1749 boost = clamp(boost, 125, (rc->baseline_gf_interval + 1) * 200); | 1752 boost = clamp(boost, 125, (rc->baseline_gf_interval + 1) * 200); |
| 1750 | 1753 |
| 1751 // Calculate the extra bits to be used for boosted frame(s) | 1754 // Calculate the extra bits to be used for boosted frame(s) |
| 1752 twopass->gf_bits = calculate_boost_bits(rc->baseline_gf_interval, | 1755 gf_arf_bits = calculate_boost_bits(rc->baseline_gf_interval, |
| 1753 boost, twopass->gf_group_bits); | 1756 boost, gf_group_bits); |
| 1754 | |
| 1755 | |
| 1756 // For key frames the frame target rate is set already. | |
| 1757 // NOTE: We dont bother to check for the special case of ARF overlay | |
| 1758 // frames here, as there is clamping code for this in the function | |
| 1759 // vp9_rc_clamp_pframe_target_size(), which applies to one and two pass | |
| 1760 // encodes. | |
| 1761 if (cpi->common.frame_type != KEY_FRAME && | |
| 1762 !vp9_is_upper_layer_key_frame(cpi)) { | |
| 1763 vp9_rc_set_frame_target(cpi, twopass->gf_bits); | |
| 1764 } | |
| 1765 } | 1757 } |
| 1766 | 1758 |
| 1767 // Adjust KF group bits and error remaining. | 1759 // Adjust KF group bits and error remaining. |
| 1768 twopass->kf_group_error_left -= (int64_t)gf_group_err; | 1760 twopass->kf_group_error_left -= (int64_t)gf_group_err; |
| 1769 | 1761 |
| 1770 // If this is an arf update we want to remove the score for the overlay | 1762 // If this is an arf update we want to remove the score for the overlay |
| 1771 // frame at the end which will usually be very cheap to code. | 1763 // frame at the end which will usually be very cheap to code. |
| 1772 // The overlay frame has already, in effect, been coded so we want to spread | 1764 // The overlay frame has already, in effect, been coded so we want to spread |
| 1773 // the remaining bits among the other frames. | 1765 // the remaining bits among the other frames. |
| 1774 // For normal GFs remove the score for the GF itself unless this is | 1766 // For normal GFs remove the score for the GF itself unless this is |
| 1775 // also a key frame in which case it has already been accounted for. | 1767 // also a key frame in which case it has already been accounted for. |
| 1776 if (rc->source_alt_ref_pending) { | 1768 if (rc->source_alt_ref_pending) { |
| 1777 twopass->gf_group_error_left = (int64_t)(gf_group_err - mod_frame_err); | 1769 gf_group_error_left = gf_group_err - mod_frame_err; |
| 1778 } else if (cpi->common.frame_type != KEY_FRAME) { | 1770 } else if (cpi->common.frame_type != KEY_FRAME) { |
| 1779 twopass->gf_group_error_left = (int64_t)(gf_group_err | 1771 gf_group_error_left = gf_group_err - gf_first_frame_err; |
| 1780 - gf_first_frame_err); | |
| 1781 } else { | 1772 } else { |
| 1782 twopass->gf_group_error_left = (int64_t)gf_group_err; | 1773 gf_group_error_left = gf_group_err; |
| 1783 } | 1774 } |
| 1784 | 1775 |
| 1776 // Allocate bits to each of the frames in the GF group. |
| 1777 allocate_gf_group_bits(cpi, gf_group_bits, gf_group_error_left, gf_arf_bits); |
| 1778 |
| 1779 // Reset the file position. |
| 1780 reset_fpf_position(twopass, start_pos); |
| 1781 |
| 1785 // Calculate a section intra ratio used in setting max loop filter. | 1782 // Calculate a section intra ratio used in setting max loop filter. |
| 1786 if (cpi->common.frame_type != KEY_FRAME) { | 1783 if (cpi->common.frame_type != KEY_FRAME) { |
| 1787 calculate_section_intra_ratio(twopass, start_pos, rc->baseline_gf_interval); | 1784 twopass->section_intra_rating = |
| 1785 calculate_section_intra_ratio(start_pos, twopass->stats_in_end, |
| 1786 rc->baseline_gf_interval); |
| 1788 } | 1787 } |
| 1789 } | 1788 } |
| 1790 | 1789 |
| 1791 // Allocate bits to a normal frame that is neither a gf an arf or a key frame. | 1790 static int test_candidate_kf(TWO_PASS *twopass, |
| 1792 static void assign_std_frame_bits(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame) { | |
| 1793 struct twopass_rc *twopass = &cpi->twopass; | |
| 1794 // For a single frame. | |
| 1795 const int max_bits = frame_max_bits(&cpi->rc, &cpi->oxcf); | |
| 1796 // Calculate modified prediction error used in bit allocation. | |
| 1797 const double modified_err = calculate_modified_err(cpi, this_frame); | |
| 1798 int target_frame_size; | |
| 1799 double err_fraction; | |
| 1800 | |
| 1801 if (twopass->gf_group_error_left > 0) | |
| 1802 // What portion of the remaining GF group error is used by this frame. | |
| 1803 err_fraction = modified_err / twopass->gf_group_error_left; | |
| 1804 else | |
| 1805 err_fraction = 0.0; | |
| 1806 | |
| 1807 // How many of those bits available for allocation should we give it? | |
| 1808 target_frame_size = (int)((double)twopass->gf_group_bits * err_fraction); | |
| 1809 | |
| 1810 // Clip target size to 0 - max_bits (or cpi->twopass.gf_group_bits) at | |
| 1811 // the top end. | |
| 1812 target_frame_size = clamp(target_frame_size, 0, | |
| 1813 MIN(max_bits, (int)twopass->gf_group_bits)); | |
| 1814 | |
| 1815 // Adjust error and bits remaining. | |
| 1816 twopass->gf_group_error_left -= (int64_t)modified_err; | |
| 1817 | |
| 1818 // Per frame bit target for this frame. | |
| 1819 vp9_rc_set_frame_target(cpi, target_frame_size); | |
| 1820 } | |
| 1821 | |
| 1822 static int test_candidate_kf(struct twopass_rc *twopass, | |
| 1823 const FIRSTPASS_STATS *last_frame, | 1791 const FIRSTPASS_STATS *last_frame, |
| 1824 const FIRSTPASS_STATS *this_frame, | 1792 const FIRSTPASS_STATS *this_frame, |
| 1825 const FIRSTPASS_STATS *next_frame) { | 1793 const FIRSTPASS_STATS *next_frame) { |
| 1826 int is_viable_kf = 0; | 1794 int is_viable_kf = 0; |
| 1827 | 1795 |
| 1828 // Does the frame satisfy the primary criteria of a key frame? | 1796 // Does the frame satisfy the primary criteria of a key frame? |
| 1829 // If so, then examine how well it predicts subsequent frames. | 1797 // If so, then examine how well it predicts subsequent frames. |
| 1830 if ((this_frame->pcnt_second_ref < 0.10) && | 1798 if ((this_frame->pcnt_second_ref < 0.10) && |
| 1831 (next_frame->pcnt_second_ref < 0.10) && | 1799 (next_frame->pcnt_second_ref < 0.10) && |
| 1832 ((this_frame->pcnt_inter < 0.05) || | 1800 ((this_frame->pcnt_inter < 0.05) || |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1892 is_viable_kf = 0; | 1860 is_viable_kf = 0; |
| 1893 } | 1861 } |
| 1894 } | 1862 } |
| 1895 | 1863 |
| 1896 return is_viable_kf; | 1864 return is_viable_kf; |
| 1897 } | 1865 } |
| 1898 | 1866 |
| 1899 static void find_next_key_frame(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame) { | 1867 static void find_next_key_frame(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame) { |
| 1900 int i, j; | 1868 int i, j; |
| 1901 RATE_CONTROL *const rc = &cpi->rc; | 1869 RATE_CONTROL *const rc = &cpi->rc; |
| 1902 struct twopass_rc *const twopass = &cpi->twopass; | 1870 TWO_PASS *const twopass = &cpi->twopass; |
| 1871 const VP9EncoderConfig *const oxcf = &cpi->oxcf; |
| 1903 const FIRSTPASS_STATS first_frame = *this_frame; | 1872 const FIRSTPASS_STATS first_frame = *this_frame; |
| 1904 const FIRSTPASS_STATS *start_position = twopass->stats_in; | 1873 const FIRSTPASS_STATS *const start_position = twopass->stats_in; |
| 1905 FIRSTPASS_STATS next_frame; | 1874 FIRSTPASS_STATS next_frame; |
| 1906 FIRSTPASS_STATS last_frame; | 1875 FIRSTPASS_STATS last_frame; |
| 1876 int kf_bits = 0; |
| 1907 double decay_accumulator = 1.0; | 1877 double decay_accumulator = 1.0; |
| 1908 double zero_motion_accumulator = 1.0; | 1878 double zero_motion_accumulator = 1.0; |
| 1909 double boost_score = 0.0; | 1879 double boost_score = 0.0; |
| 1910 double kf_mod_err = 0.0; | 1880 double kf_mod_err = 0.0; |
| 1911 double kf_group_err = 0.0; | 1881 double kf_group_err = 0.0; |
| 1912 double recent_loop_decay[8] = {1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0}; | 1882 double recent_loop_decay[8] = {1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0}; |
| 1913 | 1883 |
| 1914 vp9_zero(next_frame); | 1884 vp9_zero(next_frame); |
| 1915 | 1885 |
| 1916 cpi->common.frame_type = KEY_FRAME; | 1886 cpi->common.frame_type = KEY_FRAME; |
| 1917 | 1887 |
| 1888 // Reset the GF group data structures. |
| 1889 twopass->gf_group_index = 0; |
| 1890 vp9_zero(twopass->gf_group_bit_allocation); |
| 1891 |
| 1918 // Is this a forced key frame by interval. | 1892 // Is this a forced key frame by interval. |
| 1919 rc->this_key_frame_forced = rc->next_key_frame_forced; | 1893 rc->this_key_frame_forced = rc->next_key_frame_forced; |
| 1920 | 1894 |
| 1921 // Clear the alt ref active flag as this can never be active on a key frame. | 1895 // Clear the alt ref active flag as this can never be active on a key frame. |
| 1922 rc->source_alt_ref_active = 0; | 1896 rc->source_alt_ref_active = 0; |
| 1923 | 1897 |
| 1924 // KF is always a GF so clear frames till next gf counter. | 1898 // KF is always a GF so clear frames till next gf counter. |
| 1925 rc->frames_till_gf_update_due = 0; | 1899 rc->frames_till_gf_update_due = 0; |
| 1926 | 1900 |
| 1927 rc->frames_to_key = 1; | 1901 rc->frames_to_key = 1; |
| 1928 | 1902 |
| 1929 twopass->kf_group_bits = 0; // Total bits available to kf group | 1903 twopass->kf_group_bits = 0; // Total bits available to kf group |
| 1930 twopass->kf_group_error_left = 0; // Group modified error score. | 1904 twopass->kf_group_error_left = 0; // Group modified error score. |
| 1931 | 1905 |
| 1932 kf_mod_err = calculate_modified_err(cpi, this_frame); | 1906 kf_mod_err = calculate_modified_err(twopass, oxcf, this_frame); |
| 1933 | 1907 |
| 1934 // Find the next keyframe. | 1908 // Find the next keyframe. |
| 1935 i = 0; | 1909 i = 0; |
| 1936 while (twopass->stats_in < twopass->stats_in_end && | 1910 while (twopass->stats_in < twopass->stats_in_end && |
| 1937 rc->frames_to_key < cpi->oxcf.key_freq) { | 1911 rc->frames_to_key < cpi->oxcf.key_freq) { |
| 1938 // Accumulate kf group error. | 1912 // Accumulate kf group error. |
| 1939 kf_group_err += calculate_modified_err(cpi, this_frame); | 1913 kf_group_err += calculate_modified_err(twopass, oxcf, this_frame); |
| 1940 | 1914 |
| 1941 // Load the next frame's stats. | 1915 // Load the next frame's stats. |
| 1942 last_frame = *this_frame; | 1916 last_frame = *this_frame; |
| 1943 input_stats(twopass, this_frame); | 1917 input_stats(twopass, this_frame); |
| 1944 | 1918 |
| 1945 // Provided that we are not at the end of the file... | 1919 // Provided that we are not at the end of the file... |
| 1946 if (cpi->oxcf.auto_key && | 1920 if (cpi->oxcf.auto_key && |
| 1947 lookup_next_frame_stats(twopass, &next_frame) != EOF) { | 1921 lookup_next_frame_stats(twopass, &next_frame) != EOF) { |
| 1948 double loop_decay_rate; | 1922 double loop_decay_rate; |
| 1949 | 1923 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1991 | 1965 |
| 1992 rc->frames_to_key /= 2; | 1966 rc->frames_to_key /= 2; |
| 1993 | 1967 |
| 1994 // Reset to the start of the group. | 1968 // Reset to the start of the group. |
| 1995 reset_fpf_position(twopass, start_position); | 1969 reset_fpf_position(twopass, start_position); |
| 1996 | 1970 |
| 1997 kf_group_err = 0; | 1971 kf_group_err = 0; |
| 1998 | 1972 |
| 1999 // Rescan to get the correct error data for the forced kf group. | 1973 // Rescan to get the correct error data for the forced kf group. |
| 2000 for (i = 0; i < rc->frames_to_key; ++i) { | 1974 for (i = 0; i < rc->frames_to_key; ++i) { |
| 2001 kf_group_err += calculate_modified_err(cpi, &tmp_frame); | 1975 kf_group_err += calculate_modified_err(twopass, oxcf, &tmp_frame); |
| 2002 input_stats(twopass, &tmp_frame); | 1976 input_stats(twopass, &tmp_frame); |
| 2003 } | 1977 } |
| 2004 rc->next_key_frame_forced = 1; | 1978 rc->next_key_frame_forced = 1; |
| 2005 } else if (twopass->stats_in == twopass->stats_in_end || | 1979 } else if (twopass->stats_in == twopass->stats_in_end || |
| 2006 rc->frames_to_key >= cpi->oxcf.key_freq) { | 1980 rc->frames_to_key >= cpi->oxcf.key_freq) { |
| 2007 rc->next_key_frame_forced = 1; | 1981 rc->next_key_frame_forced = 1; |
| 2008 } else { | 1982 } else { |
| 2009 rc->next_key_frame_forced = 0; | 1983 rc->next_key_frame_forced = 0; |
| 2010 } | 1984 } |
| 2011 | 1985 |
| 2012 // Special case for the last key frame of the file. | 1986 // Special case for the last key frame of the file. |
| 2013 if (twopass->stats_in >= twopass->stats_in_end) { | 1987 if (twopass->stats_in >= twopass->stats_in_end) { |
| 2014 // Accumulate kf group error. | 1988 // Accumulate kf group error. |
| 2015 kf_group_err += calculate_modified_err(cpi, this_frame); | 1989 kf_group_err += calculate_modified_err(twopass, oxcf, this_frame); |
| 2016 } | 1990 } |
| 2017 | 1991 |
| 2018 // Calculate the number of bits that should be assigned to the kf group. | 1992 // Calculate the number of bits that should be assigned to the kf group. |
| 2019 if (twopass->bits_left > 0 && twopass->modified_error_left > 0.0) { | 1993 if (twopass->bits_left > 0 && twopass->modified_error_left > 0.0) { |
| 2020 // Maximum number of bits for a single normal frame (not key frame). | 1994 // Maximum number of bits for a single normal frame (not key frame). |
| 2021 const int max_bits = frame_max_bits(rc, &cpi->oxcf); | 1995 const int max_bits = frame_max_bits(rc, &cpi->oxcf); |
| 2022 | 1996 |
| 2023 // Maximum number of bits allocated to the key frame group. | 1997 // Maximum number of bits allocated to the key frame group. |
| 2024 int64_t max_grp_bits; | 1998 int64_t max_grp_bits; |
| 2025 | 1999 |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2073 const double loop_decay_rate = get_prediction_decay_rate(&cpi->common, | 2047 const double loop_decay_rate = get_prediction_decay_rate(&cpi->common, |
| 2074 &next_frame); | 2048 &next_frame); |
| 2075 decay_accumulator *= loop_decay_rate; | 2049 decay_accumulator *= loop_decay_rate; |
| 2076 decay_accumulator = MAX(decay_accumulator, MIN_DECAY_FACTOR); | 2050 decay_accumulator = MAX(decay_accumulator, MIN_DECAY_FACTOR); |
| 2077 } | 2051 } |
| 2078 | 2052 |
| 2079 boost_score += (decay_accumulator * r); | 2053 boost_score += (decay_accumulator * r); |
| 2080 } | 2054 } |
| 2081 } | 2055 } |
| 2082 | 2056 |
| 2057 reset_fpf_position(twopass, start_position); |
| 2058 |
| 2083 // Store the zero motion percentage | 2059 // Store the zero motion percentage |
| 2084 twopass->kf_zeromotion_pct = (int)(zero_motion_accumulator * 100.0); | 2060 twopass->kf_zeromotion_pct = (int)(zero_motion_accumulator * 100.0); |
| 2085 | 2061 |
| 2086 // Calculate a section intra ratio used in setting max loop filter. | 2062 // Calculate a section intra ratio used in setting max loop filter. |
| 2087 calculate_section_intra_ratio(twopass, start_position, rc->frames_to_key); | 2063 twopass->section_intra_rating = |
| 2064 calculate_section_intra_ratio(start_position, twopass->stats_in_end, |
| 2065 rc->frames_to_key); |
| 2088 | 2066 |
| 2089 // Work out how many bits to allocate for the key frame itself. | 2067 // Work out how many bits to allocate for the key frame itself. |
| 2090 rc->kf_boost = (int)boost_score; | 2068 rc->kf_boost = (int)boost_score; |
| 2091 | 2069 |
| 2092 if (rc->kf_boost < (rc->frames_to_key * 3)) | 2070 if (rc->kf_boost < (rc->frames_to_key * 3)) |
| 2093 rc->kf_boost = (rc->frames_to_key * 3); | 2071 rc->kf_boost = (rc->frames_to_key * 3); |
| 2094 if (rc->kf_boost < MIN_KF_BOOST) | 2072 if (rc->kf_boost < MIN_KF_BOOST) |
| 2095 rc->kf_boost = MIN_KF_BOOST; | 2073 rc->kf_boost = MIN_KF_BOOST; |
| 2096 | 2074 |
| 2097 twopass->kf_bits = calculate_boost_bits((rc->frames_to_key - 1), | 2075 kf_bits = calculate_boost_bits((rc->frames_to_key - 1), |
| 2098 rc->kf_boost, twopass->kf_group_bits); | 2076 rc->kf_boost, twopass->kf_group_bits); |
| 2099 | 2077 |
| 2100 twopass->kf_group_bits -= twopass->kf_bits; | 2078 twopass->kf_group_bits -= kf_bits; |
| 2101 | 2079 |
| 2102 // Per frame bit target for this frame. | 2080 // Save the bits to spend on the key frame. |
| 2103 vp9_rc_set_frame_target(cpi, twopass->kf_bits); | 2081 twopass->gf_group_bit_allocation[0] = kf_bits; |
| 2104 | 2082 |
| 2105 // Note the total error score of the kf group minus the key frame itself. | 2083 // Note the total error score of the kf group minus the key frame itself. |
| 2106 twopass->kf_group_error_left = (int)(kf_group_err - kf_mod_err); | 2084 twopass->kf_group_error_left = (int)(kf_group_err - kf_mod_err); |
| 2107 | 2085 |
| 2108 // Adjust the count of total modified error left. | 2086 // Adjust the count of total modified error left. |
| 2109 // The count of bits left is adjusted elsewhere based on real coded frame | 2087 // The count of bits left is adjusted elsewhere based on real coded frame |
| 2110 // sizes. | 2088 // sizes. |
| 2111 twopass->modified_error_left -= kf_group_err; | 2089 twopass->modified_error_left -= kf_group_err; |
| 2112 } | 2090 } |
| 2113 | 2091 |
| 2114 void vp9_rc_get_first_pass_params(VP9_COMP *cpi) { | |
| 2115 VP9_COMMON *const cm = &cpi->common; | |
| 2116 if (!cpi->refresh_alt_ref_frame && | |
| 2117 (cm->current_video_frame == 0 || | |
| 2118 (cpi->frame_flags & FRAMEFLAGS_KEY))) { | |
| 2119 cm->frame_type = KEY_FRAME; | |
| 2120 } else { | |
| 2121 cm->frame_type = INTER_FRAME; | |
| 2122 } | |
| 2123 // Do not use periodic key frames. | |
| 2124 cpi->rc.frames_to_key = INT_MAX; | |
| 2125 } | |
| 2126 | |
| 2127 // For VBR...adjustment to the frame target based on error from previous frames | 2092 // For VBR...adjustment to the frame target based on error from previous frames |
| 2128 void vbr_rate_correction(int * this_frame_target, | 2093 void vbr_rate_correction(int * this_frame_target, |
| 2129 const int64_t vbr_bits_off_target) { | 2094 const int64_t vbr_bits_off_target) { |
| 2130 int max_delta = (*this_frame_target * 15) / 100; | 2095 int max_delta = (*this_frame_target * 15) / 100; |
| 2131 | 2096 |
| 2132 // vbr_bits_off_target > 0 means we have extra bits to spend | 2097 // vbr_bits_off_target > 0 means we have extra bits to spend |
| 2133 if (vbr_bits_off_target > 0) { | 2098 if (vbr_bits_off_target > 0) { |
| 2134 *this_frame_target += | 2099 *this_frame_target += |
| 2135 (vbr_bits_off_target > max_delta) ? max_delta | 2100 (vbr_bits_off_target > max_delta) ? max_delta |
| 2136 : (int)vbr_bits_off_target; | 2101 : (int)vbr_bits_off_target; |
| 2137 } else { | 2102 } else { |
| 2138 *this_frame_target -= | 2103 *this_frame_target -= |
| 2139 (vbr_bits_off_target < -max_delta) ? max_delta | 2104 (vbr_bits_off_target < -max_delta) ? max_delta |
| 2140 : (int)-vbr_bits_off_target; | 2105 : (int)-vbr_bits_off_target; |
| 2141 } | 2106 } |
| 2142 } | 2107 } |
| 2143 | 2108 |
| 2144 void vp9_rc_get_second_pass_params(VP9_COMP *cpi) { | 2109 void vp9_rc_get_second_pass_params(VP9_COMP *cpi) { |
| 2145 VP9_COMMON *const cm = &cpi->common; | 2110 VP9_COMMON *const cm = &cpi->common; |
| 2146 RATE_CONTROL *const rc = &cpi->rc; | 2111 RATE_CONTROL *const rc = &cpi->rc; |
| 2147 struct twopass_rc *const twopass = &cpi->twopass; | 2112 TWO_PASS *const twopass = &cpi->twopass; |
| 2148 int frames_left; | 2113 int frames_left; |
| 2149 FIRSTPASS_STATS this_frame; | 2114 FIRSTPASS_STATS this_frame; |
| 2150 FIRSTPASS_STATS this_frame_copy; | 2115 FIRSTPASS_STATS this_frame_copy; |
| 2151 | 2116 |
| 2152 double this_frame_intra_error; | 2117 int target_rate; |
| 2153 double this_frame_coded_error; | |
| 2154 int target; | |
| 2155 LAYER_CONTEXT *lc = NULL; | 2118 LAYER_CONTEXT *lc = NULL; |
| 2156 const int is_spatial_svc = (cpi->use_svc && | 2119 const int is_spatial_svc = (cpi->use_svc && |
| 2157 cpi->svc.number_temporal_layers == 1); | 2120 cpi->svc.number_temporal_layers == 1); |
| 2158 if (is_spatial_svc) { | 2121 if (is_spatial_svc) { |
| 2159 lc = &cpi->svc.layer_context[cpi->svc.spatial_layer_id]; | 2122 lc = &cpi->svc.layer_context[cpi->svc.spatial_layer_id]; |
| 2160 frames_left = (int)(twopass->total_stats.count - | 2123 frames_left = (int)(twopass->total_stats.count - |
| 2161 lc->current_video_frame_in_layer); | 2124 lc->current_video_frame_in_layer); |
| 2162 } else { | 2125 } else { |
| 2163 frames_left = (int)(twopass->total_stats.count - | 2126 frames_left = (int)(twopass->total_stats.count - |
| 2164 cm->current_video_frame); | 2127 cm->current_video_frame); |
| 2165 } | 2128 } |
| 2166 | 2129 |
| 2167 if (!twopass->stats_in) | 2130 if (!twopass->stats_in) |
| 2168 return; | 2131 return; |
| 2169 | 2132 |
| 2133 // Increment the gf group index. |
| 2134 ++twopass->gf_group_index; |
| 2135 |
| 2136 // If this is an arf frame then we dont want to read the stats file or |
| 2137 // advance the input pointer as we already have what we need. |
| 2170 if (cpi->refresh_alt_ref_frame) { | 2138 if (cpi->refresh_alt_ref_frame) { |
| 2171 int modified_target = twopass->gf_bits; | 2139 int target_rate; |
| 2172 rc->base_frame_target = twopass->gf_bits; | 2140 target_rate = twopass->gf_group_bit_allocation[twopass->gf_group_index]; |
| 2173 cm->frame_type = INTER_FRAME; | 2141 target_rate = vp9_rc_clamp_pframe_target_size(cpi, target_rate); |
| 2142 rc->base_frame_target = target_rate; |
| 2174 #ifdef LONG_TERM_VBR_CORRECTION | 2143 #ifdef LONG_TERM_VBR_CORRECTION |
| 2175 // Correction to rate target based on prior over or under shoot. | 2144 // Correction to rate target based on prior over or under shoot. |
| 2176 if (cpi->oxcf.rc_mode == RC_MODE_VBR) | 2145 if (cpi->oxcf.rc_mode == VPX_VBR) |
| 2177 vbr_rate_correction(&modified_target, rc->vbr_bits_off_target); | 2146 vbr_rate_correction(&target_rate, rc->vbr_bits_off_target); |
| 2178 #endif | 2147 #endif |
| 2179 vp9_rc_set_frame_target(cpi, modified_target); | 2148 vp9_rc_set_frame_target(cpi, target_rate); |
| 2149 cm->frame_type = INTER_FRAME; |
| 2180 return; | 2150 return; |
| 2181 } | 2151 } |
| 2182 | 2152 |
| 2183 vp9_clear_system_state(); | 2153 vp9_clear_system_state(); |
| 2184 | 2154 |
| 2185 if (is_spatial_svc && twopass->kf_intra_err_min == 0) { | 2155 if (is_spatial_svc && twopass->kf_intra_err_min == 0) { |
| 2186 twopass->kf_intra_err_min = KF_MB_INTRA_MIN * cpi->common.MBs; | 2156 twopass->kf_intra_err_min = KF_MB_INTRA_MIN * cpi->common.MBs; |
| 2187 twopass->gf_intra_err_min = GF_MB_INTRA_MIN * cpi->common.MBs; | 2157 twopass->gf_intra_err_min = GF_MB_INTRA_MIN * cpi->common.MBs; |
| 2188 } | 2158 } |
| 2189 | 2159 |
| 2190 if (cpi->oxcf.rc_mode == RC_MODE_CONSTANT_QUALITY) { | 2160 if (cpi->oxcf.rc_mode == VPX_Q) { |
| 2191 twopass->active_worst_quality = cpi->oxcf.cq_level; | 2161 twopass->active_worst_quality = cpi->oxcf.cq_level; |
| 2192 } else if (cm->current_video_frame == 0 || | 2162 } else if (cm->current_video_frame == 0 || |
| 2193 (is_spatial_svc && lc->current_video_frame_in_layer == 0)) { | 2163 (is_spatial_svc && lc->current_video_frame_in_layer == 0)) { |
| 2194 // Special case code for first frame. | 2164 // Special case code for first frame. |
| 2195 const int section_target_bandwidth = (int)(twopass->bits_left / | 2165 const int section_target_bandwidth = (int)(twopass->bits_left / |
| 2196 frames_left); | 2166 frames_left); |
| 2197 const int tmp_q = get_twopass_worst_quality(cpi, &twopass->total_left_stats, | 2167 const int tmp_q = get_twopass_worst_quality(cpi, &twopass->total_left_stats, |
| 2198 section_target_bandwidth); | 2168 section_target_bandwidth); |
| 2199 twopass->active_worst_quality = tmp_q; | 2169 twopass->active_worst_quality = tmp_q; |
| 2200 rc->ni_av_qi = tmp_q; | 2170 rc->ni_av_qi = tmp_q; |
| 2201 rc->avg_q = vp9_convert_qindex_to_q(tmp_q); | 2171 rc->avg_q = vp9_convert_qindex_to_q(tmp_q); |
| 2202 } | 2172 } |
| 2203 vp9_zero(this_frame); | 2173 vp9_zero(this_frame); |
| 2204 if (EOF == input_stats(twopass, &this_frame)) | 2174 if (EOF == input_stats(twopass, &this_frame)) |
| 2205 return; | 2175 return; |
| 2206 | 2176 |
| 2207 this_frame_intra_error = this_frame.intra_error; | 2177 // Local copy of the current frame's first pass stats. |
| 2208 this_frame_coded_error = this_frame.coded_error; | 2178 this_frame_copy = this_frame; |
| 2209 | 2179 |
| 2210 // Keyframe and section processing. | 2180 // Keyframe and section processing. |
| 2211 if (rc->frames_to_key == 0 || | 2181 if (rc->frames_to_key == 0 || |
| 2212 (cpi->frame_flags & FRAMEFLAGS_KEY)) { | 2182 (cpi->frame_flags & FRAMEFLAGS_KEY)) { |
| 2213 // Define next KF group and assign bits to it. | 2183 // Define next KF group and assign bits to it. |
| 2214 this_frame_copy = this_frame; | |
| 2215 find_next_key_frame(cpi, &this_frame_copy); | 2184 find_next_key_frame(cpi, &this_frame_copy); |
| 2216 // Don't place key frame in any enhancement layers in spatial svc | |
| 2217 if (is_spatial_svc) { | |
| 2218 lc->is_key_frame = 1; | |
| 2219 if (cpi->svc.spatial_layer_id > 0) { | |
| 2220 cm->frame_type = INTER_FRAME; | |
| 2221 } | |
| 2222 } | |
| 2223 } else { | 2185 } else { |
| 2224 if (is_spatial_svc) { | |
| 2225 lc->is_key_frame = 0; | |
| 2226 } | |
| 2227 cm->frame_type = INTER_FRAME; | 2186 cm->frame_type = INTER_FRAME; |
| 2228 } | 2187 } |
| 2229 | 2188 |
| 2230 // Is this frame a GF / ARF? (Note: a key frame is always also a GF). | 2189 if (is_spatial_svc) { |
| 2190 if (cpi->svc.spatial_layer_id == 0) { |
| 2191 lc->is_key_frame = (cm->frame_type == KEY_FRAME); |
| 2192 } else { |
| 2193 cm->frame_type = INTER_FRAME; |
| 2194 lc->is_key_frame = cpi->svc.layer_context[0].is_key_frame; |
| 2195 |
| 2196 if (lc->is_key_frame) { |
| 2197 cpi->ref_frame_flags &= (~VP9_LAST_FLAG); |
| 2198 } |
| 2199 } |
| 2200 } |
| 2201 |
| 2202 // Define a new GF/ARF group. (Should always enter here for key frames). |
| 2231 if (rc->frames_till_gf_update_due == 0) { | 2203 if (rc->frames_till_gf_update_due == 0) { |
| 2232 // Define next gf group and assign bits to it. | |
| 2233 this_frame_copy = this_frame; | |
| 2234 | |
| 2235 #if CONFIG_MULTIPLE_ARF | 2204 #if CONFIG_MULTIPLE_ARF |
| 2236 if (cpi->multi_arf_enabled) { | 2205 if (cpi->multi_arf_enabled) { |
| 2237 define_fixed_arf_period(cpi); | 2206 define_fixed_arf_period(cpi); |
| 2238 } else { | 2207 } else { |
| 2239 #endif | 2208 #endif |
| 2240 define_gf_group(cpi, &this_frame_copy); | 2209 define_gf_group(cpi, &this_frame_copy); |
| 2241 #if CONFIG_MULTIPLE_ARF | 2210 #if CONFIG_MULTIPLE_ARF |
| 2242 } | 2211 } |
| 2243 #endif | 2212 #endif |
| 2244 | 2213 |
| 2245 if (twopass->gf_zeromotion_pct > 995) { | 2214 if (twopass->gf_zeromotion_pct > 995) { |
| 2246 // As long as max_thresh for encode breakout is small enough, it is ok | 2215 // As long as max_thresh for encode breakout is small enough, it is ok |
| 2247 // to enable it for show frame, i.e. set allow_encode_breakout to | 2216 // to enable it for show frame, i.e. set allow_encode_breakout to |
| 2248 // ENCODE_BREAKOUT_LIMITED. | 2217 // ENCODE_BREAKOUT_LIMITED. |
| 2249 if (!cm->show_frame) | 2218 if (!cm->show_frame) |
| 2250 cpi->allow_encode_breakout = ENCODE_BREAKOUT_DISABLED; | 2219 cpi->allow_encode_breakout = ENCODE_BREAKOUT_DISABLED; |
| 2251 else | 2220 else |
| 2252 cpi->allow_encode_breakout = ENCODE_BREAKOUT_LIMITED; | 2221 cpi->allow_encode_breakout = ENCODE_BREAKOUT_LIMITED; |
| 2253 } | 2222 } |
| 2254 | 2223 |
| 2255 rc->frames_till_gf_update_due = rc->baseline_gf_interval; | 2224 rc->frames_till_gf_update_due = rc->baseline_gf_interval; |
| 2256 cpi->refresh_golden_frame = 1; | 2225 cpi->refresh_golden_frame = 1; |
| 2257 } else { | |
| 2258 // Otherwise this is an ordinary frame. | |
| 2259 // Assign bits from those allocated to the GF group. | |
| 2260 this_frame_copy = this_frame; | |
| 2261 assign_std_frame_bits(cpi, &this_frame_copy); | |
| 2262 } | 2226 } |
| 2263 | 2227 |
| 2264 // Keep a globally available copy of this and the next frame's iiratio. | |
| 2265 twopass->this_iiratio = (int)(this_frame_intra_error / | |
| 2266 DOUBLE_DIVIDE_CHECK(this_frame_coded_error)); | |
| 2267 { | 2228 { |
| 2268 FIRSTPASS_STATS next_frame; | 2229 FIRSTPASS_STATS next_frame; |
| 2269 if (lookup_next_frame_stats(twopass, &next_frame) != EOF) { | 2230 if (lookup_next_frame_stats(twopass, &next_frame) != EOF) { |
| 2270 twopass->next_iiratio = (int)(next_frame.intra_error / | 2231 twopass->next_iiratio = (int)(next_frame.intra_error / |
| 2271 DOUBLE_DIVIDE_CHECK(next_frame.coded_error)); | 2232 DOUBLE_DIVIDE_CHECK(next_frame.coded_error)); |
| 2272 } | 2233 } |
| 2273 } | 2234 } |
| 2274 | 2235 |
| 2236 target_rate = twopass->gf_group_bit_allocation[twopass->gf_group_index]; |
| 2275 if (cpi->common.frame_type == KEY_FRAME) | 2237 if (cpi->common.frame_type == KEY_FRAME) |
| 2276 target = vp9_rc_clamp_iframe_target_size(cpi, rc->this_frame_target); | 2238 target_rate = vp9_rc_clamp_iframe_target_size(cpi, target_rate); |
| 2277 else | 2239 else |
| 2278 target = vp9_rc_clamp_pframe_target_size(cpi, rc->this_frame_target); | 2240 target_rate = vp9_rc_clamp_pframe_target_size(cpi, target_rate); |
| 2279 | 2241 |
| 2280 rc->base_frame_target = target; | 2242 rc->base_frame_target = target_rate; |
| 2281 #ifdef LONG_TERM_VBR_CORRECTION | 2243 #ifdef LONG_TERM_VBR_CORRECTION |
| 2282 // Correction to rate target based on prior over or under shoot. | 2244 // Correction to rate target based on prior over or under shoot. |
| 2283 if (cpi->oxcf.rc_mode == RC_MODE_VBR) | 2245 if (cpi->oxcf.rc_mode == VPX_VBR) |
| 2284 vbr_rate_correction(&target, rc->vbr_bits_off_target); | 2246 vbr_rate_correction(&target_rate, rc->vbr_bits_off_target); |
| 2285 #endif | 2247 #endif |
| 2286 vp9_rc_set_frame_target(cpi, target); | 2248 vp9_rc_set_frame_target(cpi, target_rate); |
| 2287 | 2249 |
| 2288 // Update the total stats remaining structure. | 2250 // Update the total stats remaining structure. |
| 2289 subtract_stats(&twopass->total_left_stats, &this_frame); | 2251 subtract_stats(&twopass->total_left_stats, &this_frame); |
| 2290 } | 2252 } |
| 2291 | 2253 |
| 2292 void vp9_twopass_postencode_update(VP9_COMP *cpi) { | 2254 void vp9_twopass_postencode_update(VP9_COMP *cpi) { |
| 2255 TWO_PASS *const twopass = &cpi->twopass; |
| 2293 RATE_CONTROL *const rc = &cpi->rc; | 2256 RATE_CONTROL *const rc = &cpi->rc; |
| 2294 #ifdef LONG_TERM_VBR_CORRECTION | 2257 #ifdef LONG_TERM_VBR_CORRECTION |
| 2295 // In this experimental mode, the VBR correction is done exclusively through | 2258 // In this experimental mode, the VBR correction is done exclusively through |
| 2296 // rc->vbr_bits_off_target. Based on the sign of this value, a limited % | 2259 // rc->vbr_bits_off_target. Based on the sign of this value, a limited % |
| 2297 // adjustment is made to the target rate of subsequent frames, to try and | 2260 // adjustment is made to the target rate of subsequent frames, to try and |
| 2298 // push it back towards 0. This mode is less likely to suffer from | 2261 // push it back towards 0. This mode is less likely to suffer from |
| 2299 // extreme behaviour at the end of a clip or group of frames. | 2262 // extreme behaviour at the end of a clip or group of frames. |
| 2300 const int bits_used = rc->base_frame_target; | 2263 const int bits_used = rc->base_frame_target; |
| 2301 rc->vbr_bits_off_target += rc->base_frame_target - rc->projected_frame_size; | 2264 rc->vbr_bits_off_target += rc->base_frame_target - rc->projected_frame_size; |
| 2302 #else | 2265 #else |
| 2303 // In this mode, VBR correction is acheived by altering bits_left, | 2266 // In this mode, VBR correction is acheived by altering bits_left, |
| 2304 // kf_group_bits & gf_group_bits to reflect any deviation from the target | 2267 // kf_group_bits & gf_group_bits to reflect any deviation from the target |
| 2305 // rate in this frame. This alters the allocation of bits to the | 2268 // rate in this frame. This alters the allocation of bits to the |
| 2306 // remaning frames in the group / clip. | 2269 // remaning frames in the group / clip. |
| 2307 // | 2270 // |
| 2308 // This method can give rise to unstable behaviour near the end of a clip | 2271 // This method can give rise to unstable behaviour near the end of a clip |
| 2309 // or kf/gf group of frames where any accumulated error is corrected over an | 2272 // or kf/gf group of frames where any accumulated error is corrected over an |
| 2310 // ever decreasing number of frames. Hence we change the balance of target | 2273 // ever decreasing number of frames. Hence we change the balance of target |
| 2311 // vs. actual bitrate gradually as we progress towards the end of the | 2274 // vs. actual bitrate gradually as we progress towards the end of the |
| 2312 // sequence in order to mitigate this effect. | 2275 // sequence in order to mitigate this effect. |
| 2313 const double progress = | 2276 const double progress = |
| 2314 (double)(cpi->twopass.stats_in - cpi->twopass.stats_in_start) / | 2277 (double)(twopass->stats_in - twopass->stats_in_start) / |
| 2315 (cpi->twopass.stats_in_end - cpi->twopass.stats_in_start); | 2278 (twopass->stats_in_end - twopass->stats_in_start); |
| 2316 const int bits_used = (int)(progress * rc->this_frame_target + | 2279 const int bits_used = (int)(progress * rc->this_frame_target + |
| 2317 (1.0 - progress) * rc->projected_frame_size); | 2280 (1.0 - progress) * rc->projected_frame_size); |
| 2318 #endif | 2281 #endif |
| 2319 | 2282 |
| 2320 cpi->twopass.bits_left -= bits_used; | 2283 twopass->bits_left = MAX(twopass->bits_left - bits_used, 0); |
| 2321 cpi->twopass.bits_left = MAX(cpi->twopass.bits_left, 0); | |
| 2322 | 2284 |
| 2323 #ifdef LONG_TERM_VBR_CORRECTION | 2285 #ifdef LONG_TERM_VBR_CORRECTION |
| 2324 if (cpi->common.frame_type != KEY_FRAME && | 2286 if (cpi->common.frame_type != KEY_FRAME && |
| 2325 !vp9_is_upper_layer_key_frame(cpi)) { | 2287 !vp9_is_upper_layer_key_frame(cpi)) { |
| 2326 #else | 2288 #else |
| 2327 if (cpi->common.frame_type == KEY_FRAME || | 2289 if (cpi->common.frame_type == KEY_FRAME || |
| 2328 vp9_is_upper_layer_key_frame(cpi)) { | 2290 vp9_is_upper_layer_key_frame(cpi)) { |
| 2329 // For key frames kf_group_bits already had the target bits subtracted out. | 2291 // For key frames kf_group_bits already had the target bits subtracted out. |
| 2330 // So now update to the correct value based on the actual bits used. | 2292 // So now update to the correct value based on the actual bits used. |
| 2331 cpi->twopass.kf_group_bits += cpi->rc.this_frame_target - bits_used; | 2293 twopass->kf_group_bits += rc->this_frame_target - bits_used; |
| 2332 } else { | 2294 } else { |
| 2333 #endif | 2295 #endif |
| 2334 cpi->twopass.kf_group_bits -= bits_used; | 2296 twopass->kf_group_bits -= bits_used; |
| 2335 cpi->twopass.gf_group_bits -= bits_used; | |
| 2336 cpi->twopass.gf_group_bits = MAX(cpi->twopass.gf_group_bits, 0); | |
| 2337 } | 2297 } |
| 2338 cpi->twopass.kf_group_bits = MAX(cpi->twopass.kf_group_bits, 0); | 2298 twopass->kf_group_bits = MAX(twopass->kf_group_bits, 0); |
| 2339 } | 2299 } |
| OLD | NEW |