iLBC_test.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /******************************************************************
  2. iLBC Speech Coder ANSI-C Source Code
  3. iLBC_test.c
  4. Copyright (C) The Internet Society (2004).
  5. All Rights Reserved.
  6. ******************************************************************/
  7. #include <math.h>
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include "iLBC_define.h"
  12. #include "iLBC_encode.h"
  13. #include "iLBC_decode.h"
  14. /* Runtime statistics */
  15. #include <time.h>
  16. #define ILBCNOOFWORDS_MAX (NO_OF_BYTES_30MS/2)
  17. /*----------------------------------------------------------------*
  18. * Encoder interface function
  19. *---------------------------------------------------------------*/
  20. short encode( /* (o) Number of bytes encoded */
  21. iLBC_Enc_Inst_t *iLBCenc_inst,
  22. /* (i/o) Encoder instance */
  23. short *encoded_data, /* (o) The encoded bytes */
  24. short *data /* (i) The signal block to encode*/
  25. ){
  26. float block[BLOCKL_MAX];
  27. int k;
  28. /* convert signal to float */
  29. for (k=0; k<iLBCenc_inst->blockl; k++)
  30. block[k] = (float)data[k];
  31. /* do the actual encoding */
  32. iLBC_encode((unsigned char *)encoded_data, block, iLBCenc_inst);
  33. return (iLBCenc_inst->no_of_bytes);
  34. }
  35. /*----------------------------------------------------------------*
  36. * Decoder interface function
  37. *---------------------------------------------------------------*/
  38. short decode( /* (o) Number of decoded samples */
  39. iLBC_Dec_Inst_t *iLBCdec_inst, /* (i/o) Decoder instance */
  40. short *decoded_data, /* (o) Decoded signal block*/
  41. short *encoded_data, /* (i) Encoded bytes */
  42. short mode /* (i) 0=PL, 1=Normal */
  43. ){
  44. int k;
  45. float decblock[BLOCKL_MAX], dtmp;
  46. /* check if mode is valid */
  47. if (mode<0 || mode>1) {
  48. printf("\nERROR - Wrong mode - 0, 1 allowed\n"); exit(3);}
  49. /* do actual decoding of block */
  50. iLBC_decode(decblock, (unsigned char *)encoded_data,
  51. iLBCdec_inst, mode);
  52. /* convert to short */
  53. for (k=0; k<iLBCdec_inst->blockl; k++){
  54. dtmp=decblock[k];
  55. if (dtmp<MIN_SAMPLE)
  56. dtmp=MIN_SAMPLE;
  57. else if (dtmp>MAX_SAMPLE)
  58. dtmp=MAX_SAMPLE;
  59. decoded_data[k] = (short) dtmp;
  60. }
  61. return (iLBCdec_inst->blockl);
  62. }
  63. /*---------------------------------------------------------------*
  64. * Main program to test iLBC encoding and decoding
  65. *
  66. * Usage:
  67. * exefile_name.exe <infile> <bytefile> <outfile> <channel>
  68. *
  69. * <infile> : Input file, speech for encoder (16-bit pcm file)
  70. * <bytefile> : Bit stream output from the encoder
  71. * <outfile> : Output file, decoded speech (16-bit pcm file)
  72. * <channel> : Bit error file, optional (16-bit)
  73. * 1 - Packet received correctly
  74. * 0 - Packet Lost
  75. *
  76. *--------------------------------------------------------------*/
  77. int main(int argc, char* argv[])
  78. {
  79. /* Runtime statistics */
  80. float starttime;
  81. float runtime;
  82. float outtime;
  83. FILE *ifileid,*efileid,*ofileid, *cfileid;
  84. short data[BLOCKL_MAX];
  85. short encoded_data[ILBCNOOFWORDS_MAX], decoded_data[BLOCKL_MAX];
  86. int len;
  87. short pli, mode;
  88. int blockcount = 0;
  89. int packetlosscount = 0;
  90. /* Create structs */
  91. iLBC_Enc_Inst_t Enc_Inst;
  92. iLBC_Dec_Inst_t Dec_Inst;
  93. /* get arguments and open files */
  94. if ((argc!=5) && (argc!=6)) {
  95. fprintf(stderr,
  96. "\n*-----------------------------------------------*\n");
  97. fprintf(stderr,
  98. " %s <20,30> input encoded decoded (channel)\n\n",
  99. argv[0]);
  100. fprintf(stderr,
  101. " mode : Frame size for the encoding/decoding\n");
  102. fprintf(stderr,
  103. " 20 - 20 ms\n");
  104. fprintf(stderr,
  105. " 30 - 30 ms\n");
  106. fprintf(stderr,
  107. " input : Speech for encoder (16-bit pcm file)\n");
  108. fprintf(stderr,
  109. " encoded : Encoded bit stream\n");
  110. fprintf(stderr,
  111. " decoded : Decoded speech (16-bit pcm file)\n");
  112. fprintf(stderr,
  113. " channel : Packet loss pattern, optional (16-bit)\n");
  114. fprintf(stderr,
  115. " 1 - Packet received correctly\n");
  116. fprintf(stderr,
  117. " 0 - Packet Lost\n");
  118. fprintf(stderr,
  119. "*-----------------------------------------------*\n\n");
  120. exit(1);
  121. }
  122. mode=atoi(argv[1]);
  123. if (mode != 20 && mode != 30) {
  124. fprintf(stderr,"Wrong mode %s, must be 20, or 30\n",
  125. argv[1]);
  126. exit(2);
  127. }
  128. if ( (ifileid=fopen(argv[2],"rb")) == NULL) {
  129. fprintf(stderr,"Cannot open input file %s\n", argv[2]);
  130. exit(2);}
  131. if ( (efileid=fopen(argv[3],"wb")) == NULL) {
  132. fprintf(stderr, "Cannot open encoded file %s\n",
  133. argv[3]); exit(1);}
  134. if ( (ofileid=fopen(argv[4],"wb")) == NULL) {
  135. fprintf(stderr, "Cannot open decoded file %s\n",
  136. argv[4]); exit(1);}
  137. if (argc==6) {
  138. if( (cfileid=fopen(argv[5],"rb")) == NULL) {
  139. fprintf(stderr, "Cannot open channel file %s\n",
  140. argv[5]);
  141. exit(1);
  142. }
  143. } else {
  144. cfileid=NULL;
  145. }
  146. /* print info */
  147. fprintf(stderr, "\n");
  148. fprintf(stderr,
  149. "*---------------------------------------------------*\n");
  150. fprintf(stderr,
  151. "* *\n");
  152. fprintf(stderr,
  153. "* iLBC test program *\n");
  154. fprintf(stderr,
  155. "* *\n");
  156. fprintf(stderr,
  157. "* *\n");
  158. fprintf(stderr,
  159. "*---------------------------------------------------*\n");
  160. fprintf(stderr,"\nMode : %2d ms\n", mode);
  161. fprintf(stderr,"Input file : %s\n", argv[2]);
  162. fprintf(stderr,"Encoded file : %s\n", argv[3]);
  163. fprintf(stderr,"Output file : %s\n", argv[4]);
  164. if (argc==6) {
  165. fprintf(stderr,"Channel file : %s\n", argv[5]);
  166. }
  167. fprintf(stderr,"\n");
  168. /* Initialization */
  169. initEncode(&Enc_Inst, mode);
  170. initDecode(&Dec_Inst, mode, 1);
  171. /* Runtime statistics */
  172. starttime=clock()/(float)CLOCKS_PER_SEC;
  173. /* loop over input blocks */
  174. while (fread(data,sizeof(short),Enc_Inst.blockl,ifileid)==
  175. (size_t)Enc_Inst.blockl) {
  176. blockcount++;
  177. /* encoding */
  178. fprintf(stderr, "--- Encoding block %i --- ",blockcount);
  179. len=encode(&Enc_Inst, encoded_data, data);
  180. fprintf(stderr, "\r");
  181. /* write byte file */
  182. fwrite(encoded_data, sizeof(unsigned char), len, efileid);
  183. /* get channel data if provided */
  184. if (argc==6) {
  185. if (fread(&pli, sizeof(short), 1, cfileid)) {
  186. if ((pli!=0)&&(pli!=1)) {
  187. fprintf(stderr, "Error in channel file\n");
  188. exit(0);
  189. }
  190. if (pli==0) {
  191. /* Packet loss -> remove info from frame */
  192. memset(encoded_data, 0,
  193. sizeof(short)*ILBCNOOFWORDS_MAX);
  194. packetlosscount++;
  195. }
  196. } else {
  197. fprintf(stderr, "Error. Channel file too short\n");
  198. exit(0);
  199. }
  200. } else {
  201. pli=1;
  202. }
  203. /* decoding */
  204. fprintf(stderr, "--- Decoding block %i --- ",blockcount);
  205. len=decode(&Dec_Inst, decoded_data, encoded_data, pli);
  206. fprintf(stderr, "\r");
  207. /* write output file */
  208. fwrite(decoded_data,sizeof(short),len,ofileid);
  209. }
  210. /* Runtime statistics */
  211. runtime = (float)(clock()/(float)CLOCKS_PER_SEC-starttime);
  212. outtime = (float)((float)blockcount*(float)mode/1000.0);
  213. printf("\n\nLength of speech file: %.1f s\n", outtime);
  214. printf("Packet loss : %.1f%%\n",
  215. 100.0*(float)packetlosscount/(float)blockcount);
  216. printf("Time to run iLBC :");
  217. printf(" %.1f s (%.1f %% of realtime)\n\n", runtime,
  218. (100*runtime/outtime));
  219. /* close files */
  220. fclose(ifileid); fclose(efileid); fclose(ofileid);
  221. if (argc==6) {
  222. fclose(cfileid);
  223. }
  224. return(0);
  225. }