static int codec_decoder(const struct PluginCodec_Definition *codec, void *context, const void *from, unsigned *fromLen, void *to, unsigned *toLen, unsigned int *flag) { Word16 i; Word16 *synth; Word16 parm[PRM_SIZE + 1]; DecState *decoder = (DecState *) context; if (*fromLen < BYTES_PER_FRAME || *toLen < SAMPLES_PER_FRAME * 2) return 0; Restore_Params(from, &parm[1]); synth = decoder->synth_buf + M; parm[0] = 1; for (i = 0; i < PRM_SIZE; i++) { if (parm[i + 1] != 0) { parm[0] = 0; break; } } parm[4] = Check_Parity_Pitch(parm[3], parm[4]); Decod_ld8a(decoder, parm, synth, decoder->Az_dec, decoder->T2, &decoder->bad_lsf); Post_Filter(decoder, synth, decoder->Az_dec, decoder->T2); Post_Process(decoder, synth, L_FRAME); for (i = 0; i < SAMPLES_PER_FRAME; i++) ((Word16 *) to)[i] = synth[i]; *fromLen = BYTES_PER_FRAME; *toLen = SAMPLES_PER_FRAME * 2; return 1; }
void g729a_dec_process (void *decState, UWord8 *bitstream, Word16 *pcm, Flag badFrame) #endif { g729a_decode_frame_state *state = (g729a_decode_frame_state *)decState; static Word16 bad_lsf = 0; /* Initialize bad LSF indicator */ Word16 parm[PRM_SIZE+1]; /* Synthesis parameters */ Word16 Az_dec[MP1*2]; /* Decoded Az for post-filter */ Word16 T2[2]; /* Pitch lag for 2 subframes */ bits2prm_ld8k( bitstream, &parm[1]); parm[0] = badFrame ? 1 : 0; /* No frame erasure */ /* check pitch parity and put 1 in parm[4] if parity error */ parm[4] = Check_Parity_Pitch(parm[3], parm[4]); Decod_ld8a(&state->decoderState, parm, state->synth, Az_dec, T2, bad_lsf); Post_Filter(&state->postFilterState, state->synth, Az_dec, T2); /* Post-filter */ Post_Process(&state->postProcessState, state->synth, pcm, L_FRAME); }
void G729aDecoder(Word16 serial[], Word16 speech[]) { extern Word16 *synth; /* Synthesis */ Word16 parm[PRM_SIZE+1]; /* Synthesis parameters */ Word16 Az_dec[MP1*2]; /* Decoded Az for post-filter */ Word16 T2[2]; /* Pitch lag for 2 subframes */ Word16 i; bits2prm_ld8k( &serial[2], &parm[1]); parm[0] = 0; for (i=2; i < SERIAL_SIZE; i++) if (serial[i] == 0 ) parm[0] = 1; parm[4] = Check_Parity_Pitch(parm[3], parm[4]); Decod_ld8a(parm, synth, Az_dec, T2); Post_Filter(synth, Az_dec, T2); Post_Process(synth, L_FRAME); for(i=0;i<L_FRAME;i++) speech[i]=synth[i]; }
Word16 read_frame(FILE *f_serial, Word16 *parm) { Word16 i; Word16 serial[SERIAL_SIZE]; /* Serial stream */ if(fread(serial, sizeof(short), 2, f_serial) != 2) { return(0); } if(fread(&serial[2], sizeof(Word16), (size_t)serial[1], f_serial) != (size_t)serial[1]) { return(0); } bits2prm_ld8k(&serial[1], parm); /* This part was modified for version V1.3 */ /* for speech and SID frames, the hardware detects frame erasures by checking if all bits are set to zero */ /* for untransmitted frames, the hardware detects frame erasures by testing serial[0] */ parm[0] = 0; /* No frame erasure */ if(serial[1] != 0) { for (i=0; i < serial[1]; i++) if (serial[i+2] == 0 ) parm[0] = 1; /* frame erased */ } else if(serial[0] != SYNC_WORD) parm[0] = 1; if(parm[1] == 1) { /* check parity and put 1 in parm[5] if parity error */ parm[5] = Check_Parity_Pitch(parm[4], parm[5]); } return(1); }
int main(int argc, char *argv[] ) { Word16 rate; Word16 synth_buf[L_ANA_BWD], *synth; /* Synthesis */ Word16 parm[PRM_SIZE_E+2]; /* Synthesis parameters */ Word16 serial[SERIAL_SIZE_E]; /* Serial stream */ Word16 Az_dec[M_BWDP1*2], *ptr_Az; /* Decoded Az for post-filter */ Word16 T0_first; /* Pitch lag in 1st subframe */ Word16 pst_out[L_FRAME]; /* Postfilter output */ Word16 voicing; /* voicing from previous frame */ Word16 sf_voic; /* voicing for subframe */ Word16 i, frame = 0; Word16 ga1_post, ga2_post, ga_harm; Word16 long_h_st, m_pst; Word16 serial_size; Word16 bwd_dominant; FILE *f_syn, *f_serial; printf("\n"); printf("*************************************************************************\n"); printf("**** HIGHER BIT RATE EXTENSION OF ITU G.729 8 KBIT/S SPEECH DECODER ****\n"); printf("**** MIXED BACKWARD / FORWARD LPC STRUCTURE ****\n"); printf("*************************************************************************\n"); printf("\n"); printf("------------------- Fixed point C simulation -----------------\n"); printf("\n"); printf("--------- Version 1.3 (Release 2, November 2006) ---------\n"); printf("\n"); printf(" Bit rates : 8.0 kb/s (G729) or 11.8 kb/s\n"); printf("\n"); /* Passed arguments */ if ( argc != 3 ) { printf("Usage : decoder bitstream_file output_file \n"); printf("\n"); printf("Format for bitstream_file:\n"); printf(" One (2-byte) synchronization word,\n"); printf(" One (2-byte) bit-rate word,\n"); printf("\n"); printf("Format for outputspeech_file:\n"); printf(" Output is written to a binary file of 16 bits data.\n"); exit( 1 ); } /* Open file for synthesis and packed serial stream */ if( (f_serial = fopen(argv[1],"rb") ) == NULL ) { printf("%s - Error opening file %s !!\n", argv[0], argv[1]); exit(0); } if( (f_syn = fopen(argv[2], "wb") ) == NULL ) { printf("%s - Error opening file %s !!\n", argv[0], argv[2]); exit(0); } printf("Input bitstream file : %s\n\n",argv[1]); printf("Synthesis speech file : %s\n\n",argv[2]); /*-----------------------------------------------------------------* * Initialization of decoder * *-----------------------------------------------------------------*/ for (i=0; i<L_ANA_BWD; i++) synth_buf[i] = 0; synth = synth_buf + MEM_SYN_BWD; Init_Decod_ld8e(); Init_Post_Filter(); Init_Post_Process(); voicing = 60; ga1_post = GAMMA1_PST_E; ga2_post = GAMMA2_PST_E; ga_harm = GAMMA_HARM_E; /*-----------------------------------------------------------------* * Loop for each "L_FRAME" speech data * *-----------------------------------------------------------------*/ while( fread(serial, sizeof(Word16), 2, f_serial) == 2) { if(serial[0] != SYNC_WORD ) { printf("error reading synchro word file %s frame %hd\n", argv[1], frame); exit(-1); } serial_size = serial[1]; if( (serial_size != 118) && (serial_size != 80) ){ printf("error uncorrect value of serial_size (%hd) in %s frame %hd\n", serial_size, argv[1], frame); exit(-1); } if (fread(&serial[2], sizeof(Word16), serial_size, f_serial) != (size_t)serial_size) { printf("error reading file %s frame %hd\n", argv[1], frame); exit(-1); } frame++; #if defined(__BORLANDC__) printf("Frame : %4d\r", frame); #endif if(serial_size == 118) rate = 1; else rate = 0; bits2prm_ld8e(&serial[2], &parm[1], rate); /* the hardware detects frame erasures by checking if all bits are set to zero */ parm[0] = 0; /* No frame erasure */ for (i=2; i < serial_size; i++) if (serial[i] == 0 ) parm[0] = 1; /* frame erased */ if (parm[0] == 1) printf("Frame Erased : %d\n", frame); if(rate == 0) { parm[4] = Check_Parity_Pitch(parm[3], parm[4]); } else { /* ------------------------------------------------------------------ */ /* check parity and put 1 in parm[5] if parity error in Forward mode */ /* put 1 in parm[3] if parity error in Backward mode */ /* ------------------------------------------------------------------ */ if (parm[1] == 0) { i = shr(parm[4], 1); i &= (Word16)1; parm[5] = add(parm[5], i); parm[5] = Check_Parity_Pitch(parm[4], parm[5]); } else { i = shr(parm[2], 1); i &= (Word16)1; parm[3] = add(parm[3], i); parm[3] = Check_Parity_Pitch(parm[2], parm[3]); } } /* ---------- */ /* Decoding */ /* ---------- */ Decod_ld8e(parm, rate, voicing, synth_buf, Az_dec, &T0_first, &bwd_dominant, &m_pst); /* ---------- */ /* Postfilter */ /* ---------- */ voicing = 0; ptr_Az = Az_dec; /* Adaptive parameters for postfiltering */ /* ------------------------------------- */ if (serial_size == 80) { long_h_st = LONG_H_ST; ga1_post = GAMMA1_PST; ga2_post = GAMMA2_PST; ga_harm = GAMMA_HARM; } else { long_h_st = LONG_H_ST_E; /* If backward mode is dominant => progressively reduce postfiltering */ if ((parm[1] == 1) && (bwd_dominant == 1)) { ga_harm = sub(ga_harm, 410); if (ga_harm < 0) ga_harm = 0; ga1_post = sub(ga1_post, 1147); if (ga1_post < 0) ga1_post = 0; ga2_post = sub(ga2_post, 1065); if (ga2_post < 0) ga2_post = 0; } else { ga_harm = add( ga_harm, 410); if (ga_harm > GAMMA_HARM_E) ga_harm = GAMMA_HARM_E; ga1_post = add(ga1_post, 1147); if (ga1_post > GAMMA1_PST_E) ga1_post = GAMMA1_PST_E; ga2_post = add(ga2_post, 1065); if (ga2_post > GAMMA2_PST_E) ga2_post = GAMMA2_PST_E; } } for(i=0; i<L_FRAME; i++) pst_out[i] = synth[i]; for(i=0; i<L_FRAME; i+=L_SUBFR) { Poste(T0_first, &synth[i], ptr_Az, &pst_out[i], &sf_voic, ga1_post, ga2_post, ga_harm, long_h_st, m_pst); if (sf_voic != 0) voicing = sf_voic; ptr_Az += m_pst+1; } #ifdef POSTPROC Post_Process(pst_out, L_FRAME); #else for (i=0; i<L_FRAME; i++) pst_out[i] = shl(pst_out[i],1); #endif #ifdef HARDW { Word16 *my_pt; Word16 my_temp; int my_i; my_pt = pst_out; for(my_i=0; my_i < L_FRAME; my_i++) { my_temp = *my_pt; my_temp = add( my_temp, (Word16) 4); /* rounding on 13 bit */ my_temp = my_temp & 0xFFF8; /* mask on 13 bit */ *my_pt++ = my_temp; } } #endif fwrite(pst_out, sizeof(Word16), L_FRAME, f_syn); } printf("\n"); return(0); }
//#if 0 int main(int argc, char *argv[] ) { Word16 synth_buf[L_FRAME+M], *synth; /* Synthesis */ Word16 parm[PRM_SIZE+1]; /* Synthesis parameters */ Word16 serial[SERIAL_SIZE]; /* Serial stream */ Word16 Az_dec[MP1*2]; /* Decoded Az for post-filter */ Word16 T2[2]; /* Pitch lag for 2 subframes */ Word16 i, frame; FILE *f_syn, *f_serial; printf("\n"); printf("************ G.729a 8.0 KBIT/S SPEECH DECODER ************\n"); printf("\n"); printf("------------------- Fixed point C simulation ----------------\n"); printf("\n"); printf("----------------- Version 1.1 ---------------\n"); printf("\n"); /* Passed arguments */ argc = 3; strcpy(argv[1],"test.bit"); strcpy(argv[2],"test.dec"); if ( argc != 3) { printf("Usage :%s bitstream_file outputspeech_file\n",argv[0]); printf("\n"); printf("Format for bitstream_file:\n"); printf(" One (2-byte) synchronization word \n"); printf(" One (2-byte) size word,\n"); printf(" 80 words (2-byte) containing 80 bits.\n"); printf("\n"); printf("Format for outputspeech_file:\n"); printf(" Synthesis is written to a binary file of 16 bits data.\n"); exit( 1 ); } /* Open file for synthesis and packed serial stream */ if( (f_serial = fopen(argv[1],"rb") ) == NULL ) { printf("%s - Error opening file %s !!\n", argv[0], argv[1]); exit(0); } if( (f_syn = fopen(argv[2], "wb") ) == NULL ) { printf("%s - Error opening file %s !!\n", argv[0], argv[2]); exit(0); } printf("Input bitstream file : %s\n",argv[1]); printf("Synthesis speech file : %s\n",argv[2]); /*-----------------------------------------------------------------* * Initialization of decoder * *-----------------------------------------------------------------*/ for (i=0; i<M; i++) synth_buf[i] = 0; synth = synth_buf + M; bad_lsf = 0; /* Initialize bad LSF indicator */ Init_Decod_ld8a(); Init_Post_Filter(); Init_Post_Process(); /*-----------------------------------------------------------------* * Loop for each "L_FRAME" speech data * *-----------------------------------------------------------------*/ frame = 0; /* c code */ while( fread(serial, sizeof(Word16), SERIAL_SIZE, f_serial) == SERIAL_SIZE) { //printf("Frame =%d\n", frame++); printf("Frame =%d \n", frame); frame = frame + 1; bits2prm_ld8k( &serial[2], &parm[1]); /* the hardware detects frame erasures by checking if all bits are set to zero */ parm[0] = 0; /* No frame erasure */ for (i=2; i < SERIAL_SIZE; i++) if (serial[i] == 0 ) parm[0] = 1; /* frame erased */ /* check pitch parity and put 1 in parm[4] if parity error */ parm[4] = Check_Parity_Pitch(parm[3], parm[4]); Decod_ld8a(parm, synth, Az_dec, T2); Post_Filter(synth, Az_dec, T2); /* Post-filter */ Post_Process(synth, L_FRAME); fwrite(synth, sizeof(short), L_FRAME, f_syn); } return(0); }
int main(int argc, char *argv[] ) { Word16 synth_buf[L_FRAME+M], *synth; /* Synthesis */ Word16 parm[PRM_SIZE+1]; /* Synthesis parameters */ Word16 serial[SERIAL_SIZE]; /* Serial stream */ Word16 Az_dec[MP1*2], *ptr_Az; /* Decoded Az for post-filter */ Word16 T0_first; /* Pitch lag in 1st subframe */ Word16 pst_out[L_FRAME]; /* Postfilter output */ Word16 voicing; /* voicing from previous frame */ Word16 sf_voic; /* voicing for subframe */ Word16 i, frame; FILE *f_syn, *f_serial; printf("\n"); printf("*********** ITU G.729 8 KBIT/S SPEECH CODER ***********\n"); printf("\n"); printf("------------------- Fixed point C simulation -----------------\n"); printf("\n"); printf("----------------- Version 3.3 ----------------\n"); printf("\n"); /* Passed arguments */ if ( argc != 3 ) { printf("Usage :%s bitstream_file outputspeech_file\n",argv[0]); printf("\n"); printf("Format for bitstream_file:\n"); printf(" One (2-byte) synchronization word,\n"); printf(" One (2-byte) size word,\n"); printf(" 80 words (2-byte) containing 80 bits.\n"); printf("\n"); printf("Format for outputspeech_file:\n"); printf(" Output is written to a binary file of 16 bits data.\n"); exit( 1 ); } /* Open file for synthesis and packed serial stream */ if( (f_serial = fopen(argv[1],"rb") ) == NULL ) { printf("%s - Error opening file %s !!\n", argv[0], argv[1]); exit(0); } if( (f_syn = fopen(argv[2], "wb") ) == NULL ) { printf("%s - Error opening file %s !!\n", argv[0], argv[2]); exit(0); } printf("Input bitstream file : %s\n",argv[1]); printf("Synthesis speech file : %s\n",argv[2]); /*-----------------------------------------------------------------* * Initialization of decoder * *-----------------------------------------------------------------*/ for (i=0; i<M; i++) synth_buf[i] = 0; synth = synth_buf + M; Init_Decod_ld8k(); Init_Post_Filter(); Init_Post_Process(); voicing = 60; /*-----------------------------------------------------------------* * Loop for each "L_FRAME" speech data * *-----------------------------------------------------------------*/ frame = 0; while( fread(serial, sizeof(Word16), SERIAL_SIZE, f_serial) == SERIAL_SIZE) { bits2prm_ld8k( &serial[2], &parm[1]); /* the hardware detects frame erasures by checking if all bits are set to zero */ parm[0] = 0; /* No frame erasure */ for (i=2; i < SERIAL_SIZE; i++) if (serial[i] == 0 ) parm[0] = 1; /* frame erased */ /* check parity and put 1 in parm[4] if parity error */ parm[4] = Check_Parity_Pitch(parm[3], parm[4]); Decod_ld8k(parm, voicing, synth, Az_dec, &T0_first); /* Postfilter */ voicing = 0; ptr_Az = Az_dec; for(i=0; i<L_FRAME; i+=L_SUBFR) { Post(T0_first, &synth[i], ptr_Az, &pst_out[i], &sf_voic); if (sf_voic != 0) { voicing = sf_voic;} ptr_Az += MP1; } Copy(&synth_buf[L_FRAME], &synth_buf[0], M); Post_Process(pst_out, L_FRAME); #ifdef HARDW { Word16 *my_pt; Word16 my_temp; int my_i; my_pt = pst_out; for(my_i=0; my_i < L_FRAME; my_i++) { my_temp = *my_pt; my_temp = add( my_temp, (Word16) 4); /* rounding on 13 bit */ my_temp = my_temp & 0xFFF8; /* mask on 13 bit */ *my_pt++ = my_temp; } } #endif fwrite(pst_out, sizeof(Word16), L_FRAME, f_syn); frame++; printf("Frame =%d\r", frame); } return(0); }
static tsk_size_t tdav_codec_g729ab_decode(tmedia_codec_t* self, const void* in_data, tsk_size_t in_size, void** out_data, tsk_size_t* out_max_size, const tsk_object_t* proto_hdr) { tsk_size_t out_size = 0; int i, frame_count; const uint8_t* data_start = (const uint8_t*)in_data; const uint8_t* data_end; tdav_codec_g729ab_t* g729a = (tdav_codec_g729ab_t*)self; if(!self || !in_data || !in_size || !out_data || ((in_size % 10) && (in_size % 10 != 2))){ TSK_DEBUG_ERROR("Invalid parameter"); return 0; } data_end = (data_start + in_size); frame_count = (in_size/10) + ((in_size % 10) ? 1 : 0); out_size = 160*frame_count; /* allocate new buffer if needed */ if(*out_max_size <out_size){ if(!(*out_data = tsk_realloc(*out_data, out_size))){ TSK_DEBUG_ERROR("Failed to allocate new buffer"); *out_max_size = 0; return 0; } *out_max_size = out_size; } for(i=0; i<frame_count; i++){ memset(g729a->decoder.synth_buf, 0, M); g729a->decoder.synth = g729a->decoder.synth_buf + M; if((data_end - data_start) == 2){ unpack_SID(data_start, g729a->decoder.serial); data_start += 2; } else{ unpack_G729(data_start, g729a->decoder.serial, 10); data_start += 10; } bits2prm_ld8k(&g729a->decoder.serial[1], g729a->decoder.parm); /* This part was modified for version V1.3 */ /* for speech and SID frames, the hardware detects frame erasures by checking if all bits are set to zero */ /* for untransmitted frames, the hardware detects frame erasures by testing serial[0] */ g729a->decoder.parm[0] = 0; /* No frame erasure */ if(g729a->decoder.serial[1] != 0) { int j; for (j=0; j < g729a->decoder.serial[1]; j++){ if (g729a->decoder.serial[j+2] == 0){ g729a->decoder.parm[0] = 1; /* frame erased */ break; } } } else if(g729a->decoder.serial[0] != SYNC_WORD){ g729a->decoder.parm[0] = 1; } if(g729a->decoder.parm[1] == 1) { /* check parity and put 1 in parm[5] if parity error */ g729a->decoder.parm[5] = Check_Parity_Pitch(g729a->decoder.parm[4], g729a->decoder.parm[5]); } Decod_ld8a(g729a->decoder.parm, g729a->decoder.synth, g729a->decoder.Az_dec, g729a->decoder.T2, &g729a->decoder.Vad); Post_Filter(g729a->decoder.synth, g729a->decoder.Az_dec, g729a->decoder.T2, g729a->decoder.Vad); /* Post-filter */ Post_Process(g729a->decoder.synth, L_FRAME); memcpy(&((uint8_t*)*out_data)[160*i], g729a->decoder.synth, 160); } return out_size; }