static switch_status_t switch_bcg729_decode(switch_codec_t *codec, switch_codec_t *other_codec, void *encoded_data, uint32_t encoded_data_len, uint32_t encoded_rate, void *decoded_data, uint32_t *decoded_data_len, uint32_t *decoded_rate, unsigned int *flag) { struct bcg729_context *context = codec->private_info; if (!context) { return SWITCH_STATUS_FALSE; } int framesize; int x; uint32_t new_len = 0; uint8_t *edp = encoded_data; int16_t *ddp = decoded_data; if (encoded_data_len == 0) { /* Native PLC interpolation */ bcg729Decoder(context->decoder_object, NULL, 1, ddp); ddp += 80; decoded_data_len = (uint32_t *) 160; switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "g729 zero length frame\n"); return SWITCH_STATUS_SUCCESS; } for(x = 0; x < encoded_data_len && new_len < *decoded_data_len; x += framesize) { if(encoded_data_len - x < 8) framesize = 2; /* SID */ else framesize = 10; /* regular 729a frame */ bcg729Decoder(context->decoder_object, edp, 0, ddp); ddp += 80; edp += framesize; new_len += 160; } if (new_len <= *decoded_data_len) { *decoded_data_len = new_len; } else { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "buffer overflow!!!\n"); return SWITCH_STATUS_FALSE; } return SWITCH_STATUS_SUCCESS; }
static int decode_audio(struct mgcp_process_rtp_state *state, uint8_t **src, size_t *nbytes) { while (*nbytes >= state->src_frame_size) { if (state->sample_cnt + state->src_samples_per_frame > ARRAY_SIZE(state->samples)) { LOGP(DMGCP, LOGL_ERROR, "Sample buffer too small: %d > %d.\n", state->sample_cnt + state->src_samples_per_frame, ARRAY_SIZE(state->samples)); return -ENOSPC; } switch (state->src_fmt) { case AF_GSM: if (gsm_decode(state->src.gsm_handle, (gsm_byte *)*src, state->samples + state->sample_cnt) < 0) { LOGP(DMGCP, LOGL_ERROR, "Failed to decode GSM.\n"); return -EINVAL; } break; #ifdef HAVE_BCG729 case AF_G729: bcg729Decoder(state->src.g729_dec, *src, 0, state->samples + state->sample_cnt); break; #endif case AF_PCMU: ulaw_decode(*src, state->samples + state->sample_cnt, state->src_samples_per_frame); break; case AF_PCMA: alaw_decode(*src, state->samples + state->sample_cnt, state->src_samples_per_frame); break; case AF_S16: memmove(state->samples + state->sample_cnt, *src, state->src_frame_size); break; case AF_L16: l16_decode(*src, state->samples + state->sample_cnt, state->src_samples_per_frame); break; default: break; } *src += state->src_frame_size; *nbytes -= state->src_frame_size; state->sample_cnt += state->src_samples_per_frame; } return 0; }
static int codec_drv_control( ErlDrvData handle, unsigned int command, char *buf, int len, char **rbuf, int rlen) { codec_data* d = (codec_data*)handle; int ret = 0; ErlDrvBinary *out; *rbuf = NULL; int n = 0; // Number of frames int i = 0; // Temporary counter switch(command) { case CMD_ENCODE: if (len % 160 != 0) break; n = len / 160; // Calculate a number of frames out = driver_alloc_binary(n*10); // n*80 bits ret = n*10; for(i = 0; i<n; i++) bcg729Encoder(d->estate, (int16_t*)buf+80*i, (uint8_t*)out->orig_bytes+10*i); *rbuf = (char *) out; break; case CMD_DECODE: if (len % 10 != 0) break; n = len / 10; // Calculate a number of frames out = driver_alloc_binary(n*160); // n*160 bytes ret = n*160; for(i = 0; i<n; i++) bcg729Decoder(d->dstate, ((uint8_t*)buf)+10*i, 0, (int16_t*)out->orig_bytes+80*i); *rbuf = (char *) out; break; default: break; } return ret; }
int main(int argc, char *argv[] ) { int i; /*** get calling argument ***/ char *filePrefix[MAX_CHANNEL_NBR]; getArgumentsMultiChannel(argc, argv, filePrefix); /* check argument and set filePrefix if needed */ /*** input and output file pointers ***/ FILE *fpInput[MAX_CHANNEL_NBR]; FILE *fpOutput[MAX_CHANNEL_NBR]; FILE *fpBinOutput[MAX_CHANNEL_NBR]; /*** input and output buffers ***/ uint16_t inputBuffer[NB_PARAMETERS+1]; /* input buffer: an array containing the 15 parameters and the frame erasure flag */ int16_t outputBuffer[L_FRAME]; /* output buffer: the reconstructed signal */ uint8_t bitStream[10]; /* binary input for the decoder */ bcg729DecoderChannelContextStruct *decoderChannelContext[MAX_CHANNEL_NBR]; /* context array, one per channel */ /*** inits ***/ for (i=0; i<argc-1; i++) { /* open the inputs file */ if ( (fpInput[i] = fopen(argv[i+1], "r")) == NULL) { printf("%s - Error: can't open file %s\n", argv[0], argv[i+1]); exit(-1); } /* create the outputs file(filename is the same than input file with the .out extension) */ char *outputFile = malloc((strlen(filePrefix[i])+15)*sizeof(char)); sprintf(outputFile, "%s.out.multi",filePrefix[i]); if ( (fpOutput[i] = fopen(outputFile, "w")) == NULL) { printf("%s - Error: can't create file %s\n", argv[i], outputFile); exit(-1); } sprintf(outputFile, "%s.multi.pcm",filePrefix[i]); if ( (fpBinOutput[i] = fopen(outputFile, "wb")) == NULL) { printf("%s - Error: can't create file %s\n", argv[0], outputFile); exit(-1); } /*** init of the tested bloc ***/ decoderChannelContext[i] = initBcg729DecoderChannel(); } /*** initialisation complete ***/ /* perf measurement */ clock_t start, end; double cpu_time_used=0.0; int framesNbr =0; /* increase LOOP_N to increase input length and perform a more accurate profiling or perf measurement */ #define LOOP_N 1 int j,k; for (j=0; j<LOOP_N; j++) { /* perf measurement */ /*** loop over inputs file ***/ int endedFilesNbr = 0; int endedFiles[MAX_CHANNEL_NBR]; for (k=0; k<argc-1; k++) { /* reset the array of boolean containing a flag for files already read */ endedFiles[k]=0; } while (endedFilesNbr<argc-1) { /* loop until the longest file is over */ for (k=0; k<argc-1; k++) { /* read one frame on each not ended file */ if (endedFiles[k]==0) { /* read only if the file is not over */ if (fscanf(fpInput[k], "%hd,%hd,%hd,%hd,%hd,%hd,%hd,%hd,%hd,%hd,%hd,%hd,%hd,%hd,%hd,%hd", &(inputBuffer[0]), &(inputBuffer[1]), &(inputBuffer[2]), &(inputBuffer[3]), &(inputBuffer[4]), &(inputBuffer[5]), &(inputBuffer[6]), &(inputBuffer[7]), &(inputBuffer[8]), &(inputBuffer[9]), &(inputBuffer[10]), &(inputBuffer[11]), &(inputBuffer[12]), &(inputBuffer[13]), &(inputBuffer[14]), &(inputBuffer[15]))==16) /* index 4 and 5 are inverted to get P0 in 4 and P1 in 5 in the array */ { /* input buffer contains the parameters and in [15] the frame erasure flag */ int i; framesNbr++; parametersArray2BitStream(inputBuffer, bitStream); start = clock(); bcg729Decoder(decoderChannelContext[k], bitStream, inputBuffer[15], outputBuffer); end = clock(); cpu_time_used += ((double) (end - start)); /* write the output to the output file */ if (j==0) { fprintf(fpOutput[k],"%d",outputBuffer[0]); for (i=1; i<L_FRAME; i++) { fprintf(fpOutput[k],",%d",outputBuffer[i]); } fprintf(fpOutput[k],"\n"); /* write the ouput to raw data file */ fwrite(outputBuffer, sizeof(int16_t), L_FRAME, fpBinOutput[k]); } } else { /* we've reach the end of the file */ endedFiles[k]=1; endedFilesNbr++; } } } } /* perf measurement */ for (k=0; k<argc-1; k++) { rewind(fpInput[k]); } } /* close decoder channels */ for (k=0; k<argc-1; k++) { closeBcg729DecoderChannel(decoderChannelContext[k]); } /* Perf measurement: uncomment next line to print cpu usage */ printf("Decode %d frames in %f seconds : %f us/frame\n", framesNbr, cpu_time_used/CLOCKS_PER_SEC, cpu_time_used*1000000/((double)framesNbr*CLOCKS_PER_SEC)); /* perf measurement */ exit (0); }
static void generic_plc_process(MSFilter *f) { generic_plc_struct *mgps=(generic_plc_struct*)f->data; unsigned int buff_size = mgps->rate*sizeof(int16_t)*mgps->nchannels*f->ticker->interval/1000; mblk_t *m; while((m=ms_queue_get(f->inputs[0]))!=NULL){ int transitionBufferSize = mgps->rate*sizeof(int16_t)*TRANSITION_DELAY/1000; unsigned char buf[128]; unsigned int time = (1000*(m->b_wptr - m->b_rptr))/(mgps->rate*sizeof(int16_t)*mgps->nchannels); ms_concealer_inc_sample_time(mgps->concealer, f->ticker->time, time, TRUE); /* introduce 16 sample delay (2ms) */ memcpy(buf, m->b_wptr-transitionBufferSize, transitionBufferSize); memmove(m->b_rptr+transitionBufferSize, m->b_rptr, m->b_wptr - m->b_rptr - transitionBufferSize); memcpy(m->b_rptr, mgps->continuity_buffer, transitionBufferSize); memcpy(mgps->continuity_buffer, buf, transitionBufferSize); if (mgps->cng_running){ /*we were doing CNG, now resuming with normal audio*/ int16_t continuity_buffer[80]; #ifdef HAVE_G729B bcg729Decoder(mgps->decoderChannelContext, NULL, 0, 1, 1, 1, continuity_buffer); #else memset (continuity_buffer, 0, 80*sizeof(int16_t)); #endif memcpy(m->b_rptr, continuity_buffer, transitionBufferSize); generic_plc_transition_mix((int16_t *)(m->b_rptr+transitionBufferSize), &(continuity_buffer[mgps->rate*TRANSITION_DELAY/1000]), mgps->rate*TRANSITION_DELAY/1000); mgps->cng_running=FALSE; mgps->cng_set=FALSE; } ms_queue_put(f->outputs[0], m); } if (ms_concealer_context_is_concealement_required(mgps->concealer, f->ticker->time)) { #ifdef HAVE_G729B m = allocb(buff_size, 0); /* Transmitted CNG data is in mgps->cng_data : give it to bcg729 decoder -> output in m->b_wptr */ if (mgps->cng_set) { /* received some CNG data */ mgps->cng_set=FALSE; /* reset flag */ mgps->cng_running=TRUE; bcg729Decoder(mgps->decoderChannelContext, mgps->cng_data.data, mgps->cng_data.datasize, 0, 1, 1, (int16_t *)(m->b_wptr)); mblk_set_cng_flag(m, 1); generic_plc_transition_mix((int16_t *)m->b_wptr, (int16_t *)mgps->continuity_buffer, mgps->rate*TRANSITION_DELAY/1000); /* TODO: if ticker->interval is not 10 ms which is also G729 frame length, we must generate untransmitted frame CNG until we reach the requested data amount */ } else if (mgps->cng_running) { /* missing frame but CNG is ongoing: shall be an untransmitted frame */ bcg729Decoder(mgps->decoderChannelContext, NULL, 0, 1, 1, 1, (int16_t *)(m->b_wptr)); mblk_set_cng_flag(m, 1); } else { mblk_set_plc_flag(m, 1); memset(m->b_wptr, 0, buff_size); } #else m = allocb(buff_size, 0); if (!mgps->cng_running && mgps->cng_set){ mgps->cng_running=TRUE; mblk_set_cng_flag(m, 1); /*TODO do something with the buffer*/ }else{ mblk_set_plc_flag(m, 1); } memset(m->b_wptr, 0, buff_size); #endif m->b_wptr += buff_size; ms_queue_put(f->outputs[0], m); ms_concealer_inc_sample_time(mgps->concealer, f->ticker->time, f->ticker->interval, FALSE); } }
static void generic_plc_process(MSFilter *f) { generic_plc_struct *mgps=(generic_plc_struct*)f->data; plc_context_t *plc_context = mgps->plc_context; mblk_t *m; while((m=ms_queue_get(f->inputs[0]))!=NULL){ int transitionBufferSize = mgps->rate*sizeof(int16_t)*TRANSITION_DELAY/1000; size_t msg_size = msgdsize(m); unsigned int time = (unsigned int)((1000*msg_size)/(mgps->rate*sizeof(int16_t)*mgps->nchannels)); ms_concealer_inc_sample_time(mgps->concealer, f->ticker->time, time, TRUE); /* Store current msg in plc_buffer */ generic_plc_update_plc_buffer(plc_context, m->b_rptr, msg_size); /* introduce delay (TRANSITION_DELAY ms) */ generic_plc_update_continuity_buffer(plc_context, m->b_rptr, msg_size); if (mgps->cng_running){ /*we were doing CNG, now resuming with normal audio*/ int16_t continuity_buffer[80]; #ifdef HAVE_G729B bcg729Decoder(mgps->decoderChannelContext, NULL, 0, 1, 1, 1, continuity_buffer); #else memset (continuity_buffer, 0, 80*sizeof(int16_t)); #endif memcpy(m->b_rptr, continuity_buffer, transitionBufferSize); generic_plc_transition_mix((int16_t *)(m->b_rptr+transitionBufferSize), continuity_buffer, mgps->rate*TRANSITION_DELAY/1000); mgps->cng_running=FALSE; mgps->cng_set=FALSE; } if (plc_context->plc_samples_used!=0) { /*we were doing PLC, now resuming with normal audio, continuity buffer is twice the transition delay lengths, * the second half is untouched by the update function and contains transition data generated by PLC */ generic_plc_transition_mix((int16_t *)(m->b_rptr+transitionBufferSize), (int16_t *)(plc_context->continuity_buffer+transitionBufferSize), mgps->rate*TRANSITION_DELAY/1000); } plc_context->plc_index=0; plc_context->plc_samples_used=0; ms_queue_put(f->outputs[0], m); } if (ms_concealer_context_is_concealement_required(mgps->concealer, f->ticker->time)) { unsigned int buff_size = mgps->rate*sizeof(int16_t)*mgps->nchannels*f->ticker->interval/1000; #ifdef HAVE_G729B m = allocb(buff_size, 0); /* Transmitted CNG data is in mgps->cng_data : give it to bcg729 decoder -> output in m->b_wptr */ if (mgps->cng_set) { /* received some CNG data */ mgps->cng_set=FALSE; /* reset flag */ mgps->cng_running=TRUE; bcg729Decoder(mgps->decoderChannelContext, mgps->cng_data.data, mgps->cng_data.datasize, 0, 1, 1, (int16_t *)(m->b_wptr)); mblk_set_cng_flag(m, 1); generic_plc_transition_mix((int16_t *)m->b_wptr, (int16_t *)plc_context->continuity_buffer, mgps->rate*TRANSITION_DELAY/1000); /* TODO: if ticker->interval is not 10 ms which is also G729 frame length, we must generate untransmitted frame CNG until we reach the requested data amount */ } else if (mgps->cng_running) { /* missing frame but CNG is ongoing: shall be an untransmitted frame */ bcg729Decoder(mgps->decoderChannelContext, NULL, 0, 1, 1, 1, (int16_t *)(m->b_wptr)); mblk_set_cng_flag(m, 1); } else { /* plc */ mblk_set_plc_flag(m, 1); generic_plc_generate_samples(plc_context, (int16_t *)m->b_wptr, buff_size/sizeof(int16_t)); /* store the generated samples into plc_buffer */ generic_plc_update_plc_buffer(plc_context, m->b_wptr, buff_size); //memset(m->b_wptr, 0, buff_size); } #else m = allocb(buff_size, 0); if (mgps->cng_set){ mgps->cng_set=FALSE; /* reset flag */ mgps->cng_running=TRUE; mblk_set_cng_flag(m, 1); /*TODO do something with the buffer*/ memset(m->b_wptr, 0, buff_size); } else if (mgps->cng_running) { /* missing frame but CNG is ongoing: shall be an untransmitted frame */ memset(m->b_wptr, 0, buff_size); mblk_set_cng_flag(m, 1); }else{ /* plc */ mblk_set_plc_flag(m, 1); generic_plc_generate_samples(plc_context, (int16_t *)m->b_wptr, buff_size/sizeof(int16_t)); /* store the generated samples into plc_buffer */ generic_plc_update_plc_buffer(plc_context, m->b_wptr, buff_size); //memset(m->b_wptr, 0, buff_size); } #endif m->b_wptr += buff_size; ms_queue_put(f->outputs[0], m); ms_concealer_inc_sample_time(mgps->concealer, f->ticker->time, f->ticker->interval, FALSE); } }