| Index: source/libvpx/vp9/encoder/vp9_ratectrl.c | 
| =================================================================== | 
| --- source/libvpx/vp9/encoder/vp9_ratectrl.c	(revision 281795) | 
| +++ source/libvpx/vp9/encoder/vp9_ratectrl.c	(working copy) | 
| @@ -186,6 +186,8 @@ | 
| } | 
|  | 
| void vp9_rc_init(const VP9EncoderConfig *oxcf, int pass, RATE_CONTROL *rc) { | 
| +  int i; | 
| + | 
| if (pass == 0 && oxcf->rc_mode == VPX_CBR) { | 
| rc->avg_frame_qindex[KEY_FRAME] = oxcf->worst_allowed_q; | 
| rc->avg_frame_qindex[INTER_FRAME] = oxcf->worst_allowed_q; | 
| @@ -227,9 +229,9 @@ | 
| rc->tot_q = 0.0; | 
| rc->avg_q = vp9_convert_qindex_to_q(oxcf->worst_allowed_q); | 
|  | 
| -  rc->rate_correction_factor = 1.0; | 
| -  rc->key_frame_rate_correction_factor = 1.0; | 
| -  rc->gf_rate_correction_factor = 1.0; | 
| +  for (i = 0; i < RATE_FACTOR_LEVELS; ++i) { | 
| +    rc->rate_correction_factors[i] = 1.0; | 
| +  } | 
| } | 
|  | 
| int vp9_rc_drop_frame(VP9_COMP *cpi) { | 
| @@ -271,28 +273,40 @@ | 
| } | 
|  | 
| static double get_rate_correction_factor(const VP9_COMP *cpi) { | 
| +  const RATE_CONTROL *const rc = &cpi->rc; | 
| + | 
| if (cpi->common.frame_type == KEY_FRAME) { | 
| -    return cpi->rc.key_frame_rate_correction_factor; | 
| +    return rc->rate_correction_factors[KF_STD]; | 
| +  } else if (cpi->pass == 2) { | 
| +    RATE_FACTOR_LEVEL rf_lvl = | 
| +      cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index]; | 
| +    return rc->rate_correction_factors[rf_lvl]; | 
| } else { | 
| if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) && | 
| -        !cpi->rc.is_src_frame_alt_ref && | 
| +        !rc->is_src_frame_alt_ref && | 
| !(cpi->use_svc && cpi->oxcf.rc_mode == VPX_CBR)) | 
| -      return cpi->rc.gf_rate_correction_factor; | 
| +      return rc->rate_correction_factors[GF_ARF_STD]; | 
| else | 
| -      return cpi->rc.rate_correction_factor; | 
| +      return rc->rate_correction_factors[INTER_NORMAL]; | 
| } | 
| } | 
|  | 
| static void set_rate_correction_factor(VP9_COMP *cpi, double factor) { | 
| +  RATE_CONTROL *const rc = &cpi->rc; | 
| + | 
| if (cpi->common.frame_type == KEY_FRAME) { | 
| -    cpi->rc.key_frame_rate_correction_factor = factor; | 
| +    rc->rate_correction_factors[KF_STD] = factor; | 
| +  } else if (cpi->pass == 2) { | 
| +    RATE_FACTOR_LEVEL rf_lvl = | 
| +      cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index]; | 
| +    rc->rate_correction_factors[rf_lvl] = factor; | 
| } else { | 
| if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) && | 
| -        !cpi->rc.is_src_frame_alt_ref && | 
| +        !rc->is_src_frame_alt_ref && | 
| !(cpi->use_svc && cpi->oxcf.rc_mode == VPX_CBR)) | 
| -      cpi->rc.gf_rate_correction_factor = factor; | 
| +      rc->rate_correction_factors[GF_ARF_STD] = factor; | 
| else | 
| -      cpi->rc.rate_correction_factor = factor; | 
| +      rc->rate_correction_factors[INTER_NORMAL] = factor; | 
| } | 
| } | 
|  | 
| @@ -628,8 +642,8 @@ | 
|  | 
| if (frame_is_intra_only(cm)) { | 
| active_best_quality = rc->best_quality; | 
| -#if !CONFIG_MULTIPLE_ARF | 
| -    // Handle the special case for key frames forced when we have75 reached | 
| + | 
| +    // Handle the special case for key frames forced when we have reached | 
| // the maximum key frame interval. Here force the Q to a range | 
| // based on the ambient Q to reduce the risk of popping. | 
| if (rc->this_key_frame_forced) { | 
| @@ -660,13 +674,6 @@ | 
| active_best_quality += vp9_compute_qdelta(rc, q_val, | 
| q_val * q_adj_factor); | 
| } | 
| -#else | 
| -    double current_q; | 
| -    // Force the KF quantizer to be 30% of the active_worst_quality. | 
| -    current_q = vp9_convert_qindex_to_q(active_worst_quality); | 
| -    active_best_quality = active_worst_quality | 
| -        + vp9_compute_qdelta(rc, current_q, current_q * 0.3); | 
| -#endif | 
| } else if (!rc->is_src_frame_alt_ref && | 
| (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) { | 
| // Use the lower of active_worst_quality and recent | 
| @@ -768,23 +775,7 @@ | 
| q = *top_index; | 
| } | 
| } | 
| -#if CONFIG_MULTIPLE_ARF | 
| -  // Force the quantizer determined by the coding order pattern. | 
| -  if (cpi->multi_arf_enabled && (cm->frame_type != KEY_FRAME) && | 
| -      cpi->oxcf.rc_mode != VPX_Q) { | 
| -    double new_q; | 
| -    double current_q = vp9_convert_qindex_to_q(active_worst_quality); | 
| -    int level = cpi->this_frame_weight; | 
| -    assert(level >= 0); | 
| -    new_q = current_q * (1.0 - (0.2 * (cpi->max_arf_level - level))); | 
| -    q = active_worst_quality + | 
| -        vp9_compute_qdelta(rc, current_q, new_q); | 
|  | 
| -    *bottom_index = q; | 
| -    *top_index    = q; | 
| -    printf("frame:%d q:%d\n", cm->current_video_frame, q); | 
| -  } | 
| -#endif | 
| assert(*top_index <= rc->worst_quality && | 
| *top_index >= rc->best_quality); | 
| assert(*bottom_index <= rc->worst_quality && | 
| @@ -805,7 +796,6 @@ | 
| int q; | 
|  | 
| if (frame_is_intra_only(cm) || vp9_is_upper_layer_key_frame(cpi)) { | 
| -#if !CONFIG_MULTIPLE_ARF | 
| // Handle the special case for key frames forced when we have75 reached | 
| // the maximum key frame interval. Here force the Q to a range | 
| // based on the ambient Q to reduce the risk of popping. | 
| @@ -840,13 +830,6 @@ | 
| active_best_quality += vp9_compute_qdelta(rc, q_val, | 
| q_val * q_adj_factor); | 
| } | 
| -#else | 
| -    double current_q; | 
| -    // Force the KF quantizer to be 30% of the active_worst_quality. | 
| -    current_q = vp9_convert_qindex_to_q(active_worst_quality); | 
| -    active_best_quality = active_worst_quality | 
| -        + vp9_compute_qdelta(rc, current_q, current_q * 0.3); | 
| -#endif | 
| } else if (!rc->is_src_frame_alt_ref && | 
| (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) { | 
| // Use the lower of active_worst_quality and recent | 
| @@ -909,21 +892,20 @@ | 
| *bottom_index = active_best_quality; | 
|  | 
| #if LIMIT_QRANGE_FOR_ALTREF_AND_KEY | 
| +  vp9_clear_system_state(); | 
| { | 
| -    int qdelta = 0; | 
| -    vp9_clear_system_state(); | 
| - | 
| -    // Limit Q range for the adaptive loop. | 
| -    if ((cm->frame_type == KEY_FRAME || vp9_is_upper_layer_key_frame(cpi)) && | 
| -        !rc->this_key_frame_forced) { | 
| -      qdelta = vp9_compute_qdelta_by_rate(&cpi->rc, cm->frame_type, | 
| -                                          active_worst_quality, 2.0); | 
| -    } else if (!rc->is_src_frame_alt_ref && | 
| -               (oxcf->rc_mode != VPX_CBR) && | 
| -               (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) { | 
| -      qdelta = vp9_compute_qdelta_by_rate(&cpi->rc, cm->frame_type, | 
| -                                          active_worst_quality, 1.75); | 
| -    } | 
| +    const GF_GROUP *const gf_group = &cpi->twopass.gf_group; | 
| +    const double rate_factor_deltas[RATE_FACTOR_LEVELS] = { | 
| +      1.00,  // INTER_NORMAL | 
| +      1.00,  // INTER_HIGH | 
| +      1.50,  // GF_ARF_LOW | 
| +      1.75,  // GF_ARF_STD | 
| +      2.00,  // KF_STD | 
| +    }; | 
| +    const double rate_factor = | 
| +      rate_factor_deltas[gf_group->rf_level[gf_group->index]]; | 
| +    int qdelta = vp9_compute_qdelta_by_rate(&cpi->rc, cm->frame_type, | 
| +                                            active_worst_quality, rate_factor); | 
| *top_index = active_worst_quality + qdelta; | 
| *top_index = (*top_index > *bottom_index) ? *top_index : *bottom_index; | 
| } | 
| @@ -945,23 +927,7 @@ | 
| q = *top_index; | 
| } | 
| } | 
| -#if CONFIG_MULTIPLE_ARF | 
| -  // Force the quantizer determined by the coding order pattern. | 
| -  if (cpi->multi_arf_enabled && (cm->frame_type != KEY_FRAME) && | 
| -      cpi->oxcf.rc_mode != VPX_Q) { | 
| -    double new_q; | 
| -    double current_q = vp9_convert_qindex_to_q(active_worst_quality); | 
| -    int level = cpi->this_frame_weight; | 
| -    assert(level >= 0); | 
| -    new_q = current_q * (1.0 - (0.2 * (cpi->max_arf_level - level))); | 
| -    q = active_worst_quality + | 
| -        vp9_compute_qdelta(rc, current_q, new_q); | 
|  | 
| -    *bottom_index = q; | 
| -    *top_index    = q; | 
| -    printf("frame:%d q:%d\n", cm->current_video_frame, q); | 
| -  } | 
| -#endif | 
| assert(*top_index <= rc->worst_quality && | 
| *top_index >= rc->best_quality); | 
| assert(*bottom_index <= rc->worst_quality && | 
| @@ -1026,11 +992,8 @@ | 
| RATE_CONTROL *const rc = &cpi->rc; | 
| rc->frames_since_golden = 0; | 
|  | 
| -#if CONFIG_MULTIPLE_ARF | 
| -  if (!cpi->multi_arf_enabled) | 
| -#endif | 
| -    // Clear the alternate reference update pending flag. | 
| -    rc->source_alt_ref_pending = 0; | 
| +  // Mark the alt ref as done (setting to 0 means no further alt refs pending). | 
| +  rc->source_alt_ref_pending = 0; | 
|  | 
| // Set the alternate reference frame active flag | 
| rc->source_alt_ref_active = 1; | 
| @@ -1044,8 +1007,13 @@ | 
| // this frame refreshes means next frames don't unless specified by user | 
| rc->frames_since_golden = 0; | 
|  | 
| -    if (!rc->source_alt_ref_pending) | 
| +    if (cpi->pass == 2) { | 
| +      if (!rc->source_alt_ref_pending && | 
| +          cpi->twopass.gf_group.rf_level[0] == GF_ARF_STD) | 
| rc->source_alt_ref_active = 0; | 
| +    } else if (!rc->source_alt_ref_pending) { | 
| +      rc->source_alt_ref_active = 0; | 
| +    } | 
|  | 
| // Decrement count down till next gf | 
| if (rc->frames_till_gf_update_due > 0) | 
| @@ -1388,6 +1356,8 @@ | 
|  | 
| // Extended interval for genuinely static scenes | 
| rc->static_scene_max_gf_interval = oxcf->key_freq >> 1; | 
| +  if (rc->static_scene_max_gf_interval > (MAX_LAG_BUFFERS * 2)) | 
| +    rc->static_scene_max_gf_interval = MAX_LAG_BUFFERS * 2; | 
|  | 
| if (is_altref_enabled(oxcf)) { | 
| if (rc->static_scene_max_gf_interval > oxcf->lag_in_frames - 1) | 
|  |