Fix double clamping bug in 2 pass rate control.

In the two pass rate allocation the VBR min and max rates
are applied through normalization of the per frame error scores.

Hence when the bits for an ARF/GF group are calculated,
the contributions from each frame are already clamped to the
required VBR range.

After the total number of bits for the group (based on the
frames within that group) have been allocated, a proportion
of the bits from each normal frame are then diverted to boost
the ARF/GFs.

Prior to this fix, the rate for each frame was clamped again
after the redistribution to the ARF/GF which can fundamentally
break the rate control if two_pass_vbrmin_section is non 0.

For example, the default VBR settings used in the lowres test run
have VBRmin and VBRmax section values of 0 and 2000. This works fine
because the min value is 0. However, if the min value is changed to 50
then the rate control breaks down completely and there is a huge hit to
metrics.

STATS_CHANGED:

For example in one of my test runs I saw.
Av PSNR,     Ov PSNR,    SSIM,         PSNR HVS
+25.65%     +63.66%   +38.36%    +29.4%

In fact for overall PSNR AV1 run drops below VP9.
After the patch the metrics impact is minimal.

Av PSNR,     Ov PSNR,    SSIM,         PSNR HVS
-0.0003       0.0003,      -0.0002      -0.0002

Using VBR defaults of 50 min and 150 max in my YT rates test set the
impact is also minimal.


Change-Id: I5936880706d2376e3cb03d241ace5b9eca9de99b
diff --git a/av1/encoder/pass2_strategy.c b/av1/encoder/pass2_strategy.c
index ec90f6f..3e5f17f 100644
--- a/av1/encoder/pass2_strategy.c
+++ b/av1/encoder/pass2_strategy.c
@@ -1826,7 +1826,7 @@
     twopass->fr_content_type = FC_NORMAL;
 }
 
-static void setup_target_rate(AV1_COMP *cpi, FRAME_TYPE frame_type) {
+static void setup_target_rate(AV1_COMP *cpi) {
   RATE_CONTROL *const rc = &cpi->rc;
   GF_GROUP *const gf_group = &cpi->gf_group;
 
@@ -1835,13 +1835,6 @@
   if (cpi->oxcf.pass == 0) {
     av1_rc_set_frame_target(cpi, target_rate, cpi->common.width,
                             cpi->common.height);
-  } else {
-    if (frame_type == KEY_FRAME) {
-      target_rate = av1_rc_clamp_iframe_target_size(cpi, target_rate);
-    } else {
-      target_rate = av1_rc_clamp_pframe_target_size(
-          cpi, target_rate, gf_group->update_type[gf_group->index]);
-    }
   }
 
   rc->base_frame_target = target_rate;
@@ -1860,7 +1853,7 @@
     assert(gf_group->index < gf_group->size);
     const int update_type = gf_group->update_type[gf_group->index];
 
-    setup_target_rate(cpi, frame_params->frame_type);
+    setup_target_rate(cpi);
 
     // If this is an arf frame then we dont want to read the stats file or
     // advance the input pointer as we already have what we need.
@@ -1936,7 +1929,7 @@
     cpi->partition_search_skippable_frame = is_skippable_frame(cpi);
   }
 
-  setup_target_rate(cpi, frame_params->frame_type);
+  setup_target_rate(cpi);
 }
 
 void av1_init_second_pass(AV1_COMP *cpi) {