/* * Open codec. */ static pj_status_t g726_codec_open( pjmedia_codec *codec, pjmedia_codec_param *attr ) { struct g726_private *priv = (struct g726_private*) codec->codec_data; PJ_ASSERT_RETURN(codec && attr, PJ_EINVAL); PJ_ASSERT_RETURN(priv != NULL, PJ_EINVALIDOP); /* Initialize common state */ priv->vad_enabled = (attr->setting.vad != 0); priv->plc_enabled = (attr->setting.plc != 0); switch (attr->info.pt) { case PJMEDIA_RTP_PT_G726_16: priv->encode_func = g726_16_encoder; priv->decode_func = g726_16_decoder; priv->bitrate = 16000; priv->code_bits = 2; priv->code_bit_mask = 0x03; priv->encoded_frame_size = (SAMPLES_PER_FRAME * 2) / 8; break; case PJMEDIA_RTP_PT_G726_24: priv->encode_func = g726_24_encoder; priv->decode_func = g726_24_decoder; priv->bitrate = 24000; priv->code_bits = 3; priv->code_bit_mask = 0x07; priv->encoded_frame_size = (SAMPLES_PER_FRAME * 3) / 8; break; case PJMEDIA_RTP_PT_G726_32: case PJMEDIA_RTP_PT_G721: priv->encode_func = g726_32_encoder; priv->decode_func = g726_32_decoder; priv->bitrate = 32000; priv->code_bits = 4; priv->code_bit_mask = 0x0f; priv->encoded_frame_size = (SAMPLES_PER_FRAME * 4) / 8; break; case PJMEDIA_RTP_PT_G726_40: priv->encode_func = g726_40_encoder; priv->decode_func = g726_40_decoder; priv->bitrate = 40000; priv->code_bits = 5; priv->code_bit_mask = 0x1f; priv->encoded_frame_size = (SAMPLES_PER_FRAME * 5) / 8; break; default: return PJMEDIA_CODEC_EUNSUP; } g726_init_state(&priv->encoder); g726_init_state(&priv->decoder); TRACE_("G726 codec allocated: vad=%d, plc=%d, bitrate=%d", priv->vad_enabled, priv->plc_enabled, priv->bitrate); return PJ_SUCCESS; }
extern void g726_Decode(char *bitstream,unsigned char *speech) { g726_state state_ptr; int i; int in; short temp; g726_init_state(&state_ptr); for(i=0;i<120;i++) { in=(int)(((*(bitstream+i))&(char)192)>>6); temp=g726_16_decoder(in,AUDIO_ENCODING_LINEAR,&state_ptr); memcpy(speech+i*8,&temp,2); in=(int)(((*(bitstream+i))&(char)48)>>4); temp=g726_16_decoder(in,AUDIO_ENCODING_LINEAR,&state_ptr); memcpy(speech+i*8+2,&temp,2); in=(int)(((*(bitstream+i))&(char)12)>>2); temp=g726_16_decoder(in,AUDIO_ENCODING_LINEAR,&state_ptr); memcpy(speech+i*8+4,&temp,2); in=(int)(((*(bitstream+i))&(char)3)); temp=g726_16_decoder(in,AUDIO_ENCODING_LINEAR,&state_ptr); memcpy(speech+i*8+6,&temp,2); } }
extern void g726_Encode(unsigned char *speech,char *bitstream) { // g726_state state_ptr; short temp[480]; int i; g726_init_state(&state_ptr); memcpy(temp,speech,960); for(i=0;i<120;i++) { *(bitstream+i)=(((char)(g726_16_encoder(temp[i*4],AUDIO_ENCODING_LINEAR,&state_ptr)))<<6)|(((char)(g726_16_encoder(temp[i*4+1],AUDIO_ENCODING_LINEAR,&state_ptr)))<<4)|(((char)(g726_16_encoder(temp[i*4+2],AUDIO_ENCODING_LINEAR,&state_ptr)))<<2)|(((char)(g726_16_encoder(temp[i*4+3],AUDIO_ENCODING_LINEAR,&state_ptr)))); } }
void g726_Encode(unsigned char *speech,char *bitstream) { g726_state state_ptr; short temp[480]; int i; //char c; g726_init_state(&state_ptr); /*for(i=0;i<480;i++) { c=*(speech+i*2); *(speech+i*2)=*(speech+i*2+1); *(speech+i*2+1)=c; }*/ memcpy(temp,speech,960); for(i=0;i<120;i++) { *(bitstream+i)=(((char)(g726_16_encoder(temp[i*4],AUDIO_ENCODING_LINEAR,&state_ptr)))<<6)|(((char)(g726_16_encoder(temp[i*4+1],AUDIO_ENCODING_LINEAR,&state_ptr)))<<4)|(((char)(g726_16_encoder(temp[i*4+2],AUDIO_ENCODING_LINEAR,&state_ptr)))<<2)|(((char)(g726_16_encoder(temp[i*4+3],AUDIO_ENCODING_LINEAR,&state_ptr)))); } }
int main() { int serial_fd; int sound_fd; int file_orig_fd; int file_encode_fd; int file_decode_fd; unsigned char in[120]; unsigned char out[960]; serial_fd = serial_init(); sound_fd = sound_init(); file_orig_fd = open("phone_orig.wav", O_RDONLY); if (file_orig_fd < 0) { printf("open phone.wav error! \n"); return -1; } file_encode_fd = open("phone_encode.wav", O_RDWR|O_CREAT); if (file_encode_fd < 0) { printf("open file error !\n"); return -1; } file_decode_fd = open("phone_decode.wav", O_WRONLY|O_CREAT); if (file_decode_fd < 0) { printf("open file error !\n"); return -1; } /* the encoding part */ int m; g726_state state_ptr; g726_init_state(&state_ptr); while (1) { m = read(file_orig_fd, out, 960); if (m != 960) break; g726_Encode(out, in, &state_ptr); write(file_encode_fd, in,120); } /* move the file offset to the begnning of the file */ lseek(file_encode_fd, 0, SEEK_SET); g726_init_state(&state_ptr); /* decoding part */ while(1) { m = read(file_encode_fd, in, 120); if (m != 120) break; g726_Decode(in, out, &state_ptr); write(file_decode_fd, out, 960); } close(serial_fd); close(sound_fd); close(file_orig_fd); close(file_encode_fd); close(file_decode_fd); }
static void * create_codec(const struct PluginCodec_Definition * codec) { struct g726_state_s * g726 = malloc(sizeof (struct g726_state_s)); g726_init_state(g726); return g726; }