/*! \brief encode the temporary buffer and generate a frame */ static struct ast_frame *lintoilbc_frameout(struct ast_trans_pvt *pvt) { struct ilbc_coder_pvt *tmp = pvt->pvt; int datalen = 0; int samples = 0; /* We can't work on anything less than a frame in size */ if (pvt->samples < ILBC_SAMPLES) return NULL; while (pvt->samples >= ILBC_SAMPLES) { float tmpf[ILBC_SAMPLES]; int i; /* Encode a frame of data */ for (i = 0 ; i < ILBC_SAMPLES ; i++) tmpf[i] = tmp->buf[samples + i]; iLBC_encode( pvt->outbuf.uc + datalen, tmpf, &tmp->enc); datalen += ILBC_FRAME_LEN; samples += ILBC_SAMPLES; pvt->samples -= ILBC_SAMPLES; } /* Move the data at the end of the buffer to the front */ if (pvt->samples) memmove(tmp->buf, tmp->buf + samples, pvt->samples * 2); return ast_trans_frameout(pvt, datalen, samples); }
static int encode(struct auenc_state *st, uint8_t *buf, size_t *len, const int16_t *sampv, size_t sampc) { float float_buf[sampc]; uint32_t i; /* Make sure there is enough space */ if (*len < st->enc_bytes) { warning("ilbc: encode: buffer is too small (%u bytes)\n", *len); return ENOMEM; } /* Convert from 16-bit samples to float */ for (i=0; i<sampc; i++) { const int16_t v = sampv[i]; float_buf[i] = (float)v; } iLBC_encode(buf, /* (o) encoded data bits iLBC */ float_buf, /* (o) speech vector to encode */ &st->enc); /* (i/o) the general encoder state */ *len = st->enc_bytes; return 0; }
static int codec_encoder(const struct PluginCodec_Definition * codec, void * context, const void * from, unsigned * fromLen, void * to, unsigned * toLen, unsigned int * flag) { float block[BLOCKL_MAX]; int i; struct iLBC_Enc_Inst_t_ * encoder = (struct iLBC_Enc_Inst_t_ *)context; const short * sampleBuffer = (const short *)from; if (*fromLen < encoder->blockl*2U) return 0; /* convert signal to float */ for (i = 0; i < encoder->blockl; i++) block[i] = (float)sampleBuffer[i]; /* do the actual encoding */ iLBC_encode(to, block, encoder); // set output lengths *toLen = encoder->no_of_bytes; *fromLen = encoder->blockl*2; return 1; }
int Pcm16_2_iLBC( unsigned char* out_buf, unsigned char* in_buf, unsigned int size, unsigned int channels, unsigned int rate, long h_codec ) { short* in_b = (short*)in_buf; float block[BLOCKL_MAX]; int i,k; iLBC_Codec_Inst_t* codec_inst; int out_buf_offset=0; if (!h_codec){ ERROR("iLBC codec not initialized.\n"); return 0; } if ((channels!=1)||(rate!=8000)) { ERROR("Unsupported input format for iLBC encoder.\n"); return 0; } codec_inst = (iLBC_Codec_Inst_t*)h_codec; for (i=0;i< size / (2*codec_inst->iLBC_Enc_Inst.blockl);i++) { /* convert signal to float */ for (k=0; k<codec_inst->iLBC_Enc_Inst.blockl; k++) block[k] = in_b[i*codec_inst->iLBC_Enc_Inst.blockl + k]; /* do the actual encoding */ iLBC_encode((unsigned char *)(out_buf+out_buf_offset), block, &codec_inst->iLBC_Enc_Inst); out_buf_offset+=codec_inst->iLBC_Enc_Inst.no_of_bytes; } return out_buf_offset; }
static tsk_size_t tdav_codec_ilbc_encode(tmedia_codec_t* self, const void* in_data, tsk_size_t in_size, void** out_data, tsk_size_t* out_max_size) { tdav_codec_ilbc_t* ilbc = (tdav_codec_ilbc_t*)self; int k; if(!self || !in_data || !in_size || !out_data){ TSK_DEBUG_ERROR("Invalid parameter"); return 0; } /* convert signal to float */ for(k=0; k<ilbc->encoder.blockl; k++){ ilbc->encblock[k] = (float)((short*)in_data)[k]; } /* allocate new buffer if needed */ if((int)*out_max_size <ilbc->encoder.no_of_bytes){ if(!(*out_data = tsk_realloc(*out_data, ilbc->encoder.no_of_bytes))){ TSK_DEBUG_ERROR("Failed to allocate new buffer"); *out_max_size = 0; return 0; } *out_max_size = ilbc->encoder.no_of_bytes; } /* do the actual encoding */ iLBC_encode(*out_data, ilbc->encblock, &ilbc->encoder); return ilbc->encoder.no_of_bytes; }
/* 编码一次 */ static int encode(short *samples, unsigned char *data) { int i; float block[BLOCKL_MAX]; // Convert to float representaion of voice signal. for (i = 0; i < g_enc_inst.blockl; i++) { block[i] = samples[i]; } iLBC_encode(data, block, &g_enc_inst); return g_enc_inst.no_of_bytes; }
uint16 t_ilbc_audio_encoder::encode(int16 *sample_buf, uint16 nsamples, uint8 *payload, uint16 payload_size, bool &silence) { assert(payload_size >= _max_payload_size); assert(nsamples == _ilbc_encoder.blockl); silence = false; float block[nsamples]; for (int i = 0; i < nsamples; i++) { block[i] = static_cast<float>(sample_buf[i]); } iLBC_encode((unsigned char*)payload, block, &_ilbc_encoder); return _ilbc_encoder.no_of_bytes; }
static struct ast_frame *lintoilbc_frameout(struct ast_translator_pvt *tmp) { int x=0,i; float tmpf[240]; /* We can't work on anything less than a frame in size */ if (tmp->tail < 240) return NULL; tmp->f.frametype = AST_FRAME_VOICE; tmp->f.subclass = AST_FORMAT_ILBC; tmp->f.mallocd = 0; tmp->f.offset = AST_FRIENDLY_OFFSET; tmp->f.src = __PRETTY_FUNCTION__; tmp->f.data = tmp->outbuf; while(tmp->tail >= 240) { if ((x+1) * 50 >= sizeof(tmp->outbuf)) { ast_log(LOG_WARNING, "Out of buffer space\n"); break; } for (i=0;i<240;i++) tmpf[i] = tmp->buf[i]; /* Encode a frame of data */ iLBC_encode(((unsigned char *)(tmp->outbuf)) + (x * 50), tmpf, &tmp->enc); /* Assume 8000 Hz -- 20 ms */ tmp->tail -= 240; /* Move the data at the end of the buffer to the front */ if (tmp->tail) memmove(tmp->buf, tmp->buf + 240, tmp->tail * 2); x++; } tmp->f.datalen = x * 50; tmp->f.samples = x * 240; #if 0 { static int fd = -1; if (fd == -1) { fd = open("ilbc.out", O_CREAT|O_TRUNC|O_WRONLY, 0666); write(fd, tmp->f.data, tmp->f.datalen); close(fd); } } #endif return &tmp->f; }
short encode( /* (o) Number of bytes encoded */ iLBC_Enc_Inst_t *iLBCenc_inst, /* (i/o) Encoder instance */ short *encoded_data, /* (o) The encoded bytes */ short *data /* (i) The signal block to encode*/ ){ float block[BLOCKL_MAX]; int k; /* convert signal to float */ for (k=0; k<iLBCenc_inst->blockl; k++) block[k] = (float)data[k]; /* do the actual encoding */ iLBC_encode((unsigned char *)encoded_data, block, iLBCenc_inst); return (iLBCenc_inst->no_of_bytes); }
static void enc_process(MSFilter *f){ EncState *s=(EncState*)f->data; mblk_t *im,*om; int size=s->nsamples*2; int16_t samples[1610]; /* BLOCKL_MAX * 7 is the largest size for ptime == 140 */ float samples2[BLOCKL_MAX]; int i; int frame_per_packet=1; if (s->ptime>=20 && s->ms_per_frame>0 && s->ptime%s->ms_per_frame==0) { frame_per_packet = s->ptime/s->ms_per_frame; } if (frame_per_packet<=0) frame_per_packet=1; if (frame_per_packet>7) /* 7*20 == 140 ms max */ frame_per_packet=7; while((im=ms_queue_get(f->inputs[0]))!=NULL){ ms_bufferizer_put(s->bufferizer,im); } while(ms_bufferizer_read(s->bufferizer,(uint8_t*)samples,size*frame_per_packet)==(size*frame_per_packet)){ int k; om=allocb(s->nbytes*frame_per_packet,0); for (k=0;k<frame_per_packet;k++) { for (i=0;i<s->nsamples;i++){ samples2[i]=samples[i+(s->nsamples*k)]; } iLBC_encode((uint8_t*)om->b_wptr,samples2,&s->ilbc_enc); om->b_wptr+=s->nbytes; } s->ts+=s->nsamples*frame_per_packet; mblk_set_timestamp_info(om,s->ts); ms_bufferizer_fill_current_metas(s->bufferizer,om); ms_queue_put(f->outputs[0],om); } }
void ms_ilbc_encoder_process(MSILBCEncoder *r) { MSFifo *fi; MSQueue *qo; MSMessage *m; void *src=NULL; float speech[ILBC_MAX_SAMPLES_PER_FRAME]; /* process output fifos, but there is only one for this class of filter*/ qo=r->q_outputs[0]; fi=r->f_inputs[0]; ms_fifo_get_read_ptr(fi,r->samples_per_frame*2,&src); if (src==NULL) { g_warning( "src=%p\n", src); return; } m=ms_message_new(r->bytes_per_compressed_frame); ilbc_read_16bit_samples((gint16*)src, speech, r->samples_per_frame); iLBC_encode((unsigned char *)m->data, speech, &r->ilbc_enc); ms_queue_put(qo,m); }