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