opus_decoder.c 23.8 KB
Newer Older
1
2
/* Copyright (c) 2010 Xiph.Org Foundation, Skype Limited
   Written by Jean-Marc Valin and Koen Vos */
Jean-Marc Valin's avatar
Jean-Marc Valin committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/*
   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions
   are met:

   - Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.

   - Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.

   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
   A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

32
#include <stdarg.h>
33
#include "celt.h"
34
#include "opus.h"
Jean-Marc Valin's avatar
Jean-Marc Valin committed
35
36
#include "entdec.h"
#include "modes.h"
37
#include "API.h"
38
39
#include "stack_alloc.h"
#include "float_cast.h"
40
#include "opus_private.h"
41
#include "os_support.h"
42
43
#include "structs.h"
#include "define.h"
44

45
46
47
48
struct OpusDecoder {
   int          celt_dec_offset;
   int          silk_dec_offset;
   int          channels;
49
   opus_int32   Fs;          /** Sampling rate (at the API level) */
50
51
52

   /* Everything beyond this point gets cleared on a reset */
#define OPUS_DECODER_RESET_START stream_channels
53
54
   int          stream_channels;

55
56
57
58
59
   int          bandwidth;
   int          mode;
   int          prev_mode;
   int          frame_size;
   int          prev_redundancy;
60

61
   opus_uint32  rangeFinal;
62
63
64
65
66
67
68
69
};

#ifdef FIXED_POINT
static inline opus_int16 SAT16(opus_int32 x) {
    return x > 32767 ? 32767 : x < -32768 ? -32768 : (opus_int16)x;
};
#endif

Jean-Marc Valin's avatar
Jean-Marc Valin committed
70

71
72
int opus_decoder_get_size(int channels)
{
73
74
   int silkDecSizeBytes, celtDecSizeBytes;
   int ret;
75
76
   if (channels<1 || channels > 2)
      return 0;
77
78
79
80
81
82
   ret = silk_Get_Decoder_Size( &silkDecSizeBytes );
   if(ret)
      return 0;
   silkDecSizeBytes = align(silkDecSizeBytes);
   celtDecSizeBytes = celt_decoder_get_size(channels);
   return align(sizeof(OpusDecoder))+silkDecSizeBytes+celtDecSizeBytes;
83
84
}

85
int opus_decoder_init(OpusDecoder *st, opus_int32 Fs, int channels)
Jean-Marc Valin's avatar
Jean-Marc Valin committed
86
{
87
88
89
   void *silk_dec;
   CELTDecoder *celt_dec;
   int ret, silkDecSizeBytes;
90

91
   if((Fs!=48000&&Fs!=24000&&Fs!=16000&&Fs!=12000&&Fs!=8000)||(channels!=1&&channels!=2))
92
      return OPUS_BAD_ARG;
93

94
95
   OPUS_CLEAR((char*)st, opus_decoder_get_size(channels));
   /* Initialize SILK encoder */
96
97
98
   ret = silk_Get_Decoder_Size(&silkDecSizeBytes);
   if(ret)return OPUS_INTERNAL_ERROR;

99
100
101
102
103
104
105
106
107
108
109
   silkDecSizeBytes = align(silkDecSizeBytes);
   st->silk_dec_offset = align(sizeof(OpusDecoder));
   st->celt_dec_offset = st->silk_dec_offset+silkDecSizeBytes;
   silk_dec = (char*)st+st->silk_dec_offset;
   celt_dec = (CELTDecoder*)((char*)st+st->celt_dec_offset);
   st->stream_channels = st->channels = channels;

   st->Fs = Fs;

   /* Reset decoder */
   ret = silk_InitDecoder( silk_dec );
110
   if(ret)return OPUS_INTERNAL_ERROR;
Jean-Marc Valin's avatar
Jean-Marc Valin committed
111

112
113
   /* Initialize CELT decoder */
   ret = celt_decoder_init(celt_dec, Fs, channels);
114
115
   if(ret!=OPUS_OK)return OPUS_INTERNAL_ERROR;

116
   celt_decoder_ctl(celt_dec, CELT_SET_SIGNALLING(0));
Jean-Marc Valin's avatar
Jean-Marc Valin committed
117

118
119
120
   st->prev_mode = 0;
   st->frame_size = Fs/400;
   return OPUS_OK;
121
122
}

123
OpusDecoder *opus_decoder_create(opus_int32 Fs, int channels, int *error)
124
{
125
   int ret;
126
127
128
129
130
131
   if((Fs!=48000&&Fs!=24000&&Fs!=16000&&Fs!=12000&&Fs!=8000)||(channels!=1&&channels!=2))
   {
      if (error)
         *error = OPUS_BAD_ARG;
      return NULL;
   }
132
133
   OpusDecoder *st = (OpusDecoder *)opus_alloc(opus_decoder_get_size(channels));
   if (st == NULL)
134
135
136
137
138
   {
      if (error)
         *error = OPUS_ALLOC_FAIL;
      return NULL;
   }
139
   ret = opus_decoder_init(st, Fs, channels);
140
141
   if (error)
      *error = ret;
142
143
   if (ret != OPUS_OK)
   {
144
145
      opus_free(st);
      st = NULL;
146
   }
147
   return st;
Jean-Marc Valin's avatar
Jean-Marc Valin committed
148
}
Koen Vos's avatar
Koen Vos committed
149

150
static void smooth_fade(const opus_val16 *in1, const opus_val16 *in2, opus_val16 *out,
151
        int overlap, int channels, const opus_val16 *window, opus_int32 Fs)
152
153
{
	int i, c;
154
	int inc = 48000/Fs;
155
156
157
	for (c=0;c<channels;c++)
	{
		for (i=0;i<overlap;i++)
158
		{
159
		    opus_val16 w = MULT16_16_Q15(window[i*inc], window[i*inc]);
160
161
162
		    out[i*channels+c] = SHR32(MAC16_16(MULT16_16(w,in2[i*channels+c]),
		            Q15ONE-w, in1[i*channels+c]), 15);
		}
163
164
165
	}
}

166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
static int opus_packet_get_mode(const unsigned char *data)
{
	int mode;
    if (data[0]&0x80)
    {
        mode = MODE_CELT_ONLY;
    } else if ((data[0]&0x60) == 0x60)
    {
        mode = MODE_HYBRID;
    } else {

        mode = MODE_SILK_ONLY;
    }
    return mode;
}

182
static int opus_decode_frame(OpusDecoder *st, const unsigned char *data,
183
		int len, opus_val16 *pcm, int frame_size, int decode_fec)
Jean-Marc Valin's avatar
Jean-Marc Valin committed
184
{
185
186
187
188
    void *silk_dec;
    CELTDecoder *celt_dec;
    int i, silk_ret=0, celt_ret=0;
    ec_dec dec;
189
    silk_DecControlStruct DecControl;
190
    opus_int32 silk_frame_size;
191
192
    VARDECL(opus_int16, pcm_silk);
    VARDECL(opus_val16, pcm_transition);
193
    VARDECL(opus_val16, redundant_audio);
194

195
    int audiosize;
196
197
    int mode;
    int transition=0;
198
    int start_band;
199
    int redundancy=0;
200
201
202
    int redundancy_bytes = 0;
    int celt_to_silk=0;
    int c;
203
    int F2_5, F5, F10, F20;
204
    const opus_val16 *window;
205
    opus_uint32 redundant_rng = 0;
206
    ALLOC_STACK;
Jean-Marc Valin's avatar
Jean-Marc Valin committed
207

208
209
    silk_dec = (char*)st+st->silk_dec_offset;
    celt_dec = (CELTDecoder*)((char*)st+st->celt_dec_offset);
210
211
    F20 = st->Fs/50;
    F10 = F20>>1;
Jean-Marc Valin's avatar
Jean-Marc Valin committed
212
213
    F5 = F10>>1;
    F2_5 = F5>>1;
214
215
    if (frame_size < F2_5)
       return OPUS_BUFFER_TOO_SMALL;
216
    /* Payloads of 1 (2 including ToC) or 0 trigger the PLC/DTX */
217
    if (len<=1)
218
    {
219
    	data = NULL;
220
221
222
    	/* In that case, don't conceal more than what the ToC says */
    	frame_size = IMIN(frame_size, st->frame_size);
    }
Jean-Marc Valin's avatar
Jean-Marc Valin committed
223
224
    if (data != NULL)
    {
225
    	audiosize = st->frame_size;
226
    	mode = st->mode;
227
        ec_dec_init(&dec,(unsigned char*)data,len);
Jean-Marc Valin's avatar
PLC fix    
Jean-Marc Valin committed
228
    } else {
229
    	audiosize = frame_size;
230

231
232
233
    	if (st->prev_mode == 0)
    	{
    		/* If we haven't got any packet yet, all we can do is return zeros */
234
    		for (i=0;i<audiosize*st->channels;i++)
235
    			pcm[i] = 0;
236
    		RESTORE_STACK;
237
238
239
240
    		return audiosize;
    	} else {
    		mode = st->prev_mode;
    	}
Jean-Marc Valin's avatar
Jean-Marc Valin committed
241
    }
Jean-Marc Valin's avatar
Jean-Marc Valin committed
242

243
244
    ALLOC(pcm_transition, F5*st->channels, opus_val16);

245
    if (data!=NULL && !st->prev_redundancy && mode != st->prev_mode && st->prev_mode > 0
246
247
248
249
    		&& !(mode == MODE_SILK_ONLY && st->prev_mode == MODE_HYBRID)
    		&& !(mode == MODE_HYBRID && st->prev_mode == MODE_SILK_ONLY))
    {
    	transition = 1;
250
    	if (mode == MODE_CELT_ONLY)
251
    	    opus_decode_frame(st, NULL, 0, pcm_transition, IMIN(F5, audiosize), 0);
252
    }
253
254
    if (audiosize > frame_size)
    {
255
        /*fprintf(stderr, "PCM buffer too small: %d vs %d (mode = %d)\n", audiosize, frame_size, mode);*/
256
        RESTORE_STACK;
Jean-Marc Valin's avatar
Jean-Marc Valin committed
257
        return OPUS_BAD_ARG;
258
259
260
261
    } else {
        frame_size = audiosize;
    }

262
263
264
    ALLOC(pcm_silk, frame_size*st->channels, opus_int16);
    ALLOC(redundant_audio, F5*st->channels, opus_val16);

Jean-Marc Valin's avatar
PLC fix    
Jean-Marc Valin committed
265
    /* SILK processing */
266
    if (mode != MODE_CELT_ONLY)
267
    {
Koen Vos's avatar
Koen Vos committed
268
        int lost_flag, decoded_samples;
269
        opus_int16 *pcm_ptr = pcm_silk;
270
271

        if (st->prev_mode==MODE_CELT_ONLY)
272
        	silk_InitDecoder( silk_dec );
273

274
        DecControl.API_sampleRate = st->Fs;
275
276
        DecControl.nChannelsAPI      = st->channels;
        DecControl.nChannelsInternal = st->stream_channels;
277
        DecControl.payloadSize_ms = 1000 * audiosize / st->Fs;
278
        if( mode == MODE_SILK_ONLY ) {
279
            if( st->bandwidth == OPUS_BANDWIDTH_NARROWBAND ) {
280
                DecControl.internalSampleRate = 8000;
281
            } else if( st->bandwidth == OPUS_BANDWIDTH_MEDIUMBAND ) {
282
                DecControl.internalSampleRate = 12000;
283
            } else if( st->bandwidth == OPUS_BANDWIDTH_WIDEBAND ) {
284
285
                DecControl.internalSampleRate = 16000;
            } else {
286
            	DecControl.internalSampleRate = 16000;
287
                silk_assert( 0 );
288
289
290
291
292
            }
        } else {
            /* Hybrid mode */
            DecControl.internalSampleRate = 16000;
        }
293

Koen Vos's avatar
Koen Vos committed
294
295
        lost_flag = data == NULL ? 1 : 2 * decode_fec;
        decoded_samples = 0;
Jean-Marc Valin's avatar
Jean-Marc Valin committed
296
297
        do {
            /* Call SILK decoder */
Koen Vos's avatar
Koen Vos committed
298
            int first_frame = decoded_samples == 0;
299
            silk_ret = silk_Decode( silk_dec, &DecControl,
Koen Vos's avatar
Koen Vos committed
300
                lost_flag, first_frame, &dec, pcm_ptr, &silk_frame_size );
Jean-Marc Valin's avatar
Jean-Marc Valin committed
301
            if( silk_ret ) {
302
303
304
305
306
            	if (lost_flag) {
            		/* PLC failure should not be fatal */
            		silk_frame_size = frame_size;
            		for (i=0;i<frame_size*st->channels;i++)
            			pcm_ptr[i] = 0;
307
308
            	} else {
            	   RESTORE_STACK;
309
            	   return OPUS_INVALID_PACKET;
310
            	}
Jean-Marc Valin's avatar
Jean-Marc Valin committed
311
            }
Koen Vos's avatar
Koen Vos committed
312
            pcm_ptr += silk_frame_size * st->channels;
Koen Vos's avatar
Koen Vos committed
313
314
            decoded_samples += silk_frame_size;
        } while( decoded_samples < frame_size );
315
    }
Jean-Marc Valin's avatar
Jean-Marc Valin committed
316

317
    start_band = 0;
318
    if (mode != MODE_CELT_ONLY && data != NULL && ec_tell(&dec)+29+8*(st->mode == MODE_HYBRID) < 8*len)
319
    {
320
321
        /* Check if we have a redundant 0-8 kHz band */
        redundancy = ec_dec_bit_logp(&dec, 12);
322
323
324
        if (redundancy)
        {
            celt_to_silk = ec_dec_bit_logp(&dec, 1);
325
326
            /* redundancy_bytes will be at least two, in the non-hybrid case due to the ec_tell() check above */
            redundancy_bytes = mode==MODE_HYBRID ? (opus_int32)ec_dec_uint(&dec, 256)+2 : len-((ec_tell(&dec)+7)>>3);
327
            len -= redundancy_bytes;
328
329
330
331
332
            if (len<0)
            {
               len=0;
               redundancy_bytes=0;
               redundancy = 0;
333
            }
334
335
336
337
            /* Shrink decoder because of raw bits */
            dec.storage -= redundancy_bytes;
        }
    }
338
339
    if (mode != MODE_CELT_ONLY)
    	start_band = 17;
340
341
342
343
344
345

    {
        int endband=21;

        switch(st->bandwidth)
        {
346
        case OPUS_BANDWIDTH_NARROWBAND:
347
348
            endband = 13;
            break;
349
        case OPUS_BANDWIDTH_MEDIUMBAND:
350
        case OPUS_BANDWIDTH_WIDEBAND:
351
352
            endband = 17;
            break;
353
        case OPUS_BANDWIDTH_SUPERWIDEBAND:
354
355
            endband = 19;
            break;
356
        case OPUS_BANDWIDTH_FULLBAND:
357
358
359
            endband = 21;
            break;
        }
360
361
        celt_decoder_ctl(celt_dec, CELT_SET_END_BAND(endband));
        celt_decoder_ctl(celt_dec, CELT_SET_CHANNELS(st->stream_channels));
362
    }
363

364
365
366
367
    if (redundancy)
        transition = 0;

    if (transition && mode != MODE_CELT_ONLY)
368
        opus_decode_frame(st, NULL, 0, pcm_transition, IMIN(F5, audiosize), 0);
369
370
371
372

    /* 5 ms redundant frame for CELT->SILK*/
    if (redundancy && celt_to_silk)
    {
373
        celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(0));
374
        celt_decode_with_ec(celt_dec, data+len, redundancy_bytes, redundant_audio, F5, NULL);
375
376
        celt_decoder_ctl(celt_dec, OPUS_GET_FINAL_RANGE(&redundant_rng));
        celt_decoder_ctl(celt_dec, OPUS_RESET_STATE);
377
    }
Jean-Marc Valin's avatar
Jean-Marc Valin committed
378

379
    /* MUST be after PLC */
380
    celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(start_band));
381

382
    if (transition)
383
    	celt_decoder_ctl(celt_dec, OPUS_RESET_STATE);
384

385
    if (mode != MODE_SILK_ONLY)
386
    {
387
    	int celt_frame_size = IMIN(F20, frame_size);
Jean-Marc Valin's avatar
PLC fix    
Jean-Marc Valin committed
388
        /* Decode CELT */
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
        celt_ret = celt_decode_with_ec(celt_dec, decode_fec?NULL:data, len, pcm, celt_frame_size, &dec);
    } else {
       for (i=0;i<frame_size*st->channels;i++)
          pcm[i] = 0;
    }

    if (mode != MODE_CELT_ONLY)
    {
#ifdef FIXED_POINT
        for (i=0;i<frame_size*st->channels;i++)
            pcm[i] = SAT16(pcm[i] + pcm_silk[i]);
#else
        for (i=0;i<frame_size*st->channels;i++)
            pcm[i] = pcm[i] + (1./32768.)*pcm_silk[i];
#endif
404
    }
Koen Vos's avatar
Koen Vos committed
405

406
407
    {
        const CELTMode *celt_mode;
408
        celt_decoder_ctl(celt_dec, CELT_GET_MODE(&celt_mode));
409
410
        window = celt_mode->window;
    }
Koen Vos's avatar
Koen Vos committed
411

412
413
414
    /* 5 ms redundant frame for SILK->CELT */
    if (redundancy && !celt_to_silk)
    {
415
        celt_decoder_ctl(celt_dec, OPUS_RESET_STATE);
416
        celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(0));
417

418
        celt_decode_with_ec(celt_dec, data+len, redundancy_bytes, redundant_audio, F5, NULL);
419
        celt_decoder_ctl(celt_dec, OPUS_GET_FINAL_RANGE(&redundant_rng));
Jean-Marc Valin's avatar
Jean-Marc Valin committed
420
        smooth_fade(pcm+st->channels*(frame_size-F2_5), redundant_audio+st->channels*F2_5,
421
        		pcm+st->channels*(frame_size-F2_5), F2_5, st->channels, window, st->Fs);
422
423
424
425
426
    }
    if (redundancy && celt_to_silk)
    {
        for (c=0;c<st->channels;c++)
        {
Jean-Marc Valin's avatar
Jean-Marc Valin committed
427
            for (i=0;i<F2_5;i++)
428
                pcm[st->channels*i+c] = redundant_audio[st->channels*i+c];
429
        }
430
431
        smooth_fade(redundant_audio+st->channels*F2_5, pcm+st->channels*F2_5,
                pcm+st->channels*F2_5, F2_5, st->channels, window, st->Fs);
432
    }
433
434
    if (transition)
    {
435
    	for (i=0;i<st->channels*F2_5;i++)
436
    		pcm[i] = pcm_transition[i];
437
    	if (audiosize >= F5)
438
439
    	    smooth_fade(pcm_transition+st->channels*F2_5, pcm+st->channels*F2_5,
    	            pcm+st->channels*F2_5, F2_5,
440
    	            st->channels, window, st->Fs);
441
    }
442

443
444
445
446
    if (len <= 1)
       st->rangeFinal = 0;
    else
       st->rangeFinal = dec.rng ^ redundant_rng;
Koen Vos's avatar
Koen Vos committed
447

448
    st->prev_mode = mode;
449
    st->prev_redundancy = redundancy;
450
    RESTORE_STACK;
451
	return celt_ret<0 ? celt_ret : audiosize;
Jean-Marc Valin's avatar
Jean-Marc Valin committed
452
453
454

}

Jean-Marc Valin's avatar
Jean-Marc Valin committed
455
static int parse_size(const unsigned char *data, int len, short *size)
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
{
	if (len<1)
	{
		*size = -1;
		return -1;
	} else if (data[0]<252)
	{
		*size = data[0];
		return 1;
	} else if (len<2)
	{
		*size = -1;
		return -1;
	} else {
		*size = 4*data[1] + data[0];
		return 2;
	}
}

475
476
477
static int opus_packet_parse_impl(const unsigned char *data, int len,
      int self_delimited, unsigned char *out_toc,
      const unsigned char *frames[48], short size[48], int *payload_offset)
478
479
480
{
   int i, bytes;
   int count;
481
   int cbr;
482
483
   unsigned char ch, toc;
   int framesize;
484
   int last_size;
485
   const unsigned char *data0 = data;
486
487
488
489
490
491

   if (size==NULL)
      return OPUS_BAD_ARG;

   framesize = opus_packet_get_samples_per_frame(data, 48000);

492
   cbr = 0;
493
494
   toc = *data++;
   len--;
495
   last_size = len;
496
497
498
499
500
501
502
503
504
   switch (toc&0x3)
   {
   /* One frame */
   case 0:
      count=1;
      break;
      /* Two CBR frames */
   case 1:
      count=2;
505
506
507
508
      cbr = 1;
      if (!self_delimited)
      {
         if (len&0x1)
509
            return OPUS_INVALID_PACKET;
510
511
         size[0] = last_size = len/2;
      }
512
513
514
515
516
517
518
      break;
      /* Two VBR frames */
   case 2:
      count = 2;
      bytes = parse_size(data, len, size);
      len -= bytes;
      if (size[0]<0 || size[0] > len)
519
         return OPUS_INVALID_PACKET;
520
      data += bytes;
521
      last_size = len-size[0];
522
523
524
525
      break;
      /* Multiple CBR/VBR frames (from 0 to 120 ms) */
   case 3:
      if (len<1)
526
         return OPUS_INVALID_PACKET;
527
528
529
530
      /* Number of frames encoded in bits 0 to 5 */
      ch = *data++;
      count = ch&0x3F;
      if (count <= 0 || framesize*count > 5760)
531
          return OPUS_INVALID_PACKET;
532
533
534
535
536
537
538
539
      len--;
      /* Padding flag is bit 6 */
      if (ch&0x40)
      {
         int padding=0;
         int p;
         do {
            if (len<=0)
540
               return OPUS_INVALID_PACKET;
541
542
543
544
545
546
547
            p = *data++;
            len--;
            padding += p==255 ? 254: p;
         } while (p==255);
         len -= padding;
      }
      if (len<0)
548
         return OPUS_INVALID_PACKET;
549
      /* VBR flag is bit 7 */
550
      cbr = !(ch&0x80);
551
      if (!cbr)
552
553
      {
         /* VBR case */
554
         last_size = len;
555
556
557
558
559
         for (i=0;i<count-1;i++)
         {
            bytes = parse_size(data, len, size+i);
            len -= bytes;
            if (size[i]<0 || size[i] > len)
560
               return OPUS_INVALID_PACKET;
561
562
563
564
            data += bytes;
            last_size -= bytes+size[i];
         }
         if (last_size<0)
565
            return OPUS_INVALID_PACKET;
566
567
      } else if (!self_delimited)
      {
568
         /* CBR case */
569
570
         last_size = len/count;
         if (last_size*count!=len)
571
            return OPUS_INVALID_PACKET;
572
573
         for (i=0;i<count-1;i++)
            size[i] = last_size;
574
575
576
      }
      break;
   }
577
578
579
580
581
582
   /* Self-delimited framing has an extra size for the last frame. */
   if (self_delimited)
   {
      bytes = parse_size(data, len, size+count-1);
      len -= bytes;
      if (size[count-1]<0 || size[count-1] > len)
583
         return OPUS_INVALID_PACKET;
584
585
586
587
588
      data += bytes;
      /* For CBR packets, apply the size to all the frames. */
      if (cbr)
      {
         if (size[count-1]*count > len)
589
            return OPUS_INVALID_PACKET;
590
591
592
         for (i=0;i<count-1;i++)
            size[i] = size[count-1];
      } else if(size[count-1] > last_size)
593
         return OPUS_INVALID_PACKET;
594
595
596
597
598
599
600
   } else
   {
      /* Because it's not encoded explicitly, it's possible the size of the
          last packet (or all the packets, for the CBR case) is larger than
          1275.
         Reject them here.*/
      if (last_size > 1275)
601
        return OPUS_INVALID_PACKET;
602
603
      size[count-1] = last_size;
   }
604
605
606
607
608
609
610
611
612
613
614
615
616

   if (frames)
   {
      for (i=0;i<count;i++)
      {
         frames[i] = data;
         data += size[i];
      }
   }

   if (out_toc)
      *out_toc = toc;

617
618
   if (payload_offset)
      *payload_offset = data-data0;
619
620
621
622

   return count;
}

623
624
625
626
627
628
629
630
int opus_packet_parse(const unsigned char *data, int len,
      unsigned char *out_toc, const unsigned char *frames[48],
      short size[48], int *payload_offset)
{
   return opus_packet_parse_impl(data, len, 0,
         out_toc, frames, size, payload_offset);
}

631
632
int opus_decode_native(OpusDecoder *st, const unsigned char *data,
      int len, opus_val16 *pcm, int frame_size, int decode_fec, int self_delimited, int *packet_offset)
633
{
634
	int i, nb_samples;
635
	int count, offset;
636
	unsigned char toc;
637
	int tot_offset;
638
639
	/* 48 x 2.5 ms = 120 ms */
	short size[48];
640
	if (decode_fec<0 || decode_fec>1)return OPUS_BAD_ARG;
641
642
643
	if (len==0 || data==NULL)
	    return opus_decode_frame(st, NULL, 0, pcm, frame_size, 0);
	else if (len<0)
644
		return OPUS_BAD_ARG;
645
646

	tot_offset = 0;
647
648
649
650
	st->mode = opus_packet_get_mode(data);
	st->bandwidth = opus_packet_get_bandwidth(data);
	st->frame_size = opus_packet_get_samples_per_frame(data, st->Fs);
	st->stream_channels = opus_packet_get_nb_channels(data);
651

652
	count = opus_packet_parse_impl(data, len, self_delimited, &toc, NULL, size, &offset);
653
654
655
	if (count < 0)
	   return count;

656
	data += offset;
657
658
	tot_offset += offset;

Jean-Marc Valin's avatar
Jean-Marc Valin committed
659
	if (count*st->frame_size > frame_size)
660
		return OPUS_BUFFER_TOO_SMALL;
661
662
663
664
	nb_samples=0;
	for (i=0;i<count;i++)
	{
		int ret;
665
		ret = opus_decode_frame(st, data, size[i], pcm, frame_size-nb_samples, decode_fec);
666
667
668
		if (ret<0)
			return ret;
		data += size[i];
669
		tot_offset += size[i];
670
		pcm += ret*st->channels;
671
672
		nb_samples += ret;
	}
673
674
	if (packet_offset != NULL)
	   *packet_offset = tot_offset;
675
676
	return nb_samples;
}
677

678
679
#ifdef FIXED_POINT

680
681
682
683
684
685
int opus_decode(OpusDecoder *st, const unsigned char *data,
      int len, opus_val16 *pcm, int frame_size, int decode_fec)
{
   return opus_decode_native(st, data, len, pcm, frame_size, decode_fec, 0, NULL);
}

686
687
688
689
690
691
692
693
694
695
#ifndef DISABLE_FLOAT_API
int opus_decode_float(OpusDecoder *st, const unsigned char *data,
      int len, float *pcm, int frame_size, int decode_fec)
{
   VARDECL(opus_int16, out);
   int ret, i;
   ALLOC_STACK;

   ALLOC(out, frame_size*st->channels, opus_int16);

696
   ret = opus_decode_native(st, data, len, out, frame_size, decode_fec, 0, NULL);
697
698
699
700
701
702
703
704
705
706
   if (ret > 0)
   {
      for (i=0;i<ret*st->channels;i++)
         pcm[i] = (1./32768.)*(out[i]);
   }
   RESTORE_STACK;
   return ret;
}
#endif

707

708
709
710
711
712
713
714
715
716
717
#else
int opus_decode(OpusDecoder *st, const unsigned char *data,
      int len, opus_int16 *pcm, int frame_size, int decode_fec)
{
   VARDECL(float, out);
   int ret, i;
   ALLOC_STACK;

   ALLOC(out, frame_size*st->channels, float);

718
   ret = opus_decode_native(st, data, len, out, frame_size, decode_fec, 0, NULL);
719
720
721
722
723
724
725
726
   if (ret > 0)
   {
      for (i=0;i<ret*st->channels;i++)
         pcm[i] = FLOAT2INT16(out[i]);
   }
   RESTORE_STACK;
   return ret;
}
727
728
729
730
731
732
733

int opus_decode_float(OpusDecoder *st, const unsigned char *data,
        int len, opus_val16 *pcm, int frame_size, int decode_fec)
{
   return opus_decode_native(st, data, len, pcm, frame_size, decode_fec, 0, NULL);
}

734
#endif
735

736
int opus_decoder_ctl(OpusDecoder *st, int request, ...)
737
{
738
739
   int ret = OPUS_OK;
   va_list ap;
740
741
742
743
744
745
   void *silk_dec;
   CELTDecoder *celt_dec;

   silk_dec = (char*)st+st->silk_dec_offset;
   celt_dec = (CELTDecoder*)((char*)st+st->celt_dec_offset);

746

747
   va_start(ap, request);
748

749
750
751
752
753
754
755
756
757
758
759
760
761
762
   switch (request)
   {
   case OPUS_GET_BANDWIDTH_REQUEST:
   {
      opus_int32 *value = va_arg(ap, opus_int32*);
      *value = st->bandwidth;
   }
   break;
   case OPUS_GET_FINAL_RANGE_REQUEST:
   {
      opus_uint32 *value = va_arg(ap, opus_uint32*);
      *value = st->rangeFinal;
   }
   break;
763
764
765
   case OPUS_RESET_STATE:
   {
      OPUS_CLEAR((char*)&st->OPUS_DECODER_RESET_START,
766
            sizeof(OpusDecoder)-
767
768
            ((char*)&st->OPUS_DECODER_RESET_START - (char*)st));

769
      celt_decoder_ctl(celt_dec, OPUS_RESET_STATE);
770
771
772
773
774
      silk_InitDecoder( silk_dec );
      st->stream_channels = st->channels;
      st->frame_size = st->Fs/400;
   }
   break;
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
   case OPUS_GET_PITCH_REQUEST:
   {
      int *value = va_arg(ap, opus_int32*);
      if (value==NULL)
      {
         ret = OPUS_BAD_ARG;
         break;
      }
      if (st->prev_mode == MODE_CELT_ONLY)
         celt_decoder_ctl(celt_dec, OPUS_GET_PITCH(value));
      else
         *value = ((silk_decoder_state*)silk_dec)->indices.signalType == TYPE_VOICED
         ? ((silk_decoder_state*)silk_dec)->lagPrev*48/((silk_decoder_state*)silk_dec)->fs_kHz
         : 0;
   }
   break;
791
792
   default:
      /*fprintf(stderr, "unknown opus_decoder_ctl() request: %d", request);*/
793
      ret = OPUS_UNIMPLEMENTED;
794
795
      break;
   }
Alfred E. Heggestad's avatar
Alfred E. Heggestad committed
796

797
798
   va_end(ap);
   return ret;
799
800
}

Jean-Marc Valin's avatar
Jean-Marc Valin committed
801
void opus_decoder_destroy(OpusDecoder *st)
Jean-Marc Valin's avatar
Jean-Marc Valin committed
802
{
803
	opus_free(st);
Jean-Marc Valin's avatar
Jean-Marc Valin committed
804
}
Koen Vos's avatar
Koen Vos committed
805

806
807
808
809
810
811

int opus_packet_get_bandwidth(const unsigned char *data)
{
	int bandwidth;
    if (data[0]&0x80)
    {
812
813
814
        bandwidth = OPUS_BANDWIDTH_MEDIUMBAND + ((data[0]>>5)&0x3);
        if (bandwidth == OPUS_BANDWIDTH_MEDIUMBAND)
            bandwidth = OPUS_BANDWIDTH_NARROWBAND;
815
816
    } else if ((data[0]&0x60) == 0x60)
    {
817
        bandwidth = (data[0]&0x10) ? OPUS_BANDWIDTH_FULLBAND : OPUS_BANDWIDTH_SUPERWIDEBAND;
818
819
    } else {

820
        bandwidth = OPUS_BANDWIDTH_NARROWBAND + ((data[0]>>5)&0x3);
821
822
823
824
    }
    return bandwidth;
}

825
int opus_packet_get_samples_per_frame(const unsigned char *data, opus_int32 Fs)
826
{
827
    int audiosize;
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
    if (data[0]&0x80)
    {
        audiosize = ((data[0]>>3)&0x3);
        audiosize = (Fs<<audiosize)/400;
    } else if ((data[0]&0x60) == 0x60)
    {
        audiosize = (data[0]&0x08) ? Fs/50 : Fs/100;
    } else {

        audiosize = ((data[0]>>3)&0x3);
        if (audiosize == 3)
            audiosize = Fs*60/1000;
        else
            audiosize = (Fs<<audiosize)/100;
    }
    return audiosize;
}

int opus_packet_get_nb_channels(const unsigned char *data)
{
    return (data[0]&0x4) ? 2 : 1;
}

int opus_packet_get_nb_frames(const unsigned char packet[], int len)
{
	int count;
	if (len<1)
		return OPUS_BAD_ARG;
	count = packet[0]&0x3;
	if (count==0)
		return 1;
	else if (count!=3)
		return 2;
	else if (len<2)
862
		return OPUS_INVALID_PACKET;
863
864
865
866
867
868
869
870
871
872
873
	else
		return packet[1]&0x3F;
}

int opus_decoder_get_nb_samples(const OpusDecoder *dec, const unsigned char packet[], int len)
{
	int samples;
	int count = opus_packet_get_nb_frames(packet, len);
	samples = count*opus_packet_get_samples_per_frame(packet, dec->Fs);
	/* Can't have more than 120 ms */
	if (samples*25 > dec->Fs*3)
874
		return OPUS_INVALID_PACKET;
875
876
877
	else
		return samples;
}