Encoder.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /***********************************************************************
  2. Copyright (c) 2006-2012, 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 encoder 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. /* Define codec specific settings */
  38. #define MAX_BYTES_PER_FRAME 250 // Equals peak bitrate of 100 kbps
  39. #define MAX_INPUT_FRAMES 5
  40. #define FRAME_LENGTH_MS 20
  41. #define MAX_API_FS_KHZ 48
  42. #ifdef _SYSTEM_IS_BIG_ENDIAN
  43. /* Function to convert a little endian int16 to a */
  44. /* big endian int16 or vica verca */
  45. void swap_endian(
  46. SKP_int16 vec[], /* I/O array of */
  47. SKP_int len /* I length */
  48. )
  49. {
  50. SKP_int i;
  51. SKP_int16 tmp;
  52. SKP_uint8 *p1, *p2;
  53. for( i = 0; i < len; i++ ){
  54. tmp = vec[ i ];
  55. p1 = (SKP_uint8 *)&vec[ i ]; p2 = (SKP_uint8 *)&tmp;
  56. p1[ 0 ] = p2[ 1 ]; p1[ 1 ] = p2[ 0 ];
  57. }
  58. }
  59. #endif
  60. #if (defined(_WIN32) || defined(_WINCE))
  61. #include <windows.h> /* timer */
  62. #else // Linux or Mac
  63. #include <sys/time.h>
  64. #endif
  65. #ifdef _WIN32
  66. unsigned long GetHighResolutionTime() /* O: time in usec*/
  67. {
  68. /* Returns a time counter in microsec */
  69. /* the resolution is platform dependent */
  70. /* but is typically 1.62 us resolution */
  71. LARGE_INTEGER lpPerformanceCount;
  72. LARGE_INTEGER lpFrequency;
  73. QueryPerformanceCounter(&lpPerformanceCount);
  74. QueryPerformanceFrequency(&lpFrequency);
  75. return (unsigned long)((1000000*(lpPerformanceCount.QuadPart)) / lpFrequency.QuadPart);
  76. }
  77. #else // Linux or Mac
  78. unsigned long GetHighResolutionTime() /* O: time in usec*/
  79. {
  80. struct timeval tv;
  81. gettimeofday(&tv, 0);
  82. return((tv.tv_sec*1000000)+(tv.tv_usec));
  83. }
  84. #endif // _WIN32
  85. static void print_usage( char* argv[] ) {
  86. printf( "\nusage: %s in.pcm out.bit [settings]\n", argv[ 0 ] );
  87. printf( "\nin.pcm : Speech input to encoder" );
  88. printf( "\nout.bit : Bitstream output from encoder" );
  89. printf( "\n settings:" );
  90. printf( "\n-Fs_API <Hz> : API sampling rate in Hz, default: 24000" );
  91. printf( "\n-Fs_maxInternal <Hz> : Maximum internal sampling rate in Hz, default: 24000" );
  92. printf( "\n-packetlength <ms> : Packet interval in ms, default: 20" );
  93. printf( "\n-rate <bps> : Target bitrate; default: 25000" );
  94. printf( "\n-loss <perc> : Uplink loss estimate, in percent (0-100); default: 0" );
  95. printf( "\n-inbandFEC <flag> : Enable inband FEC usage (0/1); default: 0" );
  96. printf( "\n-complexity <comp> : Set complexity, 0: low, 1: medium, 2: high; default: 2" );
  97. printf( "\n-DTX <flag> : Enable DTX (0/1); default: 0" );
  98. printf( "\n-quiet : Print only some basic values" );
  99. printf( "\n");
  100. }
  101. int main( int argc, char* argv[] )
  102. {
  103. unsigned long tottime, starttime;
  104. double filetime;
  105. size_t counter;
  106. SKP_int32 k, args, totPackets, totActPackets, ret;
  107. SKP_int16 nBytes;
  108. double sumBytes, sumActBytes, avg_rate, act_rate, nrg;
  109. SKP_uint8 payload[ MAX_BYTES_PER_FRAME * MAX_INPUT_FRAMES ];
  110. SKP_int16 in[ FRAME_LENGTH_MS * MAX_API_FS_KHZ * MAX_INPUT_FRAMES ];
  111. char speechInFileName[ 150 ], bitOutFileName[ 150 ];
  112. FILE *bitOutFile, *speechInFile;
  113. SKP_int32 encSizeBytes;
  114. void *psEnc;
  115. #ifdef _SYSTEM_IS_BIG_ENDIAN
  116. SKP_int16 nBytes_LE;
  117. #endif
  118. /* default settings */
  119. SKP_int32 API_fs_Hz = 24000;
  120. SKP_int32 max_internal_fs_Hz = 0;
  121. SKP_int32 targetRate_bps = 25000;
  122. SKP_int32 smplsSinceLastPacket, packetSize_ms = 20;
  123. SKP_int32 frameSizeReadFromFile_ms = 20;
  124. SKP_int32 packetLoss_perc = 0;
  125. #if LOW_COMPLEXITY_ONLY
  126. SKP_int32 complexity_mode = 0;
  127. #else
  128. SKP_int32 complexity_mode = 2;
  129. #endif
  130. SKP_int32 DTX_enabled = 0, INBandFEC_enabled = 0, quiet = 0;
  131. SKP_SILK_SDK_EncControlStruct encControl; // Struct for input to encoder
  132. SKP_SILK_SDK_EncControlStruct encStatus; // Struct for status of encoder
  133. if( argc < 3 ) {
  134. print_usage( argv );
  135. exit( 0 );
  136. }
  137. /* get arguments */
  138. args = 1;
  139. strcpy( speechInFileName, argv[ args ] );
  140. args++;
  141. strcpy( bitOutFileName, argv[ args ] );
  142. args++;
  143. while( args < argc ) {
  144. if( SKP_STR_CASEINSENSITIVE_COMPARE( argv[ args ], "-Fs_API" ) == 0 ) {
  145. sscanf( argv[ args + 1 ], "%d", &API_fs_Hz );
  146. args += 2;
  147. } else if( SKP_STR_CASEINSENSITIVE_COMPARE( argv[ args ], "-Fs_maxInternal" ) == 0 ) {
  148. sscanf( argv[ args + 1 ], "%d", &max_internal_fs_Hz );
  149. args += 2;
  150. } else if( SKP_STR_CASEINSENSITIVE_COMPARE( argv[ args ], "-packetlength" ) == 0 ) {
  151. sscanf( argv[ args + 1 ], "%d", &packetSize_ms );
  152. args += 2;
  153. } else if( SKP_STR_CASEINSENSITIVE_COMPARE( argv[ args ], "-rate" ) == 0 ) {
  154. sscanf( argv[ args + 1 ], "%d", &targetRate_bps );
  155. args += 2;
  156. } else if( SKP_STR_CASEINSENSITIVE_COMPARE( argv[ args ], "-loss" ) == 0 ) {
  157. sscanf( argv[ args + 1 ], "%d", &packetLoss_perc );
  158. args += 2;
  159. } else if( SKP_STR_CASEINSENSITIVE_COMPARE( argv[ args ], "-complexity" ) == 0 ) {
  160. sscanf( argv[ args + 1 ], "%d", &complexity_mode );
  161. args += 2;
  162. } else if( SKP_STR_CASEINSENSITIVE_COMPARE( argv[ args ], "-inbandFEC" ) == 0 ) {
  163. sscanf( argv[ args + 1 ], "%d", &INBandFEC_enabled );
  164. args += 2;
  165. } else if( SKP_STR_CASEINSENSITIVE_COMPARE( argv[ args ], "-DTX") == 0 ) {
  166. sscanf( argv[ args + 1 ], "%d", &DTX_enabled );
  167. args += 2;
  168. } else if( SKP_STR_CASEINSENSITIVE_COMPARE( argv[ args ], "-quiet" ) == 0 ) {
  169. quiet = 1;
  170. args++;
  171. } else {
  172. printf( "Error: unrecognized setting: %s\n\n", argv[ args ] );
  173. print_usage( argv );
  174. exit( 0 );
  175. }
  176. }
  177. /* If no max internal is specified, set to minimum of API fs and 24 kHz */
  178. if( max_internal_fs_Hz == 0 ) {
  179. max_internal_fs_Hz = 24000;
  180. if( API_fs_Hz < max_internal_fs_Hz ) {
  181. max_internal_fs_Hz = API_fs_Hz;
  182. }
  183. }
  184. /* Print options */
  185. if( !quiet ) {
  186. printf("********** Silk Encoder (Fixed Point) v %s ********************\n", SKP_Silk_SDK_get_version());
  187. printf("********** Compiled for %d bit cpu ******************************* \n", (int)sizeof(void*) * 8 );
  188. printf( "Input: %s\n", speechInFileName );
  189. printf( "Output: %s\n", bitOutFileName );
  190. printf( "API sampling rate: %d Hz\n", API_fs_Hz );
  191. printf( "Maximum internal sampling rate: %d Hz\n", max_internal_fs_Hz );
  192. printf( "Packet interval: %d ms\n", packetSize_ms );
  193. printf( "Inband FEC used: %d\n", INBandFEC_enabled );
  194. printf( "DTX used: %d\n", DTX_enabled );
  195. printf( "Complexity: %d\n", complexity_mode );
  196. printf( "Target bitrate: %d bps\n", targetRate_bps );
  197. }
  198. /* Open files */
  199. speechInFile = fopen( speechInFileName, "rb" );
  200. if( speechInFile == NULL ) {
  201. printf( "Error: could not open input file %s\n", speechInFileName );
  202. exit( 0 );
  203. }
  204. bitOutFile = fopen( bitOutFileName, "wb" );
  205. if( bitOutFile == NULL ) {
  206. printf( "Error: could not open output file %s\n", bitOutFileName );
  207. exit( 0 );
  208. }
  209. /* Add Silk header to stream */
  210. {
  211. static const char Silk_header[] = "#!SILK_V3";
  212. fwrite( Silk_header, sizeof( char ), strlen( Silk_header ), bitOutFile );
  213. }
  214. /* Create Encoder */
  215. ret = SKP_Silk_SDK_Get_Encoder_Size( &encSizeBytes );
  216. if( ret ) {
  217. printf( "\nError: SKP_Silk_create_encoder returned %d\n", ret );
  218. exit( 0 );
  219. }
  220. psEnc = malloc( encSizeBytes );
  221. /* Reset Encoder */
  222. ret = SKP_Silk_SDK_InitEncoder( psEnc, &encStatus );
  223. if( ret ) {
  224. printf( "\nError: SKP_Silk_reset_encoder returned %d\n", ret );
  225. exit( 0 );
  226. }
  227. /* Set Encoder parameters */
  228. encControl.API_sampleRate = API_fs_Hz;
  229. encControl.maxInternalSampleRate = max_internal_fs_Hz;
  230. encControl.packetSize = ( packetSize_ms * API_fs_Hz ) / 1000;
  231. encControl.packetLossPercentage = packetLoss_perc;
  232. encControl.useInBandFEC = INBandFEC_enabled;
  233. encControl.useDTX = DTX_enabled;
  234. encControl.complexity = complexity_mode;
  235. encControl.bitRate = ( targetRate_bps > 0 ? targetRate_bps : 0 );
  236. if( API_fs_Hz > MAX_API_FS_KHZ * 1000 || API_fs_Hz < 0 ) {
  237. printf( "\nError: API sampling rate = %d out of range, valid range 8000 - 48000 \n \n", API_fs_Hz );
  238. exit( 0 );
  239. }
  240. tottime = 0;
  241. totPackets = 0;
  242. totActPackets = 0;
  243. smplsSinceLastPacket = 0;
  244. sumBytes = 0.0;
  245. sumActBytes = 0.0;
  246. smplsSinceLastPacket = 0;
  247. while( 1 ) {
  248. /* Read input from file */
  249. counter = fread( in, sizeof( SKP_int16 ), ( frameSizeReadFromFile_ms * API_fs_Hz ) / 1000, speechInFile );
  250. #ifdef _SYSTEM_IS_BIG_ENDIAN
  251. swap_endian( in, counter );
  252. #endif
  253. if( ( SKP_int )counter < ( ( frameSizeReadFromFile_ms * API_fs_Hz ) / 1000 ) ) {
  254. break;
  255. }
  256. /* max payload size */
  257. nBytes = MAX_BYTES_PER_FRAME * MAX_INPUT_FRAMES;
  258. starttime = GetHighResolutionTime();
  259. /* Silk Encoder */
  260. ret = SKP_Silk_SDK_Encode( psEnc, &encControl, in, (SKP_int16)counter, payload, &nBytes );
  261. if( ret ) {
  262. printf( "\nSKP_Silk_Encode returned %d", ret );
  263. }
  264. tottime += GetHighResolutionTime() - starttime;
  265. /* Get packet size */
  266. packetSize_ms = ( SKP_int )( ( 1000 * ( SKP_int32 )encControl.packetSize ) / encControl.API_sampleRate );
  267. smplsSinceLastPacket += ( SKP_int )counter;
  268. if( ( ( 1000 * smplsSinceLastPacket ) / API_fs_Hz ) == packetSize_ms ) {
  269. /* Sends a dummy zero size packet in case of DTX period */
  270. /* to make it work with the decoder test program. */
  271. /* In practice should be handled by RTP sequence numbers */
  272. totPackets++;
  273. sumBytes += nBytes;
  274. nrg = 0.0;
  275. for( k = 0; k < ( SKP_int )counter; k++ ) {
  276. nrg += in[ k ] * (double)in[ k ];
  277. }
  278. if( ( nrg / ( SKP_int )counter ) > 1e3 ) {
  279. sumActBytes += nBytes;
  280. totActPackets++;
  281. }
  282. /* Write payload size */
  283. #ifdef _SYSTEM_IS_BIG_ENDIAN
  284. nBytes_LE = nBytes;
  285. swap_endian( &nBytes_LE, 1 );
  286. fwrite( &nBytes_LE, sizeof( SKP_int16 ), 1, bitOutFile );
  287. #else
  288. fwrite( &nBytes, sizeof( SKP_int16 ), 1, bitOutFile );
  289. #endif
  290. /* Write payload */
  291. fwrite( payload, sizeof( SKP_uint8 ), nBytes, bitOutFile );
  292. smplsSinceLastPacket = 0;
  293. if( !quiet ) {
  294. fprintf( stderr, "\rPackets encoded: %d", totPackets );
  295. }
  296. }
  297. }
  298. /* Write dummy because it can not end with 0 bytes */
  299. nBytes = -1;
  300. /* Write payload size */
  301. fwrite( &nBytes, sizeof( SKP_int16 ), 1, bitOutFile );
  302. /* Free Encoder */
  303. free( psEnc );
  304. fclose( speechInFile );
  305. fclose( bitOutFile );
  306. filetime = totPackets * 1e-3 * packetSize_ms;
  307. avg_rate = 8.0 / packetSize_ms * sumBytes / totPackets;
  308. act_rate = 8.0 / packetSize_ms * sumActBytes / totActPackets;
  309. if( !quiet ) {
  310. printf( "\nFile length: %.3f s", filetime );
  311. printf( "\nTime for encoding: %.3f s (%.3f%% of realtime)", 1e-6 * tottime, 1e-4 * tottime / filetime );
  312. printf( "\nAverage bitrate: %.3f kbps", avg_rate );
  313. printf( "\nActive bitrate: %.3f kbps", act_rate );
  314. printf( "\n\n" );
  315. } else {
  316. /* print time and % of realtime */
  317. printf("%.3f %.3f %d ", 1e-6 * tottime, 1e-4 * tottime / filetime, totPackets );
  318. /* print average and active bitrates */
  319. printf( "%.3f %.3f \n", avg_rate, act_rate );
  320. }
  321. return 0;
  322. }