PageRenderTime 82ms CodeModel.GetById 42ms RepoModel.GetById 1ms app.codeStats 1ms

/pjsip_android/apps/pjsip/project/third_party/webrtc/modules/audio_coding/codecs/iSAC/fix/test/kenny.c

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