Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / webrtc / modules / audio_coding / codecs / isac / main / test / simpleKenny.c
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
11 /* kenny.c  - Main function for the iSAC coder */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <time.h>
17
18 #ifdef WIN32
19 #include "windows.h"
20 #define CLOCKS_PER_SEC  1000
21 #endif
22
23 #include <ctype.h>
24 #include <math.h>
25
26 /* include API */
27 #include "isac.h"
28 #include "utility.h"
29 //#include "commonDefs.h"
30
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
36
37 /* sampling frequency (Hz) */
38 #define FS_SWB                               32000
39 #define FS_WB                                16000
40
41 //#define CHANGE_OUTPUT_NAME
42
43 #ifdef HAVE_DEBUG_INFO
44     #include "debugUtility.h"
45     debugStruct debugInfo;
46 #endif
47
48 unsigned long framecnt = 0;
49
50 int main(int argc, char* argv[])
51 {
52     //--- File IO ----
53     FILE* inp;
54     FILE* outp;
55     char inname[500];
56     char outname[500];
57
58     /* Runtime statistics */
59     double        rate;
60     double        rateRCU;
61     unsigned long totalbits = 0;
62     unsigned long totalBitsRCU = 0;
63     unsigned long totalsmpls =0;
64
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];
72     int16_t   payloadLimit;
73     int32_t   rateLimit;
74     ISACStruct*   ISAC_main_inst;
75
76     int16_t   stream_len = 0;
77     int16_t   declen = 0;
78     int16_t   err;
79     int16_t   cur_framesmpls;
80     int           endfile;
81 #ifdef WIN32
82     double        length_file;
83     double        runtime;
84     char          outDrive[10];
85     char          outPath[500];
86     char          outPrefix[500];
87     char          outSuffix[500];
88     char          bitrateFileName[500];
89     FILE*         bitrateFile;
90     double        starttime;
91     double        rateLB = 0;
92     double        rateUB = 0;
93 #endif
94     FILE*         histFile;
95     FILE*         averageFile;
96     int           sampFreqKHz;
97     int           samplesIn10Ms;
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;
109         int onlyEncode;
110         int onlyDecode;
111
112
113     BottleNeckModel packetData;
114         packetData.arrival_time  = 0;
115         packetData.sample_count  = 0;
116         packetData.rtp_number    = 0;
117     memset(hist, 0, sizeof(hist));
118
119     /* handling wrong input arguments in the command line */
120     if(argc < 5)
121     {
122                 int size;
123                 WebRtcIsac_AssignSize(&size);
124
125         printf("\n\nWrong number of arguments or flag values.\n\n");
126
127         printf("Usage:\n\n");
128         printf("%s infile outfile -bn bottelneck [options] \n\n", argv[0]);
129         printf("with:\n");
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");
135         printf("OPTIONS\n");
136         printf("-------\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");
147
148         printf("\n");
149         printf("Example usage:\n\n");
150         printf("%s speechIn.pcm speechOut.pcm -B 40000 -fs 32 \n\n", argv[0]);
151
152                 printf("structure size %d bytes\n", size);
153
154         exit(0);
155     }
156
157
158
159     /* Get Bottleneck value */
160     bottleneck = readParamInt(argc, argv, "-bn", 50000);
161     fprintf(stderr,"\nfixed bottleneck rate of %d bits/s\n\n", bottleneck);
162
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)
169     {
170         histFile = fopen(histFileName, "a");
171         if(histFile == NULL)
172         {
173             printf("cannot open hist file %s", histFileName);
174             exit(0);
175         }
176     }
177     else
178     {
179         // NO recording of hitstogram
180         histFile = NULL;
181     }
182
183
184     packetLossPercent = readParamInt(argc, argv, "-ploss", 0);
185
186     if(readParamString(argc, argv, "-ave", averageFileName, 500) > 0)
187     {
188         averageFile = fopen(averageFileName, "a");
189         if(averageFile == NULL)
190         {
191             printf("cannot open file to write rate %s", averageFileName);
192             exit(0);
193         }
194     }
195     else
196     {
197         averageFile = NULL;
198     }
199
200         onlyEncode = readSwitch(argc, argv, "-enc");
201         onlyDecode = readSwitch(argc, argv, "-dec");
202
203
204     switch(sampFreqKHz)
205     {
206     case 16:
207         {
208             samplesIn10Ms = 160;
209             break;
210         }
211     case 32:
212         {
213             samplesIn10Ms = 320;
214             break;
215         }
216     default:
217         printf("A sampling frequency of %d kHz is not supported,\
218 valid values are 8 and 16.\n", sampFreqKHz);
219         exit(-1);
220     }
221     payloadLimit = (int16_t)readParamInt(argc, argv, "-plim", 400);
222     rateLimit = readParamInt(argc, argv, "-rlim", 106800);
223
224     if ((inp = fopen(inname,"rb")) == NULL) {
225         printf("  iSAC: Cannot read file %s.\n", inname);
226         exit(1);
227     }
228     if ((outp = fopen(outname,"wb")) == NULL) {
229         printf("  iSAC: Cannot write file %s.\n", outname);
230         exit(1);
231     }
232
233 #ifdef WIN32
234     _splitpath(outname, outDrive, outPath, outPrefix, outSuffix);
235     _makepath(bitrateFileName, outDrive, outPath, "bitrate", ".txt");
236
237     bitrateFile = fopen(bitrateFileName, "a");
238     fprintf(bitrateFile, "%  %%s  \n", inname);
239 #endif
240
241     printf("\n");
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);
248     printf("\n");
249
250 #ifdef WIN32
251     starttime = clock()/(double)CLOCKS_PER_SEC; /* Runtime statistics */
252 #endif
253
254     /* Initialize the ISAC and BN structs */
255     err = WebRtcIsac_Create(&ISAC_main_inst);
256
257     WebRtcIsac_SetEncSampRate(ISAC_main_inst, sampFreqKHz * 1000);
258     WebRtcIsac_SetDecSampRate(ISAC_main_inst, sampFreqKHz >= 32 ? 32000 :
259         16000);
260     /* Error check */
261     if (err < 0) {
262         fprintf(stderr,"\n\n Error in create.\n\n");
263         exit(EXIT_FAILURE);
264     }
265
266     framecnt = 0;
267     endfile     = 0;
268
269     /* Initialize encoder and decoder */
270     if(WebRtcIsac_EncoderInit(ISAC_main_inst, codingMode) < 0)
271     {
272         printf("cannot initialize encoder\n");
273         return -1;
274     }
275     if(WebRtcIsac_DecoderInit(ISAC_main_inst) < 0)
276     {
277         printf("cannot initialize decoder\n");
278         return -1;
279     }
280
281     //{
282     //    int32_t b1, b2;
283     //    FILE* fileID = fopen("GetBNTest.txt", "w");
284     //    b2 = 32100;
285     //    while(b2 <= 52000)
286     //    {
287     //        WebRtcIsac_Control(ISAC_main_inst, b2, frameSize);
288     //        WebRtcIsac_GetUplinkBw(ISAC_main_inst, &b1);
289     //        fprintf(fileID, "%5d %5d\n", b2, b1);
290     //        b2 += 10;
291     //    }
292     //}
293
294     if(codingMode == 1)
295     {
296         if(WebRtcIsac_Control(ISAC_main_inst, bottleneck, frameSize) < 0)
297         {
298             printf("cannot set bottleneck\n");
299             return -1;
300         }
301     }
302     else
303     {
304         if(WebRtcIsac_ControlBwe(ISAC_main_inst, 15000, 30, 1) < 0)
305         {
306             printf("cannot configure BWE\n");
307             return -1;
308         }
309     }
310
311     if(WebRtcIsac_SetMaxPayloadSize(ISAC_main_inst, payloadLimit) < 0)
312     {
313         printf("cannot set maximum payload size %d.\n", payloadLimit);
314         return -1;
315     }
316
317     if (rateLimit < 106800) {
318         if(WebRtcIsac_SetMaxRate(ISAC_main_inst, rateLimit) < 0)
319         {
320             printf("cannot set the maximum rate %d.\n", rateLimit);
321             return -1;
322         }
323     }
324
325     //=====================================
326 //#ifdef HAVE_DEBUG_INFO
327 //    if(setupDebugStruct(&debugInfo) < 0)
328 //    {
329 //        exit(1);
330 //    }
331 //#endif
332
333     while (endfile == 0)
334     {
335         fprintf(stderr,"  \rframe = %7li", framecnt);
336
337         //============== Readind from the file and encoding =================
338         cur_framesmpls = 0;
339         stream_len = 0;
340
341
342                 if(onlyDecode)
343                 {
344                         uint8_t auxUW8;
345                         size_t auxSizet;
346                         if(fread(&auxUW8, sizeof(uint8_t), 1, inp) < 1)
347                         {
348                                 break;
349                         }
350                         stream_len = ((uint8_t)auxUW8) << 8;
351                         if(fread(&auxUW8, sizeof(uint8_t), 1, inp) < 1)
352                         {
353                                 break;
354                         }
355                         stream_len |= (uint16_t)auxUW8;
356                         auxSizet = (size_t)stream_len;
357                         if(fread(payload, 1, auxSizet, inp) < auxSizet)
358                         {
359                                 printf("last payload is corrupted\n");
360                                 break;
361                         }
362                 }
363                 else
364                 {
365                         while(stream_len == 0)
366                         {
367                                 // Read 10 ms speech block
368                                 endfile = readframe(shortdata, inp, samplesIn10Ms);
369                                 if(endfile)
370                                 {
371                                         break;
372                                 }
373                                 cur_framesmpls += samplesIn10Ms;
374
375                                 //-------- iSAC encoding ---------
376                                 stream_len = WebRtcIsac_Encode(ISAC_main_inst, shortdata,
377                                         (int16_t*)payload);
378
379                                 if(stream_len < 0)
380                                 {
381                                         // exit if returned with error
382                                         //errType=WebRtcIsac_GetErrorCode(ISAC_main_inst);
383                                         fprintf(stderr,"\nError in encoder\n");
384                                         getchar();
385                                         exit(EXIT_FAILURE);
386                                 }
387
388
389                         }
390                         //===================================================================
391                         if(endfile)
392                         {
393                                 break;
394                         }
395
396                         rcuStreamLen = WebRtcIsac_GetRedPayload(ISAC_main_inst, (int16_t*)payloadRCU);
397
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)
404                         {
405                                 printf(" BWE Error at client\n");
406                                 return -1;
407                         }
408                 }
409
410         if(endfile)
411         {
412             break;
413         }
414
415         maxStreamLen = (stream_len > maxStreamLen)? stream_len:maxStreamLen;
416         packetCntr++;
417
418         hist[stream_len]++;
419         if(averageFile != NULL)
420         {
421             tmpSumStreamLen += stream_len;
422             if(packetCntr == 100)
423             {
424                 // kbps
425                 fprintf(averageFile, "%8.3f ", (double)tmpSumStreamLen * 8.0 / (30.0 * packetCntr));
426                 packetCntr = 0;
427                 tmpSumStreamLen = 0;
428             }
429         }
430
431                 if(onlyEncode)
432                 {
433                   uint8_t auxUW8;
434                   auxUW8 = (uint8_t)(((stream_len & 0x7F00) >> 8) & 0xFF);
435                   if (fwrite(&auxUW8, sizeof(uint8_t), 1, outp) != 1) {
436                     return -1;
437                   }
438
439                   auxUW8 = (uint8_t)(stream_len & 0xFF);
440                   if (fwrite(&auxUW8, sizeof(uint8_t), 1, outp) != 1) {
441                     return -1;
442                   }
443                   if (fwrite(payload, 1, stream_len,
444                              outp) != (size_t)stream_len) {
445                     return -1;
446                   }
447                 }
448                 else
449                 {
450
451                         //======================= iSAC decoding ===========================
452
453                         if((rand() % 100) < packetLossPercent)
454                         {
455                                 declen = WebRtcIsac_DecodeRcu(ISAC_main_inst, payloadRCU,
456                                         rcuStreamLen, decoded, speechType);
457                                 lostPacketCntr++;
458                         }
459                         else
460                         {
461                                 declen = WebRtcIsac_Decode(ISAC_main_inst, payload,
462                                         stream_len, decoded, speechType);
463                         }
464                         if(declen <= 0)
465                         {
466                                 //errType=WebRtcIsac_GetErrorCode(ISAC_main_inst);
467                                 fprintf(stderr,"\nError in decoder.\n");
468                                 getchar();
469                                 exit(1);
470                         }
471
472                         // Write decoded speech frame to file
473                         if (fwrite(decoded, sizeof(int16_t),
474                                    declen, outp) != (size_t)declen) {
475                           return -1;
476                         }
477                         cur_framesmpls = declen;
478                 }
479         // Update Statistics
480         framecnt++;
481         totalsmpls += cur_framesmpls;
482         if(stream_len > 0)
483         {
484             totalbits += 8 * stream_len;
485         }
486         if(rcuStreamLen > 0)
487         {
488             totalBitsRCU += 8 * rcuStreamLen;
489         }
490     }
491
492     rate =    ((double)totalbits    * (sampFreqKHz)) / (double)totalsmpls;
493     rateRCU = ((double)totalBitsRCU * (sampFreqKHz)) / (double)totalsmpls;
494
495     printf("\n\n");
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);
499
500 #ifdef WIN32
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;
506 #endif
507
508     fprintf(bitrateFile, "%d  %10u     %d     %6.3f  %6.3f    %6.3f\n",
509         sampFreqKHz,
510         framecnt,
511         bottleneck,
512         rateLB,
513         rateUB,
514         rate);
515     fclose(bitrateFile);
516 #endif   // WIN32
517
518     printf("\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);
525
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));
531 //
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);
536 //#endif
537
538     printf("\n");
539
540     /* Runtime statistics */
541 #ifdef WIN32
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));
547 #endif
548     printf("\n\n_______________________________________________\n");
549
550     if(histFile != NULL)
551     {
552         int n;
553         for(n = 0; n < 600; n++)
554         {
555             fprintf(histFile, "%6d ", hist[n]);
556         }
557         fprintf(histFile, "\n");
558         fclose(histFile);
559     }
560     if(averageFile != NULL)
561     {
562         if(packetCntr > 0)
563         {
564             fprintf(averageFile, "%8.3f ", (double)tmpSumStreamLen * 8.0 / (30.0 * packetCntr));
565         }
566         fprintf(averageFile, "\n");
567         fclose(averageFile);
568     }
569
570     fclose(inp);
571     fclose(outp);
572
573     WebRtcIsac_Free(ISAC_main_inst);
574
575
576 #ifdef CHANGE_OUTPUT_NAME
577     {
578         char* p;
579         char myExt[50];
580         char bitRateStr[10];
581         char newOutName[500];
582         strcpy(newOutName, outname);
583
584         myExt[0] = '\0';
585         p = strchr(newOutName, '.');
586         if(p != NULL)
587         {
588             strcpy(myExt, p);
589             *p = '_';
590             p++;
591             *p = '\0';
592         }
593         else
594         {
595             strcat(newOutName, "_");
596         }
597         sprintf(bitRateStr, "%0.0fkbps", rate);
598         strcat(newOutName, bitRateStr);
599         strcat(newOutName, myExt);
600         rename(outname, newOutName);
601     }
602 #endif
603     exit(0);
604 }
605
606
607 #ifdef HAVE_DEBUG_INFO
608 int setupDebugStruct(debugStruct* str)
609 {
610     str->prevPacketLost = 0;
611     str->currPacketLost = 0;
612
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");
617
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");
622
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");
627
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");
634
635     OPEN_FILE_WB(str->arrivalTime,        NULL/*"ArivalTime.dat"*/);
636     str->lastArrivalTime = 0;
637
638     str->maxPayloadLB = 0;
639     str->maxPayloadUB = 0;
640     str->lbBytes = 0;
641     str->ubBytes = 0;
642
643     return 0;
644 };
645 #endif