00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include <vlc/vlc.h>
00029 #include <vlc/decoder.h>
00030 #include <vlc/sout.h>
00031 #include <vlc/aout.h>
00032
00033 #include <twolame.h>
00034
00035 #define MPEG_FRAME_SIZE 1152
00036 #define MAX_CODED_FRAME_SIZE 1792
00037
00038
00039
00040
00041 static int OpenEncoder ( vlc_object_t * );
00042 static void CloseEncoder ( vlc_object_t * );
00043 static block_t *Encode ( encoder_t *, aout_buffer_t * );
00044
00045
00046
00047
00048 #define ENC_CFG_PREFIX "sout-twolame-"
00049
00050 #define ENC_QUALITY_TEXT N_("Encoding quality")
00051 #define ENC_QUALITY_LONGTEXT N_( \
00052 "Allows you to specify a quality between 0.0 (high) and 50.0 (low), " \
00053 "instead of specifying a particular bitrate. " \
00054 "This will produce a VBR stream." )
00055 #define ENC_MODE_TEXT N_("Stereo mode")
00056 #define ENC_MODE_LONGTEXT N_( "Select how stereo streams will be handled" )
00057 #define ENC_VBR_TEXT N_("VBR mode")
00058 #define ENC_VBR_LONGTEXT N_( \
00059 "By default the encoding is CBR." )
00060 #define ENC_PSY_TEXT N_("Psycho-acoustic model")
00061 #define ENC_PSY_LONGTEXT N_( \
00062 "Integer from -1 (no model) to 4." )
00063
00064 static int pi_stereo_values[] = { 0, 1, 2 };
00065 static char *ppsz_stereo_descriptions[] =
00066 { N_("Stereo"), N_("Dual mono"), N_("Joint stereo") };
00067
00068
00069 vlc_module_begin();
00070 set_shortname( "Twolame");
00071 set_description( _("Libtwolame audio encoder") );
00072 set_capability( "encoder", 50 );
00073 set_callbacks( OpenEncoder, CloseEncoder );
00074 set_category( CAT_INPUT );
00075 set_subcategory( SUBCAT_INPUT_ACODEC );
00076
00077 add_float( ENC_CFG_PREFIX "quality", 0.0, NULL, ENC_QUALITY_TEXT,
00078 ENC_QUALITY_LONGTEXT, VLC_FALSE );
00079 add_integer( ENC_CFG_PREFIX "mode", 0, NULL, ENC_MODE_TEXT,
00080 ENC_MODE_LONGTEXT, VLC_FALSE );
00081 change_integer_list( pi_stereo_values, ppsz_stereo_descriptions, 0 );
00082 add_bool( ENC_CFG_PREFIX "vbr", 0, NULL, ENC_VBR_TEXT,
00083 ENC_VBR_LONGTEXT, VLC_FALSE );
00084 add_integer( ENC_CFG_PREFIX "psy", 3, NULL, ENC_PSY_TEXT,
00085 ENC_PSY_LONGTEXT, VLC_FALSE );
00086 vlc_module_end();
00087
00088 static const char *ppsz_enc_options[] = {
00089 "quality", "mode", "vbr", "psy", NULL
00090 };
00091
00092
00093
00094
00095 struct encoder_sys_t
00096 {
00097
00098
00099
00100 int16_t p_buffer[MPEG_FRAME_SIZE * 2];
00101 int i_nb_samples;
00102 mtime_t i_pts;
00103
00104
00105
00106
00107 twolame_options *p_twolame;
00108 unsigned char p_out_buffer[MAX_CODED_FRAME_SIZE];
00109 };
00110
00111
00112
00113
00114 static const uint16_t mpa_bitrate_tab[2][15] =
00115 {
00116 {0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384},
00117 {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160}
00118 };
00119
00120 static const uint16_t mpa_freq_tab[6] =
00121 { 44100, 48000, 32000, 22050, 24000, 16000 };
00122
00123 static int OpenEncoder( vlc_object_t *p_this )
00124 {
00125 encoder_t *p_enc = (encoder_t *)p_this;
00126 encoder_sys_t *p_sys;
00127 vlc_value_t val;
00128 int i_frequency;
00129
00130 if( p_enc->fmt_out.i_codec != VLC_FOURCC('m','p','g','a') &&
00131 p_enc->fmt_out.i_codec != VLC_FOURCC('m','p','2','a') &&
00132 p_enc->fmt_out.i_codec != VLC_FOURCC('m','p','2',' ') &&
00133 !p_enc->b_force )
00134 {
00135 return VLC_EGENERIC;
00136 }
00137
00138 if( p_enc->fmt_in.audio.i_channels > 2 )
00139 {
00140 msg_Err( p_enc, "doesn't support > 2 channels" );
00141 return VLC_EGENERIC;
00142 }
00143
00144 for ( i_frequency = 0; i_frequency < 6; i_frequency++ )
00145 {
00146 if ( p_enc->fmt_out.audio.i_rate == mpa_freq_tab[i_frequency] )
00147 break;
00148 }
00149 if ( i_frequency == 6 )
00150 {
00151 msg_Err( p_enc, "MPEG audio doesn't support frequency=%d",
00152 p_enc->fmt_out.audio.i_rate );
00153 return VLC_EGENERIC;
00154 }
00155
00156
00157 if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
00158 {
00159 msg_Err( p_enc, "out of memory" );
00160 return VLC_EGENERIC;
00161 }
00162 p_enc->p_sys = p_sys;
00163
00164 p_enc->pf_encode_audio = Encode;
00165 p_enc->fmt_in.i_codec = AOUT_FMT_S16_NE;
00166 p_enc->fmt_out.i_codec = VLC_FOURCC('m','p','g','a');
00167
00168 sout_CfgParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
00169
00170 p_sys->p_twolame = twolame_init();
00171
00172
00173 twolame_set_in_samplerate( p_sys->p_twolame, p_enc->fmt_out.audio.i_rate );
00174 twolame_set_out_samplerate( p_sys->p_twolame, p_enc->fmt_out.audio.i_rate );
00175
00176 var_Get( p_enc, ENC_CFG_PREFIX "vbr", &val );
00177 if ( val.b_bool )
00178 {
00179 float i_quality;
00180 var_Get( p_enc, ENC_CFG_PREFIX "quality", &val );
00181 i_quality = val.i_int;
00182 if ( i_quality > 50.0 ) i_quality = 50.0;
00183 if ( i_quality < 0.0 ) i_quality = 0.0;
00184 twolame_set_VBR( p_sys->p_twolame, 1 );
00185 twolame_set_VBR_q( p_sys->p_twolame, i_quality );
00186 }
00187 else
00188 {
00189 int i;
00190 for ( i = 1; i < 14; i++ )
00191 {
00192 if ( p_enc->fmt_out.i_bitrate / 1000
00193 <= mpa_bitrate_tab[i_frequency / 3][i] )
00194 break;
00195 }
00196 if ( p_enc->fmt_out.i_bitrate / 1000
00197 != mpa_bitrate_tab[i_frequency / 3][i] )
00198 {
00199 msg_Warn( p_enc, "MPEG audio doesn't support bitrate=%d, using %d",
00200 p_enc->fmt_out.i_bitrate,
00201 mpa_bitrate_tab[i_frequency / 3][i] * 1000 );
00202 p_enc->fmt_out.i_bitrate = mpa_bitrate_tab[i_frequency / 3][i]
00203 * 1000;
00204 }
00205
00206 twolame_set_bitrate( p_sys->p_twolame,
00207 p_enc->fmt_out.i_bitrate / 1000 );
00208 }
00209
00210 if ( p_enc->fmt_in.audio.i_channels == 1 )
00211 {
00212 twolame_set_num_channels( p_sys->p_twolame, 1 );
00213 twolame_set_mode( p_sys->p_twolame, TWOLAME_MONO );
00214 }
00215 else
00216 {
00217 twolame_set_num_channels( p_sys->p_twolame, 2 );
00218 var_Get( p_enc, ENC_CFG_PREFIX "mode", &val );
00219 switch ( val.i_int )
00220 {
00221 case 1:
00222 twolame_set_mode( p_sys->p_twolame, TWOLAME_DUAL_CHANNEL );
00223 break;
00224 case 2:
00225 twolame_set_mode( p_sys->p_twolame, TWOLAME_JOINT_STEREO );
00226 break;
00227 case 0:
00228 default:
00229 twolame_set_mode( p_sys->p_twolame, TWOLAME_STEREO );
00230 break;
00231 }
00232 }
00233
00234 var_Get( p_enc, ENC_CFG_PREFIX "psy", &val );
00235 twolame_set_psymodel( p_sys->p_twolame, val.i_int );
00236
00237 if ( twolame_init_params( p_sys->p_twolame ) )
00238 {
00239 msg_Err( p_enc, "twolame initialization failed" );
00240 return -VLC_EGENERIC;
00241 }
00242
00243 p_sys->i_nb_samples = 0;
00244
00245 return VLC_SUCCESS;
00246 }
00247
00248
00249
00250
00251
00252
00253 static void Bufferize( encoder_t *p_enc, int16_t *p_in, int i_nb_samples )
00254 {
00255 int16_t *p_buffer = p_enc->p_sys->p_buffer
00256 + (p_enc->p_sys->i_nb_samples
00257 * p_enc->fmt_in.audio.i_channels);
00258
00259 memcpy( p_buffer, p_in, i_nb_samples * p_enc->fmt_in.audio.i_channels
00260 * sizeof(int16_t) );
00261 }
00262
00263 static block_t *Encode( encoder_t *p_enc, aout_buffer_t *p_aout_buf )
00264 {
00265 encoder_sys_t *p_sys = p_enc->p_sys;
00266 int16_t *p_buffer = (int16_t *)p_aout_buf->p_buffer;
00267 int i_nb_samples = p_aout_buf->i_nb_samples;
00268 block_t *p_chain = NULL;
00269
00270 p_sys->i_pts = p_aout_buf->start_date -
00271 (mtime_t)1000000 * (mtime_t)p_sys->i_nb_samples /
00272 (mtime_t)p_enc->fmt_out.audio.i_rate;
00273
00274 while ( p_sys->i_nb_samples + i_nb_samples >= MPEG_FRAME_SIZE )
00275 {
00276 int i_used;
00277 block_t *p_block;
00278
00279 Bufferize( p_enc, p_buffer, MPEG_FRAME_SIZE - p_sys->i_nb_samples );
00280 i_nb_samples -= MPEG_FRAME_SIZE - p_sys->i_nb_samples;
00281 p_buffer += (MPEG_FRAME_SIZE - p_sys->i_nb_samples) * 2;
00282
00283 i_used = twolame_encode_buffer_interleaved( p_sys->p_twolame,
00284 p_sys->p_buffer, MPEG_FRAME_SIZE,
00285 p_sys->p_out_buffer, MAX_CODED_FRAME_SIZE );
00286 p_sys->i_nb_samples = 0;
00287 p_block = block_New( p_enc, i_used );
00288 p_enc->p_vlc->pf_memcpy( p_block->p_buffer, p_sys->p_out_buffer,
00289 i_used );
00290 p_block->i_length = (mtime_t)1000000 *
00291 (mtime_t)MPEG_FRAME_SIZE / (mtime_t)p_enc->fmt_out.audio.i_rate;
00292 p_block->i_dts = p_block->i_pts = p_sys->i_pts;
00293 p_sys->i_pts += p_block->i_length;
00294 block_ChainAppend( &p_chain, p_block );
00295 }
00296
00297 if ( i_nb_samples )
00298 {
00299 Bufferize( p_enc, p_buffer, i_nb_samples );
00300 p_sys->i_nb_samples += i_nb_samples;
00301 }
00302
00303 return p_chain;
00304 }
00305
00306
00307
00308
00309 static void CloseEncoder( vlc_object_t *p_this )
00310 {
00311 encoder_t *p_enc = (encoder_t *)p_this;
00312 encoder_sys_t *p_sys = p_enc->p_sys;
00313
00314 twolame_close( &p_sys->p_twolame );
00315
00316 free( p_sys );
00317 }