Encoder.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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 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 <math.h>
  37. #include "SKP_Silk_SDK_API.h"
  38. /* Define codec specific settings */
  39. #define MAX_BYTES_PER_FRAME 250 // Equals peak bitrate of 100 kbps
  40. #define MAX_INPUT_FRAMES 5
  41. #define MAX_LBRR_DELAY 2
  42. #define MAX_FRAME_LENGTH 480
  43. #define FRAME_LENGTH_MS 20
  44. #define MAX_API_FS_KHZ 48
  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[], /* I/O array of */
  50. SKP_int len /* I length */
  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. static void print_usage( char* argv[] ) {
  64. printf( "\nusage: %s in.pcm out.bit [settings]\n", argv[ 0 ] );
  65. printf( "\nin.pcm : Speech input to encoder" );
  66. printf( "\nstream.bit : Bitstream output from encoder" );
  67. printf( "\n settings:" );
  68. printf( "\n-Fs_API <Hz> : API sampling rate in Hz, default: 24000" );
  69. printf( "\n-Fs_maxInternal <Hz> : Maximum internal sampling rate in Hz, default: 24000" );
  70. printf( "\n-packetlength <ms> : Packet interval in ms, default: 20" );
  71. printf( "\n-rate <bps> : Target bitrate; default: 25000" );
  72. printf( "\n-loss <perc> : Uplink loss estimate, in percent (0-100); default: 0" );
  73. printf( "\n-inbandFEC <flag> : Enable inband FEC usage (0/1); default: 0" );
  74. printf( "\n-complexity <comp> : Set complexity, 0: low, 1: medium, 2: high; default: 2" );
  75. printf( "\n-DTX <flag> : Enable DTX (0/1); default: 0" );
  76. printf( "\n-quiet : Print only some basic values" );
  77. printf( "\n");
  78. }
  79. int main( int argc, char* argv[] )
  80. {
  81. size_t counter;
  82. SKP_int32 k, args, totPackets, totActPackets, ret;
  83. SKP_int16 nBytes;
  84. double sumBytes, sumActBytes, avg_rate, act_rate, nrg;
  85. SKP_uint8 payload[ MAX_BYTES_PER_FRAME * MAX_INPUT_FRAMES ];
  86. SKP_int16 in[ FRAME_LENGTH_MS * MAX_API_FS_KHZ * MAX_INPUT_FRAMES ];
  87. char speechInFileName[ 150 ], bitOutFileName[ 150 ];
  88. FILE *bitOutFile, *speechInFile;
  89. SKP_int32 encSizeBytes;
  90. void *psEnc;
  91. #ifdef _SYSTEM_IS_BIG_ENDIAN
  92. SKP_int16 nBytes_LE;
  93. #endif
  94. /* default settings */
  95. SKP_int32 API_fs_Hz = 24000;
  96. SKP_int32 max_internal_fs_Hz = 0;
  97. SKP_int32 targetRate_bps = 25000;
  98. SKP_int32 packetSize_ms = 20;
  99. SKP_int32 frameSizeReadFromFile_ms = 20;
  100. SKP_int32 packetLoss_perc = 0, complexity_mode = 2, smplsSinceLastPacket;
  101. SKP_int32 INBandFEC_enabled = 0, DTX_enabled = 0, quiet = 0;
  102. SKP_SILK_SDK_EncControlStruct encControl; // Struct for input to encoder
  103. if( argc < 3 ) {
  104. print_usage( argv );
  105. exit( 0 );
  106. }
  107. /* get arguments */
  108. args = 1;
  109. strcpy( speechInFileName, argv[ args ] );
  110. args++;
  111. strcpy( bitOutFileName, argv[ args ] );
  112. args++;
  113. while( args < argc ) {
  114. if( SKP_STR_CASEINSENSITIVE_COMPARE( argv[ args ], "-Fs_API" ) == 0 ) {
  115. sscanf( argv[ args + 1 ], "%d", &API_fs_Hz );
  116. args += 2;
  117. } else if( SKP_STR_CASEINSENSITIVE_COMPARE( argv[ args ], "-Fs_maxInternal" ) == 0 ) {
  118. sscanf( argv[ args + 1 ], "%d", &max_internal_fs_Hz );
  119. args += 2;
  120. } else if( SKP_STR_CASEINSENSITIVE_COMPARE( argv[ args ], "-packetlength" ) == 0 ) {
  121. sscanf( argv[ args + 1 ], "%d", &packetSize_ms );
  122. args += 2;
  123. } else if( SKP_STR_CASEINSENSITIVE_COMPARE( argv[ args ], "-rate" ) == 0 ) {
  124. sscanf( argv[ args + 1 ], "%d", &targetRate_bps );
  125. args += 2;
  126. } else if( SKP_STR_CASEINSENSITIVE_COMPARE( argv[ args ], "-loss" ) == 0 ) {
  127. sscanf( argv[ args + 1 ], "%d", &packetLoss_perc );
  128. args += 2;
  129. } else if( SKP_STR_CASEINSENSITIVE_COMPARE( argv[ args ], "-complexity" ) == 0 ) {
  130. sscanf( argv[ args + 1 ], "%d", &complexity_mode );
  131. args += 2;
  132. } else if( SKP_STR_CASEINSENSITIVE_COMPARE( argv[ args ], "-inbandFEC" ) == 0 ) {
  133. sscanf( argv[ args + 1 ], "%d", &INBandFEC_enabled );
  134. args += 2;
  135. } else if( SKP_STR_CASEINSENSITIVE_COMPARE( argv[ args ], "-DTX") == 0 ) {
  136. sscanf( argv[ args + 1 ], "%d", &DTX_enabled );
  137. args += 2;
  138. } else if( SKP_STR_CASEINSENSITIVE_COMPARE( argv[ args ], "-quiet" ) == 0 ) {
  139. quiet = 1;
  140. args++;
  141. } else {
  142. printf( "Error: unrecognized setting: %s\n\n", argv[ args ] );
  143. print_usage( argv );
  144. exit( 0 );
  145. }
  146. }
  147. /* If no max internal is specified, set to minimum of API fs and 24 kHz */
  148. if( max_internal_fs_Hz == 0 ) {
  149. max_internal_fs_Hz = 24000;
  150. if( API_fs_Hz < max_internal_fs_Hz ) {
  151. max_internal_fs_Hz = API_fs_Hz;
  152. }
  153. }
  154. /* Print options */
  155. if( !quiet ) {
  156. printf("******************* Silk Encoder v %s ****************\n", SKP_Silk_SDK_get_version());
  157. printf("******************* Compiled for %d bit cpu ********* \n", (int)sizeof(void*) * 8 );
  158. printf( "Input: %s\n", speechInFileName );
  159. printf( "Output: %s\n", bitOutFileName );
  160. printf( "API sampling rate: %d Hz\n", API_fs_Hz );
  161. printf( "Maximum internal sampling rate: %d Hz\n", max_internal_fs_Hz );
  162. printf( "Packet interval: %d ms\n", packetSize_ms );
  163. printf( "Inband FEC used: %d\n", INBandFEC_enabled );
  164. printf( "DTX used: %d\n", DTX_enabled );
  165. printf( "Complexity: %d\n", complexity_mode );
  166. printf( "Target bitrate: %d bps\n", targetRate_bps );
  167. }
  168. /* Open files */
  169. speechInFile = fopen( speechInFileName, "rb" );
  170. if( speechInFile == NULL ) {
  171. printf( "Error: could not open input file %s\n", speechInFileName );
  172. exit( 0 );
  173. }
  174. bitOutFile = fopen( bitOutFileName, "wb" );
  175. if( bitOutFile == NULL ) {
  176. printf( "Error: could not open output file %s\n", bitOutFileName );
  177. exit( 0 );
  178. }
  179. /* Add Silk header to stream */
  180. {
  181. static const char Silk_header[] = "#!SILK_V3";
  182. fwrite( Silk_header, sizeof( char ), strlen( Silk_header ), bitOutFile );
  183. }
  184. /* Create Encoder */
  185. ret = SKP_Silk_SDK_Get_Encoder_Size( &encSizeBytes );
  186. if( ret ) {
  187. printf( "\nSKP_Silk_create_encoder returned %d", ret );
  188. }
  189. psEnc = malloc( encSizeBytes );
  190. /* Reset Encoder */
  191. ret = SKP_Silk_SDK_InitEncoder( psEnc, &encControl );
  192. if( ret ) {
  193. printf( "\nSKP_Silk_reset_encoder returned %d", ret );
  194. }
  195. /* Set Encoder parameters */
  196. encControl.API_sampleRate = API_fs_Hz;
  197. encControl.maxInternalSampleRate = max_internal_fs_Hz;
  198. encControl.packetSize = ( packetSize_ms * API_fs_Hz ) / 1000;
  199. encControl.packetLossPercentage = packetLoss_perc;
  200. encControl.useInBandFEC = INBandFEC_enabled;
  201. encControl.useDTX = DTX_enabled;
  202. encControl.complexity = complexity_mode;
  203. encControl.bitRate = ( targetRate_bps > 0 ? targetRate_bps : 0 );
  204. if( API_fs_Hz > MAX_API_FS_KHZ * 1000 || API_fs_Hz < 0 ) {
  205. printf( "\nError: API sampling rate = %d out of range, valid range 8000 - 48000 \n \n", API_fs_Hz );
  206. exit( 0 );
  207. }
  208. totPackets = 0;
  209. totActPackets = 0;
  210. smplsSinceLastPacket = 0;
  211. sumBytes = 0.0;
  212. sumActBytes = 0.0;
  213. while( 1 ) {
  214. /* Read input from file */
  215. counter = fread( in, sizeof( SKP_int16 ), ( frameSizeReadFromFile_ms * API_fs_Hz ) / 1000, speechInFile );
  216. #ifdef _SYSTEM_IS_BIG_ENDIAN
  217. swap_endian( in, counter );
  218. #endif
  219. if( (SKP_int)counter < ( ( frameSizeReadFromFile_ms * API_fs_Hz ) / 1000 ) ) {
  220. break;
  221. }
  222. /* max payload size */
  223. nBytes = MAX_BYTES_PER_FRAME * MAX_INPUT_FRAMES;
  224. /* Silk Encoder */
  225. ret = SKP_Silk_SDK_Encode( psEnc, &encControl, in, (SKP_int16)counter, payload, &nBytes );
  226. if( ret ) {
  227. printf( "\nSKP_Silk_Encode returned %d", ret );
  228. break;
  229. }
  230. /* Get packet size */
  231. packetSize_ms = ( SKP_int )( ( 1000 * ( SKP_int32 )encControl.packetSize ) / encControl.API_sampleRate );
  232. smplsSinceLastPacket += ( SKP_int )counter;
  233. if( ( ( 1000 * smplsSinceLastPacket ) / API_fs_Hz ) == packetSize_ms ) {
  234. /* Sends a dummy zero size packet in case of DTX period */
  235. /* to make it work with the decoder test program. */
  236. /* In practice should be handled by RTP sequence numbers */
  237. totPackets++;
  238. sumBytes += nBytes;
  239. nrg = 0.0;
  240. for( k = 0; k < ( SKP_int )counter; k++ ) {
  241. nrg += in[ k ] * (double)in[ k ];
  242. }
  243. if( ( nrg / ( SKP_int )counter ) > 1e3 ) {
  244. sumActBytes += nBytes;
  245. totActPackets++;
  246. }
  247. /* Write payload size */
  248. #ifdef _SYSTEM_IS_BIG_ENDIAN
  249. nBytes_LE = nBytes;
  250. swap_endian( &nBytes_LE, 1 );
  251. fwrite( &nBytes_LE, sizeof( SKP_int16 ), 1, bitOutFile );
  252. #else
  253. fwrite( &nBytes, sizeof( SKP_int16 ), 1, bitOutFile );
  254. #endif
  255. /* Write payload */
  256. fwrite( payload, sizeof( SKP_uint8 ), nBytes, bitOutFile );
  257. if( !quiet ) {
  258. fprintf( stderr, "\rPackets encoded: %d", totPackets );
  259. }
  260. smplsSinceLastPacket = 0;
  261. }
  262. }
  263. /* Write dummy because it can not end with 0 bytes */
  264. nBytes = -1;
  265. /* Write payload size */
  266. fwrite( &nBytes, sizeof( SKP_int16 ), 1, bitOutFile );
  267. /* Free Encoder */
  268. free( psEnc );
  269. fclose( speechInFile );
  270. fclose( bitOutFile );
  271. avg_rate = 8.0 / packetSize_ms * sumBytes / totPackets;
  272. act_rate = 8.0 / packetSize_ms * sumActBytes / totActPackets;
  273. if( !quiet ) {
  274. printf( "\nAverage bitrate: %.3f kbps", avg_rate );
  275. printf( "\nActive bitrate: %.3f kbps", act_rate );
  276. printf( "\n\n" );
  277. } else {
  278. /* print average and active bitrates */
  279. printf( "%.3f %.3f \n", avg_rate, act_rate );
  280. }
  281. return 0;
  282. }