X-Git-Url: http://git.osdn.jp/view?a=blobdiff_plain;ds=sidebyside;f=libhb%2Fenclame.c;h=9692eef29e31a59511835583e270344ff58c7f96;hb=4b72a63eb61a01275493c4bfb51ba02152d1c5e1;hp=35b35ff501bb41e3ffe58fe674c0f0d6953c9fbe;hpb=90fba27222b3467b91c29803f217c4d5335c7ad0;p=handbrake-jp%2Fhandbrake-jp-git.git
diff --git a/libhb/enclame.c b/libhb/enclame.c
index 35b35ff5..9692eef2 100644
--- a/libhb/enclame.c
+++ b/libhb/enclame.c
@@ -1,7 +1,7 @@
/* $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"
@@ -28,6 +28,8 @@ struct hb_work_private_s
/* LAME handle */
lame_global_flags * lame;
+ int done;
+ int out_discrete_channels;
unsigned long input_samples;
unsigned long output_bytes;
uint8_t * buf;
@@ -39,6 +41,8 @@ struct hb_work_private_s
int enclameInit( hb_work_object_t * w, hb_job_t * job )
{
hb_work_private_t * pv = calloc( 1, sizeof( hb_work_private_t ) );
+ hb_audio_t * audio = w->audio;
+
w->private_data = pv;
pv->job = job;
@@ -46,12 +50,30 @@ int enclameInit( hb_work_object_t * w, hb_job_t * job )
hb_log( "enclame: opening libmp3lame" );
pv->lame = lame_init();
- lame_set_brate( pv->lame, job->abitrate );
- lame_set_in_samplerate( pv->lame, job->arate );
- lame_set_out_samplerate( pv->lame, job->arate );
+ // use ABR
+ lame_set_VBR( pv->lame, vbr_abr );
+ lame_set_VBR_mean_bitrate_kbps( 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 );
+
+ pv->out_discrete_channels = HB_AMIXDOWN_GET_DISCRETE_CHANNEL_COUNT(audio->config.out.mixdown);
+ // Lame's default encoding mode is JOINT_STEREO. This subtracts signal
+ // that is "common" to left and right (within some threshold) and encodes
+ // it separately. This improves quality at low bitrates, but hurts
+ // imaging (channel separation) at higher bitrates. So if the bitrate
+ // is suffeciently high, use regular STEREO mode.
+ if ( pv->out_discrete_channels == 1 )
+ {
+ lame_set_mode( pv->lame, MONO );
+ lame_set_num_channels( pv->lame, 1 );
+ }
+ else if ( audio->config.out.bitrate >= 128 )
+ {
+ lame_set_mode( pv->lame, STEREO );
+ }
lame_init_params( pv->lame );
- pv->input_samples = 1152 * 2;
+ pv->input_samples = 1152 * pv->out_discrete_channels;
pv->output_bytes = LAME_MAXMP3BUFFER;
pv->buf = malloc( pv->input_samples * sizeof( float ) );
@@ -85,10 +107,11 @@ void enclameClose( 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];
+ float samples[2][1152];
uint64_t pts, pos;
- int i;
+ int i, j;
if( hb_list_bytes( pv->list ) < pv->input_samples * sizeof( float ) )
{
@@ -98,16 +121,22 @@ static hb_buffer_t * Encode( hb_work_object_t * w )
hb_list_getbytes( pv->list, pv->buf, pv->input_samples * sizeof( float ),
&pts, &pos);
- for( i = 0; i < 1152 * 2; i++ )
+ memset(samples, 0, 2*1152*sizeof(float));
+ for( i = 0; i < 1152; i++ )
{
- samples_s16[i] = ((float*) pv->buf)[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->start = pts + 90000 * pos / 2 / sizeof( float ) / pv->job->arate;
- buf->stop = buf->start + 90000 * 1152 / pv->job->arate;
- buf->size = lame_encode_buffer_interleaved( pv->lame, samples_s16,
+ buf->start = pts + 90000 * pos / pv->out_discrete_channels / sizeof( float ) / audio->config.out.samplerate;
+ buf->stop = buf->start + 90000 * 1152 / audio->config.out.samplerate;
+ buf->size = lame_encode_buffer_float(
+ pv->lame, samples[0], samples[1],
1152, buf->data, LAME_MAXMP3BUFFER );
+
buf->frametype = HB_FRAME_AUDIO;
if( !buf->size )
@@ -136,8 +165,35 @@ 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;
+ 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;