summaryrefslogtreecommitdiff
path: root/lib/rbcodec/codecs/libopus/opus_decoder.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/rbcodec/codecs/libopus/opus_decoder.c')
-rw-r--r--lib/rbcodec/codecs/libopus/opus_decoder.c134
1 files changed, 90 insertions, 44 deletions
diff --git a/lib/rbcodec/codecs/libopus/opus_decoder.c b/lib/rbcodec/codecs/libopus/opus_decoder.c
index 5d35ed2675..9113638a00 100644
--- a/lib/rbcodec/codecs/libopus/opus_decoder.c
+++ b/lib/rbcodec/codecs/libopus/opus_decoder.c
@@ -33,7 +33,7 @@
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." 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 34#endif
35 35
36#if defined(__GNUC__) && (__GNUC__ >= 2) && !defined(__OPTIMIZE__) 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." 37# pragma message "You appear to be compiling without optimization, if so opus will be very slow."
38#endif 38#endif
39 39
@@ -59,6 +59,7 @@ struct OpusDecoder {
59 opus_int32 Fs; /** Sampling rate (at the API level) */ 59 opus_int32 Fs; /** Sampling rate (at the API level) */
60 silk_DecControlStruct DecControl; 60 silk_DecControlStruct DecControl;
61 int decode_gain; 61 int decode_gain;
62 int arch;
62 63
63 /* Everything beyond this point gets cleared on a reset */ 64 /* Everything beyond this point gets cleared on a reset */
64#define OPUS_DECODER_RESET_START stream_channels 65#define OPUS_DECODER_RESET_START stream_channels
@@ -77,6 +78,26 @@ struct OpusDecoder {
77 opus_uint32 rangeFinal; 78 opus_uint32 rangeFinal;
78}; 79};
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
80 101
81int opus_decoder_get_size(int channels) 102int opus_decoder_get_size(int channels)
82{ 103{
@@ -103,7 +124,7 @@ int opus_decoder_init(OpusDecoder *st, opus_int32 Fs, int channels)
103 return OPUS_BAD_ARG; 124 return OPUS_BAD_ARG;
104 125
105 OPUS_CLEAR((char*)st, opus_decoder_get_size(channels)); 126 OPUS_CLEAR((char*)st, opus_decoder_get_size(channels));
106 /* Initialize SILK encoder */ 127 /* Initialize SILK decoder */
107 ret = silk_Get_Decoder_Size(&silkDecSizeBytes); 128 ret = silk_Get_Decoder_Size(&silkDecSizeBytes);
108 if (ret) 129 if (ret)
109 return OPUS_INTERNAL_ERROR; 130 return OPUS_INTERNAL_ERROR;
@@ -131,16 +152,14 @@ int opus_decoder_init(OpusDecoder *st, opus_int32 Fs, int channels)
131 152
132 st->prev_mode = 0; 153 st->prev_mode = 0;
133 st->frame_size = Fs/400; 154 st->frame_size = Fs/400;
155 st->arch = opus_select_arch();
134 return OPUS_OK; 156 return OPUS_OK;
135} 157}
136 158
137#define STATIC_DECODER_SIZE 26540 /* 26540 for 64bit environment */
138static char s_dec[STATIC_DECODER_SIZE] IBSS_ATTR MEM_ALIGN_ATTR;
139
140OpusDecoder *opus_decoder_create(opus_int32 Fs, int channels, int *error) 159OpusDecoder *opus_decoder_create(opus_int32 Fs, int channels, int *error)
141{ 160{
142 int ret; 161 int ret;
143 OpusDecoder *st = NULL; 162 OpusDecoder *st;
144 if ((Fs!=48000&&Fs!=24000&&Fs!=16000&&Fs!=12000&&Fs!=8000) 163 if ((Fs!=48000&&Fs!=24000&&Fs!=16000&&Fs!=12000&&Fs!=8000)
145 || (channels!=1&&channels!=2)) 164 || (channels!=1&&channels!=2))
146 { 165 {
@@ -148,11 +167,7 @@ OpusDecoder *opus_decoder_create(opus_int32 Fs, int channels, int *error)
148 *error = OPUS_BAD_ARG; 167 *error = OPUS_BAD_ARG;
149 return NULL; 168 return NULL;
150 } 169 }
151 if (STATIC_DECODER_SIZE >= opus_decoder_get_size(channels)) 170 st = (OpusDecoder *)opus_alloc(opus_decoder_get_size(channels));
152 st = (OpusDecoder *)s_dec;
153 else
154 st = (OpusDecoder *)opus_alloc(opus_decoder_get_size(channels));
155
156 if (st == NULL) 171 if (st == NULL)
157 { 172 {
158 if (error) 173 if (error)
@@ -222,6 +237,7 @@ static int opus_decode_frame(OpusDecoder *st, const unsigned char *data,
222 237
223 int audiosize; 238 int audiosize;
224 int mode; 239 int mode;
240 int bandwidth;
225 int transition=0; 241 int transition=0;
226 int start_band; 242 int start_band;
227 int redundancy=0; 243 int redundancy=0;
@@ -258,10 +274,12 @@ static int opus_decode_frame(OpusDecoder *st, const unsigned char *data,
258 { 274 {
259 audiosize = st->frame_size; 275 audiosize = st->frame_size;
260 mode = st->mode; 276 mode = st->mode;
277 bandwidth = st->bandwidth;
261 ec_dec_init(&dec,(unsigned char*)data,len); 278 ec_dec_init(&dec,(unsigned char*)data,len);
262 } else { 279 } else {
263 audiosize = frame_size; 280 audiosize = frame_size;
264 mode = st->prev_mode; 281 mode = st->prev_mode;
282 bandwidth = 0;
265 283
266 if (mode == 0) 284 if (mode == 0)
267 { 285 {
@@ -360,15 +378,15 @@ static int opus_decode_frame(OpusDecoder *st, const unsigned char *data,
360 { 378 {
361 st->DecControl.nChannelsInternal = st->stream_channels; 379 st->DecControl.nChannelsInternal = st->stream_channels;
362 if( mode == MODE_SILK_ONLY ) { 380 if( mode == MODE_SILK_ONLY ) {
363 if( st->bandwidth == OPUS_BANDWIDTH_NARROWBAND ) { 381 if( bandwidth == OPUS_BANDWIDTH_NARROWBAND ) {
364 st->DecControl.internalSampleRate = 8000; 382 st->DecControl.internalSampleRate = 8000;
365 } else if( st->bandwidth == OPUS_BANDWIDTH_MEDIUMBAND ) { 383 } else if( bandwidth == OPUS_BANDWIDTH_MEDIUMBAND ) {
366 st->DecControl.internalSampleRate = 12000; 384 st->DecControl.internalSampleRate = 12000;
367 } else if( st->bandwidth == OPUS_BANDWIDTH_WIDEBAND ) { 385 } else if( bandwidth == OPUS_BANDWIDTH_WIDEBAND ) {
368 st->DecControl.internalSampleRate = 16000; 386 st->DecControl.internalSampleRate = 16000;
369 } else { 387 } else {
370 st->DecControl.internalSampleRate = 16000; 388 st->DecControl.internalSampleRate = 16000;
371 silk_assert( 0 ); 389 celt_assert( 0 );
372 } 390 }
373 } else { 391 } else {
374 /* Hybrid mode */ 392 /* Hybrid mode */
@@ -382,7 +400,7 @@ static int opus_decode_frame(OpusDecoder *st, const unsigned char *data,
382 /* Call SILK decoder */ 400 /* Call SILK decoder */
383 int first_frame = decoded_samples == 0; 401 int first_frame = decoded_samples == 0;
384 silk_ret = silk_Decode( silk_dec, &st->DecControl, 402 silk_ret = silk_Decode( silk_dec, &st->DecControl,
385 lost_flag, first_frame, &dec, pcm_ptr, &silk_frame_size ); 403 lost_flag, first_frame, &dec, pcm_ptr, &silk_frame_size, st->arch );
386 if( silk_ret ) { 404 if( silk_ret ) {
387 if (lost_flag) { 405 if (lost_flag) {
388 /* PLC failure should not be fatal */ 406 /* PLC failure should not be fatal */
@@ -432,10 +450,26 @@ static int opus_decode_frame(OpusDecoder *st, const unsigned char *data,
432 if (mode != MODE_CELT_ONLY) 450 if (mode != MODE_CELT_ONLY)
433 start_band = 17; 451 start_band = 17;
434 452
453 if (redundancy)
454 {
455 transition = 0;
456 pcm_transition_silk_size=ALLOC_NONE;
457 }
458
459 ALLOC(pcm_transition_silk, pcm_transition_silk_size, opus_val16);
460
461 if (transition && mode != MODE_CELT_ONLY)
462 {
463 pcm_transition = pcm_transition_silk;
464 opus_decode_frame(st, NULL, 0, pcm_transition, IMIN(F5, audiosize), 0);
465 }
466
467
468 if (bandwidth)
435 { 469 {
436 int endband=21; 470 int endband=21;
437 471
438 switch(st->bandwidth) 472 switch(bandwidth)
439 { 473 {
440 case OPUS_BANDWIDTH_NARROWBAND: 474 case OPUS_BANDWIDTH_NARROWBAND:
441 endband = 13; 475 endband = 13;
@@ -450,24 +484,13 @@ static int opus_decode_frame(OpusDecoder *st, const unsigned char *data,
450 case OPUS_BANDWIDTH_FULLBAND: 484 case OPUS_BANDWIDTH_FULLBAND:
451 endband = 21; 485 endband = 21;
452 break; 486 break;
487 default:
488 celt_assert(0);
489 break;
453 } 490 }
454 celt_decoder_ctl(celt_dec, CELT_SET_END_BAND(endband)); 491 MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_SET_END_BAND(endband)));
455 celt_decoder_ctl(celt_dec, CELT_SET_CHANNELS(st->stream_channels));
456 }
457
458 if (redundancy)
459 {
460 transition = 0;
461 pcm_transition_silk_size=ALLOC_NONE;
462 }
463
464 ALLOC(pcm_transition_silk, pcm_transition_silk_size, opus_val16);
465
466 if (transition && mode != MODE_CELT_ONLY)
467 {
468 pcm_transition = pcm_transition_silk;
469 opus_decode_frame(st, NULL, 0, pcm_transition, IMIN(F5, audiosize), 0);
470 } 492 }
493 MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_SET_CHANNELS(st->stream_channels)));
471 494
472 /* Only allocation memory for redundancy if/when needed */ 495 /* Only allocation memory for redundancy if/when needed */
473 redundant_audio_size = redundancy ? F5*st->channels : ALLOC_NONE; 496 redundant_audio_size = redundancy ? F5*st->channels : ALLOC_NONE;
@@ -476,21 +499,21 @@ static int opus_decode_frame(OpusDecoder *st, const unsigned char *data,
476 /* 5 ms redundant frame for CELT->SILK*/ 499 /* 5 ms redundant frame for CELT->SILK*/
477 if (redundancy && celt_to_silk) 500 if (redundancy && celt_to_silk)
478 { 501 {
479 celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(0)); 502 MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(0)));
480 celt_decode_with_ec(celt_dec, data+len, redundancy_bytes, 503 celt_decode_with_ec(celt_dec, data+len, redundancy_bytes,
481 redundant_audio, F5, NULL, 0); 504 redundant_audio, F5, NULL, 0);
482 celt_decoder_ctl(celt_dec, OPUS_GET_FINAL_RANGE(&redundant_rng)); 505 MUST_SUCCEED(celt_decoder_ctl(celt_dec, OPUS_GET_FINAL_RANGE(&redundant_rng)));
483 } 506 }
484 507
485 /* MUST be after PLC */ 508 /* MUST be after PLC */
486 celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(start_band)); 509 MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(start_band)));
487 510
488 if (mode != MODE_SILK_ONLY) 511 if (mode != MODE_SILK_ONLY)
489 { 512 {
490 int celt_frame_size = IMIN(F20, frame_size); 513 int celt_frame_size = IMIN(F20, frame_size);
491 /* Make sure to discard any previous CELT state */ 514 /* Make sure to discard any previous CELT state */
492 if (mode != st->prev_mode && st->prev_mode > 0 && !st->prev_redundancy) 515 if (mode != st->prev_mode && st->prev_mode > 0 && !st->prev_redundancy)
493 celt_decoder_ctl(celt_dec, OPUS_RESET_STATE); 516 MUST_SUCCEED(celt_decoder_ctl(celt_dec, OPUS_RESET_STATE));
494 /* Decode CELT */ 517 /* Decode CELT */
495 celt_ret = celt_decode_with_ec(celt_dec, decode_fec ? NULL : data, 518 celt_ret = celt_decode_with_ec(celt_dec, decode_fec ? NULL : data,
496 len, pcm, celt_frame_size, &dec, celt_accum); 519 len, pcm, celt_frame_size, &dec, celt_accum);
@@ -505,7 +528,7 @@ static int opus_decode_frame(OpusDecoder *st, const unsigned char *data,
505 do a fade-out by decoding a silence frame */ 528 do a fade-out by decoding a silence frame */
506 if (st->prev_mode == MODE_HYBRID && !(redundancy && celt_to_silk && st->prev_redundancy) ) 529 if (st->prev_mode == MODE_HYBRID && !(redundancy && celt_to_silk && st->prev_redundancy) )
507 { 530 {
508 celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(0)); 531 MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(0)));
509 celt_decode_with_ec(celt_dec, silence, 2, pcm, F2_5, NULL, celt_accum); 532 celt_decode_with_ec(celt_dec, silence, 2, pcm, F2_5, NULL, celt_accum);
510 } 533 }
511 } 534 }
@@ -523,18 +546,18 @@ static int opus_decode_frame(OpusDecoder *st, const unsigned char *data,
523 546
524 { 547 {
525 const CELTMode *celt_mode; 548 const CELTMode *celt_mode;
526 celt_decoder_ctl(celt_dec, CELT_GET_MODE(&celt_mode)); 549 MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_GET_MODE(&celt_mode)));
527 window = celt_mode->window; 550 window = celt_mode->window;
528 } 551 }
529 552
530 /* 5 ms redundant frame for SILK->CELT */ 553 /* 5 ms redundant frame for SILK->CELT */
531 if (redundancy && !celt_to_silk) 554 if (redundancy && !celt_to_silk)
532 { 555 {
533 celt_decoder_ctl(celt_dec, OPUS_RESET_STATE); 556 MUST_SUCCEED(celt_decoder_ctl(celt_dec, OPUS_RESET_STATE));
534 celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(0)); 557 MUST_SUCCEED(celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(0)));
535 558
536 celt_decode_with_ec(celt_dec, data+len, redundancy_bytes, redundant_audio, F5, NULL, 0); 559 celt_decode_with_ec(celt_dec, data+len, redundancy_bytes, redundant_audio, F5, NULL, 0);
537 celt_decoder_ctl(celt_dec, OPUS_GET_FINAL_RANGE(&redundant_rng)); 560 MUST_SUCCEED(celt_decoder_ctl(celt_dec, OPUS_GET_FINAL_RANGE(&redundant_rng)));
538 smooth_fade(pcm+st->channels*(frame_size-F2_5), redundant_audio+st->channels*F2_5, 561 smooth_fade(pcm+st->channels*(frame_size-F2_5), redundant_audio+st->channels*F2_5,
539 pcm+st->channels*(frame_size-F2_5), F2_5, st->channels, window, st->Fs); 562 pcm+st->channels*(frame_size-F2_5), F2_5, st->channels, window, st->Fs);
540 } 563 }
@@ -610,6 +633,7 @@ int opus_decode_native(OpusDecoder *st, const unsigned char *data,
610 int packet_frame_size, packet_bandwidth, packet_mode, packet_stream_channels; 633 int packet_frame_size, packet_bandwidth, packet_mode, packet_stream_channels;
611 /* 48 x 2.5 ms = 120 ms */ 634 /* 48 x 2.5 ms = 120 ms */
612 opus_int16 size[48]; 635 opus_int16 size[48];
636 VALIDATE_OPUS_DECODER(st);
613 if (decode_fec<0 || decode_fec>1) 637 if (decode_fec<0 || decode_fec>1)
614 return OPUS_BAD_ARG; 638 return OPUS_BAD_ARG;
615 /* For FEC/PLC, frame_size has to be to have a multiple of 2.5 ms */ 639 /* For FEC/PLC, frame_size has to be to have a multiple of 2.5 ms */
@@ -745,6 +769,7 @@ int opus_decode_float(OpusDecoder *st, const unsigned char *data,
745 else 769 else
746 return OPUS_INVALID_PACKET; 770 return OPUS_INVALID_PACKET;
747 } 771 }
772 celt_assert(st->channels == 1 || st->channels == 2);
748 ALLOC(out, frame_size*st->channels, opus_int16); 773 ALLOC(out, frame_size*st->channels, opus_int16);
749 774
750 ret = opus_decode_native(st, data, len, out, frame_size, decode_fec, 0, NULL, 0); 775 ret = opus_decode_native(st, data, len, out, frame_size, decode_fec, 0, NULL, 0);
@@ -782,6 +807,7 @@ int opus_decode(OpusDecoder *st, const unsigned char *data,
782 else 807 else
783 return OPUS_INVALID_PACKET; 808 return OPUS_INVALID_PACKET;
784 } 809 }
810 celt_assert(st->channels == 1 || st->channels == 2);
785 ALLOC(out, frame_size*st->channels, float); 811 ALLOC(out, frame_size*st->channels, float);
786 812
787 ret = opus_decode_native(st, data, len, out, frame_size, decode_fec, 0, NULL, 1); 813 ret = opus_decode_native(st, data, len, out, frame_size, decode_fec, 0, NULL, 1);
@@ -869,7 +895,7 @@ int opus_decoder_ctl(OpusDecoder *st, int request, ...)
869 goto bad_arg; 895 goto bad_arg;
870 } 896 }
871 if (st->prev_mode == MODE_CELT_ONLY) 897 if (st->prev_mode == MODE_CELT_ONLY)
872 celt_decoder_ctl(celt_dec, OPUS_GET_PITCH(value)); 898 ret = celt_decoder_ctl(celt_dec, OPUS_GET_PITCH(value));
873 else 899 else
874 *value = st->DecControl.prevPitchLag; 900 *value = st->DecControl.prevPitchLag;
875 } 901 }
@@ -896,7 +922,7 @@ int opus_decoder_ctl(OpusDecoder *st, int request, ...)
896 break; 922 break;
897 case OPUS_GET_LAST_PACKET_DURATION_REQUEST: 923 case OPUS_GET_LAST_PACKET_DURATION_REQUEST:
898 { 924 {
899 opus_uint32 *value = va_arg(ap, opus_uint32*); 925 opus_int32 *value = va_arg(ap, opus_int32*);
900 if (!value) 926 if (!value)
901 { 927 {
902 goto bad_arg; 928 goto bad_arg;
@@ -904,6 +930,26 @@ int opus_decoder_ctl(OpusDecoder *st, int request, ...)
904 *value = st->last_packet_duration; 930 *value = st->last_packet_duration;
905 } 931 }
906 break; 932 break;
933 case OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST:
934 {
935 opus_int32 value = va_arg(ap, opus_int32);
936 if(value<0 || value>1)
937 {
938 goto bad_arg;
939 }
940 ret = celt_decoder_ctl(celt_dec, OPUS_SET_PHASE_INVERSION_DISABLED(value));
941 }
942 break;
943 case OPUS_GET_PHASE_INVERSION_DISABLED_REQUEST:
944 {
945 opus_int32 *value = va_arg(ap, opus_int32*);
946 if (!value)
947 {
948 goto bad_arg;
949 }
950 ret = celt_decoder_ctl(celt_dec, OPUS_GET_PHASE_INVERSION_DISABLED(value));
951 }
952 break;
907 default: 953 default:
908 /*fprintf(stderr, "unknown opus_decoder_ctl() request: %d", request);*/ 954 /*fprintf(stderr, "unknown opus_decoder_ctl() request: %d", request);*/
909 ret = OPUS_UNIMPLEMENTED; 955 ret = OPUS_UNIMPLEMENTED;