PageRenderTime 58ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/CSipSimple/jni/webrtc/sources/modules/audio_coding/codecs/iSAC/fix/test/kenny.c

http://csipsimple.googlecode.com/
C | 820 lines | 624 code | 102 blank | 94 comment | 198 complexity | 6e28187e3bccaea270527b29dab29d2e 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) 2012 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. /* kenny.c - Main function for the iSAC coder */
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <time.h>
  15. #include <ctype.h>
  16. #include "isacfix.h"
  17. /* Defines */
  18. #define SEED_FILE "randseed.txt" /* Used when running decoder on garbage data */
  19. #define MAX_FRAMESAMPLES 960 /* max number of samples per frame (= 60 ms frame) */
  20. #define FRAMESAMPLES_10ms 160 /* number of samples per 10ms frame */
  21. #define FS 16000 /* sampling frequency (Hz) */
  22. /* Function for reading audio data from PCM file */
  23. int readframe(WebRtc_Word16 *data, FILE *inp, int length) {
  24. short k, rlen, status = 0;
  25. rlen = fread(data, sizeof(WebRtc_Word16), length, inp);
  26. if (rlen < length) {
  27. for (k = rlen; k < length; k++)
  28. data[k] = 0;
  29. status = 1;
  30. }
  31. return status;
  32. }
  33. /* Struct for bottleneck model */
  34. typedef struct {
  35. WebRtc_UWord32 send_time; /* samples */
  36. WebRtc_UWord32 arrival_time; /* samples */
  37. WebRtc_UWord32 sample_count; /* samples */
  38. WebRtc_UWord16 rtp_number;
  39. } BottleNeckModel;
  40. void get_arrival_time(int current_framesamples, /* samples */
  41. int packet_size, /* bytes */
  42. int bottleneck, /* excluding headers; bits/s */
  43. BottleNeckModel *BN_data)
  44. {
  45. const int HeaderSize = 35;
  46. int HeaderRate;
  47. HeaderRate = HeaderSize * 8 * FS / current_framesamples; /* bits/s */
  48. /* everything in samples */
  49. BN_data->sample_count = BN_data->sample_count + current_framesamples;
  50. BN_data->arrival_time += ((packet_size + HeaderSize) * 8 * FS) / (bottleneck + HeaderRate);
  51. BN_data->send_time += current_framesamples;
  52. if (BN_data->arrival_time < BN_data->sample_count)
  53. BN_data->arrival_time = BN_data->sample_count;
  54. BN_data->rtp_number++;
  55. }
  56. void get_arrival_time2(int current_framesamples,
  57. int current_delay,
  58. BottleNeckModel *BN_data)
  59. {
  60. if (current_delay == -1)
  61. //dropped packet
  62. {
  63. BN_data->arrival_time += current_framesamples;
  64. }
  65. else if (current_delay != -2)
  66. {
  67. //
  68. BN_data->arrival_time += (current_framesamples + ((FS/1000) * current_delay));
  69. }
  70. //else
  71. //current packet has same timestamp as previous packet
  72. BN_data->rtp_number++;
  73. }
  74. int main(int argc, char* argv[])
  75. {
  76. char inname[100], outname[100], outbitsname[100], bottleneck_file[100];
  77. FILE *inp, *outp, *f_bn, *outbits;
  78. int endfile;
  79. int i, errtype, h = 0, k, packetLossPercent = 0;
  80. WebRtc_Word16 CodingMode;
  81. WebRtc_Word16 bottleneck;
  82. WebRtc_Word16 framesize = 30; /* ms */
  83. int cur_framesmpls, err = 0, lostPackets = 0;
  84. /* Runtime statistics */
  85. double starttime, runtime, length_file;
  86. WebRtc_Word16 stream_len = 0;
  87. WebRtc_Word16 framecnt, declen = 0;
  88. WebRtc_Word16 shortdata[FRAMESAMPLES_10ms];
  89. WebRtc_Word16 decoded[MAX_FRAMESAMPLES];
  90. WebRtc_UWord16 streamdata[500];
  91. WebRtc_Word16 speechType[1];
  92. WebRtc_Word16 prevFrameSize = 1;
  93. WebRtc_Word16 rateBPS = 0;
  94. WebRtc_Word16 fixedFL = 0;
  95. WebRtc_Word16 payloadSize = 0;
  96. WebRtc_Word32 payloadRate = 0;
  97. int setControlBWE = 0;
  98. int readLoss;
  99. FILE *plFile = NULL;
  100. char version_number[20];
  101. char tmpBit[5] = ".bit";
  102. int totalbits =0;
  103. int totalsmpls =0;
  104. WebRtc_Word16 testNum, testCE;
  105. FILE *fp_gns = NULL;
  106. int gns = 0;
  107. int cur_delay = 0;
  108. char gns_file[100];
  109. int nbTest = 0;
  110. WebRtc_Word16 lostFrame;
  111. float scale = (float)0.7;
  112. /* only one structure used for ISAC encoder */
  113. ISACFIX_MainStruct *ISAC_main_inst;
  114. /* For fault test 10, garbage data */
  115. FILE *seedfile;
  116. unsigned int random_seed = (unsigned int) time(NULL);//1196764538
  117. BottleNeckModel BN_data;
  118. f_bn = NULL;
  119. readLoss = 0;
  120. packetLossPercent = 0;
  121. /* Handling wrong input arguments in the command line */
  122. if ((argc<3) || (argc>21)) {
  123. printf("\n\nWrong number of arguments or flag values.\n\n");
  124. printf("\n");
  125. WebRtcIsacfix_version(version_number);
  126. printf("iSAC version %s \n\n", version_number);
  127. printf("Usage:\n\n");
  128. printf("./kenny.exe [-F num][-I] bottleneck_value infile outfile \n\n");
  129. printf("with:\n");
  130. printf("[-I] :if -I option is specified, the coder will use\n");
  131. printf(" an instantaneous Bottleneck value. If not, it\n");
  132. printf(" will be an adaptive Bottleneck value.\n\n");
  133. printf("bottleneck_value :the value of the bottleneck provided either\n");
  134. printf(" as a fixed value (e.g. 25000) or\n");
  135. printf(" read from a file (e.g. bottleneck.txt)\n\n");
  136. printf("[-INITRATE num] :Set a new value for initial rate. Note! Only used"
  137. " in adaptive mode.\n\n");
  138. printf("[-FL num] :Set (initial) frame length in msec. Valid length"
  139. " are 30 and 60 msec.\n\n");
  140. printf("[-FIXED_FL] :Frame length will be fixed to initial value.\n\n");
  141. printf("[-MAX num] :Set the limit for the payload size of iSAC"
  142. " in bytes. \n");
  143. printf(" Minimum 100, maximum 400.\n\n");
  144. printf("[-MAXRATE num] :Set the maxrate for iSAC in bits per second. \n");
  145. printf(" Minimum 32000, maximum 53400.\n\n");
  146. printf("[-F num] :if -F option is specified, the test function\n");
  147. printf(" will run the iSAC API fault scenario specified"
  148. " by the\n");
  149. printf(" supplied number.\n");
  150. printf(" F 1 - Call encoder prior to init encoder call\n");
  151. printf(" F 2 - Call decoder prior to init decoder call\n");
  152. printf(" F 3 - Call decoder prior to encoder call\n");
  153. printf(" F 4 - Call decoder with a too short coded"
  154. " sequence\n");
  155. printf(" F 5 - Call decoder with a too long coded"
  156. " sequence\n");
  157. printf(" F 6 - Call decoder with random bit stream\n");
  158. printf(" F 7 - Call init encoder/decoder at random"
  159. " during a call\n");
  160. printf(" F 8 - Call encoder/decoder without having"
  161. " allocated memory for \n");
  162. printf(" encoder/decoder instance\n");
  163. printf(" F 9 - Call decodeB without calling decodeA\n");
  164. printf(" F 10 - Call decodeB with garbage data\n");
  165. printf("[-PL num] : if -PL option is specified 0<num<100 will "
  166. "specify the\n");
  167. printf(" percentage of packet loss\n\n");
  168. printf("[-G file] : if -G option is specified the file given is"
  169. " a .gns file\n");
  170. printf(" that represents a network profile\n\n");
  171. printf("[-NB num] : if -NB option, use the narrowband interfaces\n");
  172. printf(" num=1 => encode with narrowband encoder"
  173. " (infile is narrowband)\n");
  174. printf(" num=2 => decode with narrowband decoder"
  175. " (outfile is narrowband)\n\n");
  176. printf("[-CE num] : Test of APIs used by Conference Engine.\n");
  177. printf(" CE 1 - createInternal, freeInternal,"
  178. " getNewBitstream \n");
  179. printf(" CE 2 - transcode, getBWE \n");
  180. printf(" CE 3 - getSendBWE, setSendBWE. \n\n");
  181. printf("[-RTP_INIT num] : if -RTP_INIT option is specified num will be"
  182. " the initial\n");
  183. printf(" value of the rtp sequence number.\n\n");
  184. printf("infile : Normal speech input file\n\n");
  185. printf("outfile : Speech output file\n\n");
  186. printf("Example usage : \n\n");
  187. printf("./kenny.exe -I bottleneck.txt speechIn.pcm speechOut.pcm\n\n");
  188. exit(0);
  189. }
  190. /* Print version number */
  191. WebRtcIsacfix_version(version_number);
  192. printf("iSAC version %s \n\n", version_number);
  193. /* Loop over all command line arguments */
  194. CodingMode = 0;
  195. testNum = 0;
  196. testCE = 0;
  197. for (i = 1; i < argc-2;i++) {
  198. /* Instantaneous mode */
  199. if (!strcmp ("-I", argv[i])) {
  200. printf("\nInstantaneous BottleNeck\n");
  201. CodingMode = 1;
  202. i++;
  203. }
  204. /* Set (initial) bottleneck value */
  205. if (!strcmp ("-INITRATE", argv[i])) {
  206. rateBPS = atoi(argv[i + 1]);
  207. setControlBWE = 1;
  208. if ((rateBPS < 10000) || (rateBPS > 32000)) {
  209. printf("\n%d is not a initial rate. "
  210. "Valid values are in the range 10000 to 32000.\n", rateBPS);
  211. exit(0);
  212. }
  213. printf("\nNew initial rate: %d\n", rateBPS);
  214. i++;
  215. }
  216. /* Set (initial) framelength */
  217. if (!strcmp ("-FL", argv[i])) {
  218. framesize = atoi(argv[i + 1]);
  219. if ((framesize != 30) && (framesize != 60)) {
  220. printf("\n%d is not a valid frame length. "
  221. "Valid length are 30 and 60 msec.\n", framesize);
  222. exit(0);
  223. }
  224. printf("\nFrame Length: %d\n", framesize);
  225. i++;
  226. }
  227. /* Fixed frame length */
  228. if (!strcmp ("-FIXED_FL", argv[i])) {
  229. fixedFL = 1;
  230. setControlBWE = 1;
  231. }
  232. /* Set maximum allowed payload size in bytes */
  233. if (!strcmp ("-MAX", argv[i])) {
  234. payloadSize = atoi(argv[i + 1]);
  235. printf("Maximum Payload Size: %d\n", payloadSize);
  236. i++;
  237. }
  238. /* Set maximum rate in bytes */
  239. if (!strcmp ("-MAXRATE", argv[i])) {
  240. payloadRate = atoi(argv[i + 1]);
  241. printf("Maximum Rate in kbps: %d\n", payloadRate);
  242. i++;
  243. }
  244. /* Test of fault scenarious */
  245. if (!strcmp ("-F", argv[i])) {
  246. testNum = atoi(argv[i + 1]);
  247. printf("\nFault test: %d\n", testNum);
  248. if (testNum < 1 || testNum > 10) {
  249. printf("\n%d is not a valid Fault Scenario number."
  250. " Valid Fault Scenarios are numbered 1-10.\n", testNum);
  251. exit(0);
  252. }
  253. i++;
  254. }
  255. /* Packet loss test */
  256. if (!strcmp ("-PL", argv[i])) {
  257. if( isdigit( *argv[i+1] ) ) {
  258. packetLossPercent = atoi( argv[i+1] );
  259. if( (packetLossPercent < 0) | (packetLossPercent > 100) ) {
  260. printf( "\nInvalid packet loss perentage \n" );
  261. exit( 0 );
  262. }
  263. if( packetLossPercent > 0 ) {
  264. printf( "\nSimulating %d %% of independent packet loss\n",
  265. packetLossPercent );
  266. } else {
  267. printf( "\nNo Packet Loss Is Simulated \n" );
  268. }
  269. readLoss = 0;
  270. } else {
  271. readLoss = 1;
  272. plFile = fopen( argv[i+1], "rb" );
  273. if( plFile == NULL ) {
  274. printf( "\n couldn't open the frameloss file: %s\n", argv[i+1] );
  275. exit( 0 );
  276. }
  277. printf( "\nSimulating packet loss through the given "
  278. "channel file: %s\n", argv[i+1] );
  279. }
  280. i++;
  281. }
  282. /* Random packetlosses */
  283. if (!strcmp ("-rnd", argv[i])) {
  284. srand(time(NULL) );
  285. printf( "\n Random pattern in lossed packets \n" );
  286. }
  287. /* Use gns file */
  288. if (!strcmp ("-G", argv[i])) {
  289. sscanf(argv[i + 1], "%s", gns_file);
  290. fp_gns = fopen(gns_file, "rb");
  291. if (fp_gns == NULL) {
  292. printf("Cannot read file %s.\n", gns_file);
  293. exit(0);
  294. }
  295. gns = 1;
  296. i++;
  297. }
  298. /* Run Narrowband interfaces (either encoder or decoder) */
  299. if (!strcmp ("-NB", argv[i])) {
  300. nbTest = atoi(argv[i + 1]);
  301. i++;
  302. }
  303. /* Run Conference Engine APIs */
  304. if (!strcmp ("-CE", argv[i])) {
  305. testCE = atoi(argv[i + 1]);
  306. if (testCE==1 || testCE==2) {
  307. i++;
  308. scale = (float)atof( argv[i+1] );
  309. } else if (testCE < 1 || testCE > 3) {
  310. printf("\n%d is not a valid CE-test number, valid Fault "
  311. "Scenarios are numbered 1-3\n", testCE);
  312. exit(0);
  313. }
  314. i++;
  315. }
  316. /* Set initial RTP number */
  317. if (!strcmp ("-RTP_INIT", argv[i])) {
  318. i++;
  319. }
  320. }
  321. /* Get Bottleneck value */
  322. /* Gns files and bottleneck should not and can not be used simultaneously */
  323. bottleneck = atoi(argv[CodingMode+1]);
  324. if (bottleneck == 0 && gns == 0) {
  325. sscanf(argv[CodingMode+1], "%s", bottleneck_file);
  326. f_bn = fopen(bottleneck_file, "rb");
  327. if (f_bn == NULL) {
  328. printf("No value provided for BottleNeck and cannot read file %s\n", bottleneck_file);
  329. exit(0);
  330. } else {
  331. int aux_var;
  332. printf("reading bottleneck rates from file %s\n\n",bottleneck_file);
  333. if (fscanf(f_bn, "%d", &aux_var) == EOF) {
  334. /* Set pointer to beginning of file */
  335. fseek(f_bn, 0L, SEEK_SET);
  336. if (fscanf(f_bn, "%d", &aux_var) == EOF) {
  337. exit(0);
  338. }
  339. }
  340. bottleneck = (WebRtc_Word16)aux_var;
  341. /* Bottleneck is a cosine function
  342. * Matlab code for writing the bottleneck file:
  343. * BottleNeck_10ms = 20e3 + 10e3 * cos((0:5999)/5999*2*pi);
  344. * fid = fopen('bottleneck.txt', 'wb');
  345. * fprintf(fid, '%d\n', BottleNeck_10ms); fclose(fid);
  346. */
  347. }
  348. } else {
  349. f_bn = NULL;
  350. printf("\nfixed bottleneck rate of %d bits/s\n\n", bottleneck);
  351. }
  352. if (CodingMode == 0) {
  353. printf("\nAdaptive BottleNeck\n");
  354. }
  355. /* Get Input and Output files */
  356. sscanf(argv[argc-2], "%s", inname);
  357. sscanf(argv[argc-1], "%s", outname);
  358. /* Add '.bit' to output bitstream file */
  359. while ((int)outname[h] != 0) {
  360. outbitsname[h] = outname[h];
  361. h++;
  362. }
  363. for (k=0; k<5; k++) {
  364. outbitsname[h] = tmpBit[k];
  365. h++;
  366. }
  367. if ((inp = fopen(inname,"rb")) == NULL) {
  368. printf(" iSAC: Cannot read file %s\n", inname);
  369. exit(1);
  370. }
  371. if ((outp = fopen(outname,"wb")) == NULL) {
  372. printf(" iSAC: Cannot write file %s\n", outname);
  373. exit(1);
  374. }
  375. if ((outbits = fopen(outbitsname,"wb")) == NULL) {
  376. printf(" iSAC: Cannot write file %s\n", outbitsname);
  377. exit(1);
  378. }
  379. printf("\nInput:%s\nOutput:%s\n\n", inname, outname);
  380. /* Error test number 10, garbage data */
  381. if (testNum == 10) {
  382. /* Test to run decoder with garbage data */
  383. srand(random_seed);
  384. if ( (seedfile = fopen(SEED_FILE, "a+t") ) == NULL ) {
  385. printf("Error: Could not open file %s\n", SEED_FILE);
  386. }
  387. else {
  388. fprintf(seedfile, "%u\n", random_seed);
  389. fclose(seedfile);
  390. }
  391. }
  392. /* Runtime statistics */
  393. starttime = clock()/(double)CLOCKS_PER_SEC;
  394. /* Initialize the ISAC and BN structs */
  395. if (testNum != 8)
  396. {
  397. if(1){
  398. err =WebRtcIsacfix_Create(&ISAC_main_inst);
  399. }else{
  400. /* Test the Assign functions */
  401. int sss;
  402. void *ppp;
  403. err =WebRtcIsacfix_AssignSize(&sss);
  404. ppp=malloc(sss);
  405. err =WebRtcIsacfix_Assign(&ISAC_main_inst,ppp);
  406. }
  407. /* Error check */
  408. if (err < 0) {
  409. printf("\n\n Error in create.\n\n");
  410. }
  411. if (testCE == 1) {
  412. err = WebRtcIsacfix_CreateInternal(ISAC_main_inst);
  413. /* Error check */
  414. if (err < 0) {
  415. printf("\n\n Error in createInternal.\n\n");
  416. }
  417. }
  418. }
  419. /* Init of bandwidth data */
  420. BN_data.send_time = 0;
  421. BN_data.arrival_time = 0;
  422. BN_data.sample_count = 0;
  423. BN_data.rtp_number = 0;
  424. /* Initialize encoder and decoder */
  425. framecnt= 0;
  426. endfile = 0;
  427. if (testNum != 1) {
  428. WebRtcIsacfix_EncoderInit(ISAC_main_inst, CodingMode);
  429. }
  430. if (testNum != 2) {
  431. WebRtcIsacfix_DecoderInit(ISAC_main_inst);
  432. }
  433. if (CodingMode == 1) {
  434. err = WebRtcIsacfix_Control(ISAC_main_inst, bottleneck, framesize);
  435. if (err < 0) {
  436. /* exit if returned with error */
  437. errtype=WebRtcIsacfix_GetErrorCode(ISAC_main_inst);
  438. printf("\n\n Error in control: %d.\n\n", errtype);
  439. }
  440. } else if(setControlBWE == 1) {
  441. err = WebRtcIsacfix_ControlBwe(ISAC_main_inst, rateBPS, framesize, fixedFL);
  442. }
  443. if (payloadSize != 0) {
  444. err = WebRtcIsacfix_SetMaxPayloadSize(ISAC_main_inst, payloadSize);
  445. if (err < 0) {
  446. /* exit if returned with error */
  447. errtype=WebRtcIsacfix_GetErrorCode(ISAC_main_inst);
  448. printf("\n\n Error in SetMaxPayloadSize: %d.\n\n", errtype);
  449. exit(EXIT_FAILURE);
  450. }
  451. }
  452. if (payloadRate != 0) {
  453. err = WebRtcIsacfix_SetMaxRate(ISAC_main_inst, payloadRate);
  454. if (err < 0) {
  455. /* exit if returned with error */
  456. errtype=WebRtcIsacfix_GetErrorCode(ISAC_main_inst);
  457. printf("\n\n Error in SetMaxRateInBytes: %d.\n\n", errtype);
  458. exit(EXIT_FAILURE);
  459. }
  460. }
  461. *speechType = 1;
  462. while (endfile == 0) {
  463. if(testNum == 7 && (rand()%2 == 0)) {
  464. err = WebRtcIsacfix_EncoderInit(ISAC_main_inst, CodingMode);
  465. /* Error check */
  466. if (err < 0) {
  467. errtype=WebRtcIsacfix_GetErrorCode(ISAC_main_inst);
  468. printf("\n\n Error in encoderinit: %d.\n\n", errtype);
  469. }
  470. err = WebRtcIsacfix_DecoderInit(ISAC_main_inst);
  471. /* Error check */
  472. if (err < 0) {
  473. errtype=WebRtcIsacfix_GetErrorCode(ISAC_main_inst);
  474. printf("\n\n Error in decoderinit: %d.\n\n", errtype);
  475. }
  476. }
  477. cur_framesmpls = 0;
  478. while (1) {
  479. /* Read 10 ms speech block */
  480. if (nbTest != 1) {
  481. endfile = readframe(shortdata, inp, FRAMESAMPLES_10ms);
  482. } else {
  483. endfile = readframe(shortdata, inp, (FRAMESAMPLES_10ms/2));
  484. }
  485. if (testNum == 7) {
  486. srand(time(NULL));
  487. }
  488. /* iSAC encoding */
  489. if (!(testNum == 3 && framecnt == 0)) {
  490. if (nbTest != 1) {
  491. short bwe;
  492. /* Encode */
  493. stream_len = WebRtcIsacfix_Encode(ISAC_main_inst,
  494. shortdata,
  495. (WebRtc_Word16*)streamdata);
  496. /* If packet is ready, and CE testing, call the different API functions
  497. from the internal API. */
  498. if (stream_len>0) {
  499. if (testCE == 1) {
  500. err = WebRtcIsacfix_ReadBwIndex((WebRtc_Word16*)streamdata, &bwe);
  501. stream_len = WebRtcIsacfix_GetNewBitStream(
  502. ISAC_main_inst,
  503. bwe,
  504. scale,
  505. (WebRtc_Word16*)streamdata);
  506. } else if (testCE == 2) {
  507. /* transcode function not supported */
  508. } else if (testCE == 3) {
  509. /* Only for Function testing. The functions should normally
  510. not be used in this way */
  511. err = WebRtcIsacfix_GetDownLinkBwIndex(ISAC_main_inst, &bwe);
  512. /* Error Check */
  513. if (err < 0) {
  514. errtype=WebRtcIsacfix_GetErrorCode(ISAC_main_inst);
  515. printf("\nError in getSendBWE: %d.\n", errtype);
  516. }
  517. err = WebRtcIsacfix_UpdateUplinkBw(ISAC_main_inst, bwe);
  518. /* Error Check */
  519. if (err < 0) {
  520. errtype=WebRtcIsacfix_GetErrorCode(ISAC_main_inst);
  521. printf("\nError in setBWE: %d.\n", errtype);
  522. }
  523. }
  524. }
  525. } else {
  526. #ifdef WEBRTC_ISAC_FIX_NB_CALLS_ENABLED
  527. stream_len = WebRtcIsacfix_EncodeNb(ISAC_main_inst,
  528. shortdata,
  529. streamdata);
  530. #else
  531. stream_len = -1;
  532. #endif
  533. }
  534. }
  535. else
  536. {
  537. break;
  538. }
  539. if (stream_len < 0 || err < 0) {
  540. /* exit if returned with error */
  541. errtype=WebRtcIsacfix_GetErrorCode(ISAC_main_inst);
  542. printf("\nError in encoder: %d.\n", errtype);
  543. } else {
  544. if (fwrite(streamdata, sizeof(char),
  545. stream_len, outbits) != (size_t)stream_len) {
  546. return -1;
  547. }
  548. }
  549. cur_framesmpls += FRAMESAMPLES_10ms;
  550. /* read next bottleneck rate */
  551. if (f_bn != NULL) {
  552. int aux_var;
  553. if (fscanf(f_bn, "%d", &aux_var) == EOF) {
  554. /* Set pointer to beginning of file */
  555. fseek(f_bn, 0L, SEEK_SET);
  556. if (fscanf(f_bn, "%d", &aux_var) == EOF) {
  557. exit(0);
  558. }
  559. }
  560. bottleneck = (WebRtc_Word16)aux_var;
  561. if (CodingMode == 1) {
  562. WebRtcIsacfix_Control(ISAC_main_inst, bottleneck, framesize);
  563. }
  564. }
  565. /* exit encoder loop if the encoder returned a bitstream */
  566. if (stream_len != 0) break;
  567. }
  568. /* make coded sequence to short be inreasing */
  569. /* the length the decoder expects */
  570. if (testNum == 4) {
  571. stream_len += 10;
  572. }
  573. /* make coded sequence to long be decreasing */
  574. /* the length the decoder expects */
  575. if (testNum == 5) {
  576. stream_len -= 10;
  577. }
  578. if (testNum == 6) {
  579. srand(time(NULL));
  580. for (i = 0; i < stream_len; i++ ) {
  581. streamdata[i] = rand();
  582. }
  583. }
  584. /* set pointer to beginning of file */
  585. if (fp_gns != NULL) {
  586. if (fscanf(fp_gns, "%d", &cur_delay) == EOF) {
  587. fseek(fp_gns, 0L, SEEK_SET);
  588. if (fscanf(fp_gns, "%d", &cur_delay) == EOF) {
  589. exit(0);
  590. }
  591. }
  592. }
  593. /* simulate packet handling through NetEq and the modem */
  594. if (!(testNum == 3 && framecnt == 0)) {
  595. if (gns == 0) {
  596. get_arrival_time(cur_framesmpls, stream_len, bottleneck,
  597. &BN_data);
  598. } else {
  599. get_arrival_time2(cur_framesmpls, cur_delay, &BN_data);
  600. }
  601. }
  602. /* packet not dropped */
  603. if (cur_delay != -1) {
  604. /* Error test number 10, garbage data */
  605. if (testNum == 10) {
  606. for ( i = 0; i < stream_len; i++) {
  607. streamdata[i] = (short) (streamdata[i] + (short) rand());
  608. }
  609. }
  610. if (testNum != 9) {
  611. err = WebRtcIsacfix_UpdateBwEstimate(ISAC_main_inst,
  612. streamdata,
  613. stream_len,
  614. BN_data.rtp_number,
  615. BN_data.send_time,
  616. BN_data.arrival_time);
  617. if (err < 0) {
  618. /* exit if returned with error */
  619. errtype=WebRtcIsacfix_GetErrorCode(ISAC_main_inst);
  620. printf("\nError in decoder: %d.\n", errtype);
  621. }
  622. }
  623. if( readLoss == 1 ) {
  624. if( fread( &lostFrame, sizeof(WebRtc_Word16), 1, plFile ) != 1 ) {
  625. rewind( plFile );
  626. }
  627. lostFrame = !lostFrame;
  628. } else {
  629. lostFrame = (rand()%100 < packetLossPercent);
  630. }
  631. /* iSAC decoding */
  632. if( lostFrame && framecnt > 0) {
  633. if (nbTest !=2) {
  634. declen = WebRtcIsacfix_DecodePlc(ISAC_main_inst,
  635. decoded, prevFrameSize );
  636. } else {
  637. #ifdef WEBRTC_ISAC_FIX_NB_CALLS_ENABLED
  638. declen = WebRtcIsacfix_DecodePlcNb(ISAC_main_inst, decoded,
  639. prevFrameSize );
  640. #else
  641. declen = -1;
  642. #endif
  643. }
  644. lostPackets++;
  645. } else {
  646. if (nbTest !=2 ) {
  647. short FL;
  648. /* Call getFramelen, only used here for function test */
  649. err = WebRtcIsacfix_ReadFrameLen((WebRtc_Word16*)streamdata, &FL);
  650. declen = WebRtcIsacfix_Decode( ISAC_main_inst, streamdata, stream_len,
  651. decoded, speechType );
  652. /* Error check */
  653. if (err<0 || declen<0 || FL!=declen) {
  654. errtype=WebRtcIsacfix_GetErrorCode(ISAC_main_inst);
  655. printf("\nError in decode_B/or getFrameLen: %d.\n", errtype);
  656. }
  657. prevFrameSize = declen/480;
  658. } else {
  659. #ifdef WEBRTC_ISAC_FIX_NB_CALLS_ENABLED
  660. declen = WebRtcIsacfix_DecodeNb( ISAC_main_inst, streamdata,
  661. stream_len, decoded, speechType );
  662. #else
  663. declen = -1;
  664. #endif
  665. prevFrameSize = declen/240;
  666. }
  667. }
  668. if (declen <= 0) {
  669. /* exit if returned with error */
  670. errtype=WebRtcIsacfix_GetErrorCode(ISAC_main_inst);
  671. printf("\nError in decoder: %d.\n", errtype);
  672. }
  673. /* Write decoded speech frame to file */
  674. if (fwrite(decoded, sizeof(WebRtc_Word16),
  675. declen, outp) != (size_t)declen) {
  676. return -1;
  677. }
  678. // fprintf( ratefile, "%f \n", stream_len / ( ((double)declen)/
  679. // ((double)FS) ) * 8 );
  680. } else {
  681. lostPackets++;
  682. }
  683. framecnt++;
  684. totalsmpls += declen;
  685. totalbits += 8 * stream_len;
  686. /* Error test number 10, garbage data */
  687. if (testNum == 10) {
  688. if ( (seedfile = fopen(SEED_FILE, "a+t") ) == NULL ) {
  689. printf( "Error: Could not open file %s\n", SEED_FILE);
  690. }
  691. else {
  692. fprintf(seedfile, "ok\n\n");
  693. fclose(seedfile);
  694. }
  695. }
  696. }
  697. printf("\nLost Frames %d ~ %4.1f%%\n", lostPackets,
  698. (double)lostPackets/(double)framecnt*100.0 );
  699. printf("\n\ntotal bits = %d bits", totalbits);
  700. printf("\nmeasured average bitrate = %0.3f kbits/s",
  701. (double)totalbits *(FS/1000) / totalsmpls);
  702. printf("\n");
  703. /* Runtime statistics */
  704. runtime = (double)(((double)clock()/(double)CLOCKS_PER_SEC)-starttime);
  705. length_file = ((double)framecnt*(double)declen/FS);
  706. printf("\n\nLength of speech file: %.1f s\n", length_file);
  707. printf("Time to run iSAC: %.2f s (%.2f %% of realtime)\n\n",
  708. runtime, (100*runtime/length_file));
  709. printf("\n\n_______________________________________________\n");
  710. fclose(inp);
  711. fclose(outp);
  712. fclose(outbits);
  713. if ( testCE == 1) {
  714. WebRtcIsacfix_FreeInternal(ISAC_main_inst);
  715. }
  716. WebRtcIsacfix_Free(ISAC_main_inst);
  717. return 0;
  718. }