1 /*****************************************************************************
2 * toolame.c: libtoolame encoder (MPEG-1/2 layer II) module
3 * (using libtoolame from http://users.tpg.com.au/adslblvi/)
4 *****************************************************************************
5 * Copyright (C) 2004 VideoLAN
8 * Authors: Christophe Massiot <massiot@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
29 #include <vlc/decoder.h>
35 #define MPEG_FRAME_SIZE 1152
36 #define MAX_CODED_FRAME_SIZE 1792
38 /****************************************************************************
40 ****************************************************************************/
41 static int OpenEncoder ( vlc_object_t * );
42 static void CloseEncoder ( vlc_object_t * );
43 static block_t *Encode ( encoder_t *, aout_buffer_t * );
45 /*****************************************************************************
47 *****************************************************************************/
48 #define ENC_CFG_PREFIX "sout-toolame-"
50 #define ENC_QUALITY_TEXT N_("Encoding quality")
51 #define ENC_QUALITY_LONGTEXT N_( \
52 "Allows you to specify a quality between 0.0 (high) and 50.0 (low), " \
53 "instead of specifying a particular bitrate. " \
54 "This will produce a VBR stream." )
55 #define ENC_MODE_TEXT N_("Stereo mode")
56 #define ENC_MODE_LONGTEXT N_( \
57 "[0=stereo, 1=dual-mono, 2=joint-stereo]" )
58 #define ENC_VBR_TEXT N_("VBR mode")
59 #define ENC_VBR_LONGTEXT N_( \
60 "By default the encoding is CBR." )
63 set_description( _("libtoolame audio encoder") );
64 set_capability( "encoder", 50 );
65 set_callbacks( OpenEncoder, CloseEncoder );
67 add_float( ENC_CFG_PREFIX "quality", 0.0, NULL, ENC_QUALITY_TEXT,
68 ENC_QUALITY_LONGTEXT, VLC_FALSE );
69 add_integer( ENC_CFG_PREFIX "mode", 0, NULL, ENC_MODE_TEXT,
70 ENC_MODE_LONGTEXT, VLC_FALSE );
71 add_bool( ENC_CFG_PREFIX "vbr", 0, NULL, ENC_VBR_TEXT,
72 ENC_VBR_LONGTEXT, VLC_FALSE );
75 static const char *ppsz_enc_options[] = {
76 "quality", "mode", "vbr", NULL
79 /*****************************************************************************
80 * encoder_sys_t : toolame encoder descriptor
81 *****************************************************************************/
87 int16_t p_left[MPEG_FRAME_SIZE];
88 int16_t p_right[MPEG_FRAME_SIZE];
93 * libtoolame properties
95 toolame_options *p_toolame;
96 unsigned char p_out_buffer[MAX_CODED_FRAME_SIZE];
99 /*****************************************************************************
100 * OpenEncoder: probe the encoder and return score
101 *****************************************************************************/
102 static int OpenEncoder( vlc_object_t *p_this )
104 encoder_t *p_enc = (encoder_t *)p_this;
105 encoder_sys_t *p_sys;
108 if( p_enc->fmt_out.i_codec != VLC_FOURCC('m','p','g','a') &&
109 p_enc->fmt_out.i_codec != VLC_FOURCC('m','p','2','a') &&
110 p_enc->fmt_out.i_codec != VLC_FOURCC('m','p','2',' ') &&
116 if( p_enc->fmt_in.audio.i_channels > 2 )
118 msg_Err( p_enc, "doesn't support > 2 channels" );
122 /* Allocate the memory needed to store the decoder's structure */
123 if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
125 msg_Err( p_enc, "out of memory" );
128 p_enc->p_sys = p_sys;
130 p_enc->pf_encode_audio = Encode;
131 p_enc->fmt_in.i_codec = AOUT_FMT_S16_NE;
132 p_enc->fmt_out.i_codec = VLC_FOURCC('m','p','g','a');
134 sout_CfgParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
136 p_sys->p_toolame = toolame_init();
139 toolame_setSampleFreq( p_sys->p_toolame, p_enc->fmt_out.audio.i_rate );
141 var_Get( p_enc, ENC_CFG_PREFIX "vbr", &val );
145 var_Get( p_enc, ENC_CFG_PREFIX "quality", &val );
146 i_quality = val.i_int;
147 if ( i_quality > 50.0 ) i_quality = 50.0;
148 if ( i_quality < 0.0 ) i_quality = 0.0;
149 toolame_setVBR( p_sys->p_toolame, 1 );
150 toolame_setVBRLevel( p_sys->p_toolame, i_quality );
154 toolame_setBitrate( p_sys->p_toolame, p_enc->fmt_out.i_bitrate / 1000 );
157 if ( p_enc->fmt_in.audio.i_channels == 1 )
159 toolame_setMode( p_sys->p_toolame, MPG_MD_MONO );
163 var_Get( p_enc, ENC_CFG_PREFIX "mode", &val );
167 toolame_setMode( p_sys->p_toolame, MPG_MD_DUAL_CHANNEL );
170 toolame_setMode( p_sys->p_toolame, MPG_MD_JOINT_STEREO );
174 toolame_setMode( p_sys->p_toolame, MPG_MD_STEREO );
179 if ( toolame_init_params( p_sys->p_toolame ) )
181 msg_Err( p_enc, "toolame initialization failed" );
182 return -VLC_EGENERIC;
185 p_sys->i_nb_samples = 0;
186 aout_DateInit( &p_sys->pts, p_enc->fmt_out.audio.i_rate );
191 /****************************************************************************
192 * Encode: the whole thing
193 ****************************************************************************
194 * This function spits out MPEG packets.
195 ****************************************************************************/
196 static void Uninterleave( encoder_t *p_enc, int16_t *p_in, int i_nb_samples )
198 int16_t *p_left = p_enc->p_sys->p_left + p_enc->p_sys->i_nb_samples;
199 int16_t *p_right = p_enc->p_sys->p_right + p_enc->p_sys->i_nb_samples;
201 while ( i_nb_samples > 0 )
204 *p_right++ = *p_in++;
209 static block_t *Encode( encoder_t *p_enc, aout_buffer_t *p_aout_buf )
211 encoder_sys_t *p_sys = p_enc->p_sys;
212 int16_t *p_buffer = (int16_t *)p_aout_buf->p_buffer;
213 int i_nb_samples = p_aout_buf->i_nb_samples;
214 block_t *p_chain = NULL;
215 mtime_t i_computed_pts = p_aout_buf->start_date -
216 (mtime_t)1000000 * (mtime_t)p_sys->i_nb_samples /
217 (mtime_t)p_enc->fmt_in.audio.i_rate;
219 if ( aout_DateGet( &p_sys->pts ) - i_computed_pts > 10000 ||
220 aout_DateGet( &p_sys->pts ) - i_computed_pts < -10000 )
222 msg_Dbg( p_enc, "resetting audio date" );
223 aout_DateSet( &p_sys->pts, i_computed_pts );
226 while ( p_sys->i_nb_samples + i_nb_samples >= MPEG_FRAME_SIZE )
231 Uninterleave( p_enc, p_buffer, MPEG_FRAME_SIZE - p_sys->i_nb_samples );
232 i_nb_samples -= MPEG_FRAME_SIZE - p_sys->i_nb_samples;
233 p_buffer += (MPEG_FRAME_SIZE - p_sys->i_nb_samples) * 2;
235 toolame_encode_buffer( p_sys->p_toolame, p_sys->p_left,
236 p_sys->p_right, MPEG_FRAME_SIZE,
237 p_sys->p_out_buffer, MAX_CODED_FRAME_SIZE,
239 p_sys->i_nb_samples = 0;
240 p_block = block_New( p_enc, i_used );
241 p_enc->p_vlc->pf_memcpy( p_block->p_buffer, p_sys->p_out_buffer,
243 p_block->i_length = (mtime_t)1000000 *
244 (mtime_t)MPEG_FRAME_SIZE / (mtime_t)p_enc->fmt_in.audio.i_rate;
245 p_block->i_dts = p_block->i_pts = aout_DateGet( &p_sys->pts );
246 aout_DateIncrement( &p_sys->pts, MPEG_FRAME_SIZE );
247 block_ChainAppend( &p_chain, p_block );
252 Uninterleave( p_enc, p_buffer, i_nb_samples );
253 p_sys->i_nb_samples += i_nb_samples;
259 /*****************************************************************************
260 * CloseEncoder: toolame encoder destruction
261 *****************************************************************************/
262 static void CloseEncoder( vlc_object_t *p_this )
264 encoder_t *p_enc = (encoder_t *)p_this;
265 encoder_sys_t *p_sys = p_enc->p_sys;
267 toolame_deinit( p_sys->p_toolame );