Fixed issues with forcing I and IDR frames.

Setting I frame interval to 1 caused encoder to go into
infinite loop. Fixed that by stopping RC update.

Forcing of I and IDR frame did not cause the frames at expected time
due to delayed encoding. This is fixed now.

Change-Id: Ibf549fb335c053ee33de768455b20e46be3e7c1c
diff --git a/encoder/ih264e_structs.h b/encoder/ih264e_structs.h
index 5d7fc6f..fc61277 100644
--- a/encoder/ih264e_structs.h
+++ b/encoder/ih264e_structs.h
@@ -2622,6 +2622,10 @@
      */
     inp_buf_t as_inp_list[MAX_NUM_BFRAMES];
 
+    /**
+     * IDR flags for each input
+     */
+    WORD32 i4_idr_inp_list[MAX_NUM_BFRAMES];
 
     /*
     *Flag to indicate if we have recived the last input frame
diff --git a/encoder/ih264e_utils.c b/encoder/ih264e_utils.c
index d6a67b6..6feb036 100644
--- a/encoder/ih264e_utils.c
+++ b/encoder/ih264e_utils.c
@@ -245,12 +245,35 @@
     /***************************************************************************
      * Now we should add the picture to RC stack here
      **************************************************************************/
+    /*
+     * If an I frame has been requested, ask  RC to force it
+     * For IDR requests, we have to ask RC to force I and set IDR by our selves
+     * since RC Donot know about IDR. For forcing an IDR at dequeue stage we
+     * should record that an IDR has been requested some where. Hence we will
+     * store it in the u4_idr_inp_list at a position same as that of input frame
+     */
+    {
+        WORD32 i4_force_idr, i4_force_i;
+
+        i4_force_idr = (ps_codec->force_curr_frame_type == IV_IDR_FRAME);
+        i4_force_idr |= !(ps_codec->i4_pic_cnt % ps_codec->s_cfg.u4_idr_frm_interval);
+
+        i4_force_i = (ps_codec->force_curr_frame_type == IV_I_FRAME);
+
+        ps_codec->i4_idr_inp_list[ps_codec->i4_pic_cnt % MAX_NUM_BFRAMES] = i4_force_idr;
+
+        if ((ps_codec->i4_frame_num > 0) && (i4_force_idr || i4_force_i))
+        {
+            irc_force_I_frame(ps_codec->s_rate_control.pps_rate_control_api);
+        }
+        ps_codec->force_curr_frame_type = IV_NA_FRAME;
+    }
+
     irc_add_picture_to_stack(ps_codec->s_rate_control.pps_rate_control_api,
                              ps_codec->i4_pic_cnt);
 
-    /*
-     * Rc has a problem with this delayed processing
-     */
+
+    /* Delay */
     if (ps_codec->i4_encode_api_call_cnt
                     < (WORD32)(ps_codec->s_cfg.u4_num_bframes))
     {
@@ -261,18 +284,6 @@
     /***************************************************************************
      * Get a new pic to encode
      **************************************************************************/
-
-    /*
-     * If a frame is forced, apply it
-     * We cannot force an I frame for first frame
-     */
-    if ((ps_codec->i4_frame_num > 0)&&
-        ((ps_codec->force_curr_frame_type == IV_I_FRAME)||
-         (ps_codec->force_curr_frame_type == IV_IDR_FRAME)))
-    {
-        irc_force_I_frame(ps_codec->s_rate_control.pps_rate_control_api);
-    }
-
     /* Query the picture_type */
     e_pictype = ih264e_rc_get_picture_details(
                     ps_codec->s_rate_control.pps_rate_control_api, (WORD32 *)(&u4_pic_id),
@@ -295,12 +306,12 @@
             return 0;
     }
 
+    /* Set IDR if it has been requested or its the IDR interval */
+    ps_codec->pic_type = ps_codec->i4_idr_inp_list[u4_pic_id % MAX_NUM_BFRAMES] ?
+                                    PIC_IDR : ps_codec->pic_type;
+    ps_codec->i4_idr_inp_list[u4_pic_id % MAX_NUM_BFRAMES] = 0;
 
-    ps_codec->pic_type = ( (u4_pic_id % ps_codec->s_cfg.u4_idr_frm_interval) ||
-                           (ps_codec->force_curr_frame_type != IV_IDR_FRAME) ) ?
-                                    ps_codec->pic_type : PIC_IDR;
 
-    ps_codec->force_curr_frame_type = IV_NA_FRAME;
 
     /* Get current frame Qp */
     u1_frame_qp = (UWORD8)irc_get_frame_level_qp(
diff --git a/encoder/irc_picture_type.c b/encoder/irc_picture_type.c
index 949ffb9..2a91572 100644
--- a/encoder/irc_picture_type.c
+++ b/encoder/irc_picture_type.c
@@ -916,12 +916,9 @@
     }
 
     /* Updating the vars which work on the encoded pics */
-    /* For the first gop
-     * TODO (BPIC) this  //  || (i4_intra_frm_int == 1)) may cause problems for bpics */
-    if(((ps_pic_handling->i4_is_first_gop)
-                    && (ps_pic_handling->i4_pic_disp_order_no
-                                    == (i4_max_inter_frm_int - 2))))
-                 //   || (i4_intra_frm_int == 1))
+    /* For the first gop */
+    if ((ps_pic_handling->i4_is_first_gop)
+                    && (ps_pic_handling->i4_pic_disp_order_no == 0))
     {
         ps_pic_handling->i4_coded_pic_no = 0;
         ps_pic_handling->i4_stack_count = 0;