G729 codec call

When G729 encodes, it can only encode 160 bytes at a time, and the size after encoding is 10 bytes, and the compression ratio is fixed at 16:1.
The following code has a premise. The input raw audio data has a sampling rate of 16k, a bit depth of 16, a single channel, and a frame is sent in 20ms. Then one frame of audio data is 16k x 16 / 8 / (1000 / 20) = 640 bytes. After resampling to 8k, one frame is 320 bytes.
Because only 160 bytes can be encoded at a time, encoding 320 bytes of data needs to be done twice, so audio_encode_g729 and audio_decode_g729 need to be called twice as shown below.

//编码
if (rl_strcasecmp("G729", enc_name) == 0)
{
    
    
	unsigned char pcm_out[1500];
	unsigned char rec_buf[320];
	audio_resample_16k_to_8k(rec_8_buf, rec_16_buf, 640);
	audio_encode_g729((short*)rec_8_buf, 80, (pcm_out + 12));//RTP 头长度12
	audio_encode_g729((short*)(rec_8_buf + 160), 80, (pcm_out + 12 + 10));//编码后为10字节
}

//解码
if(rl_strcmp("G729", dec_name) == 0)
{
    
    
	audio_decode_g729(encode_buf, 10, (short *)(play_8_buf),(int*)&speechType);
	audio_decode_g729((encode_buf + 10), 10, (short *)(play_8_buf+ 160),(int*)&speechType);
	audio_resample_8k_to_16k(play_16_buf, play_8_buf, 320);
}

Guess you like

Origin blog.csdn.net/csdn_zmf/article/details/125646962