Coverage Report

Created: 2026-10-28 08:13

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