g.711 Coding principle and code

Source: Internet
Author: User

Recently read the voice coding, found that most of the online only gave the G711 code, does not introduce the principle, although very simple, but directly see the code is a little touch. Here is a brief description of the principle and the code found on the Web.

1. Introduction:

G.711

also known asPCM(Pulse code modulation), is the International Telecommunication Union set out a set of speech compression standards, mainly for the telephone. It mainly uses the Pulse code modulation to the audio sampling, the sampling rate is8kper second. It uses a64KbpsThe uncompressed channel transmits voice signals. the compression rate is1:2,that the -bit data is compressed into8bit. g.711is the mainstream waveform sound codec.
G.711There are two main kinds of compression algorithms under the standard. One is Μ-law algorithm (also known as often u-law, Ulaw, Mu-law), mainly used in North America and Japan, the other is A-law algorithm, mainly used in Europe and other parts of the world. Among them, the latter is specially designed to facilitate computer processing. Both of these algorithms use a sample rate of 8kHz input to create a digital output of 64Kbps. G.711 uses a technique called packet loss concealment (PLC) to reduce the actual impact of packet drops. Effective signal bandwidth is reduced by the process of voice activity detection (VAD) during silence.

2. Principle Overview:

g.711 the encoding method will 14bits (with 16bits sampling and storage). PCM The signal is encoded into 8bits of the sample to be transmitted.

principle: Take the most important bits encoded into 8 bits (reserved for important bits), such as the data below, before N more influential, (the following 2 Group data, n=5 )

0000 0001 1111 1111 (511)

0000 0001 1111 0000 (496)

0001 1111 1111 1111 (8191)

0001 1111 0000 0000 (7936)

3.alaw Encoding Rules:

Take the most influential 5-bit (1-bit is the intensity bit, 4-bit sample bit), sign is the symbol bit of the sample, and the encoded data is the number of even-numbered complement. That is ^0xd5. The encoding table is as follows, with the s bit sign bit positive when S=1.


such as pcm=3210 (0000 1100 1000 1010) 2

1. int sign = PCM & 0x8000) >> 8;

S=1;

2. Take the strength level

0 0001 10010001010

Xs = 100

3. Take high-level samples

0 0001 10010001010

WXYZ = 1001

4. Combine the above figures

s xxx wxyz

1 100 1001

5. Every couple of digits

1 1001001

1 0011100

4.ULAW Encoding Rules:

1. Obtain the range value, get the 8-bit base value

2. Get the interval value size. :

3. Get the area basic value RB (EX2015)

4. Calculate the distance from the base value RB D = Rb-sample

5. According to the size of the distance, calculate the translation amount S = d/size

6. Add output to 8-bit base value = B + S

7903

8159

4319

4063

4063

2143

2015

2015

1055

991

991

511

479

479

239

223

223

103

95

95

35

31

31

3

0

1

0

Example: 2345 = 0x9d

1. Get Range values

Range = 4063~2015

1-1. Get 8-bit Base value

b = 0x90

2. Get the interval value

size = 128

3. Get the basic value of the area

4063

4063

2143

RB = 4063

4. Calculate the distance d from the base value RB

D = rb–sample = 4063–2345 = 1718

5. Calculate the translation amount according to the size of the distance

s = d/size = 1718/128 = 13.42 ...

6. Add to 8-bit base value

Output = B + S = 0x90 + 9D

5. Code
encoded int Cg711decoder::g711_encode (unsigned char* pcodecbits, const char* pbuffer, int nbuffersize) {short* buffer      = (short*) pbuffer;      for (int i=0; i<nbuffersize/2; i++) {pcodecbits[i] = encode (buffer[i]);  } return NBUFFERSIZE/2; }//Decode int Cg711decoder::g711_decode (char* prawdata, const unsigned char* pbuffer, int nbuffersize) {short *ou      T_data = (short*) prawdata;      for (int i=0; i<nbuffersize; i++) {Out_data[i] = decode (pbuffer[i]);  } return nbuffersize*2;      } #define MAX (32635) unsigned char cg711decoder::encode (short PCM) {int sign = (PCM & 0x8000) >> 8;  if (sign! = 0) PCM =-PCM;  Highest level, sign bit if (PCM > max) PCM = max;      max=32635 int exponent = 7;      int expmask; for (expmask = 0x4000; (PCM & expmask) = = 0///bit14->14-7, locate the first bit in the high byte that is not 0 && exponent>0; Exponent--, Expmask >>= 1) {} int mantissa = (PCM &GT;&GT ((exponent = = 0)? 4: (exponent + 3)))      & 0x0f;      unsigned char Alaw = (unsigned char) (sign | exponent << 4 | mantissa);  return (unsigned char) (ALAW^0XD5);      11010101} short Cg711decoder::d ecode (unsigned char alaw) {alaw ^= 0xd5;      int sign = alaw & 0x80;      int exponent = (Alaw & 0x70) >> 4;      int data = Alaw & 0x0f;      Data <<= 4;      Data + = 8;      if (exponent! = 0) Data + = 0x100;            if (exponent > 1) Data <<= (exponent-1);  Return (short) (sign = = 0? data:-data);   }
Reference:Lhttp://www.itu.int/rec/dologin_pub.asp?lang=e&id=t-rec-g.711-198811-i!! Pdf-e&type=itemsLitu-tg.711 PDFLhttp://en.wikipedia.org/wiki/G.711Lwikipedia:g.711 & A-lawLHttp://en.wikipedia.org/wiki/Mu-lawLWikipedia:mu-lawLHttp://72.14.235.104/search?q=cache:CIgTx4nuxeQJ:telecom.tbi.net/pcm1.html+mu-law+Comparison+with+A-law&hl =zh-tw&ct=clnk&cd=5&gl=tw
Detailed ppt download (share from Network): http://download.csdn.net/detail/guo8113/8587203



g.711 Coding principle and code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.