Home | History | Annotate | Download | only in cng
      1 /*
      2  *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
      3  *
      4  *  Use of this source code is governed by a BSD-style license
      5  *  that can be found in the LICENSE file in the root of the source
      6  *  tree. An additional intellectual property rights grant can be found
      7  *  in the file PATENTS.  All contributing project authors may
      8  *  be found in the AUTHORS file in the root of the source tree.
      9  */
     10 
     11 #include "webrtc_cng.h"
     12 
     13 #include <string.h>
     14 #include <stdlib.h>
     15 
     16 #include "cng_helpfuns.h"
     17 #include "signal_processing_library.h"
     18 
     19 typedef struct WebRtcCngDecInst_t_ {
     20   uint32_t dec_seed;
     21   int32_t dec_target_energy;
     22   int32_t dec_used_energy;
     23   int16_t dec_target_reflCoefs[WEBRTC_CNG_MAX_LPC_ORDER + 1];
     24   int16_t dec_used_reflCoefs[WEBRTC_CNG_MAX_LPC_ORDER + 1];
     25   int16_t dec_filtstate[WEBRTC_CNG_MAX_LPC_ORDER + 1];
     26   int16_t dec_filtstateLow[WEBRTC_CNG_MAX_LPC_ORDER + 1];
     27   int16_t dec_Efiltstate[WEBRTC_CNG_MAX_LPC_ORDER + 1];
     28   int16_t dec_EfiltstateLow[WEBRTC_CNG_MAX_LPC_ORDER + 1];
     29   int16_t dec_order;
     30   int16_t dec_target_scale_factor;  /* Q29 */
     31   int16_t dec_used_scale_factor;  /* Q29 */
     32   int16_t target_scale_factor;  /* Q13 */
     33   int16_t errorcode;
     34   int16_t initflag;
     35 } WebRtcCngDecInst_t;
     36 
     37 typedef struct WebRtcCngEncInst_t_ {
     38   int16_t enc_nrOfCoefs;
     39   uint16_t enc_sampfreq;
     40   int16_t enc_interval;
     41   int16_t enc_msSinceSID;
     42   int32_t enc_Energy;
     43   int16_t enc_reflCoefs[WEBRTC_CNG_MAX_LPC_ORDER + 1];
     44   int32_t enc_corrVector[WEBRTC_CNG_MAX_LPC_ORDER + 1];
     45   uint32_t enc_seed;
     46   int16_t errorcode;
     47   int16_t initflag;
     48 } WebRtcCngEncInst_t;
     49 
     50 const int32_t WebRtcCng_kDbov[94] = {
     51   1081109975,  858756178,  682134279,  541838517,  430397633,  341876992,
     52   271562548,  215709799,  171344384,  136103682,  108110997,   85875618,
     53   68213428,   54183852,   43039763,   34187699,   27156255,   21570980,
     54   17134438,   13610368,   10811100,    8587562,    6821343,    5418385,
     55   4303976,    3418770,    2715625,    2157098,    1713444,    1361037,
     56   1081110,     858756,     682134,     541839,     430398,     341877,
     57   271563,     215710,     171344,     136104,     108111,      85876,
     58   68213,      54184,      43040,      34188,      27156,      21571,
     59   17134,      13610,      10811,       8588,       6821,       5418,
     60   4304,       3419,       2716,       2157,       1713,       1361,
     61   1081,        859,        682,        542,        430,        342,
     62   272,        216,        171,        136,        108,         86,
     63   68,         54,         43,         34,         27,         22,
     64   17,         14,         11,          9,          7,          5,
     65   4,          3,          3,          2,          2,           1,
     66   1,          1,          1,          1
     67 };
     68 
     69 const int16_t WebRtcCng_kCorrWindow[WEBRTC_CNG_MAX_LPC_ORDER] = {
     70   32702, 32636, 32570, 32505, 32439, 32374,
     71   32309, 32244, 32179, 32114, 32049, 31985
     72 };
     73 
     74 /****************************************************************************
     75  * WebRtcCng_CreateEnc/Dec(...)
     76  *
     77  * These functions create an instance to the specified structure
     78  *
     79  * Input:
     80  *      - XXX_inst      : Pointer to created instance that should be created
     81  *
     82  * Return value         :  0 - Ok
     83  *                        -1 - Error
     84  */
     85 int16_t WebRtcCng_CreateEnc(CNG_enc_inst** cng_inst) {
     86   if (cng_inst != NULL) {
     87     *cng_inst = (CNG_enc_inst*) malloc(sizeof(WebRtcCngEncInst_t));
     88     if (*cng_inst != NULL) {
     89       (*(WebRtcCngEncInst_t**) cng_inst)->errorcode = 0;
     90       (*(WebRtcCngEncInst_t**) cng_inst)->initflag = 0;
     91 
     92       /* Needed to get the right function pointers in SPLIB. */
     93       WebRtcSpl_Init();
     94 
     95       return 0;
     96     } else {
     97       /* The memory could not be allocated. */
     98       return -1;
     99     }
    100   } else {
    101     /* The input pointer is invalid (NULL). */
    102     return -1;
    103   }
    104 }
    105 
    106 int16_t WebRtcCng_CreateDec(CNG_dec_inst** cng_inst) {
    107   if (cng_inst != NULL ) {
    108     *cng_inst = (CNG_dec_inst*) malloc(sizeof(WebRtcCngDecInst_t));
    109     if (*cng_inst != NULL ) {
    110       (*(WebRtcCngDecInst_t**) cng_inst)->errorcode = 0;
    111       (*(WebRtcCngDecInst_t**) cng_inst)->initflag = 0;
    112 
    113       /* Needed to get the right function pointers in SPLIB. */
    114       WebRtcSpl_Init();
    115 
    116       return 0;
    117     } else {
    118       /* The memory could not be allocated */
    119       return -1;
    120     }
    121   } else {
    122     /* The input pointer is invalid (NULL). */
    123     return -1;
    124   }
    125 }
    126 
    127 /****************************************************************************
    128  * WebRtcCng_InitEnc/Dec(...)
    129  *
    130  * This function initializes a instance
    131  *
    132  * Input:
    133  *    - cng_inst      : Instance that should be initialized
    134  *
    135  *    - fs            : 8000 for narrowband and 16000 for wideband
    136  *    - interval      : generate SID data every interval ms
    137  *    - quality       : TBD
    138  *
    139  * Output:
    140  *    - cng_inst      : Initialized instance
    141  *
    142  * Return value       :  0 - Ok
    143  *                      -1 - Error
    144  */
    145 int16_t WebRtcCng_InitEnc(CNG_enc_inst* cng_inst, uint16_t fs, int16_t interval,
    146                           int16_t quality) {
    147   int i;
    148   WebRtcCngEncInst_t* inst = (WebRtcCngEncInst_t*) cng_inst;
    149   memset(inst, 0, sizeof(WebRtcCngEncInst_t));
    150 
    151   /* Check LPC order */
    152   if (quality > WEBRTC_CNG_MAX_LPC_ORDER || quality <= 0) {
    153     inst->errorcode = CNG_DISALLOWED_LPC_ORDER;
    154     return -1;
    155   }
    156 
    157   inst->enc_sampfreq = fs;
    158   inst->enc_interval = interval;
    159   inst->enc_nrOfCoefs = quality;
    160   inst->enc_msSinceSID = 0;
    161   inst->enc_seed = 7777;  /* For debugging only. */
    162   inst->enc_Energy = 0;
    163   for (i = 0; i < (WEBRTC_CNG_MAX_LPC_ORDER + 1); i++) {
    164     inst->enc_reflCoefs[i] = 0;
    165     inst->enc_corrVector[i] = 0;
    166   }
    167   inst->initflag = 1;
    168 
    169   return 0;
    170 }
    171 
    172 int16_t WebRtcCng_InitDec(CNG_dec_inst* cng_inst) {
    173   int i;
    174 
    175   WebRtcCngDecInst_t* inst = (WebRtcCngDecInst_t*) cng_inst;
    176 
    177   memset(inst, 0, sizeof(WebRtcCngDecInst_t));
    178   inst->dec_seed = 7777;  /* For debugging only. */
    179   inst->dec_order = 5;
    180   inst->dec_target_scale_factor = 0;
    181   inst->dec_used_scale_factor = 0;
    182   for (i = 0; i < (WEBRTC_CNG_MAX_LPC_ORDER + 1); i++) {
    183     inst->dec_filtstate[i] = 0;
    184     inst->dec_target_reflCoefs[i] = 0;
    185     inst->dec_used_reflCoefs[i] = 0;
    186   }
    187   inst->dec_target_reflCoefs[0] = 0;
    188   inst->dec_used_reflCoefs[0] = 0;
    189   inst->dec_used_energy = 0;
    190   inst->initflag = 1;
    191 
    192   return 0;
    193 }
    194 
    195 /****************************************************************************
    196  * WebRtcCng_FreeEnc/Dec(...)
    197  *
    198  * These functions frees the dynamic memory of a specified instance
    199  *
    200  * Input:
    201  *    - cng_inst      : Pointer to created instance that should be freed
    202  *
    203  * Return value       :  0 - Ok
    204  *                      -1 - Error
    205  */
    206 int16_t WebRtcCng_FreeEnc(CNG_enc_inst* cng_inst) {
    207   free(cng_inst);
    208   return 0;
    209 }
    210 
    211 int16_t WebRtcCng_FreeDec(CNG_dec_inst* cng_inst) {
    212   free(cng_inst);
    213   return 0;
    214 }
    215 
    216 /****************************************************************************
    217  * WebRtcCng_Encode(...)
    218  *
    219  * These functions analyzes background noise
    220  *
    221  * Input:
    222  *    - cng_inst      : Pointer to created instance
    223  *    - speech        : Signal (noise) to be analyzed
    224  *    - nrOfSamples   : Size of speech vector
    225  *    - bytesOut      : Nr of bytes to transmit, might be 0
    226  *
    227  * Return value       :  0 - Ok
    228  *                      -1 - Error
    229  */
    230 int16_t WebRtcCng_Encode(CNG_enc_inst* cng_inst, int16_t* speech,
    231                          int16_t nrOfSamples, uint8_t* SIDdata,
    232                          int16_t* bytesOut, int16_t forceSID) {
    233   WebRtcCngEncInst_t* inst = (WebRtcCngEncInst_t*) cng_inst;
    234 
    235   int16_t arCoefs[WEBRTC_CNG_MAX_LPC_ORDER + 1];
    236   int32_t corrVector[WEBRTC_CNG_MAX_LPC_ORDER + 1];
    237   int16_t refCs[WEBRTC_CNG_MAX_LPC_ORDER + 1];
    238   int16_t hanningW[WEBRTC_CNG_MAX_OUTSIZE_ORDER];
    239   int16_t ReflBeta = 19661;  /* 0.6 in q15. */
    240   int16_t ReflBetaComp = 13107;  /* 0.4 in q15. */
    241   int32_t outEnergy;
    242   int outShifts;
    243   int i, stab;
    244   int acorrScale;
    245   int index;
    246   int16_t ind, factor;
    247   int32_t* bptr;
    248   int32_t blo, bhi;
    249   int16_t negate;
    250   const int16_t* aptr;
    251   int16_t speechBuf[WEBRTC_CNG_MAX_OUTSIZE_ORDER];
    252 
    253   /* Check if encoder initiated. */
    254   if (inst->initflag != 1) {
    255     inst->errorcode = CNG_ENCODER_NOT_INITIATED;
    256     return -1;
    257   }
    258 
    259   /* Check framesize. */
    260   if (nrOfSamples > WEBRTC_CNG_MAX_OUTSIZE_ORDER) {
    261     inst->errorcode = CNG_DISALLOWED_FRAME_SIZE;
    262     return -1;
    263   }
    264 
    265   for (i = 0; i < nrOfSamples; i++) {
    266     speechBuf[i] = speech[i];
    267   }
    268 
    269   factor = nrOfSamples;
    270 
    271   /* Calculate energy and a coefficients. */
    272   outEnergy = WebRtcSpl_Energy(speechBuf, nrOfSamples, &outShifts);
    273   while (outShifts > 0) {
    274     /* We can only do 5 shifts without destroying accuracy in
    275      * division factor. */
    276     if (outShifts > 5) {
    277       outEnergy <<= (outShifts - 5);
    278       outShifts = 5;
    279     } else {
    280       factor /= 2;
    281       outShifts--;
    282     }
    283   }
    284   outEnergy = WebRtcSpl_DivW32W16(outEnergy, factor);
    285 
    286   if (outEnergy > 1) {
    287     /* Create Hanning Window. */
    288     WebRtcSpl_GetHanningWindow(hanningW, nrOfSamples / 2);
    289     for (i = 0; i < (nrOfSamples / 2); i++)
    290       hanningW[nrOfSamples - i - 1] = hanningW[i];
    291 
    292     WebRtcSpl_ElementwiseVectorMult(speechBuf, hanningW, speechBuf, nrOfSamples,
    293                                     14);
    294 
    295     WebRtcSpl_AutoCorrelation(speechBuf, nrOfSamples, inst->enc_nrOfCoefs,
    296                               corrVector, &acorrScale);
    297 
    298     if (*corrVector == 0)
    299       *corrVector = WEBRTC_SPL_WORD16_MAX;
    300 
    301     /* Adds the bandwidth expansion. */
    302     aptr = WebRtcCng_kCorrWindow;
    303     bptr = corrVector;
    304 
    305     /* (zzz) lpc16_1 = 17+1+820+2+2 = 842 (ordo2=700). */
    306     for (ind = 0; ind < inst->enc_nrOfCoefs; ind++) {
    307       /* The below code multiplies the 16 b corrWindow values (Q15) with
    308        * the 32 b corrvector (Q0) and shifts the result down 15 steps. */
    309       negate = *bptr < 0;
    310       if (negate)
    311         *bptr = -*bptr;
    312 
    313       blo = (int32_t) * aptr * (*bptr & 0xffff);
    314       bhi = ((blo >> 16) & 0xffff)
    315           + ((int32_t)(*aptr++) * ((*bptr >> 16) & 0xffff));
    316       blo = (blo & 0xffff) | ((bhi & 0xffff) << 16);
    317 
    318       *bptr = (((bhi >> 16) & 0x7fff) << 17) | ((uint32_t) blo >> 15);
    319       if (negate)
    320         *bptr = -*bptr;
    321       bptr++;
    322     }
    323     /* End of bandwidth expansion. */
    324 
    325     stab = WebRtcSpl_LevinsonDurbin(corrVector, arCoefs, refCs,
    326                                     inst->enc_nrOfCoefs);
    327 
    328     if (!stab) {
    329       /* Disregard from this frame */
    330       *bytesOut = 0;
    331       return 0;
    332     }
    333 
    334   } else {
    335     for (i = 0; i < inst->enc_nrOfCoefs; i++)
    336       refCs[i] = 0;
    337   }
    338 
    339   if (forceSID) {
    340     /* Read instantaneous values instead of averaged. */
    341     for (i = 0; i < inst->enc_nrOfCoefs; i++)
    342       inst->enc_reflCoefs[i] = refCs[i];
    343     inst->enc_Energy = outEnergy;
    344   } else {
    345     /* Average history with new values. */
    346     for (i = 0; i < (inst->enc_nrOfCoefs); i++) {
    347       inst->enc_reflCoefs[i] = (int16_t) WEBRTC_SPL_MUL_16_16_RSFT(
    348           inst->enc_reflCoefs[i], ReflBeta, 15);
    349       inst->enc_reflCoefs[i] += (int16_t) WEBRTC_SPL_MUL_16_16_RSFT(
    350           refCs[i], ReflBetaComp, 15);
    351     }
    352     inst->enc_Energy = (outEnergy >> 2) + (inst->enc_Energy >> 1)
    353         + (inst->enc_Energy >> 2);
    354   }
    355 
    356   if (inst->enc_Energy < 1) {
    357     inst->enc_Energy = 1;
    358   }
    359 
    360   if ((inst->enc_msSinceSID > (inst->enc_interval - 1)) || forceSID) {
    361 
    362     /* Search for best dbov value. */
    363     index = 0;
    364     for (i = 1; i < 93; i++) {
    365       /* Always round downwards. */
    366       if ((inst->enc_Energy - WebRtcCng_kDbov[i]) > 0) {
    367         index = i;
    368         break;
    369       }
    370     }
    371     if ((i == 93) && (index == 0))
    372       index = 94;
    373     SIDdata[0] = index;
    374 
    375     /* Quantize coefficients with tweak for WebRtc implementation of RFC3389. */
    376     if (inst->enc_nrOfCoefs == WEBRTC_CNG_MAX_LPC_ORDER) {
    377       for (i = 0; i < inst->enc_nrOfCoefs; i++) {
    378         /* Q15 to Q7 with rounding. */
    379         SIDdata[i + 1] = ((inst->enc_reflCoefs[i] + 128) >> 8);
    380       }
    381     } else {
    382       for (i = 0; i < inst->enc_nrOfCoefs; i++) {
    383         /* Q15 to Q7 with rounding. */
    384         SIDdata[i + 1] = (127 + ((inst->enc_reflCoefs[i] + 128) >> 8));
    385       }
    386     }
    387 
    388     inst->enc_msSinceSID = 0;
    389     *bytesOut = inst->enc_nrOfCoefs + 1;
    390 
    391     inst->enc_msSinceSID += (1000 * nrOfSamples) / inst->enc_sampfreq;
    392     return inst->enc_nrOfCoefs + 1;
    393   } else {
    394     inst->enc_msSinceSID += (1000 * nrOfSamples) / inst->enc_sampfreq;
    395     *bytesOut = 0;
    396     return 0;
    397   }
    398 }
    399 
    400 /****************************************************************************
    401  * WebRtcCng_UpdateSid(...)
    402  *
    403  * These functions updates the CN state, when a new SID packet arrives
    404  *
    405  * Input:
    406  *    - cng_inst      : Pointer to created instance that should be freed
    407  *    - SID           : SID packet, all headers removed
    408  *    - length        : Length in bytes of SID packet
    409  *
    410  * Return value       :  0 - Ok
    411  *                      -1 - Error
    412  */
    413 int16_t WebRtcCng_UpdateSid(CNG_dec_inst* cng_inst, uint8_t* SID,
    414                             int16_t length) {
    415 
    416   WebRtcCngDecInst_t* inst = (WebRtcCngDecInst_t*) cng_inst;
    417   int16_t refCs[WEBRTC_CNG_MAX_LPC_ORDER];
    418   int32_t targetEnergy;
    419   int i;
    420 
    421   if (inst->initflag != 1) {
    422     inst->errorcode = CNG_DECODER_NOT_INITIATED;
    423     return -1;
    424   }
    425 
    426   /* Throw away reflection coefficients of higher order than we can handle. */
    427   if (length > (WEBRTC_CNG_MAX_LPC_ORDER + 1))
    428     length = WEBRTC_CNG_MAX_LPC_ORDER + 1;
    429 
    430   inst->dec_order = length - 1;
    431 
    432   if (SID[0] > 93)
    433     SID[0] = 93;
    434   targetEnergy = WebRtcCng_kDbov[SID[0]];
    435   /* Take down target energy to 75%. */
    436   targetEnergy = targetEnergy >> 1;
    437   targetEnergy += targetEnergy >> 2;
    438 
    439   inst->dec_target_energy = targetEnergy;
    440 
    441   /* Reconstruct coeffs with tweak for WebRtc implementation of RFC3389. */
    442   if (inst->dec_order == WEBRTC_CNG_MAX_LPC_ORDER) {
    443     for (i = 0; i < (inst->dec_order); i++) {
    444       refCs[i] = SID[i + 1] << 8; /* Q7 to Q15*/
    445       inst->dec_target_reflCoefs[i] = refCs[i];
    446     }
    447   } else {
    448     for (i = 0; i < (inst->dec_order); i++) {
    449       refCs[i] = (SID[i + 1] - 127) << 8; /* Q7 to Q15. */
    450       inst->dec_target_reflCoefs[i] = refCs[i];
    451     }
    452   }
    453 
    454   for (i = (inst->dec_order); i < WEBRTC_CNG_MAX_LPC_ORDER; i++) {
    455     refCs[i] = 0;
    456     inst->dec_target_reflCoefs[i] = refCs[i];
    457   }
    458 
    459   return 0;
    460 }
    461 
    462 /****************************************************************************
    463  * WebRtcCng_Generate(...)
    464  *
    465  * These functions generates CN data when needed
    466  *
    467  * Input:
    468  *    - cng_inst      : Pointer to created instance that should be freed
    469  *    - outData       : pointer to area to write CN data
    470  *    - nrOfSamples   : How much data to generate
    471  *
    472  * Return value        :  0 - Ok
    473  *                       -1 - Error
    474  */
    475 int16_t WebRtcCng_Generate(CNG_dec_inst* cng_inst, int16_t* outData,
    476                            int16_t nrOfSamples, int16_t new_period) {
    477   WebRtcCngDecInst_t* inst = (WebRtcCngDecInst_t*) cng_inst;
    478 
    479   int i;
    480   int16_t excitation[WEBRTC_CNG_MAX_OUTSIZE_ORDER];
    481   int16_t low[WEBRTC_CNG_MAX_OUTSIZE_ORDER];
    482   int16_t lpPoly[WEBRTC_CNG_MAX_LPC_ORDER + 1];
    483   int16_t ReflBetaStd = 26214;  /* 0.8 in q15. */
    484   int16_t ReflBetaCompStd = 6553;  /* 0.2 in q15. */
    485   int16_t ReflBetaNewP = 19661;  /* 0.6 in q15. */
    486   int16_t ReflBetaCompNewP = 13107;  /* 0.4 in q15. */
    487   int16_t Beta, BetaC, tmp1, tmp2, tmp3;
    488   int32_t targetEnergy;
    489   int16_t En;
    490   int16_t temp16;
    491 
    492   if (nrOfSamples > WEBRTC_CNG_MAX_OUTSIZE_ORDER) {
    493     inst->errorcode = CNG_DISALLOWED_FRAME_SIZE;
    494     return -1;
    495   }
    496 
    497   if (new_period) {
    498     inst->dec_used_scale_factor = inst->dec_target_scale_factor;
    499     Beta = ReflBetaNewP;
    500     BetaC = ReflBetaCompNewP;
    501   } else {
    502     Beta = ReflBetaStd;
    503     BetaC = ReflBetaCompStd;
    504   }
    505 
    506   /* Here we use a 0.5 weighting, should possibly be modified to 0.6. */
    507   tmp1 = inst->dec_used_scale_factor << 2; /* Q13->Q15 */
    508   tmp2 = inst->dec_target_scale_factor << 2; /* Q13->Q15 */
    509   tmp3 = (int16_t) WEBRTC_SPL_MUL_16_16_RSFT(tmp1, Beta, 15);
    510   tmp3 += (int16_t) WEBRTC_SPL_MUL_16_16_RSFT(tmp2, BetaC, 15);
    511   inst->dec_used_scale_factor = tmp3 >> 2; /* Q15->Q13 */
    512 
    513   inst->dec_used_energy = inst->dec_used_energy >> 1;
    514   inst->dec_used_energy += inst->dec_target_energy >> 1;
    515 
    516   /* Do the same for the reflection coeffs. */
    517   for (i = 0; i < WEBRTC_CNG_MAX_LPC_ORDER; i++) {
    518     inst->dec_used_reflCoefs[i] = (int16_t) WEBRTC_SPL_MUL_16_16_RSFT(
    519         inst->dec_used_reflCoefs[i], Beta, 15);
    520     inst->dec_used_reflCoefs[i] += (int16_t) WEBRTC_SPL_MUL_16_16_RSFT(
    521         inst->dec_target_reflCoefs[i], BetaC, 15);
    522   }
    523 
    524   /* Compute the polynomial coefficients. */
    525   WebRtcCng_K2a16(inst->dec_used_reflCoefs, WEBRTC_CNG_MAX_LPC_ORDER, lpPoly);
    526 
    527 
    528   targetEnergy = inst->dec_used_energy;
    529 
    530   /* Calculate scaling factor based on filter energy. */
    531   En = 8192;  /* 1.0 in Q13. */
    532   for (i = 0; i < (WEBRTC_CNG_MAX_LPC_ORDER); i++) {
    533 
    534     /* Floating point value for reference.
    535        E *= 1.0 - (inst->dec_used_reflCoefs[i] / 32768.0) *
    536        (inst->dec_used_reflCoefs[i] / 32768.0);
    537      */
    538 
    539     /* Same in fixed point. */
    540     /* K(i).^2 in Q15. */
    541     temp16 = (int16_t) WEBRTC_SPL_MUL_16_16_RSFT(
    542         inst->dec_used_reflCoefs[i], inst->dec_used_reflCoefs[i], 15);
    543     /* 1 - K(i).^2 in Q15. */
    544     temp16 = 0x7fff - temp16;
    545     En = (int16_t) WEBRTC_SPL_MUL_16_16_RSFT(En, temp16, 15);
    546   }
    547 
    548   /* float scaling= sqrt(E * inst->dec_target_energy / (1 << 24)); */
    549 
    550   /* Calculate sqrt(En * target_energy / excitation energy) */
    551   targetEnergy = WebRtcSpl_Sqrt(inst->dec_used_energy);
    552 
    553   En = (int16_t) WebRtcSpl_Sqrt(En) << 6;
    554   En = (En * 3) >> 1;  /* 1.5 estimates sqrt(2). */
    555   inst->dec_used_scale_factor = (int16_t)((En * targetEnergy) >> 12);
    556 
    557   /* Generate excitation. */
    558   /* Excitation energy per sample is 2.^24 - Q13 N(0,1). */
    559   for (i = 0; i < nrOfSamples; i++) {
    560     excitation[i] = WebRtcSpl_RandN(&inst->dec_seed) >> 1;
    561   }
    562 
    563   /* Scale to correct energy. */
    564   WebRtcSpl_ScaleVector(excitation, excitation, inst->dec_used_scale_factor,
    565                         nrOfSamples, 13);
    566 
    567   /* |lpPoly| - Coefficients in Q12.
    568    * |excitation| - Speech samples.
    569    * |nst->dec_filtstate| - State preservation.
    570    * |outData| - Filtered speech samples. */
    571   WebRtcSpl_FilterAR(lpPoly, WEBRTC_CNG_MAX_LPC_ORDER + 1, excitation,
    572                      nrOfSamples, inst->dec_filtstate, WEBRTC_CNG_MAX_LPC_ORDER,
    573                      inst->dec_filtstateLow, WEBRTC_CNG_MAX_LPC_ORDER, outData,
    574                      low, nrOfSamples);
    575 
    576   return 0;
    577 }
    578 
    579 /****************************************************************************
    580  * WebRtcCng_GetErrorCodeEnc/Dec(...)
    581  *
    582  * This functions can be used to check the error code of a CNG instance. When
    583  * a function returns -1 a error code will be set for that instance. The
    584  * function below extract the code of the last error that occured in the
    585  * specified instance.
    586  *
    587  * Input:
    588  *    - CNG_inst    : CNG enc/dec instance
    589  *
    590  * Return value     : Error code
    591  */
    592 int16_t WebRtcCng_GetErrorCodeEnc(CNG_enc_inst* cng_inst) {
    593   /* Typecast pointer to real structure. */
    594   WebRtcCngEncInst_t* inst = (WebRtcCngEncInst_t*) cng_inst;
    595   return inst->errorcode;
    596 }
    597 
    598 int16_t WebRtcCng_GetErrorCodeDec(CNG_dec_inst* cng_inst) {
    599   /* Typecast pointer to real structure. */
    600   WebRtcCngDecInst_t* inst = (WebRtcCngDecInst_t*) cng_inst;
    601   return inst->errorcode;
    602 }
    603