X-Git-Url: http://git.osdn.jp/view?a=blobdiff_plain;f=libhb%2Fenclame.c;h=027aa3add0645a82f98a7e2622f1a30a08181465;hb=1c9c75d452c8d15ee972b4c4612763f9b95b1790;hp=19014ac26ba812cf02b81c4d1c79810ffbd0dfdd;hpb=cdba71d1dca214142fdcca4c52f7672252752c2e;p=handbrake-jp%2Fhandbrake-jp-git.git diff --git a/libhb/enclame.c b/libhb/enclame.c index 19014ac2..027aa3ad 100644 --- a/libhb/enclame.c +++ b/libhb/enclame.c @@ -1,23 +1,34 @@ /* $Id: enclame.c,v 1.9 2005/03/05 14:27:05 titer Exp $ This file is part of the HandBrake source code. - Homepage: . + Homepage: . It may be used under the terms of the GNU General Public License. */ #include "hb.h" #include "lame/lame.h" -struct hb_work_object_s +int enclameInit( hb_work_object_t *, hb_job_t * ); +int enclameWork( hb_work_object_t *, hb_buffer_t **, hb_buffer_t ** ); +void enclameClose( hb_work_object_t * ); + +hb_work_object_t hb_enclame = { - HB_WORK_COMMON; + WORK_ENCLAME, + "MP3 encoder (libmp3lame)", + enclameInit, + enclameWork, + enclameClose +}; +struct hb_work_private_s +{ hb_job_t * job; - hb_audio_t * audio; /* LAME handle */ lame_global_flags * lame; + int done; unsigned long input_samples; unsigned long output_bytes; uint8_t * buf; @@ -26,44 +37,30 @@ struct hb_work_object_s int64_t pts; }; -/*********************************************************************** - * Local prototypes - **********************************************************************/ -static void Close( hb_work_object_t ** _w ); -static int Work( hb_work_object_t * w, hb_buffer_t ** buf_in, - hb_buffer_t ** buf_out ); - -/*********************************************************************** - * hb_work_enclame_init - *********************************************************************** - * - **********************************************************************/ -hb_work_object_t * hb_work_enclame_init( hb_job_t * job, hb_audio_t * audio ) +int enclameInit( hb_work_object_t * w, hb_job_t * job ) { - hb_work_object_t * w = calloc( sizeof( hb_work_object_t ), 1 ); - w->name = strdup( "MP3 encoder (libmp3lame)" ); - w->work = Work; - w->close = Close; + hb_work_private_t * pv = calloc( 1, sizeof( hb_work_private_t ) ); + hb_audio_t * audio = w->audio; + w->private_data = pv; - w->job = job; - w->audio = audio; + pv->job = job; hb_log( "enclame: opening libmp3lame" ); - w->lame = lame_init(); - lame_set_brate( w->lame, job->abitrate ); - lame_set_in_samplerate( w->lame, job->arate ); - lame_set_out_samplerate( w->lame, job->arate ); - lame_init_params( w->lame ); - - w->input_samples = 1152 * 2; - w->output_bytes = LAME_MAXMP3BUFFER; - w->buf = malloc( w->input_samples * sizeof( float ) ); + pv->lame = lame_init(); + lame_set_brate( pv->lame, audio->config.out.bitrate ); + lame_set_in_samplerate( pv->lame, audio->config.out.samplerate ); + lame_set_out_samplerate( pv->lame, audio->config.out.samplerate ); + lame_init_params( pv->lame ); - w->list = hb_list_init(); - w->pts = -1; + pv->input_samples = 1152 * 2; + pv->output_bytes = LAME_MAXMP3BUFFER; + pv->buf = malloc( pv->input_samples * sizeof( float ) ); - return w; + pv->list = hb_list_init(); + pv->pts = -1; + + return 0; } /*********************************************************************** @@ -71,12 +68,15 @@ hb_work_object_t * hb_work_enclame_init( hb_job_t * job, hb_audio_t * audio ) *********************************************************************** * **********************************************************************/ -static void Close( hb_work_object_t ** _w ) +void enclameClose( hb_work_object_t * w ) { - hb_work_object_t * w = *_w; - free( w->name ); - free( w ); - *_w = NULL; + hb_work_private_t * pv = w->private_data; + + lame_close( pv->lame ); + hb_list_empty( &pv->list ); + free( pv->buf ); + free( pv ); + w->private_data = NULL; } /*********************************************************************** @@ -86,30 +86,32 @@ static void Close( hb_work_object_t ** _w ) **********************************************************************/ 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; int16_t samples_s16[1152 * 2]; - uint64_t pts; - int pos, i; + uint64_t pts, pos; + int i; - if( hb_list_bytes( w->list ) < w->input_samples * sizeof( float ) ) + if( hb_list_bytes( pv->list ) < pv->input_samples * sizeof( float ) ) { return NULL; } - hb_list_getbytes( w->list, w->buf, w->input_samples * sizeof( float ), + hb_list_getbytes( pv->list, pv->buf, pv->input_samples * sizeof( float ), &pts, &pos); for( i = 0; i < 1152 * 2; i++ ) { - samples_s16[i] = ((float*) w->buf)[i]; + samples_s16[i] = ((float*) pv->buf)[i]; } - buf = hb_buffer_init( w->output_bytes ); - buf->start = pts + 90000 * pos / 2 / sizeof( float ) / w->job->arate; - buf->stop = buf->start + 90000 * 1152 / w->job->arate; - buf->size = lame_encode_buffer_interleaved( w->lame, samples_s16, + buf = hb_buffer_init( pv->output_bytes ); + buf->start = pts + 90000 * pos / 2 / sizeof( float ) / audio->config.out.samplerate; + buf->stop = buf->start + 90000 * 1152 / audio->config.out.samplerate; + buf->size = lame_encode_buffer_interleaved( pv->lame, samples_s16, 1152, buf->data, LAME_MAXMP3BUFFER ); - buf->key = 1; + buf->frametype = HB_FRAME_AUDIO; if( !buf->size ) { @@ -133,12 +135,40 @@ static hb_buffer_t * Encode( hb_work_object_t * w ) *********************************************************************** * **********************************************************************/ -static int Work( hb_work_object_t * w, hb_buffer_t ** buf_in, +int enclameWork( hb_work_object_t * w, hb_buffer_t ** buf_in, hb_buffer_t ** buf_out ) { + hb_work_private_t * pv = w->private_data; + hb_buffer_t * in = *buf_in; hb_buffer_t * buf; - hb_list_add( w->list, *buf_in ); + if ( (*buf_in)->size <= 0 ) + { + /* EOF on input - send it downstream & say we're done */ + if ( pv->done ) + { + *buf_out = *buf_in; + *buf_in = NULL; + return HB_WORK_DONE; + } + else + { + pv->done = 1; + hb_fifo_push( w->fifo_in, in); + *buf_in = NULL; + + buf = hb_buffer_init( pv->output_bytes ); + buf->size = lame_encode_flush( pv->lame, buf->data, LAME_MAXMP3BUFFER ); + if( buf->size <= 0 ) + { + hb_buffer_close( &buf ); + } + *buf_out = buf; + return HB_WORK_OK; + } + } + + hb_list_add( pv->list, *buf_in ); *buf_in = NULL; *buf_out = buf = Encode( w );