RTPencode.cc revision 1172988c794d15706b4c951dcbaa57b11221d225
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 uint8_t 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] = 759 WebRtcIsac_GetRedPayload(ISAC_inst[0], red_data); 760 } 761 else 762 { 763#endif 764 memcpy(red_data, &rtp_data[RTPheaderLen+red_len[0]], enc_len); 765 red_len[0]=red_len[1]; 766#ifdef CODEC_ISAC 767 } 768#endif 769 red_TS[0]=red_TS[1]; 770 red_PT[0]=red_PT[1]; 771 } 772 773 } 774 775 /* read next frame */ 776 len=fread(org_data,2,packet_size * numChannels,in_file) / numChannels; 777 /* de-interleave if stereo */ 778 if ( usingStereo ) 779 { 780 stereoDeInterleave(org_data, len * numChannels); 781 } 782 783 if (payloadType==NETEQ_CODEC_G722_PT) 784 timestamp+=len>>1; 785 else 786 timestamp+=len; 787 788 sendtime += (double) len/(fs/1000); 789 } 790 791 NetEQTest_free_coders(usedCodec, numChannels); 792 fclose(in_file); 793 fclose(out_file); 794 printf("Done!\n"); 795 796 return(0); 797} 798 799 800 801 802/****************/ 803/* Subfunctions */ 804/****************/ 805 806void NetEQTest_GetCodec_and_PT(char * name, webrtc::NetEqDecoder *codec, int *PT, int frameLen, int *fs, int *bitrate, int *useRed) { 807 808 *bitrate = 0; /* Default bitrate setting */ 809 *useRed = 0; /* Default no redundancy */ 810 811 if(!strcmp(name,"pcmu")){ 812 *codec=webrtc::kDecoderPCMu; 813 *PT=NETEQ_CODEC_PCMU_PT; 814 *fs=8000; 815 } 816 else if(!strcmp(name,"pcma")){ 817 *codec=webrtc::kDecoderPCMa; 818 *PT=NETEQ_CODEC_PCMA_PT; 819 *fs=8000; 820 } 821 else if(!strcmp(name,"pcm16b")){ 822 *codec=webrtc::kDecoderPCM16B; 823 *PT=NETEQ_CODEC_PCM16B_PT; 824 *fs=8000; 825 } 826 else if(!strcmp(name,"pcm16b_wb")){ 827 *codec=webrtc::kDecoderPCM16Bwb; 828 *PT=NETEQ_CODEC_PCM16B_WB_PT; 829 *fs=16000; 830 } 831 else if(!strcmp(name,"pcm16b_swb32")){ 832 *codec=webrtc::kDecoderPCM16Bswb32kHz; 833 *PT=NETEQ_CODEC_PCM16B_SWB32KHZ_PT; 834 *fs=32000; 835 } 836 else if(!strcmp(name,"pcm16b_swb48")){ 837 *codec=webrtc::kDecoderPCM16Bswb48kHz; 838 *PT=NETEQ_CODEC_PCM16B_SWB48KHZ_PT; 839 *fs=48000; 840 } 841 else if(!strcmp(name,"g722")){ 842 *codec=webrtc::kDecoderG722; 843 *PT=NETEQ_CODEC_G722_PT; 844 *fs=16000; 845 } 846 else if((!strcmp(name,"ilbc"))&&((frameLen%240==0)||(frameLen%160==0))){ 847 *fs=8000; 848 *codec=webrtc::kDecoderILBC; 849 *PT=NETEQ_CODEC_ILBC_PT; 850 } 851 else if(!strcmp(name,"isac")){ 852 *fs=16000; 853 *codec=webrtc::kDecoderISAC; 854 *PT=NETEQ_CODEC_ISAC_PT; 855 } 856 else if(!strcmp(name,"isacswb")){ 857 *fs=32000; 858 *codec=webrtc::kDecoderISACswb; 859 *PT=NETEQ_CODEC_ISACSWB_PT; 860 } 861 else if(!strcmp(name,"celt32")){ 862 *fs=32000; 863 *codec=webrtc::kDecoderCELT_32; 864 *PT=NETEQ_CODEC_CELT32_PT; 865 } 866 else if(!strcmp(name,"red_pcm")){ 867 *codec=webrtc::kDecoderPCMa; 868 *PT=NETEQ_CODEC_PCMA_PT; /* this will be the PT for the sub-headers */ 869 *fs=8000; 870 *useRed = 1; 871 } else if(!strcmp(name,"red_isac")){ 872 *codec=webrtc::kDecoderISAC; 873 *PT=NETEQ_CODEC_ISAC_PT; /* this will be the PT for the sub-headers */ 874 *fs=16000; 875 *useRed = 1; 876 } else { 877 printf("Error: Not a supported codec (%s)\n", name); 878 exit(0); 879 } 880 881} 882 883 884 885 886int NetEQTest_init_coders(webrtc::NetEqDecoder coder, int enc_frameSize, int bitrate, int sampfreq , int vad, int numChannels){ 887 888 int ok=0; 889 890 for (int k = 0; k < numChannels; k++) 891 { 892 ok=WebRtcVad_Create(&VAD_inst[k]); 893 if (ok!=0) { 894 printf("Error: Couldn't allocate memory for VAD instance\n"); 895 exit(0); 896 } 897 ok=WebRtcVad_Init(VAD_inst[k]); 898 if (ok==-1) { 899 printf("Error: Initialization of VAD struct failed\n"); 900 exit(0); 901 } 902 903 904#if (defined(CODEC_CNGCODEC8) || defined(CODEC_CNGCODEC16) || \ 905 defined(CODEC_CNGCODEC32) || defined(CODEC_CNGCODEC48)) 906 ok=WebRtcCng_CreateEnc(&CNGenc_inst[k]); 907 if (ok!=0) { 908 printf("Error: Couldn't allocate memory for CNG encoding instance\n"); 909 exit(0); 910 } 911 if(sampfreq <= 16000) { 912 ok=WebRtcCng_InitEnc(CNGenc_inst[k],sampfreq, 200, 5); 913 if (ok==-1) { 914 printf("Error: Initialization of CNG struct failed. Error code %d\n", 915 WebRtcCng_GetErrorCodeEnc(CNGenc_inst[k])); 916 exit(0); 917 } 918 } 919#endif 920 921 switch (coder) { 922#ifdef CODEC_PCM16B 923 case webrtc::kDecoderPCM16B : 924#endif 925#ifdef CODEC_PCM16B_WB 926 case webrtc::kDecoderPCM16Bwb : 927#endif 928#ifdef CODEC_PCM16B_32KHZ 929 case webrtc::kDecoderPCM16Bswb32kHz : 930#endif 931#ifdef CODEC_PCM16B_48KHZ 932 case webrtc::kDecoderPCM16Bswb48kHz : 933#endif 934#ifdef CODEC_G711 935 case webrtc::kDecoderPCMu : 936 case webrtc::kDecoderPCMa : 937#endif 938 // do nothing 939 break; 940#ifdef CODEC_G729 941 case webrtc::kDecoderG729: 942 if (sampfreq==8000) { 943 if ((enc_frameSize==80)||(enc_frameSize==160)||(enc_frameSize==240)||(enc_frameSize==320)||(enc_frameSize==400)||(enc_frameSize==480)) { 944 ok=WebRtcG729_CreateEnc(&G729enc_inst[k]); 945 if (ok!=0) { 946 printf("Error: Couldn't allocate memory for G729 encoding instance\n"); 947 exit(0); 948 } 949 } else { 950 printf("\nError: g729 only supports 10, 20, 30, 40, 50 or 60 ms!!\n\n"); 951 exit(0); 952 } 953 WebRtcG729_EncoderInit(G729enc_inst[k], vad); 954 if ((vad==1)&&(enc_frameSize!=80)) { 955 printf("\nError - This simulation only supports VAD for G729 at 10ms packets (not %dms)\n", (enc_frameSize>>3)); 956 } 957 } else { 958 printf("\nError - g729 is only developed for 8kHz \n"); 959 exit(0); 960 } 961 break; 962#endif 963#ifdef CODEC_G729_1 964 case webrtc::kDecoderG729_1: 965 if (sampfreq==16000) { 966 if ((enc_frameSize==320)||(enc_frameSize==640)||(enc_frameSize==960) 967 ) { 968 ok=WebRtcG7291_Create(&G729_1_inst[k]); 969 if (ok!=0) { 970 printf("Error: Couldn't allocate memory for G.729.1 codec instance\n"); 971 exit(0); 972 } 973 } else { 974 printf("\nError: G.729.1 only supports 20, 40 or 60 ms!!\n\n"); 975 exit(0); 976 } 977 if (!(((bitrate >= 12000) && (bitrate <= 32000) && (bitrate%2000 == 0)) || (bitrate == 8000))) { 978 /* must be 8, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, or 32 kbps */ 979 printf("\nError: G.729.1 bitrate must be 8000 or 12000--32000 in steps of 2000 bps\n"); 980 exit(0); 981 } 982 WebRtcG7291_EncoderInit(G729_1_inst[k], bitrate, 0 /* flag8kHz*/, 0 /*flagG729mode*/); 983 } else { 984 printf("\nError - G.729.1 input is always 16 kHz \n"); 985 exit(0); 986 } 987 break; 988#endif 989#ifdef CODEC_SPEEX_8 990 case webrtc::kDecoderSPEEX_8 : 991 if (sampfreq==8000) { 992 if ((enc_frameSize==160)||(enc_frameSize==320)||(enc_frameSize==480)) { 993 ok=WebRtcSpeex_CreateEnc(&SPEEX8enc_inst[k], sampfreq); 994 if (ok!=0) { 995 printf("Error: Couldn't allocate memory for Speex encoding instance\n"); 996 exit(0); 997 } 998 } else { 999 printf("\nError: Speex only supports 20, 40, and 60 ms!!\n\n"); 1000 exit(0); 1001 } 1002 if ((vad==1)&&(enc_frameSize!=160)) { 1003 printf("\nError - This simulation only supports VAD for Speex at 20ms packets (not %dms)\n", (enc_frameSize>>3)); 1004 vad=0; 1005 } 1006 ok=WebRtcSpeex_EncoderInit(SPEEX8enc_inst[k], 0/*vbr*/, 3 /*complexity*/, vad); 1007 if (ok!=0) exit(0); 1008 } else { 1009 printf("\nError - Speex8 called with sample frequency other than 8 kHz.\n\n"); 1010 } 1011 break; 1012#endif 1013#ifdef CODEC_SPEEX_16 1014 case webrtc::kDecoderSPEEX_16 : 1015 if (sampfreq==16000) { 1016 if ((enc_frameSize==320)||(enc_frameSize==640)||(enc_frameSize==960)) { 1017 ok=WebRtcSpeex_CreateEnc(&SPEEX16enc_inst[k], sampfreq); 1018 if (ok!=0) { 1019 printf("Error: Couldn't allocate memory for Speex encoding instance\n"); 1020 exit(0); 1021 } 1022 } else { 1023 printf("\nError: Speex only supports 20, 40, and 60 ms!!\n\n"); 1024 exit(0); 1025 } 1026 if ((vad==1)&&(enc_frameSize!=320)) { 1027 printf("\nError - This simulation only supports VAD for Speex at 20ms packets (not %dms)\n", (enc_frameSize>>4)); 1028 vad=0; 1029 } 1030 ok=WebRtcSpeex_EncoderInit(SPEEX16enc_inst[k], 0/*vbr*/, 3 /*complexity*/, vad); 1031 if (ok!=0) exit(0); 1032 } else { 1033 printf("\nError - Speex16 called with sample frequency other than 16 kHz.\n\n"); 1034 } 1035 break; 1036#endif 1037#ifdef CODEC_CELT_32 1038 case webrtc::kDecoderCELT_32 : 1039 if (sampfreq==32000) { 1040 if (enc_frameSize==320) { 1041 ok=WebRtcCelt_CreateEnc(&CELT32enc_inst[k], 1 /*mono*/); 1042 if (ok!=0) { 1043 printf("Error: Couldn't allocate memory for Celt encoding instance\n"); 1044 exit(0); 1045 } 1046 } else { 1047 printf("\nError: Celt only supports 10 ms!!\n\n"); 1048 exit(0); 1049 } 1050 ok=WebRtcCelt_EncoderInit(CELT32enc_inst[k], 1 /*mono*/, 48000 /*bitrate*/); 1051 if (ok!=0) exit(0); 1052 } else { 1053 printf("\nError - Celt32 called with sample frequency other than 32 kHz.\n\n"); 1054 } 1055 break; 1056#endif 1057 1058#ifdef CODEC_G722_1_16 1059 case webrtc::kDecoderG722_1_16 : 1060 if (sampfreq==16000) { 1061 ok=WebRtcG7221_CreateEnc16(&G722_1_16enc_inst[k]); 1062 if (ok!=0) { 1063 printf("Error: Couldn't allocate memory for G.722.1 instance\n"); 1064 exit(0); 1065 } 1066 if (enc_frameSize==320) { 1067 } else { 1068 printf("\nError: G722.1 only supports 20 ms!!\n\n"); 1069 exit(0); 1070 } 1071 WebRtcG7221_EncoderInit16((G722_1_16_encinst_t*)G722_1_16enc_inst[k]); 1072 } else { 1073 printf("\nError - G722.1 is only developed for 16kHz \n"); 1074 exit(0); 1075 } 1076 break; 1077#endif 1078#ifdef CODEC_G722_1_24 1079 case webrtc::kDecoderG722_1_24 : 1080 if (sampfreq==16000) { 1081 ok=WebRtcG7221_CreateEnc24(&G722_1_24enc_inst[k]); 1082 if (ok!=0) { 1083 printf("Error: Couldn't allocate memory for G.722.1 instance\n"); 1084 exit(0); 1085 } 1086 if (enc_frameSize==320) { 1087 } else { 1088 printf("\nError: G722.1 only supports 20 ms!!\n\n"); 1089 exit(0); 1090 } 1091 WebRtcG7221_EncoderInit24((G722_1_24_encinst_t*)G722_1_24enc_inst[k]); 1092 } else { 1093 printf("\nError - G722.1 is only developed for 16kHz \n"); 1094 exit(0); 1095 } 1096 break; 1097#endif 1098#ifdef CODEC_G722_1_32 1099 case webrtc::kDecoderG722_1_32 : 1100 if (sampfreq==16000) { 1101 ok=WebRtcG7221_CreateEnc32(&G722_1_32enc_inst[k]); 1102 if (ok!=0) { 1103 printf("Error: Couldn't allocate memory for G.722.1 instance\n"); 1104 exit(0); 1105 } 1106 if (enc_frameSize==320) { 1107 } else { 1108 printf("\nError: G722.1 only supports 20 ms!!\n\n"); 1109 exit(0); 1110 } 1111 WebRtcG7221_EncoderInit32((G722_1_32_encinst_t*)G722_1_32enc_inst[k]); 1112 } else { 1113 printf("\nError - G722.1 is only developed for 16kHz \n"); 1114 exit(0); 1115 } 1116 break; 1117#endif 1118#ifdef CODEC_G722_1C_24 1119 case webrtc::kDecoderG722_1C_24 : 1120 if (sampfreq==32000) { 1121 ok=WebRtcG7221C_CreateEnc24(&G722_1C_24enc_inst[k]); 1122 if (ok!=0) { 1123 printf("Error: Couldn't allocate memory for G.722.1C instance\n"); 1124 exit(0); 1125 } 1126 if (enc_frameSize==640) { 1127 } else { 1128 printf("\nError: G722.1 C only supports 20 ms!!\n\n"); 1129 exit(0); 1130 } 1131 WebRtcG7221C_EncoderInit24((G722_1C_24_encinst_t*)G722_1C_24enc_inst[k]); 1132 } else { 1133 printf("\nError - G722.1 C is only developed for 32kHz \n"); 1134 exit(0); 1135 } 1136 break; 1137#endif 1138#ifdef CODEC_G722_1C_32 1139 case webrtc::kDecoderG722_1C_32 : 1140 if (sampfreq==32000) { 1141 ok=WebRtcG7221C_CreateEnc32(&G722_1C_32enc_inst[k]); 1142 if (ok!=0) { 1143 printf("Error: Couldn't allocate memory for G.722.1C instance\n"); 1144 exit(0); 1145 } 1146 if (enc_frameSize==640) { 1147 } else { 1148 printf("\nError: G722.1 C only supports 20 ms!!\n\n"); 1149 exit(0); 1150 } 1151 WebRtcG7221C_EncoderInit32((G722_1C_32_encinst_t*)G722_1C_32enc_inst[k]); 1152 } else { 1153 printf("\nError - G722.1 C is only developed for 32kHz \n"); 1154 exit(0); 1155 } 1156 break; 1157#endif 1158#ifdef CODEC_G722_1C_48 1159 case webrtc::kDecoderG722_1C_48 : 1160 if (sampfreq==32000) { 1161 ok=WebRtcG7221C_CreateEnc48(&G722_1C_48enc_inst[k]); 1162 if (ok!=0) { 1163 printf("Error: Couldn't allocate memory for G.722.1C instance\n"); 1164 exit(0); 1165 } 1166 if (enc_frameSize==640) { 1167 } else { 1168 printf("\nError: G722.1 C only supports 20 ms!!\n\n"); 1169 exit(0); 1170 } 1171 WebRtcG7221C_EncoderInit48((G722_1C_48_encinst_t*)G722_1C_48enc_inst[k]); 1172 } else { 1173 printf("\nError - G722.1 C is only developed for 32kHz \n"); 1174 exit(0); 1175 } 1176 break; 1177#endif 1178#ifdef CODEC_G722 1179 case webrtc::kDecoderG722 : 1180 if (sampfreq==16000) { 1181 if (enc_frameSize%2==0) { 1182 } else { 1183 printf("\nError - g722 frames must have an even number of enc_frameSize\n"); 1184 exit(0); 1185 } 1186 WebRtcG722_CreateEncoder(&g722EncState[k]); 1187 WebRtcG722_EncoderInit(g722EncState[k]); 1188 } else { 1189 printf("\nError - g722 is only developed for 16kHz \n"); 1190 exit(0); 1191 } 1192 break; 1193#endif 1194#ifdef CODEC_AMR 1195 case webrtc::kDecoderAMR : 1196 if (sampfreq==8000) { 1197 ok=WebRtcAmr_CreateEnc(&AMRenc_inst[k]); 1198 if (ok!=0) { 1199 printf("Error: Couldn't allocate memory for AMR encoding instance\n"); 1200 exit(0); 1201 }if ((enc_frameSize==160)||(enc_frameSize==320)||(enc_frameSize==480)) { 1202 } else { 1203 printf("\nError - AMR must have a multiple of 160 enc_frameSize\n"); 1204 exit(0); 1205 } 1206 WebRtcAmr_EncoderInit(AMRenc_inst[k], vad); 1207 WebRtcAmr_EncodeBitmode(AMRenc_inst[k], AMRBandwidthEfficient); 1208 AMR_bitrate = bitrate; 1209 } else { 1210 printf("\nError - AMR is only developed for 8kHz \n"); 1211 exit(0); 1212 } 1213 break; 1214#endif 1215#ifdef CODEC_AMRWB 1216 case webrtc::kDecoderAMRWB : 1217 if (sampfreq==16000) { 1218 ok=WebRtcAmrWb_CreateEnc(&AMRWBenc_inst[k]); 1219 if (ok!=0) { 1220 printf("Error: Couldn't allocate memory for AMRWB encoding instance\n"); 1221 exit(0); 1222 } 1223 if (((enc_frameSize/320)<0)||((enc_frameSize/320)>3)||((enc_frameSize%320)!=0)) { 1224 printf("\nError - AMRwb must have frameSize of 20, 40 or 60ms\n"); 1225 exit(0); 1226 } 1227 WebRtcAmrWb_EncoderInit(AMRWBenc_inst[k], vad); 1228 if (bitrate==7000) { 1229 AMRWB_bitrate = AMRWB_MODE_7k; 1230 } else if (bitrate==9000) { 1231 AMRWB_bitrate = AMRWB_MODE_9k; 1232 } else if (bitrate==12000) { 1233 AMRWB_bitrate = AMRWB_MODE_12k; 1234 } else if (bitrate==14000) { 1235 AMRWB_bitrate = AMRWB_MODE_14k; 1236 } else if (bitrate==16000) { 1237 AMRWB_bitrate = AMRWB_MODE_16k; 1238 } else if (bitrate==18000) { 1239 AMRWB_bitrate = AMRWB_MODE_18k; 1240 } else if (bitrate==20000) { 1241 AMRWB_bitrate = AMRWB_MODE_20k; 1242 } else if (bitrate==23000) { 1243 AMRWB_bitrate = AMRWB_MODE_23k; 1244 } else if (bitrate==24000) { 1245 AMRWB_bitrate = AMRWB_MODE_24k; 1246 } 1247 WebRtcAmrWb_EncodeBitmode(AMRWBenc_inst[k], AMRBandwidthEfficient); 1248 1249 } else { 1250 printf("\nError - AMRwb is only developed for 16kHz \n"); 1251 exit(0); 1252 } 1253 break; 1254#endif 1255#ifdef CODEC_ILBC 1256 case webrtc::kDecoderILBC : 1257 if (sampfreq==8000) { 1258 ok=WebRtcIlbcfix_EncoderCreate(&iLBCenc_inst[k]); 1259 if (ok!=0) { 1260 printf("Error: Couldn't allocate memory for iLBC encoding instance\n"); 1261 exit(0); 1262 } 1263 if ((enc_frameSize==160)||(enc_frameSize==240)||(enc_frameSize==320)||(enc_frameSize==480)) { 1264 } else { 1265 printf("\nError - iLBC only supports 160, 240, 320 and 480 enc_frameSize (20, 30, 40 and 60 ms)\n"); 1266 exit(0); 1267 } 1268 if ((enc_frameSize==160)||(enc_frameSize==320)) { 1269 /* 20 ms version */ 1270 WebRtcIlbcfix_EncoderInit(iLBCenc_inst[k], 20); 1271 } else { 1272 /* 30 ms version */ 1273 WebRtcIlbcfix_EncoderInit(iLBCenc_inst[k], 30); 1274 } 1275 } else { 1276 printf("\nError - iLBC is only developed for 8kHz \n"); 1277 exit(0); 1278 } 1279 break; 1280#endif 1281#ifdef CODEC_ISAC 1282 case webrtc::kDecoderISAC: 1283 if (sampfreq==16000) { 1284 ok=WebRtcIsac_Create(&ISAC_inst[k]); 1285 if (ok!=0) { 1286 printf("Error: Couldn't allocate memory for iSAC instance\n"); 1287 exit(0); 1288 }if ((enc_frameSize==480)||(enc_frameSize==960)) { 1289 } else { 1290 printf("\nError - iSAC only supports frameSize (30 and 60 ms)\n"); 1291 exit(0); 1292 } 1293 WebRtcIsac_EncoderInit(ISAC_inst[k],1); 1294 if ((bitrate<10000)||(bitrate>32000)) { 1295 printf("\nError - iSAC bitrate has to be between 10000 and 32000 bps (not %i)\n", bitrate); 1296 exit(0); 1297 } 1298 WebRtcIsac_Control(ISAC_inst[k], bitrate, enc_frameSize>>4); 1299 } else { 1300 printf("\nError - iSAC only supports 480 or 960 enc_frameSize (30 or 60 ms)\n"); 1301 exit(0); 1302 } 1303 break; 1304#endif 1305#ifdef NETEQ_ISACFIX_CODEC 1306 case webrtc::kDecoderISAC: 1307 if (sampfreq==16000) { 1308 ok=WebRtcIsacfix_Create(&ISAC_inst[k]); 1309 if (ok!=0) { 1310 printf("Error: Couldn't allocate memory for iSAC instance\n"); 1311 exit(0); 1312 }if ((enc_frameSize==480)||(enc_frameSize==960)) { 1313 } else { 1314 printf("\nError - iSAC only supports frameSize (30 and 60 ms)\n"); 1315 exit(0); 1316 } 1317 WebRtcIsacfix_EncoderInit(ISAC_inst[k],1); 1318 if ((bitrate<10000)||(bitrate>32000)) { 1319 printf("\nError - iSAC bitrate has to be between 10000 and 32000 bps (not %i)\n", bitrate); 1320 exit(0); 1321 } 1322 WebRtcIsacfix_Control(ISAC_inst[k], bitrate, enc_frameSize>>4); 1323 } else { 1324 printf("\nError - iSAC only supports 480 or 960 enc_frameSize (30 or 60 ms)\n"); 1325 exit(0); 1326 } 1327 break; 1328#endif 1329#ifdef CODEC_ISAC_SWB 1330 case webrtc::kDecoderISACswb: 1331 if (sampfreq==32000) { 1332 ok=WebRtcIsac_Create(&ISACSWB_inst[k]); 1333 if (ok!=0) { 1334 printf("Error: Couldn't allocate memory for iSAC SWB instance\n"); 1335 exit(0); 1336 }if (enc_frameSize==960) { 1337 } else { 1338 printf("\nError - iSAC SWB only supports frameSize 30 ms\n"); 1339 exit(0); 1340 } 1341 ok = WebRtcIsac_SetEncSampRate(ISACSWB_inst[k], 32000); 1342 if (ok!=0) { 1343 printf("Error: Couldn't set sample rate for iSAC SWB instance\n"); 1344 exit(0); 1345 } 1346 WebRtcIsac_EncoderInit(ISACSWB_inst[k],1); 1347 if ((bitrate<32000)||(bitrate>56000)) { 1348 printf("\nError - iSAC SWB bitrate has to be between 32000 and 56000 bps (not %i)\n", bitrate); 1349 exit(0); 1350 } 1351 WebRtcIsac_Control(ISACSWB_inst[k], bitrate, enc_frameSize>>5); 1352 } else { 1353 printf("\nError - iSAC SWB only supports 960 enc_frameSize (30 ms)\n"); 1354 exit(0); 1355 } 1356 break; 1357#endif 1358#ifdef CODEC_GSMFR 1359 case webrtc::kDecoderGSMFR: 1360 if (sampfreq==8000) { 1361 ok=WebRtcGSMFR_CreateEnc(&GSMFRenc_inst[k]); 1362 if (ok!=0) { 1363 printf("Error: Couldn't allocate memory for GSM FR encoding instance\n"); 1364 exit(0); 1365 } 1366 if ((enc_frameSize==160)||(enc_frameSize==320)||(enc_frameSize==480)) { 1367 } else { 1368 printf("\nError - GSM FR must have a multiple of 160 enc_frameSize\n"); 1369 exit(0); 1370 } 1371 WebRtcGSMFR_EncoderInit(GSMFRenc_inst[k], 0); 1372 } else { 1373 printf("\nError - GSM FR is only developed for 8kHz \n"); 1374 exit(0); 1375 } 1376 break; 1377#endif 1378 default : 1379 printf("Error: unknown codec in call to NetEQTest_init_coders.\n"); 1380 exit(0); 1381 break; 1382 } 1383 1384 if (ok != 0) { 1385 return(ok); 1386 } 1387 } // end for 1388 1389 return(0); 1390} 1391 1392 1393 1394 1395int NetEQTest_free_coders(webrtc::NetEqDecoder coder, int numChannels) { 1396 1397 for (int k = 0; k < numChannels; k++) 1398 { 1399 WebRtcVad_Free(VAD_inst[k]); 1400#if (defined(CODEC_CNGCODEC8) || defined(CODEC_CNGCODEC16) || \ 1401 defined(CODEC_CNGCODEC32) || defined(CODEC_CNGCODEC48)) 1402 WebRtcCng_FreeEnc(CNGenc_inst[k]); 1403#endif 1404 1405 switch (coder) 1406 { 1407#ifdef CODEC_PCM16B 1408 case webrtc::kDecoderPCM16B : 1409#endif 1410#ifdef CODEC_PCM16B_WB 1411 case webrtc::kDecoderPCM16Bwb : 1412#endif 1413#ifdef CODEC_PCM16B_32KHZ 1414 case webrtc::kDecoderPCM16Bswb32kHz : 1415#endif 1416#ifdef CODEC_PCM16B_48KHZ 1417 case webrtc::kDecoderPCM16Bswb48kHz : 1418#endif 1419#ifdef CODEC_G711 1420 case webrtc::kDecoderPCMu : 1421 case webrtc::kDecoderPCMa : 1422#endif 1423 // do nothing 1424 break; 1425#ifdef CODEC_G729 1426 case webrtc::kDecoderG729: 1427 WebRtcG729_FreeEnc(G729enc_inst[k]); 1428 break; 1429#endif 1430#ifdef CODEC_G729_1 1431 case webrtc::kDecoderG729_1: 1432 WebRtcG7291_Free(G729_1_inst[k]); 1433 break; 1434#endif 1435#ifdef CODEC_SPEEX_8 1436 case webrtc::kDecoderSPEEX_8 : 1437 WebRtcSpeex_FreeEnc(SPEEX8enc_inst[k]); 1438 break; 1439#endif 1440#ifdef CODEC_SPEEX_16 1441 case webrtc::kDecoderSPEEX_16 : 1442 WebRtcSpeex_FreeEnc(SPEEX16enc_inst[k]); 1443 break; 1444#endif 1445#ifdef CODEC_CELT_32 1446 case webrtc::kDecoderCELT_32 : 1447 WebRtcCelt_FreeEnc(CELT32enc_inst[k]); 1448 break; 1449#endif 1450 1451#ifdef CODEC_G722_1_16 1452 case webrtc::kDecoderG722_1_16 : 1453 WebRtcG7221_FreeEnc16(G722_1_16enc_inst[k]); 1454 break; 1455#endif 1456#ifdef CODEC_G722_1_24 1457 case webrtc::kDecoderG722_1_24 : 1458 WebRtcG7221_FreeEnc24(G722_1_24enc_inst[k]); 1459 break; 1460#endif 1461#ifdef CODEC_G722_1_32 1462 case webrtc::kDecoderG722_1_32 : 1463 WebRtcG7221_FreeEnc32(G722_1_32enc_inst[k]); 1464 break; 1465#endif 1466#ifdef CODEC_G722_1C_24 1467 case webrtc::kDecoderG722_1C_24 : 1468 WebRtcG7221C_FreeEnc24(G722_1C_24enc_inst[k]); 1469 break; 1470#endif 1471#ifdef CODEC_G722_1C_32 1472 case webrtc::kDecoderG722_1C_32 : 1473 WebRtcG7221C_FreeEnc32(G722_1C_32enc_inst[k]); 1474 break; 1475#endif 1476#ifdef CODEC_G722_1C_48 1477 case webrtc::kDecoderG722_1C_48 : 1478 WebRtcG7221C_FreeEnc48(G722_1C_48enc_inst[k]); 1479 break; 1480#endif 1481#ifdef CODEC_G722 1482 case webrtc::kDecoderG722 : 1483 WebRtcG722_FreeEncoder(g722EncState[k]); 1484 break; 1485#endif 1486#ifdef CODEC_AMR 1487 case webrtc::kDecoderAMR : 1488 WebRtcAmr_FreeEnc(AMRenc_inst[k]); 1489 break; 1490#endif 1491#ifdef CODEC_AMRWB 1492 case webrtc::kDecoderAMRWB : 1493 WebRtcAmrWb_FreeEnc(AMRWBenc_inst[k]); 1494 break; 1495#endif 1496#ifdef CODEC_ILBC 1497 case webrtc::kDecoderILBC : 1498 WebRtcIlbcfix_EncoderFree(iLBCenc_inst[k]); 1499 break; 1500#endif 1501#ifdef CODEC_ISAC 1502 case webrtc::kDecoderISAC: 1503 WebRtcIsac_Free(ISAC_inst[k]); 1504 break; 1505#endif 1506#ifdef NETEQ_ISACFIX_CODEC 1507 case webrtc::kDecoderISAC: 1508 WebRtcIsacfix_Free(ISAC_inst[k]); 1509 break; 1510#endif 1511#ifdef CODEC_ISAC_SWB 1512 case webrtc::kDecoderISACswb: 1513 WebRtcIsac_Free(ISACSWB_inst[k]); 1514 break; 1515#endif 1516#ifdef CODEC_GSMFR 1517 case webrtc::kDecoderGSMFR: 1518 WebRtcGSMFR_FreeEnc(GSMFRenc_inst[k]); 1519 break; 1520#endif 1521 default : 1522 printf("Error: unknown codec in call to NetEQTest_init_coders.\n"); 1523 exit(0); 1524 break; 1525 } 1526 } 1527 1528 return(0); 1529} 1530 1531 1532 1533 1534 1535 1536int NetEQTest_encode(int coder, int16_t *indata, int frameLen, unsigned char * encoded,int sampleRate , 1537 int * vad, int useVAD, int bitrate, int numChannels){ 1538 1539 short cdlen = 0; 1540 int16_t *tempdata; 1541 static int first_cng=1; 1542 int16_t tempLen; 1543 1544 *vad =1; 1545 1546 // check VAD first 1547 if(useVAD) 1548 { 1549 *vad = 0; 1550 1551 for (int k = 0; k < numChannels; k++) 1552 { 1553 tempLen = frameLen; 1554 tempdata = &indata[k*frameLen]; 1555 int localVad=0; 1556 /* Partition the signal and test each chunk for VAD. 1557 All chunks must be VAD=0 to produce a total VAD=0. */ 1558 while (tempLen >= 10*sampleRate/1000) { 1559 if ((tempLen % 30*sampleRate/1000) == 0) { // tempLen is multiple of 30ms 1560 localVad |= WebRtcVad_Process(VAD_inst[k] ,sampleRate, tempdata, 30*sampleRate/1000); 1561 tempdata += 30*sampleRate/1000; 1562 tempLen -= 30*sampleRate/1000; 1563 } 1564 else if (tempLen >= 20*sampleRate/1000) { // tempLen >= 20ms 1565 localVad |= WebRtcVad_Process(VAD_inst[k] ,sampleRate, tempdata, 20*sampleRate/1000); 1566 tempdata += 20*sampleRate/1000; 1567 tempLen -= 20*sampleRate/1000; 1568 } 1569 else { // use 10ms 1570 localVad |= WebRtcVad_Process(VAD_inst[k] ,sampleRate, tempdata, 10*sampleRate/1000); 1571 tempdata += 10*sampleRate/1000; 1572 tempLen -= 10*sampleRate/1000; 1573 } 1574 } 1575 1576 // aggregate all VAD decisions over all channels 1577 *vad |= localVad; 1578 } 1579 1580 if(!*vad){ 1581 // all channels are silent 1582 cdlen = 0; 1583 for (int k = 0; k < numChannels; k++) 1584 { 1585 WebRtcCng_Encode(CNGenc_inst[k],&indata[k*frameLen], (frameLen <= 640 ? frameLen : 640) /* max 640 */, 1586 encoded,&tempLen,first_cng); 1587 encoded += tempLen; 1588 cdlen += tempLen; 1589 } 1590 *vad=0; 1591 first_cng=0; 1592 return(cdlen); 1593 } 1594 } 1595 1596 1597 // loop over all channels 1598 int totalLen = 0; 1599 1600 for (int k = 0; k < numChannels; k++) 1601 { 1602 /* Encode with the selected coder type */ 1603 if (coder==webrtc::kDecoderPCMu) { /*g711 u-law */ 1604#ifdef CODEC_G711 1605 cdlen = WebRtcG711_EncodeU(G711state[k], indata, frameLen, (int16_t*) encoded); 1606#endif 1607 } 1608 else if (coder==webrtc::kDecoderPCMa) { /*g711 A-law */ 1609#ifdef CODEC_G711 1610 cdlen = WebRtcG711_EncodeA(G711state[k], indata, frameLen, (int16_t*) encoded); 1611 } 1612#endif 1613#ifdef CODEC_PCM16B 1614 else if ((coder==webrtc::kDecoderPCM16B)||(coder==webrtc::kDecoderPCM16Bwb)|| 1615 (coder==webrtc::kDecoderPCM16Bswb32kHz)||(coder==webrtc::kDecoderPCM16Bswb48kHz)) { /*pcm16b (8kHz, 16kHz, 32kHz or 48kHz) */ 1616 cdlen = WebRtcPcm16b_EncodeW16(indata, frameLen, (int16_t*) encoded); 1617 } 1618#endif 1619#ifdef CODEC_G722 1620 else if (coder==webrtc::kDecoderG722) { /*g722 */ 1621 cdlen=WebRtcG722_Encode(g722EncState[k], indata, frameLen, (int16_t*)encoded); 1622 assert(cdlen == frameLen>>1); 1623 } 1624#endif 1625#ifdef CODEC_ILBC 1626 else if (coder==webrtc::kDecoderILBC) { /*iLBC */ 1627 cdlen=WebRtcIlbcfix_Encode(iLBCenc_inst[k], indata,frameLen,(int16_t*)encoded); 1628 } 1629#endif 1630#if (defined(CODEC_ISAC) || defined(NETEQ_ISACFIX_CODEC)) // TODO(hlundin): remove all NETEQ_ISACFIX_CODEC 1631 else if (coder==webrtc::kDecoderISAC) { /*iSAC */ 1632 int noOfCalls=0; 1633 cdlen=0; 1634 while (cdlen<=0) { 1635#ifdef CODEC_ISAC /* floating point */ 1636 cdlen = WebRtcIsac_Encode(ISAC_inst[k], 1637 &indata[noOfCalls * 160], 1638 encoded); 1639#else /* fixed point */ 1640 cdlen = WebRtcIsacfix_Encode(ISAC_inst[k], 1641 &indata[noOfCalls * 160], 1642 encoded); 1643#endif 1644 noOfCalls++; 1645 } 1646 } 1647#endif 1648#ifdef CODEC_ISAC_SWB 1649 else if (coder==webrtc::kDecoderISACswb) { /* iSAC SWB */ 1650 int noOfCalls=0; 1651 cdlen=0; 1652 while (cdlen<=0) { 1653 cdlen = WebRtcIsac_Encode(ISACSWB_inst[k], 1654 &indata[noOfCalls * 320], 1655 encoded); 1656 noOfCalls++; 1657 } 1658 } 1659#endif 1660#ifdef CODEC_CELT_32 1661 else if (coder==webrtc::kDecoderCELT_32) { /* Celt */ 1662 int encodedLen = 0; 1663 cdlen = 0; 1664 while (cdlen <= 0) { 1665 cdlen = WebRtcCelt_Encode(CELT32enc_inst[k], &indata[encodedLen], encoded); 1666 encodedLen += 10*32; /* 10 ms */ 1667 } 1668 if( (encodedLen != frameLen) || cdlen < 0) { 1669 printf("Error encoding Celt frame!\n"); 1670 exit(0); 1671 } 1672 } 1673#endif 1674 1675 indata += frameLen; 1676 encoded += cdlen; 1677 totalLen += cdlen; 1678 1679 } // end for 1680 1681 first_cng=1; 1682 return(totalLen); 1683} 1684 1685 1686 1687void makeRTPheader(unsigned char* rtp_data, int payloadType, int seqNo, uint32_t timestamp, uint32_t ssrc){ 1688 1689 rtp_data[0]=(unsigned char)0x80; 1690 rtp_data[1]=(unsigned char)(payloadType & 0xFF); 1691 rtp_data[2]=(unsigned char)((seqNo>>8)&0xFF); 1692 rtp_data[3]=(unsigned char)((seqNo)&0xFF); 1693 rtp_data[4]=(unsigned char)((timestamp>>24)&0xFF); 1694 rtp_data[5]=(unsigned char)((timestamp>>16)&0xFF); 1695 1696 rtp_data[6]=(unsigned char)((timestamp>>8)&0xFF); 1697 rtp_data[7]=(unsigned char)(timestamp & 0xFF); 1698 1699 rtp_data[8]=(unsigned char)((ssrc>>24)&0xFF); 1700 rtp_data[9]=(unsigned char)((ssrc>>16)&0xFF); 1701 1702 rtp_data[10]=(unsigned char)((ssrc>>8)&0xFF); 1703 rtp_data[11]=(unsigned char)(ssrc & 0xFF); 1704} 1705 1706 1707int makeRedundantHeader(unsigned char* rtp_data, int *payloadType, int numPayloads, uint32_t *timestamp, uint16_t *blockLen, 1708 int seqNo, uint32_t ssrc) 1709{ 1710 1711 int i; 1712 unsigned char *rtpPointer; 1713 uint16_t offset; 1714 1715 /* first create "standard" RTP header */ 1716 makeRTPheader(rtp_data, NETEQ_CODEC_RED_PT, seqNo, timestamp[numPayloads-1], ssrc); 1717 1718 rtpPointer = &rtp_data[12]; 1719 1720 /* add one sub-header for each redundant payload (not the primary) */ 1721 for(i=0; i<numPayloads-1; i++) { /* |0 1 2 3 4 5 6 7| */ 1722 if(blockLen[i] > 0) { 1723 offset = (uint16_t) (timestamp[numPayloads-1] - timestamp[i]); 1724 1725 rtpPointer[0] = (unsigned char) ( 0x80 | (0x7F & payloadType[i]) ); /* |F| block PT | */ 1726 rtpPointer[1] = (unsigned char) ((offset >> 6) & 0xFF); /* | timestamp- | */ 1727 rtpPointer[2] = (unsigned char) ( ((offset & 0x3F)<<2) | 1728 ( (blockLen[i]>>8) & 0x03 ) ); /* | -offset |bl-| */ 1729 rtpPointer[3] = (unsigned char) ( blockLen[i] & 0xFF ); /* | -ock length | */ 1730 1731 rtpPointer += 4; 1732 } 1733 } 1734 1735 /* last sub-header */ 1736 rtpPointer[0]= (unsigned char) (0x00 | (0x7F&payloadType[numPayloads-1]));/* |F| block PT | */ 1737 rtpPointer += 1; 1738 1739 return(rtpPointer - rtp_data); /* length of header in bytes */ 1740} 1741 1742 1743 1744int makeDTMFpayload(unsigned char* payload_data, int Event, int End, int Volume, int Duration) { 1745 unsigned char E,R,V; 1746 R=0; 1747 V=(unsigned char)Volume; 1748 if (End==0) { 1749 E = 0x00; 1750 } else { 1751 E = 0x80; 1752 } 1753 payload_data[0]=(unsigned char)Event; 1754 payload_data[1]=(unsigned char)(E|R|V); 1755 //Duration equals 8 times time_ms, default is 8000 Hz. 1756 payload_data[2]=(unsigned char)((Duration>>8)&0xFF); 1757 payload_data[3]=(unsigned char)(Duration&0xFF); 1758 return(4); 1759} 1760 1761void stereoDeInterleave(int16_t* audioSamples, int numSamples) 1762{ 1763 1764 int16_t *tempVec; 1765 int16_t *readPtr, *writeL, *writeR; 1766 1767 if (numSamples <= 0) 1768 return; 1769 1770 tempVec = (int16_t *) malloc(sizeof(int16_t) * numSamples); 1771 if (tempVec == NULL) { 1772 printf("Error allocating memory\n"); 1773 exit(0); 1774 } 1775 1776 memcpy(tempVec, audioSamples, numSamples*sizeof(int16_t)); 1777 1778 writeL = audioSamples; 1779 writeR = &audioSamples[numSamples/2]; 1780 readPtr = tempVec; 1781 1782 for (int k = 0; k < numSamples; k += 2) 1783 { 1784 *writeL = *readPtr; 1785 readPtr++; 1786 *writeR = *readPtr; 1787 readPtr++; 1788 writeL++; 1789 writeR++; 1790 } 1791 1792 free(tempVec); 1793 1794} 1795 1796 1797void stereoInterleave(unsigned char* data, int dataLen, int stride) 1798{ 1799 1800 unsigned char *ptrL, *ptrR; 1801 unsigned char temp[10]; 1802 1803 if (stride > 10) 1804 { 1805 exit(0); 1806 } 1807 1808 if (dataLen%1 != 0) 1809 { 1810 // must be even number of samples 1811 printf("Error: cannot interleave odd sample number\n"); 1812 exit(0); 1813 } 1814 1815 ptrL = data + stride; 1816 ptrR = &data[dataLen/2]; 1817 1818 while (ptrL < ptrR) { 1819 // copy from right pointer to temp 1820 memcpy(temp, ptrR, stride); 1821 1822 // shift data between pointers 1823 memmove(ptrL + stride, ptrL, ptrR - ptrL); 1824 1825 // copy from temp to left pointer 1826 memcpy(ptrL, temp, stride); 1827 1828 // advance pointers 1829 ptrL += stride*2; 1830 ptrR += stride; 1831 } 1832 1833} 1834