LCOV - code coverage report
Current view: top level - lib_lc3plus - setup_dec_lc3plus.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ 867217ee32c8e8cd2cf5aae69e60c58e00160b49 Lines: 242 331 73.1 %
Date: 2025-12-13 06:47:12 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /******************************************************************************
       2             : *                        ETSI TS 103 634 V1.6.1                               *
       3             : *              Low Complexity Communication Codec Plus (LC3plus)              *
       4             : *                                                                             *
       5             : * Copyright licence is solely granted through ETSI Intellectual Property      *
       6             : * Rights Policy, 3rd April 2019. No patent licence is granted by implication, *
       7             : * estoppel or otherwise.                                                      *
       8             : ******************************************************************************/
       9             : 
      10             : #include "options.h"
      11             : #include "wmc_auto.h"
      12             : #include "setup_dec_lc3plus.h"
      13             : #include "functions.h"
      14             : #include <stdio.h>
      15             : #include <assert.h>
      16             : 
      17             : /* if decoder is null only size is reported */
      18             : #include "fft/iis_fft.h"
      19             : 
      20        6594 : int alloc_decoder(LC3PLUS_Dec* decoder, int samplerate, int channels)
      21             : {
      22        6594 :    int    ch   = 0;
      23        6594 :    size_t size = sizeof(LC3PLUS_Dec);
      24        6594 :    size_t frame_len = DYN_MAX_LEN_EXT(samplerate);
      25             : 
      26        6594 :    void *PlcAdvSetup = NULL;
      27             :    LC3_FLOAT *pcmbufHist, *harmonicBuf;
      28             :    LC3_FLOAT *PhECU_oold_grp_shape, *PhECU_old_grp_shape;
      29             :    LC3_FLOAT *PhECU_xfp;
      30             :    Complex   *PhECU_X_sav_m;
      31             :    LC3_INT32   *PhECU_plocs;
      32             :    LC3_FLOAT *PhECU_f0est, *PhECU_mag_chg_1st, *PhECU_Xavg;
      33             :    LC3_FLOAT *sine_table1_phecu, *sine_table2_phecu;
      34             :    HANDLE_IIS_FFT handle_fft_phaseecu;
      35             :    HANDLE_IIS_FFT handle_ifft_phaseecu;
      36             :    LC3_FLOAT *q_old_res;
      37             : 
      38        6594 :    LC3_INT32 * plc_longterm_advc_tdc = NULL, *plc_longterm_advc_ns = NULL;
      39        6594 :    LC3_INT16 longterm_analysis_counter_max = 0, longterm_analysis_counter_max_bytebuffer = 0;
      40             : 
      41       13188 :     for (ch = 0; ch < channels; ch++) {
      42        6594 :         DecSetup* setup = balloc(decoder, &size, sizeof(DecSetup));
      43             : 
      44        6594 :          size_t max_pitch = ceilf(228.0 * CODEC_FS(samplerate) / 12800.0);
      45        6594 :          size_t pcm_plc_len = max_pitch + frame_len;
      46        6594 :          pcmbufHist = balloc(decoder, &size, sizeof(LC3_FLOAT) * pcm_plc_len);
      47        6594 :          harmonicBuf = balloc(decoder, &size, sizeof(LC3_FLOAT) * max_pitch);
      48        6594 :          PlcAdvSetup = balloc(decoder, &size, sizeof(*setup->PlcAdvSetup));
      49        6594 :          PhECU_oold_grp_shape = balloc(decoder, &size, sizeof(LC3_FLOAT) *MAX_LGW);          /* BASOP Word16  PhECU_oold_grp_shape_fx[MAX_LGW]; */
      50        6594 :          PhECU_old_grp_shape = balloc(decoder, &size, sizeof(LC3_FLOAT) *MAX_LGW);          /* BASOP Word16  PhECU_old_grp_shape_fx[MAX_LGW] ; */
      51        6594 :          PhECU_xfp = balloc(decoder, &size, sizeof(LC3_FLOAT) *(frame_len * 16 / 10));
      52        6594 :          PhECU_X_sav_m = balloc(decoder, &size, sizeof(Complex) *(((frame_len * 16 / 10) / 2) + 1));/*MAX_PLC_LMSPEC*/
      53        6594 :          PhECU_plocs = balloc(decoder, &size, sizeof(LC3_INT32) * (((frame_len * 16 / 10) / 4) + 1 + 1));   /* BASOP Word16 *PhECU_plocs;    */
      54             : 
      55        6594 :          handle_fft_phaseecu = balloc(decoder, &size, sizeof(IIS_FFT) * 1);
      56        6594 :          handle_ifft_phaseecu = balloc(decoder, &size, sizeof(IIS_FFT) * 1);
      57        6594 :          PhECU_f0est = balloc(decoder, &size, sizeof(LC3_FLOAT) * (((frame_len * 16 / 10) / 4) + 1));        /*BASOP Word32 *PhECU_f0est;*/
      58        6594 :          PhECU_mag_chg_1st = balloc(decoder, &size, sizeof(LC3_FLOAT) *MAX_LGW);                /*  BASOP Word16  PhECU_mag_chg_1st[MAX_LGW];*/
      59        6594 :          PhECU_Xavg = balloc(decoder, &size, sizeof(LC3_FLOAT) * MAX_LGW);               /*  BASOP Word16  PhECU_Xavg[MAX_LGW] ; */
      60             : 
      61        6594 :          sine_table1_phecu = balloc(decoder, &size, sizeof(LC3_FLOAT) * (((CODEC_FS(samplerate) * 16) / 1000) / 2 + 1));
      62        6594 :          sine_table2_phecu = balloc(decoder, &size, sizeof(LC3_FLOAT) * (((CODEC_FS(samplerate) * 16) / 1000) / 2 + 1));
      63             : 
      64        6594 :          longterm_analysis_counter_max = plc_fadeout_param_maxlen[0];
      65        6594 :          longterm_analysis_counter_max_bytebuffer = plc_fadeout_param_maxbytes[0];
      66             : 
      67        6594 :          plc_longterm_advc_tdc = balloc(decoder, &size, sizeof(LC3_INT32) * longterm_analysis_counter_max_bytebuffer);
      68        6594 :          plc_longterm_advc_ns  = balloc(decoder, &size, sizeof(LC3_INT32) * longterm_analysis_counter_max_bytebuffer);
      69             : 
      70        6594 :          q_old_res = balloc(decoder, &size, sizeof(LC3_FLOAT) * frame_len);
      71             : 
      72        6594 :         if (decoder) {
      73        2198 :             decoder->channel_setup[ch] = setup;
      74             : #ifdef CR9_C_ADD_1p25MS
      75        2198 :             setup->ltpf_mem_continuation = 0;
      76             : #ifdef FIX_TX_RX_STRUCT_STEREO
      77        2198 :             setup->ltpf_rx_status[0] = 0;
      78        2198 :             setup->ltpf_rx_status[1] = 0;
      79             : #endif
      80             : #ifdef NEW_SIGNALLING_SCHEME_1p25
      81        2198 :             setup->ltpfinfo_frame_cntr = -32768;
      82             : #endif
      83             : #endif
      84             : 
      85        2198 :             setup->PlcAdvSetup = PlcAdvSetup;
      86             : 
      87        2198 :             setup->PlcAdvSetup->pcmbufHist = pcmbufHist;
      88        2198 :             setup->PlcAdvSetup->PlcTdcSetup.harmonicBuf = harmonicBuf;
      89        2198 :             setup->PlcAdvSetup->PlcPhEcuSetup.PhECU_oold_grp_shape = PhECU_oold_grp_shape;
      90        2198 :             setup->PlcAdvSetup->PlcPhEcuSetup.PhECU_old_grp_shape = PhECU_old_grp_shape;
      91        2198 :             setup->PlcAdvSetup->PlcPhEcuSetup.PhECU_xfp = PhECU_xfp;
      92        2198 :             setup->PlcAdvSetup->PlcPhEcuSetup.PhECU_X_sav_m = PhECU_X_sav_m;
      93        2198 :             setup->PlcAdvSetup->PlcPhEcuSetup.PhECU_plocs = PhECU_plocs;
      94        2198 :             setup->PlcAdvSetup->PlcPhEcuSetup.PhECU_f0est = PhECU_f0est;
      95        2198 :             setup->PlcAdvSetup->PlcPhEcuSetup.PhECU_mag_chg_1st = PhECU_mag_chg_1st;
      96        2198 :             setup->PlcAdvSetup->PlcPhEcuSetup.PhECU_Xavg = PhECU_Xavg;
      97        2198 :             setup->PlcAdvSetup->PlcPhEcuSetup.handle_fft_phaseecu = handle_fft_phaseecu;
      98        2198 :             setup->PlcAdvSetup->PlcPhEcuSetup.handle_ifft_phaseecu = handle_ifft_phaseecu;
      99             : 
     100        2198 :             setup->PlcAdvSetup->PlcPhEcuSetup.handle_fft_phaseecu->sine_table = sine_table1_phecu;
     101        2198 :             setup->PlcAdvSetup->PlcPhEcuSetup.handle_ifft_phaseecu->sine_table = sine_table2_phecu;
     102             : 
     103        2198 :             setup->PlcAdvSetup->longterm_analysis_counter_max = longterm_analysis_counter_max;
     104        2198 :             setup->PlcAdvSetup->longterm_analysis_counter_max_bytebuffer = longterm_analysis_counter_max_bytebuffer;
     105             : 
     106        2198 :             setup->PlcAdvSetup->plc_longterm_advc_tdc = plc_longterm_advc_tdc;
     107        2198 :             setup->PlcAdvSetup->plc_longterm_advc_ns  = plc_longterm_advc_ns;
     108             : 
     109        2198 :             setup->PlcAdvSetup->PlcPhEcuSetup.PhECU_Lprot = (CODEC_FS(samplerate) * 16) / 1000;
     110        2198 :             real_fft_init(&(setup->PlcAdvSetup->PlcPhEcuSetup.PhEcu_Fft), setup->PlcAdvSetup->PlcPhEcuSetup.PhECU_Lprot, &(setup->PlcAdvSetup->PlcPhEcuSetup.handle_fft_phaseecu));
     111        2198 :             real_ifft_init(&(setup->PlcAdvSetup->PlcPhEcuSetup.PhEcu_Ifft), setup->PlcAdvSetup->PlcPhEcuSetup.PhECU_Lprot, &(setup->PlcAdvSetup->PlcPhEcuSetup.handle_ifft_phaseecu));
     112        2198 :             setup->statePC.q_old_res = q_old_res;
     113             :         }
     114             :     }
     115             : 
     116        6594 :     return (int)size;
     117             : }
     118             : 
     119        2198 : LC3PLUS_Error FillDecSetup(LC3PLUS_Dec* decoder, int samplerate, int channels, LC3PLUS_PlcMode plc_mode
     120             :                             , int hrmode
     121             : )
     122             : {
     123        2198 :     memset(decoder, 0, lc3plus_dec_get_size(samplerate, channels));
     124        2198 :     alloc_decoder(decoder, samplerate, channels);
     125             : 
     126        2198 :     decoder->fs     = CODEC_FS(samplerate);
     127        2198 :     decoder->fs_out = samplerate;
     128        2198 :     decoder->fs_idx = FS2FS_IDX(decoder->fs);
     129        2198 :     decoder->plcMeth = plc_mode;
     130             : 
     131        2198 :     decoder->hrmode = hrmode != 0;
     132             : 
     133        2198 :     decoder->channels       = channels;
     134        2198 :     decoder->frame_ms       = LC3PLUS_FRAME_DURATION_10MS;
     135        2198 :     decoder->frame_dms      = LC3PLUS_FRAME_DURATION_10MS;
     136        2198 :     decoder->BW_cutoff_bits = BW_cutoff_bits_all[decoder->fs_idx];
     137             : 
     138        2198 :     if (decoder->fs == 8000) {
     139           0 :         decoder->tilt = 14;
     140        2198 :     } else if (decoder->fs == 16000) {
     141           2 :         decoder->tilt = 18;
     142        2196 :     } else if (decoder->fs == 24000) {
     143           0 :         decoder->tilt = 22;
     144        2196 :     } else if (decoder->fs == 32000) {
     145           2 :         decoder->tilt = 26;
     146        2194 :     } else if (decoder->fs == 48000) {
     147        2194 :         decoder->tilt = 30;
     148             :     }
     149           0 :     else if (decoder->fs == 96000) {
     150           0 :         decoder->tilt = 34;
     151             :     }
     152             : 
     153        2198 :     set_dec_frame_params(decoder);
     154             : 
     155        2198 :     lc3plus_dec_set_ep_enabled(decoder, 0);
     156             : 
     157        2198 :     return LC3PLUS_OK;
     158             : }
     159             : 
     160             : /* set frame config params */
     161        4395 : void set_dec_frame_params(LC3PLUS_Dec* decoder)
     162             : {
     163        4395 :     int ch = 0, idx = 0;
     164             :     int n;
     165             : 
     166        4395 :     if (decoder->fs_idx == 5)
     167             :     {
     168           0 :         decoder->hrmode = 1;
     169             :     }
     170             : 
     171        4395 :     decoder->frame_length = ceil(decoder->fs * 10 / 1000); /* fs * 0.01*2^6 */
     172        4395 :     if (decoder->hrmode == 1)
     173             :     {
     174           0 :         decoder->yLen = decoder->frame_length;
     175             :     }
     176             :     else
     177             :     {
     178        4395 :         decoder->yLen = MIN(MAX_BW, decoder->frame_length);
     179             :     }
     180             : 
     181             : #ifndef FIX_TX_RX_STRUCT_STEREO
     182             :     decoder->ltpf_rx_status[0] = 0;
     183             :     decoder->ltpf_rx_status[1] = 0;
     184             : #endif
     185             : 
     186        4395 :     decoder->bands_number = 64;
     187             : #ifdef CR9_C_ADD_1p25MS
     188        4395 :     if (decoder->frame_ms == LC3PLUS_FRAME_DURATION_1p25MS)
     189             :     {
     190           0 :             decoder->frame_length = decoder->frame_length >> 3;
     191           0 :             decoder->yLen /= 8;
     192             : 
     193           0 :             if (decoder->hrmode)
     194             :             {
     195           0 :                 assert(0);
     196             :             }
     197             :             else
     198             :             {
     199           0 :                 decoder->bands_number = bands_number_1_25ms[decoder->fs_idx];
     200             :             }
     201             : 
     202           0 :             decoder->bands_offset = ACC_COEFF_PER_BAND_1_25ms[decoder->fs_idx];
     203           0 :             decoder->BW_cutoff_bits = 0;
     204           0 :             decoder->cutoffBins     = BW_cutoff_bin_all_1_25ms;
     205             : 
     206           0 :             decoder->imdct_win     = MDCT_WINS_1_25ms[decoder->hrmode][decoder->fs_idx];
     207           0 :             decoder->imdct_laZeros = MDCT_la_zeroes_1_25ms[decoder->fs_idx];
     208           0 :             decoder->imdct_winLen  = MDCT_WINDOWS_LENGTHS_1_25ms[decoder->fs_idx];
     209             : 
     210           0 :             decoder->bands_offsetPLC = ACC_COEFF_PER_BAND_PLC_1_25ms[decoder->fs_idx];
     211           0 :             decoder->n_bandsPLC      = decoder->frame_length;
     212             :     }
     213             : #endif
     214        4395 :     if (decoder->frame_ms == LC3PLUS_FRAME_DURATION_2p5MS)
     215             :     {
     216           2 :         decoder->frame_length = decoder->frame_length >> 2;
     217           2 :         decoder->yLen /= 4;
     218           2 :         if (decoder->hrmode)
     219             :         {
     220           0 :             decoder->bands_number = bands_number_2_5ms_HR[decoder->fs_idx];
     221             :         }
     222             :         else
     223             :         {
     224           2 :             decoder->bands_number = bands_number_2_5ms[decoder->fs_idx];
     225             :         }
     226             :     }
     227        4395 :     if (decoder->frame_ms == LC3PLUS_FRAME_DURATION_5MS)
     228             :     {
     229        2014 :         decoder->frame_length = decoder->frame_length >> 1;
     230        2014 :         decoder->yLen /= 2;
     231        2014 :         decoder->bands_number = bands_number_5ms[decoder->fs_idx];
     232             :     }
     233        4395 :     if (decoder->frame_ms == LC3PLUS_FRAME_DURATION_7p5MS)
     234             :     {
     235           0 :         decoder->frame_length = (decoder->frame_length >> 2) * 3;
     236           0 :         decoder->yLen = (decoder->yLen / 4) * 3;
     237           0 :         if (decoder->hrmode)
     238             :         {
     239           0 :             decoder->bands_number = bands_number_7_5ms_HR[decoder->fs_idx];
     240             :         }
     241             :         else
     242             :         {
     243           0 :             decoder->bands_number = bands_number_7_5ms[decoder->fs_idx];
     244             :         }
     245             :     }
     246             : 
     247        4395 :     if (decoder->hrmode)
     248             :     {
     249           0 :         decoder->BW_cutoff_bits    = 0;
     250             :     }
     251             : #ifdef CR9_C_ADD_1p25MS
     252        4395 :     else if (decoder->frame_ms == LC3PLUS_FRAME_DURATION_1p25MS)
     253             :     {
     254           0 :         decoder->BW_cutoff_bits    = 0;
     255             :     }
     256             : #endif
     257             :     else
     258             :     {
     259        4395 :         decoder->BW_cutoff_bits    = BW_cutoff_bits_all[decoder->fs_idx];
     260             :     }
     261             : 
     262        4395 :     if (decoder->frame_ms == LC3PLUS_FRAME_DURATION_10MS)
     263             :     {
     264        2379 :         if (decoder->hrmode)
     265             :         {
     266           0 :             decoder->bands_offset = ACC_COEFF_PER_BAND_HR[decoder->fs_idx];
     267             :         }
     268             :         else
     269             :         {
     270        2379 :             decoder->bands_offset = ACC_COEFF_PER_BAND[decoder->fs_idx];
     271             :         }
     272        2379 :         decoder->cutoffBins   = BW_cutoff_bin_all;
     273             :     }
     274        2016 :     else if (decoder->frame_ms == LC3PLUS_FRAME_DURATION_2p5MS)
     275             :     {
     276           2 :         if (decoder->hrmode)
     277             :         {
     278           0 :             decoder->bands_offset = ACC_COEFF_PER_BAND_2_5ms_HR[decoder->fs_idx];
     279             :         }
     280             :         else
     281             :         {
     282           2 :             decoder->bands_offset = ACC_COEFF_PER_BAND_2_5ms[decoder->fs_idx];
     283             :         }
     284           2 :         decoder->cutoffBins   = BW_cutoff_bin_all_2_5ms;
     285             :     }
     286        2014 :     else if (decoder->frame_ms == LC3PLUS_FRAME_DURATION_5MS)
     287             :     {
     288        2014 :         if (decoder->hrmode)
     289             :         {
     290           0 :             decoder->bands_offset = ACC_COEFF_PER_BAND_5ms_HR[decoder->fs_idx];
     291             :         }
     292             :         else
     293             :         {
     294        2014 :             decoder->bands_offset = ACC_COEFF_PER_BAND_5ms[decoder->fs_idx];
     295             :         }
     296        2014 :         decoder->cutoffBins   = BW_cutoff_bin_all_5ms;
     297             :     }
     298           0 :     else if (decoder->frame_ms == LC3PLUS_FRAME_DURATION_7p5MS)
     299             :     {
     300           0 :         if (decoder->hrmode)
     301             :         {
     302           0 :             decoder->bands_offset = ACC_COEFF_PER_BAND_7_5ms_HR[decoder->fs_idx];
     303             :         }
     304             :         else
     305             :         {
     306           0 :             decoder->bands_offset = ACC_COEFF_PER_BAND_7_5ms[decoder->fs_idx];
     307             :         }
     308           0 :         decoder->cutoffBins   = BW_cutoff_bin_all_7_5ms;
     309             :     }
     310             : 
     311        4395 :     decoder->n_bandsPLC = MIN(decoder->frame_length, 80);
     312             : 
     313        4395 :     if (decoder->frame_ms == LC3PLUS_FRAME_DURATION_10MS)
     314             :     {
     315        2379 :         decoder->bands_offsetPLC = ACC_COEFF_PER_BAND_PLC[decoder->fs_idx];
     316             :     }
     317        2016 :     else if (decoder->frame_ms == LC3PLUS_FRAME_DURATION_5MS)
     318             :     {
     319        2014 :         decoder->bands_offsetPLC = ACC_COEFF_PER_BAND_PLC_5ms[decoder->fs_idx];
     320             : 
     321        2014 :         if (decoder->fs == 24000)
     322             :         {
     323           0 :             decoder->n_bandsPLC = 40;
     324             :         }
     325             :     }
     326           2 :     else if (decoder->frame_ms == LC3PLUS_FRAME_DURATION_2p5MS)
     327             :     {
     328           2 :         decoder->bands_offsetPLC = ACC_COEFF_PER_BAND_PLC_2_5ms[decoder->fs_idx];
     329             : 
     330           2 :         if (decoder->fs == 48000)
     331             :         {
     332           2 :             decoder->n_bandsPLC = 60;
     333             :         }
     334             :     }
     335             : #ifdef CR9_C_ADD_1p25MS
     336           0 :     else if (decoder->frame_ms == LC3PLUS_FRAME_DURATION_1p25MS)
     337             :     {
     338           0 :         decoder->bands_offsetPLC = ACC_COEFF_PER_BAND_PLC_1_25ms[decoder->fs_idx];
     339             : 
     340           0 :         if (decoder->fs == 48000)
     341             :         {
     342           0 :             decoder->n_bandsPLC = 60;
     343             :         }
     344             :     }
     345             : #endif
     346           0 :     else if (decoder->frame_ms == LC3PLUS_FRAME_DURATION_7p5MS)
     347             :     {
     348           0 :         decoder->bands_offsetPLC = ACC_COEFF_PER_BAND_PLC_7_5ms[decoder->fs_idx];
     349             : 
     350           0 :         if (decoder->fs != 32000 && decoder->fs != 96000)
     351             :         {
     352           0 :             decoder->n_bandsPLC = 60;
     353             :         }
     354             :     }
     355             : 
     356        4395 :     assert(decoder->bands_offsetPLC);
     357             : 
     358        4395 :     if (decoder->frame_ms == LC3PLUS_FRAME_DURATION_10MS) {
     359        2379 :         decoder->imdct_win     = MDCT_WINS_10ms[decoder->hrmode][decoder->fs_idx];
     360        2379 :         decoder->imdct_laZeros = MDCT_la_zeroes[decoder->fs_idx];
     361        2379 :         decoder->imdct_winLen  = MDCT_WINDOWS_LENGTHS_10ms[decoder->fs_idx];
     362             :     }
     363        2016 :     else if (decoder->frame_ms == LC3PLUS_FRAME_DURATION_2p5MS) {
     364           2 :         decoder->imdct_win     = MDCT_WINS_2_5ms[decoder->hrmode][decoder->fs_idx];
     365           2 :         decoder->imdct_laZeros = MDCT_la_zeroes_2_5ms[decoder->fs_idx];
     366           2 :         decoder->imdct_winLen  = MDCT_WINDOWS_LENGTHS_2_5ms[decoder->fs_idx];
     367             :     }
     368        2014 :     else if (decoder->frame_ms == LC3PLUS_FRAME_DURATION_5MS) {
     369        2014 :         decoder->imdct_win     = MDCT_WINS_5ms[decoder->hrmode][decoder->fs_idx];
     370        2014 :         decoder->imdct_laZeros = MDCT_la_zeroes_5ms[decoder->fs_idx];
     371        2014 :         decoder->imdct_winLen  = MDCT_WINDOWS_LENGTHS_5ms[decoder->fs_idx];
     372             :     }
     373           0 :     else if (decoder->frame_ms == LC3PLUS_FRAME_DURATION_7p5MS) {
     374           0 :         decoder->imdct_win     = MDCT_WINS_7_5ms[decoder->hrmode][decoder->fs_idx];
     375           0 :         decoder->imdct_laZeros = MDCT_la_zeroes_7_5ms[decoder->fs_idx];
     376           0 :                 decoder->imdct_winLen  = MDCT_WINDOWS_LENGTHS_7_5ms[decoder->fs_idx];
     377             :     }
     378             : #ifdef CR9_C_ADD_1p25MS
     379           0 :     else if (decoder->frame_ms == LC3PLUS_FRAME_DURATION_1p25MS) {
     380           0 :         decoder->imdct_win     = MDCT_WINS_1_25ms[decoder->hrmode][decoder->fs_idx];
     381           0 :         decoder->imdct_laZeros = MDCT_la_zeroes_1_25ms[decoder->fs_idx];
     382           0 :         decoder->imdct_winLen  = MDCT_WINDOWS_LENGTHS_1_25ms[decoder->fs_idx];
     383             :     }
     384             : #endif
     385             : 
     386        4395 :     decoder->la_zeroes = decoder->imdct_laZeros;
     387             : 
     388        4395 :     decoder->imdct_memLen = decoder->frame_length - decoder->imdct_laZeros;
     389             : 
     390        8790 :     for (ch = 0; ch < decoder->channels; ch++) {
     391        4395 :         DecSetup* setup = decoder->channel_setup[ch];
     392             : 
     393        4395 :         setup->ltpf_mem_beta_idx = -1;
     394             : 
     395        4395 :         setup->statePC.seed = 24607;
     396             : 
     397        4395 :         if (decoder) {
     398             :             /* Init DCT4 structs */
     399        4395 :             if (setup->dct4structImdct.length != 0) {
     400        2197 :                 dct4_free(&setup->dct4structImdct);
     401        2197 :                 dct4_init(&setup->dct4structImdct, decoder->frame_length);
     402             :             } else {
     403        2198 :                 dct4_init(&setup->dct4structImdct, decoder->frame_length);
     404             :             }
     405             : 
     406        4395 :             setup->PlcNsSetup.cum_alpha = 1;
     407        4395 :             setup->PlcNsSetup.seed = 24607;
     408        4395 :             setup->alpha = 1;
     409        4395 :             if (setup->PlcAdvSetup)
     410             :             {
     411        4395 :             LC3_INT32 pitch_max = 0, pitch_ana_len = 0, tdc_synt_len = 0;
     412        4395 :             pitch_max     = ceil(228.0 * (LC3_FLOAT) decoder->fs / 12800.0);
     413        4395 :                 pitch_ana_len = pitch_max + decoder->frame_length * (LC3_FLOAT) 100 / (decoder->frame_dms * 1.25 * 10);
     414        4395 :                 tdc_synt_len  = 16 + 1 + pitch_max  + ceil(decoder->frame_length / 2);
     415        4395 :                 setup->PlcAdvSetup->max_len_pcm_plc = MAX(pitch_ana_len, tdc_synt_len);
     416        4395 :                 setup->PlcAdvSetup->PlcTdcSetup.preemphFac = plc_preemph_fac[decoder->fs_idx];
     417        4395 :                 setup->PlcAdvSetup->PlcTdcSetup.seed = 24607;
     418        4395 :                 setup->PlcAdvSetup->PlcTdcSetup.lpcorder = 16;
     419             : 
     420        4395 :                 if ((decoder->fs_idx == 0 || decoder->frame_length <= 20) && decoder->frame_dms <= LC3PLUS_FRAME_DURATION_2p5MS)
     421             :                 {
     422           0 :                     setup->PlcAdvSetup->PlcTdcSetup.lpcorder = 8;
     423             :                 }
     424             : 
     425        4395 :                 setup->PlcAdvSetup->stabFac = 1;
     426        4395 :                 setup->PlcAdvSetup->cum_fading_fast = 1;
     427        4395 :                 setup->PlcAdvSetup->cum_fading_slow = 1;
     428        4395 :                 setup->PlcAdvSetup->cum_fflcAtten = 1;
     429             : 
     430        4395 :                 idx = (decoder->frame_dms * 1.25 * 10 / 25) - 1;
     431        4395 :                 setup->PlcAdvSetup->longterm_analysis_counter_max = plc_fadeout_param_maxlen[idx];
     432        4395 :                 setup->PlcAdvSetup->longterm_analysis_counter_max_bytebuffer = plc_fadeout_param_maxbytes[idx];
     433             : 
     434        4395 :                 if (decoder->fs_idx <= 4 && decoder->frame_dms == LC3PLUS_FRAME_DURATION_10MS)
     435             :                 {
     436        2379 :                    setup->PlcAdvSetup->PlcPhEcuSetup.PhECU_Lprot = (decoder->fs * 16) / 1000;  /* 16 ms of samples at fs*/
     437             : 
     438        2379 :                    setup->PlcAdvSetup->PlcPhEcuSetup.PhECU_f0hzLtpBin = 0;
     439        2379 :                    setup->PlcAdvSetup->PlcPhEcuSetup.PhECU_norm_corr = 0;
     440             : 
     441        2379 :                    set_vec(PHECU_GRP_SHAPE_INIT, setup->PlcAdvSetup->PlcPhEcuSetup.PhECU_oold_grp_shape, MAX_LGW);
     442        2379 :                    set_vec(PHECU_GRP_SHAPE_INIT, setup->PlcAdvSetup->PlcPhEcuSetup.PhECU_old_grp_shape, MAX_LGW);
     443             : 
     444        2379 :                    setup->PlcAdvSetup->PlcPhEcuSetup.PhECU_L_oold_xfp_w_E = (LC3_FLOAT)PHECU_LTOT_MIN_MAN * LC3_POW(2.0, PHECU_LTOT_MIN_EXP - 31);
     445        2379 :                    setup->PlcAdvSetup->PlcPhEcuSetup.PhECU_L_old_xfp_w_E = (LC3_FLOAT)PHECU_LTOT_MIN_MAN * LC3_POW(2.0, PHECU_LTOT_MIN_EXP - 31);
     446             : 
     447             :                    /* CFL uses separate buffers for pcmHist,  xfp and Xsav and q_d , BASOP uses an optimized joint buffer*/
     448        2379 :                    zero_float(setup->PlcAdvSetup->PlcPhEcuSetup.PhECU_xfp, setup->PlcAdvSetup->PlcPhEcuSetup.PhECU_Lprot);
     449        2379 :                    zero_cmplx(setup->PlcAdvSetup->PlcPhEcuSetup.PhECU_X_sav_m, (setup->PlcAdvSetup->PlcPhEcuSetup.PhECU_Lprot/2 + 1));
     450             : 
     451        2379 :                    set_vec(POS_ONE_Q15, setup->PlcAdvSetup->PlcPhEcuSetup.PhECU_mag_chg_1st,  MAX_LGW);
     452             : 
     453        2379 :                    setup->PlcAdvSetup->PlcPhEcuSetup.PhECU_beta_mute = (16384.0/32768.0);
     454        2379 :                    setup->PlcAdvSetup->PlcPhEcuSetup.PhECU_seed = 21845;
     455             : 
     456        2379 :                    assert(decoder->frame_dms == LC3PLUS_FRAME_DURATION_10MS);
     457        2379 :                    setup->PlcAdvSetup->PlcPhEcuSetup.PhECU_LDWIN_OLAP =  (decoder->frame_length / 4 );   /* 2.5 ms for regular 10 ms MDCT */
     458             : 
     459        2379 :                    setup->PlcAdvSetup->PlcPhEcuSetup.PhECU_t_adv = (
     460        2379 :                       decoder->frame_length
     461        2379 :                       + setup->PlcAdvSetup->PlcPhEcuSetup.PhECU_Lprot
     462        2379 :                       + setup->PlcAdvSetup->PlcPhEcuSetup.PhECU_LDWIN_OLAP )/ 2;
     463             :                 }
     464        4395 :                 setup->PlcAdvSetup->PlcPhEcuSetup.PhECU_short_flag_prev  = 0;    /* fullband transient  */
     465        4395 :                 setup->PlcAdvSetup->PlcPhEcuSetup.PhECU_num_plocs = 0;
     466        4395 :                                 setup->PlcAdvSetup->PlcPhEcuSetup.PhECU_nonpure_tone_flag = -1;  /* nonpure tone flag,  -1==new calc., 0==pure, 1==nonpure  */
     467             :             }
     468             :         }
     469             :     }
     470      132695 :     for (n=0; n < LC3_ROUND(PLC_FADEOUT_TYPE_1_IN_MS*10/(decoder->frame_dms*1.25*10));n++){
     471      128300 :         decoder->alpha_type_2_table[n] = type_2_fadeout(n, decoder->frame_dms);
     472             :     }
     473        4395 : }
     474             : 
     475       62823 : LC3PLUS_Error update_dec_bitrate(LC3PLUS_Dec* decoder, int ch, int nBytes)
     476             : {
     477       62823 :     int totalBits = 0, bitsTmp = 0, channel_bytes = 0, maxBytes = 0, minBytes = 0;
     478             :     DecSetup* setup;
     479             : 
     480       62823 :     if (decoder->hrmode)
     481             :     {
     482           0 :         switch (decoder->frame_dms)
     483             :         {
     484             : #ifdef CR9_C_ADD_1p25MS
     485           0 :         case LC3PLUS_FRAME_DURATION_1p25MS:
     486           0 :             assert(0);
     487             :             maxBytes = 210;
     488             :             minBytes = MIN_NBYTES;
     489             :             break;
     490             : #endif
     491           0 :         case LC3PLUS_FRAME_DURATION_2p5MS:
     492           0 :             maxBytes = 210;
     493           0 :             minBytes = MIN_NBYTES;
     494           0 :             break;
     495           0 :         case LC3PLUS_FRAME_DURATION_5MS:
     496           0 :             maxBytes = 375;
     497           0 :             minBytes = MIN_NBYTES;
     498           0 :             break;
     499           0 :         case LC3PLUS_FRAME_DURATION_7p5MS:
     500           0 :             maxBytes = 625;
     501           0 :             minBytes = MIN_NBYTES;
     502           0 :             break;
     503           0 :         case LC3PLUS_FRAME_DURATION_10MS:
     504           0 :             maxBytes = 625;
     505           0 :             minBytes = MIN_NBYTES;
     506           0 :             break;
     507           0 :         default:
     508           0 :             return LC3PLUS_HRMODE_ERROR;
     509             :         }
     510             :     }
     511             :     else
     512             :     {
     513       62823 :         minBytes = MIN_NBYTES;
     514       62823 :         maxBytes = MAX_NBYTES_100; /* for backward compatibility, MAX_NBYTES_100 is used for all frame lengths */
     515             :     }
     516             : 
     517       62823 :     channel_bytes = nBytes;
     518             : 
     519       62823 :         setup = decoder->channel_setup[ch];
     520             : 
     521       62823 :         if (channel_bytes < minBytes || channel_bytes > maxBytes)
     522             :         {
     523           0 :             return LC3PLUS_NUMBYTES_ERROR;
     524             :         }
     525             : 
     526       62823 :         setup->targetBytes          = channel_bytes;
     527       62823 :         setup->total_bits           = setup->targetBytes << 3;
     528       62823 :         setup->enable_lpc_weighting = (setup->total_bits < 480);
     529             : 
     530             : #ifdef FIX_BOTH_1p25_WB_GLOBGAINOFFSET_NONBE  
     531       62823 :         if (decoder->frame_dms == LC3PLUS_FRAME_DURATION_1p25MS)
     532             :         {
     533           0 :             setup->quantizedGainOff = calc_GGainOffset_1p25(setup->total_bits, decoder->fs_idx); /* enc/dec common function */
     534             :         }
     535             :         else
     536             :         {
     537       62823 :             setup->quantizedGainOff =
     538       62823 :                 -(MIN(115, setup->total_bits / (10 * (decoder->fs_idx + 1))) + 105 + 5 * (decoder->fs_idx + 1));
     539             :         }
     540             : #else 
     541             :         setup->quantizedGainOff =
     542             :             -(MIN(115, setup->total_bits / (10 * (decoder->fs_idx + 1))) + 105 + 5 * (decoder->fs_idx + 1));
     543             : #endif 
     544             : 
     545       62823 :         if (decoder->hrmode && decoder->fs_idx == 5)
     546             :         {
     547           0 :             setup->quantizedGainOff = MAX(setup->quantizedGainOff, -181);
     548             :         }
     549             : 
     550       62823 :         totalBits = setup->total_bits;
     551             : 
     552             : #ifdef CR9_C_ADD_1p25MS
     553       62823 :         if (decoder->frame_ms == LC3PLUS_FRAME_DURATION_1p25MS) {
     554           0 :             setup->enable_lpc_weighting = setup->total_bits < 60;
     555           0 :             totalBits                   = setup->total_bits * 8.0 * 0.42;
     556             :         }
     557             : #endif
     558       62823 :         if (decoder->frame_ms == LC3PLUS_FRAME_DURATION_2p5MS) {
     559           2 :             setup->enable_lpc_weighting = setup->total_bits < 120;
     560           2 :             totalBits                   = setup->total_bits * 4.0 * (1.0 - 0.4);
     561             :         }
     562       62823 :         if (decoder->frame_ms == LC3PLUS_FRAME_DURATION_5MS) {
     563       57132 :             setup->enable_lpc_weighting = (setup->total_bits < 240);
     564       57132 :             totalBits                   = setup->total_bits * 2 - 160;
     565             :         }
     566       62823 :         if (decoder->frame_ms == LC3PLUS_FRAME_DURATION_7p5MS) {
     567           0 :             setup->enable_lpc_weighting = (setup->total_bits < 360);
     568           0 :             totalBits                   = round(setup->total_bits * 10 / 7.5);
     569             :         }
     570             : 
     571       62823 :         if (decoder->frame_length > 40 * ((LC3_FLOAT) (decoder->frame_dms * 1.25 * 10) / 10.0)) {
     572       62819 :             setup->N_red_tns  = 40 * ((LC3_FLOAT) (decoder->frame_dms * 1.25 * 10) / 10.0);
     573       62819 :             setup->fs_red_tns = 40000;
     574             :         } else {
     575           4 :             setup->N_red_tns = decoder->frame_length;
     576           4 :             setup->fs_red_tns = decoder->fs;
     577             :         }
     578             : 
     579       62823 :         bitsTmp = totalBits;
     580             : 
     581       62823 :         if (bitsTmp < 400 + (decoder->fs_idx - 1) * 80) {
     582           0 :             setup->ltpf_conf_beta     = 0.4;
     583           0 :             setup->ltpf_conf_beta_idx = 0;
     584             : #ifdef CR9_C_ADD_1p25MS
     585           0 :             setup->ltpf_conf_beta_max = 0.6;
     586             : #endif
     587       62823 :         } else if (bitsTmp < 480 + (decoder->fs_idx - 1) * 80) {
     588           2 :             setup->ltpf_conf_beta     = 0.35;
     589           2 :             setup->ltpf_conf_beta_idx = 1;
     590             : #ifdef CR9_C_ADD_1p25MS
     591           2 :             setup->ltpf_conf_beta_max = 0.55;
     592             : #endif
     593       62821 :         } else if (bitsTmp < 560 + (decoder->fs_idx - 1) * 80) {
     594           0 :             setup->ltpf_conf_beta     = 0.3;
     595           0 :             setup->ltpf_conf_beta_idx = 2;
     596             : #ifdef CR9_C_ADD_1p25MS
     597           0 :             setup->ltpf_conf_beta_max = 0.5;
     598             : #endif
     599       62821 :         } else if (bitsTmp < 640 + (decoder->fs_idx - 1) * 80) {
     600           2 :             setup->ltpf_conf_beta     = 0.25;
     601           2 :             setup->ltpf_conf_beta_idx = 3;
     602             : #ifdef CR9_C_ADD_1p25MS
     603           2 :             setup->ltpf_conf_beta_max = 0.45;
     604             : #endif
     605             :         } else {
     606       62819 :             setup->ltpf_conf_beta     = 0;
     607       62819 :             setup->ltpf_conf_beta_idx = -1;
     608             : #ifdef CR9_C_ADD_1p25MS
     609       62819 :             setup->ltpf_conf_beta_max = 0;
     610             : #endif
     611             :         }
     612             : 
     613             : #ifdef CR9_C_ADD_1p25MS
     614       62823 :         setup->ltpf_gain_step = ( setup->ltpf_conf_beta_max - setup->ltpf_conf_beta ) / LTPF_ADAPTIVE_GAIN_RATE;
     615             : #endif
     616             : 
     617             :         /* No LTPF in hrmode */
     618       62823 :         if (decoder->hrmode == 1) {
     619           0 :             setup->ltpf_conf_beta     = 0;
     620           0 :             setup->ltpf_conf_beta_idx = -1;
     621             :         }
     622             : 
     623       62823 :     return LC3PLUS_OK;
     624             : }

Generated by: LCOV version 1.14