2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
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.
11 /* kenny.c - Main function for the iSAC coder */
20 #define CLOCKS_PER_SEC 1000
29 //#include "commonDefs.h"
31 /* max number of samples per frame (= 60 ms frame) */
32 #define MAX_FRAMESAMPLES_SWB 1920
33 /* number of samples per 10ms frame */
34 #define FRAMESAMPLES_SWB_10ms 320
35 #define FRAMESAMPLES_WB_10ms 160
37 /* sampling frequency (Hz) */
41 //#define CHANGE_OUTPUT_NAME
43 #ifdef HAVE_DEBUG_INFO
44 #include "debugUtility.h"
45 debugStruct debugInfo;
48 unsigned long framecnt = 0;
50 int main(int argc, char* argv[])
58 /* Runtime statistics */
61 unsigned long totalbits = 0;
62 unsigned long totalBitsRCU = 0;
63 unsigned long totalsmpls =0;
65 int32_t bottleneck = 39;
66 int16_t frameSize = 30; /* ms */
67 int16_t codingMode = 1;
68 int16_t shortdata[FRAMESAMPLES_SWB_10ms];
69 int16_t decoded[MAX_FRAMESAMPLES_SWB];
70 //uint16_t streamdata[1000];
71 int16_t speechType[1];
74 ISACStruct* ISAC_main_inst;
76 int16_t stream_len = 0;
79 int16_t cur_framesmpls;
88 char bitrateFileName[500];
98 int16_t maxStreamLen = 0;
99 char histFileName[500];
100 char averageFileName[500];
101 unsigned int hist[600];
102 unsigned int tmpSumStreamLen = 0;
103 unsigned int packetCntr = 0;
104 unsigned int lostPacketCntr = 0;
105 uint16_t payload[600];
106 uint16_t payloadRCU[600];
107 uint16_t packetLossPercent = 0;
108 int16_t rcuStreamLen = 0;
113 BottleNeckModel packetData;
114 packetData.arrival_time = 0;
115 packetData.sample_count = 0;
116 packetData.rtp_number = 0;
117 memset(hist, 0, sizeof(hist));
119 /* handling wrong input arguments in the command line */
123 WebRtcIsac_AssignSize(&size);
125 printf("\n\nWrong number of arguments or flag values.\n\n");
127 printf("Usage:\n\n");
128 printf("%s infile outfile -bn bottelneck [options] \n\n", argv[0]);
130 printf("-I................... indicates encoding in instantaneous mode.\n");
131 printf("-bn bottleneck....... the value of the bottleneck in bit/sec, e.g. 39742,\n");
132 printf(" in instantaneous (channel-independent) mode.\n\n");
133 printf("infile............... Normal speech input file\n\n");
134 printf("outfile.............. Speech output file\n\n");
137 printf("-fs sampFreq......... sampling frequency of codec 16 or 32 (default) kHz.\n");
138 printf("-plim payloadLim..... payload limit in bytes,\n");
139 printf(" default is the maximum possible.\n");
140 printf("-rlim rateLim........ rate limit in bits/sec, \n");
141 printf(" default is the maimum possible.\n");
142 printf("-h file.............. record histogram and *append* to 'file'.\n");
143 printf("-ave file............ record average rate of 3 sec intervales and *append* to 'file'.\n");
144 printf("-ploss............... packet-loss percentage.\n");
145 printf("-enc................. do only encoding and store the bit-stream\n");
146 printf("-dec................. the input file is a bit-stream, decode it.\n");
149 printf("Example usage:\n\n");
150 printf("%s speechIn.pcm speechOut.pcm -B 40000 -fs 32 \n\n", argv[0]);
152 printf("structure size %d bytes\n", size);
159 /* Get Bottleneck value */
160 bottleneck = readParamInt(argc, argv, "-bn", 50000);
161 fprintf(stderr,"\nfixed bottleneck rate of %d bits/s\n\n", bottleneck);
163 /* Get Input and Output files */
164 sscanf(argv[1], "%s", inname);
165 sscanf(argv[2], "%s", outname);
166 codingMode = readSwitch(argc, argv, "-I");
167 sampFreqKHz = (int16_t)readParamInt(argc, argv, "-fs", 32);
168 if(readParamString(argc, argv, "-h", histFileName, 500) > 0)
170 histFile = fopen(histFileName, "a");
173 printf("cannot open hist file %s", histFileName);
179 // NO recording of hitstogram
184 packetLossPercent = readParamInt(argc, argv, "-ploss", 0);
186 if(readParamString(argc, argv, "-ave", averageFileName, 500) > 0)
188 averageFile = fopen(averageFileName, "a");
189 if(averageFile == NULL)
191 printf("cannot open file to write rate %s", averageFileName);
200 onlyEncode = readSwitch(argc, argv, "-enc");
201 onlyDecode = readSwitch(argc, argv, "-dec");
217 printf("A sampling frequency of %d kHz is not supported,\
218 valid values are 8 and 16.\n", sampFreqKHz);
221 payloadLimit = (int16_t)readParamInt(argc, argv, "-plim", 400);
222 rateLimit = readParamInt(argc, argv, "-rlim", 106800);
224 if ((inp = fopen(inname,"rb")) == NULL) {
225 printf(" iSAC: Cannot read file %s.\n", inname);
228 if ((outp = fopen(outname,"wb")) == NULL) {
229 printf(" iSAC: Cannot write file %s.\n", outname);
234 _splitpath(outname, outDrive, outPath, outPrefix, outSuffix);
235 _makepath(bitrateFileName, outDrive, outPath, "bitrate", ".txt");
237 bitrateFile = fopen(bitrateFileName, "a");
238 fprintf(bitrateFile, "% %%s \n", inname);
242 printf("Input.................... %s\n", inname);
243 printf("Output................... %s\n", outname);
244 printf("Encoding Mode............ %s\n",
245 (codingMode == 1)? "Channel-Independent":"Channel-Adaptive");
246 printf("Bottleneck............... %d bits/sec\n", bottleneck);
247 printf("Packet-loss Percentage... %d\n", packetLossPercent);
251 starttime = clock()/(double)CLOCKS_PER_SEC; /* Runtime statistics */
254 /* Initialize the ISAC and BN structs */
255 err = WebRtcIsac_Create(&ISAC_main_inst);
257 WebRtcIsac_SetEncSampRate(ISAC_main_inst, sampFreqKHz * 1000);
258 WebRtcIsac_SetDecSampRate(ISAC_main_inst, sampFreqKHz >= 32 ? 32000 :
262 fprintf(stderr,"\n\n Error in create.\n\n");
269 /* Initialize encoder and decoder */
270 if(WebRtcIsac_EncoderInit(ISAC_main_inst, codingMode) < 0)
272 printf("cannot initialize encoder\n");
275 if(WebRtcIsac_DecoderInit(ISAC_main_inst) < 0)
277 printf("cannot initialize decoder\n");
283 // FILE* fileID = fopen("GetBNTest.txt", "w");
285 // while(b2 <= 52000)
287 // WebRtcIsac_Control(ISAC_main_inst, b2, frameSize);
288 // WebRtcIsac_GetUplinkBw(ISAC_main_inst, &b1);
289 // fprintf(fileID, "%5d %5d\n", b2, b1);
296 if(WebRtcIsac_Control(ISAC_main_inst, bottleneck, frameSize) < 0)
298 printf("cannot set bottleneck\n");
304 if(WebRtcIsac_ControlBwe(ISAC_main_inst, 15000, 30, 1) < 0)
306 printf("cannot configure BWE\n");
311 if(WebRtcIsac_SetMaxPayloadSize(ISAC_main_inst, payloadLimit) < 0)
313 printf("cannot set maximum payload size %d.\n", payloadLimit);
317 if (rateLimit < 106800) {
318 if(WebRtcIsac_SetMaxRate(ISAC_main_inst, rateLimit) < 0)
320 printf("cannot set the maximum rate %d.\n", rateLimit);
325 //=====================================
326 //#ifdef HAVE_DEBUG_INFO
327 // if(setupDebugStruct(&debugInfo) < 0)
335 fprintf(stderr," \rframe = %7li", framecnt);
337 //============== Readind from the file and encoding =================
346 if(fread(&auxUW8, sizeof(uint8_t), 1, inp) < 1)
350 stream_len = ((uint8_t)auxUW8) << 8;
351 if(fread(&auxUW8, sizeof(uint8_t), 1, inp) < 1)
355 stream_len |= (uint16_t)auxUW8;
356 auxSizet = (size_t)stream_len;
357 if(fread(payload, 1, auxSizet, inp) < auxSizet)
359 printf("last payload is corrupted\n");
365 while(stream_len == 0)
367 // Read 10 ms speech block
368 endfile = readframe(shortdata, inp, samplesIn10Ms);
373 cur_framesmpls += samplesIn10Ms;
375 //-------- iSAC encoding ---------
376 stream_len = WebRtcIsac_Encode(ISAC_main_inst, shortdata,
381 // exit if returned with error
382 //errType=WebRtcIsac_GetErrorCode(ISAC_main_inst);
383 fprintf(stderr,"\nError in encoder\n");
390 //===================================================================
396 rcuStreamLen = WebRtcIsac_GetRedPayload(ISAC_main_inst, (int16_t*)payloadRCU);
398 get_arrival_time(cur_framesmpls, stream_len, bottleneck, &packetData,
399 sampFreqKHz * 1000, sampFreqKHz * 1000);
400 if(WebRtcIsac_UpdateBwEstimate(ISAC_main_inst,
401 payload, stream_len, packetData.rtp_number,
402 packetData.sample_count,
403 packetData.arrival_time) < 0)
405 printf(" BWE Error at client\n");
415 maxStreamLen = (stream_len > maxStreamLen)? stream_len:maxStreamLen;
419 if(averageFile != NULL)
421 tmpSumStreamLen += stream_len;
422 if(packetCntr == 100)
425 fprintf(averageFile, "%8.3f ", (double)tmpSumStreamLen * 8.0 / (30.0 * packetCntr));
434 auxUW8 = (uint8_t)(((stream_len & 0x7F00) >> 8) & 0xFF);
435 if (fwrite(&auxUW8, sizeof(uint8_t), 1, outp) != 1) {
439 auxUW8 = (uint8_t)(stream_len & 0xFF);
440 if (fwrite(&auxUW8, sizeof(uint8_t), 1, outp) != 1) {
443 if (fwrite(payload, 1, stream_len,
444 outp) != (size_t)stream_len) {
451 //======================= iSAC decoding ===========================
453 if((rand() % 100) < packetLossPercent)
455 declen = WebRtcIsac_DecodeRcu(ISAC_main_inst, payloadRCU,
456 rcuStreamLen, decoded, speechType);
461 declen = WebRtcIsac_Decode(ISAC_main_inst, payload,
462 stream_len, decoded, speechType);
466 //errType=WebRtcIsac_GetErrorCode(ISAC_main_inst);
467 fprintf(stderr,"\nError in decoder.\n");
472 // Write decoded speech frame to file
473 if (fwrite(decoded, sizeof(int16_t),
474 declen, outp) != (size_t)declen) {
477 cur_framesmpls = declen;
481 totalsmpls += cur_framesmpls;
484 totalbits += 8 * stream_len;
488 totalBitsRCU += 8 * rcuStreamLen;
492 rate = ((double)totalbits * (sampFreqKHz)) / (double)totalsmpls;
493 rateRCU = ((double)totalBitsRCU * (sampFreqKHz)) / (double)totalsmpls;
496 printf("Sampling Rate......................... %d kHz\n", sampFreqKHz);
497 printf("Payload Limit......................... %d bytes \n", payloadLimit);
498 printf("Rate Limit............................ %d bits/sec \n", rateLimit);
501 #ifdef HAVE_DEBUG_INFO
502 rateLB = ((double)debugInfo.lbBytes * 8. *
503 (sampFreqKHz)) / (double)totalsmpls;
504 rateUB = ((double)debugInfo.ubBytes * 8. *
505 (sampFreqKHz)) / (double)totalsmpls;
508 fprintf(bitrateFile, "%d %10u %d %6.3f %6.3f %6.3f\n",
519 printf("Measured bit-rate..................... %0.3f kbps\n", rate);
520 printf("Measured RCU bit-ratre................ %0.3f kbps\n", rateRCU);
521 printf("Maximum bit-rate/payloadsize.......... %0.3f / %d\n",
522 maxStreamLen * 8 / 0.03, maxStreamLen);
523 printf("Measured packet-loss.................. %0.1f%% \n",
524 100.0f * (float)lostPacketCntr / (float)packetCntr);
526 //#ifdef HAVE_DEBUG_INFO
527 // printf("Measured lower-band bit-rate.......... %0.3f kbps (%.0f%%)\n",
528 // rateLB, (double)(rateLB) * 100. /(double)(rate));
529 // printf("Measured upper-band bit-rate.......... %0.3f kbps (%.0f%%)\n",
530 // rateUB, (double)(rateUB) * 100. /(double)(rate));
532 // printf("Maximum payload lower-band............ %d bytes (%0.3f kbps)\n",
533 // debugInfo.maxPayloadLB, debugInfo.maxPayloadLB * 8.0 / 0.03);
534 // printf("Maximum payload upper-band............ %d bytes (%0.3f kbps)\n",
535 // debugInfo.maxPayloadUB, debugInfo.maxPayloadUB * 8.0 / 0.03);
540 /* Runtime statistics */
542 runtime = (double)(clock()/(double)CLOCKS_PER_SEC-starttime);
543 length_file = ((double)framecnt*(double)declen/(sampFreqKHz*1000));
544 printf("Length of speech file................ %.1f s\n", length_file);
545 printf("Time to run iSAC..................... %.2f s (%.2f %% of realtime)\n\n",
546 runtime, (100*runtime/length_file));
548 printf("\n\n_______________________________________________\n");
553 for(n = 0; n < 600; n++)
555 fprintf(histFile, "%6d ", hist[n]);
557 fprintf(histFile, "\n");
560 if(averageFile != NULL)
564 fprintf(averageFile, "%8.3f ", (double)tmpSumStreamLen * 8.0 / (30.0 * packetCntr));
566 fprintf(averageFile, "\n");
573 WebRtcIsac_Free(ISAC_main_inst);
576 #ifdef CHANGE_OUTPUT_NAME
581 char newOutName[500];
582 strcpy(newOutName, outname);
585 p = strchr(newOutName, '.');
595 strcat(newOutName, "_");
597 sprintf(bitRateStr, "%0.0fkbps", rate);
598 strcat(newOutName, bitRateStr);
599 strcat(newOutName, myExt);
600 rename(outname, newOutName);
607 #ifdef HAVE_DEBUG_INFO
608 int setupDebugStruct(debugStruct* str)
610 str->prevPacketLost = 0;
611 str->currPacketLost = 0;
613 OPEN_FILE_WB(str->res0to4FilePtr, "Res0to4.dat");
614 OPEN_FILE_WB(str->res4to8FilePtr, "Res4to8.dat");
615 OPEN_FILE_WB(str->res8to12FilePtr, "Res8to12.dat");
616 OPEN_FILE_WB(str->res8to16FilePtr, "Res8to16.dat");
618 OPEN_FILE_WB(str->res0to4DecFilePtr, "Res0to4Dec.dat");
619 OPEN_FILE_WB(str->res4to8DecFilePtr, "Res4to8Dec.dat");
620 OPEN_FILE_WB(str->res8to12DecFilePtr, "Res8to12Dec.dat");
621 OPEN_FILE_WB(str->res8to16DecFilePtr, "Res8to16Dec.dat");
623 OPEN_FILE_WB(str->in0to4FilePtr, "in0to4.dat");
624 OPEN_FILE_WB(str->in4to8FilePtr, "in4to8.dat");
625 OPEN_FILE_WB(str->in8to12FilePtr, "in8to12.dat");
626 OPEN_FILE_WB(str->in8to16FilePtr, "in8to16.dat");
628 OPEN_FILE_WB(str->out0to4FilePtr, "out0to4.dat");
629 OPEN_FILE_WB(str->out4to8FilePtr, "out4to8.dat");
630 OPEN_FILE_WB(str->out8to12FilePtr, "out8to12.dat");
631 OPEN_FILE_WB(str->out8to16FilePtr, "out8to16.dat");
632 OPEN_FILE_WB(str->fftFilePtr, "riFFT.dat");
633 OPEN_FILE_WB(str->fftDecFilePtr, "riFFTDec.dat");
635 OPEN_FILE_WB(str->arrivalTime, NULL/*"ArivalTime.dat"*/);
636 str->lastArrivalTime = 0;
638 str->maxPayloadLB = 0;
639 str->maxPayloadUB = 0;