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