1 /* Copyright (c) 2010 Xiph.Org Foundation, Skype Limited
2 Written by Jean-Marc Valin and Koen Vos */
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
8 - Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
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.
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.
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."
42 #include "stack_alloc.h"
43 #include "float_cast.h"
44 #include "opus_private.h"
45 #include "os_support.h"
49 #include "cpu_support.h"
55 opus_int32 Fs; /** Sampling rate (at the API level) */
56 silk_DecControlStruct DecControl;
59 /* Everything beyond this point gets cleared on a reset */
60 #define OPUS_DECODER_RESET_START stream_channels
68 int last_packet_duration;
70 opus_val16 softclip_mem[2];
73 opus_uint32 rangeFinal;
78 static inline opus_int16 SAT16(opus_int32 x) {
79 return x > 32767 ? 32767 : x < -32768 ? -32768 : (opus_int16)x;
84 int opus_decoder_get_size(int channels)
86 int silkDecSizeBytes, celtDecSizeBytes;
88 if (channels<1 || channels > 2)
90 ret = silk_Get_Decoder_Size( &silkDecSizeBytes );
93 silkDecSizeBytes = align(silkDecSizeBytes);
94 celtDecSizeBytes = celt_decoder_get_size(channels);
95 return align(sizeof(OpusDecoder))+silkDecSizeBytes+celtDecSizeBytes;
98 int opus_decoder_init(OpusDecoder *st, opus_int32 Fs, int channels)
101 CELTDecoder *celt_dec;
102 int ret, silkDecSizeBytes;
104 if ((Fs!=48000&&Fs!=24000&&Fs!=16000&&Fs!=12000&&Fs!=8000)
105 || (channels!=1&&channels!=2))
108 OPUS_CLEAR((char*)st, opus_decoder_get_size(channels));
109 /* Initialize SILK encoder */
110 ret = silk_Get_Decoder_Size(&silkDecSizeBytes);
112 return OPUS_INTERNAL_ERROR;
114 silkDecSizeBytes = align(silkDecSizeBytes);
115 st->silk_dec_offset = align(sizeof(OpusDecoder));
116 st->celt_dec_offset = st->silk_dec_offset+silkDecSizeBytes;
117 silk_dec = (char*)st+st->silk_dec_offset;
118 celt_dec = (CELTDecoder*)((char*)st+st->celt_dec_offset);
119 st->stream_channels = st->channels = channels;
122 st->DecControl.API_sampleRate = st->Fs;
123 st->DecControl.nChannelsAPI = st->channels;
124 st->arch = opus_select_arch();
127 ret = silk_InitDecoder( silk_dec );
128 if(ret)return OPUS_INTERNAL_ERROR;
130 /* Initialize CELT decoder */
131 ret = celt_decoder_init(celt_dec, Fs, channels);
132 if(ret!=OPUS_OK)return OPUS_INTERNAL_ERROR;
134 celt_decoder_ctl(celt_dec, CELT_SET_SIGNALLING(0));
137 st->frame_size = Fs/400;
141 OpusDecoder *opus_decoder_create(opus_int32 Fs, int channels, int *error)
145 if ((Fs!=48000&&Fs!=24000&&Fs!=16000&&Fs!=12000&&Fs!=8000)
146 || (channels!=1&&channels!=2))
149 *error = OPUS_BAD_ARG;
152 st = (OpusDecoder *)opus_alloc(opus_decoder_get_size(channels));
156 *error = OPUS_ALLOC_FAIL;
159 ret = opus_decoder_init(st, Fs, channels);
170 static void smooth_fade(const opus_val16 *in1, const opus_val16 *in2,
171 opus_val16 *out, int overlap, int channels,
172 const opus_val16 *window, opus_int32 Fs)
176 for (c=0;c<channels;c++)
178 for (i=0;i<overlap;i++)
180 opus_val16 w = MULT16_16_Q15(window[i*inc], window[i*inc]);
181 out[i*channels+c] = SHR32(MAC16_16(MULT16_16(w,in2[i*channels+c]),
182 Q15ONE-w, in1[i*channels+c]), 15);
187 static int opus_packet_get_mode(const unsigned char *data)
192 mode = MODE_CELT_ONLY;
193 } else if ((data[0]&0x60) == 0x60)
197 mode = MODE_SILK_ONLY;
202 static int opus_decode_frame(OpusDecoder *st, const unsigned char *data,
203 opus_int32 len, opus_val16 *pcm, int frame_size, int decode_fec)
206 CELTDecoder *celt_dec;
207 int i, silk_ret=0, celt_ret=0;
209 opus_int32 silk_frame_size;
211 VARDECL(opus_int16, pcm_silk);
212 int pcm_transition_silk_size;
213 VARDECL(opus_val16, pcm_transition_silk);
214 int pcm_transition_celt_size;
215 VARDECL(opus_val16, pcm_transition_celt);
216 opus_val16 *pcm_transition;
217 int redundant_audio_size;
218 VARDECL(opus_val16, redundant_audio);
225 int redundancy_bytes = 0;
228 int F2_5, F5, F10, F20;
229 const opus_val16 *window;
230 opus_uint32 redundant_rng = 0;
233 silk_dec = (char*)st+st->silk_dec_offset;
234 celt_dec = (CELTDecoder*)((char*)st+st->celt_dec_offset);
239 if (frame_size < F2_5)
242 return OPUS_BUFFER_TOO_SMALL;
244 /* Limit frame_size to avoid excessive stack allocations. */
245 frame_size = IMIN(frame_size, st->Fs/25*3);
246 /* Payloads of 1 (2 including ToC) or 0 trigger the PLC/DTX */
250 /* In that case, don't conceal more than what the ToC says */
251 frame_size = IMIN(frame_size, st->frame_size);
255 audiosize = st->frame_size;
257 ec_dec_init(&dec,(unsigned char*)data,len);
259 audiosize = frame_size;
260 mode = st->prev_mode;
264 /* If we haven't got any packet yet, all we can do is return zeros */
265 for (i=0;i<audiosize*st->channels;i++)
271 /* Avoids trying to run the PLC on sizes other than 2.5 (CELT), 5 (CELT),
272 10, or 20 (e.g. 12.5 or 30 ms). */
276 int ret = opus_decode_frame(st, NULL, 0, pcm, IMIN(audiosize, F20), 0);
282 pcm += ret*st->channels;
284 } while (audiosize > 0);
287 } else if (audiosize < F20)
291 else if (mode != MODE_SILK_ONLY && audiosize > F5 && audiosize < F10)
296 pcm_transition_silk_size = 0;
297 pcm_transition_celt_size = 0;
298 if (data!=NULL && st->prev_mode > 0 && (
299 (mode == MODE_CELT_ONLY && st->prev_mode != MODE_CELT_ONLY && !st->prev_redundancy)
300 || (mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY) )
304 /* Decide where to allocate the stack memory for pcm_transition */
305 if (mode == MODE_CELT_ONLY)
306 pcm_transition_celt_size = F5*st->channels;
308 pcm_transition_silk_size = F5*st->channels;
310 ALLOC(pcm_transition_celt, pcm_transition_celt_size, opus_val16);
311 if (transition && mode == MODE_CELT_ONLY)
313 pcm_transition = pcm_transition_celt;
314 opus_decode_frame(st, NULL, 0, pcm_transition, IMIN(F5, audiosize), 0);
316 if (audiosize > frame_size)
318 /*fprintf(stderr, "PCM buffer too small: %d vs %d (mode = %d)\n", audiosize, frame_size, mode);*/
322 frame_size = audiosize;
325 /* Don't allocate any memory when in CELT-only mode */
326 pcm_silk_size = (mode != MODE_CELT_ONLY) ? IMAX(F10, frame_size)*st->channels : 0;
327 ALLOC(pcm_silk, pcm_silk_size, opus_int16);
329 /* SILK processing */
330 if (mode != MODE_CELT_ONLY)
332 int lost_flag, decoded_samples;
333 opus_int16 *pcm_ptr = pcm_silk;
335 if (st->prev_mode==MODE_CELT_ONLY)
336 silk_InitDecoder( silk_dec );
338 /* The SILK PLC cannot produce frames of less than 10 ms */
339 st->DecControl.payloadSize_ms = IMAX(10, 1000 * audiosize / st->Fs);
343 st->DecControl.nChannelsInternal = st->stream_channels;
344 if( mode == MODE_SILK_ONLY ) {
345 if( st->bandwidth == OPUS_BANDWIDTH_NARROWBAND ) {
346 st->DecControl.internalSampleRate = 8000;
347 } else if( st->bandwidth == OPUS_BANDWIDTH_MEDIUMBAND ) {
348 st->DecControl.internalSampleRate = 12000;
349 } else if( st->bandwidth == OPUS_BANDWIDTH_WIDEBAND ) {
350 st->DecControl.internalSampleRate = 16000;
352 st->DecControl.internalSampleRate = 16000;
357 st->DecControl.internalSampleRate = 16000;
361 lost_flag = data == NULL ? 1 : 2 * decode_fec;
364 /* Call SILK decoder */
365 int first_frame = decoded_samples == 0;
366 silk_ret = silk_Decode( silk_dec, &st->DecControl,
367 lost_flag, first_frame, &dec, pcm_ptr, &silk_frame_size );
370 /* PLC failure should not be fatal */
371 silk_frame_size = frame_size;
372 for (i=0;i<frame_size*st->channels;i++)
376 return OPUS_INTERNAL_ERROR;
379 pcm_ptr += silk_frame_size * st->channels;
380 decoded_samples += silk_frame_size;
381 } while( decoded_samples < frame_size );
385 if (!decode_fec && mode != MODE_CELT_ONLY && data != NULL
386 && ec_tell(&dec)+17+20*(st->mode == MODE_HYBRID) <= 8*len)
388 /* Check if we have a redundant 0-8 kHz band */
389 if (mode == MODE_HYBRID)
390 redundancy = ec_dec_bit_logp(&dec, 12);
395 celt_to_silk = ec_dec_bit_logp(&dec, 1);
396 /* redundancy_bytes will be at least two, in the non-hybrid
397 case due to the ec_tell() check above */
398 redundancy_bytes = mode==MODE_HYBRID ?
399 (opus_int32)ec_dec_uint(&dec, 256)+2 :
400 len-((ec_tell(&dec)+7)>>3);
401 len -= redundancy_bytes;
402 /* This is a sanity check. It should never happen for a valid
403 packet, so the exact behaviour is not normative. */
404 if (len*8 < ec_tell(&dec))
407 redundancy_bytes = 0;
410 /* Shrink decoder because of raw bits */
411 dec.storage -= redundancy_bytes;
414 if (mode != MODE_CELT_ONLY)
420 switch(st->bandwidth)
422 case OPUS_BANDWIDTH_NARROWBAND:
425 case OPUS_BANDWIDTH_MEDIUMBAND:
426 case OPUS_BANDWIDTH_WIDEBAND:
429 case OPUS_BANDWIDTH_SUPERWIDEBAND:
432 case OPUS_BANDWIDTH_FULLBAND:
436 celt_decoder_ctl(celt_dec, CELT_SET_END_BAND(endband));
437 celt_decoder_ctl(celt_dec, CELT_SET_CHANNELS(st->stream_channels));
443 pcm_transition_silk_size=0;
446 ALLOC(pcm_transition_silk, pcm_transition_silk_size, opus_val16);
448 if (transition && mode != MODE_CELT_ONLY)
450 pcm_transition = pcm_transition_silk;
451 opus_decode_frame(st, NULL, 0, pcm_transition, IMIN(F5, audiosize), 0);
454 /* Only allocation memory for redundancy if/when needed */
455 redundant_audio_size = redundancy ? F5*st->channels : 0;
456 ALLOC(redundant_audio, redundant_audio_size, opus_val16);
458 /* 5 ms redundant frame for CELT->SILK*/
459 if (redundancy && celt_to_silk)
461 celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(0));
462 celt_decode_with_ec(celt_dec, data+len, redundancy_bytes,
463 redundant_audio, F5, NULL);
464 celt_decoder_ctl(celt_dec, OPUS_GET_FINAL_RANGE(&redundant_rng));
467 /* MUST be after PLC */
468 celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(start_band));
470 if (mode != MODE_SILK_ONLY)
472 int celt_frame_size = IMIN(F20, frame_size);
473 /* Make sure to discard any previous CELT state */
474 if (mode != st->prev_mode && st->prev_mode > 0 && !st->prev_redundancy)
475 celt_decoder_ctl(celt_dec, OPUS_RESET_STATE);
477 celt_ret = celt_decode_with_ec(celt_dec, decode_fec ? NULL : data,
478 len, pcm, celt_frame_size, &dec);
480 unsigned char silence[2] = {0xFF, 0xFF};
481 for (i=0;i<frame_size*st->channels;i++)
483 /* For hybrid -> SILK transitions, we let the CELT MDCT
484 do a fade-out by decoding a silence frame */
485 if (st->prev_mode == MODE_HYBRID && !(redundancy && celt_to_silk && st->prev_redundancy) )
487 celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(0));
488 celt_decode_with_ec(celt_dec, silence, 2, pcm, F2_5, NULL);
492 if (mode != MODE_CELT_ONLY)
495 for (i=0;i<frame_size*st->channels;i++)
496 pcm[i] = SAT16(pcm[i] + pcm_silk[i]);
498 for (i=0;i<frame_size*st->channels;i++)
499 pcm[i] = pcm[i] + (opus_val16)((1.f/32768.f)*pcm_silk[i]);
504 const CELTMode *celt_mode;
505 celt_decoder_ctl(celt_dec, CELT_GET_MODE(&celt_mode));
506 window = celt_mode->window;
509 /* 5 ms redundant frame for SILK->CELT */
510 if (redundancy && !celt_to_silk)
512 celt_decoder_ctl(celt_dec, OPUS_RESET_STATE);
513 celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(0));
515 celt_decode_with_ec(celt_dec, data+len, redundancy_bytes, redundant_audio, F5, NULL);
516 celt_decoder_ctl(celt_dec, OPUS_GET_FINAL_RANGE(&redundant_rng));
517 smooth_fade(pcm+st->channels*(frame_size-F2_5), redundant_audio+st->channels*F2_5,
518 pcm+st->channels*(frame_size-F2_5), F2_5, st->channels, window, st->Fs);
520 if (redundancy && celt_to_silk)
522 for (c=0;c<st->channels;c++)
525 pcm[st->channels*i+c] = redundant_audio[st->channels*i+c];
527 smooth_fade(redundant_audio+st->channels*F2_5, pcm+st->channels*F2_5,
528 pcm+st->channels*F2_5, F2_5, st->channels, window, st->Fs);
534 for (i=0;i<st->channels*F2_5;i++)
535 pcm[i] = pcm_transition[i];
536 smooth_fade(pcm_transition+st->channels*F2_5, pcm+st->channels*F2_5,
537 pcm+st->channels*F2_5, F2_5,
538 st->channels, window, st->Fs);
540 /* Not enough time to do a clean transition, but we do it anyway
541 This will not preserve amplitude perfectly and may introduce
542 a bit of temporal aliasing, but it shouldn't be too bad and
543 that's pretty much the best we can do. In any case, generating this
544 transition it pretty silly in the first place */
545 smooth_fade(pcm_transition, pcm,
547 st->channels, window, st->Fs);
554 gain = celt_exp2(MULT16_16_P15(QCONST16(6.48814081e-4f, 25), st->decode_gain));
555 for (i=0;i<frame_size*st->channels;i++)
558 x = MULT16_32_P16(pcm[i],gain);
559 pcm[i] = SATURATE(x, 32767);
566 st->rangeFinal = dec.rng ^ redundant_rng;
568 st->prev_mode = mode;
569 st->prev_redundancy = redundancy && !celt_to_silk;
573 if (OPUS_CHECK_ARRAY(pcm, audiosize*st->channels))
574 OPUS_PRINT_INT(audiosize);
578 return celt_ret < 0 ? celt_ret : audiosize;
582 static int parse_size(const unsigned char *data, opus_int32 len, opus_int16 *size)
588 } else if (data[0]<252)
597 *size = 4*data[1] + data[0];
602 int opus_packet_parse_impl(const unsigned char *data, opus_int32 len,
603 int self_delimited, unsigned char *out_toc,
604 const unsigned char *frames[48], opus_int16 size[48], int *payload_offset)
609 unsigned char ch, toc;
611 opus_int32 last_size;
612 const unsigned char *data0 = data;
617 framesize = opus_packet_get_samples_per_frame(data, 48000);
636 return OPUS_INVALID_PACKET;
638 /* If last_size doesn't fit in size[0], we'll catch it later */
639 size[0] = (opus_int16)last_size;
645 bytes = parse_size(data, len, size);
647 if (size[0]<0 || size[0] > len)
648 return OPUS_INVALID_PACKET;
650 last_size = len-size[0];
652 /* Multiple CBR/VBR frames (from 0 to 120 ms) */
655 return OPUS_INVALID_PACKET;
656 /* Number of frames encoded in bits 0 to 5 */
659 if (count <= 0 || framesize*count > 5760)
660 return OPUS_INVALID_PACKET;
662 /* Padding flag is bit 6 */
668 return OPUS_INVALID_PACKET;
671 len -= p==255 ? 254: p;
675 return OPUS_INVALID_PACKET;
676 /* VBR flag is bit 7 */
682 for (i=0;i<count-1;i++)
684 bytes = parse_size(data, len, size+i);
686 if (size[i]<0 || size[i] > len)
687 return OPUS_INVALID_PACKET;
689 last_size -= bytes+size[i];
692 return OPUS_INVALID_PACKET;
693 } else if (!self_delimited)
696 last_size = len/count;
697 if (last_size*count!=len)
698 return OPUS_INVALID_PACKET;
699 for (i=0;i<count-1;i++)
700 size[i] = (opus_int16)last_size;
704 /* Self-delimited framing has an extra size for the last frame. */
707 bytes = parse_size(data, len, size+count-1);
709 if (size[count-1]<0 || size[count-1] > len)
710 return OPUS_INVALID_PACKET;
712 /* For CBR packets, apply the size to all the frames. */
715 if (size[count-1]*count > len)
716 return OPUS_INVALID_PACKET;
717 for (i=0;i<count-1;i++)
718 size[i] = size[count-1];
719 } else if (bytes+size[count-1] > last_size)
720 return OPUS_INVALID_PACKET;
723 /* Because it's not encoded explicitly, it's possible the size of the
724 last packet (or all the packets, for the CBR case) is larger than
725 1275. Reject them here.*/
726 if (last_size > 1275)
727 return OPUS_INVALID_PACKET;
728 size[count-1] = (opus_int16)last_size;
732 *payload_offset = (int)(data-data0);
736 for (i=0;i<count;i++)
749 int opus_packet_parse(const unsigned char *data, opus_int32 len,
750 unsigned char *out_toc, const unsigned char *frames[48],
751 opus_int16 size[48], int *payload_offset)
753 return opus_packet_parse_impl(data, len, 0, out_toc,
754 frames, size, payload_offset);
757 int opus_decode_native(OpusDecoder *st, const unsigned char *data,
758 opus_int32 len, opus_val16 *pcm, int frame_size, int decode_fec,
759 int self_delimited, int *packet_offset, int soft_clip)
765 int packet_frame_size, packet_bandwidth, packet_mode, packet_stream_channels;
766 /* 48 x 2.5 ms = 120 ms */
768 if (decode_fec<0 || decode_fec>1)
770 /* For FEC/PLC, frame_size has to be to have a multiple of 2.5 ms */
771 if ((decode_fec || len==0 || data==NULL) && frame_size%(st->Fs/400)!=0)
773 if (len==0 || data==NULL)
778 ret = opus_decode_frame(st, NULL, 0, pcm+pcm_count*st->channels, frame_size-pcm_count, 0);
782 } while (pcm_count < frame_size);
783 celt_assert(pcm_count == frame_size);
784 if (OPUS_CHECK_ARRAY(pcm, pcm_count*st->channels))
785 OPUS_PRINT_INT(pcm_count);
786 st->last_packet_duration = pcm_count;
791 packet_mode = opus_packet_get_mode(data);
792 packet_bandwidth = opus_packet_get_bandwidth(data);
793 packet_frame_size = opus_packet_get_samples_per_frame(data, st->Fs);
794 packet_stream_channels = opus_packet_get_nb_channels(data);
796 count = opus_packet_parse_impl(data, len, self_delimited, &toc, NULL, size, &offset);
807 /* If no FEC can be present, run the PLC (recursive call) */
808 if (frame_size < packet_frame_size || packet_mode == MODE_CELT_ONLY || st->mode == MODE_CELT_ONLY)
809 return opus_decode_native(st, NULL, 0, pcm, frame_size, 0, 0, NULL, soft_clip);
810 /* Otherwise, run the PLC on everything except the size for which we might have FEC */
811 duration_copy = st->last_packet_duration;
812 if (frame_size-packet_frame_size!=0)
814 ret = opus_decode_native(st, NULL, 0, pcm, frame_size-packet_frame_size, 0, 0, NULL, soft_clip);
817 st->last_packet_duration = duration_copy;
820 celt_assert(ret==frame_size-packet_frame_size);
822 /* Complete with FEC */
823 st->mode = packet_mode;
824 st->bandwidth = packet_bandwidth;
825 st->frame_size = packet_frame_size;
826 st->stream_channels = packet_stream_channels;
827 ret = opus_decode_frame(st, data, size[0], pcm+st->channels*(frame_size-packet_frame_size),
828 packet_frame_size, 1);
832 if (OPUS_CHECK_ARRAY(pcm, frame_size*st->channels))
833 OPUS_PRINT_INT(frame_size);
834 st->last_packet_duration = frame_size;
840 if (count*packet_frame_size > frame_size)
841 return OPUS_BUFFER_TOO_SMALL;
843 /* Update the state as the last step to avoid updating it on an invalid packet */
844 st->mode = packet_mode;
845 st->bandwidth = packet_bandwidth;
846 st->frame_size = packet_frame_size;
847 st->stream_channels = packet_stream_channels;
850 for (i=0;i<count;i++)
853 ret = opus_decode_frame(st, data, size[i], pcm+nb_samples*st->channels, frame_size-nb_samples, 0);
856 celt_assert(ret==packet_frame_size);
858 tot_offset += size[i];
861 if (packet_offset != NULL)
862 *packet_offset = tot_offset;
863 st->last_packet_duration = nb_samples;
864 if (OPUS_CHECK_ARRAY(pcm, nb_samples*st->channels))
865 OPUS_PRINT_INT(nb_samples);
868 opus_pcm_soft_clip(pcm, nb_samples, st->channels, st->softclip_mem);
870 st->softclip_mem[0]=st->softclip_mem[1]=0;
877 int opus_decode(OpusDecoder *st, const unsigned char *data,
878 opus_int32 len, opus_val16 *pcm, int frame_size, int decode_fec)
880 return opus_decode_native(st, data, len, pcm, frame_size, decode_fec, 0, NULL, 0);
883 #ifndef DISABLE_FLOAT_API
884 int opus_decode_float(OpusDecoder *st, const unsigned char *data,
885 opus_int32 len, float *pcm, int frame_size, int decode_fec)
887 VARDECL(opus_int16, out);
891 ALLOC(out, frame_size*st->channels, opus_int16);
893 ret = opus_decode_native(st, data, len, out, frame_size, decode_fec, 0, NULL, 0);
896 for (i=0;i<ret*st->channels;i++)
897 pcm[i] = (1.f/32768.f)*(out[i]);
906 int opus_decode(OpusDecoder *st, const unsigned char *data,
907 opus_int32 len, opus_int16 *pcm, int frame_size, int decode_fec)
919 ALLOC(out, frame_size*st->channels, float);
921 ret = opus_decode_native(st, data, len, out, frame_size, decode_fec, 0, NULL, 1);
924 for (i=0;i<ret*st->channels;i++)
925 pcm[i] = FLOAT2INT16(out[i]);
931 int opus_decode_float(OpusDecoder *st, const unsigned char *data,
932 opus_int32 len, opus_val16 *pcm, int frame_size, int decode_fec)
934 return opus_decode_native(st, data, len, pcm, frame_size, decode_fec, 0, NULL, 0);
939 int opus_decoder_ctl(OpusDecoder *st, int request, ...)
944 CELTDecoder *celt_dec;
946 silk_dec = (char*)st+st->silk_dec_offset;
947 celt_dec = (CELTDecoder*)((char*)st+st->celt_dec_offset);
950 va_start(ap, request);
954 case OPUS_GET_BANDWIDTH_REQUEST:
956 opus_int32 *value = va_arg(ap, opus_int32*);
961 *value = st->bandwidth;
964 case OPUS_GET_FINAL_RANGE_REQUEST:
966 opus_uint32 *value = va_arg(ap, opus_uint32*);
971 *value = st->rangeFinal;
974 case OPUS_RESET_STATE:
976 OPUS_CLEAR((char*)&st->OPUS_DECODER_RESET_START,
978 ((char*)&st->OPUS_DECODER_RESET_START - (char*)st));
980 celt_decoder_ctl(celt_dec, OPUS_RESET_STATE);
981 silk_InitDecoder( silk_dec );
982 st->stream_channels = st->channels;
983 st->frame_size = st->Fs/400;
986 case OPUS_GET_SAMPLE_RATE_REQUEST:
988 opus_int32 *value = va_arg(ap, opus_int32*);
996 case OPUS_GET_PITCH_REQUEST:
998 opus_int32 *value = va_arg(ap, opus_int32*);
1003 if (st->prev_mode == MODE_CELT_ONLY)
1004 celt_decoder_ctl(celt_dec, OPUS_GET_PITCH(value));
1006 *value = st->DecControl.prevPitchLag;
1009 case OPUS_GET_GAIN_REQUEST:
1011 opus_int32 *value = va_arg(ap, opus_int32*);
1016 *value = st->decode_gain;
1019 case OPUS_SET_GAIN_REQUEST:
1021 opus_int32 value = va_arg(ap, opus_int32);
1022 if (value<-32768 || value>32767)
1026 st->decode_gain = value;
1029 case OPUS_GET_LAST_PACKET_DURATION_REQUEST:
1031 opus_uint32 *value = va_arg(ap, opus_uint32*);
1036 *value = st->last_packet_duration;
1040 /*fprintf(stderr, "unknown opus_decoder_ctl() request: %d", request);*/
1041 ret = OPUS_UNIMPLEMENTED;
1049 return OPUS_BAD_ARG;
1052 void opus_decoder_destroy(OpusDecoder *st)
1058 int opus_packet_get_bandwidth(const unsigned char *data)
1063 bandwidth = OPUS_BANDWIDTH_MEDIUMBAND + ((data[0]>>5)&0x3);
1064 if (bandwidth == OPUS_BANDWIDTH_MEDIUMBAND)
1065 bandwidth = OPUS_BANDWIDTH_NARROWBAND;
1066 } else if ((data[0]&0x60) == 0x60)
1068 bandwidth = (data[0]&0x10) ? OPUS_BANDWIDTH_FULLBAND :
1069 OPUS_BANDWIDTH_SUPERWIDEBAND;
1071 bandwidth = OPUS_BANDWIDTH_NARROWBAND + ((data[0]>>5)&0x3);
1076 int opus_packet_get_samples_per_frame(const unsigned char *data,
1082 audiosize = ((data[0]>>3)&0x3);
1083 audiosize = (Fs<<audiosize)/400;
1084 } else if ((data[0]&0x60) == 0x60)
1086 audiosize = (data[0]&0x08) ? Fs/50 : Fs/100;
1088 audiosize = ((data[0]>>3)&0x3);
1090 audiosize = Fs*60/1000;
1092 audiosize = (Fs<<audiosize)/100;
1097 int opus_packet_get_nb_channels(const unsigned char *data)
1099 return (data[0]&0x4) ? 2 : 1;
1102 int opus_packet_get_nb_frames(const unsigned char packet[], opus_int32 len)
1106 return OPUS_BAD_ARG;
1107 count = packet[0]&0x3;
1113 return OPUS_INVALID_PACKET;
1115 return packet[1]&0x3F;
1118 int opus_packet_get_nb_samples(const unsigned char packet[], opus_int32 len,
1122 int count = opus_packet_get_nb_frames(packet, len);
1127 samples = count*opus_packet_get_samples_per_frame(packet, Fs);
1128 /* Can't have more than 120 ms */
1129 if (samples*25 > Fs*3)
1130 return OPUS_INVALID_PACKET;
1135 int opus_decoder_get_nb_samples(const OpusDecoder *dec,
1136 const unsigned char packet[], opus_int32 len)
1138 return opus_packet_get_nb_samples(packet, len, dec->Fs);