/*********************************************************************** * Encode *********************************************************************** * **********************************************************************/ static hb_buffer_t * Encode( hb_work_object_t * w ) { hb_work_private_t * pv = w->private_data; hb_audio_t * audio = w->audio; hb_buffer_t * buf; float samples[2][1152]; uint64_t pts, pos; int i, j; if( hb_list_bytes( pv->list ) < pv->input_samples * sizeof( float ) ) { return NULL; } hb_list_getbytes( pv->list, pv->buf, pv->input_samples * sizeof( float ), &pts, &pos); for( i = 0; i < 1152; i++ ) { for( j = 0; j < pv->out_discrete_channels; j++ ) { samples[j][i] = ((float *) pv->buf)[(pv->out_discrete_channels * i + j)]; } } buf = hb_buffer_init( pv->output_bytes ); buf->s.start = pts + 90000 * pos / pv->out_discrete_channels / sizeof( float ) / audio->config.out.samplerate; buf->s.stop = buf->s.start + 90000 * 1152 / audio->config.out.samplerate; pv->pts = buf->s.stop; buf->size = lame_encode_buffer_float( pv->lame, samples[0], samples[1], 1152, buf->data, LAME_MAXMP3BUFFER ); buf->s.type = AUDIO_BUF; buf->s.frametype = HB_FRAME_AUDIO; if( !buf->size ) { /* Encoding was successful but we got no data. Try to encode more */ hb_buffer_close( &buf ); return Encode( w ); } else if( buf->size < 0 ) { hb_log( "enclame: lame_encode_buffer failed" ); hb_buffer_close( &buf ); return NULL; } return buf; }
static bool lame_encoder_write(struct encoder *_encoder, const void *data, size_t length, G_GNUC_UNUSED GError **error) { struct lame_encoder *encoder = (struct lame_encoder *)_encoder; unsigned num_frames; float *left, *right; const int16_t *src = (const int16_t*)data; unsigned int i; int bytes_out; assert(encoder->buffer_length == 0); num_frames = length / audio_format_frame_size(&encoder->audio_format); left = g_malloc(sizeof(left[0]) * num_frames); right = g_malloc(sizeof(right[0]) * num_frames); /* this is for only 16-bit audio */ for (i = 0; i < num_frames; i++) { left[i] = *src++; right[i] = *src++; } bytes_out = lame_encode_buffer_float(encoder->gfp, left, right, num_frames, encoder->buffer, sizeof(encoder->buffer)); g_free(left); g_free(right); if (bytes_out < 0) { g_set_error(error, lame_encoder_quark(), 0, "lame encoder failed"); return false; } encoder->buffer_length = (size_t)bytes_out; return true; }
int encode(float const* ch0, float const* ch1, int n_in, unsigned char* out_buffer, int m_out_free) { ensureInitialized(); return lame_encode_buffer_float(m_gf, ch0, ch1, n_in, out_buffer, m_out_free); }