Reformatting changes with an update to the MSVC project files
[opus.git] / silk / dec_API.c
1 /***********************************************************************
2 Copyright (c) 2006-2011, 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 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31 #include "API.h"
32 #include "main.h"
33
34 /************************/
35 /* Decoder Super Struct */
36 /************************/
37 typedef struct {
38     silk_decoder_state          channel_state[ DECODER_NUM_CHANNELS ];
39     stereo_dec_state                sStereo;
40     opus_int                         nChannelsAPI;
41     opus_int                         nChannelsInternal;
42     opus_int                         prev_decode_only_middle;
43 } silk_decoder;
44
45 /*********************/
46 /* Decoder functions */
47 /*********************/
48
49 opus_int silk_Get_Decoder_Size(                         /* O    Returns error code                              */
50     opus_int                        *decSizeBytes       /* O    Number of bytes in SILK decoder state           */
51 )
52 {
53     opus_int ret = SILK_NO_ERROR;
54
55     *decSizeBytes = sizeof( silk_decoder );
56
57     return ret;
58 }
59
60 /* Reset decoder state */
61 opus_int silk_InitDecoder(                              /* O    Returns error code                              */
62     void                            *decState           /* I/O  State                                           */
63 )
64 {
65     opus_int n, ret = SILK_NO_ERROR;
66     silk_decoder_state *channel_state = ((silk_decoder *)decState)->channel_state;
67
68     for( n = 0; n < DECODER_NUM_CHANNELS; n++ ) {
69         ret  = silk_init_decoder( &channel_state[ n ] );
70     }
71
72     return ret;
73 }
74
75 /* Decode a frame */
76 opus_int silk_Decode(                                   /* O    Returns error code                              */
77     void*                           decState,           /* I/O  State                                           */
78     silk_DecControlStruct*          decControl,         /* I/O  Control Structure                               */
79     opus_int                        lostFlag,           /* I    0: no loss, 1 loss, 2 decode fec                */
80     opus_int                        newPacketFlag,      /* I    Indicates first decoder call for this packet    */
81     ec_dec                          *psRangeDec,        /* I/O  Compressor data structure                       */
82     opus_int16                      *samplesOut,        /* O    Decoded output speech vector                    */
83     opus_int32                      *nSamplesOut        /* O    Number of samples decoded                       */
84 )
85 {
86     opus_int   i, n, delay, decode_only_middle = 0, ret = SILK_NO_ERROR;
87     opus_int32 nSamplesOutDec, LBRR_symbol;
88     opus_int16 samplesOut1_tmp[ 2 ][ MAX_FS_KHZ * MAX_FRAME_LENGTH_MS + 2 + MAX_DECODER_DELAY ];
89     opus_int16 samplesOut2_tmp[ MAX_API_FS_KHZ * MAX_FRAME_LENGTH_MS ];
90     opus_int32 MS_pred_Q13[ 2 ] = { 0 };
91     opus_int16 *resample_out_ptr;
92     silk_decoder *psDec = ( silk_decoder * )decState;
93     silk_decoder_state *channel_state = psDec->channel_state;
94     opus_int has_side;
95
96     /**********************************/
97     /* Test if first frame in payload */
98     /**********************************/
99     if( newPacketFlag ) {
100         for( n = 0; n < decControl->nChannelsInternal; n++ ) {
101             channel_state[ n ].nFramesDecoded = 0;  /* Used to count frames in packet */
102         }
103     }
104
105     /* If Mono -> Stereo transition in bitstream: init state of second channel */
106     if( decControl->nChannelsInternal > psDec->nChannelsInternal ) {
107         ret += silk_init_decoder( &channel_state[ 1 ] );
108     }
109
110     if( channel_state[ 0 ].nFramesDecoded == 0 ) {
111         for( n = 0; n < decControl->nChannelsInternal; n++ ) {
112             opus_int fs_kHz_dec;
113             if( decControl->payloadSize_ms == 0 ) {
114                 /* Assuming packet loss, use 10 ms */
115                 channel_state[ n ].nFramesPerPacket = 1;
116                 channel_state[ n ].nb_subfr = 2;
117             } else if( decControl->payloadSize_ms == 10 ) {
118                 channel_state[ n ].nFramesPerPacket = 1;
119                 channel_state[ n ].nb_subfr = 2;
120             } else if( decControl->payloadSize_ms == 20 ) {
121                 channel_state[ n ].nFramesPerPacket = 1;
122                 channel_state[ n ].nb_subfr = 4;
123             } else if( decControl->payloadSize_ms == 40 ) {
124                 channel_state[ n ].nFramesPerPacket = 2;
125                 channel_state[ n ].nb_subfr = 4;
126             } else if( decControl->payloadSize_ms == 60 ) {
127                 channel_state[ n ].nFramesPerPacket = 3;
128                 channel_state[ n ].nb_subfr = 4;
129             } else {
130                 silk_assert( 0 );
131                 return SILK_DEC_INVALID_FRAME_SIZE;
132             }
133             fs_kHz_dec = ( decControl->internalSampleRate >> 10 ) + 1;
134             if( fs_kHz_dec != 8 && fs_kHz_dec != 12 && fs_kHz_dec != 16 ) {
135                 silk_assert( 0 );
136                 return SILK_DEC_INVALID_SAMPLING_FREQUENCY;
137             }
138             ret += silk_decoder_set_fs( &channel_state[ n ], fs_kHz_dec, decControl->API_sampleRate );
139         }
140     }
141
142     delay = channel_state[ 0 ].delay;
143
144     if( decControl->nChannelsAPI == 2 && decControl->nChannelsInternal == 2 && ( psDec->nChannelsAPI == 1 || psDec->nChannelsInternal == 1 ) ) {
145         silk_memset( psDec->sStereo.pred_prev_Q13, 0, sizeof( psDec->sStereo.pred_prev_Q13 ) );
146         silk_memset( psDec->sStereo.sSide, 0, sizeof( psDec->sStereo.sSide ) );
147         silk_memcpy( &channel_state[ 1 ].resampler_state, &channel_state[ 0 ].resampler_state, sizeof( silk_resampler_state_struct ) );
148         silk_memcpy( &channel_state[ 1 ].delayBuf, &channel_state[ 0 ].delayBuf, sizeof(channel_state[ 0 ].delayBuf));
149     }
150     psDec->nChannelsAPI      = decControl->nChannelsAPI;
151     psDec->nChannelsInternal = decControl->nChannelsInternal;
152
153     if( decControl->API_sampleRate > MAX_API_FS_KHZ * 1000 || decControl->API_sampleRate < 8000 ) {
154         ret = SILK_DEC_INVALID_SAMPLING_FREQUENCY;
155         return( ret );
156     }
157
158     if( lostFlag != FLAG_PACKET_LOST && channel_state[ 0 ].nFramesDecoded == 0 ) {
159         /* First decoder call for this payload */
160         /* Decode VAD flags and LBRR flag */
161         for( n = 0; n < decControl->nChannelsInternal; n++ ) {
162             for( i = 0; i < channel_state[ n ].nFramesPerPacket; i++ ) {
163                 channel_state[ n ].VAD_flags[ i ] = ec_dec_bit_logp(psRangeDec, 1);
164             }
165             channel_state[ n ].LBRR_flag = ec_dec_bit_logp(psRangeDec, 1);
166         }
167         /* Decode LBRR flags */
168         for( n = 0; n < decControl->nChannelsInternal; n++ ) {
169             silk_memset( channel_state[ n ].LBRR_flags, 0, sizeof( channel_state[ n ].LBRR_flags ) );
170             if( channel_state[ n ].LBRR_flag ) {
171                 if( channel_state[ n ].nFramesPerPacket == 1 ) {
172                     channel_state[ n ].LBRR_flags[ 0 ] = 1;
173                 } else {
174                     LBRR_symbol = ec_dec_icdf( psRangeDec, silk_LBRR_flags_iCDF_ptr[ channel_state[ n ].nFramesPerPacket - 2 ], 8 ) + 1;
175                     for( i = 0; i < channel_state[ n ].nFramesPerPacket; i++ ) {
176                         channel_state[ n ].LBRR_flags[ i ] = silk_RSHIFT( LBRR_symbol, i ) & 1;
177                     }
178                 }
179             }
180         }
181
182         if( lostFlag == FLAG_DECODE_NORMAL ) {
183             /* Regular decoding: skip all LBRR data */
184             for( i = 0; i < channel_state[ 0 ].nFramesPerPacket; i++ ) {
185                 for( n = 0; n < decControl->nChannelsInternal; n++ ) {
186                     if( channel_state[ n ].LBRR_flags[ i ] ) {
187                         opus_int pulses[ MAX_FRAME_LENGTH ];
188                         opus_int condCoding;
189
190                         if( decControl->nChannelsInternal == 2 && n == 0 ) {
191                             silk_stereo_decode_pred( psRangeDec, MS_pred_Q13 );
192                             if( channel_state[ 1 ].LBRR_flags[ i ] == 0 ) {
193                                 silk_stereo_decode_mid_only( psRangeDec, &decode_only_middle );
194                             }
195                         }
196                         /* Use conditional coding if previous frame available */
197                         if( i > 0 && channel_state[ n ].LBRR_flags[ i - 1 ] ) {
198                             condCoding = CODE_CONDITIONALLY;
199                         } else {
200                             condCoding = CODE_INDEPENDENTLY;
201                         }
202                         silk_decode_indices( &channel_state[ n ], psRangeDec, i, 1, condCoding );
203                         silk_decode_pulses( psRangeDec, pulses, channel_state[ n ].indices.signalType,
204                             channel_state[ n ].indices.quantOffsetType, channel_state[ n ].frame_length );
205                     }
206                 }
207             }
208         }
209     }
210
211     /* Get MS predictor index */
212     if( decControl->nChannelsInternal == 2 ) {
213         if(   lostFlag == FLAG_DECODE_NORMAL ||
214             ( lostFlag == FLAG_DECODE_LBRR && channel_state[ 0 ].LBRR_flags[ channel_state[ 0 ].nFramesDecoded ] == 1 ) )
215         {
216             silk_stereo_decode_pred( psRangeDec, MS_pred_Q13 );
217             /* For LBRR data, decode mid-only flag only if side-channel's LBRR flag is false */
218             if( ( lostFlag == FLAG_DECODE_NORMAL && channel_state[ 1 ].VAD_flags[ channel_state[ 0 ].nFramesDecoded ] == 0 ) ||
219                 ( lostFlag == FLAG_DECODE_LBRR && channel_state[ 1 ].LBRR_flags[ channel_state[ 0 ].nFramesDecoded ] == 0 ) )
220             {
221                 silk_stereo_decode_mid_only( psRangeDec, &decode_only_middle );
222             } else {
223                 decode_only_middle = 0;
224             }
225         } else {
226             for( n = 0; n < 2; n++ ) {
227                 MS_pred_Q13[ n ] = psDec->sStereo.pred_prev_Q13[ n ];
228             }
229         }
230     }
231
232     /* Reset side channel decoder prediction memory for first frame with side coding */
233     if( decControl->nChannelsInternal == 2 && decode_only_middle == 0 && psDec->prev_decode_only_middle == 1 ) {
234         silk_memset( psDec->channel_state[ 1 ].outBuf, 0, sizeof(psDec->channel_state[ 1 ].outBuf) );
235         silk_memset( psDec->channel_state[ 1 ].sLPC_Q14_buf, 0, sizeof(psDec->channel_state[ 1 ].sLPC_Q14_buf) );
236         psDec->channel_state[ 1 ].lagPrev        = 100;
237         psDec->channel_state[ 1 ].LastGainIndex  = 10;
238         psDec->channel_state[ 1 ].prevSignalType = TYPE_NO_VOICE_ACTIVITY;
239         psDec->channel_state[ 1 ].first_frame_after_reset = 1;
240     }
241
242     if( lostFlag == FLAG_DECODE_NORMAL ) {
243         has_side = !decode_only_middle;
244     } else {
245         has_side = !psDec->prev_decode_only_middle
246               || (decControl->nChannelsInternal == 2 && lostFlag == FLAG_DECODE_LBRR && channel_state[1].LBRR_flags[ channel_state[1].nFramesDecoded ] == 1 );
247     }
248     /* Call decoder for one frame */
249     for( n = 0; n < decControl->nChannelsInternal; n++ ) {
250         if( n == 0 || has_side ) {
251             opus_int FrameIndex;
252             opus_int condCoding;
253
254             FrameIndex = channel_state[ 0 ].nFramesDecoded - n;
255             /* Use independent coding if no previous frame available */
256             if( FrameIndex <= 0 ) {
257                 condCoding = CODE_INDEPENDENTLY;
258             } else if( lostFlag == FLAG_DECODE_LBRR ) {
259                 condCoding = channel_state[ n ].LBRR_flags[ FrameIndex - 1 ] ? CODE_CONDITIONALLY : CODE_INDEPENDENTLY;
260             } else if( n > 0 && psDec->prev_decode_only_middle ) {
261                 /* If we skipped a side frame in this packet, we don't
262                    need LTP scaling; the LTP state is well-defined. */
263                 condCoding = CODE_INDEPENDENTLY_NO_LTP_SCALING;
264             } else {
265                 condCoding = CODE_CONDITIONALLY;
266             }
267             ret += silk_decode_frame( &channel_state[ n ], psRangeDec, &samplesOut1_tmp[ n ][ 2 + delay ], &nSamplesOutDec, lostFlag, condCoding);
268         } else {
269             silk_memset( &samplesOut1_tmp[ n ][ 2 + delay ], 0, nSamplesOutDec * sizeof( opus_int16 ) );
270         }
271         channel_state[ n ].nFramesDecoded++;
272     }
273
274     if( decControl->nChannelsAPI == 2 && decControl->nChannelsInternal == 2 ) {
275         /* Convert Mid/Side to Left/Right */
276         silk_stereo_MS_to_LR( &psDec->sStereo, &samplesOut1_tmp[ 0 ][ delay ], &samplesOut1_tmp[ 1 ][ delay ], MS_pred_Q13, channel_state[ 0 ].fs_kHz, nSamplesOutDec );
277     } else {
278         /* Buffering */
279         silk_memcpy( &samplesOut1_tmp[ 0 ][ delay ], psDec->sStereo.sMid, 2 * sizeof( opus_int16 ) );
280         silk_memcpy( psDec->sStereo.sMid, &samplesOut1_tmp[ 0 ][ nSamplesOutDec + delay ], 2 * sizeof( opus_int16 ) );
281     }
282
283     /* Number of output samples */
284     *nSamplesOut = silk_DIV32( nSamplesOutDec * decControl->API_sampleRate, silk_SMULBB( channel_state[ 0 ].fs_kHz, 1000 ) );
285
286     /* Set up pointers to temp buffers */
287     if( decControl->nChannelsAPI == 2 ) {
288         resample_out_ptr = samplesOut2_tmp;
289     } else {
290         resample_out_ptr = samplesOut;
291     }
292
293     for( n = 0; n < silk_min( decControl->nChannelsAPI, decControl->nChannelsInternal ); n++ ) {
294
295         silk_memcpy(&samplesOut1_tmp[ n ][ 1 ], &channel_state[ n ].delayBuf[ MAX_DECODER_DELAY - delay ], delay * sizeof(opus_int16));
296         /* Resample decoded signal to API_sampleRate */
297         ret += silk_resampler( &channel_state[ n ].resampler_state, resample_out_ptr, &samplesOut1_tmp[ n ][ 1 ], nSamplesOutDec );
298         silk_memcpy(channel_state[ n ].delayBuf, &samplesOut1_tmp[ n ][ 1 + nSamplesOutDec + delay - MAX_DECODER_DELAY ], MAX_DECODER_DELAY * sizeof(opus_int16));
299
300         /* Interleave if stereo output and stereo stream */
301         if( decControl->nChannelsAPI == 2 && decControl->nChannelsInternal == 2 ) {
302             for( i = 0; i < *nSamplesOut; i++ ) {
303                 samplesOut[ n + 2 * i ] = resample_out_ptr[ i ];
304             }
305         }
306     }
307
308     /* Create two channel output from mono stream */
309     if( decControl->nChannelsAPI == 2 && decControl->nChannelsInternal == 1 ) {
310         for( i = 0; i < *nSamplesOut; i++ ) {
311             samplesOut[ 0 + 2 * i ] = samplesOut[ 1 + 2 * i ] = resample_out_ptr[ i ];
312         }
313     }
314
315     /* Export pitch lag, measured at 48 kHz sampling rate */
316     if( channel_state[ 0 ].prevSignalType == TYPE_VOICED ) {
317         int mult_tab[ 3 ] = { 6, 4, 3 };
318         decControl->prevPitchLag = channel_state[ 0 ].lagPrev * mult_tab[ ( channel_state[ 0 ].fs_kHz - 8 ) >> 2 ];
319     } else {
320         decControl->prevPitchLag = 0;
321     }
322
323     if( lostFlag != FLAG_PACKET_LOST ) {
324        psDec->prev_decode_only_middle = decode_only_middle;
325     }
326     return ret;
327 }
328
329 /* Getting table of contents for a packet */
330 opus_int silk_get_TOC(
331     const opus_uint8                *payload,           /* I    Payload data                                */
332     const opus_int                  nBytesIn,           /* I    Number of input bytes                       */
333     const opus_int                  nFramesPerPayload,  /* I    Number of SILK frames per payload           */
334     silk_TOC_struct                 *Silk_TOC           /* O    Type of content                             */
335 )
336 {
337     opus_int i, flags, ret = SILK_NO_ERROR;
338
339     if( nBytesIn < 1 ) {
340         return -1;
341     }
342     if( nFramesPerPayload < 0 || nFramesPerPayload > 3 ) {
343         return -1;
344     }
345
346     silk_memset( Silk_TOC, 0, sizeof( Silk_TOC ) );
347
348     /* For stereo, extract the flags for the mid channel */
349     flags = silk_RSHIFT( payload[ 0 ], 7 - nFramesPerPayload ) & ( silk_LSHIFT( 1, nFramesPerPayload + 1 ) - 1 );
350
351     Silk_TOC->inbandFECFlag = flags & 1;
352     for( i = nFramesPerPayload - 1; i >= 0 ; i-- ) {
353         flags = silk_RSHIFT( flags, 1 );
354         Silk_TOC->VADFlags[ i ] = flags & 1;
355         Silk_TOC->VADFlag |= flags & 1;
356     }
357
358     return ret;
359 }