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 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
202 double correction_factor) { | 202 double correction_factor) { |
203 const int bpm = (int)(vp9_rc_bits_per_mb(frame_kind, q, correction_factor)); | 203 const int bpm = (int)(vp9_rc_bits_per_mb(frame_kind, q, correction_factor)); |
204 | 204 |
205 // Attempt to retain reasonable accuracy without overflow. The cutoff is | 205 // Attempt to retain reasonable accuracy without overflow. The cutoff is |
206 // chosen such that the maximum product of Bpm and MBs fits 31 bits. The | 206 // chosen such that the maximum product of Bpm and MBs fits 31 bits. The |
207 // largest Bpm takes 20 bits. | 207 // largest Bpm takes 20 bits. |
208 return (mbs > (1 << 11)) ? (bpm >> BPER_MB_NORMBITS) * mbs | 208 return (mbs > (1 << 11)) ? (bpm >> BPER_MB_NORMBITS) * mbs |
209 : (bpm * mbs) >> BPER_MB_NORMBITS; | 209 : (bpm * mbs) >> BPER_MB_NORMBITS; |
210 } | 210 } |
211 | 211 |
| 212 int vp9_rc_clamp_pframe_target_size(const VP9_COMP *const cpi, int target) { |
| 213 const RATE_CONTROL *rc = &cpi->rc; |
| 214 const int min_frame_target = MAX(rc->min_frame_bandwidth, |
| 215 rc->av_per_frame_bandwidth >> 5); |
| 216 if (target < min_frame_target) |
| 217 target = min_frame_target; |
| 218 if (cpi->refresh_golden_frame && rc->is_src_frame_alt_ref) { |
| 219 // If there is an active ARF at this location use the minimum |
| 220 // bits on this frame even if it is a constructed arf. |
| 221 // The active maximum quantizer insures that an appropriate |
| 222 // number of bits will be spent if needed for constructed ARFs. |
| 223 target = min_frame_target; |
| 224 } |
| 225 // Clip the frame target to the maximum allowed value. |
| 226 if (target > rc->max_frame_bandwidth) |
| 227 target = rc->max_frame_bandwidth; |
| 228 return target; |
| 229 } |
212 | 230 |
213 static void calc_iframe_target_size(VP9_COMP *cpi) { | 231 int vp9_rc_clamp_iframe_target_size(const VP9_COMP *const cpi, int target) { |
| 232 const RATE_CONTROL *rc = &cpi->rc; |
214 const VP9_CONFIG *oxcf = &cpi->oxcf; | 233 const VP9_CONFIG *oxcf = &cpi->oxcf; |
215 RATE_CONTROL *const rc = &cpi->rc; | |
216 int target; | |
217 | |
218 vp9_clear_system_state(); // __asm emms; | |
219 | |
220 // For 1-pass. | |
221 if (cpi->pass == 0) { | |
222 if (cpi->common.current_video_frame == 0) { | |
223 target = oxcf->starting_buffer_level / 2; | |
224 } else { | |
225 // TODO(marpan): Add in adjustment based on Q. | |
226 // If this keyframe was forced, use a more recent Q estimate. | |
227 // int Q = (cpi->common.frame_flags & FRAMEFLAGS_KEY) ? | |
228 // cpi->rc.avg_frame_qindex : cpi->rc.ni_av_qi; | |
229 int initial_boost = 32; | |
230 // Boost depends somewhat on frame rate. | |
231 int kf_boost = MAX(initial_boost, (int)(2 * cpi->output_framerate - 16)); | |
232 // Adjustment up based on q: need to fix. | |
233 // kf_boost = kf_boost * kfboost_qadjust(Q) / 100; | |
234 // Frame separation adjustment (down). | |
235 if (rc->frames_since_key < cpi->output_framerate / 2) { | |
236 kf_boost = (int)(kf_boost * rc->frames_since_key / | |
237 (cpi->output_framerate / 2)); | |
238 } | |
239 kf_boost = (kf_boost < 16) ? 16 : kf_boost; | |
240 target = ((16 + kf_boost) * rc->per_frame_bandwidth) >> 4; | |
241 } | |
242 rc->active_worst_quality = rc->worst_quality; | |
243 } else { | |
244 target = rc->per_frame_bandwidth; | |
245 } | |
246 | |
247 if (oxcf->rc_max_intra_bitrate_pct) { | 234 if (oxcf->rc_max_intra_bitrate_pct) { |
248 const int max_rate = rc->per_frame_bandwidth * | 235 const int max_rate = rc->av_per_frame_bandwidth * |
249 oxcf->rc_max_intra_bitrate_pct / 100; | 236 oxcf->rc_max_intra_bitrate_pct / 100; |
250 target = MIN(target, max_rate); | 237 target = MIN(target, max_rate); |
251 } | 238 } |
252 rc->this_frame_target = target; | 239 if (target > rc->max_frame_bandwidth) |
| 240 target = rc->max_frame_bandwidth; |
| 241 return target; |
| 242 } |
| 243 |
| 244 |
| 245 // Update the buffer level for higher layers, given the encoded current layer. |
| 246 static void update_layer_buffer_level(VP9_COMP *const cpi, |
| 247 int encoded_frame_size) { |
| 248 int temporal_layer = 0; |
| 249 int current_temporal_layer = cpi->svc.temporal_layer_id; |
| 250 for (temporal_layer = current_temporal_layer + 1; |
| 251 temporal_layer < cpi->svc.number_temporal_layers; ++temporal_layer) { |
| 252 LAYER_CONTEXT *lc = &cpi->svc.layer_context[temporal_layer]; |
| 253 RATE_CONTROL *lrc = &lc->rc; |
| 254 int bits_off_for_this_layer = (int)(lc->target_bandwidth / lc->framerate - |
| 255 encoded_frame_size); |
| 256 lrc->bits_off_target += bits_off_for_this_layer; |
| 257 |
| 258 // Clip buffer level to maximum buffer size for the layer. |
| 259 lrc->bits_off_target = MIN(lrc->bits_off_target, lc->maximum_buffer_size); |
| 260 lrc->buffer_level = lrc->bits_off_target; |
| 261 } |
253 } | 262 } |
254 | 263 |
255 // Update the buffer level: leaky bucket model. | 264 // Update the buffer level: leaky bucket model. |
256 void vp9_update_buffer_level(VP9_COMP *const cpi, int encoded_frame_size) { | 265 static void update_buffer_level(VP9_COMP *cpi, int encoded_frame_size) { |
257 const VP9_COMMON *const cm = &cpi->common; | 266 const VP9_COMMON *const cm = &cpi->common; |
258 const VP9_CONFIG *oxcf = &cpi->oxcf; | 267 const VP9_CONFIG *oxcf = &cpi->oxcf; |
259 RATE_CONTROL *const rc = &cpi->rc; | 268 RATE_CONTROL *const rc = &cpi->rc; |
260 | 269 |
261 // Non-viewable frames are a special case and are treated as pure overhead. | 270 // Non-viewable frames are a special case and are treated as pure overhead. |
262 if (!cm->show_frame) { | 271 if (!cm->show_frame) { |
263 rc->bits_off_target -= encoded_frame_size; | 272 rc->bits_off_target -= encoded_frame_size; |
264 } else { | 273 } else { |
265 rc->bits_off_target += rc->av_per_frame_bandwidth - encoded_frame_size; | 274 rc->bits_off_target += rc->av_per_frame_bandwidth - encoded_frame_size; |
266 } | 275 } |
267 | 276 |
268 // Clip the buffer level to the maximum specified buffer size. | 277 // Clip the buffer level to the maximum specified buffer size. |
269 rc->buffer_level = MIN(rc->bits_off_target, oxcf->maximum_buffer_size); | 278 rc->bits_off_target = MIN(rc->bits_off_target, oxcf->maximum_buffer_size); |
| 279 rc->buffer_level = rc->bits_off_target; |
| 280 |
| 281 if (cpi->use_svc && cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) { |
| 282 update_layer_buffer_level(cpi, encoded_frame_size); |
| 283 } |
270 } | 284 } |
271 | 285 |
272 int vp9_drop_frame(VP9_COMP *const cpi) { | 286 int vp9_rc_drop_frame(VP9_COMP *cpi) { |
273 const VP9_CONFIG *oxcf = &cpi->oxcf; | 287 const VP9_CONFIG *oxcf = &cpi->oxcf; |
274 RATE_CONTROL *const rc = &cpi->rc; | 288 RATE_CONTROL *const rc = &cpi->rc; |
275 | 289 |
276 | |
277 if (!oxcf->drop_frames_water_mark) { | 290 if (!oxcf->drop_frames_water_mark) { |
278 return 0; | 291 return 0; |
279 } else { | 292 } else { |
280 if (rc->buffer_level < 0) { | 293 if (rc->buffer_level < 0) { |
281 // Always drop if buffer is below 0. | 294 // Always drop if buffer is below 0. |
282 return 1; | 295 return 1; |
283 } else { | 296 } else { |
284 // If buffer is below drop_mark, for now just drop every other frame | 297 // If buffer is below drop_mark, for now just drop every other frame |
285 // (starting with the next frame) until it increases back over drop_mark. | 298 // (starting with the next frame) until it increases back over drop_mark. |
286 int drop_mark = (int)(oxcf->drop_frames_water_mark * | 299 int drop_mark = (int)(oxcf->drop_frames_water_mark * |
287 oxcf->optimal_buffer_level / 100); | 300 oxcf->optimal_buffer_level / 100); |
288 if ((rc->buffer_level > drop_mark) && | 301 if ((rc->buffer_level > drop_mark) && |
289 (rc->decimation_factor > 0)) { | 302 (rc->decimation_factor > 0)) { |
290 --rc->decimation_factor; | 303 --rc->decimation_factor; |
291 } else if (rc->buffer_level <= drop_mark && | 304 } else if (rc->buffer_level <= drop_mark && |
292 rc->decimation_factor == 0) { | 305 rc->decimation_factor == 0) { |
293 rc->decimation_factor = 1; | 306 rc->decimation_factor = 1; |
294 } | 307 } |
295 if (rc->decimation_factor > 0) { | 308 if (rc->decimation_factor > 0) { |
296 if (rc->decimation_count > 0) { | 309 if (rc->decimation_count > 0) { |
297 --rc->decimation_count; | 310 --rc->decimation_count; |
298 return 1; | 311 return 1; |
299 } else { | 312 } else { |
300 rc->decimation_count = rc->decimation_factor; | 313 rc->decimation_count = rc->decimation_factor; |
301 return 0; | 314 return 0; |
302 } | 315 } |
303 } else { | 316 } else { |
304 rc->decimation_count = 0; | 317 rc->decimation_count = 0; |
305 return 0; | 318 return 0; |
306 } | 319 } |
307 } | 320 } |
308 } | 321 } |
309 } | 322 } |
310 | 323 |
311 // Adjust active_worst_quality level based on buffer level. | |
312 static int adjust_active_worst_quality_from_buffer_level(const VP9_CONFIG *oxcf, | |
313 const RATE_CONTROL *rc) { | |
314 // Adjust active_worst_quality: If buffer is above the optimal/target level, | |
315 // bring active_worst_quality down depending on fullness over buffer. | |
316 // If buffer is below the optimal level, let the active_worst_quality go from | |
317 // ambient Q (at buffer = optimal level) to worst_quality level | |
318 // (at buffer = critical level). | |
319 | |
320 int active_worst_quality = rc->active_worst_quality; | |
321 // Maximum limit for down adjustment, ~20%. | |
322 int max_adjustment_down = active_worst_quality / 5; | |
323 // Buffer level below which we push active_worst to worst_quality. | |
324 int critical_level = oxcf->optimal_buffer_level >> 2; | |
325 int adjustment = 0; | |
326 int buff_lvl_step = 0; | |
327 if (rc->buffer_level > oxcf->optimal_buffer_level) { | |
328 // Adjust down. | |
329 if (max_adjustment_down) { | |
330 buff_lvl_step = (int)((oxcf->maximum_buffer_size - | |
331 oxcf->optimal_buffer_level) / max_adjustment_down); | |
332 if (buff_lvl_step) | |
333 adjustment = (int)((rc->buffer_level - oxcf->optimal_buffer_level) / | |
334 buff_lvl_step); | |
335 active_worst_quality -= adjustment; | |
336 } | |
337 } else if (rc->buffer_level > critical_level) { | |
338 // Adjust up from ambient Q. | |
339 if (critical_level) { | |
340 buff_lvl_step = (oxcf->optimal_buffer_level - critical_level); | |
341 if (buff_lvl_step) { | |
342 adjustment = (rc->worst_quality - rc->avg_frame_qindex[INTER_FRAME]) * | |
343 (oxcf->optimal_buffer_level - rc->buffer_level) / | |
344 buff_lvl_step; | |
345 } | |
346 active_worst_quality = rc->avg_frame_qindex[INTER_FRAME] + adjustment; | |
347 } | |
348 } else { | |
349 // Set to worst_quality if buffer is below critical level. | |
350 active_worst_quality = rc->worst_quality; | |
351 } | |
352 return active_worst_quality; | |
353 } | |
354 | |
355 // Adjust target frame size with respect to the buffering constraints: | |
356 static int target_size_from_buffer_level(const VP9_CONFIG *oxcf, | |
357 const RATE_CONTROL *rc) { | |
358 int target = rc->this_frame_target; | |
359 const int64_t diff = oxcf->optimal_buffer_level - rc->buffer_level; | |
360 const int one_pct_bits = 1 + oxcf->optimal_buffer_level / 100; | |
361 | |
362 if (diff > 0) { | |
363 // Lower the target bandwidth for this frame. | |
364 const int pct_low = MIN(diff / one_pct_bits, oxcf->under_shoot_pct); | |
365 target -= (target * pct_low) / 200; | |
366 } else if (diff < 0) { | |
367 // Increase the target bandwidth for this frame. | |
368 const int pct_high = MIN(-diff / one_pct_bits, oxcf->over_shoot_pct); | |
369 target += (target * pct_high) / 200; | |
370 } | |
371 | |
372 return target; | |
373 } | |
374 | |
375 static void calc_pframe_target_size(VP9_COMP *const cpi) { | |
376 RATE_CONTROL *const rc = &cpi->rc; | |
377 const VP9_CONFIG *const oxcf = &cpi->oxcf; | |
378 int min_frame_target = MAX(rc->min_frame_bandwidth, | |
379 rc->av_per_frame_bandwidth >> 5); | |
380 if (cpi->refresh_alt_ref_frame) { | |
381 // Special alt reference frame case | |
382 // Per frame bit target for the alt ref frame | |
383 rc->per_frame_bandwidth = cpi->twopass.gf_bits; | |
384 rc->this_frame_target = rc->per_frame_bandwidth; | |
385 } else { | |
386 // Normal frames (gf and inter). | |
387 rc->this_frame_target = rc->per_frame_bandwidth; | |
388 // Set target frame size based on buffer level, for 1 pass CBR. | |
389 if (cpi->pass == 0 && oxcf->end_usage == USAGE_STREAM_FROM_SERVER) { | |
390 // Need to decide how low min_frame_target should be for 1-pass CBR. | |
391 // For now, use: cpi->rc.av_per_frame_bandwidth / 16: | |
392 min_frame_target = MAX(rc->av_per_frame_bandwidth >> 4, | |
393 FRAME_OVERHEAD_BITS); | |
394 rc->this_frame_target = target_size_from_buffer_level(oxcf, rc); | |
395 // Adjust qp-max based on buffer level. | |
396 rc->active_worst_quality = | |
397 adjust_active_worst_quality_from_buffer_level(oxcf, rc); | |
398 } | |
399 } | |
400 | |
401 // Check that the total sum of adjustments is not above the maximum allowed. | |
402 // That is, having allowed for the KF and GF penalties, we have not pushed | |
403 // the current inter-frame target too low. If the adjustment we apply here is | |
404 // not capable of recovering all the extra bits we have spent in the KF or GF, | |
405 // then the remainder will have to be recovered over a longer time span via | |
406 // other buffer / rate control mechanisms. | |
407 if (rc->this_frame_target < min_frame_target) | |
408 rc->this_frame_target = min_frame_target; | |
409 | |
410 // Adjust target frame size for Golden Frames: | |
411 if (cpi->refresh_golden_frame) { | |
412 // If we are using alternate ref instead of gf then do not apply the boost | |
413 // It will instead be applied to the altref update | |
414 // Jims modified boost | |
415 if (!rc->source_alt_ref_active) { | |
416 // The spend on the GF is defined in the two pass code | |
417 // for two pass encodes | |
418 rc->this_frame_target = rc->per_frame_bandwidth; | |
419 } else { | |
420 // If there is an active ARF at this location use the minimum | |
421 // bits on this frame even if it is a constructed arf. | |
422 // The active maximum quantizer insures that an appropriate | |
423 // number of bits will be spent if needed for constructed ARFs. | |
424 rc->this_frame_target = 0; | |
425 } | |
426 } | |
427 } | |
428 | |
429 static double get_rate_correction_factor(const VP9_COMP *cpi) { | 324 static double get_rate_correction_factor(const VP9_COMP *cpi) { |
430 if (cpi->common.frame_type == KEY_FRAME) { | 325 if (cpi->common.frame_type == KEY_FRAME) { |
431 return cpi->rc.key_frame_rate_correction_factor; | 326 return cpi->rc.key_frame_rate_correction_factor; |
432 } else { | 327 } else { |
433 if (cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) | 328 if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) && |
| 329 !(cpi->use_svc && cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)) |
434 return cpi->rc.gf_rate_correction_factor; | 330 return cpi->rc.gf_rate_correction_factor; |
435 else | 331 else |
436 return cpi->rc.rate_correction_factor; | 332 return cpi->rc.rate_correction_factor; |
437 } | 333 } |
438 } | 334 } |
439 | 335 |
440 static void set_rate_correction_factor(VP9_COMP *cpi, double factor) { | 336 static void set_rate_correction_factor(VP9_COMP *cpi, double factor) { |
441 if (cpi->common.frame_type == KEY_FRAME) { | 337 if (cpi->common.frame_type == KEY_FRAME) { |
442 cpi->rc.key_frame_rate_correction_factor = factor; | 338 cpi->rc.key_frame_rate_correction_factor = factor; |
443 } else { | 339 } else { |
444 if (cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) | 340 if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) && |
| 341 !(cpi->use_svc && cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)) |
445 cpi->rc.gf_rate_correction_factor = factor; | 342 cpi->rc.gf_rate_correction_factor = factor; |
446 else | 343 else |
447 cpi->rc.rate_correction_factor = factor; | 344 cpi->rc.rate_correction_factor = factor; |
448 } | 345 } |
449 } | 346 } |
450 | 347 |
451 void vp9_rc_update_rate_correction_factors(VP9_COMP *cpi, int damp_var) { | 348 void vp9_rc_update_rate_correction_factors(VP9_COMP *cpi, int damp_var) { |
452 const int q = cpi->common.base_qindex; | 349 const int q = cpi->common.base_qindex; |
453 int correction_factor = 100; | 350 int correction_factor = 100; |
454 double rate_correction_factor = get_rate_correction_factor(cpi); | 351 double rate_correction_factor = get_rate_correction_factor(cpi); |
455 double adjustment_limit; | 352 double adjustment_limit; |
456 | 353 |
457 int projected_size_based_on_q = 0; | 354 int projected_size_based_on_q = 0; |
458 | 355 |
459 // Clear down mmx registers to allow floating point in what follows | 356 // Clear down mmx registers to allow floating point in what follows |
460 vp9_clear_system_state(); // __asm emms; | 357 vp9_clear_system_state(); // __asm emms; |
461 | 358 |
462 // Work out how big we would have expected the frame to be at this Q given | 359 // Work out how big we would have expected the frame to be at this Q given |
463 // the current correction factor. | 360 // the current correction factor. |
464 // Stay in double to avoid int overflow when values are large | 361 // Stay in double to avoid int overflow when values are large |
465 projected_size_based_on_q = estimate_bits_at_q(cpi->common.frame_type, q, | 362 projected_size_based_on_q = estimate_bits_at_q(cpi->common.frame_type, q, |
466 cpi->common.MBs, | 363 cpi->common.MBs, |
467 rate_correction_factor); | 364 rate_correction_factor); |
468 | |
469 // Work out a size correction factor. | 365 // Work out a size correction factor. |
470 if (projected_size_based_on_q > 0) | 366 if (projected_size_based_on_q > 0) |
471 correction_factor = (100 * cpi->rc.projected_frame_size) / | 367 correction_factor = (100 * cpi->rc.projected_frame_size) / |
472 projected_size_based_on_q; | 368 projected_size_based_on_q; |
473 | 369 |
474 // More heavily damped adjustment used if we have been oscillating either side | 370 // More heavily damped adjustment used if we have been oscillating either side |
475 // of target. | 371 // of target. |
476 switch (damp_var) { | 372 switch (damp_var) { |
477 case 0: | 373 case 0: |
478 adjustment_limit = 0.75; | 374 adjustment_limit = 0.75; |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
557 return high_motion_minq[q]; | 453 return high_motion_minq[q]; |
558 } else { | 454 } else { |
559 const int gap = high - low; | 455 const int gap = high - low; |
560 const int offset = high - gfu_boost; | 456 const int offset = high - gfu_boost; |
561 const int qdiff = high_motion_minq[q] - low_motion_minq[q]; | 457 const int qdiff = high_motion_minq[q] - low_motion_minq[q]; |
562 const int adjustment = ((offset * qdiff) + (gap >> 1)) / gap; | 458 const int adjustment = ((offset * qdiff) + (gap >> 1)) / gap; |
563 return low_motion_minq[q] + adjustment; | 459 return low_motion_minq[q] + adjustment; |
564 } | 460 } |
565 } | 461 } |
566 | 462 |
567 int vp9_rc_pick_q_and_adjust_q_bounds(const VP9_COMP *cpi, | 463 static int calc_active_worst_quality_one_pass_vbr(const VP9_COMP *cpi) { |
568 int *bottom_index, int *top_index) { | 464 int active_worst_quality; |
| 465 if (cpi->common.frame_type == KEY_FRAME) { |
| 466 if (cpi->common.current_video_frame == 0) { |
| 467 active_worst_quality = cpi->rc.worst_quality; |
| 468 } else { |
| 469 // Choose active worst quality twice as large as the last q. |
| 470 active_worst_quality = cpi->rc.last_q[KEY_FRAME] * 2; |
| 471 } |
| 472 } else if (!cpi->rc.is_src_frame_alt_ref && |
| 473 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) { |
| 474 if (cpi->common.current_video_frame == 1) { |
| 475 active_worst_quality = cpi->rc.last_q[KEY_FRAME] * 5 / 4; |
| 476 } else { |
| 477 // Choose active worst quality twice as large as the last q. |
| 478 active_worst_quality = cpi->rc.last_q[INTER_FRAME]; |
| 479 } |
| 480 } else { |
| 481 if (cpi->common.current_video_frame == 1) { |
| 482 active_worst_quality = cpi->rc.last_q[KEY_FRAME] * 2; |
| 483 } else { |
| 484 // Choose active worst quality twice as large as the last q. |
| 485 active_worst_quality = cpi->rc.last_q[INTER_FRAME] * 2; |
| 486 } |
| 487 } |
| 488 if (active_worst_quality > cpi->rc.worst_quality) |
| 489 active_worst_quality = cpi->rc.worst_quality; |
| 490 return active_worst_quality; |
| 491 } |
| 492 |
| 493 // Adjust active_worst_quality level based on buffer level. |
| 494 static int calc_active_worst_quality_one_pass_cbr(const VP9_COMP *cpi) { |
| 495 // Adjust active_worst_quality: If buffer is above the optimal/target level, |
| 496 // bring active_worst_quality down depending on fullness of buffer. |
| 497 // If buffer is below the optimal level, let the active_worst_quality go from |
| 498 // ambient Q (at buffer = optimal level) to worst_quality level |
| 499 // (at buffer = critical level). |
| 500 const VP9_CONFIG *oxcf = &cpi->oxcf; |
| 501 const RATE_CONTROL *rc = &cpi->rc; |
| 502 // Buffer level below which we push active_worst to worst_quality. |
| 503 int critical_level = oxcf->optimal_buffer_level >> 2; |
| 504 int adjustment = 0; |
| 505 int buff_lvl_step = 0; |
| 506 int active_worst_quality; |
| 507 if (cpi->common.frame_type == KEY_FRAME) |
| 508 return rc->worst_quality; |
| 509 if (cpi->common.current_video_frame > 1) |
| 510 active_worst_quality = MIN(rc->worst_quality, |
| 511 rc->avg_frame_qindex[INTER_FRAME] * 5 / 4); |
| 512 else |
| 513 active_worst_quality = MIN(rc->worst_quality, |
| 514 rc->avg_frame_qindex[KEY_FRAME] * 3 / 2); |
| 515 if (rc->buffer_level > oxcf->optimal_buffer_level) { |
| 516 // Adjust down. |
| 517 // Maximum limit for down adjustment, ~30%. |
| 518 int max_adjustment_down = active_worst_quality / 3; |
| 519 if (max_adjustment_down) { |
| 520 buff_lvl_step = (int)((oxcf->maximum_buffer_size - |
| 521 oxcf->optimal_buffer_level) / max_adjustment_down); |
| 522 if (buff_lvl_step) |
| 523 adjustment = (int)((rc->buffer_level - oxcf->optimal_buffer_level) / |
| 524 buff_lvl_step); |
| 525 active_worst_quality -= adjustment; |
| 526 } |
| 527 } else if (rc->buffer_level > critical_level) { |
| 528 // Adjust up from ambient Q. |
| 529 if (critical_level) { |
| 530 buff_lvl_step = (oxcf->optimal_buffer_level - critical_level); |
| 531 if (buff_lvl_step) { |
| 532 adjustment = (rc->worst_quality - rc->avg_frame_qindex[INTER_FRAME]) * |
| 533 (oxcf->optimal_buffer_level - rc->buffer_level) / |
| 534 buff_lvl_step; |
| 535 } |
| 536 active_worst_quality = rc->avg_frame_qindex[INTER_FRAME] + adjustment; |
| 537 } |
| 538 } else { |
| 539 // Set to worst_quality if buffer is below critical level. |
| 540 active_worst_quality = rc->worst_quality; |
| 541 } |
| 542 return active_worst_quality; |
| 543 } |
| 544 |
| 545 static int rc_pick_q_and_bounds_one_pass_cbr(const VP9_COMP *cpi, |
| 546 int *bottom_index, |
| 547 int *top_index) { |
| 548 const VP9_COMMON *const cm = &cpi->common; |
| 549 const RATE_CONTROL *const rc = &cpi->rc; |
| 550 int active_best_quality; |
| 551 int active_worst_quality = calc_active_worst_quality_one_pass_cbr(cpi); |
| 552 int q; |
| 553 |
| 554 if (frame_is_intra_only(cm)) { |
| 555 active_best_quality = rc->best_quality; |
| 556 // Handle the special case for key frames forced when we have75 reached |
| 557 // the maximum key frame interval. Here force the Q to a range |
| 558 // based on the ambient Q to reduce the risk of popping. |
| 559 if (rc->this_key_frame_forced) { |
| 560 int qindex = rc->last_boosted_qindex; |
| 561 double last_boosted_q = vp9_convert_qindex_to_q(qindex); |
| 562 int delta_qindex = vp9_compute_qdelta(cpi, last_boosted_q, |
| 563 (last_boosted_q * 0.75)); |
| 564 active_best_quality = MAX(qindex + delta_qindex, rc->best_quality); |
| 565 } else if (cm->current_video_frame > 0) { |
| 566 // not first frame of one pass and kf_boost is set |
| 567 double q_adj_factor = 1.0; |
| 568 double q_val; |
| 569 |
| 570 active_best_quality = get_active_quality(rc->avg_frame_qindex[KEY_FRAME], |
| 571 rc->kf_boost, |
| 572 kf_low, kf_high, |
| 573 kf_low_motion_minq, |
| 574 kf_high_motion_minq); |
| 575 |
| 576 // Allow somewhat lower kf minq with small image formats. |
| 577 if ((cm->width * cm->height) <= (352 * 288)) { |
| 578 q_adj_factor -= 0.25; |
| 579 } |
| 580 |
| 581 // Convert the adjustment factor to a qindex delta |
| 582 // on active_best_quality. |
| 583 q_val = vp9_convert_qindex_to_q(active_best_quality); |
| 584 active_best_quality += vp9_compute_qdelta(cpi, q_val, q_val * |
| 585 q_adj_factor); |
| 586 } |
| 587 } else if (!rc->is_src_frame_alt_ref && |
| 588 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) { |
| 589 // Use the lower of active_worst_quality and recent |
| 590 // average Q as basis for GF/ARF best Q limit unless last frame was |
| 591 // a key frame. |
| 592 if (rc->frames_since_key > 1 && |
| 593 rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) { |
| 594 q = rc->avg_frame_qindex[INTER_FRAME]; |
| 595 } else { |
| 596 q = active_worst_quality; |
| 597 } |
| 598 active_best_quality = get_active_quality( |
| 599 q, rc->gfu_boost, gf_low, gf_high, |
| 600 gf_low_motion_minq, gf_high_motion_minq); |
| 601 } else { |
| 602 // Use the lower of active_worst_quality and recent/average Q. |
| 603 if (cm->current_video_frame > 1) { |
| 604 if (rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) |
| 605 active_best_quality = inter_minq[rc->avg_frame_qindex[INTER_FRAME]]; |
| 606 else |
| 607 active_best_quality = inter_minq[active_worst_quality]; |
| 608 } else { |
| 609 if (rc->avg_frame_qindex[KEY_FRAME] < active_worst_quality) |
| 610 active_best_quality = inter_minq[rc->avg_frame_qindex[KEY_FRAME]]; |
| 611 else |
| 612 active_best_quality = inter_minq[active_worst_quality]; |
| 613 } |
| 614 } |
| 615 |
| 616 // Clip the active best and worst quality values to limits |
| 617 active_best_quality = clamp(active_best_quality, |
| 618 rc->best_quality, rc->worst_quality); |
| 619 active_worst_quality = clamp(active_worst_quality, |
| 620 active_best_quality, rc->worst_quality); |
| 621 |
| 622 *top_index = active_worst_quality; |
| 623 *bottom_index = active_best_quality; |
| 624 |
| 625 #if LIMIT_QRANGE_FOR_ALTREF_AND_KEY |
| 626 // Limit Q range for the adaptive loop. |
| 627 if (cm->frame_type == KEY_FRAME && !rc->this_key_frame_forced) { |
| 628 if (!(cm->current_video_frame == 0)) |
| 629 *top_index = (active_worst_quality + active_best_quality * 3) / 4; |
| 630 } |
| 631 #endif |
| 632 // Special case code to try and match quality with forced key frames |
| 633 if (cm->frame_type == KEY_FRAME && rc->this_key_frame_forced) { |
| 634 q = rc->last_boosted_qindex; |
| 635 } else { |
| 636 q = vp9_rc_regulate_q(cpi, rc->this_frame_target, |
| 637 active_best_quality, active_worst_quality); |
| 638 if (q > *top_index) { |
| 639 // Special case when we are targeting the max allowed rate |
| 640 if (cpi->rc.this_frame_target >= cpi->rc.max_frame_bandwidth) |
| 641 *top_index = q; |
| 642 else |
| 643 q = *top_index; |
| 644 } |
| 645 } |
| 646 assert(*top_index <= rc->worst_quality && |
| 647 *top_index >= rc->best_quality); |
| 648 assert(*bottom_index <= rc->worst_quality && |
| 649 *bottom_index >= rc->best_quality); |
| 650 assert(q <= rc->worst_quality && q >= rc->best_quality); |
| 651 return q; |
| 652 } |
| 653 |
| 654 static int rc_pick_q_and_bounds_one_pass_vbr(const VP9_COMP *cpi, |
| 655 int *bottom_index, |
| 656 int *top_index) { |
569 const VP9_COMMON *const cm = &cpi->common; | 657 const VP9_COMMON *const cm = &cpi->common; |
570 const RATE_CONTROL *const rc = &cpi->rc; | 658 const RATE_CONTROL *const rc = &cpi->rc; |
571 const VP9_CONFIG *const oxcf = &cpi->oxcf; | 659 const VP9_CONFIG *const oxcf = &cpi->oxcf; |
572 int active_best_quality; | 660 int active_best_quality; |
573 int active_worst_quality = rc->active_worst_quality; | 661 int active_worst_quality = calc_active_worst_quality_one_pass_vbr(cpi); |
574 int q; | 662 int q; |
575 | 663 |
576 if (frame_is_intra_only(cm)) { | 664 if (frame_is_intra_only(cm)) { |
577 active_best_quality = rc->best_quality; | 665 active_best_quality = rc->best_quality; |
578 #if !CONFIG_MULTIPLE_ARF | 666 #if !CONFIG_MULTIPLE_ARF |
579 // Handle the special case for key frames forced when we have75 reached | 667 // Handle the special case for key frames forced when we have75 reached |
580 // the maximum key frame interval. Here force the Q to a range | 668 // the maximum key frame interval. Here force the Q to a range |
581 // based on the ambient Q to reduce the risk of popping. | 669 // based on the ambient Q to reduce the risk of popping. |
582 if (rc->this_key_frame_forced) { | 670 if (rc->this_key_frame_forced) { |
583 int qindex = rc->last_boosted_qindex; | 671 int qindex = rc->last_boosted_qindex; |
584 double last_boosted_q = vp9_convert_qindex_to_q(qindex); | 672 double last_boosted_q = vp9_convert_qindex_to_q(qindex); |
585 int delta_qindex = vp9_compute_qdelta(cpi, last_boosted_q, | 673 int delta_qindex = vp9_compute_qdelta(cpi, last_boosted_q, |
586 (last_boosted_q * 0.75)); | 674 (last_boosted_q * 0.75)); |
587 active_best_quality = MAX(qindex + delta_qindex, rc->best_quality); | 675 active_best_quality = MAX(qindex + delta_qindex, rc->best_quality); |
588 } else if (!(cpi->pass == 0 && cm->current_video_frame == 0)) { | 676 } else if (cm->current_video_frame > 0) { |
589 // not first frame of one pass and kf_boost is set | 677 // not first frame of one pass and kf_boost is set |
590 double q_adj_factor = 1.0; | 678 double q_adj_factor = 1.0; |
591 double q_val; | 679 double q_val; |
592 | 680 |
593 // Baseline value derived from cpi->active_worst_quality and kf boost | 681 active_best_quality = get_active_quality(rc->avg_frame_qindex[KEY_FRAME], |
| 682 rc->kf_boost, |
| 683 kf_low, kf_high, |
| 684 kf_low_motion_minq, |
| 685 kf_high_motion_minq); |
| 686 |
| 687 // Allow somewhat lower kf minq with small image formats. |
| 688 if ((cm->width * cm->height) <= (352 * 288)) { |
| 689 q_adj_factor -= 0.25; |
| 690 } |
| 691 |
| 692 // Convert the adjustment factor to a qindex delta |
| 693 // on active_best_quality. |
| 694 q_val = vp9_convert_qindex_to_q(active_best_quality); |
| 695 active_best_quality += vp9_compute_qdelta(cpi, q_val, q_val * |
| 696 q_adj_factor); |
| 697 } |
| 698 #else |
| 699 double current_q; |
| 700 // Force the KF quantizer to be 30% of the active_worst_quality. |
| 701 current_q = vp9_convert_qindex_to_q(active_worst_quality); |
| 702 active_best_quality = active_worst_quality |
| 703 + vp9_compute_qdelta(cpi, current_q, current_q * 0.3); |
| 704 #endif |
| 705 } else if (!rc->is_src_frame_alt_ref && |
| 706 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) { |
| 707 // Use the lower of active_worst_quality and recent |
| 708 // average Q as basis for GF/ARF best Q limit unless last frame was |
| 709 // a key frame. |
| 710 if (rc->frames_since_key > 1 && |
| 711 rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) { |
| 712 q = rc->avg_frame_qindex[INTER_FRAME]; |
| 713 } else { |
| 714 q = rc->avg_frame_qindex[KEY_FRAME]; |
| 715 } |
| 716 // For constrained quality dont allow Q less than the cq level |
| 717 if (oxcf->end_usage == USAGE_CONSTRAINED_QUALITY) { |
| 718 if (q < cpi->cq_target_quality) |
| 719 q = cpi->cq_target_quality; |
| 720 if (rc->frames_since_key > 1) { |
| 721 active_best_quality = get_active_quality(q, rc->gfu_boost, |
| 722 gf_low, gf_high, |
| 723 afq_low_motion_minq, |
| 724 afq_high_motion_minq); |
| 725 } else { |
| 726 active_best_quality = get_active_quality(q, rc->gfu_boost, |
| 727 gf_low, gf_high, |
| 728 gf_low_motion_minq, |
| 729 gf_high_motion_minq); |
| 730 } |
| 731 // Constrained quality use slightly lower active best. |
| 732 active_best_quality = active_best_quality * 15 / 16; |
| 733 |
| 734 } else if (oxcf->end_usage == USAGE_CONSTANT_QUALITY) { |
| 735 if (!cpi->refresh_alt_ref_frame) { |
| 736 active_best_quality = cpi->cq_target_quality; |
| 737 } else { |
| 738 if (rc->frames_since_key > 1) { |
| 739 active_best_quality = get_active_quality( |
| 740 q, rc->gfu_boost, gf_low, gf_high, |
| 741 afq_low_motion_minq, afq_high_motion_minq); |
| 742 } else { |
| 743 active_best_quality = get_active_quality( |
| 744 q, rc->gfu_boost, gf_low, gf_high, |
| 745 gf_low_motion_minq, gf_high_motion_minq); |
| 746 } |
| 747 } |
| 748 } else { |
| 749 active_best_quality = get_active_quality( |
| 750 q, rc->gfu_boost, gf_low, gf_high, |
| 751 gf_low_motion_minq, gf_high_motion_minq); |
| 752 } |
| 753 } else { |
| 754 if (oxcf->end_usage == USAGE_CONSTANT_QUALITY) { |
| 755 active_best_quality = cpi->cq_target_quality; |
| 756 } else { |
| 757 // Use the lower of active_worst_quality and recent/average Q. |
| 758 if (cm->current_video_frame > 1) |
| 759 active_best_quality = inter_minq[rc->avg_frame_qindex[INTER_FRAME]]; |
| 760 else |
| 761 active_best_quality = inter_minq[rc->avg_frame_qindex[KEY_FRAME]]; |
| 762 // For the constrained quality mode we don't want |
| 763 // q to fall below the cq level. |
| 764 if ((oxcf->end_usage == USAGE_CONSTRAINED_QUALITY) && |
| 765 (active_best_quality < cpi->cq_target_quality)) { |
| 766 // If we are strongly undershooting the target rate in the last |
| 767 // frames then use the user passed in cq value not the auto |
| 768 // cq value. |
| 769 if (rc->rolling_actual_bits < rc->min_frame_bandwidth) |
| 770 active_best_quality = oxcf->cq_level; |
| 771 else |
| 772 active_best_quality = cpi->cq_target_quality; |
| 773 } |
| 774 } |
| 775 } |
| 776 |
| 777 // Clip the active best and worst quality values to limits |
| 778 active_best_quality = clamp(active_best_quality, |
| 779 rc->best_quality, rc->worst_quality); |
| 780 active_worst_quality = clamp(active_worst_quality, |
| 781 active_best_quality, rc->worst_quality); |
| 782 |
| 783 *top_index = active_worst_quality; |
| 784 *bottom_index = active_best_quality; |
| 785 |
| 786 #if LIMIT_QRANGE_FOR_ALTREF_AND_KEY |
| 787 // Limit Q range for the adaptive loop. |
| 788 if (cm->frame_type == KEY_FRAME && !rc->this_key_frame_forced) { |
| 789 if (!(cm->current_video_frame == 0)) |
| 790 *top_index = (active_worst_quality + active_best_quality * 3) / 4; |
| 791 } else if (!rc->is_src_frame_alt_ref && |
| 792 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) { |
| 793 *top_index = (active_worst_quality + active_best_quality) / 2; |
| 794 } |
| 795 #endif |
| 796 if (oxcf->end_usage == USAGE_CONSTANT_QUALITY) { |
| 797 q = active_best_quality; |
| 798 // Special case code to try and match quality with forced key frames |
| 799 } else if ((cm->frame_type == KEY_FRAME) && rc->this_key_frame_forced) { |
| 800 q = rc->last_boosted_qindex; |
| 801 } else { |
| 802 q = vp9_rc_regulate_q(cpi, rc->this_frame_target, |
| 803 active_best_quality, active_worst_quality); |
| 804 if (q > *top_index) { |
| 805 // Special case when we are targeting the max allowed rate |
| 806 if (cpi->rc.this_frame_target >= cpi->rc.max_frame_bandwidth) |
| 807 *top_index = q; |
| 808 else |
| 809 q = *top_index; |
| 810 } |
| 811 } |
| 812 #if CONFIG_MULTIPLE_ARF |
| 813 // Force the quantizer determined by the coding order pattern. |
| 814 if (cpi->multi_arf_enabled && (cm->frame_type != KEY_FRAME) && |
| 815 cpi->oxcf.end_usage != USAGE_CONSTANT_QUALITY) { |
| 816 double new_q; |
| 817 double current_q = vp9_convert_qindex_to_q(active_worst_quality); |
| 818 int level = cpi->this_frame_weight; |
| 819 assert(level >= 0); |
| 820 new_q = current_q * (1.0 - (0.2 * (cpi->max_arf_level - level))); |
| 821 q = active_worst_quality + |
| 822 vp9_compute_qdelta(cpi, current_q, new_q); |
| 823 |
| 824 *bottom_index = q; |
| 825 *top_index = q; |
| 826 printf("frame:%d q:%d\n", cm->current_video_frame, q); |
| 827 } |
| 828 #endif |
| 829 assert(*top_index <= rc->worst_quality && |
| 830 *top_index >= rc->best_quality); |
| 831 assert(*bottom_index <= rc->worst_quality && |
| 832 *bottom_index >= rc->best_quality); |
| 833 assert(q <= rc->worst_quality && q >= rc->best_quality); |
| 834 return q; |
| 835 } |
| 836 |
| 837 static int rc_pick_q_and_bounds_two_pass(const VP9_COMP *cpi, |
| 838 int *bottom_index, |
| 839 int *top_index) { |
| 840 const VP9_COMMON *const cm = &cpi->common; |
| 841 const RATE_CONTROL *const rc = &cpi->rc; |
| 842 const VP9_CONFIG *const oxcf = &cpi->oxcf; |
| 843 int active_best_quality; |
| 844 int active_worst_quality = cpi->twopass.active_worst_quality; |
| 845 int q; |
| 846 |
| 847 if (frame_is_intra_only(cm)) { |
| 848 #if !CONFIG_MULTIPLE_ARF |
| 849 // Handle the special case for key frames forced when we have75 reached |
| 850 // the maximum key frame interval. Here force the Q to a range |
| 851 // based on the ambient Q to reduce the risk of popping. |
| 852 if (rc->this_key_frame_forced) { |
| 853 int qindex = rc->last_boosted_qindex; |
| 854 double last_boosted_q = vp9_convert_qindex_to_q(qindex); |
| 855 int delta_qindex = vp9_compute_qdelta(cpi, last_boosted_q, |
| 856 (last_boosted_q * 0.75)); |
| 857 active_best_quality = MAX(qindex + delta_qindex, rc->best_quality); |
| 858 } else { |
| 859 // Not forced keyframe. |
| 860 double q_adj_factor = 1.0; |
| 861 double q_val; |
| 862 // Baseline value derived from cpi->active_worst_quality and kf boost. |
594 active_best_quality = get_active_quality(active_worst_quality, | 863 active_best_quality = get_active_quality(active_worst_quality, |
595 rc->kf_boost, | 864 rc->kf_boost, |
596 kf_low, kf_high, | 865 kf_low, kf_high, |
597 kf_low_motion_minq, | 866 kf_low_motion_minq, |
598 kf_high_motion_minq); | 867 kf_high_motion_minq); |
599 | 868 |
600 // Allow somewhat lower kf minq with small image formats. | 869 // Allow somewhat lower kf minq with small image formats. |
601 if ((cm->width * cm->height) <= (352 * 288)) { | 870 if ((cm->width * cm->height) <= (352 * 288)) { |
602 q_adj_factor -= 0.25; | 871 q_adj_factor -= 0.25; |
603 } | 872 } |
604 | 873 |
605 // Make a further adjustment based on the kf zero motion measure. | 874 // Make a further adjustment based on the kf zero motion measure. |
606 q_adj_factor += 0.05 - (0.001 * (double)cpi->twopass.kf_zeromotion_pct); | 875 q_adj_factor += 0.05 - (0.001 * (double)cpi->twopass.kf_zeromotion_pct); |
607 | 876 |
608 // Convert the adjustment factor to a qindex delta | 877 // Convert the adjustment factor to a qindex delta |
609 // on active_best_quality. | 878 // on active_best_quality. |
610 q_val = vp9_convert_qindex_to_q(active_best_quality); | 879 q_val = vp9_convert_qindex_to_q(active_best_quality); |
611 active_best_quality += vp9_compute_qdelta(cpi, q_val, q_val * | 880 active_best_quality += vp9_compute_qdelta(cpi, q_val, q_val * |
612 q_adj_factor); | 881 q_adj_factor); |
613 } | 882 } |
614 #else | 883 #else |
615 double current_q; | 884 double current_q; |
616 // Force the KF quantizer to be 30% of the active_worst_quality. | 885 // Force the KF quantizer to be 30% of the active_worst_quality. |
617 current_q = vp9_convert_qindex_to_q(active_worst_quality); | 886 current_q = vp9_convert_qindex_to_q(active_worst_quality); |
618 active_best_quality = active_worst_quality | 887 active_best_quality = active_worst_quality |
619 + vp9_compute_qdelta(cpi, current_q, current_q * 0.3); | 888 + vp9_compute_qdelta(cpi, current_q, current_q * 0.3); |
620 #endif | 889 #endif |
621 } else if (!rc->is_src_frame_alt_ref && | 890 } else if (!rc->is_src_frame_alt_ref && |
622 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) { | 891 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) { |
623 | |
624 // Use the lower of active_worst_quality and recent | 892 // Use the lower of active_worst_quality and recent |
625 // average Q as basis for GF/ARF best Q limit unless last frame was | 893 // average Q as basis for GF/ARF best Q limit unless last frame was |
626 // a key frame. | 894 // a key frame. |
627 if (rc->frames_since_key > 1 && | 895 if (rc->frames_since_key > 1 && |
628 rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) { | 896 rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) { |
629 q = rc->avg_frame_qindex[INTER_FRAME]; | 897 q = rc->avg_frame_qindex[INTER_FRAME]; |
630 } else { | 898 } else { |
631 q = active_worst_quality; | 899 q = active_worst_quality; |
632 } | 900 } |
633 // For constrained quality dont allow Q less than the cq level | 901 // For constrained quality dont allow Q less than the cq level |
(...skipping 30 matching lines...) Expand all Loading... |
664 } | 932 } |
665 } else { | 933 } else { |
666 active_best_quality = get_active_quality( | 934 active_best_quality = get_active_quality( |
667 q, rc->gfu_boost, gf_low, gf_high, | 935 q, rc->gfu_boost, gf_low, gf_high, |
668 gf_low_motion_minq, gf_high_motion_minq); | 936 gf_low_motion_minq, gf_high_motion_minq); |
669 } | 937 } |
670 } else { | 938 } else { |
671 if (oxcf->end_usage == USAGE_CONSTANT_QUALITY) { | 939 if (oxcf->end_usage == USAGE_CONSTANT_QUALITY) { |
672 active_best_quality = cpi->cq_target_quality; | 940 active_best_quality = cpi->cq_target_quality; |
673 } else { | 941 } else { |
674 if (cpi->pass == 0 && | 942 active_best_quality = inter_minq[active_worst_quality]; |
675 rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) | |
676 // 1-pass: for now, use the average Q for the active_best, if its lower | |
677 // than active_worst. | |
678 active_best_quality = inter_minq[rc->avg_frame_qindex[INTER_FRAME]]; | |
679 else | |
680 active_best_quality = inter_minq[active_worst_quality]; | |
681 | 943 |
682 // For the constrained quality mode we don't want | 944 // For the constrained quality mode we don't want |
683 // q to fall below the cq level. | 945 // q to fall below the cq level. |
684 if ((oxcf->end_usage == USAGE_CONSTRAINED_QUALITY) && | 946 if ((oxcf->end_usage == USAGE_CONSTRAINED_QUALITY) && |
685 (active_best_quality < cpi->cq_target_quality)) { | 947 (active_best_quality < cpi->cq_target_quality)) { |
686 // If we are strongly undershooting the target rate in the last | 948 // If we are strongly undershooting the target rate in the last |
687 // frames then use the user passed in cq value not the auto | 949 // frames then use the user passed in cq value not the auto |
688 // cq value. | 950 // cq value. |
689 if (rc->rolling_actual_bits < rc->min_frame_bandwidth) | 951 if (rc->rolling_actual_bits < rc->min_frame_bandwidth) |
690 active_best_quality = oxcf->cq_level; | 952 active_best_quality = oxcf->cq_level; |
691 else | 953 else |
692 active_best_quality = cpi->cq_target_quality; | 954 active_best_quality = cpi->cq_target_quality; |
693 } | 955 } |
694 } | 956 } |
695 } | 957 } |
696 | 958 |
697 // Clip the active best and worst quality values to limits | 959 // Clip the active best and worst quality values to limits. |
698 if (active_worst_quality > rc->worst_quality) | 960 if (active_worst_quality > rc->worst_quality) |
699 active_worst_quality = rc->worst_quality; | 961 active_worst_quality = rc->worst_quality; |
700 | 962 |
701 if (active_best_quality < rc->best_quality) | 963 if (active_best_quality < rc->best_quality) |
702 active_best_quality = rc->best_quality; | 964 active_best_quality = rc->best_quality; |
703 | 965 |
704 if (active_best_quality > rc->worst_quality) | 966 if (active_best_quality > rc->worst_quality) |
705 active_best_quality = rc->worst_quality; | 967 active_best_quality = rc->worst_quality; |
706 | 968 |
707 if (active_worst_quality < active_best_quality) | 969 if (active_worst_quality < active_best_quality) |
708 active_worst_quality = active_best_quality; | 970 active_worst_quality = active_best_quality; |
709 | 971 |
710 *top_index = active_worst_quality; | 972 *top_index = active_worst_quality; |
711 *bottom_index = active_best_quality; | 973 *bottom_index = active_best_quality; |
712 | 974 |
713 #if LIMIT_QRANGE_FOR_ALTREF_AND_KEY | 975 #if LIMIT_QRANGE_FOR_ALTREF_AND_KEY |
714 // Limit Q range for the adaptive loop. | 976 // Limit Q range for the adaptive loop. |
715 if (cm->frame_type == KEY_FRAME && !rc->this_key_frame_forced) { | 977 if (cm->frame_type == KEY_FRAME && !rc->this_key_frame_forced) { |
716 if (!(cpi->pass == 0 && cm->current_video_frame == 0)) | 978 *top_index = (active_worst_quality + active_best_quality * 3) / 4; |
717 *top_index = (active_worst_quality + active_best_quality * 3) / 4; | |
718 } else if (!rc->is_src_frame_alt_ref && | 979 } else if (!rc->is_src_frame_alt_ref && |
719 (oxcf->end_usage != USAGE_STREAM_FROM_SERVER) && | 980 (oxcf->end_usage != USAGE_STREAM_FROM_SERVER) && |
720 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) { | 981 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) { |
721 *top_index = (active_worst_quality + active_best_quality) / 2; | 982 *top_index = (active_worst_quality + active_best_quality) / 2; |
722 } | 983 } |
723 #endif | 984 #endif |
724 | 985 |
725 if (oxcf->end_usage == USAGE_CONSTANT_QUALITY) { | 986 if (oxcf->end_usage == USAGE_CONSTANT_QUALITY) { |
726 q = active_best_quality; | 987 q = active_best_quality; |
727 // Special case code to try and match quality with forced key frames | 988 // Special case code to try and match quality with forced key frames. |
728 } else if ((cm->frame_type == KEY_FRAME) && rc->this_key_frame_forced) { | 989 } else if ((cm->frame_type == KEY_FRAME) && rc->this_key_frame_forced) { |
729 q = rc->last_boosted_qindex; | 990 q = rc->last_boosted_qindex; |
730 } else { | 991 } else { |
731 q = vp9_rc_regulate_q(cpi, rc->this_frame_target, | 992 q = vp9_rc_regulate_q(cpi, rc->this_frame_target, |
732 active_best_quality, active_worst_quality); | 993 active_best_quality, active_worst_quality); |
733 if (q > *top_index) { | 994 if (q > *top_index) { |
734 // Special case when we are targeting the max allowed rate | 995 // Special case when we are targeting the max allowed rate. |
735 if (cpi->rc.this_frame_target >= cpi->rc.max_frame_bandwidth) | 996 if (cpi->rc.this_frame_target >= cpi->rc.max_frame_bandwidth) |
736 *top_index = q; | 997 *top_index = q; |
737 else | 998 else |
738 q = *top_index; | 999 q = *top_index; |
739 } | 1000 } |
740 } | 1001 } |
741 #if CONFIG_MULTIPLE_ARF | 1002 #if CONFIG_MULTIPLE_ARF |
742 // Force the quantizer determined by the coding order pattern. | 1003 // Force the quantizer determined by the coding order pattern. |
743 if (cpi->multi_arf_enabled && (cm->frame_type != KEY_FRAME) && | 1004 if (cpi->multi_arf_enabled && (cm->frame_type != KEY_FRAME) && |
744 cpi->oxcf.end_usage != USAGE_CONSTANT_QUALITY) { | 1005 cpi->oxcf.end_usage != USAGE_CONSTANT_QUALITY) { |
(...skipping 11 matching lines...) Expand all Loading... |
756 } | 1017 } |
757 #endif | 1018 #endif |
758 assert(*top_index <= rc->worst_quality && | 1019 assert(*top_index <= rc->worst_quality && |
759 *top_index >= rc->best_quality); | 1020 *top_index >= rc->best_quality); |
760 assert(*bottom_index <= rc->worst_quality && | 1021 assert(*bottom_index <= rc->worst_quality && |
761 *bottom_index >= rc->best_quality); | 1022 *bottom_index >= rc->best_quality); |
762 assert(q <= rc->worst_quality && q >= rc->best_quality); | 1023 assert(q <= rc->worst_quality && q >= rc->best_quality); |
763 return q; | 1024 return q; |
764 } | 1025 } |
765 | 1026 |
| 1027 int vp9_rc_pick_q_and_bounds(const VP9_COMP *cpi, |
| 1028 int *bottom_index, |
| 1029 int *top_index) { |
| 1030 int q; |
| 1031 if (cpi->pass == 0) { |
| 1032 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) |
| 1033 q = rc_pick_q_and_bounds_one_pass_cbr(cpi, bottom_index, top_index); |
| 1034 else |
| 1035 q = rc_pick_q_and_bounds_one_pass_vbr(cpi, bottom_index, top_index); |
| 1036 } else { |
| 1037 q = rc_pick_q_and_bounds_two_pass(cpi, bottom_index, top_index); |
| 1038 } |
| 1039 |
| 1040 // JBB : This is realtime mode. In real time mode the first frame |
| 1041 // should be larger. Q of 0 is disabled because we force tx size to be |
| 1042 // 16x16... |
| 1043 if (cpi->sf.use_pick_mode) { |
| 1044 if (cpi->common.current_video_frame == 0) |
| 1045 q /= 3; |
| 1046 if (q == 0) |
| 1047 q++; |
| 1048 if (q < *bottom_index) |
| 1049 *bottom_index = q; |
| 1050 else if (q > *top_index) |
| 1051 *top_index = q; |
| 1052 } |
| 1053 return q; |
| 1054 } |
| 1055 |
766 void vp9_rc_compute_frame_size_bounds(const VP9_COMP *cpi, | 1056 void vp9_rc_compute_frame_size_bounds(const VP9_COMP *cpi, |
767 int this_frame_target, | 1057 int this_frame_target, |
768 int *frame_under_shoot_limit, | 1058 int *frame_under_shoot_limit, |
769 int *frame_over_shoot_limit) { | 1059 int *frame_over_shoot_limit) { |
770 // Set-up bounds on acceptable frame size: | 1060 // Set-up bounds on acceptable frame size: |
771 if (cpi->oxcf.end_usage == USAGE_CONSTANT_QUALITY) { | 1061 if (cpi->oxcf.end_usage == USAGE_CONSTANT_QUALITY) { |
772 *frame_under_shoot_limit = 0; | 1062 *frame_under_shoot_limit = 0; |
773 *frame_over_shoot_limit = INT_MAX; | 1063 *frame_over_shoot_limit = INT_MAX; |
774 } else { | 1064 } else { |
775 if (cpi->common.frame_type == KEY_FRAME) { | 1065 if (cpi->common.frame_type == KEY_FRAME) { |
(...skipping 23 matching lines...) Expand all Loading... |
799 if (*frame_under_shoot_limit < 0) | 1089 if (*frame_under_shoot_limit < 0) |
800 *frame_under_shoot_limit = 0; | 1090 *frame_under_shoot_limit = 0; |
801 | 1091 |
802 // Clip to maximum allowed rate for a frame. | 1092 // Clip to maximum allowed rate for a frame. |
803 if (*frame_over_shoot_limit > cpi->rc.max_frame_bandwidth) { | 1093 if (*frame_over_shoot_limit > cpi->rc.max_frame_bandwidth) { |
804 *frame_over_shoot_limit = cpi->rc.max_frame_bandwidth; | 1094 *frame_over_shoot_limit = cpi->rc.max_frame_bandwidth; |
805 } | 1095 } |
806 } | 1096 } |
807 } | 1097 } |
808 | 1098 |
809 // return of 0 means drop frame | 1099 void vp9_rc_set_frame_target(VP9_COMP *cpi, int target) { |
810 int vp9_rc_pick_frame_size_target(VP9_COMP *cpi) { | |
811 const VP9_COMMON *const cm = &cpi->common; | 1100 const VP9_COMMON *const cm = &cpi->common; |
812 RATE_CONTROL *const rc = &cpi->rc; | 1101 RATE_CONTROL *const rc = &cpi->rc; |
813 | 1102 |
814 if (cm->frame_type == KEY_FRAME) | 1103 rc->this_frame_target = target; |
815 calc_iframe_target_size(cpi); | |
816 else | |
817 calc_pframe_target_size(cpi); | |
818 | |
819 // Clip the frame target to the maximum allowed value. | |
820 if (rc->this_frame_target > rc->max_frame_bandwidth) | |
821 rc->this_frame_target = rc->max_frame_bandwidth; | |
822 | |
823 // Target rate per SB64 (including partial SB64s. | 1104 // Target rate per SB64 (including partial SB64s. |
824 rc->sb64_target_rate = ((int64_t)rc->this_frame_target * 64 * 64) / | 1105 rc->sb64_target_rate = ((int64_t)rc->this_frame_target * 64 * 64) / |
825 (cm->width * cm->height); | 1106 (cm->width * cm->height); |
826 return 1; | |
827 } | 1107 } |
828 | 1108 |
829 static void update_alt_ref_frame_stats(VP9_COMP *cpi) { | 1109 static void update_alt_ref_frame_stats(VP9_COMP *cpi) { |
830 // this frame refreshes means next frames don't unless specified by user | 1110 // this frame refreshes means next frames don't unless specified by user |
831 cpi->rc.frames_since_golden = 0; | 1111 cpi->rc.frames_since_golden = 0; |
832 | 1112 |
833 #if CONFIG_MULTIPLE_ARF | 1113 #if CONFIG_MULTIPLE_ARF |
834 if (!cpi->multi_arf_enabled) | 1114 if (!cpi->multi_arf_enabled) |
835 #endif | 1115 #endif |
836 // Clear the alternate reference update pending flag. | 1116 // Clear the alternate reference update pending flag. |
(...skipping 23 matching lines...) Expand all Loading... |
860 if (rc->frames_till_gf_update_due > 0) | 1140 if (rc->frames_till_gf_update_due > 0) |
861 rc->frames_till_gf_update_due--; | 1141 rc->frames_till_gf_update_due--; |
862 | 1142 |
863 rc->frames_since_golden++; | 1143 rc->frames_since_golden++; |
864 } | 1144 } |
865 } | 1145 } |
866 | 1146 |
867 void vp9_rc_postencode_update(VP9_COMP *cpi, uint64_t bytes_used) { | 1147 void vp9_rc_postencode_update(VP9_COMP *cpi, uint64_t bytes_used) { |
868 VP9_COMMON *const cm = &cpi->common; | 1148 VP9_COMMON *const cm = &cpi->common; |
869 RATE_CONTROL *const rc = &cpi->rc; | 1149 RATE_CONTROL *const rc = &cpi->rc; |
| 1150 |
| 1151 cm->last_frame_type = cm->frame_type; |
870 // Update rate control heuristics | 1152 // Update rate control heuristics |
871 rc->projected_frame_size = (bytes_used << 3); | 1153 rc->projected_frame_size = (bytes_used << 3); |
872 | 1154 |
873 // Post encode loop adjustment of Q prediction. | 1155 // Post encode loop adjustment of Q prediction. |
874 vp9_rc_update_rate_correction_factors(cpi, (cpi->sf.recode_loop || | 1156 vp9_rc_update_rate_correction_factors( |
| 1157 cpi, (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF || |
875 cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) ? 2 : 0); | 1158 cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) ? 2 : 0); |
876 | 1159 |
877 // Keep a record of last Q and ambient average Q. | 1160 // Keep a record of last Q and ambient average Q. |
878 if (cm->frame_type == KEY_FRAME) { | 1161 if (cm->frame_type == KEY_FRAME) { |
879 rc->last_q[KEY_FRAME] = cm->base_qindex; | 1162 rc->last_q[KEY_FRAME] = cm->base_qindex; |
880 rc->avg_frame_qindex[KEY_FRAME] = ROUND_POWER_OF_TWO( | 1163 rc->avg_frame_qindex[KEY_FRAME] = ROUND_POWER_OF_TWO( |
881 3 * rc->avg_frame_qindex[KEY_FRAME] + cm->base_qindex, 2); | 1164 3 * rc->avg_frame_qindex[KEY_FRAME] + cm->base_qindex, 2); |
882 } else if (!rc->is_src_frame_alt_ref && | 1165 } else if (!rc->is_src_frame_alt_ref && |
883 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) { | 1166 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame) && |
| 1167 !(cpi->use_svc && cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)) { |
884 rc->last_q[2] = cm->base_qindex; | 1168 rc->last_q[2] = cm->base_qindex; |
885 rc->avg_frame_qindex[2] = ROUND_POWER_OF_TWO( | 1169 rc->avg_frame_qindex[2] = ROUND_POWER_OF_TWO( |
886 3 * rc->avg_frame_qindex[2] + cm->base_qindex, 2); | 1170 3 * rc->avg_frame_qindex[2] + cm->base_qindex, 2); |
887 } else { | 1171 } else { |
888 rc->last_q[INTER_FRAME] = cm->base_qindex; | 1172 rc->last_q[INTER_FRAME] = cm->base_qindex; |
889 rc->avg_frame_qindex[INTER_FRAME] = ROUND_POWER_OF_TWO( | 1173 rc->avg_frame_qindex[INTER_FRAME] = ROUND_POWER_OF_TWO( |
890 3 * rc->avg_frame_qindex[INTER_FRAME] + cm->base_qindex, 2); | 1174 3 * rc->avg_frame_qindex[INTER_FRAME] + cm->base_qindex, 2); |
891 rc->ni_frames++; | 1175 rc->ni_frames++; |
892 rc->tot_q += vp9_convert_qindex_to_q(cm->base_qindex); | 1176 rc->tot_q += vp9_convert_qindex_to_q(cm->base_qindex); |
893 rc->avg_q = rc->tot_q / (double)rc->ni_frames; | 1177 rc->avg_q = rc->tot_q / (double)rc->ni_frames; |
894 | 1178 |
895 // Calculate the average Q for normal inter frames (not key or GFU frames). | 1179 // Calculate the average Q for normal inter frames (not key or GFU frames). |
896 rc->ni_tot_qi += cm->base_qindex; | 1180 rc->ni_tot_qi += cm->base_qindex; |
897 rc->ni_av_qi = rc->ni_tot_qi / rc->ni_frames; | 1181 rc->ni_av_qi = rc->ni_tot_qi / rc->ni_frames; |
898 } | 1182 } |
899 | 1183 |
900 // Keep record of last boosted (KF/KF/ARF) Q value. | 1184 // Keep record of last boosted (KF/KF/ARF) Q value. |
901 // If the current frame is coded at a lower Q then we also update it. | 1185 // If the current frame is coded at a lower Q then we also update it. |
902 // If all mbs in this group are skipped only update if the Q value is | 1186 // If all mbs in this group are skipped only update if the Q value is |
903 // better than that already stored. | 1187 // better than that already stored. |
904 // This is used to help set quality in forced key frames to reduce popping | 1188 // This is used to help set quality in forced key frames to reduce popping |
905 if ((cm->base_qindex < rc->last_boosted_qindex) || | 1189 if ((cm->base_qindex < rc->last_boosted_qindex) || |
906 ((cpi->static_mb_pct < 100) && | 1190 ((cpi->static_mb_pct < 100) && |
907 ((cm->frame_type == KEY_FRAME) || cpi->refresh_alt_ref_frame || | 1191 ((cm->frame_type == KEY_FRAME) || cpi->refresh_alt_ref_frame || |
908 (cpi->refresh_golden_frame && !rc->is_src_frame_alt_ref)))) { | 1192 (cpi->refresh_golden_frame && !rc->is_src_frame_alt_ref)))) { |
909 rc->last_boosted_qindex = cm->base_qindex; | 1193 rc->last_boosted_qindex = cm->base_qindex; |
910 } | 1194 } |
911 | 1195 |
912 vp9_update_buffer_level(cpi, rc->projected_frame_size); | 1196 update_buffer_level(cpi, rc->projected_frame_size); |
913 | 1197 |
914 // Rolling monitors of whether we are over or underspending used to help | 1198 // Rolling monitors of whether we are over or underspending used to help |
915 // regulate min and Max Q in two pass. | 1199 // regulate min and Max Q in two pass. |
916 if (cm->frame_type != KEY_FRAME) { | 1200 if (cm->frame_type != KEY_FRAME) { |
917 rc->rolling_target_bits = ROUND_POWER_OF_TWO( | 1201 rc->rolling_target_bits = ROUND_POWER_OF_TWO( |
918 rc->rolling_target_bits * 3 + rc->this_frame_target, 2); | 1202 rc->rolling_target_bits * 3 + rc->this_frame_target, 2); |
919 rc->rolling_actual_bits = ROUND_POWER_OF_TWO( | 1203 rc->rolling_actual_bits = ROUND_POWER_OF_TWO( |
920 rc->rolling_actual_bits * 3 + rc->projected_frame_size, 2); | 1204 rc->rolling_actual_bits * 3 + rc->projected_frame_size, 2); |
921 rc->long_rolling_target_bits = ROUND_POWER_OF_TWO( | 1205 rc->long_rolling_target_bits = ROUND_POWER_OF_TWO( |
922 rc->long_rolling_target_bits * 31 + rc->this_frame_target, 5); | 1206 rc->long_rolling_target_bits * 31 + rc->this_frame_target, 5); |
923 rc->long_rolling_actual_bits = ROUND_POWER_OF_TWO( | 1207 rc->long_rolling_actual_bits = ROUND_POWER_OF_TWO( |
924 rc->long_rolling_actual_bits * 31 + rc->projected_frame_size, 5); | 1208 rc->long_rolling_actual_bits * 31 + rc->projected_frame_size, 5); |
925 } | 1209 } |
926 | 1210 |
927 // Actual bits spent | 1211 // Actual bits spent |
928 rc->total_actual_bits += rc->projected_frame_size; | 1212 rc->total_actual_bits += rc->projected_frame_size; |
929 | 1213 |
930 // Debug stats | 1214 // Debug stats |
931 rc->total_target_vs_actual += (rc->this_frame_target - | 1215 rc->total_target_vs_actual += (rc->this_frame_target - |
932 rc->projected_frame_size); | 1216 rc->projected_frame_size); |
933 | 1217 |
934 #ifndef DISABLE_RC_LONG_TERM_MEM | |
935 // Update bits left to the kf and gf groups to account for overshoot or | |
936 // undershoot on these frames | |
937 if (cm->frame_type == KEY_FRAME) { | |
938 cpi->twopass.kf_group_bits += cpi->rc.this_frame_target - | |
939 cpi->rc.projected_frame_size; | |
940 | |
941 cpi->twopass.kf_group_bits = MAX(cpi->twopass.kf_group_bits, 0); | |
942 } else if (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame) { | |
943 cpi->twopass.gf_group_bits += cpi->rc.this_frame_target - | |
944 cpi->rc.projected_frame_size; | |
945 | |
946 cpi->twopass.gf_group_bits = MAX(cpi->twopass.gf_group_bits, 0); | |
947 } | |
948 #endif | |
949 | |
950 if (cpi->oxcf.play_alternate && cpi->refresh_alt_ref_frame && | 1218 if (cpi->oxcf.play_alternate && cpi->refresh_alt_ref_frame && |
951 (cm->frame_type != KEY_FRAME)) | 1219 (cm->frame_type != KEY_FRAME)) |
952 // Update the alternate reference frame stats as appropriate. | 1220 // Update the alternate reference frame stats as appropriate. |
953 update_alt_ref_frame_stats(cpi); | 1221 update_alt_ref_frame_stats(cpi); |
954 else | 1222 else |
955 // Update the Golden frame stats as appropriate. | 1223 // Update the Golden frame stats as appropriate. |
956 update_golden_frame_stats(cpi); | 1224 update_golden_frame_stats(cpi); |
957 | 1225 |
958 if (cm->frame_type == KEY_FRAME) | 1226 if (cm->frame_type == KEY_FRAME) |
959 rc->frames_since_key = 0; | 1227 rc->frames_since_key = 0; |
960 if (cm->show_frame) { | 1228 if (cm->show_frame) { |
961 rc->frames_since_key++; | 1229 rc->frames_since_key++; |
962 rc->frames_to_key--; | 1230 rc->frames_to_key--; |
963 } | 1231 } |
964 } | 1232 } |
965 | 1233 |
966 void vp9_rc_postencode_update_drop_frame(VP9_COMP *cpi) { | 1234 void vp9_rc_postencode_update_drop_frame(VP9_COMP *cpi) { |
| 1235 // Update buffer level with zero size, update frame counters, and return. |
| 1236 update_buffer_level(cpi, 0); |
| 1237 cpi->common.last_frame_type = cpi->common.frame_type; |
967 cpi->rc.frames_since_key++; | 1238 cpi->rc.frames_since_key++; |
968 cpi->rc.frames_to_key--; | 1239 cpi->rc.frames_to_key--; |
969 } | 1240 } |
| 1241 |
| 1242 static int test_for_kf_one_pass(VP9_COMP *cpi) { |
| 1243 // Placeholder function for auto key frame |
| 1244 return 0; |
| 1245 } |
| 1246 // Use this macro to turn on/off use of alt-refs in one-pass mode. |
| 1247 #define USE_ALTREF_FOR_ONE_PASS 1 |
| 1248 |
| 1249 static int calc_pframe_target_size_one_pass_vbr(const VP9_COMP *const cpi) { |
| 1250 static const int af_ratio = 10; |
| 1251 const RATE_CONTROL *rc = &cpi->rc; |
| 1252 int target; |
| 1253 #if USE_ALTREF_FOR_ONE_PASS |
| 1254 target = (!rc->is_src_frame_alt_ref && |
| 1255 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) ? |
| 1256 (rc->av_per_frame_bandwidth * cpi->rc.baseline_gf_interval * af_ratio) / |
| 1257 (cpi->rc.baseline_gf_interval + af_ratio - 1) : |
| 1258 (rc->av_per_frame_bandwidth * cpi->rc.baseline_gf_interval) / |
| 1259 (cpi->rc.baseline_gf_interval + af_ratio - 1); |
| 1260 #else |
| 1261 target = rc->av_per_frame_bandwidth; |
| 1262 #endif |
| 1263 return vp9_rc_clamp_pframe_target_size(cpi, target); |
| 1264 } |
| 1265 |
| 1266 static int calc_iframe_target_size_one_pass_vbr(const VP9_COMP *const cpi) { |
| 1267 static const int kf_ratio = 25; |
| 1268 const RATE_CONTROL *rc = &cpi->rc; |
| 1269 int target = rc->av_per_frame_bandwidth * kf_ratio; |
| 1270 return vp9_rc_clamp_iframe_target_size(cpi, target); |
| 1271 } |
| 1272 |
| 1273 void vp9_rc_get_one_pass_vbr_params(VP9_COMP *cpi) { |
| 1274 VP9_COMMON *const cm = &cpi->common; |
| 1275 RATE_CONTROL *const rc = &cpi->rc; |
| 1276 int target; |
| 1277 if (!cpi->refresh_alt_ref_frame && |
| 1278 (cm->current_video_frame == 0 || |
| 1279 cm->frame_flags & FRAMEFLAGS_KEY || |
| 1280 rc->frames_to_key == 0 || |
| 1281 (cpi->oxcf.auto_key && test_for_kf_one_pass(cpi)))) { |
| 1282 cm->frame_type = KEY_FRAME; |
| 1283 rc->this_key_frame_forced = cm->current_video_frame != 0 && |
| 1284 rc->frames_to_key == 0; |
| 1285 rc->frames_to_key = cpi->key_frame_frequency; |
| 1286 rc->kf_boost = DEFAULT_KF_BOOST; |
| 1287 rc->source_alt_ref_active = 0; |
| 1288 } else { |
| 1289 cm->frame_type = INTER_FRAME; |
| 1290 } |
| 1291 if (rc->frames_till_gf_update_due == 0) { |
| 1292 rc->baseline_gf_interval = DEFAULT_GF_INTERVAL; |
| 1293 rc->frames_till_gf_update_due = rc->baseline_gf_interval; |
| 1294 // NOTE: frames_till_gf_update_due must be <= frames_to_key. |
| 1295 if (rc->frames_till_gf_update_due > rc->frames_to_key) |
| 1296 rc->frames_till_gf_update_due = rc->frames_to_key; |
| 1297 cpi->refresh_golden_frame = 1; |
| 1298 rc->source_alt_ref_pending = USE_ALTREF_FOR_ONE_PASS; |
| 1299 rc->gfu_boost = DEFAULT_GF_BOOST; |
| 1300 } |
| 1301 if (cm->frame_type == KEY_FRAME) |
| 1302 target = calc_iframe_target_size_one_pass_vbr(cpi); |
| 1303 else |
| 1304 target = calc_pframe_target_size_one_pass_vbr(cpi); |
| 1305 vp9_rc_set_frame_target(cpi, target); |
| 1306 } |
| 1307 |
| 1308 static int calc_pframe_target_size_one_pass_cbr(const VP9_COMP *cpi) { |
| 1309 const VP9_CONFIG *oxcf = &cpi->oxcf; |
| 1310 const RATE_CONTROL *rc = &cpi->rc; |
| 1311 const int64_t diff = oxcf->optimal_buffer_level - rc->buffer_level; |
| 1312 const int one_pct_bits = 1 + oxcf->optimal_buffer_level / 100; |
| 1313 int min_frame_target = MAX(rc->av_per_frame_bandwidth >> 4, |
| 1314 FRAME_OVERHEAD_BITS); |
| 1315 int target = rc->av_per_frame_bandwidth; |
| 1316 if (cpi->svc.number_temporal_layers > 1 && |
| 1317 cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) { |
| 1318 // Note that for layers, av_per_frame_bandwidth is the cumulative |
| 1319 // per-frame-bandwidth. For the target size of this frame, use the |
| 1320 // layer average frame size (i.e., non-cumulative per-frame-bw). |
| 1321 int current_temporal_layer = cpi->svc.temporal_layer_id; |
| 1322 const LAYER_CONTEXT *lc = &cpi->svc.layer_context[current_temporal_layer]; |
| 1323 target = lc->avg_frame_size; |
| 1324 min_frame_target = MAX(lc->avg_frame_size >> 4, FRAME_OVERHEAD_BITS); |
| 1325 } |
| 1326 if (diff > 0) { |
| 1327 // Lower the target bandwidth for this frame. |
| 1328 const int pct_low = MIN(diff / one_pct_bits, oxcf->under_shoot_pct); |
| 1329 target -= (target * pct_low) / 200; |
| 1330 } else if (diff < 0) { |
| 1331 // Increase the target bandwidth for this frame. |
| 1332 const int pct_high = MIN(-diff / one_pct_bits, oxcf->over_shoot_pct); |
| 1333 target += (target * pct_high) / 200; |
| 1334 } |
| 1335 return MAX(min_frame_target, target); |
| 1336 } |
| 1337 |
| 1338 static int calc_iframe_target_size_one_pass_cbr(const VP9_COMP *cpi) { |
| 1339 const RATE_CONTROL *rc = &cpi->rc; |
| 1340 |
| 1341 if (cpi->common.current_video_frame == 0) { |
| 1342 return cpi->oxcf.starting_buffer_level / 2; |
| 1343 } else { |
| 1344 const int initial_boost = 32; |
| 1345 int kf_boost = MAX(initial_boost, (int)(2 * cpi->output_framerate - 16)); |
| 1346 if (rc->frames_since_key < cpi->output_framerate / 2) { |
| 1347 kf_boost = (int)(kf_boost * rc->frames_since_key / |
| 1348 (cpi->output_framerate / 2)); |
| 1349 } |
| 1350 return ((16 + kf_boost) * rc->av_per_frame_bandwidth) >> 4; |
| 1351 } |
| 1352 } |
| 1353 |
| 1354 void vp9_rc_get_svc_params(VP9_COMP *cpi) { |
| 1355 VP9_COMMON *const cm = &cpi->common; |
| 1356 int target = cpi->rc.av_per_frame_bandwidth; |
| 1357 if ((cm->current_video_frame == 0) || |
| 1358 (cm->frame_flags & FRAMEFLAGS_KEY) || |
| 1359 (cpi->oxcf.auto_key && (cpi->rc.frames_since_key % |
| 1360 cpi->key_frame_frequency == 0))) { |
| 1361 cm->frame_type = KEY_FRAME; |
| 1362 cpi->rc.source_alt_ref_active = 0; |
| 1363 if (cpi->pass == 0 && cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) { |
| 1364 target = calc_iframe_target_size_one_pass_cbr(cpi); |
| 1365 } |
| 1366 } else { |
| 1367 cm->frame_type = INTER_FRAME; |
| 1368 if (cpi->pass == 0 && cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) { |
| 1369 target = calc_pframe_target_size_one_pass_cbr(cpi); |
| 1370 } |
| 1371 } |
| 1372 vp9_rc_set_frame_target(cpi, target); |
| 1373 cpi->rc.frames_till_gf_update_due = INT_MAX; |
| 1374 cpi->rc.baseline_gf_interval = INT_MAX; |
| 1375 } |
| 1376 |
| 1377 void vp9_rc_get_one_pass_cbr_params(VP9_COMP *cpi) { |
| 1378 VP9_COMMON *const cm = &cpi->common; |
| 1379 RATE_CONTROL *const rc = &cpi->rc; |
| 1380 int target; |
| 1381 if ((cm->current_video_frame == 0 || |
| 1382 cm->frame_flags & FRAMEFLAGS_KEY || |
| 1383 rc->frames_to_key == 0 || |
| 1384 (cpi->oxcf.auto_key && test_for_kf_one_pass(cpi)))) { |
| 1385 cm->frame_type = KEY_FRAME; |
| 1386 rc->this_key_frame_forced = cm->current_video_frame != 0 && |
| 1387 rc->frames_to_key == 0; |
| 1388 rc->frames_to_key = cpi->key_frame_frequency; |
| 1389 rc->kf_boost = DEFAULT_KF_BOOST; |
| 1390 rc->source_alt_ref_active = 0; |
| 1391 target = calc_iframe_target_size_one_pass_cbr(cpi); |
| 1392 } else { |
| 1393 cm->frame_type = INTER_FRAME; |
| 1394 target = calc_pframe_target_size_one_pass_cbr(cpi); |
| 1395 } |
| 1396 vp9_rc_set_frame_target(cpi, target); |
| 1397 // Don't use gf_update by default in CBR mode. |
| 1398 rc->frames_till_gf_update_due = INT_MAX; |
| 1399 rc->baseline_gf_interval = INT_MAX; |
| 1400 } |
OLD | NEW |