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