diff --git a/webrtc/modules/audio_coding/codecs/isac/fix/test/test_iSACfixfloat.c b/webrtc/modules/audio_coding/codecs/isac/fix/test/test_iSACfixfloat.c index 965f2bc1fe..9aabd04a95 100644 --- a/webrtc/modules/audio_coding/codecs/isac/fix/test/test_iSACfixfloat.c +++ b/webrtc/modules/audio_coding/codecs/isac/fix/test/test_iSACfixfloat.c @@ -22,677 +22,614 @@ #include "isac.h" #include "isacfix.h" - /* max number of samples per frame (= 60 ms frame) */ -#define MAX_FRAMESAMPLES 960 +#define MAX_FRAMESAMPLES 960 /* number of samples per 10ms frame */ -#define FRAMESAMPLES_10ms 160 +#define FRAMESAMPLES_10ms 160 /* sampling frequency (Hz) */ -#define FS 16000 - - +#define FS 16000 /* Runtime statistics */ #include -#define CLOCKS_PER_SEC 1000 - - +#define CLOCKS_PER_SEC 1000 // FILE *histfile, *ratefile; - /* function for reading audio data from PCM file */ -int readframe(int16_t *data, FILE *inp, int length) { - - short k, rlen, status = 0; - - rlen = fread(data, sizeof(int16_t), length, inp); - if (rlen < length) { - for (k = rlen; k < length; k++) - data[k] = 0; - status = 1; - } - - return status; +int readframe(int16_t* data, FILE* inp, int length) { + short k, rlen, status = 0; + + rlen = fread(data, sizeof(int16_t), length, inp); + if (rlen < length) { + for (k = rlen; k < length; k++) + data[k] = 0; + status = 1; + } + + return status; } typedef struct { - uint32_t send_time; /* samples */ - uint32_t arrival_time; /* samples */ - uint32_t sample_count; /* samples */ - uint16_t rtp_number; + uint32_t send_time; /* samples */ + uint32_t arrival_time; /* samples */ + uint32_t sample_count; /* samples */ + uint16_t rtp_number; } BottleNeckModel; -void get_arrival_time(int current_framesamples, /* samples */ - int packet_size, /* bytes */ - int bottleneck, /* excluding headers; bits/s */ - BottleNeckModel *BN_data) -{ - const int HeaderSize = 35; - int HeaderRate; +void get_arrival_time(int current_framesamples, /* samples */ + int packet_size, /* bytes */ + int bottleneck, /* excluding headers; bits/s */ + BottleNeckModel* BN_data) { + const int HeaderSize = 35; + int HeaderRate; - HeaderRate = HeaderSize * 8 * FS / current_framesamples; /* bits/s */ + HeaderRate = HeaderSize * 8 * FS / current_framesamples; /* bits/s */ - /* everything in samples */ - BN_data->sample_count = BN_data->sample_count + current_framesamples; + /* everything in samples */ + BN_data->sample_count = BN_data->sample_count + current_framesamples; - BN_data->arrival_time += ((packet_size + HeaderSize) * 8 * FS) / (bottleneck + HeaderRate); - BN_data->send_time += current_framesamples; + BN_data->arrival_time += + ((packet_size + HeaderSize) * 8 * FS) / (bottleneck + HeaderRate); + BN_data->send_time += current_framesamples; - if (BN_data->arrival_time < BN_data->sample_count) - BN_data->arrival_time = BN_data->sample_count; + if (BN_data->arrival_time < BN_data->sample_count) + BN_data->arrival_time = BN_data->sample_count; - BN_data->rtp_number++; + BN_data->rtp_number++; } +int main(int argc, char* argv[]) { + char inname[50], outname[50], bottleneck_file[50], bitfilename[60], + bitending[10] = "_bits.pcm"; + FILE* inp, *outp, *f_bn, *bitsp; + int framecnt, endfile; + int i, j, errtype, plc = 0; + int16_t CodingMode; + int16_t bottleneck; -int main(int argc, char* argv[]) -{ + int16_t framesize = 30; /* ms */ + // int16_t framesize = 60; /* To invoke cisco complexity case at frame 2252 */ - char inname[50], outname[50], bottleneck_file[50], bitfilename[60], bitending[10]="_bits.pcm"; - FILE *inp, *outp, *f_bn, *bitsp; - int framecnt, endfile; + int cur_framesmpls, err; + /* Runtime statistics */ + double starttime; + double runtime; + double length_file; - int i,j,errtype, plc=0; - int16_t CodingMode; - int16_t bottleneck; + int16_t stream_len = 0; + int16_t declen; - int16_t framesize = 30; /* ms */ - //int16_t framesize = 60; /* To invoke cisco complexity case at frame 2252 */ - - int cur_framesmpls, err; - - /* Runtime statistics */ - double starttime; - double runtime; - double length_file; - - int16_t stream_len = 0; - int16_t declen; - - int16_t shortdata[FRAMESAMPLES_10ms]; - int16_t decoded[MAX_FRAMESAMPLES]; - uint16_t streamdata[600]; - int16_t speechType[1]; - -// int16_t *iSACstruct; + int16_t shortdata[FRAMESAMPLES_10ms]; + int16_t decoded[MAX_FRAMESAMPLES]; + uint16_t streamdata[600]; + int16_t speechType[1]; - char version_number[20]; - int mode=-1, tmp, nbTest=0; /*,sss;*/ + // int16_t *iSACstruct; + + char version_number[20]; + int mode = -1, tmp, nbTest = 0; /*,sss;*/ #ifdef _DEBUG - FILE *fy; - double kbps; - int totalbits =0; - int totalsmpls =0; + FILE* fy; + double kbps; + int totalbits = 0; + int totalsmpls = 0; #endif /* _DEBUG */ + /* only one structure used for ISAC encoder */ + ISAC_MainStruct* ISAC_main_inst; + ISACFIX_MainStruct* ISACFIX_main_inst; - - - /* only one structure used for ISAC encoder */ - ISAC_MainStruct *ISAC_main_inst; - ISACFIX_MainStruct *ISACFIX_main_inst; - - BottleNeckModel BN_data; - f_bn = NULL; + BottleNeckModel BN_data; + f_bn = NULL; #ifdef _DEBUG - fy = fopen("bit_rate.dat", "w"); - fclose(fy); - fy = fopen("bytes_frames.dat", "w"); - fclose(fy); + fy = fopen("bit_rate.dat", "w"); + fclose(fy); + fy = fopen("bytes_frames.dat", "w"); + fclose(fy); #endif /* _DEBUG */ + // histfile = fopen("histo.dat", "ab"); + // ratefile = fopen("rates.dat", "ab"); -//histfile = fopen("histo.dat", "ab"); -//ratefile = fopen("rates.dat", "ab"); + /* handling wrong input arguments in the command line */ + if ((argc < 6) || (argc > 10)) { + printf("\n\nWrong number of arguments or flag values.\n\n"); - /* handling wrong input arguments in the command line */ - if ((argc<6) || (argc>10)) { - printf("\n\nWrong number of arguments or flag values.\n\n"); + printf("\n"); + WebRtcIsacfix_version(version_number); + printf("iSAC version %s \n\n", version_number); - printf("\n"); - WebRtcIsacfix_version(version_number); - printf("iSAC version %s \n\n", version_number); + printf("Usage:\n\n"); + printf("./kenny.exe [-I] bottleneck_value infile outfile \n\n"); + printf("with:\n"); - printf("Usage:\n\n"); - printf("./kenny.exe [-I] bottleneck_value infile outfile \n\n"); - printf("with:\n"); + printf( + "[-I] : if -I option is specified, the coder " + "will use\n"); + printf( + " an instantaneous Bottleneck value. If " + "not, it\n"); + printf( + " will be an adaptive Bottleneck " + "value.\n\n"); + printf( + "bottleneck_value : the value of the bottleneck provided " + "either\n"); + printf( + " as a fixed value (e.g. 25000) or\n"); + printf( + " read from a file (e.g. " + "bottleneck.txt)\n\n"); + printf("[-m] mode : Mode (encoder - decoder):\n"); + printf( + " : 0 - float - float \n"); + printf( + " : 1 - float - fix \n"); + printf( + " : 2 - fix - float \n"); + printf(" : 3 - fix - fix \n"); + printf("[-PLC] : Test PLC packetlosses\n"); + printf( + "[-NB] num : Test NB interfaces, num=1 encNB, num=2 " + "decNB\n"); + printf("infile : Normal speech input file\n\n"); + printf("outfile : Speech output file\n\n"); + printf("Example usage:\n\n"); + printf("./kenny.exe -I bottleneck.txt -m 1 speechIn.pcm speechOut.pcm\n\n"); + exit(0); + } - printf("[-I] : if -I option is specified, the coder will use\n"); - printf(" an instantaneous Bottleneck value. If not, it\n"); - printf(" will be an adaptive Bottleneck value.\n\n"); - printf("bottleneck_value : the value of the bottleneck provided either\n"); - printf(" as a fixed value (e.g. 25000) or\n"); - printf(" read from a file (e.g. bottleneck.txt)\n\n"); - printf("[-m] mode : Mode (encoder - decoder):\n"); - printf(" : 0 - float - float \n"); - printf(" : 1 - float - fix \n"); - printf(" : 2 - fix - float \n"); - printf(" : 3 - fix - fix \n"); - printf("[-PLC] : Test PLC packetlosses\n"); - printf("[-NB] num : Test NB interfaces, num=1 encNB, num=2 decNB\n"); - printf("infile : Normal speech input file\n\n"); - printf("outfile : Speech output file\n\n"); - printf("Example usage:\n\n"); - printf("./kenny.exe -I bottleneck.txt -m 1 speechIn.pcm speechOut.pcm\n\n"); - exit(0); + printf("--------------------START---------------------\n\n"); + WebRtcIsac_version(version_number); + printf("iSAC FLOAT version %s \n", version_number); + WebRtcIsacfix_version(version_number); + printf("iSAC FIX version %s \n\n", version_number); - } - - - printf("--------------------START---------------------\n\n"); - WebRtcIsac_version(version_number); - printf("iSAC FLOAT version %s \n", version_number); - WebRtcIsacfix_version(version_number); - printf("iSAC FIX version %s \n\n", version_number); + CodingMode = 0; + tmp = 1; + for (i = 1; i < argc; i++) { + if (!strcmp("-I", argv[i])) { + printf("\nInstantaneous BottleNeck\n"); + CodingMode = 1; + i++; + tmp = 0; + } - CodingMode = 0; - tmp=1; - for (i = 1; i < argc;i++) - { - if (!strcmp ("-I", argv[i])) - { - printf("\nInstantaneous BottleNeck\n"); - CodingMode = 1; - i++; - tmp=0; - } + if (!strcmp("-m", argv[i])) { + mode = atoi(argv[i + 1]); + i++; + } - if (!strcmp ("-m", argv[i])) { - mode=atoi(argv[i+1]); - i++; - } + if (!strcmp("-PLC", argv[i])) { + plc = 1; + } - if (!strcmp ("-PLC", argv[i])) - { - plc=1; - } - - if (!strcmp ("-NB", argv[i])) - { - nbTest = atoi(argv[i + 1]); - i++; - } - - } - - if(mode<0) { - printf("\nError! Mode must be set: -m 0 \n"); - exit(0); - } - - if (CodingMode == 0) - { - printf("\nAdaptive BottleNeck\n"); - } + if (!strcmp("-NB", argv[i])) { + nbTest = atoi(argv[i + 1]); + i++; + } + } + if (mode < 0) { + printf("\nError! Mode must be set: -m 0 \n"); + exit(0); + } + if (CodingMode == 0) { + printf("\nAdaptive BottleNeck\n"); + } - /* Get Bottleneck value */ - bottleneck = atoi(argv[2-tmp]); - if (bottleneck == 0) - { - sscanf(argv[2-tmp], "%s", bottleneck_file); - f_bn = fopen(bottleneck_file, "rb"); - if (f_bn == NULL) - { - printf("No value provided for BottleNeck and cannot read file %s.\n", bottleneck_file); - exit(0); - } - else { - printf("reading bottleneck rates from file %s\n\n",bottleneck_file); - if (fscanf(f_bn, "%d", &bottleneck) == EOF) { - /* Set pointer to beginning of file */ - fseek(f_bn, 0L, SEEK_SET); - fscanf(f_bn, "%d", &bottleneck); - } + /* Get Bottleneck value */ + bottleneck = atoi(argv[2 - tmp]); + if (bottleneck == 0) { + sscanf(argv[2 - tmp], "%s", bottleneck_file); + f_bn = fopen(bottleneck_file, "rb"); + if (f_bn == NULL) { + printf("No value provided for BottleNeck and cannot read file %s.\n", + bottleneck_file); + exit(0); + } else { + printf("reading bottleneck rates from file %s\n\n", bottleneck_file); + if (fscanf(f_bn, "%d", &bottleneck) == EOF) { + /* Set pointer to beginning of file */ + fseek(f_bn, 0L, SEEK_SET); + fscanf(f_bn, "%d", &bottleneck); + } - /* Bottleneck is a cosine function - * Matlab code for writing the bottleneck file: - * BottleNeck_10ms = 20e3 + 10e3 * cos((0:5999)/5999*2*pi); - * fid = fopen('bottleneck.txt', 'wb'); - * fprintf(fid, '%d\n', BottleNeck_10ms); fclose(fid); - */ - } - } - else - { - printf("\nfixed bottleneck rate of %d bits/s\n\n", bottleneck); - } + /* Bottleneck is a cosine function + * Matlab code for writing the bottleneck file: + * BottleNeck_10ms = 20e3 + 10e3 * cos((0:5999)/5999*2*pi); + * fid = fopen('bottleneck.txt', 'wb'); + * fprintf(fid, '%d\n', BottleNeck_10ms); fclose(fid); + */ + } + } else { + printf("\nfixed bottleneck rate of %d bits/s\n\n", bottleneck); + } + /* Get Input and Output files */ + sscanf(argv[argc - 2], "%s", inname); + sscanf(argv[argc - 1], "%s", outname); + if ((inp = fopen(inname, "rb")) == NULL) { + printf(" iSAC: Cannot read file %s.\n", inname); + exit(1); + } + if ((outp = fopen(outname, "wb")) == NULL) { + printf(" iSAC: Cannot write file %s.\n", outname); + exit(1); + } + printf("\nInput:%s\nOutput:%s\n", inname, outname); - /* Get Input and Output files */ - sscanf(argv[argc-2], "%s", inname); - sscanf(argv[argc-1], "%s", outname); - - if ((inp = fopen(inname,"rb")) == NULL) { - printf(" iSAC: Cannot read file %s.\n", inname); - exit(1); - } - if ((outp = fopen(outname,"wb")) == NULL) { - printf(" iSAC: Cannot write file %s.\n", outname); - exit(1); - } - printf("\nInput:%s\nOutput:%s\n", inname, outname); + i = 0; + while (outname[i] != '\0') { + bitfilename[i] = outname[i]; + i++; + } + i -= 4; + for (j = 0; j < 9; j++, i++) + bitfilename[i] = bitending[j]; + bitfilename[i] = '\0'; + if ((bitsp = fopen(bitfilename, "wb")) == NULL) { + printf(" iSAC: Cannot read file %s.\n", bitfilename); + exit(1); + } + printf("Bitstream:%s\n\n", bitfilename); - i=0; - while (outname[i]!='\0') { - bitfilename[i]=outname[i]; - i++; - } - i-=4; - for (j=0;j<9;j++, i++) - bitfilename[i]=bitending[j]; - bitfilename[i]='\0'; - if ((bitsp = fopen(bitfilename,"wb")) == NULL) { - printf(" iSAC: Cannot read file %s.\n", bitfilename); - exit(1); - } - printf("Bitstream:%s\n\n", bitfilename); + starttime = clock() / (double)CLOCKS_PER_SEC; /* Runtime statistics */ + /* Initialize the ISAC and BN structs */ + WebRtcIsac_create(&ISAC_main_inst); + /* WebRtcIsacfix_AssignSize(&sss); + iSACstruct=malloc(sss); + WebRtcIsacfix_Assign(&ISACFIX_main_inst,iSACstruct);*/ + WebRtcIsacfix_Create(&ISACFIX_main_inst); - - starttime = clock()/(double)CLOCKS_PER_SEC; /* Runtime statistics */ + BN_data.send_time = 0; + BN_data.arrival_time = 0; + BN_data.sample_count = 0; + BN_data.rtp_number = 0; + /* Initialize encoder and decoder */ + framecnt = 0; + endfile = 0; - /* Initialize the ISAC and BN structs */ - WebRtcIsac_create(&ISAC_main_inst); -/* WebRtcIsacfix_AssignSize(&sss); - iSACstruct=malloc(sss); - WebRtcIsacfix_Assign(&ISACFIX_main_inst,iSACstruct);*/ - WebRtcIsacfix_Create(&ISACFIX_main_inst); - - BN_data.send_time = 0; - BN_data.arrival_time = 0; - BN_data.sample_count = 0; - BN_data.rtp_number = 0; - - /* Initialize encoder and decoder */ - framecnt= 0; - endfile = 0; + if (mode == 0) { /* Encode using FLOAT, decode using FLOAT */ - if (mode==0) { /* Encode using FLOAT, decode using FLOAT */ + printf("Coding mode: Encode using FLOAT, decode using FLOAT \n\n"); - printf("Coding mode: Encode using FLOAT, decode using FLOAT \n\n"); + /* Init iSAC FLOAT */ + WebRtcIsac_EncoderInit(ISAC_main_inst, CodingMode); + WebRtcIsac_DecoderInit(ISAC_main_inst); + if (CodingMode == 1) { + err = WebRtcIsac_Control(ISAC_main_inst, bottleneck, framesize); + if (err < 0) { + /* exit if returned with error */ + errtype = WebRtcIsac_GetErrorCode(ISAC_main_inst); + printf("\n\n Error in initialization: %d.\n\n", errtype); + // exit(EXIT_FAILURE); + } + } - /* Init iSAC FLOAT */ - WebRtcIsac_EncoderInit(ISAC_main_inst, CodingMode); - WebRtcIsac_DecoderInit(ISAC_main_inst); - if (CodingMode == 1) { - err = WebRtcIsac_Control(ISAC_main_inst, bottleneck, framesize); - if (err < 0) { - /* exit if returned with error */ - errtype=WebRtcIsac_GetErrorCode(ISAC_main_inst); - printf("\n\n Error in initialization: %d.\n\n", errtype); - // exit(EXIT_FAILURE); - } - } - - } else if (mode==1) { /* Encode using FLOAT, decode using FIX */ + } else if (mode == 1) { /* Encode using FLOAT, decode using FIX */ - printf("Coding mode: Encode using FLOAT, decode using FIX \n\n"); + printf("Coding mode: Encode using FLOAT, decode using FIX \n\n"); - /* Init iSAC FLOAT */ - WebRtcIsac_EncoderInit(ISAC_main_inst, CodingMode); - WebRtcIsac_DecoderInit(ISAC_main_inst); - if (CodingMode == 1) { - err = WebRtcIsac_Control(ISAC_main_inst, bottleneck, framesize); - if (err < 0) { - /* exit if returned with error */ - errtype=WebRtcIsac_GetErrorCode(ISAC_main_inst); - printf("\n\n Error in initialization: %d.\n\n", errtype); - // exit(EXIT_FAILURE); - } - } + /* Init iSAC FLOAT */ + WebRtcIsac_EncoderInit(ISAC_main_inst, CodingMode); + WebRtcIsac_DecoderInit(ISAC_main_inst); + if (CodingMode == 1) { + err = WebRtcIsac_Control(ISAC_main_inst, bottleneck, framesize); + if (err < 0) { + /* exit if returned with error */ + errtype = WebRtcIsac_GetErrorCode(ISAC_main_inst); + printf("\n\n Error in initialization: %d.\n\n", errtype); + // exit(EXIT_FAILURE); + } + } - /* Init iSAC FIX */ - WebRtcIsacfix_EncoderInit(ISACFIX_main_inst, CodingMode); - WebRtcIsacfix_DecoderInit(ISACFIX_main_inst); - if (CodingMode == 1) { - err = WebRtcIsacfix_Control(ISACFIX_main_inst, bottleneck, framesize); - if (err < 0) { - /* exit if returned with error */ - errtype=WebRtcIsacfix_GetErrorCode(ISACFIX_main_inst); - printf("\n\n Error in initialization: %d.\n\n", errtype); - //exit(EXIT_FAILURE); - } - } - } else if (mode==2) { /* Encode using FIX, decode using FLOAT */ + /* Init iSAC FIX */ + WebRtcIsacfix_EncoderInit(ISACFIX_main_inst, CodingMode); + WebRtcIsacfix_DecoderInit(ISACFIX_main_inst); + if (CodingMode == 1) { + err = WebRtcIsacfix_Control(ISACFIX_main_inst, bottleneck, framesize); + if (err < 0) { + /* exit if returned with error */ + errtype = WebRtcIsacfix_GetErrorCode(ISACFIX_main_inst); + printf("\n\n Error in initialization: %d.\n\n", errtype); + // exit(EXIT_FAILURE); + } + } + } else if (mode == 2) { /* Encode using FIX, decode using FLOAT */ - printf("Coding mode: Encode using FIX, decode using FLOAT \n\n"); + printf("Coding mode: Encode using FIX, decode using FLOAT \n\n"); - /* Init iSAC FLOAT */ - WebRtcIsac_EncoderInit(ISAC_main_inst, CodingMode); - WebRtcIsac_DecoderInit(ISAC_main_inst); - if (CodingMode == 1) { - err = WebRtcIsac_Control(ISAC_main_inst, bottleneck, framesize); - if (err < 0) { - /* exit if returned with error */ - errtype=WebRtcIsac_GetErrorCode(ISAC_main_inst); - printf("\n\n Error in initialization: %d.\n\n", errtype); - //exit(EXIT_FAILURE); - } - } - - /* Init iSAC FIX */ - WebRtcIsacfix_EncoderInit(ISACFIX_main_inst, CodingMode); - WebRtcIsacfix_DecoderInit(ISACFIX_main_inst); - if (CodingMode == 1) { - err = WebRtcIsacfix_Control(ISACFIX_main_inst, bottleneck, framesize); - if (err < 0) { - /* exit if returned with error */ - errtype=WebRtcIsacfix_GetErrorCode(ISACFIX_main_inst); - printf("\n\n Error in initialization: %d.\n\n", errtype); - //exit(EXIT_FAILURE); - } - } - } else if (mode==3) { + /* Init iSAC FLOAT */ + WebRtcIsac_EncoderInit(ISAC_main_inst, CodingMode); + WebRtcIsac_DecoderInit(ISAC_main_inst); + if (CodingMode == 1) { + err = WebRtcIsac_Control(ISAC_main_inst, bottleneck, framesize); + if (err < 0) { + /* exit if returned with error */ + errtype = WebRtcIsac_GetErrorCode(ISAC_main_inst); + printf("\n\n Error in initialization: %d.\n\n", errtype); + // exit(EXIT_FAILURE); + } + } - printf("Coding mode: Encode using FIX, decode using FIX \n\n"); + /* Init iSAC FIX */ + WebRtcIsacfix_EncoderInit(ISACFIX_main_inst, CodingMode); + WebRtcIsacfix_DecoderInit(ISACFIX_main_inst); + if (CodingMode == 1) { + err = WebRtcIsacfix_Control(ISACFIX_main_inst, bottleneck, framesize); + if (err < 0) { + /* exit if returned with error */ + errtype = WebRtcIsacfix_GetErrorCode(ISACFIX_main_inst); + printf("\n\n Error in initialization: %d.\n\n", errtype); + // exit(EXIT_FAILURE); + } + } + } else if (mode == 3) { + printf("Coding mode: Encode using FIX, decode using FIX \n\n"); - WebRtcIsacfix_EncoderInit(ISACFIX_main_inst, CodingMode); - WebRtcIsacfix_DecoderInit(ISACFIX_main_inst); - if (CodingMode == 1) { - err = WebRtcIsacfix_Control(ISACFIX_main_inst, bottleneck, framesize); - if (err < 0) { - /* exit if returned with error */ - errtype=WebRtcIsacfix_GetErrorCode(ISACFIX_main_inst); - printf("\n\n Error in initialization: %d.\n\n", errtype); - //exit(EXIT_FAILURE); - } - } + WebRtcIsacfix_EncoderInit(ISACFIX_main_inst, CodingMode); + WebRtcIsacfix_DecoderInit(ISACFIX_main_inst); + if (CodingMode == 1) { + err = WebRtcIsacfix_Control(ISACFIX_main_inst, bottleneck, framesize); + if (err < 0) { + /* exit if returned with error */ + errtype = WebRtcIsacfix_GetErrorCode(ISACFIX_main_inst); + printf("\n\n Error in initialization: %d.\n\n", errtype); + // exit(EXIT_FAILURE); + } + } - } else - printf("Mode must be value between 0 and 3\n"); - *speechType = 1; + } else + printf("Mode must be value between 0 and 3\n"); + *speechType = 1; //#define BI_TEST 1 #ifdef BI_TEST - err = WebRtcIsacfix_SetMaxPayloadSize(ISACFIX_main_inst, 300); - if (err < 0) { - /* exit if returned with error */ - errtype=WebRtcIsacfix_GetErrorCode(ISACFIX_main_inst); - printf("\n\n Error in setMaxPayloadSize: %d.\n\n", errtype); - fclose(inp); - fclose(outp); - fclose(bitsp); - return(EXIT_FAILURE); - } + err = WebRtcIsacfix_SetMaxPayloadSize(ISACFIX_main_inst, 300); + if (err < 0) { + /* exit if returned with error */ + errtype = WebRtcIsacfix_GetErrorCode(ISACFIX_main_inst); + printf("\n\n Error in setMaxPayloadSize: %d.\n\n", errtype); + fclose(inp); + fclose(outp); + fclose(bitsp); + return (EXIT_FAILURE); + } #endif + while (endfile == 0) { + cur_framesmpls = 0; + while (1) { + /* Read 10 ms speech block */ + if (nbTest != 1) + endfile = readframe(shortdata, inp, FRAMESAMPLES_10ms); + else + endfile = readframe(shortdata, inp, (FRAMESAMPLES_10ms / 2)); - while (endfile == 0) { + /* iSAC encoding */ - cur_framesmpls = 0; - while (1) { - /* Read 10 ms speech block */ - if (nbTest != 1) - endfile = readframe(shortdata, inp, FRAMESAMPLES_10ms); - else - endfile = readframe(shortdata, inp, (FRAMESAMPLES_10ms/2)); + if (mode == 0 || mode == 1) { + stream_len = + WebRtcIsac_Encode(ISAC_main_inst, shortdata, (uint8_t*)streamdata); + if (stream_len < 0) { + /* exit if returned with error */ + errtype = WebRtcIsac_GetErrorCode(ISAC_main_inst); + printf("\n\nError in encoder: %d.\n\n", errtype); + // exit(EXIT_FAILURE); + } + } else if (mode == 2 || mode == 3) { + /* iSAC encoding */ + if (nbTest != 1) + stream_len = WebRtcIsacfix_Encode(ISACFIX_main_inst, shortdata, + (uint8_t*)streamdata); + else + stream_len = + WebRtcIsacfix_EncodeNb(ISACFIX_main_inst, shortdata, streamdata); - /* iSAC encoding */ + if (stream_len < 0) { + /* exit if returned with error */ + errtype = WebRtcIsacfix_GetErrorCode(ISACFIX_main_inst); + printf("\n\nError in encoder: %d.\n\n", errtype); + // exit(EXIT_FAILURE); + } + } - if (mode==0 || mode ==1) { - stream_len = WebRtcIsac_Encode(ISAC_main_inst, - shortdata, - (uint8_t*)streamdata); - if (stream_len < 0) { - /* exit if returned with error */ - errtype=WebRtcIsac_GetErrorCode(ISAC_main_inst); - printf("\n\nError in encoder: %d.\n\n", errtype); - // exit(EXIT_FAILURE); - } - } else if (mode==2 || mode==3) { - /* iSAC encoding */ - if (nbTest != 1) - stream_len = WebRtcIsacfix_Encode( - ISACFIX_main_inst, - shortdata, - (uint8_t*)streamdata); - else - stream_len = WebRtcIsacfix_EncodeNb(ISACFIX_main_inst, shortdata, streamdata); - - if (stream_len < 0) { - /* exit if returned with error */ - errtype=WebRtcIsacfix_GetErrorCode(ISACFIX_main_inst); - printf("\n\nError in encoder: %d.\n\n", errtype); - // exit(EXIT_FAILURE); - } - } + cur_framesmpls += FRAMESAMPLES_10ms; - cur_framesmpls += FRAMESAMPLES_10ms; + /* read next bottleneck rate */ + if (f_bn != NULL) { + if (fscanf(f_bn, "%d", &bottleneck) == EOF) { + /* Set pointer to beginning of file */ + fseek(f_bn, 0L, SEEK_SET); + fscanf(f_bn, "%d", &bottleneck); + } + if (CodingMode == 1) { + if (mode == 0 || mode == 1) + WebRtcIsac_Control(ISAC_main_inst, bottleneck, framesize); + else if (mode == 2 || mode == 3) + WebRtcIsacfix_Control(ISACFIX_main_inst, bottleneck, framesize); + } + } - /* read next bottleneck rate */ - if (f_bn != NULL) { - if (fscanf(f_bn, "%d", &bottleneck) == EOF) { - /* Set pointer to beginning of file */ - fseek(f_bn, 0L, SEEK_SET); - fscanf(f_bn, "%d", &bottleneck); - } - if (CodingMode == 1) { - if (mode==0 || mode==1) - WebRtcIsac_Control(ISAC_main_inst, bottleneck, framesize); - else if (mode==2 || mode==3) - WebRtcIsacfix_Control(ISACFIX_main_inst, bottleneck, framesize); - } - } + /* exit encoder loop if the encoder returned a bitstream */ + if (stream_len != 0) + break; + } - /* exit encoder loop if the encoder returned a bitstream */ - if (stream_len != 0) break; - } - - fwrite(streamdata, 1, stream_len, bitsp); /* NOTE! Writes bytes to file */ + fwrite(streamdata, 1, stream_len, bitsp); /* NOTE! Writes bytes to file */ - /* simulate packet handling through NetEq and the modem */ - get_arrival_time(cur_framesmpls, stream_len, bottleneck, - &BN_data); -//***************************** - if (1){ - if (mode==0) { - err = WebRtcIsac_UpdateBwEstimate(ISAC_main_inst, - streamdata, - stream_len, - BN_data.rtp_number, - BN_data.arrival_time); + /* simulate packet handling through NetEq and the modem */ + get_arrival_time(cur_framesmpls, stream_len, bottleneck, &BN_data); + //***************************** + if (1) { + if (mode == 0) { + err = WebRtcIsac_UpdateBwEstimate(ISAC_main_inst, streamdata, + stream_len, BN_data.rtp_number, + BN_data.arrival_time); - if (err < 0) { - /* exit if returned with error */ - errtype=WebRtcIsac_GetErrorCode(ISAC_main_inst); - printf("\n\nError in decoder: %d.\n\n", errtype); - //exit(EXIT_FAILURE); - } - /* iSAC decoding */ - declen = WebRtcIsac_Decode(ISAC_main_inst, - streamdata, - stream_len, - decoded, - speechType); - if (declen <= 0) { - /* exit if returned with error */ - errtype=WebRtcIsac_GetErrorCode(ISAC_main_inst); - printf("\n\nError in decoder: %d.\n\n", errtype); - //exit(EXIT_FAILURE); - } - } else if (mode==1) { + if (err < 0) { + /* exit if returned with error */ + errtype = WebRtcIsac_GetErrorCode(ISAC_main_inst); + printf("\n\nError in decoder: %d.\n\n", errtype); + // exit(EXIT_FAILURE); + } + /* iSAC decoding */ + declen = WebRtcIsac_Decode(ISAC_main_inst, streamdata, stream_len, + decoded, speechType); + if (declen <= 0) { + /* exit if returned with error */ + errtype = WebRtcIsac_GetErrorCode(ISAC_main_inst); + printf("\n\nError in decoder: %d.\n\n", errtype); + // exit(EXIT_FAILURE); + } + } else if (mode == 1) { + err = WebRtcIsac_UpdateBwEstimate(ISAC_main_inst, streamdata, + stream_len, BN_data.rtp_number, + BN_data.arrival_time); + err = WebRtcIsacfix_UpdateBwEstimate1(ISACFIX_main_inst, streamdata, + stream_len, BN_data.rtp_number, + BN_data.arrival_time); + if (err < 0) { + /* exit if returned with error */ + errtype = WebRtcIsacfix_GetErrorCode(ISACFIX_main_inst); + printf("\n\nError in decoder: %d.\n\n", errtype); + // exit(EXIT_FAILURE); + } - err = WebRtcIsac_UpdateBwEstimate(ISAC_main_inst, - streamdata, - stream_len, - BN_data.rtp_number, - BN_data.arrival_time); - err = WebRtcIsacfix_UpdateBwEstimate1(ISACFIX_main_inst, - streamdata, - stream_len, - BN_data.rtp_number, - BN_data.arrival_time); - if (err < 0) { - /* exit if returned with error */ - errtype=WebRtcIsacfix_GetErrorCode(ISACFIX_main_inst); - printf("\n\nError in decoder: %d.\n\n", errtype); - //exit(EXIT_FAILURE); - } + declen = WebRtcIsac_Decode(ISAC_main_inst, streamdata, stream_len, + decoded, speechType); - declen = WebRtcIsac_Decode(ISAC_main_inst, - streamdata, - stream_len, - decoded, - speechType); + /* iSAC decoding */ + if (plc && (framecnt + 1) % 10 == 0) { + if (nbTest != 2) + declen = WebRtcIsacfix_DecodePlc(ISACFIX_main_inst, decoded, 1); + else + declen = WebRtcIsacfix_DecodePlcNb(ISACFIX_main_inst, decoded, 1); + } else { + if (nbTest != 2) + declen = WebRtcIsacfix_Decode(ISACFIX_main_inst, streamdata, + stream_len, decoded, speechType); + else + declen = WebRtcIsacfix_DecodeNb(ISACFIX_main_inst, streamdata, + stream_len, decoded, speechType); + } - /* iSAC decoding */ - if (plc && (framecnt+1)%10 == 0) { - if (nbTest !=2 ) - declen = WebRtcIsacfix_DecodePlc( ISACFIX_main_inst, decoded, 1 ); - else - declen = WebRtcIsacfix_DecodePlcNb( ISACFIX_main_inst, decoded, 1 ); - } else { - if (nbTest !=2 ) - declen = WebRtcIsacfix_Decode(ISACFIX_main_inst, - streamdata, - stream_len, - decoded, - speechType); - else - declen = WebRtcIsacfix_DecodeNb(ISACFIX_main_inst, - streamdata, - stream_len, - decoded, - speechType); - } - - if (declen <= 0) { - /* exit if returned with error */ - errtype=WebRtcIsacfix_GetErrorCode(ISACFIX_main_inst); - printf("\n\nError in decoder: %d.\n\n", errtype); - //exit(EXIT_FAILURE); - } - } else if (mode==2) { - err = WebRtcIsacfix_UpdateBwEstimate1(ISACFIX_main_inst, - streamdata, - stream_len, - BN_data.rtp_number, - BN_data.arrival_time); + if (declen <= 0) { + /* exit if returned with error */ + errtype = WebRtcIsacfix_GetErrorCode(ISACFIX_main_inst); + printf("\n\nError in decoder: %d.\n\n", errtype); + // exit(EXIT_FAILURE); + } + } else if (mode == 2) { + err = WebRtcIsacfix_UpdateBwEstimate1(ISACFIX_main_inst, streamdata, + stream_len, BN_data.rtp_number, + BN_data.arrival_time); - err = WebRtcIsac_UpdateBwEstimate(ISAC_main_inst, - streamdata, - stream_len, - BN_data.rtp_number, - BN_data.arrival_time); + err = WebRtcIsac_UpdateBwEstimate(ISAC_main_inst, streamdata, + stream_len, BN_data.rtp_number, + BN_data.arrival_time); - if (err < 0) { - /* exit if returned with error */ - errtype=WebRtcIsac_GetErrorCode(ISAC_main_inst); - printf("\n\nError in decoder: %d.\n\n", errtype); - //exit(EXIT_FAILURE); - } - /* iSAC decoding */ - declen = WebRtcIsac_Decode(ISAC_main_inst, - streamdata, - stream_len, - decoded, - speechType); - if (declen <= 0) { - /* exit if returned with error */ - errtype=WebRtcIsac_GetErrorCode(ISAC_main_inst); - printf("\n\nError in decoder: %d.\n\n", errtype); - //exit(EXIT_FAILURE); - } - } else if (mode==3) { - err = WebRtcIsacfix_UpdateBwEstimate(ISACFIX_main_inst, - streamdata, - stream_len, - BN_data.rtp_number, - BN_data.send_time, - BN_data.arrival_time); + if (err < 0) { + /* exit if returned with error */ + errtype = WebRtcIsac_GetErrorCode(ISAC_main_inst); + printf("\n\nError in decoder: %d.\n\n", errtype); + // exit(EXIT_FAILURE); + } + /* iSAC decoding */ + declen = WebRtcIsac_Decode(ISAC_main_inst, streamdata, stream_len, + decoded, speechType); + if (declen <= 0) { + /* exit if returned with error */ + errtype = WebRtcIsac_GetErrorCode(ISAC_main_inst); + printf("\n\nError in decoder: %d.\n\n", errtype); + // exit(EXIT_FAILURE); + } + } else if (mode == 3) { + err = WebRtcIsacfix_UpdateBwEstimate( + ISACFIX_main_inst, streamdata, stream_len, BN_data.rtp_number, + BN_data.send_time, BN_data.arrival_time); - if (err < 0) { - /* exit if returned with error */ - errtype=WebRtcIsacfix_GetErrorCode(ISACFIX_main_inst); - printf("\n\nError in decoder: %d.\n\n", errtype); - //exit(EXIT_FAILURE); - } - /* iSAC decoding */ - - if (plc && (framecnt+1)%10 == 0) { - if (nbTest !=2 ) - declen = WebRtcIsacfix_DecodePlc( ISACFIX_main_inst, decoded, 1 ); - else - declen = WebRtcIsacfix_DecodePlcNb( ISACFIX_main_inst, decoded, 1 ); - } else { - if (nbTest !=2 ) - declen = WebRtcIsacfix_Decode(ISACFIX_main_inst, - streamdata, - stream_len, - decoded, - speechType); - else - declen = WebRtcIsacfix_DecodeNb(ISACFIX_main_inst, - streamdata, - stream_len, - decoded, - speechType); - } - if (declen <= 0) { - /* exit if returned with error */ - errtype=WebRtcIsacfix_GetErrorCode(ISACFIX_main_inst); - printf("\n\nError in decoder: %d.\n\n", errtype); - //exit(EXIT_FAILURE); - } - } + if (err < 0) { + /* exit if returned with error */ + errtype = WebRtcIsacfix_GetErrorCode(ISACFIX_main_inst); + printf("\n\nError in decoder: %d.\n\n", errtype); + // exit(EXIT_FAILURE); + } + /* iSAC decoding */ - /* Write decoded speech frame to file */ - fwrite(decoded, sizeof(int16_t), declen, outp); - } - - fprintf(stderr," \rframe = %d", framecnt); - framecnt++; + if (plc && (framecnt + 1) % 10 == 0) { + if (nbTest != 2) + declen = WebRtcIsacfix_DecodePlc(ISACFIX_main_inst, decoded, 1); + else + declen = WebRtcIsacfix_DecodePlcNb(ISACFIX_main_inst, decoded, 1); + } else { + if (nbTest != 2) + declen = WebRtcIsacfix_Decode(ISACFIX_main_inst, streamdata, + stream_len, decoded, speechType); + else + declen = WebRtcIsacfix_DecodeNb(ISACFIX_main_inst, streamdata, + stream_len, decoded, speechType); + } + if (declen <= 0) { + /* exit if returned with error */ + errtype = WebRtcIsacfix_GetErrorCode(ISACFIX_main_inst); + printf("\n\nError in decoder: %d.\n\n", errtype); + // exit(EXIT_FAILURE); + } + } + /* Write decoded speech frame to file */ + fwrite(decoded, sizeof(int16_t), declen, outp); + } + fprintf(stderr, " \rframe = %d", framecnt); + framecnt++; #ifdef _DEBUG - - totalsmpls += declen; - totalbits += 8 * stream_len; - kbps = ((double) FS) / ((double) cur_framesmpls) * 8.0 * stream_len / 1000.0;// kbits/s - fy = fopen("bit_rate.dat", "a"); - fprintf(fy, "Frame %i = %0.14f\n", framecnt, kbps); - fclose(fy); - + + totalsmpls += declen; + totalbits += 8 * stream_len; + kbps = ((double)FS) / ((double)cur_framesmpls) * 8.0 * stream_len / + 1000.0; // kbits/s + fy = fopen("bit_rate.dat", "a"); + fprintf(fy, "Frame %i = %0.14f\n", framecnt, kbps); + fclose(fy); + #endif /* _DEBUG */ - - } - + } + #ifdef _DEBUG - printf("\n\ntotal bits = %d bits", totalbits); - printf("\nmeasured average bitrate = %0.3f kbits/s", (double)totalbits *(FS/1000) / totalsmpls); - printf("\n"); + printf("\n\ntotal bits = %d bits", totalbits); + printf("\nmeasured average bitrate = %0.3f kbits/s", + (double)totalbits * (FS / 1000) / totalsmpls); + printf("\n"); #endif /* _DEBUG */ - - /* Runtime statistics */ - runtime = (double)(clock()/(double)CLOCKS_PER_SEC-starttime); - length_file = ((double)framecnt*(double)declen/FS); - printf("\n\nLength of speech file: %.1f s\n", length_file); - printf("Time to run iSAC: %.2f s (%.2f %% of realtime)\n\n", runtime, (100*runtime/length_file)); - printf("---------------------END----------------------\n"); - - fclose(inp); - fclose(outp); - - WebRtcIsac_Free(ISAC_main_inst); - WebRtcIsacfix_Free(ISACFIX_main_inst); - + /* Runtime statistics */ + runtime = (double)(clock() / (double)CLOCKS_PER_SEC - starttime); + length_file = ((double)framecnt * (double)declen / FS); + printf("\n\nLength of speech file: %.1f s\n", length_file); + printf("Time to run iSAC: %.2f s (%.2f %% of realtime)\n\n", runtime, + (100 * runtime / length_file)); + printf("---------------------END----------------------\n"); -// fclose(histfile); -// fclose(ratefile); - - return 0; + fclose(inp); + fclose(outp); -} + WebRtcIsac_Free(ISAC_main_inst); + WebRtcIsacfix_Free(ISACFIX_main_inst); + // fclose(histfile); + // fclose(ratefile); + return 0; +} diff --git a/webrtc/modules/audio_coding/codecs/isac/main/test/ReleaseTest-API/ReleaseTest-API.cc b/webrtc/modules/audio_coding/codecs/isac/main/test/ReleaseTest-API/ReleaseTest-API.cc index ffdcc0c1a7..717da09b6e 100644 --- a/webrtc/modules/audio_coding/codecs/isac/main/test/ReleaseTest-API/ReleaseTest-API.cc +++ b/webrtc/modules/audio_coding/codecs/isac/main/test/ReleaseTest-API/ReleaseTest-API.cc @@ -23,1073 +23,959 @@ #include "utility.h" /* Defines */ -#define SEED_FILE "randseed.txt" /* Used when running decoder on garbage data */ -#define MAX_FRAMESAMPLES 960 /* max number of samples per frame - (= 60 ms frame & 16 kHz) or - (= 30 ms frame & 32 kHz) */ -#define FRAMESAMPLES_10ms 160 /* number of samples per 10ms frame */ +#define SEED_FILE \ + "randseed.txt" /* Used when running decoder on garbage data */ +#define MAX_FRAMESAMPLES \ + 960 /* max number of samples per frame \ + (= 60 ms frame & 16 kHz) or \ + (= 30 ms frame & 32 kHz) */ +#define FRAMESAMPLES_10ms 160 /* number of samples per 10ms frame */ #define SWBFRAMESAMPLES_10ms 320 //#define FS 16000 /* sampling frequency (Hz) */ #ifdef WIN32 -#define CLOCKS_PER_SEC 1000 /* Runtime statistics */ +#define CLOCKS_PER_SEC 1000 /* Runtime statistics */ #endif - - - using namespace std; -int main(int argc, char* argv[]) -{ +int main(int argc, char* argv[]) { + char inname[100], outname[100], bottleneck_file[100], vadfile[100]; + FILE* inp, *outp, * f_bn = NULL, * vadp = NULL, *bandwidthp; + int framecnt, endfile; - char inname[100], outname[100], bottleneck_file[100], vadfile[100]; - FILE *inp, *outp, *f_bn = NULL, *vadp = NULL, *bandwidthp; - int framecnt, endfile; + int i, errtype, VADusage = 0, packetLossPercent = 0; + int16_t CodingMode; + int32_t bottleneck = 0; + int16_t framesize = 30; /* ms */ + int cur_framesmpls, err; - int i, errtype, VADusage = 0, packetLossPercent = 0; - int16_t CodingMode; - int32_t bottleneck = 0; - int16_t framesize = 30; /* ms */ - int cur_framesmpls, err; + /* Runtime statistics */ + double starttime, runtime, length_file; - /* Runtime statistics */ - double starttime, runtime, length_file; + int16_t stream_len = 0; + int16_t declen = 0, lostFrame = 0, declenTC = 0; - int16_t stream_len = 0; - int16_t declen = 0, lostFrame = 0, declenTC = 0; - - int16_t shortdata[SWBFRAMESAMPLES_10ms]; - int16_t vaddata[SWBFRAMESAMPLES_10ms*3]; - int16_t decoded[MAX_FRAMESAMPLES << 1]; - int16_t decodedTC[MAX_FRAMESAMPLES << 1]; - uint16_t streamdata[500]; - int16_t speechType[1]; - int16_t rateBPS = 0; - int16_t fixedFL = 0; - int16_t payloadSize = 0; - int32_t payloadRate = 0; - int setControlBWE = 0; - short FL, testNum; - char version_number[20]; - FILE *plFile; - int32_t sendBN; + int16_t shortdata[SWBFRAMESAMPLES_10ms]; + int16_t vaddata[SWBFRAMESAMPLES_10ms * 3]; + int16_t decoded[MAX_FRAMESAMPLES << 1]; + int16_t decodedTC[MAX_FRAMESAMPLES << 1]; + uint16_t streamdata[500]; + int16_t speechType[1]; + int16_t rateBPS = 0; + int16_t fixedFL = 0; + int16_t payloadSize = 0; + int32_t payloadRate = 0; + int setControlBWE = 0; + short FL, testNum; + char version_number[20]; + FILE* plFile; + int32_t sendBN; #ifdef _DEBUG - FILE *fy; - double kbps; + FILE* fy; + double kbps; #endif /* _DEBUG */ - int totalbits =0; - int totalsmpls =0; + int totalbits = 0; + int totalsmpls = 0; - /* If use GNS file */ - FILE *fp_gns = NULL; - char gns_file[100]; - short maxStreamLen30 = 0; - short maxStreamLen60 = 0; - short sampFreqKHz = 32; - short samplesIn10Ms; - short useAssign = 0; - //FILE logFile; - bool doTransCoding = false; - int32_t rateTransCoding = 0; - uint8_t streamDataTransCoding[1200]; - int16_t streamLenTransCoding = 0; - FILE* transCodingFile = NULL; - FILE* transcodingBitstream = NULL; - uint32_t numTransCodingBytes = 0; + /* If use GNS file */ + FILE* fp_gns = NULL; + char gns_file[100]; + short maxStreamLen30 = 0; + short maxStreamLen60 = 0; + short sampFreqKHz = 32; + short samplesIn10Ms; + short useAssign = 0; + // FILE logFile; + bool doTransCoding = false; + int32_t rateTransCoding = 0; + uint8_t streamDataTransCoding[1200]; + int16_t streamLenTransCoding = 0; + FILE* transCodingFile = NULL; + FILE* transcodingBitstream = NULL; + uint32_t numTransCodingBytes = 0; - /* only one structure used for ISAC encoder */ - ISACStruct* ISAC_main_inst = NULL; - ISACStruct* decoderTransCoding = NULL; + /* only one structure used for ISAC encoder */ + ISACStruct* ISAC_main_inst = NULL; + ISACStruct* decoderTransCoding = NULL; - BottleNeckModel BN_data; + BottleNeckModel BN_data; #ifdef _DEBUG - fy = fopen("bit_rate.dat", "w"); - fclose(fy); - fy = fopen("bytes_frames.dat", "w"); - fclose(fy); + fy = fopen("bit_rate.dat", "w"); + fclose(fy); + fy = fopen("bytes_frames.dat", "w"); + fclose(fy); #endif /* _DEBUG */ - /* Handling wrong input arguments in the command line */ - if((argc<3) || (argc>17)) { - printf("\n\nWrong number of arguments or flag values.\n\n"); + /* Handling wrong input arguments in the command line */ + if ((argc < 3) || (argc > 17)) { + printf("\n\nWrong number of arguments or flag values.\n\n"); - printf("\n"); - WebRtcIsac_version(version_number); - printf("iSAC-swb version %s \n\n", version_number); - - printf("Usage:\n\n"); - printf("./kenny.exe [-I] bottleneck_value infile outfile \n\n"); - printf("with:\n"); - printf("[-FS num] : sampling frequency in kHz, valid values are 16 & 32,\n"); - printf(" with 16 as default.\n"); - printf("[-I] : if -I option is specified, the coder will use\n"); - printf(" an instantaneous Bottleneck value. If not, it\n"); - printf(" will be an adaptive Bottleneck value.\n\n"); - printf("[-assign] : Use Assign API.\n"); - printf("[-B num] : the value of the bottleneck provided either\n"); - printf(" as a fixed value in bits/sec (e.g. 25000) or\n"); - printf(" read from a file (e.g. bottleneck.txt)\n\n"); - printf("[-INITRATE num] : Set a new value for initial rate. Note! Only used in \n"); - printf(" adaptive mode.\n\n"); - printf("[-FL num] : Set (initial) frame length in msec. Valid length are \n"); - printf(" 30 and 60 msec.\n\n"); - printf("[-FIXED_FL] : Frame length will be fixed to initial value.\n\n"); - printf("[-MAX num] : Set the limit for the payload size of iSAC in bytes. \n"); - printf(" Minimum 100 maximum 400.\n\n"); - printf("[-MAXRATE num] : Set the maxrate for iSAC in bits per second. \n"); - printf(" Minimum 32000, maximum 53400.\n\n"); - printf("[-F num] : if -F option is specified, the test function\n"); - printf(" will run the iSAC API fault scenario specified by the\n"); - printf(" supplied number.\n"); - printf(" F 1 - Call encoder prior to init encoder call\n"); - printf(" F 2 - Call decoder prior to init decoder call\n"); - printf(" F 3 - Call decoder prior to encoder call\n"); - printf(" F 4 - Call decoder with a too short coded sequence\n"); - printf(" F 5 - Call decoder with a too long coded sequence\n"); - printf(" F 6 - Call decoder with random bit stream\n"); - printf(" F 7 - Call init encoder/decoder at random during a call\n"); - printf(" F 8 - Call encoder/decoder without having allocated memory \n"); - printf(" for encoder/decoder instance\n"); - printf(" F 9 - Call decodeB without calling decodeA\n"); - printf(" F 10 - Call decodeB with garbage data\n"); - printf("[-PL num] : if -PL option is specified \n"); - printf("[-T rate file] : test trans-coding with target bottleneck 'rate' bits/sec\n"); - printf(" the output file is written to 'file'\n"); - printf("[-LOOP num] : number of times to repeat coding the input file for stress testing\n"); - //printf("[-CE num] : Test of APIs used by Conference Engine.\n"); - //printf(" CE 1 - getNewBitstream, getBWE \n"); - //printf(" (CE 2 - RESERVED for transcoding)\n"); - //printf(" CE 3 - getSendBWE, setSendBWE. \n\n"); - //printf("-L filename : write the logging info into file (appending)\n"); - printf("infile : Normal speech input file\n\n"); - printf("outfile : Speech output file\n\n"); - exit(0); - } - - /* Print version number */ - printf("-------------------------------------------------\n"); + printf("\n"); WebRtcIsac_version(version_number); - printf("iSAC version %s \n\n", version_number); + printf("iSAC-swb version %s \n\n", version_number); - /* Loop over all command line arguments */ - CodingMode = 0; - testNum = 0; - useAssign = 0; - //logFile = NULL; - char transCodingFileName[500]; - int16_t totFileLoop = 0; - int16_t numFileLoop = 0; - for (i = 1; i < argc-2;i++) - { - if(!strcmp("-LOOP", argv[i])) - { - i++; - totFileLoop = (int16_t)atol(argv[i]); - if(totFileLoop <= 0) - { - fprintf(stderr, "Invalid number of runs for the given input file, %d.", totFileLoop); - exit(0); - } - } + printf("Usage:\n\n"); + printf("./kenny.exe [-I] bottleneck_value infile outfile \n\n"); + printf("with:\n"); + printf( + "[-FS num] : sampling frequency in kHz, valid values are 16 & " + "32,\n"); + printf(" with 16 as default.\n"); + printf( + "[-I] : if -I option is specified, the coder will use\n"); + printf( + " an instantaneous Bottleneck value. If not, it\n"); + printf(" will be an adaptive Bottleneck value.\n\n"); + printf("[-assign] : Use Assign API.\n"); + printf("[-B num] : the value of the bottleneck provided either\n"); + printf( + " as a fixed value in bits/sec (e.g. 25000) or\n"); + printf(" read from a file (e.g. bottleneck.txt)\n\n"); + printf( + "[-INITRATE num] : Set a new value for initial rate. Note! Only used " + "in \n"); + printf(" adaptive mode.\n\n"); + printf( + "[-FL num] : Set (initial) frame length in msec. Valid length " + "are \n"); + printf(" 30 and 60 msec.\n\n"); + printf( + "[-FIXED_FL] : Frame length will be fixed to initial value.\n\n"); + printf( + "[-MAX num] : Set the limit for the payload size of iSAC in " + "bytes. \n"); + printf(" Minimum 100 maximum 400.\n\n"); + printf( + "[-MAXRATE num] : Set the maxrate for iSAC in bits per second. \n"); + printf(" Minimum 32000, maximum 53400.\n\n"); + printf( + "[-F num] : if -F option is specified, the test function\n"); + printf( + " will run the iSAC API fault scenario specified by " + "the\n"); + printf(" supplied number.\n"); + printf( + " F 1 - Call encoder prior to init encoder call\n"); + printf( + " F 2 - Call decoder prior to init decoder call\n"); + printf(" F 3 - Call decoder prior to encoder call\n"); + printf( + " F 4 - Call decoder with a too short coded " + "sequence\n"); + printf( + " F 5 - Call decoder with a too long coded " + "sequence\n"); + printf(" F 6 - Call decoder with random bit stream\n"); + printf( + " F 7 - Call init encoder/decoder at random during " + "a call\n"); + printf( + " F 8 - Call encoder/decoder without having " + "allocated memory \n"); + printf(" for encoder/decoder instance\n"); + printf(" F 9 - Call decodeB without calling decodeA\n"); + printf(" F 10 - Call decodeB with garbage data\n"); + printf("[-PL num] : if -PL option is specified \n"); + printf( + "[-T rate file] : test trans-coding with target bottleneck 'rate' " + "bits/sec\n"); + printf(" the output file is written to 'file'\n"); + printf( + "[-LOOP num] : number of times to repeat coding the input file " + "for stress testing\n"); + // printf("[-CE num] : Test of APIs used by Conference Engine.\n"); + // printf(" CE 1 - getNewBitstream, getBWE \n"); + // printf(" (CE 2 - RESERVED for transcoding)\n"); + // printf(" CE 3 - getSendBWE, setSendBWE. \n\n"); + // printf("-L filename : write the logging info into file + // (appending)\n"); + printf("infile : Normal speech input file\n\n"); + printf("outfile : Speech output file\n\n"); + exit(0); + } - if(!strcmp("-T", argv[i])) - { - doTransCoding = true; - i++; - rateTransCoding = atoi(argv[i]); - i++; - strcpy(transCodingFileName, argv[i]); - } + /* Print version number */ + printf("-------------------------------------------------\n"); + WebRtcIsac_version(version_number); + printf("iSAC version %s \n\n", version_number); - /*Should we use assign API*/ - if(!strcmp("-assign", argv[i])) - { - useAssign = 1; - } - - /* Set Sampling Rate */ - if(!strcmp("-FS", argv[i])) - { - i++; - sampFreqKHz = atoi(argv[i]); - } - - /* Instantaneous mode */ - if(!strcmp ("-I", argv[i])) - { - printf("Instantaneous BottleNeck\n"); - CodingMode = 1; - } - - /* Set (initial) bottleneck value */ - if(!strcmp ("-INITRATE", argv[i])) { - rateBPS = atoi(argv[i + 1]); - setControlBWE = 1; - if((rateBPS < 10000) || (rateBPS > 32000)) - { - printf("\n%d is not a initial rate. Valid values are in the range 10000 to 32000.\n", rateBPS); - exit(0); - } - printf("New initial rate: %d\n", rateBPS); - i++; - } - - /* Set (initial) framelength */ - if(!strcmp ("-FL", argv[i])) { - framesize = atoi(argv[i + 1]); - if((framesize != 30) && (framesize != 60)) - { - printf("\n%d is not a valid frame length. Valid length are 30 and 60 msec.\n", framesize); - exit(0); - } - setControlBWE = 1; - printf("Frame Length: %d\n", framesize); - i++; - } - - /* Fixed frame length */ - if(!strcmp ("-FIXED_FL", argv[i])) - { - fixedFL = 1; - setControlBWE = 1; - printf("Fixed Frame Length\n"); - } - - /* Set maximum allowed payload size in bytes */ - if(!strcmp ("-MAX", argv[i])) { - payloadSize = atoi(argv[i + 1]); - printf("Maximum Payload Size: %d\n", payloadSize); - i++; - } - - /* Set maximum rate in bytes */ - if(!strcmp ("-MAXRATE", argv[i])) { - payloadRate = atoi(argv[i + 1]); - printf("Maximum Rate in kbps: %d\n", payloadRate); - i++; - } - - /* Test of fault scenarious */ - if(!strcmp ("-F", argv[i])) - { - testNum = atoi(argv[i + 1]); - printf("Fault test: %d\n", testNum); - if(testNum < 1 || testNum > 10) - { - printf("\n%d is not a valid Fault Scenario number. Valid Fault Scenarios are numbered 1-10.\n", testNum); - exit(0); - } - i++; - } - - /* Packet loss test */ - if(!strcmp ("-PL", argv[i])) - { - if( isdigit( *argv[i+1] ) ) - { - packetLossPercent = atoi( argv[i+1] ); - if( (packetLossPercent < 0) | (packetLossPercent > 100) ) - { - printf( "\nInvalid packet loss perentage \n" ); - exit( 0 ); - } - if( packetLossPercent > 0 ) - { - printf( "Simulating %d %% of independent packet loss\n", packetLossPercent ); - } - else - { - printf( "\nNo Packet Loss Is Simulated \n" ); - } - } - else - { - plFile = fopen( argv[i+1], "rb" ); - if( plFile == NULL ) - { - printf( "\n couldn't open the frameloss file: %s\n", argv[i+1] ); - exit( 0 ); - } - printf( "Simulating packet loss through the given channel file: %s\n", argv[i+1] ); - } - i++; - } - - /* Random packetlosses */ - if(!strcmp ("-rnd", argv[i])) - { - srand((unsigned int)time(NULL) ); - printf( "Random pattern in lossed packets \n" ); - } - - /* Use gns file */ - if(!strcmp ("-G", argv[i])) - { - sscanf(argv[i + 1], "%s", gns_file); - fp_gns = fopen(gns_file, "rb"); - if(fp_gns == NULL) - { - printf("Cannot read file %s.\n", gns_file); - exit(0); - } - i++; - } - - - // make it with '-B' - /* Get Bottleneck value */ - if(!strcmp("-B", argv[i])) - { - i++; - bottleneck = atoi(argv[i]); - if(bottleneck == 0) - { - sscanf(argv[i], "%s", bottleneck_file); - f_bn = fopen(bottleneck_file, "rb"); - if(f_bn == NULL) - { - printf("Error No value provided for BottleNeck and cannot read file %s.\n", bottleneck_file); - exit(0); - } - else - { - printf("reading bottleneck rates from file %s\n\n",bottleneck_file); - if(fscanf(f_bn, "%d", &bottleneck) == EOF) - { - /* Set pointer to beginning of file */ - fseek(f_bn, 0L, SEEK_SET); - if (fscanf(f_bn, "%d", &bottleneck) == EOF) { - exit(0); - } - } - - /* Bottleneck is a cosine function - * Matlab code for writing the bottleneck file: - * BottleNeck_10ms = 20e3 + 10e3 * cos((0:5999)/5999*2*pi); - * fid = fopen('bottleneck.txt', 'wb'); - * fprintf(fid, '%d\n', BottleNeck_10ms); fclose(fid); - */ - } - } - else - { - printf("\nfixed bottleneck rate of %d bits/s\n\n", bottleneck); - } - } - /* Run Conference Engine APIs */ - // Do not test it in the first release - // - // if(!strcmp ("-CE", argv[i])) - // { - // testCE = atoi(argv[i + 1]); - // if(testCE==1) - // { - // i++; - // scale = (float)atof( argv[i+1] ); - // } - // else if(testCE == 2) - // { - // printf("\nCE-test 2 (transcoding) not implemented.\n"); - // exit(0); - // } - // else if(testCE < 1 || testCE > 3) - // { - // printf("\n%d is not a valid CE-test number. Valid CE tests are 1-3.\n", testCE); - // exit(0); - // } - // printf("CE-test number: %d\n", testCE); - // i++; - // } + /* Loop over all command line arguments */ + CodingMode = 0; + testNum = 0; + useAssign = 0; + // logFile = NULL; + char transCodingFileName[500]; + int16_t totFileLoop = 0; + int16_t numFileLoop = 0; + for (i = 1; i < argc - 2; i++) { + if (!strcmp("-LOOP", argv[i])) { + i++; + totFileLoop = (int16_t)atol(argv[i]); + if (totFileLoop <= 0) { + fprintf(stderr, "Invalid number of runs for the given input file, %d.", + totFileLoop); + exit(0); + } } - if(CodingMode == 0) - { - printf("\nAdaptive BottleNeck\n"); - } + if (!strcmp("-T", argv[i])) { + doTransCoding = true; + i++; + rateTransCoding = atoi(argv[i]); + i++; + strcpy(transCodingFileName, argv[i]); + } - switch(sampFreqKHz) - { - case 16: - { - printf("iSAC Wideband.\n"); - samplesIn10Ms = FRAMESAMPLES_10ms; - break; + /*Should we use assign API*/ + if (!strcmp("-assign", argv[i])) { + useAssign = 1; + } + + /* Set Sampling Rate */ + if (!strcmp("-FS", argv[i])) { + i++; + sampFreqKHz = atoi(argv[i]); + } + + /* Instantaneous mode */ + if (!strcmp("-I", argv[i])) { + printf("Instantaneous BottleNeck\n"); + CodingMode = 1; + } + + /* Set (initial) bottleneck value */ + if (!strcmp("-INITRATE", argv[i])) { + rateBPS = atoi(argv[i + 1]); + setControlBWE = 1; + if ((rateBPS < 10000) || (rateBPS > 32000)) { + printf( + "\n%d is not a initial rate. Valid values are in the range 10000 " + "to 32000.\n", + rateBPS); + exit(0); + } + printf("New initial rate: %d\n", rateBPS); + i++; + } + + /* Set (initial) framelength */ + if (!strcmp("-FL", argv[i])) { + framesize = atoi(argv[i + 1]); + if ((framesize != 30) && (framesize != 60)) { + printf( + "\n%d is not a valid frame length. Valid length are 30 and 60 " + "msec.\n", + framesize); + exit(0); + } + setControlBWE = 1; + printf("Frame Length: %d\n", framesize); + i++; + } + + /* Fixed frame length */ + if (!strcmp("-FIXED_FL", argv[i])) { + fixedFL = 1; + setControlBWE = 1; + printf("Fixed Frame Length\n"); + } + + /* Set maximum allowed payload size in bytes */ + if (!strcmp("-MAX", argv[i])) { + payloadSize = atoi(argv[i + 1]); + printf("Maximum Payload Size: %d\n", payloadSize); + i++; + } + + /* Set maximum rate in bytes */ + if (!strcmp("-MAXRATE", argv[i])) { + payloadRate = atoi(argv[i + 1]); + printf("Maximum Rate in kbps: %d\n", payloadRate); + i++; + } + + /* Test of fault scenarious */ + if (!strcmp("-F", argv[i])) { + testNum = atoi(argv[i + 1]); + printf("Fault test: %d\n", testNum); + if (testNum < 1 || testNum > 10) { + printf( + "\n%d is not a valid Fault Scenario number. Valid Fault Scenarios " + "are numbered 1-10.\n", + testNum); + exit(0); + } + i++; + } + + /* Packet loss test */ + if (!strcmp("-PL", argv[i])) { + if (isdigit(*argv[i + 1])) { + packetLossPercent = atoi(argv[i + 1]); + if ((packetLossPercent < 0) | (packetLossPercent > 100)) { + printf("\nInvalid packet loss perentage \n"); + exit(0); } - case 32: - { - printf("iSAC Supper-Wideband.\n"); - samplesIn10Ms = SWBFRAMESAMPLES_10ms; - break; + if (packetLossPercent > 0) { + printf("Simulating %d %% of independent packet loss\n", + packetLossPercent); + } else { + printf("\nNo Packet Loss Is Simulated \n"); } + } else { + plFile = fopen(argv[i + 1], "rb"); + if (plFile == NULL) { + printf("\n couldn't open the frameloss file: %s\n", argv[i + 1]); + exit(0); + } + printf("Simulating packet loss through the given channel file: %s\n", + argv[i + 1]); + } + i++; + } + + /* Random packetlosses */ + if (!strcmp("-rnd", argv[i])) { + srand((unsigned int)time(NULL)); + printf("Random pattern in lossed packets \n"); + } + + /* Use gns file */ + if (!strcmp("-G", argv[i])) { + sscanf(argv[i + 1], "%s", gns_file); + fp_gns = fopen(gns_file, "rb"); + if (fp_gns == NULL) { + printf("Cannot read file %s.\n", gns_file); + exit(0); + } + i++; + } + + // make it with '-B' + /* Get Bottleneck value */ + if (!strcmp("-B", argv[i])) { + i++; + bottleneck = atoi(argv[i]); + if (bottleneck == 0) { + sscanf(argv[i], "%s", bottleneck_file); + f_bn = fopen(bottleneck_file, "rb"); + if (f_bn == NULL) { + printf( + "Error No value provided for BottleNeck and cannot read file " + "%s.\n", + bottleneck_file); + exit(0); + } else { + printf("reading bottleneck rates from file %s\n\n", bottleneck_file); + if (fscanf(f_bn, "%d", &bottleneck) == EOF) { + /* Set pointer to beginning of file */ + fseek(f_bn, 0L, SEEK_SET); + if (fscanf(f_bn, "%d", &bottleneck) == EOF) { + exit(0); + } + } + + /* Bottleneck is a cosine function + * Matlab code for writing the bottleneck file: + * BottleNeck_10ms = 20e3 + 10e3 * cos((0:5999)/5999*2*pi); + * fid = fopen('bottleneck.txt', 'wb'); + * fprintf(fid, '%d\n', BottleNeck_10ms); fclose(fid); + */ + } + } else { + printf("\nfixed bottleneck rate of %d bits/s\n\n", bottleneck); + } + } + /* Run Conference Engine APIs */ + // Do not test it in the first release + // + // if(!strcmp ("-CE", argv[i])) + // { + // testCE = atoi(argv[i + 1]); + // if(testCE==1) + // { + // i++; + // scale = (float)atof( argv[i+1] ); + // } + // else if(testCE == 2) + // { + // printf("\nCE-test 2 (transcoding) not implemented.\n"); + // exit(0); + // } + // else if(testCE < 1 || testCE > 3) + // { + // printf("\n%d is not a valid CE-test number. Valid CE tests + // are 1-3.\n", testCE); + // exit(0); + // } + // printf("CE-test number: %d\n", testCE); + // i++; + // } + } + + if (CodingMode == 0) { + printf("\nAdaptive BottleNeck\n"); + } + + switch (sampFreqKHz) { + case 16: { + printf("iSAC Wideband.\n"); + samplesIn10Ms = FRAMESAMPLES_10ms; + break; + } + case 32: { + printf("iSAC Supper-Wideband.\n"); + samplesIn10Ms = SWBFRAMESAMPLES_10ms; + break; + } default: - printf("Unsupported sampling frequency %d kHz", sampFreqKHz); - exit(0); + printf("Unsupported sampling frequency %d kHz", sampFreqKHz); + exit(0); + } + + /* Get Input and Output files */ + sscanf(argv[argc - 2], "%s", inname); + sscanf(argv[argc - 1], "%s", outname); + printf("\nInput file: %s\n", inname); + printf("Output file: %s\n\n", outname); + if ((inp = fopen(inname, "rb")) == NULL) { + printf(" Error iSAC Cannot read file %s.\n", inname); + cout << flush; + exit(1); + } + + if ((outp = fopen(outname, "wb")) == NULL) { + printf(" Error iSAC Cannot write file %s.\n", outname); + cout << flush; + getc(stdin); + exit(1); + } + if (VADusage) { + if ((vadp = fopen(vadfile, "rb")) == NULL) { + printf(" Error iSAC Cannot read file %s.\n", vadfile); + cout << flush; + exit(1); } + } + if ((bandwidthp = fopen("bwe.pcm", "wb")) == NULL) { + printf(" Error iSAC Cannot read file %s.\n", "bwe.pcm"); + cout << flush; + exit(1); + } + starttime = clock() / (double)CLOCKS_PER_SEC; /* Runtime statistics */ + /* Initialize the ISAC and BN structs */ + if (testNum != 8) { + if (!useAssign) { + err = WebRtcIsac_Create(&ISAC_main_inst); + WebRtcIsac_SetEncSampRate(ISAC_main_inst, sampFreqKHz * 1000); + WebRtcIsac_SetDecSampRate(ISAC_main_inst, + sampFreqKHz >= 32 ? 32000 : 16000); + } else { + /* Test the Assign functions */ + int sss; + void* ppp; + err = WebRtcIsac_AssignSize(&sss); + ppp = malloc(sss); + err = WebRtcIsac_Assign(&ISAC_main_inst, ppp); + WebRtcIsac_SetEncSampRate(ISAC_main_inst, sampFreqKHz * 1000); + WebRtcIsac_SetDecSampRate(ISAC_main_inst, + sampFreqKHz >= 32 ? 32000 : 16000); + } + /* Error check */ + if (err < 0) { + printf("\n\n Error in create.\n\n"); + cout << flush; + exit(EXIT_FAILURE); + } + } + BN_data.arrival_time = 0; + BN_data.sample_count = 0; + BN_data.rtp_number = 0; - /* Get Input and Output files */ - sscanf(argv[argc-2], "%s", inname); - sscanf(argv[argc-1], "%s", outname); - printf("\nInput file: %s\n", inname); - printf("Output file: %s\n\n", outname); - if((inp = fopen(inname,"rb")) == NULL) - { - printf(" Error iSAC Cannot read file %s.\n", inname); + /* Initialize encoder and decoder */ + framecnt = 0; + endfile = 0; + + if (doTransCoding) { + WebRtcIsac_Create(&decoderTransCoding); + WebRtcIsac_SetEncSampRate(decoderTransCoding, sampFreqKHz * 1000); + WebRtcIsac_SetDecSampRate(decoderTransCoding, + sampFreqKHz >= 32 ? 32000 : 16000); + WebRtcIsac_DecoderInit(decoderTransCoding); + transCodingFile = fopen(transCodingFileName, "wb"); + if (transCodingFile == NULL) { + printf("Could not open %s to output trans-coding.\n", + transCodingFileName); + exit(0); + } + strcat(transCodingFileName, ".bit"); + transcodingBitstream = fopen(transCodingFileName, "wb"); + if (transcodingBitstream == NULL) { + printf("Could not open %s to write the bit-stream of transcoder.\n", + transCodingFileName); + exit(0); + } + } + + if (testNum != 1) { + if (WebRtcIsac_EncoderInit(ISAC_main_inst, CodingMode) < 0) { + printf("Error could not initialize the encoder \n"); + cout << flush; + return 0; + } + } + if (testNum != 2) { + if (WebRtcIsac_DecoderInit(ISAC_main_inst) < 0) { + printf("Error could not initialize the decoder \n"); + cout << flush; + return 0; + } + } + if (CodingMode == 1) { + err = WebRtcIsac_Control(ISAC_main_inst, bottleneck, framesize); + if (err < 0) { + /* exit if returned with error */ + errtype = WebRtcIsac_GetErrorCode(ISAC_main_inst); + printf("\n\n Error in initialization (control): %d.\n\n", errtype); + cout << flush; + if (testNum == 0) { + exit(EXIT_FAILURE); + } + } + } + + if ((setControlBWE) && (CodingMode == 0)) { + err = WebRtcIsac_ControlBwe(ISAC_main_inst, rateBPS, framesize, fixedFL); + if (err < 0) { + /* exit if returned with error */ + errtype = WebRtcIsac_GetErrorCode(ISAC_main_inst); + + printf("\n\n Error in Control BWE: %d.\n\n", errtype); + cout << flush; + exit(EXIT_FAILURE); + } + } + + if (payloadSize != 0) { + err = WebRtcIsac_SetMaxPayloadSize(ISAC_main_inst, payloadSize); + if (err < 0) { + /* exit if returned with error */ + errtype = WebRtcIsac_GetErrorCode(ISAC_main_inst); + printf("\n\n Error in SetMaxPayloadSize: %d.\n\n", errtype); + cout << flush; + exit(EXIT_FAILURE); + } + } + if (payloadRate != 0) { + err = WebRtcIsac_SetMaxRate(ISAC_main_inst, payloadRate); + if (err < 0) { + /* exit if returned with error */ + errtype = WebRtcIsac_GetErrorCode(ISAC_main_inst); + printf("\n\n Error in SetMaxRateInBytes: %d.\n\n", errtype); + cout << flush; + exit(EXIT_FAILURE); + } + } + + *speechType = 1; + + cout << "\n" << flush; + + length_file = 0; + int16_t bnIdxTC = 0; + int16_t jitterInfoTC = 0; + while (endfile == 0) { + /* Call init functions at random, fault test number 7 */ + if (testNum == 7 && (rand() % 2 == 0)) { + err = WebRtcIsac_EncoderInit(ISAC_main_inst, CodingMode); + /* Error check */ + if (err < 0) { + errtype = WebRtcIsac_GetErrorCode(ISAC_main_inst); + printf("\n\n Error in encoderinit: %d.\n\n", errtype); cout << flush; - exit(1); - } + } - if((outp = fopen(outname,"wb")) == NULL) - { - printf(" Error iSAC Cannot write file %s.\n", outname); + err = WebRtcIsac_DecoderInit(ISAC_main_inst); + /* Error check */ + if (err < 0) { + errtype = WebRtcIsac_GetErrorCode(ISAC_main_inst); + printf("\n\n Error in decoderinit: %d.\n\n", errtype); cout << flush; - getc(stdin); - exit(1); - } - if(VADusage) - { - if((vadp = fopen(vadfile,"rb")) == NULL) - { - printf(" Error iSAC Cannot read file %s.\n", vadfile); - cout << flush; - exit(1); - } - } - - if((bandwidthp = fopen("bwe.pcm","wb")) == NULL) - { - printf(" Error iSAC Cannot read file %s.\n", "bwe.pcm"); - cout << flush; - exit(1); + } } + cur_framesmpls = 0; + while (1) { + /* Read 10 ms speech block */ + endfile = readframe(shortdata, inp, samplesIn10Ms); - starttime = clock()/(double)CLOCKS_PER_SEC; /* Runtime statistics */ + if (endfile) { + numFileLoop++; + if (numFileLoop < totFileLoop) { + rewind(inp); + framecnt = 0; + fprintf(stderr, "\n"); + endfile = readframe(shortdata, inp, samplesIn10Ms); + } + } - /* Initialize the ISAC and BN structs */ - if(testNum != 8) - { - if(!useAssign) - { - err =WebRtcIsac_Create(&ISAC_main_inst); - WebRtcIsac_SetEncSampRate(ISAC_main_inst, sampFreqKHz * 1000); - WebRtcIsac_SetDecSampRate(ISAC_main_inst, sampFreqKHz >= 32 ? - 32000 : 16000); - } - else - { - /* Test the Assign functions */ - int sss; - void *ppp; - err = WebRtcIsac_AssignSize(&sss); - ppp = malloc(sss); - err = WebRtcIsac_Assign(&ISAC_main_inst, ppp); - WebRtcIsac_SetEncSampRate(ISAC_main_inst, sampFreqKHz * 1000); - WebRtcIsac_SetDecSampRate(ISAC_main_inst, sampFreqKHz >= 32 ? - 32000 : 16000); - } - /* Error check */ - if(err < 0) - { - printf("\n\n Error in create.\n\n"); - cout << flush; - exit(EXIT_FAILURE); - } - } - BN_data.arrival_time = 0; - BN_data.sample_count = 0; - BN_data.rtp_number = 0; + if (testNum == 7) { + srand((unsigned int)time(NULL)); + } - /* Initialize encoder and decoder */ - framecnt= 0; - endfile = 0; + /* iSAC encoding */ + if (!(testNum == 3 && framecnt == 0)) { + stream_len = + WebRtcIsac_Encode(ISAC_main_inst, shortdata, (uint8_t*)streamdata); + if ((payloadSize != 0) && (stream_len > payloadSize)) { + if (testNum == 0) { + printf("\n\n"); + } - if(doTransCoding) - { - WebRtcIsac_Create(&decoderTransCoding); - WebRtcIsac_SetEncSampRate(decoderTransCoding, sampFreqKHz * 1000); - WebRtcIsac_SetDecSampRate(decoderTransCoding, sampFreqKHz >= 32 ? - 32000 : 16000); - WebRtcIsac_DecoderInit(decoderTransCoding); - transCodingFile = fopen(transCodingFileName, "wb"); - if(transCodingFile == NULL) - { - printf("Could not open %s to output trans-coding.\n", transCodingFileName); - exit(0); + printf("\nError: Streamsize out of range %d\n", + stream_len - payloadSize); + cout << flush; } - strcat(transCodingFileName, ".bit"); - transcodingBitstream = fopen(transCodingFileName, "wb"); - if(transcodingBitstream == NULL) - { - printf("Could not open %s to write the bit-stream of transcoder.\n", transCodingFileName); - exit(0); - } - } - if(testNum != 1) - { - if(WebRtcIsac_EncoderInit(ISAC_main_inst, CodingMode) < 0) - { - printf("Error could not initialize the encoder \n"); - cout << flush; - return 0; - } - } - if(testNum != 2) - { - if(WebRtcIsac_DecoderInit(ISAC_main_inst) < 0) - { - printf("Error could not initialize the decoder \n"); - cout << flush; - return 0; - } - } - if(CodingMode == 1) - { - err = WebRtcIsac_Control(ISAC_main_inst, bottleneck, framesize); - if(err < 0) - { - /* exit if returned with error */ - errtype=WebRtcIsac_GetErrorCode(ISAC_main_inst); - printf("\n\n Error in initialization (control): %d.\n\n", errtype); - cout << flush; - if(testNum == 0) - { - exit(EXIT_FAILURE); + WebRtcIsac_GetUplinkBw(ISAC_main_inst, &sendBN); + + if (stream_len > 0) { + if (doTransCoding) { + int16_t indexStream; + uint8_t auxUW8; + + /************************* Main Transcoding stream + * *******************************/ + WebRtcIsac_GetDownLinkBwIndex(ISAC_main_inst, &bnIdxTC, + &jitterInfoTC); + streamLenTransCoding = WebRtcIsac_GetNewBitStream( + ISAC_main_inst, bnIdxTC, jitterInfoTC, rateTransCoding, + streamDataTransCoding, false); + if (streamLenTransCoding < 0) { + fprintf(stderr, "Error in trans-coding\n"); + exit(0); } - } - } - - if((setControlBWE) && (CodingMode == 0)) - { - err = WebRtcIsac_ControlBwe(ISAC_main_inst, rateBPS, framesize, fixedFL); - if(err < 0) - { - /* exit if returned with error */ - errtype=WebRtcIsac_GetErrorCode(ISAC_main_inst); - - printf("\n\n Error in Control BWE: %d.\n\n", errtype); - cout << flush; - exit(EXIT_FAILURE); - } - } - - if(payloadSize != 0) - { - err = WebRtcIsac_SetMaxPayloadSize(ISAC_main_inst, payloadSize); - if(err < 0) - { - /* exit if returned with error */ - errtype=WebRtcIsac_GetErrorCode(ISAC_main_inst); - printf("\n\n Error in SetMaxPayloadSize: %d.\n\n", errtype); - cout << flush; - exit(EXIT_FAILURE); - } - } - if(payloadRate != 0) - { - err = WebRtcIsac_SetMaxRate(ISAC_main_inst, payloadRate); - if(err < 0) - { - /* exit if returned with error */ - errtype=WebRtcIsac_GetErrorCode(ISAC_main_inst); - printf("\n\n Error in SetMaxRateInBytes: %d.\n\n", errtype); - cout << flush; - exit(EXIT_FAILURE); - } - } - - *speechType = 1; - - cout << "\n" << flush; - - length_file = 0; - int16_t bnIdxTC = 0; - int16_t jitterInfoTC = 0; - while (endfile == 0) - { - /* Call init functions at random, fault test number 7 */ - if(testNum == 7 && (rand()%2 == 0)) - { - err = WebRtcIsac_EncoderInit(ISAC_main_inst, CodingMode); - /* Error check */ - if(err < 0) - { - errtype=WebRtcIsac_GetErrorCode(ISAC_main_inst); - printf("\n\n Error in encoderinit: %d.\n\n", errtype); - cout << flush; + auxUW8 = (uint8_t)(((streamLenTransCoding & 0xFF00) >> 8) & 0x00FF); + if (fwrite(&auxUW8, sizeof(uint8_t), 1, transcodingBitstream) != + 1) { + return -1; } - err = WebRtcIsac_DecoderInit(ISAC_main_inst); - /* Error check */ - if(err < 0) - { - errtype=WebRtcIsac_GetErrorCode(ISAC_main_inst); - printf("\n\n Error in decoderinit: %d.\n\n", errtype); - cout << flush; - } - } - - cur_framesmpls = 0; - while (1) - { - /* Read 10 ms speech block */ - endfile = readframe(shortdata, inp, samplesIn10Ms); - - if(endfile) - { - numFileLoop++; - if(numFileLoop < totFileLoop) - { - rewind(inp); - framecnt = 0; - fprintf(stderr, "\n"); - endfile = readframe(shortdata, inp, samplesIn10Ms); - } + auxUW8 = (uint8_t)(streamLenTransCoding & 0x00FF); + if (fwrite(&auxUW8, sizeof(uint8_t), 1, transcodingBitstream) != + 1) { + return -1; } - if(testNum == 7) - { - srand((unsigned int)time(NULL)); - } - - /* iSAC encoding */ - if(!(testNum == 3 && framecnt == 0)) - { - stream_len = WebRtcIsac_Encode(ISAC_main_inst, - shortdata, - (uint8_t*)streamdata); - if((payloadSize != 0) && (stream_len > payloadSize)) - { - if(testNum == 0) - { - printf("\n\n"); - } - - printf("\nError: Streamsize out of range %d\n", stream_len - payloadSize); - cout << flush; - } - - WebRtcIsac_GetUplinkBw(ISAC_main_inst, &sendBN); - - if(stream_len>0) - { - if(doTransCoding) - { - int16_t indexStream; - uint8_t auxUW8; - - /************************* Main Transcoding stream *******************************/ - WebRtcIsac_GetDownLinkBwIndex(ISAC_main_inst, &bnIdxTC, &jitterInfoTC); - streamLenTransCoding = WebRtcIsac_GetNewBitStream( - ISAC_main_inst, - bnIdxTC, - jitterInfoTC, - rateTransCoding, - streamDataTransCoding, - false); - if(streamLenTransCoding < 0) - { - fprintf(stderr, "Error in trans-coding\n"); - exit(0); - } - auxUW8 = (uint8_t)(((streamLenTransCoding & 0xFF00) >> 8) & 0x00FF); - if (fwrite(&auxUW8, sizeof(uint8_t), 1, - transcodingBitstream) != 1) { - return -1; - } - - auxUW8 = (uint8_t)(streamLenTransCoding & 0x00FF); - if (fwrite(&auxUW8, sizeof(uint8_t), - 1, transcodingBitstream) != 1) { - return -1; - } - - if (fwrite(streamDataTransCoding, - sizeof(uint8_t), - streamLenTransCoding, - transcodingBitstream) != - static_cast(streamLenTransCoding)) { - return -1; - } - - WebRtcIsac_ReadBwIndex(streamDataTransCoding, - &indexStream); - if (indexStream != bnIdxTC) { - fprintf(stderr, "Error in inserting Bandwidth index into transcoding stream.\n"); - exit(0); - } - numTransCodingBytes += streamLenTransCoding; - } - } - } - else - { - break; + if (fwrite(streamDataTransCoding, sizeof(uint8_t), + streamLenTransCoding, transcodingBitstream) != + static_cast(streamLenTransCoding)) { + return -1; } - if(stream_len < 0) - { - /* exit if returned with error */ - errtype=WebRtcIsac_GetErrorCode(ISAC_main_inst); - printf("\n\nError in encoder: %d.\n\n", errtype); - cout << flush; - } - cur_framesmpls += samplesIn10Ms; - /* exit encoder loop if the encoder returned a bitstream */ - if(stream_len != 0) break; - } - - /* read next bottleneck rate */ - if(f_bn != NULL) - { - if(fscanf(f_bn, "%d", &bottleneck) == EOF) - { - /* Set pointer to beginning of file */ - fseek(f_bn, 0L, SEEK_SET); - if (fscanf(f_bn, "%d", &bottleneck) == EOF) { - exit(0); - } + WebRtcIsac_ReadBwIndex(streamDataTransCoding, &indexStream); + if (indexStream != bnIdxTC) { + fprintf(stderr, + "Error in inserting Bandwidth index into transcoding " + "stream.\n"); + exit(0); } - if(CodingMode == 1) - { - WebRtcIsac_Control(ISAC_main_inst, bottleneck, framesize); - } - } - - length_file += cur_framesmpls; - if(cur_framesmpls == (3 * samplesIn10Ms)) - { - maxStreamLen30 = (stream_len > maxStreamLen30)? stream_len:maxStreamLen30; - } - else - { - maxStreamLen60 = (stream_len > maxStreamLen60)? stream_len:maxStreamLen60; - } - - if(!lostFrame) - { - lostFrame = ((rand()%100) < packetLossPercent); - } - else - { - lostFrame = 0; - } - - // RED. - if(lostFrame) - { - stream_len = WebRtcIsac_GetRedPayload( - ISAC_main_inst, reinterpret_cast(streamdata)); - - if(doTransCoding) - { - streamLenTransCoding = WebRtcIsac_GetNewBitStream( - ISAC_main_inst, - bnIdxTC, - jitterInfoTC, - rateTransCoding, - streamDataTransCoding, - true); - if(streamLenTransCoding < 0) - { - fprintf(stderr, "Error in RED trans-coding\n"); - exit(0); - } - } - } - - /* make coded sequence to short be inreasing */ - /* the length the decoder expects */ - if(testNum == 4) - { - stream_len += 10; - } - - /* make coded sequence to long be decreasing */ - /* the length the decoder expects */ - if(testNum == 5) - { - stream_len -= 10; - } - - if(testNum == 6) - { - srand((unsigned int)time(NULL)); - for(i = 0; i < stream_len; i++) - { - streamdata[i] = rand(); - } - } - - if(VADusage){ - readframe(vaddata, vadp, samplesIn10Ms*3); - } - - /* simulate packet handling through NetEq and the modem */ - if(!(testNum == 3 && framecnt == 0)) - { - get_arrival_time(cur_framesmpls, stream_len, bottleneck, &BN_data, - sampFreqKHz*1000, sampFreqKHz*1000); - } - - if(VADusage && (framecnt>10 && vaddata[0]==0)) - { - BN_data.rtp_number--; - } - else - { - /* Error test number 10, garbage data */ - if(testNum == 10) - { - /* Test to run decoder with garbage data */ - for(i = 0; i < stream_len; i++) - { - streamdata[i] = (short) (streamdata[i]) + (short) rand(); - } - } - - if(testNum != 9) - { - err = WebRtcIsac_UpdateBwEstimate( - ISAC_main_inst, - reinterpret_cast(streamdata), - stream_len, - BN_data.rtp_number, - BN_data.sample_count, - BN_data.arrival_time); - - if(err < 0) - { - /* exit if returned with error */ - errtype=WebRtcIsac_GetErrorCode(ISAC_main_inst); - if(testNum == 0) - { - printf("\n\n"); - } - - printf("Error: in decoder: %d.", errtype); - cout << flush; - if(testNum == 0) - { - printf("\n\n"); - } - - } - } - - /* Call getFramelen, only used here for function test */ - err = WebRtcIsac_ReadFrameLen( - ISAC_main_inst, - reinterpret_cast(streamdata), - &FL); - if(err < 0) - { - /* exit if returned with error */ - errtype=WebRtcIsac_GetErrorCode(ISAC_main_inst); - if(testNum == 0) - { - printf("\n\n"); - } - printf(" Error: in getFrameLen %d.", errtype); - cout << flush; - if(testNum == 0) - { - printf("\n\n"); - } - } - - // iSAC decoding - - if(lostFrame) - { - declen = WebRtcIsac_DecodeRcu( - ISAC_main_inst, - reinterpret_cast(streamdata), - stream_len, - decoded, - speechType); - - if(doTransCoding) - { - declenTC = WebRtcIsac_DecodeRcu( - decoderTransCoding, - streamDataTransCoding, - streamLenTransCoding, - decodedTC, - speechType); - } - } - else - { - declen = WebRtcIsac_Decode( - ISAC_main_inst, - reinterpret_cast(streamdata), - stream_len, - decoded, - speechType); - if(doTransCoding) - { - declenTC = WebRtcIsac_Decode( - decoderTransCoding, - streamDataTransCoding, - streamLenTransCoding, - decodedTC, - speechType); - } - } - - if(declen < 0) - { - /* exit if returned with error */ - errtype=WebRtcIsac_GetErrorCode(ISAC_main_inst); - if(testNum == 0) - { - printf("\n\n"); - } - printf(" Error: in decoder %d.", errtype); - cout << flush; - if(testNum == 0) - { - printf("\n\n"); - } - } - - if(declenTC < 0) - { - if(testNum == 0) - { - printf("\n\n"); - } - printf(" Error: in decoding the transcoded stream"); - cout << flush; - if(testNum == 0) - { - printf("\n\n"); - } - - } - } - /* Write decoded speech frame to file */ - if((declen > 0) && (numFileLoop == 0)) - { - if (fwrite(decoded, sizeof(int16_t), declen, - outp) != static_cast(declen)) { - return -1; + numTransCodingBytes += streamLenTransCoding; } } + } else { + break; + } - if((declenTC > 0) && (numFileLoop == 0)) - { - if (fwrite(decodedTC, sizeof(int16_t), declen, - transCodingFile) != static_cast(declen)) { - return -1; + if (stream_len < 0) { + /* exit if returned with error */ + errtype = WebRtcIsac_GetErrorCode(ISAC_main_inst); + printf("\n\nError in encoder: %d.\n\n", errtype); + cout << flush; + } + cur_framesmpls += samplesIn10Ms; + /* exit encoder loop if the encoder returned a bitstream */ + if (stream_len != 0) + break; + } + + /* read next bottleneck rate */ + if (f_bn != NULL) { + if (fscanf(f_bn, "%d", &bottleneck) == EOF) { + /* Set pointer to beginning of file */ + fseek(f_bn, 0L, SEEK_SET); + if (fscanf(f_bn, "%d", &bottleneck) == EOF) { + exit(0); + } + } + if (CodingMode == 1) { + WebRtcIsac_Control(ISAC_main_inst, bottleneck, framesize); + } + } + + length_file += cur_framesmpls; + if (cur_framesmpls == (3 * samplesIn10Ms)) { + maxStreamLen30 = + (stream_len > maxStreamLen30) ? stream_len : maxStreamLen30; + } else { + maxStreamLen60 = + (stream_len > maxStreamLen60) ? stream_len : maxStreamLen60; + } + + if (!lostFrame) { + lostFrame = ((rand() % 100) < packetLossPercent); + } else { + lostFrame = 0; + } + + // RED. + if (lostFrame) { + stream_len = WebRtcIsac_GetRedPayload( + ISAC_main_inst, reinterpret_cast(streamdata)); + + if (doTransCoding) { + streamLenTransCoding = WebRtcIsac_GetNewBitStream( + ISAC_main_inst, bnIdxTC, jitterInfoTC, rateTransCoding, + streamDataTransCoding, true); + if (streamLenTransCoding < 0) { + fprintf(stderr, "Error in RED trans-coding\n"); + exit(0); + } + } + } + + /* make coded sequence to short be inreasing */ + /* the length the decoder expects */ + if (testNum == 4) { + stream_len += 10; + } + + /* make coded sequence to long be decreasing */ + /* the length the decoder expects */ + if (testNum == 5) { + stream_len -= 10; + } + + if (testNum == 6) { + srand((unsigned int)time(NULL)); + for (i = 0; i < stream_len; i++) { + streamdata[i] = rand(); + } + } + + if (VADusage) { + readframe(vaddata, vadp, samplesIn10Ms * 3); + } + + /* simulate packet handling through NetEq and the modem */ + if (!(testNum == 3 && framecnt == 0)) { + get_arrival_time(cur_framesmpls, stream_len, bottleneck, &BN_data, + sampFreqKHz * 1000, sampFreqKHz * 1000); + } + + if (VADusage && (framecnt > 10 && vaddata[0] == 0)) { + BN_data.rtp_number--; + } else { + /* Error test number 10, garbage data */ + if (testNum == 10) { + /* Test to run decoder with garbage data */ + for (i = 0; i < stream_len; i++) { + streamdata[i] = (short)(streamdata[i]) + (short)rand(); + } + } + + if (testNum != 9) { + err = WebRtcIsac_UpdateBwEstimate( + ISAC_main_inst, reinterpret_cast(streamdata), + stream_len, BN_data.rtp_number, BN_data.sample_count, + BN_data.arrival_time); + + if (err < 0) { + /* exit if returned with error */ + errtype = WebRtcIsac_GetErrorCode(ISAC_main_inst); + if (testNum == 0) { + printf("\n\n"); + } + + printf("Error: in decoder: %d.", errtype); + cout << flush; + if (testNum == 0) { + printf("\n\n"); } } + } + /* Call getFramelen, only used here for function test */ + err = WebRtcIsac_ReadFrameLen( + ISAC_main_inst, reinterpret_cast(streamdata), &FL); + if (err < 0) { + /* exit if returned with error */ + errtype = WebRtcIsac_GetErrorCode(ISAC_main_inst); + if (testNum == 0) { + printf("\n\n"); + } + printf(" Error: in getFrameLen %d.", errtype); + cout << flush; + if (testNum == 0) { + printf("\n\n"); + } + } - fprintf(stderr, "\rframe = %5d ", framecnt); - fflush(stderr); - framecnt++; + // iSAC decoding - /* Error test number 10, garbage data */ - //if(testNum == 10) - //{ - // /* Test to run decoder with garbage data */ - // if( (seedfile = fopen(SEED_FILE, "a+t") ) == NULL ) - // { - // fprintf(stderr, "Error: Could not open file %s\n", SEED_FILE); - // } - // else - // { - // fprintf(seedfile, "ok\n\n"); - // fclose(seedfile); - // } - //} - /* Error test number 10, garbage data */ - //if(testNum == 10) - //{ - // /* Test to run decoder with garbage data */ - // for ( i = 0; i < stream_len; i++) - // { - // streamdata[i] = (short) (streamdata[i] + (short) rand()); - // } - //} + if (lostFrame) { + declen = WebRtcIsac_DecodeRcu( + ISAC_main_inst, reinterpret_cast(streamdata), + stream_len, decoded, speechType); + if (doTransCoding) { + declenTC = + WebRtcIsac_DecodeRcu(decoderTransCoding, streamDataTransCoding, + streamLenTransCoding, decodedTC, speechType); + } + } else { + declen = WebRtcIsac_Decode(ISAC_main_inst, + reinterpret_cast(streamdata), + stream_len, decoded, speechType); + if (doTransCoding) { + declenTC = + WebRtcIsac_Decode(decoderTransCoding, streamDataTransCoding, + streamLenTransCoding, decodedTC, speechType); + } + } - totalsmpls += declen; - totalbits += 8 * stream_len; + if (declen < 0) { + /* exit if returned with error */ + errtype = WebRtcIsac_GetErrorCode(ISAC_main_inst); + if (testNum == 0) { + printf("\n\n"); + } + printf(" Error: in decoder %d.", errtype); + cout << flush; + if (testNum == 0) { + printf("\n\n"); + } + } + + if (declenTC < 0) { + if (testNum == 0) { + printf("\n\n"); + } + printf(" Error: in decoding the transcoded stream"); + cout << flush; + if (testNum == 0) { + printf("\n\n"); + } + } + } + /* Write decoded speech frame to file */ + if ((declen > 0) && (numFileLoop == 0)) { + if (fwrite(decoded, sizeof(int16_t), declen, outp) != + static_cast(declen)) { + return -1; + } + } + + if ((declenTC > 0) && (numFileLoop == 0)) { + if (fwrite(decodedTC, sizeof(int16_t), declen, transCodingFile) != + static_cast(declen)) { + return -1; + } + } + + fprintf(stderr, "\rframe = %5d ", framecnt); + fflush(stderr); + framecnt++; + + /* Error test number 10, garbage data */ + // if(testNum == 10) + //{ + // /* Test to run decoder with garbage data */ + // if( (seedfile = fopen(SEED_FILE, "a+t") ) == NULL ) + // { + // fprintf(stderr, "Error: Could not open file %s\n", SEED_FILE); + // } + // else + // { + // fprintf(seedfile, "ok\n\n"); + // fclose(seedfile); + // } + //} + /* Error test number 10, garbage data */ + // if(testNum == 10) + //{ + // /* Test to run decoder with garbage data */ + // for ( i = 0; i < stream_len; i++) + // { + // streamdata[i] = (short) (streamdata[i] + (short) rand()); + // } + //} + + totalsmpls += declen; + totalbits += 8 * stream_len; #ifdef _DEBUG - kbps = ((double) sampFreqKHz * 1000.) / ((double) cur_framesmpls) * 8.0 * stream_len / 1000.0;// kbits/s - fy = fopen("bit_rate.dat", "a"); - fprintf(fy, "Frame %i = %0.14f\n", framecnt, kbps); - fclose(fy); + kbps = ((double)sampFreqKHz * 1000.) / ((double)cur_framesmpls) * 8.0 * + stream_len / 1000.0; // kbits/s + fy = fopen("bit_rate.dat", "a"); + fprintf(fy, "Frame %i = %0.14f\n", framecnt, kbps); + fclose(fy); #endif /* _DEBUG */ + } + printf("\n"); + printf("total bits = %d bits\n", totalbits); + printf("measured average bitrate = %0.3f kbits/s\n", + (double)totalbits * (sampFreqKHz) / totalsmpls); + if (doTransCoding) { + printf("Transcoding average bit-rate = %0.3f kbps\n", + (double)numTransCodingBytes * 8.0 * (sampFreqKHz) / totalsmpls); + fclose(transCodingFile); + } + printf("\n"); - } - printf("\n"); - printf("total bits = %d bits\n", totalbits); - printf("measured average bitrate = %0.3f kbits/s\n", - (double)totalbits *(sampFreqKHz) / totalsmpls); - if(doTransCoding) - { - printf("Transcoding average bit-rate = %0.3f kbps\n", - (double)numTransCodingBytes * 8.0 *(sampFreqKHz) / totalsmpls); - fclose(transCodingFile); - } - printf("\n"); + /* Runtime statistics */ + runtime = (double)(clock() / (double)CLOCKS_PER_SEC - starttime); + length_file = length_file / (sampFreqKHz * 1000.); - /* Runtime statistics */ - runtime = (double)(clock()/(double)CLOCKS_PER_SEC-starttime); - length_file = length_file /(sampFreqKHz * 1000.); + printf("\n\nLength of speech file: %.1f s\n", length_file); + printf("Time to run iSAC: %.2f s (%.2f %% of realtime)\n\n", runtime, + (100 * runtime / length_file)); - printf("\n\nLength of speech file: %.1f s\n", length_file); - printf("Time to run iSAC: %.2f s (%.2f %% of realtime)\n\n", runtime, (100*runtime/length_file)); + if (maxStreamLen30 != 0) { + printf("Maximum payload size 30ms Frames %d bytes (%0.3f kbps)\n", + maxStreamLen30, maxStreamLen30 * 8 / 30.); + } + if (maxStreamLen60 != 0) { + printf("Maximum payload size 60ms Frames %d bytes (%0.3f kbps)\n", + maxStreamLen60, maxStreamLen60 * 8 / 60.); + } + // fprintf(stderr, "\n"); - if(maxStreamLen30 != 0) - { - printf("Maximum payload size 30ms Frames %d bytes (%0.3f kbps)\n", - maxStreamLen30, + fprintf(stderr, " %.1f s", length_file); + fprintf(stderr, " %0.1f kbps", + (double)totalbits * (sampFreqKHz) / totalsmpls); + if (maxStreamLen30 != 0) { + fprintf(stderr, " plmax-30ms %d bytes (%0.0f kbps)", maxStreamLen30, maxStreamLen30 * 8 / 30.); - } - if(maxStreamLen60 != 0) - { - printf("Maximum payload size 60ms Frames %d bytes (%0.3f kbps)\n", - maxStreamLen60, + } + if (maxStreamLen60 != 0) { + fprintf(stderr, " plmax-60ms %d bytes (%0.0f kbps)", maxStreamLen60, maxStreamLen60 * 8 / 60.); - } - //fprintf(stderr, "\n"); + } + if (doTransCoding) { + fprintf(stderr, " transcoding rate %.0f kbps", + (double)numTransCodingBytes * 8.0 * (sampFreqKHz) / totalsmpls); + } - fprintf(stderr, " %.1f s", length_file); - fprintf(stderr, " %0.1f kbps", (double)totalbits *(sampFreqKHz) / totalsmpls); - if(maxStreamLen30 != 0) - { - fprintf(stderr, " plmax-30ms %d bytes (%0.0f kbps)", - maxStreamLen30, - maxStreamLen30 * 8 / 30.); - } - if(maxStreamLen60 != 0) - { - fprintf(stderr, " plmax-60ms %d bytes (%0.0f kbps)", - maxStreamLen60, - maxStreamLen60 * 8 / 60.); - } - if(doTransCoding) - { - fprintf(stderr, " transcoding rate %.0f kbps", - (double)numTransCodingBytes * 8.0 *(sampFreqKHz) / totalsmpls); - } + fclose(inp); + fclose(outp); + WebRtcIsac_Free(ISAC_main_inst); - fclose(inp); - fclose(outp); - WebRtcIsac_Free(ISAC_main_inst); - - - exit(0); + exit(0); } diff --git a/webrtc/modules/audio_coding/codecs/isac/main/test/simpleKenny.c b/webrtc/modules/audio_coding/codecs/isac/main/test/simpleKenny.c index d10b4addbd..959be7d4c8 100644 --- a/webrtc/modules/audio_coding/codecs/isac/main/test/simpleKenny.c +++ b/webrtc/modules/audio_coding/codecs/isac/main/test/simpleKenny.c @@ -17,7 +17,7 @@ #ifdef WIN32 #include "windows.h" -#define CLOCKS_PER_SEC 1000 +#define CLOCKS_PER_SEC 1000 #endif #include @@ -29,631 +29,556 @@ //#include "commonDefs.h" /* max number of samples per frame (= 60 ms frame) */ -#define MAX_FRAMESAMPLES_SWB 1920 +#define MAX_FRAMESAMPLES_SWB 1920 /* number of samples per 10ms frame */ -#define FRAMESAMPLES_SWB_10ms 320 -#define FRAMESAMPLES_WB_10ms 160 +#define FRAMESAMPLES_SWB_10ms 320 +#define FRAMESAMPLES_WB_10ms 160 /* sampling frequency (Hz) */ -#define FS_SWB 32000 -#define FS_WB 16000 +#define FS_SWB 32000 +#define FS_WB 16000 //#define CHANGE_OUTPUT_NAME #ifdef HAVE_DEBUG_INFO - #include "debugUtility.h" - debugStruct debugInfo; +#include "debugUtility.h" +debugStruct debugInfo; #endif unsigned long framecnt = 0; -int main(int argc, char* argv[]) -{ - //--- File IO ---- - FILE* inp; - FILE* outp; - char inname[500]; - char outname[500]; +int main(int argc, char* argv[]) { + //--- File IO ---- + FILE* inp; + FILE* outp; + char inname[500]; + char outname[500]; - /* Runtime statistics */ - double rate; - double rateRCU; - unsigned long totalbits = 0; - unsigned long totalBitsRCU = 0; - unsigned long totalsmpls =0; + /* Runtime statistics */ + double rate; + double rateRCU; + unsigned long totalbits = 0; + unsigned long totalBitsRCU = 0; + unsigned long totalsmpls = 0; - int32_t bottleneck = 39; - int16_t frameSize = 30; /* ms */ - int16_t codingMode = 1; - int16_t shortdata[FRAMESAMPLES_SWB_10ms]; - int16_t decoded[MAX_FRAMESAMPLES_SWB]; - //uint16_t streamdata[1000]; - int16_t speechType[1]; - int16_t payloadLimit; - int32_t rateLimit; - ISACStruct* ISAC_main_inst; + int32_t bottleneck = 39; + int16_t frameSize = 30; /* ms */ + int16_t codingMode = 1; + int16_t shortdata[FRAMESAMPLES_SWB_10ms]; + int16_t decoded[MAX_FRAMESAMPLES_SWB]; + // uint16_t streamdata[1000]; + int16_t speechType[1]; + int16_t payloadLimit; + int32_t rateLimit; + ISACStruct* ISAC_main_inst; - int16_t stream_len = 0; - int16_t declen = 0; - int16_t err; - int16_t cur_framesmpls; - int endfile; + int16_t stream_len = 0; + int16_t declen = 0; + int16_t err; + int16_t cur_framesmpls; + int endfile; #ifdef WIN32 - double length_file; - double runtime; - char outDrive[10]; - char outPath[500]; - char outPrefix[500]; - char outSuffix[500]; - char bitrateFileName[500]; - FILE* bitrateFile; - double starttime; - double rateLB = 0; - double rateUB = 0; + double length_file; + double runtime; + char outDrive[10]; + char outPath[500]; + char outPrefix[500]; + char outSuffix[500]; + char bitrateFileName[500]; + FILE* bitrateFile; + double starttime; + double rateLB = 0; + double rateUB = 0; #endif - FILE* histFile; - FILE* averageFile; - int sampFreqKHz; - int samplesIn10Ms; - int16_t maxStreamLen = 0; - char histFileName[500]; - char averageFileName[500]; - unsigned int hist[600]; - unsigned int tmpSumStreamLen = 0; - unsigned int packetCntr = 0; - unsigned int lostPacketCntr = 0; - uint8_t payload[1200]; - uint8_t payloadRCU[1200]; - uint16_t packetLossPercent = 0; - int16_t rcuStreamLen = 0; - int onlyEncode; - int onlyDecode; + FILE* histFile; + FILE* averageFile; + int sampFreqKHz; + int samplesIn10Ms; + int16_t maxStreamLen = 0; + char histFileName[500]; + char averageFileName[500]; + unsigned int hist[600]; + unsigned int tmpSumStreamLen = 0; + unsigned int packetCntr = 0; + unsigned int lostPacketCntr = 0; + uint8_t payload[1200]; + uint8_t payloadRCU[1200]; + uint16_t packetLossPercent = 0; + int16_t rcuStreamLen = 0; + int onlyEncode; + int onlyDecode; + BottleNeckModel packetData; + packetData.arrival_time = 0; + packetData.sample_count = 0; + packetData.rtp_number = 0; + memset(hist, 0, sizeof(hist)); - BottleNeckModel packetData; - packetData.arrival_time = 0; - packetData.sample_count = 0; - packetData.rtp_number = 0; - memset(hist, 0, sizeof(hist)); + /* handling wrong input arguments in the command line */ + if (argc < 5) { + int size; + WebRtcIsac_AssignSize(&size); - /* handling wrong input arguments in the command line */ - if(argc < 5) - { - int size; - WebRtcIsac_AssignSize(&size); + printf("\n\nWrong number of arguments or flag values.\n\n"); - printf("\n\nWrong number of arguments or flag values.\n\n"); + printf("Usage:\n\n"); + printf("%s infile outfile -bn bottelneck [options] \n\n", argv[0]); + printf("with:\n"); + printf("-I................... indicates encoding in instantaneous mode.\n"); + printf( + "-bn bottleneck....... the value of the bottleneck in bit/sec, e.g. " + "39742,\n"); + printf( + " in instantaneous (channel-independent) " + "mode.\n\n"); + printf("infile............... Normal speech input file\n\n"); + printf("outfile.............. Speech output file\n\n"); + printf("OPTIONS\n"); + printf("-------\n"); + printf( + "-fs sampFreq......... sampling frequency of codec 16 or 32 (default) " + "kHz.\n"); + printf("-plim payloadLim..... payload limit in bytes,\n"); + printf(" default is the maximum possible.\n"); + printf("-rlim rateLim........ rate limit in bits/sec, \n"); + printf(" default is the maimum possible.\n"); + printf("-h file.............. record histogram and *append* to 'file'.\n"); + printf( + "-ave file............ record average rate of 3 sec intervales and " + "*append* to 'file'.\n"); + printf("-ploss............... packet-loss percentage.\n"); + printf("-enc................. do only encoding and store the bit-stream\n"); + printf( + "-dec................. the input file is a bit-stream, decode it.\n"); - printf("Usage:\n\n"); - printf("%s infile outfile -bn bottelneck [options] \n\n", argv[0]); - printf("with:\n"); - printf("-I................... indicates encoding in instantaneous mode.\n"); - printf("-bn bottleneck....... the value of the bottleneck in bit/sec, e.g. 39742,\n"); - printf(" in instantaneous (channel-independent) mode.\n\n"); - printf("infile............... Normal speech input file\n\n"); - printf("outfile.............. Speech output file\n\n"); - printf("OPTIONS\n"); - printf("-------\n"); - printf("-fs sampFreq......... sampling frequency of codec 16 or 32 (default) kHz.\n"); - printf("-plim payloadLim..... payload limit in bytes,\n"); - printf(" default is the maximum possible.\n"); - printf("-rlim rateLim........ rate limit in bits/sec, \n"); - printf(" default is the maimum possible.\n"); - printf("-h file.............. record histogram and *append* to 'file'.\n"); - printf("-ave file............ record average rate of 3 sec intervales and *append* to 'file'.\n"); - printf("-ploss............... packet-loss percentage.\n"); - printf("-enc................. do only encoding and store the bit-stream\n"); - printf("-dec................. the input file is a bit-stream, decode it.\n"); + printf("\n"); + printf("Example usage:\n\n"); + printf("%s speechIn.pcm speechOut.pcm -B 40000 -fs 32 \n\n", argv[0]); - printf("\n"); - printf("Example usage:\n\n"); - printf("%s speechIn.pcm speechOut.pcm -B 40000 -fs 32 \n\n", argv[0]); + printf("structure size %d bytes\n", size); - printf("structure size %d bytes\n", size); + exit(0); + } - exit(0); + /* Get Bottleneck value */ + bottleneck = readParamInt(argc, argv, "-bn", 50000); + fprintf(stderr, "\nfixed bottleneck rate of %d bits/s\n\n", bottleneck); + + /* Get Input and Output files */ + sscanf(argv[1], "%s", inname); + sscanf(argv[2], "%s", outname); + codingMode = readSwitch(argc, argv, "-I"); + sampFreqKHz = (int16_t)readParamInt(argc, argv, "-fs", 32); + if (readParamString(argc, argv, "-h", histFileName, 500) > 0) { + histFile = fopen(histFileName, "a"); + if (histFile == NULL) { + printf("cannot open hist file %s", histFileName); + exit(0); } + } else { + // NO recording of hitstogram + histFile = NULL; + } + packetLossPercent = readParamInt(argc, argv, "-ploss", 0); - - /* Get Bottleneck value */ - bottleneck = readParamInt(argc, argv, "-bn", 50000); - fprintf(stderr,"\nfixed bottleneck rate of %d bits/s\n\n", bottleneck); - - /* Get Input and Output files */ - sscanf(argv[1], "%s", inname); - sscanf(argv[2], "%s", outname); - codingMode = readSwitch(argc, argv, "-I"); - sampFreqKHz = (int16_t)readParamInt(argc, argv, "-fs", 32); - if(readParamString(argc, argv, "-h", histFileName, 500) > 0) - { - histFile = fopen(histFileName, "a"); - if(histFile == NULL) - { - printf("cannot open hist file %s", histFileName); - exit(0); - } + if (readParamString(argc, argv, "-ave", averageFileName, 500) > 0) { + averageFile = fopen(averageFileName, "a"); + if (averageFile == NULL) { + printf("cannot open file to write rate %s", averageFileName); + exit(0); } - else - { - // NO recording of hitstogram - histFile = NULL; + } else { + averageFile = NULL; + } + + onlyEncode = readSwitch(argc, argv, "-enc"); + onlyDecode = readSwitch(argc, argv, "-dec"); + + switch (sampFreqKHz) { + case 16: { + samplesIn10Ms = 160; + break; } - - - packetLossPercent = readParamInt(argc, argv, "-ploss", 0); - - if(readParamString(argc, argv, "-ave", averageFileName, 500) > 0) - { - averageFile = fopen(averageFileName, "a"); - if(averageFile == NULL) - { - printf("cannot open file to write rate %s", averageFileName); - exit(0); - } + case 32: { + samplesIn10Ms = 320; + break; } - else - { - averageFile = NULL; - } - - onlyEncode = readSwitch(argc, argv, "-enc"); - onlyDecode = readSwitch(argc, argv, "-dec"); - - - switch(sampFreqKHz) - { - case 16: - { - samplesIn10Ms = 160; - break; - } - case 32: - { - samplesIn10Ms = 320; - break; - } default: - printf("A sampling frequency of %d kHz is not supported,\ -valid values are 8 and 16.\n", sampFreqKHz); - exit(-1); - } - payloadLimit = (int16_t)readParamInt(argc, argv, "-plim", 400); - rateLimit = readParamInt(argc, argv, "-rlim", 106800); + printf( + "A sampling frequency of %d kHz is not supported,\ +valid values are 8 and 16.\n", + sampFreqKHz); + exit(-1); + } + payloadLimit = (int16_t)readParamInt(argc, argv, "-plim", 400); + rateLimit = readParamInt(argc, argv, "-rlim", 106800); - if ((inp = fopen(inname,"rb")) == NULL) { - printf(" iSAC: Cannot read file %s.\n", inname); - exit(1); - } - if ((outp = fopen(outname,"wb")) == NULL) { - printf(" iSAC: Cannot write file %s.\n", outname); - exit(1); - } + if ((inp = fopen(inname, "rb")) == NULL) { + printf(" iSAC: Cannot read file %s.\n", inname); + exit(1); + } + if ((outp = fopen(outname, "wb")) == NULL) { + printf(" iSAC: Cannot write file %s.\n", outname); + exit(1); + } #ifdef WIN32 - _splitpath(outname, outDrive, outPath, outPrefix, outSuffix); - _makepath(bitrateFileName, outDrive, outPath, "bitrate", ".txt"); + _splitpath(outname, outDrive, outPath, outPrefix, outSuffix); + _makepath(bitrateFileName, outDrive, outPath, "bitrate", ".txt"); - bitrateFile = fopen(bitrateFileName, "a"); - fprintf(bitrateFile, "% %%s \n", inname); + bitrateFile = fopen(bitrateFileName, "a"); + fprintf(bitrateFile, "% %%s \n", inname); #endif - printf("\n"); - printf("Input.................... %s\n", inname); - printf("Output................... %s\n", outname); - printf("Encoding Mode............ %s\n", - (codingMode == 1)? "Channel-Independent":"Channel-Adaptive"); - printf("Bottleneck............... %d bits/sec\n", bottleneck); - printf("Packet-loss Percentage... %d\n", packetLossPercent); - printf("\n"); + printf("\n"); + printf("Input.................... %s\n", inname); + printf("Output................... %s\n", outname); + printf("Encoding Mode............ %s\n", + (codingMode == 1) ? "Channel-Independent" : "Channel-Adaptive"); + printf("Bottleneck............... %d bits/sec\n", bottleneck); + printf("Packet-loss Percentage... %d\n", packetLossPercent); + printf("\n"); #ifdef WIN32 - starttime = clock()/(double)CLOCKS_PER_SEC; /* Runtime statistics */ + starttime = clock() / (double)CLOCKS_PER_SEC; /* Runtime statistics */ #endif - /* Initialize the ISAC and BN structs */ - err = WebRtcIsac_Create(&ISAC_main_inst); + /* Initialize the ISAC and BN structs */ + err = WebRtcIsac_Create(&ISAC_main_inst); - WebRtcIsac_SetEncSampRate(ISAC_main_inst, sampFreqKHz * 1000); - WebRtcIsac_SetDecSampRate(ISAC_main_inst, sampFreqKHz >= 32 ? 32000 : - 16000); - /* Error check */ - if (err < 0) { - fprintf(stderr,"\n\n Error in create.\n\n"); - exit(EXIT_FAILURE); + WebRtcIsac_SetEncSampRate(ISAC_main_inst, sampFreqKHz * 1000); + WebRtcIsac_SetDecSampRate(ISAC_main_inst, sampFreqKHz >= 32 ? 32000 : 16000); + /* Error check */ + if (err < 0) { + fprintf(stderr, "\n\n Error in create.\n\n"); + exit(EXIT_FAILURE); + } + + framecnt = 0; + endfile = 0; + + /* Initialize encoder and decoder */ + if (WebRtcIsac_EncoderInit(ISAC_main_inst, codingMode) < 0) { + printf("cannot initialize encoder\n"); + return -1; + } + if (WebRtcIsac_DecoderInit(ISAC_main_inst) < 0) { + printf("cannot initialize decoder\n"); + return -1; + } + + //{ + // int32_t b1, b2; + // FILE* fileID = fopen("GetBNTest.txt", "w"); + // b2 = 32100; + // while(b2 <= 52000) + // { + // WebRtcIsac_Control(ISAC_main_inst, b2, frameSize); + // WebRtcIsac_GetUplinkBw(ISAC_main_inst, &b1); + // fprintf(fileID, "%5d %5d\n", b2, b1); + // b2 += 10; + // } + //} + + if (codingMode == 1) { + if (WebRtcIsac_Control(ISAC_main_inst, bottleneck, frameSize) < 0) { + printf("cannot set bottleneck\n"); + return -1; } + } else { + if (WebRtcIsac_ControlBwe(ISAC_main_inst, 15000, 30, 1) < 0) { + printf("cannot configure BWE\n"); + return -1; + } + } - framecnt = 0; - endfile = 0; + if (WebRtcIsac_SetMaxPayloadSize(ISAC_main_inst, payloadLimit) < 0) { + printf("cannot set maximum payload size %d.\n", payloadLimit); + return -1; + } - /* Initialize encoder and decoder */ - if(WebRtcIsac_EncoderInit(ISAC_main_inst, codingMode) < 0) - { - printf("cannot initialize encoder\n"); + if (rateLimit < 106800) { + if (WebRtcIsac_SetMaxRate(ISAC_main_inst, rateLimit) < 0) { + printf("cannot set the maximum rate %d.\n", rateLimit); + return -1; + } + } + + //===================================== + //#ifdef HAVE_DEBUG_INFO + // if(setupDebugStruct(&debugInfo) < 0) + // { + // exit(1); + // } + //#endif + + while (endfile == 0) { + fprintf(stderr, " \rframe = %7li", framecnt); + + //============== Readind from the file and encoding ================= + cur_framesmpls = 0; + stream_len = 0; + + if (onlyDecode) { + uint8_t auxUW8; + size_t auxSizet; + if (fread(&auxUW8, sizeof(uint8_t), 1, inp) < 1) { + break; + } + stream_len = ((uint8_t)auxUW8) << 8; + if (fread(&auxUW8, sizeof(uint8_t), 1, inp) < 1) { + break; + } + stream_len |= (uint16_t)auxUW8; + auxSizet = (size_t)stream_len; + if (fread(payload, 1, auxSizet, inp) < auxSizet) { + printf("last payload is corrupted\n"); + break; + } + } else { + while (stream_len == 0) { + // Read 10 ms speech block + endfile = readframe(shortdata, inp, samplesIn10Ms); + if (endfile) { + break; + } + cur_framesmpls += samplesIn10Ms; + + //-------- iSAC encoding --------- + stream_len = WebRtcIsac_Encode(ISAC_main_inst, shortdata, payload); + + if (stream_len < 0) { + // exit if returned with error + // errType=WebRtcIsac_GetErrorCode(ISAC_main_inst); + fprintf(stderr, "\nError in encoder\n"); + getc(stdin); + exit(EXIT_FAILURE); + } + } + //=================================================================== + if (endfile) { + break; + } + + rcuStreamLen = WebRtcIsac_GetRedPayload(ISAC_main_inst, payloadRCU); + + get_arrival_time(cur_framesmpls, stream_len, bottleneck, &packetData, + sampFreqKHz * 1000, sampFreqKHz * 1000); + if (WebRtcIsac_UpdateBwEstimate( + ISAC_main_inst, payload, stream_len, packetData.rtp_number, + packetData.sample_count, packetData.arrival_time) < 0) { + printf(" BWE Error at client\n"); return -1; + } } - if(WebRtcIsac_DecoderInit(ISAC_main_inst) < 0) - { - printf("cannot initialize decoder\n"); + + if (endfile) { + break; + } + + maxStreamLen = (stream_len > maxStreamLen) ? stream_len : maxStreamLen; + packetCntr++; + + hist[stream_len]++; + if (averageFile != NULL) { + tmpSumStreamLen += stream_len; + if (packetCntr == 100) { + // kbps + fprintf(averageFile, "%8.3f ", + (double)tmpSumStreamLen * 8.0 / (30.0 * packetCntr)); + packetCntr = 0; + tmpSumStreamLen = 0; + } + } + + if (onlyEncode) { + uint8_t auxUW8; + auxUW8 = (uint8_t)(((stream_len & 0x7F00) >> 8) & 0xFF); + if (fwrite(&auxUW8, sizeof(uint8_t), 1, outp) != 1) { return -1; - } + } - //{ - // int32_t b1, b2; - // FILE* fileID = fopen("GetBNTest.txt", "w"); - // b2 = 32100; - // while(b2 <= 52000) - // { - // WebRtcIsac_Control(ISAC_main_inst, b2, frameSize); - // WebRtcIsac_GetUplinkBw(ISAC_main_inst, &b1); - // fprintf(fileID, "%5d %5d\n", b2, b1); - // b2 += 10; - // } - //} - - if(codingMode == 1) - { - if(WebRtcIsac_Control(ISAC_main_inst, bottleneck, frameSize) < 0) - { - printf("cannot set bottleneck\n"); - return -1; - } - } - else - { - if(WebRtcIsac_ControlBwe(ISAC_main_inst, 15000, 30, 1) < 0) - { - printf("cannot configure BWE\n"); - return -1; - } - } - - if(WebRtcIsac_SetMaxPayloadSize(ISAC_main_inst, payloadLimit) < 0) - { - printf("cannot set maximum payload size %d.\n", payloadLimit); + auxUW8 = (uint8_t)(stream_len & 0xFF); + if (fwrite(&auxUW8, sizeof(uint8_t), 1, outp) != 1) { return -1; + } + if (fwrite(payload, 1, stream_len, outp) != (size_t)stream_len) { + return -1; + } + } else { + //======================= iSAC decoding =========================== + + if ((rand() % 100) < packetLossPercent) { + declen = WebRtcIsac_DecodeRcu(ISAC_main_inst, payloadRCU, rcuStreamLen, + decoded, speechType); + lostPacketCntr++; + } else { + declen = WebRtcIsac_Decode(ISAC_main_inst, payload, stream_len, decoded, + speechType); + } + if (declen <= 0) { + // errType=WebRtcIsac_GetErrorCode(ISAC_main_inst); + fprintf(stderr, "\nError in decoder.\n"); + getc(stdin); + exit(1); + } + + // Write decoded speech frame to file + if (fwrite(decoded, sizeof(int16_t), declen, outp) != (size_t)declen) { + return -1; + } + cur_framesmpls = declen; } - - if (rateLimit < 106800) { - if(WebRtcIsac_SetMaxRate(ISAC_main_inst, rateLimit) < 0) - { - printf("cannot set the maximum rate %d.\n", rateLimit); - return -1; - } + // Update Statistics + framecnt++; + totalsmpls += cur_framesmpls; + if (stream_len > 0) { + totalbits += 8 * stream_len; } - - //===================================== -//#ifdef HAVE_DEBUG_INFO -// if(setupDebugStruct(&debugInfo) < 0) -// { -// exit(1); -// } -//#endif - - while (endfile == 0) - { - fprintf(stderr," \rframe = %7li", framecnt); - - //============== Readind from the file and encoding ================= - cur_framesmpls = 0; - stream_len = 0; - - - if(onlyDecode) - { - uint8_t auxUW8; - size_t auxSizet; - if(fread(&auxUW8, sizeof(uint8_t), 1, inp) < 1) - { - break; - } - stream_len = ((uint8_t)auxUW8) << 8; - if(fread(&auxUW8, sizeof(uint8_t), 1, inp) < 1) - { - break; - } - stream_len |= (uint16_t)auxUW8; - auxSizet = (size_t)stream_len; - if(fread(payload, 1, auxSizet, inp) < auxSizet) - { - printf("last payload is corrupted\n"); - break; - } - } - else - { - while(stream_len == 0) - { - // Read 10 ms speech block - endfile = readframe(shortdata, inp, samplesIn10Ms); - if(endfile) - { - break; - } - cur_framesmpls += samplesIn10Ms; - - //-------- iSAC encoding --------- - stream_len = WebRtcIsac_Encode( - ISAC_main_inst, - shortdata, - payload); - - if(stream_len < 0) - { - // exit if returned with error - //errType=WebRtcIsac_GetErrorCode(ISAC_main_inst); - fprintf(stderr,"\nError in encoder\n"); - getc(stdin); - exit(EXIT_FAILURE); - } - - - } - //=================================================================== - if(endfile) - { - break; - } - - rcuStreamLen = WebRtcIsac_GetRedPayload( - ISAC_main_inst, payloadRCU); - - get_arrival_time(cur_framesmpls, stream_len, bottleneck, &packetData, - sampFreqKHz * 1000, sampFreqKHz * 1000); - if(WebRtcIsac_UpdateBwEstimate(ISAC_main_inst, - payload, - stream_len, - packetData.rtp_number, - packetData.sample_count, - packetData.arrival_time) - < 0) - { - printf(" BWE Error at client\n"); - return -1; - } - } - - if(endfile) - { - break; - } - - maxStreamLen = (stream_len > maxStreamLen)? stream_len:maxStreamLen; - packetCntr++; - - hist[stream_len]++; - if(averageFile != NULL) - { - tmpSumStreamLen += stream_len; - if(packetCntr == 100) - { - // kbps - fprintf(averageFile, "%8.3f ", (double)tmpSumStreamLen * 8.0 / (30.0 * packetCntr)); - packetCntr = 0; - tmpSumStreamLen = 0; - } - } - - if(onlyEncode) - { - uint8_t auxUW8; - auxUW8 = (uint8_t)(((stream_len & 0x7F00) >> 8) & 0xFF); - if (fwrite(&auxUW8, sizeof(uint8_t), 1, outp) != 1) { - return -1; - } - - auxUW8 = (uint8_t)(stream_len & 0xFF); - if (fwrite(&auxUW8, sizeof(uint8_t), 1, outp) != 1) { - return -1; - } - if (fwrite(payload, 1, stream_len, - outp) != (size_t)stream_len) { - return -1; - } - } - else - { - - //======================= iSAC decoding =========================== - - if((rand() % 100) < packetLossPercent) - { - declen = WebRtcIsac_DecodeRcu( - ISAC_main_inst, - payloadRCU, - rcuStreamLen, - decoded, - speechType); - lostPacketCntr++; - } - else - { - declen = WebRtcIsac_Decode( - ISAC_main_inst, - payload, - stream_len, - decoded, - speechType); - } - if(declen <= 0) - { - //errType=WebRtcIsac_GetErrorCode(ISAC_main_inst); - fprintf(stderr,"\nError in decoder.\n"); - getc(stdin); - exit(1); - } - - // Write decoded speech frame to file - if (fwrite(decoded, sizeof(int16_t), - declen, outp) != (size_t)declen) { - return -1; - } - cur_framesmpls = declen; - } - // Update Statistics - framecnt++; - totalsmpls += cur_framesmpls; - if(stream_len > 0) - { - totalbits += 8 * stream_len; - } - if(rcuStreamLen > 0) - { - totalBitsRCU += 8 * rcuStreamLen; - } + if (rcuStreamLen > 0) { + totalBitsRCU += 8 * rcuStreamLen; } + } - rate = ((double)totalbits * (sampFreqKHz)) / (double)totalsmpls; - rateRCU = ((double)totalBitsRCU * (sampFreqKHz)) / (double)totalsmpls; + rate = ((double)totalbits * (sampFreqKHz)) / (double)totalsmpls; + rateRCU = ((double)totalBitsRCU * (sampFreqKHz)) / (double)totalsmpls; - printf("\n\n"); - printf("Sampling Rate......................... %d kHz\n", sampFreqKHz); - printf("Payload Limit......................... %d bytes \n", payloadLimit); - printf("Rate Limit............................ %d bits/sec \n", rateLimit); + printf("\n\n"); + printf("Sampling Rate......................... %d kHz\n", sampFreqKHz); + printf("Payload Limit......................... %d bytes \n", payloadLimit); + printf("Rate Limit............................ %d bits/sec \n", rateLimit); #ifdef WIN32 #ifdef HAVE_DEBUG_INFO - rateLB = ((double)debugInfo.lbBytes * 8. * - (sampFreqKHz)) / (double)totalsmpls; - rateUB = ((double)debugInfo.ubBytes * 8. * - (sampFreqKHz)) / (double)totalsmpls; + rateLB = + ((double)debugInfo.lbBytes * 8. * (sampFreqKHz)) / (double)totalsmpls; + rateUB = + ((double)debugInfo.ubBytes * 8. * (sampFreqKHz)) / (double)totalsmpls; #endif - fprintf(bitrateFile, "%d %10u %d %6.3f %6.3f %6.3f\n", - sampFreqKHz, - framecnt, - bottleneck, - rateLB, - rateUB, - rate); - fclose(bitrateFile); -#endif // WIN32 + fprintf(bitrateFile, "%d %10u %d %6.3f %6.3f %6.3f\n", + sampFreqKHz, framecnt, bottleneck, rateLB, rateUB, rate); + fclose(bitrateFile); +#endif // WIN32 - printf("\n"); - printf("Measured bit-rate..................... %0.3f kbps\n", rate); - printf("Measured RCU bit-ratre................ %0.3f kbps\n", rateRCU); - printf("Maximum bit-rate/payloadsize.......... %0.3f / %d\n", - maxStreamLen * 8 / 0.03, maxStreamLen); - printf("Measured packet-loss.................. %0.1f%% \n", - 100.0f * (float)lostPacketCntr / (float)packetCntr); + printf("\n"); + printf("Measured bit-rate..................... %0.3f kbps\n", rate); + printf("Measured RCU bit-ratre................ %0.3f kbps\n", rateRCU); + printf("Maximum bit-rate/payloadsize.......... %0.3f / %d\n", + maxStreamLen * 8 / 0.03, maxStreamLen); + printf("Measured packet-loss.................. %0.1f%% \n", + 100.0f * (float)lostPacketCntr / (float)packetCntr); -//#ifdef HAVE_DEBUG_INFO -// printf("Measured lower-band bit-rate.......... %0.3f kbps (%.0f%%)\n", -// rateLB, (double)(rateLB) * 100. /(double)(rate)); -// printf("Measured upper-band bit-rate.......... %0.3f kbps (%.0f%%)\n", -// rateUB, (double)(rateUB) * 100. /(double)(rate)); -// -// printf("Maximum payload lower-band............ %d bytes (%0.3f kbps)\n", -// debugInfo.maxPayloadLB, debugInfo.maxPayloadLB * 8.0 / 0.03); -// printf("Maximum payload upper-band............ %d bytes (%0.3f kbps)\n", -// debugInfo.maxPayloadUB, debugInfo.maxPayloadUB * 8.0 / 0.03); -//#endif + //#ifdef HAVE_DEBUG_INFO + // printf("Measured lower-band bit-rate.......... %0.3f kbps (%.0f%%)\n", + // rateLB, (double)(rateLB) * 100. /(double)(rate)); + // printf("Measured upper-band bit-rate.......... %0.3f kbps (%.0f%%)\n", + // rateUB, (double)(rateUB) * 100. /(double)(rate)); + // + // printf("Maximum payload lower-band............ %d bytes (%0.3f kbps)\n", + // debugInfo.maxPayloadLB, debugInfo.maxPayloadLB * 8.0 / 0.03); + // printf("Maximum payload upper-band............ %d bytes (%0.3f kbps)\n", + // debugInfo.maxPayloadUB, debugInfo.maxPayloadUB * 8.0 / 0.03); + //#endif - printf("\n"); + printf("\n"); - /* Runtime statistics */ +/* Runtime statistics */ #ifdef WIN32 - runtime = (double)(clock()/(double)CLOCKS_PER_SEC-starttime); - length_file = ((double)framecnt*(double)declen/(sampFreqKHz*1000)); - printf("Length of speech file................ %.1f s\n", length_file); - printf("Time to run iSAC..................... %.2f s (%.2f %% of realtime)\n\n", - runtime, (100*runtime/length_file)); + runtime = (double)(clock() / (double)CLOCKS_PER_SEC - starttime); + length_file = ((double)framecnt * (double)declen / (sampFreqKHz * 1000)); + printf("Length of speech file................ %.1f s\n", length_file); + printf( + "Time to run iSAC..................... %.2f s (%.2f %% of realtime)\n\n", + runtime, (100 * runtime / length_file)); #endif - printf("\n\n_______________________________________________\n"); + printf("\n\n_______________________________________________\n"); - if(histFile != NULL) - { - int n; - for(n = 0; n < 600; n++) - { - fprintf(histFile, "%6d ", hist[n]); - } - fprintf(histFile, "\n"); - fclose(histFile); + if (histFile != NULL) { + int n; + for (n = 0; n < 600; n++) { + fprintf(histFile, "%6d ", hist[n]); } - if(averageFile != NULL) - { - if(packetCntr > 0) - { - fprintf(averageFile, "%8.3f ", (double)tmpSumStreamLen * 8.0 / (30.0 * packetCntr)); - } - fprintf(averageFile, "\n"); - fclose(averageFile); + fprintf(histFile, "\n"); + fclose(histFile); + } + if (averageFile != NULL) { + if (packetCntr > 0) { + fprintf(averageFile, "%8.3f ", + (double)tmpSumStreamLen * 8.0 / (30.0 * packetCntr)); } + fprintf(averageFile, "\n"); + fclose(averageFile); + } - fclose(inp); - fclose(outp); - - WebRtcIsac_Free(ISAC_main_inst); + fclose(inp); + fclose(outp); + WebRtcIsac_Free(ISAC_main_inst); #ifdef CHANGE_OUTPUT_NAME - { - char* p; - char myExt[50]; - char bitRateStr[10]; - char newOutName[500]; - strcpy(newOutName, outname); + { + char* p; + char myExt[50]; + char bitRateStr[10]; + char newOutName[500]; + strcpy(newOutName, outname); - myExt[0] = '\0'; - p = strchr(newOutName, '.'); - if(p != NULL) - { - strcpy(myExt, p); - *p = '_'; - p++; - *p = '\0'; - } - else - { - strcat(newOutName, "_"); - } - sprintf(bitRateStr, "%0.0fkbps", rate); - strcat(newOutName, bitRateStr); - strcat(newOutName, myExt); - rename(outname, newOutName); + myExt[0] = '\0'; + p = strchr(newOutName, '.'); + if (p != NULL) { + strcpy(myExt, p); + *p = '_'; + p++; + *p = '\0'; + } else { + strcat(newOutName, "_"); } + sprintf(bitRateStr, "%0.0fkbps", rate); + strcat(newOutName, bitRateStr); + strcat(newOutName, myExt); + rename(outname, newOutName); + } #endif - exit(0); + exit(0); } - #ifdef HAVE_DEBUG_INFO -int setupDebugStruct(debugStruct* str) -{ - str->prevPacketLost = 0; - str->currPacketLost = 0; +int setupDebugStruct(debugStruct* str) { + str->prevPacketLost = 0; + str->currPacketLost = 0; - OPEN_FILE_WB(str->res0to4FilePtr, "Res0to4.dat"); - OPEN_FILE_WB(str->res4to8FilePtr, "Res4to8.dat"); - OPEN_FILE_WB(str->res8to12FilePtr, "Res8to12.dat"); - OPEN_FILE_WB(str->res8to16FilePtr, "Res8to16.dat"); + OPEN_FILE_WB(str->res0to4FilePtr, "Res0to4.dat"); + OPEN_FILE_WB(str->res4to8FilePtr, "Res4to8.dat"); + OPEN_FILE_WB(str->res8to12FilePtr, "Res8to12.dat"); + OPEN_FILE_WB(str->res8to16FilePtr, "Res8to16.dat"); - OPEN_FILE_WB(str->res0to4DecFilePtr, "Res0to4Dec.dat"); - OPEN_FILE_WB(str->res4to8DecFilePtr, "Res4to8Dec.dat"); - OPEN_FILE_WB(str->res8to12DecFilePtr, "Res8to12Dec.dat"); - OPEN_FILE_WB(str->res8to16DecFilePtr, "Res8to16Dec.dat"); + OPEN_FILE_WB(str->res0to4DecFilePtr, "Res0to4Dec.dat"); + OPEN_FILE_WB(str->res4to8DecFilePtr, "Res4to8Dec.dat"); + OPEN_FILE_WB(str->res8to12DecFilePtr, "Res8to12Dec.dat"); + OPEN_FILE_WB(str->res8to16DecFilePtr, "Res8to16Dec.dat"); - OPEN_FILE_WB(str->in0to4FilePtr, "in0to4.dat"); - OPEN_FILE_WB(str->in4to8FilePtr, "in4to8.dat"); - OPEN_FILE_WB(str->in8to12FilePtr, "in8to12.dat"); - OPEN_FILE_WB(str->in8to16FilePtr, "in8to16.dat"); + OPEN_FILE_WB(str->in0to4FilePtr, "in0to4.dat"); + OPEN_FILE_WB(str->in4to8FilePtr, "in4to8.dat"); + OPEN_FILE_WB(str->in8to12FilePtr, "in8to12.dat"); + OPEN_FILE_WB(str->in8to16FilePtr, "in8to16.dat"); - OPEN_FILE_WB(str->out0to4FilePtr, "out0to4.dat"); - OPEN_FILE_WB(str->out4to8FilePtr, "out4to8.dat"); - OPEN_FILE_WB(str->out8to12FilePtr, "out8to12.dat"); - OPEN_FILE_WB(str->out8to16FilePtr, "out8to16.dat"); - OPEN_FILE_WB(str->fftFilePtr, "riFFT.dat"); - OPEN_FILE_WB(str->fftDecFilePtr, "riFFTDec.dat"); + OPEN_FILE_WB(str->out0to4FilePtr, "out0to4.dat"); + OPEN_FILE_WB(str->out4to8FilePtr, "out4to8.dat"); + OPEN_FILE_WB(str->out8to12FilePtr, "out8to12.dat"); + OPEN_FILE_WB(str->out8to16FilePtr, "out8to16.dat"); + OPEN_FILE_WB(str->fftFilePtr, "riFFT.dat"); + OPEN_FILE_WB(str->fftDecFilePtr, "riFFTDec.dat"); - OPEN_FILE_WB(str->arrivalTime, NULL/*"ArivalTime.dat"*/); - str->lastArrivalTime = 0; + OPEN_FILE_WB(str->arrivalTime, NULL /*"ArivalTime.dat"*/); + str->lastArrivalTime = 0; - str->maxPayloadLB = 0; - str->maxPayloadUB = 0; - str->lbBytes = 0; - str->ubBytes = 0; + str->maxPayloadLB = 0; + str->maxPayloadUB = 0; + str->lbBytes = 0; + str->ubBytes = 0; - return 0; + return 0; }; #endif diff --git a/webrtc/modules/audio_coding/neteq/test/RTPencode.cc b/webrtc/modules/audio_coding/neteq/test/RTPencode.cc index b4263c7e4b..427601e7a3 100644 --- a/webrtc/modules/audio_coding/neteq/test/RTPencode.cc +++ b/webrtc/modules/audio_coding/neteq/test/RTPencode.cc @@ -8,7 +8,7 @@ * be found in the AUTHORS file in the root of the source tree. */ -//TODO(hlundin): Reformat file to meet style guide. +// TODO(hlundin): Reformat file to meet style guide. /* header includes */ #include @@ -34,23 +34,25 @@ #include "PayloadTypes.h" - - /*********************/ /* Misc. definitions */ /*********************/ #define STOPSENDTIME 3000 -#define RESTARTSENDTIME 0 //162500 +#define RESTARTSENDTIME 0 // 162500 #define FIRSTLINELEN 40 -#define CHECK_NOT_NULL(a) if((a)==0){printf("\n %s \n line: %d \nerror at %s\n",__FILE__,__LINE__,#a );return(-1);} +#define CHECK_NOT_NULL(a) \ + if ((a) == 0) { \ + printf("\n %s \n line: %d \nerror at %s\n", __FILE__, __LINE__, #a); \ + return (-1); \ + } //#define MULTIPLE_SAME_TIMESTAMP #define REPEAT_PACKET_DISTANCE 17 #define REPEAT_PACKET_COUNT 1 // number of extra packets to send //#define INSERT_OLD_PACKETS -#define OLD_PACKET 5 // how many seconds too old should the packet be? +#define OLD_PACKET 5 // how many seconds too old should the packet be? //#define TIMESTAMP_WRAPAROUND @@ -64,8 +66,8 @@ #define DTMF_DURATION 500 #define STEREO_MODE_FRAME 0 -#define STEREO_MODE_SAMPLE_1 1 //1 octet per sample -#define STEREO_MODE_SAMPLE_2 2 //2 octets per sample +#define STEREO_MODE_SAMPLE_1 1 // 1 octet per sample +#define STEREO_MODE_SAMPLE_2 2 // 2 octets per sample /*************************/ /* Function declarations */ @@ -121,702 +123,720 @@ void stereoInterleave(unsigned char* data, int dataLen, int stride); #include "webrtc_vad.h" -#if ((defined CODEC_PCM16B)||(defined NETEQ_ARBITRARY_CODEC)) - #include "pcm16b.h" +#if ((defined CODEC_PCM16B) || (defined NETEQ_ARBITRARY_CODEC)) +#include "pcm16b.h" #endif #ifdef CODEC_G711 - #include "g711_interface.h" +#include "g711_interface.h" #endif #ifdef CODEC_G729 - #include "G729Interface.h" +#include "G729Interface.h" #endif #ifdef CODEC_G729_1 - #include "G729_1Interface.h" +#include "G729_1Interface.h" #endif #ifdef CODEC_AMR - #include "AMRInterface.h" - #include "AMRCreation.h" +#include "AMRInterface.h" +#include "AMRCreation.h" #endif #ifdef CODEC_AMRWB - #include "AMRWBInterface.h" - #include "AMRWBCreation.h" +#include "AMRWBInterface.h" +#include "AMRWBCreation.h" #endif #ifdef CODEC_ILBC - #include "ilbc.h" +#include "ilbc.h" #endif -#if (defined CODEC_ISAC || defined CODEC_ISAC_SWB) - #include "isac.h" +#if (defined CODEC_ISAC || defined CODEC_ISAC_SWB) +#include "isac.h" #endif #ifdef NETEQ_ISACFIX_CODEC - #include "isacfix.h" - #ifdef CODEC_ISAC - #error Cannot have both ISAC and ISACfix defined. Please de-select one in the beginning of RTPencode.cpp - #endif +#include "isacfix.h" +#ifdef CODEC_ISAC +#error Cannot have both ISAC and ISACfix defined. Please de-select one in the beginning of RTPencode.cpp +#endif #endif #ifdef CODEC_G722 - #include "g722_interface.h" +#include "g722_interface.h" #endif #ifdef CODEC_G722_1_24 - #include "G722_1Interface.h" +#include "G722_1Interface.h" #endif #ifdef CODEC_G722_1_32 - #include "G722_1Interface.h" +#include "G722_1Interface.h" #endif #ifdef CODEC_G722_1_16 - #include "G722_1Interface.h" +#include "G722_1Interface.h" #endif #ifdef CODEC_G722_1C_24 - #include "G722_1Interface.h" +#include "G722_1Interface.h" #endif #ifdef CODEC_G722_1C_32 - #include "G722_1Interface.h" +#include "G722_1Interface.h" #endif #ifdef CODEC_G722_1C_48 - #include "G722_1Interface.h" +#include "G722_1Interface.h" #endif #ifdef CODEC_G726 - #include "G726Creation.h" - #include "G726Interface.h" +#include "G726Creation.h" +#include "G726Interface.h" #endif #ifdef CODEC_GSMFR - #include "GSMFRInterface.h" - #include "GSMFRCreation.h" +#include "GSMFRInterface.h" +#include "GSMFRCreation.h" #endif #if (defined(CODEC_CNGCODEC8) || defined(CODEC_CNGCODEC16) || \ - defined(CODEC_CNGCODEC32) || defined(CODEC_CNGCODEC48)) - #include "webrtc_cng.h" + defined(CODEC_CNGCODEC32) || defined(CODEC_CNGCODEC48)) +#include "webrtc_cng.h" #endif -#if ((defined CODEC_SPEEX_8)||(defined CODEC_SPEEX_16)) - #include "SpeexInterface.h" +#if ((defined CODEC_SPEEX_8) || (defined CODEC_SPEEX_16)) +#include "SpeexInterface.h" #endif /***********************************/ /* Global codec instance variables */ /***********************************/ -WebRtcVadInst *VAD_inst[2]; +WebRtcVadInst* VAD_inst[2]; #ifdef CODEC_G722 - G722EncInst *g722EncState[2]; +G722EncInst* g722EncState[2]; #endif #ifdef CODEC_G722_1_24 - G722_1_24_encinst_t *G722_1_24enc_inst[2]; +G722_1_24_encinst_t* G722_1_24enc_inst[2]; #endif #ifdef CODEC_G722_1_32 - G722_1_32_encinst_t *G722_1_32enc_inst[2]; +G722_1_32_encinst_t* G722_1_32enc_inst[2]; #endif #ifdef CODEC_G722_1_16 - G722_1_16_encinst_t *G722_1_16enc_inst[2]; +G722_1_16_encinst_t* G722_1_16enc_inst[2]; #endif #ifdef CODEC_G722_1C_24 - G722_1C_24_encinst_t *G722_1C_24enc_inst[2]; +G722_1C_24_encinst_t* G722_1C_24enc_inst[2]; #endif #ifdef CODEC_G722_1C_32 - G722_1C_32_encinst_t *G722_1C_32enc_inst[2]; +G722_1C_32_encinst_t* G722_1C_32enc_inst[2]; #endif #ifdef CODEC_G722_1C_48 - G722_1C_48_encinst_t *G722_1C_48enc_inst[2]; +G722_1C_48_encinst_t* G722_1C_48enc_inst[2]; #endif #ifdef CODEC_G726 - G726_encinst_t *G726enc_inst[2]; +G726_encinst_t* G726enc_inst[2]; #endif #ifdef CODEC_G729 - G729_encinst_t *G729enc_inst[2]; +G729_encinst_t* G729enc_inst[2]; #endif #ifdef CODEC_G729_1 - G729_1_inst_t *G729_1_inst[2]; +G729_1_inst_t* G729_1_inst[2]; #endif #ifdef CODEC_AMR - AMR_encinst_t *AMRenc_inst[2]; - int16_t AMR_bitrate; +AMR_encinst_t* AMRenc_inst[2]; +int16_t AMR_bitrate; #endif #ifdef CODEC_AMRWB - AMRWB_encinst_t *AMRWBenc_inst[2]; - int16_t AMRWB_bitrate; +AMRWB_encinst_t* AMRWBenc_inst[2]; +int16_t AMRWB_bitrate; #endif #ifdef CODEC_ILBC - IlbcEncoderInstance *iLBCenc_inst[2]; +IlbcEncoderInstance* iLBCenc_inst[2]; #endif #ifdef CODEC_ISAC - ISACStruct *ISAC_inst[2]; +ISACStruct* ISAC_inst[2]; #endif #ifdef NETEQ_ISACFIX_CODEC - ISACFIX_MainStruct *ISAC_inst[2]; +ISACFIX_MainStruct* ISAC_inst[2]; #endif #ifdef CODEC_ISAC_SWB - ISACStruct *ISACSWB_inst[2]; +ISACStruct* ISACSWB_inst[2]; #endif #ifdef CODEC_GSMFR - GSMFR_encinst_t *GSMFRenc_inst[2]; +GSMFR_encinst_t* GSMFRenc_inst[2]; #endif #if (defined(CODEC_CNGCODEC8) || defined(CODEC_CNGCODEC16) || \ - defined(CODEC_CNGCODEC32) || defined(CODEC_CNGCODEC48)) - CNG_enc_inst *CNGenc_inst[2]; + defined(CODEC_CNGCODEC32) || defined(CODEC_CNGCODEC48)) +CNG_enc_inst* CNGenc_inst[2]; #endif #ifdef CODEC_SPEEX_8 - SPEEX_encinst_t *SPEEX8enc_inst[2]; +SPEEX_encinst_t* SPEEX8enc_inst[2]; #endif #ifdef CODEC_SPEEX_16 - SPEEX_encinst_t *SPEEX16enc_inst[2]; +SPEEX_encinst_t* SPEEX16enc_inst[2]; #endif -int main(int argc, char* argv[]) -{ - int packet_size, fs; - webrtc::NetEqDecoder usedCodec; - int payloadType; - int bitrate = 0; - int useVAD, vad; - int useRed=0; - int len, enc_len; - int16_t org_data[4000]; - unsigned char rtp_data[8000]; - int16_t seqNo=0xFFF; - uint32_t ssrc=1235412312; - uint32_t timestamp=0xAC1245; - uint16_t length, plen; - uint32_t offset; - double sendtime = 0; - int red_PT[2] = {0}; - uint32_t red_TS[2] = {0}; - uint16_t red_len[2] = {0}; - int RTPheaderLen=12; - uint8_t red_data[8000]; +int main(int argc, char* argv[]) { + int packet_size, fs; + webrtc::NetEqDecoder usedCodec; + int payloadType; + int bitrate = 0; + int useVAD, vad; + int useRed = 0; + int len, enc_len; + int16_t org_data[4000]; + unsigned char rtp_data[8000]; + int16_t seqNo = 0xFFF; + uint32_t ssrc = 1235412312; + uint32_t timestamp = 0xAC1245; + uint16_t length, plen; + uint32_t offset; + double sendtime = 0; + int red_PT[2] = {0}; + uint32_t red_TS[2] = {0}; + uint16_t red_len[2] = {0}; + int RTPheaderLen = 12; + uint8_t red_data[8000]; #ifdef INSERT_OLD_PACKETS - uint16_t old_length, old_plen; - int old_enc_len; - int first_old_packet=1; - unsigned char old_rtp_data[8000]; - int packet_age=0; + uint16_t old_length, old_plen; + int old_enc_len; + int first_old_packet = 1; + unsigned char old_rtp_data[8000]; + int packet_age = 0; #endif #ifdef INSERT_DTMF_PACKETS - int NTone = 1; - int DTMFfirst = 1; - uint32_t DTMFtimestamp; - bool dtmfSent = false; + int NTone = 1; + int DTMFfirst = 1; + uint32_t DTMFtimestamp; + bool dtmfSent = false; #endif - bool usingStereo = false; - int stereoMode = 0; - int numChannels = 1; + bool usingStereo = false; + int stereoMode = 0; + int numChannels = 1; - /* check number of parameters */ - if ((argc != 6) && (argc != 7)) { - /* print help text and exit */ - printf("Application to encode speech into an RTP stream.\n"); - printf("The program reads a PCM file and encodes is using the specified codec.\n"); - printf("The coded speech is packetized in RTP packest and written to the output file.\n"); - printf("The format of the RTP stream file is simlilar to that of rtpplay,\n"); - printf("but with the receive time euqal to 0 for all packets.\n"); - printf("Usage:\n\n"); - printf("%s PCMfile RTPfile frameLen codec useVAD bitrate\n", argv[0]); - printf("where:\n"); + /* check number of parameters */ + if ((argc != 6) && (argc != 7)) { + /* print help text and exit */ + printf("Application to encode speech into an RTP stream.\n"); + printf( + "The program reads a PCM file and encodes is using the specified " + "codec.\n"); + printf( + "The coded speech is packetized in RTP packest and written to the " + "output file.\n"); + printf( + "The format of the RTP stream file is simlilar to that of rtpplay,\n"); + printf("but with the receive time euqal to 0 for all packets.\n"); + printf("Usage:\n\n"); + printf("%s PCMfile RTPfile frameLen codec useVAD bitrate\n", argv[0]); + printf("where:\n"); - printf("PCMfile : PCM speech input file\n\n"); + printf("PCMfile : PCM speech input file\n\n"); - printf("RTPfile : RTP stream output file\n\n"); + printf("RTPfile : RTP stream output file\n\n"); - printf("frameLen : 80...960... Number of samples per packet (limit depends on codec)\n\n"); + printf( + "frameLen : 80...960... Number of samples per packet (limit " + "depends on codec)\n\n"); - printf("codecName\n"); + printf("codecName\n"); #ifdef CODEC_PCM16B - printf(" : pcm16b 16 bit PCM (8kHz)\n"); + printf(" : pcm16b 16 bit PCM (8kHz)\n"); #endif #ifdef CODEC_PCM16B_WB - printf(" : pcm16b_wb 16 bit PCM (16kHz)\n"); + printf(" : pcm16b_wb 16 bit PCM (16kHz)\n"); #endif #ifdef CODEC_PCM16B_32KHZ - printf(" : pcm16b_swb32 16 bit PCM (32kHz)\n"); + printf(" : pcm16b_swb32 16 bit PCM (32kHz)\n"); #endif #ifdef CODEC_PCM16B_48KHZ - printf(" : pcm16b_swb48 16 bit PCM (48kHz)\n"); + printf(" : pcm16b_swb48 16 bit PCM (48kHz)\n"); #endif #ifdef CODEC_G711 - printf(" : pcma g711 A-law (8kHz)\n"); + printf(" : pcma g711 A-law (8kHz)\n"); #endif #ifdef CODEC_G711 - printf(" : pcmu g711 u-law (8kHz)\n"); + printf(" : pcmu g711 u-law (8kHz)\n"); #endif #ifdef CODEC_G729 - printf(" : g729 G729 (8kHz and 8kbps) CELP (One-Three frame(s)/packet)\n"); + printf( + " : g729 G729 (8kHz and 8kbps) CELP (One-Three " + "frame(s)/packet)\n"); #endif #ifdef CODEC_G729_1 - printf(" : g729.1 G729.1 (16kHz) variable rate (8--32 kbps)\n"); + printf( + " : g729.1 G729.1 (16kHz) variable rate (8--32 " + "kbps)\n"); #endif #ifdef CODEC_G722_1_16 - printf(" : g722.1_16 G722.1 coder (16kHz) (g722.1 with 16kbps)\n"); + printf( + " : g722.1_16 G722.1 coder (16kHz) (g722.1 with " + "16kbps)\n"); #endif #ifdef CODEC_G722_1_24 - printf(" : g722.1_24 G722.1 coder (16kHz) (the 24kbps version)\n"); + printf( + " : g722.1_24 G722.1 coder (16kHz) (the 24kbps " + "version)\n"); #endif #ifdef CODEC_G722_1_32 - printf(" : g722.1_32 G722.1 coder (16kHz) (the 32kbps version)\n"); + printf( + " : g722.1_32 G722.1 coder (16kHz) (the 32kbps " + "version)\n"); #endif #ifdef CODEC_G722_1C_24 - printf(" : g722.1C_24 G722.1 C coder (32kHz) (the 24kbps version)\n"); + printf( + " : g722.1C_24 G722.1 C coder (32kHz) (the 24kbps " + "version)\n"); #endif #ifdef CODEC_G722_1C_32 - printf(" : g722.1C_32 G722.1 C coder (32kHz) (the 32kbps version)\n"); + printf( + " : g722.1C_32 G722.1 C coder (32kHz) (the 32kbps " + "version)\n"); #endif #ifdef CODEC_G722_1C_48 - printf(" : g722.1C_48 G722.1 C coder (32kHz) (the 48kbps)\n"); + printf( + " : g722.1C_48 G722.1 C coder (32kHz) (the 48kbps)\n"); #endif #ifdef CODEC_G726 - printf(" : g726_16 G726 coder (8kHz) 16kbps\n"); - printf(" : g726_24 G726 coder (8kHz) 24kbps\n"); - printf(" : g726_32 G726 coder (8kHz) 32kbps\n"); - printf(" : g726_40 G726 coder (8kHz) 40kbps\n"); + printf(" : g726_16 G726 coder (8kHz) 16kbps\n"); + printf(" : g726_24 G726 coder (8kHz) 24kbps\n"); + printf(" : g726_32 G726 coder (8kHz) 32kbps\n"); + printf(" : g726_40 G726 coder (8kHz) 40kbps\n"); #endif #ifdef CODEC_AMR - printf(" : AMRXk Adaptive Multi Rate CELP codec (8kHz)\n"); - printf(" X = 4.75, 5.15, 5.9, 6.7, 7.4, 7.95, 10.2 or 12.2\n"); + printf( + " : AMRXk Adaptive Multi Rate CELP codec (8kHz)\n"); + printf( + " X = 4.75, 5.15, 5.9, 6.7, 7.4, 7.95, 10.2 " + "or 12.2\n"); #endif #ifdef CODEC_AMRWB - printf(" : AMRwbXk Adaptive Multi Rate Wideband CELP codec (16kHz)\n"); - printf(" X = 7, 9, 12, 14, 16, 18, 20, 23 or 24\n"); + printf( + " : AMRwbXk Adaptive Multi Rate Wideband CELP codec " + "(16kHz)\n"); + printf( + " X = 7, 9, 12, 14, 16, 18, 20, 23 or 24\n"); #endif #ifdef CODEC_ILBC - printf(" : ilbc iLBC codec (8kHz and 13.8kbps)\n"); + printf(" : ilbc iLBC codec (8kHz and 13.8kbps)\n"); #endif #ifdef CODEC_ISAC - printf(" : isac iSAC (16kHz and 32.0 kbps). To set rate specify a rate parameter as last parameter\n"); + printf( + " : isac iSAC (16kHz and 32.0 kbps). To set rate " + "specify a rate parameter as last parameter\n"); #endif #ifdef CODEC_ISAC_SWB - printf(" : isacswb iSAC SWB (32kHz and 32.0-52.0 kbps). To set rate specify a rate parameter as last parameter\n"); + printf( + " : isacswb iSAC SWB (32kHz and 32.0-52.0 kbps). To " + "set rate specify a rate parameter as last parameter\n"); #endif #ifdef CODEC_GSMFR - printf(" : gsmfr GSM FR codec (8kHz and 13kbps)\n"); + printf(" : gsmfr GSM FR codec (8kHz and 13kbps)\n"); #endif #ifdef CODEC_G722 - printf(" : g722 g722 coder (16kHz) (the 64kbps version)\n"); + printf( + " : g722 g722 coder (16kHz) (the 64kbps " + "version)\n"); #endif #ifdef CODEC_SPEEX_8 - printf(" : speex8 speex coder (8 kHz)\n"); + printf(" : speex8 speex coder (8 kHz)\n"); #endif #ifdef CODEC_SPEEX_16 - printf(" : speex16 speex coder (16 kHz)\n"); + printf(" : speex16 speex coder (16 kHz)\n"); #endif #ifdef CODEC_RED #ifdef CODEC_G711 - printf(" : red_pcm Redundancy RTP packet with 2*G711A frames\n"); + printf( + " : red_pcm Redundancy RTP packet with 2*G711A " + "frames\n"); #endif #ifdef CODEC_ISAC - printf(" : red_isac Redundancy RTP packet with 2*iSAC frames\n"); + printf( + " : red_isac Redundancy RTP packet with 2*iSAC " + "frames\n"); #endif #endif - printf("\n"); + printf("\n"); #if (defined(CODEC_CNGCODEC8) || defined(CODEC_CNGCODEC16) || \ - defined(CODEC_CNGCODEC32) || defined(CODEC_CNGCODEC48)) - printf("useVAD : 0 Voice Activity Detection is switched off\n"); - printf(" : 1 Voice Activity Detection is switched on\n\n"); + defined(CODEC_CNGCODEC32) || defined(CODEC_CNGCODEC48)) + printf("useVAD : 0 Voice Activity Detection is switched off\n"); + printf(" : 1 Voice Activity Detection is switched on\n\n"); #else - printf("useVAD : 0 Voice Activity Detection switched off (on not supported)\n\n"); + printf( + "useVAD : 0 Voice Activity Detection switched off (on not " + "supported)\n\n"); #endif - printf("bitrate : Codec bitrate in bps (only applies to vbr codecs)\n\n"); + printf( + "bitrate : Codec bitrate in bps (only applies to vbr codecs)\n\n"); - return(0); - } + return (0); + } - FILE* in_file=fopen(argv[1],"rb"); - CHECK_NOT_NULL(in_file); - printf("Input file: %s\n",argv[1]); - FILE* out_file=fopen(argv[2],"wb"); - CHECK_NOT_NULL(out_file); - printf("Output file: %s\n\n",argv[2]); - packet_size=atoi(argv[3]); - CHECK_NOT_NULL(packet_size); - printf("Packet size: %i\n",packet_size); + FILE* in_file = fopen(argv[1], "rb"); + CHECK_NOT_NULL(in_file); + printf("Input file: %s\n", argv[1]); + FILE* out_file = fopen(argv[2], "wb"); + CHECK_NOT_NULL(out_file); + printf("Output file: %s\n\n", argv[2]); + packet_size = atoi(argv[3]); + CHECK_NOT_NULL(packet_size); + printf("Packet size: %i\n", packet_size); - // check for stereo - if(argv[4][strlen(argv[4])-1] == '*') { - // use stereo - usingStereo = true; - numChannels = 2; - argv[4][strlen(argv[4])-1] = '\0'; - } + // check for stereo + if (argv[4][strlen(argv[4]) - 1] == '*') { + // use stereo + usingStereo = true; + numChannels = 2; + argv[4][strlen(argv[4]) - 1] = '\0'; + } - NetEQTest_GetCodec_and_PT(argv[4], &usedCodec, &payloadType, packet_size, &fs, &bitrate, &useRed); + NetEQTest_GetCodec_and_PT(argv[4], &usedCodec, &payloadType, packet_size, &fs, + &bitrate, &useRed); - if(useRed) { - RTPheaderLen = 12 + 4 + 1; /* standard RTP = 12; 4 bytes per redundant payload, except last one which is 1 byte */ - } + if (useRed) { + RTPheaderLen = 12 + 4 + 1; /* standard RTP = 12; 4 bytes per redundant + payload, except last one which is 1 byte */ + } - useVAD=atoi(argv[5]); + useVAD = atoi(argv[5]); #if !(defined(CODEC_CNGCODEC8) || defined(CODEC_CNGCODEC16) || \ - defined(CODEC_CNGCODEC32) || defined(CODEC_CNGCODEC48)) - if (useVAD!=0) { - printf("Error: this simulation does not support VAD/DTX/CNG\n"); - } + defined(CODEC_CNGCODEC32) || defined(CODEC_CNGCODEC48)) + if (useVAD != 0) { + printf("Error: this simulation does not support VAD/DTX/CNG\n"); + } #endif - - // check stereo type - if(usingStereo) - { - switch(usedCodec) - { - // sample based codecs - case webrtc::kDecoderPCMu: - case webrtc::kDecoderPCMa: - case webrtc::kDecoderG722: - { - // 1 octet per sample - stereoMode = STEREO_MODE_SAMPLE_1; - break; - } - case webrtc::kDecoderPCM16B: - case webrtc::kDecoderPCM16Bwb: - case webrtc::kDecoderPCM16Bswb32kHz: - case webrtc::kDecoderPCM16Bswb48kHz: - { - // 2 octets per sample - stereoMode = STEREO_MODE_SAMPLE_2; - break; - } - // fixed-rate frame codecs (with internal VAD) - default: - { - printf("Cannot use codec %s as stereo codec\n", argv[4]); - exit(0); - } - } - } + // check stereo type + if (usingStereo) { + switch (usedCodec) { + // sample based codecs + case webrtc::kDecoderPCMu: + case webrtc::kDecoderPCMa: + case webrtc::kDecoderG722: { + // 1 octet per sample + stereoMode = STEREO_MODE_SAMPLE_1; + break; + } + case webrtc::kDecoderPCM16B: + case webrtc::kDecoderPCM16Bwb: + case webrtc::kDecoderPCM16Bswb32kHz: + case webrtc::kDecoderPCM16Bswb48kHz: { + // 2 octets per sample + stereoMode = STEREO_MODE_SAMPLE_2; + break; + } - if ((usedCodec == webrtc::kDecoderISAC) || (usedCodec == webrtc::kDecoderISACswb)) - { - if (argc != 7) - { - if (usedCodec == webrtc::kDecoderISAC) - { - bitrate = 32000; - printf( - "Running iSAC at default bitrate of 32000 bps (to specify explicitly add the bps as last parameter)\n"); - } - else // (usedCodec==webrtc::kDecoderISACswb) - { - bitrate = 56000; - printf( - "Running iSAC at default bitrate of 56000 bps (to specify explicitly add the bps as last parameter)\n"); - } - } - else - { - bitrate = atoi(argv[6]); - if (usedCodec == webrtc::kDecoderISAC) - { - if ((bitrate < 10000) || (bitrate > 32000)) - { - printf( - "Error: iSAC bitrate must be between 10000 and 32000 bps (%i is invalid)\n", - bitrate); - exit(0); - } - printf("Running iSAC at bitrate of %i bps\n", bitrate); - } - else // (usedCodec==webrtc::kDecoderISACswb) - { - if ((bitrate < 32000) || (bitrate > 56000)) - { - printf( - "Error: iSAC SWB bitrate must be between 32000 and 56000 bps (%i is invalid)\n", - bitrate); - exit(0); - } - } - } + // fixed-rate frame codecs (with internal VAD) + default: { + printf("Cannot use codec %s as stereo codec\n", argv[4]); + exit(0); + } } - else - { - if (argc == 7) - { - printf( - "Error: Bitrate parameter can only be specified for iSAC, G.723, and G.729.1\n"); - exit(0); - } - } - - if(useRed) { - printf("Redundancy engaged. "); - } - printf("Used codec: %i\n",usedCodec); - printf("Payload type: %i\n",payloadType); - - NetEQTest_init_coders(usedCodec, packet_size, bitrate, fs, useVAD, numChannels); + } - /* write file header */ - //fprintf(out_file, "#!RTPencode%s\n", "1.0"); - fprintf(out_file, "#!rtpplay%s \n", "1.0"); // this is the string that rtpplay needs - uint32_t dummy_variable = 0; // should be converted to network endian format, but does not matter when 0 - if (fwrite(&dummy_variable, 4, 1, out_file) != 1) { - return -1; + if ((usedCodec == webrtc::kDecoderISAC) || + (usedCodec == webrtc::kDecoderISACswb)) { + if (argc != 7) { + if (usedCodec == webrtc::kDecoderISAC) { + bitrate = 32000; + printf( + "Running iSAC at default bitrate of 32000 bps (to specify " + "explicitly add the bps as last parameter)\n"); + } else // (usedCodec==webrtc::kDecoderISACswb) + { + bitrate = 56000; + printf( + "Running iSAC at default bitrate of 56000 bps (to specify " + "explicitly add the bps as last parameter)\n"); + } + } else { + bitrate = atoi(argv[6]); + if (usedCodec == webrtc::kDecoderISAC) { + if ((bitrate < 10000) || (bitrate > 32000)) { + printf( + "Error: iSAC bitrate must be between 10000 and 32000 bps (%i is " + "invalid)\n", + bitrate); + exit(0); } - if (fwrite(&dummy_variable, 4, 1, out_file) != 1) { - return -1; - } - if (fwrite(&dummy_variable, 4, 1, out_file) != 1) { - return -1; - } - if (fwrite(&dummy_variable, 2, 1, out_file) != 1) { - return -1; - } - if (fwrite(&dummy_variable, 2, 1, out_file) != 1) { - return -1; + printf("Running iSAC at bitrate of %i bps\n", bitrate); + } else // (usedCodec==webrtc::kDecoderISACswb) + { + if ((bitrate < 32000) || (bitrate > 56000)) { + printf( + "Error: iSAC SWB bitrate must be between 32000 and 56000 bps (%i " + "is invalid)\n", + bitrate); + exit(0); } + } + } + } else { + if (argc == 7) { + printf( + "Error: Bitrate parameter can only be specified for iSAC, G.723, and " + "G.729.1\n"); + exit(0); + } + } + + if (useRed) { + printf("Redundancy engaged. "); + } + printf("Used codec: %i\n", usedCodec); + printf("Payload type: %i\n", payloadType); + + NetEQTest_init_coders(usedCodec, packet_size, bitrate, fs, useVAD, + numChannels); + + /* write file header */ + // fprintf(out_file, "#!RTPencode%s\n", "1.0"); + fprintf(out_file, "#!rtpplay%s \n", + "1.0"); // this is the string that rtpplay needs + uint32_t dummy_variable = 0; // should be converted to network endian format, + // but does not matter when 0 + if (fwrite(&dummy_variable, 4, 1, out_file) != 1) { + return -1; + } + if (fwrite(&dummy_variable, 4, 1, out_file) != 1) { + return -1; + } + if (fwrite(&dummy_variable, 4, 1, out_file) != 1) { + return -1; + } + if (fwrite(&dummy_variable, 2, 1, out_file) != 1) { + return -1; + } + if (fwrite(&dummy_variable, 2, 1, out_file) != 1) { + return -1; + } #ifdef TIMESTAMP_WRAPAROUND - timestamp = 0xFFFFFFFF - fs*10; /* should give wrap-around in 10 seconds */ + timestamp = 0xFFFFFFFF - fs * 10; /* should give wrap-around in 10 seconds */ #endif #if defined(RANDOM_DATA) | defined(RANDOM_PAYLOAD_DATA) - srand(RANDOM_SEED); + srand(RANDOM_SEED); #endif - /* if redundancy is used, the first redundant payload is zero length */ - red_len[0] = 0; + /* if redundancy is used, the first redundant payload is zero length */ + red_len[0] = 0; - /* read first frame */ - len=fread(org_data,2,packet_size * numChannels,in_file) / numChannels; + /* read first frame */ + len = fread(org_data, 2, packet_size * numChannels, in_file) / numChannels; - /* de-interleave if stereo */ - if ( usingStereo ) - { - stereoDeInterleave(org_data, len * numChannels); - } - - while (len==packet_size) { + /* de-interleave if stereo */ + if (usingStereo) { + stereoDeInterleave(org_data, len * numChannels); + } + while (len == packet_size) { #ifdef INSERT_DTMF_PACKETS - dtmfSent = false; + dtmfSent = false; - if ( sendtime >= NTone * DTMF_PACKET_INTERVAL ) { - if ( sendtime < NTone * DTMF_PACKET_INTERVAL + DTMF_DURATION ) { - // tone has not ended - if (DTMFfirst==1) { - DTMFtimestamp = timestamp; // save this timestamp - DTMFfirst=0; - } - makeRTPheader(rtp_data, NETEQ_CODEC_AVT_PT, seqNo,DTMFtimestamp, ssrc); - enc_len = makeDTMFpayload(&rtp_data[12], NTone % 12, 0, 4, (int) (sendtime - NTone * DTMF_PACKET_INTERVAL)*(fs/1000) + len); - } - else { - // tone has ended - makeRTPheader(rtp_data, NETEQ_CODEC_AVT_PT, seqNo,DTMFtimestamp, ssrc); - enc_len = makeDTMFpayload(&rtp_data[12], NTone % 12, 1, 4, DTMF_DURATION*(fs/1000)); - NTone++; - DTMFfirst=1; - } - - /* write RTP packet to file */ - length = htons(12 + enc_len + 8); - plen = htons(12 + enc_len); - offset = (uint32_t) sendtime; //(timestamp/(fs/1000)); - offset = htonl(offset); - if (fwrite(&length, 2, 1, out_file) != 1) { - return -1; - } - if (fwrite(&plen, 2, 1, out_file) != 1) { - return -1; - } - if (fwrite(&offset, 4, 1, out_file) != 1) { - return -1; - } - if (fwrite(rtp_data, 12 + enc_len, 1, out_file) != 1) { - return -1; - } - - dtmfSent = true; + if (sendtime >= NTone * DTMF_PACKET_INTERVAL) { + if (sendtime < NTone * DTMF_PACKET_INTERVAL + DTMF_DURATION) { + // tone has not ended + if (DTMFfirst == 1) { + DTMFtimestamp = timestamp; // save this timestamp + DTMFfirst = 0; } + makeRTPheader(rtp_data, NETEQ_CODEC_AVT_PT, seqNo, DTMFtimestamp, ssrc); + enc_len = makeDTMFpayload( + &rtp_data[12], NTone % 12, 0, 4, + (int)(sendtime - NTone * DTMF_PACKET_INTERVAL) * (fs / 1000) + len); + } else { + // tone has ended + makeRTPheader(rtp_data, NETEQ_CODEC_AVT_PT, seqNo, DTMFtimestamp, ssrc); + enc_len = makeDTMFpayload(&rtp_data[12], NTone % 12, 1, 4, + DTMF_DURATION * (fs / 1000)); + NTone++; + DTMFfirst = 1; + } + + /* write RTP packet to file */ + length = htons(12 + enc_len + 8); + plen = htons(12 + enc_len); + offset = (uint32_t)sendtime; //(timestamp/(fs/1000)); + offset = htonl(offset); + if (fwrite(&length, 2, 1, out_file) != 1) { + return -1; + } + if (fwrite(&plen, 2, 1, out_file) != 1) { + return -1; + } + if (fwrite(&offset, 4, 1, out_file) != 1) { + return -1; + } + if (fwrite(rtp_data, 12 + enc_len, 1, out_file) != 1) { + return -1; + } + + dtmfSent = true; + } #endif #ifdef NO_DTMF_OVERDUB - /* If DTMF is sent, we should not send any speech packets during the same time */ - if (dtmfSent) { - enc_len = 0; - } - else { + /* If DTMF is sent, we should not send any speech packets during the same + * time */ + if (dtmfSent) { + enc_len = 0; + } else { #endif - /* encode frame */ - enc_len=NetEQTest_encode(usedCodec, org_data, packet_size, &rtp_data[12] ,fs,&vad, useVAD, bitrate, numChannels); - if (enc_len==-1) { - printf("Error encoding frame\n"); - exit(0); - } + /* encode frame */ + enc_len = + NetEQTest_encode(usedCodec, org_data, packet_size, &rtp_data[12], fs, + &vad, useVAD, bitrate, numChannels); + if (enc_len == -1) { + printf("Error encoding frame\n"); + exit(0); + } - if ( usingStereo && - stereoMode != STEREO_MODE_FRAME && - vad == 1 ) - { - // interleave the encoded payload for sample-based codecs (not for CNG) - stereoInterleave(&rtp_data[12], enc_len, stereoMode); - } + if (usingStereo && stereoMode != STEREO_MODE_FRAME && vad == 1) { + // interleave the encoded payload for sample-based codecs (not for CNG) + stereoInterleave(&rtp_data[12], enc_len, stereoMode); + } #ifdef NO_DTMF_OVERDUB - } + } #endif - - if (enc_len > 0 && (sendtime <= STOPSENDTIME || sendtime > RESTARTSENDTIME)) { - if(useRed) { - if(red_len[0] > 0) { - memmove(&rtp_data[RTPheaderLen+red_len[0]], &rtp_data[12], enc_len); - memcpy(&rtp_data[RTPheaderLen], red_data, red_len[0]); - red_len[1] = enc_len; - red_TS[1] = timestamp; - if(vad) - red_PT[1] = payloadType; - else - red_PT[1] = NETEQ_CODEC_CN_PT; + if (enc_len > 0 && + (sendtime <= STOPSENDTIME || sendtime > RESTARTSENDTIME)) { + if (useRed) { + if (red_len[0] > 0) { + memmove(&rtp_data[RTPheaderLen + red_len[0]], &rtp_data[12], enc_len); + memcpy(&rtp_data[RTPheaderLen], red_data, red_len[0]); - makeRedundantHeader(rtp_data, red_PT, 2, red_TS, red_len, seqNo++, ssrc); + red_len[1] = enc_len; + red_TS[1] = timestamp; + if (vad) + red_PT[1] = payloadType; + else + red_PT[1] = NETEQ_CODEC_CN_PT; + makeRedundantHeader(rtp_data, red_PT, 2, red_TS, red_len, seqNo++, + ssrc); - enc_len += red_len[0] + RTPheaderLen - 12; - } - else { // do not use redundancy payload for this packet, i.e., only last payload - memmove(&rtp_data[RTPheaderLen-4], &rtp_data[12], enc_len); - //memcpy(&rtp_data[RTPheaderLen], red_data, red_len[0]); + enc_len += red_len[0] + RTPheaderLen - 12; + } else { // do not use redundancy payload for this packet, i.e., only + // last payload + memmove(&rtp_data[RTPheaderLen - 4], &rtp_data[12], enc_len); + // memcpy(&rtp_data[RTPheaderLen], red_data, red_len[0]); - red_len[1] = enc_len; - red_TS[1] = timestamp; - if(vad) - red_PT[1] = payloadType; - else - red_PT[1] = NETEQ_CODEC_CN_PT; + red_len[1] = enc_len; + red_TS[1] = timestamp; + if (vad) + red_PT[1] = payloadType; + else + red_PT[1] = NETEQ_CODEC_CN_PT; - makeRedundantHeader(rtp_data, red_PT, 2, red_TS, red_len, seqNo++, ssrc); + makeRedundantHeader(rtp_data, red_PT, 2, red_TS, red_len, seqNo++, + ssrc); - - enc_len += red_len[0] + RTPheaderLen - 4 - 12; // 4 is length of redundancy header (not used) - } - } - else { - - /* make RTP header */ - if (vad) // regular speech data - makeRTPheader(rtp_data, payloadType, seqNo++,timestamp, ssrc); - else // CNG data - makeRTPheader(rtp_data, NETEQ_CODEC_CN_PT, seqNo++,timestamp, ssrc); - - } + enc_len += red_len[0] + RTPheaderLen - 4 - + 12; // 4 is length of redundancy header (not used) + } + } else { + /* make RTP header */ + if (vad) // regular speech data + makeRTPheader(rtp_data, payloadType, seqNo++, timestamp, ssrc); + else // CNG data + makeRTPheader(rtp_data, NETEQ_CODEC_CN_PT, seqNo++, timestamp, ssrc); + } #ifdef MULTIPLE_SAME_TIMESTAMP - int mult_pack=0; - do { -#endif //MULTIPLE_SAME_TIMESTAMP - /* write RTP packet to file */ - length = htons(12 + enc_len + 8); - plen = htons(12 + enc_len); - offset = (uint32_t) sendtime; - //(timestamp/(fs/1000)); - offset = htonl(offset); - if (fwrite(&length, 2, 1, out_file) != 1) { - return -1; - } - if (fwrite(&plen, 2, 1, out_file) != 1) { - return -1; - } - if (fwrite(&offset, 4, 1, out_file) != 1) { - return -1; - } + int mult_pack = 0; + do { +#endif // MULTIPLE_SAME_TIMESTAMP + /* write RTP packet to file */ + length = htons(12 + enc_len + 8); + plen = htons(12 + enc_len); + offset = (uint32_t)sendtime; + //(timestamp/(fs/1000)); + offset = htonl(offset); + if (fwrite(&length, 2, 1, out_file) != 1) { + return -1; + } + if (fwrite(&plen, 2, 1, out_file) != 1) { + return -1; + } + if (fwrite(&offset, 4, 1, out_file) != 1) { + return -1; + } #ifdef RANDOM_DATA - for (int k=0; k<12+enc_len; k++) { - rtp_data[k] = rand() + rand(); - } + for (int k = 0; k < 12 + enc_len; k++) { + rtp_data[k] = rand() + rand(); + } #endif #ifdef RANDOM_PAYLOAD_DATA - for (int k=12; k<12+enc_len; k++) { - rtp_data[k] = rand() + rand(); - } + for (int k = 12; k < 12 + enc_len; k++) { + rtp_data[k] = rand() + rand(); + } #endif - if (fwrite(rtp_data, 12 + enc_len, 1, out_file) != 1) { - return -1; - } + if (fwrite(rtp_data, 12 + enc_len, 1, out_file) != 1) { + return -1; + } #ifdef MULTIPLE_SAME_TIMESTAMP - } while ( (seqNo%REPEAT_PACKET_DISTANCE == 0) && (mult_pack++ < REPEAT_PACKET_COUNT) ); -#endif //MULTIPLE_SAME_TIMESTAMP + } while ((seqNo % REPEAT_PACKET_DISTANCE == 0) && + (mult_pack++ < REPEAT_PACKET_COUNT)); +#endif // MULTIPLE_SAME_TIMESTAMP #ifdef INSERT_OLD_PACKETS - if (packet_age >= OLD_PACKET*fs) { - if (!first_old_packet) { - // send the old packet - if (fwrite(&old_length, 2, 1, - out_file) != 1) { - return -1; - } - if (fwrite(&old_plen, 2, 1, - out_file) != 1) { - return -1; - } - if (fwrite(&offset, 4, 1, - out_file) != 1) { - return -1; - } - if (fwrite(old_rtp_data, 12 + old_enc_len, - 1, out_file) != 1) { - return -1; - } - } - // store current packet as old - old_length=length; - old_plen=plen; - memcpy(old_rtp_data,rtp_data,12+enc_len); - old_enc_len=enc_len; - first_old_packet=0; - packet_age=0; - - } - packet_age += packet_size; -#endif - - if(useRed) { - /* move data to redundancy store */ -#ifdef CODEC_ISAC - if(usedCodec==webrtc::kDecoderISAC) - { - assert(!usingStereo); // Cannot handle stereo yet - red_len[0] = - WebRtcIsac_GetRedPayload(ISAC_inst[0], red_data); - } - else - { -#endif - memcpy(red_data, &rtp_data[RTPheaderLen+red_len[0]], enc_len); - red_len[0]=red_len[1]; -#ifdef CODEC_ISAC - } -#endif - red_TS[0]=red_TS[1]; - red_PT[0]=red_PT[1]; - } - - } - - /* read next frame */ - len=fread(org_data,2,packet_size * numChannels,in_file) / numChannels; - /* de-interleave if stereo */ - if ( usingStereo ) - { - stereoDeInterleave(org_data, len * numChannels); + if (packet_age >= OLD_PACKET * fs) { + if (!first_old_packet) { + // send the old packet + if (fwrite(&old_length, 2, 1, out_file) != 1) { + return -1; + } + if (fwrite(&old_plen, 2, 1, out_file) != 1) { + return -1; + } + if (fwrite(&offset, 4, 1, out_file) != 1) { + return -1; + } + if (fwrite(old_rtp_data, 12 + old_enc_len, 1, out_file) != 1) { + return -1; + } } + // store current packet as old + old_length = length; + old_plen = plen; + memcpy(old_rtp_data, rtp_data, 12 + enc_len); + old_enc_len = enc_len; + first_old_packet = 0; + packet_age = 0; + } + packet_age += packet_size; +#endif - if (payloadType==NETEQ_CODEC_G722_PT) - timestamp+=len>>1; - else - timestamp+=len; + if (useRed) { +/* move data to redundancy store */ +#ifdef CODEC_ISAC + if (usedCodec == webrtc::kDecoderISAC) { + assert(!usingStereo); // Cannot handle stereo yet + red_len[0] = WebRtcIsac_GetRedPayload(ISAC_inst[0], red_data); + } else { +#endif + memcpy(red_data, &rtp_data[RTPheaderLen + red_len[0]], enc_len); + red_len[0] = red_len[1]; +#ifdef CODEC_ISAC + } +#endif + red_TS[0] = red_TS[1]; + red_PT[0] = red_PT[1]; + } + } - sendtime += (double) len/(fs/1000); - } - - NetEQTest_free_coders(usedCodec, numChannels); - fclose(in_file); - fclose(out_file); - printf("Done!\n"); + /* read next frame */ + len = fread(org_data, 2, packet_size * numChannels, in_file) / numChannels; + /* de-interleave if stereo */ + if (usingStereo) { + stereoDeInterleave(org_data, len * numChannels); + } - return(0); + if (payloadType == NETEQ_CODEC_G722_PT) + timestamp += len >> 1; + else + timestamp += len; + + sendtime += (double)len / (fs / 1000); + } + + NetEQTest_free_coders(usedCodec, numChannels); + fclose(in_file); + fclose(out_file); + printf("Done!\n"); + + return (0); } - - - /****************/ /* Subfunctions */ /****************/ @@ -828,998 +848,1038 @@ void NetEQTest_GetCodec_and_PT(char* name, int* fs, int* bitrate, int* useRed) { + *bitrate = 0; /* Default bitrate setting */ + *useRed = 0; /* Default no redundancy */ - *bitrate = 0; /* Default bitrate setting */ - *useRed = 0; /* Default no redundancy */ - - if(!strcmp(name,"pcmu")){ - *codec=webrtc::kDecoderPCMu; - *PT=NETEQ_CODEC_PCMU_PT; - *fs=8000; - } - else if(!strcmp(name,"pcma")){ - *codec=webrtc::kDecoderPCMa; - *PT=NETEQ_CODEC_PCMA_PT; - *fs=8000; - } - else if(!strcmp(name,"pcm16b")){ - *codec=webrtc::kDecoderPCM16B; - *PT=NETEQ_CODEC_PCM16B_PT; - *fs=8000; - } - else if(!strcmp(name,"pcm16b_wb")){ - *codec=webrtc::kDecoderPCM16Bwb; - *PT=NETEQ_CODEC_PCM16B_WB_PT; - *fs=16000; - } - else if(!strcmp(name,"pcm16b_swb32")){ - *codec=webrtc::kDecoderPCM16Bswb32kHz; - *PT=NETEQ_CODEC_PCM16B_SWB32KHZ_PT; - *fs=32000; - } - else if(!strcmp(name,"pcm16b_swb48")){ - *codec=webrtc::kDecoderPCM16Bswb48kHz; - *PT=NETEQ_CODEC_PCM16B_SWB48KHZ_PT; - *fs=48000; - } - else if(!strcmp(name,"g722")){ - *codec=webrtc::kDecoderG722; - *PT=NETEQ_CODEC_G722_PT; - *fs=16000; - } - else if((!strcmp(name,"ilbc"))&&((frameLen%240==0)||(frameLen%160==0))){ - *fs=8000; - *codec=webrtc::kDecoderILBC; - *PT=NETEQ_CODEC_ILBC_PT; - } - else if(!strcmp(name,"isac")){ - *fs=16000; - *codec=webrtc::kDecoderISAC; - *PT=NETEQ_CODEC_ISAC_PT; - } - else if(!strcmp(name,"isacswb")){ - *fs=32000; - *codec=webrtc::kDecoderISACswb; - *PT=NETEQ_CODEC_ISACSWB_PT; - } - else if(!strcmp(name,"red_pcm")){ - *codec=webrtc::kDecoderPCMa; - *PT=NETEQ_CODEC_PCMA_PT; /* this will be the PT for the sub-headers */ - *fs=8000; - *useRed = 1; - } else if(!strcmp(name,"red_isac")){ - *codec=webrtc::kDecoderISAC; - *PT=NETEQ_CODEC_ISAC_PT; /* this will be the PT for the sub-headers */ - *fs=16000; - *useRed = 1; - } else { - printf("Error: Not a supported codec (%s)\n", name); - exit(0); - } - + if (!strcmp(name, "pcmu")) { + *codec = webrtc::kDecoderPCMu; + *PT = NETEQ_CODEC_PCMU_PT; + *fs = 8000; + } else if (!strcmp(name, "pcma")) { + *codec = webrtc::kDecoderPCMa; + *PT = NETEQ_CODEC_PCMA_PT; + *fs = 8000; + } else if (!strcmp(name, "pcm16b")) { + *codec = webrtc::kDecoderPCM16B; + *PT = NETEQ_CODEC_PCM16B_PT; + *fs = 8000; + } else if (!strcmp(name, "pcm16b_wb")) { + *codec = webrtc::kDecoderPCM16Bwb; + *PT = NETEQ_CODEC_PCM16B_WB_PT; + *fs = 16000; + } else if (!strcmp(name, "pcm16b_swb32")) { + *codec = webrtc::kDecoderPCM16Bswb32kHz; + *PT = NETEQ_CODEC_PCM16B_SWB32KHZ_PT; + *fs = 32000; + } else if (!strcmp(name, "pcm16b_swb48")) { + *codec = webrtc::kDecoderPCM16Bswb48kHz; + *PT = NETEQ_CODEC_PCM16B_SWB48KHZ_PT; + *fs = 48000; + } else if (!strcmp(name, "g722")) { + *codec = webrtc::kDecoderG722; + *PT = NETEQ_CODEC_G722_PT; + *fs = 16000; + } else if ((!strcmp(name, "ilbc")) && + ((frameLen % 240 == 0) || (frameLen % 160 == 0))) { + *fs = 8000; + *codec = webrtc::kDecoderILBC; + *PT = NETEQ_CODEC_ILBC_PT; + } else if (!strcmp(name, "isac")) { + *fs = 16000; + *codec = webrtc::kDecoderISAC; + *PT = NETEQ_CODEC_ISAC_PT; + } else if (!strcmp(name, "isacswb")) { + *fs = 32000; + *codec = webrtc::kDecoderISACswb; + *PT = NETEQ_CODEC_ISACSWB_PT; + } else if (!strcmp(name, "red_pcm")) { + *codec = webrtc::kDecoderPCMa; + *PT = NETEQ_CODEC_PCMA_PT; /* this will be the PT for the sub-headers */ + *fs = 8000; + *useRed = 1; + } else if (!strcmp(name, "red_isac")) { + *codec = webrtc::kDecoderISAC; + *PT = NETEQ_CODEC_ISAC_PT; /* this will be the PT for the sub-headers */ + *fs = 16000; + *useRed = 1; + } else { + printf("Error: Not a supported codec (%s)\n", name); + exit(0); + } } +int NetEQTest_init_coders(webrtc::NetEqDecoder coder, + int enc_frameSize, + int bitrate, + int sampfreq, + int vad, + int numChannels) { + int ok = 0; - - -int NetEQTest_init_coders(webrtc::NetEqDecoder coder, int enc_frameSize, int bitrate, int sampfreq , int vad, int numChannels){ - - int ok=0; - - for (int k = 0; k < numChannels; k++) - { - VAD_inst[k] = WebRtcVad_Create(); - if (!VAD_inst[k]) { - printf("Error: Couldn't allocate memory for VAD instance\n"); - exit(0); - } - ok=WebRtcVad_Init(VAD_inst[k]); - if (ok==-1) { - printf("Error: Initialization of VAD struct failed\n"); - exit(0); - } - + for (int k = 0; k < numChannels; k++) { + VAD_inst[k] = WebRtcVad_Create(); + if (!VAD_inst[k]) { + printf("Error: Couldn't allocate memory for VAD instance\n"); + exit(0); + } + ok = WebRtcVad_Init(VAD_inst[k]); + if (ok == -1) { + printf("Error: Initialization of VAD struct failed\n"); + exit(0); + } #if (defined(CODEC_CNGCODEC8) || defined(CODEC_CNGCODEC16) || \ - defined(CODEC_CNGCODEC32) || defined(CODEC_CNGCODEC48)) - ok=WebRtcCng_CreateEnc(&CNGenc_inst[k]); - if (ok!=0) { - printf("Error: Couldn't allocate memory for CNG encoding instance\n"); - exit(0); - } - if(sampfreq <= 16000) { - ok=WebRtcCng_InitEnc(CNGenc_inst[k],sampfreq, 200, 5); - if (ok==-1) { - printf("Error: Initialization of CNG struct failed. Error code %d\n", - WebRtcCng_GetErrorCodeEnc(CNGenc_inst[k])); - exit(0); - } - } + defined(CODEC_CNGCODEC32) || defined(CODEC_CNGCODEC48)) + ok = WebRtcCng_CreateEnc(&CNGenc_inst[k]); + if (ok != 0) { + printf("Error: Couldn't allocate memory for CNG encoding instance\n"); + exit(0); + } + if (sampfreq <= 16000) { + ok = WebRtcCng_InitEnc(CNGenc_inst[k], sampfreq, 200, 5); + if (ok == -1) { + printf("Error: Initialization of CNG struct failed. Error code %d\n", + WebRtcCng_GetErrorCodeEnc(CNGenc_inst[k])); + exit(0); + } + } #endif - switch (coder) { + switch (coder) { #ifdef CODEC_PCM16B - case webrtc::kDecoderPCM16B : + case webrtc::kDecoderPCM16B: #endif #ifdef CODEC_PCM16B_WB - case webrtc::kDecoderPCM16Bwb : + case webrtc::kDecoderPCM16Bwb: #endif #ifdef CODEC_PCM16B_32KHZ - case webrtc::kDecoderPCM16Bswb32kHz : + case webrtc::kDecoderPCM16Bswb32kHz: #endif #ifdef CODEC_PCM16B_48KHZ - case webrtc::kDecoderPCM16Bswb48kHz : + case webrtc::kDecoderPCM16Bswb48kHz: #endif #ifdef CODEC_G711 - case webrtc::kDecoderPCMu : - case webrtc::kDecoderPCMa : + case webrtc::kDecoderPCMu: + case webrtc::kDecoderPCMa: #endif // do nothing break; #ifdef CODEC_G729 - case webrtc::kDecoderG729: - if (sampfreq==8000) { - if ((enc_frameSize==80)||(enc_frameSize==160)||(enc_frameSize==240)||(enc_frameSize==320)||(enc_frameSize==400)||(enc_frameSize==480)) { - ok=WebRtcG729_CreateEnc(&G729enc_inst[k]); - if (ok!=0) { - printf("Error: Couldn't allocate memory for G729 encoding instance\n"); - exit(0); - } - } else { - printf("\nError: g729 only supports 10, 20, 30, 40, 50 or 60 ms!!\n\n"); - exit(0); + case webrtc::kDecoderG729: + if (sampfreq == 8000) { + if ((enc_frameSize == 80) || (enc_frameSize == 160) || + (enc_frameSize == 240) || (enc_frameSize == 320) || + (enc_frameSize == 400) || (enc_frameSize == 480)) { + ok = WebRtcG729_CreateEnc(&G729enc_inst[k]); + if (ok != 0) { + printf( + "Error: Couldn't allocate memory for G729 encoding " + "instance\n"); + exit(0); } - WebRtcG729_EncoderInit(G729enc_inst[k], vad); - if ((vad==1)&&(enc_frameSize!=80)) { - printf("\nError - This simulation only supports VAD for G729 at 10ms packets (not %dms)\n", (enc_frameSize>>3)); - } - } else { - printf("\nError - g729 is only developed for 8kHz \n"); + } else { + printf( + "\nError: g729 only supports 10, 20, 30, 40, 50 or 60 " + "ms!!\n\n"); exit(0); + } + WebRtcG729_EncoderInit(G729enc_inst[k], vad); + if ((vad == 1) && (enc_frameSize != 80)) { + printf( + "\nError - This simulation only supports VAD for G729 at 10ms " + "packets (not %dms)\n", + (enc_frameSize >> 3)); + } + } else { + printf("\nError - g729 is only developed for 8kHz \n"); + exit(0); } break; #endif #ifdef CODEC_G729_1 - case webrtc::kDecoderG729_1: - if (sampfreq==16000) { - if ((enc_frameSize==320)||(enc_frameSize==640)||(enc_frameSize==960) - ) { - ok=WebRtcG7291_Create(&G729_1_inst[k]); - if (ok!=0) { - printf("Error: Couldn't allocate memory for G.729.1 codec instance\n"); - exit(0); - } - } else { - printf("\nError: G.729.1 only supports 20, 40 or 60 ms!!\n\n"); - exit(0); - } - if (!(((bitrate >= 12000) && (bitrate <= 32000) && (bitrate%2000 == 0)) || (bitrate == 8000))) { - /* must be 8, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, or 32 kbps */ - printf("\nError: G.729.1 bitrate must be 8000 or 12000--32000 in steps of 2000 bps\n"); - exit(0); - } - WebRtcG7291_EncoderInit(G729_1_inst[k], bitrate, 0 /* flag8kHz*/, 0 /*flagG729mode*/); - } else { - printf("\nError - G.729.1 input is always 16 kHz \n"); + case webrtc::kDecoderG729_1: + if (sampfreq == 16000) { + if ((enc_frameSize == 320) || (enc_frameSize == 640) || + (enc_frameSize == 960)) { + ok = WebRtcG7291_Create(&G729_1_inst[k]); + if (ok != 0) { + printf( + "Error: Couldn't allocate memory for G.729.1 codec " + "instance\n"); + exit(0); + } + } else { + printf("\nError: G.729.1 only supports 20, 40 or 60 ms!!\n\n"); exit(0); + } + if (!(((bitrate >= 12000) && (bitrate <= 32000) && + (bitrate % 2000 == 0)) || + (bitrate == 8000))) { + /* must be 8, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, or 32 kbps */ + printf( + "\nError: G.729.1 bitrate must be 8000 or 12000--32000 in " + "steps of 2000 bps\n"); + exit(0); + } + WebRtcG7291_EncoderInit(G729_1_inst[k], bitrate, 0 /* flag8kHz*/, + 0 /*flagG729mode*/); + } else { + printf("\nError - G.729.1 input is always 16 kHz \n"); + exit(0); } break; #endif #ifdef CODEC_SPEEX_8 - case webrtc::kDecoderSPEEX_8 : - if (sampfreq==8000) { - if ((enc_frameSize==160)||(enc_frameSize==320)||(enc_frameSize==480)) { - ok=WebRtcSpeex_CreateEnc(&SPEEX8enc_inst[k], sampfreq); - if (ok!=0) { - printf("Error: Couldn't allocate memory for Speex encoding instance\n"); - exit(0); - } - } else { - printf("\nError: Speex only supports 20, 40, and 60 ms!!\n\n"); - exit(0); + case webrtc::kDecoderSPEEX_8: + if (sampfreq == 8000) { + if ((enc_frameSize == 160) || (enc_frameSize == 320) || + (enc_frameSize == 480)) { + ok = WebRtcSpeex_CreateEnc(&SPEEX8enc_inst[k], sampfreq); + if (ok != 0) { + printf( + "Error: Couldn't allocate memory for Speex encoding " + "instance\n"); + exit(0); } - if ((vad==1)&&(enc_frameSize!=160)) { - printf("\nError - This simulation only supports VAD for Speex at 20ms packets (not %dms)\n", (enc_frameSize>>3)); - vad=0; - } - ok=WebRtcSpeex_EncoderInit(SPEEX8enc_inst[k], 0/*vbr*/, 3 /*complexity*/, vad); - if (ok!=0) exit(0); + } else { + printf("\nError: Speex only supports 20, 40, and 60 ms!!\n\n"); + exit(0); + } + if ((vad == 1) && (enc_frameSize != 160)) { + printf( + "\nError - This simulation only supports VAD for Speex at 20ms " + "packets (not %dms)\n", + (enc_frameSize >> 3)); + vad = 0; + } + ok = WebRtcSpeex_EncoderInit(SPEEX8enc_inst[k], 0 /*vbr*/, + 3 /*complexity*/, vad); + if (ok != 0) + exit(0); } else { - printf("\nError - Speex8 called with sample frequency other than 8 kHz.\n\n"); + printf( + "\nError - Speex8 called with sample frequency other than 8 " + "kHz.\n\n"); } break; #endif #ifdef CODEC_SPEEX_16 - case webrtc::kDecoderSPEEX_16 : - if (sampfreq==16000) { - if ((enc_frameSize==320)||(enc_frameSize==640)||(enc_frameSize==960)) { - ok=WebRtcSpeex_CreateEnc(&SPEEX16enc_inst[k], sampfreq); - if (ok!=0) { - printf("Error: Couldn't allocate memory for Speex encoding instance\n"); - exit(0); - } - } else { - printf("\nError: Speex only supports 20, 40, and 60 ms!!\n\n"); - exit(0); + case webrtc::kDecoderSPEEX_16: + if (sampfreq == 16000) { + if ((enc_frameSize == 320) || (enc_frameSize == 640) || + (enc_frameSize == 960)) { + ok = WebRtcSpeex_CreateEnc(&SPEEX16enc_inst[k], sampfreq); + if (ok != 0) { + printf( + "Error: Couldn't allocate memory for Speex encoding " + "instance\n"); + exit(0); } - if ((vad==1)&&(enc_frameSize!=320)) { - printf("\nError - This simulation only supports VAD for Speex at 20ms packets (not %dms)\n", (enc_frameSize>>4)); - vad=0; - } - ok=WebRtcSpeex_EncoderInit(SPEEX16enc_inst[k], 0/*vbr*/, 3 /*complexity*/, vad); - if (ok!=0) exit(0); + } else { + printf("\nError: Speex only supports 20, 40, and 60 ms!!\n\n"); + exit(0); + } + if ((vad == 1) && (enc_frameSize != 320)) { + printf( + "\nError - This simulation only supports VAD for Speex at 20ms " + "packets (not %dms)\n", + (enc_frameSize >> 4)); + vad = 0; + } + ok = WebRtcSpeex_EncoderInit(SPEEX16enc_inst[k], 0 /*vbr*/, + 3 /*complexity*/, vad); + if (ok != 0) + exit(0); } else { - printf("\nError - Speex16 called with sample frequency other than 16 kHz.\n\n"); + printf( + "\nError - Speex16 called with sample frequency other than 16 " + "kHz.\n\n"); } break; #endif #ifdef CODEC_G722_1_16 - case webrtc::kDecoderG722_1_16 : - if (sampfreq==16000) { - ok=WebRtcG7221_CreateEnc16(&G722_1_16enc_inst[k]); - if (ok!=0) { - printf("Error: Couldn't allocate memory for G.722.1 instance\n"); - exit(0); - } - if (enc_frameSize==320) { - } else { - printf("\nError: G722.1 only supports 20 ms!!\n\n"); - exit(0); - } - WebRtcG7221_EncoderInit16((G722_1_16_encinst_t*)G722_1_16enc_inst[k]); - } else { - printf("\nError - G722.1 is only developed for 16kHz \n"); + case webrtc::kDecoderG722_1_16: + if (sampfreq == 16000) { + ok = WebRtcG7221_CreateEnc16(&G722_1_16enc_inst[k]); + if (ok != 0) { + printf("Error: Couldn't allocate memory for G.722.1 instance\n"); exit(0); + } + if (enc_frameSize == 320) { + } else { + printf("\nError: G722.1 only supports 20 ms!!\n\n"); + exit(0); + } + WebRtcG7221_EncoderInit16((G722_1_16_encinst_t*)G722_1_16enc_inst[k]); + } else { + printf("\nError - G722.1 is only developed for 16kHz \n"); + exit(0); } break; #endif #ifdef CODEC_G722_1_24 - case webrtc::kDecoderG722_1_24 : - if (sampfreq==16000) { - ok=WebRtcG7221_CreateEnc24(&G722_1_24enc_inst[k]); - if (ok!=0) { - printf("Error: Couldn't allocate memory for G.722.1 instance\n"); - exit(0); - } - if (enc_frameSize==320) { - } else { - printf("\nError: G722.1 only supports 20 ms!!\n\n"); - exit(0); - } - WebRtcG7221_EncoderInit24((G722_1_24_encinst_t*)G722_1_24enc_inst[k]); - } else { - printf("\nError - G722.1 is only developed for 16kHz \n"); + case webrtc::kDecoderG722_1_24: + if (sampfreq == 16000) { + ok = WebRtcG7221_CreateEnc24(&G722_1_24enc_inst[k]); + if (ok != 0) { + printf("Error: Couldn't allocate memory for G.722.1 instance\n"); exit(0); + } + if (enc_frameSize == 320) { + } else { + printf("\nError: G722.1 only supports 20 ms!!\n\n"); + exit(0); + } + WebRtcG7221_EncoderInit24((G722_1_24_encinst_t*)G722_1_24enc_inst[k]); + } else { + printf("\nError - G722.1 is only developed for 16kHz \n"); + exit(0); } break; #endif #ifdef CODEC_G722_1_32 - case webrtc::kDecoderG722_1_32 : - if (sampfreq==16000) { - ok=WebRtcG7221_CreateEnc32(&G722_1_32enc_inst[k]); - if (ok!=0) { - printf("Error: Couldn't allocate memory for G.722.1 instance\n"); - exit(0); - } - if (enc_frameSize==320) { - } else { - printf("\nError: G722.1 only supports 20 ms!!\n\n"); - exit(0); - } - WebRtcG7221_EncoderInit32((G722_1_32_encinst_t*)G722_1_32enc_inst[k]); - } else { - printf("\nError - G722.1 is only developed for 16kHz \n"); + case webrtc::kDecoderG722_1_32: + if (sampfreq == 16000) { + ok = WebRtcG7221_CreateEnc32(&G722_1_32enc_inst[k]); + if (ok != 0) { + printf("Error: Couldn't allocate memory for G.722.1 instance\n"); exit(0); + } + if (enc_frameSize == 320) { + } else { + printf("\nError: G722.1 only supports 20 ms!!\n\n"); + exit(0); + } + WebRtcG7221_EncoderInit32((G722_1_32_encinst_t*)G722_1_32enc_inst[k]); + } else { + printf("\nError - G722.1 is only developed for 16kHz \n"); + exit(0); } break; #endif #ifdef CODEC_G722_1C_24 - case webrtc::kDecoderG722_1C_24 : - if (sampfreq==32000) { - ok=WebRtcG7221C_CreateEnc24(&G722_1C_24enc_inst[k]); - if (ok!=0) { - printf("Error: Couldn't allocate memory for G.722.1C instance\n"); - exit(0); - } - if (enc_frameSize==640) { - } else { - printf("\nError: G722.1 C only supports 20 ms!!\n\n"); - exit(0); - } - WebRtcG7221C_EncoderInit24((G722_1C_24_encinst_t*)G722_1C_24enc_inst[k]); - } else { - printf("\nError - G722.1 C is only developed for 32kHz \n"); + case webrtc::kDecoderG722_1C_24: + if (sampfreq == 32000) { + ok = WebRtcG7221C_CreateEnc24(&G722_1C_24enc_inst[k]); + if (ok != 0) { + printf("Error: Couldn't allocate memory for G.722.1C instance\n"); exit(0); + } + if (enc_frameSize == 640) { + } else { + printf("\nError: G722.1 C only supports 20 ms!!\n\n"); + exit(0); + } + WebRtcG7221C_EncoderInit24( + (G722_1C_24_encinst_t*)G722_1C_24enc_inst[k]); + } else { + printf("\nError - G722.1 C is only developed for 32kHz \n"); + exit(0); } break; #endif #ifdef CODEC_G722_1C_32 - case webrtc::kDecoderG722_1C_32 : - if (sampfreq==32000) { - ok=WebRtcG7221C_CreateEnc32(&G722_1C_32enc_inst[k]); - if (ok!=0) { - printf("Error: Couldn't allocate memory for G.722.1C instance\n"); - exit(0); - } - if (enc_frameSize==640) { - } else { - printf("\nError: G722.1 C only supports 20 ms!!\n\n"); - exit(0); - } - WebRtcG7221C_EncoderInit32((G722_1C_32_encinst_t*)G722_1C_32enc_inst[k]); - } else { - printf("\nError - G722.1 C is only developed for 32kHz \n"); + case webrtc::kDecoderG722_1C_32: + if (sampfreq == 32000) { + ok = WebRtcG7221C_CreateEnc32(&G722_1C_32enc_inst[k]); + if (ok != 0) { + printf("Error: Couldn't allocate memory for G.722.1C instance\n"); exit(0); + } + if (enc_frameSize == 640) { + } else { + printf("\nError: G722.1 C only supports 20 ms!!\n\n"); + exit(0); + } + WebRtcG7221C_EncoderInit32( + (G722_1C_32_encinst_t*)G722_1C_32enc_inst[k]); + } else { + printf("\nError - G722.1 C is only developed for 32kHz \n"); + exit(0); } break; #endif #ifdef CODEC_G722_1C_48 - case webrtc::kDecoderG722_1C_48 : - if (sampfreq==32000) { - ok=WebRtcG7221C_CreateEnc48(&G722_1C_48enc_inst[k]); - if (ok!=0) { - printf("Error: Couldn't allocate memory for G.722.1C instance\n"); - exit(0); - } - if (enc_frameSize==640) { - } else { - printf("\nError: G722.1 C only supports 20 ms!!\n\n"); - exit(0); - } - WebRtcG7221C_EncoderInit48((G722_1C_48_encinst_t*)G722_1C_48enc_inst[k]); - } else { - printf("\nError - G722.1 C is only developed for 32kHz \n"); + case webrtc::kDecoderG722_1C_48: + if (sampfreq == 32000) { + ok = WebRtcG7221C_CreateEnc48(&G722_1C_48enc_inst[k]); + if (ok != 0) { + printf("Error: Couldn't allocate memory for G.722.1C instance\n"); exit(0); + } + if (enc_frameSize == 640) { + } else { + printf("\nError: G722.1 C only supports 20 ms!!\n\n"); + exit(0); + } + WebRtcG7221C_EncoderInit48( + (G722_1C_48_encinst_t*)G722_1C_48enc_inst[k]); + } else { + printf("\nError - G722.1 C is only developed for 32kHz \n"); + exit(0); } break; #endif #ifdef CODEC_G722 - case webrtc::kDecoderG722 : - if (sampfreq==16000) { - if (enc_frameSize%2==0) { - } else { - printf("\nError - g722 frames must have an even number of enc_frameSize\n"); - exit(0); - } - WebRtcG722_CreateEncoder(&g722EncState[k]); - WebRtcG722_EncoderInit(g722EncState[k]); - } else { - printf("\nError - g722 is only developed for 16kHz \n"); + case webrtc::kDecoderG722: + if (sampfreq == 16000) { + if (enc_frameSize % 2 == 0) { + } else { + printf( + "\nError - g722 frames must have an even number of " + "enc_frameSize\n"); exit(0); + } + WebRtcG722_CreateEncoder(&g722EncState[k]); + WebRtcG722_EncoderInit(g722EncState[k]); + } else { + printf("\nError - g722 is only developed for 16kHz \n"); + exit(0); } break; #endif #ifdef CODEC_AMR - case webrtc::kDecoderAMR : - if (sampfreq==8000) { - ok=WebRtcAmr_CreateEnc(&AMRenc_inst[k]); - if (ok!=0) { - printf("Error: Couldn't allocate memory for AMR encoding instance\n"); - exit(0); - }if ((enc_frameSize==160)||(enc_frameSize==320)||(enc_frameSize==480)) { - } else { - printf("\nError - AMR must have a multiple of 160 enc_frameSize\n"); - exit(0); - } - WebRtcAmr_EncoderInit(AMRenc_inst[k], vad); - WebRtcAmr_EncodeBitmode(AMRenc_inst[k], AMRBandwidthEfficient); - AMR_bitrate = bitrate; - } else { - printf("\nError - AMR is only developed for 8kHz \n"); + case webrtc::kDecoderAMR: + if (sampfreq == 8000) { + ok = WebRtcAmr_CreateEnc(&AMRenc_inst[k]); + if (ok != 0) { + printf( + "Error: Couldn't allocate memory for AMR encoding instance\n"); exit(0); + } + if ((enc_frameSize == 160) || (enc_frameSize == 320) || + (enc_frameSize == 480)) { + } else { + printf("\nError - AMR must have a multiple of 160 enc_frameSize\n"); + exit(0); + } + WebRtcAmr_EncoderInit(AMRenc_inst[k], vad); + WebRtcAmr_EncodeBitmode(AMRenc_inst[k], AMRBandwidthEfficient); + AMR_bitrate = bitrate; + } else { + printf("\nError - AMR is only developed for 8kHz \n"); + exit(0); } break; #endif #ifdef CODEC_AMRWB - case webrtc::kDecoderAMRWB : - if (sampfreq==16000) { - ok=WebRtcAmrWb_CreateEnc(&AMRWBenc_inst[k]); - if (ok!=0) { - printf("Error: Couldn't allocate memory for AMRWB encoding instance\n"); - exit(0); - } - if (((enc_frameSize/320)<0)||((enc_frameSize/320)>3)||((enc_frameSize%320)!=0)) { - printf("\nError - AMRwb must have frameSize of 20, 40 or 60ms\n"); - exit(0); - } - WebRtcAmrWb_EncoderInit(AMRWBenc_inst[k], vad); - if (bitrate==7000) { - AMRWB_bitrate = AMRWB_MODE_7k; - } else if (bitrate==9000) { - AMRWB_bitrate = AMRWB_MODE_9k; - } else if (bitrate==12000) { - AMRWB_bitrate = AMRWB_MODE_12k; - } else if (bitrate==14000) { - AMRWB_bitrate = AMRWB_MODE_14k; - } else if (bitrate==16000) { - AMRWB_bitrate = AMRWB_MODE_16k; - } else if (bitrate==18000) { - AMRWB_bitrate = AMRWB_MODE_18k; - } else if (bitrate==20000) { - AMRWB_bitrate = AMRWB_MODE_20k; - } else if (bitrate==23000) { - AMRWB_bitrate = AMRWB_MODE_23k; - } else if (bitrate==24000) { - AMRWB_bitrate = AMRWB_MODE_24k; - } - WebRtcAmrWb_EncodeBitmode(AMRWBenc_inst[k], AMRBandwidthEfficient); + case webrtc::kDecoderAMRWB: + if (sampfreq == 16000) { + ok = WebRtcAmrWb_CreateEnc(&AMRWBenc_inst[k]); + if (ok != 0) { + printf( + "Error: Couldn't allocate memory for AMRWB encoding " + "instance\n"); + exit(0); + } + if (((enc_frameSize / 320) < 0) || ((enc_frameSize / 320) > 3) || + ((enc_frameSize % 320) != 0)) { + printf("\nError - AMRwb must have frameSize of 20, 40 or 60ms\n"); + exit(0); + } + WebRtcAmrWb_EncoderInit(AMRWBenc_inst[k], vad); + if (bitrate == 7000) { + AMRWB_bitrate = AMRWB_MODE_7k; + } else if (bitrate == 9000) { + AMRWB_bitrate = AMRWB_MODE_9k; + } else if (bitrate == 12000) { + AMRWB_bitrate = AMRWB_MODE_12k; + } else if (bitrate == 14000) { + AMRWB_bitrate = AMRWB_MODE_14k; + } else if (bitrate == 16000) { + AMRWB_bitrate = AMRWB_MODE_16k; + } else if (bitrate == 18000) { + AMRWB_bitrate = AMRWB_MODE_18k; + } else if (bitrate == 20000) { + AMRWB_bitrate = AMRWB_MODE_20k; + } else if (bitrate == 23000) { + AMRWB_bitrate = AMRWB_MODE_23k; + } else if (bitrate == 24000) { + AMRWB_bitrate = AMRWB_MODE_24k; + } + WebRtcAmrWb_EncodeBitmode(AMRWBenc_inst[k], AMRBandwidthEfficient); } else { - printf("\nError - AMRwb is only developed for 16kHz \n"); - exit(0); + printf("\nError - AMRwb is only developed for 16kHz \n"); + exit(0); } break; #endif #ifdef CODEC_ILBC - case webrtc::kDecoderILBC : - if (sampfreq==8000) { - ok=WebRtcIlbcfix_EncoderCreate(&iLBCenc_inst[k]); - if (ok!=0) { - printf("Error: Couldn't allocate memory for iLBC encoding instance\n"); - exit(0); - } - if ((enc_frameSize==160)||(enc_frameSize==240)||(enc_frameSize==320)||(enc_frameSize==480)) { - } else { - printf("\nError - iLBC only supports 160, 240, 320 and 480 enc_frameSize (20, 30, 40 and 60 ms)\n"); - exit(0); - } - if ((enc_frameSize==160)||(enc_frameSize==320)) { - /* 20 ms version */ - WebRtcIlbcfix_EncoderInit(iLBCenc_inst[k], 20); - } else { - /* 30 ms version */ - WebRtcIlbcfix_EncoderInit(iLBCenc_inst[k], 30); - } - } else { - printf("\nError - iLBC is only developed for 8kHz \n"); + case webrtc::kDecoderILBC: + if (sampfreq == 8000) { + ok = WebRtcIlbcfix_EncoderCreate(&iLBCenc_inst[k]); + if (ok != 0) { + printf( + "Error: Couldn't allocate memory for iLBC encoding instance\n"); exit(0); + } + if ((enc_frameSize == 160) || (enc_frameSize == 240) || + (enc_frameSize == 320) || (enc_frameSize == 480)) { + } else { + printf( + "\nError - iLBC only supports 160, 240, 320 and 480 " + "enc_frameSize (20, 30, 40 and 60 ms)\n"); + exit(0); + } + if ((enc_frameSize == 160) || (enc_frameSize == 320)) { + /* 20 ms version */ + WebRtcIlbcfix_EncoderInit(iLBCenc_inst[k], 20); + } else { + /* 30 ms version */ + WebRtcIlbcfix_EncoderInit(iLBCenc_inst[k], 30); + } + } else { + printf("\nError - iLBC is only developed for 8kHz \n"); + exit(0); } break; #endif #ifdef CODEC_ISAC - case webrtc::kDecoderISAC: - if (sampfreq==16000) { - ok=WebRtcIsac_Create(&ISAC_inst[k]); - if (ok!=0) { - printf("Error: Couldn't allocate memory for iSAC instance\n"); - exit(0); - }if ((enc_frameSize==480)||(enc_frameSize==960)) { - } else { - printf("\nError - iSAC only supports frameSize (30 and 60 ms)\n"); - exit(0); - } - WebRtcIsac_EncoderInit(ISAC_inst[k],1); - if ((bitrate<10000)||(bitrate>32000)) { - printf("\nError - iSAC bitrate has to be between 10000 and 32000 bps (not %i)\n", bitrate); - exit(0); - } - WebRtcIsac_Control(ISAC_inst[k], bitrate, enc_frameSize>>4); - } else { - printf("\nError - iSAC only supports 480 or 960 enc_frameSize (30 or 60 ms)\n"); + case webrtc::kDecoderISAC: + if (sampfreq == 16000) { + ok = WebRtcIsac_Create(&ISAC_inst[k]); + if (ok != 0) { + printf("Error: Couldn't allocate memory for iSAC instance\n"); exit(0); + } + if ((enc_frameSize == 480) || (enc_frameSize == 960)) { + } else { + printf("\nError - iSAC only supports frameSize (30 and 60 ms)\n"); + exit(0); + } + WebRtcIsac_EncoderInit(ISAC_inst[k], 1); + if ((bitrate < 10000) || (bitrate > 32000)) { + printf( + "\nError - iSAC bitrate has to be between 10000 and 32000 bps " + "(not %i)\n", + bitrate); + exit(0); + } + WebRtcIsac_Control(ISAC_inst[k], bitrate, enc_frameSize >> 4); + } else { + printf( + "\nError - iSAC only supports 480 or 960 enc_frameSize (30 or 60 " + "ms)\n"); + exit(0); } break; #endif #ifdef NETEQ_ISACFIX_CODEC - case webrtc::kDecoderISAC: - if (sampfreq==16000) { - ok=WebRtcIsacfix_Create(&ISAC_inst[k]); - if (ok!=0) { - printf("Error: Couldn't allocate memory for iSAC instance\n"); - exit(0); - }if ((enc_frameSize==480)||(enc_frameSize==960)) { - } else { - printf("\nError - iSAC only supports frameSize (30 and 60 ms)\n"); - exit(0); - } - WebRtcIsacfix_EncoderInit(ISAC_inst[k],1); - if ((bitrate<10000)||(bitrate>32000)) { - printf("\nError - iSAC bitrate has to be between 10000 and 32000 bps (not %i)\n", bitrate); - exit(0); - } - WebRtcIsacfix_Control(ISAC_inst[k], bitrate, enc_frameSize>>4); - } else { - printf("\nError - iSAC only supports 480 or 960 enc_frameSize (30 or 60 ms)\n"); + case webrtc::kDecoderISAC: + if (sampfreq == 16000) { + ok = WebRtcIsacfix_Create(&ISAC_inst[k]); + if (ok != 0) { + printf("Error: Couldn't allocate memory for iSAC instance\n"); exit(0); + } + if ((enc_frameSize == 480) || (enc_frameSize == 960)) { + } else { + printf("\nError - iSAC only supports frameSize (30 and 60 ms)\n"); + exit(0); + } + WebRtcIsacfix_EncoderInit(ISAC_inst[k], 1); + if ((bitrate < 10000) || (bitrate > 32000)) { + printf( + "\nError - iSAC bitrate has to be between 10000 and 32000 bps " + "(not %i)\n", + bitrate); + exit(0); + } + WebRtcIsacfix_Control(ISAC_inst[k], bitrate, enc_frameSize >> 4); + } else { + printf( + "\nError - iSAC only supports 480 or 960 enc_frameSize (30 or 60 " + "ms)\n"); + exit(0); } break; #endif #ifdef CODEC_ISAC_SWB - case webrtc::kDecoderISACswb: - if (sampfreq==32000) { - ok=WebRtcIsac_Create(&ISACSWB_inst[k]); - if (ok!=0) { - printf("Error: Couldn't allocate memory for iSAC SWB instance\n"); - exit(0); - }if (enc_frameSize==960) { - } else { - printf("\nError - iSAC SWB only supports frameSize 30 ms\n"); - exit(0); - } - ok = WebRtcIsac_SetEncSampRate(ISACSWB_inst[k], 32000); - if (ok!=0) { - printf("Error: Couldn't set sample rate for iSAC SWB instance\n"); - exit(0); - } - WebRtcIsac_EncoderInit(ISACSWB_inst[k],1); - if ((bitrate<32000)||(bitrate>56000)) { - printf("\nError - iSAC SWB bitrate has to be between 32000 and 56000 bps (not %i)\n", bitrate); - exit(0); - } - WebRtcIsac_Control(ISACSWB_inst[k], bitrate, enc_frameSize>>5); - } else { - printf("\nError - iSAC SWB only supports 960 enc_frameSize (30 ms)\n"); + case webrtc::kDecoderISACswb: + if (sampfreq == 32000) { + ok = WebRtcIsac_Create(&ISACSWB_inst[k]); + if (ok != 0) { + printf("Error: Couldn't allocate memory for iSAC SWB instance\n"); exit(0); + } + if (enc_frameSize == 960) { + } else { + printf("\nError - iSAC SWB only supports frameSize 30 ms\n"); + exit(0); + } + ok = WebRtcIsac_SetEncSampRate(ISACSWB_inst[k], 32000); + if (ok != 0) { + printf("Error: Couldn't set sample rate for iSAC SWB instance\n"); + exit(0); + } + WebRtcIsac_EncoderInit(ISACSWB_inst[k], 1); + if ((bitrate < 32000) || (bitrate > 56000)) { + printf( + "\nError - iSAC SWB bitrate has to be between 32000 and 56000 " + "bps (not %i)\n", + bitrate); + exit(0); + } + WebRtcIsac_Control(ISACSWB_inst[k], bitrate, enc_frameSize >> 5); + } else { + printf( + "\nError - iSAC SWB only supports 960 enc_frameSize (30 ms)\n"); + exit(0); } break; #endif #ifdef CODEC_GSMFR - case webrtc::kDecoderGSMFR: - if (sampfreq==8000) { - ok=WebRtcGSMFR_CreateEnc(&GSMFRenc_inst[k]); - if (ok!=0) { - printf("Error: Couldn't allocate memory for GSM FR encoding instance\n"); - exit(0); - } - if ((enc_frameSize==160)||(enc_frameSize==320)||(enc_frameSize==480)) { - } else { - printf("\nError - GSM FR must have a multiple of 160 enc_frameSize\n"); - exit(0); - } - WebRtcGSMFR_EncoderInit(GSMFRenc_inst[k], 0); - } else { - printf("\nError - GSM FR is only developed for 8kHz \n"); + case webrtc::kDecoderGSMFR: + if (sampfreq == 8000) { + ok = WebRtcGSMFR_CreateEnc(&GSMFRenc_inst[k]); + if (ok != 0) { + printf( + "Error: Couldn't allocate memory for GSM FR encoding " + "instance\n"); exit(0); + } + if ((enc_frameSize == 160) || (enc_frameSize == 320) || + (enc_frameSize == 480)) { + } else { + printf( + "\nError - GSM FR must have a multiple of 160 enc_frameSize\n"); + exit(0); + } + WebRtcGSMFR_EncoderInit(GSMFRenc_inst[k], 0); + } else { + printf("\nError - GSM FR is only developed for 8kHz \n"); + exit(0); } break; #endif - default : + default: printf("Error: unknown codec in call to NetEQTest_init_coders.\n"); exit(0); break; - } - - if (ok != 0) { - return(ok); - } - } // end for - - return(0); -} - + } + if (ok != 0) { + return (ok); + } + } // end for + return (0); +} int NetEQTest_free_coders(webrtc::NetEqDecoder coder, int numChannels) { - - for (int k = 0; k < numChannels; k++) - { - WebRtcVad_Free(VAD_inst[k]); + for (int k = 0; k < numChannels; k++) { + WebRtcVad_Free(VAD_inst[k]); #if (defined(CODEC_CNGCODEC8) || defined(CODEC_CNGCODEC16) || \ - defined(CODEC_CNGCODEC32) || defined(CODEC_CNGCODEC48)) - WebRtcCng_FreeEnc(CNGenc_inst[k]); + defined(CODEC_CNGCODEC32) || defined(CODEC_CNGCODEC48)) + WebRtcCng_FreeEnc(CNGenc_inst[k]); #endif - switch (coder) - { + switch (coder) { #ifdef CODEC_PCM16B - case webrtc::kDecoderPCM16B : + case webrtc::kDecoderPCM16B: #endif #ifdef CODEC_PCM16B_WB - case webrtc::kDecoderPCM16Bwb : + case webrtc::kDecoderPCM16Bwb: #endif #ifdef CODEC_PCM16B_32KHZ - case webrtc::kDecoderPCM16Bswb32kHz : + case webrtc::kDecoderPCM16Bswb32kHz: #endif #ifdef CODEC_PCM16B_48KHZ - case webrtc::kDecoderPCM16Bswb48kHz : + case webrtc::kDecoderPCM16Bswb48kHz: #endif #ifdef CODEC_G711 - case webrtc::kDecoderPCMu : - case webrtc::kDecoderPCMa : + case webrtc::kDecoderPCMu: + case webrtc::kDecoderPCMa: #endif - // do nothing - break; + // do nothing + break; #ifdef CODEC_G729 - case webrtc::kDecoderG729: - WebRtcG729_FreeEnc(G729enc_inst[k]); - break; + case webrtc::kDecoderG729: + WebRtcG729_FreeEnc(G729enc_inst[k]); + break; #endif #ifdef CODEC_G729_1 - case webrtc::kDecoderG729_1: - WebRtcG7291_Free(G729_1_inst[k]); - break; + case webrtc::kDecoderG729_1: + WebRtcG7291_Free(G729_1_inst[k]); + break; #endif #ifdef CODEC_SPEEX_8 - case webrtc::kDecoderSPEEX_8 : - WebRtcSpeex_FreeEnc(SPEEX8enc_inst[k]); - break; + case webrtc::kDecoderSPEEX_8: + WebRtcSpeex_FreeEnc(SPEEX8enc_inst[k]); + break; #endif #ifdef CODEC_SPEEX_16 - case webrtc::kDecoderSPEEX_16 : - WebRtcSpeex_FreeEnc(SPEEX16enc_inst[k]); - break; + case webrtc::kDecoderSPEEX_16: + WebRtcSpeex_FreeEnc(SPEEX16enc_inst[k]); + break; #endif #ifdef CODEC_G722_1_16 - case webrtc::kDecoderG722_1_16 : - WebRtcG7221_FreeEnc16(G722_1_16enc_inst[k]); - break; + case webrtc::kDecoderG722_1_16: + WebRtcG7221_FreeEnc16(G722_1_16enc_inst[k]); + break; #endif #ifdef CODEC_G722_1_24 - case webrtc::kDecoderG722_1_24 : - WebRtcG7221_FreeEnc24(G722_1_24enc_inst[k]); - break; + case webrtc::kDecoderG722_1_24: + WebRtcG7221_FreeEnc24(G722_1_24enc_inst[k]); + break; #endif #ifdef CODEC_G722_1_32 - case webrtc::kDecoderG722_1_32 : - WebRtcG7221_FreeEnc32(G722_1_32enc_inst[k]); - break; + case webrtc::kDecoderG722_1_32: + WebRtcG7221_FreeEnc32(G722_1_32enc_inst[k]); + break; #endif #ifdef CODEC_G722_1C_24 - case webrtc::kDecoderG722_1C_24 : - WebRtcG7221C_FreeEnc24(G722_1C_24enc_inst[k]); - break; + case webrtc::kDecoderG722_1C_24: + WebRtcG7221C_FreeEnc24(G722_1C_24enc_inst[k]); + break; #endif #ifdef CODEC_G722_1C_32 - case webrtc::kDecoderG722_1C_32 : - WebRtcG7221C_FreeEnc32(G722_1C_32enc_inst[k]); - break; + case webrtc::kDecoderG722_1C_32: + WebRtcG7221C_FreeEnc32(G722_1C_32enc_inst[k]); + break; #endif #ifdef CODEC_G722_1C_48 - case webrtc::kDecoderG722_1C_48 : - WebRtcG7221C_FreeEnc48(G722_1C_48enc_inst[k]); - break; + case webrtc::kDecoderG722_1C_48: + WebRtcG7221C_FreeEnc48(G722_1C_48enc_inst[k]); + break; #endif #ifdef CODEC_G722 - case webrtc::kDecoderG722 : - WebRtcG722_FreeEncoder(g722EncState[k]); - break; + case webrtc::kDecoderG722: + WebRtcG722_FreeEncoder(g722EncState[k]); + break; #endif #ifdef CODEC_AMR - case webrtc::kDecoderAMR : - WebRtcAmr_FreeEnc(AMRenc_inst[k]); - break; + case webrtc::kDecoderAMR: + WebRtcAmr_FreeEnc(AMRenc_inst[k]); + break; #endif #ifdef CODEC_AMRWB - case webrtc::kDecoderAMRWB : - WebRtcAmrWb_FreeEnc(AMRWBenc_inst[k]); - break; + case webrtc::kDecoderAMRWB: + WebRtcAmrWb_FreeEnc(AMRWBenc_inst[k]); + break; #endif #ifdef CODEC_ILBC - case webrtc::kDecoderILBC : - WebRtcIlbcfix_EncoderFree(iLBCenc_inst[k]); - break; + case webrtc::kDecoderILBC: + WebRtcIlbcfix_EncoderFree(iLBCenc_inst[k]); + break; #endif #ifdef CODEC_ISAC - case webrtc::kDecoderISAC: - WebRtcIsac_Free(ISAC_inst[k]); - break; + case webrtc::kDecoderISAC: + WebRtcIsac_Free(ISAC_inst[k]); + break; #endif #ifdef NETEQ_ISACFIX_CODEC - case webrtc::kDecoderISAC: - WebRtcIsacfix_Free(ISAC_inst[k]); - break; + case webrtc::kDecoderISAC: + WebRtcIsacfix_Free(ISAC_inst[k]); + break; #endif #ifdef CODEC_ISAC_SWB - case webrtc::kDecoderISACswb: - WebRtcIsac_Free(ISACSWB_inst[k]); - break; + case webrtc::kDecoderISACswb: + WebRtcIsac_Free(ISACSWB_inst[k]); + break; #endif #ifdef CODEC_GSMFR - case webrtc::kDecoderGSMFR: - WebRtcGSMFR_FreeEnc(GSMFRenc_inst[k]); - break; + case webrtc::kDecoderGSMFR: + WebRtcGSMFR_FreeEnc(GSMFRenc_inst[k]); + break; #endif - default : - printf("Error: unknown codec in call to NetEQTest_init_coders.\n"); - exit(0); - break; + default: + printf("Error: unknown codec in call to NetEQTest_init_coders.\n"); + exit(0); + break; + } + } + + return (0); +} + +int NetEQTest_encode(int coder, + int16_t* indata, + int frameLen, + unsigned char* encoded, + int sampleRate, + int* vad, + int useVAD, + int bitrate, + int numChannels) { + short cdlen = 0; + int16_t* tempdata; + static int first_cng = 1; + int16_t tempLen; + + *vad = 1; + + // check VAD first + if (useVAD) { + *vad = 0; + + for (int k = 0; k < numChannels; k++) { + tempLen = frameLen; + tempdata = &indata[k * frameLen]; + int localVad = 0; + /* Partition the signal and test each chunk for VAD. + All chunks must be VAD=0 to produce a total VAD=0. */ + while (tempLen >= 10 * sampleRate / 1000) { + if ((tempLen % 30 * sampleRate / 1000) == + 0) { // tempLen is multiple of 30ms + localVad |= WebRtcVad_Process(VAD_inst[k], sampleRate, tempdata, + 30 * sampleRate / 1000); + tempdata += 30 * sampleRate / 1000; + tempLen -= 30 * sampleRate / 1000; + } else if (tempLen >= 20 * sampleRate / 1000) { // tempLen >= 20ms + localVad |= WebRtcVad_Process(VAD_inst[k], sampleRate, tempdata, + 20 * sampleRate / 1000); + tempdata += 20 * sampleRate / 1000; + tempLen -= 20 * sampleRate / 1000; + } else { // use 10ms + localVad |= WebRtcVad_Process(VAD_inst[k], sampleRate, tempdata, + 10 * sampleRate / 1000); + tempdata += 10 * sampleRate / 1000; + tempLen -= 10 * sampleRate / 1000; } + } + + // aggregate all VAD decisions over all channels + *vad |= localVad; } - return(0); -} + if (!*vad) { + // all channels are silent + cdlen = 0; + for (int k = 0; k < numChannels; k++) { + WebRtcCng_Encode(CNGenc_inst[k], &indata[k * frameLen], + (frameLen <= 640 ? frameLen : 640) /* max 640 */, + encoded, &tempLen, first_cng); + encoded += tempLen; + cdlen += tempLen; + } + *vad = 0; + first_cng = 0; + return (cdlen); + } + } + // loop over all channels + int totalLen = 0; - - - - -int NetEQTest_encode(int coder, int16_t *indata, int frameLen, unsigned char * encoded,int sampleRate , - int * vad, int useVAD, int bitrate, int numChannels){ - - short cdlen = 0; - int16_t *tempdata; - static int first_cng=1; - int16_t tempLen; - - *vad =1; - - // check VAD first - if(useVAD) - { - *vad = 0; - - for (int k = 0; k < numChannels; k++) - { - tempLen = frameLen; - tempdata = &indata[k*frameLen]; - int localVad=0; - /* Partition the signal and test each chunk for VAD. - All chunks must be VAD=0 to produce a total VAD=0. */ - while (tempLen >= 10*sampleRate/1000) { - if ((tempLen % 30*sampleRate/1000) == 0) { // tempLen is multiple of 30ms - localVad |= WebRtcVad_Process(VAD_inst[k] ,sampleRate, tempdata, 30*sampleRate/1000); - tempdata += 30*sampleRate/1000; - tempLen -= 30*sampleRate/1000; - } - else if (tempLen >= 20*sampleRate/1000) { // tempLen >= 20ms - localVad |= WebRtcVad_Process(VAD_inst[k] ,sampleRate, tempdata, 20*sampleRate/1000); - tempdata += 20*sampleRate/1000; - tempLen -= 20*sampleRate/1000; - } - else { // use 10ms - localVad |= WebRtcVad_Process(VAD_inst[k] ,sampleRate, tempdata, 10*sampleRate/1000); - tempdata += 10*sampleRate/1000; - tempLen -= 10*sampleRate/1000; - } - } - - // aggregate all VAD decisions over all channels - *vad |= localVad; - } - - if(!*vad){ - // all channels are silent - cdlen = 0; - for (int k = 0; k < numChannels; k++) - { - WebRtcCng_Encode(CNGenc_inst[k],&indata[k*frameLen], (frameLen <= 640 ? frameLen : 640) /* max 640 */, - encoded,&tempLen,first_cng); - encoded += tempLen; - cdlen += tempLen; - } - *vad=0; - first_cng=0; - return(cdlen); - } - } - - - // loop over all channels - int totalLen = 0; - - for (int k = 0; k < numChannels; k++) - { - /* Encode with the selected coder type */ - if (coder==webrtc::kDecoderPCMu) { /*g711 u-law */ + for (int k = 0; k < numChannels; k++) { + /* Encode with the selected coder type */ + if (coder == webrtc::kDecoderPCMu) { /*g711 u-law */ #ifdef CODEC_G711 - cdlen = WebRtcG711_EncodeU(indata, frameLen, encoded); + cdlen = WebRtcG711_EncodeU(indata, frameLen, encoded); #endif - } - else if (coder==webrtc::kDecoderPCMa) { /*g711 A-law */ + } else if (coder == webrtc::kDecoderPCMa) { /*g711 A-law */ #ifdef CODEC_G711 - cdlen = WebRtcG711_EncodeA(indata, frameLen, encoded); - } + cdlen = WebRtcG711_EncodeA(indata, frameLen, encoded); + } #endif #ifdef CODEC_PCM16B - else if ((coder==webrtc::kDecoderPCM16B)||(coder==webrtc::kDecoderPCM16Bwb)|| - (coder==webrtc::kDecoderPCM16Bswb32kHz)||(coder==webrtc::kDecoderPCM16Bswb48kHz)) { /*pcm16b (8kHz, 16kHz, 32kHz or 48kHz) */ - cdlen = WebRtcPcm16b_Encode(indata, frameLen, encoded); - } + else if ((coder == webrtc::kDecoderPCM16B) || + (coder == webrtc::kDecoderPCM16Bwb) || + (coder == webrtc::kDecoderPCM16Bswb32kHz) || + (coder == webrtc:: + kDecoderPCM16Bswb48kHz)) { /*pcm16b (8kHz, 16kHz, + 32kHz or 48kHz) */ + cdlen = WebRtcPcm16b_Encode(indata, frameLen, encoded); + } #endif #ifdef CODEC_G722 - else if (coder==webrtc::kDecoderG722) { /*g722 */ - cdlen=WebRtcG722_Encode(g722EncState[k], indata, frameLen, encoded); - assert(cdlen == frameLen>>1); - } + else if (coder == webrtc::kDecoderG722) { /*g722 */ + cdlen = WebRtcG722_Encode(g722EncState[k], indata, frameLen, encoded); + assert(cdlen == frameLen >> 1); + } #endif #ifdef CODEC_ILBC - else if (coder==webrtc::kDecoderILBC) { /*iLBC */ - cdlen = WebRtcIlbcfix_Encode(iLBCenc_inst[k], indata, - frameLen, encoded); - } + else if (coder == webrtc::kDecoderILBC) { /*iLBC */ + cdlen = WebRtcIlbcfix_Encode(iLBCenc_inst[k], indata, frameLen, encoded); + } #endif -#if (defined(CODEC_ISAC) || defined(NETEQ_ISACFIX_CODEC)) // TODO(hlundin): remove all NETEQ_ISACFIX_CODEC - else if (coder==webrtc::kDecoderISAC) { /*iSAC */ - int noOfCalls=0; - cdlen=0; - while (cdlen<=0) { +#if (defined(CODEC_ISAC) || \ + defined(NETEQ_ISACFIX_CODEC)) // TODO(hlundin): remove all + // NETEQ_ISACFIX_CODEC + else if (coder == webrtc::kDecoderISAC) { /*iSAC */ + int noOfCalls = 0; + cdlen = 0; + while (cdlen <= 0) { #ifdef CODEC_ISAC /* floating point */ - cdlen = WebRtcIsac_Encode(ISAC_inst[k], - &indata[noOfCalls * 160], - encoded); + cdlen = + WebRtcIsac_Encode(ISAC_inst[k], &indata[noOfCalls * 160], encoded); #else /* fixed point */ - cdlen = WebRtcIsacfix_Encode(ISAC_inst[k], - &indata[noOfCalls * 160], - encoded); + cdlen = WebRtcIsacfix_Encode(ISAC_inst[k], &indata[noOfCalls * 160], + encoded); #endif - noOfCalls++; - } - } + noOfCalls++; + } + } #endif #ifdef CODEC_ISAC_SWB - else if (coder==webrtc::kDecoderISACswb) { /* iSAC SWB */ - int noOfCalls=0; - cdlen=0; - while (cdlen<=0) { - cdlen = WebRtcIsac_Encode(ISACSWB_inst[k], - &indata[noOfCalls * 320], - encoded); - noOfCalls++; - } - } + else if (coder == webrtc::kDecoderISACswb) { /* iSAC SWB */ + int noOfCalls = 0; + cdlen = 0; + while (cdlen <= 0) { + cdlen = WebRtcIsac_Encode(ISACSWB_inst[k], &indata[noOfCalls * 320], + encoded); + noOfCalls++; + } + } #endif - indata += frameLen; - encoded += cdlen; - totalLen += cdlen; + indata += frameLen; + encoded += cdlen; + totalLen += cdlen; - } // end for + } // end for - first_cng=1; - return(totalLen); + first_cng = 1; + return (totalLen); } - - void makeRTPheader(unsigned char* rtp_data, int payloadType, int seqNo, uint32_t timestamp, uint32_t ssrc) { - rtp_data[0] = 0x80; - rtp_data[1] = payloadType & 0xFF; - rtp_data[2] = (seqNo >> 8) & 0xFF; - rtp_data[3] = seqNo & 0xFF; - rtp_data[4] = timestamp >> 24; - rtp_data[5] = (timestamp >> 16) & 0xFF; - rtp_data[6] = (timestamp >> 8) & 0xFF; - rtp_data[7] = timestamp & 0xFF; - rtp_data[8] = ssrc >> 24; - rtp_data[9] = (ssrc >> 16) & 0xFF; - rtp_data[10] = (ssrc >> 8) & 0xFF; - rtp_data[11] = ssrc & 0xFF; + rtp_data[0] = 0x80; + rtp_data[1] = payloadType & 0xFF; + rtp_data[2] = (seqNo >> 8) & 0xFF; + rtp_data[3] = seqNo & 0xFF; + rtp_data[4] = timestamp >> 24; + rtp_data[5] = (timestamp >> 16) & 0xFF; + rtp_data[6] = (timestamp >> 8) & 0xFF; + rtp_data[7] = timestamp & 0xFF; + rtp_data[8] = ssrc >> 24; + rtp_data[9] = (ssrc >> 16) & 0xFF; + rtp_data[10] = (ssrc >> 8) & 0xFF; + rtp_data[11] = ssrc & 0xFF; } - int makeRedundantHeader(unsigned char* rtp_data, int* payloadType, int numPayloads, uint32_t* timestamp, uint16_t* blockLen, int seqNo, - uint32_t ssrc) -{ - int i; - unsigned char* rtpPointer; - uint16_t offset; + uint32_t ssrc) { + int i; + unsigned char* rtpPointer; + uint16_t offset; - /* first create "standard" RTP header */ - makeRTPheader(rtp_data, NETEQ_CODEC_RED_PT, seqNo, timestamp[numPayloads-1], - ssrc); + /* first create "standard" RTP header */ + makeRTPheader(rtp_data, NETEQ_CODEC_RED_PT, seqNo, timestamp[numPayloads - 1], + ssrc); - rtpPointer = &rtp_data[12]; + rtpPointer = &rtp_data[12]; - /* add one sub-header for each redundant payload (not the primary) */ - for (i = 0; i < numPayloads - 1; i++) { - if (blockLen[i] > 0) { - offset = static_cast( - timestamp[numPayloads - 1] - timestamp[i]); + /* add one sub-header for each redundant payload (not the primary) */ + for (i = 0; i < numPayloads - 1; i++) { + if (blockLen[i] > 0) { + offset = static_cast(timestamp[numPayloads - 1] - timestamp[i]); - // Byte |0| |1 2 | 3 | - // Bit |0|1234567|01234567012345|6701234567| - // |F|payload| timestamp | block | - // | | type | offset | length | - rtpPointer[0] = (payloadType[i] & 0x7F) | 0x80; - rtpPointer[1] = (offset >> 6) & 0xFF; - rtpPointer[2] = - ((offset & 0x3F) << 2) | ((blockLen[i] >> 8) & 0x03); - rtpPointer[3] = blockLen[i] & 0xFF; + // Byte |0| |1 2 | 3 | + // Bit |0|1234567|01234567012345|6701234567| + // |F|payload| timestamp | block | + // | | type | offset | length | + rtpPointer[0] = (payloadType[i] & 0x7F) | 0x80; + rtpPointer[1] = (offset >> 6) & 0xFF; + rtpPointer[2] = ((offset & 0x3F) << 2) | ((blockLen[i] >> 8) & 0x03); + rtpPointer[3] = blockLen[i] & 0xFF; - rtpPointer += 4; - } + rtpPointer += 4; } + } - // Bit |0|1234567| - // |0|payload| - // | | type | - rtpPointer[0] = payloadType[numPayloads - 1] & 0x7F; - ++rtpPointer; + // Bit |0|1234567| + // |0|payload| + // | | type | + rtpPointer[0] = payloadType[numPayloads - 1] & 0x7F; + ++rtpPointer; - return rtpPointer - rtp_data; // length of header in bytes + return rtpPointer - rtp_data; // length of header in bytes } - - -int makeDTMFpayload(unsigned char* payload_data, int Event, int End, int Volume, int Duration) { - unsigned char E,R,V; - R=0; - V=(unsigned char)Volume; - if (End==0) { - E = 0x00; - } else { - E = 0x80; - } - payload_data[0]=(unsigned char)Event; - payload_data[1]=(unsigned char)(E|R|V); - //Duration equals 8 times time_ms, default is 8000 Hz. - payload_data[2]=(unsigned char)((Duration>>8)&0xFF); - payload_data[3]=(unsigned char)(Duration&0xFF); - return(4); +int makeDTMFpayload(unsigned char* payload_data, + int Event, + int End, + int Volume, + int Duration) { + unsigned char E, R, V; + R = 0; + V = (unsigned char)Volume; + if (End == 0) { + E = 0x00; + } else { + E = 0x80; + } + payload_data[0] = (unsigned char)Event; + payload_data[1] = (unsigned char)(E | R | V); + // Duration equals 8 times time_ms, default is 8000 Hz. + payload_data[2] = (unsigned char)((Duration >> 8) & 0xFF); + payload_data[3] = (unsigned char)(Duration & 0xFF); + return (4); } -void stereoDeInterleave(int16_t* audioSamples, int numSamples) -{ +void stereoDeInterleave(int16_t* audioSamples, int numSamples) { + int16_t* tempVec; + int16_t* readPtr, *writeL, *writeR; - int16_t *tempVec; - int16_t *readPtr, *writeL, *writeR; + if (numSamples <= 0) + return; - if (numSamples <= 0) - return; + tempVec = (int16_t*)malloc(sizeof(int16_t) * numSamples); + if (tempVec == NULL) { + printf("Error allocating memory\n"); + exit(0); + } - tempVec = (int16_t *) malloc(sizeof(int16_t) * numSamples); - if (tempVec == NULL) { - printf("Error allocating memory\n"); - exit(0); - } + memcpy(tempVec, audioSamples, numSamples * sizeof(int16_t)); - memcpy(tempVec, audioSamples, numSamples*sizeof(int16_t)); + writeL = audioSamples; + writeR = &audioSamples[numSamples / 2]; + readPtr = tempVec; - writeL = audioSamples; - writeR = &audioSamples[numSamples/2]; - readPtr = tempVec; - - for (int k = 0; k < numSamples; k += 2) - { - *writeL = *readPtr; - readPtr++; - *writeR = *readPtr; - readPtr++; - writeL++; - writeR++; - } - - free(tempVec); + for (int k = 0; k < numSamples; k += 2) { + *writeL = *readPtr; + readPtr++; + *writeR = *readPtr; + readPtr++; + writeL++; + writeR++; + } + free(tempVec); } +void stereoInterleave(unsigned char* data, int dataLen, int stride) { + unsigned char* ptrL, *ptrR; + unsigned char temp[10]; -void stereoInterleave(unsigned char* data, int dataLen, int stride) -{ + if (stride > 10) { + exit(0); + } - unsigned char *ptrL, *ptrR; - unsigned char temp[10]; + if (dataLen % 1 != 0) { + // must be even number of samples + printf("Error: cannot interleave odd sample number\n"); + exit(0); + } - if (stride > 10) - { - exit(0); - } + ptrL = data + stride; + ptrR = &data[dataLen / 2]; - if (dataLen%1 != 0) - { - // must be even number of samples - printf("Error: cannot interleave odd sample number\n"); - exit(0); - } + while (ptrL < ptrR) { + // copy from right pointer to temp + memcpy(temp, ptrR, stride); - ptrL = data + stride; - ptrR = &data[dataLen/2]; + // shift data between pointers + memmove(ptrL + stride, ptrL, ptrR - ptrL); - while (ptrL < ptrR) { - // copy from right pointer to temp - memcpy(temp, ptrR, stride); - - // shift data between pointers - memmove(ptrL + stride, ptrL, ptrR - ptrL); - - // copy from temp to left pointer - memcpy(ptrL, temp, stride); - - // advance pointers - ptrL += stride*2; - ptrR += stride; - } + // copy from temp to left pointer + memcpy(ptrL, temp, stride); + // advance pointers + ptrL += stride * 2; + ptrR += stride; + } }