]> git.sesse.net Git - vlc/blob - modules/codec/toolame.c
* record: use add_directory (instead of add_string) + parse the name to
[vlc] / modules / codec / toolame.c
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
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., 59 Temple Place - Suite 330, Boston, MA  02111, 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 <toolame.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-toolame-"
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_( \
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." )
61
62 vlc_module_begin();
63     set_shortname( "toolame");
64     set_description( _("Libtoolame audio encoder") );
65     set_capability( "encoder", 50 );
66     set_callbacks( OpenEncoder, CloseEncoder );
67     set_category( CAT_INPUT );
68     set_subcategory( SUBCAT_INPUT_ACODEC );
69
70     add_float( ENC_CFG_PREFIX "quality", 0.0, NULL, ENC_QUALITY_TEXT,
71                ENC_QUALITY_LONGTEXT, VLC_FALSE );
72     add_integer( ENC_CFG_PREFIX "mode", 0, NULL, ENC_MODE_TEXT,
73                  ENC_MODE_LONGTEXT, VLC_FALSE );
74     add_bool( ENC_CFG_PREFIX "vbr", 0, NULL, ENC_VBR_TEXT,
75               ENC_VBR_LONGTEXT, VLC_FALSE );
76 vlc_module_end();
77
78 static const char *ppsz_enc_options[] = {
79     "quality", "mode", "vbr", NULL
80 };
81
82 /*****************************************************************************
83  * encoder_sys_t : toolame encoder descriptor
84  *****************************************************************************/
85 struct encoder_sys_t
86 {
87     /*
88      * Input properties
89      */
90     int16_t p_left[MPEG_FRAME_SIZE];
91     int16_t p_right[MPEG_FRAME_SIZE];
92     int i_nb_samples;
93     audio_date_t pts;
94
95     /*
96      * libtoolame properties
97      */
98     toolame_options *p_toolame;
99     unsigned char p_out_buffer[MAX_CODED_FRAME_SIZE];
100 };
101
102 /*****************************************************************************
103  * OpenEncoder: probe the encoder and return score
104  *****************************************************************************/
105 static int OpenEncoder( vlc_object_t *p_this )
106 {
107     encoder_t *p_enc = (encoder_t *)p_this;
108     encoder_sys_t *p_sys;
109     vlc_value_t val;
110
111     if( p_enc->fmt_out.i_codec != VLC_FOURCC('m','p','g','a') &&
112         p_enc->fmt_out.i_codec != VLC_FOURCC('m','p','2','a') &&
113         p_enc->fmt_out.i_codec != VLC_FOURCC('m','p','2',' ') &&
114         !p_enc->b_force )
115     {
116         return VLC_EGENERIC;
117     }
118
119     if( p_enc->fmt_in.audio.i_channels > 2 )
120     {
121         msg_Err( p_enc, "doesn't support > 2 channels" );
122         return VLC_EGENERIC;
123     }
124
125     /* Allocate the memory needed to store the decoder's structure */
126     if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
127     {
128         msg_Err( p_enc, "out of memory" );
129         return VLC_EGENERIC;
130     }
131     p_enc->p_sys = p_sys;
132
133     p_enc->pf_encode_audio = Encode;
134     p_enc->fmt_in.i_codec = AOUT_FMT_S16_NE;
135     p_enc->fmt_out.i_codec = VLC_FOURCC('m','p','g','a');
136
137     sout_CfgParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
138
139     p_sys->p_toolame = toolame_init();
140
141     /* Set options */
142     toolame_setSampleFreq( p_sys->p_toolame, p_enc->fmt_out.audio.i_rate );
143
144     var_Get( p_enc, ENC_CFG_PREFIX "vbr", &val );
145     if ( val.b_bool )
146     {
147         FLOAT i_quality;
148         var_Get( p_enc, ENC_CFG_PREFIX "quality", &val );
149         i_quality = val.i_int;
150         if ( i_quality > 50.0 ) i_quality = 50.0;
151         if ( i_quality < 0.0 ) i_quality = 0.0;
152         toolame_setVBR( p_sys->p_toolame, 1 );
153         toolame_setVBRLevel( p_sys->p_toolame, i_quality );
154     }
155     else
156     {
157         toolame_setBitrate( p_sys->p_toolame, p_enc->fmt_out.i_bitrate / 1000 );
158     }
159
160     if ( p_enc->fmt_in.audio.i_channels == 1 )
161     {
162         toolame_setMode( p_sys->p_toolame, MPG_MD_MONO );
163     }
164     else
165     {
166         var_Get( p_enc, ENC_CFG_PREFIX "mode", &val );
167         switch ( val.i_int )
168         {
169         case 1:
170             toolame_setMode( p_sys->p_toolame, MPG_MD_DUAL_CHANNEL );
171             break;
172         case 2:
173             toolame_setMode( p_sys->p_toolame, MPG_MD_JOINT_STEREO );
174             break;
175         case 0:
176         default:
177             toolame_setMode( p_sys->p_toolame, MPG_MD_STEREO );
178             break;
179         }
180     }
181
182     if ( toolame_init_params( p_sys->p_toolame ) )
183     {
184         msg_Err( p_enc, "toolame initialization failed" );
185         return -VLC_EGENERIC;
186     }
187
188     p_sys->i_nb_samples = 0;
189     aout_DateInit( &p_sys->pts, p_enc->fmt_out.audio.i_rate );
190
191     return VLC_SUCCESS;
192 }
193
194 /****************************************************************************
195  * Encode: the whole thing
196  ****************************************************************************
197  * This function spits out MPEG packets.
198  ****************************************************************************/
199 static void Uninterleave( encoder_t *p_enc, int16_t *p_in, int i_nb_samples )
200 {
201     int16_t *p_left = p_enc->p_sys->p_left + p_enc->p_sys->i_nb_samples;
202     int16_t *p_right = p_enc->p_sys->p_right + p_enc->p_sys->i_nb_samples;
203
204     while ( i_nb_samples > 0 )
205     {
206         *p_left++ = *p_in++;
207         *p_right++ = *p_in++;
208         i_nb_samples--;
209     }
210 }
211
212 static block_t *Encode( encoder_t *p_enc, aout_buffer_t *p_aout_buf )
213 {
214     encoder_sys_t *p_sys = p_enc->p_sys;
215     int16_t *p_buffer = (int16_t *)p_aout_buf->p_buffer;
216     int i_nb_samples = p_aout_buf->i_nb_samples;
217     block_t *p_chain = NULL;
218     mtime_t i_computed_pts = p_aout_buf->start_date -
219                 (mtime_t)1000000 * (mtime_t)p_sys->i_nb_samples /
220                 (mtime_t)p_enc->fmt_in.audio.i_rate;
221
222     if ( aout_DateGet( &p_sys->pts ) - i_computed_pts > 10000 ||
223          aout_DateGet( &p_sys->pts ) - i_computed_pts < -10000 )
224     {
225         msg_Dbg( p_enc, "resetting audio date" );
226         aout_DateSet( &p_sys->pts, i_computed_pts );
227     }
228
229     while ( p_sys->i_nb_samples + i_nb_samples >= MPEG_FRAME_SIZE )
230     {
231         int i_used;
232         block_t *p_block;
233
234         Uninterleave( p_enc, p_buffer, MPEG_FRAME_SIZE - p_sys->i_nb_samples );
235         i_nb_samples -= MPEG_FRAME_SIZE - p_sys->i_nb_samples;
236         p_buffer += (MPEG_FRAME_SIZE - p_sys->i_nb_samples) * 2;
237
238         toolame_encode_buffer( p_sys->p_toolame, p_sys->p_left,
239                                p_sys->p_right, MPEG_FRAME_SIZE,
240                                p_sys->p_out_buffer, MAX_CODED_FRAME_SIZE,
241                                &i_used );
242         p_sys->i_nb_samples = 0;
243         p_block = block_New( p_enc, i_used );
244         p_enc->p_vlc->pf_memcpy( p_block->p_buffer, p_sys->p_out_buffer,
245                                  i_used );
246         p_block->i_length = (mtime_t)1000000 *
247                 (mtime_t)MPEG_FRAME_SIZE / (mtime_t)p_enc->fmt_in.audio.i_rate;
248         p_block->i_dts = p_block->i_pts = aout_DateGet( &p_sys->pts );
249         aout_DateIncrement( &p_sys->pts, MPEG_FRAME_SIZE );
250         block_ChainAppend( &p_chain, p_block );
251     }
252
253     if ( i_nb_samples )
254     {
255         Uninterleave( p_enc, p_buffer, i_nb_samples );
256         p_sys->i_nb_samples += i_nb_samples;
257     }
258
259     return p_chain;
260 }
261
262 /*****************************************************************************
263  * CloseEncoder: toolame encoder destruction
264  *****************************************************************************/
265 static void CloseEncoder( vlc_object_t *p_this )
266 {
267     encoder_t *p_enc = (encoder_t *)p_this;
268     encoder_sys_t *p_sys = p_enc->p_sys;
269
270     toolame_deinit( p_sys->p_toolame );
271
272     free( p_sys );
273 }