RTPencode.cc revision a3c4d4dd2cece2cfbbd687eb76da833c37fbde3c
1/* 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11//TODO(hlundin): Reformat file to meet style guide. 12 13/* header includes */ 14#include <stdio.h> 15#include <stdlib.h> 16#include <string.h> 17#ifdef WIN32 18#include <winsock2.h> 19#endif 20#ifdef WEBRTC_LINUX 21#include <netinet/in.h> 22#endif 23 24#include <assert.h> 25 26#include "webrtc/typedefs.h" 27// needed for NetEqDecoder 28#include "webrtc/modules/audio_coding/neteq/interface/audio_decoder.h" 29#include "webrtc/modules/audio_coding/neteq/interface/neteq.h" 30 31/************************/ 32/* Define payload types */ 33/************************/ 34 35#include "PayloadTypes.h" 36 37 38 39/*********************/ 40/* Misc. definitions */ 41/*********************/ 42 43#define STOPSENDTIME 3000 44#define RESTARTSENDTIME 0 //162500 45#define FIRSTLINELEN 40 46#define CHECK_NOT_NULL(a) if((a)==0){printf("\n %s \n line: %d \nerror at %s\n",__FILE__,__LINE__,#a );return(-1);} 47 48//#define MULTIPLE_SAME_TIMESTAMP 49#define REPEAT_PACKET_DISTANCE 17 50#define REPEAT_PACKET_COUNT 1 // number of extra packets to send 51 52//#define INSERT_OLD_PACKETS 53#define OLD_PACKET 5 // how many seconds too old should the packet be? 54 55//#define TIMESTAMP_WRAPAROUND 56 57//#define RANDOM_DATA 58//#define RANDOM_PAYLOAD_DATA 59#define RANDOM_SEED 10 60 61//#define INSERT_DTMF_PACKETS 62//#define NO_DTMF_OVERDUB 63#define DTMF_PACKET_INTERVAL 2000 64#define DTMF_DURATION 500 65 66#define STEREO_MODE_FRAME 0 67#define STEREO_MODE_SAMPLE_1 1 //1 octet per sample 68#define STEREO_MODE_SAMPLE_2 2 //2 octets per sample 69 70/*************************/ 71/* Function declarations */ 72/*************************/ 73 74void NetEQTest_GetCodec_and_PT(char * name, webrtc::NetEqDecoder *codec, int *PT, int frameLen, int *fs, int *bitrate, int *useRed); 75int NetEQTest_init_coders(webrtc::NetEqDecoder coder, int enc_frameSize, int bitrate, int sampfreq , int vad, int numChannels); 76void defineCodecs(webrtc::NetEqDecoder *usedCodec, int *noOfCodecs ); 77int NetEQTest_free_coders(webrtc::NetEqDecoder coder, int numChannels); 78int NetEQTest_encode(int coder, int16_t *indata, int frameLen, unsigned char * encoded,int sampleRate , int * vad, int useVAD, int bitrate, int numChannels); 79void makeRTPheader(unsigned char* rtp_data, int payloadType, int seqNo, uint32_t timestamp, uint32_t ssrc); 80int makeRedundantHeader(unsigned char* rtp_data, int *payloadType, int numPayloads, uint32_t *timestamp, uint16_t *blockLen, 81 int seqNo, uint32_t ssrc); 82int makeDTMFpayload(unsigned char* payload_data, int Event, int End, int Volume, int Duration); 83void stereoDeInterleave(int16_t* audioSamples, int numSamples); 84void stereoInterleave(unsigned char* data, int dataLen, int stride); 85 86/*********************/ 87/* Codec definitions */ 88/*********************/ 89 90#include "webrtc_vad.h" 91 92#if ((defined CODEC_PCM16B)||(defined NETEQ_ARBITRARY_CODEC)) 93 #include "pcm16b.h" 94#endif 95#ifdef CODEC_G711 96 #include "g711_interface.h" 97#endif 98#ifdef CODEC_G729 99 #include "G729Interface.h" 100#endif 101#ifdef CODEC_G729_1 102 #include "G729_1Interface.h" 103#endif 104#ifdef CODEC_AMR 105 #include "AMRInterface.h" 106 #include "AMRCreation.h" 107#endif 108#ifdef CODEC_AMRWB 109 #include "AMRWBInterface.h" 110 #include "AMRWBCreation.h" 111#endif 112#ifdef CODEC_ILBC 113 #include "ilbc.h" 114#endif 115#if (defined CODEC_ISAC || defined CODEC_ISAC_SWB) 116 #include "isac.h" 117#endif 118#ifdef NETEQ_ISACFIX_CODEC 119 #include "isacfix.h" 120 #ifdef CODEC_ISAC 121 #error Cannot have both ISAC and ISACfix defined. Please de-select one in the beginning of RTPencode.cpp 122 #endif 123#endif 124#ifdef CODEC_G722 125 #include "g722_interface.h" 126#endif 127#ifdef CODEC_G722_1_24 128 #include "G722_1Interface.h" 129#endif 130#ifdef CODEC_G722_1_32 131 #include "G722_1Interface.h" 132#endif 133#ifdef CODEC_G722_1_16 134 #include "G722_1Interface.h" 135#endif 136#ifdef CODEC_G722_1C_24 137 #include "G722_1Interface.h" 138#endif 139#ifdef CODEC_G722_1C_32 140 #include "G722_1Interface.h" 141#endif 142#ifdef CODEC_G722_1C_48 143 #include "G722_1Interface.h" 144#endif 145#ifdef CODEC_G726 146 #include "G726Creation.h" 147 #include "G726Interface.h" 148#endif 149#ifdef CODEC_GSMFR 150 #include "GSMFRInterface.h" 151 #include "GSMFRCreation.h" 152#endif 153#if (defined(CODEC_CNGCODEC8) || defined(CODEC_CNGCODEC16) || \ 154 defined(CODEC_CNGCODEC32) || defined(CODEC_CNGCODEC48)) 155 #include "webrtc_cng.h" 156#endif 157#if ((defined CODEC_SPEEX_8)||(defined CODEC_SPEEX_16)) 158 #include "SpeexInterface.h" 159#endif 160#ifdef CODEC_CELT_32 161#include "celt_interface.h" 162#endif 163 164 165/***********************************/ 166/* Global codec instance variables */ 167/***********************************/ 168 169WebRtcVadInst *VAD_inst[2]; 170 171#ifdef CODEC_G722 172 G722EncInst *g722EncState[2]; 173#endif 174 175#ifdef CODEC_G722_1_24 176 G722_1_24_encinst_t *G722_1_24enc_inst[2]; 177#endif 178#ifdef CODEC_G722_1_32 179 G722_1_32_encinst_t *G722_1_32enc_inst[2]; 180#endif 181#ifdef CODEC_G722_1_16 182 G722_1_16_encinst_t *G722_1_16enc_inst[2]; 183#endif 184#ifdef CODEC_G722_1C_24 185 G722_1C_24_encinst_t *G722_1C_24enc_inst[2]; 186#endif 187#ifdef CODEC_G722_1C_32 188 G722_1C_32_encinst_t *G722_1C_32enc_inst[2]; 189#endif 190#ifdef CODEC_G722_1C_48 191 G722_1C_48_encinst_t *G722_1C_48enc_inst[2]; 192#endif 193#ifdef CODEC_G726 194 G726_encinst_t *G726enc_inst[2]; 195#endif 196#ifdef CODEC_G729 197 G729_encinst_t *G729enc_inst[2]; 198#endif 199#ifdef CODEC_G729_1 200 G729_1_inst_t *G729_1_inst[2]; 201#endif 202#ifdef CODEC_AMR 203 AMR_encinst_t *AMRenc_inst[2]; 204 int16_t AMR_bitrate; 205#endif 206#ifdef CODEC_AMRWB 207 AMRWB_encinst_t *AMRWBenc_inst[2]; 208 int16_t AMRWB_bitrate; 209#endif 210#ifdef CODEC_ILBC 211 iLBC_encinst_t *iLBCenc_inst[2]; 212#endif 213#ifdef CODEC_ISAC 214 ISACStruct *ISAC_inst[2]; 215#endif 216#ifdef NETEQ_ISACFIX_CODEC 217 ISACFIX_MainStruct *ISAC_inst[2]; 218#endif 219#ifdef CODEC_ISAC_SWB 220 ISACStruct *ISACSWB_inst[2]; 221#endif 222#ifdef CODEC_GSMFR 223 GSMFR_encinst_t *GSMFRenc_inst[2]; 224#endif 225#if (defined(CODEC_CNGCODEC8) || defined(CODEC_CNGCODEC16) || \ 226 defined(CODEC_CNGCODEC32) || defined(CODEC_CNGCODEC48)) 227 CNG_enc_inst *CNGenc_inst[2]; 228#endif 229#ifdef CODEC_SPEEX_8 230 SPEEX_encinst_t *SPEEX8enc_inst[2]; 231#endif 232#ifdef CODEC_SPEEX_16 233 SPEEX_encinst_t *SPEEX16enc_inst[2]; 234#endif 235#ifdef CODEC_CELT_32 236 CELT_encinst_t *CELT32enc_inst[2]; 237#endif 238#ifdef CODEC_G711 239 void *G711state[2]={NULL, NULL}; 240#endif 241 242 243int main(int argc, char* argv[]) 244{ 245 int packet_size, fs; 246 webrtc::NetEqDecoder usedCodec; 247 int payloadType; 248 int bitrate = 0; 249 int useVAD, vad; 250 int useRed=0; 251 int len, enc_len; 252 int16_t org_data[4000]; 253 unsigned char rtp_data[8000]; 254 int16_t seqNo=0xFFF; 255 uint32_t ssrc=1235412312; 256 uint32_t timestamp=0xAC1245; 257 uint16_t length, plen; 258 uint32_t offset; 259 double sendtime = 0; 260 int red_PT[2] = {0}; 261 uint32_t red_TS[2] = {0}; 262 uint16_t red_len[2] = {0}; 263 int RTPheaderLen=12; 264 unsigned char red_data[8000]; 265#ifdef INSERT_OLD_PACKETS 266 uint16_t old_length, old_plen; 267 int old_enc_len; 268 int first_old_packet=1; 269 unsigned char old_rtp_data[8000]; 270 int packet_age=0; 271#endif 272#ifdef INSERT_DTMF_PACKETS 273 int NTone = 1; 274 int DTMFfirst = 1; 275 uint32_t DTMFtimestamp; 276 bool dtmfSent = false; 277#endif 278 bool usingStereo = false; 279 int stereoMode = 0; 280 int numChannels = 1; 281 282 /* check number of parameters */ 283 if ((argc != 6) && (argc != 7)) { 284 /* print help text and exit */ 285 printf("Application to encode speech into an RTP stream.\n"); 286 printf("The program reads a PCM file and encodes is using the specified codec.\n"); 287 printf("The coded speech is packetized in RTP packest and written to the output file.\n"); 288 printf("The format of the RTP stream file is simlilar to that of rtpplay,\n"); 289 printf("but with the receive time euqal to 0 for all packets.\n"); 290 printf("Usage:\n\n"); 291 printf("%s PCMfile RTPfile frameLen codec useVAD bitrate\n", argv[0]); 292 printf("where:\n"); 293 294 printf("PCMfile : PCM speech input file\n\n"); 295 296 printf("RTPfile : RTP stream output file\n\n"); 297 298 printf("frameLen : 80...960... Number of samples per packet (limit depends on codec)\n\n"); 299 300 printf("codecName\n"); 301#ifdef CODEC_PCM16B 302 printf(" : pcm16b 16 bit PCM (8kHz)\n"); 303#endif 304#ifdef CODEC_PCM16B_WB 305 printf(" : pcm16b_wb 16 bit PCM (16kHz)\n"); 306#endif 307#ifdef CODEC_PCM16B_32KHZ 308 printf(" : pcm16b_swb32 16 bit PCM (32kHz)\n"); 309#endif 310#ifdef CODEC_PCM16B_48KHZ 311 printf(" : pcm16b_swb48 16 bit PCM (48kHz)\n"); 312#endif 313#ifdef CODEC_G711 314 printf(" : pcma g711 A-law (8kHz)\n"); 315#endif 316#ifdef CODEC_G711 317 printf(" : pcmu g711 u-law (8kHz)\n"); 318#endif 319#ifdef CODEC_G729 320 printf(" : g729 G729 (8kHz and 8kbps) CELP (One-Three frame(s)/packet)\n"); 321#endif 322#ifdef CODEC_G729_1 323 printf(" : g729.1 G729.1 (16kHz) variable rate (8--32 kbps)\n"); 324#endif 325#ifdef CODEC_G722_1_16 326 printf(" : g722.1_16 G722.1 coder (16kHz) (g722.1 with 16kbps)\n"); 327#endif 328#ifdef CODEC_G722_1_24 329 printf(" : g722.1_24 G722.1 coder (16kHz) (the 24kbps version)\n"); 330#endif 331#ifdef CODEC_G722_1_32 332 printf(" : g722.1_32 G722.1 coder (16kHz) (the 32kbps version)\n"); 333#endif 334#ifdef CODEC_G722_1C_24 335 printf(" : g722.1C_24 G722.1 C coder (32kHz) (the 24kbps version)\n"); 336#endif 337#ifdef CODEC_G722_1C_32 338 printf(" : g722.1C_32 G722.1 C coder (32kHz) (the 32kbps version)\n"); 339#endif 340#ifdef CODEC_G722_1C_48 341 printf(" : g722.1C_48 G722.1 C coder (32kHz) (the 48kbps)\n"); 342#endif 343 344#ifdef CODEC_G726 345 printf(" : g726_16 G726 coder (8kHz) 16kbps\n"); 346 printf(" : g726_24 G726 coder (8kHz) 24kbps\n"); 347 printf(" : g726_32 G726 coder (8kHz) 32kbps\n"); 348 printf(" : g726_40 G726 coder (8kHz) 40kbps\n"); 349#endif 350#ifdef CODEC_AMR 351 printf(" : AMRXk Adaptive Multi Rate CELP codec (8kHz)\n"); 352 printf(" X = 4.75, 5.15, 5.9, 6.7, 7.4, 7.95, 10.2 or 12.2\n"); 353#endif 354#ifdef CODEC_AMRWB 355 printf(" : AMRwbXk Adaptive Multi Rate Wideband CELP codec (16kHz)\n"); 356 printf(" X = 7, 9, 12, 14, 16, 18, 20, 23 or 24\n"); 357#endif 358#ifdef CODEC_ILBC 359 printf(" : ilbc iLBC codec (8kHz and 13.8kbps)\n"); 360#endif 361#ifdef CODEC_ISAC 362 printf(" : isac iSAC (16kHz and 32.0 kbps). To set rate specify a rate parameter as last parameter\n"); 363#endif 364#ifdef CODEC_ISAC_SWB 365 printf(" : isacswb iSAC SWB (32kHz and 32.0-52.0 kbps). To set rate specify a rate parameter as last parameter\n"); 366#endif 367#ifdef CODEC_GSMFR 368 printf(" : gsmfr GSM FR codec (8kHz and 13kbps)\n"); 369#endif 370#ifdef CODEC_G722 371 printf(" : g722 g722 coder (16kHz) (the 64kbps version)\n"); 372#endif 373#ifdef CODEC_SPEEX_8 374 printf(" : speex8 speex coder (8 kHz)\n"); 375#endif 376#ifdef CODEC_SPEEX_16 377 printf(" : speex16 speex coder (16 kHz)\n"); 378#endif 379#ifdef CODEC_CELT_32 380 printf(" : celt32 celt coder (32 kHz)\n"); 381#endif 382#ifdef CODEC_RED 383#ifdef CODEC_G711 384 printf(" : red_pcm Redundancy RTP packet with 2*G711A frames\n"); 385#endif 386#ifdef CODEC_ISAC 387 printf(" : red_isac Redundancy RTP packet with 2*iSAC frames\n"); 388#endif 389#endif 390 printf("\n"); 391 392#if (defined(CODEC_CNGCODEC8) || defined(CODEC_CNGCODEC16) || \ 393 defined(CODEC_CNGCODEC32) || defined(CODEC_CNGCODEC48)) 394 printf("useVAD : 0 Voice Activity Detection is switched off\n"); 395 printf(" : 1 Voice Activity Detection is switched on\n\n"); 396#else 397 printf("useVAD : 0 Voice Activity Detection switched off (on not supported)\n\n"); 398#endif 399 printf("bitrate : Codec bitrate in bps (only applies to vbr codecs)\n\n"); 400 401 return(0); 402 } 403 404 FILE* in_file=fopen(argv[1],"rb"); 405 CHECK_NOT_NULL(in_file); 406 printf("Input file: %s\n",argv[1]); 407 FILE* out_file=fopen(argv[2],"wb"); 408 CHECK_NOT_NULL(out_file); 409 printf("Output file: %s\n\n",argv[2]); 410 packet_size=atoi(argv[3]); 411 CHECK_NOT_NULL(packet_size); 412 printf("Packet size: %i\n",packet_size); 413 414 // check for stereo 415 if(argv[4][strlen(argv[4])-1] == '*') { 416 // use stereo 417 usingStereo = true; 418 numChannels = 2; 419 argv[4][strlen(argv[4])-1] = '\0'; 420 } 421 422 NetEQTest_GetCodec_and_PT(argv[4], &usedCodec, &payloadType, packet_size, &fs, &bitrate, &useRed); 423 424 if(useRed) { 425 RTPheaderLen = 12 + 4 + 1; /* standard RTP = 12; 4 bytes per redundant payload, except last one which is 1 byte */ 426 } 427 428 useVAD=atoi(argv[5]); 429#if !(defined(CODEC_CNGCODEC8) || defined(CODEC_CNGCODEC16) || \ 430 defined(CODEC_CNGCODEC32) || defined(CODEC_CNGCODEC48)) 431 if (useVAD!=0) { 432 printf("Error: this simulation does not support VAD/DTX/CNG\n"); 433 } 434#endif 435 436 // check stereo type 437 if(usingStereo) 438 { 439 switch(usedCodec) 440 { 441 // sample based codecs 442 case webrtc::kDecoderPCMu: 443 case webrtc::kDecoderPCMa: 444 case webrtc::kDecoderG722: 445 { 446 // 1 octet per sample 447 stereoMode = STEREO_MODE_SAMPLE_1; 448 break; 449 } 450 case webrtc::kDecoderPCM16B: 451 case webrtc::kDecoderPCM16Bwb: 452 case webrtc::kDecoderPCM16Bswb32kHz: 453 case webrtc::kDecoderPCM16Bswb48kHz: 454 { 455 // 2 octets per sample 456 stereoMode = STEREO_MODE_SAMPLE_2; 457 break; 458 } 459 460 // fixed-rate frame codecs (with internal VAD) 461 default: 462 { 463 printf("Cannot use codec %s as stereo codec\n", argv[4]); 464 exit(0); 465 } 466 } 467 } 468 469 if ((usedCodec == webrtc::kDecoderISAC) || (usedCodec == webrtc::kDecoderISACswb)) 470 { 471 if (argc != 7) 472 { 473 if (usedCodec == webrtc::kDecoderISAC) 474 { 475 bitrate = 32000; 476 printf( 477 "Running iSAC at default bitrate of 32000 bps (to specify explicitly add the bps as last parameter)\n"); 478 } 479 else // (usedCodec==webrtc::kDecoderISACswb) 480 { 481 bitrate = 56000; 482 printf( 483 "Running iSAC at default bitrate of 56000 bps (to specify explicitly add the bps as last parameter)\n"); 484 } 485 } 486 else 487 { 488 bitrate = atoi(argv[6]); 489 if (usedCodec == webrtc::kDecoderISAC) 490 { 491 if ((bitrate < 10000) || (bitrate > 32000)) 492 { 493 printf( 494 "Error: iSAC bitrate must be between 10000 and 32000 bps (%i is invalid)\n", 495 bitrate); 496 exit(0); 497 } 498 printf("Running iSAC at bitrate of %i bps\n", bitrate); 499 } 500 else // (usedCodec==webrtc::kDecoderISACswb) 501 { 502 if ((bitrate < 32000) || (bitrate > 56000)) 503 { 504 printf( 505 "Error: iSAC SWB bitrate must be between 32000 and 56000 bps (%i is invalid)\n", 506 bitrate); 507 exit(0); 508 } 509 } 510 } 511 } 512 else 513 { 514 if (argc == 7) 515 { 516 printf( 517 "Error: Bitrate parameter can only be specified for iSAC, G.723, and G.729.1\n"); 518 exit(0); 519 } 520 } 521 522 if(useRed) { 523 printf("Redundancy engaged. "); 524 } 525 printf("Used codec: %i\n",usedCodec); 526 printf("Payload type: %i\n",payloadType); 527 528 NetEQTest_init_coders(usedCodec, packet_size, bitrate, fs, useVAD, numChannels); 529 530 /* write file header */ 531 //fprintf(out_file, "#!RTPencode%s\n", "1.0"); 532 fprintf(out_file, "#!rtpplay%s \n", "1.0"); // this is the string that rtpplay needs 533 uint32_t dummy_variable = 0; // should be converted to network endian format, but does not matter when 0 534 if (fwrite(&dummy_variable, 4, 1, out_file) != 1) { 535 return -1; 536 } 537 if (fwrite(&dummy_variable, 4, 1, out_file) != 1) { 538 return -1; 539 } 540 if (fwrite(&dummy_variable, 4, 1, out_file) != 1) { 541 return -1; 542 } 543 if (fwrite(&dummy_variable, 2, 1, out_file) != 1) { 544 return -1; 545 } 546 if (fwrite(&dummy_variable, 2, 1, out_file) != 1) { 547 return -1; 548 } 549 550#ifdef TIMESTAMP_WRAPAROUND 551 timestamp = 0xFFFFFFFF - fs*10; /* should give wrap-around in 10 seconds */ 552#endif 553#if defined(RANDOM_DATA) | defined(RANDOM_PAYLOAD_DATA) 554 srand(RANDOM_SEED); 555#endif 556 557 /* if redundancy is used, the first redundant payload is zero length */ 558 red_len[0] = 0; 559 560 /* read first frame */ 561 len=fread(org_data,2,packet_size * numChannels,in_file) / numChannels; 562 563 /* de-interleave if stereo */ 564 if ( usingStereo ) 565 { 566 stereoDeInterleave(org_data, len * numChannels); 567 } 568 569 while (len==packet_size) { 570 571#ifdef INSERT_DTMF_PACKETS 572 dtmfSent = false; 573 574 if ( sendtime >= NTone * DTMF_PACKET_INTERVAL ) { 575 if ( sendtime < NTone * DTMF_PACKET_INTERVAL + DTMF_DURATION ) { 576 // tone has not ended 577 if (DTMFfirst==1) { 578 DTMFtimestamp = timestamp; // save this timestamp 579 DTMFfirst=0; 580 } 581 makeRTPheader(rtp_data, NETEQ_CODEC_AVT_PT, seqNo,DTMFtimestamp, ssrc); 582 enc_len = makeDTMFpayload(&rtp_data[12], NTone % 12, 0, 4, (int) (sendtime - NTone * DTMF_PACKET_INTERVAL)*(fs/1000) + len); 583 } 584 else { 585 // tone has ended 586 makeRTPheader(rtp_data, NETEQ_CODEC_AVT_PT, seqNo,DTMFtimestamp, ssrc); 587 enc_len = makeDTMFpayload(&rtp_data[12], NTone % 12, 1, 4, DTMF_DURATION*(fs/1000)); 588 NTone++; 589 DTMFfirst=1; 590 } 591 592 /* write RTP packet to file */ 593 length = htons(12 + enc_len + 8); 594 plen = htons(12 + enc_len); 595 offset = (uint32_t) sendtime; //(timestamp/(fs/1000)); 596 offset = htonl(offset); 597 if (fwrite(&length, 2, 1, out_file) != 1) { 598 return -1; 599 } 600 if (fwrite(&plen, 2, 1, out_file) != 1) { 601 return -1; 602 } 603 if (fwrite(&offset, 4, 1, out_file) != 1) { 604 return -1; 605 } 606 if (fwrite(rtp_data, 12 + enc_len, 1, out_file) != 1) { 607 return -1; 608 } 609 610 dtmfSent = true; 611 } 612#endif 613 614#ifdef NO_DTMF_OVERDUB 615 /* If DTMF is sent, we should not send any speech packets during the same time */ 616 if (dtmfSent) { 617 enc_len = 0; 618 } 619 else { 620#endif 621 /* encode frame */ 622 enc_len=NetEQTest_encode(usedCodec, org_data, packet_size, &rtp_data[12] ,fs,&vad, useVAD, bitrate, numChannels); 623 if (enc_len==-1) { 624 printf("Error encoding frame\n"); 625 exit(0); 626 } 627 628 if ( usingStereo && 629 stereoMode != STEREO_MODE_FRAME && 630 vad == 1 ) 631 { 632 // interleave the encoded payload for sample-based codecs (not for CNG) 633 stereoInterleave(&rtp_data[12], enc_len, stereoMode); 634 } 635#ifdef NO_DTMF_OVERDUB 636 } 637#endif 638 639 if (enc_len > 0 && (sendtime <= STOPSENDTIME || sendtime > RESTARTSENDTIME)) { 640 if(useRed) { 641 if(red_len[0] > 0) { 642 memmove(&rtp_data[RTPheaderLen+red_len[0]], &rtp_data[12], enc_len); 643 memcpy(&rtp_data[RTPheaderLen], red_data, red_len[0]); 644 645 red_len[1] = enc_len; 646 red_TS[1] = timestamp; 647 if(vad) 648 red_PT[1] = payloadType; 649 else 650 red_PT[1] = NETEQ_CODEC_CN_PT; 651 652 makeRedundantHeader(rtp_data, red_PT, 2, red_TS, red_len, seqNo++, ssrc); 653 654 655 enc_len += red_len[0] + RTPheaderLen - 12; 656 } 657 else { // do not use redundancy payload for this packet, i.e., only last payload 658 memmove(&rtp_data[RTPheaderLen-4], &rtp_data[12], enc_len); 659 //memcpy(&rtp_data[RTPheaderLen], red_data, red_len[0]); 660 661 red_len[1] = enc_len; 662 red_TS[1] = timestamp; 663 if(vad) 664 red_PT[1] = payloadType; 665 else 666 red_PT[1] = NETEQ_CODEC_CN_PT; 667 668 makeRedundantHeader(rtp_data, red_PT, 2, red_TS, red_len, seqNo++, ssrc); 669 670 671 enc_len += red_len[0] + RTPheaderLen - 4 - 12; // 4 is length of redundancy header (not used) 672 } 673 } 674 else { 675 676 /* make RTP header */ 677 if (vad) // regular speech data 678 makeRTPheader(rtp_data, payloadType, seqNo++,timestamp, ssrc); 679 else // CNG data 680 makeRTPheader(rtp_data, NETEQ_CODEC_CN_PT, seqNo++,timestamp, ssrc); 681 682 } 683#ifdef MULTIPLE_SAME_TIMESTAMP 684 int mult_pack=0; 685 do { 686#endif //MULTIPLE_SAME_TIMESTAMP 687 /* write RTP packet to file */ 688 length = htons(12 + enc_len + 8); 689 plen = htons(12 + enc_len); 690 offset = (uint32_t) sendtime; 691 //(timestamp/(fs/1000)); 692 offset = htonl(offset); 693 if (fwrite(&length, 2, 1, out_file) != 1) { 694 return -1; 695 } 696 if (fwrite(&plen, 2, 1, out_file) != 1) { 697 return -1; 698 } 699 if (fwrite(&offset, 4, 1, out_file) != 1) { 700 return -1; 701 } 702#ifdef RANDOM_DATA 703 for (int k=0; k<12+enc_len; k++) { 704 rtp_data[k] = rand() + rand(); 705 } 706#endif 707#ifdef RANDOM_PAYLOAD_DATA 708 for (int k=12; k<12+enc_len; k++) { 709 rtp_data[k] = rand() + rand(); 710 } 711#endif 712 if (fwrite(rtp_data, 12 + enc_len, 1, out_file) != 1) { 713 return -1; 714 } 715#ifdef MULTIPLE_SAME_TIMESTAMP 716 } while ( (seqNo%REPEAT_PACKET_DISTANCE == 0) && (mult_pack++ < REPEAT_PACKET_COUNT) ); 717#endif //MULTIPLE_SAME_TIMESTAMP 718 719#ifdef INSERT_OLD_PACKETS 720 if (packet_age >= OLD_PACKET*fs) { 721 if (!first_old_packet) { 722 // send the old packet 723 if (fwrite(&old_length, 2, 1, 724 out_file) != 1) { 725 return -1; 726 } 727 if (fwrite(&old_plen, 2, 1, 728 out_file) != 1) { 729 return -1; 730 } 731 if (fwrite(&offset, 4, 1, 732 out_file) != 1) { 733 return -1; 734 } 735 if (fwrite(old_rtp_data, 12 + old_enc_len, 736 1, out_file) != 1) { 737 return -1; 738 } 739 } 740 // store current packet as old 741 old_length=length; 742 old_plen=plen; 743 memcpy(old_rtp_data,rtp_data,12+enc_len); 744 old_enc_len=enc_len; 745 first_old_packet=0; 746 packet_age=0; 747 748 } 749 packet_age += packet_size; 750#endif 751 752 if(useRed) { 753 /* move data to redundancy store */ 754#ifdef CODEC_ISAC 755 if(usedCodec==webrtc::kDecoderISAC) 756 { 757 assert(!usingStereo); // Cannot handle stereo yet 758 red_len[0] = WebRtcIsac_GetRedPayload(ISAC_inst[0], (int16_t*)red_data); 759 } 760 else 761 { 762#endif 763 memcpy(red_data, &rtp_data[RTPheaderLen+red_len[0]], enc_len); 764 red_len[0]=red_len[1]; 765#ifdef CODEC_ISAC 766 } 767#endif 768 red_TS[0]=red_TS[1]; 769 red_PT[0]=red_PT[1]; 770 } 771 772 } 773 774 /* read next frame */ 775 len=fread(org_data,2,packet_size * numChannels,in_file) / numChannels; 776 /* de-interleave if stereo */ 777 if ( usingStereo ) 778 { 779 stereoDeInterleave(org_data, len * numChannels); 780 } 781 782 if (payloadType==NETEQ_CODEC_G722_PT) 783 timestamp+=len>>1; 784 else 785 timestamp+=len; 786 787 sendtime += (double) len/(fs/1000); 788 } 789 790 NetEQTest_free_coders(usedCodec, numChannels); 791 fclose(in_file); 792 fclose(out_file); 793 printf("Done!\n"); 794 795 return(0); 796} 797 798 799 800 801/****************/ 802/* Subfunctions */ 803/****************/ 804 805void NetEQTest_GetCodec_and_PT(char * name, webrtc::NetEqDecoder *codec, int *PT, int frameLen, int *fs, int *bitrate, int *useRed) { 806 807 *bitrate = 0; /* Default bitrate setting */ 808 *useRed = 0; /* Default no redundancy */ 809 810 if(!strcmp(name,"pcmu")){ 811 *codec=webrtc::kDecoderPCMu; 812 *PT=NETEQ_CODEC_PCMU_PT; 813 *fs=8000; 814 } 815 else if(!strcmp(name,"pcma")){ 816 *codec=webrtc::kDecoderPCMa; 817 *PT=NETEQ_CODEC_PCMA_PT; 818 *fs=8000; 819 } 820 else if(!strcmp(name,"pcm16b")){ 821 *codec=webrtc::kDecoderPCM16B; 822 *PT=NETEQ_CODEC_PCM16B_PT; 823 *fs=8000; 824 } 825 else if(!strcmp(name,"pcm16b_wb")){ 826 *codec=webrtc::kDecoderPCM16Bwb; 827 *PT=NETEQ_CODEC_PCM16B_WB_PT; 828 *fs=16000; 829 } 830 else if(!strcmp(name,"pcm16b_swb32")){ 831 *codec=webrtc::kDecoderPCM16Bswb32kHz; 832 *PT=NETEQ_CODEC_PCM16B_SWB32KHZ_PT; 833 *fs=32000; 834 } 835 else if(!strcmp(name,"pcm16b_swb48")){ 836 *codec=webrtc::kDecoderPCM16Bswb48kHz; 837 *PT=NETEQ_CODEC_PCM16B_SWB48KHZ_PT; 838 *fs=48000; 839 } 840 else if(!strcmp(name,"g722")){ 841 *codec=webrtc::kDecoderG722; 842 *PT=NETEQ_CODEC_G722_PT; 843 *fs=16000; 844 } 845 else if((!strcmp(name,"ilbc"))&&((frameLen%240==0)||(frameLen%160==0))){ 846 *fs=8000; 847 *codec=webrtc::kDecoderILBC; 848 *PT=NETEQ_CODEC_ILBC_PT; 849 } 850 else if(!strcmp(name,"isac")){ 851 *fs=16000; 852 *codec=webrtc::kDecoderISAC; 853 *PT=NETEQ_CODEC_ISAC_PT; 854 } 855 else if(!strcmp(name,"isacswb")){ 856 *fs=32000; 857 *codec=webrtc::kDecoderISACswb; 858 *PT=NETEQ_CODEC_ISACSWB_PT; 859 } 860 else if(!strcmp(name,"celt32")){ 861 *fs=32000; 862 *codec=webrtc::kDecoderCELT_32; 863 *PT=NETEQ_CODEC_CELT32_PT; 864 } 865 else if(!strcmp(name,"red_pcm")){ 866 *codec=webrtc::kDecoderPCMa; 867 *PT=NETEQ_CODEC_PCMA_PT; /* this will be the PT for the sub-headers */ 868 *fs=8000; 869 *useRed = 1; 870 } else if(!strcmp(name,"red_isac")){ 871 *codec=webrtc::kDecoderISAC; 872 *PT=NETEQ_CODEC_ISAC_PT; /* this will be the PT for the sub-headers */ 873 *fs=16000; 874 *useRed = 1; 875 } else { 876 printf("Error: Not a supported codec (%s)\n", name); 877 exit(0); 878 } 879 880} 881 882 883 884 885int NetEQTest_init_coders(webrtc::NetEqDecoder coder, int enc_frameSize, int bitrate, int sampfreq , int vad, int numChannels){ 886 887 int ok=0; 888 889 for (int k = 0; k < numChannels; k++) 890 { 891 ok=WebRtcVad_Create(&VAD_inst[k]); 892 if (ok!=0) { 893 printf("Error: Couldn't allocate memory for VAD instance\n"); 894 exit(0); 895 } 896 ok=WebRtcVad_Init(VAD_inst[k]); 897 if (ok==-1) { 898 printf("Error: Initialization of VAD struct failed\n"); 899 exit(0); 900 } 901 902 903#if (defined(CODEC_CNGCODEC8) || defined(CODEC_CNGCODEC16) || \ 904 defined(CODEC_CNGCODEC32) || defined(CODEC_CNGCODEC48)) 905 ok=WebRtcCng_CreateEnc(&CNGenc_inst[k]); 906 if (ok!=0) { 907 printf("Error: Couldn't allocate memory for CNG encoding instance\n"); 908 exit(0); 909 } 910 if(sampfreq <= 16000) { 911 ok=WebRtcCng_InitEnc(CNGenc_inst[k],sampfreq, 200, 5); 912 if (ok==-1) { 913 printf("Error: Initialization of CNG struct failed. Error code %d\n", 914 WebRtcCng_GetErrorCodeEnc(CNGenc_inst[k])); 915 exit(0); 916 } 917 } 918#endif 919 920 switch (coder) { 921#ifdef CODEC_PCM16B 922 case webrtc::kDecoderPCM16B : 923#endif 924#ifdef CODEC_PCM16B_WB 925 case webrtc::kDecoderPCM16Bwb : 926#endif 927#ifdef CODEC_PCM16B_32KHZ 928 case webrtc::kDecoderPCM16Bswb32kHz : 929#endif 930#ifdef CODEC_PCM16B_48KHZ 931 case webrtc::kDecoderPCM16Bswb48kHz : 932#endif 933#ifdef CODEC_G711 934 case webrtc::kDecoderPCMu : 935 case webrtc::kDecoderPCMa : 936#endif 937 // do nothing 938 break; 939#ifdef CODEC_G729 940 case webrtc::kDecoderG729: 941 if (sampfreq==8000) { 942 if ((enc_frameSize==80)||(enc_frameSize==160)||(enc_frameSize==240)||(enc_frameSize==320)||(enc_frameSize==400)||(enc_frameSize==480)) { 943 ok=WebRtcG729_CreateEnc(&G729enc_inst[k]); 944 if (ok!=0) { 945 printf("Error: Couldn't allocate memory for G729 encoding instance\n"); 946 exit(0); 947 } 948 } else { 949 printf("\nError: g729 only supports 10, 20, 30, 40, 50 or 60 ms!!\n\n"); 950 exit(0); 951 } 952 WebRtcG729_EncoderInit(G729enc_inst[k], vad); 953 if ((vad==1)&&(enc_frameSize!=80)) { 954 printf("\nError - This simulation only supports VAD for G729 at 10ms packets (not %dms)\n", (enc_frameSize>>3)); 955 } 956 } else { 957 printf("\nError - g729 is only developed for 8kHz \n"); 958 exit(0); 959 } 960 break; 961#endif 962#ifdef CODEC_G729_1 963 case webrtc::kDecoderG729_1: 964 if (sampfreq==16000) { 965 if ((enc_frameSize==320)||(enc_frameSize==640)||(enc_frameSize==960) 966 ) { 967 ok=WebRtcG7291_Create(&G729_1_inst[k]); 968 if (ok!=0) { 969 printf("Error: Couldn't allocate memory for G.729.1 codec instance\n"); 970 exit(0); 971 } 972 } else { 973 printf("\nError: G.729.1 only supports 20, 40 or 60 ms!!\n\n"); 974 exit(0); 975 } 976 if (!(((bitrate >= 12000) && (bitrate <= 32000) && (bitrate%2000 == 0)) || (bitrate == 8000))) { 977 /* must be 8, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, or 32 kbps */ 978 printf("\nError: G.729.1 bitrate must be 8000 or 12000--32000 in steps of 2000 bps\n"); 979 exit(0); 980 } 981 WebRtcG7291_EncoderInit(G729_1_inst[k], bitrate, 0 /* flag8kHz*/, 0 /*flagG729mode*/); 982 } else { 983 printf("\nError - G.729.1 input is always 16 kHz \n"); 984 exit(0); 985 } 986 break; 987#endif 988#ifdef CODEC_SPEEX_8 989 case webrtc::kDecoderSPEEX_8 : 990 if (sampfreq==8000) { 991 if ((enc_frameSize==160)||(enc_frameSize==320)||(enc_frameSize==480)) { 992 ok=WebRtcSpeex_CreateEnc(&SPEEX8enc_inst[k], sampfreq); 993 if (ok!=0) { 994 printf("Error: Couldn't allocate memory for Speex encoding instance\n"); 995 exit(0); 996 } 997 } else { 998 printf("\nError: Speex only supports 20, 40, and 60 ms!!\n\n"); 999 exit(0); 1000 } 1001 if ((vad==1)&&(enc_frameSize!=160)) { 1002 printf("\nError - This simulation only supports VAD for Speex at 20ms packets (not %dms)\n", (enc_frameSize>>3)); 1003 vad=0; 1004 } 1005 ok=WebRtcSpeex_EncoderInit(SPEEX8enc_inst[k], 0/*vbr*/, 3 /*complexity*/, vad); 1006 if (ok!=0) exit(0); 1007 } else { 1008 printf("\nError - Speex8 called with sample frequency other than 8 kHz.\n\n"); 1009 } 1010 break; 1011#endif 1012#ifdef CODEC_SPEEX_16 1013 case webrtc::kDecoderSPEEX_16 : 1014 if (sampfreq==16000) { 1015 if ((enc_frameSize==320)||(enc_frameSize==640)||(enc_frameSize==960)) { 1016 ok=WebRtcSpeex_CreateEnc(&SPEEX16enc_inst[k], sampfreq); 1017 if (ok!=0) { 1018 printf("Error: Couldn't allocate memory for Speex encoding instance\n"); 1019 exit(0); 1020 } 1021 } else { 1022 printf("\nError: Speex only supports 20, 40, and 60 ms!!\n\n"); 1023 exit(0); 1024 } 1025 if ((vad==1)&&(enc_frameSize!=320)) { 1026 printf("\nError - This simulation only supports VAD for Speex at 20ms packets (not %dms)\n", (enc_frameSize>>4)); 1027 vad=0; 1028 } 1029 ok=WebRtcSpeex_EncoderInit(SPEEX16enc_inst[k], 0/*vbr*/, 3 /*complexity*/, vad); 1030 if (ok!=0) exit(0); 1031 } else { 1032 printf("\nError - Speex16 called with sample frequency other than 16 kHz.\n\n"); 1033 } 1034 break; 1035#endif 1036#ifdef CODEC_CELT_32 1037 case webrtc::kDecoderCELT_32 : 1038 if (sampfreq==32000) { 1039 if (enc_frameSize==320) { 1040 ok=WebRtcCelt_CreateEnc(&CELT32enc_inst[k], 1 /*mono*/); 1041 if (ok!=0) { 1042 printf("Error: Couldn't allocate memory for Celt encoding instance\n"); 1043 exit(0); 1044 } 1045 } else { 1046 printf("\nError: Celt only supports 10 ms!!\n\n"); 1047 exit(0); 1048 } 1049 ok=WebRtcCelt_EncoderInit(CELT32enc_inst[k], 1 /*mono*/, 48000 /*bitrate*/); 1050 if (ok!=0) exit(0); 1051 } else { 1052 printf("\nError - Celt32 called with sample frequency other than 32 kHz.\n\n"); 1053 } 1054 break; 1055#endif 1056 1057#ifdef CODEC_G722_1_16 1058 case webrtc::kDecoderG722_1_16 : 1059 if (sampfreq==16000) { 1060 ok=WebRtcG7221_CreateEnc16(&G722_1_16enc_inst[k]); 1061 if (ok!=0) { 1062 printf("Error: Couldn't allocate memory for G.722.1 instance\n"); 1063 exit(0); 1064 } 1065 if (enc_frameSize==320) { 1066 } else { 1067 printf("\nError: G722.1 only supports 20 ms!!\n\n"); 1068 exit(0); 1069 } 1070 WebRtcG7221_EncoderInit16((G722_1_16_encinst_t*)G722_1_16enc_inst[k]); 1071 } else { 1072 printf("\nError - G722.1 is only developed for 16kHz \n"); 1073 exit(0); 1074 } 1075 break; 1076#endif 1077#ifdef CODEC_G722_1_24 1078 case webrtc::kDecoderG722_1_24 : 1079 if (sampfreq==16000) { 1080 ok=WebRtcG7221_CreateEnc24(&G722_1_24enc_inst[k]); 1081 if (ok!=0) { 1082 printf("Error: Couldn't allocate memory for G.722.1 instance\n"); 1083 exit(0); 1084 } 1085 if (enc_frameSize==320) { 1086 } else { 1087 printf("\nError: G722.1 only supports 20 ms!!\n\n"); 1088 exit(0); 1089 } 1090 WebRtcG7221_EncoderInit24((G722_1_24_encinst_t*)G722_1_24enc_inst[k]); 1091 } else { 1092 printf("\nError - G722.1 is only developed for 16kHz \n"); 1093 exit(0); 1094 } 1095 break; 1096#endif 1097#ifdef CODEC_G722_1_32 1098 case webrtc::kDecoderG722_1_32 : 1099 if (sampfreq==16000) { 1100 ok=WebRtcG7221_CreateEnc32(&G722_1_32enc_inst[k]); 1101 if (ok!=0) { 1102 printf("Error: Couldn't allocate memory for G.722.1 instance\n"); 1103 exit(0); 1104 } 1105 if (enc_frameSize==320) { 1106 } else { 1107 printf("\nError: G722.1 only supports 20 ms!!\n\n"); 1108 exit(0); 1109 } 1110 WebRtcG7221_EncoderInit32((G722_1_32_encinst_t*)G722_1_32enc_inst[k]); 1111 } else { 1112 printf("\nError - G722.1 is only developed for 16kHz \n"); 1113 exit(0); 1114 } 1115 break; 1116#endif 1117#ifdef CODEC_G722_1C_24 1118 case webrtc::kDecoderG722_1C_24 : 1119 if (sampfreq==32000) { 1120 ok=WebRtcG7221C_CreateEnc24(&G722_1C_24enc_inst[k]); 1121 if (ok!=0) { 1122 printf("Error: Couldn't allocate memory for G.722.1C instance\n"); 1123 exit(0); 1124 } 1125 if (enc_frameSize==640) { 1126 } else { 1127 printf("\nError: G722.1 C only supports 20 ms!!\n\n"); 1128 exit(0); 1129 } 1130 WebRtcG7221C_EncoderInit24((G722_1C_24_encinst_t*)G722_1C_24enc_inst[k]); 1131 } else { 1132 printf("\nError - G722.1 C is only developed for 32kHz \n"); 1133 exit(0); 1134 } 1135 break; 1136#endif 1137#ifdef CODEC_G722_1C_32 1138 case webrtc::kDecoderG722_1C_32 : 1139 if (sampfreq==32000) { 1140 ok=WebRtcG7221C_CreateEnc32(&G722_1C_32enc_inst[k]); 1141 if (ok!=0) { 1142 printf("Error: Couldn't allocate memory for G.722.1C instance\n"); 1143 exit(0); 1144 } 1145 if (enc_frameSize==640) { 1146 } else { 1147 printf("\nError: G722.1 C only supports 20 ms!!\n\n"); 1148 exit(0); 1149 } 1150 WebRtcG7221C_EncoderInit32((G722_1C_32_encinst_t*)G722_1C_32enc_inst[k]); 1151 } else { 1152 printf("\nError - G722.1 C is only developed for 32kHz \n"); 1153 exit(0); 1154 } 1155 break; 1156#endif 1157#ifdef CODEC_G722_1C_48 1158 case webrtc::kDecoderG722_1C_48 : 1159 if (sampfreq==32000) { 1160 ok=WebRtcG7221C_CreateEnc48(&G722_1C_48enc_inst[k]); 1161 if (ok!=0) { 1162 printf("Error: Couldn't allocate memory for G.722.1C instance\n"); 1163 exit(0); 1164 } 1165 if (enc_frameSize==640) { 1166 } else { 1167 printf("\nError: G722.1 C only supports 20 ms!!\n\n"); 1168 exit(0); 1169 } 1170 WebRtcG7221C_EncoderInit48((G722_1C_48_encinst_t*)G722_1C_48enc_inst[k]); 1171 } else { 1172 printf("\nError - G722.1 C is only developed for 32kHz \n"); 1173 exit(0); 1174 } 1175 break; 1176#endif 1177#ifdef CODEC_G722 1178 case webrtc::kDecoderG722 : 1179 if (sampfreq==16000) { 1180 if (enc_frameSize%2==0) { 1181 } else { 1182 printf("\nError - g722 frames must have an even number of enc_frameSize\n"); 1183 exit(0); 1184 } 1185 WebRtcG722_CreateEncoder(&g722EncState[k]); 1186 WebRtcG722_EncoderInit(g722EncState[k]); 1187 } else { 1188 printf("\nError - g722 is only developed for 16kHz \n"); 1189 exit(0); 1190 } 1191 break; 1192#endif 1193#ifdef CODEC_AMR 1194 case webrtc::kDecoderAMR : 1195 if (sampfreq==8000) { 1196 ok=WebRtcAmr_CreateEnc(&AMRenc_inst[k]); 1197 if (ok!=0) { 1198 printf("Error: Couldn't allocate memory for AMR encoding instance\n"); 1199 exit(0); 1200 }if ((enc_frameSize==160)||(enc_frameSize==320)||(enc_frameSize==480)) { 1201 } else { 1202 printf("\nError - AMR must have a multiple of 160 enc_frameSize\n"); 1203 exit(0); 1204 } 1205 WebRtcAmr_EncoderInit(AMRenc_inst[k], vad); 1206 WebRtcAmr_EncodeBitmode(AMRenc_inst[k], AMRBandwidthEfficient); 1207 AMR_bitrate = bitrate; 1208 } else { 1209 printf("\nError - AMR is only developed for 8kHz \n"); 1210 exit(0); 1211 } 1212 break; 1213#endif 1214#ifdef CODEC_AMRWB 1215 case webrtc::kDecoderAMRWB : 1216 if (sampfreq==16000) { 1217 ok=WebRtcAmrWb_CreateEnc(&AMRWBenc_inst[k]); 1218 if (ok!=0) { 1219 printf("Error: Couldn't allocate memory for AMRWB encoding instance\n"); 1220 exit(0); 1221 } 1222 if (((enc_frameSize/320)<0)||((enc_frameSize/320)>3)||((enc_frameSize%320)!=0)) { 1223 printf("\nError - AMRwb must have frameSize of 20, 40 or 60ms\n"); 1224 exit(0); 1225 } 1226 WebRtcAmrWb_EncoderInit(AMRWBenc_inst[k], vad); 1227 if (bitrate==7000) { 1228 AMRWB_bitrate = AMRWB_MODE_7k; 1229 } else if (bitrate==9000) { 1230 AMRWB_bitrate = AMRWB_MODE_9k; 1231 } else if (bitrate==12000) { 1232 AMRWB_bitrate = AMRWB_MODE_12k; 1233 } else if (bitrate==14000) { 1234 AMRWB_bitrate = AMRWB_MODE_14k; 1235 } else if (bitrate==16000) { 1236 AMRWB_bitrate = AMRWB_MODE_16k; 1237 } else if (bitrate==18000) { 1238 AMRWB_bitrate = AMRWB_MODE_18k; 1239 } else if (bitrate==20000) { 1240 AMRWB_bitrate = AMRWB_MODE_20k; 1241 } else if (bitrate==23000) { 1242 AMRWB_bitrate = AMRWB_MODE_23k; 1243 } else if (bitrate==24000) { 1244 AMRWB_bitrate = AMRWB_MODE_24k; 1245 } 1246 WebRtcAmrWb_EncodeBitmode(AMRWBenc_inst[k], AMRBandwidthEfficient); 1247 1248 } else { 1249 printf("\nError - AMRwb is only developed for 16kHz \n"); 1250 exit(0); 1251 } 1252 break; 1253#endif 1254#ifdef CODEC_ILBC 1255 case webrtc::kDecoderILBC : 1256 if (sampfreq==8000) { 1257 ok=WebRtcIlbcfix_EncoderCreate(&iLBCenc_inst[k]); 1258 if (ok!=0) { 1259 printf("Error: Couldn't allocate memory for iLBC encoding instance\n"); 1260 exit(0); 1261 } 1262 if ((enc_frameSize==160)||(enc_frameSize==240)||(enc_frameSize==320)||(enc_frameSize==480)) { 1263 } else { 1264 printf("\nError - iLBC only supports 160, 240, 320 and 480 enc_frameSize (20, 30, 40 and 60 ms)\n"); 1265 exit(0); 1266 } 1267 if ((enc_frameSize==160)||(enc_frameSize==320)) { 1268 /* 20 ms version */ 1269 WebRtcIlbcfix_EncoderInit(iLBCenc_inst[k], 20); 1270 } else { 1271 /* 30 ms version */ 1272 WebRtcIlbcfix_EncoderInit(iLBCenc_inst[k], 30); 1273 } 1274 } else { 1275 printf("\nError - iLBC is only developed for 8kHz \n"); 1276 exit(0); 1277 } 1278 break; 1279#endif 1280#ifdef CODEC_ISAC 1281 case webrtc::kDecoderISAC: 1282 if (sampfreq==16000) { 1283 ok=WebRtcIsac_Create(&ISAC_inst[k]); 1284 if (ok!=0) { 1285 printf("Error: Couldn't allocate memory for iSAC instance\n"); 1286 exit(0); 1287 }if ((enc_frameSize==480)||(enc_frameSize==960)) { 1288 } else { 1289 printf("\nError - iSAC only supports frameSize (30 and 60 ms)\n"); 1290 exit(0); 1291 } 1292 WebRtcIsac_EncoderInit(ISAC_inst[k],1); 1293 if ((bitrate<10000)||(bitrate>32000)) { 1294 printf("\nError - iSAC bitrate has to be between 10000 and 32000 bps (not %i)\n", bitrate); 1295 exit(0); 1296 } 1297 WebRtcIsac_Control(ISAC_inst[k], bitrate, enc_frameSize>>4); 1298 } else { 1299 printf("\nError - iSAC only supports 480 or 960 enc_frameSize (30 or 60 ms)\n"); 1300 exit(0); 1301 } 1302 break; 1303#endif 1304#ifdef NETEQ_ISACFIX_CODEC 1305 case webrtc::kDecoderISAC: 1306 if (sampfreq==16000) { 1307 ok=WebRtcIsacfix_Create(&ISAC_inst[k]); 1308 if (ok!=0) { 1309 printf("Error: Couldn't allocate memory for iSAC instance\n"); 1310 exit(0); 1311 }if ((enc_frameSize==480)||(enc_frameSize==960)) { 1312 } else { 1313 printf("\nError - iSAC only supports frameSize (30 and 60 ms)\n"); 1314 exit(0); 1315 } 1316 WebRtcIsacfix_EncoderInit(ISAC_inst[k],1); 1317 if ((bitrate<10000)||(bitrate>32000)) { 1318 printf("\nError - iSAC bitrate has to be between 10000 and 32000 bps (not %i)\n", bitrate); 1319 exit(0); 1320 } 1321 WebRtcIsacfix_Control(ISAC_inst[k], bitrate, enc_frameSize>>4); 1322 } else { 1323 printf("\nError - iSAC only supports 480 or 960 enc_frameSize (30 or 60 ms)\n"); 1324 exit(0); 1325 } 1326 break; 1327#endif 1328#ifdef CODEC_ISAC_SWB 1329 case webrtc::kDecoderISACswb: 1330 if (sampfreq==32000) { 1331 ok=WebRtcIsac_Create(&ISACSWB_inst[k]); 1332 if (ok!=0) { 1333 printf("Error: Couldn't allocate memory for iSAC SWB instance\n"); 1334 exit(0); 1335 }if (enc_frameSize==960) { 1336 } else { 1337 printf("\nError - iSAC SWB only supports frameSize 30 ms\n"); 1338 exit(0); 1339 } 1340 ok = WebRtcIsac_SetEncSampRate(ISACSWB_inst[k], 32000); 1341 if (ok!=0) { 1342 printf("Error: Couldn't set sample rate for iSAC SWB instance\n"); 1343 exit(0); 1344 } 1345 WebRtcIsac_EncoderInit(ISACSWB_inst[k],1); 1346 if ((bitrate<32000)||(bitrate>56000)) { 1347 printf("\nError - iSAC SWB bitrate has to be between 32000 and 56000 bps (not %i)\n", bitrate); 1348 exit(0); 1349 } 1350 WebRtcIsac_Control(ISACSWB_inst[k], bitrate, enc_frameSize>>5); 1351 } else { 1352 printf("\nError - iSAC SWB only supports 960 enc_frameSize (30 ms)\n"); 1353 exit(0); 1354 } 1355 break; 1356#endif 1357#ifdef CODEC_GSMFR 1358 case webrtc::kDecoderGSMFR: 1359 if (sampfreq==8000) { 1360 ok=WebRtcGSMFR_CreateEnc(&GSMFRenc_inst[k]); 1361 if (ok!=0) { 1362 printf("Error: Couldn't allocate memory for GSM FR encoding instance\n"); 1363 exit(0); 1364 } 1365 if ((enc_frameSize==160)||(enc_frameSize==320)||(enc_frameSize==480)) { 1366 } else { 1367 printf("\nError - GSM FR must have a multiple of 160 enc_frameSize\n"); 1368 exit(0); 1369 } 1370 WebRtcGSMFR_EncoderInit(GSMFRenc_inst[k], 0); 1371 } else { 1372 printf("\nError - GSM FR is only developed for 8kHz \n"); 1373 exit(0); 1374 } 1375 break; 1376#endif 1377 default : 1378 printf("Error: unknown codec in call to NetEQTest_init_coders.\n"); 1379 exit(0); 1380 break; 1381 } 1382 1383 if (ok != 0) { 1384 return(ok); 1385 } 1386 } // end for 1387 1388 return(0); 1389} 1390 1391 1392 1393 1394int NetEQTest_free_coders(webrtc::NetEqDecoder coder, int numChannels) { 1395 1396 for (int k = 0; k < numChannels; k++) 1397 { 1398 WebRtcVad_Free(VAD_inst[k]); 1399#if (defined(CODEC_CNGCODEC8) || defined(CODEC_CNGCODEC16) || \ 1400 defined(CODEC_CNGCODEC32) || defined(CODEC_CNGCODEC48)) 1401 WebRtcCng_FreeEnc(CNGenc_inst[k]); 1402#endif 1403 1404 switch (coder) 1405 { 1406#ifdef CODEC_PCM16B 1407 case webrtc::kDecoderPCM16B : 1408#endif 1409#ifdef CODEC_PCM16B_WB 1410 case webrtc::kDecoderPCM16Bwb : 1411#endif 1412#ifdef CODEC_PCM16B_32KHZ 1413 case webrtc::kDecoderPCM16Bswb32kHz : 1414#endif 1415#ifdef CODEC_PCM16B_48KHZ 1416 case webrtc::kDecoderPCM16Bswb48kHz : 1417#endif 1418#ifdef CODEC_G711 1419 case webrtc::kDecoderPCMu : 1420 case webrtc::kDecoderPCMa : 1421#endif 1422 // do nothing 1423 break; 1424#ifdef CODEC_G729 1425 case webrtc::kDecoderG729: 1426 WebRtcG729_FreeEnc(G729enc_inst[k]); 1427 break; 1428#endif 1429#ifdef CODEC_G729_1 1430 case webrtc::kDecoderG729_1: 1431 WebRtcG7291_Free(G729_1_inst[k]); 1432 break; 1433#endif 1434#ifdef CODEC_SPEEX_8 1435 case webrtc::kDecoderSPEEX_8 : 1436 WebRtcSpeex_FreeEnc(SPEEX8enc_inst[k]); 1437 break; 1438#endif 1439#ifdef CODEC_SPEEX_16 1440 case webrtc::kDecoderSPEEX_16 : 1441 WebRtcSpeex_FreeEnc(SPEEX16enc_inst[k]); 1442 break; 1443#endif 1444#ifdef CODEC_CELT_32 1445 case webrtc::kDecoderCELT_32 : 1446 WebRtcCelt_FreeEnc(CELT32enc_inst[k]); 1447 break; 1448#endif 1449 1450#ifdef CODEC_G722_1_16 1451 case webrtc::kDecoderG722_1_16 : 1452 WebRtcG7221_FreeEnc16(G722_1_16enc_inst[k]); 1453 break; 1454#endif 1455#ifdef CODEC_G722_1_24 1456 case webrtc::kDecoderG722_1_24 : 1457 WebRtcG7221_FreeEnc24(G722_1_24enc_inst[k]); 1458 break; 1459#endif 1460#ifdef CODEC_G722_1_32 1461 case webrtc::kDecoderG722_1_32 : 1462 WebRtcG7221_FreeEnc32(G722_1_32enc_inst[k]); 1463 break; 1464#endif 1465#ifdef CODEC_G722_1C_24 1466 case webrtc::kDecoderG722_1C_24 : 1467 WebRtcG7221C_FreeEnc24(G722_1C_24enc_inst[k]); 1468 break; 1469#endif 1470#ifdef CODEC_G722_1C_32 1471 case webrtc::kDecoderG722_1C_32 : 1472 WebRtcG7221C_FreeEnc32(G722_1C_32enc_inst[k]); 1473 break; 1474#endif 1475#ifdef CODEC_G722_1C_48 1476 case webrtc::kDecoderG722_1C_48 : 1477 WebRtcG7221C_FreeEnc48(G722_1C_48enc_inst[k]); 1478 break; 1479#endif 1480#ifdef CODEC_G722 1481 case webrtc::kDecoderG722 : 1482 WebRtcG722_FreeEncoder(g722EncState[k]); 1483 break; 1484#endif 1485#ifdef CODEC_AMR 1486 case webrtc::kDecoderAMR : 1487 WebRtcAmr_FreeEnc(AMRenc_inst[k]); 1488 break; 1489#endif 1490#ifdef CODEC_AMRWB 1491 case webrtc::kDecoderAMRWB : 1492 WebRtcAmrWb_FreeEnc(AMRWBenc_inst[k]); 1493 break; 1494#endif 1495#ifdef CODEC_ILBC 1496 case webrtc::kDecoderILBC : 1497 WebRtcIlbcfix_EncoderFree(iLBCenc_inst[k]); 1498 break; 1499#endif 1500#ifdef CODEC_ISAC 1501 case webrtc::kDecoderISAC: 1502 WebRtcIsac_Free(ISAC_inst[k]); 1503 break; 1504#endif 1505#ifdef NETEQ_ISACFIX_CODEC 1506 case webrtc::kDecoderISAC: 1507 WebRtcIsacfix_Free(ISAC_inst[k]); 1508 break; 1509#endif 1510#ifdef CODEC_ISAC_SWB 1511 case webrtc::kDecoderISACswb: 1512 WebRtcIsac_Free(ISACSWB_inst[k]); 1513 break; 1514#endif 1515#ifdef CODEC_GSMFR 1516 case webrtc::kDecoderGSMFR: 1517 WebRtcGSMFR_FreeEnc(GSMFRenc_inst[k]); 1518 break; 1519#endif 1520 default : 1521 printf("Error: unknown codec in call to NetEQTest_init_coders.\n"); 1522 exit(0); 1523 break; 1524 } 1525 } 1526 1527 return(0); 1528} 1529 1530 1531 1532 1533 1534 1535int NetEQTest_encode(int coder, int16_t *indata, int frameLen, unsigned char * encoded,int sampleRate , 1536 int * vad, int useVAD, int bitrate, int numChannels){ 1537 1538 short cdlen = 0; 1539 int16_t *tempdata; 1540 static int first_cng=1; 1541 int16_t tempLen; 1542 1543 *vad =1; 1544 1545 // check VAD first 1546 if(useVAD) 1547 { 1548 *vad = 0; 1549 1550 for (int k = 0; k < numChannels; k++) 1551 { 1552 tempLen = frameLen; 1553 tempdata = &indata[k*frameLen]; 1554 int localVad=0; 1555 /* Partition the signal and test each chunk for VAD. 1556 All chunks must be VAD=0 to produce a total VAD=0. */ 1557 while (tempLen >= 10*sampleRate/1000) { 1558 if ((tempLen % 30*sampleRate/1000) == 0) { // tempLen is multiple of 30ms 1559 localVad |= WebRtcVad_Process(VAD_inst[k] ,sampleRate, tempdata, 30*sampleRate/1000); 1560 tempdata += 30*sampleRate/1000; 1561 tempLen -= 30*sampleRate/1000; 1562 } 1563 else if (tempLen >= 20*sampleRate/1000) { // tempLen >= 20ms 1564 localVad |= WebRtcVad_Process(VAD_inst[k] ,sampleRate, tempdata, 20*sampleRate/1000); 1565 tempdata += 20*sampleRate/1000; 1566 tempLen -= 20*sampleRate/1000; 1567 } 1568 else { // use 10ms 1569 localVad |= WebRtcVad_Process(VAD_inst[k] ,sampleRate, tempdata, 10*sampleRate/1000); 1570 tempdata += 10*sampleRate/1000; 1571 tempLen -= 10*sampleRate/1000; 1572 } 1573 } 1574 1575 // aggregate all VAD decisions over all channels 1576 *vad |= localVad; 1577 } 1578 1579 if(!*vad){ 1580 // all channels are silent 1581 cdlen = 0; 1582 for (int k = 0; k < numChannels; k++) 1583 { 1584 WebRtcCng_Encode(CNGenc_inst[k],&indata[k*frameLen], (frameLen <= 640 ? frameLen : 640) /* max 640 */, 1585 encoded,&tempLen,first_cng); 1586 encoded += tempLen; 1587 cdlen += tempLen; 1588 } 1589 *vad=0; 1590 first_cng=0; 1591 return(cdlen); 1592 } 1593 } 1594 1595 1596 // loop over all channels 1597 int totalLen = 0; 1598 1599 for (int k = 0; k < numChannels; k++) 1600 { 1601 /* Encode with the selected coder type */ 1602 if (coder==webrtc::kDecoderPCMu) { /*g711 u-law */ 1603#ifdef CODEC_G711 1604 cdlen = WebRtcG711_EncodeU(G711state[k], indata, frameLen, (int16_t*) encoded); 1605#endif 1606 } 1607 else if (coder==webrtc::kDecoderPCMa) { /*g711 A-law */ 1608#ifdef CODEC_G711 1609 cdlen = WebRtcG711_EncodeA(G711state[k], indata, frameLen, (int16_t*) encoded); 1610 } 1611#endif 1612#ifdef CODEC_PCM16B 1613 else if ((coder==webrtc::kDecoderPCM16B)||(coder==webrtc::kDecoderPCM16Bwb)|| 1614 (coder==webrtc::kDecoderPCM16Bswb32kHz)||(coder==webrtc::kDecoderPCM16Bswb48kHz)) { /*pcm16b (8kHz, 16kHz, 32kHz or 48kHz) */ 1615 cdlen = WebRtcPcm16b_EncodeW16(indata, frameLen, (int16_t*) encoded); 1616 } 1617#endif 1618#ifdef CODEC_G722 1619 else if (coder==webrtc::kDecoderG722) { /*g722 */ 1620 cdlen=WebRtcG722_Encode(g722EncState[k], indata, frameLen, (int16_t*)encoded); 1621 assert(cdlen == frameLen>>1); 1622 } 1623#endif 1624#ifdef CODEC_ILBC 1625 else if (coder==webrtc::kDecoderILBC) { /*iLBC */ 1626 cdlen=WebRtcIlbcfix_Encode(iLBCenc_inst[k], indata,frameLen,(int16_t*)encoded); 1627 } 1628#endif 1629#if (defined(CODEC_ISAC) || defined(NETEQ_ISACFIX_CODEC)) // TODO(hlundin): remove all NETEQ_ISACFIX_CODEC 1630 else if (coder==webrtc::kDecoderISAC) { /*iSAC */ 1631 int noOfCalls=0; 1632 cdlen=0; 1633 while (cdlen<=0) { 1634#ifdef CODEC_ISAC /* floating point */ 1635 cdlen=WebRtcIsac_Encode(ISAC_inst[k],&indata[noOfCalls*160],(int16_t*)encoded); 1636#else /* fixed point */ 1637 cdlen=WebRtcIsacfix_Encode(ISAC_inst[k],&indata[noOfCalls*160],(int16_t*)encoded); 1638#endif 1639 noOfCalls++; 1640 } 1641 } 1642#endif 1643#ifdef CODEC_ISAC_SWB 1644 else if (coder==webrtc::kDecoderISACswb) { /* iSAC SWB */ 1645 int noOfCalls=0; 1646 cdlen=0; 1647 while (cdlen<=0) { 1648 cdlen=WebRtcIsac_Encode(ISACSWB_inst[k],&indata[noOfCalls*320],(int16_t*)encoded); 1649 noOfCalls++; 1650 } 1651 } 1652#endif 1653#ifdef CODEC_CELT_32 1654 else if (coder==webrtc::kDecoderCELT_32) { /* Celt */ 1655 int encodedLen = 0; 1656 cdlen = 0; 1657 while (cdlen <= 0) { 1658 cdlen = WebRtcCelt_Encode(CELT32enc_inst[k], &indata[encodedLen], encoded); 1659 encodedLen += 10*32; /* 10 ms */ 1660 } 1661 if( (encodedLen != frameLen) || cdlen < 0) { 1662 printf("Error encoding Celt frame!\n"); 1663 exit(0); 1664 } 1665 } 1666#endif 1667 1668 indata += frameLen; 1669 encoded += cdlen; 1670 totalLen += cdlen; 1671 1672 } // end for 1673 1674 first_cng=1; 1675 return(totalLen); 1676} 1677 1678 1679 1680void makeRTPheader(unsigned char* rtp_data, int payloadType, int seqNo, uint32_t timestamp, uint32_t ssrc){ 1681 1682 rtp_data[0]=(unsigned char)0x80; 1683 rtp_data[1]=(unsigned char)(payloadType & 0xFF); 1684 rtp_data[2]=(unsigned char)((seqNo>>8)&0xFF); 1685 rtp_data[3]=(unsigned char)((seqNo)&0xFF); 1686 rtp_data[4]=(unsigned char)((timestamp>>24)&0xFF); 1687 rtp_data[5]=(unsigned char)((timestamp>>16)&0xFF); 1688 1689 rtp_data[6]=(unsigned char)((timestamp>>8)&0xFF); 1690 rtp_data[7]=(unsigned char)(timestamp & 0xFF); 1691 1692 rtp_data[8]=(unsigned char)((ssrc>>24)&0xFF); 1693 rtp_data[9]=(unsigned char)((ssrc>>16)&0xFF); 1694 1695 rtp_data[10]=(unsigned char)((ssrc>>8)&0xFF); 1696 rtp_data[11]=(unsigned char)(ssrc & 0xFF); 1697} 1698 1699 1700int makeRedundantHeader(unsigned char* rtp_data, int *payloadType, int numPayloads, uint32_t *timestamp, uint16_t *blockLen, 1701 int seqNo, uint32_t ssrc) 1702{ 1703 1704 int i; 1705 unsigned char *rtpPointer; 1706 uint16_t offset; 1707 1708 /* first create "standard" RTP header */ 1709 makeRTPheader(rtp_data, NETEQ_CODEC_RED_PT, seqNo, timestamp[numPayloads-1], ssrc); 1710 1711 rtpPointer = &rtp_data[12]; 1712 1713 /* add one sub-header for each redundant payload (not the primary) */ 1714 for(i=0; i<numPayloads-1; i++) { /* |0 1 2 3 4 5 6 7| */ 1715 if(blockLen[i] > 0) { 1716 offset = (uint16_t) (timestamp[numPayloads-1] - timestamp[i]); 1717 1718 rtpPointer[0] = (unsigned char) ( 0x80 | (0x7F & payloadType[i]) ); /* |F| block PT | */ 1719 rtpPointer[1] = (unsigned char) ((offset >> 6) & 0xFF); /* | timestamp- | */ 1720 rtpPointer[2] = (unsigned char) ( ((offset & 0x3F)<<2) | 1721 ( (blockLen[i]>>8) & 0x03 ) ); /* | -offset |bl-| */ 1722 rtpPointer[3] = (unsigned char) ( blockLen[i] & 0xFF ); /* | -ock length | */ 1723 1724 rtpPointer += 4; 1725 } 1726 } 1727 1728 /* last sub-header */ 1729 rtpPointer[0]= (unsigned char) (0x00 | (0x7F&payloadType[numPayloads-1]));/* |F| block PT | */ 1730 rtpPointer += 1; 1731 1732 return(rtpPointer - rtp_data); /* length of header in bytes */ 1733} 1734 1735 1736 1737int makeDTMFpayload(unsigned char* payload_data, int Event, int End, int Volume, int Duration) { 1738 unsigned char E,R,V; 1739 R=0; 1740 V=(unsigned char)Volume; 1741 if (End==0) { 1742 E = 0x00; 1743 } else { 1744 E = 0x80; 1745 } 1746 payload_data[0]=(unsigned char)Event; 1747 payload_data[1]=(unsigned char)(E|R|V); 1748 //Duration equals 8 times time_ms, default is 8000 Hz. 1749 payload_data[2]=(unsigned char)((Duration>>8)&0xFF); 1750 payload_data[3]=(unsigned char)(Duration&0xFF); 1751 return(4); 1752} 1753 1754void stereoDeInterleave(int16_t* audioSamples, int numSamples) 1755{ 1756 1757 int16_t *tempVec; 1758 int16_t *readPtr, *writeL, *writeR; 1759 1760 if (numSamples <= 0) 1761 return; 1762 1763 tempVec = (int16_t *) malloc(sizeof(int16_t) * numSamples); 1764 if (tempVec == NULL) { 1765 printf("Error allocating memory\n"); 1766 exit(0); 1767 } 1768 1769 memcpy(tempVec, audioSamples, numSamples*sizeof(int16_t)); 1770 1771 writeL = audioSamples; 1772 writeR = &audioSamples[numSamples/2]; 1773 readPtr = tempVec; 1774 1775 for (int k = 0; k < numSamples; k += 2) 1776 { 1777 *writeL = *readPtr; 1778 readPtr++; 1779 *writeR = *readPtr; 1780 readPtr++; 1781 writeL++; 1782 writeR++; 1783 } 1784 1785 free(tempVec); 1786 1787} 1788 1789 1790void stereoInterleave(unsigned char* data, int dataLen, int stride) 1791{ 1792 1793 unsigned char *ptrL, *ptrR; 1794 unsigned char temp[10]; 1795 1796 if (stride > 10) 1797 { 1798 exit(0); 1799 } 1800 1801 if (dataLen%1 != 0) 1802 { 1803 // must be even number of samples 1804 printf("Error: cannot interleave odd sample number\n"); 1805 exit(0); 1806 } 1807 1808 ptrL = data + stride; 1809 ptrR = &data[dataLen/2]; 1810 1811 while (ptrL < ptrR) { 1812 // copy from right pointer to temp 1813 memcpy(temp, ptrR, stride); 1814 1815 // shift data between pointers 1816 memmove(ptrL + stride, ptrL, ptrR - ptrL); 1817 1818 // copy from temp to left pointer 1819 memcpy(ptrL, temp, stride); 1820 1821 // advance pointers 1822 ptrL += stride*2; 1823 ptrR += stride; 1824 } 1825 1826} 1827