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