PageRenderTime 58ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/pjsip_android/apps/pjsip/project/third_party/webrtc/modules/audio_coding/codecs/iSAC/main/test/ReleaseTest-API/ReleaseTest-API.cc

http://csipsimple.googlecode.com/
C++ | 1050 lines | 806 code | 112 blank | 132 comment | 192 complexity | 9e14d79c1b7630e9312694263424a9af MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0, GPL-2.0, BSD-3-Clause, LGPL-2.1
  1. /*
  2. * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. // ReleaseTest-API.cpp : Defines the entry point for the console application.
  11. //
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <time.h>
  16. #include <ctype.h>
  17. #include <iostream>
  18. /* include API */
  19. #include "isac.h"
  20. #include "utility.h"
  21. /* Defines */
  22. #define SEED_FILE "randseed.txt" /* Used when running decoder on garbage data */
  23. #define MAX_FRAMESAMPLES 960 /* max number of samples per frame
  24. (= 60 ms frame & 16 kHz) or
  25. (= 30 ms frame & 32 kHz) */
  26. #define FRAMESAMPLES_10ms 160 /* number of samples per 10ms frame */
  27. #define SWBFRAMESAMPLES_10ms 320
  28. //#define FS 16000 /* sampling frequency (Hz) */
  29. #ifdef WIN32
  30. #define CLOCKS_PER_SEC 1000 /* Runtime statistics */
  31. #endif
  32. using namespace std;
  33. int main(int argc, char* argv[])
  34. {
  35. char inname[100], outname[100], bottleneck_file[100], vadfile[100];
  36. FILE *inp, *outp, *f_bn=NULL, *vadp, *bandwidthp;
  37. int framecnt, endfile;
  38. int i, errtype, VADusage = 0, packetLossPercent = 0;
  39. WebRtc_Word16 CodingMode;
  40. WebRtc_Word32 bottleneck;
  41. WebRtc_Word16 framesize = 30; /* ms */
  42. int cur_framesmpls, err;
  43. /* Runtime statistics */
  44. double starttime, runtime, length_file;
  45. WebRtc_Word16 stream_len = 0;
  46. WebRtc_Word16 declen, lostFrame = 0, declenTC = 0;
  47. WebRtc_Word16 shortdata[SWBFRAMESAMPLES_10ms];
  48. WebRtc_Word16 vaddata[SWBFRAMESAMPLES_10ms*3];
  49. WebRtc_Word16 decoded[MAX_FRAMESAMPLES << 1];
  50. WebRtc_Word16 decodedTC[MAX_FRAMESAMPLES << 1];
  51. WebRtc_UWord16 streamdata[500];
  52. WebRtc_Word16 speechType[1];
  53. WebRtc_Word16 rateBPS = 0;
  54. WebRtc_Word16 fixedFL = 0;
  55. WebRtc_Word16 payloadSize = 0;
  56. WebRtc_Word32 payloadRate = 0;
  57. int setControlBWE = 0;
  58. short FL, testNum;
  59. char version_number[20];
  60. FILE *plFile;
  61. WebRtc_Word32 sendBN;
  62. #ifdef _DEBUG
  63. FILE *fy;
  64. double kbps;
  65. #endif /* _DEBUG */
  66. int totalbits =0;
  67. int totalsmpls =0;
  68. /* If use GNS file */
  69. FILE *fp_gns = NULL;
  70. char gns_file[100];
  71. short maxStreamLen30 = 0;
  72. short maxStreamLen60 = 0;
  73. short sampFreqKHz = 32;
  74. short samplesIn10Ms;
  75. short useAssign = 0;
  76. //FILE logFile;
  77. bool doTransCoding = false;
  78. WebRtc_Word32 rateTransCoding = 0;
  79. WebRtc_UWord16 streamDataTransCoding[600];
  80. WebRtc_Word16 streamLenTransCoding = 0;
  81. FILE* transCodingFile = NULL;
  82. FILE* transcodingBitstream = NULL;
  83. WebRtc_UWord32 numTransCodingBytes = 0;
  84. /* only one structure used for ISAC encoder */
  85. ISACStruct* ISAC_main_inst;
  86. ISACStruct* decoderTransCoding;
  87. BottleNeckModel BN_data;
  88. #ifdef _DEBUG
  89. fy = fopen("bit_rate.dat", "w");
  90. fclose(fy);
  91. fy = fopen("bytes_frames.dat", "w");
  92. fclose(fy);
  93. #endif /* _DEBUG */
  94. /* Handling wrong input arguments in the command line */
  95. if((argc<3) || (argc>17)) {
  96. printf("\n\nWrong number of arguments or flag values.\n\n");
  97. printf("\n");
  98. WebRtcIsac_version(version_number);
  99. printf("iSAC-swb version %s \n\n", version_number);
  100. printf("Usage:\n\n");
  101. printf("./kenny.exe [-I] bottleneck_value infile outfile \n\n");
  102. printf("with:\n");
  103. printf("[-FS num] : sampling frequency in kHz, valid values are 16 & 32,\n");
  104. printf(" with 16 as default.\n");
  105. printf("[-I] : if -I option is specified, the coder will use\n");
  106. printf(" an instantaneous Bottleneck value. If not, it\n");
  107. printf(" will be an adaptive Bottleneck value.\n\n");
  108. printf("[-assign] : Use Assign API.\n");
  109. printf("[-B num] : the value of the bottleneck provided either\n");
  110. printf(" as a fixed value in bits/sec (e.g. 25000) or\n");
  111. printf(" read from a file (e.g. bottleneck.txt)\n\n");
  112. printf("[-INITRATE num] : Set a new value for initial rate. Note! Only used in \n");
  113. printf(" adaptive mode.\n\n");
  114. printf("[-FL num] : Set (initial) frame length in msec. Valid length are \n");
  115. printf(" 30 and 60 msec.\n\n");
  116. printf("[-FIXED_FL] : Frame length will be fixed to initial value.\n\n");
  117. printf("[-MAX num] : Set the limit for the payload size of iSAC in bytes. \n");
  118. printf(" Minimum 100 maximum 400.\n\n");
  119. printf("[-MAXRATE num] : Set the maxrate for iSAC in bits per second. \n");
  120. printf(" Minimum 32000, maximum 53400.\n\n");
  121. printf("[-F num] : if -F option is specified, the test function\n");
  122. printf(" will run the iSAC API fault scenario specified by the\n");
  123. printf(" supplied number.\n");
  124. printf(" F 1 - Call encoder prior to init encoder call\n");
  125. printf(" F 2 - Call decoder prior to init decoder call\n");
  126. printf(" F 3 - Call decoder prior to encoder call\n");
  127. printf(" F 4 - Call decoder with a too short coded sequence\n");
  128. printf(" F 5 - Call decoder with a too long coded sequence\n");
  129. printf(" F 6 - Call decoder with random bit stream\n");
  130. printf(" F 7 - Call init encoder/decoder at random during a call\n");
  131. printf(" F 8 - Call encoder/decoder without having allocated memory \n");
  132. printf(" for encoder/decoder instance\n");
  133. printf(" F 9 - Call decodeB without calling decodeA\n");
  134. printf(" F 10 - Call decodeB with garbage data\n");
  135. printf("[-PL num] : if -PL option is specified \n");
  136. printf("[-T rate file] : test trans-coding with target bottleneck 'rate' bits/sec\n");
  137. printf(" the output file is written to 'file'\n");
  138. printf("[-LOOP num] : number of times to repeat coding the input file for stress testing\n");
  139. //printf("[-CE num] : Test of APIs used by Conference Engine.\n");
  140. //printf(" CE 1 - getNewBitstream, getBWE \n");
  141. //printf(" (CE 2 - RESERVED for transcoding)\n");
  142. //printf(" CE 3 - getSendBWE, setSendBWE. \n\n");
  143. //printf("-L filename : write the logging info into file (appending)\n");
  144. printf("infile : Normal speech input file\n\n");
  145. printf("outfile : Speech output file\n\n");
  146. exit(0);
  147. }
  148. /* Print version number */
  149. printf("-------------------------------------------------\n");
  150. WebRtcIsac_version(version_number);
  151. printf("iSAC version %s \n\n", version_number);
  152. /* Loop over all command line arguments */
  153. CodingMode = 0;
  154. testNum = 0;
  155. useAssign = 0;
  156. //logFile = NULL;
  157. char transCodingFileName[500];
  158. WebRtc_Word16 totFileLoop = 0;
  159. WebRtc_Word16 numFileLoop = 0;
  160. for (i = 1; i < argc-2;i++)
  161. {
  162. if(!strcmp("-LOOP", argv[i]))
  163. {
  164. i++;
  165. totFileLoop = (WebRtc_Word16)atol(argv[i]);
  166. if(totFileLoop <= 0)
  167. {
  168. fprintf(stderr, "Invalid number of runs for the given input file, %d.", totFileLoop);
  169. exit(0);
  170. }
  171. }
  172. if(!strcmp("-T", argv[i]))
  173. {
  174. doTransCoding = true;
  175. i++;
  176. rateTransCoding = atoi(argv[i]);
  177. i++;
  178. strcpy(transCodingFileName, argv[i]);
  179. }
  180. /*Should we use assign API*/
  181. if(!strcmp("-assign", argv[i]))
  182. {
  183. useAssign = 1;
  184. }
  185. /* Set Sampling Rate */
  186. if(!strcmp("-FS", argv[i]))
  187. {
  188. i++;
  189. sampFreqKHz = atoi(argv[i]);
  190. }
  191. /* Instantaneous mode */
  192. if(!strcmp ("-I", argv[i]))
  193. {
  194. printf("Instantaneous BottleNeck\n");
  195. CodingMode = 1;
  196. }
  197. /* Set (initial) bottleneck value */
  198. if(!strcmp ("-INITRATE", argv[i])) {
  199. rateBPS = atoi(argv[i + 1]);
  200. setControlBWE = 1;
  201. if((rateBPS < 10000) || (rateBPS > 32000))
  202. {
  203. printf("\n%d is not a initial rate. Valid values are in the range 10000 to 32000.\n", rateBPS);
  204. exit(0);
  205. }
  206. printf("New initial rate: %d\n", rateBPS);
  207. i++;
  208. }
  209. /* Set (initial) framelength */
  210. if(!strcmp ("-FL", argv[i])) {
  211. framesize = atoi(argv[i + 1]);
  212. if((framesize != 30) && (framesize != 60))
  213. {
  214. printf("\n%d is not a valid frame length. Valid length are 30 and 60 msec.\n", framesize);
  215. exit(0);
  216. }
  217. setControlBWE = 1;
  218. printf("Frame Length: %d\n", framesize);
  219. i++;
  220. }
  221. /* Fixed frame length */
  222. if(!strcmp ("-FIXED_FL", argv[i]))
  223. {
  224. fixedFL = 1;
  225. setControlBWE = 1;
  226. printf("Fixed Frame Length\n");
  227. }
  228. /* Set maximum allowed payload size in bytes */
  229. if(!strcmp ("-MAX", argv[i])) {
  230. payloadSize = atoi(argv[i + 1]);
  231. printf("Maximum Payload Size: %d\n", payloadSize);
  232. i++;
  233. }
  234. /* Set maximum rate in bytes */
  235. if(!strcmp ("-MAXRATE", argv[i])) {
  236. payloadRate = atoi(argv[i + 1]);
  237. printf("Maximum Rate in kbps: %d\n", payloadRate);
  238. i++;
  239. }
  240. /* Test of fault scenarious */
  241. if(!strcmp ("-F", argv[i]))
  242. {
  243. testNum = atoi(argv[i + 1]);
  244. printf("Fault test: %d\n", testNum);
  245. if(testNum < 1 || testNum > 10)
  246. {
  247. printf("\n%d is not a valid Fault Scenario number. Valid Fault Scenarios are numbered 1-10.\n", testNum);
  248. exit(0);
  249. }
  250. i++;
  251. }
  252. /* Packet loss test */
  253. if(!strcmp ("-PL", argv[i]))
  254. {
  255. if( isdigit( *argv[i+1] ) )
  256. {
  257. packetLossPercent = atoi( argv[i+1] );
  258. if( (packetLossPercent < 0) | (packetLossPercent > 100) )
  259. {
  260. printf( "\nInvalid packet loss perentage \n" );
  261. exit( 0 );
  262. }
  263. if( packetLossPercent > 0 )
  264. {
  265. printf( "Simulating %d %% of independent packet loss\n", packetLossPercent );
  266. }
  267. else
  268. {
  269. printf( "\nNo Packet Loss Is Simulated \n" );
  270. }
  271. }
  272. else
  273. {
  274. plFile = fopen( argv[i+1], "rb" );
  275. if( plFile == NULL )
  276. {
  277. printf( "\n couldn't open the frameloss file: %s\n", argv[i+1] );
  278. exit( 0 );
  279. }
  280. printf( "Simulating packet loss through the given channel file: %s\n", argv[i+1] );
  281. }
  282. i++;
  283. }
  284. /* Random packetlosses */
  285. if(!strcmp ("-rnd", argv[i]))
  286. {
  287. srand((unsigned int)time(NULL) );
  288. printf( "Random pattern in lossed packets \n" );
  289. }
  290. /* Use gns file */
  291. if(!strcmp ("-G", argv[i]))
  292. {
  293. sscanf(argv[i + 1], "%s", gns_file);
  294. fp_gns = fopen(gns_file, "rb");
  295. if(fp_gns == NULL)
  296. {
  297. printf("Cannot read file %s.\n", gns_file);
  298. exit(0);
  299. }
  300. i++;
  301. }
  302. // make it with '-B'
  303. /* Get Bottleneck value */
  304. if(!strcmp("-B", argv[i]))
  305. {
  306. i++;
  307. bottleneck = atoi(argv[i]);
  308. if(bottleneck == 0)
  309. {
  310. sscanf(argv[i], "%s", bottleneck_file);
  311. f_bn = fopen(bottleneck_file, "rb");
  312. if(f_bn == NULL)
  313. {
  314. printf("Error No value provided for BottleNeck and cannot read file %s.\n", bottleneck_file);
  315. exit(0);
  316. }
  317. else
  318. {
  319. printf("reading bottleneck rates from file %s\n\n",bottleneck_file);
  320. if(fscanf(f_bn, "%d", &bottleneck) == EOF)
  321. {
  322. /* Set pointer to beginning of file */
  323. fseek(f_bn, 0L, SEEK_SET);
  324. if (fscanf(f_bn, "%d", &bottleneck) == EOF) {
  325. exit(0);
  326. }
  327. }
  328. /* Bottleneck is a cosine function
  329. * Matlab code for writing the bottleneck file:
  330. * BottleNeck_10ms = 20e3 + 10e3 * cos((0:5999)/5999*2*pi);
  331. * fid = fopen('bottleneck.txt', 'wb');
  332. * fprintf(fid, '%d\n', BottleNeck_10ms); fclose(fid);
  333. */
  334. }
  335. }
  336. else
  337. {
  338. printf("\nfixed bottleneck rate of %d bits/s\n\n", bottleneck);
  339. }
  340. }
  341. /* Run Conference Engine APIs */
  342. // Do not test it in the first release
  343. //
  344. // if(!strcmp ("-CE", argv[i]))
  345. // {
  346. // testCE = atoi(argv[i + 1]);
  347. // if(testCE==1)
  348. // {
  349. // i++;
  350. // scale = (float)atof( argv[i+1] );
  351. // }
  352. // else if(testCE == 2)
  353. // {
  354. // printf("\nCE-test 2 (transcoding) not implemented.\n");
  355. // exit(0);
  356. // }
  357. // else if(testCE < 1 || testCE > 3)
  358. // {
  359. // printf("\n%d is not a valid CE-test number. Valid CE tests are 1-3.\n", testCE);
  360. // exit(0);
  361. // }
  362. // printf("CE-test number: %d\n", testCE);
  363. // i++;
  364. // }
  365. }
  366. if(CodingMode == 0)
  367. {
  368. printf("\nAdaptive BottleNeck\n");
  369. }
  370. switch(sampFreqKHz)
  371. {
  372. case 16:
  373. {
  374. printf("iSAC Wideband.\n");
  375. samplesIn10Ms = FRAMESAMPLES_10ms;
  376. break;
  377. }
  378. case 32:
  379. {
  380. printf("iSAC Supper-Wideband.\n");
  381. samplesIn10Ms = SWBFRAMESAMPLES_10ms;
  382. break;
  383. }
  384. default:
  385. printf("Unsupported sampling frequency %d kHz", sampFreqKHz);
  386. exit(0);
  387. }
  388. /* Get Input and Output files */
  389. sscanf(argv[argc-2], "%s", inname);
  390. sscanf(argv[argc-1], "%s", outname);
  391. printf("\nInput file: %s\n", inname);
  392. printf("Output file: %s\n\n", outname);
  393. if((inp = fopen(inname,"rb")) == NULL)
  394. {
  395. printf(" Error iSAC Cannot read file %s.\n", inname);
  396. cout << flush;
  397. exit(1);
  398. }
  399. if((outp = fopen(outname,"wb")) == NULL)
  400. {
  401. printf(" Error iSAC Cannot write file %s.\n", outname);
  402. cout << flush;
  403. getchar();
  404. exit(1);
  405. }
  406. if(VADusage)
  407. {
  408. if((vadp = fopen(vadfile,"rb")) == NULL)
  409. {
  410. printf(" Error iSAC Cannot read file %s.\n", vadfile);
  411. cout << flush;
  412. exit(1);
  413. }
  414. }
  415. if((bandwidthp = fopen("bwe.pcm","wb")) == NULL)
  416. {
  417. printf(" Error iSAC Cannot read file %s.\n", "bwe.pcm");
  418. cout << flush;
  419. exit(1);
  420. }
  421. starttime = clock()/(double)CLOCKS_PER_SEC; /* Runtime statistics */
  422. /* Initialize the ISAC and BN structs */
  423. if(testNum != 8)
  424. {
  425. if(!useAssign)
  426. {
  427. err =WebRtcIsac_Create(&ISAC_main_inst);
  428. WebRtcIsac_SetEncSampRate(ISAC_main_inst, (sampFreqKHz == 16)? kIsacWideband:kIsacSuperWideband);
  429. WebRtcIsac_SetDecSampRate(ISAC_main_inst, (sampFreqKHz == 16)? kIsacWideband:kIsacSuperWideband);
  430. }
  431. else
  432. {
  433. /* Test the Assign functions */
  434. int sss;
  435. void *ppp;
  436. err = WebRtcIsac_AssignSize(&sss);
  437. ppp = malloc(sss);
  438. err = WebRtcIsac_Assign(&ISAC_main_inst, ppp);
  439. WebRtcIsac_SetEncSampRate(ISAC_main_inst, (sampFreqKHz == 16)? kIsacWideband:kIsacSuperWideband);
  440. WebRtcIsac_SetDecSampRate(ISAC_main_inst, (sampFreqKHz == 16)? kIsacWideband:kIsacSuperWideband);
  441. }
  442. /* Error check */
  443. if(err < 0)
  444. {
  445. printf("\n\n Error in create.\n\n");
  446. cout << flush;
  447. exit(EXIT_FAILURE);
  448. }
  449. }
  450. BN_data.arrival_time = 0;
  451. BN_data.sample_count = 0;
  452. BN_data.rtp_number = 0;
  453. /* Initialize encoder and decoder */
  454. framecnt= 0;
  455. endfile = 0;
  456. if(doTransCoding)
  457. {
  458. WebRtcIsac_Create(&decoderTransCoding);
  459. WebRtcIsac_SetEncSampRate(decoderTransCoding, (sampFreqKHz == 16)? kIsacWideband:kIsacSuperWideband);
  460. WebRtcIsac_SetDecSampRate(decoderTransCoding, (sampFreqKHz == 16)? kIsacWideband:kIsacSuperWideband);
  461. WebRtcIsac_DecoderInit(decoderTransCoding);
  462. transCodingFile = fopen(transCodingFileName, "wb");
  463. if(transCodingFile == NULL)
  464. {
  465. printf("Could not open %s to output trans-coding.\n", transCodingFileName);
  466. exit(0);
  467. }
  468. strcat(transCodingFileName, ".bit");
  469. transcodingBitstream = fopen(transCodingFileName, "wb");
  470. if(transcodingBitstream == NULL)
  471. {
  472. printf("Could not open %s to write the bit-stream of transcoder.\n", transCodingFileName);
  473. exit(0);
  474. }
  475. }
  476. if(testNum != 1)
  477. {
  478. if(WebRtcIsac_EncoderInit(ISAC_main_inst, CodingMode) < 0)
  479. {
  480. printf("Error could not initialize the encoder \n");
  481. cout << flush;
  482. return 0;
  483. }
  484. }
  485. if(testNum != 2)
  486. {
  487. if(WebRtcIsac_DecoderInit(ISAC_main_inst) < 0)
  488. {
  489. printf("Error could not initialize the decoder \n");
  490. cout << flush;
  491. return 0;
  492. }
  493. }
  494. if(CodingMode == 1)
  495. {
  496. err = WebRtcIsac_Control(ISAC_main_inst, bottleneck, framesize);
  497. if(err < 0)
  498. {
  499. /* exit if returned with error */
  500. errtype=WebRtcIsac_GetErrorCode(ISAC_main_inst);
  501. printf("\n\n Error in initialization (control): %d.\n\n", errtype);
  502. cout << flush;
  503. if(testNum == 0)
  504. {
  505. exit(EXIT_FAILURE);
  506. }
  507. }
  508. }
  509. if((setControlBWE) && (CodingMode == 0))
  510. {
  511. err = WebRtcIsac_ControlBwe(ISAC_main_inst, rateBPS, framesize, fixedFL);
  512. if(err < 0)
  513. {
  514. /* exit if returned with error */
  515. errtype=WebRtcIsac_GetErrorCode(ISAC_main_inst);
  516. printf("\n\n Error in Control BWE: %d.\n\n", errtype);
  517. cout << flush;
  518. exit(EXIT_FAILURE);
  519. }
  520. }
  521. if(payloadSize != 0)
  522. {
  523. err = WebRtcIsac_SetMaxPayloadSize(ISAC_main_inst, payloadSize);
  524. if(err < 0)
  525. {
  526. /* exit if returned with error */
  527. errtype=WebRtcIsac_GetErrorCode(ISAC_main_inst);
  528. printf("\n\n Error in SetMaxPayloadSize: %d.\n\n", errtype);
  529. cout << flush;
  530. exit(EXIT_FAILURE);
  531. }
  532. }
  533. if(payloadRate != 0)
  534. {
  535. err = WebRtcIsac_SetMaxRate(ISAC_main_inst, payloadRate);
  536. if(err < 0)
  537. {
  538. /* exit if returned with error */
  539. errtype=WebRtcIsac_GetErrorCode(ISAC_main_inst);
  540. printf("\n\n Error in SetMaxRateInBytes: %d.\n\n", errtype);
  541. cout << flush;
  542. exit(EXIT_FAILURE);
  543. }
  544. }
  545. *speechType = 1;
  546. cout << "\n" << flush;
  547. length_file = 0;
  548. WebRtc_Word16 bnIdxTC;
  549. WebRtc_Word16 jitterInfoTC;
  550. while (endfile == 0)
  551. {
  552. /* Call init functions at random, fault test number 7 */
  553. if(testNum == 7 && (rand()%2 == 0))
  554. {
  555. err = WebRtcIsac_EncoderInit(ISAC_main_inst, CodingMode);
  556. /* Error check */
  557. if(err < 0)
  558. {
  559. errtype=WebRtcIsac_GetErrorCode(ISAC_main_inst);
  560. printf("\n\n Error in encoderinit: %d.\n\n", errtype);
  561. cout << flush;
  562. }
  563. err = WebRtcIsac_DecoderInit(ISAC_main_inst);
  564. /* Error check */
  565. if(err < 0)
  566. {
  567. errtype=WebRtcIsac_GetErrorCode(ISAC_main_inst);
  568. printf("\n\n Error in decoderinit: %d.\n\n", errtype);
  569. cout << flush;
  570. }
  571. }
  572. cur_framesmpls = 0;
  573. while (1)
  574. {
  575. /* Read 10 ms speech block */
  576. endfile = readframe(shortdata, inp, samplesIn10Ms);
  577. if(endfile)
  578. {
  579. numFileLoop++;
  580. if(numFileLoop < totFileLoop)
  581. {
  582. rewind(inp);
  583. framecnt = 0;
  584. fprintf(stderr, "\n");
  585. endfile = readframe(shortdata, inp, samplesIn10Ms);
  586. }
  587. }
  588. if(testNum == 7)
  589. {
  590. srand((unsigned int)time(NULL));
  591. }
  592. /* iSAC encoding */
  593. if(!(testNum == 3 && framecnt == 0))
  594. {
  595. stream_len = WebRtcIsac_Encode(ISAC_main_inst,
  596. shortdata,
  597. (WebRtc_Word16*)streamdata);
  598. if((payloadSize != 0) && (stream_len > payloadSize))
  599. {
  600. if(testNum == 0)
  601. {
  602. printf("\n\n");
  603. }
  604. printf("\nError: Streamsize out of range %d\n", stream_len - payloadSize);
  605. cout << flush;
  606. }
  607. WebRtcIsac_GetUplinkBw(ISAC_main_inst, &sendBN);
  608. if(stream_len>0)
  609. {
  610. if(doTransCoding)
  611. {
  612. WebRtc_Word16 indexStream;
  613. WebRtc_UWord8 auxUW8;
  614. /************************* Main Transcoding stream *******************************/
  615. WebRtcIsac_GetDownLinkBwIndex(ISAC_main_inst, &bnIdxTC, &jitterInfoTC);
  616. streamLenTransCoding = WebRtcIsac_GetNewBitStream(
  617. ISAC_main_inst, bnIdxTC, jitterInfoTC, rateTransCoding,
  618. (WebRtc_Word16*)streamDataTransCoding, false);
  619. if(streamLenTransCoding < 0)
  620. {
  621. fprintf(stderr, "Error in trans-coding\n");
  622. exit(0);
  623. }
  624. auxUW8 = (WebRtc_UWord8)(((streamLenTransCoding & 0xFF00) >> 8) & 0x00FF);
  625. fwrite(&auxUW8, sizeof(WebRtc_UWord8),
  626. 1, transcodingBitstream);
  627. auxUW8 = (WebRtc_UWord8)(streamLenTransCoding & 0x00FF);
  628. fwrite(&auxUW8, sizeof(WebRtc_UWord8),
  629. 1, transcodingBitstream);
  630. fwrite((WebRtc_UWord8*)streamDataTransCoding, sizeof(WebRtc_UWord8),
  631. streamLenTransCoding, transcodingBitstream);
  632. WebRtcIsac_ReadBwIndex((WebRtc_Word16*)streamDataTransCoding, &indexStream);
  633. if(indexStream != bnIdxTC)
  634. {
  635. fprintf(stderr, "Error in inserting Bandwidth index into transcoding stream.\n");
  636. exit(0);
  637. }
  638. numTransCodingBytes += streamLenTransCoding;
  639. }
  640. }
  641. }
  642. else
  643. {
  644. break;
  645. }
  646. if(stream_len < 0)
  647. {
  648. /* exit if returned with error */
  649. errtype=WebRtcIsac_GetErrorCode(ISAC_main_inst);
  650. printf("\n\nError in encoder: %d.\n\n", errtype);
  651. cout << flush;
  652. }
  653. cur_framesmpls += samplesIn10Ms;
  654. /* exit encoder loop if the encoder returned a bitstream */
  655. if(stream_len != 0) break;
  656. }
  657. /* read next bottleneck rate */
  658. if(f_bn != NULL)
  659. {
  660. if(fscanf(f_bn, "%d", &bottleneck) == EOF)
  661. {
  662. /* Set pointer to beginning of file */
  663. fseek(f_bn, 0L, SEEK_SET);
  664. if (fscanf(f_bn, "%d", &bottleneck) == EOF) {
  665. exit(0);
  666. }
  667. }
  668. if(CodingMode == 1)
  669. {
  670. WebRtcIsac_Control(ISAC_main_inst, bottleneck, framesize);
  671. }
  672. }
  673. length_file += cur_framesmpls;
  674. if(cur_framesmpls == (3 * samplesIn10Ms))
  675. {
  676. maxStreamLen30 = (stream_len > maxStreamLen30)? stream_len:maxStreamLen30;
  677. }
  678. else
  679. {
  680. maxStreamLen60 = (stream_len > maxStreamLen60)? stream_len:maxStreamLen60;
  681. }
  682. if(!lostFrame)
  683. {
  684. lostFrame = ((rand()%100) < packetLossPercent);
  685. }
  686. else
  687. {
  688. lostFrame = 0;
  689. }
  690. // RED.
  691. if(lostFrame)
  692. {
  693. stream_len = WebRtcIsac_GetRedPayload(ISAC_main_inst,
  694. (WebRtc_Word16*)streamdata);
  695. if(doTransCoding)
  696. {
  697. streamLenTransCoding = WebRtcIsac_GetNewBitStream(
  698. ISAC_main_inst, bnIdxTC, jitterInfoTC, rateTransCoding,
  699. (WebRtc_Word16*)streamDataTransCoding, true);
  700. if(streamLenTransCoding < 0)
  701. {
  702. fprintf(stderr, "Error in RED trans-coding\n");
  703. exit(0);
  704. }
  705. }
  706. }
  707. /* make coded sequence to short be inreasing */
  708. /* the length the decoder expects */
  709. if(testNum == 4)
  710. {
  711. stream_len += 10;
  712. }
  713. /* make coded sequence to long be decreasing */
  714. /* the length the decoder expects */
  715. if(testNum == 5)
  716. {
  717. stream_len -= 10;
  718. }
  719. if(testNum == 6)
  720. {
  721. srand((unsigned int)time(NULL));
  722. for(i = 0; i < stream_len; i++)
  723. {
  724. streamdata[i] = rand();
  725. }
  726. }
  727. if(VADusage){
  728. readframe(vaddata, vadp, samplesIn10Ms*3);
  729. }
  730. /* simulate packet handling through NetEq and the modem */
  731. if(!(testNum == 3 && framecnt == 0))
  732. {
  733. get_arrival_time(cur_framesmpls, stream_len, bottleneck, &BN_data,
  734. sampFreqKHz*1000, sampFreqKHz*1000);
  735. }
  736. if(VADusage && (framecnt>10 && vaddata[0]==0))
  737. {
  738. BN_data.rtp_number--;
  739. }
  740. else
  741. {
  742. /* Error test number 10, garbage data */
  743. if(testNum == 10)
  744. {
  745. /* Test to run decoder with garbage data */
  746. for(i = 0; i < stream_len; i++)
  747. {
  748. streamdata[i] = (short) (streamdata[i]) + (short) rand();
  749. }
  750. }
  751. if(testNum != 9)
  752. {
  753. err = WebRtcIsac_UpdateBwEstimate(ISAC_main_inst, streamdata,
  754. stream_len, BN_data.rtp_number, BN_data.sample_count,
  755. BN_data.arrival_time);
  756. if(err < 0)
  757. {
  758. /* exit if returned with error */
  759. errtype=WebRtcIsac_GetErrorCode(ISAC_main_inst);
  760. if(testNum == 0)
  761. {
  762. printf("\n\n");
  763. }
  764. printf("Error: in decoder: %d.", errtype);
  765. cout << flush;
  766. if(testNum == 0)
  767. {
  768. printf("\n\n");
  769. }
  770. }
  771. }
  772. /* Call getFramelen, only used here for function test */
  773. err = WebRtcIsac_ReadFrameLen(ISAC_main_inst,
  774. (WebRtc_Word16*)streamdata, &FL);
  775. if(err < 0)
  776. {
  777. /* exit if returned with error */
  778. errtype=WebRtcIsac_GetErrorCode(ISAC_main_inst);
  779. if(testNum == 0)
  780. {
  781. printf("\n\n");
  782. }
  783. printf(" Error: in getFrameLen %d.", errtype);
  784. cout << flush;
  785. if(testNum == 0)
  786. {
  787. printf("\n\n");
  788. }
  789. }
  790. // iSAC decoding
  791. if(lostFrame)
  792. {
  793. declen = WebRtcIsac_DecodeRcu(ISAC_main_inst, streamdata,
  794. stream_len, decoded, speechType);
  795. if(doTransCoding)
  796. {
  797. declenTC = WebRtcIsac_DecodeRcu(decoderTransCoding,
  798. streamDataTransCoding, streamLenTransCoding,
  799. decodedTC, speechType);
  800. }
  801. }
  802. else
  803. {
  804. declen = WebRtcIsac_Decode(ISAC_main_inst, streamdata,
  805. stream_len, decoded, speechType);
  806. if(doTransCoding)
  807. {
  808. declenTC = WebRtcIsac_Decode(decoderTransCoding,
  809. streamDataTransCoding, streamLenTransCoding,
  810. decodedTC, speechType);
  811. }
  812. }
  813. if(declen < 0)
  814. {
  815. /* exit if returned with error */
  816. errtype=WebRtcIsac_GetErrorCode(ISAC_main_inst);
  817. if(testNum == 0)
  818. {
  819. printf("\n\n");
  820. }
  821. printf(" Error: in decoder %d.", errtype);
  822. cout << flush;
  823. if(testNum == 0)
  824. {
  825. printf("\n\n");
  826. }
  827. }
  828. if(declenTC < 0)
  829. {
  830. if(testNum == 0)
  831. {
  832. printf("\n\n");
  833. }
  834. printf(" Error: in decoding the transcoded stream");
  835. cout << flush;
  836. if(testNum == 0)
  837. {
  838. printf("\n\n");
  839. }
  840. }
  841. }
  842. /* Write decoded speech frame to file */
  843. if((declen > 0) && (numFileLoop == 0))
  844. {
  845. fwrite(decoded, sizeof(WebRtc_Word16), declen, outp);
  846. }
  847. if((declenTC > 0) && (numFileLoop == 0))
  848. {
  849. fwrite(decodedTC, sizeof(WebRtc_Word16), declen, transCodingFile);
  850. }
  851. fprintf(stderr, "\rframe = %5d ", framecnt);
  852. fflush(stderr);
  853. framecnt++;
  854. /* Error test number 10, garbage data */
  855. //if(testNum == 10)
  856. //{
  857. // /* Test to run decoder with garbage data */
  858. // if( (seedfile = fopen(SEED_FILE, "a+t") ) == NULL )
  859. // {
  860. // fprintf(stderr, "Error: Could not open file %s\n", SEED_FILE);
  861. // }
  862. // else
  863. // {
  864. // fprintf(seedfile, "ok\n\n");
  865. // fclose(seedfile);
  866. // }
  867. //}
  868. /* Error test number 10, garbage data */
  869. //if(testNum == 10)
  870. //{
  871. // /* Test to run decoder with garbage data */
  872. // for ( i = 0; i < stream_len; i++)
  873. // {
  874. // streamdata[i] = (short) (streamdata[i] + (short) rand());
  875. // }
  876. //}
  877. totalsmpls += declen;
  878. totalbits += 8 * stream_len;
  879. #ifdef _DEBUG
  880. kbps = ((double) sampFreqKHz * 1000.) / ((double) cur_framesmpls) * 8.0 * stream_len / 1000.0;// kbits/s
  881. fy = fopen("bit_rate.dat", "a");
  882. fprintf(fy, "Frame %i = %0.14f\n", framecnt, kbps);
  883. fclose(fy);
  884. #endif /* _DEBUG */
  885. }
  886. printf("\n");
  887. printf("total bits = %d bits\n", totalbits);
  888. printf("measured average bitrate = %0.3f kbits/s\n",
  889. (double)totalbits *(sampFreqKHz) / totalsmpls);
  890. if(doTransCoding)
  891. {
  892. printf("Transcoding average bit-rate = %0.3f kbps\n",
  893. (double)numTransCodingBytes * 8.0 *(sampFreqKHz) / totalsmpls);
  894. fclose(transCodingFile);
  895. }
  896. printf("\n");
  897. /* Runtime statistics */
  898. runtime = (double)(clock()/(double)CLOCKS_PER_SEC-starttime);
  899. length_file = length_file /(sampFreqKHz * 1000.);
  900. printf("\n\nLength of speech file: %.1f s\n", length_file);
  901. printf("Time to run iSAC: %.2f s (%.2f %% of realtime)\n\n", runtime, (100*runtime/length_file));
  902. if(maxStreamLen30 != 0)
  903. {
  904. printf("Maximum payload size 30ms Frames %d bytes (%0.3f kbps)\n",
  905. maxStreamLen30,
  906. maxStreamLen30 * 8 / 30.);
  907. }
  908. if(maxStreamLen60 != 0)
  909. {
  910. printf("Maximum payload size 60ms Frames %d bytes (%0.3f kbps)\n",
  911. maxStreamLen60,
  912. maxStreamLen60 * 8 / 60.);
  913. }
  914. //fprintf(stderr, "\n");
  915. fprintf(stderr, " %.1f s", length_file);
  916. fprintf(stderr, " %0.1f kbps", (double)totalbits *(sampFreqKHz) / totalsmpls);
  917. if(maxStreamLen30 != 0)
  918. {
  919. fprintf(stderr, " plmax-30ms %d bytes (%0.0f kbps)",
  920. maxStreamLen30,
  921. maxStreamLen30 * 8 / 30.);
  922. }
  923. if(maxStreamLen60 != 0)
  924. {
  925. fprintf(stderr, " plmax-60ms %d bytes (%0.0f kbps)",
  926. maxStreamLen60,
  927. maxStreamLen60 * 8 / 60.);
  928. }
  929. if(doTransCoding)
  930. {
  931. fprintf(stderr, " transcoding rate %.0f kbps",
  932. (double)numTransCodingBytes * 8.0 *(sampFreqKHz) / totalsmpls);
  933. }
  934. fclose(inp);
  935. fclose(outp);
  936. WebRtcIsac_Free(ISAC_main_inst);
  937. exit(0);
  938. }