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 struct OpusDecoder {
56    int          celt_dec_offset;
57    int          silk_dec_offset;
58    int          channels;
59    opus_int32   Fs;          /** Sampling rate (at the API level) */
60    silk_DecControlStruct DecControl;
61    int          decode_gain;
62    int          arch;
63 
64    /* Everything beyond this point gets cleared on a reset */
65 #define OPUS_DECODER_RESET_START stream_channels
66    int          stream_channels;
67 
68    int          bandwidth;
69    int          mode;
70    int          prev_mode;
71    int          frame_size;
72    int          prev_redundancy;
73    int          last_packet_duration;
74 #ifndef FIXED_POINT
75    opus_val16   softclip_mem[2];
76 #endif
77 
78    opus_uint32  rangeFinal;
79 };
80 
81 #if defined(ENABLE_HARDENING) || defined(ENABLE_ASSERTIONS)
validate_opus_decoder(OpusDecoder * st)82 static void validate_opus_decoder(OpusDecoder *st)
83 {
84    celt_assert(st->channels == 1 || st->channels == 2);
85    celt_assert(st->Fs == 48000 || st->Fs == 24000 || st->Fs == 16000 || st->Fs == 12000 || st->Fs == 8000);
86    celt_assert(st->DecControl.API_sampleRate == st->Fs);
87    celt_assert(st->DecControl.internalSampleRate == 0 || st->DecControl.internalSampleRate == 16000 || st->DecControl.internalSampleRate == 12000 || st->DecControl.internalSampleRate == 8000);
88    celt_assert(st->DecControl.nChannelsAPI == st->channels);
89    celt_assert(st->DecControl.nChannelsInternal == 0 || st->DecControl.nChannelsInternal == 1 || st->DecControl.nChannelsInternal == 2);
90    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);
91 #ifdef OPUS_ARCHMASK
92    celt_assert(st->arch >= 0);
93    celt_assert(st->arch <= OPUS_ARCHMASK);
94 #endif
95    celt_assert(st->stream_channels == 1 || st->stream_channels == 2);
96 }
97 #define VALIDATE_OPUS_DECODER(st) validate_opus_decoder(st)
98 #else
99 #define VALIDATE_OPUS_DECODER(st)
100 #endif
101 
opus_decoder_get_size(int channels)102 int opus_decoder_get_size(int channels)
103 {
104    int silkDecSizeBytes, celtDecSizeBytes;
105    int ret;
106    if (channels<1 || channels > 2)
107       return 0;
108    ret = silk_Get_Decoder_Size( &silkDecSizeBytes );
109    if(ret)
110       return 0;
111    silkDecSizeBytes = align(silkDecSizeBytes);
112    celtDecSizeBytes = celt_decoder_get_size(channels);
113    return align(sizeof(OpusDecoder))+silkDecSizeBytes+celtDecSizeBytes;
114 }
115 
opus_decoder_init(OpusDecoder * st,opus_int32 Fs,int channels)116 int opus_decoder_init(OpusDecoder *st, opus_int32 Fs, int channels)
117 {
118    void *silk_dec;
119    CELTDecoder *celt_dec;
120    int ret, silkDecSizeBytes;
121 
122    if ((Fs!=48000&&Fs!=24000&&Fs!=16000&&Fs!=12000&&Fs!=8000)
123     || (channels!=1&&channels!=2))
124       return OPUS_BAD_ARG;
125 
126    OPUS_CLEAR((char*)st, opus_decoder_get_size(channels));
127    /* Initialize SILK decoder */
128    ret = silk_Get_Decoder_Size(&silkDecSizeBytes);
129    if (ret)
130       return OPUS_INTERNAL_ERROR;
131 
132    silkDecSizeBytes = align(silkDecSizeBytes);
133    st->silk_dec_offset = align(sizeof(OpusDecoder));
134    st->celt_dec_offset = st->silk_dec_offset+silkDecSizeBytes;
135    silk_dec = (char*)st+st->silk_dec_offset;
136    celt_dec = (CELTDecoder*)((char*)st+st->celt_dec_offset);
137    st->stream_channels = st->channels = channels;
138 
139    st->Fs = Fs;
140    st->DecControl.API_sampleRate = st->Fs;
141    st->DecControl.nChannelsAPI      = st->channels;
142 
143    /* Reset decoder */
144    ret = silk_InitDecoder( silk_dec );
145    if(ret)return OPUS_INTERNAL_ERROR;
146 
147    /* Initialize CELT decoder */
148    ret = celt_decoder_init(celt_dec, Fs, channels);
149    if(ret!=OPUS_OK)return OPUS_INTERNAL_ERROR;
150 
151    celt_decoder_ctl(celt_dec, CELT_SET_SIGNALLING(0));
152 
153    st->prev_mode = 0;
154    st->frame_size = Fs/400;
155    st->arch = opus_select_arch();
156    return OPUS_OK;
157 }
158 
opus_decoder_create(opus_int32 Fs,int channels,int * error)159 OpusDecoder *opus_decoder_create(opus_int32 Fs, int channels, int *error)
160 {
161    int ret;
162    OpusDecoder *st;
163    if ((Fs!=48000&&Fs!=24000&&Fs!=16000&&Fs!=12000&&Fs!=8000)
164     || (channels!=1&&channels!=2))
165    {
166       if (error)
167          *error = OPUS_BAD_ARG;
168       return NULL;
169    }
170    st = (OpusDecoder *)opus_alloc(opus_decoder_get_size(channels));
171    if (st == NULL)
172    {
173       if (error)
174          *error = OPUS_ALLOC_FAIL;
175       return NULL;
176    }
177    ret = opus_decoder_init(st, Fs, channels);
178    if (error)
179       *error = ret;
180    if (ret != OPUS_OK)
181    {
182       opus_free(st);
183       st = NULL;
184    }
185    return st;
186 }
187 
smooth_fade(const opus_val16 * in1,const opus_val16 * in2,opus_val16 * out,int overlap,int channels,const opus_val16 * window,opus_int32 Fs)188 static void smooth_fade(const opus_val16 *in1, const opus_val16 *in2,
189       opus_val16 *out, int overlap, int channels,
190       const opus_val16 *window, opus_int32 Fs)
191 {
192    int i, c;
193    int inc = 48000/Fs;
194    for (c=0;c<channels;c++)
195    {
196       for (i=0;i<overlap;i++)
197       {
198          opus_val16 w = MULT16_16_Q15(window[i*inc], window[i*inc]);
199          out[i*channels+c] = SHR32(MAC16_16(MULT16_16(w,in2[i*channels+c]),
200                                    Q15ONE-w, in1[i*channels+c]), 15);
201       }
202    }
203 }
204 
opus_packet_get_mode(const unsigned char * data)205 static int opus_packet_get_mode(const unsigned char *data)
206 {
207    int mode;
208    if (data[0]&0x80)
209    {
210       mode = MODE_CELT_ONLY;
211    } else if ((data[0]&0x60) == 0x60)
212    {
213       mode = MODE_HYBRID;
214    } else {
215       mode = MODE_SILK_ONLY;
216    }
217    return mode;
218 }
219 
opus_decode_frame(OpusDecoder * st,const unsigned char * data,opus_int32 len,opus_val16 * pcm,int frame_size,int decode_fec)220 static int opus_decode_frame(OpusDecoder *st, const unsigned char *data,
221       opus_int32 len, opus_val16 *pcm, int frame_size, int decode_fec)
222 {
223    void *silk_dec;
224    CELTDecoder *celt_dec;
225    int i, silk_ret=0, celt_ret=0;
226    ec_dec dec;
227    opus_int32 silk_frame_size;
228    int pcm_silk_size;
229    VARDECL(opus_int16, pcm_silk);
230    int pcm_transition_silk_size;
231    VARDECL(opus_val16, pcm_transition_silk);
232    int pcm_transition_celt_size;
233    VARDECL(opus_val16, pcm_transition_celt);
234    opus_val16 *pcm_transition=NULL;
235    int redundant_audio_size;
236    VARDECL(opus_val16, redundant_audio);
237 
238    int audiosize;
239    int mode;
240    int bandwidth;
241    int transition=0;
242    int start_band;
243    int redundancy=0;
244    int redundancy_bytes = 0;
245    int celt_to_silk=0;
246    int c;
247    int F2_5, F5, F10, F20;
248    const opus_val16 *window;
249    opus_uint32 redundant_rng = 0;
250    int celt_accum;
251    ALLOC_STACK;
252 
253    silk_dec = (char*)st+st->silk_dec_offset;
254    celt_dec = (CELTDecoder*)((char*)st+st->celt_dec_offset);
255    F20 = st->Fs/50;
256    F10 = F20>>1;
257    F5 = F10>>1;
258    F2_5 = F5>>1;
259    if (frame_size < F2_5)
260    {
261       RESTORE_STACK;
262       return OPUS_BUFFER_TOO_SMALL;
263    }
264    /* Limit frame_size to avoid excessive stack allocations. */
265    frame_size = IMIN(frame_size, st->Fs/25*3);
266    /* Payloads of 1 (2 including ToC) or 0 trigger the PLC/DTX */
267    if (len<=1)
268    {
269       data = NULL;
270       /* In that case, don't conceal more than what the ToC says */
271       frame_size = IMIN(frame_size, st->frame_size);
272    }
273    if (data != NULL)
274    {
275       audiosize = st->frame_size;
276       mode = st->mode;
277       bandwidth = st->bandwidth;
278       ec_dec_init(&dec,(unsigned char*)data,len);
279    } else {
280       audiosize = frame_size;
281       mode = st->prev_mode;
282       bandwidth = 0;
283 
284       if (mode == 0)
285       {
286          /* If we haven't got any packet yet, all we can do is return zeros */
287          for (i=0;i<audiosize*st->channels;i++)
288             pcm[i] = 0;
289          RESTORE_STACK;
290          return audiosize;
291       }
292 
293       /* Avoids trying to run the PLC on sizes other than 2.5 (CELT), 5 (CELT),
294          10, or 20 (e.g. 12.5 or 30 ms). */
295       if (audiosize > F20)
296       {
297          do {
298             int ret = opus_decode_frame(st, NULL, 0, pcm, IMIN(audiosize, F20), 0);
299             if (ret<0)
300             {
301                RESTORE_STACK;
302                return ret;
303             }
304             pcm += ret*st->channels;
305             audiosize -= ret;
306          } while (audiosize > 0);
307          RESTORE_STACK;
308          return frame_size;
309       } else if (audiosize < F20)
310       {
311          if (audiosize > F10)
312             audiosize = F10;
313          else if (mode != MODE_SILK_ONLY && audiosize > F5 && audiosize < F10)
314             audiosize = F5;
315       }
316    }
317 
318    /* In fixed-point, we can tell CELT to do the accumulation on top of the
319       SILK PCM buffer. This saves some stack space. */
320 #ifdef FIXED_POINT
321    celt_accum = (mode != MODE_CELT_ONLY) && (frame_size >= F10);
322 #else
323    celt_accum = 0;
324 #endif
325 
326    pcm_transition_silk_size = ALLOC_NONE;
327    pcm_transition_celt_size = ALLOC_NONE;
328    if (data!=NULL && st->prev_mode > 0 && (
329        (mode == MODE_CELT_ONLY && st->prev_mode != MODE_CELT_ONLY && !st->prev_redundancy)
330     || (mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY) )
331       )
332    {
333       transition = 1;
334       /* Decide where to allocate the stack memory for pcm_transition */
335       if (mode == MODE_CELT_ONLY)
336          pcm_transition_celt_size = F5*st->channels;
337       else
338          pcm_transition_silk_size = F5*st->channels;
339    }
340    ALLOC(pcm_transition_celt, pcm_transition_celt_size, opus_val16);
341    if (transition && mode == MODE_CELT_ONLY)
342    {
343       pcm_transition = pcm_transition_celt;
344       opus_decode_frame(st, NULL, 0, pcm_transition, IMIN(F5, audiosize), 0);
345    }
346    if (audiosize > frame_size)
347    {
348       /*fprintf(stderr, "PCM buffer too small: %d vs %d (mode = %d)\n", audiosize, frame_size, mode);*/
349       RESTORE_STACK;
350       return OPUS_BAD_ARG;
351    } else {
352       frame_size = audiosize;
353    }
354 
355    /* Don't allocate any memory when in CELT-only mode */
356    pcm_silk_size = (mode != MODE_CELT_ONLY && !celt_accum) ? IMAX(F10, frame_size)*st->channels : ALLOC_NONE;
357    ALLOC(pcm_silk, pcm_silk_size, opus_int16);
358 
359    /* SILK processing */
360    if (mode != MODE_CELT_ONLY)
361    {
362       int lost_flag, decoded_samples;
363       opus_int16 *pcm_ptr;
364 #ifdef FIXED_POINT
365       if (celt_accum)
366          pcm_ptr = pcm;
367       else
368 #endif
369          pcm_ptr = pcm_silk;
370 
371       if (st->prev_mode==MODE_CELT_ONLY)
372          silk_InitDecoder( silk_dec );
373 
374       /* The SILK PLC cannot produce frames of less than 10 ms */
375       st->DecControl.payloadSize_ms = IMAX(10, 1000 * audiosize / st->Fs);
376 
377       if (data != NULL)
378       {
379         st->DecControl.nChannelsInternal = st->stream_channels;
380         if( mode == MODE_SILK_ONLY ) {
381            if( bandwidth == OPUS_BANDWIDTH_NARROWBAND ) {
382               st->DecControl.internalSampleRate = 8000;
383            } else if( bandwidth == OPUS_BANDWIDTH_MEDIUMBAND ) {
384               st->DecControl.internalSampleRate = 12000;
385            } else if( bandwidth == OPUS_BANDWIDTH_WIDEBAND ) {
386               st->DecControl.internalSampleRate = 16000;
387            } else {
388               st->DecControl.internalSampleRate = 16000;
389               celt_assert( 0 );
390            }
391         } else {
392            /* Hybrid mode */
393            st->DecControl.internalSampleRate = 16000;
394         }
395      }
396 
397      lost_flag = data == NULL ? 1 : 2 * decode_fec;
398      decoded_samples = 0;
399      do {
400         /* Call SILK decoder */
401         int first_frame = decoded_samples == 0;
402         silk_ret = silk_Decode( silk_dec, &st->DecControl,
403                                 lost_flag, first_frame, &dec, pcm_ptr, &silk_frame_size, st->arch );
404         if( silk_ret ) {
405            if (lost_flag) {
406               /* PLC failure should not be fatal */
407               silk_frame_size = frame_size;
408               for (i=0;i<frame_size*st->channels;i++)
409                  pcm_ptr[i] = 0;
410            } else {
411              RESTORE_STACK;
412              return OPUS_INTERNAL_ERROR;
413            }
414         }
415         pcm_ptr += silk_frame_size * st->channels;
416         decoded_samples += silk_frame_size;
417       } while( decoded_samples < frame_size );
418    }
419 
420    start_band = 0;
421    if (!decode_fec && mode != MODE_CELT_ONLY && data != NULL
422     && ec_tell(&dec)+17+20*(st->mode == MODE_HYBRID) <= 8*len)
423    {
424       /* Check if we have a redundant 0-8 kHz band */
425       if (mode == MODE_HYBRID)
426          redundancy = ec_dec_bit_logp(&dec, 12);
427       else
428          redundancy = 1;
429       if (redundancy)
430       {
431          celt_to_silk = ec_dec_bit_logp(&dec, 1);
432          /* redundancy_bytes will be at least two, in the non-hybrid
433             case due to the ec_tell() check above */
434          redundancy_bytes = mode==MODE_HYBRID ?
435                (opus_int32)ec_dec_uint(&dec, 256)+2 :
436                len-((ec_tell(&dec)+7)>>3);
437          len -= redundancy_bytes;
438          /* This is a sanity check. It should never happen for a valid
439             packet, so the exact behaviour is not normative. */
440          if (len*8 < ec_tell(&dec))
441          {
442             len = 0;
443             redundancy_bytes = 0;
444             redundancy = 0;
445          }
446          /* Shrink decoder because of raw bits */
447          dec.storage -= redundancy_bytes;
448       }
449    }
450    if (mode != MODE_CELT_ONLY)
451       start_band = 17;
452 
453    if (redundancy)
454    {
455       transition = 0;
456       pcm_transition_silk_size=ALLOC_NONE;
457    }
458 
459    ALLOC(pcm_transition_silk, pcm_transition_silk_size, opus_val16);
460 
461    if (transition && mode != MODE_CELT_ONLY)
462    {
463       pcm_transition = pcm_transition_silk;
464       opus_decode_frame(st, NULL, 0, pcm_transition, IMIN(F5, audiosize), 0);
465    }
466 
467 
468    if (bandwidth)
469    {
470       int endband=21;
471 
472       switch(bandwidth)
473       {
474       case OPUS_BANDWIDTH_NARROWBAND:
475          endband = 13;
476          break;
477       case OPUS_BANDWIDTH_MEDIUMBAND:
478       case OPUS_BANDWIDTH_WIDEBAND:
479          endband = 17;
480          break;
481       case OPUS_BANDWIDTH_SUPERWIDEBAND:
482          endband = 19;
483          break;
484       case OPUS_BANDWIDTH_FULLBAND:
485          endband = 21;
486          break;
487       default:
488          celt_assert(0);
489          break;
490       }
491       MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_SET_END_BAND(endband)));
492    }
493    MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_SET_CHANNELS(st->stream_channels)));
494 
495    /* Only allocation memory for redundancy if/when needed */
496    redundant_audio_size = redundancy ? F5*st->channels : ALLOC_NONE;
497    ALLOC(redundant_audio, redundant_audio_size, opus_val16);
498 
499    /* 5 ms redundant frame for CELT->SILK*/
500    if (redundancy && celt_to_silk)
501    {
502       MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(0)));
503       celt_decode_with_ec(celt_dec, data+len, redundancy_bytes,
504                           redundant_audio, F5, NULL, 0);
505       MUST_SUCCEED(celt_decoder_ctl(celt_dec, OPUS_GET_FINAL_RANGE(&redundant_rng)));
506    }
507 
508    /* MUST be after PLC */
509    MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(start_band)));
510 
511    if (mode != MODE_SILK_ONLY)
512    {
513       int celt_frame_size = IMIN(F20, frame_size);
514       /* Make sure to discard any previous CELT state */
515       if (mode != st->prev_mode && st->prev_mode > 0 && !st->prev_redundancy)
516          MUST_SUCCEED(celt_decoder_ctl(celt_dec, OPUS_RESET_STATE));
517       /* Decode CELT */
518       celt_ret = celt_decode_with_ec(celt_dec, decode_fec ? NULL : data,
519                                      len, pcm, celt_frame_size, &dec, celt_accum);
520    } else {
521       unsigned char silence[2] = {0xFF, 0xFF};
522       if (!celt_accum)
523       {
524          for (i=0;i<frame_size*st->channels;i++)
525             pcm[i] = 0;
526       }
527       /* For hybrid -> SILK transitions, we let the CELT MDCT
528          do a fade-out by decoding a silence frame */
529       if (st->prev_mode == MODE_HYBRID && !(redundancy && celt_to_silk && st->prev_redundancy) )
530       {
531          MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(0)));
532          celt_decode_with_ec(celt_dec, silence, 2, pcm, F2_5, NULL, celt_accum);
533       }
534    }
535 
536    if (mode != MODE_CELT_ONLY && !celt_accum)
537    {
538 #ifdef FIXED_POINT
539       for (i=0;i<frame_size*st->channels;i++)
540          pcm[i] = SAT16(ADD32(pcm[i], pcm_silk[i]));
541 #else
542       for (i=0;i<frame_size*st->channels;i++)
543          pcm[i] = pcm[i] + (opus_val16)((1.f/32768.f)*pcm_silk[i]);
544 #endif
545    }
546 
547    {
548       const CELTMode *celt_mode;
549       MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_GET_MODE(&celt_mode)));
550       window = celt_mode->window;
551    }
552 
553    /* 5 ms redundant frame for SILK->CELT */
554    if (redundancy && !celt_to_silk)
555    {
556       MUST_SUCCEED(celt_decoder_ctl(celt_dec, OPUS_RESET_STATE));
557       MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(0)));
558 
559       celt_decode_with_ec(celt_dec, data+len, redundancy_bytes, redundant_audio, F5, NULL, 0);
560       MUST_SUCCEED(celt_decoder_ctl(celt_dec, OPUS_GET_FINAL_RANGE(&redundant_rng)));
561       smooth_fade(pcm+st->channels*(frame_size-F2_5), redundant_audio+st->channels*F2_5,
562                   pcm+st->channels*(frame_size-F2_5), F2_5, st->channels, window, st->Fs);
563    }
564    if (redundancy && celt_to_silk)
565    {
566       for (c=0;c<st->channels;c++)
567       {
568          for (i=0;i<F2_5;i++)
569             pcm[st->channels*i+c] = redundant_audio[st->channels*i+c];
570       }
571       smooth_fade(redundant_audio+st->channels*F2_5, pcm+st->channels*F2_5,
572                   pcm+st->channels*F2_5, F2_5, st->channels, window, st->Fs);
573    }
574    if (transition)
575    {
576       if (audiosize >= F5)
577       {
578          for (i=0;i<st->channels*F2_5;i++)
579             pcm[i] = pcm_transition[i];
580          smooth_fade(pcm_transition+st->channels*F2_5, pcm+st->channels*F2_5,
581                      pcm+st->channels*F2_5, F2_5,
582                      st->channels, window, st->Fs);
583       } else {
584          /* Not enough time to do a clean transition, but we do it anyway
585             This will not preserve amplitude perfectly and may introduce
586             a bit of temporal aliasing, but it shouldn't be too bad and
587             that's pretty much the best we can do. In any case, generating this
588             transition it pretty silly in the first place */
589          smooth_fade(pcm_transition, pcm,
590                      pcm, F2_5,
591                      st->channels, window, st->Fs);
592       }
593    }
594 
595    if(st->decode_gain)
596    {
597       opus_val32 gain;
598       gain = celt_exp2(MULT16_16_P15(QCONST16(6.48814081e-4f, 25), st->decode_gain));
599       for (i=0;i<frame_size*st->channels;i++)
600       {
601          opus_val32 x;
602          x = MULT16_32_P16(pcm[i],gain);
603          pcm[i] = SATURATE(x, 32767);
604       }
605    }
606 
607    if (len <= 1)
608       st->rangeFinal = 0;
609    else
610       st->rangeFinal = dec.rng ^ redundant_rng;
611 
612    st->prev_mode = mode;
613    st->prev_redundancy = redundancy && !celt_to_silk;
614 
615    if (celt_ret>=0)
616    {
617       if (OPUS_CHECK_ARRAY(pcm, audiosize*st->channels))
618          OPUS_PRINT_INT(audiosize);
619    }
620 
621    RESTORE_STACK;
622    return celt_ret < 0 ? celt_ret : audiosize;
623 
624 }
625 
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)626 int opus_decode_native(OpusDecoder *st, const unsigned char *data,
627       opus_int32 len, opus_val16 *pcm, int frame_size, int decode_fec,
628       int self_delimited, opus_int32 *packet_offset, int soft_clip)
629 {
630    int i, nb_samples;
631    int count, offset;
632    unsigned char toc;
633    int packet_frame_size, packet_bandwidth, packet_mode, packet_stream_channels;
634    /* 48 x 2.5 ms = 120 ms */
635    opus_int16 size[48];
636    VALIDATE_OPUS_DECODER(st);
637    if (decode_fec<0 || decode_fec>1)
638       return OPUS_BAD_ARG;
639    /* For FEC/PLC, frame_size has to be to have a multiple of 2.5 ms */
640    if ((decode_fec || len==0 || data==NULL) && frame_size%(st->Fs/400)!=0)
641       return OPUS_BAD_ARG;
642    if (len==0 || data==NULL)
643    {
644       int pcm_count=0;
645       do {
646          int ret;
647          ret = opus_decode_frame(st, NULL, 0, pcm+pcm_count*st->channels, frame_size-pcm_count, 0);
648          if (ret<0)
649             return ret;
650          pcm_count += ret;
651       } while (pcm_count < frame_size);
652       celt_assert(pcm_count == frame_size);
653       if (OPUS_CHECK_ARRAY(pcm, pcm_count*st->channels))
654          OPUS_PRINT_INT(pcm_count);
655       st->last_packet_duration = pcm_count;
656       return pcm_count;
657    } else if (len<0)
658       return OPUS_BAD_ARG;
659 
660    packet_mode = opus_packet_get_mode(data);
661    packet_bandwidth = opus_packet_get_bandwidth(data);
662    packet_frame_size = opus_packet_get_samples_per_frame(data, st->Fs);
663    packet_stream_channels = opus_packet_get_nb_channels(data);
664 
665    count = opus_packet_parse_impl(data, len, self_delimited, &toc, NULL,
666                                   size, &offset, packet_offset);
667    if (count<0)
668       return count;
669 
670    data += offset;
671 
672    if (decode_fec)
673    {
674       int duration_copy;
675       int ret;
676       /* If no FEC can be present, run the PLC (recursive call) */
677       if (frame_size < packet_frame_size || packet_mode == MODE_CELT_ONLY || st->mode == MODE_CELT_ONLY)
678          return opus_decode_native(st, NULL, 0, pcm, frame_size, 0, 0, NULL, soft_clip);
679       /* Otherwise, run the PLC on everything except the size for which we might have FEC */
680       duration_copy = st->last_packet_duration;
681       if (frame_size-packet_frame_size!=0)
682       {
683          ret = opus_decode_native(st, NULL, 0, pcm, frame_size-packet_frame_size, 0, 0, NULL, soft_clip);
684          if (ret<0)
685          {
686             st->last_packet_duration = duration_copy;
687             return ret;
688          }
689          celt_assert(ret==frame_size-packet_frame_size);
690       }
691       /* Complete with FEC */
692       st->mode = packet_mode;
693       st->bandwidth = packet_bandwidth;
694       st->frame_size = packet_frame_size;
695       st->stream_channels = packet_stream_channels;
696       ret = opus_decode_frame(st, data, size[0], pcm+st->channels*(frame_size-packet_frame_size),
697             packet_frame_size, 1);
698       if (ret<0)
699          return ret;
700       else {
701          if (OPUS_CHECK_ARRAY(pcm, frame_size*st->channels))
702             OPUS_PRINT_INT(frame_size);
703          st->last_packet_duration = frame_size;
704          return frame_size;
705       }
706    }
707 
708    if (count*packet_frame_size > frame_size)
709       return OPUS_BUFFER_TOO_SMALL;
710 
711    /* Update the state as the last step to avoid updating it on an invalid packet */
712    st->mode = packet_mode;
713    st->bandwidth = packet_bandwidth;
714    st->frame_size = packet_frame_size;
715    st->stream_channels = packet_stream_channels;
716 
717    nb_samples=0;
718    for (i=0;i<count;i++)
719    {
720       int ret;
721       ret = opus_decode_frame(st, data, size[i], pcm+nb_samples*st->channels, frame_size-nb_samples, 0);
722       if (ret<0)
723          return ret;
724       celt_assert(ret==packet_frame_size);
725       data += size[i];
726       nb_samples += ret;
727    }
728    st->last_packet_duration = nb_samples;
729    if (OPUS_CHECK_ARRAY(pcm, nb_samples*st->channels))
730       OPUS_PRINT_INT(nb_samples);
731 #ifndef FIXED_POINT
732    if (soft_clip)
733       opus_pcm_soft_clip(pcm, nb_samples, st->channels, st->softclip_mem);
734    else
735       st->softclip_mem[0]=st->softclip_mem[1]=0;
736 #endif
737    return nb_samples;
738 }
739 
740 #ifdef FIXED_POINT
741 
opus_decode(OpusDecoder * st,const unsigned char * data,opus_int32 len,opus_val16 * pcm,int frame_size,int decode_fec)742 int opus_decode(OpusDecoder *st, const unsigned char *data,
743       opus_int32 len, opus_val16 *pcm, int frame_size, int decode_fec)
744 {
745    if(frame_size<=0)
746       return OPUS_BAD_ARG;
747    return opus_decode_native(st, data, len, pcm, frame_size, decode_fec, 0, NULL, 0);
748 }
749 
750 #ifndef DISABLE_FLOAT_API
opus_decode_float(OpusDecoder * st,const unsigned char * data,opus_int32 len,float * pcm,int frame_size,int decode_fec)751 int opus_decode_float(OpusDecoder *st, const unsigned char *data,
752       opus_int32 len, float *pcm, int frame_size, int decode_fec)
753 {
754    VARDECL(opus_int16, out);
755    int ret, i;
756    int nb_samples;
757    ALLOC_STACK;
758 
759    if(frame_size<=0)
760    {
761       RESTORE_STACK;
762       return OPUS_BAD_ARG;
763    }
764    if (data != NULL && len > 0 && !decode_fec)
765    {
766       nb_samples = opus_decoder_get_nb_samples(st, data, len);
767       if (nb_samples>0)
768          frame_size = IMIN(frame_size, nb_samples);
769       else
770          return OPUS_INVALID_PACKET;
771    }
772    celt_assert(st->channels == 1 || st->channels == 2);
773    ALLOC(out, frame_size*st->channels, opus_int16);
774 
775    ret = opus_decode_native(st, data, len, out, frame_size, decode_fec, 0, NULL, 0);
776    if (ret > 0)
777    {
778       for (i=0;i<ret*st->channels;i++)
779          pcm[i] = (1.f/32768.f)*(out[i]);
780    }
781    RESTORE_STACK;
782    return ret;
783 }
784 #endif
785 
786 
787 #else
opus_decode(OpusDecoder * st,const unsigned char * data,opus_int32 len,opus_int16 * pcm,int frame_size,int decode_fec)788 int opus_decode(OpusDecoder *st, const unsigned char *data,
789       opus_int32 len, opus_int16 *pcm, int frame_size, int decode_fec)
790 {
791    VARDECL(float, out);
792    int ret, i;
793    int nb_samples;
794    ALLOC_STACK;
795 
796    if(frame_size<=0)
797    {
798       RESTORE_STACK;
799       return OPUS_BAD_ARG;
800    }
801 
802    if (data != NULL && len > 0 && !decode_fec)
803    {
804       nb_samples = opus_decoder_get_nb_samples(st, data, len);
805       if (nb_samples>0)
806          frame_size = IMIN(frame_size, nb_samples);
807       else
808          return OPUS_INVALID_PACKET;
809    }
810    celt_assert(st->channels == 1 || st->channels == 2);
811    ALLOC(out, frame_size*st->channels, float);
812 
813    ret = opus_decode_native(st, data, len, out, frame_size, decode_fec, 0, NULL, 1);
814    if (ret > 0)
815    {
816       for (i=0;i<ret*st->channels;i++)
817          pcm[i] = FLOAT2INT16(out[i]);
818    }
819    RESTORE_STACK;
820    return ret;
821 }
822 
opus_decode_float(OpusDecoder * st,const unsigned char * data,opus_int32 len,opus_val16 * pcm,int frame_size,int decode_fec)823 int opus_decode_float(OpusDecoder *st, const unsigned char *data,
824       opus_int32 len, opus_val16 *pcm, int frame_size, int decode_fec)
825 {
826    if(frame_size<=0)
827       return OPUS_BAD_ARG;
828    return opus_decode_native(st, data, len, pcm, frame_size, decode_fec, 0, NULL, 0);
829 }
830 
831 #endif
832 
opus_decoder_ctl(OpusDecoder * st,int request,...)833 int opus_decoder_ctl(OpusDecoder *st, int request, ...)
834 {
835    int ret = OPUS_OK;
836    va_list ap;
837    void *silk_dec;
838    CELTDecoder *celt_dec;
839 
840    silk_dec = (char*)st+st->silk_dec_offset;
841    celt_dec = (CELTDecoder*)((char*)st+st->celt_dec_offset);
842 
843 
844    va_start(ap, request);
845 
846    switch (request)
847    {
848    case OPUS_GET_BANDWIDTH_REQUEST:
849    {
850       opus_int32 *value = va_arg(ap, opus_int32*);
851       if (!value)
852       {
853          goto bad_arg;
854       }
855       *value = st->bandwidth;
856    }
857    break;
858    case OPUS_GET_FINAL_RANGE_REQUEST:
859    {
860       opus_uint32 *value = va_arg(ap, opus_uint32*);
861       if (!value)
862       {
863          goto bad_arg;
864       }
865       *value = st->rangeFinal;
866    }
867    break;
868    case OPUS_RESET_STATE:
869    {
870       OPUS_CLEAR((char*)&st->OPUS_DECODER_RESET_START,
871             sizeof(OpusDecoder)-
872             ((char*)&st->OPUS_DECODER_RESET_START - (char*)st));
873 
874       celt_decoder_ctl(celt_dec, OPUS_RESET_STATE);
875       silk_InitDecoder( silk_dec );
876       st->stream_channels = st->channels;
877       st->frame_size = st->Fs/400;
878    }
879    break;
880    case OPUS_GET_SAMPLE_RATE_REQUEST:
881    {
882       opus_int32 *value = va_arg(ap, opus_int32*);
883       if (!value)
884       {
885          goto bad_arg;
886       }
887       *value = st->Fs;
888    }
889    break;
890    case OPUS_GET_PITCH_REQUEST:
891    {
892       opus_int32 *value = va_arg(ap, opus_int32*);
893       if (!value)
894       {
895          goto bad_arg;
896       }
897       if (st->prev_mode == MODE_CELT_ONLY)
898          ret = celt_decoder_ctl(celt_dec, OPUS_GET_PITCH(value));
899       else
900          *value = st->DecControl.prevPitchLag;
901    }
902    break;
903    case OPUS_GET_GAIN_REQUEST:
904    {
905       opus_int32 *value = va_arg(ap, opus_int32*);
906       if (!value)
907       {
908          goto bad_arg;
909       }
910       *value = st->decode_gain;
911    }
912    break;
913    case OPUS_SET_GAIN_REQUEST:
914    {
915        opus_int32 value = va_arg(ap, opus_int32);
916        if (value<-32768 || value>32767)
917        {
918           goto bad_arg;
919        }
920        st->decode_gain = value;
921    }
922    break;
923    case OPUS_GET_LAST_PACKET_DURATION_REQUEST:
924    {
925       opus_int32 *value = va_arg(ap, opus_int32*);
926       if (!value)
927       {
928          goto bad_arg;
929       }
930       *value = st->last_packet_duration;
931    }
932    break;
933    case OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST:
934    {
935        opus_int32 value = va_arg(ap, opus_int32);
936        if(value<0 || value>1)
937        {
938           goto bad_arg;
939        }
940        ret = celt_decoder_ctl(celt_dec, OPUS_SET_PHASE_INVERSION_DISABLED(value));
941    }
942    break;
943    case OPUS_GET_PHASE_INVERSION_DISABLED_REQUEST:
944    {
945        opus_int32 *value = va_arg(ap, opus_int32*);
946        if (!value)
947        {
948           goto bad_arg;
949        }
950        ret = celt_decoder_ctl(celt_dec, OPUS_GET_PHASE_INVERSION_DISABLED(value));
951    }
952    break;
953    default:
954       /*fprintf(stderr, "unknown opus_decoder_ctl() request: %d", request);*/
955       ret = OPUS_UNIMPLEMENTED;
956       break;
957    }
958 
959    va_end(ap);
960    return ret;
961 bad_arg:
962    va_end(ap);
963    return OPUS_BAD_ARG;
964 }
965 
opus_decoder_destroy(OpusDecoder * st)966 void opus_decoder_destroy(OpusDecoder *st)
967 {
968    opus_free(st);
969 }
970 
971 
opus_packet_get_bandwidth(const unsigned char * data)972 int opus_packet_get_bandwidth(const unsigned char *data)
973 {
974    int bandwidth;
975    if (data[0]&0x80)
976    {
977       bandwidth = OPUS_BANDWIDTH_MEDIUMBAND + ((data[0]>>5)&0x3);
978       if (bandwidth == OPUS_BANDWIDTH_MEDIUMBAND)
979          bandwidth = OPUS_BANDWIDTH_NARROWBAND;
980    } else if ((data[0]&0x60) == 0x60)
981    {
982       bandwidth = (data[0]&0x10) ? OPUS_BANDWIDTH_FULLBAND :
983                                    OPUS_BANDWIDTH_SUPERWIDEBAND;
984    } else {
985       bandwidth = OPUS_BANDWIDTH_NARROWBAND + ((data[0]>>5)&0x3);
986    }
987    return bandwidth;
988 }
989 
opus_packet_get_nb_channels(const unsigned char * data)990 int opus_packet_get_nb_channels(const unsigned char *data)
991 {
992    return (data[0]&0x4) ? 2 : 1;
993 }
994 
opus_packet_get_nb_frames(const unsigned char packet[],opus_int32 len)995 int opus_packet_get_nb_frames(const unsigned char packet[], opus_int32 len)
996 {
997    int count;
998    if (len<1)
999       return OPUS_BAD_ARG;
1000    count = packet[0]&0x3;
1001    if (count==0)
1002       return 1;
1003    else if (count!=3)
1004       return 2;
1005    else if (len<2)
1006       return OPUS_INVALID_PACKET;
1007    else
1008       return packet[1]&0x3F;
1009 }
1010 
opus_packet_get_nb_samples(const unsigned char packet[],opus_int32 len,opus_int32 Fs)1011 int opus_packet_get_nb_samples(const unsigned char packet[], opus_int32 len,
1012       opus_int32 Fs)
1013 {
1014    int samples;
1015    int count = opus_packet_get_nb_frames(packet, len);
1016 
1017    if (count<0)
1018       return count;
1019 
1020    samples = count*opus_packet_get_samples_per_frame(packet, Fs);
1021    /* Can't have more than 120 ms */
1022    if (samples*25 > Fs*3)
1023       return OPUS_INVALID_PACKET;
1024    else
1025       return samples;
1026 }
1027 
opus_decoder_get_nb_samples(const OpusDecoder * dec,const unsigned char packet[],opus_int32 len)1028 int opus_decoder_get_nb_samples(const OpusDecoder *dec,
1029       const unsigned char packet[], opus_int32 len)
1030 {
1031    return opus_packet_get_nb_samples(packet, len, dec->Fs);
1032 }
1033