Line data Source code
1 : /* Copyright (c) 2010 Xiph.Org Foundation, Skype Limited
2 : Written by Jean-Marc Valin and Koen Vos */
3 : /*
4 : Redistribution and use in source and binary forms, with or without
5 : modification, are permitted provided that the following conditions
6 : are met:
7 :
8 : - Redistributions of source code must retain the above copyright
9 : notice, this list of conditions and the following disclaimer.
10 :
11 : - Redistributions in binary form must reproduce the above copyright
12 : notice, this list of conditions and the following disclaimer in the
13 : documentation and/or other materials provided with the distribution.
14 :
15 : THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 : ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 : LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 : A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
19 : OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 : EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 : PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 : PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 : LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 : NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 : SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 : */
27 :
28 : #ifdef HAVE_CONFIG_H
29 : # include "config.h"
30 : #endif
31 :
32 : #ifndef OPUS_BUILD
33 : # error "OPUS_BUILD _MUST_ be defined to build Opus. This probably means you need other defines as well, as in a config.h. See the included build files for details."
34 : #endif
35 :
36 : #if defined(__GNUC__) && (__GNUC__ >= 2) && !defined(__OPTIMIZE__) && !defined(OPUS_WILL_BE_SLOW)
37 : # pragma message "You appear to be compiling without optimization, if so opus will be very slow."
38 : #endif
39 :
40 : #include <stdarg.h>
41 : #include "celt.h"
42 : #include "opus.h"
43 : #include "entdec.h"
44 : #include "modes.h"
45 : #include "API.h"
46 : #include "stack_alloc.h"
47 : #include "float_cast.h"
48 : #include "opus_private.h"
49 : #include "os_support.h"
50 : #include "structs.h"
51 : #include "define.h"
52 : #include "mathops.h"
53 : #include "cpu_support.h"
54 :
55 : struct OpusDecoder {
56 : int celt_dec_offset;
57 : int silk_dec_offset;
58 : int channels;
59 : opus_int32 Fs; /** Sampling rate (at the API level) */
60 : silk_DecControlStruct DecControl;
61 : int decode_gain;
62 : int arch;
63 :
64 : /* Everything beyond this point gets cleared on a reset */
65 : #define OPUS_DECODER_RESET_START stream_channels
66 : int stream_channels;
67 :
68 : int bandwidth;
69 : int mode;
70 : int prev_mode;
71 : int frame_size;
72 : int prev_redundancy;
73 : int last_packet_duration;
74 : #ifndef FIXED_POINT
75 : opus_val16 softclip_mem[2];
76 : #endif
77 :
78 : opus_uint32 rangeFinal;
79 : };
80 :
81 :
82 0 : int opus_decoder_get_size(int channels)
83 : {
84 : int silkDecSizeBytes, celtDecSizeBytes;
85 : int ret;
86 0 : if (channels<1 || channels > 2)
87 0 : return 0;
88 0 : ret = silk_Get_Decoder_Size( &silkDecSizeBytes );
89 0 : if(ret)
90 0 : return 0;
91 0 : silkDecSizeBytes = align(silkDecSizeBytes);
92 0 : celtDecSizeBytes = celt_decoder_get_size(channels);
93 0 : return align(sizeof(OpusDecoder))+silkDecSizeBytes+celtDecSizeBytes;
94 : }
95 :
96 0 : int 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 0 : if ((Fs!=48000&&Fs!=24000&&Fs!=16000&&Fs!=12000&&Fs!=8000)
103 0 : || (channels!=1&&channels!=2))
104 0 : return OPUS_BAD_ARG;
105 :
106 0 : OPUS_CLEAR((char*)st, opus_decoder_get_size(channels));
107 : /* Initialize SILK encoder */
108 0 : ret = silk_Get_Decoder_Size(&silkDecSizeBytes);
109 0 : if (ret)
110 0 : return OPUS_INTERNAL_ERROR;
111 :
112 0 : silkDecSizeBytes = align(silkDecSizeBytes);
113 0 : st->silk_dec_offset = align(sizeof(OpusDecoder));
114 0 : st->celt_dec_offset = st->silk_dec_offset+silkDecSizeBytes;
115 0 : silk_dec = (char*)st+st->silk_dec_offset;
116 0 : celt_dec = (CELTDecoder*)((char*)st+st->celt_dec_offset);
117 0 : st->stream_channels = st->channels = channels;
118 :
119 0 : st->Fs = Fs;
120 0 : st->DecControl.API_sampleRate = st->Fs;
121 0 : st->DecControl.nChannelsAPI = st->channels;
122 :
123 : /* Reset decoder */
124 0 : ret = silk_InitDecoder( silk_dec );
125 0 : if(ret)return OPUS_INTERNAL_ERROR;
126 :
127 : /* Initialize CELT decoder */
128 0 : ret = celt_decoder_init(celt_dec, Fs, channels);
129 0 : if(ret!=OPUS_OK)return OPUS_INTERNAL_ERROR;
130 :
131 0 : celt_decoder_ctl(celt_dec, CELT_SET_SIGNALLING(0));
132 :
133 0 : st->prev_mode = 0;
134 0 : st->frame_size = Fs/400;
135 0 : st->arch = opus_select_arch();
136 0 : return OPUS_OK;
137 : }
138 :
139 0 : OpusDecoder *opus_decoder_create(opus_int32 Fs, int channels, int *error)
140 : {
141 : int ret;
142 : OpusDecoder *st;
143 0 : if ((Fs!=48000&&Fs!=24000&&Fs!=16000&&Fs!=12000&&Fs!=8000)
144 0 : || (channels!=1&&channels!=2))
145 : {
146 0 : if (error)
147 0 : *error = OPUS_BAD_ARG;
148 0 : return NULL;
149 : }
150 0 : st = (OpusDecoder *)opus_alloc(opus_decoder_get_size(channels));
151 0 : if (st == NULL)
152 : {
153 0 : if (error)
154 0 : *error = OPUS_ALLOC_FAIL;
155 0 : return NULL;
156 : }
157 0 : ret = opus_decoder_init(st, Fs, channels);
158 0 : if (error)
159 0 : *error = ret;
160 0 : if (ret != OPUS_OK)
161 : {
162 0 : opus_free(st);
163 0 : st = NULL;
164 : }
165 0 : return st;
166 : }
167 :
168 0 : static 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 0 : int inc = 48000/Fs;
174 0 : for (c=0;c<channels;c++)
175 : {
176 0 : for (i=0;i<overlap;i++)
177 : {
178 0 : opus_val16 w = MULT16_16_Q15(window[i*inc], window[i*inc]);
179 0 : 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 0 : }
184 :
185 0 : static int opus_packet_get_mode(const unsigned char *data)
186 : {
187 : int mode;
188 0 : if (data[0]&0x80)
189 : {
190 0 : mode = MODE_CELT_ONLY;
191 0 : } else if ((data[0]&0x60) == 0x60)
192 : {
193 0 : mode = MODE_HYBRID;
194 : } else {
195 0 : mode = MODE_SILK_ONLY;
196 : }
197 0 : return mode;
198 : }
199 :
200 0 : static 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 0 : 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 0 : opus_val16 *pcm_transition=NULL;
215 : int redundant_audio_size;
216 : VARDECL(opus_val16, redundant_audio);
217 :
218 : int audiosize;
219 : int mode;
220 0 : int transition=0;
221 : int start_band;
222 0 : int redundancy=0;
223 0 : int redundancy_bytes = 0;
224 0 : int celt_to_silk=0;
225 : int c;
226 : int F2_5, F5, F10, F20;
227 : const opus_val16 *window;
228 0 : opus_uint32 redundant_rng = 0;
229 : int celt_accum;
230 : ALLOC_STACK;
231 :
232 0 : silk_dec = (char*)st+st->silk_dec_offset;
233 0 : celt_dec = (CELTDecoder*)((char*)st+st->celt_dec_offset);
234 0 : F20 = st->Fs/50;
235 0 : F10 = F20>>1;
236 0 : F5 = F10>>1;
237 0 : F2_5 = F5>>1;
238 0 : if (frame_size < F2_5)
239 : {
240 : RESTORE_STACK;
241 0 : return OPUS_BUFFER_TOO_SMALL;
242 : }
243 : /* Limit frame_size to avoid excessive stack allocations. */
244 0 : frame_size = IMIN(frame_size, st->Fs/25*3);
245 : /* Payloads of 1 (2 including ToC) or 0 trigger the PLC/DTX */
246 0 : if (len<=1)
247 : {
248 0 : data = NULL;
249 : /* In that case, don't conceal more than what the ToC says */
250 0 : frame_size = IMIN(frame_size, st->frame_size);
251 : }
252 0 : if (data != NULL)
253 : {
254 0 : audiosize = st->frame_size;
255 0 : mode = st->mode;
256 0 : ec_dec_init(&dec,(unsigned char*)data,len);
257 : } else {
258 0 : audiosize = frame_size;
259 0 : mode = st->prev_mode;
260 :
261 0 : if (mode == 0)
262 : {
263 : /* If we haven't got any packet yet, all we can do is return zeros */
264 0 : for (i=0;i<audiosize*st->channels;i++)
265 0 : pcm[i] = 0;
266 : RESTORE_STACK;
267 0 : 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 0 : if (audiosize > F20)
273 : {
274 : do {
275 0 : int ret = opus_decode_frame(st, NULL, 0, pcm, IMIN(audiosize, F20), 0);
276 0 : if (ret<0)
277 : {
278 : RESTORE_STACK;
279 0 : return ret;
280 : }
281 0 : pcm += ret*st->channels;
282 0 : audiosize -= ret;
283 0 : } while (audiosize > 0);
284 : RESTORE_STACK;
285 0 : return frame_size;
286 0 : } else if (audiosize < F20)
287 : {
288 0 : if (audiosize > F10)
289 0 : audiosize = F10;
290 0 : else if (mode != MODE_SILK_ONLY && audiosize > F5 && audiosize < F10)
291 0 : 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 0 : celt_accum = 0;
301 : #endif
302 :
303 0 : pcm_transition_silk_size = ALLOC_NONE;
304 0 : pcm_transition_celt_size = ALLOC_NONE;
305 0 : if (data!=NULL && st->prev_mode > 0 && (
306 0 : (mode == MODE_CELT_ONLY && st->prev_mode != MODE_CELT_ONLY && !st->prev_redundancy)
307 0 : || (mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY) )
308 : )
309 : {
310 0 : transition = 1;
311 : /* Decide where to allocate the stack memory for pcm_transition */
312 0 : if (mode == MODE_CELT_ONLY)
313 0 : pcm_transition_celt_size = F5*st->channels;
314 : else
315 0 : pcm_transition_silk_size = F5*st->channels;
316 : }
317 0 : ALLOC(pcm_transition_celt, pcm_transition_celt_size, opus_val16);
318 0 : if (transition && mode == MODE_CELT_ONLY)
319 : {
320 0 : pcm_transition = pcm_transition_celt;
321 0 : opus_decode_frame(st, NULL, 0, pcm_transition, IMIN(F5, audiosize), 0);
322 : }
323 0 : 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 0 : return OPUS_BAD_ARG;
328 : } else {
329 0 : frame_size = audiosize;
330 : }
331 :
332 : /* Don't allocate any memory when in CELT-only mode */
333 0 : pcm_silk_size = (mode != MODE_CELT_ONLY && !celt_accum) ? IMAX(F10, frame_size)*st->channels : ALLOC_NONE;
334 0 : ALLOC(pcm_silk, pcm_silk_size, opus_int16);
335 :
336 : /* SILK processing */
337 0 : 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 0 : pcm_ptr = pcm_silk;
347 :
348 0 : if (st->prev_mode==MODE_CELT_ONLY)
349 0 : silk_InitDecoder( silk_dec );
350 :
351 : /* The SILK PLC cannot produce frames of less than 10 ms */
352 0 : st->DecControl.payloadSize_ms = IMAX(10, 1000 * audiosize / st->Fs);
353 :
354 0 : if (data != NULL)
355 : {
356 0 : st->DecControl.nChannelsInternal = st->stream_channels;
357 0 : if( mode == MODE_SILK_ONLY ) {
358 0 : if( st->bandwidth == OPUS_BANDWIDTH_NARROWBAND ) {
359 0 : st->DecControl.internalSampleRate = 8000;
360 0 : } else if( st->bandwidth == OPUS_BANDWIDTH_MEDIUMBAND ) {
361 0 : st->DecControl.internalSampleRate = 12000;
362 0 : } else if( st->bandwidth == OPUS_BANDWIDTH_WIDEBAND ) {
363 0 : st->DecControl.internalSampleRate = 16000;
364 : } else {
365 0 : st->DecControl.internalSampleRate = 16000;
366 0 : silk_assert( 0 );
367 : }
368 : } else {
369 : /* Hybrid mode */
370 0 : st->DecControl.internalSampleRate = 16000;
371 : }
372 : }
373 :
374 0 : lost_flag = data == NULL ? 1 : 2 * decode_fec;
375 0 : decoded_samples = 0;
376 : do {
377 : /* Call SILK decoder */
378 0 : int first_frame = decoded_samples == 0;
379 0 : silk_ret = silk_Decode( silk_dec, &st->DecControl,
380 : lost_flag, first_frame, &dec, pcm_ptr, &silk_frame_size, st->arch );
381 0 : if( silk_ret ) {
382 0 : if (lost_flag) {
383 : /* PLC failure should not be fatal */
384 0 : silk_frame_size = frame_size;
385 0 : for (i=0;i<frame_size*st->channels;i++)
386 0 : pcm_ptr[i] = 0;
387 : } else {
388 : RESTORE_STACK;
389 0 : return OPUS_INTERNAL_ERROR;
390 : }
391 : }
392 0 : pcm_ptr += silk_frame_size * st->channels;
393 0 : decoded_samples += silk_frame_size;
394 0 : } while( decoded_samples < frame_size );
395 : }
396 :
397 0 : start_band = 0;
398 0 : if (!decode_fec && mode != MODE_CELT_ONLY && data != NULL
399 0 : && ec_tell(&dec)+17+20*(st->mode == MODE_HYBRID) <= 8*len)
400 : {
401 : /* Check if we have a redundant 0-8 kHz band */
402 0 : if (mode == MODE_HYBRID)
403 0 : redundancy = ec_dec_bit_logp(&dec, 12);
404 : else
405 0 : redundancy = 1;
406 0 : if (redundancy)
407 : {
408 0 : 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 0 : redundancy_bytes = mode==MODE_HYBRID ?
412 0 : (opus_int32)ec_dec_uint(&dec, 256)+2 :
413 0 : len-((ec_tell(&dec)+7)>>3);
414 0 : 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 0 : if (len*8 < ec_tell(&dec))
418 : {
419 0 : len = 0;
420 0 : redundancy_bytes = 0;
421 0 : redundancy = 0;
422 : }
423 : /* Shrink decoder because of raw bits */
424 0 : dec.storage -= redundancy_bytes;
425 : }
426 : }
427 0 : if (mode != MODE_CELT_ONLY)
428 0 : start_band = 17;
429 :
430 : {
431 0 : int endband=21;
432 :
433 0 : switch(st->bandwidth)
434 : {
435 : case OPUS_BANDWIDTH_NARROWBAND:
436 0 : endband = 13;
437 0 : break;
438 : case OPUS_BANDWIDTH_MEDIUMBAND:
439 : case OPUS_BANDWIDTH_WIDEBAND:
440 0 : endband = 17;
441 0 : break;
442 : case OPUS_BANDWIDTH_SUPERWIDEBAND:
443 0 : endband = 19;
444 0 : break;
445 : case OPUS_BANDWIDTH_FULLBAND:
446 0 : endband = 21;
447 0 : break;
448 : }
449 0 : celt_decoder_ctl(celt_dec, CELT_SET_END_BAND(endband));
450 0 : celt_decoder_ctl(celt_dec, CELT_SET_CHANNELS(st->stream_channels));
451 : }
452 :
453 0 : if (redundancy)
454 : {
455 0 : transition = 0;
456 0 : pcm_transition_silk_size=ALLOC_NONE;
457 : }
458 :
459 0 : ALLOC(pcm_transition_silk, pcm_transition_silk_size, opus_val16);
460 :
461 0 : if (transition && mode != MODE_CELT_ONLY)
462 : {
463 0 : pcm_transition = pcm_transition_silk;
464 0 : opus_decode_frame(st, NULL, 0, pcm_transition, IMIN(F5, audiosize), 0);
465 : }
466 :
467 : /* Only allocation memory for redundancy if/when needed */
468 0 : redundant_audio_size = redundancy ? F5*st->channels : ALLOC_NONE;
469 0 : ALLOC(redundant_audio, redundant_audio_size, opus_val16);
470 :
471 : /* 5 ms redundant frame for CELT->SILK*/
472 0 : if (redundancy && celt_to_silk)
473 : {
474 0 : celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(0));
475 0 : celt_decode_with_ec(celt_dec, data+len, redundancy_bytes,
476 : redundant_audio, F5, NULL, 0);
477 0 : celt_decoder_ctl(celt_dec, OPUS_GET_FINAL_RANGE(&redundant_rng));
478 : }
479 :
480 : /* MUST be after PLC */
481 0 : celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(start_band));
482 :
483 0 : if (mode != MODE_SILK_ONLY)
484 : {
485 0 : int celt_frame_size = IMIN(F20, frame_size);
486 : /* Make sure to discard any previous CELT state */
487 0 : if (mode != st->prev_mode && st->prev_mode > 0 && !st->prev_redundancy)
488 0 : celt_decoder_ctl(celt_dec, OPUS_RESET_STATE);
489 : /* Decode CELT */
490 0 : celt_ret = celt_decode_with_ec(celt_dec, decode_fec ? NULL : data,
491 : len, pcm, celt_frame_size, &dec, celt_accum);
492 : } else {
493 0 : unsigned char silence[2] = {0xFF, 0xFF};
494 0 : if (!celt_accum)
495 : {
496 0 : for (i=0;i<frame_size*st->channels;i++)
497 0 : 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 0 : if (st->prev_mode == MODE_HYBRID && !(redundancy && celt_to_silk && st->prev_redundancy) )
502 : {
503 0 : celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(0));
504 0 : celt_decode_with_ec(celt_dec, silence, 2, pcm, F2_5, NULL, celt_accum);
505 : }
506 : }
507 :
508 0 : 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 0 : for (i=0;i<frame_size*st->channels;i++)
515 0 : pcm[i] = pcm[i] + (opus_val16)((1.f/32768.f)*pcm_silk[i]);
516 : #endif
517 : }
518 :
519 : {
520 : const CELTMode *celt_mode;
521 0 : celt_decoder_ctl(celt_dec, CELT_GET_MODE(&celt_mode));
522 0 : window = celt_mode->window;
523 : }
524 :
525 : /* 5 ms redundant frame for SILK->CELT */
526 0 : if (redundancy && !celt_to_silk)
527 : {
528 0 : celt_decoder_ctl(celt_dec, OPUS_RESET_STATE);
529 0 : celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(0));
530 :
531 0 : celt_decode_with_ec(celt_dec, data+len, redundancy_bytes, redundant_audio, F5, NULL, 0);
532 0 : celt_decoder_ctl(celt_dec, OPUS_GET_FINAL_RANGE(&redundant_rng));
533 0 : smooth_fade(pcm+st->channels*(frame_size-F2_5), redundant_audio+st->channels*F2_5,
534 0 : pcm+st->channels*(frame_size-F2_5), F2_5, st->channels, window, st->Fs);
535 : }
536 0 : if (redundancy && celt_to_silk)
537 : {
538 0 : for (c=0;c<st->channels;c++)
539 : {
540 0 : for (i=0;i<F2_5;i++)
541 0 : pcm[st->channels*i+c] = redundant_audio[st->channels*i+c];
542 : }
543 0 : smooth_fade(redundant_audio+st->channels*F2_5, pcm+st->channels*F2_5,
544 0 : pcm+st->channels*F2_5, F2_5, st->channels, window, st->Fs);
545 : }
546 0 : if (transition)
547 : {
548 0 : if (audiosize >= F5)
549 : {
550 0 : for (i=0;i<st->channels*F2_5;i++)
551 0 : pcm[i] = pcm_transition[i];
552 0 : smooth_fade(pcm_transition+st->channels*F2_5, pcm+st->channels*F2_5,
553 0 : 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 0 : smooth_fade(pcm_transition, pcm,
562 : pcm, F2_5,
563 : st->channels, window, st->Fs);
564 : }
565 : }
566 :
567 0 : if(st->decode_gain)
568 : {
569 : opus_val32 gain;
570 0 : gain = celt_exp2(MULT16_16_P15(QCONST16(6.48814081e-4f, 25), st->decode_gain));
571 0 : for (i=0;i<frame_size*st->channels;i++)
572 : {
573 : opus_val32 x;
574 0 : x = MULT16_32_P16(pcm[i],gain);
575 0 : pcm[i] = SATURATE(x, 32767);
576 : }
577 : }
578 :
579 0 : if (len <= 1)
580 0 : st->rangeFinal = 0;
581 : else
582 0 : st->rangeFinal = dec.rng ^ redundant_rng;
583 :
584 0 : st->prev_mode = mode;
585 0 : st->prev_redundancy = redundancy && !celt_to_silk;
586 :
587 0 : if (celt_ret>=0)
588 : {
589 0 : if (OPUS_CHECK_ARRAY(pcm, audiosize*st->channels))
590 : OPUS_PRINT_INT(audiosize);
591 : }
592 :
593 : RESTORE_STACK;
594 0 : return celt_ret < 0 ? celt_ret : audiosize;
595 :
596 : }
597 :
598 0 : int 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 0 : if (decode_fec<0 || decode_fec>1)
609 0 : return OPUS_BAD_ARG;
610 : /* For FEC/PLC, frame_size has to be to have a multiple of 2.5 ms */
611 0 : if ((decode_fec || len==0 || data==NULL) && frame_size%(st->Fs/400)!=0)
612 0 : return OPUS_BAD_ARG;
613 0 : if (len==0 || data==NULL)
614 : {
615 0 : int pcm_count=0;
616 : do {
617 : int ret;
618 0 : ret = opus_decode_frame(st, NULL, 0, pcm+pcm_count*st->channels, frame_size-pcm_count, 0);
619 0 : if (ret<0)
620 0 : return ret;
621 0 : pcm_count += ret;
622 0 : } while (pcm_count < frame_size);
623 0 : celt_assert(pcm_count == frame_size);
624 0 : if (OPUS_CHECK_ARRAY(pcm, pcm_count*st->channels))
625 : OPUS_PRINT_INT(pcm_count);
626 0 : st->last_packet_duration = pcm_count;
627 0 : return pcm_count;
628 0 : } else if (len<0)
629 0 : return OPUS_BAD_ARG;
630 :
631 0 : packet_mode = opus_packet_get_mode(data);
632 0 : packet_bandwidth = opus_packet_get_bandwidth(data);
633 0 : packet_frame_size = opus_packet_get_samples_per_frame(data, st->Fs);
634 0 : packet_stream_channels = opus_packet_get_nb_channels(data);
635 :
636 0 : count = opus_packet_parse_impl(data, len, self_delimited, &toc, NULL,
637 : size, &offset, packet_offset);
638 0 : if (count<0)
639 0 : return count;
640 :
641 0 : data += offset;
642 :
643 0 : if (decode_fec)
644 : {
645 : int duration_copy;
646 : int ret;
647 : /* If no FEC can be present, run the PLC (recursive call) */
648 0 : if (frame_size < packet_frame_size || packet_mode == MODE_CELT_ONLY || st->mode == MODE_CELT_ONLY)
649 0 : 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 0 : duration_copy = st->last_packet_duration;
652 0 : if (frame_size-packet_frame_size!=0)
653 : {
654 0 : ret = opus_decode_native(st, NULL, 0, pcm, frame_size-packet_frame_size, 0, 0, NULL, soft_clip);
655 0 : if (ret<0)
656 : {
657 0 : st->last_packet_duration = duration_copy;
658 0 : return ret;
659 : }
660 0 : celt_assert(ret==frame_size-packet_frame_size);
661 : }
662 : /* Complete with FEC */
663 0 : st->mode = packet_mode;
664 0 : st->bandwidth = packet_bandwidth;
665 0 : st->frame_size = packet_frame_size;
666 0 : st->stream_channels = packet_stream_channels;
667 0 : ret = opus_decode_frame(st, data, size[0], pcm+st->channels*(frame_size-packet_frame_size),
668 : packet_frame_size, 1);
669 0 : if (ret<0)
670 0 : return ret;
671 : else {
672 0 : if (OPUS_CHECK_ARRAY(pcm, frame_size*st->channels))
673 : OPUS_PRINT_INT(frame_size);
674 0 : st->last_packet_duration = frame_size;
675 0 : return frame_size;
676 : }
677 : }
678 :
679 0 : if (count*packet_frame_size > frame_size)
680 0 : return OPUS_BUFFER_TOO_SMALL;
681 :
682 : /* Update the state as the last step to avoid updating it on an invalid packet */
683 0 : st->mode = packet_mode;
684 0 : st->bandwidth = packet_bandwidth;
685 0 : st->frame_size = packet_frame_size;
686 0 : st->stream_channels = packet_stream_channels;
687 :
688 0 : nb_samples=0;
689 0 : for (i=0;i<count;i++)
690 : {
691 : int ret;
692 0 : ret = opus_decode_frame(st, data, size[i], pcm+nb_samples*st->channels, frame_size-nb_samples, 0);
693 0 : if (ret<0)
694 0 : return ret;
695 0 : celt_assert(ret==packet_frame_size);
696 0 : data += size[i];
697 0 : nb_samples += ret;
698 : }
699 0 : st->last_packet_duration = nb_samples;
700 0 : if (OPUS_CHECK_ARRAY(pcm, nb_samples*st->channels))
701 : OPUS_PRINT_INT(nb_samples);
702 : #ifndef FIXED_POINT
703 0 : if (soft_clip)
704 0 : opus_pcm_soft_clip(pcm, nb_samples, st->channels, st->softclip_mem);
705 : else
706 0 : st->softclip_mem[0]=st->softclip_mem[1]=0;
707 : #endif
708 0 : return nb_samples;
709 : }
710 :
711 : #ifdef FIXED_POINT
712 :
713 : int 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
722 : int 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
758 0 : int 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 0 : if(frame_size<=0)
767 : {
768 : RESTORE_STACK;
769 0 : return OPUS_BAD_ARG;
770 : }
771 :
772 0 : if (data != NULL && len > 0 && !decode_fec)
773 : {
774 0 : nb_samples = opus_decoder_get_nb_samples(st, data, len);
775 0 : if (nb_samples>0)
776 0 : frame_size = IMIN(frame_size, nb_samples);
777 : else
778 0 : return OPUS_INVALID_PACKET;
779 : }
780 0 : ALLOC(out, frame_size*st->channels, float);
781 :
782 0 : ret = opus_decode_native(st, data, len, out, frame_size, decode_fec, 0, NULL, 1);
783 0 : if (ret > 0)
784 : {
785 0 : for (i=0;i<ret*st->channels;i++)
786 0 : pcm[i] = FLOAT2INT16(out[i]);
787 : }
788 : RESTORE_STACK;
789 0 : return ret;
790 : }
791 :
792 0 : int opus_decode_float(OpusDecoder *st, const unsigned char *data,
793 : opus_int32 len, opus_val16 *pcm, int frame_size, int decode_fec)
794 : {
795 0 : if(frame_size<=0)
796 0 : return OPUS_BAD_ARG;
797 0 : return opus_decode_native(st, data, len, pcm, frame_size, decode_fec, 0, NULL, 0);
798 : }
799 :
800 : #endif
801 :
802 0 : int opus_decoder_ctl(OpusDecoder *st, int request, ...)
803 : {
804 0 : int ret = OPUS_OK;
805 : va_list ap;
806 : void *silk_dec;
807 : CELTDecoder *celt_dec;
808 :
809 0 : silk_dec = (char*)st+st->silk_dec_offset;
810 0 : celt_dec = (CELTDecoder*)((char*)st+st->celt_dec_offset);
811 :
812 :
813 0 : va_start(ap, request);
814 :
815 0 : switch (request)
816 : {
817 : case OPUS_GET_BANDWIDTH_REQUEST:
818 : {
819 0 : opus_int32 *value = va_arg(ap, opus_int32*);
820 0 : if (!value)
821 : {
822 0 : goto bad_arg;
823 : }
824 0 : *value = st->bandwidth;
825 : }
826 0 : break;
827 : case OPUS_GET_FINAL_RANGE_REQUEST:
828 : {
829 0 : opus_uint32 *value = va_arg(ap, opus_uint32*);
830 0 : if (!value)
831 : {
832 0 : goto bad_arg;
833 : }
834 0 : *value = st->rangeFinal;
835 : }
836 0 : break;
837 : case OPUS_RESET_STATE:
838 : {
839 0 : OPUS_CLEAR((char*)&st->OPUS_DECODER_RESET_START,
840 : sizeof(OpusDecoder)-
841 : ((char*)&st->OPUS_DECODER_RESET_START - (char*)st));
842 :
843 0 : celt_decoder_ctl(celt_dec, OPUS_RESET_STATE);
844 0 : silk_InitDecoder( silk_dec );
845 0 : st->stream_channels = st->channels;
846 0 : st->frame_size = st->Fs/400;
847 : }
848 0 : break;
849 : case OPUS_GET_SAMPLE_RATE_REQUEST:
850 : {
851 0 : opus_int32 *value = va_arg(ap, opus_int32*);
852 0 : if (!value)
853 : {
854 0 : goto bad_arg;
855 : }
856 0 : *value = st->Fs;
857 : }
858 0 : break;
859 : case OPUS_GET_PITCH_REQUEST:
860 : {
861 0 : opus_int32 *value = va_arg(ap, opus_int32*);
862 0 : if (!value)
863 : {
864 0 : goto bad_arg;
865 : }
866 0 : if (st->prev_mode == MODE_CELT_ONLY)
867 0 : celt_decoder_ctl(celt_dec, OPUS_GET_PITCH(value));
868 : else
869 0 : *value = st->DecControl.prevPitchLag;
870 : }
871 0 : break;
872 : case OPUS_GET_GAIN_REQUEST:
873 : {
874 0 : opus_int32 *value = va_arg(ap, opus_int32*);
875 0 : if (!value)
876 : {
877 0 : goto bad_arg;
878 : }
879 0 : *value = st->decode_gain;
880 : }
881 0 : break;
882 : case OPUS_SET_GAIN_REQUEST:
883 : {
884 0 : opus_int32 value = va_arg(ap, opus_int32);
885 0 : if (value<-32768 || value>32767)
886 : {
887 : goto bad_arg;
888 : }
889 0 : st->decode_gain = value;
890 : }
891 0 : break;
892 : case OPUS_GET_LAST_PACKET_DURATION_REQUEST:
893 : {
894 0 : opus_int32 *value = va_arg(ap, opus_int32*);
895 0 : if (!value)
896 : {
897 0 : goto bad_arg;
898 : }
899 0 : *value = st->last_packet_duration;
900 : }
901 0 : break;
902 : case OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST:
903 : {
904 0 : opus_int32 value = va_arg(ap, opus_int32);
905 0 : if(value<0 || value>1)
906 : {
907 : goto bad_arg;
908 : }
909 0 : celt_decoder_ctl(celt_dec, OPUS_SET_PHASE_INVERSION_DISABLED(value));
910 : }
911 0 : break;
912 : case OPUS_GET_PHASE_INVERSION_DISABLED_REQUEST:
913 : {
914 0 : opus_int32 *value = va_arg(ap, opus_int32*);
915 0 : if (!value)
916 : {
917 0 : goto bad_arg;
918 : }
919 0 : celt_decoder_ctl(celt_dec, OPUS_GET_PHASE_INVERSION_DISABLED(value));
920 : }
921 0 : break;
922 : default:
923 : /*fprintf(stderr, "unknown opus_decoder_ctl() request: %d", request);*/
924 0 : ret = OPUS_UNIMPLEMENTED;
925 0 : break;
926 : }
927 :
928 0 : va_end(ap);
929 0 : return ret;
930 : bad_arg:
931 0 : va_end(ap);
932 0 : return OPUS_BAD_ARG;
933 : }
934 :
935 0 : void opus_decoder_destroy(OpusDecoder *st)
936 : {
937 0 : opus_free(st);
938 0 : }
939 :
940 :
941 0 : int opus_packet_get_bandwidth(const unsigned char *data)
942 : {
943 : int bandwidth;
944 0 : if (data[0]&0x80)
945 : {
946 0 : bandwidth = OPUS_BANDWIDTH_MEDIUMBAND + ((data[0]>>5)&0x3);
947 0 : if (bandwidth == OPUS_BANDWIDTH_MEDIUMBAND)
948 0 : bandwidth = OPUS_BANDWIDTH_NARROWBAND;
949 0 : } else if ((data[0]&0x60) == 0x60)
950 : {
951 0 : bandwidth = (data[0]&0x10) ? OPUS_BANDWIDTH_FULLBAND :
952 : OPUS_BANDWIDTH_SUPERWIDEBAND;
953 : } else {
954 0 : bandwidth = OPUS_BANDWIDTH_NARROWBAND + ((data[0]>>5)&0x3);
955 : }
956 0 : return bandwidth;
957 : }
958 :
959 0 : int opus_packet_get_nb_channels(const unsigned char *data)
960 : {
961 0 : return (data[0]&0x4) ? 2 : 1;
962 : }
963 :
964 0 : int opus_packet_get_nb_frames(const unsigned char packet[], opus_int32 len)
965 : {
966 : int count;
967 0 : if (len<1)
968 0 : return OPUS_BAD_ARG;
969 0 : count = packet[0]&0x3;
970 0 : if (count==0)
971 0 : return 1;
972 0 : else if (count!=3)
973 0 : return 2;
974 0 : else if (len<2)
975 0 : return OPUS_INVALID_PACKET;
976 : else
977 0 : return packet[1]&0x3F;
978 : }
979 :
980 0 : int opus_packet_get_nb_samples(const unsigned char packet[], opus_int32 len,
981 : opus_int32 Fs)
982 : {
983 : int samples;
984 0 : int count = opus_packet_get_nb_frames(packet, len);
985 :
986 0 : if (count<0)
987 0 : return count;
988 :
989 0 : samples = count*opus_packet_get_samples_per_frame(packet, Fs);
990 : /* Can't have more than 120 ms */
991 0 : if (samples*25 > Fs*3)
992 0 : return OPUS_INVALID_PACKET;
993 : else
994 0 : return samples;
995 : }
996 :
997 0 : int opus_decoder_get_nb_samples(const OpusDecoder *dec,
998 : const unsigned char packet[], opus_int32 len)
999 : {
1000 0 : return opus_packet_get_nb_samples(packet, len, dec->Fs);
1001 : }
|