1 /* $Id: enclame.c,v 1.9 2005/03/05 14:27:05 titer Exp $
3 This file is part of the HandBrake source code.
4 Homepage: <http://handbrake.fr/>.
5 It may be used under the terms of the GNU General Public License. */
11 int enclameInit( hb_work_object_t *, hb_job_t * );
12 int enclameWork( hb_work_object_t *, hb_buffer_t **, hb_buffer_t ** );
13 void enclameClose( hb_work_object_t * );
15 hb_work_object_t hb_enclame =
18 "MP3 encoder (libmp3lame)",
24 struct hb_work_private_s
29 lame_global_flags * lame;
31 int out_discrete_channels;
32 unsigned long input_samples;
33 unsigned long output_bytes;
40 int enclameInit( hb_work_object_t * w, hb_job_t * job )
42 hb_work_private_t * pv = calloc( 1, sizeof( hb_work_private_t ) );
43 hb_audio_t * audio = w->audio;
49 hb_log( "enclame: opening libmp3lame" );
51 pv->lame = lame_init();
53 lame_set_VBR( pv->lame, vbr_abr );
54 lame_set_VBR_mean_bitrate_kbps( pv->lame, audio->config.out.bitrate );
55 lame_set_in_samplerate( pv->lame, audio->config.out.samplerate );
56 lame_set_out_samplerate( pv->lame, audio->config.out.samplerate );
58 pv->out_discrete_channels = HB_AMIXDOWN_GET_DISCRETE_CHANNEL_COUNT(audio->config.out.mixdown);
59 // Lame's default encoding mode is JOINT_STEREO. This subtracts signal
60 // that is "common" to left and right (within some threshold) and encodes
61 // it separately. This improves quality at low bitrates, but hurts
62 // imaging (channel separation) at higher bitrates. So if the bitrate
63 // is suffeciently high, use regular STEREO mode.
64 if ( pv->out_discrete_channels == 1 )
66 lame_set_mode( pv->lame, MONO );
67 lame_set_num_channels( pv->lame, 1 );
69 else if ( audio->config.out.bitrate >= 128 )
71 lame_set_mode( pv->lame, STEREO );
73 lame_init_params( pv->lame );
75 pv->input_samples = 1152 * pv->out_discrete_channels;
76 pv->output_bytes = LAME_MAXMP3BUFFER;
77 pv->buf = malloc( pv->input_samples * sizeof( float ) );
79 pv->list = hb_list_init();
85 /***********************************************************************
87 ***********************************************************************
89 **********************************************************************/
90 void enclameClose( hb_work_object_t * w )
92 hb_work_private_t * pv = w->private_data;
94 lame_close( pv->lame );
95 hb_list_empty( &pv->list );
98 w->private_data = NULL;
101 /***********************************************************************
103 ***********************************************************************
105 **********************************************************************/
106 static hb_buffer_t * Encode( hb_work_object_t * w )
108 hb_work_private_t * pv = w->private_data;
109 hb_audio_t * audio = w->audio;
111 float samples[2][1152];
115 if( hb_list_bytes( pv->list ) < pv->input_samples * sizeof( float ) )
120 hb_list_getbytes( pv->list, pv->buf, pv->input_samples * sizeof( float ),
123 memset(samples, 0, 2*1152*sizeof(float));
124 for( i = 0; i < 1152; i++ )
126 for( j = 0; j < pv->out_discrete_channels; j++ )
128 samples[j][i] = ((float *) pv->buf)[(pv->out_discrete_channels * i + j)];
132 buf = hb_buffer_init( pv->output_bytes );
133 buf->start = pts + 90000 * pos / pv->out_discrete_channels / sizeof( float ) / audio->config.out.samplerate;
134 buf->stop = buf->start + 90000 * 1152 / audio->config.out.samplerate;
136 buf->size = lame_encode_buffer_float(
137 pv->lame, samples[0], samples[1],
138 1152, buf->data, LAME_MAXMP3BUFFER );
140 buf->frametype = HB_FRAME_AUDIO;
144 /* Encoding was successful but we got no data. Try to encode
146 hb_buffer_close( &buf );
149 else if( buf->size < 0 )
151 hb_log( "enclame: lame_encode_buffer failed" );
152 hb_buffer_close( &buf );
158 /***********************************************************************
160 ***********************************************************************
162 **********************************************************************/
163 int enclameWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
164 hb_buffer_t ** buf_out )
166 hb_work_private_t * pv = w->private_data;
167 hb_audio_t * audio = w->audio;
168 hb_buffer_t * in = *buf_in;
171 if ( (*buf_in)->size <= 0 )
173 /* EOF on input - send it downstream & say we're done */
175 buf = hb_buffer_init( pv->output_bytes );
176 buf->size = lame_encode_flush( pv->lame, buf->data, LAME_MAXMP3BUFFER );
177 buf->start = pv->pts;
178 buf->stop = buf->start + 90000 * 1152 / audio->config.out.samplerate;
179 buf->frametype = HB_FRAME_AUDIO;
182 hb_buffer_close( &buf );
185 // Add the flushed data
202 hb_list_add( pv->list, *buf_in );
205 *buf_out = buf = Encode( w );
209 buf->next = Encode( w );