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