blob: 5b56ac1f601cde84581acfa1cc18028ec639d0d4 [file] [log] [blame]
Jean-Marc Valinfbaabc12010-07-05 15:47:04 -04001/* Copyright (c) 2010 Xiph.Org Foundation, Skype Limited
2 Written by Jean-Marc Valin and Koen Vos */
Jean-Marc Valin04584ea2010-06-30 15:03:35 -04003/*
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
Jean-Marc Valin04584ea2010-06-30 15:03:35 -040015 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
Jean-Marc Valincb05e7c2012-04-20 16:40:24 -040018 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
19 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
Jean-Marc Valin04584ea2010-06-30 15:03:35 -040020 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
Gregory Maxwell936f52c2012-05-13 11:40:19 -040032#ifndef OPUS_BUILD
Ralph Giles799b1702012-11-29 11:40:04 -080033#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."
Gregory Maxwell936f52c2012-05-13 11:40:19 -040034#endif
35
Jean-Marc Valin24f36e02010-07-06 14:41:20 -040036#include <stdarg.h>
Jean-Marc Valin8ea67042011-03-11 17:49:10 -050037#include "celt.h"
Jean-Marc Valind9920f32011-08-19 13:00:49 -040038#include "opus.h"
Jean-Marc Valina0cbeca2010-07-08 11:27:20 -040039#include "entdec.h"
40#include "modes.h"
Jean-Marc Valin1c2f5632011-09-16 01:16:53 -070041#include "API.h"
Jean-Marc Valin222494f2011-08-17 15:53:37 -040042#include "stack_alloc.h"
43#include "float_cast.h"
Jean-Marc Valin6696a142011-08-22 10:40:38 -040044#include "opus_private.h"
Jean-Marc Valin07f88402011-08-29 15:08:51 -040045#include "os_support.h"
Jean-Marc Valin1c2f5632011-09-16 01:16:53 -070046#include "structs.h"
47#include "define.h"
Gregory Maxwell28b41ae2012-07-11 00:04:24 -040048#include "mathops.h"
Aurélien Zanellicd4c8242013-05-31 15:07:00 +020049#include "cpu_support.h"
Jean-Marc Valin222494f2011-08-17 15:53:37 -040050
Jean-Marc Valind9920f32011-08-19 13:00:49 -040051struct OpusDecoder {
52 int celt_dec_offset;
53 int silk_dec_offset;
54 int channels;
Gregory Maxwell64a35412011-09-02 10:31:17 -040055 opus_int32 Fs; /** Sampling rate (at the API level) */
Jean-Marc Valin0c5085a2011-10-04 01:10:32 -040056 silk_DecControlStruct DecControl;
Gregory Maxwell03105f52012-07-11 02:33:55 -040057 int decode_gain;
Jean-Marc Valin04465632011-08-30 18:01:06 -040058
59 /* Everything beyond this point gets cleared on a reset */
60#define OPUS_DECODER_RESET_START stream_channels
Jean-Marc Valind9920f32011-08-19 13:00:49 -040061 int stream_channels;
62
Jean-Marc Valin04465632011-08-30 18:01:06 -040063 int bandwidth;
64 int mode;
65 int prev_mode;
66 int frame_size;
67 int prev_redundancy;
Jean-Marc Valin512d8492012-12-04 14:13:46 -050068 int last_packet_duration;
Jean-Marc Valin32c4a0c2013-03-01 15:18:23 -050069#ifndef FIXED_POINT
70 opus_val16 softclip_mem[2];
71#endif
Jean-Marc Valind9920f32011-08-19 13:00:49 -040072
Gregory Maxwell64a35412011-09-02 10:31:17 -040073 opus_uint32 rangeFinal;
Aurélien Zanellicd4c8242013-05-31 15:07:00 +020074 int arch;
Jean-Marc Valind9920f32011-08-19 13:00:49 -040075};
76
77#ifdef FIXED_POINT
78static inline opus_int16 SAT16(opus_int32 x) {
Ralph Gilesda025d52011-10-26 20:24:49 -070079 return x > 32767 ? 32767 : x < -32768 ? -32768 : (opus_int16)x;
Jean-Marc Valin14d63d12012-05-16 17:47:17 -040080}
Jean-Marc Valind9920f32011-08-19 13:00:49 -040081#endif
82
Jean-Marc Valin04584ea2010-06-30 15:03:35 -040083
Jean-Marc Valin280c0602011-05-05 20:47:42 -040084int opus_decoder_get_size(int channels)
85{
Gregory Maxwell64a35412011-09-02 10:31:17 -040086 int silkDecSizeBytes, celtDecSizeBytes;
87 int ret;
Gregory Maxwellf451b332011-09-04 10:47:15 -040088 if (channels<1 || channels > 2)
89 return 0;
Gregory Maxwell64a35412011-09-02 10:31:17 -040090 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;
Jean-Marc Valin280c0602011-05-05 20:47:42 -040096}
97
Gregory Maxwell64a35412011-09-02 10:31:17 -040098int opus_decoder_init(OpusDecoder *st, opus_int32 Fs, int channels)
Jean-Marc Valin04584ea2010-06-30 15:03:35 -040099{
Gregory Maxwell64a35412011-09-02 10:31:17 -0400100 void *silk_dec;
101 CELTDecoder *celt_dec;
102 int ret, silkDecSizeBytes;
Jean-Marc Valin04584ea2010-06-30 15:03:35 -0400103
Ralph Gilesda025d52011-10-26 20:24:49 -0700104 if ((Fs!=48000&&Fs!=24000&&Fs!=16000&&Fs!=12000&&Fs!=8000)
105 || (channels!=1&&channels!=2))
Gregory Maxwell64a35412011-09-02 10:31:17 -0400106 return OPUS_BAD_ARG;
Gregory Maxwell220a7d42011-10-01 20:30:16 -0400107
Gregory Maxwell64a35412011-09-02 10:31:17 -0400108 OPUS_CLEAR((char*)st, opus_decoder_get_size(channels));
109 /* Initialize SILK encoder */
Gregory Maxwelle03af442011-09-04 04:43:11 -0400110 ret = silk_Get_Decoder_Size(&silkDecSizeBytes);
Ralph Gilesda025d52011-10-26 20:24:49 -0700111 if (ret)
112 return OPUS_INTERNAL_ERROR;
Gregory Maxwelle03af442011-09-04 04:43:11 -0400113
Gregory Maxwell64a35412011-09-02 10:31:17 -0400114 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;
Jean-Marc Valin2e8a55c2010-08-31 11:31:54 -0400120
Gregory Maxwell64a35412011-09-02 10:31:17 -0400121 st->Fs = Fs;
Jean-Marc Valin0c5085a2011-10-04 01:10:32 -0400122 st->DecControl.API_sampleRate = st->Fs;
123 st->DecControl.nChannelsAPI = st->channels;
Aurélien Zanellicd4c8242013-05-31 15:07:00 +0200124 st->arch = opus_select_arch();
Jean-Marc Valin62dda622010-07-05 11:35:40 -0400125
Gregory Maxwell64a35412011-09-02 10:31:17 -0400126 /* Reset decoder */
127 ret = silk_InitDecoder( silk_dec );
Gregory Maxwelle03af442011-09-04 04:43:11 -0400128 if(ret)return OPUS_INTERNAL_ERROR;
Jean-Marc Valin04584ea2010-06-30 15:03:35 -0400129
Gregory Maxwell64a35412011-09-02 10:31:17 -0400130 /* Initialize CELT decoder */
131 ret = celt_decoder_init(celt_dec, Fs, channels);
Gregory Maxwelle03af442011-09-04 04:43:11 -0400132 if(ret!=OPUS_OK)return OPUS_INTERNAL_ERROR;
133
Gregory Maxwell64a35412011-09-02 10:31:17 -0400134 celt_decoder_ctl(celt_dec, CELT_SET_SIGNALLING(0));
Jean-Marc Valin04584ea2010-06-30 15:03:35 -0400135
Gregory Maxwell64a35412011-09-02 10:31:17 -0400136 st->prev_mode = 0;
137 st->frame_size = Fs/400;
138 return OPUS_OK;
Jean-Marc Valin280c0602011-05-05 20:47:42 -0400139}
140
Gregory Maxwell64a35412011-09-02 10:31:17 -0400141OpusDecoder *opus_decoder_create(opus_int32 Fs, int channels, int *error)
Jean-Marc Valin280c0602011-05-05 20:47:42 -0400142{
Jean-Marc Valin9d8dc3a2011-08-29 09:40:57 -0400143 int ret;
Gregory Maxwelle98f1f52011-10-05 01:59:28 -0400144 OpusDecoder *st;
Ralph Gilesda025d52011-10-26 20:24:49 -0700145 if ((Fs!=48000&&Fs!=24000&&Fs!=16000&&Fs!=12000&&Fs!=8000)
146 || (channels!=1&&channels!=2))
Gregory Maxwell220a7d42011-10-01 20:30:16 -0400147 {
148 if (error)
149 *error = OPUS_BAD_ARG;
150 return NULL;
151 }
Gregory Maxwelle98f1f52011-10-05 01:59:28 -0400152 st = (OpusDecoder *)opus_alloc(opus_decoder_get_size(channels));
Jean-Marc Valin85b8e622011-08-29 16:10:08 -0400153 if (st == NULL)
Jean-Marc Valin9d8dc3a2011-08-29 09:40:57 -0400154 {
155 if (error)
156 *error = OPUS_ALLOC_FAIL;
157 return NULL;
158 }
Jean-Marc Valin85b8e622011-08-29 16:10:08 -0400159 ret = opus_decoder_init(st, Fs, channels);
Gregory Maxwella40721a2011-09-04 04:25:12 -0400160 if (error)
161 *error = ret;
Jean-Marc Valin9d8dc3a2011-08-29 09:40:57 -0400162 if (ret != OPUS_OK)
163 {
Jean-Marc Valin85b8e622011-08-29 16:10:08 -0400164 opus_free(st);
165 st = NULL;
Jean-Marc Valin9d8dc3a2011-08-29 09:40:57 -0400166 }
Jean-Marc Valin85b8e622011-08-29 16:10:08 -0400167 return st;
Jean-Marc Valin04584ea2010-06-30 15:03:35 -0400168}
Koen Vos8f67b202011-02-03 09:31:12 -0500169
Ralph Gilesda025d52011-10-26 20:24:49 -0700170static 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)
Jean-Marc Valin0c0c5f92011-03-07 20:54:33 -0500173{
Ralph Gilesda025d52011-10-26 20:24:49 -0700174 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 }
Jean-Marc Valin0c0c5f92011-03-07 20:54:33 -0500185}
186
Jean-Marc Valin0fe40782011-03-13 12:41:08 -0400187static int opus_packet_get_mode(const unsigned char *data)
188{
Ralph Gilesda025d52011-10-26 20:24:49 -0700189 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;
Jean-Marc Valin0fe40782011-03-13 12:41:08 -0400200}
201
Jean-Marc Valina7d31b72011-03-13 20:41:52 -0400202static int opus_decode_frame(OpusDecoder *st, const unsigned char *data,
Gregory Maxwelle7028172011-11-19 23:58:09 -0500203 opus_int32 len, opus_val16 *pcm, int frame_size, int decode_fec)
Jean-Marc Valin04584ea2010-06-30 15:03:35 -0400204{
Ralph Gilesda025d52011-10-26 20:24:49 -0700205 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;
Jean-Marc Valincc83f6b2012-11-05 10:25:20 -0500210 int pcm_silk_size;
Ralph Gilesda025d52011-10-26 20:24:49 -0700211 VARDECL(opus_int16, pcm_silk);
Jean-Marc Valincc83f6b2012-11-05 10:25:20 -0500212 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;
Ralph Gilesda025d52011-10-26 20:24:49 -0700218 VARDECL(opus_val16, redundant_audio);
Jean-Marc Valina0653ed2011-07-05 13:18:59 -0400219
Ralph Gilesda025d52011-10-26 20:24:49 -0700220 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;
Jean-Marc Valin04584ea2010-06-30 15:03:35 -0400232
Ralph Gilesda025d52011-10-26 20:24:49 -0700233 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)
Jean-Marc Valinc7921082012-03-05 19:56:13 -0500240 {
241 RESTORE_STACK;
Ralph Gilesda025d52011-10-26 20:24:49 -0700242 return OPUS_BUFFER_TOO_SMALL;
Jean-Marc Valinc7921082012-03-05 19:56:13 -0500243 }
Timothy B. Terriberrya40689e2012-09-07 06:01:53 -0700244 /* Limit frame_size to avoid excessive stack allocations. */
245 frame_size = IMIN(frame_size, st->Fs/25*3);
Ralph Gilesda025d52011-10-26 20:24:49 -0700246 /* 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;
Jean-Marc Valin8fe8b8e2011-08-18 15:00:59 -0400260
Ralph Gilesda025d52011-10-26 20:24:49 -0700261 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 }
Jean-Marc Valin04584ea2010-06-30 15:03:35 -0400272
Jean-Marc Valin7fcd66c2012-12-04 15:07:45 -0500273 /* 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);
Jean-Marc Valin222494f2011-08-17 15:53:37 -0400277
Jean-Marc Valincc83f6b2012-11-05 10:25:20 -0500278 pcm_transition_silk_size = 0;
279 pcm_transition_celt_size = 0;
Ralph Gilesda025d52011-10-26 20:24:49 -0700280 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;
Jean-Marc Valincc83f6b2012-11-05 10:25:20 -0500286 /* Decide where to allocate the stack memory for pcm_transition */
Ralph Gilesda025d52011-10-26 20:24:49 -0700287 if (mode == MODE_CELT_ONLY)
Jean-Marc Valincc83f6b2012-11-05 10:25:20 -0500288 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);
Ralph Gilesda025d52011-10-26 20:24:49 -0700297 }
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 }
Jean-Marc Valin53fb0f72011-01-31 15:56:38 -0500306
Jean-Marc Valincc83f6b2012-11-05 10:25:20 -0500307 /* 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);
Jean-Marc Valin222494f2011-08-17 15:53:37 -0400310
Ralph Gilesda025d52011-10-26 20:24:49 -0700311 /* SILK processing */
312 if (mode != MODE_CELT_ONLY)
313 {
314 int lost_flag, decoded_samples;
315 opus_int16 *pcm_ptr = pcm_silk;
Jean-Marc Valin606250a2011-02-15 14:31:21 -0500316
Ralph Gilesda025d52011-10-26 20:24:49 -0700317 if (st->prev_mode==MODE_CELT_ONLY)
318 silk_InitDecoder( silk_dec );
Jean-Marc Valin606250a2011-02-15 14:31:21 -0500319
Jean-Marc Valin72273002012-04-20 10:26:08 -0400320 /* The SILK PLC cannot produce frames of less than 10 ms */
Ralph Gilesda025d52011-10-26 20:24:49 -0700321 st->DecControl.payloadSize_ms = IMAX(10, 1000 * audiosize / st->Fs);
Jean-Marc Valin0c5085a2011-10-04 01:10:32 -0400322
Ralph Gilesda025d52011-10-26 20:24:49 -0700323 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;
Jean-Marc Valinb5be8262010-11-12 06:47:46 +0800340 }
Ralph Gilesda025d52011-10-26 20:24:49 -0700341 }
Jean-Marc Valin67008d22010-07-20 11:13:30 -0400342
Ralph Gilesda025d52011-10-26 20:24:49 -0700343 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 }
Jean-Marc Valin0c0c5f92011-03-07 20:54:33 -0500360 }
Ralph Gilesda025d52011-10-26 20:24:49 -0700361 pcm_ptr += silk_frame_size * st->channels;
362 decoded_samples += silk_frame_size;
363 } while( decoded_samples < frame_size );
364 }
Jean-Marc Valin0c0c5f92011-03-07 20:54:33 -0500365
Ralph Gilesda025d52011-10-26 20:24:49 -0700366 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;
Jean-Marc Valin0c0c5f92011-03-07 20:54:33 -0500398
Ralph Gilesda025d52011-10-26 20:24:49 -0700399 {
400 int endband=21;
Jean-Marc Valin0c0c5f92011-03-07 20:54:33 -0500401
Ralph Gilesda025d52011-10-26 20:24:49 -0700402 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 }
Jean-Marc Valine2a09db2011-03-02 17:54:43 -0500421
Ralph Gilesda025d52011-10-26 20:24:49 -0700422 if (redundancy)
Jean-Marc Valincc83f6b2012-11-05 10:25:20 -0500423 {
Ralph Gilesda025d52011-10-26 20:24:49 -0700424 transition = 0;
Jean-Marc Valincc83f6b2012-11-05 10:25:20 -0500425 pcm_transition_silk_size=0;
426 }
427
428 ALLOC(pcm_transition_silk, pcm_transition_silk_size, opus_val16);
Jean-Marc Valin0c0c5f92011-03-07 20:54:33 -0500429
Ralph Gilesda025d52011-10-26 20:24:49 -0700430 if (transition && mode != MODE_CELT_ONLY)
Jean-Marc Valincc83f6b2012-11-05 10:25:20 -0500431 {
432 pcm_transition = pcm_transition_silk;
Ralph Gilesda025d52011-10-26 20:24:49 -0700433 opus_decode_frame(st, NULL, 0, pcm_transition, IMIN(F5, audiosize), 0);
Jean-Marc Valincc83f6b2012-11-05 10:25:20 -0500434 }
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);
Jean-Marc Valin04584ea2010-06-30 15:03:35 -0400439
Ralph Gilesda025d52011-10-26 20:24:49 -0700440 /* 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 }
Jean-Marc Valina93f5012011-03-03 15:50:08 -0500448
Ralph Gilesda025d52011-10-26 20:24:49 -0700449 /* MUST be after PLC */
450 celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(start_band));
Jean-Marc Valin222494f2011-08-17 15:53:37 -0400451
Ralph Gilesda025d52011-10-26 20:24:49 -0700452 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 */
Jean-Marc Valin17c59662012-02-17 16:09:21 -0500467 if (st->prev_mode == MODE_HYBRID && !(redundancy && celt_to_silk && st->prev_redundancy) )
Ralph Gilesda025d52011-10-26 20:24:49 -0700468 {
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 {
Jean-Marc Valin222494f2011-08-17 15:53:37 -0400476#ifdef FIXED_POINT
Ralph Gilesda025d52011-10-26 20:24:49 -0700477 for (i=0;i<frame_size*st->channels;i++)
478 pcm[i] = SAT16(pcm[i] + pcm_silk[i]);
Jean-Marc Valin222494f2011-08-17 15:53:37 -0400479#else
Ralph Gilesda025d52011-10-26 20:24:49 -0700480 for (i=0;i<frame_size*st->channels;i++)
Gregory Maxwell37f56592012-07-17 17:40:55 -0400481 pcm[i] = pcm[i] + (opus_val16)((1.f/32768.f)*pcm_silk[i]);
Jean-Marc Valin222494f2011-08-17 15:53:37 -0400482#endif
Ralph Gilesda025d52011-10-26 20:24:49 -0700483 }
Koen Vos8f67b202011-02-03 09:31:12 -0500484
Ralph Gilesda025d52011-10-26 20:24:49 -0700485 {
486 const CELTMode *celt_mode;
487 celt_decoder_ctl(celt_dec, CELT_GET_MODE(&celt_mode));
488 window = celt_mode->window;
489 }
Koen Vosd8765e52011-04-26 23:21:27 -0400490
Ralph Gilesda025d52011-10-26 20:24:49 -0700491 /* 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));
Jean-Marc Valin0c0c5f92011-03-07 20:54:33 -0500496
Ralph Gilesda025d52011-10-26 20:24:49 -0700497 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 }
Ralph Giles3f0962c2011-07-29 14:01:54 -0700532
Gregory Maxwell28b41ae2012-07-11 00:04:24 -0400533 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
Ralph Gilesda025d52011-10-26 20:24:49 -0700545 if (len <= 1)
546 st->rangeFinal = 0;
547 else
548 st->rangeFinal = dec.rng ^ redundant_rng;
Koen Vos8f67b202011-02-03 09:31:12 -0500549
Ralph Gilesda025d52011-10-26 20:24:49 -0700550 st->prev_mode = mode;
551 st->prev_redundancy = redundancy && !celt_to_silk;
Jean-Marc Valin92831142012-12-05 00:50:53 -0500552
553 if (celt_ret>=0)
554 {
555 if (OPUS_CHECK_ARRAY(pcm, audiosize*st->channels))
556 OPUS_PRINT_INT(audiosize);
557 }
558
Ralph Gilesda025d52011-10-26 20:24:49 -0700559 RESTORE_STACK;
560 return celt_ret < 0 ? celt_ret : audiosize;
Jean-Marc Valin04584ea2010-06-30 15:03:35 -0400561
562}
563
Jean-Marc Valin35930692013-05-18 02:50:40 -0400564static int parse_size(const unsigned char *data, opus_int32 len, opus_int16 *size)
Jean-Marc Valina7d31b72011-03-13 20:41:52 -0400565{
Ralph Gilesda025d52011-10-26 20:24:49 -0700566 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 }
Jean-Marc Valina7d31b72011-03-13 20:41:52 -0400582}
583
Gregory Maxwelle7028172011-11-19 23:58:09 -0500584static int opus_packet_parse_impl(const unsigned char *data, opus_int32 len,
Timothy B. Terriberry79540652011-08-18 23:29:52 -0400585 int self_delimited, unsigned char *out_toc,
Jean-Marc Valin35930692013-05-18 02:50:40 -0400586 const unsigned char *frames[48], opus_int16 size[48], int *payload_offset)
Jean-Marc Valin4154dad2011-08-08 11:57:13 -0400587{
588 int i, bytes;
589 int count;
Timothy B. Terriberry79540652011-08-18 23:29:52 -0400590 int cbr;
Jean-Marc Valin4154dad2011-08-08 11:57:13 -0400591 unsigned char ch, toc;
592 int framesize;
Jean-Marc Valin259e1662012-12-03 13:05:24 -0500593 opus_int32 last_size;
Jean-Marc Valin6bb1c182011-08-18 15:54:00 -0400594 const unsigned char *data0 = data;
Jean-Marc Valin4154dad2011-08-08 11:57:13 -0400595
596 if (size==NULL)
597 return OPUS_BAD_ARG;
598
599 framesize = opus_packet_get_samples_per_frame(data, 48000);
600
Timothy B. Terriberry79540652011-08-18 23:29:52 -0400601 cbr = 0;
Jean-Marc Valin4154dad2011-08-08 11:57:13 -0400602 toc = *data++;
603 len--;
Timothy B. Terriberry79540652011-08-18 23:29:52 -0400604 last_size = len;
Jean-Marc Valin4154dad2011-08-08 11:57:13 -0400605 switch (toc&0x3)
606 {
607 /* One frame */
608 case 0:
609 count=1;
Jean-Marc Valin4154dad2011-08-08 11:57:13 -0400610 break;
Ralph Gilesda025d52011-10-26 20:24:49 -0700611 /* Two CBR frames */
Jean-Marc Valin4154dad2011-08-08 11:57:13 -0400612 case 1:
613 count=2;
Timothy B. Terriberry79540652011-08-18 23:29:52 -0400614 cbr = 1;
615 if (!self_delimited)
616 {
617 if (len&0x1)
Jean-Marc Valin331e9fe2011-09-06 14:30:19 -0400618 return OPUS_INVALID_PACKET;
Jean-Marc Valin259e1662012-12-03 13:05:24 -0500619 last_size = len/2;
620 /* If last_size doesn't fit in size[0], we'll catch it later */
Jean-Marc Valin35930692013-05-18 02:50:40 -0400621 size[0] = (opus_int16)last_size;
Timothy B. Terriberry79540652011-08-18 23:29:52 -0400622 }
Jean-Marc Valin4154dad2011-08-08 11:57:13 -0400623 break;
Ralph Gilesda025d52011-10-26 20:24:49 -0700624 /* Two VBR frames */
Jean-Marc Valin4154dad2011-08-08 11:57:13 -0400625 case 2:
626 count = 2;
627 bytes = parse_size(data, len, size);
628 len -= bytes;
629 if (size[0]<0 || size[0] > len)
Jean-Marc Valin331e9fe2011-09-06 14:30:19 -0400630 return OPUS_INVALID_PACKET;
Jean-Marc Valin4154dad2011-08-08 11:57:13 -0400631 data += bytes;
Timothy B. Terriberry79540652011-08-18 23:29:52 -0400632 last_size = len-size[0];
Jean-Marc Valin4154dad2011-08-08 11:57:13 -0400633 break;
Ralph Gilesda025d52011-10-26 20:24:49 -0700634 /* Multiple CBR/VBR frames (from 0 to 120 ms) */
Jean-Marc Valin72273002012-04-20 10:26:08 -0400635 default: /*case 3:*/
Jean-Marc Valin4154dad2011-08-08 11:57:13 -0400636 if (len<1)
Jean-Marc Valin331e9fe2011-09-06 14:30:19 -0400637 return OPUS_INVALID_PACKET;
Jean-Marc Valin4154dad2011-08-08 11:57:13 -0400638 /* Number of frames encoded in bits 0 to 5 */
639 ch = *data++;
640 count = ch&0x3F;
641 if (count <= 0 || framesize*count > 5760)
Ralph Gilesda025d52011-10-26 20:24:49 -0700642 return OPUS_INVALID_PACKET;
Jean-Marc Valin4154dad2011-08-08 11:57:13 -0400643 len--;
644 /* Padding flag is bit 6 */
645 if (ch&0x40)
646 {
Jean-Marc Valin4154dad2011-08-08 11:57:13 -0400647 int p;
648 do {
649 if (len<=0)
Jean-Marc Valin331e9fe2011-09-06 14:30:19 -0400650 return OPUS_INVALID_PACKET;
Jean-Marc Valin4154dad2011-08-08 11:57:13 -0400651 p = *data++;
652 len--;
Jean-Marc Valin9345aaa2012-11-30 17:36:36 -0500653 len -= p==255 ? 254: p;
Jean-Marc Valin4154dad2011-08-08 11:57:13 -0400654 } while (p==255);
Jean-Marc Valin4154dad2011-08-08 11:57:13 -0400655 }
656 if (len<0)
Jean-Marc Valin331e9fe2011-09-06 14:30:19 -0400657 return OPUS_INVALID_PACKET;
Jean-Marc Valin4154dad2011-08-08 11:57:13 -0400658 /* VBR flag is bit 7 */
Timothy B. Terriberry79540652011-08-18 23:29:52 -0400659 cbr = !(ch&0x80);
Jean-Marc Valin823a0542011-09-08 16:26:54 -0400660 if (!cbr)
Jean-Marc Valin4154dad2011-08-08 11:57:13 -0400661 {
662 /* VBR case */
Timothy B. Terriberry79540652011-08-18 23:29:52 -0400663 last_size = len;
Jean-Marc Valin4154dad2011-08-08 11:57:13 -0400664 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)
Jean-Marc Valin331e9fe2011-09-06 14:30:19 -0400669 return OPUS_INVALID_PACKET;
Jean-Marc Valin4154dad2011-08-08 11:57:13 -0400670 data += bytes;
671 last_size -= bytes+size[i];
672 }
673 if (last_size<0)
Jean-Marc Valin331e9fe2011-09-06 14:30:19 -0400674 return OPUS_INVALID_PACKET;
Timothy B. Terriberry79540652011-08-18 23:29:52 -0400675 } else if (!self_delimited)
676 {
Jean-Marc Valin4154dad2011-08-08 11:57:13 -0400677 /* CBR case */
Timothy B. Terriberry79540652011-08-18 23:29:52 -0400678 last_size = len/count;
679 if (last_size*count!=len)
Jean-Marc Valin331e9fe2011-09-06 14:30:19 -0400680 return OPUS_INVALID_PACKET;
Timothy B. Terriberry79540652011-08-18 23:29:52 -0400681 for (i=0;i<count-1;i++)
Jean-Marc Valin35930692013-05-18 02:50:40 -0400682 size[i] = (opus_int16)last_size;
Jean-Marc Valin4154dad2011-08-08 11:57:13 -0400683 }
684 break;
685 }
Timothy B. Terriberry79540652011-08-18 23:29:52 -0400686 /* 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)
Jean-Marc Valin331e9fe2011-09-06 14:30:19 -0400692 return OPUS_INVALID_PACKET;
Timothy B. Terriberry79540652011-08-18 23:29:52 -0400693 data += bytes;
694 /* For CBR packets, apply the size to all the frames. */
695 if (cbr)
696 {
697 if (size[count-1]*count > len)
Jean-Marc Valin331e9fe2011-09-06 14:30:19 -0400698 return OPUS_INVALID_PACKET;
Timothy B. Terriberry79540652011-08-18 23:29:52 -0400699 for (i=0;i<count-1;i++)
700 size[i] = size[count-1];
701 } else if(size[count-1] > last_size)
Jean-Marc Valin331e9fe2011-09-06 14:30:19 -0400702 return OPUS_INVALID_PACKET;
Timothy B. Terriberry79540652011-08-18 23:29:52 -0400703 } else
704 {
705 /* Because it's not encoded explicitly, it's possible the size of the
Ralph Gilesda025d52011-10-26 20:24:49 -0700706 last packet (or all the packets, for the CBR case) is larger than
707 1275. Reject them here.*/
Timothy B. Terriberry79540652011-08-18 23:29:52 -0400708 if (last_size > 1275)
Ralph Gilesda025d52011-10-26 20:24:49 -0700709 return OPUS_INVALID_PACKET;
Jean-Marc Valin35930692013-05-18 02:50:40 -0400710 size[count-1] = (opus_int16)last_size;
Timothy B. Terriberry79540652011-08-18 23:29:52 -0400711 }
Jean-Marc Valin4154dad2011-08-08 11:57:13 -0400712
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
Jean-Marc Valin6bb1c182011-08-18 15:54:00 -0400725 if (payload_offset)
Koen Vos0b00b312012-07-12 14:55:49 -0400726 *payload_offset = (int)(data-data0);
Jean-Marc Valin4154dad2011-08-08 11:57:13 -0400727
728 return count;
729}
730
Gregory Maxwelle7028172011-11-19 23:58:09 -0500731int opus_packet_parse(const unsigned char *data, opus_int32 len,
Timothy B. Terriberry79540652011-08-18 23:29:52 -0400732 unsigned char *out_toc, const unsigned char *frames[48],
Jean-Marc Valin35930692013-05-18 02:50:40 -0400733 opus_int16 size[48], int *payload_offset)
Timothy B. Terriberry79540652011-08-18 23:29:52 -0400734{
Ralph Gilesda025d52011-10-26 20:24:49 -0700735 return opus_packet_parse_impl(data, len, 0, out_toc,
736 frames, size, payload_offset);
Timothy B. Terriberry79540652011-08-18 23:29:52 -0400737}
738
Jean-Marc Valind4e93402011-08-27 00:52:26 -0400739int opus_decode_native(OpusDecoder *st, const unsigned char *data,
Gregory Maxwelle7028172011-11-19 23:58:09 -0500740 opus_int32 len, opus_val16 *pcm, int frame_size, int decode_fec,
Jean-Marc Valin32c4a0c2013-03-01 15:18:23 -0500741 int self_delimited, int *packet_offset, int soft_clip)
Jean-Marc Valina7d31b72011-03-13 20:41:52 -0400742{
Ralph Gilesda025d52011-10-26 20:24:49 -0700743 int i, nb_samples;
744 int count, offset;
745 unsigned char toc;
746 int tot_offset;
Jean-Marc Valin7fcd66c2012-12-04 15:07:45 -0500747 int packet_frame_size, packet_bandwidth, packet_mode, packet_stream_channels;
Ralph Gilesda025d52011-10-26 20:24:49 -0700748 /* 48 x 2.5 ms = 120 ms */
Jean-Marc Valin35930692013-05-18 02:50:40 -0400749 opus_int16 size[48];
Ralph Gilesda025d52011-10-26 20:24:49 -0700750 if (decode_fec<0 || decode_fec>1)
751 return OPUS_BAD_ARG;
Jean-Marc Valin7fcd66c2012-12-04 15:07:45 -0500752 /* 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;
Ralph Gilesda025d52011-10-26 20:24:49 -0700755 if (len==0 || data==NULL)
Jean-Marc Valin7fcd66c2012-12-04 15:07:45 -0500756 {
757 int pcm_count=0;
758 do {
759 int ret;
Jean-Marc Valin92831142012-12-05 00:50:53 -0500760 ret = opus_decode_frame(st, NULL, 0, pcm+pcm_count*st->channels, frame_size-pcm_count, 0);
Jean-Marc Valin7fcd66c2012-12-04 15:07:45 -0500761 if (ret<0)
762 return ret;
Jean-Marc Valin7fcd66c2012-12-04 15:07:45 -0500763 pcm_count += ret;
764 } while (pcm_count < frame_size);
Jean-Marc Valin92831142012-12-05 00:50:53 -0500765 celt_assert(pcm_count == frame_size);
766 if (OPUS_CHECK_ARRAY(pcm, pcm_count*st->channels))
767 OPUS_PRINT_INT(pcm_count);
Jean-Marc Valina0737d12012-12-05 21:48:45 -0500768 st->last_packet_duration = pcm_count;
Jean-Marc Valin7fcd66c2012-12-04 15:07:45 -0500769 return pcm_count;
770 } else if (len<0)
Ralph Gilesda025d52011-10-26 20:24:49 -0700771 return OPUS_BAD_ARG;
Jean-Marc Valind4e93402011-08-27 00:52:26 -0400772
Jean-Marc Valin7fcd66c2012-12-04 15:07:45 -0500773 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);
Jean-Marc Valin4154dad2011-08-08 11:57:13 -0400777
Ralph Gilesda025d52011-10-26 20:24:49 -0700778 count = opus_packet_parse_impl(data, len, self_delimited, &toc, NULL, size, &offset);
Jean-Marc Valin7fcd66c2012-12-04 15:07:45 -0500779
780 data += offset;
781
782 if (decode_fec)
783 {
Jean-Marc Valina0737d12012-12-05 21:48:45 -0500784 int duration_copy;
Jean-Marc Valin7fcd66c2012-12-04 15:07:45 -0500785 int ret;
786 /* If no FEC can be present, run the PLC (recursive call) */
Jean-Marc Valin1bf32bb2013-04-23 02:41:28 -0400787 if (frame_size < packet_frame_size || packet_mode == MODE_CELT_ONLY || st->mode == MODE_CELT_ONLY)
Jean-Marc Valin32c4a0c2013-03-01 15:18:23 -0500788 return opus_decode_native(st, NULL, 0, pcm, frame_size, 0, 0, NULL, soft_clip);
Jean-Marc Valin7fcd66c2012-12-04 15:07:45 -0500789 /* Otherwise, run the PLC on everything except the size for which we might have FEC */
Jean-Marc Valina0737d12012-12-05 21:48:45 -0500790 duration_copy = st->last_packet_duration;
Jean-Marc Valin1bf32bb2013-04-23 02:41:28 -0400791 if (frame_size-packet_frame_size!=0)
Jean-Marc Valina0737d12012-12-05 21:48:45 -0500792 {
Jean-Marc Valin1bf32bb2013-04-23 02:41:28 -0400793 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);
Jean-Marc Valina0737d12012-12-05 21:48:45 -0500800 }
Jean-Marc Valin7fcd66c2012-12-04 15:07:45 -0500801 /* 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;
Jean-Marc Valin92831142012-12-05 00:50:53 -0500810 else {
811 if (OPUS_CHECK_ARRAY(pcm, frame_size*st->channels))
812 OPUS_PRINT_INT(frame_size);
Jean-Marc Valina0737d12012-12-05 21:48:45 -0500813 st->last_packet_duration = frame_size;
Jean-Marc Valin7fcd66c2012-12-04 15:07:45 -0500814 return frame_size;
Jean-Marc Valin92831142012-12-05 00:50:53 -0500815 }
Jean-Marc Valin7fcd66c2012-12-04 15:07:45 -0500816 }
817 tot_offset = 0;
Ralph Gilesda025d52011-10-26 20:24:49 -0700818 if (count < 0)
819 return count;
Jean-Marc Valin4154dad2011-08-08 11:57:13 -0400820
Ralph Gilesda025d52011-10-26 20:24:49 -0700821 tot_offset += offset;
Jean-Marc Valind4e93402011-08-27 00:52:26 -0400822
Jean-Marc Valina5bd4402012-12-04 14:13:00 -0500823 if (count*packet_frame_size > frame_size)
Ralph Gilesda025d52011-10-26 20:24:49 -0700824 return OPUS_BUFFER_TOO_SMALL;
Jean-Marc Valina5bd4402012-12-04 14:13:00 -0500825
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
Ralph Gilesda025d52011-10-26 20:24:49 -0700832 nb_samples=0;
833 for (i=0;i<count;i++)
834 {
835 int ret;
Jean-Marc Valin92831142012-12-05 00:50:53 -0500836 ret = opus_decode_frame(st, data, size[i], pcm+nb_samples*st->channels, frame_size-nb_samples, 0);
Ralph Gilesda025d52011-10-26 20:24:49 -0700837 if (ret<0)
838 return ret;
Jean-Marc Valin92831142012-12-05 00:50:53 -0500839 celt_assert(ret==packet_frame_size);
Ralph Gilesda025d52011-10-26 20:24:49 -0700840 data += size[i];
841 tot_offset += size[i];
Ralph Gilesda025d52011-10-26 20:24:49 -0700842 nb_samples += ret;
843 }
844 if (packet_offset != NULL)
845 *packet_offset = tot_offset;
Jean-Marc Valin512d8492012-12-04 14:13:46 -0500846 st->last_packet_duration = nb_samples;
Jean-Marc Valin92831142012-12-05 00:50:53 -0500847 if (OPUS_CHECK_ARRAY(pcm, nb_samples*st->channels))
848 OPUS_PRINT_INT(nb_samples);
Jean-Marc Valin32c4a0c2013-03-01 15:18:23 -0500849#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
Ralph Gilesda025d52011-10-26 20:24:49 -0700855 return nb_samples;
Jean-Marc Valina7d31b72011-03-13 20:41:52 -0400856}
Jean-Marc Valin4154dad2011-08-08 11:57:13 -0400857
Jean-Marc Valin222494f2011-08-17 15:53:37 -0400858#ifdef FIXED_POINT
859
Jean-Marc Valind4e93402011-08-27 00:52:26 -0400860int opus_decode(OpusDecoder *st, const unsigned char *data,
Gregory Maxwelle7028172011-11-19 23:58:09 -0500861 opus_int32 len, opus_val16 *pcm, int frame_size, int decode_fec)
Jean-Marc Valind4e93402011-08-27 00:52:26 -0400862{
Jean-Marc Valin32c4a0c2013-03-01 15:18:23 -0500863 return opus_decode_native(st, data, len, pcm, frame_size, decode_fec, 0, NULL, 0);
Jean-Marc Valind4e93402011-08-27 00:52:26 -0400864}
865
Jean-Marc Valin222494f2011-08-17 15:53:37 -0400866#ifndef DISABLE_FLOAT_API
867int opus_decode_float(OpusDecoder *st, const unsigned char *data,
Gregory Maxwelle7028172011-11-19 23:58:09 -0500868 opus_int32 len, float *pcm, int frame_size, int decode_fec)
Jean-Marc Valin222494f2011-08-17 15:53:37 -0400869{
870 VARDECL(opus_int16, out);
871 int ret, i;
872 ALLOC_STACK;
873
874 ALLOC(out, frame_size*st->channels, opus_int16);
875
Jean-Marc Valin32c4a0c2013-03-01 15:18:23 -0500876 ret = opus_decode_native(st, data, len, out, frame_size, decode_fec, 0, NULL, 0);
Jean-Marc Valin222494f2011-08-17 15:53:37 -0400877 if (ret > 0)
878 {
879 for (i=0;i<ret*st->channels;i++)
Jean-Marc Valin294bfec2011-10-20 00:39:41 -0400880 pcm[i] = (1.f/32768.f)*(out[i]);
Jean-Marc Valin222494f2011-08-17 15:53:37 -0400881 }
882 RESTORE_STACK;
883 return ret;
884}
885#endif
886
Jean-Marc Valind4e93402011-08-27 00:52:26 -0400887
Jean-Marc Valin222494f2011-08-17 15:53:37 -0400888#else
889int opus_decode(OpusDecoder *st, const unsigned char *data,
Gregory Maxwelle7028172011-11-19 23:58:09 -0500890 opus_int32 len, opus_int16 *pcm, int frame_size, int decode_fec)
Jean-Marc Valin222494f2011-08-17 15:53:37 -0400891{
892 VARDECL(float, out);
893 int ret, i;
894 ALLOC_STACK;
895
Jean-Marc Valinc7921082012-03-05 19:56:13 -0500896 if(frame_size<0)
897 {
898 RESTORE_STACK;
899 return OPUS_BAD_ARG;
900 }
Gregory Maxwelle699c192011-11-25 23:53:15 -0500901
Jean-Marc Valin222494f2011-08-17 15:53:37 -0400902 ALLOC(out, frame_size*st->channels, float);
903
Jean-Marc Valin32c4a0c2013-03-01 15:18:23 -0500904 ret = opus_decode_native(st, data, len, out, frame_size, decode_fec, 0, NULL, 1);
Jean-Marc Valin222494f2011-08-17 15:53:37 -0400905 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}
Jean-Marc Valind4e93402011-08-27 00:52:26 -0400913
914int opus_decode_float(OpusDecoder *st, const unsigned char *data,
Gregory Maxwelle7028172011-11-19 23:58:09 -0500915 opus_int32 len, opus_val16 *pcm, int frame_size, int decode_fec)
Jean-Marc Valind4e93402011-08-27 00:52:26 -0400916{
Jean-Marc Valin32c4a0c2013-03-01 15:18:23 -0500917 return opus_decode_native(st, data, len, pcm, frame_size, decode_fec, 0, NULL, 0);
Jean-Marc Valind4e93402011-08-27 00:52:26 -0400918}
919
Jean-Marc Valin222494f2011-08-17 15:53:37 -0400920#endif
Jean-Marc Valin4154dad2011-08-08 11:57:13 -0400921
Jean-Marc Valin955f94c2011-03-08 22:12:43 -0500922int opus_decoder_ctl(OpusDecoder *st, int request, ...)
Jean-Marc Valin24f36e02010-07-06 14:41:20 -0400923{
Jean-Marc Valinbe89c392011-08-30 12:39:51 -0400924 int ret = OPUS_OK;
925 va_list ap;
Jean-Marc Valin25f7f352011-09-14 09:50:06 -0700926 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
Jean-Marc Valin24f36e02010-07-06 14:41:20 -0400932
Jean-Marc Valinbe89c392011-08-30 12:39:51 -0400933 va_start(ap, request);
Jean-Marc Valin24f36e02010-07-06 14:41:20 -0400934
Jean-Marc Valinbe89c392011-08-30 12:39:51 -0400935 switch (request)
936 {
937 case OPUS_GET_BANDWIDTH_REQUEST:
938 {
939 opus_int32 *value = va_arg(ap, opus_int32*);
Gregory Maxwella0d096f2013-06-29 20:33:32 -0700940 if (!value)
941 {
Gregory Maxwellb271dae2013-06-29 20:25:55 -0700942 goto bad_arg;
943 }
Jean-Marc Valinbe89c392011-08-30 12:39:51 -0400944 *value = st->bandwidth;
945 }
946 break;
947 case OPUS_GET_FINAL_RANGE_REQUEST:
948 {
949 opus_uint32 *value = va_arg(ap, opus_uint32*);
Gregory Maxwella0d096f2013-06-29 20:33:32 -0700950 if (!value)
951 {
Gregory Maxwellb271dae2013-06-29 20:25:55 -0700952 goto bad_arg;
953 }
Jean-Marc Valinbe89c392011-08-30 12:39:51 -0400954 *value = st->rangeFinal;
955 }
956 break;
Jean-Marc Valin04465632011-08-30 18:01:06 -0400957 case OPUS_RESET_STATE:
958 {
Jean-Marc Valin04465632011-08-30 18:01:06 -0400959 OPUS_CLEAR((char*)&st->OPUS_DECODER_RESET_START,
Gregory Maxwelld32f9482011-09-04 07:48:20 -0400960 sizeof(OpusDecoder)-
Jean-Marc Valin04465632011-08-30 18:01:06 -0400961 ((char*)&st->OPUS_DECODER_RESET_START - (char*)st));
962
Jean-Marc Valinf9e701a2011-08-31 17:47:48 -0400963 celt_decoder_ctl(celt_dec, OPUS_RESET_STATE);
Jean-Marc Valin04465632011-08-30 18:01:06 -0400964 silk_InitDecoder( silk_dec );
965 st->stream_channels = st->channels;
966 st->frame_size = st->Fs/400;
967 }
968 break;
Timothy B. Terriberrya40689e2012-09-07 06:01:53 -0700969 case OPUS_GET_SAMPLE_RATE_REQUEST:
970 {
971 opus_int32 *value = va_arg(ap, opus_int32*);
Gregory Maxwella0d096f2013-06-29 20:33:32 -0700972 if (!value)
973 {
Gregory Maxwellb271dae2013-06-29 20:25:55 -0700974 goto bad_arg;
975 }
Timothy B. Terriberrya40689e2012-09-07 06:01:53 -0700976 *value = st->Fs;
977 }
978 break;
Jean-Marc Valin25f7f352011-09-14 09:50:06 -0700979 case OPUS_GET_PITCH_REQUEST:
980 {
Jean-Marc Valinc0387ff2012-03-05 19:19:59 -0500981 opus_int32 *value = va_arg(ap, opus_int32*);
Gregory Maxwella0d096f2013-06-29 20:33:32 -0700982 if (!value)
983 {
Gregory Maxwellb271dae2013-06-29 20:25:55 -0700984 goto bad_arg;
985 }
Jean-Marc Valin25f7f352011-09-14 09:50:06 -0700986 if (st->prev_mode == MODE_CELT_ONLY)
987 celt_decoder_ctl(celt_dec, OPUS_GET_PITCH(value));
988 else
Jean-Marc Valinb24e5742011-10-11 21:09:14 -0400989 *value = st->DecControl.prevPitchLag;
Jean-Marc Valin25f7f352011-09-14 09:50:06 -0700990 }
991 break;
Gregory Maxwell28b41ae2012-07-11 00:04:24 -0400992 case OPUS_GET_GAIN_REQUEST:
993 {
994 opus_int32 *value = va_arg(ap, opus_int32*);
Gregory Maxwella0d096f2013-06-29 20:33:32 -0700995 if (!value)
996 {
Gregory Maxwellb271dae2013-06-29 20:25:55 -0700997 goto bad_arg;
998 }
Gregory Maxwell28b41ae2012-07-11 00:04:24 -0400999 *value = st->decode_gain;
1000 }
1001 break;
1002 case OPUS_SET_GAIN_REQUEST:
1003 {
1004 opus_int32 value = va_arg(ap, opus_int32);
Gregory Maxwella0d096f2013-06-29 20:33:32 -07001005 if (value<-32768 || value>32767)
1006 {
Gregory Maxwellb271dae2013-06-29 20:25:55 -07001007 goto bad_arg;
1008 }
Gregory Maxwell28b41ae2012-07-11 00:04:24 -04001009 st->decode_gain = value;
1010 }
1011 break;
Jean-Marc Valin512d8492012-12-04 14:13:46 -05001012 case OPUS_GET_LAST_PACKET_DURATION_REQUEST:
1013 {
1014 opus_uint32 *value = va_arg(ap, opus_uint32*);
Gregory Maxwella0d096f2013-06-29 20:33:32 -07001015 if (!value)
1016 {
Gregory Maxwellb271dae2013-06-29 20:25:55 -07001017 goto bad_arg;
1018 }
Jean-Marc Valin512d8492012-12-04 14:13:46 -05001019 *value = st->last_packet_duration;
1020 }
1021 break;
Jean-Marc Valinbe89c392011-08-30 12:39:51 -04001022 default:
1023 /*fprintf(stderr, "unknown opus_decoder_ctl() request: %d", request);*/
Jean-Marc Valin06237d72011-09-01 13:20:40 -04001024 ret = OPUS_UNIMPLEMENTED;
Jean-Marc Valinbe89c392011-08-30 12:39:51 -04001025 break;
1026 }
Alfred E. Heggestad3d31d702010-07-27 16:38:04 +02001027
Jean-Marc Valinbe89c392011-08-30 12:39:51 -04001028 va_end(ap);
1029 return ret;
Gregory Maxwelldd7b0da2013-06-29 20:06:07 -07001030bad_arg:
1031 va_end(ap);
1032 return OPUS_BAD_ARG;
Jean-Marc Valin24f36e02010-07-06 14:41:20 -04001033}
1034
Jean-Marc Valin05dd36a2010-10-18 12:50:49 -04001035void opus_decoder_destroy(OpusDecoder *st)
Jean-Marc Valin04584ea2010-06-30 15:03:35 -04001036{
Ralph Gilesda025d52011-10-26 20:24:49 -07001037 opus_free(st);
Jean-Marc Valin04584ea2010-06-30 15:03:35 -04001038}
Koen Vos8f67b202011-02-03 09:31:12 -05001039
Jean-Marc Valin0fe40782011-03-13 12:41:08 -04001040
1041int opus_packet_get_bandwidth(const unsigned char *data)
1042{
Ralph Gilesda025d52011-10-26 20:24:49 -07001043 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;
Jean-Marc Valin0fe40782011-03-13 12:41:08 -04001057}
1058
Ralph Gilesda025d52011-10-26 20:24:49 -07001059int opus_packet_get_samples_per_frame(const unsigned char *data,
1060 opus_int32 Fs)
Jean-Marc Valin0fe40782011-03-13 12:41:08 -04001061{
Ralph Gilesda025d52011-10-26 20:24:49 -07001062 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;
Jean-Marc Valin0fe40782011-03-13 12:41:08 -04001078}
1079
1080int opus_packet_get_nb_channels(const unsigned char *data)
1081{
Ralph Gilesda025d52011-10-26 20:24:49 -07001082 return (data[0]&0x4) ? 2 : 1;
Jean-Marc Valin0fe40782011-03-13 12:41:08 -04001083}
1084
Gregory Maxwelle7028172011-11-19 23:58:09 -05001085int opus_packet_get_nb_frames(const unsigned char packet[], opus_int32 len)
Jean-Marc Valin0fe40782011-03-13 12:41:08 -04001086{
Ralph Gilesda025d52011-10-26 20:24:49 -07001087 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;
Jean-Marc Valin0fe40782011-03-13 12:41:08 -04001099}
1100
Jean-Marc Valind0fd9d42012-12-04 15:45:31 -05001101int opus_packet_get_nb_samples(const unsigned char packet[], opus_int32 len,
1102 opus_int32 Fs)
Jean-Marc Valin0fe40782011-03-13 12:41:08 -04001103{
Ralph Gilesda025d52011-10-26 20:24:49 -07001104 int samples;
1105 int count = opus_packet_get_nb_frames(packet, len);
Jean-Marc Valinee8adbe2012-01-24 14:45:08 +13001106
1107 if (count<0)
1108 return count;
1109
Jean-Marc Valind0fd9d42012-12-04 15:45:31 -05001110 samples = count*opus_packet_get_samples_per_frame(packet, Fs);
Ralph Gilesda025d52011-10-26 20:24:49 -07001111 /* Can't have more than 120 ms */
Jean-Marc Valind0fd9d42012-12-04 15:45:31 -05001112 if (samples*25 > Fs*3)
Ralph Gilesda025d52011-10-26 20:24:49 -07001113 return OPUS_INVALID_PACKET;
1114 else
1115 return samples;
Jean-Marc Valin0fe40782011-03-13 12:41:08 -04001116}
Jean-Marc Valind0fd9d42012-12-04 15:45:31 -05001117
1118int 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}