Fix the side frame conditional coding rules.

b24e5746 introduced changes to LastGainIndex which broke
 conditional coding for side frames after a mid-only frame (i.e.,
 in a 60 ms frame where the side is coded, not coded, then coded
 again).
These rules were a mess in general, however, because the side
 channel state kept a different nFramesDecoded count from the mid
 channel state, and had no way to tell if the prior side frame was
 coded.

This patch attempts to rationalize them by moving the conditional
 coding decision up to the top level, where all this information is
 available.
The first coded side frame after an uncoded side frame now always
 uses independent coding.
If such a frame is also not the first side frame in an Opus frame,
 then it doesn't include an LTP scaling parameter (because the LTP
 state is well-defined).
diff --git a/silk/dec_API.c b/silk/dec_API.c
index ade060f..018173e 100644
--- a/silk/dec_API.c
+++ b/silk/dec_API.c
@@ -182,13 +182,21 @@
                 for( n = 0; n < decControl->nChannelsInternal; n++ ) {
                     if( channel_state[ n ].LBRR_flags[ i ] ) {
                         opus_int pulses[ MAX_FRAME_LENGTH ];
+                        opus_int condCoding;
+
                         if( decControl->nChannelsInternal == 2 && n == 0 ) {
                             silk_stereo_decode_pred( psRangeDec, MS_pred_Q13 );
                             if( channel_state[ 1 ].LBRR_flags[ i ] == 0 ) {
                                 silk_stereo_decode_mid_only( psRangeDec, &decode_only_middle );
                             }
                         }
-                        silk_decode_indices( &channel_state[ n ], psRangeDec, i, 1 );
+                        /* Use conditional coding if previous frame available */
+                        if( i > 0 && channel_state[ n ].LBRR_flags[ i - 1 ] ) {
+                            condCoding = CODE_CONDITIONALLY;
+                        } else {
+                            condCoding = CODE_INDEPENDENTLY;
+                        }
+                        silk_decode_indices( &channel_state[ n ], psRangeDec, i, 1, condCoding );
                         silk_decode_pulses( psRangeDec, pulses, channel_state[ n ].indices.signalType,
                             channel_state[ n ].indices.quantOffsetType, channel_state[ n ].frame_length );
                     }
@@ -230,7 +238,23 @@
     /* Call decoder for one frame */
     for( n = 0; n < decControl->nChannelsInternal; n++ ) {
         if( n == 0 || ( ( lostFlag != FLAG_PACKET_LOST ? decode_only_middle : psDec->prev_decode_only_middle ) == 0 ) ) {
-            ret += silk_decode_frame( &channel_state[ n ], psRangeDec, &samplesOut1_tmp[ n ][ 2 + delay ], &nSamplesOutDec, lostFlag );
+            opus_int FrameIndex;
+            opus_int condCoding;
+
+            FrameIndex = channel_state[ 0 ].nFramesDecoded - n;
+            /* Use independent coding if no previous frame available */
+            if( FrameIndex <= 0 ) {
+                condCoding = CODE_INDEPENDENTLY;
+            } else if( lostFlag == FLAG_DECODE_LBRR ) {
+                condCoding = channel_state[ n ].LBRR_flags[ FrameIndex - 1 ] ? CODE_CONDITIONALLY : CODE_INDEPENDENTLY;
+            } else if( n > 0 && psDec->prev_decode_only_middle ) {
+                /* If we skipped a side frame in this packet, we don't
+                   need LTP scaling; the LTP state is well-defined. */
+                condCoding = CODE_INDEPENDENTLY_NO_LTP_SCALING;
+            } else {
+                condCoding = CODE_CONDITIONALLY;
+            }
+            ret += silk_decode_frame( &channel_state[ n ], psRangeDec, &samplesOut1_tmp[ n ][ 2 + delay ], &nSamplesOutDec, lostFlag, condCoding);
         } else {
             silk_memset( &samplesOut1_tmp[ n ][ 2 + delay ], 0, nSamplesOutDec * sizeof( opus_int16 ) );
         }
diff --git a/silk/decode_frame.c b/silk/decode_frame.c
index 2478617..54bb920 100644
--- a/silk/decode_frame.c
+++ b/silk/decode_frame.c
@@ -40,7 +40,8 @@
     ec_dec                      *psRangeDec,        /* I/O  Compressor data structure                   */
     opus_int16                   pOut[],             /* O    Pointer to output speech frame              */
     opus_int32                   *pN,                /* O    Pointer to size of output frame             */
-    opus_int                     lostFlag            /* I    0: no loss, 1 loss, 2 decode fec            */
+    opus_int                     lostFlag,           /* I    0: no loss, 1 loss, 2 decode fec            */
+    opus_int                     condCoding          /* I    The type of conditional coding to use       */
 )
 {
     silk_decoder_control sDecCtrl;
@@ -62,7 +63,7 @@
         /* Decode quantization indices of side info  */
         /*********************************************/
 TIC(decode_indices)
-        silk_decode_indices( psDec, psRangeDec, psDec->nFramesDecoded, lostFlag );
+        silk_decode_indices( psDec, psRangeDec, psDec->nFramesDecoded, lostFlag, condCoding );
 TOC(decode_indices)
 
         /*********************************************/
@@ -77,7 +78,7 @@
         /* Decode parameters and pulse signal       */
         /********************************************/
 TIC(decode_params)
-        silk_decode_parameters( psDec, &sDecCtrl );
+        silk_decode_parameters( psDec, &sDecCtrl, condCoding );
 TOC(decode_params)
 
         /* Update length. Sampling frequency may have changed */
diff --git a/silk/decode_indices.c b/silk/decode_indices.c
index 9ea0a80..8ac53f4 100644
--- a/silk/decode_indices.c
+++ b/silk/decode_indices.c
@@ -36,21 +36,15 @@
     silk_decoder_state      *psDec,             /* I/O  State                                       */
     ec_dec                      *psRangeDec,        /* I/O  Compressor data structure                   */
     opus_int                     FrameIndex,         /* I    Frame number                                */
-    opus_int                     decode_LBRR         /* I    Flag indicating LBRR data is being decoded  */
+    opus_int                     decode_LBRR,        /* I    Flag indicating LBRR data is being decoded  */
+    opus_int                     condCoding          /* I    The type of conditional coding to use       */
 )
 {
-    opus_int   i, k, Ix, condCoding;
+    opus_int   i, k, Ix;
     opus_int   decode_absolute_lagIndex, delta_lagIndex;
     opus_int16 ec_ix[ MAX_LPC_ORDER ];
     opus_uint8 pred_Q8[ MAX_LPC_ORDER ];
 
-    /* Use conditional coding if previous frame available */
-    if( FrameIndex > 0 && ( decode_LBRR == 0 || psDec->LBRR_flags[ FrameIndex - 1 ] == 1 ) ) {
-        condCoding = 1;
-    } else {
-        condCoding = 0;
-    }
-
     /*******************************************/
     /* Decode signal type and quantizer offset */
     /*******************************************/
@@ -66,7 +60,7 @@
     /* Decode gains */
     /****************/
     /* First subframe */
-    if( condCoding ) {
+    if( condCoding == CODE_CONDITIONALLY ) {
         /* Conditional coding */
         psDec->indices.GainsIndices[ 0 ] = (opus_int8)ec_dec_icdf( psRangeDec, silk_delta_gain_iCDF, 8 );
     } else {
@@ -110,7 +104,7 @@
         /*********************/
         /* Get lag index */
         decode_absolute_lagIndex = 1;
-        if( condCoding && psDec->ec_prevSignalType == TYPE_VOICED ) {
+        if( condCoding == CODE_CONDITIONALLY && psDec->ec_prevSignalType == TYPE_VOICED ) {
             /* Decode Delta index */
             delta_lagIndex = (opus_int16)ec_dec_icdf( psRangeDec, silk_pitch_delta_iCDF, 8 );
             if( delta_lagIndex > 0 ) {
@@ -142,7 +136,7 @@
         /**********************/
         /* Decode LTP scaling */
         /**********************/
-        if( !condCoding ) {
+        if( condCoding == CODE_INDEPENDENTLY ) {
             psDec->indices.LTP_scaleIndex = (opus_int8)ec_dec_icdf( psRangeDec, silk_LTPscale_iCDF, 8 );
         } else {
             psDec->indices.LTP_scaleIndex = 0;
diff --git a/silk/decode_parameters.c b/silk/decode_parameters.c
index ad4788f..1ab8ed7 100644
--- a/silk/decode_parameters.c
+++ b/silk/decode_parameters.c
@@ -34,7 +34,8 @@
 /* Decode parameters from payload */
 void silk_decode_parameters(
     silk_decoder_state      *psDec,                             /* I/O  State                                    */
-    silk_decoder_control    *psDecCtrl                          /* I/O  Decoder control                          */
+    silk_decoder_control    *psDecCtrl,                         /* I/O  Decoder control                          */
+    opus_int                 condCoding                         /* I    The type of conditional coding to use    */
 )
 {
     opus_int   i, k, Ix;
@@ -43,7 +44,7 @@
 
     /* Dequant Gains */
     silk_gains_dequant( psDecCtrl->Gains_Q16, psDec->indices.GainsIndices,
-        &psDec->LastGainIndex, psDec->nFramesDecoded, psDec->nb_subfr );
+        &psDec->LastGainIndex, condCoding == CODE_CONDITIONALLY, psDec->nb_subfr );
 
     /****************/
     /* Decode NLSFs */
diff --git a/silk/define.h b/silk/define.h
index f56ca2d..8f9935f 100644
--- a/silk/define.h
+++ b/silk/define.h
@@ -66,6 +66,11 @@
 #define TYPE_UNVOICED                           1
 #define TYPE_VOICED                             2
 
+/* Conditional coding types */
+#define CODE_INDEPENDENTLY                      0
+#define CODE_INDEPENDENTLY_NO_LTP_SCALING       1
+#define CODE_CONDITIONALLY                      2
+
 /* Settings for stereo processing */
 #define STEREO_QUANT_TAB_SIZE                   16
 #define STEREO_QUANT_SUB_STEPS                  5
diff --git a/silk/enc_API.c b/silk/enc_API.c
index 339dafc..7177d4d 100644
--- a/silk/enc_API.c
+++ b/silk/enc_API.c
@@ -330,6 +330,8 @@
                 for( i = 0; i < psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket; i++ ) {
                     for( n = 0; n < encControl->nChannelsInternal; n++ ) {
                         if( psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i ] ) {
+                            opus_int condCoding;
+
                             if( encControl->nChannelsInternal == 2 && n == 0 ) {
                                 silk_stereo_encode_pred( psRangeEnc, psEnc->sStereo.predIx[ i ] );
                                 /* For LBRR data there's no need to code the mid-only flag if the side-channel LBRR flag is set */
@@ -337,7 +339,13 @@
                                     silk_stereo_encode_mid_only( psRangeEnc, psEnc->sStereo.mid_only_flags[ i ] );
                                 }
                             }
-                            silk_encode_indices( &psEnc->state_Fxx[ n ].sCmn, psRangeEnc, i, 1 );
+                            /* Use conditional coding if previous frame available */
+                            if( i > 0 && psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i - 1 ] ) {
+                                condCoding = CODE_CONDITIONALLY;
+                            } else {
+                                condCoding = CODE_INDEPENDENTLY;
+                            }
+                            silk_encode_indices( &psEnc->state_Fxx[ n ].sCmn, psRangeEnc, i, 1, condCoding );
                             silk_encode_pulses( psRangeEnc, psEnc->state_Fxx[ n ].sCmn.indices_LBRR[i].signalType, psEnc->state_Fxx[ n ].sCmn.indices_LBRR[i].quantOffsetType,
                                 psEnc->state_Fxx[ n ].sCmn.pulses_LBRR[ i ], psEnc->state_Fxx[ n ].sCmn.frame_length );
                         }
@@ -400,7 +408,6 @@
                 psEnc->state_Fxx[ 1 ].sCmn.prevSignalType         = TYPE_NO_VOICE_ACTIVITY;
                 psEnc->state_Fxx[ 1 ].sCmn.sNSQ.prev_inv_gain_Q16 = 65536;
             }
-            psEnc->prev_decode_only_middle = psEnc->sStereo.mid_only_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ];
 
             /* Encode */
             for( n = 0; n < encControl->nChannelsInternal; n++ ) {
@@ -411,9 +418,21 @@
                 }
 
                 if( channelRate_bps > 0 ) {
+                    opus_int condCoding;
+
                     silk_control_SNR( &psEnc->state_Fxx[ n ].sCmn, channelRate_bps );
 
-                    if( ( ret = silk_encode_frame_Fxx( &psEnc->state_Fxx[ n ], nBytesOut, psRangeEnc ) ) != 0 ) {
+                    /* Use independent coding if no previous frame available */
+                    if( psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded - n <= 0 ) {
+                        condCoding = CODE_INDEPENDENTLY;
+                    } else if( n > 0 && psEnc->prev_decode_only_middle ) {
+                        /* If we skipped a side frame in this packet, we don't
+                           need LTP scaling; the LTP state is well-defined. */
+                        condCoding = CODE_INDEPENDENTLY_NO_LTP_SCALING;
+                    } else {
+                        condCoding = CODE_CONDITIONALLY;
+                    }
+                    if( ( ret = silk_encode_frame_Fxx( &psEnc->state_Fxx[ n ], nBytesOut, psRangeEnc, condCoding ) ) != 0 ) {
                         silk_assert( 0 );
                     }
                     psEnc->state_Fxx[ n ].sCmn.nFramesEncoded++;
@@ -421,6 +440,7 @@
                 psEnc->state_Fxx[ n ].sCmn.controlled_since_last_payload = 0;
                 psEnc->state_Fxx[ n ].sCmn.inputBufIx = 0;
             }
+            psEnc->prev_decode_only_middle = psEnc->sStereo.mid_only_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded - 1 ];
 
             /* Insert VAD and FEC flags at beginning of bitstream */
             if( *nBytesOut > 0 && psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded == psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket) {
diff --git a/silk/encode_indices.c b/silk/encode_indices.c
index 6d8e82a..f87de03 100644
--- a/silk/encode_indices.c
+++ b/silk/encode_indices.c
@@ -36,10 +36,11 @@
     silk_encoder_state          *psEncC,            /* I/O  Encoder state                               */
     ec_enc                      *psRangeEnc,        /* I/O  Compressor data structure                   */
     opus_int                     FrameIndex,         /* I    Frame number                                */
-    opus_int                     encode_LBRR         /* I    Flag indicating LBRR data is being encoded  */
+    opus_int                     encode_LBRR,        /* I    Flag indicating LBRR data is being encoded  */
+    opus_int                     condCoding          /* I    The type of conditional coding to use      */
 )
 {
-    opus_int   i, k, condCoding, typeOffset;
+    opus_int   i, k, typeOffset;
     opus_int   encode_absolute_lagIndex, delta_lagIndex;
     opus_int16 ec_ix[ MAX_LPC_ORDER ];
     opus_uint8 pred_Q8[ MAX_LPC_ORDER ];
@@ -49,13 +50,6 @@
     opus_int nBytes_after, nBytes_before;
 #endif
 
-    /* Use conditional coding if previous frame available */
-    if( FrameIndex > 0 && ( encode_LBRR == 0 || psEncC->LBRR_flags[ FrameIndex - 1 ] == 1 ) ) {
-        condCoding = 1;
-    } else {
-        condCoding = 0;
-    }
-
     if( encode_LBRR ) {
          psIndices = &psEncC->indices_LBRR[ FrameIndex ];
     } else {
@@ -81,7 +75,7 @@
     nBytes_before = silk_RSHIFT( ec_tell( psRangeEnc ) + 7, 3 );
 #endif
     /* first subframe */
-    if( condCoding ) {
+    if( condCoding == CODE_CONDITIONALLY ) {
         /* conditional coding */
         silk_assert( psIndices->GainsIndices[ 0 ] >= 0 && psIndices->GainsIndices[ 0 ] < MAX_DELTA_GAIN_QUANT - MIN_DELTA_GAIN_QUANT + 1 );
         ec_enc_icdf( psRangeEnc, psIndices->GainsIndices[ 0 ], silk_delta_gain_iCDF, 8 );
@@ -148,7 +142,7 @@
 #endif
         /* lag index */
         encode_absolute_lagIndex = 1;
-        if( condCoding && psEncC->ec_prevSignalType == TYPE_VOICED ) {
+        if( condCoding == CODE_CONDITIONALLY && psEncC->ec_prevSignalType == TYPE_VOICED ) {
             /* Delta Encoding */
             delta_lagIndex = psIndices->lagIndex - psEncC->ec_prevLagIndex;
             if( delta_lagIndex < -8 || delta_lagIndex > 11 ) {
@@ -212,7 +206,7 @@
         /**********************/
         /* Encode LTP scaling */
         /**********************/
-        if( !condCoding ) {
+        if( condCoding == CODE_INDEPENDENTLY ) {
             silk_assert( psIndices->LTP_scaleIndex >= 0 && psIndices->LTP_scaleIndex < 3 );
             ec_enc_icdf( psRangeEnc, psIndices->LTP_scaleIndex, silk_LTPscale_iCDF, 8 );
         }
diff --git a/silk/fixed/LTP_scale_ctrl_FIX.c b/silk/fixed/LTP_scale_ctrl_FIX.c
index 463cf73..5e85cc7 100644
--- a/silk/fixed/LTP_scale_ctrl_FIX.c
+++ b/silk/fixed/LTP_scale_ctrl_FIX.c
@@ -33,7 +33,9 @@
 
 void silk_LTP_scale_ctrl_FIX(
     silk_encoder_state_FIX      *psEnc,     /* I/O  encoder state FIX                           */
-    silk_encoder_control_FIX    *psEncCtrl  /* I/O  encoder control FIX                         */
+    silk_encoder_control_FIX    *psEncCtrl, /* I/O  encoder control FIX                         */
+    opus_int                     condCoding /* I    The type of conditional coding to use       */
+
 )
 {
     opus_int round_loss;
@@ -44,7 +46,7 @@
     psEnc->prevLTPredCodGain_Q7 = psEncCtrl->LTPredCodGain_Q7;
 
     /* Only scale if first frame in packet */
-    if( psEnc->sCmn.nFramesEncoded == 0 ) {
+    if( condCoding == CODE_INDEPENDENTLY ) {
         round_loss = psEnc->sCmn.PacketLoss_perc + psEnc->sCmn.nFramesPerPacket - 1;
         psEnc->sCmn.indices.LTP_scaleIndex = (opus_int8)silk_LIMIT(
             silk_SMULWB( silk_SMULBB( round_loss, psEnc->HPLTPredCodGain_Q7 ), SILK_FIX_CONST( 0.1, 9 ) ), 0, 2 );
diff --git a/silk/fixed/encode_frame_FIX.c b/silk/fixed/encode_frame_FIX.c
index 6f4367f..c3da9bc 100644
--- a/silk/fixed/encode_frame_FIX.c
+++ b/silk/fixed/encode_frame_FIX.c
@@ -38,7 +38,8 @@
 opus_int silk_encode_frame_FIX(
     silk_encoder_state_FIX          *psEnc,             /* I/O  Encoder state FIX                       */
     opus_int32                       *pnBytesOut,        /*   O  Number of payload bytes                 */
-    ec_enc                          *psRangeEnc         /* I/O  compressor data structure               */
+    ec_enc                          *psRangeEnc,        /* I/O  compressor data structure               */
+    opus_int                         condCoding         /* I    The type of conditional coding to use   */
 )
 {
     silk_encoder_control_FIX sEncCtrl;
@@ -116,7 +117,7 @@
     /* Find linear prediction coefficients (LPC + LTP) */
     /***************************************************/
 TIC(FIND_PRED_COEF)
-    silk_find_pred_coefs_FIX( psEnc, &sEncCtrl, res_pitch, x_frame );
+    silk_find_pred_coefs_FIX( psEnc, &sEncCtrl, res_pitch, x_frame, condCoding );
 TOC(FIND_PRED_COEF)
 
     /****************************************/
@@ -137,7 +138,7 @@
     /* Low Bitrate Redundant Encoding       */
     /****************************************/
 TIC(LBRR)
-    silk_LBRR_encode_FIX( psEnc, &sEncCtrl, xfw );
+    silk_LBRR_encode_FIX( psEnc, &sEncCtrl, xfw, condCoding );
 TOC(LBRR)
 
     /*****************************************/
@@ -174,7 +175,7 @@
     /* Encode Parameters                    */
     /****************************************/
 TIC(ENCODE_PARAMS)
-    silk_encode_indices( &psEnc->sCmn, psRangeEnc, psEnc->sCmn.nFramesEncoded, 0 );
+    silk_encode_indices( &psEnc->sCmn, psRangeEnc, psEnc->sCmn.nFramesEncoded, 0, condCoding );
 TOC(ENCODE_PARAMS)
 
     /****************************************/
@@ -242,7 +243,8 @@
 void silk_LBRR_encode_FIX(
     silk_encoder_state_FIX          *psEnc,         /* I/O  Pointer to Silk FIX encoder state           */
     silk_encoder_control_FIX        *psEncCtrl,     /* I/O  Pointer to Silk FIX encoder control struct  */
-    const opus_int16                 xfw[]           /* I    Input signal                                */
+    const opus_int16                 xfw[],          /* I    Input signal                                */
+    opus_int                         condCoding     /* I    The type of conditional coding used so far for this frame */
 )
 {
     opus_int32   TempGains_Q16[ MAX_NB_SUBFR ];
@@ -274,7 +276,7 @@
         /* Decode to get gains in sync with decoder         */
         /* Overwrite unquantized gains with quantized gains */
         silk_gains_dequant( psEncCtrl->Gains_Q16, psIndices_LBRR->GainsIndices,
-            &psEnc->sCmn.LBRRprevLastGainIndex, psEnc->sCmn.nFramesEncoded, psEnc->sCmn.nb_subfr );
+            &psEnc->sCmn.LBRRprevLastGainIndex, condCoding == CODE_CONDITIONALLY, psEnc->sCmn.nb_subfr );
 
         /*****************************************/
         /* Noise shaping quantization            */
diff --git a/silk/fixed/find_pred_coefs_FIX.c b/silk/fixed/find_pred_coefs_FIX.c
index 980e321..e9891cd 100644
--- a/silk/fixed/find_pred_coefs_FIX.c
+++ b/silk/fixed/find_pred_coefs_FIX.c
@@ -35,7 +35,8 @@
     silk_encoder_state_FIX          *psEnc,         /* I/O  encoder state                               */
     silk_encoder_control_FIX        *psEncCtrl,     /* I/O  encoder control                             */
     const opus_int16                 res_pitch[],    /* I    Residual from pitch analysis                */
-    const opus_int16                 x[]             /* I    Speech signal                               */
+    const opus_int16                 x[],            /* I    Speech signal                               */
+    opus_int                         condCoding      /* I    The type of conditional coding to use       */
 )
 {
     opus_int         i;
@@ -86,7 +87,7 @@
             WLTP, psEnc->sCmn.mu_LTP_Q9, psEnc->sCmn.LTPQuantLowComplexity, psEnc->sCmn.nb_subfr);
 
         /* Control LTP scaling */
-        silk_LTP_scale_ctrl_FIX( psEnc, psEncCtrl );
+        silk_LTP_scale_ctrl_FIX( psEnc, psEncCtrl, condCoding );
 
         /* Create LTP residual */
         silk_LTP_analysis_filter_FIX( LPC_in_pre, x - psEnc->sCmn.predictLPCOrder, psEncCtrl->LTPCoef_Q14,
diff --git a/silk/fixed/main_FIX.h b/silk/fixed/main_FIX.h
index e0b1e05..56ba1ef 100644
--- a/silk/fixed/main_FIX.h
+++ b/silk/fixed/main_FIX.h
@@ -59,7 +59,8 @@
 opus_int silk_encode_frame_FIX(
     silk_encoder_state_FIX          *psEnc,             /* I/O  Pointer to Silk FIX encoder state       */
     opus_int32                       *pnBytesOut,        /*   O  Pointer to number of payload bytes;     */
-    ec_enc                          *psRangeEnc         /* I/O  compressor data structure               */
+    ec_enc                          *psRangeEnc,        /* I/O  compressor data structure               */
+    opus_int                         condCoding         /* I    The type of conditional coding to use   */
 );
 
 /* Low Bitrate Redundancy (LBRR) encoding. Reuse all parameters but encode with lower bitrate           */
@@ -118,7 +119,8 @@
 /* Calculation of LTP state scaling */
 void silk_LTP_scale_ctrl_FIX(
     silk_encoder_state_FIX          *psEnc,         /* I/O  encoder state                               */
-    silk_encoder_control_FIX        *psEncCtrl      /* I/O  encoder control                             */
+    silk_encoder_control_FIX        *psEncCtrl,     /* I/O  encoder control                             */
+    opus_int                         condCoding     /* I    The type of conditional coding to use       */
 );
 
 /**********************************************/
@@ -137,7 +139,8 @@
     silk_encoder_state_FIX          *psEnc,         /* I/O  encoder state                               */
     silk_encoder_control_FIX        *psEncCtrl,     /* I/O  encoder control                             */
     const opus_int16                 res_pitch[],    /* I    Residual from pitch analysis                */
-    const opus_int16                 x[]             /* I    Speech signal                               */
+    const opus_int16                 x[],            /* I    Speech signal                               */
+    opus_int                         condCoding      /* I    The type of conditional coding to use       */
 );
 
 /* LPC analysis */
@@ -204,7 +207,8 @@
 /* Processing of gains */
 void silk_process_gains_FIX(
     silk_encoder_state_FIX          *psEnc,         /* I/O  Encoder state                               */
-    silk_encoder_control_FIX        *psEncCtrl      /* I/O  Encoder control                             */
+    silk_encoder_control_FIX        *psEncCtrl,     /* I/O  Encoder control                             */
+    opus_int                         condCoding     /* I    The type of conditional coding to use       */
 );
 
 /******************/
diff --git a/silk/fixed/process_gains_FIX.c b/silk/fixed/process_gains_FIX.c
index 0dea69b..4fe55bb 100644
--- a/silk/fixed/process_gains_FIX.c
+++ b/silk/fixed/process_gains_FIX.c
@@ -36,6 +36,7 @@
 void silk_process_gains_FIX(
     silk_encoder_state_FIX      *psEnc,         /* I/O  Encoder state_FIX                           */
     silk_encoder_control_FIX    *psEncCtrl      /* I/O  Encoder control_FIX                         */
+    opus_int                     condCoding     /* The type of conditional coding to use            */
 )
 {
     silk_shape_state_FIX    *psShapeSt = &psEnc->sShape;
@@ -87,7 +88,7 @@
 
     /* Noise shaping quantization */
     silk_gains_quant( psEnc->sCmn.indices.GainsIndices, psEncCtrl->Gains_Q16,
-        &psShapeSt->LastGainIndex, psEnc->sCmn.nFramesEncoded, psEnc->sCmn.nb_subfr );
+        &psShapeSt->LastGainIndex, condCoding == CODE_CONDITIONALLY, psEnc->sCmn.nb_subfr );
 
     /* Set quantizer offset for voiced signals. Larger offset when LTP coding gain is low or tilt is high (ie low-pass) */
     if( psEnc->sCmn.indices.signalType == TYPE_VOICED ) {
diff --git a/silk/float/LTP_scale_ctrl_FLP.c b/silk/float/LTP_scale_ctrl_FLP.c
index 76290b1..cbac100 100644
--- a/silk/float/LTP_scale_ctrl_FLP.c
+++ b/silk/float/LTP_scale_ctrl_FLP.c
@@ -33,7 +33,8 @@
 
 void silk_LTP_scale_ctrl_FLP(
     silk_encoder_state_FLP      *psEnc,             /* I/O  Encoder state FLP                       */
-    silk_encoder_control_FLP    *psEncCtrl          /* I/O  Encoder control FLP                     */
+    silk_encoder_control_FLP    *psEncCtrl,         /* I/O  Encoder control FLP                     */
+    opus_int                     condCoding         /* I    The type of conditional coding to use   */
 )
 {
     opus_int   round_loss;
@@ -45,7 +46,7 @@
     psEnc->prevLTPredCodGain = psEncCtrl->LTPredCodGain;
 
     /* Only scale if first frame in packet */
-    if( psEnc->sCmn.nFramesEncoded == 0 ) {
+    if( condCoding == CODE_INDEPENDENTLY ) {
         round_loss = psEnc->sCmn.PacketLoss_perc + psEnc->sCmn.nFramesPerPacket;
         psEnc->sCmn.indices.LTP_scaleIndex = (opus_int8)silk_LIMIT( round_loss * psEnc->HPLTPredCodGain * 0.1f, 0.0f, 2.0f );
     } else {
diff --git a/silk/float/encode_frame_FLP.c b/silk/float/encode_frame_FLP.c
index 4d156a0..d105642 100644
--- a/silk/float/encode_frame_FLP.c
+++ b/silk/float/encode_frame_FLP.c
@@ -36,7 +36,8 @@
 static inline void silk_LBRR_encode_FLP(
     silk_encoder_state_FLP          *psEnc,             /* I/O  Encoder state FLP                       */
     silk_encoder_control_FLP        *psEncCtrl,         /* I/O  Encoder control FLP                     */
-    const silk_float                 xfw[]               /* I    Input signal                            */
+    const silk_float                 xfw[],              /* I    Input signal                            */
+    opus_int                         condCoding         /* I    The type of conditional coding used so far for this frame */
 );
 
 /****************/
@@ -45,7 +46,8 @@
 opus_int silk_encode_frame_FLP(
     silk_encoder_state_FLP          *psEnc,             /* I/O  Encoder state FLP                       */
     opus_int32                       *pnBytesOut,        /*   O  Number of payload bytes                 */
-    ec_enc                          *psRangeEnc         /* I/O  compressor data structure               */
+    ec_enc                          *psRangeEnc,        /* I/O  compressor data structure               */
+    opus_int                         condCoding         /* I    The type of conditional coding to use   */
 )
 {
     silk_encoder_control_FLP sEncCtrl;
@@ -128,14 +130,14 @@
     /* Find linear prediction coefficients (LPC + LTP) */
     /***************************************************/
 TIC(FIND_PRED_COEF)
-    silk_find_pred_coefs_FLP( psEnc, &sEncCtrl, res_pitch, x_frame );
+    silk_find_pred_coefs_FLP( psEnc, &sEncCtrl, res_pitch, x_frame, condCoding );
 TOC(FIND_PRED_COEF)
 
     /****************************************/
     /* Process gains                        */
     /****************************************/
 TIC(PROCESS_GAINS)
-    silk_process_gains_FLP( psEnc, &sEncCtrl );
+    silk_process_gains_FLP( psEnc, &sEncCtrl, condCoding );
 TOC(PROCESS_GAINS)
 
     /*****************************************/
@@ -149,7 +151,7 @@
     /* Low Bitrate Redundant Encoding       */
     /****************************************/
 TIC(LBRR)
-    silk_LBRR_encode_FLP( psEnc, &sEncCtrl, xfw );
+    silk_LBRR_encode_FLP( psEnc, &sEncCtrl, xfw, condCoding );
 TOC(LBRR)
 
     /*****************************************/
@@ -178,7 +180,7 @@
     /* Encode Parameters                    */
     /****************************************/
 TIC(ENCODE_PARAMS)
-    silk_encode_indices( &psEnc->sCmn, psRangeEnc, psEnc->sCmn.nFramesEncoded, 0 );
+    silk_encode_indices( &psEnc->sCmn, psRangeEnc, psEnc->sCmn.nFramesEncoded, 0, condCoding );
 TOC(ENCODE_PARAMS)
 
     /****************************************/
@@ -222,7 +224,8 @@
 static inline void silk_LBRR_encode_FLP(
     silk_encoder_state_FLP          *psEnc,             /* I/O  Encoder state FLP                       */
     silk_encoder_control_FLP        *psEncCtrl,         /* I/O  Encoder control FLP                     */
-    const silk_float                 xfw[]               /* I    Input signal                            */
+    const silk_float                 xfw[],              /* I    Input signal                            */
+    opus_int                         condCoding         /* I    The type of conditional coding used so far for this frame */
 )
 {
     opus_int     k;
@@ -255,7 +258,7 @@
 
         /* Decode to get gains in sync with decoder */
         silk_gains_dequant( Gains_Q16, psIndices_LBRR->GainsIndices,
-            &psEnc->sCmn.LBRRprevLastGainIndex, psEnc->sCmn.nFramesEncoded, psEnc->sCmn.nb_subfr );
+            &psEnc->sCmn.LBRRprevLastGainIndex, condCoding == CODE_CONDITIONALLY, psEnc->sCmn.nb_subfr );
 
         /* Overwrite unquantized gains with quantized gains and convert back to Q0 from Q16 */
         for( k = 0; k <  psEnc->sCmn.nb_subfr; k++ ) {
diff --git a/silk/float/find_pred_coefs_FLP.c b/silk/float/find_pred_coefs_FLP.c
index 49c29d3..0488185 100644
--- a/silk/float/find_pred_coefs_FLP.c
+++ b/silk/float/find_pred_coefs_FLP.c
@@ -36,7 +36,8 @@
     silk_encoder_state_FLP          *psEnc,             /* I/O  Encoder state FLP                       */
     silk_encoder_control_FLP        *psEncCtrl,         /* I/O  Encoder control FLP                     */
     const silk_float                 res_pitch[],        /* I    Residual from pitch analysis            */
-    const silk_float                 x[]                 /* I    Speech signal                           */
+    const silk_float                 x[],                /* I    Speech signal                           */
+    opus_int                         condCoding          /* I    The type of conditional coding to use   */
 )
 {
     opus_int         i;
@@ -73,7 +74,7 @@
             WLTP, psEnc->sCmn.mu_LTP_Q9, psEnc->sCmn.LTPQuantLowComplexity, psEnc->sCmn.nb_subfr );
 
         /* Control LTP scaling */
-        silk_LTP_scale_ctrl_FLP( psEnc, psEncCtrl );
+        silk_LTP_scale_ctrl_FLP( psEnc, psEncCtrl, condCoding );
 
         /* Create LTP residual */
         silk_LTP_analysis_filter_FLP( LPC_in_pre, x - psEnc->sCmn.predictLPCOrder, psEncCtrl->LTPCoef,
diff --git a/silk/float/main_FLP.h b/silk/float/main_FLP.h
index 605fd38..d0c2de7 100644
--- a/silk/float/main_FLP.h
+++ b/silk/float/main_FLP.h
@@ -57,7 +57,8 @@
 opus_int silk_encode_frame_FLP(
     silk_encoder_state_FLP          *psEnc,             /* I/O  Encoder state FLP                       */
     opus_int32                       *pnBytesOut,        /*   O  Number of payload bytes;                */
-    ec_enc                          *psRangeEnc         /* I/O  compressor data structure               */
+    ec_enc                          *psRangeEnc,        /* I/O  compressor data structure               */
+    opus_int                         condCoding         /* I    The type of conditional coding to use   */
 );
 
 /* Initializes the Silk encoder state */
@@ -108,7 +109,8 @@
 /* Calculation of LTP state scaling */
 void silk_LTP_scale_ctrl_FLP(
     silk_encoder_state_FLP          *psEnc,             /* I/O  Encoder state FLP                       */
-    silk_encoder_control_FLP        *psEncCtrl          /* I/O  Encoder control FLP                     */
+    silk_encoder_control_FLP        *psEncCtrl,         /* I/O  Encoder control FLP                     */
+    opus_int                         condCoding         /* I    The type of conditional coding to use   */
 );
 
 /**********************************************/
@@ -127,7 +129,8 @@
     silk_encoder_state_FLP          *psEnc,             /* I/O  Encoder state FLP                       */
     silk_encoder_control_FLP        *psEncCtrl,         /* I/O  Encoder control FLP                     */
     const silk_float                 res_pitch[],        /* I    Residual from pitch analysis            */
-    const silk_float                 x[]                 /* I    Speech signal                           */
+    const silk_float                 x[],                /* I    Speech signal                           */
+    opus_int                         condCoding          /* I    The type of conditional coding to use   */
 );
 
 /* LPC analysis */
@@ -234,7 +237,8 @@
 /* Processing of gains */
 void silk_process_gains_FLP(
     silk_encoder_state_FLP          *psEnc,             /* I/O  Encoder state FLP                       */
-    silk_encoder_control_FLP        *psEncCtrl          /* I/O  Encoder control FLP                     */
+    silk_encoder_control_FLP        *psEncCtrl,         /* I/O  Encoder control FLP                     */
+    opus_int                         condCoding         /* I    The type of conditional coding to use   */
 );
 
 /******************/
diff --git a/silk/float/process_gains_FLP.c b/silk/float/process_gains_FLP.c
index e0c18a9..2c7dfdd 100644
--- a/silk/float/process_gains_FLP.c
+++ b/silk/float/process_gains_FLP.c
@@ -35,7 +35,8 @@
 /* Processing of gains */
 void silk_process_gains_FLP(
     silk_encoder_state_FLP      *psEnc,             /* I/O  Encoder state FLP                       */
-    silk_encoder_control_FLP    *psEncCtrl          /* I/O  Encoder control FLP                     */
+    silk_encoder_control_FLP    *psEncCtrl,         /* I/O  Encoder control FLP                     */
+    opus_int                     condCoding         /* I    The type of conditional coding to use   */
 )
 {
     silk_shape_state_FLP *psShapeSt = &psEnc->sShape;
@@ -68,7 +69,7 @@
 
     /* Noise shaping quantization */
     silk_gains_quant( psEnc->sCmn.indices.GainsIndices, pGains_Q16,
-            &psShapeSt->LastGainIndex, psEnc->sCmn.nFramesEncoded, psEnc->sCmn.nb_subfr );
+            &psShapeSt->LastGainIndex, condCoding == CODE_CONDITIONALLY, psEnc->sCmn.nb_subfr );
 
     /* Overwrite unquantized gains with quantized gains and convert back to Q0 from Q16 */
     for( k = 0; k < psEnc->sCmn.nb_subfr; k++ ) {
diff --git a/silk/main.h b/silk/main.h
index 85423ab..1f2f01a 100644
--- a/silk/main.h
+++ b/silk/main.h
@@ -377,7 +377,8 @@
     ec_dec                      *psRangeDec,        /* I/O  Compressor data structure                   */
     opus_int16                   pOut[],             /* O    Pointer to output speech frame              */
     opus_int32                   *pN,                /* O    Pointer to size of output frame             */
-    opus_int                     lostFlag            /* I    0: no loss, 1 loss, 2 decode fec            */
+    opus_int                     lostFlag,           /* I    0: no loss, 1 loss, 2 decode fec            */
+    opus_int                     condCoding          /* I    The type of conditional coding to use       */
 );
 
 /* Decode LBRR side info and excitation */
@@ -391,13 +392,15 @@
     silk_decoder_state          *psDec,             /* I/O  State                                       */
     ec_dec                      *psRangeDec,        /* I/O  Compressor data structure                   */
     opus_int                     FrameIndex,         /* I    Frame number                                */
-    opus_int                     decode_LBRR         /* I    Flag indicating LBRR data is being decoded  */
+    opus_int                     decode_LBRR,        /* I    Flag indicating LBRR data is being decoded  */
+    opus_int                     condCoding          /* I    The type of conditional coding to use       */
 );
 
 /* Decode parameters from payload */
 void silk_decode_parameters(
     silk_decoder_state      *psDec,                             /* I/O  State                                    */
-    silk_decoder_control    *psDecCtrl                          /* I/O  Decoder control                          */
+    silk_decoder_control    *psDecCtrl,                         /* I/O  Decoder control                          */
+    opus_int                 condCoding                         /* I    The type of conditional coding to use    */
 );
 
 /* Core decoder. Performs inverse NSQ operation LTP + LPC */
@@ -439,7 +442,8 @@
     silk_encoder_state          *psEncC,            /* I/O  Encoder state                               */
     ec_enc                      *psRangeEnc,        /* I/O  Compressor data structure                   */
     opus_int                     FrameIndex,         /* I    Frame number                                */
-    opus_int                     encode_LBRR         /* I    Flag indicating LBRR data is being encoded  */
+    opus_int                     encode_LBRR,        /* I    Flag indicating LBRR data is being encoded  */
+    opus_int                     condCoding          /* I    The type of conditional coding to use       */
 );
 
 #endif