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.
35 #include "opus_decoder.h"
38 #include "SKP_Silk_SDK_API.h"
41 OpusDecoder *opus_decoder_create(int Fs, int channels)
44 int ret, silkDecSizeBytes, celtDecSizeBytes;
47 /* Initialize SILK encoder */
48 ret = SKP_Silk_SDK_Get_Decoder_Size( &silkDecSizeBytes );
52 celtDecSizeBytes = celt_decoder_get_size(channels);
53 raw_state = calloc(sizeof(OpusDecoder)+silkDecSizeBytes+celtDecSizeBytes, 1);
54 st = (OpusDecoder*)raw_state;
55 st->silk_dec = (void*)(raw_state+sizeof(OpusDecoder));
56 st->celt_dec = (CELTDecoder*)(raw_state+sizeof(OpusDecoder)+silkDecSizeBytes);
57 st->stream_channels = st->channels = channels;
62 ret = SKP_Silk_SDK_InitDecoder( st->silk_dec );
67 /* Initialize CELT decoder */
68 st->celt_dec = celt_decoder_init(st->celt_dec, Fs, channels, NULL);
74 int opus_decode(OpusDecoder *st, const unsigned char *data,
75 int len, short *pcm, int frame_size, int decode_fec)
77 int i, silk_ret=0, celt_ret=0;
79 SKP_SILK_SDK_DecControlStruct DecControl;
80 SKP_int32 silk_frame_size;
81 short pcm_celt[960*2];
82 short pcm_transition[960*2];
87 /* Payloads of 1 (2 including ToC) or 0 trigger the PLC/DTX */
93 /* Decoding mode/bandwidth/framesize from first byte */
96 mode = MODE_CELT_ONLY;
97 st->bandwidth = BANDWIDTH_MEDIUMBAND + ((data[0]>>5)&0x3);
98 if (st->bandwidth == BANDWIDTH_MEDIUMBAND)
99 st->bandwidth = BANDWIDTH_NARROWBAND;
100 audiosize = ((data[0]>>3)&0x3);
101 audiosize = (st->Fs<<audiosize)/400;
102 } else if ((data[0]&0x60) == 0x60)
105 st->bandwidth = (data[0]&0x10) ? BANDWIDTH_FULLBAND : BANDWIDTH_SUPERWIDEBAND;
106 audiosize = (data[0]&0x08) ? st->Fs/50 : st->Fs/100;
109 mode = MODE_SILK_ONLY;
110 st->bandwidth = BANDWIDTH_NARROWBAND + ((data[0]>>5)&0x3);
111 audiosize = ((data[0]>>3)&0x3);
113 audiosize = st->Fs*60/1000;
115 audiosize = (st->Fs<<audiosize)/100;
117 st->stream_channels = (data[0]&0x4) ? 2 : 1;
118 /*printf ("%d %d %d\n", st->mode, st->bandwidth, audiosize);*/
122 ec_dec_init(&dec,(unsigned char*)data,len);
124 audiosize = frame_size;
125 mode = st->prev_mode;
128 if (mode != st->prev_mode && st->prev_mode > 0
129 && !(mode == MODE_SILK_ONLY && st->prev_mode == MODE_HYBRID)
130 && !(mode == MODE_HYBRID && st->prev_mode == MODE_SILK_ONLY))
133 opus_decode(st, NULL, 0, pcm_transition, IMAX(480, audiosize), 0);
135 if (audiosize > frame_size)
137 fprintf(stderr, "PCM buffer too small");
140 frame_size = audiosize;
143 /* SILK processing */
144 if (mode != MODE_CELT_ONLY)
146 int lost_flag, decoded_samples;
147 SKP_int16 *pcm_ptr = pcm;
149 if (st->prev_mode==MODE_CELT_ONLY)
150 SKP_Silk_SDK_InitDecoder( st->silk_dec );
152 DecControl.API_sampleRate = st->Fs;
153 DecControl.payloadSize_ms = 1000 * audiosize / st->Fs;
154 if( mode == MODE_SILK_ONLY ) {
155 if( st->bandwidth == BANDWIDTH_NARROWBAND ) {
156 DecControl.internalSampleRate = 8000;
157 } else if( st->bandwidth == BANDWIDTH_MEDIUMBAND ) {
158 DecControl.internalSampleRate = 12000;
159 } else if( st->bandwidth == BANDWIDTH_WIDEBAND ) {
160 DecControl.internalSampleRate = 16000;
166 DecControl.internalSampleRate = 16000;
169 lost_flag = data == NULL ? 1 : 2 * decode_fec;
172 /* Call SILK decoder */
173 int first_frame = decoded_samples == 0;
174 silk_ret = SKP_Silk_SDK_Decode( st->silk_dec, &DecControl,
175 lost_flag, first_frame, &dec, len, pcm_ptr, &silk_frame_size );
177 fprintf (stderr, "SILK decode error\n");
180 pcm_ptr += silk_frame_size;
181 decoded_samples += silk_frame_size;
182 } while( decoded_samples < frame_size );
184 for (i=0;i<frame_size*st->channels;i++)
188 if (mode == MODE_HYBRID)
190 /* This should be adjusted based on the SILK bandwidth */
191 celt_decoder_ctl(st->celt_dec, CELT_SET_START_BAND(17));
193 celt_decoder_ctl(st->celt_dec, CELT_SET_START_BAND(0));
196 if (mode != MODE_SILK_ONLY)
200 switch(st->bandwidth)
202 case BANDWIDTH_NARROWBAND:
205 case BANDWIDTH_WIDEBAND:
208 case BANDWIDTH_SUPERWIDEBAND:
211 case BANDWIDTH_FULLBAND:
215 celt_decoder_ctl(st->celt_dec, CELT_SET_END_BAND(endband));
216 celt_decoder_ctl(st->celt_dec, CELT_SET_CHANNELS(st->stream_channels));
218 if (st->prev_mode == MODE_SILK_ONLY)
219 celt_decoder_ctl(st->celt_dec, CELT_RESET_STATE);
221 celt_ret = celt_decode_with_ec(st->celt_dec, decode_fec?NULL:data, len, pcm_celt, frame_size, &dec);
222 for (i=0;i<frame_size*st->channels;i++)
223 pcm[i] = ADD_SAT16(pcm[i], pcm_celt[i]);
228 int plc_length, overlap;
229 if (mode == MODE_CELT_ONLY)
230 plc_length = IMIN(audiosize, 10+st->Fs/200);
232 plc_length = IMIN(audiosize, 10+st->Fs/400);
233 for (i=0;i<plc_length;i++)
234 pcm[i] = pcm_transition[i];
236 overlap = IMIN(480, IMAX(0, audiosize-plc_length));
237 for (i=0;i<overlap;i++)
238 pcm[plc_length+i] = (i*pcm[plc_length+i] + (overlap-i)*pcm_transition[plc_length+i])/overlap;
240 #if OPUS_TEST_RANGE_CODER_STATE
241 st->rangeFinal = dec.rng;
244 st->prev_mode = mode;
245 return celt_ret<0 ? celt_ret : audiosize;
249 void opus_decoder_ctl(OpusDecoder *st, int request, ...)
253 va_start(ap, request);
257 case OPUS_GET_MODE_REQUEST:
259 int *value = va_arg(ap, int*);
260 *value = st->prev_mode;
263 case OPUS_SET_BANDWIDTH_REQUEST:
265 int value = va_arg(ap, int);
266 st->bandwidth = value;
269 case OPUS_GET_BANDWIDTH_REQUEST:
271 int *value = va_arg(ap, int*);
272 *value = st->bandwidth;
276 fprintf(stderr, "unknown opus_decoder_ctl() request: %d", request);
283 void opus_decoder_destroy(OpusDecoder *st)
288 #if OPUS_TEST_RANGE_CODER_STATE
289 int opus_decoder_get_final_range(OpusDecoder *st)
291 return st->rangeFinal;