tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

opus_decoder.c (49604B)


      1 /* Copyright (c) 2010 Xiph.Org Foundation, Skype Limited
      2   Copyright (c) 2024 Arm Limited
      3   Written by Jean-Marc Valin and Koen Vos */
      4 /*
      5   Redistribution and use in source and binary forms, with or without
      6   modification, are permitted provided that the following conditions
      7   are met:
      8 
      9   - Redistributions of source code must retain the above copyright
     10   notice, this list of conditions and the following disclaimer.
     11 
     12   - Redistributions in binary form must reproduce the above copyright
     13   notice, this list of conditions and the following disclaimer in the
     14   documentation and/or other materials provided with the distribution.
     15 
     16   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     17   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     18   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     19   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
     20   OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     21   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     22   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     23   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     24   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     25   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     26   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27 */
     28 
     29 #ifdef HAVE_CONFIG_H
     30 # include "config.h"
     31 #endif
     32 
     33 #ifndef OPUS_BUILD
     34 # error "OPUS_BUILD _MUST_ be defined to build Opus. This probably means you need other defines as well, as in a config.h. See the included build files for details."
     35 #endif
     36 
     37 #if defined(__GNUC__) && (__GNUC__ >= 2) && !defined(__OPTIMIZE__) && !defined(OPUS_WILL_BE_SLOW)
     38 # pragma message "You appear to be compiling without optimization, if so opus will be very slow."
     39 #endif
     40 
     41 #include <stdarg.h>
     42 #include "celt.h"
     43 #include "opus.h"
     44 #include "entdec.h"
     45 #include "modes.h"
     46 #include "API.h"
     47 #include "stack_alloc.h"
     48 #include "float_cast.h"
     49 #include "opus_private.h"
     50 #include "os_support.h"
     51 #include "structs.h"
     52 #include "define.h"
     53 #include "mathops.h"
     54 #include "cpu_support.h"
     55 
     56 #ifdef ENABLE_DEEP_PLC
     57 #include "dred_rdovae_dec_data.h"
     58 #include "dred_rdovae_dec.h"
     59 #endif
     60 
     61 #ifdef ENABLE_OSCE
     62 #include "osce.h"
     63 #endif
     64 
     65 struct OpusDecoder {
     66   int          celt_dec_offset;
     67   int          silk_dec_offset;
     68   int          channels;
     69   opus_int32   Fs;          /** Sampling rate (at the API level) */
     70   silk_DecControlStruct DecControl;
     71   int          decode_gain;
     72   int          complexity;
     73   int          ignore_extensions;
     74   int          arch;
     75 #ifdef ENABLE_DEEP_PLC
     76    LPCNetPLCState lpcnet;
     77 #endif
     78 
     79   /* Everything beyond this point gets cleared on a reset */
     80 #define OPUS_DECODER_RESET_START stream_channels
     81   int          stream_channels;
     82 
     83   int          bandwidth;
     84   int          mode;
     85   int          prev_mode;
     86   int          frame_size;
     87   int          prev_redundancy;
     88   int          last_packet_duration;
     89 #ifndef FIXED_POINT
     90   opus_val16   softclip_mem[2];
     91 #endif
     92 
     93   opus_uint32  rangeFinal;
     94 };
     95 
     96 #if defined(ENABLE_HARDENING) || defined(ENABLE_ASSERTIONS)
     97 static void validate_opus_decoder(OpusDecoder *st)
     98 {
     99   celt_assert(st->channels == 1 || st->channels == 2);
    100 #ifdef ENABLE_QEXT
    101   celt_assert(st->Fs == 96000 || st->Fs == 48000 || st->Fs == 24000 || st->Fs == 16000 || st->Fs == 12000 || st->Fs == 8000);
    102 #else
    103   celt_assert(st->Fs == 48000 || st->Fs == 24000 || st->Fs == 16000 || st->Fs == 12000 || st->Fs == 8000);
    104 #endif
    105   celt_assert(st->DecControl.API_sampleRate == st->Fs);
    106   celt_assert(st->DecControl.internalSampleRate == 0 || st->DecControl.internalSampleRate == 16000 || st->DecControl.internalSampleRate == 12000 || st->DecControl.internalSampleRate == 8000);
    107   celt_assert(st->DecControl.nChannelsAPI == st->channels);
    108   celt_assert(st->DecControl.nChannelsInternal == 0 || st->DecControl.nChannelsInternal == 1 || st->DecControl.nChannelsInternal == 2);
    109   celt_assert(st->DecControl.payloadSize_ms == 0 || st->DecControl.payloadSize_ms == 10 || st->DecControl.payloadSize_ms == 20 || st->DecControl.payloadSize_ms == 40 || st->DecControl.payloadSize_ms == 60);
    110 #ifdef OPUS_ARCHMASK
    111   celt_assert(st->arch >= 0);
    112   celt_assert(st->arch <= OPUS_ARCHMASK);
    113 #endif
    114   celt_assert(st->stream_channels == 1 || st->stream_channels == 2);
    115 }
    116 #define VALIDATE_OPUS_DECODER(st) validate_opus_decoder(st)
    117 #else
    118 #define VALIDATE_OPUS_DECODER(st)
    119 #endif
    120 
    121 int opus_decoder_get_size(int channels)
    122 {
    123   int silkDecSizeBytes, celtDecSizeBytes;
    124   int ret;
    125   if (channels<1 || channels > 2)
    126      return 0;
    127   ret = silk_Get_Decoder_Size( &silkDecSizeBytes );
    128   if(ret)
    129      return 0;
    130   silkDecSizeBytes = align(silkDecSizeBytes);
    131   celtDecSizeBytes = celt_decoder_get_size(channels);
    132   return align(sizeof(OpusDecoder))+silkDecSizeBytes+celtDecSizeBytes;
    133 }
    134 
    135 int opus_decoder_init(OpusDecoder *st, opus_int32 Fs, int channels)
    136 {
    137   void *silk_dec;
    138   CELTDecoder *celt_dec;
    139   int ret, silkDecSizeBytes;
    140 
    141   if ((Fs!=48000&&Fs!=24000&&Fs!=16000&&Fs!=12000&&Fs!=8000
    142 #ifdef ENABLE_QEXT
    143         &&Fs!=96000
    144 #endif
    145         )
    146    || (channels!=1&&channels!=2))
    147      return OPUS_BAD_ARG;
    148 
    149   OPUS_CLEAR((char*)st, opus_decoder_get_size(channels));
    150   /* Initialize SILK decoder */
    151   ret = silk_Get_Decoder_Size(&silkDecSizeBytes);
    152   if (ret)
    153      return OPUS_INTERNAL_ERROR;
    154 
    155   silkDecSizeBytes = align(silkDecSizeBytes);
    156   st->silk_dec_offset = align(sizeof(OpusDecoder));
    157   st->celt_dec_offset = st->silk_dec_offset+silkDecSizeBytes;
    158   silk_dec = (char*)st+st->silk_dec_offset;
    159   celt_dec = (CELTDecoder*)((char*)st+st->celt_dec_offset);
    160   st->stream_channels = st->channels = channels;
    161   st->complexity = 0;
    162 
    163   st->Fs = Fs;
    164   st->DecControl.API_sampleRate = st->Fs;
    165   st->DecControl.nChannelsAPI      = st->channels;
    166 
    167   /* Reset decoder */
    168   ret = silk_InitDecoder( silk_dec );
    169   if(ret)return OPUS_INTERNAL_ERROR;
    170 
    171   /* Initialize CELT decoder */
    172   ret = celt_decoder_init(celt_dec, Fs, channels);
    173   if(ret!=OPUS_OK)return OPUS_INTERNAL_ERROR;
    174 
    175   celt_decoder_ctl(celt_dec, CELT_SET_SIGNALLING(0));
    176 
    177   st->prev_mode = 0;
    178   st->frame_size = Fs/400;
    179 #ifdef ENABLE_DEEP_PLC
    180    lpcnet_plc_init( &st->lpcnet);
    181 #endif
    182   st->arch = opus_select_arch();
    183   return OPUS_OK;
    184 }
    185 
    186 OpusDecoder *opus_decoder_create(opus_int32 Fs, int channels, int *error)
    187 {
    188   int ret;
    189   OpusDecoder *st;
    190   if ((Fs!=48000&&Fs!=24000&&Fs!=16000&&Fs!=12000&&Fs!=8000
    191 #ifdef ENABLE_QEXT
    192         &&Fs!=96000
    193 #endif
    194         )
    195    || (channels!=1&&channels!=2))
    196   {
    197      if (error)
    198         *error = OPUS_BAD_ARG;
    199      return NULL;
    200   }
    201   st = (OpusDecoder *)opus_alloc(opus_decoder_get_size(channels));
    202   if (st == NULL)
    203   {
    204      if (error)
    205         *error = OPUS_ALLOC_FAIL;
    206      return NULL;
    207   }
    208   ret = opus_decoder_init(st, Fs, channels);
    209   if (error)
    210      *error = ret;
    211   if (ret != OPUS_OK)
    212   {
    213      opus_free(st);
    214      st = NULL;
    215   }
    216   return st;
    217 }
    218 
    219 #ifdef ENABLE_RES24
    220 static void smooth_fade(const opus_res *in1, const opus_res *in2,
    221      opus_res *out, int overlap, int channels,
    222      const celt_coef *window, opus_int32 Fs)
    223 {
    224   int i, c;
    225   int inc = 48000/Fs;
    226   for (c=0;c<channels;c++)
    227   {
    228      for (i=0;i<overlap;i++)
    229      {
    230         celt_coef w = MULT_COEF(window[i*inc], window[i*inc]);
    231         out[i*channels+c] = ADD32(MULT_COEF_32(w,in2[i*channels+c]),
    232                                   MULT_COEF_32(COEF_ONE-w, in1[i*channels+c]));
    233      }
    234   }
    235 }
    236 #else
    237 static void smooth_fade(const opus_res *in1, const opus_res *in2,
    238      opus_res *out, int overlap, int channels,
    239      const opus_val16 *window, opus_int32 Fs)
    240 {
    241   int i, c;
    242   int inc = 48000/Fs;
    243   for (c=0;c<channels;c++)
    244   {
    245      for (i=0;i<overlap;i++)
    246      {
    247         opus_val16 w = MULT16_16_Q15(window[i*inc], window[i*inc]);
    248         out[i*channels+c] = SHR32(MAC16_16(MULT16_16(w,in2[i*channels+c]),
    249                                   Q15ONE-w, in1[i*channels+c]), 15);
    250      }
    251   }
    252 }
    253 #endif
    254 
    255 static int opus_packet_get_mode(const unsigned char *data)
    256 {
    257   int mode;
    258   if (data[0]&0x80)
    259   {
    260      mode = MODE_CELT_ONLY;
    261   } else if ((data[0]&0x60) == 0x60)
    262   {
    263      mode = MODE_HYBRID;
    264   } else {
    265      mode = MODE_SILK_ONLY;
    266   }
    267   return mode;
    268 }
    269 
    270 static int opus_decode_frame(OpusDecoder *st, const unsigned char *data,
    271      opus_int32 len, opus_res *pcm, int frame_size, int decode_fec ARG_QEXT(opus_extension_data *ext))
    272 {
    273   void *silk_dec;
    274   CELTDecoder *celt_dec;
    275   int i, silk_ret=0, celt_ret=0;
    276   ec_dec dec;
    277   opus_int32 silk_frame_size;
    278   int pcm_transition_silk_size;
    279   VARDECL(opus_res, pcm_transition_silk);
    280   int pcm_transition_celt_size;
    281   VARDECL(opus_res, pcm_transition_celt);
    282   opus_res *pcm_transition=NULL;
    283   int redundant_audio_size;
    284   VARDECL(opus_res, redundant_audio);
    285 
    286   int audiosize;
    287   int mode;
    288   int bandwidth;
    289   int transition=0;
    290   int start_band;
    291   int redundancy=0;
    292   int redundancy_bytes = 0;
    293   int celt_to_silk=0;
    294   int c;
    295   int F2_5, F5, F10, F20;
    296   const celt_coef *window;
    297   opus_uint32 redundant_rng = 0;
    298   int celt_accum;
    299   ALLOC_STACK;
    300 
    301   silk_dec = (char*)st+st->silk_dec_offset;
    302   celt_dec = (CELTDecoder*)((char*)st+st->celt_dec_offset);
    303   F20 = st->Fs/50;
    304   F10 = F20>>1;
    305   F5 = F10>>1;
    306   F2_5 = F5>>1;
    307   if (frame_size < F2_5)
    308   {
    309      RESTORE_STACK;
    310      return OPUS_BUFFER_TOO_SMALL;
    311   }
    312   /* Limit frame_size to avoid excessive stack allocations. */
    313   frame_size = IMIN(frame_size, st->Fs/25*3);
    314   /* Payloads of 1 (2 including ToC) or 0 trigger the PLC/DTX */
    315   if (len<=1)
    316   {
    317      data = NULL;
    318      /* In that case, don't conceal more than what the ToC says */
    319      frame_size = IMIN(frame_size, st->frame_size);
    320   }
    321   if (data != NULL)
    322   {
    323      audiosize = st->frame_size;
    324      mode = st->mode;
    325      bandwidth = st->bandwidth;
    326      ec_dec_init(&dec,(unsigned char*)data,len);
    327   } else {
    328      audiosize = frame_size;
    329      /* Run PLC using last used mode (CELT if we ended with CELT redundancy) */
    330      mode = st->prev_redundancy ? MODE_CELT_ONLY : st->prev_mode;
    331      bandwidth = 0;
    332 
    333      if (mode == 0)
    334      {
    335         /* If we haven't got any packet yet, all we can do is return zeros */
    336         for (i=0;i<audiosize*st->channels;i++)
    337            pcm[i] = 0;
    338         RESTORE_STACK;
    339         return audiosize;
    340      }
    341 
    342      /* Avoids trying to run the PLC on sizes other than 2.5 (CELT), 5 (CELT),
    343         10, or 20 (e.g. 12.5 or 30 ms). */
    344      if (audiosize > F20)
    345      {
    346         do {
    347            int ret = opus_decode_frame(st, NULL, 0, pcm, IMIN(audiosize, F20), 0 ARG_QEXT(NULL));
    348            if (ret<0)
    349            {
    350               RESTORE_STACK;
    351               return ret;
    352            }
    353            pcm += ret*st->channels;
    354            audiosize -= ret;
    355         } while (audiosize > 0);
    356         RESTORE_STACK;
    357         return frame_size;
    358      } else if (audiosize < F20)
    359      {
    360         if (audiosize > F10)
    361            audiosize = F10;
    362         else if (mode != MODE_SILK_ONLY && audiosize > F5 && audiosize < F10)
    363            audiosize = F5;
    364      }
    365   }
    366 
    367   /* In fixed-point, we can tell CELT to do the accumulation on top of the
    368      SILK PCM buffer. This saves some stack space. */
    369   celt_accum = (mode != MODE_CELT_ONLY);
    370 
    371   pcm_transition_silk_size = ALLOC_NONE;
    372   pcm_transition_celt_size = ALLOC_NONE;
    373   if (data!=NULL && st->prev_mode > 0 && (
    374       (mode == MODE_CELT_ONLY && st->prev_mode != MODE_CELT_ONLY && !st->prev_redundancy)
    375    || (mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY) )
    376      )
    377   {
    378      transition = 1;
    379      /* Decide where to allocate the stack memory for pcm_transition */
    380      if (mode == MODE_CELT_ONLY)
    381         pcm_transition_celt_size = F5*st->channels;
    382      else
    383         pcm_transition_silk_size = F5*st->channels;
    384   }
    385   ALLOC(pcm_transition_celt, pcm_transition_celt_size, opus_res);
    386   if (transition && mode == MODE_CELT_ONLY)
    387   {
    388      pcm_transition = pcm_transition_celt;
    389      opus_decode_frame(st, NULL, 0, pcm_transition, IMIN(F5, audiosize), 0 ARG_QEXT(NULL));
    390   }
    391   if (audiosize > frame_size)
    392   {
    393      /*fprintf(stderr, "PCM buffer too small: %d vs %d (mode = %d)\n", audiosize, frame_size, mode);*/
    394      RESTORE_STACK;
    395      return OPUS_BAD_ARG;
    396   } else {
    397      frame_size = audiosize;
    398   }
    399 
    400   /* SILK processing */
    401   if (mode != MODE_CELT_ONLY)
    402   {
    403      int lost_flag, decoded_samples;
    404      opus_res *pcm_ptr;
    405      int pcm_too_small;
    406      int pcm_silk_size = ALLOC_NONE;
    407      VARDECL(opus_res, pcm_silk);
    408      pcm_too_small = (frame_size < F10);
    409      if (pcm_too_small)
    410         pcm_silk_size = F10*st->channels;
    411      ALLOC(pcm_silk, pcm_silk_size, opus_res);
    412      if (pcm_too_small)
    413         pcm_ptr = pcm_silk;
    414      else
    415         pcm_ptr = pcm;
    416 
    417      if (st->prev_mode==MODE_CELT_ONLY)
    418         silk_ResetDecoder( silk_dec );
    419 
    420      /* The SILK PLC cannot produce frames of less than 10 ms */
    421      st->DecControl.payloadSize_ms = IMAX(10, 1000 * audiosize / st->Fs);
    422 
    423      if (data != NULL)
    424      {
    425        st->DecControl.nChannelsInternal = st->stream_channels;
    426        if( mode == MODE_SILK_ONLY ) {
    427           if( bandwidth == OPUS_BANDWIDTH_NARROWBAND ) {
    428              st->DecControl.internalSampleRate = 8000;
    429           } else if( bandwidth == OPUS_BANDWIDTH_MEDIUMBAND ) {
    430              st->DecControl.internalSampleRate = 12000;
    431           } else if( bandwidth == OPUS_BANDWIDTH_WIDEBAND ) {
    432              st->DecControl.internalSampleRate = 16000;
    433           } else {
    434              st->DecControl.internalSampleRate = 16000;
    435              celt_assert( 0 );
    436           }
    437        } else {
    438           /* Hybrid mode */
    439           st->DecControl.internalSampleRate = 16000;
    440        }
    441     }
    442     st->DecControl.enable_deep_plc = st->complexity >= 5;
    443 #ifdef ENABLE_OSCE
    444     st->DecControl.osce_method = OSCE_METHOD_NONE;
    445 #ifndef DISABLE_LACE
    446     if (st->complexity >= 6) {st->DecControl.osce_method = OSCE_METHOD_LACE;}
    447 #endif
    448 #ifndef DISABLE_NOLACE
    449     if (st->complexity >= 7) {st->DecControl.osce_method = OSCE_METHOD_NOLACE;}
    450 #endif
    451 #ifdef ENABLE_OSCE_BWE
    452     if (st->complexity >= 4 && st->DecControl.enable_osce_bwe &&
    453         st->Fs == 48000 && st->DecControl.internalSampleRate == 16000 &&
    454         ((mode == MODE_SILK_ONLY) || (data == NULL))) {
    455         /* request WB -> FB signal extension */
    456         st->DecControl.osce_extended_mode = OSCE_MODE_SILK_BBWE;
    457     } else {
    458         /* at this point, mode can only be MODE_SILK_ONLY or MODE_HYBRID */
    459         st->DecControl.osce_extended_mode = mode == MODE_SILK_ONLY ? OSCE_MODE_SILK_ONLY : OSCE_MODE_HYBRID;
    460     }
    461     if (st->prev_mode == MODE_CELT_ONLY) {
    462         /* Update extended mode for CELT->SILK transition */
    463         st->DecControl.prev_osce_extended_mode = OSCE_MODE_CELT_ONLY;
    464     }
    465 #endif
    466 #endif
    467 
    468     lost_flag = data == NULL ? 1 : 2 * !!decode_fec;
    469     decoded_samples = 0;
    470     do {
    471        /* Call SILK decoder */
    472        int first_frame = decoded_samples == 0;
    473        silk_ret = silk_Decode( silk_dec, &st->DecControl,
    474                                lost_flag, first_frame, &dec, pcm_ptr, &silk_frame_size,
    475 #ifdef ENABLE_DEEP_PLC
    476                                &st->lpcnet,
    477 #endif
    478                                st->arch );
    479        if( silk_ret ) {
    480           if (lost_flag) {
    481              /* PLC failure should not be fatal */
    482              silk_frame_size = frame_size;
    483              for (i=0;i<frame_size*st->channels;i++)
    484                 pcm_ptr[i] = 0;
    485           } else {
    486             RESTORE_STACK;
    487             return OPUS_INTERNAL_ERROR;
    488           }
    489        }
    490        pcm_ptr += silk_frame_size * st->channels;
    491        decoded_samples += silk_frame_size;
    492      } while( decoded_samples < frame_size );
    493     if (pcm_too_small) {
    494        OPUS_COPY(pcm, pcm_silk, frame_size*st->channels);
    495     }
    496   }
    497 
    498   start_band = 0;
    499   if (!decode_fec && mode != MODE_CELT_ONLY && data != NULL
    500    && ec_tell(&dec)+17+20*(mode == MODE_HYBRID) <= 8*len)
    501   {
    502      /* Check if we have a redundant 0-8 kHz band */
    503      if (mode == MODE_HYBRID)
    504         redundancy = ec_dec_bit_logp(&dec, 12);
    505      else
    506         redundancy = 1;
    507      if (redundancy)
    508      {
    509         celt_to_silk = ec_dec_bit_logp(&dec, 1);
    510         /* redundancy_bytes will be at least two, in the non-hybrid
    511            case due to the ec_tell() check above */
    512         redundancy_bytes = mode==MODE_HYBRID ?
    513               (opus_int32)ec_dec_uint(&dec, 256)+2 :
    514               len-((ec_tell(&dec)+7)>>3);
    515         len -= redundancy_bytes;
    516         /* This is a sanity check. It should never happen for a valid
    517            packet, so the exact behaviour is not normative. */
    518         if (len*8 < ec_tell(&dec))
    519         {
    520            len = 0;
    521            redundancy_bytes = 0;
    522            redundancy = 0;
    523         }
    524         /* Shrink decoder because of raw bits */
    525         dec.storage -= redundancy_bytes;
    526      }
    527   }
    528   if (mode != MODE_CELT_ONLY)
    529      start_band = 17;
    530 
    531   if (redundancy)
    532   {
    533      transition = 0;
    534      pcm_transition_silk_size=ALLOC_NONE;
    535   }
    536 
    537   ALLOC(pcm_transition_silk, pcm_transition_silk_size, opus_res);
    538 
    539   if (transition && mode != MODE_CELT_ONLY)
    540   {
    541      pcm_transition = pcm_transition_silk;
    542      opus_decode_frame(st, NULL, 0, pcm_transition, IMIN(F5, audiosize), 0 ARG_QEXT(NULL));
    543   }
    544 
    545 
    546   if (bandwidth)
    547   {
    548      int endband=21;
    549 
    550      switch(bandwidth)
    551      {
    552      case OPUS_BANDWIDTH_NARROWBAND:
    553         endband = 13;
    554         break;
    555      case OPUS_BANDWIDTH_MEDIUMBAND:
    556      case OPUS_BANDWIDTH_WIDEBAND:
    557         endband = 17;
    558         break;
    559      case OPUS_BANDWIDTH_SUPERWIDEBAND:
    560         endband = 19;
    561         break;
    562      case OPUS_BANDWIDTH_FULLBAND:
    563         endband = 21;
    564         break;
    565      default:
    566         celt_assert(0);
    567         break;
    568      }
    569      MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_SET_END_BAND(endband)));
    570   }
    571   MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_SET_CHANNELS(st->stream_channels)));
    572 
    573   /* Only allocation memory for redundancy if/when needed */
    574   redundant_audio_size = redundancy ? F5*st->channels : ALLOC_NONE;
    575   ALLOC(redundant_audio, redundant_audio_size, opus_res);
    576 
    577   /* 5 ms redundant frame for CELT->SILK*/
    578   if (redundancy && celt_to_silk)
    579   {
    580      /* If the previous frame did not use CELT (the first redundancy frame in
    581         a transition from SILK may have been lost) then the CELT decoder is
    582         stale at this point and the redundancy audio is not useful, however
    583         the final range is still needed (for testing), so the redundancy is
    584         always decoded but the decoded audio may not be used */
    585      MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(0)));
    586      celt_decode_with_ec(celt_dec, data+len, redundancy_bytes,
    587                          redundant_audio, F5, NULL, 0);
    588      MUST_SUCCEED(celt_decoder_ctl(celt_dec, OPUS_GET_FINAL_RANGE(&redundant_rng)));
    589   }
    590 
    591   /* MUST be after PLC */
    592   MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(start_band)));
    593 
    594 #ifdef ENABLE_OSCE_BWE
    595   if (mode != MODE_SILK_ONLY && st->DecControl.osce_extended_mode != OSCE_MODE_SILK_BBWE)
    596 #else
    597   if (mode != MODE_SILK_ONLY)
    598 #endif
    599   {
    600      int celt_frame_size = IMIN(F20, frame_size);
    601      /* Make sure to discard any previous CELT state */
    602      if (mode != st->prev_mode && st->prev_mode > 0 && !st->prev_redundancy)
    603         MUST_SUCCEED(celt_decoder_ctl(celt_dec, OPUS_RESET_STATE));
    604      /* Decode CELT */
    605      celt_ret = celt_decode_with_ec_dred(celt_dec, decode_fec ? NULL : data,
    606                                     len, pcm, celt_frame_size, &dec, celt_accum
    607 #ifdef ENABLE_DEEP_PLC
    608                                     , &st->lpcnet
    609 #endif
    610                                     ARG_QEXT(ext ? ext->data : NULL) ARG_QEXT(ext ? ext->len : 0));
    611      celt_decoder_ctl(celt_dec, OPUS_GET_FINAL_RANGE(&st->rangeFinal));
    612   } else {
    613      unsigned char silence[2] = {0xFF, 0xFF};
    614      if (!celt_accum)
    615      {
    616         for (i=0;i<frame_size*st->channels;i++)
    617            pcm[i] = 0;
    618      }
    619      /* For hybrid -> SILK transitions, we let the CELT MDCT
    620         do a fade-out by decoding a silence frame */
    621      if (st->prev_mode == MODE_HYBRID && !(redundancy && celt_to_silk && st->prev_redundancy) )
    622      {
    623         MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(0)));
    624         celt_decode_with_ec(celt_dec, silence, 2, pcm, F2_5, NULL, celt_accum);
    625      }
    626      st->rangeFinal = dec.rng;
    627   }
    628 
    629   {
    630      const CELTMode *celt_mode;
    631      MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_GET_MODE(&celt_mode)));
    632      window = celt_mode->window;
    633   }
    634 
    635   /* 5 ms redundant frame for SILK->CELT */
    636   if (redundancy && !celt_to_silk)
    637   {
    638      MUST_SUCCEED(celt_decoder_ctl(celt_dec, OPUS_RESET_STATE));
    639      MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(0)));
    640 
    641      celt_decode_with_ec(celt_dec, data+len, redundancy_bytes, redundant_audio, F5, NULL, 0);
    642      MUST_SUCCEED(celt_decoder_ctl(celt_dec, OPUS_GET_FINAL_RANGE(&redundant_rng)));
    643      smooth_fade(pcm+st->channels*(frame_size-F2_5), redundant_audio+st->channels*F2_5,
    644                  pcm+st->channels*(frame_size-F2_5), F2_5, st->channels, window, st->Fs);
    645   }
    646   /* 5ms redundant frame for CELT->SILK; ignore if the previous frame did not
    647      use CELT (the first redundancy frame in a transition from SILK may have
    648      been lost) */
    649   if (redundancy && celt_to_silk && (st->prev_mode != MODE_SILK_ONLY || st->prev_redundancy))
    650   {
    651      for (c=0;c<st->channels;c++)
    652      {
    653         for (i=0;i<F2_5;i++)
    654            pcm[st->channels*i+c] = redundant_audio[st->channels*i+c];
    655      }
    656      smooth_fade(redundant_audio+st->channels*F2_5, pcm+st->channels*F2_5,
    657                  pcm+st->channels*F2_5, F2_5, st->channels, window, st->Fs);
    658   }
    659   if (transition)
    660   {
    661      if (audiosize >= F5)
    662      {
    663         for (i=0;i<st->channels*F2_5;i++)
    664            pcm[i] = pcm_transition[i];
    665         smooth_fade(pcm_transition+st->channels*F2_5, pcm+st->channels*F2_5,
    666                     pcm+st->channels*F2_5, F2_5,
    667                     st->channels, window, st->Fs);
    668      } else {
    669         /* Not enough time to do a clean transition, but we do it anyway
    670            This will not preserve amplitude perfectly and may introduce
    671            a bit of temporal aliasing, but it shouldn't be too bad and
    672            that's pretty much the best we can do. In any case, generating this
    673            transition it pretty silly in the first place */
    674         smooth_fade(pcm_transition, pcm,
    675                     pcm, F2_5,
    676                     st->channels, window, st->Fs);
    677      }
    678   }
    679 
    680   if(st->decode_gain)
    681   {
    682      opus_val32 gain;
    683      gain = celt_exp2(MULT16_16_P15(QCONST16(6.48814081e-4f, 25), st->decode_gain));
    684      for (i=0;i<frame_size*st->channels;i++)
    685      {
    686         opus_val32 x;
    687 #ifdef ENABLE_RES24
    688         x = MULT32_32_Q16(pcm[i],gain);
    689 #else
    690         x = MULT16_32_P16(pcm[i],gain);
    691 #endif
    692         pcm[i] = SATURATE(x, 32767);
    693      }
    694   }
    695 
    696   if (len <= 1)
    697      st->rangeFinal = 0;
    698   else
    699      st->rangeFinal ^= redundant_rng;
    700 
    701   st->prev_mode = mode;
    702   st->prev_redundancy = redundancy && !celt_to_silk;
    703 
    704   if (celt_ret>=0)
    705   {
    706      if (OPUS_CHECK_ARRAY(pcm, audiosize*st->channels))
    707         OPUS_PRINT_INT(audiosize);
    708   }
    709 
    710   RESTORE_STACK;
    711   return celt_ret < 0 ? celt_ret : audiosize;
    712 
    713 }
    714 
    715 int opus_decode_native(OpusDecoder *st, const unsigned char *data,
    716      opus_int32 len, opus_res *pcm, int frame_size, int decode_fec,
    717      int self_delimited, opus_int32 *packet_offset, int soft_clip, const OpusDRED *dred, opus_int32 dred_offset)
    718 {
    719   int i, nb_samples;
    720   int count, offset;
    721   unsigned char toc;
    722   int packet_frame_size, packet_bandwidth, packet_mode, packet_stream_channels;
    723   /* 48 x 2.5 ms = 120 ms */
    724   opus_int16 size[48];
    725   const unsigned char *padding;
    726   opus_int32 padding_len;
    727   OpusExtensionIterator iter;
    728   VALIDATE_OPUS_DECODER(st);
    729   if (decode_fec<0 || decode_fec>1)
    730      return OPUS_BAD_ARG;
    731   /* For FEC/PLC, frame_size has to be to have a multiple of 2.5 ms */
    732   if ((decode_fec || len==0 || data==NULL) && frame_size%(st->Fs/400)!=0)
    733      return OPUS_BAD_ARG;
    734 #ifdef ENABLE_DRED
    735   if (dred != NULL && dred->process_stage == 2) {
    736      int F10;
    737      int features_per_frame;
    738      int needed_feature_frames;
    739      int init_frames;
    740      lpcnet_plc_fec_clear(&st->lpcnet);
    741      F10 = st->Fs/100;
    742      /* if blend==0, the last PLC call was "update" and we need to feed two extra 10-ms frames. */
    743      init_frames = (st->lpcnet.blend == 0) ? 2 : 0;
    744      features_per_frame = IMAX(1, frame_size/F10);
    745      needed_feature_frames = init_frames + features_per_frame;
    746      for (i=0;i<needed_feature_frames;i++) {
    747         int feature_offset;
    748         /* We floor instead of rounding because 5-ms overlap compensates for the missing 0.5 rounding offset. */
    749         feature_offset = init_frames - i - 2 + (int)floor(((float)dred_offset + dred->dred_offset*F10/4)/F10);
    750         if (feature_offset <= 4*dred->nb_latents-1 && feature_offset >= 0) {
    751           lpcnet_plc_fec_add(&st->lpcnet, dred->fec_features+feature_offset*DRED_NUM_FEATURES);
    752         } else {
    753           if (feature_offset >= 0) lpcnet_plc_fec_add(&st->lpcnet, NULL);
    754         }
    755 
    756      }
    757   }
    758 #else
    759   (void)dred;
    760   (void)dred_offset;
    761 #endif
    762   if (len==0 || data==NULL)
    763   {
    764      int pcm_count=0;
    765      do {
    766         int ret;
    767         ret = opus_decode_frame(st, NULL, 0, pcm+pcm_count*st->channels, frame_size-pcm_count, 0 ARG_QEXT(NULL));
    768         if (ret<0)
    769            return ret;
    770         pcm_count += ret;
    771      } while (pcm_count < frame_size);
    772      celt_assert(pcm_count == frame_size);
    773      if (OPUS_CHECK_ARRAY(pcm, pcm_count*st->channels))
    774         OPUS_PRINT_INT(pcm_count);
    775      st->last_packet_duration = pcm_count;
    776      return pcm_count;
    777   } else if (len<0)
    778      return OPUS_BAD_ARG;
    779 
    780   packet_mode = opus_packet_get_mode(data);
    781   packet_bandwidth = opus_packet_get_bandwidth(data);
    782   packet_frame_size = opus_packet_get_samples_per_frame(data, st->Fs);
    783   packet_stream_channels = opus_packet_get_nb_channels(data);
    784 
    785   count = opus_packet_parse_impl(data, len, self_delimited, &toc, NULL,
    786                                  size, &offset, packet_offset, &padding, &padding_len);
    787   if (st->ignore_extensions) {
    788      padding = NULL;
    789      padding_len = 0;
    790   }
    791   if (count<0)
    792      return count;
    793   opus_extension_iterator_init(&iter, padding, padding_len, count);
    794 
    795   data += offset;
    796 
    797   if (decode_fec)
    798   {
    799      int duration_copy;
    800      int ret;
    801      /* If no FEC can be present, run the PLC (recursive call) */
    802      if (frame_size < packet_frame_size || packet_mode == MODE_CELT_ONLY || st->mode == MODE_CELT_ONLY)
    803         return opus_decode_native(st, NULL, 0, pcm, frame_size, 0, 0, NULL, soft_clip, NULL, 0);
    804      /* Otherwise, run the PLC on everything except the size for which we might have FEC */
    805      duration_copy = st->last_packet_duration;
    806      if (frame_size-packet_frame_size!=0)
    807      {
    808         ret = opus_decode_native(st, NULL, 0, pcm, frame_size-packet_frame_size, 0, 0, NULL, soft_clip, NULL, 0);
    809         if (ret<0)
    810         {
    811            st->last_packet_duration = duration_copy;
    812            return ret;
    813         }
    814         celt_assert(ret==frame_size-packet_frame_size);
    815      }
    816      /* Complete with FEC */
    817      st->mode = packet_mode;
    818      st->bandwidth = packet_bandwidth;
    819      st->frame_size = packet_frame_size;
    820      st->stream_channels = packet_stream_channels;
    821      ret = opus_decode_frame(st, data, size[0], pcm+st->channels*(frame_size-packet_frame_size),
    822            packet_frame_size, 1 ARG_QEXT(NULL));
    823      if (ret<0)
    824         return ret;
    825      else {
    826         if (OPUS_CHECK_ARRAY(pcm, frame_size*st->channels))
    827            OPUS_PRINT_INT(frame_size);
    828         st->last_packet_duration = frame_size;
    829         return frame_size;
    830      }
    831   }
    832 
    833   if (count*packet_frame_size > frame_size)
    834      return OPUS_BUFFER_TOO_SMALL;
    835 
    836   /* Update the state as the last step to avoid updating it on an invalid packet */
    837   st->mode = packet_mode;
    838   st->bandwidth = packet_bandwidth;
    839   st->frame_size = packet_frame_size;
    840   st->stream_channels = packet_stream_channels;
    841 
    842   nb_samples=0;
    843   for (i=0;i<count;i++)
    844   {
    845      int ret;
    846 #ifdef ENABLE_QEXT
    847      opus_extension_data ext;
    848      ext.frame = -1;
    849      ext.data = NULL;
    850      ext.len = 0;
    851      ext.id = -1;
    852      while (ext.frame < i) {
    853         OpusExtensionIterator iter_copy;
    854         iter_copy = iter;
    855         ret = opus_extension_iterator_find(&iter, &ext, QEXT_EXTENSION_ID);
    856         if (ret <= 0) break;
    857         if (ext.frame > i) iter = iter_copy;
    858      }
    859      if (ext.frame != i) ext.data = NULL;
    860 #endif
    861      ret = opus_decode_frame(st, data, size[i], pcm+nb_samples*st->channels, frame_size-nb_samples, 0 ARG_QEXT(&ext));
    862      if (ret<0)
    863         return ret;
    864      celt_assert(ret==packet_frame_size);
    865      data += size[i];
    866      nb_samples += ret;
    867   }
    868   st->last_packet_duration = nb_samples;
    869   if (OPUS_CHECK_ARRAY(pcm, nb_samples*st->channels))
    870      OPUS_PRINT_INT(nb_samples);
    871 #ifndef FIXED_POINT
    872   if (soft_clip)
    873      opus_pcm_soft_clip_impl(pcm, nb_samples, st->channels, st->softclip_mem, st->arch);
    874   else
    875      st->softclip_mem[0]=st->softclip_mem[1]=0;
    876 #endif
    877   return nb_samples;
    878 }
    879 
    880 #ifdef FIXED_POINT
    881 #define OPTIONAL_CLIP 0
    882 #else
    883 #define OPTIONAL_CLIP 1
    884 #endif
    885 
    886 #if defined(FIXED_POINT) && !defined(ENABLE_RES24)
    887 int opus_decode(OpusDecoder *st, const unsigned char *data,
    888      opus_int32 len, opus_int16 *pcm, int frame_size, int decode_fec)
    889 {
    890   if(frame_size<=0)
    891      return OPUS_BAD_ARG;
    892   return opus_decode_native(st, data, len, pcm, frame_size, decode_fec, 0, NULL, 0, NULL, 0);
    893 }
    894 #else
    895 int opus_decode(OpusDecoder *st, const unsigned char *data,
    896      opus_int32 len, opus_int16 *pcm, int frame_size, int decode_fec)
    897 {
    898       VARDECL(opus_res, out);
    899       int ret;
    900       int nb_samples;
    901       ALLOC_STACK;
    902 
    903       if(frame_size<=0)
    904       {
    905          RESTORE_STACK;
    906          return OPUS_BAD_ARG;
    907       }
    908       if (data != NULL && len > 0 && !decode_fec)
    909       {
    910          nb_samples = opus_decoder_get_nb_samples(st, data, len);
    911          if (nb_samples>0)
    912             frame_size = IMIN(frame_size, nb_samples);
    913          else
    914             return OPUS_INVALID_PACKET;
    915       }
    916       celt_assert(st->channels == 1 || st->channels == 2);
    917       ALLOC(out, frame_size*st->channels, opus_res);
    918 
    919       ret = opus_decode_native(st, data, len, out, frame_size, decode_fec, 0, NULL, OPTIONAL_CLIP, NULL, 0);
    920       if (ret > 0)
    921       {
    922 # if defined(FIXED_POINT)
    923          int i;
    924          for (i=0;i<ret*st->channels;i++)
    925             pcm[i] = RES2INT16(out[i]);
    926 # else
    927          celt_float2int16(out, pcm, ret*st->channels, st->arch);
    928 # endif
    929       }
    930       RESTORE_STACK;
    931       return ret;
    932 }
    933 #endif
    934 
    935 #if defined(FIXED_POINT) && defined(ENABLE_RES24)
    936 int opus_decode24(OpusDecoder *st, const unsigned char *data,
    937      opus_int32 len, opus_int32 *pcm, int frame_size, int decode_fec)
    938 {
    939   if(frame_size<=0)
    940      return OPUS_BAD_ARG;
    941   return opus_decode_native(st, data, len, pcm, frame_size, decode_fec, 0, NULL, 0, NULL, 0);
    942 }
    943 #else
    944 int opus_decode24(OpusDecoder *st, const unsigned char *data,
    945      opus_int32 len, opus_int32 *pcm, int frame_size, int decode_fec)
    946 {
    947       VARDECL(opus_res, out);
    948       int ret, i;
    949       int nb_samples;
    950       ALLOC_STACK;
    951 
    952       if(frame_size<=0)
    953       {
    954          RESTORE_STACK;
    955          return OPUS_BAD_ARG;
    956       }
    957       if (data != NULL && len > 0 && !decode_fec)
    958       {
    959          nb_samples = opus_decoder_get_nb_samples(st, data, len);
    960          if (nb_samples>0)
    961             frame_size = IMIN(frame_size, nb_samples);
    962          else
    963             return OPUS_INVALID_PACKET;
    964       }
    965       celt_assert(st->channels == 1 || st->channels == 2);
    966       ALLOC(out, frame_size*st->channels, opus_res);
    967 
    968       ret = opus_decode_native(st, data, len, out, frame_size, decode_fec, 0, NULL, 0, NULL, 0);
    969       if (ret > 0)
    970       {
    971          nb_samples = ret*st->channels;
    972          for (i=0;i<nb_samples;i++)
    973             pcm[i] = RES2INT24(out[i]);
    974       }
    975       RESTORE_STACK;
    976       return ret;
    977 }
    978 #endif
    979 
    980 
    981 #ifndef DISABLE_FLOAT_API
    982 
    983 # if !defined(FIXED_POINT)
    984 int opus_decode_float(OpusDecoder *st, const unsigned char *data,
    985      opus_int32 len, opus_val16 *pcm, int frame_size, int decode_fec)
    986 {
    987   if(frame_size<=0)
    988      return OPUS_BAD_ARG;
    989   return opus_decode_native(st, data, len, pcm, frame_size, decode_fec, 0, NULL, 0, NULL, 0);
    990 }
    991 # else
    992 int opus_decode_float(OpusDecoder *st, const unsigned char *data,
    993      opus_int32 len, float *pcm, int frame_size, int decode_fec)
    994 {
    995   VARDECL(opus_res, out);
    996   int ret, i;
    997   int nb_samples;
    998   ALLOC_STACK;
    999 
   1000   if(frame_size<=0)
   1001   {
   1002      RESTORE_STACK;
   1003      return OPUS_BAD_ARG;
   1004   }
   1005   if (data != NULL && len > 0 && !decode_fec)
   1006   {
   1007      nb_samples = opus_decoder_get_nb_samples(st, data, len);
   1008      if (nb_samples>0)
   1009         frame_size = IMIN(frame_size, nb_samples);
   1010      else
   1011         return OPUS_INVALID_PACKET;
   1012   }
   1013   celt_assert(st->channels == 1 || st->channels == 2);
   1014   ALLOC(out, frame_size*st->channels, opus_res);
   1015 
   1016   ret = opus_decode_native(st, data, len, out, frame_size, decode_fec, 0, NULL, 0, NULL, 0);
   1017   if (ret > 0)
   1018   {
   1019      for (i=0;i<ret*st->channels;i++)
   1020         pcm[i] = RES2FLOAT(out[i]);
   1021   }
   1022   RESTORE_STACK;
   1023   return ret;
   1024 }
   1025 # endif
   1026 
   1027 #endif
   1028 
   1029 
   1030 int opus_decoder_ctl(OpusDecoder *st, int request, ...)
   1031 {
   1032   int ret = OPUS_OK;
   1033   va_list ap;
   1034   void *silk_dec;
   1035   CELTDecoder *celt_dec;
   1036 
   1037   silk_dec = (char*)st+st->silk_dec_offset;
   1038   celt_dec = (CELTDecoder*)((char*)st+st->celt_dec_offset);
   1039 
   1040 
   1041   va_start(ap, request);
   1042 
   1043   switch (request)
   1044   {
   1045   case OPUS_GET_BANDWIDTH_REQUEST:
   1046   {
   1047      opus_int32 *value = va_arg(ap, opus_int32*);
   1048      if (!value)
   1049      {
   1050         goto bad_arg;
   1051      }
   1052      *value = st->bandwidth;
   1053   }
   1054   break;
   1055   case OPUS_SET_COMPLEXITY_REQUEST:
   1056   {
   1057       opus_int32 value = va_arg(ap, opus_int32);
   1058       if(value<0 || value>10)
   1059       {
   1060          goto bad_arg;
   1061       }
   1062       st->complexity = value;
   1063       celt_decoder_ctl(celt_dec, OPUS_SET_COMPLEXITY(value));
   1064   }
   1065   break;
   1066   case OPUS_GET_COMPLEXITY_REQUEST:
   1067   {
   1068       opus_int32 *value = va_arg(ap, opus_int32*);
   1069       if (!value)
   1070       {
   1071          goto bad_arg;
   1072       }
   1073       *value = st->complexity;
   1074   }
   1075   break;
   1076 #ifdef ENABLE_OSCE_BWE
   1077   case OPUS_SET_OSCE_BWE_REQUEST:
   1078   {
   1079       opus_int32 value = va_arg(ap, opus_int32);
   1080       if(value<0 || value>1)
   1081       {          goto bad_arg;
   1082       }
   1083       st->DecControl.enable_osce_bwe = value;
   1084 
   1085      }
   1086   break;
   1087   case OPUS_GET_OSCE_BWE_REQUEST:
   1088   {
   1089       opus_int32 *value = va_arg(ap, opus_int32*);
   1090       if (!value)
   1091       {
   1092          goto bad_arg;
   1093       }
   1094       *value = st->DecControl.enable_osce_bwe;
   1095   }
   1096   break;
   1097 #endif
   1098   case OPUS_GET_FINAL_RANGE_REQUEST:
   1099   {
   1100      opus_uint32 *value = va_arg(ap, opus_uint32*);
   1101      if (!value)
   1102      {
   1103         goto bad_arg;
   1104      }
   1105      *value = st->rangeFinal;
   1106   }
   1107   break;
   1108   case OPUS_RESET_STATE:
   1109   {
   1110      OPUS_CLEAR((char*)&st->OPUS_DECODER_RESET_START,
   1111            sizeof(OpusDecoder)-
   1112            ((char*)&st->OPUS_DECODER_RESET_START - (char*)st));
   1113 
   1114      celt_decoder_ctl(celt_dec, OPUS_RESET_STATE);
   1115      silk_ResetDecoder( silk_dec );
   1116      st->stream_channels = st->channels;
   1117      st->frame_size = st->Fs/400;
   1118 #ifdef ENABLE_DEEP_PLC
   1119      lpcnet_plc_reset( &st->lpcnet );
   1120 #endif
   1121   }
   1122   break;
   1123   case OPUS_GET_SAMPLE_RATE_REQUEST:
   1124   {
   1125      opus_int32 *value = va_arg(ap, opus_int32*);
   1126      if (!value)
   1127      {
   1128         goto bad_arg;
   1129      }
   1130      *value = st->Fs;
   1131   }
   1132   break;
   1133   case OPUS_GET_PITCH_REQUEST:
   1134   {
   1135      opus_int32 *value = va_arg(ap, opus_int32*);
   1136      if (!value)
   1137      {
   1138         goto bad_arg;
   1139      }
   1140      if (st->prev_mode == MODE_CELT_ONLY)
   1141         ret = celt_decoder_ctl(celt_dec, OPUS_GET_PITCH(value));
   1142      else
   1143         *value = st->DecControl.prevPitchLag;
   1144   }
   1145   break;
   1146   case OPUS_GET_GAIN_REQUEST:
   1147   {
   1148      opus_int32 *value = va_arg(ap, opus_int32*);
   1149      if (!value)
   1150      {
   1151         goto bad_arg;
   1152      }
   1153      *value = st->decode_gain;
   1154   }
   1155   break;
   1156   case OPUS_SET_GAIN_REQUEST:
   1157   {
   1158       opus_int32 value = va_arg(ap, opus_int32);
   1159       if (value<-32768 || value>32767)
   1160       {
   1161          goto bad_arg;
   1162       }
   1163       st->decode_gain = value;
   1164   }
   1165   break;
   1166   case OPUS_GET_LAST_PACKET_DURATION_REQUEST:
   1167   {
   1168      opus_int32 *value = va_arg(ap, opus_int32*);
   1169      if (!value)
   1170      {
   1171         goto bad_arg;
   1172      }
   1173      *value = st->last_packet_duration;
   1174   }
   1175   break;
   1176   case OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST:
   1177   {
   1178       opus_int32 value = va_arg(ap, opus_int32);
   1179       if(value<0 || value>1)
   1180       {
   1181          goto bad_arg;
   1182       }
   1183       ret = celt_decoder_ctl(celt_dec, OPUS_SET_PHASE_INVERSION_DISABLED(value));
   1184   }
   1185   break;
   1186   case OPUS_GET_PHASE_INVERSION_DISABLED_REQUEST:
   1187   {
   1188       opus_int32 *value = va_arg(ap, opus_int32*);
   1189       if (!value)
   1190       {
   1191          goto bad_arg;
   1192       }
   1193       ret = celt_decoder_ctl(celt_dec, OPUS_GET_PHASE_INVERSION_DISABLED(value));
   1194   }
   1195   break;
   1196   case OPUS_SET_IGNORE_EXTENSIONS_REQUEST:
   1197   {
   1198       opus_int32 value = va_arg(ap, opus_int32);
   1199       if(value<0 || value>1)
   1200       {
   1201          goto bad_arg;
   1202       }
   1203       st->ignore_extensions = value;
   1204   }
   1205   break;
   1206   case OPUS_GET_IGNORE_EXTENSIONS_REQUEST:
   1207   {
   1208       opus_int32 *value = va_arg(ap, opus_int32*);
   1209       if (!value)
   1210       {
   1211          goto bad_arg;
   1212       }
   1213       *value = st->ignore_extensions;
   1214   }
   1215   break;
   1216 #ifdef USE_WEIGHTS_FILE
   1217   case OPUS_SET_DNN_BLOB_REQUEST:
   1218   {
   1219       const unsigned char *data = va_arg(ap, const unsigned char *);
   1220       opus_int32 len = va_arg(ap, opus_int32);
   1221       if(len<0 || data == NULL)
   1222       {
   1223          goto bad_arg;
   1224       }
   1225       ret = lpcnet_plc_load_model(&st->lpcnet, data, len);
   1226       ret = silk_LoadOSCEModels(silk_dec, data, len) || ret;
   1227   }
   1228   break;
   1229 #endif
   1230   default:
   1231      /*fprintf(stderr, "unknown opus_decoder_ctl() request: %d", request);*/
   1232      ret = OPUS_UNIMPLEMENTED;
   1233      break;
   1234   }
   1235 
   1236   va_end(ap);
   1237   return ret;
   1238 bad_arg:
   1239   va_end(ap);
   1240   return OPUS_BAD_ARG;
   1241 }
   1242 
   1243 void opus_decoder_destroy(OpusDecoder *st)
   1244 {
   1245   opus_free(st);
   1246 }
   1247 
   1248 
   1249 int opus_packet_get_bandwidth(const unsigned char *data)
   1250 {
   1251   int bandwidth;
   1252   if (data[0]&0x80)
   1253   {
   1254      bandwidth = OPUS_BANDWIDTH_MEDIUMBAND + ((data[0]>>5)&0x3);
   1255      if (bandwidth == OPUS_BANDWIDTH_MEDIUMBAND)
   1256         bandwidth = OPUS_BANDWIDTH_NARROWBAND;
   1257   } else if ((data[0]&0x60) == 0x60)
   1258   {
   1259      bandwidth = (data[0]&0x10) ? OPUS_BANDWIDTH_FULLBAND :
   1260                                   OPUS_BANDWIDTH_SUPERWIDEBAND;
   1261   } else {
   1262      bandwidth = OPUS_BANDWIDTH_NARROWBAND + ((data[0]>>5)&0x3);
   1263   }
   1264   return bandwidth;
   1265 }
   1266 
   1267 int opus_packet_get_nb_channels(const unsigned char *data)
   1268 {
   1269   return (data[0]&0x4) ? 2 : 1;
   1270 }
   1271 
   1272 int opus_packet_get_nb_frames(const unsigned char packet[], opus_int32 len)
   1273 {
   1274   int count;
   1275   if (len<1)
   1276      return OPUS_BAD_ARG;
   1277   count = packet[0]&0x3;
   1278   if (count==0)
   1279      return 1;
   1280   else if (count!=3)
   1281      return 2;
   1282   else if (len<2)
   1283      return OPUS_INVALID_PACKET;
   1284   else
   1285      return packet[1]&0x3F;
   1286 }
   1287 
   1288 int opus_packet_get_nb_samples(const unsigned char packet[], opus_int32 len,
   1289      opus_int32 Fs)
   1290 {
   1291   int samples;
   1292   int count = opus_packet_get_nb_frames(packet, len);
   1293 
   1294   if (count<0)
   1295      return count;
   1296 
   1297   samples = count*opus_packet_get_samples_per_frame(packet, Fs);
   1298   /* Can't have more than 120 ms */
   1299   if (samples*25 > Fs*3)
   1300      return OPUS_INVALID_PACKET;
   1301   else
   1302      return samples;
   1303 }
   1304 
   1305 int opus_packet_has_lbrr(const unsigned char packet[], opus_int32 len)
   1306 {
   1307   int ret;
   1308   const unsigned char *frames[48];
   1309   opus_int16 size[48];
   1310   int packet_mode, packet_frame_size, packet_stream_channels;
   1311   int nb_frames=1;
   1312   int lbrr;
   1313 
   1314   packet_mode = opus_packet_get_mode(packet);
   1315   if (packet_mode == MODE_CELT_ONLY)
   1316      return 0;
   1317   packet_frame_size = opus_packet_get_samples_per_frame(packet, 48000);
   1318   if (packet_frame_size > 960)
   1319      nb_frames = packet_frame_size/960;
   1320   packet_stream_channels = opus_packet_get_nb_channels(packet);
   1321   ret = opus_packet_parse(packet, len, NULL, frames, size, NULL);
   1322   if (ret <= 0)
   1323      return ret;
   1324   if (size[0] == 0)
   1325      return 0;
   1326   lbrr = (frames[0][0] >> (7-nb_frames)) & 0x1;
   1327   if (packet_stream_channels == 2)
   1328      lbrr = lbrr || ((frames[0][0] >> (6-2*nb_frames)) & 0x1);
   1329   return lbrr;
   1330 }
   1331 
   1332 int opus_decoder_get_nb_samples(const OpusDecoder *dec,
   1333      const unsigned char packet[], opus_int32 len)
   1334 {
   1335   return opus_packet_get_nb_samples(packet, len, dec->Fs);
   1336 }
   1337 
   1338 struct OpusDREDDecoder {
   1339 #ifdef ENABLE_DRED
   1340   RDOVAEDec model;
   1341 #endif
   1342   int loaded;
   1343   int arch;
   1344   opus_uint32 magic;
   1345 };
   1346 
   1347 #if defined(ENABLE_DRED) && (defined(ENABLE_HARDENING) || defined(ENABLE_ASSERTIONS))
   1348 static void validate_dred_decoder(OpusDREDDecoder *st)
   1349 {
   1350   celt_assert(st->magic == 0xD8EDDEC0);
   1351 #ifdef OPUS_ARCHMASK
   1352   celt_assert(st->arch >= 0);
   1353   celt_assert(st->arch <= OPUS_ARCHMASK);
   1354 #endif
   1355 }
   1356 #define VALIDATE_DRED_DECODER(st) validate_dred_decoder(st)
   1357 #else
   1358 #define VALIDATE_DRED_DECODER(st)
   1359 #endif
   1360 
   1361 
   1362 int opus_dred_decoder_get_size(void)
   1363 {
   1364  return sizeof(OpusDREDDecoder);
   1365 }
   1366 
   1367 #ifdef ENABLE_DRED
   1368 int dred_decoder_load_model(OpusDREDDecoder *dec, const unsigned char *data, int len)
   1369 {
   1370    WeightArray *list;
   1371    int ret;
   1372    parse_weights(&list, data, len);
   1373    ret = init_rdovaedec(&dec->model, list);
   1374    opus_free(list);
   1375    if (ret == 0) dec->loaded = 1;
   1376    return (ret == 0) ? OPUS_OK : OPUS_BAD_ARG;
   1377 }
   1378 #endif
   1379 
   1380 int opus_dred_decoder_init(OpusDREDDecoder *dec)
   1381 {
   1382   int ret = 0;
   1383   dec->loaded = 0;
   1384 #if defined(ENABLE_DRED) && !defined(USE_WEIGHTS_FILE)
   1385   ret = init_rdovaedec(&dec->model, rdovaedec_arrays);
   1386   if (ret == 0) dec->loaded = 1;
   1387 #endif
   1388   dec->arch = opus_select_arch();
   1389   /* To make sure nobody forgets to init, use a magic number. */
   1390   dec->magic = 0xD8EDDEC0;
   1391   return (ret == 0) ? OPUS_OK : OPUS_UNIMPLEMENTED;
   1392 }
   1393 
   1394 OpusDREDDecoder *opus_dred_decoder_create(int *error)
   1395 {
   1396   int ret;
   1397   OpusDREDDecoder *dec;
   1398   dec = (OpusDREDDecoder *)opus_alloc(opus_dred_decoder_get_size());
   1399   if (dec == NULL)
   1400   {
   1401      if (error)
   1402         *error = OPUS_ALLOC_FAIL;
   1403      return NULL;
   1404   }
   1405   ret = opus_dred_decoder_init(dec);
   1406   if (error)
   1407      *error = ret;
   1408   if (ret != OPUS_OK)
   1409   {
   1410      opus_free(dec);
   1411      dec = NULL;
   1412   }
   1413   return dec;
   1414 }
   1415 
   1416 void opus_dred_decoder_destroy(OpusDREDDecoder *dec)
   1417 {
   1418   if (dec) dec->magic = 0xDE57801D;
   1419   opus_free(dec);
   1420 }
   1421 
   1422 int opus_dred_decoder_ctl(OpusDREDDecoder *dred_dec, int request, ...)
   1423 {
   1424 #ifdef ENABLE_DRED
   1425   int ret = OPUS_OK;
   1426   va_list ap;
   1427 
   1428   va_start(ap, request);
   1429   (void)dred_dec;
   1430   switch (request)
   1431   {
   1432 # ifdef USE_WEIGHTS_FILE
   1433   case OPUS_SET_DNN_BLOB_REQUEST:
   1434   {
   1435      const unsigned char *data = va_arg(ap, const unsigned char *);
   1436      opus_int32 len = va_arg(ap, opus_int32);
   1437      if(len<0 || data == NULL)
   1438      {
   1439         goto bad_arg;
   1440      }
   1441      return dred_decoder_load_model(dred_dec, data, len);
   1442   }
   1443   break;
   1444 # endif
   1445   default:
   1446     /*fprintf(stderr, "unknown opus_decoder_ctl() request: %d", request);*/
   1447     ret = OPUS_UNIMPLEMENTED;
   1448     break;
   1449  }
   1450  va_end(ap);
   1451  return ret;
   1452 # ifdef USE_WEIGHTS_FILE
   1453 bad_arg:
   1454  va_end(ap);
   1455  return OPUS_BAD_ARG;
   1456 # endif
   1457 #else
   1458  (void)dred_dec;
   1459  (void)request;
   1460  return OPUS_UNIMPLEMENTED;
   1461 #endif
   1462 }
   1463 
   1464 #ifdef ENABLE_DRED
   1465 static int dred_find_payload(const unsigned char *data, opus_int32 len, const unsigned char **payload, int *dred_frame_offset)
   1466 {
   1467   OpusExtensionIterator iter;
   1468   opus_extension_data ext;
   1469   const unsigned char *padding;
   1470   opus_int32 padding_len;
   1471   int nb_frames;
   1472   const unsigned char *frames[48];
   1473   opus_int16 size[48];
   1474   int frame_size;
   1475   int ret;
   1476 
   1477   *payload = NULL;
   1478   /* Get the padding section of the packet. */
   1479   ret = opus_packet_parse_impl(data, len, 0, NULL, frames, size, NULL, NULL,
   1480    &padding, &padding_len);
   1481   if (ret < 0)
   1482      return ret;
   1483   nb_frames = ret;
   1484   frame_size = opus_packet_get_samples_per_frame(data, 48000);
   1485   opus_extension_iterator_init(&iter, padding, padding_len, nb_frames);
   1486   for (;;) {
   1487      ret = opus_extension_iterator_find(&iter, &ext, DRED_EXTENSION_ID);
   1488      if (ret <= 0)
   1489         return ret;
   1490      /* DRED position in the packet, in units of 2.5 ms like for the signaled DRED offset. */
   1491      *dred_frame_offset = ext.frame*frame_size/120;
   1492 #ifdef DRED_EXPERIMENTAL_VERSION
   1493      /* Check that temporary extension type and version match.
   1494         This check will be removed once extension is finalized. */
   1495      if (ext.len > DRED_EXPERIMENTAL_BYTES && ext.data[0] == 'D'
   1496       && ext.data[1] == DRED_EXPERIMENTAL_VERSION) {
   1497         *payload = ext.data+2;
   1498         return ext.len-2;
   1499      }
   1500 #else
   1501      if (ext.len > 0) {
   1502         *payload = ext.data;
   1503         return ext.len;
   1504      }
   1505 #endif
   1506   }
   1507 }
   1508 #endif
   1509 
   1510 int opus_dred_get_size(void)
   1511 {
   1512 #ifdef ENABLE_DRED
   1513  return sizeof(OpusDRED);
   1514 #else
   1515  return 0;
   1516 #endif
   1517 }
   1518 
   1519 OpusDRED *opus_dred_alloc(int *error)
   1520 {
   1521 #ifdef ENABLE_DRED
   1522  OpusDRED *dec;
   1523  dec = (OpusDRED *)opus_alloc(opus_dred_get_size());
   1524  if (dec == NULL)
   1525  {
   1526    if (error)
   1527      *error = OPUS_ALLOC_FAIL;
   1528    return NULL;
   1529  }
   1530  return dec;
   1531 #else
   1532  if (error)
   1533    *error = OPUS_UNIMPLEMENTED;
   1534  return NULL;
   1535 #endif
   1536 }
   1537 
   1538 void opus_dred_free(OpusDRED *dec)
   1539 {
   1540 #ifdef ENABLE_DRED
   1541  opus_free(dec);
   1542 #else
   1543  (void)dec;
   1544 #endif
   1545 }
   1546 
   1547 int opus_dred_parse(OpusDREDDecoder *dred_dec, OpusDRED *dred, const unsigned char *data, opus_int32 len, opus_int32 max_dred_samples, opus_int32 sampling_rate, int *dred_end, int defer_processing)
   1548 {
   1549 #ifdef ENABLE_DRED
   1550   const unsigned char *payload;
   1551   opus_int32 payload_len;
   1552   int dred_frame_offset=0;
   1553   VALIDATE_DRED_DECODER(dred_dec);
   1554   if (!dred_dec->loaded) return OPUS_UNIMPLEMENTED;
   1555   dred->process_stage = -1;
   1556   payload_len = dred_find_payload(data, len, &payload, &dred_frame_offset);
   1557   if (payload_len < 0)
   1558      return payload_len;
   1559   if (payload != NULL)
   1560   {
   1561      int offset;
   1562      int min_feature_frames;
   1563      offset = 100*max_dred_samples/sampling_rate;
   1564      min_feature_frames = IMIN(2 + offset, 2*DRED_NUM_REDUNDANCY_FRAMES);
   1565      dred_ec_decode(dred, payload, payload_len, min_feature_frames, dred_frame_offset);
   1566      if (!defer_processing)
   1567         opus_dred_process(dred_dec, dred, dred);
   1568      if (dred_end) *dred_end = IMAX(0, -dred->dred_offset*sampling_rate/400);
   1569      return IMAX(0, dred->nb_latents*sampling_rate/25 - dred->dred_offset* sampling_rate/400);
   1570   }
   1571   if (dred_end) *dred_end = 0;
   1572   return 0;
   1573 #else
   1574   (void)dred_dec;
   1575   (void)dred;
   1576   (void)data;
   1577   (void)len;
   1578   (void)max_dred_samples;
   1579   (void)sampling_rate;
   1580   (void)defer_processing;
   1581   (void)dred_end;
   1582   return OPUS_UNIMPLEMENTED;
   1583 #endif
   1584 }
   1585 
   1586 int opus_dred_process(OpusDREDDecoder *dred_dec, const OpusDRED *src, OpusDRED *dst)
   1587 {
   1588 #ifdef ENABLE_DRED
   1589   if (dred_dec == NULL || src == NULL || dst == NULL || (src->process_stage != 1 && src->process_stage != 2))
   1590      return OPUS_BAD_ARG;
   1591   VALIDATE_DRED_DECODER(dred_dec);
   1592   if (!dred_dec->loaded) return OPUS_UNIMPLEMENTED;
   1593   if (src != dst)
   1594      OPUS_COPY(dst, src, 1);
   1595   if (dst->process_stage == 2)
   1596      return OPUS_OK;
   1597   DRED_rdovae_decode_all(&dred_dec->model, dst->fec_features, dst->state, dst->latents, dst->nb_latents, dred_dec->arch);
   1598   dst->process_stage = 2;
   1599   return OPUS_OK;
   1600 #else
   1601   (void)dred_dec;
   1602   (void)src;
   1603   (void)dst;
   1604   return OPUS_UNIMPLEMENTED;
   1605 #endif
   1606 }
   1607 
   1608 int opus_decoder_dred_decode(OpusDecoder *st, const OpusDRED *dred, opus_int32 dred_offset, opus_int16 *pcm, opus_int32 frame_size)
   1609 {
   1610 #ifdef ENABLE_DRED
   1611   VARDECL(float, out);
   1612   int ret, i;
   1613   ALLOC_STACK;
   1614 
   1615   if(frame_size<=0)
   1616   {
   1617      RESTORE_STACK;
   1618      return OPUS_BAD_ARG;
   1619   }
   1620 
   1621   celt_assert(st->channels == 1 || st->channels == 2);
   1622   ALLOC(out, frame_size*st->channels, float);
   1623 
   1624   ret = opus_decode_native(st, NULL, 0, out, frame_size, 0, 0, NULL, 1, dred, dred_offset);
   1625   if (ret > 0)
   1626   {
   1627      for (i=0;i<ret*st->channels;i++)
   1628         pcm[i] = RES2INT16(out[i]);
   1629   }
   1630   RESTORE_STACK;
   1631   return ret;
   1632 #else
   1633   (void)st;
   1634   (void)dred;
   1635   (void)dred_offset;
   1636   (void)pcm;
   1637   (void)frame_size;
   1638   return OPUS_UNIMPLEMENTED;
   1639 #endif
   1640 }
   1641 
   1642 int opus_decoder_dred_decode24(OpusDecoder *st, const OpusDRED *dred, opus_int32 dred_offset, opus_int32 *pcm, opus_int32 frame_size)
   1643 {
   1644 #ifdef ENABLE_DRED
   1645   VARDECL(float, out);
   1646   int ret, i;
   1647   ALLOC_STACK;
   1648 
   1649   if(frame_size<=0)
   1650   {
   1651      RESTORE_STACK;
   1652      return OPUS_BAD_ARG;
   1653   }
   1654 
   1655   celt_assert(st->channels == 1 || st->channels == 2);
   1656   ALLOC(out, frame_size*st->channels, float);
   1657 
   1658   ret = opus_decode_native(st, NULL, 0, out, frame_size, 0, 0, NULL, 1, dred, dred_offset);
   1659   if (ret > 0)
   1660   {
   1661      for (i=0;i<ret*st->channels;i++)
   1662         pcm[i] = RES2INT24(out[i]);
   1663   }
   1664   RESTORE_STACK;
   1665   return ret;
   1666 #else
   1667   (void)st;
   1668   (void)dred;
   1669   (void)dred_offset;
   1670   (void)pcm;
   1671   (void)frame_size;
   1672   return OPUS_UNIMPLEMENTED;
   1673 #endif
   1674 }
   1675 
   1676 int opus_decoder_dred_decode_float(OpusDecoder *st, const OpusDRED *dred, opus_int32 dred_offset, float *pcm, opus_int32 frame_size)
   1677 {
   1678 #ifdef ENABLE_DRED
   1679   if(frame_size<=0)
   1680      return OPUS_BAD_ARG;
   1681   return opus_decode_native(st, NULL, 0, pcm, frame_size, 0, 0, NULL, 0, dred, dred_offset);
   1682 #else
   1683   (void)st;
   1684   (void)dred;
   1685   (void)dred_offset;
   1686   (void)pcm;
   1687   (void)frame_size;
   1688   return OPUS_UNIMPLEMENTED;
   1689 #endif
   1690 }