1 /***********************************************************************
2 Copyright (c) 2006-2010, Skype Limited. All rights reserved.
3 Redistribution and use in source and binary forms, with or without
4 modification, (subject to the limitations in the disclaimer below)
5 are permitted provided that the following conditions are met:
6 - Redistributions of source code must retain the above copyright notice,
7 this list of conditions and the following disclaimer.
8 - Redistributions in binary form must reproduce the above copyright
9 notice, this list of conditions and the following disclaimer in the
10 documentation and/or other materials provided with the distribution.
11 - Neither the name of Skype Limited, nor the names of specific
12 contributors, may be used to endorse or promote products derived from
13 this software without specific prior written permission.
14 NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED
15 BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
16 CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
17 BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
22 USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
23 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 ***********************************************************************/
27
28 #include "SKP_Silk_SDK_API.h"
29 #include "SKP_Silk_main_FIX.h"
30
31 /*********************/
32 /* Decoder functions */
33 /*********************/
34
SKP_Silk_SDK_Get_Decoder_Size(SKP_int32 * decSizeBytes)35 SKP_int SKP_Silk_SDK_Get_Decoder_Size( SKP_int32 *decSizeBytes )
36 {
37 SKP_int ret = 0;
38
39 *decSizeBytes = sizeof( SKP_Silk_decoder_state );
40
41 return ret;
42 }
43
44 /* Reset decoder state */
SKP_Silk_SDK_InitDecoder(void * decState)45 SKP_int SKP_Silk_SDK_InitDecoder(
46 void* decState /* I/O: State */
47 )
48 {
49 SKP_int ret = 0;
50 SKP_Silk_decoder_state *struc;
51
52 struc = (SKP_Silk_decoder_state *)decState;
53
54 ret = SKP_Silk_init_decoder( struc );
55
56 return ret;
57 }
58
59 /* Decode a frame */
SKP_Silk_SDK_Decode(void * decState,SKP_SILK_SDK_DecControlStruct * decControl,SKP_int lostFlag,const SKP_uint8 * inData,const SKP_int nBytesIn,SKP_int16 * samplesOut,SKP_int16 * nSamplesOut)60 SKP_int SKP_Silk_SDK_Decode(
61 void* decState, /* I/O: State */
62 SKP_SILK_SDK_DecControlStruct* decControl, /* I/O: Control structure */
63 SKP_int lostFlag, /* I: 0: no loss, 1 loss */
64 const SKP_uint8 *inData, /* I: Encoded input vector */
65 const SKP_int nBytesIn, /* I: Number of input Bytes */
66 SKP_int16 *samplesOut, /* O: Decoded output speech vector */
67 SKP_int16 *nSamplesOut /* I/O: Number of samples (vector/decoded) */
68 )
69 {
70 SKP_int ret = 0, used_bytes, prev_fs_kHz;
71 SKP_Silk_decoder_state *psDec;
72
73 psDec = (SKP_Silk_decoder_state *)decState;
74
75 /**********************************/
76 /* Test if first frame in payload */
77 /**********************************/
78 if( psDec->moreInternalDecoderFrames == 0 ) {
79 /* First Frame in Payload */
80 psDec->nFramesDecoded = 0; /* Used to count frames in packet */
81 }
82
83 if( psDec->moreInternalDecoderFrames == 0 && /* First frame in packet */
84 lostFlag == 0 && /* Not packet loss */
85 nBytesIn > MAX_ARITHM_BYTES ) { /* Too long payload */
86 /* Avoid trying to decode a too large packet */
87 lostFlag = 1;
88 ret = SKP_SILK_DEC_PAYLOAD_TOO_LARGE;
89 }
90
91 /* Save previous sample frequency */
92 prev_fs_kHz = psDec->fs_kHz;
93
94 /* Call decoder for one frame */
95 ret += SKP_Silk_decode_frame( psDec, samplesOut, nSamplesOut, inData, nBytesIn,
96 lostFlag, &used_bytes );
97
98 if( used_bytes ) { /* Only Call if not a packet loss */
99 if( psDec->nBytesLeft > 0 && psDec->FrameTermination == SKP_SILK_MORE_FRAMES && psDec->nFramesDecoded < 5 ) {
100 /* We have more frames in the Payload */
101 psDec->moreInternalDecoderFrames = 1;
102 } else {
103 /* Last frame in Payload */
104 psDec->moreInternalDecoderFrames = 0;
105 psDec->nFramesInPacket = psDec->nFramesDecoded;
106
107 /* Track inband FEC usage */
108 if( psDec->vadFlag == VOICE_ACTIVITY ) {
109 if( psDec->FrameTermination == SKP_SILK_LAST_FRAME ) {
110 psDec->no_FEC_counter++;
111 if( psDec->no_FEC_counter > NO_LBRR_THRES ) {
112 psDec->inband_FEC_offset = 0;
113 }
114 } else if( psDec->FrameTermination == SKP_SILK_LBRR_VER1 ) {
115 psDec->inband_FEC_offset = 1; /* FEC info with 1 packet delay */
116 psDec->no_FEC_counter = 0;
117 } else if( psDec->FrameTermination == SKP_SILK_LBRR_VER2 ) {
118 psDec->inband_FEC_offset = 2; /* FEC info with 2 packets delay */
119 psDec->no_FEC_counter = 0;
120 }
121 }
122 }
123 }
124
125 if( psDec->fs_kHz * 1000 > decControl->sampleRate ) {
126 ret = SKP_SILK_DEC_WRONG_SAMPLING_FREQUENCY;
127 }
128
129 /* Do any resampling if needed */
130 if( psDec->fs_kHz * 1000 != decControl->sampleRate ) {
131 SKP_int16 samplesOut_tmp[ 2 * MAX_FRAME_LENGTH ];
132 SKP_int32 scratch[ 3 * MAX_FRAME_LENGTH ];
133
134 /* Copy to a tmpbuffer as the resampling writes to samplesOut */
135 memcpy( samplesOut_tmp, samplesOut, *nSamplesOut * sizeof( SKP_int16 ) );
136
137 /* Clear resampler state when switching internal sampling frequency */
138 if( prev_fs_kHz != psDec->fs_kHz ) {
139 SKP_memset( psDec->resampleState, 0, sizeof( psDec->resampleState ) );
140 }
141
142 if( psDec->fs_kHz == 16 && decControl->sampleRate == 24000 ) {
143 /* Resample from 16 kHz to 24 kHz */
144 SKP_Silk_resample_3_2( samplesOut, psDec->resampleState, samplesOut_tmp, *nSamplesOut );
145 } else if( psDec->fs_kHz == 12 && decControl->sampleRate == 24000 ) {
146 /* Resample from 12 kHz to 24 kHz */
147 SKP_Silk_resample_2_1_coarse( samplesOut_tmp, psDec->resampleState, samplesOut, scratch, *nSamplesOut );
148 } else if( psDec->fs_kHz == 8 && decControl->sampleRate == 24000 ) {
149 /* Resample from 8 kHz to 24 kHz */
150 SKP_Silk_resample_3_1( samplesOut, psDec->resampleState, samplesOut_tmp, *nSamplesOut );
151 } else if( psDec->fs_kHz == 12 && decControl->sampleRate == 16000 ) {
152 /* Resample from 12 kHz to 16 kHz */
153 SKP_Silk_resample_4_3( samplesOut, psDec->resampleState, samplesOut_tmp, *nSamplesOut );
154 } else if( psDec->fs_kHz == 8 && decControl->sampleRate == 16000 ) {
155 /* Resample from 8 kHz to 16 kHz */
156 SKP_Silk_resample_2_1_coarse( samplesOut_tmp, psDec->resampleState, samplesOut, scratch, *nSamplesOut );
157 } else if( psDec->fs_kHz == 8 && decControl->sampleRate == 12000 ) {
158 /* Resample from 8 kHz to 12 kHz */
159 SKP_Silk_resample_3_2( samplesOut, psDec->resampleState, samplesOut_tmp, *nSamplesOut );
160 }
161
162 *nSamplesOut = SKP_DIV32( ( SKP_int32 )*nSamplesOut * decControl->sampleRate, psDec->fs_kHz * 1000 );
163 }
164
165 /* Copy all parameters that are needed out of internal structure to the control stucture */
166 decControl->frameSize = ( SKP_int )psDec->frame_length;
167 decControl->framesPerPacket = ( SKP_int )psDec->nFramesInPacket;
168 decControl->inBandFECOffset = ( SKP_int )psDec->inband_FEC_offset;
169 decControl->moreInternalDecoderFrames = ( SKP_int )psDec->moreInternalDecoderFrames;
170
171 return ret;
172 }
173
174 /* Function to find LBRR information in a packet */
SKP_Silk_SDK_search_for_LBRR(void * decState,const SKP_uint8 * inData,const SKP_int16 nBytesIn,SKP_int lost_offset,SKP_uint8 * LBRRData,SKP_int16 * nLBRRBytes)175 void SKP_Silk_SDK_search_for_LBRR(
176 void *decState, /* I: Decoder state, to select bitstream version only */
177 const SKP_uint8 *inData, /* I: Encoded input vector */
178 const SKP_int16 nBytesIn, /* I: Number of input Bytes */
179 SKP_int lost_offset, /* I: Offset from lost packet */
180 SKP_uint8 *LBRRData, /* O: LBRR payload */
181 SKP_int16 *nLBRRBytes /* O: Number of LBRR Bytes */
182 )
183 {
184 SKP_Silk_decoder_state *psDec;
185 SKP_Silk_decoder_state sDec; // Local decoder state to avoid interfering with running decoder */
186 SKP_Silk_decoder_control sDecCtrl;
187 SKP_int i, TempQ[ MAX_FRAME_LENGTH ];
188
189 psDec = ( SKP_Silk_decoder_state * )decState;
190
191 if( lost_offset < 1 || lost_offset > MAX_LBRR_DELAY ) {
192 /* No useful FEC in this packet */
193 *nLBRRBytes = 0;
194 return;
195 }
196
197 sDec.nFramesDecoded = 0;
198 sDec.fs_kHz = 0; /* Force update parameters LPC_order etc */
199 SKP_memset( sDec.prevNLSF_Q15, 0, MAX_LPC_ORDER * sizeof( SKP_int ) );
200 SKP_Silk_range_dec_init( &sDec.sRC, inData, ( SKP_int32 )nBytesIn );
201
202 if( psDec->bitstream_v == BIT_STREAM_V4 ) { /* Silk_v4 payload */
203 /* Decode all parameter indices for the whole packet*/
204 SKP_Silk_decode_indices_v4( &sDec );
205
206 /* Is there usable LBRR in this packet */
207 *nLBRRBytes = 0;
208 if( ( sDec.FrameTermination - 1 ) & lost_offset && sDec.FrameTermination > 0 && sDec.nBytesLeft >= 0 ) {
209 /* The wanted FEC is present in the packet */
210 for( i = 0; i < sDec.nFramesInPacket; i++ ) {
211 SKP_Silk_decode_parameters_v4( &sDec, &sDecCtrl, TempQ, 0 );
212
213 if( sDec.nBytesLeft <= 0 || sDec.sRC.error ) {
214 /* Corrupt stream */
215 LBRRData = NULL;
216 *nLBRRBytes = 0;
217 break;
218 } else {
219 sDec.nFramesDecoded++;
220 }
221 }
222
223 if( LBRRData != NULL ) {
224 /* The wanted FEC is present in the packet */
225 *nLBRRBytes = sDec.nBytesLeft;
226 SKP_memcpy( LBRRData, &inData[ nBytesIn - sDec.nBytesLeft ], sDec.nBytesLeft * sizeof( SKP_uint8 ) );
227 }
228 }
229 } else { /* Silk_v3 payload */
230 while(1) {
231 SKP_Silk_decode_parameters( &sDec, &sDecCtrl, TempQ, 0 );
232
233 if( sDec.sRC.error ) {
234 /* Corrupt stream */
235 *nLBRRBytes = 0;
236 return;
237 };
238
239 if( ( sDec.FrameTermination - 1 ) & lost_offset && sDec.FrameTermination > 0 && sDec.nBytesLeft >= 0 ) {
240 /* The wanted FEC is present in the packet */
241 *nLBRRBytes = sDec.nBytesLeft;
242 SKP_memcpy( LBRRData, &inData[ nBytesIn - sDec.nBytesLeft ], sDec.nBytesLeft * sizeof( SKP_uint8 ) );
243 break;
244 }
245 if( sDec.nBytesLeft > 0 && sDec.FrameTermination == SKP_SILK_MORE_FRAMES ) {
246 sDec.nFramesDecoded++;
247 } else {
248 LBRRData = NULL;
249 *nLBRRBytes = 0;
250 break;
251 }
252 }
253 }
254 }
255
256 /* Getting type of content for a packet */
SKP_Silk_SDK_get_TOC(void * decState,const SKP_uint8 * inData,const SKP_int16 nBytesIn,SKP_Silk_TOC_struct * Silk_TOC)257 void SKP_Silk_SDK_get_TOC(
258 void *decState, /* I/O: Decoder state, to select bitstream version only */
259 const SKP_uint8 *inData, /* I: Encoded input vector */
260 const SKP_int16 nBytesIn, /* I: Number of input bytes */
261 SKP_Silk_TOC_struct *Silk_TOC /* O: Type of content */
262 )
263 {
264 SKP_Silk_decoder_state *psDec;
265 SKP_Silk_decoder_state sDec; // Local Decoder state to avoid interfering with running decoder */
266 SKP_Silk_decoder_control sDecCtrl;
267 SKP_int i, TempQ[ MAX_FRAME_LENGTH ];
268
269 psDec = (SKP_Silk_decoder_state *)decState;
270
271 sDec.nFramesDecoded = 0;
272 sDec.fs_kHz = 0; /* Force update parameters LPC_order etc */
273 SKP_Silk_range_dec_init( &sDec.sRC, inData, ( SKP_int32 )nBytesIn );
274
275 if( psDec->bitstream_v == BIT_STREAM_V4 ) { /* Silk_v4 payload */
276 /* Decode all parameter indices for the whole packet*/
277 SKP_Silk_decode_indices_v4( &sDec );
278
279 if( sDec.nFramesInPacket > SILK_MAX_FRAMES_PER_PACKET || sDec.sRC.error ) {
280 /* Corrupt packet */
281 SKP_memset( Silk_TOC, 0, sizeof( SKP_Silk_TOC_struct ) );
282 Silk_TOC->corrupt = 1;
283 } else {
284 Silk_TOC->corrupt = 0;
285 Silk_TOC->framesInPacket = sDec.nFramesInPacket;
286 Silk_TOC->fs_kHz = sDec.fs_kHz;
287 if( sDec.FrameTermination == SKP_SILK_LAST_FRAME ) {
288 Silk_TOC->inbandLBRR = sDec.FrameTermination;
289 } else {
290 Silk_TOC->inbandLBRR = sDec.FrameTermination - 1;
291 }
292 /* Copy data */
293 for( i = 0; i < sDec.nFramesInPacket; i++ ) {
294 Silk_TOC->vadFlags[ i ] = sDec.vadFlagBuf[ i ];
295 Silk_TOC->sigtypeFlags[ i ] = sDec.sigtype[ i ];
296 }
297 }
298 } else { /* Silk_v3 payload */
299 Silk_TOC->corrupt = 0;
300 while( 1 ) {
301 SKP_Silk_decode_parameters( &sDec, &sDecCtrl, TempQ, 0 );
302
303 Silk_TOC->vadFlags[ sDec.nFramesDecoded ] = sDec.vadFlag;
304 Silk_TOC->sigtypeFlags[ sDec.nFramesDecoded ] = sDecCtrl.sigtype;
305
306 if( sDec.sRC.error ) {
307 /* Corrupt stream */
308 Silk_TOC->corrupt = 1;
309 break;
310 };
311
312 if( sDec.nBytesLeft > 0 && sDec.FrameTermination == SKP_SILK_MORE_FRAMES ) {
313 sDec.nFramesDecoded++;
314 } else {
315 break;
316 }
317 }
318 if( Silk_TOC->corrupt || sDec.FrameTermination == SKP_SILK_MORE_FRAMES ||
319 sDec.nFramesInPacket > SILK_MAX_FRAMES_PER_PACKET ) {
320 /* Corrupt packet */
321 SKP_memset( Silk_TOC, 0, sizeof( SKP_Silk_TOC_struct ) );
322 Silk_TOC->corrupt = 1;
323 } else {
324 Silk_TOC->framesInPacket = sDec.nFramesDecoded;
325 Silk_TOC->fs_kHz = sDec.fs_kHz;
326 if( sDec.FrameTermination == SKP_SILK_LAST_FRAME ) {
327 Silk_TOC->inbandLBRR = sDec.FrameTermination;
328 } else {
329 Silk_TOC->inbandLBRR = sDec.FrameTermination - 1;
330 }
331 }
332 }
333 }
334
335 /**************************/
336 /* Get the version number */
337 /**************************/
338 /* Return a pointer to string specifying the version */
SKP_Silk_SDK_get_version()339 const char *SKP_Silk_SDK_get_version()
340 {
341 static const char version[] = "1.0.2";
342 return version;
343 }
344