]> git.sesse.net Git - vlc/blob - modules/codec/twolame.c
FSF address change.
[vlc] / modules / codec / twolame.c
1 /*****************************************************************************
2  * twolame.c: libtwolame encoder (MPEG-1/2 layer II) module
3  *            (using libtwolame from http://users.tpg.com.au/adslblvi/)
4  *****************************************************************************
5  * Copyright (C) 2004-2005 the VideoLAN team
6  * $Id$
7  *
8  * Authors: Christophe Massiot <massiot@via.ecp.fr>
9  *
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.
14  *
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.
19  *
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <vlc/vlc.h>
29 #include <vlc/decoder.h>
30 #include <vlc/sout.h>
31 #include <vlc/aout.h>
32
33 #include <twolame.h>
34
35 #define MPEG_FRAME_SIZE 1152
36 #define MAX_CODED_FRAME_SIZE 1792
37
38 /****************************************************************************
39  * Local prototypes
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 * );
44
45 /*****************************************************************************
46  * Module descriptor
47  *****************************************************************************/
48 #define ENC_CFG_PREFIX "sout-twolame-"
49
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_( "Select how stereo streams will be handled" )
57 #define ENC_VBR_TEXT N_("VBR mode")
58 #define ENC_VBR_LONGTEXT N_( \
59   "By default the encoding is CBR." )
60 #define ENC_PSY_TEXT N_("Psycho-acoustic model")
61 #define ENC_PSY_LONGTEXT N_( \
62   "Integer from -1 (no model) to 4." )
63
64 static int pi_stereo_values[] = { 0, 1, 2 };
65 static char *ppsz_stereo_descriptions[] =
66 { N_("Stereo"), N_("Dual mono"), N_("Joint stereo") };
67
68
69 vlc_module_begin();
70     set_shortname( "Twolame");
71     set_description( _("Libtwolame audio encoder") );
72     set_capability( "encoder", 50 );
73     set_callbacks( OpenEncoder, CloseEncoder );
74     set_category( CAT_INPUT );
75     set_subcategory( SUBCAT_INPUT_ACODEC );
76
77     add_float( ENC_CFG_PREFIX "quality", 0.0, NULL, ENC_QUALITY_TEXT,
78                ENC_QUALITY_LONGTEXT, VLC_FALSE );
79     add_integer( ENC_CFG_PREFIX "mode", 0, NULL, ENC_MODE_TEXT,
80                  ENC_MODE_LONGTEXT, VLC_FALSE );
81         change_integer_list( pi_stereo_values, ppsz_stereo_descriptions, 0 );
82     add_bool( ENC_CFG_PREFIX "vbr", 0, NULL, ENC_VBR_TEXT,
83               ENC_VBR_LONGTEXT, VLC_FALSE );
84     add_integer( ENC_CFG_PREFIX "psy", 3, NULL, ENC_PSY_TEXT,
85                  ENC_PSY_LONGTEXT, VLC_FALSE );
86 vlc_module_end();
87
88 static const char *ppsz_enc_options[] = {
89     "quality", "mode", "vbr", "psy", NULL
90 };
91
92 /*****************************************************************************
93  * encoder_sys_t : twolame encoder descriptor
94  *****************************************************************************/
95 struct encoder_sys_t
96 {
97     /*
98      * Input properties
99      */
100     int16_t p_buffer[MPEG_FRAME_SIZE * 2];
101     int i_nb_samples;
102     mtime_t i_pts;
103
104     /*
105      * libtwolame properties
106      */
107     twolame_options *p_twolame;
108     unsigned char p_out_buffer[MAX_CODED_FRAME_SIZE];
109 };
110
111 /*****************************************************************************
112  * OpenEncoder: probe the encoder and return score
113  *****************************************************************************/
114 static const uint16_t mpa_bitrate_tab[2][15] =
115 {
116     {0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384},
117     {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160}
118 };
119
120 static const uint16_t mpa_freq_tab[6] =
121 { 44100, 48000, 32000, 22050, 24000, 16000 };
122
123 static int OpenEncoder( vlc_object_t *p_this )
124 {
125     encoder_t *p_enc = (encoder_t *)p_this;
126     encoder_sys_t *p_sys;
127     vlc_value_t val;
128     int i_frequency;
129
130     if( p_enc->fmt_out.i_codec != VLC_FOURCC('m','p','g','a') &&
131         p_enc->fmt_out.i_codec != VLC_FOURCC('m','p','2','a') &&
132         p_enc->fmt_out.i_codec != VLC_FOURCC('m','p','2',' ') &&
133         !p_enc->b_force )
134     {
135         return VLC_EGENERIC;
136     }
137
138     if( p_enc->fmt_in.audio.i_channels > 2 )
139     {
140         msg_Err( p_enc, "doesn't support > 2 channels" );
141         return VLC_EGENERIC;
142     }
143
144     for ( i_frequency = 0; i_frequency < 6; i_frequency++ )
145     {
146         if ( p_enc->fmt_out.audio.i_rate == mpa_freq_tab[i_frequency] )
147             break;
148     }
149     if ( i_frequency == 6 )
150     {
151         msg_Err( p_enc, "MPEG audio doesn't support frequency=%d",
152                  p_enc->fmt_out.audio.i_rate );
153         return VLC_EGENERIC;
154     }
155
156     /* Allocate the memory needed to store the decoder's structure */
157     if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
158     {
159         msg_Err( p_enc, "out of memory" );
160         return VLC_EGENERIC;
161     }
162     p_enc->p_sys = p_sys;
163
164     p_enc->pf_encode_audio = Encode;
165     p_enc->fmt_in.i_codec = AOUT_FMT_S16_NE;
166     p_enc->fmt_out.i_codec = VLC_FOURCC('m','p','g','a');
167
168     sout_CfgParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
169
170     p_sys->p_twolame = twolame_init();
171
172     /* Set options */
173     twolame_set_in_samplerate( p_sys->p_twolame, p_enc->fmt_out.audio.i_rate );
174     twolame_set_out_samplerate( p_sys->p_twolame, p_enc->fmt_out.audio.i_rate );
175
176     var_Get( p_enc, ENC_CFG_PREFIX "vbr", &val );
177     if ( val.b_bool )
178     {
179         float i_quality;
180         var_Get( p_enc, ENC_CFG_PREFIX "quality", &val );
181         i_quality = val.i_int;
182         if ( i_quality > 50.0 ) i_quality = 50.0;
183         if ( i_quality < 0.0 ) i_quality = 0.0;
184         twolame_set_VBR( p_sys->p_twolame, 1 );
185         twolame_set_VBR_q( p_sys->p_twolame, i_quality );
186     }
187     else
188     {
189         int i;
190         for ( i = 1; i < 14; i++ )
191         {
192             if ( p_enc->fmt_out.i_bitrate / 1000
193                   <= mpa_bitrate_tab[i_frequency / 3][i] )
194                 break;
195         }
196         if ( p_enc->fmt_out.i_bitrate / 1000
197               != mpa_bitrate_tab[i_frequency / 3][i] )
198         {
199             msg_Warn( p_enc, "MPEG audio doesn't support bitrate=%d, using %d",
200                       p_enc->fmt_out.i_bitrate,
201                       mpa_bitrate_tab[i_frequency / 3][i] * 1000 );
202             p_enc->fmt_out.i_bitrate = mpa_bitrate_tab[i_frequency / 3][i]
203                                         * 1000;
204         }
205
206         twolame_set_bitrate( p_sys->p_twolame,
207                              p_enc->fmt_out.i_bitrate / 1000 );
208     }
209
210     if ( p_enc->fmt_in.audio.i_channels == 1 )
211     {
212         twolame_set_num_channels( p_sys->p_twolame, 1 );
213         twolame_set_mode( p_sys->p_twolame, TWOLAME_MONO );
214     }
215     else
216     {
217         twolame_set_num_channels( p_sys->p_twolame, 2 );
218         var_Get( p_enc, ENC_CFG_PREFIX "mode", &val );
219         switch ( val.i_int )
220         {
221         case 1:
222             twolame_set_mode( p_sys->p_twolame, TWOLAME_DUAL_CHANNEL );
223             break;
224         case 2:
225             twolame_set_mode( p_sys->p_twolame, TWOLAME_JOINT_STEREO );
226             break;
227         case 0:
228         default:
229             twolame_set_mode( p_sys->p_twolame, TWOLAME_STEREO );
230             break;
231         }
232     }
233
234     var_Get( p_enc, ENC_CFG_PREFIX "psy", &val );
235     twolame_set_psymodel( p_sys->p_twolame, val.i_int );
236
237     if ( twolame_init_params( p_sys->p_twolame ) )
238     {
239         msg_Err( p_enc, "twolame initialization failed" );
240         return -VLC_EGENERIC;
241     }
242
243     p_sys->i_nb_samples = 0;
244
245     return VLC_SUCCESS;
246 }
247
248 /****************************************************************************
249  * Encode: the whole thing
250  ****************************************************************************
251  * This function spits out MPEG packets.
252  ****************************************************************************/
253 static void Bufferize( encoder_t *p_enc, int16_t *p_in, int i_nb_samples )
254 {
255     int16_t *p_buffer = p_enc->p_sys->p_buffer
256                          + (p_enc->p_sys->i_nb_samples
257                              * p_enc->fmt_in.audio.i_channels);
258
259     memcpy( p_buffer, p_in, i_nb_samples * p_enc->fmt_in.audio.i_channels
260                              * sizeof(int16_t) );
261 }
262
263 static block_t *Encode( encoder_t *p_enc, aout_buffer_t *p_aout_buf )
264 {
265     encoder_sys_t *p_sys = p_enc->p_sys;
266     int16_t *p_buffer = (int16_t *)p_aout_buf->p_buffer;
267     int i_nb_samples = p_aout_buf->i_nb_samples;
268     block_t *p_chain = NULL;
269
270     p_sys->i_pts = p_aout_buf->start_date -
271                 (mtime_t)1000000 * (mtime_t)p_sys->i_nb_samples /
272                 (mtime_t)p_enc->fmt_out.audio.i_rate;
273
274     while ( p_sys->i_nb_samples + i_nb_samples >= MPEG_FRAME_SIZE )
275     {
276         int i_used;
277         block_t *p_block;
278
279         Bufferize( p_enc, p_buffer, MPEG_FRAME_SIZE - p_sys->i_nb_samples );
280         i_nb_samples -= MPEG_FRAME_SIZE - p_sys->i_nb_samples;
281         p_buffer += (MPEG_FRAME_SIZE - p_sys->i_nb_samples) * 2;
282
283         i_used = twolame_encode_buffer_interleaved( p_sys->p_twolame,
284                                p_sys->p_buffer, MPEG_FRAME_SIZE,
285                                p_sys->p_out_buffer, MAX_CODED_FRAME_SIZE );
286         p_sys->i_nb_samples = 0;
287         p_block = block_New( p_enc, i_used );
288         p_enc->p_vlc->pf_memcpy( p_block->p_buffer, p_sys->p_out_buffer,
289                                  i_used );
290         p_block->i_length = (mtime_t)1000000 *
291                 (mtime_t)MPEG_FRAME_SIZE / (mtime_t)p_enc->fmt_out.audio.i_rate;
292         p_block->i_dts = p_block->i_pts = p_sys->i_pts;
293         p_sys->i_pts += p_block->i_length;
294         block_ChainAppend( &p_chain, p_block );
295     }
296
297     if ( i_nb_samples )
298     {
299         Bufferize( p_enc, p_buffer, i_nb_samples );
300         p_sys->i_nb_samples += i_nb_samples;
301     }
302
303     return p_chain;
304 }
305
306 /*****************************************************************************
307  * CloseEncoder: twolame encoder destruction
308  *****************************************************************************/
309 static void CloseEncoder( vlc_object_t *p_this )
310 {
311     encoder_t *p_enc = (encoder_t *)p_this;
312     encoder_sys_t *p_sys = p_enc->p_sys;
313
314     twolame_close( &p_sys->p_twolame );
315
316     free( p_sys );
317 }