dec_API.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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, are permitted provided that the following conditions
  5. 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 Internet Society, IETF or IETF Trust, nor the
  12. names of specific contributors, may be used to endorse or promote
  13. products derived from this software without specific prior written
  14. permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  19. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. POSSIBILITY OF SUCH DAMAGE.
  26. ***********************************************************************/
  27. #ifdef HAVE_CONFIG_H
  28. #include "config.h"
  29. #endif
  30. #include "API.h"
  31. #include "main.h"
  32. #include "stack_alloc.h"
  33. #include "os_support.h"
  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. /* Decoder functions */
  46. /*********************/
  47. opus_int silk_Get_Decoder_Size( /* O Returns error code */
  48. opus_int *decSizeBytes /* O Number of bytes in SILK decoder state */
  49. )
  50. {
  51. opus_int ret = SILK_NO_ERROR;
  52. *decSizeBytes = sizeof( silk_decoder );
  53. return ret;
  54. }
  55. /* Reset decoder state */
  56. opus_int silk_InitDecoder( /* O Returns error code */
  57. void *decState /* I/O State */
  58. )
  59. {
  60. opus_int n, ret = SILK_NO_ERROR;
  61. silk_decoder_state *channel_state = ((silk_decoder *)decState)->channel_state;
  62. for( n = 0; n < DECODER_NUM_CHANNELS; n++ ) {
  63. ret = silk_init_decoder( &channel_state[ n ] );
  64. }
  65. silk_memset(&((silk_decoder *)decState)->sStereo, 0, sizeof(((silk_decoder *)decState)->sStereo));
  66. /* Not strictly needed, but it's cleaner that way */
  67. ((silk_decoder *)decState)->prev_decode_only_middle = 0;
  68. return ret;
  69. }
  70. /* Decode a frame */
  71. opus_int silk_Decode( /* O Returns error code */
  72. void* decState, /* I/O State */
  73. silk_DecControlStruct* decControl, /* I/O Control Structure */
  74. opus_int lostFlag, /* I 0: no loss, 1 loss, 2 decode fec */
  75. opus_int newPacketFlag, /* I Indicates first decoder call for this packet */
  76. ec_dec *psRangeDec, /* I/O Compressor data structure */
  77. opus_int16 *samplesOut, /* O Decoded output speech vector */
  78. opus_int32 *nSamplesOut, /* O Number of samples decoded */
  79. int arch /* I Run-time architecture */
  80. )
  81. {
  82. opus_int i, n, decode_only_middle = 0, ret = SILK_NO_ERROR;
  83. opus_int32 nSamplesOutDec, LBRR_symbol;
  84. opus_int16 *samplesOut1_tmp[ 2 ];
  85. VARDECL( opus_int16, samplesOut1_tmp_storage1 );
  86. VARDECL( opus_int16, samplesOut1_tmp_storage2 );
  87. VARDECL( opus_int16, samplesOut2_tmp );
  88. opus_int32 MS_pred_Q13[ 2 ] = { 0 };
  89. opus_int16 *resample_out_ptr;
  90. silk_decoder *psDec = ( silk_decoder * )decState;
  91. silk_decoder_state *channel_state = psDec->channel_state;
  92. opus_int has_side;
  93. opus_int stereo_to_mono;
  94. int delay_stack_alloc;
  95. SAVE_STACK;
  96. silk_assert( decControl->nChannelsInternal == 1 || decControl->nChannelsInternal == 2 );
  97. /**********************************/
  98. /* Test if first frame in payload */
  99. /**********************************/
  100. if( newPacketFlag ) {
  101. for( n = 0; n < decControl->nChannelsInternal; n++ ) {
  102. channel_state[ n ].nFramesDecoded = 0; /* Used to count frames in packet */
  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. stereo_to_mono = decControl->nChannelsInternal == 1 && psDec->nChannelsInternal == 2 &&
  110. ( decControl->internalSampleRate == 1000*channel_state[ 0 ].fs_kHz );
  111. if( channel_state[ 0 ].nFramesDecoded == 0 ) {
  112. for( n = 0; n < decControl->nChannelsInternal; n++ ) {
  113. opus_int fs_kHz_dec;
  114. if( decControl->payloadSize_ms == 0 ) {
  115. /* Assuming packet loss, use 10 ms */
  116. channel_state[ n ].nFramesPerPacket = 1;
  117. channel_state[ n ].nb_subfr = 2;
  118. } else if( decControl->payloadSize_ms == 10 ) {
  119. channel_state[ n ].nFramesPerPacket = 1;
  120. channel_state[ n ].nb_subfr = 2;
  121. } else if( decControl->payloadSize_ms == 20 ) {
  122. channel_state[ n ].nFramesPerPacket = 1;
  123. channel_state[ n ].nb_subfr = 4;
  124. } else if( decControl->payloadSize_ms == 40 ) {
  125. channel_state[ n ].nFramesPerPacket = 2;
  126. channel_state[ n ].nb_subfr = 4;
  127. } else if( decControl->payloadSize_ms == 60 ) {
  128. channel_state[ n ].nFramesPerPacket = 3;
  129. channel_state[ n ].nb_subfr = 4;
  130. } else {
  131. silk_assert( 0 );
  132. RESTORE_STACK;
  133. return SILK_DEC_INVALID_FRAME_SIZE;
  134. }
  135. fs_kHz_dec = ( decControl->internalSampleRate >> 10 ) + 1;
  136. if( fs_kHz_dec != 8 && fs_kHz_dec != 12 && fs_kHz_dec != 16 ) {
  137. silk_assert( 0 );
  138. RESTORE_STACK;
  139. return SILK_DEC_INVALID_SAMPLING_FREQUENCY;
  140. }
  141. ret += silk_decoder_set_fs( &channel_state[ n ], fs_kHz_dec, decControl->API_sampleRate );
  142. }
  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. }
  149. psDec->nChannelsAPI = decControl->nChannelsAPI;
  150. psDec->nChannelsInternal = decControl->nChannelsInternal;
  151. if( decControl->API_sampleRate > (opus_int32)MAX_API_FS_KHZ * 1000 || decControl->API_sampleRate < 8000 ) {
  152. ret = SILK_DEC_INVALID_SAMPLING_FREQUENCY;
  153. RESTORE_STACK;
  154. return( ret );
  155. }
  156. if( lostFlag != FLAG_PACKET_LOST && channel_state[ 0 ].nFramesDecoded == 0 ) {
  157. /* First decoder call for this payload */
  158. /* Decode VAD flags and LBRR flag */
  159. for( n = 0; n < decControl->nChannelsInternal; n++ ) {
  160. for( i = 0; i < channel_state[ n ].nFramesPerPacket; i++ ) {
  161. channel_state[ n ].VAD_flags[ i ] = ec_dec_bit_logp(psRangeDec, 1);
  162. }
  163. channel_state[ n ].LBRR_flag = ec_dec_bit_logp(psRangeDec, 1);
  164. }
  165. /* Decode LBRR flags */
  166. for( n = 0; n < decControl->nChannelsInternal; n++ ) {
  167. silk_memset( channel_state[ n ].LBRR_flags, 0, sizeof( channel_state[ n ].LBRR_flags ) );
  168. if( channel_state[ n ].LBRR_flag ) {
  169. if( channel_state[ n ].nFramesPerPacket == 1 ) {
  170. channel_state[ n ].LBRR_flags[ 0 ] = 1;
  171. } else {
  172. LBRR_symbol = ec_dec_icdf( psRangeDec, silk_LBRR_flags_iCDF_ptr[ channel_state[ n ].nFramesPerPacket - 2 ], 8 ) + 1;
  173. for( i = 0; i < channel_state[ n ].nFramesPerPacket; i++ ) {
  174. channel_state[ n ].LBRR_flags[ i ] = silk_RSHIFT( LBRR_symbol, i ) & 1;
  175. }
  176. }
  177. }
  178. }
  179. if( lostFlag == FLAG_DECODE_NORMAL ) {
  180. /* Regular decoding: skip all LBRR data */
  181. for( i = 0; i < channel_state[ 0 ].nFramesPerPacket; i++ ) {
  182. for( n = 0; n < decControl->nChannelsInternal; n++ ) {
  183. if( channel_state[ n ].LBRR_flags[ i ] ) {
  184. opus_int16 pulses[ MAX_FRAME_LENGTH ];
  185. opus_int condCoding;
  186. if( decControl->nChannelsInternal == 2 && n == 0 ) {
  187. silk_stereo_decode_pred( psRangeDec, MS_pred_Q13 );
  188. if( channel_state[ 1 ].LBRR_flags[ i ] == 0 ) {
  189. silk_stereo_decode_mid_only( psRangeDec, &decode_only_middle );
  190. }
  191. }
  192. /* Use conditional coding if previous frame available */
  193. if( i > 0 && channel_state[ n ].LBRR_flags[ i - 1 ] ) {
  194. condCoding = CODE_CONDITIONALLY;
  195. } else {
  196. condCoding = CODE_INDEPENDENTLY;
  197. }
  198. silk_decode_indices( &channel_state[ n ], psRangeDec, i, 1, condCoding );
  199. silk_decode_pulses( psRangeDec, pulses, channel_state[ n ].indices.signalType,
  200. channel_state[ n ].indices.quantOffsetType, channel_state[ n ].frame_length );
  201. }
  202. }
  203. }
  204. }
  205. }
  206. /* Get MS predictor index */
  207. if( decControl->nChannelsInternal == 2 ) {
  208. if( lostFlag == FLAG_DECODE_NORMAL ||
  209. ( lostFlag == FLAG_DECODE_LBRR && channel_state[ 0 ].LBRR_flags[ channel_state[ 0 ].nFramesDecoded ] == 1 ) )
  210. {
  211. silk_stereo_decode_pred( psRangeDec, MS_pred_Q13 );
  212. /* For LBRR data, decode mid-only flag only if side-channel's LBRR flag is false */
  213. if( ( lostFlag == FLAG_DECODE_NORMAL && channel_state[ 1 ].VAD_flags[ channel_state[ 0 ].nFramesDecoded ] == 0 ) ||
  214. ( lostFlag == FLAG_DECODE_LBRR && channel_state[ 1 ].LBRR_flags[ channel_state[ 0 ].nFramesDecoded ] == 0 ) )
  215. {
  216. silk_stereo_decode_mid_only( psRangeDec, &decode_only_middle );
  217. } else {
  218. decode_only_middle = 0;
  219. }
  220. } else {
  221. for( n = 0; n < 2; n++ ) {
  222. MS_pred_Q13[ n ] = psDec->sStereo.pred_prev_Q13[ n ];
  223. }
  224. }
  225. }
  226. /* Reset side channel decoder prediction memory for first frame with side coding */
  227. if( decControl->nChannelsInternal == 2 && decode_only_middle == 0 && psDec->prev_decode_only_middle == 1 ) {
  228. silk_memset( psDec->channel_state[ 1 ].outBuf, 0, sizeof(psDec->channel_state[ 1 ].outBuf) );
  229. silk_memset( psDec->channel_state[ 1 ].sLPC_Q14_buf, 0, sizeof(psDec->channel_state[ 1 ].sLPC_Q14_buf) );
  230. psDec->channel_state[ 1 ].lagPrev = 100;
  231. psDec->channel_state[ 1 ].LastGainIndex = 10;
  232. psDec->channel_state[ 1 ].prevSignalType = TYPE_NO_VOICE_ACTIVITY;
  233. psDec->channel_state[ 1 ].first_frame_after_reset = 1;
  234. }
  235. /* Check if the temp buffer fits into the output PCM buffer. If it fits,
  236. we can delay allocating the temp buffer until after the SILK peak stack
  237. usage. We need to use a < and not a <= because of the two extra samples. */
  238. delay_stack_alloc = decControl->internalSampleRate*decControl->nChannelsInternal
  239. < decControl->API_sampleRate*decControl->nChannelsAPI;
  240. ALLOC( samplesOut1_tmp_storage1, delay_stack_alloc ? ALLOC_NONE
  241. : decControl->nChannelsInternal*(channel_state[ 0 ].frame_length + 2 ),
  242. opus_int16 );
  243. if ( delay_stack_alloc )
  244. {
  245. samplesOut1_tmp[ 0 ] = samplesOut;
  246. samplesOut1_tmp[ 1 ] = samplesOut + channel_state[ 0 ].frame_length + 2;
  247. } else {
  248. samplesOut1_tmp[ 0 ] = samplesOut1_tmp_storage1;
  249. samplesOut1_tmp[ 1 ] = samplesOut1_tmp_storage1 + channel_state[ 0 ].frame_length + 2;
  250. }
  251. if( lostFlag == FLAG_DECODE_NORMAL ) {
  252. has_side = !decode_only_middle;
  253. } else {
  254. has_side = !psDec->prev_decode_only_middle
  255. || (decControl->nChannelsInternal == 2 && lostFlag == FLAG_DECODE_LBRR && channel_state[1].LBRR_flags[ channel_state[1].nFramesDecoded ] == 1 );
  256. }
  257. /* Call decoder for one frame */
  258. for( n = 0; n < decControl->nChannelsInternal; n++ ) {
  259. if( n == 0 || has_side ) {
  260. opus_int FrameIndex;
  261. opus_int condCoding;
  262. FrameIndex = channel_state[ 0 ].nFramesDecoded - n;
  263. /* Use independent coding if no previous frame available */
  264. if( FrameIndex <= 0 ) {
  265. condCoding = CODE_INDEPENDENTLY;
  266. } else if( lostFlag == FLAG_DECODE_LBRR ) {
  267. condCoding = channel_state[ n ].LBRR_flags[ FrameIndex - 1 ] ? CODE_CONDITIONALLY : CODE_INDEPENDENTLY;
  268. } else if( n > 0 && psDec->prev_decode_only_middle ) {
  269. /* If we skipped a side frame in this packet, we don't
  270. need LTP scaling; the LTP state is well-defined. */
  271. condCoding = CODE_INDEPENDENTLY_NO_LTP_SCALING;
  272. } else {
  273. condCoding = CODE_CONDITIONALLY;
  274. }
  275. ret += silk_decode_frame( &channel_state[ n ], psRangeDec, &samplesOut1_tmp[ n ][ 2 ], &nSamplesOutDec, lostFlag, condCoding, arch);
  276. } else {
  277. silk_memset( &samplesOut1_tmp[ n ][ 2 ], 0, nSamplesOutDec * sizeof( opus_int16 ) );
  278. }
  279. channel_state[ n ].nFramesDecoded++;
  280. }
  281. if( decControl->nChannelsAPI == 2 && decControl->nChannelsInternal == 2 ) {
  282. /* Convert Mid/Side to Left/Right */
  283. silk_stereo_MS_to_LR( &psDec->sStereo, samplesOut1_tmp[ 0 ], samplesOut1_tmp[ 1 ], MS_pred_Q13, channel_state[ 0 ].fs_kHz, nSamplesOutDec );
  284. } else {
  285. /* Buffering */
  286. silk_memcpy( samplesOut1_tmp[ 0 ], psDec->sStereo.sMid, 2 * sizeof( opus_int16 ) );
  287. silk_memcpy( psDec->sStereo.sMid, &samplesOut1_tmp[ 0 ][ nSamplesOutDec ], 2 * sizeof( opus_int16 ) );
  288. }
  289. /* Number of output samples */
  290. *nSamplesOut = silk_DIV32( nSamplesOutDec * decControl->API_sampleRate, silk_SMULBB( channel_state[ 0 ].fs_kHz, 1000 ) );
  291. /* Set up pointers to temp buffers */
  292. ALLOC( samplesOut2_tmp,
  293. decControl->nChannelsAPI == 2 ? *nSamplesOut : ALLOC_NONE, opus_int16 );
  294. if( decControl->nChannelsAPI == 2 ) {
  295. resample_out_ptr = samplesOut2_tmp;
  296. } else {
  297. resample_out_ptr = samplesOut;
  298. }
  299. ALLOC( samplesOut1_tmp_storage2, delay_stack_alloc
  300. ? decControl->nChannelsInternal*(channel_state[ 0 ].frame_length + 2 )
  301. : ALLOC_NONE,
  302. opus_int16 );
  303. if ( delay_stack_alloc ) {
  304. OPUS_COPY(samplesOut1_tmp_storage2, samplesOut, decControl->nChannelsInternal*(channel_state[ 0 ].frame_length + 2));
  305. samplesOut1_tmp[ 0 ] = samplesOut1_tmp_storage2;
  306. samplesOut1_tmp[ 1 ] = samplesOut1_tmp_storage2 + channel_state[ 0 ].frame_length + 2;
  307. }
  308. for( n = 0; n < silk_min( decControl->nChannelsAPI, decControl->nChannelsInternal ); n++ ) {
  309. /* Resample decoded signal to API_sampleRate */
  310. ret += silk_resampler( &channel_state[ n ].resampler_state, resample_out_ptr, &samplesOut1_tmp[ n ][ 1 ], nSamplesOutDec );
  311. /* Interleave if stereo output and stereo stream */
  312. if( decControl->nChannelsAPI == 2 ) {
  313. for( i = 0; i < *nSamplesOut; i++ ) {
  314. samplesOut[ n + 2 * i ] = resample_out_ptr[ i ];
  315. }
  316. }
  317. }
  318. /* Create two channel output from mono stream */
  319. if( decControl->nChannelsAPI == 2 && decControl->nChannelsInternal == 1 ) {
  320. if ( stereo_to_mono ){
  321. /* Resample right channel for newly collapsed stereo just in case
  322. we weren't doing collapsing when switching to mono */
  323. ret += silk_resampler( &channel_state[ 1 ].resampler_state, resample_out_ptr, &samplesOut1_tmp[ 0 ][ 1 ], nSamplesOutDec );
  324. for( i = 0; i < *nSamplesOut; i++ ) {
  325. samplesOut[ 1 + 2 * i ] = resample_out_ptr[ i ];
  326. }
  327. } else {
  328. for( i = 0; i < *nSamplesOut; i++ ) {
  329. samplesOut[ 1 + 2 * i ] = samplesOut[ 0 + 2 * i ];
  330. }
  331. }
  332. }
  333. /* Export pitch lag, measured at 48 kHz sampling rate */
  334. if( channel_state[ 0 ].prevSignalType == TYPE_VOICED ) {
  335. int mult_tab[ 3 ] = { 6, 4, 3 };
  336. decControl->prevPitchLag = channel_state[ 0 ].lagPrev * mult_tab[ ( channel_state[ 0 ].fs_kHz - 8 ) >> 2 ];
  337. } else {
  338. decControl->prevPitchLag = 0;
  339. }
  340. if( lostFlag == FLAG_PACKET_LOST ) {
  341. /* On packet loss, remove the gain clamping to prevent having the energy "bounce back"
  342. if we lose packets when the energy is going down */
  343. for ( i = 0; i < psDec->nChannelsInternal; i++ )
  344. psDec->channel_state[ i ].LastGainIndex = 10;
  345. } else {
  346. psDec->prev_decode_only_middle = decode_only_middle;
  347. }
  348. RESTORE_STACK;
  349. return ret;
  350. }
  351. #if 0
  352. /* Getting table of contents for a packet */
  353. opus_int silk_get_TOC(
  354. const opus_uint8 *payload, /* I Payload data */
  355. const opus_int nBytesIn, /* I Number of input bytes */
  356. const opus_int nFramesPerPayload, /* I Number of SILK frames per payload */
  357. silk_TOC_struct *Silk_TOC /* O Type of content */
  358. )
  359. {
  360. opus_int i, flags, ret = SILK_NO_ERROR;
  361. if( nBytesIn < 1 ) {
  362. return -1;
  363. }
  364. if( nFramesPerPayload < 0 || nFramesPerPayload > 3 ) {
  365. return -1;
  366. }
  367. silk_memset( Silk_TOC, 0, sizeof( *Silk_TOC ) );
  368. /* For stereo, extract the flags for the mid channel */
  369. flags = silk_RSHIFT( payload[ 0 ], 7 - nFramesPerPayload ) & ( silk_LSHIFT( 1, nFramesPerPayload + 1 ) - 1 );
  370. Silk_TOC->inbandFECFlag = flags & 1;
  371. for( i = nFramesPerPayload - 1; i >= 0 ; i-- ) {
  372. flags = silk_RSHIFT( flags, 1 );
  373. Silk_TOC->VADFlags[ i ] = flags & 1;
  374. Silk_TOC->VADFlag |= flags & 1;
  375. }
  376. return ret;
  377. }
  378. #endif