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;
32 int out_discrete_channels;
33 unsigned long input_samples;
34 unsigned long output_bytes;
41 int enclameInit( hb_work_object_t * w, hb_job_t * job )
43 hb_work_private_t * pv = calloc( 1, sizeof( hb_work_private_t ) );
44 hb_audio_t * audio = w->audio;
50 hb_log( "enclame: opening libmp3lame" );
52 pv->lame = lame_init();
54 lame_set_VBR( pv->lame, vbr_abr );
55 lame_set_VBR_mean_bitrate_kbps( pv->lame, audio->config.out.bitrate );
56 lame_set_in_samplerate( pv->lame, audio->config.out.samplerate );
57 lame_set_out_samplerate( pv->lame, audio->config.out.samplerate );
59 pv->out_discrete_channels = HB_AMIXDOWN_GET_DISCRETE_CHANNEL_COUNT(audio->config.out.mixdown);
60 // Lame's default encoding mode is JOINT_STEREO. This subtracts signal
61 // that is "common" to left and right (within some threshold) and encodes
62 // it separately. This improves quality at low bitrates, but hurts
63 // imaging (channel separation) at higher bitrates. So if the bitrate
64 // is suffeciently high, use regular STEREO mode.
65 if ( pv->out_discrete_channels == 1 )
67 lame_set_mode( pv->lame, MONO );
68 lame_set_num_channels( pv->lame, 1 );
70 else if ( audio->config.out.bitrate >= 128 )
72 lame_set_mode( pv->lame, STEREO );
74 lame_init_params( pv->lame );
76 pv->input_samples = 1152 * pv->out_discrete_channels;
77 pv->output_bytes = LAME_MAXMP3BUFFER;
78 pv->buf = malloc( pv->input_samples * sizeof( float ) );
80 pv->list = hb_list_init();
86 /***********************************************************************
88 ***********************************************************************
90 **********************************************************************/
91 void enclameClose( hb_work_object_t * w )
93 hb_work_private_t * pv = w->private_data;
95 lame_close( pv->lame );
96 hb_list_empty( &pv->list );
99 w->private_data = NULL;
102 /***********************************************************************
104 ***********************************************************************
106 **********************************************************************/
107 static hb_buffer_t * Encode( hb_work_object_t * w )
109 hb_work_private_t * pv = w->private_data;
110 hb_audio_t * audio = w->audio;
112 float samples[2][1152];
116 if( hb_list_bytes( pv->list ) < pv->input_samples * sizeof( float ) )
121 hb_list_getbytes( pv->list, pv->buf, pv->input_samples * sizeof( float ),
124 memset(samples, 0, 2*1152*sizeof(float));
125 for( i = 0; i < 1152; i++ )
127 for( j = 0; j < pv->out_discrete_channels; j++ )
129 samples[j][i] = ((float *) pv->buf)[(pv->out_discrete_channels * i + j)];
133 buf = hb_buffer_init( pv->output_bytes );
134 buf->start = pts + 90000 * pos / pv->out_discrete_channels / sizeof( float ) / audio->config.out.samplerate;
135 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 );
159 /***********************************************************************
161 ***********************************************************************
163 **********************************************************************/
164 int enclameWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
165 hb_buffer_t ** buf_out )
167 hb_work_private_t * pv = w->private_data;
168 hb_buffer_t * in = *buf_in;
171 if ( (*buf_in)->size <= 0 )
173 /* EOF on input - send it downstream & say we're done */
183 hb_fifo_push( w->fifo_in, in);
186 buf = hb_buffer_init( pv->output_bytes );
187 buf->size = lame_encode_flush( pv->lame, buf->data, LAME_MAXMP3BUFFER );
190 hb_buffer_close( &buf );
197 hb_list_add( pv->list, *buf_in );
200 *buf_out = buf = Encode( w );
204 buf->next = Encode( w );