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