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 FOUNDATION OR
19 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.
41 #include "stack_alloc.h"
42 #include "float_cast.h"
43 #include "opus_private.h"
44 #include "os_support.h"
47 #define celt_decode_native celt_decode
49 #define celt_decode_native celt_decode_float
59 /* Sampling rate (at the API level) */
70 static inline opus_int16 SAT16(opus_int32 x) {
71 return x > 32767 ? 32767 : x < -32768 ? -32768 : (opus_int16)x;
76 int opus_decoder_get_size(int channels)
78 int silkDecSizeBytes, celtDecSizeBytes;
80 ret = silk_Get_Decoder_Size( &silkDecSizeBytes );
83 silkDecSizeBytes = align(silkDecSizeBytes);
84 celtDecSizeBytes = celt_decoder_get_size(channels);
85 return align(sizeof(OpusDecoder))+silkDecSizeBytes+celtDecSizeBytes;
89 int opus_decoder_init(OpusDecoder *st, int Fs, int channels)
92 CELTDecoder *celt_dec;
93 int ret, silkDecSizeBytes;
95 if (channels<1 || channels > 2)
97 memset(st, 0, opus_decoder_get_size(channels));
98 /* Initialize SILK encoder */
99 ret = silk_Get_Decoder_Size( &silkDecSizeBytes );
101 return OPUS_INTERNAL_ERROR;
103 silkDecSizeBytes = align(silkDecSizeBytes);
104 st->silk_dec_offset = align(sizeof(OpusDecoder));
105 st->celt_dec_offset = st->silk_dec_offset+silkDecSizeBytes;
106 silk_dec = (char*)st+st->silk_dec_offset;
107 celt_dec = (CELTDecoder*)((char*)st+st->celt_dec_offset);
108 st->stream_channels = st->channels = channels;
113 ret = silk_InitDecoder( silk_dec );
118 /* Initialize CELT decoder */
119 celt_decoder_init(celt_dec, Fs, channels, &ret);
122 celt_decoder_ctl(celt_dec, CELT_SET_SIGNALLING(0));
125 st->frame_size = Fs/400;
129 return OPUS_INTERNAL_ERROR;
132 OpusDecoder *opus_decoder_create(int Fs, int channels, int *error)
135 char *raw_state = (char*)opus_alloc(opus_decoder_get_size(channels));
136 if (raw_state == NULL)
139 *error = OPUS_ALLOC_FAIL;
142 ret = opus_decoder_init((OpusDecoder*)raw_state, Fs, channels);
145 opus_free(raw_state);
148 return (OpusDecoder*)raw_state;
151 static void smooth_fade(const opus_val16 *in1, const opus_val16 *in2, opus_val16 *out,
152 int overlap, int channels, const opus_val16 *window, int Fs)
156 for (c=0;c<channels;c++)
158 for (i=0;i<overlap;i++)
160 opus_val16 w = MULT16_16_Q15(window[i*inc], window[i*inc]);
161 out[i*channels+c] = SHR32(MAC16_16(MULT16_16(w,in2[i*channels+c]),
162 Q15ONE-w, in1[i*channels+c]), 15);
167 static int opus_packet_get_mode(const unsigned char *data)
172 mode = MODE_CELT_ONLY;
173 } else if ((data[0]&0x60) == 0x60)
178 mode = MODE_SILK_ONLY;
183 static int opus_decode_frame(OpusDecoder *st, const unsigned char *data,
184 int len, opus_val16 *pcm, int frame_size, int decode_fec)
187 CELTDecoder *celt_dec;
188 int i, silk_ret=0, celt_ret=0;
190 silk_DecControlStruct DecControl;
191 opus_int32 silk_frame_size;
192 VARDECL(opus_int16, pcm_silk);
193 VARDECL(opus_val16, pcm_transition);
194 VARDECL(opus_val16, redundant_audio);
201 int redundancy_bytes = 0;
204 int F2_5, F5, F10, F20;
205 const opus_val16 *window;
206 opus_uint32 redundant_rng = 0;
209 silk_dec = (char*)st+st->silk_dec_offset;
210 celt_dec = (CELTDecoder*)((char*)st+st->celt_dec_offset);
215 if (frame_size < F2_5)
216 return OPUS_BUFFER_TOO_SMALL;
217 /* Payloads of 1 (2 including ToC) or 0 trigger the PLC/DTX */
221 /* In that case, don't conceal more than what the ToC says */
222 /* FIXME: What if st->frame_size has never been set? */
223 frame_size = IMIN(frame_size, st->frame_size);
227 audiosize = st->frame_size;
229 ec_dec_init(&dec,(unsigned char*)data,len);
231 audiosize = frame_size;
233 if (st->prev_mode == 0)
235 /* If we haven't got any packet yet, all we can do is return zeros */
236 for (i=0;i<audiosize*st->channels;i++)
241 mode = st->prev_mode;
245 ALLOC(pcm_transition, F5*st->channels, opus_val16);
247 if (data!=NULL && !st->prev_redundancy && mode != st->prev_mode && st->prev_mode > 0
248 && !(mode == MODE_SILK_ONLY && st->prev_mode == MODE_HYBRID)
249 && !(mode == MODE_HYBRID && st->prev_mode == MODE_SILK_ONLY))
252 if (mode == MODE_CELT_ONLY)
253 opus_decode_frame(st, NULL, 0, pcm_transition, IMIN(F5, audiosize), 0);
255 if (audiosize > frame_size)
257 fprintf(stderr, "PCM buffer too small: %d vs %d (mode = %d)\n", audiosize, frame_size, mode);
261 frame_size = audiosize;
264 ALLOC(pcm_silk, frame_size*st->channels, opus_int16);
265 ALLOC(redundant_audio, F5*st->channels, opus_val16);
267 /* SILK processing */
268 if (mode != MODE_CELT_ONLY)
270 int lost_flag, decoded_samples;
271 opus_int16 *pcm_ptr = pcm_silk;
273 if (st->prev_mode==MODE_CELT_ONLY)
274 silk_InitDecoder( silk_dec );
276 DecControl.API_sampleRate = st->Fs;
277 DecControl.nChannelsAPI = st->channels;
278 DecControl.nChannelsInternal = st->stream_channels;
279 DecControl.payloadSize_ms = 1000 * audiosize / st->Fs;
280 if( mode == MODE_SILK_ONLY ) {
281 if( st->bandwidth == OPUS_BANDWIDTH_NARROWBAND ) {
282 DecControl.internalSampleRate = 8000;
283 } else if( st->bandwidth == OPUS_BANDWIDTH_MEDIUMBAND ) {
284 DecControl.internalSampleRate = 12000;
285 } else if( st->bandwidth == OPUS_BANDWIDTH_WIDEBAND ) {
286 DecControl.internalSampleRate = 16000;
288 DecControl.internalSampleRate = 16000;
293 DecControl.internalSampleRate = 16000;
296 lost_flag = data == NULL ? 1 : 2 * decode_fec;
299 /* Call SILK decoder */
300 int first_frame = decoded_samples == 0;
301 silk_ret = silk_Decode( silk_dec, &DecControl,
302 lost_flag, first_frame, &dec, pcm_ptr, &silk_frame_size );
305 /* PLC failure should not be fatal */
306 silk_frame_size = frame_size;
307 for (i=0;i<frame_size*st->channels;i++)
311 return OPUS_CORRUPTED_DATA;
314 pcm_ptr += silk_frame_size * st->channels;
315 decoded_samples += silk_frame_size;
316 } while( decoded_samples < frame_size );
320 if (mode != MODE_CELT_ONLY && data != NULL)
322 /* Check if we have a redundant 0-8 kHz band */
323 redundancy = ec_dec_bit_logp(&dec, 12);
326 celt_to_silk = ec_dec_bit_logp(&dec, 1);
327 if (mode == MODE_HYBRID)
328 redundancy_bytes = 2 + ec_dec_uint(&dec, 256);
330 redundancy_bytes = len - ((ec_tell(&dec)+7)>>3);
331 /* Can only happen on an invalid packet */
332 if (redundancy_bytes<0)
334 redundancy_bytes = 0;
338 len -= redundancy_bytes;
341 return OPUS_CORRUPTED_DATA;
343 /* Shrink decoder because of raw bits */
344 dec.storage -= redundancy_bytes;
347 if (mode != MODE_CELT_ONLY)
353 switch(st->bandwidth)
355 case OPUS_BANDWIDTH_NARROWBAND:
358 case OPUS_BANDWIDTH_MEDIUMBAND:
359 case OPUS_BANDWIDTH_WIDEBAND:
362 case OPUS_BANDWIDTH_SUPERWIDEBAND:
365 case OPUS_BANDWIDTH_FULLBAND:
369 celt_decoder_ctl(celt_dec, CELT_SET_END_BAND(endband));
370 celt_decoder_ctl(celt_dec, CELT_SET_CHANNELS(st->stream_channels));
376 if (transition && mode != MODE_CELT_ONLY)
377 opus_decode_frame(st, NULL, 0, pcm_transition, IMIN(F5, audiosize), 0);
379 /* 5 ms redundant frame for CELT->SILK*/
380 if (redundancy && celt_to_silk)
382 celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(0));
383 celt_decode_native(celt_dec, data+len, redundancy_bytes, redundant_audio, F5);
384 celt_decoder_ctl(celt_dec, CELT_GET_RANGE(&redundant_rng));
385 celt_decoder_ctl(celt_dec, CELT_RESET_STATE);
388 /* MUST be after PLC */
389 celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(start_band));
392 celt_decoder_ctl(celt_dec, CELT_RESET_STATE);
394 if (mode != MODE_SILK_ONLY)
396 int celt_frame_size = IMIN(F20, frame_size);
398 celt_ret = celt_decode_with_ec(celt_dec, decode_fec?NULL:data, len, pcm, celt_frame_size, &dec);
400 for (i=0;i<frame_size*st->channels;i++)
404 if (mode != MODE_CELT_ONLY)
407 for (i=0;i<frame_size*st->channels;i++)
408 pcm[i] = SAT16(pcm[i] + pcm_silk[i]);
410 for (i=0;i<frame_size*st->channels;i++)
411 pcm[i] = pcm[i] + (1./32768.)*pcm_silk[i];
416 const CELTMode *celt_mode;
417 celt_decoder_ctl(celt_dec, CELT_GET_MODE(&celt_mode));
418 window = celt_mode->window;
421 /* 5 ms redundant frame for SILK->CELT */
422 if (redundancy && !celt_to_silk)
424 celt_decoder_ctl(celt_dec, CELT_RESET_STATE);
425 celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(0));
427 celt_decode_native(celt_dec, data+len, redundancy_bytes, redundant_audio, F5);
428 celt_decoder_ctl(celt_dec, CELT_GET_RANGE(&redundant_rng));
429 smooth_fade(pcm+st->channels*(frame_size-F2_5), redundant_audio+st->channels*F2_5,
430 pcm+st->channels*(frame_size-F2_5), F2_5, st->channels, window, st->Fs);
432 if (redundancy && celt_to_silk)
434 for (c=0;c<st->channels;c++)
437 pcm[st->channels*i+c] = redundant_audio[st->channels*i+c];
439 smooth_fade(redundant_audio+st->channels*F2_5, pcm+st->channels*F2_5,
440 pcm+st->channels*F2_5, F2_5, st->channels, window, st->Fs);
444 for (i=0;i<st->channels*F2_5;i++)
445 pcm[i] = pcm_transition[i];
447 smooth_fade(pcm_transition+st->channels*F2_5, pcm+st->channels*F2_5,
448 pcm+st->channels*F2_5, F2_5,
449 st->channels, window, st->Fs);
455 st->rangeFinal = dec.rng ^ redundant_rng;
457 st->prev_mode = mode;
458 st->prev_redundancy = redundancy;
460 return celt_ret<0 ? celt_ret : audiosize;
464 static int parse_size(const unsigned char *data, int len, short *size)
470 } else if (data[0]<252)
479 *size = 4*data[1] + data[0];
484 static int opus_packet_parse_impl(const unsigned char *data, int len,
485 int self_delimited, unsigned char *out_toc,
486 const unsigned char *frames[48], short size[48], int *payload_offset)
491 unsigned char ch, toc;
494 const unsigned char *data0 = data;
499 framesize = opus_packet_get_samples_per_frame(data, 48000);
518 return OPUS_CORRUPTED_DATA;
519 size[0] = last_size = len/2;
525 bytes = parse_size(data, len, size);
527 if (size[0]<0 || size[0] > len)
528 return OPUS_CORRUPTED_DATA;
530 last_size = len-size[0];
532 /* Multiple CBR/VBR frames (from 0 to 120 ms) */
535 return OPUS_CORRUPTED_DATA;
536 /* Number of frames encoded in bits 0 to 5 */
539 if (count <= 0 || framesize*count > 5760)
540 return OPUS_CORRUPTED_DATA;
542 /* Padding flag is bit 6 */
549 return OPUS_CORRUPTED_DATA;
552 padding += p==255 ? 254: p;
557 return OPUS_CORRUPTED_DATA;
558 /* VBR flag is bit 7 */
564 for (i=0;i<count-1;i++)
566 bytes = parse_size(data, len, size+i);
568 if (size[i]<0 || size[i] > len)
569 return OPUS_CORRUPTED_DATA;
571 last_size -= bytes+size[i];
574 return OPUS_CORRUPTED_DATA;
575 } else if (!self_delimited)
578 last_size = len/count;
579 if (last_size*count!=len)
580 return OPUS_CORRUPTED_DATA;
581 for (i=0;i<count-1;i++)
586 /* Self-delimited framing has an extra size for the last frame. */
589 bytes = parse_size(data, len, size+count-1);
591 if (size[count-1]<0 || size[count-1] > len)
592 return OPUS_CORRUPTED_DATA;
594 /* For CBR packets, apply the size to all the frames. */
597 if (size[count-1]*count > len)
598 return OPUS_CORRUPTED_DATA;
599 for (i=0;i<count-1;i++)
600 size[i] = size[count-1];
601 } else if(size[count-1] > last_size)
602 return OPUS_CORRUPTED_DATA;
605 /* Because it's not encoded explicitly, it's possible the size of the
606 last packet (or all the packets, for the CBR case) is larger than
609 if (last_size > 1275)
610 return OPUS_CORRUPTED_DATA;
611 size[count-1] = last_size;
616 for (i=0;i<count;i++)
627 *payload_offset = data-data0;
632 int opus_packet_parse(const unsigned char *data, int len,
633 unsigned char *out_toc, const unsigned char *frames[48],
634 short size[48], int *payload_offset)
636 return opus_packet_parse_impl(data, len, 0,
637 out_toc, frames, size, payload_offset);
640 int opus_decode_native(OpusDecoder *st, const unsigned char *data,
641 int len, opus_val16 *pcm, int frame_size, int decode_fec, int self_delimited, int *packet_offset)
647 /* 48 x 2.5 ms = 120 ms */
649 if (len==0 || data==NULL)
650 return opus_decode_frame(st, NULL, 0, pcm, frame_size, 0);
655 st->mode = opus_packet_get_mode(data);
656 st->bandwidth = opus_packet_get_bandwidth(data);
657 st->frame_size = opus_packet_get_samples_per_frame(data, st->Fs);
658 st->stream_channels = opus_packet_get_nb_channels(data);
660 count = opus_packet_parse_impl(data, len, 0, &toc, NULL, size, &offset);
665 tot_offset += offset;
667 if (count*st->frame_size > frame_size)
670 for (i=0;i<count;i++)
673 ret = opus_decode_frame(st, data, size[i], pcm, frame_size-nb_samples, decode_fec);
677 tot_offset += size[i];
678 pcm += ret*st->channels;
681 if (packet_offset != NULL)
682 *packet_offset = tot_offset;
688 int opus_decode(OpusDecoder *st, const unsigned char *data,
689 int len, opus_val16 *pcm, int frame_size, int decode_fec)
691 return opus_decode_native(st, data, len, pcm, frame_size, decode_fec, 0, NULL);
694 #ifndef DISABLE_FLOAT_API
695 int opus_decode_float(OpusDecoder *st, const unsigned char *data,
696 int len, float *pcm, int frame_size, int decode_fec)
698 VARDECL(opus_int16, out);
702 ALLOC(out, frame_size*st->channels, opus_int16);
704 ret = opus_decode_native(st, data, len, out, frame_size, decode_fec, 0, NULL);
707 for (i=0;i<ret*st->channels;i++)
708 pcm[i] = (1./32768.)*(out[i]);
717 int opus_decode(OpusDecoder *st, const unsigned char *data,
718 int len, opus_int16 *pcm, int frame_size, int decode_fec)
724 ALLOC(out, frame_size*st->channels, float);
726 ret = opus_decode_native(st, data, len, out, frame_size, decode_fec, 0, NULL);
729 for (i=0;i<ret*st->channels;i++)
730 pcm[i] = FLOAT2INT16(out[i]);
736 int opus_decode_float(OpusDecoder *st, const unsigned char *data,
737 int len, opus_val16 *pcm, int frame_size, int decode_fec)
739 return opus_decode_native(st, data, len, pcm, frame_size, decode_fec, 0, NULL);
744 int opus_decoder_ctl(OpusDecoder *st, int request, ...)
748 va_start(ap, request);
752 case OPUS_GET_BANDWIDTH_REQUEST:
754 opus_int32 *value = va_arg(ap, opus_int32*);
755 *value = st->bandwidth;
758 case OPUS_GET_FINAL_RANGE_REQUEST:
760 opus_uint32 *value = va_arg(ap, opus_uint32*);
761 *value = st->rangeFinal;
765 fprintf(stderr, "unknown opus_decoder_ctl() request: %d", request);
773 void opus_decoder_destroy(OpusDecoder *st)
779 int opus_packet_get_bandwidth(const unsigned char *data)
784 bandwidth = OPUS_BANDWIDTH_MEDIUMBAND + ((data[0]>>5)&0x3);
785 if (bandwidth == OPUS_BANDWIDTH_MEDIUMBAND)
786 bandwidth = OPUS_BANDWIDTH_NARROWBAND;
787 } else if ((data[0]&0x60) == 0x60)
789 bandwidth = (data[0]&0x10) ? OPUS_BANDWIDTH_FULLBAND : OPUS_BANDWIDTH_SUPERWIDEBAND;
792 bandwidth = OPUS_BANDWIDTH_NARROWBAND + ((data[0]>>5)&0x3);
797 int opus_packet_get_samples_per_frame(const unsigned char *data, int Fs)
802 audiosize = ((data[0]>>3)&0x3);
803 audiosize = (Fs<<audiosize)/400;
804 } else if ((data[0]&0x60) == 0x60)
806 audiosize = (data[0]&0x08) ? Fs/50 : Fs/100;
809 audiosize = ((data[0]>>3)&0x3);
811 audiosize = Fs*60/1000;
813 audiosize = (Fs<<audiosize)/100;
818 int opus_packet_get_nb_channels(const unsigned char *data)
820 return (data[0]&0x4) ? 2 : 1;
823 int opus_packet_get_nb_frames(const unsigned char packet[], int len)
828 count = packet[0]&0x3;
834 return OPUS_CORRUPTED_DATA;
836 return packet[1]&0x3F;
839 int opus_decoder_get_nb_samples(const OpusDecoder *dec, const unsigned char packet[], int len)
842 int count = opus_packet_get_nb_frames(packet, len);
843 samples = count*opus_packet_get_samples_per_frame(packet, dec->Fs);
844 /* Can't have more than 120 ms */
845 if (samples*25 > dec->Fs*3)
846 return OPUS_CORRUPTED_DATA;