Optimize bit stream buffer allocation size

Bit stream buffer size is calculated by checking
when no-show frames are not used.

BUG=aomedia:2938

Change-Id: I5866c42221b7b7f49ebf6840428589b3a4d78cd9
diff --git a/av1/av1_cx_iface.c b/av1/av1_cx_iface.c
index 9c098b1..d1b0fea 100644
--- a/av1/av1_cx_iface.c
+++ b/av1/av1_cx_iface.c
@@ -2151,8 +2151,22 @@
   if (img != NULL) {
     res = validate_img(ctx, img);
     if (res == AOM_CODEC_OK) {
-      size_t data_sz = ALIGN_POWER_OF_TWO(ctx->cfg.g_w, 5) *
-                       ALIGN_POWER_OF_TWO(ctx->cfg.g_h, 5) * get_image_bps(img);
+      // Worst case buffer size needed to hold bit stream corresponding
+      // to one frame.
+      const size_t frame_sz = ALIGN_POWER_OF_TWO(ctx->cfg.g_w, 5) *
+                              ALIGN_POWER_OF_TWO(ctx->cfg.g_h, 5) *
+                              get_image_bps(img) / 8;
+
+      // Due to the presence of no-show frames, bit stream buffer constitutes
+      // data corresponding to multiple frames. Hence bit stream buffer is
+      // allocated with a smaller size for all intra frame encoding with no
+      // forward key frames, as no-show frames are not possible in this case.
+      int max_num_frames = 8;
+      if (cpi->oxcf.kf_cfg.key_freq_max == 0 &&
+          !cpi->oxcf.kf_cfg.fwd_kf_enabled)
+        max_num_frames = 1;
+      size_t data_sz = frame_sz * max_num_frames;
+
       if (data_sz < kMinCompressedSize) data_sz = kMinCompressedSize;
       if (ctx->cx_data == NULL || ctx->cx_data_sz < data_sz) {
         ctx->cx_data_sz = data_sz;