Decoder.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. /* Silk decoder test program */
  29. /*****************************/
  30. #ifdef _WIN32
  31. #define _CRT_SECURE_NO_DEPRECATE 1
  32. #endif
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include "SKP_Silk_SDK_API.h"
  37. #include "SKP_Silk_SigProc_FIX.h"
  38. /* Define codec specific settings should be moved to h file */
  39. #define MAX_BYTES_PER_FRAME 1024
  40. #define MAX_INPUT_FRAMES 5
  41. #define MAX_FRAME_LENGTH 480
  42. #define FRAME_LENGTH_MS 20
  43. #define MAX_API_FS_KHZ 48
  44. #define MAX_LBRR_DELAY 2
  45. #ifdef _SYSTEM_IS_BIG_ENDIAN
  46. /* Function to convert a little endian int16 to a */
  47. /* big endian int16 or vica verca */
  48. void swap_endian(
  49. SKP_int16 vec[],
  50. SKP_int len
  51. )
  52. {
  53. SKP_int i;
  54. SKP_int16 tmp;
  55. SKP_uint8 *p1, *p2;
  56. for( i = 0; i < len; i++ ){
  57. tmp = vec[ i ];
  58. p1 = (SKP_uint8 *)&vec[ i ]; p2 = (SKP_uint8 *)&tmp;
  59. p1[ 0 ] = p2[ 1 ]; p1[ 1 ] = p2[ 0 ];
  60. }
  61. }
  62. #endif
  63. /* Seed for the random number generator, which is used for simulating packet loss */
  64. static SKP_int32 rand_seed = 1;
  65. static void print_usage(char* argv[]) {
  66. printf( "\nusage: %s in.bit out.pcm [settings]\n", argv[ 0 ] );
  67. printf( "\nstream.bit : Bitstream input to decoder" );
  68. printf( "\nout.pcm : Speech output from decoder" );
  69. printf( "\n settings:" );
  70. printf( "\n-Fs_API <Hz> : Sampling rate of output signal in Hz; default: 24000" );
  71. printf( "\n-loss <perc> : Simulated packet loss percentage (0-100); default: 0" );
  72. printf( "\n" );
  73. }
  74. int main( int argc, char* argv[] )
  75. {
  76. size_t counter;
  77. SKP_int32 args, totPackets, i, k;
  78. SKP_int16 ret, len, tot_len;
  79. SKP_int16 nBytes;
  80. SKP_uint8 payload[ MAX_BYTES_PER_FRAME * MAX_INPUT_FRAMES * ( MAX_LBRR_DELAY + 1 ) ];
  81. SKP_uint8 *payloadEnd = NULL, *payloadToDec = NULL;
  82. SKP_uint8 FECpayload[ MAX_BYTES_PER_FRAME * MAX_INPUT_FRAMES ], *payloadPtr;
  83. SKP_int16 nBytesFEC;
  84. SKP_int16 nBytesPerPacket[ MAX_LBRR_DELAY + 1 ], totBytes;
  85. SKP_int16 out[ ( ( FRAME_LENGTH_MS * MAX_API_FS_KHZ ) << 1 ) * MAX_INPUT_FRAMES ], *outPtr;
  86. char speechOutFileName[ 150 ], bitInFileName[ 150 ];
  87. FILE *bitInFile, *speechOutFile;
  88. SKP_int32 API_Fs_Hz = 0;
  89. SKP_int32 decSizeBytes;
  90. void *psDec;
  91. float loss_prob;
  92. SKP_int32 frames, lost, quiet;
  93. SKP_SILK_SDK_DecControlStruct DecControl;
  94. if( argc < 3 ) {
  95. print_usage( argv );
  96. exit( 0 );
  97. }
  98. /* default settings */
  99. quiet = 0;
  100. loss_prob = 0.0f;
  101. /* get arguments */
  102. args = 1;
  103. strcpy( bitInFileName, argv[ args ] );
  104. args++;
  105. strcpy( speechOutFileName, argv[ args ] );
  106. args++;
  107. while( args < argc ) {
  108. if( SKP_STR_CASEINSENSITIVE_COMPARE( argv[ args ], "-loss" ) == 0 ) {
  109. sscanf( argv[ args + 1 ], "%f", &loss_prob );
  110. args += 2;
  111. } else if( SKP_STR_CASEINSENSITIVE_COMPARE( argv[ args ], "-Fs_API" ) == 0 ) {
  112. sscanf( argv[ args + 1 ], "%d", &API_Fs_Hz );
  113. args += 2;
  114. } else if( SKP_STR_CASEINSENSITIVE_COMPARE( argv[ args ], "-quiet" ) == 0 ) {
  115. quiet = 1;
  116. args++;
  117. } else {
  118. printf( "Error: unrecognized setting: %s\n\n", argv[ args ] );
  119. print_usage( argv );
  120. exit( 0 );
  121. }
  122. }
  123. if( !quiet ) {
  124. printf("******************* Silk Decoder v %s ****************\n", SKP_Silk_SDK_get_version());
  125. printf("******************* Compiled for %d bit cpu ********* \n", (int)sizeof(void*) * 8 );
  126. printf( "Input: %s\n", bitInFileName );
  127. printf( "Output: %s\n", speechOutFileName );
  128. }
  129. /* Open files */
  130. bitInFile = fopen( bitInFileName, "rb" );
  131. if( bitInFile == NULL ) {
  132. printf( "Error: could not open input file %s\n", bitInFileName );
  133. exit( 0 );
  134. }
  135. /* Check Silk header */
  136. {
  137. char header_buf[ 50 ];
  138. counter = fread( header_buf, sizeof( char ), strlen( "#!SILK_V3" ), bitInFile );
  139. header_buf[ strlen( "#!SILK_V3" ) ] = ( char )0; /* Terminate with a null character */
  140. if( strcmp( header_buf, "#!SILK_V3" ) != 0 ) {
  141. /* Non-equal strings */
  142. printf( "Error: Wrong Header %s\n", header_buf );
  143. exit( 0 );
  144. }
  145. }
  146. speechOutFile = fopen( speechOutFileName, "wb" );
  147. if( speechOutFile == NULL ) {
  148. printf( "Error: could not open output file %s\n", speechOutFileName );
  149. exit( 0 );
  150. }
  151. /* Set the samplingrate that is requested for the output */
  152. if( API_Fs_Hz == 0 ) {
  153. DecControl.API_sampleRate = 24000;
  154. } else {
  155. DecControl.API_sampleRate = API_Fs_Hz;
  156. }
  157. /* Initialize to one frame per packet, for proper concealment before first packet arrives */
  158. DecControl.framesPerPacket = 1;
  159. /* Create decoder */
  160. ret = SKP_Silk_SDK_Get_Decoder_Size( &decSizeBytes );
  161. if( ret ) {
  162. printf( "\nSKP_Silk_SDK_Get_Decoder_Size returned %d", ret );
  163. }
  164. psDec = malloc( decSizeBytes );
  165. /* Reset decoder */
  166. ret = SKP_Silk_SDK_InitDecoder( psDec );
  167. if( ret ) {
  168. printf( "\nSKP_Silk_InitDecoder returned %d", ret );
  169. }
  170. totPackets = 0;
  171. payloadEnd = payload;
  172. /* Simulate the jitter buffer holding MAX_FEC_DELAY packets */
  173. for( i = 0; i < MAX_LBRR_DELAY; i++ ) {
  174. /* Read payload size */
  175. counter = fread( &nBytes, sizeof( SKP_int16 ), 1, bitInFile );
  176. #ifdef _SYSTEM_IS_BIG_ENDIAN
  177. swap_endian( &nBytes, 1 );
  178. #endif
  179. /* Read payload */
  180. counter = fread( payloadEnd, sizeof( SKP_uint8 ), nBytes, bitInFile );
  181. if( ( SKP_int16 )counter < nBytes ) {
  182. break;
  183. }
  184. nBytesPerPacket[ i ] = nBytes;
  185. payloadEnd += nBytes;
  186. }
  187. while( 1 ) {
  188. /* Read payload size */
  189. counter = fread( &nBytes, sizeof( SKP_int16 ), 1, bitInFile );
  190. #ifdef _SYSTEM_IS_BIG_ENDIAN
  191. swap_endian( &nBytes, 1 );
  192. #endif
  193. if( nBytes < 0 || counter < 1 ) {
  194. break;
  195. }
  196. /* Read payload */
  197. counter = fread( payloadEnd, sizeof( SKP_uint8 ), nBytes, bitInFile );
  198. if( ( SKP_int16 )counter < nBytes ) {
  199. break;
  200. }
  201. /* Simulate losses */
  202. rand_seed = SKP_RAND( rand_seed );
  203. if( ( ( ( float )( ( rand_seed >> 16 ) + ( 1 << 15 ) ) ) / 65535.0f >= ( loss_prob / 100.0f ) ) && ( counter > 0 ) ) {
  204. nBytesPerPacket[ MAX_LBRR_DELAY ] = nBytes;
  205. payloadEnd += nBytes;
  206. } else {
  207. nBytesPerPacket[ MAX_LBRR_DELAY ] = 0;
  208. }
  209. if( nBytesPerPacket[ 0 ] == 0 ) {
  210. /* Indicate lost packet */
  211. lost = 1;
  212. /* Packet loss. Search after FEC in next packets. Should be done in the jitter buffer */
  213. payloadPtr = payload;
  214. for( i = 0; i < MAX_LBRR_DELAY; i++ ) {
  215. if( nBytesPerPacket[ i + 1 ] > 0 ) {
  216. SKP_Silk_SDK_search_for_LBRR( payloadPtr, nBytesPerPacket[ i + 1 ], i + 1, FECpayload, &nBytesFEC );
  217. if( nBytesFEC > 0 ) {
  218. payloadToDec = FECpayload;
  219. nBytes = nBytesFEC;
  220. lost = 0;
  221. break;
  222. }
  223. }
  224. payloadPtr += nBytesPerPacket[ i + 1 ];
  225. }
  226. } else {
  227. lost = 0;
  228. nBytes = nBytesPerPacket[ 0 ];
  229. payloadToDec = payload;
  230. }
  231. /* Silk decoder */
  232. outPtr = out;
  233. tot_len = 0;
  234. if( lost == 0 ) {
  235. /* No Loss: Decode all frames in the packet */
  236. frames = 0;
  237. do {
  238. /* Decode 20 ms */
  239. ret = SKP_Silk_SDK_Decode( psDec, &DecControl, 0, payloadToDec, nBytes, outPtr, &len );
  240. if( ret ) {
  241. printf( "\nSKP_Silk_SDK_Decode returned %d", ret );
  242. }
  243. frames++;
  244. outPtr += len;
  245. tot_len += len;
  246. if( frames > MAX_INPUT_FRAMES ) {
  247. /* Hack for corrupt stream that could generate too many frames */
  248. outPtr = out;
  249. tot_len = 0;
  250. frames = 0;
  251. }
  252. /* Until last 20 ms frame of packet has been decoded */
  253. } while( DecControl.moreInternalDecoderFrames );
  254. } else {
  255. /* Loss: Decode enough frames to cover one packet duration */
  256. for( i = 0; i < DecControl.framesPerPacket; i++ ) {
  257. /* Generate 20 ms */
  258. ret = SKP_Silk_SDK_Decode( psDec, &DecControl, 1, payloadToDec, nBytes, outPtr, &len );
  259. if( ret ) {
  260. printf( "\nSKP_Silk_Decode returned %d", ret );
  261. }
  262. outPtr += len;
  263. tot_len += len;
  264. }
  265. }
  266. totPackets++;
  267. /* Write output to file */
  268. #ifdef _SYSTEM_IS_BIG_ENDIAN
  269. swap_endian( out, tot_len );
  270. #endif
  271. fwrite( out, sizeof( SKP_int16 ), tot_len, speechOutFile );
  272. /* Update buffer */
  273. totBytes = 0;
  274. for( i = 0; i < MAX_LBRR_DELAY; i++ ) {
  275. totBytes += nBytesPerPacket[ i + 1 ];
  276. }
  277. SKP_memmove( payload, &payload[ nBytesPerPacket[ 0 ] ], totBytes * sizeof( SKP_uint8 ) );
  278. payloadEnd -= nBytesPerPacket[ 0 ];
  279. SKP_memmove( nBytesPerPacket, &nBytesPerPacket[ 1 ], MAX_LBRR_DELAY * sizeof( SKP_int16 ) );
  280. if( !quiet ) {
  281. fprintf( stderr, "\rPackets decoded: %d", totPackets );
  282. }
  283. }
  284. /* Empty the recieve buffer */
  285. for( k = 0; k < MAX_LBRR_DELAY; k++ ) {
  286. if( nBytesPerPacket[ 0 ] == 0 ) {
  287. /* Indicate lost packet */
  288. lost = 1;
  289. /* Packet loss. Search after FEC in next packets. Should be done in the jitter buffer */
  290. payloadPtr = payload;
  291. for( i = 0; i < MAX_LBRR_DELAY; i++ ) {
  292. if( nBytesPerPacket[ i + 1 ] > 0 ) {
  293. SKP_Silk_SDK_search_for_LBRR( payloadPtr, nBytesPerPacket[ i + 1 ], i + 1, FECpayload, &nBytesFEC );
  294. if( nBytesFEC > 0 ) {
  295. payloadToDec = FECpayload;
  296. nBytes = nBytesFEC;
  297. lost = 0;
  298. break;
  299. }
  300. }
  301. payloadPtr += nBytesPerPacket[ i + 1 ];
  302. }
  303. } else {
  304. lost = 0;
  305. nBytes = nBytesPerPacket[ 0 ];
  306. payloadToDec = payload;
  307. }
  308. /* Silk decoder */
  309. outPtr = out;
  310. tot_len = 0;
  311. if( lost == 0 ) {
  312. /* No loss: Decode all frames in the packet */
  313. frames = 0;
  314. do {
  315. /* Decode 20 ms */
  316. ret = SKP_Silk_SDK_Decode( psDec, &DecControl, 0, payloadToDec, nBytes, outPtr, &len );
  317. if( ret ) {
  318. printf( "\nSKP_Silk_SDK_Decode returned %d", ret );
  319. }
  320. frames++;
  321. outPtr += len;
  322. tot_len += len;
  323. if( frames > MAX_INPUT_FRAMES ) {
  324. /* Hack for corrupt stream that could generate too many frames */
  325. outPtr = out;
  326. tot_len = 0;
  327. frames = 0;
  328. }
  329. /* Until last 20 ms frame of packet has been decoded */
  330. } while( DecControl.moreInternalDecoderFrames );
  331. } else {
  332. /* Loss: Decode enough frames to cover one packet duration */
  333. /* Generate 20 ms */
  334. for( i = 0; i < DecControl.framesPerPacket; i++ ) {
  335. ret = SKP_Silk_SDK_Decode( psDec, &DecControl, 1, payloadToDec, nBytes, outPtr, &len );
  336. if( ret ) {
  337. printf( "\nSKP_Silk_Decode returned %d", ret );
  338. }
  339. outPtr += len;
  340. tot_len += len;
  341. }
  342. }
  343. totPackets++;
  344. /* Write output to file */
  345. #ifdef _SYSTEM_IS_BIG_ENDIAN
  346. swap_endian( out, tot_len );
  347. #endif
  348. fwrite( out, sizeof( SKP_int16 ), tot_len, speechOutFile );
  349. /* Update Buffer */
  350. totBytes = 0;
  351. for( i = 0; i < MAX_LBRR_DELAY; i++ ) {
  352. totBytes += nBytesPerPacket[ i + 1 ];
  353. }
  354. SKP_memmove( payload, &payload[ nBytesPerPacket[ 0 ] ], totBytes * sizeof( SKP_uint8 ) );
  355. payloadEnd -= nBytesPerPacket[ 0 ];
  356. SKP_memmove( nBytesPerPacket, &nBytesPerPacket[ 1 ], MAX_LBRR_DELAY * sizeof( SKP_int16 ) );
  357. if( !quiet ) {
  358. fprintf( stderr, "\rPackets decoded: %d", totPackets );
  359. }
  360. }
  361. if( !quiet ) {
  362. printf( "\nDecoding Finished \n" );
  363. }
  364. /* Free decoder */
  365. free( psDec );
  366. /* Close files */
  367. fclose( speechOutFile );
  368. fclose( bitInFile );
  369. return 0;
  370. }