media/libopus/src/opus_decoder.c

Thu, 15 Jan 2015 15:59:08 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:59:08 +0100
branch
TOR_BUG_9701
changeset 10
ac0c01689b40
permissions
-rw-r--r--

Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

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

mercurial