/********************************************************************************************************* /* Created by Abdel Rabassa (AHR) * /* This subroutine takes as input the codec type and the number of voice frames encapsulated per packet * /* and calculates the voice payload size and the packet generation frequency (or period)for each of * /* seven encoding algorithms and modes. For AMR-NB, also a one way ETE delay must be entered * /* corresponding with the value obtained from the last RTCP packet received. * /* * /* ******************************************************************************************************* #include #include #include using namespace std; /* AMR frame sizes in bytes*/ #define AMR_122_FR_SIZE 31 #define AMR_102_FR_SIZE 26 #define AMR_795_FR_SIZE 21 #define AMR_740_FR_SIZE 19 #define AMR_670_FR_SIZE 18 #define AMR_590_FR_SIZE 16 #define AMR_515_FR_SIZE 14 #define AMR_475_FR_SIZE 13 //************************************************************/ /* Generate a random number between 0 and 1 */ /* return a uniform number in [0,1]. */ /*************************************************************/ double unifRand() { return rand() / double(RAND_MAX); } //************************************************************/ /** Generates a random number bounded by L and R **/ /*************************************************************/ double AHR_random_bounded(double L,double R) { double r1; if (L>R) //L<->R { r1 = L; L = R; R = r1; }//L always contains the smallest boundary r1 = (R-L)* unifRand() + L; //generates random number between [L, R) return r1; } /************************************************************/ /** Receives a random number U[0,1] with autocorrelation = 0**/ /** and returns a Vn random number that follows the **/ /** innovation PDF to be used to generate a random number **/ /** U[0,1] with certain autocorrelation function size that **/ /** follows the PDF */ /*************************************************************/ double AHR_Speex8_generates_Vn (double U) { double Vn; /* INNOVATION DENSITY STEP FUNCTION FOR SPEEX Q8 (18 steps) k Lk Rk Pk 1 -0.385, -0.375 0.006427 2 -0.375, -0.365 0.006427 3 -0.245, -0.235 0.00153809 4 -0.235, -0.225 0.00175781 5 -0.225, -0.215 0.00175781 6 -0.215, -0.205 0.00175781 7 -0.205, -0.195 0.00175781 8 -0.195, -0.185 0.00175781 9 -0.185, -0.175 0.00175781 10 -0.175, -0.165 0.00230713 11 -0.165, -0.155 0.00324097 12 -0.155, -0.145 0.00324097 13 -0.145, -0.135 0.00324097 14 -0.135, -0.125 0.00324097 15 -0.125, -0.115 0.00274658 16 -0.0355556, -0.0177778 0.878826 17 0.145, 0.155 0.000439453 18 0.164444, 0.171111 0.0777778 */ //Generates random numbers in the range [Ln,Rn) with probabilty Pk if (U<0.006427) //k = 1 { Vn = AHR_random_bounded(-0.385, -0.375); } else if (U>= 0.006427 && U < 0.012854 ) //k = 2 { Vn = AHR_random_bounded(-0.375, -0.365); } else if (U>= 0.012854 && U < 0.014392 ) //k = 3 { Vn = AHR_random_bounded(-0.245, -0.235); } else if (U>= 0.014392 && U < 0.01615 ) //k = 4 { Vn = AHR_random_bounded(-0.235, -0.225); } else if (U>= 0.01615 && U < 0.017908 ) //k = 5 { Vn = AHR_random_bounded(-0.225, -0.215); } else if (U>= 0.017908 && U < 0.019666 ) //k = 6 { Vn = AHR_random_bounded(-0.215, -0.205); } else if (U>= 0.019666 && U < 0.021423 ) //k = 7 { Vn = AHR_random_bounded(-0.205, -0.195); } else if (U>= 0.021423 && U < 0.023181 ) //k = 8 { Vn = AHR_random_bounded(-0.195, -0.185); } else if (U>= 0.023181 && U < 0.024939 ) //k = 9 { Vn = AHR_random_bounded(-0.185, -0.175); } else if (U>= 0.024939 && U < 0.027246 ) //k = 10 { Vn = AHR_random_bounded(-0.175, -0.165); } else if (U>= 0.027246 && U < 0.030487 ) //k = 11 { Vn = AHR_random_bounded(-0.165, -0.155); } else if (U>= 0.030487 && U < 0.033728 ) //k = 12 { Vn = AHR_random_bounded(-0.155, -0.145); } else if (U>= 0.033728 && U < 0.036969 ) //k = 13 { Vn = AHR_random_bounded(-0.145, -0.135); } else if (U>= 0.036969 && U < 0.04021 ) //k = 14 { Vn = AHR_random_bounded(-0.135, -0.125); } else if (U>= 0.04021 && U < 0.042957 ) //k = 15 { Vn = AHR_random_bounded(-0.125, -0.115); } else if (U>= 0.042957 && U < 0.921783 ) //k = 16 { Vn = AHR_random_bounded(-0.0355556, -0.0177778); } else if (U>= 0.921783 && U < 0.922222 ) //k = 17 { Vn = AHR_random_bounded( 0.145, 0.155); } else //k = 18 { Vn = AHR_random_bounded(0.164444, 0.171111); } return(Vn); } //************************************************************/ //** AHR CREATED **/ /** Receives a random number U[0,1] with autocorrelation **/ /** determined by the Innovation function and returns a frame*/ /** size that follows the PDF of Speex Q8 original data */ /*************************************************************/ double AHR_Speex8_histogram_inversion(double U) { double f_s; /* SpeexQ8 Histogram Cell Left boundary Right boundary Probability 1 4.2 6.848 0.000335627 2 17.44 20.088 0.000167813 3 20.088 22.736 0.000335627 4 28.032 30.68 0.00050344 5 30.68 33.328 0.0062091 6 33.328 35.976 0.0241651 7 35.976 38.624 0.0345696 8 38.624 41.272 0.0689713 9 41.272 43.92 0.0823964 10 43.92 46.568 0.145662 11 46.568 49.216 0.0954858 12 49.216 51.864 0.140292 13 51.864 54.512 0.194328 14 54.512 57.16 0.00922974 15 57.16 59.808 0.0768585 16 59.808 62.456 0.0583991 17 62.456 65.104 0.0355764 18 65.104 67.752 0.0248364 19 67.752 70.4 0.00167813 */ if (U<0.000335627) //Cell = 1 { f_s = AHR_random_bounded(4.2, 6.848); } else if (U>= 0.000335627 && U < 0.00050344 ) //Cell = 2 { f_s = AHR_random_bounded(17.44, 20.088); } else if (U>= 0.00050344 && U < 0.000839067 ) //Cell = 3 { f_s = AHR_random_bounded(20.088, 22.736); } else if (U>= 0.000839067 && U < 0.001342507 ) //Cell = 4 { f_s = AHR_random_bounded(28.032, 30.68); } else if (U>= 0.001342507 && U < 0.007551607 ) //Cell = 5 { f_s = AHR_random_bounded(30.68, 33.328); } else if (U>= 0.007551607 && U < 0.031716707 ) //Cell = 6 { f_s = AHR_random_bounded(33.328, 35.976); } else if (U>= 0.031716707 && U < 0.066286307 ) //Cell = 7 { f_s = AHR_random_bounded(35.976, 38.624); } else if (U>= 0.066286307 && U < 0.135257607 ) //Cell = 8 { f_s = AHR_random_bounded(38.624, 41.272); } else if (U>= 0.135257607 && U < 0.217654007 ) //Cell = 9 { f_s = AHR_random_bounded(41.272, 43.92); } else if (U>= 0.217654007 && U < 0.363316007 ) //Cell = 10 { f_s = AHR_random_bounded(43.92, 46.568); } else if (U>= 0.363316007 && U < 0.458801807 ) //Cell = 11 { f_s = AHR_random_bounded(46.568, 49.216); } else if (U>= 0.458801807 && U < 0.599093807 ) //Cell = 12 { f_s = AHR_random_bounded(49.216, 51.864); } else if (U>= 0.599093807 && U < 0.793421807 ) //Cell = 13 { f_s = AHR_random_bounded(51.864, 54.512); } else if (U>= 0.793421807 && U < 0.802651547 ) //Cell = 14 { f_s = AHR_random_bounded(54.512, 57.16); } else if (U>= 0.802651547 && U < 0.879510047 ) //Cell = 15 { f_s = AHR_random_bounded(57.16, 59.808); } else if (U>= 0.879510047 && U < 0.937909147 ) //Cell = 16 { f_s = AHR_random_bounded(59.808, 62.456); } else if (U>= 0.937909147 && U < 0.973485547 ) //Cell = 17 { f_s = AHR_random_bounded(62.456, 65.104); } else if (U>= 0.973485547 && U < 0.998321947 ) //Cell = 18 { f_s = AHR_random_bounded(65.104, 67.752); } else //Cell = 19 { f_s = AHR_random_bounded(67.752, 70.4); } return (f_s); } /************************************************************/ //** AHR CREATED **/ /** Receives a random number U[0,1] with autocorrelation = 0**/ /** and returns a Vn random number that follows the **/ /** innovation PDF to be used to generate a random number **/ /** U[0,1] with certain autocorrelation function size that **/ /** follows the PDF */ /*************************************************************/ double AHR_Speex2_generates_Vn (double U) { double Vn; /*INNOVATION DENSITY STEP FUNCTION FOR SPEEX Q2 (2 steps) k Lk Rk Pk 1 -0.035, -0.0275, 0.504235 2 0.075, 0.085, 0.495765 */ //Generates random numbers in the range [Ln,Rn) with probabilty Pk if (U<0.504235) //k = 1 { Vn = AHR_random_bounded(-0.035, -0.0275); } else { Vn = AHR_random_bounded(0.075, 0.085); } return(Vn); } //************************************************************/ //** AHR CREATED **/ /** Receives a random number U[0,1] with autocorrelation **/ /** determined by the Innovation function and returns a frame*/ /** size that follows the PDF of Speex Q2 original data */ /*************************************************************/ double AHR_Speex2_histogram_inversion(double U) { double f_s; /* Speex Q2 Histogram Cell Left boundary Right boundary Probability 1 0, 0.224, 0.000320461 2 1.448, 2.672, 0.000160231 3 5.12, 6.344, 0.000160231 4 6.344, 7.568, 0.000480692 5 8.792, 10.016, 0.004166 6 10.016, 11.24, 0.00288415 7 11.24, 12.464, 0.0365326 8 12.464, 13.688, 0.0169845 9 13.688, 14.912, 0.0620093 10 14.912, 16.136, 0.195481 11 16.136, 17.36, 0.0990226 12 17.36, 18.584, 0.0722641 13 18.584, 19.808, 0.118411 14 19.808, 21.032, 0.0922929 15 21.032, 22.256, 0.167281 16 22.256, 23.48, 0.064573 17 23.48, 24.704, 0.00176254 18 24.704, 25.928, 0.0368531 19 25.928, 27.152, 0.0190675 20 27.152, 28.376, 0.00881269 21 28.376, 29.6, 0.000480692 */ if (U<0.000320461) //Cell = 1 { f_s = AHR_random_bounded(0, 0.224); } else if (U>= 0.000320461 && U < 0.000480692 ) //Cell = 2 { f_s = AHR_random_bounded(1.448, 2.672); } else if (U>= 0.000480692 && U < 0.000640923 ) //Cell = 3 { f_s = AHR_random_bounded(5.12, 6.344); } else if (U>= 0.000640923 && U < 0.001121615 ) //Cell = 4 { f_s = AHR_random_bounded(6.344, 7.568); } else if (U>= 0.001121615 && U < 0.005287615 ) //Cell = 5 { f_s = AHR_random_bounded(8.792, 10.016); } else if (U>= 0.005287615 && U < 0.008171765 ) //Cell = 6 { f_s = AHR_random_bounded(10.016, 11.24); } else if (U>= 0.008171765 && U < 0.044704365 ) //Cell = 7 { f_s = AHR_random_bounded(11.24, 12.464); } else if (U>= 0.044704365 && U < 0.061688865 ) //Cell = 8 { f_s = AHR_random_bounded(12.464, 13.688); } else if (U>= 0.061688865 && U < 0.123698165 ) //Cell = 9 { f_s = AHR_random_bounded(14.912, 16.136); //i swapped the frma cell values for cell 9 and 10 } else if (U>= 0.123698165 && U < 0.319179165 ) //Cell = 10 { f_s = AHR_random_bounded(13.688, 14.912); } else if (U>= 0.319179165 && U < 0.418201765 ) //Cell = 11 { f_s = AHR_random_bounded(16.136, 17.36); } else if (U>= 0.418201765 && U < 0.490465865 ) //Cell = 12 { f_s = AHR_random_bounded(17.36, 18.584); } else if (U>= 0.490465865 && U < 0.608876865 ) //Cell = 13 { f_s = AHR_random_bounded(18.584, 19.808); } else if (U>= 0.608876865 && U < 0.701169765 ) //Cell = 14 { f_s = AHR_random_bounded(19.808, 21.032); } else if (U>= 0.701169765 && U < 0.868450765 ) //Cell = 15 { f_s = AHR_random_bounded(21.032, 22.256); } else if (U>= 0.868450765 && U < 0.933023765 ) //Cell = 16 { f_s = AHR_random_bounded(22.256, 23.48); } else if (U>= 0.933023765 && U < 0.934786305 ) //Cell = 17 { f_s = AHR_random_bounded(23.48, 24.704); } else if (U>= 0.934786305 && U < 0.971639405 ) //Cell = 18 { f_s = AHR_random_bounded(24.704, 25.928); } else if (U>= 0.971639405 && U < 0.990706905 ) //Cell = 19 { f_s = AHR_random_bounded(25.928, 27.152); } else if (U>= 0.990706905 && U < 0.999519595 ) //Cell = 20 { f_s = AHR_random_bounded(27.152, 28.376); } else //Cell = 21 { f_s = AHR_random_bounded(28.376, 29.6); } return (f_s); } int AMR_fr_size (double M2E_delay) { int fr_size; // voice frame size [bytes] //Set the AMR frame size depending on the M2E_delay calculated. See documentation for delay limits if (M2E_delay <= 150) fr_size = AMR_122_FR_SIZE; else if (M2E_delay>150 && M2E_delay<=164.3) fr_size = AMR_102_FR_SIZE; else if (M2E_delay>164.3 && M2E_delay<=178.6) fr_size = AMR_795_FR_SIZE; else if (M2E_delay>178.6 && M2E_delay<=192.9) fr_size = AMR_740_FR_SIZE; else if (M2E_delay>192.9 && M2E_delay<=207.2) fr_size = AMR_670_FR_SIZE; else if (M2E_delay>207.2 && M2E_delay<=221.5) fr_size = AMR_590_FR_SIZE; else if (M2E_delay>221.5 && M2E_delay<=235.8) fr_size = AMR_515_FR_SIZE; else if (M2E_delay>235.8) fr_size = AMR_475_FR_SIZE; return fr_size; } void main() { //General var declaration double fr_duration; int codec; int fr_packet; // number of voice frmaes to be encapsulated per packet int fr_size; double pkt_generation_period; //1/pkt_generation_period = pkts/sec generated of the specified codec int voice_payload_size; //Speex specific vars int Speex_quality; // Speex Quality mode. Speex Quality =8 and Quality =2 allowed. int i; double r1; double Speex_Un; //AMR-NB specific vars double ETE_delay; //One way network delay [ms] double M2E; //Mouth to ear delay [ms]. cout<<"Enter the codec type : [1:G.729, 2:G.711, 3:iLBC20, 4:iLBC30, 5:SpeexQ8, 6:SpeexQ2, 7:AMR-NB]: "; cin>>codec; cout<<"Enter the number of frames per packet: "; cin>>fr_packet; voice_payload_size =0; if (codec<=4) //Fix data rate codecs { if (codec ==1) //G.729 { fr_size = 10; //G.729 voice frame is 10 bytes long fr_duration = 0.01; //G.729 frame duration is 10ms } else if (codec ==2) //G.711 { fr_size = 80; //G.711 voice frame is 80 bytes long fr_duration = 0.01; //G.711 frame duration is 10ms } else if (codec ==3) // ILBC20ms { fr_size = 38; //iLBC 20ms voice frame is 38 bytes long fr_duration = 0.02; //iLBC 20ms frame duration is 20ms } else if (codec ==4) // iLBC30ms { fr_size = 50; //iLBC 30ms voice frame is 50 bytes long fr_duration = 0.03; //iLBC 30ms frame duration is 30ms } voice_payload_size = fr_size * fr_packet; pkt_generation_period = fr_duration * fr_packet; } else if ( (codec == 5) || (codec ==6)) //Speex { srand((unsigned)time(0)); Speex_Un = (double) rand() / (RAND_MAX+1); for (i=0; i>ETE_delay; //Calculation of Mouth-to Ear delay M2E = (20.5 * fr_packet) + 10 + ETE_delay ; voice_payload_size = AMR_fr_size (M2E)* fr_packet; pkt_generation_period = 0.02 * fr_packet; } cout<< "Voice_Payload size is: "<< voice_payload_size << " bytes\nPacket generation period is: "<