• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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       /* Run PLC using last used mode (CELT if we ended with CELT redundancy) */
282       mode = st->prev_redundancy ? MODE_CELT_ONLY : st->prev_mode;
283       bandwidth = 0;
284 
285       if (mode == 0)
286       {
287          /* If we haven't got any packet yet, all we can do is return zeros */
288          for (i=0;i<audiosize*st->channels;i++)
289             pcm[i] = 0;
290          RESTORE_STACK;
291          return audiosize;
292       }
293 
294       /* Avoids trying to run the PLC on sizes other than 2.5 (CELT), 5 (CELT),
295          10, or 20 (e.g. 12.5 or 30 ms). */
296       if (audiosize > F20)
297       {
298          do {
299             int ret = opus_decode_frame(st, NULL, 0, pcm, IMIN(audiosize, F20), 0);
300             if (ret<0)
301             {
302                RESTORE_STACK;
303                return ret;
304             }
305             pcm += ret*st->channels;
306             audiosize -= ret;
307          } while (audiosize > 0);
308          RESTORE_STACK;
309          return frame_size;
310       } else if (audiosize < F20)
311       {
312          if (audiosize > F10)
313             audiosize = F10;
314          else if (mode != MODE_SILK_ONLY && audiosize > F5 && audiosize < F10)
315             audiosize = F5;
316       }
317    }
318 
319    /* In fixed-point, we can tell CELT to do the accumulation on top of the
320       SILK PCM buffer. This saves some stack space. */
321 #ifdef FIXED_POINT
322    celt_accum = (mode != MODE_CELT_ONLY) && (frame_size >= F10);
323 #else
324    celt_accum = 0;
325 #endif
326 
327    pcm_transition_silk_size = ALLOC_NONE;
328    pcm_transition_celt_size = ALLOC_NONE;
329    if (data!=NULL && st->prev_mode > 0 && (
330        (mode == MODE_CELT_ONLY && st->prev_mode != MODE_CELT_ONLY && !st->prev_redundancy)
331     || (mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY) )
332       )
333    {
334       transition = 1;
335       /* Decide where to allocate the stack memory for pcm_transition */
336       if (mode == MODE_CELT_ONLY)
337          pcm_transition_celt_size = F5*st->channels;
338       else
339          pcm_transition_silk_size = F5*st->channels;
340    }
341    ALLOC(pcm_transition_celt, pcm_transition_celt_size, opus_val16);
342    if (transition && mode == MODE_CELT_ONLY)
343    {
344       pcm_transition = pcm_transition_celt;
345       opus_decode_frame(st, NULL, 0, pcm_transition, IMIN(F5, audiosize), 0);
346    }
347    if (audiosize > frame_size)
348    {
349       /*fprintf(stderr, "PCM buffer too small: %d vs %d (mode = %d)\n", audiosize, frame_size, mode);*/
350       RESTORE_STACK;
351       return OPUS_BAD_ARG;
352    } else {
353       frame_size = audiosize;
354    }
355 
356    /* Don't allocate any memory when in CELT-only mode */
357    pcm_silk_size = (mode != MODE_CELT_ONLY && !celt_accum) ? IMAX(F10, frame_size)*st->channels : ALLOC_NONE;
358    ALLOC(pcm_silk, pcm_silk_size, opus_int16);
359 
360    /* SILK processing */
361    if (mode != MODE_CELT_ONLY)
362    {
363       int lost_flag, decoded_samples;
364       opus_int16 *pcm_ptr;
365 #ifdef FIXED_POINT
366       if (celt_accum)
367          pcm_ptr = pcm;
368       else
369 #endif
370          pcm_ptr = pcm_silk;
371 
372       if (st->prev_mode==MODE_CELT_ONLY)
373          silk_InitDecoder( silk_dec );
374 
375       /* The SILK PLC cannot produce frames of less than 10 ms */
376       st->DecControl.payloadSize_ms = IMAX(10, 1000 * audiosize / st->Fs);
377 
378       if (data != NULL)
379       {
380         st->DecControl.nChannelsInternal = st->stream_channels;
381         if( mode == MODE_SILK_ONLY ) {
382            if( bandwidth == OPUS_BANDWIDTH_NARROWBAND ) {
383               st->DecControl.internalSampleRate = 8000;
384            } else if( bandwidth == OPUS_BANDWIDTH_MEDIUMBAND ) {
385               st->DecControl.internalSampleRate = 12000;
386            } else if( bandwidth == OPUS_BANDWIDTH_WIDEBAND ) {
387               st->DecControl.internalSampleRate = 16000;
388            } else {
389               st->DecControl.internalSampleRate = 16000;
390               celt_assert( 0 );
391            }
392         } else {
393            /* Hybrid mode */
394            st->DecControl.internalSampleRate = 16000;
395         }
396      }
397 
398      lost_flag = data == NULL ? 1 : 2 * decode_fec;
399      decoded_samples = 0;
400      do {
401         /* Call SILK decoder */
402         int first_frame = decoded_samples == 0;
403         silk_ret = silk_Decode( silk_dec, &st->DecControl,
404                                 lost_flag, first_frame, &dec, pcm_ptr, &silk_frame_size, st->arch );
405         if( silk_ret ) {
406            if (lost_flag) {
407               /* PLC failure should not be fatal */
408               silk_frame_size = frame_size;
409               for (i=0;i<frame_size*st->channels;i++)
410                  pcm_ptr[i] = 0;
411            } else {
412              RESTORE_STACK;
413              return OPUS_INTERNAL_ERROR;
414            }
415         }
416         pcm_ptr += silk_frame_size * st->channels;
417         decoded_samples += silk_frame_size;
418       } while( decoded_samples < frame_size );
419    }
420 
421    start_band = 0;
422    if (!decode_fec && mode != MODE_CELT_ONLY && data != NULL
423     && ec_tell(&dec)+17+20*(mode == MODE_HYBRID) <= 8*len)
424    {
425       /* Check if we have a redundant 0-8 kHz band */
426       if (mode == MODE_HYBRID)
427          redundancy = ec_dec_bit_logp(&dec, 12);
428       else
429          redundancy = 1;
430       if (redundancy)
431       {
432          celt_to_silk = ec_dec_bit_logp(&dec, 1);
433          /* redundancy_bytes will be at least two, in the non-hybrid
434             case due to the ec_tell() check above */
435          redundancy_bytes = mode==MODE_HYBRID ?
436                (opus_int32)ec_dec_uint(&dec, 256)+2 :
437                len-((ec_tell(&dec)+7)>>3);
438          len -= redundancy_bytes;
439          /* This is a sanity check. It should never happen for a valid
440             packet, so the exact behaviour is not normative. */
441          if (len*8 < ec_tell(&dec))
442          {
443             len = 0;
444             redundancy_bytes = 0;
445             redundancy = 0;
446          }
447          /* Shrink decoder because of raw bits */
448          dec.storage -= redundancy_bytes;
449       }
450    }
451    if (mode != MODE_CELT_ONLY)
452       start_band = 17;
453 
454    if (redundancy)
455    {
456       transition = 0;
457       pcm_transition_silk_size=ALLOC_NONE;
458    }
459 
460    ALLOC(pcm_transition_silk, pcm_transition_silk_size, opus_val16);
461 
462    if (transition && mode != MODE_CELT_ONLY)
463    {
464       pcm_transition = pcm_transition_silk;
465       opus_decode_frame(st, NULL, 0, pcm_transition, IMIN(F5, audiosize), 0);
466    }
467 
468 
469    if (bandwidth)
470    {
471       int endband=21;
472 
473       switch(bandwidth)
474       {
475       case OPUS_BANDWIDTH_NARROWBAND:
476          endband = 13;
477          break;
478       case OPUS_BANDWIDTH_MEDIUMBAND:
479       case OPUS_BANDWIDTH_WIDEBAND:
480          endband = 17;
481          break;
482       case OPUS_BANDWIDTH_SUPERWIDEBAND:
483          endband = 19;
484          break;
485       case OPUS_BANDWIDTH_FULLBAND:
486          endband = 21;
487          break;
488       default:
489          celt_assert(0);
490          break;
491       }
492       MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_SET_END_BAND(endband)));
493    }
494    MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_SET_CHANNELS(st->stream_channels)));
495 
496    /* Only allocation memory for redundancy if/when needed */
497    redundant_audio_size = redundancy ? F5*st->channels : ALLOC_NONE;
498    ALLOC(redundant_audio, redundant_audio_size, opus_val16);
499 
500    /* 5 ms redundant frame for CELT->SILK*/
501    if (redundancy && celt_to_silk)
502    {
503       /* If the previous frame did not use CELT (the first redundancy frame in
504          a transition from SILK may have been lost) then the CELT decoder is
505          stale at this point and the redundancy audio is not useful, however
506          the final range is still needed (for testing), so the redundancy is
507          always decoded but the decoded audio may not be used */
508       MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(0)));
509       celt_decode_with_ec(celt_dec, data+len, redundancy_bytes,
510                           redundant_audio, F5, NULL, 0);
511       MUST_SUCCEED(celt_decoder_ctl(celt_dec, OPUS_GET_FINAL_RANGE(&redundant_rng)));
512    }
513 
514    /* MUST be after PLC */
515    MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(start_band)));
516 
517    if (mode != MODE_SILK_ONLY)
518    {
519       int celt_frame_size = IMIN(F20, frame_size);
520       /* Make sure to discard any previous CELT state */
521       if (mode != st->prev_mode && st->prev_mode > 0 && !st->prev_redundancy)
522          MUST_SUCCEED(celt_decoder_ctl(celt_dec, OPUS_RESET_STATE));
523       /* Decode CELT */
524       celt_ret = celt_decode_with_ec(celt_dec, decode_fec ? NULL : data,
525                                      len, pcm, celt_frame_size, &dec, celt_accum);
526    } else {
527       unsigned char silence[2] = {0xFF, 0xFF};
528       if (!celt_accum)
529       {
530          for (i=0;i<frame_size*st->channels;i++)
531             pcm[i] = 0;
532       }
533       /* For hybrid -> SILK transitions, we let the CELT MDCT
534          do a fade-out by decoding a silence frame */
535       if (st->prev_mode == MODE_HYBRID && !(redundancy && celt_to_silk && st->prev_redundancy) )
536       {
537          MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(0)));
538          celt_decode_with_ec(celt_dec, silence, 2, pcm, F2_5, NULL, celt_accum);
539       }
540    }
541 
542    if (mode != MODE_CELT_ONLY && !celt_accum)
543    {
544 #ifdef FIXED_POINT
545       for (i=0;i<frame_size*st->channels;i++)
546          pcm[i] = SAT16(ADD32(pcm[i], pcm_silk[i]));
547 #else
548       for (i=0;i<frame_size*st->channels;i++)
549          pcm[i] = pcm[i] + (opus_val16)((1.f/32768.f)*pcm_silk[i]);
550 #endif
551    }
552 
553    {
554       const CELTMode *celt_mode;
555       MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_GET_MODE(&celt_mode)));
556       window = celt_mode->window;
557    }
558 
559    /* 5 ms redundant frame for SILK->CELT */
560    if (redundancy && !celt_to_silk)
561    {
562       MUST_SUCCEED(celt_decoder_ctl(celt_dec, OPUS_RESET_STATE));
563       MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(0)));
564 
565       celt_decode_with_ec(celt_dec, data+len, redundancy_bytes, redundant_audio, F5, NULL, 0);
566       MUST_SUCCEED(celt_decoder_ctl(celt_dec, OPUS_GET_FINAL_RANGE(&redundant_rng)));
567       smooth_fade(pcm+st->channels*(frame_size-F2_5), redundant_audio+st->channels*F2_5,
568                   pcm+st->channels*(frame_size-F2_5), F2_5, st->channels, window, st->Fs);
569    }
570    /* 5ms redundant frame for CELT->SILK; ignore if the previous frame did not
571       use CELT (the first redundancy frame in a transition from SILK may have
572       been lost) */
573    if (redundancy && celt_to_silk && (st->prev_mode != MODE_SILK_ONLY || st->prev_redundancy))
574    {
575       for (c=0;c<st->channels;c++)
576       {
577          for (i=0;i<F2_5;i++)
578             pcm[st->channels*i+c] = redundant_audio[st->channels*i+c];
579       }
580       smooth_fade(redundant_audio+st->channels*F2_5, pcm+st->channels*F2_5,
581                   pcm+st->channels*F2_5, F2_5, st->channels, window, st->Fs);
582    }
583    if (transition)
584    {
585       if (audiosize >= F5)
586       {
587          for (i=0;i<st->channels*F2_5;i++)
588             pcm[i] = pcm_transition[i];
589          smooth_fade(pcm_transition+st->channels*F2_5, pcm+st->channels*F2_5,
590                      pcm+st->channels*F2_5, F2_5,
591                      st->channels, window, st->Fs);
592       } else {
593          /* Not enough time to do a clean transition, but we do it anyway
594             This will not preserve amplitude perfectly and may introduce
595             a bit of temporal aliasing, but it shouldn't be too bad and
596             that's pretty much the best we can do. In any case, generating this
597             transition it pretty silly in the first place */
598          smooth_fade(pcm_transition, pcm,
599                      pcm, F2_5,
600                      st->channels, window, st->Fs);
601       }
602    }
603 
604    if(st->decode_gain)
605    {
606       opus_val32 gain;
607       gain = celt_exp2(MULT16_16_P15(QCONST16(6.48814081e-4f, 25), st->decode_gain));
608       for (i=0;i<frame_size*st->channels;i++)
609       {
610          opus_val32 x;
611          x = MULT16_32_P16(pcm[i],gain);
612          pcm[i] = SATURATE(x, 32767);
613       }
614    }
615 
616    if (len <= 1)
617       st->rangeFinal = 0;
618    else
619       st->rangeFinal = dec.rng ^ redundant_rng;
620 
621    st->prev_mode = mode;
622    st->prev_redundancy = redundancy && !celt_to_silk;
623 
624    if (celt_ret>=0)
625    {
626       if (OPUS_CHECK_ARRAY(pcm, audiosize*st->channels))
627          OPUS_PRINT_INT(audiosize);
628    }
629 
630    RESTORE_STACK;
631    return celt_ret < 0 ? celt_ret : audiosize;
632 
633 }
634 
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)635 int opus_decode_native(OpusDecoder *st, const unsigned char *data,
636       opus_int32 len, opus_val16 *pcm, int frame_size, int decode_fec,
637       int self_delimited, opus_int32 *packet_offset, int soft_clip)
638 {
639    int i, nb_samples;
640    int count, offset;
641    unsigned char toc;
642    int packet_frame_size, packet_bandwidth, packet_mode, packet_stream_channels;
643    /* 48 x 2.5 ms = 120 ms */
644    opus_int16 size[48];
645    VALIDATE_OPUS_DECODER(st);
646    if (decode_fec<0 || decode_fec>1)
647       return OPUS_BAD_ARG;
648    /* For FEC/PLC, frame_size has to be to have a multiple of 2.5 ms */
649    if ((decode_fec || len==0 || data==NULL) && frame_size%(st->Fs/400)!=0)
650       return OPUS_BAD_ARG;
651    if (len==0 || data==NULL)
652    {
653       int pcm_count=0;
654       do {
655          int ret;
656          ret = opus_decode_frame(st, NULL, 0, pcm+pcm_count*st->channels, frame_size-pcm_count, 0);
657          if (ret<0)
658             return ret;
659          pcm_count += ret;
660       } while (pcm_count < frame_size);
661       celt_assert(pcm_count == frame_size);
662       if (OPUS_CHECK_ARRAY(pcm, pcm_count*st->channels))
663          OPUS_PRINT_INT(pcm_count);
664       st->last_packet_duration = pcm_count;
665       return pcm_count;
666    } else if (len<0)
667       return OPUS_BAD_ARG;
668 
669    packet_mode = opus_packet_get_mode(data);
670    packet_bandwidth = opus_packet_get_bandwidth(data);
671    packet_frame_size = opus_packet_get_samples_per_frame(data, st->Fs);
672    packet_stream_channels = opus_packet_get_nb_channels(data);
673 
674    count = opus_packet_parse_impl(data, len, self_delimited, &toc, NULL,
675                                   size, &offset, packet_offset);
676    if (count<0)
677       return count;
678 
679    data += offset;
680 
681    if (decode_fec)
682    {
683       int duration_copy;
684       int ret;
685       /* If no FEC can be present, run the PLC (recursive call) */
686       if (frame_size < packet_frame_size || packet_mode == MODE_CELT_ONLY || st->mode == MODE_CELT_ONLY)
687          return opus_decode_native(st, NULL, 0, pcm, frame_size, 0, 0, NULL, soft_clip);
688       /* Otherwise, run the PLC on everything except the size for which we might have FEC */
689       duration_copy = st->last_packet_duration;
690       if (frame_size-packet_frame_size!=0)
691       {
692          ret = opus_decode_native(st, NULL, 0, pcm, frame_size-packet_frame_size, 0, 0, NULL, soft_clip);
693          if (ret<0)
694          {
695             st->last_packet_duration = duration_copy;
696             return ret;
697          }
698          celt_assert(ret==frame_size-packet_frame_size);
699       }
700       /* Complete with FEC */
701       st->mode = packet_mode;
702       st->bandwidth = packet_bandwidth;
703       st->frame_size = packet_frame_size;
704       st->stream_channels = packet_stream_channels;
705       ret = opus_decode_frame(st, data, size[0], pcm+st->channels*(frame_size-packet_frame_size),
706             packet_frame_size, 1);
707       if (ret<0)
708          return ret;
709       else {
710          if (OPUS_CHECK_ARRAY(pcm, frame_size*st->channels))
711             OPUS_PRINT_INT(frame_size);
712          st->last_packet_duration = frame_size;
713          return frame_size;
714       }
715    }
716 
717    if (count*packet_frame_size > frame_size)
718       return OPUS_BUFFER_TOO_SMALL;
719 
720    /* Update the state as the last step to avoid updating it on an invalid packet */
721    st->mode = packet_mode;
722    st->bandwidth = packet_bandwidth;
723    st->frame_size = packet_frame_size;
724    st->stream_channels = packet_stream_channels;
725 
726    nb_samples=0;
727    for (i=0;i<count;i++)
728    {
729       int ret;
730       ret = opus_decode_frame(st, data, size[i], pcm+nb_samples*st->channels, frame_size-nb_samples, 0);
731       if (ret<0)
732          return ret;
733       celt_assert(ret==packet_frame_size);
734       data += size[i];
735       nb_samples += ret;
736    }
737    st->last_packet_duration = nb_samples;
738    if (OPUS_CHECK_ARRAY(pcm, nb_samples*st->channels))
739       OPUS_PRINT_INT(nb_samples);
740 #ifndef FIXED_POINT
741    if (soft_clip)
742       opus_pcm_soft_clip(pcm, nb_samples, st->channels, st->softclip_mem);
743    else
744       st->softclip_mem[0]=st->softclip_mem[1]=0;
745 #endif
746    return nb_samples;
747 }
748 
749 #ifdef FIXED_POINT
750 
opus_decode(OpusDecoder * st,const unsigned char * data,opus_int32 len,opus_val16 * pcm,int frame_size,int decode_fec)751 int opus_decode(OpusDecoder *st, const unsigned char *data,
752       opus_int32 len, opus_val16 *pcm, int frame_size, int decode_fec)
753 {
754    if(frame_size<=0)
755       return OPUS_BAD_ARG;
756    return opus_decode_native(st, data, len, pcm, frame_size, decode_fec, 0, NULL, 0);
757 }
758 
759 #ifndef DISABLE_FLOAT_API
opus_decode_float(OpusDecoder * st,const unsigned char * data,opus_int32 len,float * pcm,int frame_size,int decode_fec)760 int opus_decode_float(OpusDecoder *st, const unsigned char *data,
761       opus_int32 len, float *pcm, int frame_size, int decode_fec)
762 {
763    VARDECL(opus_int16, out);
764    int ret, i;
765    int nb_samples;
766    ALLOC_STACK;
767 
768    if(frame_size<=0)
769    {
770       RESTORE_STACK;
771       return OPUS_BAD_ARG;
772    }
773    if (data != NULL && len > 0 && !decode_fec)
774    {
775       nb_samples = opus_decoder_get_nb_samples(st, data, len);
776       if (nb_samples>0)
777          frame_size = IMIN(frame_size, nb_samples);
778       else
779          return OPUS_INVALID_PACKET;
780    }
781    celt_assert(st->channels == 1 || st->channels == 2);
782    ALLOC(out, frame_size*st->channels, opus_int16);
783 
784    ret = opus_decode_native(st, data, len, out, frame_size, decode_fec, 0, NULL, 0);
785    if (ret > 0)
786    {
787       for (i=0;i<ret*st->channels;i++)
788          pcm[i] = (1.f/32768.f)*(out[i]);
789    }
790    RESTORE_STACK;
791    return ret;
792 }
793 #endif
794 
795 
796 #else
opus_decode(OpusDecoder * st,const unsigned char * data,opus_int32 len,opus_int16 * pcm,int frame_size,int decode_fec)797 int opus_decode(OpusDecoder *st, const unsigned char *data,
798       opus_int32 len, opus_int16 *pcm, int frame_size, int decode_fec)
799 {
800    VARDECL(float, out);
801    int ret, i;
802    int nb_samples;
803    ALLOC_STACK;
804 
805    if(frame_size<=0)
806    {
807       RESTORE_STACK;
808       return OPUS_BAD_ARG;
809    }
810 
811    if (data != NULL && len > 0 && !decode_fec)
812    {
813       nb_samples = opus_decoder_get_nb_samples(st, data, len);
814       if (nb_samples>0)
815          frame_size = IMIN(frame_size, nb_samples);
816       else
817          return OPUS_INVALID_PACKET;
818    }
819    celt_assert(st->channels == 1 || st->channels == 2);
820    ALLOC(out, frame_size*st->channels, float);
821 
822    ret = opus_decode_native(st, data, len, out, frame_size, decode_fec, 0, NULL, 1);
823    if (ret > 0)
824    {
825       for (i=0;i<ret*st->channels;i++)
826          pcm[i] = FLOAT2INT16(out[i]);
827    }
828    RESTORE_STACK;
829    return ret;
830 }
831 
opus_decode_float(OpusDecoder * st,const unsigned char * data,opus_int32 len,opus_val16 * pcm,int frame_size,int decode_fec)832 int opus_decode_float(OpusDecoder *st, const unsigned char *data,
833       opus_int32 len, opus_val16 *pcm, int frame_size, int decode_fec)
834 {
835    if(frame_size<=0)
836       return OPUS_BAD_ARG;
837    return opus_decode_native(st, data, len, pcm, frame_size, decode_fec, 0, NULL, 0);
838 }
839 
840 #endif
841 
opus_decoder_ctl(OpusDecoder * st,int request,...)842 int opus_decoder_ctl(OpusDecoder *st, int request, ...)
843 {
844    int ret = OPUS_OK;
845    va_list ap;
846    void *silk_dec;
847    CELTDecoder *celt_dec;
848 
849    silk_dec = (char*)st+st->silk_dec_offset;
850    celt_dec = (CELTDecoder*)((char*)st+st->celt_dec_offset);
851 
852 
853    va_start(ap, request);
854 
855    switch (request)
856    {
857    case OPUS_GET_BANDWIDTH_REQUEST:
858    {
859       opus_int32 *value = va_arg(ap, opus_int32*);
860       if (!value)
861       {
862          goto bad_arg;
863       }
864       *value = st->bandwidth;
865    }
866    break;
867    case OPUS_GET_FINAL_RANGE_REQUEST:
868    {
869       opus_uint32 *value = va_arg(ap, opus_uint32*);
870       if (!value)
871       {
872          goto bad_arg;
873       }
874       *value = st->rangeFinal;
875    }
876    break;
877    case OPUS_RESET_STATE:
878    {
879       OPUS_CLEAR((char*)&st->OPUS_DECODER_RESET_START,
880             sizeof(OpusDecoder)-
881             ((char*)&st->OPUS_DECODER_RESET_START - (char*)st));
882 
883       celt_decoder_ctl(celt_dec, OPUS_RESET_STATE);
884       silk_InitDecoder( silk_dec );
885       st->stream_channels = st->channels;
886       st->frame_size = st->Fs/400;
887    }
888    break;
889    case OPUS_GET_SAMPLE_RATE_REQUEST:
890    {
891       opus_int32 *value = va_arg(ap, opus_int32*);
892       if (!value)
893       {
894          goto bad_arg;
895       }
896       *value = st->Fs;
897    }
898    break;
899    case OPUS_GET_PITCH_REQUEST:
900    {
901       opus_int32 *value = va_arg(ap, opus_int32*);
902       if (!value)
903       {
904          goto bad_arg;
905       }
906       if (st->prev_mode == MODE_CELT_ONLY)
907          ret = celt_decoder_ctl(celt_dec, OPUS_GET_PITCH(value));
908       else
909          *value = st->DecControl.prevPitchLag;
910    }
911    break;
912    case OPUS_GET_GAIN_REQUEST:
913    {
914       opus_int32 *value = va_arg(ap, opus_int32*);
915       if (!value)
916       {
917          goto bad_arg;
918       }
919       *value = st->decode_gain;
920    }
921    break;
922    case OPUS_SET_GAIN_REQUEST:
923    {
924        opus_int32 value = va_arg(ap, opus_int32);
925        if (value<-32768 || value>32767)
926        {
927           goto bad_arg;
928        }
929        st->decode_gain = value;
930    }
931    break;
932    case OPUS_GET_LAST_PACKET_DURATION_REQUEST:
933    {
934       opus_int32 *value = va_arg(ap, opus_int32*);
935       if (!value)
936       {
937          goto bad_arg;
938       }
939       *value = st->last_packet_duration;
940    }
941    break;
942    case OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST:
943    {
944        opus_int32 value = va_arg(ap, opus_int32);
945        if(value<0 || value>1)
946        {
947           goto bad_arg;
948        }
949        ret = celt_decoder_ctl(celt_dec, OPUS_SET_PHASE_INVERSION_DISABLED(value));
950    }
951    break;
952    case OPUS_GET_PHASE_INVERSION_DISABLED_REQUEST:
953    {
954        opus_int32 *value = va_arg(ap, opus_int32*);
955        if (!value)
956        {
957           goto bad_arg;
958        }
959        ret = celt_decoder_ctl(celt_dec, OPUS_GET_PHASE_INVERSION_DISABLED(value));
960    }
961    break;
962    default:
963       /*fprintf(stderr, "unknown opus_decoder_ctl() request: %d", request);*/
964       ret = OPUS_UNIMPLEMENTED;
965       break;
966    }
967 
968    va_end(ap);
969    return ret;
970 bad_arg:
971    va_end(ap);
972    return OPUS_BAD_ARG;
973 }
974 
opus_decoder_destroy(OpusDecoder * st)975 void opus_decoder_destroy(OpusDecoder *st)
976 {
977    opus_free(st);
978 }
979 
980 
opus_packet_get_bandwidth(const unsigned char * data)981 int opus_packet_get_bandwidth(const unsigned char *data)
982 {
983    int bandwidth;
984    if (data[0]&0x80)
985    {
986       bandwidth = OPUS_BANDWIDTH_MEDIUMBAND + ((data[0]>>5)&0x3);
987       if (bandwidth == OPUS_BANDWIDTH_MEDIUMBAND)
988          bandwidth = OPUS_BANDWIDTH_NARROWBAND;
989    } else if ((data[0]&0x60) == 0x60)
990    {
991       bandwidth = (data[0]&0x10) ? OPUS_BANDWIDTH_FULLBAND :
992                                    OPUS_BANDWIDTH_SUPERWIDEBAND;
993    } else {
994       bandwidth = OPUS_BANDWIDTH_NARROWBAND + ((data[0]>>5)&0x3);
995    }
996    return bandwidth;
997 }
998 
opus_packet_get_nb_channels(const unsigned char * data)999 int opus_packet_get_nb_channels(const unsigned char *data)
1000 {
1001    return (data[0]&0x4) ? 2 : 1;
1002 }
1003 
opus_packet_get_nb_frames(const unsigned char packet[],opus_int32 len)1004 int opus_packet_get_nb_frames(const unsigned char packet[], opus_int32 len)
1005 {
1006    int count;
1007    if (len<1)
1008       return OPUS_BAD_ARG;
1009    count = packet[0]&0x3;
1010    if (count==0)
1011       return 1;
1012    else if (count!=3)
1013       return 2;
1014    else if (len<2)
1015       return OPUS_INVALID_PACKET;
1016    else
1017       return packet[1]&0x3F;
1018 }
1019 
opus_packet_get_nb_samples(const unsigned char packet[],opus_int32 len,opus_int32 Fs)1020 int opus_packet_get_nb_samples(const unsigned char packet[], opus_int32 len,
1021       opus_int32 Fs)
1022 {
1023    int samples;
1024    int count = opus_packet_get_nb_frames(packet, len);
1025 
1026    if (count<0)
1027       return count;
1028 
1029    samples = count*opus_packet_get_samples_per_frame(packet, Fs);
1030    /* Can't have more than 120 ms */
1031    if (samples*25 > Fs*3)
1032       return OPUS_INVALID_PACKET;
1033    else
1034       return samples;
1035 }
1036 
opus_decoder_get_nb_samples(const OpusDecoder * dec,const unsigned char packet[],opus_int32 len)1037 int opus_decoder_get_nb_samples(const OpusDecoder *dec,
1038       const unsigned char packet[], opus_int32 len)
1039 {
1040    return opus_packet_get_nb_samples(packet, len, dec->Fs);
1041 }
1042