G711 codec (G711 with PCM type) __g711

Source: Internet
Author: User
Tags fread

Summary of the online G711 codec, it has been to the Andorid system, through the JNI to G711 and PCM, because of the two formats do not have in-depth understanding, so if you want to understand them can Baidu, here is just to achieve such a function.

G711.C contains encoding and decoding methods

http://www.easydarwin.org/article/Streaming/38.html Reference Article//HTTPS://GITHUB.COM/EASYDARWIN/EASYAACENCODER/BL Ob/master/g711.cpp//http://www.oschina.net/code/snippet_1173523_38946//http://blog.csdn.net/rightorwrong/
article/details/4209467 PCM 2 G711 G711 2 pcm unsigned char encode (short PCM);

Short decode (unsigned char alaw); /** Personal Understanding * bitsize should be, pbuffer (PCM data) two char synthesis of a short, the length of nature is the original half (NBUFFERSIZE/2), * by encoding the short type of data into a char type, so that after copying to P codecbits */int g711_encode (unsigned char* pcodecbits, const char* pbuffer, int nbuffersize) {short* buffer = (short*)
	pbuffer;
	int i;
	For (i=0 i<nbuffersize/2; i++) {pcodecbits[i] = encode (buffer[i));
return NBUFFERSIZE/2; /** Personal Understanding * bitsize should be, PCM array width changed to twice times (short *out_data = (short*) prawdata;), * convert char decoding to pbuffer (g711 data) to two bytes sho RT, after copying to the Out_data array, and then to the char type when used, the decoded data is the original twice times (nbuffersize*2)/int G711_decode (char* prawdata, const unsigned cha r* pbuffer, int nbuffersize) {short *out_datA = (short*) prawdata;
	int i;
	For (i=0 i<nbuffersize; i++) {Out_data[i] = decode (pbuffer[i));
return nbuffersize*2;
    #define MAX 32635 unsigned char encode (short PCM) {int sign = (PCM & 0x8000) >> 8;
    if (sign!= 0) PCM =-PCM;
    if (PCM > Max) PCM = MAX;
    int exponent = 7;
    int expmask; for (expmask = 0x4000; (PCM & expmask) = = 0 && exponent>0;
    Exponent--, Expmask >>= 1) {} int mantissa = (PCM >> ((exponent = = 0) 4: (exponent + 3))) & 0x0f;
    unsigned char Alaw = (unsigned char) (sign | exponent << 4 | mantissa);
return (unsigned char) (ALAW^0XD5);
    Short decode (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);
 }

Implement codec in Jni, just post the main code

G711-> PCM

jniexport void Jnicall java_com_ff_aacdemo_jni_g711coder_g711topcm
  (jnienv *env, Jobject obj) {
	FILE* fpOut = fopen ("/STORAGE/EMULATED/0/T/PCM_TO_G711.PCM", "WB");
	file* Fpin = fopen ("/storage/emulated/0/t/pcm_to_g711.g711", "RB"); the//g711 format file gives the connection

	int g711_buffersize = 1024 at the end;
	Char g711_buffer[g711_buffersize];

	int Len;
	while (len = Fread (G711_buffer, 1, g711_buffersize, fpin)) > 0) {
		logd ("g711topcm length =%d", Len);
		Char Pcmbuffer[len];
		int pcmbufsize = G711_decode (Pcmbuffer, G711_buffer, Len);
		Fwrite (Pcmbuffer, 1, pcmbufsize, fpout);
	}
	LOGD ("G711TOPCM end");
	Fclose (Fpin);
	Fclose (fpout);
}

PCM-> G711

jniexport void Jnicall java_com_ff_aacdemo_jni_g711coder_pcmtog711
  (jnienv *env, Jobject obj) {
	FILE* fpIn = fopen ("/STORAGE/EMULATED/0/T/PCM_TO_G711.PCM", "RB");
	file* fpout = fopen ("/storage/emulated/0/t/pcm_to_g711.g711", "WB");

	LOGD ("pcmTog711 1");

	int pcm_buffersize = 1024;
	Char pcm_buffer[pcm_buffersize];

	int Len;
	while (len = Fread (Pcm_buffer, 1, pcm_buffersize, fpin)) > 0) {
		logd ("pcmTog711 length =%d", Len);
		Char G711buffer[len];
		int  g711bufsize = G711_encode (G711buffer, Pcm_buffer, Len);
		Fwrite (G711buffer, 1, g711bufsize, fpout);
	}
	LOGD ("pcmTog711 end");
	Fclose (Fpin);
	Fclose (fpout);
}

Although able to achieve their mutual conversion, but, not very good to find the player playback, the effect is always not validated, in the next chapter, will summarize the G711 converted to AAC, AAC This format will be able to use the regular player to play

g711 file http://download.csdn.net/detail/yuanzhenhai/2693878

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.