qml/KeyNavigableTableView: Add 'delegate' and 'contentY' aliases
[vlc.git] / modules / codec / twolame.c
blob4208ab100e3e39a9cb7c490aa775e407ffb099e3
1 /*****************************************************************************
2 * twolame.c: libtwolame encoder (MPEG-1/2 layer II) module
3 * (using libtwolame from http://www.twolame.org/)
4 *****************************************************************************
5 * Copyright (C) 2004-2005 VLC authors and VideoLAN
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * Gildas Bazin
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or
13 * (at your option) any later version.
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 Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
26 * Preamble
27 *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_codec.h>
36 #include <twolame.h>
38 #define MPEG_FRAME_SIZE 1152
39 #define MAX_CODED_FRAME_SIZE 1792
41 /****************************************************************************
42 * Local prototypes
43 ****************************************************************************/
44 static int OpenEncoder ( vlc_object_t * );
45 static void CloseEncoder ( vlc_object_t * );
46 static block_t *Encode ( encoder_t *, block_t * );
48 /*****************************************************************************
49 * Module descriptor
50 *****************************************************************************/
51 #define ENC_CFG_PREFIX "sout-twolame-"
53 #define ENC_QUALITY_TEXT N_("Encoding quality")
54 #define ENC_QUALITY_LONGTEXT N_( \
55 "Force a specific encoding quality between 0.0 (high) and 50.0 (low), " \
56 "instead of specifying a particular bitrate. " \
57 "This will produce a VBR stream." )
58 #define ENC_MODE_TEXT N_("Stereo mode")
59 #define ENC_MODE_LONGTEXT N_( "Handling mode for stereo streams" )
60 #define ENC_VBR_TEXT N_("VBR mode")
61 #define ENC_VBR_LONGTEXT N_( \
62 "Use Variable BitRate. Default is to use Constant BitRate (CBR)." )
63 #define ENC_PSY_TEXT N_("Psycho-acoustic model")
64 #define ENC_PSY_LONGTEXT N_( \
65 "Integer from -1 (no model) to 4." )
67 static const int pi_stereo_values[] = { 0, 1, 2 };
68 static const char *const ppsz_stereo_descriptions[] =
69 { N_("Stereo"), N_("Dual mono"), N_("Joint stereo") };
72 vlc_module_begin ()
73 set_shortname( "Twolame")
74 set_description( N_("Libtwolame audio encoder") )
75 set_capability( "encoder", 120 )
76 set_callbacks( OpenEncoder, CloseEncoder )
77 set_category( CAT_INPUT )
78 set_subcategory( SUBCAT_INPUT_ACODEC )
80 add_float( ENC_CFG_PREFIX "quality", 0.0, ENC_QUALITY_TEXT,
81 ENC_QUALITY_LONGTEXT, false )
82 add_integer( ENC_CFG_PREFIX "mode", 0, ENC_MODE_TEXT,
83 ENC_MODE_LONGTEXT, false )
84 change_integer_list( pi_stereo_values, ppsz_stereo_descriptions );
85 add_bool( ENC_CFG_PREFIX "vbr", false, ENC_VBR_TEXT,
86 ENC_VBR_LONGTEXT, false )
87 add_integer( ENC_CFG_PREFIX "psy", 3, ENC_PSY_TEXT,
88 ENC_PSY_LONGTEXT, false )
89 vlc_module_end ()
91 static const char *const ppsz_enc_options[] = {
92 "quality", "mode", "vbr", "psy", NULL
95 /*****************************************************************************
96 * encoder_sys_t : twolame encoder descriptor
97 *****************************************************************************/
98 typedef struct
101 * Input properties
103 int16_t p_buffer[MPEG_FRAME_SIZE * 2];
104 int i_nb_samples;
105 vlc_tick_t i_pts;
108 * libtwolame properties
110 twolame_options *p_twolame;
111 unsigned char p_out_buffer[MAX_CODED_FRAME_SIZE];
112 } encoder_sys_t;
114 /*****************************************************************************
115 * OpenEncoder: probe the encoder and return score
116 *****************************************************************************/
117 static const uint16_t mpa_bitrate_tab[2][15] =
119 {0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384},
120 {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160}
123 static const uint16_t mpa_freq_tab[6] =
124 { 44100, 48000, 32000, 22050, 24000, 16000 };
126 static int OpenEncoder( vlc_object_t *p_this )
128 encoder_t *p_enc = (encoder_t *)p_this;
129 encoder_sys_t *p_sys;
130 int i_frequency;
132 if( p_enc->fmt_out.i_codec != VLC_CODEC_MP2 &&
133 p_enc->fmt_out.i_codec != VLC_CODEC_MPGA &&
134 p_enc->fmt_out.i_codec != VLC_FOURCC( 'm', 'p', '2', 'a' ) &&
135 !p_enc->obj.force )
137 return VLC_EGENERIC;
140 if( p_enc->fmt_in.audio.i_channels > 2 )
142 msg_Err( p_enc, "doesn't support > 2 channels" );
143 return VLC_EGENERIC;
146 for ( i_frequency = 0; i_frequency < 6; i_frequency++ )
148 if ( p_enc->fmt_out.audio.i_rate == mpa_freq_tab[i_frequency] )
149 break;
151 if ( i_frequency == 6 )
153 msg_Err( p_enc, "MPEG audio doesn't support frequency=%d",
154 p_enc->fmt_out.audio.i_rate );
155 return VLC_EGENERIC;
158 /* Allocate the memory needed to store the decoder's structure */
159 if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
160 return VLC_ENOMEM;
161 p_enc->p_sys = p_sys;
163 p_enc->fmt_in.i_codec = VLC_CODEC_S16N;
165 p_enc->fmt_out.i_cat = AUDIO_ES;
166 p_enc->fmt_out.i_codec = VLC_CODEC_MPGA;
168 config_ChainParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
170 p_sys->p_twolame = twolame_init();
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 );
176 if( var_GetBool( p_enc, ENC_CFG_PREFIX "vbr" ) )
178 float f_quality = var_GetFloat( p_enc, ENC_CFG_PREFIX "quality" );
179 if ( f_quality > 50.f ) f_quality = 50.f;
180 if ( f_quality < 0.f ) f_quality = 0.f;
181 twolame_set_VBR( p_sys->p_twolame, 1 );
182 twolame_set_VBR_q( p_sys->p_twolame, f_quality );
184 else
186 int i;
187 for ( i = 1; i < 14; i++ )
189 if ( p_enc->fmt_out.i_bitrate / 1000
190 <= mpa_bitrate_tab[i_frequency / 3][i] )
191 break;
193 if ( p_enc->fmt_out.i_bitrate / 1000
194 != mpa_bitrate_tab[i_frequency / 3][i] )
196 msg_Warn( p_enc, "MPEG audio doesn't support bitrate=%d, using %d",
197 p_enc->fmt_out.i_bitrate,
198 mpa_bitrate_tab[i_frequency / 3][i] * 1000 );
199 p_enc->fmt_out.i_bitrate = mpa_bitrate_tab[i_frequency / 3][i]
200 * 1000;
203 twolame_set_bitrate( p_sys->p_twolame,
204 p_enc->fmt_out.i_bitrate / 1000 );
207 if ( p_enc->fmt_in.audio.i_channels == 1 )
209 twolame_set_num_channels( p_sys->p_twolame, 1 );
210 twolame_set_mode( p_sys->p_twolame, TWOLAME_MONO );
212 else
214 twolame_set_num_channels( p_sys->p_twolame, 2 );
215 switch( var_GetInteger( p_enc, ENC_CFG_PREFIX "mode" ) )
217 case 1:
218 twolame_set_mode( p_sys->p_twolame, TWOLAME_DUAL_CHANNEL );
219 break;
220 case 2:
221 twolame_set_mode( p_sys->p_twolame, TWOLAME_JOINT_STEREO );
222 break;
223 case 0:
224 default:
225 twolame_set_mode( p_sys->p_twolame, TWOLAME_STEREO );
226 break;
230 twolame_set_psymodel( p_sys->p_twolame,
231 var_GetInteger( p_enc, ENC_CFG_PREFIX "psy" ) );
233 if ( twolame_init_params( p_sys->p_twolame ) )
235 msg_Err( p_enc, "twolame initialization failed" );
236 return -VLC_EGENERIC;
239 p_enc->pf_encode_audio = Encode;
241 p_sys->i_nb_samples = 0;
243 return VLC_SUCCESS;
246 /****************************************************************************
247 * Encode: the whole thing
248 ****************************************************************************
249 * This function spits out MPEG packets.
250 ****************************************************************************/
251 static void Bufferize( encoder_t *p_enc, int16_t *p_in, int i_nb_samples )
253 encoder_sys_t *p_sys = p_enc->p_sys;
254 const unsigned i_offset = p_sys->i_nb_samples * p_enc->fmt_in.audio.i_channels;
255 const unsigned i_len = ARRAY_SIZE(p_sys->p_buffer);
257 if( i_offset >= i_len )
259 msg_Err( p_enc, "buffer full" );
260 return;
263 unsigned i_copy = i_nb_samples * p_enc->fmt_in.audio.i_channels;
264 if( i_copy + i_offset > i_len)
266 msg_Err( p_enc, "dropping samples" );
267 i_copy = i_len - i_offset;
270 memcpy( p_sys->p_buffer + i_offset, p_in, i_copy * sizeof(int16_t) );
273 static block_t *Encode( encoder_t *p_enc, block_t *p_aout_buf )
275 encoder_sys_t *p_sys = p_enc->p_sys;
276 block_t *p_chain = NULL;
278 if( unlikely( !p_aout_buf ) ) {
279 int i_used = 0;
280 block_t *p_block;
282 i_used = twolame_encode_flush( p_sys->p_twolame,
283 p_sys->p_out_buffer, MAX_CODED_FRAME_SIZE );
284 if( i_used <= 0 )
285 return NULL;
287 p_block = block_Alloc( i_used );
288 if( !p_block )
289 return NULL;
290 memcpy( p_block->p_buffer, p_sys->p_out_buffer, i_used );
291 p_block->i_length = vlc_tick_from_samples( MPEG_FRAME_SIZE,
292 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;
296 return p_block;
299 int16_t *p_buffer = (int16_t *)p_aout_buf->p_buffer;
300 int i_nb_samples = p_aout_buf->i_nb_samples;
302 p_sys->i_pts = p_aout_buf->i_pts -
303 vlc_tick_from_samples( p_sys->i_nb_samples,
304 p_enc->fmt_out.audio.i_rate );
306 while ( p_sys->i_nb_samples + i_nb_samples >= MPEG_FRAME_SIZE )
308 int i_used;
309 block_t *p_block;
311 Bufferize( p_enc, p_buffer, MPEG_FRAME_SIZE - p_sys->i_nb_samples );
312 i_nb_samples -= MPEG_FRAME_SIZE - p_sys->i_nb_samples;
313 p_buffer += (MPEG_FRAME_SIZE - p_sys->i_nb_samples) * 2;
315 i_used = twolame_encode_buffer_interleaved( p_sys->p_twolame,
316 p_sys->p_buffer, MPEG_FRAME_SIZE,
317 p_sys->p_out_buffer, MAX_CODED_FRAME_SIZE );
318 /* On error, buffer samples and return what was already encoded */
319 if( i_used < 0 )
321 msg_Err( p_enc, "encoder error: %d", i_used );
322 break;
325 p_sys->i_nb_samples = 0;
326 p_block = block_Alloc( i_used );
327 if( !p_block )
329 if( p_chain )
330 block_ChainRelease( p_chain );
331 return NULL;
333 memcpy( p_block->p_buffer, p_sys->p_out_buffer, i_used );
334 p_block->i_length = vlc_tick_from_samples( MPEG_FRAME_SIZE,
335 p_enc->fmt_out.audio.i_rate );
336 p_block->i_dts = p_block->i_pts = p_sys->i_pts;
337 p_sys->i_pts += p_block->i_length;
338 block_ChainAppend( &p_chain, p_block );
341 if ( i_nb_samples )
343 Bufferize( p_enc, p_buffer, i_nb_samples );
344 p_sys->i_nb_samples += i_nb_samples;
347 return p_chain;
350 /*****************************************************************************
351 * CloseEncoder: twolame encoder destruction
352 *****************************************************************************/
353 static void CloseEncoder( vlc_object_t *p_this )
355 encoder_t *p_enc = (encoder_t *)p_this;
356 encoder_sys_t *p_sys = p_enc->p_sys;
358 twolame_close( &p_sys->p_twolame );
360 free( p_sys );