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