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