From 16b25e908a57ee1ca16db2b6d0b8135312f4b6ad Mon Sep 17 00:00:00 2001
From: Gregory Maxwell <greg@xiph.org>
Date: Tue, 6 Sep 2011 22:58:01 -0400
Subject: [PATCH] Eliminate the last non-static pointer from the LPC-mode
 encoder structures. The API permits the caller to freely copy the codec state
 on their own, but this can't work if there are any any position dependant
 pointers in the codec state.

---
 silk/fixed/silk_encode_frame_FIX.c |  6 +++---
 silk/float/silk_encode_frame_FLP.c |  6 +++---
 silk/silk_enc_API.c                | 14 +++++++-------
 silk/silk_init_encoder.c           |  2 --
 silk/silk_structs.h                |  3 +--
 5 files changed, 14 insertions(+), 17 deletions(-)

diff --git a/silk/fixed/silk_encode_frame_FIX.c b/silk/fixed/silk_encode_frame_FIX.c
index 6afe42166..079577b73 100644
--- a/silk/fixed/silk_encode_frame_FIX.c
+++ b/silk/fixed/silk_encode_frame_FIX.c
@@ -62,7 +62,7 @@ TIC(ENCODE_FRAME)
     /* Voice Activity Detection */
     /****************************/
 TIC(VAD)
-    ret = silk_VAD_GetSA_Q8( &psEnc->sCmn, psEnc->sCmn.inputBuf - 1 );
+    ret = silk_VAD_GetSA_Q8( &psEnc->sCmn, psEnc->sCmn.inputBuf + 1 );
 TOC(VAD)
 
     /**************************************************/
@@ -91,12 +91,12 @@ TOC(VAD)
     /***************************************/
     /* Ensure smooth bandwidth transitions */
     /***************************************/
-    silk_LP_variable_cutoff( &psEnc->sCmn.sLP, psEnc->sCmn.inputBuf - 1, psEnc->sCmn.frame_length );
+    silk_LP_variable_cutoff( &psEnc->sCmn.sLP, psEnc->sCmn.inputBuf + 1, psEnc->sCmn.frame_length );
 
     /*******************************************/
     /* Copy new frame to front of input buffer */
     /*******************************************/
-    SKP_memcpy( x_frame + LA_SHAPE_MS * psEnc->sCmn.fs_kHz, psEnc->sCmn.inputBuf - 1, psEnc->sCmn.frame_length * sizeof( opus_int16 ) );
+    SKP_memcpy( x_frame + LA_SHAPE_MS * psEnc->sCmn.fs_kHz, psEnc->sCmn.inputBuf + 1, psEnc->sCmn.frame_length * sizeof( opus_int16 ) );
 
     /*****************************************/
     /* Find pitch lags, initial LPC analysis */
diff --git a/silk/float/silk_encode_frame_FLP.c b/silk/float/silk_encode_frame_FLP.c
index 3ec100e91..2fc701de6 100644
--- a/silk/float/silk_encode_frame_FLP.c
+++ b/silk/float/silk_encode_frame_FLP.c
@@ -62,7 +62,7 @@ TIC(ENCODE_FRAME)
     /* Voice Activity Detection */
     /****************************/
 TIC(VAD)
-    ret = silk_VAD_GetSA_Q8( &psEnc->sCmn, psEnc->sCmn.inputBuf - 1 );
+    ret = silk_VAD_GetSA_Q8( &psEnc->sCmn, psEnc->sCmn.inputBuf + 1 );
 TOC(VAD)
 
     /**************************************************/
@@ -91,12 +91,12 @@ TOC(VAD)
     /***************************************/
     /* Ensure smooth bandwidth transitions */
     /***************************************/
-    silk_LP_variable_cutoff( &psEnc->sCmn.sLP, psEnc->sCmn.inputBuf - 1, psEnc->sCmn.frame_length );
+    silk_LP_variable_cutoff( &psEnc->sCmn.sLP, psEnc->sCmn.inputBuf + 1, psEnc->sCmn.frame_length );
 
     /*******************************************/
     /* Copy new frame to front of input buffer */
     /*******************************************/
-    SKP_short2float_array( x_frame + LA_SHAPE_MS * psEnc->sCmn.fs_kHz, psEnc->sCmn.inputBuf - 1, psEnc->sCmn.frame_length );
+    SKP_short2float_array( x_frame + LA_SHAPE_MS * psEnc->sCmn.fs_kHz, psEnc->sCmn.inputBuf + 1, psEnc->sCmn.frame_length );
 
     /* Add tiny signal to avoid high CPU load from denormalized floating point numbers */
     for( i = 0; i < 8; i++ ) {
diff --git a/silk/silk_enc_API.c b/silk/silk_enc_API.c
index c9055e717..862438226 100644
--- a/silk/silk_enc_API.c
+++ b/silk/silk_enc_API.c
@@ -220,7 +220,7 @@ opus_int silk_Encode(
                 buf[ n ] = samplesIn[ 2 * n ];
             }
             ret += silk_resampler( &psEnc->state_Fxx[ 0 ].sCmn.resampler_state,
-                &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx ], buf, nSamplesFromInput );
+                &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput );
             psEnc->state_Fxx[ 0 ].sCmn.inputBufIx += nSamplesToBuffer;
 
             nSamplesToBuffer  = psEnc->state_Fxx[ 1 ].sCmn.frame_length - psEnc->state_Fxx[ 1 ].sCmn.inputBufIx;
@@ -229,7 +229,7 @@ opus_int silk_Encode(
                 buf[ n ] = samplesIn[ 2 * n + 1 ];
             }
             ret += silk_resampler( &psEnc->state_Fxx[ 1 ].sCmn.resampler_state,
-                &psEnc->state_Fxx[ 1 ].sCmn.inputBuf[ psEnc->state_Fxx[ 1 ].sCmn.inputBufIx ], buf, nSamplesFromInput );
+                &psEnc->state_Fxx[ 1 ].sCmn.inputBuf[ psEnc->state_Fxx[ 1 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput );
             psEnc->state_Fxx[ 1 ].sCmn.inputBufIx += nSamplesToBuffer;
         } else if( encControl->nChannelsAPI == 2 && encControl->nChannelsInternal == 1 ) {
             /* Combine left and right channels before resampling */
@@ -237,12 +237,12 @@ opus_int silk_Encode(
                 buf[ n ] = (opus_int16)SKP_RSHIFT_ROUND( samplesIn[ 2 * n ] + samplesIn[ 2 * n + 1 ],  1 );
             }
             ret += silk_resampler( &psEnc->state_Fxx[ 0 ].sCmn.resampler_state,
-                &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx ], buf, nSamplesFromInput );
+                &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput );
             psEnc->state_Fxx[ 0 ].sCmn.inputBufIx += nSamplesToBuffer;
         } else {
             SKP_assert( encControl->nChannelsAPI == 1 && encControl->nChannelsInternal == 1 );
             ret += silk_resampler( &psEnc->state_Fxx[ 0 ].sCmn.resampler_state,
-                &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx ], samplesIn, nSamplesFromInput );
+                &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx + 2 ], samplesIn, nSamplesFromInput );
             psEnc->state_Fxx[ 0 ].sCmn.inputBufIx += nSamplesToBuffer;
         }
         samplesIn  += nSamplesFromInput * encControl->nChannelsAPI;
@@ -323,7 +323,7 @@ opus_int silk_Encode(
 
             /* Convert Left/Right to Mid/Side */
             if( encControl->nChannelsInternal == 2 ) {
-                silk_stereo_LR_to_MS( &psEnc->sStereo, psEnc->state_Fxx[ 0 ].sCmn.inputBuf, psEnc->state_Fxx[ 1 ].sCmn.inputBuf,
+                silk_stereo_LR_to_MS( &psEnc->sStereo, &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ 2 ], &psEnc->state_Fxx[ 1 ].sCmn.inputBuf[ 2 ],
                     psEnc->sStereo.predIx[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ], &psEnc->sStereo.mid_only_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ],
                     MStargetRates_bps, TargetRate_bps, psEnc->state_Fxx[ 0 ].sCmn.speech_activity_Q8,
                     psEnc->state_Fxx[ 0 ].sCmn.fs_kHz, psEnc->state_Fxx[ 0 ].sCmn.frame_length );
@@ -333,8 +333,8 @@ opus_int silk_Encode(
                 }
             } else {
                 /* Buffering */
-                SKP_memcpy( &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ -2 ], psEnc->sStereo.sMid, 2 * sizeof( opus_int16 ) );
-                SKP_memcpy( psEnc->sStereo.sMid, &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.frame_length - 2 ], 2 * sizeof( opus_int16 ) );
+                SKP_memcpy( psEnc->state_Fxx[ 0 ].sCmn.inputBuf, psEnc->sStereo.sMid, 2 * sizeof( opus_int16 ) );
+                SKP_memcpy( psEnc->sStereo.sMid, &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.frame_length ], 2 * sizeof( opus_int16 ) );
             }
 
             /* Encode */
diff --git a/silk/silk_init_encoder.c b/silk/silk_init_encoder.c
index 0a6779649..63b43b4a8 100644
--- a/silk/silk_init_encoder.c
+++ b/silk/silk_init_encoder.c
@@ -46,8 +46,6 @@ opus_int silk_init_encoder(
     /* Clear the entire encoder state */
     SKP_memset( psEnc, 0, sizeof( silk_encoder_state_Fxx ) );
 
-    psEnc->sCmn.inputBuf = &psEnc->sCmn.inputBuf__[ 2 ];
-
     psEnc->sCmn.variable_HP_smth1_Q15 = SKP_LSHIFT( silk_lin2log( SILK_FIX_CONST( VARIABLE_HP_MIN_CUTOFF_HZ, 16 ) ) - ( 16 << 7 ), 8 );
     psEnc->sCmn.variable_HP_smth2_Q15 = psEnc->sCmn.variable_HP_smth1_Q15;
 
diff --git a/silk/silk_structs.h b/silk/silk_structs.h
index 3044a02fc..854d0cdde 100644
--- a/silk/silk_structs.h
+++ b/silk/silk_structs.h
@@ -191,8 +191,7 @@ typedef struct {
     opus_int8                        pulses[ MAX_FRAME_LENGTH ];
 
     /* Input/output buffering */
-    opus_int16                       inputBuf__[ MAX_FRAME_LENGTH + 2 ]; /* Buffer containing input signal                                   */
-    opus_int16                       *inputBuf;                      /* Points to second element of above buffer                             */
+    opus_int16                       inputBuf[ MAX_FRAME_LENGTH + 2 ]; /* Buffer containing input signal                                   */
     opus_int                         inputBufIx;
     opus_int                         nFramesPerPacket;
     opus_int                         nFramesEncoded;                 /* Number of frames analyzed in current packet                          */
-- 
GitLab