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 unsigned long input_samples;
32 unsigned long output_bytes;
39 int enclameInit( hb_work_object_t * w, hb_job_t * job )
41 hb_work_private_t * pv = calloc( 1, sizeof( hb_work_private_t ) );
42 hb_audio_t * audio = w->audio;
47 hb_log( "enclame: opening libmp3lame" );
49 pv->lame = lame_init();
50 lame_set_brate( pv->lame, audio->config.out.bitrate );
51 lame_set_in_samplerate( pv->lame, audio->config.out.samplerate );
52 lame_set_out_samplerate( pv->lame, audio->config.out.samplerate );
53 lame_init_params( pv->lame );
55 pv->input_samples = 1152 * 2;
56 pv->output_bytes = LAME_MAXMP3BUFFER;
57 pv->buf = malloc( pv->input_samples * sizeof( float ) );
59 pv->list = hb_list_init();
65 /***********************************************************************
67 ***********************************************************************
69 **********************************************************************/
70 void enclameClose( hb_work_object_t * w )
72 hb_work_private_t * pv = w->private_data;
74 lame_close( pv->lame );
75 hb_list_empty( &pv->list );
78 w->private_data = NULL;
81 /***********************************************************************
83 ***********************************************************************
85 **********************************************************************/
86 static hb_buffer_t * Encode( hb_work_object_t * w )
88 hb_work_private_t * pv = w->private_data;
89 hb_audio_t * audio = w->audio;
91 int16_t samples_s16[1152 * 2];
95 if( hb_list_bytes( pv->list ) < pv->input_samples * sizeof( float ) )
100 hb_list_getbytes( pv->list, pv->buf, pv->input_samples * sizeof( float ),
103 for( i = 0; i < 1152 * 2; i++ )
105 samples_s16[i] = ((float*) pv->buf)[i];
108 buf = hb_buffer_init( pv->output_bytes );
109 buf->start = pts + 90000 * pos / 2 / sizeof( float ) / audio->config.out.samplerate;
110 buf->stop = buf->start + 90000 * 1152 / audio->config.out.samplerate;
111 buf->size = lame_encode_buffer_interleaved( pv->lame, samples_s16,
112 1152, buf->data, LAME_MAXMP3BUFFER );
113 buf->frametype = HB_FRAME_AUDIO;
117 /* Encoding was successful but we got no data. Try to encode
119 hb_buffer_close( &buf );
122 else if( buf->size < 0 )
124 hb_log( "enclame: lame_encode_buffer failed" );
125 hb_buffer_close( &buf );
132 /***********************************************************************
134 ***********************************************************************
136 **********************************************************************/
137 int enclameWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
138 hb_buffer_t ** buf_out )
140 hb_work_private_t * pv = w->private_data;
143 hb_list_add( pv->list, *buf_in );
146 *buf_out = buf = Encode( w );
150 buf->next = Encode( w );