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 - Neither the name of the Xiph.org Foundation nor the names of its
16 contributors may be used to endorse or promote products derived from
17 this software without specific prior written permission.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
23 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include "opus_decoder.h"
42 #include "SKP_Silk_SDK_API.h"
45 OpusDecoder *opus_decoder_create(int Fs, int channels)
48 int ret, silkDecSizeBytes, celtDecSizeBytes;
51 /* Initialize SILK encoder */
52 ret = SKP_Silk_SDK_Get_Decoder_Size( &silkDecSizeBytes );
56 celtDecSizeBytes = celt_decoder_get_size(channels);
57 raw_state = calloc(sizeof(OpusDecoder)+silkDecSizeBytes+celtDecSizeBytes, 1);
58 st = (OpusDecoder*)raw_state;
59 st->silk_dec = (void*)(raw_state+sizeof(OpusDecoder));
60 st->celt_dec = (CELTDecoder*)(raw_state+sizeof(OpusDecoder)+silkDecSizeBytes);
61 st->channels = channels;
66 ret = SKP_Silk_SDK_InitDecoder( st->silk_dec );
71 /* Initialize CELT decoder */
72 st->celt_dec = celt_decoder_init(st->celt_dec, 48000, channels, NULL);
77 int opus_decode(OpusDecoder *st, const unsigned char *data,
78 int len, short *pcm, int frame_size)
80 int i, silk_ret=0, celt_ret=0;
83 SKP_SILK_SDK_DecControlStruct DecControl;
84 SKP_int32 silk_frame_size;
85 short pcm_celt[960*2];
90 /* Decoding mode/bandwidth/framesize from first byte */
93 st->mode = MODE_CELT_ONLY;
94 st->bandwidth = BANDWIDTH_MEDIUMBAND + ((data[0]>>5)&0x3);
95 if (st->bandwidth == BANDWIDTH_MEDIUMBAND)
96 st->bandwidth = BANDWIDTH_NARROWBAND;
97 audiosize = ((data[0]>>3)&0x3);
98 audiosize = (st->Fs<<audiosize)/400;
99 } else if ((data[0]&0x60) == 0x60)
101 st->mode = MODE_HYBRID;
102 st->bandwidth = (data[0]&0x10) ? BANDWIDTH_FULLBAND : BANDWIDTH_SUPERWIDEBAND;
103 audiosize = (data[0]&0x08) ? st->Fs/50 : st->Fs/100;
106 st->mode = MODE_SILK_ONLY;
107 st->bandwidth = BANDWIDTH_NARROWBAND + ((data[0]>>5)&0x3);
108 audiosize = ((data[0]>>3)&0x3);
110 audiosize = st->Fs*60/1000;
112 audiosize = (st->Fs<<audiosize)/100;
114 /*printf ("%d %d %d\n", st->mode, st->bandwidth, audiosize);*/
118 ec_byte_readinit(&buf,(unsigned char*)data,len);
119 ec_dec_init(&dec,&buf);
122 if (st->mode != MODE_CELT_ONLY)
124 DecControl.API_sampleRate = st->Fs;
125 DecControl.payloadSize_ms = 1000 * audiosize / st->Fs;
126 if( st->mode == MODE_SILK_ONLY ) {
127 if( st->bandwidth == BANDWIDTH_NARROWBAND ) {
128 DecControl.internalSampleRate = 8000;
129 } else if( st->bandwidth == BANDWIDTH_MEDIUMBAND ) {
130 DecControl.internalSampleRate = 12000;
131 } else if( st->bandwidth == BANDWIDTH_WIDEBAND ) {
132 DecControl.internalSampleRate = 16000;
138 DecControl.internalSampleRate = 16000;
141 /* We Should eventually have to set the bandwidth here */
143 /* Call SILK encoder for the low band */
144 silk_ret = SKP_Silk_SDK_Decode( st->silk_dec, &DecControl, data == NULL, &dec, len, pcm, &silk_frame_size );
147 fprintf (stderr, "SILK decode error\n");
151 for (i=0;i<frame_size*st->channels;i++)
155 if (st->mode == MODE_HYBRID)
157 /* This should be adjusted based on the SILK bandwidth */
158 celt_decoder_ctl(st->celt_dec, CELT_SET_START_BAND(17));
160 celt_decoder_ctl(st->celt_dec, CELT_SET_START_BAND(0));
163 if (st->mode != MODE_SILK_ONLY && st->bandwidth > BANDWIDTH_WIDEBAND)
167 switch(st->bandwidth)
169 case BANDWIDTH_NARROWBAND:
172 case BANDWIDTH_WIDEBAND:
175 case BANDWIDTH_SUPERWIDEBAND:
178 case BANDWIDTH_FULLBAND:
182 celt_decoder_ctl(st->celt_dec, CELT_SET_END_BAND(endband));
184 /* Encode high band with CELT */
185 celt_ret = celt_decode_with_ec(st->celt_dec, data, len, pcm_celt, frame_size, &dec);
186 for (i=0;i<frame_size*st->channels;i++)
187 pcm[i] += pcm_celt[i];
193 void opus_decoder_ctl(OpusDecoder *st, int request, ...)
197 va_start(ap, request);
201 case OPUS_SET_MODE_REQUEST:
203 int value = va_arg(ap, int);
207 case OPUS_GET_MODE_REQUEST:
209 int *value = va_arg(ap, int*);
213 case OPUS_SET_BANDWIDTH_REQUEST:
215 int value = va_arg(ap, int);
216 st->bandwidth = value;
219 case OPUS_GET_BANDWIDTH_REQUEST:
221 int *value = va_arg(ap, int*);
222 *value = st->bandwidth;
226 fprintf(stderr, "unknown opus_decoder_ctl() request: %d", request);
233 void opus_decoder_destroy(OpusDecoder *st)