Clamp bit allocations in the two-pass rate control

calculate_boost_bits() and allocate_gf_group_bits() cast a 64-bit bit
count to int without checking that it fits. With a high target bitrate
and a low frame rate the value exceeds INT_MAX and the cast wraps.

For example, with a target bitrate of 2000000 kbps at one frame per
second and 4 frames, calculate_boost_bits() computes 5333333333 bits
for the key frame, which wraps to 1038366037. That leaves a group
budget of 6961633963 bits for the remaining 3 frames, and
base_frame_bits in allocate_gf_group_bits() becomes -1974422642.

The negative value is stored in gf_group->bit_allocation[] and becomes
rc->base_frame_target and rc->this_frame_target. UBSan then reports:

  pass2_strategy.c: runtime error: signed integer overflow:
  2147483647 - -1974422642 cannot be represented in type 'int'
  ratectrl.c: runtime error: left shift of negative value -987211321

Clamp both values to [0, INT_MAX] before the cast.

Change-Id: I53ed5e94256450d9a7269701f0b0fc07265eb727
diff --git a/av1/encoder/pass2_strategy.c b/av1/encoder/pass2_strategy.c
index e516d6d..95bd338 100644
--- a/av1/encoder/pass2_strategy.c
+++ b/av1/encoder/pass2_strategy.c
@@ -906,8 +906,8 @@
   }
 
   // Calculate the number of extra bits for use in the boosted frame or frames.
-  return AOMMAX((int)(((int64_t)boost * total_group_bits) / allocation_chunks),
-                0);
+  return (int)clamp64(((int64_t)boost * total_group_bits) / allocation_chunks,
+                      0, INT_MAX);
 }
 
 // Calculate the boost factor based on the number of bits assigned, i.e. the
@@ -999,7 +999,7 @@
 
   int num_frames =
       AOMMAX(1, p_rc->baseline_gf_interval - (rc->frames_since_key == 0));
-  base_frame_bits = (int)(total_group_bits / num_frames);
+  base_frame_bits = (int)clamp64(total_group_bits / num_frames, 0, INT_MAX);
 
   // Check the number of frames in each layer in case we have a
   // non standard group length.